Formatting
[llpp.git] / main.ml
blob5c7e1ae68c652b075afd62312e33b12eadb1e24c
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 * onkey * ondone)
37 and onkey = string -> int -> te
38 and ondone = string -> unit
39 and onhist = (histcmd -> string) option
40 and histcmd = HCnext | HCprev | HCfirst | HClast | HCcancel
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 cbcap b = Array.length b.store;;
58 let cbput b v =
59 let cap = cbcap b in
60 b.store.(b.wc) <- v;
61 b.wc <- (b.wc + 1) mod cap;
62 b.rc <- b.wc;
63 b.len <- min (b.len + 1) cap;
66 let cbempty b = b.len = 0;;
68 let cbget b dir =
69 if cbempty b
70 then b.store.(0)
71 else
72 let rc = b.rc + dir in
73 let rc = max 0 (min rc (b.len-1)) in
74 b.rc <- rc;
75 b.store.(rc);
78 let cbpeek b =
79 let rc = b.wc - b.len in
80 let rc = if rc < 0 then cbcap b + rc else rc in
81 b.store.(rc);
84 let cbdecr b = b.len <- b.len - 1;;
86 type layout =
87 { pageno : int
88 ; pagedimno : int
89 ; pagew : int
90 ; pageh : int
91 ; pagedispy : int
92 ; pagey : int
93 ; pagevh : int
94 ; pagex : int
98 type conf =
99 { mutable scrollw : int
100 ; mutable scrollh : int
101 ; mutable icase : bool
102 ; mutable preload : bool
103 ; mutable pagebias : int
104 ; mutable verbose : bool
105 ; mutable scrollincr : int
106 ; mutable maxhfit : bool
107 ; mutable crophack : bool
108 ; mutable autoscroll : bool
109 ; mutable showall : bool
110 ; mutable hlinks : bool
111 ; mutable underinfo : bool
112 ; mutable interpagespace : int
113 ; mutable zoom : float
114 ; mutable presentation : bool
115 ; mutable angle : int
116 ; mutable winw : int
117 ; mutable winh : int
118 ; mutable savebmarks : bool
119 ; mutable proportional : bool
120 ; mutable memlimit : int
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
132 and pixmapsize = int
135 type outline = string * int * int * float;;
136 type outlines =
137 | Oarray of outline array
138 | Olist of outline list
139 | Onarrow of string * outline array * outline array
142 type rect = (float * float * float * float * float * float * float * float);;
144 type state =
145 { mutable csock : Unix.file_descr
146 ; mutable ssock : Unix.file_descr
147 ; mutable w : int
148 ; mutable x : int
149 ; mutable y : int
150 ; mutable ty : float
151 ; mutable maxy : int
152 ; mutable layout : layout list
153 ; pagemap :
154 ((pageno * width * angle * proportional), (opaque * pixmapsize)) Hashtbl.t
155 ; mutable pdims : (pageno * width * height * leftx) list
156 ; mutable pagecount : int
157 ; pagecache : string circbuf
158 ; mutable rendering : bool
159 ; mutable mstate : mstate
160 ; mutable searchpattern : string
161 ; mutable rects : (pageno * recttype * rect) list
162 ; mutable rects1 : (pageno * recttype * rect) list
163 ; mutable text : string
164 ; mutable fullscreen : (width * height) option
165 ; mutable textentry : textentry option
166 ; mutable outlines : outlines
167 ; mutable outline : (bool * int * int * outline array * string) option
168 ; mutable bookmarks : outline list
169 ; mutable path : string
170 ; mutable password : string
171 ; mutable invalidated : int
172 ; mutable colorscale : float
173 ; mutable memused : int
174 ; hists : hists
176 and hists =
177 { pat : string circbuf
178 ; pag : string circbuf
179 ; nav : float circbuf
183 let defconf =
184 { scrollw = 7
185 ; scrollh = 12
186 ; icase = true
187 ; preload = true
188 ; pagebias = 0
189 ; verbose = false
190 ; scrollincr = 24
191 ; maxhfit = true
192 ; crophack = false
193 ; autoscroll = false
194 ; showall = false
195 ; hlinks = false
196 ; underinfo = false
197 ; interpagespace = 2
198 ; zoom = 1.0
199 ; presentation = false
200 ; angle = 0
201 ; winw = 900
202 ; winh = 900
203 ; savebmarks = true
204 ; proportional = true
205 ; memlimit = 32*1024*1024
209 let conf = { defconf with angle = defconf.angle };;
211 let state =
212 { csock = Unix.stdin
213 ; ssock = Unix.stdin
214 ; w = 0
215 ; y = 0
216 ; x = 0
217 ; ty = 0.0
218 ; layout = []
219 ; maxy = max_int
220 ; pagemap = Hashtbl.create 10
221 ; pagecache = cbnew 100 ""
222 ; pdims = []
223 ; pagecount = 0
224 ; rendering = false
225 ; mstate = Mnone
226 ; rects = []
227 ; rects1 = []
228 ; text = ""
229 ; fullscreen = None
230 ; textentry = None
231 ; searchpattern = ""
232 ; outlines = Olist []
233 ; outline = None
234 ; bookmarks = []
235 ; path = ""
236 ; password = ""
237 ; invalidated = 0
238 ; hists =
239 { nav = cbnew 100 0.0
240 ; pat = cbnew 20 ""
241 ; pag = cbnew 10 ""
243 ; colorscale = 1.0
244 ; memused = 0
248 let vlog fmt =
249 if conf.verbose
250 then
251 Printf.kprintf prerr_endline fmt
252 else
253 Printf.kprintf ignore fmt
256 let writecmd fd s =
257 let len = String.length s in
258 let n = 4 + len in
259 let b = Buffer.create n in
260 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
261 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
262 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
263 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
264 Buffer.add_string b s;
265 let s' = Buffer.contents b in
266 let n' = Unix.write fd s' 0 n in
267 if n' != n then failwith "write failed";
270 let readcmd fd =
271 let s = "xxxx" in
272 let n = Unix.read fd s 0 4 in
273 if n != 4 then failwith "incomplete read(len)";
274 let len = 0
275 lor (Char.code s.[0] lsl 24)
276 lor (Char.code s.[1] lsl 16)
277 lor (Char.code s.[2] lsl 8)
278 lor (Char.code s.[3] lsl 0)
280 let s = String.create len in
281 let n = Unix.read fd s 0 len in
282 if n != len then failwith "incomplete read(data)";
286 let yratio y =
287 if y = state.maxy
288 then 1.0
289 else float y /. float state.maxy
292 let makecmd s l =
293 let b = Buffer.create 10 in
294 Buffer.add_string b s;
295 let rec combine = function
296 | [] -> b
297 | x :: xs ->
298 Buffer.add_char b ' ';
299 let s =
300 match x with
301 | `b b -> if b then "1" else "0"
302 | `s s -> s
303 | `i i -> string_of_int i
304 | `f f -> string_of_float f
305 | `I f -> string_of_int (truncate f)
307 Buffer.add_string b s;
308 combine xs;
310 combine l;
313 let wcmd s l =
314 let cmd = Buffer.contents (makecmd s l) in
315 writecmd state.csock cmd;
318 let calcips h =
319 if conf.presentation
320 then
321 let d = conf.winh - h in
322 max 0 ((d + 1) / 2)
323 else
324 conf.interpagespace
327 let calcheight () =
328 let rec f pn ph pi fh l =
329 match l with
330 | (n, _, h, _) :: rest ->
331 let ips = calcips h in
332 let fh =
333 if conf.presentation
334 then fh+ips
335 else fh
337 let fh = fh + ((n - pn) * (ph + pi)) in
338 f n h ips fh rest
340 | [] ->
341 let inc =
342 if conf.presentation
343 then 0
344 else -pi
346 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
347 max 0 fh
349 let fh = f 0 0 0 0 state.pdims in
353 let getpageyh pageno =
354 let rec f pn ph pi y l =
355 match l with
356 | (n, _, h, _) :: rest ->
357 let ips = calcips h in
358 if n >= pageno
359 then
360 if conf.presentation && n = pageno
361 then
362 y + (pageno - pn) * (ph + pi) + pi, h
363 else
364 y + (pageno - pn) * (ph + pi), h
365 else
366 let y = y + (if conf.presentation then pi else 0) in
367 let y = y + (n - pn) * (ph + pi) in
368 f n h ips y rest
370 | [] ->
371 y + (pageno - pn) * (ph + pi), ph
373 f 0 0 0 0 state.pdims
376 let getpagey pageno = fst (getpageyh pageno);;
378 let layout y sh =
379 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
380 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
381 match pdims with
382 | (pageno', w, h, x) :: rest when pageno' = pageno ->
383 let ips = calcips h in
384 let yinc = if conf.presentation then ips else 0 in
385 (w, h, ips, x), rest, pdimno + 1, yinc
386 | _ ->
387 prev, pdims, pdimno, 0
389 let dy = dy + yinc in
390 let py = py + yinc in
391 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
392 then
393 accu
394 else
395 let vy = y + dy in
396 if py + h <= vy - yinc
397 then
398 let py = py + h + ips in
399 let dy = max 0 (py - y) in
400 f ~pageno:(pageno+1)
401 ~pdimno
402 ~prev:curr
405 ~pdims:rest
406 ~cacheleft
407 ~accu
408 else
409 let pagey = vy - py in
410 let pagevh = h - pagey in
411 let pagevh = min (sh - dy) pagevh in
412 let off = if yinc > 0 then py - vy else 0 in
413 let py = py + h + ips in
414 let e =
415 { pageno = pageno
416 ; pagedimno = pdimno
417 ; pagew = w
418 ; pageh = h
419 ; pagedispy = dy + off
420 ; pagey = pagey + off
421 ; pagevh = pagevh - off
422 ; pagex = x
425 let accu = e :: accu in
426 f ~pageno:(pageno+1)
427 ~pdimno
428 ~prev:curr
430 ~dy:(dy+pagevh+ips)
431 ~pdims:rest
432 ~cacheleft:(cacheleft-1)
433 ~accu
435 if state.invalidated = 0
436 then (
437 let accu =
439 ~pageno:0
440 ~pdimno:~-1
441 ~prev:(0,0,0,0)
442 ~py:0
443 ~dy:0
444 ~pdims:state.pdims
445 ~cacheleft:(cbcap state.pagecache)
446 ~accu:[]
448 List.rev accu
450 else
454 let clamp incr =
455 let y = state.y + incr in
456 let y = max 0 y in
457 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
461 let getopaque pageno =
462 try Some (Hashtbl.find state.pagemap
463 (pageno + 1, state.w, conf.angle, conf.proportional))
464 with Not_found -> None
467 let cache pageno opaque =
468 Hashtbl.replace state.pagemap
469 (pageno + 1, state.w, conf.angle, conf.proportional) opaque
472 let validopaque opaque = String.length opaque > 0;;
474 let render l =
475 match getopaque l.pageno with
476 | None when not state.rendering ->
477 state.rendering <- true;
478 cache l.pageno ("", -1);
479 wcmd "render" [`i (l.pageno + 1)
480 ;`i l.pagedimno
481 ;`i l.pagew
482 ;`i l.pageh];
484 | _ -> ()
487 let loadlayout layout =
488 let rec f all = function
489 | l :: ls ->
490 begin match getopaque l.pageno with
491 | None -> render l; f false ls
492 | Some (opaque, _) -> f (all && validopaque opaque) ls
494 | [] -> all
496 f (layout <> []) layout;
499 let findpageforopaque opaque =
500 Hashtbl.fold
501 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
502 state.pagemap None
505 let pagevisible n = List.exists (fun l -> l.pageno + 1 = n) state.layout;;
507 let preload () =
508 if conf.preload
509 then
510 let oktopreload =
511 let opaque = cbpeek state.pagecache in
512 match findpageforopaque opaque with
513 | Some ((n, _, _, _), size) ->
514 not (pagevisible n) && state.memused - size <= conf.memlimit
515 | None -> false
517 if oktopreload
518 then
519 let rely = yratio state.y in
520 let presentation = conf.presentation in
521 let interpagespace = conf.interpagespace in
522 let maxy = state.maxy in
523 conf.presentation <- false;
524 conf.interpagespace <- 0;
525 state.maxy <- calcheight ();
526 let y = truncate (float state.maxy *. rely) in
527 let y = if y < conf.winh then 0 else y - conf.winh in
528 let pages = layout y (conf.winh*3) in
529 List.iter render pages;
530 conf.presentation <- presentation;
531 conf.interpagespace <- interpagespace;
532 state.maxy <- maxy;
535 let gotoy y =
536 let y = max 0 y in
537 let y = min state.maxy y in
538 let pages = layout y conf.winh in
539 let ready = loadlayout pages in
540 state.ty <- yratio y;
541 if conf.showall
542 then (
543 if ready
544 then (
545 state.layout <- pages;
546 state.y <- y;
547 Glut.postRedisplay ();
550 else (
551 state.layout <- pages;
552 state.y <- y;
553 Glut.postRedisplay ();
555 preload ();
558 let gotoy_and_clear_text y =
559 gotoy y;
560 if not conf.verbose then state.text <- "";
563 let addnav () =
564 cbput state.hists.nav (yratio state.y);
567 let getnav () =
568 let y = cbget state.hists.nav ~-1 in
569 truncate (y *. float state.maxy)
572 let gotopage n top =
573 let y, h = getpageyh n in
574 addnav ();
575 gotoy_and_clear_text (y + (truncate (top *. float h)));
578 let gotopage1 n top =
579 let y = getpagey n in
580 addnav ();
581 gotoy_and_clear_text (y + top);
584 let invalidate () =
585 state.layout <- [];
586 state.pdims <- [];
587 state.rects <- [];
588 state.rects1 <- [];
589 state.invalidated <- state.invalidated + 1;
592 let scalecolor c =
593 let c = c *. state.colorscale in
594 (c, c, c);
597 let represent () =
598 let y =
599 match state.layout with
600 | [] ->
601 let rely = yratio state.y in
602 state.maxy <- calcheight ();
603 truncate (float state.maxy *. rely)
605 | l :: _ ->
606 state.maxy <- calcheight ();
607 getpagey l.pageno
609 gotoy y
612 let pagematrix () =
613 GlMat.mode `projection;
614 GlMat.load_identity ();
615 GlMat.rotate ~x:1.0 ~angle:180.0 ();
616 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
617 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
620 let winmatrix () =
621 GlMat.mode `projection;
622 GlMat.load_identity ();
623 GlMat.rotate ~x:1.0 ~angle:180.0 ();
624 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
625 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
628 let reshape ~w ~h =
629 conf.winw <- w;
630 let w = truncate (float w *. conf.zoom) - conf.scrollw in
631 let w = max w 2 in
632 state.w <- w;
633 conf.winh <- h;
634 GlMat.mode `modelview;
635 GlMat.load_identity ();
636 GlClear.color (scalecolor 1.0);
637 GlClear.clear [`color];
639 invalidate ();
640 wcmd "geometry" [`i w; `i h];
643 let showtext c s =
644 GlDraw.color (0.0, 0.0, 0.0);
645 GlDraw.rect
646 (0.0, float (conf.winh - 18))
647 (float (conf.winw - conf.scrollw - 1), float conf.winh)
649 let font = Glut.BITMAP_8_BY_13 in
650 GlDraw.color (1.0, 1.0, 1.0);
651 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
652 Glut.bitmapCharacter ~font ~c:(Char.code c);
653 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
656 let enttext () =
657 let len = String.length state.text in
658 match state.textentry with
659 | None ->
660 if len > 0 then showtext ' ' state.text
662 | Some (c, text, _, _, _) ->
663 let s =
664 if len > 0
665 then
666 text ^ " [" ^ state.text ^ "]"
667 else
668 text
670 showtext c s;
673 let showtext c s =
674 if true
675 then (
676 state.text <- Printf.sprintf "%c%s" c s;
677 Glut.postRedisplay ();
679 else (
680 showtext c s;
681 Glut.swapBuffers ();
685 let act cmd =
686 match cmd.[0] with
687 | 'c' ->
688 state.pdims <- [];
690 | 'D' ->
691 state.rects <- state.rects1;
692 Glut.postRedisplay ()
694 | 'C' ->
695 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
696 state.pagecount <- n;
697 state.invalidated <- state.invalidated - 1;
698 if state.invalidated = 0
699 then represent ()
701 | 't' ->
702 let s = Scanf.sscanf cmd "t %n"
703 (fun n -> String.sub cmd n (String.length cmd - n))
705 Glut.setWindowTitle s
707 | 'T' ->
708 let s = Scanf.sscanf cmd "T %n"
709 (fun n -> String.sub cmd n (String.length cmd - n))
711 if state.textentry = None
712 then (
713 state.text <- s;
714 showtext ' ' s;
716 else (
717 state.text <- s;
718 Glut.postRedisplay ();
721 | 'V' ->
722 if conf.verbose
723 then
724 let s = Scanf.sscanf cmd "V %n"
725 (fun n -> String.sub cmd n (String.length cmd - n))
727 state.text <- s;
728 showtext ' ' s;
730 | 'F' ->
731 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
732 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
733 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
734 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
736 let y = (getpagey pageno) + truncate y0 in
737 addnav ();
738 gotoy y;
739 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
741 | 'R' ->
742 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
743 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
744 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
745 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
747 state.rects1 <-
748 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
750 | 'r' ->
751 let n, w, h, r, l, s, p =
752 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
753 (fun n w h r l s p -> (n, w, h, r, l != 0, s, p))
756 Hashtbl.replace state.pagemap (n, w, r, l) (p, s);
757 state.memused <- state.memused + s;
759 let rec gc () =
760 if (state.memused <= conf.memlimit) || cbempty state.pagecache
761 then ()
762 else (
763 let evictedopaque = cbpeek state.pagecache in
764 match findpageforopaque evictedopaque with
765 | None -> failwith "bug in gc"
766 | Some ((evictedn, _, _, _) as k, evictedsize) ->
767 if not (pagevisible evictedn)
768 then (
769 wcmd "free" [`s evictedopaque];
770 state.memused <- state.memused - evictedsize;
771 Hashtbl.remove state.pagemap k;
772 cbdecr state.pagecache;
773 gc ();
777 gc ();
779 cbput state.pagecache p;
780 state.rendering <- false;
782 if conf.showall
783 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
784 else (
785 if pagevisible n
786 then gotoy state.y
787 else (ignore (loadlayout state.layout); preload ())
790 | 'l' ->
791 let (n, w, h, x) as pdim =
792 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
794 state.pdims <- pdim :: state.pdims
796 | 'o' ->
797 let (l, n, t, h, pos) =
798 Scanf.sscanf cmd "o %u %u %u %u %n" (fun l n t h pos -> l, n, t, h, pos)
800 let s = String.sub cmd pos (String.length cmd - pos) in
801 let s =
802 let l = String.length s in
803 let b = Buffer.create (String.length s) in
804 let rec loop pc2 i =
805 if i = l
806 then ()
807 else
808 let pc2 =
809 match s.[i] with
810 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
811 | '\xc2' -> true
812 | c ->
813 let c = if Char.code c land 0x80 = 0 then c else '?' in
814 Buffer.add_char b c;
815 false
817 loop pc2 (i+1)
819 loop false 0;
820 Buffer.contents b
822 let outline = (s, l, n, float t /. float h) in
823 let outlines =
824 match state.outlines with
825 | Olist outlines -> Olist (outline :: outlines)
826 | Oarray _ -> Olist [outline]
827 | Onarrow _ -> Olist [outline]
829 state.outlines <- outlines
831 | _ ->
832 log "unknown cmd `%S'" cmd
835 let now = Unix.gettimeofday;;
837 let idle () =
838 let rec loop delay =
839 let r, _, _ = Unix.select [state.csock] [] [] delay in
840 begin match r with
841 | [] ->
842 if conf.autoscroll
843 then begin
844 let y = state.y + conf.scrollincr in
845 let y = if y >= state.maxy then 0 else y in
846 gotoy y;
847 state.text <- "";
848 end;
850 | _ ->
851 let cmd = readcmd state.csock in
852 act cmd;
853 loop 0.0
854 end;
855 in loop 0.001
858 let onhist cb =
859 let rc = cb.rc in
860 function
861 | HCprev -> cbget cb ~-1
862 | HCnext -> cbget cb 1
863 | HCfirst -> cbget cb ~-(cb.rc)
864 | HClast -> cbget cb (cb.len - 1 - cb.rc)
865 | HCcancel -> cb.rc <- rc; cb.store.(0) (* ugly *)
868 let search pattern forward =
869 if String.length pattern > 0
870 then
871 let pn, py =
872 match state.layout with
873 | [] -> 0, 0
874 | l :: _ ->
875 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
877 let cmd =
878 let b = makecmd "search"
879 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
881 Buffer.add_char b ',';
882 Buffer.add_string b pattern;
883 Buffer.add_char b '\000';
884 Buffer.contents b;
886 writecmd state.csock cmd;
889 let intentry text key =
890 let c = Char.unsafe_chr key in
891 match c with
892 | '0' .. '9' ->
893 let s = "x" in s.[0] <- c;
894 let text = text ^ s in
895 TEcont text
897 | _ ->
898 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
899 TEcont text
902 let addchar s c =
903 let b = Buffer.create (String.length s + 1) in
904 Buffer.add_string b s;
905 Buffer.add_char b c;
906 Buffer.contents b;
909 let textentry text key =
910 let c = Char.unsafe_chr key in
911 match c with
912 | _ when key >= 32 && key < 127 ->
913 let text = addchar text c in
914 TEcont text
916 | _ ->
917 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
918 TEcont text
921 let reinit angle proportional =
922 conf.angle <- angle;
923 conf.proportional <- proportional;
924 invalidate ();
925 wcmd "reinit" [`i angle; `b proportional];
928 let optentry text key =
929 let btos b = if b then "on" else "off" in
930 let c = Char.unsafe_chr key in
931 match c with
932 | 's' ->
933 let ondone s =
934 try conf.scrollincr <- int_of_string s with exc ->
935 state.text <- Printf.sprintf "bad integer `%s': %s"
936 s (Printexc.to_string exc)
938 TEswitch ('#', "", None, intentry, ondone)
940 | 'R' ->
941 let ondone s =
942 match try
943 Some (int_of_string s)
944 with exc ->
945 state.text <- Printf.sprintf "bad integer `%s': %s"
946 s (Printexc.to_string exc);
947 None
948 with
949 | Some angle -> reinit angle conf.proportional
950 | None -> ()
952 TEswitch ('^', "", None, intentry, ondone)
954 | 'i' ->
955 conf.icase <- not conf.icase;
956 TEdone ("case insensitive search " ^ (btos conf.icase))
958 | 'p' ->
959 conf.preload <- not conf.preload;
960 gotoy state.y;
961 TEdone ("preload " ^ (btos conf.preload))
963 | 'v' ->
964 conf.verbose <- not conf.verbose;
965 TEdone ("verbose " ^ (btos conf.verbose))
967 | 'h' ->
968 conf.maxhfit <- not conf.maxhfit;
969 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
970 TEdone ("maxhfit " ^ (btos conf.maxhfit))
972 | 'c' ->
973 conf.crophack <- not conf.crophack;
974 TEdone ("crophack " ^ btos conf.crophack)
976 | 'a' ->
977 conf.showall <- not conf.showall;
978 TEdone ("showall " ^ btos conf.showall)
980 | 'f' ->
981 conf.underinfo <- not conf.underinfo;
982 TEdone ("underinfo " ^ btos conf.underinfo)
984 | 'P' ->
985 conf.savebmarks <- not conf.savebmarks;
986 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
988 | 'S' ->
989 let ondone s =
991 conf.interpagespace <- int_of_string s;
992 let rely = yratio state.y in
993 state.maxy <- calcheight ();
994 gotoy (truncate (float state.maxy *. rely));
995 with exc ->
996 state.text <- Printf.sprintf "bad integer `%s': %s"
997 s (Printexc.to_string exc)
999 TEswitch ('%', "", None, intentry, ondone)
1001 | 'l' ->
1002 reinit conf.angle (not conf.proportional);
1003 TEdone ("proprortional display " ^ btos conf.proportional)
1005 | _ ->
1006 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1007 TEstop
1010 let maxoutlinerows () = (conf.winh - 31) / 16;;
1012 let enterselector allowdel outlines errmsg msg =
1013 if Array.length outlines = 0
1014 then (
1015 showtext ' ' errmsg;
1017 else (
1018 state.text <- msg;
1019 Glut.setCursor Glut.CURSOR_INHERIT;
1020 let pageno =
1021 match state.layout with
1022 | [] -> -1
1023 | {pageno=pageno} :: rest -> pageno
1025 let active =
1026 let rec loop n =
1027 if n = Array.length outlines
1028 then 0
1029 else
1030 let (_, _, outlinepageno, _) = outlines.(n) in
1031 if outlinepageno >= pageno then n else loop (n+1)
1033 loop 0
1035 state.outline <-
1036 Some (allowdel, active,
1037 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1038 Glut.postRedisplay ();
1042 let enteroutlinemode () =
1043 let outlines, msg =
1044 match state.outlines with
1045 | Oarray a -> a, ""
1046 | Olist l ->
1047 let a = Array.of_list (List.rev l) in
1048 state.outlines <- Oarray a;
1049 a, ""
1050 | Onarrow (pat, a, b) ->
1051 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1053 enterselector false outlines "Document has no outline" msg;
1056 let enterbookmarkmode () =
1057 let bookmarks = Array.of_list state.bookmarks in
1058 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1061 let quickbookmark ?title () =
1062 match state.layout with
1063 | [] -> ()
1064 | l :: _ ->
1065 let title =
1066 match title with
1067 | None ->
1068 let sec = Unix.gettimeofday () in
1069 let tm = Unix.localtime sec in
1070 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1071 (l.pageno+1)
1072 tm.Unix.tm_mday
1073 tm.Unix.tm_mon
1074 (tm.Unix.tm_year + 1900)
1075 tm.Unix.tm_hour
1076 tm.Unix.tm_min
1077 | Some title -> title
1079 state.bookmarks <-
1080 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1083 let doreshape w h =
1084 state.fullscreen <- None;
1085 Glut.reshapeWindow w h;
1088 let writeopen path password =
1089 writecmd state.csock
1090 ("open " ^ path ^ "\000"
1091 ^ state.password ^ "\000"
1092 ^ string_of_int conf.angle ^ " "
1093 ^ (if conf.proportional then "1" else "0"))
1097 let opendoc path password =
1098 invalidate ();
1099 state.path <- path;
1100 state.password <- password;
1101 Hashtbl.clear state.pagemap;
1103 writeopen path password;
1104 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1105 wcmd "geometry" [`i state.w; `i conf.winh];
1108 let viewkeyboard ~key ~x ~y =
1109 let enttext te =
1110 state.textentry <- te;
1111 state.text <- "";
1112 enttext ();
1113 Glut.postRedisplay ()
1115 match state.textentry with
1116 | None ->
1117 let c = Char.chr key in
1118 begin match c with
1119 | '\027' | 'q' ->
1120 exit 0
1122 | '\008' ->
1123 let y = getnav () in
1124 gotoy_and_clear_text y
1126 | 'o' ->
1127 enteroutlinemode ()
1129 | 'u' ->
1130 state.rects <- [];
1131 state.text <- "";
1132 Glut.postRedisplay ()
1134 | '/' | '?' ->
1135 let ondone isforw s =
1136 cbput state.hists.pat s;
1137 state.searchpattern <- s;
1138 search s isforw
1140 enttext (Some (c, "", Some (onhist state.hists.pat),
1141 textentry, ondone (c ='/')))
1143 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1144 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1145 conf.zoom <- min 2.2 (conf.zoom +. incr);
1146 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1147 reshape conf.winw conf.winh
1149 | '+' ->
1150 let ondone s =
1151 let n =
1152 try int_of_string s with exc ->
1153 state.text <- Printf.sprintf "bad integer `%s': %s"
1154 s (Printexc.to_string exc);
1155 max_int
1157 if n != max_int
1158 then (
1159 conf.pagebias <- n;
1160 state.text <- "page bias is now " ^ string_of_int n;
1163 enttext (Some ('+', "", None, intentry, ondone))
1165 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1166 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1167 conf.zoom <- max 0.01 (conf.zoom -. decr);
1168 if conf.zoom <= 1.0 then state.x <- 0;
1169 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1170 reshape conf.winw conf.winh;
1172 | '-' ->
1173 let ondone msg =
1174 state.text <- msg;
1176 enttext (Some ('-', "", None, optentry, ondone))
1178 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1179 state.x <- 0;
1180 conf.zoom <- 1.0;
1181 state.text <- "zoom is 100%";
1182 reshape conf.winw conf.winh
1184 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1185 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1186 if zoom < 1.0
1187 then (
1188 conf.zoom <- zoom;
1189 state.x <- 0;
1190 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1191 reshape conf.winw conf.winh;
1194 | '0' .. '9' ->
1195 let ondone s =
1196 let n =
1197 try int_of_string s with exc ->
1198 state.text <- Printf.sprintf "bad integer `%s': %s"
1199 s (Printexc.to_string exc);
1202 if n >= 0
1203 then (
1204 addnav ();
1205 cbput state.hists.pag (string_of_int n);
1206 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1209 let pageentry text key =
1210 match Char.unsafe_chr key with
1211 | 'g' -> TEdone text
1212 | _ -> intentry text key
1214 let text = "x" in text.[0] <- c;
1215 enttext (Some (':', text, Some (onhist state.hists.pag),
1216 pageentry, ondone))
1218 | 'b' ->
1219 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1220 reshape conf.winw conf.winh;
1222 | 'l' ->
1223 conf.hlinks <- not conf.hlinks;
1224 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1225 Glut.postRedisplay ()
1227 | 'a' ->
1228 conf.autoscroll <- not conf.autoscroll
1230 | 'P' ->
1231 conf.presentation <- not conf.presentation;
1232 showtext ' ' ("presentation mode " ^
1233 if conf.presentation then "on" else "off");
1234 represent ()
1236 | 'f' ->
1237 begin match state.fullscreen with
1238 | None ->
1239 state.fullscreen <- Some (conf.winw, conf.winh);
1240 Glut.fullScreen ()
1241 | Some (w, h) ->
1242 state.fullscreen <- None;
1243 doreshape w h
1246 | 'g' ->
1247 gotoy_and_clear_text 0
1249 | 'n' ->
1250 search state.searchpattern true
1252 | 'p' | 'N' ->
1253 search state.searchpattern false
1255 | 't' ->
1256 begin match state.layout with
1257 | [] -> ()
1258 | l :: _ ->
1259 gotoy_and_clear_text (getpagey l.pageno)
1262 | ' ' ->
1263 begin match List.rev state.layout with
1264 | [] -> ()
1265 | l :: _ ->
1266 let pageno = min (l.pageno+1) (state.pagecount-1) in
1267 gotoy_and_clear_text (getpagey pageno)
1270 | '\127' ->
1271 begin match state.layout with
1272 | [] -> ()
1273 | l :: _ ->
1274 let pageno = max 0 (l.pageno-1) in
1275 gotoy_and_clear_text (getpagey pageno)
1278 | '=' ->
1279 let f (fn, ln) l =
1280 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1282 let fn, ln = List.fold_left f (-1, -1) state.layout in
1283 let s =
1284 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1285 let percent =
1286 if maxy <= 0
1287 then 100.
1288 else (100. *. (float state.y /. float maxy)) in
1289 if fn = ln
1290 then
1291 Printf.sprintf "Page %d of %d %.2f%%"
1292 (fn+1) state.pagecount percent
1293 else
1294 Printf.sprintf
1295 "Pages %d-%d of %d %.2f%%"
1296 (fn+1) (ln+1) state.pagecount percent
1298 showtext ' ' s;
1300 | 'w' ->
1301 begin match state.layout with
1302 | [] -> ()
1303 | l :: _ ->
1304 doreshape (l.pagew + conf.scrollw) l.pageh;
1305 Glut.postRedisplay ();
1308 | '\'' ->
1309 enterbookmarkmode ()
1311 | 'm' ->
1312 let ondone s =
1313 match state.layout with
1314 | l :: _ ->
1315 state.bookmarks <-
1316 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1317 :: state.bookmarks
1318 | _ -> ()
1320 enttext (Some ('~', "", None, textentry, ondone))
1322 | '~' ->
1323 quickbookmark ();
1324 showtext ' ' "Quick bookmark added";
1326 | 'z' ->
1327 begin match state.layout with
1328 | l :: _ ->
1329 let rect = getpdimrect l.pagedimno in
1330 let w, h =
1331 if conf.crophack
1332 then
1333 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1334 truncate (1.2 *. (rect.(3) -. rect.(0))))
1335 else
1336 (truncate (rect.(1) -. rect.(0)),
1337 truncate (rect.(3) -. rect.(0)))
1339 if w != 0 && h != 0
1340 then
1341 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1343 Glut.postRedisplay ();
1345 | [] -> ()
1348 | '<' | '>' ->
1349 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1351 | '[' | ']' ->
1352 state.colorscale <-
1353 max 0.0
1354 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1355 Glut.postRedisplay ()
1357 | 'k' -> gotoy (clamp (-conf.scrollincr))
1358 | 'j' -> gotoy (clamp conf.scrollincr)
1360 | 'r' -> opendoc state.path state.password
1362 | _ ->
1363 vlog "huh? %d %c" key (Char.chr key);
1366 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1367 let len = String.length text in
1368 if len = 0
1369 then (
1370 state.textentry <- None;
1371 Glut.postRedisplay ();
1373 else (
1374 let s = String.sub text 0 (len - 1) in
1375 enttext (Some (c, s, opthist, onkey, ondone))
1378 | Some (c, text, onhist, onkey, ondone) ->
1379 begin match Char.unsafe_chr key with
1380 | '\r' | '\n' ->
1381 ondone text;
1382 state.textentry <- None;
1383 Glut.postRedisplay ()
1385 | '\027' ->
1386 begin match onhist with
1387 | None -> ()
1388 | Some onhist -> ignore (onhist HCcancel)
1389 end;
1390 state.textentry <- None;
1391 Glut.postRedisplay ()
1393 | _ ->
1394 begin match onkey text key with
1395 | TEdone text ->
1396 state.textentry <- None;
1397 ondone text;
1398 Glut.postRedisplay ()
1400 | TEcont text ->
1401 enttext (Some (c, text, onhist, onkey, ondone));
1403 | TEstop ->
1404 state.textentry <- None;
1405 Glut.postRedisplay ()
1407 | TEswitch te ->
1408 state.textentry <- Some te;
1409 Glut.postRedisplay ()
1410 end;
1411 end;
1414 let narrow outlines pattern =
1415 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1416 match reopt with
1417 | None -> None
1418 | Some re ->
1419 let rec fold accu n =
1420 if n = -1
1421 then accu
1422 else
1423 let (s, _, _, _) as o = outlines.(n) in
1424 let accu =
1425 if (try ignore (Str.search_forward re s 0); true
1426 with Not_found -> false)
1427 then (o :: accu)
1428 else accu
1430 fold accu (n-1)
1432 let matched = fold [] (Array.length outlines - 1) in
1433 if matched = [] then None else Some (Array.of_list matched)
1436 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1437 let search active pattern incr =
1438 let dosearch re =
1439 let rec loop n =
1440 if n = Array.length outlines || n = -1
1441 then None
1442 else
1443 let (s, _, _, _) = outlines.(n) in
1445 (try ignore (Str.search_forward re s 0); true
1446 with Not_found -> false)
1447 then Some n
1448 else loop (n + incr)
1450 loop active
1453 let re = Str.regexp_case_fold pattern in
1454 dosearch re
1455 with Failure s ->
1456 state.text <- s;
1457 None
1459 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1460 match key with
1461 | 27 ->
1462 if String.length qsearch = 0
1463 then (
1464 state.text <- "";
1465 state.outline <- None;
1466 Glut.postRedisplay ();
1468 else (
1469 state.text <- "";
1470 state.outline <- Some (allowdel, active, first, outlines, "");
1471 Glut.postRedisplay ();
1474 | 18 | 19 ->
1475 let incr = if key = 18 then -1 else 1 in
1476 let active, first =
1477 match search (active + incr) qsearch incr with
1478 | None ->
1479 state.text <- qsearch ^ " [not found]";
1480 active, first
1481 | Some active ->
1482 state.text <- qsearch;
1483 active, firstof active
1485 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1486 Glut.postRedisplay ();
1488 | 8 ->
1489 let len = String.length qsearch in
1490 if len = 0
1491 then ()
1492 else (
1493 if len = 1
1494 then (
1495 state.text <- "";
1496 state.outline <- Some (allowdel, active, first, outlines, "");
1498 else
1499 let qsearch = String.sub qsearch 0 (len - 1) in
1500 let active, first =
1501 match search active qsearch ~-1 with
1502 | None ->
1503 state.text <- qsearch ^ " [not found]";
1504 active, first
1505 | Some active ->
1506 state.text <- qsearch;
1507 active, firstof active
1509 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1511 Glut.postRedisplay ()
1513 | 13 ->
1514 if active < Array.length outlines
1515 then (
1516 let (_, _, n, t) = outlines.(active) in
1517 gotopage n t;
1519 state.text <- "";
1520 if allowdel then state.bookmarks <- Array.to_list outlines;
1521 state.outline <- None;
1522 Glut.postRedisplay ();
1524 | _ when key >= 32 && key < 127 ->
1525 let pattern = addchar qsearch (Char.chr key) in
1526 let active, first =
1527 match search active pattern 1 with
1528 | None ->
1529 state.text <- pattern ^ " [not found]";
1530 active, first
1531 | Some active ->
1532 state.text <- pattern;
1533 active, firstof active
1535 state.outline <- Some (allowdel, active, first, outlines, pattern);
1536 Glut.postRedisplay ()
1538 | 14 when not allowdel -> (* ctrl-n *)
1539 if String.length qsearch > 0
1540 then (
1541 let optoutlines = narrow outlines qsearch in
1542 begin match optoutlines with
1543 | None -> state.text <- "can't narrow"
1544 | Some outlines ->
1545 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1546 match state.outlines with
1547 | Olist l -> ()
1548 | Oarray a ->
1549 state.outlines <- Onarrow (qsearch, outlines, a)
1550 | Onarrow (pat, a, b) ->
1551 state.outlines <- Onarrow (qsearch, outlines, b)
1552 end;
1554 Glut.postRedisplay ()
1556 | 21 when not allowdel -> (* ctrl-u *)
1557 let outline =
1558 match state.outlines with
1559 | Oarray a -> a
1560 | Olist l ->
1561 let a = Array.of_list (List.rev l) in
1562 state.outlines <- Oarray a;
1564 | Onarrow (pat, a, b) ->
1565 state.outlines <- Oarray b;
1566 state.text <- "";
1569 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1570 Glut.postRedisplay ()
1572 | 12 ->
1573 state.outline <-
1574 Some (allowdel, active, firstof active, outlines, qsearch);
1575 Glut.postRedisplay ()
1577 | 127 when allowdel ->
1578 let len = Array.length outlines - 1 in
1579 if len = 0
1580 then (
1581 state.outline <- None;
1582 state.bookmarks <- [];
1584 else (
1585 let bookmarks = Array.init len
1586 (fun i ->
1587 let i = if i >= active then i + 1 else i in
1588 outlines.(i)
1591 state.outline <-
1592 Some (allowdel,
1593 min active (len-1),
1594 min first (len-1),
1595 bookmarks, qsearch)
1598 Glut.postRedisplay ()
1600 | _ -> log "unknown key %d" key
1603 let keyboard ~key ~x ~y =
1604 if key = 7
1605 then
1606 wcmd "interrupt" []
1607 else
1608 match state.outline with
1609 | None -> viewkeyboard ~key ~x ~y
1610 | Some outline -> outlinekeyboard ~key ~x ~y outline
1613 let special ~key ~x ~y =
1614 match state.outline with
1615 | None ->
1616 begin match state.textentry with
1617 | None ->
1618 let y =
1619 match key with
1620 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1621 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1622 | Glut.KEY_DOWN -> clamp conf.scrollincr
1623 | Glut.KEY_PAGE_UP ->
1624 if Glut.getModifiers () land Glut.active_ctrl != 0
1625 then
1626 match state.layout with
1627 | [] -> state.y
1628 | l :: _ -> state.y - l.pagey
1629 else
1630 clamp (-conf.winh)
1631 | Glut.KEY_PAGE_DOWN ->
1632 if Glut.getModifiers () land Glut.active_ctrl != 0
1633 then
1634 match List.rev state.layout with
1635 | [] -> state.y
1636 | l :: _ -> getpagey l.pageno
1637 else
1638 clamp conf.winh
1639 | Glut.KEY_HOME -> addnav (); 0
1640 | Glut.KEY_END ->
1641 addnav ();
1642 state.maxy - (if conf.maxhfit then conf.winh else 0)
1644 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1645 state.x <- state.x - 10;
1646 state.y
1647 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1648 state.x <- state.x + 10;
1649 state.y
1651 | _ -> state.y
1653 gotoy_and_clear_text y
1655 | Some (c, s, Some onhist, onkey, ondone) ->
1656 let s =
1657 match key with
1658 | Glut.KEY_UP -> onhist HCprev
1659 | Glut.KEY_DOWN -> onhist HCnext
1660 | Glut.KEY_HOME -> onhist HCfirst
1661 | Glut.KEY_END -> onhist HClast
1662 | _ -> state.text
1664 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1665 Glut.postRedisplay ()
1667 | _ -> ()
1670 | Some (allowdel, active, first, outlines, qsearch) ->
1671 let maxrows = maxoutlinerows () in
1672 let calcfirst first active =
1673 if active > first
1674 then
1675 let rows = active - first in
1676 if rows > maxrows then active - maxrows else first
1677 else active
1679 let navigate incr =
1680 let active = active + incr in
1681 let active = max 0 (min active (Array.length outlines - 1)) in
1682 let first = calcfirst first active in
1683 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1684 Glut.postRedisplay ()
1686 let updownlevel incr =
1687 let len = Array.length outlines in
1688 let (_, curlevel, _, _) = outlines.(active) in
1689 let rec flow i =
1690 if i = len then i-1 else if i = -1 then 0 else
1691 let (_, l, _, _) = outlines.(i) in
1692 if l != curlevel then i else flow (i+incr)
1694 let active = flow active in
1695 let first = calcfirst first active in
1696 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1697 Glut.postRedisplay ()
1699 match key with
1700 | Glut.KEY_UP -> navigate ~-1
1701 | Glut.KEY_DOWN -> navigate 1
1702 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1703 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1705 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1706 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1708 | Glut.KEY_HOME ->
1709 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1710 Glut.postRedisplay ()
1712 | Glut.KEY_END ->
1713 let active = Array.length outlines - 1 in
1714 let first = max 0 (active - maxrows) in
1715 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1716 Glut.postRedisplay ()
1718 | _ -> ()
1721 let drawplaceholder l =
1722 GlDraw.color (scalecolor 1.0);
1723 GlDraw.rect
1724 (float l.pagex, float l.pagedispy)
1725 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1727 let x = float l.pagex
1728 and y = float (l.pagedispy + 13) in
1729 let font = Glut.BITMAP_8_BY_13 in
1730 GlDraw.color (0.0, 0.0, 0.0);
1731 GlPix.raster_pos ~x ~y ();
1732 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1733 ("Loading " ^ string_of_int (l.pageno + 1));
1736 let now () = Unix.gettimeofday ();;
1738 let drawpage i l =
1739 begin match getopaque l.pageno with
1740 | Some (opaque, _) when validopaque opaque ->
1741 if state.textentry = None
1742 then GlDraw.color (scalecolor 1.0)
1743 else GlDraw.color (scalecolor 0.4);
1744 let a = now () in
1745 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1746 opaque;
1747 let b = now () in
1748 let d = b-.a in
1749 vlog "draw %d %f sec" l.pageno d;
1751 | _ ->
1752 drawplaceholder l;
1753 end;
1754 l.pagedispy + l.pagevh;
1757 let scrollph y =
1758 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1759 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1760 let sh = float conf.winh /. sh in
1761 let sh = max sh (float conf.scrollh) in
1763 let percent =
1764 if state.y = state.maxy
1765 then 1.0
1766 else float y /. float maxy
1768 let position = (float conf.winh -. sh) *. percent in
1770 let position =
1771 if position +. sh > float conf.winh
1772 then float conf.winh -. sh
1773 else position
1775 position, sh;
1778 let scrollindicator () =
1779 GlDraw.color (0.64 , 0.64, 0.64);
1780 GlDraw.rect
1781 (float (conf.winw - conf.scrollw), 0.)
1782 (float conf.winw, float conf.winh)
1784 GlDraw.color (0.0, 0.0, 0.0);
1786 let position, sh = scrollph state.y in
1787 GlDraw.rect
1788 (float (conf.winw - conf.scrollw), position)
1789 (float conf.winw, position +. sh)
1793 let showsel margin =
1794 match state.mstate with
1795 | Mnone | Mscroll _ | Mpan _ ->
1798 | Msel ((x0, y0), (x1, y1)) ->
1799 let rec loop = function
1800 | l :: ls ->
1801 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1802 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1803 then
1804 match getopaque l.pageno with
1805 | Some (opaque, _) when validopaque opaque ->
1806 let oy = -l.pagey + l.pagedispy in
1807 seltext opaque
1808 (x0 - margin - state.x, y0,
1809 x1 - margin - state.x, y1) oy;
1811 | _ -> ()
1812 else loop ls
1813 | [] -> ()
1815 loop state.layout
1818 let showrects () =
1819 let panx = float state.x in
1820 Gl.enable `blend;
1821 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1822 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1823 List.iter
1824 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1825 List.iter (fun l ->
1826 if l.pageno = pageno
1827 then (
1828 let d = float (l.pagedispy - l.pagey) in
1829 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1830 GlDraw.begins `quads;
1832 GlDraw.vertex2 (x0+.panx, y0+.d);
1833 GlDraw.vertex2 (x1+.panx, y1+.d);
1834 GlDraw.vertex2 (x2+.panx, y2+.d);
1835 GlDraw.vertex2 (x3+.panx, y3+.d);
1837 GlDraw.ends ();
1839 ) state.layout
1840 ) state.rects
1842 Gl.disable `blend;
1845 let showoutline = function
1846 | None -> ()
1847 | Some (allowdel, active, first, outlines, qsearch) ->
1848 Gl.enable `blend;
1849 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1850 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1851 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
1852 Gl.disable `blend;
1854 GlDraw.color (1., 1., 1.);
1855 let font = Glut.BITMAP_9_BY_15 in
1856 let draw_string x y s =
1857 GlPix.raster_pos ~x ~y ();
1858 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1860 let rec loop row =
1861 if row = Array.length outlines || (row - first) * 16 > conf.winh
1862 then ()
1863 else (
1864 let (s, l, _, _) = outlines.(row) in
1865 let y = (row - first) * 16 in
1866 let x = 5 + 15*l in
1867 if row = active
1868 then (
1869 Gl.enable `blend;
1870 GlDraw.polygon_mode `both `line;
1871 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1872 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1873 GlDraw.rect (0., float (y + 1))
1874 (float (conf.winw - 1), float (y + 18));
1875 GlDraw.polygon_mode `both `fill;
1876 Gl.disable `blend;
1877 GlDraw.color (1., 1., 1.);
1879 draw_string (float x) (float (y + 16)) s;
1880 loop (row+1)
1883 loop first
1886 let display () =
1887 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1888 GlDraw.viewport margin 0 state.w conf.winh;
1889 pagematrix ();
1890 GlClear.color (scalecolor 0.5);
1891 GlClear.clear [`color];
1892 if state.x != 0
1893 then (
1894 let x = float state.x in
1895 GlMat.translate ~x ();
1897 if conf.zoom > 1.0
1898 then (
1899 Gl.enable `scissor_test;
1900 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
1902 let _lasty = List.fold_left drawpage 0 (state.layout) in
1903 if conf.zoom > 1.0
1904 then
1905 Gl.disable `scissor_test
1907 if state.x != 0
1908 then (
1909 let x = -.float state.x in
1910 GlMat.translate ~x ();
1912 showrects ();
1913 showsel margin;
1914 GlDraw.viewport 0 0 conf.winw conf.winh;
1915 winmatrix ();
1916 scrollindicator ();
1917 showoutline state.outline;
1918 enttext ();
1919 Glut.swapBuffers ();
1922 let getunder x y =
1923 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1924 let x = x - margin - state.x in
1925 let rec f = function
1926 | l :: rest ->
1927 begin match getopaque l.pageno with
1928 | Some (opaque, _) when validopaque opaque ->
1929 let y = y - l.pagedispy in
1930 if y > 0
1931 then
1932 let y = l.pagey + y in
1933 let x = x - l.pagex in
1934 match whatsunder opaque x y with
1935 | Unone -> f rest
1936 | under -> under
1937 else
1938 f rest
1939 | _ ->
1940 f rest
1942 | [] -> Unone
1944 f state.layout
1947 let mouse ~button ~bstate ~x ~y =
1948 match button with
1949 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1950 let incr =
1951 if n = 3
1952 then
1953 -conf.scrollincr
1954 else
1955 conf.scrollincr
1957 let incr = incr * 2 in
1958 let y = clamp incr in
1959 gotoy_and_clear_text y
1961 | Glut.LEFT_BUTTON when state.outline = None
1962 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
1963 if bstate = Glut.DOWN
1964 then (
1965 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1966 state.mstate <- Mpan (x, y)
1968 else
1969 state.mstate <- Mnone
1971 | Glut.LEFT_BUTTON
1972 when state.outline = None && x > conf.winw - conf.scrollw ->
1973 if bstate = Glut.DOWN
1974 then
1975 let position, sh = scrollph state.y in
1976 if y > truncate position && y < truncate (position +. sh)
1977 then
1978 state.mstate <- Mscroll
1979 else
1980 let percent = float y /. float conf.winh in
1981 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
1982 gotoy desty;
1983 state.mstate <- Mscroll
1984 else
1985 state.mstate <- Mnone
1987 | Glut.LEFT_BUTTON when state.outline = None ->
1988 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1989 begin match dest with
1990 | Ulinkgoto (pageno, top) ->
1991 if pageno >= 0
1992 then
1993 gotopage1 pageno top
1995 | Ulinkuri s ->
1996 print_endline s
1998 | Unone when bstate = Glut.DOWN ->
1999 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2000 state.mstate <- Mpan (x, y);
2002 | Unone | Utext _ ->
2003 if bstate = Glut.DOWN
2004 then (
2005 if conf.angle mod 360 = 0
2006 then (
2007 state.mstate <- Msel ((x, y), (x, y));
2008 Glut.postRedisplay ()
2011 else (
2012 match state.mstate with
2013 | Mnone -> ()
2015 | Mscroll ->
2016 state.mstate <- Mnone
2018 | Mpan _ ->
2019 Glut.setCursor Glut.CURSOR_INHERIT;
2020 state.mstate <- Mnone
2022 | Msel ((x0, y0), (x1, y1)) ->
2023 let f l =
2024 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2025 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2026 then
2027 match getopaque l.pageno with
2028 | Some (opaque, _) when validopaque opaque ->
2029 copysel opaque
2030 | _ -> ()
2032 List.iter f state.layout;
2033 copysel ""; (* ugly *)
2034 Glut.setCursor Glut.CURSOR_INHERIT;
2035 state.mstate <- Mnone;
2039 | _ ->
2042 let mouse ~button ~state ~x ~y = mouse button state x y;;
2044 let motion ~x ~y =
2045 if state.outline = None
2046 then
2047 match state.mstate with
2048 | Mnone -> ()
2050 | Mpan (x0, y0) ->
2051 let dx = x - x0
2052 and dy = y0 - y in
2053 state.mstate <- Mpan (x, y);
2054 if conf.zoom > 1.0 then state.x <- state.x + dx;
2055 let y = clamp dy in
2056 gotoy_and_clear_text y
2058 | Msel (a, _) ->
2059 state.mstate <- Msel (a, (x, y));
2060 Glut.postRedisplay ()
2062 | Mscroll ->
2063 let y = min conf.winh (max 0 y) in
2064 let percent = float y /. float conf.winh in
2065 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2066 gotoy_and_clear_text y
2069 let pmotion ~x ~y =
2070 if state.outline = None
2071 then
2072 match state.mstate with
2073 | Mnone ->
2074 begin match getunder x y with
2075 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2076 | Ulinkuri uri ->
2077 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2078 Glut.setCursor Glut.CURSOR_INFO
2079 | Ulinkgoto (page, y) ->
2080 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
2081 Glut.setCursor Glut.CURSOR_INFO
2082 | Utext s ->
2083 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2084 Glut.setCursor Glut.CURSOR_TEXT
2087 | Mpan _ | Msel _ | Mscroll ->
2091 module State =
2092 struct
2093 open Parser
2095 let home =
2097 match Sys.os_type with
2098 | "Win32" -> Sys.getenv "HOMEPATH"
2099 | _ -> Sys.getenv "HOME"
2100 with exn ->
2101 prerr_endline
2102 ("Can not determine home directory location: " ^
2103 Printexc.to_string exn);
2107 let config_of c attrs =
2108 let apply c k v =
2110 match k with
2111 | "scroll-bar-width" -> { c with scrollw = int_of_string v }
2112 | "scroll-handle-height" -> { c with scrollh = int_of_string v }
2113 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2114 | "preload" -> { c with preload = bool_of_string v }
2115 | "page-bias" -> { c with pagebias = int_of_string v }
2116 | "scroll-step" -> { c with scrollincr = int_of_string v }
2117 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2118 | "crop-hack" -> { c with crophack = bool_of_string v }
2119 | "throttle" -> { c with showall = bool_of_string v }
2120 | "highlight-links" -> { c with hlinks = bool_of_string v }
2121 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2122 | "vertical-margin" -> { c with interpagespace = int_of_string v }
2123 | "zoom" ->
2124 let zoom = float_of_string v /. 100. in
2125 let zoom = max 0.01 (min 2.2 zoom) in
2126 { c with zoom = zoom }
2127 | "presentation" -> { c with presentation = bool_of_string v }
2128 | "rotation-angle" -> { c with angle = int_of_string v }
2129 | "width" -> { c with winw = int_of_string v }
2130 | "height" -> { c with winh = int_of_string v }
2131 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2132 | "proportional-display" -> { c with proportional = bool_of_string v }
2133 | "pixmap-cache-size" -> { c with memlimit = int_of_string v }
2134 | _ -> c
2135 with exn ->
2136 prerr_endline ("Error processing attribute (`" ^
2137 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2140 let rec fold c = function
2141 | [] -> c
2142 | (k, v) :: rest ->
2143 let c = apply c k v in
2144 fold c rest
2146 fold c attrs;
2149 let bookmark_of attrs =
2150 let rec fold title page rely = function
2151 | ("title", v) :: rest -> fold v page rely rest
2152 | ("page", v) :: rest -> fold title v rely rest
2153 | ("rely", v) :: rest -> fold title page v rest
2154 | _ :: rest -> fold title page rely rest
2155 | [] -> title, page, rely
2157 fold "invalid" "0" "0" attrs
2160 let setconf dst src =
2161 dst.scrollw <- src.scrollw;
2162 dst.scrollh <- src.scrollh;
2163 dst.icase <- src.icase;
2164 dst.preload <- src.preload;
2165 dst.pagebias <- src.pagebias;
2166 dst.verbose <- src.verbose;
2167 dst.scrollincr <- src.scrollincr;
2168 dst.maxhfit <- src.maxhfit;
2169 dst.crophack <- src.crophack;
2170 dst.autoscroll <- src.autoscroll;
2171 dst.showall <- src.showall;
2172 dst.hlinks <- src.hlinks;
2173 dst.underinfo <- src.underinfo;
2174 dst.interpagespace <- src.interpagespace;
2175 dst.zoom <- src.zoom;
2176 dst.presentation <- src.presentation;
2177 dst.angle <- src.angle;
2178 dst.winw <- src.winw;
2179 dst.winh <- src.winh;
2180 dst.savebmarks <- src.savebmarks;
2181 dst.memlimit <- src.memlimit;
2182 dst.proportional <- src.proportional;
2185 let unent s =
2186 let l = String.length s in
2187 let b = Buffer.create l in
2188 unent b s 0 l;
2189 Buffer.contents b;
2192 let get s =
2193 let h = Hashtbl.create 10 in
2194 let dc = { defconf with angle = defconf.angle } in
2195 let rec toplevel v t spos epos =
2196 match t with
2197 | Vdata | Vcdata | Vend -> v
2198 | Vopen ("llppconfig", attrs, closed) ->
2199 if closed
2200 then v
2201 else { v with f = llppconfig }
2202 | Vopen _ ->
2203 error "unexpected subelement at top level" s spos
2204 | Vclose tag -> error "unexpected close at top level" s spos
2206 and llppconfig v t spos epos =
2207 match t with
2208 | Vdata | Vcdata | Vend -> v
2209 | Vopen ("defaults", attrs, closed) ->
2210 let c = config_of dc attrs in
2211 setconf dc c;
2212 if closed
2213 then v
2214 else { v with f = skip "defaults" (fun () -> v) }
2216 | Vopen ("doc", attrs, closed) ->
2217 let pathent =
2219 List.assoc "path" attrs
2220 with Not_found -> error "doc is missing path attribute" s spos
2222 let path = unent pathent in
2223 let c = config_of dc attrs in
2224 let y =
2226 float_of_string (List.assoc "rely" attrs)
2227 with
2228 | Not_found -> 0.0
2229 | exn ->
2230 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2233 let x =
2235 int_of_string (List.assoc "pan" attrs)
2236 with
2237 | Not_found -> 0
2238 | exn ->
2239 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2242 if closed
2243 then (Hashtbl.add h path (c, [], x, y); v)
2244 else { v with f = doc path x y c [] }
2246 | Vopen (tag, _, closed) ->
2247 error "unexpected subelement in llppconfig" s spos
2249 | Vclose "llppconfig" -> { v with f = toplevel }
2250 | Vclose tag -> error "unexpected close in llppconfig" s spos
2252 and doc path x y c bookmarks v t spos epos =
2253 match t with
2254 | Vdata | Vcdata -> v
2255 | Vend -> error "unexpected end of input in doc" s spos
2256 | Vopen ("bookmarks", attrs, closed) ->
2257 { v with f = pbookmarks path x y c bookmarks }
2259 | Vopen (tag, _, _) ->
2260 error "unexpected subelement in doc" s spos
2262 | Vclose "doc" ->
2263 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2264 { v with f = llppconfig }
2266 | Vclose tag -> error "unexpected close in doc" s spos
2268 and pbookmarks path x y c bookmarks v t spos epos =
2269 match t with
2270 | Vdata | Vcdata -> v
2271 | Vend -> error "unexpected end of input in bookmarks" s spos
2272 | Vopen ("item", attrs, closed) ->
2273 let titleent, spage, srely = bookmark_of attrs in
2274 let page =
2276 int_of_string spage
2277 with exn ->
2278 dolog "Failed to convert page %S to integer: %s"
2279 spage (Printexc.to_string exn);
2282 let rely =
2284 float_of_string srely
2285 with exn ->
2286 dolog "Failed to convert rely %S to real: %s"
2287 srely (Printexc.to_string exn);
2290 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2291 if closed
2292 then { v with f = pbookmarks path x y c bookmarks }
2293 else
2294 let f () = v in
2295 { v with f = skip "item" f }
2297 | Vopen _ ->
2298 error "unexpected subelement in bookmarks" s spos
2300 | Vclose "bookmarks" ->
2301 { v with f = doc path x y c bookmarks }
2303 | Vclose tag -> error "unexpected close in bookmarks" s spos
2305 and skip tag f v t spos epos =
2306 match t with
2307 | Vdata | Vcdata -> v
2308 | Vend ->
2309 error ("unexpected end of input in skipped " ^ tag) s spos
2310 | Vopen (tag', _, closed) ->
2311 if closed
2312 then v
2313 else
2314 let f' () = { v with f = skip tag f } in
2315 { v with f = skip tag' f' }
2316 | Vclose ctag ->
2317 if tag = ctag
2318 then f ()
2319 else error ("unexpected close in skipped " ^ tag) s spos
2322 parse { f = toplevel; accu = () } s;
2323 h, dc;
2326 let do_load f ic =
2328 let len = in_channel_length ic in
2329 let s = String.create len in
2330 really_input ic s 0 len;
2331 f s;
2332 with
2333 | Parse_error (msg, s, pos) ->
2334 let subs = subs s pos in
2335 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2336 failwith ("parse error: " ^ s)
2338 | exn ->
2339 failwith ("config load error: " ^ Printexc.to_string exn)
2342 let path =
2343 let dir =
2345 let dir = Filename.concat home ".config" in
2346 if Sys.is_directory dir then dir else home
2347 with _ -> home
2349 Filename.concat dir "llpp.conf"
2352 let load1 f =
2353 if Sys.file_exists path
2354 then
2355 match
2356 (try Some (open_in_bin path)
2357 with exn ->
2358 prerr_endline
2359 ("Error opening configuation file `" ^ path ^ "': " ^
2360 Printexc.to_string exn);
2361 None
2363 with
2364 | Some ic ->
2365 begin try
2366 f (do_load get ic)
2367 with exn ->
2368 prerr_endline
2369 ("Error loading configuation from `" ^ path ^ "': " ^
2370 Printexc.to_string exn);
2371 end;
2372 close_in ic;
2374 | None -> ()
2375 else
2376 f (Hashtbl.create 0, defconf)
2379 let load () =
2380 let f (h, dc) =
2381 let pc, pb, px, py =
2383 Hashtbl.find h state.path
2384 with Not_found -> dc, [], 0, 0.0
2386 setconf defconf dc;
2387 setconf conf pc;
2388 state.bookmarks <- pb;
2389 state.x <- px;
2390 cbput state.hists.nav py;
2392 load1 f
2395 let add_attrs bb always dc c =
2396 let ob s a b =
2397 if always || a != b
2398 then Printf.bprintf bb "\n %s='%b'" s a
2399 and oi s a b =
2400 if always || a != b
2401 then Printf.bprintf bb "\n %s='%d'" s a
2402 and oz s a b =
2403 if always || a <> b
2404 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2406 let w, h =
2407 if always
2408 then dc.winw, dc.winh
2409 else
2410 match state.fullscreen with
2411 | Some wh -> wh
2412 | None -> c.winw, c.winh
2414 oi "width" w dc.winw;
2415 oi "height" h dc.winh;
2416 oi "scroll-bar-width" c.scrollw dc.scrollw;
2417 oi "scroll-handle-height" c.scrollh dc.scrollh;
2418 ob "case-insensitive-search" c.icase dc.icase;
2419 ob "preload" c.preload dc.preload;
2420 oi "page-bias" c.pagebias dc.pagebias;
2421 oi "scroll-step" c.scrollincr dc.scrollincr;
2422 ob "max-height-fit" c.maxhfit dc.maxhfit;
2423 ob "crop-hack" c.crophack dc.crophack;
2424 ob "throttle" c.showall dc.showall;
2425 ob "highlight-links" c.hlinks dc.hlinks;
2426 ob "under-cursor-info" c.underinfo dc.underinfo;
2427 oi "vertical-margin" c.interpagespace dc.interpagespace;
2428 oz "zoom" c.zoom dc.zoom;
2429 ob "presentation" c.presentation dc.presentation;
2430 oi "rotation-angle" c.angle dc.angle;
2431 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2432 ob "proportional-display" c.proportional dc.proportional;
2433 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2436 let save () =
2437 let bb = Buffer.create 32768 in
2438 let f (h, dc) =
2439 Buffer.add_string bb "<llppconfig>\n<defaults ";
2440 add_attrs bb true dc dc;
2441 Buffer.add_string bb "/>\n";
2443 let adddoc path x y c bookmarks =
2444 if bookmarks == [] && c = dc && y = 0.0
2445 then ()
2446 else (
2447 Printf.bprintf bb "<doc path='%s'"
2448 (enent path 0 (String.length path));
2450 if y <> 0.0
2451 then Printf.bprintf bb " rely='%f'" y;
2453 if x != 0
2454 then Printf.bprintf bb " pan='%d'" x;
2456 add_attrs bb false dc c;
2458 begin match bookmarks with
2459 | [] -> Buffer.add_string bb "/>\n"
2460 | _ ->
2461 Buffer.add_string bb ">\n<bookmarks>\n";
2462 List.iter (fun (title, _level, page, rely) ->
2463 Printf.bprintf bb
2464 "<item title='%s' page='%d' rely='%f'/>\n"
2465 (enent title 0 (String.length title))
2466 page
2467 rely
2468 ) bookmarks;
2469 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2470 end;
2474 adddoc state.path state.x (yratio state.y) conf
2475 (if conf.savebmarks then state.bookmarks else []);
2477 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2478 if path <> state.path
2479 then
2480 adddoc path x y c bookmarks
2481 ) h;
2482 Buffer.add_string bb "</llppconfig>";
2484 load1 f;
2485 if Buffer.length bb > 0
2486 then
2488 let tmp = path ^ ".tmp" in
2489 let oc = open_out_bin tmp in
2490 Buffer.output_buffer oc bb;
2491 close_out oc;
2492 Sys.rename tmp path;
2493 with exn ->
2494 prerr_endline
2495 ("error while saving configuration: " ^ Printexc.to_string exn)
2497 end;;
2499 let () =
2500 Arg.parse
2501 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2502 (fun s -> state.path <- s)
2503 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2505 let path =
2506 if String.length state.path = 0
2507 then (prerr_endline "filename missing"; exit 1)
2508 else (
2509 if Filename.is_relative state.path
2510 then Filename.concat (Sys.getcwd ()) state.path
2511 else state.path
2514 state.path <- path;
2516 State.load ();
2518 let _ = Glut.init Sys.argv in
2519 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2520 let () = Glut.initWindowSize conf.winw conf.winh in
2521 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2523 let csock, ssock =
2524 if Sys.os_type = "Unix"
2525 then
2526 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2527 else
2528 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2529 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2530 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2531 Unix.bind sock addr;
2532 Unix.listen sock 1;
2533 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2534 Unix.connect csock addr;
2535 let ssock, _ = Unix.accept sock in
2536 Unix.close sock;
2537 let opts sock =
2538 Unix.setsockopt sock Unix.TCP_NODELAY true;
2539 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2541 opts ssock;
2542 opts csock;
2543 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2544 ssock, csock
2547 let () = Glut.displayFunc display in
2548 let () = Glut.reshapeFunc reshape in
2549 let () = Glut.keyboardFunc keyboard in
2550 let () = Glut.specialFunc special in
2551 let () = Glut.idleFunc (Some idle) in
2552 let () = Glut.mouseFunc mouse in
2553 let () = Glut.motionFunc motion in
2554 let () = Glut.passiveMotionFunc pmotion in
2556 init ssock;
2557 state.csock <- csock;
2558 state.ssock <- ssock;
2559 state.text <- "Opening " ^ path;
2560 writeopen state.path state.password;
2562 at_exit State.save;
2564 let rec handlelablglutbug () =
2566 Glut.mainLoop ();
2567 with Glut.BadEnum "key in special_of_int" ->
2568 showtext '!' " LablGlut bug: special key not recognized";
2569 handlelablglutbug ()
2571 handlelablglutbug ();