Sane defaults for leaving birdseye
[llpp.git] / main.ml
blob8e9c028d9b10c04ed5949c3441d37f7a3c490506
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
10 type params = angle * proportional * texcount * sliceheight
11 and pageno = int
12 and width = int
13 and height = int
14 and leftx = int
15 and opaque = string
16 and recttype = int
17 and pixmapsize = int
18 and angle = int
19 and proportional = bool
20 and interpagespace = int
21 and texcount = int
22 and sliceheight = int
23 and gen = int
24 and top = float
27 external init : Unix.file_descr -> params -> unit = "ml_init";;
28 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
29 external seltext : string -> (int * int * int * int) -> int -> unit =
30 "ml_seltext";;
31 external copysel : string -> unit = "ml_copysel";;
32 external getpdimrect : int -> float array = "ml_getpdimrect";;
33 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
34 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 type mpos = int * int
37 and mstate =
38 | Msel of (mpos * mpos)
39 | Mpan of mpos
40 | Mscroll
41 | Mnone
44 type textentry = (char * string * onhist * onkey * ondone)
45 and onkey = string -> int -> te
46 and ondone = string -> unit
47 and histcancel = unit -> unit
48 and onhist = ((histcmd -> string) * histcancel) option
49 and histcmd = HCnext | HCprev | HCfirst | HClast
50 and te =
51 | TEstop
52 | TEdone of string
53 | TEcont of string
54 | TEswitch of textentry
57 type 'a circbuf =
58 { store : 'a array
59 ; mutable rc : int
60 ; mutable wc : int
61 ; mutable len : int
65 let cbnew n v =
66 { store = Array.create n v
67 ; rc = 0
68 ; wc = 0
69 ; len = 0
73 let cbcap b = Array.length b.store;;
75 let cbput b v =
76 let cap = cbcap b in
77 b.store.(b.wc) <- v;
78 b.wc <- (b.wc + 1) mod cap;
79 b.rc <- b.wc;
80 b.len <- min (b.len + 1) cap;
83 let cbempty b = b.len = 0;;
85 let cbgetg b circular dir =
86 if cbempty b
87 then b.store.(0)
88 else
89 let rc = b.rc + dir in
90 let rc =
91 if circular
92 then (
93 if rc = -1
94 then b.len-1
95 else (
96 if rc = b.len
97 then 0
98 else rc
101 else max 0 (min rc (b.len-1))
103 b.rc <- rc;
104 b.store.(rc);
107 let cbget b = cbgetg b false;;
108 let cbgetc b = cbgetg b true;;
110 let cbpeek b =
111 let rc = b.wc - b.len in
112 let rc = if rc < 0 then cbcap b + rc else rc in
113 b.store.(rc);
116 let cbdecr b = b.len <- b.len - 1;;
118 type layout =
119 { pageno : int
120 ; pagedimno : int
121 ; pagew : int
122 ; pageh : int
123 ; pagedispy : int
124 ; pagey : int
125 ; pagevh : int
126 ; pagex : int
130 type conf =
131 { mutable scrollw : int
132 ; mutable scrollh : int
133 ; mutable icase : bool
134 ; mutable preload : bool
135 ; mutable pagebias : int
136 ; mutable verbose : bool
137 ; mutable scrollincr : int
138 ; mutable maxhfit : bool
139 ; mutable crophack : bool
140 ; mutable autoscroll : bool
141 ; mutable showall : bool
142 ; mutable hlinks : bool
143 ; mutable underinfo : bool
144 ; mutable interpagespace : interpagespace
145 ; mutable zoom : float
146 ; mutable presentation : bool
147 ; mutable angle : angle
148 ; mutable winw : int
149 ; mutable winh : int
150 ; mutable savebmarks : bool
151 ; mutable proportional : proportional
152 ; mutable memlimit : int
153 ; mutable texcount : texcount
154 ; mutable sliceheight : sliceheight
155 ; mutable thumbw : width
159 type outline = string * int * int * float;;
160 type outlines =
161 | Oarray of outline array
162 | Olist of outline list
163 | Onarrow of string * outline array * outline array
166 type rect = (float * float * float * float * float * float * float * float);;
168 type pagemapkey = (pageno * width * angle * proportional * gen);;
170 type anchor = pageno * top;;
172 type mode =
173 | Birdseye of (conf * leftx * pageno * pageno * anchor)
174 | Outline of (bool * int * int * outline array * string)
175 | Textentry of (textentry * mode)
176 | View
179 let isbirdseye = function Birdseye _ -> true | _ -> false;;
180 let istextentry = function Textentry _ -> true | _ -> false;;
182 type state =
183 { mutable csock : Unix.file_descr
184 ; mutable ssock : Unix.file_descr
185 ; mutable w : int
186 ; mutable x : int
187 ; mutable y : int
188 ; mutable anchor : anchor
189 ; mutable maxy : int
190 ; mutable layout : layout list
191 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
192 ; mutable pdims : (pageno * width * height * leftx) list
193 ; mutable pagecount : int
194 ; pagecache : string circbuf
195 ; mutable rendering : bool
196 ; mutable mstate : mstate
197 ; mutable searchpattern : string
198 ; mutable rects : (pageno * recttype * rect) list
199 ; mutable rects1 : (pageno * recttype * rect) list
200 ; mutable text : string
201 ; mutable fullscreen : (width * height) option
202 ; mutable mode : mode
203 ; mutable outlines : outlines
204 ; mutable bookmarks : outline list
205 ; mutable path : string
206 ; mutable password : string
207 ; mutable invalidated : int
208 ; mutable colorscale : float
209 ; mutable memused : int
210 ; mutable gen : gen
211 ; mutable throttle : layout list option
212 ; hists : hists
214 and hists =
215 { pat : string circbuf
216 ; pag : string circbuf
217 ; nav : anchor circbuf
221 let defconf =
222 { scrollw = 7
223 ; scrollh = 12
224 ; icase = true
225 ; preload = true
226 ; pagebias = 0
227 ; verbose = false
228 ; scrollincr = 24
229 ; maxhfit = true
230 ; crophack = false
231 ; autoscroll = false
232 ; showall = false
233 ; hlinks = false
234 ; underinfo = false
235 ; interpagespace = 2
236 ; zoom = 1.0
237 ; presentation = false
238 ; angle = 0
239 ; winw = 900
240 ; winh = 900
241 ; savebmarks = true
242 ; proportional = true
243 ; memlimit = 32*1024*1024
244 ; texcount = 256
245 ; sliceheight = 24
246 ; thumbw = 76
250 let conf = { defconf with angle = defconf.angle };;
252 let state =
253 { csock = Unix.stdin
254 ; ssock = Unix.stdin
255 ; x = 0
256 ; y = 0
257 ; anchor = (0, 0.0)
258 ; w = 0
259 ; layout = []
260 ; maxy = max_int
261 ; pagemap = Hashtbl.create 10
262 ; pagecache = cbnew 100 ""
263 ; pdims = []
264 ; pagecount = 0
265 ; rendering = false
266 ; mstate = Mnone
267 ; rects = []
268 ; rects1 = []
269 ; text = ""
270 ; mode = View
271 ; fullscreen = None
272 ; searchpattern = ""
273 ; outlines = Olist []
274 ; bookmarks = []
275 ; path = ""
276 ; password = ""
277 ; invalidated = 0
278 ; hists =
279 { nav = cbnew 100 (0, 0.0)
280 ; pat = cbnew 20 ""
281 ; pag = cbnew 10 ""
283 ; colorscale = 1.0
284 ; memused = 0
285 ; gen = 0
286 ; throttle = None
290 let vlog fmt =
291 if conf.verbose
292 then
293 Printf.kprintf prerr_endline fmt
294 else
295 Printf.kprintf ignore fmt
298 let writecmd fd s =
299 let len = String.length s in
300 let n = 4 + len in
301 let b = Buffer.create n in
302 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
303 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
304 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
305 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
306 Buffer.add_string b s;
307 let s' = Buffer.contents b in
308 let n' = Unix.write fd s' 0 n in
309 if n' != n then failwith "write failed";
312 let readcmd fd =
313 let s = "xxxx" in
314 let n = Unix.read fd s 0 4 in
315 if n != 4 then failwith "incomplete read(len)";
316 let len = 0
317 lor (Char.code s.[0] lsl 24)
318 lor (Char.code s.[1] lsl 16)
319 lor (Char.code s.[2] lsl 8)
320 lor (Char.code s.[3] lsl 0)
322 let s = String.create len in
323 let n = Unix.read fd s 0 len in
324 if n != len then failwith "incomplete read(data)";
328 let makecmd s l =
329 let b = Buffer.create 10 in
330 Buffer.add_string b s;
331 let rec combine = function
332 | [] -> b
333 | x :: xs ->
334 Buffer.add_char b ' ';
335 let s =
336 match x with
337 | `b b -> if b then "1" else "0"
338 | `s s -> s
339 | `i i -> string_of_int i
340 | `f f -> string_of_float f
341 | `I f -> string_of_int (truncate f)
343 Buffer.add_string b s;
344 combine xs;
346 combine l;
349 let wcmd s l =
350 let cmd = Buffer.contents (makecmd s l) in
351 writecmd state.csock cmd;
354 let calcips h =
355 if conf.presentation
356 then
357 let d = conf.winh - h in
358 max 0 ((d + 1) / 2)
359 else
360 conf.interpagespace
363 let calcheight () =
364 let rec f pn ph pi fh l =
365 match l with
366 | (n, _, h, _) :: rest ->
367 let ips = calcips h in
368 let fh =
369 if conf.presentation
370 then fh+ips
371 else (
372 if isbirdseye state.mode && pn = 0
373 then fh + ips
374 else fh
377 let fh = fh + ((n - pn) * (ph + pi)) in
378 f n h ips fh rest;
380 | [] ->
381 let inc =
382 if conf.presentation || (isbirdseye state.mode && pn = 0)
383 then 0
384 else -pi
386 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
387 max 0 fh
389 let fh = f 0 0 0 0 state.pdims in
393 let getpageyh pageno =
394 let rec f pn ph pi y l =
395 match l with
396 | (n, _, h, _) :: rest ->
397 let ips = calcips h in
398 if n >= pageno
399 then
400 let h = if n = pageno then h else ph in
401 if conf.presentation && n = pageno
402 then
403 y + (pageno - pn) * (ph + pi) + pi, h
404 else
405 y + (pageno - pn) * (ph + pi), h
406 else
407 let y = y + (if conf.presentation then pi else 0) in
408 let y = y + (n - pn) * (ph + pi) in
409 f n h ips y rest
411 | [] ->
412 y + (pageno - pn) * (ph + pi), ph
414 f 0 0 0 0 state.pdims
417 let getpagey pageno = fst (getpageyh pageno);;
419 let layout y sh =
420 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
421 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
422 match pdims with
423 | (pageno', w, h, x) :: rest when pageno' = pageno ->
424 let ips = calcips h in
425 let yinc =
426 if conf.presentation || (isbirdseye state.mode && pageno = 0)
427 then ips
428 else 0
430 (w, h, ips, x), rest, pdimno + 1, yinc
431 | _ ->
432 prev, pdims, pdimno, 0
434 let dy = dy + yinc in
435 let py = py + yinc in
436 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
437 then
438 accu
439 else
440 let vy = y + dy in
441 if py + h <= vy - yinc
442 then
443 let py = py + h + ips in
444 let dy = max 0 (py - y) in
445 f ~pageno:(pageno+1)
446 ~pdimno
447 ~prev:curr
450 ~pdims:rest
451 ~cacheleft
452 ~accu
453 else
454 let pagey = vy - py in
455 let pagevh = h - pagey in
456 let pagevh = min (sh - dy) pagevh in
457 let off = if yinc > 0 then py - vy else 0 in
458 let py = py + h + ips in
459 let e =
460 { pageno = pageno
461 ; pagedimno = pdimno
462 ; pagew = w
463 ; pageh = h
464 ; pagedispy = dy + off
465 ; pagey = pagey + off
466 ; pagevh = pagevh - off
467 ; pagex = x
470 let accu = e :: accu in
471 f ~pageno:(pageno+1)
472 ~pdimno
473 ~prev:curr
475 ~dy:(dy+pagevh+ips)
476 ~pdims:rest
477 ~cacheleft:(cacheleft-1)
478 ~accu
480 if state.invalidated = 0
481 then (
482 let accu =
484 ~pageno:0
485 ~pdimno:~-1
486 ~prev:(0,0,0,0)
487 ~py:0
488 ~dy:0
489 ~pdims:state.pdims
490 ~cacheleft:(cbcap state.pagecache)
491 ~accu:[]
493 List.rev accu
495 else
499 let clamp incr =
500 let y = state.y + incr in
501 let y = max 0 y in
502 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
506 let getopaque pageno =
507 try Some (Hashtbl.find state.pagemap
508 (pageno, state.w, conf.angle, conf.proportional, state.gen))
509 with Not_found -> None
512 let cache pageno opaque =
513 Hashtbl.replace state.pagemap
514 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
517 let validopaque opaque = String.length opaque > 0;;
519 let render l =
520 match getopaque l.pageno with
521 | None when not state.rendering ->
522 state.rendering <- true;
523 cache l.pageno ("", -1);
524 wcmd "render" [`i (l.pageno + 1)
525 ;`i l.pagedimno
526 ;`i l.pagew
527 ;`i l.pageh];
529 | _ -> ()
532 let loadlayout layout =
533 let rec f all = function
534 | l :: ls ->
535 begin match getopaque l.pageno with
536 | None -> render l; f false ls
537 | Some (opaque, _) -> f (all && validopaque opaque) ls
539 | [] -> all
541 f (layout <> []) layout;
544 let findpageforopaque opaque =
545 Hashtbl.fold
546 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
547 state.pagemap None
550 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
552 let preload () =
553 let oktopreload =
554 if conf.preload
555 then
556 let memleft = conf.memlimit - state.memused in
557 if memleft < 0
558 then
559 let opaque = cbpeek state.pagecache in
560 match findpageforopaque opaque with
561 | Some ((n, _, _, _, _), size) ->
562 memleft + size >= 0 && not (pagevisible state.layout n)
563 | None -> false
564 else true
565 else false
567 if oktopreload
568 then
569 let presentation = conf.presentation in
570 let interpagespace = conf.interpagespace in
571 let maxy = state.maxy in
572 conf.presentation <- false;
573 conf.interpagespace <- 0;
574 state.maxy <- calcheight ();
575 let y =
576 match state.layout with
577 | [] -> 0
578 | l :: _ -> getpagey l.pageno
580 let y = if y < conf.winh then 0 else y - conf.winh in
581 let pages = layout y (conf.winh*3) in
582 List.iter render pages;
583 conf.presentation <- presentation;
584 conf.interpagespace <- interpagespace;
585 state.maxy <- maxy;
588 let gotoy y =
589 let y = max 0 y in
590 let y = min state.maxy y in
591 let pages = layout y conf.winh in
592 let ready = loadlayout pages in
593 if conf.showall
594 then (
595 if ready
596 then (
597 state.y <- y;
598 state.layout <- pages;
599 state.throttle <- None;
600 Glut.postRedisplay ();
602 else (
603 state.throttle <- Some pages;
606 else (
607 state.y <- y;
608 state.layout <- pages;
609 state.throttle <- None;
610 Glut.postRedisplay ();
612 begin match state.mode with
613 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
614 if not (pagevisible pages pageno)
615 then (
616 match state.layout with
617 | [] -> ()
618 | l :: _ ->
619 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
621 | _ -> ()
622 end;
623 preload ();
626 let gotoy_and_clear_text y =
627 gotoy y;
628 if not conf.verbose then state.text <- "";
631 let emptyanchor = (0, 0.0);;
633 let getanchor () =
634 match state.layout with
635 | [] -> emptyanchor
636 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
639 let getanchory (n, top) =
640 let y, h = getpageyh n in
641 y + (truncate (top *. float h));
644 let gotoanchor anchor =
645 gotoy (getanchory anchor);
648 let addnav () =
649 cbput state.hists.nav (getanchor ());
652 let getnav () =
653 let anchor = cbgetc state.hists.nav ~-1 in
654 getanchory anchor;
657 let gotopagenonav n top =
658 let y, h = getpageyh n in
659 gotoy_and_clear_text (y + (truncate (top *. float h)));
662 let gotopage1nonav n top =
663 let y, h = getpageyh n in
664 addnav ();
665 gotoy_and_clear_text (y + top);
668 let gotopage n top =
669 let y, h = getpageyh n in
670 addnav ();
671 gotoy_and_clear_text (y + (truncate (top *. float h)));
674 let gotopage1 n top =
675 let y = getpagey n in
676 addnav ();
677 gotoy_and_clear_text (y + top);
680 let invalidate () =
681 state.layout <- [];
682 state.pdims <- [];
683 state.rects <- [];
684 state.rects1 <- [];
685 state.invalidated <- state.invalidated + 1;
688 let scalecolor c =
689 let c = c *. state.colorscale in
690 (c, c, c);
693 let represent () =
694 state.maxy <- calcheight ();
695 match state.mode with
696 | Birdseye (_, _, pageno, _, _) ->
697 let y, h = getpageyh pageno in
698 let top = (conf.winh - h) / 2 in
699 gotoy (max 0 (y - top))
700 | _ -> gotoanchor state.anchor
703 let pagematrix () =
704 GlMat.mode `projection;
705 GlMat.load_identity ();
706 GlMat.rotate ~x:1.0 ~angle:180.0 ();
707 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
708 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
709 if state.x != 0
710 then (
711 GlMat.translate ~x:(float state.x) ();
715 let winmatrix () =
716 GlMat.mode `projection;
717 GlMat.load_identity ();
718 GlMat.rotate ~x:1.0 ~angle:180.0 ();
719 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
720 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
723 let reshape ~w ~h =
724 if state.invalidated = 0
725 then state.anchor <- getanchor ();
727 conf.winw <- w;
728 let w = truncate (float w *. conf.zoom) - conf.scrollw in
729 let w = max w 2 in
730 state.w <- w;
731 conf.winh <- h;
732 GlMat.mode `modelview;
733 GlMat.load_identity ();
734 GlClear.color (scalecolor 1.0);
735 GlClear.clear [`color];
737 invalidate ();
738 wcmd "geometry" [`i w; `i h];
741 let showtext c s =
742 GlDraw.color (0.0, 0.0, 0.0);
743 GlDraw.rect
744 (0.0, float (conf.winh - 18))
745 (float (conf.winw - conf.scrollw - 1), float conf.winh)
747 let font = Glut.BITMAP_8_BY_13 in
748 GlDraw.color (1.0, 1.0, 1.0);
749 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
750 Glut.bitmapCharacter ~font ~c:(Char.code c);
751 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
754 let enttext () =
755 let len = String.length state.text in
756 match state.mode with
757 | Textentry ((c, text, _, _, _), _) ->
758 let s =
759 if len > 0
760 then
761 text ^ " [" ^ state.text ^ "]"
762 else
763 text
765 showtext c s;
767 | _ ->
768 if len > 0 then showtext ' ' state.text
771 let showtext c s =
772 state.text <- Printf.sprintf "%c%s" c s;
773 Glut.postRedisplay ();
776 let act cmd =
777 match cmd.[0] with
778 | 'c' ->
779 state.pdims <- [];
781 | 'D' ->
782 state.rects <- state.rects1;
783 Glut.postRedisplay ()
785 | 'C' ->
786 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
787 state.pagecount <- n;
788 state.invalidated <- state.invalidated - 1;
789 if state.invalidated = 0
790 then represent ()
792 | 't' ->
793 let s = Scanf.sscanf cmd "t %n"
794 (fun n -> String.sub cmd n (String.length cmd - n))
796 Glut.setWindowTitle s
798 | 'T' ->
799 let s = Scanf.sscanf cmd "T %n"
800 (fun n -> String.sub cmd n (String.length cmd - n))
802 if istextentry state.mode
803 then (
804 state.text <- s;
805 showtext ' ' s;
807 else (
808 state.text <- s;
809 Glut.postRedisplay ();
812 | 'V' ->
813 if conf.verbose
814 then
815 let s = Scanf.sscanf cmd "V %n"
816 (fun n -> String.sub cmd n (String.length cmd - n))
818 state.text <- s;
819 showtext ' ' s;
821 | 'F' ->
822 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
823 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
824 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
825 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
827 let y = (getpagey pageno) + truncate y0 in
828 addnav ();
829 gotoy y;
830 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
832 | 'R' ->
833 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
834 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
835 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
836 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
838 state.rects1 <-
839 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
841 | 'r' ->
842 let n, w, h, r, l, s, p =
843 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
844 (fun n w h r l s p ->
845 (n-1, w, h, r, l != 0, s, p))
848 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
849 state.memused <- state.memused + s;
851 let layout =
852 match state.throttle with
853 | None -> state.layout
854 | Some layout -> layout
857 let rec gc () =
858 if (state.memused <= conf.memlimit) || cbempty state.pagecache
859 then ()
860 else (
861 let evictedopaque = cbpeek state.pagecache in
862 match findpageforopaque evictedopaque with
863 | None -> failwith "bug in gc"
864 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
865 if state.gen != gen || not (pagevisible layout evictedn)
866 then (
867 wcmd "free" [`s evictedopaque];
868 state.memused <- state.memused - evictedsize;
869 Hashtbl.remove state.pagemap k;
870 cbdecr state.pagecache;
871 gc ();
875 gc ();
877 cbput state.pagecache p;
878 state.rendering <- false;
880 begin match state.throttle with
881 | None ->
882 if pagevisible state.layout n
883 then gotoy state.y
884 else (
885 let allvisible = loadlayout state.layout in
886 if allvisible then preload ();
889 | Some layout ->
890 match layout with
891 | [] -> ()
892 | l :: _ ->
893 let y = getpagey l.pageno + l.pagey in
894 gotoy y
897 | 'l' ->
898 let (n, w, h, x) as pdim =
899 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
901 state.pdims <- pdim :: state.pdims
903 | 'o' ->
904 let (l, n, t, h, pos) =
905 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
907 let s = String.sub cmd pos (String.length cmd - pos) in
908 let s =
909 let l = String.length s in
910 let b = Buffer.create (String.length s) in
911 let rec loop pc2 i =
912 if i = l
913 then ()
914 else
915 let pc2 =
916 match s.[i] with
917 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
918 | '\xc2' -> true
919 | c ->
920 let c = if Char.code c land 0x80 = 0 then c else '?' in
921 Buffer.add_char b c;
922 false
924 loop pc2 (i+1)
926 loop false 0;
927 Buffer.contents b
929 let outline = (s, l, n, float t /. float h) in
930 let outlines =
931 match state.outlines with
932 | Olist outlines -> Olist (outline :: outlines)
933 | Oarray _ -> Olist [outline]
934 | Onarrow _ -> Olist [outline]
936 state.outlines <- outlines
938 | _ ->
939 dolog "unknown cmd `%S'" cmd
942 let now = Unix.gettimeofday;;
944 let idle () =
945 let rec loop delay =
946 let r, _, _ = Unix.select [state.csock] [] [] delay in
947 begin match r with
948 | [] ->
949 if conf.autoscroll && conf.scrollincr != 0
950 then begin
951 let y = state.y + conf.scrollincr in
952 let y = if y >= state.maxy then 0 else y in
953 gotoy y;
954 state.text <- "";
955 end;
957 | _ ->
958 let cmd = readcmd state.csock in
959 act cmd;
960 loop 0.0
961 end;
962 in loop 0.001
965 let onhist cb =
966 let rc = cb.rc in
967 let action = function
968 | HCprev -> cbget cb ~-1
969 | HCnext -> cbget cb 1
970 | HCfirst -> cbget cb ~-(cb.rc)
971 | HClast -> cbget cb (cb.len - 1 - cb.rc)
972 and cancel () = cb.rc <- rc
973 in (action, cancel)
976 let search pattern forward =
977 if String.length pattern > 0
978 then
979 let pn, py =
980 match state.layout with
981 | [] -> 0, 0
982 | l :: _ ->
983 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
985 let cmd =
986 let b = makecmd "search"
987 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
989 Buffer.add_char b ',';
990 Buffer.add_string b pattern;
991 Buffer.add_char b '\000';
992 Buffer.contents b;
994 writecmd state.csock cmd;
997 let intentry text key =
998 let c = Char.unsafe_chr key in
999 match c with
1000 | '0' .. '9' ->
1001 let s = "x" in s.[0] <- c;
1002 let text = text ^ s in
1003 TEcont text
1005 | _ ->
1006 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1007 TEcont text
1010 let addchar s c =
1011 let b = Buffer.create (String.length s + 1) in
1012 Buffer.add_string b s;
1013 Buffer.add_char b c;
1014 Buffer.contents b;
1017 let textentry text key =
1018 let c = Char.unsafe_chr key in
1019 match c with
1020 | _ when key >= 32 && key < 127 ->
1021 let text = addchar text c in
1022 TEcont text
1024 | _ ->
1025 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1026 TEcont text
1029 let reinit angle proportional =
1030 conf.angle <- angle;
1031 conf.proportional <- proportional;
1032 invalidate ();
1033 wcmd "reinit" [`i angle; `b proportional];
1036 let optentry text key =
1037 let btos b = if b then "on" else "off" in
1038 let c = Char.unsafe_chr key in
1039 match c with
1040 | 's' ->
1041 let ondone s =
1042 try conf.scrollincr <- int_of_string s with exc ->
1043 state.text <- Printf.sprintf "bad integer `%s': %s"
1044 s (Printexc.to_string exc)
1046 TEswitch ('#', "", None, intentry, ondone)
1048 | 'R' ->
1049 let ondone s =
1050 match try
1051 Some (int_of_string s)
1052 with exc ->
1053 state.text <- Printf.sprintf "bad integer `%s': %s"
1054 s (Printexc.to_string exc);
1055 None
1056 with
1057 | Some angle -> reinit angle conf.proportional
1058 | None -> ()
1060 TEswitch ('^', "", None, intentry, ondone)
1062 | 'i' ->
1063 conf.icase <- not conf.icase;
1064 TEdone ("case insensitive search " ^ (btos conf.icase))
1066 | 'p' ->
1067 conf.preload <- not conf.preload;
1068 gotoy state.y;
1069 TEdone ("preload " ^ (btos conf.preload))
1071 | 'v' ->
1072 conf.verbose <- not conf.verbose;
1073 TEdone ("verbose " ^ (btos conf.verbose))
1075 | 'h' ->
1076 conf.maxhfit <- not conf.maxhfit;
1077 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1078 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1080 | 'c' ->
1081 conf.crophack <- not conf.crophack;
1082 TEdone ("crophack " ^ btos conf.crophack)
1084 | 'a' ->
1085 conf.showall <- not conf.showall;
1086 TEdone ("showall " ^ btos conf.showall)
1088 | 'f' ->
1089 conf.underinfo <- not conf.underinfo;
1090 TEdone ("underinfo " ^ btos conf.underinfo)
1092 | 'P' ->
1093 conf.savebmarks <- not conf.savebmarks;
1094 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1096 | 'S' ->
1097 let ondone s =
1099 let pageno, py =
1100 match state.layout with
1101 | [] -> 0, 0
1102 | l :: _ ->
1103 l.pageno, l.pagey
1105 conf.interpagespace <- int_of_string s;
1106 state.maxy <- calcheight ();
1107 let y = getpagey pageno in
1108 gotoy (y + py)
1109 with exc ->
1110 state.text <- Printf.sprintf "bad integer `%s': %s"
1111 s (Printexc.to_string exc)
1113 TEswitch ('%', "", None, intentry, ondone)
1115 | 'l' ->
1116 reinit conf.angle (not conf.proportional);
1117 TEdone ("proprortional display " ^ btos conf.proportional)
1119 | _ ->
1120 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1121 TEstop
1124 let maxoutlinerows () = (conf.winh - 31) / 16;;
1126 let enterselector allowdel outlines errmsg msg =
1127 if Array.length outlines = 0
1128 then (
1129 showtext ' ' errmsg;
1131 else (
1132 state.text <- msg;
1133 Glut.setCursor Glut.CURSOR_INHERIT;
1134 let pageno =
1135 match state.layout with
1136 | [] -> -1
1137 | {pageno=pageno} :: rest -> pageno
1139 let active =
1140 let rec loop n =
1141 if n = Array.length outlines
1142 then 0
1143 else
1144 let (_, _, outlinepageno, _) = outlines.(n) in
1145 if outlinepageno >= pageno then n else loop (n+1)
1147 loop 0
1149 state.mode <- Outline
1150 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "");
1151 Glut.postRedisplay ();
1155 let enteroutlinemode () =
1156 let outlines, msg =
1157 match state.outlines with
1158 | Oarray a -> a, ""
1159 | Olist l ->
1160 let a = Array.of_list (List.rev l) in
1161 state.outlines <- Oarray a;
1162 a, ""
1163 | Onarrow (pat, a, b) ->
1164 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1166 enterselector false outlines "Document has no outline" msg;
1169 let enterbookmarkmode () =
1170 let bookmarks = Array.of_list state.bookmarks in
1171 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1174 let quickbookmark ?title () =
1175 match state.layout with
1176 | [] -> ()
1177 | l :: _ ->
1178 let title =
1179 match title with
1180 | None ->
1181 let sec = Unix.gettimeofday () in
1182 let tm = Unix.localtime sec in
1183 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1184 (l.pageno+1)
1185 tm.Unix.tm_mday
1186 tm.Unix.tm_mon
1187 (tm.Unix.tm_year + 1900)
1188 tm.Unix.tm_hour
1189 tm.Unix.tm_min
1190 | Some title -> title
1192 state.bookmarks <-
1193 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1196 let doreshape w h =
1197 state.fullscreen <- None;
1198 Glut.reshapeWindow w h;
1201 let writeopen path password =
1202 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1205 let opendoc path password =
1206 invalidate ();
1207 state.path <- path;
1208 state.password <- password;
1209 state.gen <- state.gen + 1;
1211 writeopen path password;
1212 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1213 wcmd "geometry" [`i state.w; `i conf.winh];
1216 let birdseyeon () =
1217 let zoom = float conf.thumbw /. float conf.winw in
1218 let birdseyepageno =
1219 let rec fold = function
1220 | [] -> 0
1221 | l :: _ when l.pagey = 0 -> l.pageno
1222 | _ :: rest -> fold rest
1224 fold state.layout
1226 state.mode <- Birdseye (
1227 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1229 conf.zoom <- zoom;
1230 conf.presentation <- false;
1231 conf.interpagespace <- 10;
1232 conf.hlinks <- false;
1233 state.x <- 0;
1234 state.mstate <- Mnone;
1235 conf.showall <- false;
1236 Glut.setCursor Glut.CURSOR_INHERIT;
1237 if conf.verbose
1238 then
1239 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1240 (100.0*.zoom)
1241 else
1242 state.text <- ""
1244 reshape conf.winw conf.winh;
1247 let birdseyeoff (c, leftx, pageno, _, anchor) goback =
1248 state.mode <- View;
1249 conf.zoom <- c.zoom;
1250 conf.presentation <- c.presentation;
1251 conf.interpagespace <- c.interpagespace;
1252 conf.showall <- c.showall;
1253 conf.hlinks <- c.hlinks;
1254 state.x <- leftx;
1255 if conf.verbose
1256 then
1257 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1258 (100.0*.conf.zoom)
1260 reshape conf.winw conf.winh;
1261 state.anchor <- if goback then anchor else (pageno, 0.0);
1264 let togglebirdseye () =
1265 match state.mode with
1266 | Birdseye vals -> birdseyeoff vals true
1267 | View | Outline _ -> birdseyeon ()
1268 | _ -> ()
1271 let viewkeyboard ~key ~x ~y =
1272 let enttext te =
1273 state.mode <- Textentry (te, state.mode);
1274 state.text <- "";
1275 enttext ();
1276 Glut.postRedisplay ()
1278 let c = Char.chr key in
1279 match c with
1280 | '\027' | 'q' ->
1281 exit 0
1283 | '\008' ->
1284 let y = getnav () in
1285 gotoy_and_clear_text y
1287 | 'o' ->
1288 enteroutlinemode ()
1290 | 'u' ->
1291 state.rects <- [];
1292 state.text <- "";
1293 Glut.postRedisplay ()
1295 | '/' | '?' ->
1296 let ondone isforw s =
1297 cbput state.hists.pat s;
1298 state.searchpattern <- s;
1299 search s isforw
1301 enttext (c, "", Some (onhist state.hists.pat),
1302 textentry, ondone (c ='/'))
1304 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1305 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1306 conf.zoom <- min 2.2 (conf.zoom +. incr);
1307 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1308 reshape conf.winw conf.winh
1310 | '+' ->
1311 let ondone s =
1312 let n =
1313 try int_of_string s with exc ->
1314 state.text <- Printf.sprintf "bad integer `%s': %s"
1315 s (Printexc.to_string exc);
1316 max_int
1318 if n != max_int
1319 then (
1320 conf.pagebias <- n;
1321 state.text <- "page bias is now " ^ string_of_int n;
1324 enttext ('+', "", None, intentry, ondone)
1326 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1327 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1328 conf.zoom <- max 0.01 (conf.zoom -. decr);
1329 if conf.zoom <= 1.0 then state.x <- 0;
1330 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1331 reshape conf.winw conf.winh;
1333 | '-' ->
1334 let ondone msg =
1335 state.text <- msg;
1337 enttext ('-', "", None, optentry, ondone)
1339 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1340 state.x <- 0;
1341 conf.zoom <- 1.0;
1342 state.text <- "zoom is 100%";
1343 reshape conf.winw conf.winh
1345 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1346 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1347 if zoom < 1.0
1348 then (
1349 conf.zoom <- zoom;
1350 state.x <- 0;
1351 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1352 reshape conf.winw conf.winh;
1355 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1356 togglebirdseye ()
1358 | '0' .. '9' ->
1359 let ondone s =
1360 let n =
1361 try int_of_string s with exc ->
1362 state.text <- Printf.sprintf "bad integer `%s': %s"
1363 s (Printexc.to_string exc);
1366 if n >= 0
1367 then (
1368 addnav ();
1369 cbput state.hists.pag (string_of_int n);
1370 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1373 let pageentry text key =
1374 match Char.unsafe_chr key with
1375 | 'g' -> TEdone text
1376 | _ -> intentry text key
1378 let text = "x" in text.[0] <- c;
1379 enttext (':', text, Some (onhist state.hists.pag), pageentry, ondone)
1381 | 'b' ->
1382 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1383 reshape conf.winw conf.winh;
1385 | 'l' ->
1386 conf.hlinks <- not conf.hlinks;
1387 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1388 Glut.postRedisplay ()
1390 | 'a' ->
1391 conf.autoscroll <- not conf.autoscroll
1393 | 'P' ->
1394 conf.presentation <- not conf.presentation;
1395 showtext ' ' ("presentation mode " ^
1396 if conf.presentation then "on" else "off");
1397 represent ()
1399 | 'f' ->
1400 begin match state.fullscreen with
1401 | None ->
1402 state.fullscreen <- Some (conf.winw, conf.winh);
1403 Glut.fullScreen ()
1404 | Some (w, h) ->
1405 state.fullscreen <- None;
1406 doreshape w h
1409 | 'g' ->
1410 gotoy_and_clear_text 0
1412 | 'n' ->
1413 search state.searchpattern true
1415 | 'p' | 'N' ->
1416 search state.searchpattern false
1418 | 't' ->
1419 begin match state.layout with
1420 | [] -> ()
1421 | l :: _ ->
1422 gotoy_and_clear_text (getpagey l.pageno)
1425 | ' ' ->
1426 begin match List.rev state.layout with
1427 | [] -> ()
1428 | l :: _ ->
1429 let pageno = min (l.pageno+1) (state.pagecount-1) in
1430 gotoy_and_clear_text (getpagey pageno)
1433 | '\127' ->
1434 begin match state.layout with
1435 | [] -> ()
1436 | l :: _ ->
1437 let pageno = max 0 (l.pageno-1) in
1438 gotoy_and_clear_text (getpagey pageno)
1441 | '=' ->
1442 let f (fn, ln) l =
1443 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1445 let fn, ln = List.fold_left f (-1, -1) state.layout in
1446 let s =
1447 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1448 let percent =
1449 if maxy <= 0
1450 then 100.
1451 else (100. *. (float state.y /. float maxy)) in
1452 if fn = ln
1453 then
1454 Printf.sprintf "Page %d of %d %.2f%%"
1455 (fn+1) state.pagecount percent
1456 else
1457 Printf.sprintf
1458 "Pages %d-%d of %d %.2f%%"
1459 (fn+1) (ln+1) state.pagecount percent
1461 showtext ' ' s;
1463 | 'w' ->
1464 begin match state.layout with
1465 | [] -> ()
1466 | l :: _ ->
1467 doreshape (l.pagew + conf.scrollw) l.pageh;
1468 Glut.postRedisplay ();
1471 | '\'' ->
1472 enterbookmarkmode ()
1474 | 'm' ->
1475 let ondone s =
1476 match state.layout with
1477 | l :: _ ->
1478 state.bookmarks <-
1479 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1480 :: state.bookmarks
1481 | _ -> ()
1483 enttext ('~', "", None, textentry, ondone)
1485 | '~' ->
1486 quickbookmark ();
1487 showtext ' ' "Quick bookmark added";
1489 | 'z' ->
1490 begin match state.layout with
1491 | l :: _ ->
1492 let rect = getpdimrect l.pagedimno in
1493 let w, h =
1494 if conf.crophack
1495 then
1496 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1497 truncate (1.2 *. (rect.(3) -. rect.(0))))
1498 else
1499 (truncate (rect.(1) -. rect.(0)),
1500 truncate (rect.(3) -. rect.(0)))
1502 if w != 0 && h != 0
1503 then
1504 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1506 Glut.postRedisplay ();
1508 | [] -> ()
1511 | '<' | '>' ->
1512 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1514 | '[' | ']' ->
1515 state.colorscale <-
1516 max 0.0
1517 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1518 Glut.postRedisplay ()
1520 | 'k' -> gotoy (clamp (-conf.scrollincr))
1521 | 'j' -> gotoy (clamp conf.scrollincr)
1523 | 'r' -> opendoc state.path state.password
1525 | _ ->
1526 vlog "huh? %d %c" key (Char.chr key);
1529 let textentrykeyboard ~key ~x ~y ((c, text, opthist, onkey, ondone), mode) =
1530 let enttext te =
1531 state.mode <- Textentry (te, mode);
1532 state.text <- "";
1533 enttext ();
1534 Glut.postRedisplay ()
1536 match Char.unsafe_chr key with
1537 | '\008' ->
1538 let len = String.length text in
1539 if len = 0
1540 then (
1541 state.mode <- mode;
1542 Glut.postRedisplay ();
1544 else (
1545 let s = String.sub text 0 (len - 1) in
1546 enttext (c, s, opthist, onkey, ondone)
1549 | '\r' | '\n' ->
1550 ondone text;
1551 state.mode <- mode;
1552 Glut.postRedisplay ()
1554 | '\027' ->
1555 begin match opthist with
1556 | None -> ()
1557 | Some (_, onhistcancel) -> onhistcancel ()
1558 end;
1559 state.mode <- View;
1560 Glut.postRedisplay ()
1562 | _ ->
1563 begin match onkey text key with
1564 | TEdone text ->
1565 state.mode <- mode;
1566 ondone text;
1567 Glut.postRedisplay ()
1569 | TEcont text ->
1570 enttext (c, text, opthist, onkey, ondone);
1572 | TEstop ->
1573 state.mode <- mode;
1574 Glut.postRedisplay ()
1576 | TEswitch te ->
1577 state.mode <- Textentry (te, mode);
1578 Glut.postRedisplay ()
1579 end;
1582 let birdseyekeyboard ~key ~x ~y ((_, _, pageno, _, anchor) as beye) =
1583 match key with
1584 | 27 ->
1585 birdseyeoff beye true
1587 | 12 ->
1588 let y, h = getpageyh pageno in
1589 let top = (conf.winh - h) / 2 in
1590 gotoy (max 0 (y - top))
1592 | 13 ->
1593 birdseyeoff beye false
1595 | _ ->
1596 viewkeyboard ~key ~x ~y
1599 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1600 let narrow outlines pattern =
1601 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1602 match reopt with
1603 | None -> None
1604 | Some re ->
1605 let rec fold accu n =
1606 if n = -1
1607 then accu
1608 else
1609 let (s, _, _, _) as o = outlines.(n) in
1610 let accu =
1611 if (try ignore (Str.search_forward re s 0); true
1612 with Not_found -> false)
1613 then (o :: accu)
1614 else accu
1616 fold accu (n-1)
1618 let matched = fold [] (Array.length outlines - 1) in
1619 if matched = [] then None else Some (Array.of_list matched)
1621 let search active pattern incr =
1622 let dosearch re =
1623 let rec loop n =
1624 if n = Array.length outlines || n = -1
1625 then None
1626 else
1627 let (s, _, _, _) = outlines.(n) in
1629 (try ignore (Str.search_forward re s 0); true
1630 with Not_found -> false)
1631 then Some n
1632 else loop (n + incr)
1634 loop active
1637 let re = Str.regexp_case_fold pattern in
1638 dosearch re
1639 with Failure s ->
1640 state.text <- s;
1641 None
1643 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1644 match key with
1645 | 27 ->
1646 if String.length qsearch = 0
1647 then (
1648 state.text <- "";
1649 state.mode <- View;
1650 Glut.postRedisplay ();
1652 else (
1653 state.text <- "";
1654 state.mode <- Outline (allowdel, active, first, outlines, "");
1655 Glut.postRedisplay ();
1658 | 18 | 19 ->
1659 let incr = if key = 18 then -1 else 1 in
1660 let active, first =
1661 match search (active + incr) qsearch incr with
1662 | None ->
1663 state.text <- qsearch ^ " [not found]";
1664 active, first
1665 | Some active ->
1666 state.text <- qsearch;
1667 active, firstof active
1669 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1670 Glut.postRedisplay ();
1672 | 8 ->
1673 let len = String.length qsearch in
1674 if len = 0
1675 then ()
1676 else (
1677 if len = 1
1678 then (
1679 state.text <- "";
1680 state.mode <- Outline (allowdel, active, first, outlines, "");
1682 else
1683 let qsearch = String.sub qsearch 0 (len - 1) in
1684 let active, first =
1685 match search active qsearch ~-1 with
1686 | None ->
1687 state.text <- qsearch ^ " [not found]";
1688 active, first
1689 | Some active ->
1690 state.text <- qsearch;
1691 active, firstof active
1693 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1695 Glut.postRedisplay ()
1697 | 13 ->
1698 if active < Array.length outlines
1699 then (
1700 let (_, _, n, t) = outlines.(active) in
1701 gotopage n t;
1703 state.text <- "";
1704 if allowdel then state.bookmarks <- Array.to_list outlines;
1705 state.mode <- View;
1706 Glut.postRedisplay ();
1708 | _ when key >= 32 && key < 127 ->
1709 let pattern = addchar qsearch (Char.chr key) in
1710 let active, first =
1711 match search active pattern 1 with
1712 | None ->
1713 state.text <- pattern ^ " [not found]";
1714 active, first
1715 | Some active ->
1716 state.text <- pattern;
1717 active, firstof active
1719 state.mode <- Outline (allowdel, active, first, outlines, pattern);
1720 Glut.postRedisplay ()
1722 | 14 when not allowdel -> (* ctrl-n *)
1723 if String.length qsearch > 0
1724 then (
1725 let optoutlines = narrow outlines qsearch in
1726 begin match optoutlines with
1727 | None -> state.text <- "can't narrow"
1728 | Some outlines ->
1729 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1730 match state.outlines with
1731 | Olist l -> ()
1732 | Oarray a ->
1733 state.outlines <- Onarrow (qsearch, outlines, a)
1734 | Onarrow (pat, a, b) ->
1735 state.outlines <- Onarrow (qsearch, outlines, b)
1736 end;
1738 Glut.postRedisplay ()
1740 | 21 when not allowdel -> (* ctrl-u *)
1741 let outline =
1742 match state.outlines with
1743 | Oarray a -> a
1744 | Olist l ->
1745 let a = Array.of_list (List.rev l) in
1746 state.outlines <- Oarray a;
1748 | Onarrow (pat, a, b) ->
1749 state.outlines <- Oarray b;
1750 state.text <- "";
1753 state.mode <- Outline (allowdel, 0, 0, outline, qsearch);
1754 Glut.postRedisplay ()
1756 | 12 ->
1757 state.mode <- Outline
1758 (allowdel, active, firstof active, outlines, qsearch);
1759 Glut.postRedisplay ()
1761 | 127 when allowdel ->
1762 let len = Array.length outlines - 1 in
1763 if len = 0
1764 then (
1765 state.mode <- View;
1766 state.bookmarks <- [];
1768 else (
1769 let bookmarks = Array.init len
1770 (fun i ->
1771 let i = if i >= active then i + 1 else i in
1772 outlines.(i)
1775 state.mode <-
1776 Outline (
1777 allowdel,
1778 min active (len-1),
1779 min first (len-1),
1780 bookmarks, qsearch
1783 Glut.postRedisplay ()
1785 | _ -> dolog "unknown key %d" key
1788 let keyboard ~key ~x ~y =
1789 if key = 7
1790 then
1791 wcmd "interrupt" []
1792 else
1793 match state.mode with
1794 | Outline outline -> outlinekeyboard ~key ~x ~y outline
1795 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
1796 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
1797 | View -> viewkeyboard ~key ~x ~y
1800 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno, anchor) =
1801 match key with
1802 | Glut.KEY_UP ->
1803 let pageno = max 0 (pageno - 1) in
1804 let rec loop = function
1805 | [] -> gotopage1nonav pageno 0
1806 | l :: _ when l.pageno = pageno ->
1807 if l.pagedispy >= 0 && l.pagey = 0
1808 then Glut.postRedisplay ()
1809 else gotopage1nonav pageno 0
1810 | _ :: rest -> loop rest
1812 loop state.layout;
1813 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1815 | Glut.KEY_DOWN ->
1816 let pageno = min (state.pagecount - 1) (pageno + 1) in
1817 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1818 let rec loop = function
1819 | [] ->
1820 let y, h = getpageyh pageno in
1821 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1822 gotoy (clamp dy)
1823 | l :: rest when l.pageno = pageno ->
1824 if l.pagevh != l.pageh
1825 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1826 else Glut.postRedisplay ()
1827 | l :: rest -> loop rest
1829 loop state.layout
1831 | Glut.KEY_PAGE_UP ->
1832 begin match state.layout with
1833 | l :: _ ->
1834 if l.pagey != 0
1835 then (
1836 state.mode <- Birdseye (
1837 conf, leftx, l.pageno, hooverpageno, anchor
1839 gotopage1nonav l.pageno 0;
1841 else (
1842 let layout = layout (state.y-conf.winh) conf.winh in
1843 match layout with
1844 | [] -> gotoy (clamp (-conf.winh))
1845 | l :: _ ->
1846 state.mode <- Birdseye (
1847 conf, leftx, l.pageno, hooverpageno, anchor
1849 gotopage1nonav l.pageno 0
1852 | [] -> gotoy (clamp (-conf.winh))
1853 end;
1855 | Glut.KEY_PAGE_DOWN ->
1856 begin match List.rev state.layout with
1857 | l :: _ ->
1858 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
1859 gotoy (clamp (l.pagedispy + l.pageh))
1860 | [] -> gotoy (clamp conf.winh)
1861 end;
1863 | Glut.KEY_HOME ->
1864 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
1865 gotopage1nonav 0 0
1867 | Glut.KEY_END ->
1868 let pageno = state.pagecount - 1 in
1869 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1870 if not (pagevisible state.layout pageno)
1871 then
1872 let h =
1873 match List.rev state.pdims with
1874 | [] -> conf.winh
1875 | (_, _, h, _) :: _ -> h
1877 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1878 else Glut.postRedisplay ();
1879 | _ -> ()
1882 let special ~key ~x ~y =
1883 match state.mode with
1884 | View | (Birdseye _) when key = Glut.KEY_F9 ->
1885 togglebirdseye ()
1887 | Birdseye vals ->
1888 birdseyespecial key x y vals
1890 | View | Textentry _ ->
1891 begin match state.mode with
1892 | View ->
1893 let y =
1894 match key with
1895 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1896 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1897 | Glut.KEY_DOWN -> clamp conf.scrollincr
1898 | Glut.KEY_PAGE_UP ->
1899 if Glut.getModifiers () land Glut.active_ctrl != 0
1900 then
1901 match state.layout with
1902 | [] -> state.y
1903 | l :: _ -> state.y - l.pagey
1904 else
1905 clamp (-conf.winh)
1906 | Glut.KEY_PAGE_DOWN ->
1907 if Glut.getModifiers () land Glut.active_ctrl != 0
1908 then
1909 match List.rev state.layout with
1910 | [] -> state.y
1911 | l :: _ -> getpagey l.pageno
1912 else
1913 clamp conf.winh
1914 | Glut.KEY_HOME -> addnav (); 0
1915 | Glut.KEY_END ->
1916 addnav ();
1917 state.maxy - (if conf.maxhfit then conf.winh else 0)
1919 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1920 state.x <- state.x - 10;
1921 state.y
1922 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1923 state.x <- state.x + 10;
1924 state.y
1926 | _ -> state.y
1928 gotoy_and_clear_text y
1930 | Textentry
1931 ((c, s, (Some (action, _) as onhist), onkey, ondone), mode) ->
1932 let s =
1933 match key with
1934 | Glut.KEY_UP -> action HCprev
1935 | Glut.KEY_DOWN -> action HCnext
1936 | Glut.KEY_HOME -> action HCfirst
1937 | Glut.KEY_END -> action HClast
1938 | _ -> state.text
1940 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
1941 Glut.postRedisplay ()
1943 | _ -> ()
1946 | Outline (allowdel, active, first, outlines, qsearch) ->
1947 let maxrows = maxoutlinerows () in
1948 let calcfirst first active =
1949 if active > first
1950 then
1951 let rows = active - first in
1952 if rows > maxrows then active - maxrows else first
1953 else active
1955 let navigate incr =
1956 let active = active + incr in
1957 let active = max 0 (min active (Array.length outlines - 1)) in
1958 let first = calcfirst first active in
1959 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1960 Glut.postRedisplay ()
1962 let updownlevel incr =
1963 let len = Array.length outlines in
1964 let (_, curlevel, _, _) = outlines.(active) in
1965 let rec flow i =
1966 if i = len then i-1 else if i = -1 then 0 else
1967 let (_, l, _, _) = outlines.(i) in
1968 if l != curlevel then i else flow (i+incr)
1970 let active = flow active in
1971 let first = calcfirst first active in
1972 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1973 Glut.postRedisplay ()
1975 match key with
1976 | Glut.KEY_UP -> navigate ~-1
1977 | Glut.KEY_DOWN -> navigate 1
1978 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1979 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1981 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1982 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1984 | Glut.KEY_HOME ->
1985 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1986 Glut.postRedisplay ()
1988 | Glut.KEY_END ->
1989 let active = Array.length outlines - 1 in
1990 let first = max 0 (active - maxrows) in
1991 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1992 Glut.postRedisplay ()
1994 | _ -> ()
1997 let drawplaceholder l =
1998 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1999 GlDraw.rect
2000 (float l.pagex, float l.pagedispy)
2001 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2003 let x = float (if margin < 0 then -margin else l.pagex)
2004 and y = float (l.pagedispy + 13) in
2005 let font = Glut.BITMAP_8_BY_13 in
2006 GlDraw.color (0.0, 0.0, 0.0);
2007 GlPix.raster_pos ~x ~y ();
2008 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2009 ("Loading " ^ string_of_int (l.pageno + 1));
2012 let now () = Unix.gettimeofday ();;
2014 let drawpage l =
2015 let color =
2016 match state.mode with
2017 | Textentry _ -> scalecolor 0.4
2018 | View | Outline _ -> scalecolor 1.0
2019 | Birdseye (_, _, pageno, hooverpageno, _) ->
2020 if l.pageno = pageno
2021 then scalecolor 1.0
2022 else (
2023 if l.pageno = hooverpageno
2024 then scalecolor 0.9
2025 else scalecolor 0.8
2028 GlDraw.color color;
2029 begin match getopaque l.pageno with
2030 | Some (opaque, _) when validopaque opaque ->
2031 let a = now () in
2032 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2033 opaque;
2034 let b = now () in
2035 let d = b-.a in
2036 vlog "draw %d %f sec" l.pageno d;
2038 | _ ->
2039 drawplaceholder l;
2040 end;
2043 let scrollph y =
2044 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2045 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2046 let sh = float conf.winh /. sh in
2047 let sh = max sh (float conf.scrollh) in
2049 let percent =
2050 if state.y = state.maxy
2051 then 1.0
2052 else float y /. float maxy
2054 let position = (float conf.winh -. sh) *. percent in
2056 let position =
2057 if position +. sh > float conf.winh
2058 then float conf.winh -. sh
2059 else position
2061 position, sh;
2064 let scrollindicator () =
2065 GlDraw.color (0.64 , 0.64, 0.64);
2066 GlDraw.rect
2067 (float (conf.winw - conf.scrollw), 0.)
2068 (float conf.winw, float conf.winh)
2070 GlDraw.color (0.0, 0.0, 0.0);
2072 let position, sh = scrollph state.y in
2073 GlDraw.rect
2074 (float (conf.winw - conf.scrollw), position)
2075 (float conf.winw, position +. sh)
2079 let showsel margin =
2080 match state.mstate with
2081 | Mnone | Mscroll _ | Mpan _ ->
2084 | Msel ((x0, y0), (x1, y1)) ->
2085 let rec loop = function
2086 | l :: ls ->
2087 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2088 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2089 then
2090 match getopaque l.pageno with
2091 | Some (opaque, _) when validopaque opaque ->
2092 let oy = -l.pagey + l.pagedispy in
2093 seltext opaque
2094 (x0 - margin - state.x, y0,
2095 x1 - margin - state.x, y1) oy;
2097 | _ -> ()
2098 else loop ls
2099 | [] -> ()
2101 loop state.layout
2104 let showrects () =
2105 let panx = float state.x in
2106 Gl.enable `blend;
2107 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2108 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2109 List.iter
2110 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2111 List.iter (fun l ->
2112 if l.pageno = pageno
2113 then (
2114 let d = float (l.pagedispy - l.pagey) in
2115 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2116 GlDraw.begins `quads;
2118 GlDraw.vertex2 (x0+.panx, y0+.d);
2119 GlDraw.vertex2 (x1+.panx, y1+.d);
2120 GlDraw.vertex2 (x2+.panx, y2+.d);
2121 GlDraw.vertex2 (x3+.panx, y3+.d);
2123 GlDraw.ends ();
2125 ) state.layout
2126 ) state.rects
2128 Gl.disable `blend;
2131 let showoutline () =
2132 match state.mode with
2133 | Outline (allowdel, active, first, outlines, qsearch) ->
2134 Gl.enable `blend;
2135 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2136 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2137 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2138 Gl.disable `blend;
2140 GlDraw.color (1., 1., 1.);
2141 let font = Glut.BITMAP_9_BY_15 in
2142 let draw_string x y s =
2143 GlPix.raster_pos ~x ~y ();
2144 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2146 let rec loop row =
2147 if row = Array.length outlines || (row - first) * 16 > conf.winh
2148 then ()
2149 else (
2150 let (s, l, _, _) = outlines.(row) in
2151 let y = (row - first) * 16 in
2152 let x = 5 + 15*l in
2153 if row = active
2154 then (
2155 Gl.enable `blend;
2156 GlDraw.polygon_mode `both `line;
2157 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2158 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2159 GlDraw.rect (0., float (y + 1))
2160 (float (conf.winw - 1), float (y + 18));
2161 GlDraw.polygon_mode `both `fill;
2162 Gl.disable `blend;
2163 GlDraw.color (1., 1., 1.);
2165 draw_string (float x) (float (y + 16)) s;
2166 loop (row+1)
2169 loop first
2171 | _ -> ()
2174 let display () =
2175 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2176 GlDraw.viewport margin 0 state.w conf.winh;
2177 pagematrix ();
2178 GlClear.color (scalecolor 0.5);
2179 GlClear.clear [`color];
2180 if conf.zoom > 1.0
2181 then (
2182 Gl.enable `scissor_test;
2183 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2185 List.iter drawpage state.layout;
2186 if conf.zoom > 1.0
2187 then
2188 Gl.disable `scissor_test
2190 if state.x != 0
2191 then (
2192 let x = -.float state.x in
2193 GlMat.translate ~x ();
2195 showrects ();
2196 showsel margin;
2197 GlDraw.viewport 0 0 conf.winw conf.winh;
2198 winmatrix ();
2199 scrollindicator ();
2200 showoutline ();
2201 enttext ();
2202 Glut.swapBuffers ();
2205 let getunder x y =
2206 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2207 let x = x - margin - state.x in
2208 let rec f = function
2209 | l :: rest ->
2210 begin match getopaque l.pageno with
2211 | Some (opaque, _) when validopaque opaque ->
2212 let y = y - l.pagedispy in
2213 if y > 0
2214 then
2215 let y = l.pagey + y in
2216 let x = x - l.pagex in
2217 match whatsunder opaque x y with
2218 | Unone -> f rest
2219 | under -> under
2220 else
2221 f rest
2222 | _ ->
2223 f rest
2225 | [] -> Unone
2227 f state.layout
2230 let viewmouse button bstate x y =
2231 match button with
2232 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2233 let incr =
2234 if n = 3
2235 then
2236 -conf.scrollincr
2237 else
2238 conf.scrollincr
2240 let incr = incr * 2 in
2241 let y = clamp incr in
2242 gotoy_and_clear_text y
2244 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2245 if bstate = Glut.DOWN
2246 then (
2247 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2248 state.mstate <- Mpan (x, y)
2250 else
2251 state.mstate <- Mnone
2253 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2254 if bstate = Glut.DOWN
2255 then
2256 let position, sh = scrollph state.y in
2257 if y > truncate position && y < truncate (position +. sh)
2258 then
2259 state.mstate <- Mscroll
2260 else
2261 let percent = float y /. float conf.winh in
2262 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2263 gotoy desty;
2264 state.mstate <- Mscroll
2265 else
2266 state.mstate <- Mnone
2268 | Glut.LEFT_BUTTON ->
2269 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2270 begin match dest with
2271 | Ulinkgoto (pageno, top) ->
2272 if pageno >= 0
2273 then
2274 gotopage1 pageno top
2276 | Ulinkuri s ->
2277 print_endline s
2279 | Unone when bstate = Glut.DOWN ->
2280 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2281 state.mstate <- Mpan (x, y);
2283 | Unone | Utext _ ->
2284 if bstate = Glut.DOWN
2285 then (
2286 if conf.angle mod 360 = 0
2287 then (
2288 state.mstate <- Msel ((x, y), (x, y));
2289 Glut.postRedisplay ()
2292 else (
2293 match state.mstate with
2294 | Mnone -> ()
2296 | Mscroll ->
2297 state.mstate <- Mnone
2299 | Mpan _ ->
2300 Glut.setCursor Glut.CURSOR_INHERIT;
2301 state.mstate <- Mnone
2303 | Msel ((x0, y0), (x1, y1)) ->
2304 let f l =
2305 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2306 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2307 then
2308 match getopaque l.pageno with
2309 | Some (opaque, _) when validopaque opaque ->
2310 copysel opaque
2311 | _ -> ()
2313 List.iter f state.layout;
2314 copysel ""; (* ugly *)
2315 Glut.setCursor Glut.CURSOR_INHERIT;
2316 state.mstate <- Mnone;
2320 | _ -> ()
2323 let birdseyemouse button bstate x y
2324 (conf, leftx, pageno, hooverpageno, anchor) =
2325 match button with
2326 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2327 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2328 let rec loop = function
2329 | [] -> ()
2330 | l :: rest ->
2331 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2332 && x > margin && x < margin + l.pagew
2333 then (
2334 birdseyeoff (conf, leftx, l.pageno, hooverpageno, anchor) false;
2336 else loop rest
2338 loop state.layout
2339 | _ -> ()
2342 let mouse bstate button x y =
2343 match state.mode with
2344 | View -> viewmouse button bstate x y
2345 | Birdseye beye -> birdseyemouse button bstate x y beye
2346 | Textentry _ -> ()
2347 | Outline _ -> ()
2350 let mouse ~button ~state ~x ~y = mouse state button x y;;
2352 let motion ~x ~y =
2353 match state.mode with
2354 | Outline _ -> ()
2355 | _ ->
2356 match state.mstate with
2357 | Mnone -> ()
2359 | Mpan (x0, y0) ->
2360 let dx = x - x0
2361 and dy = y0 - y in
2362 state.mstate <- Mpan (x, y);
2363 if conf.zoom > 1.0 then state.x <- state.x + dx;
2364 let y = clamp dy in
2365 gotoy_and_clear_text y
2367 | Msel (a, _) ->
2368 state.mstate <- Msel (a, (x, y));
2369 Glut.postRedisplay ()
2371 | Mscroll ->
2372 let y = min conf.winh (max 0 y) in
2373 let percent = float y /. float conf.winh in
2374 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2375 gotoy_and_clear_text y
2378 let pmotion ~x ~y =
2379 match state.mode with
2380 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
2381 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2382 let rec loop = function
2383 | [] ->
2384 if hooverpageno != -1
2385 then (
2386 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
2387 Glut.postRedisplay ();
2389 | l :: rest ->
2390 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2391 && x > margin && x < margin + l.pagew
2392 then (
2393 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
2394 Glut.postRedisplay ();
2396 else loop rest
2398 loop state.layout
2400 | Outline _ -> ()
2401 | _ ->
2402 match state.mstate with
2403 | Mnone ->
2404 begin match getunder x y with
2405 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2406 | Ulinkuri uri ->
2407 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2408 Glut.setCursor Glut.CURSOR_INFO
2409 | Ulinkgoto (page, y) ->
2410 if conf.underinfo
2411 then showtext 'p' ("age: " ^ string_of_int page);
2412 Glut.setCursor Glut.CURSOR_INFO
2413 | Utext s ->
2414 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2415 Glut.setCursor Glut.CURSOR_TEXT
2418 | Mpan _ | Msel _ | Mscroll ->
2423 module State =
2424 struct
2425 open Parser
2427 let home =
2429 match Sys.os_type with
2430 | "Win32" -> Sys.getenv "HOMEPATH"
2431 | _ -> Sys.getenv "HOME"
2432 with exn ->
2433 prerr_endline
2434 ("Can not determine home directory location: " ^
2435 Printexc.to_string exn);
2439 let config_of c attrs =
2440 let apply c k v =
2442 match k with
2443 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2444 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2445 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2446 | "preload" -> { c with preload = bool_of_string v }
2447 | "page-bias" -> { c with pagebias = int_of_string v }
2448 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2449 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2450 | "crop-hack" -> { c with crophack = bool_of_string v }
2451 | "throttle" -> { c with showall = bool_of_string v }
2452 | "highlight-links" -> { c with hlinks = bool_of_string v }
2453 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2454 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2455 | "zoom" ->
2456 let zoom = float_of_string v /. 100. in
2457 let zoom = max 0.01 (min 2.2 zoom) in
2458 { c with zoom = zoom }
2459 | "presentation" -> { c with presentation = bool_of_string v }
2460 | "rotation-angle" -> { c with angle = int_of_string v }
2461 | "width" -> { c with winw = max 20 (int_of_string v) }
2462 | "height" -> { c with winh = max 20 (int_of_string v) }
2463 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2464 | "proportional-display" -> { c with proportional = bool_of_string v }
2465 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2466 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2467 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2468 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2469 | _ -> c
2470 with exn ->
2471 prerr_endline ("Error processing attribute (`" ^
2472 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2475 let rec fold c = function
2476 | [] -> c
2477 | (k, v) :: rest ->
2478 let c = apply c k v in
2479 fold c rest
2481 fold c attrs;
2484 let fromstring f pos n v d =
2485 try f v
2486 with exn ->
2487 dolog "Error processing attribute (%S=%S) at %d\n%s"
2488 n v pos (Printexc.to_string exn)
2493 let bookmark_of attrs =
2494 let rec fold title page rely = function
2495 | ("title", v) :: rest -> fold v page rely rest
2496 | ("page", v) :: rest -> fold title v rely rest
2497 | ("rely", v) :: rest -> fold title page v rest
2498 | _ :: rest -> fold title page rely rest
2499 | [] -> title, page, rely
2501 fold "invalid" "0" "0" attrs
2504 let doc_of attrs =
2505 let rec fold path page rely pan = function
2506 | ("path", v) :: rest -> fold v page rely pan rest
2507 | ("page", v) :: rest -> fold path v rely pan rest
2508 | ("rely", v) :: rest -> fold path page v pan rest
2509 | ("pan", v) :: rest -> fold path page rely v rest
2510 | _ :: rest -> fold path page rely pan rest
2511 | [] -> path, page, rely, pan
2513 fold "" "0" "0" "0" attrs
2516 let setconf dst src =
2517 dst.scrollw <- src.scrollw;
2518 dst.scrollh <- src.scrollh;
2519 dst.icase <- src.icase;
2520 dst.preload <- src.preload;
2521 dst.pagebias <- src.pagebias;
2522 dst.verbose <- src.verbose;
2523 dst.scrollincr <- src.scrollincr;
2524 dst.maxhfit <- src.maxhfit;
2525 dst.crophack <- src.crophack;
2526 dst.autoscroll <- src.autoscroll;
2527 dst.showall <- src.showall;
2528 dst.hlinks <- src.hlinks;
2529 dst.underinfo <- src.underinfo;
2530 dst.interpagespace <- src.interpagespace;
2531 dst.zoom <- src.zoom;
2532 dst.presentation <- src.presentation;
2533 dst.angle <- src.angle;
2534 dst.winw <- src.winw;
2535 dst.winh <- src.winh;
2536 dst.savebmarks <- src.savebmarks;
2537 dst.memlimit <- src.memlimit;
2538 dst.proportional <- src.proportional;
2539 dst.texcount <- src.texcount;
2540 dst.sliceheight <- src.sliceheight;
2541 dst.thumbw <- src.thumbw;
2544 let unent s =
2545 let l = String.length s in
2546 let b = Buffer.create l in
2547 unent b s 0 l;
2548 Buffer.contents b;
2551 let get s =
2552 let h = Hashtbl.create 10 in
2553 let dc = { defconf with angle = defconf.angle } in
2554 let rec toplevel v t spos epos =
2555 match t with
2556 | Vdata | Vcdata | Vend -> v
2557 | Vopen ("llppconfig", attrs, closed) ->
2558 if closed
2559 then v
2560 else { v with f = llppconfig }
2561 | Vopen _ ->
2562 error "unexpected subelement at top level" s spos
2563 | Vclose tag -> error "unexpected close at top level" s spos
2565 and llppconfig v t spos epos =
2566 match t with
2567 | Vdata | Vcdata | Vend -> v
2568 | Vopen ("defaults", attrs, closed) ->
2569 let c = config_of dc attrs in
2570 setconf dc c;
2571 if closed
2572 then v
2573 else { v with f = skip "defaults" (fun () -> v) }
2575 | Vopen ("doc", attrs, closed) ->
2576 let pathent, spage, srely, span = doc_of attrs in
2577 let path = unent pathent
2578 and pageno = fromstring int_of_string spos "page" spage 0
2579 and rely = fromstring float_of_string spos "rely" srely 0.0
2580 and pan = fromstring int_of_string spos "pan" span 0 in
2581 let c = config_of dc attrs in
2582 let anchor = (pageno, rely) in
2583 if closed
2584 then (Hashtbl.add h path (c, [], pan, anchor); v)
2585 else { v with f = doc path pan anchor c [] }
2587 | Vopen (tag, _, closed) ->
2588 error "unexpected subelement in llppconfig" s spos
2590 | Vclose "llppconfig" -> { v with f = toplevel }
2591 | Vclose tag -> error "unexpected close in llppconfig" s spos
2593 and doc path pan anchor c bookmarks v t spos epos =
2594 match t with
2595 | Vdata | Vcdata -> v
2596 | Vend -> error "unexpected end of input in doc" s spos
2597 | Vopen ("bookmarks", attrs, closed) ->
2598 { v with f = pbookmarks path pan anchor c bookmarks }
2600 | Vopen (tag, _, _) ->
2601 error "unexpected subelement in doc" s spos
2603 | Vclose "doc" ->
2604 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
2605 { v with f = llppconfig }
2607 | Vclose tag -> error "unexpected close in doc" s spos
2609 and pbookmarks path pan anchor c bookmarks v t spos epos =
2610 match t with
2611 | Vdata | Vcdata -> v
2612 | Vend -> error "unexpected end of input in bookmarks" s spos
2613 | Vopen ("item", attrs, closed) ->
2614 let titleent, spage, srely = bookmark_of attrs in
2615 let page = fromstring int_of_string spos "page" spage 0
2616 and rely = fromstring float_of_string spos "rely" srely 0.0 in
2617 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2618 if closed
2619 then { v with f = pbookmarks path pan anchor c bookmarks }
2620 else
2621 let f () = v in
2622 { v with f = skip "item" f }
2624 | Vopen _ ->
2625 error "unexpected subelement in bookmarks" s spos
2627 | Vclose "bookmarks" ->
2628 { v with f = doc path pan anchor c bookmarks }
2630 | Vclose tag -> error "unexpected close in bookmarks" s spos
2632 and skip tag f v t spos epos =
2633 match t with
2634 | Vdata | Vcdata -> v
2635 | Vend ->
2636 error ("unexpected end of input in skipped " ^ tag) s spos
2637 | Vopen (tag', _, closed) ->
2638 if closed
2639 then v
2640 else
2641 let f' () = { v with f = skip tag f } in
2642 { v with f = skip tag' f' }
2643 | Vclose ctag ->
2644 if tag = ctag
2645 then f ()
2646 else error ("unexpected close in skipped " ^ tag) s spos
2649 parse { f = toplevel; accu = () } s;
2650 h, dc;
2653 let do_load f ic =
2655 let len = in_channel_length ic in
2656 let s = String.create len in
2657 really_input ic s 0 len;
2658 f s;
2659 with
2660 | Parse_error (msg, s, pos) ->
2661 let subs = subs s pos in
2662 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2663 failwith ("parse error: " ^ s)
2665 | exn ->
2666 failwith ("config load error: " ^ Printexc.to_string exn)
2669 let path =
2670 let dir =
2672 let dir = Filename.concat home ".config" in
2673 if Sys.is_directory dir then dir else home
2674 with _ -> home
2676 Filename.concat dir "llpp.conf"
2679 let load1 f =
2680 if Sys.file_exists path
2681 then
2682 match
2683 (try Some (open_in_bin path)
2684 with exn ->
2685 prerr_endline
2686 ("Error opening configuation file `" ^ path ^ "': " ^
2687 Printexc.to_string exn);
2688 None
2690 with
2691 | Some ic ->
2692 begin try
2693 f (do_load get ic)
2694 with exn ->
2695 prerr_endline
2696 ("Error loading configuation from `" ^ path ^ "': " ^
2697 Printexc.to_string exn);
2698 end;
2699 close_in ic;
2701 | None -> ()
2702 else
2703 f (Hashtbl.create 0, defconf)
2706 let load () =
2707 let f (h, dc) =
2708 let pc, pb, px, pa =
2710 Hashtbl.find h (Filename.basename state.path)
2711 with Not_found -> dc, [], 0, (0, 0.0)
2713 setconf defconf dc;
2714 setconf conf pc;
2715 state.bookmarks <- pb;
2716 state.x <- px;
2717 cbput state.hists.nav pa;
2719 load1 f
2722 let add_attrs bb always dc c =
2723 let ob s a b =
2724 if always || a != b
2725 then Printf.bprintf bb "\n %s='%b'" s a
2726 and oi s a b =
2727 if always || a != b
2728 then Printf.bprintf bb "\n %s='%d'" s a
2729 and oz s a b =
2730 if always || a <> b
2731 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2733 let w, h =
2734 if always
2735 then dc.winw, dc.winh
2736 else
2737 match state.fullscreen with
2738 | Some wh -> wh
2739 | None -> c.winw, c.winh
2741 let zoom, presentation, interpagespace, showall=
2742 if always
2743 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2744 else
2745 match state.mode with
2746 | Birdseye (bc, _, _, _, _) ->
2747 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2748 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2750 oi "width" w dc.winw;
2751 oi "height" h dc.winh;
2752 oi "scroll-bar-width" c.scrollw dc.scrollw;
2753 oi "scroll-handle-height" c.scrollh dc.scrollh;
2754 ob "case-insensitive-search" c.icase dc.icase;
2755 ob "preload" c.preload dc.preload;
2756 oi "page-bias" c.pagebias dc.pagebias;
2757 oi "scroll-step" c.scrollincr dc.scrollincr;
2758 ob "max-height-fit" c.maxhfit dc.maxhfit;
2759 ob "crop-hack" c.crophack dc.crophack;
2760 ob "throttle" showall dc.showall;
2761 ob "highlight-links" c.hlinks dc.hlinks;
2762 ob "under-cursor-info" c.underinfo dc.underinfo;
2763 oi "vertical-margin" interpagespace dc.interpagespace;
2764 oz "zoom" zoom dc.zoom;
2765 ob "presentation" presentation dc.presentation;
2766 oi "rotation-angle" c.angle dc.angle;
2767 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2768 ob "proportional-display" c.proportional dc.proportional;
2769 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2770 oi "texcount" c.texcount dc.texcount;
2771 oi "slice-height" c.sliceheight dc.sliceheight;
2772 oi "thumbnail-width" c.thumbw dc.thumbw;
2775 let save () =
2776 let bb = Buffer.create 32768 in
2777 let f (h, dc) =
2778 Buffer.add_string bb "<llppconfig>\n<defaults ";
2779 add_attrs bb true dc dc;
2780 Buffer.add_string bb "/>\n";
2782 let adddoc path x anchor c bookmarks =
2783 if bookmarks == [] && c = dc && anchor = emptyanchor
2784 then ()
2785 else (
2786 Printf.bprintf bb "<doc path='%s'"
2787 (enent path 0 (String.length path));
2789 if anchor <> emptyanchor
2790 then (
2791 let n, y = anchor in
2792 Printf.bprintf bb " page='%d'" n;
2793 Printf.bprintf bb " rely='%f'" y;
2796 if x != 0
2797 then Printf.bprintf bb " pan='%d'" x;
2799 add_attrs bb false dc c;
2801 begin match bookmarks with
2802 | [] -> Buffer.add_string bb "/>\n"
2803 | _ ->
2804 Buffer.add_string bb ">\n<bookmarks>\n";
2805 List.iter (fun (title, _level, page, rely) ->
2806 Printf.bprintf bb
2807 "<item title='%s' page='%d' rely='%f'/>\n"
2808 (enent title 0 (String.length title))
2809 page
2810 rely
2811 ) bookmarks;
2812 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2813 end;
2817 let x =
2818 match state.mode with
2819 | Birdseye (_, x, _, _, _) -> x
2820 | _ -> state.x
2822 let basename = Filename.basename state.path in
2823 adddoc basename x (getanchor ()) conf
2824 (if conf.savebmarks then state.bookmarks else []);
2826 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2827 if basename <> path
2828 then adddoc path x y c bookmarks
2829 ) h;
2830 Buffer.add_string bb "</llppconfig>";
2832 load1 f;
2833 if Buffer.length bb > 0
2834 then
2836 let tmp = path ^ ".tmp" in
2837 let oc = open_out_bin tmp in
2838 Buffer.output_buffer oc bb;
2839 close_out oc;
2840 Sys.rename tmp path;
2841 with exn ->
2842 prerr_endline
2843 ("error while saving configuration: " ^ Printexc.to_string exn)
2845 end;;
2847 let () =
2848 Arg.parse
2849 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2850 (fun s -> state.path <- s)
2851 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2853 if String.length state.path = 0
2854 then (prerr_endline "filename missing"; exit 1);
2856 State.load ();
2858 let _ = Glut.init Sys.argv in
2859 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2860 let () = Glut.initWindowSize conf.winw conf.winh in
2861 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
2863 let csock, ssock =
2864 if Sys.os_type = "Unix"
2865 then
2866 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2867 else
2868 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2869 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2870 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2871 Unix.bind sock addr;
2872 Unix.listen sock 1;
2873 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2874 Unix.connect csock addr;
2875 let ssock, _ = Unix.accept sock in
2876 Unix.close sock;
2877 let opts sock =
2878 Unix.setsockopt sock Unix.TCP_NODELAY true;
2879 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2881 opts ssock;
2882 opts csock;
2883 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2884 ssock, csock
2887 let () = Glut.displayFunc display in
2888 let () = Glut.reshapeFunc reshape in
2889 let () = Glut.keyboardFunc keyboard in
2890 let () = Glut.specialFunc special in
2891 let () = Glut.idleFunc (Some idle) in
2892 let () = Glut.mouseFunc mouse in
2893 let () = Glut.motionFunc motion in
2894 let () = Glut.passiveMotionFunc pmotion in
2896 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2897 state.csock <- csock;
2898 state.ssock <- ssock;
2899 state.text <- "Opening " ^ state.path;
2900 writeopen state.path state.password;
2902 at_exit State.save;
2904 let rec handlelablglutbug () =
2906 Glut.mainLoop ();
2907 with Glut.BadEnum "key in special_of_int" ->
2908 showtext '!' " LablGlut bug: special key not recognized";
2909 handlelablglutbug ()
2911 handlelablglutbug ();