Textentry can be entered not just from View
[llpp.git] / main.ml
blob564ff19fc0fe3389e37d7f2a77b9bc500a07aff9
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let log fmt = Printf.kprintf prerr_endline fmt;;
9 let dolog fmt = Printf.kprintf prerr_endline fmt;;
11 type params = angle * proportional * texcount * sliceheight
12 and pageno = int
13 and width = int
14 and height = int
15 and leftx = int
16 and opaque = string
17 and recttype = int
18 and pixmapsize = int
19 and angle = int
20 and proportional = bool
21 and interpagespace = int
22 and texcount = int
23 and sliceheight = int
24 and gen = int
25 and top = float
28 external init : Unix.file_descr -> params -> unit = "ml_init";;
29 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext : string -> (int * int * int * int) -> int -> unit =
31 "ml_seltext";;
32 external copysel : string -> unit = "ml_copysel";;
33 external getpdimrect : int -> float array = "ml_getpdimrect";;
34 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
35 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
37 type mpos = int * int
38 and mstate =
39 | Msel of (mpos * mpos)
40 | Mpan of mpos
41 | Mscroll
42 | Mnone
45 type 'a circbuf =
46 { store : 'a array
47 ; mutable rc : int
48 ; mutable wc : int
49 ; mutable len : int
53 type textentry = (char * string * onhist * onkey * ondone)
54 and onkey = string -> int -> te
55 and ondone = string -> unit
56 and histcancel = unit -> unit
57 and onhist = ((histcmd -> string) * histcancel) option
58 and histcmd = HCnext | HCprev | HCfirst | HClast
59 and te =
60 | TEstop
61 | TEdone of string
62 | TEcont of string
63 | TEswitch of textentry
66 let cbnew n v =
67 { store = Array.create n v
68 ; rc = 0
69 ; wc = 0
70 ; len = 0
74 let cbcap b = Array.length b.store;;
76 let cbput b v =
77 let cap = cbcap b in
78 b.store.(b.wc) <- v;
79 b.wc <- (b.wc + 1) mod cap;
80 b.rc <- b.wc;
81 b.len <- min (b.len + 1) cap;
84 let cbempty b = b.len = 0;;
86 let cbgetg b circular dir =
87 if cbempty b
88 then b.store.(0)
89 else
90 let rc = b.rc + dir in
91 let rc =
92 if circular
93 then (
94 if rc = -1
95 then b.len-1
96 else (
97 if rc = b.len
98 then 0
99 else rc
102 else max 0 (min rc (b.len-1))
104 b.rc <- rc;
105 b.store.(rc);
108 let cbget b = cbgetg b false;;
109 let cbgetc b = cbgetg b true;;
111 let cbpeek b =
112 let rc = b.wc - b.len in
113 let rc = if rc < 0 then cbcap b + rc else rc in
114 b.store.(rc);
117 let cbdecr b = b.len <- b.len - 1;;
119 type layout =
120 { pageno : int
121 ; pagedimno : int
122 ; pagew : int
123 ; pageh : int
124 ; pagedispy : int
125 ; pagey : int
126 ; pagevh : int
127 ; pagex : int
131 type conf =
132 { mutable scrollw : int
133 ; mutable scrollh : int
134 ; mutable icase : bool
135 ; mutable preload : bool
136 ; mutable pagebias : int
137 ; mutable verbose : bool
138 ; mutable scrollincr : int
139 ; mutable maxhfit : bool
140 ; mutable crophack : bool
141 ; mutable autoscroll : bool
142 ; mutable showall : bool
143 ; mutable hlinks : bool
144 ; mutable underinfo : bool
145 ; mutable interpagespace : interpagespace
146 ; mutable zoom : float
147 ; mutable presentation : bool
148 ; mutable angle : angle
149 ; mutable winw : int
150 ; mutable winh : int
151 ; mutable savebmarks : bool
152 ; mutable proportional : proportional
153 ; mutable memlimit : int
154 ; mutable texcount : texcount
155 ; mutable sliceheight : sliceheight
156 ; mutable thumbw : width
160 type outline = string * int * int * float;;
161 type outlines =
162 | Oarray of outline array
163 | Olist of outline list
164 | Onarrow of string * outline array * outline array
167 type rect = (float * float * float * float * float * float * float * float);;
169 type pagemapkey = (pageno * width * angle * proportional * gen);;
171 type anchor = pageno * top;;
173 type mode =
174 | Birdseye of (conf * leftx * pageno * pageno)
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 ; 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 ; scrollincr = 24
230 ; maxhfit = true
231 ; crophack = false
232 ; autoscroll = false
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
291 let vlog fmt =
292 if conf.verbose
293 then
294 Printf.kprintf prerr_endline fmt
295 else
296 Printf.kprintf ignore fmt
299 let writecmd fd s =
300 let len = String.length s in
301 let n = 4 + len in
302 let b = Buffer.create n in
303 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
304 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
305 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
306 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
307 Buffer.add_string b s;
308 let s' = Buffer.contents b in
309 let n' = Unix.write fd s' 0 n in
310 if n' != n then failwith "write failed";
313 let readcmd fd =
314 let s = "xxxx" in
315 let n = Unix.read fd s 0 4 in
316 if n != 4 then failwith "incomplete read(len)";
317 let len = 0
318 lor (Char.code s.[0] lsl 24)
319 lor (Char.code s.[1] lsl 16)
320 lor (Char.code s.[2] lsl 8)
321 lor (Char.code s.[3] lsl 0)
323 let s = String.create len in
324 let n = Unix.read fd s 0 len in
325 if n != len then failwith "incomplete read(data)";
329 let makecmd s l =
330 let b = Buffer.create 10 in
331 Buffer.add_string b s;
332 let rec combine = function
333 | [] -> b
334 | x :: xs ->
335 Buffer.add_char b ' ';
336 let s =
337 match x with
338 | `b b -> if b then "1" else "0"
339 | `s s -> s
340 | `i i -> string_of_int i
341 | `f f -> string_of_float f
342 | `I f -> string_of_int (truncate f)
344 Buffer.add_string b s;
345 combine xs;
347 combine l;
350 let wcmd s l =
351 let cmd = Buffer.contents (makecmd s l) in
352 writecmd state.csock cmd;
355 let calcips h =
356 if conf.presentation
357 then
358 let d = conf.winh - h in
359 max 0 ((d + 1) / 2)
360 else
361 conf.interpagespace
364 let calcheight () =
365 let rec f pn ph pi fh l =
366 match l with
367 | (n, _, h, _) :: rest ->
368 let ips = calcips h in
369 let fh =
370 if conf.presentation
371 then fh+ips
372 else (
373 if isbirdseye state.mode && pn = 0
374 then fh + ips
375 else fh
378 let fh = fh + ((n - pn) * (ph + pi)) in
379 f n h ips fh rest;
381 | [] ->
382 let inc =
383 if conf.presentation || (isbirdseye state.mode && pn = 0)
384 then 0
385 else -pi
387 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
388 max 0 fh
390 let fh = f 0 0 0 0 state.pdims in
394 let getpageyh pageno =
395 let rec f pn ph pi y l =
396 match l with
397 | (n, _, h, _) :: rest ->
398 let ips = calcips h in
399 if n >= pageno
400 then
401 let h = if n = pageno then h else ph in
402 if conf.presentation && n = pageno
403 then
404 y + (pageno - pn) * (ph + pi) + pi, h
405 else
406 y + (pageno - pn) * (ph + pi), h
407 else
408 let y = y + (if conf.presentation then pi else 0) in
409 let y = y + (n - pn) * (ph + pi) in
410 f n h ips y rest
412 | [] ->
413 y + (pageno - pn) * (ph + pi), ph
415 f 0 0 0 0 state.pdims
418 let getpagey pageno = fst (getpageyh pageno);;
420 let layout y sh =
421 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
422 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
423 match pdims with
424 | (pageno', w, h, x) :: rest when pageno' = pageno ->
425 let ips = calcips h in
426 let yinc =
427 if conf.presentation || (isbirdseye state.mode && pageno = 0)
428 then ips
429 else 0
431 (w, h, ips, x), rest, pdimno + 1, yinc
432 | _ ->
433 prev, pdims, pdimno, 0
435 let dy = dy + yinc in
436 let py = py + yinc in
437 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
438 then
439 accu
440 else
441 let vy = y + dy in
442 if py + h <= vy - yinc
443 then
444 let py = py + h + ips in
445 let dy = max 0 (py - y) in
446 f ~pageno:(pageno+1)
447 ~pdimno
448 ~prev:curr
451 ~pdims:rest
452 ~cacheleft
453 ~accu
454 else
455 let pagey = vy - py in
456 let pagevh = h - pagey in
457 let pagevh = min (sh - dy) pagevh in
458 let off = if yinc > 0 then py - vy else 0 in
459 let py = py + h + ips in
460 let e =
461 { pageno = pageno
462 ; pagedimno = pdimno
463 ; pagew = w
464 ; pageh = h
465 ; pagedispy = dy + off
466 ; pagey = pagey + off
467 ; pagevh = pagevh - off
468 ; pagex = x
471 let accu = e :: accu in
472 f ~pageno:(pageno+1)
473 ~pdimno
474 ~prev:curr
476 ~dy:(dy+pagevh+ips)
477 ~pdims:rest
478 ~cacheleft:(cacheleft-1)
479 ~accu
481 if state.invalidated = 0
482 then (
483 let accu =
485 ~pageno:0
486 ~pdimno:~-1
487 ~prev:(0,0,0,0)
488 ~py:0
489 ~dy:0
490 ~pdims:state.pdims
491 ~cacheleft:(cbcap state.pagecache)
492 ~accu:[]
494 List.rev accu
496 else
500 let clamp incr =
501 let y = state.y + incr in
502 let y = max 0 y in
503 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
507 let getopaque pageno =
508 try Some (Hashtbl.find state.pagemap
509 (pageno, state.w, conf.angle, conf.proportional, state.gen))
510 with Not_found -> None
513 let cache pageno opaque =
514 Hashtbl.replace state.pagemap
515 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
518 let validopaque opaque = String.length opaque > 0;;
520 let render l =
521 match getopaque l.pageno with
522 | None when not state.rendering ->
523 state.rendering <- true;
524 cache l.pageno ("", -1);
525 wcmd "render" [`i (l.pageno + 1)
526 ;`i l.pagedimno
527 ;`i l.pagew
528 ;`i l.pageh];
530 | _ -> ()
533 let loadlayout layout =
534 let rec f all = function
535 | l :: ls ->
536 begin match getopaque l.pageno with
537 | None -> render l; f false ls
538 | Some (opaque, _) -> f (all && validopaque opaque) ls
540 | [] -> all
542 f (layout <> []) layout;
545 let findpageforopaque opaque =
546 Hashtbl.fold
547 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
548 state.pagemap None
551 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
553 let preload () =
554 let oktopreload =
555 if conf.preload
556 then
557 let memleft = conf.memlimit - state.memused in
558 if memleft < 0
559 then
560 let opaque = cbpeek state.pagecache in
561 match findpageforopaque opaque with
562 | Some ((n, _, _, _, _), size) ->
563 memleft + size >= 0 && not (pagevisible state.layout n)
564 | None -> false
565 else true
566 else false
568 if oktopreload
569 then
570 let presentation = conf.presentation in
571 let interpagespace = conf.interpagespace in
572 let maxy = state.maxy in
573 conf.presentation <- false;
574 conf.interpagespace <- 0;
575 state.maxy <- calcheight ();
576 let y =
577 match state.layout with
578 | [] -> 0
579 | l :: _ -> getpagey l.pageno
581 let y = if y < conf.winh then 0 else y - conf.winh in
582 let pages = layout y (conf.winh*3) in
583 List.iter render pages;
584 conf.presentation <- presentation;
585 conf.interpagespace <- interpagespace;
586 state.maxy <- maxy;
589 let gotoy y =
590 let y = max 0 y in
591 let y = min state.maxy y in
592 let pages = layout y conf.winh in
593 let ready = loadlayout pages in
594 if conf.showall
595 then (
596 if ready
597 then (
598 state.y <- y;
599 state.layout <- pages;
600 state.throttle <- None;
601 Glut.postRedisplay ();
603 else (
604 state.throttle <- Some pages;
607 else (
608 state.y <- y;
609 state.layout <- pages;
610 state.throttle <- None;
611 Glut.postRedisplay ();
613 begin match state.mode with
614 | Birdseye (conf, leftx, pageno, hooverpageno) ->
615 if not (pagevisible pages pageno)
616 then (
617 match state.layout with
618 | [] -> ()
619 | l :: _ ->
620 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno)
622 | _ -> ()
623 end;
624 preload ();
627 let gotoy_and_clear_text y =
628 gotoy y;
629 if not conf.verbose then state.text <- "";
632 let emptyanchor = (0, 0.0);;
634 let getanchor () =
635 match state.layout with
636 | [] -> emptyanchor
637 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
640 let getanchory (n, top) =
641 let y, h = getpageyh n in
642 y + (truncate (top *. float h));
645 let gotoanchor anchor =
646 gotoy (getanchory anchor);
649 let addnav () =
650 cbput state.hists.nav (getanchor ());
653 let getnav () =
654 let anchor = cbgetc state.hists.nav ~-1 in
655 getanchory anchor;
658 let gotopagenonav n top =
659 let y, h = getpageyh n in
660 gotoy_and_clear_text (y + (truncate (top *. float h)));
663 let gotopage1nonav n top =
664 let y, h = getpageyh n in
665 addnav ();
666 gotoy_and_clear_text (y + top);
669 let gotopage n top =
670 let y, h = getpageyh n in
671 addnav ();
672 gotoy_and_clear_text (y + (truncate (top *. float h)));
675 let gotopage1 n top =
676 let y = getpagey n in
677 addnav ();
678 gotoy_and_clear_text (y + top);
681 let invalidate () =
682 state.layout <- [];
683 state.pdims <- [];
684 state.rects <- [];
685 state.rects1 <- [];
686 state.invalidated <- state.invalidated + 1;
689 let scalecolor c =
690 let c = c *. state.colorscale in
691 (c, c, c);
694 let represent () =
695 state.maxy <- calcheight ();
696 match state.mode with
697 | Birdseye (_, _, pageno, _) ->
698 let y, h = getpageyh pageno in
699 let top = (conf.winh - h) / 2 in
700 gotoy (max 0 (y - top))
701 | _ -> gotoanchor state.anchor
704 let pagematrix () =
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 state.w, 2.0 /. float conf.winh, 1.0);
712 let winmatrix () =
713 GlMat.mode `projection;
714 GlMat.load_identity ();
715 GlMat.rotate ~x:1.0 ~angle:180.0 ();
716 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
717 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
720 let reshape ~w ~h =
721 if state.invalidated = 0
722 then state.anchor <- getanchor ();
724 conf.winw <- w;
725 let w = truncate (float w *. conf.zoom) - conf.scrollw in
726 let w = max w 2 in
727 state.w <- w;
728 conf.winh <- h;
729 GlMat.mode `modelview;
730 GlMat.load_identity ();
731 GlClear.color (scalecolor 1.0);
732 GlClear.clear [`color];
734 invalidate ();
735 wcmd "geometry" [`i w; `i h];
738 let showtext c s =
739 GlDraw.color (0.0, 0.0, 0.0);
740 GlDraw.rect
741 (0.0, float (conf.winh - 18))
742 (float (conf.winw - conf.scrollw - 1), float conf.winh)
744 let font = Glut.BITMAP_8_BY_13 in
745 GlDraw.color (1.0, 1.0, 1.0);
746 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
747 Glut.bitmapCharacter ~font ~c:(Char.code c);
748 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
751 let enttext () =
752 let len = String.length state.text in
753 match state.mode with
754 | Textentry ((c, text, _, _, _), _) ->
755 let s =
756 if len > 0
757 then
758 text ^ " [" ^ state.text ^ "]"
759 else
760 text
762 showtext c s;
764 | _ ->
765 if len > 0 then showtext ' ' state.text
768 let showtext c s =
769 if true
770 then (
771 state.text <- Printf.sprintf "%c%s" c s;
772 Glut.postRedisplay ();
774 else (
775 showtext c s;
776 Glut.swapBuffers ();
780 let act cmd =
781 match cmd.[0] with
782 | 'c' ->
783 state.pdims <- [];
785 | 'D' ->
786 state.rects <- state.rects1;
787 Glut.postRedisplay ()
789 | 'C' ->
790 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
791 state.pagecount <- n;
792 state.invalidated <- state.invalidated - 1;
793 if state.invalidated = 0
794 then represent ()
796 | 't' ->
797 let s = Scanf.sscanf cmd "t %n"
798 (fun n -> String.sub cmd n (String.length cmd - n))
800 Glut.setWindowTitle s
802 | 'T' ->
803 let s = Scanf.sscanf cmd "T %n"
804 (fun n -> String.sub cmd n (String.length cmd - n))
806 if istextentry state.mode
807 then (
808 state.text <- s;
809 showtext ' ' s;
811 else (
812 state.text <- s;
813 Glut.postRedisplay ();
816 | 'V' ->
817 if conf.verbose
818 then
819 let s = Scanf.sscanf cmd "V %n"
820 (fun n -> String.sub cmd n (String.length cmd - n))
822 state.text <- s;
823 showtext ' ' s;
825 | 'F' ->
826 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
827 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
828 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
829 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
831 let y = (getpagey pageno) + truncate y0 in
832 addnav ();
833 gotoy y;
834 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
836 | 'R' ->
837 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
838 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
839 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
840 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
842 state.rects1 <-
843 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
845 | 'r' ->
846 let n, w, h, r, l, s, p =
847 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
848 (fun n w h r l s p ->
849 (n-1, w, h, r, l != 0, s, p))
852 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
853 state.memused <- state.memused + s;
855 let layout =
856 match state.throttle with
857 | None -> state.layout
858 | Some layout -> layout
861 let rec gc () =
862 if (state.memused <= conf.memlimit) || cbempty state.pagecache
863 then ()
864 else (
865 let evictedopaque = cbpeek state.pagecache in
866 match findpageforopaque evictedopaque with
867 | None -> failwith "bug in gc"
868 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
869 if state.gen != gen || not (pagevisible layout evictedn)
870 then (
871 wcmd "free" [`s evictedopaque];
872 state.memused <- state.memused - evictedsize;
873 Hashtbl.remove state.pagemap k;
874 cbdecr state.pagecache;
875 gc ();
879 gc ();
881 cbput state.pagecache p;
882 state.rendering <- false;
884 begin match state.throttle with
885 | None ->
886 if pagevisible state.layout n
887 then gotoy state.y
888 else (
889 let allvisible = loadlayout state.layout in
890 if allvisible then preload ();
893 | Some layout ->
894 match layout with
895 | [] -> ()
896 | l :: _ ->
897 let y = getpagey l.pageno + l.pagey in
898 gotoy y
901 | 'l' ->
902 let (n, w, h, x) as pdim =
903 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
905 state.pdims <- pdim :: state.pdims
907 | 'o' ->
908 let (l, n, t, h, pos) =
909 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
911 let s = String.sub cmd pos (String.length cmd - pos) in
912 let s =
913 let l = String.length s in
914 let b = Buffer.create (String.length s) in
915 let rec loop pc2 i =
916 if i = l
917 then ()
918 else
919 let pc2 =
920 match s.[i] with
921 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
922 | '\xc2' -> true
923 | c ->
924 let c = if Char.code c land 0x80 = 0 then c else '?' in
925 Buffer.add_char b c;
926 false
928 loop pc2 (i+1)
930 loop false 0;
931 Buffer.contents b
933 let outline = (s, l, n, float t /. float h) in
934 let outlines =
935 match state.outlines with
936 | Olist outlines -> Olist (outline :: outlines)
937 | Oarray _ -> Olist [outline]
938 | Onarrow _ -> Olist [outline]
940 state.outlines <- outlines
942 | _ ->
943 log "unknown cmd `%S'" cmd
946 let now = Unix.gettimeofday;;
948 let idle () =
949 let rec loop delay =
950 let r, _, _ = Unix.select [state.csock] [] [] delay in
951 begin match r with
952 | [] ->
953 if conf.autoscroll && conf.scrollincr != 0
954 then begin
955 let y = state.y + conf.scrollincr in
956 let y = if y >= state.maxy then 0 else y in
957 gotoy y;
958 state.text <- "";
959 end;
961 | _ ->
962 let cmd = readcmd state.csock in
963 act cmd;
964 loop 0.0
965 end;
966 in loop 0.001
969 let onhist cb =
970 let rc = cb.rc in
971 let action = function
972 | HCprev -> cbget cb ~-1
973 | HCnext -> cbget cb 1
974 | HCfirst -> cbget cb ~-(cb.rc)
975 | HClast -> cbget cb (cb.len - 1 - cb.rc)
976 and cancel () = cb.rc <- rc
977 in (action, cancel)
980 let search pattern forward =
981 if String.length pattern > 0
982 then
983 let pn, py =
984 match state.layout with
985 | [] -> 0, 0
986 | l :: _ ->
987 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
989 let cmd =
990 let b = makecmd "search"
991 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
993 Buffer.add_char b ',';
994 Buffer.add_string b pattern;
995 Buffer.add_char b '\000';
996 Buffer.contents b;
998 writecmd state.csock cmd;
1001 let intentry text key =
1002 let c = Char.unsafe_chr key in
1003 match c with
1004 | '0' .. '9' ->
1005 let s = "x" in s.[0] <- c;
1006 let text = text ^ s in
1007 TEcont text
1009 | _ ->
1010 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1011 TEcont text
1014 let addchar s c =
1015 let b = Buffer.create (String.length s + 1) in
1016 Buffer.add_string b s;
1017 Buffer.add_char b c;
1018 Buffer.contents b;
1021 let textentry text key =
1022 let c = Char.unsafe_chr key in
1023 match c with
1024 | _ when key >= 32 && key < 127 ->
1025 let text = addchar text c in
1026 TEcont text
1028 | _ ->
1029 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1030 TEcont text
1033 let reinit angle proportional =
1034 conf.angle <- angle;
1035 conf.proportional <- proportional;
1036 invalidate ();
1037 wcmd "reinit" [`i angle; `b proportional];
1040 let optentry text key =
1041 let btos b = if b then "on" else "off" in
1042 let c = Char.unsafe_chr key in
1043 match c with
1044 | 's' ->
1045 let ondone s =
1046 try conf.scrollincr <- int_of_string s with exc ->
1047 state.text <- Printf.sprintf "bad integer `%s': %s"
1048 s (Printexc.to_string exc)
1050 TEswitch ('#', "", None, intentry, ondone)
1052 | 'R' ->
1053 let ondone s =
1054 match try
1055 Some (int_of_string s)
1056 with exc ->
1057 state.text <- Printf.sprintf "bad integer `%s': %s"
1058 s (Printexc.to_string exc);
1059 None
1060 with
1061 | Some angle -> reinit angle conf.proportional
1062 | None -> ()
1064 TEswitch ('^', "", None, intentry, ondone)
1066 | 'i' ->
1067 conf.icase <- not conf.icase;
1068 TEdone ("case insensitive search " ^ (btos conf.icase))
1070 | 'p' ->
1071 conf.preload <- not conf.preload;
1072 gotoy state.y;
1073 TEdone ("preload " ^ (btos conf.preload))
1075 | 'v' ->
1076 conf.verbose <- not conf.verbose;
1077 TEdone ("verbose " ^ (btos conf.verbose))
1079 | 'h' ->
1080 conf.maxhfit <- not conf.maxhfit;
1081 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1082 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1084 | 'c' ->
1085 conf.crophack <- not conf.crophack;
1086 TEdone ("crophack " ^ btos conf.crophack)
1088 | 'a' ->
1089 conf.showall <- not conf.showall;
1090 TEdone ("showall " ^ btos conf.showall)
1092 | 'f' ->
1093 conf.underinfo <- not conf.underinfo;
1094 TEdone ("underinfo " ^ btos conf.underinfo)
1096 | 'P' ->
1097 conf.savebmarks <- not conf.savebmarks;
1098 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1100 | 'S' ->
1101 let ondone s =
1103 let pageno, py =
1104 match state.layout with
1105 | [] -> 0, 0
1106 | l :: _ ->
1107 l.pageno, l.pagey
1109 conf.interpagespace <- int_of_string s;
1110 state.maxy <- calcheight ();
1111 let y = getpagey pageno in
1112 gotoy (y + py)
1113 with exc ->
1114 state.text <- Printf.sprintf "bad integer `%s': %s"
1115 s (Printexc.to_string exc)
1117 TEswitch ('%', "", None, intentry, ondone)
1119 | 'l' ->
1120 reinit conf.angle (not conf.proportional);
1121 TEdone ("proprortional display " ^ btos conf.proportional)
1123 | _ ->
1124 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1125 TEstop
1128 let maxoutlinerows () = (conf.winh - 31) / 16;;
1130 let enterselector allowdel outlines errmsg msg =
1131 if Array.length outlines = 0
1132 then (
1133 showtext ' ' errmsg;
1135 else (
1136 state.text <- msg;
1137 Glut.setCursor Glut.CURSOR_INHERIT;
1138 let pageno =
1139 match state.layout with
1140 | [] -> -1
1141 | {pageno=pageno} :: rest -> pageno
1143 let active =
1144 let rec loop n =
1145 if n = Array.length outlines
1146 then 0
1147 else
1148 let (_, _, outlinepageno, _) = outlines.(n) in
1149 if outlinepageno >= pageno then n else loop (n+1)
1151 loop 0
1153 state.mode <- Outline
1154 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "");
1155 Glut.postRedisplay ();
1159 let enteroutlinemode () =
1160 let outlines, msg =
1161 match state.outlines with
1162 | Oarray a -> a, ""
1163 | Olist l ->
1164 let a = Array.of_list (List.rev l) in
1165 state.outlines <- Oarray a;
1166 a, ""
1167 | Onarrow (pat, a, b) ->
1168 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1170 enterselector false outlines "Document has no outline" msg;
1173 let enterbookmarkmode () =
1174 let bookmarks = Array.of_list state.bookmarks in
1175 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1178 let quickbookmark ?title () =
1179 match state.layout with
1180 | [] -> ()
1181 | l :: _ ->
1182 let title =
1183 match title with
1184 | None ->
1185 let sec = Unix.gettimeofday () in
1186 let tm = Unix.localtime sec in
1187 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1188 (l.pageno+1)
1189 tm.Unix.tm_mday
1190 tm.Unix.tm_mon
1191 (tm.Unix.tm_year + 1900)
1192 tm.Unix.tm_hour
1193 tm.Unix.tm_min
1194 | Some title -> title
1196 state.bookmarks <-
1197 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1200 let doreshape w h =
1201 state.fullscreen <- None;
1202 Glut.reshapeWindow w h;
1205 let writeopen path password =
1206 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1209 let opendoc path password =
1210 invalidate ();
1211 state.path <- path;
1212 state.password <- password;
1213 state.gen <- state.gen + 1;
1215 writeopen path password;
1216 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1217 wcmd "geometry" [`i state.w; `i conf.winh];
1220 let birdseyeon () =
1221 let zoom = float conf.thumbw /. float conf.winw in
1222 let (birdseyepageno, _) as anchor = getanchor () in
1223 state.mode <-
1224 Birdseye ({ conf with zoom = conf.zoom }, state.x, birdseyepageno, -1);
1225 conf.zoom <- zoom;
1226 conf.presentation <- false;
1227 conf.interpagespace <- 10;
1228 conf.hlinks <- false;
1229 state.x <- 0;
1230 state.mstate <- Mnone;
1231 conf.showall <- false;
1232 Glut.setCursor Glut.CURSOR_INHERIT;
1233 if conf.verbose
1234 then
1235 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1236 (100.0*.zoom)
1237 else
1238 state.text <- ""
1242 let birdseyeoff (c, leftx, pageno, _) =
1243 state.mode <- View;
1244 conf.zoom <- c.zoom;
1245 conf.presentation <- c.presentation;
1246 conf.interpagespace <- c.interpagespace;
1247 conf.showall <- c.showall;
1248 conf.hlinks <- c.hlinks;
1249 state.x <- leftx;
1250 if conf.verbose
1251 then
1252 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1253 (100.0*.conf.zoom)
1257 let togglebirdseye () =
1258 match state.mode with
1259 | Birdseye vals -> birdseyeoff vals
1260 | View | Outline _ -> birdseyeon ()
1261 | _ -> ()
1264 let viewkeyboard ~key ~x ~y =
1265 let enttext te =
1266 state.mode <- Textentry (te, state.mode);
1267 state.text <- "";
1268 enttext ();
1269 Glut.postRedisplay ()
1271 let c = Char.chr key in
1272 match c with
1273 | '\027' | 'q' ->
1274 exit 0
1276 | '\008' ->
1277 let y = getnav () in
1278 gotoy_and_clear_text y
1280 | 'o' ->
1281 enteroutlinemode ()
1283 | 'u' ->
1284 state.rects <- [];
1285 state.text <- "";
1286 Glut.postRedisplay ()
1288 | '/' | '?' ->
1289 let ondone isforw s =
1290 cbput state.hists.pat s;
1291 state.searchpattern <- s;
1292 search s isforw
1294 enttext (c, "", Some (onhist state.hists.pat),
1295 textentry, ondone (c ='/'))
1297 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1298 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1299 conf.zoom <- min 2.2 (conf.zoom +. incr);
1300 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1301 reshape conf.winw conf.winh
1303 | '+' ->
1304 let ondone s =
1305 let n =
1306 try int_of_string s with exc ->
1307 state.text <- Printf.sprintf "bad integer `%s': %s"
1308 s (Printexc.to_string exc);
1309 max_int
1311 if n != max_int
1312 then (
1313 conf.pagebias <- n;
1314 state.text <- "page bias is now " ^ string_of_int n;
1317 enttext ('+', "", None, intentry, ondone)
1319 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1320 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1321 conf.zoom <- max 0.01 (conf.zoom -. decr);
1322 if conf.zoom <= 1.0 then state.x <- 0;
1323 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1324 reshape conf.winw conf.winh;
1326 | '-' ->
1327 let ondone msg =
1328 state.text <- msg;
1330 enttext ('-', "", None, optentry, ondone)
1332 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1333 state.x <- 0;
1334 conf.zoom <- 1.0;
1335 state.text <- "zoom is 100%";
1336 reshape conf.winw conf.winh
1338 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1339 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1340 if zoom < 1.0
1341 then (
1342 conf.zoom <- zoom;
1343 state.x <- 0;
1344 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1345 reshape conf.winw conf.winh;
1348 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1349 togglebirdseye ();
1350 reshape conf.winw conf.winh;
1352 | '0' .. '9' ->
1353 let ondone s =
1354 let n =
1355 try int_of_string s with exc ->
1356 state.text <- Printf.sprintf "bad integer `%s': %s"
1357 s (Printexc.to_string exc);
1360 if n >= 0
1361 then (
1362 addnav ();
1363 cbput state.hists.pag (string_of_int n);
1364 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1367 let pageentry text key =
1368 match Char.unsafe_chr key with
1369 | 'g' -> TEdone text
1370 | _ -> intentry text key
1372 let text = "x" in text.[0] <- c;
1373 enttext (':', text, Some (onhist state.hists.pag), pageentry, ondone)
1375 | 'b' ->
1376 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1377 reshape conf.winw conf.winh;
1379 | 'l' ->
1380 conf.hlinks <- not conf.hlinks;
1381 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1382 Glut.postRedisplay ()
1384 | 'a' ->
1385 conf.autoscroll <- not conf.autoscroll
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.scrollincr))
1515 | 'j' -> gotoy (clamp conf.scrollincr)
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 ((c, leftx, pageno, hooverpageno) as beye) =
1577 match key with
1578 | 27 ->
1579 birdseyeoff beye;
1580 reshape conf.winw conf.winh
1582 | 12 ->
1583 let y, h = getpageyh pageno in
1584 let top = (conf.winh - h) / 2 in
1585 gotoy (max 0 (y - top))
1587 | 13 ->
1588 addnav ();
1589 birdseyeoff beye;
1590 reshape conf.winw conf.winh;
1591 state.anchor <- (pageno, 0.0);
1593 | _ ->
1594 viewkeyboard ~key ~x ~y
1597 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1598 let narrow outlines pattern =
1599 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1600 match reopt with
1601 | None -> None
1602 | Some re ->
1603 let rec fold accu n =
1604 if n = -1
1605 then accu
1606 else
1607 let (s, _, _, _) as o = outlines.(n) in
1608 let accu =
1609 if (try ignore (Str.search_forward re s 0); true
1610 with Not_found -> false)
1611 then (o :: accu)
1612 else accu
1614 fold accu (n-1)
1616 let matched = fold [] (Array.length outlines - 1) in
1617 if matched = [] then None else Some (Array.of_list matched)
1619 let search active pattern incr =
1620 let dosearch re =
1621 let rec loop n =
1622 if n = Array.length outlines || n = -1
1623 then None
1624 else
1625 let (s, _, _, _) = outlines.(n) in
1627 (try ignore (Str.search_forward re s 0); true
1628 with Not_found -> false)
1629 then Some n
1630 else loop (n + incr)
1632 loop active
1635 let re = Str.regexp_case_fold pattern in
1636 dosearch re
1637 with Failure s ->
1638 state.text <- s;
1639 None
1641 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1642 match key with
1643 | 27 ->
1644 if String.length qsearch = 0
1645 then (
1646 state.text <- "";
1647 state.mode <- View;
1648 Glut.postRedisplay ();
1650 else (
1651 state.text <- "";
1652 state.mode <- Outline (allowdel, active, first, outlines, "");
1653 Glut.postRedisplay ();
1656 | 18 | 19 ->
1657 let incr = if key = 18 then -1 else 1 in
1658 let active, first =
1659 match search (active + incr) qsearch incr with
1660 | None ->
1661 state.text <- qsearch ^ " [not found]";
1662 active, first
1663 | Some active ->
1664 state.text <- qsearch;
1665 active, firstof active
1667 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1668 Glut.postRedisplay ();
1670 | 8 ->
1671 let len = String.length qsearch in
1672 if len = 0
1673 then ()
1674 else (
1675 if len = 1
1676 then (
1677 state.text <- "";
1678 state.mode <- Outline (allowdel, active, first, outlines, "");
1680 else
1681 let qsearch = String.sub qsearch 0 (len - 1) in
1682 let active, first =
1683 match search active qsearch ~-1 with
1684 | None ->
1685 state.text <- qsearch ^ " [not found]";
1686 active, first
1687 | Some active ->
1688 state.text <- qsearch;
1689 active, firstof active
1691 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1693 Glut.postRedisplay ()
1695 | 13 ->
1696 if active < Array.length outlines
1697 then (
1698 let (_, _, n, t) = outlines.(active) in
1699 gotopage n t;
1701 state.text <- "";
1702 if allowdel then state.bookmarks <- Array.to_list outlines;
1703 state.mode <- View;
1704 Glut.postRedisplay ();
1706 | _ when key >= 32 && key < 127 ->
1707 let pattern = addchar qsearch (Char.chr key) in
1708 let active, first =
1709 match search active pattern 1 with
1710 | None ->
1711 state.text <- pattern ^ " [not found]";
1712 active, first
1713 | Some active ->
1714 state.text <- pattern;
1715 active, firstof active
1717 state.mode <- Outline (allowdel, active, first, outlines, pattern);
1718 Glut.postRedisplay ()
1720 | 14 when not allowdel -> (* ctrl-n *)
1721 if String.length qsearch > 0
1722 then (
1723 let optoutlines = narrow outlines qsearch in
1724 begin match optoutlines with
1725 | None -> state.text <- "can't narrow"
1726 | Some outlines ->
1727 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1728 match state.outlines with
1729 | Olist l -> ()
1730 | Oarray a ->
1731 state.outlines <- Onarrow (qsearch, outlines, a)
1732 | Onarrow (pat, a, b) ->
1733 state.outlines <- Onarrow (qsearch, outlines, b)
1734 end;
1736 Glut.postRedisplay ()
1738 | 21 when not allowdel -> (* ctrl-u *)
1739 let outline =
1740 match state.outlines with
1741 | Oarray a -> a
1742 | Olist l ->
1743 let a = Array.of_list (List.rev l) in
1744 state.outlines <- Oarray a;
1746 | Onarrow (pat, a, b) ->
1747 state.outlines <- Oarray b;
1748 state.text <- "";
1751 state.mode <- Outline (allowdel, 0, 0, outline, qsearch);
1752 Glut.postRedisplay ()
1754 | 12 ->
1755 state.mode <- Outline
1756 (allowdel, active, firstof active, outlines, qsearch);
1757 Glut.postRedisplay ()
1759 | 127 when allowdel ->
1760 let len = Array.length outlines - 1 in
1761 if len = 0
1762 then (
1763 state.mode <- View;
1764 state.bookmarks <- [];
1766 else (
1767 let bookmarks = Array.init len
1768 (fun i ->
1769 let i = if i >= active then i + 1 else i in
1770 outlines.(i)
1773 state.mode <-
1774 Outline (
1775 allowdel,
1776 min active (len-1),
1777 min first (len-1),
1778 bookmarks, qsearch
1781 Glut.postRedisplay ()
1783 | _ -> log "unknown key %d" key
1786 let keyboard ~key ~x ~y =
1787 if key = 7
1788 then
1789 wcmd "interrupt" []
1790 else
1791 match state.mode with
1792 | Outline outline -> outlinekeyboard ~key ~x ~y outline
1793 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
1794 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
1795 | View -> viewkeyboard ~key ~x ~y
1798 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno) =
1799 match key with
1800 | Glut.KEY_UP ->
1801 let pageno = max 0 (pageno - 1) in
1802 let rec loop = function
1803 | [] -> gotopage1nonav pageno 0
1804 | l :: _ when l.pageno = pageno ->
1805 if l.pagedispy >= 0 && l.pagey = 0
1806 then Glut.postRedisplay ()
1807 else gotopage1nonav pageno 0
1808 | _ :: rest -> loop rest
1810 loop state.layout;
1811 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno)
1813 | Glut.KEY_DOWN ->
1814 let pageno = min (state.pagecount - 1) (pageno + 1) in
1815 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno);
1816 let rec loop = function
1817 | [] ->
1818 let y, h = getpageyh pageno in
1819 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1820 gotoy (clamp dy)
1821 | l :: rest when l.pageno = pageno ->
1822 if l.pagevh != l.pageh
1823 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1824 else Glut.postRedisplay ()
1825 | l :: rest -> loop rest
1827 loop state.layout
1829 | Glut.KEY_PAGE_UP ->
1830 begin match state.layout with
1831 | l :: _ ->
1832 if l.pagey != 0
1833 then (
1834 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno);
1835 gotopage1nonav l.pageno 0;
1837 else (
1838 let layout = layout (state.y-conf.winh) conf.winh in
1839 match layout with
1840 | [] -> gotoy (clamp (-conf.winh))
1841 | l :: _ ->
1842 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno);
1843 gotopage1nonav l.pageno 0
1846 | [] -> gotoy (clamp (-conf.winh))
1847 end;
1849 | Glut.KEY_PAGE_DOWN ->
1850 begin match List.rev state.layout with
1851 | l :: _ ->
1852 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno);
1853 gotoy (clamp (l.pagedispy + l.pageh))
1854 | [] -> gotoy (clamp conf.winh)
1855 end;
1857 | Glut.KEY_HOME ->
1858 state.mode <- Birdseye (conf, leftx, 0, hooverpageno);
1859 gotopage1nonav 0 0
1861 | Glut.KEY_END ->
1862 let pageno = state.pagecount - 1 in
1863 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno);
1864 if not (pagevisible state.layout pageno)
1865 then
1866 let h =
1867 match List.rev state.pdims with
1868 | [] -> conf.winh
1869 | (_, _, h, _) :: _ -> h
1871 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1872 else Glut.postRedisplay ();
1873 | _ -> ()
1876 let special ~key ~x ~y =
1877 match state.mode with
1878 | View | (Birdseye _) when key = Glut.KEY_F9 ->
1879 togglebirdseye ();
1880 reshape conf.winw conf.winh;
1882 | Birdseye vals ->
1883 birdseyespecial key x y vals
1885 | View | Textentry _ ->
1886 begin match state.mode with
1887 | View ->
1888 let y =
1889 match key with
1890 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1891 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1892 | Glut.KEY_DOWN -> clamp conf.scrollincr
1893 | Glut.KEY_PAGE_UP ->
1894 if Glut.getModifiers () land Glut.active_ctrl != 0
1895 then
1896 match state.layout with
1897 | [] -> state.y
1898 | l :: _ -> state.y - l.pagey
1899 else
1900 clamp (-conf.winh)
1901 | Glut.KEY_PAGE_DOWN ->
1902 if Glut.getModifiers () land Glut.active_ctrl != 0
1903 then
1904 match List.rev state.layout with
1905 | [] -> state.y
1906 | l :: _ -> getpagey l.pageno
1907 else
1908 clamp conf.winh
1909 | Glut.KEY_HOME -> addnav (); 0
1910 | Glut.KEY_END ->
1911 addnav ();
1912 state.maxy - (if conf.maxhfit then conf.winh else 0)
1914 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1915 state.x <- state.x - 10;
1916 state.y
1917 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1918 state.x <- state.x + 10;
1919 state.y
1921 | _ -> state.y
1923 gotoy_and_clear_text y
1925 | Textentry
1926 ((c, s, (Some (action, _) as onhist), onkey, ondone), mode) ->
1927 let s =
1928 match key with
1929 | Glut.KEY_UP -> action HCprev
1930 | Glut.KEY_DOWN -> action HCnext
1931 | Glut.KEY_HOME -> action HCfirst
1932 | Glut.KEY_END -> action HClast
1933 | _ -> state.text
1935 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
1936 Glut.postRedisplay ()
1938 | _ -> ()
1941 | Outline (allowdel, active, first, outlines, qsearch) ->
1942 let maxrows = maxoutlinerows () in
1943 let calcfirst first active =
1944 if active > first
1945 then
1946 let rows = active - first in
1947 if rows > maxrows then active - maxrows else first
1948 else active
1950 let navigate incr =
1951 let active = active + incr in
1952 let active = max 0 (min active (Array.length outlines - 1)) in
1953 let first = calcfirst first active in
1954 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1955 Glut.postRedisplay ()
1957 let updownlevel incr =
1958 let len = Array.length outlines in
1959 let (_, curlevel, _, _) = outlines.(active) in
1960 let rec flow i =
1961 if i = len then i-1 else if i = -1 then 0 else
1962 let (_, l, _, _) = outlines.(i) in
1963 if l != curlevel then i else flow (i+incr)
1965 let active = flow active in
1966 let first = calcfirst first active in
1967 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1968 Glut.postRedisplay ()
1970 match key with
1971 | Glut.KEY_UP -> navigate ~-1
1972 | Glut.KEY_DOWN -> navigate 1
1973 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1974 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1976 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1977 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1979 | Glut.KEY_HOME ->
1980 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1981 Glut.postRedisplay ()
1983 | Glut.KEY_END ->
1984 let active = Array.length outlines - 1 in
1985 let first = max 0 (active - maxrows) in
1986 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1987 Glut.postRedisplay ()
1989 | _ -> ()
1992 let drawplaceholder l =
1993 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1994 GlDraw.rect
1995 (float l.pagex, float l.pagedispy)
1996 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1998 let x = float (if margin < 0 then -margin else l.pagex)
1999 and y = float (l.pagedispy + 13) in
2000 let font = Glut.BITMAP_8_BY_13 in
2001 GlDraw.color (0.0, 0.0, 0.0);
2002 GlPix.raster_pos ~x ~y ();
2003 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2004 ("Loading " ^ string_of_int (l.pageno + 1));
2007 let now () = Unix.gettimeofday ();;
2009 let drawpage l =
2010 let color =
2011 match state.mode with
2012 | Textentry _ -> scalecolor 0.4
2013 | View | Outline _ -> scalecolor 1.0
2014 | Birdseye (_, _, pageno, hooverpageno) ->
2015 if l.pageno = pageno
2016 then scalecolor 1.0
2017 else (
2018 if l.pageno = hooverpageno
2019 then scalecolor 0.9
2020 else scalecolor 0.8
2023 GlDraw.color color;
2024 begin match getopaque l.pageno with
2025 | Some (opaque, _) when validopaque opaque ->
2026 let a = now () in
2027 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2028 opaque;
2029 let b = now () in
2030 let d = b-.a in
2031 vlog "draw %d %f sec" l.pageno d;
2033 | _ ->
2034 drawplaceholder l;
2035 end;
2038 let scrollph y =
2039 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2040 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2041 let sh = float conf.winh /. sh in
2042 let sh = max sh (float conf.scrollh) in
2044 let percent =
2045 if state.y = state.maxy
2046 then 1.0
2047 else float y /. float maxy
2049 let position = (float conf.winh -. sh) *. percent in
2051 let position =
2052 if position +. sh > float conf.winh
2053 then float conf.winh -. sh
2054 else position
2056 position, sh;
2059 let scrollindicator () =
2060 GlDraw.color (0.64 , 0.64, 0.64);
2061 GlDraw.rect
2062 (float (conf.winw - conf.scrollw), 0.)
2063 (float conf.winw, float conf.winh)
2065 GlDraw.color (0.0, 0.0, 0.0);
2067 let position, sh = scrollph state.y in
2068 GlDraw.rect
2069 (float (conf.winw - conf.scrollw), position)
2070 (float conf.winw, position +. sh)
2074 let showsel margin =
2075 match state.mstate with
2076 | Mnone | Mscroll _ | Mpan _ ->
2079 | Msel ((x0, y0), (x1, y1)) ->
2080 let rec loop = function
2081 | l :: ls ->
2082 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2083 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2084 then
2085 match getopaque l.pageno with
2086 | Some (opaque, _) when validopaque opaque ->
2087 let oy = -l.pagey + l.pagedispy in
2088 seltext opaque
2089 (x0 - margin - state.x, y0,
2090 x1 - margin - state.x, y1) oy;
2092 | _ -> ()
2093 else loop ls
2094 | [] -> ()
2096 loop state.layout
2099 let showrects () =
2100 let panx = float state.x in
2101 Gl.enable `blend;
2102 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2103 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2104 List.iter
2105 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2106 List.iter (fun l ->
2107 if l.pageno = pageno
2108 then (
2109 let d = float (l.pagedispy - l.pagey) in
2110 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2111 GlDraw.begins `quads;
2113 GlDraw.vertex2 (x0+.panx, y0+.d);
2114 GlDraw.vertex2 (x1+.panx, y1+.d);
2115 GlDraw.vertex2 (x2+.panx, y2+.d);
2116 GlDraw.vertex2 (x3+.panx, y3+.d);
2118 GlDraw.ends ();
2120 ) state.layout
2121 ) state.rects
2123 Gl.disable `blend;
2126 let showoutline = function
2127 | Outline (allowdel, active, first, outlines, qsearch) ->
2128 Gl.enable `blend;
2129 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2130 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2131 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2132 Gl.disable `blend;
2134 GlDraw.color (1., 1., 1.);
2135 let font = Glut.BITMAP_9_BY_15 in
2136 let draw_string x y s =
2137 GlPix.raster_pos ~x ~y ();
2138 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2140 let rec loop row =
2141 if row = Array.length outlines || (row - first) * 16 > conf.winh
2142 then ()
2143 else (
2144 let (s, l, _, _) = outlines.(row) in
2145 let y = (row - first) * 16 in
2146 let x = 5 + 15*l in
2147 if row = active
2148 then (
2149 Gl.enable `blend;
2150 GlDraw.polygon_mode `both `line;
2151 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2152 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2153 GlDraw.rect (0., float (y + 1))
2154 (float (conf.winw - 1), float (y + 18));
2155 GlDraw.polygon_mode `both `fill;
2156 Gl.disable `blend;
2157 GlDraw.color (1., 1., 1.);
2159 draw_string (float x) (float (y + 16)) s;
2160 loop (row+1)
2163 loop first
2165 | _ -> ()
2168 let display () =
2169 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2170 GlDraw.viewport margin 0 state.w conf.winh;
2171 pagematrix ();
2172 GlClear.color (scalecolor 0.5);
2173 GlClear.clear [`color];
2174 if state.x != 0
2175 then (
2176 let x = float state.x in
2177 GlMat.translate ~x ();
2179 if conf.zoom > 1.0
2180 then (
2181 Gl.enable `scissor_test;
2182 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2184 List.iter drawpage state.layout;
2185 if conf.zoom > 1.0
2186 then
2187 Gl.disable `scissor_test
2189 if state.x != 0
2190 then (
2191 let x = -.float state.x in
2192 GlMat.translate ~x ();
2194 showrects ();
2195 showsel margin;
2196 GlDraw.viewport 0 0 conf.winw conf.winh;
2197 winmatrix ();
2198 scrollindicator ();
2199 showoutline state.mode;
2200 enttext ();
2201 Glut.swapBuffers ();
2204 let getunder x y =
2205 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2206 let x = x - margin - state.x in
2207 let rec f = function
2208 | l :: rest ->
2209 begin match getopaque l.pageno with
2210 | Some (opaque, _) when validopaque opaque ->
2211 let y = y - l.pagedispy in
2212 if y > 0
2213 then
2214 let y = l.pagey + y in
2215 let x = x - l.pagex in
2216 match whatsunder opaque x y with
2217 | Unone -> f rest
2218 | under -> under
2219 else
2220 f rest
2221 | _ ->
2222 f rest
2224 | [] -> Unone
2226 f state.layout
2229 let viewmouse button bstate x y =
2230 match button with
2231 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2232 let incr =
2233 if n = 3
2234 then
2235 -conf.scrollincr
2236 else
2237 conf.scrollincr
2239 let incr = incr * 2 in
2240 let y = clamp incr in
2241 gotoy_and_clear_text y
2243 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2244 if bstate = Glut.DOWN
2245 then (
2246 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2247 state.mstate <- Mpan (x, y)
2249 else
2250 state.mstate <- Mnone
2252 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2253 if bstate = Glut.DOWN
2254 then
2255 let position, sh = scrollph state.y in
2256 if y > truncate position && y < truncate (position +. sh)
2257 then
2258 state.mstate <- Mscroll
2259 else
2260 let percent = float y /. float conf.winh in
2261 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2262 gotoy desty;
2263 state.mstate <- Mscroll
2264 else
2265 state.mstate <- Mnone
2267 | Glut.LEFT_BUTTON ->
2268 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2269 begin match dest with
2270 | Ulinkgoto (pageno, top) ->
2271 if pageno >= 0
2272 then
2273 gotopage1 pageno top
2275 | Ulinkuri s ->
2276 print_endline s
2278 | Unone when bstate = Glut.DOWN ->
2279 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2280 state.mstate <- Mpan (x, y);
2282 | Unone | Utext _ ->
2283 if bstate = Glut.DOWN
2284 then (
2285 if conf.angle mod 360 = 0
2286 then (
2287 state.mstate <- Msel ((x, y), (x, y));
2288 Glut.postRedisplay ()
2291 else (
2292 match state.mstate with
2293 | Mnone -> ()
2295 | Mscroll ->
2296 state.mstate <- Mnone
2298 | Mpan _ ->
2299 Glut.setCursor Glut.CURSOR_INHERIT;
2300 state.mstate <- Mnone
2302 | Msel ((x0, y0), (x1, y1)) ->
2303 let f l =
2304 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2305 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2306 then
2307 match getopaque l.pageno with
2308 | Some (opaque, _) when validopaque opaque ->
2309 copysel opaque
2310 | _ -> ()
2312 List.iter f state.layout;
2313 copysel ""; (* ugly *)
2314 Glut.setCursor Glut.CURSOR_INHERIT;
2315 state.mstate <- Mnone;
2319 | _ -> ()
2322 let birdseyemouse button bstate x y (conf, leftx, pageno, hooverpageno) =
2323 match button with
2324 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2325 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2326 let rec loop = function
2327 | [] -> ()
2328 | l :: rest ->
2329 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2330 && x > margin && x < margin + l.pagew
2331 then (
2332 birdseyeoff (conf, leftx, l.pageno, hooverpageno);
2333 reshape conf.winw conf.winh;
2334 state.anchor <- (l.pageno, 0.0);
2336 else loop rest
2338 loop state.layout
2339 | _ -> ()
2342 let mouse bstate button x y =
2343 match state.mode with
2344 | View -> viewmouse button bstate x y
2345 | Birdseye beye -> birdseyemouse button bstate x y beye
2346 | Textentry _ -> ()
2347 | Outline _ -> ()
2350 let mouse ~button ~state ~x ~y = mouse state button x y;;
2352 let motion ~x ~y =
2353 match state.mode with
2354 | Outline _ -> ()
2355 | _ ->
2356 match state.mstate with
2357 | Mnone -> ()
2359 | Mpan (x0, y0) ->
2360 let dx = x - x0
2361 and dy = y0 - y in
2362 state.mstate <- Mpan (x, y);
2363 if conf.zoom > 1.0 then state.x <- state.x + dx;
2364 let y = clamp dy in
2365 gotoy_and_clear_text y
2367 | Msel (a, _) ->
2368 state.mstate <- Msel (a, (x, y));
2369 Glut.postRedisplay ()
2371 | Mscroll ->
2372 let y = min conf.winh (max 0 y) in
2373 let percent = float y /. float conf.winh in
2374 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2375 gotoy_and_clear_text y
2378 let pmotion ~x ~y =
2379 match state.mode with
2380 | Birdseye (conf, leftx, pageno, hooverpageno) ->
2381 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2382 let rec loop = function
2383 | [] ->
2384 if hooverpageno != -1
2385 then (
2386 state.mode <- Birdseye (conf, leftx, pageno, -1);
2387 Glut.postRedisplay ();
2389 | l :: rest ->
2390 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2391 && x > margin && x < margin + l.pagew
2392 then (
2393 state.mode <- Birdseye (conf, leftx, pageno, l.pageno);
2394 Glut.postRedisplay ();
2396 else loop rest
2398 loop state.layout
2400 | Outline _ -> ()
2401 | _ ->
2402 match state.mstate with
2403 | Mnone ->
2404 begin match getunder x y with
2405 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2406 | Ulinkuri uri ->
2407 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2408 Glut.setCursor Glut.CURSOR_INFO
2409 | Ulinkgoto (page, y) ->
2410 if conf.underinfo
2411 then showtext 'p' ("age: " ^ string_of_int page);
2412 Glut.setCursor Glut.CURSOR_INFO
2413 | Utext s ->
2414 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2415 Glut.setCursor Glut.CURSOR_TEXT
2418 | Mpan _ | Msel _ | Mscroll ->
2423 module State =
2424 struct
2425 open Parser
2427 let home =
2429 match Sys.os_type with
2430 | "Win32" -> Sys.getenv "HOMEPATH"
2431 | _ -> Sys.getenv "HOME"
2432 with exn ->
2433 prerr_endline
2434 ("Can not determine home directory location: " ^
2435 Printexc.to_string exn);
2439 let config_of c attrs =
2440 let apply c k v =
2442 match k with
2443 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2444 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2445 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2446 | "preload" -> { c with preload = bool_of_string v }
2447 | "page-bias" -> { c with pagebias = int_of_string v }
2448 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2449 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2450 | "crop-hack" -> { c with crophack = bool_of_string v }
2451 | "throttle" -> { c with showall = bool_of_string v }
2452 | "highlight-links" -> { c with hlinks = bool_of_string v }
2453 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2454 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2455 | "zoom" ->
2456 let zoom = float_of_string v /. 100. in
2457 let zoom = max 0.01 (min 2.2 zoom) in
2458 { c with zoom = zoom }
2459 | "presentation" -> { c with presentation = bool_of_string v }
2460 | "rotation-angle" -> { c with angle = int_of_string v }
2461 | "width" -> { c with winw = max 20 (int_of_string v) }
2462 | "height" -> { c with winh = max 20 (int_of_string v) }
2463 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2464 | "proportional-display" -> { c with proportional = bool_of_string v }
2465 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2466 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2467 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2468 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2469 | _ -> c
2470 with exn ->
2471 prerr_endline ("Error processing attribute (`" ^
2472 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2475 let rec fold c = function
2476 | [] -> c
2477 | (k, v) :: rest ->
2478 let c = apply c k v in
2479 fold c rest
2481 fold c attrs;
2484 let bookmark_of attrs =
2485 let rec fold title page rely = function
2486 | ("title", v) :: rest -> fold v page rely rest
2487 | ("page", v) :: rest -> fold title v rely rest
2488 | ("rely", v) :: rest -> fold title page v rest
2489 | _ :: rest -> fold title page rely rest
2490 | [] -> title, page, rely
2492 fold "invalid" "0" "0" attrs
2495 let setconf dst src =
2496 dst.scrollw <- src.scrollw;
2497 dst.scrollh <- src.scrollh;
2498 dst.icase <- src.icase;
2499 dst.preload <- src.preload;
2500 dst.pagebias <- src.pagebias;
2501 dst.verbose <- src.verbose;
2502 dst.scrollincr <- src.scrollincr;
2503 dst.maxhfit <- src.maxhfit;
2504 dst.crophack <- src.crophack;
2505 dst.autoscroll <- src.autoscroll;
2506 dst.showall <- src.showall;
2507 dst.hlinks <- src.hlinks;
2508 dst.underinfo <- src.underinfo;
2509 dst.interpagespace <- src.interpagespace;
2510 dst.zoom <- src.zoom;
2511 dst.presentation <- src.presentation;
2512 dst.angle <- src.angle;
2513 dst.winw <- src.winw;
2514 dst.winh <- src.winh;
2515 dst.savebmarks <- src.savebmarks;
2516 dst.memlimit <- src.memlimit;
2517 dst.proportional <- src.proportional;
2518 dst.texcount <- src.texcount;
2519 dst.sliceheight <- src.sliceheight;
2520 dst.thumbw <- src.thumbw;
2523 let unent s =
2524 let l = String.length s in
2525 let b = Buffer.create l in
2526 unent b s 0 l;
2527 Buffer.contents b;
2530 let get s =
2531 let h = Hashtbl.create 10 in
2532 let dc = { defconf with angle = defconf.angle } in
2533 let rec toplevel v t spos epos =
2534 match t with
2535 | Vdata | Vcdata | Vend -> v
2536 | Vopen ("llppconfig", attrs, closed) ->
2537 if closed
2538 then v
2539 else { v with f = llppconfig }
2540 | Vopen _ ->
2541 error "unexpected subelement at top level" s spos
2542 | Vclose tag -> error "unexpected close at top level" s spos
2544 and llppconfig v t spos epos =
2545 match t with
2546 | Vdata | Vcdata | Vend -> v
2547 | Vopen ("defaults", attrs, closed) ->
2548 let c = config_of dc attrs in
2549 setconf dc c;
2550 if closed
2551 then v
2552 else { v with f = skip "defaults" (fun () -> v) }
2554 | Vopen ("doc", attrs, closed) ->
2555 let pathent =
2557 List.assoc "path" attrs
2558 with Not_found -> error "doc is missing path attribute" s spos
2560 let path = unent pathent in
2561 let c = config_of dc attrs in
2562 let pageno, rely, x =
2563 let safef f n v d =
2564 try f v
2565 with exn ->
2566 dolog "error accessing %s (%S) at postion %d:\n %s"
2567 n v spos (Printexc.to_string exn);
2570 let rec fold pageno rely x = function
2571 | [] -> pageno, rely, x
2572 | ("rely", v) :: rest ->
2573 fold pageno (safef float_of_string "rely" v 0.0) x rest
2574 | ("page", v) :: rest ->
2575 fold (safef int_of_string "page" v 0) rely x rest
2576 | ("x", v) :: rest ->
2577 fold pageno rely (safef int_of_string "x" v 0) rest
2578 | _ :: rest ->
2579 fold pageno rely x rest
2581 fold 0 0.0 0 attrs
2583 let anchor = (pageno, rely) in
2584 if closed
2585 then (Hashtbl.add h path (c, [], x, anchor); v)
2586 else { v with f = doc path x anchor c [] }
2588 | Vopen (tag, _, closed) ->
2589 error "unexpected subelement in llppconfig" s spos
2591 | Vclose "llppconfig" -> { v with f = toplevel }
2592 | Vclose tag -> error "unexpected close in llppconfig" s spos
2594 and doc path x anchor c bookmarks v t spos epos =
2595 match t with
2596 | Vdata | Vcdata -> v
2597 | Vend -> error "unexpected end of input in doc" s spos
2598 | Vopen ("bookmarks", attrs, closed) ->
2599 { v with f = pbookmarks path x anchor c bookmarks }
2601 | Vopen (tag, _, _) ->
2602 error "unexpected subelement in doc" s spos
2604 | Vclose "doc" ->
2605 Hashtbl.add h path (c, List.rev bookmarks, x, anchor);
2606 { v with f = llppconfig }
2608 | Vclose tag -> error "unexpected close in doc" s spos
2610 and pbookmarks path x anchor c bookmarks v t spos epos =
2611 match t with
2612 | Vdata | Vcdata -> v
2613 | Vend -> error "unexpected end of input in bookmarks" s spos
2614 | Vopen ("item", attrs, closed) ->
2615 let titleent, spage, srely = bookmark_of attrs in
2616 let page =
2618 int_of_string spage
2619 with exn ->
2620 dolog "Failed to convert page %S to integer: %s"
2621 spage (Printexc.to_string exn);
2624 let rely =
2626 float_of_string srely
2627 with exn ->
2628 dolog "Failed to convert rely %S to real: %s"
2629 srely (Printexc.to_string exn);
2632 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2633 if closed
2634 then { v with f = pbookmarks path x anchor c bookmarks }
2635 else
2636 let f () = v in
2637 { v with f = skip "item" f }
2639 | Vopen _ ->
2640 error "unexpected subelement in bookmarks" s spos
2642 | Vclose "bookmarks" ->
2643 { v with f = doc path x anchor c bookmarks }
2645 | Vclose tag -> error "unexpected close in bookmarks" s spos
2647 and skip tag f v t spos epos =
2648 match t with
2649 | Vdata | Vcdata -> v
2650 | Vend ->
2651 error ("unexpected end of input in skipped " ^ tag) s spos
2652 | Vopen (tag', _, closed) ->
2653 if closed
2654 then v
2655 else
2656 let f' () = { v with f = skip tag f } in
2657 { v with f = skip tag' f' }
2658 | Vclose ctag ->
2659 if tag = ctag
2660 then f ()
2661 else error ("unexpected close in skipped " ^ tag) s spos
2664 parse { f = toplevel; accu = () } s;
2665 h, dc;
2668 let do_load f ic =
2670 let len = in_channel_length ic in
2671 let s = String.create len in
2672 really_input ic s 0 len;
2673 f s;
2674 with
2675 | Parse_error (msg, s, pos) ->
2676 let subs = subs s pos in
2677 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2678 failwith ("parse error: " ^ s)
2680 | exn ->
2681 failwith ("config load error: " ^ Printexc.to_string exn)
2684 let path =
2685 let dir =
2687 let dir = Filename.concat home ".config" in
2688 if Sys.is_directory dir then dir else home
2689 with _ -> home
2691 Filename.concat dir "llpp.conf"
2694 let load1 f =
2695 if Sys.file_exists path
2696 then
2697 match
2698 (try Some (open_in_bin path)
2699 with exn ->
2700 prerr_endline
2701 ("Error opening configuation file `" ^ path ^ "': " ^
2702 Printexc.to_string exn);
2703 None
2705 with
2706 | Some ic ->
2707 begin try
2708 f (do_load get ic)
2709 with exn ->
2710 prerr_endline
2711 ("Error loading configuation from `" ^ path ^ "': " ^
2712 Printexc.to_string exn);
2713 end;
2714 close_in ic;
2716 | None -> ()
2717 else
2718 f (Hashtbl.create 0, defconf)
2721 let load () =
2722 let f (h, dc) =
2723 let pc, pb, px, pa =
2725 Hashtbl.find h (Filename.basename state.path)
2726 with Not_found -> dc, [], 0, (0, 0.0)
2728 setconf defconf dc;
2729 setconf conf pc;
2730 state.bookmarks <- pb;
2731 state.x <- px;
2732 cbput state.hists.nav pa;
2734 load1 f
2737 let add_attrs bb always dc c =
2738 let ob s a b =
2739 if always || a != b
2740 then Printf.bprintf bb "\n %s='%b'" s a
2741 and oi s a b =
2742 if always || a != b
2743 then Printf.bprintf bb "\n %s='%d'" s a
2744 and oz s a b =
2745 if always || a <> b
2746 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2748 let w, h =
2749 if always
2750 then dc.winw, dc.winh
2751 else
2752 match state.fullscreen with
2753 | Some wh -> wh
2754 | None -> c.winw, c.winh
2756 let zoom, presentation, interpagespace, showall=
2757 if always
2758 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2759 else
2760 match state.mode with
2761 | Birdseye (bc, _, _, _) ->
2762 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2763 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2765 oi "width" w dc.winw;
2766 oi "height" h dc.winh;
2767 oi "scroll-bar-width" c.scrollw dc.scrollw;
2768 oi "scroll-handle-height" c.scrollh dc.scrollh;
2769 ob "case-insensitive-search" c.icase dc.icase;
2770 ob "preload" c.preload dc.preload;
2771 oi "page-bias" c.pagebias dc.pagebias;
2772 oi "scroll-step" c.scrollincr dc.scrollincr;
2773 ob "max-height-fit" c.maxhfit dc.maxhfit;
2774 ob "crop-hack" c.crophack dc.crophack;
2775 ob "throttle" showall dc.showall;
2776 ob "highlight-links" c.hlinks dc.hlinks;
2777 ob "under-cursor-info" c.underinfo dc.underinfo;
2778 oi "vertical-margin" interpagespace dc.interpagespace;
2779 oz "zoom" zoom dc.zoom;
2780 ob "presentation" presentation dc.presentation;
2781 oi "rotation-angle" c.angle dc.angle;
2782 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2783 ob "proportional-display" c.proportional dc.proportional;
2784 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2785 oi "texcount" c.texcount dc.texcount;
2786 oi "slice-height" c.sliceheight dc.sliceheight;
2787 oi "thumbnail-width" c.thumbw dc.thumbw;
2790 let save () =
2791 let bb = Buffer.create 32768 in
2792 let f (h, dc) =
2793 Buffer.add_string bb "<llppconfig>\n<defaults ";
2794 add_attrs bb true dc dc;
2795 Buffer.add_string bb "/>\n";
2797 let adddoc path x anchor c bookmarks =
2798 if bookmarks == [] && c = dc && anchor = emptyanchor
2799 then ()
2800 else (
2801 Printf.bprintf bb "<doc path='%s'"
2802 (enent path 0 (String.length path));
2804 if anchor <> emptyanchor
2805 then (
2806 let n, y = anchor in
2807 Printf.bprintf bb " page='%d'" n;
2808 Printf.bprintf bb " rely='%f'" y;
2811 if x != 0
2812 then Printf.bprintf bb " pan='%d'" x;
2814 add_attrs bb false dc c;
2816 begin match bookmarks with
2817 | [] -> Buffer.add_string bb "/>\n"
2818 | _ ->
2819 Buffer.add_string bb ">\n<bookmarks>\n";
2820 List.iter (fun (title, _level, page, rely) ->
2821 Printf.bprintf bb
2822 "<item title='%s' page='%d' rely='%f'/>\n"
2823 (enent title 0 (String.length title))
2824 page
2825 rely
2826 ) bookmarks;
2827 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2828 end;
2832 let x =
2833 match state.mode with
2834 | Birdseye (_, x, _, _) -> x
2835 | _ -> state.x
2837 let basename = Filename.basename state.path in
2838 adddoc basename x (getanchor ()) conf
2839 (if conf.savebmarks then state.bookmarks else []);
2841 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2842 if basename <> path
2843 then adddoc path x y c bookmarks
2844 ) h;
2845 Buffer.add_string bb "</llppconfig>";
2847 load1 f;
2848 if Buffer.length bb > 0
2849 then
2851 let tmp = path ^ ".tmp" in
2852 let oc = open_out_bin tmp in
2853 Buffer.output_buffer oc bb;
2854 close_out oc;
2855 Sys.rename tmp path;
2856 with exn ->
2857 prerr_endline
2858 ("error while saving configuration: " ^ Printexc.to_string exn)
2860 end;;
2862 let () =
2863 Arg.parse
2864 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2865 (fun s -> state.path <- s)
2866 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2868 if String.length state.path = 0
2869 then (prerr_endline "filename missing"; exit 1);
2871 State.load ();
2873 let _ = Glut.init Sys.argv in
2874 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2875 let () = Glut.initWindowSize conf.winw conf.winh in
2876 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
2878 let csock, ssock =
2879 if Sys.os_type = "Unix"
2880 then
2881 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2882 else
2883 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2884 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2885 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2886 Unix.bind sock addr;
2887 Unix.listen sock 1;
2888 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2889 Unix.connect csock addr;
2890 let ssock, _ = Unix.accept sock in
2891 Unix.close sock;
2892 let opts sock =
2893 Unix.setsockopt sock Unix.TCP_NODELAY true;
2894 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2896 opts ssock;
2897 opts csock;
2898 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2899 ssock, csock
2902 let () = Glut.displayFunc display in
2903 let () = Glut.reshapeFunc reshape in
2904 let () = Glut.keyboardFunc keyboard in
2905 let () = Glut.specialFunc special in
2906 let () = Glut.idleFunc (Some idle) in
2907 let () = Glut.mouseFunc mouse in
2908 let () = Glut.motionFunc motion in
2909 let () = Glut.passiveMotionFunc pmotion in
2911 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2912 state.csock <- csock;
2913 state.ssock <- ssock;
2914 state.text <- "Opening " ^ state.path;
2915 writeopen state.path state.password;
2917 at_exit State.save;
2919 let rec handlelablglutbug () =
2921 Glut.mainLoop ();
2922 with Glut.BadEnum "key in special_of_int" ->
2923 showtext '!' " LablGlut bug: special key not recognized";
2924 handlelablglutbug ()
2926 handlelablglutbug ();