Fix repositioning when changing interpagespace
[llpp.git] / main.ml
blobc91b4e70ce9dcb4c34aa9c31fb5390065f361b34
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let log fmt = Printf.kprintf prerr_endline fmt;;
9 let dolog fmt = Printf.kprintf prerr_endline fmt;;
11 type params = angle * proportional * texcount * sliceheight
12 and pageno = int
13 and width = int
14 and height = int
15 and leftx = int
16 and opaque = string
17 and recttype = int
18 and pixmapsize = int
19 and angle = int
20 and proportional = bool
21 and interpagespace = int
22 and texcount = int
23 and sliceheight = int
24 and gen = int
27 external init : Unix.file_descr -> params -> unit = "ml_init";;
28 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
29 external seltext : string -> (int * int * int * int) -> int -> unit =
30 "ml_seltext";;
31 external copysel : string -> unit = "ml_copysel";;
32 external getpdimrect : int -> float array = "ml_getpdimrect";;
33 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
34 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 type mpos = int * int
37 and mstate =
38 | Msel of (mpos * mpos)
39 | Mpan of mpos
40 | Mscroll
41 | Mnone
44 type 'a circbuf =
45 { store : 'a array
46 ; mutable rc : int
47 ; mutable wc : int
48 ; mutable len : int
52 type textentry = (char * string * onhist * onkey * ondone)
53 and onkey = string -> int -> te
54 and ondone = string -> unit
55 and histcancel = unit -> unit
56 and onhist = ((histcmd -> string) * histcancel) option
57 and histcmd = HCnext | HCprev | HCfirst | HClast
58 and te =
59 | TEstop
60 | TEdone of string
61 | TEcont of string
62 | TEswitch of textentry
65 let cbnew n v =
66 { store = Array.create n v
67 ; rc = 0
68 ; wc = 0
69 ; len = 0
73 let cbcap b = Array.length b.store;;
75 let cbput b v =
76 let cap = cbcap b in
77 b.store.(b.wc) <- v;
78 b.wc <- (b.wc + 1) mod cap;
79 b.rc <- b.wc;
80 b.len <- min (b.len + 1) cap;
83 let cbempty b = b.len = 0;;
85 let cbgetg b circular dir =
86 if cbempty b
87 then b.store.(0)
88 else
89 let rc = b.rc + dir in
90 let rc =
91 if circular
92 then (
93 if rc = -1
94 then b.len-1
95 else (
96 if rc = b.len
97 then 0
98 else rc
101 else max 0 (min rc (b.len-1))
103 b.rc <- rc;
104 b.store.(rc);
107 let cbget b = cbgetg b false;;
108 let cbgetc b = cbgetg b true;;
110 let cbpeek b =
111 let rc = b.wc - b.len in
112 let rc = if rc < 0 then cbcap b + rc else rc in
113 b.store.(rc);
116 let cbdecr b = b.len <- b.len - 1;;
118 type layout =
119 { pageno : int
120 ; pagedimno : int
121 ; pagew : int
122 ; pageh : int
123 ; pagedispy : int
124 ; pagey : int
125 ; pagevh : int
126 ; pagex : int
130 type conf =
131 { mutable scrollw : int
132 ; mutable scrollh : int
133 ; mutable icase : bool
134 ; mutable preload : bool
135 ; mutable pagebias : int
136 ; mutable verbose : bool
137 ; mutable scrollincr : int
138 ; mutable maxhfit : bool
139 ; mutable crophack : bool
140 ; mutable autoscroll : bool
141 ; mutable showall : bool
142 ; mutable hlinks : bool
143 ; mutable underinfo : bool
144 ; mutable interpagespace : interpagespace
145 ; mutable zoom : float
146 ; mutable presentation : bool
147 ; mutable angle : angle
148 ; mutable winw : int
149 ; mutable winh : int
150 ; mutable savebmarks : bool
151 ; mutable proportional : proportional
152 ; mutable memlimit : int
153 ; mutable texcount : texcount
154 ; mutable sliceheight : sliceheight
158 type outline = string * int * int * float;;
159 type outlines =
160 | Oarray of outline array
161 | Olist of outline list
162 | Onarrow of string * outline array * outline array
165 type rect = (float * float * float * float * float * float * float * float);;
167 type pagemapkey = (pageno * width * angle * proportional * gen);;
169 type state =
170 { mutable csock : Unix.file_descr
171 ; mutable ssock : Unix.file_descr
172 ; mutable w : int
173 ; mutable x : int
174 ; mutable y : int
175 ; mutable maxy : int
176 ; mutable layout : layout list
177 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
178 ; mutable pdims : (pageno * width * height * leftx) list
179 ; mutable pagecount : int
180 ; pagecache : string circbuf
181 ; mutable rendering : bool
182 ; mutable mstate : mstate
183 ; mutable searchpattern : string
184 ; mutable rects : (pageno * recttype * rect) list
185 ; mutable rects1 : (pageno * recttype * rect) list
186 ; mutable text : string
187 ; mutable fullscreen : (width * height) option
188 ; mutable birdseye : (conf * leftx) option
189 ; mutable textentry : textentry option
190 ; mutable outlines : outlines
191 ; mutable outline : (bool * int * int * outline array * string) option
192 ; mutable bookmarks : outline list
193 ; mutable path : string
194 ; mutable password : string
195 ; mutable invalidated : int
196 ; mutable colorscale : float
197 ; mutable memused : int
198 ; mutable birdseyepageno : pageno
199 ; mutable gen : gen
200 ; mutable throttle : layout list option
201 ; hists : hists
203 and hists =
204 { pat : string circbuf
205 ; pag : string circbuf
206 ; nav : float circbuf
210 let defconf =
211 { scrollw = 7
212 ; scrollh = 12
213 ; icase = true
214 ; preload = true
215 ; pagebias = 0
216 ; verbose = false
217 ; scrollincr = 24
218 ; maxhfit = true
219 ; crophack = false
220 ; autoscroll = false
221 ; showall = false
222 ; hlinks = false
223 ; underinfo = false
224 ; interpagespace = 2
225 ; zoom = 1.0
226 ; presentation = false
227 ; angle = 0
228 ; winw = 900
229 ; winh = 900
230 ; savebmarks = true
231 ; proportional = true
232 ; memlimit = 32*1024*1024
233 ; texcount = 256
234 ; sliceheight = 24
238 let conf = { defconf with angle = defconf.angle };;
240 let state =
241 { csock = Unix.stdin
242 ; ssock = Unix.stdin
243 ; w = 0
244 ; y = 0
245 ; x = 0
246 ; layout = []
247 ; maxy = max_int
248 ; pagemap = Hashtbl.create 10
249 ; pagecache = cbnew 100 ""
250 ; pdims = []
251 ; pagecount = 0
252 ; rendering = false
253 ; mstate = Mnone
254 ; rects = []
255 ; rects1 = []
256 ; text = ""
257 ; fullscreen = None
258 ; birdseye = None
259 ; textentry = None
260 ; searchpattern = ""
261 ; outlines = Olist []
262 ; outline = None
263 ; bookmarks = []
264 ; path = ""
265 ; password = ""
266 ; invalidated = 0
267 ; hists =
268 { nav = cbnew 100 0.0
269 ; pat = cbnew 20 ""
270 ; pag = cbnew 10 ""
272 ; colorscale = 1.0
273 ; memused = 0
274 ; birdseyepageno = 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 if conf.preload
549 then
550 let oktopreload =
551 let memleft = conf.memlimit - state.memused in
552 if memleft < 0
553 then
554 let opaque = cbpeek state.pagecache in
555 match findpageforopaque opaque with
556 | Some ((n, _, _, _, _), size) ->
557 memleft + size >= 0 && not (pagevisible state.layout n)
558 | None -> false
559 else true
561 if oktopreload
562 then
563 let rely = yratio state.y in
564 let presentation = conf.presentation in
565 let interpagespace = conf.interpagespace in
566 let maxy = state.maxy in
567 conf.presentation <- false;
568 conf.interpagespace <- 0;
569 state.maxy <- calcheight ();
570 let y = truncate (float state.maxy *. rely) in
571 let y = if y < conf.winh then 0 else y - conf.winh in
572 let pages = layout y (conf.winh*3) in
573 List.iter render pages;
574 conf.presentation <- presentation;
575 conf.interpagespace <- interpagespace;
576 state.maxy <- maxy;
579 let gotoy y =
580 let y = max 0 y in
581 let y = min state.maxy y in
582 let pages = layout y conf.winh in
583 let ready = loadlayout pages in
584 if conf.showall
585 then (
586 if ready
587 then (
588 state.y <- y;
589 state.layout <- pages;
590 state.throttle <- None;
591 Glut.postRedisplay ();
593 else (
594 state.throttle <- Some pages;
597 else (
598 state.y <- y;
599 state.layout <- pages;
600 state.throttle <- None;
601 Glut.postRedisplay ();
603 if state.birdseye <> None
604 then (
605 if not (pagevisible pages state.birdseyepageno)
606 then
607 match state.layout with
608 | [] -> ()
609 | l :: _ -> state.birdseyepageno <- l.pageno
611 preload ();
614 let gotoy_and_clear_text y =
615 gotoy y;
616 if not conf.verbose then state.text <- "";
619 let addnav () =
620 cbput state.hists.nav (yratio state.y);
623 let getnav () =
624 let y = cbgetc state.hists.nav ~-1 in
625 truncate (y *. float state.maxy)
628 let gotopage n top =
629 let y, h = getpageyh n in
630 addnav ();
631 gotoy_and_clear_text (y + (truncate (top *. float h)));
634 let gotopage1 n top =
635 let y = getpagey n in
636 addnav ();
637 gotoy_and_clear_text (y + top);
640 let invalidate () =
641 state.layout <- [];
642 state.pdims <- [];
643 state.rects <- [];
644 state.rects1 <- [];
645 state.invalidated <- state.invalidated + 1;
648 let scalecolor c =
649 let c = c *. state.colorscale in
650 (c, c, c);
653 let represent () =
654 let y =
655 match state.layout with
656 | [] ->
657 let rely = yratio state.y in
658 state.maxy <- calcheight ();
659 truncate (float state.maxy *. rely)
661 | l :: _ ->
662 state.maxy <- calcheight ();
663 getpagey l.pageno
665 gotoy y
668 let pagematrix () =
669 GlMat.mode `projection;
670 GlMat.load_identity ();
671 GlMat.rotate ~x:1.0 ~angle:180.0 ();
672 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
673 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
676 let winmatrix () =
677 GlMat.mode `projection;
678 GlMat.load_identity ();
679 GlMat.rotate ~x:1.0 ~angle:180.0 ();
680 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
681 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
684 let reshape ~w ~h =
685 conf.winw <- w;
686 let w = truncate (float w *. conf.zoom) - conf.scrollw in
687 let w = max w 2 in
688 state.w <- w;
689 conf.winh <- h;
690 GlMat.mode `modelview;
691 GlMat.load_identity ();
692 GlClear.color (scalecolor 1.0);
693 GlClear.clear [`color];
695 invalidate ();
696 wcmd "geometry" [`i w; `i h];
699 let showtext c s =
700 GlDraw.color (0.0, 0.0, 0.0);
701 GlDraw.rect
702 (0.0, float (conf.winh - 18))
703 (float (conf.winw - conf.scrollw - 1), float conf.winh)
705 let font = Glut.BITMAP_8_BY_13 in
706 GlDraw.color (1.0, 1.0, 1.0);
707 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
708 Glut.bitmapCharacter ~font ~c:(Char.code c);
709 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
712 let enttext () =
713 let len = String.length state.text in
714 match state.textentry with
715 | None ->
716 if len > 0 then showtext ' ' state.text
718 | Some (c, text, _, _, _) ->
719 let s =
720 if len > 0
721 then
722 text ^ " [" ^ state.text ^ "]"
723 else
724 text
726 showtext c s;
729 let showtext c s =
730 if true
731 then (
732 state.text <- Printf.sprintf "%c%s" c s;
733 Glut.postRedisplay ();
735 else (
736 showtext c s;
737 Glut.swapBuffers ();
741 let act cmd =
742 match cmd.[0] with
743 | 'c' ->
744 state.pdims <- [];
746 | 'D' ->
747 state.rects <- state.rects1;
748 Glut.postRedisplay ()
750 | 'C' ->
751 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
752 state.pagecount <- n;
753 state.invalidated <- state.invalidated - 1;
754 if state.invalidated = 0
755 then represent ()
757 | 't' ->
758 let s = Scanf.sscanf cmd "t %n"
759 (fun n -> String.sub cmd n (String.length cmd - n))
761 Glut.setWindowTitle s
763 | 'T' ->
764 let s = Scanf.sscanf cmd "T %n"
765 (fun n -> String.sub cmd n (String.length cmd - n))
767 if state.textentry = None
768 then (
769 state.text <- s;
770 showtext ' ' s;
772 else (
773 state.text <- s;
774 Glut.postRedisplay ();
777 | 'V' ->
778 if conf.verbose
779 then
780 let s = Scanf.sscanf cmd "V %n"
781 (fun n -> String.sub cmd n (String.length cmd - n))
783 state.text <- s;
784 showtext ' ' s;
786 | 'F' ->
787 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
788 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
789 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
790 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
792 let y = (getpagey pageno) + truncate y0 in
793 addnav ();
794 gotoy y;
795 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
797 | 'R' ->
798 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
799 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
800 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
801 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
803 state.rects1 <-
804 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
806 | 'r' ->
807 let n, w, h, r, l, s, p =
808 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
809 (fun n w h r l s p ->
810 (n-1, w, h, r, l != 0, s, p))
813 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
814 state.memused <- state.memused + s;
816 let layout =
817 match state.throttle with
818 | None -> state.layout
819 | Some layout -> layout
822 let rec gc () =
823 if (state.memused <= conf.memlimit) || cbempty state.pagecache
824 then ()
825 else (
826 let evictedopaque = cbpeek state.pagecache in
827 match findpageforopaque evictedopaque with
828 | None -> failwith "bug in gc"
829 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
830 if state.gen != gen || not (pagevisible layout evictedn)
831 then (
832 wcmd "free" [`s evictedopaque];
833 state.memused <- state.memused - evictedsize;
834 Hashtbl.remove state.pagemap k;
835 cbdecr state.pagecache;
836 gc ();
840 gc ();
842 cbput state.pagecache p;
843 state.rendering <- false;
845 begin match state.throttle with
846 | None ->
847 if pagevisible state.layout n
848 then gotoy state.y
849 else (
850 let allvisible = loadlayout state.layout in
851 if allvisible then preload ();
854 | Some layout ->
855 match layout with
856 | [] -> ()
857 | l :: _ ->
858 let y = getpagey l.pageno + l.pagey in
859 gotoy y
862 | 'l' ->
863 let (n, w, h, x) as pdim =
864 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
866 state.pdims <- pdim :: state.pdims
868 | 'o' ->
869 let (l, n, t, h, pos) =
870 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
872 let s = String.sub cmd pos (String.length cmd - pos) in
873 let s =
874 let l = String.length s in
875 let b = Buffer.create (String.length s) in
876 let rec loop pc2 i =
877 if i = l
878 then ()
879 else
880 let pc2 =
881 match s.[i] with
882 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
883 | '\xc2' -> true
884 | c ->
885 let c = if Char.code c land 0x80 = 0 then c else '?' in
886 Buffer.add_char b c;
887 false
889 loop pc2 (i+1)
891 loop false 0;
892 Buffer.contents b
894 let outline = (s, l, n, float t /. float h) in
895 let outlines =
896 match state.outlines with
897 | Olist outlines -> Olist (outline :: outlines)
898 | Oarray _ -> Olist [outline]
899 | Onarrow _ -> Olist [outline]
901 state.outlines <- outlines
903 | _ ->
904 log "unknown cmd `%S'" cmd
907 let now = Unix.gettimeofday;;
909 let idle () =
910 let rec loop delay =
911 let r, _, _ = Unix.select [state.csock] [] [] delay in
912 begin match r with
913 | [] ->
914 if conf.autoscroll
915 then begin
916 let y = state.y + conf.scrollincr in
917 let y = if y >= state.maxy then 0 else y in
918 gotoy y;
919 state.text <- "";
920 end;
922 | _ ->
923 let cmd = readcmd state.csock in
924 act cmd;
925 loop 0.0
926 end;
927 in loop 0.001
930 let onhist cb =
931 let rc = cb.rc in
932 let action = function
933 | HCprev -> cbget cb ~-1
934 | HCnext -> cbget cb 1
935 | HCfirst -> cbget cb ~-(cb.rc)
936 | HClast -> cbget cb (cb.len - 1 - cb.rc)
937 and cancel () = cb.rc <- rc
938 in (action, cancel)
941 let search pattern forward =
942 if String.length pattern > 0
943 then
944 let pn, py =
945 match state.layout with
946 | [] -> 0, 0
947 | l :: _ ->
948 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
950 let cmd =
951 let b = makecmd "search"
952 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
954 Buffer.add_char b ',';
955 Buffer.add_string b pattern;
956 Buffer.add_char b '\000';
957 Buffer.contents b;
959 writecmd state.csock cmd;
962 let intentry text key =
963 let c = Char.unsafe_chr key in
964 match c with
965 | '0' .. '9' ->
966 let s = "x" in s.[0] <- c;
967 let text = text ^ s in
968 TEcont text
970 | _ ->
971 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
972 TEcont text
975 let addchar s c =
976 let b = Buffer.create (String.length s + 1) in
977 Buffer.add_string b s;
978 Buffer.add_char b c;
979 Buffer.contents b;
982 let textentry text key =
983 let c = Char.unsafe_chr key in
984 match c with
985 | _ when key >= 32 && key < 127 ->
986 let text = addchar text c in
987 TEcont text
989 | _ ->
990 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
991 TEcont text
994 let reinit angle proportional =
995 conf.angle <- angle;
996 conf.proportional <- proportional;
997 invalidate ();
998 wcmd "reinit" [`i angle; `b proportional];
1001 let optentry text key =
1002 let btos b = if b then "on" else "off" in
1003 let c = Char.unsafe_chr key in
1004 match c with
1005 | 's' ->
1006 let ondone s =
1007 try conf.scrollincr <- int_of_string s with exc ->
1008 state.text <- Printf.sprintf "bad integer `%s': %s"
1009 s (Printexc.to_string exc)
1011 TEswitch ('#', "", None, intentry, ondone)
1013 | 'R' ->
1014 let ondone s =
1015 match try
1016 Some (int_of_string s)
1017 with exc ->
1018 state.text <- Printf.sprintf "bad integer `%s': %s"
1019 s (Printexc.to_string exc);
1020 None
1021 with
1022 | Some angle -> reinit angle conf.proportional
1023 | None -> ()
1025 TEswitch ('^', "", None, intentry, ondone)
1027 | 'i' ->
1028 conf.icase <- not conf.icase;
1029 TEdone ("case insensitive search " ^ (btos conf.icase))
1031 | 'p' ->
1032 conf.preload <- not conf.preload;
1033 gotoy state.y;
1034 TEdone ("preload " ^ (btos conf.preload))
1036 | 'v' ->
1037 conf.verbose <- not conf.verbose;
1038 TEdone ("verbose " ^ (btos conf.verbose))
1040 | 'h' ->
1041 conf.maxhfit <- not conf.maxhfit;
1042 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1043 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1045 | 'c' ->
1046 conf.crophack <- not conf.crophack;
1047 TEdone ("crophack " ^ btos conf.crophack)
1049 | 'a' ->
1050 conf.showall <- not conf.showall;
1051 TEdone ("showall " ^ btos conf.showall)
1053 | 'f' ->
1054 conf.underinfo <- not conf.underinfo;
1055 TEdone ("underinfo " ^ btos conf.underinfo)
1057 | 'P' ->
1058 conf.savebmarks <- not conf.savebmarks;
1059 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1061 | 'S' ->
1062 let ondone s =
1064 let pageno, py =
1065 match state.layout with
1066 | [] -> 0, 0
1067 | l :: _ ->
1068 l.pageno, l.pagey
1070 conf.interpagespace <- int_of_string s;
1071 state.maxy <- calcheight ();
1072 let y = getpagey pageno in
1073 gotoy (y + py)
1074 with exc ->
1075 state.text <- Printf.sprintf "bad integer `%s': %s"
1076 s (Printexc.to_string exc)
1078 TEswitch ('%', "", None, intentry, ondone)
1080 | 'l' ->
1081 reinit conf.angle (not conf.proportional);
1082 TEdone ("proprortional display " ^ btos conf.proportional)
1084 | _ ->
1085 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1086 TEstop
1089 let maxoutlinerows () = (conf.winh - 31) / 16;;
1091 let enterselector allowdel outlines errmsg msg =
1092 if Array.length outlines = 0
1093 then (
1094 showtext ' ' errmsg;
1096 else (
1097 state.text <- msg;
1098 Glut.setCursor Glut.CURSOR_INHERIT;
1099 let pageno =
1100 match state.layout with
1101 | [] -> -1
1102 | {pageno=pageno} :: rest -> pageno
1104 let active =
1105 let rec loop n =
1106 if n = Array.length outlines
1107 then 0
1108 else
1109 let (_, _, outlinepageno, _) = outlines.(n) in
1110 if outlinepageno >= pageno then n else loop (n+1)
1112 loop 0
1114 state.outline <-
1115 Some (allowdel, active,
1116 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1117 Glut.postRedisplay ();
1121 let enteroutlinemode () =
1122 let outlines, msg =
1123 match state.outlines with
1124 | Oarray a -> a, ""
1125 | Olist l ->
1126 let a = Array.of_list (List.rev l) in
1127 state.outlines <- Oarray a;
1128 a, ""
1129 | Onarrow (pat, a, b) ->
1130 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1132 enterselector false outlines "Document has no outline" msg;
1135 let enterbookmarkmode () =
1136 let bookmarks = Array.of_list state.bookmarks in
1137 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1140 let quickbookmark ?title () =
1141 match state.layout with
1142 | [] -> ()
1143 | l :: _ ->
1144 let title =
1145 match title with
1146 | None ->
1147 let sec = Unix.gettimeofday () in
1148 let tm = Unix.localtime sec in
1149 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1150 (l.pageno+1)
1151 tm.Unix.tm_mday
1152 tm.Unix.tm_mon
1153 (tm.Unix.tm_year + 1900)
1154 tm.Unix.tm_hour
1155 tm.Unix.tm_min
1156 | Some title -> title
1158 state.bookmarks <-
1159 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1162 let doreshape w h =
1163 state.fullscreen <- None;
1164 Glut.reshapeWindow w h;
1167 let writeopen path password =
1168 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1171 let opendoc path password =
1172 invalidate ();
1173 state.path <- path;
1174 state.password <- password;
1175 state.gen <- state.gen + 1;
1177 writeopen path password;
1178 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1179 wcmd "geometry" [`i state.w; `i conf.winh];
1182 let birdseyeoff (c, leftx) =
1183 state.birdseye <- None;
1184 conf.zoom <- c.zoom;
1185 conf.presentation <- c.presentation;
1186 conf.interpagespace <- c.interpagespace;
1187 conf.showall <- c.showall;
1188 conf.hlinks <- c.hlinks;
1189 state.x <- leftx;
1190 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1191 (100.0*.conf.zoom);
1194 let viewkeyboard ~key ~x ~y =
1195 let enttext te =
1196 state.textentry <- te;
1197 state.text <- "";
1198 enttext ();
1199 Glut.postRedisplay ()
1201 match state.textentry with
1202 | None ->
1203 let c = Char.chr key in
1204 begin match c with
1205 | '\027' | 'q' ->
1206 exit 0
1208 | '\008' ->
1209 let y = getnav () in
1210 gotoy_and_clear_text y
1212 | '\013' ->
1213 begin match state.birdseye with
1214 | None -> ()
1215 | Some vals ->
1216 let y = getpagey state.birdseyepageno in
1217 state.y <- y;
1218 birdseyeoff vals;
1219 reshape conf.winw conf.winh;
1220 end;
1222 | 'o' ->
1223 enteroutlinemode ()
1225 | 'u' ->
1226 state.rects <- [];
1227 state.text <- "";
1228 Glut.postRedisplay ()
1230 | '/' | '?' ->
1231 let ondone isforw s =
1232 cbput state.hists.pat s;
1233 state.searchpattern <- s;
1234 search s isforw
1236 enttext (Some (c, "", Some (onhist state.hists.pat),
1237 textentry, ondone (c ='/')))
1239 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1240 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1241 conf.zoom <- min 2.2 (conf.zoom +. incr);
1242 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1243 reshape conf.winw conf.winh
1245 | '+' ->
1246 let ondone s =
1247 let n =
1248 try int_of_string s with exc ->
1249 state.text <- Printf.sprintf "bad integer `%s': %s"
1250 s (Printexc.to_string exc);
1251 max_int
1253 if n != max_int
1254 then (
1255 conf.pagebias <- n;
1256 state.text <- "page bias is now " ^ string_of_int n;
1259 enttext (Some ('+', "", None, intentry, ondone))
1261 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1262 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1263 conf.zoom <- max 0.01 (conf.zoom -. decr);
1264 if conf.zoom <= 1.0 then state.x <- 0;
1265 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1266 reshape conf.winw conf.winh;
1268 | '-' ->
1269 let ondone msg =
1270 state.text <- msg;
1272 enttext (Some ('-', "", None, optentry, ondone))
1274 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1275 state.x <- 0;
1276 conf.zoom <- 1.0;
1277 state.text <- "zoom is 100%";
1278 reshape conf.winw conf.winh
1280 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1281 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1282 if zoom < 1.0
1283 then (
1284 conf.zoom <- zoom;
1285 state.x <- 0;
1286 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1287 reshape conf.winw conf.winh;
1290 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1291 begin match state.birdseye with
1292 | None ->
1293 let zoom = 76.0 /. float state.w in
1294 state.birdseye <- Some ({ conf with zoom = conf.zoom }, state.x);
1295 conf.zoom <- zoom;
1296 conf.presentation <- false;
1297 conf.interpagespace <- 10;
1298 conf.hlinks <- false;
1299 state.x <- 0;
1300 state.mstate <- Mnone;
1301 conf.showall <- false;
1302 Glut.setCursor Glut.CURSOR_INHERIT;
1303 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1304 (100.0*.zoom)
1306 | Some vals ->
1307 birdseyeoff vals;
1308 end;
1309 reshape conf.winw conf.winh
1311 | '0' .. '9' ->
1312 let ondone s =
1313 let n =
1314 try int_of_string s with exc ->
1315 state.text <- Printf.sprintf "bad integer `%s': %s"
1316 s (Printexc.to_string exc);
1319 if n >= 0
1320 then (
1321 addnav ();
1322 cbput state.hists.pag (string_of_int n);
1323 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1326 let pageentry text key =
1327 match Char.unsafe_chr key with
1328 | 'g' -> TEdone text
1329 | _ -> intentry text key
1331 let text = "x" in text.[0] <- c;
1332 enttext (Some (':', text, Some (onhist state.hists.pag),
1333 pageentry, ondone))
1335 | 'b' ->
1336 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1337 reshape conf.winw conf.winh;
1339 | 'l' ->
1340 conf.hlinks <- not conf.hlinks;
1341 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1342 Glut.postRedisplay ()
1344 | 'a' ->
1345 conf.autoscroll <- not conf.autoscroll
1347 | 'P' ->
1348 conf.presentation <- not conf.presentation;
1349 showtext ' ' ("presentation mode " ^
1350 if conf.presentation then "on" else "off");
1351 represent ()
1353 | 'f' ->
1354 begin match state.fullscreen with
1355 | None ->
1356 state.fullscreen <- Some (conf.winw, conf.winh);
1357 Glut.fullScreen ()
1358 | Some (w, h) ->
1359 state.fullscreen <- None;
1360 doreshape w h
1363 | 'g' ->
1364 gotoy_and_clear_text 0
1366 | 'n' ->
1367 search state.searchpattern true
1369 | 'p' | 'N' ->
1370 search state.searchpattern false
1372 | 't' ->
1373 begin match state.layout with
1374 | [] -> ()
1375 | l :: _ ->
1376 gotoy_and_clear_text (getpagey l.pageno)
1379 | ' ' ->
1380 begin match List.rev state.layout with
1381 | [] -> ()
1382 | l :: _ ->
1383 let pageno = min (l.pageno+1) (state.pagecount-1) in
1384 gotoy_and_clear_text (getpagey pageno)
1387 | '\127' ->
1388 begin match state.layout with
1389 | [] -> ()
1390 | l :: _ ->
1391 let pageno = max 0 (l.pageno-1) in
1392 gotoy_and_clear_text (getpagey pageno)
1395 | '=' ->
1396 let f (fn, ln) l =
1397 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1399 let fn, ln = List.fold_left f (-1, -1) state.layout in
1400 let s =
1401 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1402 let percent =
1403 if maxy <= 0
1404 then 100.
1405 else (100. *. (float state.y /. float maxy)) in
1406 if fn = ln
1407 then
1408 Printf.sprintf "Page %d of %d %.2f%%"
1409 (fn+1) state.pagecount percent
1410 else
1411 Printf.sprintf
1412 "Pages %d-%d of %d %.2f%%"
1413 (fn+1) (ln+1) state.pagecount percent
1415 showtext ' ' s;
1417 | 'w' ->
1418 begin match state.layout with
1419 | [] -> ()
1420 | l :: _ ->
1421 doreshape (l.pagew + conf.scrollw) l.pageh;
1422 Glut.postRedisplay ();
1425 | '\'' ->
1426 enterbookmarkmode ()
1428 | 'm' ->
1429 let ondone s =
1430 match state.layout with
1431 | l :: _ ->
1432 state.bookmarks <-
1433 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1434 :: state.bookmarks
1435 | _ -> ()
1437 enttext (Some ('~', "", None, textentry, ondone))
1439 | '~' ->
1440 quickbookmark ();
1441 showtext ' ' "Quick bookmark added";
1443 | 'z' ->
1444 begin match state.layout with
1445 | l :: _ ->
1446 let rect = getpdimrect l.pagedimno in
1447 let w, h =
1448 if conf.crophack
1449 then
1450 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1451 truncate (1.2 *. (rect.(3) -. rect.(0))))
1452 else
1453 (truncate (rect.(1) -. rect.(0)),
1454 truncate (rect.(3) -. rect.(0)))
1456 if w != 0 && h != 0
1457 then
1458 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1460 Glut.postRedisplay ();
1462 | [] -> ()
1465 | '<' | '>' ->
1466 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1468 | '[' | ']' ->
1469 state.colorscale <-
1470 max 0.0
1471 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1472 Glut.postRedisplay ()
1474 | 'k' -> gotoy (clamp (-conf.scrollincr))
1475 | 'j' -> gotoy (clamp conf.scrollincr)
1477 | 'r' -> opendoc state.path state.password
1479 | _ ->
1480 vlog "huh? %d %c" key (Char.chr key);
1483 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1484 let len = String.length text in
1485 if len = 0
1486 then (
1487 state.textentry <- None;
1488 Glut.postRedisplay ();
1490 else (
1491 let s = String.sub text 0 (len - 1) in
1492 enttext (Some (c, s, opthist, onkey, ondone))
1495 | Some (c, text, onhist, onkey, ondone) ->
1496 begin match Char.unsafe_chr key with
1497 | '\r' | '\n' ->
1498 ondone text;
1499 state.textentry <- None;
1500 Glut.postRedisplay ()
1502 | '\027' ->
1503 begin match onhist with
1504 | None -> ()
1505 | Some (_, onhistcancel) -> onhistcancel ()
1506 end;
1507 state.textentry <- None;
1508 Glut.postRedisplay ()
1510 | _ ->
1511 begin match onkey text key with
1512 | TEdone text ->
1513 state.textentry <- None;
1514 ondone text;
1515 Glut.postRedisplay ()
1517 | TEcont text ->
1518 enttext (Some (c, text, onhist, onkey, ondone));
1520 | TEstop ->
1521 state.textentry <- None;
1522 Glut.postRedisplay ()
1524 | TEswitch te ->
1525 state.textentry <- Some te;
1526 Glut.postRedisplay ()
1527 end;
1528 end;
1531 let narrow outlines pattern =
1532 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1533 match reopt with
1534 | None -> None
1535 | Some re ->
1536 let rec fold accu n =
1537 if n = -1
1538 then accu
1539 else
1540 let (s, _, _, _) as o = outlines.(n) in
1541 let accu =
1542 if (try ignore (Str.search_forward re s 0); true
1543 with Not_found -> false)
1544 then (o :: accu)
1545 else accu
1547 fold accu (n-1)
1549 let matched = fold [] (Array.length outlines - 1) in
1550 if matched = [] then None else Some (Array.of_list matched)
1553 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1554 let search active pattern incr =
1555 let dosearch re =
1556 let rec loop n =
1557 if n = Array.length outlines || n = -1
1558 then None
1559 else
1560 let (s, _, _, _) = outlines.(n) in
1562 (try ignore (Str.search_forward re s 0); true
1563 with Not_found -> false)
1564 then Some n
1565 else loop (n + incr)
1567 loop active
1570 let re = Str.regexp_case_fold pattern in
1571 dosearch re
1572 with Failure s ->
1573 state.text <- s;
1574 None
1576 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1577 match key with
1578 | 27 ->
1579 if String.length qsearch = 0
1580 then (
1581 state.text <- "";
1582 state.outline <- None;
1583 Glut.postRedisplay ();
1585 else (
1586 state.text <- "";
1587 state.outline <- Some (allowdel, active, first, outlines, "");
1588 Glut.postRedisplay ();
1591 | 18 | 19 ->
1592 let incr = if key = 18 then -1 else 1 in
1593 let active, first =
1594 match search (active + incr) qsearch incr with
1595 | None ->
1596 state.text <- qsearch ^ " [not found]";
1597 active, first
1598 | Some active ->
1599 state.text <- qsearch;
1600 active, firstof active
1602 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1603 Glut.postRedisplay ();
1605 | 8 ->
1606 let len = String.length qsearch in
1607 if len = 0
1608 then ()
1609 else (
1610 if len = 1
1611 then (
1612 state.text <- "";
1613 state.outline <- Some (allowdel, active, first, outlines, "");
1615 else
1616 let qsearch = String.sub qsearch 0 (len - 1) in
1617 let active, first =
1618 match search active qsearch ~-1 with
1619 | None ->
1620 state.text <- qsearch ^ " [not found]";
1621 active, first
1622 | Some active ->
1623 state.text <- qsearch;
1624 active, firstof active
1626 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1628 Glut.postRedisplay ()
1630 | 13 ->
1631 if active < Array.length outlines
1632 then (
1633 let (_, _, n, t) = outlines.(active) in
1634 gotopage n t;
1636 state.text <- "";
1637 if allowdel then state.bookmarks <- Array.to_list outlines;
1638 state.outline <- None;
1639 Glut.postRedisplay ();
1641 | _ when key >= 32 && key < 127 ->
1642 let pattern = addchar qsearch (Char.chr key) in
1643 let active, first =
1644 match search active pattern 1 with
1645 | None ->
1646 state.text <- pattern ^ " [not found]";
1647 active, first
1648 | Some active ->
1649 state.text <- pattern;
1650 active, firstof active
1652 state.outline <- Some (allowdel, active, first, outlines, pattern);
1653 Glut.postRedisplay ()
1655 | 14 when not allowdel -> (* ctrl-n *)
1656 if String.length qsearch > 0
1657 then (
1658 let optoutlines = narrow outlines qsearch in
1659 begin match optoutlines with
1660 | None -> state.text <- "can't narrow"
1661 | Some outlines ->
1662 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1663 match state.outlines with
1664 | Olist l -> ()
1665 | Oarray a ->
1666 state.outlines <- Onarrow (qsearch, outlines, a)
1667 | Onarrow (pat, a, b) ->
1668 state.outlines <- Onarrow (qsearch, outlines, b)
1669 end;
1671 Glut.postRedisplay ()
1673 | 21 when not allowdel -> (* ctrl-u *)
1674 let outline =
1675 match state.outlines with
1676 | Oarray a -> a
1677 | Olist l ->
1678 let a = Array.of_list (List.rev l) in
1679 state.outlines <- Oarray a;
1681 | Onarrow (pat, a, b) ->
1682 state.outlines <- Oarray b;
1683 state.text <- "";
1686 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1687 Glut.postRedisplay ()
1689 | 12 ->
1690 state.outline <-
1691 Some (allowdel, active, firstof active, outlines, qsearch);
1692 Glut.postRedisplay ()
1694 | 127 when allowdel ->
1695 let len = Array.length outlines - 1 in
1696 if len = 0
1697 then (
1698 state.outline <- None;
1699 state.bookmarks <- [];
1701 else (
1702 let bookmarks = Array.init len
1703 (fun i ->
1704 let i = if i >= active then i + 1 else i in
1705 outlines.(i)
1708 state.outline <-
1709 Some (allowdel,
1710 min active (len-1),
1711 min first (len-1),
1712 bookmarks, qsearch)
1715 Glut.postRedisplay ()
1717 | _ -> log "unknown key %d" key
1720 let keyboard ~key ~x ~y =
1721 if key = 7
1722 then
1723 wcmd "interrupt" []
1724 else
1725 match state.outline with
1726 | None -> viewkeyboard ~key ~x ~y
1727 | Some outline -> outlinekeyboard ~key ~x ~y outline
1730 let special ~key ~x ~y =
1731 match state.outline with
1732 | None when state.birdseye <> None ->
1733 begin match key with
1734 | Glut.KEY_UP ->
1735 let pageno = max 0 (state.birdseyepageno - 1) in
1736 state.birdseyepageno <- pageno;
1737 if not (pagevisible state.layout pageno)
1738 then gotopage pageno 0.0
1739 else Glut.postRedisplay ();
1741 | Glut.KEY_DOWN ->
1742 let pageno = min (state.pagecount - 1) (state.birdseyepageno + 1) in
1743 state.birdseyepageno <- pageno;
1744 if not (pagevisible state.layout pageno)
1745 then
1746 begin match List.rev state.layout with
1747 | [] -> gotopage pageno 0.0
1748 | l :: _ ->
1749 gotoy (state.y + conf.interpagespace + l.pageh*2 - l.pagevh)
1751 else Glut.postRedisplay ();
1753 | Glut.KEY_PAGE_UP ->
1754 begin match state.layout with
1755 | l :: _ ->
1756 if l.pageno = state.birdseyepageno
1757 then (
1758 match layout (state.y - conf.winh) conf.winh with
1759 | [] -> gotoy (clamp (-conf.winh))
1760 | l :: _ ->
1761 state.birdseyepageno <- max 0 (l.pageno - 1);
1762 gotopage state.birdseyepageno 0.0
1764 else (
1765 state.birdseyepageno <- max 0 (l.pageno - 1);
1766 gotopage state.birdseyepageno 0.0
1768 | [] -> gotoy (clamp (-conf.winh))
1769 end;
1770 | Glut.KEY_PAGE_DOWN ->
1771 begin match List.rev state.layout with
1772 | l :: _ ->
1773 state.birdseyepageno <- min (state.pagecount - 1) (l.pageno + 1);
1774 gotoy (clamp (l.pagedispy + conf.interpagespace + l.pageh))
1775 | [] -> gotoy (clamp conf.winh)
1776 end;
1778 | Glut.KEY_HOME ->
1779 state.birdseyepageno <- 0;
1780 gotopage 0 0.0
1781 | Glut.KEY_END ->
1782 state.birdseyepageno <- state.pagecount - 1;
1783 if not (pagevisible state.layout state.birdseyepageno)
1784 then
1785 gotopage state.birdseyepageno 0.0
1786 else
1787 Glut.postRedisplay ()
1789 | _ -> ()
1792 | None ->
1793 begin match state.textentry with
1794 | None ->
1795 let y =
1796 match key with
1797 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1798 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1799 | Glut.KEY_DOWN -> clamp conf.scrollincr
1800 | Glut.KEY_PAGE_UP ->
1801 if Glut.getModifiers () land Glut.active_ctrl != 0
1802 then
1803 match state.layout with
1804 | [] -> state.y
1805 | l :: _ -> state.y - l.pagey
1806 else
1807 clamp (-conf.winh)
1808 | Glut.KEY_PAGE_DOWN ->
1809 if Glut.getModifiers () land Glut.active_ctrl != 0
1810 then
1811 match List.rev state.layout with
1812 | [] -> state.y
1813 | l :: _ -> getpagey l.pageno
1814 else
1815 clamp conf.winh
1816 | Glut.KEY_HOME -> addnav (); 0
1817 | Glut.KEY_END ->
1818 addnav ();
1819 state.maxy - (if conf.maxhfit then conf.winh else 0)
1821 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1822 state.x <- state.x - 10;
1823 state.y
1824 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1825 state.x <- state.x + 10;
1826 state.y
1828 | _ -> state.y
1830 gotoy_and_clear_text y
1832 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1833 let s =
1834 match key with
1835 | Glut.KEY_UP -> action HCprev
1836 | Glut.KEY_DOWN -> action HCnext
1837 | Glut.KEY_HOME -> action HCfirst
1838 | Glut.KEY_END -> action HClast
1839 | _ -> state.text
1841 state.textentry <- Some (c, s, onhist, onkey, ondone);
1842 Glut.postRedisplay ()
1844 | _ -> ()
1847 | Some (allowdel, active, first, outlines, qsearch) ->
1848 let maxrows = maxoutlinerows () in
1849 let calcfirst first active =
1850 if active > first
1851 then
1852 let rows = active - first in
1853 if rows > maxrows then active - maxrows else first
1854 else active
1856 let navigate incr =
1857 let active = active + incr in
1858 let active = max 0 (min active (Array.length outlines - 1)) in
1859 let first = calcfirst first active in
1860 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1861 Glut.postRedisplay ()
1863 let updownlevel incr =
1864 let len = Array.length outlines in
1865 let (_, curlevel, _, _) = outlines.(active) in
1866 let rec flow i =
1867 if i = len then i-1 else if i = -1 then 0 else
1868 let (_, l, _, _) = outlines.(i) in
1869 if l != curlevel then i else flow (i+incr)
1871 let active = flow active in
1872 let first = calcfirst first active in
1873 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1874 Glut.postRedisplay ()
1876 match key with
1877 | Glut.KEY_UP -> navigate ~-1
1878 | Glut.KEY_DOWN -> navigate 1
1879 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1880 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1882 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1883 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1885 | Glut.KEY_HOME ->
1886 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1887 Glut.postRedisplay ()
1889 | Glut.KEY_END ->
1890 let active = Array.length outlines - 1 in
1891 let first = max 0 (active - maxrows) in
1892 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1893 Glut.postRedisplay ()
1895 | _ -> ()
1898 let drawplaceholder l =
1899 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1900 GlDraw.color (scalecolor 1.0);
1901 GlDraw.rect
1902 (float l.pagex, float l.pagedispy)
1903 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1905 let x = float (if margin < 0 then -margin else 0)
1906 and y = float (l.pagedispy + 13) in
1907 let font = Glut.BITMAP_8_BY_13 in
1908 GlDraw.color (0.0, 0.0, 0.0);
1909 GlPix.raster_pos ~x ~y ();
1910 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1911 ("Loading " ^ string_of_int (l.pageno + 1));
1914 let now () = Unix.gettimeofday ();;
1916 let drawpage l =
1917 begin match getopaque l.pageno with
1918 | Some (opaque, _) when validopaque opaque ->
1919 if state.textentry = None
1920 then GlDraw.color (scalecolor 1.0)
1921 else GlDraw.color (scalecolor 0.4);
1922 let a = now () in
1923 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1924 opaque;
1925 let b = now () in
1926 let d = b-.a in
1927 vlog "draw %d %f sec" l.pageno d;
1929 | _ ->
1930 drawplaceholder l;
1931 end;
1932 if state.birdseye <> None && state.birdseyepageno = l.pageno
1933 then (
1934 GlDraw.polygon_mode `both `line;
1935 GlDraw.line_width 4.0;
1936 GlDraw.color (0.8, 0.0, 0.0);
1937 GlDraw.rect
1938 (float (l.pagex - 1), float (l.pagedispy - 1))
1939 (float (l.pagew + l.pagex + 1), float (l.pagedispy + l.pagevh + 1))
1941 GlDraw.line_width 1.0;
1942 GlDraw.polygon_mode `both `fill;
1946 let scrollph y =
1947 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1948 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1949 let sh = float conf.winh /. sh in
1950 let sh = max sh (float conf.scrollh) in
1952 let percent =
1953 if state.y = state.maxy
1954 then 1.0
1955 else float y /. float maxy
1957 let position = (float conf.winh -. sh) *. percent in
1959 let position =
1960 if position +. sh > float conf.winh
1961 then float conf.winh -. sh
1962 else position
1964 position, sh;
1967 let scrollindicator () =
1968 GlDraw.color (0.64 , 0.64, 0.64);
1969 GlDraw.rect
1970 (float (conf.winw - conf.scrollw), 0.)
1971 (float conf.winw, float conf.winh)
1973 GlDraw.color (0.0, 0.0, 0.0);
1975 let position, sh = scrollph state.y in
1976 GlDraw.rect
1977 (float (conf.winw - conf.scrollw), position)
1978 (float conf.winw, position +. sh)
1982 let showsel margin =
1983 match state.mstate with
1984 | Mnone | Mscroll _ | Mpan _ ->
1987 | Msel ((x0, y0), (x1, y1)) ->
1988 let rec loop = function
1989 | l :: ls ->
1990 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1991 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1992 then
1993 match getopaque l.pageno with
1994 | Some (opaque, _) when validopaque opaque ->
1995 let oy = -l.pagey + l.pagedispy in
1996 seltext opaque
1997 (x0 - margin - state.x, y0,
1998 x1 - margin - state.x, y1) oy;
2000 | _ -> ()
2001 else loop ls
2002 | [] -> ()
2004 loop state.layout
2007 let showrects () =
2008 let panx = float state.x in
2009 Gl.enable `blend;
2010 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2011 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2012 List.iter
2013 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2014 List.iter (fun l ->
2015 if l.pageno = pageno
2016 then (
2017 let d = float (l.pagedispy - l.pagey) in
2018 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2019 GlDraw.begins `quads;
2021 GlDraw.vertex2 (x0+.panx, y0+.d);
2022 GlDraw.vertex2 (x1+.panx, y1+.d);
2023 GlDraw.vertex2 (x2+.panx, y2+.d);
2024 GlDraw.vertex2 (x3+.panx, y3+.d);
2026 GlDraw.ends ();
2028 ) state.layout
2029 ) state.rects
2031 Gl.disable `blend;
2034 let showoutline = function
2035 | None -> ()
2036 | Some (allowdel, active, first, outlines, qsearch) ->
2037 Gl.enable `blend;
2038 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2039 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2040 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2041 Gl.disable `blend;
2043 GlDraw.color (1., 1., 1.);
2044 let font = Glut.BITMAP_9_BY_15 in
2045 let draw_string x y s =
2046 GlPix.raster_pos ~x ~y ();
2047 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2049 let rec loop row =
2050 if row = Array.length outlines || (row - first) * 16 > conf.winh
2051 then ()
2052 else (
2053 let (s, l, _, _) = outlines.(row) in
2054 let y = (row - first) * 16 in
2055 let x = 5 + 15*l in
2056 if row = active
2057 then (
2058 Gl.enable `blend;
2059 GlDraw.polygon_mode `both `line;
2060 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2061 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2062 GlDraw.rect (0., float (y + 1))
2063 (float (conf.winw - 1), float (y + 18));
2064 GlDraw.polygon_mode `both `fill;
2065 Gl.disable `blend;
2066 GlDraw.color (1., 1., 1.);
2068 draw_string (float x) (float (y + 16)) s;
2069 loop (row+1)
2072 loop first
2075 let display () =
2076 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2077 GlDraw.viewport margin 0 state.w conf.winh;
2078 pagematrix ();
2079 if state.birdseye <> None
2080 then
2081 GlClear.color (0.5, 0.5, 0.55)
2082 else
2083 GlClear.color (scalecolor 0.5)
2085 GlClear.clear [`color];
2086 if state.x != 0
2087 then (
2088 let x = float state.x in
2089 GlMat.translate ~x ();
2091 if conf.zoom > 1.0
2092 then (
2093 Gl.enable `scissor_test;
2094 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2096 List.iter drawpage state.layout;
2097 if conf.zoom > 1.0
2098 then
2099 Gl.disable `scissor_test
2101 if state.x != 0
2102 then (
2103 let x = -.float state.x in
2104 GlMat.translate ~x ();
2106 showrects ();
2107 showsel margin;
2108 GlDraw.viewport 0 0 conf.winw conf.winh;
2109 winmatrix ();
2110 scrollindicator ();
2111 showoutline state.outline;
2112 enttext ();
2113 Glut.swapBuffers ();
2116 let getunder x y =
2117 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2118 let x = x - margin - state.x in
2119 let rec f = function
2120 | l :: rest ->
2121 begin match getopaque l.pageno with
2122 | Some (opaque, _) when validopaque opaque ->
2123 let y = y - l.pagedispy in
2124 if y > 0
2125 then
2126 let y = l.pagey + y in
2127 let x = x - l.pagex in
2128 match whatsunder opaque x y with
2129 | Unone -> f rest
2130 | under -> under
2131 else
2132 f rest
2133 | _ ->
2134 f rest
2136 | [] -> Unone
2138 f state.layout
2141 let mouse ~button ~bstate ~x ~y =
2142 match button with
2143 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2144 let incr =
2145 if n = 3
2146 then
2147 -conf.scrollincr
2148 else
2149 conf.scrollincr
2151 let incr = incr * 2 in
2152 let y = clamp incr in
2153 gotoy_and_clear_text y
2155 | Glut.LEFT_BUTTON when state.outline = None
2156 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2157 if bstate = Glut.DOWN
2158 then (
2159 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2160 state.mstate <- Mpan (x, y)
2162 else
2163 state.mstate <- Mnone
2165 | Glut.LEFT_BUTTON
2166 when state.outline = None && x > conf.winw - conf.scrollw ->
2167 if bstate = Glut.DOWN
2168 then
2169 let position, sh = scrollph state.y in
2170 if y > truncate position && y < truncate (position +. sh)
2171 then
2172 state.mstate <- Mscroll
2173 else
2174 let percent = float y /. float conf.winh in
2175 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2176 gotoy desty;
2177 state.mstate <- Mscroll
2178 else
2179 state.mstate <- Mnone
2181 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2182 begin match state.birdseye with
2183 | Some vals ->
2184 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2185 let rec loop = function
2186 | [] -> ()
2187 | l :: rest ->
2188 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2189 && x > margin && x < margin + l.pagew
2190 then (
2191 let y = getpagey l.pageno in
2192 state.y <- y;
2193 birdseyeoff vals;
2194 reshape conf.winw conf.winh;
2196 else loop rest
2198 loop state.layout;
2199 | None -> () (* impossible *)
2202 | Glut.LEFT_BUTTON when state.outline = None ->
2203 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2204 begin match dest with
2205 | Ulinkgoto (pageno, top) ->
2206 if pageno >= 0
2207 then
2208 gotopage1 pageno top
2210 | Ulinkuri s ->
2211 print_endline s
2213 | Unone when bstate = Glut.DOWN ->
2214 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2215 state.mstate <- Mpan (x, y);
2217 | Unone | Utext _ ->
2218 if bstate = Glut.DOWN
2219 then (
2220 if conf.angle mod 360 = 0
2221 then (
2222 state.mstate <- Msel ((x, y), (x, y));
2223 Glut.postRedisplay ()
2226 else (
2227 match state.mstate with
2228 | Mnone -> ()
2230 | Mscroll ->
2231 state.mstate <- Mnone
2233 | Mpan _ ->
2234 Glut.setCursor Glut.CURSOR_INHERIT;
2235 state.mstate <- Mnone
2237 | Msel ((x0, y0), (x1, y1)) ->
2238 let f l =
2239 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2240 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2241 then
2242 match getopaque l.pageno with
2243 | Some (opaque, _) when validopaque opaque ->
2244 copysel opaque
2245 | _ -> ()
2247 List.iter f state.layout;
2248 copysel ""; (* ugly *)
2249 Glut.setCursor Glut.CURSOR_INHERIT;
2250 state.mstate <- Mnone;
2254 | _ ->
2257 let mouse ~button ~state ~x ~y = mouse button state x y;;
2259 let motion ~x ~y =
2260 if state.outline = None
2261 then
2262 match state.mstate with
2263 | Mnone -> ()
2265 | Mpan (x0, y0) ->
2266 let dx = x - x0
2267 and dy = y0 - y in
2268 state.mstate <- Mpan (x, y);
2269 if conf.zoom > 1.0 then state.x <- state.x + dx;
2270 let y = clamp dy in
2271 gotoy_and_clear_text y
2273 | Msel (a, _) ->
2274 state.mstate <- Msel (a, (x, y));
2275 Glut.postRedisplay ()
2277 | Mscroll ->
2278 let y = min conf.winh (max 0 y) in
2279 let percent = float y /. float conf.winh in
2280 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2281 gotoy_and_clear_text y
2284 let pmotion ~x ~y =
2285 if state.outline = None && state.birdseye = None
2286 then
2287 match state.mstate with
2288 | Mnone ->
2289 begin match getunder x y with
2290 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2291 | Ulinkuri uri ->
2292 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2293 Glut.setCursor Glut.CURSOR_INFO
2294 | Ulinkgoto (page, y) ->
2295 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
2296 Glut.setCursor Glut.CURSOR_INFO
2297 | Utext s ->
2298 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2299 Glut.setCursor Glut.CURSOR_TEXT
2302 | Mpan _ | Msel _ | Mscroll ->
2306 module State =
2307 struct
2308 open Parser
2310 let home =
2312 match Sys.os_type with
2313 | "Win32" -> Sys.getenv "HOMEPATH"
2314 | _ -> Sys.getenv "HOME"
2315 with exn ->
2316 prerr_endline
2317 ("Can not determine home directory location: " ^
2318 Printexc.to_string exn);
2322 let config_of c attrs =
2323 let apply c k v =
2325 match k with
2326 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2327 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2328 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2329 | "preload" -> { c with preload = bool_of_string v }
2330 | "page-bias" -> { c with pagebias = int_of_string v }
2331 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2332 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2333 | "crop-hack" -> { c with crophack = bool_of_string v }
2334 | "throttle" -> { c with showall = bool_of_string v }
2335 | "highlight-links" -> { c with hlinks = bool_of_string v }
2336 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2337 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2338 | "zoom" ->
2339 let zoom = float_of_string v /. 100. in
2340 let zoom = max 0.01 (min 2.2 zoom) in
2341 { c with zoom = zoom }
2342 | "presentation" -> { c with presentation = bool_of_string v }
2343 | "rotation-angle" -> { c with angle = int_of_string v }
2344 | "width" -> { c with winw = max 20 (int_of_string v) }
2345 | "height" -> { c with winh = max 20 (int_of_string v) }
2346 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2347 | "proportional-display" -> { c with proportional = bool_of_string v }
2348 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2349 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2350 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2351 | _ -> c
2352 with exn ->
2353 prerr_endline ("Error processing attribute (`" ^
2354 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2357 let rec fold c = function
2358 | [] -> c
2359 | (k, v) :: rest ->
2360 let c = apply c k v in
2361 fold c rest
2363 fold c attrs;
2366 let bookmark_of attrs =
2367 let rec fold title page rely = function
2368 | ("title", v) :: rest -> fold v page rely rest
2369 | ("page", v) :: rest -> fold title v rely rest
2370 | ("rely", v) :: rest -> fold title page v rest
2371 | _ :: rest -> fold title page rely rest
2372 | [] -> title, page, rely
2374 fold "invalid" "0" "0" attrs
2377 let setconf dst src =
2378 dst.scrollw <- src.scrollw;
2379 dst.scrollh <- src.scrollh;
2380 dst.icase <- src.icase;
2381 dst.preload <- src.preload;
2382 dst.pagebias <- src.pagebias;
2383 dst.verbose <- src.verbose;
2384 dst.scrollincr <- src.scrollincr;
2385 dst.maxhfit <- src.maxhfit;
2386 dst.crophack <- src.crophack;
2387 dst.autoscroll <- src.autoscroll;
2388 dst.showall <- src.showall;
2389 dst.hlinks <- src.hlinks;
2390 dst.underinfo <- src.underinfo;
2391 dst.interpagespace <- src.interpagespace;
2392 dst.zoom <- src.zoom;
2393 dst.presentation <- src.presentation;
2394 dst.angle <- src.angle;
2395 dst.winw <- src.winw;
2396 dst.winh <- src.winh;
2397 dst.savebmarks <- src.savebmarks;
2398 dst.memlimit <- src.memlimit;
2399 dst.proportional <- src.proportional;
2400 dst.texcount <- src.texcount;
2401 dst.sliceheight <- src.sliceheight;
2404 let unent s =
2405 let l = String.length s in
2406 let b = Buffer.create l in
2407 unent b s 0 l;
2408 Buffer.contents b;
2411 let get s =
2412 let h = Hashtbl.create 10 in
2413 let dc = { defconf with angle = defconf.angle } in
2414 let rec toplevel v t spos epos =
2415 match t with
2416 | Vdata | Vcdata | Vend -> v
2417 | Vopen ("llppconfig", attrs, closed) ->
2418 if closed
2419 then v
2420 else { v with f = llppconfig }
2421 | Vopen _ ->
2422 error "unexpected subelement at top level" s spos
2423 | Vclose tag -> error "unexpected close at top level" s spos
2425 and llppconfig v t spos epos =
2426 match t with
2427 | Vdata | Vcdata | Vend -> v
2428 | Vopen ("defaults", attrs, closed) ->
2429 let c = config_of dc attrs in
2430 setconf dc c;
2431 if closed
2432 then v
2433 else { v with f = skip "defaults" (fun () -> v) }
2435 | Vopen ("doc", attrs, closed) ->
2436 let pathent =
2438 List.assoc "path" attrs
2439 with Not_found -> error "doc is missing path attribute" s spos
2441 let path = unent pathent in
2442 let c = config_of dc attrs in
2443 let y =
2445 float_of_string (List.assoc "rely" attrs)
2446 with
2447 | Not_found -> 0.0
2448 | exn ->
2449 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2452 let x =
2454 int_of_string (List.assoc "pan" attrs)
2455 with
2456 | Not_found -> 0
2457 | exn ->
2458 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2461 if closed
2462 then (Hashtbl.add h path (c, [], x, y); v)
2463 else { v with f = doc path x y c [] }
2465 | Vopen (tag, _, closed) ->
2466 error "unexpected subelement in llppconfig" s spos
2468 | Vclose "llppconfig" -> { v with f = toplevel }
2469 | Vclose tag -> error "unexpected close in llppconfig" s spos
2471 and doc path x y c bookmarks v t spos epos =
2472 match t with
2473 | Vdata | Vcdata -> v
2474 | Vend -> error "unexpected end of input in doc" s spos
2475 | Vopen ("bookmarks", attrs, closed) ->
2476 { v with f = pbookmarks path x y c bookmarks }
2478 | Vopen (tag, _, _) ->
2479 error "unexpected subelement in doc" s spos
2481 | Vclose "doc" ->
2482 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2483 { v with f = llppconfig }
2485 | Vclose tag -> error "unexpected close in doc" s spos
2487 and pbookmarks path x y c bookmarks v t spos epos =
2488 match t with
2489 | Vdata | Vcdata -> v
2490 | Vend -> error "unexpected end of input in bookmarks" s spos
2491 | Vopen ("item", attrs, closed) ->
2492 let titleent, spage, srely = bookmark_of attrs in
2493 let page =
2495 int_of_string spage
2496 with exn ->
2497 dolog "Failed to convert page %S to integer: %s"
2498 spage (Printexc.to_string exn);
2501 let rely =
2503 float_of_string srely
2504 with exn ->
2505 dolog "Failed to convert rely %S to real: %s"
2506 srely (Printexc.to_string exn);
2509 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2510 if closed
2511 then { v with f = pbookmarks path x y c bookmarks }
2512 else
2513 let f () = v in
2514 { v with f = skip "item" f }
2516 | Vopen _ ->
2517 error "unexpected subelement in bookmarks" s spos
2519 | Vclose "bookmarks" ->
2520 { v with f = doc path x y c bookmarks }
2522 | Vclose tag -> error "unexpected close in bookmarks" s spos
2524 and skip tag f v t spos epos =
2525 match t with
2526 | Vdata | Vcdata -> v
2527 | Vend ->
2528 error ("unexpected end of input in skipped " ^ tag) s spos
2529 | Vopen (tag', _, closed) ->
2530 if closed
2531 then v
2532 else
2533 let f' () = { v with f = skip tag f } in
2534 { v with f = skip tag' f' }
2535 | Vclose ctag ->
2536 if tag = ctag
2537 then f ()
2538 else error ("unexpected close in skipped " ^ tag) s spos
2541 parse { f = toplevel; accu = () } s;
2542 h, dc;
2545 let do_load f ic =
2547 let len = in_channel_length ic in
2548 let s = String.create len in
2549 really_input ic s 0 len;
2550 f s;
2551 with
2552 | Parse_error (msg, s, pos) ->
2553 let subs = subs s pos in
2554 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2555 failwith ("parse error: " ^ s)
2557 | exn ->
2558 failwith ("config load error: " ^ Printexc.to_string exn)
2561 let path =
2562 let dir =
2564 let dir = Filename.concat home ".config" in
2565 if Sys.is_directory dir then dir else home
2566 with _ -> home
2568 Filename.concat dir "llpp.conf"
2571 let load1 f =
2572 if Sys.file_exists path
2573 then
2574 match
2575 (try Some (open_in_bin path)
2576 with exn ->
2577 prerr_endline
2578 ("Error opening configuation file `" ^ path ^ "': " ^
2579 Printexc.to_string exn);
2580 None
2582 with
2583 | Some ic ->
2584 begin try
2585 f (do_load get ic)
2586 with exn ->
2587 prerr_endline
2588 ("Error loading configuation from `" ^ path ^ "': " ^
2589 Printexc.to_string exn);
2590 end;
2591 close_in ic;
2593 | None -> ()
2594 else
2595 f (Hashtbl.create 0, defconf)
2598 let load () =
2599 let f (h, dc) =
2600 let pc, pb, px, py =
2602 Hashtbl.find h state.path
2603 with Not_found -> dc, [], 0, 0.0
2605 setconf defconf dc;
2606 setconf conf pc;
2607 state.bookmarks <- pb;
2608 state.x <- px;
2609 cbput state.hists.nav py;
2611 load1 f
2614 let add_attrs bb always dc c =
2615 let ob s a b =
2616 if always || a != b
2617 then Printf.bprintf bb "\n %s='%b'" s a
2618 and oi s a b =
2619 if always || a != b
2620 then Printf.bprintf bb "\n %s='%d'" s a
2621 and oz s a b =
2622 if always || a <> b
2623 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2625 let w, h =
2626 if always
2627 then dc.winw, dc.winh
2628 else
2629 match state.fullscreen with
2630 | Some wh -> wh
2631 | None -> c.winw, c.winh
2633 let zoom, presentation, interpagespace, showall=
2634 if always
2635 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2636 else
2637 match state.birdseye with
2638 | Some (bc, _) ->
2639 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2640 | None -> c.zoom, c.presentation, c.interpagespace, c.showall
2642 oi "width" w dc.winw;
2643 oi "height" h dc.winh;
2644 oi "scroll-bar-width" c.scrollw dc.scrollw;
2645 oi "scroll-handle-height" c.scrollh dc.scrollh;
2646 ob "case-insensitive-search" c.icase dc.icase;
2647 ob "preload" c.preload dc.preload;
2648 oi "page-bias" c.pagebias dc.pagebias;
2649 oi "scroll-step" c.scrollincr dc.scrollincr;
2650 ob "max-height-fit" c.maxhfit dc.maxhfit;
2651 ob "crop-hack" c.crophack dc.crophack;
2652 ob "throttle" showall dc.showall;
2653 ob "highlight-links" c.hlinks dc.hlinks;
2654 ob "under-cursor-info" c.underinfo dc.underinfo;
2655 oi "vertical-margin" interpagespace dc.interpagespace;
2656 oz "zoom" zoom dc.zoom;
2657 ob "presentation" presentation dc.presentation;
2658 oi "rotation-angle" c.angle dc.angle;
2659 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2660 ob "proportional-display" c.proportional dc.proportional;
2661 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2662 oi "texcount" c.texcount dc.texcount;
2663 oi "slice-height" c.sliceheight dc.sliceheight;
2666 let save () =
2667 let bb = Buffer.create 32768 in
2668 let f (h, dc) =
2669 Buffer.add_string bb "<llppconfig>\n<defaults ";
2670 add_attrs bb true dc dc;
2671 Buffer.add_string bb "/>\n";
2673 let adddoc path x y c bookmarks =
2674 if bookmarks == [] && c = dc && y = 0.0
2675 then ()
2676 else (
2677 Printf.bprintf bb "<doc path='%s'"
2678 (enent path 0 (String.length path));
2680 if y <> 0.0
2681 then Printf.bprintf bb " rely='%f'" y;
2683 if x != 0
2684 then Printf.bprintf bb " pan='%d'" x;
2686 add_attrs bb false dc c;
2688 begin match bookmarks with
2689 | [] -> Buffer.add_string bb "/>\n"
2690 | _ ->
2691 Buffer.add_string bb ">\n<bookmarks>\n";
2692 List.iter (fun (title, _level, page, rely) ->
2693 Printf.bprintf bb
2694 "<item title='%s' page='%d' rely='%f'/>\n"
2695 (enent title 0 (String.length title))
2696 page
2697 rely
2698 ) bookmarks;
2699 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2700 end;
2704 let x =
2705 match state.birdseye with
2706 | Some (_, x) -> x
2707 | None -> state.x
2709 adddoc state.path x (yratio state.y) conf
2710 (if conf.savebmarks then state.bookmarks else []);
2712 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2713 if path <> state.path
2714 then
2715 adddoc path x y c bookmarks
2716 ) h;
2717 Buffer.add_string bb "</llppconfig>";
2719 load1 f;
2720 if Buffer.length bb > 0
2721 then
2723 let tmp = path ^ ".tmp" in
2724 let oc = open_out_bin tmp in
2725 Buffer.output_buffer oc bb;
2726 close_out oc;
2727 Sys.rename tmp path;
2728 with exn ->
2729 prerr_endline
2730 ("error while saving configuration: " ^ Printexc.to_string exn)
2732 end;;
2734 let () =
2735 Arg.parse
2736 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2737 (fun s -> state.path <- s)
2738 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2740 let path =
2741 if String.length state.path = 0
2742 then (prerr_endline "filename missing"; exit 1)
2743 else (
2744 if Filename.is_relative state.path
2745 then Filename.concat (Sys.getcwd ()) state.path
2746 else state.path
2749 state.path <- path;
2751 State.load ();
2753 let _ = Glut.init Sys.argv in
2754 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2755 let () = Glut.initWindowSize conf.winw conf.winh in
2756 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2758 let csock, ssock =
2759 if Sys.os_type = "Unix"
2760 then
2761 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2762 else
2763 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2764 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2765 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2766 Unix.bind sock addr;
2767 Unix.listen sock 1;
2768 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2769 Unix.connect csock addr;
2770 let ssock, _ = Unix.accept sock in
2771 Unix.close sock;
2772 let opts sock =
2773 Unix.setsockopt sock Unix.TCP_NODELAY true;
2774 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2776 opts ssock;
2777 opts csock;
2778 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2779 ssock, csock
2782 let () = Glut.displayFunc display in
2783 let () = Glut.reshapeFunc reshape in
2784 let () = Glut.keyboardFunc keyboard in
2785 let () = Glut.specialFunc special in
2786 let () = Glut.idleFunc (Some idle) in
2787 let () = Glut.mouseFunc mouse in
2788 let () = Glut.motionFunc motion in
2789 let () = Glut.passiveMotionFunc pmotion in
2791 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2792 state.csock <- csock;
2793 state.ssock <- ssock;
2794 state.text <- "Opening " ^ path;
2795 writeopen state.path state.password;
2797 at_exit State.save;
2799 let rec handlelablglutbug () =
2801 Glut.mainLoop ();
2802 with Glut.BadEnum "key in special_of_int" ->
2803 showtext '!' " LablGlut bug: special key not recognized";
2804 handlelablglutbug ()
2806 handlelablglutbug ();