Consistency
[llpp.git] / main.ml
blob798336b727a0d10bb667150a46189f6abcbcc532
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
157 ; mutable jumpback : bool
161 type outline = string * int * int * float;;
162 type outlines =
163 | Oarray of outline array
164 | Olist of outline list
165 | Onarrow of string * outline array * outline array
168 type rect = float * float * float * float * float * float * float * float;;
170 type pagemapkey = pageno * width * angle * proportional * gen;;
172 type anchor = pageno * top;;
174 let emptyanchor = (0, 0.0);;
175 let initialanchor = (-1, nan);;
177 type mode =
178 | Birdseye of (conf * leftx * pageno * pageno * anchor)
179 | Outline of (bool * int * int * outline array * string * int)
180 | Textentry of (textentry * mode)
181 | View
184 let isbirdseye = function Birdseye _ -> true | _ -> false;;
185 let istextentry = function Textentry _ -> true | _ -> false;;
187 type state =
188 { mutable csock : Unix.file_descr
189 ; mutable ssock : Unix.file_descr
190 ; mutable w : int
191 ; mutable x : int
192 ; mutable y : int
193 ; mutable anchor : anchor
194 ; mutable maxy : int
195 ; mutable layout : layout list
196 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
197 ; mutable pdims : (pageno * width * height * leftx) list
198 ; mutable pagecount : int
199 ; pagecache : string circbuf
200 ; mutable rendering : bool
201 ; mutable mstate : mstate
202 ; mutable searchpattern : string
203 ; mutable rects : (pageno * recttype * rect) list
204 ; mutable rects1 : (pageno * recttype * rect) list
205 ; mutable text : string
206 ; mutable fullscreen : (width * height) option
207 ; mutable mode : mode
208 ; mutable outlines : outlines
209 ; mutable bookmarks : outline list
210 ; mutable path : string
211 ; mutable password : string
212 ; mutable invalidated : int
213 ; mutable colorscale : float
214 ; mutable memused : int
215 ; mutable gen : gen
216 ; mutable throttle : layout list option
217 ; mutable ascrollstep : int
218 ; mutable help : string list
219 ; mutable docinfo : (int * string) list
220 ; hists : hists
222 and hists =
223 { pat : string circbuf
224 ; pag : string circbuf
225 ; nav : anchor circbuf
229 let defconf =
230 { scrollw = 7
231 ; scrollh = 12
232 ; icase = true
233 ; preload = true
234 ; pagebias = 0
235 ; verbose = false
236 ; scrollstep = 24
237 ; maxhfit = true
238 ; crophack = false
239 ; autoscrollstep = 24
240 ; showall = false
241 ; hlinks = false
242 ; underinfo = false
243 ; interpagespace = 2
244 ; zoom = 1.0
245 ; presentation = false
246 ; angle = 0
247 ; winw = 900
248 ; winh = 900
249 ; savebmarks = true
250 ; proportional = true
251 ; memlimit = 32*1024*1024
252 ; texcount = 256
253 ; sliceheight = 24
254 ; thumbw = 76
255 ; jumpback = false
259 let conf = { defconf with angle = defconf.angle };;
261 let state =
262 { csock = Unix.stdin
263 ; ssock = Unix.stdin
264 ; x = 0
265 ; y = 0
266 ; anchor = initialanchor
267 ; w = 0
268 ; layout = []
269 ; maxy = max_int
270 ; pagemap = Hashtbl.create 10
271 ; pagecache = cbnew 100 ""
272 ; pdims = []
273 ; pagecount = 0
274 ; rendering = false
275 ; mstate = Mnone
276 ; rects = []
277 ; rects1 = []
278 ; text = ""
279 ; mode = View
280 ; fullscreen = None
281 ; searchpattern = ""
282 ; outlines = Olist []
283 ; bookmarks = []
284 ; path = ""
285 ; password = ""
286 ; invalidated = 0
287 ; hists =
288 { nav = cbnew 100 (0, 0.0)
289 ; pat = cbnew 20 ""
290 ; pag = cbnew 10 ""
292 ; colorscale = 1.0
293 ; memused = 0
294 ; gen = 0
295 ; throttle = None
296 ; ascrollstep = 0
297 ; help = Help.keys
298 ; docinfo = []
302 let vlog fmt =
303 if conf.verbose
304 then
305 Printf.kprintf prerr_endline fmt
306 else
307 Printf.kprintf ignore fmt
310 let writecmd fd s =
311 let len = String.length s in
312 let n = 4 + len in
313 let b = Buffer.create n in
314 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
315 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
316 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
317 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
318 Buffer.add_string b s;
319 let s' = Buffer.contents b in
320 let n' = Unix.write fd s' 0 n in
321 if n' != n then failwith "write failed";
324 let readcmd fd =
325 let s = "xxxx" in
326 let n = Unix.read fd s 0 4 in
327 if n != 4 then failwith "incomplete read(len)";
328 let len = 0
329 lor (Char.code s.[0] lsl 24)
330 lor (Char.code s.[1] lsl 16)
331 lor (Char.code s.[2] lsl 8)
332 lor (Char.code s.[3] lsl 0)
334 let s = String.create len in
335 let n = Unix.read fd s 0 len in
336 if n != len then failwith "incomplete read(data)";
340 let makecmd s l =
341 let b = Buffer.create 10 in
342 Buffer.add_string b s;
343 let rec combine = function
344 | [] -> b
345 | x :: xs ->
346 Buffer.add_char b ' ';
347 let s =
348 match x with
349 | `b b -> if b then "1" else "0"
350 | `s s -> s
351 | `i i -> string_of_int i
352 | `f f -> string_of_float f
353 | `I f -> string_of_int (truncate f)
355 Buffer.add_string b s;
356 combine xs;
358 combine l;
361 let wcmd s l =
362 let cmd = Buffer.contents (makecmd s l) in
363 writecmd state.csock cmd;
366 let calcips h =
367 if conf.presentation
368 then
369 let d = conf.winh - h in
370 max 0 ((d + 1) / 2)
371 else
372 conf.interpagespace
375 let calcheight () =
376 let rec f pn ph pi fh l =
377 match l with
378 | (n, _, h, _) :: rest ->
379 let ips = calcips h in
380 let fh =
381 if conf.presentation
382 then fh+ips
383 else (
384 if isbirdseye state.mode && pn = 0
385 then fh + ips
386 else fh
389 let fh = fh + ((n - pn) * (ph + pi)) in
390 f n h ips fh rest;
392 | [] ->
393 let inc =
394 if conf.presentation || (isbirdseye state.mode && pn = 0)
395 then 0
396 else -pi
398 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
399 max 0 fh
401 let fh = f 0 0 0 0 state.pdims in
405 let getpageyh pageno =
406 let rec f pn ph pi y l =
407 match l with
408 | (n, _, h, _) :: rest ->
409 let ips = calcips h in
410 if n >= pageno
411 then
412 let h = if n = pageno then h else ph in
413 if conf.presentation && n = pageno
414 then
415 y + (pageno - pn) * (ph + pi) + pi, h
416 else
417 y + (pageno - pn) * (ph + pi), h
418 else
419 let y = y + (if conf.presentation then pi else 0) in
420 let y = y + (n - pn) * (ph + pi) in
421 f n h ips y rest
423 | [] ->
424 y + (pageno - pn) * (ph + pi), ph
426 f 0 0 0 0 state.pdims
429 let getpagey pageno = fst (getpageyh pageno);;
431 let layout y sh =
432 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
433 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
434 match pdims with
435 | (pageno', w, h, x) :: rest when pageno' = pageno ->
436 let ips = calcips h in
437 let yinc =
438 if conf.presentation || (isbirdseye state.mode && pageno = 0)
439 then ips
440 else 0
442 (w, h, ips, x), rest, pdimno + 1, yinc
443 | _ ->
444 prev, pdims, pdimno, 0
446 let dy = dy + yinc in
447 let py = py + yinc in
448 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
449 then
450 accu
451 else
452 let vy = y + dy in
453 if py + h <= vy - yinc
454 then
455 let py = py + h + ips in
456 let dy = max 0 (py - y) in
457 f ~pageno:(pageno+1)
458 ~pdimno
459 ~prev:curr
462 ~pdims:rest
463 ~cacheleft
464 ~accu
465 else
466 let pagey = vy - py in
467 let pagevh = h - pagey in
468 let pagevh = min (sh - dy) pagevh in
469 let off = if yinc > 0 then py - vy else 0 in
470 let py = py + h + ips in
471 let e =
472 { pageno = pageno
473 ; pagedimno = pdimno
474 ; pagew = w
475 ; pageh = h
476 ; pagedispy = dy + off
477 ; pagey = pagey + off
478 ; pagevh = pagevh - off
479 ; pagex = x
482 let accu = e :: accu in
483 f ~pageno:(pageno+1)
484 ~pdimno
485 ~prev:curr
487 ~dy:(dy+pagevh+ips)
488 ~pdims:rest
489 ~cacheleft:(cacheleft-1)
490 ~accu
492 if state.invalidated = 0
493 then (
494 let accu =
496 ~pageno:0
497 ~pdimno:~-1
498 ~prev:(0,0,0,0)
499 ~py:0
500 ~dy:0
501 ~pdims:state.pdims
502 ~cacheleft:(cbcap state.pagecache)
503 ~accu:[]
505 List.rev accu
507 else
511 let clamp incr =
512 let y = state.y + incr in
513 let y = max 0 y in
514 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
518 let getopaque pageno =
519 try Some (Hashtbl.find state.pagemap
520 (pageno, state.w, conf.angle, conf.proportional, state.gen))
521 with Not_found -> None
524 let cache pageno opaque =
525 Hashtbl.replace state.pagemap
526 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
529 let validopaque opaque = String.length opaque > 0;;
531 let render l =
532 match getopaque l.pageno with
533 | None when not state.rendering ->
534 state.rendering <- true;
535 cache l.pageno ("", -1);
536 wcmd "render" [`i (l.pageno + 1)
537 ;`i l.pagedimno
538 ;`i l.pagew
539 ;`i l.pageh];
540 | _ -> ()
543 let loadlayout layout =
544 let rec f all = function
545 | l :: ls ->
546 begin match getopaque l.pageno with
547 | None -> render l; f false ls
548 | Some (opaque, _) -> f (all && validopaque opaque) ls
550 | [] -> all
552 f (layout <> []) layout;
555 let findpageforopaque opaque =
556 Hashtbl.fold
557 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
558 state.pagemap None
561 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
563 let preload () =
564 let oktopreload =
565 if conf.preload
566 then
567 let memleft = conf.memlimit - state.memused in
568 if memleft < 0
569 then
570 let opaque = cbpeek state.pagecache in
571 match findpageforopaque opaque with
572 | Some ((n, _, _, _, _), size) ->
573 memleft + size >= 0 && not (pagevisible state.layout n)
574 | None -> false
575 else true
576 else false
578 if oktopreload
579 then
580 let presentation = conf.presentation in
581 let interpagespace = conf.interpagespace in
582 let maxy = state.maxy in
583 conf.presentation <- false;
584 conf.interpagespace <- 0;
585 state.maxy <- calcheight ();
586 let y =
587 match state.layout with
588 | [] -> 0
589 | l :: _ -> getpagey l.pageno + l.pagey
591 let y = if y < conf.winh then 0 else y - conf.winh in
592 let pages = layout y (conf.winh*3) in
593 List.iter render pages;
594 conf.presentation <- presentation;
595 conf.interpagespace <- interpagespace;
596 state.maxy <- maxy;
599 let gotoy y =
600 let y = max 0 y in
601 let y = min state.maxy y in
602 let pages = layout y conf.winh in
603 let ready = loadlayout pages in
604 if conf.showall
605 then (
606 if ready
607 then (
608 state.y <- y;
609 state.layout <- pages;
610 state.throttle <- None;
611 Glut.postRedisplay ();
613 else (
614 state.throttle <- Some pages;
617 else (
618 state.y <- y;
619 state.layout <- pages;
620 state.throttle <- None;
621 Glut.postRedisplay ();
623 begin match state.mode with
624 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
625 if not (pagevisible pages pageno)
626 then (
627 match state.layout with
628 | [] -> ()
629 | l :: _ ->
630 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
632 | _ -> ()
633 end;
634 preload ();
637 let gotoy_and_clear_text y =
638 gotoy y;
639 if not conf.verbose then state.text <- "";
642 let getanchor () =
643 match state.layout with
644 | [] -> emptyanchor
645 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
648 let getanchory (n, top) =
649 let y, h = getpageyh n in
650 y + (truncate (top *. float h));
653 let gotoanchor anchor =
654 gotoy (getanchory anchor);
657 let addnav () =
658 cbput state.hists.nav (getanchor ());
661 let getnav () =
662 let anchor = cbgetc state.hists.nav ~-1 in
663 getanchory anchor;
666 let gotopage n top =
667 let y, h = getpageyh n in
668 gotoy_and_clear_text (y + (truncate (top *. float h)));
671 let gotopage1 n top =
672 let y = getpagey n in
673 gotoy_and_clear_text (y + top);
676 let invalidate () =
677 state.layout <- [];
678 state.pdims <- [];
679 state.rects <- [];
680 state.rects1 <- [];
681 state.invalidated <- state.invalidated + 1;
684 let scalecolor c =
685 let c = c *. state.colorscale in
686 (c, c, c);
689 let represent () =
690 state.maxy <- calcheight ();
691 match state.mode with
692 | Birdseye (_, _, pageno, _, _) ->
693 let y, h = getpageyh pageno in
694 let top = (conf.winh - h) / 2 in
695 gotoy (max 0 (y - top))
696 | _ -> gotoanchor state.anchor
699 let pagematrix () =
700 GlMat.mode `projection;
701 GlMat.load_identity ();
702 GlMat.rotate ~x:1.0 ~angle:180.0 ();
703 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
704 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
705 if state.x != 0
706 then (
707 GlMat.translate ~x:(float state.x) ();
711 let winmatrix () =
712 GlMat.mode `projection;
713 GlMat.load_identity ();
714 GlMat.rotate ~x:1.0 ~angle:180.0 ();
715 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
716 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
719 let reshape ~w ~h =
720 if state.invalidated = 0 && state.anchor == initialanchor
721 then state.anchor <- getanchor ();
723 conf.winw <- w;
724 let w = truncate (float w *. conf.zoom) - conf.scrollw in
725 let w = max w 2 in
726 state.w <- w;
727 conf.winh <- h;
728 GlMat.mode `modelview;
729 GlMat.load_identity ();
730 GlClear.color (scalecolor 1.0);
731 GlClear.clear [`color];
733 invalidate ();
734 wcmd "geometry" [`i w; `i h];
737 let showtext c s =
738 GlDraw.color (0.0, 0.0, 0.0);
739 GlDraw.rect
740 (0.0, float (conf.winh - 18))
741 (float (conf.winw - conf.scrollw - 1), float conf.winh)
743 let font = Glut.BITMAP_8_BY_13 in
744 GlDraw.color (1.0, 1.0, 1.0);
745 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
746 Glut.bitmapCharacter ~font ~c:(Char.code c);
747 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
750 let enttext () =
751 let len = String.length state.text in
752 match state.mode with
753 | Textentry ((c, text, _, _, _), _) ->
754 let s =
755 if len > 0
756 then
757 text ^ " [" ^ state.text ^ "]"
758 else
759 text
761 showtext c s;
763 | _ ->
764 if len > 0 then showtext ' ' state.text
767 let showtext c s =
768 state.text <- Printf.sprintf "%c%s" c s;
769 Glut.postRedisplay ();
772 let act cmd =
773 match cmd.[0] with
774 | 'c' ->
775 state.pdims <- [];
777 | 'D' ->
778 state.rects <- state.rects1;
779 Glut.postRedisplay ()
781 | 'C' ->
782 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
783 state.pagecount <- n;
784 state.invalidated <- state.invalidated - 1;
785 if state.invalidated = 0
786 then represent ()
788 | 't' ->
789 let s = Scanf.sscanf cmd "t %n"
790 (fun n -> String.sub cmd n (String.length cmd - n))
792 Glut.setWindowTitle s
794 | 'T' ->
795 let s = Scanf.sscanf cmd "T %n"
796 (fun n -> String.sub cmd n (String.length cmd - n))
798 if istextentry state.mode
799 then (
800 state.text <- s;
801 showtext ' ' s;
803 else (
804 state.text <- s;
805 Glut.postRedisplay ();
808 | 'V' ->
809 if conf.verbose
810 then
811 let s = Scanf.sscanf cmd "V %n"
812 (fun n -> String.sub cmd n (String.length cmd - n))
814 state.text <- s;
815 showtext ' ' s;
817 | 'F' ->
818 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
819 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
820 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
821 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
823 let y = (getpagey pageno) + truncate y0 in
824 addnav ();
825 gotoy y;
826 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
828 | 'R' ->
829 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
830 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
831 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
832 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
834 state.rects1 <-
835 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
837 | 'r' ->
838 let n, w, h, r, l, s, p =
839 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
840 (fun n w h r l s p ->
841 (n-1, w, h, r, l != 0, s, p))
844 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
845 state.memused <- state.memused + s;
847 let layout =
848 match state.throttle with
849 | None -> state.layout
850 | Some layout -> layout
853 let rec gc () =
854 if (state.memused <= conf.memlimit) || cbempty state.pagecache
855 then ()
856 else (
857 let evictedopaque = cbpeek state.pagecache in
858 match findpageforopaque evictedopaque with
859 | None -> failwith "bug in gc"
860 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
861 if state.gen != gen || not (pagevisible layout evictedn)
862 then (
863 wcmd "free" [`s evictedopaque];
864 state.memused <- state.memused - evictedsize;
865 Hashtbl.remove state.pagemap k;
866 cbdecr state.pagecache;
867 gc ();
871 gc ();
873 cbput state.pagecache p;
874 state.rendering <- false;
876 begin match state.throttle with
877 | None ->
878 if pagevisible state.layout n
879 then gotoy state.y
880 else (
881 let allvisible = loadlayout state.layout in
882 if allvisible then preload ();
885 | Some layout ->
886 match layout with
887 | [] -> ()
888 | l :: _ ->
889 let y = getpagey l.pageno + l.pagey in
890 gotoy y
893 | 'l' ->
894 let (n, w, h, x) as pdim =
895 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
897 state.pdims <- pdim :: state.pdims
899 | 'o' ->
900 let (l, n, t, h, pos) =
901 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
903 let s = String.sub cmd pos (String.length cmd - pos) in
904 let s =
905 let l = String.length s in
906 let b = Buffer.create (String.length s) in
907 let rec loop pc2 i =
908 if i = l
909 then ()
910 else
911 let pc2 =
912 match s.[i] with
913 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
914 | '\xc2' -> true
915 | c ->
916 let c = if Char.code c land 0x80 = 0 then c else '?' in
917 Buffer.add_char b c;
918 false
920 loop pc2 (i+1)
922 loop false 0;
923 Buffer.contents b
925 let outline = (s, l, n, float t /. float h) in
926 let outlines =
927 match state.outlines with
928 | Olist outlines -> Olist (outline :: outlines)
929 | Oarray _ -> Olist [outline]
930 | Onarrow _ -> Olist [outline]
932 state.outlines <- outlines
935 | 'i' ->
936 let s = Scanf.sscanf cmd "i %n"
937 (fun n -> String.sub cmd n (String.length cmd - n))
939 let len = String.length s in
940 let rec fold accu pos =
941 let eolpos =
942 try String.index_from s pos '\n' with Not_found -> len
944 if eolpos = len
945 then List.rev accu
946 else
947 let line = String.sub s pos (eolpos - pos) in
948 fold ((1, line)::accu) (eolpos+1)
950 state.docinfo <- fold state.docinfo 0
952 | _ ->
953 dolog "unknown cmd `%S'" cmd
956 let now = Unix.gettimeofday;;
958 let idle () =
959 let rec loop delay =
960 let r, _, _ = Unix.select [state.csock] [] [] delay in
961 begin match r with
962 | [] ->
963 if state.ascrollstep > 0
964 then begin
965 let y = state.y + state.ascrollstep in
966 let y = if y >= state.maxy then 0 else y in
967 gotoy y;
968 state.text <- "";
969 end;
971 | _ ->
972 let cmd = readcmd state.csock in
973 act cmd;
974 loop 0.0
975 end;
976 in loop 0.001
979 let onhist cb =
980 let rc = cb.rc in
981 let action = function
982 | HCprev -> cbget cb ~-1
983 | HCnext -> cbget cb 1
984 | HCfirst -> cbget cb ~-(cb.rc)
985 | HClast -> cbget cb (cb.len - 1 - cb.rc)
986 and cancel () = cb.rc <- rc
987 in (action, cancel)
990 let search pattern forward =
991 if String.length pattern > 0
992 then
993 let pn, py =
994 match state.layout with
995 | [] -> 0, 0
996 | l :: _ ->
997 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
999 let cmd =
1000 let b = makecmd "search"
1001 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1003 Buffer.add_char b ',';
1004 Buffer.add_string b pattern;
1005 Buffer.add_char b '\000';
1006 Buffer.contents b;
1008 writecmd state.csock cmd;
1011 let intentry text key =
1012 let c = Char.unsafe_chr key in
1013 match c with
1014 | '0' .. '9' ->
1015 let s = "x" in s.[0] <- c;
1016 let text = text ^ s in
1017 TEcont text
1019 | _ ->
1020 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1021 TEcont text
1024 let addchar s c =
1025 let b = Buffer.create (String.length s + 1) in
1026 Buffer.add_string b s;
1027 Buffer.add_char b c;
1028 Buffer.contents b;
1031 let textentry text key =
1032 let c = Char.unsafe_chr key in
1033 match c with
1034 | _ when key >= 32 && key < 127 ->
1035 let text = addchar text c in
1036 TEcont text
1038 | _ ->
1039 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1040 TEcont text
1043 let reinit angle proportional =
1044 conf.angle <- angle;
1045 conf.proportional <- proportional;
1046 invalidate ();
1047 wcmd "reinit" [`i angle; `b proportional];
1050 let setzoom zoom =
1051 let zoom = max 0.01 (min 2.2 zoom) in
1052 if zoom <> conf.zoom
1053 then (
1054 if zoom <= 1.0
1055 then state.x <- 0;
1056 conf.zoom <- zoom;
1057 reshape conf.winw conf.winh;
1058 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1062 let enterbirdseye () =
1063 let zoom = float conf.thumbw /. float conf.winw in
1064 let birdseyepageno =
1065 let rec fold candidate = function
1066 | [] -> candidate
1067 | l :: _ when l.pagey = 0 -> l.pageno
1068 | l :: rest -> fold l.pageno rest
1070 fold 0 state.layout
1072 state.mode <- Birdseye (
1073 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1075 conf.zoom <- zoom;
1076 conf.presentation <- false;
1077 conf.interpagespace <- 10;
1078 conf.hlinks <- false;
1079 state.x <- 0;
1080 state.mstate <- Mnone;
1081 conf.showall <- false;
1082 Glut.setCursor Glut.CURSOR_INHERIT;
1083 if conf.verbose
1084 then
1085 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1086 (100.0*.zoom)
1087 else
1088 state.text <- ""
1090 reshape conf.winw conf.winh;
1093 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1094 state.mode <- View;
1095 conf.zoom <- c.zoom;
1096 conf.presentation <- c.presentation;
1097 conf.interpagespace <- c.interpagespace;
1098 conf.showall <- c.showall;
1099 conf.hlinks <- c.hlinks;
1100 state.x <- leftx;
1101 if conf.verbose
1102 then
1103 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1104 (100.0*.conf.zoom)
1106 reshape conf.winw conf.winh;
1107 state.anchor <- if goback then anchor else (pageno, 0.0);
1110 let togglebirdseye () =
1111 match state.mode with
1112 | Birdseye vals -> leavebirdseye vals true
1113 | View | Outline _ -> enterbirdseye ()
1114 | _ -> ()
1117 let optentry text key =
1118 let btos b = if b then "on" else "off" in
1119 let c = Char.unsafe_chr key in
1120 match c with
1121 | 's' ->
1122 let ondone s =
1123 try conf.scrollstep <- int_of_string s with exc ->
1124 state.text <- Printf.sprintf "bad integer `%s': %s"
1125 s (Printexc.to_string exc)
1127 TEswitch ('#', "", None, intentry, ondone)
1129 | 'A' ->
1130 let ondone s =
1132 conf.autoscrollstep <- int_of_string s;
1133 if state.ascrollstep > 0
1134 then state.ascrollstep <- conf.autoscrollstep;
1135 with exc ->
1136 state.text <- Printf.sprintf "bad integer `%s': %s"
1137 s (Printexc.to_string exc)
1139 TEswitch ('*', "", None, intentry, ondone)
1141 | 'Z' ->
1142 let ondone s =
1144 let zoom = float (int_of_string s) /. 100.0 in
1145 setzoom zoom
1146 with exc ->
1147 state.text <- Printf.sprintf "bad integer `%s': %s"
1148 s (Printexc.to_string exc)
1150 TEswitch ('@', "", None, intentry, ondone)
1152 | 't' ->
1153 let ondone s =
1155 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1156 state.text <-
1157 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1158 begin match state.mode with
1159 | Textentry (_, Birdseye beye) ->
1160 leavebirdseye beye false;
1161 enterbirdseye ()
1162 | _ -> ()
1163 end;
1164 with exc ->
1165 state.text <- Printf.sprintf "bad integer `%s': %s"
1166 s (Printexc.to_string exc)
1168 TEswitch ('$', "", None, intentry, ondone)
1170 | 'R' ->
1171 let ondone s =
1172 match try
1173 Some (int_of_string s)
1174 with exc ->
1175 state.text <- Printf.sprintf "bad integer `%s': %s"
1176 s (Printexc.to_string exc);
1177 None
1178 with
1179 | Some angle -> reinit angle conf.proportional
1180 | None -> ()
1182 TEswitch ('^', "", None, intentry, ondone)
1184 | 'i' ->
1185 conf.icase <- not conf.icase;
1186 TEdone ("case insensitive search " ^ (btos conf.icase))
1188 | 'p' ->
1189 conf.preload <- not conf.preload;
1190 gotoy state.y;
1191 TEdone ("preload " ^ (btos conf.preload))
1193 | 'v' ->
1194 conf.verbose <- not conf.verbose;
1195 TEdone ("verbose " ^ (btos conf.verbose))
1197 | 'h' ->
1198 conf.maxhfit <- not conf.maxhfit;
1199 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1200 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1202 | 'c' ->
1203 conf.crophack <- not conf.crophack;
1204 TEdone ("crophack " ^ btos conf.crophack)
1206 | 'a' ->
1207 conf.showall <- not conf.showall;
1208 TEdone ("showall " ^ btos conf.showall)
1210 | 'f' ->
1211 conf.underinfo <- not conf.underinfo;
1212 TEdone ("underinfo " ^ btos conf.underinfo)
1214 | 'P' ->
1215 conf.savebmarks <- not conf.savebmarks;
1216 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1218 | 'S' ->
1219 let ondone s =
1221 let pageno, py =
1222 match state.layout with
1223 | [] -> 0, 0
1224 | l :: _ ->
1225 l.pageno, l.pagey
1227 conf.interpagespace <- int_of_string s;
1228 state.maxy <- calcheight ();
1229 let y = getpagey pageno in
1230 gotoy (y + py)
1231 with exc ->
1232 state.text <- Printf.sprintf "bad integer `%s': %s"
1233 s (Printexc.to_string exc)
1235 TEswitch ('%', "", None, intentry, ondone)
1237 | 'l' ->
1238 reinit conf.angle (not conf.proportional);
1239 TEdone ("proprortional display " ^ btos conf.proportional)
1241 | _ ->
1242 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1243 TEstop
1246 let maxoutlinerows () = (conf.winh - 31) / 16;;
1248 let enterselector allowdel outlines errmsg msg =
1249 if Array.length outlines = 0
1250 then (
1251 showtext ' ' errmsg;
1253 else (
1254 state.text <- msg;
1255 Glut.setCursor Glut.CURSOR_INHERIT;
1256 let pageno =
1257 match state.layout with
1258 | [] -> -1
1259 | {pageno=pageno} :: rest -> pageno
1261 let active =
1262 let rec loop n =
1263 if n = Array.length outlines
1264 then 0
1265 else
1266 let (_, _, outlinepageno, _) = outlines.(n) in
1267 if outlinepageno >= pageno then n else loop (n+1)
1269 loop 0
1271 state.mode <- Outline
1272 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0);
1273 Glut.postRedisplay ();
1277 let enteroutlinemode () =
1278 let outlines, msg =
1279 match state.outlines with
1280 | Oarray a -> a, ""
1281 | Olist l ->
1282 let a = Array.of_list (List.rev l) in
1283 state.outlines <- Oarray a;
1284 a, ""
1285 | Onarrow (pat, a, b) ->
1286 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1288 enterselector false outlines "Document has no outline" msg;
1291 let enterbookmarkmode () =
1292 let bookmarks = Array.of_list state.bookmarks in
1293 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1296 let enterinfomode () =
1297 let btos = function true -> "on" | _ -> "off" in
1298 let pageno, top = getanchor () in
1299 let info =
1300 let autoscrollstep =
1301 if state.ascrollstep > 0
1302 then state.ascrollstep
1303 else conf.autoscrollstep
1305 (0, "Current parameters")
1306 :: (1, "presentation mode " ^ btos conf.presentation)
1307 :: (1, "case insensitive search " ^ btos conf.icase)
1308 :: (1, "preload " ^ btos conf.preload)
1309 :: (1, "page bias " ^ string_of_int conf.pagebias)
1310 :: (1, "verbose " ^ btos conf.verbose)
1311 :: (1, "scroll step " ^ string_of_int conf.scrollstep)
1312 :: (1, "max fit " ^ btos conf.maxhfit)
1313 :: (1, "crop hack " ^ btos conf.crophack)
1314 :: (1, "autoscroll step " ^ string_of_int autoscrollstep)
1315 :: (1, "throttle " ^ btos conf.showall)
1316 :: (1, "highlight links " ^ btos conf.hlinks)
1317 :: (1, "under info " ^ btos conf.underinfo)
1318 :: (1, "veritcal margin " ^ string_of_int conf.interpagespace)
1319 :: (1, "zoom " ^ Printf.sprintf "%-5.1f" (conf.zoom*.100.))
1320 :: (1, "rotation " ^ string_of_int conf.angle)
1321 :: (1, "persistent bookmarks " ^ btos conf.savebmarks)
1322 :: (1, "proportional display " ^ btos conf.proportional)
1323 :: (1, "pixmap cache size " ^ string_of_int conf.memlimit)
1324 :: (1, "pixmap cache used " ^ string_of_int state.memused)
1325 :: (1, "thumbnail width " ^ string_of_int conf.thumbw)
1326 :: (1, "persistent location " ^ btos conf.jumpback)
1327 :: (1, Printf.sprintf "window dimensions %dx%d " conf.winw conf.winh)
1328 :: (0, "Document information")
1329 :: (1, string_of_int state.pagecount ^ " pages")
1330 :: state.docinfo
1332 let o =
1333 let o = Array.create (List.length info) ("", 0, pageno, top) in
1334 let rec iteri i = function
1335 | [] -> ()
1336 | (l, s) :: rest ->
1337 o.(i) <- (s, l, pageno, top);
1338 iteri (i+1) rest
1340 iteri 0 info;
1343 enterselector false o "Info not available" "";
1346 let enterhelpmode () =
1347 let pageno, top = getanchor () in
1348 let o =
1349 let help = ("Keys for llpp version " ^ Help.version) :: state.help in
1350 let o = Array.create (List.length help) ("", 0, pageno, top) in
1351 let rec iteri i = function
1352 | [] -> ()
1353 | s :: rest ->
1354 o.(i) <- (s, (if i = 0 then 0 else 1), pageno, top);
1355 iteri (i+1) rest
1357 iteri 0 help;
1360 enterselector false o "Help not available" "";
1363 let quickbookmark ?title () =
1364 match state.layout with
1365 | [] -> ()
1366 | l :: _ ->
1367 let title =
1368 match title with
1369 | None ->
1370 let sec = Unix.gettimeofday () in
1371 let tm = Unix.localtime sec in
1372 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1373 (l.pageno+1)
1374 tm.Unix.tm_mday
1375 tm.Unix.tm_mon
1376 (tm.Unix.tm_year + 1900)
1377 tm.Unix.tm_hour
1378 tm.Unix.tm_min
1379 | Some title -> title
1381 state.bookmarks <-
1382 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1385 let doreshape w h =
1386 state.fullscreen <- None;
1387 Glut.reshapeWindow w h;
1390 let writeopen path password =
1391 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1392 writecmd state.csock "info";
1395 let opendoc path password =
1396 invalidate ();
1397 state.path <- path;
1398 state.password <- password;
1399 state.gen <- state.gen + 1;
1401 writeopen path password;
1402 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1403 wcmd "geometry" [`i state.w; `i conf.winh];
1406 let viewkeyboard ~key ~x ~y =
1407 let enttext te =
1408 state.mode <- Textentry (te, state.mode);
1409 state.text <- "";
1410 enttext ();
1411 Glut.postRedisplay ()
1413 let c = Char.chr key in
1414 match c with
1415 | '\027' | 'q' ->
1416 exit 0
1418 | '\008' ->
1419 let y = getnav () in
1420 gotoy_and_clear_text y
1422 | 'o' ->
1423 enteroutlinemode ()
1425 | 'u' ->
1426 state.rects <- [];
1427 state.text <- "";
1428 Glut.postRedisplay ()
1430 | '/' | '?' ->
1431 let ondone isforw s =
1432 cbput state.hists.pat s;
1433 state.searchpattern <- s;
1434 search s isforw
1436 enttext (c, "", Some (onhist state.hists.pat),
1437 textentry, ondone (c ='/'))
1439 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1440 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1441 setzoom (min 2.2 (conf.zoom +. incr))
1443 | '+' ->
1444 let ondone s =
1445 let n =
1446 try int_of_string s with exc ->
1447 state.text <- Printf.sprintf "bad integer `%s': %s"
1448 s (Printexc.to_string exc);
1449 max_int
1451 if n != max_int
1452 then (
1453 conf.pagebias <- n;
1454 state.text <- "page bias is now " ^ string_of_int n;
1457 enttext ('+', "", None, intentry, ondone)
1459 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1460 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1461 setzoom (max 0.01 (conf.zoom -. decr))
1463 | '-' ->
1464 let ondone msg =
1465 state.text <- msg;
1467 enttext ('-', "", None, optentry, ondone)
1469 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1470 setzoom 1.0
1472 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1473 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1474 if zoom < 1.0
1475 then setzoom zoom
1477 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1478 togglebirdseye ()
1480 | '0' .. '9' ->
1481 let ondone s =
1482 let n =
1483 try int_of_string s with exc ->
1484 state.text <- Printf.sprintf "bad integer `%s': %s"
1485 s (Printexc.to_string exc);
1488 if n >= 0
1489 then (
1490 addnav ();
1491 cbput state.hists.pag (string_of_int n);
1492 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1495 let pageentry text key =
1496 match Char.unsafe_chr key with
1497 | 'g' -> TEdone text
1498 | _ -> intentry text key
1500 let text = "x" in text.[0] <- c;
1501 enttext (':', text, Some (onhist state.hists.pag), pageentry, ondone)
1503 | 'b' ->
1504 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1505 reshape conf.winw conf.winh;
1507 | 'l' ->
1508 conf.hlinks <- not conf.hlinks;
1509 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1510 Glut.postRedisplay ()
1512 | 'a' ->
1513 if state.ascrollstep = 0
1514 then state.ascrollstep <- conf.autoscrollstep
1515 else (
1516 conf.autoscrollstep <- state.ascrollstep;
1517 state.ascrollstep <- 0;
1520 | 'P' ->
1521 conf.presentation <- not conf.presentation;
1522 showtext ' ' ("presentation mode " ^
1523 if conf.presentation then "on" else "off");
1524 state.anchor <- getanchor ();
1525 represent ()
1527 | 'f' ->
1528 begin match state.fullscreen with
1529 | None ->
1530 state.fullscreen <- Some (conf.winw, conf.winh);
1531 Glut.fullScreen ()
1532 | Some (w, h) ->
1533 state.fullscreen <- None;
1534 doreshape w h
1537 | 'g' ->
1538 gotoy_and_clear_text 0
1540 | 'n' ->
1541 search state.searchpattern true
1543 | 'p' | 'N' ->
1544 search state.searchpattern false
1546 | 't' ->
1547 begin match state.layout with
1548 | [] -> ()
1549 | l :: _ ->
1550 gotoy_and_clear_text (getpagey l.pageno)
1553 | ' ' ->
1554 begin match List.rev state.layout with
1555 | [] -> ()
1556 | l :: _ ->
1557 let pageno = min (l.pageno+1) (state.pagecount-1) in
1558 gotoy_and_clear_text (getpagey pageno)
1561 | '\127' ->
1562 begin match state.layout with
1563 | [] -> ()
1564 | l :: _ ->
1565 let pageno = max 0 (l.pageno-1) in
1566 gotoy_and_clear_text (getpagey pageno)
1569 | '=' ->
1570 let f (fn, ln) l =
1571 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1573 let fn, ln = List.fold_left f (-1, -1) state.layout in
1574 let s =
1575 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1576 let percent =
1577 if maxy <= 0
1578 then 100.
1579 else (100. *. (float state.y /. float maxy)) in
1580 if fn = ln
1581 then
1582 Printf.sprintf "Page %d of %d %.2f%%"
1583 (fn+1) state.pagecount percent
1584 else
1585 Printf.sprintf
1586 "Pages %d-%d of %d %.2f%%"
1587 (fn+1) (ln+1) state.pagecount percent
1589 showtext ' ' s;
1591 | 'w' ->
1592 begin match state.layout with
1593 | [] -> ()
1594 | l :: _ ->
1595 doreshape (l.pagew + conf.scrollw) l.pageh;
1596 Glut.postRedisplay ();
1599 | '\'' ->
1600 enterbookmarkmode ()
1602 | 'h' ->
1603 enterhelpmode ()
1605 | 'i' ->
1606 enterinfomode ()
1608 | 'm' ->
1609 let ondone s =
1610 match state.layout with
1611 | l :: _ ->
1612 state.bookmarks <-
1613 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1614 :: state.bookmarks
1615 | _ -> ()
1617 enttext ('~', "", None, textentry, ondone)
1619 | '~' ->
1620 quickbookmark ();
1621 showtext ' ' "Quick bookmark added";
1623 | 'z' ->
1624 begin match state.layout with
1625 | l :: _ ->
1626 let rect = getpdimrect l.pagedimno in
1627 let w, h =
1628 if conf.crophack
1629 then
1630 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1631 truncate (1.2 *. (rect.(3) -. rect.(0))))
1632 else
1633 (truncate (rect.(1) -. rect.(0)),
1634 truncate (rect.(3) -. rect.(0)))
1636 if w != 0 && h != 0
1637 then
1638 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1640 Glut.postRedisplay ();
1642 | [] -> ()
1645 | '<' | '>' ->
1646 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1648 | '[' | ']' ->
1649 state.colorscale <-
1650 max 0.0
1651 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1652 Glut.postRedisplay ()
1654 | 'k' -> gotoy (clamp (-conf.scrollstep))
1655 | 'j' -> gotoy (clamp conf.scrollstep)
1657 | 'r' -> opendoc state.path state.password
1659 | _ ->
1660 vlog "huh? %d %c" key (Char.chr key);
1663 let textentrykeyboard ~key ~x ~y ((c, text, opthist, onkey, ondone), mode) =
1664 let enttext te =
1665 state.mode <- Textentry (te, mode);
1666 state.text <- "";
1667 enttext ();
1668 Glut.postRedisplay ()
1670 match Char.unsafe_chr key with
1671 | '\008' ->
1672 let len = String.length text in
1673 if len = 0
1674 then (
1675 state.mode <- mode;
1676 Glut.postRedisplay ();
1678 else (
1679 let s = String.sub text 0 (len - 1) in
1680 enttext (c, s, opthist, onkey, ondone)
1683 | '\r' | '\n' ->
1684 ondone text;
1685 state.mode <- mode;
1686 Glut.postRedisplay ()
1688 | '\027' ->
1689 begin match opthist with
1690 | None -> ()
1691 | Some (_, onhistcancel) -> onhistcancel ()
1692 end;
1693 state.mode <- View;
1694 Glut.postRedisplay ()
1696 | _ ->
1697 begin match onkey text key with
1698 | TEdone text ->
1699 state.mode <- mode;
1700 ondone text;
1701 Glut.postRedisplay ()
1703 | TEcont text ->
1704 enttext (c, text, opthist, onkey, ondone);
1706 | TEstop ->
1707 state.mode <- mode;
1708 Glut.postRedisplay ()
1710 | TEswitch te ->
1711 state.mode <- Textentry (te, mode);
1712 Glut.postRedisplay ()
1713 end;
1716 let birdseyekeyboard ~key ~x ~y ((_, _, pageno, _, anchor) as beye) =
1717 match key with
1718 | 27 ->
1719 leavebirdseye beye true
1721 | 12 ->
1722 let y, h = getpageyh pageno in
1723 let top = (conf.winh - h) / 2 in
1724 gotoy (max 0 (y - top))
1726 | 13 ->
1727 leavebirdseye beye false
1729 | _ ->
1730 viewkeyboard ~key ~x ~y
1733 let outlinekeyboard ~key ~x ~y
1734 (allowdel, active, first, outlines, qsearch, pan) =
1735 let narrow outlines pattern =
1736 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1737 match reopt with
1738 | None -> None
1739 | Some re ->
1740 let rec fold accu n =
1741 if n = -1
1742 then accu
1743 else
1744 let (s, _, _, _) as o = outlines.(n) in
1745 let accu =
1746 if (try ignore (Str.search_forward re s 0); true
1747 with Not_found -> false)
1748 then (o :: accu)
1749 else accu
1751 fold accu (n-1)
1753 let matched = fold [] (Array.length outlines - 1) in
1754 if matched = [] then None else Some (Array.of_list matched)
1756 let search active pattern incr =
1757 let dosearch re =
1758 let rec loop n =
1759 if n = Array.length outlines || n = -1
1760 then None
1761 else
1762 let (s, _, _, _) = outlines.(n) in
1764 (try ignore (Str.search_forward re s 0); true
1765 with Not_found -> false)
1766 then Some n
1767 else loop (n + incr)
1769 loop active
1772 let re = Str.regexp_case_fold pattern in
1773 dosearch re
1774 with Failure s ->
1775 state.text <- s;
1776 None
1778 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1779 match key with
1780 | 27 ->
1781 if String.length qsearch = 0
1782 then (
1783 state.text <- "";
1784 state.mode <- View;
1785 Glut.postRedisplay ();
1787 else (
1788 state.text <- "";
1789 state.mode <- Outline (allowdel, active, first, outlines, "", pan);
1790 Glut.postRedisplay ();
1793 | 18 | 19 ->
1794 let incr = if key = 18 then -1 else 1 in
1795 let active, first =
1796 match search (active + incr) qsearch incr with
1797 | None ->
1798 state.text <- qsearch ^ " [not found]";
1799 active, first
1800 | Some active ->
1801 state.text <- qsearch;
1802 active, firstof active
1804 state.mode <- Outline (allowdel, active, first, outlines, qsearch, pan);
1805 Glut.postRedisplay ();
1807 | 8 ->
1808 let len = String.length qsearch in
1809 if len = 0
1810 then ()
1811 else (
1812 if len = 1
1813 then (
1814 state.text <- "";
1815 state.mode <- Outline (allowdel, active, first, outlines, "", pan);
1817 else
1818 let qsearch = String.sub qsearch 0 (len - 1) in
1819 let active, first =
1820 match search active qsearch ~-1 with
1821 | None ->
1822 state.text <- qsearch ^ " [not found]";
1823 active, first
1824 | Some active ->
1825 state.text <- qsearch;
1826 active, firstof active
1828 state.mode <- Outline (allowdel, active, first, outlines, qsearch, pan);
1830 Glut.postRedisplay ()
1832 | 13 ->
1833 if active < Array.length outlines
1834 then (
1835 let (_, _, n, t) = outlines.(active) in
1836 addnav ();
1837 gotopage n t;
1839 state.text <- "";
1840 if allowdel then state.bookmarks <- Array.to_list outlines;
1841 state.mode <- View;
1842 Glut.postRedisplay ();
1844 | _ when key >= 32 && key < 127 ->
1845 let pattern = addchar qsearch (Char.chr key) in
1846 let active, first =
1847 match search active pattern 1 with
1848 | None ->
1849 state.text <- pattern ^ " [not found]";
1850 active, first
1851 | Some active ->
1852 state.text <- pattern;
1853 active, firstof active
1855 state.mode <- Outline (allowdel, active, first, outlines, pattern, pan);
1856 Glut.postRedisplay ()
1858 | 14 when not allowdel -> (* ctrl-n *)
1859 if String.length qsearch > 0
1860 then (
1861 let optoutlines = narrow outlines qsearch in
1862 begin match optoutlines with
1863 | None -> state.text <- "can't narrow"
1864 | Some outlines ->
1865 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch, pan);
1866 match state.outlines with
1867 | Olist l -> ()
1868 | Oarray a ->
1869 state.outlines <- Onarrow (qsearch, outlines, a)
1870 | Onarrow (pat, a, b) ->
1871 state.outlines <- Onarrow (qsearch, outlines, b)
1872 end;
1874 Glut.postRedisplay ()
1876 | 21 when not allowdel -> (* ctrl-u *)
1877 let outline =
1878 match state.outlines with
1879 | Oarray a -> a
1880 | Olist l ->
1881 let a = Array.of_list (List.rev l) in
1882 state.outlines <- Oarray a;
1884 | Onarrow (pat, a, b) ->
1885 state.outlines <- Oarray b;
1886 state.text <- "";
1889 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan);
1890 Glut.postRedisplay ()
1892 | 12 ->
1893 state.mode <- Outline
1894 (allowdel, active, firstof active, outlines, qsearch, pan);
1895 Glut.postRedisplay ()
1897 | 127 when allowdel ->
1898 let len = Array.length outlines - 1 in
1899 if len = 0
1900 then (
1901 state.mode <- View;
1902 state.bookmarks <- [];
1904 else (
1905 let bookmarks = Array.init len
1906 (fun i ->
1907 let i = if i >= active then i + 1 else i in
1908 outlines.(i)
1911 state.mode <-
1912 Outline (
1913 allowdel,
1914 min active (len-1),
1915 min first (len-1),
1916 bookmarks, qsearch,
1920 Glut.postRedisplay ()
1922 | _ -> dolog "unknown key %d" key
1925 let keyboard ~key ~x ~y =
1926 if key = 7
1927 then
1928 wcmd "interrupt" []
1929 else
1930 match state.mode with
1931 | Outline outline -> outlinekeyboard ~key ~x ~y outline
1932 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
1933 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
1934 | View -> viewkeyboard ~key ~x ~y
1937 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno, anchor) =
1938 match key with
1939 | Glut.KEY_UP ->
1940 let pageno = max 0 (pageno - 1) in
1941 let rec loop = function
1942 | [] -> gotopage1 pageno 0
1943 | l :: _ when l.pageno = pageno ->
1944 if l.pagedispy >= 0 && l.pagey = 0
1945 then Glut.postRedisplay ()
1946 else gotopage1 pageno 0
1947 | _ :: rest -> loop rest
1949 loop state.layout;
1950 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1952 | Glut.KEY_DOWN ->
1953 let pageno = min (state.pagecount - 1) (pageno + 1) in
1954 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1955 let rec loop = function
1956 | [] ->
1957 let y, h = getpageyh pageno in
1958 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1959 gotoy (clamp dy)
1960 | l :: rest when l.pageno = pageno ->
1961 if l.pagevh != l.pageh
1962 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1963 else Glut.postRedisplay ()
1964 | l :: rest -> loop rest
1966 loop state.layout
1968 | Glut.KEY_PAGE_UP ->
1969 begin match state.layout with
1970 | l :: _ ->
1971 if l.pagey != 0
1972 then (
1973 state.mode <- Birdseye (
1974 conf, leftx, l.pageno, hooverpageno, anchor
1976 gotopage1 l.pageno 0;
1978 else (
1979 let layout = layout (state.y-conf.winh) conf.winh in
1980 match layout with
1981 | [] -> gotoy (clamp (-conf.winh))
1982 | l :: _ ->
1983 state.mode <- Birdseye (
1984 conf, leftx, l.pageno, hooverpageno, anchor
1986 gotopage1 l.pageno 0
1989 | [] -> gotoy (clamp (-conf.winh))
1990 end;
1992 | Glut.KEY_PAGE_DOWN ->
1993 begin match List.rev state.layout with
1994 | l :: _ ->
1995 let layout = layout (state.y + conf.winh) conf.winh in
1996 begin match layout with
1997 | [] ->
1998 let incr = l.pageh - l.pagevh in
1999 if incr = 0
2000 then (
2001 state.mode <-
2002 Birdseye (
2003 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2005 Glut.postRedisplay ();
2007 else gotoy (clamp (incr + conf.interpagespace*2));
2009 | l :: _ ->
2010 state.mode <-
2011 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2012 gotopage1 l.pageno 0;
2015 | [] -> gotoy (clamp conf.winh)
2016 end;
2018 | Glut.KEY_HOME ->
2019 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2020 gotopage1 0 0
2022 | Glut.KEY_END ->
2023 let pageno = state.pagecount - 1 in
2024 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2025 if not (pagevisible state.layout pageno)
2026 then
2027 let h =
2028 match List.rev state.pdims with
2029 | [] -> conf.winh
2030 | (_, _, h, _) :: _ -> h
2032 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2033 else Glut.postRedisplay ();
2034 | _ -> ()
2037 let setautoscrollspeed goingdown =
2038 let incr = max 1 (state.ascrollstep / 2) in
2039 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2040 state.ascrollstep <- astep;
2043 let special ~key ~x ~y =
2044 match state.mode with
2045 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2046 togglebirdseye ()
2048 | Birdseye vals ->
2049 birdseyespecial key x y vals
2051 | View when key = Glut.KEY_F1 ->
2052 enterhelpmode ()
2054 | View ->
2055 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2056 then setautoscrollspeed (key = Glut.KEY_DOWN)
2057 else
2058 let y =
2059 match key with
2060 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2061 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2062 | Glut.KEY_DOWN -> clamp conf.scrollstep
2063 | Glut.KEY_PAGE_UP ->
2064 if Glut.getModifiers () land Glut.active_ctrl != 0
2065 then
2066 match state.layout with
2067 | [] -> state.y
2068 | l :: _ -> state.y - l.pagey
2069 else
2070 clamp (-conf.winh)
2071 | Glut.KEY_PAGE_DOWN ->
2072 if Glut.getModifiers () land Glut.active_ctrl != 0
2073 then
2074 match List.rev state.layout with
2075 | [] -> state.y
2076 | l :: _ -> getpagey l.pageno
2077 else
2078 clamp conf.winh
2079 | Glut.KEY_HOME -> addnav (); 0
2080 | Glut.KEY_END ->
2081 addnav ();
2082 state.maxy - (if conf.maxhfit then conf.winh else 0)
2084 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2085 state.x <- state.x - 10;
2086 state.y
2087 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2088 state.x <- state.x + 10;
2089 state.y
2091 | _ -> state.y
2093 gotoy_and_clear_text y
2095 | Textentry
2096 ((c, s, (Some (action, _) as onhist), onkey, ondone), mode) ->
2097 let s =
2098 match key with
2099 | Glut.KEY_UP -> action HCprev
2100 | Glut.KEY_DOWN -> action HCnext
2101 | Glut.KEY_HOME -> action HCfirst
2102 | Glut.KEY_END -> action HClast
2103 | _ -> state.text
2105 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2106 Glut.postRedisplay ()
2108 | Textentry _ -> ()
2110 | Outline (allowdel, active, first, outlines, qsearch, pan) ->
2111 let maxrows = maxoutlinerows () in
2112 let calcfirst first active =
2113 if active > first
2114 then
2115 let rows = active - first in
2116 if rows > maxrows then active - maxrows else first
2117 else active
2119 let navigate incr =
2120 let active = active + incr in
2121 let active = max 0 (min active (Array.length outlines - 1)) in
2122 let first = calcfirst first active in
2123 state.mode <- Outline (allowdel, active, first, outlines, qsearch, pan);
2124 Glut.postRedisplay ()
2126 let updownlevel incr =
2127 let len = Array.length outlines in
2128 let (_, curlevel, _, _) = outlines.(active) in
2129 let rec flow i =
2130 if i = len then i-1 else if i = -1 then 0 else
2131 let (_, l, _, _) = outlines.(i) in
2132 if l != curlevel then i else flow (i+incr)
2134 let active = flow active in
2135 let first = calcfirst first active in
2136 state.mode <- Outline (allowdel, active, first, outlines, qsearch, pan);
2137 Glut.postRedisplay ()
2139 match key with
2140 | Glut.KEY_UP -> navigate ~-1
2141 | Glut.KEY_DOWN -> navigate 1
2142 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2143 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2145 | Glut.KEY_RIGHT ->
2146 if Glut.active_ctrl != 0
2147 then (
2148 state.mode <- Outline (
2149 allowdel, active, first, outlines, qsearch, min 0 (pan + 1)
2151 Glut.postRedisplay ();
2153 else (
2154 if not allowdel
2155 then updownlevel 1
2158 | Glut.KEY_LEFT ->
2159 if Glut.active_ctrl != 0
2160 then (
2161 state.mode <- Outline (
2162 allowdel, active, first, outlines, qsearch, pan - 1
2164 Glut.postRedisplay ();
2166 else (
2167 if not allowdel
2168 then updownlevel ~-1
2171 | Glut.KEY_HOME ->
2172 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch, pan);
2173 Glut.postRedisplay ()
2175 | Glut.KEY_END ->
2176 let active = Array.length outlines - 1 in
2177 let first = max 0 (active - maxrows) in
2178 state.mode <- Outline (
2179 allowdel, active, first, outlines, qsearch, pan
2181 Glut.postRedisplay ()
2183 | _ -> ()
2186 let drawplaceholder l =
2187 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
2188 GlDraw.rect
2189 (float l.pagex, float l.pagedispy)
2190 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2192 let x = float (if margin < 0 then -margin else l.pagex)
2193 and y = float (l.pagedispy + 13) in
2194 let font = Glut.BITMAP_8_BY_13 in
2195 GlDraw.color (0.0, 0.0, 0.0);
2196 GlPix.raster_pos ~x ~y ();
2197 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2198 ("Loading " ^ string_of_int (l.pageno + 1));
2201 let now () = Unix.gettimeofday ();;
2203 let drawpage l =
2204 let color =
2205 match state.mode with
2206 | Textentry _ -> scalecolor 0.4
2207 | View | Outline _ -> scalecolor 1.0
2208 | Birdseye (_, _, pageno, hooverpageno, _) ->
2209 if l.pageno = hooverpageno
2210 then scalecolor 0.9
2211 else (
2212 if l.pageno = pageno
2213 then scalecolor 1.0
2214 else scalecolor 0.8
2217 GlDraw.color color;
2218 begin match getopaque l.pageno with
2219 | Some (opaque, _) when validopaque opaque ->
2220 let a = now () in
2221 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2222 opaque;
2223 let b = now () in
2224 let d = b-.a in
2225 vlog "draw %d %f sec" l.pageno d;
2227 | _ ->
2228 drawplaceholder l;
2229 end;
2232 let scrollph y =
2233 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2234 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2235 let sh = float conf.winh /. sh in
2236 let sh = max sh (float conf.scrollh) in
2238 let percent =
2239 if state.y = state.maxy
2240 then 1.0
2241 else float y /. float maxy
2243 let position = (float conf.winh -. sh) *. percent in
2245 let position =
2246 if position +. sh > float conf.winh
2247 then float conf.winh -. sh
2248 else position
2250 position, sh;
2253 let scrollindicator () =
2254 GlDraw.color (0.64 , 0.64, 0.64);
2255 GlDraw.rect
2256 (float (conf.winw - conf.scrollw), 0.)
2257 (float conf.winw, float conf.winh)
2259 GlDraw.color (0.0, 0.0, 0.0);
2261 let position, sh = scrollph state.y in
2262 GlDraw.rect
2263 (float (conf.winw - conf.scrollw), position)
2264 (float conf.winw, position +. sh)
2268 let showsel margin =
2269 match state.mstate with
2270 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2273 | Msel ((x0, y0), (x1, y1)) ->
2274 let rec loop = function
2275 | l :: ls ->
2276 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2277 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2278 then
2279 match getopaque l.pageno with
2280 | Some (opaque, _) when validopaque opaque ->
2281 let oy = -l.pagey + l.pagedispy in
2282 seltext opaque
2283 (x0 - margin - state.x, y0,
2284 x1 - margin - state.x, y1) oy;
2286 | _ -> ()
2287 else loop ls
2288 | [] -> ()
2290 loop state.layout
2293 let showrects () =
2294 let panx = float state.x in
2295 Gl.enable `blend;
2296 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2297 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2298 List.iter
2299 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2300 List.iter (fun l ->
2301 if l.pageno = pageno
2302 then (
2303 let d = float (l.pagedispy - l.pagey) in
2304 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2305 GlDraw.begins `quads;
2307 GlDraw.vertex2 (x0+.panx, y0+.d);
2308 GlDraw.vertex2 (x1+.panx, y1+.d);
2309 GlDraw.vertex2 (x2+.panx, y2+.d);
2310 GlDraw.vertex2 (x3+.panx, y3+.d);
2312 GlDraw.ends ();
2314 ) state.layout
2315 ) state.rects
2317 Gl.disable `blend;
2320 let showoutline () =
2321 match state.mode with
2322 | Outline (allowdel, active, first, outlines, qsearch, pan) ->
2323 Gl.enable `blend;
2324 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2325 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2326 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2327 Gl.disable `blend;
2329 GlDraw.color (1., 1., 1.);
2330 let font = Glut.BITMAP_9_BY_15 in
2331 let draw_string x y s =
2332 GlPix.raster_pos ~x ~y ();
2333 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2335 let rec loop row =
2336 if row = Array.length outlines || (row - first) * 16 > conf.winh
2337 then ()
2338 else (
2339 let (s, l, _, _) = outlines.(row) in
2340 let y = (row - first) * 16 in
2341 let x = 5 + 15*l in
2342 if row = active
2343 then (
2344 Gl.enable `blend;
2345 GlDraw.polygon_mode `both `line;
2346 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2347 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2348 GlDraw.rect (0., float (y + 1))
2349 (float (conf.winw - 1), float (y + 18));
2350 GlDraw.polygon_mode `both `fill;
2351 Gl.disable `blend;
2352 GlDraw.color (1., 1., 1.);
2354 let draw_string s =
2355 let l = String.length s in
2356 if pan < 0
2357 then (
2358 let pan = pan * 2 in
2359 let left = l + pan in
2360 if left > 0
2361 then
2362 let s = String.sub s (-pan) left in
2363 draw_string (float x) (float (y + 16)) s
2365 else
2366 draw_string (float (x + pan*15)) (float (y + 16)) s
2368 draw_string s;
2369 loop (row+1)
2372 loop first
2374 | _ -> ()
2377 let display () =
2378 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2379 GlDraw.viewport margin 0 state.w conf.winh;
2380 pagematrix ();
2381 GlClear.color (scalecolor 0.5);
2382 GlClear.clear [`color];
2383 if conf.zoom > 1.0
2384 then (
2385 Gl.enable `scissor_test;
2386 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2388 List.iter drawpage state.layout;
2389 if conf.zoom > 1.0
2390 then
2391 Gl.disable `scissor_test
2393 if state.x != 0
2394 then (
2395 let x = -.float state.x in
2396 GlMat.translate ~x ();
2398 showrects ();
2399 showsel margin;
2400 GlDraw.viewport 0 0 conf.winw conf.winh;
2401 winmatrix ();
2402 scrollindicator ();
2403 showoutline ();
2404 enttext ();
2405 Glut.swapBuffers ();
2408 let getunder x y =
2409 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2410 let x = x - margin - state.x in
2411 let rec f = function
2412 | l :: rest ->
2413 begin match getopaque l.pageno with
2414 | Some (opaque, _) when validopaque opaque ->
2415 let y = y - l.pagedispy in
2416 if y > 0
2417 then
2418 let y = l.pagey + y in
2419 let x = x - l.pagex in
2420 match whatsunder opaque x y with
2421 | Unone -> f rest
2422 | under -> under
2423 else
2424 f rest
2425 | _ ->
2426 f rest
2428 | [] -> Unone
2430 f state.layout
2433 let viewmouse button bstate x y =
2434 match button with
2435 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2436 if Glut.getModifiers () land Glut.active_ctrl != 0
2437 then (
2438 match state.mstate with
2439 | Mzoom (oldn, i) ->
2440 if oldn = n
2441 then (
2442 if i = 2
2443 then
2444 let incr =
2445 match n with
2446 | 4 ->
2447 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2448 | _ ->
2449 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2451 let zoom = conf.zoom +. incr in
2452 setzoom zoom;
2453 state.mstate <- Mzoom (n, 0);
2454 else
2455 state.mstate <- Mzoom (n, i+1);
2457 else state.mstate <- Mzoom (n, 0)
2459 | _ -> state.mstate <- Mzoom (n, 0)
2461 else (
2462 if state.ascrollstep > 0
2463 then
2464 setautoscrollspeed (n=4)
2465 else
2466 let incr =
2467 if n = 3
2468 then -conf.scrollstep
2469 else conf.scrollstep
2471 let incr = incr * 2 in
2472 let y = clamp incr in
2473 gotoy_and_clear_text y
2476 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2477 if bstate = Glut.DOWN
2478 then (
2479 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2480 state.mstate <- Mpan (x, y)
2482 else
2483 state.mstate <- Mnone
2485 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2486 if bstate = Glut.DOWN
2487 then
2488 let position, sh = scrollph state.y in
2489 if y > truncate position && y < truncate (position +. sh)
2490 then
2491 state.mstate <- Mscroll
2492 else
2493 let percent = float y /. float conf.winh in
2494 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2495 gotoy desty;
2496 state.mstate <- Mscroll
2497 else
2498 state.mstate <- Mnone
2500 | Glut.LEFT_BUTTON ->
2501 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2502 begin match dest with
2503 | Ulinkgoto (pageno, top) ->
2504 if pageno >= 0
2505 then (
2506 addnav ();
2507 gotopage1 pageno top;
2510 | Ulinkuri s ->
2511 print_endline s
2513 | Unone when bstate = Glut.DOWN ->
2514 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2515 state.mstate <- Mpan (x, y);
2517 | Unone | Utext _ ->
2518 if bstate = Glut.DOWN
2519 then (
2520 if conf.angle mod 360 = 0
2521 then (
2522 state.mstate <- Msel ((x, y), (x, y));
2523 Glut.postRedisplay ()
2526 else (
2527 match state.mstate with
2528 | Mnone -> ()
2530 | Mzoom _ | Mscroll ->
2531 state.mstate <- Mnone
2533 | Mpan _ ->
2534 Glut.setCursor Glut.CURSOR_INHERIT;
2535 state.mstate <- Mnone
2537 | Msel ((x0, y0), (x1, y1)) ->
2538 let f l =
2539 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2540 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2541 then
2542 match getopaque l.pageno with
2543 | Some (opaque, _) when validopaque opaque ->
2544 copysel opaque
2545 | _ -> ()
2547 List.iter f state.layout;
2548 copysel ""; (* ugly *)
2549 Glut.setCursor Glut.CURSOR_INHERIT;
2550 state.mstate <- Mnone;
2554 | _ -> ()
2557 let birdseyemouse button bstate x y
2558 (conf, leftx, pageno, hooverpageno, anchor) =
2559 match button with
2560 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2561 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2562 let rec loop = function
2563 | [] -> ()
2564 | l :: rest ->
2565 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2566 && x > margin && x < margin + l.pagew
2567 then (
2568 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
2570 else loop rest
2572 loop state.layout
2573 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
2574 | _ -> ()
2577 let mouse bstate button x y =
2578 match state.mode with
2579 | View -> viewmouse button bstate x y
2580 | Birdseye beye -> birdseyemouse button bstate x y beye
2581 | Textentry _ -> ()
2582 | Outline _ -> ()
2585 let mouse ~button ~state ~x ~y = mouse state button x y;;
2587 let motion ~x ~y =
2588 match state.mode with
2589 | Outline _ -> ()
2590 | _ ->
2591 match state.mstate with
2592 | Mzoom _ | Mnone -> ()
2594 | Mpan (x0, y0) ->
2595 let dx = x - x0
2596 and dy = y0 - y in
2597 state.mstate <- Mpan (x, y);
2598 if conf.zoom > 1.0 then state.x <- state.x + dx;
2599 let y = clamp dy in
2600 gotoy_and_clear_text y
2602 | Msel (a, _) ->
2603 state.mstate <- Msel (a, (x, y));
2604 Glut.postRedisplay ()
2606 | Mscroll ->
2607 let y = min conf.winh (max 0 y) in
2608 let percent = float y /. float conf.winh in
2609 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2610 gotoy_and_clear_text y
2613 let pmotion ~x ~y =
2614 match state.mode with
2615 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
2616 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2617 let rec loop = function
2618 | [] ->
2619 if hooverpageno != -1
2620 then (
2621 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
2622 Glut.postRedisplay ();
2624 | l :: rest ->
2625 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2626 && x > margin && x < margin + l.pagew
2627 then (
2628 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
2629 Glut.postRedisplay ();
2631 else loop rest
2633 loop state.layout
2635 | Outline _ -> ()
2636 | _ ->
2637 match state.mstate with
2638 | Mnone ->
2639 begin match getunder x y with
2640 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2641 | Ulinkuri uri ->
2642 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2643 Glut.setCursor Glut.CURSOR_INFO
2644 | Ulinkgoto (page, y) ->
2645 if conf.underinfo
2646 then showtext 'p' ("age: " ^ string_of_int page);
2647 Glut.setCursor Glut.CURSOR_INFO
2648 | Utext s ->
2649 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2650 Glut.setCursor Glut.CURSOR_TEXT
2653 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
2658 module State =
2659 struct
2660 open Parser
2662 let home =
2664 match Sys.os_type with
2665 | "Win32" -> Sys.getenv "HOMEPATH"
2666 | _ -> Sys.getenv "HOME"
2667 with exn ->
2668 prerr_endline
2669 ("Can not determine home directory location: " ^
2670 Printexc.to_string exn);
2674 let config_of c attrs =
2675 let apply c k v =
2677 match k with
2678 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2679 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2680 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2681 | "preload" -> { c with preload = bool_of_string v }
2682 | "page-bias" -> { c with pagebias = int_of_string v }
2683 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
2684 | "auto-scroll-step" ->
2685 { c with autoscrollstep = max 0 (int_of_string v) }
2686 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2687 | "crop-hack" -> { c with crophack = bool_of_string v }
2688 | "throttle" -> { c with showall = bool_of_string v }
2689 | "highlight-links" -> { c with hlinks = bool_of_string v }
2690 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2691 | "vertical-margin" ->
2692 { c with interpagespace = max 0 (int_of_string v) }
2693 | "zoom" ->
2694 let zoom = float_of_string v /. 100. in
2695 let zoom = max 0.01 (min 2.2 zoom) in
2696 { c with zoom = zoom }
2697 | "presentation" -> { c with presentation = bool_of_string v }
2698 | "rotation-angle" -> { c with angle = int_of_string v }
2699 | "width" -> { c with winw = max 20 (int_of_string v) }
2700 | "height" -> { c with winh = max 20 (int_of_string v) }
2701 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2702 | "proportional-display" -> { c with proportional = bool_of_string v }
2703 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2704 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2705 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2706 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2707 | "persistent-location" -> { c with jumpback = bool_of_string v }
2708 | _ -> c
2709 with exn ->
2710 prerr_endline ("Error processing attribute (`" ^
2711 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2714 let rec fold c = function
2715 | [] -> c
2716 | (k, v) :: rest ->
2717 let c = apply c k v in
2718 fold c rest
2720 fold c attrs;
2723 let fromstring f pos n v d =
2724 try f v
2725 with exn ->
2726 dolog "Error processing attribute (%S=%S) at %d\n%s"
2727 n v pos (Printexc.to_string exn)
2732 let bookmark_of attrs =
2733 let rec fold title page rely = function
2734 | ("title", v) :: rest -> fold v page rely rest
2735 | ("page", v) :: rest -> fold title v rely rest
2736 | ("rely", v) :: rest -> fold title page v rest
2737 | _ :: rest -> fold title page rely rest
2738 | [] -> title, page, rely
2740 fold "invalid" "0" "0" attrs
2743 let doc_of attrs =
2744 let rec fold path page rely pan = function
2745 | ("path", v) :: rest -> fold v page rely pan rest
2746 | ("page", v) :: rest -> fold path v rely pan rest
2747 | ("rely", v) :: rest -> fold path page v pan rest
2748 | ("pan", v) :: rest -> fold path page rely v rest
2749 | _ :: rest -> fold path page rely pan rest
2750 | [] -> path, page, rely, pan
2752 fold "" "0" "0" "0" attrs
2755 let setconf dst src =
2756 dst.scrollw <- src.scrollw;
2757 dst.scrollh <- src.scrollh;
2758 dst.icase <- src.icase;
2759 dst.preload <- src.preload;
2760 dst.pagebias <- src.pagebias;
2761 dst.verbose <- src.verbose;
2762 dst.scrollstep <- src.scrollstep;
2763 dst.maxhfit <- src.maxhfit;
2764 dst.crophack <- src.crophack;
2765 dst.autoscrollstep <- src.autoscrollstep;
2766 dst.showall <- src.showall;
2767 dst.hlinks <- src.hlinks;
2768 dst.underinfo <- src.underinfo;
2769 dst.interpagespace <- src.interpagespace;
2770 dst.zoom <- src.zoom;
2771 dst.presentation <- src.presentation;
2772 dst.angle <- src.angle;
2773 dst.winw <- src.winw;
2774 dst.winh <- src.winh;
2775 dst.savebmarks <- src.savebmarks;
2776 dst.memlimit <- src.memlimit;
2777 dst.proportional <- src.proportional;
2778 dst.texcount <- src.texcount;
2779 dst.sliceheight <- src.sliceheight;
2780 dst.thumbw <- src.thumbw;
2781 dst.jumpback <- src.jumpback;
2784 let unent s =
2785 let l = String.length s in
2786 let b = Buffer.create l in
2787 unent b s 0 l;
2788 Buffer.contents b;
2791 let get s =
2792 let h = Hashtbl.create 10 in
2793 let dc = { defconf with angle = defconf.angle } in
2794 let rec toplevel v t spos epos =
2795 match t with
2796 | Vdata | Vcdata | Vend -> v
2797 | Vopen ("llppconfig", attrs, closed) ->
2798 if closed
2799 then v
2800 else { v with f = llppconfig }
2801 | Vopen _ ->
2802 error "unexpected subelement at top level" s spos
2803 | Vclose tag -> error "unexpected close at top level" s spos
2805 and llppconfig v t spos epos =
2806 match t with
2807 | Vdata | Vcdata | Vend -> v
2808 | Vopen ("defaults", attrs, closed) ->
2809 let c = config_of dc attrs in
2810 setconf dc c;
2811 if closed
2812 then v
2813 else { v with f = skip "defaults" (fun () -> v) }
2815 | Vopen ("doc", attrs, closed) ->
2816 let pathent, spage, srely, span = doc_of attrs in
2817 let path = unent pathent
2818 and pageno = fromstring int_of_string spos "page" spage 0
2819 and rely = fromstring float_of_string spos "rely" srely 0.0
2820 and pan = fromstring int_of_string spos "pan" span 0 in
2821 let c = config_of dc attrs in
2822 let anchor = (pageno, rely) in
2823 if closed
2824 then (Hashtbl.add h path (c, [], pan, anchor); v)
2825 else { v with f = doc path pan anchor c [] }
2827 | Vopen (tag, _, closed) ->
2828 error "unexpected subelement in llppconfig" s spos
2830 | Vclose "llppconfig" -> { v with f = toplevel }
2831 | Vclose tag -> error "unexpected close in llppconfig" s spos
2833 and doc path pan anchor c bookmarks v t spos epos =
2834 match t with
2835 | Vdata | Vcdata -> v
2836 | Vend -> error "unexpected end of input in doc" s spos
2837 | Vopen ("bookmarks", attrs, closed) ->
2838 { v with f = pbookmarks path pan anchor c bookmarks }
2840 | Vopen (tag, _, _) ->
2841 error "unexpected subelement in doc" s spos
2843 | Vclose "doc" ->
2844 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
2845 { v with f = llppconfig }
2847 | Vclose tag -> error "unexpected close in doc" s spos
2849 and pbookmarks path pan anchor c bookmarks v t spos epos =
2850 match t with
2851 | Vdata | Vcdata -> v
2852 | Vend -> error "unexpected end of input in bookmarks" s spos
2853 | Vopen ("item", attrs, closed) ->
2854 let titleent, spage, srely = bookmark_of attrs in
2855 let page = fromstring int_of_string spos "page" spage 0
2856 and rely = fromstring float_of_string spos "rely" srely 0.0 in
2857 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2858 if closed
2859 then { v with f = pbookmarks path pan anchor c bookmarks }
2860 else
2861 let f () = v in
2862 { v with f = skip "item" f }
2864 | Vopen _ ->
2865 error "unexpected subelement in bookmarks" s spos
2867 | Vclose "bookmarks" ->
2868 { v with f = doc path pan anchor c bookmarks }
2870 | Vclose tag -> error "unexpected close in bookmarks" s spos
2872 and skip tag f v t spos epos =
2873 match t with
2874 | Vdata | Vcdata -> v
2875 | Vend ->
2876 error ("unexpected end of input in skipped " ^ tag) s spos
2877 | Vopen (tag', _, closed) ->
2878 if closed
2879 then v
2880 else
2881 let f' () = { v with f = skip tag f } in
2882 { v with f = skip tag' f' }
2883 | Vclose ctag ->
2884 if tag = ctag
2885 then f ()
2886 else error ("unexpected close in skipped " ^ tag) s spos
2889 parse { f = toplevel; accu = () } s;
2890 h, dc;
2893 let do_load f ic =
2895 let len = in_channel_length ic in
2896 let s = String.create len in
2897 really_input ic s 0 len;
2898 f s;
2899 with
2900 | Parse_error (msg, s, pos) ->
2901 let subs = subs s pos in
2902 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2903 failwith ("parse error: " ^ s)
2905 | exn ->
2906 failwith ("config load error: " ^ Printexc.to_string exn)
2909 let path =
2910 let dir =
2912 let dir = Filename.concat home ".config" in
2913 if Sys.is_directory dir then dir else home
2914 with _ -> home
2916 Filename.concat dir "llpp.conf"
2919 let load1 f =
2920 if Sys.file_exists path
2921 then
2922 match
2923 (try Some (open_in_bin path)
2924 with exn ->
2925 prerr_endline
2926 ("Error opening configuation file `" ^ path ^ "': " ^
2927 Printexc.to_string exn);
2928 None
2930 with
2931 | Some ic ->
2932 begin try
2933 f (do_load get ic)
2934 with exn ->
2935 prerr_endline
2936 ("Error loading configuation from `" ^ path ^ "': " ^
2937 Printexc.to_string exn);
2938 end;
2939 close_in ic;
2941 | None -> ()
2942 else
2943 f (Hashtbl.create 0, defconf)
2946 let load () =
2947 let f (h, dc) =
2948 let pc, pb, px, pa =
2950 Hashtbl.find h (Filename.basename state.path)
2951 with Not_found -> dc, [], 0, (0, 0.0)
2953 setconf defconf dc;
2954 setconf conf pc;
2955 state.bookmarks <- pb;
2956 state.x <- px;
2957 if conf.jumpback
2958 then state.anchor <- pa
2959 else cbput state.hists.nav pa;
2961 load1 f
2964 let add_attrs bb always dc c =
2965 let ob s a b =
2966 if always || a != b
2967 then Printf.bprintf bb "\n %s='%b'" s a
2968 and oi s a b =
2969 if always || a != b
2970 then Printf.bprintf bb "\n %s='%d'" s a
2971 and oz s a b =
2972 if always || a <> b
2973 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2975 let w, h =
2976 if always
2977 then dc.winw, dc.winh
2978 else
2979 match state.fullscreen with
2980 | Some wh -> wh
2981 | None -> c.winw, c.winh
2983 let zoom, presentation, interpagespace, showall=
2984 if always
2985 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2986 else
2987 match state.mode with
2988 | Birdseye (bc, _, _, _, _) ->
2989 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2990 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2992 oi "width" w dc.winw;
2993 oi "height" h dc.winh;
2994 oi "scroll-bar-width" c.scrollw dc.scrollw;
2995 oi "scroll-handle-height" c.scrollh dc.scrollh;
2996 ob "case-insensitive-search" c.icase dc.icase;
2997 ob "preload" c.preload dc.preload;
2998 oi "page-bias" c.pagebias dc.pagebias;
2999 oi "scroll-step" c.scrollstep dc.scrollstep;
3000 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3001 ob "max-height-fit" c.maxhfit dc.maxhfit;
3002 ob "crop-hack" c.crophack dc.crophack;
3003 ob "throttle" showall dc.showall;
3004 ob "highlight-links" c.hlinks dc.hlinks;
3005 ob "under-cursor-info" c.underinfo dc.underinfo;
3006 oi "vertical-margin" interpagespace dc.interpagespace;
3007 oz "zoom" zoom dc.zoom;
3008 ob "presentation" presentation dc.presentation;
3009 oi "rotation-angle" c.angle dc.angle;
3010 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3011 ob "proportional-display" c.proportional dc.proportional;
3012 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3013 oi "texcount" c.texcount dc.texcount;
3014 oi "slice-height" c.sliceheight dc.sliceheight;
3015 oi "thumbnail-width" c.thumbw dc.thumbw;
3016 ob "persistent-location" c.jumpback dc.jumpback;
3019 let save () =
3020 let bb = Buffer.create 32768 in
3021 let f (h, dc) =
3022 Buffer.add_string bb "<llppconfig>\n<defaults ";
3023 add_attrs bb true dc dc;
3024 Buffer.add_string bb "/>\n";
3026 let adddoc path pan anchor c bookmarks =
3027 if bookmarks == [] && c = dc && anchor = emptyanchor
3028 then ()
3029 else (
3030 Printf.bprintf bb "<doc path='%s'"
3031 (enent path 0 (String.length path));
3033 if anchor <> emptyanchor
3034 then (
3035 let n, y = anchor in
3036 Printf.bprintf bb " page='%d'" n;
3037 if y > 1e-6
3038 then
3039 Printf.bprintf bb " rely='%f'" y
3043 if pan != 0
3044 then Printf.bprintf bb " pan='%d'" pan;
3046 add_attrs bb false dc c;
3048 begin match bookmarks with
3049 | [] -> Buffer.add_string bb "/>\n"
3050 | _ ->
3051 Buffer.add_string bb ">\n<bookmarks>\n";
3052 List.iter (fun (title, _level, page, rely) ->
3053 Printf.bprintf bb
3054 "<item title='%s' page='%d'"
3055 (enent title 0 (String.length title))
3056 page
3058 if rely > 1e-6
3059 then
3060 Printf.bprintf bb " rely='%f'" rely
3062 Buffer.add_string bb "/>\n";
3063 ) bookmarks;
3064 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3065 end;
3069 let pan =
3070 match state.mode with
3071 | Birdseye (_, pan, _, _, _) -> pan
3072 | _ -> state.x
3074 let basename = Filename.basename state.path in
3075 adddoc basename pan (getanchor ())
3076 { conf with
3077 autoscrollstep =
3078 if state.ascrollstep > 0
3079 then state.ascrollstep
3080 else conf.autoscrollstep }
3081 (if conf.savebmarks then state.bookmarks else []);
3083 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3084 if basename <> path
3085 then adddoc path x y c bookmarks
3086 ) h;
3087 Buffer.add_string bb "</llppconfig>";
3089 load1 f;
3090 if Buffer.length bb > 0
3091 then
3093 let tmp = path ^ ".tmp" in
3094 let oc = open_out_bin tmp in
3095 Buffer.output_buffer oc bb;
3096 close_out oc;
3097 Sys.rename tmp path;
3098 with exn ->
3099 prerr_endline
3100 ("error while saving configuration: " ^ Printexc.to_string exn)
3102 end;;
3104 let () =
3105 Arg.parse
3106 (Arg.align
3107 ["-p", Arg.String (fun s -> state.password <- s) , " Set password"
3108 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3109 " Print version and exit")]
3111 (fun s -> state.path <- s)
3112 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3114 if String.length state.path = 0
3115 then (prerr_endline "file name missing"; exit 1);
3117 State.load ();
3119 let _ = Glut.init Sys.argv in
3120 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3121 let () = Glut.initWindowSize conf.winw conf.winh in
3122 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3124 let csock, ssock =
3125 if Sys.os_type = "Unix"
3126 then
3127 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3128 else
3129 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3130 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3131 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3132 Unix.bind sock addr;
3133 Unix.listen sock 1;
3134 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3135 Unix.connect csock addr;
3136 let ssock, _ = Unix.accept sock in
3137 Unix.close sock;
3138 let opts sock =
3139 Unix.setsockopt sock Unix.TCP_NODELAY true;
3140 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3142 opts ssock;
3143 opts csock;
3144 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3145 ssock, csock
3148 let () = Glut.displayFunc display in
3149 let () = Glut.reshapeFunc reshape in
3150 let () = Glut.keyboardFunc keyboard in
3151 let () = Glut.specialFunc special in
3152 let () = Glut.idleFunc (Some idle) in
3153 let () = Glut.mouseFunc mouse in
3154 let () = Glut.motionFunc motion in
3155 let () = Glut.passiveMotionFunc pmotion in
3157 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
3158 state.csock <- csock;
3159 state.ssock <- ssock;
3160 state.text <- "Opening " ^ state.path;
3161 writeopen state.path state.password;
3163 at_exit State.save;
3165 let rec handlelablglutbug () =
3167 Glut.mainLoop ();
3168 with Glut.BadEnum "key in special_of_int" ->
3169 showtext '!' " LablGlut bug: special key not recognized";
3170 handlelablglutbug ()
3172 handlelablglutbug ();