Unbreak positioing after reshape
[llpp.git] / main.ml
blob2a8ad976195ef5498ad5c40537069883c32df45d
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let log fmt = Printf.kprintf prerr_endline fmt;;
9 let dolog fmt = Printf.kprintf prerr_endline fmt;;
11 type params = angle * proportional * texcount * sliceheight
12 and pageno = int
13 and width = int
14 and height = int
15 and leftx = int
16 and opaque = string
17 and recttype = int
18 and pixmapsize = int
19 and angle = int
20 and proportional = bool
21 and interpagespace = int
22 and texcount = int
23 and sliceheight = int
24 and gen = int
27 external init : Unix.file_descr -> params -> unit = "ml_init";;
28 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
29 external seltext : string -> (int * int * int * int) -> int -> unit =
30 "ml_seltext";;
31 external copysel : string -> unit = "ml_copysel";;
32 external getpdimrect : int -> float array = "ml_getpdimrect";;
33 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
34 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 type mpos = int * int
37 and mstate =
38 | Msel of (mpos * mpos)
39 | Mpan of mpos
40 | Mscroll
41 | Mnone
44 type 'a circbuf =
45 { store : 'a array
46 ; mutable rc : int
47 ; mutable wc : int
48 ; mutable len : int
52 type textentry = (char * string * onhist * onkey * ondone)
53 and onkey = string -> int -> te
54 and ondone = string -> unit
55 and histcancel = unit -> unit
56 and onhist = ((histcmd -> string) * histcancel) option
57 and histcmd = HCnext | HCprev | HCfirst | HClast
58 and te =
59 | TEstop
60 | TEdone of string
61 | TEcont of string
62 | TEswitch of textentry
65 let cbnew n v =
66 { store = Array.create n v
67 ; rc = 0
68 ; wc = 0
69 ; len = 0
73 let cbcap b = Array.length b.store;;
75 let cbput b v =
76 let cap = cbcap b in
77 b.store.(b.wc) <- v;
78 b.wc <- (b.wc + 1) mod cap;
79 b.rc <- b.wc;
80 b.len <- min (b.len + 1) cap;
83 let cbempty b = b.len = 0;;
85 let cbgetg b circular dir =
86 if cbempty b
87 then b.store.(0)
88 else
89 let rc = b.rc + dir in
90 let rc =
91 if circular
92 then (
93 if rc = -1
94 then b.len-1
95 else (
96 if rc = b.len
97 then 0
98 else rc
101 else max 0 (min rc (b.len-1))
103 b.rc <- rc;
104 b.store.(rc);
107 let cbget b = cbgetg b false;;
108 let cbgetc b = cbgetg b true;;
110 let cbpeek b =
111 let rc = b.wc - b.len in
112 let rc = if rc < 0 then cbcap b + rc else rc in
113 b.store.(rc);
116 let cbdecr b = b.len <- b.len - 1;;
118 type layout =
119 { pageno : int
120 ; pagedimno : int
121 ; pagew : int
122 ; pageh : int
123 ; pagedispy : int
124 ; pagey : int
125 ; pagevh : int
126 ; pagex : int
130 type conf =
131 { mutable scrollw : int
132 ; mutable scrollh : int
133 ; mutable icase : bool
134 ; mutable preload : bool
135 ; mutable pagebias : int
136 ; mutable verbose : bool
137 ; mutable scrollincr : int
138 ; mutable maxhfit : bool
139 ; mutable crophack : bool
140 ; mutable autoscroll : bool
141 ; mutable showall : bool
142 ; mutable hlinks : bool
143 ; mutable underinfo : bool
144 ; mutable interpagespace : interpagespace
145 ; mutable zoom : float
146 ; mutable presentation : bool
147 ; mutable angle : angle
148 ; mutable winw : int
149 ; mutable winh : int
150 ; mutable savebmarks : bool
151 ; mutable proportional : proportional
152 ; mutable memlimit : int
153 ; mutable texcount : texcount
154 ; mutable sliceheight : sliceheight
158 type outline = string * int * int * float;;
159 type outlines =
160 | Oarray of outline array
161 | Olist of outline list
162 | Onarrow of string * outline array * outline array
165 type rect = (float * float * float * float * float * float * float * float);;
167 type pagemapkey = (pageno * width * angle * proportional * gen);;
169 type state =
170 { mutable csock : Unix.file_descr
171 ; mutable ssock : Unix.file_descr
172 ; mutable w : int
173 ; mutable x : int
174 ; mutable y : int
175 ; mutable maxy : int
176 ; mutable layout : layout list
177 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
178 ; mutable pdims : (pageno * width * height * leftx) list
179 ; mutable pagecount : int
180 ; pagecache : string circbuf
181 ; mutable rendering : bool
182 ; mutable mstate : mstate
183 ; mutable searchpattern : string
184 ; mutable rects : (pageno * recttype * rect) list
185 ; mutable rects1 : (pageno * recttype * rect) list
186 ; mutable text : string
187 ; mutable fullscreen : (width * height) option
188 ; mutable birdseye : (conf * leftx * pageno) option
189 ; mutable textentry : textentry option
190 ; mutable outlines : outlines
191 ; mutable outline : (bool * int * int * outline array * string) option
192 ; mutable bookmarks : outline list
193 ; mutable path : string
194 ; mutable password : string
195 ; mutable invalidated : int
196 ; mutable colorscale : float
197 ; mutable memused : int
198 ; mutable birdseyepageno : pageno
199 ; mutable gen : gen
200 ; mutable throttle : layout list option
201 ; hists : hists
203 and hists =
204 { pat : string circbuf
205 ; pag : string circbuf
206 ; nav : float circbuf
210 let defconf =
211 { scrollw = 7
212 ; scrollh = 12
213 ; icase = true
214 ; preload = true
215 ; pagebias = 0
216 ; verbose = false
217 ; scrollincr = 24
218 ; maxhfit = true
219 ; crophack = false
220 ; autoscroll = false
221 ; showall = false
222 ; hlinks = false
223 ; underinfo = false
224 ; interpagespace = 2
225 ; zoom = 1.0
226 ; presentation = false
227 ; angle = 0
228 ; winw = 900
229 ; winh = 900
230 ; savebmarks = true
231 ; proportional = true
232 ; memlimit = 32*1024*1024
233 ; texcount = 256
234 ; sliceheight = 24
238 let conf = { defconf with angle = defconf.angle };;
240 let state =
241 { csock = Unix.stdin
242 ; ssock = Unix.stdin
243 ; w = 0
244 ; y = 0
245 ; x = 0
246 ; layout = []
247 ; maxy = max_int
248 ; pagemap = Hashtbl.create 10
249 ; pagecache = cbnew 100 ""
250 ; pdims = []
251 ; pagecount = 0
252 ; rendering = false
253 ; mstate = Mnone
254 ; rects = []
255 ; rects1 = []
256 ; text = ""
257 ; fullscreen = None
258 ; birdseye = None
259 ; textentry = None
260 ; searchpattern = ""
261 ; outlines = Olist []
262 ; outline = None
263 ; bookmarks = []
264 ; path = ""
265 ; password = ""
266 ; invalidated = 0
267 ; hists =
268 { nav = cbnew 100 0.0
269 ; pat = cbnew 20 ""
270 ; pag = cbnew 10 ""
272 ; colorscale = 1.0
273 ; memused = 0
274 ; birdseyepageno = -1
275 ; gen = 0
276 ; throttle = None
280 let vlog fmt =
281 if conf.verbose
282 then
283 Printf.kprintf prerr_endline fmt
284 else
285 Printf.kprintf ignore fmt
288 let writecmd fd s =
289 let len = String.length s in
290 let n = 4 + len in
291 let b = Buffer.create n in
292 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
293 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
294 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
295 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
296 Buffer.add_string b s;
297 let s' = Buffer.contents b in
298 let n' = Unix.write fd s' 0 n in
299 if n' != n then failwith "write failed";
302 let readcmd fd =
303 let s = "xxxx" in
304 let n = Unix.read fd s 0 4 in
305 if n != 4 then failwith "incomplete read(len)";
306 let len = 0
307 lor (Char.code s.[0] lsl 24)
308 lor (Char.code s.[1] lsl 16)
309 lor (Char.code s.[2] lsl 8)
310 lor (Char.code s.[3] lsl 0)
312 let s = String.create len in
313 let n = Unix.read fd s 0 len in
314 if n != len then failwith "incomplete read(data)";
318 let yratio y =
319 if y = state.maxy
320 then 1.0
321 else float y /. float state.maxy
324 let makecmd s l =
325 let b = Buffer.create 10 in
326 Buffer.add_string b s;
327 let rec combine = function
328 | [] -> b
329 | x :: xs ->
330 Buffer.add_char b ' ';
331 let s =
332 match x with
333 | `b b -> if b then "1" else "0"
334 | `s s -> s
335 | `i i -> string_of_int i
336 | `f f -> string_of_float f
337 | `I f -> string_of_int (truncate f)
339 Buffer.add_string b s;
340 combine xs;
342 combine l;
345 let wcmd s l =
346 let cmd = Buffer.contents (makecmd s l) in
347 writecmd state.csock cmd;
350 let calcips h =
351 if conf.presentation
352 then
353 let d = conf.winh - h in
354 max 0 ((d + 1) / 2)
355 else
356 conf.interpagespace
359 let calcheight () =
360 let rec f pn ph pi fh l =
361 match l with
362 | (n, _, h, _) :: rest ->
363 let ips = calcips h in
364 let fh =
365 if conf.presentation
366 then fh+ips
367 else (
368 if state.birdseye <> None && pn = 0
369 then fh + ips
370 else fh
373 let fh = fh + ((n - pn) * (ph + pi)) in
374 f n h ips fh rest
376 | [] ->
377 let inc =
378 if conf.presentation || (state.birdseye <> None && pn = 0)
379 then 0
380 else -pi
382 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
383 max 0 fh
385 let fh = f 0 0 0 0 state.pdims in
389 let getpageyh pageno =
390 let rec f pn ph pi y l =
391 match l with
392 | (n, _, h, _) :: rest ->
393 let ips = calcips h in
394 if n >= pageno
395 then
396 if conf.presentation && n = pageno
397 then
398 y + (pageno - pn) * (ph + pi) + pi, h
399 else
400 y + (pageno - pn) * (ph + pi), h
401 else
402 let y = y + (if conf.presentation then pi else 0) in
403 let y = y + (n - pn) * (ph + pi) in
404 f n h ips y rest
406 | [] ->
407 y + (pageno - pn) * (ph + pi), ph
409 f 0 0 0 0 state.pdims
412 let getpagey pageno = fst (getpageyh pageno);;
414 let layout y sh =
415 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
416 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
417 match pdims with
418 | (pageno', w, h, x) :: rest when pageno' = pageno ->
419 let ips = calcips h in
420 let yinc =
421 if conf.presentation || (state.birdseye <> None && pageno = 0)
422 then ips
423 else 0
425 (w, h, ips, x), rest, pdimno + 1, yinc
426 | _ ->
427 prev, pdims, pdimno, 0
429 let dy = dy + yinc in
430 let py = py + yinc in
431 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
432 then
433 accu
434 else
435 let vy = y + dy in
436 if py + h <= vy - yinc
437 then
438 let py = py + h + ips in
439 let dy = max 0 (py - y) in
440 f ~pageno:(pageno+1)
441 ~pdimno
442 ~prev:curr
445 ~pdims:rest
446 ~cacheleft
447 ~accu
448 else
449 let pagey = vy - py in
450 let pagevh = h - pagey in
451 let pagevh = min (sh - dy) pagevh in
452 let off = if yinc > 0 then py - vy else 0 in
453 let py = py + h + ips in
454 let e =
455 { pageno = pageno
456 ; pagedimno = pdimno
457 ; pagew = w
458 ; pageh = h
459 ; pagedispy = dy + off
460 ; pagey = pagey + off
461 ; pagevh = pagevh - off
462 ; pagex = x
465 let accu = e :: accu in
466 f ~pageno:(pageno+1)
467 ~pdimno
468 ~prev:curr
470 ~dy:(dy+pagevh+ips)
471 ~pdims:rest
472 ~cacheleft:(cacheleft-1)
473 ~accu
475 if state.invalidated = 0
476 then (
477 let accu =
479 ~pageno:0
480 ~pdimno:~-1
481 ~prev:(0,0,0,0)
482 ~py:0
483 ~dy:0
484 ~pdims:state.pdims
485 ~cacheleft:(cbcap state.pagecache)
486 ~accu:[]
488 List.rev accu
490 else
494 let clamp incr =
495 let y = state.y + incr in
496 let y = max 0 y in
497 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
501 let getopaque pageno =
502 try Some (Hashtbl.find state.pagemap
503 (pageno, state.w, conf.angle, conf.proportional, state.gen))
504 with Not_found -> None
507 let cache pageno opaque =
508 Hashtbl.replace state.pagemap
509 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
512 let validopaque opaque = String.length opaque > 0;;
514 let render l =
515 match getopaque l.pageno with
516 | None when not state.rendering ->
517 state.rendering <- true;
518 cache l.pageno ("", -1);
519 wcmd "render" [`i (l.pageno + 1)
520 ;`i l.pagedimno
521 ;`i l.pagew
522 ;`i l.pageh];
524 | _ -> ()
527 let loadlayout layout =
528 let rec f all = function
529 | l :: ls ->
530 begin match getopaque l.pageno with
531 | None -> render l; f false ls
532 | Some (opaque, _) -> f (all && validopaque opaque) ls
534 | [] -> all
536 f (layout <> []) layout;
539 let findpageforopaque opaque =
540 Hashtbl.fold
541 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
542 state.pagemap None
545 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
547 let preload () =
548 let oktopreload =
549 if conf.preload
550 then
551 let memleft = conf.memlimit - state.memused in
552 if memleft < 0
553 then
554 let opaque = cbpeek state.pagecache in
555 match findpageforopaque opaque with
556 | Some ((n, _, _, _, _), size) ->
557 memleft + size >= 0 && not (pagevisible state.layout n)
558 | None -> false
559 else true
560 else false
562 if oktopreload
563 then
564 let presentation = conf.presentation in
565 let interpagespace = conf.interpagespace in
566 let maxy = state.maxy in
567 conf.presentation <- false;
568 conf.interpagespace <- 0;
569 state.maxy <- calcheight ();
570 let y =
571 match state.layout with
572 | [] -> 0
573 | l :: _ -> getpagey l.pageno
575 let y = if y < conf.winh then 0 else y - conf.winh in
576 let pages = layout y (conf.winh*3) in
577 List.iter render pages;
578 conf.presentation <- presentation;
579 conf.interpagespace <- interpagespace;
580 state.maxy <- maxy;
583 let gotoy y =
584 let y = max 0 y in
585 let y = min state.maxy y in
586 let pages = layout y conf.winh in
587 let ready = loadlayout pages in
588 if conf.showall
589 then (
590 if ready
591 then (
592 state.y <- y;
593 state.layout <- pages;
594 state.throttle <- None;
595 Glut.postRedisplay ();
597 else (
598 state.throttle <- Some pages;
601 else (
602 state.y <- y;
603 state.layout <- pages;
604 state.throttle <- None;
605 Glut.postRedisplay ();
607 if state.birdseye <> None
608 then (
609 if not (pagevisible pages state.birdseyepageno)
610 then
611 match state.layout with
612 | [] -> ()
613 | l :: _ -> state.birdseyepageno <- l.pageno
615 preload ();
618 let gotoy_and_clear_text y =
619 gotoy y;
620 if not conf.verbose then state.text <- "";
623 let addnav () =
624 cbput state.hists.nav (yratio state.y);
627 let getnav () =
628 let y = cbgetc state.hists.nav ~-1 in
629 truncate (y *. float state.maxy)
632 let gotopage n top =
633 let y, h = getpageyh n in
634 addnav ();
635 gotoy_and_clear_text (y + (truncate (top *. float h)));
638 let gotopage1 n top =
639 let y = getpagey n in
640 addnav ();
641 gotoy_and_clear_text (y + top);
644 let invalidate () =
645 state.layout <- [];
646 state.pdims <- [];
647 state.rects <- [];
648 state.rects1 <- [];
649 state.invalidated <- state.invalidated + 1;
652 let scalecolor c =
653 let c = c *. state.colorscale in
654 (c, c, c);
657 let represent () =
658 let maxy = calcheight () in
659 let y =
660 if state.birdseyepageno = -1 && state.birdseye = None
661 then
662 match state.layout with
663 | [] ->
664 let rely = yratio state.y in
665 truncate (float maxy *. rely)
667 | l :: _ ->
668 getpagey l.pageno
669 else (
670 let y = getpagey state.birdseyepageno in
671 state.birdseyepageno <- -1;
675 state.maxy <- maxy;
676 gotoy y
679 let pagematrix () =
680 GlMat.mode `projection;
681 GlMat.load_identity ();
682 GlMat.rotate ~x:1.0 ~angle:180.0 ();
683 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
684 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
687 let winmatrix () =
688 GlMat.mode `projection;
689 GlMat.load_identity ();
690 GlMat.rotate ~x:1.0 ~angle:180.0 ();
691 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
692 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
695 let reshape ~w ~h =
696 conf.winw <- w;
697 let w = truncate (float w *. conf.zoom) - conf.scrollw in
698 let w = max w 2 in
699 state.w <- w;
700 conf.winh <- h;
701 GlMat.mode `modelview;
702 GlMat.load_identity ();
703 GlClear.color (scalecolor 1.0);
704 GlClear.clear [`color];
706 invalidate ();
707 wcmd "geometry" [`i w; `i h];
710 let showtext c s =
711 GlDraw.color (0.0, 0.0, 0.0);
712 GlDraw.rect
713 (0.0, float (conf.winh - 18))
714 (float (conf.winw - conf.scrollw - 1), float conf.winh)
716 let font = Glut.BITMAP_8_BY_13 in
717 GlDraw.color (1.0, 1.0, 1.0);
718 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
719 Glut.bitmapCharacter ~font ~c:(Char.code c);
720 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
723 let enttext () =
724 let len = String.length state.text in
725 match state.textentry with
726 | None ->
727 if len > 0 then showtext ' ' state.text
729 | Some (c, text, _, _, _) ->
730 let s =
731 if len > 0
732 then
733 text ^ " [" ^ state.text ^ "]"
734 else
735 text
737 showtext c s;
740 let showtext c s =
741 if true
742 then (
743 state.text <- Printf.sprintf "%c%s" c s;
744 Glut.postRedisplay ();
746 else (
747 showtext c s;
748 Glut.swapBuffers ();
752 let act cmd =
753 match cmd.[0] with
754 | 'c' ->
755 state.pdims <- [];
757 | 'D' ->
758 state.rects <- state.rects1;
759 Glut.postRedisplay ()
761 | 'C' ->
762 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
763 state.pagecount <- n;
764 state.invalidated <- state.invalidated - 1;
765 if state.invalidated = 0
766 then represent ()
768 | 't' ->
769 let s = Scanf.sscanf cmd "t %n"
770 (fun n -> String.sub cmd n (String.length cmd - n))
772 Glut.setWindowTitle s
774 | 'T' ->
775 let s = Scanf.sscanf cmd "T %n"
776 (fun n -> String.sub cmd n (String.length cmd - n))
778 if state.textentry = None
779 then (
780 state.text <- s;
781 showtext ' ' s;
783 else (
784 state.text <- s;
785 Glut.postRedisplay ();
788 | 'V' ->
789 if conf.verbose
790 then
791 let s = Scanf.sscanf cmd "V %n"
792 (fun n -> String.sub cmd n (String.length cmd - n))
794 state.text <- s;
795 showtext ' ' s;
797 | 'F' ->
798 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
799 Scanf.sscanf cmd "F %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 let y = (getpagey pageno) + truncate y0 in
804 addnav ();
805 gotoy y;
806 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
808 | 'R' ->
809 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
810 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
811 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
812 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
814 state.rects1 <-
815 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
817 | 'r' ->
818 let n, w, h, r, l, s, p =
819 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
820 (fun n w h r l s p ->
821 (n-1, w, h, r, l != 0, s, p))
824 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
825 state.memused <- state.memused + s;
827 let layout =
828 match state.throttle with
829 | None -> state.layout
830 | Some layout -> layout
833 let rec gc () =
834 if (state.memused <= conf.memlimit) || cbempty state.pagecache
835 then ()
836 else (
837 let evictedopaque = cbpeek state.pagecache in
838 match findpageforopaque evictedopaque with
839 | None -> failwith "bug in gc"
840 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
841 if state.gen != gen || not (pagevisible layout evictedn)
842 then (
843 wcmd "free" [`s evictedopaque];
844 state.memused <- state.memused - evictedsize;
845 Hashtbl.remove state.pagemap k;
846 cbdecr state.pagecache;
847 gc ();
851 gc ();
853 cbput state.pagecache p;
854 state.rendering <- false;
856 begin match state.throttle with
857 | None ->
858 if pagevisible state.layout n
859 then gotoy state.y
860 else (
861 let allvisible = loadlayout state.layout in
862 if allvisible then preload ();
865 | Some layout ->
866 match layout with
867 | [] -> ()
868 | l :: _ ->
869 let y = getpagey l.pageno + l.pagey in
870 gotoy y
873 | 'l' ->
874 let (n, w, h, x) as pdim =
875 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
877 state.pdims <- pdim :: state.pdims
879 | 'o' ->
880 let (l, n, t, h, pos) =
881 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
883 let s = String.sub cmd pos (String.length cmd - pos) in
884 let s =
885 let l = String.length s in
886 let b = Buffer.create (String.length s) in
887 let rec loop pc2 i =
888 if i = l
889 then ()
890 else
891 let pc2 =
892 match s.[i] with
893 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
894 | '\xc2' -> true
895 | c ->
896 let c = if Char.code c land 0x80 = 0 then c else '?' in
897 Buffer.add_char b c;
898 false
900 loop pc2 (i+1)
902 loop false 0;
903 Buffer.contents b
905 let outline = (s, l, n, float t /. float h) in
906 let outlines =
907 match state.outlines with
908 | Olist outlines -> Olist (outline :: outlines)
909 | Oarray _ -> Olist [outline]
910 | Onarrow _ -> Olist [outline]
912 state.outlines <- outlines
914 | _ ->
915 log "unknown cmd `%S'" cmd
918 let now = Unix.gettimeofday;;
920 let idle () =
921 let rec loop delay =
922 let r, _, _ = Unix.select [state.csock] [] [] delay in
923 begin match r with
924 | [] ->
925 if conf.autoscroll
926 then begin
927 let y = state.y + conf.scrollincr in
928 let y = if y >= state.maxy then 0 else y in
929 gotoy y;
930 state.text <- "";
931 end;
933 | _ ->
934 let cmd = readcmd state.csock in
935 act cmd;
936 loop 0.0
937 end;
938 in loop 0.001
941 let onhist cb =
942 let rc = cb.rc in
943 let action = function
944 | HCprev -> cbget cb ~-1
945 | HCnext -> cbget cb 1
946 | HCfirst -> cbget cb ~-(cb.rc)
947 | HClast -> cbget cb (cb.len - 1 - cb.rc)
948 and cancel () = cb.rc <- rc
949 in (action, cancel)
952 let search pattern forward =
953 if String.length pattern > 0
954 then
955 let pn, py =
956 match state.layout with
957 | [] -> 0, 0
958 | l :: _ ->
959 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
961 let cmd =
962 let b = makecmd "search"
963 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
965 Buffer.add_char b ',';
966 Buffer.add_string b pattern;
967 Buffer.add_char b '\000';
968 Buffer.contents b;
970 writecmd state.csock cmd;
973 let intentry text key =
974 let c = Char.unsafe_chr key in
975 match c with
976 | '0' .. '9' ->
977 let s = "x" in s.[0] <- c;
978 let text = text ^ s in
979 TEcont text
981 | _ ->
982 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
983 TEcont text
986 let addchar s c =
987 let b = Buffer.create (String.length s + 1) in
988 Buffer.add_string b s;
989 Buffer.add_char b c;
990 Buffer.contents b;
993 let textentry text key =
994 let c = Char.unsafe_chr key in
995 match c with
996 | _ when key >= 32 && key < 127 ->
997 let text = addchar text c in
998 TEcont text
1000 | _ ->
1001 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1002 TEcont text
1005 let reinit angle proportional =
1006 conf.angle <- angle;
1007 conf.proportional <- proportional;
1008 invalidate ();
1009 wcmd "reinit" [`i angle; `b proportional];
1012 let optentry text key =
1013 let btos b = if b then "on" else "off" in
1014 let c = Char.unsafe_chr key in
1015 match c with
1016 | 's' ->
1017 let ondone s =
1018 try conf.scrollincr <- int_of_string s with exc ->
1019 state.text <- Printf.sprintf "bad integer `%s': %s"
1020 s (Printexc.to_string exc)
1022 TEswitch ('#', "", None, intentry, ondone)
1024 | 'R' ->
1025 let ondone s =
1026 match try
1027 Some (int_of_string s)
1028 with exc ->
1029 state.text <- Printf.sprintf "bad integer `%s': %s"
1030 s (Printexc.to_string exc);
1031 None
1032 with
1033 | Some angle -> reinit angle conf.proportional
1034 | None -> ()
1036 TEswitch ('^', "", None, intentry, ondone)
1038 | 'i' ->
1039 conf.icase <- not conf.icase;
1040 TEdone ("case insensitive search " ^ (btos conf.icase))
1042 | 'p' ->
1043 conf.preload <- not conf.preload;
1044 gotoy state.y;
1045 TEdone ("preload " ^ (btos conf.preload))
1047 | 'v' ->
1048 conf.verbose <- not conf.verbose;
1049 TEdone ("verbose " ^ (btos conf.verbose))
1051 | 'h' ->
1052 conf.maxhfit <- not conf.maxhfit;
1053 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1054 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1056 | 'c' ->
1057 conf.crophack <- not conf.crophack;
1058 TEdone ("crophack " ^ btos conf.crophack)
1060 | 'a' ->
1061 conf.showall <- not conf.showall;
1062 TEdone ("showall " ^ btos conf.showall)
1064 | 'f' ->
1065 conf.underinfo <- not conf.underinfo;
1066 TEdone ("underinfo " ^ btos conf.underinfo)
1068 | 'P' ->
1069 conf.savebmarks <- not conf.savebmarks;
1070 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1072 | 'S' ->
1073 let ondone s =
1075 let pageno, py =
1076 match state.layout with
1077 | [] -> 0, 0
1078 | l :: _ ->
1079 l.pageno, l.pagey
1081 conf.interpagespace <- int_of_string s;
1082 state.maxy <- calcheight ();
1083 let y = getpagey pageno in
1084 gotoy (y + py)
1085 with exc ->
1086 state.text <- Printf.sprintf "bad integer `%s': %s"
1087 s (Printexc.to_string exc)
1089 TEswitch ('%', "", None, intentry, ondone)
1091 | 'l' ->
1092 reinit conf.angle (not conf.proportional);
1093 TEdone ("proprortional display " ^ btos conf.proportional)
1095 | _ ->
1096 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1097 TEstop
1100 let maxoutlinerows () = (conf.winh - 31) / 16;;
1102 let enterselector allowdel outlines errmsg msg =
1103 if Array.length outlines = 0
1104 then (
1105 showtext ' ' errmsg;
1107 else (
1108 state.text <- msg;
1109 Glut.setCursor Glut.CURSOR_INHERIT;
1110 let pageno =
1111 match state.layout with
1112 | [] -> -1
1113 | {pageno=pageno} :: rest -> pageno
1115 let active =
1116 let rec loop n =
1117 if n = Array.length outlines
1118 then 0
1119 else
1120 let (_, _, outlinepageno, _) = outlines.(n) in
1121 if outlinepageno >= pageno then n else loop (n+1)
1123 loop 0
1125 state.outline <-
1126 Some (allowdel, active,
1127 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1128 Glut.postRedisplay ();
1132 let enteroutlinemode () =
1133 let outlines, msg =
1134 match state.outlines with
1135 | Oarray a -> a, ""
1136 | Olist l ->
1137 let a = Array.of_list (List.rev l) in
1138 state.outlines <- Oarray a;
1139 a, ""
1140 | Onarrow (pat, a, b) ->
1141 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1143 enterselector false outlines "Document has no outline" msg;
1146 let enterbookmarkmode () =
1147 let bookmarks = Array.of_list state.bookmarks in
1148 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1151 let quickbookmark ?title () =
1152 match state.layout with
1153 | [] -> ()
1154 | l :: _ ->
1155 let title =
1156 match title with
1157 | None ->
1158 let sec = Unix.gettimeofday () in
1159 let tm = Unix.localtime sec in
1160 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1161 (l.pageno+1)
1162 tm.Unix.tm_mday
1163 tm.Unix.tm_mon
1164 (tm.Unix.tm_year + 1900)
1165 tm.Unix.tm_hour
1166 tm.Unix.tm_min
1167 | Some title -> title
1169 state.bookmarks <-
1170 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1173 let doreshape w h =
1174 state.fullscreen <- None;
1175 Glut.reshapeWindow w h;
1178 let writeopen path password =
1179 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1182 let opendoc path password =
1183 invalidate ();
1184 state.path <- path;
1185 state.password <- password;
1186 state.gen <- state.gen + 1;
1188 writeopen path password;
1189 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1190 wcmd "geometry" [`i state.w; `i conf.winh];
1193 let birdseyeoff (c, leftx, _) =
1194 state.birdseye <- None;
1195 conf.zoom <- c.zoom;
1196 conf.presentation <- c.presentation;
1197 conf.interpagespace <- c.interpagespace;
1198 conf.showall <- c.showall;
1199 conf.hlinks <- c.hlinks;
1200 state.x <- leftx;
1201 if conf.verbose
1202 then
1203 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1204 (100.0*.conf.zoom)
1208 let viewkeyboard ~key ~x ~y =
1209 let enttext te =
1210 state.textentry <- te;
1211 state.text <- "";
1212 enttext ();
1213 Glut.postRedisplay ()
1215 match state.textentry with
1216 | None ->
1217 let c = Char.chr key in
1218 begin match c with
1219 | '\027' | 'q' ->
1220 exit 0
1222 | '\008' ->
1223 let y = getnav () in
1224 gotoy_and_clear_text y
1226 | '\013' ->
1227 begin match state.birdseye with
1228 | None -> ()
1229 | Some vals ->
1230 birdseyeoff vals;
1231 reshape conf.winw conf.winh;
1232 end;
1234 | 'o' ->
1235 enteroutlinemode ()
1237 | 'u' ->
1238 state.rects <- [];
1239 state.text <- "";
1240 Glut.postRedisplay ()
1242 | '/' | '?' ->
1243 let ondone isforw s =
1244 cbput state.hists.pat s;
1245 state.searchpattern <- s;
1246 search s isforw
1248 enttext (Some (c, "", Some (onhist state.hists.pat),
1249 textentry, ondone (c ='/')))
1251 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1252 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1253 conf.zoom <- min 2.2 (conf.zoom +. incr);
1254 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1255 reshape conf.winw conf.winh
1257 | '+' ->
1258 let ondone s =
1259 let n =
1260 try int_of_string s with exc ->
1261 state.text <- Printf.sprintf "bad integer `%s': %s"
1262 s (Printexc.to_string exc);
1263 max_int
1265 if n != max_int
1266 then (
1267 conf.pagebias <- n;
1268 state.text <- "page bias is now " ^ string_of_int n;
1271 enttext (Some ('+', "", None, intentry, ondone))
1273 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1274 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1275 conf.zoom <- max 0.01 (conf.zoom -. decr);
1276 if conf.zoom <= 1.0 then state.x <- 0;
1277 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1278 reshape conf.winw conf.winh;
1280 | '-' ->
1281 let ondone msg =
1282 state.text <- msg;
1284 enttext (Some ('-', "", None, optentry, ondone))
1286 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1287 state.x <- 0;
1288 conf.zoom <- 1.0;
1289 state.text <- "zoom is 100%";
1290 reshape conf.winw conf.winh
1292 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1293 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1294 if zoom < 1.0
1295 then (
1296 conf.zoom <- zoom;
1297 state.x <- 0;
1298 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1299 reshape conf.winw conf.winh;
1302 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1303 begin match state.birdseye with
1304 | None ->
1305 let zoom = 76.0 /. float state.w in
1306 let birdseyepageno =
1307 match state.layout with
1308 | [] -> 0
1309 | l :: _ -> l.pageno
1311 state.birdseyepageno <- birdseyepageno;
1312 state.birdseye <-
1313 Some ({ conf with zoom = conf.zoom }, state.x, -1);
1314 conf.zoom <- zoom;
1315 conf.presentation <- false;
1316 conf.interpagespace <- 10;
1317 conf.hlinks <- false;
1318 state.x <- 0;
1319 state.mstate <- Mnone;
1320 conf.showall <- false;
1321 Glut.setCursor Glut.CURSOR_INHERIT;
1322 if conf.verbose
1323 then
1324 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1325 (100.0*.zoom)
1326 else
1327 state.text <- ""
1330 | Some vals ->
1331 birdseyeoff vals;
1332 end;
1333 reshape conf.winw conf.winh;
1335 | '0' .. '9' ->
1336 let ondone s =
1337 let n =
1338 try int_of_string s with exc ->
1339 state.text <- Printf.sprintf "bad integer `%s': %s"
1340 s (Printexc.to_string exc);
1343 if n >= 0
1344 then (
1345 addnav ();
1346 cbput state.hists.pag (string_of_int n);
1347 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1350 let pageentry text key =
1351 match Char.unsafe_chr key with
1352 | 'g' -> TEdone text
1353 | _ -> intentry text key
1355 let text = "x" in text.[0] <- c;
1356 enttext (Some (':', text, Some (onhist state.hists.pag),
1357 pageentry, ondone))
1359 | 'b' ->
1360 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1361 reshape conf.winw conf.winh;
1363 | 'l' ->
1364 conf.hlinks <- not conf.hlinks;
1365 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1366 Glut.postRedisplay ()
1368 | 'a' ->
1369 conf.autoscroll <- not conf.autoscroll
1371 | 'P' ->
1372 conf.presentation <- not conf.presentation;
1373 showtext ' ' ("presentation mode " ^
1374 if conf.presentation then "on" else "off");
1375 represent ()
1377 | 'f' ->
1378 begin match state.fullscreen with
1379 | None ->
1380 state.fullscreen <- Some (conf.winw, conf.winh);
1381 Glut.fullScreen ()
1382 | Some (w, h) ->
1383 state.fullscreen <- None;
1384 doreshape w h
1387 | 'g' ->
1388 gotoy_and_clear_text 0
1390 | 'n' ->
1391 search state.searchpattern true
1393 | 'p' | 'N' ->
1394 search state.searchpattern false
1396 | 't' ->
1397 begin match state.layout with
1398 | [] -> ()
1399 | l :: _ ->
1400 gotoy_and_clear_text (getpagey l.pageno)
1403 | ' ' ->
1404 begin match List.rev state.layout with
1405 | [] -> ()
1406 | l :: _ ->
1407 let pageno = min (l.pageno+1) (state.pagecount-1) in
1408 gotoy_and_clear_text (getpagey pageno)
1411 | '\127' ->
1412 begin match state.layout with
1413 | [] -> ()
1414 | l :: _ ->
1415 let pageno = max 0 (l.pageno-1) in
1416 gotoy_and_clear_text (getpagey pageno)
1419 | '=' ->
1420 let f (fn, ln) l =
1421 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1423 let fn, ln = List.fold_left f (-1, -1) state.layout in
1424 let s =
1425 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1426 let percent =
1427 if maxy <= 0
1428 then 100.
1429 else (100. *. (float state.y /. float maxy)) in
1430 if fn = ln
1431 then
1432 Printf.sprintf "Page %d of %d %.2f%%"
1433 (fn+1) state.pagecount percent
1434 else
1435 Printf.sprintf
1436 "Pages %d-%d of %d %.2f%%"
1437 (fn+1) (ln+1) state.pagecount percent
1439 showtext ' ' s;
1441 | 'w' ->
1442 begin match state.layout with
1443 | [] -> ()
1444 | l :: _ ->
1445 doreshape (l.pagew + conf.scrollw) l.pageh;
1446 Glut.postRedisplay ();
1449 | '\'' ->
1450 enterbookmarkmode ()
1452 | 'm' ->
1453 let ondone s =
1454 match state.layout with
1455 | l :: _ ->
1456 state.bookmarks <-
1457 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1458 :: state.bookmarks
1459 | _ -> ()
1461 enttext (Some ('~', "", None, textentry, ondone))
1463 | '~' ->
1464 quickbookmark ();
1465 showtext ' ' "Quick bookmark added";
1467 | 'z' ->
1468 begin match state.layout with
1469 | l :: _ ->
1470 let rect = getpdimrect l.pagedimno in
1471 let w, h =
1472 if conf.crophack
1473 then
1474 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1475 truncate (1.2 *. (rect.(3) -. rect.(0))))
1476 else
1477 (truncate (rect.(1) -. rect.(0)),
1478 truncate (rect.(3) -. rect.(0)))
1480 if w != 0 && h != 0
1481 then
1482 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1484 Glut.postRedisplay ();
1486 | [] -> ()
1489 | '<' | '>' ->
1490 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1492 | '[' | ']' ->
1493 state.colorscale <-
1494 max 0.0
1495 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1496 Glut.postRedisplay ()
1498 | 'k' -> gotoy (clamp (-conf.scrollincr))
1499 | 'j' -> gotoy (clamp conf.scrollincr)
1501 | 'r' -> opendoc state.path state.password
1503 | _ ->
1504 vlog "huh? %d %c" key (Char.chr key);
1507 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1508 let len = String.length text in
1509 if len = 0
1510 then (
1511 state.textentry <- None;
1512 Glut.postRedisplay ();
1514 else (
1515 let s = String.sub text 0 (len - 1) in
1516 enttext (Some (c, s, opthist, onkey, ondone))
1519 | Some (c, text, onhist, onkey, ondone) ->
1520 begin match Char.unsafe_chr key with
1521 | '\r' | '\n' ->
1522 ondone text;
1523 state.textentry <- None;
1524 Glut.postRedisplay ()
1526 | '\027' ->
1527 begin match onhist with
1528 | None -> ()
1529 | Some (_, onhistcancel) -> onhistcancel ()
1530 end;
1531 state.textentry <- None;
1532 Glut.postRedisplay ()
1534 | _ ->
1535 begin match onkey text key with
1536 | TEdone text ->
1537 state.textentry <- None;
1538 ondone text;
1539 Glut.postRedisplay ()
1541 | TEcont text ->
1542 enttext (Some (c, text, onhist, onkey, ondone));
1544 | TEstop ->
1545 state.textentry <- None;
1546 Glut.postRedisplay ()
1548 | TEswitch te ->
1549 state.textentry <- Some te;
1550 Glut.postRedisplay ()
1551 end;
1552 end;
1555 let narrow outlines pattern =
1556 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1557 match reopt with
1558 | None -> None
1559 | Some re ->
1560 let rec fold accu n =
1561 if n = -1
1562 then accu
1563 else
1564 let (s, _, _, _) as o = outlines.(n) in
1565 let accu =
1566 if (try ignore (Str.search_forward re s 0); true
1567 with Not_found -> false)
1568 then (o :: accu)
1569 else accu
1571 fold accu (n-1)
1573 let matched = fold [] (Array.length outlines - 1) in
1574 if matched = [] then None else Some (Array.of_list matched)
1577 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1578 let search active pattern incr =
1579 let dosearch re =
1580 let rec loop n =
1581 if n = Array.length outlines || n = -1
1582 then None
1583 else
1584 let (s, _, _, _) = outlines.(n) in
1586 (try ignore (Str.search_forward re s 0); true
1587 with Not_found -> false)
1588 then Some n
1589 else loop (n + incr)
1591 loop active
1594 let re = Str.regexp_case_fold pattern in
1595 dosearch re
1596 with Failure s ->
1597 state.text <- s;
1598 None
1600 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1601 match key with
1602 | 27 ->
1603 if String.length qsearch = 0
1604 then (
1605 state.text <- "";
1606 state.outline <- None;
1607 Glut.postRedisplay ();
1609 else (
1610 state.text <- "";
1611 state.outline <- Some (allowdel, active, first, outlines, "");
1612 Glut.postRedisplay ();
1615 | 18 | 19 ->
1616 let incr = if key = 18 then -1 else 1 in
1617 let active, first =
1618 match search (active + incr) qsearch incr 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);
1627 Glut.postRedisplay ();
1629 | 8 ->
1630 let len = String.length qsearch in
1631 if len = 0
1632 then ()
1633 else (
1634 if len = 1
1635 then (
1636 state.text <- "";
1637 state.outline <- Some (allowdel, active, first, outlines, "");
1639 else
1640 let qsearch = String.sub qsearch 0 (len - 1) in
1641 let active, first =
1642 match search active qsearch ~-1 with
1643 | None ->
1644 state.text <- qsearch ^ " [not found]";
1645 active, first
1646 | Some active ->
1647 state.text <- qsearch;
1648 active, firstof active
1650 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1652 Glut.postRedisplay ()
1654 | 13 ->
1655 if active < Array.length outlines
1656 then (
1657 let (_, _, n, t) = outlines.(active) in
1658 gotopage n t;
1660 state.text <- "";
1661 if allowdel then state.bookmarks <- Array.to_list outlines;
1662 state.outline <- None;
1663 Glut.postRedisplay ();
1665 | _ when key >= 32 && key < 127 ->
1666 let pattern = addchar qsearch (Char.chr key) in
1667 let active, first =
1668 match search active pattern 1 with
1669 | None ->
1670 state.text <- pattern ^ " [not found]";
1671 active, first
1672 | Some active ->
1673 state.text <- pattern;
1674 active, firstof active
1676 state.outline <- Some (allowdel, active, first, outlines, pattern);
1677 Glut.postRedisplay ()
1679 | 14 when not allowdel -> (* ctrl-n *)
1680 if String.length qsearch > 0
1681 then (
1682 let optoutlines = narrow outlines qsearch in
1683 begin match optoutlines with
1684 | None -> state.text <- "can't narrow"
1685 | Some outlines ->
1686 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1687 match state.outlines with
1688 | Olist l -> ()
1689 | Oarray a ->
1690 state.outlines <- Onarrow (qsearch, outlines, a)
1691 | Onarrow (pat, a, b) ->
1692 state.outlines <- Onarrow (qsearch, outlines, b)
1693 end;
1695 Glut.postRedisplay ()
1697 | 21 when not allowdel -> (* ctrl-u *)
1698 let outline =
1699 match state.outlines with
1700 | Oarray a -> a
1701 | Olist l ->
1702 let a = Array.of_list (List.rev l) in
1703 state.outlines <- Oarray a;
1705 | Onarrow (pat, a, b) ->
1706 state.outlines <- Oarray b;
1707 state.text <- "";
1710 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1711 Glut.postRedisplay ()
1713 | 12 ->
1714 state.outline <-
1715 Some (allowdel, active, firstof active, outlines, qsearch);
1716 Glut.postRedisplay ()
1718 | 127 when allowdel ->
1719 let len = Array.length outlines - 1 in
1720 if len = 0
1721 then (
1722 state.outline <- None;
1723 state.bookmarks <- [];
1725 else (
1726 let bookmarks = Array.init len
1727 (fun i ->
1728 let i = if i >= active then i + 1 else i in
1729 outlines.(i)
1732 state.outline <-
1733 Some (allowdel,
1734 min active (len-1),
1735 min first (len-1),
1736 bookmarks, qsearch)
1739 Glut.postRedisplay ()
1741 | _ -> log "unknown key %d" key
1744 let keyboard ~key ~x ~y =
1745 if key = 7
1746 then
1747 wcmd "interrupt" []
1748 else
1749 match state.outline with
1750 | None -> viewkeyboard ~key ~x ~y
1751 | Some outline -> outlinekeyboard ~key ~x ~y outline
1754 let special ~key ~x ~y =
1755 match state.outline with
1756 | None when state.birdseye <> None ->
1757 begin match key with
1758 | Glut.KEY_UP ->
1759 let pageno = max 0 (state.birdseyepageno - 1) in
1760 state.birdseyepageno <- pageno;
1761 if not (pagevisible state.layout pageno)
1762 then gotopage pageno 0.0
1763 else Glut.postRedisplay ();
1765 | Glut.KEY_DOWN ->
1766 let pageno = min (state.pagecount - 1) (state.birdseyepageno + 1) in
1767 state.birdseyepageno <- pageno;
1768 if not (pagevisible state.layout pageno)
1769 then
1770 begin match List.rev state.layout with
1771 | [] -> gotopage pageno 0.0
1772 | l :: _ ->
1773 gotoy (state.y + conf.interpagespace + l.pageh*2 - l.pagevh)
1775 else Glut.postRedisplay ();
1777 | Glut.KEY_PAGE_UP ->
1778 begin match state.layout with
1779 | l :: _ ->
1780 if l.pageno = state.birdseyepageno
1781 then (
1782 match layout (state.y - conf.winh) conf.winh with
1783 | [] -> gotoy (clamp (-conf.winh))
1784 | l :: _ ->
1785 state.birdseyepageno <- max 0 (l.pageno - 1);
1786 gotopage state.birdseyepageno 0.0
1788 else (
1789 state.birdseyepageno <- max 0 (l.pageno - 1);
1790 gotopage state.birdseyepageno 0.0
1792 | [] -> gotoy (clamp (-conf.winh))
1793 end;
1794 | Glut.KEY_PAGE_DOWN ->
1795 begin match List.rev state.layout with
1796 | l :: _ ->
1797 state.birdseyepageno <- min (state.pagecount - 1) (l.pageno + 1);
1798 gotoy (clamp (l.pagedispy + l.pageh))
1799 | [] -> gotoy (clamp conf.winh)
1800 end;
1802 | Glut.KEY_HOME ->
1803 state.birdseyepageno <- 0;
1804 gotopage 0 0.0
1805 | Glut.KEY_END ->
1806 state.birdseyepageno <- state.pagecount - 1;
1807 if not (pagevisible state.layout state.birdseyepageno)
1808 then
1809 gotopage state.birdseyepageno 0.0
1810 else
1811 Glut.postRedisplay ()
1813 | _ -> ()
1816 | None ->
1817 begin match state.textentry with
1818 | None ->
1819 let y =
1820 match key with
1821 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1822 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1823 | Glut.KEY_DOWN -> clamp conf.scrollincr
1824 | Glut.KEY_PAGE_UP ->
1825 if Glut.getModifiers () land Glut.active_ctrl != 0
1826 then
1827 match state.layout with
1828 | [] -> state.y
1829 | l :: _ -> state.y - l.pagey
1830 else
1831 clamp (-conf.winh)
1832 | Glut.KEY_PAGE_DOWN ->
1833 if Glut.getModifiers () land Glut.active_ctrl != 0
1834 then
1835 match List.rev state.layout with
1836 | [] -> state.y
1837 | l :: _ -> getpagey l.pageno
1838 else
1839 clamp conf.winh
1840 | Glut.KEY_HOME -> addnav (); 0
1841 | Glut.KEY_END ->
1842 addnav ();
1843 state.maxy - (if conf.maxhfit then conf.winh else 0)
1845 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1846 state.x <- state.x - 10;
1847 state.y
1848 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1849 state.x <- state.x + 10;
1850 state.y
1852 | _ -> state.y
1854 gotoy_and_clear_text y
1856 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1857 let s =
1858 match key with
1859 | Glut.KEY_UP -> action HCprev
1860 | Glut.KEY_DOWN -> action HCnext
1861 | Glut.KEY_HOME -> action HCfirst
1862 | Glut.KEY_END -> action HClast
1863 | _ -> state.text
1865 state.textentry <- Some (c, s, onhist, onkey, ondone);
1866 Glut.postRedisplay ()
1868 | _ -> ()
1871 | Some (allowdel, active, first, outlines, qsearch) ->
1872 let maxrows = maxoutlinerows () in
1873 let calcfirst first active =
1874 if active > first
1875 then
1876 let rows = active - first in
1877 if rows > maxrows then active - maxrows else first
1878 else active
1880 let navigate incr =
1881 let active = active + incr in
1882 let active = max 0 (min active (Array.length outlines - 1)) in
1883 let first = calcfirst first active in
1884 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1885 Glut.postRedisplay ()
1887 let updownlevel incr =
1888 let len = Array.length outlines in
1889 let (_, curlevel, _, _) = outlines.(active) in
1890 let rec flow i =
1891 if i = len then i-1 else if i = -1 then 0 else
1892 let (_, l, _, _) = outlines.(i) in
1893 if l != curlevel then i else flow (i+incr)
1895 let active = flow active in
1896 let first = calcfirst first active in
1897 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1898 Glut.postRedisplay ()
1900 match key with
1901 | Glut.KEY_UP -> navigate ~-1
1902 | Glut.KEY_DOWN -> navigate 1
1903 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1904 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1906 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1907 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1909 | Glut.KEY_HOME ->
1910 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1911 Glut.postRedisplay ()
1913 | Glut.KEY_END ->
1914 let active = Array.length outlines - 1 in
1915 let first = max 0 (active - maxrows) in
1916 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1917 Glut.postRedisplay ()
1919 | _ -> ()
1922 let drawplaceholder l =
1923 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1924 GlDraw.rect
1925 (float l.pagex, float l.pagedispy)
1926 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1928 let x = float (if margin < 0 then -margin else l.pagex)
1929 and y = float (l.pagedispy + 13) in
1930 let font = Glut.BITMAP_8_BY_13 in
1931 GlDraw.color (0.0, 0.0, 0.0);
1932 GlPix.raster_pos ~x ~y ();
1933 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1934 ("Loading " ^ string_of_int (l.pageno + 1));
1937 let now () = Unix.gettimeofday ();;
1939 let drawpage l =
1940 if state.textentry = None
1941 then (
1942 match state.birdseye with
1943 | None -> GlDraw.color (scalecolor 1.0);
1944 | Some (_, _, hooverpageno) ->
1945 let color =
1946 if l.pageno = state.birdseyepageno
1947 then 1.0
1948 else (
1949 if l.pageno = hooverpageno
1950 then 0.9
1951 else 0.8
1954 GlDraw.color (scalecolor color);
1956 else GlDraw.color (scalecolor 0.4);
1957 begin match getopaque l.pageno with
1958 | Some (opaque, _) when validopaque opaque ->
1959 let a = now () in
1960 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1961 opaque;
1962 let b = now () in
1963 let d = b-.a in
1964 vlog "draw %d %f sec" l.pageno d;
1966 | _ ->
1967 drawplaceholder l;
1968 end;
1971 let scrollph y =
1972 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1973 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1974 let sh = float conf.winh /. sh in
1975 let sh = max sh (float conf.scrollh) in
1977 let percent =
1978 if state.y = state.maxy
1979 then 1.0
1980 else float y /. float maxy
1982 let position = (float conf.winh -. sh) *. percent in
1984 let position =
1985 if position +. sh > float conf.winh
1986 then float conf.winh -. sh
1987 else position
1989 position, sh;
1992 let scrollindicator () =
1993 GlDraw.color (0.64 , 0.64, 0.64);
1994 GlDraw.rect
1995 (float (conf.winw - conf.scrollw), 0.)
1996 (float conf.winw, float conf.winh)
1998 GlDraw.color (0.0, 0.0, 0.0);
2000 let position, sh = scrollph state.y in
2001 GlDraw.rect
2002 (float (conf.winw - conf.scrollw), position)
2003 (float conf.winw, position +. sh)
2007 let showsel margin =
2008 match state.mstate with
2009 | Mnone | Mscroll _ | Mpan _ ->
2012 | Msel ((x0, y0), (x1, y1)) ->
2013 let rec loop = function
2014 | l :: ls ->
2015 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2016 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2017 then
2018 match getopaque l.pageno with
2019 | Some (opaque, _) when validopaque opaque ->
2020 let oy = -l.pagey + l.pagedispy in
2021 seltext opaque
2022 (x0 - margin - state.x, y0,
2023 x1 - margin - state.x, y1) oy;
2025 | _ -> ()
2026 else loop ls
2027 | [] -> ()
2029 loop state.layout
2032 let showrects () =
2033 let panx = float state.x in
2034 Gl.enable `blend;
2035 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2036 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2037 List.iter
2038 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2039 List.iter (fun l ->
2040 if l.pageno = pageno
2041 then (
2042 let d = float (l.pagedispy - l.pagey) in
2043 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2044 GlDraw.begins `quads;
2046 GlDraw.vertex2 (x0+.panx, y0+.d);
2047 GlDraw.vertex2 (x1+.panx, y1+.d);
2048 GlDraw.vertex2 (x2+.panx, y2+.d);
2049 GlDraw.vertex2 (x3+.panx, y3+.d);
2051 GlDraw.ends ();
2053 ) state.layout
2054 ) state.rects
2056 Gl.disable `blend;
2059 let showoutline = function
2060 | None -> ()
2061 | Some (allowdel, active, first, outlines, qsearch) ->
2062 Gl.enable `blend;
2063 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2064 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2065 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2066 Gl.disable `blend;
2068 GlDraw.color (1., 1., 1.);
2069 let font = Glut.BITMAP_9_BY_15 in
2070 let draw_string x y s =
2071 GlPix.raster_pos ~x ~y ();
2072 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2074 let rec loop row =
2075 if row = Array.length outlines || (row - first) * 16 > conf.winh
2076 then ()
2077 else (
2078 let (s, l, _, _) = outlines.(row) in
2079 let y = (row - first) * 16 in
2080 let x = 5 + 15*l in
2081 if row = active
2082 then (
2083 Gl.enable `blend;
2084 GlDraw.polygon_mode `both `line;
2085 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2086 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2087 GlDraw.rect (0., float (y + 1))
2088 (float (conf.winw - 1), float (y + 18));
2089 GlDraw.polygon_mode `both `fill;
2090 Gl.disable `blend;
2091 GlDraw.color (1., 1., 1.);
2093 draw_string (float x) (float (y + 16)) s;
2094 loop (row+1)
2097 loop first
2100 let display () =
2101 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2102 GlDraw.viewport margin 0 state.w conf.winh;
2103 pagematrix ();
2104 GlClear.color (scalecolor 0.5);
2105 GlClear.clear [`color];
2106 if state.x != 0
2107 then (
2108 let x = float state.x in
2109 GlMat.translate ~x ();
2111 if conf.zoom > 1.0
2112 then (
2113 Gl.enable `scissor_test;
2114 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2116 List.iter drawpage state.layout;
2117 if conf.zoom > 1.0
2118 then
2119 Gl.disable `scissor_test
2121 if state.x != 0
2122 then (
2123 let x = -.float state.x in
2124 GlMat.translate ~x ();
2126 showrects ();
2127 showsel margin;
2128 GlDraw.viewport 0 0 conf.winw conf.winh;
2129 winmatrix ();
2130 scrollindicator ();
2131 showoutline state.outline;
2132 enttext ();
2133 Glut.swapBuffers ();
2136 let getunder x y =
2137 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2138 let x = x - margin - state.x in
2139 let rec f = function
2140 | l :: rest ->
2141 begin match getopaque l.pageno with
2142 | Some (opaque, _) when validopaque opaque ->
2143 let y = y - l.pagedispy in
2144 if y > 0
2145 then
2146 let y = l.pagey + y in
2147 let x = x - l.pagex in
2148 match whatsunder opaque x y with
2149 | Unone -> f rest
2150 | under -> under
2151 else
2152 f rest
2153 | _ ->
2154 f rest
2156 | [] -> Unone
2158 f state.layout
2161 let mouse ~button ~bstate ~x ~y =
2162 match button with
2163 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2164 let incr =
2165 if n = 3
2166 then
2167 -conf.scrollincr
2168 else
2169 conf.scrollincr
2171 let incr = incr * 2 in
2172 let y = clamp incr in
2173 gotoy_and_clear_text y
2175 | Glut.LEFT_BUTTON when state.outline = None
2176 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2177 if bstate = Glut.DOWN
2178 then (
2179 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2180 state.mstate <- Mpan (x, y)
2182 else
2183 state.mstate <- Mnone
2185 | Glut.LEFT_BUTTON
2186 when state.outline = None && x > conf.winw - conf.scrollw ->
2187 if bstate = Glut.DOWN
2188 then
2189 let position, sh = scrollph state.y in
2190 if y > truncate position && y < truncate (position +. sh)
2191 then
2192 state.mstate <- Mscroll
2193 else
2194 let percent = float y /. float conf.winh in
2195 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2196 gotoy desty;
2197 state.mstate <- Mscroll
2198 else
2199 state.mstate <- Mnone
2201 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2202 begin match state.birdseye with
2203 | Some vals ->
2204 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2205 let rec loop = function
2206 | [] -> ()
2207 | l :: rest ->
2208 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2209 && x > margin && x < margin + l.pagew
2210 then (
2211 state.birdseyepageno <- l.pageno;
2212 birdseyeoff vals;
2213 reshape conf.winw conf.winh;
2215 else loop rest
2217 loop state.layout;
2218 | None -> () (* impossible *)
2221 | Glut.LEFT_BUTTON when state.outline = None ->
2222 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2223 begin match dest with
2224 | Ulinkgoto (pageno, top) ->
2225 if pageno >= 0
2226 then
2227 gotopage1 pageno top
2229 | Ulinkuri s ->
2230 print_endline s
2232 | Unone when bstate = Glut.DOWN ->
2233 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2234 state.mstate <- Mpan (x, y);
2236 | Unone | Utext _ ->
2237 if bstate = Glut.DOWN
2238 then (
2239 if conf.angle mod 360 = 0
2240 then (
2241 state.mstate <- Msel ((x, y), (x, y));
2242 Glut.postRedisplay ()
2245 else (
2246 match state.mstate with
2247 | Mnone -> ()
2249 | Mscroll ->
2250 state.mstate <- Mnone
2252 | Mpan _ ->
2253 Glut.setCursor Glut.CURSOR_INHERIT;
2254 state.mstate <- Mnone
2256 | Msel ((x0, y0), (x1, y1)) ->
2257 let f l =
2258 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2259 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2260 then
2261 match getopaque l.pageno with
2262 | Some (opaque, _) when validopaque opaque ->
2263 copysel opaque
2264 | _ -> ()
2266 List.iter f state.layout;
2267 copysel ""; (* ugly *)
2268 Glut.setCursor Glut.CURSOR_INHERIT;
2269 state.mstate <- Mnone;
2273 | _ ->
2276 let mouse ~button ~state ~x ~y = mouse button state x y;;
2278 let motion ~x ~y =
2279 if state.outline = None
2280 then
2281 match state.mstate with
2282 | Mnone -> ()
2284 | Mpan (x0, y0) ->
2285 let dx = x - x0
2286 and dy = y0 - y in
2287 state.mstate <- Mpan (x, y);
2288 if conf.zoom > 1.0 then state.x <- state.x + dx;
2289 let y = clamp dy in
2290 gotoy_and_clear_text y
2292 | Msel (a, _) ->
2293 state.mstate <- Msel (a, (x, y));
2294 Glut.postRedisplay ()
2296 | Mscroll ->
2297 let y = min conf.winh (max 0 y) in
2298 let percent = float y /. float conf.winh in
2299 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2300 gotoy_and_clear_text y
2303 let pmotion ~x ~y =
2304 match state.birdseye with
2305 | Some (conf, leftx, hooverpageno) ->
2306 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2307 let rec loop = function
2308 | [] ->
2309 if hooverpageno != -1
2310 then (
2311 state.birdseye <- Some (conf, leftx, -1);
2312 Glut.postRedisplay ();
2314 | l :: rest ->
2315 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2316 && x > margin && x < margin + l.pagew
2317 then (
2318 state.birdseye <- Some (conf, leftx, l.pageno);
2319 Glut.postRedisplay ();
2321 else loop rest
2323 loop state.layout
2325 | None ->
2326 if state.outline = None
2327 then
2328 match state.mstate with
2329 | Mnone ->
2330 begin match getunder x y with
2331 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2332 | Ulinkuri uri ->
2333 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2334 Glut.setCursor Glut.CURSOR_INFO
2335 | Ulinkgoto (page, y) ->
2336 if conf.underinfo
2337 then showtext 'p' ("age: " ^ string_of_int page);
2338 Glut.setCursor Glut.CURSOR_INFO
2339 | Utext s ->
2340 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2341 Glut.setCursor Glut.CURSOR_TEXT
2344 | Mpan _ | Msel _ | Mscroll ->
2349 module State =
2350 struct
2351 open Parser
2353 let home =
2355 match Sys.os_type with
2356 | "Win32" -> Sys.getenv "HOMEPATH"
2357 | _ -> Sys.getenv "HOME"
2358 with exn ->
2359 prerr_endline
2360 ("Can not determine home directory location: " ^
2361 Printexc.to_string exn);
2365 let config_of c attrs =
2366 let apply c k v =
2368 match k with
2369 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2370 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2371 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2372 | "preload" -> { c with preload = bool_of_string v }
2373 | "page-bias" -> { c with pagebias = int_of_string v }
2374 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2375 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2376 | "crop-hack" -> { c with crophack = bool_of_string v }
2377 | "throttle" -> { c with showall = bool_of_string v }
2378 | "highlight-links" -> { c with hlinks = bool_of_string v }
2379 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2380 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2381 | "zoom" ->
2382 let zoom = float_of_string v /. 100. in
2383 let zoom = max 0.01 (min 2.2 zoom) in
2384 { c with zoom = zoom }
2385 | "presentation" -> { c with presentation = bool_of_string v }
2386 | "rotation-angle" -> { c with angle = int_of_string v }
2387 | "width" -> { c with winw = max 20 (int_of_string v) }
2388 | "height" -> { c with winh = max 20 (int_of_string v) }
2389 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2390 | "proportional-display" -> { c with proportional = bool_of_string v }
2391 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2392 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2393 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2394 | _ -> c
2395 with exn ->
2396 prerr_endline ("Error processing attribute (`" ^
2397 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2400 let rec fold c = function
2401 | [] -> c
2402 | (k, v) :: rest ->
2403 let c = apply c k v in
2404 fold c rest
2406 fold c attrs;
2409 let bookmark_of attrs =
2410 let rec fold title page rely = function
2411 | ("title", v) :: rest -> fold v page rely rest
2412 | ("page", v) :: rest -> fold title v rely rest
2413 | ("rely", v) :: rest -> fold title page v rest
2414 | _ :: rest -> fold title page rely rest
2415 | [] -> title, page, rely
2417 fold "invalid" "0" "0" attrs
2420 let setconf dst src =
2421 dst.scrollw <- src.scrollw;
2422 dst.scrollh <- src.scrollh;
2423 dst.icase <- src.icase;
2424 dst.preload <- src.preload;
2425 dst.pagebias <- src.pagebias;
2426 dst.verbose <- src.verbose;
2427 dst.scrollincr <- src.scrollincr;
2428 dst.maxhfit <- src.maxhfit;
2429 dst.crophack <- src.crophack;
2430 dst.autoscroll <- src.autoscroll;
2431 dst.showall <- src.showall;
2432 dst.hlinks <- src.hlinks;
2433 dst.underinfo <- src.underinfo;
2434 dst.interpagespace <- src.interpagespace;
2435 dst.zoom <- src.zoom;
2436 dst.presentation <- src.presentation;
2437 dst.angle <- src.angle;
2438 dst.winw <- src.winw;
2439 dst.winh <- src.winh;
2440 dst.savebmarks <- src.savebmarks;
2441 dst.memlimit <- src.memlimit;
2442 dst.proportional <- src.proportional;
2443 dst.texcount <- src.texcount;
2444 dst.sliceheight <- src.sliceheight;
2447 let unent s =
2448 let l = String.length s in
2449 let b = Buffer.create l in
2450 unent b s 0 l;
2451 Buffer.contents b;
2454 let get s =
2455 let h = Hashtbl.create 10 in
2456 let dc = { defconf with angle = defconf.angle } in
2457 let rec toplevel v t spos epos =
2458 match t with
2459 | Vdata | Vcdata | Vend -> v
2460 | Vopen ("llppconfig", attrs, closed) ->
2461 if closed
2462 then v
2463 else { v with f = llppconfig }
2464 | Vopen _ ->
2465 error "unexpected subelement at top level" s spos
2466 | Vclose tag -> error "unexpected close at top level" s spos
2468 and llppconfig v t spos epos =
2469 match t with
2470 | Vdata | Vcdata | Vend -> v
2471 | Vopen ("defaults", attrs, closed) ->
2472 let c = config_of dc attrs in
2473 setconf dc c;
2474 if closed
2475 then v
2476 else { v with f = skip "defaults" (fun () -> v) }
2478 | Vopen ("doc", attrs, closed) ->
2479 let pathent =
2481 List.assoc "path" attrs
2482 with Not_found -> error "doc is missing path attribute" s spos
2484 let path = unent pathent in
2485 let c = config_of dc attrs in
2486 let y =
2488 float_of_string (List.assoc "rely" attrs)
2489 with
2490 | Not_found -> 0.0
2491 | exn ->
2492 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2495 let x =
2497 int_of_string (List.assoc "pan" attrs)
2498 with
2499 | Not_found -> 0
2500 | exn ->
2501 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2504 if closed
2505 then (Hashtbl.add h path (c, [], x, y); v)
2506 else { v with f = doc path x y c [] }
2508 | Vopen (tag, _, closed) ->
2509 error "unexpected subelement in llppconfig" s spos
2511 | Vclose "llppconfig" -> { v with f = toplevel }
2512 | Vclose tag -> error "unexpected close in llppconfig" s spos
2514 and doc path x y c bookmarks v t spos epos =
2515 match t with
2516 | Vdata | Vcdata -> v
2517 | Vend -> error "unexpected end of input in doc" s spos
2518 | Vopen ("bookmarks", attrs, closed) ->
2519 { v with f = pbookmarks path x y c bookmarks }
2521 | Vopen (tag, _, _) ->
2522 error "unexpected subelement in doc" s spos
2524 | Vclose "doc" ->
2525 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2526 { v with f = llppconfig }
2528 | Vclose tag -> error "unexpected close in doc" s spos
2530 and pbookmarks path x y c bookmarks v t spos epos =
2531 match t with
2532 | Vdata | Vcdata -> v
2533 | Vend -> error "unexpected end of input in bookmarks" s spos
2534 | Vopen ("item", attrs, closed) ->
2535 let titleent, spage, srely = bookmark_of attrs in
2536 let page =
2538 int_of_string spage
2539 with exn ->
2540 dolog "Failed to convert page %S to integer: %s"
2541 spage (Printexc.to_string exn);
2544 let rely =
2546 float_of_string srely
2547 with exn ->
2548 dolog "Failed to convert rely %S to real: %s"
2549 srely (Printexc.to_string exn);
2552 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2553 if closed
2554 then { v with f = pbookmarks path x y c bookmarks }
2555 else
2556 let f () = v in
2557 { v with f = skip "item" f }
2559 | Vopen _ ->
2560 error "unexpected subelement in bookmarks" s spos
2562 | Vclose "bookmarks" ->
2563 { v with f = doc path x y c bookmarks }
2565 | Vclose tag -> error "unexpected close in bookmarks" s spos
2567 and skip tag f v t spos epos =
2568 match t with
2569 | Vdata | Vcdata -> v
2570 | Vend ->
2571 error ("unexpected end of input in skipped " ^ tag) s spos
2572 | Vopen (tag', _, closed) ->
2573 if closed
2574 then v
2575 else
2576 let f' () = { v with f = skip tag f } in
2577 { v with f = skip tag' f' }
2578 | Vclose ctag ->
2579 if tag = ctag
2580 then f ()
2581 else error ("unexpected close in skipped " ^ tag) s spos
2584 parse { f = toplevel; accu = () } s;
2585 h, dc;
2588 let do_load f ic =
2590 let len = in_channel_length ic in
2591 let s = String.create len in
2592 really_input ic s 0 len;
2593 f s;
2594 with
2595 | Parse_error (msg, s, pos) ->
2596 let subs = subs s pos in
2597 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2598 failwith ("parse error: " ^ s)
2600 | exn ->
2601 failwith ("config load error: " ^ Printexc.to_string exn)
2604 let path =
2605 let dir =
2607 let dir = Filename.concat home ".config" in
2608 if Sys.is_directory dir then dir else home
2609 with _ -> home
2611 Filename.concat dir "llpp.conf"
2614 let load1 f =
2615 if Sys.file_exists path
2616 then
2617 match
2618 (try Some (open_in_bin path)
2619 with exn ->
2620 prerr_endline
2621 ("Error opening configuation file `" ^ path ^ "': " ^
2622 Printexc.to_string exn);
2623 None
2625 with
2626 | Some ic ->
2627 begin try
2628 f (do_load get ic)
2629 with exn ->
2630 prerr_endline
2631 ("Error loading configuation from `" ^ path ^ "': " ^
2632 Printexc.to_string exn);
2633 end;
2634 close_in ic;
2636 | None -> ()
2637 else
2638 f (Hashtbl.create 0, defconf)
2641 let load () =
2642 let f (h, dc) =
2643 let pc, pb, px, py =
2645 Hashtbl.find h state.path
2646 with Not_found -> dc, [], 0, 0.0
2648 setconf defconf dc;
2649 setconf conf pc;
2650 state.bookmarks <- pb;
2651 state.x <- px;
2652 cbput state.hists.nav py;
2654 load1 f
2657 let add_attrs bb always dc c =
2658 let ob s a b =
2659 if always || a != b
2660 then Printf.bprintf bb "\n %s='%b'" s a
2661 and oi s a b =
2662 if always || a != b
2663 then Printf.bprintf bb "\n %s='%d'" s a
2664 and oz s a b =
2665 if always || a <> b
2666 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2668 let w, h =
2669 if always
2670 then dc.winw, dc.winh
2671 else
2672 match state.fullscreen with
2673 | Some wh -> wh
2674 | None -> c.winw, c.winh
2676 let zoom, presentation, interpagespace, showall=
2677 if always
2678 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2679 else
2680 match state.birdseye with
2681 | Some (bc, _, _) ->
2682 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2683 | None -> c.zoom, c.presentation, c.interpagespace, c.showall
2685 oi "width" w dc.winw;
2686 oi "height" h dc.winh;
2687 oi "scroll-bar-width" c.scrollw dc.scrollw;
2688 oi "scroll-handle-height" c.scrollh dc.scrollh;
2689 ob "case-insensitive-search" c.icase dc.icase;
2690 ob "preload" c.preload dc.preload;
2691 oi "page-bias" c.pagebias dc.pagebias;
2692 oi "scroll-step" c.scrollincr dc.scrollincr;
2693 ob "max-height-fit" c.maxhfit dc.maxhfit;
2694 ob "crop-hack" c.crophack dc.crophack;
2695 ob "throttle" showall dc.showall;
2696 ob "highlight-links" c.hlinks dc.hlinks;
2697 ob "under-cursor-info" c.underinfo dc.underinfo;
2698 oi "vertical-margin" interpagespace dc.interpagespace;
2699 oz "zoom" zoom dc.zoom;
2700 ob "presentation" presentation dc.presentation;
2701 oi "rotation-angle" c.angle dc.angle;
2702 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2703 ob "proportional-display" c.proportional dc.proportional;
2704 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2705 oi "texcount" c.texcount dc.texcount;
2706 oi "slice-height" c.sliceheight dc.sliceheight;
2709 let save () =
2710 let bb = Buffer.create 32768 in
2711 let f (h, dc) =
2712 Buffer.add_string bb "<llppconfig>\n<defaults ";
2713 add_attrs bb true dc dc;
2714 Buffer.add_string bb "/>\n";
2716 let adddoc path x y c bookmarks =
2717 if bookmarks == [] && c = dc && y = 0.0
2718 then ()
2719 else (
2720 Printf.bprintf bb "<doc path='%s'"
2721 (enent path 0 (String.length path));
2723 if y <> 0.0
2724 then Printf.bprintf bb " rely='%f'" y;
2726 if x != 0
2727 then Printf.bprintf bb " pan='%d'" x;
2729 add_attrs bb false dc c;
2731 begin match bookmarks with
2732 | [] -> Buffer.add_string bb "/>\n"
2733 | _ ->
2734 Buffer.add_string bb ">\n<bookmarks>\n";
2735 List.iter (fun (title, _level, page, rely) ->
2736 Printf.bprintf bb
2737 "<item title='%s' page='%d' rely='%f'/>\n"
2738 (enent title 0 (String.length title))
2739 page
2740 rely
2741 ) bookmarks;
2742 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2743 end;
2747 let x =
2748 match state.birdseye with
2749 | Some (_, x, _) -> x
2750 | None -> state.x
2752 adddoc state.path x (yratio state.y) conf
2753 (if conf.savebmarks then state.bookmarks else []);
2755 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2756 if path <> state.path
2757 then
2758 adddoc path x y c bookmarks
2759 ) h;
2760 Buffer.add_string bb "</llppconfig>";
2762 load1 f;
2763 if Buffer.length bb > 0
2764 then
2766 let tmp = path ^ ".tmp" in
2767 let oc = open_out_bin tmp in
2768 Buffer.output_buffer oc bb;
2769 close_out oc;
2770 Sys.rename tmp path;
2771 with exn ->
2772 prerr_endline
2773 ("error while saving configuration: " ^ Printexc.to_string exn)
2775 end;;
2777 let () =
2778 Arg.parse
2779 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2780 (fun s -> state.path <- s)
2781 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2783 let path =
2784 if String.length state.path = 0
2785 then (prerr_endline "filename missing"; exit 1)
2786 else (
2787 if Filename.is_relative state.path
2788 then Filename.concat (Sys.getcwd ()) state.path
2789 else state.path
2792 state.path <- path;
2794 State.load ();
2796 let _ = Glut.init Sys.argv in
2797 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2798 let () = Glut.initWindowSize conf.winw conf.winh in
2799 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2801 let csock, ssock =
2802 if Sys.os_type = "Unix"
2803 then
2804 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2805 else
2806 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2807 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2808 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2809 Unix.bind sock addr;
2810 Unix.listen sock 1;
2811 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2812 Unix.connect csock addr;
2813 let ssock, _ = Unix.accept sock in
2814 Unix.close sock;
2815 let opts sock =
2816 Unix.setsockopt sock Unix.TCP_NODELAY true;
2817 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2819 opts ssock;
2820 opts csock;
2821 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2822 ssock, csock
2825 let () = Glut.displayFunc display in
2826 let () = Glut.reshapeFunc reshape in
2827 let () = Glut.keyboardFunc keyboard in
2828 let () = Glut.specialFunc special in
2829 let () = Glut.idleFunc (Some idle) in
2830 let () = Glut.mouseFunc mouse in
2831 let () = Glut.motionFunc motion in
2832 let () = Glut.passiveMotionFunc pmotion in
2834 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2835 state.csock <- csock;
2836 state.ssock <- ssock;
2837 state.text <- "Opening " ^ path;
2838 writeopen state.path state.password;
2840 at_exit State.save;
2842 let rec handlelablglutbug () =
2844 Glut.mainLoop ();
2845 with Glut.BadEnum "key in special_of_int" ->
2846 showtext '!' " LablGlut bug: special key not recognized";
2847 handlelablglutbug ()
2849 handlelablglutbug ();