Remove stray space
[llpp.git] / main.ml
blob634be91b553584dec2b8f54b012da35a51eff0b7
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 type params = angle * proportional * texcount * sliceheight
12 and pageno = int
13 and width = int
14 and height = int
15 and leftx = int
16 and opaque = string
17 and recttype = int
18 and pixmapsize = int
19 and angle = int
20 and proportional = bool
21 and interpagespace = int
22 and texcount = int
23 and sliceheight = int
24 and gen = int
25 and top = float
28 external init : Unix.file_descr -> params -> unit = "ml_init";;
29 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext : string -> (int * int * int * int) -> int -> unit =
31 "ml_seltext";;
32 external copysel : string -> unit = "ml_copysel";;
33 external getpdimrect : int -> float array = "ml_getpdimrect";;
34 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
35 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
37 type mpos = int * int
38 and mstate =
39 | Msel of (mpos * mpos)
40 | Mpan of mpos
41 | Mscroll
42 | Mnone
45 type 'a circbuf =
46 { store : 'a array
47 ; mutable rc : int
48 ; mutable wc : int
49 ; mutable len : int
53 type textentry = (char * string * onhist * onkey * ondone)
54 and onkey = string -> int -> te
55 and ondone = string -> unit
56 and histcancel = unit -> unit
57 and onhist = ((histcmd -> string) * histcancel) option
58 and histcmd = HCnext | HCprev | HCfirst | HClast
59 and te =
60 | TEstop
61 | TEdone of string
62 | TEcont of string
63 | TEswitch of textentry
66 let cbnew n v =
67 { store = Array.create n v
68 ; rc = 0
69 ; wc = 0
70 ; len = 0
74 let cbcap b = Array.length b.store;;
76 let cbput b v =
77 let cap = cbcap b in
78 b.store.(b.wc) <- v;
79 b.wc <- (b.wc + 1) mod cap;
80 b.rc <- b.wc;
81 b.len <- min (b.len + 1) cap;
84 let cbempty b = b.len = 0;;
86 let cbgetg b circular dir =
87 if cbempty b
88 then b.store.(0)
89 else
90 let rc = b.rc + dir in
91 let rc =
92 if circular
93 then (
94 if rc = -1
95 then b.len-1
96 else (
97 if rc = b.len
98 then 0
99 else rc
102 else max 0 (min rc (b.len-1))
104 b.rc <- rc;
105 b.store.(rc);
108 let cbget b = cbgetg b false;;
109 let cbgetc b = cbgetg b true;;
111 let cbpeek b =
112 let rc = b.wc - b.len in
113 let rc = if rc < 0 then cbcap b + rc else rc in
114 b.store.(rc);
117 let cbdecr b = b.len <- b.len - 1;;
119 type layout =
120 { pageno : int
121 ; pagedimno : int
122 ; pagew : int
123 ; pageh : int
124 ; pagedispy : int
125 ; pagey : int
126 ; pagevh : int
127 ; pagex : int
131 type conf =
132 { mutable scrollw : int
133 ; mutable scrollh : int
134 ; mutable icase : bool
135 ; mutable preload : bool
136 ; mutable pagebias : int
137 ; mutable verbose : bool
138 ; mutable scrollincr : int
139 ; mutable maxhfit : bool
140 ; mutable crophack : bool
141 ; mutable autoscroll : bool
142 ; mutable showall : bool
143 ; mutable hlinks : bool
144 ; mutable underinfo : bool
145 ; mutable interpagespace : interpagespace
146 ; mutable zoom : float
147 ; mutable presentation : bool
148 ; mutable angle : angle
149 ; mutable winw : int
150 ; mutable winh : int
151 ; mutable savebmarks : bool
152 ; mutable proportional : proportional
153 ; mutable memlimit : int
154 ; mutable texcount : texcount
155 ; mutable sliceheight : sliceheight
156 ; mutable thumbw : width
160 type outline = string * int * int * float;;
161 type outlines =
162 | Oarray of outline array
163 | Olist of outline list
164 | Onarrow of string * outline array * outline array
167 type rect = (float * float * float * float * float * float * float * float);;
169 type pagemapkey = (pageno * width * angle * proportional * gen);;
171 type anchor = pageno * top;;
173 type state =
174 { mutable csock : Unix.file_descr
175 ; mutable ssock : Unix.file_descr
176 ; mutable w : int
177 ; mutable x : int
178 ; mutable y : int
179 ; mutable anchor : anchor
180 ; mutable maxy : int
181 ; mutable layout : layout list
182 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
183 ; mutable pdims : (pageno * width * height * leftx) list
184 ; mutable pagecount : int
185 ; pagecache : string circbuf
186 ; mutable rendering : bool
187 ; mutable mstate : mstate
188 ; mutable searchpattern : string
189 ; mutable rects : (pageno * recttype * rect) list
190 ; mutable rects1 : (pageno * recttype * rect) list
191 ; mutable text : string
192 ; mutable fullscreen : (width * height) option
193 ; mutable birdseye : (conf * leftx * pageno * pageno) option
194 ; mutable textentry : textentry option
195 ; mutable outlines : outlines
196 ; mutable outline : (bool * int * int * outline array * string) option
197 ; mutable bookmarks : outline list
198 ; mutable path : string
199 ; mutable password : string
200 ; mutable invalidated : int
201 ; mutable colorscale : float
202 ; mutable memused : int
203 ; mutable gen : gen
204 ; mutable throttle : layout list option
205 ; hists : hists
207 and hists =
208 { pat : string circbuf
209 ; pag : string circbuf
210 ; nav : anchor circbuf
214 let defconf =
215 { scrollw = 7
216 ; scrollh = 12
217 ; icase = true
218 ; preload = true
219 ; pagebias = 0
220 ; verbose = false
221 ; scrollincr = 24
222 ; maxhfit = true
223 ; crophack = false
224 ; autoscroll = false
225 ; showall = false
226 ; hlinks = false
227 ; underinfo = false
228 ; interpagespace = 2
229 ; zoom = 1.0
230 ; presentation = false
231 ; angle = 0
232 ; winw = 900
233 ; winh = 900
234 ; savebmarks = true
235 ; proportional = true
236 ; memlimit = 32*1024*1024
237 ; texcount = 256
238 ; sliceheight = 24
239 ; thumbw = 76
243 let conf = { defconf with angle = defconf.angle };;
245 let state =
246 { csock = Unix.stdin
247 ; ssock = Unix.stdin
248 ; x = 0
249 ; y = 0
250 ; anchor = (0, 0.0)
251 ; w = 0
252 ; layout = []
253 ; maxy = max_int
254 ; pagemap = Hashtbl.create 10
255 ; pagecache = cbnew 100 ""
256 ; pdims = []
257 ; pagecount = 0
258 ; rendering = false
259 ; mstate = Mnone
260 ; rects = []
261 ; rects1 = []
262 ; text = ""
263 ; fullscreen = None
264 ; birdseye = None
265 ; textentry = None
266 ; searchpattern = ""
267 ; outlines = Olist []
268 ; outline = None
269 ; bookmarks = []
270 ; path = ""
271 ; password = ""
272 ; invalidated = 0
273 ; hists =
274 { nav = cbnew 100 (0, 0.0)
275 ; pat = cbnew 20 ""
276 ; pag = cbnew 10 ""
278 ; colorscale = 1.0
279 ; memused = 0
280 ; gen = 0
281 ; throttle = None
285 let vlog fmt =
286 if conf.verbose
287 then
288 Printf.kprintf prerr_endline fmt
289 else
290 Printf.kprintf ignore fmt
293 let writecmd fd s =
294 let len = String.length s in
295 let n = 4 + len in
296 let b = Buffer.create n in
297 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
298 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
299 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
300 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
301 Buffer.add_string b s;
302 let s' = Buffer.contents b in
303 let n' = Unix.write fd s' 0 n in
304 if n' != n then failwith "write failed";
307 let readcmd fd =
308 let s = "xxxx" in
309 let n = Unix.read fd s 0 4 in
310 if n != 4 then failwith "incomplete read(len)";
311 let len = 0
312 lor (Char.code s.[0] lsl 24)
313 lor (Char.code s.[1] lsl 16)
314 lor (Char.code s.[2] lsl 8)
315 lor (Char.code s.[3] lsl 0)
317 let s = String.create len in
318 let n = Unix.read fd s 0 len in
319 if n != len then failwith "incomplete read(data)";
323 let makecmd s l =
324 let b = Buffer.create 10 in
325 Buffer.add_string b s;
326 let rec combine = function
327 | [] -> b
328 | x :: xs ->
329 Buffer.add_char b ' ';
330 let s =
331 match x with
332 | `b b -> if b then "1" else "0"
333 | `s s -> s
334 | `i i -> string_of_int i
335 | `f f -> string_of_float f
336 | `I f -> string_of_int (truncate f)
338 Buffer.add_string b s;
339 combine xs;
341 combine l;
344 let wcmd s l =
345 let cmd = Buffer.contents (makecmd s l) in
346 writecmd state.csock cmd;
349 let calcips h =
350 if conf.presentation
351 then
352 let d = conf.winh - h in
353 max 0 ((d + 1) / 2)
354 else
355 conf.interpagespace
358 let calcheight () =
359 let rec f pn ph pi fh l =
360 match l with
361 | (n, _, h, _) :: rest ->
362 let ips = calcips h in
363 let fh =
364 if conf.presentation
365 then fh+ips
366 else (
367 if state.birdseye <> None && pn = 0
368 then fh + ips
369 else fh
372 let fh = fh + ((n - pn) * (ph + pi)) in
373 f n h ips fh rest
375 | [] ->
376 let inc =
377 if conf.presentation || (state.birdseye <> None && pn = 0)
378 then 0
379 else -pi
381 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
382 max 0 fh
384 let fh = f 0 0 0 0 state.pdims in
388 let getpageyh pageno =
389 let rec f pn ph pi y l =
390 match l with
391 | (n, _, h, _) :: rest ->
392 let ips = calcips h in
393 if n >= pageno
394 then
395 if conf.presentation && n = pageno
396 then
397 y + (pageno - pn) * (ph + pi) + pi, h
398 else
399 y + (pageno - pn) * (ph + pi), h
400 else
401 let y = y + (if conf.presentation then pi else 0) in
402 let y = y + (n - pn) * (ph + pi) in
403 f n h ips y rest
405 | [] ->
406 y + (pageno - pn) * (ph + pi), ph
408 f 0 0 0 0 state.pdims
411 let getpagey pageno = fst (getpageyh pageno);;
413 let layout y sh =
414 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
415 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
416 match pdims with
417 | (pageno', w, h, x) :: rest when pageno' = pageno ->
418 let ips = calcips h in
419 let yinc =
420 if conf.presentation || (state.birdseye <> None && pageno = 0)
421 then ips
422 else 0
424 (w, h, ips, x), rest, pdimno + 1, yinc
425 | _ ->
426 prev, pdims, pdimno, 0
428 let dy = dy + yinc in
429 let py = py + yinc in
430 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
431 then
432 accu
433 else
434 let vy = y + dy in
435 if py + h <= vy - yinc
436 then
437 let py = py + h + ips in
438 let dy = max 0 (py - y) in
439 f ~pageno:(pageno+1)
440 ~pdimno
441 ~prev:curr
444 ~pdims:rest
445 ~cacheleft
446 ~accu
447 else
448 let pagey = vy - py in
449 let pagevh = h - pagey in
450 let pagevh = min (sh - dy) pagevh in
451 let off = if yinc > 0 then py - vy else 0 in
452 let py = py + h + ips in
453 let e =
454 { pageno = pageno
455 ; pagedimno = pdimno
456 ; pagew = w
457 ; pageh = h
458 ; pagedispy = dy + off
459 ; pagey = pagey + off
460 ; pagevh = pagevh - off
461 ; pagex = x
464 let accu = e :: accu in
465 f ~pageno:(pageno+1)
466 ~pdimno
467 ~prev:curr
469 ~dy:(dy+pagevh+ips)
470 ~pdims:rest
471 ~cacheleft:(cacheleft-1)
472 ~accu
474 if state.invalidated = 0
475 then (
476 let accu =
478 ~pageno:0
479 ~pdimno:~-1
480 ~prev:(0,0,0,0)
481 ~py:0
482 ~dy:0
483 ~pdims:state.pdims
484 ~cacheleft:(cbcap state.pagecache)
485 ~accu:[]
487 List.rev accu
489 else
493 let clamp incr =
494 let y = state.y + incr in
495 let y = max 0 y in
496 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
500 let getopaque pageno =
501 try Some (Hashtbl.find state.pagemap
502 (pageno, state.w, conf.angle, conf.proportional, state.gen))
503 with Not_found -> None
506 let cache pageno opaque =
507 Hashtbl.replace state.pagemap
508 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
511 let validopaque opaque = String.length opaque > 0;;
513 let render l =
514 match getopaque l.pageno with
515 | None when not state.rendering ->
516 state.rendering <- true;
517 cache l.pageno ("", -1);
518 wcmd "render" [`i (l.pageno + 1)
519 ;`i l.pagedimno
520 ;`i l.pagew
521 ;`i l.pageh];
523 | _ -> ()
526 let loadlayout layout =
527 let rec f all = function
528 | l :: ls ->
529 begin match getopaque l.pageno with
530 | None -> render l; f false ls
531 | Some (opaque, _) -> f (all && validopaque opaque) ls
533 | [] -> all
535 f (layout <> []) layout;
538 let findpageforopaque opaque =
539 Hashtbl.fold
540 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
541 state.pagemap None
544 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
546 let preload () =
547 let oktopreload =
548 if conf.preload
549 then
550 let memleft = conf.memlimit - state.memused in
551 if memleft < 0
552 then
553 let opaque = cbpeek state.pagecache in
554 match findpageforopaque opaque with
555 | Some ((n, _, _, _, _), size) ->
556 memleft + size >= 0 && not (pagevisible state.layout n)
557 | None -> false
558 else true
559 else false
561 if oktopreload
562 then
563 let presentation = conf.presentation in
564 let interpagespace = conf.interpagespace in
565 let maxy = state.maxy in
566 conf.presentation <- false;
567 conf.interpagespace <- 0;
568 state.maxy <- calcheight ();
569 let y =
570 match state.layout with
571 | [] -> 0
572 | l :: _ -> getpagey l.pageno
574 let y = if y < conf.winh then 0 else y - conf.winh in
575 let pages = layout y (conf.winh*3) in
576 List.iter render pages;
577 conf.presentation <- presentation;
578 conf.interpagespace <- interpagespace;
579 state.maxy <- maxy;
582 let gotoy y =
583 let y = max 0 y in
584 let y = min state.maxy y in
585 let pages = layout y conf.winh in
586 let ready = loadlayout pages in
587 if conf.showall
588 then (
589 if ready
590 then (
591 state.y <- y;
592 state.layout <- pages;
593 state.throttle <- None;
594 Glut.postRedisplay ();
596 else (
597 state.throttle <- Some pages;
600 else (
601 state.y <- y;
602 state.layout <- pages;
603 state.throttle <- None;
604 Glut.postRedisplay ();
606 begin match state.birdseye with
607 | Some (conf, leftx, pageno, hooverpageno) ->
608 if not (pagevisible pages pageno)
609 then (
610 match state.layout with
611 | [] -> ()
612 | l :: _ ->
613 state.birdseye <- Some (conf, leftx, l.pageno, hooverpageno)
615 | _ -> ()
616 end;
617 preload ();
620 let gotoy_and_clear_text y =
621 gotoy y;
622 if not conf.verbose then state.text <- "";
625 let emptyanchor = (0, 0.0);;
627 let getanchor () =
628 match state.layout with
629 | [] -> emptyanchor
630 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
633 let getanchory (n, top) =
634 let y, h = getpageyh n in
635 y + (truncate (top *. float h));
638 let gotoanchor anchor =
639 gotoy (getanchory anchor);
642 let addnav () =
643 cbput state.hists.nav (getanchor ());
646 let getnav () =
647 let anchor = cbgetc state.hists.nav ~-1 in
648 getanchory anchor;
651 let gotopage n top =
652 let y, h = getpageyh n in
653 addnav ();
654 gotoy_and_clear_text (y + (truncate (top *. float h)));
657 let gotopage1 n top =
658 let y = getpagey n in
659 addnav ();
660 gotoy_and_clear_text (y + top);
663 let invalidate () =
664 state.layout <- [];
665 state.pdims <- [];
666 state.rects <- [];
667 state.rects1 <- [];
668 state.invalidated <- state.invalidated + 1;
671 let scalecolor c =
672 let c = c *. state.colorscale in
673 (c, c, c);
676 let represent () =
677 state.maxy <- calcheight ();
678 gotoanchor state.anchor;
681 let pagematrix () =
682 GlMat.mode `projection;
683 GlMat.load_identity ();
684 GlMat.rotate ~x:1.0 ~angle:180.0 ();
685 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
686 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
689 let winmatrix () =
690 GlMat.mode `projection;
691 GlMat.load_identity ();
692 GlMat.rotate ~x:1.0 ~angle:180.0 ();
693 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
694 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
697 let reshape ~w ~h =
698 if state.invalidated = 0
699 then state.anchor <- getanchor ();
701 conf.winw <- w;
702 let w = truncate (float w *. conf.zoom) - conf.scrollw in
703 let w = max w 2 in
704 state.w <- w;
705 conf.winh <- h;
706 GlMat.mode `modelview;
707 GlMat.load_identity ();
708 GlClear.color (scalecolor 1.0);
709 GlClear.clear [`color];
711 invalidate ();
712 wcmd "geometry" [`i w; `i h];
715 let showtext c s =
716 GlDraw.color (0.0, 0.0, 0.0);
717 GlDraw.rect
718 (0.0, float (conf.winh - 18))
719 (float (conf.winw - conf.scrollw - 1), float conf.winh)
721 let font = Glut.BITMAP_8_BY_13 in
722 GlDraw.color (1.0, 1.0, 1.0);
723 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
724 Glut.bitmapCharacter ~font ~c:(Char.code c);
725 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
728 let enttext () =
729 let len = String.length state.text in
730 match state.textentry with
731 | None ->
732 if len > 0 then showtext ' ' state.text
734 | Some (c, text, _, _, _) ->
735 let s =
736 if len > 0
737 then
738 text ^ " [" ^ state.text ^ "]"
739 else
740 text
742 showtext c s;
745 let showtext c s =
746 if true
747 then (
748 state.text <- Printf.sprintf "%c%s" c s;
749 Glut.postRedisplay ();
751 else (
752 showtext c s;
753 Glut.swapBuffers ();
757 let act cmd =
758 match cmd.[0] with
759 | 'c' ->
760 state.pdims <- [];
762 | 'D' ->
763 state.rects <- state.rects1;
764 Glut.postRedisplay ()
766 | 'C' ->
767 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
768 state.pagecount <- n;
769 state.invalidated <- state.invalidated - 1;
770 if state.invalidated = 0
771 then represent ()
773 | 't' ->
774 let s = Scanf.sscanf cmd "t %n"
775 (fun n -> String.sub cmd n (String.length cmd - n))
777 Glut.setWindowTitle s
779 | 'T' ->
780 let s = Scanf.sscanf cmd "T %n"
781 (fun n -> String.sub cmd n (String.length cmd - n))
783 if state.textentry = None
784 then (
785 state.text <- s;
786 showtext ' ' s;
788 else (
789 state.text <- s;
790 Glut.postRedisplay ();
793 | 'V' ->
794 if conf.verbose
795 then
796 let s = Scanf.sscanf cmd "V %n"
797 (fun n -> String.sub cmd n (String.length cmd - n))
799 state.text <- s;
800 showtext ' ' s;
802 | 'F' ->
803 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
804 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
805 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
806 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
808 let y = (getpagey pageno) + truncate y0 in
809 addnav ();
810 gotoy y;
811 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
813 | 'R' ->
814 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
815 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
816 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
817 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
819 state.rects1 <-
820 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
822 | 'r' ->
823 let n, w, h, r, l, s, p =
824 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
825 (fun n w h r l s p ->
826 (n-1, w, h, r, l != 0, s, p))
829 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
830 state.memused <- state.memused + s;
832 let layout =
833 match state.throttle with
834 | None -> state.layout
835 | Some layout -> layout
838 let rec gc () =
839 if (state.memused <= conf.memlimit) || cbempty state.pagecache
840 then ()
841 else (
842 let evictedopaque = cbpeek state.pagecache in
843 match findpageforopaque evictedopaque with
844 | None -> failwith "bug in gc"
845 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
846 if state.gen != gen || not (pagevisible layout evictedn)
847 then (
848 wcmd "free" [`s evictedopaque];
849 state.memused <- state.memused - evictedsize;
850 Hashtbl.remove state.pagemap k;
851 cbdecr state.pagecache;
852 gc ();
856 gc ();
858 cbput state.pagecache p;
859 state.rendering <- false;
861 begin match state.throttle with
862 | None ->
863 if pagevisible state.layout n
864 then gotoy state.y
865 else (
866 let allvisible = loadlayout state.layout in
867 if allvisible then preload ();
870 | Some layout ->
871 match layout with
872 | [] -> ()
873 | l :: _ ->
874 let y = getpagey l.pageno + l.pagey in
875 gotoy y
878 | 'l' ->
879 let (n, w, h, x) as pdim =
880 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
882 state.pdims <- pdim :: state.pdims
884 | 'o' ->
885 let (l, n, t, h, pos) =
886 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
888 let s = String.sub cmd pos (String.length cmd - pos) in
889 let s =
890 let l = String.length s in
891 let b = Buffer.create (String.length s) in
892 let rec loop pc2 i =
893 if i = l
894 then ()
895 else
896 let pc2 =
897 match s.[i] with
898 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
899 | '\xc2' -> true
900 | c ->
901 let c = if Char.code c land 0x80 = 0 then c else '?' in
902 Buffer.add_char b c;
903 false
905 loop pc2 (i+1)
907 loop false 0;
908 Buffer.contents b
910 let outline = (s, l, n, float t /. float h) in
911 let outlines =
912 match state.outlines with
913 | Olist outlines -> Olist (outline :: outlines)
914 | Oarray _ -> Olist [outline]
915 | Onarrow _ -> Olist [outline]
917 state.outlines <- outlines
919 | _ ->
920 log "unknown cmd `%S'" cmd
923 let now = Unix.gettimeofday;;
925 let idle () =
926 let rec loop delay =
927 let r, _, _ = Unix.select [state.csock] [] [] delay in
928 begin match r with
929 | [] ->
930 if conf.autoscroll
931 then begin
932 let y = state.y + conf.scrollincr in
933 let y = if y >= state.maxy then 0 else y in
934 gotoy y;
935 state.text <- "";
936 end;
938 | _ ->
939 let cmd = readcmd state.csock in
940 act cmd;
941 loop 0.0
942 end;
943 in loop 0.001
946 let onhist cb =
947 let rc = cb.rc in
948 let action = function
949 | HCprev -> cbget cb ~-1
950 | HCnext -> cbget cb 1
951 | HCfirst -> cbget cb ~-(cb.rc)
952 | HClast -> cbget cb (cb.len - 1 - cb.rc)
953 and cancel () = cb.rc <- rc
954 in (action, cancel)
957 let search pattern forward =
958 if String.length pattern > 0
959 then
960 let pn, py =
961 match state.layout with
962 | [] -> 0, 0
963 | l :: _ ->
964 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
966 let cmd =
967 let b = makecmd "search"
968 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
970 Buffer.add_char b ',';
971 Buffer.add_string b pattern;
972 Buffer.add_char b '\000';
973 Buffer.contents b;
975 writecmd state.csock cmd;
978 let intentry text key =
979 let c = Char.unsafe_chr key in
980 match c with
981 | '0' .. '9' ->
982 let s = "x" in s.[0] <- c;
983 let text = text ^ s in
984 TEcont text
986 | _ ->
987 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
988 TEcont text
991 let addchar s c =
992 let b = Buffer.create (String.length s + 1) in
993 Buffer.add_string b s;
994 Buffer.add_char b c;
995 Buffer.contents b;
998 let textentry text key =
999 let c = Char.unsafe_chr key in
1000 match c with
1001 | _ when key >= 32 && key < 127 ->
1002 let text = addchar text c in
1003 TEcont text
1005 | _ ->
1006 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1007 TEcont text
1010 let reinit angle proportional =
1011 conf.angle <- angle;
1012 conf.proportional <- proportional;
1013 invalidate ();
1014 wcmd "reinit" [`i angle; `b proportional];
1017 let optentry text key =
1018 let btos b = if b then "on" else "off" in
1019 let c = Char.unsafe_chr key in
1020 match c with
1021 | 's' ->
1022 let ondone s =
1023 try conf.scrollincr <- int_of_string s with exc ->
1024 state.text <- Printf.sprintf "bad integer `%s': %s"
1025 s (Printexc.to_string exc)
1027 TEswitch ('#', "", None, intentry, ondone)
1029 | 'R' ->
1030 let ondone s =
1031 match try
1032 Some (int_of_string s)
1033 with exc ->
1034 state.text <- Printf.sprintf "bad integer `%s': %s"
1035 s (Printexc.to_string exc);
1036 None
1037 with
1038 | Some angle -> reinit angle conf.proportional
1039 | None -> ()
1041 TEswitch ('^', "", None, intentry, ondone)
1043 | 'i' ->
1044 conf.icase <- not conf.icase;
1045 TEdone ("case insensitive search " ^ (btos conf.icase))
1047 | 'p' ->
1048 conf.preload <- not conf.preload;
1049 gotoy state.y;
1050 TEdone ("preload " ^ (btos conf.preload))
1052 | 'v' ->
1053 conf.verbose <- not conf.verbose;
1054 TEdone ("verbose " ^ (btos conf.verbose))
1056 | 'h' ->
1057 conf.maxhfit <- not conf.maxhfit;
1058 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1059 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1061 | 'c' ->
1062 conf.crophack <- not conf.crophack;
1063 TEdone ("crophack " ^ btos conf.crophack)
1065 | 'a' ->
1066 conf.showall <- not conf.showall;
1067 TEdone ("showall " ^ btos conf.showall)
1069 | 'f' ->
1070 conf.underinfo <- not conf.underinfo;
1071 TEdone ("underinfo " ^ btos conf.underinfo)
1073 | 'P' ->
1074 conf.savebmarks <- not conf.savebmarks;
1075 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1077 | 'S' ->
1078 let ondone s =
1080 let pageno, py =
1081 match state.layout with
1082 | [] -> 0, 0
1083 | l :: _ ->
1084 l.pageno, l.pagey
1086 conf.interpagespace <- int_of_string s;
1087 state.maxy <- calcheight ();
1088 let y = getpagey pageno in
1089 gotoy (y + py)
1090 with exc ->
1091 state.text <- Printf.sprintf "bad integer `%s': %s"
1092 s (Printexc.to_string exc)
1094 TEswitch ('%', "", None, intentry, ondone)
1096 | 'l' ->
1097 reinit conf.angle (not conf.proportional);
1098 TEdone ("proprortional display " ^ btos conf.proportional)
1100 | _ ->
1101 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1102 TEstop
1105 let maxoutlinerows () = (conf.winh - 31) / 16;;
1107 let enterselector allowdel outlines errmsg msg =
1108 if Array.length outlines = 0
1109 then (
1110 showtext ' ' errmsg;
1112 else (
1113 state.text <- msg;
1114 Glut.setCursor Glut.CURSOR_INHERIT;
1115 let pageno =
1116 match state.layout with
1117 | [] -> -1
1118 | {pageno=pageno} :: rest -> pageno
1120 let active =
1121 let rec loop n =
1122 if n = Array.length outlines
1123 then 0
1124 else
1125 let (_, _, outlinepageno, _) = outlines.(n) in
1126 if outlinepageno >= pageno then n else loop (n+1)
1128 loop 0
1130 state.outline <-
1131 Some (allowdel, active,
1132 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1133 Glut.postRedisplay ();
1137 let enteroutlinemode () =
1138 let outlines, msg =
1139 match state.outlines with
1140 | Oarray a -> a, ""
1141 | Olist l ->
1142 let a = Array.of_list (List.rev l) in
1143 state.outlines <- Oarray a;
1144 a, ""
1145 | Onarrow (pat, a, b) ->
1146 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1148 enterselector false outlines "Document has no outline" msg;
1151 let enterbookmarkmode () =
1152 let bookmarks = Array.of_list state.bookmarks in
1153 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1156 let quickbookmark ?title () =
1157 match state.layout with
1158 | [] -> ()
1159 | l :: _ ->
1160 let title =
1161 match title with
1162 | None ->
1163 let sec = Unix.gettimeofday () in
1164 let tm = Unix.localtime sec in
1165 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1166 (l.pageno+1)
1167 tm.Unix.tm_mday
1168 tm.Unix.tm_mon
1169 (tm.Unix.tm_year + 1900)
1170 tm.Unix.tm_hour
1171 tm.Unix.tm_min
1172 | Some title -> title
1174 state.bookmarks <-
1175 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1178 let doreshape w h =
1179 state.fullscreen <- None;
1180 Glut.reshapeWindow w h;
1183 let writeopen path password =
1184 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1187 let opendoc path password =
1188 invalidate ();
1189 state.path <- path;
1190 state.password <- password;
1191 state.gen <- state.gen + 1;
1193 writeopen path password;
1194 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1195 wcmd "geometry" [`i state.w; `i conf.winh];
1198 let birdseyeon () =
1199 let zoom = float conf.thumbw /. float conf.winw in
1200 let (birdseyepageno, _) as anchor = getanchor () in
1201 state.anchor <- anchor;
1202 state.birdseye <-
1203 Some ({ conf with zoom = conf.zoom }, state.x, birdseyepageno, -1);
1204 conf.zoom <- zoom;
1205 conf.presentation <- false;
1206 conf.interpagespace <- 10;
1207 conf.hlinks <- false;
1208 state.x <- 0;
1209 state.mstate <- Mnone;
1210 conf.showall <- false;
1211 Glut.setCursor Glut.CURSOR_INHERIT;
1212 if conf.verbose
1213 then
1214 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1215 (100.0*.zoom)
1216 else
1217 state.text <- ""
1221 let birdseyeoff (c, leftx, pageno, _) =
1222 state.birdseye <- None;
1223 conf.zoom <- c.zoom;
1224 conf.presentation <- c.presentation;
1225 conf.interpagespace <- c.interpagespace;
1226 conf.showall <- c.showall;
1227 conf.hlinks <- c.hlinks;
1228 state.x <- leftx;
1229 if conf.verbose
1230 then
1231 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1232 (100.0*.conf.zoom)
1236 let togglebirdseye () =
1237 match state.birdseye with
1238 | None -> birdseyeon ()
1239 | Some vals -> birdseyeoff vals
1242 let viewkeyboard ~key ~x ~y =
1243 let enttext te =
1244 state.textentry <- te;
1245 state.text <- "";
1246 enttext ();
1247 Glut.postRedisplay ()
1249 match state.textentry with
1250 | None ->
1251 let c = Char.chr key in
1252 begin match c with
1253 | '\027' | 'q' ->
1254 exit 0
1256 | '\008' ->
1257 let y = getnav () in
1258 gotoy_and_clear_text y
1260 | '\013' ->
1261 begin match state.birdseye with
1262 | None -> ()
1263 | Some ((_, _, pageno, _) as vals) ->
1264 addnav ();
1265 birdseyeoff vals;
1266 reshape conf.winw conf.winh;
1267 state.anchor <- (pageno, 0.0);
1268 end;
1270 | 'o' ->
1271 enteroutlinemode ()
1273 | 'u' ->
1274 state.rects <- [];
1275 state.text <- "";
1276 Glut.postRedisplay ()
1278 | '/' | '?' ->
1279 let ondone isforw s =
1280 cbput state.hists.pat s;
1281 state.searchpattern <- s;
1282 search s isforw
1284 enttext (Some (c, "", Some (onhist state.hists.pat),
1285 textentry, ondone (c ='/')))
1287 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1288 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1289 conf.zoom <- min 2.2 (conf.zoom +. incr);
1290 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1291 reshape conf.winw conf.winh
1293 | '+' ->
1294 let ondone s =
1295 let n =
1296 try int_of_string s with exc ->
1297 state.text <- Printf.sprintf "bad integer `%s': %s"
1298 s (Printexc.to_string exc);
1299 max_int
1301 if n != max_int
1302 then (
1303 conf.pagebias <- n;
1304 state.text <- "page bias is now " ^ string_of_int n;
1307 enttext (Some ('+', "", None, intentry, ondone))
1309 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1310 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1311 conf.zoom <- max 0.01 (conf.zoom -. decr);
1312 if conf.zoom <= 1.0 then state.x <- 0;
1313 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1314 reshape conf.winw conf.winh;
1316 | '-' ->
1317 let ondone msg =
1318 state.text <- msg;
1320 enttext (Some ('-', "", None, optentry, ondone))
1322 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1323 state.x <- 0;
1324 conf.zoom <- 1.0;
1325 state.text <- "zoom is 100%";
1326 reshape conf.winw conf.winh
1328 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1329 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1330 if zoom < 1.0
1331 then (
1332 conf.zoom <- zoom;
1333 state.x <- 0;
1334 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1335 reshape conf.winw conf.winh;
1338 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1339 togglebirdseye ();
1340 reshape conf.winw conf.winh;
1342 | '0' .. '9' ->
1343 let ondone s =
1344 let n =
1345 try int_of_string s with exc ->
1346 state.text <- Printf.sprintf "bad integer `%s': %s"
1347 s (Printexc.to_string exc);
1350 if n >= 0
1351 then (
1352 addnav ();
1353 cbput state.hists.pag (string_of_int n);
1354 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1357 let pageentry text key =
1358 match Char.unsafe_chr key with
1359 | 'g' -> TEdone text
1360 | _ -> intentry text key
1362 let text = "x" in text.[0] <- c;
1363 enttext (Some (':', text, Some (onhist state.hists.pag),
1364 pageentry, ondone))
1366 | 'b' ->
1367 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1368 reshape conf.winw conf.winh;
1370 | 'l' ->
1371 conf.hlinks <- not conf.hlinks;
1372 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1373 Glut.postRedisplay ()
1375 | 'a' ->
1376 conf.autoscroll <- not conf.autoscroll
1378 | 'P' ->
1379 conf.presentation <- not conf.presentation;
1380 showtext ' ' ("presentation mode " ^
1381 if conf.presentation then "on" else "off");
1382 represent ()
1384 | 'f' ->
1385 begin match state.fullscreen with
1386 | None ->
1387 state.fullscreen <- Some (conf.winw, conf.winh);
1388 Glut.fullScreen ()
1389 | Some (w, h) ->
1390 state.fullscreen <- None;
1391 doreshape w h
1394 | 'g' ->
1395 gotoy_and_clear_text 0
1397 | 'n' ->
1398 search state.searchpattern true
1400 | 'p' | 'N' ->
1401 search state.searchpattern false
1403 | 't' ->
1404 begin match state.layout with
1405 | [] -> ()
1406 | l :: _ ->
1407 gotoy_and_clear_text (getpagey l.pageno)
1410 | ' ' ->
1411 begin match List.rev state.layout with
1412 | [] -> ()
1413 | l :: _ ->
1414 let pageno = min (l.pageno+1) (state.pagecount-1) in
1415 gotoy_and_clear_text (getpagey pageno)
1418 | '\127' ->
1419 begin match state.layout with
1420 | [] -> ()
1421 | l :: _ ->
1422 let pageno = max 0 (l.pageno-1) in
1423 gotoy_and_clear_text (getpagey pageno)
1426 | '=' ->
1427 let f (fn, ln) l =
1428 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1430 let fn, ln = List.fold_left f (-1, -1) state.layout in
1431 let s =
1432 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1433 let percent =
1434 if maxy <= 0
1435 then 100.
1436 else (100. *. (float state.y /. float maxy)) in
1437 if fn = ln
1438 then
1439 Printf.sprintf "Page %d of %d %.2f%%"
1440 (fn+1) state.pagecount percent
1441 else
1442 Printf.sprintf
1443 "Pages %d-%d of %d %.2f%%"
1444 (fn+1) (ln+1) state.pagecount percent
1446 showtext ' ' s;
1448 | 'w' ->
1449 begin match state.layout with
1450 | [] -> ()
1451 | l :: _ ->
1452 doreshape (l.pagew + conf.scrollw) l.pageh;
1453 Glut.postRedisplay ();
1456 | '\'' ->
1457 enterbookmarkmode ()
1459 | 'm' ->
1460 let ondone s =
1461 match state.layout with
1462 | l :: _ ->
1463 state.bookmarks <-
1464 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1465 :: state.bookmarks
1466 | _ -> ()
1468 enttext (Some ('~', "", None, textentry, ondone))
1470 | '~' ->
1471 quickbookmark ();
1472 showtext ' ' "Quick bookmark added";
1474 | 'z' ->
1475 begin match state.layout with
1476 | l :: _ ->
1477 let rect = getpdimrect l.pagedimno in
1478 let w, h =
1479 if conf.crophack
1480 then
1481 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1482 truncate (1.2 *. (rect.(3) -. rect.(0))))
1483 else
1484 (truncate (rect.(1) -. rect.(0)),
1485 truncate (rect.(3) -. rect.(0)))
1487 if w != 0 && h != 0
1488 then
1489 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1491 Glut.postRedisplay ();
1493 | [] -> ()
1496 | '<' | '>' ->
1497 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1499 | '[' | ']' ->
1500 state.colorscale <-
1501 max 0.0
1502 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1503 Glut.postRedisplay ()
1505 | 'k' -> gotoy (clamp (-conf.scrollincr))
1506 | 'j' -> gotoy (clamp conf.scrollincr)
1508 | 'r' -> opendoc state.path state.password
1510 | _ ->
1511 vlog "huh? %d %c" key (Char.chr key);
1514 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1515 let len = String.length text in
1516 if len = 0
1517 then (
1518 state.textentry <- None;
1519 Glut.postRedisplay ();
1521 else (
1522 let s = String.sub text 0 (len - 1) in
1523 enttext (Some (c, s, opthist, onkey, ondone))
1526 | Some (c, text, onhist, onkey, ondone) ->
1527 begin match Char.unsafe_chr key with
1528 | '\r' | '\n' ->
1529 ondone text;
1530 state.textentry <- None;
1531 Glut.postRedisplay ()
1533 | '\027' ->
1534 begin match onhist with
1535 | None -> ()
1536 | Some (_, onhistcancel) -> onhistcancel ()
1537 end;
1538 state.textentry <- None;
1539 Glut.postRedisplay ()
1541 | _ ->
1542 begin match onkey text key with
1543 | TEdone text ->
1544 state.textentry <- None;
1545 ondone text;
1546 Glut.postRedisplay ()
1548 | TEcont text ->
1549 enttext (Some (c, text, onhist, onkey, ondone));
1551 | TEstop ->
1552 state.textentry <- None;
1553 Glut.postRedisplay ()
1555 | TEswitch te ->
1556 state.textentry <- Some te;
1557 Glut.postRedisplay ()
1558 end;
1559 end;
1562 let narrow outlines pattern =
1563 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1564 match reopt with
1565 | None -> None
1566 | Some re ->
1567 let rec fold accu n =
1568 if n = -1
1569 then accu
1570 else
1571 let (s, _, _, _) as o = outlines.(n) in
1572 let accu =
1573 if (try ignore (Str.search_forward re s 0); true
1574 with Not_found -> false)
1575 then (o :: accu)
1576 else accu
1578 fold accu (n-1)
1580 let matched = fold [] (Array.length outlines - 1) in
1581 if matched = [] then None else Some (Array.of_list matched)
1584 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1585 let search active pattern incr =
1586 let dosearch re =
1587 let rec loop n =
1588 if n = Array.length outlines || n = -1
1589 then None
1590 else
1591 let (s, _, _, _) = outlines.(n) in
1593 (try ignore (Str.search_forward re s 0); true
1594 with Not_found -> false)
1595 then Some n
1596 else loop (n + incr)
1598 loop active
1601 let re = Str.regexp_case_fold pattern in
1602 dosearch re
1603 with Failure s ->
1604 state.text <- s;
1605 None
1607 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1608 match key with
1609 | 27 ->
1610 if String.length qsearch = 0
1611 then (
1612 state.text <- "";
1613 state.outline <- None;
1614 Glut.postRedisplay ();
1616 else (
1617 state.text <- "";
1618 state.outline <- Some (allowdel, active, first, outlines, "");
1619 Glut.postRedisplay ();
1622 | 18 | 19 ->
1623 let incr = if key = 18 then -1 else 1 in
1624 let active, first =
1625 match search (active + incr) qsearch incr with
1626 | None ->
1627 state.text <- qsearch ^ " [not found]";
1628 active, first
1629 | Some active ->
1630 state.text <- qsearch;
1631 active, firstof active
1633 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1634 Glut.postRedisplay ();
1636 | 8 ->
1637 let len = String.length qsearch in
1638 if len = 0
1639 then ()
1640 else (
1641 if len = 1
1642 then (
1643 state.text <- "";
1644 state.outline <- Some (allowdel, active, first, outlines, "");
1646 else
1647 let qsearch = String.sub qsearch 0 (len - 1) in
1648 let active, first =
1649 match search active qsearch ~-1 with
1650 | None ->
1651 state.text <- qsearch ^ " [not found]";
1652 active, first
1653 | Some active ->
1654 state.text <- qsearch;
1655 active, firstof active
1657 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1659 Glut.postRedisplay ()
1661 | 13 ->
1662 if active < Array.length outlines
1663 then (
1664 let (_, _, n, t) = outlines.(active) in
1665 gotopage n t;
1667 state.text <- "";
1668 if allowdel then state.bookmarks <- Array.to_list outlines;
1669 state.outline <- None;
1670 Glut.postRedisplay ();
1672 | _ when key >= 32 && key < 127 ->
1673 let pattern = addchar qsearch (Char.chr key) in
1674 let active, first =
1675 match search active pattern 1 with
1676 | None ->
1677 state.text <- pattern ^ " [not found]";
1678 active, first
1679 | Some active ->
1680 state.text <- pattern;
1681 active, firstof active
1683 state.outline <- Some (allowdel, active, first, outlines, pattern);
1684 Glut.postRedisplay ()
1686 | 14 when not allowdel -> (* ctrl-n *)
1687 if String.length qsearch > 0
1688 then (
1689 let optoutlines = narrow outlines qsearch in
1690 begin match optoutlines with
1691 | None -> state.text <- "can't narrow"
1692 | Some outlines ->
1693 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1694 match state.outlines with
1695 | Olist l -> ()
1696 | Oarray a ->
1697 state.outlines <- Onarrow (qsearch, outlines, a)
1698 | Onarrow (pat, a, b) ->
1699 state.outlines <- Onarrow (qsearch, outlines, b)
1700 end;
1702 Glut.postRedisplay ()
1704 | 21 when not allowdel -> (* ctrl-u *)
1705 let outline =
1706 match state.outlines with
1707 | Oarray a -> a
1708 | Olist l ->
1709 let a = Array.of_list (List.rev l) in
1710 state.outlines <- Oarray a;
1712 | Onarrow (pat, a, b) ->
1713 state.outlines <- Oarray b;
1714 state.text <- "";
1717 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1718 Glut.postRedisplay ()
1720 | 12 ->
1721 state.outline <-
1722 Some (allowdel, active, firstof active, outlines, qsearch);
1723 Glut.postRedisplay ()
1725 | 127 when allowdel ->
1726 let len = Array.length outlines - 1 in
1727 if len = 0
1728 then (
1729 state.outline <- None;
1730 state.bookmarks <- [];
1732 else (
1733 let bookmarks = Array.init len
1734 (fun i ->
1735 let i = if i >= active then i + 1 else i in
1736 outlines.(i)
1739 state.outline <-
1740 Some (allowdel,
1741 min active (len-1),
1742 min first (len-1),
1743 bookmarks, qsearch)
1746 Glut.postRedisplay ()
1748 | _ -> log "unknown key %d" key
1751 let keyboard ~key ~x ~y =
1752 if key = 7
1753 then
1754 wcmd "interrupt" []
1755 else
1756 match state.outline with
1757 | None -> viewkeyboard ~key ~x ~y
1758 | Some outline -> outlinekeyboard ~key ~x ~y outline
1761 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno) =
1762 match key with
1763 | Glut.KEY_UP ->
1764 let pageno = max 0 (pageno - 1) in
1765 let move =
1766 let rec loop = function
1767 | [] -> true
1768 | l :: rest ->
1769 if l.pageno = pageno
1770 then l.pagey != 0
1771 else loop rest
1773 loop state.layout
1775 if move
1776 then gotopage1 pageno 0
1777 else Glut.postRedisplay ();
1778 state.birdseye <- Some (conf, leftx, pageno, hooverpageno)
1780 | Glut.KEY_DOWN ->
1781 let pageno = min (state.pagecount - 1) (pageno + 1) in
1782 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1783 let rec loop = function
1784 | [] -> gotopage1 pageno 0
1785 | l :: rest ->
1786 if l.pageno = pageno
1787 then (
1788 if l.pagevh != l.pageh
1789 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1790 else Glut.postRedisplay ()
1792 else loop rest
1794 loop state.layout
1796 | Glut.KEY_PAGE_UP ->
1797 begin match state.layout with
1798 | l :: _ ->
1799 if l.pageno = pageno
1800 then (
1801 match layout (state.y - conf.winh) conf.winh with
1802 | [] -> gotoy (clamp (-conf.winh))
1803 | l :: _ ->
1804 let pageno = max 0 (l.pageno - 1) in
1805 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1806 gotopage pageno 0.0
1808 else (
1809 let pageno = max 0 (l.pageno - 1) in
1810 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1811 gotopage pageno 0.0
1813 | [] -> gotoy (clamp (-conf.winh))
1814 end;
1815 | Glut.KEY_PAGE_DOWN ->
1816 begin match List.rev state.layout with
1817 | l :: _ ->
1818 let pageno = min l.pageno (state.pagecount - 1) in
1819 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1820 gotoy (clamp (l.pagedispy + l.pageh))
1821 | [] -> gotoy (clamp conf.winh)
1822 end;
1824 | Glut.KEY_HOME ->
1825 state.birdseye <- Some (conf, leftx, 0, hooverpageno);
1826 gotopage 0 0.0
1827 | Glut.KEY_END ->
1828 let pageno = state.pagecount - 1 in
1829 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1830 if not (pagevisible state.layout pageno)
1831 then
1832 let h =
1833 match List.rev state.pdims with
1834 | [] -> conf.winh
1835 | (_, _, h, _) :: _ -> h
1837 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1838 else Glut.postRedisplay ();
1839 | _ -> ()
1842 let special ~key ~x ~y =
1843 match state.outline, state.birdseye with
1844 | None, _ when key = Glut.KEY_F9 ->
1845 togglebirdseye ();
1846 reshape conf.winw conf.winh;
1848 | None, (Some vals) ->
1849 birdseyespecial key x y vals
1851 | None, None ->
1852 begin match state.textentry with
1853 | None ->
1854 let y =
1855 match key with
1856 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1857 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1858 | Glut.KEY_DOWN -> clamp conf.scrollincr
1859 | Glut.KEY_PAGE_UP ->
1860 if Glut.getModifiers () land Glut.active_ctrl != 0
1861 then
1862 match state.layout with
1863 | [] -> state.y
1864 | l :: _ -> state.y - l.pagey
1865 else
1866 clamp (-conf.winh)
1867 | Glut.KEY_PAGE_DOWN ->
1868 if Glut.getModifiers () land Glut.active_ctrl != 0
1869 then
1870 match List.rev state.layout with
1871 | [] -> state.y
1872 | l :: _ -> getpagey l.pageno
1873 else
1874 clamp conf.winh
1875 | Glut.KEY_HOME -> addnav (); 0
1876 | Glut.KEY_END ->
1877 addnav ();
1878 state.maxy - (if conf.maxhfit then conf.winh else 0)
1880 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1881 state.x <- state.x - 10;
1882 state.y
1883 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1884 state.x <- state.x + 10;
1885 state.y
1887 | _ -> state.y
1889 gotoy_and_clear_text y
1891 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1892 let s =
1893 match key with
1894 | Glut.KEY_UP -> action HCprev
1895 | Glut.KEY_DOWN -> action HCnext
1896 | Glut.KEY_HOME -> action HCfirst
1897 | Glut.KEY_END -> action HClast
1898 | _ -> state.text
1900 state.textentry <- Some (c, s, onhist, onkey, ondone);
1901 Glut.postRedisplay ()
1903 | _ -> ()
1906 | Some (allowdel, active, first, outlines, qsearch), _ ->
1907 let maxrows = maxoutlinerows () in
1908 let calcfirst first active =
1909 if active > first
1910 then
1911 let rows = active - first in
1912 if rows > maxrows then active - maxrows else first
1913 else active
1915 let navigate incr =
1916 let active = active + incr in
1917 let active = max 0 (min active (Array.length outlines - 1)) in
1918 let first = calcfirst first active in
1919 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1920 Glut.postRedisplay ()
1922 let updownlevel incr =
1923 let len = Array.length outlines in
1924 let (_, curlevel, _, _) = outlines.(active) in
1925 let rec flow i =
1926 if i = len then i-1 else if i = -1 then 0 else
1927 let (_, l, _, _) = outlines.(i) in
1928 if l != curlevel then i else flow (i+incr)
1930 let active = flow active in
1931 let first = calcfirst first active in
1932 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1933 Glut.postRedisplay ()
1935 match key with
1936 | Glut.KEY_UP -> navigate ~-1
1937 | Glut.KEY_DOWN -> navigate 1
1938 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1939 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1941 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1942 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1944 | Glut.KEY_HOME ->
1945 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1946 Glut.postRedisplay ()
1948 | Glut.KEY_END ->
1949 let active = Array.length outlines - 1 in
1950 let first = max 0 (active - maxrows) in
1951 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1952 Glut.postRedisplay ()
1954 | _ -> ()
1957 let drawplaceholder l =
1958 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1959 GlDraw.rect
1960 (float l.pagex, float l.pagedispy)
1961 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1963 let x = float (if margin < 0 then -margin else l.pagex)
1964 and y = float (l.pagedispy + 13) in
1965 let font = Glut.BITMAP_8_BY_13 in
1966 GlDraw.color (0.0, 0.0, 0.0);
1967 GlPix.raster_pos ~x ~y ();
1968 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1969 ("Loading " ^ string_of_int (l.pageno + 1));
1972 let now () = Unix.gettimeofday ();;
1974 let drawpage l =
1975 if state.textentry = None
1976 then (
1977 match state.birdseye with
1978 | None -> GlDraw.color (scalecolor 1.0);
1979 | Some (_, _, pageno, hooverpageno) ->
1980 let color =
1981 if l.pageno = pageno
1982 then 1.0
1983 else (
1984 if l.pageno = hooverpageno
1985 then 0.9
1986 else 0.8
1989 GlDraw.color (scalecolor color);
1991 else GlDraw.color (scalecolor 0.4);
1992 begin match getopaque l.pageno with
1993 | Some (opaque, _) when validopaque opaque ->
1994 let a = now () in
1995 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1996 opaque;
1997 let b = now () in
1998 let d = b-.a in
1999 vlog "draw %d %f sec" l.pageno d;
2001 | _ ->
2002 drawplaceholder l;
2003 end;
2006 let scrollph y =
2007 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2008 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2009 let sh = float conf.winh /. sh in
2010 let sh = max sh (float conf.scrollh) in
2012 let percent =
2013 if state.y = state.maxy
2014 then 1.0
2015 else float y /. float maxy
2017 let position = (float conf.winh -. sh) *. percent in
2019 let position =
2020 if position +. sh > float conf.winh
2021 then float conf.winh -. sh
2022 else position
2024 position, sh;
2027 let scrollindicator () =
2028 GlDraw.color (0.64 , 0.64, 0.64);
2029 GlDraw.rect
2030 (float (conf.winw - conf.scrollw), 0.)
2031 (float conf.winw, float conf.winh)
2033 GlDraw.color (0.0, 0.0, 0.0);
2035 let position, sh = scrollph state.y in
2036 GlDraw.rect
2037 (float (conf.winw - conf.scrollw), position)
2038 (float conf.winw, position +. sh)
2042 let showsel margin =
2043 match state.mstate with
2044 | Mnone | Mscroll _ | Mpan _ ->
2047 | Msel ((x0, y0), (x1, y1)) ->
2048 let rec loop = function
2049 | l :: ls ->
2050 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2051 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2052 then
2053 match getopaque l.pageno with
2054 | Some (opaque, _) when validopaque opaque ->
2055 let oy = -l.pagey + l.pagedispy in
2056 seltext opaque
2057 (x0 - margin - state.x, y0,
2058 x1 - margin - state.x, y1) oy;
2060 | _ -> ()
2061 else loop ls
2062 | [] -> ()
2064 loop state.layout
2067 let showrects () =
2068 let panx = float state.x in
2069 Gl.enable `blend;
2070 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2071 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2072 List.iter
2073 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2074 List.iter (fun l ->
2075 if l.pageno = pageno
2076 then (
2077 let d = float (l.pagedispy - l.pagey) in
2078 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2079 GlDraw.begins `quads;
2081 GlDraw.vertex2 (x0+.panx, y0+.d);
2082 GlDraw.vertex2 (x1+.panx, y1+.d);
2083 GlDraw.vertex2 (x2+.panx, y2+.d);
2084 GlDraw.vertex2 (x3+.panx, y3+.d);
2086 GlDraw.ends ();
2088 ) state.layout
2089 ) state.rects
2091 Gl.disable `blend;
2094 let showoutline = function
2095 | None -> ()
2096 | Some (allowdel, active, first, outlines, qsearch) ->
2097 Gl.enable `blend;
2098 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2099 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2100 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2101 Gl.disable `blend;
2103 GlDraw.color (1., 1., 1.);
2104 let font = Glut.BITMAP_9_BY_15 in
2105 let draw_string x y s =
2106 GlPix.raster_pos ~x ~y ();
2107 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2109 let rec loop row =
2110 if row = Array.length outlines || (row - first) * 16 > conf.winh
2111 then ()
2112 else (
2113 let (s, l, _, _) = outlines.(row) in
2114 let y = (row - first) * 16 in
2115 let x = 5 + 15*l in
2116 if row = active
2117 then (
2118 Gl.enable `blend;
2119 GlDraw.polygon_mode `both `line;
2120 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2121 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2122 GlDraw.rect (0., float (y + 1))
2123 (float (conf.winw - 1), float (y + 18));
2124 GlDraw.polygon_mode `both `fill;
2125 Gl.disable `blend;
2126 GlDraw.color (1., 1., 1.);
2128 draw_string (float x) (float (y + 16)) s;
2129 loop (row+1)
2132 loop first
2135 let display () =
2136 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2137 GlDraw.viewport margin 0 state.w conf.winh;
2138 pagematrix ();
2139 GlClear.color (scalecolor 0.5);
2140 GlClear.clear [`color];
2141 if state.x != 0
2142 then (
2143 let x = float state.x in
2144 GlMat.translate ~x ();
2146 if conf.zoom > 1.0
2147 then (
2148 Gl.enable `scissor_test;
2149 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2151 List.iter drawpage state.layout;
2152 if conf.zoom > 1.0
2153 then
2154 Gl.disable `scissor_test
2156 if state.x != 0
2157 then (
2158 let x = -.float state.x in
2159 GlMat.translate ~x ();
2161 showrects ();
2162 showsel margin;
2163 GlDraw.viewport 0 0 conf.winw conf.winh;
2164 winmatrix ();
2165 scrollindicator ();
2166 showoutline state.outline;
2167 enttext ();
2168 Glut.swapBuffers ();
2171 let getunder x y =
2172 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2173 let x = x - margin - state.x in
2174 let rec f = function
2175 | l :: rest ->
2176 begin match getopaque l.pageno with
2177 | Some (opaque, _) when validopaque opaque ->
2178 let y = y - l.pagedispy in
2179 if y > 0
2180 then
2181 let y = l.pagey + y in
2182 let x = x - l.pagex in
2183 match whatsunder opaque x y with
2184 | Unone -> f rest
2185 | under -> under
2186 else
2187 f rest
2188 | _ ->
2189 f rest
2191 | [] -> Unone
2193 f state.layout
2196 let mouse ~button ~bstate ~x ~y =
2197 match button with
2198 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2199 let incr =
2200 if n = 3
2201 then
2202 -conf.scrollincr
2203 else
2204 conf.scrollincr
2206 let incr = incr * 2 in
2207 let y = clamp incr in
2208 gotoy_and_clear_text y
2210 | Glut.LEFT_BUTTON when state.outline = None
2211 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2212 if bstate = Glut.DOWN
2213 then (
2214 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2215 state.mstate <- Mpan (x, y)
2217 else
2218 state.mstate <- Mnone
2220 | Glut.LEFT_BUTTON
2221 when state.outline = None && x > conf.winw - conf.scrollw ->
2222 if bstate = Glut.DOWN
2223 then
2224 let position, sh = scrollph state.y in
2225 if y > truncate position && y < truncate (position +. sh)
2226 then
2227 state.mstate <- Mscroll
2228 else
2229 let percent = float y /. float conf.winh in
2230 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2231 gotoy desty;
2232 state.mstate <- Mscroll
2233 else
2234 state.mstate <- Mnone
2236 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2237 begin match state.birdseye with
2238 | Some (conf, leftx, pageno, hooverpageno) ->
2239 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2240 let rec loop = function
2241 | [] -> ()
2242 | l :: rest ->
2243 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2244 && x > margin && x < margin + l.pagew
2245 then (
2246 birdseyeoff (conf, leftx, l.pageno, hooverpageno);
2247 reshape conf.winw conf.winh;
2248 state.anchor <- (l.pageno, 0.0);
2250 else loop rest
2252 loop state.layout;
2253 | None -> () (* impossible *)
2256 | Glut.LEFT_BUTTON when state.outline = None ->
2257 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2258 begin match dest with
2259 | Ulinkgoto (pageno, top) ->
2260 if pageno >= 0
2261 then
2262 gotopage1 pageno top
2264 | Ulinkuri s ->
2265 print_endline s
2267 | Unone when bstate = Glut.DOWN ->
2268 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2269 state.mstate <- Mpan (x, y);
2271 | Unone | Utext _ ->
2272 if bstate = Glut.DOWN
2273 then (
2274 if conf.angle mod 360 = 0
2275 then (
2276 state.mstate <- Msel ((x, y), (x, y));
2277 Glut.postRedisplay ()
2280 else (
2281 match state.mstate with
2282 | Mnone -> ()
2284 | Mscroll ->
2285 state.mstate <- Mnone
2287 | Mpan _ ->
2288 Glut.setCursor Glut.CURSOR_INHERIT;
2289 state.mstate <- Mnone
2291 | Msel ((x0, y0), (x1, y1)) ->
2292 let f l =
2293 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2294 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2295 then
2296 match getopaque l.pageno with
2297 | Some (opaque, _) when validopaque opaque ->
2298 copysel opaque
2299 | _ -> ()
2301 List.iter f state.layout;
2302 copysel ""; (* ugly *)
2303 Glut.setCursor Glut.CURSOR_INHERIT;
2304 state.mstate <- Mnone;
2308 | _ ->
2311 let mouse ~button ~state ~x ~y = mouse button state x y;;
2313 let motion ~x ~y =
2314 if state.outline = None
2315 then
2316 match state.mstate with
2317 | Mnone -> ()
2319 | Mpan (x0, y0) ->
2320 let dx = x - x0
2321 and dy = y0 - y in
2322 state.mstate <- Mpan (x, y);
2323 if conf.zoom > 1.0 then state.x <- state.x + dx;
2324 let y = clamp dy in
2325 gotoy_and_clear_text y
2327 | Msel (a, _) ->
2328 state.mstate <- Msel (a, (x, y));
2329 Glut.postRedisplay ()
2331 | Mscroll ->
2332 let y = min conf.winh (max 0 y) in
2333 let percent = float y /. float conf.winh in
2334 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2335 gotoy_and_clear_text y
2338 let pmotion ~x ~y =
2339 match state.birdseye with
2340 | Some (conf, leftx, pageno, hooverpageno) ->
2341 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2342 let rec loop = function
2343 | [] ->
2344 if hooverpageno != -1
2345 then (
2346 state.birdseye <- Some (conf, leftx, pageno, -1);
2347 Glut.postRedisplay ();
2349 | l :: rest ->
2350 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2351 && x > margin && x < margin + l.pagew
2352 then (
2353 state.birdseye <- Some (conf, leftx, pageno, l.pageno);
2354 Glut.postRedisplay ();
2356 else loop rest
2358 loop state.layout
2360 | None ->
2361 if state.outline = None
2362 then
2363 match state.mstate with
2364 | Mnone ->
2365 begin match getunder x y with
2366 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2367 | Ulinkuri uri ->
2368 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2369 Glut.setCursor Glut.CURSOR_INFO
2370 | Ulinkgoto (page, y) ->
2371 if conf.underinfo
2372 then showtext 'p' ("age: " ^ string_of_int page);
2373 Glut.setCursor Glut.CURSOR_INFO
2374 | Utext s ->
2375 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2376 Glut.setCursor Glut.CURSOR_TEXT
2379 | Mpan _ | Msel _ | Mscroll ->
2384 module State =
2385 struct
2386 open Parser
2388 let home =
2390 match Sys.os_type with
2391 | "Win32" -> Sys.getenv "HOMEPATH"
2392 | _ -> Sys.getenv "HOME"
2393 with exn ->
2394 prerr_endline
2395 ("Can not determine home directory location: " ^
2396 Printexc.to_string exn);
2400 let config_of c attrs =
2401 let apply c k v =
2403 match k with
2404 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2405 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2406 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2407 | "preload" -> { c with preload = bool_of_string v }
2408 | "page-bias" -> { c with pagebias = int_of_string v }
2409 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2410 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2411 | "crop-hack" -> { c with crophack = bool_of_string v }
2412 | "throttle" -> { c with showall = bool_of_string v }
2413 | "highlight-links" -> { c with hlinks = bool_of_string v }
2414 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2415 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2416 | "zoom" ->
2417 let zoom = float_of_string v /. 100. in
2418 let zoom = max 0.01 (min 2.2 zoom) in
2419 { c with zoom = zoom }
2420 | "presentation" -> { c with presentation = bool_of_string v }
2421 | "rotation-angle" -> { c with angle = int_of_string v }
2422 | "width" -> { c with winw = max 20 (int_of_string v) }
2423 | "height" -> { c with winh = max 20 (int_of_string v) }
2424 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2425 | "proportional-display" -> { c with proportional = bool_of_string v }
2426 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2427 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2428 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2429 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2430 | _ -> c
2431 with exn ->
2432 prerr_endline ("Error processing attribute (`" ^
2433 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2436 let rec fold c = function
2437 | [] -> c
2438 | (k, v) :: rest ->
2439 let c = apply c k v in
2440 fold c rest
2442 fold c attrs;
2445 let bookmark_of attrs =
2446 let rec fold title page rely = function
2447 | ("title", v) :: rest -> fold v page rely rest
2448 | ("page", v) :: rest -> fold title v rely rest
2449 | ("rely", v) :: rest -> fold title page v rest
2450 | _ :: rest -> fold title page rely rest
2451 | [] -> title, page, rely
2453 fold "invalid" "0" "0" attrs
2456 let setconf dst src =
2457 dst.scrollw <- src.scrollw;
2458 dst.scrollh <- src.scrollh;
2459 dst.icase <- src.icase;
2460 dst.preload <- src.preload;
2461 dst.pagebias <- src.pagebias;
2462 dst.verbose <- src.verbose;
2463 dst.scrollincr <- src.scrollincr;
2464 dst.maxhfit <- src.maxhfit;
2465 dst.crophack <- src.crophack;
2466 dst.autoscroll <- src.autoscroll;
2467 dst.showall <- src.showall;
2468 dst.hlinks <- src.hlinks;
2469 dst.underinfo <- src.underinfo;
2470 dst.interpagespace <- src.interpagespace;
2471 dst.zoom <- src.zoom;
2472 dst.presentation <- src.presentation;
2473 dst.angle <- src.angle;
2474 dst.winw <- src.winw;
2475 dst.winh <- src.winh;
2476 dst.savebmarks <- src.savebmarks;
2477 dst.memlimit <- src.memlimit;
2478 dst.proportional <- src.proportional;
2479 dst.texcount <- src.texcount;
2480 dst.sliceheight <- src.sliceheight;
2481 dst.thumbw <- src.thumbw;
2484 let unent s =
2485 let l = String.length s in
2486 let b = Buffer.create l in
2487 unent b s 0 l;
2488 Buffer.contents b;
2491 let get s =
2492 let h = Hashtbl.create 10 in
2493 let dc = { defconf with angle = defconf.angle } in
2494 let rec toplevel v t spos epos =
2495 match t with
2496 | Vdata | Vcdata | Vend -> v
2497 | Vopen ("llppconfig", attrs, closed) ->
2498 if closed
2499 then v
2500 else { v with f = llppconfig }
2501 | Vopen _ ->
2502 error "unexpected subelement at top level" s spos
2503 | Vclose tag -> error "unexpected close at top level" s spos
2505 and llppconfig v t spos epos =
2506 match t with
2507 | Vdata | Vcdata | Vend -> v
2508 | Vopen ("defaults", attrs, closed) ->
2509 let c = config_of dc attrs in
2510 setconf dc c;
2511 if closed
2512 then v
2513 else { v with f = skip "defaults" (fun () -> v) }
2515 | Vopen ("doc", attrs, closed) ->
2516 let pathent =
2518 List.assoc "path" attrs
2519 with Not_found -> error "doc is missing path attribute" s spos
2521 let path = unent pathent in
2522 let c = config_of dc attrs in
2523 let pageno, rely, x =
2524 let safef f n v d =
2525 try f v
2526 with exn ->
2527 dolog "error accessing %s (%S) at postion %d:\n %s"
2528 n v spos (Printexc.to_string exn);
2531 let rec fold pageno rely x = function
2532 | [] -> pageno, rely, x
2533 | ("rely", v) :: rest ->
2534 fold pageno (safef float_of_string "rely" v 0.0) x rest
2535 | ("page", v) :: rest ->
2536 fold (safef int_of_string "page" v 0) rely x rest
2537 | ("x", v) :: rest ->
2538 fold pageno rely (safef int_of_string "x" v 0) rest
2539 | _ :: rest ->
2540 fold pageno rely x rest
2542 fold 0 0.0 0 attrs
2544 let anchor = (pageno, rely) in
2545 if closed
2546 then (Hashtbl.add h path (c, [], x, anchor); v)
2547 else { v with f = doc path x anchor c [] }
2549 | Vopen (tag, _, closed) ->
2550 error "unexpected subelement in llppconfig" s spos
2552 | Vclose "llppconfig" -> { v with f = toplevel }
2553 | Vclose tag -> error "unexpected close in llppconfig" s spos
2555 and doc path x anchor c bookmarks v t spos epos =
2556 match t with
2557 | Vdata | Vcdata -> v
2558 | Vend -> error "unexpected end of input in doc" s spos
2559 | Vopen ("bookmarks", attrs, closed) ->
2560 { v with f = pbookmarks path x anchor c bookmarks }
2562 | Vopen (tag, _, _) ->
2563 error "unexpected subelement in doc" s spos
2565 | Vclose "doc" ->
2566 Hashtbl.add h path (c, List.rev bookmarks, x, anchor);
2567 { v with f = llppconfig }
2569 | Vclose tag -> error "unexpected close in doc" s spos
2571 and pbookmarks path x anchor c bookmarks v t spos epos =
2572 match t with
2573 | Vdata | Vcdata -> v
2574 | Vend -> error "unexpected end of input in bookmarks" s spos
2575 | Vopen ("item", attrs, closed) ->
2576 let titleent, spage, srely = bookmark_of attrs in
2577 let page =
2579 int_of_string spage
2580 with exn ->
2581 dolog "Failed to convert page %S to integer: %s"
2582 spage (Printexc.to_string exn);
2585 let rely =
2587 float_of_string srely
2588 with exn ->
2589 dolog "Failed to convert rely %S to real: %s"
2590 srely (Printexc.to_string exn);
2593 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2594 if closed
2595 then { v with f = pbookmarks path x anchor c bookmarks }
2596 else
2597 let f () = v in
2598 { v with f = skip "item" f }
2600 | Vopen _ ->
2601 error "unexpected subelement in bookmarks" s spos
2603 | Vclose "bookmarks" ->
2604 { v with f = doc path x anchor c bookmarks }
2606 | Vclose tag -> error "unexpected close in bookmarks" s spos
2608 and skip tag f v t spos epos =
2609 match t with
2610 | Vdata | Vcdata -> v
2611 | Vend ->
2612 error ("unexpected end of input in skipped " ^ tag) s spos
2613 | Vopen (tag', _, closed) ->
2614 if closed
2615 then v
2616 else
2617 let f' () = { v with f = skip tag f } in
2618 { v with f = skip tag' f' }
2619 | Vclose ctag ->
2620 if tag = ctag
2621 then f ()
2622 else error ("unexpected close in skipped " ^ tag) s spos
2625 parse { f = toplevel; accu = () } s;
2626 h, dc;
2629 let do_load f ic =
2631 let len = in_channel_length ic in
2632 let s = String.create len in
2633 really_input ic s 0 len;
2634 f s;
2635 with
2636 | Parse_error (msg, s, pos) ->
2637 let subs = subs s pos in
2638 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2639 failwith ("parse error: " ^ s)
2641 | exn ->
2642 failwith ("config load error: " ^ Printexc.to_string exn)
2645 let path =
2646 let dir =
2648 let dir = Filename.concat home ".config" in
2649 if Sys.is_directory dir then dir else home
2650 with _ -> home
2652 Filename.concat dir "llpp.conf"
2655 let load1 f =
2656 if Sys.file_exists path
2657 then
2658 match
2659 (try Some (open_in_bin path)
2660 with exn ->
2661 prerr_endline
2662 ("Error opening configuation file `" ^ path ^ "': " ^
2663 Printexc.to_string exn);
2664 None
2666 with
2667 | Some ic ->
2668 begin try
2669 f (do_load get ic)
2670 with exn ->
2671 prerr_endline
2672 ("Error loading configuation from `" ^ path ^ "': " ^
2673 Printexc.to_string exn);
2674 end;
2675 close_in ic;
2677 | None -> ()
2678 else
2679 f (Hashtbl.create 0, defconf)
2682 let load () =
2683 let f (h, dc) =
2684 let pc, pb, px, pa =
2686 Hashtbl.find h state.path
2687 with Not_found -> dc, [], 0, (0, 0.0)
2689 setconf defconf dc;
2690 setconf conf pc;
2691 state.bookmarks <- pb;
2692 state.x <- px;
2693 cbput state.hists.nav pa;
2695 load1 f
2698 let add_attrs bb always dc c =
2699 let ob s a b =
2700 if always || a != b
2701 then Printf.bprintf bb "\n %s='%b'" s a
2702 and oi s a b =
2703 if always || a != b
2704 then Printf.bprintf bb "\n %s='%d'" s a
2705 and oz s a b =
2706 if always || a <> b
2707 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2709 let w, h =
2710 if always
2711 then dc.winw, dc.winh
2712 else
2713 match state.fullscreen with
2714 | Some wh -> wh
2715 | None -> c.winw, c.winh
2717 let zoom, presentation, interpagespace, showall=
2718 if always
2719 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2720 else
2721 match state.birdseye with
2722 | Some (bc, _, _, _) ->
2723 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2724 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2726 oi "width" w dc.winw;
2727 oi "height" h dc.winh;
2728 oi "scroll-bar-width" c.scrollw dc.scrollw;
2729 oi "scroll-handle-height" c.scrollh dc.scrollh;
2730 ob "case-insensitive-search" c.icase dc.icase;
2731 ob "preload" c.preload dc.preload;
2732 oi "page-bias" c.pagebias dc.pagebias;
2733 oi "scroll-step" c.scrollincr dc.scrollincr;
2734 ob "max-height-fit" c.maxhfit dc.maxhfit;
2735 ob "crop-hack" c.crophack dc.crophack;
2736 ob "throttle" showall dc.showall;
2737 ob "highlight-links" c.hlinks dc.hlinks;
2738 ob "under-cursor-info" c.underinfo dc.underinfo;
2739 oi "vertical-margin" interpagespace dc.interpagespace;
2740 oz "zoom" zoom dc.zoom;
2741 ob "presentation" presentation dc.presentation;
2742 oi "rotation-angle" c.angle dc.angle;
2743 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2744 ob "proportional-display" c.proportional dc.proportional;
2745 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2746 oi "texcount" c.texcount dc.texcount;
2747 oi "slice-height" c.sliceheight dc.sliceheight;
2748 oi "thumbnail-width" c.thumbw dc.thumbw;
2751 let save () =
2752 let bb = Buffer.create 32768 in
2753 let f (h, dc) =
2754 Buffer.add_string bb "<llppconfig>\n<defaults ";
2755 add_attrs bb true dc dc;
2756 Buffer.add_string bb "/>\n";
2758 let adddoc path x anchor c bookmarks =
2759 if bookmarks == [] && c = dc && anchor = emptyanchor
2760 then ()
2761 else (
2762 Printf.bprintf bb "<doc path='%s'"
2763 (enent path 0 (String.length path));
2765 if anchor <> emptyanchor
2766 then (
2767 let n, y = anchor in
2768 Printf.bprintf bb " page='%d'" n;
2769 Printf.bprintf bb " rely='%f'" y;
2772 if x != 0
2773 then Printf.bprintf bb " pan='%d'" x;
2775 add_attrs bb false dc c;
2777 begin match bookmarks with
2778 | [] -> Buffer.add_string bb "/>\n"
2779 | _ ->
2780 Buffer.add_string bb ">\n<bookmarks>\n";
2781 List.iter (fun (title, _level, page, rely) ->
2782 Printf.bprintf bb
2783 "<item title='%s' page='%d' rely='%f'/>\n"
2784 (enent title 0 (String.length title))
2785 page
2786 rely
2787 ) bookmarks;
2788 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2789 end;
2793 let x =
2794 match state.birdseye with
2795 | Some (_, x, _, _) -> x
2796 | None -> state.x
2798 adddoc state.path x (getanchor ()) conf
2799 (if conf.savebmarks then state.bookmarks else []);
2801 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2802 if path <> state.path
2803 then
2804 adddoc path x y c bookmarks
2805 ) h;
2806 Buffer.add_string bb "</llppconfig>";
2808 load1 f;
2809 if Buffer.length bb > 0
2810 then
2812 let tmp = path ^ ".tmp" in
2813 let oc = open_out_bin tmp in
2814 Buffer.output_buffer oc bb;
2815 close_out oc;
2816 Sys.rename tmp path;
2817 with exn ->
2818 prerr_endline
2819 ("error while saving configuration: " ^ Printexc.to_string exn)
2821 end;;
2823 let () =
2824 Arg.parse
2825 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2826 (fun s -> state.path <- s)
2827 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2829 let path =
2830 if String.length state.path = 0
2831 then (prerr_endline "filename missing"; exit 1)
2832 else (
2833 if Filename.is_relative state.path
2834 then Filename.concat (Sys.getcwd ()) state.path
2835 else state.path
2838 state.path <- path;
2840 State.load ();
2842 let _ = Glut.init Sys.argv in
2843 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2844 let () = Glut.initWindowSize conf.winw conf.winh in
2845 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2847 let csock, ssock =
2848 if Sys.os_type = "Unix"
2849 then
2850 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2851 else
2852 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2853 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2854 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2855 Unix.bind sock addr;
2856 Unix.listen sock 1;
2857 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2858 Unix.connect csock addr;
2859 let ssock, _ = Unix.accept sock in
2860 Unix.close sock;
2861 let opts sock =
2862 Unix.setsockopt sock Unix.TCP_NODELAY true;
2863 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2865 opts ssock;
2866 opts csock;
2867 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2868 ssock, csock
2871 let () = Glut.displayFunc display in
2872 let () = Glut.reshapeFunc reshape in
2873 let () = Glut.keyboardFunc keyboard in
2874 let () = Glut.specialFunc special in
2875 let () = Glut.idleFunc (Some idle) in
2876 let () = Glut.mouseFunc mouse in
2877 let () = Glut.motionFunc motion in
2878 let () = Glut.passiveMotionFunc pmotion in
2880 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2881 state.csock <- csock;
2882 state.ssock <- ssock;
2883 state.text <- "Opening " ^ path;
2884 writeopen state.path state.password;
2886 at_exit State.save;
2888 let rec handlelablglutbug () =
2890 Glut.mainLoop ();
2891 with Glut.BadEnum "key in special_of_int" ->
2892 showtext '!' " LablGlut bug: special key not recognized";
2893 handlelablglutbug ()
2895 handlelablglutbug ();