Remove log function, dolog does the same
[llpp.git] / main.ml
blobf92febeef52dbe17ff2b49e2e291cc26e49a083f
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 'a circbuf =
45 { store : 'a array
46 ; mutable rc : int
47 ; mutable wc : int
48 ; mutable len : int
52 type textentry = (char * string * onhist * onkey * ondone)
53 and onkey = string -> int -> te
54 and ondone = string -> unit
55 and histcancel = unit -> unit
56 and onhist = ((histcmd -> string) * histcancel) option
57 and histcmd = HCnext | HCprev | HCfirst | HClast
58 and te =
59 | TEstop
60 | TEdone of string
61 | TEcont of string
62 | TEswitch of textentry
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 scrollincr : int
138 ; mutable maxhfit : bool
139 ; mutable crophack : bool
140 ; mutable autoscroll : bool
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)
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 ; hists : hists
214 and hists =
215 { pat : string circbuf
216 ; pag : string circbuf
217 ; nav : anchor circbuf
221 let defconf =
222 { scrollw = 7
223 ; scrollh = 12
224 ; icase = true
225 ; preload = true
226 ; pagebias = 0
227 ; verbose = false
228 ; scrollincr = 24
229 ; maxhfit = true
230 ; crophack = false
231 ; autoscroll = false
232 ; showall = false
233 ; hlinks = false
234 ; underinfo = false
235 ; interpagespace = 2
236 ; zoom = 1.0
237 ; presentation = false
238 ; angle = 0
239 ; winw = 900
240 ; winh = 900
241 ; savebmarks = true
242 ; proportional = true
243 ; memlimit = 32*1024*1024
244 ; texcount = 256
245 ; sliceheight = 24
246 ; thumbw = 76
250 let conf = { defconf with angle = defconf.angle };;
252 let state =
253 { csock = Unix.stdin
254 ; ssock = Unix.stdin
255 ; x = 0
256 ; y = 0
257 ; anchor = (0, 0.0)
258 ; w = 0
259 ; layout = []
260 ; maxy = max_int
261 ; pagemap = Hashtbl.create 10
262 ; pagecache = cbnew 100 ""
263 ; pdims = []
264 ; pagecount = 0
265 ; rendering = false
266 ; mstate = Mnone
267 ; rects = []
268 ; rects1 = []
269 ; text = ""
270 ; mode = View
271 ; fullscreen = None
272 ; searchpattern = ""
273 ; outlines = Olist []
274 ; bookmarks = []
275 ; path = ""
276 ; password = ""
277 ; invalidated = 0
278 ; hists =
279 { nav = cbnew 100 (0, 0.0)
280 ; pat = cbnew 20 ""
281 ; pag = cbnew 10 ""
283 ; colorscale = 1.0
284 ; memused = 0
285 ; gen = 0
286 ; throttle = None
290 let vlog fmt =
291 if conf.verbose
292 then
293 Printf.kprintf prerr_endline fmt
294 else
295 Printf.kprintf ignore fmt
298 let writecmd fd s =
299 let len = String.length s in
300 let n = 4 + len in
301 let b = Buffer.create n in
302 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
303 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
304 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
305 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
306 Buffer.add_string b s;
307 let s' = Buffer.contents b in
308 let n' = Unix.write fd s' 0 n in
309 if n' != n then failwith "write failed";
312 let readcmd fd =
313 let s = "xxxx" in
314 let n = Unix.read fd s 0 4 in
315 if n != 4 then failwith "incomplete read(len)";
316 let len = 0
317 lor (Char.code s.[0] lsl 24)
318 lor (Char.code s.[1] lsl 16)
319 lor (Char.code s.[2] lsl 8)
320 lor (Char.code s.[3] lsl 0)
322 let s = String.create len in
323 let n = Unix.read fd s 0 len in
324 if n != len then failwith "incomplete read(data)";
328 let makecmd s l =
329 let b = Buffer.create 10 in
330 Buffer.add_string b s;
331 let rec combine = function
332 | [] -> b
333 | x :: xs ->
334 Buffer.add_char b ' ';
335 let s =
336 match x with
337 | `b b -> if b then "1" else "0"
338 | `s s -> s
339 | `i i -> string_of_int i
340 | `f f -> string_of_float f
341 | `I f -> string_of_int (truncate f)
343 Buffer.add_string b s;
344 combine xs;
346 combine l;
349 let wcmd s l =
350 let cmd = Buffer.contents (makecmd s l) in
351 writecmd state.csock cmd;
354 let calcips h =
355 if conf.presentation
356 then
357 let d = conf.winh - h in
358 max 0 ((d + 1) / 2)
359 else
360 conf.interpagespace
363 let calcheight () =
364 let rec f pn ph pi fh l =
365 match l with
366 | (n, _, h, _) :: rest ->
367 let ips = calcips h in
368 let fh =
369 if conf.presentation
370 then fh+ips
371 else (
372 if isbirdseye state.mode && pn = 0
373 then fh + ips
374 else fh
377 let fh = fh + ((n - pn) * (ph + pi)) in
378 f n h ips fh rest;
380 | [] ->
381 let inc =
382 if conf.presentation || (isbirdseye state.mode && pn = 0)
383 then 0
384 else -pi
386 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
387 max 0 fh
389 let fh = f 0 0 0 0 state.pdims in
393 let getpageyh pageno =
394 let rec f pn ph pi y l =
395 match l with
396 | (n, _, h, _) :: rest ->
397 let ips = calcips h in
398 if n >= pageno
399 then
400 let h = if n = pageno then h else ph in
401 if conf.presentation && n = pageno
402 then
403 y + (pageno - pn) * (ph + pi) + pi, h
404 else
405 y + (pageno - pn) * (ph + pi), h
406 else
407 let y = y + (if conf.presentation then pi else 0) in
408 let y = y + (n - pn) * (ph + pi) in
409 f n h ips y rest
411 | [] ->
412 y + (pageno - pn) * (ph + pi), ph
414 f 0 0 0 0 state.pdims
417 let getpagey pageno = fst (getpageyh pageno);;
419 let layout y sh =
420 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
421 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
422 match pdims with
423 | (pageno', w, h, x) :: rest when pageno' = pageno ->
424 let ips = calcips h in
425 let yinc =
426 if conf.presentation || (isbirdseye state.mode && pageno = 0)
427 then ips
428 else 0
430 (w, h, ips, x), rest, pdimno + 1, yinc
431 | _ ->
432 prev, pdims, pdimno, 0
434 let dy = dy + yinc in
435 let py = py + yinc in
436 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
437 then
438 accu
439 else
440 let vy = y + dy in
441 if py + h <= vy - yinc
442 then
443 let py = py + h + ips in
444 let dy = max 0 (py - y) in
445 f ~pageno:(pageno+1)
446 ~pdimno
447 ~prev:curr
450 ~pdims:rest
451 ~cacheleft
452 ~accu
453 else
454 let pagey = vy - py in
455 let pagevh = h - pagey in
456 let pagevh = min (sh - dy) pagevh in
457 let off = if yinc > 0 then py - vy else 0 in
458 let py = py + h + ips in
459 let e =
460 { pageno = pageno
461 ; pagedimno = pdimno
462 ; pagew = w
463 ; pageh = h
464 ; pagedispy = dy + off
465 ; pagey = pagey + off
466 ; pagevh = pagevh - off
467 ; pagex = x
470 let accu = e :: accu in
471 f ~pageno:(pageno+1)
472 ~pdimno
473 ~prev:curr
475 ~dy:(dy+pagevh+ips)
476 ~pdims:rest
477 ~cacheleft:(cacheleft-1)
478 ~accu
480 if state.invalidated = 0
481 then (
482 let accu =
484 ~pageno:0
485 ~pdimno:~-1
486 ~prev:(0,0,0,0)
487 ~py:0
488 ~dy:0
489 ~pdims:state.pdims
490 ~cacheleft:(cbcap state.pagecache)
491 ~accu:[]
493 List.rev accu
495 else
499 let clamp incr =
500 let y = state.y + incr in
501 let y = max 0 y in
502 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
506 let getopaque pageno =
507 try Some (Hashtbl.find state.pagemap
508 (pageno, state.w, conf.angle, conf.proportional, state.gen))
509 with Not_found -> None
512 let cache pageno opaque =
513 Hashtbl.replace state.pagemap
514 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
517 let validopaque opaque = String.length opaque > 0;;
519 let render l =
520 match getopaque l.pageno with
521 | None when not state.rendering ->
522 state.rendering <- true;
523 cache l.pageno ("", -1);
524 wcmd "render" [`i (l.pageno + 1)
525 ;`i l.pagedimno
526 ;`i l.pagew
527 ;`i l.pageh];
529 | _ -> ()
532 let loadlayout layout =
533 let rec f all = function
534 | l :: ls ->
535 begin match getopaque l.pageno with
536 | None -> render l; f false ls
537 | Some (opaque, _) -> f (all && validopaque opaque) ls
539 | [] -> all
541 f (layout <> []) layout;
544 let findpageforopaque opaque =
545 Hashtbl.fold
546 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
547 state.pagemap None
550 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
552 let preload () =
553 let oktopreload =
554 if conf.preload
555 then
556 let memleft = conf.memlimit - state.memused in
557 if memleft < 0
558 then
559 let opaque = cbpeek state.pagecache in
560 match findpageforopaque opaque with
561 | Some ((n, _, _, _, _), size) ->
562 memleft + size >= 0 && not (pagevisible state.layout n)
563 | None -> false
564 else true
565 else false
567 if oktopreload
568 then
569 let presentation = conf.presentation in
570 let interpagespace = conf.interpagespace in
571 let maxy = state.maxy in
572 conf.presentation <- false;
573 conf.interpagespace <- 0;
574 state.maxy <- calcheight ();
575 let y =
576 match state.layout with
577 | [] -> 0
578 | l :: _ -> getpagey l.pageno
580 let y = if y < conf.winh then 0 else y - conf.winh in
581 let pages = layout y (conf.winh*3) in
582 List.iter render pages;
583 conf.presentation <- presentation;
584 conf.interpagespace <- interpagespace;
585 state.maxy <- maxy;
588 let gotoy y =
589 let y = max 0 y in
590 let y = min state.maxy y in
591 let pages = layout y conf.winh in
592 let ready = loadlayout pages in
593 if conf.showall
594 then (
595 if ready
596 then (
597 state.y <- y;
598 state.layout <- pages;
599 state.throttle <- None;
600 Glut.postRedisplay ();
602 else (
603 state.throttle <- Some pages;
606 else (
607 state.y <- y;
608 state.layout <- pages;
609 state.throttle <- None;
610 Glut.postRedisplay ();
612 begin match state.mode with
613 | Birdseye (conf, leftx, pageno, hooverpageno) ->
614 if not (pagevisible pages pageno)
615 then (
616 match state.layout with
617 | [] -> ()
618 | l :: _ ->
619 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno)
621 | _ -> ()
622 end;
623 preload ();
626 let gotoy_and_clear_text y =
627 gotoy y;
628 if not conf.verbose then state.text <- "";
631 let emptyanchor = (0, 0.0);;
633 let getanchor () =
634 match state.layout with
635 | [] -> emptyanchor
636 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
639 let getanchory (n, top) =
640 let y, h = getpageyh n in
641 y + (truncate (top *. float h));
644 let gotoanchor anchor =
645 gotoy (getanchory anchor);
648 let addnav () =
649 cbput state.hists.nav (getanchor ());
652 let getnav () =
653 let anchor = cbgetc state.hists.nav ~-1 in
654 getanchory anchor;
657 let gotopagenonav n top =
658 let y, h = getpageyh n in
659 gotoy_and_clear_text (y + (truncate (top *. float h)));
662 let gotopage1nonav n top =
663 let y, h = getpageyh n in
664 addnav ();
665 gotoy_and_clear_text (y + top);
668 let gotopage n top =
669 let y, h = getpageyh n in
670 addnav ();
671 gotoy_and_clear_text (y + (truncate (top *. float h)));
674 let gotopage1 n top =
675 let y = getpagey n in
676 addnav ();
677 gotoy_and_clear_text (y + top);
680 let invalidate () =
681 state.layout <- [];
682 state.pdims <- [];
683 state.rects <- [];
684 state.rects1 <- [];
685 state.invalidated <- state.invalidated + 1;
688 let scalecolor c =
689 let c = c *. state.colorscale in
690 (c, c, c);
693 let represent () =
694 state.maxy <- calcheight ();
695 match state.mode with
696 | Birdseye (_, _, pageno, _) ->
697 let y, h = getpageyh pageno in
698 let top = (conf.winh - h) / 2 in
699 gotoy (max 0 (y - top))
700 | _ -> gotoanchor state.anchor
703 let pagematrix () =
704 GlMat.mode `projection;
705 GlMat.load_identity ();
706 GlMat.rotate ~x:1.0 ~angle:180.0 ();
707 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
708 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
711 let winmatrix () =
712 GlMat.mode `projection;
713 GlMat.load_identity ();
714 GlMat.rotate ~x:1.0 ~angle:180.0 ();
715 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
716 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
719 let reshape ~w ~h =
720 if state.invalidated = 0
721 then state.anchor <- getanchor ();
723 conf.winw <- w;
724 let w = truncate (float w *. conf.zoom) - conf.scrollw in
725 let w = max w 2 in
726 state.w <- w;
727 conf.winh <- h;
728 GlMat.mode `modelview;
729 GlMat.load_identity ();
730 GlClear.color (scalecolor 1.0);
731 GlClear.clear [`color];
733 invalidate ();
734 wcmd "geometry" [`i w; `i h];
737 let showtext c s =
738 GlDraw.color (0.0, 0.0, 0.0);
739 GlDraw.rect
740 (0.0, float (conf.winh - 18))
741 (float (conf.winw - conf.scrollw - 1), float conf.winh)
743 let font = Glut.BITMAP_8_BY_13 in
744 GlDraw.color (1.0, 1.0, 1.0);
745 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
746 Glut.bitmapCharacter ~font ~c:(Char.code c);
747 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
750 let enttext () =
751 let len = String.length state.text in
752 match state.mode with
753 | Textentry ((c, text, _, _, _), _) ->
754 let s =
755 if len > 0
756 then
757 text ^ " [" ^ state.text ^ "]"
758 else
759 text
761 showtext c s;
763 | _ ->
764 if len > 0 then showtext ' ' state.text
767 let showtext c s =
768 if true
769 then (
770 state.text <- Printf.sprintf "%c%s" c s;
771 Glut.postRedisplay ();
773 else (
774 showtext c s;
775 Glut.swapBuffers ();
779 let act cmd =
780 match cmd.[0] with
781 | 'c' ->
782 state.pdims <- [];
784 | 'D' ->
785 state.rects <- state.rects1;
786 Glut.postRedisplay ()
788 | 'C' ->
789 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
790 state.pagecount <- n;
791 state.invalidated <- state.invalidated - 1;
792 if state.invalidated = 0
793 then represent ()
795 | 't' ->
796 let s = Scanf.sscanf cmd "t %n"
797 (fun n -> String.sub cmd n (String.length cmd - n))
799 Glut.setWindowTitle s
801 | 'T' ->
802 let s = Scanf.sscanf cmd "T %n"
803 (fun n -> String.sub cmd n (String.length cmd - n))
805 if istextentry state.mode
806 then (
807 state.text <- s;
808 showtext ' ' s;
810 else (
811 state.text <- s;
812 Glut.postRedisplay ();
815 | 'V' ->
816 if conf.verbose
817 then
818 let s = Scanf.sscanf cmd "V %n"
819 (fun n -> String.sub cmd n (String.length cmd - n))
821 state.text <- s;
822 showtext ' ' s;
824 | 'F' ->
825 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
826 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
827 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
828 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
830 let y = (getpagey pageno) + truncate y0 in
831 addnav ();
832 gotoy y;
833 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
835 | 'R' ->
836 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
837 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
838 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
839 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
841 state.rects1 <-
842 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
844 | 'r' ->
845 let n, w, h, r, l, s, p =
846 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
847 (fun n w h r l s p ->
848 (n-1, w, h, r, l != 0, s, p))
851 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
852 state.memused <- state.memused + s;
854 let layout =
855 match state.throttle with
856 | None -> state.layout
857 | Some layout -> layout
860 let rec gc () =
861 if (state.memused <= conf.memlimit) || cbempty state.pagecache
862 then ()
863 else (
864 let evictedopaque = cbpeek state.pagecache in
865 match findpageforopaque evictedopaque with
866 | None -> failwith "bug in gc"
867 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
868 if state.gen != gen || not (pagevisible layout evictedn)
869 then (
870 wcmd "free" [`s evictedopaque];
871 state.memused <- state.memused - evictedsize;
872 Hashtbl.remove state.pagemap k;
873 cbdecr state.pagecache;
874 gc ();
878 gc ();
880 cbput state.pagecache p;
881 state.rendering <- false;
883 begin match state.throttle with
884 | None ->
885 if pagevisible state.layout n
886 then gotoy state.y
887 else (
888 let allvisible = loadlayout state.layout in
889 if allvisible then preload ();
892 | Some layout ->
893 match layout with
894 | [] -> ()
895 | l :: _ ->
896 let y = getpagey l.pageno + l.pagey in
897 gotoy y
900 | 'l' ->
901 let (n, w, h, x) as pdim =
902 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
904 state.pdims <- pdim :: state.pdims
906 | 'o' ->
907 let (l, n, t, h, pos) =
908 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
910 let s = String.sub cmd pos (String.length cmd - pos) in
911 let s =
912 let l = String.length s in
913 let b = Buffer.create (String.length s) in
914 let rec loop pc2 i =
915 if i = l
916 then ()
917 else
918 let pc2 =
919 match s.[i] with
920 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
921 | '\xc2' -> true
922 | c ->
923 let c = if Char.code c land 0x80 = 0 then c else '?' in
924 Buffer.add_char b c;
925 false
927 loop pc2 (i+1)
929 loop false 0;
930 Buffer.contents b
932 let outline = (s, l, n, float t /. float h) in
933 let outlines =
934 match state.outlines with
935 | Olist outlines -> Olist (outline :: outlines)
936 | Oarray _ -> Olist [outline]
937 | Onarrow _ -> Olist [outline]
939 state.outlines <- outlines
941 | _ ->
942 dolog "unknown cmd `%S'" cmd
945 let now = Unix.gettimeofday;;
947 let idle () =
948 let rec loop delay =
949 let r, _, _ = Unix.select [state.csock] [] [] delay in
950 begin match r with
951 | [] ->
952 if conf.autoscroll && conf.scrollincr != 0
953 then begin
954 let y = state.y + conf.scrollincr in
955 let y = if y >= state.maxy then 0 else y in
956 gotoy y;
957 state.text <- "";
958 end;
960 | _ ->
961 let cmd = readcmd state.csock in
962 act cmd;
963 loop 0.0
964 end;
965 in loop 0.001
968 let onhist cb =
969 let rc = cb.rc in
970 let action = function
971 | HCprev -> cbget cb ~-1
972 | HCnext -> cbget cb 1
973 | HCfirst -> cbget cb ~-(cb.rc)
974 | HClast -> cbget cb (cb.len - 1 - cb.rc)
975 and cancel () = cb.rc <- rc
976 in (action, cancel)
979 let search pattern forward =
980 if String.length pattern > 0
981 then
982 let pn, py =
983 match state.layout with
984 | [] -> 0, 0
985 | l :: _ ->
986 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
988 let cmd =
989 let b = makecmd "search"
990 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
992 Buffer.add_char b ',';
993 Buffer.add_string b pattern;
994 Buffer.add_char b '\000';
995 Buffer.contents b;
997 writecmd state.csock cmd;
1000 let intentry text key =
1001 let c = Char.unsafe_chr key in
1002 match c with
1003 | '0' .. '9' ->
1004 let s = "x" in s.[0] <- c;
1005 let text = text ^ s in
1006 TEcont text
1008 | _ ->
1009 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1010 TEcont text
1013 let addchar s c =
1014 let b = Buffer.create (String.length s + 1) in
1015 Buffer.add_string b s;
1016 Buffer.add_char b c;
1017 Buffer.contents b;
1020 let textentry text key =
1021 let c = Char.unsafe_chr key in
1022 match c with
1023 | _ when key >= 32 && key < 127 ->
1024 let text = addchar text c in
1025 TEcont text
1027 | _ ->
1028 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1029 TEcont text
1032 let reinit angle proportional =
1033 conf.angle <- angle;
1034 conf.proportional <- proportional;
1035 invalidate ();
1036 wcmd "reinit" [`i angle; `b proportional];
1039 let optentry text key =
1040 let btos b = if b then "on" else "off" in
1041 let c = Char.unsafe_chr key in
1042 match c with
1043 | 's' ->
1044 let ondone s =
1045 try conf.scrollincr <- int_of_string s with exc ->
1046 state.text <- Printf.sprintf "bad integer `%s': %s"
1047 s (Printexc.to_string exc)
1049 TEswitch ('#', "", None, intentry, ondone)
1051 | 'R' ->
1052 let ondone s =
1053 match try
1054 Some (int_of_string s)
1055 with exc ->
1056 state.text <- Printf.sprintf "bad integer `%s': %s"
1057 s (Printexc.to_string exc);
1058 None
1059 with
1060 | Some angle -> reinit angle conf.proportional
1061 | None -> ()
1063 TEswitch ('^', "", None, intentry, ondone)
1065 | 'i' ->
1066 conf.icase <- not conf.icase;
1067 TEdone ("case insensitive search " ^ (btos conf.icase))
1069 | 'p' ->
1070 conf.preload <- not conf.preload;
1071 gotoy state.y;
1072 TEdone ("preload " ^ (btos conf.preload))
1074 | 'v' ->
1075 conf.verbose <- not conf.verbose;
1076 TEdone ("verbose " ^ (btos conf.verbose))
1078 | 'h' ->
1079 conf.maxhfit <- not conf.maxhfit;
1080 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1081 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1083 | 'c' ->
1084 conf.crophack <- not conf.crophack;
1085 TEdone ("crophack " ^ btos conf.crophack)
1087 | 'a' ->
1088 conf.showall <- not conf.showall;
1089 TEdone ("showall " ^ btos conf.showall)
1091 | 'f' ->
1092 conf.underinfo <- not conf.underinfo;
1093 TEdone ("underinfo " ^ btos conf.underinfo)
1095 | 'P' ->
1096 conf.savebmarks <- not conf.savebmarks;
1097 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1099 | 'S' ->
1100 let ondone s =
1102 let pageno, py =
1103 match state.layout with
1104 | [] -> 0, 0
1105 | l :: _ ->
1106 l.pageno, l.pagey
1108 conf.interpagespace <- int_of_string s;
1109 state.maxy <- calcheight ();
1110 let y = getpagey pageno in
1111 gotoy (y + py)
1112 with exc ->
1113 state.text <- Printf.sprintf "bad integer `%s': %s"
1114 s (Printexc.to_string exc)
1116 TEswitch ('%', "", None, intentry, ondone)
1118 | 'l' ->
1119 reinit conf.angle (not conf.proportional);
1120 TEdone ("proprortional display " ^ btos conf.proportional)
1122 | _ ->
1123 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1124 TEstop
1127 let maxoutlinerows () = (conf.winh - 31) / 16;;
1129 let enterselector allowdel outlines errmsg msg =
1130 if Array.length outlines = 0
1131 then (
1132 showtext ' ' errmsg;
1134 else (
1135 state.text <- msg;
1136 Glut.setCursor Glut.CURSOR_INHERIT;
1137 let pageno =
1138 match state.layout with
1139 | [] -> -1
1140 | {pageno=pageno} :: rest -> pageno
1142 let active =
1143 let rec loop n =
1144 if n = Array.length outlines
1145 then 0
1146 else
1147 let (_, _, outlinepageno, _) = outlines.(n) in
1148 if outlinepageno >= pageno then n else loop (n+1)
1150 loop 0
1152 state.mode <- Outline
1153 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "");
1154 Glut.postRedisplay ();
1158 let enteroutlinemode () =
1159 let outlines, msg =
1160 match state.outlines with
1161 | Oarray a -> a, ""
1162 | Olist l ->
1163 let a = Array.of_list (List.rev l) in
1164 state.outlines <- Oarray a;
1165 a, ""
1166 | Onarrow (pat, a, b) ->
1167 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1169 enterselector false outlines "Document has no outline" msg;
1172 let enterbookmarkmode () =
1173 let bookmarks = Array.of_list state.bookmarks in
1174 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1177 let quickbookmark ?title () =
1178 match state.layout with
1179 | [] -> ()
1180 | l :: _ ->
1181 let title =
1182 match title with
1183 | None ->
1184 let sec = Unix.gettimeofday () in
1185 let tm = Unix.localtime sec in
1186 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1187 (l.pageno+1)
1188 tm.Unix.tm_mday
1189 tm.Unix.tm_mon
1190 (tm.Unix.tm_year + 1900)
1191 tm.Unix.tm_hour
1192 tm.Unix.tm_min
1193 | Some title -> title
1195 state.bookmarks <-
1196 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1199 let doreshape w h =
1200 state.fullscreen <- None;
1201 Glut.reshapeWindow w h;
1204 let writeopen path password =
1205 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1208 let opendoc path password =
1209 invalidate ();
1210 state.path <- path;
1211 state.password <- password;
1212 state.gen <- state.gen + 1;
1214 writeopen path password;
1215 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1216 wcmd "geometry" [`i state.w; `i conf.winh];
1219 let birdseyeon () =
1220 let zoom = float conf.thumbw /. float conf.winw in
1221 let (birdseyepageno, _) as anchor = getanchor () in
1222 state.mode <-
1223 Birdseye ({ conf with zoom = conf.zoom }, state.x, birdseyepageno, -1);
1224 conf.zoom <- zoom;
1225 conf.presentation <- false;
1226 conf.interpagespace <- 10;
1227 conf.hlinks <- false;
1228 state.x <- 0;
1229 state.mstate <- Mnone;
1230 conf.showall <- false;
1231 Glut.setCursor Glut.CURSOR_INHERIT;
1232 if conf.verbose
1233 then
1234 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1235 (100.0*.zoom)
1236 else
1237 state.text <- ""
1241 let birdseyeoff (c, leftx, pageno, _) =
1242 state.mode <- View;
1243 conf.zoom <- c.zoom;
1244 conf.presentation <- c.presentation;
1245 conf.interpagespace <- c.interpagespace;
1246 conf.showall <- c.showall;
1247 conf.hlinks <- c.hlinks;
1248 state.x <- leftx;
1249 if conf.verbose
1250 then
1251 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1252 (100.0*.conf.zoom)
1256 let togglebirdseye () =
1257 match state.mode with
1258 | Birdseye vals -> birdseyeoff vals
1259 | View | Outline _ -> birdseyeon ()
1260 | _ -> ()
1263 let viewkeyboard ~key ~x ~y =
1264 let enttext te =
1265 state.mode <- Textentry (te, state.mode);
1266 state.text <- "";
1267 enttext ();
1268 Glut.postRedisplay ()
1270 let c = Char.chr key in
1271 match c with
1272 | '\027' | 'q' ->
1273 exit 0
1275 | '\008' ->
1276 let y = getnav () in
1277 gotoy_and_clear_text y
1279 | 'o' ->
1280 enteroutlinemode ()
1282 | 'u' ->
1283 state.rects <- [];
1284 state.text <- "";
1285 Glut.postRedisplay ()
1287 | '/' | '?' ->
1288 let ondone isforw s =
1289 cbput state.hists.pat s;
1290 state.searchpattern <- s;
1291 search s isforw
1293 enttext (c, "", Some (onhist state.hists.pat),
1294 textentry, ondone (c ='/'))
1296 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1297 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1298 conf.zoom <- min 2.2 (conf.zoom +. incr);
1299 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1300 reshape conf.winw conf.winh
1302 | '+' ->
1303 let ondone s =
1304 let n =
1305 try int_of_string s with exc ->
1306 state.text <- Printf.sprintf "bad integer `%s': %s"
1307 s (Printexc.to_string exc);
1308 max_int
1310 if n != max_int
1311 then (
1312 conf.pagebias <- n;
1313 state.text <- "page bias is now " ^ string_of_int n;
1316 enttext ('+', "", None, intentry, ondone)
1318 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1319 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1320 conf.zoom <- max 0.01 (conf.zoom -. decr);
1321 if conf.zoom <= 1.0 then state.x <- 0;
1322 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1323 reshape conf.winw conf.winh;
1325 | '-' ->
1326 let ondone msg =
1327 state.text <- msg;
1329 enttext ('-', "", None, optentry, ondone)
1331 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1332 state.x <- 0;
1333 conf.zoom <- 1.0;
1334 state.text <- "zoom is 100%";
1335 reshape conf.winw conf.winh
1337 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1338 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1339 if zoom < 1.0
1340 then (
1341 conf.zoom <- zoom;
1342 state.x <- 0;
1343 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1344 reshape conf.winw conf.winh;
1347 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1348 togglebirdseye ();
1349 reshape conf.winw conf.winh;
1351 | '0' .. '9' ->
1352 let ondone s =
1353 let n =
1354 try int_of_string s with exc ->
1355 state.text <- Printf.sprintf "bad integer `%s': %s"
1356 s (Printexc.to_string exc);
1359 if n >= 0
1360 then (
1361 addnav ();
1362 cbput state.hists.pag (string_of_int n);
1363 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1366 let pageentry text key =
1367 match Char.unsafe_chr key with
1368 | 'g' -> TEdone text
1369 | _ -> intentry text key
1371 let text = "x" in text.[0] <- c;
1372 enttext (':', text, Some (onhist state.hists.pag), pageentry, ondone)
1374 | 'b' ->
1375 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1376 reshape conf.winw conf.winh;
1378 | 'l' ->
1379 conf.hlinks <- not conf.hlinks;
1380 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1381 Glut.postRedisplay ()
1383 | 'a' ->
1384 conf.autoscroll <- not conf.autoscroll
1386 | 'P' ->
1387 conf.presentation <- not conf.presentation;
1388 showtext ' ' ("presentation mode " ^
1389 if conf.presentation then "on" else "off");
1390 represent ()
1392 | 'f' ->
1393 begin match state.fullscreen with
1394 | None ->
1395 state.fullscreen <- Some (conf.winw, conf.winh);
1396 Glut.fullScreen ()
1397 | Some (w, h) ->
1398 state.fullscreen <- None;
1399 doreshape w h
1402 | 'g' ->
1403 gotoy_and_clear_text 0
1405 | 'n' ->
1406 search state.searchpattern true
1408 | 'p' | 'N' ->
1409 search state.searchpattern false
1411 | 't' ->
1412 begin match state.layout with
1413 | [] -> ()
1414 | l :: _ ->
1415 gotoy_and_clear_text (getpagey l.pageno)
1418 | ' ' ->
1419 begin match List.rev state.layout with
1420 | [] -> ()
1421 | l :: _ ->
1422 let pageno = min (l.pageno+1) (state.pagecount-1) in
1423 gotoy_and_clear_text (getpagey pageno)
1426 | '\127' ->
1427 begin match state.layout with
1428 | [] -> ()
1429 | l :: _ ->
1430 let pageno = max 0 (l.pageno-1) in
1431 gotoy_and_clear_text (getpagey pageno)
1434 | '=' ->
1435 let f (fn, ln) l =
1436 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1438 let fn, ln = List.fold_left f (-1, -1) state.layout in
1439 let s =
1440 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1441 let percent =
1442 if maxy <= 0
1443 then 100.
1444 else (100. *. (float state.y /. float maxy)) in
1445 if fn = ln
1446 then
1447 Printf.sprintf "Page %d of %d %.2f%%"
1448 (fn+1) state.pagecount percent
1449 else
1450 Printf.sprintf
1451 "Pages %d-%d of %d %.2f%%"
1452 (fn+1) (ln+1) state.pagecount percent
1454 showtext ' ' s;
1456 | 'w' ->
1457 begin match state.layout with
1458 | [] -> ()
1459 | l :: _ ->
1460 doreshape (l.pagew + conf.scrollw) l.pageh;
1461 Glut.postRedisplay ();
1464 | '\'' ->
1465 enterbookmarkmode ()
1467 | 'm' ->
1468 let ondone s =
1469 match state.layout with
1470 | l :: _ ->
1471 state.bookmarks <-
1472 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1473 :: state.bookmarks
1474 | _ -> ()
1476 enttext ('~', "", None, textentry, ondone)
1478 | '~' ->
1479 quickbookmark ();
1480 showtext ' ' "Quick bookmark added";
1482 | 'z' ->
1483 begin match state.layout with
1484 | l :: _ ->
1485 let rect = getpdimrect l.pagedimno in
1486 let w, h =
1487 if conf.crophack
1488 then
1489 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1490 truncate (1.2 *. (rect.(3) -. rect.(0))))
1491 else
1492 (truncate (rect.(1) -. rect.(0)),
1493 truncate (rect.(3) -. rect.(0)))
1495 if w != 0 && h != 0
1496 then
1497 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1499 Glut.postRedisplay ();
1501 | [] -> ()
1504 | '<' | '>' ->
1505 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1507 | '[' | ']' ->
1508 state.colorscale <-
1509 max 0.0
1510 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1511 Glut.postRedisplay ()
1513 | 'k' -> gotoy (clamp (-conf.scrollincr))
1514 | 'j' -> gotoy (clamp conf.scrollincr)
1516 | 'r' -> opendoc state.path state.password
1518 | _ ->
1519 vlog "huh? %d %c" key (Char.chr key);
1522 let textentrykeyboard ~key ~x ~y ((c, text, opthist, onkey, ondone), mode) =
1523 let enttext te =
1524 state.mode <- Textentry (te, mode);
1525 state.text <- "";
1526 enttext ();
1527 Glut.postRedisplay ()
1529 match Char.unsafe_chr key with
1530 | '\008' ->
1531 let len = String.length text in
1532 if len = 0
1533 then (
1534 state.mode <- mode;
1535 Glut.postRedisplay ();
1537 else (
1538 let s = String.sub text 0 (len - 1) in
1539 enttext (c, s, opthist, onkey, ondone)
1542 | '\r' | '\n' ->
1543 ondone text;
1544 state.mode <- mode;
1545 Glut.postRedisplay ()
1547 | '\027' ->
1548 begin match opthist with
1549 | None -> ()
1550 | Some (_, onhistcancel) -> onhistcancel ()
1551 end;
1552 state.mode <- View;
1553 Glut.postRedisplay ()
1555 | _ ->
1556 begin match onkey text key with
1557 | TEdone text ->
1558 state.mode <- mode;
1559 ondone text;
1560 Glut.postRedisplay ()
1562 | TEcont text ->
1563 enttext (c, text, opthist, onkey, ondone);
1565 | TEstop ->
1566 state.mode <- mode;
1567 Glut.postRedisplay ()
1569 | TEswitch te ->
1570 state.mode <- Textentry (te, mode);
1571 Glut.postRedisplay ()
1572 end;
1575 let birdseyekeyboard ~key ~x ~y ((c, leftx, pageno, hooverpageno) as beye) =
1576 match key with
1577 | 27 ->
1578 birdseyeoff beye;
1579 reshape conf.winw conf.winh
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 addnav ();
1588 birdseyeoff beye;
1589 reshape conf.winw conf.winh;
1590 state.anchor <- (pageno, 0.0);
1592 | _ ->
1593 viewkeyboard ~key ~x ~y
1596 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1597 let narrow outlines pattern =
1598 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1599 match reopt with
1600 | None -> None
1601 | Some re ->
1602 let rec fold accu n =
1603 if n = -1
1604 then accu
1605 else
1606 let (s, _, _, _) as o = outlines.(n) in
1607 let accu =
1608 if (try ignore (Str.search_forward re s 0); true
1609 with Not_found -> false)
1610 then (o :: accu)
1611 else accu
1613 fold accu (n-1)
1615 let matched = fold [] (Array.length outlines - 1) in
1616 if matched = [] then None else Some (Array.of_list matched)
1618 let search active pattern incr =
1619 let dosearch re =
1620 let rec loop n =
1621 if n = Array.length outlines || n = -1
1622 then None
1623 else
1624 let (s, _, _, _) = outlines.(n) in
1626 (try ignore (Str.search_forward re s 0); true
1627 with Not_found -> false)
1628 then Some n
1629 else loop (n + incr)
1631 loop active
1634 let re = Str.regexp_case_fold pattern in
1635 dosearch re
1636 with Failure s ->
1637 state.text <- s;
1638 None
1640 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1641 match key with
1642 | 27 ->
1643 if String.length qsearch = 0
1644 then (
1645 state.text <- "";
1646 state.mode <- View;
1647 Glut.postRedisplay ();
1649 else (
1650 state.text <- "";
1651 state.mode <- Outline (allowdel, active, first, outlines, "");
1652 Glut.postRedisplay ();
1655 | 18 | 19 ->
1656 let incr = if key = 18 then -1 else 1 in
1657 let active, first =
1658 match search (active + incr) qsearch incr with
1659 | None ->
1660 state.text <- qsearch ^ " [not found]";
1661 active, first
1662 | Some active ->
1663 state.text <- qsearch;
1664 active, firstof active
1666 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1667 Glut.postRedisplay ();
1669 | 8 ->
1670 let len = String.length qsearch in
1671 if len = 0
1672 then ()
1673 else (
1674 if len = 1
1675 then (
1676 state.text <- "";
1677 state.mode <- Outline (allowdel, active, first, outlines, "");
1679 else
1680 let qsearch = String.sub qsearch 0 (len - 1) in
1681 let active, first =
1682 match search active qsearch ~-1 with
1683 | None ->
1684 state.text <- qsearch ^ " [not found]";
1685 active, first
1686 | Some active ->
1687 state.text <- qsearch;
1688 active, firstof active
1690 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1692 Glut.postRedisplay ()
1694 | 13 ->
1695 if active < Array.length outlines
1696 then (
1697 let (_, _, n, t) = outlines.(active) in
1698 gotopage n t;
1700 state.text <- "";
1701 if allowdel then state.bookmarks <- Array.to_list outlines;
1702 state.mode <- View;
1703 Glut.postRedisplay ();
1705 | _ when key >= 32 && key < 127 ->
1706 let pattern = addchar qsearch (Char.chr key) in
1707 let active, first =
1708 match search active pattern 1 with
1709 | None ->
1710 state.text <- pattern ^ " [not found]";
1711 active, first
1712 | Some active ->
1713 state.text <- pattern;
1714 active, firstof active
1716 state.mode <- Outline (allowdel, active, first, outlines, pattern);
1717 Glut.postRedisplay ()
1719 | 14 when not allowdel -> (* ctrl-n *)
1720 if String.length qsearch > 0
1721 then (
1722 let optoutlines = narrow outlines qsearch in
1723 begin match optoutlines with
1724 | None -> state.text <- "can't narrow"
1725 | Some outlines ->
1726 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1727 match state.outlines with
1728 | Olist l -> ()
1729 | Oarray a ->
1730 state.outlines <- Onarrow (qsearch, outlines, a)
1731 | Onarrow (pat, a, b) ->
1732 state.outlines <- Onarrow (qsearch, outlines, b)
1733 end;
1735 Glut.postRedisplay ()
1737 | 21 when not allowdel -> (* ctrl-u *)
1738 let outline =
1739 match state.outlines with
1740 | Oarray a -> a
1741 | Olist l ->
1742 let a = Array.of_list (List.rev l) in
1743 state.outlines <- Oarray a;
1745 | Onarrow (pat, a, b) ->
1746 state.outlines <- Oarray b;
1747 state.text <- "";
1750 state.mode <- Outline (allowdel, 0, 0, outline, qsearch);
1751 Glut.postRedisplay ()
1753 | 12 ->
1754 state.mode <- Outline
1755 (allowdel, active, firstof active, outlines, qsearch);
1756 Glut.postRedisplay ()
1758 | 127 when allowdel ->
1759 let len = Array.length outlines - 1 in
1760 if len = 0
1761 then (
1762 state.mode <- View;
1763 state.bookmarks <- [];
1765 else (
1766 let bookmarks = Array.init len
1767 (fun i ->
1768 let i = if i >= active then i + 1 else i in
1769 outlines.(i)
1772 state.mode <-
1773 Outline (
1774 allowdel,
1775 min active (len-1),
1776 min first (len-1),
1777 bookmarks, qsearch
1780 Glut.postRedisplay ()
1782 | _ -> dolog "unknown key %d" key
1785 let keyboard ~key ~x ~y =
1786 if key = 7
1787 then
1788 wcmd "interrupt" []
1789 else
1790 match state.mode with
1791 | Outline outline -> outlinekeyboard ~key ~x ~y outline
1792 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
1793 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
1794 | View -> viewkeyboard ~key ~x ~y
1797 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno) =
1798 match key with
1799 | Glut.KEY_UP ->
1800 let pageno = max 0 (pageno - 1) in
1801 let rec loop = function
1802 | [] -> gotopage1nonav pageno 0
1803 | l :: _ when l.pageno = pageno ->
1804 if l.pagedispy >= 0 && l.pagey = 0
1805 then Glut.postRedisplay ()
1806 else gotopage1nonav pageno 0
1807 | _ :: rest -> loop rest
1809 loop state.layout;
1810 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno)
1812 | Glut.KEY_DOWN ->
1813 let pageno = min (state.pagecount - 1) (pageno + 1) in
1814 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno);
1815 let rec loop = function
1816 | [] ->
1817 let y, h = getpageyh pageno in
1818 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1819 gotoy (clamp dy)
1820 | l :: rest when l.pageno = pageno ->
1821 if l.pagevh != l.pageh
1822 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1823 else Glut.postRedisplay ()
1824 | l :: rest -> loop rest
1826 loop state.layout
1828 | Glut.KEY_PAGE_UP ->
1829 begin match state.layout with
1830 | l :: _ ->
1831 if l.pagey != 0
1832 then (
1833 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno);
1834 gotopage1nonav 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 (conf, leftx, l.pageno, hooverpageno);
1842 gotopage1nonav l.pageno 0
1845 | [] -> gotoy (clamp (-conf.winh))
1846 end;
1848 | Glut.KEY_PAGE_DOWN ->
1849 begin match List.rev state.layout with
1850 | l :: _ ->
1851 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno);
1852 gotoy (clamp (l.pagedispy + l.pageh))
1853 | [] -> gotoy (clamp conf.winh)
1854 end;
1856 | Glut.KEY_HOME ->
1857 state.mode <- Birdseye (conf, leftx, 0, hooverpageno);
1858 gotopage1nonav 0 0
1860 | Glut.KEY_END ->
1861 let pageno = state.pagecount - 1 in
1862 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno);
1863 if not (pagevisible state.layout pageno)
1864 then
1865 let h =
1866 match List.rev state.pdims with
1867 | [] -> conf.winh
1868 | (_, _, h, _) :: _ -> h
1870 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1871 else Glut.postRedisplay ();
1872 | _ -> ()
1875 let special ~key ~x ~y =
1876 match state.mode with
1877 | View | (Birdseye _) when key = Glut.KEY_F9 ->
1878 togglebirdseye ();
1879 reshape conf.winw conf.winh;
1881 | Birdseye vals ->
1882 birdseyespecial key x y vals
1884 | View | Textentry _ ->
1885 begin match state.mode with
1886 | View ->
1887 let y =
1888 match key with
1889 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1890 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1891 | Glut.KEY_DOWN -> clamp conf.scrollincr
1892 | Glut.KEY_PAGE_UP ->
1893 if Glut.getModifiers () land Glut.active_ctrl != 0
1894 then
1895 match state.layout with
1896 | [] -> state.y
1897 | l :: _ -> state.y - l.pagey
1898 else
1899 clamp (-conf.winh)
1900 | Glut.KEY_PAGE_DOWN ->
1901 if Glut.getModifiers () land Glut.active_ctrl != 0
1902 then
1903 match List.rev state.layout with
1904 | [] -> state.y
1905 | l :: _ -> getpagey l.pageno
1906 else
1907 clamp conf.winh
1908 | Glut.KEY_HOME -> addnav (); 0
1909 | Glut.KEY_END ->
1910 addnav ();
1911 state.maxy - (if conf.maxhfit then conf.winh else 0)
1913 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1914 state.x <- state.x - 10;
1915 state.y
1916 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1917 state.x <- state.x + 10;
1918 state.y
1920 | _ -> state.y
1922 gotoy_and_clear_text y
1924 | Textentry
1925 ((c, s, (Some (action, _) as onhist), onkey, ondone), mode) ->
1926 let s =
1927 match key with
1928 | Glut.KEY_UP -> action HCprev
1929 | Glut.KEY_DOWN -> action HCnext
1930 | Glut.KEY_HOME -> action HCfirst
1931 | Glut.KEY_END -> action HClast
1932 | _ -> state.text
1934 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
1935 Glut.postRedisplay ()
1937 | _ -> ()
1940 | Outline (allowdel, active, first, outlines, qsearch) ->
1941 let maxrows = maxoutlinerows () in
1942 let calcfirst first active =
1943 if active > first
1944 then
1945 let rows = active - first in
1946 if rows > maxrows then active - maxrows else first
1947 else active
1949 let navigate incr =
1950 let active = active + incr in
1951 let active = max 0 (min active (Array.length outlines - 1)) in
1952 let first = calcfirst first active in
1953 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1954 Glut.postRedisplay ()
1956 let updownlevel incr =
1957 let len = Array.length outlines in
1958 let (_, curlevel, _, _) = outlines.(active) in
1959 let rec flow i =
1960 if i = len then i-1 else if i = -1 then 0 else
1961 let (_, l, _, _) = outlines.(i) in
1962 if l != curlevel then i else flow (i+incr)
1964 let active = flow active in
1965 let first = calcfirst first active in
1966 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1967 Glut.postRedisplay ()
1969 match key with
1970 | Glut.KEY_UP -> navigate ~-1
1971 | Glut.KEY_DOWN -> navigate 1
1972 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1973 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1975 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1976 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1978 | Glut.KEY_HOME ->
1979 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1980 Glut.postRedisplay ()
1982 | Glut.KEY_END ->
1983 let active = Array.length outlines - 1 in
1984 let first = max 0 (active - maxrows) in
1985 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1986 Glut.postRedisplay ()
1988 | _ -> ()
1991 let drawplaceholder l =
1992 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1993 GlDraw.rect
1994 (float l.pagex, float l.pagedispy)
1995 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1997 let x = float (if margin < 0 then -margin else l.pagex)
1998 and y = float (l.pagedispy + 13) in
1999 let font = Glut.BITMAP_8_BY_13 in
2000 GlDraw.color (0.0, 0.0, 0.0);
2001 GlPix.raster_pos ~x ~y ();
2002 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2003 ("Loading " ^ string_of_int (l.pageno + 1));
2006 let now () = Unix.gettimeofday ();;
2008 let drawpage l =
2009 let color =
2010 match state.mode with
2011 | Textentry _ -> scalecolor 0.4
2012 | View | Outline _ -> scalecolor 1.0
2013 | Birdseye (_, _, pageno, hooverpageno) ->
2014 if l.pageno = pageno
2015 then scalecolor 1.0
2016 else (
2017 if l.pageno = hooverpageno
2018 then scalecolor 0.9
2019 else scalecolor 0.8
2022 GlDraw.color color;
2023 begin match getopaque l.pageno with
2024 | Some (opaque, _) when validopaque opaque ->
2025 let a = now () in
2026 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2027 opaque;
2028 let b = now () in
2029 let d = b-.a in
2030 vlog "draw %d %f sec" l.pageno d;
2032 | _ ->
2033 drawplaceholder l;
2034 end;
2037 let scrollph y =
2038 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2039 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2040 let sh = float conf.winh /. sh in
2041 let sh = max sh (float conf.scrollh) in
2043 let percent =
2044 if state.y = state.maxy
2045 then 1.0
2046 else float y /. float maxy
2048 let position = (float conf.winh -. sh) *. percent in
2050 let position =
2051 if position +. sh > float conf.winh
2052 then float conf.winh -. sh
2053 else position
2055 position, sh;
2058 let scrollindicator () =
2059 GlDraw.color (0.64 , 0.64, 0.64);
2060 GlDraw.rect
2061 (float (conf.winw - conf.scrollw), 0.)
2062 (float conf.winw, float conf.winh)
2064 GlDraw.color (0.0, 0.0, 0.0);
2066 let position, sh = scrollph state.y in
2067 GlDraw.rect
2068 (float (conf.winw - conf.scrollw), position)
2069 (float conf.winw, position +. sh)
2073 let showsel margin =
2074 match state.mstate with
2075 | Mnone | Mscroll _ | Mpan _ ->
2078 | Msel ((x0, y0), (x1, y1)) ->
2079 let rec loop = function
2080 | l :: ls ->
2081 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2082 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2083 then
2084 match getopaque l.pageno with
2085 | Some (opaque, _) when validopaque opaque ->
2086 let oy = -l.pagey + l.pagedispy in
2087 seltext opaque
2088 (x0 - margin - state.x, y0,
2089 x1 - margin - state.x, y1) oy;
2091 | _ -> ()
2092 else loop ls
2093 | [] -> ()
2095 loop state.layout
2098 let showrects () =
2099 let panx = float state.x in
2100 Gl.enable `blend;
2101 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2102 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2103 List.iter
2104 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2105 List.iter (fun l ->
2106 if l.pageno = pageno
2107 then (
2108 let d = float (l.pagedispy - l.pagey) in
2109 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2110 GlDraw.begins `quads;
2112 GlDraw.vertex2 (x0+.panx, y0+.d);
2113 GlDraw.vertex2 (x1+.panx, y1+.d);
2114 GlDraw.vertex2 (x2+.panx, y2+.d);
2115 GlDraw.vertex2 (x3+.panx, y3+.d);
2117 GlDraw.ends ();
2119 ) state.layout
2120 ) state.rects
2122 Gl.disable `blend;
2125 let showoutline = function
2126 | Outline (allowdel, active, first, outlines, qsearch) ->
2127 Gl.enable `blend;
2128 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2129 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2130 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2131 Gl.disable `blend;
2133 GlDraw.color (1., 1., 1.);
2134 let font = Glut.BITMAP_9_BY_15 in
2135 let draw_string x y s =
2136 GlPix.raster_pos ~x ~y ();
2137 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2139 let rec loop row =
2140 if row = Array.length outlines || (row - first) * 16 > conf.winh
2141 then ()
2142 else (
2143 let (s, l, _, _) = outlines.(row) in
2144 let y = (row - first) * 16 in
2145 let x = 5 + 15*l in
2146 if row = active
2147 then (
2148 Gl.enable `blend;
2149 GlDraw.polygon_mode `both `line;
2150 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2151 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2152 GlDraw.rect (0., float (y + 1))
2153 (float (conf.winw - 1), float (y + 18));
2154 GlDraw.polygon_mode `both `fill;
2155 Gl.disable `blend;
2156 GlDraw.color (1., 1., 1.);
2158 draw_string (float x) (float (y + 16)) s;
2159 loop (row+1)
2162 loop first
2164 | _ -> ()
2167 let display () =
2168 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2169 GlDraw.viewport margin 0 state.w conf.winh;
2170 pagematrix ();
2171 GlClear.color (scalecolor 0.5);
2172 GlClear.clear [`color];
2173 if state.x != 0
2174 then (
2175 let x = float state.x in
2176 GlMat.translate ~x ();
2178 if conf.zoom > 1.0
2179 then (
2180 Gl.enable `scissor_test;
2181 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2183 List.iter drawpage state.layout;
2184 if conf.zoom > 1.0
2185 then
2186 Gl.disable `scissor_test
2188 if state.x != 0
2189 then (
2190 let x = -.float state.x in
2191 GlMat.translate ~x ();
2193 showrects ();
2194 showsel margin;
2195 GlDraw.viewport 0 0 conf.winw conf.winh;
2196 winmatrix ();
2197 scrollindicator ();
2198 showoutline state.mode;
2199 enttext ();
2200 Glut.swapBuffers ();
2203 let getunder x y =
2204 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2205 let x = x - margin - state.x in
2206 let rec f = function
2207 | l :: rest ->
2208 begin match getopaque l.pageno with
2209 | Some (opaque, _) when validopaque opaque ->
2210 let y = y - l.pagedispy in
2211 if y > 0
2212 then
2213 let y = l.pagey + y in
2214 let x = x - l.pagex in
2215 match whatsunder opaque x y with
2216 | Unone -> f rest
2217 | under -> under
2218 else
2219 f rest
2220 | _ ->
2221 f rest
2223 | [] -> Unone
2225 f state.layout
2228 let viewmouse button bstate x y =
2229 match button with
2230 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2231 let incr =
2232 if n = 3
2233 then
2234 -conf.scrollincr
2235 else
2236 conf.scrollincr
2238 let incr = incr * 2 in
2239 let y = clamp incr in
2240 gotoy_and_clear_text y
2242 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2243 if bstate = Glut.DOWN
2244 then (
2245 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2246 state.mstate <- Mpan (x, y)
2248 else
2249 state.mstate <- Mnone
2251 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2252 if bstate = Glut.DOWN
2253 then
2254 let position, sh = scrollph state.y in
2255 if y > truncate position && y < truncate (position +. sh)
2256 then
2257 state.mstate <- Mscroll
2258 else
2259 let percent = float y /. float conf.winh in
2260 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2261 gotoy desty;
2262 state.mstate <- Mscroll
2263 else
2264 state.mstate <- Mnone
2266 | Glut.LEFT_BUTTON ->
2267 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2268 begin match dest with
2269 | Ulinkgoto (pageno, top) ->
2270 if pageno >= 0
2271 then
2272 gotopage1 pageno top
2274 | Ulinkuri s ->
2275 print_endline s
2277 | Unone when bstate = Glut.DOWN ->
2278 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2279 state.mstate <- Mpan (x, y);
2281 | Unone | Utext _ ->
2282 if bstate = Glut.DOWN
2283 then (
2284 if conf.angle mod 360 = 0
2285 then (
2286 state.mstate <- Msel ((x, y), (x, y));
2287 Glut.postRedisplay ()
2290 else (
2291 match state.mstate with
2292 | Mnone -> ()
2294 | Mscroll ->
2295 state.mstate <- Mnone
2297 | Mpan _ ->
2298 Glut.setCursor Glut.CURSOR_INHERIT;
2299 state.mstate <- Mnone
2301 | Msel ((x0, y0), (x1, y1)) ->
2302 let f l =
2303 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2304 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2305 then
2306 match getopaque l.pageno with
2307 | Some (opaque, _) when validopaque opaque ->
2308 copysel opaque
2309 | _ -> ()
2311 List.iter f state.layout;
2312 copysel ""; (* ugly *)
2313 Glut.setCursor Glut.CURSOR_INHERIT;
2314 state.mstate <- Mnone;
2318 | _ -> ()
2321 let birdseyemouse button bstate x y (conf, leftx, pageno, hooverpageno) =
2322 match button with
2323 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2324 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2325 let rec loop = function
2326 | [] -> ()
2327 | l :: rest ->
2328 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2329 && x > margin && x < margin + l.pagew
2330 then (
2331 birdseyeoff (conf, leftx, l.pageno, hooverpageno);
2332 reshape conf.winw conf.winh;
2333 state.anchor <- (l.pageno, 0.0);
2335 else loop rest
2337 loop state.layout
2338 | _ -> ()
2341 let mouse bstate button x y =
2342 match state.mode with
2343 | View -> viewmouse button bstate x y
2344 | Birdseye beye -> birdseyemouse button bstate x y beye
2345 | Textentry _ -> ()
2346 | Outline _ -> ()
2349 let mouse ~button ~state ~x ~y = mouse state button x y;;
2351 let motion ~x ~y =
2352 match state.mode with
2353 | Outline _ -> ()
2354 | _ ->
2355 match state.mstate with
2356 | Mnone -> ()
2358 | Mpan (x0, y0) ->
2359 let dx = x - x0
2360 and dy = y0 - y in
2361 state.mstate <- Mpan (x, y);
2362 if conf.zoom > 1.0 then state.x <- state.x + dx;
2363 let y = clamp dy in
2364 gotoy_and_clear_text y
2366 | Msel (a, _) ->
2367 state.mstate <- Msel (a, (x, y));
2368 Glut.postRedisplay ()
2370 | Mscroll ->
2371 let y = min conf.winh (max 0 y) in
2372 let percent = float y /. float conf.winh in
2373 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2374 gotoy_and_clear_text y
2377 let pmotion ~x ~y =
2378 match state.mode with
2379 | Birdseye (conf, leftx, pageno, hooverpageno) ->
2380 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2381 let rec loop = function
2382 | [] ->
2383 if hooverpageno != -1
2384 then (
2385 state.mode <- Birdseye (conf, leftx, pageno, -1);
2386 Glut.postRedisplay ();
2388 | l :: rest ->
2389 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2390 && x > margin && x < margin + l.pagew
2391 then (
2392 state.mode <- Birdseye (conf, leftx, pageno, l.pageno);
2393 Glut.postRedisplay ();
2395 else loop rest
2397 loop state.layout
2399 | Outline _ -> ()
2400 | _ ->
2401 match state.mstate with
2402 | Mnone ->
2403 begin match getunder x y with
2404 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2405 | Ulinkuri uri ->
2406 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2407 Glut.setCursor Glut.CURSOR_INFO
2408 | Ulinkgoto (page, y) ->
2409 if conf.underinfo
2410 then showtext 'p' ("age: " ^ string_of_int page);
2411 Glut.setCursor Glut.CURSOR_INFO
2412 | Utext s ->
2413 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2414 Glut.setCursor Glut.CURSOR_TEXT
2417 | Mpan _ | Msel _ | Mscroll ->
2422 module State =
2423 struct
2424 open Parser
2426 let home =
2428 match Sys.os_type with
2429 | "Win32" -> Sys.getenv "HOMEPATH"
2430 | _ -> Sys.getenv "HOME"
2431 with exn ->
2432 prerr_endline
2433 ("Can not determine home directory location: " ^
2434 Printexc.to_string exn);
2438 let config_of c attrs =
2439 let apply c k v =
2441 match k with
2442 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2443 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2444 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2445 | "preload" -> { c with preload = bool_of_string v }
2446 | "page-bias" -> { c with pagebias = int_of_string v }
2447 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2448 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2449 | "crop-hack" -> { c with crophack = bool_of_string v }
2450 | "throttle" -> { c with showall = bool_of_string v }
2451 | "highlight-links" -> { c with hlinks = bool_of_string v }
2452 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2453 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2454 | "zoom" ->
2455 let zoom = float_of_string v /. 100. in
2456 let zoom = max 0.01 (min 2.2 zoom) in
2457 { c with zoom = zoom }
2458 | "presentation" -> { c with presentation = bool_of_string v }
2459 | "rotation-angle" -> { c with angle = int_of_string v }
2460 | "width" -> { c with winw = max 20 (int_of_string v) }
2461 | "height" -> { c with winh = max 20 (int_of_string v) }
2462 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2463 | "proportional-display" -> { c with proportional = bool_of_string v }
2464 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2465 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2466 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2467 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2468 | _ -> c
2469 with exn ->
2470 prerr_endline ("Error processing attribute (`" ^
2471 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2474 let rec fold c = function
2475 | [] -> c
2476 | (k, v) :: rest ->
2477 let c = apply c k v in
2478 fold c rest
2480 fold c attrs;
2483 let bookmark_of attrs =
2484 let rec fold title page rely = function
2485 | ("title", v) :: rest -> fold v page rely rest
2486 | ("page", v) :: rest -> fold title v rely rest
2487 | ("rely", v) :: rest -> fold title page v rest
2488 | _ :: rest -> fold title page rely rest
2489 | [] -> title, page, rely
2491 fold "invalid" "0" "0" attrs
2494 let setconf dst src =
2495 dst.scrollw <- src.scrollw;
2496 dst.scrollh <- src.scrollh;
2497 dst.icase <- src.icase;
2498 dst.preload <- src.preload;
2499 dst.pagebias <- src.pagebias;
2500 dst.verbose <- src.verbose;
2501 dst.scrollincr <- src.scrollincr;
2502 dst.maxhfit <- src.maxhfit;
2503 dst.crophack <- src.crophack;
2504 dst.autoscroll <- src.autoscroll;
2505 dst.showall <- src.showall;
2506 dst.hlinks <- src.hlinks;
2507 dst.underinfo <- src.underinfo;
2508 dst.interpagespace <- src.interpagespace;
2509 dst.zoom <- src.zoom;
2510 dst.presentation <- src.presentation;
2511 dst.angle <- src.angle;
2512 dst.winw <- src.winw;
2513 dst.winh <- src.winh;
2514 dst.savebmarks <- src.savebmarks;
2515 dst.memlimit <- src.memlimit;
2516 dst.proportional <- src.proportional;
2517 dst.texcount <- src.texcount;
2518 dst.sliceheight <- src.sliceheight;
2519 dst.thumbw <- src.thumbw;
2522 let unent s =
2523 let l = String.length s in
2524 let b = Buffer.create l in
2525 unent b s 0 l;
2526 Buffer.contents b;
2529 let get s =
2530 let h = Hashtbl.create 10 in
2531 let dc = { defconf with angle = defconf.angle } in
2532 let rec toplevel v t spos epos =
2533 match t with
2534 | Vdata | Vcdata | Vend -> v
2535 | Vopen ("llppconfig", attrs, closed) ->
2536 if closed
2537 then v
2538 else { v with f = llppconfig }
2539 | Vopen _ ->
2540 error "unexpected subelement at top level" s spos
2541 | Vclose tag -> error "unexpected close at top level" s spos
2543 and llppconfig v t spos epos =
2544 match t with
2545 | Vdata | Vcdata | Vend -> v
2546 | Vopen ("defaults", attrs, closed) ->
2547 let c = config_of dc attrs in
2548 setconf dc c;
2549 if closed
2550 then v
2551 else { v with f = skip "defaults" (fun () -> v) }
2553 | Vopen ("doc", attrs, closed) ->
2554 let pathent =
2556 List.assoc "path" attrs
2557 with Not_found -> error "doc is missing path attribute" s spos
2559 let path = unent pathent in
2560 let c = config_of dc attrs in
2561 let pageno, rely, x =
2562 let safef f n v d =
2563 try f v
2564 with exn ->
2565 dolog "error accessing %s (%S) at postion %d:\n %s"
2566 n v spos (Printexc.to_string exn);
2569 let rec fold pageno rely x = function
2570 | [] -> pageno, rely, x
2571 | ("rely", v) :: rest ->
2572 fold pageno (safef float_of_string "rely" v 0.0) x rest
2573 | ("page", v) :: rest ->
2574 fold (safef int_of_string "page" v 0) rely x rest
2575 | ("x", v) :: rest ->
2576 fold pageno rely (safef int_of_string "x" v 0) rest
2577 | _ :: rest ->
2578 fold pageno rely x rest
2580 fold 0 0.0 0 attrs
2582 let anchor = (pageno, rely) in
2583 if closed
2584 then (Hashtbl.add h path (c, [], x, anchor); v)
2585 else { v with f = doc path x anchor c [] }
2587 | Vopen (tag, _, closed) ->
2588 error "unexpected subelement in llppconfig" s spos
2590 | Vclose "llppconfig" -> { v with f = toplevel }
2591 | Vclose tag -> error "unexpected close in llppconfig" s spos
2593 and doc path x anchor c bookmarks v t spos epos =
2594 match t with
2595 | Vdata | Vcdata -> v
2596 | Vend -> error "unexpected end of input in doc" s spos
2597 | Vopen ("bookmarks", attrs, closed) ->
2598 { v with f = pbookmarks path x anchor c bookmarks }
2600 | Vopen (tag, _, _) ->
2601 error "unexpected subelement in doc" s spos
2603 | Vclose "doc" ->
2604 Hashtbl.add h path (c, List.rev bookmarks, x, anchor);
2605 { v with f = llppconfig }
2607 | Vclose tag -> error "unexpected close in doc" s spos
2609 and pbookmarks path x anchor c bookmarks v t spos epos =
2610 match t with
2611 | Vdata | Vcdata -> v
2612 | Vend -> error "unexpected end of input in bookmarks" s spos
2613 | Vopen ("item", attrs, closed) ->
2614 let titleent, spage, srely = bookmark_of attrs in
2615 let page =
2617 int_of_string spage
2618 with exn ->
2619 dolog "Failed to convert page %S to integer: %s"
2620 spage (Printexc.to_string exn);
2623 let rely =
2625 float_of_string srely
2626 with exn ->
2627 dolog "Failed to convert rely %S to real: %s"
2628 srely (Printexc.to_string exn);
2631 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2632 if closed
2633 then { v with f = pbookmarks path x anchor c bookmarks }
2634 else
2635 let f () = v in
2636 { v with f = skip "item" f }
2638 | Vopen _ ->
2639 error "unexpected subelement in bookmarks" s spos
2641 | Vclose "bookmarks" ->
2642 { v with f = doc path x anchor c bookmarks }
2644 | Vclose tag -> error "unexpected close in bookmarks" s spos
2646 and skip tag f v t spos epos =
2647 match t with
2648 | Vdata | Vcdata -> v
2649 | Vend ->
2650 error ("unexpected end of input in skipped " ^ tag) s spos
2651 | Vopen (tag', _, closed) ->
2652 if closed
2653 then v
2654 else
2655 let f' () = { v with f = skip tag f } in
2656 { v with f = skip tag' f' }
2657 | Vclose ctag ->
2658 if tag = ctag
2659 then f ()
2660 else error ("unexpected close in skipped " ^ tag) s spos
2663 parse { f = toplevel; accu = () } s;
2664 h, dc;
2667 let do_load f ic =
2669 let len = in_channel_length ic in
2670 let s = String.create len in
2671 really_input ic s 0 len;
2672 f s;
2673 with
2674 | Parse_error (msg, s, pos) ->
2675 let subs = subs s pos in
2676 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2677 failwith ("parse error: " ^ s)
2679 | exn ->
2680 failwith ("config load error: " ^ Printexc.to_string exn)
2683 let path =
2684 let dir =
2686 let dir = Filename.concat home ".config" in
2687 if Sys.is_directory dir then dir else home
2688 with _ -> home
2690 Filename.concat dir "llpp.conf"
2693 let load1 f =
2694 if Sys.file_exists path
2695 then
2696 match
2697 (try Some (open_in_bin path)
2698 with exn ->
2699 prerr_endline
2700 ("Error opening configuation file `" ^ path ^ "': " ^
2701 Printexc.to_string exn);
2702 None
2704 with
2705 | Some ic ->
2706 begin try
2707 f (do_load get ic)
2708 with exn ->
2709 prerr_endline
2710 ("Error loading configuation from `" ^ path ^ "': " ^
2711 Printexc.to_string exn);
2712 end;
2713 close_in ic;
2715 | None -> ()
2716 else
2717 f (Hashtbl.create 0, defconf)
2720 let load () =
2721 let f (h, dc) =
2722 let pc, pb, px, pa =
2724 Hashtbl.find h (Filename.basename state.path)
2725 with Not_found -> dc, [], 0, (0, 0.0)
2727 setconf defconf dc;
2728 setconf conf pc;
2729 state.bookmarks <- pb;
2730 state.x <- px;
2731 cbput state.hists.nav pa;
2733 load1 f
2736 let add_attrs bb always dc c =
2737 let ob s a b =
2738 if always || a != b
2739 then Printf.bprintf bb "\n %s='%b'" s a
2740 and oi s a b =
2741 if always || a != b
2742 then Printf.bprintf bb "\n %s='%d'" s a
2743 and oz s a b =
2744 if always || a <> b
2745 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2747 let w, h =
2748 if always
2749 then dc.winw, dc.winh
2750 else
2751 match state.fullscreen with
2752 | Some wh -> wh
2753 | None -> c.winw, c.winh
2755 let zoom, presentation, interpagespace, showall=
2756 if always
2757 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2758 else
2759 match state.mode with
2760 | Birdseye (bc, _, _, _) ->
2761 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2762 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2764 oi "width" w dc.winw;
2765 oi "height" h dc.winh;
2766 oi "scroll-bar-width" c.scrollw dc.scrollw;
2767 oi "scroll-handle-height" c.scrollh dc.scrollh;
2768 ob "case-insensitive-search" c.icase dc.icase;
2769 ob "preload" c.preload dc.preload;
2770 oi "page-bias" c.pagebias dc.pagebias;
2771 oi "scroll-step" c.scrollincr dc.scrollincr;
2772 ob "max-height-fit" c.maxhfit dc.maxhfit;
2773 ob "crop-hack" c.crophack dc.crophack;
2774 ob "throttle" showall dc.showall;
2775 ob "highlight-links" c.hlinks dc.hlinks;
2776 ob "under-cursor-info" c.underinfo dc.underinfo;
2777 oi "vertical-margin" interpagespace dc.interpagespace;
2778 oz "zoom" zoom dc.zoom;
2779 ob "presentation" presentation dc.presentation;
2780 oi "rotation-angle" c.angle dc.angle;
2781 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2782 ob "proportional-display" c.proportional dc.proportional;
2783 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2784 oi "texcount" c.texcount dc.texcount;
2785 oi "slice-height" c.sliceheight dc.sliceheight;
2786 oi "thumbnail-width" c.thumbw dc.thumbw;
2789 let save () =
2790 let bb = Buffer.create 32768 in
2791 let f (h, dc) =
2792 Buffer.add_string bb "<llppconfig>\n<defaults ";
2793 add_attrs bb true dc dc;
2794 Buffer.add_string bb "/>\n";
2796 let adddoc path x anchor c bookmarks =
2797 if bookmarks == [] && c = dc && anchor = emptyanchor
2798 then ()
2799 else (
2800 Printf.bprintf bb "<doc path='%s'"
2801 (enent path 0 (String.length path));
2803 if anchor <> emptyanchor
2804 then (
2805 let n, y = anchor in
2806 Printf.bprintf bb " page='%d'" n;
2807 Printf.bprintf bb " rely='%f'" y;
2810 if x != 0
2811 then Printf.bprintf bb " pan='%d'" x;
2813 add_attrs bb false dc c;
2815 begin match bookmarks with
2816 | [] -> Buffer.add_string bb "/>\n"
2817 | _ ->
2818 Buffer.add_string bb ">\n<bookmarks>\n";
2819 List.iter (fun (title, _level, page, rely) ->
2820 Printf.bprintf bb
2821 "<item title='%s' page='%d' rely='%f'/>\n"
2822 (enent title 0 (String.length title))
2823 page
2824 rely
2825 ) bookmarks;
2826 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2827 end;
2831 let x =
2832 match state.mode with
2833 | Birdseye (_, x, _, _) -> x
2834 | _ -> state.x
2836 let basename = Filename.basename state.path in
2837 adddoc basename x (getanchor ()) conf
2838 (if conf.savebmarks then state.bookmarks else []);
2840 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2841 if basename <> path
2842 then adddoc path x y c bookmarks
2843 ) h;
2844 Buffer.add_string bb "</llppconfig>";
2846 load1 f;
2847 if Buffer.length bb > 0
2848 then
2850 let tmp = path ^ ".tmp" in
2851 let oc = open_out_bin tmp in
2852 Buffer.output_buffer oc bb;
2853 close_out oc;
2854 Sys.rename tmp path;
2855 with exn ->
2856 prerr_endline
2857 ("error while saving configuration: " ^ Printexc.to_string exn)
2859 end;;
2861 let () =
2862 Arg.parse
2863 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2864 (fun s -> state.path <- s)
2865 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2867 if String.length state.path = 0
2868 then (prerr_endline "filename missing"; exit 1);
2870 State.load ();
2872 let _ = Glut.init Sys.argv in
2873 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2874 let () = Glut.initWindowSize conf.winw conf.winh in
2875 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
2877 let csock, ssock =
2878 if Sys.os_type = "Unix"
2879 then
2880 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2881 else
2882 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2883 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2884 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2885 Unix.bind sock addr;
2886 Unix.listen sock 1;
2887 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2888 Unix.connect csock addr;
2889 let ssock, _ = Unix.accept sock in
2890 Unix.close sock;
2891 let opts sock =
2892 Unix.setsockopt sock Unix.TCP_NODELAY true;
2893 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2895 opts ssock;
2896 opts csock;
2897 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2898 ssock, csock
2901 let () = Glut.displayFunc display in
2902 let () = Glut.reshapeFunc reshape in
2903 let () = Glut.keyboardFunc keyboard in
2904 let () = Glut.specialFunc special in
2905 let () = Glut.idleFunc (Some idle) in
2906 let () = Glut.mouseFunc mouse in
2907 let () = Glut.motionFunc motion in
2908 let () = Glut.passiveMotionFunc pmotion in
2910 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2911 state.csock <- csock;
2912 state.ssock <- ssock;
2913 state.text <- "Opening " ^ state.path;
2914 writeopen state.path state.password;
2916 at_exit State.save;
2918 let rec handlelablglutbug () =
2920 Glut.mainLoop ();
2921 with Glut.BadEnum "key in special_of_int" ->
2922 showtext '!' " LablGlut bug: special key not recognized";
2923 handlelablglutbug ()
2925 handlelablglutbug ();