Yet another batch of bird's eye pgup/down changes
[llpp.git] / main.ml
blob7b48091e1a412e70a53207aab0e06e59d4939c9c
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 -> Glut.postRedisplay ()
1801 | _ :: rest -> loop rest
1803 loop state.layout;
1804 state.birdseye <- Some (conf, leftx, pageno, hooverpageno)
1806 | Glut.KEY_DOWN ->
1807 let pageno = min (state.pagecount - 1) (pageno + 1) in
1808 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1809 let rec loop = function
1810 | [] ->
1811 let y, h = getpageyh pageno in
1812 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1813 gotoy (clamp dy)
1814 | l :: rest when l.pageno = pageno ->
1815 if l.pagevh != l.pageh
1816 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1817 else Glut.postRedisplay ()
1818 | l :: rest -> loop rest
1820 loop state.layout
1822 | Glut.KEY_PAGE_UP ->
1823 begin match state.layout with
1824 | l :: _ ->
1825 if l.pagey != 0
1826 then (
1827 state.birdseye <- Some (conf, leftx, l.pageno, hooverpageno);
1828 gotopage1nonav l.pageno 0;
1830 else (
1831 let layout = layout (state.y-conf.winh) conf.winh in
1832 match layout with
1833 | [] -> gotoy (clamp (-conf.winh))
1834 | l :: _ ->
1835 state.birdseye <- Some (conf, leftx, l.pageno, hooverpageno);
1836 gotopage1nonav l.pageno 0
1839 | [] -> gotoy (clamp (-conf.winh))
1840 end;
1842 | Glut.KEY_PAGE_DOWN ->
1843 begin match List.rev state.layout with
1844 | l :: _ ->
1845 state.birdseye <- Some (conf, leftx, l.pageno, hooverpageno);
1846 gotoy (clamp (l.pagedispy + l.pageh))
1847 | [] -> gotoy (clamp conf.winh)
1848 end;
1850 | Glut.KEY_HOME ->
1851 state.birdseye <- Some (conf, leftx, 0, hooverpageno);
1852 gotopage1nonav 0 0
1854 | Glut.KEY_END ->
1855 let pageno = state.pagecount - 1 in
1856 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1857 if not (pagevisible state.layout pageno)
1858 then
1859 let h =
1860 match List.rev state.pdims with
1861 | [] -> conf.winh
1862 | (_, _, h, _) :: _ -> h
1864 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1865 else Glut.postRedisplay ();
1866 | _ -> ()
1869 let special ~key ~x ~y =
1870 match state.outline, state.birdseye with
1871 | None, _ when key = Glut.KEY_F9 ->
1872 togglebirdseye ();
1873 reshape conf.winw conf.winh;
1875 | None, (Some vals) ->
1876 birdseyespecial key x y vals
1878 | None, None ->
1879 begin match state.textentry with
1880 | None ->
1881 let y =
1882 match key with
1883 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1884 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1885 | Glut.KEY_DOWN -> clamp conf.scrollincr
1886 | Glut.KEY_PAGE_UP ->
1887 if Glut.getModifiers () land Glut.active_ctrl != 0
1888 then
1889 match state.layout with
1890 | [] -> state.y
1891 | l :: _ -> state.y - l.pagey
1892 else
1893 clamp (-conf.winh)
1894 | Glut.KEY_PAGE_DOWN ->
1895 if Glut.getModifiers () land Glut.active_ctrl != 0
1896 then
1897 match List.rev state.layout with
1898 | [] -> state.y
1899 | l :: _ -> getpagey l.pageno
1900 else
1901 clamp conf.winh
1902 | Glut.KEY_HOME -> addnav (); 0
1903 | Glut.KEY_END ->
1904 addnav ();
1905 state.maxy - (if conf.maxhfit then conf.winh else 0)
1907 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1908 state.x <- state.x - 10;
1909 state.y
1910 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1911 state.x <- state.x + 10;
1912 state.y
1914 | _ -> state.y
1916 gotoy_and_clear_text y
1918 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1919 let s =
1920 match key with
1921 | Glut.KEY_UP -> action HCprev
1922 | Glut.KEY_DOWN -> action HCnext
1923 | Glut.KEY_HOME -> action HCfirst
1924 | Glut.KEY_END -> action HClast
1925 | _ -> state.text
1927 state.textentry <- Some (c, s, onhist, onkey, ondone);
1928 Glut.postRedisplay ()
1930 | _ -> ()
1933 | Some (allowdel, active, first, outlines, qsearch), _ ->
1934 let maxrows = maxoutlinerows () in
1935 let calcfirst first active =
1936 if active > first
1937 then
1938 let rows = active - first in
1939 if rows > maxrows then active - maxrows else first
1940 else active
1942 let navigate incr =
1943 let active = active + incr in
1944 let active = max 0 (min active (Array.length outlines - 1)) in
1945 let first = calcfirst first active in
1946 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1947 Glut.postRedisplay ()
1949 let updownlevel incr =
1950 let len = Array.length outlines in
1951 let (_, curlevel, _, _) = outlines.(active) in
1952 let rec flow i =
1953 if i = len then i-1 else if i = -1 then 0 else
1954 let (_, l, _, _) = outlines.(i) in
1955 if l != curlevel then i else flow (i+incr)
1957 let active = flow active in
1958 let first = calcfirst first active in
1959 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1960 Glut.postRedisplay ()
1962 match key with
1963 | Glut.KEY_UP -> navigate ~-1
1964 | Glut.KEY_DOWN -> navigate 1
1965 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1966 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1968 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1969 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1971 | Glut.KEY_HOME ->
1972 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1973 Glut.postRedisplay ()
1975 | Glut.KEY_END ->
1976 let active = Array.length outlines - 1 in
1977 let first = max 0 (active - maxrows) in
1978 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1979 Glut.postRedisplay ()
1981 | _ -> ()
1984 let drawplaceholder l =
1985 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1986 GlDraw.rect
1987 (float l.pagex, float l.pagedispy)
1988 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1990 let x = float (if margin < 0 then -margin else l.pagex)
1991 and y = float (l.pagedispy + 13) in
1992 let font = Glut.BITMAP_8_BY_13 in
1993 GlDraw.color (0.0, 0.0, 0.0);
1994 GlPix.raster_pos ~x ~y ();
1995 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1996 ("Loading " ^ string_of_int (l.pageno + 1));
1999 let now () = Unix.gettimeofday ();;
2001 let drawpage l =
2002 if state.textentry = None
2003 then (
2004 match state.birdseye with
2005 | None -> GlDraw.color (scalecolor 1.0);
2006 | Some (_, _, pageno, hooverpageno) ->
2007 let color =
2008 if l.pageno = pageno
2009 then 1.0
2010 else (
2011 if l.pageno = hooverpageno
2012 then 0.9
2013 else 0.8
2016 GlDraw.color (scalecolor color);
2018 else GlDraw.color (scalecolor 0.4);
2019 begin match getopaque l.pageno with
2020 | Some (opaque, _) when validopaque opaque ->
2021 let a = now () in
2022 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2023 opaque;
2024 let b = now () in
2025 let d = b-.a in
2026 vlog "draw %d %f sec" l.pageno d;
2028 | _ ->
2029 drawplaceholder l;
2030 end;
2033 let scrollph y =
2034 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2035 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2036 let sh = float conf.winh /. sh in
2037 let sh = max sh (float conf.scrollh) in
2039 let percent =
2040 if state.y = state.maxy
2041 then 1.0
2042 else float y /. float maxy
2044 let position = (float conf.winh -. sh) *. percent in
2046 let position =
2047 if position +. sh > float conf.winh
2048 then float conf.winh -. sh
2049 else position
2051 position, sh;
2054 let scrollindicator () =
2055 GlDraw.color (0.64 , 0.64, 0.64);
2056 GlDraw.rect
2057 (float (conf.winw - conf.scrollw), 0.)
2058 (float conf.winw, float conf.winh)
2060 GlDraw.color (0.0, 0.0, 0.0);
2062 let position, sh = scrollph state.y in
2063 GlDraw.rect
2064 (float (conf.winw - conf.scrollw), position)
2065 (float conf.winw, position +. sh)
2069 let showsel margin =
2070 match state.mstate with
2071 | Mnone | Mscroll _ | Mpan _ ->
2074 | Msel ((x0, y0), (x1, y1)) ->
2075 let rec loop = function
2076 | l :: ls ->
2077 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2078 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2079 then
2080 match getopaque l.pageno with
2081 | Some (opaque, _) when validopaque opaque ->
2082 let oy = -l.pagey + l.pagedispy in
2083 seltext opaque
2084 (x0 - margin - state.x, y0,
2085 x1 - margin - state.x, y1) oy;
2087 | _ -> ()
2088 else loop ls
2089 | [] -> ()
2091 loop state.layout
2094 let showrects () =
2095 let panx = float state.x in
2096 Gl.enable `blend;
2097 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2098 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2099 List.iter
2100 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2101 List.iter (fun l ->
2102 if l.pageno = pageno
2103 then (
2104 let d = float (l.pagedispy - l.pagey) in
2105 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2106 GlDraw.begins `quads;
2108 GlDraw.vertex2 (x0+.panx, y0+.d);
2109 GlDraw.vertex2 (x1+.panx, y1+.d);
2110 GlDraw.vertex2 (x2+.panx, y2+.d);
2111 GlDraw.vertex2 (x3+.panx, y3+.d);
2113 GlDraw.ends ();
2115 ) state.layout
2116 ) state.rects
2118 Gl.disable `blend;
2121 let showoutline = function
2122 | None -> ()
2123 | Some (allowdel, active, first, outlines, qsearch) ->
2124 Gl.enable `blend;
2125 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2126 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2127 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2128 Gl.disable `blend;
2130 GlDraw.color (1., 1., 1.);
2131 let font = Glut.BITMAP_9_BY_15 in
2132 let draw_string x y s =
2133 GlPix.raster_pos ~x ~y ();
2134 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2136 let rec loop row =
2137 if row = Array.length outlines || (row - first) * 16 > conf.winh
2138 then ()
2139 else (
2140 let (s, l, _, _) = outlines.(row) in
2141 let y = (row - first) * 16 in
2142 let x = 5 + 15*l in
2143 if row = active
2144 then (
2145 Gl.enable `blend;
2146 GlDraw.polygon_mode `both `line;
2147 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2148 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2149 GlDraw.rect (0., float (y + 1))
2150 (float (conf.winw - 1), float (y + 18));
2151 GlDraw.polygon_mode `both `fill;
2152 Gl.disable `blend;
2153 GlDraw.color (1., 1., 1.);
2155 draw_string (float x) (float (y + 16)) s;
2156 loop (row+1)
2159 loop first
2162 let display () =
2163 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2164 GlDraw.viewport margin 0 state.w conf.winh;
2165 pagematrix ();
2166 GlClear.color (scalecolor 0.5);
2167 GlClear.clear [`color];
2168 if state.x != 0
2169 then (
2170 let x = float state.x in
2171 GlMat.translate ~x ();
2173 if conf.zoom > 1.0
2174 then (
2175 Gl.enable `scissor_test;
2176 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2178 List.iter drawpage state.layout;
2179 if conf.zoom > 1.0
2180 then
2181 Gl.disable `scissor_test
2183 if state.x != 0
2184 then (
2185 let x = -.float state.x in
2186 GlMat.translate ~x ();
2188 showrects ();
2189 showsel margin;
2190 GlDraw.viewport 0 0 conf.winw conf.winh;
2191 winmatrix ();
2192 scrollindicator ();
2193 showoutline state.outline;
2194 enttext ();
2195 Glut.swapBuffers ();
2198 let getunder x y =
2199 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2200 let x = x - margin - state.x in
2201 let rec f = function
2202 | l :: rest ->
2203 begin match getopaque l.pageno with
2204 | Some (opaque, _) when validopaque opaque ->
2205 let y = y - l.pagedispy in
2206 if y > 0
2207 then
2208 let y = l.pagey + y in
2209 let x = x - l.pagex in
2210 match whatsunder opaque x y with
2211 | Unone -> f rest
2212 | under -> under
2213 else
2214 f rest
2215 | _ ->
2216 f rest
2218 | [] -> Unone
2220 f state.layout
2223 let mouse ~button ~bstate ~x ~y =
2224 match button with
2225 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2226 let incr =
2227 if n = 3
2228 then
2229 -conf.scrollincr
2230 else
2231 conf.scrollincr
2233 let incr = incr * 2 in
2234 let y = clamp incr in
2235 gotoy_and_clear_text y
2237 | Glut.LEFT_BUTTON when state.outline = None
2238 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2239 if bstate = Glut.DOWN
2240 then (
2241 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2242 state.mstate <- Mpan (x, y)
2244 else
2245 state.mstate <- Mnone
2247 | Glut.LEFT_BUTTON
2248 when state.outline = None && x > conf.winw - conf.scrollw ->
2249 if bstate = Glut.DOWN
2250 then
2251 let position, sh = scrollph state.y in
2252 if y > truncate position && y < truncate (position +. sh)
2253 then
2254 state.mstate <- Mscroll
2255 else
2256 let percent = float y /. float conf.winh in
2257 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2258 gotoy desty;
2259 state.mstate <- Mscroll
2260 else
2261 state.mstate <- Mnone
2263 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2264 begin match state.birdseye with
2265 | Some (conf, leftx, pageno, hooverpageno) ->
2266 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2267 let rec loop = function
2268 | [] -> ()
2269 | l :: rest ->
2270 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2271 && x > margin && x < margin + l.pagew
2272 then (
2273 birdseyeoff (conf, leftx, l.pageno, hooverpageno);
2274 reshape conf.winw conf.winh;
2275 state.anchor <- (l.pageno, 0.0);
2277 else loop rest
2279 loop state.layout;
2280 | None -> () (* impossible *)
2283 | Glut.LEFT_BUTTON when state.outline = None ->
2284 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2285 begin match dest with
2286 | Ulinkgoto (pageno, top) ->
2287 if pageno >= 0
2288 then
2289 gotopage1 pageno top
2291 | Ulinkuri s ->
2292 print_endline s
2294 | Unone when bstate = Glut.DOWN ->
2295 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2296 state.mstate <- Mpan (x, y);
2298 | Unone | Utext _ ->
2299 if bstate = Glut.DOWN
2300 then (
2301 if conf.angle mod 360 = 0
2302 then (
2303 state.mstate <- Msel ((x, y), (x, y));
2304 Glut.postRedisplay ()
2307 else (
2308 match state.mstate with
2309 | Mnone -> ()
2311 | Mscroll ->
2312 state.mstate <- Mnone
2314 | Mpan _ ->
2315 Glut.setCursor Glut.CURSOR_INHERIT;
2316 state.mstate <- Mnone
2318 | Msel ((x0, y0), (x1, y1)) ->
2319 let f l =
2320 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2321 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2322 then
2323 match getopaque l.pageno with
2324 | Some (opaque, _) when validopaque opaque ->
2325 copysel opaque
2326 | _ -> ()
2328 List.iter f state.layout;
2329 copysel ""; (* ugly *)
2330 Glut.setCursor Glut.CURSOR_INHERIT;
2331 state.mstate <- Mnone;
2335 | _ ->
2338 let mouse ~button ~state ~x ~y = mouse button state x y;;
2340 let motion ~x ~y =
2341 if state.outline = None
2342 then
2343 match state.mstate with
2344 | Mnone -> ()
2346 | Mpan (x0, y0) ->
2347 let dx = x - x0
2348 and dy = y0 - y in
2349 state.mstate <- Mpan (x, y);
2350 if conf.zoom > 1.0 then state.x <- state.x + dx;
2351 let y = clamp dy in
2352 gotoy_and_clear_text y
2354 | Msel (a, _) ->
2355 state.mstate <- Msel (a, (x, y));
2356 Glut.postRedisplay ()
2358 | Mscroll ->
2359 let y = min conf.winh (max 0 y) in
2360 let percent = float y /. float conf.winh in
2361 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2362 gotoy_and_clear_text y
2365 let pmotion ~x ~y =
2366 match state.birdseye with
2367 | Some (conf, leftx, pageno, hooverpageno) ->
2368 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2369 let rec loop = function
2370 | [] ->
2371 if hooverpageno != -1
2372 then (
2373 state.birdseye <- Some (conf, leftx, pageno, -1);
2374 Glut.postRedisplay ();
2376 | l :: rest ->
2377 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2378 && x > margin && x < margin + l.pagew
2379 then (
2380 state.birdseye <- Some (conf, leftx, pageno, l.pageno);
2381 Glut.postRedisplay ();
2383 else loop rest
2385 loop state.layout
2387 | None ->
2388 if state.outline = None
2389 then
2390 match state.mstate with
2391 | Mnone ->
2392 begin match getunder x y with
2393 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2394 | Ulinkuri uri ->
2395 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2396 Glut.setCursor Glut.CURSOR_INFO
2397 | Ulinkgoto (page, y) ->
2398 if conf.underinfo
2399 then showtext 'p' ("age: " ^ string_of_int page);
2400 Glut.setCursor Glut.CURSOR_INFO
2401 | Utext s ->
2402 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2403 Glut.setCursor Glut.CURSOR_TEXT
2406 | Mpan _ | Msel _ | Mscroll ->
2411 module State =
2412 struct
2413 open Parser
2415 let home =
2417 match Sys.os_type with
2418 | "Win32" -> Sys.getenv "HOMEPATH"
2419 | _ -> Sys.getenv "HOME"
2420 with exn ->
2421 prerr_endline
2422 ("Can not determine home directory location: " ^
2423 Printexc.to_string exn);
2427 let config_of c attrs =
2428 let apply c k v =
2430 match k with
2431 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2432 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2433 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2434 | "preload" -> { c with preload = bool_of_string v }
2435 | "page-bias" -> { c with pagebias = int_of_string v }
2436 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2437 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2438 | "crop-hack" -> { c with crophack = bool_of_string v }
2439 | "throttle" -> { c with showall = bool_of_string v }
2440 | "highlight-links" -> { c with hlinks = bool_of_string v }
2441 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2442 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2443 | "zoom" ->
2444 let zoom = float_of_string v /. 100. in
2445 let zoom = max 0.01 (min 2.2 zoom) in
2446 { c with zoom = zoom }
2447 | "presentation" -> { c with presentation = bool_of_string v }
2448 | "rotation-angle" -> { c with angle = int_of_string v }
2449 | "width" -> { c with winw = max 20 (int_of_string v) }
2450 | "height" -> { c with winh = max 20 (int_of_string v) }
2451 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2452 | "proportional-display" -> { c with proportional = bool_of_string v }
2453 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2454 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2455 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2456 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2457 | _ -> c
2458 with exn ->
2459 prerr_endline ("Error processing attribute (`" ^
2460 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2463 let rec fold c = function
2464 | [] -> c
2465 | (k, v) :: rest ->
2466 let c = apply c k v in
2467 fold c rest
2469 fold c attrs;
2472 let bookmark_of attrs =
2473 let rec fold title page rely = function
2474 | ("title", v) :: rest -> fold v page rely rest
2475 | ("page", v) :: rest -> fold title v rely rest
2476 | ("rely", v) :: rest -> fold title page v rest
2477 | _ :: rest -> fold title page rely rest
2478 | [] -> title, page, rely
2480 fold "invalid" "0" "0" attrs
2483 let setconf dst src =
2484 dst.scrollw <- src.scrollw;
2485 dst.scrollh <- src.scrollh;
2486 dst.icase <- src.icase;
2487 dst.preload <- src.preload;
2488 dst.pagebias <- src.pagebias;
2489 dst.verbose <- src.verbose;
2490 dst.scrollincr <- src.scrollincr;
2491 dst.maxhfit <- src.maxhfit;
2492 dst.crophack <- src.crophack;
2493 dst.autoscroll <- src.autoscroll;
2494 dst.showall <- src.showall;
2495 dst.hlinks <- src.hlinks;
2496 dst.underinfo <- src.underinfo;
2497 dst.interpagespace <- src.interpagespace;
2498 dst.zoom <- src.zoom;
2499 dst.presentation <- src.presentation;
2500 dst.angle <- src.angle;
2501 dst.winw <- src.winw;
2502 dst.winh <- src.winh;
2503 dst.savebmarks <- src.savebmarks;
2504 dst.memlimit <- src.memlimit;
2505 dst.proportional <- src.proportional;
2506 dst.texcount <- src.texcount;
2507 dst.sliceheight <- src.sliceheight;
2508 dst.thumbw <- src.thumbw;
2511 let unent s =
2512 let l = String.length s in
2513 let b = Buffer.create l in
2514 unent b s 0 l;
2515 Buffer.contents b;
2518 let get s =
2519 let h = Hashtbl.create 10 in
2520 let dc = { defconf with angle = defconf.angle } in
2521 let rec toplevel v t spos epos =
2522 match t with
2523 | Vdata | Vcdata | Vend -> v
2524 | Vopen ("llppconfig", attrs, closed) ->
2525 if closed
2526 then v
2527 else { v with f = llppconfig }
2528 | Vopen _ ->
2529 error "unexpected subelement at top level" s spos
2530 | Vclose tag -> error "unexpected close at top level" s spos
2532 and llppconfig v t spos epos =
2533 match t with
2534 | Vdata | Vcdata | Vend -> v
2535 | Vopen ("defaults", attrs, closed) ->
2536 let c = config_of dc attrs in
2537 setconf dc c;
2538 if closed
2539 then v
2540 else { v with f = skip "defaults" (fun () -> v) }
2542 | Vopen ("doc", attrs, closed) ->
2543 let pathent =
2545 List.assoc "path" attrs
2546 with Not_found -> error "doc is missing path attribute" s spos
2548 let path = unent pathent in
2549 let c = config_of dc attrs in
2550 let pageno, rely, x =
2551 let safef f n v d =
2552 try f v
2553 with exn ->
2554 dolog "error accessing %s (%S) at postion %d:\n %s"
2555 n v spos (Printexc.to_string exn);
2558 let rec fold pageno rely x = function
2559 | [] -> pageno, rely, x
2560 | ("rely", v) :: rest ->
2561 fold pageno (safef float_of_string "rely" v 0.0) x rest
2562 | ("page", v) :: rest ->
2563 fold (safef int_of_string "page" v 0) rely x rest
2564 | ("x", v) :: rest ->
2565 fold pageno rely (safef int_of_string "x" v 0) rest
2566 | _ :: rest ->
2567 fold pageno rely x rest
2569 fold 0 0.0 0 attrs
2571 let anchor = (pageno, rely) in
2572 if closed
2573 then (Hashtbl.add h path (c, [], x, anchor); v)
2574 else { v with f = doc path x anchor c [] }
2576 | Vopen (tag, _, closed) ->
2577 error "unexpected subelement in llppconfig" s spos
2579 | Vclose "llppconfig" -> { v with f = toplevel }
2580 | Vclose tag -> error "unexpected close in llppconfig" s spos
2582 and doc path x anchor c bookmarks v t spos epos =
2583 match t with
2584 | Vdata | Vcdata -> v
2585 | Vend -> error "unexpected end of input in doc" s spos
2586 | Vopen ("bookmarks", attrs, closed) ->
2587 { v with f = pbookmarks path x anchor c bookmarks }
2589 | Vopen (tag, _, _) ->
2590 error "unexpected subelement in doc" s spos
2592 | Vclose "doc" ->
2593 Hashtbl.add h path (c, List.rev bookmarks, x, anchor);
2594 { v with f = llppconfig }
2596 | Vclose tag -> error "unexpected close in doc" s spos
2598 and pbookmarks path x anchor c bookmarks v t spos epos =
2599 match t with
2600 | Vdata | Vcdata -> v
2601 | Vend -> error "unexpected end of input in bookmarks" s spos
2602 | Vopen ("item", attrs, closed) ->
2603 let titleent, spage, srely = bookmark_of attrs in
2604 let page =
2606 int_of_string spage
2607 with exn ->
2608 dolog "Failed to convert page %S to integer: %s"
2609 spage (Printexc.to_string exn);
2612 let rely =
2614 float_of_string srely
2615 with exn ->
2616 dolog "Failed to convert rely %S to real: %s"
2617 srely (Printexc.to_string exn);
2620 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2621 if closed
2622 then { v with f = pbookmarks path x anchor c bookmarks }
2623 else
2624 let f () = v in
2625 { v with f = skip "item" f }
2627 | Vopen _ ->
2628 error "unexpected subelement in bookmarks" s spos
2630 | Vclose "bookmarks" ->
2631 { v with f = doc path x anchor c bookmarks }
2633 | Vclose tag -> error "unexpected close in bookmarks" s spos
2635 and skip tag f v t spos epos =
2636 match t with
2637 | Vdata | Vcdata -> v
2638 | Vend ->
2639 error ("unexpected end of input in skipped " ^ tag) s spos
2640 | Vopen (tag', _, closed) ->
2641 if closed
2642 then v
2643 else
2644 let f' () = { v with f = skip tag f } in
2645 { v with f = skip tag' f' }
2646 | Vclose ctag ->
2647 if tag = ctag
2648 then f ()
2649 else error ("unexpected close in skipped " ^ tag) s spos
2652 parse { f = toplevel; accu = () } s;
2653 h, dc;
2656 let do_load f ic =
2658 let len = in_channel_length ic in
2659 let s = String.create len in
2660 really_input ic s 0 len;
2661 f s;
2662 with
2663 | Parse_error (msg, s, pos) ->
2664 let subs = subs s pos in
2665 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2666 failwith ("parse error: " ^ s)
2668 | exn ->
2669 failwith ("config load error: " ^ Printexc.to_string exn)
2672 let path =
2673 let dir =
2675 let dir = Filename.concat home ".config" in
2676 if Sys.is_directory dir then dir else home
2677 with _ -> home
2679 Filename.concat dir "llpp.conf"
2682 let load1 f =
2683 if Sys.file_exists path
2684 then
2685 match
2686 (try Some (open_in_bin path)
2687 with exn ->
2688 prerr_endline
2689 ("Error opening configuation file `" ^ path ^ "': " ^
2690 Printexc.to_string exn);
2691 None
2693 with
2694 | Some ic ->
2695 begin try
2696 f (do_load get ic)
2697 with exn ->
2698 prerr_endline
2699 ("Error loading configuation from `" ^ path ^ "': " ^
2700 Printexc.to_string exn);
2701 end;
2702 close_in ic;
2704 | None -> ()
2705 else
2706 f (Hashtbl.create 0, defconf)
2709 let load () =
2710 let f (h, dc) =
2711 let pc, pb, px, pa =
2713 Hashtbl.find h state.path
2714 with Not_found -> dc, [], 0, (0, 0.0)
2716 setconf defconf dc;
2717 setconf conf pc;
2718 state.bookmarks <- pb;
2719 state.x <- px;
2720 cbput state.hists.nav pa;
2722 load1 f
2725 let add_attrs bb always dc c =
2726 let ob s a b =
2727 if always || a != b
2728 then Printf.bprintf bb "\n %s='%b'" s a
2729 and oi s a b =
2730 if always || a != b
2731 then Printf.bprintf bb "\n %s='%d'" s a
2732 and oz s a b =
2733 if always || a <> b
2734 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2736 let w, h =
2737 if always
2738 then dc.winw, dc.winh
2739 else
2740 match state.fullscreen with
2741 | Some wh -> wh
2742 | None -> c.winw, c.winh
2744 let zoom, presentation, interpagespace, showall=
2745 if always
2746 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2747 else
2748 match state.birdseye with
2749 | Some (bc, _, _, _) ->
2750 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2751 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2753 oi "width" w dc.winw;
2754 oi "height" h dc.winh;
2755 oi "scroll-bar-width" c.scrollw dc.scrollw;
2756 oi "scroll-handle-height" c.scrollh dc.scrollh;
2757 ob "case-insensitive-search" c.icase dc.icase;
2758 ob "preload" c.preload dc.preload;
2759 oi "page-bias" c.pagebias dc.pagebias;
2760 oi "scroll-step" c.scrollincr dc.scrollincr;
2761 ob "max-height-fit" c.maxhfit dc.maxhfit;
2762 ob "crop-hack" c.crophack dc.crophack;
2763 ob "throttle" showall dc.showall;
2764 ob "highlight-links" c.hlinks dc.hlinks;
2765 ob "under-cursor-info" c.underinfo dc.underinfo;
2766 oi "vertical-margin" interpagespace dc.interpagespace;
2767 oz "zoom" zoom dc.zoom;
2768 ob "presentation" presentation dc.presentation;
2769 oi "rotation-angle" c.angle dc.angle;
2770 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2771 ob "proportional-display" c.proportional dc.proportional;
2772 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2773 oi "texcount" c.texcount dc.texcount;
2774 oi "slice-height" c.sliceheight dc.sliceheight;
2775 oi "thumbnail-width" c.thumbw dc.thumbw;
2778 let save () =
2779 let bb = Buffer.create 32768 in
2780 let f (h, dc) =
2781 Buffer.add_string bb "<llppconfig>\n<defaults ";
2782 add_attrs bb true dc dc;
2783 Buffer.add_string bb "/>\n";
2785 let adddoc path x anchor c bookmarks =
2786 if bookmarks == [] && c = dc && anchor = emptyanchor
2787 then ()
2788 else (
2789 Printf.bprintf bb "<doc path='%s'"
2790 (enent path 0 (String.length path));
2792 if anchor <> emptyanchor
2793 then (
2794 let n, y = anchor in
2795 Printf.bprintf bb " page='%d'" n;
2796 Printf.bprintf bb " rely='%f'" y;
2799 if x != 0
2800 then Printf.bprintf bb " pan='%d'" x;
2802 add_attrs bb false dc c;
2804 begin match bookmarks with
2805 | [] -> Buffer.add_string bb "/>\n"
2806 | _ ->
2807 Buffer.add_string bb ">\n<bookmarks>\n";
2808 List.iter (fun (title, _level, page, rely) ->
2809 Printf.bprintf bb
2810 "<item title='%s' page='%d' rely='%f'/>\n"
2811 (enent title 0 (String.length title))
2812 page
2813 rely
2814 ) bookmarks;
2815 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2816 end;
2820 let x =
2821 match state.birdseye with
2822 | Some (_, x, _, _) -> x
2823 | None -> state.x
2825 adddoc state.path x (getanchor ()) conf
2826 (if conf.savebmarks then state.bookmarks else []);
2828 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2829 if path <> state.path
2830 then
2831 adddoc path x y c bookmarks
2832 ) h;
2833 Buffer.add_string bb "</llppconfig>";
2835 load1 f;
2836 if Buffer.length bb > 0
2837 then
2839 let tmp = path ^ ".tmp" in
2840 let oc = open_out_bin tmp in
2841 Buffer.output_buffer oc bb;
2842 close_out oc;
2843 Sys.rename tmp path;
2844 with exn ->
2845 prerr_endline
2846 ("error while saving configuration: " ^ Printexc.to_string exn)
2848 end;;
2850 let () =
2851 Arg.parse
2852 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2853 (fun s -> state.path <- s)
2854 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2856 let path =
2857 if String.length state.path = 0
2858 then (prerr_endline "filename missing"; exit 1)
2859 else (
2860 if Filename.is_relative state.path
2861 then Filename.concat (Sys.getcwd ()) state.path
2862 else state.path
2865 state.path <- path;
2867 State.load ();
2869 let _ = Glut.init Sys.argv in
2870 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2871 let () = Glut.initWindowSize conf.winw conf.winh in
2872 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2874 let csock, ssock =
2875 if Sys.os_type = "Unix"
2876 then
2877 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2878 else
2879 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2880 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2881 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2882 Unix.bind sock addr;
2883 Unix.listen sock 1;
2884 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2885 Unix.connect csock addr;
2886 let ssock, _ = Unix.accept sock in
2887 Unix.close sock;
2888 let opts sock =
2889 Unix.setsockopt sock Unix.TCP_NODELAY true;
2890 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2892 opts ssock;
2893 opts csock;
2894 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2895 ssock, csock
2898 let () = Glut.displayFunc display in
2899 let () = Glut.reshapeFunc reshape in
2900 let () = Glut.keyboardFunc keyboard in
2901 let () = Glut.specialFunc special in
2902 let () = Glut.idleFunc (Some idle) in
2903 let () = Glut.mouseFunc mouse in
2904 let () = Glut.motionFunc motion in
2905 let () = Glut.passiveMotionFunc pmotion in
2907 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2908 state.csock <- csock;
2909 state.ssock <- ssock;
2910 state.text <- "Opening " ^ path;
2911 writeopen state.path state.password;
2913 at_exit State.save;
2915 let rec handlelablglutbug () =
2917 Glut.mainLoop ();
2918 with Glut.BadEnum "key in special_of_int" ->
2919 showtext '!' " LablGlut bug: special key not recognized";
2920 handlelablglutbug ()
2922 handlelablglutbug ();