Sanitize zoom that is comming from config
[llpp.git] / main.ml
blobd0e58673a54780c6ba93d02cbc1e4c94be2b9850
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 pageno = int
125 and width = int
126 and height = int
127 and leftx = int
128 and angle = int
129 and proportional = bool
130 and opaque = string
131 and recttype = int
134 type outline = string * int * int * float;;
135 type outlines =
136 | Oarray of outline array
137 | Olist of outline list
138 | Onarrow of string * outline array * outline array
141 type rect = (float * float * float * float * float * float * float * float);;
143 type state =
144 { mutable csock : Unix.file_descr
145 ; mutable ssock : Unix.file_descr
146 ; mutable w : int
147 ; mutable x : int
148 ; mutable y : int
149 ; mutable ty : float
150 ; mutable maxy : int
151 ; mutable layout : layout list
152 ; pagemap : ((pageno * width * angle * proportional), opaque) Hashtbl.t
153 ; mutable pdims : (pageno * width * height * leftx) list
154 ; mutable pagecount : int
155 ; pagecache : string circbuf
156 ; mutable rendering : bool
157 ; mutable mstate : mstate
158 ; mutable searchpattern : string
159 ; mutable rects : (pageno * recttype * rect) list
160 ; mutable rects1 : (pageno * recttype * rect) list
161 ; mutable text : string
162 ; mutable fullscreen : (width * height) option
163 ; mutable textentry : textentry option
164 ; mutable outlines : outlines
165 ; mutable outline : (bool * int * int * outline array * string) option
166 ; mutable bookmarks : outline list
167 ; mutable path : string
168 ; mutable password : string
169 ; mutable invalidated : int
170 ; mutable colorscale : float
171 ; hists : hists
173 and hists =
174 { pat : string circbuf
175 ; pag : string circbuf
176 ; nav : float circbuf
180 let defconf =
181 { scrollw = 7
182 ; scrollh = 12
183 ; icase = true
184 ; preload = true
185 ; pagebias = 0
186 ; verbose = false
187 ; scrollincr = 24
188 ; maxhfit = true
189 ; crophack = false
190 ; autoscroll = false
191 ; showall = false
192 ; hlinks = false
193 ; underinfo = false
194 ; interpagespace = 2
195 ; zoom = 1.0
196 ; presentation = false
197 ; angle = 0
198 ; winw = 900
199 ; winh = 900
200 ; savebmarks = true
201 ; proportional = true
205 let conf = { defconf with angle = defconf.angle };;
207 let state =
208 { csock = Unix.stdin
209 ; ssock = Unix.stdin
210 ; w = 0
211 ; y = 0
212 ; x = 0
213 ; ty = 0.0
214 ; layout = []
215 ; maxy = max_int
216 ; pagemap = Hashtbl.create 10
217 ; pagecache = cbnew 10 ""
218 ; pdims = []
219 ; pagecount = 0
220 ; rendering = false
221 ; mstate = Mnone
222 ; rects = []
223 ; rects1 = []
224 ; text = ""
225 ; fullscreen = None
226 ; textentry = None
227 ; searchpattern = ""
228 ; outlines = Olist []
229 ; outline = None
230 ; bookmarks = []
231 ; path = ""
232 ; password = ""
233 ; invalidated = 0
234 ; hists =
235 { nav = cbnew 100 0.0
236 ; pat = cbnew 20 ""
237 ; pag = cbnew 10 ""
239 ; colorscale = 1.0
243 let vlog fmt =
244 if conf.verbose
245 then
246 Printf.kprintf prerr_endline fmt
247 else
248 Printf.kprintf ignore fmt
251 let writecmd fd s =
252 let len = String.length s in
253 let n = 4 + len in
254 let b = Buffer.create n in
255 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
256 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
257 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
258 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
259 Buffer.add_string b s;
260 let s' = Buffer.contents b in
261 let n' = Unix.write fd s' 0 n in
262 if n' != n then failwith "write failed";
265 let readcmd fd =
266 let s = "xxxx" in
267 let n = Unix.read fd s 0 4 in
268 if n != 4 then failwith "incomplete read(len)";
269 let len = 0
270 lor (Char.code s.[0] lsl 24)
271 lor (Char.code s.[1] lsl 16)
272 lor (Char.code s.[2] lsl 8)
273 lor (Char.code s.[3] lsl 0)
275 let s = String.create len in
276 let n = Unix.read fd s 0 len in
277 if n != len then failwith "incomplete read(data)";
281 let yratio y =
282 if y = state.maxy
283 then 1.0
284 else float y /. float state.maxy
287 let makecmd s l =
288 let b = Buffer.create 10 in
289 Buffer.add_string b s;
290 let rec combine = function
291 | [] -> b
292 | x :: xs ->
293 Buffer.add_char b ' ';
294 let s =
295 match x with
296 | `b b -> if b then "1" else "0"
297 | `s s -> s
298 | `i i -> string_of_int i
299 | `f f -> string_of_float f
300 | `I f -> string_of_int (truncate f)
302 Buffer.add_string b s;
303 combine xs;
305 combine l;
308 let wcmd s l =
309 let cmd = Buffer.contents (makecmd s l) in
310 writecmd state.csock cmd;
313 let calcips h =
314 if conf.presentation
315 then
316 let d = conf.winh - h in
317 max 0 ((d + 1) / 2)
318 else
319 conf.interpagespace
322 let calcheight () =
323 let rec f pn ph pi fh l =
324 match l with
325 | (n, _, h, _) :: rest ->
326 let ips = calcips h in
327 let fh =
328 if conf.presentation
329 then fh+ips
330 else fh
332 let fh = fh + ((n - pn) * (ph + pi)) in
333 f n h ips fh rest
335 | [] ->
336 let inc =
337 if conf.presentation
338 then 0
339 else -pi
341 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
342 max 0 fh
344 let fh = f 0 0 0 0 state.pdims in
348 let getpageyh pageno =
349 let rec f pn ph pi y l =
350 match l with
351 | (n, _, h, _) :: rest ->
352 let ips = calcips h in
353 if n >= pageno
354 then
355 if conf.presentation && n = pageno
356 then
357 y + (pageno - pn) * (ph + pi) + pi, h
358 else
359 y + (pageno - pn) * (ph + pi), h
360 else
361 let y = y + (if conf.presentation then pi else 0) in
362 let y = y + (n - pn) * (ph + pi) in
363 f n h ips y rest
365 | [] ->
366 y + (pageno - pn) * (ph + pi), ph
368 f 0 0 0 0 state.pdims
371 let getpagey pageno = fst (getpageyh pageno);;
373 let layout y sh =
374 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
375 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
376 match pdims with
377 | (pageno', w, h, x) :: rest when pageno' = pageno ->
378 let ips = calcips h in
379 let yinc = if conf.presentation then ips else 0 in
380 (w, h, ips, x), rest, pdimno + 1, yinc
381 | _ ->
382 prev, pdims, pdimno, 0
384 let dy = dy + yinc in
385 let py = py + yinc in
386 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
387 then
388 accu
389 else
390 let vy = y + dy in
391 if py + h <= vy - yinc
392 then
393 let py = py + h + ips in
394 let dy = max 0 (py - y) in
395 f ~pageno:(pageno+1)
396 ~pdimno
397 ~prev:curr
400 ~pdims:rest
401 ~cacheleft
402 ~accu
403 else
404 let pagey = vy - py in
405 let pagevh = h - pagey in
406 let pagevh = min (sh - dy) pagevh in
407 let off = if yinc > 0 then py - vy else 0
409 let py = py + h + ips in
410 let e =
411 { pageno = pageno
412 ; pagedimno = pdimno
413 ; pagew = w
414 ; pageh = h
415 ; pagedispy = dy + off
416 ; pagey = pagey + off
417 ; pagevh = pagevh - off
418 ; pagex = x
421 let accu = e :: accu in
422 f ~pageno:(pageno+1)
423 ~pdimno
424 ~prev:curr
426 ~dy:(dy+pagevh+ips)
427 ~pdims:rest
428 ~cacheleft:(cacheleft-1)
429 ~accu
431 if state.invalidated = 0
432 then (
433 let accu =
435 ~pageno:0
436 ~pdimno:~-1
437 ~prev:(0,0,0,0)
438 ~py:0
439 ~dy:0
440 ~pdims:state.pdims
441 ~cacheleft:(cblen state.pagecache)
442 ~accu:[]
444 List.rev accu
446 else
450 let clamp incr =
451 let y = state.y + incr in
452 let y = max 0 y in
453 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
457 let getopaque pageno =
458 try Some (Hashtbl.find state.pagemap
459 (pageno + 1, state.w, conf.angle, conf.proportional))
460 with Not_found -> None
463 let cache pageno opaque =
464 Hashtbl.replace state.pagemap
465 (pageno + 1, state.w, conf.angle, conf.proportional) opaque
468 let validopaque opaque = String.length opaque > 0;;
470 let render l =
471 match getopaque l.pageno with
472 | None when not state.rendering ->
473 state.rendering <- true;
474 cache l.pageno "";
475 wcmd "render" [`i (l.pageno + 1)
476 ;`i l.pagedimno
477 ;`i l.pagew
478 ;`i l.pageh];
480 | _ -> ()
483 let loadlayout layout =
484 let rec f all = function
485 | l :: ls ->
486 begin match getopaque l.pageno with
487 | None -> render l; f false ls
488 | Some opaque -> f (all && validopaque opaque) ls
490 | [] -> all
492 f (layout <> []) layout;
495 let preload () =
496 if conf.preload
497 then
498 let evictedvisible =
499 let evictedopaque = cbpeekw state.pagecache in
500 List.exists (fun l ->
501 match getopaque l.pageno with
502 | Some opaque when validopaque opaque ->
503 evictedopaque = opaque
504 | otherwise -> false
505 ) state.layout
507 if not evictedvisible
508 then
509 let rely = yratio state.y in
510 let presentation = conf.presentation in
511 let interpagespace = conf.interpagespace in
512 let maxy = state.maxy in
513 conf.presentation <- false;
514 conf.interpagespace <- 0;
515 state.maxy <- calcheight ();
516 let y = truncate (float state.maxy *. rely) in
517 let y = if y < conf.winh then 0 else y - conf.winh in
518 let pages = layout y (conf.winh*3) in
519 List.iter render pages;
520 conf.presentation <- presentation;
521 conf.interpagespace <- interpagespace;
522 state.maxy <- maxy;
525 let gotoy y =
526 let y = max 0 y in
527 let y = min state.maxy y in
528 let pages = layout y conf.winh in
529 let ready = loadlayout pages in
530 state.ty <- yratio y;
531 if conf.showall
532 then (
533 if ready
534 then (
535 state.layout <- pages;
536 state.y <- y;
537 Glut.postRedisplay ();
540 else (
541 state.layout <- pages;
542 state.y <- y;
543 Glut.postRedisplay ();
545 preload ();
548 let gotoy_and_clear_text y =
549 gotoy y;
550 if not conf.verbose then state.text <- "";
553 let addnav () =
554 cbput state.hists.nav (yratio state.y);
555 cbrfollowlen state.hists.nav;
558 let getnav () =
559 let y = cbget state.hists.nav ~-1 in
560 truncate (y *. float state.maxy)
563 let gotopage n top =
564 let y, h = getpageyh n in
565 addnav ();
566 gotoy_and_clear_text (y + (truncate (top *. float h)));
569 let gotopage1 n top =
570 let y = getpagey n in
571 addnav ();
572 gotoy_and_clear_text (y + top);
575 let invalidate () =
576 state.layout <- [];
577 state.pdims <- [];
578 state.rects <- [];
579 state.rects1 <- [];
580 state.invalidated <- state.invalidated + 1;
583 let scalecolor c =
584 let c = c *. state.colorscale in
585 (c, c, c);
588 let represent () =
589 let y =
590 match state.layout with
591 | [] ->
592 let rely = yratio state.y in
593 state.maxy <- calcheight ();
594 truncate (float state.maxy *. rely)
596 | l :: _ ->
597 state.maxy <- calcheight ();
598 getpagey l.pageno
600 gotoy y
603 let pagematrix () =
604 GlMat.mode `projection;
605 GlMat.load_identity ();
606 GlMat.rotate ~x:1.0 ~angle:180.0 ();
607 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
608 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
611 let winmatrix () =
612 GlMat.mode `projection;
613 GlMat.load_identity ();
614 GlMat.rotate ~x:1.0 ~angle:180.0 ();
615 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
616 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
619 let reshape ~w ~h =
620 conf.winw <- w;
621 let w = truncate (float w *. conf.zoom) - conf.scrollw in
622 state.w <- w;
623 conf.winh <- h;
624 GlMat.mode `modelview;
625 GlMat.load_identity ();
626 GlClear.color (scalecolor 1.0);
627 GlClear.clear [`color];
629 invalidate ();
630 wcmd "geometry" [`i w; `i h];
633 let showtext c s =
634 GlDraw.color (0.0, 0.0, 0.0);
635 GlDraw.rect
636 (0.0, float (conf.winh - 18))
637 (float (conf.winw - conf.scrollw - 1), float conf.winh)
639 let font = Glut.BITMAP_8_BY_13 in
640 GlDraw.color (1.0, 1.0, 1.0);
641 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
642 Glut.bitmapCharacter ~font ~c:(Char.code c);
643 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
646 let enttext () =
647 let len = String.length state.text in
648 match state.textentry with
649 | None ->
650 if len > 0 then showtext ' ' state.text
652 | Some (c, text, _, _, _) ->
653 let s =
654 if len > 0
655 then
656 text ^ " [" ^ state.text ^ "]"
657 else
658 text
660 showtext c s;
663 let showtext c s =
664 if true
665 then (
666 state.text <- Printf.sprintf "%c%s" c s;
667 Glut.postRedisplay ();
669 else (
670 showtext c s;
671 Glut.swapBuffers ();
675 let act cmd =
676 match cmd.[0] with
677 | 'c' ->
678 state.pdims <- [];
680 | 'D' ->
681 state.rects <- state.rects1;
682 Glut.postRedisplay ()
684 | 'C' ->
685 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
686 state.pagecount <- n;
687 state.invalidated <- state.invalidated - 1;
688 if state.invalidated = 0
689 then represent ()
691 | 't' ->
692 let s = Scanf.sscanf cmd "t %n"
693 (fun n -> String.sub cmd n (String.length cmd - n))
695 Glut.setWindowTitle s
697 | 'T' ->
698 let s = Scanf.sscanf cmd "T %n"
699 (fun n -> String.sub cmd n (String.length cmd - n))
701 if state.textentry = None
702 then (
703 state.text <- s;
704 showtext ' ' s;
706 else (
707 state.text <- s;
708 Glut.postRedisplay ();
711 | 'V' ->
712 if conf.verbose
713 then
714 let s = Scanf.sscanf cmd "V %n"
715 (fun n -> String.sub cmd n (String.length cmd - n))
717 state.text <- s;
718 showtext ' ' s;
720 | 'F' ->
721 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
722 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
723 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
724 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
726 let y = (getpagey pageno) + truncate y0 in
727 addnav ();
728 gotoy y;
729 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
731 | 'R' ->
732 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
733 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
734 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
735 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
737 state.rects1 <-
738 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
740 | 'r' ->
741 let n, w, h, r, l, p =
742 Scanf.sscanf cmd "r %d %d %d %d %d %s"
743 (fun n w h r l p -> (n, w, h, r, l != 0, p))
745 Hashtbl.replace state.pagemap (n, w, r, l) p;
746 let opaque = cbpeekw state.pagecache in
747 if validopaque opaque
748 then (
749 let k =
750 Hashtbl.fold
751 (fun k v a -> if v = opaque then k else a)
752 state.pagemap (-1, -1, -1, false)
754 wcmd "free" [`s opaque];
755 Hashtbl.remove state.pagemap k
757 cbput state.pagecache p;
758 state.rendering <- false;
759 if conf.showall
760 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
761 else (
762 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
763 if visible
764 then gotoy state.y
765 else (ignore (loadlayout state.layout); preload ())
768 | 'l' ->
769 let (n, w, h, x) as pdim =
770 Scanf.sscanf cmd "l %d %d %d %d" (fun n w h x -> n, w, h, x)
772 state.pdims <- pdim :: state.pdims
774 | 'o' ->
775 let (l, n, t, h, pos) =
776 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
778 let s = String.sub cmd pos (String.length cmd - pos) in
779 let s =
780 let l = String.length s in
781 let b = Buffer.create (String.length s) in
782 let rec loop pc2 i =
783 if i = l
784 then ()
785 else
786 let pc2 =
787 match s.[i] with
788 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
789 | '\xc2' -> true
790 | c ->
791 let c = if Char.code c land 0x80 = 0 then c else '?' in
792 Buffer.add_char b c;
793 false
795 loop pc2 (i+1)
797 loop false 0;
798 Buffer.contents b
800 let outline = (s, l, n, float t /. float h) in
801 let outlines =
802 match state.outlines with
803 | Olist outlines -> Olist (outline :: outlines)
804 | Oarray _ -> Olist [outline]
805 | Onarrow _ -> Olist [outline]
807 state.outlines <- outlines
809 | _ ->
810 log "unknown cmd `%S'" cmd
813 let now = Unix.gettimeofday;;
815 let idle () =
816 let rec loop delay =
817 let r, _, _ = Unix.select [state.csock] [] [] delay in
818 begin match r with
819 | [] ->
820 if conf.autoscroll
821 then begin
822 let y = state.y + conf.scrollincr in
823 let y = if y >= state.maxy then 0 else y in
824 gotoy y;
825 state.text <- "";
826 end;
828 | _ ->
829 let cmd = readcmd state.csock in
830 act cmd;
831 loop 0.0
832 end;
833 in loop 0.001
836 let onhist cb = function
837 | HCprev -> cbget cb ~-1
838 | HCnext -> cbget cb 1
839 | HCfirst -> cbget cb ~-(cb.rc)
840 | HClast -> cbget cb (cb.len - 1 - cb.rc)
843 let search pattern forward =
844 if String.length pattern > 0
845 then
846 let pn, py =
847 match state.layout with
848 | [] -> 0, 0
849 | l :: _ ->
850 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
852 let cmd =
853 let b = makecmd "search"
854 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
856 Buffer.add_char b ',';
857 Buffer.add_string b pattern;
858 Buffer.add_char b '\000';
859 Buffer.contents b;
861 writecmd state.csock cmd;
864 let intentry text key =
865 let c = Char.unsafe_chr key in
866 match c with
867 | '0' .. '9' ->
868 let s = "x" in s.[0] <- c;
869 let text = text ^ s in
870 TEcont text
872 | _ ->
873 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
874 TEcont text
877 let addchar s c =
878 let b = Buffer.create (String.length s + 1) in
879 Buffer.add_string b s;
880 Buffer.add_char b c;
881 Buffer.contents b;
884 let textentry text key =
885 let c = Char.unsafe_chr key in
886 match c with
887 | _ when key >= 32 && key < 127 ->
888 let text = addchar text c in
889 TEcont text
891 | _ ->
892 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
893 TEcont text
896 let reinit angle proportional =
897 conf.angle <- angle;
898 conf.proportional <- proportional;
899 invalidate ();
900 wcmd "reinit" [`i angle; `b proportional];
903 let optentry text key =
904 let btos b = if b then "on" else "off" in
905 let c = Char.unsafe_chr key in
906 match c with
907 | 's' ->
908 let ondone s =
909 try conf.scrollincr <- int_of_string s with exc ->
910 state.text <- Printf.sprintf "bad integer `%s': %s"
911 s (Printexc.to_string exc)
913 TEswitch ('#', "", None, intentry, ondone)
915 | 'R' ->
916 let ondone s =
917 match try
918 Some (int_of_string s)
919 with exc ->
920 state.text <- Printf.sprintf "bad integer `%s': %s"
921 s (Printexc.to_string exc);
922 None
923 with
924 | Some angle -> reinit angle conf.proportional
925 | None -> ()
927 TEswitch ('^', "", None, intentry, ondone)
929 | 'i' ->
930 conf.icase <- not conf.icase;
931 TEdone ("case insensitive search " ^ (btos conf.icase))
933 | 'p' ->
934 conf.preload <- not conf.preload;
935 gotoy state.y;
936 TEdone ("preload " ^ (btos conf.preload))
938 | 'v' ->
939 conf.verbose <- not conf.verbose;
940 TEdone ("verbose " ^ (btos conf.verbose))
942 | 'h' ->
943 conf.maxhfit <- not conf.maxhfit;
944 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
945 TEdone ("maxhfit " ^ (btos conf.maxhfit))
947 | 'c' ->
948 conf.crophack <- not conf.crophack;
949 TEdone ("crophack " ^ btos conf.crophack)
951 | 'a' ->
952 conf.showall <- not conf.showall;
953 TEdone ("showall " ^ btos conf.showall)
955 | 'f' ->
956 conf.underinfo <- not conf.underinfo;
957 TEdone ("underinfo " ^ btos conf.underinfo)
959 | 'P' ->
960 conf.savebmarks <- not conf.savebmarks;
961 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
963 | 'S' ->
964 let ondone s =
966 conf.interpagespace <- int_of_string s;
967 let rely = yratio state.y in
968 state.maxy <- calcheight ();
969 gotoy (truncate (float state.maxy *. rely));
970 with exc ->
971 state.text <- Printf.sprintf "bad integer `%s': %s"
972 s (Printexc.to_string exc)
974 TEswitch ('%', "", None, intentry, ondone)
976 | 'l' ->
977 reinit conf.angle (not conf.proportional);
978 TEdone ("proprortional display " ^ btos conf.proportional)
980 | _ ->
981 state.text <- Printf.sprintf "bad option %d `%c'" key c;
982 TEstop
985 let maxoutlinerows () = (conf.winh - 31) / 16;;
987 let enterselector allowdel outlines errmsg msg =
988 if Array.length outlines = 0
989 then (
990 showtext ' ' errmsg;
992 else (
993 state.text <- msg;
994 Glut.setCursor Glut.CURSOR_INHERIT;
995 let pageno =
996 match state.layout with
997 | [] -> -1
998 | {pageno=pageno} :: rest -> pageno
1000 let active =
1001 let rec loop n =
1002 if n = Array.length outlines
1003 then 0
1004 else
1005 let (_, _, outlinepageno, _) = outlines.(n) in
1006 if outlinepageno >= pageno then n else loop (n+1)
1008 loop 0
1010 state.outline <-
1011 Some (allowdel, active,
1012 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1013 Glut.postRedisplay ();
1017 let enteroutlinemode () =
1018 let outlines, msg =
1019 match state.outlines with
1020 | Oarray a -> a, ""
1021 | Olist l ->
1022 let a = Array.of_list (List.rev l) in
1023 state.outlines <- Oarray a;
1024 a, ""
1025 | Onarrow (pat, a, b) ->
1026 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1028 enterselector false outlines "Document has no outline" msg;
1031 let enterbookmarkmode () =
1032 let bookmarks = Array.of_list state.bookmarks in
1033 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1036 let quickbookmark ?title () =
1037 match state.layout with
1038 | [] -> ()
1039 | l :: _ ->
1040 let title =
1041 match title with
1042 | None ->
1043 let sec = Unix.gettimeofday () in
1044 let tm = Unix.localtime sec in
1045 Printf.sprintf "Quick (page %d) visited (%d/%d/%d %d:%d)"
1046 (l.pageno+1)
1047 tm.Unix.tm_mday
1048 tm.Unix.tm_mon
1049 (tm.Unix.tm_year + 1900)
1050 tm.Unix.tm_hour
1051 tm.Unix.tm_min
1052 | Some title -> title
1054 state.bookmarks <-
1055 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1058 let doreshape w h =
1059 state.fullscreen <- None;
1060 Glut.reshapeWindow w h;
1063 let writeopen path password =
1064 writecmd state.csock
1065 ("open " ^ path ^ "\000"
1066 ^ state.password ^ "\000"
1067 ^ string_of_int conf.angle ^ " "
1068 ^ (if conf.proportional then "1" else "0"))
1072 let opendoc path password =
1073 invalidate ();
1074 state.path <- path;
1075 state.password <- password;
1076 Hashtbl.clear state.pagemap;
1078 writeopen path password;
1079 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1080 wcmd "geometry" [`i state.w; `i conf.winh];
1083 let viewkeyboard ~key ~x ~y =
1084 let enttext te =
1085 state.textentry <- te;
1086 state.text <- "";
1087 enttext ();
1088 Glut.postRedisplay ()
1090 match state.textentry with
1091 | None ->
1092 let c = Char.chr key in
1093 begin match c with
1094 | '\027' | 'q' ->
1095 exit 0
1097 | '\008' ->
1098 let y = getnav () in
1099 gotoy_and_clear_text y
1101 | 'o' ->
1102 enteroutlinemode ()
1104 | 'u' ->
1105 state.rects <- [];
1106 state.text <- "";
1107 Glut.postRedisplay ()
1109 | '/' | '?' ->
1110 let ondone isforw s =
1111 cbput state.hists.pat s;
1112 cbrfollowlen state.hists.pat;
1113 state.searchpattern <- s;
1114 search s isforw
1116 enttext (Some (c, "", Some (onhist state.hists.pat),
1117 textentry, ondone (c ='/')))
1119 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1120 conf.zoom <- min 2.2 (conf.zoom +. 0.1);
1121 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1122 reshape conf.winw conf.winh
1124 | '+' ->
1125 let ondone s =
1126 let n =
1127 try int_of_string s with exc ->
1128 state.text <- Printf.sprintf "bad integer `%s': %s"
1129 s (Printexc.to_string exc);
1130 max_int
1132 if n != max_int
1133 then (
1134 conf.pagebias <- n;
1135 state.text <- "page bias is now " ^ string_of_int n;
1138 enttext (Some ('+', "", None, intentry, ondone))
1140 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1141 conf.zoom <- max 0.1 (conf.zoom -. 0.1);
1142 if conf.zoom <= 1.0 then state.x <- 0;
1143 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1144 reshape conf.winw conf.winh;
1146 | '-' ->
1147 let ondone msg =
1148 state.text <- msg;
1150 enttext (Some ('-', "", None, optentry, ondone))
1152 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1153 state.x <- 0;
1154 conf.zoom <- 1.0;
1155 state.text <- "zoom is 100%";
1156 reshape conf.winw conf.winh
1158 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1159 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1160 if zoom < 1.0
1161 then (
1162 conf.zoom <- zoom;
1163 state.x <- 0;
1164 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1165 reshape conf.winw conf.winh;
1168 | '0' .. '9' ->
1169 let ondone s =
1170 let n =
1171 try int_of_string s with exc ->
1172 state.text <- Printf.sprintf "bad integer `%s': %s"
1173 s (Printexc.to_string exc);
1176 if n >= 0
1177 then (
1178 addnav ();
1179 cbput state.hists.pag (string_of_int n);
1180 cbrfollowlen state.hists.pag;
1181 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1184 let pageentry text key =
1185 match Char.unsafe_chr key with
1186 | 'g' -> TEdone text
1187 | _ -> intentry text key
1189 let text = "x" in text.[0] <- c;
1190 enttext (Some (':', text, Some (onhist state.hists.pag),
1191 pageentry, ondone))
1193 | 'b' ->
1194 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1195 reshape conf.winw conf.winh;
1197 | 'l' ->
1198 conf.hlinks <- not conf.hlinks;
1199 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1200 Glut.postRedisplay ()
1202 | 'a' ->
1203 conf.autoscroll <- not conf.autoscroll
1205 | 'P' ->
1206 conf.presentation <- not conf.presentation;
1207 showtext ' ' ("presentation mode " ^
1208 if conf.presentation then "on" else "off");
1209 represent ()
1211 | 'f' ->
1212 begin match state.fullscreen with
1213 | None ->
1214 state.fullscreen <- Some (conf.winw, conf.winh);
1215 Glut.fullScreen ()
1216 | Some (w, h) ->
1217 state.fullscreen <- None;
1218 doreshape w h
1221 | 'g' ->
1222 gotoy_and_clear_text 0
1224 | 'n' ->
1225 search state.searchpattern true
1227 | 'p' | 'N' ->
1228 search state.searchpattern false
1230 | 't' ->
1231 begin match state.layout with
1232 | [] -> ()
1233 | l :: _ ->
1234 gotoy_and_clear_text (getpagey l.pageno)
1237 | ' ' ->
1238 begin match List.rev state.layout with
1239 | [] -> ()
1240 | l :: _ ->
1241 let pageno = min (l.pageno+1) (state.pagecount-1) in
1242 gotoy_and_clear_text (getpagey pageno)
1245 | '\127' ->
1246 begin match state.layout with
1247 | [] -> ()
1248 | l :: _ ->
1249 let pageno = max 0 (l.pageno-1) in
1250 gotoy_and_clear_text (getpagey pageno)
1253 | '=' ->
1254 let f (fn, ln) l =
1255 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1257 let fn, ln = List.fold_left f (-1, -1) state.layout in
1258 let s =
1259 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1260 let percent =
1261 if maxy <= 0
1262 then 100.
1263 else (100. *. (float state.y /. float maxy)) in
1264 if fn = ln
1265 then
1266 Printf.sprintf "Page %d of %d %.2f%%"
1267 (fn+1) state.pagecount percent
1268 else
1269 Printf.sprintf
1270 "Pages %d-%d of %d %.2f%%"
1271 (fn+1) (ln+1) state.pagecount percent
1273 showtext ' ' s;
1275 | 'w' ->
1276 begin match state.layout with
1277 | [] -> ()
1278 | l :: _ ->
1279 doreshape (l.pagew + conf.scrollw) l.pageh;
1280 Glut.postRedisplay ();
1283 | '\'' ->
1284 enterbookmarkmode ()
1286 | 'm' ->
1287 let ondone s =
1288 match state.layout with
1289 | l :: _ ->
1290 state.bookmarks <-
1291 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1292 :: state.bookmarks
1293 | _ -> ()
1295 enttext (Some ('~', "", None, textentry, ondone))
1297 | '~' ->
1298 quickbookmark ();
1299 showtext ' ' "Quick bookmark added";
1301 | 'z' ->
1302 begin match state.layout with
1303 | l :: _ ->
1304 let rect = getpdimrect l.pagedimno in
1305 let w, h =
1306 if conf.crophack
1307 then
1308 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1309 truncate (1.2 *. (rect.(3) -. rect.(0))))
1310 else
1311 (truncate (rect.(1) -. rect.(0)),
1312 truncate (rect.(3) -. rect.(0)))
1314 if w != 0 && h != 0
1315 then
1316 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1318 Glut.postRedisplay ();
1320 | [] -> ()
1323 | '<' | '>' ->
1324 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1326 | '[' | ']' ->
1327 state.colorscale <-
1328 max 0.0
1329 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1330 Glut.postRedisplay ()
1332 | 'k' -> gotoy (clamp (-conf.scrollincr))
1333 | 'j' -> gotoy (clamp conf.scrollincr)
1335 | 'r' -> opendoc state.path state.password
1337 | _ ->
1338 vlog "huh? %d %c" key (Char.chr key);
1341 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1342 let len = String.length text in
1343 if len = 0
1344 then (
1345 state.textentry <- None;
1346 Glut.postRedisplay ();
1348 else (
1349 let s = String.sub text 0 (len - 1) in
1350 enttext (Some (c, s, onhist, onkey, ondone))
1353 | Some (c, text, onhist, onkey, ondone) ->
1354 begin match Char.unsafe_chr key with
1355 | '\r' | '\n' ->
1356 ondone text;
1357 state.textentry <- None;
1358 Glut.postRedisplay ()
1360 | '\027' ->
1361 state.textentry <- None;
1362 Glut.postRedisplay ()
1364 | _ ->
1365 begin match onkey text key with
1366 | TEdone text ->
1367 state.textentry <- None;
1368 ondone text;
1369 Glut.postRedisplay ()
1371 | TEcont text ->
1372 enttext (Some (c, text, onhist, onkey, ondone));
1374 | TEstop ->
1375 state.textentry <- None;
1376 Glut.postRedisplay ()
1378 | TEswitch te ->
1379 state.textentry <- Some te;
1380 Glut.postRedisplay ()
1381 end;
1382 end;
1385 let narrow outlines pattern =
1386 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1387 match reopt with
1388 | None -> None
1389 | Some re ->
1390 let rec fold accu n =
1391 if n = -1
1392 then accu
1393 else
1394 let (s, _, _, _) as o = outlines.(n) in
1395 let accu =
1396 if (try ignore (Str.search_forward re s 0); true
1397 with Not_found -> false)
1398 then (o :: accu)
1399 else accu
1401 fold accu (n-1)
1403 let matched = fold [] (Array.length outlines - 1) in
1404 if matched = [] then None else Some (Array.of_list matched)
1407 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1408 let search active pattern incr =
1409 let dosearch re =
1410 let rec loop n =
1411 if n = Array.length outlines || n = -1
1412 then None
1413 else
1414 let (s, _, _, _) = outlines.(n) in
1416 (try ignore (Str.search_forward re s 0); true
1417 with Not_found -> false)
1418 then Some n
1419 else loop (n + incr)
1421 loop active
1424 let re = Str.regexp_case_fold pattern in
1425 dosearch re
1426 with Failure s ->
1427 state.text <- s;
1428 None
1430 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1431 match key with
1432 | 27 ->
1433 if String.length qsearch = 0
1434 then (
1435 state.text <- "";
1436 state.outline <- None;
1437 Glut.postRedisplay ();
1439 else (
1440 state.text <- "";
1441 state.outline <- Some (allowdel, active, first, outlines, "");
1442 Glut.postRedisplay ();
1445 | 18 | 19 ->
1446 let incr = if key = 18 then -1 else 1 in
1447 let active, first =
1448 match search (active + incr) qsearch incr with
1449 | None ->
1450 state.text <- qsearch ^ " [not found]";
1451 active, first
1452 | Some active ->
1453 state.text <- qsearch;
1454 active, firstof active
1456 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1457 Glut.postRedisplay ();
1459 | 8 ->
1460 let len = String.length qsearch in
1461 if len = 0
1462 then ()
1463 else (
1464 if len = 1
1465 then (
1466 state.text <- "";
1467 state.outline <- Some (allowdel, active, first, outlines, "");
1469 else
1470 let qsearch = String.sub qsearch 0 (len - 1) in
1471 let active, first =
1472 match search active qsearch ~-1 with
1473 | None ->
1474 state.text <- qsearch ^ " [not found]";
1475 active, first
1476 | Some active ->
1477 state.text <- qsearch;
1478 active, firstof active
1480 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1482 Glut.postRedisplay ()
1484 | 13 ->
1485 if active < Array.length outlines
1486 then (
1487 let (_, _, n, t) = outlines.(active) in
1488 gotopage n t;
1490 state.text <- "";
1491 if allowdel then state.bookmarks <- Array.to_list outlines;
1492 state.outline <- None;
1493 Glut.postRedisplay ();
1495 | _ when key >= 32 && key < 127 ->
1496 let pattern = addchar qsearch (Char.chr key) in
1497 let active, first =
1498 match search active pattern 1 with
1499 | None ->
1500 state.text <- pattern ^ " [not found]";
1501 active, first
1502 | Some active ->
1503 state.text <- pattern;
1504 active, firstof active
1506 state.outline <- Some (allowdel, active, first, outlines, pattern);
1507 Glut.postRedisplay ()
1509 | 14 when not allowdel -> (* ctrl-n *)
1510 if String.length qsearch > 0
1511 then (
1512 let optoutlines = narrow outlines qsearch in
1513 begin match optoutlines with
1514 | None -> state.text <- "can't narrow"
1515 | Some outlines ->
1516 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1517 match state.outlines with
1518 | Olist l -> ()
1519 | Oarray a ->
1520 state.outlines <- Onarrow (qsearch, outlines, a)
1521 | Onarrow (pat, a, b) ->
1522 state.outlines <- Onarrow (qsearch, outlines, b)
1523 end;
1525 Glut.postRedisplay ()
1527 | 21 when not allowdel -> (* ctrl-u *)
1528 let outline =
1529 match state.outlines with
1530 | Oarray a -> a
1531 | Olist l ->
1532 let a = Array.of_list (List.rev l) in
1533 state.outlines <- Oarray a;
1535 | Onarrow (pat, a, b) ->
1536 state.outlines <- Oarray b;
1537 state.text <- "";
1540 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1541 Glut.postRedisplay ()
1543 | 12 ->
1544 state.outline <-
1545 Some (allowdel, active, firstof active, outlines, qsearch);
1546 Glut.postRedisplay ()
1548 | 127 when allowdel ->
1549 let len = Array.length outlines - 1 in
1550 if len = 0
1551 then (
1552 state.outline <- None;
1553 state.bookmarks <- [];
1555 else (
1556 let bookmarks = Array.init len
1557 (fun i ->
1558 let i = if i >= active then i + 1 else i in
1559 outlines.(i)
1562 state.outline <-
1563 Some (allowdel,
1564 min active (len-1),
1565 min first (len-1),
1566 bookmarks, qsearch)
1569 Glut.postRedisplay ()
1571 | _ -> log "unknown key %d" key
1574 let keyboard ~key ~x ~y =
1575 if key = 7
1576 then
1577 wcmd "interrupt" []
1578 else
1579 match state.outline with
1580 | None -> viewkeyboard ~key ~x ~y
1581 | Some outline -> outlinekeyboard ~key ~x ~y outline
1584 let special ~key ~x ~y =
1585 match state.outline with
1586 | None ->
1587 begin match state.textentry with
1588 | None ->
1589 let y =
1590 match key with
1591 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1592 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1593 | Glut.KEY_DOWN -> clamp conf.scrollincr
1594 | Glut.KEY_PAGE_UP ->
1595 if Glut.getModifiers () land Glut.active_ctrl != 0
1596 then
1597 match state.layout with
1598 | [] -> state.y
1599 | l :: _ -> state.y - l.pagey
1600 else
1601 clamp (-conf.winh)
1602 | Glut.KEY_PAGE_DOWN ->
1603 if Glut.getModifiers () land Glut.active_ctrl != 0
1604 then
1605 match List.rev state.layout with
1606 | [] -> state.y
1607 | l :: _ -> getpagey l.pageno
1608 else
1609 clamp conf.winh
1610 | Glut.KEY_HOME -> addnav (); 0
1611 | Glut.KEY_END ->
1612 addnav ();
1613 state.maxy - (if conf.maxhfit then conf.winh else 0)
1615 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1616 state.x <- state.x - 10;
1617 state.y
1618 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1619 state.x <- state.x + 10;
1620 state.y
1622 | _ -> state.y
1624 gotoy_and_clear_text y
1626 | Some (c, s, Some onhist, onkey, ondone) ->
1627 let s =
1628 match key with
1629 | Glut.KEY_UP -> onhist HCprev
1630 | Glut.KEY_DOWN -> onhist HCnext
1631 | Glut.KEY_HOME -> onhist HCfirst
1632 | Glut.KEY_END -> onhist HClast
1633 | _ -> state.text
1635 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1636 Glut.postRedisplay ()
1638 | _ -> ()
1641 | Some (allowdel, active, first, outlines, qsearch) ->
1642 let maxrows = maxoutlinerows () in
1643 let calcfirst first active =
1644 if active > first
1645 then
1646 let rows = active - first in
1647 if rows > maxrows then active - maxrows else first
1648 else active
1650 let navigate incr =
1651 let active = active + incr in
1652 let active = max 0 (min active (Array.length outlines - 1)) in
1653 let first = calcfirst first active in
1654 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1655 Glut.postRedisplay ()
1657 let updownlevel incr =
1658 let len = Array.length outlines in
1659 let (_, curlevel, _, _) = outlines.(active) in
1660 let rec flow i =
1661 if i = len then i-1 else if i = -1 then 0 else
1662 let (_, l, _, _) = outlines.(i) in
1663 if l != curlevel then i else flow (i+incr)
1665 let active = flow active in
1666 let first = calcfirst first active in
1667 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1668 Glut.postRedisplay ()
1670 match key with
1671 | Glut.KEY_UP -> navigate ~-1
1672 | Glut.KEY_DOWN -> navigate 1
1673 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1674 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1676 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1677 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1679 | Glut.KEY_HOME ->
1680 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1681 Glut.postRedisplay ()
1683 | Glut.KEY_END ->
1684 let active = Array.length outlines - 1 in
1685 let first = max 0 (active - maxrows) in
1686 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1687 Glut.postRedisplay ()
1689 | _ -> ()
1692 let drawplaceholder l =
1693 GlDraw.color (scalecolor 1.0);
1694 GlDraw.rect
1695 (float l.pagex, float l.pagedispy)
1696 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1698 let x = float l.pagex
1699 and y = float (l.pagedispy + 13) in
1700 let font = Glut.BITMAP_8_BY_13 in
1701 GlDraw.color (0.0, 0.0, 0.0);
1702 GlPix.raster_pos ~x ~y ();
1703 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1704 ("Loading " ^ string_of_int (l.pageno + 1));
1707 let now () = Unix.gettimeofday ();;
1709 let drawpage i l =
1710 begin match getopaque l.pageno with
1711 | Some opaque when validopaque opaque ->
1712 if state.textentry = None
1713 then GlDraw.color (scalecolor 1.0)
1714 else GlDraw.color (scalecolor 0.4);
1715 let a = now () in
1716 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1717 opaque;
1718 let b = now () in
1719 let d = b-.a in
1720 vlog "draw %d %f sec" l.pageno d;
1722 | _ ->
1723 drawplaceholder l;
1724 end;
1725 l.pagedispy + l.pagevh;
1728 let scrollph y =
1729 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1730 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1731 let sh = float conf.winh /. sh in
1732 let sh = max sh (float conf.scrollh) in
1734 let percent =
1735 if state.y = state.maxy
1736 then 1.0
1737 else float y /. float maxy
1739 let position = (float conf.winh -. sh) *. percent in
1741 let position =
1742 if position +. sh > float conf.winh
1743 then float conf.winh -. sh
1744 else position
1746 position, sh;
1749 let scrollindicator () =
1750 GlDraw.color (0.64 , 0.64, 0.64);
1751 GlDraw.rect
1752 (float (conf.winw - conf.scrollw), 0.)
1753 (float conf.winw, float conf.winh)
1755 GlDraw.color (0.0, 0.0, 0.0);
1757 let position, sh = scrollph state.y in
1758 GlDraw.rect
1759 (float (conf.winw - conf.scrollw), position)
1760 (float conf.winw, position +. sh)
1764 let showsel margin =
1765 match state.mstate with
1766 | Mnone | Mscroll _ | Mpan _ ->
1769 | Msel ((x0, y0), (x1, y1)) ->
1770 let rec loop = function
1771 | l :: ls ->
1772 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1773 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1774 then
1775 match getopaque l.pageno with
1776 | Some opaque when validopaque opaque ->
1777 let oy = -l.pagey + l.pagedispy in
1778 seltext opaque
1779 (x0 - margin - state.x, y0,
1780 x1 - margin - state.x, y1) oy;
1782 | _ -> ()
1783 else loop ls
1784 | [] -> ()
1786 loop state.layout
1789 let showrects () =
1790 let panx = float state.x in
1791 Gl.enable `blend;
1792 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1793 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1794 List.iter
1795 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1796 List.iter (fun l ->
1797 if l.pageno = pageno
1798 then (
1799 let d = float (l.pagedispy - l.pagey) in
1800 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1801 GlDraw.begins `quads;
1803 GlDraw.vertex2 (x0+.panx, y0+.d);
1804 GlDraw.vertex2 (x1+.panx, y1+.d);
1805 GlDraw.vertex2 (x2+.panx, y2+.d);
1806 GlDraw.vertex2 (x3+.panx, y3+.d);
1808 GlDraw.ends ();
1810 ) state.layout
1811 ) state.rects
1813 Gl.disable `blend;
1816 let showoutline = function
1817 | None -> ()
1818 | Some (allowdel, active, first, outlines, qsearch) ->
1819 Gl.enable `blend;
1820 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1821 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1822 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
1823 Gl.disable `blend;
1825 GlDraw.color (1., 1., 1.);
1826 let font = Glut.BITMAP_9_BY_15 in
1827 let draw_string x y s =
1828 GlPix.raster_pos ~x ~y ();
1829 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1831 let rec loop row =
1832 if row = Array.length outlines || (row - first) * 16 > conf.winh
1833 then ()
1834 else (
1835 let (s, l, _, _) = outlines.(row) in
1836 let y = (row - first) * 16 in
1837 let x = 5 + 15*l in
1838 if row = active
1839 then (
1840 Gl.enable `blend;
1841 GlDraw.polygon_mode `both `line;
1842 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1843 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1844 GlDraw.rect (0., float (y + 1))
1845 (float (conf.winw - 1), float (y + 18));
1846 GlDraw.polygon_mode `both `fill;
1847 Gl.disable `blend;
1848 GlDraw.color (1., 1., 1.);
1850 draw_string (float x) (float (y + 16)) s;
1851 loop (row+1)
1854 loop first
1857 let display () =
1858 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1859 GlDraw.viewport margin 0 state.w conf.winh;
1860 pagematrix ();
1861 GlClear.color (scalecolor 0.5);
1862 GlClear.clear [`color];
1863 if state.x != 0
1864 then (
1865 let x = float state.x in
1866 GlMat.translate ~x ();
1868 if conf.zoom > 1.0
1869 then (
1870 Gl.enable `scissor_test;
1871 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
1873 let _lasty = List.fold_left drawpage 0 (state.layout) in
1874 if conf.zoom > 1.0
1875 then
1876 Gl.disable `scissor_test
1878 if state.x != 0
1879 then (
1880 let x = -.float state.x in
1881 GlMat.translate ~x ();
1883 showrects ();
1884 showsel margin;
1885 GlDraw.viewport 0 0 conf.winw conf.winh;
1886 winmatrix ();
1887 scrollindicator ();
1888 showoutline state.outline;
1889 enttext ();
1890 Glut.swapBuffers ();
1893 let getunder x y =
1894 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1895 let x = x - margin - state.x in
1896 let rec f = function
1897 | l :: rest ->
1898 begin match getopaque l.pageno with
1899 | Some opaque when validopaque opaque ->
1900 let y = y - l.pagedispy in
1901 if y > 0
1902 then
1903 let y = l.pagey + y in
1904 let x = x - l.pagex in
1905 match whatsunder opaque x y with
1906 | Unone -> f rest
1907 | under -> under
1908 else
1909 f rest
1910 | _ ->
1911 f rest
1913 | [] -> Unone
1915 f state.layout
1918 let mouse ~button ~bstate ~x ~y =
1919 match button with
1920 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1921 let incr =
1922 if n = 3
1923 then
1924 -conf.scrollincr
1925 else
1926 conf.scrollincr
1928 let incr = incr * 2 in
1929 let y = clamp incr in
1930 gotoy_and_clear_text y
1932 | Glut.LEFT_BUTTON when state.outline = None
1933 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
1934 if bstate = Glut.DOWN
1935 then (
1936 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1937 state.mstate <- Mpan (x, y)
1939 else
1940 state.mstate <- Mnone
1942 | Glut.LEFT_BUTTON
1943 when state.outline = None && x > conf.winw - conf.scrollw ->
1944 if bstate = Glut.DOWN
1945 then
1946 let position, sh = scrollph state.y in
1947 if y > truncate position && y < truncate (position +. sh)
1948 then
1949 state.mstate <- Mscroll
1950 else
1951 let percent = float y /. float conf.winh in
1952 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
1953 gotoy desty;
1954 state.mstate <- Mscroll
1955 else
1956 state.mstate <- Mnone
1958 | Glut.LEFT_BUTTON when state.outline = None ->
1959 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1960 begin match dest with
1961 | Ulinkgoto (pageno, top) ->
1962 if pageno >= 0
1963 then
1964 gotopage1 pageno top
1966 | Ulinkuri s ->
1967 print_endline s
1969 | Unone when bstate = Glut.DOWN ->
1970 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1971 state.mstate <- Mpan (x, y);
1973 | Unone | Utext _ ->
1974 if bstate = Glut.DOWN
1975 then (
1976 if conf.angle mod 360 = 0
1977 then (
1978 state.mstate <- Msel ((x, y), (x, y));
1979 Glut.postRedisplay ()
1982 else (
1983 match state.mstate with
1984 | Mnone -> ()
1986 | Mscroll ->
1987 state.mstate <- Mnone
1989 | Mpan _ ->
1990 Glut.setCursor Glut.CURSOR_INHERIT;
1991 state.mstate <- Mnone
1993 | Msel ((x0, y0), (x1, y1)) ->
1994 let f l =
1995 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1996 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1997 then
1998 match getopaque l.pageno with
1999 | Some opaque when validopaque opaque ->
2000 copysel opaque
2001 | _ -> ()
2003 List.iter f state.layout;
2004 copysel ""; (* ugly *)
2005 Glut.setCursor Glut.CURSOR_INHERIT;
2006 state.mstate <- Mnone;
2010 | _ ->
2013 let mouse ~button ~state ~x ~y = mouse button state x y;;
2015 let motion ~x ~y =
2016 if state.outline = None
2017 then
2018 match state.mstate with
2019 | Mnone -> ()
2021 | Mpan (x0, y0) ->
2022 let dx = x - x0
2023 and dy = y0 - y in
2024 state.mstate <- Mpan (x, y);
2025 if conf.zoom > 1.0 then state.x <- state.x + dx;
2026 let y = clamp dy in
2027 gotoy_and_clear_text y
2029 | Msel (a, _) ->
2030 state.mstate <- Msel (a, (x, y));
2031 Glut.postRedisplay ()
2033 | Mscroll ->
2034 let y = min conf.winh (max 0 y) in
2035 let percent = float y /. float conf.winh in
2036 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2037 gotoy_and_clear_text y
2040 let pmotion ~x ~y =
2041 if state.outline = None
2042 then
2043 match state.mstate with
2044 | Mnone ->
2045 begin match getunder x y with
2046 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2047 | Ulinkuri uri ->
2048 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2049 Glut.setCursor Glut.CURSOR_INFO
2050 | Ulinkgoto (page, y) ->
2051 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
2052 Glut.setCursor Glut.CURSOR_INFO
2053 | Utext s ->
2054 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2055 Glut.setCursor Glut.CURSOR_TEXT
2058 | Mpan _ | Msel _ | Mscroll ->
2062 module State =
2063 struct
2064 open Parser
2066 let home =
2068 match Sys.os_type with
2069 | "Win32" -> Sys.getenv "HOMEPATH"
2070 | _ -> Sys.getenv "HOME"
2071 with exn ->
2072 prerr_endline
2073 ("Can not determine home directory location: " ^
2074 Printexc.to_string exn);
2078 let config_of c attrs =
2079 let apply c k v =
2081 match k with
2082 | "scroll-bar-width" -> { c with scrollw = int_of_string v }
2083 | "scroll-handle-height" -> { c with scrollh = int_of_string v }
2084 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2085 | "preload" -> { c with preload = bool_of_string v }
2086 | "page-bias" -> { c with pagebias = int_of_string v }
2087 | "scroll-step" -> { c with scrollincr = int_of_string v }
2088 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2089 | "crop-hack" -> { c with crophack = bool_of_string v }
2090 | "throttle" -> { c with showall = bool_of_string v }
2091 | "highlight-links" -> { c with hlinks = bool_of_string v }
2092 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2093 | "vertical-margin" -> { c with interpagespace = int_of_string v }
2094 | "zoom" ->
2095 let zoom = float_of_string v /. 100. in
2096 let zoom = max 0.1 (min 2.2 zoom) in
2097 { c with zoom = zoom }
2098 | "presentation" -> { c with presentation = bool_of_string v }
2099 | "rotation-angle" -> { c with angle = int_of_string v }
2100 | "width" -> { c with winw = int_of_string v }
2101 | "height" -> { c with winh = int_of_string v }
2102 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2103 | "proportional-display" -> { c with proportional = bool_of_string v }
2104 | _ -> c
2105 with exn ->
2106 prerr_endline ("Error processing attribute (`" ^
2107 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2110 let rec fold c = function
2111 | [] -> c
2112 | (k, v) :: rest ->
2113 let c = apply c k v in
2114 fold c rest
2116 fold c attrs;
2119 let bookmark_of attrs =
2120 let rec fold title page rely = function
2121 | ("title", v) :: rest -> fold v page rely rest
2122 | ("page", v) :: rest -> fold title v rely rest
2123 | ("rely", v) :: rest -> fold title page v rest
2124 | _ :: rest -> fold title page rely rest
2125 | [] -> title, page, rely
2127 fold "invalid" "0" "0" attrs
2130 let setconf dst src =
2131 dst.scrollw <- src.scrollw;
2132 dst.scrollh <- src.scrollh;
2133 dst.icase <- src.icase;
2134 dst.preload <- src.preload;
2135 dst.pagebias <- src.pagebias;
2136 dst.verbose <- src.verbose;
2137 dst.scrollincr <- src.scrollincr;
2138 dst.maxhfit <- src.maxhfit;
2139 dst.crophack <- src.crophack;
2140 dst.autoscroll <- src.autoscroll;
2141 dst.showall <- src.showall;
2142 dst.hlinks <- src.hlinks;
2143 dst.underinfo <- src.underinfo;
2144 dst.interpagespace <- src.interpagespace;
2145 dst.zoom <- src.zoom;
2146 dst.presentation <- src.presentation;
2147 dst.angle <- src.angle;
2148 dst.winw <- src.winw;
2149 dst.winh <- src.winh;
2150 dst.savebmarks <- src.savebmarks;
2151 dst.proportional <- src.proportional;
2154 let unent s =
2155 let l = String.length s in
2156 let b = Buffer.create l in
2157 unent b s 0 l;
2158 Buffer.contents b;
2161 let get s =
2162 let h = Hashtbl.create 10 in
2163 let dc = { defconf with angle = defconf.angle } in
2164 let rec toplevel v t spos epos =
2165 match t with
2166 | Vdata | Vcdata | Vend -> v
2167 | Vopen ("llppconfig", attrs, closed) ->
2168 if closed
2169 then v
2170 else { v with f = llppconfig }
2171 | Vopen _ ->
2172 error "unexpected subelement at top level" s spos
2173 | Vclose tag -> error "unexpected close at toplevel" s spos
2175 and llppconfig v t spos epos =
2176 match t with
2177 | Vdata | Vcdata | Vend -> v
2178 | Vopen ("defaults", attrs, closed) ->
2179 let c = config_of dc attrs in
2180 setconf dc c;
2181 if closed
2182 then v
2183 else { v with f = skip "defaults" (fun () -> v) }
2185 | Vopen ("doc", attrs, closed) ->
2186 let pathent =
2188 List.assoc "path" attrs
2189 with Not_found -> error "doc is missing path attribute" s spos
2191 let path = unent pathent in
2192 let c = config_of dc attrs in
2193 let y =
2195 float_of_string (List.assoc "rely" attrs)
2196 with
2197 | Not_found -> 0.0
2198 | exn ->
2199 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2202 let x =
2204 int_of_string (List.assoc "pan" attrs)
2205 with
2206 | Not_found -> 0
2207 | exn ->
2208 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2211 if closed
2212 then (Hashtbl.add h path (c, [], x, y); v)
2213 else { v with f = doc path x y c [] }
2215 | Vopen (tag, _, closed) ->
2216 error "unexpected subelement in llppconfig" s spos
2218 | Vclose "llppconfig" -> { v with f = toplevel }
2219 | Vclose tag -> error "unexpected close in llppconfig" s spos
2221 and doc path x y c bookmarks v t spos epos =
2222 match t with
2223 | Vdata | Vcdata -> v
2224 | Vend -> error "unexpected end of input in doc" s spos
2225 | Vopen ("bookmarks", attrs, closed) ->
2226 { v with f = pbookmarks path x y c bookmarks }
2228 | Vopen (tag, _, _) ->
2229 error "unexpected subelement in doc" s spos
2231 | Vclose "doc" ->
2232 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2233 { v with f = llppconfig }
2235 | Vclose tag -> error "unexpected close in doc" s spos
2237 and pbookmarks path x y c bookmarks v t spos epos =
2238 match t with
2239 | Vdata | Vcdata -> v
2240 | Vend -> error "unexpected end of input in bookmarks" s spos
2241 | Vopen ("item", attrs, closed) ->
2242 let titleent, spage, srely = bookmark_of attrs in
2243 let page =
2245 int_of_string spage
2246 with exn ->
2247 dolog "Failed to convert page %S to integer: %s"
2248 spage (Printexc.to_string exn);
2251 let rely =
2253 float_of_string srely
2254 with exn ->
2255 dolog "Failed to convert rely %S to real: %s"
2256 srely (Printexc.to_string exn);
2259 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2260 if closed
2261 then { v with f = pbookmarks path x y c bookmarks }
2262 else
2263 let f () = v in
2264 { v with f = skip "item" f }
2266 | Vopen _ ->
2267 error "unexpected subelement in bookmarks" s spos
2269 | Vclose "bookmarks" ->
2270 { v with f = doc path x y c bookmarks }
2272 | Vclose tag -> error "unexpected close in bookmarks" s spos
2274 and skip tag f v t spos epos =
2275 match t with
2276 | Vdata | Vcdata -> v
2277 | Vend ->
2278 error ("unexpected end of input in skipped " ^ tag) s spos
2279 | Vopen (tag', _, closed) ->
2280 if closed
2281 then v
2282 else
2283 let f' () = { v with f = skip tag f } in
2284 { v with f = skip tag' f' }
2285 | Vclose ctag ->
2286 if tag = ctag
2287 then f ()
2288 else error ("unexpected close in skipped " ^ tag) s spos
2291 parse { f = toplevel; accu = () } s;
2292 h, dc;
2295 let do_load f ic =
2297 let len = in_channel_length ic in
2298 let s = String.create len in
2299 really_input ic s 0 len;
2300 f s;
2301 with
2302 | Parse_error (msg, s, pos) ->
2303 let subs = subs s pos in
2304 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2305 failwith ("parse error: " ^ s)
2307 | exn ->
2308 failwith ("config load error: " ^ Printexc.to_string exn)
2311 let path =
2312 let dir =
2314 let dir = Filename.concat home ".config" in
2315 if Sys.is_directory dir then dir else home
2316 with _ -> home
2318 Filename.concat dir "llpp.conf"
2321 let load1 f =
2322 if Sys.file_exists path
2323 then
2324 match
2325 (try Some (open_in_bin path)
2326 with exn ->
2327 prerr_endline
2328 ("Error opening configuation file `" ^ path ^ "': " ^
2329 Printexc.to_string exn);
2330 None
2332 with
2333 | Some ic ->
2334 begin try
2335 f (do_load get ic)
2336 with exn ->
2337 prerr_endline
2338 ("Error loading configuation from `" ^ path ^ "': " ^
2339 Printexc.to_string exn);
2340 end;
2341 close_in ic;
2343 | None -> ()
2344 else
2345 f (Hashtbl.create 0, defconf)
2348 let load () =
2349 let f (h, dc) =
2350 let pc, pb, px, py =
2352 Hashtbl.find h state.path
2353 with Not_found -> dc, [], 0, 0.0
2355 setconf defconf dc;
2356 setconf conf pc;
2357 state.bookmarks <- pb;
2358 state.x <- px;
2359 cbput state.hists.nav py;
2360 cbrfollowlen state.hists.nav;
2362 load1 f
2365 let add_attrs bb always dc c =
2366 let ob s a b =
2367 if always || a != b
2368 then Printf.bprintf bb "\n %s='%b'" s a
2369 and oi s a b =
2370 if always || a != b
2371 then Printf.bprintf bb "\n %s='%d'" s a
2372 and oz s a b =
2373 if always || a <> b
2374 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2376 let w, h =
2377 if always
2378 then dc.winw, dc.winh
2379 else
2380 match state.fullscreen with
2381 | Some wh -> wh
2382 | None -> c.winw, c.winh
2384 oi "width" w dc.winw;
2385 oi "height" h dc.winh;
2386 oi "scroll-bar-width" c.scrollw dc.scrollw;
2387 oi "scroll-handle-height" c.scrollh dc.scrollh;
2388 ob "case-insensitive-search" c.icase dc.icase;
2389 ob "preload" c.preload dc.preload;
2390 oi "page-bias" c.pagebias dc.pagebias;
2391 oi "scroll-step" c.scrollincr dc.scrollincr;
2392 ob "max-height-fit" c.maxhfit dc.maxhfit;
2393 ob "crop-hack" c.crophack dc.crophack;
2394 ob "throttle" c.showall dc.showall;
2395 ob "highlight-links" c.hlinks dc.hlinks;
2396 ob "under-cursor-info" c.underinfo dc.underinfo;
2397 oi "vertical-margin" c.interpagespace dc.interpagespace;
2398 oz "zoom" c.zoom dc.zoom;
2399 ob "presentation" c.presentation dc.presentation;
2400 oi "rotation-angle" c.angle dc.angle;
2401 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2402 ob "proportional-display" c.proportional dc.proportional;
2405 let save () =
2406 let bb = Buffer.create 32768 in
2407 let f (h, dc) =
2408 Buffer.add_string bb "<llppconfig>\n<defaults ";
2409 add_attrs bb true dc dc;
2410 Buffer.add_string bb "/>\n";
2412 let adddoc path x y c bookmarks =
2413 if bookmarks == [] && c = dc && y = 0.0
2414 then ()
2415 else (
2416 Printf.bprintf bb "<doc path='%s'"
2417 (enent path 0 (String.length path));
2419 if y <> 0.0
2420 then Printf.bprintf bb " rely='%f'" y;
2422 if x != 0
2423 then Printf.bprintf bb " pan='%d'" x;
2425 add_attrs bb false dc c;
2427 begin match bookmarks with
2428 | [] -> Buffer.add_string bb "/>\n"
2429 | _ ->
2430 Buffer.add_string bb ">\n<bookmarks>\n";
2431 List.iter (fun (title, _level, page, rely) ->
2432 Printf.bprintf bb
2433 "<item title='%s' page='%d' rely='%f'/>\n"
2434 (enent title 0 (String.length title))
2435 page
2436 rely
2437 ) bookmarks;
2438 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2439 end;
2443 adddoc state.path state.x (yratio state.y) conf
2444 (if conf.savebmarks then state.bookmarks else []);
2446 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2447 if path <> state.path
2448 then
2449 adddoc path x y c bookmarks
2450 ) h;
2451 Buffer.add_string bb "</llppconfig>";
2453 load1 f;
2454 if Buffer.length bb > 0
2455 then
2457 let tmp = path ^ ".tmp" in
2458 let oc = open_out_bin tmp in
2459 Buffer.output_buffer oc bb;
2460 close_out oc;
2461 Sys.rename tmp path;
2462 with exn ->
2463 prerr_endline
2464 ("error while saving configuration: " ^ Printexc.to_string exn)
2466 end;;
2468 let () =
2469 Arg.parse
2470 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2471 (fun s -> state.path <- s)
2472 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2474 let path =
2475 if String.length state.path = 0
2476 then (prerr_endline "filename missing"; exit 1)
2477 else (
2478 if Filename.is_relative state.path
2479 then Filename.concat (Sys.getcwd ()) state.path
2480 else state.path
2483 state.path <- path;
2485 State.load ();
2487 let _ = Glut.init Sys.argv in
2488 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2489 let () = Glut.initWindowSize conf.winw conf.winh in
2490 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2492 let csock, ssock =
2493 if Sys.os_type = "Unix"
2494 then
2495 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2496 else
2497 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2498 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2499 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2500 Unix.bind sock addr;
2501 Unix.listen sock 1;
2502 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2503 Unix.connect csock addr;
2504 let ssock, _ = Unix.accept sock in
2505 Unix.close sock;
2506 let opts sock =
2507 Unix.setsockopt sock Unix.TCP_NODELAY true;
2508 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2510 opts ssock;
2511 opts csock;
2512 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2513 ssock, csock
2516 let () = Glut.displayFunc display in
2517 let () = Glut.reshapeFunc reshape in
2518 let () = Glut.keyboardFunc keyboard in
2519 let () = Glut.specialFunc special in
2520 let () = Glut.idleFunc (Some idle) in
2521 let () = Glut.mouseFunc mouse in
2522 let () = Glut.motionFunc motion in
2523 let () = Glut.passiveMotionFunc pmotion in
2525 init ssock;
2526 state.csock <- csock;
2527 state.ssock <- ssock;
2528 state.text <- "Opening " ^ path;
2529 writeopen state.path state.password;
2531 at_exit State.save;
2533 let rec handlelablglutbug () =
2535 Glut.mainLoop ();
2536 with Glut.BadEnum "key in special_of_int" ->
2537 showtext '!' " LablGlut bug: special key not recognized";
2538 handlelablglutbug ()
2540 handlelablglutbug ();