Tweak End behavior in bird's eye
[llpp.git] / main.ml
blob83f4b07c9f0bd3240922a0cef846eaf6ed76826f
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
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 state =
171 { mutable csock : Unix.file_descr
172 ; mutable ssock : Unix.file_descr
173 ; mutable w : int
174 ; mutable x : int
175 ; mutable y : int
176 ; mutable maxy : int
177 ; mutable layout : layout list
178 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
179 ; mutable pdims : (pageno * width * height * leftx) list
180 ; mutable pagecount : int
181 ; pagecache : string circbuf
182 ; mutable rendering : bool
183 ; mutable mstate : mstate
184 ; mutable searchpattern : string
185 ; mutable rects : (pageno * recttype * rect) list
186 ; mutable rects1 : (pageno * recttype * rect) list
187 ; mutable text : string
188 ; mutable fullscreen : (width * height) option
189 ; mutable birdseye : (conf * leftx * pageno * pageno) option
190 ; mutable textentry : textentry option
191 ; mutable outlines : outlines
192 ; mutable outline : (bool * int * int * outline array * string) option
193 ; mutable bookmarks : outline list
194 ; mutable path : string
195 ; mutable password : string
196 ; mutable invalidated : int
197 ; mutable colorscale : float
198 ; mutable memused : int
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
235 ; thumbw = 76
239 let conf = { defconf with angle = defconf.angle };;
241 let state =
242 { csock = Unix.stdin
243 ; ssock = Unix.stdin
244 ; w = 0
245 ; y = 0
246 ; x = 0
247 ; layout = []
248 ; maxy = max_int
249 ; pagemap = Hashtbl.create 10
250 ; pagecache = cbnew 100 ""
251 ; pdims = []
252 ; pagecount = 0
253 ; rendering = false
254 ; mstate = Mnone
255 ; rects = []
256 ; rects1 = []
257 ; text = ""
258 ; fullscreen = None
259 ; birdseye = None
260 ; textentry = None
261 ; searchpattern = ""
262 ; outlines = Olist []
263 ; outline = None
264 ; bookmarks = []
265 ; path = ""
266 ; password = ""
267 ; invalidated = 0
268 ; hists =
269 { nav = cbnew 100 0.0
270 ; pat = cbnew 20 ""
271 ; pag = cbnew 10 ""
273 ; colorscale = 1.0
274 ; memused = 0
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 let oktopreload =
549 if conf.preload
550 then
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
560 else false
562 if oktopreload
563 then
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 =
571 match state.layout with
572 | [] -> 0
573 | l :: _ -> getpagey l.pageno
575 let y = if y < conf.winh then 0 else y - conf.winh in
576 let pages = layout y (conf.winh*3) in
577 List.iter render pages;
578 conf.presentation <- presentation;
579 conf.interpagespace <- interpagespace;
580 state.maxy <- maxy;
583 let gotoy y =
584 let y = max 0 y in
585 let y = min state.maxy y in
586 let pages = layout y conf.winh in
587 let ready = loadlayout pages in
588 if conf.showall
589 then (
590 if ready
591 then (
592 state.y <- y;
593 state.layout <- pages;
594 state.throttle <- None;
595 Glut.postRedisplay ();
597 else (
598 state.throttle <- Some pages;
601 else (
602 state.y <- y;
603 state.layout <- pages;
604 state.throttle <- None;
605 Glut.postRedisplay ();
607 begin match state.birdseye with
608 | Some (conf, leftx, pageno, hooverpageno) ->
609 if not (pagevisible pages pageno)
610 then (
611 match state.layout with
612 | [] -> ()
613 | l :: _ ->
614 state.birdseye <- Some (conf, leftx, l.pageno, hooverpageno)
616 | _ -> ()
617 end;
618 preload ();
621 let gotoy_and_clear_text y =
622 gotoy y;
623 if not conf.verbose then state.text <- "";
626 let addnav () =
627 cbput state.hists.nav (yratio state.y);
630 let getnav () =
631 let y = cbgetc state.hists.nav ~-1 in
632 truncate (y *. float state.maxy)
635 let gotopage n top =
636 let y, h = getpageyh n in
637 addnav ();
638 gotoy_and_clear_text (y + (truncate (top *. float h)));
641 let gotopage1 n top =
642 let y = getpagey n in
643 addnav ();
644 gotoy_and_clear_text (y + top);
647 let invalidate () =
648 state.layout <- [];
649 state.pdims <- [];
650 state.rects <- [];
651 state.rects1 <- [];
652 state.invalidated <- state.invalidated + 1;
655 let scalecolor c =
656 let c = c *. state.colorscale in
657 (c, c, c);
660 let represent () =
661 let maxy = calcheight () in
662 let y =
663 match state.birdseye with
664 | None ->
665 begin match state.layout with
666 | [] ->
667 let rely = yratio state.y in
668 truncate (float maxy *. rely)
670 | l :: _ -> getpagey l.pageno
671 end;
673 | Some (conf, leftx, pageno, hooverpageno) ->
674 let rely = yratio state.y in
675 truncate (float maxy *. rely)
677 state.maxy <- maxy;
678 gotoy y;
681 let pagematrix () =
682 GlMat.mode `projection;
683 GlMat.load_identity ();
684 GlMat.rotate ~x:1.0 ~angle:180.0 ();
685 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
686 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
689 let winmatrix () =
690 GlMat.mode `projection;
691 GlMat.load_identity ();
692 GlMat.rotate ~x:1.0 ~angle:180.0 ();
693 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
694 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
697 let reshape ~w ~h =
698 conf.winw <- w;
699 let w = truncate (float w *. conf.zoom) - conf.scrollw in
700 let w = max w 2 in
701 state.w <- w;
702 conf.winh <- h;
703 GlMat.mode `modelview;
704 GlMat.load_identity ();
705 GlClear.color (scalecolor 1.0);
706 GlClear.clear [`color];
708 invalidate ();
709 wcmd "geometry" [`i w; `i h];
712 let showtext c s =
713 GlDraw.color (0.0, 0.0, 0.0);
714 GlDraw.rect
715 (0.0, float (conf.winh - 18))
716 (float (conf.winw - conf.scrollw - 1), float conf.winh)
718 let font = Glut.BITMAP_8_BY_13 in
719 GlDraw.color (1.0, 1.0, 1.0);
720 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
721 Glut.bitmapCharacter ~font ~c:(Char.code c);
722 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
725 let enttext () =
726 let len = String.length state.text in
727 match state.textentry with
728 | None ->
729 if len > 0 then showtext ' ' state.text
731 | Some (c, text, _, _, _) ->
732 let s =
733 if len > 0
734 then
735 text ^ " [" ^ state.text ^ "]"
736 else
737 text
739 showtext c s;
742 let showtext c s =
743 if true
744 then (
745 state.text <- Printf.sprintf "%c%s" c s;
746 Glut.postRedisplay ();
748 else (
749 showtext c s;
750 Glut.swapBuffers ();
754 let act cmd =
755 match cmd.[0] with
756 | 'c' ->
757 state.pdims <- [];
759 | 'D' ->
760 state.rects <- state.rects1;
761 Glut.postRedisplay ()
763 | 'C' ->
764 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
765 state.pagecount <- n;
766 state.invalidated <- state.invalidated - 1;
767 if state.invalidated = 0
768 then represent ()
770 | 't' ->
771 let s = Scanf.sscanf cmd "t %n"
772 (fun n -> String.sub cmd n (String.length cmd - n))
774 Glut.setWindowTitle s
776 | 'T' ->
777 let s = Scanf.sscanf cmd "T %n"
778 (fun n -> String.sub cmd n (String.length cmd - n))
780 if state.textentry = None
781 then (
782 state.text <- s;
783 showtext ' ' s;
785 else (
786 state.text <- s;
787 Glut.postRedisplay ();
790 | 'V' ->
791 if conf.verbose
792 then
793 let s = Scanf.sscanf cmd "V %n"
794 (fun n -> String.sub cmd n (String.length cmd - n))
796 state.text <- s;
797 showtext ' ' s;
799 | 'F' ->
800 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
801 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
802 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
803 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
805 let y = (getpagey pageno) + truncate y0 in
806 addnav ();
807 gotoy y;
808 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
810 | 'R' ->
811 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
812 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
813 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
814 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
816 state.rects1 <-
817 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
819 | 'r' ->
820 let n, w, h, r, l, s, p =
821 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
822 (fun n w h r l s p ->
823 (n-1, w, h, r, l != 0, s, p))
826 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
827 state.memused <- state.memused + s;
829 let layout =
830 match state.throttle with
831 | None -> state.layout
832 | Some layout -> layout
835 let rec gc () =
836 if (state.memused <= conf.memlimit) || cbempty state.pagecache
837 then ()
838 else (
839 let evictedopaque = cbpeek state.pagecache in
840 match findpageforopaque evictedopaque with
841 | None -> failwith "bug in gc"
842 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
843 if state.gen != gen || not (pagevisible layout evictedn)
844 then (
845 wcmd "free" [`s evictedopaque];
846 state.memused <- state.memused - evictedsize;
847 Hashtbl.remove state.pagemap k;
848 cbdecr state.pagecache;
849 gc ();
853 gc ();
855 cbput state.pagecache p;
856 state.rendering <- false;
858 begin match state.throttle with
859 | None ->
860 if pagevisible state.layout n
861 then gotoy state.y
862 else (
863 let allvisible = loadlayout state.layout in
864 if allvisible then preload ();
867 | Some layout ->
868 match layout with
869 | [] -> ()
870 | l :: _ ->
871 let y = getpagey l.pageno + l.pagey in
872 gotoy y
875 | 'l' ->
876 let (n, w, h, x) as pdim =
877 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
879 state.pdims <- pdim :: state.pdims
881 | 'o' ->
882 let (l, n, t, h, pos) =
883 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
885 let s = String.sub cmd pos (String.length cmd - pos) in
886 let s =
887 let l = String.length s in
888 let b = Buffer.create (String.length s) in
889 let rec loop pc2 i =
890 if i = l
891 then ()
892 else
893 let pc2 =
894 match s.[i] with
895 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
896 | '\xc2' -> true
897 | c ->
898 let c = if Char.code c land 0x80 = 0 then c else '?' in
899 Buffer.add_char b c;
900 false
902 loop pc2 (i+1)
904 loop false 0;
905 Buffer.contents b
907 let outline = (s, l, n, float t /. float h) in
908 let outlines =
909 match state.outlines with
910 | Olist outlines -> Olist (outline :: outlines)
911 | Oarray _ -> Olist [outline]
912 | Onarrow _ -> Olist [outline]
914 state.outlines <- outlines
916 | _ ->
917 log "unknown cmd `%S'" cmd
920 let now = Unix.gettimeofday;;
922 let idle () =
923 let rec loop delay =
924 let r, _, _ = Unix.select [state.csock] [] [] delay in
925 begin match r with
926 | [] ->
927 if conf.autoscroll
928 then begin
929 let y = state.y + conf.scrollincr in
930 let y = if y >= state.maxy then 0 else y in
931 gotoy y;
932 state.text <- "";
933 end;
935 | _ ->
936 let cmd = readcmd state.csock in
937 act cmd;
938 loop 0.0
939 end;
940 in loop 0.001
943 let onhist cb =
944 let rc = cb.rc in
945 let action = function
946 | HCprev -> cbget cb ~-1
947 | HCnext -> cbget cb 1
948 | HCfirst -> cbget cb ~-(cb.rc)
949 | HClast -> cbget cb (cb.len - 1 - cb.rc)
950 and cancel () = cb.rc <- rc
951 in (action, cancel)
954 let search pattern forward =
955 if String.length pattern > 0
956 then
957 let pn, py =
958 match state.layout with
959 | [] -> 0, 0
960 | l :: _ ->
961 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
963 let cmd =
964 let b = makecmd "search"
965 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
967 Buffer.add_char b ',';
968 Buffer.add_string b pattern;
969 Buffer.add_char b '\000';
970 Buffer.contents b;
972 writecmd state.csock cmd;
975 let intentry text key =
976 let c = Char.unsafe_chr key in
977 match c with
978 | '0' .. '9' ->
979 let s = "x" in s.[0] <- c;
980 let text = text ^ s in
981 TEcont text
983 | _ ->
984 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
985 TEcont text
988 let addchar s c =
989 let b = Buffer.create (String.length s + 1) in
990 Buffer.add_string b s;
991 Buffer.add_char b c;
992 Buffer.contents b;
995 let textentry text key =
996 let c = Char.unsafe_chr key in
997 match c with
998 | _ when key >= 32 && key < 127 ->
999 let text = addchar text c in
1000 TEcont text
1002 | _ ->
1003 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1004 TEcont text
1007 let reinit angle proportional =
1008 conf.angle <- angle;
1009 conf.proportional <- proportional;
1010 invalidate ();
1011 wcmd "reinit" [`i angle; `b proportional];
1014 let optentry text key =
1015 let btos b = if b then "on" else "off" in
1016 let c = Char.unsafe_chr key in
1017 match c with
1018 | 's' ->
1019 let ondone s =
1020 try conf.scrollincr <- int_of_string s with exc ->
1021 state.text <- Printf.sprintf "bad integer `%s': %s"
1022 s (Printexc.to_string exc)
1024 TEswitch ('#', "", None, intentry, ondone)
1026 | 'R' ->
1027 let ondone s =
1028 match try
1029 Some (int_of_string s)
1030 with exc ->
1031 state.text <- Printf.sprintf "bad integer `%s': %s"
1032 s (Printexc.to_string exc);
1033 None
1034 with
1035 | Some angle -> reinit angle conf.proportional
1036 | None -> ()
1038 TEswitch ('^', "", None, intentry, ondone)
1040 | 'i' ->
1041 conf.icase <- not conf.icase;
1042 TEdone ("case insensitive search " ^ (btos conf.icase))
1044 | 'p' ->
1045 conf.preload <- not conf.preload;
1046 gotoy state.y;
1047 TEdone ("preload " ^ (btos conf.preload))
1049 | 'v' ->
1050 conf.verbose <- not conf.verbose;
1051 TEdone ("verbose " ^ (btos conf.verbose))
1053 | 'h' ->
1054 conf.maxhfit <- not conf.maxhfit;
1055 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1056 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1058 | 'c' ->
1059 conf.crophack <- not conf.crophack;
1060 TEdone ("crophack " ^ btos conf.crophack)
1062 | 'a' ->
1063 conf.showall <- not conf.showall;
1064 TEdone ("showall " ^ btos conf.showall)
1066 | 'f' ->
1067 conf.underinfo <- not conf.underinfo;
1068 TEdone ("underinfo " ^ btos conf.underinfo)
1070 | 'P' ->
1071 conf.savebmarks <- not conf.savebmarks;
1072 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1074 | 'S' ->
1075 let ondone s =
1077 let pageno, py =
1078 match state.layout with
1079 | [] -> 0, 0
1080 | l :: _ ->
1081 l.pageno, l.pagey
1083 conf.interpagespace <- int_of_string s;
1084 state.maxy <- calcheight ();
1085 let y = getpagey pageno in
1086 gotoy (y + py)
1087 with exc ->
1088 state.text <- Printf.sprintf "bad integer `%s': %s"
1089 s (Printexc.to_string exc)
1091 TEswitch ('%', "", None, intentry, ondone)
1093 | 'l' ->
1094 reinit conf.angle (not conf.proportional);
1095 TEdone ("proprortional display " ^ btos conf.proportional)
1097 | _ ->
1098 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1099 TEstop
1102 let maxoutlinerows () = (conf.winh - 31) / 16;;
1104 let enterselector allowdel outlines errmsg msg =
1105 if Array.length outlines = 0
1106 then (
1107 showtext ' ' errmsg;
1109 else (
1110 state.text <- msg;
1111 Glut.setCursor Glut.CURSOR_INHERIT;
1112 let pageno =
1113 match state.layout with
1114 | [] -> -1
1115 | {pageno=pageno} :: rest -> pageno
1117 let active =
1118 let rec loop n =
1119 if n = Array.length outlines
1120 then 0
1121 else
1122 let (_, _, outlinepageno, _) = outlines.(n) in
1123 if outlinepageno >= pageno then n else loop (n+1)
1125 loop 0
1127 state.outline <-
1128 Some (allowdel, active,
1129 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1130 Glut.postRedisplay ();
1134 let enteroutlinemode () =
1135 let outlines, msg =
1136 match state.outlines with
1137 | Oarray a -> a, ""
1138 | Olist l ->
1139 let a = Array.of_list (List.rev l) in
1140 state.outlines <- Oarray a;
1141 a, ""
1142 | Onarrow (pat, a, b) ->
1143 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1145 enterselector false outlines "Document has no outline" msg;
1148 let enterbookmarkmode () =
1149 let bookmarks = Array.of_list state.bookmarks in
1150 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1153 let quickbookmark ?title () =
1154 match state.layout with
1155 | [] -> ()
1156 | l :: _ ->
1157 let title =
1158 match title with
1159 | None ->
1160 let sec = Unix.gettimeofday () in
1161 let tm = Unix.localtime sec in
1162 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1163 (l.pageno+1)
1164 tm.Unix.tm_mday
1165 tm.Unix.tm_mon
1166 (tm.Unix.tm_year + 1900)
1167 tm.Unix.tm_hour
1168 tm.Unix.tm_min
1169 | Some title -> title
1171 state.bookmarks <-
1172 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1175 let doreshape w h =
1176 state.fullscreen <- None;
1177 Glut.reshapeWindow w h;
1180 let writeopen path password =
1181 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1184 let opendoc path password =
1185 invalidate ();
1186 state.path <- path;
1187 state.password <- password;
1188 state.gen <- state.gen + 1;
1190 writeopen path password;
1191 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1192 wcmd "geometry" [`i state.w; `i conf.winh];
1195 let birdseyeon () =
1196 let zoom = float conf.thumbw /. float conf.winw in
1197 let birdseyepageno =
1198 match state.layout with
1199 | [] -> 0
1200 | l :: _ -> l.pageno
1202 state.birdseye <-
1203 Some ({ conf with zoom = conf.zoom }, state.x, birdseyepageno, -1);
1204 conf.zoom <- zoom;
1205 conf.presentation <- false;
1206 conf.interpagespace <- 10;
1207 conf.hlinks <- false;
1208 state.x <- 0;
1209 state.mstate <- Mnone;
1210 conf.showall <- false;
1211 Glut.setCursor Glut.CURSOR_INHERIT;
1212 if conf.verbose
1213 then
1214 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1215 (100.0*.zoom)
1216 else
1217 state.text <- ""
1221 let birdseyeoff (c, leftx, pageno, _) =
1222 state.birdseye <- None;
1223 conf.zoom <- c.zoom;
1224 conf.presentation <- c.presentation;
1225 conf.interpagespace <- c.interpagespace;
1226 conf.showall <- c.showall;
1227 conf.hlinks <- c.hlinks;
1228 state.x <- leftx;
1229 state.maxy <- calcheight ();
1230 state.y <- getpagey pageno;
1231 if conf.verbose
1232 then
1233 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1234 (100.0*.conf.zoom)
1238 let togglebirdseye () =
1239 match state.birdseye with
1240 | None -> birdseyeon ()
1241 | Some vals -> birdseyeoff vals
1244 let viewkeyboard ~key ~x ~y =
1245 let enttext te =
1246 state.textentry <- te;
1247 state.text <- "";
1248 enttext ();
1249 Glut.postRedisplay ()
1251 match state.textentry with
1252 | None ->
1253 let c = Char.chr key in
1254 begin match c with
1255 | '\027' | 'q' ->
1256 exit 0
1258 | '\008' ->
1259 let y = getnav () in
1260 gotoy_and_clear_text y
1262 | '\013' ->
1263 begin match state.birdseye with
1264 | None -> ()
1265 | Some vals ->
1266 birdseyeoff vals;
1267 reshape conf.winw conf.winh;
1268 end;
1270 | 'o' ->
1271 enteroutlinemode ()
1273 | 'u' ->
1274 state.rects <- [];
1275 state.text <- "";
1276 Glut.postRedisplay ()
1278 | '/' | '?' ->
1279 let ondone isforw s =
1280 cbput state.hists.pat s;
1281 state.searchpattern <- s;
1282 search s isforw
1284 enttext (Some (c, "", Some (onhist state.hists.pat),
1285 textentry, ondone (c ='/')))
1287 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1288 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1289 conf.zoom <- min 2.2 (conf.zoom +. incr);
1290 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1291 reshape conf.winw conf.winh
1293 | '+' ->
1294 let ondone s =
1295 let n =
1296 try int_of_string s with exc ->
1297 state.text <- Printf.sprintf "bad integer `%s': %s"
1298 s (Printexc.to_string exc);
1299 max_int
1301 if n != max_int
1302 then (
1303 conf.pagebias <- n;
1304 state.text <- "page bias is now " ^ string_of_int n;
1307 enttext (Some ('+', "", None, intentry, ondone))
1309 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1310 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1311 conf.zoom <- max 0.01 (conf.zoom -. decr);
1312 if conf.zoom <= 1.0 then state.x <- 0;
1313 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1314 reshape conf.winw conf.winh;
1316 | '-' ->
1317 let ondone msg =
1318 state.text <- msg;
1320 enttext (Some ('-', "", None, optentry, ondone))
1322 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1323 state.x <- 0;
1324 conf.zoom <- 1.0;
1325 state.text <- "zoom is 100%";
1326 reshape conf.winw conf.winh
1328 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1329 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1330 if zoom < 1.0
1331 then (
1332 conf.zoom <- zoom;
1333 state.x <- 0;
1334 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1335 reshape conf.winw conf.winh;
1338 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1339 togglebirdseye ();
1340 reshape conf.winw conf.winh;
1342 | '0' .. '9' ->
1343 let ondone s =
1344 let n =
1345 try int_of_string s with exc ->
1346 state.text <- Printf.sprintf "bad integer `%s': %s"
1347 s (Printexc.to_string exc);
1350 if n >= 0
1351 then (
1352 addnav ();
1353 cbput state.hists.pag (string_of_int n);
1354 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1357 let pageentry text key =
1358 match Char.unsafe_chr key with
1359 | 'g' -> TEdone text
1360 | _ -> intentry text key
1362 let text = "x" in text.[0] <- c;
1363 enttext (Some (':', text, Some (onhist state.hists.pag),
1364 pageentry, ondone))
1366 | 'b' ->
1367 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1368 reshape conf.winw conf.winh;
1370 | 'l' ->
1371 conf.hlinks <- not conf.hlinks;
1372 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1373 Glut.postRedisplay ()
1375 | 'a' ->
1376 conf.autoscroll <- not conf.autoscroll
1378 | 'P' ->
1379 conf.presentation <- not conf.presentation;
1380 showtext ' ' ("presentation mode " ^
1381 if conf.presentation then "on" else "off");
1382 represent ()
1384 | 'f' ->
1385 begin match state.fullscreen with
1386 | None ->
1387 state.fullscreen <- Some (conf.winw, conf.winh);
1388 Glut.fullScreen ()
1389 | Some (w, h) ->
1390 state.fullscreen <- None;
1391 doreshape w h
1394 | 'g' ->
1395 gotoy_and_clear_text 0
1397 | 'n' ->
1398 search state.searchpattern true
1400 | 'p' | 'N' ->
1401 search state.searchpattern false
1403 | 't' ->
1404 begin match state.layout with
1405 | [] -> ()
1406 | l :: _ ->
1407 gotoy_and_clear_text (getpagey l.pageno)
1410 | ' ' ->
1411 begin match List.rev state.layout with
1412 | [] -> ()
1413 | l :: _ ->
1414 let pageno = min (l.pageno+1) (state.pagecount-1) in
1415 gotoy_and_clear_text (getpagey pageno)
1418 | '\127' ->
1419 begin match state.layout with
1420 | [] -> ()
1421 | l :: _ ->
1422 let pageno = max 0 (l.pageno-1) in
1423 gotoy_and_clear_text (getpagey pageno)
1426 | '=' ->
1427 let f (fn, ln) l =
1428 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1430 let fn, ln = List.fold_left f (-1, -1) state.layout in
1431 let s =
1432 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1433 let percent =
1434 if maxy <= 0
1435 then 100.
1436 else (100. *. (float state.y /. float maxy)) in
1437 if fn = ln
1438 then
1439 Printf.sprintf "Page %d of %d %.2f%%"
1440 (fn+1) state.pagecount percent
1441 else
1442 Printf.sprintf
1443 "Pages %d-%d of %d %.2f%%"
1444 (fn+1) (ln+1) state.pagecount percent
1446 showtext ' ' s;
1448 | 'w' ->
1449 begin match state.layout with
1450 | [] -> ()
1451 | l :: _ ->
1452 doreshape (l.pagew + conf.scrollw) l.pageh;
1453 Glut.postRedisplay ();
1456 | '\'' ->
1457 enterbookmarkmode ()
1459 | 'm' ->
1460 let ondone s =
1461 match state.layout with
1462 | l :: _ ->
1463 state.bookmarks <-
1464 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1465 :: state.bookmarks
1466 | _ -> ()
1468 enttext (Some ('~', "", None, textentry, ondone))
1470 | '~' ->
1471 quickbookmark ();
1472 showtext ' ' "Quick bookmark added";
1474 | 'z' ->
1475 begin match state.layout with
1476 | l :: _ ->
1477 let rect = getpdimrect l.pagedimno in
1478 let w, h =
1479 if conf.crophack
1480 then
1481 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1482 truncate (1.2 *. (rect.(3) -. rect.(0))))
1483 else
1484 (truncate (rect.(1) -. rect.(0)),
1485 truncate (rect.(3) -. rect.(0)))
1487 if w != 0 && h != 0
1488 then
1489 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1491 Glut.postRedisplay ();
1493 | [] -> ()
1496 | '<' | '>' ->
1497 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1499 | '[' | ']' ->
1500 state.colorscale <-
1501 max 0.0
1502 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1503 Glut.postRedisplay ()
1505 | 'k' -> gotoy (clamp (-conf.scrollincr))
1506 | 'j' -> gotoy (clamp conf.scrollincr)
1508 | 'r' -> opendoc state.path state.password
1510 | _ ->
1511 vlog "huh? %d %c" key (Char.chr key);
1514 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1515 let len = String.length text in
1516 if len = 0
1517 then (
1518 state.textentry <- None;
1519 Glut.postRedisplay ();
1521 else (
1522 let s = String.sub text 0 (len - 1) in
1523 enttext (Some (c, s, opthist, onkey, ondone))
1526 | Some (c, text, onhist, onkey, ondone) ->
1527 begin match Char.unsafe_chr key with
1528 | '\r' | '\n' ->
1529 ondone text;
1530 state.textentry <- None;
1531 Glut.postRedisplay ()
1533 | '\027' ->
1534 begin match onhist with
1535 | None -> ()
1536 | Some (_, onhistcancel) -> onhistcancel ()
1537 end;
1538 state.textentry <- None;
1539 Glut.postRedisplay ()
1541 | _ ->
1542 begin match onkey text key with
1543 | TEdone text ->
1544 state.textentry <- None;
1545 ondone text;
1546 Glut.postRedisplay ()
1548 | TEcont text ->
1549 enttext (Some (c, text, onhist, onkey, ondone));
1551 | TEstop ->
1552 state.textentry <- None;
1553 Glut.postRedisplay ()
1555 | TEswitch te ->
1556 state.textentry <- Some te;
1557 Glut.postRedisplay ()
1558 end;
1559 end;
1562 let narrow outlines pattern =
1563 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1564 match reopt with
1565 | None -> None
1566 | Some re ->
1567 let rec fold accu n =
1568 if n = -1
1569 then accu
1570 else
1571 let (s, _, _, _) as o = outlines.(n) in
1572 let accu =
1573 if (try ignore (Str.search_forward re s 0); true
1574 with Not_found -> false)
1575 then (o :: accu)
1576 else accu
1578 fold accu (n-1)
1580 let matched = fold [] (Array.length outlines - 1) in
1581 if matched = [] then None else Some (Array.of_list matched)
1584 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1585 let search active pattern incr =
1586 let dosearch re =
1587 let rec loop n =
1588 if n = Array.length outlines || n = -1
1589 then None
1590 else
1591 let (s, _, _, _) = outlines.(n) in
1593 (try ignore (Str.search_forward re s 0); true
1594 with Not_found -> false)
1595 then Some n
1596 else loop (n + incr)
1598 loop active
1601 let re = Str.regexp_case_fold pattern in
1602 dosearch re
1603 with Failure s ->
1604 state.text <- s;
1605 None
1607 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1608 match key with
1609 | 27 ->
1610 if String.length qsearch = 0
1611 then (
1612 state.text <- "";
1613 state.outline <- None;
1614 Glut.postRedisplay ();
1616 else (
1617 state.text <- "";
1618 state.outline <- Some (allowdel, active, first, outlines, "");
1619 Glut.postRedisplay ();
1622 | 18 | 19 ->
1623 let incr = if key = 18 then -1 else 1 in
1624 let active, first =
1625 match search (active + incr) qsearch incr with
1626 | None ->
1627 state.text <- qsearch ^ " [not found]";
1628 active, first
1629 | Some active ->
1630 state.text <- qsearch;
1631 active, firstof active
1633 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1634 Glut.postRedisplay ();
1636 | 8 ->
1637 let len = String.length qsearch in
1638 if len = 0
1639 then ()
1640 else (
1641 if len = 1
1642 then (
1643 state.text <- "";
1644 state.outline <- Some (allowdel, active, first, outlines, "");
1646 else
1647 let qsearch = String.sub qsearch 0 (len - 1) in
1648 let active, first =
1649 match search active qsearch ~-1 with
1650 | None ->
1651 state.text <- qsearch ^ " [not found]";
1652 active, first
1653 | Some active ->
1654 state.text <- qsearch;
1655 active, firstof active
1657 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1659 Glut.postRedisplay ()
1661 | 13 ->
1662 if active < Array.length outlines
1663 then (
1664 let (_, _, n, t) = outlines.(active) in
1665 gotopage n t;
1667 state.text <- "";
1668 if allowdel then state.bookmarks <- Array.to_list outlines;
1669 state.outline <- None;
1670 Glut.postRedisplay ();
1672 | _ when key >= 32 && key < 127 ->
1673 let pattern = addchar qsearch (Char.chr key) in
1674 let active, first =
1675 match search active pattern 1 with
1676 | None ->
1677 state.text <- pattern ^ " [not found]";
1678 active, first
1679 | Some active ->
1680 state.text <- pattern;
1681 active, firstof active
1683 state.outline <- Some (allowdel, active, first, outlines, pattern);
1684 Glut.postRedisplay ()
1686 | 14 when not allowdel -> (* ctrl-n *)
1687 if String.length qsearch > 0
1688 then (
1689 let optoutlines = narrow outlines qsearch in
1690 begin match optoutlines with
1691 | None -> state.text <- "can't narrow"
1692 | Some outlines ->
1693 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1694 match state.outlines with
1695 | Olist l -> ()
1696 | Oarray a ->
1697 state.outlines <- Onarrow (qsearch, outlines, a)
1698 | Onarrow (pat, a, b) ->
1699 state.outlines <- Onarrow (qsearch, outlines, b)
1700 end;
1702 Glut.postRedisplay ()
1704 | 21 when not allowdel -> (* ctrl-u *)
1705 let outline =
1706 match state.outlines with
1707 | Oarray a -> a
1708 | Olist l ->
1709 let a = Array.of_list (List.rev l) in
1710 state.outlines <- Oarray a;
1712 | Onarrow (pat, a, b) ->
1713 state.outlines <- Oarray b;
1714 state.text <- "";
1717 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1718 Glut.postRedisplay ()
1720 | 12 ->
1721 state.outline <-
1722 Some (allowdel, active, firstof active, outlines, qsearch);
1723 Glut.postRedisplay ()
1725 | 127 when allowdel ->
1726 let len = Array.length outlines - 1 in
1727 if len = 0
1728 then (
1729 state.outline <- None;
1730 state.bookmarks <- [];
1732 else (
1733 let bookmarks = Array.init len
1734 (fun i ->
1735 let i = if i >= active then i + 1 else i in
1736 outlines.(i)
1739 state.outline <-
1740 Some (allowdel,
1741 min active (len-1),
1742 min first (len-1),
1743 bookmarks, qsearch)
1746 Glut.postRedisplay ()
1748 | _ -> log "unknown key %d" key
1751 let keyboard ~key ~x ~y =
1752 if key = 7
1753 then
1754 wcmd "interrupt" []
1755 else
1756 match state.outline with
1757 | None -> viewkeyboard ~key ~x ~y
1758 | Some outline -> outlinekeyboard ~key ~x ~y outline
1761 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno) =
1762 match key with
1763 | Glut.KEY_UP ->
1764 let pageno = max 0 (pageno - 1) in
1765 let move =
1766 let rec loop = function
1767 | [] -> true
1768 | l :: rest ->
1769 if l.pageno = pageno
1770 then l.pagey != 0
1771 else loop rest
1773 loop state.layout
1775 if move
1776 then gotopage1 pageno 0
1777 else Glut.postRedisplay ();
1778 state.birdseye <- Some (conf, leftx, pageno, hooverpageno)
1780 | Glut.KEY_DOWN ->
1781 let pageno = min (state.pagecount - 1) (pageno + 1) in
1782 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1783 if not (pagevisible state.layout pageno)
1784 then
1785 begin match List.rev state.layout with
1786 | [] -> gotopage pageno 0.0
1787 | l :: _ ->
1788 gotoy (state.y + conf.interpagespace + l.pageh*2 - l.pagevh)
1790 else Glut.postRedisplay ();
1792 | Glut.KEY_PAGE_UP ->
1793 begin match state.layout with
1794 | l :: _ ->
1795 if l.pageno = pageno
1796 then (
1797 match layout (state.y - conf.winh) conf.winh with
1798 | [] -> gotoy (clamp (-conf.winh))
1799 | l :: _ ->
1800 let pageno = max 0 (l.pageno - 1) in
1801 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1802 gotopage pageno 0.0
1804 else (
1805 let pageno = max 0 (l.pageno - 1) in
1806 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1807 gotopage pageno 0.0
1809 | [] -> gotoy (clamp (-conf.winh))
1810 end;
1811 | Glut.KEY_PAGE_DOWN ->
1812 begin match List.rev state.layout with
1813 | l :: _ ->
1814 let pageno = min l.pageno (state.pagecount - 1) in
1815 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1816 gotoy (clamp (l.pagedispy + l.pageh))
1817 | [] -> gotoy (clamp conf.winh)
1818 end;
1820 | Glut.KEY_HOME ->
1821 state.birdseye <- Some (conf, leftx, 0, hooverpageno);
1822 gotopage 0 0.0
1823 | Glut.KEY_END ->
1824 let pageno = state.pagecount - 1 in
1825 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1826 if not (pagevisible state.layout pageno)
1827 then
1828 let h =
1829 match List.rev state.pdims with
1830 | [] -> conf.winh
1831 | (_, _, h, _) :: _ -> h
1833 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1834 else Glut.postRedisplay ();
1835 | _ -> ()
1838 let special ~key ~x ~y =
1839 match state.outline, state.birdseye with
1840 | None, _ when key = Glut.KEY_F9 ->
1841 togglebirdseye ();
1842 reshape conf.winw conf.winh;
1844 | None, (Some vals) ->
1845 birdseyespecial key x y vals
1847 | None, None ->
1848 begin match state.textentry with
1849 | None ->
1850 let y =
1851 match key with
1852 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1853 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1854 | Glut.KEY_DOWN -> clamp conf.scrollincr
1855 | Glut.KEY_PAGE_UP ->
1856 if Glut.getModifiers () land Glut.active_ctrl != 0
1857 then
1858 match state.layout with
1859 | [] -> state.y
1860 | l :: _ -> state.y - l.pagey
1861 else
1862 clamp (-conf.winh)
1863 | Glut.KEY_PAGE_DOWN ->
1864 if Glut.getModifiers () land Glut.active_ctrl != 0
1865 then
1866 match List.rev state.layout with
1867 | [] -> state.y
1868 | l :: _ -> getpagey l.pageno
1869 else
1870 clamp conf.winh
1871 | Glut.KEY_HOME -> addnav (); 0
1872 | Glut.KEY_END ->
1873 addnav ();
1874 state.maxy - (if conf.maxhfit then conf.winh else 0)
1876 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1877 state.x <- state.x - 10;
1878 state.y
1879 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1880 state.x <- state.x + 10;
1881 state.y
1883 | _ -> state.y
1885 gotoy_and_clear_text y
1887 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1888 let s =
1889 match key with
1890 | Glut.KEY_UP -> action HCprev
1891 | Glut.KEY_DOWN -> action HCnext
1892 | Glut.KEY_HOME -> action HCfirst
1893 | Glut.KEY_END -> action HClast
1894 | _ -> state.text
1896 state.textentry <- Some (c, s, onhist, onkey, ondone);
1897 Glut.postRedisplay ()
1899 | _ -> ()
1902 | Some (allowdel, active, first, outlines, qsearch), _ ->
1903 let maxrows = maxoutlinerows () in
1904 let calcfirst first active =
1905 if active > first
1906 then
1907 let rows = active - first in
1908 if rows > maxrows then active - maxrows else first
1909 else active
1911 let navigate incr =
1912 let active = active + incr in
1913 let active = max 0 (min active (Array.length outlines - 1)) in
1914 let first = calcfirst first active in
1915 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1916 Glut.postRedisplay ()
1918 let updownlevel incr =
1919 let len = Array.length outlines in
1920 let (_, curlevel, _, _) = outlines.(active) in
1921 let rec flow i =
1922 if i = len then i-1 else if i = -1 then 0 else
1923 let (_, l, _, _) = outlines.(i) in
1924 if l != curlevel then i else flow (i+incr)
1926 let active = flow active in
1927 let first = calcfirst first active in
1928 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1929 Glut.postRedisplay ()
1931 match key with
1932 | Glut.KEY_UP -> navigate ~-1
1933 | Glut.KEY_DOWN -> navigate 1
1934 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1935 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1937 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1938 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1940 | Glut.KEY_HOME ->
1941 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1942 Glut.postRedisplay ()
1944 | Glut.KEY_END ->
1945 let active = Array.length outlines - 1 in
1946 let first = max 0 (active - maxrows) in
1947 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1948 Glut.postRedisplay ()
1950 | _ -> ()
1953 let drawplaceholder l =
1954 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1955 GlDraw.rect
1956 (float l.pagex, float l.pagedispy)
1957 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1959 let x = float (if margin < 0 then -margin else l.pagex)
1960 and y = float (l.pagedispy + 13) in
1961 let font = Glut.BITMAP_8_BY_13 in
1962 GlDraw.color (0.0, 0.0, 0.0);
1963 GlPix.raster_pos ~x ~y ();
1964 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1965 ("Loading " ^ string_of_int (l.pageno + 1));
1968 let now () = Unix.gettimeofday ();;
1970 let drawpage l =
1971 if state.textentry = None
1972 then (
1973 match state.birdseye with
1974 | None -> GlDraw.color (scalecolor 1.0);
1975 | Some (_, _, pageno, hooverpageno) ->
1976 let color =
1977 if l.pageno = pageno
1978 then 1.0
1979 else (
1980 if l.pageno = hooverpageno
1981 then 0.9
1982 else 0.8
1985 GlDraw.color (scalecolor color);
1987 else GlDraw.color (scalecolor 0.4);
1988 begin match getopaque l.pageno with
1989 | Some (opaque, _) when validopaque opaque ->
1990 let a = now () in
1991 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1992 opaque;
1993 let b = now () in
1994 let d = b-.a in
1995 vlog "draw %d %f sec" l.pageno d;
1997 | _ ->
1998 drawplaceholder l;
1999 end;
2002 let scrollph y =
2003 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2004 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2005 let sh = float conf.winh /. sh in
2006 let sh = max sh (float conf.scrollh) in
2008 let percent =
2009 if state.y = state.maxy
2010 then 1.0
2011 else float y /. float maxy
2013 let position = (float conf.winh -. sh) *. percent in
2015 let position =
2016 if position +. sh > float conf.winh
2017 then float conf.winh -. sh
2018 else position
2020 position, sh;
2023 let scrollindicator () =
2024 GlDraw.color (0.64 , 0.64, 0.64);
2025 GlDraw.rect
2026 (float (conf.winw - conf.scrollw), 0.)
2027 (float conf.winw, float conf.winh)
2029 GlDraw.color (0.0, 0.0, 0.0);
2031 let position, sh = scrollph state.y in
2032 GlDraw.rect
2033 (float (conf.winw - conf.scrollw), position)
2034 (float conf.winw, position +. sh)
2038 let showsel margin =
2039 match state.mstate with
2040 | Mnone | Mscroll _ | Mpan _ ->
2043 | Msel ((x0, y0), (x1, y1)) ->
2044 let rec loop = function
2045 | l :: ls ->
2046 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2047 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2048 then
2049 match getopaque l.pageno with
2050 | Some (opaque, _) when validopaque opaque ->
2051 let oy = -l.pagey + l.pagedispy in
2052 seltext opaque
2053 (x0 - margin - state.x, y0,
2054 x1 - margin - state.x, y1) oy;
2056 | _ -> ()
2057 else loop ls
2058 | [] -> ()
2060 loop state.layout
2063 let showrects () =
2064 let panx = float state.x in
2065 Gl.enable `blend;
2066 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2067 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2068 List.iter
2069 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2070 List.iter (fun l ->
2071 if l.pageno = pageno
2072 then (
2073 let d = float (l.pagedispy - l.pagey) in
2074 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2075 GlDraw.begins `quads;
2077 GlDraw.vertex2 (x0+.panx, y0+.d);
2078 GlDraw.vertex2 (x1+.panx, y1+.d);
2079 GlDraw.vertex2 (x2+.panx, y2+.d);
2080 GlDraw.vertex2 (x3+.panx, y3+.d);
2082 GlDraw.ends ();
2084 ) state.layout
2085 ) state.rects
2087 Gl.disable `blend;
2090 let showoutline = function
2091 | None -> ()
2092 | Some (allowdel, active, first, outlines, qsearch) ->
2093 Gl.enable `blend;
2094 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2095 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2096 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2097 Gl.disable `blend;
2099 GlDraw.color (1., 1., 1.);
2100 let font = Glut.BITMAP_9_BY_15 in
2101 let draw_string x y s =
2102 GlPix.raster_pos ~x ~y ();
2103 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2105 let rec loop row =
2106 if row = Array.length outlines || (row - first) * 16 > conf.winh
2107 then ()
2108 else (
2109 let (s, l, _, _) = outlines.(row) in
2110 let y = (row - first) * 16 in
2111 let x = 5 + 15*l in
2112 if row = active
2113 then (
2114 Gl.enable `blend;
2115 GlDraw.polygon_mode `both `line;
2116 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2117 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2118 GlDraw.rect (0., float (y + 1))
2119 (float (conf.winw - 1), float (y + 18));
2120 GlDraw.polygon_mode `both `fill;
2121 Gl.disable `blend;
2122 GlDraw.color (1., 1., 1.);
2124 draw_string (float x) (float (y + 16)) s;
2125 loop (row+1)
2128 loop first
2131 let display () =
2132 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2133 GlDraw.viewport margin 0 state.w conf.winh;
2134 pagematrix ();
2135 GlClear.color (scalecolor 0.5);
2136 GlClear.clear [`color];
2137 if state.x != 0
2138 then (
2139 let x = float state.x in
2140 GlMat.translate ~x ();
2142 if conf.zoom > 1.0
2143 then (
2144 Gl.enable `scissor_test;
2145 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2147 List.iter drawpage state.layout;
2148 if conf.zoom > 1.0
2149 then
2150 Gl.disable `scissor_test
2152 if state.x != 0
2153 then (
2154 let x = -.float state.x in
2155 GlMat.translate ~x ();
2157 showrects ();
2158 showsel margin;
2159 GlDraw.viewport 0 0 conf.winw conf.winh;
2160 winmatrix ();
2161 scrollindicator ();
2162 showoutline state.outline;
2163 enttext ();
2164 Glut.swapBuffers ();
2167 let getunder x y =
2168 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2169 let x = x - margin - state.x in
2170 let rec f = function
2171 | l :: rest ->
2172 begin match getopaque l.pageno with
2173 | Some (opaque, _) when validopaque opaque ->
2174 let y = y - l.pagedispy in
2175 if y > 0
2176 then
2177 let y = l.pagey + y in
2178 let x = x - l.pagex in
2179 match whatsunder opaque x y with
2180 | Unone -> f rest
2181 | under -> under
2182 else
2183 f rest
2184 | _ ->
2185 f rest
2187 | [] -> Unone
2189 f state.layout
2192 let mouse ~button ~bstate ~x ~y =
2193 match button with
2194 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2195 let incr =
2196 if n = 3
2197 then
2198 -conf.scrollincr
2199 else
2200 conf.scrollincr
2202 let incr = incr * 2 in
2203 let y = clamp incr in
2204 gotoy_and_clear_text y
2206 | Glut.LEFT_BUTTON when state.outline = None
2207 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2208 if bstate = Glut.DOWN
2209 then (
2210 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2211 state.mstate <- Mpan (x, y)
2213 else
2214 state.mstate <- Mnone
2216 | Glut.LEFT_BUTTON
2217 when state.outline = None && x > conf.winw - conf.scrollw ->
2218 if bstate = Glut.DOWN
2219 then
2220 let position, sh = scrollph state.y in
2221 if y > truncate position && y < truncate (position +. sh)
2222 then
2223 state.mstate <- Mscroll
2224 else
2225 let percent = float y /. float conf.winh in
2226 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2227 gotoy desty;
2228 state.mstate <- Mscroll
2229 else
2230 state.mstate <- Mnone
2232 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2233 begin match state.birdseye with
2234 | Some (conf, leftx, pageno, hooverpageno) ->
2235 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2236 let rec loop = function
2237 | [] -> ()
2238 | l :: rest ->
2239 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2240 && x > margin && x < margin + l.pagew
2241 then (
2242 birdseyeoff (conf, leftx, l.pageno, hooverpageno);
2243 reshape conf.winw conf.winh;
2245 else loop rest
2247 loop state.layout;
2248 | None -> () (* impossible *)
2251 | Glut.LEFT_BUTTON when state.outline = None ->
2252 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2253 begin match dest with
2254 | Ulinkgoto (pageno, top) ->
2255 if pageno >= 0
2256 then
2257 gotopage1 pageno top
2259 | Ulinkuri s ->
2260 print_endline s
2262 | Unone when bstate = Glut.DOWN ->
2263 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2264 state.mstate <- Mpan (x, y);
2266 | Unone | Utext _ ->
2267 if bstate = Glut.DOWN
2268 then (
2269 if conf.angle mod 360 = 0
2270 then (
2271 state.mstate <- Msel ((x, y), (x, y));
2272 Glut.postRedisplay ()
2275 else (
2276 match state.mstate with
2277 | Mnone -> ()
2279 | Mscroll ->
2280 state.mstate <- Mnone
2282 | Mpan _ ->
2283 Glut.setCursor Glut.CURSOR_INHERIT;
2284 state.mstate <- Mnone
2286 | Msel ((x0, y0), (x1, y1)) ->
2287 let f l =
2288 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2289 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2290 then
2291 match getopaque l.pageno with
2292 | Some (opaque, _) when validopaque opaque ->
2293 copysel opaque
2294 | _ -> ()
2296 List.iter f state.layout;
2297 copysel ""; (* ugly *)
2298 Glut.setCursor Glut.CURSOR_INHERIT;
2299 state.mstate <- Mnone;
2303 | _ ->
2306 let mouse ~button ~state ~x ~y = mouse button state x y;;
2308 let motion ~x ~y =
2309 if state.outline = None
2310 then
2311 match state.mstate with
2312 | Mnone -> ()
2314 | Mpan (x0, y0) ->
2315 let dx = x - x0
2316 and dy = y0 - y in
2317 state.mstate <- Mpan (x, y);
2318 if conf.zoom > 1.0 then state.x <- state.x + dx;
2319 let y = clamp dy in
2320 gotoy_and_clear_text y
2322 | Msel (a, _) ->
2323 state.mstate <- Msel (a, (x, y));
2324 Glut.postRedisplay ()
2326 | Mscroll ->
2327 let y = min conf.winh (max 0 y) in
2328 let percent = float y /. float conf.winh in
2329 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2330 gotoy_and_clear_text y
2333 let pmotion ~x ~y =
2334 match state.birdseye with
2335 | Some (conf, leftx, pageno, hooverpageno) ->
2336 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2337 let rec loop = function
2338 | [] ->
2339 if hooverpageno != -1
2340 then (
2341 state.birdseye <- Some (conf, leftx, pageno, -1);
2342 Glut.postRedisplay ();
2344 | l :: rest ->
2345 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2346 && x > margin && x < margin + l.pagew
2347 then (
2348 state.birdseye <- Some (conf, leftx, pageno, l.pageno);
2349 Glut.postRedisplay ();
2351 else loop rest
2353 loop state.layout
2355 | None ->
2356 if state.outline = None
2357 then
2358 match state.mstate with
2359 | Mnone ->
2360 begin match getunder x y with
2361 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2362 | Ulinkuri uri ->
2363 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2364 Glut.setCursor Glut.CURSOR_INFO
2365 | Ulinkgoto (page, y) ->
2366 if conf.underinfo
2367 then showtext 'p' ("age: " ^ string_of_int page);
2368 Glut.setCursor Glut.CURSOR_INFO
2369 | Utext s ->
2370 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2371 Glut.setCursor Glut.CURSOR_TEXT
2374 | Mpan _ | Msel _ | Mscroll ->
2379 module State =
2380 struct
2381 open Parser
2383 let home =
2385 match Sys.os_type with
2386 | "Win32" -> Sys.getenv "HOMEPATH"
2387 | _ -> Sys.getenv "HOME"
2388 with exn ->
2389 prerr_endline
2390 ("Can not determine home directory location: " ^
2391 Printexc.to_string exn);
2395 let config_of c attrs =
2396 let apply c k v =
2398 match k with
2399 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2400 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2401 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2402 | "preload" -> { c with preload = bool_of_string v }
2403 | "page-bias" -> { c with pagebias = int_of_string v }
2404 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2405 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2406 | "crop-hack" -> { c with crophack = bool_of_string v }
2407 | "throttle" -> { c with showall = bool_of_string v }
2408 | "highlight-links" -> { c with hlinks = bool_of_string v }
2409 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2410 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2411 | "zoom" ->
2412 let zoom = float_of_string v /. 100. in
2413 let zoom = max 0.01 (min 2.2 zoom) in
2414 { c with zoom = zoom }
2415 | "presentation" -> { c with presentation = bool_of_string v }
2416 | "rotation-angle" -> { c with angle = int_of_string v }
2417 | "width" -> { c with winw = max 20 (int_of_string v) }
2418 | "height" -> { c with winh = max 20 (int_of_string v) }
2419 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2420 | "proportional-display" -> { c with proportional = bool_of_string v }
2421 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2422 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2423 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2424 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2425 | _ -> c
2426 with exn ->
2427 prerr_endline ("Error processing attribute (`" ^
2428 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2431 let rec fold c = function
2432 | [] -> c
2433 | (k, v) :: rest ->
2434 let c = apply c k v in
2435 fold c rest
2437 fold c attrs;
2440 let bookmark_of attrs =
2441 let rec fold title page rely = function
2442 | ("title", v) :: rest -> fold v page rely rest
2443 | ("page", v) :: rest -> fold title v rely rest
2444 | ("rely", v) :: rest -> fold title page v rest
2445 | _ :: rest -> fold title page rely rest
2446 | [] -> title, page, rely
2448 fold "invalid" "0" "0" attrs
2451 let setconf dst src =
2452 dst.scrollw <- src.scrollw;
2453 dst.scrollh <- src.scrollh;
2454 dst.icase <- src.icase;
2455 dst.preload <- src.preload;
2456 dst.pagebias <- src.pagebias;
2457 dst.verbose <- src.verbose;
2458 dst.scrollincr <- src.scrollincr;
2459 dst.maxhfit <- src.maxhfit;
2460 dst.crophack <- src.crophack;
2461 dst.autoscroll <- src.autoscroll;
2462 dst.showall <- src.showall;
2463 dst.hlinks <- src.hlinks;
2464 dst.underinfo <- src.underinfo;
2465 dst.interpagespace <- src.interpagespace;
2466 dst.zoom <- src.zoom;
2467 dst.presentation <- src.presentation;
2468 dst.angle <- src.angle;
2469 dst.winw <- src.winw;
2470 dst.winh <- src.winh;
2471 dst.savebmarks <- src.savebmarks;
2472 dst.memlimit <- src.memlimit;
2473 dst.proportional <- src.proportional;
2474 dst.texcount <- src.texcount;
2475 dst.sliceheight <- src.sliceheight;
2476 dst.thumbw <- src.thumbw;
2479 let unent s =
2480 let l = String.length s in
2481 let b = Buffer.create l in
2482 unent b s 0 l;
2483 Buffer.contents b;
2486 let get s =
2487 let h = Hashtbl.create 10 in
2488 let dc = { defconf with angle = defconf.angle } in
2489 let rec toplevel v t spos epos =
2490 match t with
2491 | Vdata | Vcdata | Vend -> v
2492 | Vopen ("llppconfig", attrs, closed) ->
2493 if closed
2494 then v
2495 else { v with f = llppconfig }
2496 | Vopen _ ->
2497 error "unexpected subelement at top level" s spos
2498 | Vclose tag -> error "unexpected close at top level" s spos
2500 and llppconfig v t spos epos =
2501 match t with
2502 | Vdata | Vcdata | Vend -> v
2503 | Vopen ("defaults", attrs, closed) ->
2504 let c = config_of dc attrs in
2505 setconf dc c;
2506 if closed
2507 then v
2508 else { v with f = skip "defaults" (fun () -> v) }
2510 | Vopen ("doc", attrs, closed) ->
2511 let pathent =
2513 List.assoc "path" attrs
2514 with Not_found -> error "doc is missing path attribute" s spos
2516 let path = unent pathent in
2517 let c = config_of dc attrs in
2518 let y =
2520 float_of_string (List.assoc "rely" attrs)
2521 with
2522 | Not_found -> 0.0
2523 | exn ->
2524 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2527 let x =
2529 int_of_string (List.assoc "pan" attrs)
2530 with
2531 | Not_found -> 0
2532 | exn ->
2533 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2536 if closed
2537 then (Hashtbl.add h path (c, [], x, y); v)
2538 else { v with f = doc path x y c [] }
2540 | Vopen (tag, _, closed) ->
2541 error "unexpected subelement in llppconfig" s spos
2543 | Vclose "llppconfig" -> { v with f = toplevel }
2544 | Vclose tag -> error "unexpected close in llppconfig" s spos
2546 and doc path x y c bookmarks v t spos epos =
2547 match t with
2548 | Vdata | Vcdata -> v
2549 | Vend -> error "unexpected end of input in doc" s spos
2550 | Vopen ("bookmarks", attrs, closed) ->
2551 { v with f = pbookmarks path x y c bookmarks }
2553 | Vopen (tag, _, _) ->
2554 error "unexpected subelement in doc" s spos
2556 | Vclose "doc" ->
2557 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2558 { v with f = llppconfig }
2560 | Vclose tag -> error "unexpected close in doc" s spos
2562 and pbookmarks path x y c bookmarks v t spos epos =
2563 match t with
2564 | Vdata | Vcdata -> v
2565 | Vend -> error "unexpected end of input in bookmarks" s spos
2566 | Vopen ("item", attrs, closed) ->
2567 let titleent, spage, srely = bookmark_of attrs in
2568 let page =
2570 int_of_string spage
2571 with exn ->
2572 dolog "Failed to convert page %S to integer: %s"
2573 spage (Printexc.to_string exn);
2576 let rely =
2578 float_of_string srely
2579 with exn ->
2580 dolog "Failed to convert rely %S to real: %s"
2581 srely (Printexc.to_string exn);
2584 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2585 if closed
2586 then { v with f = pbookmarks path x y c bookmarks }
2587 else
2588 let f () = v in
2589 { v with f = skip "item" f }
2591 | Vopen _ ->
2592 error "unexpected subelement in bookmarks" s spos
2594 | Vclose "bookmarks" ->
2595 { v with f = doc path x y c bookmarks }
2597 | Vclose tag -> error "unexpected close in bookmarks" s spos
2599 and skip tag f v t spos epos =
2600 match t with
2601 | Vdata | Vcdata -> v
2602 | Vend ->
2603 error ("unexpected end of input in skipped " ^ tag) s spos
2604 | Vopen (tag', _, closed) ->
2605 if closed
2606 then v
2607 else
2608 let f' () = { v with f = skip tag f } in
2609 { v with f = skip tag' f' }
2610 | Vclose ctag ->
2611 if tag = ctag
2612 then f ()
2613 else error ("unexpected close in skipped " ^ tag) s spos
2616 parse { f = toplevel; accu = () } s;
2617 h, dc;
2620 let do_load f ic =
2622 let len = in_channel_length ic in
2623 let s = String.create len in
2624 really_input ic s 0 len;
2625 f s;
2626 with
2627 | Parse_error (msg, s, pos) ->
2628 let subs = subs s pos in
2629 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2630 failwith ("parse error: " ^ s)
2632 | exn ->
2633 failwith ("config load error: " ^ Printexc.to_string exn)
2636 let path =
2637 let dir =
2639 let dir = Filename.concat home ".config" in
2640 if Sys.is_directory dir then dir else home
2641 with _ -> home
2643 Filename.concat dir "llpp.conf"
2646 let load1 f =
2647 if Sys.file_exists path
2648 then
2649 match
2650 (try Some (open_in_bin path)
2651 with exn ->
2652 prerr_endline
2653 ("Error opening configuation file `" ^ path ^ "': " ^
2654 Printexc.to_string exn);
2655 None
2657 with
2658 | Some ic ->
2659 begin try
2660 f (do_load get ic)
2661 with exn ->
2662 prerr_endline
2663 ("Error loading configuation from `" ^ path ^ "': " ^
2664 Printexc.to_string exn);
2665 end;
2666 close_in ic;
2668 | None -> ()
2669 else
2670 f (Hashtbl.create 0, defconf)
2673 let load () =
2674 let f (h, dc) =
2675 let pc, pb, px, py =
2677 Hashtbl.find h state.path
2678 with Not_found -> dc, [], 0, 0.0
2680 setconf defconf dc;
2681 setconf conf pc;
2682 state.bookmarks <- pb;
2683 state.x <- px;
2684 cbput state.hists.nav py;
2686 load1 f
2689 let add_attrs bb always dc c =
2690 let ob s a b =
2691 if always || a != b
2692 then Printf.bprintf bb "\n %s='%b'" s a
2693 and oi s a b =
2694 if always || a != b
2695 then Printf.bprintf bb "\n %s='%d'" s a
2696 and oz s a b =
2697 if always || a <> b
2698 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2700 let w, h =
2701 if always
2702 then dc.winw, dc.winh
2703 else
2704 match state.fullscreen with
2705 | Some wh -> wh
2706 | None -> c.winw, c.winh
2708 let zoom, presentation, interpagespace, showall=
2709 if always
2710 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2711 else
2712 match state.birdseye with
2713 | Some (bc, _, _, _) ->
2714 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2715 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2717 oi "width" w dc.winw;
2718 oi "height" h dc.winh;
2719 oi "scroll-bar-width" c.scrollw dc.scrollw;
2720 oi "scroll-handle-height" c.scrollh dc.scrollh;
2721 ob "case-insensitive-search" c.icase dc.icase;
2722 ob "preload" c.preload dc.preload;
2723 oi "page-bias" c.pagebias dc.pagebias;
2724 oi "scroll-step" c.scrollincr dc.scrollincr;
2725 ob "max-height-fit" c.maxhfit dc.maxhfit;
2726 ob "crop-hack" c.crophack dc.crophack;
2727 ob "throttle" showall dc.showall;
2728 ob "highlight-links" c.hlinks dc.hlinks;
2729 ob "under-cursor-info" c.underinfo dc.underinfo;
2730 oi "vertical-margin" interpagespace dc.interpagespace;
2731 oz "zoom" zoom dc.zoom;
2732 ob "presentation" presentation dc.presentation;
2733 oi "rotation-angle" c.angle dc.angle;
2734 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2735 ob "proportional-display" c.proportional dc.proportional;
2736 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2737 oi "texcount" c.texcount dc.texcount;
2738 oi "slice-height" c.sliceheight dc.sliceheight;
2739 oi "thumbnail-width" c.thumbw dc.thumbw;
2742 let save () =
2743 let bb = Buffer.create 32768 in
2744 let f (h, dc) =
2745 Buffer.add_string bb "<llppconfig>\n<defaults ";
2746 add_attrs bb true dc dc;
2747 Buffer.add_string bb "/>\n";
2749 let adddoc path x y c bookmarks =
2750 if bookmarks == [] && c = dc && y = 0.0
2751 then ()
2752 else (
2753 Printf.bprintf bb "<doc path='%s'"
2754 (enent path 0 (String.length path));
2756 if y <> 0.0
2757 then Printf.bprintf bb " rely='%f'" y;
2759 if x != 0
2760 then Printf.bprintf bb " pan='%d'" x;
2762 add_attrs bb false dc c;
2764 begin match bookmarks with
2765 | [] -> Buffer.add_string bb "/>\n"
2766 | _ ->
2767 Buffer.add_string bb ">\n<bookmarks>\n";
2768 List.iter (fun (title, _level, page, rely) ->
2769 Printf.bprintf bb
2770 "<item title='%s' page='%d' rely='%f'/>\n"
2771 (enent title 0 (String.length title))
2772 page
2773 rely
2774 ) bookmarks;
2775 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2776 end;
2780 let x =
2781 match state.birdseye with
2782 | Some (_, x, _, _) -> x
2783 | None -> state.x
2785 adddoc state.path x (yratio state.y) conf
2786 (if conf.savebmarks then state.bookmarks else []);
2788 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2789 if path <> state.path
2790 then
2791 adddoc path x y c bookmarks
2792 ) h;
2793 Buffer.add_string bb "</llppconfig>";
2795 load1 f;
2796 if Buffer.length bb > 0
2797 then
2799 let tmp = path ^ ".tmp" in
2800 let oc = open_out_bin tmp in
2801 Buffer.output_buffer oc bb;
2802 close_out oc;
2803 Sys.rename tmp path;
2804 with exn ->
2805 prerr_endline
2806 ("error while saving configuration: " ^ Printexc.to_string exn)
2808 end;;
2810 let () =
2811 Arg.parse
2812 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2813 (fun s -> state.path <- s)
2814 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2816 let path =
2817 if String.length state.path = 0
2818 then (prerr_endline "filename missing"; exit 1)
2819 else (
2820 if Filename.is_relative state.path
2821 then Filename.concat (Sys.getcwd ()) state.path
2822 else state.path
2825 state.path <- path;
2827 State.load ();
2829 let _ = Glut.init Sys.argv in
2830 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2831 let () = Glut.initWindowSize conf.winw conf.winh in
2832 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2834 let csock, ssock =
2835 if Sys.os_type = "Unix"
2836 then
2837 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2838 else
2839 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2840 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2841 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2842 Unix.bind sock addr;
2843 Unix.listen sock 1;
2844 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2845 Unix.connect csock addr;
2846 let ssock, _ = Unix.accept sock in
2847 Unix.close sock;
2848 let opts sock =
2849 Unix.setsockopt sock Unix.TCP_NODELAY true;
2850 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2852 opts ssock;
2853 opts csock;
2854 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2855 ssock, csock
2858 let () = Glut.displayFunc display in
2859 let () = Glut.reshapeFunc reshape in
2860 let () = Glut.keyboardFunc keyboard in
2861 let () = Glut.specialFunc special in
2862 let () = Glut.idleFunc (Some idle) in
2863 let () = Glut.mouseFunc mouse in
2864 let () = Glut.motionFunc motion in
2865 let () = Glut.passiveMotionFunc pmotion in
2867 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2868 state.csock <- csock;
2869 state.ssock <- ssock;
2870 state.text <- "Opening " ^ path;
2871 writeopen state.path state.password;
2873 at_exit State.save;
2875 let rec handlelablglutbug () =
2877 Glut.mainLoop ();
2878 with Glut.BadEnum "key in special_of_int" ->
2879 showtext '!' " LablGlut bug: special key not recognized";
2880 handlelablglutbug ()
2882 handlelablglutbug ();