Make proportional display user configurable
[llpp.git] / main.ml
blobe2ad69643146af573a86d8aecfa9b7a097821496
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let log fmt = Printf.kprintf prerr_endline fmt;;
9 let dolog fmt = Printf.kprintf prerr_endline fmt;;
11 external init : Unix.file_descr -> unit = "ml_init";;
12 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
13 external seltext : string -> (int * int * int * int) -> int -> unit =
14 "ml_seltext";;
15 external copysel : string -> unit = "ml_copysel";;
16 external getpdimrect : int -> float array = "ml_getpdimrect";;
17 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
18 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
20 type mpos = int * int
21 and mstate =
22 | Msel of (mpos * mpos)
23 | Mpan of mpos
24 | Mscroll
25 | Mnone
28 type 'a circbuf =
29 { store : 'a array
30 ; mutable rc : int
31 ; mutable wc : int
32 ; mutable len : int
36 type textentry = (char * string * onhist option * onkey * ondone)
37 and onkey = string -> int -> te
38 and ondone = string -> unit
39 and onhist = histcmd -> string
40 and histcmd = HCnext | HCprev | HCfirst | HClast
41 and te =
42 | TEstop
43 | TEdone of string
44 | TEcont of string
45 | TEswitch of textentry
48 let cbnew n v =
49 { store = Array.create n v
50 ; rc = 0
51 ; wc = 0
52 ; len = 0
56 let cblen b = Array.length b.store;;
58 let cbput b v =
59 let len = cblen b in
60 b.store.(b.wc) <- v;
61 b.wc <- (b.wc + 1) mod len;
62 b.len <- min (b.len + 1) len;
65 let cbpeekw b = b.store.(b.wc);;
67 let cbget b dir =
68 if b.len = 0
69 then b.store.(0)
70 else
71 let rc = b.rc + dir in
72 let rc = if rc = -1 then b.len - 1 else rc in
73 let rc = if rc = b.len then 0 else rc in
74 b.rc <- rc;
75 b.store.(rc);
78 let cbrfollowlen b =
79 b.rc <- b.len;
82 let cbclear b v =
83 b.len <- 0;
84 Array.fill b.store 0 (Array.length b.store) v;
87 type layout =
88 { pageno : int
89 ; pagedimno : int
90 ; pagew : int
91 ; pageh : int
92 ; pagedispy : int
93 ; pagey : int
94 ; pagevh : int
95 ; pagex : int
99 type conf =
100 { mutable scrollw : int
101 ; mutable scrollh : int
102 ; mutable icase : bool
103 ; mutable preload : bool
104 ; mutable pagebias : int
105 ; mutable verbose : bool
106 ; mutable scrollincr : int
107 ; mutable maxhfit : bool
108 ; mutable crophack : bool
109 ; mutable autoscroll : bool
110 ; mutable showall : bool
111 ; mutable hlinks : bool
112 ; mutable underinfo : bool
113 ; mutable interpagespace : int
114 ; mutable zoom : float
115 ; mutable presentation : bool
116 ; mutable angle : int
117 ; mutable winw : int
118 ; mutable winh : int
119 ; mutable savebmarks : bool
120 ; mutable proportional : bool
124 type outline = string * int * int * float;;
125 type outlines =
126 | Oarray of outline array
127 | Olist of outline list
128 | Onarrow of string * outline array * outline array
131 type rect = (float * float * float * float * float * float * float * float);;
133 type state =
134 { mutable csock : Unix.file_descr
135 ; mutable ssock : Unix.file_descr
136 ; mutable w : int
137 ; mutable x : int
138 ; mutable y : int
139 ; mutable ty : float
140 ; mutable maxy : int
141 ; mutable layout : layout list
142 ; pagemap : ((int * int * int * bool), string) Hashtbl.t
143 ; mutable pdims : (int * int * int * int) list
144 ; mutable pagecount : int
145 ; pagecache : string circbuf
146 ; mutable rendering : bool
147 ; mutable mstate : mstate
148 ; mutable searchpattern : string
149 ; mutable rects : (int * int * rect) list
150 ; mutable rects1 : (int * int * rect) list
151 ; mutable text : string
152 ; mutable fullscreen : (int * int) option
153 ; mutable textentry : textentry option
154 ; mutable outlines : outlines
155 ; mutable outline : (bool * int * int * outline array * string) option
156 ; mutable bookmarks : outline list
157 ; mutable path : string
158 ; mutable password : string
159 ; mutable invalidated : int
160 ; mutable colorscale : float
161 ; hists : hists
163 and hists =
164 { pat : string circbuf
165 ; pag : string circbuf
166 ; nav : float circbuf
170 let conf =
171 { scrollw = 7
172 ; scrollh = 12
173 ; icase = true
174 ; preload = true
175 ; pagebias = 0
176 ; verbose = false
177 ; scrollincr = 24
178 ; maxhfit = true
179 ; crophack = false
180 ; autoscroll = false
181 ; showall = false
182 ; hlinks = false
183 ; underinfo = false
184 ; interpagespace = 2
185 ; zoom = 1.0
186 ; presentation = false
187 ; angle = 0
188 ; winw = 900
189 ; winh = 900
190 ; savebmarks = true
191 ; proportional = true
195 let defconf = { conf with angle=conf.angle };;
197 let state =
198 { csock = Unix.stdin
199 ; ssock = Unix.stdin
200 ; w = 0
201 ; y = 0
202 ; x = 0
203 ; ty = 0.0
204 ; layout = []
205 ; maxy = max_int
206 ; pagemap = Hashtbl.create 10
207 ; pagecache = cbnew 10 ""
208 ; pdims = []
209 ; pagecount = 0
210 ; rendering = false
211 ; mstate = Mnone
212 ; rects = []
213 ; rects1 = []
214 ; text = ""
215 ; fullscreen = None
216 ; textentry = None
217 ; searchpattern = ""
218 ; outlines = Olist []
219 ; outline = None
220 ; bookmarks = []
221 ; path = ""
222 ; password = ""
223 ; invalidated = 0
224 ; hists =
225 { nav = cbnew 100 0.0
226 ; pat = cbnew 20 ""
227 ; pag = cbnew 10 ""
229 ; colorscale = 1.0
233 let vlog fmt =
234 if conf.verbose
235 then
236 Printf.kprintf prerr_endline fmt
237 else
238 Printf.kprintf ignore fmt
241 let writecmd fd s =
242 let len = String.length s in
243 let n = 4 + len in
244 let b = Buffer.create n in
245 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
246 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
247 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
248 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
249 Buffer.add_string b s;
250 let s' = Buffer.contents b in
251 let n' = Unix.write fd s' 0 n in
252 if n' != n then failwith "write failed";
255 let readcmd fd =
256 let s = "xxxx" in
257 let n = Unix.read fd s 0 4 in
258 if n != 4 then failwith "incomplete read(len)";
259 let len = 0
260 lor (Char.code s.[0] lsl 24)
261 lor (Char.code s.[1] lsl 16)
262 lor (Char.code s.[2] lsl 8)
263 lor (Char.code s.[3] lsl 0)
265 let s = String.create len in
266 let n = Unix.read fd s 0 len in
267 if n != len then failwith "incomplete read(data)";
271 let yratio y =
272 if y = state.maxy
273 then 1.0
274 else float y /. float state.maxy
277 let makecmd s l =
278 let b = Buffer.create 10 in
279 Buffer.add_string b s;
280 let rec combine = function
281 | [] -> b
282 | x :: xs ->
283 Buffer.add_char b ' ';
284 let s =
285 match x with
286 | `b b -> if b then "1" else "0"
287 | `s s -> s
288 | `i i -> string_of_int i
289 | `f f -> string_of_float f
290 | `I f -> string_of_int (truncate f)
292 Buffer.add_string b s;
293 combine xs;
295 combine l;
298 let wcmd s l =
299 let cmd = Buffer.contents (makecmd s l) in
300 writecmd state.csock cmd;
303 let calcips h =
304 if conf.presentation
305 then
306 let d = conf.winh - h in
307 max 0 ((d + 1) / 2)
308 else
309 conf.interpagespace
312 let calcheight () =
313 let rec f pn ph pi fh l =
314 match l with
315 | (n, _, h, _) :: rest ->
316 let ips = calcips h in
317 let fh =
318 if conf.presentation
319 then fh+ips
320 else fh
322 let fh = fh + ((n - pn) * (ph + pi)) in
323 f n h ips fh rest
325 | [] ->
326 let inc =
327 if conf.presentation
328 then 0
329 else -pi
331 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
332 max 0 fh
334 let fh = f 0 0 0 0 state.pdims in
338 let getpageyh pageno =
339 let rec f pn ph pi y l =
340 match l with
341 | (n, _, h, _) :: rest ->
342 let ips = calcips h in
343 if n >= pageno
344 then
345 if conf.presentation && n = pageno
346 then
347 y + (pageno - pn) * (ph + pi) + pi, h
348 else
349 y + (pageno - pn) * (ph + pi), h
350 else
351 let y = y + (if conf.presentation then pi else 0) in
352 let y = y + (n - pn) * (ph + pi) in
353 f n h ips y rest
355 | [] ->
356 y + (pageno - pn) * (ph + pi), ph
358 f 0 0 0 0 state.pdims
361 let getpagey pageno = fst (getpageyh pageno);;
363 let layout y sh =
364 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
365 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
366 match pdims with
367 | (pageno', w, h, x) :: rest when pageno' = pageno ->
368 let ips = calcips h in
369 let yinc = if conf.presentation then ips else 0 in
370 (w, h, ips, x), rest, pdimno + 1, yinc
371 | _ ->
372 prev, pdims, pdimno, 0
374 let dy = dy + yinc in
375 let py = py + yinc in
376 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
377 then
378 accu
379 else
380 let vy = y + dy in
381 if py + h <= vy - yinc
382 then
383 let py = py + h + ips in
384 let dy = max 0 (py - y) in
385 f ~pageno:(pageno+1)
386 ~pdimno
387 ~prev:curr
390 ~pdims:rest
391 ~cacheleft
392 ~accu
393 else
394 let pagey = vy - py in
395 let pagevh = h - pagey in
396 let pagevh = min (sh - dy) pagevh in
397 let off = if yinc > 0 then py - vy else 0
399 let py = py + h + ips in
400 let e =
401 { pageno = pageno
402 ; pagedimno = pdimno
403 ; pagew = w
404 ; pageh = h
405 ; pagedispy = dy + off
406 ; pagey = pagey + off
407 ; pagevh = pagevh - off
408 ; pagex = x
411 let accu = e :: accu in
412 f ~pageno:(pageno+1)
413 ~pdimno
414 ~prev:curr
416 ~dy:(dy+pagevh+ips)
417 ~pdims:rest
418 ~cacheleft:(cacheleft-1)
419 ~accu
421 if state.invalidated = 0
422 then (
423 let accu =
425 ~pageno:0
426 ~pdimno:~-1
427 ~prev:(0,0,0,0)
428 ~py:0
429 ~dy:0
430 ~pdims:state.pdims
431 ~cacheleft:(cblen state.pagecache)
432 ~accu:[]
434 List.rev accu
436 else
440 let clamp incr =
441 let y = state.y + incr in
442 let y = max 0 y in
443 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
447 let getopaque pageno =
448 try Some (Hashtbl.find state.pagemap
449 (pageno + 1, state.w, conf.angle, conf.proportional))
450 with Not_found -> None
453 let cache pageno opaque =
454 Hashtbl.replace state.pagemap
455 (pageno + 1, state.w, conf.angle, conf.proportional) opaque
458 let validopaque opaque = String.length opaque > 0;;
460 let render l =
461 match getopaque l.pageno with
462 | None when not state.rendering ->
463 state.rendering <- true;
464 cache l.pageno "";
465 wcmd "render" [`i (l.pageno + 1)
466 ;`i l.pagedimno
467 ;`i l.pagew
468 ;`i l.pageh];
470 | _ -> ()
473 let loadlayout layout =
474 let rec f all = function
475 | l :: ls ->
476 begin match getopaque l.pageno with
477 | None -> render l; f false ls
478 | Some opaque -> f (all && validopaque opaque) ls
480 | [] -> all
482 f (layout <> []) layout;
485 let preload () =
486 if conf.preload
487 then
488 let evictedvisible =
489 let evictedopaque = cbpeekw state.pagecache in
490 List.exists (fun l ->
491 match getopaque l.pageno with
492 | Some opaque when validopaque opaque ->
493 evictedopaque = opaque
494 | otherwise -> false
495 ) state.layout
497 if not evictedvisible
498 then
499 let rely = yratio state.y in
500 let presentation = conf.presentation in
501 let interpagespace = conf.interpagespace in
502 let maxy = state.maxy in
503 conf.presentation <- false;
504 conf.interpagespace <- 0;
505 state.maxy <- calcheight ();
506 let y = truncate (float state.maxy *. rely) in
507 let y = if y < conf.winh then 0 else y - conf.winh in
508 let pages = layout y (conf.winh*3) in
509 List.iter render pages;
510 conf.presentation <- presentation;
511 conf.interpagespace <- interpagespace;
512 state.maxy <- maxy;
515 let gotoy y =
516 let y = max 0 y in
517 let y = min state.maxy y in
518 let pages = layout y conf.winh in
519 let ready = loadlayout pages in
520 state.ty <- yratio y;
521 if conf.showall
522 then (
523 if ready
524 then (
525 state.layout <- pages;
526 state.y <- y;
527 Glut.postRedisplay ();
530 else (
531 state.layout <- pages;
532 state.y <- y;
533 Glut.postRedisplay ();
535 preload ();
538 let gotoy_and_clear_text y =
539 gotoy y;
540 if not conf.verbose then state.text <- "";
543 let addnav () =
544 cbput state.hists.nav (yratio state.y);
545 cbrfollowlen state.hists.nav;
548 let getnav () =
549 let y = cbget state.hists.nav ~-1 in
550 truncate (y *. float state.maxy)
553 let gotopage n top =
554 let y, h = getpageyh n in
555 addnav ();
556 gotoy_and_clear_text (y + (truncate (top *. float h)));
559 let gotopage1 n top =
560 let y = getpagey n in
561 addnav ();
562 gotoy_and_clear_text (y + top);
565 let invalidate () =
566 state.layout <- [];
567 state.pdims <- [];
568 state.rects <- [];
569 state.rects1 <- [];
570 state.invalidated <- state.invalidated + 1;
573 let scalecolor c =
574 let c = c *. state.colorscale in
575 (c, c, c);
578 let represent () =
579 let y =
580 match state.layout with
581 | [] ->
582 let rely = yratio state.y in
583 state.maxy <- calcheight ();
584 truncate (float state.maxy *. rely)
586 | l :: _ ->
587 state.maxy <- calcheight ();
588 getpagey l.pageno
590 gotoy y
593 let pagematrix () =
594 GlMat.mode `projection;
595 GlMat.load_identity ();
596 GlMat.rotate ~x:1.0 ~angle:180.0 ();
597 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
598 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
601 let winmatrix () =
602 GlMat.mode `projection;
603 GlMat.load_identity ();
604 GlMat.rotate ~x:1.0 ~angle:180.0 ();
605 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
606 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
609 let reshape ~w ~h =
610 conf.winw <- w;
611 let w = truncate (float w *. conf.zoom) - conf.scrollw in
612 state.w <- w;
613 conf.winh <- h;
614 GlMat.mode `modelview;
615 GlMat.load_identity ();
616 GlClear.color (scalecolor 1.0);
617 GlClear.clear [`color];
619 invalidate ();
620 wcmd "geometry" [`i w; `i h];
623 let showtext c s =
624 GlDraw.color (0.0, 0.0, 0.0);
625 GlDraw.rect
626 (0.0, float (conf.winh - 18))
627 (float (conf.winw - conf.scrollw - 1), float conf.winh)
629 let font = Glut.BITMAP_8_BY_13 in
630 GlDraw.color (1.0, 1.0, 1.0);
631 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
632 Glut.bitmapCharacter ~font ~c:(Char.code c);
633 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
636 let enttext () =
637 let len = String.length state.text in
638 match state.textentry with
639 | None ->
640 if len > 0 then showtext ' ' state.text
642 | Some (c, text, _, _, _) ->
643 let s =
644 if len > 0
645 then
646 text ^ " [" ^ state.text ^ "]"
647 else
648 text
650 showtext c s;
653 let showtext c s =
654 if true
655 then (
656 state.text <- Printf.sprintf "%c%s" c s;
657 Glut.postRedisplay ();
659 else (
660 showtext c s;
661 Glut.swapBuffers ();
665 let act cmd =
666 match cmd.[0] with
667 | 'c' ->
668 state.pdims <- [];
670 | 'D' ->
671 state.rects <- state.rects1;
672 Glut.postRedisplay ()
674 | 'C' ->
675 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
676 state.pagecount <- n;
677 state.invalidated <- state.invalidated - 1;
678 if state.invalidated = 0
679 then represent ()
681 | 't' ->
682 let s = Scanf.sscanf cmd "t %n"
683 (fun n -> String.sub cmd n (String.length cmd - n))
685 Glut.setWindowTitle s
687 | 'T' ->
688 let s = Scanf.sscanf cmd "T %n"
689 (fun n -> String.sub cmd n (String.length cmd - n))
691 if state.textentry = None
692 then (
693 state.text <- s;
694 showtext ' ' s;
696 else (
697 state.text <- s;
698 Glut.postRedisplay ();
701 | 'V' ->
702 if conf.verbose
703 then
704 let s = Scanf.sscanf cmd "V %n"
705 (fun n -> String.sub cmd n (String.length cmd - n))
707 state.text <- s;
708 showtext ' ' s;
710 | 'F' ->
711 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
712 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
713 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
714 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
716 let y = (getpagey pageno) + truncate y0 in
717 addnav ();
718 gotoy y;
719 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
721 | 'R' ->
722 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
723 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
724 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
725 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
727 state.rects1 <-
728 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
730 | 'r' ->
731 let n, w, h, r, l, p =
732 Scanf.sscanf cmd "r %d %d %d %d %d %s"
733 (fun n w h r l p -> (n, w, h, r, l != 0, p))
735 Hashtbl.replace state.pagemap (n, w, r, l) p;
736 let opaque = cbpeekw state.pagecache in
737 if validopaque opaque
738 then (
739 let k =
740 Hashtbl.fold
741 (fun k v a -> if v = opaque then k else a)
742 state.pagemap (-1, -1, -1, false)
744 wcmd "free" [`s opaque];
745 Hashtbl.remove state.pagemap k
747 cbput state.pagecache p;
748 state.rendering <- false;
749 if conf.showall
750 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
751 else (
752 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
753 if visible
754 then gotoy state.y
755 else (ignore (loadlayout state.layout); preload ())
758 | 'l' ->
759 let (n, w, h, x) as pdim =
760 Scanf.sscanf cmd "l %d %d %d %d" (fun n w h x -> n, w, h, x)
762 state.pdims <- pdim :: state.pdims
764 | 'o' ->
765 let (l, n, t, h, pos) =
766 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
768 let s = String.sub cmd pos (String.length cmd - pos) in
769 let s =
770 let l = String.length s in
771 let b = Buffer.create (String.length s) in
772 let rec loop pc2 i =
773 if i = l
774 then ()
775 else
776 let pc2 =
777 match s.[i] with
778 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
779 | '\xc2' -> true
780 | c ->
781 let c = if Char.code c land 0x80 = 0 then c else '?' in
782 Buffer.add_char b c;
783 false
785 loop pc2 (i+1)
787 loop false 0;
788 Buffer.contents b
790 let outline = (s, l, n, float t /. float h) in
791 let outlines =
792 match state.outlines with
793 | Olist outlines -> Olist (outline :: outlines)
794 | Oarray _ -> Olist [outline]
795 | Onarrow _ -> Olist [outline]
797 state.outlines <- outlines
799 | _ ->
800 log "unknown cmd `%S'" cmd
803 let now = Unix.gettimeofday;;
805 let idle () =
806 let rec loop delay =
807 let r, _, _ = Unix.select [state.csock] [] [] delay in
808 begin match r with
809 | [] ->
810 if conf.autoscroll
811 then begin
812 let y = state.y + conf.scrollincr in
813 let y = if y >= state.maxy then 0 else y in
814 gotoy y;
815 state.text <- "";
816 end;
818 | _ ->
819 let cmd = readcmd state.csock in
820 act cmd;
821 loop 0.0
822 end;
823 in loop 0.001
826 let onhist cb = function
827 | HCprev -> cbget cb ~-1
828 | HCnext -> cbget cb 1
829 | HCfirst -> cbget cb ~-(cb.rc)
830 | HClast -> cbget cb (cb.len - 1 - cb.rc)
833 let search pattern forward =
834 if String.length pattern > 0
835 then
836 let pn, py =
837 match state.layout with
838 | [] -> 0, 0
839 | l :: _ ->
840 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
842 let cmd =
843 let b = makecmd "search"
844 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
846 Buffer.add_char b ',';
847 Buffer.add_string b pattern;
848 Buffer.add_char b '\000';
849 Buffer.contents b;
851 writecmd state.csock cmd;
854 let intentry text key =
855 let c = Char.unsafe_chr key in
856 match c with
857 | '0' .. '9' ->
858 let s = "x" in s.[0] <- c;
859 let text = text ^ s in
860 TEcont text
862 | _ ->
863 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
864 TEcont text
867 let addchar s c =
868 let b = Buffer.create (String.length s + 1) in
869 Buffer.add_string b s;
870 Buffer.add_char b c;
871 Buffer.contents b;
874 let textentry text key =
875 let c = Char.unsafe_chr key in
876 match c with
877 | _ when key >= 32 && key < 127 ->
878 let text = addchar text c in
879 TEcont text
881 | _ ->
882 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
883 TEcont text
886 let reinit angle proportional =
887 conf.angle <- angle;
888 conf.proportional <- proportional;
889 invalidate ();
890 wcmd "reinit" [`i angle; `b proportional];
893 let optentry text key =
894 let btos b = if b then "on" else "off" in
895 let c = Char.unsafe_chr key in
896 match c with
897 | 's' ->
898 let ondone s =
899 try conf.scrollincr <- int_of_string s with exc ->
900 state.text <- Printf.sprintf "bad integer `%s': %s"
901 s (Printexc.to_string exc)
903 TEswitch ('#', "", None, intentry, ondone)
905 | 'R' ->
906 let ondone s =
907 match try
908 Some (int_of_string s)
909 with exc ->
910 state.text <- Printf.sprintf "bad integer `%s': %s"
911 s (Printexc.to_string exc);
912 None
913 with
914 | Some angle -> reinit angle conf.proportional
915 | None -> ()
917 TEswitch ('^', "", None, intentry, ondone)
919 | 'i' ->
920 conf.icase <- not conf.icase;
921 TEdone ("case insensitive search " ^ (btos conf.icase))
923 | 'p' ->
924 conf.preload <- not conf.preload;
925 gotoy state.y;
926 TEdone ("preload " ^ (btos conf.preload))
928 | 'v' ->
929 conf.verbose <- not conf.verbose;
930 TEdone ("verbose " ^ (btos conf.verbose))
932 | 'h' ->
933 conf.maxhfit <- not conf.maxhfit;
934 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
935 TEdone ("maxhfit " ^ (btos conf.maxhfit))
937 | 'c' ->
938 conf.crophack <- not conf.crophack;
939 TEdone ("crophack " ^ btos conf.crophack)
941 | 'a' ->
942 conf.showall <- not conf.showall;
943 TEdone ("showall " ^ btos conf.showall)
945 | 'f' ->
946 conf.underinfo <- not conf.underinfo;
947 TEdone ("underinfo " ^ btos conf.underinfo)
949 | 'P' ->
950 conf.savebmarks <- not conf.savebmarks;
951 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
953 | 'S' ->
954 let ondone s =
956 conf.interpagespace <- int_of_string s;
957 let rely = yratio state.y in
958 state.maxy <- calcheight ();
959 gotoy (truncate (float state.maxy *. rely));
960 with exc ->
961 state.text <- Printf.sprintf "bad integer `%s': %s"
962 s (Printexc.to_string exc)
964 TEswitch ('%', "", None, intentry, ondone)
966 | 'l' ->
967 reinit conf.angle (not conf.proportional);
968 TEdone ("proprortional display " ^ btos conf.proportional)
970 | _ ->
971 state.text <- Printf.sprintf "bad option %d `%c'" key c;
972 TEstop
975 let maxoutlinerows () = (conf.winh - 31) / 16;;
977 let enterselector allowdel outlines errmsg msg =
978 if Array.length outlines = 0
979 then (
980 showtext ' ' errmsg;
982 else (
983 state.text <- msg;
984 Glut.setCursor Glut.CURSOR_INHERIT;
985 let pageno =
986 match state.layout with
987 | [] -> -1
988 | {pageno=pageno} :: rest -> pageno
990 let active =
991 let rec loop n =
992 if n = Array.length outlines
993 then 0
994 else
995 let (_, _, outlinepageno, _) = outlines.(n) in
996 if outlinepageno >= pageno then n else loop (n+1)
998 loop 0
1000 state.outline <-
1001 Some (allowdel, active,
1002 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1003 Glut.postRedisplay ();
1007 let enteroutlinemode () =
1008 let outlines, msg =
1009 match state.outlines with
1010 | Oarray a -> a, ""
1011 | Olist l ->
1012 let a = Array.of_list (List.rev l) in
1013 state.outlines <- Oarray a;
1014 a, ""
1015 | Onarrow (pat, a, b) ->
1016 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1018 enterselector false outlines "Document has no outline" msg;
1021 let enterbookmarkmode () =
1022 let bookmarks = Array.of_list state.bookmarks in
1023 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1026 let quickbookmark ?title () =
1027 match state.layout with
1028 | [] -> ()
1029 | l :: _ ->
1030 let title =
1031 match title with
1032 | None ->
1033 let sec = Unix.gettimeofday () in
1034 let tm = Unix.localtime sec in
1035 Printf.sprintf "Quick (page %d) visited (%d/%d/%d %d:%d)"
1036 (l.pageno+1)
1037 tm.Unix.tm_mday
1038 tm.Unix.tm_mon
1039 (tm.Unix.tm_year + 1900)
1040 tm.Unix.tm_hour
1041 tm.Unix.tm_min
1042 | Some title -> title
1044 state.bookmarks <-
1045 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1048 let doreshape w h =
1049 state.fullscreen <- None;
1050 Glut.reshapeWindow w h;
1053 let writeopen path password =
1054 writecmd state.csock
1055 ("open " ^ path ^ "\000"
1056 ^ state.password ^ "\000"
1057 ^ string_of_int conf.angle ^ " "
1058 ^ (if conf.proportional then "1" else "0"))
1062 let opendoc path password =
1063 invalidate ();
1064 state.path <- path;
1065 state.password <- password;
1066 Hashtbl.clear state.pagemap;
1068 writeopen path password;
1069 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1070 wcmd "geometry" [`i state.w; `i conf.winh];
1073 let viewkeyboard ~key ~x ~y =
1074 let enttext te =
1075 state.textentry <- te;
1076 state.text <- "";
1077 enttext ();
1078 Glut.postRedisplay ()
1080 match state.textentry with
1081 | None ->
1082 let c = Char.chr key in
1083 begin match c with
1084 | '\027' | 'q' ->
1085 exit 0
1087 | '\008' ->
1088 let y = getnav () in
1089 gotoy_and_clear_text y
1091 | 'o' ->
1092 enteroutlinemode ()
1094 | 'u' ->
1095 state.rects <- [];
1096 state.text <- "";
1097 Glut.postRedisplay ()
1099 | '/' | '?' ->
1100 let ondone isforw s =
1101 cbput state.hists.pat s;
1102 cbrfollowlen state.hists.pat;
1103 state.searchpattern <- s;
1104 search s isforw
1106 enttext (Some (c, "", Some (onhist state.hists.pat),
1107 textentry, ondone (c ='/')))
1109 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1110 conf.zoom <- min 2.2 (conf.zoom +. 0.1);
1111 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1112 reshape conf.winw conf.winh
1114 | '+' ->
1115 let ondone s =
1116 let n =
1117 try int_of_string s with exc ->
1118 state.text <- Printf.sprintf "bad integer `%s': %s"
1119 s (Printexc.to_string exc);
1120 max_int
1122 if n != max_int
1123 then (
1124 conf.pagebias <- n;
1125 state.text <- "page bias is now " ^ string_of_int n;
1128 enttext (Some ('+', "", None, intentry, ondone))
1130 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1131 conf.zoom <- max 0.1 (conf.zoom -. 0.1);
1132 if conf.zoom <= 1.0 then state.x <- 0;
1133 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1134 reshape conf.winw conf.winh;
1136 | '-' ->
1137 let ondone msg =
1138 state.text <- msg;
1140 enttext (Some ('-', "", None, optentry, ondone))
1142 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1143 state.x <- 0;
1144 conf.zoom <- 1.0;
1145 state.text <- "zoom is 100%";
1146 reshape conf.winw conf.winh
1148 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1149 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1150 if zoom < 1.0
1151 then (
1152 conf.zoom <- zoom;
1153 state.x <- 0;
1154 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1155 reshape conf.winw conf.winh;
1158 | '0' .. '9' ->
1159 let ondone s =
1160 let n =
1161 try int_of_string s with exc ->
1162 state.text <- Printf.sprintf "bad integer `%s': %s"
1163 s (Printexc.to_string exc);
1166 if n >= 0
1167 then (
1168 addnav ();
1169 cbput state.hists.pag (string_of_int n);
1170 cbrfollowlen state.hists.pag;
1171 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1174 let pageentry text key =
1175 match Char.unsafe_chr key with
1176 | 'g' -> TEdone text
1177 | _ -> intentry text key
1179 let text = "x" in text.[0] <- c;
1180 enttext (Some (':', text, Some (onhist state.hists.pag),
1181 pageentry, ondone))
1183 | 'b' ->
1184 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1185 reshape conf.winw conf.winh;
1187 | 'l' ->
1188 conf.hlinks <- not conf.hlinks;
1189 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1190 Glut.postRedisplay ()
1192 | 'a' ->
1193 conf.autoscroll <- not conf.autoscroll
1195 | 'P' ->
1196 conf.presentation <- not conf.presentation;
1197 showtext ' ' ("presentation mode " ^
1198 if conf.presentation then "on" else "off");
1199 represent ()
1201 | 'f' ->
1202 begin match state.fullscreen with
1203 | None ->
1204 state.fullscreen <- Some (conf.winw, conf.winh);
1205 Glut.fullScreen ()
1206 | Some (w, h) ->
1207 state.fullscreen <- None;
1208 doreshape w h
1211 | 'g' ->
1212 gotoy_and_clear_text 0
1214 | 'n' ->
1215 search state.searchpattern true
1217 | 'p' | 'N' ->
1218 search state.searchpattern false
1220 | 't' ->
1221 begin match state.layout with
1222 | [] -> ()
1223 | l :: _ ->
1224 gotoy_and_clear_text (getpagey l.pageno)
1227 | ' ' ->
1228 begin match List.rev state.layout with
1229 | [] -> ()
1230 | l :: _ ->
1231 let pageno = min (l.pageno+1) (state.pagecount-1) in
1232 gotoy_and_clear_text (getpagey pageno)
1235 | '\127' ->
1236 begin match state.layout with
1237 | [] -> ()
1238 | l :: _ ->
1239 let pageno = max 0 (l.pageno-1) in
1240 gotoy_and_clear_text (getpagey pageno)
1243 | '=' ->
1244 let f (fn, ln) l =
1245 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1247 let fn, ln = List.fold_left f (-1, -1) state.layout in
1248 let s =
1249 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1250 let percent =
1251 if maxy <= 0
1252 then 100.
1253 else (100. *. (float state.y /. float maxy)) in
1254 if fn = ln
1255 then
1256 Printf.sprintf "Page %d of %d %.2f%%"
1257 (fn+1) state.pagecount percent
1258 else
1259 Printf.sprintf
1260 "Pages %d-%d of %d %.2f%%"
1261 (fn+1) (ln+1) state.pagecount percent
1263 showtext ' ' s;
1265 | 'w' ->
1266 begin match state.layout with
1267 | [] -> ()
1268 | l :: _ ->
1269 doreshape (l.pagew + conf.scrollw) l.pageh;
1270 Glut.postRedisplay ();
1273 | '\'' ->
1274 enterbookmarkmode ()
1276 | 'm' ->
1277 let ondone s =
1278 match state.layout with
1279 | l :: _ ->
1280 state.bookmarks <-
1281 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1282 :: state.bookmarks
1283 | _ -> ()
1285 enttext (Some ('~', "", None, textentry, ondone))
1287 | '~' ->
1288 quickbookmark ();
1289 showtext ' ' "Quick bookmark added";
1291 | 'z' ->
1292 begin match state.layout with
1293 | l :: _ ->
1294 let rect = getpdimrect l.pagedimno in
1295 let w, h =
1296 if conf.crophack
1297 then
1298 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1299 truncate (1.2 *. (rect.(3) -. rect.(0))))
1300 else
1301 (truncate (rect.(1) -. rect.(0)),
1302 truncate (rect.(3) -. rect.(0)))
1304 if w != 0 && h != 0
1305 then
1306 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1308 Glut.postRedisplay ();
1310 | [] -> ()
1313 | '<' | '>' ->
1314 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1316 | '[' | ']' ->
1317 state.colorscale <-
1318 max 0.0
1319 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1320 Glut.postRedisplay ()
1322 | 'k' -> gotoy (clamp (-conf.scrollincr))
1323 | 'j' -> gotoy (clamp conf.scrollincr)
1325 | 'r' -> opendoc state.path state.password
1327 | _ ->
1328 vlog "huh? %d %c" key (Char.chr key);
1331 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1332 let len = String.length text in
1333 if len = 0
1334 then (
1335 state.textentry <- None;
1336 Glut.postRedisplay ();
1338 else (
1339 let s = String.sub text 0 (len - 1) in
1340 enttext (Some (c, s, onhist, onkey, ondone))
1343 | Some (c, text, onhist, onkey, ondone) ->
1344 begin match Char.unsafe_chr key with
1345 | '\r' | '\n' ->
1346 ondone text;
1347 state.textentry <- None;
1348 Glut.postRedisplay ()
1350 | '\027' ->
1351 state.textentry <- None;
1352 Glut.postRedisplay ()
1354 | _ ->
1355 begin match onkey text key with
1356 | TEdone text ->
1357 state.textentry <- None;
1358 ondone text;
1359 Glut.postRedisplay ()
1361 | TEcont text ->
1362 enttext (Some (c, text, onhist, onkey, ondone));
1364 | TEstop ->
1365 state.textentry <- None;
1366 Glut.postRedisplay ()
1368 | TEswitch te ->
1369 state.textentry <- Some te;
1370 Glut.postRedisplay ()
1371 end;
1372 end;
1375 let narrow outlines pattern =
1376 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1377 match reopt with
1378 | None -> None
1379 | Some re ->
1380 let rec fold accu n =
1381 if n = -1
1382 then accu
1383 else
1384 let (s, _, _, _) as o = outlines.(n) in
1385 let accu =
1386 if (try ignore (Str.search_forward re s 0); true
1387 with Not_found -> false)
1388 then (o :: accu)
1389 else accu
1391 fold accu (n-1)
1393 let matched = fold [] (Array.length outlines - 1) in
1394 if matched = [] then None else Some (Array.of_list matched)
1397 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1398 let search active pattern incr =
1399 let dosearch re =
1400 let rec loop n =
1401 if n = Array.length outlines || n = -1
1402 then None
1403 else
1404 let (s, _, _, _) = outlines.(n) in
1406 (try ignore (Str.search_forward re s 0); true
1407 with Not_found -> false)
1408 then Some n
1409 else loop (n + incr)
1411 loop active
1414 let re = Str.regexp_case_fold pattern in
1415 dosearch re
1416 with Failure s ->
1417 state.text <- s;
1418 None
1420 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1421 match key with
1422 | 27 ->
1423 if String.length qsearch = 0
1424 then (
1425 state.text <- "";
1426 state.outline <- None;
1427 Glut.postRedisplay ();
1429 else (
1430 state.text <- "";
1431 state.outline <- Some (allowdel, active, first, outlines, "");
1432 Glut.postRedisplay ();
1435 | 18 | 19 ->
1436 let incr = if key = 18 then -1 else 1 in
1437 let active, first =
1438 match search (active + incr) qsearch incr with
1439 | None ->
1440 state.text <- qsearch ^ " [not found]";
1441 active, first
1442 | Some active ->
1443 state.text <- qsearch;
1444 active, firstof active
1446 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1447 Glut.postRedisplay ();
1449 | 8 ->
1450 let len = String.length qsearch in
1451 if len = 0
1452 then ()
1453 else (
1454 if len = 1
1455 then (
1456 state.text <- "";
1457 state.outline <- Some (allowdel, active, first, outlines, "");
1459 else
1460 let qsearch = String.sub qsearch 0 (len - 1) in
1461 let active, first =
1462 match search active qsearch ~-1 with
1463 | None ->
1464 state.text <- qsearch ^ " [not found]";
1465 active, first
1466 | Some active ->
1467 state.text <- qsearch;
1468 active, firstof active
1470 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1472 Glut.postRedisplay ()
1474 | 13 ->
1475 if active < Array.length outlines
1476 then (
1477 let (_, _, n, t) = outlines.(active) in
1478 gotopage n t;
1480 state.text <- "";
1481 if allowdel then state.bookmarks <- Array.to_list outlines;
1482 state.outline <- None;
1483 Glut.postRedisplay ();
1485 | _ when key >= 32 && key < 127 ->
1486 let pattern = addchar qsearch (Char.chr key) in
1487 let active, first =
1488 match search active pattern 1 with
1489 | None ->
1490 state.text <- pattern ^ " [not found]";
1491 active, first
1492 | Some active ->
1493 state.text <- pattern;
1494 active, firstof active
1496 state.outline <- Some (allowdel, active, first, outlines, pattern);
1497 Glut.postRedisplay ()
1499 | 14 when not allowdel -> (* ctrl-n *)
1500 if String.length qsearch > 0
1501 then (
1502 let optoutlines = narrow outlines qsearch in
1503 begin match optoutlines with
1504 | None -> state.text <- "can't narrow"
1505 | Some outlines ->
1506 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1507 match state.outlines with
1508 | Olist l -> ()
1509 | Oarray a ->
1510 state.outlines <- Onarrow (qsearch, outlines, a)
1511 | Onarrow (pat, a, b) ->
1512 state.outlines <- Onarrow (qsearch, outlines, b)
1513 end;
1515 Glut.postRedisplay ()
1517 | 21 when not allowdel -> (* ctrl-u *)
1518 let outline =
1519 match state.outlines with
1520 | Oarray a -> a
1521 | Olist l ->
1522 let a = Array.of_list (List.rev l) in
1523 state.outlines <- Oarray a;
1525 | Onarrow (pat, a, b) ->
1526 state.outlines <- Oarray b;
1527 state.text <- "";
1530 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1531 Glut.postRedisplay ()
1533 | 12 ->
1534 state.outline <-
1535 Some (allowdel, active, firstof active, outlines, qsearch);
1536 Glut.postRedisplay ()
1538 | 127 when allowdel ->
1539 let len = Array.length outlines - 1 in
1540 if len = 0
1541 then (
1542 state.outline <- None;
1543 state.bookmarks <- [];
1545 else (
1546 let bookmarks = Array.init len
1547 (fun i ->
1548 let i = if i >= active then i + 1 else i in
1549 outlines.(i)
1552 state.outline <-
1553 Some (allowdel,
1554 min active (len-1),
1555 min first (len-1),
1556 bookmarks, qsearch)
1559 Glut.postRedisplay ()
1561 | _ -> log "unknown key %d" key
1564 let keyboard ~key ~x ~y =
1565 if key = 7
1566 then
1567 wcmd "interrupt" []
1568 else
1569 match state.outline with
1570 | None -> viewkeyboard ~key ~x ~y
1571 | Some outline -> outlinekeyboard ~key ~x ~y outline
1574 let special ~key ~x ~y =
1575 match state.outline with
1576 | None ->
1577 begin match state.textentry with
1578 | None ->
1579 let y =
1580 match key with
1581 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1582 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1583 | Glut.KEY_DOWN -> clamp conf.scrollincr
1584 | Glut.KEY_PAGE_UP ->
1585 if Glut.getModifiers () land Glut.active_ctrl != 0
1586 then
1587 match state.layout with
1588 | [] -> state.y
1589 | l :: _ -> state.y - l.pagey
1590 else
1591 clamp (-conf.winh)
1592 | Glut.KEY_PAGE_DOWN ->
1593 if Glut.getModifiers () land Glut.active_ctrl != 0
1594 then
1595 match List.rev state.layout with
1596 | [] -> state.y
1597 | l :: _ -> getpagey l.pageno
1598 else
1599 clamp conf.winh
1600 | Glut.KEY_HOME -> addnav (); 0
1601 | Glut.KEY_END ->
1602 addnav ();
1603 state.maxy - (if conf.maxhfit then conf.winh else 0)
1605 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1606 state.x <- state.x - 10;
1607 state.y
1608 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1609 state.x <- state.x + 10;
1610 state.y
1612 | _ -> state.y
1614 gotoy_and_clear_text y
1616 | Some (c, s, Some onhist, onkey, ondone) ->
1617 let s =
1618 match key with
1619 | Glut.KEY_UP -> onhist HCprev
1620 | Glut.KEY_DOWN -> onhist HCnext
1621 | Glut.KEY_HOME -> onhist HCfirst
1622 | Glut.KEY_END -> onhist HClast
1623 | _ -> state.text
1625 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1626 Glut.postRedisplay ()
1628 | _ -> ()
1631 | Some (allowdel, active, first, outlines, qsearch) ->
1632 let maxrows = maxoutlinerows () in
1633 let navigate incr =
1634 let active = active + incr in
1635 let active = max 0 (min active (Array.length outlines - 1)) in
1636 let first =
1637 if active > first
1638 then
1639 let rows = active - first in
1640 if rows > maxrows then active - maxrows else first
1641 else active
1643 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1644 Glut.postRedisplay ()
1646 match key with
1647 | Glut.KEY_UP -> navigate ~-1
1648 | Glut.KEY_DOWN -> navigate 1
1649 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1650 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1652 | Glut.KEY_HOME ->
1653 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1654 Glut.postRedisplay ()
1656 | Glut.KEY_END ->
1657 let active = Array.length outlines - 1 in
1658 let first = max 0 (active - maxrows) in
1659 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1660 Glut.postRedisplay ()
1662 | _ -> ()
1665 let drawplaceholder l =
1666 GlDraw.color (scalecolor 1.0);
1667 GlDraw.rect
1668 (float l.pagex, float l.pagedispy)
1669 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1671 let x = float l.pagex
1672 and y = float (l.pagedispy + 13) in
1673 let font = Glut.BITMAP_8_BY_13 in
1674 GlDraw.color (0.0, 0.0, 0.0);
1675 GlPix.raster_pos ~x ~y ();
1676 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1677 ("Loading " ^ string_of_int (l.pageno + 1));
1680 let now () = Unix.gettimeofday ();;
1682 let drawpage i l =
1683 begin match getopaque l.pageno with
1684 | Some opaque when validopaque opaque ->
1685 if state.textentry = None
1686 then GlDraw.color (scalecolor 1.0)
1687 else GlDraw.color (scalecolor 0.4);
1688 let a = now () in
1689 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1690 opaque;
1691 let b = now () in
1692 let d = b-.a in
1693 vlog "draw %d %f sec" l.pageno d;
1695 | _ ->
1696 drawplaceholder l;
1697 end;
1698 l.pagedispy + l.pagevh;
1701 let scrollph y =
1702 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1703 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1704 let sh = float conf.winh /. sh in
1705 let sh = max sh (float conf.scrollh) in
1707 let percent =
1708 if state.y = state.maxy
1709 then 1.0
1710 else float y /. float maxy
1712 let position = (float conf.winh -. sh) *. percent in
1714 let position =
1715 if position +. sh > float conf.winh
1716 then float conf.winh -. sh
1717 else position
1719 position, sh;
1722 let scrollindicator () =
1723 GlDraw.color (0.64 , 0.64, 0.64);
1724 GlDraw.rect
1725 (float (conf.winw - conf.scrollw), 0.)
1726 (float conf.winw, float conf.winh)
1728 GlDraw.color (0.0, 0.0, 0.0);
1730 let position, sh = scrollph state.y in
1731 GlDraw.rect
1732 (float (conf.winw - conf.scrollw), position)
1733 (float conf.winw, position +. sh)
1737 let showsel margin =
1738 match state.mstate with
1739 | Mnone | Mscroll _ | Mpan _ ->
1742 | Msel ((x0, y0), (x1, y1)) ->
1743 let rec loop = function
1744 | l :: ls ->
1745 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1746 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1747 then
1748 match getopaque l.pageno with
1749 | Some opaque when validopaque opaque ->
1750 let oy = -l.pagey + l.pagedispy in
1751 seltext opaque
1752 (x0 - margin - state.x, y0,
1753 x1 - margin - state.x, y1) oy;
1755 | _ -> ()
1756 else loop ls
1757 | [] -> ()
1759 loop state.layout
1762 let showrects () =
1763 let panx = float state.x in
1764 Gl.enable `blend;
1765 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1766 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1767 List.iter
1768 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1769 List.iter (fun l ->
1770 if l.pageno = pageno
1771 then (
1772 let d = float (l.pagedispy - l.pagey) in
1773 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1774 GlDraw.begins `quads;
1776 GlDraw.vertex2 (x0+.panx, y0+.d);
1777 GlDraw.vertex2 (x1+.panx, y1+.d);
1778 GlDraw.vertex2 (x2+.panx, y2+.d);
1779 GlDraw.vertex2 (x3+.panx, y3+.d);
1781 GlDraw.ends ();
1783 ) state.layout
1784 ) state.rects
1786 Gl.disable `blend;
1789 let showoutline = function
1790 | None -> ()
1791 | Some (allowdel, active, first, outlines, qsearch) ->
1792 Gl.enable `blend;
1793 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1794 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1795 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
1796 Gl.disable `blend;
1798 GlDraw.color (1., 1., 1.);
1799 let font = Glut.BITMAP_9_BY_15 in
1800 let draw_string x y s =
1801 GlPix.raster_pos ~x ~y ();
1802 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1804 let rec loop row =
1805 if row = Array.length outlines || (row - first) * 16 > conf.winh
1806 then ()
1807 else (
1808 let (s, l, _, _) = outlines.(row) in
1809 let y = (row - first) * 16 in
1810 let x = 5 + 15*l in
1811 if row = active
1812 then (
1813 Gl.enable `blend;
1814 GlDraw.polygon_mode `both `line;
1815 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1816 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1817 GlDraw.rect (0., float (y + 1))
1818 (float (conf.winw - 1), float (y + 18));
1819 GlDraw.polygon_mode `both `fill;
1820 Gl.disable `blend;
1821 GlDraw.color (1., 1., 1.);
1823 draw_string (float x) (float (y + 16)) s;
1824 loop (row+1)
1827 loop first
1830 let display () =
1831 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1832 GlDraw.viewport margin 0 state.w conf.winh;
1833 pagematrix ();
1834 GlClear.color (scalecolor 0.5);
1835 GlClear.clear [`color];
1836 if state.x != 0
1837 then (
1838 let x = float state.x in
1839 GlMat.translate ~x ();
1841 if conf.zoom > 1.0
1842 then (
1843 Gl.enable `scissor_test;
1844 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
1846 let _lasty = List.fold_left drawpage 0 (state.layout) in
1847 if conf.zoom > 1.0
1848 then
1849 Gl.disable `scissor_test
1851 if state.x != 0
1852 then (
1853 let x = -.float state.x in
1854 GlMat.translate ~x ();
1856 showrects ();
1857 showsel margin;
1858 GlDraw.viewport 0 0 conf.winw conf.winh;
1859 winmatrix ();
1860 scrollindicator ();
1861 showoutline state.outline;
1862 enttext ();
1863 Glut.swapBuffers ();
1866 let getunder x y =
1867 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1868 let x = x - margin - state.x in
1869 let rec f = function
1870 | l :: rest ->
1871 begin match getopaque l.pageno with
1872 | Some opaque when validopaque opaque ->
1873 let y = y - l.pagedispy in
1874 if y > 0
1875 then
1876 let y = l.pagey + y in
1877 let x = x - l.pagex in
1878 match whatsunder opaque x y with
1879 | Unone -> f rest
1880 | under -> under
1881 else
1882 f rest
1883 | _ ->
1884 f rest
1886 | [] -> Unone
1888 f state.layout
1891 let mouse ~button ~bstate ~x ~y =
1892 match button with
1893 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1894 let incr =
1895 if n = 3
1896 then
1897 -conf.scrollincr
1898 else
1899 conf.scrollincr
1901 let incr = incr * 2 in
1902 let y = clamp incr in
1903 gotoy_and_clear_text y
1905 | Glut.LEFT_BUTTON when state.outline = None
1906 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
1907 if bstate = Glut.DOWN
1908 then (
1909 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1910 state.mstate <- Mpan (x, y)
1912 else
1913 state.mstate <- Mnone
1915 | Glut.LEFT_BUTTON
1916 when state.outline = None && x > conf.winw - conf.scrollw ->
1917 if bstate = Glut.DOWN
1918 then
1919 let position, sh = scrollph state.y in
1920 if y > truncate position && y < truncate (position +. sh)
1921 then
1922 state.mstate <- Mscroll
1923 else
1924 let percent = float y /. float conf.winh in
1925 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
1926 gotoy desty;
1927 state.mstate <- Mscroll
1928 else
1929 state.mstate <- Mnone
1931 | Glut.LEFT_BUTTON when state.outline = None ->
1932 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1933 begin match dest with
1934 | Ulinkgoto (pageno, top) ->
1935 if pageno >= 0
1936 then
1937 gotopage1 pageno top
1939 | Ulinkuri s ->
1940 print_endline s
1942 | Unone when bstate = Glut.DOWN ->
1943 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1944 state.mstate <- Mpan (x, y);
1946 | Unone | Utext _ ->
1947 if bstate = Glut.DOWN
1948 then (
1949 if conf.angle mod 360 = 0
1950 then (
1951 state.mstate <- Msel ((x, y), (x, y));
1952 Glut.postRedisplay ()
1955 else (
1956 match state.mstate with
1957 | Mnone -> ()
1959 | Mscroll ->
1960 state.mstate <- Mnone
1962 | Mpan _ ->
1963 Glut.setCursor Glut.CURSOR_INHERIT;
1964 state.mstate <- Mnone
1966 | Msel ((x0, y0), (x1, y1)) ->
1967 let f l =
1968 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1969 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1970 then
1971 match getopaque l.pageno with
1972 | Some opaque when validopaque opaque ->
1973 copysel opaque
1974 | _ -> ()
1976 List.iter f state.layout;
1977 copysel ""; (* ugly *)
1978 Glut.setCursor Glut.CURSOR_INHERIT;
1979 state.mstate <- Mnone;
1983 | _ ->
1986 let mouse ~button ~state ~x ~y = mouse button state x y;;
1988 let motion ~x ~y =
1989 if state.outline = None
1990 then
1991 match state.mstate with
1992 | Mnone -> ()
1994 | Mpan (x0, y0) ->
1995 let dx = x - x0
1996 and dy = y0 - y in
1997 state.mstate <- Mpan (x, y);
1998 if conf.zoom > 1.0 then state.x <- state.x + dx;
1999 let y = clamp dy in
2000 gotoy_and_clear_text y
2002 | Msel (a, _) ->
2003 state.mstate <- Msel (a, (x, y));
2004 Glut.postRedisplay ()
2006 | Mscroll ->
2007 let y = min conf.winh (max 0 y) in
2008 let percent = float y /. float conf.winh in
2009 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2010 gotoy_and_clear_text y
2013 let pmotion ~x ~y =
2014 if state.outline = None
2015 then
2016 match state.mstate with
2017 | Mnone ->
2018 begin match getunder x y with
2019 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2020 | Ulinkuri uri ->
2021 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2022 Glut.setCursor Glut.CURSOR_INFO
2023 | Ulinkgoto (page, y) ->
2024 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
2025 Glut.setCursor Glut.CURSOR_INFO
2026 | Utext s ->
2027 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2028 Glut.setCursor Glut.CURSOR_TEXT
2031 | Mpan _ | Msel _ | Mscroll ->
2035 module State =
2036 struct
2037 open Parser
2039 let home =
2041 match Sys.os_type with
2042 | "Win32" -> Sys.getenv "HOMEPATH"
2043 | _ -> Sys.getenv "HOME"
2044 with exn ->
2045 prerr_endline
2046 ("Can not determine home directory location: " ^
2047 Printexc.to_string exn);
2051 let config_of c attrs =
2052 let apply c k v =
2054 match k with
2055 | "scroll-bar-width" -> { c with scrollw = int_of_string v }
2056 | "scroll-handle-height" -> { c with scrollh = int_of_string v }
2057 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2058 | "preload" -> { c with preload = bool_of_string v }
2059 | "page-bias" -> { c with pagebias = int_of_string v }
2060 | "scroll-step" -> { c with scrollincr = int_of_string v }
2061 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2062 | "crop-hack" -> { c with crophack = bool_of_string v }
2063 | "throttle" -> { c with showall = bool_of_string v }
2064 | "highlight-links" -> { c with hlinks = bool_of_string v }
2065 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2066 | "vertical-margin" -> { c with interpagespace = int_of_string v }
2067 | "zoom" -> { c with zoom = float_of_string v /. 100. }
2068 | "presentation" -> { c with presentation = bool_of_string v }
2069 | "rotation-angle" -> { c with angle = int_of_string v }
2070 | "width" -> { c with winw = int_of_string v }
2071 | "height" -> { c with winh = int_of_string v }
2072 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2073 | "proportional-display" -> { c with proportional = bool_of_string v }
2074 | _ -> c
2075 with exn ->
2076 prerr_endline ("Error processing attribute (`" ^
2077 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2080 let rec fold c = function
2081 | [] -> c
2082 | (k, v) :: rest ->
2083 let c = apply c k v in
2084 fold c rest
2086 fold c attrs;
2089 let bookmark_of attrs =
2090 let rec fold title page rely = function
2091 | ("title", v) :: rest -> fold v page rely rest
2092 | ("page", v) :: rest -> fold title v rely rest
2093 | ("rely", v) :: rest -> fold title page v rest
2094 | _ :: rest -> fold title page rely rest
2095 | [] -> title, page, rely
2097 fold "invalid" "0" "0" attrs
2100 let setconf dst src =
2101 dst.scrollw <- src.scrollw;
2102 dst.scrollh <- src.scrollh;
2103 dst.icase <- src.icase;
2104 dst.preload <- src.preload;
2105 dst.pagebias <- src.pagebias;
2106 dst.verbose <- src.verbose;
2107 dst.scrollincr <- src.scrollincr;
2108 dst.maxhfit <- src.maxhfit;
2109 dst.crophack <- src.crophack;
2110 dst.autoscroll <- src.autoscroll;
2111 dst.showall <- src.showall;
2112 dst.hlinks <- src.hlinks;
2113 dst.underinfo <- src.underinfo;
2114 dst.interpagespace <- src.interpagespace;
2115 dst.zoom <- src.zoom;
2116 dst.presentation <- src.presentation;
2117 dst.angle <- src.angle;
2118 dst.winw <- src.winw;
2119 dst.winh <- src.winh;
2120 dst.savebmarks <- src.savebmarks;
2121 dst.proportional <- src.proportional;
2124 let unent s =
2125 let l = String.length s in
2126 let b = Buffer.create l in
2127 unent b s 0 l;
2128 Buffer.contents b;
2131 let get s =
2132 let h = Hashtbl.create 10 in
2133 let dc = { defconf with angle = defconf.angle } in
2134 let rec toplevel v t spos epos =
2135 match t with
2136 | Vdata | Vcdata | Vend -> v
2137 | Vopen ("llppconfig", attrs, closed) ->
2138 if closed
2139 then v
2140 else { v with f = llppconfig }
2141 | Vopen _ ->
2142 error "unexpected subelement at top level" s spos
2143 | Vclose tag -> error "unexpected close at toplevel" s spos
2145 and llppconfig v t spos epos =
2146 match t with
2147 | Vdata | Vcdata | Vend -> v
2148 | Vopen ("defaults", attrs, closed) ->
2149 let c = config_of dc attrs in
2150 setconf dc c;
2151 if closed
2152 then v
2153 else { v with f = skip "defaults" (fun () -> v) }
2155 | Vopen ("doc", attrs, closed) ->
2156 let pathent =
2158 List.assoc "path" attrs
2159 with Not_found -> error "doc is missing path attribute" s spos
2161 let path = unent pathent in
2162 let c = config_of dc attrs in
2163 let y =
2165 float_of_string (List.assoc "rely" attrs)
2166 with
2167 | Not_found -> 0.0
2168 | exn ->
2169 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2172 let x =
2174 int_of_string (List.assoc "pan" attrs)
2175 with
2176 | Not_found -> 0
2177 | exn ->
2178 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2181 if closed
2182 then (Hashtbl.add h path (c, [], x, y); v)
2183 else { v with f = doc path x y c [] }
2185 | Vopen (tag, _, closed) ->
2186 error "unexpected subelement in llppconfig" s spos
2188 | Vclose "llppconfig" -> { v with f = toplevel }
2189 | Vclose tag -> error "unexpected close in llppconfig" s spos
2191 and doc path x y c bookmarks v t spos epos =
2192 match t with
2193 | Vdata | Vcdata -> v
2194 | Vend -> error "unexpected end of input in doc" s spos
2195 | Vopen ("bookmarks", attrs, closed) ->
2196 { v with f = pbookmarks path x y c bookmarks }
2198 | Vopen (tag, _, _) ->
2199 error "unexpected subelement in doc" s spos
2201 | Vclose "doc" ->
2202 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2203 { v with f = llppconfig }
2205 | Vclose tag -> error "unexpected close in doc" s spos
2207 and pbookmarks path x y c bookmarks v t spos epos =
2208 match t with
2209 | Vdata | Vcdata -> v
2210 | Vend -> error "unexpected end of input in bookmarks" s spos
2211 | Vopen ("item", attrs, closed) ->
2212 let titleent, spage, srely = bookmark_of attrs in
2213 let page =
2215 int_of_string spage
2216 with exn ->
2217 dolog "Failed to convert page %S to integer: %s"
2218 spage (Printexc.to_string exn);
2221 let rely =
2223 float_of_string srely
2224 with exn ->
2225 dolog "Failed to convert rely %S to real: %s"
2226 srely (Printexc.to_string exn);
2229 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2230 if closed
2231 then { v with f = pbookmarks path x y c bookmarks }
2232 else
2233 let f () = v in
2234 { v with f = skip "item" f }
2236 | Vopen _ ->
2237 error "unexpected subelement in bookmarks" s spos
2239 | Vclose "bookmarks" ->
2240 { v with f = doc path x y c bookmarks }
2242 | Vclose tag -> error "unexpected close in bookmarks" s spos
2244 and skip tag f v t spos epos =
2245 match t with
2246 | Vdata | Vcdata -> v
2247 | Vend ->
2248 error ("unexpected end of input in skipped " ^ tag) s spos
2249 | Vopen (tag', _, closed) ->
2250 if closed
2251 then v
2252 else
2253 let f' () = { v with f = skip tag f } in
2254 { v with f = skip tag' f' }
2255 | Vclose ctag ->
2256 if tag = ctag
2257 then f ()
2258 else error ("unexpected close in skipped " ^ tag) s spos
2261 parse { f = toplevel; accu = () } s;
2262 h, dc;
2265 let do_load f ic =
2267 let len = in_channel_length ic in
2268 let s = String.create len in
2269 really_input ic s 0 len;
2270 f s;
2271 with
2272 | Parse_error (msg, s, pos) ->
2273 let subs = subs s pos in
2274 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2275 failwith ("parse error: " ^ s)
2277 | exn ->
2278 failwith ("config load error: " ^ Printexc.to_string exn)
2281 let path =
2282 let dir =
2284 let dir = Filename.concat home ".config" in
2285 if Sys.is_directory dir then dir else home
2286 with _ -> home
2288 Filename.concat dir "llpp.conf"
2291 let load1 f =
2292 if Sys.file_exists path
2293 then
2294 match
2295 (try Some (open_in_bin path)
2296 with exn ->
2297 prerr_endline
2298 ("Error opening configuation file `" ^ path ^ "': " ^
2299 Printexc.to_string exn);
2300 None
2302 with
2303 | Some ic ->
2304 begin try
2305 f (do_load get ic)
2306 with exn ->
2307 prerr_endline
2308 ("Error loading configuation from `" ^ path ^ "': " ^
2309 Printexc.to_string exn);
2310 end;
2311 close_in ic;
2313 | None -> ()
2314 else
2315 f (Hashtbl.create 0, defconf)
2318 let load () =
2319 let f (h, dc) =
2320 let pc, pb, px, py =
2322 Hashtbl.find h state.path
2323 with Not_found -> dc, [], 0, 0.0
2325 setconf defconf dc;
2326 setconf conf pc;
2327 state.bookmarks <- pb;
2328 state.x <- px;
2329 cbput state.hists.nav py;
2330 cbrfollowlen state.hists.nav;
2332 load1 f
2335 let add_attrs bb always dc c =
2336 let ob s a b =
2337 if always || a != b
2338 then Printf.bprintf bb "\n %s='%b'" s a
2339 and oi s a b =
2340 if always || a != b
2341 then Printf.bprintf bb "\n %s='%d'" s a
2342 and oz s a b =
2343 if always || a <> b
2344 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2346 let w, h =
2347 if always
2348 then dc.winw, dc.winh
2349 else
2350 match state.fullscreen with
2351 | Some wh -> wh
2352 | None -> c.winw, c.winh
2354 oi "width" w dc.winw;
2355 oi "height" h dc.winh;
2356 oi "scroll-bar-width" c.scrollw dc.scrollw;
2357 oi "scroll-handle-height" c.scrollh dc.scrollh;
2358 ob "case-insensitive-search" c.icase dc.icase;
2359 ob "preload" c.preload dc.preload;
2360 oi "page-bias" c.pagebias dc.pagebias;
2361 oi "scroll-step" c.scrollincr dc.scrollincr;
2362 ob "max-height-fit" c.maxhfit dc.maxhfit;
2363 ob "crop-hack" c.crophack dc.crophack;
2364 ob "throttle" c.showall dc.showall;
2365 ob "highlight-links" c.hlinks dc.hlinks;
2366 ob "under-cursor-info" c.underinfo dc.underinfo;
2367 oi "vertical-margin" c.interpagespace dc.interpagespace;
2368 oz "zoom" c.zoom dc.zoom;
2369 ob "presentation" c.presentation dc.presentation;
2370 oi "rotation-angle" c.angle dc.angle;
2371 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2372 ob "proportional-display" c.proportional dc.proportional;
2375 let save () =
2376 let bb = Buffer.create 32768 in
2377 let f (h, dc) =
2378 Buffer.add_string bb "<llppconfig>\n<defaults ";
2379 add_attrs bb true dc dc;
2380 Buffer.add_string bb "/>\n";
2382 let adddoc path x y c bookmarks =
2383 if bookmarks == [] && c = dc && y = 0.0
2384 then ()
2385 else (
2386 Printf.bprintf bb "<doc path='%s'"
2387 (enent path 0 (String.length path));
2389 if y <> 0.0
2390 then Printf.bprintf bb " rely='%f'" y;
2392 if x != 0
2393 then Printf.bprintf bb " pan='%d'" x;
2395 add_attrs bb false dc c;
2397 begin match bookmarks with
2398 | [] -> Buffer.add_string bb "/>\n"
2399 | _ ->
2400 Buffer.add_string bb ">\n<bookmarks>\n";
2401 List.iter (fun (title, _level, page, rely) ->
2402 Printf.bprintf bb
2403 "<item title='%s' page='%d' rely='%f'/>\n"
2404 (enent title 0 (String.length title))
2405 page
2406 rely
2407 ) bookmarks;
2408 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2409 end;
2413 adddoc state.path state.x (yratio state.y) conf
2414 (if conf.savebmarks then state.bookmarks else []);
2416 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2417 if path <> state.path
2418 then
2419 adddoc path x y c bookmarks
2420 ) h;
2421 Buffer.add_string bb "</llppconfig>";
2423 load1 f;
2424 if Buffer.length bb > 0
2425 then
2427 let tmp = path ^ ".tmp" in
2428 let oc = open_out_bin tmp in
2429 Buffer.output_buffer oc bb;
2430 close_out oc;
2431 Sys.rename tmp path;
2432 with exn ->
2433 prerr_endline
2434 ("error while saving configuration: " ^ Printexc.to_string exn)
2436 end;;
2438 let () =
2439 Arg.parse
2440 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2441 (fun s -> state.path <- s)
2442 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2444 let path =
2445 if String.length state.path = 0
2446 then (prerr_endline "filename missing"; exit 1)
2447 else (
2448 if Filename.is_relative state.path
2449 then Filename.concat (Sys.getcwd ()) state.path
2450 else state.path
2453 state.path <- path;
2455 State.load ();
2457 let _ = Glut.init Sys.argv in
2458 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2459 let () = Glut.initWindowSize conf.winw conf.winh in
2460 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2462 let csock, ssock =
2463 if Sys.os_type = "Unix"
2464 then
2465 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2466 else
2467 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2468 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2469 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2470 Unix.bind sock addr;
2471 Unix.listen sock 1;
2472 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2473 Unix.connect csock addr;
2474 let ssock, _ = Unix.accept sock in
2475 Unix.close sock;
2476 let opts sock =
2477 Unix.setsockopt sock Unix.TCP_NODELAY true;
2478 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2480 opts ssock;
2481 opts csock;
2482 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2483 ssock, csock
2486 let () = Glut.displayFunc display in
2487 let () = Glut.reshapeFunc reshape in
2488 let () = Glut.keyboardFunc keyboard in
2489 let () = Glut.specialFunc special in
2490 let () = Glut.idleFunc (Some idle) in
2491 let () = Glut.mouseFunc mouse in
2492 let () = Glut.motionFunc motion in
2493 let () = Glut.passiveMotionFunc pmotion in
2495 init ssock;
2496 state.csock <- csock;
2497 state.ssock <- ssock;
2498 state.text <- "Opening " ^ path;
2499 writeopen state.path state.password;
2501 at_exit State.save;
2503 let rec handlelablglutbug () =
2505 Glut.mainLoop ();
2506 with Glut.BadEnum "key in special_of_int" ->
2507 showtext '!' " LablGlut bug: special key not recognized";
2508 handlelablglutbug ()
2510 handlelablglutbug ();