Sanitize width on rehsape
[llpp.git] / main.ml
blobd67cc5f631d6166bad0050698c741245b2030ae0
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 let w = max w 2 in
623 state.w <- w;
624 conf.winh <- h;
625 GlMat.mode `modelview;
626 GlMat.load_identity ();
627 GlClear.color (scalecolor 1.0);
628 GlClear.clear [`color];
630 invalidate ();
631 wcmd "geometry" [`i w; `i h];
634 let showtext c s =
635 GlDraw.color (0.0, 0.0, 0.0);
636 GlDraw.rect
637 (0.0, float (conf.winh - 18))
638 (float (conf.winw - conf.scrollw - 1), float conf.winh)
640 let font = Glut.BITMAP_8_BY_13 in
641 GlDraw.color (1.0, 1.0, 1.0);
642 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
643 Glut.bitmapCharacter ~font ~c:(Char.code c);
644 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
647 let enttext () =
648 let len = String.length state.text in
649 match state.textentry with
650 | None ->
651 if len > 0 then showtext ' ' state.text
653 | Some (c, text, _, _, _) ->
654 let s =
655 if len > 0
656 then
657 text ^ " [" ^ state.text ^ "]"
658 else
659 text
661 showtext c s;
664 let showtext c s =
665 if true
666 then (
667 state.text <- Printf.sprintf "%c%s" c s;
668 Glut.postRedisplay ();
670 else (
671 showtext c s;
672 Glut.swapBuffers ();
676 let act cmd =
677 match cmd.[0] with
678 | 'c' ->
679 state.pdims <- [];
681 | 'D' ->
682 state.rects <- state.rects1;
683 Glut.postRedisplay ()
685 | 'C' ->
686 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
687 state.pagecount <- n;
688 state.invalidated <- state.invalidated - 1;
689 if state.invalidated = 0
690 then represent ()
692 | 't' ->
693 let s = Scanf.sscanf cmd "t %n"
694 (fun n -> String.sub cmd n (String.length cmd - n))
696 Glut.setWindowTitle s
698 | 'T' ->
699 let s = Scanf.sscanf cmd "T %n"
700 (fun n -> String.sub cmd n (String.length cmd - n))
702 if state.textentry = None
703 then (
704 state.text <- s;
705 showtext ' ' s;
707 else (
708 state.text <- s;
709 Glut.postRedisplay ();
712 | 'V' ->
713 if conf.verbose
714 then
715 let s = Scanf.sscanf cmd "V %n"
716 (fun n -> String.sub cmd n (String.length cmd - n))
718 state.text <- s;
719 showtext ' ' s;
721 | 'F' ->
722 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
723 Scanf.sscanf cmd "F %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 let y = (getpagey pageno) + truncate y0 in
728 addnav ();
729 gotoy y;
730 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
732 | 'R' ->
733 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
734 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
735 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
736 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
738 state.rects1 <-
739 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
741 | 'r' ->
742 let n, w, h, r, l, p =
743 Scanf.sscanf cmd "r %d %d %d %d %d %s"
744 (fun n w h r l p -> (n, w, h, r, l != 0, p))
746 Hashtbl.replace state.pagemap (n, w, r, l) p;
747 let opaque = cbpeekw state.pagecache in
748 if validopaque opaque
749 then (
750 let k =
751 Hashtbl.fold
752 (fun k v a -> if v = opaque then k else a)
753 state.pagemap (-1, -1, -1, false)
755 wcmd "free" [`s opaque];
756 Hashtbl.remove state.pagemap k
758 cbput state.pagecache p;
759 state.rendering <- false;
760 if conf.showall
761 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
762 else (
763 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
764 if visible
765 then gotoy state.y
766 else (ignore (loadlayout state.layout); preload ())
769 | 'l' ->
770 let (n, w, h, x) as pdim =
771 Scanf.sscanf cmd "l %d %d %d %d" (fun n w h x -> n, w, h, x)
773 state.pdims <- pdim :: state.pdims
775 | 'o' ->
776 let (l, n, t, h, pos) =
777 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
779 let s = String.sub cmd pos (String.length cmd - pos) in
780 let s =
781 let l = String.length s in
782 let b = Buffer.create (String.length s) in
783 let rec loop pc2 i =
784 if i = l
785 then ()
786 else
787 let pc2 =
788 match s.[i] with
789 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
790 | '\xc2' -> true
791 | c ->
792 let c = if Char.code c land 0x80 = 0 then c else '?' in
793 Buffer.add_char b c;
794 false
796 loop pc2 (i+1)
798 loop false 0;
799 Buffer.contents b
801 let outline = (s, l, n, float t /. float h) in
802 let outlines =
803 match state.outlines with
804 | Olist outlines -> Olist (outline :: outlines)
805 | Oarray _ -> Olist [outline]
806 | Onarrow _ -> Olist [outline]
808 state.outlines <- outlines
810 | _ ->
811 log "unknown cmd `%S'" cmd
814 let now = Unix.gettimeofday;;
816 let idle () =
817 let rec loop delay =
818 let r, _, _ = Unix.select [state.csock] [] [] delay in
819 begin match r with
820 | [] ->
821 if conf.autoscroll
822 then begin
823 let y = state.y + conf.scrollincr in
824 let y = if y >= state.maxy then 0 else y in
825 gotoy y;
826 state.text <- "";
827 end;
829 | _ ->
830 let cmd = readcmd state.csock in
831 act cmd;
832 loop 0.0
833 end;
834 in loop 0.001
837 let onhist cb = function
838 | HCprev -> cbget cb ~-1
839 | HCnext -> cbget cb 1
840 | HCfirst -> cbget cb ~-(cb.rc)
841 | HClast -> cbget cb (cb.len - 1 - cb.rc)
844 let search pattern forward =
845 if String.length pattern > 0
846 then
847 let pn, py =
848 match state.layout with
849 | [] -> 0, 0
850 | l :: _ ->
851 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
853 let cmd =
854 let b = makecmd "search"
855 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
857 Buffer.add_char b ',';
858 Buffer.add_string b pattern;
859 Buffer.add_char b '\000';
860 Buffer.contents b;
862 writecmd state.csock cmd;
865 let intentry text key =
866 let c = Char.unsafe_chr key in
867 match c with
868 | '0' .. '9' ->
869 let s = "x" in s.[0] <- c;
870 let text = text ^ s in
871 TEcont text
873 | _ ->
874 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
875 TEcont text
878 let addchar s c =
879 let b = Buffer.create (String.length s + 1) in
880 Buffer.add_string b s;
881 Buffer.add_char b c;
882 Buffer.contents b;
885 let textentry text key =
886 let c = Char.unsafe_chr key in
887 match c with
888 | _ when key >= 32 && key < 127 ->
889 let text = addchar text c in
890 TEcont text
892 | _ ->
893 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
894 TEcont text
897 let reinit angle proportional =
898 conf.angle <- angle;
899 conf.proportional <- proportional;
900 invalidate ();
901 wcmd "reinit" [`i angle; `b proportional];
904 let optentry text key =
905 let btos b = if b then "on" else "off" in
906 let c = Char.unsafe_chr key in
907 match c with
908 | 's' ->
909 let ondone s =
910 try conf.scrollincr <- int_of_string s with exc ->
911 state.text <- Printf.sprintf "bad integer `%s': %s"
912 s (Printexc.to_string exc)
914 TEswitch ('#', "", None, intentry, ondone)
916 | 'R' ->
917 let ondone s =
918 match try
919 Some (int_of_string s)
920 with exc ->
921 state.text <- Printf.sprintf "bad integer `%s': %s"
922 s (Printexc.to_string exc);
923 None
924 with
925 | Some angle -> reinit angle conf.proportional
926 | None -> ()
928 TEswitch ('^', "", None, intentry, ondone)
930 | 'i' ->
931 conf.icase <- not conf.icase;
932 TEdone ("case insensitive search " ^ (btos conf.icase))
934 | 'p' ->
935 conf.preload <- not conf.preload;
936 gotoy state.y;
937 TEdone ("preload " ^ (btos conf.preload))
939 | 'v' ->
940 conf.verbose <- not conf.verbose;
941 TEdone ("verbose " ^ (btos conf.verbose))
943 | 'h' ->
944 conf.maxhfit <- not conf.maxhfit;
945 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
946 TEdone ("maxhfit " ^ (btos conf.maxhfit))
948 | 'c' ->
949 conf.crophack <- not conf.crophack;
950 TEdone ("crophack " ^ btos conf.crophack)
952 | 'a' ->
953 conf.showall <- not conf.showall;
954 TEdone ("showall " ^ btos conf.showall)
956 | 'f' ->
957 conf.underinfo <- not conf.underinfo;
958 TEdone ("underinfo " ^ btos conf.underinfo)
960 | 'P' ->
961 conf.savebmarks <- not conf.savebmarks;
962 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
964 | 'S' ->
965 let ondone s =
967 conf.interpagespace <- int_of_string s;
968 let rely = yratio state.y in
969 state.maxy <- calcheight ();
970 gotoy (truncate (float state.maxy *. rely));
971 with exc ->
972 state.text <- Printf.sprintf "bad integer `%s': %s"
973 s (Printexc.to_string exc)
975 TEswitch ('%', "", None, intentry, ondone)
977 | 'l' ->
978 reinit conf.angle (not conf.proportional);
979 TEdone ("proprortional display " ^ btos conf.proportional)
981 | _ ->
982 state.text <- Printf.sprintf "bad option %d `%c'" key c;
983 TEstop
986 let maxoutlinerows () = (conf.winh - 31) / 16;;
988 let enterselector allowdel outlines errmsg msg =
989 if Array.length outlines = 0
990 then (
991 showtext ' ' errmsg;
993 else (
994 state.text <- msg;
995 Glut.setCursor Glut.CURSOR_INHERIT;
996 let pageno =
997 match state.layout with
998 | [] -> -1
999 | {pageno=pageno} :: rest -> pageno
1001 let active =
1002 let rec loop n =
1003 if n = Array.length outlines
1004 then 0
1005 else
1006 let (_, _, outlinepageno, _) = outlines.(n) in
1007 if outlinepageno >= pageno then n else loop (n+1)
1009 loop 0
1011 state.outline <-
1012 Some (allowdel, active,
1013 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1014 Glut.postRedisplay ();
1018 let enteroutlinemode () =
1019 let outlines, msg =
1020 match state.outlines with
1021 | Oarray a -> a, ""
1022 | Olist l ->
1023 let a = Array.of_list (List.rev l) in
1024 state.outlines <- Oarray a;
1025 a, ""
1026 | Onarrow (pat, a, b) ->
1027 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1029 enterselector false outlines "Document has no outline" msg;
1032 let enterbookmarkmode () =
1033 let bookmarks = Array.of_list state.bookmarks in
1034 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1037 let quickbookmark ?title () =
1038 match state.layout with
1039 | [] -> ()
1040 | l :: _ ->
1041 let title =
1042 match title with
1043 | None ->
1044 let sec = Unix.gettimeofday () in
1045 let tm = Unix.localtime sec in
1046 Printf.sprintf "Quick (page %d) visited (%d/%d/%d %d:%d)"
1047 (l.pageno+1)
1048 tm.Unix.tm_mday
1049 tm.Unix.tm_mon
1050 (tm.Unix.tm_year + 1900)
1051 tm.Unix.tm_hour
1052 tm.Unix.tm_min
1053 | Some title -> title
1055 state.bookmarks <-
1056 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1059 let doreshape w h =
1060 state.fullscreen <- None;
1061 Glut.reshapeWindow w h;
1064 let writeopen path password =
1065 writecmd state.csock
1066 ("open " ^ path ^ "\000"
1067 ^ state.password ^ "\000"
1068 ^ string_of_int conf.angle ^ " "
1069 ^ (if conf.proportional then "1" else "0"))
1073 let opendoc path password =
1074 invalidate ();
1075 state.path <- path;
1076 state.password <- password;
1077 Hashtbl.clear state.pagemap;
1079 writeopen path password;
1080 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1081 wcmd "geometry" [`i state.w; `i conf.winh];
1084 let viewkeyboard ~key ~x ~y =
1085 let enttext te =
1086 state.textentry <- te;
1087 state.text <- "";
1088 enttext ();
1089 Glut.postRedisplay ()
1091 match state.textentry with
1092 | None ->
1093 let c = Char.chr key in
1094 begin match c with
1095 | '\027' | 'q' ->
1096 exit 0
1098 | '\008' ->
1099 let y = getnav () in
1100 gotoy_and_clear_text y
1102 | 'o' ->
1103 enteroutlinemode ()
1105 | 'u' ->
1106 state.rects <- [];
1107 state.text <- "";
1108 Glut.postRedisplay ()
1110 | '/' | '?' ->
1111 let ondone isforw s =
1112 cbput state.hists.pat s;
1113 cbrfollowlen state.hists.pat;
1114 state.searchpattern <- s;
1115 search s isforw
1117 enttext (Some (c, "", Some (onhist state.hists.pat),
1118 textentry, ondone (c ='/')))
1120 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1121 conf.zoom <- min 2.2 (conf.zoom +. 0.1);
1122 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1123 reshape conf.winw conf.winh
1125 | '+' ->
1126 let ondone s =
1127 let n =
1128 try int_of_string s with exc ->
1129 state.text <- Printf.sprintf "bad integer `%s': %s"
1130 s (Printexc.to_string exc);
1131 max_int
1133 if n != max_int
1134 then (
1135 conf.pagebias <- n;
1136 state.text <- "page bias is now " ^ string_of_int n;
1139 enttext (Some ('+', "", None, intentry, ondone))
1141 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1142 conf.zoom <- max 0.1 (conf.zoom -. 0.1);
1143 if conf.zoom <= 1.0 then state.x <- 0;
1144 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1145 reshape conf.winw conf.winh;
1147 | '-' ->
1148 let ondone msg =
1149 state.text <- msg;
1151 enttext (Some ('-', "", None, optentry, ondone))
1153 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1154 state.x <- 0;
1155 conf.zoom <- 1.0;
1156 state.text <- "zoom is 100%";
1157 reshape conf.winw conf.winh
1159 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1160 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1161 if zoom < 1.0
1162 then (
1163 conf.zoom <- zoom;
1164 state.x <- 0;
1165 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1166 reshape conf.winw conf.winh;
1169 | '0' .. '9' ->
1170 let ondone s =
1171 let n =
1172 try int_of_string s with exc ->
1173 state.text <- Printf.sprintf "bad integer `%s': %s"
1174 s (Printexc.to_string exc);
1177 if n >= 0
1178 then (
1179 addnav ();
1180 cbput state.hists.pag (string_of_int n);
1181 cbrfollowlen state.hists.pag;
1182 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1185 let pageentry text key =
1186 match Char.unsafe_chr key with
1187 | 'g' -> TEdone text
1188 | _ -> intentry text key
1190 let text = "x" in text.[0] <- c;
1191 enttext (Some (':', text, Some (onhist state.hists.pag),
1192 pageentry, ondone))
1194 | 'b' ->
1195 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1196 reshape conf.winw conf.winh;
1198 | 'l' ->
1199 conf.hlinks <- not conf.hlinks;
1200 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1201 Glut.postRedisplay ()
1203 | 'a' ->
1204 conf.autoscroll <- not conf.autoscroll
1206 | 'P' ->
1207 conf.presentation <- not conf.presentation;
1208 showtext ' ' ("presentation mode " ^
1209 if conf.presentation then "on" else "off");
1210 represent ()
1212 | 'f' ->
1213 begin match state.fullscreen with
1214 | None ->
1215 state.fullscreen <- Some (conf.winw, conf.winh);
1216 Glut.fullScreen ()
1217 | Some (w, h) ->
1218 state.fullscreen <- None;
1219 doreshape w h
1222 | 'g' ->
1223 gotoy_and_clear_text 0
1225 | 'n' ->
1226 search state.searchpattern true
1228 | 'p' | 'N' ->
1229 search state.searchpattern false
1231 | 't' ->
1232 begin match state.layout with
1233 | [] -> ()
1234 | l :: _ ->
1235 gotoy_and_clear_text (getpagey l.pageno)
1238 | ' ' ->
1239 begin match List.rev state.layout with
1240 | [] -> ()
1241 | l :: _ ->
1242 let pageno = min (l.pageno+1) (state.pagecount-1) in
1243 gotoy_and_clear_text (getpagey pageno)
1246 | '\127' ->
1247 begin match state.layout with
1248 | [] -> ()
1249 | l :: _ ->
1250 let pageno = max 0 (l.pageno-1) in
1251 gotoy_and_clear_text (getpagey pageno)
1254 | '=' ->
1255 let f (fn, ln) l =
1256 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1258 let fn, ln = List.fold_left f (-1, -1) state.layout in
1259 let s =
1260 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1261 let percent =
1262 if maxy <= 0
1263 then 100.
1264 else (100. *. (float state.y /. float maxy)) in
1265 if fn = ln
1266 then
1267 Printf.sprintf "Page %d of %d %.2f%%"
1268 (fn+1) state.pagecount percent
1269 else
1270 Printf.sprintf
1271 "Pages %d-%d of %d %.2f%%"
1272 (fn+1) (ln+1) state.pagecount percent
1274 showtext ' ' s;
1276 | 'w' ->
1277 begin match state.layout with
1278 | [] -> ()
1279 | l :: _ ->
1280 doreshape (l.pagew + conf.scrollw) l.pageh;
1281 Glut.postRedisplay ();
1284 | '\'' ->
1285 enterbookmarkmode ()
1287 | 'm' ->
1288 let ondone s =
1289 match state.layout with
1290 | l :: _ ->
1291 state.bookmarks <-
1292 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1293 :: state.bookmarks
1294 | _ -> ()
1296 enttext (Some ('~', "", None, textentry, ondone))
1298 | '~' ->
1299 quickbookmark ();
1300 showtext ' ' "Quick bookmark added";
1302 | 'z' ->
1303 begin match state.layout with
1304 | l :: _ ->
1305 let rect = getpdimrect l.pagedimno in
1306 let w, h =
1307 if conf.crophack
1308 then
1309 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1310 truncate (1.2 *. (rect.(3) -. rect.(0))))
1311 else
1312 (truncate (rect.(1) -. rect.(0)),
1313 truncate (rect.(3) -. rect.(0)))
1315 if w != 0 && h != 0
1316 then
1317 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1319 Glut.postRedisplay ();
1321 | [] -> ()
1324 | '<' | '>' ->
1325 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1327 | '[' | ']' ->
1328 state.colorscale <-
1329 max 0.0
1330 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1331 Glut.postRedisplay ()
1333 | 'k' -> gotoy (clamp (-conf.scrollincr))
1334 | 'j' -> gotoy (clamp conf.scrollincr)
1336 | 'r' -> opendoc state.path state.password
1338 | _ ->
1339 vlog "huh? %d %c" key (Char.chr key);
1342 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1343 let len = String.length text in
1344 if len = 0
1345 then (
1346 state.textentry <- None;
1347 Glut.postRedisplay ();
1349 else (
1350 let s = String.sub text 0 (len - 1) in
1351 enttext (Some (c, s, onhist, onkey, ondone))
1354 | Some (c, text, onhist, onkey, ondone) ->
1355 begin match Char.unsafe_chr key with
1356 | '\r' | '\n' ->
1357 ondone text;
1358 state.textentry <- None;
1359 Glut.postRedisplay ()
1361 | '\027' ->
1362 state.textentry <- None;
1363 Glut.postRedisplay ()
1365 | _ ->
1366 begin match onkey text key with
1367 | TEdone text ->
1368 state.textentry <- None;
1369 ondone text;
1370 Glut.postRedisplay ()
1372 | TEcont text ->
1373 enttext (Some (c, text, onhist, onkey, ondone));
1375 | TEstop ->
1376 state.textentry <- None;
1377 Glut.postRedisplay ()
1379 | TEswitch te ->
1380 state.textentry <- Some te;
1381 Glut.postRedisplay ()
1382 end;
1383 end;
1386 let narrow outlines pattern =
1387 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1388 match reopt with
1389 | None -> None
1390 | Some re ->
1391 let rec fold accu n =
1392 if n = -1
1393 then accu
1394 else
1395 let (s, _, _, _) as o = outlines.(n) in
1396 let accu =
1397 if (try ignore (Str.search_forward re s 0); true
1398 with Not_found -> false)
1399 then (o :: accu)
1400 else accu
1402 fold accu (n-1)
1404 let matched = fold [] (Array.length outlines - 1) in
1405 if matched = [] then None else Some (Array.of_list matched)
1408 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1409 let search active pattern incr =
1410 let dosearch re =
1411 let rec loop n =
1412 if n = Array.length outlines || n = -1
1413 then None
1414 else
1415 let (s, _, _, _) = outlines.(n) in
1417 (try ignore (Str.search_forward re s 0); true
1418 with Not_found -> false)
1419 then Some n
1420 else loop (n + incr)
1422 loop active
1425 let re = Str.regexp_case_fold pattern in
1426 dosearch re
1427 with Failure s ->
1428 state.text <- s;
1429 None
1431 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1432 match key with
1433 | 27 ->
1434 if String.length qsearch = 0
1435 then (
1436 state.text <- "";
1437 state.outline <- None;
1438 Glut.postRedisplay ();
1440 else (
1441 state.text <- "";
1442 state.outline <- Some (allowdel, active, first, outlines, "");
1443 Glut.postRedisplay ();
1446 | 18 | 19 ->
1447 let incr = if key = 18 then -1 else 1 in
1448 let active, first =
1449 match search (active + incr) qsearch incr with
1450 | None ->
1451 state.text <- qsearch ^ " [not found]";
1452 active, first
1453 | Some active ->
1454 state.text <- qsearch;
1455 active, firstof active
1457 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1458 Glut.postRedisplay ();
1460 | 8 ->
1461 let len = String.length qsearch in
1462 if len = 0
1463 then ()
1464 else (
1465 if len = 1
1466 then (
1467 state.text <- "";
1468 state.outline <- Some (allowdel, active, first, outlines, "");
1470 else
1471 let qsearch = String.sub qsearch 0 (len - 1) in
1472 let active, first =
1473 match search active qsearch ~-1 with
1474 | None ->
1475 state.text <- qsearch ^ " [not found]";
1476 active, first
1477 | Some active ->
1478 state.text <- qsearch;
1479 active, firstof active
1481 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1483 Glut.postRedisplay ()
1485 | 13 ->
1486 if active < Array.length outlines
1487 then (
1488 let (_, _, n, t) = outlines.(active) in
1489 gotopage n t;
1491 state.text <- "";
1492 if allowdel then state.bookmarks <- Array.to_list outlines;
1493 state.outline <- None;
1494 Glut.postRedisplay ();
1496 | _ when key >= 32 && key < 127 ->
1497 let pattern = addchar qsearch (Char.chr key) in
1498 let active, first =
1499 match search active pattern 1 with
1500 | None ->
1501 state.text <- pattern ^ " [not found]";
1502 active, first
1503 | Some active ->
1504 state.text <- pattern;
1505 active, firstof active
1507 state.outline <- Some (allowdel, active, first, outlines, pattern);
1508 Glut.postRedisplay ()
1510 | 14 when not allowdel -> (* ctrl-n *)
1511 if String.length qsearch > 0
1512 then (
1513 let optoutlines = narrow outlines qsearch in
1514 begin match optoutlines with
1515 | None -> state.text <- "can't narrow"
1516 | Some outlines ->
1517 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1518 match state.outlines with
1519 | Olist l -> ()
1520 | Oarray a ->
1521 state.outlines <- Onarrow (qsearch, outlines, a)
1522 | Onarrow (pat, a, b) ->
1523 state.outlines <- Onarrow (qsearch, outlines, b)
1524 end;
1526 Glut.postRedisplay ()
1528 | 21 when not allowdel -> (* ctrl-u *)
1529 let outline =
1530 match state.outlines with
1531 | Oarray a -> a
1532 | Olist l ->
1533 let a = Array.of_list (List.rev l) in
1534 state.outlines <- Oarray a;
1536 | Onarrow (pat, a, b) ->
1537 state.outlines <- Oarray b;
1538 state.text <- "";
1541 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1542 Glut.postRedisplay ()
1544 | 12 ->
1545 state.outline <-
1546 Some (allowdel, active, firstof active, outlines, qsearch);
1547 Glut.postRedisplay ()
1549 | 127 when allowdel ->
1550 let len = Array.length outlines - 1 in
1551 if len = 0
1552 then (
1553 state.outline <- None;
1554 state.bookmarks <- [];
1556 else (
1557 let bookmarks = Array.init len
1558 (fun i ->
1559 let i = if i >= active then i + 1 else i in
1560 outlines.(i)
1563 state.outline <-
1564 Some (allowdel,
1565 min active (len-1),
1566 min first (len-1),
1567 bookmarks, qsearch)
1570 Glut.postRedisplay ()
1572 | _ -> log "unknown key %d" key
1575 let keyboard ~key ~x ~y =
1576 if key = 7
1577 then
1578 wcmd "interrupt" []
1579 else
1580 match state.outline with
1581 | None -> viewkeyboard ~key ~x ~y
1582 | Some outline -> outlinekeyboard ~key ~x ~y outline
1585 let special ~key ~x ~y =
1586 match state.outline with
1587 | None ->
1588 begin match state.textentry with
1589 | None ->
1590 let y =
1591 match key with
1592 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1593 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1594 | Glut.KEY_DOWN -> clamp conf.scrollincr
1595 | Glut.KEY_PAGE_UP ->
1596 if Glut.getModifiers () land Glut.active_ctrl != 0
1597 then
1598 match state.layout with
1599 | [] -> state.y
1600 | l :: _ -> state.y - l.pagey
1601 else
1602 clamp (-conf.winh)
1603 | Glut.KEY_PAGE_DOWN ->
1604 if Glut.getModifiers () land Glut.active_ctrl != 0
1605 then
1606 match List.rev state.layout with
1607 | [] -> state.y
1608 | l :: _ -> getpagey l.pageno
1609 else
1610 clamp conf.winh
1611 | Glut.KEY_HOME -> addnav (); 0
1612 | Glut.KEY_END ->
1613 addnav ();
1614 state.maxy - (if conf.maxhfit then conf.winh else 0)
1616 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1617 state.x <- state.x - 10;
1618 state.y
1619 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1620 state.x <- state.x + 10;
1621 state.y
1623 | _ -> state.y
1625 gotoy_and_clear_text y
1627 | Some (c, s, Some onhist, onkey, ondone) ->
1628 let s =
1629 match key with
1630 | Glut.KEY_UP -> onhist HCprev
1631 | Glut.KEY_DOWN -> onhist HCnext
1632 | Glut.KEY_HOME -> onhist HCfirst
1633 | Glut.KEY_END -> onhist HClast
1634 | _ -> state.text
1636 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1637 Glut.postRedisplay ()
1639 | _ -> ()
1642 | Some (allowdel, active, first, outlines, qsearch) ->
1643 let maxrows = maxoutlinerows () in
1644 let calcfirst first active =
1645 if active > first
1646 then
1647 let rows = active - first in
1648 if rows > maxrows then active - maxrows else first
1649 else active
1651 let navigate incr =
1652 let active = active + incr in
1653 let active = max 0 (min active (Array.length outlines - 1)) in
1654 let first = calcfirst first active in
1655 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1656 Glut.postRedisplay ()
1658 let updownlevel incr =
1659 let len = Array.length outlines in
1660 let (_, curlevel, _, _) = outlines.(active) in
1661 let rec flow i =
1662 if i = len then i-1 else if i = -1 then 0 else
1663 let (_, l, _, _) = outlines.(i) in
1664 if l != curlevel then i else flow (i+incr)
1666 let active = flow active in
1667 let first = calcfirst first active in
1668 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1669 Glut.postRedisplay ()
1671 match key with
1672 | Glut.KEY_UP -> navigate ~-1
1673 | Glut.KEY_DOWN -> navigate 1
1674 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1675 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1677 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1678 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1680 | Glut.KEY_HOME ->
1681 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1682 Glut.postRedisplay ()
1684 | Glut.KEY_END ->
1685 let active = Array.length outlines - 1 in
1686 let first = max 0 (active - maxrows) in
1687 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1688 Glut.postRedisplay ()
1690 | _ -> ()
1693 let drawplaceholder l =
1694 GlDraw.color (scalecolor 1.0);
1695 GlDraw.rect
1696 (float l.pagex, float l.pagedispy)
1697 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1699 let x = float l.pagex
1700 and y = float (l.pagedispy + 13) in
1701 let font = Glut.BITMAP_8_BY_13 in
1702 GlDraw.color (0.0, 0.0, 0.0);
1703 GlPix.raster_pos ~x ~y ();
1704 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1705 ("Loading " ^ string_of_int (l.pageno + 1));
1708 let now () = Unix.gettimeofday ();;
1710 let drawpage i l =
1711 begin match getopaque l.pageno with
1712 | Some opaque when validopaque opaque ->
1713 if state.textentry = None
1714 then GlDraw.color (scalecolor 1.0)
1715 else GlDraw.color (scalecolor 0.4);
1716 let a = now () in
1717 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1718 opaque;
1719 let b = now () in
1720 let d = b-.a in
1721 vlog "draw %d %f sec" l.pageno d;
1723 | _ ->
1724 drawplaceholder l;
1725 end;
1726 l.pagedispy + l.pagevh;
1729 let scrollph y =
1730 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1731 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1732 let sh = float conf.winh /. sh in
1733 let sh = max sh (float conf.scrollh) in
1735 let percent =
1736 if state.y = state.maxy
1737 then 1.0
1738 else float y /. float maxy
1740 let position = (float conf.winh -. sh) *. percent in
1742 let position =
1743 if position +. sh > float conf.winh
1744 then float conf.winh -. sh
1745 else position
1747 position, sh;
1750 let scrollindicator () =
1751 GlDraw.color (0.64 , 0.64, 0.64);
1752 GlDraw.rect
1753 (float (conf.winw - conf.scrollw), 0.)
1754 (float conf.winw, float conf.winh)
1756 GlDraw.color (0.0, 0.0, 0.0);
1758 let position, sh = scrollph state.y in
1759 GlDraw.rect
1760 (float (conf.winw - conf.scrollw), position)
1761 (float conf.winw, position +. sh)
1765 let showsel margin =
1766 match state.mstate with
1767 | Mnone | Mscroll _ | Mpan _ ->
1770 | Msel ((x0, y0), (x1, y1)) ->
1771 let rec loop = function
1772 | l :: ls ->
1773 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1774 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1775 then
1776 match getopaque l.pageno with
1777 | Some opaque when validopaque opaque ->
1778 let oy = -l.pagey + l.pagedispy in
1779 seltext opaque
1780 (x0 - margin - state.x, y0,
1781 x1 - margin - state.x, y1) oy;
1783 | _ -> ()
1784 else loop ls
1785 | [] -> ()
1787 loop state.layout
1790 let showrects () =
1791 let panx = float state.x in
1792 Gl.enable `blend;
1793 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1794 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1795 List.iter
1796 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1797 List.iter (fun l ->
1798 if l.pageno = pageno
1799 then (
1800 let d = float (l.pagedispy - l.pagey) in
1801 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1802 GlDraw.begins `quads;
1804 GlDraw.vertex2 (x0+.panx, y0+.d);
1805 GlDraw.vertex2 (x1+.panx, y1+.d);
1806 GlDraw.vertex2 (x2+.panx, y2+.d);
1807 GlDraw.vertex2 (x3+.panx, y3+.d);
1809 GlDraw.ends ();
1811 ) state.layout
1812 ) state.rects
1814 Gl.disable `blend;
1817 let showoutline = function
1818 | None -> ()
1819 | Some (allowdel, active, first, outlines, qsearch) ->
1820 Gl.enable `blend;
1821 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1822 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1823 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
1824 Gl.disable `blend;
1826 GlDraw.color (1., 1., 1.);
1827 let font = Glut.BITMAP_9_BY_15 in
1828 let draw_string x y s =
1829 GlPix.raster_pos ~x ~y ();
1830 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1832 let rec loop row =
1833 if row = Array.length outlines || (row - first) * 16 > conf.winh
1834 then ()
1835 else (
1836 let (s, l, _, _) = outlines.(row) in
1837 let y = (row - first) * 16 in
1838 let x = 5 + 15*l in
1839 if row = active
1840 then (
1841 Gl.enable `blend;
1842 GlDraw.polygon_mode `both `line;
1843 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1844 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1845 GlDraw.rect (0., float (y + 1))
1846 (float (conf.winw - 1), float (y + 18));
1847 GlDraw.polygon_mode `both `fill;
1848 Gl.disable `blend;
1849 GlDraw.color (1., 1., 1.);
1851 draw_string (float x) (float (y + 16)) s;
1852 loop (row+1)
1855 loop first
1858 let display () =
1859 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1860 GlDraw.viewport margin 0 state.w conf.winh;
1861 pagematrix ();
1862 GlClear.color (scalecolor 0.5);
1863 GlClear.clear [`color];
1864 if state.x != 0
1865 then (
1866 let x = float state.x in
1867 GlMat.translate ~x ();
1869 if conf.zoom > 1.0
1870 then (
1871 Gl.enable `scissor_test;
1872 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
1874 let _lasty = List.fold_left drawpage 0 (state.layout) in
1875 if conf.zoom > 1.0
1876 then
1877 Gl.disable `scissor_test
1879 if state.x != 0
1880 then (
1881 let x = -.float state.x in
1882 GlMat.translate ~x ();
1884 showrects ();
1885 showsel margin;
1886 GlDraw.viewport 0 0 conf.winw conf.winh;
1887 winmatrix ();
1888 scrollindicator ();
1889 showoutline state.outline;
1890 enttext ();
1891 Glut.swapBuffers ();
1894 let getunder x y =
1895 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1896 let x = x - margin - state.x in
1897 let rec f = function
1898 | l :: rest ->
1899 begin match getopaque l.pageno with
1900 | Some opaque when validopaque opaque ->
1901 let y = y - l.pagedispy in
1902 if y > 0
1903 then
1904 let y = l.pagey + y in
1905 let x = x - l.pagex in
1906 match whatsunder opaque x y with
1907 | Unone -> f rest
1908 | under -> under
1909 else
1910 f rest
1911 | _ ->
1912 f rest
1914 | [] -> Unone
1916 f state.layout
1919 let mouse ~button ~bstate ~x ~y =
1920 match button with
1921 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1922 let incr =
1923 if n = 3
1924 then
1925 -conf.scrollincr
1926 else
1927 conf.scrollincr
1929 let incr = incr * 2 in
1930 let y = clamp incr in
1931 gotoy_and_clear_text y
1933 | Glut.LEFT_BUTTON when state.outline = None
1934 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
1935 if bstate = Glut.DOWN
1936 then (
1937 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1938 state.mstate <- Mpan (x, y)
1940 else
1941 state.mstate <- Mnone
1943 | Glut.LEFT_BUTTON
1944 when state.outline = None && x > conf.winw - conf.scrollw ->
1945 if bstate = Glut.DOWN
1946 then
1947 let position, sh = scrollph state.y in
1948 if y > truncate position && y < truncate (position +. sh)
1949 then
1950 state.mstate <- Mscroll
1951 else
1952 let percent = float y /. float conf.winh in
1953 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
1954 gotoy desty;
1955 state.mstate <- Mscroll
1956 else
1957 state.mstate <- Mnone
1959 | Glut.LEFT_BUTTON when state.outline = None ->
1960 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1961 begin match dest with
1962 | Ulinkgoto (pageno, top) ->
1963 if pageno >= 0
1964 then
1965 gotopage1 pageno top
1967 | Ulinkuri s ->
1968 print_endline s
1970 | Unone when bstate = Glut.DOWN ->
1971 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1972 state.mstate <- Mpan (x, y);
1974 | Unone | Utext _ ->
1975 if bstate = Glut.DOWN
1976 then (
1977 if conf.angle mod 360 = 0
1978 then (
1979 state.mstate <- Msel ((x, y), (x, y));
1980 Glut.postRedisplay ()
1983 else (
1984 match state.mstate with
1985 | Mnone -> ()
1987 | Mscroll ->
1988 state.mstate <- Mnone
1990 | Mpan _ ->
1991 Glut.setCursor Glut.CURSOR_INHERIT;
1992 state.mstate <- Mnone
1994 | Msel ((x0, y0), (x1, y1)) ->
1995 let f l =
1996 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1997 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1998 then
1999 match getopaque l.pageno with
2000 | Some opaque when validopaque opaque ->
2001 copysel opaque
2002 | _ -> ()
2004 List.iter f state.layout;
2005 copysel ""; (* ugly *)
2006 Glut.setCursor Glut.CURSOR_INHERIT;
2007 state.mstate <- Mnone;
2011 | _ ->
2014 let mouse ~button ~state ~x ~y = mouse button state x y;;
2016 let motion ~x ~y =
2017 if state.outline = None
2018 then
2019 match state.mstate with
2020 | Mnone -> ()
2022 | Mpan (x0, y0) ->
2023 let dx = x - x0
2024 and dy = y0 - y in
2025 state.mstate <- Mpan (x, y);
2026 if conf.zoom > 1.0 then state.x <- state.x + dx;
2027 let y = clamp dy in
2028 gotoy_and_clear_text y
2030 | Msel (a, _) ->
2031 state.mstate <- Msel (a, (x, y));
2032 Glut.postRedisplay ()
2034 | Mscroll ->
2035 let y = min conf.winh (max 0 y) in
2036 let percent = float y /. float conf.winh in
2037 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2038 gotoy_and_clear_text y
2041 let pmotion ~x ~y =
2042 if state.outline = None
2043 then
2044 match state.mstate with
2045 | Mnone ->
2046 begin match getunder x y with
2047 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2048 | Ulinkuri uri ->
2049 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2050 Glut.setCursor Glut.CURSOR_INFO
2051 | Ulinkgoto (page, y) ->
2052 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
2053 Glut.setCursor Glut.CURSOR_INFO
2054 | Utext s ->
2055 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2056 Glut.setCursor Glut.CURSOR_TEXT
2059 | Mpan _ | Msel _ | Mscroll ->
2063 module State =
2064 struct
2065 open Parser
2067 let home =
2069 match Sys.os_type with
2070 | "Win32" -> Sys.getenv "HOMEPATH"
2071 | _ -> Sys.getenv "HOME"
2072 with exn ->
2073 prerr_endline
2074 ("Can not determine home directory location: " ^
2075 Printexc.to_string exn);
2079 let config_of c attrs =
2080 let apply c k v =
2082 match k with
2083 | "scroll-bar-width" -> { c with scrollw = int_of_string v }
2084 | "scroll-handle-height" -> { c with scrollh = int_of_string v }
2085 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2086 | "preload" -> { c with preload = bool_of_string v }
2087 | "page-bias" -> { c with pagebias = int_of_string v }
2088 | "scroll-step" -> { c with scrollincr = int_of_string v }
2089 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2090 | "crop-hack" -> { c with crophack = bool_of_string v }
2091 | "throttle" -> { c with showall = bool_of_string v }
2092 | "highlight-links" -> { c with hlinks = bool_of_string v }
2093 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2094 | "vertical-margin" -> { c with interpagespace = int_of_string v }
2095 | "zoom" ->
2096 let zoom = float_of_string v /. 100. in
2097 let zoom = max 0.1 (min 2.2 zoom) in
2098 { c with zoom = zoom }
2099 | "presentation" -> { c with presentation = bool_of_string v }
2100 | "rotation-angle" -> { c with angle = int_of_string v }
2101 | "width" -> { c with winw = int_of_string v }
2102 | "height" -> { c with winh = int_of_string v }
2103 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2104 | "proportional-display" -> { c with proportional = bool_of_string v }
2105 | _ -> c
2106 with exn ->
2107 prerr_endline ("Error processing attribute (`" ^
2108 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2111 let rec fold c = function
2112 | [] -> c
2113 | (k, v) :: rest ->
2114 let c = apply c k v in
2115 fold c rest
2117 fold c attrs;
2120 let bookmark_of attrs =
2121 let rec fold title page rely = function
2122 | ("title", v) :: rest -> fold v page rely rest
2123 | ("page", v) :: rest -> fold title v rely rest
2124 | ("rely", v) :: rest -> fold title page v rest
2125 | _ :: rest -> fold title page rely rest
2126 | [] -> title, page, rely
2128 fold "invalid" "0" "0" attrs
2131 let setconf dst src =
2132 dst.scrollw <- src.scrollw;
2133 dst.scrollh <- src.scrollh;
2134 dst.icase <- src.icase;
2135 dst.preload <- src.preload;
2136 dst.pagebias <- src.pagebias;
2137 dst.verbose <- src.verbose;
2138 dst.scrollincr <- src.scrollincr;
2139 dst.maxhfit <- src.maxhfit;
2140 dst.crophack <- src.crophack;
2141 dst.autoscroll <- src.autoscroll;
2142 dst.showall <- src.showall;
2143 dst.hlinks <- src.hlinks;
2144 dst.underinfo <- src.underinfo;
2145 dst.interpagespace <- src.interpagespace;
2146 dst.zoom <- src.zoom;
2147 dst.presentation <- src.presentation;
2148 dst.angle <- src.angle;
2149 dst.winw <- src.winw;
2150 dst.winh <- src.winh;
2151 dst.savebmarks <- src.savebmarks;
2152 dst.proportional <- src.proportional;
2155 let unent s =
2156 let l = String.length s in
2157 let b = Buffer.create l in
2158 unent b s 0 l;
2159 Buffer.contents b;
2162 let get s =
2163 let h = Hashtbl.create 10 in
2164 let dc = { defconf with angle = defconf.angle } in
2165 let rec toplevel v t spos epos =
2166 match t with
2167 | Vdata | Vcdata | Vend -> v
2168 | Vopen ("llppconfig", attrs, closed) ->
2169 if closed
2170 then v
2171 else { v with f = llppconfig }
2172 | Vopen _ ->
2173 error "unexpected subelement at top level" s spos
2174 | Vclose tag -> error "unexpected close at toplevel" s spos
2176 and llppconfig v t spos epos =
2177 match t with
2178 | Vdata | Vcdata | Vend -> v
2179 | Vopen ("defaults", attrs, closed) ->
2180 let c = config_of dc attrs in
2181 setconf dc c;
2182 if closed
2183 then v
2184 else { v with f = skip "defaults" (fun () -> v) }
2186 | Vopen ("doc", attrs, closed) ->
2187 let pathent =
2189 List.assoc "path" attrs
2190 with Not_found -> error "doc is missing path attribute" s spos
2192 let path = unent pathent in
2193 let c = config_of dc attrs in
2194 let y =
2196 float_of_string (List.assoc "rely" attrs)
2197 with
2198 | Not_found -> 0.0
2199 | exn ->
2200 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2203 let x =
2205 int_of_string (List.assoc "pan" attrs)
2206 with
2207 | Not_found -> 0
2208 | exn ->
2209 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2212 if closed
2213 then (Hashtbl.add h path (c, [], x, y); v)
2214 else { v with f = doc path x y c [] }
2216 | Vopen (tag, _, closed) ->
2217 error "unexpected subelement in llppconfig" s spos
2219 | Vclose "llppconfig" -> { v with f = toplevel }
2220 | Vclose tag -> error "unexpected close in llppconfig" s spos
2222 and doc path x y c bookmarks v t spos epos =
2223 match t with
2224 | Vdata | Vcdata -> v
2225 | Vend -> error "unexpected end of input in doc" s spos
2226 | Vopen ("bookmarks", attrs, closed) ->
2227 { v with f = pbookmarks path x y c bookmarks }
2229 | Vopen (tag, _, _) ->
2230 error "unexpected subelement in doc" s spos
2232 | Vclose "doc" ->
2233 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2234 { v with f = llppconfig }
2236 | Vclose tag -> error "unexpected close in doc" s spos
2238 and pbookmarks path x y c bookmarks v t spos epos =
2239 match t with
2240 | Vdata | Vcdata -> v
2241 | Vend -> error "unexpected end of input in bookmarks" s spos
2242 | Vopen ("item", attrs, closed) ->
2243 let titleent, spage, srely = bookmark_of attrs in
2244 let page =
2246 int_of_string spage
2247 with exn ->
2248 dolog "Failed to convert page %S to integer: %s"
2249 spage (Printexc.to_string exn);
2252 let rely =
2254 float_of_string srely
2255 with exn ->
2256 dolog "Failed to convert rely %S to real: %s"
2257 srely (Printexc.to_string exn);
2260 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2261 if closed
2262 then { v with f = pbookmarks path x y c bookmarks }
2263 else
2264 let f () = v in
2265 { v with f = skip "item" f }
2267 | Vopen _ ->
2268 error "unexpected subelement in bookmarks" s spos
2270 | Vclose "bookmarks" ->
2271 { v with f = doc path x y c bookmarks }
2273 | Vclose tag -> error "unexpected close in bookmarks" s spos
2275 and skip tag f v t spos epos =
2276 match t with
2277 | Vdata | Vcdata -> v
2278 | Vend ->
2279 error ("unexpected end of input in skipped " ^ tag) s spos
2280 | Vopen (tag', _, closed) ->
2281 if closed
2282 then v
2283 else
2284 let f' () = { v with f = skip tag f } in
2285 { v with f = skip tag' f' }
2286 | Vclose ctag ->
2287 if tag = ctag
2288 then f ()
2289 else error ("unexpected close in skipped " ^ tag) s spos
2292 parse { f = toplevel; accu = () } s;
2293 h, dc;
2296 let do_load f ic =
2298 let len = in_channel_length ic in
2299 let s = String.create len in
2300 really_input ic s 0 len;
2301 f s;
2302 with
2303 | Parse_error (msg, s, pos) ->
2304 let subs = subs s pos in
2305 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2306 failwith ("parse error: " ^ s)
2308 | exn ->
2309 failwith ("config load error: " ^ Printexc.to_string exn)
2312 let path =
2313 let dir =
2315 let dir = Filename.concat home ".config" in
2316 if Sys.is_directory dir then dir else home
2317 with _ -> home
2319 Filename.concat dir "llpp.conf"
2322 let load1 f =
2323 if Sys.file_exists path
2324 then
2325 match
2326 (try Some (open_in_bin path)
2327 with exn ->
2328 prerr_endline
2329 ("Error opening configuation file `" ^ path ^ "': " ^
2330 Printexc.to_string exn);
2331 None
2333 with
2334 | Some ic ->
2335 begin try
2336 f (do_load get ic)
2337 with exn ->
2338 prerr_endline
2339 ("Error loading configuation from `" ^ path ^ "': " ^
2340 Printexc.to_string exn);
2341 end;
2342 close_in ic;
2344 | None -> ()
2345 else
2346 f (Hashtbl.create 0, defconf)
2349 let load () =
2350 let f (h, dc) =
2351 let pc, pb, px, py =
2353 Hashtbl.find h state.path
2354 with Not_found -> dc, [], 0, 0.0
2356 setconf defconf dc;
2357 setconf conf pc;
2358 state.bookmarks <- pb;
2359 state.x <- px;
2360 cbput state.hists.nav py;
2361 cbrfollowlen state.hists.nav;
2363 load1 f
2366 let add_attrs bb always dc c =
2367 let ob s a b =
2368 if always || a != b
2369 then Printf.bprintf bb "\n %s='%b'" s a
2370 and oi s a b =
2371 if always || a != b
2372 then Printf.bprintf bb "\n %s='%d'" s a
2373 and oz s a b =
2374 if always || a <> b
2375 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2377 let w, h =
2378 if always
2379 then dc.winw, dc.winh
2380 else
2381 match state.fullscreen with
2382 | Some wh -> wh
2383 | None -> c.winw, c.winh
2385 oi "width" w dc.winw;
2386 oi "height" h dc.winh;
2387 oi "scroll-bar-width" c.scrollw dc.scrollw;
2388 oi "scroll-handle-height" c.scrollh dc.scrollh;
2389 ob "case-insensitive-search" c.icase dc.icase;
2390 ob "preload" c.preload dc.preload;
2391 oi "page-bias" c.pagebias dc.pagebias;
2392 oi "scroll-step" c.scrollincr dc.scrollincr;
2393 ob "max-height-fit" c.maxhfit dc.maxhfit;
2394 ob "crop-hack" c.crophack dc.crophack;
2395 ob "throttle" c.showall dc.showall;
2396 ob "highlight-links" c.hlinks dc.hlinks;
2397 ob "under-cursor-info" c.underinfo dc.underinfo;
2398 oi "vertical-margin" c.interpagespace dc.interpagespace;
2399 oz "zoom" c.zoom dc.zoom;
2400 ob "presentation" c.presentation dc.presentation;
2401 oi "rotation-angle" c.angle dc.angle;
2402 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2403 ob "proportional-display" c.proportional dc.proportional;
2406 let save () =
2407 let bb = Buffer.create 32768 in
2408 let f (h, dc) =
2409 Buffer.add_string bb "<llppconfig>\n<defaults ";
2410 add_attrs bb true dc dc;
2411 Buffer.add_string bb "/>\n";
2413 let adddoc path x y c bookmarks =
2414 if bookmarks == [] && c = dc && y = 0.0
2415 then ()
2416 else (
2417 Printf.bprintf bb "<doc path='%s'"
2418 (enent path 0 (String.length path));
2420 if y <> 0.0
2421 then Printf.bprintf bb " rely='%f'" y;
2423 if x != 0
2424 then Printf.bprintf bb " pan='%d'" x;
2426 add_attrs bb false dc c;
2428 begin match bookmarks with
2429 | [] -> Buffer.add_string bb "/>\n"
2430 | _ ->
2431 Buffer.add_string bb ">\n<bookmarks>\n";
2432 List.iter (fun (title, _level, page, rely) ->
2433 Printf.bprintf bb
2434 "<item title='%s' page='%d' rely='%f'/>\n"
2435 (enent title 0 (String.length title))
2436 page
2437 rely
2438 ) bookmarks;
2439 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2440 end;
2444 adddoc state.path state.x (yratio state.y) conf
2445 (if conf.savebmarks then state.bookmarks else []);
2447 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2448 if path <> state.path
2449 then
2450 adddoc path x y c bookmarks
2451 ) h;
2452 Buffer.add_string bb "</llppconfig>";
2454 load1 f;
2455 if Buffer.length bb > 0
2456 then
2458 let tmp = path ^ ".tmp" in
2459 let oc = open_out_bin tmp in
2460 Buffer.output_buffer oc bb;
2461 close_out oc;
2462 Sys.rename tmp path;
2463 with exn ->
2464 prerr_endline
2465 ("error while saving configuration: " ^ Printexc.to_string exn)
2467 end;;
2469 let () =
2470 Arg.parse
2471 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2472 (fun s -> state.path <- s)
2473 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2475 let path =
2476 if String.length state.path = 0
2477 then (prerr_endline "filename missing"; exit 1)
2478 else (
2479 if Filename.is_relative state.path
2480 then Filename.concat (Sys.getcwd ()) state.path
2481 else state.path
2484 state.path <- path;
2486 State.load ();
2488 let _ = Glut.init Sys.argv in
2489 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2490 let () = Glut.initWindowSize conf.winw conf.winh in
2491 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2493 let csock, ssock =
2494 if Sys.os_type = "Unix"
2495 then
2496 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2497 else
2498 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2499 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2500 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2501 Unix.bind sock addr;
2502 Unix.listen sock 1;
2503 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2504 Unix.connect csock addr;
2505 let ssock, _ = Unix.accept sock in
2506 Unix.close sock;
2507 let opts sock =
2508 Unix.setsockopt sock Unix.TCP_NODELAY true;
2509 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2511 opts ssock;
2512 opts csock;
2513 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2514 ssock, csock
2517 let () = Glut.displayFunc display in
2518 let () = Glut.reshapeFunc reshape in
2519 let () = Glut.keyboardFunc keyboard in
2520 let () = Glut.specialFunc special in
2521 let () = Glut.idleFunc (Some idle) in
2522 let () = Glut.mouseFunc mouse in
2523 let () = Glut.motionFunc motion in
2524 let () = Glut.passiveMotionFunc pmotion in
2526 init ssock;
2527 state.csock <- csock;
2528 state.ssock <- ssock;
2529 state.text <- "Opening " ^ path;
2530 writeopen state.path state.password;
2532 at_exit State.save;
2534 let rec handlelablglutbug () =
2536 Glut.mainLoop ();
2537 with Glut.BadEnum "key in special_of_int" ->
2538 showtext '!' " LablGlut bug: special key not recognized";
2539 handlelablglutbug ()
2541 handlelablglutbug ();