Allow setting tumbnail width
[llpp.git] / main.ml
blob5a85ece2eb26b36cab6e8f4ca661fb39bd59106b
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 birdseyeon () =
1039 let zoom = float conf.thumbw /. float conf.winw in
1040 let birdseyepageno =
1041 let rec fold = function
1042 | [] -> 0
1043 | l :: _ when l.pagey = 0 -> l.pageno
1044 | _ :: rest -> fold rest
1046 fold state.layout
1048 state.mode <- Birdseye (
1049 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1051 conf.zoom <- zoom;
1052 conf.presentation <- false;
1053 conf.interpagespace <- 10;
1054 conf.hlinks <- false;
1055 state.x <- 0;
1056 state.mstate <- Mnone;
1057 conf.showall <- false;
1058 Glut.setCursor Glut.CURSOR_INHERIT;
1059 if conf.verbose
1060 then
1061 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1062 (100.0*.zoom)
1063 else
1064 state.text <- ""
1066 reshape conf.winw conf.winh;
1069 let birdseyeoff (c, leftx, pageno, _, anchor) goback =
1070 state.mode <- View;
1071 conf.zoom <- c.zoom;
1072 conf.presentation <- c.presentation;
1073 conf.interpagespace <- c.interpagespace;
1074 conf.showall <- c.showall;
1075 conf.hlinks <- c.hlinks;
1076 state.x <- leftx;
1077 if conf.verbose
1078 then
1079 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1080 (100.0*.conf.zoom)
1082 reshape conf.winw conf.winh;
1083 state.anchor <- if goback then anchor else (pageno, 0.0);
1086 let togglebirdseye () =
1087 match state.mode with
1088 | Birdseye vals -> birdseyeoff vals true
1089 | View | Outline _ -> birdseyeon ()
1090 | _ -> ()
1093 let optentry text key =
1094 let btos b = if b then "on" else "off" in
1095 let c = Char.unsafe_chr key in
1096 match c with
1097 | 's' ->
1098 let ondone s =
1099 try conf.scrollstep <- int_of_string s with exc ->
1100 state.text <- Printf.sprintf "bad integer `%s': %s"
1101 s (Printexc.to_string exc)
1103 TEswitch ('#', "", None, intentry, ondone)
1105 | 'A' ->
1106 let ondone s =
1108 conf.autoscrollstep <- int_of_string s;
1109 if state.ascrollstep > 0
1110 then state.ascrollstep <- conf.autoscrollstep;
1111 with exc ->
1112 state.text <- Printf.sprintf "bad integer `%s': %s"
1113 s (Printexc.to_string exc)
1115 TEswitch ('*', "", None, intentry, ondone)
1117 | 'Z' ->
1118 let ondone s =
1120 let zoom = float (int_of_string s) /. 100.0 in
1121 setzoom zoom
1122 with exc ->
1123 state.text <- Printf.sprintf "bad integer `%s': %s"
1124 s (Printexc.to_string exc)
1126 TEswitch ('@', "", None, intentry, ondone)
1128 | 't' ->
1129 let ondone s =
1131 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1132 state.text <-
1133 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1134 begin match state.mode with
1135 | Textentry (_, Birdseye beye) ->
1136 birdseyeoff beye false;
1137 birdseyeon ()
1138 | _ -> ()
1139 end;
1140 with exc ->
1141 state.text <- Printf.sprintf "bad integer `%s': %s"
1142 s (Printexc.to_string exc)
1144 TEswitch ('$', "", None, intentry, ondone)
1146 | 'R' ->
1147 let ondone s =
1148 match try
1149 Some (int_of_string s)
1150 with exc ->
1151 state.text <- Printf.sprintf "bad integer `%s': %s"
1152 s (Printexc.to_string exc);
1153 None
1154 with
1155 | Some angle -> reinit angle conf.proportional
1156 | None -> ()
1158 TEswitch ('^', "", None, intentry, ondone)
1160 | 'i' ->
1161 conf.icase <- not conf.icase;
1162 TEdone ("case insensitive search " ^ (btos conf.icase))
1164 | 'p' ->
1165 conf.preload <- not conf.preload;
1166 gotoy state.y;
1167 TEdone ("preload " ^ (btos conf.preload))
1169 | 'v' ->
1170 conf.verbose <- not conf.verbose;
1171 TEdone ("verbose " ^ (btos conf.verbose))
1173 | 'h' ->
1174 conf.maxhfit <- not conf.maxhfit;
1175 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1176 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1178 | 'c' ->
1179 conf.crophack <- not conf.crophack;
1180 TEdone ("crophack " ^ btos conf.crophack)
1182 | 'a' ->
1183 conf.showall <- not conf.showall;
1184 TEdone ("showall " ^ btos conf.showall)
1186 | 'f' ->
1187 conf.underinfo <- not conf.underinfo;
1188 TEdone ("underinfo " ^ btos conf.underinfo)
1190 | 'P' ->
1191 conf.savebmarks <- not conf.savebmarks;
1192 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1194 | 'S' ->
1195 let ondone s =
1197 let pageno, py =
1198 match state.layout with
1199 | [] -> 0, 0
1200 | l :: _ ->
1201 l.pageno, l.pagey
1203 conf.interpagespace <- int_of_string s;
1204 state.maxy <- calcheight ();
1205 let y = getpagey pageno in
1206 gotoy (y + py)
1207 with exc ->
1208 state.text <- Printf.sprintf "bad integer `%s': %s"
1209 s (Printexc.to_string exc)
1211 TEswitch ('%', "", None, intentry, ondone)
1213 | 'l' ->
1214 reinit conf.angle (not conf.proportional);
1215 TEdone ("proprortional display " ^ btos conf.proportional)
1217 | _ ->
1218 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1219 TEstop
1222 let maxoutlinerows () = (conf.winh - 31) / 16;;
1224 let enterselector allowdel outlines errmsg msg =
1225 if Array.length outlines = 0
1226 then (
1227 showtext ' ' errmsg;
1229 else (
1230 state.text <- msg;
1231 Glut.setCursor Glut.CURSOR_INHERIT;
1232 let pageno =
1233 match state.layout with
1234 | [] -> -1
1235 | {pageno=pageno} :: rest -> pageno
1237 let active =
1238 let rec loop n =
1239 if n = Array.length outlines
1240 then 0
1241 else
1242 let (_, _, outlinepageno, _) = outlines.(n) in
1243 if outlinepageno >= pageno then n else loop (n+1)
1245 loop 0
1247 state.mode <- Outline
1248 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "");
1249 Glut.postRedisplay ();
1253 let enteroutlinemode () =
1254 let outlines, msg =
1255 match state.outlines with
1256 | Oarray a -> a, ""
1257 | Olist l ->
1258 let a = Array.of_list (List.rev l) in
1259 state.outlines <- Oarray a;
1260 a, ""
1261 | Onarrow (pat, a, b) ->
1262 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1264 enterselector false outlines "Document has no outline" msg;
1267 let enterbookmarkmode () =
1268 let bookmarks = Array.of_list state.bookmarks in
1269 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1272 let quickbookmark ?title () =
1273 match state.layout with
1274 | [] -> ()
1275 | l :: _ ->
1276 let title =
1277 match title with
1278 | None ->
1279 let sec = Unix.gettimeofday () in
1280 let tm = Unix.localtime sec in
1281 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1282 (l.pageno+1)
1283 tm.Unix.tm_mday
1284 tm.Unix.tm_mon
1285 (tm.Unix.tm_year + 1900)
1286 tm.Unix.tm_hour
1287 tm.Unix.tm_min
1288 | Some title -> title
1290 state.bookmarks <-
1291 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1294 let doreshape w h =
1295 state.fullscreen <- None;
1296 Glut.reshapeWindow w h;
1299 let writeopen path password =
1300 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1303 let opendoc path password =
1304 invalidate ();
1305 state.path <- path;
1306 state.password <- password;
1307 state.gen <- state.gen + 1;
1309 writeopen path password;
1310 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1311 wcmd "geometry" [`i state.w; `i conf.winh];
1314 let viewkeyboard ~key ~x ~y =
1315 let enttext te =
1316 state.mode <- Textentry (te, state.mode);
1317 state.text <- "";
1318 enttext ();
1319 Glut.postRedisplay ()
1321 let c = Char.chr key in
1322 match c with
1323 | '\027' | 'q' ->
1324 exit 0
1326 | '\008' ->
1327 let y = getnav () in
1328 gotoy_and_clear_text y
1330 | 'o' ->
1331 enteroutlinemode ()
1333 | 'u' ->
1334 state.rects <- [];
1335 state.text <- "";
1336 Glut.postRedisplay ()
1338 | '/' | '?' ->
1339 let ondone isforw s =
1340 cbput state.hists.pat s;
1341 state.searchpattern <- s;
1342 search s isforw
1344 enttext (c, "", Some (onhist state.hists.pat),
1345 textentry, ondone (c ='/'))
1347 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1348 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1349 setzoom (min 2.2 (conf.zoom +. incr))
1351 | '+' ->
1352 let ondone s =
1353 let n =
1354 try int_of_string s with exc ->
1355 state.text <- Printf.sprintf "bad integer `%s': %s"
1356 s (Printexc.to_string exc);
1357 max_int
1359 if n != max_int
1360 then (
1361 conf.pagebias <- n;
1362 state.text <- "page bias is now " ^ string_of_int n;
1365 enttext ('+', "", None, intentry, ondone)
1367 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1368 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1369 setzoom (max 0.01 (conf.zoom -. decr))
1371 | '-' ->
1372 let ondone msg =
1373 state.text <- msg;
1375 enttext ('-', "", None, optentry, ondone)
1377 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1378 setzoom 1.0
1380 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1381 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1382 if zoom < 1.0
1383 then setzoom zoom
1385 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1386 togglebirdseye ()
1388 | '0' .. '9' ->
1389 let ondone s =
1390 let n =
1391 try int_of_string s with exc ->
1392 state.text <- Printf.sprintf "bad integer `%s': %s"
1393 s (Printexc.to_string exc);
1396 if n >= 0
1397 then (
1398 addnav ();
1399 cbput state.hists.pag (string_of_int n);
1400 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1403 let pageentry text key =
1404 match Char.unsafe_chr key with
1405 | 'g' -> TEdone text
1406 | _ -> intentry text key
1408 let text = "x" in text.[0] <- c;
1409 enttext (':', text, Some (onhist state.hists.pag), pageentry, ondone)
1411 | 'b' ->
1412 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1413 reshape conf.winw conf.winh;
1415 | 'l' ->
1416 conf.hlinks <- not conf.hlinks;
1417 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1418 Glut.postRedisplay ()
1420 | 'a' ->
1421 if state.ascrollstep = 0
1422 then state.ascrollstep <- conf.autoscrollstep
1423 else (
1424 conf.autoscrollstep <- state.ascrollstep;
1425 state.ascrollstep <- 0;
1428 | 'P' ->
1429 conf.presentation <- not conf.presentation;
1430 showtext ' ' ("presentation mode " ^
1431 if conf.presentation then "on" else "off");
1432 represent ()
1434 | 'f' ->
1435 begin match state.fullscreen with
1436 | None ->
1437 state.fullscreen <- Some (conf.winw, conf.winh);
1438 Glut.fullScreen ()
1439 | Some (w, h) ->
1440 state.fullscreen <- None;
1441 doreshape w h
1444 | 'g' ->
1445 gotoy_and_clear_text 0
1447 | 'n' ->
1448 search state.searchpattern true
1450 | 'p' | 'N' ->
1451 search state.searchpattern false
1453 | 't' ->
1454 begin match state.layout with
1455 | [] -> ()
1456 | l :: _ ->
1457 gotoy_and_clear_text (getpagey l.pageno)
1460 | ' ' ->
1461 begin match List.rev state.layout with
1462 | [] -> ()
1463 | l :: _ ->
1464 let pageno = min (l.pageno+1) (state.pagecount-1) in
1465 gotoy_and_clear_text (getpagey pageno)
1468 | '\127' ->
1469 begin match state.layout with
1470 | [] -> ()
1471 | l :: _ ->
1472 let pageno = max 0 (l.pageno-1) in
1473 gotoy_and_clear_text (getpagey pageno)
1476 | '=' ->
1477 let f (fn, ln) l =
1478 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1480 let fn, ln = List.fold_left f (-1, -1) state.layout in
1481 let s =
1482 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1483 let percent =
1484 if maxy <= 0
1485 then 100.
1486 else (100. *. (float state.y /. float maxy)) in
1487 if fn = ln
1488 then
1489 Printf.sprintf "Page %d of %d %.2f%%"
1490 (fn+1) state.pagecount percent
1491 else
1492 Printf.sprintf
1493 "Pages %d-%d of %d %.2f%%"
1494 (fn+1) (ln+1) state.pagecount percent
1496 showtext ' ' s;
1498 | 'w' ->
1499 begin match state.layout with
1500 | [] -> ()
1501 | l :: _ ->
1502 doreshape (l.pagew + conf.scrollw) l.pageh;
1503 Glut.postRedisplay ();
1506 | '\'' ->
1507 enterbookmarkmode ()
1509 | 'm' ->
1510 let ondone s =
1511 match state.layout with
1512 | l :: _ ->
1513 state.bookmarks <-
1514 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1515 :: state.bookmarks
1516 | _ -> ()
1518 enttext ('~', "", None, textentry, ondone)
1520 | '~' ->
1521 quickbookmark ();
1522 showtext ' ' "Quick bookmark added";
1524 | 'z' ->
1525 begin match state.layout with
1526 | l :: _ ->
1527 let rect = getpdimrect l.pagedimno in
1528 let w, h =
1529 if conf.crophack
1530 then
1531 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1532 truncate (1.2 *. (rect.(3) -. rect.(0))))
1533 else
1534 (truncate (rect.(1) -. rect.(0)),
1535 truncate (rect.(3) -. rect.(0)))
1537 if w != 0 && h != 0
1538 then
1539 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1541 Glut.postRedisplay ();
1543 | [] -> ()
1546 | '<' | '>' ->
1547 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1549 | '[' | ']' ->
1550 state.colorscale <-
1551 max 0.0
1552 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1553 Glut.postRedisplay ()
1555 | 'k' -> gotoy (clamp (-conf.scrollstep))
1556 | 'j' -> gotoy (clamp conf.scrollstep)
1558 | 'r' -> opendoc state.path state.password
1560 | _ ->
1561 vlog "huh? %d %c" key (Char.chr key);
1564 let textentrykeyboard ~key ~x ~y ((c, text, opthist, onkey, ondone), mode) =
1565 let enttext te =
1566 state.mode <- Textentry (te, mode);
1567 state.text <- "";
1568 enttext ();
1569 Glut.postRedisplay ()
1571 match Char.unsafe_chr key with
1572 | '\008' ->
1573 let len = String.length text in
1574 if len = 0
1575 then (
1576 state.mode <- mode;
1577 Glut.postRedisplay ();
1579 else (
1580 let s = String.sub text 0 (len - 1) in
1581 enttext (c, s, opthist, onkey, ondone)
1584 | '\r' | '\n' ->
1585 ondone text;
1586 state.mode <- mode;
1587 Glut.postRedisplay ()
1589 | '\027' ->
1590 begin match opthist with
1591 | None -> ()
1592 | Some (_, onhistcancel) -> onhistcancel ()
1593 end;
1594 state.mode <- View;
1595 Glut.postRedisplay ()
1597 | _ ->
1598 begin match onkey text key with
1599 | TEdone text ->
1600 state.mode <- mode;
1601 ondone text;
1602 Glut.postRedisplay ()
1604 | TEcont text ->
1605 enttext (c, text, opthist, onkey, ondone);
1607 | TEstop ->
1608 state.mode <- mode;
1609 Glut.postRedisplay ()
1611 | TEswitch te ->
1612 state.mode <- Textentry (te, mode);
1613 Glut.postRedisplay ()
1614 end;
1617 let birdseyekeyboard ~key ~x ~y ((_, _, pageno, _, anchor) as beye) =
1618 match key with
1619 | 27 ->
1620 birdseyeoff beye true
1622 | 12 ->
1623 let y, h = getpageyh pageno in
1624 let top = (conf.winh - h) / 2 in
1625 gotoy (max 0 (y - top))
1627 | 13 ->
1628 birdseyeoff beye false
1630 | _ ->
1631 viewkeyboard ~key ~x ~y
1634 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1635 let narrow outlines pattern =
1636 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1637 match reopt with
1638 | None -> None
1639 | Some re ->
1640 let rec fold accu n =
1641 if n = -1
1642 then accu
1643 else
1644 let (s, _, _, _) as o = outlines.(n) in
1645 let accu =
1646 if (try ignore (Str.search_forward re s 0); true
1647 with Not_found -> false)
1648 then (o :: accu)
1649 else accu
1651 fold accu (n-1)
1653 let matched = fold [] (Array.length outlines - 1) in
1654 if matched = [] then None else Some (Array.of_list matched)
1656 let search active pattern incr =
1657 let dosearch re =
1658 let rec loop n =
1659 if n = Array.length outlines || n = -1
1660 then None
1661 else
1662 let (s, _, _, _) = outlines.(n) in
1664 (try ignore (Str.search_forward re s 0); true
1665 with Not_found -> false)
1666 then Some n
1667 else loop (n + incr)
1669 loop active
1672 let re = Str.regexp_case_fold pattern in
1673 dosearch re
1674 with Failure s ->
1675 state.text <- s;
1676 None
1678 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1679 match key with
1680 | 27 ->
1681 if String.length qsearch = 0
1682 then (
1683 state.text <- "";
1684 state.mode <- View;
1685 Glut.postRedisplay ();
1687 else (
1688 state.text <- "";
1689 state.mode <- Outline (allowdel, active, first, outlines, "");
1690 Glut.postRedisplay ();
1693 | 18 | 19 ->
1694 let incr = if key = 18 then -1 else 1 in
1695 let active, first =
1696 match search (active + incr) qsearch incr with
1697 | None ->
1698 state.text <- qsearch ^ " [not found]";
1699 active, first
1700 | Some active ->
1701 state.text <- qsearch;
1702 active, firstof active
1704 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1705 Glut.postRedisplay ();
1707 | 8 ->
1708 let len = String.length qsearch in
1709 if len = 0
1710 then ()
1711 else (
1712 if len = 1
1713 then (
1714 state.text <- "";
1715 state.mode <- Outline (allowdel, active, first, outlines, "");
1717 else
1718 let qsearch = String.sub qsearch 0 (len - 1) in
1719 let active, first =
1720 match search active qsearch ~-1 with
1721 | None ->
1722 state.text <- qsearch ^ " [not found]";
1723 active, first
1724 | Some active ->
1725 state.text <- qsearch;
1726 active, firstof active
1728 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1730 Glut.postRedisplay ()
1732 | 13 ->
1733 if active < Array.length outlines
1734 then (
1735 let (_, _, n, t) = outlines.(active) in
1736 addnav ();
1737 gotopage n t;
1739 state.text <- "";
1740 if allowdel then state.bookmarks <- Array.to_list outlines;
1741 state.mode <- View;
1742 Glut.postRedisplay ();
1744 | _ when key >= 32 && key < 127 ->
1745 let pattern = addchar qsearch (Char.chr key) in
1746 let active, first =
1747 match search active pattern 1 with
1748 | None ->
1749 state.text <- pattern ^ " [not found]";
1750 active, first
1751 | Some active ->
1752 state.text <- pattern;
1753 active, firstof active
1755 state.mode <- Outline (allowdel, active, first, outlines, pattern);
1756 Glut.postRedisplay ()
1758 | 14 when not allowdel -> (* ctrl-n *)
1759 if String.length qsearch > 0
1760 then (
1761 let optoutlines = narrow outlines qsearch in
1762 begin match optoutlines with
1763 | None -> state.text <- "can't narrow"
1764 | Some outlines ->
1765 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1766 match state.outlines with
1767 | Olist l -> ()
1768 | Oarray a ->
1769 state.outlines <- Onarrow (qsearch, outlines, a)
1770 | Onarrow (pat, a, b) ->
1771 state.outlines <- Onarrow (qsearch, outlines, b)
1772 end;
1774 Glut.postRedisplay ()
1776 | 21 when not allowdel -> (* ctrl-u *)
1777 let outline =
1778 match state.outlines with
1779 | Oarray a -> a
1780 | Olist l ->
1781 let a = Array.of_list (List.rev l) in
1782 state.outlines <- Oarray a;
1784 | Onarrow (pat, a, b) ->
1785 state.outlines <- Oarray b;
1786 state.text <- "";
1789 state.mode <- Outline (allowdel, 0, 0, outline, qsearch);
1790 Glut.postRedisplay ()
1792 | 12 ->
1793 state.mode <- Outline
1794 (allowdel, active, firstof active, outlines, qsearch);
1795 Glut.postRedisplay ()
1797 | 127 when allowdel ->
1798 let len = Array.length outlines - 1 in
1799 if len = 0
1800 then (
1801 state.mode <- View;
1802 state.bookmarks <- [];
1804 else (
1805 let bookmarks = Array.init len
1806 (fun i ->
1807 let i = if i >= active then i + 1 else i in
1808 outlines.(i)
1811 state.mode <-
1812 Outline (
1813 allowdel,
1814 min active (len-1),
1815 min first (len-1),
1816 bookmarks, qsearch
1819 Glut.postRedisplay ()
1821 | _ -> dolog "unknown key %d" key
1824 let keyboard ~key ~x ~y =
1825 if key = 7
1826 then
1827 wcmd "interrupt" []
1828 else
1829 match state.mode with
1830 | Outline outline -> outlinekeyboard ~key ~x ~y outline
1831 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
1832 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
1833 | View -> viewkeyboard ~key ~x ~y
1836 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno, anchor) =
1837 match key with
1838 | Glut.KEY_UP ->
1839 let pageno = max 0 (pageno - 1) in
1840 let rec loop = function
1841 | [] -> gotopage1 pageno 0
1842 | l :: _ when l.pageno = pageno ->
1843 if l.pagedispy >= 0 && l.pagey = 0
1844 then Glut.postRedisplay ()
1845 else gotopage1 pageno 0
1846 | _ :: rest -> loop rest
1848 loop state.layout;
1849 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1851 | Glut.KEY_DOWN ->
1852 let pageno = min (state.pagecount - 1) (pageno + 1) in
1853 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1854 let rec loop = function
1855 | [] ->
1856 let y, h = getpageyh pageno in
1857 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1858 gotoy (clamp dy)
1859 | l :: rest when l.pageno = pageno ->
1860 if l.pagevh != l.pageh
1861 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1862 else Glut.postRedisplay ()
1863 | l :: rest -> loop rest
1865 loop state.layout
1867 | Glut.KEY_PAGE_UP ->
1868 begin match state.layout with
1869 | l :: _ ->
1870 if l.pagey != 0
1871 then (
1872 state.mode <- Birdseye (
1873 conf, leftx, l.pageno, hooverpageno, anchor
1875 gotopage1 l.pageno 0;
1877 else (
1878 let layout = layout (state.y-conf.winh) conf.winh in
1879 match layout with
1880 | [] -> gotoy (clamp (-conf.winh))
1881 | l :: _ ->
1882 state.mode <- Birdseye (
1883 conf, leftx, l.pageno, hooverpageno, anchor
1885 gotopage1 l.pageno 0
1888 | [] -> gotoy (clamp (-conf.winh))
1889 end;
1891 | Glut.KEY_PAGE_DOWN ->
1892 begin match List.rev state.layout with
1893 | l :: _ ->
1894 let layout = layout (state.y + conf.winh) conf.winh in
1895 begin match layout with
1896 | [] ->
1897 let incr = l.pageh - l.pagevh in
1898 if incr = 0
1899 then (
1900 state.mode <-
1901 Birdseye (
1902 conf, leftx, state.pagecount - 1, hooverpageno, anchor
1904 Glut.postRedisplay ();
1906 else gotoy (clamp (incr + conf.interpagespace*2));
1908 | l :: _ ->
1909 state.mode <-
1910 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
1911 gotopage1 l.pageno 0;
1914 | [] -> gotoy (clamp conf.winh)
1915 end;
1917 | Glut.KEY_HOME ->
1918 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
1919 gotopage1 0 0
1921 | Glut.KEY_END ->
1922 let pageno = state.pagecount - 1 in
1923 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1924 if not (pagevisible state.layout pageno)
1925 then
1926 let h =
1927 match List.rev state.pdims with
1928 | [] -> conf.winh
1929 | (_, _, h, _) :: _ -> h
1931 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1932 else Glut.postRedisplay ();
1933 | _ -> ()
1936 let setautoscrollspeed goingdown =
1937 let incr = max 1 (state.ascrollstep / 2) in
1938 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
1939 state.ascrollstep <- astep;
1942 let special ~key ~x ~y =
1943 match state.mode with
1944 | View | (Birdseye _) when key = Glut.KEY_F9 ->
1945 togglebirdseye ()
1947 | Birdseye vals ->
1948 birdseyespecial key x y vals
1950 | View ->
1951 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
1952 then setautoscrollspeed (key = Glut.KEY_DOWN)
1953 else
1954 let y =
1955 match key with
1956 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1957 | Glut.KEY_UP -> clamp (-conf.scrollstep)
1958 | Glut.KEY_DOWN -> clamp conf.scrollstep
1959 | Glut.KEY_PAGE_UP ->
1960 if Glut.getModifiers () land Glut.active_ctrl != 0
1961 then
1962 match state.layout with
1963 | [] -> state.y
1964 | l :: _ -> state.y - l.pagey
1965 else
1966 clamp (-conf.winh)
1967 | Glut.KEY_PAGE_DOWN ->
1968 if Glut.getModifiers () land Glut.active_ctrl != 0
1969 then
1970 match List.rev state.layout with
1971 | [] -> state.y
1972 | l :: _ -> getpagey l.pageno
1973 else
1974 clamp conf.winh
1975 | Glut.KEY_HOME -> addnav (); 0
1976 | Glut.KEY_END ->
1977 addnav ();
1978 state.maxy - (if conf.maxhfit then conf.winh else 0)
1980 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1981 state.x <- state.x - 10;
1982 state.y
1983 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1984 state.x <- state.x + 10;
1985 state.y
1987 | _ -> state.y
1989 gotoy_and_clear_text y
1991 | Textentry
1992 ((c, s, (Some (action, _) as onhist), onkey, ondone), mode) ->
1993 let s =
1994 match key with
1995 | Glut.KEY_UP -> action HCprev
1996 | Glut.KEY_DOWN -> action HCnext
1997 | Glut.KEY_HOME -> action HCfirst
1998 | Glut.KEY_END -> action HClast
1999 | _ -> state.text
2001 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2002 Glut.postRedisplay ()
2004 | Textentry _ -> ()
2006 | Outline (allowdel, active, first, outlines, qsearch) ->
2007 let maxrows = maxoutlinerows () in
2008 let calcfirst first active =
2009 if active > first
2010 then
2011 let rows = active - first in
2012 if rows > maxrows then active - maxrows else first
2013 else active
2015 let navigate incr =
2016 let active = active + incr in
2017 let active = max 0 (min active (Array.length outlines - 1)) in
2018 let first = calcfirst first active in
2019 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2020 Glut.postRedisplay ()
2022 let updownlevel incr =
2023 let len = Array.length outlines in
2024 let (_, curlevel, _, _) = outlines.(active) in
2025 let rec flow i =
2026 if i = len then i-1 else if i = -1 then 0 else
2027 let (_, l, _, _) = outlines.(i) in
2028 if l != curlevel then i else flow (i+incr)
2030 let active = flow active in
2031 let first = calcfirst first active in
2032 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2033 Glut.postRedisplay ()
2035 match key with
2036 | Glut.KEY_UP -> navigate ~-1
2037 | Glut.KEY_DOWN -> navigate 1
2038 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2039 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2041 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
2042 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
2044 | Glut.KEY_HOME ->
2045 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
2046 Glut.postRedisplay ()
2048 | Glut.KEY_END ->
2049 let active = Array.length outlines - 1 in
2050 let first = max 0 (active - maxrows) in
2051 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2052 Glut.postRedisplay ()
2054 | _ -> ()
2057 let drawplaceholder l =
2058 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
2059 GlDraw.rect
2060 (float l.pagex, float l.pagedispy)
2061 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2063 let x = float (if margin < 0 then -margin else l.pagex)
2064 and y = float (l.pagedispy + 13) in
2065 let font = Glut.BITMAP_8_BY_13 in
2066 GlDraw.color (0.0, 0.0, 0.0);
2067 GlPix.raster_pos ~x ~y ();
2068 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2069 ("Loading " ^ string_of_int (l.pageno + 1));
2072 let now () = Unix.gettimeofday ();;
2074 let drawpage l =
2075 let color =
2076 match state.mode with
2077 | Textentry _ -> scalecolor 0.4
2078 | View | Outline _ -> scalecolor 1.0
2079 | Birdseye (_, _, pageno, hooverpageno, _) ->
2080 if l.pageno = pageno
2081 then scalecolor 1.0
2082 else (
2083 if l.pageno = hooverpageno
2084 then scalecolor 0.9
2085 else scalecolor 0.8
2088 GlDraw.color color;
2089 begin match getopaque l.pageno with
2090 | Some (opaque, _) when validopaque opaque ->
2091 let a = now () in
2092 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2093 opaque;
2094 let b = now () in
2095 let d = b-.a in
2096 vlog "draw %d %f sec" l.pageno d;
2098 | _ ->
2099 drawplaceholder l;
2100 end;
2103 let scrollph y =
2104 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2105 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2106 let sh = float conf.winh /. sh in
2107 let sh = max sh (float conf.scrollh) in
2109 let percent =
2110 if state.y = state.maxy
2111 then 1.0
2112 else float y /. float maxy
2114 let position = (float conf.winh -. sh) *. percent in
2116 let position =
2117 if position +. sh > float conf.winh
2118 then float conf.winh -. sh
2119 else position
2121 position, sh;
2124 let scrollindicator () =
2125 GlDraw.color (0.64 , 0.64, 0.64);
2126 GlDraw.rect
2127 (float (conf.winw - conf.scrollw), 0.)
2128 (float conf.winw, float conf.winh)
2130 GlDraw.color (0.0, 0.0, 0.0);
2132 let position, sh = scrollph state.y in
2133 GlDraw.rect
2134 (float (conf.winw - conf.scrollw), position)
2135 (float conf.winw, position +. sh)
2139 let showsel margin =
2140 match state.mstate with
2141 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2144 | Msel ((x0, y0), (x1, y1)) ->
2145 let rec loop = function
2146 | l :: ls ->
2147 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2148 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2149 then
2150 match getopaque l.pageno with
2151 | Some (opaque, _) when validopaque opaque ->
2152 let oy = -l.pagey + l.pagedispy in
2153 seltext opaque
2154 (x0 - margin - state.x, y0,
2155 x1 - margin - state.x, y1) oy;
2157 | _ -> ()
2158 else loop ls
2159 | [] -> ()
2161 loop state.layout
2164 let showrects () =
2165 let panx = float state.x in
2166 Gl.enable `blend;
2167 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2168 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2169 List.iter
2170 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2171 List.iter (fun l ->
2172 if l.pageno = pageno
2173 then (
2174 let d = float (l.pagedispy - l.pagey) in
2175 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2176 GlDraw.begins `quads;
2178 GlDraw.vertex2 (x0+.panx, y0+.d);
2179 GlDraw.vertex2 (x1+.panx, y1+.d);
2180 GlDraw.vertex2 (x2+.panx, y2+.d);
2181 GlDraw.vertex2 (x3+.panx, y3+.d);
2183 GlDraw.ends ();
2185 ) state.layout
2186 ) state.rects
2188 Gl.disable `blend;
2191 let showoutline () =
2192 match state.mode with
2193 | Outline (allowdel, active, first, outlines, qsearch) ->
2194 Gl.enable `blend;
2195 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2196 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2197 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2198 Gl.disable `blend;
2200 GlDraw.color (1., 1., 1.);
2201 let font = Glut.BITMAP_9_BY_15 in
2202 let draw_string x y s =
2203 GlPix.raster_pos ~x ~y ();
2204 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2206 let rec loop row =
2207 if row = Array.length outlines || (row - first) * 16 > conf.winh
2208 then ()
2209 else (
2210 let (s, l, _, _) = outlines.(row) in
2211 let y = (row - first) * 16 in
2212 let x = 5 + 15*l in
2213 if row = active
2214 then (
2215 Gl.enable `blend;
2216 GlDraw.polygon_mode `both `line;
2217 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2218 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2219 GlDraw.rect (0., float (y + 1))
2220 (float (conf.winw - 1), float (y + 18));
2221 GlDraw.polygon_mode `both `fill;
2222 Gl.disable `blend;
2223 GlDraw.color (1., 1., 1.);
2225 draw_string (float x) (float (y + 16)) s;
2226 loop (row+1)
2229 loop first
2231 | _ -> ()
2234 let display () =
2235 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2236 GlDraw.viewport margin 0 state.w conf.winh;
2237 pagematrix ();
2238 GlClear.color (scalecolor 0.5);
2239 GlClear.clear [`color];
2240 if conf.zoom > 1.0
2241 then (
2242 Gl.enable `scissor_test;
2243 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2245 List.iter drawpage state.layout;
2246 if conf.zoom > 1.0
2247 then
2248 Gl.disable `scissor_test
2250 if state.x != 0
2251 then (
2252 let x = -.float state.x in
2253 GlMat.translate ~x ();
2255 showrects ();
2256 showsel margin;
2257 GlDraw.viewport 0 0 conf.winw conf.winh;
2258 winmatrix ();
2259 scrollindicator ();
2260 showoutline ();
2261 enttext ();
2262 Glut.swapBuffers ();
2265 let getunder x y =
2266 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2267 let x = x - margin - state.x in
2268 let rec f = function
2269 | l :: rest ->
2270 begin match getopaque l.pageno with
2271 | Some (opaque, _) when validopaque opaque ->
2272 let y = y - l.pagedispy in
2273 if y > 0
2274 then
2275 let y = l.pagey + y in
2276 let x = x - l.pagex in
2277 match whatsunder opaque x y with
2278 | Unone -> f rest
2279 | under -> under
2280 else
2281 f rest
2282 | _ ->
2283 f rest
2285 | [] -> Unone
2287 f state.layout
2290 let viewmouse button bstate x y =
2291 match button with
2292 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2293 if Glut.getModifiers () land Glut.active_ctrl != 0
2294 then (
2295 match state.mstate with
2296 | Mzoom (oldn, i) ->
2297 if oldn = n
2298 then (
2299 if i = 2
2300 then
2301 let incr =
2302 match n with
2303 | 4 ->
2304 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2305 | _ ->
2306 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2308 let zoom = conf.zoom +. incr in
2309 setzoom zoom;
2310 state.mstate <- Mzoom (n, 0);
2311 else
2312 state.mstate <- Mzoom (n, i+1);
2314 else state.mstate <- Mzoom (n, 0)
2316 | _ -> state.mstate <- Mzoom (n, 0)
2318 else (
2319 if state.ascrollstep > 0
2320 then
2321 setautoscrollspeed (n=4)
2322 else
2323 let incr =
2324 if n = 3
2325 then -conf.scrollstep
2326 else conf.scrollstep
2328 let incr = incr * 2 in
2329 let y = clamp incr in
2330 gotoy_and_clear_text y
2333 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2334 if bstate = Glut.DOWN
2335 then (
2336 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2337 state.mstate <- Mpan (x, y)
2339 else
2340 state.mstate <- Mnone
2342 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2343 if bstate = Glut.DOWN
2344 then
2345 let position, sh = scrollph state.y in
2346 if y > truncate position && y < truncate (position +. sh)
2347 then
2348 state.mstate <- Mscroll
2349 else
2350 let percent = float y /. float conf.winh in
2351 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2352 gotoy desty;
2353 state.mstate <- Mscroll
2354 else
2355 state.mstate <- Mnone
2357 | Glut.LEFT_BUTTON ->
2358 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2359 begin match dest with
2360 | Ulinkgoto (pageno, top) ->
2361 if pageno >= 0
2362 then (
2363 addnav ();
2364 gotopage1 pageno top;
2367 | Ulinkuri s ->
2368 print_endline s
2370 | Unone when bstate = Glut.DOWN ->
2371 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2372 state.mstate <- Mpan (x, y);
2374 | Unone | Utext _ ->
2375 if bstate = Glut.DOWN
2376 then (
2377 if conf.angle mod 360 = 0
2378 then (
2379 state.mstate <- Msel ((x, y), (x, y));
2380 Glut.postRedisplay ()
2383 else (
2384 match state.mstate with
2385 | Mnone -> ()
2387 | Mzoom _ | Mscroll ->
2388 state.mstate <- Mnone
2390 | Mpan _ ->
2391 Glut.setCursor Glut.CURSOR_INHERIT;
2392 state.mstate <- Mnone
2394 | Msel ((x0, y0), (x1, y1)) ->
2395 let f l =
2396 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2397 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2398 then
2399 match getopaque l.pageno with
2400 | Some (opaque, _) when validopaque opaque ->
2401 copysel opaque
2402 | _ -> ()
2404 List.iter f state.layout;
2405 copysel ""; (* ugly *)
2406 Glut.setCursor Glut.CURSOR_INHERIT;
2407 state.mstate <- Mnone;
2411 | _ -> ()
2414 let birdseyemouse button bstate x y
2415 (conf, leftx, pageno, hooverpageno, anchor) =
2416 match button with
2417 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2418 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2419 let rec loop = function
2420 | [] -> ()
2421 | l :: rest ->
2422 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2423 && x > margin && x < margin + l.pagew
2424 then (
2425 birdseyeoff (conf, leftx, l.pageno, hooverpageno, anchor) false;
2427 else loop rest
2429 loop state.layout
2430 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
2431 | _ -> ()
2434 let mouse bstate button x y =
2435 match state.mode with
2436 | View -> viewmouse button bstate x y
2437 | Birdseye beye -> birdseyemouse button bstate x y beye
2438 | Textentry _ -> ()
2439 | Outline _ -> ()
2442 let mouse ~button ~state ~x ~y = mouse state button x y;;
2444 let motion ~x ~y =
2445 match state.mode with
2446 | Outline _ -> ()
2447 | _ ->
2448 match state.mstate with
2449 | Mzoom _ | Mnone -> ()
2451 | Mpan (x0, y0) ->
2452 let dx = x - x0
2453 and dy = y0 - y in
2454 state.mstate <- Mpan (x, y);
2455 if conf.zoom > 1.0 then state.x <- state.x + dx;
2456 let y = clamp dy in
2457 gotoy_and_clear_text y
2459 | Msel (a, _) ->
2460 state.mstate <- Msel (a, (x, y));
2461 Glut.postRedisplay ()
2463 | Mscroll ->
2464 let y = min conf.winh (max 0 y) in
2465 let percent = float y /. float conf.winh in
2466 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2467 gotoy_and_clear_text y
2470 let pmotion ~x ~y =
2471 match state.mode with
2472 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
2473 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2474 let rec loop = function
2475 | [] ->
2476 if hooverpageno != -1
2477 then (
2478 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
2479 Glut.postRedisplay ();
2481 | l :: rest ->
2482 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2483 && x > margin && x < margin + l.pagew
2484 then (
2485 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
2486 Glut.postRedisplay ();
2488 else loop rest
2490 loop state.layout
2492 | Outline _ -> ()
2493 | _ ->
2494 match state.mstate with
2495 | Mnone ->
2496 begin match getunder x y with
2497 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2498 | Ulinkuri uri ->
2499 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2500 Glut.setCursor Glut.CURSOR_INFO
2501 | Ulinkgoto (page, y) ->
2502 if conf.underinfo
2503 then showtext 'p' ("age: " ^ string_of_int page);
2504 Glut.setCursor Glut.CURSOR_INFO
2505 | Utext s ->
2506 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2507 Glut.setCursor Glut.CURSOR_TEXT
2510 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
2515 module State =
2516 struct
2517 open Parser
2519 let home =
2521 match Sys.os_type with
2522 | "Win32" -> Sys.getenv "HOMEPATH"
2523 | _ -> Sys.getenv "HOME"
2524 with exn ->
2525 prerr_endline
2526 ("Can not determine home directory location: " ^
2527 Printexc.to_string exn);
2531 let config_of c attrs =
2532 let apply c k v =
2534 match k with
2535 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2536 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2537 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2538 | "preload" -> { c with preload = bool_of_string v }
2539 | "page-bias" -> { c with pagebias = int_of_string v }
2540 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
2541 | "auto-scroll-step" ->
2542 { c with autoscrollstep = max 0 (int_of_string v) }
2543 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2544 | "crop-hack" -> { c with crophack = bool_of_string v }
2545 | "throttle" -> { c with showall = bool_of_string v }
2546 | "highlight-links" -> { c with hlinks = bool_of_string v }
2547 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2548 | "vertical-margin" ->
2549 { c with interpagespace = max 0 (int_of_string v) }
2550 | "zoom" ->
2551 let zoom = float_of_string v /. 100. in
2552 let zoom = max 0.01 (min 2.2 zoom) in
2553 { c with zoom = zoom }
2554 | "presentation" -> { c with presentation = bool_of_string v }
2555 | "rotation-angle" -> { c with angle = int_of_string v }
2556 | "width" -> { c with winw = max 20 (int_of_string v) }
2557 | "height" -> { c with winh = max 20 (int_of_string v) }
2558 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2559 | "proportional-display" -> { c with proportional = bool_of_string v }
2560 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2561 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2562 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2563 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2564 | _ -> c
2565 with exn ->
2566 prerr_endline ("Error processing attribute (`" ^
2567 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2570 let rec fold c = function
2571 | [] -> c
2572 | (k, v) :: rest ->
2573 let c = apply c k v in
2574 fold c rest
2576 fold c attrs;
2579 let fromstring f pos n v d =
2580 try f v
2581 with exn ->
2582 dolog "Error processing attribute (%S=%S) at %d\n%s"
2583 n v pos (Printexc.to_string exn)
2588 let bookmark_of attrs =
2589 let rec fold title page rely = function
2590 | ("title", v) :: rest -> fold v page rely rest
2591 | ("page", v) :: rest -> fold title v rely rest
2592 | ("rely", v) :: rest -> fold title page v rest
2593 | _ :: rest -> fold title page rely rest
2594 | [] -> title, page, rely
2596 fold "invalid" "0" "0" attrs
2599 let doc_of attrs =
2600 let rec fold path page rely pan = function
2601 | ("path", v) :: rest -> fold v page rely pan rest
2602 | ("page", v) :: rest -> fold path v rely pan rest
2603 | ("rely", v) :: rest -> fold path page v pan rest
2604 | ("pan", v) :: rest -> fold path page rely v rest
2605 | _ :: rest -> fold path page rely pan rest
2606 | [] -> path, page, rely, pan
2608 fold "" "0" "0" "0" attrs
2611 let setconf dst src =
2612 dst.scrollw <- src.scrollw;
2613 dst.scrollh <- src.scrollh;
2614 dst.icase <- src.icase;
2615 dst.preload <- src.preload;
2616 dst.pagebias <- src.pagebias;
2617 dst.verbose <- src.verbose;
2618 dst.scrollstep <- src.scrollstep;
2619 dst.maxhfit <- src.maxhfit;
2620 dst.crophack <- src.crophack;
2621 dst.autoscrollstep <- src.autoscrollstep;
2622 dst.showall <- src.showall;
2623 dst.hlinks <- src.hlinks;
2624 dst.underinfo <- src.underinfo;
2625 dst.interpagespace <- src.interpagespace;
2626 dst.zoom <- src.zoom;
2627 dst.presentation <- src.presentation;
2628 dst.angle <- src.angle;
2629 dst.winw <- src.winw;
2630 dst.winh <- src.winh;
2631 dst.savebmarks <- src.savebmarks;
2632 dst.memlimit <- src.memlimit;
2633 dst.proportional <- src.proportional;
2634 dst.texcount <- src.texcount;
2635 dst.sliceheight <- src.sliceheight;
2636 dst.thumbw <- src.thumbw;
2639 let unent s =
2640 let l = String.length s in
2641 let b = Buffer.create l in
2642 unent b s 0 l;
2643 Buffer.contents b;
2646 let get s =
2647 let h = Hashtbl.create 10 in
2648 let dc = { defconf with angle = defconf.angle } in
2649 let rec toplevel v t spos epos =
2650 match t with
2651 | Vdata | Vcdata | Vend -> v
2652 | Vopen ("llppconfig", attrs, closed) ->
2653 if closed
2654 then v
2655 else { v with f = llppconfig }
2656 | Vopen _ ->
2657 error "unexpected subelement at top level" s spos
2658 | Vclose tag -> error "unexpected close at top level" s spos
2660 and llppconfig v t spos epos =
2661 match t with
2662 | Vdata | Vcdata | Vend -> v
2663 | Vopen ("defaults", attrs, closed) ->
2664 let c = config_of dc attrs in
2665 setconf dc c;
2666 if closed
2667 then v
2668 else { v with f = skip "defaults" (fun () -> v) }
2670 | Vopen ("doc", attrs, closed) ->
2671 let pathent, spage, srely, span = doc_of attrs in
2672 let path = unent pathent
2673 and pageno = fromstring int_of_string spos "page" spage 0
2674 and rely = fromstring float_of_string spos "rely" srely 0.0
2675 and pan = fromstring int_of_string spos "pan" span 0 in
2676 let c = config_of dc attrs in
2677 let anchor = (pageno, rely) in
2678 if closed
2679 then (Hashtbl.add h path (c, [], pan, anchor); v)
2680 else { v with f = doc path pan anchor c [] }
2682 | Vopen (tag, _, closed) ->
2683 error "unexpected subelement in llppconfig" s spos
2685 | Vclose "llppconfig" -> { v with f = toplevel }
2686 | Vclose tag -> error "unexpected close in llppconfig" s spos
2688 and doc path pan anchor c bookmarks v t spos epos =
2689 match t with
2690 | Vdata | Vcdata -> v
2691 | Vend -> error "unexpected end of input in doc" s spos
2692 | Vopen ("bookmarks", attrs, closed) ->
2693 { v with f = pbookmarks path pan anchor c bookmarks }
2695 | Vopen (tag, _, _) ->
2696 error "unexpected subelement in doc" s spos
2698 | Vclose "doc" ->
2699 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
2700 { v with f = llppconfig }
2702 | Vclose tag -> error "unexpected close in doc" s spos
2704 and pbookmarks path pan anchor c bookmarks v t spos epos =
2705 match t with
2706 | Vdata | Vcdata -> v
2707 | Vend -> error "unexpected end of input in bookmarks" s spos
2708 | Vopen ("item", attrs, closed) ->
2709 let titleent, spage, srely = bookmark_of attrs in
2710 let page = fromstring int_of_string spos "page" spage 0
2711 and rely = fromstring float_of_string spos "rely" srely 0.0 in
2712 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2713 if closed
2714 then { v with f = pbookmarks path pan anchor c bookmarks }
2715 else
2716 let f () = v in
2717 { v with f = skip "item" f }
2719 | Vopen _ ->
2720 error "unexpected subelement in bookmarks" s spos
2722 | Vclose "bookmarks" ->
2723 { v with f = doc path pan anchor c bookmarks }
2725 | Vclose tag -> error "unexpected close in bookmarks" s spos
2727 and skip tag f v t spos epos =
2728 match t with
2729 | Vdata | Vcdata -> v
2730 | Vend ->
2731 error ("unexpected end of input in skipped " ^ tag) s spos
2732 | Vopen (tag', _, closed) ->
2733 if closed
2734 then v
2735 else
2736 let f' () = { v with f = skip tag f } in
2737 { v with f = skip tag' f' }
2738 | Vclose ctag ->
2739 if tag = ctag
2740 then f ()
2741 else error ("unexpected close in skipped " ^ tag) s spos
2744 parse { f = toplevel; accu = () } s;
2745 h, dc;
2748 let do_load f ic =
2750 let len = in_channel_length ic in
2751 let s = String.create len in
2752 really_input ic s 0 len;
2753 f s;
2754 with
2755 | Parse_error (msg, s, pos) ->
2756 let subs = subs s pos in
2757 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2758 failwith ("parse error: " ^ s)
2760 | exn ->
2761 failwith ("config load error: " ^ Printexc.to_string exn)
2764 let path =
2765 let dir =
2767 let dir = Filename.concat home ".config" in
2768 if Sys.is_directory dir then dir else home
2769 with _ -> home
2771 Filename.concat dir "llpp.conf"
2774 let load1 f =
2775 if Sys.file_exists path
2776 then
2777 match
2778 (try Some (open_in_bin path)
2779 with exn ->
2780 prerr_endline
2781 ("Error opening configuation file `" ^ path ^ "': " ^
2782 Printexc.to_string exn);
2783 None
2785 with
2786 | Some ic ->
2787 begin try
2788 f (do_load get ic)
2789 with exn ->
2790 prerr_endline
2791 ("Error loading configuation from `" ^ path ^ "': " ^
2792 Printexc.to_string exn);
2793 end;
2794 close_in ic;
2796 | None -> ()
2797 else
2798 f (Hashtbl.create 0, defconf)
2801 let load () =
2802 let f (h, dc) =
2803 let pc, pb, px, pa =
2805 Hashtbl.find h (Filename.basename state.path)
2806 with Not_found -> dc, [], 0, (0, 0.0)
2808 setconf defconf dc;
2809 setconf conf pc;
2810 state.bookmarks <- pb;
2811 state.x <- px;
2812 cbput state.hists.nav pa;
2814 load1 f
2817 let add_attrs bb always dc c =
2818 let ob s a b =
2819 if always || a != b
2820 then Printf.bprintf bb "\n %s='%b'" s a
2821 and oi s a b =
2822 if always || a != b
2823 then Printf.bprintf bb "\n %s='%d'" s a
2824 and oz s a b =
2825 if always || a <> b
2826 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2828 let w, h =
2829 if always
2830 then dc.winw, dc.winh
2831 else
2832 match state.fullscreen with
2833 | Some wh -> wh
2834 | None -> c.winw, c.winh
2836 let zoom, presentation, interpagespace, showall=
2837 if always
2838 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2839 else
2840 match state.mode with
2841 | Birdseye (bc, _, _, _, _) ->
2842 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2843 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2845 oi "width" w dc.winw;
2846 oi "height" h dc.winh;
2847 oi "scroll-bar-width" c.scrollw dc.scrollw;
2848 oi "scroll-handle-height" c.scrollh dc.scrollh;
2849 ob "case-insensitive-search" c.icase dc.icase;
2850 ob "preload" c.preload dc.preload;
2851 oi "page-bias" c.pagebias dc.pagebias;
2852 oi "scroll-step" c.scrollstep dc.scrollstep;
2853 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
2854 ob "max-height-fit" c.maxhfit dc.maxhfit;
2855 ob "crop-hack" c.crophack dc.crophack;
2856 ob "throttle" showall dc.showall;
2857 ob "highlight-links" c.hlinks dc.hlinks;
2858 ob "under-cursor-info" c.underinfo dc.underinfo;
2859 oi "vertical-margin" interpagespace dc.interpagespace;
2860 oz "zoom" zoom dc.zoom;
2861 ob "presentation" presentation dc.presentation;
2862 oi "rotation-angle" c.angle dc.angle;
2863 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2864 ob "proportional-display" c.proportional dc.proportional;
2865 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2866 oi "texcount" c.texcount dc.texcount;
2867 oi "slice-height" c.sliceheight dc.sliceheight;
2868 oi "thumbnail-width" c.thumbw dc.thumbw;
2871 let save () =
2872 let bb = Buffer.create 32768 in
2873 let f (h, dc) =
2874 Buffer.add_string bb "<llppconfig>\n<defaults ";
2875 add_attrs bb true dc dc;
2876 Buffer.add_string bb "/>\n";
2878 let adddoc path pan anchor c bookmarks =
2879 if bookmarks == [] && c = dc && anchor = emptyanchor
2880 then ()
2881 else (
2882 Printf.bprintf bb "<doc path='%s'"
2883 (enent path 0 (String.length path));
2885 if anchor <> emptyanchor
2886 then (
2887 let n, y = anchor in
2888 Printf.bprintf bb " page='%d'" n;
2889 Printf.bprintf bb " rely='%f'" y;
2892 if pan != 0
2893 then Printf.bprintf bb " pan='%d'" pan;
2895 add_attrs bb false dc c;
2897 begin match bookmarks with
2898 | [] -> Buffer.add_string bb "/>\n"
2899 | _ ->
2900 Buffer.add_string bb ">\n<bookmarks>\n";
2901 List.iter (fun (title, _level, page, rely) ->
2902 Printf.bprintf bb
2903 "<item title='%s' page='%d' rely='%f'/>\n"
2904 (enent title 0 (String.length title))
2905 page
2906 rely
2907 ) bookmarks;
2908 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2909 end;
2913 let pan =
2914 match state.mode with
2915 | Birdseye (_, pan, _, _, _) -> pan
2916 | _ -> state.x
2918 let basename = Filename.basename state.path in
2919 adddoc basename pan (getanchor ())
2920 { conf with
2921 autoscrollstep =
2922 if state.ascrollstep > 0
2923 then state.ascrollstep
2924 else conf.autoscrollstep }
2925 (if conf.savebmarks then state.bookmarks else []);
2927 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2928 if basename <> path
2929 then adddoc path x y c bookmarks
2930 ) h;
2931 Buffer.add_string bb "</llppconfig>";
2933 load1 f;
2934 if Buffer.length bb > 0
2935 then
2937 let tmp = path ^ ".tmp" in
2938 let oc = open_out_bin tmp in
2939 Buffer.output_buffer oc bb;
2940 close_out oc;
2941 Sys.rename tmp path;
2942 with exn ->
2943 prerr_endline
2944 ("error while saving configuration: " ^ Printexc.to_string exn)
2946 end;;
2948 let () =
2949 Arg.parse
2950 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2951 (fun s -> state.path <- s)
2952 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2954 if String.length state.path = 0
2955 then (prerr_endline "filename missing"; exit 1);
2957 State.load ();
2959 let _ = Glut.init Sys.argv in
2960 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2961 let () = Glut.initWindowSize conf.winw conf.winh in
2962 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
2964 let csock, ssock =
2965 if Sys.os_type = "Unix"
2966 then
2967 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2968 else
2969 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2970 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2971 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2972 Unix.bind sock addr;
2973 Unix.listen sock 1;
2974 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2975 Unix.connect csock addr;
2976 let ssock, _ = Unix.accept sock in
2977 Unix.close sock;
2978 let opts sock =
2979 Unix.setsockopt sock Unix.TCP_NODELAY true;
2980 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2982 opts ssock;
2983 opts csock;
2984 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2985 ssock, csock
2988 let () = Glut.displayFunc display in
2989 let () = Glut.reshapeFunc reshape in
2990 let () = Glut.keyboardFunc keyboard in
2991 let () = Glut.specialFunc special in
2992 let () = Glut.idleFunc (Some idle) in
2993 let () = Glut.mouseFunc mouse in
2994 let () = Glut.motionFunc motion in
2995 let () = Glut.passiveMotionFunc pmotion in
2997 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2998 state.csock <- csock;
2999 state.ssock <- ssock;
3000 state.text <- "Opening " ^ state.path;
3001 writeopen state.path state.password;
3003 at_exit State.save;
3005 let rec handlelablglutbug () =
3007 Glut.mainLoop ();
3008 with Glut.BadEnum "key in special_of_int" ->
3009 showtext '!' " LablGlut bug: special key not recognized";
3010 handlelablglutbug ()
3012 handlelablglutbug ();