Allow zooming with the mouse wheel
[llpp.git] / main.ml
blobd76a9ed9c326e446586da2895f75c3e6481bc5a5
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 | Mzoom of (int * int)
42 | Mnone
45 type textentry = (char * string * onhist * onkey * ondone)
46 and onkey = string -> int -> te
47 and ondone = string -> unit
48 and histcancel = unit -> unit
49 and onhist = ((histcmd -> string) * histcancel) option
50 and histcmd = HCnext | HCprev | HCfirst | HClast
51 and te =
52 | TEstop
53 | TEdone of string
54 | TEcont of string
55 | TEswitch of textentry
58 type 'a circbuf =
59 { store : 'a array
60 ; mutable rc : int
61 ; mutable wc : int
62 ; mutable len : int
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 scrollstep : int
139 ; mutable maxhfit : bool
140 ; mutable crophack : bool
141 ; mutable autoscrollstep : int
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 mode =
174 | Birdseye of (conf * leftx * pageno * pageno * anchor)
175 | Outline of (bool * int * int * outline array * string)
176 | Textentry of (textentry * mode)
177 | View
180 let isbirdseye = function Birdseye _ -> true | _ -> false;;
181 let istextentry = function Textentry _ -> true | _ -> false;;
183 type state =
184 { mutable csock : Unix.file_descr
185 ; mutable ssock : Unix.file_descr
186 ; mutable w : int
187 ; mutable x : int
188 ; mutable y : int
189 ; mutable anchor : anchor
190 ; mutable maxy : int
191 ; mutable layout : layout list
192 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
193 ; mutable pdims : (pageno * width * height * leftx) list
194 ; mutable pagecount : int
195 ; pagecache : string circbuf
196 ; mutable rendering : bool
197 ; mutable mstate : mstate
198 ; mutable searchpattern : string
199 ; mutable rects : (pageno * recttype * rect) list
200 ; mutable rects1 : (pageno * recttype * rect) list
201 ; mutable text : string
202 ; mutable fullscreen : (width * height) option
203 ; mutable mode : mode
204 ; mutable outlines : outlines
205 ; mutable bookmarks : outline list
206 ; mutable path : string
207 ; mutable password : string
208 ; mutable invalidated : int
209 ; mutable colorscale : float
210 ; mutable memused : int
211 ; mutable gen : gen
212 ; mutable throttle : layout list option
213 ; mutable ascrollstep : int
214 ; hists : hists
216 and hists =
217 { pat : string circbuf
218 ; pag : string circbuf
219 ; nav : anchor circbuf
223 let defconf =
224 { scrollw = 7
225 ; scrollh = 12
226 ; icase = true
227 ; preload = true
228 ; pagebias = 0
229 ; verbose = false
230 ; scrollstep = 24
231 ; maxhfit = true
232 ; crophack = false
233 ; autoscrollstep = 24
234 ; showall = false
235 ; hlinks = false
236 ; underinfo = false
237 ; interpagespace = 2
238 ; zoom = 1.0
239 ; presentation = false
240 ; angle = 0
241 ; winw = 900
242 ; winh = 900
243 ; savebmarks = true
244 ; proportional = true
245 ; memlimit = 32*1024*1024
246 ; texcount = 256
247 ; sliceheight = 24
248 ; thumbw = 76
252 let conf = { defconf with angle = defconf.angle };;
254 let state =
255 { csock = Unix.stdin
256 ; ssock = Unix.stdin
257 ; x = 0
258 ; y = 0
259 ; anchor = (0, 0.0)
260 ; w = 0
261 ; layout = []
262 ; maxy = max_int
263 ; pagemap = Hashtbl.create 10
264 ; pagecache = cbnew 100 ""
265 ; pdims = []
266 ; pagecount = 0
267 ; rendering = false
268 ; mstate = Mnone
269 ; rects = []
270 ; rects1 = []
271 ; text = ""
272 ; mode = View
273 ; fullscreen = None
274 ; searchpattern = ""
275 ; outlines = Olist []
276 ; bookmarks = []
277 ; path = ""
278 ; password = ""
279 ; invalidated = 0
280 ; hists =
281 { nav = cbnew 100 (0, 0.0)
282 ; pat = cbnew 20 ""
283 ; pag = cbnew 10 ""
285 ; colorscale = 1.0
286 ; memused = 0
287 ; gen = 0
288 ; throttle = None
289 ; ascrollstep = 0
293 let vlog fmt =
294 if conf.verbose
295 then
296 Printf.kprintf prerr_endline fmt
297 else
298 Printf.kprintf ignore fmt
301 let writecmd fd s =
302 let len = String.length s in
303 let n = 4 + len in
304 let b = Buffer.create n in
305 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
306 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
307 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
308 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
309 Buffer.add_string b s;
310 let s' = Buffer.contents b in
311 let n' = Unix.write fd s' 0 n in
312 if n' != n then failwith "write failed";
315 let readcmd fd =
316 let s = "xxxx" in
317 let n = Unix.read fd s 0 4 in
318 if n != 4 then failwith "incomplete read(len)";
319 let len = 0
320 lor (Char.code s.[0] lsl 24)
321 lor (Char.code s.[1] lsl 16)
322 lor (Char.code s.[2] lsl 8)
323 lor (Char.code s.[3] lsl 0)
325 let s = String.create len in
326 let n = Unix.read fd s 0 len in
327 if n != len then failwith "incomplete read(data)";
331 let makecmd s l =
332 let b = Buffer.create 10 in
333 Buffer.add_string b s;
334 let rec combine = function
335 | [] -> b
336 | x :: xs ->
337 Buffer.add_char b ' ';
338 let s =
339 match x with
340 | `b b -> if b then "1" else "0"
341 | `s s -> s
342 | `i i -> string_of_int i
343 | `f f -> string_of_float f
344 | `I f -> string_of_int (truncate f)
346 Buffer.add_string b s;
347 combine xs;
349 combine l;
352 let wcmd s l =
353 let cmd = Buffer.contents (makecmd s l) in
354 writecmd state.csock cmd;
357 let calcips h =
358 if conf.presentation
359 then
360 let d = conf.winh - h in
361 max 0 ((d + 1) / 2)
362 else
363 conf.interpagespace
366 let calcheight () =
367 let rec f pn ph pi fh l =
368 match l with
369 | (n, _, h, _) :: rest ->
370 let ips = calcips h in
371 let fh =
372 if conf.presentation
373 then fh+ips
374 else (
375 if isbirdseye state.mode && pn = 0
376 then fh + ips
377 else fh
380 let fh = fh + ((n - pn) * (ph + pi)) in
381 f n h ips fh rest;
383 | [] ->
384 let inc =
385 if conf.presentation || (isbirdseye state.mode && pn = 0)
386 then 0
387 else -pi
389 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
390 max 0 fh
392 let fh = f 0 0 0 0 state.pdims in
396 let getpageyh pageno =
397 let rec f pn ph pi y l =
398 match l with
399 | (n, _, h, _) :: rest ->
400 let ips = calcips h in
401 if n >= pageno
402 then
403 let h = if n = pageno then h else ph in
404 if conf.presentation && n = pageno
405 then
406 y + (pageno - pn) * (ph + pi) + pi, h
407 else
408 y + (pageno - pn) * (ph + pi), h
409 else
410 let y = y + (if conf.presentation then pi else 0) in
411 let y = y + (n - pn) * (ph + pi) in
412 f n h ips y rest
414 | [] ->
415 y + (pageno - pn) * (ph + pi), ph
417 f 0 0 0 0 state.pdims
420 let getpagey pageno = fst (getpageyh pageno);;
422 let layout y sh =
423 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
424 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
425 match pdims with
426 | (pageno', w, h, x) :: rest when pageno' = pageno ->
427 let ips = calcips h in
428 let yinc =
429 if conf.presentation || (isbirdseye state.mode && pageno = 0)
430 then ips
431 else 0
433 (w, h, ips, x), rest, pdimno + 1, yinc
434 | _ ->
435 prev, pdims, pdimno, 0
437 let dy = dy + yinc in
438 let py = py + yinc in
439 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
440 then
441 accu
442 else
443 let vy = y + dy in
444 if py + h <= vy - yinc
445 then
446 let py = py + h + ips in
447 let dy = max 0 (py - y) in
448 f ~pageno:(pageno+1)
449 ~pdimno
450 ~prev:curr
453 ~pdims:rest
454 ~cacheleft
455 ~accu
456 else
457 let pagey = vy - py in
458 let pagevh = h - pagey in
459 let pagevh = min (sh - dy) pagevh in
460 let off = if yinc > 0 then py - vy else 0 in
461 let py = py + h + ips in
462 let e =
463 { pageno = pageno
464 ; pagedimno = pdimno
465 ; pagew = w
466 ; pageh = h
467 ; pagedispy = dy + off
468 ; pagey = pagey + off
469 ; pagevh = pagevh - off
470 ; pagex = x
473 let accu = e :: accu in
474 f ~pageno:(pageno+1)
475 ~pdimno
476 ~prev:curr
478 ~dy:(dy+pagevh+ips)
479 ~pdims:rest
480 ~cacheleft:(cacheleft-1)
481 ~accu
483 if state.invalidated = 0
484 then (
485 let accu =
487 ~pageno:0
488 ~pdimno:~-1
489 ~prev:(0,0,0,0)
490 ~py:0
491 ~dy:0
492 ~pdims:state.pdims
493 ~cacheleft:(cbcap state.pagecache)
494 ~accu:[]
496 List.rev accu
498 else
502 let clamp incr =
503 let y = state.y + incr in
504 let y = max 0 y in
505 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
509 let getopaque pageno =
510 try Some (Hashtbl.find state.pagemap
511 (pageno, state.w, conf.angle, conf.proportional, state.gen))
512 with Not_found -> None
515 let cache pageno opaque =
516 Hashtbl.replace state.pagemap
517 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
520 let validopaque opaque = String.length opaque > 0;;
522 let render l =
523 match getopaque l.pageno with
524 | None when not state.rendering ->
525 state.rendering <- true;
526 cache l.pageno ("", -1);
527 wcmd "render" [`i (l.pageno + 1)
528 ;`i l.pagedimno
529 ;`i l.pagew
530 ;`i l.pageh];
531 | _ -> ()
534 let loadlayout layout =
535 let rec f all = function
536 | l :: ls ->
537 begin match getopaque l.pageno with
538 | None -> render l; f false ls
539 | Some (opaque, _) -> f (all && validopaque opaque) ls
541 | [] -> all
543 f (layout <> []) layout;
546 let findpageforopaque opaque =
547 Hashtbl.fold
548 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
549 state.pagemap None
552 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
554 let preload () =
555 let oktopreload =
556 if conf.preload
557 then
558 let memleft = conf.memlimit - state.memused in
559 if memleft < 0
560 then
561 let opaque = cbpeek state.pagecache in
562 match findpageforopaque opaque with
563 | Some ((n, _, _, _, _), size) ->
564 memleft + size >= 0 && not (pagevisible state.layout n)
565 | None -> false
566 else true
567 else false
569 if oktopreload
570 then
571 let presentation = conf.presentation in
572 let interpagespace = conf.interpagespace in
573 let maxy = state.maxy in
574 conf.presentation <- false;
575 conf.interpagespace <- 0;
576 state.maxy <- calcheight ();
577 let y =
578 match state.layout with
579 | [] -> 0
580 | l :: _ -> getpagey l.pageno + l.pagey
582 let y = if y < conf.winh then 0 else y - conf.winh in
583 let pages = layout y (conf.winh*3) in
584 List.iter render pages;
585 conf.presentation <- presentation;
586 conf.interpagespace <- interpagespace;
587 state.maxy <- maxy;
590 let gotoy y =
591 let y = max 0 y in
592 let y = min state.maxy y in
593 let pages = layout y conf.winh in
594 let ready = loadlayout pages in
595 if conf.showall
596 then (
597 if ready
598 then (
599 state.y <- y;
600 state.layout <- pages;
601 state.throttle <- None;
602 Glut.postRedisplay ();
604 else (
605 state.throttle <- Some pages;
608 else (
609 state.y <- y;
610 state.layout <- pages;
611 state.throttle <- None;
612 Glut.postRedisplay ();
614 begin match state.mode with
615 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
616 if not (pagevisible pages pageno)
617 then (
618 match state.layout with
619 | [] -> ()
620 | l :: _ ->
621 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
623 | _ -> ()
624 end;
625 preload ();
628 let gotoy_and_clear_text y =
629 gotoy y;
630 if not conf.verbose then state.text <- "";
633 let emptyanchor = (0, 0.0);;
635 let getanchor () =
636 match state.layout with
637 | [] -> emptyanchor
638 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
641 let getanchory (n, top) =
642 let y, h = getpageyh n in
643 y + (truncate (top *. float h));
646 let gotoanchor anchor =
647 gotoy (getanchory anchor);
650 let addnav () =
651 cbput state.hists.nav (getanchor ());
654 let getnav () =
655 let anchor = cbgetc state.hists.nav ~-1 in
656 getanchory anchor;
659 let gotopage n top =
660 let y, h = getpageyh n in
661 gotoy_and_clear_text (y + (truncate (top *. float h)));
664 let gotopage1 n top =
665 let y = getpagey n in
666 gotoy_and_clear_text (y + top);
669 let invalidate () =
670 state.layout <- [];
671 state.pdims <- [];
672 state.rects <- [];
673 state.rects1 <- [];
674 state.invalidated <- state.invalidated + 1;
677 let scalecolor c =
678 let c = c *. state.colorscale in
679 (c, c, c);
682 let represent () =
683 state.maxy <- calcheight ();
684 match state.mode with
685 | Birdseye (_, _, pageno, _, _) ->
686 let y, h = getpageyh pageno in
687 let top = (conf.winh - h) / 2 in
688 gotoy (max 0 (y - top))
689 | _ -> gotoanchor state.anchor
692 let pagematrix () =
693 GlMat.mode `projection;
694 GlMat.load_identity ();
695 GlMat.rotate ~x:1.0 ~angle:180.0 ();
696 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
697 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
698 if state.x != 0
699 then (
700 GlMat.translate ~x:(float state.x) ();
704 let winmatrix () =
705 GlMat.mode `projection;
706 GlMat.load_identity ();
707 GlMat.rotate ~x:1.0 ~angle:180.0 ();
708 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
709 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
712 let reshape ~w ~h =
713 if state.invalidated = 0
714 then state.anchor <- getanchor ();
716 conf.winw <- w;
717 let w = truncate (float w *. conf.zoom) - conf.scrollw in
718 let w = max w 2 in
719 state.w <- w;
720 conf.winh <- h;
721 GlMat.mode `modelview;
722 GlMat.load_identity ();
723 GlClear.color (scalecolor 1.0);
724 GlClear.clear [`color];
726 invalidate ();
727 wcmd "geometry" [`i w; `i h];
730 let showtext c s =
731 GlDraw.color (0.0, 0.0, 0.0);
732 GlDraw.rect
733 (0.0, float (conf.winh - 18))
734 (float (conf.winw - conf.scrollw - 1), float conf.winh)
736 let font = Glut.BITMAP_8_BY_13 in
737 GlDraw.color (1.0, 1.0, 1.0);
738 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
739 Glut.bitmapCharacter ~font ~c:(Char.code c);
740 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
743 let enttext () =
744 let len = String.length state.text in
745 match state.mode with
746 | Textentry ((c, text, _, _, _), _) ->
747 let s =
748 if len > 0
749 then
750 text ^ " [" ^ state.text ^ "]"
751 else
752 text
754 showtext c s;
756 | _ ->
757 if len > 0 then showtext ' ' state.text
760 let showtext c s =
761 state.text <- Printf.sprintf "%c%s" c s;
762 Glut.postRedisplay ();
765 let act cmd =
766 match cmd.[0] with
767 | 'c' ->
768 state.pdims <- [];
770 | 'D' ->
771 state.rects <- state.rects1;
772 Glut.postRedisplay ()
774 | 'C' ->
775 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
776 state.pagecount <- n;
777 state.invalidated <- state.invalidated - 1;
778 if state.invalidated = 0
779 then represent ()
781 | 't' ->
782 let s = Scanf.sscanf cmd "t %n"
783 (fun n -> String.sub cmd n (String.length cmd - n))
785 Glut.setWindowTitle s
787 | 'T' ->
788 let s = Scanf.sscanf cmd "T %n"
789 (fun n -> String.sub cmd n (String.length cmd - n))
791 if istextentry state.mode
792 then (
793 state.text <- s;
794 showtext ' ' s;
796 else (
797 state.text <- s;
798 Glut.postRedisplay ();
801 | 'V' ->
802 if conf.verbose
803 then
804 let s = Scanf.sscanf cmd "V %n"
805 (fun n -> String.sub cmd n (String.length cmd - n))
807 state.text <- s;
808 showtext ' ' s;
810 | 'F' ->
811 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
812 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
813 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
814 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
816 let y = (getpagey pageno) + truncate y0 in
817 addnav ();
818 gotoy y;
819 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
821 | 'R' ->
822 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
823 Scanf.sscanf cmd "R %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 state.rects1 <-
828 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
830 | 'r' ->
831 let n, w, h, r, l, s, p =
832 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
833 (fun n w h r l s p ->
834 (n-1, w, h, r, l != 0, s, p))
837 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
838 state.memused <- state.memused + s;
840 let layout =
841 match state.throttle with
842 | None -> state.layout
843 | Some layout -> layout
846 let rec gc () =
847 if (state.memused <= conf.memlimit) || cbempty state.pagecache
848 then ()
849 else (
850 let evictedopaque = cbpeek state.pagecache in
851 match findpageforopaque evictedopaque with
852 | None -> failwith "bug in gc"
853 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
854 if state.gen != gen || not (pagevisible layout evictedn)
855 then (
856 wcmd "free" [`s evictedopaque];
857 state.memused <- state.memused - evictedsize;
858 Hashtbl.remove state.pagemap k;
859 cbdecr state.pagecache;
860 gc ();
864 gc ();
866 cbput state.pagecache p;
867 state.rendering <- false;
869 begin match state.throttle with
870 | None ->
871 if pagevisible state.layout n
872 then gotoy state.y
873 else (
874 let allvisible = loadlayout state.layout in
875 if allvisible then preload ();
878 | Some layout ->
879 match layout with
880 | [] -> ()
881 | l :: _ ->
882 let y = getpagey l.pageno + l.pagey in
883 gotoy y
886 | 'l' ->
887 let (n, w, h, x) as pdim =
888 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
890 state.pdims <- pdim :: state.pdims
892 | 'o' ->
893 let (l, n, t, h, pos) =
894 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
896 let s = String.sub cmd pos (String.length cmd - pos) in
897 let s =
898 let l = String.length s in
899 let b = Buffer.create (String.length s) in
900 let rec loop pc2 i =
901 if i = l
902 then ()
903 else
904 let pc2 =
905 match s.[i] with
906 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
907 | '\xc2' -> true
908 | c ->
909 let c = if Char.code c land 0x80 = 0 then c else '?' in
910 Buffer.add_char b c;
911 false
913 loop pc2 (i+1)
915 loop false 0;
916 Buffer.contents b
918 let outline = (s, l, n, float t /. float h) in
919 let outlines =
920 match state.outlines with
921 | Olist outlines -> Olist (outline :: outlines)
922 | Oarray _ -> Olist [outline]
923 | Onarrow _ -> Olist [outline]
925 state.outlines <- outlines
927 | _ ->
928 dolog "unknown cmd `%S'" cmd
931 let now = Unix.gettimeofday;;
933 let idle () =
934 let rec loop delay =
935 let r, _, _ = Unix.select [state.csock] [] [] delay in
936 begin match r with
937 | [] ->
938 if state.ascrollstep > 0
939 then begin
940 let y = state.y + state.ascrollstep in
941 let y = if y >= state.maxy then 0 else y in
942 gotoy y;
943 state.text <- "";
944 end;
946 | _ ->
947 let cmd = readcmd state.csock in
948 act cmd;
949 loop 0.0
950 end;
951 in loop 0.001
954 let onhist cb =
955 let rc = cb.rc in
956 let action = function
957 | HCprev -> cbget cb ~-1
958 | HCnext -> cbget cb 1
959 | HCfirst -> cbget cb ~-(cb.rc)
960 | HClast -> cbget cb (cb.len - 1 - cb.rc)
961 and cancel () = cb.rc <- rc
962 in (action, cancel)
965 let search pattern forward =
966 if String.length pattern > 0
967 then
968 let pn, py =
969 match state.layout with
970 | [] -> 0, 0
971 | l :: _ ->
972 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
974 let cmd =
975 let b = makecmd "search"
976 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
978 Buffer.add_char b ',';
979 Buffer.add_string b pattern;
980 Buffer.add_char b '\000';
981 Buffer.contents b;
983 writecmd state.csock cmd;
986 let intentry text key =
987 let c = Char.unsafe_chr key in
988 match c with
989 | '0' .. '9' ->
990 let s = "x" in s.[0] <- c;
991 let text = text ^ s in
992 TEcont text
994 | _ ->
995 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
996 TEcont text
999 let addchar s c =
1000 let b = Buffer.create (String.length s + 1) in
1001 Buffer.add_string b s;
1002 Buffer.add_char b c;
1003 Buffer.contents b;
1006 let textentry text key =
1007 let c = Char.unsafe_chr key in
1008 match c with
1009 | _ when key >= 32 && key < 127 ->
1010 let text = addchar text c in
1011 TEcont text
1013 | _ ->
1014 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1015 TEcont text
1018 let reinit angle proportional =
1019 conf.angle <- angle;
1020 conf.proportional <- proportional;
1021 invalidate ();
1022 wcmd "reinit" [`i angle; `b proportional];
1025 let setzoom zoom =
1026 let zoom = max 0.01 (min 2.2 zoom) in
1027 if zoom <> conf.zoom
1028 then (
1029 if zoom <= 1.0
1030 then state.x <- 0;
1031 conf.zoom <- zoom;
1032 reshape conf.winw conf.winh;
1033 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1034 state.text <- Printf.sprintf "zoom is now %f" (zoom *. 100.0);
1038 let optentry text key =
1039 let btos b = if b then "on" else "off" in
1040 let c = Char.unsafe_chr key in
1041 match c with
1042 | 's' ->
1043 let ondone s =
1044 try conf.scrollstep <- int_of_string s with exc ->
1045 state.text <- Printf.sprintf "bad integer `%s': %s"
1046 s (Printexc.to_string exc)
1048 TEswitch ('#', "", None, intentry, ondone)
1050 | 'A' ->
1051 let ondone s =
1053 conf.autoscrollstep <- int_of_string s;
1054 if state.ascrollstep > 0
1055 then state.ascrollstep <- conf.autoscrollstep;
1056 with exc ->
1057 state.text <- Printf.sprintf "bad integer `%s': %s"
1058 s (Printexc.to_string exc)
1060 TEswitch ('*', "", None, intentry, ondone)
1062 | 'Z' ->
1063 let ondone s =
1065 let zoom = float (int_of_string s) /. 100.0 in
1066 setzoom zoom
1067 with exc ->
1068 state.text <- Printf.sprintf "bad integer `%s': %s"
1069 s (Printexc.to_string exc)
1071 TEswitch ('@', "", None, intentry, ondone)
1073 | 'R' ->
1074 let ondone s =
1075 match try
1076 Some (int_of_string s)
1077 with exc ->
1078 state.text <- Printf.sprintf "bad integer `%s': %s"
1079 s (Printexc.to_string exc);
1080 None
1081 with
1082 | Some angle -> reinit angle conf.proportional
1083 | None -> ()
1085 TEswitch ('^', "", None, intentry, ondone)
1087 | 'i' ->
1088 conf.icase <- not conf.icase;
1089 TEdone ("case insensitive search " ^ (btos conf.icase))
1091 | 'p' ->
1092 conf.preload <- not conf.preload;
1093 gotoy state.y;
1094 TEdone ("preload " ^ (btos conf.preload))
1096 | 'v' ->
1097 conf.verbose <- not conf.verbose;
1098 TEdone ("verbose " ^ (btos conf.verbose))
1100 | 'h' ->
1101 conf.maxhfit <- not conf.maxhfit;
1102 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1103 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1105 | 'c' ->
1106 conf.crophack <- not conf.crophack;
1107 TEdone ("crophack " ^ btos conf.crophack)
1109 | 'a' ->
1110 conf.showall <- not conf.showall;
1111 TEdone ("showall " ^ btos conf.showall)
1113 | 'f' ->
1114 conf.underinfo <- not conf.underinfo;
1115 TEdone ("underinfo " ^ btos conf.underinfo)
1117 | 'P' ->
1118 conf.savebmarks <- not conf.savebmarks;
1119 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1121 | 'S' ->
1122 let ondone s =
1124 let pageno, py =
1125 match state.layout with
1126 | [] -> 0, 0
1127 | l :: _ ->
1128 l.pageno, l.pagey
1130 conf.interpagespace <- int_of_string s;
1131 state.maxy <- calcheight ();
1132 let y = getpagey pageno in
1133 gotoy (y + py)
1134 with exc ->
1135 state.text <- Printf.sprintf "bad integer `%s': %s"
1136 s (Printexc.to_string exc)
1138 TEswitch ('%', "", None, intentry, ondone)
1140 | 'l' ->
1141 reinit conf.angle (not conf.proportional);
1142 TEdone ("proprortional display " ^ btos conf.proportional)
1144 | _ ->
1145 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1146 TEstop
1149 let maxoutlinerows () = (conf.winh - 31) / 16;;
1151 let enterselector allowdel outlines errmsg msg =
1152 if Array.length outlines = 0
1153 then (
1154 showtext ' ' errmsg;
1156 else (
1157 state.text <- msg;
1158 Glut.setCursor Glut.CURSOR_INHERIT;
1159 let pageno =
1160 match state.layout with
1161 | [] -> -1
1162 | {pageno=pageno} :: rest -> pageno
1164 let active =
1165 let rec loop n =
1166 if n = Array.length outlines
1167 then 0
1168 else
1169 let (_, _, outlinepageno, _) = outlines.(n) in
1170 if outlinepageno >= pageno then n else loop (n+1)
1172 loop 0
1174 state.mode <- Outline
1175 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "");
1176 Glut.postRedisplay ();
1180 let enteroutlinemode () =
1181 let outlines, msg =
1182 match state.outlines with
1183 | Oarray a -> a, ""
1184 | Olist l ->
1185 let a = Array.of_list (List.rev l) in
1186 state.outlines <- Oarray a;
1187 a, ""
1188 | Onarrow (pat, a, b) ->
1189 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1191 enterselector false outlines "Document has no outline" msg;
1194 let enterbookmarkmode () =
1195 let bookmarks = Array.of_list state.bookmarks in
1196 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1199 let quickbookmark ?title () =
1200 match state.layout with
1201 | [] -> ()
1202 | l :: _ ->
1203 let title =
1204 match title with
1205 | None ->
1206 let sec = Unix.gettimeofday () in
1207 let tm = Unix.localtime sec in
1208 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1209 (l.pageno+1)
1210 tm.Unix.tm_mday
1211 tm.Unix.tm_mon
1212 (tm.Unix.tm_year + 1900)
1213 tm.Unix.tm_hour
1214 tm.Unix.tm_min
1215 | Some title -> title
1217 state.bookmarks <-
1218 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1221 let doreshape w h =
1222 state.fullscreen <- None;
1223 Glut.reshapeWindow w h;
1226 let writeopen path password =
1227 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1230 let opendoc path password =
1231 invalidate ();
1232 state.path <- path;
1233 state.password <- password;
1234 state.gen <- state.gen + 1;
1236 writeopen path password;
1237 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1238 wcmd "geometry" [`i state.w; `i conf.winh];
1241 let birdseyeon () =
1242 let zoom = float conf.thumbw /. float conf.winw in
1243 let birdseyepageno =
1244 let rec fold = function
1245 | [] -> 0
1246 | l :: _ when l.pagey = 0 -> l.pageno
1247 | _ :: rest -> fold rest
1249 fold state.layout
1251 state.mode <- Birdseye (
1252 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1254 conf.zoom <- zoom;
1255 conf.presentation <- false;
1256 conf.interpagespace <- 10;
1257 conf.hlinks <- false;
1258 state.x <- 0;
1259 state.mstate <- Mnone;
1260 conf.showall <- false;
1261 Glut.setCursor Glut.CURSOR_INHERIT;
1262 if conf.verbose
1263 then
1264 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1265 (100.0*.zoom)
1266 else
1267 state.text <- ""
1269 reshape conf.winw conf.winh;
1272 let birdseyeoff (c, leftx, pageno, _, anchor) goback =
1273 state.mode <- View;
1274 conf.zoom <- c.zoom;
1275 conf.presentation <- c.presentation;
1276 conf.interpagespace <- c.interpagespace;
1277 conf.showall <- c.showall;
1278 conf.hlinks <- c.hlinks;
1279 state.x <- leftx;
1280 if conf.verbose
1281 then
1282 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1283 (100.0*.conf.zoom)
1285 reshape conf.winw conf.winh;
1286 state.anchor <- if goback then anchor else (pageno, 0.0);
1289 let togglebirdseye () =
1290 match state.mode with
1291 | Birdseye vals -> birdseyeoff vals true
1292 | View | Outline _ -> birdseyeon ()
1293 | _ -> ()
1296 let viewkeyboard ~key ~x ~y =
1297 let enttext te =
1298 state.mode <- Textentry (te, state.mode);
1299 state.text <- "";
1300 enttext ();
1301 Glut.postRedisplay ()
1303 let c = Char.chr key in
1304 match c with
1305 | '\027' | 'q' ->
1306 exit 0
1308 | '\008' ->
1309 let y = getnav () in
1310 gotoy_and_clear_text y
1312 | 'o' ->
1313 enteroutlinemode ()
1315 | 'u' ->
1316 state.rects <- [];
1317 state.text <- "";
1318 Glut.postRedisplay ()
1320 | '/' | '?' ->
1321 let ondone isforw s =
1322 cbput state.hists.pat s;
1323 state.searchpattern <- s;
1324 search s isforw
1326 enttext (c, "", Some (onhist state.hists.pat),
1327 textentry, ondone (c ='/'))
1329 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1330 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1331 setzoom (min 2.2 (conf.zoom +. incr))
1333 | '+' ->
1334 let ondone s =
1335 let n =
1336 try int_of_string s with exc ->
1337 state.text <- Printf.sprintf "bad integer `%s': %s"
1338 s (Printexc.to_string exc);
1339 max_int
1341 if n != max_int
1342 then (
1343 conf.pagebias <- n;
1344 state.text <- "page bias is now " ^ string_of_int n;
1347 enttext ('+', "", None, intentry, ondone)
1349 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1350 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1351 setzoom (max 0.01 (conf.zoom -. decr))
1353 | '-' ->
1354 let ondone msg =
1355 state.text <- msg;
1357 enttext ('-', "", None, optentry, ondone)
1359 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1360 setzoom 1.0
1362 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1363 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1364 if zoom < 1.0
1365 then setzoom zoom
1367 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1368 togglebirdseye ()
1370 | '0' .. '9' ->
1371 let ondone s =
1372 let n =
1373 try int_of_string s with exc ->
1374 state.text <- Printf.sprintf "bad integer `%s': %s"
1375 s (Printexc.to_string exc);
1378 if n >= 0
1379 then (
1380 addnav ();
1381 cbput state.hists.pag (string_of_int n);
1382 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1385 let pageentry text key =
1386 match Char.unsafe_chr key with
1387 | 'g' -> TEdone text
1388 | _ -> intentry text key
1390 let text = "x" in text.[0] <- c;
1391 enttext (':', text, Some (onhist state.hists.pag), pageentry, ondone)
1393 | 'b' ->
1394 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1395 reshape conf.winw conf.winh;
1397 | 'l' ->
1398 conf.hlinks <- not conf.hlinks;
1399 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1400 Glut.postRedisplay ()
1402 | 'a' ->
1403 if state.ascrollstep = 0
1404 then state.ascrollstep <- conf.autoscrollstep
1405 else (
1406 conf.autoscrollstep <- state.ascrollstep;
1407 state.ascrollstep <- 0;
1410 | 'P' ->
1411 conf.presentation <- not conf.presentation;
1412 showtext ' ' ("presentation mode " ^
1413 if conf.presentation then "on" else "off");
1414 represent ()
1416 | 'f' ->
1417 begin match state.fullscreen with
1418 | None ->
1419 state.fullscreen <- Some (conf.winw, conf.winh);
1420 Glut.fullScreen ()
1421 | Some (w, h) ->
1422 state.fullscreen <- None;
1423 doreshape w h
1426 | 'g' ->
1427 gotoy_and_clear_text 0
1429 | 'n' ->
1430 search state.searchpattern true
1432 | 'p' | 'N' ->
1433 search state.searchpattern false
1435 | 't' ->
1436 begin match state.layout with
1437 | [] -> ()
1438 | l :: _ ->
1439 gotoy_and_clear_text (getpagey l.pageno)
1442 | ' ' ->
1443 begin match List.rev state.layout with
1444 | [] -> ()
1445 | l :: _ ->
1446 let pageno = min (l.pageno+1) (state.pagecount-1) in
1447 gotoy_and_clear_text (getpagey pageno)
1450 | '\127' ->
1451 begin match state.layout with
1452 | [] -> ()
1453 | l :: _ ->
1454 let pageno = max 0 (l.pageno-1) in
1455 gotoy_and_clear_text (getpagey pageno)
1458 | '=' ->
1459 let f (fn, ln) l =
1460 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1462 let fn, ln = List.fold_left f (-1, -1) state.layout in
1463 let s =
1464 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1465 let percent =
1466 if maxy <= 0
1467 then 100.
1468 else (100. *. (float state.y /. float maxy)) in
1469 if fn = ln
1470 then
1471 Printf.sprintf "Page %d of %d %.2f%%"
1472 (fn+1) state.pagecount percent
1473 else
1474 Printf.sprintf
1475 "Pages %d-%d of %d %.2f%%"
1476 (fn+1) (ln+1) state.pagecount percent
1478 showtext ' ' s;
1480 | 'w' ->
1481 begin match state.layout with
1482 | [] -> ()
1483 | l :: _ ->
1484 doreshape (l.pagew + conf.scrollw) l.pageh;
1485 Glut.postRedisplay ();
1488 | '\'' ->
1489 enterbookmarkmode ()
1491 | 'm' ->
1492 let ondone s =
1493 match state.layout with
1494 | l :: _ ->
1495 state.bookmarks <-
1496 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1497 :: state.bookmarks
1498 | _ -> ()
1500 enttext ('~', "", None, textentry, ondone)
1502 | '~' ->
1503 quickbookmark ();
1504 showtext ' ' "Quick bookmark added";
1506 | 'z' ->
1507 begin match state.layout with
1508 | l :: _ ->
1509 let rect = getpdimrect l.pagedimno in
1510 let w, h =
1511 if conf.crophack
1512 then
1513 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1514 truncate (1.2 *. (rect.(3) -. rect.(0))))
1515 else
1516 (truncate (rect.(1) -. rect.(0)),
1517 truncate (rect.(3) -. rect.(0)))
1519 if w != 0 && h != 0
1520 then
1521 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1523 Glut.postRedisplay ();
1525 | [] -> ()
1528 | '<' | '>' ->
1529 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1531 | '[' | ']' ->
1532 state.colorscale <-
1533 max 0.0
1534 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1535 Glut.postRedisplay ()
1537 | 'k' -> gotoy (clamp (-conf.scrollstep))
1538 | 'j' -> gotoy (clamp conf.scrollstep)
1540 | 'r' -> opendoc state.path state.password
1542 | _ ->
1543 vlog "huh? %d %c" key (Char.chr key);
1546 let textentrykeyboard ~key ~x ~y ((c, text, opthist, onkey, ondone), mode) =
1547 let enttext te =
1548 state.mode <- Textentry (te, mode);
1549 state.text <- "";
1550 enttext ();
1551 Glut.postRedisplay ()
1553 match Char.unsafe_chr key with
1554 | '\008' ->
1555 let len = String.length text in
1556 if len = 0
1557 then (
1558 state.mode <- mode;
1559 Glut.postRedisplay ();
1561 else (
1562 let s = String.sub text 0 (len - 1) in
1563 enttext (c, s, opthist, onkey, ondone)
1566 | '\r' | '\n' ->
1567 ondone text;
1568 state.mode <- mode;
1569 Glut.postRedisplay ()
1571 | '\027' ->
1572 begin match opthist with
1573 | None -> ()
1574 | Some (_, onhistcancel) -> onhistcancel ()
1575 end;
1576 state.mode <- View;
1577 Glut.postRedisplay ()
1579 | _ ->
1580 begin match onkey text key with
1581 | TEdone text ->
1582 state.mode <- mode;
1583 ondone text;
1584 Glut.postRedisplay ()
1586 | TEcont text ->
1587 enttext (c, text, opthist, onkey, ondone);
1589 | TEstop ->
1590 state.mode <- mode;
1591 Glut.postRedisplay ()
1593 | TEswitch te ->
1594 state.mode <- Textentry (te, mode);
1595 Glut.postRedisplay ()
1596 end;
1599 let birdseyekeyboard ~key ~x ~y ((_, _, pageno, _, anchor) as beye) =
1600 match key with
1601 | 27 ->
1602 birdseyeoff beye true
1604 | 12 ->
1605 let y, h = getpageyh pageno in
1606 let top = (conf.winh - h) / 2 in
1607 gotoy (max 0 (y - top))
1609 | 13 ->
1610 birdseyeoff beye false
1612 | _ ->
1613 viewkeyboard ~key ~x ~y
1616 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1617 let narrow outlines pattern =
1618 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1619 match reopt with
1620 | None -> None
1621 | Some re ->
1622 let rec fold accu n =
1623 if n = -1
1624 then accu
1625 else
1626 let (s, _, _, _) as o = outlines.(n) in
1627 let accu =
1628 if (try ignore (Str.search_forward re s 0); true
1629 with Not_found -> false)
1630 then (o :: accu)
1631 else accu
1633 fold accu (n-1)
1635 let matched = fold [] (Array.length outlines - 1) in
1636 if matched = [] then None else Some (Array.of_list matched)
1638 let search active pattern incr =
1639 let dosearch re =
1640 let rec loop n =
1641 if n = Array.length outlines || n = -1
1642 then None
1643 else
1644 let (s, _, _, _) = outlines.(n) in
1646 (try ignore (Str.search_forward re s 0); true
1647 with Not_found -> false)
1648 then Some n
1649 else loop (n + incr)
1651 loop active
1654 let re = Str.regexp_case_fold pattern in
1655 dosearch re
1656 with Failure s ->
1657 state.text <- s;
1658 None
1660 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1661 match key with
1662 | 27 ->
1663 if String.length qsearch = 0
1664 then (
1665 state.text <- "";
1666 state.mode <- View;
1667 Glut.postRedisplay ();
1669 else (
1670 state.text <- "";
1671 state.mode <- Outline (allowdel, active, first, outlines, "");
1672 Glut.postRedisplay ();
1675 | 18 | 19 ->
1676 let incr = if key = 18 then -1 else 1 in
1677 let active, first =
1678 match search (active + incr) qsearch incr with
1679 | None ->
1680 state.text <- qsearch ^ " [not found]";
1681 active, first
1682 | Some active ->
1683 state.text <- qsearch;
1684 active, firstof active
1686 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1687 Glut.postRedisplay ();
1689 | 8 ->
1690 let len = String.length qsearch in
1691 if len = 0
1692 then ()
1693 else (
1694 if len = 1
1695 then (
1696 state.text <- "";
1697 state.mode <- Outline (allowdel, active, first, outlines, "");
1699 else
1700 let qsearch = String.sub qsearch 0 (len - 1) in
1701 let active, first =
1702 match search active qsearch ~-1 with
1703 | None ->
1704 state.text <- qsearch ^ " [not found]";
1705 active, first
1706 | Some active ->
1707 state.text <- qsearch;
1708 active, firstof active
1710 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1712 Glut.postRedisplay ()
1714 | 13 ->
1715 if active < Array.length outlines
1716 then (
1717 let (_, _, n, t) = outlines.(active) in
1718 addnav ();
1719 gotopage n t;
1721 state.text <- "";
1722 if allowdel then state.bookmarks <- Array.to_list outlines;
1723 state.mode <- View;
1724 Glut.postRedisplay ();
1726 | _ when key >= 32 && key < 127 ->
1727 let pattern = addchar qsearch (Char.chr key) in
1728 let active, first =
1729 match search active pattern 1 with
1730 | None ->
1731 state.text <- pattern ^ " [not found]";
1732 active, first
1733 | Some active ->
1734 state.text <- pattern;
1735 active, firstof active
1737 state.mode <- Outline (allowdel, active, first, outlines, pattern);
1738 Glut.postRedisplay ()
1740 | 14 when not allowdel -> (* ctrl-n *)
1741 if String.length qsearch > 0
1742 then (
1743 let optoutlines = narrow outlines qsearch in
1744 begin match optoutlines with
1745 | None -> state.text <- "can't narrow"
1746 | Some outlines ->
1747 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1748 match state.outlines with
1749 | Olist l -> ()
1750 | Oarray a ->
1751 state.outlines <- Onarrow (qsearch, outlines, a)
1752 | Onarrow (pat, a, b) ->
1753 state.outlines <- Onarrow (qsearch, outlines, b)
1754 end;
1756 Glut.postRedisplay ()
1758 | 21 when not allowdel -> (* ctrl-u *)
1759 let outline =
1760 match state.outlines with
1761 | Oarray a -> a
1762 | Olist l ->
1763 let a = Array.of_list (List.rev l) in
1764 state.outlines <- Oarray a;
1766 | Onarrow (pat, a, b) ->
1767 state.outlines <- Oarray b;
1768 state.text <- "";
1771 state.mode <- Outline (allowdel, 0, 0, outline, qsearch);
1772 Glut.postRedisplay ()
1774 | 12 ->
1775 state.mode <- Outline
1776 (allowdel, active, firstof active, outlines, qsearch);
1777 Glut.postRedisplay ()
1779 | 127 when allowdel ->
1780 let len = Array.length outlines - 1 in
1781 if len = 0
1782 then (
1783 state.mode <- View;
1784 state.bookmarks <- [];
1786 else (
1787 let bookmarks = Array.init len
1788 (fun i ->
1789 let i = if i >= active then i + 1 else i in
1790 outlines.(i)
1793 state.mode <-
1794 Outline (
1795 allowdel,
1796 min active (len-1),
1797 min first (len-1),
1798 bookmarks, qsearch
1801 Glut.postRedisplay ()
1803 | _ -> dolog "unknown key %d" key
1806 let keyboard ~key ~x ~y =
1807 if key = 7
1808 then
1809 wcmd "interrupt" []
1810 else
1811 match state.mode with
1812 | Outline outline -> outlinekeyboard ~key ~x ~y outline
1813 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
1814 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
1815 | View -> viewkeyboard ~key ~x ~y
1818 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno, anchor) =
1819 match key with
1820 | Glut.KEY_UP ->
1821 let pageno = max 0 (pageno - 1) in
1822 let rec loop = function
1823 | [] -> gotopage1 pageno 0
1824 | l :: _ when l.pageno = pageno ->
1825 if l.pagedispy >= 0 && l.pagey = 0
1826 then Glut.postRedisplay ()
1827 else gotopage1 pageno 0
1828 | _ :: rest -> loop rest
1830 loop state.layout;
1831 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1833 | Glut.KEY_DOWN ->
1834 let pageno = min (state.pagecount - 1) (pageno + 1) in
1835 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1836 let rec loop = function
1837 | [] ->
1838 let y, h = getpageyh pageno in
1839 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1840 gotoy (clamp dy)
1841 | l :: rest when l.pageno = pageno ->
1842 if l.pagevh != l.pageh
1843 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1844 else Glut.postRedisplay ()
1845 | l :: rest -> loop rest
1847 loop state.layout
1849 | Glut.KEY_PAGE_UP ->
1850 begin match state.layout with
1851 | l :: _ ->
1852 if l.pagey != 0
1853 then (
1854 state.mode <- Birdseye (
1855 conf, leftx, l.pageno, hooverpageno, anchor
1857 gotopage1 l.pageno 0;
1859 else (
1860 let layout = layout (state.y-conf.winh) conf.winh in
1861 match layout with
1862 | [] -> gotoy (clamp (-conf.winh))
1863 | l :: _ ->
1864 state.mode <- Birdseye (
1865 conf, leftx, l.pageno, hooverpageno, anchor
1867 gotopage1 l.pageno 0
1870 | [] -> gotoy (clamp (-conf.winh))
1871 end;
1873 | Glut.KEY_PAGE_DOWN ->
1874 begin match List.rev state.layout with
1875 | l :: _ ->
1876 let layout = layout (state.y + conf.winh) conf.winh in
1877 begin match layout with
1878 | [] ->
1879 let incr = l.pageh - l.pagevh in
1880 if incr = 0
1881 then (
1882 state.mode <-
1883 Birdseye (
1884 conf, leftx, state.pagecount - 1, hooverpageno, anchor
1886 Glut.postRedisplay ();
1888 else gotoy (clamp (incr + conf.interpagespace*2));
1890 | l :: _ ->
1891 state.mode <-
1892 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
1893 gotopage1 l.pageno 0;
1896 | [] -> gotoy (clamp conf.winh)
1897 end;
1899 | Glut.KEY_HOME ->
1900 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
1901 gotopage1 0 0
1903 | Glut.KEY_END ->
1904 let pageno = state.pagecount - 1 in
1905 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1906 if not (pagevisible state.layout pageno)
1907 then
1908 let h =
1909 match List.rev state.pdims with
1910 | [] -> conf.winh
1911 | (_, _, h, _) :: _ -> h
1913 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1914 else Glut.postRedisplay ();
1915 | _ -> ()
1918 let setautoscrollspeed goingdown =
1919 let incr = max 1 (state.ascrollstep / 2) in
1920 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
1921 state.ascrollstep <- astep;
1924 let special ~key ~x ~y =
1925 match state.mode with
1926 | View | (Birdseye _) when key = Glut.KEY_F9 ->
1927 togglebirdseye ()
1929 | Birdseye vals ->
1930 birdseyespecial key x y vals
1932 | View ->
1933 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
1934 then setautoscrollspeed (key = Glut.KEY_DOWN)
1935 else
1936 let y =
1937 match key with
1938 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1939 | Glut.KEY_UP -> clamp (-conf.scrollstep)
1940 | Glut.KEY_DOWN -> clamp conf.scrollstep
1941 | Glut.KEY_PAGE_UP ->
1942 if Glut.getModifiers () land Glut.active_ctrl != 0
1943 then
1944 match state.layout with
1945 | [] -> state.y
1946 | l :: _ -> state.y - l.pagey
1947 else
1948 clamp (-conf.winh)
1949 | Glut.KEY_PAGE_DOWN ->
1950 if Glut.getModifiers () land Glut.active_ctrl != 0
1951 then
1952 match List.rev state.layout with
1953 | [] -> state.y
1954 | l :: _ -> getpagey l.pageno
1955 else
1956 clamp conf.winh
1957 | Glut.KEY_HOME -> addnav (); 0
1958 | Glut.KEY_END ->
1959 addnav ();
1960 state.maxy - (if conf.maxhfit then conf.winh else 0)
1962 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1963 state.x <- state.x - 10;
1964 state.y
1965 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1966 state.x <- state.x + 10;
1967 state.y
1969 | _ -> state.y
1971 gotoy_and_clear_text y
1973 | Textentry
1974 ((c, s, (Some (action, _) as onhist), onkey, ondone), mode) ->
1975 let s =
1976 match key with
1977 | Glut.KEY_UP -> action HCprev
1978 | Glut.KEY_DOWN -> action HCnext
1979 | Glut.KEY_HOME -> action HCfirst
1980 | Glut.KEY_END -> action HClast
1981 | _ -> state.text
1983 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
1984 Glut.postRedisplay ()
1986 | Textentry _ -> ()
1988 | Outline (allowdel, active, first, outlines, qsearch) ->
1989 let maxrows = maxoutlinerows () in
1990 let calcfirst first active =
1991 if active > first
1992 then
1993 let rows = active - first in
1994 if rows > maxrows then active - maxrows else first
1995 else active
1997 let navigate incr =
1998 let active = active + incr in
1999 let active = max 0 (min active (Array.length outlines - 1)) in
2000 let first = calcfirst first active in
2001 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2002 Glut.postRedisplay ()
2004 let updownlevel incr =
2005 let len = Array.length outlines in
2006 let (_, curlevel, _, _) = outlines.(active) in
2007 let rec flow i =
2008 if i = len then i-1 else if i = -1 then 0 else
2009 let (_, l, _, _) = outlines.(i) in
2010 if l != curlevel then i else flow (i+incr)
2012 let active = flow active in
2013 let first = calcfirst first active in
2014 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2015 Glut.postRedisplay ()
2017 match key with
2018 | Glut.KEY_UP -> navigate ~-1
2019 | Glut.KEY_DOWN -> navigate 1
2020 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2021 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2023 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
2024 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
2026 | Glut.KEY_HOME ->
2027 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
2028 Glut.postRedisplay ()
2030 | Glut.KEY_END ->
2031 let active = Array.length outlines - 1 in
2032 let first = max 0 (active - maxrows) in
2033 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2034 Glut.postRedisplay ()
2036 | _ -> ()
2039 let drawplaceholder l =
2040 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
2041 GlDraw.rect
2042 (float l.pagex, float l.pagedispy)
2043 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2045 let x = float (if margin < 0 then -margin else l.pagex)
2046 and y = float (l.pagedispy + 13) in
2047 let font = Glut.BITMAP_8_BY_13 in
2048 GlDraw.color (0.0, 0.0, 0.0);
2049 GlPix.raster_pos ~x ~y ();
2050 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2051 ("Loading " ^ string_of_int (l.pageno + 1));
2054 let now () = Unix.gettimeofday ();;
2056 let drawpage l =
2057 let color =
2058 match state.mode with
2059 | Textentry _ -> scalecolor 0.4
2060 | View | Outline _ -> scalecolor 1.0
2061 | Birdseye (_, _, pageno, hooverpageno, _) ->
2062 if l.pageno = pageno
2063 then scalecolor 1.0
2064 else (
2065 if l.pageno = hooverpageno
2066 then scalecolor 0.9
2067 else scalecolor 0.8
2070 GlDraw.color color;
2071 begin match getopaque l.pageno with
2072 | Some (opaque, _) when validopaque opaque ->
2073 let a = now () in
2074 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2075 opaque;
2076 let b = now () in
2077 let d = b-.a in
2078 vlog "draw %d %f sec" l.pageno d;
2080 | _ ->
2081 drawplaceholder l;
2082 end;
2085 let scrollph y =
2086 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2087 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2088 let sh = float conf.winh /. sh in
2089 let sh = max sh (float conf.scrollh) in
2091 let percent =
2092 if state.y = state.maxy
2093 then 1.0
2094 else float y /. float maxy
2096 let position = (float conf.winh -. sh) *. percent in
2098 let position =
2099 if position +. sh > float conf.winh
2100 then float conf.winh -. sh
2101 else position
2103 position, sh;
2106 let scrollindicator () =
2107 GlDraw.color (0.64 , 0.64, 0.64);
2108 GlDraw.rect
2109 (float (conf.winw - conf.scrollw), 0.)
2110 (float conf.winw, float conf.winh)
2112 GlDraw.color (0.0, 0.0, 0.0);
2114 let position, sh = scrollph state.y in
2115 GlDraw.rect
2116 (float (conf.winw - conf.scrollw), position)
2117 (float conf.winw, position +. sh)
2121 let showsel margin =
2122 match state.mstate with
2123 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2126 | Msel ((x0, y0), (x1, y1)) ->
2127 let rec loop = function
2128 | l :: ls ->
2129 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2130 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2131 then
2132 match getopaque l.pageno with
2133 | Some (opaque, _) when validopaque opaque ->
2134 let oy = -l.pagey + l.pagedispy in
2135 seltext opaque
2136 (x0 - margin - state.x, y0,
2137 x1 - margin - state.x, y1) oy;
2139 | _ -> ()
2140 else loop ls
2141 | [] -> ()
2143 loop state.layout
2146 let showrects () =
2147 let panx = float state.x in
2148 Gl.enable `blend;
2149 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2150 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2151 List.iter
2152 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2153 List.iter (fun l ->
2154 if l.pageno = pageno
2155 then (
2156 let d = float (l.pagedispy - l.pagey) in
2157 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2158 GlDraw.begins `quads;
2160 GlDraw.vertex2 (x0+.panx, y0+.d);
2161 GlDraw.vertex2 (x1+.panx, y1+.d);
2162 GlDraw.vertex2 (x2+.panx, y2+.d);
2163 GlDraw.vertex2 (x3+.panx, y3+.d);
2165 GlDraw.ends ();
2167 ) state.layout
2168 ) state.rects
2170 Gl.disable `blend;
2173 let showoutline () =
2174 match state.mode with
2175 | Outline (allowdel, active, first, outlines, qsearch) ->
2176 Gl.enable `blend;
2177 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2178 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2179 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2180 Gl.disable `blend;
2182 GlDraw.color (1., 1., 1.);
2183 let font = Glut.BITMAP_9_BY_15 in
2184 let draw_string x y s =
2185 GlPix.raster_pos ~x ~y ();
2186 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2188 let rec loop row =
2189 if row = Array.length outlines || (row - first) * 16 > conf.winh
2190 then ()
2191 else (
2192 let (s, l, _, _) = outlines.(row) in
2193 let y = (row - first) * 16 in
2194 let x = 5 + 15*l in
2195 if row = active
2196 then (
2197 Gl.enable `blend;
2198 GlDraw.polygon_mode `both `line;
2199 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2200 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2201 GlDraw.rect (0., float (y + 1))
2202 (float (conf.winw - 1), float (y + 18));
2203 GlDraw.polygon_mode `both `fill;
2204 Gl.disable `blend;
2205 GlDraw.color (1., 1., 1.);
2207 draw_string (float x) (float (y + 16)) s;
2208 loop (row+1)
2211 loop first
2213 | _ -> ()
2216 let display () =
2217 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2218 GlDraw.viewport margin 0 state.w conf.winh;
2219 pagematrix ();
2220 GlClear.color (scalecolor 0.5);
2221 GlClear.clear [`color];
2222 if conf.zoom > 1.0
2223 then (
2224 Gl.enable `scissor_test;
2225 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2227 List.iter drawpage state.layout;
2228 if conf.zoom > 1.0
2229 then
2230 Gl.disable `scissor_test
2232 if state.x != 0
2233 then (
2234 let x = -.float state.x in
2235 GlMat.translate ~x ();
2237 showrects ();
2238 showsel margin;
2239 GlDraw.viewport 0 0 conf.winw conf.winh;
2240 winmatrix ();
2241 scrollindicator ();
2242 showoutline ();
2243 enttext ();
2244 Glut.swapBuffers ();
2247 let getunder x y =
2248 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2249 let x = x - margin - state.x in
2250 let rec f = function
2251 | l :: rest ->
2252 begin match getopaque l.pageno with
2253 | Some (opaque, _) when validopaque opaque ->
2254 let y = y - l.pagedispy in
2255 if y > 0
2256 then
2257 let y = l.pagey + y in
2258 let x = x - l.pagex in
2259 match whatsunder opaque x y with
2260 | Unone -> f rest
2261 | under -> under
2262 else
2263 f rest
2264 | _ ->
2265 f rest
2267 | [] -> Unone
2269 f state.layout
2272 let viewmouse button bstate x y =
2273 match button with
2274 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2275 if Glut.getModifiers () land Glut.active_ctrl != 0
2276 then (
2277 match state.mstate with
2278 | Mzoom (oldn, i) ->
2279 if oldn = n
2280 then (
2281 if i = 2
2282 then
2283 let incr =
2284 match n with
2285 | 4 ->
2286 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2287 | _ ->
2288 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2290 let zoom = conf.zoom +. incr in
2291 setzoom zoom;
2292 state.mstate <- Mzoom (n, 0);
2293 else
2294 state.mstate <- Mzoom (n, i+1);
2296 else state.mstate <- Mzoom (n, 0)
2298 | _ -> state.mstate <- Mzoom (n, 0)
2300 else (
2301 if state.ascrollstep > 0
2302 then
2303 setautoscrollspeed (n=4)
2304 else
2305 let incr =
2306 if n = 3
2307 then -conf.scrollstep
2308 else conf.scrollstep
2310 let incr = incr * 2 in
2311 let y = clamp incr in
2312 gotoy_and_clear_text y
2315 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2316 if bstate = Glut.DOWN
2317 then (
2318 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2319 state.mstate <- Mpan (x, y)
2321 else
2322 state.mstate <- Mnone
2324 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2325 if bstate = Glut.DOWN
2326 then
2327 let position, sh = scrollph state.y in
2328 if y > truncate position && y < truncate (position +. sh)
2329 then
2330 state.mstate <- Mscroll
2331 else
2332 let percent = float y /. float conf.winh in
2333 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2334 gotoy desty;
2335 state.mstate <- Mscroll
2336 else
2337 state.mstate <- Mnone
2339 | Glut.LEFT_BUTTON ->
2340 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2341 begin match dest with
2342 | Ulinkgoto (pageno, top) ->
2343 if pageno >= 0
2344 then (
2345 addnav ();
2346 gotopage1 pageno top;
2349 | Ulinkuri s ->
2350 print_endline s
2352 | Unone when bstate = Glut.DOWN ->
2353 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2354 state.mstate <- Mpan (x, y);
2356 | Unone | Utext _ ->
2357 if bstate = Glut.DOWN
2358 then (
2359 if conf.angle mod 360 = 0
2360 then (
2361 state.mstate <- Msel ((x, y), (x, y));
2362 Glut.postRedisplay ()
2365 else (
2366 match state.mstate with
2367 | Mnone -> ()
2369 | Mzoom _ | Mscroll ->
2370 state.mstate <- Mnone
2372 | Mpan _ ->
2373 Glut.setCursor Glut.CURSOR_INHERIT;
2374 state.mstate <- Mnone
2376 | Msel ((x0, y0), (x1, y1)) ->
2377 let f l =
2378 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2379 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2380 then
2381 match getopaque l.pageno with
2382 | Some (opaque, _) when validopaque opaque ->
2383 copysel opaque
2384 | _ -> ()
2386 List.iter f state.layout;
2387 copysel ""; (* ugly *)
2388 Glut.setCursor Glut.CURSOR_INHERIT;
2389 state.mstate <- Mnone;
2393 | _ -> ()
2396 let birdseyemouse button bstate x y
2397 (conf, leftx, pageno, hooverpageno, anchor) =
2398 match button with
2399 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2400 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2401 let rec loop = function
2402 | [] -> ()
2403 | l :: rest ->
2404 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2405 && x > margin && x < margin + l.pagew
2406 then (
2407 birdseyeoff (conf, leftx, l.pageno, hooverpageno, anchor) false;
2409 else loop rest
2411 loop state.layout
2412 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
2413 | _ -> ()
2416 let mouse bstate button x y =
2417 match state.mode with
2418 | View -> viewmouse button bstate x y
2419 | Birdseye beye -> birdseyemouse button bstate x y beye
2420 | Textentry _ -> ()
2421 | Outline _ -> ()
2424 let mouse ~button ~state ~x ~y = mouse state button x y;;
2426 let motion ~x ~y =
2427 match state.mode with
2428 | Outline _ -> ()
2429 | _ ->
2430 match state.mstate with
2431 | Mzoom _ | Mnone -> ()
2433 | Mpan (x0, y0) ->
2434 let dx = x - x0
2435 and dy = y0 - y in
2436 state.mstate <- Mpan (x, y);
2437 if conf.zoom > 1.0 then state.x <- state.x + dx;
2438 let y = clamp dy in
2439 gotoy_and_clear_text y
2441 | Msel (a, _) ->
2442 state.mstate <- Msel (a, (x, y));
2443 Glut.postRedisplay ()
2445 | Mscroll ->
2446 let y = min conf.winh (max 0 y) in
2447 let percent = float y /. float conf.winh in
2448 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2449 gotoy_and_clear_text y
2452 let pmotion ~x ~y =
2453 match state.mode with
2454 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
2455 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2456 let rec loop = function
2457 | [] ->
2458 if hooverpageno != -1
2459 then (
2460 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
2461 Glut.postRedisplay ();
2463 | l :: rest ->
2464 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2465 && x > margin && x < margin + l.pagew
2466 then (
2467 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
2468 Glut.postRedisplay ();
2470 else loop rest
2472 loop state.layout
2474 | Outline _ -> ()
2475 | _ ->
2476 match state.mstate with
2477 | Mnone ->
2478 begin match getunder x y with
2479 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2480 | Ulinkuri uri ->
2481 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2482 Glut.setCursor Glut.CURSOR_INFO
2483 | Ulinkgoto (page, y) ->
2484 if conf.underinfo
2485 then showtext 'p' ("age: " ^ string_of_int page);
2486 Glut.setCursor Glut.CURSOR_INFO
2487 | Utext s ->
2488 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2489 Glut.setCursor Glut.CURSOR_TEXT
2492 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
2497 module State =
2498 struct
2499 open Parser
2501 let home =
2503 match Sys.os_type with
2504 | "Win32" -> Sys.getenv "HOMEPATH"
2505 | _ -> Sys.getenv "HOME"
2506 with exn ->
2507 prerr_endline
2508 ("Can not determine home directory location: " ^
2509 Printexc.to_string exn);
2513 let config_of c attrs =
2514 let apply c k v =
2516 match k with
2517 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2518 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2519 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2520 | "preload" -> { c with preload = bool_of_string v }
2521 | "page-bias" -> { c with pagebias = int_of_string v }
2522 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
2523 | "auto-scroll-step" ->
2524 { c with autoscrollstep = max 0 (int_of_string v) }
2525 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2526 | "crop-hack" -> { c with crophack = bool_of_string v }
2527 | "throttle" -> { c with showall = bool_of_string v }
2528 | "highlight-links" -> { c with hlinks = bool_of_string v }
2529 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2530 | "vertical-margin" ->
2531 { c with interpagespace = max 0 (int_of_string v) }
2532 | "zoom" ->
2533 let zoom = float_of_string v /. 100. in
2534 let zoom = max 0.01 (min 2.2 zoom) in
2535 { c with zoom = zoom }
2536 | "presentation" -> { c with presentation = bool_of_string v }
2537 | "rotation-angle" -> { c with angle = int_of_string v }
2538 | "width" -> { c with winw = max 20 (int_of_string v) }
2539 | "height" -> { c with winh = max 20 (int_of_string v) }
2540 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2541 | "proportional-display" -> { c with proportional = bool_of_string v }
2542 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2543 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2544 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2545 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2546 | _ -> c
2547 with exn ->
2548 prerr_endline ("Error processing attribute (`" ^
2549 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2552 let rec fold c = function
2553 | [] -> c
2554 | (k, v) :: rest ->
2555 let c = apply c k v in
2556 fold c rest
2558 fold c attrs;
2561 let fromstring f pos n v d =
2562 try f v
2563 with exn ->
2564 dolog "Error processing attribute (%S=%S) at %d\n%s"
2565 n v pos (Printexc.to_string exn)
2570 let bookmark_of attrs =
2571 let rec fold title page rely = function
2572 | ("title", v) :: rest -> fold v page rely rest
2573 | ("page", v) :: rest -> fold title v rely rest
2574 | ("rely", v) :: rest -> fold title page v rest
2575 | _ :: rest -> fold title page rely rest
2576 | [] -> title, page, rely
2578 fold "invalid" "0" "0" attrs
2581 let doc_of attrs =
2582 let rec fold path page rely pan = function
2583 | ("path", v) :: rest -> fold v page rely pan rest
2584 | ("page", v) :: rest -> fold path v rely pan rest
2585 | ("rely", v) :: rest -> fold path page v pan rest
2586 | ("pan", v) :: rest -> fold path page rely v rest
2587 | _ :: rest -> fold path page rely pan rest
2588 | [] -> path, page, rely, pan
2590 fold "" "0" "0" "0" attrs
2593 let setconf dst src =
2594 dst.scrollw <- src.scrollw;
2595 dst.scrollh <- src.scrollh;
2596 dst.icase <- src.icase;
2597 dst.preload <- src.preload;
2598 dst.pagebias <- src.pagebias;
2599 dst.verbose <- src.verbose;
2600 dst.scrollstep <- src.scrollstep;
2601 dst.maxhfit <- src.maxhfit;
2602 dst.crophack <- src.crophack;
2603 dst.autoscrollstep <- src.autoscrollstep;
2604 dst.showall <- src.showall;
2605 dst.hlinks <- src.hlinks;
2606 dst.underinfo <- src.underinfo;
2607 dst.interpagespace <- src.interpagespace;
2608 dst.zoom <- src.zoom;
2609 dst.presentation <- src.presentation;
2610 dst.angle <- src.angle;
2611 dst.winw <- src.winw;
2612 dst.winh <- src.winh;
2613 dst.savebmarks <- src.savebmarks;
2614 dst.memlimit <- src.memlimit;
2615 dst.proportional <- src.proportional;
2616 dst.texcount <- src.texcount;
2617 dst.sliceheight <- src.sliceheight;
2618 dst.thumbw <- src.thumbw;
2621 let unent s =
2622 let l = String.length s in
2623 let b = Buffer.create l in
2624 unent b s 0 l;
2625 Buffer.contents b;
2628 let get s =
2629 let h = Hashtbl.create 10 in
2630 let dc = { defconf with angle = defconf.angle } in
2631 let rec toplevel v t spos epos =
2632 match t with
2633 | Vdata | Vcdata | Vend -> v
2634 | Vopen ("llppconfig", attrs, closed) ->
2635 if closed
2636 then v
2637 else { v with f = llppconfig }
2638 | Vopen _ ->
2639 error "unexpected subelement at top level" s spos
2640 | Vclose tag -> error "unexpected close at top level" s spos
2642 and llppconfig v t spos epos =
2643 match t with
2644 | Vdata | Vcdata | Vend -> v
2645 | Vopen ("defaults", attrs, closed) ->
2646 let c = config_of dc attrs in
2647 setconf dc c;
2648 if closed
2649 then v
2650 else { v with f = skip "defaults" (fun () -> v) }
2652 | Vopen ("doc", attrs, closed) ->
2653 let pathent, spage, srely, span = doc_of attrs in
2654 let path = unent pathent
2655 and pageno = fromstring int_of_string spos "page" spage 0
2656 and rely = fromstring float_of_string spos "rely" srely 0.0
2657 and pan = fromstring int_of_string spos "pan" span 0 in
2658 let c = config_of dc attrs in
2659 let anchor = (pageno, rely) in
2660 if closed
2661 then (Hashtbl.add h path (c, [], pan, anchor); v)
2662 else { v with f = doc path pan anchor c [] }
2664 | Vopen (tag, _, closed) ->
2665 error "unexpected subelement in llppconfig" s spos
2667 | Vclose "llppconfig" -> { v with f = toplevel }
2668 | Vclose tag -> error "unexpected close in llppconfig" s spos
2670 and doc path pan anchor c bookmarks v t spos epos =
2671 match t with
2672 | Vdata | Vcdata -> v
2673 | Vend -> error "unexpected end of input in doc" s spos
2674 | Vopen ("bookmarks", attrs, closed) ->
2675 { v with f = pbookmarks path pan anchor c bookmarks }
2677 | Vopen (tag, _, _) ->
2678 error "unexpected subelement in doc" s spos
2680 | Vclose "doc" ->
2681 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
2682 { v with f = llppconfig }
2684 | Vclose tag -> error "unexpected close in doc" s spos
2686 and pbookmarks path pan anchor c bookmarks v t spos epos =
2687 match t with
2688 | Vdata | Vcdata -> v
2689 | Vend -> error "unexpected end of input in bookmarks" s spos
2690 | Vopen ("item", attrs, closed) ->
2691 let titleent, spage, srely = bookmark_of attrs in
2692 let page = fromstring int_of_string spos "page" spage 0
2693 and rely = fromstring float_of_string spos "rely" srely 0.0 in
2694 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2695 if closed
2696 then { v with f = pbookmarks path pan anchor c bookmarks }
2697 else
2698 let f () = v in
2699 { v with f = skip "item" f }
2701 | Vopen _ ->
2702 error "unexpected subelement in bookmarks" s spos
2704 | Vclose "bookmarks" ->
2705 { v with f = doc path pan anchor c bookmarks }
2707 | Vclose tag -> error "unexpected close in bookmarks" s spos
2709 and skip tag f v t spos epos =
2710 match t with
2711 | Vdata | Vcdata -> v
2712 | Vend ->
2713 error ("unexpected end of input in skipped " ^ tag) s spos
2714 | Vopen (tag', _, closed) ->
2715 if closed
2716 then v
2717 else
2718 let f' () = { v with f = skip tag f } in
2719 { v with f = skip tag' f' }
2720 | Vclose ctag ->
2721 if tag = ctag
2722 then f ()
2723 else error ("unexpected close in skipped " ^ tag) s spos
2726 parse { f = toplevel; accu = () } s;
2727 h, dc;
2730 let do_load f ic =
2732 let len = in_channel_length ic in
2733 let s = String.create len in
2734 really_input ic s 0 len;
2735 f s;
2736 with
2737 | Parse_error (msg, s, pos) ->
2738 let subs = subs s pos in
2739 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2740 failwith ("parse error: " ^ s)
2742 | exn ->
2743 failwith ("config load error: " ^ Printexc.to_string exn)
2746 let path =
2747 let dir =
2749 let dir = Filename.concat home ".config" in
2750 if Sys.is_directory dir then dir else home
2751 with _ -> home
2753 Filename.concat dir "llpp.conf"
2756 let load1 f =
2757 if Sys.file_exists path
2758 then
2759 match
2760 (try Some (open_in_bin path)
2761 with exn ->
2762 prerr_endline
2763 ("Error opening configuation file `" ^ path ^ "': " ^
2764 Printexc.to_string exn);
2765 None
2767 with
2768 | Some ic ->
2769 begin try
2770 f (do_load get ic)
2771 with exn ->
2772 prerr_endline
2773 ("Error loading configuation from `" ^ path ^ "': " ^
2774 Printexc.to_string exn);
2775 end;
2776 close_in ic;
2778 | None -> ()
2779 else
2780 f (Hashtbl.create 0, defconf)
2783 let load () =
2784 let f (h, dc) =
2785 let pc, pb, px, pa =
2787 Hashtbl.find h (Filename.basename state.path)
2788 with Not_found -> dc, [], 0, (0, 0.0)
2790 setconf defconf dc;
2791 setconf conf pc;
2792 state.bookmarks <- pb;
2793 state.x <- px;
2794 cbput state.hists.nav pa;
2796 load1 f
2799 let add_attrs bb always dc c =
2800 let ob s a b =
2801 if always || a != b
2802 then Printf.bprintf bb "\n %s='%b'" s a
2803 and oi s a b =
2804 if always || a != b
2805 then Printf.bprintf bb "\n %s='%d'" s a
2806 and oz s a b =
2807 if always || a <> b
2808 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2810 let w, h =
2811 if always
2812 then dc.winw, dc.winh
2813 else
2814 match state.fullscreen with
2815 | Some wh -> wh
2816 | None -> c.winw, c.winh
2818 let zoom, presentation, interpagespace, showall=
2819 if always
2820 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2821 else
2822 match state.mode with
2823 | Birdseye (bc, _, _, _, _) ->
2824 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2825 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2827 oi "width" w dc.winw;
2828 oi "height" h dc.winh;
2829 oi "scroll-bar-width" c.scrollw dc.scrollw;
2830 oi "scroll-handle-height" c.scrollh dc.scrollh;
2831 ob "case-insensitive-search" c.icase dc.icase;
2832 ob "preload" c.preload dc.preload;
2833 oi "page-bias" c.pagebias dc.pagebias;
2834 oi "scroll-step" c.scrollstep dc.scrollstep;
2835 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
2836 ob "max-height-fit" c.maxhfit dc.maxhfit;
2837 ob "crop-hack" c.crophack dc.crophack;
2838 ob "throttle" showall dc.showall;
2839 ob "highlight-links" c.hlinks dc.hlinks;
2840 ob "under-cursor-info" c.underinfo dc.underinfo;
2841 oi "vertical-margin" interpagespace dc.interpagespace;
2842 oz "zoom" zoom dc.zoom;
2843 ob "presentation" presentation dc.presentation;
2844 oi "rotation-angle" c.angle dc.angle;
2845 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2846 ob "proportional-display" c.proportional dc.proportional;
2847 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2848 oi "texcount" c.texcount dc.texcount;
2849 oi "slice-height" c.sliceheight dc.sliceheight;
2850 oi "thumbnail-width" c.thumbw dc.thumbw;
2853 let save () =
2854 let bb = Buffer.create 32768 in
2855 let f (h, dc) =
2856 Buffer.add_string bb "<llppconfig>\n<defaults ";
2857 add_attrs bb true dc dc;
2858 Buffer.add_string bb "/>\n";
2860 let adddoc path pan anchor c bookmarks =
2861 if bookmarks == [] && c = dc && anchor = emptyanchor
2862 then ()
2863 else (
2864 Printf.bprintf bb "<doc path='%s'"
2865 (enent path 0 (String.length path));
2867 if anchor <> emptyanchor
2868 then (
2869 let n, y = anchor in
2870 Printf.bprintf bb " page='%d'" n;
2871 Printf.bprintf bb " rely='%f'" y;
2874 if pan != 0
2875 then Printf.bprintf bb " pan='%d'" pan;
2877 add_attrs bb false dc c;
2879 begin match bookmarks with
2880 | [] -> Buffer.add_string bb "/>\n"
2881 | _ ->
2882 Buffer.add_string bb ">\n<bookmarks>\n";
2883 List.iter (fun (title, _level, page, rely) ->
2884 Printf.bprintf bb
2885 "<item title='%s' page='%d' rely='%f'/>\n"
2886 (enent title 0 (String.length title))
2887 page
2888 rely
2889 ) bookmarks;
2890 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2891 end;
2895 let pan =
2896 match state.mode with
2897 | Birdseye (_, pan, _, _, _) -> pan
2898 | _ -> state.x
2900 let basename = Filename.basename state.path in
2901 adddoc basename pan (getanchor ())
2902 { conf with
2903 autoscrollstep =
2904 if state.ascrollstep > 0
2905 then state.ascrollstep
2906 else conf.autoscrollstep }
2907 (if conf.savebmarks then state.bookmarks else []);
2909 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2910 if basename <> path
2911 then adddoc path x y c bookmarks
2912 ) h;
2913 Buffer.add_string bb "</llppconfig>";
2915 load1 f;
2916 if Buffer.length bb > 0
2917 then
2919 let tmp = path ^ ".tmp" in
2920 let oc = open_out_bin tmp in
2921 Buffer.output_buffer oc bb;
2922 close_out oc;
2923 Sys.rename tmp path;
2924 with exn ->
2925 prerr_endline
2926 ("error while saving configuration: " ^ Printexc.to_string exn)
2928 end;;
2930 let () =
2931 Arg.parse
2932 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2933 (fun s -> state.path <- s)
2934 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2936 if String.length state.path = 0
2937 then (prerr_endline "filename missing"; exit 1);
2939 State.load ();
2941 let _ = Glut.init Sys.argv in
2942 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2943 let () = Glut.initWindowSize conf.winw conf.winh in
2944 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
2946 let csock, ssock =
2947 if Sys.os_type = "Unix"
2948 then
2949 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2950 else
2951 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2952 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2953 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2954 Unix.bind sock addr;
2955 Unix.listen sock 1;
2956 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2957 Unix.connect csock addr;
2958 let ssock, _ = Unix.accept sock in
2959 Unix.close sock;
2960 let opts sock =
2961 Unix.setsockopt sock Unix.TCP_NODELAY true;
2962 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2964 opts ssock;
2965 opts csock;
2966 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2967 ssock, csock
2970 let () = Glut.displayFunc display in
2971 let () = Glut.reshapeFunc reshape in
2972 let () = Glut.keyboardFunc keyboard in
2973 let () = Glut.specialFunc special in
2974 let () = Glut.idleFunc (Some idle) in
2975 let () = Glut.mouseFunc mouse in
2976 let () = Glut.motionFunc motion in
2977 let () = Glut.passiveMotionFunc pmotion in
2979 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2980 state.csock <- csock;
2981 state.ssock <- ssock;
2982 state.text <- "Opening " ^ state.path;
2983 writeopen state.path state.password;
2985 at_exit State.save;
2987 let rec handlelablglutbug () =
2989 Glut.mainLoop ();
2990 with Glut.BadEnum "key in special_of_int" ->
2991 showtext '!' " LablGlut bug: special key not recognized";
2992 handlelablglutbug ()
2994 handlelablglutbug ();