More birds eye visual tweaks
[llpp.git] / main.ml
blobaefce9aecdffb6bef2850561f08e58f9eb0c4da7
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
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
158 type outline = string * int * int * float;;
159 type outlines =
160 | Oarray of outline array
161 | Olist of outline list
162 | Onarrow of string * outline array * outline array
165 type rect = (float * float * float * float * float * float * float * float);;
167 type pagemapkey = (pageno * width * angle * proportional * gen);;
169 type state =
170 { mutable csock : Unix.file_descr
171 ; mutable ssock : Unix.file_descr
172 ; mutable w : int
173 ; mutable x : int
174 ; mutable y : int
175 ; mutable maxy : int
176 ; mutable layout : layout list
177 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
178 ; mutable pdims : (pageno * width * height * leftx) list
179 ; mutable pagecount : int
180 ; pagecache : string circbuf
181 ; mutable rendering : bool
182 ; mutable mstate : mstate
183 ; mutable searchpattern : string
184 ; mutable rects : (pageno * recttype * rect) list
185 ; mutable rects1 : (pageno * recttype * rect) list
186 ; mutable text : string
187 ; mutable fullscreen : (width * height) option
188 ; mutable birdseye : (conf * leftx * pageno) option
189 ; mutable textentry : textentry option
190 ; mutable outlines : outlines
191 ; mutable outline : (bool * int * int * outline array * string) option
192 ; mutable bookmarks : outline list
193 ; mutable path : string
194 ; mutable password : string
195 ; mutable invalidated : int
196 ; mutable colorscale : float
197 ; mutable memused : int
198 ; mutable birdseyepageno : pageno
199 ; mutable gen : gen
200 ; mutable throttle : layout list option
201 ; hists : hists
203 and hists =
204 { pat : string circbuf
205 ; pag : string circbuf
206 ; nav : float circbuf
210 let defconf =
211 { scrollw = 7
212 ; scrollh = 12
213 ; icase = true
214 ; preload = true
215 ; pagebias = 0
216 ; verbose = false
217 ; scrollincr = 24
218 ; maxhfit = true
219 ; crophack = false
220 ; autoscroll = false
221 ; showall = false
222 ; hlinks = false
223 ; underinfo = false
224 ; interpagespace = 2
225 ; zoom = 1.0
226 ; presentation = false
227 ; angle = 0
228 ; winw = 900
229 ; winh = 900
230 ; savebmarks = true
231 ; proportional = true
232 ; memlimit = 32*1024*1024
233 ; texcount = 256
234 ; sliceheight = 24
238 let conf = { defconf with angle = defconf.angle };;
240 let state =
241 { csock = Unix.stdin
242 ; ssock = Unix.stdin
243 ; w = 0
244 ; y = 0
245 ; x = 0
246 ; layout = []
247 ; maxy = max_int
248 ; pagemap = Hashtbl.create 10
249 ; pagecache = cbnew 100 ""
250 ; pdims = []
251 ; pagecount = 0
252 ; rendering = false
253 ; mstate = Mnone
254 ; rects = []
255 ; rects1 = []
256 ; text = ""
257 ; fullscreen = None
258 ; birdseye = None
259 ; textentry = None
260 ; searchpattern = ""
261 ; outlines = Olist []
262 ; outline = None
263 ; bookmarks = []
264 ; path = ""
265 ; password = ""
266 ; invalidated = 0
267 ; hists =
268 { nav = cbnew 100 0.0
269 ; pat = cbnew 20 ""
270 ; pag = cbnew 10 ""
272 ; colorscale = 1.0
273 ; memused = 0
274 ; birdseyepageno = -1
275 ; gen = 0
276 ; throttle = None
280 let vlog fmt =
281 if conf.verbose
282 then
283 Printf.kprintf prerr_endline fmt
284 else
285 Printf.kprintf ignore fmt
288 let writecmd fd s =
289 let len = String.length s in
290 let n = 4 + len in
291 let b = Buffer.create n in
292 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
293 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
294 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
295 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
296 Buffer.add_string b s;
297 let s' = Buffer.contents b in
298 let n' = Unix.write fd s' 0 n in
299 if n' != n then failwith "write failed";
302 let readcmd fd =
303 let s = "xxxx" in
304 let n = Unix.read fd s 0 4 in
305 if n != 4 then failwith "incomplete read(len)";
306 let len = 0
307 lor (Char.code s.[0] lsl 24)
308 lor (Char.code s.[1] lsl 16)
309 lor (Char.code s.[2] lsl 8)
310 lor (Char.code s.[3] lsl 0)
312 let s = String.create len in
313 let n = Unix.read fd s 0 len in
314 if n != len then failwith "incomplete read(data)";
318 let yratio y =
319 if y = state.maxy
320 then 1.0
321 else float y /. float state.maxy
324 let makecmd s l =
325 let b = Buffer.create 10 in
326 Buffer.add_string b s;
327 let rec combine = function
328 | [] -> b
329 | x :: xs ->
330 Buffer.add_char b ' ';
331 let s =
332 match x with
333 | `b b -> if b then "1" else "0"
334 | `s s -> s
335 | `i i -> string_of_int i
336 | `f f -> string_of_float f
337 | `I f -> string_of_int (truncate f)
339 Buffer.add_string b s;
340 combine xs;
342 combine l;
345 let wcmd s l =
346 let cmd = Buffer.contents (makecmd s l) in
347 writecmd state.csock cmd;
350 let calcips h =
351 if conf.presentation
352 then
353 let d = conf.winh - h in
354 max 0 ((d + 1) / 2)
355 else
356 conf.interpagespace
359 let calcheight () =
360 let rec f pn ph pi fh l =
361 match l with
362 | (n, _, h, _) :: rest ->
363 let ips = calcips h in
364 let fh =
365 if conf.presentation
366 then fh+ips
367 else (
368 if state.birdseye <> None && pn = 0
369 then fh + ips
370 else fh
373 let fh = fh + ((n - pn) * (ph + pi)) in
374 f n h ips fh rest
376 | [] ->
377 let inc =
378 if conf.presentation || (state.birdseye <> None && pn = 0)
379 then 0
380 else -pi
382 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
383 max 0 fh
385 let fh = f 0 0 0 0 state.pdims in
389 let getpageyh pageno =
390 let rec f pn ph pi y l =
391 match l with
392 | (n, _, h, _) :: rest ->
393 let ips = calcips h in
394 if n >= pageno
395 then
396 if conf.presentation && n = pageno
397 then
398 y + (pageno - pn) * (ph + pi) + pi, h
399 else
400 y + (pageno - pn) * (ph + pi), h
401 else
402 let y = y + (if conf.presentation then pi else 0) in
403 let y = y + (n - pn) * (ph + pi) in
404 f n h ips y rest
406 | [] ->
407 y + (pageno - pn) * (ph + pi), ph
409 f 0 0 0 0 state.pdims
412 let getpagey pageno = fst (getpageyh pageno);;
414 let layout y sh =
415 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
416 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
417 match pdims with
418 | (pageno', w, h, x) :: rest when pageno' = pageno ->
419 let ips = calcips h in
420 let yinc =
421 if conf.presentation || (state.birdseye <> None && pageno = 0)
422 then ips
423 else 0
425 (w, h, ips, x), rest, pdimno + 1, yinc
426 | _ ->
427 prev, pdims, pdimno, 0
429 let dy = dy + yinc in
430 let py = py + yinc in
431 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
432 then
433 accu
434 else
435 let vy = y + dy in
436 if py + h <= vy - yinc
437 then
438 let py = py + h + ips in
439 let dy = max 0 (py - y) in
440 f ~pageno:(pageno+1)
441 ~pdimno
442 ~prev:curr
445 ~pdims:rest
446 ~cacheleft
447 ~accu
448 else
449 let pagey = vy - py in
450 let pagevh = h - pagey in
451 let pagevh = min (sh - dy) pagevh in
452 let off = if yinc > 0 then py - vy else 0 in
453 let py = py + h + ips in
454 let e =
455 { pageno = pageno
456 ; pagedimno = pdimno
457 ; pagew = w
458 ; pageh = h
459 ; pagedispy = dy + off
460 ; pagey = pagey + off
461 ; pagevh = pagevh - off
462 ; pagex = x
465 let accu = e :: accu in
466 f ~pageno:(pageno+1)
467 ~pdimno
468 ~prev:curr
470 ~dy:(dy+pagevh+ips)
471 ~pdims:rest
472 ~cacheleft:(cacheleft-1)
473 ~accu
475 if state.invalidated = 0
476 then (
477 let accu =
479 ~pageno:0
480 ~pdimno:~-1
481 ~prev:(0,0,0,0)
482 ~py:0
483 ~dy:0
484 ~pdims:state.pdims
485 ~cacheleft:(cbcap state.pagecache)
486 ~accu:[]
488 List.rev accu
490 else
494 let clamp incr =
495 let y = state.y + incr in
496 let y = max 0 y in
497 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
501 let getopaque pageno =
502 try Some (Hashtbl.find state.pagemap
503 (pageno, state.w, conf.angle, conf.proportional, state.gen))
504 with Not_found -> None
507 let cache pageno opaque =
508 Hashtbl.replace state.pagemap
509 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
512 let validopaque opaque = String.length opaque > 0;;
514 let render l =
515 match getopaque l.pageno with
516 | None when not state.rendering ->
517 state.rendering <- true;
518 cache l.pageno ("", -1);
519 wcmd "render" [`i (l.pageno + 1)
520 ;`i l.pagedimno
521 ;`i l.pagew
522 ;`i l.pageh];
524 | _ -> ()
527 let loadlayout layout =
528 let rec f all = function
529 | l :: ls ->
530 begin match getopaque l.pageno with
531 | None -> render l; f false ls
532 | Some (opaque, _) -> f (all && validopaque opaque) ls
534 | [] -> all
536 f (layout <> []) layout;
539 let findpageforopaque opaque =
540 Hashtbl.fold
541 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
542 state.pagemap None
545 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
547 let preload () =
548 if conf.preload
549 then
550 let oktopreload =
551 let memleft = conf.memlimit - state.memused in
552 if memleft < 0
553 then
554 let opaque = cbpeek state.pagecache in
555 match findpageforopaque opaque with
556 | Some ((n, _, _, _, _), size) ->
557 memleft + size >= 0 && not (pagevisible state.layout n)
558 | None -> false
559 else true
561 if oktopreload
562 then
563 let rely = yratio state.y in
564 let presentation = conf.presentation in
565 let interpagespace = conf.interpagespace in
566 let maxy = state.maxy in
567 conf.presentation <- false;
568 conf.interpagespace <- 0;
569 state.maxy <- calcheight ();
570 let y = truncate (float state.maxy *. rely) in
571 let y = if y < conf.winh then 0 else y - conf.winh in
572 let pages = layout y (conf.winh*3) in
573 List.iter render pages;
574 conf.presentation <- presentation;
575 conf.interpagespace <- interpagespace;
576 state.maxy <- maxy;
579 let gotoy y =
580 let y = max 0 y in
581 let y = min state.maxy y in
582 let pages = layout y conf.winh in
583 let ready = loadlayout pages in
584 if conf.showall
585 then (
586 if ready
587 then (
588 state.y <- y;
589 state.layout <- pages;
590 state.throttle <- None;
591 Glut.postRedisplay ();
593 else (
594 state.throttle <- Some pages;
597 else (
598 state.y <- y;
599 state.layout <- pages;
600 state.throttle <- None;
601 Glut.postRedisplay ();
603 if state.birdseye <> None
604 then (
605 if not (pagevisible pages state.birdseyepageno)
606 then
607 match state.layout with
608 | [] -> ()
609 | l :: _ -> state.birdseyepageno <- l.pageno
611 preload ();
614 let gotoy_and_clear_text y =
615 gotoy y;
616 if not conf.verbose then state.text <- "";
619 let addnav () =
620 cbput state.hists.nav (yratio state.y);
623 let getnav () =
624 let y = cbgetc state.hists.nav ~-1 in
625 truncate (y *. float state.maxy)
628 let gotopage n top =
629 let y, h = getpageyh n in
630 addnav ();
631 gotoy_and_clear_text (y + (truncate (top *. float h)));
634 let gotopage1 n top =
635 let y = getpagey n in
636 addnav ();
637 gotoy_and_clear_text (y + top);
640 let invalidate () =
641 state.layout <- [];
642 state.pdims <- [];
643 state.rects <- [];
644 state.rects1 <- [];
645 state.invalidated <- state.invalidated + 1;
648 let scalecolor c =
649 let c = c *. state.colorscale in
650 (c, c, c);
653 let represent () =
654 let y =
655 if state.birdseyepageno = -1 && state.birdseye = None
656 then
657 match state.layout with
658 | [] ->
659 let rely = yratio state.y in
660 state.maxy <- calcheight ();
661 truncate (float state.maxy *. rely)
663 | l :: _ ->
664 state.maxy <- calcheight ();
665 getpagey l.pageno
666 else (
667 let y = getpagey state.birdseyepageno in
668 state.birdseyepageno <- -1;
672 gotoy y
675 let pagematrix () =
676 GlMat.mode `projection;
677 GlMat.load_identity ();
678 GlMat.rotate ~x:1.0 ~angle:180.0 ();
679 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
680 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
683 let winmatrix () =
684 GlMat.mode `projection;
685 GlMat.load_identity ();
686 GlMat.rotate ~x:1.0 ~angle:180.0 ();
687 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
688 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
691 let reshape ~w ~h =
692 conf.winw <- w;
693 let w = truncate (float w *. conf.zoom) - conf.scrollw in
694 let w = max w 2 in
695 state.w <- w;
696 conf.winh <- h;
697 GlMat.mode `modelview;
698 GlMat.load_identity ();
699 GlClear.color (scalecolor 1.0);
700 GlClear.clear [`color];
702 invalidate ();
703 wcmd "geometry" [`i w; `i h];
706 let showtext c s =
707 GlDraw.color (0.0, 0.0, 0.0);
708 GlDraw.rect
709 (0.0, float (conf.winh - 18))
710 (float (conf.winw - conf.scrollw - 1), float conf.winh)
712 let font = Glut.BITMAP_8_BY_13 in
713 GlDraw.color (1.0, 1.0, 1.0);
714 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
715 Glut.bitmapCharacter ~font ~c:(Char.code c);
716 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
719 let enttext () =
720 let len = String.length state.text in
721 match state.textentry with
722 | None ->
723 if len > 0 then showtext ' ' state.text
725 | Some (c, text, _, _, _) ->
726 let s =
727 if len > 0
728 then
729 text ^ " [" ^ state.text ^ "]"
730 else
731 text
733 showtext c s;
736 let showtext c s =
737 if true
738 then (
739 state.text <- Printf.sprintf "%c%s" c s;
740 Glut.postRedisplay ();
742 else (
743 showtext c s;
744 Glut.swapBuffers ();
748 let act cmd =
749 match cmd.[0] with
750 | 'c' ->
751 state.pdims <- [];
753 | 'D' ->
754 state.rects <- state.rects1;
755 Glut.postRedisplay ()
757 | 'C' ->
758 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
759 state.pagecount <- n;
760 state.invalidated <- state.invalidated - 1;
761 if state.invalidated = 0
762 then represent ()
764 | 't' ->
765 let s = Scanf.sscanf cmd "t %n"
766 (fun n -> String.sub cmd n (String.length cmd - n))
768 Glut.setWindowTitle s
770 | 'T' ->
771 let s = Scanf.sscanf cmd "T %n"
772 (fun n -> String.sub cmd n (String.length cmd - n))
774 if state.textentry = None
775 then (
776 state.text <- s;
777 showtext ' ' s;
779 else (
780 state.text <- s;
781 Glut.postRedisplay ();
784 | 'V' ->
785 if conf.verbose
786 then
787 let s = Scanf.sscanf cmd "V %n"
788 (fun n -> String.sub cmd n (String.length cmd - n))
790 state.text <- s;
791 showtext ' ' s;
793 | 'F' ->
794 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
795 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
796 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
797 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
799 let y = (getpagey pageno) + truncate y0 in
800 addnav ();
801 gotoy y;
802 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
804 | 'R' ->
805 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
806 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
807 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
808 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
810 state.rects1 <-
811 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
813 | 'r' ->
814 let n, w, h, r, l, s, p =
815 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
816 (fun n w h r l s p ->
817 (n-1, w, h, r, l != 0, s, p))
820 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
821 state.memused <- state.memused + s;
823 let layout =
824 match state.throttle with
825 | None -> state.layout
826 | Some layout -> layout
829 let rec gc () =
830 if (state.memused <= conf.memlimit) || cbempty state.pagecache
831 then ()
832 else (
833 let evictedopaque = cbpeek state.pagecache in
834 match findpageforopaque evictedopaque with
835 | None -> failwith "bug in gc"
836 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
837 if state.gen != gen || not (pagevisible layout evictedn)
838 then (
839 wcmd "free" [`s evictedopaque];
840 state.memused <- state.memused - evictedsize;
841 Hashtbl.remove state.pagemap k;
842 cbdecr state.pagecache;
843 gc ();
847 gc ();
849 cbput state.pagecache p;
850 state.rendering <- false;
852 begin match state.throttle with
853 | None ->
854 if pagevisible state.layout n
855 then gotoy state.y
856 else (
857 let allvisible = loadlayout state.layout in
858 if allvisible then preload ();
861 | Some layout ->
862 match layout with
863 | [] -> ()
864 | l :: _ ->
865 let y = getpagey l.pageno + l.pagey in
866 gotoy y
869 | 'l' ->
870 let (n, w, h, x) as pdim =
871 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
873 state.pdims <- pdim :: state.pdims
875 | 'o' ->
876 let (l, n, t, h, pos) =
877 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
879 let s = String.sub cmd pos (String.length cmd - pos) in
880 let s =
881 let l = String.length s in
882 let b = Buffer.create (String.length s) in
883 let rec loop pc2 i =
884 if i = l
885 then ()
886 else
887 let pc2 =
888 match s.[i] with
889 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
890 | '\xc2' -> true
891 | c ->
892 let c = if Char.code c land 0x80 = 0 then c else '?' in
893 Buffer.add_char b c;
894 false
896 loop pc2 (i+1)
898 loop false 0;
899 Buffer.contents b
901 let outline = (s, l, n, float t /. float h) in
902 let outlines =
903 match state.outlines with
904 | Olist outlines -> Olist (outline :: outlines)
905 | Oarray _ -> Olist [outline]
906 | Onarrow _ -> Olist [outline]
908 state.outlines <- outlines
910 | _ ->
911 log "unknown cmd `%S'" cmd
914 let now = Unix.gettimeofday;;
916 let idle () =
917 let rec loop delay =
918 let r, _, _ = Unix.select [state.csock] [] [] delay in
919 begin match r with
920 | [] ->
921 if conf.autoscroll
922 then begin
923 let y = state.y + conf.scrollincr in
924 let y = if y >= state.maxy then 0 else y in
925 gotoy y;
926 state.text <- "";
927 end;
929 | _ ->
930 let cmd = readcmd state.csock in
931 act cmd;
932 loop 0.0
933 end;
934 in loop 0.001
937 let onhist cb =
938 let rc = cb.rc in
939 let action = function
940 | HCprev -> cbget cb ~-1
941 | HCnext -> cbget cb 1
942 | HCfirst -> cbget cb ~-(cb.rc)
943 | HClast -> cbget cb (cb.len - 1 - cb.rc)
944 and cancel () = cb.rc <- rc
945 in (action, cancel)
948 let search pattern forward =
949 if String.length pattern > 0
950 then
951 let pn, py =
952 match state.layout with
953 | [] -> 0, 0
954 | l :: _ ->
955 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
957 let cmd =
958 let b = makecmd "search"
959 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
961 Buffer.add_char b ',';
962 Buffer.add_string b pattern;
963 Buffer.add_char b '\000';
964 Buffer.contents b;
966 writecmd state.csock cmd;
969 let intentry text key =
970 let c = Char.unsafe_chr key in
971 match c with
972 | '0' .. '9' ->
973 let s = "x" in s.[0] <- c;
974 let text = text ^ s in
975 TEcont text
977 | _ ->
978 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
979 TEcont text
982 let addchar s c =
983 let b = Buffer.create (String.length s + 1) in
984 Buffer.add_string b s;
985 Buffer.add_char b c;
986 Buffer.contents b;
989 let textentry text key =
990 let c = Char.unsafe_chr key in
991 match c with
992 | _ when key >= 32 && key < 127 ->
993 let text = addchar text c in
994 TEcont text
996 | _ ->
997 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
998 TEcont text
1001 let reinit angle proportional =
1002 conf.angle <- angle;
1003 conf.proportional <- proportional;
1004 invalidate ();
1005 wcmd "reinit" [`i angle; `b proportional];
1008 let optentry text key =
1009 let btos b = if b then "on" else "off" in
1010 let c = Char.unsafe_chr key in
1011 match c with
1012 | 's' ->
1013 let ondone s =
1014 try conf.scrollincr <- int_of_string s with exc ->
1015 state.text <- Printf.sprintf "bad integer `%s': %s"
1016 s (Printexc.to_string exc)
1018 TEswitch ('#', "", None, intentry, ondone)
1020 | 'R' ->
1021 let ondone s =
1022 match try
1023 Some (int_of_string s)
1024 with exc ->
1025 state.text <- Printf.sprintf "bad integer `%s': %s"
1026 s (Printexc.to_string exc);
1027 None
1028 with
1029 | Some angle -> reinit angle conf.proportional
1030 | None -> ()
1032 TEswitch ('^', "", None, intentry, ondone)
1034 | 'i' ->
1035 conf.icase <- not conf.icase;
1036 TEdone ("case insensitive search " ^ (btos conf.icase))
1038 | 'p' ->
1039 conf.preload <- not conf.preload;
1040 gotoy state.y;
1041 TEdone ("preload " ^ (btos conf.preload))
1043 | 'v' ->
1044 conf.verbose <- not conf.verbose;
1045 TEdone ("verbose " ^ (btos conf.verbose))
1047 | 'h' ->
1048 conf.maxhfit <- not conf.maxhfit;
1049 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1050 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1052 | 'c' ->
1053 conf.crophack <- not conf.crophack;
1054 TEdone ("crophack " ^ btos conf.crophack)
1056 | 'a' ->
1057 conf.showall <- not conf.showall;
1058 TEdone ("showall " ^ btos conf.showall)
1060 | 'f' ->
1061 conf.underinfo <- not conf.underinfo;
1062 TEdone ("underinfo " ^ btos conf.underinfo)
1064 | 'P' ->
1065 conf.savebmarks <- not conf.savebmarks;
1066 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1068 | 'S' ->
1069 let ondone s =
1071 let pageno, py =
1072 match state.layout with
1073 | [] -> 0, 0
1074 | l :: _ ->
1075 l.pageno, l.pagey
1077 conf.interpagespace <- int_of_string s;
1078 state.maxy <- calcheight ();
1079 let y = getpagey pageno in
1080 gotoy (y + py)
1081 with exc ->
1082 state.text <- Printf.sprintf "bad integer `%s': %s"
1083 s (Printexc.to_string exc)
1085 TEswitch ('%', "", None, intentry, ondone)
1087 | 'l' ->
1088 reinit conf.angle (not conf.proportional);
1089 TEdone ("proprortional display " ^ btos conf.proportional)
1091 | _ ->
1092 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1093 TEstop
1096 let maxoutlinerows () = (conf.winh - 31) / 16;;
1098 let enterselector allowdel outlines errmsg msg =
1099 if Array.length outlines = 0
1100 then (
1101 showtext ' ' errmsg;
1103 else (
1104 state.text <- msg;
1105 Glut.setCursor Glut.CURSOR_INHERIT;
1106 let pageno =
1107 match state.layout with
1108 | [] -> -1
1109 | {pageno=pageno} :: rest -> pageno
1111 let active =
1112 let rec loop n =
1113 if n = Array.length outlines
1114 then 0
1115 else
1116 let (_, _, outlinepageno, _) = outlines.(n) in
1117 if outlinepageno >= pageno then n else loop (n+1)
1119 loop 0
1121 state.outline <-
1122 Some (allowdel, active,
1123 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1124 Glut.postRedisplay ();
1128 let enteroutlinemode () =
1129 let outlines, msg =
1130 match state.outlines with
1131 | Oarray a -> a, ""
1132 | Olist l ->
1133 let a = Array.of_list (List.rev l) in
1134 state.outlines <- Oarray a;
1135 a, ""
1136 | Onarrow (pat, a, b) ->
1137 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1139 enterselector false outlines "Document has no outline" msg;
1142 let enterbookmarkmode () =
1143 let bookmarks = Array.of_list state.bookmarks in
1144 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1147 let quickbookmark ?title () =
1148 match state.layout with
1149 | [] -> ()
1150 | l :: _ ->
1151 let title =
1152 match title with
1153 | None ->
1154 let sec = Unix.gettimeofday () in
1155 let tm = Unix.localtime sec in
1156 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1157 (l.pageno+1)
1158 tm.Unix.tm_mday
1159 tm.Unix.tm_mon
1160 (tm.Unix.tm_year + 1900)
1161 tm.Unix.tm_hour
1162 tm.Unix.tm_min
1163 | Some title -> title
1165 state.bookmarks <-
1166 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1169 let doreshape w h =
1170 state.fullscreen <- None;
1171 Glut.reshapeWindow w h;
1174 let writeopen path password =
1175 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1178 let opendoc path password =
1179 invalidate ();
1180 state.path <- path;
1181 state.password <- password;
1182 state.gen <- state.gen + 1;
1184 writeopen path password;
1185 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1186 wcmd "geometry" [`i state.w; `i conf.winh];
1189 let birdseyeoff (c, leftx, _) =
1190 state.birdseye <- None;
1191 conf.zoom <- c.zoom;
1192 conf.presentation <- c.presentation;
1193 conf.interpagespace <- c.interpagespace;
1194 conf.showall <- c.showall;
1195 conf.hlinks <- c.hlinks;
1196 state.x <- leftx;
1197 if conf.verbose
1198 then
1199 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1200 (100.0*.conf.zoom)
1204 let viewkeyboard ~key ~x ~y =
1205 let enttext te =
1206 state.textentry <- te;
1207 state.text <- "";
1208 enttext ();
1209 Glut.postRedisplay ()
1211 match state.textentry with
1212 | None ->
1213 let c = Char.chr key in
1214 begin match c with
1215 | '\027' | 'q' ->
1216 exit 0
1218 | '\008' ->
1219 let y = getnav () in
1220 gotoy_and_clear_text y
1222 | '\013' ->
1223 begin match state.birdseye with
1224 | None -> ()
1225 | Some vals ->
1226 birdseyeoff vals;
1227 reshape conf.winw conf.winh;
1228 end;
1230 | 'o' ->
1231 enteroutlinemode ()
1233 | 'u' ->
1234 state.rects <- [];
1235 state.text <- "";
1236 Glut.postRedisplay ()
1238 | '/' | '?' ->
1239 let ondone isforw s =
1240 cbput state.hists.pat s;
1241 state.searchpattern <- s;
1242 search s isforw
1244 enttext (Some (c, "", Some (onhist state.hists.pat),
1245 textentry, ondone (c ='/')))
1247 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1248 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1249 conf.zoom <- min 2.2 (conf.zoom +. incr);
1250 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1251 reshape conf.winw conf.winh
1253 | '+' ->
1254 let ondone s =
1255 let n =
1256 try int_of_string s with exc ->
1257 state.text <- Printf.sprintf "bad integer `%s': %s"
1258 s (Printexc.to_string exc);
1259 max_int
1261 if n != max_int
1262 then (
1263 conf.pagebias <- n;
1264 state.text <- "page bias is now " ^ string_of_int n;
1267 enttext (Some ('+', "", None, intentry, ondone))
1269 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1270 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1271 conf.zoom <- max 0.01 (conf.zoom -. decr);
1272 if conf.zoom <= 1.0 then state.x <- 0;
1273 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1274 reshape conf.winw conf.winh;
1276 | '-' ->
1277 let ondone msg =
1278 state.text <- msg;
1280 enttext (Some ('-', "", None, optentry, ondone))
1282 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1283 state.x <- 0;
1284 conf.zoom <- 1.0;
1285 state.text <- "zoom is 100%";
1286 reshape conf.winw conf.winh
1288 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1289 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1290 if zoom < 1.0
1291 then (
1292 conf.zoom <- zoom;
1293 state.x <- 0;
1294 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1295 reshape conf.winw conf.winh;
1298 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1299 begin match state.birdseye with
1300 | None ->
1301 let zoom = 76.0 /. float state.w in
1302 let birdseyepageno =
1303 match state.layout with
1304 | [] -> 0
1305 | l :: _ -> l.pageno
1307 state.birdseyepageno <- birdseyepageno;
1308 state.birdseye <-
1309 Some ({ conf with zoom = conf.zoom }, state.x, -1);
1310 conf.zoom <- zoom;
1311 conf.presentation <- false;
1312 conf.interpagespace <- 10;
1313 conf.hlinks <- false;
1314 state.x <- 0;
1315 state.mstate <- Mnone;
1316 conf.showall <- false;
1317 Glut.setCursor Glut.CURSOR_INHERIT;
1318 if conf.verbose
1319 then
1320 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1321 (100.0*.zoom)
1322 else
1323 state.text <- ""
1326 | Some vals ->
1327 birdseyeoff vals;
1328 end;
1329 reshape conf.winw conf.winh
1331 | '0' .. '9' ->
1332 let ondone s =
1333 let n =
1334 try int_of_string s with exc ->
1335 state.text <- Printf.sprintf "bad integer `%s': %s"
1336 s (Printexc.to_string exc);
1339 if n >= 0
1340 then (
1341 addnav ();
1342 cbput state.hists.pag (string_of_int n);
1343 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1346 let pageentry text key =
1347 match Char.unsafe_chr key with
1348 | 'g' -> TEdone text
1349 | _ -> intentry text key
1351 let text = "x" in text.[0] <- c;
1352 enttext (Some (':', text, Some (onhist state.hists.pag),
1353 pageentry, ondone))
1355 | 'b' ->
1356 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1357 reshape conf.winw conf.winh;
1359 | 'l' ->
1360 conf.hlinks <- not conf.hlinks;
1361 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1362 Glut.postRedisplay ()
1364 | 'a' ->
1365 conf.autoscroll <- not conf.autoscroll
1367 | 'P' ->
1368 conf.presentation <- not conf.presentation;
1369 showtext ' ' ("presentation mode " ^
1370 if conf.presentation then "on" else "off");
1371 represent ()
1373 | 'f' ->
1374 begin match state.fullscreen with
1375 | None ->
1376 state.fullscreen <- Some (conf.winw, conf.winh);
1377 Glut.fullScreen ()
1378 | Some (w, h) ->
1379 state.fullscreen <- None;
1380 doreshape w h
1383 | 'g' ->
1384 gotoy_and_clear_text 0
1386 | 'n' ->
1387 search state.searchpattern true
1389 | 'p' | 'N' ->
1390 search state.searchpattern false
1392 | 't' ->
1393 begin match state.layout with
1394 | [] -> ()
1395 | l :: _ ->
1396 gotoy_and_clear_text (getpagey l.pageno)
1399 | ' ' ->
1400 begin match List.rev state.layout with
1401 | [] -> ()
1402 | l :: _ ->
1403 let pageno = min (l.pageno+1) (state.pagecount-1) in
1404 gotoy_and_clear_text (getpagey pageno)
1407 | '\127' ->
1408 begin match state.layout with
1409 | [] -> ()
1410 | l :: _ ->
1411 let pageno = max 0 (l.pageno-1) in
1412 gotoy_and_clear_text (getpagey pageno)
1415 | '=' ->
1416 let f (fn, ln) l =
1417 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1419 let fn, ln = List.fold_left f (-1, -1) state.layout in
1420 let s =
1421 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1422 let percent =
1423 if maxy <= 0
1424 then 100.
1425 else (100. *. (float state.y /. float maxy)) in
1426 if fn = ln
1427 then
1428 Printf.sprintf "Page %d of %d %.2f%%"
1429 (fn+1) state.pagecount percent
1430 else
1431 Printf.sprintf
1432 "Pages %d-%d of %d %.2f%%"
1433 (fn+1) (ln+1) state.pagecount percent
1435 showtext ' ' s;
1437 | 'w' ->
1438 begin match state.layout with
1439 | [] -> ()
1440 | l :: _ ->
1441 doreshape (l.pagew + conf.scrollw) l.pageh;
1442 Glut.postRedisplay ();
1445 | '\'' ->
1446 enterbookmarkmode ()
1448 | 'm' ->
1449 let ondone s =
1450 match state.layout with
1451 | l :: _ ->
1452 state.bookmarks <-
1453 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1454 :: state.bookmarks
1455 | _ -> ()
1457 enttext (Some ('~', "", None, textentry, ondone))
1459 | '~' ->
1460 quickbookmark ();
1461 showtext ' ' "Quick bookmark added";
1463 | 'z' ->
1464 begin match state.layout with
1465 | l :: _ ->
1466 let rect = getpdimrect l.pagedimno in
1467 let w, h =
1468 if conf.crophack
1469 then
1470 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1471 truncate (1.2 *. (rect.(3) -. rect.(0))))
1472 else
1473 (truncate (rect.(1) -. rect.(0)),
1474 truncate (rect.(3) -. rect.(0)))
1476 if w != 0 && h != 0
1477 then
1478 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1480 Glut.postRedisplay ();
1482 | [] -> ()
1485 | '<' | '>' ->
1486 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1488 | '[' | ']' ->
1489 state.colorscale <-
1490 max 0.0
1491 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1492 Glut.postRedisplay ()
1494 | 'k' -> gotoy (clamp (-conf.scrollincr))
1495 | 'j' -> gotoy (clamp conf.scrollincr)
1497 | 'r' -> opendoc state.path state.password
1499 | _ ->
1500 vlog "huh? %d %c" key (Char.chr key);
1503 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1504 let len = String.length text in
1505 if len = 0
1506 then (
1507 state.textentry <- None;
1508 Glut.postRedisplay ();
1510 else (
1511 let s = String.sub text 0 (len - 1) in
1512 enttext (Some (c, s, opthist, onkey, ondone))
1515 | Some (c, text, onhist, onkey, ondone) ->
1516 begin match Char.unsafe_chr key with
1517 | '\r' | '\n' ->
1518 ondone text;
1519 state.textentry <- None;
1520 Glut.postRedisplay ()
1522 | '\027' ->
1523 begin match onhist with
1524 | None -> ()
1525 | Some (_, onhistcancel) -> onhistcancel ()
1526 end;
1527 state.textentry <- None;
1528 Glut.postRedisplay ()
1530 | _ ->
1531 begin match onkey text key with
1532 | TEdone text ->
1533 state.textentry <- None;
1534 ondone text;
1535 Glut.postRedisplay ()
1537 | TEcont text ->
1538 enttext (Some (c, text, onhist, onkey, ondone));
1540 | TEstop ->
1541 state.textentry <- None;
1542 Glut.postRedisplay ()
1544 | TEswitch te ->
1545 state.textentry <- Some te;
1546 Glut.postRedisplay ()
1547 end;
1548 end;
1551 let narrow outlines pattern =
1552 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1553 match reopt with
1554 | None -> None
1555 | Some re ->
1556 let rec fold accu n =
1557 if n = -1
1558 then accu
1559 else
1560 let (s, _, _, _) as o = outlines.(n) in
1561 let accu =
1562 if (try ignore (Str.search_forward re s 0); true
1563 with Not_found -> false)
1564 then (o :: accu)
1565 else accu
1567 fold accu (n-1)
1569 let matched = fold [] (Array.length outlines - 1) in
1570 if matched = [] then None else Some (Array.of_list matched)
1573 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1574 let search active pattern incr =
1575 let dosearch re =
1576 let rec loop n =
1577 if n = Array.length outlines || n = -1
1578 then None
1579 else
1580 let (s, _, _, _) = outlines.(n) in
1582 (try ignore (Str.search_forward re s 0); true
1583 with Not_found -> false)
1584 then Some n
1585 else loop (n + incr)
1587 loop active
1590 let re = Str.regexp_case_fold pattern in
1591 dosearch re
1592 with Failure s ->
1593 state.text <- s;
1594 None
1596 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1597 match key with
1598 | 27 ->
1599 if String.length qsearch = 0
1600 then (
1601 state.text <- "";
1602 state.outline <- None;
1603 Glut.postRedisplay ();
1605 else (
1606 state.text <- "";
1607 state.outline <- Some (allowdel, active, first, outlines, "");
1608 Glut.postRedisplay ();
1611 | 18 | 19 ->
1612 let incr = if key = 18 then -1 else 1 in
1613 let active, first =
1614 match search (active + incr) qsearch incr with
1615 | None ->
1616 state.text <- qsearch ^ " [not found]";
1617 active, first
1618 | Some active ->
1619 state.text <- qsearch;
1620 active, firstof active
1622 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1623 Glut.postRedisplay ();
1625 | 8 ->
1626 let len = String.length qsearch in
1627 if len = 0
1628 then ()
1629 else (
1630 if len = 1
1631 then (
1632 state.text <- "";
1633 state.outline <- Some (allowdel, active, first, outlines, "");
1635 else
1636 let qsearch = String.sub qsearch 0 (len - 1) in
1637 let active, first =
1638 match search active qsearch ~-1 with
1639 | None ->
1640 state.text <- qsearch ^ " [not found]";
1641 active, first
1642 | Some active ->
1643 state.text <- qsearch;
1644 active, firstof active
1646 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1648 Glut.postRedisplay ()
1650 | 13 ->
1651 if active < Array.length outlines
1652 then (
1653 let (_, _, n, t) = outlines.(active) in
1654 gotopage n t;
1656 state.text <- "";
1657 if allowdel then state.bookmarks <- Array.to_list outlines;
1658 state.outline <- None;
1659 Glut.postRedisplay ();
1661 | _ when key >= 32 && key < 127 ->
1662 let pattern = addchar qsearch (Char.chr key) in
1663 let active, first =
1664 match search active pattern 1 with
1665 | None ->
1666 state.text <- pattern ^ " [not found]";
1667 active, first
1668 | Some active ->
1669 state.text <- pattern;
1670 active, firstof active
1672 state.outline <- Some (allowdel, active, first, outlines, pattern);
1673 Glut.postRedisplay ()
1675 | 14 when not allowdel -> (* ctrl-n *)
1676 if String.length qsearch > 0
1677 then (
1678 let optoutlines = narrow outlines qsearch in
1679 begin match optoutlines with
1680 | None -> state.text <- "can't narrow"
1681 | Some outlines ->
1682 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1683 match state.outlines with
1684 | Olist l -> ()
1685 | Oarray a ->
1686 state.outlines <- Onarrow (qsearch, outlines, a)
1687 | Onarrow (pat, a, b) ->
1688 state.outlines <- Onarrow (qsearch, outlines, b)
1689 end;
1691 Glut.postRedisplay ()
1693 | 21 when not allowdel -> (* ctrl-u *)
1694 let outline =
1695 match state.outlines with
1696 | Oarray a -> a
1697 | Olist l ->
1698 let a = Array.of_list (List.rev l) in
1699 state.outlines <- Oarray a;
1701 | Onarrow (pat, a, b) ->
1702 state.outlines <- Oarray b;
1703 state.text <- "";
1706 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1707 Glut.postRedisplay ()
1709 | 12 ->
1710 state.outline <-
1711 Some (allowdel, active, firstof active, outlines, qsearch);
1712 Glut.postRedisplay ()
1714 | 127 when allowdel ->
1715 let len = Array.length outlines - 1 in
1716 if len = 0
1717 then (
1718 state.outline <- None;
1719 state.bookmarks <- [];
1721 else (
1722 let bookmarks = Array.init len
1723 (fun i ->
1724 let i = if i >= active then i + 1 else i in
1725 outlines.(i)
1728 state.outline <-
1729 Some (allowdel,
1730 min active (len-1),
1731 min first (len-1),
1732 bookmarks, qsearch)
1735 Glut.postRedisplay ()
1737 | _ -> log "unknown key %d" key
1740 let keyboard ~key ~x ~y =
1741 if key = 7
1742 then
1743 wcmd "interrupt" []
1744 else
1745 match state.outline with
1746 | None -> viewkeyboard ~key ~x ~y
1747 | Some outline -> outlinekeyboard ~key ~x ~y outline
1750 let special ~key ~x ~y =
1751 match state.outline with
1752 | None when state.birdseye <> None ->
1753 begin match key with
1754 | Glut.KEY_UP ->
1755 let pageno = max 0 (state.birdseyepageno - 1) in
1756 state.birdseyepageno <- pageno;
1757 if not (pagevisible state.layout pageno)
1758 then gotopage pageno 0.0
1759 else Glut.postRedisplay ();
1761 | Glut.KEY_DOWN ->
1762 let pageno = min (state.pagecount - 1) (state.birdseyepageno + 1) in
1763 state.birdseyepageno <- pageno;
1764 if not (pagevisible state.layout pageno)
1765 then
1766 begin match List.rev state.layout with
1767 | [] -> gotopage pageno 0.0
1768 | l :: _ ->
1769 gotoy (state.y + conf.interpagespace + l.pageh*2 - l.pagevh)
1771 else Glut.postRedisplay ();
1773 | Glut.KEY_PAGE_UP ->
1774 begin match state.layout with
1775 | l :: _ ->
1776 if l.pageno = state.birdseyepageno
1777 then (
1778 match layout (state.y - conf.winh) conf.winh with
1779 | [] -> gotoy (clamp (-conf.winh))
1780 | l :: _ ->
1781 state.birdseyepageno <- max 0 (l.pageno - 1);
1782 gotopage state.birdseyepageno 0.0
1784 else (
1785 state.birdseyepageno <- max 0 (l.pageno - 1);
1786 gotopage state.birdseyepageno 0.0
1788 | [] -> gotoy (clamp (-conf.winh))
1789 end;
1790 | Glut.KEY_PAGE_DOWN ->
1791 begin match List.rev state.layout with
1792 | l :: _ ->
1793 state.birdseyepageno <- min (state.pagecount - 1) (l.pageno + 1);
1794 gotoy (clamp (l.pagedispy + l.pageh))
1795 | [] -> gotoy (clamp conf.winh)
1796 end;
1798 | Glut.KEY_HOME ->
1799 state.birdseyepageno <- 0;
1800 gotopage 0 0.0
1801 | Glut.KEY_END ->
1802 state.birdseyepageno <- state.pagecount - 1;
1803 if not (pagevisible state.layout state.birdseyepageno)
1804 then
1805 gotopage state.birdseyepageno 0.0
1806 else
1807 Glut.postRedisplay ()
1809 | _ -> ()
1812 | None ->
1813 begin match state.textentry with
1814 | None ->
1815 let y =
1816 match key with
1817 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1818 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1819 | Glut.KEY_DOWN -> clamp conf.scrollincr
1820 | Glut.KEY_PAGE_UP ->
1821 if Glut.getModifiers () land Glut.active_ctrl != 0
1822 then
1823 match state.layout with
1824 | [] -> state.y
1825 | l :: _ -> state.y - l.pagey
1826 else
1827 clamp (-conf.winh)
1828 | Glut.KEY_PAGE_DOWN ->
1829 if Glut.getModifiers () land Glut.active_ctrl != 0
1830 then
1831 match List.rev state.layout with
1832 | [] -> state.y
1833 | l :: _ -> getpagey l.pageno
1834 else
1835 clamp conf.winh
1836 | Glut.KEY_HOME -> addnav (); 0
1837 | Glut.KEY_END ->
1838 addnav ();
1839 state.maxy - (if conf.maxhfit then conf.winh else 0)
1841 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1842 state.x <- state.x - 10;
1843 state.y
1844 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1845 state.x <- state.x + 10;
1846 state.y
1848 | _ -> state.y
1850 gotoy_and_clear_text y
1852 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1853 let s =
1854 match key with
1855 | Glut.KEY_UP -> action HCprev
1856 | Glut.KEY_DOWN -> action HCnext
1857 | Glut.KEY_HOME -> action HCfirst
1858 | Glut.KEY_END -> action HClast
1859 | _ -> state.text
1861 state.textentry <- Some (c, s, onhist, onkey, ondone);
1862 Glut.postRedisplay ()
1864 | _ -> ()
1867 | Some (allowdel, active, first, outlines, qsearch) ->
1868 let maxrows = maxoutlinerows () in
1869 let calcfirst first active =
1870 if active > first
1871 then
1872 let rows = active - first in
1873 if rows > maxrows then active - maxrows else first
1874 else active
1876 let navigate incr =
1877 let active = active + incr in
1878 let active = max 0 (min active (Array.length outlines - 1)) in
1879 let first = calcfirst first active in
1880 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1881 Glut.postRedisplay ()
1883 let updownlevel incr =
1884 let len = Array.length outlines in
1885 let (_, curlevel, _, _) = outlines.(active) in
1886 let rec flow i =
1887 if i = len then i-1 else if i = -1 then 0 else
1888 let (_, l, _, _) = outlines.(i) in
1889 if l != curlevel then i else flow (i+incr)
1891 let active = flow active in
1892 let first = calcfirst first active in
1893 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1894 Glut.postRedisplay ()
1896 match key with
1897 | Glut.KEY_UP -> navigate ~-1
1898 | Glut.KEY_DOWN -> navigate 1
1899 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1900 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1902 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1903 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1905 | Glut.KEY_HOME ->
1906 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1907 Glut.postRedisplay ()
1909 | Glut.KEY_END ->
1910 let active = Array.length outlines - 1 in
1911 let first = max 0 (active - maxrows) in
1912 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1913 Glut.postRedisplay ()
1915 | _ -> ()
1918 let drawplaceholder l =
1919 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1920 GlDraw.rect
1921 (float l.pagex, float l.pagedispy)
1922 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1924 let x = float (if margin < 0 then -margin else 0)
1925 and y = float (l.pagedispy + 13) in
1926 let font = Glut.BITMAP_8_BY_13 in
1927 GlDraw.color (0.0, 0.0, 0.0);
1928 GlPix.raster_pos ~x ~y ();
1929 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1930 ("Loading " ^ string_of_int (l.pageno + 1));
1933 let now () = Unix.gettimeofday ();;
1935 let drawpage l =
1936 if state.textentry = None
1937 then (
1938 match state.birdseye with
1939 | None -> GlDraw.color (scalecolor 1.0);
1940 | Some (_, _, hooverpageno) ->
1941 let color =
1942 if l.pageno = state.birdseyepageno
1943 then 1.0
1944 else (
1945 if l.pageno = hooverpageno
1946 then 0.9
1947 else 0.8
1950 GlDraw.color (scalecolor color);
1952 else GlDraw.color (scalecolor 0.4);
1953 begin match getopaque l.pageno with
1954 | Some (opaque, _) when validopaque opaque ->
1955 let a = now () in
1956 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1957 opaque;
1958 let b = now () in
1959 let d = b-.a in
1960 vlog "draw %d %f sec" l.pageno d;
1962 | _ ->
1963 drawplaceholder l;
1964 end;
1967 let scrollph y =
1968 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1969 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1970 let sh = float conf.winh /. sh in
1971 let sh = max sh (float conf.scrollh) in
1973 let percent =
1974 if state.y = state.maxy
1975 then 1.0
1976 else float y /. float maxy
1978 let position = (float conf.winh -. sh) *. percent in
1980 let position =
1981 if position +. sh > float conf.winh
1982 then float conf.winh -. sh
1983 else position
1985 position, sh;
1988 let scrollindicator () =
1989 GlDraw.color (0.64 , 0.64, 0.64);
1990 GlDraw.rect
1991 (float (conf.winw - conf.scrollw), 0.)
1992 (float conf.winw, float conf.winh)
1994 GlDraw.color (0.0, 0.0, 0.0);
1996 let position, sh = scrollph state.y in
1997 GlDraw.rect
1998 (float (conf.winw - conf.scrollw), position)
1999 (float conf.winw, position +. sh)
2003 let showsel margin =
2004 match state.mstate with
2005 | Mnone | Mscroll _ | Mpan _ ->
2008 | Msel ((x0, y0), (x1, y1)) ->
2009 let rec loop = function
2010 | l :: ls ->
2011 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2012 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2013 then
2014 match getopaque l.pageno with
2015 | Some (opaque, _) when validopaque opaque ->
2016 let oy = -l.pagey + l.pagedispy in
2017 seltext opaque
2018 (x0 - margin - state.x, y0,
2019 x1 - margin - state.x, y1) oy;
2021 | _ -> ()
2022 else loop ls
2023 | [] -> ()
2025 loop state.layout
2028 let showrects () =
2029 let panx = float state.x in
2030 Gl.enable `blend;
2031 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2032 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2033 List.iter
2034 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2035 List.iter (fun l ->
2036 if l.pageno = pageno
2037 then (
2038 let d = float (l.pagedispy - l.pagey) in
2039 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2040 GlDraw.begins `quads;
2042 GlDraw.vertex2 (x0+.panx, y0+.d);
2043 GlDraw.vertex2 (x1+.panx, y1+.d);
2044 GlDraw.vertex2 (x2+.panx, y2+.d);
2045 GlDraw.vertex2 (x3+.panx, y3+.d);
2047 GlDraw.ends ();
2049 ) state.layout
2050 ) state.rects
2052 Gl.disable `blend;
2055 let showoutline = function
2056 | None -> ()
2057 | Some (allowdel, active, first, outlines, qsearch) ->
2058 Gl.enable `blend;
2059 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2060 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2061 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2062 Gl.disable `blend;
2064 GlDraw.color (1., 1., 1.);
2065 let font = Glut.BITMAP_9_BY_15 in
2066 let draw_string x y s =
2067 GlPix.raster_pos ~x ~y ();
2068 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2070 let rec loop row =
2071 if row = Array.length outlines || (row - first) * 16 > conf.winh
2072 then ()
2073 else (
2074 let (s, l, _, _) = outlines.(row) in
2075 let y = (row - first) * 16 in
2076 let x = 5 + 15*l in
2077 if row = active
2078 then (
2079 Gl.enable `blend;
2080 GlDraw.polygon_mode `both `line;
2081 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2082 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2083 GlDraw.rect (0., float (y + 1))
2084 (float (conf.winw - 1), float (y + 18));
2085 GlDraw.polygon_mode `both `fill;
2086 Gl.disable `blend;
2087 GlDraw.color (1., 1., 1.);
2089 draw_string (float x) (float (y + 16)) s;
2090 loop (row+1)
2093 loop first
2096 let display () =
2097 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2098 GlDraw.viewport margin 0 state.w conf.winh;
2099 pagematrix ();
2100 GlClear.color (scalecolor 0.5);
2101 GlClear.clear [`color];
2102 if state.x != 0
2103 then (
2104 let x = float state.x in
2105 GlMat.translate ~x ();
2107 if conf.zoom > 1.0
2108 then (
2109 Gl.enable `scissor_test;
2110 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2112 List.iter drawpage state.layout;
2113 if conf.zoom > 1.0
2114 then
2115 Gl.disable `scissor_test
2117 if state.x != 0
2118 then (
2119 let x = -.float state.x in
2120 GlMat.translate ~x ();
2122 showrects ();
2123 showsel margin;
2124 GlDraw.viewport 0 0 conf.winw conf.winh;
2125 winmatrix ();
2126 scrollindicator ();
2127 showoutline state.outline;
2128 enttext ();
2129 Glut.swapBuffers ();
2132 let getunder x y =
2133 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2134 let x = x - margin - state.x in
2135 let rec f = function
2136 | l :: rest ->
2137 begin match getopaque l.pageno with
2138 | Some (opaque, _) when validopaque opaque ->
2139 let y = y - l.pagedispy in
2140 if y > 0
2141 then
2142 let y = l.pagey + y in
2143 let x = x - l.pagex in
2144 match whatsunder opaque x y with
2145 | Unone -> f rest
2146 | under -> under
2147 else
2148 f rest
2149 | _ ->
2150 f rest
2152 | [] -> Unone
2154 f state.layout
2157 let mouse ~button ~bstate ~x ~y =
2158 match button with
2159 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2160 let incr =
2161 if n = 3
2162 then
2163 -conf.scrollincr
2164 else
2165 conf.scrollincr
2167 let incr = incr * 2 in
2168 let y = clamp incr in
2169 gotoy_and_clear_text y
2171 | Glut.LEFT_BUTTON when state.outline = None
2172 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2173 if bstate = Glut.DOWN
2174 then (
2175 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2176 state.mstate <- Mpan (x, y)
2178 else
2179 state.mstate <- Mnone
2181 | Glut.LEFT_BUTTON
2182 when state.outline = None && x > conf.winw - conf.scrollw ->
2183 if bstate = Glut.DOWN
2184 then
2185 let position, sh = scrollph state.y in
2186 if y > truncate position && y < truncate (position +. sh)
2187 then
2188 state.mstate <- Mscroll
2189 else
2190 let percent = float y /. float conf.winh in
2191 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2192 gotoy desty;
2193 state.mstate <- Mscroll
2194 else
2195 state.mstate <- Mnone
2197 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2198 begin match state.birdseye with
2199 | Some vals ->
2200 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2201 let rec loop = function
2202 | [] -> ()
2203 | l :: rest ->
2204 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2205 && x > margin && x < margin + l.pagew
2206 then (
2207 state.birdseyepageno <- l.pageno;
2208 birdseyeoff vals;
2209 reshape conf.winw conf.winh;
2211 else loop rest
2213 loop state.layout;
2214 | None -> () (* impossible *)
2217 | Glut.LEFT_BUTTON when state.outline = None ->
2218 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2219 begin match dest with
2220 | Ulinkgoto (pageno, top) ->
2221 if pageno >= 0
2222 then
2223 gotopage1 pageno top
2225 | Ulinkuri s ->
2226 print_endline s
2228 | Unone when bstate = Glut.DOWN ->
2229 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2230 state.mstate <- Mpan (x, y);
2232 | Unone | Utext _ ->
2233 if bstate = Glut.DOWN
2234 then (
2235 if conf.angle mod 360 = 0
2236 then (
2237 state.mstate <- Msel ((x, y), (x, y));
2238 Glut.postRedisplay ()
2241 else (
2242 match state.mstate with
2243 | Mnone -> ()
2245 | Mscroll ->
2246 state.mstate <- Mnone
2248 | Mpan _ ->
2249 Glut.setCursor Glut.CURSOR_INHERIT;
2250 state.mstate <- Mnone
2252 | Msel ((x0, y0), (x1, y1)) ->
2253 let f l =
2254 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2255 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2256 then
2257 match getopaque l.pageno with
2258 | Some (opaque, _) when validopaque opaque ->
2259 copysel opaque
2260 | _ -> ()
2262 List.iter f state.layout;
2263 copysel ""; (* ugly *)
2264 Glut.setCursor Glut.CURSOR_INHERIT;
2265 state.mstate <- Mnone;
2269 | _ ->
2272 let mouse ~button ~state ~x ~y = mouse button state x y;;
2274 let motion ~x ~y =
2275 if state.outline = None
2276 then
2277 match state.mstate with
2278 | Mnone -> ()
2280 | Mpan (x0, y0) ->
2281 let dx = x - x0
2282 and dy = y0 - y in
2283 state.mstate <- Mpan (x, y);
2284 if conf.zoom > 1.0 then state.x <- state.x + dx;
2285 let y = clamp dy in
2286 gotoy_and_clear_text y
2288 | Msel (a, _) ->
2289 state.mstate <- Msel (a, (x, y));
2290 Glut.postRedisplay ()
2292 | Mscroll ->
2293 let y = min conf.winh (max 0 y) in
2294 let percent = float y /. float conf.winh in
2295 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2296 gotoy_and_clear_text y
2299 let pmotion ~x ~y =
2300 match state.birdseye with
2301 | Some (conf, leftx, hooverpageno) ->
2302 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2303 let rec loop = function
2304 | [] ->
2305 if hooverpageno != -1
2306 then (
2307 state.birdseye <- Some (conf, leftx, -1);
2308 Glut.postRedisplay ();
2310 | l :: rest ->
2311 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2312 && x > margin && x < margin + l.pagew
2313 then (
2314 state.birdseye <- Some (conf, leftx, l.pageno);
2315 Glut.postRedisplay ();
2317 else loop rest
2319 loop state.layout
2321 | None ->
2322 if state.outline = None
2323 then
2324 match state.mstate with
2325 | Mnone ->
2326 begin match getunder x y with
2327 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2328 | Ulinkuri uri ->
2329 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2330 Glut.setCursor Glut.CURSOR_INFO
2331 | Ulinkgoto (page, y) ->
2332 if conf.underinfo
2333 then showtext 'p' ("age: " ^ string_of_int page);
2334 Glut.setCursor Glut.CURSOR_INFO
2335 | Utext s ->
2336 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2337 Glut.setCursor Glut.CURSOR_TEXT
2340 | Mpan _ | Msel _ | Mscroll ->
2345 module State =
2346 struct
2347 open Parser
2349 let home =
2351 match Sys.os_type with
2352 | "Win32" -> Sys.getenv "HOMEPATH"
2353 | _ -> Sys.getenv "HOME"
2354 with exn ->
2355 prerr_endline
2356 ("Can not determine home directory location: " ^
2357 Printexc.to_string exn);
2361 let config_of c attrs =
2362 let apply c k v =
2364 match k with
2365 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2366 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2367 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2368 | "preload" -> { c with preload = bool_of_string v }
2369 | "page-bias" -> { c with pagebias = int_of_string v }
2370 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2371 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2372 | "crop-hack" -> { c with crophack = bool_of_string v }
2373 | "throttle" -> { c with showall = bool_of_string v }
2374 | "highlight-links" -> { c with hlinks = bool_of_string v }
2375 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2376 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2377 | "zoom" ->
2378 let zoom = float_of_string v /. 100. in
2379 let zoom = max 0.01 (min 2.2 zoom) in
2380 { c with zoom = zoom }
2381 | "presentation" -> { c with presentation = bool_of_string v }
2382 | "rotation-angle" -> { c with angle = int_of_string v }
2383 | "width" -> { c with winw = max 20 (int_of_string v) }
2384 | "height" -> { c with winh = max 20 (int_of_string v) }
2385 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2386 | "proportional-display" -> { c with proportional = bool_of_string v }
2387 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2388 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2389 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2390 | _ -> c
2391 with exn ->
2392 prerr_endline ("Error processing attribute (`" ^
2393 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2396 let rec fold c = function
2397 | [] -> c
2398 | (k, v) :: rest ->
2399 let c = apply c k v in
2400 fold c rest
2402 fold c attrs;
2405 let bookmark_of attrs =
2406 let rec fold title page rely = function
2407 | ("title", v) :: rest -> fold v page rely rest
2408 | ("page", v) :: rest -> fold title v rely rest
2409 | ("rely", v) :: rest -> fold title page v rest
2410 | _ :: rest -> fold title page rely rest
2411 | [] -> title, page, rely
2413 fold "invalid" "0" "0" attrs
2416 let setconf dst src =
2417 dst.scrollw <- src.scrollw;
2418 dst.scrollh <- src.scrollh;
2419 dst.icase <- src.icase;
2420 dst.preload <- src.preload;
2421 dst.pagebias <- src.pagebias;
2422 dst.verbose <- src.verbose;
2423 dst.scrollincr <- src.scrollincr;
2424 dst.maxhfit <- src.maxhfit;
2425 dst.crophack <- src.crophack;
2426 dst.autoscroll <- src.autoscroll;
2427 dst.showall <- src.showall;
2428 dst.hlinks <- src.hlinks;
2429 dst.underinfo <- src.underinfo;
2430 dst.interpagespace <- src.interpagespace;
2431 dst.zoom <- src.zoom;
2432 dst.presentation <- src.presentation;
2433 dst.angle <- src.angle;
2434 dst.winw <- src.winw;
2435 dst.winh <- src.winh;
2436 dst.savebmarks <- src.savebmarks;
2437 dst.memlimit <- src.memlimit;
2438 dst.proportional <- src.proportional;
2439 dst.texcount <- src.texcount;
2440 dst.sliceheight <- src.sliceheight;
2443 let unent s =
2444 let l = String.length s in
2445 let b = Buffer.create l in
2446 unent b s 0 l;
2447 Buffer.contents b;
2450 let get s =
2451 let h = Hashtbl.create 10 in
2452 let dc = { defconf with angle = defconf.angle } in
2453 let rec toplevel v t spos epos =
2454 match t with
2455 | Vdata | Vcdata | Vend -> v
2456 | Vopen ("llppconfig", attrs, closed) ->
2457 if closed
2458 then v
2459 else { v with f = llppconfig }
2460 | Vopen _ ->
2461 error "unexpected subelement at top level" s spos
2462 | Vclose tag -> error "unexpected close at top level" s spos
2464 and llppconfig v t spos epos =
2465 match t with
2466 | Vdata | Vcdata | Vend -> v
2467 | Vopen ("defaults", attrs, closed) ->
2468 let c = config_of dc attrs in
2469 setconf dc c;
2470 if closed
2471 then v
2472 else { v with f = skip "defaults" (fun () -> v) }
2474 | Vopen ("doc", attrs, closed) ->
2475 let pathent =
2477 List.assoc "path" attrs
2478 with Not_found -> error "doc is missing path attribute" s spos
2480 let path = unent pathent in
2481 let c = config_of dc attrs in
2482 let y =
2484 float_of_string (List.assoc "rely" attrs)
2485 with
2486 | Not_found -> 0.0
2487 | exn ->
2488 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2491 let x =
2493 int_of_string (List.assoc "pan" attrs)
2494 with
2495 | Not_found -> 0
2496 | exn ->
2497 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2500 if closed
2501 then (Hashtbl.add h path (c, [], x, y); v)
2502 else { v with f = doc path x y c [] }
2504 | Vopen (tag, _, closed) ->
2505 error "unexpected subelement in llppconfig" s spos
2507 | Vclose "llppconfig" -> { v with f = toplevel }
2508 | Vclose tag -> error "unexpected close in llppconfig" s spos
2510 and doc path x y c bookmarks v t spos epos =
2511 match t with
2512 | Vdata | Vcdata -> v
2513 | Vend -> error "unexpected end of input in doc" s spos
2514 | Vopen ("bookmarks", attrs, closed) ->
2515 { v with f = pbookmarks path x y c bookmarks }
2517 | Vopen (tag, _, _) ->
2518 error "unexpected subelement in doc" s spos
2520 | Vclose "doc" ->
2521 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2522 { v with f = llppconfig }
2524 | Vclose tag -> error "unexpected close in doc" s spos
2526 and pbookmarks path x y c bookmarks v t spos epos =
2527 match t with
2528 | Vdata | Vcdata -> v
2529 | Vend -> error "unexpected end of input in bookmarks" s spos
2530 | Vopen ("item", attrs, closed) ->
2531 let titleent, spage, srely = bookmark_of attrs in
2532 let page =
2534 int_of_string spage
2535 with exn ->
2536 dolog "Failed to convert page %S to integer: %s"
2537 spage (Printexc.to_string exn);
2540 let rely =
2542 float_of_string srely
2543 with exn ->
2544 dolog "Failed to convert rely %S to real: %s"
2545 srely (Printexc.to_string exn);
2548 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2549 if closed
2550 then { v with f = pbookmarks path x y c bookmarks }
2551 else
2552 let f () = v in
2553 { v with f = skip "item" f }
2555 | Vopen _ ->
2556 error "unexpected subelement in bookmarks" s spos
2558 | Vclose "bookmarks" ->
2559 { v with f = doc path x y c bookmarks }
2561 | Vclose tag -> error "unexpected close in bookmarks" s spos
2563 and skip tag f v t spos epos =
2564 match t with
2565 | Vdata | Vcdata -> v
2566 | Vend ->
2567 error ("unexpected end of input in skipped " ^ tag) s spos
2568 | Vopen (tag', _, closed) ->
2569 if closed
2570 then v
2571 else
2572 let f' () = { v with f = skip tag f } in
2573 { v with f = skip tag' f' }
2574 | Vclose ctag ->
2575 if tag = ctag
2576 then f ()
2577 else error ("unexpected close in skipped " ^ tag) s spos
2580 parse { f = toplevel; accu = () } s;
2581 h, dc;
2584 let do_load f ic =
2586 let len = in_channel_length ic in
2587 let s = String.create len in
2588 really_input ic s 0 len;
2589 f s;
2590 with
2591 | Parse_error (msg, s, pos) ->
2592 let subs = subs s pos in
2593 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2594 failwith ("parse error: " ^ s)
2596 | exn ->
2597 failwith ("config load error: " ^ Printexc.to_string exn)
2600 let path =
2601 let dir =
2603 let dir = Filename.concat home ".config" in
2604 if Sys.is_directory dir then dir else home
2605 with _ -> home
2607 Filename.concat dir "llpp.conf"
2610 let load1 f =
2611 if Sys.file_exists path
2612 then
2613 match
2614 (try Some (open_in_bin path)
2615 with exn ->
2616 prerr_endline
2617 ("Error opening configuation file `" ^ path ^ "': " ^
2618 Printexc.to_string exn);
2619 None
2621 with
2622 | Some ic ->
2623 begin try
2624 f (do_load get ic)
2625 with exn ->
2626 prerr_endline
2627 ("Error loading configuation from `" ^ path ^ "': " ^
2628 Printexc.to_string exn);
2629 end;
2630 close_in ic;
2632 | None -> ()
2633 else
2634 f (Hashtbl.create 0, defconf)
2637 let load () =
2638 let f (h, dc) =
2639 let pc, pb, px, py =
2641 Hashtbl.find h state.path
2642 with Not_found -> dc, [], 0, 0.0
2644 setconf defconf dc;
2645 setconf conf pc;
2646 state.bookmarks <- pb;
2647 state.x <- px;
2648 cbput state.hists.nav py;
2650 load1 f
2653 let add_attrs bb always dc c =
2654 let ob s a b =
2655 if always || a != b
2656 then Printf.bprintf bb "\n %s='%b'" s a
2657 and oi s a b =
2658 if always || a != b
2659 then Printf.bprintf bb "\n %s='%d'" s a
2660 and oz s a b =
2661 if always || a <> b
2662 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2664 let w, h =
2665 if always
2666 then dc.winw, dc.winh
2667 else
2668 match state.fullscreen with
2669 | Some wh -> wh
2670 | None -> c.winw, c.winh
2672 let zoom, presentation, interpagespace, showall=
2673 if always
2674 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2675 else
2676 match state.birdseye with
2677 | Some (bc, _, _) ->
2678 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2679 | None -> c.zoom, c.presentation, c.interpagespace, c.showall
2681 oi "width" w dc.winw;
2682 oi "height" h dc.winh;
2683 oi "scroll-bar-width" c.scrollw dc.scrollw;
2684 oi "scroll-handle-height" c.scrollh dc.scrollh;
2685 ob "case-insensitive-search" c.icase dc.icase;
2686 ob "preload" c.preload dc.preload;
2687 oi "page-bias" c.pagebias dc.pagebias;
2688 oi "scroll-step" c.scrollincr dc.scrollincr;
2689 ob "max-height-fit" c.maxhfit dc.maxhfit;
2690 ob "crop-hack" c.crophack dc.crophack;
2691 ob "throttle" showall dc.showall;
2692 ob "highlight-links" c.hlinks dc.hlinks;
2693 ob "under-cursor-info" c.underinfo dc.underinfo;
2694 oi "vertical-margin" interpagespace dc.interpagespace;
2695 oz "zoom" zoom dc.zoom;
2696 ob "presentation" presentation dc.presentation;
2697 oi "rotation-angle" c.angle dc.angle;
2698 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2699 ob "proportional-display" c.proportional dc.proportional;
2700 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2701 oi "texcount" c.texcount dc.texcount;
2702 oi "slice-height" c.sliceheight dc.sliceheight;
2705 let save () =
2706 let bb = Buffer.create 32768 in
2707 let f (h, dc) =
2708 Buffer.add_string bb "<llppconfig>\n<defaults ";
2709 add_attrs bb true dc dc;
2710 Buffer.add_string bb "/>\n";
2712 let adddoc path x y c bookmarks =
2713 if bookmarks == [] && c = dc && y = 0.0
2714 then ()
2715 else (
2716 Printf.bprintf bb "<doc path='%s'"
2717 (enent path 0 (String.length path));
2719 if y <> 0.0
2720 then Printf.bprintf bb " rely='%f'" y;
2722 if x != 0
2723 then Printf.bprintf bb " pan='%d'" x;
2725 add_attrs bb false dc c;
2727 begin match bookmarks with
2728 | [] -> Buffer.add_string bb "/>\n"
2729 | _ ->
2730 Buffer.add_string bb ">\n<bookmarks>\n";
2731 List.iter (fun (title, _level, page, rely) ->
2732 Printf.bprintf bb
2733 "<item title='%s' page='%d' rely='%f'/>\n"
2734 (enent title 0 (String.length title))
2735 page
2736 rely
2737 ) bookmarks;
2738 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2739 end;
2743 let x =
2744 match state.birdseye with
2745 | Some (_, x, _) -> x
2746 | None -> state.x
2748 adddoc state.path x (yratio state.y) conf
2749 (if conf.savebmarks then state.bookmarks else []);
2751 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2752 if path <> state.path
2753 then
2754 adddoc path x y c bookmarks
2755 ) h;
2756 Buffer.add_string bb "</llppconfig>";
2758 load1 f;
2759 if Buffer.length bb > 0
2760 then
2762 let tmp = path ^ ".tmp" in
2763 let oc = open_out_bin tmp in
2764 Buffer.output_buffer oc bb;
2765 close_out oc;
2766 Sys.rename tmp path;
2767 with exn ->
2768 prerr_endline
2769 ("error while saving configuration: " ^ Printexc.to_string exn)
2771 end;;
2773 let () =
2774 Arg.parse
2775 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2776 (fun s -> state.path <- s)
2777 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2779 let path =
2780 if String.length state.path = 0
2781 then (prerr_endline "filename missing"; exit 1)
2782 else (
2783 if Filename.is_relative state.path
2784 then Filename.concat (Sys.getcwd ()) state.path
2785 else state.path
2788 state.path <- path;
2790 State.load ();
2792 let _ = Glut.init Sys.argv in
2793 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2794 let () = Glut.initWindowSize conf.winw conf.winh in
2795 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2797 let csock, ssock =
2798 if Sys.os_type = "Unix"
2799 then
2800 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2801 else
2802 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2803 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2804 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2805 Unix.bind sock addr;
2806 Unix.listen sock 1;
2807 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2808 Unix.connect csock addr;
2809 let ssock, _ = Unix.accept sock in
2810 Unix.close sock;
2811 let opts sock =
2812 Unix.setsockopt sock Unix.TCP_NODELAY true;
2813 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2815 opts ssock;
2816 opts csock;
2817 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2818 ssock, csock
2821 let () = Glut.displayFunc display in
2822 let () = Glut.reshapeFunc reshape in
2823 let () = Glut.keyboardFunc keyboard in
2824 let () = Glut.specialFunc special in
2825 let () = Glut.idleFunc (Some idle) in
2826 let () = Glut.mouseFunc mouse in
2827 let () = Glut.motionFunc motion in
2828 let () = Glut.passiveMotionFunc pmotion in
2830 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2831 state.csock <- csock;
2832 state.ssock <- ssock;
2833 state.text <- "Opening " ^ path;
2834 writeopen state.path state.password;
2836 at_exit State.save;
2838 let rec handlelablglutbug () =
2840 Glut.mainLoop ();
2841 with Glut.BadEnum "key in special_of_int" ->
2842 showtext '!' " LablGlut bug: special key not recognized";
2843 handlelablglutbug ()
2845 handlelablglutbug ();