Fix a bunch of issues with repositioning after state transitions
[llpp.git] / main.ml
blob02e6e63b100209172763255a64815c4426aa0ccb
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
25 and top = float
28 external init : Unix.file_descr -> params -> unit = "ml_init";;
29 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext : string -> (int * int * int * int) -> int -> unit =
31 "ml_seltext";;
32 external copysel : string -> unit = "ml_copysel";;
33 external getpdimrect : int -> float array = "ml_getpdimrect";;
34 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
35 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
37 type mpos = int * int
38 and mstate =
39 | Msel of (mpos * mpos)
40 | Mpan of mpos
41 | Mscroll
42 | Mnone
45 type 'a circbuf =
46 { store : 'a array
47 ; mutable rc : int
48 ; mutable wc : int
49 ; mutable len : int
53 type textentry = (char * string * onhist * onkey * ondone)
54 and onkey = string -> int -> te
55 and ondone = string -> unit
56 and histcancel = unit -> unit
57 and onhist = ((histcmd -> string) * histcancel) option
58 and histcmd = HCnext | HCprev | HCfirst | HClast
59 and te =
60 | TEstop
61 | TEdone of string
62 | TEcont of string
63 | TEswitch of textentry
66 let cbnew n v =
67 { store = Array.create n v
68 ; rc = 0
69 ; wc = 0
70 ; len = 0
74 let cbcap b = Array.length b.store;;
76 let cbput b v =
77 let cap = cbcap b in
78 b.store.(b.wc) <- v;
79 b.wc <- (b.wc + 1) mod cap;
80 b.rc <- b.wc;
81 b.len <- min (b.len + 1) cap;
84 let cbempty b = b.len = 0;;
86 let cbgetg b circular dir =
87 if cbempty b
88 then b.store.(0)
89 else
90 let rc = b.rc + dir in
91 let rc =
92 if circular
93 then (
94 if rc = -1
95 then b.len-1
96 else (
97 if rc = b.len
98 then 0
99 else rc
102 else max 0 (min rc (b.len-1))
104 b.rc <- rc;
105 b.store.(rc);
108 let cbget b = cbgetg b false;;
109 let cbgetc b = cbgetg b true;;
111 let cbpeek b =
112 let rc = b.wc - b.len in
113 let rc = if rc < 0 then cbcap b + rc else rc in
114 b.store.(rc);
117 let cbdecr b = b.len <- b.len - 1;;
119 type layout =
120 { pageno : int
121 ; pagedimno : int
122 ; pagew : int
123 ; pageh : int
124 ; pagedispy : int
125 ; pagey : int
126 ; pagevh : int
127 ; pagex : int
131 type conf =
132 { mutable scrollw : int
133 ; mutable scrollh : int
134 ; mutable icase : bool
135 ; mutable preload : bool
136 ; mutable pagebias : int
137 ; mutable verbose : bool
138 ; mutable scrollincr : int
139 ; mutable maxhfit : bool
140 ; mutable crophack : bool
141 ; mutable autoscroll : bool
142 ; mutable showall : bool
143 ; mutable hlinks : bool
144 ; mutable underinfo : bool
145 ; mutable interpagespace : interpagespace
146 ; mutable zoom : float
147 ; mutable presentation : bool
148 ; mutable angle : angle
149 ; mutable winw : int
150 ; mutable winh : int
151 ; mutable savebmarks : bool
152 ; mutable proportional : proportional
153 ; mutable memlimit : int
154 ; mutable texcount : texcount
155 ; mutable sliceheight : sliceheight
156 ; mutable thumbw : width
160 type outline = string * int * int * float;;
161 type outlines =
162 | Oarray of outline array
163 | Olist of outline list
164 | Onarrow of string * outline array * outline array
167 type rect = (float * float * float * float * float * float * float * float);;
169 type pagemapkey = (pageno * width * angle * proportional * gen);;
171 type anchor = pageno * top;;
173 type state =
174 { mutable csock : Unix.file_descr
175 ; mutable ssock : Unix.file_descr
176 ; mutable w : int
177 ; mutable x : int
178 ; mutable y : int
179 ; mutable anchor : anchor
180 ; mutable maxy : int
181 ; mutable layout : layout list
182 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
183 ; mutable pdims : (pageno * width * height * leftx) list
184 ; mutable pagecount : int
185 ; pagecache : string circbuf
186 ; mutable rendering : bool
187 ; mutable mstate : mstate
188 ; mutable searchpattern : string
189 ; mutable rects : (pageno * recttype * rect) list
190 ; mutable rects1 : (pageno * recttype * rect) list
191 ; mutable text : string
192 ; mutable fullscreen : (width * height) option
193 ; mutable birdseye : (conf * leftx * pageno * pageno) option
194 ; mutable textentry : textentry option
195 ; mutable outlines : outlines
196 ; mutable outline : (bool * int * int * outline array * string) option
197 ; mutable bookmarks : outline list
198 ; mutable path : string
199 ; mutable password : string
200 ; mutable invalidated : int
201 ; mutable colorscale : float
202 ; mutable memused : int
203 ; mutable gen : gen
204 ; mutable throttle : layout list option
205 ; hists : hists
207 and hists =
208 { pat : string circbuf
209 ; pag : string circbuf
210 ; nav : float circbuf
214 let defconf =
215 { scrollw = 7
216 ; scrollh = 12
217 ; icase = true
218 ; preload = true
219 ; pagebias = 0
220 ; verbose = false
221 ; scrollincr = 24
222 ; maxhfit = true
223 ; crophack = false
224 ; autoscroll = false
225 ; showall = false
226 ; hlinks = false
227 ; underinfo = false
228 ; interpagespace = 2
229 ; zoom = 1.0
230 ; presentation = false
231 ; angle = 0
232 ; winw = 900
233 ; winh = 900
234 ; savebmarks = true
235 ; proportional = true
236 ; memlimit = 32*1024*1024
237 ; texcount = 256
238 ; sliceheight = 24
239 ; thumbw = 76
243 let conf = { defconf with angle = defconf.angle };;
245 let state =
246 { csock = Unix.stdin
247 ; ssock = Unix.stdin
248 ; x = 0
249 ; y = 0
250 ; anchor = (0, 0.0)
251 ; w = 0
252 ; layout = []
253 ; maxy = max_int
254 ; pagemap = Hashtbl.create 10
255 ; pagecache = cbnew 100 ""
256 ; pdims = []
257 ; pagecount = 0
258 ; rendering = false
259 ; mstate = Mnone
260 ; rects = []
261 ; rects1 = []
262 ; text = ""
263 ; fullscreen = None
264 ; birdseye = None
265 ; textentry = None
266 ; searchpattern = ""
267 ; outlines = Olist []
268 ; outline = None
269 ; bookmarks = []
270 ; path = ""
271 ; password = ""
272 ; invalidated = 0
273 ; hists =
274 { nav = cbnew 100 0.0
275 ; pat = cbnew 20 ""
276 ; pag = cbnew 10 ""
278 ; colorscale = 1.0
279 ; memused = 0
280 ; gen = 0
281 ; throttle = None
285 let vlog fmt =
286 if conf.verbose
287 then
288 Printf.kprintf prerr_endline fmt
289 else
290 Printf.kprintf ignore fmt
293 let writecmd fd s =
294 let len = String.length s in
295 let n = 4 + len in
296 let b = Buffer.create n in
297 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
298 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
299 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
300 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
301 Buffer.add_string b s;
302 let s' = Buffer.contents b in
303 let n' = Unix.write fd s' 0 n in
304 if n' != n then failwith "write failed";
307 let readcmd fd =
308 let s = "xxxx" in
309 let n = Unix.read fd s 0 4 in
310 if n != 4 then failwith "incomplete read(len)";
311 let len = 0
312 lor (Char.code s.[0] lsl 24)
313 lor (Char.code s.[1] lsl 16)
314 lor (Char.code s.[2] lsl 8)
315 lor (Char.code s.[3] lsl 0)
317 let s = String.create len in
318 let n = Unix.read fd s 0 len in
319 if n != len then failwith "incomplete read(data)";
323 let yratio y =
324 if y = state.maxy
325 then 1.0
326 else float y /. float state.maxy
329 let makecmd s l =
330 let b = Buffer.create 10 in
331 Buffer.add_string b s;
332 let rec combine = function
333 | [] -> b
334 | x :: xs ->
335 Buffer.add_char b ' ';
336 let s =
337 match x with
338 | `b b -> if b then "1" else "0"
339 | `s s -> s
340 | `i i -> string_of_int i
341 | `f f -> string_of_float f
342 | `I f -> string_of_int (truncate f)
344 Buffer.add_string b s;
345 combine xs;
347 combine l;
350 let wcmd s l =
351 let cmd = Buffer.contents (makecmd s l) in
352 writecmd state.csock cmd;
355 let calcips h =
356 if conf.presentation
357 then
358 let d = conf.winh - h in
359 max 0 ((d + 1) / 2)
360 else
361 conf.interpagespace
364 let calcheight () =
365 let rec f pn ph pi fh l =
366 match l with
367 | (n, _, h, _) :: rest ->
368 let ips = calcips h in
369 let fh =
370 if conf.presentation
371 then fh+ips
372 else (
373 if state.birdseye <> None && pn = 0
374 then fh + ips
375 else fh
378 let fh = fh + ((n - pn) * (ph + pi)) in
379 f n h ips fh rest
381 | [] ->
382 let inc =
383 if conf.presentation || (state.birdseye <> None && pn = 0)
384 then 0
385 else -pi
387 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
388 max 0 fh
390 let fh = f 0 0 0 0 state.pdims in
394 let getpageyh pageno =
395 let rec f pn ph pi y l =
396 match l with
397 | (n, _, h, _) :: rest ->
398 let ips = calcips h in
399 if n >= pageno
400 then
401 if conf.presentation && n = pageno
402 then
403 y + (pageno - pn) * (ph + pi) + pi, h
404 else
405 y + (pageno - pn) * (ph + pi), h
406 else
407 let y = y + (if conf.presentation then pi else 0) in
408 let y = y + (n - pn) * (ph + pi) in
409 f n h ips y rest
411 | [] ->
412 y + (pageno - pn) * (ph + pi), ph
414 f 0 0 0 0 state.pdims
417 let getpagey pageno = fst (getpageyh pageno);;
419 let layout y sh =
420 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
421 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
422 match pdims with
423 | (pageno', w, h, x) :: rest when pageno' = pageno ->
424 let ips = calcips h in
425 let yinc =
426 if conf.presentation || (state.birdseye <> None && pageno = 0)
427 then ips
428 else 0
430 (w, h, ips, x), rest, pdimno + 1, yinc
431 | _ ->
432 prev, pdims, pdimno, 0
434 let dy = dy + yinc in
435 let py = py + yinc in
436 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
437 then
438 accu
439 else
440 let vy = y + dy in
441 if py + h <= vy - yinc
442 then
443 let py = py + h + ips in
444 let dy = max 0 (py - y) in
445 f ~pageno:(pageno+1)
446 ~pdimno
447 ~prev:curr
450 ~pdims:rest
451 ~cacheleft
452 ~accu
453 else
454 let pagey = vy - py in
455 let pagevh = h - pagey in
456 let pagevh = min (sh - dy) pagevh in
457 let off = if yinc > 0 then py - vy else 0 in
458 let py = py + h + ips in
459 let e =
460 { pageno = pageno
461 ; pagedimno = pdimno
462 ; pagew = w
463 ; pageh = h
464 ; pagedispy = dy + off
465 ; pagey = pagey + off
466 ; pagevh = pagevh - off
467 ; pagex = x
470 let accu = e :: accu in
471 f ~pageno:(pageno+1)
472 ~pdimno
473 ~prev:curr
475 ~dy:(dy+pagevh+ips)
476 ~pdims:rest
477 ~cacheleft:(cacheleft-1)
478 ~accu
480 if state.invalidated = 0
481 then (
482 let accu =
484 ~pageno:0
485 ~pdimno:~-1
486 ~prev:(0,0,0,0)
487 ~py:0
488 ~dy:0
489 ~pdims:state.pdims
490 ~cacheleft:(cbcap state.pagecache)
491 ~accu:[]
493 List.rev accu
495 else
499 let clamp incr =
500 let y = state.y + incr in
501 let y = max 0 y in
502 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
506 let getopaque pageno =
507 try Some (Hashtbl.find state.pagemap
508 (pageno, state.w, conf.angle, conf.proportional, state.gen))
509 with Not_found -> None
512 let cache pageno opaque =
513 Hashtbl.replace state.pagemap
514 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
517 let validopaque opaque = String.length opaque > 0;;
519 let render l =
520 match getopaque l.pageno with
521 | None when not state.rendering ->
522 state.rendering <- true;
523 cache l.pageno ("", -1);
524 wcmd "render" [`i (l.pageno + 1)
525 ;`i l.pagedimno
526 ;`i l.pagew
527 ;`i l.pageh];
529 | _ -> ()
532 let loadlayout layout =
533 let rec f all = function
534 | l :: ls ->
535 begin match getopaque l.pageno with
536 | None -> render l; f false ls
537 | Some (opaque, _) -> f (all && validopaque opaque) ls
539 | [] -> all
541 f (layout <> []) layout;
544 let findpageforopaque opaque =
545 Hashtbl.fold
546 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
547 state.pagemap None
550 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
552 let preload () =
553 let oktopreload =
554 if conf.preload
555 then
556 let memleft = conf.memlimit - state.memused in
557 if memleft < 0
558 then
559 let opaque = cbpeek state.pagecache in
560 match findpageforopaque opaque with
561 | Some ((n, _, _, _, _), size) ->
562 memleft + size >= 0 && not (pagevisible state.layout n)
563 | None -> false
564 else true
565 else false
567 if oktopreload
568 then
569 let presentation = conf.presentation in
570 let interpagespace = conf.interpagespace in
571 let maxy = state.maxy in
572 conf.presentation <- false;
573 conf.interpagespace <- 0;
574 state.maxy <- calcheight ();
575 let y =
576 match state.layout with
577 | [] -> 0
578 | l :: _ -> getpagey l.pageno
580 let y = if y < conf.winh then 0 else y - conf.winh in
581 let pages = layout y (conf.winh*3) in
582 List.iter render pages;
583 conf.presentation <- presentation;
584 conf.interpagespace <- interpagespace;
585 state.maxy <- maxy;
588 let gotoy y =
589 let y = max 0 y in
590 let y = min state.maxy y in
591 let pages = layout y conf.winh in
592 let ready = loadlayout pages in
593 if conf.showall
594 then (
595 if ready
596 then (
597 state.y <- y;
598 state.layout <- pages;
599 state.throttle <- None;
600 Glut.postRedisplay ();
602 else (
603 state.throttle <- Some pages;
606 else (
607 state.y <- y;
608 state.layout <- pages;
609 state.throttle <- None;
610 Glut.postRedisplay ();
612 begin match state.birdseye with
613 | Some (conf, leftx, pageno, hooverpageno) ->
614 if not (pagevisible pages pageno)
615 then (
616 match state.layout with
617 | [] -> ()
618 | l :: _ ->
619 state.birdseye <- Some (conf, leftx, l.pageno, hooverpageno)
621 | _ -> ()
622 end;
623 preload ();
626 let gotoy_and_clear_text y =
627 gotoy y;
628 if not conf.verbose then state.text <- "";
631 let addnav () =
632 cbput state.hists.nav (yratio state.y);
635 let getnav () =
636 let y = cbgetc state.hists.nav ~-1 in
637 truncate (y *. float state.maxy)
640 let gotopage n top =
641 let y, h = getpageyh n in
642 addnav ();
643 gotoy_and_clear_text (y + (truncate (top *. float h)));
646 let gotopage1 n top =
647 let y = getpagey n in
648 addnav ();
649 gotoy_and_clear_text (y + top);
652 let invalidate () =
653 state.layout <- [];
654 state.pdims <- [];
655 state.rects <- [];
656 state.rects1 <- [];
657 state.invalidated <- state.invalidated + 1;
660 let scalecolor c =
661 let c = c *. state.colorscale in
662 (c, c, c);
665 let represent () =
666 let maxy = calcheight () in
667 let y =
668 let (n, top) = state.anchor in
669 let y, h = getpageyh n in
670 y + (truncate (top *. float h))
672 state.maxy <- maxy;
673 gotoy y;
676 let pagematrix () =
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 state.w, 2.0 /. float conf.winh, 1.0);
684 let winmatrix () =
685 GlMat.mode `projection;
686 GlMat.load_identity ();
687 GlMat.rotate ~x:1.0 ~angle:180.0 ();
688 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
689 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
692 let getanchor () =
693 match state.layout with
694 | [] -> (0, 0.0)
695 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
698 let reshape ~w ~h =
699 if state.invalidated = 0
700 then state.anchor <- getanchor ();
702 conf.winw <- w;
703 let w = truncate (float w *. conf.zoom) - conf.scrollw in
704 let w = max w 2 in
705 state.w <- w;
706 conf.winh <- h;
707 GlMat.mode `modelview;
708 GlMat.load_identity ();
709 GlClear.color (scalecolor 1.0);
710 GlClear.clear [`color];
712 invalidate ();
713 wcmd "geometry" [`i w; `i h];
716 let showtext c s =
717 GlDraw.color (0.0, 0.0, 0.0);
718 GlDraw.rect
719 (0.0, float (conf.winh - 18))
720 (float (conf.winw - conf.scrollw - 1), float conf.winh)
722 let font = Glut.BITMAP_8_BY_13 in
723 GlDraw.color (1.0, 1.0, 1.0);
724 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
725 Glut.bitmapCharacter ~font ~c:(Char.code c);
726 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
729 let enttext () =
730 let len = String.length state.text in
731 match state.textentry with
732 | None ->
733 if len > 0 then showtext ' ' state.text
735 | Some (c, text, _, _, _) ->
736 let s =
737 if len > 0
738 then
739 text ^ " [" ^ state.text ^ "]"
740 else
741 text
743 showtext c s;
746 let showtext c s =
747 if true
748 then (
749 state.text <- Printf.sprintf "%c%s" c s;
750 Glut.postRedisplay ();
752 else (
753 showtext c s;
754 Glut.swapBuffers ();
758 let act cmd =
759 match cmd.[0] with
760 | 'c' ->
761 state.pdims <- [];
763 | 'D' ->
764 state.rects <- state.rects1;
765 Glut.postRedisplay ()
767 | 'C' ->
768 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
769 state.pagecount <- n;
770 state.invalidated <- state.invalidated - 1;
771 if state.invalidated = 0
772 then represent ()
774 | 't' ->
775 let s = Scanf.sscanf cmd "t %n"
776 (fun n -> String.sub cmd n (String.length cmd - n))
778 Glut.setWindowTitle s
780 | 'T' ->
781 let s = Scanf.sscanf cmd "T %n"
782 (fun n -> String.sub cmd n (String.length cmd - n))
784 if state.textentry = None
785 then (
786 state.text <- s;
787 showtext ' ' s;
789 else (
790 state.text <- s;
791 Glut.postRedisplay ();
794 | 'V' ->
795 if conf.verbose
796 then
797 let s = Scanf.sscanf cmd "V %n"
798 (fun n -> String.sub cmd n (String.length cmd - n))
800 state.text <- s;
801 showtext ' ' s;
803 | 'F' ->
804 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
805 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
806 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
807 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
809 let y = (getpagey pageno) + truncate y0 in
810 addnav ();
811 gotoy y;
812 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
814 | 'R' ->
815 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
816 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
817 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
818 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
820 state.rects1 <-
821 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
823 | 'r' ->
824 let n, w, h, r, l, s, p =
825 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
826 (fun n w h r l s p ->
827 (n-1, w, h, r, l != 0, s, p))
830 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
831 state.memused <- state.memused + s;
833 let layout =
834 match state.throttle with
835 | None -> state.layout
836 | Some layout -> layout
839 let rec gc () =
840 if (state.memused <= conf.memlimit) || cbempty state.pagecache
841 then ()
842 else (
843 let evictedopaque = cbpeek state.pagecache in
844 match findpageforopaque evictedopaque with
845 | None -> failwith "bug in gc"
846 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
847 if state.gen != gen || not (pagevisible layout evictedn)
848 then (
849 wcmd "free" [`s evictedopaque];
850 state.memused <- state.memused - evictedsize;
851 Hashtbl.remove state.pagemap k;
852 cbdecr state.pagecache;
853 gc ();
857 gc ();
859 cbput state.pagecache p;
860 state.rendering <- false;
862 begin match state.throttle with
863 | None ->
864 if pagevisible state.layout n
865 then gotoy state.y
866 else (
867 let allvisible = loadlayout state.layout in
868 if allvisible then preload ();
871 | Some layout ->
872 match layout with
873 | [] -> ()
874 | l :: _ ->
875 let y = getpagey l.pageno + l.pagey in
876 gotoy y
879 | 'l' ->
880 let (n, w, h, x) as pdim =
881 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
883 state.pdims <- pdim :: state.pdims
885 | 'o' ->
886 let (l, n, t, h, pos) =
887 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
889 let s = String.sub cmd pos (String.length cmd - pos) in
890 let s =
891 let l = String.length s in
892 let b = Buffer.create (String.length s) in
893 let rec loop pc2 i =
894 if i = l
895 then ()
896 else
897 let pc2 =
898 match s.[i] with
899 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
900 | '\xc2' -> true
901 | c ->
902 let c = if Char.code c land 0x80 = 0 then c else '?' in
903 Buffer.add_char b c;
904 false
906 loop pc2 (i+1)
908 loop false 0;
909 Buffer.contents b
911 let outline = (s, l, n, float t /. float h) in
912 let outlines =
913 match state.outlines with
914 | Olist outlines -> Olist (outline :: outlines)
915 | Oarray _ -> Olist [outline]
916 | Onarrow _ -> Olist [outline]
918 state.outlines <- outlines
920 | _ ->
921 log "unknown cmd `%S'" cmd
924 let now = Unix.gettimeofday;;
926 let idle () =
927 let rec loop delay =
928 let r, _, _ = Unix.select [state.csock] [] [] delay in
929 begin match r with
930 | [] ->
931 if conf.autoscroll
932 then begin
933 let y = state.y + conf.scrollincr in
934 let y = if y >= state.maxy then 0 else y in
935 gotoy y;
936 state.text <- "";
937 end;
939 | _ ->
940 let cmd = readcmd state.csock in
941 act cmd;
942 loop 0.0
943 end;
944 in loop 0.001
947 let onhist cb =
948 let rc = cb.rc in
949 let action = function
950 | HCprev -> cbget cb ~-1
951 | HCnext -> cbget cb 1
952 | HCfirst -> cbget cb ~-(cb.rc)
953 | HClast -> cbget cb (cb.len - 1 - cb.rc)
954 and cancel () = cb.rc <- rc
955 in (action, cancel)
958 let search pattern forward =
959 if String.length pattern > 0
960 then
961 let pn, py =
962 match state.layout with
963 | [] -> 0, 0
964 | l :: _ ->
965 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
967 let cmd =
968 let b = makecmd "search"
969 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
971 Buffer.add_char b ',';
972 Buffer.add_string b pattern;
973 Buffer.add_char b '\000';
974 Buffer.contents b;
976 writecmd state.csock cmd;
979 let intentry text key =
980 let c = Char.unsafe_chr key in
981 match c with
982 | '0' .. '9' ->
983 let s = "x" in s.[0] <- c;
984 let text = text ^ s in
985 TEcont text
987 | _ ->
988 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
989 TEcont text
992 let addchar s c =
993 let b = Buffer.create (String.length s + 1) in
994 Buffer.add_string b s;
995 Buffer.add_char b c;
996 Buffer.contents b;
999 let textentry text key =
1000 let c = Char.unsafe_chr key in
1001 match c with
1002 | _ when key >= 32 && key < 127 ->
1003 let text = addchar text c in
1004 TEcont text
1006 | _ ->
1007 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1008 TEcont text
1011 let reinit angle proportional =
1012 conf.angle <- angle;
1013 conf.proportional <- proportional;
1014 invalidate ();
1015 wcmd "reinit" [`i angle; `b proportional];
1018 let optentry text key =
1019 let btos b = if b then "on" else "off" in
1020 let c = Char.unsafe_chr key in
1021 match c with
1022 | 's' ->
1023 let ondone s =
1024 try conf.scrollincr <- int_of_string s with exc ->
1025 state.text <- Printf.sprintf "bad integer `%s': %s"
1026 s (Printexc.to_string exc)
1028 TEswitch ('#', "", None, intentry, ondone)
1030 | 'R' ->
1031 let ondone s =
1032 match try
1033 Some (int_of_string s)
1034 with exc ->
1035 state.text <- Printf.sprintf "bad integer `%s': %s"
1036 s (Printexc.to_string exc);
1037 None
1038 with
1039 | Some angle -> reinit angle conf.proportional
1040 | None -> ()
1042 TEswitch ('^', "", None, intentry, ondone)
1044 | 'i' ->
1045 conf.icase <- not conf.icase;
1046 TEdone ("case insensitive search " ^ (btos conf.icase))
1048 | 'p' ->
1049 conf.preload <- not conf.preload;
1050 gotoy state.y;
1051 TEdone ("preload " ^ (btos conf.preload))
1053 | 'v' ->
1054 conf.verbose <- not conf.verbose;
1055 TEdone ("verbose " ^ (btos conf.verbose))
1057 | 'h' ->
1058 conf.maxhfit <- not conf.maxhfit;
1059 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1060 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1062 | 'c' ->
1063 conf.crophack <- not conf.crophack;
1064 TEdone ("crophack " ^ btos conf.crophack)
1066 | 'a' ->
1067 conf.showall <- not conf.showall;
1068 TEdone ("showall " ^ btos conf.showall)
1070 | 'f' ->
1071 conf.underinfo <- not conf.underinfo;
1072 TEdone ("underinfo " ^ btos conf.underinfo)
1074 | 'P' ->
1075 conf.savebmarks <- not conf.savebmarks;
1076 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1078 | 'S' ->
1079 let ondone s =
1081 let pageno, py =
1082 match state.layout with
1083 | [] -> 0, 0
1084 | l :: _ ->
1085 l.pageno, l.pagey
1087 conf.interpagespace <- int_of_string s;
1088 state.maxy <- calcheight ();
1089 let y = getpagey pageno in
1090 gotoy (y + py)
1091 with exc ->
1092 state.text <- Printf.sprintf "bad integer `%s': %s"
1093 s (Printexc.to_string exc)
1095 TEswitch ('%', "", None, intentry, ondone)
1097 | 'l' ->
1098 reinit conf.angle (not conf.proportional);
1099 TEdone ("proprortional display " ^ btos conf.proportional)
1101 | _ ->
1102 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1103 TEstop
1106 let maxoutlinerows () = (conf.winh - 31) / 16;;
1108 let enterselector allowdel outlines errmsg msg =
1109 if Array.length outlines = 0
1110 then (
1111 showtext ' ' errmsg;
1113 else (
1114 state.text <- msg;
1115 Glut.setCursor Glut.CURSOR_INHERIT;
1116 let pageno =
1117 match state.layout with
1118 | [] -> -1
1119 | {pageno=pageno} :: rest -> pageno
1121 let active =
1122 let rec loop n =
1123 if n = Array.length outlines
1124 then 0
1125 else
1126 let (_, _, outlinepageno, _) = outlines.(n) in
1127 if outlinepageno >= pageno then n else loop (n+1)
1129 loop 0
1131 state.outline <-
1132 Some (allowdel, active,
1133 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1134 Glut.postRedisplay ();
1138 let enteroutlinemode () =
1139 let outlines, msg =
1140 match state.outlines with
1141 | Oarray a -> a, ""
1142 | Olist l ->
1143 let a = Array.of_list (List.rev l) in
1144 state.outlines <- Oarray a;
1145 a, ""
1146 | Onarrow (pat, a, b) ->
1147 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1149 enterselector false outlines "Document has no outline" msg;
1152 let enterbookmarkmode () =
1153 let bookmarks = Array.of_list state.bookmarks in
1154 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1157 let quickbookmark ?title () =
1158 match state.layout with
1159 | [] -> ()
1160 | l :: _ ->
1161 let title =
1162 match title with
1163 | None ->
1164 let sec = Unix.gettimeofday () in
1165 let tm = Unix.localtime sec in
1166 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1167 (l.pageno+1)
1168 tm.Unix.tm_mday
1169 tm.Unix.tm_mon
1170 (tm.Unix.tm_year + 1900)
1171 tm.Unix.tm_hour
1172 tm.Unix.tm_min
1173 | Some title -> title
1175 state.bookmarks <-
1176 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1179 let doreshape w h =
1180 state.fullscreen <- None;
1181 Glut.reshapeWindow w h;
1184 let writeopen path password =
1185 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1188 let opendoc path password =
1189 invalidate ();
1190 state.path <- path;
1191 state.password <- password;
1192 state.gen <- state.gen + 1;
1194 writeopen path password;
1195 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1196 wcmd "geometry" [`i state.w; `i conf.winh];
1199 let birdseyeon () =
1200 let zoom = float conf.thumbw /. float conf.winw in
1201 let birdseyepageno =
1202 match state.layout with
1203 | [] -> 0
1204 | l :: _ -> l.pageno
1206 state.birdseye <-
1207 Some ({ conf with zoom = conf.zoom }, state.x, birdseyepageno, -1);
1208 conf.zoom <- zoom;
1209 conf.presentation <- false;
1210 conf.interpagespace <- 10;
1211 conf.hlinks <- false;
1212 state.x <- 0;
1213 state.mstate <- Mnone;
1214 conf.showall <- false;
1215 Glut.setCursor Glut.CURSOR_INHERIT;
1216 if conf.verbose
1217 then
1218 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1219 (100.0*.zoom)
1220 else
1221 state.text <- ""
1225 let birdseyeoff (c, leftx, pageno, _) =
1226 state.birdseye <- None;
1227 conf.zoom <- c.zoom;
1228 conf.presentation <- c.presentation;
1229 conf.interpagespace <- c.interpagespace;
1230 conf.showall <- c.showall;
1231 conf.hlinks <- c.hlinks;
1232 state.x <- leftx;
1233 if conf.verbose
1234 then
1235 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1236 (100.0*.conf.zoom)
1240 let togglebirdseye () =
1241 match state.birdseye with
1242 | None -> birdseyeon ()
1243 | Some vals -> birdseyeoff vals
1246 let viewkeyboard ~key ~x ~y =
1247 let enttext te =
1248 state.textentry <- te;
1249 state.text <- "";
1250 enttext ();
1251 Glut.postRedisplay ()
1253 match state.textentry with
1254 | None ->
1255 let c = Char.chr key in
1256 begin match c with
1257 | '\027' | 'q' ->
1258 exit 0
1260 | '\008' ->
1261 let y = getnav () in
1262 gotoy_and_clear_text y
1264 | '\013' ->
1265 begin match state.birdseye with
1266 | None -> ()
1267 | Some ((_, _, pageno, _) as vals) ->
1268 birdseyeoff vals;
1269 reshape conf.winw conf.winh;
1270 state.anchor <- (pageno, 0.0);
1271 end;
1273 | 'o' ->
1274 enteroutlinemode ()
1276 | 'u' ->
1277 state.rects <- [];
1278 state.text <- "";
1279 Glut.postRedisplay ()
1281 | '/' | '?' ->
1282 let ondone isforw s =
1283 cbput state.hists.pat s;
1284 state.searchpattern <- s;
1285 search s isforw
1287 enttext (Some (c, "", Some (onhist state.hists.pat),
1288 textentry, ondone (c ='/')))
1290 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1291 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1292 conf.zoom <- min 2.2 (conf.zoom +. incr);
1293 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1294 reshape conf.winw conf.winh
1296 | '+' ->
1297 let ondone s =
1298 let n =
1299 try int_of_string s with exc ->
1300 state.text <- Printf.sprintf "bad integer `%s': %s"
1301 s (Printexc.to_string exc);
1302 max_int
1304 if n != max_int
1305 then (
1306 conf.pagebias <- n;
1307 state.text <- "page bias is now " ^ string_of_int n;
1310 enttext (Some ('+', "", None, intentry, ondone))
1312 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1313 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1314 conf.zoom <- max 0.01 (conf.zoom -. decr);
1315 if conf.zoom <= 1.0 then state.x <- 0;
1316 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1317 reshape conf.winw conf.winh;
1319 | '-' ->
1320 let ondone msg =
1321 state.text <- msg;
1323 enttext (Some ('-', "", None, optentry, ondone))
1325 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1326 state.x <- 0;
1327 conf.zoom <- 1.0;
1328 state.text <- "zoom is 100%";
1329 reshape conf.winw conf.winh
1331 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1332 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1333 if zoom < 1.0
1334 then (
1335 conf.zoom <- zoom;
1336 state.x <- 0;
1337 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1338 reshape conf.winw conf.winh;
1341 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1342 togglebirdseye ();
1343 reshape conf.winw conf.winh;
1345 | '0' .. '9' ->
1346 let ondone s =
1347 let n =
1348 try int_of_string s with exc ->
1349 state.text <- Printf.sprintf "bad integer `%s': %s"
1350 s (Printexc.to_string exc);
1353 if n >= 0
1354 then (
1355 addnav ();
1356 cbput state.hists.pag (string_of_int n);
1357 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1360 let pageentry text key =
1361 match Char.unsafe_chr key with
1362 | 'g' -> TEdone text
1363 | _ -> intentry text key
1365 let text = "x" in text.[0] <- c;
1366 enttext (Some (':', text, Some (onhist state.hists.pag),
1367 pageentry, ondone))
1369 | 'b' ->
1370 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1371 reshape conf.winw conf.winh;
1373 | 'l' ->
1374 conf.hlinks <- not conf.hlinks;
1375 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1376 Glut.postRedisplay ()
1378 | 'a' ->
1379 conf.autoscroll <- not conf.autoscroll
1381 | 'P' ->
1382 conf.presentation <- not conf.presentation;
1383 showtext ' ' ("presentation mode " ^
1384 if conf.presentation then "on" else "off");
1385 represent ()
1387 | 'f' ->
1388 begin match state.fullscreen with
1389 | None ->
1390 state.fullscreen <- Some (conf.winw, conf.winh);
1391 Glut.fullScreen ()
1392 | Some (w, h) ->
1393 state.fullscreen <- None;
1394 doreshape w h
1397 | 'g' ->
1398 gotoy_and_clear_text 0
1400 | 'n' ->
1401 search state.searchpattern true
1403 | 'p' | 'N' ->
1404 search state.searchpattern false
1406 | 't' ->
1407 begin match state.layout with
1408 | [] -> ()
1409 | l :: _ ->
1410 gotoy_and_clear_text (getpagey l.pageno)
1413 | ' ' ->
1414 begin match List.rev state.layout with
1415 | [] -> ()
1416 | l :: _ ->
1417 let pageno = min (l.pageno+1) (state.pagecount-1) in
1418 gotoy_and_clear_text (getpagey pageno)
1421 | '\127' ->
1422 begin match state.layout with
1423 | [] -> ()
1424 | l :: _ ->
1425 let pageno = max 0 (l.pageno-1) in
1426 gotoy_and_clear_text (getpagey pageno)
1429 | '=' ->
1430 let f (fn, ln) l =
1431 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1433 let fn, ln = List.fold_left f (-1, -1) state.layout in
1434 let s =
1435 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1436 let percent =
1437 if maxy <= 0
1438 then 100.
1439 else (100. *. (float state.y /. float maxy)) in
1440 if fn = ln
1441 then
1442 Printf.sprintf "Page %d of %d %.2f%%"
1443 (fn+1) state.pagecount percent
1444 else
1445 Printf.sprintf
1446 "Pages %d-%d of %d %.2f%%"
1447 (fn+1) (ln+1) state.pagecount percent
1449 showtext ' ' s;
1451 | 'w' ->
1452 begin match state.layout with
1453 | [] -> ()
1454 | l :: _ ->
1455 doreshape (l.pagew + conf.scrollw) l.pageh;
1456 Glut.postRedisplay ();
1459 | '\'' ->
1460 enterbookmarkmode ()
1462 | 'm' ->
1463 let ondone s =
1464 match state.layout with
1465 | l :: _ ->
1466 state.bookmarks <-
1467 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1468 :: state.bookmarks
1469 | _ -> ()
1471 enttext (Some ('~', "", None, textentry, ondone))
1473 | '~' ->
1474 quickbookmark ();
1475 showtext ' ' "Quick bookmark added";
1477 | 'z' ->
1478 begin match state.layout with
1479 | l :: _ ->
1480 let rect = getpdimrect l.pagedimno in
1481 let w, h =
1482 if conf.crophack
1483 then
1484 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1485 truncate (1.2 *. (rect.(3) -. rect.(0))))
1486 else
1487 (truncate (rect.(1) -. rect.(0)),
1488 truncate (rect.(3) -. rect.(0)))
1490 if w != 0 && h != 0
1491 then
1492 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1494 Glut.postRedisplay ();
1496 | [] -> ()
1499 | '<' | '>' ->
1500 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1502 | '[' | ']' ->
1503 state.colorscale <-
1504 max 0.0
1505 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1506 Glut.postRedisplay ()
1508 | 'k' -> gotoy (clamp (-conf.scrollincr))
1509 | 'j' -> gotoy (clamp conf.scrollincr)
1511 | 'r' -> opendoc state.path state.password
1513 | _ ->
1514 vlog "huh? %d %c" key (Char.chr key);
1517 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1518 let len = String.length text in
1519 if len = 0
1520 then (
1521 state.textentry <- None;
1522 Glut.postRedisplay ();
1524 else (
1525 let s = String.sub text 0 (len - 1) in
1526 enttext (Some (c, s, opthist, onkey, ondone))
1529 | Some (c, text, onhist, onkey, ondone) ->
1530 begin match Char.unsafe_chr key with
1531 | '\r' | '\n' ->
1532 ondone text;
1533 state.textentry <- None;
1534 Glut.postRedisplay ()
1536 | '\027' ->
1537 begin match onhist with
1538 | None -> ()
1539 | Some (_, onhistcancel) -> onhistcancel ()
1540 end;
1541 state.textentry <- None;
1542 Glut.postRedisplay ()
1544 | _ ->
1545 begin match onkey text key with
1546 | TEdone text ->
1547 state.textentry <- None;
1548 ondone text;
1549 Glut.postRedisplay ()
1551 | TEcont text ->
1552 enttext (Some (c, text, onhist, onkey, ondone));
1554 | TEstop ->
1555 state.textentry <- None;
1556 Glut.postRedisplay ()
1558 | TEswitch te ->
1559 state.textentry <- Some te;
1560 Glut.postRedisplay ()
1561 end;
1562 end;
1565 let narrow outlines pattern =
1566 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1567 match reopt with
1568 | None -> None
1569 | Some re ->
1570 let rec fold accu n =
1571 if n = -1
1572 then accu
1573 else
1574 let (s, _, _, _) as o = outlines.(n) in
1575 let accu =
1576 if (try ignore (Str.search_forward re s 0); true
1577 with Not_found -> false)
1578 then (o :: accu)
1579 else accu
1581 fold accu (n-1)
1583 let matched = fold [] (Array.length outlines - 1) in
1584 if matched = [] then None else Some (Array.of_list matched)
1587 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1588 let search active pattern incr =
1589 let dosearch re =
1590 let rec loop n =
1591 if n = Array.length outlines || n = -1
1592 then None
1593 else
1594 let (s, _, _, _) = outlines.(n) in
1596 (try ignore (Str.search_forward re s 0); true
1597 with Not_found -> false)
1598 then Some n
1599 else loop (n + incr)
1601 loop active
1604 let re = Str.regexp_case_fold pattern in
1605 dosearch re
1606 with Failure s ->
1607 state.text <- s;
1608 None
1610 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1611 match key with
1612 | 27 ->
1613 if String.length qsearch = 0
1614 then (
1615 state.text <- "";
1616 state.outline <- None;
1617 Glut.postRedisplay ();
1619 else (
1620 state.text <- "";
1621 state.outline <- Some (allowdel, active, first, outlines, "");
1622 Glut.postRedisplay ();
1625 | 18 | 19 ->
1626 let incr = if key = 18 then -1 else 1 in
1627 let active, first =
1628 match search (active + incr) qsearch incr with
1629 | None ->
1630 state.text <- qsearch ^ " [not found]";
1631 active, first
1632 | Some active ->
1633 state.text <- qsearch;
1634 active, firstof active
1636 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1637 Glut.postRedisplay ();
1639 | 8 ->
1640 let len = String.length qsearch in
1641 if len = 0
1642 then ()
1643 else (
1644 if len = 1
1645 then (
1646 state.text <- "";
1647 state.outline <- Some (allowdel, active, first, outlines, "");
1649 else
1650 let qsearch = String.sub qsearch 0 (len - 1) in
1651 let active, first =
1652 match search active qsearch ~-1 with
1653 | None ->
1654 state.text <- qsearch ^ " [not found]";
1655 active, first
1656 | Some active ->
1657 state.text <- qsearch;
1658 active, firstof active
1660 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1662 Glut.postRedisplay ()
1664 | 13 ->
1665 if active < Array.length outlines
1666 then (
1667 let (_, _, n, t) = outlines.(active) in
1668 gotopage n t;
1670 state.text <- "";
1671 if allowdel then state.bookmarks <- Array.to_list outlines;
1672 state.outline <- None;
1673 Glut.postRedisplay ();
1675 | _ when key >= 32 && key < 127 ->
1676 let pattern = addchar qsearch (Char.chr key) in
1677 let active, first =
1678 match search active pattern 1 with
1679 | None ->
1680 state.text <- pattern ^ " [not found]";
1681 active, first
1682 | Some active ->
1683 state.text <- pattern;
1684 active, firstof active
1686 state.outline <- Some (allowdel, active, first, outlines, pattern);
1687 Glut.postRedisplay ()
1689 | 14 when not allowdel -> (* ctrl-n *)
1690 if String.length qsearch > 0
1691 then (
1692 let optoutlines = narrow outlines qsearch in
1693 begin match optoutlines with
1694 | None -> state.text <- "can't narrow"
1695 | Some outlines ->
1696 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1697 match state.outlines with
1698 | Olist l -> ()
1699 | Oarray a ->
1700 state.outlines <- Onarrow (qsearch, outlines, a)
1701 | Onarrow (pat, a, b) ->
1702 state.outlines <- Onarrow (qsearch, outlines, b)
1703 end;
1705 Glut.postRedisplay ()
1707 | 21 when not allowdel -> (* ctrl-u *)
1708 let outline =
1709 match state.outlines with
1710 | Oarray a -> a
1711 | Olist l ->
1712 let a = Array.of_list (List.rev l) in
1713 state.outlines <- Oarray a;
1715 | Onarrow (pat, a, b) ->
1716 state.outlines <- Oarray b;
1717 state.text <- "";
1720 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1721 Glut.postRedisplay ()
1723 | 12 ->
1724 state.outline <-
1725 Some (allowdel, active, firstof active, outlines, qsearch);
1726 Glut.postRedisplay ()
1728 | 127 when allowdel ->
1729 let len = Array.length outlines - 1 in
1730 if len = 0
1731 then (
1732 state.outline <- None;
1733 state.bookmarks <- [];
1735 else (
1736 let bookmarks = Array.init len
1737 (fun i ->
1738 let i = if i >= active then i + 1 else i in
1739 outlines.(i)
1742 state.outline <-
1743 Some (allowdel,
1744 min active (len-1),
1745 min first (len-1),
1746 bookmarks, qsearch)
1749 Glut.postRedisplay ()
1751 | _ -> log "unknown key %d" key
1754 let keyboard ~key ~x ~y =
1755 if key = 7
1756 then
1757 wcmd "interrupt" []
1758 else
1759 match state.outline with
1760 | None -> viewkeyboard ~key ~x ~y
1761 | Some outline -> outlinekeyboard ~key ~x ~y outline
1764 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno) =
1765 match key with
1766 | Glut.KEY_UP ->
1767 let pageno = max 0 (pageno - 1) in
1768 let move =
1769 let rec loop = function
1770 | [] -> true
1771 | l :: rest ->
1772 if l.pageno = pageno
1773 then l.pagey != 0
1774 else loop rest
1776 loop state.layout
1778 if move
1779 then gotopage1 pageno 0
1780 else Glut.postRedisplay ();
1781 state.birdseye <- Some (conf, leftx, pageno, hooverpageno)
1783 | Glut.KEY_DOWN ->
1784 let pageno = min (state.pagecount - 1) (pageno + 1) in
1785 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1786 let rec loop = function
1787 | [] -> gotopage1 pageno 0
1788 | l :: rest ->
1789 if l.pageno = pageno
1790 then (
1791 if l.pagevh != l.pageh
1792 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1793 else Glut.postRedisplay ()
1795 else loop rest
1797 loop state.layout
1799 | Glut.KEY_PAGE_UP ->
1800 begin match state.layout with
1801 | l :: _ ->
1802 if l.pageno = pageno
1803 then (
1804 match layout (state.y - conf.winh) conf.winh with
1805 | [] -> gotoy (clamp (-conf.winh))
1806 | l :: _ ->
1807 let pageno = max 0 (l.pageno - 1) in
1808 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1809 gotopage pageno 0.0
1811 else (
1812 let pageno = max 0 (l.pageno - 1) in
1813 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1814 gotopage pageno 0.0
1816 | [] -> gotoy (clamp (-conf.winh))
1817 end;
1818 | Glut.KEY_PAGE_DOWN ->
1819 begin match List.rev state.layout with
1820 | l :: _ ->
1821 let pageno = min l.pageno (state.pagecount - 1) in
1822 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1823 gotoy (clamp (l.pagedispy + l.pageh))
1824 | [] -> gotoy (clamp conf.winh)
1825 end;
1827 | Glut.KEY_HOME ->
1828 state.birdseye <- Some (conf, leftx, 0, hooverpageno);
1829 gotopage 0 0.0
1830 | Glut.KEY_END ->
1831 let pageno = state.pagecount - 1 in
1832 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1833 if not (pagevisible state.layout pageno)
1834 then
1835 let h =
1836 match List.rev state.pdims with
1837 | [] -> conf.winh
1838 | (_, _, h, _) :: _ -> h
1840 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1841 else Glut.postRedisplay ();
1842 | _ -> ()
1845 let special ~key ~x ~y =
1846 match state.outline, state.birdseye with
1847 | None, _ when key = Glut.KEY_F9 ->
1848 togglebirdseye ();
1849 reshape conf.winw conf.winh;
1851 | None, (Some vals) ->
1852 birdseyespecial key x y vals
1854 | None, None ->
1855 begin match state.textentry with
1856 | None ->
1857 let y =
1858 match key with
1859 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1860 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1861 | Glut.KEY_DOWN -> clamp conf.scrollincr
1862 | Glut.KEY_PAGE_UP ->
1863 if Glut.getModifiers () land Glut.active_ctrl != 0
1864 then
1865 match state.layout with
1866 | [] -> state.y
1867 | l :: _ -> state.y - l.pagey
1868 else
1869 clamp (-conf.winh)
1870 | Glut.KEY_PAGE_DOWN ->
1871 if Glut.getModifiers () land Glut.active_ctrl != 0
1872 then
1873 match List.rev state.layout with
1874 | [] -> state.y
1875 | l :: _ -> getpagey l.pageno
1876 else
1877 clamp conf.winh
1878 | Glut.KEY_HOME -> addnav (); 0
1879 | Glut.KEY_END ->
1880 addnav ();
1881 state.maxy - (if conf.maxhfit then conf.winh else 0)
1883 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1884 state.x <- state.x - 10;
1885 state.y
1886 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1887 state.x <- state.x + 10;
1888 state.y
1890 | _ -> state.y
1892 gotoy_and_clear_text y
1894 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1895 let s =
1896 match key with
1897 | Glut.KEY_UP -> action HCprev
1898 | Glut.KEY_DOWN -> action HCnext
1899 | Glut.KEY_HOME -> action HCfirst
1900 | Glut.KEY_END -> action HClast
1901 | _ -> state.text
1903 state.textentry <- Some (c, s, onhist, onkey, ondone);
1904 Glut.postRedisplay ()
1906 | _ -> ()
1909 | Some (allowdel, active, first, outlines, qsearch), _ ->
1910 let maxrows = maxoutlinerows () in
1911 let calcfirst first active =
1912 if active > first
1913 then
1914 let rows = active - first in
1915 if rows > maxrows then active - maxrows else first
1916 else active
1918 let navigate incr =
1919 let active = active + incr in
1920 let active = max 0 (min active (Array.length outlines - 1)) in
1921 let first = calcfirst first active in
1922 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1923 Glut.postRedisplay ()
1925 let updownlevel incr =
1926 let len = Array.length outlines in
1927 let (_, curlevel, _, _) = outlines.(active) in
1928 let rec flow i =
1929 if i = len then i-1 else if i = -1 then 0 else
1930 let (_, l, _, _) = outlines.(i) in
1931 if l != curlevel then i else flow (i+incr)
1933 let active = flow active in
1934 let first = calcfirst first active in
1935 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1936 Glut.postRedisplay ()
1938 match key with
1939 | Glut.KEY_UP -> navigate ~-1
1940 | Glut.KEY_DOWN -> navigate 1
1941 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1942 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1944 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1945 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1947 | Glut.KEY_HOME ->
1948 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1949 Glut.postRedisplay ()
1951 | Glut.KEY_END ->
1952 let active = Array.length outlines - 1 in
1953 let first = max 0 (active - maxrows) in
1954 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1955 Glut.postRedisplay ()
1957 | _ -> ()
1960 let drawplaceholder l =
1961 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1962 GlDraw.rect
1963 (float l.pagex, float l.pagedispy)
1964 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1966 let x = float (if margin < 0 then -margin else l.pagex)
1967 and y = float (l.pagedispy + 13) in
1968 let font = Glut.BITMAP_8_BY_13 in
1969 GlDraw.color (0.0, 0.0, 0.0);
1970 GlPix.raster_pos ~x ~y ();
1971 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1972 ("Loading " ^ string_of_int (l.pageno + 1));
1975 let now () = Unix.gettimeofday ();;
1977 let drawpage l =
1978 if state.textentry = None
1979 then (
1980 match state.birdseye with
1981 | None -> GlDraw.color (scalecolor 1.0);
1982 | Some (_, _, pageno, hooverpageno) ->
1983 let color =
1984 if l.pageno = pageno
1985 then 1.0
1986 else (
1987 if l.pageno = hooverpageno
1988 then 0.9
1989 else 0.8
1992 GlDraw.color (scalecolor color);
1994 else GlDraw.color (scalecolor 0.4);
1995 begin match getopaque l.pageno with
1996 | Some (opaque, _) when validopaque opaque ->
1997 let a = now () in
1998 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1999 opaque;
2000 let b = now () in
2001 let d = b-.a in
2002 vlog "draw %d %f sec" l.pageno d;
2004 | _ ->
2005 drawplaceholder l;
2006 end;
2009 let scrollph y =
2010 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2011 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2012 let sh = float conf.winh /. sh in
2013 let sh = max sh (float conf.scrollh) in
2015 let percent =
2016 if state.y = state.maxy
2017 then 1.0
2018 else float y /. float maxy
2020 let position = (float conf.winh -. sh) *. percent in
2022 let position =
2023 if position +. sh > float conf.winh
2024 then float conf.winh -. sh
2025 else position
2027 position, sh;
2030 let scrollindicator () =
2031 GlDraw.color (0.64 , 0.64, 0.64);
2032 GlDraw.rect
2033 (float (conf.winw - conf.scrollw), 0.)
2034 (float conf.winw, float conf.winh)
2036 GlDraw.color (0.0, 0.0, 0.0);
2038 let position, sh = scrollph state.y in
2039 GlDraw.rect
2040 (float (conf.winw - conf.scrollw), position)
2041 (float conf.winw, position +. sh)
2045 let showsel margin =
2046 match state.mstate with
2047 | Mnone | Mscroll _ | Mpan _ ->
2050 | Msel ((x0, y0), (x1, y1)) ->
2051 let rec loop = function
2052 | l :: ls ->
2053 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2054 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2055 then
2056 match getopaque l.pageno with
2057 | Some (opaque, _) when validopaque opaque ->
2058 let oy = -l.pagey + l.pagedispy in
2059 seltext opaque
2060 (x0 - margin - state.x, y0,
2061 x1 - margin - state.x, y1) oy;
2063 | _ -> ()
2064 else loop ls
2065 | [] -> ()
2067 loop state.layout
2070 let showrects () =
2071 let panx = float state.x in
2072 Gl.enable `blend;
2073 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2074 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2075 List.iter
2076 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2077 List.iter (fun l ->
2078 if l.pageno = pageno
2079 then (
2080 let d = float (l.pagedispy - l.pagey) in
2081 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2082 GlDraw.begins `quads;
2084 GlDraw.vertex2 (x0+.panx, y0+.d);
2085 GlDraw.vertex2 (x1+.panx, y1+.d);
2086 GlDraw.vertex2 (x2+.panx, y2+.d);
2087 GlDraw.vertex2 (x3+.panx, y3+.d);
2089 GlDraw.ends ();
2091 ) state.layout
2092 ) state.rects
2094 Gl.disable `blend;
2097 let showoutline = function
2098 | None -> ()
2099 | Some (allowdel, active, first, outlines, qsearch) ->
2100 Gl.enable `blend;
2101 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2102 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2103 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2104 Gl.disable `blend;
2106 GlDraw.color (1., 1., 1.);
2107 let font = Glut.BITMAP_9_BY_15 in
2108 let draw_string x y s =
2109 GlPix.raster_pos ~x ~y ();
2110 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2112 let rec loop row =
2113 if row = Array.length outlines || (row - first) * 16 > conf.winh
2114 then ()
2115 else (
2116 let (s, l, _, _) = outlines.(row) in
2117 let y = (row - first) * 16 in
2118 let x = 5 + 15*l in
2119 if row = active
2120 then (
2121 Gl.enable `blend;
2122 GlDraw.polygon_mode `both `line;
2123 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2124 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2125 GlDraw.rect (0., float (y + 1))
2126 (float (conf.winw - 1), float (y + 18));
2127 GlDraw.polygon_mode `both `fill;
2128 Gl.disable `blend;
2129 GlDraw.color (1., 1., 1.);
2131 draw_string (float x) (float (y + 16)) s;
2132 loop (row+1)
2135 loop first
2138 let display () =
2139 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2140 GlDraw.viewport margin 0 state.w conf.winh;
2141 pagematrix ();
2142 GlClear.color (scalecolor 0.5);
2143 GlClear.clear [`color];
2144 if state.x != 0
2145 then (
2146 let x = float state.x in
2147 GlMat.translate ~x ();
2149 if conf.zoom > 1.0
2150 then (
2151 Gl.enable `scissor_test;
2152 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2154 List.iter drawpage state.layout;
2155 if conf.zoom > 1.0
2156 then
2157 Gl.disable `scissor_test
2159 if state.x != 0
2160 then (
2161 let x = -.float state.x in
2162 GlMat.translate ~x ();
2164 showrects ();
2165 showsel margin;
2166 GlDraw.viewport 0 0 conf.winw conf.winh;
2167 winmatrix ();
2168 scrollindicator ();
2169 showoutline state.outline;
2170 enttext ();
2171 Glut.swapBuffers ();
2174 let getunder x y =
2175 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2176 let x = x - margin - state.x in
2177 let rec f = function
2178 | l :: rest ->
2179 begin match getopaque l.pageno with
2180 | Some (opaque, _) when validopaque opaque ->
2181 let y = y - l.pagedispy in
2182 if y > 0
2183 then
2184 let y = l.pagey + y in
2185 let x = x - l.pagex in
2186 match whatsunder opaque x y with
2187 | Unone -> f rest
2188 | under -> under
2189 else
2190 f rest
2191 | _ ->
2192 f rest
2194 | [] -> Unone
2196 f state.layout
2199 let mouse ~button ~bstate ~x ~y =
2200 match button with
2201 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2202 let incr =
2203 if n = 3
2204 then
2205 -conf.scrollincr
2206 else
2207 conf.scrollincr
2209 let incr = incr * 2 in
2210 let y = clamp incr in
2211 gotoy_and_clear_text y
2213 | Glut.LEFT_BUTTON when state.outline = None
2214 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2215 if bstate = Glut.DOWN
2216 then (
2217 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2218 state.mstate <- Mpan (x, y)
2220 else
2221 state.mstate <- Mnone
2223 | Glut.LEFT_BUTTON
2224 when state.outline = None && x > conf.winw - conf.scrollw ->
2225 if bstate = Glut.DOWN
2226 then
2227 let position, sh = scrollph state.y in
2228 if y > truncate position && y < truncate (position +. sh)
2229 then
2230 state.mstate <- Mscroll
2231 else
2232 let percent = float y /. float conf.winh in
2233 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2234 gotoy desty;
2235 state.mstate <- Mscroll
2236 else
2237 state.mstate <- Mnone
2239 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2240 begin match state.birdseye with
2241 | Some (conf, leftx, pageno, hooverpageno) ->
2242 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2243 let rec loop = function
2244 | [] -> ()
2245 | l :: rest ->
2246 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2247 && x > margin && x < margin + l.pagew
2248 then (
2249 birdseyeoff (conf, leftx, l.pageno, hooverpageno);
2250 reshape conf.winw conf.winh;
2251 state.anchor <- (l.pageno, 0.0);
2253 else loop rest
2255 loop state.layout;
2256 | None -> () (* impossible *)
2259 | Glut.LEFT_BUTTON when state.outline = None ->
2260 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2261 begin match dest with
2262 | Ulinkgoto (pageno, top) ->
2263 if pageno >= 0
2264 then
2265 gotopage1 pageno top
2267 | Ulinkuri s ->
2268 print_endline s
2270 | Unone when bstate = Glut.DOWN ->
2271 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2272 state.mstate <- Mpan (x, y);
2274 | Unone | Utext _ ->
2275 if bstate = Glut.DOWN
2276 then (
2277 if conf.angle mod 360 = 0
2278 then (
2279 state.mstate <- Msel ((x, y), (x, y));
2280 Glut.postRedisplay ()
2283 else (
2284 match state.mstate with
2285 | Mnone -> ()
2287 | Mscroll ->
2288 state.mstate <- Mnone
2290 | Mpan _ ->
2291 Glut.setCursor Glut.CURSOR_INHERIT;
2292 state.mstate <- Mnone
2294 | Msel ((x0, y0), (x1, y1)) ->
2295 let f l =
2296 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2297 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2298 then
2299 match getopaque l.pageno with
2300 | Some (opaque, _) when validopaque opaque ->
2301 copysel opaque
2302 | _ -> ()
2304 List.iter f state.layout;
2305 copysel ""; (* ugly *)
2306 Glut.setCursor Glut.CURSOR_INHERIT;
2307 state.mstate <- Mnone;
2311 | _ ->
2314 let mouse ~button ~state ~x ~y = mouse button state x y;;
2316 let motion ~x ~y =
2317 if state.outline = None
2318 then
2319 match state.mstate with
2320 | Mnone -> ()
2322 | Mpan (x0, y0) ->
2323 let dx = x - x0
2324 and dy = y0 - y in
2325 state.mstate <- Mpan (x, y);
2326 if conf.zoom > 1.0 then state.x <- state.x + dx;
2327 let y = clamp dy in
2328 gotoy_and_clear_text y
2330 | Msel (a, _) ->
2331 state.mstate <- Msel (a, (x, y));
2332 Glut.postRedisplay ()
2334 | Mscroll ->
2335 let y = min conf.winh (max 0 y) in
2336 let percent = float y /. float conf.winh in
2337 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2338 gotoy_and_clear_text y
2341 let pmotion ~x ~y =
2342 match state.birdseye with
2343 | Some (conf, leftx, pageno, hooverpageno) ->
2344 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2345 let rec loop = function
2346 | [] ->
2347 if hooverpageno != -1
2348 then (
2349 state.birdseye <- Some (conf, leftx, pageno, -1);
2350 Glut.postRedisplay ();
2352 | l :: rest ->
2353 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2354 && x > margin && x < margin + l.pagew
2355 then (
2356 state.birdseye <- Some (conf, leftx, pageno, l.pageno);
2357 Glut.postRedisplay ();
2359 else loop rest
2361 loop state.layout
2363 | None ->
2364 if state.outline = None
2365 then
2366 match state.mstate with
2367 | Mnone ->
2368 begin match getunder x y with
2369 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2370 | Ulinkuri uri ->
2371 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2372 Glut.setCursor Glut.CURSOR_INFO
2373 | Ulinkgoto (page, y) ->
2374 if conf.underinfo
2375 then showtext 'p' ("age: " ^ string_of_int page);
2376 Glut.setCursor Glut.CURSOR_INFO
2377 | Utext s ->
2378 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2379 Glut.setCursor Glut.CURSOR_TEXT
2382 | Mpan _ | Msel _ | Mscroll ->
2387 module State =
2388 struct
2389 open Parser
2391 let home =
2393 match Sys.os_type with
2394 | "Win32" -> Sys.getenv "HOMEPATH"
2395 | _ -> Sys.getenv "HOME"
2396 with exn ->
2397 prerr_endline
2398 ("Can not determine home directory location: " ^
2399 Printexc.to_string exn);
2403 let config_of c attrs =
2404 let apply c k v =
2406 match k with
2407 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2408 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2409 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2410 | "preload" -> { c with preload = bool_of_string v }
2411 | "page-bias" -> { c with pagebias = int_of_string v }
2412 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2413 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2414 | "crop-hack" -> { c with crophack = bool_of_string v }
2415 | "throttle" -> { c with showall = bool_of_string v }
2416 | "highlight-links" -> { c with hlinks = bool_of_string v }
2417 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2418 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2419 | "zoom" ->
2420 let zoom = float_of_string v /. 100. in
2421 let zoom = max 0.01 (min 2.2 zoom) in
2422 { c with zoom = zoom }
2423 | "presentation" -> { c with presentation = bool_of_string v }
2424 | "rotation-angle" -> { c with angle = int_of_string v }
2425 | "width" -> { c with winw = max 20 (int_of_string v) }
2426 | "height" -> { c with winh = max 20 (int_of_string v) }
2427 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2428 | "proportional-display" -> { c with proportional = bool_of_string v }
2429 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2430 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2431 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2432 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2433 | _ -> c
2434 with exn ->
2435 prerr_endline ("Error processing attribute (`" ^
2436 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2439 let rec fold c = function
2440 | [] -> c
2441 | (k, v) :: rest ->
2442 let c = apply c k v in
2443 fold c rest
2445 fold c attrs;
2448 let bookmark_of attrs =
2449 let rec fold title page rely = function
2450 | ("title", v) :: rest -> fold v page rely rest
2451 | ("page", v) :: rest -> fold title v rely rest
2452 | ("rely", v) :: rest -> fold title page v rest
2453 | _ :: rest -> fold title page rely rest
2454 | [] -> title, page, rely
2456 fold "invalid" "0" "0" attrs
2459 let setconf dst src =
2460 dst.scrollw <- src.scrollw;
2461 dst.scrollh <- src.scrollh;
2462 dst.icase <- src.icase;
2463 dst.preload <- src.preload;
2464 dst.pagebias <- src.pagebias;
2465 dst.verbose <- src.verbose;
2466 dst.scrollincr <- src.scrollincr;
2467 dst.maxhfit <- src.maxhfit;
2468 dst.crophack <- src.crophack;
2469 dst.autoscroll <- src.autoscroll;
2470 dst.showall <- src.showall;
2471 dst.hlinks <- src.hlinks;
2472 dst.underinfo <- src.underinfo;
2473 dst.interpagespace <- src.interpagespace;
2474 dst.zoom <- src.zoom;
2475 dst.presentation <- src.presentation;
2476 dst.angle <- src.angle;
2477 dst.winw <- src.winw;
2478 dst.winh <- src.winh;
2479 dst.savebmarks <- src.savebmarks;
2480 dst.memlimit <- src.memlimit;
2481 dst.proportional <- src.proportional;
2482 dst.texcount <- src.texcount;
2483 dst.sliceheight <- src.sliceheight;
2484 dst.thumbw <- src.thumbw;
2487 let unent s =
2488 let l = String.length s in
2489 let b = Buffer.create l in
2490 unent b s 0 l;
2491 Buffer.contents b;
2494 let get s =
2495 let h = Hashtbl.create 10 in
2496 let dc = { defconf with angle = defconf.angle } in
2497 let rec toplevel v t spos epos =
2498 match t with
2499 | Vdata | Vcdata | Vend -> v
2500 | Vopen ("llppconfig", attrs, closed) ->
2501 if closed
2502 then v
2503 else { v with f = llppconfig }
2504 | Vopen _ ->
2505 error "unexpected subelement at top level" s spos
2506 | Vclose tag -> error "unexpected close at top level" s spos
2508 and llppconfig v t spos epos =
2509 match t with
2510 | Vdata | Vcdata | Vend -> v
2511 | Vopen ("defaults", attrs, closed) ->
2512 let c = config_of dc attrs in
2513 setconf dc c;
2514 if closed
2515 then v
2516 else { v with f = skip "defaults" (fun () -> v) }
2518 | Vopen ("doc", attrs, closed) ->
2519 let pathent =
2521 List.assoc "path" attrs
2522 with Not_found -> error "doc is missing path attribute" s spos
2524 let path = unent pathent in
2525 let c = config_of dc attrs in
2526 let y =
2528 float_of_string (List.assoc "rely" attrs)
2529 with
2530 | Not_found -> 0.0
2531 | exn ->
2532 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2535 let x =
2537 int_of_string (List.assoc "pan" attrs)
2538 with
2539 | Not_found -> 0
2540 | exn ->
2541 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2544 if closed
2545 then (Hashtbl.add h path (c, [], x, y); v)
2546 else { v with f = doc path x y c [] }
2548 | Vopen (tag, _, closed) ->
2549 error "unexpected subelement in llppconfig" s spos
2551 | Vclose "llppconfig" -> { v with f = toplevel }
2552 | Vclose tag -> error "unexpected close in llppconfig" s spos
2554 and doc path x y c bookmarks v t spos epos =
2555 match t with
2556 | Vdata | Vcdata -> v
2557 | Vend -> error "unexpected end of input in doc" s spos
2558 | Vopen ("bookmarks", attrs, closed) ->
2559 { v with f = pbookmarks path x y c bookmarks }
2561 | Vopen (tag, _, _) ->
2562 error "unexpected subelement in doc" s spos
2564 | Vclose "doc" ->
2565 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2566 { v with f = llppconfig }
2568 | Vclose tag -> error "unexpected close in doc" s spos
2570 and pbookmarks path x y c bookmarks v t spos epos =
2571 match t with
2572 | Vdata | Vcdata -> v
2573 | Vend -> error "unexpected end of input in bookmarks" s spos
2574 | Vopen ("item", attrs, closed) ->
2575 let titleent, spage, srely = bookmark_of attrs in
2576 let page =
2578 int_of_string spage
2579 with exn ->
2580 dolog "Failed to convert page %S to integer: %s"
2581 spage (Printexc.to_string exn);
2584 let rely =
2586 float_of_string srely
2587 with exn ->
2588 dolog "Failed to convert rely %S to real: %s"
2589 srely (Printexc.to_string exn);
2592 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2593 if closed
2594 then { v with f = pbookmarks path x y c bookmarks }
2595 else
2596 let f () = v in
2597 { v with f = skip "item" f }
2599 | Vopen _ ->
2600 error "unexpected subelement in bookmarks" s spos
2602 | Vclose "bookmarks" ->
2603 { v with f = doc path x y c bookmarks }
2605 | Vclose tag -> error "unexpected close in bookmarks" s spos
2607 and skip tag f v t spos epos =
2608 match t with
2609 | Vdata | Vcdata -> v
2610 | Vend ->
2611 error ("unexpected end of input in skipped " ^ tag) s spos
2612 | Vopen (tag', _, closed) ->
2613 if closed
2614 then v
2615 else
2616 let f' () = { v with f = skip tag f } in
2617 { v with f = skip tag' f' }
2618 | Vclose ctag ->
2619 if tag = ctag
2620 then f ()
2621 else error ("unexpected close in skipped " ^ tag) s spos
2624 parse { f = toplevel; accu = () } s;
2625 h, dc;
2628 let do_load f ic =
2630 let len = in_channel_length ic in
2631 let s = String.create len in
2632 really_input ic s 0 len;
2633 f s;
2634 with
2635 | Parse_error (msg, s, pos) ->
2636 let subs = subs s pos in
2637 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2638 failwith ("parse error: " ^ s)
2640 | exn ->
2641 failwith ("config load error: " ^ Printexc.to_string exn)
2644 let path =
2645 let dir =
2647 let dir = Filename.concat home ".config" in
2648 if Sys.is_directory dir then dir else home
2649 with _ -> home
2651 Filename.concat dir "llpp.conf"
2654 let load1 f =
2655 if Sys.file_exists path
2656 then
2657 match
2658 (try Some (open_in_bin path)
2659 with exn ->
2660 prerr_endline
2661 ("Error opening configuation file `" ^ path ^ "': " ^
2662 Printexc.to_string exn);
2663 None
2665 with
2666 | Some ic ->
2667 begin try
2668 f (do_load get ic)
2669 with exn ->
2670 prerr_endline
2671 ("Error loading configuation from `" ^ path ^ "': " ^
2672 Printexc.to_string exn);
2673 end;
2674 close_in ic;
2676 | None -> ()
2677 else
2678 f (Hashtbl.create 0, defconf)
2681 let load () =
2682 let f (h, dc) =
2683 let pc, pb, px, py =
2685 Hashtbl.find h state.path
2686 with Not_found -> dc, [], 0, 0.0
2688 setconf defconf dc;
2689 setconf conf pc;
2690 state.bookmarks <- pb;
2691 state.x <- px;
2692 cbput state.hists.nav py;
2694 load1 f
2697 let add_attrs bb always dc c =
2698 let ob s a b =
2699 if always || a != b
2700 then Printf.bprintf bb "\n %s='%b'" s a
2701 and oi s a b =
2702 if always || a != b
2703 then Printf.bprintf bb "\n %s='%d'" s a
2704 and oz s a b =
2705 if always || a <> b
2706 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2708 let w, h =
2709 if always
2710 then dc.winw, dc.winh
2711 else
2712 match state.fullscreen with
2713 | Some wh -> wh
2714 | None -> c.winw, c.winh
2716 let zoom, presentation, interpagespace, showall=
2717 if always
2718 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2719 else
2720 match state.birdseye with
2721 | Some (bc, _, _, _) ->
2722 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2723 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2725 oi "width" w dc.winw;
2726 oi "height" h dc.winh;
2727 oi "scroll-bar-width" c.scrollw dc.scrollw;
2728 oi "scroll-handle-height" c.scrollh dc.scrollh;
2729 ob "case-insensitive-search" c.icase dc.icase;
2730 ob "preload" c.preload dc.preload;
2731 oi "page-bias" c.pagebias dc.pagebias;
2732 oi "scroll-step" c.scrollincr dc.scrollincr;
2733 ob "max-height-fit" c.maxhfit dc.maxhfit;
2734 ob "crop-hack" c.crophack dc.crophack;
2735 ob "throttle" showall dc.showall;
2736 ob "highlight-links" c.hlinks dc.hlinks;
2737 ob "under-cursor-info" c.underinfo dc.underinfo;
2738 oi "vertical-margin" interpagespace dc.interpagespace;
2739 oz "zoom" zoom dc.zoom;
2740 ob "presentation" presentation dc.presentation;
2741 oi "rotation-angle" c.angle dc.angle;
2742 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2743 ob "proportional-display" c.proportional dc.proportional;
2744 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2745 oi "texcount" c.texcount dc.texcount;
2746 oi "slice-height" c.sliceheight dc.sliceheight;
2747 oi "thumbnail-width" c.thumbw dc.thumbw;
2750 let save () =
2751 let bb = Buffer.create 32768 in
2752 let f (h, dc) =
2753 Buffer.add_string bb "<llppconfig>\n<defaults ";
2754 add_attrs bb true dc dc;
2755 Buffer.add_string bb "/>\n";
2757 let adddoc path x y c bookmarks =
2758 if bookmarks == [] && c = dc && y = 0.0
2759 then ()
2760 else (
2761 Printf.bprintf bb "<doc path='%s'"
2762 (enent path 0 (String.length path));
2764 if y <> 0.0
2765 then Printf.bprintf bb " rely='%f'" y;
2767 if x != 0
2768 then Printf.bprintf bb " pan='%d'" x;
2770 add_attrs bb false dc c;
2772 begin match bookmarks with
2773 | [] -> Buffer.add_string bb "/>\n"
2774 | _ ->
2775 Buffer.add_string bb ">\n<bookmarks>\n";
2776 List.iter (fun (title, _level, page, rely) ->
2777 Printf.bprintf bb
2778 "<item title='%s' page='%d' rely='%f'/>\n"
2779 (enent title 0 (String.length title))
2780 page
2781 rely
2782 ) bookmarks;
2783 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2784 end;
2788 let x =
2789 match state.birdseye with
2790 | Some (_, x, _, _) -> x
2791 | None -> state.x
2793 adddoc state.path x (yratio state.y) conf
2794 (if conf.savebmarks then state.bookmarks else []);
2796 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2797 if path <> state.path
2798 then
2799 adddoc path x y c bookmarks
2800 ) h;
2801 Buffer.add_string bb "</llppconfig>";
2803 load1 f;
2804 if Buffer.length bb > 0
2805 then
2807 let tmp = path ^ ".tmp" in
2808 let oc = open_out_bin tmp in
2809 Buffer.output_buffer oc bb;
2810 close_out oc;
2811 Sys.rename tmp path;
2812 with exn ->
2813 prerr_endline
2814 ("error while saving configuration: " ^ Printexc.to_string exn)
2816 end;;
2818 let () =
2819 Arg.parse
2820 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2821 (fun s -> state.path <- s)
2822 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2824 let path =
2825 if String.length state.path = 0
2826 then (prerr_endline "filename missing"; exit 1)
2827 else (
2828 if Filename.is_relative state.path
2829 then Filename.concat (Sys.getcwd ()) state.path
2830 else state.path
2833 state.path <- path;
2835 State.load ();
2837 let _ = Glut.init Sys.argv in
2838 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2839 let () = Glut.initWindowSize conf.winw conf.winh in
2840 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2842 let csock, ssock =
2843 if Sys.os_type = "Unix"
2844 then
2845 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2846 else
2847 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2848 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2849 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2850 Unix.bind sock addr;
2851 Unix.listen sock 1;
2852 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2853 Unix.connect csock addr;
2854 let ssock, _ = Unix.accept sock in
2855 Unix.close sock;
2856 let opts sock =
2857 Unix.setsockopt sock Unix.TCP_NODELAY true;
2858 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2860 opts ssock;
2861 opts csock;
2862 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2863 ssock, csock
2866 let () = Glut.displayFunc display in
2867 let () = Glut.reshapeFunc reshape in
2868 let () = Glut.keyboardFunc keyboard in
2869 let () = Glut.specialFunc special in
2870 let () = Glut.idleFunc (Some idle) in
2871 let () = Glut.mouseFunc mouse in
2872 let () = Glut.motionFunc motion in
2873 let () = Glut.passiveMotionFunc pmotion in
2875 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2876 state.csock <- csock;
2877 state.ssock <- ssock;
2878 state.text <- "Opening " ^ path;
2879 writeopen state.path state.password;
2881 at_exit State.save;
2883 let rec handlelablglutbug () =
2885 Glut.mainLoop ();
2886 with Glut.BadEnum "key in special_of_int" ->
2887 showtext '!' " LablGlut bug: special key not recognized";
2888 handlelablglutbug ()
2890 handlelablglutbug ();