Make autoscroll saving/loading behavior saner
[llpp.git] / main.ml
blob2a27af1261e59da10280bbdee119bf50ca2ff72e
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
10 type params = angle * proportional * texcount * sliceheight
11 and pageno = int
12 and width = int
13 and height = int
14 and leftx = int
15 and opaque = string
16 and recttype = int
17 and pixmapsize = int
18 and angle = int
19 and proportional = bool
20 and interpagespace = int
21 and texcount = int
22 and sliceheight = int
23 and gen = int
24 and top = float
27 external init : Unix.file_descr -> params -> unit = "ml_init";;
28 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
29 external seltext : string -> (int * int * int * int) -> int -> unit =
30 "ml_seltext";;
31 external copysel : string -> unit = "ml_copysel";;
32 external getpdimrect : int -> float array = "ml_getpdimrect";;
33 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
34 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 type mpos = int * int
37 and mstate =
38 | Msel of (mpos * mpos)
39 | Mpan of mpos
40 | Mscroll
41 | Mnone
44 type textentry = (char * string * onhist * onkey * ondone)
45 and onkey = string -> int -> te
46 and ondone = string -> unit
47 and histcancel = unit -> unit
48 and onhist = ((histcmd -> string) * histcancel) option
49 and histcmd = HCnext | HCprev | HCfirst | HClast
50 and te =
51 | TEstop
52 | TEdone of string
53 | TEcont of string
54 | TEswitch of textentry
57 type 'a circbuf =
58 { store : 'a array
59 ; mutable rc : int
60 ; mutable wc : int
61 ; mutable len : int
65 let cbnew n v =
66 { store = Array.create n v
67 ; rc = 0
68 ; wc = 0
69 ; len = 0
73 let cbcap b = Array.length b.store;;
75 let cbput b v =
76 let cap = cbcap b in
77 b.store.(b.wc) <- v;
78 b.wc <- (b.wc + 1) mod cap;
79 b.rc <- b.wc;
80 b.len <- min (b.len + 1) cap;
83 let cbempty b = b.len = 0;;
85 let cbgetg b circular dir =
86 if cbempty b
87 then b.store.(0)
88 else
89 let rc = b.rc + dir in
90 let rc =
91 if circular
92 then (
93 if rc = -1
94 then b.len-1
95 else (
96 if rc = b.len
97 then 0
98 else rc
101 else max 0 (min rc (b.len-1))
103 b.rc <- rc;
104 b.store.(rc);
107 let cbget b = cbgetg b false;;
108 let cbgetc b = cbgetg b true;;
110 let cbpeek b =
111 let rc = b.wc - b.len in
112 let rc = if rc < 0 then cbcap b + rc else rc in
113 b.store.(rc);
116 let cbdecr b = b.len <- b.len - 1;;
118 type layout =
119 { pageno : int
120 ; pagedimno : int
121 ; pagew : int
122 ; pageh : int
123 ; pagedispy : int
124 ; pagey : int
125 ; pagevh : int
126 ; pagex : int
130 type conf =
131 { mutable scrollw : int
132 ; mutable scrollh : int
133 ; mutable icase : bool
134 ; mutable preload : bool
135 ; mutable pagebias : int
136 ; mutable verbose : bool
137 ; mutable scrollstep : int
138 ; mutable maxhfit : bool
139 ; mutable crophack : bool
140 ; mutable autoscrollstep : int
141 ; mutable showall : bool
142 ; mutable hlinks : bool
143 ; mutable underinfo : bool
144 ; mutable interpagespace : interpagespace
145 ; mutable zoom : float
146 ; mutable presentation : bool
147 ; mutable angle : angle
148 ; mutable winw : int
149 ; mutable winh : int
150 ; mutable savebmarks : bool
151 ; mutable proportional : proportional
152 ; mutable memlimit : int
153 ; mutable texcount : texcount
154 ; mutable sliceheight : sliceheight
155 ; mutable thumbw : width
159 type outline = string * int * int * float;;
160 type outlines =
161 | Oarray of outline array
162 | Olist of outline list
163 | Onarrow of string * outline array * outline array
166 type rect = (float * float * float * float * float * float * float * float);;
168 type pagemapkey = (pageno * width * angle * proportional * gen);;
170 type anchor = pageno * top;;
172 type mode =
173 | Birdseye of (conf * leftx * pageno * pageno * anchor)
174 | Outline of (bool * int * int * outline array * string)
175 | Textentry of (textentry * mode)
176 | View
179 let isbirdseye = function Birdseye _ -> true | _ -> false;;
180 let istextentry = function Textentry _ -> true | _ -> false;;
182 type state =
183 { mutable csock : Unix.file_descr
184 ; mutable ssock : Unix.file_descr
185 ; mutable w : int
186 ; mutable x : int
187 ; mutable y : int
188 ; mutable anchor : anchor
189 ; mutable maxy : int
190 ; mutable layout : layout list
191 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
192 ; mutable pdims : (pageno * width * height * leftx) list
193 ; mutable pagecount : int
194 ; pagecache : string circbuf
195 ; mutable rendering : bool
196 ; mutable mstate : mstate
197 ; mutable searchpattern : string
198 ; mutable rects : (pageno * recttype * rect) list
199 ; mutable rects1 : (pageno * recttype * rect) list
200 ; mutable text : string
201 ; mutable fullscreen : (width * height) option
202 ; mutable mode : mode
203 ; mutable outlines : outlines
204 ; mutable bookmarks : outline list
205 ; mutable path : string
206 ; mutable password : string
207 ; mutable invalidated : int
208 ; mutable colorscale : float
209 ; mutable memused : int
210 ; mutable gen : gen
211 ; mutable throttle : layout list option
212 ; mutable ascrollstep : int
213 ; hists : hists
215 and hists =
216 { pat : string circbuf
217 ; pag : string circbuf
218 ; nav : anchor circbuf
222 let defconf =
223 { scrollw = 7
224 ; scrollh = 12
225 ; icase = true
226 ; preload = true
227 ; pagebias = 0
228 ; verbose = false
229 ; scrollstep = 24
230 ; maxhfit = true
231 ; crophack = false
232 ; autoscrollstep = 24
233 ; showall = false
234 ; hlinks = false
235 ; underinfo = false
236 ; interpagespace = 2
237 ; zoom = 1.0
238 ; presentation = false
239 ; angle = 0
240 ; winw = 900
241 ; winh = 900
242 ; savebmarks = true
243 ; proportional = true
244 ; memlimit = 32*1024*1024
245 ; texcount = 256
246 ; sliceheight = 24
247 ; thumbw = 76
251 let conf = { defconf with angle = defconf.angle };;
253 let state =
254 { csock = Unix.stdin
255 ; ssock = Unix.stdin
256 ; x = 0
257 ; y = 0
258 ; anchor = (0, 0.0)
259 ; w = 0
260 ; layout = []
261 ; maxy = max_int
262 ; pagemap = Hashtbl.create 10
263 ; pagecache = cbnew 100 ""
264 ; pdims = []
265 ; pagecount = 0
266 ; rendering = false
267 ; mstate = Mnone
268 ; rects = []
269 ; rects1 = []
270 ; text = ""
271 ; mode = View
272 ; fullscreen = None
273 ; searchpattern = ""
274 ; outlines = Olist []
275 ; bookmarks = []
276 ; path = ""
277 ; password = ""
278 ; invalidated = 0
279 ; hists =
280 { nav = cbnew 100 (0, 0.0)
281 ; pat = cbnew 20 ""
282 ; pag = cbnew 10 ""
284 ; colorscale = 1.0
285 ; memused = 0
286 ; gen = 0
287 ; throttle = None
288 ; ascrollstep = 0
292 let vlog fmt =
293 if conf.verbose
294 then
295 Printf.kprintf prerr_endline fmt
296 else
297 Printf.kprintf ignore fmt
300 let writecmd fd s =
301 let len = String.length s in
302 let n = 4 + len in
303 let b = Buffer.create n in
304 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
305 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
306 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
307 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
308 Buffer.add_string b s;
309 let s' = Buffer.contents b in
310 let n' = Unix.write fd s' 0 n in
311 if n' != n then failwith "write failed";
314 let readcmd fd =
315 let s = "xxxx" in
316 let n = Unix.read fd s 0 4 in
317 if n != 4 then failwith "incomplete read(len)";
318 let len = 0
319 lor (Char.code s.[0] lsl 24)
320 lor (Char.code s.[1] lsl 16)
321 lor (Char.code s.[2] lsl 8)
322 lor (Char.code s.[3] lsl 0)
324 let s = String.create len in
325 let n = Unix.read fd s 0 len in
326 if n != len then failwith "incomplete read(data)";
330 let makecmd s l =
331 let b = Buffer.create 10 in
332 Buffer.add_string b s;
333 let rec combine = function
334 | [] -> b
335 | x :: xs ->
336 Buffer.add_char b ' ';
337 let s =
338 match x with
339 | `b b -> if b then "1" else "0"
340 | `s s -> s
341 | `i i -> string_of_int i
342 | `f f -> string_of_float f
343 | `I f -> string_of_int (truncate f)
345 Buffer.add_string b s;
346 combine xs;
348 combine l;
351 let wcmd s l =
352 let cmd = Buffer.contents (makecmd s l) in
353 writecmd state.csock cmd;
356 let calcips h =
357 if conf.presentation
358 then
359 let d = conf.winh - h in
360 max 0 ((d + 1) / 2)
361 else
362 conf.interpagespace
365 let calcheight () =
366 let rec f pn ph pi fh l =
367 match l with
368 | (n, _, h, _) :: rest ->
369 let ips = calcips h in
370 let fh =
371 if conf.presentation
372 then fh+ips
373 else (
374 if isbirdseye state.mode && pn = 0
375 then fh + ips
376 else fh
379 let fh = fh + ((n - pn) * (ph + pi)) in
380 f n h ips fh rest;
382 | [] ->
383 let inc =
384 if conf.presentation || (isbirdseye state.mode && pn = 0)
385 then 0
386 else -pi
388 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
389 max 0 fh
391 let fh = f 0 0 0 0 state.pdims in
395 let getpageyh pageno =
396 let rec f pn ph pi y l =
397 match l with
398 | (n, _, h, _) :: rest ->
399 let ips = calcips h in
400 if n >= pageno
401 then
402 let h = if n = pageno then h else ph in
403 if conf.presentation && n = pageno
404 then
405 y + (pageno - pn) * (ph + pi) + pi, h
406 else
407 y + (pageno - pn) * (ph + pi), h
408 else
409 let y = y + (if conf.presentation then pi else 0) in
410 let y = y + (n - pn) * (ph + pi) in
411 f n h ips y rest
413 | [] ->
414 y + (pageno - pn) * (ph + pi), ph
416 f 0 0 0 0 state.pdims
419 let getpagey pageno = fst (getpageyh pageno);;
421 let layout y sh =
422 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
423 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
424 match pdims with
425 | (pageno', w, h, x) :: rest when pageno' = pageno ->
426 let ips = calcips h in
427 let yinc =
428 if conf.presentation || (isbirdseye state.mode && pageno = 0)
429 then ips
430 else 0
432 (w, h, ips, x), rest, pdimno + 1, yinc
433 | _ ->
434 prev, pdims, pdimno, 0
436 let dy = dy + yinc in
437 let py = py + yinc in
438 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
439 then
440 accu
441 else
442 let vy = y + dy in
443 if py + h <= vy - yinc
444 then
445 let py = py + h + ips in
446 let dy = max 0 (py - y) in
447 f ~pageno:(pageno+1)
448 ~pdimno
449 ~prev:curr
452 ~pdims:rest
453 ~cacheleft
454 ~accu
455 else
456 let pagey = vy - py in
457 let pagevh = h - pagey in
458 let pagevh = min (sh - dy) pagevh in
459 let off = if yinc > 0 then py - vy else 0 in
460 let py = py + h + ips in
461 let e =
462 { pageno = pageno
463 ; pagedimno = pdimno
464 ; pagew = w
465 ; pageh = h
466 ; pagedispy = dy + off
467 ; pagey = pagey + off
468 ; pagevh = pagevh - off
469 ; pagex = x
472 let accu = e :: accu in
473 f ~pageno:(pageno+1)
474 ~pdimno
475 ~prev:curr
477 ~dy:(dy+pagevh+ips)
478 ~pdims:rest
479 ~cacheleft:(cacheleft-1)
480 ~accu
482 if state.invalidated = 0
483 then (
484 let accu =
486 ~pageno:0
487 ~pdimno:~-1
488 ~prev:(0,0,0,0)
489 ~py:0
490 ~dy:0
491 ~pdims:state.pdims
492 ~cacheleft:(cbcap state.pagecache)
493 ~accu:[]
495 List.rev accu
497 else
501 let clamp incr =
502 let y = state.y + incr in
503 let y = max 0 y in
504 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
508 let getopaque pageno =
509 try Some (Hashtbl.find state.pagemap
510 (pageno, state.w, conf.angle, conf.proportional, state.gen))
511 with Not_found -> None
514 let cache pageno opaque =
515 Hashtbl.replace state.pagemap
516 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
519 let validopaque opaque = String.length opaque > 0;;
521 let render l =
522 match getopaque l.pageno with
523 | None when not state.rendering ->
524 state.rendering <- true;
525 cache l.pageno ("", -1);
526 wcmd "render" [`i (l.pageno + 1)
527 ;`i l.pagedimno
528 ;`i l.pagew
529 ;`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
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 optentry text key =
1026 let btos b = if b then "on" else "off" in
1027 let c = Char.unsafe_chr key in
1028 match c with
1029 | 's' ->
1030 let ondone s =
1031 try conf.scrollstep <- int_of_string s with exc ->
1032 state.text <- Printf.sprintf "bad integer `%s': %s"
1033 s (Printexc.to_string exc)
1035 TEswitch ('#', "", None, intentry, ondone)
1037 | 'R' ->
1038 let ondone s =
1039 match try
1040 Some (int_of_string s)
1041 with exc ->
1042 state.text <- Printf.sprintf "bad integer `%s': %s"
1043 s (Printexc.to_string exc);
1044 None
1045 with
1046 | Some angle -> reinit angle conf.proportional
1047 | None -> ()
1049 TEswitch ('^', "", None, intentry, ondone)
1051 | 'i' ->
1052 conf.icase <- not conf.icase;
1053 TEdone ("case insensitive search " ^ (btos conf.icase))
1055 | 'p' ->
1056 conf.preload <- not conf.preload;
1057 gotoy state.y;
1058 TEdone ("preload " ^ (btos conf.preload))
1060 | 'v' ->
1061 conf.verbose <- not conf.verbose;
1062 TEdone ("verbose " ^ (btos conf.verbose))
1064 | 'h' ->
1065 conf.maxhfit <- not conf.maxhfit;
1066 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1067 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1069 | 'c' ->
1070 conf.crophack <- not conf.crophack;
1071 TEdone ("crophack " ^ btos conf.crophack)
1073 | 'a' ->
1074 conf.showall <- not conf.showall;
1075 TEdone ("showall " ^ btos conf.showall)
1077 | 'f' ->
1078 conf.underinfo <- not conf.underinfo;
1079 TEdone ("underinfo " ^ btos conf.underinfo)
1081 | 'P' ->
1082 conf.savebmarks <- not conf.savebmarks;
1083 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1085 | 'S' ->
1086 let ondone s =
1088 let pageno, py =
1089 match state.layout with
1090 | [] -> 0, 0
1091 | l :: _ ->
1092 l.pageno, l.pagey
1094 conf.interpagespace <- int_of_string s;
1095 state.maxy <- calcheight ();
1096 let y = getpagey pageno in
1097 gotoy (y + py)
1098 with exc ->
1099 state.text <- Printf.sprintf "bad integer `%s': %s"
1100 s (Printexc.to_string exc)
1102 TEswitch ('%', "", None, intentry, ondone)
1104 | 'l' ->
1105 reinit conf.angle (not conf.proportional);
1106 TEdone ("proprortional display " ^ btos conf.proportional)
1108 | _ ->
1109 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1110 TEstop
1113 let maxoutlinerows () = (conf.winh - 31) / 16;;
1115 let enterselector allowdel outlines errmsg msg =
1116 if Array.length outlines = 0
1117 then (
1118 showtext ' ' errmsg;
1120 else (
1121 state.text <- msg;
1122 Glut.setCursor Glut.CURSOR_INHERIT;
1123 let pageno =
1124 match state.layout with
1125 | [] -> -1
1126 | {pageno=pageno} :: rest -> pageno
1128 let active =
1129 let rec loop n =
1130 if n = Array.length outlines
1131 then 0
1132 else
1133 let (_, _, outlinepageno, _) = outlines.(n) in
1134 if outlinepageno >= pageno then n else loop (n+1)
1136 loop 0
1138 state.mode <- Outline
1139 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "");
1140 Glut.postRedisplay ();
1144 let enteroutlinemode () =
1145 let outlines, msg =
1146 match state.outlines with
1147 | Oarray a -> a, ""
1148 | Olist l ->
1149 let a = Array.of_list (List.rev l) in
1150 state.outlines <- Oarray a;
1151 a, ""
1152 | Onarrow (pat, a, b) ->
1153 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1155 enterselector false outlines "Document has no outline" msg;
1158 let enterbookmarkmode () =
1159 let bookmarks = Array.of_list state.bookmarks in
1160 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1163 let quickbookmark ?title () =
1164 match state.layout with
1165 | [] -> ()
1166 | l :: _ ->
1167 let title =
1168 match title with
1169 | None ->
1170 let sec = Unix.gettimeofday () in
1171 let tm = Unix.localtime sec in
1172 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1173 (l.pageno+1)
1174 tm.Unix.tm_mday
1175 tm.Unix.tm_mon
1176 (tm.Unix.tm_year + 1900)
1177 tm.Unix.tm_hour
1178 tm.Unix.tm_min
1179 | Some title -> title
1181 state.bookmarks <-
1182 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1185 let doreshape w h =
1186 state.fullscreen <- None;
1187 Glut.reshapeWindow w h;
1190 let writeopen path password =
1191 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1194 let opendoc path password =
1195 invalidate ();
1196 state.path <- path;
1197 state.password <- password;
1198 state.gen <- state.gen + 1;
1200 writeopen path password;
1201 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1202 wcmd "geometry" [`i state.w; `i conf.winh];
1205 let birdseyeon () =
1206 let zoom = float conf.thumbw /. float conf.winw in
1207 let birdseyepageno =
1208 let rec fold = function
1209 | [] -> 0
1210 | l :: _ when l.pagey = 0 -> l.pageno
1211 | _ :: rest -> fold rest
1213 fold state.layout
1215 state.mode <- Birdseye (
1216 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1218 conf.zoom <- zoom;
1219 conf.presentation <- false;
1220 conf.interpagespace <- 10;
1221 conf.hlinks <- false;
1222 state.x <- 0;
1223 state.mstate <- Mnone;
1224 conf.showall <- false;
1225 Glut.setCursor Glut.CURSOR_INHERIT;
1226 if conf.verbose
1227 then
1228 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1229 (100.0*.zoom)
1230 else
1231 state.text <- ""
1233 reshape conf.winw conf.winh;
1236 let birdseyeoff (c, leftx, pageno, _, anchor) goback =
1237 state.mode <- View;
1238 conf.zoom <- c.zoom;
1239 conf.presentation <- c.presentation;
1240 conf.interpagespace <- c.interpagespace;
1241 conf.showall <- c.showall;
1242 conf.hlinks <- c.hlinks;
1243 state.x <- leftx;
1244 if conf.verbose
1245 then
1246 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1247 (100.0*.conf.zoom)
1249 reshape conf.winw conf.winh;
1250 state.anchor <- if goback then anchor else (pageno, 0.0);
1253 let togglebirdseye () =
1254 match state.mode with
1255 | Birdseye vals -> birdseyeoff vals true
1256 | View | Outline _ -> birdseyeon ()
1257 | _ -> ()
1260 let viewkeyboard ~key ~x ~y =
1261 let enttext te =
1262 state.mode <- Textentry (te, state.mode);
1263 state.text <- "";
1264 enttext ();
1265 Glut.postRedisplay ()
1267 let c = Char.chr key in
1268 match c with
1269 | '\027' | 'q' ->
1270 exit 0
1272 | '\008' ->
1273 let y = getnav () in
1274 gotoy_and_clear_text y
1276 | 'o' ->
1277 enteroutlinemode ()
1279 | 'u' ->
1280 state.rects <- [];
1281 state.text <- "";
1282 Glut.postRedisplay ()
1284 | '/' | '?' ->
1285 let ondone isforw s =
1286 cbput state.hists.pat s;
1287 state.searchpattern <- s;
1288 search s isforw
1290 enttext (c, "", Some (onhist state.hists.pat),
1291 textentry, ondone (c ='/'))
1293 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1294 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1295 conf.zoom <- min 2.2 (conf.zoom +. incr);
1296 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1297 reshape conf.winw conf.winh
1299 | '+' ->
1300 let ondone s =
1301 let n =
1302 try int_of_string s with exc ->
1303 state.text <- Printf.sprintf "bad integer `%s': %s"
1304 s (Printexc.to_string exc);
1305 max_int
1307 if n != max_int
1308 then (
1309 conf.pagebias <- n;
1310 state.text <- "page bias is now " ^ string_of_int n;
1313 enttext ('+', "", None, intentry, ondone)
1315 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1316 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1317 conf.zoom <- max 0.01 (conf.zoom -. decr);
1318 if conf.zoom <= 1.0 then state.x <- 0;
1319 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1320 reshape conf.winw conf.winh;
1322 | '-' ->
1323 let ondone msg =
1324 state.text <- msg;
1326 enttext ('-', "", None, optentry, ondone)
1328 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1329 state.x <- 0;
1330 conf.zoom <- 1.0;
1331 state.text <- "zoom is 100%";
1332 reshape conf.winw conf.winh
1334 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1335 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1336 if zoom < 1.0
1337 then (
1338 conf.zoom <- zoom;
1339 state.x <- 0;
1340 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1341 reshape conf.winw conf.winh;
1344 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1345 togglebirdseye ()
1347 | '0' .. '9' ->
1348 let ondone s =
1349 let n =
1350 try int_of_string s with exc ->
1351 state.text <- Printf.sprintf "bad integer `%s': %s"
1352 s (Printexc.to_string exc);
1355 if n >= 0
1356 then (
1357 addnav ();
1358 cbput state.hists.pag (string_of_int n);
1359 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1362 let pageentry text key =
1363 match Char.unsafe_chr key with
1364 | 'g' -> TEdone text
1365 | _ -> intentry text key
1367 let text = "x" in text.[0] <- c;
1368 enttext (':', text, Some (onhist state.hists.pag), pageentry, ondone)
1370 | 'b' ->
1371 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1372 reshape conf.winw conf.winh;
1374 | 'l' ->
1375 conf.hlinks <- not conf.hlinks;
1376 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1377 Glut.postRedisplay ()
1379 | 'a' ->
1380 if state.ascrollstep = 0
1381 then state.ascrollstep <- conf.autoscrollstep
1382 else (
1383 conf.autoscrollstep <- state.ascrollstep;
1384 state.ascrollstep <- 0;
1387 | 'P' ->
1388 conf.presentation <- not conf.presentation;
1389 showtext ' ' ("presentation mode " ^
1390 if conf.presentation then "on" else "off");
1391 represent ()
1393 | 'f' ->
1394 begin match state.fullscreen with
1395 | None ->
1396 state.fullscreen <- Some (conf.winw, conf.winh);
1397 Glut.fullScreen ()
1398 | Some (w, h) ->
1399 state.fullscreen <- None;
1400 doreshape w h
1403 | 'g' ->
1404 gotoy_and_clear_text 0
1406 | 'n' ->
1407 search state.searchpattern true
1409 | 'p' | 'N' ->
1410 search state.searchpattern false
1412 | 't' ->
1413 begin match state.layout with
1414 | [] -> ()
1415 | l :: _ ->
1416 gotoy_and_clear_text (getpagey l.pageno)
1419 | ' ' ->
1420 begin match List.rev state.layout with
1421 | [] -> ()
1422 | l :: _ ->
1423 let pageno = min (l.pageno+1) (state.pagecount-1) in
1424 gotoy_and_clear_text (getpagey pageno)
1427 | '\127' ->
1428 begin match state.layout with
1429 | [] -> ()
1430 | l :: _ ->
1431 let pageno = max 0 (l.pageno-1) in
1432 gotoy_and_clear_text (getpagey pageno)
1435 | '=' ->
1436 let f (fn, ln) l =
1437 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1439 let fn, ln = List.fold_left f (-1, -1) state.layout in
1440 let s =
1441 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1442 let percent =
1443 if maxy <= 0
1444 then 100.
1445 else (100. *. (float state.y /. float maxy)) in
1446 if fn = ln
1447 then
1448 Printf.sprintf "Page %d of %d %.2f%%"
1449 (fn+1) state.pagecount percent
1450 else
1451 Printf.sprintf
1452 "Pages %d-%d of %d %.2f%%"
1453 (fn+1) (ln+1) state.pagecount percent
1455 showtext ' ' s;
1457 | 'w' ->
1458 begin match state.layout with
1459 | [] -> ()
1460 | l :: _ ->
1461 doreshape (l.pagew + conf.scrollw) l.pageh;
1462 Glut.postRedisplay ();
1465 | '\'' ->
1466 enterbookmarkmode ()
1468 | 'm' ->
1469 let ondone s =
1470 match state.layout with
1471 | l :: _ ->
1472 state.bookmarks <-
1473 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1474 :: state.bookmarks
1475 | _ -> ()
1477 enttext ('~', "", None, textentry, ondone)
1479 | '~' ->
1480 quickbookmark ();
1481 showtext ' ' "Quick bookmark added";
1483 | 'z' ->
1484 begin match state.layout with
1485 | l :: _ ->
1486 let rect = getpdimrect l.pagedimno in
1487 let w, h =
1488 if conf.crophack
1489 then
1490 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1491 truncate (1.2 *. (rect.(3) -. rect.(0))))
1492 else
1493 (truncate (rect.(1) -. rect.(0)),
1494 truncate (rect.(3) -. rect.(0)))
1496 if w != 0 && h != 0
1497 then
1498 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1500 Glut.postRedisplay ();
1502 | [] -> ()
1505 | '<' | '>' ->
1506 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1508 | '[' | ']' ->
1509 state.colorscale <-
1510 max 0.0
1511 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1512 Glut.postRedisplay ()
1514 | 'k' -> gotoy (clamp (-conf.scrollstep))
1515 | 'j' -> gotoy (clamp conf.scrollstep)
1517 | 'r' -> opendoc state.path state.password
1519 | _ ->
1520 vlog "huh? %d %c" key (Char.chr key);
1523 let textentrykeyboard ~key ~x ~y ((c, text, opthist, onkey, ondone), mode) =
1524 let enttext te =
1525 state.mode <- Textentry (te, mode);
1526 state.text <- "";
1527 enttext ();
1528 Glut.postRedisplay ()
1530 match Char.unsafe_chr key with
1531 | '\008' ->
1532 let len = String.length text in
1533 if len = 0
1534 then (
1535 state.mode <- mode;
1536 Glut.postRedisplay ();
1538 else (
1539 let s = String.sub text 0 (len - 1) in
1540 enttext (c, s, opthist, onkey, ondone)
1543 | '\r' | '\n' ->
1544 ondone text;
1545 state.mode <- mode;
1546 Glut.postRedisplay ()
1548 | '\027' ->
1549 begin match opthist with
1550 | None -> ()
1551 | Some (_, onhistcancel) -> onhistcancel ()
1552 end;
1553 state.mode <- View;
1554 Glut.postRedisplay ()
1556 | _ ->
1557 begin match onkey text key with
1558 | TEdone text ->
1559 state.mode <- mode;
1560 ondone text;
1561 Glut.postRedisplay ()
1563 | TEcont text ->
1564 enttext (c, text, opthist, onkey, ondone);
1566 | TEstop ->
1567 state.mode <- mode;
1568 Glut.postRedisplay ()
1570 | TEswitch te ->
1571 state.mode <- Textentry (te, mode);
1572 Glut.postRedisplay ()
1573 end;
1576 let birdseyekeyboard ~key ~x ~y ((_, _, pageno, _, anchor) as beye) =
1577 match key with
1578 | 27 ->
1579 birdseyeoff beye true
1581 | 12 ->
1582 let y, h = getpageyh pageno in
1583 let top = (conf.winh - h) / 2 in
1584 gotoy (max 0 (y - top))
1586 | 13 ->
1587 birdseyeoff beye false
1589 | _ ->
1590 viewkeyboard ~key ~x ~y
1593 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1594 let narrow outlines pattern =
1595 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1596 match reopt with
1597 | None -> None
1598 | Some re ->
1599 let rec fold accu n =
1600 if n = -1
1601 then accu
1602 else
1603 let (s, _, _, _) as o = outlines.(n) in
1604 let accu =
1605 if (try ignore (Str.search_forward re s 0); true
1606 with Not_found -> false)
1607 then (o :: accu)
1608 else accu
1610 fold accu (n-1)
1612 let matched = fold [] (Array.length outlines - 1) in
1613 if matched = [] then None else Some (Array.of_list matched)
1615 let search active pattern incr =
1616 let dosearch re =
1617 let rec loop n =
1618 if n = Array.length outlines || n = -1
1619 then None
1620 else
1621 let (s, _, _, _) = outlines.(n) in
1623 (try ignore (Str.search_forward re s 0); true
1624 with Not_found -> false)
1625 then Some n
1626 else loop (n + incr)
1628 loop active
1631 let re = Str.regexp_case_fold pattern in
1632 dosearch re
1633 with Failure s ->
1634 state.text <- s;
1635 None
1637 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1638 match key with
1639 | 27 ->
1640 if String.length qsearch = 0
1641 then (
1642 state.text <- "";
1643 state.mode <- View;
1644 Glut.postRedisplay ();
1646 else (
1647 state.text <- "";
1648 state.mode <- Outline (allowdel, active, first, outlines, "");
1649 Glut.postRedisplay ();
1652 | 18 | 19 ->
1653 let incr = if key = 18 then -1 else 1 in
1654 let active, first =
1655 match search (active + incr) qsearch incr with
1656 | None ->
1657 state.text <- qsearch ^ " [not found]";
1658 active, first
1659 | Some active ->
1660 state.text <- qsearch;
1661 active, firstof active
1663 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1664 Glut.postRedisplay ();
1666 | 8 ->
1667 let len = String.length qsearch in
1668 if len = 0
1669 then ()
1670 else (
1671 if len = 1
1672 then (
1673 state.text <- "";
1674 state.mode <- Outline (allowdel, active, first, outlines, "");
1676 else
1677 let qsearch = String.sub qsearch 0 (len - 1) in
1678 let active, first =
1679 match search active qsearch ~-1 with
1680 | None ->
1681 state.text <- qsearch ^ " [not found]";
1682 active, first
1683 | Some active ->
1684 state.text <- qsearch;
1685 active, firstof active
1687 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1689 Glut.postRedisplay ()
1691 | 13 ->
1692 if active < Array.length outlines
1693 then (
1694 let (_, _, n, t) = outlines.(active) in
1695 addnav ();
1696 gotopage n t;
1698 state.text <- "";
1699 if allowdel then state.bookmarks <- Array.to_list outlines;
1700 state.mode <- View;
1701 Glut.postRedisplay ();
1703 | _ when key >= 32 && key < 127 ->
1704 let pattern = addchar qsearch (Char.chr key) in
1705 let active, first =
1706 match search active pattern 1 with
1707 | None ->
1708 state.text <- pattern ^ " [not found]";
1709 active, first
1710 | Some active ->
1711 state.text <- pattern;
1712 active, firstof active
1714 state.mode <- Outline (allowdel, active, first, outlines, pattern);
1715 Glut.postRedisplay ()
1717 | 14 when not allowdel -> (* ctrl-n *)
1718 if String.length qsearch > 0
1719 then (
1720 let optoutlines = narrow outlines qsearch in
1721 begin match optoutlines with
1722 | None -> state.text <- "can't narrow"
1723 | Some outlines ->
1724 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1725 match state.outlines with
1726 | Olist l -> ()
1727 | Oarray a ->
1728 state.outlines <- Onarrow (qsearch, outlines, a)
1729 | Onarrow (pat, a, b) ->
1730 state.outlines <- Onarrow (qsearch, outlines, b)
1731 end;
1733 Glut.postRedisplay ()
1735 | 21 when not allowdel -> (* ctrl-u *)
1736 let outline =
1737 match state.outlines with
1738 | Oarray a -> a
1739 | Olist l ->
1740 let a = Array.of_list (List.rev l) in
1741 state.outlines <- Oarray a;
1743 | Onarrow (pat, a, b) ->
1744 state.outlines <- Oarray b;
1745 state.text <- "";
1748 state.mode <- Outline (allowdel, 0, 0, outline, qsearch);
1749 Glut.postRedisplay ()
1751 | 12 ->
1752 state.mode <- Outline
1753 (allowdel, active, firstof active, outlines, qsearch);
1754 Glut.postRedisplay ()
1756 | 127 when allowdel ->
1757 let len = Array.length outlines - 1 in
1758 if len = 0
1759 then (
1760 state.mode <- View;
1761 state.bookmarks <- [];
1763 else (
1764 let bookmarks = Array.init len
1765 (fun i ->
1766 let i = if i >= active then i + 1 else i in
1767 outlines.(i)
1770 state.mode <-
1771 Outline (
1772 allowdel,
1773 min active (len-1),
1774 min first (len-1),
1775 bookmarks, qsearch
1778 Glut.postRedisplay ()
1780 | _ -> dolog "unknown key %d" key
1783 let keyboard ~key ~x ~y =
1784 if key = 7
1785 then
1786 wcmd "interrupt" []
1787 else
1788 match state.mode with
1789 | Outline outline -> outlinekeyboard ~key ~x ~y outline
1790 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
1791 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
1792 | View -> viewkeyboard ~key ~x ~y
1795 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno, anchor) =
1796 match key with
1797 | Glut.KEY_UP ->
1798 let pageno = max 0 (pageno - 1) in
1799 let rec loop = function
1800 | [] -> gotopage1 pageno 0
1801 | l :: _ when l.pageno = pageno ->
1802 if l.pagedispy >= 0 && l.pagey = 0
1803 then Glut.postRedisplay ()
1804 else gotopage1 pageno 0
1805 | _ :: rest -> loop rest
1807 loop state.layout;
1808 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1810 | Glut.KEY_DOWN ->
1811 let pageno = min (state.pagecount - 1) (pageno + 1) in
1812 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1813 let rec loop = function
1814 | [] ->
1815 let y, h = getpageyh pageno in
1816 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1817 gotoy (clamp dy)
1818 | l :: rest when l.pageno = pageno ->
1819 if l.pagevh != l.pageh
1820 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1821 else Glut.postRedisplay ()
1822 | l :: rest -> loop rest
1824 loop state.layout
1826 | Glut.KEY_PAGE_UP ->
1827 begin match state.layout with
1828 | l :: _ ->
1829 if l.pagey != 0
1830 then (
1831 state.mode <- Birdseye (
1832 conf, leftx, l.pageno, hooverpageno, anchor
1834 gotopage1 l.pageno 0;
1836 else (
1837 let layout = layout (state.y-conf.winh) conf.winh in
1838 match layout with
1839 | [] -> gotoy (clamp (-conf.winh))
1840 | l :: _ ->
1841 state.mode <- Birdseye (
1842 conf, leftx, l.pageno, hooverpageno, anchor
1844 gotopage1 l.pageno 0
1847 | [] -> gotoy (clamp (-conf.winh))
1848 end;
1850 | Glut.KEY_PAGE_DOWN ->
1851 begin match List.rev state.layout with
1852 | l :: _ ->
1853 let layout = layout (state.y + conf.winh) conf.winh in
1854 begin match layout with
1855 | [] ->
1856 let incr = l.pageh - l.pagevh in
1857 if incr = 0
1858 then (
1859 state.mode <-
1860 Birdseye (
1861 conf, leftx, state.pagecount - 1, hooverpageno, anchor
1863 Glut.postRedisplay ();
1865 else gotoy (clamp (incr + conf.interpagespace*2));
1867 | l :: _ ->
1868 state.mode <-
1869 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
1870 gotopage1 l.pageno 0;
1873 | [] -> gotoy (clamp conf.winh)
1874 end;
1876 | Glut.KEY_HOME ->
1877 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
1878 gotopage1 0 0
1880 | Glut.KEY_END ->
1881 let pageno = state.pagecount - 1 in
1882 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1883 if not (pagevisible state.layout pageno)
1884 then
1885 let h =
1886 match List.rev state.pdims with
1887 | [] -> conf.winh
1888 | (_, _, h, _) :: _ -> h
1890 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1891 else Glut.postRedisplay ();
1892 | _ -> ()
1895 let setautoscrollspeed goingdown =
1896 let incr = max 1 (state.ascrollstep / 2) in
1897 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
1898 state.ascrollstep <- astep;
1901 let special ~key ~x ~y =
1902 match state.mode with
1903 | View | (Birdseye _) when key = Glut.KEY_F9 ->
1904 togglebirdseye ()
1906 | Birdseye vals ->
1907 birdseyespecial key x y vals
1909 | View ->
1910 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
1911 then setautoscrollspeed (key = Glut.KEY_DOWN)
1912 else
1913 let y =
1914 match key with
1915 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1916 | Glut.KEY_UP -> clamp (-conf.scrollstep)
1917 | Glut.KEY_DOWN -> clamp conf.scrollstep
1918 | Glut.KEY_PAGE_UP ->
1919 if Glut.getModifiers () land Glut.active_ctrl != 0
1920 then
1921 match state.layout with
1922 | [] -> state.y
1923 | l :: _ -> state.y - l.pagey
1924 else
1925 clamp (-conf.winh)
1926 | Glut.KEY_PAGE_DOWN ->
1927 if Glut.getModifiers () land Glut.active_ctrl != 0
1928 then
1929 match List.rev state.layout with
1930 | [] -> state.y
1931 | l :: _ -> getpagey l.pageno
1932 else
1933 clamp conf.winh
1934 | Glut.KEY_HOME -> addnav (); 0
1935 | Glut.KEY_END ->
1936 addnav ();
1937 state.maxy - (if conf.maxhfit then conf.winh else 0)
1939 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1940 state.x <- state.x - 10;
1941 state.y
1942 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1943 state.x <- state.x + 10;
1944 state.y
1946 | _ -> state.y
1948 gotoy_and_clear_text y
1950 | Textentry
1951 ((c, s, (Some (action, _) as onhist), onkey, ondone), mode) ->
1952 let s =
1953 match key with
1954 | Glut.KEY_UP -> action HCprev
1955 | Glut.KEY_DOWN -> action HCnext
1956 | Glut.KEY_HOME -> action HCfirst
1957 | Glut.KEY_END -> action HClast
1958 | _ -> state.text
1960 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
1961 Glut.postRedisplay ()
1963 | Textentry _ -> ()
1965 | Outline (allowdel, active, first, outlines, qsearch) ->
1966 let maxrows = maxoutlinerows () in
1967 let calcfirst first active =
1968 if active > first
1969 then
1970 let rows = active - first in
1971 if rows > maxrows then active - maxrows else first
1972 else active
1974 let navigate incr =
1975 let active = active + incr in
1976 let active = max 0 (min active (Array.length outlines - 1)) in
1977 let first = calcfirst first active in
1978 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1979 Glut.postRedisplay ()
1981 let updownlevel incr =
1982 let len = Array.length outlines in
1983 let (_, curlevel, _, _) = outlines.(active) in
1984 let rec flow i =
1985 if i = len then i-1 else if i = -1 then 0 else
1986 let (_, l, _, _) = outlines.(i) in
1987 if l != curlevel then i else flow (i+incr)
1989 let active = flow active in
1990 let first = calcfirst first active in
1991 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1992 Glut.postRedisplay ()
1994 match key with
1995 | Glut.KEY_UP -> navigate ~-1
1996 | Glut.KEY_DOWN -> navigate 1
1997 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1998 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2000 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
2001 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
2003 | Glut.KEY_HOME ->
2004 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
2005 Glut.postRedisplay ()
2007 | Glut.KEY_END ->
2008 let active = Array.length outlines - 1 in
2009 let first = max 0 (active - maxrows) in
2010 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2011 Glut.postRedisplay ()
2013 | _ -> ()
2016 let drawplaceholder l =
2017 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
2018 GlDraw.rect
2019 (float l.pagex, float l.pagedispy)
2020 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2022 let x = float (if margin < 0 then -margin else l.pagex)
2023 and y = float (l.pagedispy + 13) in
2024 let font = Glut.BITMAP_8_BY_13 in
2025 GlDraw.color (0.0, 0.0, 0.0);
2026 GlPix.raster_pos ~x ~y ();
2027 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2028 ("Loading " ^ string_of_int (l.pageno + 1));
2031 let now () = Unix.gettimeofday ();;
2033 let drawpage l =
2034 let color =
2035 match state.mode with
2036 | Textentry _ -> scalecolor 0.4
2037 | View | Outline _ -> scalecolor 1.0
2038 | Birdseye (_, _, pageno, hooverpageno, _) ->
2039 if l.pageno = pageno
2040 then scalecolor 1.0
2041 else (
2042 if l.pageno = hooverpageno
2043 then scalecolor 0.9
2044 else scalecolor 0.8
2047 GlDraw.color color;
2048 begin match getopaque l.pageno with
2049 | Some (opaque, _) when validopaque opaque ->
2050 let a = now () in
2051 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2052 opaque;
2053 let b = now () in
2054 let d = b-.a in
2055 vlog "draw %d %f sec" l.pageno d;
2057 | _ ->
2058 drawplaceholder l;
2059 end;
2062 let scrollph y =
2063 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2064 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2065 let sh = float conf.winh /. sh in
2066 let sh = max sh (float conf.scrollh) in
2068 let percent =
2069 if state.y = state.maxy
2070 then 1.0
2071 else float y /. float maxy
2073 let position = (float conf.winh -. sh) *. percent in
2075 let position =
2076 if position +. sh > float conf.winh
2077 then float conf.winh -. sh
2078 else position
2080 position, sh;
2083 let scrollindicator () =
2084 GlDraw.color (0.64 , 0.64, 0.64);
2085 GlDraw.rect
2086 (float (conf.winw - conf.scrollw), 0.)
2087 (float conf.winw, float conf.winh)
2089 GlDraw.color (0.0, 0.0, 0.0);
2091 let position, sh = scrollph state.y in
2092 GlDraw.rect
2093 (float (conf.winw - conf.scrollw), position)
2094 (float conf.winw, position +. sh)
2098 let showsel margin =
2099 match state.mstate with
2100 | Mnone | Mscroll _ | Mpan _ ->
2103 | Msel ((x0, y0), (x1, y1)) ->
2104 let rec loop = function
2105 | l :: ls ->
2106 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2107 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2108 then
2109 match getopaque l.pageno with
2110 | Some (opaque, _) when validopaque opaque ->
2111 let oy = -l.pagey + l.pagedispy in
2112 seltext opaque
2113 (x0 - margin - state.x, y0,
2114 x1 - margin - state.x, y1) oy;
2116 | _ -> ()
2117 else loop ls
2118 | [] -> ()
2120 loop state.layout
2123 let showrects () =
2124 let panx = float state.x in
2125 Gl.enable `blend;
2126 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2127 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2128 List.iter
2129 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2130 List.iter (fun l ->
2131 if l.pageno = pageno
2132 then (
2133 let d = float (l.pagedispy - l.pagey) in
2134 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2135 GlDraw.begins `quads;
2137 GlDraw.vertex2 (x0+.panx, y0+.d);
2138 GlDraw.vertex2 (x1+.panx, y1+.d);
2139 GlDraw.vertex2 (x2+.panx, y2+.d);
2140 GlDraw.vertex2 (x3+.panx, y3+.d);
2142 GlDraw.ends ();
2144 ) state.layout
2145 ) state.rects
2147 Gl.disable `blend;
2150 let showoutline () =
2151 match state.mode with
2152 | Outline (allowdel, active, first, outlines, qsearch) ->
2153 Gl.enable `blend;
2154 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2155 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2156 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2157 Gl.disable `blend;
2159 GlDraw.color (1., 1., 1.);
2160 let font = Glut.BITMAP_9_BY_15 in
2161 let draw_string x y s =
2162 GlPix.raster_pos ~x ~y ();
2163 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2165 let rec loop row =
2166 if row = Array.length outlines || (row - first) * 16 > conf.winh
2167 then ()
2168 else (
2169 let (s, l, _, _) = outlines.(row) in
2170 let y = (row - first) * 16 in
2171 let x = 5 + 15*l in
2172 if row = active
2173 then (
2174 Gl.enable `blend;
2175 GlDraw.polygon_mode `both `line;
2176 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2177 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2178 GlDraw.rect (0., float (y + 1))
2179 (float (conf.winw - 1), float (y + 18));
2180 GlDraw.polygon_mode `both `fill;
2181 Gl.disable `blend;
2182 GlDraw.color (1., 1., 1.);
2184 draw_string (float x) (float (y + 16)) s;
2185 loop (row+1)
2188 loop first
2190 | _ -> ()
2193 let display () =
2194 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2195 GlDraw.viewport margin 0 state.w conf.winh;
2196 pagematrix ();
2197 GlClear.color (scalecolor 0.5);
2198 GlClear.clear [`color];
2199 if conf.zoom > 1.0
2200 then (
2201 Gl.enable `scissor_test;
2202 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2204 List.iter drawpage state.layout;
2205 if conf.zoom > 1.0
2206 then
2207 Gl.disable `scissor_test
2209 if state.x != 0
2210 then (
2211 let x = -.float state.x in
2212 GlMat.translate ~x ();
2214 showrects ();
2215 showsel margin;
2216 GlDraw.viewport 0 0 conf.winw conf.winh;
2217 winmatrix ();
2218 scrollindicator ();
2219 showoutline ();
2220 enttext ();
2221 Glut.swapBuffers ();
2224 let getunder x y =
2225 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2226 let x = x - margin - state.x in
2227 let rec f = function
2228 | l :: rest ->
2229 begin match getopaque l.pageno with
2230 | Some (opaque, _) when validopaque opaque ->
2231 let y = y - l.pagedispy in
2232 if y > 0
2233 then
2234 let y = l.pagey + y in
2235 let x = x - l.pagex in
2236 match whatsunder opaque x y with
2237 | Unone -> f rest
2238 | under -> under
2239 else
2240 f rest
2241 | _ ->
2242 f rest
2244 | [] -> Unone
2246 f state.layout
2249 let viewmouse button bstate x y =
2250 match button with
2251 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2252 if state.ascrollstep > 0
2253 then
2254 setautoscrollspeed (n=4)
2255 else
2256 let incr =
2257 if n = 3
2258 then -conf.scrollstep
2259 else conf.scrollstep
2261 let incr = incr * 2 in
2262 let y = clamp incr in
2263 gotoy_and_clear_text y
2265 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2266 if bstate = Glut.DOWN
2267 then (
2268 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2269 state.mstate <- Mpan (x, y)
2271 else
2272 state.mstate <- Mnone
2274 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2275 if bstate = Glut.DOWN
2276 then
2277 let position, sh = scrollph state.y in
2278 if y > truncate position && y < truncate (position +. sh)
2279 then
2280 state.mstate <- Mscroll
2281 else
2282 let percent = float y /. float conf.winh in
2283 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2284 gotoy desty;
2285 state.mstate <- Mscroll
2286 else
2287 state.mstate <- Mnone
2289 | Glut.LEFT_BUTTON ->
2290 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2291 begin match dest with
2292 | Ulinkgoto (pageno, top) ->
2293 if pageno >= 0
2294 then (
2295 addnav ();
2296 gotopage1 pageno top;
2299 | Ulinkuri s ->
2300 print_endline s
2302 | Unone when bstate = Glut.DOWN ->
2303 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2304 state.mstate <- Mpan (x, y);
2306 | Unone | Utext _ ->
2307 if bstate = Glut.DOWN
2308 then (
2309 if conf.angle mod 360 = 0
2310 then (
2311 state.mstate <- Msel ((x, y), (x, y));
2312 Glut.postRedisplay ()
2315 else (
2316 match state.mstate with
2317 | Mnone -> ()
2319 | Mscroll ->
2320 state.mstate <- Mnone
2322 | Mpan _ ->
2323 Glut.setCursor Glut.CURSOR_INHERIT;
2324 state.mstate <- Mnone
2326 | Msel ((x0, y0), (x1, y1)) ->
2327 let f l =
2328 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2329 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2330 then
2331 match getopaque l.pageno with
2332 | Some (opaque, _) when validopaque opaque ->
2333 copysel opaque
2334 | _ -> ()
2336 List.iter f state.layout;
2337 copysel ""; (* ugly *)
2338 Glut.setCursor Glut.CURSOR_INHERIT;
2339 state.mstate <- Mnone;
2343 | _ -> ()
2346 let birdseyemouse button bstate x y
2347 (conf, leftx, pageno, hooverpageno, anchor) =
2348 match button with
2349 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2350 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2351 let rec loop = function
2352 | [] -> ()
2353 | l :: rest ->
2354 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2355 && x > margin && x < margin + l.pagew
2356 then (
2357 birdseyeoff (conf, leftx, l.pageno, hooverpageno, anchor) false;
2359 else loop rest
2361 loop state.layout
2362 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
2363 | _ -> ()
2366 let mouse bstate button x y =
2367 match state.mode with
2368 | View -> viewmouse button bstate x y
2369 | Birdseye beye -> birdseyemouse button bstate x y beye
2370 | Textentry _ -> ()
2371 | Outline _ -> ()
2374 let mouse ~button ~state ~x ~y = mouse state button x y;;
2376 let motion ~x ~y =
2377 match state.mode with
2378 | Outline _ -> ()
2379 | _ ->
2380 match state.mstate with
2381 | Mnone -> ()
2383 | Mpan (x0, y0) ->
2384 let dx = x - x0
2385 and dy = y0 - y in
2386 state.mstate <- Mpan (x, y);
2387 if conf.zoom > 1.0 then state.x <- state.x + dx;
2388 let y = clamp dy in
2389 gotoy_and_clear_text y
2391 | Msel (a, _) ->
2392 state.mstate <- Msel (a, (x, y));
2393 Glut.postRedisplay ()
2395 | Mscroll ->
2396 let y = min conf.winh (max 0 y) in
2397 let percent = float y /. float conf.winh in
2398 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2399 gotoy_and_clear_text y
2402 let pmotion ~x ~y =
2403 match state.mode with
2404 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
2405 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2406 let rec loop = function
2407 | [] ->
2408 if hooverpageno != -1
2409 then (
2410 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
2411 Glut.postRedisplay ();
2413 | l :: rest ->
2414 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2415 && x > margin && x < margin + l.pagew
2416 then (
2417 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
2418 Glut.postRedisplay ();
2420 else loop rest
2422 loop state.layout
2424 | Outline _ -> ()
2425 | _ ->
2426 match state.mstate with
2427 | Mnone ->
2428 begin match getunder x y with
2429 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2430 | Ulinkuri uri ->
2431 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2432 Glut.setCursor Glut.CURSOR_INFO
2433 | Ulinkgoto (page, y) ->
2434 if conf.underinfo
2435 then showtext 'p' ("age: " ^ string_of_int page);
2436 Glut.setCursor Glut.CURSOR_INFO
2437 | Utext s ->
2438 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2439 Glut.setCursor Glut.CURSOR_TEXT
2442 | Mpan _ | Msel _ | Mscroll ->
2447 module State =
2448 struct
2449 open Parser
2451 let home =
2453 match Sys.os_type with
2454 | "Win32" -> Sys.getenv "HOMEPATH"
2455 | _ -> Sys.getenv "HOME"
2456 with exn ->
2457 prerr_endline
2458 ("Can not determine home directory location: " ^
2459 Printexc.to_string exn);
2463 let config_of c attrs =
2464 let apply c k v =
2466 match k with
2467 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2468 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2469 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2470 | "preload" -> { c with preload = bool_of_string v }
2471 | "page-bias" -> { c with pagebias = int_of_string v }
2472 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
2473 | "auto-scroll-step" ->
2474 { c with autoscrollstep = max 0 (int_of_string v) }
2475 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2476 | "crop-hack" -> { c with crophack = bool_of_string v }
2477 | "throttle" -> { c with showall = bool_of_string v }
2478 | "highlight-links" -> { c with hlinks = bool_of_string v }
2479 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2480 | "vertical-margin" ->
2481 { c with interpagespace = max 0 (int_of_string v) }
2482 | "zoom" ->
2483 let zoom = float_of_string v /. 100. in
2484 let zoom = max 0.01 (min 2.2 zoom) in
2485 { c with zoom = zoom }
2486 | "presentation" -> { c with presentation = bool_of_string v }
2487 | "rotation-angle" -> { c with angle = int_of_string v }
2488 | "width" -> { c with winw = max 20 (int_of_string v) }
2489 | "height" -> { c with winh = max 20 (int_of_string v) }
2490 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2491 | "proportional-display" -> { c with proportional = bool_of_string v }
2492 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2493 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2494 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2495 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2496 | _ -> c
2497 with exn ->
2498 prerr_endline ("Error processing attribute (`" ^
2499 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2502 let rec fold c = function
2503 | [] -> c
2504 | (k, v) :: rest ->
2505 let c = apply c k v in
2506 fold c rest
2508 fold c attrs;
2511 let fromstring f pos n v d =
2512 try f v
2513 with exn ->
2514 dolog "Error processing attribute (%S=%S) at %d\n%s"
2515 n v pos (Printexc.to_string exn)
2520 let bookmark_of attrs =
2521 let rec fold title page rely = function
2522 | ("title", v) :: rest -> fold v page rely rest
2523 | ("page", v) :: rest -> fold title v rely rest
2524 | ("rely", v) :: rest -> fold title page v rest
2525 | _ :: rest -> fold title page rely rest
2526 | [] -> title, page, rely
2528 fold "invalid" "0" "0" attrs
2531 let doc_of attrs =
2532 let rec fold path page rely pan = function
2533 | ("path", v) :: rest -> fold v page rely pan rest
2534 | ("page", v) :: rest -> fold path v rely pan rest
2535 | ("rely", v) :: rest -> fold path page v pan rest
2536 | ("pan", v) :: rest -> fold path page rely v rest
2537 | _ :: rest -> fold path page rely pan rest
2538 | [] -> path, page, rely, pan
2540 fold "" "0" "0" "0" attrs
2543 let setconf dst src =
2544 dst.scrollw <- src.scrollw;
2545 dst.scrollh <- src.scrollh;
2546 dst.icase <- src.icase;
2547 dst.preload <- src.preload;
2548 dst.pagebias <- src.pagebias;
2549 dst.verbose <- src.verbose;
2550 dst.scrollstep <- src.scrollstep;
2551 dst.maxhfit <- src.maxhfit;
2552 dst.crophack <- src.crophack;
2553 dst.autoscrollstep <- src.autoscrollstep;
2554 dst.showall <- src.showall;
2555 dst.hlinks <- src.hlinks;
2556 dst.underinfo <- src.underinfo;
2557 dst.interpagespace <- src.interpagespace;
2558 dst.zoom <- src.zoom;
2559 dst.presentation <- src.presentation;
2560 dst.angle <- src.angle;
2561 dst.winw <- src.winw;
2562 dst.winh <- src.winh;
2563 dst.savebmarks <- src.savebmarks;
2564 dst.memlimit <- src.memlimit;
2565 dst.proportional <- src.proportional;
2566 dst.texcount <- src.texcount;
2567 dst.sliceheight <- src.sliceheight;
2568 dst.thumbw <- src.thumbw;
2571 let unent s =
2572 let l = String.length s in
2573 let b = Buffer.create l in
2574 unent b s 0 l;
2575 Buffer.contents b;
2578 let get s =
2579 let h = Hashtbl.create 10 in
2580 let dc = { defconf with angle = defconf.angle } in
2581 let rec toplevel v t spos epos =
2582 match t with
2583 | Vdata | Vcdata | Vend -> v
2584 | Vopen ("llppconfig", attrs, closed) ->
2585 if closed
2586 then v
2587 else { v with f = llppconfig }
2588 | Vopen _ ->
2589 error "unexpected subelement at top level" s spos
2590 | Vclose tag -> error "unexpected close at top level" s spos
2592 and llppconfig v t spos epos =
2593 match t with
2594 | Vdata | Vcdata | Vend -> v
2595 | Vopen ("defaults", attrs, closed) ->
2596 let c = config_of dc attrs in
2597 setconf dc c;
2598 if closed
2599 then v
2600 else { v with f = skip "defaults" (fun () -> v) }
2602 | Vopen ("doc", attrs, closed) ->
2603 let pathent, spage, srely, span = doc_of attrs in
2604 let path = unent pathent
2605 and pageno = fromstring int_of_string spos "page" spage 0
2606 and rely = fromstring float_of_string spos "rely" srely 0.0
2607 and pan = fromstring int_of_string spos "pan" span 0 in
2608 let c = config_of dc attrs in
2609 let anchor = (pageno, rely) in
2610 if closed
2611 then (Hashtbl.add h path (c, [], pan, anchor); v)
2612 else { v with f = doc path pan anchor c [] }
2614 | Vopen (tag, _, closed) ->
2615 error "unexpected subelement in llppconfig" s spos
2617 | Vclose "llppconfig" -> { v with f = toplevel }
2618 | Vclose tag -> error "unexpected close in llppconfig" s spos
2620 and doc path pan anchor c bookmarks v t spos epos =
2621 match t with
2622 | Vdata | Vcdata -> v
2623 | Vend -> error "unexpected end of input in doc" s spos
2624 | Vopen ("bookmarks", attrs, closed) ->
2625 { v with f = pbookmarks path pan anchor c bookmarks }
2627 | Vopen (tag, _, _) ->
2628 error "unexpected subelement in doc" s spos
2630 | Vclose "doc" ->
2631 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
2632 { v with f = llppconfig }
2634 | Vclose tag -> error "unexpected close in doc" s spos
2636 and pbookmarks path pan anchor c bookmarks v t spos epos =
2637 match t with
2638 | Vdata | Vcdata -> v
2639 | Vend -> error "unexpected end of input in bookmarks" s spos
2640 | Vopen ("item", attrs, closed) ->
2641 let titleent, spage, srely = bookmark_of attrs in
2642 let page = fromstring int_of_string spos "page" spage 0
2643 and rely = fromstring float_of_string spos "rely" srely 0.0 in
2644 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2645 if closed
2646 then { v with f = pbookmarks path pan anchor c bookmarks }
2647 else
2648 let f () = v in
2649 { v with f = skip "item" f }
2651 | Vopen _ ->
2652 error "unexpected subelement in bookmarks" s spos
2654 | Vclose "bookmarks" ->
2655 { v with f = doc path pan anchor c bookmarks }
2657 | Vclose tag -> error "unexpected close in bookmarks" s spos
2659 and skip tag f v t spos epos =
2660 match t with
2661 | Vdata | Vcdata -> v
2662 | Vend ->
2663 error ("unexpected end of input in skipped " ^ tag) s spos
2664 | Vopen (tag', _, closed) ->
2665 if closed
2666 then v
2667 else
2668 let f' () = { v with f = skip tag f } in
2669 { v with f = skip tag' f' }
2670 | Vclose ctag ->
2671 if tag = ctag
2672 then f ()
2673 else error ("unexpected close in skipped " ^ tag) s spos
2676 parse { f = toplevel; accu = () } s;
2677 h, dc;
2680 let do_load f ic =
2682 let len = in_channel_length ic in
2683 let s = String.create len in
2684 really_input ic s 0 len;
2685 f s;
2686 with
2687 | Parse_error (msg, s, pos) ->
2688 let subs = subs s pos in
2689 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2690 failwith ("parse error: " ^ s)
2692 | exn ->
2693 failwith ("config load error: " ^ Printexc.to_string exn)
2696 let path =
2697 let dir =
2699 let dir = Filename.concat home ".config" in
2700 if Sys.is_directory dir then dir else home
2701 with _ -> home
2703 Filename.concat dir "llpp.conf"
2706 let load1 f =
2707 if Sys.file_exists path
2708 then
2709 match
2710 (try Some (open_in_bin path)
2711 with exn ->
2712 prerr_endline
2713 ("Error opening configuation file `" ^ path ^ "': " ^
2714 Printexc.to_string exn);
2715 None
2717 with
2718 | Some ic ->
2719 begin try
2720 f (do_load get ic)
2721 with exn ->
2722 prerr_endline
2723 ("Error loading configuation from `" ^ path ^ "': " ^
2724 Printexc.to_string exn);
2725 end;
2726 close_in ic;
2728 | None -> ()
2729 else
2730 f (Hashtbl.create 0, defconf)
2733 let load () =
2734 let f (h, dc) =
2735 let pc, pb, px, pa =
2737 Hashtbl.find h (Filename.basename state.path)
2738 with Not_found -> dc, [], 0, (0, 0.0)
2740 setconf defconf dc;
2741 setconf conf pc;
2742 state.bookmarks <- pb;
2743 state.x <- px;
2744 cbput state.hists.nav pa;
2746 load1 f
2749 let add_attrs bb always dc c =
2750 let ob s a b =
2751 if always || a != b
2752 then Printf.bprintf bb "\n %s='%b'" s a
2753 and oi s a b =
2754 if always || a != b
2755 then Printf.bprintf bb "\n %s='%d'" s a
2756 and oz s a b =
2757 if always || a <> b
2758 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2760 let w, h =
2761 if always
2762 then dc.winw, dc.winh
2763 else
2764 match state.fullscreen with
2765 | Some wh -> wh
2766 | None -> c.winw, c.winh
2768 let zoom, presentation, interpagespace, showall=
2769 if always
2770 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2771 else
2772 match state.mode with
2773 | Birdseye (bc, _, _, _, _) ->
2774 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2775 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2777 oi "width" w dc.winw;
2778 oi "height" h dc.winh;
2779 oi "scroll-bar-width" c.scrollw dc.scrollw;
2780 oi "scroll-handle-height" c.scrollh dc.scrollh;
2781 ob "case-insensitive-search" c.icase dc.icase;
2782 ob "preload" c.preload dc.preload;
2783 oi "page-bias" c.pagebias dc.pagebias;
2784 oi "scroll-step" c.scrollstep dc.scrollstep;
2785 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
2786 ob "max-height-fit" c.maxhfit dc.maxhfit;
2787 ob "crop-hack" c.crophack dc.crophack;
2788 ob "throttle" showall dc.showall;
2789 ob "highlight-links" c.hlinks dc.hlinks;
2790 ob "under-cursor-info" c.underinfo dc.underinfo;
2791 oi "vertical-margin" interpagespace dc.interpagespace;
2792 oz "zoom" zoom dc.zoom;
2793 ob "presentation" presentation dc.presentation;
2794 oi "rotation-angle" c.angle dc.angle;
2795 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2796 ob "proportional-display" c.proportional dc.proportional;
2797 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2798 oi "texcount" c.texcount dc.texcount;
2799 oi "slice-height" c.sliceheight dc.sliceheight;
2800 oi "thumbnail-width" c.thumbw dc.thumbw;
2803 let save () =
2804 let bb = Buffer.create 32768 in
2805 let f (h, dc) =
2806 Buffer.add_string bb "<llppconfig>\n<defaults ";
2807 add_attrs bb true dc dc;
2808 Buffer.add_string bb "/>\n";
2810 let adddoc path x anchor c bookmarks =
2811 if bookmarks == [] && c = dc && anchor = emptyanchor
2812 then ()
2813 else (
2814 Printf.bprintf bb "<doc path='%s'"
2815 (enent path 0 (String.length path));
2817 if anchor <> emptyanchor
2818 then (
2819 let n, y = anchor in
2820 Printf.bprintf bb " page='%d'" n;
2821 Printf.bprintf bb " rely='%f'" y;
2824 if x != 0
2825 then Printf.bprintf bb " pan='%d'" x;
2827 add_attrs bb false dc c;
2829 begin match bookmarks with
2830 | [] -> Buffer.add_string bb "/>\n"
2831 | _ ->
2832 Buffer.add_string bb ">\n<bookmarks>\n";
2833 List.iter (fun (title, _level, page, rely) ->
2834 Printf.bprintf bb
2835 "<item title='%s' page='%d' rely='%f'/>\n"
2836 (enent title 0 (String.length title))
2837 page
2838 rely
2839 ) bookmarks;
2840 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2841 end;
2845 let x =
2846 match state.mode with
2847 | Birdseye (_, x, _, _, _) -> x
2848 | _ -> state.x
2850 let basename = Filename.basename state.path in
2851 adddoc basename x (getanchor ())
2852 { conf with
2853 autoscrollstep =
2854 if state.ascrollstep > 0
2855 then state.ascrollstep
2856 else conf.autoscrollstep }
2857 (if conf.savebmarks then state.bookmarks else []);
2859 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2860 if basename <> path
2861 then adddoc path x y c bookmarks
2862 ) h;
2863 Buffer.add_string bb "</llppconfig>";
2865 load1 f;
2866 if Buffer.length bb > 0
2867 then
2869 let tmp = path ^ ".tmp" in
2870 let oc = open_out_bin tmp in
2871 Buffer.output_buffer oc bb;
2872 close_out oc;
2873 Sys.rename tmp path;
2874 with exn ->
2875 prerr_endline
2876 ("error while saving configuration: " ^ Printexc.to_string exn)
2878 end;;
2880 let () =
2881 Arg.parse
2882 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2883 (fun s -> state.path <- s)
2884 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2886 if String.length state.path = 0
2887 then (prerr_endline "filename missing"; exit 1);
2889 State.load ();
2891 let _ = Glut.init Sys.argv in
2892 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2893 let () = Glut.initWindowSize conf.winw conf.winh in
2894 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
2896 let csock, ssock =
2897 if Sys.os_type = "Unix"
2898 then
2899 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2900 else
2901 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2902 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2903 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2904 Unix.bind sock addr;
2905 Unix.listen sock 1;
2906 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2907 Unix.connect csock addr;
2908 let ssock, _ = Unix.accept sock in
2909 Unix.close sock;
2910 let opts sock =
2911 Unix.setsockopt sock Unix.TCP_NODELAY true;
2912 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2914 opts ssock;
2915 opts csock;
2916 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2917 ssock, csock
2920 let () = Glut.displayFunc display in
2921 let () = Glut.reshapeFunc reshape in
2922 let () = Glut.keyboardFunc keyboard in
2923 let () = Glut.specialFunc special in
2924 let () = Glut.idleFunc (Some idle) in
2925 let () = Glut.mouseFunc mouse in
2926 let () = Glut.motionFunc motion in
2927 let () = Glut.passiveMotionFunc pmotion in
2929 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2930 state.csock <- csock;
2931 state.ssock <- ssock;
2932 state.text <- "Opening " ^ state.path;
2933 writeopen state.path state.password;
2935 at_exit State.save;
2937 let rec handlelablglutbug () =
2939 Glut.mainLoop ();
2940 with Glut.BadEnum "key in special_of_int" ->
2941 showtext '!' " LablGlut bug: special key not recognized";
2942 handlelablglutbug ()
2944 handlelablglutbug ();