Whoops
[llpp.git] / main.ml
blob1bdf24f06ff056ee4254fb0198ede123503f515f
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 let h = if n = pageno then h else ph in
396 if conf.presentation && n = pageno
397 then
398 y + (pageno - pn) * (ph + pi) + pi, h
399 else
400 y + (pageno - pn) * (ph + pi), h
401 else
402 let y = y + (if conf.presentation then pi else 0) in
403 let y = y + (n - pn) * (ph + pi) in
404 f n h ips y rest
406 | [] ->
407 y + (pageno - pn) * (ph + pi), ph
409 f 0 0 0 0 state.pdims
412 let getpagey pageno = fst (getpageyh pageno);;
414 let layout y sh =
415 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
416 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
417 match pdims with
418 | (pageno', w, h, x) :: rest when pageno' = pageno ->
419 let ips = calcips h in
420 let yinc =
421 if conf.presentation || (state.birdseye <> None && pageno = 0)
422 then ips
423 else 0
425 (w, h, ips, x), rest, pdimno + 1, yinc
426 | _ ->
427 prev, pdims, pdimno, 0
429 let dy = dy + yinc in
430 let py = py + yinc in
431 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
432 then
433 accu
434 else
435 let vy = y + dy in
436 if py + h <= vy - yinc
437 then
438 let py = py + h + ips in
439 let dy = max 0 (py - y) in
440 f ~pageno:(pageno+1)
441 ~pdimno
442 ~prev:curr
445 ~pdims:rest
446 ~cacheleft
447 ~accu
448 else
449 let pagey = vy - py in
450 let pagevh = h - pagey in
451 let pagevh = min (sh - dy) pagevh in
452 let off = if yinc > 0 then py - vy else 0 in
453 let py = py + h + ips in
454 let e =
455 { pageno = pageno
456 ; pagedimno = pdimno
457 ; pagew = w
458 ; pageh = h
459 ; pagedispy = dy + off
460 ; pagey = pagey + off
461 ; pagevh = pagevh - off
462 ; pagex = x
465 let accu = e :: accu in
466 f ~pageno:(pageno+1)
467 ~pdimno
468 ~prev:curr
470 ~dy:(dy+pagevh+ips)
471 ~pdims:rest
472 ~cacheleft:(cacheleft-1)
473 ~accu
475 if state.invalidated = 0
476 then (
477 let accu =
479 ~pageno:0
480 ~pdimno:~-1
481 ~prev:(0,0,0,0)
482 ~py:0
483 ~dy:0
484 ~pdims:state.pdims
485 ~cacheleft:(cbcap state.pagecache)
486 ~accu:[]
488 List.rev accu
490 else
494 let clamp incr =
495 let y = state.y + incr in
496 let y = max 0 y in
497 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
501 let getopaque pageno =
502 try Some (Hashtbl.find state.pagemap
503 (pageno, state.w, conf.angle, conf.proportional, state.gen))
504 with Not_found -> None
507 let cache pageno opaque =
508 Hashtbl.replace state.pagemap
509 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
512 let validopaque opaque = String.length opaque > 0;;
514 let render l =
515 match getopaque l.pageno with
516 | None when not state.rendering ->
517 state.rendering <- true;
518 cache l.pageno ("", -1);
519 wcmd "render" [`i (l.pageno + 1)
520 ;`i l.pagedimno
521 ;`i l.pagew
522 ;`i l.pageh];
524 | _ -> ()
527 let loadlayout layout =
528 let rec f all = function
529 | l :: ls ->
530 begin match getopaque l.pageno with
531 | None -> render l; f false ls
532 | Some (opaque, _) -> f (all && validopaque opaque) ls
534 | [] -> all
536 f (layout <> []) layout;
539 let findpageforopaque opaque =
540 Hashtbl.fold
541 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
542 state.pagemap None
545 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
547 let preload () =
548 let oktopreload =
549 if conf.preload
550 then
551 let memleft = conf.memlimit - state.memused in
552 if memleft < 0
553 then
554 let opaque = cbpeek state.pagecache in
555 match findpageforopaque opaque with
556 | Some ((n, _, _, _, _), size) ->
557 memleft + size >= 0 && not (pagevisible state.layout n)
558 | None -> false
559 else true
560 else false
562 if oktopreload
563 then
564 let presentation = conf.presentation in
565 let interpagespace = conf.interpagespace in
566 let maxy = state.maxy in
567 conf.presentation <- false;
568 conf.interpagespace <- 0;
569 state.maxy <- calcheight ();
570 let y =
571 match state.layout with
572 | [] -> 0
573 | l :: _ -> getpagey l.pageno
575 let y = if y < conf.winh then 0 else y - conf.winh in
576 let pages = layout y (conf.winh*3) in
577 List.iter render pages;
578 conf.presentation <- presentation;
579 conf.interpagespace <- interpagespace;
580 state.maxy <- maxy;
583 let gotoy y =
584 let y = max 0 y in
585 let y = min state.maxy y in
586 let pages = layout y conf.winh in
587 let ready = loadlayout pages in
588 if conf.showall
589 then (
590 if ready
591 then (
592 state.y <- y;
593 state.layout <- pages;
594 state.throttle <- None;
595 Glut.postRedisplay ();
597 else (
598 state.throttle <- Some pages;
601 else (
602 state.y <- y;
603 state.layout <- pages;
604 state.throttle <- None;
605 Glut.postRedisplay ();
607 begin match state.birdseye with
608 | Some (conf, leftx, pageno, hooverpageno) ->
609 if not (pagevisible pages pageno)
610 then (
611 match state.layout with
612 | [] -> ()
613 | l :: _ ->
614 state.birdseye <- Some (conf, leftx, l.pageno, hooverpageno)
616 | _ -> ()
617 end;
618 preload ();
621 let gotoy_and_clear_text y =
622 gotoy y;
623 if not conf.verbose then state.text <- "";
626 let emptyanchor = (0, 0.0);;
628 let getanchor () =
629 match state.layout with
630 | [] -> emptyanchor
631 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
634 let getanchory (n, top) =
635 let y, h = getpageyh n in
636 y + (truncate (top *. float h));
639 let gotoanchor anchor =
640 gotoy (getanchory anchor);
643 let addnav () =
644 cbput state.hists.nav (getanchor ());
647 let getnav () =
648 let anchor = cbgetc state.hists.nav ~-1 in
649 getanchory anchor;
652 let gotopagenonav n top =
653 let y, h = getpageyh n in
654 gotoy_and_clear_text (y + (truncate (top *. float h)));
657 let gotopage1nonav n top =
658 let y, h = getpageyh n in
659 addnav ();
660 gotoy_and_clear_text (y + top);
663 let gotopage n top =
664 let y, h = getpageyh n in
665 addnav ();
666 gotoy_and_clear_text (y + (truncate (top *. float h)));
669 let gotopage1 n top =
670 let y = getpagey n in
671 addnav ();
672 gotoy_and_clear_text (y + top);
675 let invalidate () =
676 state.layout <- [];
677 state.pdims <- [];
678 state.rects <- [];
679 state.rects1 <- [];
680 state.invalidated <- state.invalidated + 1;
683 let scalecolor c =
684 let c = c *. state.colorscale in
685 (c, c, c);
688 let represent () =
689 state.maxy <- calcheight ();
690 match state.birdseye with
691 | None -> gotoanchor state.anchor
692 | Some (_, _, pageno, _) ->
693 let y, h = getpageyh pageno in
694 let top = (conf.winh - h) / 2 in
695 gotoy (max 0 (y - top))
698 let pagematrix () =
699 GlMat.mode `projection;
700 GlMat.load_identity ();
701 GlMat.rotate ~x:1.0 ~angle:180.0 ();
702 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
703 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
706 let winmatrix () =
707 GlMat.mode `projection;
708 GlMat.load_identity ();
709 GlMat.rotate ~x:1.0 ~angle:180.0 ();
710 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
711 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
714 let reshape ~w ~h =
715 if state.invalidated = 0
716 then state.anchor <- getanchor ();
718 conf.winw <- w;
719 let w = truncate (float w *. conf.zoom) - conf.scrollw in
720 let w = max w 2 in
721 state.w <- w;
722 conf.winh <- h;
723 GlMat.mode `modelview;
724 GlMat.load_identity ();
725 GlClear.color (scalecolor 1.0);
726 GlClear.clear [`color];
728 invalidate ();
729 wcmd "geometry" [`i w; `i h];
732 let showtext c s =
733 GlDraw.color (0.0, 0.0, 0.0);
734 GlDraw.rect
735 (0.0, float (conf.winh - 18))
736 (float (conf.winw - conf.scrollw - 1), float conf.winh)
738 let font = Glut.BITMAP_8_BY_13 in
739 GlDraw.color (1.0, 1.0, 1.0);
740 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
741 Glut.bitmapCharacter ~font ~c:(Char.code c);
742 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
745 let enttext () =
746 let len = String.length state.text in
747 match state.textentry with
748 | None ->
749 if len > 0 then showtext ' ' state.text
751 | Some (c, text, _, _, _) ->
752 let s =
753 if len > 0
754 then
755 text ^ " [" ^ state.text ^ "]"
756 else
757 text
759 showtext c s;
762 let showtext c s =
763 if true
764 then (
765 state.text <- Printf.sprintf "%c%s" c s;
766 Glut.postRedisplay ();
768 else (
769 showtext c s;
770 Glut.swapBuffers ();
774 let act cmd =
775 match cmd.[0] with
776 | 'c' ->
777 state.pdims <- [];
779 | 'D' ->
780 state.rects <- state.rects1;
781 Glut.postRedisplay ()
783 | 'C' ->
784 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
785 state.pagecount <- n;
786 state.invalidated <- state.invalidated - 1;
787 if state.invalidated = 0
788 then represent ()
790 | 't' ->
791 let s = Scanf.sscanf cmd "t %n"
792 (fun n -> String.sub cmd n (String.length cmd - n))
794 Glut.setWindowTitle s
796 | 'T' ->
797 let s = Scanf.sscanf cmd "T %n"
798 (fun n -> String.sub cmd n (String.length cmd - n))
800 if state.textentry = None
801 then (
802 state.text <- s;
803 showtext ' ' s;
805 else (
806 state.text <- s;
807 Glut.postRedisplay ();
810 | 'V' ->
811 if conf.verbose
812 then
813 let s = Scanf.sscanf cmd "V %n"
814 (fun n -> String.sub cmd n (String.length cmd - n))
816 state.text <- s;
817 showtext ' ' s;
819 | 'F' ->
820 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
821 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
822 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
823 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
825 let y = (getpagey pageno) + truncate y0 in
826 addnav ();
827 gotoy y;
828 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
830 | 'R' ->
831 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
832 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
833 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
834 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
836 state.rects1 <-
837 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
839 | 'r' ->
840 let n, w, h, r, l, s, p =
841 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
842 (fun n w h r l s p ->
843 (n-1, w, h, r, l != 0, s, p))
846 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
847 state.memused <- state.memused + s;
849 let layout =
850 match state.throttle with
851 | None -> state.layout
852 | Some layout -> layout
855 let rec gc () =
856 if (state.memused <= conf.memlimit) || cbempty state.pagecache
857 then ()
858 else (
859 let evictedopaque = cbpeek state.pagecache in
860 match findpageforopaque evictedopaque with
861 | None -> failwith "bug in gc"
862 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
863 if state.gen != gen || not (pagevisible layout evictedn)
864 then (
865 wcmd "free" [`s evictedopaque];
866 state.memused <- state.memused - evictedsize;
867 Hashtbl.remove state.pagemap k;
868 cbdecr state.pagecache;
869 gc ();
873 gc ();
875 cbput state.pagecache p;
876 state.rendering <- false;
878 begin match state.throttle with
879 | None ->
880 if pagevisible state.layout n
881 then gotoy state.y
882 else (
883 let allvisible = loadlayout state.layout in
884 if allvisible then preload ();
887 | Some layout ->
888 match layout with
889 | [] -> ()
890 | l :: _ ->
891 let y = getpagey l.pageno + l.pagey in
892 gotoy y
895 | 'l' ->
896 let (n, w, h, x) as pdim =
897 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
899 state.pdims <- pdim :: state.pdims
901 | 'o' ->
902 let (l, n, t, h, pos) =
903 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
905 let s = String.sub cmd pos (String.length cmd - pos) in
906 let s =
907 let l = String.length s in
908 let b = Buffer.create (String.length s) in
909 let rec loop pc2 i =
910 if i = l
911 then ()
912 else
913 let pc2 =
914 match s.[i] with
915 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
916 | '\xc2' -> true
917 | c ->
918 let c = if Char.code c land 0x80 = 0 then c else '?' in
919 Buffer.add_char b c;
920 false
922 loop pc2 (i+1)
924 loop false 0;
925 Buffer.contents b
927 let outline = (s, l, n, float t /. float h) in
928 let outlines =
929 match state.outlines with
930 | Olist outlines -> Olist (outline :: outlines)
931 | Oarray _ -> Olist [outline]
932 | Onarrow _ -> Olist [outline]
934 state.outlines <- outlines
936 | _ ->
937 log "unknown cmd `%S'" cmd
940 let now = Unix.gettimeofday;;
942 let idle () =
943 let rec loop delay =
944 let r, _, _ = Unix.select [state.csock] [] [] delay in
945 begin match r with
946 | [] ->
947 if conf.autoscroll
948 then begin
949 let y = state.y + conf.scrollincr in
950 let y = if y >= state.maxy then 0 else y in
951 gotoy y;
952 state.text <- "";
953 end;
955 | _ ->
956 let cmd = readcmd state.csock in
957 act cmd;
958 loop 0.0
959 end;
960 in loop 0.001
963 let onhist cb =
964 let rc = cb.rc in
965 let action = function
966 | HCprev -> cbget cb ~-1
967 | HCnext -> cbget cb 1
968 | HCfirst -> cbget cb ~-(cb.rc)
969 | HClast -> cbget cb (cb.len - 1 - cb.rc)
970 and cancel () = cb.rc <- rc
971 in (action, cancel)
974 let search pattern forward =
975 if String.length pattern > 0
976 then
977 let pn, py =
978 match state.layout with
979 | [] -> 0, 0
980 | l :: _ ->
981 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
983 let cmd =
984 let b = makecmd "search"
985 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
987 Buffer.add_char b ',';
988 Buffer.add_string b pattern;
989 Buffer.add_char b '\000';
990 Buffer.contents b;
992 writecmd state.csock cmd;
995 let intentry text key =
996 let c = Char.unsafe_chr key in
997 match c with
998 | '0' .. '9' ->
999 let s = "x" in s.[0] <- c;
1000 let text = text ^ s in
1001 TEcont text
1003 | _ ->
1004 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1005 TEcont text
1008 let addchar s c =
1009 let b = Buffer.create (String.length s + 1) in
1010 Buffer.add_string b s;
1011 Buffer.add_char b c;
1012 Buffer.contents b;
1015 let textentry text key =
1016 let c = Char.unsafe_chr key in
1017 match c with
1018 | _ when key >= 32 && key < 127 ->
1019 let text = addchar text c in
1020 TEcont text
1022 | _ ->
1023 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1024 TEcont text
1027 let reinit angle proportional =
1028 conf.angle <- angle;
1029 conf.proportional <- proportional;
1030 invalidate ();
1031 wcmd "reinit" [`i angle; `b proportional];
1034 let optentry text key =
1035 let btos b = if b then "on" else "off" in
1036 let c = Char.unsafe_chr key in
1037 match c with
1038 | 's' ->
1039 let ondone s =
1040 try conf.scrollincr <- int_of_string s with exc ->
1041 state.text <- Printf.sprintf "bad integer `%s': %s"
1042 s (Printexc.to_string exc)
1044 TEswitch ('#', "", None, intentry, ondone)
1046 | 'R' ->
1047 let ondone s =
1048 match try
1049 Some (int_of_string s)
1050 with exc ->
1051 state.text <- Printf.sprintf "bad integer `%s': %s"
1052 s (Printexc.to_string exc);
1053 None
1054 with
1055 | Some angle -> reinit angle conf.proportional
1056 | None -> ()
1058 TEswitch ('^', "", None, intentry, ondone)
1060 | 'i' ->
1061 conf.icase <- not conf.icase;
1062 TEdone ("case insensitive search " ^ (btos conf.icase))
1064 | 'p' ->
1065 conf.preload <- not conf.preload;
1066 gotoy state.y;
1067 TEdone ("preload " ^ (btos conf.preload))
1069 | 'v' ->
1070 conf.verbose <- not conf.verbose;
1071 TEdone ("verbose " ^ (btos conf.verbose))
1073 | 'h' ->
1074 conf.maxhfit <- not conf.maxhfit;
1075 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1076 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1078 | 'c' ->
1079 conf.crophack <- not conf.crophack;
1080 TEdone ("crophack " ^ btos conf.crophack)
1082 | 'a' ->
1083 conf.showall <- not conf.showall;
1084 TEdone ("showall " ^ btos conf.showall)
1086 | 'f' ->
1087 conf.underinfo <- not conf.underinfo;
1088 TEdone ("underinfo " ^ btos conf.underinfo)
1090 | 'P' ->
1091 conf.savebmarks <- not conf.savebmarks;
1092 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1094 | 'S' ->
1095 let ondone s =
1097 let pageno, py =
1098 match state.layout with
1099 | [] -> 0, 0
1100 | l :: _ ->
1101 l.pageno, l.pagey
1103 conf.interpagespace <- int_of_string s;
1104 state.maxy <- calcheight ();
1105 let y = getpagey pageno in
1106 gotoy (y + py)
1107 with exc ->
1108 state.text <- Printf.sprintf "bad integer `%s': %s"
1109 s (Printexc.to_string exc)
1111 TEswitch ('%', "", None, intentry, ondone)
1113 | 'l' ->
1114 reinit conf.angle (not conf.proportional);
1115 TEdone ("proprortional display " ^ btos conf.proportional)
1117 | _ ->
1118 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1119 TEstop
1122 let maxoutlinerows () = (conf.winh - 31) / 16;;
1124 let enterselector allowdel outlines errmsg msg =
1125 if Array.length outlines = 0
1126 then (
1127 showtext ' ' errmsg;
1129 else (
1130 state.text <- msg;
1131 Glut.setCursor Glut.CURSOR_INHERIT;
1132 let pageno =
1133 match state.layout with
1134 | [] -> -1
1135 | {pageno=pageno} :: rest -> pageno
1137 let active =
1138 let rec loop n =
1139 if n = Array.length outlines
1140 then 0
1141 else
1142 let (_, _, outlinepageno, _) = outlines.(n) in
1143 if outlinepageno >= pageno then n else loop (n+1)
1145 loop 0
1147 state.outline <-
1148 Some (allowdel, active,
1149 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1150 Glut.postRedisplay ();
1154 let enteroutlinemode () =
1155 let outlines, msg =
1156 match state.outlines with
1157 | Oarray a -> a, ""
1158 | Olist l ->
1159 let a = Array.of_list (List.rev l) in
1160 state.outlines <- Oarray a;
1161 a, ""
1162 | Onarrow (pat, a, b) ->
1163 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1165 enterselector false outlines "Document has no outline" msg;
1168 let enterbookmarkmode () =
1169 let bookmarks = Array.of_list state.bookmarks in
1170 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1173 let quickbookmark ?title () =
1174 match state.layout with
1175 | [] -> ()
1176 | l :: _ ->
1177 let title =
1178 match title with
1179 | None ->
1180 let sec = Unix.gettimeofday () in
1181 let tm = Unix.localtime sec in
1182 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1183 (l.pageno+1)
1184 tm.Unix.tm_mday
1185 tm.Unix.tm_mon
1186 (tm.Unix.tm_year + 1900)
1187 tm.Unix.tm_hour
1188 tm.Unix.tm_min
1189 | Some title -> title
1191 state.bookmarks <-
1192 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1195 let doreshape w h =
1196 state.fullscreen <- None;
1197 Glut.reshapeWindow w h;
1200 let writeopen path password =
1201 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1204 let opendoc path password =
1205 invalidate ();
1206 state.path <- path;
1207 state.password <- password;
1208 state.gen <- state.gen + 1;
1210 writeopen path password;
1211 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1212 wcmd "geometry" [`i state.w; `i conf.winh];
1215 let birdseyeon () =
1216 let zoom = float conf.thumbw /. float conf.winw in
1217 let (birdseyepageno, _) as anchor = getanchor () in
1218 state.birdseye <-
1219 Some ({ conf with zoom = conf.zoom }, state.x, birdseyepageno, -1);
1220 conf.zoom <- zoom;
1221 conf.presentation <- false;
1222 conf.interpagespace <- 10;
1223 conf.hlinks <- false;
1224 state.x <- 0;
1225 state.mstate <- Mnone;
1226 conf.showall <- false;
1227 Glut.setCursor Glut.CURSOR_INHERIT;
1228 if conf.verbose
1229 then
1230 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1231 (100.0*.zoom)
1232 else
1233 state.text <- ""
1237 let birdseyeoff (c, leftx, pageno, _) =
1238 state.birdseye <- None;
1239 conf.zoom <- c.zoom;
1240 conf.presentation <- c.presentation;
1241 conf.interpagespace <- c.interpagespace;
1242 conf.showall <- c.showall;
1243 conf.hlinks <- c.hlinks;
1244 state.x <- leftx;
1245 if conf.verbose
1246 then
1247 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1248 (100.0*.conf.zoom)
1252 let togglebirdseye () =
1253 match state.birdseye with
1254 | None -> birdseyeon ()
1255 | Some vals -> birdseyeoff vals
1258 let viewkeyboard ~key ~x ~y =
1259 let enttext te =
1260 state.textentry <- te;
1261 state.text <- "";
1262 enttext ();
1263 Glut.postRedisplay ()
1265 match state.textentry with
1266 | None ->
1267 let c = Char.chr key in
1268 begin match c with
1269 | '\027' ->
1270 begin match state.birdseye with
1271 | None -> exit 0
1272 | Some vals ->
1273 birdseyeoff vals;
1274 reshape conf.winw conf.winh
1275 end;
1277 | 'q' ->
1278 exit 0
1280 | '\008' ->
1281 let y = getnav () in
1282 gotoy_and_clear_text y
1284 | '\012' -> (* ctrl-l *)
1285 begin match state.birdseye with
1286 | None -> ()
1287 | Some ((_, _, pageno, _) as vals) ->
1288 let y, h = getpageyh pageno in
1289 let top = (conf.winh - h) / 2 in
1290 gotoy (max 0 (y - top))
1291 end;
1293 | '\013' ->
1294 begin match state.birdseye with
1295 | None -> ()
1296 | Some ((_, _, pageno, _) as vals) ->
1297 addnav ();
1298 birdseyeoff vals;
1299 reshape conf.winw conf.winh;
1300 state.anchor <- (pageno, 0.0);
1301 end;
1303 | 'o' ->
1304 enteroutlinemode ()
1306 | 'u' ->
1307 state.rects <- [];
1308 state.text <- "";
1309 Glut.postRedisplay ()
1311 | '/' | '?' ->
1312 let ondone isforw s =
1313 cbput state.hists.pat s;
1314 state.searchpattern <- s;
1315 search s isforw
1317 enttext (Some (c, "", Some (onhist state.hists.pat),
1318 textentry, ondone (c ='/')))
1320 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1321 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1322 conf.zoom <- min 2.2 (conf.zoom +. incr);
1323 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1324 reshape conf.winw conf.winh
1326 | '+' ->
1327 let ondone s =
1328 let n =
1329 try int_of_string s with exc ->
1330 state.text <- Printf.sprintf "bad integer `%s': %s"
1331 s (Printexc.to_string exc);
1332 max_int
1334 if n != max_int
1335 then (
1336 conf.pagebias <- n;
1337 state.text <- "page bias is now " ^ string_of_int n;
1340 enttext (Some ('+', "", None, intentry, ondone))
1342 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1343 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1344 conf.zoom <- max 0.01 (conf.zoom -. decr);
1345 if conf.zoom <= 1.0 then state.x <- 0;
1346 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1347 reshape conf.winw conf.winh;
1349 | '-' ->
1350 let ondone msg =
1351 state.text <- msg;
1353 enttext (Some ('-', "", None, optentry, ondone))
1355 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1356 state.x <- 0;
1357 conf.zoom <- 1.0;
1358 state.text <- "zoom is 100%";
1359 reshape conf.winw conf.winh
1361 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1362 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1363 if zoom < 1.0
1364 then (
1365 conf.zoom <- zoom;
1366 state.x <- 0;
1367 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1368 reshape conf.winw conf.winh;
1371 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1372 togglebirdseye ();
1373 reshape conf.winw conf.winh;
1375 | '0' .. '9' ->
1376 let ondone s =
1377 let n =
1378 try int_of_string s with exc ->
1379 state.text <- Printf.sprintf "bad integer `%s': %s"
1380 s (Printexc.to_string exc);
1383 if n >= 0
1384 then (
1385 addnav ();
1386 cbput state.hists.pag (string_of_int n);
1387 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1390 let pageentry text key =
1391 match Char.unsafe_chr key with
1392 | 'g' -> TEdone text
1393 | _ -> intentry text key
1395 let text = "x" in text.[0] <- c;
1396 enttext (Some (':', text, Some (onhist state.hists.pag),
1397 pageentry, ondone))
1399 | 'b' ->
1400 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1401 reshape conf.winw conf.winh;
1403 | 'l' ->
1404 conf.hlinks <- not conf.hlinks;
1405 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1406 Glut.postRedisplay ()
1408 | 'a' ->
1409 conf.autoscroll <- not conf.autoscroll
1411 | 'P' ->
1412 conf.presentation <- not conf.presentation;
1413 showtext ' ' ("presentation mode " ^
1414 if conf.presentation then "on" else "off");
1415 represent ()
1417 | 'f' ->
1418 begin match state.fullscreen with
1419 | None ->
1420 state.fullscreen <- Some (conf.winw, conf.winh);
1421 Glut.fullScreen ()
1422 | Some (w, h) ->
1423 state.fullscreen <- None;
1424 doreshape w h
1427 | 'g' ->
1428 gotoy_and_clear_text 0
1430 | 'n' ->
1431 search state.searchpattern true
1433 | 'p' | 'N' ->
1434 search state.searchpattern false
1436 | 't' ->
1437 begin match state.layout with
1438 | [] -> ()
1439 | l :: _ ->
1440 gotoy_and_clear_text (getpagey l.pageno)
1443 | ' ' ->
1444 begin match List.rev state.layout with
1445 | [] -> ()
1446 | l :: _ ->
1447 let pageno = min (l.pageno+1) (state.pagecount-1) in
1448 gotoy_and_clear_text (getpagey pageno)
1451 | '\127' ->
1452 begin match state.layout with
1453 | [] -> ()
1454 | l :: _ ->
1455 let pageno = max 0 (l.pageno-1) in
1456 gotoy_and_clear_text (getpagey pageno)
1459 | '=' ->
1460 let f (fn, ln) l =
1461 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1463 let fn, ln = List.fold_left f (-1, -1) state.layout in
1464 let s =
1465 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1466 let percent =
1467 if maxy <= 0
1468 then 100.
1469 else (100. *. (float state.y /. float maxy)) in
1470 if fn = ln
1471 then
1472 Printf.sprintf "Page %d of %d %.2f%%"
1473 (fn+1) state.pagecount percent
1474 else
1475 Printf.sprintf
1476 "Pages %d-%d of %d %.2f%%"
1477 (fn+1) (ln+1) state.pagecount percent
1479 showtext ' ' s;
1481 | 'w' ->
1482 begin match state.layout with
1483 | [] -> ()
1484 | l :: _ ->
1485 doreshape (l.pagew + conf.scrollw) l.pageh;
1486 Glut.postRedisplay ();
1489 | '\'' ->
1490 enterbookmarkmode ()
1492 | 'm' ->
1493 let ondone s =
1494 match state.layout with
1495 | l :: _ ->
1496 state.bookmarks <-
1497 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1498 :: state.bookmarks
1499 | _ -> ()
1501 enttext (Some ('~', "", None, textentry, ondone))
1503 | '~' ->
1504 quickbookmark ();
1505 showtext ' ' "Quick bookmark added";
1507 | 'z' ->
1508 begin match state.layout with
1509 | l :: _ ->
1510 let rect = getpdimrect l.pagedimno in
1511 let w, h =
1512 if conf.crophack
1513 then
1514 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1515 truncate (1.2 *. (rect.(3) -. rect.(0))))
1516 else
1517 (truncate (rect.(1) -. rect.(0)),
1518 truncate (rect.(3) -. rect.(0)))
1520 if w != 0 && h != 0
1521 then
1522 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1524 Glut.postRedisplay ();
1526 | [] -> ()
1529 | '<' | '>' ->
1530 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1532 | '[' | ']' ->
1533 state.colorscale <-
1534 max 0.0
1535 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1536 Glut.postRedisplay ()
1538 | 'k' -> gotoy (clamp (-conf.scrollincr))
1539 | 'j' -> gotoy (clamp conf.scrollincr)
1541 | 'r' -> opendoc state.path state.password
1543 | _ ->
1544 vlog "huh? %d %c" key (Char.chr key);
1547 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1548 let len = String.length text in
1549 if len = 0
1550 then (
1551 state.textentry <- None;
1552 Glut.postRedisplay ();
1554 else (
1555 let s = String.sub text 0 (len - 1) in
1556 enttext (Some (c, s, opthist, onkey, ondone))
1559 | Some (c, text, onhist, onkey, ondone) ->
1560 begin match Char.unsafe_chr key with
1561 | '\r' | '\n' ->
1562 ondone text;
1563 state.textentry <- None;
1564 Glut.postRedisplay ()
1566 | '\027' ->
1567 begin match onhist with
1568 | None -> ()
1569 | Some (_, onhistcancel) -> onhistcancel ()
1570 end;
1571 state.textentry <- None;
1572 Glut.postRedisplay ()
1574 | _ ->
1575 begin match onkey text key with
1576 | TEdone text ->
1577 state.textentry <- None;
1578 ondone text;
1579 Glut.postRedisplay ()
1581 | TEcont text ->
1582 enttext (Some (c, text, onhist, onkey, ondone));
1584 | TEstop ->
1585 state.textentry <- None;
1586 Glut.postRedisplay ()
1588 | TEswitch te ->
1589 state.textentry <- Some te;
1590 Glut.postRedisplay ()
1591 end;
1592 end;
1595 let narrow outlines pattern =
1596 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1597 match reopt with
1598 | None -> None
1599 | Some re ->
1600 let rec fold accu n =
1601 if n = -1
1602 then accu
1603 else
1604 let (s, _, _, _) as o = outlines.(n) in
1605 let accu =
1606 if (try ignore (Str.search_forward re s 0); true
1607 with Not_found -> false)
1608 then (o :: accu)
1609 else accu
1611 fold accu (n-1)
1613 let matched = fold [] (Array.length outlines - 1) in
1614 if matched = [] then None else Some (Array.of_list matched)
1617 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1618 let search active pattern incr =
1619 let dosearch re =
1620 let rec loop n =
1621 if n = Array.length outlines || n = -1
1622 then None
1623 else
1624 let (s, _, _, _) = outlines.(n) in
1626 (try ignore (Str.search_forward re s 0); true
1627 with Not_found -> false)
1628 then Some n
1629 else loop (n + incr)
1631 loop active
1634 let re = Str.regexp_case_fold pattern in
1635 dosearch re
1636 with Failure s ->
1637 state.text <- s;
1638 None
1640 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1641 match key with
1642 | 27 ->
1643 if String.length qsearch = 0
1644 then (
1645 state.text <- "";
1646 state.outline <- None;
1647 Glut.postRedisplay ();
1649 else (
1650 state.text <- "";
1651 state.outline <- Some (allowdel, active, first, outlines, "");
1652 Glut.postRedisplay ();
1655 | 18 | 19 ->
1656 let incr = if key = 18 then -1 else 1 in
1657 let active, first =
1658 match search (active + incr) qsearch incr with
1659 | None ->
1660 state.text <- qsearch ^ " [not found]";
1661 active, first
1662 | Some active ->
1663 state.text <- qsearch;
1664 active, firstof active
1666 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1667 Glut.postRedisplay ();
1669 | 8 ->
1670 let len = String.length qsearch in
1671 if len = 0
1672 then ()
1673 else (
1674 if len = 1
1675 then (
1676 state.text <- "";
1677 state.outline <- Some (allowdel, active, first, outlines, "");
1679 else
1680 let qsearch = String.sub qsearch 0 (len - 1) in
1681 let active, first =
1682 match search active qsearch ~-1 with
1683 | None ->
1684 state.text <- qsearch ^ " [not found]";
1685 active, first
1686 | Some active ->
1687 state.text <- qsearch;
1688 active, firstof active
1690 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1692 Glut.postRedisplay ()
1694 | 13 ->
1695 if active < Array.length outlines
1696 then (
1697 let (_, _, n, t) = outlines.(active) in
1698 gotopage n t;
1700 state.text <- "";
1701 if allowdel then state.bookmarks <- Array.to_list outlines;
1702 state.outline <- None;
1703 Glut.postRedisplay ();
1705 | _ when key >= 32 && key < 127 ->
1706 let pattern = addchar qsearch (Char.chr key) in
1707 let active, first =
1708 match search active pattern 1 with
1709 | None ->
1710 state.text <- pattern ^ " [not found]";
1711 active, first
1712 | Some active ->
1713 state.text <- pattern;
1714 active, firstof active
1716 state.outline <- Some (allowdel, active, first, outlines, pattern);
1717 Glut.postRedisplay ()
1719 | 14 when not allowdel -> (* ctrl-n *)
1720 if String.length qsearch > 0
1721 then (
1722 let optoutlines = narrow outlines qsearch in
1723 begin match optoutlines with
1724 | None -> state.text <- "can't narrow"
1725 | Some outlines ->
1726 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1727 match state.outlines with
1728 | Olist l -> ()
1729 | Oarray a ->
1730 state.outlines <- Onarrow (qsearch, outlines, a)
1731 | Onarrow (pat, a, b) ->
1732 state.outlines <- Onarrow (qsearch, outlines, b)
1733 end;
1735 Glut.postRedisplay ()
1737 | 21 when not allowdel -> (* ctrl-u *)
1738 let outline =
1739 match state.outlines with
1740 | Oarray a -> a
1741 | Olist l ->
1742 let a = Array.of_list (List.rev l) in
1743 state.outlines <- Oarray a;
1745 | Onarrow (pat, a, b) ->
1746 state.outlines <- Oarray b;
1747 state.text <- "";
1750 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1751 Glut.postRedisplay ()
1753 | 12 ->
1754 state.outline <-
1755 Some (allowdel, active, firstof active, outlines, qsearch);
1756 Glut.postRedisplay ()
1758 | 127 when allowdel ->
1759 let len = Array.length outlines - 1 in
1760 if len = 0
1761 then (
1762 state.outline <- None;
1763 state.bookmarks <- [];
1765 else (
1766 let bookmarks = Array.init len
1767 (fun i ->
1768 let i = if i >= active then i + 1 else i in
1769 outlines.(i)
1772 state.outline <-
1773 Some (allowdel,
1774 min active (len-1),
1775 min first (len-1),
1776 bookmarks, qsearch)
1779 Glut.postRedisplay ()
1781 | _ -> log "unknown key %d" key
1784 let keyboard ~key ~x ~y =
1785 if key = 7
1786 then
1787 wcmd "interrupt" []
1788 else
1789 match state.outline with
1790 | None -> viewkeyboard ~key ~x ~y
1791 | Some outline -> outlinekeyboard ~key ~x ~y outline
1794 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno) =
1795 match key with
1796 | Glut.KEY_UP ->
1797 let pageno = max 0 (pageno - 1) in
1798 let rec loop = function
1799 | [] -> gotopage1nonav pageno 0
1800 | l :: _ when l.pageno = pageno ->
1801 if l.pagedispy >= 0 && l.pagey = 0
1802 then Glut.postRedisplay ()
1803 else gotopage1nonav pageno 0
1804 | _ :: rest -> loop rest
1806 loop state.layout;
1807 state.birdseye <- Some (conf, leftx, pageno, hooverpageno)
1809 | Glut.KEY_DOWN ->
1810 let pageno = min (state.pagecount - 1) (pageno + 1) in
1811 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1812 let rec loop = function
1813 | [] ->
1814 let y, h = getpageyh pageno in
1815 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1816 gotoy (clamp dy)
1817 | l :: rest when l.pageno = pageno ->
1818 if l.pagevh != l.pageh
1819 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1820 else Glut.postRedisplay ()
1821 | l :: rest -> loop rest
1823 loop state.layout
1825 | Glut.KEY_PAGE_UP ->
1826 begin match state.layout with
1827 | l :: _ ->
1828 if l.pagey != 0
1829 then (
1830 state.birdseye <- Some (conf, leftx, l.pageno, hooverpageno);
1831 gotopage1nonav l.pageno 0;
1833 else (
1834 let layout = layout (state.y-conf.winh) conf.winh in
1835 match layout with
1836 | [] -> gotoy (clamp (-conf.winh))
1837 | l :: _ ->
1838 state.birdseye <- Some (conf, leftx, l.pageno, hooverpageno);
1839 gotopage1nonav l.pageno 0
1842 | [] -> gotoy (clamp (-conf.winh))
1843 end;
1845 | Glut.KEY_PAGE_DOWN ->
1846 begin match List.rev state.layout with
1847 | l :: _ ->
1848 state.birdseye <- Some (conf, leftx, l.pageno, hooverpageno);
1849 gotoy (clamp (l.pagedispy + l.pageh))
1850 | [] -> gotoy (clamp conf.winh)
1851 end;
1853 | Glut.KEY_HOME ->
1854 state.birdseye <- Some (conf, leftx, 0, hooverpageno);
1855 gotopage1nonav 0 0
1857 | Glut.KEY_END ->
1858 let pageno = state.pagecount - 1 in
1859 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1860 if not (pagevisible state.layout pageno)
1861 then
1862 let h =
1863 match List.rev state.pdims with
1864 | [] -> conf.winh
1865 | (_, _, h, _) :: _ -> h
1867 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1868 else Glut.postRedisplay ();
1869 | _ -> ()
1872 let special ~key ~x ~y =
1873 match state.outline, state.birdseye with
1874 | None, _ when key = Glut.KEY_F9 ->
1875 togglebirdseye ();
1876 reshape conf.winw conf.winh;
1878 | None, (Some vals) ->
1879 birdseyespecial key x y vals
1881 | None, None ->
1882 begin match state.textentry with
1883 | None ->
1884 let y =
1885 match key with
1886 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1887 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1888 | Glut.KEY_DOWN -> clamp conf.scrollincr
1889 | Glut.KEY_PAGE_UP ->
1890 if Glut.getModifiers () land Glut.active_ctrl != 0
1891 then
1892 match state.layout with
1893 | [] -> state.y
1894 | l :: _ -> state.y - l.pagey
1895 else
1896 clamp (-conf.winh)
1897 | Glut.KEY_PAGE_DOWN ->
1898 if Glut.getModifiers () land Glut.active_ctrl != 0
1899 then
1900 match List.rev state.layout with
1901 | [] -> state.y
1902 | l :: _ -> getpagey l.pageno
1903 else
1904 clamp conf.winh
1905 | Glut.KEY_HOME -> addnav (); 0
1906 | Glut.KEY_END ->
1907 addnav ();
1908 state.maxy - (if conf.maxhfit then conf.winh else 0)
1910 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1911 state.x <- state.x - 10;
1912 state.y
1913 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1914 state.x <- state.x + 10;
1915 state.y
1917 | _ -> state.y
1919 gotoy_and_clear_text y
1921 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1922 let s =
1923 match key with
1924 | Glut.KEY_UP -> action HCprev
1925 | Glut.KEY_DOWN -> action HCnext
1926 | Glut.KEY_HOME -> action HCfirst
1927 | Glut.KEY_END -> action HClast
1928 | _ -> state.text
1930 state.textentry <- Some (c, s, onhist, onkey, ondone);
1931 Glut.postRedisplay ()
1933 | _ -> ()
1936 | Some (allowdel, active, first, outlines, qsearch), _ ->
1937 let maxrows = maxoutlinerows () in
1938 let calcfirst first active =
1939 if active > first
1940 then
1941 let rows = active - first in
1942 if rows > maxrows then active - maxrows else first
1943 else active
1945 let navigate incr =
1946 let active = active + incr in
1947 let active = max 0 (min active (Array.length outlines - 1)) in
1948 let first = calcfirst first active in
1949 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1950 Glut.postRedisplay ()
1952 let updownlevel incr =
1953 let len = Array.length outlines in
1954 let (_, curlevel, _, _) = outlines.(active) in
1955 let rec flow i =
1956 if i = len then i-1 else if i = -1 then 0 else
1957 let (_, l, _, _) = outlines.(i) in
1958 if l != curlevel then i else flow (i+incr)
1960 let active = flow active in
1961 let first = calcfirst first active in
1962 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1963 Glut.postRedisplay ()
1965 match key with
1966 | Glut.KEY_UP -> navigate ~-1
1967 | Glut.KEY_DOWN -> navigate 1
1968 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1969 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1971 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1972 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1974 | Glut.KEY_HOME ->
1975 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1976 Glut.postRedisplay ()
1978 | Glut.KEY_END ->
1979 let active = Array.length outlines - 1 in
1980 let first = max 0 (active - maxrows) in
1981 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1982 Glut.postRedisplay ()
1984 | _ -> ()
1987 let drawplaceholder l =
1988 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1989 GlDraw.rect
1990 (float l.pagex, float l.pagedispy)
1991 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1993 let x = float (if margin < 0 then -margin else l.pagex)
1994 and y = float (l.pagedispy + 13) in
1995 let font = Glut.BITMAP_8_BY_13 in
1996 GlDraw.color (0.0, 0.0, 0.0);
1997 GlPix.raster_pos ~x ~y ();
1998 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1999 ("Loading " ^ string_of_int (l.pageno + 1));
2002 let now () = Unix.gettimeofday ();;
2004 let drawpage l =
2005 if state.textentry = None
2006 then (
2007 match state.birdseye with
2008 | None -> GlDraw.color (scalecolor 1.0);
2009 | Some (_, _, pageno, hooverpageno) ->
2010 let color =
2011 if l.pageno = pageno
2012 then 1.0
2013 else (
2014 if l.pageno = hooverpageno
2015 then 0.9
2016 else 0.8
2019 GlDraw.color (scalecolor color);
2021 else GlDraw.color (scalecolor 0.4);
2022 begin match getopaque l.pageno with
2023 | Some (opaque, _) when validopaque opaque ->
2024 let a = now () in
2025 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2026 opaque;
2027 let b = now () in
2028 let d = b-.a in
2029 vlog "draw %d %f sec" l.pageno d;
2031 | _ ->
2032 drawplaceholder l;
2033 end;
2036 let scrollph y =
2037 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2038 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2039 let sh = float conf.winh /. sh in
2040 let sh = max sh (float conf.scrollh) in
2042 let percent =
2043 if state.y = state.maxy
2044 then 1.0
2045 else float y /. float maxy
2047 let position = (float conf.winh -. sh) *. percent in
2049 let position =
2050 if position +. sh > float conf.winh
2051 then float conf.winh -. sh
2052 else position
2054 position, sh;
2057 let scrollindicator () =
2058 GlDraw.color (0.64 , 0.64, 0.64);
2059 GlDraw.rect
2060 (float (conf.winw - conf.scrollw), 0.)
2061 (float conf.winw, float conf.winh)
2063 GlDraw.color (0.0, 0.0, 0.0);
2065 let position, sh = scrollph state.y in
2066 GlDraw.rect
2067 (float (conf.winw - conf.scrollw), position)
2068 (float conf.winw, position +. sh)
2072 let showsel margin =
2073 match state.mstate with
2074 | Mnone | Mscroll _ | Mpan _ ->
2077 | Msel ((x0, y0), (x1, y1)) ->
2078 let rec loop = function
2079 | l :: ls ->
2080 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2081 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2082 then
2083 match getopaque l.pageno with
2084 | Some (opaque, _) when validopaque opaque ->
2085 let oy = -l.pagey + l.pagedispy in
2086 seltext opaque
2087 (x0 - margin - state.x, y0,
2088 x1 - margin - state.x, y1) oy;
2090 | _ -> ()
2091 else loop ls
2092 | [] -> ()
2094 loop state.layout
2097 let showrects () =
2098 let panx = float state.x in
2099 Gl.enable `blend;
2100 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2101 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2102 List.iter
2103 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2104 List.iter (fun l ->
2105 if l.pageno = pageno
2106 then (
2107 let d = float (l.pagedispy - l.pagey) in
2108 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2109 GlDraw.begins `quads;
2111 GlDraw.vertex2 (x0+.panx, y0+.d);
2112 GlDraw.vertex2 (x1+.panx, y1+.d);
2113 GlDraw.vertex2 (x2+.panx, y2+.d);
2114 GlDraw.vertex2 (x3+.panx, y3+.d);
2116 GlDraw.ends ();
2118 ) state.layout
2119 ) state.rects
2121 Gl.disable `blend;
2124 let showoutline = function
2125 | None -> ()
2126 | Some (allowdel, active, first, outlines, qsearch) ->
2127 Gl.enable `blend;
2128 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2129 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2130 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2131 Gl.disable `blend;
2133 GlDraw.color (1., 1., 1.);
2134 let font = Glut.BITMAP_9_BY_15 in
2135 let draw_string x y s =
2136 GlPix.raster_pos ~x ~y ();
2137 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2139 let rec loop row =
2140 if row = Array.length outlines || (row - first) * 16 > conf.winh
2141 then ()
2142 else (
2143 let (s, l, _, _) = outlines.(row) in
2144 let y = (row - first) * 16 in
2145 let x = 5 + 15*l in
2146 if row = active
2147 then (
2148 Gl.enable `blend;
2149 GlDraw.polygon_mode `both `line;
2150 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2151 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2152 GlDraw.rect (0., float (y + 1))
2153 (float (conf.winw - 1), float (y + 18));
2154 GlDraw.polygon_mode `both `fill;
2155 Gl.disable `blend;
2156 GlDraw.color (1., 1., 1.);
2158 draw_string (float x) (float (y + 16)) s;
2159 loop (row+1)
2162 loop first
2165 let display () =
2166 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2167 GlDraw.viewport margin 0 state.w conf.winh;
2168 pagematrix ();
2169 GlClear.color (scalecolor 0.5);
2170 GlClear.clear [`color];
2171 if state.x != 0
2172 then (
2173 let x = float state.x in
2174 GlMat.translate ~x ();
2176 if conf.zoom > 1.0
2177 then (
2178 Gl.enable `scissor_test;
2179 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2181 List.iter drawpage state.layout;
2182 if conf.zoom > 1.0
2183 then
2184 Gl.disable `scissor_test
2186 if state.x != 0
2187 then (
2188 let x = -.float state.x in
2189 GlMat.translate ~x ();
2191 showrects ();
2192 showsel margin;
2193 GlDraw.viewport 0 0 conf.winw conf.winh;
2194 winmatrix ();
2195 scrollindicator ();
2196 showoutline state.outline;
2197 enttext ();
2198 Glut.swapBuffers ();
2201 let getunder x y =
2202 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2203 let x = x - margin - state.x in
2204 let rec f = function
2205 | l :: rest ->
2206 begin match getopaque l.pageno with
2207 | Some (opaque, _) when validopaque opaque ->
2208 let y = y - l.pagedispy in
2209 if y > 0
2210 then
2211 let y = l.pagey + y in
2212 let x = x - l.pagex in
2213 match whatsunder opaque x y with
2214 | Unone -> f rest
2215 | under -> under
2216 else
2217 f rest
2218 | _ ->
2219 f rest
2221 | [] -> Unone
2223 f state.layout
2226 let mouse ~button ~bstate ~x ~y =
2227 match button with
2228 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2229 let incr =
2230 if n = 3
2231 then
2232 -conf.scrollincr
2233 else
2234 conf.scrollincr
2236 let incr = incr * 2 in
2237 let y = clamp incr in
2238 gotoy_and_clear_text y
2240 | Glut.LEFT_BUTTON when state.outline = None
2241 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2242 if bstate = Glut.DOWN
2243 then (
2244 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2245 state.mstate <- Mpan (x, y)
2247 else
2248 state.mstate <- Mnone
2250 | Glut.LEFT_BUTTON
2251 when state.outline = None && x > conf.winw - conf.scrollw ->
2252 if bstate = Glut.DOWN
2253 then
2254 let position, sh = scrollph state.y in
2255 if y > truncate position && y < truncate (position +. sh)
2256 then
2257 state.mstate <- Mscroll
2258 else
2259 let percent = float y /. float conf.winh in
2260 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2261 gotoy desty;
2262 state.mstate <- Mscroll
2263 else
2264 state.mstate <- Mnone
2266 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2267 begin match state.birdseye with
2268 | Some (conf, leftx, pageno, hooverpageno) ->
2269 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2270 let rec loop = function
2271 | [] -> ()
2272 | l :: rest ->
2273 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2274 && x > margin && x < margin + l.pagew
2275 then (
2276 birdseyeoff (conf, leftx, l.pageno, hooverpageno);
2277 reshape conf.winw conf.winh;
2278 state.anchor <- (l.pageno, 0.0);
2280 else loop rest
2282 loop state.layout;
2283 | None -> () (* impossible *)
2286 | Glut.LEFT_BUTTON when state.outline = None ->
2287 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2288 begin match dest with
2289 | Ulinkgoto (pageno, top) ->
2290 if pageno >= 0
2291 then
2292 gotopage1 pageno top
2294 | Ulinkuri s ->
2295 print_endline s
2297 | Unone when bstate = Glut.DOWN ->
2298 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2299 state.mstate <- Mpan (x, y);
2301 | Unone | Utext _ ->
2302 if bstate = Glut.DOWN
2303 then (
2304 if conf.angle mod 360 = 0
2305 then (
2306 state.mstate <- Msel ((x, y), (x, y));
2307 Glut.postRedisplay ()
2310 else (
2311 match state.mstate with
2312 | Mnone -> ()
2314 | Mscroll ->
2315 state.mstate <- Mnone
2317 | Mpan _ ->
2318 Glut.setCursor Glut.CURSOR_INHERIT;
2319 state.mstate <- Mnone
2321 | Msel ((x0, y0), (x1, y1)) ->
2322 let f l =
2323 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2324 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2325 then
2326 match getopaque l.pageno with
2327 | Some (opaque, _) when validopaque opaque ->
2328 copysel opaque
2329 | _ -> ()
2331 List.iter f state.layout;
2332 copysel ""; (* ugly *)
2333 Glut.setCursor Glut.CURSOR_INHERIT;
2334 state.mstate <- Mnone;
2338 | _ ->
2341 let mouse ~button ~state ~x ~y = mouse button state x y;;
2343 let motion ~x ~y =
2344 if state.outline = None
2345 then
2346 match state.mstate with
2347 | Mnone -> ()
2349 | Mpan (x0, y0) ->
2350 let dx = x - x0
2351 and dy = y0 - y in
2352 state.mstate <- Mpan (x, y);
2353 if conf.zoom > 1.0 then state.x <- state.x + dx;
2354 let y = clamp dy in
2355 gotoy_and_clear_text y
2357 | Msel (a, _) ->
2358 state.mstate <- Msel (a, (x, y));
2359 Glut.postRedisplay ()
2361 | Mscroll ->
2362 let y = min conf.winh (max 0 y) in
2363 let percent = float y /. float conf.winh in
2364 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2365 gotoy_and_clear_text y
2368 let pmotion ~x ~y =
2369 match state.birdseye with
2370 | Some (conf, leftx, pageno, hooverpageno) ->
2371 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2372 let rec loop = function
2373 | [] ->
2374 if hooverpageno != -1
2375 then (
2376 state.birdseye <- Some (conf, leftx, pageno, -1);
2377 Glut.postRedisplay ();
2379 | l :: rest ->
2380 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2381 && x > margin && x < margin + l.pagew
2382 then (
2383 state.birdseye <- Some (conf, leftx, pageno, l.pageno);
2384 Glut.postRedisplay ();
2386 else loop rest
2388 loop state.layout
2390 | None ->
2391 if state.outline = None
2392 then
2393 match state.mstate with
2394 | Mnone ->
2395 begin match getunder x y with
2396 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2397 | Ulinkuri uri ->
2398 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2399 Glut.setCursor Glut.CURSOR_INFO
2400 | Ulinkgoto (page, y) ->
2401 if conf.underinfo
2402 then showtext 'p' ("age: " ^ string_of_int page);
2403 Glut.setCursor Glut.CURSOR_INFO
2404 | Utext s ->
2405 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2406 Glut.setCursor Glut.CURSOR_TEXT
2409 | Mpan _ | Msel _ | Mscroll ->
2414 module State =
2415 struct
2416 open Parser
2418 let home =
2420 match Sys.os_type with
2421 | "Win32" -> Sys.getenv "HOMEPATH"
2422 | _ -> Sys.getenv "HOME"
2423 with exn ->
2424 prerr_endline
2425 ("Can not determine home directory location: " ^
2426 Printexc.to_string exn);
2430 let config_of c attrs =
2431 let apply c k v =
2433 match k with
2434 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2435 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2436 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2437 | "preload" -> { c with preload = bool_of_string v }
2438 | "page-bias" -> { c with pagebias = int_of_string v }
2439 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2440 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2441 | "crop-hack" -> { c with crophack = bool_of_string v }
2442 | "throttle" -> { c with showall = bool_of_string v }
2443 | "highlight-links" -> { c with hlinks = bool_of_string v }
2444 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2445 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2446 | "zoom" ->
2447 let zoom = float_of_string v /. 100. in
2448 let zoom = max 0.01 (min 2.2 zoom) in
2449 { c with zoom = zoom }
2450 | "presentation" -> { c with presentation = bool_of_string v }
2451 | "rotation-angle" -> { c with angle = int_of_string v }
2452 | "width" -> { c with winw = max 20 (int_of_string v) }
2453 | "height" -> { c with winh = max 20 (int_of_string v) }
2454 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2455 | "proportional-display" -> { c with proportional = bool_of_string v }
2456 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2457 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2458 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2459 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2460 | _ -> c
2461 with exn ->
2462 prerr_endline ("Error processing attribute (`" ^
2463 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2466 let rec fold c = function
2467 | [] -> c
2468 | (k, v) :: rest ->
2469 let c = apply c k v in
2470 fold c rest
2472 fold c attrs;
2475 let bookmark_of attrs =
2476 let rec fold title page rely = function
2477 | ("title", v) :: rest -> fold v page rely rest
2478 | ("page", v) :: rest -> fold title v rely rest
2479 | ("rely", v) :: rest -> fold title page v rest
2480 | _ :: rest -> fold title page rely rest
2481 | [] -> title, page, rely
2483 fold "invalid" "0" "0" attrs
2486 let setconf dst src =
2487 dst.scrollw <- src.scrollw;
2488 dst.scrollh <- src.scrollh;
2489 dst.icase <- src.icase;
2490 dst.preload <- src.preload;
2491 dst.pagebias <- src.pagebias;
2492 dst.verbose <- src.verbose;
2493 dst.scrollincr <- src.scrollincr;
2494 dst.maxhfit <- src.maxhfit;
2495 dst.crophack <- src.crophack;
2496 dst.autoscroll <- src.autoscroll;
2497 dst.showall <- src.showall;
2498 dst.hlinks <- src.hlinks;
2499 dst.underinfo <- src.underinfo;
2500 dst.interpagespace <- src.interpagespace;
2501 dst.zoom <- src.zoom;
2502 dst.presentation <- src.presentation;
2503 dst.angle <- src.angle;
2504 dst.winw <- src.winw;
2505 dst.winh <- src.winh;
2506 dst.savebmarks <- src.savebmarks;
2507 dst.memlimit <- src.memlimit;
2508 dst.proportional <- src.proportional;
2509 dst.texcount <- src.texcount;
2510 dst.sliceheight <- src.sliceheight;
2511 dst.thumbw <- src.thumbw;
2514 let unent s =
2515 let l = String.length s in
2516 let b = Buffer.create l in
2517 unent b s 0 l;
2518 Buffer.contents b;
2521 let get s =
2522 let h = Hashtbl.create 10 in
2523 let dc = { defconf with angle = defconf.angle } in
2524 let rec toplevel v t spos epos =
2525 match t with
2526 | Vdata | Vcdata | Vend -> v
2527 | Vopen ("llppconfig", attrs, closed) ->
2528 if closed
2529 then v
2530 else { v with f = llppconfig }
2531 | Vopen _ ->
2532 error "unexpected subelement at top level" s spos
2533 | Vclose tag -> error "unexpected close at top level" s spos
2535 and llppconfig v t spos epos =
2536 match t with
2537 | Vdata | Vcdata | Vend -> v
2538 | Vopen ("defaults", attrs, closed) ->
2539 let c = config_of dc attrs in
2540 setconf dc c;
2541 if closed
2542 then v
2543 else { v with f = skip "defaults" (fun () -> v) }
2545 | Vopen ("doc", attrs, closed) ->
2546 let pathent =
2548 List.assoc "path" attrs
2549 with Not_found -> error "doc is missing path attribute" s spos
2551 let path = unent pathent in
2552 let c = config_of dc attrs in
2553 let pageno, rely, x =
2554 let safef f n v d =
2555 try f v
2556 with exn ->
2557 dolog "error accessing %s (%S) at postion %d:\n %s"
2558 n v spos (Printexc.to_string exn);
2561 let rec fold pageno rely x = function
2562 | [] -> pageno, rely, x
2563 | ("rely", v) :: rest ->
2564 fold pageno (safef float_of_string "rely" v 0.0) x rest
2565 | ("page", v) :: rest ->
2566 fold (safef int_of_string "page" v 0) rely x rest
2567 | ("x", v) :: rest ->
2568 fold pageno rely (safef int_of_string "x" v 0) rest
2569 | _ :: rest ->
2570 fold pageno rely x rest
2572 fold 0 0.0 0 attrs
2574 let anchor = (pageno, rely) in
2575 if closed
2576 then (Hashtbl.add h path (c, [], x, anchor); v)
2577 else { v with f = doc path x anchor c [] }
2579 | Vopen (tag, _, closed) ->
2580 error "unexpected subelement in llppconfig" s spos
2582 | Vclose "llppconfig" -> { v with f = toplevel }
2583 | Vclose tag -> error "unexpected close in llppconfig" s spos
2585 and doc path x anchor c bookmarks v t spos epos =
2586 match t with
2587 | Vdata | Vcdata -> v
2588 | Vend -> error "unexpected end of input in doc" s spos
2589 | Vopen ("bookmarks", attrs, closed) ->
2590 { v with f = pbookmarks path x anchor c bookmarks }
2592 | Vopen (tag, _, _) ->
2593 error "unexpected subelement in doc" s spos
2595 | Vclose "doc" ->
2596 Hashtbl.add h path (c, List.rev bookmarks, x, anchor);
2597 { v with f = llppconfig }
2599 | Vclose tag -> error "unexpected close in doc" s spos
2601 and pbookmarks path x anchor c bookmarks v t spos epos =
2602 match t with
2603 | Vdata | Vcdata -> v
2604 | Vend -> error "unexpected end of input in bookmarks" s spos
2605 | Vopen ("item", attrs, closed) ->
2606 let titleent, spage, srely = bookmark_of attrs in
2607 let page =
2609 int_of_string spage
2610 with exn ->
2611 dolog "Failed to convert page %S to integer: %s"
2612 spage (Printexc.to_string exn);
2615 let rely =
2617 float_of_string srely
2618 with exn ->
2619 dolog "Failed to convert rely %S to real: %s"
2620 srely (Printexc.to_string exn);
2623 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2624 if closed
2625 then { v with f = pbookmarks path x anchor c bookmarks }
2626 else
2627 let f () = v in
2628 { v with f = skip "item" f }
2630 | Vopen _ ->
2631 error "unexpected subelement in bookmarks" s spos
2633 | Vclose "bookmarks" ->
2634 { v with f = doc path x anchor c bookmarks }
2636 | Vclose tag -> error "unexpected close in bookmarks" s spos
2638 and skip tag f v t spos epos =
2639 match t with
2640 | Vdata | Vcdata -> v
2641 | Vend ->
2642 error ("unexpected end of input in skipped " ^ tag) s spos
2643 | Vopen (tag', _, closed) ->
2644 if closed
2645 then v
2646 else
2647 let f' () = { v with f = skip tag f } in
2648 { v with f = skip tag' f' }
2649 | Vclose ctag ->
2650 if tag = ctag
2651 then f ()
2652 else error ("unexpected close in skipped " ^ tag) s spos
2655 parse { f = toplevel; accu = () } s;
2656 h, dc;
2659 let do_load f ic =
2661 let len = in_channel_length ic in
2662 let s = String.create len in
2663 really_input ic s 0 len;
2664 f s;
2665 with
2666 | Parse_error (msg, s, pos) ->
2667 let subs = subs s pos in
2668 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2669 failwith ("parse error: " ^ s)
2671 | exn ->
2672 failwith ("config load error: " ^ Printexc.to_string exn)
2675 let path =
2676 let dir =
2678 let dir = Filename.concat home ".config" in
2679 if Sys.is_directory dir then dir else home
2680 with _ -> home
2682 Filename.concat dir "llpp.conf"
2685 let load1 f =
2686 if Sys.file_exists path
2687 then
2688 match
2689 (try Some (open_in_bin path)
2690 with exn ->
2691 prerr_endline
2692 ("Error opening configuation file `" ^ path ^ "': " ^
2693 Printexc.to_string exn);
2694 None
2696 with
2697 | Some ic ->
2698 begin try
2699 f (do_load get ic)
2700 with exn ->
2701 prerr_endline
2702 ("Error loading configuation from `" ^ path ^ "': " ^
2703 Printexc.to_string exn);
2704 end;
2705 close_in ic;
2707 | None -> ()
2708 else
2709 f (Hashtbl.create 0, defconf)
2712 let load () =
2713 let f (h, dc) =
2714 let pc, pb, px, pa =
2716 Hashtbl.find h (Filename.basename state.path)
2717 with Not_found -> dc, [], 0, (0, 0.0)
2719 setconf defconf dc;
2720 setconf conf pc;
2721 state.bookmarks <- pb;
2722 state.x <- px;
2723 cbput state.hists.nav pa;
2725 load1 f
2728 let add_attrs bb always dc c =
2729 let ob s a b =
2730 if always || a != b
2731 then Printf.bprintf bb "\n %s='%b'" s a
2732 and oi s a b =
2733 if always || a != b
2734 then Printf.bprintf bb "\n %s='%d'" s a
2735 and oz s a b =
2736 if always || a <> b
2737 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2739 let w, h =
2740 if always
2741 then dc.winw, dc.winh
2742 else
2743 match state.fullscreen with
2744 | Some wh -> wh
2745 | None -> c.winw, c.winh
2747 let zoom, presentation, interpagespace, showall=
2748 if always
2749 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2750 else
2751 match state.birdseye with
2752 | Some (bc, _, _, _) ->
2753 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2754 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2756 oi "width" w dc.winw;
2757 oi "height" h dc.winh;
2758 oi "scroll-bar-width" c.scrollw dc.scrollw;
2759 oi "scroll-handle-height" c.scrollh dc.scrollh;
2760 ob "case-insensitive-search" c.icase dc.icase;
2761 ob "preload" c.preload dc.preload;
2762 oi "page-bias" c.pagebias dc.pagebias;
2763 oi "scroll-step" c.scrollincr dc.scrollincr;
2764 ob "max-height-fit" c.maxhfit dc.maxhfit;
2765 ob "crop-hack" c.crophack dc.crophack;
2766 ob "throttle" showall dc.showall;
2767 ob "highlight-links" c.hlinks dc.hlinks;
2768 ob "under-cursor-info" c.underinfo dc.underinfo;
2769 oi "vertical-margin" interpagespace dc.interpagespace;
2770 oz "zoom" zoom dc.zoom;
2771 ob "presentation" presentation dc.presentation;
2772 oi "rotation-angle" c.angle dc.angle;
2773 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2774 ob "proportional-display" c.proportional dc.proportional;
2775 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2776 oi "texcount" c.texcount dc.texcount;
2777 oi "slice-height" c.sliceheight dc.sliceheight;
2778 oi "thumbnail-width" c.thumbw dc.thumbw;
2781 let save () =
2782 let bb = Buffer.create 32768 in
2783 let f (h, dc) =
2784 Buffer.add_string bb "<llppconfig>\n<defaults ";
2785 add_attrs bb true dc dc;
2786 Buffer.add_string bb "/>\n";
2788 let adddoc path x anchor c bookmarks =
2789 if bookmarks == [] && c = dc && anchor = emptyanchor
2790 then ()
2791 else (
2792 Printf.bprintf bb "<doc path='%s'"
2793 (enent path 0 (String.length path));
2795 if anchor <> emptyanchor
2796 then (
2797 let n, y = anchor in
2798 Printf.bprintf bb " page='%d'" n;
2799 Printf.bprintf bb " rely='%f'" y;
2802 if x != 0
2803 then Printf.bprintf bb " pan='%d'" x;
2805 add_attrs bb false dc c;
2807 begin match bookmarks with
2808 | [] -> Buffer.add_string bb "/>\n"
2809 | _ ->
2810 Buffer.add_string bb ">\n<bookmarks>\n";
2811 List.iter (fun (title, _level, page, rely) ->
2812 Printf.bprintf bb
2813 "<item title='%s' page='%d' rely='%f'/>\n"
2814 (enent title 0 (String.length title))
2815 page
2816 rely
2817 ) bookmarks;
2818 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2819 end;
2823 let x =
2824 match state.birdseye with
2825 | Some (_, x, _, _) -> x
2826 | None -> state.x
2828 let basename = Filename.basename state.path in
2829 adddoc basename x (getanchor ()) conf
2830 (if conf.savebmarks then state.bookmarks else []);
2832 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2833 if basename <> path
2834 then adddoc path x y c bookmarks
2835 ) h;
2836 Buffer.add_string bb "</llppconfig>";
2838 load1 f;
2839 if Buffer.length bb > 0
2840 then
2842 let tmp = path ^ ".tmp" in
2843 let oc = open_out_bin tmp in
2844 Buffer.output_buffer oc bb;
2845 close_out oc;
2846 Sys.rename tmp path;
2847 with exn ->
2848 prerr_endline
2849 ("error while saving configuration: " ^ Printexc.to_string exn)
2851 end;;
2853 let () =
2854 Arg.parse
2855 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2856 (fun s -> state.path <- s)
2857 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2859 if String.length state.path = 0
2860 then (prerr_endline "filename missing"; exit 1);
2862 State.load ();
2864 let _ = Glut.init Sys.argv in
2865 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2866 let () = Glut.initWindowSize conf.winw conf.winh in
2867 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
2869 let csock, ssock =
2870 if Sys.os_type = "Unix"
2871 then
2872 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2873 else
2874 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2875 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2876 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2877 Unix.bind sock addr;
2878 Unix.listen sock 1;
2879 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2880 Unix.connect csock addr;
2881 let ssock, _ = Unix.accept sock in
2882 Unix.close sock;
2883 let opts sock =
2884 Unix.setsockopt sock Unix.TCP_NODELAY true;
2885 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2887 opts ssock;
2888 opts csock;
2889 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2890 ssock, csock
2893 let () = Glut.displayFunc display in
2894 let () = Glut.reshapeFunc reshape in
2895 let () = Glut.keyboardFunc keyboard in
2896 let () = Glut.specialFunc special in
2897 let () = Glut.idleFunc (Some idle) in
2898 let () = Glut.mouseFunc mouse in
2899 let () = Glut.motionFunc motion in
2900 let () = Glut.passiveMotionFunc pmotion in
2902 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2903 state.csock <- csock;
2904 state.ssock <- ssock;
2905 state.text <- "Opening " ^ state.path;
2906 writeopen state.path state.password;
2908 at_exit State.save;
2910 let rec handlelablglutbug () =
2912 Glut.mainLoop ();
2913 with Glut.BadEnum "key in special_of_int" ->
2914 showtext '!' " LablGlut bug: special key not recognized";
2915 handlelablglutbug ()
2917 handlelablglutbug ();