Too many things needs special care in bird's eye so this..
[llpp.git] / main.ml
blobea435a0efbc5645706aff2a419ecd49f3ff1e99b
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 ty : float
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) 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 birdseyepageno : pageno
200 ; mutable gen : gen
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 ; ty = 0.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 ; birdseyepageno = 0
276 ; gen = 0
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 n = List.exists (fun l -> l.pageno = n) state.layout;;
547 let preload () =
548 if conf.preload
549 then
550 let oktopreload =
551 let opaque = cbpeek state.pagecache in
552 match findpageforopaque opaque with
553 | Some ((n, _, _, _, _), size) ->
554 not (pagevisible n) && state.memused - size <= conf.memlimit
555 | None -> false
557 if oktopreload
558 then
559 let rely = yratio state.y in
560 let presentation = conf.presentation in
561 let interpagespace = conf.interpagespace in
562 let maxy = state.maxy in
563 conf.presentation <- false;
564 conf.interpagespace <- 0;
565 state.maxy <- calcheight ();
566 let y = truncate (float state.maxy *. rely) in
567 let y = if y < conf.winh then 0 else y - conf.winh in
568 let pages = layout y (conf.winh*3) in
569 List.iter render pages;
570 conf.presentation <- presentation;
571 conf.interpagespace <- interpagespace;
572 state.maxy <- maxy;
575 let gotoy y =
576 let y = max 0 y in
577 let y = min state.maxy y in
578 let pages = layout y conf.winh in
579 let ready = loadlayout pages in
580 state.ty <- yratio y;
581 state.layout <- pages;
582 if conf.showall
583 then (
584 if ready
585 then (
586 state.y <- y;
587 Glut.postRedisplay ();
590 else (
591 state.y <- y;
592 Glut.postRedisplay ();
594 if state.birdseye <> None
595 then (
596 if not (pagevisible state.birdseyepageno)
597 then
598 match state.layout with
599 | [] -> ()
600 | l :: _ -> state.birdseyepageno <- l.pageno
602 preload ();
605 let gotoy_and_clear_text y =
606 gotoy y;
607 if not conf.verbose then state.text <- "";
610 let addnav () =
611 cbput state.hists.nav (yratio state.y);
614 let getnav () =
615 let y = cbgetc state.hists.nav ~-1 in
616 truncate (y *. float state.maxy)
619 let gotopage n top =
620 let y, h = getpageyh n in
621 addnav ();
622 gotoy_and_clear_text (y + (truncate (top *. float h)));
625 let gotopage1 n top =
626 let y = getpagey n in
627 addnav ();
628 gotoy_and_clear_text (y + top);
631 let invalidate () =
632 state.layout <- [];
633 state.pdims <- [];
634 state.rects <- [];
635 state.rects1 <- [];
636 state.invalidated <- state.invalidated + 1;
639 let scalecolor c =
640 let c = c *. state.colorscale in
641 (c, c, c);
644 let represent () =
645 let y =
646 match state.layout with
647 | [] ->
648 let rely = yratio state.y in
649 state.maxy <- calcheight ();
650 truncate (float state.maxy *. rely)
652 | l :: _ ->
653 state.maxy <- calcheight ();
654 getpagey l.pageno
656 gotoy y
659 let pagematrix () =
660 GlMat.mode `projection;
661 GlMat.load_identity ();
662 GlMat.rotate ~x:1.0 ~angle:180.0 ();
663 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
664 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
667 let winmatrix () =
668 GlMat.mode `projection;
669 GlMat.load_identity ();
670 GlMat.rotate ~x:1.0 ~angle:180.0 ();
671 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
672 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
675 let reshape ~w ~h =
676 conf.winw <- w;
677 let w = truncate (float w *. conf.zoom) - conf.scrollw in
678 let w = max w 2 in
679 state.w <- w;
680 conf.winh <- h;
681 GlMat.mode `modelview;
682 GlMat.load_identity ();
683 GlClear.color (scalecolor 1.0);
684 GlClear.clear [`color];
686 invalidate ();
687 wcmd "geometry" [`i w; `i h];
690 let showtext c s =
691 GlDraw.color (0.0, 0.0, 0.0);
692 GlDraw.rect
693 (0.0, float (conf.winh - 18))
694 (float (conf.winw - conf.scrollw - 1), float conf.winh)
696 let font = Glut.BITMAP_8_BY_13 in
697 GlDraw.color (1.0, 1.0, 1.0);
698 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
699 Glut.bitmapCharacter ~font ~c:(Char.code c);
700 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
703 let enttext () =
704 let len = String.length state.text in
705 match state.textentry with
706 | None ->
707 if len > 0 then showtext ' ' state.text
709 | Some (c, text, _, _, _) ->
710 let s =
711 if len > 0
712 then
713 text ^ " [" ^ state.text ^ "]"
714 else
715 text
717 showtext c s;
720 let showtext c s =
721 if true
722 then (
723 state.text <- Printf.sprintf "%c%s" c s;
724 Glut.postRedisplay ();
726 else (
727 showtext c s;
728 Glut.swapBuffers ();
732 let act cmd =
733 match cmd.[0] with
734 | 'c' ->
735 state.pdims <- [];
737 | 'D' ->
738 state.rects <- state.rects1;
739 Glut.postRedisplay ()
741 | 'C' ->
742 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
743 state.pagecount <- n;
744 state.invalidated <- state.invalidated - 1;
745 if state.invalidated = 0
746 then represent ()
748 | 't' ->
749 let s = Scanf.sscanf cmd "t %n"
750 (fun n -> String.sub cmd n (String.length cmd - n))
752 Glut.setWindowTitle s
754 | 'T' ->
755 let s = Scanf.sscanf cmd "T %n"
756 (fun n -> String.sub cmd n (String.length cmd - n))
758 if state.textentry = None
759 then (
760 state.text <- s;
761 showtext ' ' s;
763 else (
764 state.text <- s;
765 Glut.postRedisplay ();
768 | 'V' ->
769 if conf.verbose
770 then
771 let s = Scanf.sscanf cmd "V %n"
772 (fun n -> String.sub cmd n (String.length cmd - n))
774 state.text <- s;
775 showtext ' ' s;
777 | 'F' ->
778 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
779 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
780 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
781 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
783 let y = (getpagey pageno) + truncate y0 in
784 addnav ();
785 gotoy y;
786 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
788 | 'R' ->
789 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
790 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
791 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
792 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
794 state.rects1 <-
795 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
797 | 'r' ->
798 let n, w, h, r, l, s, p =
799 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
800 (fun n w h r l s p ->
801 (n-1, w, h, r, l != 0, s, p))
804 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
805 state.memused <- state.memused + s;
807 let rec gc () =
808 if (state.memused <= conf.memlimit) || cbempty state.pagecache
809 then ()
810 else (
811 let evictedopaque = cbpeek state.pagecache in
812 match findpageforopaque evictedopaque with
813 | None -> failwith "bug in gc"
814 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
815 if state.gen != gen || not (pagevisible evictedn)
816 then (
817 wcmd "free" [`s evictedopaque];
818 state.memused <- state.memused - evictedsize;
819 Hashtbl.remove state.pagemap k;
820 cbdecr state.pagecache;
821 gc ();
825 gc ();
827 cbput state.pagecache p;
828 state.rendering <- false;
830 if conf.showall
831 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
832 else (
833 if pagevisible n
834 then gotoy state.y
835 else (ignore (loadlayout state.layout); preload ())
838 | 'l' ->
839 let (n, w, h, x) as pdim =
840 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
842 state.pdims <- pdim :: state.pdims
844 | 'o' ->
845 let (l, n, t, h, pos) =
846 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
848 let s = String.sub cmd pos (String.length cmd - pos) in
849 let s =
850 let l = String.length s in
851 let b = Buffer.create (String.length s) in
852 let rec loop pc2 i =
853 if i = l
854 then ()
855 else
856 let pc2 =
857 match s.[i] with
858 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
859 | '\xc2' -> true
860 | c ->
861 let c = if Char.code c land 0x80 = 0 then c else '?' in
862 Buffer.add_char b c;
863 false
865 loop pc2 (i+1)
867 loop false 0;
868 Buffer.contents b
870 let outline = (s, l, n, float t /. float h) in
871 let outlines =
872 match state.outlines with
873 | Olist outlines -> Olist (outline :: outlines)
874 | Oarray _ -> Olist [outline]
875 | Onarrow _ -> Olist [outline]
877 state.outlines <- outlines
879 | _ ->
880 log "unknown cmd `%S'" cmd
883 let now = Unix.gettimeofday;;
885 let idle () =
886 let rec loop delay =
887 let r, _, _ = Unix.select [state.csock] [] [] delay in
888 begin match r with
889 | [] ->
890 if conf.autoscroll
891 then begin
892 let y = state.y + conf.scrollincr in
893 let y = if y >= state.maxy then 0 else y in
894 gotoy y;
895 state.text <- "";
896 end;
898 | _ ->
899 let cmd = readcmd state.csock in
900 act cmd;
901 loop 0.0
902 end;
903 in loop 0.001
906 let onhist cb =
907 let rc = cb.rc in
908 let action = function
909 | HCprev -> cbget cb ~-1
910 | HCnext -> cbget cb 1
911 | HCfirst -> cbget cb ~-(cb.rc)
912 | HClast -> cbget cb (cb.len - 1 - cb.rc)
913 and cancel () = cb.rc <- rc
914 in (action, cancel)
917 let search pattern forward =
918 if String.length pattern > 0
919 then
920 let pn, py =
921 match state.layout with
922 | [] -> 0, 0
923 | l :: _ ->
924 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
926 let cmd =
927 let b = makecmd "search"
928 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
930 Buffer.add_char b ',';
931 Buffer.add_string b pattern;
932 Buffer.add_char b '\000';
933 Buffer.contents b;
935 writecmd state.csock cmd;
938 let intentry text key =
939 let c = Char.unsafe_chr key in
940 match c with
941 | '0' .. '9' ->
942 let s = "x" in s.[0] <- c;
943 let text = text ^ s in
944 TEcont text
946 | _ ->
947 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
948 TEcont text
951 let addchar s c =
952 let b = Buffer.create (String.length s + 1) in
953 Buffer.add_string b s;
954 Buffer.add_char b c;
955 Buffer.contents b;
958 let textentry text key =
959 let c = Char.unsafe_chr key in
960 match c with
961 | _ when key >= 32 && key < 127 ->
962 let text = addchar text c in
963 TEcont text
965 | _ ->
966 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
967 TEcont text
970 let reinit angle proportional =
971 conf.angle <- angle;
972 conf.proportional <- proportional;
973 invalidate ();
974 wcmd "reinit" [`i angle; `b proportional];
977 let optentry text key =
978 let btos b = if b then "on" else "off" in
979 let c = Char.unsafe_chr key in
980 match c with
981 | 's' ->
982 let ondone s =
983 try conf.scrollincr <- int_of_string s with exc ->
984 state.text <- Printf.sprintf "bad integer `%s': %s"
985 s (Printexc.to_string exc)
987 TEswitch ('#', "", None, intentry, ondone)
989 | 'R' ->
990 let ondone s =
991 match try
992 Some (int_of_string s)
993 with exc ->
994 state.text <- Printf.sprintf "bad integer `%s': %s"
995 s (Printexc.to_string exc);
996 None
997 with
998 | Some angle -> reinit angle conf.proportional
999 | None -> ()
1001 TEswitch ('^', "", None, intentry, ondone)
1003 | 'i' ->
1004 conf.icase <- not conf.icase;
1005 TEdone ("case insensitive search " ^ (btos conf.icase))
1007 | 'p' ->
1008 conf.preload <- not conf.preload;
1009 gotoy state.y;
1010 TEdone ("preload " ^ (btos conf.preload))
1012 | 'v' ->
1013 conf.verbose <- not conf.verbose;
1014 TEdone ("verbose " ^ (btos conf.verbose))
1016 | 'h' ->
1017 conf.maxhfit <- not conf.maxhfit;
1018 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1019 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1021 | 'c' ->
1022 conf.crophack <- not conf.crophack;
1023 TEdone ("crophack " ^ btos conf.crophack)
1025 | 'a' ->
1026 conf.showall <- not conf.showall;
1027 TEdone ("showall " ^ btos conf.showall)
1029 | 'f' ->
1030 conf.underinfo <- not conf.underinfo;
1031 TEdone ("underinfo " ^ btos conf.underinfo)
1033 | 'P' ->
1034 conf.savebmarks <- not conf.savebmarks;
1035 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1037 | 'S' ->
1038 let ondone s =
1040 conf.interpagespace <- int_of_string s;
1041 let rely = yratio state.y in
1042 state.maxy <- calcheight ();
1043 gotoy (truncate (float state.maxy *. rely));
1044 with exc ->
1045 state.text <- Printf.sprintf "bad integer `%s': %s"
1046 s (Printexc.to_string exc)
1048 TEswitch ('%', "", None, intentry, ondone)
1050 | 'l' ->
1051 reinit conf.angle (not conf.proportional);
1052 TEdone ("proprortional display " ^ btos conf.proportional)
1054 | _ ->
1055 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1056 TEstop
1059 let maxoutlinerows () = (conf.winh - 31) / 16;;
1061 let enterselector allowdel outlines errmsg msg =
1062 if Array.length outlines = 0
1063 then (
1064 showtext ' ' errmsg;
1066 else (
1067 state.text <- msg;
1068 Glut.setCursor Glut.CURSOR_INHERIT;
1069 let pageno =
1070 match state.layout with
1071 | [] -> -1
1072 | {pageno=pageno} :: rest -> pageno
1074 let active =
1075 let rec loop n =
1076 if n = Array.length outlines
1077 then 0
1078 else
1079 let (_, _, outlinepageno, _) = outlines.(n) in
1080 if outlinepageno >= pageno then n else loop (n+1)
1082 loop 0
1084 state.outline <-
1085 Some (allowdel, active,
1086 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1087 Glut.postRedisplay ();
1091 let enteroutlinemode () =
1092 let outlines, msg =
1093 match state.outlines with
1094 | Oarray a -> a, ""
1095 | Olist l ->
1096 let a = Array.of_list (List.rev l) in
1097 state.outlines <- Oarray a;
1098 a, ""
1099 | Onarrow (pat, a, b) ->
1100 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1102 enterselector false outlines "Document has no outline" msg;
1105 let enterbookmarkmode () =
1106 let bookmarks = Array.of_list state.bookmarks in
1107 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1110 let quickbookmark ?title () =
1111 match state.layout with
1112 | [] -> ()
1113 | l :: _ ->
1114 let title =
1115 match title with
1116 | None ->
1117 let sec = Unix.gettimeofday () in
1118 let tm = Unix.localtime sec in
1119 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1120 (l.pageno+1)
1121 tm.Unix.tm_mday
1122 tm.Unix.tm_mon
1123 (tm.Unix.tm_year + 1900)
1124 tm.Unix.tm_hour
1125 tm.Unix.tm_min
1126 | Some title -> title
1128 state.bookmarks <-
1129 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1132 let doreshape w h =
1133 state.fullscreen <- None;
1134 Glut.reshapeWindow w h;
1137 let writeopen path password =
1138 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1141 let opendoc path password =
1142 invalidate ();
1143 state.path <- path;
1144 state.password <- password;
1145 state.gen <- state.gen + 1;
1147 writeopen path password;
1148 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1149 wcmd "geometry" [`i state.w; `i conf.winh];
1152 let birdseyeoff (c, leftx) =
1153 state.birdseye <- None;
1154 conf.zoom <- c.zoom;
1155 conf.presentation <- c.presentation;
1156 conf.interpagespace <- c.interpagespace;
1157 conf.showall <- c.showall;
1158 conf.hlinks <- c.hlinks;
1159 state.x <- leftx;
1160 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1161 (100.0*.conf.zoom);
1164 let viewkeyboard ~key ~x ~y =
1165 let enttext te =
1166 state.textentry <- te;
1167 state.text <- "";
1168 enttext ();
1169 Glut.postRedisplay ()
1171 match state.textentry with
1172 | None ->
1173 let c = Char.chr key in
1174 begin match c with
1175 | '\027' | 'q' ->
1176 exit 0
1178 | '\008' ->
1179 let y = getnav () in
1180 gotoy_and_clear_text y
1182 | '\013' ->
1183 begin match state.birdseye with
1184 | None -> ()
1185 | Some vals ->
1186 let y = getpagey state.birdseyepageno in
1187 state.y <- y;
1188 birdseyeoff vals;
1189 reshape conf.winw conf.winh;
1190 end;
1192 | 'o' ->
1193 enteroutlinemode ()
1195 | 'u' ->
1196 state.rects <- [];
1197 state.text <- "";
1198 Glut.postRedisplay ()
1200 | '/' | '?' ->
1201 let ondone isforw s =
1202 cbput state.hists.pat s;
1203 state.searchpattern <- s;
1204 search s isforw
1206 enttext (Some (c, "", Some (onhist state.hists.pat),
1207 textentry, ondone (c ='/')))
1209 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1210 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1211 conf.zoom <- min 2.2 (conf.zoom +. incr);
1212 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1213 reshape conf.winw conf.winh
1215 | '+' ->
1216 let ondone s =
1217 let n =
1218 try int_of_string s with exc ->
1219 state.text <- Printf.sprintf "bad integer `%s': %s"
1220 s (Printexc.to_string exc);
1221 max_int
1223 if n != max_int
1224 then (
1225 conf.pagebias <- n;
1226 state.text <- "page bias is now " ^ string_of_int n;
1229 enttext (Some ('+', "", None, intentry, ondone))
1231 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1232 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1233 conf.zoom <- max 0.01 (conf.zoom -. decr);
1234 if conf.zoom <= 1.0 then state.x <- 0;
1235 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1236 reshape conf.winw conf.winh;
1238 | '-' ->
1239 let ondone msg =
1240 state.text <- msg;
1242 enttext (Some ('-', "", None, optentry, ondone))
1244 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1245 state.x <- 0;
1246 conf.zoom <- 1.0;
1247 state.text <- "zoom is 100%";
1248 reshape conf.winw conf.winh
1250 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1251 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1252 if zoom < 1.0
1253 then (
1254 conf.zoom <- zoom;
1255 state.x <- 0;
1256 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1257 reshape conf.winw conf.winh;
1260 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1261 begin match state.birdseye with
1262 | None ->
1263 let zoom = 50.0 /. float state.w in
1264 state.birdseye <- Some ({ conf with zoom = conf.zoom }, state.x);
1265 conf.zoom <- zoom;
1266 conf.presentation <- false;
1267 conf.interpagespace <- 10;
1268 conf.hlinks <- false;
1269 state.x <- 0;
1270 state.mstate <- Mnone;
1271 conf.showall <- false;
1272 Glut.setCursor Glut.CURSOR_INHERIT;
1273 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1274 (100.0*.zoom)
1276 | Some vals ->
1277 birdseyeoff vals;
1278 end;
1279 reshape conf.winw conf.winh
1281 | '0' .. '9' ->
1282 let ondone s =
1283 let n =
1284 try int_of_string s with exc ->
1285 state.text <- Printf.sprintf "bad integer `%s': %s"
1286 s (Printexc.to_string exc);
1289 if n >= 0
1290 then (
1291 addnav ();
1292 cbput state.hists.pag (string_of_int n);
1293 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1296 let pageentry text key =
1297 match Char.unsafe_chr key with
1298 | 'g' -> TEdone text
1299 | _ -> intentry text key
1301 let text = "x" in text.[0] <- c;
1302 enttext (Some (':', text, Some (onhist state.hists.pag),
1303 pageentry, ondone))
1305 | 'b' ->
1306 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1307 reshape conf.winw conf.winh;
1309 | 'l' ->
1310 conf.hlinks <- not conf.hlinks;
1311 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1312 Glut.postRedisplay ()
1314 | 'a' ->
1315 conf.autoscroll <- not conf.autoscroll
1317 | 'P' ->
1318 conf.presentation <- not conf.presentation;
1319 showtext ' ' ("presentation mode " ^
1320 if conf.presentation then "on" else "off");
1321 represent ()
1323 | 'f' ->
1324 begin match state.fullscreen with
1325 | None ->
1326 state.fullscreen <- Some (conf.winw, conf.winh);
1327 Glut.fullScreen ()
1328 | Some (w, h) ->
1329 state.fullscreen <- None;
1330 doreshape w h
1333 | 'g' ->
1334 gotoy_and_clear_text 0
1336 | 'n' ->
1337 search state.searchpattern true
1339 | 'p' | 'N' ->
1340 search state.searchpattern false
1342 | 't' ->
1343 begin match state.layout with
1344 | [] -> ()
1345 | l :: _ ->
1346 gotoy_and_clear_text (getpagey l.pageno)
1349 | ' ' ->
1350 begin match List.rev state.layout with
1351 | [] -> ()
1352 | l :: _ ->
1353 let pageno = min (l.pageno+1) (state.pagecount-1) in
1354 gotoy_and_clear_text (getpagey pageno)
1357 | '\127' ->
1358 begin match state.layout with
1359 | [] -> ()
1360 | l :: _ ->
1361 let pageno = max 0 (l.pageno-1) in
1362 gotoy_and_clear_text (getpagey pageno)
1365 | '=' ->
1366 let f (fn, ln) l =
1367 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1369 let fn, ln = List.fold_left f (-1, -1) state.layout in
1370 let s =
1371 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1372 let percent =
1373 if maxy <= 0
1374 then 100.
1375 else (100. *. (float state.y /. float maxy)) in
1376 if fn = ln
1377 then
1378 Printf.sprintf "Page %d of %d %.2f%%"
1379 (fn+1) state.pagecount percent
1380 else
1381 Printf.sprintf
1382 "Pages %d-%d of %d %.2f%%"
1383 (fn+1) (ln+1) state.pagecount percent
1385 showtext ' ' s;
1387 | 'w' ->
1388 begin match state.layout with
1389 | [] -> ()
1390 | l :: _ ->
1391 doreshape (l.pagew + conf.scrollw) l.pageh;
1392 Glut.postRedisplay ();
1395 | '\'' ->
1396 enterbookmarkmode ()
1398 | 'm' ->
1399 let ondone s =
1400 match state.layout with
1401 | l :: _ ->
1402 state.bookmarks <-
1403 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1404 :: state.bookmarks
1405 | _ -> ()
1407 enttext (Some ('~', "", None, textentry, ondone))
1409 | '~' ->
1410 quickbookmark ();
1411 showtext ' ' "Quick bookmark added";
1413 | 'z' ->
1414 begin match state.layout with
1415 | l :: _ ->
1416 let rect = getpdimrect l.pagedimno in
1417 let w, h =
1418 if conf.crophack
1419 then
1420 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1421 truncate (1.2 *. (rect.(3) -. rect.(0))))
1422 else
1423 (truncate (rect.(1) -. rect.(0)),
1424 truncate (rect.(3) -. rect.(0)))
1426 if w != 0 && h != 0
1427 then
1428 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1430 Glut.postRedisplay ();
1432 | [] -> ()
1435 | '<' | '>' ->
1436 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1438 | '[' | ']' ->
1439 state.colorscale <-
1440 max 0.0
1441 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1442 Glut.postRedisplay ()
1444 | 'k' -> gotoy (clamp (-conf.scrollincr))
1445 | 'j' -> gotoy (clamp conf.scrollincr)
1447 | 'r' -> opendoc state.path state.password
1449 | _ ->
1450 vlog "huh? %d %c" key (Char.chr key);
1453 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1454 let len = String.length text in
1455 if len = 0
1456 then (
1457 state.textentry <- None;
1458 Glut.postRedisplay ();
1460 else (
1461 let s = String.sub text 0 (len - 1) in
1462 enttext (Some (c, s, opthist, onkey, ondone))
1465 | Some (c, text, onhist, onkey, ondone) ->
1466 begin match Char.unsafe_chr key with
1467 | '\r' | '\n' ->
1468 ondone text;
1469 state.textentry <- None;
1470 Glut.postRedisplay ()
1472 | '\027' ->
1473 begin match onhist with
1474 | None -> ()
1475 | Some (_, onhistcancel) -> onhistcancel ()
1476 end;
1477 state.textentry <- None;
1478 Glut.postRedisplay ()
1480 | _ ->
1481 begin match onkey text key with
1482 | TEdone text ->
1483 state.textentry <- None;
1484 ondone text;
1485 Glut.postRedisplay ()
1487 | TEcont text ->
1488 enttext (Some (c, text, onhist, onkey, ondone));
1490 | TEstop ->
1491 state.textentry <- None;
1492 Glut.postRedisplay ()
1494 | TEswitch te ->
1495 state.textentry <- Some te;
1496 Glut.postRedisplay ()
1497 end;
1498 end;
1501 let narrow outlines pattern =
1502 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1503 match reopt with
1504 | None -> None
1505 | Some re ->
1506 let rec fold accu n =
1507 if n = -1
1508 then accu
1509 else
1510 let (s, _, _, _) as o = outlines.(n) in
1511 let accu =
1512 if (try ignore (Str.search_forward re s 0); true
1513 with Not_found -> false)
1514 then (o :: accu)
1515 else accu
1517 fold accu (n-1)
1519 let matched = fold [] (Array.length outlines - 1) in
1520 if matched = [] then None else Some (Array.of_list matched)
1523 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1524 let search active pattern incr =
1525 let dosearch re =
1526 let rec loop n =
1527 if n = Array.length outlines || n = -1
1528 then None
1529 else
1530 let (s, _, _, _) = outlines.(n) in
1532 (try ignore (Str.search_forward re s 0); true
1533 with Not_found -> false)
1534 then Some n
1535 else loop (n + incr)
1537 loop active
1540 let re = Str.regexp_case_fold pattern in
1541 dosearch re
1542 with Failure s ->
1543 state.text <- s;
1544 None
1546 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1547 match key with
1548 | 27 ->
1549 if String.length qsearch = 0
1550 then (
1551 state.text <- "";
1552 state.outline <- None;
1553 Glut.postRedisplay ();
1555 else (
1556 state.text <- "";
1557 state.outline <- Some (allowdel, active, first, outlines, "");
1558 Glut.postRedisplay ();
1561 | 18 | 19 ->
1562 let incr = if key = 18 then -1 else 1 in
1563 let active, first =
1564 match search (active + incr) qsearch incr with
1565 | None ->
1566 state.text <- qsearch ^ " [not found]";
1567 active, first
1568 | Some active ->
1569 state.text <- qsearch;
1570 active, firstof active
1572 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1573 Glut.postRedisplay ();
1575 | 8 ->
1576 let len = String.length qsearch in
1577 if len = 0
1578 then ()
1579 else (
1580 if len = 1
1581 then (
1582 state.text <- "";
1583 state.outline <- Some (allowdel, active, first, outlines, "");
1585 else
1586 let qsearch = String.sub qsearch 0 (len - 1) in
1587 let active, first =
1588 match search active qsearch ~-1 with
1589 | None ->
1590 state.text <- qsearch ^ " [not found]";
1591 active, first
1592 | Some active ->
1593 state.text <- qsearch;
1594 active, firstof active
1596 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1598 Glut.postRedisplay ()
1600 | 13 ->
1601 if active < Array.length outlines
1602 then (
1603 let (_, _, n, t) = outlines.(active) in
1604 gotopage n t;
1606 state.text <- "";
1607 if allowdel then state.bookmarks <- Array.to_list outlines;
1608 state.outline <- None;
1609 Glut.postRedisplay ();
1611 | _ when key >= 32 && key < 127 ->
1612 let pattern = addchar qsearch (Char.chr key) in
1613 let active, first =
1614 match search active pattern 1 with
1615 | None ->
1616 state.text <- pattern ^ " [not found]";
1617 active, first
1618 | Some active ->
1619 state.text <- pattern;
1620 active, firstof active
1622 state.outline <- Some (allowdel, active, first, outlines, pattern);
1623 Glut.postRedisplay ()
1625 | 14 when not allowdel -> (* ctrl-n *)
1626 if String.length qsearch > 0
1627 then (
1628 let optoutlines = narrow outlines qsearch in
1629 begin match optoutlines with
1630 | None -> state.text <- "can't narrow"
1631 | Some outlines ->
1632 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1633 match state.outlines with
1634 | Olist l -> ()
1635 | Oarray a ->
1636 state.outlines <- Onarrow (qsearch, outlines, a)
1637 | Onarrow (pat, a, b) ->
1638 state.outlines <- Onarrow (qsearch, outlines, b)
1639 end;
1641 Glut.postRedisplay ()
1643 | 21 when not allowdel -> (* ctrl-u *)
1644 let outline =
1645 match state.outlines with
1646 | Oarray a -> a
1647 | Olist l ->
1648 let a = Array.of_list (List.rev l) in
1649 state.outlines <- Oarray a;
1651 | Onarrow (pat, a, b) ->
1652 state.outlines <- Oarray b;
1653 state.text <- "";
1656 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1657 Glut.postRedisplay ()
1659 | 12 ->
1660 state.outline <-
1661 Some (allowdel, active, firstof active, outlines, qsearch);
1662 Glut.postRedisplay ()
1664 | 127 when allowdel ->
1665 let len = Array.length outlines - 1 in
1666 if len = 0
1667 then (
1668 state.outline <- None;
1669 state.bookmarks <- [];
1671 else (
1672 let bookmarks = Array.init len
1673 (fun i ->
1674 let i = if i >= active then i + 1 else i in
1675 outlines.(i)
1678 state.outline <-
1679 Some (allowdel,
1680 min active (len-1),
1681 min first (len-1),
1682 bookmarks, qsearch)
1685 Glut.postRedisplay ()
1687 | _ -> log "unknown key %d" key
1690 let keyboard ~key ~x ~y =
1691 if key = 7
1692 then
1693 wcmd "interrupt" []
1694 else
1695 match state.outline with
1696 | None -> viewkeyboard ~key ~x ~y
1697 | Some outline -> outlinekeyboard ~key ~x ~y outline
1700 let special ~key ~x ~y =
1701 match state.outline with
1702 | None when state.birdseye <> None ->
1703 begin match key with
1704 | Glut.KEY_UP ->
1705 let pageno = max 0 (state.birdseyepageno - 1) in
1706 state.birdseyepageno <- pageno;
1707 if not (pagevisible pageno)
1708 then gotopage pageno 0.0
1709 else Glut.postRedisplay ();
1711 | Glut.KEY_DOWN ->
1712 let pageno = min (state.pagecount - 1) (state.birdseyepageno + 1) in
1713 state.birdseyepageno <- pageno;
1714 if not (pagevisible pageno)
1715 then
1716 begin match List.rev state.layout with
1717 | [] -> gotopage pageno 0.0
1718 | l :: _ ->
1719 gotoy (state.y + conf.interpagespace + l.pageh*2 - l.pagevh)
1721 else Glut.postRedisplay ();
1723 | Glut.KEY_PAGE_UP ->
1724 begin match state.layout with
1725 | l :: _ ->
1726 if l.pageno = state.birdseyepageno
1727 then (
1728 match layout (state.y - conf.winh) conf.winh with
1729 | [] -> gotoy (clamp (-conf.winh))
1730 | l :: _ ->
1731 state.birdseyepageno <- max 0 (l.pageno - 1);
1732 gotopage state.birdseyepageno 0.0
1734 else (
1735 state.birdseyepageno <- max 0 (l.pageno - 1);
1736 gotopage state.birdseyepageno 0.0
1738 | [] -> gotoy (clamp (-conf.winh))
1739 end;
1740 | Glut.KEY_PAGE_DOWN ->
1741 begin match List.rev state.layout with
1742 | l :: _ ->
1743 state.birdseyepageno <- min (state.pagecount - 1) (l.pageno + 1);
1744 gotoy (clamp (l.pagedispy + conf.interpagespace + l.pageh))
1745 | [] -> gotoy (clamp conf.winh)
1746 end;
1748 | Glut.KEY_HOME ->
1749 state.birdseyepageno <- 0;
1750 gotopage 0 0.0
1751 | Glut.KEY_END ->
1752 state.birdseyepageno <- state.pagecount - 1;
1753 if not (pagevisible state.birdseyepageno)
1754 then
1755 gotopage state.birdseyepageno 0.0
1756 else
1757 Glut.postRedisplay ()
1759 | _ -> ()
1762 | None ->
1763 begin match state.textentry with
1764 | None ->
1765 let y =
1766 match key with
1767 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1768 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1769 | Glut.KEY_DOWN -> clamp conf.scrollincr
1770 | Glut.KEY_PAGE_UP ->
1771 if Glut.getModifiers () land Glut.active_ctrl != 0
1772 then
1773 match state.layout with
1774 | [] -> state.y
1775 | l :: _ -> state.y - l.pagey
1776 else
1777 clamp (-conf.winh)
1778 | Glut.KEY_PAGE_DOWN ->
1779 if Glut.getModifiers () land Glut.active_ctrl != 0
1780 then
1781 match List.rev state.layout with
1782 | [] -> state.y
1783 | l :: _ -> getpagey l.pageno
1784 else
1785 clamp conf.winh
1786 | Glut.KEY_HOME -> addnav (); 0
1787 | Glut.KEY_END ->
1788 addnav ();
1789 state.maxy - (if conf.maxhfit then conf.winh else 0)
1791 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1792 state.x <- state.x - 10;
1793 state.y
1794 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1795 state.x <- state.x + 10;
1796 state.y
1798 | _ -> state.y
1800 gotoy_and_clear_text y
1802 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1803 let s =
1804 match key with
1805 | Glut.KEY_UP -> action HCprev
1806 | Glut.KEY_DOWN -> action HCnext
1807 | Glut.KEY_HOME -> action HCfirst
1808 | Glut.KEY_END -> action HClast
1809 | _ -> state.text
1811 state.textentry <- Some (c, s, onhist, onkey, ondone);
1812 Glut.postRedisplay ()
1814 | _ -> ()
1817 | Some (allowdel, active, first, outlines, qsearch) ->
1818 let maxrows = maxoutlinerows () in
1819 let calcfirst first active =
1820 if active > first
1821 then
1822 let rows = active - first in
1823 if rows > maxrows then active - maxrows else first
1824 else active
1826 let navigate incr =
1827 let active = active + incr in
1828 let active = max 0 (min active (Array.length outlines - 1)) in
1829 let first = calcfirst first active in
1830 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1831 Glut.postRedisplay ()
1833 let updownlevel incr =
1834 let len = Array.length outlines in
1835 let (_, curlevel, _, _) = outlines.(active) in
1836 let rec flow i =
1837 if i = len then i-1 else if i = -1 then 0 else
1838 let (_, l, _, _) = outlines.(i) in
1839 if l != curlevel then i else flow (i+incr)
1841 let active = flow active in
1842 let first = calcfirst first active in
1843 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1844 Glut.postRedisplay ()
1846 match key with
1847 | Glut.KEY_UP -> navigate ~-1
1848 | Glut.KEY_DOWN -> navigate 1
1849 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1850 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1852 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1853 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1855 | Glut.KEY_HOME ->
1856 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1857 Glut.postRedisplay ()
1859 | Glut.KEY_END ->
1860 let active = Array.length outlines - 1 in
1861 let first = max 0 (active - maxrows) in
1862 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1863 Glut.postRedisplay ()
1865 | _ -> ()
1868 let drawplaceholder l =
1869 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1870 GlDraw.color (scalecolor 1.0);
1871 GlDraw.rect
1872 (float l.pagex, float l.pagedispy)
1873 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1875 let x = float (if margin < 0 then -margin else 0)
1876 and y = float (l.pagedispy + 13) in
1877 let font = Glut.BITMAP_8_BY_13 in
1878 GlDraw.color (0.0, 0.0, 0.0);
1879 GlPix.raster_pos ~x ~y ();
1880 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1881 ("Loading " ^ string_of_int (l.pageno + 1));
1884 let now () = Unix.gettimeofday ();;
1886 let drawpage l =
1887 begin match getopaque l.pageno with
1888 | Some (opaque, _) when validopaque opaque ->
1889 if state.textentry = None
1890 then GlDraw.color (scalecolor 1.0)
1891 else GlDraw.color (scalecolor 0.4);
1892 let a = now () in
1893 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1894 opaque;
1895 let b = now () in
1896 let d = b-.a in
1897 vlog "draw %d %f sec" l.pageno d;
1899 | _ ->
1900 drawplaceholder l;
1901 end;
1902 if state.birdseye <> None && state.birdseyepageno = l.pageno
1903 then (
1904 GlDraw.polygon_mode `both `line;
1905 GlDraw.line_width 4.0;
1906 GlDraw.color (0.8, 0.0, 0.0);
1907 GlDraw.rect
1908 (float (l.pagex - 1), float (l.pagedispy - 1))
1909 (float (l.pagew + l.pagex + 1), float (l.pagedispy + l.pagevh + 1))
1911 GlDraw.line_width 1.0;
1912 GlDraw.polygon_mode `both `fill;
1916 let scrollph y =
1917 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1918 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1919 let sh = float conf.winh /. sh in
1920 let sh = max sh (float conf.scrollh) in
1922 let percent =
1923 if state.y = state.maxy
1924 then 1.0
1925 else float y /. float maxy
1927 let position = (float conf.winh -. sh) *. percent in
1929 let position =
1930 if position +. sh > float conf.winh
1931 then float conf.winh -. sh
1932 else position
1934 position, sh;
1937 let scrollindicator () =
1938 GlDraw.color (0.64 , 0.64, 0.64);
1939 GlDraw.rect
1940 (float (conf.winw - conf.scrollw), 0.)
1941 (float conf.winw, float conf.winh)
1943 GlDraw.color (0.0, 0.0, 0.0);
1945 let position, sh = scrollph state.y in
1946 GlDraw.rect
1947 (float (conf.winw - conf.scrollw), position)
1948 (float conf.winw, position +. sh)
1952 let showsel margin =
1953 match state.mstate with
1954 | Mnone | Mscroll _ | Mpan _ ->
1957 | Msel ((x0, y0), (x1, y1)) ->
1958 let rec loop = function
1959 | l :: ls ->
1960 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1961 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1962 then
1963 match getopaque l.pageno with
1964 | Some (opaque, _) when validopaque opaque ->
1965 let oy = -l.pagey + l.pagedispy in
1966 seltext opaque
1967 (x0 - margin - state.x, y0,
1968 x1 - margin - state.x, y1) oy;
1970 | _ -> ()
1971 else loop ls
1972 | [] -> ()
1974 loop state.layout
1977 let showrects () =
1978 let panx = float state.x in
1979 Gl.enable `blend;
1980 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1981 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1982 List.iter
1983 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1984 List.iter (fun l ->
1985 if l.pageno = pageno
1986 then (
1987 let d = float (l.pagedispy - l.pagey) in
1988 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1989 GlDraw.begins `quads;
1991 GlDraw.vertex2 (x0+.panx, y0+.d);
1992 GlDraw.vertex2 (x1+.panx, y1+.d);
1993 GlDraw.vertex2 (x2+.panx, y2+.d);
1994 GlDraw.vertex2 (x3+.panx, y3+.d);
1996 GlDraw.ends ();
1998 ) state.layout
1999 ) state.rects
2001 Gl.disable `blend;
2004 let showoutline = function
2005 | None -> ()
2006 | Some (allowdel, active, first, outlines, qsearch) ->
2007 Gl.enable `blend;
2008 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2009 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2010 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2011 Gl.disable `blend;
2013 GlDraw.color (1., 1., 1.);
2014 let font = Glut.BITMAP_9_BY_15 in
2015 let draw_string x y s =
2016 GlPix.raster_pos ~x ~y ();
2017 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2019 let rec loop row =
2020 if row = Array.length outlines || (row - first) * 16 > conf.winh
2021 then ()
2022 else (
2023 let (s, l, _, _) = outlines.(row) in
2024 let y = (row - first) * 16 in
2025 let x = 5 + 15*l in
2026 if row = active
2027 then (
2028 Gl.enable `blend;
2029 GlDraw.polygon_mode `both `line;
2030 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2031 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2032 GlDraw.rect (0., float (y + 1))
2033 (float (conf.winw - 1), float (y + 18));
2034 GlDraw.polygon_mode `both `fill;
2035 Gl.disable `blend;
2036 GlDraw.color (1., 1., 1.);
2038 draw_string (float x) (float (y + 16)) s;
2039 loop (row+1)
2042 loop first
2045 let display () =
2046 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2047 GlDraw.viewport margin 0 state.w conf.winh;
2048 pagematrix ();
2049 if state.birdseye <> None
2050 then
2051 GlClear.color (0.5, 0.5, 0.55)
2052 else
2053 GlClear.color (scalecolor 0.5)
2055 GlClear.clear [`color];
2056 if state.x != 0
2057 then (
2058 let x = float state.x in
2059 GlMat.translate ~x ();
2061 if conf.zoom > 1.0
2062 then (
2063 Gl.enable `scissor_test;
2064 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2066 List.iter drawpage state.layout;
2067 if conf.zoom > 1.0
2068 then
2069 Gl.disable `scissor_test
2071 if state.x != 0
2072 then (
2073 let x = -.float state.x in
2074 GlMat.translate ~x ();
2076 showrects ();
2077 showsel margin;
2078 GlDraw.viewport 0 0 conf.winw conf.winh;
2079 winmatrix ();
2080 scrollindicator ();
2081 showoutline state.outline;
2082 enttext ();
2083 Glut.swapBuffers ();
2086 let getunder x y =
2087 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2088 let x = x - margin - state.x in
2089 let rec f = function
2090 | l :: rest ->
2091 begin match getopaque l.pageno with
2092 | Some (opaque, _) when validopaque opaque ->
2093 let y = y - l.pagedispy in
2094 if y > 0
2095 then
2096 let y = l.pagey + y in
2097 let x = x - l.pagex in
2098 match whatsunder opaque x y with
2099 | Unone -> f rest
2100 | under -> under
2101 else
2102 f rest
2103 | _ ->
2104 f rest
2106 | [] -> Unone
2108 f state.layout
2111 let mouse ~button ~bstate ~x ~y =
2112 match button with
2113 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2114 let incr =
2115 if n = 3
2116 then
2117 -conf.scrollincr
2118 else
2119 conf.scrollincr
2121 let incr = incr * 2 in
2122 let y = clamp incr in
2123 gotoy_and_clear_text y
2125 | Glut.LEFT_BUTTON when state.outline = None
2126 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2127 if bstate = Glut.DOWN
2128 then (
2129 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2130 state.mstate <- Mpan (x, y)
2132 else
2133 state.mstate <- Mnone
2135 | Glut.LEFT_BUTTON
2136 when state.outline = None && x > conf.winw - conf.scrollw ->
2137 if bstate = Glut.DOWN
2138 then
2139 let position, sh = scrollph state.y in
2140 if y > truncate position && y < truncate (position +. sh)
2141 then
2142 state.mstate <- Mscroll
2143 else
2144 let percent = float y /. float conf.winh in
2145 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2146 gotoy desty;
2147 state.mstate <- Mscroll
2148 else
2149 state.mstate <- Mnone
2151 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2152 begin match state.birdseye with
2153 | Some vals ->
2154 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2155 let rec loop = function
2156 | [] -> ()
2157 | l :: rest ->
2158 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2159 && x > margin && x < margin + l.pagew
2160 then (
2161 let y = getpagey l.pageno in
2162 state.y <- y;
2163 birdseyeoff vals;
2164 reshape conf.winw conf.winh;
2166 else loop rest
2168 loop state.layout;
2169 | None -> () (* impossible *)
2172 | Glut.LEFT_BUTTON when state.outline = None ->
2173 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2174 begin match dest with
2175 | Ulinkgoto (pageno, top) ->
2176 if pageno >= 0
2177 then
2178 gotopage1 pageno top
2180 | Ulinkuri s ->
2181 print_endline s
2183 | Unone when bstate = Glut.DOWN ->
2184 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2185 state.mstate <- Mpan (x, y);
2187 | Unone | Utext _ ->
2188 if bstate = Glut.DOWN
2189 then (
2190 if conf.angle mod 360 = 0
2191 then (
2192 state.mstate <- Msel ((x, y), (x, y));
2193 Glut.postRedisplay ()
2196 else (
2197 match state.mstate with
2198 | Mnone -> ()
2200 | Mscroll ->
2201 state.mstate <- Mnone
2203 | Mpan _ ->
2204 Glut.setCursor Glut.CURSOR_INHERIT;
2205 state.mstate <- Mnone
2207 | Msel ((x0, y0), (x1, y1)) ->
2208 let f l =
2209 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2210 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2211 then
2212 match getopaque l.pageno with
2213 | Some (opaque, _) when validopaque opaque ->
2214 copysel opaque
2215 | _ -> ()
2217 List.iter f state.layout;
2218 copysel ""; (* ugly *)
2219 Glut.setCursor Glut.CURSOR_INHERIT;
2220 state.mstate <- Mnone;
2224 | _ ->
2227 let mouse ~button ~state ~x ~y = mouse button state x y;;
2229 let motion ~x ~y =
2230 if state.outline = None
2231 then
2232 match state.mstate with
2233 | Mnone -> ()
2235 | Mpan (x0, y0) ->
2236 let dx = x - x0
2237 and dy = y0 - y in
2238 state.mstate <- Mpan (x, y);
2239 if conf.zoom > 1.0 then state.x <- state.x + dx;
2240 let y = clamp dy in
2241 gotoy_and_clear_text y
2243 | Msel (a, _) ->
2244 state.mstate <- Msel (a, (x, y));
2245 Glut.postRedisplay ()
2247 | Mscroll ->
2248 let y = min conf.winh (max 0 y) in
2249 let percent = float y /. float conf.winh in
2250 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2251 gotoy_and_clear_text y
2254 let pmotion ~x ~y =
2255 if state.outline = None && state.birdseye = None
2256 then
2257 match state.mstate with
2258 | Mnone ->
2259 begin match getunder x y with
2260 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2261 | Ulinkuri uri ->
2262 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2263 Glut.setCursor Glut.CURSOR_INFO
2264 | Ulinkgoto (page, y) ->
2265 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
2266 Glut.setCursor Glut.CURSOR_INFO
2267 | Utext s ->
2268 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2269 Glut.setCursor Glut.CURSOR_TEXT
2272 | Mpan _ | Msel _ | Mscroll ->
2276 module State =
2277 struct
2278 open Parser
2280 let home =
2282 match Sys.os_type with
2283 | "Win32" -> Sys.getenv "HOMEPATH"
2284 | _ -> Sys.getenv "HOME"
2285 with exn ->
2286 prerr_endline
2287 ("Can not determine home directory location: " ^
2288 Printexc.to_string exn);
2292 let config_of c attrs =
2293 let apply c k v =
2295 match k with
2296 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2297 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2298 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2299 | "preload" -> { c with preload = bool_of_string v }
2300 | "page-bias" -> { c with pagebias = int_of_string v }
2301 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2302 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2303 | "crop-hack" -> { c with crophack = bool_of_string v }
2304 | "throttle" -> { c with showall = bool_of_string v }
2305 | "highlight-links" -> { c with hlinks = bool_of_string v }
2306 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2307 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2308 | "zoom" ->
2309 let zoom = float_of_string v /. 100. in
2310 let zoom = max 0.01 (min 2.2 zoom) in
2311 { c with zoom = zoom }
2312 | "presentation" -> { c with presentation = bool_of_string v }
2313 | "rotation-angle" -> { c with angle = int_of_string v }
2314 | "width" -> { c with winw = max 20 (int_of_string v) }
2315 | "height" -> { c with winh = max 20 (int_of_string v) }
2316 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2317 | "proportional-display" -> { c with proportional = bool_of_string v }
2318 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2319 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2320 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2321 | _ -> c
2322 with exn ->
2323 prerr_endline ("Error processing attribute (`" ^
2324 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2327 let rec fold c = function
2328 | [] -> c
2329 | (k, v) :: rest ->
2330 let c = apply c k v in
2331 fold c rest
2333 fold c attrs;
2336 let bookmark_of attrs =
2337 let rec fold title page rely = function
2338 | ("title", v) :: rest -> fold v page rely rest
2339 | ("page", v) :: rest -> fold title v rely rest
2340 | ("rely", v) :: rest -> fold title page v rest
2341 | _ :: rest -> fold title page rely rest
2342 | [] -> title, page, rely
2344 fold "invalid" "0" "0" attrs
2347 let setconf dst src =
2348 dst.scrollw <- src.scrollw;
2349 dst.scrollh <- src.scrollh;
2350 dst.icase <- src.icase;
2351 dst.preload <- src.preload;
2352 dst.pagebias <- src.pagebias;
2353 dst.verbose <- src.verbose;
2354 dst.scrollincr <- src.scrollincr;
2355 dst.maxhfit <- src.maxhfit;
2356 dst.crophack <- src.crophack;
2357 dst.autoscroll <- src.autoscroll;
2358 dst.showall <- src.showall;
2359 dst.hlinks <- src.hlinks;
2360 dst.underinfo <- src.underinfo;
2361 dst.interpagespace <- src.interpagespace;
2362 dst.zoom <- src.zoom;
2363 dst.presentation <- src.presentation;
2364 dst.angle <- src.angle;
2365 dst.winw <- src.winw;
2366 dst.winh <- src.winh;
2367 dst.savebmarks <- src.savebmarks;
2368 dst.memlimit <- src.memlimit;
2369 dst.proportional <- src.proportional;
2370 dst.texcount <- src.texcount;
2371 dst.sliceheight <- src.sliceheight;
2374 let unent s =
2375 let l = String.length s in
2376 let b = Buffer.create l in
2377 unent b s 0 l;
2378 Buffer.contents b;
2381 let get s =
2382 let h = Hashtbl.create 10 in
2383 let dc = { defconf with angle = defconf.angle } in
2384 let rec toplevel v t spos epos =
2385 match t with
2386 | Vdata | Vcdata | Vend -> v
2387 | Vopen ("llppconfig", attrs, closed) ->
2388 if closed
2389 then v
2390 else { v with f = llppconfig }
2391 | Vopen _ ->
2392 error "unexpected subelement at top level" s spos
2393 | Vclose tag -> error "unexpected close at top level" s spos
2395 and llppconfig v t spos epos =
2396 match t with
2397 | Vdata | Vcdata | Vend -> v
2398 | Vopen ("defaults", attrs, closed) ->
2399 let c = config_of dc attrs in
2400 setconf dc c;
2401 if closed
2402 then v
2403 else { v with f = skip "defaults" (fun () -> v) }
2405 | Vopen ("doc", attrs, closed) ->
2406 let pathent =
2408 List.assoc "path" attrs
2409 with Not_found -> error "doc is missing path attribute" s spos
2411 let path = unent pathent in
2412 let c = config_of dc attrs in
2413 let y =
2415 float_of_string (List.assoc "rely" attrs)
2416 with
2417 | Not_found -> 0.0
2418 | exn ->
2419 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2422 let x =
2424 int_of_string (List.assoc "pan" attrs)
2425 with
2426 | Not_found -> 0
2427 | exn ->
2428 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2431 if closed
2432 then (Hashtbl.add h path (c, [], x, y); v)
2433 else { v with f = doc path x y c [] }
2435 | Vopen (tag, _, closed) ->
2436 error "unexpected subelement in llppconfig" s spos
2438 | Vclose "llppconfig" -> { v with f = toplevel }
2439 | Vclose tag -> error "unexpected close in llppconfig" s spos
2441 and doc path x y c bookmarks v t spos epos =
2442 match t with
2443 | Vdata | Vcdata -> v
2444 | Vend -> error "unexpected end of input in doc" s spos
2445 | Vopen ("bookmarks", attrs, closed) ->
2446 { v with f = pbookmarks path x y c bookmarks }
2448 | Vopen (tag, _, _) ->
2449 error "unexpected subelement in doc" s spos
2451 | Vclose "doc" ->
2452 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2453 { v with f = llppconfig }
2455 | Vclose tag -> error "unexpected close in doc" s spos
2457 and pbookmarks path x y c bookmarks v t spos epos =
2458 match t with
2459 | Vdata | Vcdata -> v
2460 | Vend -> error "unexpected end of input in bookmarks" s spos
2461 | Vopen ("item", attrs, closed) ->
2462 let titleent, spage, srely = bookmark_of attrs in
2463 let page =
2465 int_of_string spage
2466 with exn ->
2467 dolog "Failed to convert page %S to integer: %s"
2468 spage (Printexc.to_string exn);
2471 let rely =
2473 float_of_string srely
2474 with exn ->
2475 dolog "Failed to convert rely %S to real: %s"
2476 srely (Printexc.to_string exn);
2479 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2480 if closed
2481 then { v with f = pbookmarks path x y c bookmarks }
2482 else
2483 let f () = v in
2484 { v with f = skip "item" f }
2486 | Vopen _ ->
2487 error "unexpected subelement in bookmarks" s spos
2489 | Vclose "bookmarks" ->
2490 { v with f = doc path x y c bookmarks }
2492 | Vclose tag -> error "unexpected close in bookmarks" s spos
2494 and skip tag f v t spos epos =
2495 match t with
2496 | Vdata | Vcdata -> v
2497 | Vend ->
2498 error ("unexpected end of input in skipped " ^ tag) s spos
2499 | Vopen (tag', _, closed) ->
2500 if closed
2501 then v
2502 else
2503 let f' () = { v with f = skip tag f } in
2504 { v with f = skip tag' f' }
2505 | Vclose ctag ->
2506 if tag = ctag
2507 then f ()
2508 else error ("unexpected close in skipped " ^ tag) s spos
2511 parse { f = toplevel; accu = () } s;
2512 h, dc;
2515 let do_load f ic =
2517 let len = in_channel_length ic in
2518 let s = String.create len in
2519 really_input ic s 0 len;
2520 f s;
2521 with
2522 | Parse_error (msg, s, pos) ->
2523 let subs = subs s pos in
2524 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2525 failwith ("parse error: " ^ s)
2527 | exn ->
2528 failwith ("config load error: " ^ Printexc.to_string exn)
2531 let path =
2532 let dir =
2534 let dir = Filename.concat home ".config" in
2535 if Sys.is_directory dir then dir else home
2536 with _ -> home
2538 Filename.concat dir "llpp.conf"
2541 let load1 f =
2542 if Sys.file_exists path
2543 then
2544 match
2545 (try Some (open_in_bin path)
2546 with exn ->
2547 prerr_endline
2548 ("Error opening configuation file `" ^ path ^ "': " ^
2549 Printexc.to_string exn);
2550 None
2552 with
2553 | Some ic ->
2554 begin try
2555 f (do_load get ic)
2556 with exn ->
2557 prerr_endline
2558 ("Error loading configuation from `" ^ path ^ "': " ^
2559 Printexc.to_string exn);
2560 end;
2561 close_in ic;
2563 | None -> ()
2564 else
2565 f (Hashtbl.create 0, defconf)
2568 let load () =
2569 let f (h, dc) =
2570 let pc, pb, px, py =
2572 Hashtbl.find h state.path
2573 with Not_found -> dc, [], 0, 0.0
2575 setconf defconf dc;
2576 setconf conf pc;
2577 state.bookmarks <- pb;
2578 state.x <- px;
2579 cbput state.hists.nav py;
2581 load1 f
2584 let add_attrs bb always dc c =
2585 let ob s a b =
2586 if always || a != b
2587 then Printf.bprintf bb "\n %s='%b'" s a
2588 and oi s a b =
2589 if always || a != b
2590 then Printf.bprintf bb "\n %s='%d'" s a
2591 and oz s a b =
2592 if always || a <> b
2593 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2595 let w, h =
2596 if always
2597 then dc.winw, dc.winh
2598 else
2599 match state.fullscreen with
2600 | Some wh -> wh
2601 | None -> c.winw, c.winh
2603 let zoom, presentation, interpagespace, showall=
2604 if always
2605 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2606 else
2607 match state.birdseye with
2608 | Some (bc, _) ->
2609 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2610 | None -> c.zoom, c.presentation, c.interpagespace, c.showall
2612 oi "width" w dc.winw;
2613 oi "height" h dc.winh;
2614 oi "scroll-bar-width" c.scrollw dc.scrollw;
2615 oi "scroll-handle-height" c.scrollh dc.scrollh;
2616 ob "case-insensitive-search" c.icase dc.icase;
2617 ob "preload" c.preload dc.preload;
2618 oi "page-bias" c.pagebias dc.pagebias;
2619 oi "scroll-step" c.scrollincr dc.scrollincr;
2620 ob "max-height-fit" c.maxhfit dc.maxhfit;
2621 ob "crop-hack" c.crophack dc.crophack;
2622 ob "throttle" showall dc.showall;
2623 ob "highlight-links" c.hlinks dc.hlinks;
2624 ob "under-cursor-info" c.underinfo dc.underinfo;
2625 oi "vertical-margin" interpagespace dc.interpagespace;
2626 oz "zoom" zoom dc.zoom;
2627 ob "presentation" presentation dc.presentation;
2628 oi "rotation-angle" c.angle dc.angle;
2629 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2630 ob "proportional-display" c.proportional dc.proportional;
2631 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2632 oi "texcount" c.texcount dc.texcount;
2633 oi "slice-height" c.sliceheight dc.sliceheight;
2636 let save () =
2637 let bb = Buffer.create 32768 in
2638 let f (h, dc) =
2639 Buffer.add_string bb "<llppconfig>\n<defaults ";
2640 add_attrs bb true dc dc;
2641 Buffer.add_string bb "/>\n";
2643 let adddoc path x y c bookmarks =
2644 if bookmarks == [] && c = dc && y = 0.0
2645 then ()
2646 else (
2647 Printf.bprintf bb "<doc path='%s'"
2648 (enent path 0 (String.length path));
2650 if y <> 0.0
2651 then Printf.bprintf bb " rely='%f'" y;
2653 if x != 0
2654 then Printf.bprintf bb " pan='%d'" x;
2656 add_attrs bb false dc c;
2658 begin match bookmarks with
2659 | [] -> Buffer.add_string bb "/>\n"
2660 | _ ->
2661 Buffer.add_string bb ">\n<bookmarks>\n";
2662 List.iter (fun (title, _level, page, rely) ->
2663 Printf.bprintf bb
2664 "<item title='%s' page='%d' rely='%f'/>\n"
2665 (enent title 0 (String.length title))
2666 page
2667 rely
2668 ) bookmarks;
2669 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2670 end;
2674 let x =
2675 match state.birdseye with
2676 | Some (_, x) -> x
2677 | None -> state.x
2679 adddoc state.path x (yratio state.y) conf
2680 (if conf.savebmarks then state.bookmarks else []);
2682 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2683 if path <> state.path
2684 then
2685 adddoc path x y c bookmarks
2686 ) h;
2687 Buffer.add_string bb "</llppconfig>";
2689 load1 f;
2690 if Buffer.length bb > 0
2691 then
2693 let tmp = path ^ ".tmp" in
2694 let oc = open_out_bin tmp in
2695 Buffer.output_buffer oc bb;
2696 close_out oc;
2697 Sys.rename tmp path;
2698 with exn ->
2699 prerr_endline
2700 ("error while saving configuration: " ^ Printexc.to_string exn)
2702 end;;
2704 let () =
2705 Arg.parse
2706 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2707 (fun s -> state.path <- s)
2708 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2710 let path =
2711 if String.length state.path = 0
2712 then (prerr_endline "filename missing"; exit 1)
2713 else (
2714 if Filename.is_relative state.path
2715 then Filename.concat (Sys.getcwd ()) state.path
2716 else state.path
2719 state.path <- path;
2721 State.load ();
2723 let _ = Glut.init Sys.argv in
2724 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2725 let () = Glut.initWindowSize conf.winw conf.winh in
2726 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2728 let csock, ssock =
2729 if Sys.os_type = "Unix"
2730 then
2731 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2732 else
2733 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2734 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2735 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2736 Unix.bind sock addr;
2737 Unix.listen sock 1;
2738 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2739 Unix.connect csock addr;
2740 let ssock, _ = Unix.accept sock in
2741 Unix.close sock;
2742 let opts sock =
2743 Unix.setsockopt sock Unix.TCP_NODELAY true;
2744 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2746 opts ssock;
2747 opts csock;
2748 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2749 ssock, csock
2752 let () = Glut.displayFunc display in
2753 let () = Glut.reshapeFunc reshape in
2754 let () = Glut.keyboardFunc keyboard in
2755 let () = Glut.specialFunc special in
2756 let () = Glut.idleFunc (Some idle) in
2757 let () = Glut.mouseFunc mouse in
2758 let () = Glut.motionFunc motion in
2759 let () = Glut.passiveMotionFunc pmotion in
2761 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2762 state.csock <- csock;
2763 state.ssock <- ssock;
2764 state.text <- "Opening " ^ path;
2765 writeopen state.path state.password;
2767 at_exit State.save;
2769 let rec handlelablglutbug () =
2771 Glut.mainLoop ();
2772 with Glut.BadEnum "key in special_of_int" ->
2773 showtext '!' " LablGlut bug: special key not recognized";
2774 handlelablglutbug ()
2776 handlelablglutbug ();