Be less ad-hoc
[llpp.git] / main.ml
blobbd2fbe43b8c59bcb479296eeb3f9285bc9a6b1d8
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 * 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 gen : gen
199 ; mutable throttle : layout list option
200 ; hists : hists
202 and hists =
203 { pat : string circbuf
204 ; pag : string circbuf
205 ; nav : float circbuf
209 let defconf =
210 { scrollw = 7
211 ; scrollh = 12
212 ; icase = true
213 ; preload = true
214 ; pagebias = 0
215 ; verbose = false
216 ; scrollincr = 24
217 ; maxhfit = true
218 ; crophack = false
219 ; autoscroll = false
220 ; showall = false
221 ; hlinks = false
222 ; underinfo = false
223 ; interpagespace = 2
224 ; zoom = 1.0
225 ; presentation = false
226 ; angle = 0
227 ; winw = 900
228 ; winh = 900
229 ; savebmarks = true
230 ; proportional = true
231 ; memlimit = 32*1024*1024
232 ; texcount = 256
233 ; sliceheight = 24
237 let conf = { defconf with angle = defconf.angle };;
239 let state =
240 { csock = Unix.stdin
241 ; ssock = Unix.stdin
242 ; w = 0
243 ; y = 0
244 ; x = 0
245 ; layout = []
246 ; maxy = max_int
247 ; pagemap = Hashtbl.create 10
248 ; pagecache = cbnew 100 ""
249 ; pdims = []
250 ; pagecount = 0
251 ; rendering = false
252 ; mstate = Mnone
253 ; rects = []
254 ; rects1 = []
255 ; text = ""
256 ; fullscreen = None
257 ; birdseye = None
258 ; textentry = None
259 ; searchpattern = ""
260 ; outlines = Olist []
261 ; outline = None
262 ; bookmarks = []
263 ; path = ""
264 ; password = ""
265 ; invalidated = 0
266 ; hists =
267 { nav = cbnew 100 0.0
268 ; pat = cbnew 20 ""
269 ; pag = cbnew 10 ""
271 ; colorscale = 1.0
272 ; memused = 0
273 ; gen = 0
274 ; throttle = None
278 let vlog fmt =
279 if conf.verbose
280 then
281 Printf.kprintf prerr_endline fmt
282 else
283 Printf.kprintf ignore fmt
286 let writecmd fd s =
287 let len = String.length s in
288 let n = 4 + len in
289 let b = Buffer.create n in
290 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
291 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
292 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
293 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
294 Buffer.add_string b s;
295 let s' = Buffer.contents b in
296 let n' = Unix.write fd s' 0 n in
297 if n' != n then failwith "write failed";
300 let readcmd fd =
301 let s = "xxxx" in
302 let n = Unix.read fd s 0 4 in
303 if n != 4 then failwith "incomplete read(len)";
304 let len = 0
305 lor (Char.code s.[0] lsl 24)
306 lor (Char.code s.[1] lsl 16)
307 lor (Char.code s.[2] lsl 8)
308 lor (Char.code s.[3] lsl 0)
310 let s = String.create len in
311 let n = Unix.read fd s 0 len in
312 if n != len then failwith "incomplete read(data)";
316 let yratio y =
317 if y = state.maxy
318 then 1.0
319 else float y /. float state.maxy
322 let makecmd s l =
323 let b = Buffer.create 10 in
324 Buffer.add_string b s;
325 let rec combine = function
326 | [] -> b
327 | x :: xs ->
328 Buffer.add_char b ' ';
329 let s =
330 match x with
331 | `b b -> if b then "1" else "0"
332 | `s s -> s
333 | `i i -> string_of_int i
334 | `f f -> string_of_float f
335 | `I f -> string_of_int (truncate f)
337 Buffer.add_string b s;
338 combine xs;
340 combine l;
343 let wcmd s l =
344 let cmd = Buffer.contents (makecmd s l) in
345 writecmd state.csock cmd;
348 let calcips h =
349 if conf.presentation
350 then
351 let d = conf.winh - h in
352 max 0 ((d + 1) / 2)
353 else
354 conf.interpagespace
357 let calcheight () =
358 let rec f pn ph pi fh l =
359 match l with
360 | (n, _, h, _) :: rest ->
361 let ips = calcips h in
362 let fh =
363 if conf.presentation
364 then fh+ips
365 else (
366 if state.birdseye <> None && pn = 0
367 then fh + ips
368 else fh
371 let fh = fh + ((n - pn) * (ph + pi)) in
372 f n h ips fh rest
374 | [] ->
375 let inc =
376 if conf.presentation || (state.birdseye <> None && pn = 0)
377 then 0
378 else -pi
380 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
381 max 0 fh
383 let fh = f 0 0 0 0 state.pdims in
387 let getpageyh pageno =
388 let rec f pn ph pi y l =
389 match l with
390 | (n, _, h, _) :: rest ->
391 let ips = calcips h in
392 if n >= pageno
393 then
394 if conf.presentation && n = pageno
395 then
396 y + (pageno - pn) * (ph + pi) + pi, h
397 else
398 y + (pageno - pn) * (ph + pi), h
399 else
400 let y = y + (if conf.presentation then pi else 0) in
401 let y = y + (n - pn) * (ph + pi) in
402 f n h ips y rest
404 | [] ->
405 y + (pageno - pn) * (ph + pi), ph
407 f 0 0 0 0 state.pdims
410 let getpagey pageno = fst (getpageyh pageno);;
412 let layout y sh =
413 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
414 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
415 match pdims with
416 | (pageno', w, h, x) :: rest when pageno' = pageno ->
417 let ips = calcips h in
418 let yinc =
419 if conf.presentation || (state.birdseye <> None && pageno = 0)
420 then ips
421 else 0
423 (w, h, ips, x), rest, pdimno + 1, yinc
424 | _ ->
425 prev, pdims, pdimno, 0
427 let dy = dy + yinc in
428 let py = py + yinc in
429 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
430 then
431 accu
432 else
433 let vy = y + dy in
434 if py + h <= vy - yinc
435 then
436 let py = py + h + ips in
437 let dy = max 0 (py - y) in
438 f ~pageno:(pageno+1)
439 ~pdimno
440 ~prev:curr
443 ~pdims:rest
444 ~cacheleft
445 ~accu
446 else
447 let pagey = vy - py in
448 let pagevh = h - pagey in
449 let pagevh = min (sh - dy) pagevh in
450 let off = if yinc > 0 then py - vy else 0 in
451 let py = py + h + ips in
452 let e =
453 { pageno = pageno
454 ; pagedimno = pdimno
455 ; pagew = w
456 ; pageh = h
457 ; pagedispy = dy + off
458 ; pagey = pagey + off
459 ; pagevh = pagevh - off
460 ; pagex = x
463 let accu = e :: accu in
464 f ~pageno:(pageno+1)
465 ~pdimno
466 ~prev:curr
468 ~dy:(dy+pagevh+ips)
469 ~pdims:rest
470 ~cacheleft:(cacheleft-1)
471 ~accu
473 if state.invalidated = 0
474 then (
475 let accu =
477 ~pageno:0
478 ~pdimno:~-1
479 ~prev:(0,0,0,0)
480 ~py:0
481 ~dy:0
482 ~pdims:state.pdims
483 ~cacheleft:(cbcap state.pagecache)
484 ~accu:[]
486 List.rev accu
488 else
492 let clamp incr =
493 let y = state.y + incr in
494 let y = max 0 y in
495 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
499 let getopaque pageno =
500 try Some (Hashtbl.find state.pagemap
501 (pageno, state.w, conf.angle, conf.proportional, state.gen))
502 with Not_found -> None
505 let cache pageno opaque =
506 Hashtbl.replace state.pagemap
507 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
510 let validopaque opaque = String.length opaque > 0;;
512 let render l =
513 match getopaque l.pageno with
514 | None when not state.rendering ->
515 state.rendering <- true;
516 cache l.pageno ("", -1);
517 wcmd "render" [`i (l.pageno + 1)
518 ;`i l.pagedimno
519 ;`i l.pagew
520 ;`i l.pageh];
522 | _ -> ()
525 let loadlayout layout =
526 let rec f all = function
527 | l :: ls ->
528 begin match getopaque l.pageno with
529 | None -> render l; f false ls
530 | Some (opaque, _) -> f (all && validopaque opaque) ls
532 | [] -> all
534 f (layout <> []) layout;
537 let findpageforopaque opaque =
538 Hashtbl.fold
539 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
540 state.pagemap None
543 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
545 let preload () =
546 let oktopreload =
547 if conf.preload
548 then
549 let memleft = conf.memlimit - state.memused in
550 if memleft < 0
551 then
552 let opaque = cbpeek state.pagecache in
553 match findpageforopaque opaque with
554 | Some ((n, _, _, _, _), size) ->
555 memleft + size >= 0 && not (pagevisible state.layout n)
556 | None -> false
557 else true
558 else false
560 if oktopreload
561 then
562 let presentation = conf.presentation in
563 let interpagespace = conf.interpagespace in
564 let maxy = state.maxy in
565 conf.presentation <- false;
566 conf.interpagespace <- 0;
567 state.maxy <- calcheight ();
568 let y =
569 match state.layout with
570 | [] -> 0
571 | l :: _ -> getpagey l.pageno
573 let y = if y < conf.winh then 0 else y - conf.winh in
574 let pages = layout y (conf.winh*3) in
575 List.iter render pages;
576 conf.presentation <- presentation;
577 conf.interpagespace <- interpagespace;
578 state.maxy <- maxy;
581 let gotoy y =
582 let y = max 0 y in
583 let y = min state.maxy y in
584 let pages = layout y conf.winh in
585 let ready = loadlayout pages in
586 if conf.showall
587 then (
588 if ready
589 then (
590 state.y <- y;
591 state.layout <- pages;
592 state.throttle <- None;
593 Glut.postRedisplay ();
595 else (
596 state.throttle <- Some pages;
599 else (
600 state.y <- y;
601 state.layout <- pages;
602 state.throttle <- None;
603 Glut.postRedisplay ();
605 begin match state.birdseye with
606 | Some (conf, leftx, pageno, hooverpageno) ->
607 if not (pagevisible pages pageno)
608 then (
609 match state.layout with
610 | [] -> ()
611 | l :: _ ->
612 state.birdseye <- Some (conf, leftx, l.pageno, hooverpageno)
614 | _ -> ()
615 end;
616 preload ();
619 let gotoy_and_clear_text y =
620 gotoy y;
621 if not conf.verbose then state.text <- "";
624 let addnav () =
625 cbput state.hists.nav (yratio state.y);
628 let getnav () =
629 let y = cbgetc state.hists.nav ~-1 in
630 truncate (y *. float state.maxy)
633 let gotopage n top =
634 let y, h = getpageyh n in
635 addnav ();
636 gotoy_and_clear_text (y + (truncate (top *. float h)));
639 let gotopage1 n top =
640 let y = getpagey n in
641 addnav ();
642 gotoy_and_clear_text (y + top);
645 let invalidate () =
646 state.layout <- [];
647 state.pdims <- [];
648 state.rects <- [];
649 state.rects1 <- [];
650 state.invalidated <- state.invalidated + 1;
653 let scalecolor c =
654 let c = c *. state.colorscale in
655 (c, c, c);
658 let represent () =
659 let maxy = calcheight () in
660 let y =
661 match state.birdseye with
662 | None ->
663 begin match state.layout with
664 | [] ->
665 let rely = yratio state.y in
666 truncate (float maxy *. rely)
668 | l :: _ -> getpagey l.pageno
669 end;
671 | Some (conf, leftx, pageno, hooverpageno) ->
672 let rely = yratio state.y in
673 truncate (float maxy *. rely)
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 birdseyeon () =
1194 let zoom = 76.0 /. float state.w in
1195 let birdseyepageno =
1196 match state.layout with
1197 | [] -> 0
1198 | l :: _ -> l.pageno
1200 state.birdseye <-
1201 Some ({ conf with zoom = conf.zoom }, state.x, birdseyepageno, -1);
1202 conf.zoom <- zoom;
1203 conf.presentation <- false;
1204 conf.interpagespace <- 10;
1205 conf.hlinks <- false;
1206 state.x <- 0;
1207 state.mstate <- Mnone;
1208 conf.showall <- false;
1209 Glut.setCursor Glut.CURSOR_INHERIT;
1210 if conf.verbose
1211 then
1212 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1213 (100.0*.zoom)
1214 else
1215 state.text <- ""
1219 let birdseyeoff (c, leftx, pageno, _) =
1220 state.birdseye <- None;
1221 conf.zoom <- c.zoom;
1222 conf.presentation <- c.presentation;
1223 conf.interpagespace <- c.interpagespace;
1224 conf.showall <- c.showall;
1225 conf.hlinks <- c.hlinks;
1226 state.x <- leftx;
1227 state.maxy <- calcheight ();
1228 state.y <- getpagey pageno;
1229 if conf.verbose
1230 then
1231 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1232 (100.0*.conf.zoom)
1236 let togglebirdseye () =
1237 match state.birdseye with
1238 | None -> birdseyeon ()
1239 | Some vals -> birdseyeoff vals
1242 let viewkeyboard ~key ~x ~y =
1243 let enttext te =
1244 state.textentry <- te;
1245 state.text <- "";
1246 enttext ();
1247 Glut.postRedisplay ()
1249 match state.textentry with
1250 | None ->
1251 let c = Char.chr key in
1252 begin match c with
1253 | '\027' | 'q' ->
1254 exit 0
1256 | '\008' ->
1257 let y = getnav () in
1258 gotoy_and_clear_text y
1260 | '\013' ->
1261 begin match state.birdseye with
1262 | None -> ()
1263 | Some vals ->
1264 birdseyeoff vals;
1265 reshape conf.winw conf.winh;
1266 end;
1268 | 'o' ->
1269 enteroutlinemode ()
1271 | 'u' ->
1272 state.rects <- [];
1273 state.text <- "";
1274 Glut.postRedisplay ()
1276 | '/' | '?' ->
1277 let ondone isforw s =
1278 cbput state.hists.pat s;
1279 state.searchpattern <- s;
1280 search s isforw
1282 enttext (Some (c, "", Some (onhist state.hists.pat),
1283 textentry, ondone (c ='/')))
1285 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1286 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1287 conf.zoom <- min 2.2 (conf.zoom +. incr);
1288 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1289 reshape conf.winw conf.winh
1291 | '+' ->
1292 let ondone s =
1293 let n =
1294 try int_of_string s with exc ->
1295 state.text <- Printf.sprintf "bad integer `%s': %s"
1296 s (Printexc.to_string exc);
1297 max_int
1299 if n != max_int
1300 then (
1301 conf.pagebias <- n;
1302 state.text <- "page bias is now " ^ string_of_int n;
1305 enttext (Some ('+', "", None, intentry, ondone))
1307 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1308 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1309 conf.zoom <- max 0.01 (conf.zoom -. decr);
1310 if conf.zoom <= 1.0 then state.x <- 0;
1311 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1312 reshape conf.winw conf.winh;
1314 | '-' ->
1315 let ondone msg =
1316 state.text <- msg;
1318 enttext (Some ('-', "", None, optentry, ondone))
1320 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1321 state.x <- 0;
1322 conf.zoom <- 1.0;
1323 state.text <- "zoom is 100%";
1324 reshape conf.winw conf.winh
1326 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1327 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1328 if zoom < 1.0
1329 then (
1330 conf.zoom <- zoom;
1331 state.x <- 0;
1332 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1333 reshape conf.winw conf.winh;
1336 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1337 togglebirdseye ();
1338 reshape conf.winw conf.winh;
1340 | '0' .. '9' ->
1341 let ondone s =
1342 let n =
1343 try int_of_string s with exc ->
1344 state.text <- Printf.sprintf "bad integer `%s': %s"
1345 s (Printexc.to_string exc);
1348 if n >= 0
1349 then (
1350 addnav ();
1351 cbput state.hists.pag (string_of_int n);
1352 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1355 let pageentry text key =
1356 match Char.unsafe_chr key with
1357 | 'g' -> TEdone text
1358 | _ -> intentry text key
1360 let text = "x" in text.[0] <- c;
1361 enttext (Some (':', text, Some (onhist state.hists.pag),
1362 pageentry, ondone))
1364 | 'b' ->
1365 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1366 reshape conf.winw conf.winh;
1368 | 'l' ->
1369 conf.hlinks <- not conf.hlinks;
1370 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1371 Glut.postRedisplay ()
1373 | 'a' ->
1374 conf.autoscroll <- not conf.autoscroll
1376 | 'P' ->
1377 conf.presentation <- not conf.presentation;
1378 showtext ' ' ("presentation mode " ^
1379 if conf.presentation then "on" else "off");
1380 represent ()
1382 | 'f' ->
1383 begin match state.fullscreen with
1384 | None ->
1385 state.fullscreen <- Some (conf.winw, conf.winh);
1386 Glut.fullScreen ()
1387 | Some (w, h) ->
1388 state.fullscreen <- None;
1389 doreshape w h
1392 | 'g' ->
1393 gotoy_and_clear_text 0
1395 | 'n' ->
1396 search state.searchpattern true
1398 | 'p' | 'N' ->
1399 search state.searchpattern false
1401 | 't' ->
1402 begin match state.layout with
1403 | [] -> ()
1404 | l :: _ ->
1405 gotoy_and_clear_text (getpagey l.pageno)
1408 | ' ' ->
1409 begin match List.rev state.layout with
1410 | [] -> ()
1411 | l :: _ ->
1412 let pageno = min (l.pageno+1) (state.pagecount-1) in
1413 gotoy_and_clear_text (getpagey pageno)
1416 | '\127' ->
1417 begin match state.layout with
1418 | [] -> ()
1419 | l :: _ ->
1420 let pageno = max 0 (l.pageno-1) in
1421 gotoy_and_clear_text (getpagey pageno)
1424 | '=' ->
1425 let f (fn, ln) l =
1426 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1428 let fn, ln = List.fold_left f (-1, -1) state.layout in
1429 let s =
1430 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1431 let percent =
1432 if maxy <= 0
1433 then 100.
1434 else (100. *. (float state.y /. float maxy)) in
1435 if fn = ln
1436 then
1437 Printf.sprintf "Page %d of %d %.2f%%"
1438 (fn+1) state.pagecount percent
1439 else
1440 Printf.sprintf
1441 "Pages %d-%d of %d %.2f%%"
1442 (fn+1) (ln+1) state.pagecount percent
1444 showtext ' ' s;
1446 | 'w' ->
1447 begin match state.layout with
1448 | [] -> ()
1449 | l :: _ ->
1450 doreshape (l.pagew + conf.scrollw) l.pageh;
1451 Glut.postRedisplay ();
1454 | '\'' ->
1455 enterbookmarkmode ()
1457 | 'm' ->
1458 let ondone s =
1459 match state.layout with
1460 | l :: _ ->
1461 state.bookmarks <-
1462 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1463 :: state.bookmarks
1464 | _ -> ()
1466 enttext (Some ('~', "", None, textentry, ondone))
1468 | '~' ->
1469 quickbookmark ();
1470 showtext ' ' "Quick bookmark added";
1472 | 'z' ->
1473 begin match state.layout with
1474 | l :: _ ->
1475 let rect = getpdimrect l.pagedimno in
1476 let w, h =
1477 if conf.crophack
1478 then
1479 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1480 truncate (1.2 *. (rect.(3) -. rect.(0))))
1481 else
1482 (truncate (rect.(1) -. rect.(0)),
1483 truncate (rect.(3) -. rect.(0)))
1485 if w != 0 && h != 0
1486 then
1487 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1489 Glut.postRedisplay ();
1491 | [] -> ()
1494 | '<' | '>' ->
1495 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1497 | '[' | ']' ->
1498 state.colorscale <-
1499 max 0.0
1500 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1501 Glut.postRedisplay ()
1503 | 'k' -> gotoy (clamp (-conf.scrollincr))
1504 | 'j' -> gotoy (clamp conf.scrollincr)
1506 | 'r' -> opendoc state.path state.password
1508 | _ ->
1509 vlog "huh? %d %c" key (Char.chr key);
1512 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1513 let len = String.length text in
1514 if len = 0
1515 then (
1516 state.textentry <- None;
1517 Glut.postRedisplay ();
1519 else (
1520 let s = String.sub text 0 (len - 1) in
1521 enttext (Some (c, s, opthist, onkey, ondone))
1524 | Some (c, text, onhist, onkey, ondone) ->
1525 begin match Char.unsafe_chr key with
1526 | '\r' | '\n' ->
1527 ondone text;
1528 state.textentry <- None;
1529 Glut.postRedisplay ()
1531 | '\027' ->
1532 begin match onhist with
1533 | None -> ()
1534 | Some (_, onhistcancel) -> onhistcancel ()
1535 end;
1536 state.textentry <- None;
1537 Glut.postRedisplay ()
1539 | _ ->
1540 begin match onkey text key with
1541 | TEdone text ->
1542 state.textentry <- None;
1543 ondone text;
1544 Glut.postRedisplay ()
1546 | TEcont text ->
1547 enttext (Some (c, text, onhist, onkey, ondone));
1549 | TEstop ->
1550 state.textentry <- None;
1551 Glut.postRedisplay ()
1553 | TEswitch te ->
1554 state.textentry <- Some te;
1555 Glut.postRedisplay ()
1556 end;
1557 end;
1560 let narrow outlines pattern =
1561 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1562 match reopt with
1563 | None -> None
1564 | Some re ->
1565 let rec fold accu n =
1566 if n = -1
1567 then accu
1568 else
1569 let (s, _, _, _) as o = outlines.(n) in
1570 let accu =
1571 if (try ignore (Str.search_forward re s 0); true
1572 with Not_found -> false)
1573 then (o :: accu)
1574 else accu
1576 fold accu (n-1)
1578 let matched = fold [] (Array.length outlines - 1) in
1579 if matched = [] then None else Some (Array.of_list matched)
1582 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1583 let search active pattern incr =
1584 let dosearch re =
1585 let rec loop n =
1586 if n = Array.length outlines || n = -1
1587 then None
1588 else
1589 let (s, _, _, _) = outlines.(n) in
1591 (try ignore (Str.search_forward re s 0); true
1592 with Not_found -> false)
1593 then Some n
1594 else loop (n + incr)
1596 loop active
1599 let re = Str.regexp_case_fold pattern in
1600 dosearch re
1601 with Failure s ->
1602 state.text <- s;
1603 None
1605 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1606 match key with
1607 | 27 ->
1608 if String.length qsearch = 0
1609 then (
1610 state.text <- "";
1611 state.outline <- None;
1612 Glut.postRedisplay ();
1614 else (
1615 state.text <- "";
1616 state.outline <- Some (allowdel, active, first, outlines, "");
1617 Glut.postRedisplay ();
1620 | 18 | 19 ->
1621 let incr = if key = 18 then -1 else 1 in
1622 let active, first =
1623 match search (active + incr) qsearch incr with
1624 | None ->
1625 state.text <- qsearch ^ " [not found]";
1626 active, first
1627 | Some active ->
1628 state.text <- qsearch;
1629 active, firstof active
1631 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1632 Glut.postRedisplay ();
1634 | 8 ->
1635 let len = String.length qsearch in
1636 if len = 0
1637 then ()
1638 else (
1639 if len = 1
1640 then (
1641 state.text <- "";
1642 state.outline <- Some (allowdel, active, first, outlines, "");
1644 else
1645 let qsearch = String.sub qsearch 0 (len - 1) in
1646 let active, first =
1647 match search active qsearch ~-1 with
1648 | None ->
1649 state.text <- qsearch ^ " [not found]";
1650 active, first
1651 | Some active ->
1652 state.text <- qsearch;
1653 active, firstof active
1655 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1657 Glut.postRedisplay ()
1659 | 13 ->
1660 if active < Array.length outlines
1661 then (
1662 let (_, _, n, t) = outlines.(active) in
1663 gotopage n t;
1665 state.text <- "";
1666 if allowdel then state.bookmarks <- Array.to_list outlines;
1667 state.outline <- None;
1668 Glut.postRedisplay ();
1670 | _ when key >= 32 && key < 127 ->
1671 let pattern = addchar qsearch (Char.chr key) in
1672 let active, first =
1673 match search active pattern 1 with
1674 | None ->
1675 state.text <- pattern ^ " [not found]";
1676 active, first
1677 | Some active ->
1678 state.text <- pattern;
1679 active, firstof active
1681 state.outline <- Some (allowdel, active, first, outlines, pattern);
1682 Glut.postRedisplay ()
1684 | 14 when not allowdel -> (* ctrl-n *)
1685 if String.length qsearch > 0
1686 then (
1687 let optoutlines = narrow outlines qsearch in
1688 begin match optoutlines with
1689 | None -> state.text <- "can't narrow"
1690 | Some outlines ->
1691 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1692 match state.outlines with
1693 | Olist l -> ()
1694 | Oarray a ->
1695 state.outlines <- Onarrow (qsearch, outlines, a)
1696 | Onarrow (pat, a, b) ->
1697 state.outlines <- Onarrow (qsearch, outlines, b)
1698 end;
1700 Glut.postRedisplay ()
1702 | 21 when not allowdel -> (* ctrl-u *)
1703 let outline =
1704 match state.outlines with
1705 | Oarray a -> a
1706 | Olist l ->
1707 let a = Array.of_list (List.rev l) in
1708 state.outlines <- Oarray a;
1710 | Onarrow (pat, a, b) ->
1711 state.outlines <- Oarray b;
1712 state.text <- "";
1715 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1716 Glut.postRedisplay ()
1718 | 12 ->
1719 state.outline <-
1720 Some (allowdel, active, firstof active, outlines, qsearch);
1721 Glut.postRedisplay ()
1723 | 127 when allowdel ->
1724 let len = Array.length outlines - 1 in
1725 if len = 0
1726 then (
1727 state.outline <- None;
1728 state.bookmarks <- [];
1730 else (
1731 let bookmarks = Array.init len
1732 (fun i ->
1733 let i = if i >= active then i + 1 else i in
1734 outlines.(i)
1737 state.outline <-
1738 Some (allowdel,
1739 min active (len-1),
1740 min first (len-1),
1741 bookmarks, qsearch)
1744 Glut.postRedisplay ()
1746 | _ -> log "unknown key %d" key
1749 let keyboard ~key ~x ~y =
1750 if key = 7
1751 then
1752 wcmd "interrupt" []
1753 else
1754 match state.outline with
1755 | None -> viewkeyboard ~key ~x ~y
1756 | Some outline -> outlinekeyboard ~key ~x ~y outline
1759 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno) =
1760 match key with
1761 | Glut.KEY_UP ->
1762 let pageno = max 0 (pageno - 1) in
1763 let move =
1764 let rec loop = function
1765 | [] -> true
1766 | l :: rest ->
1767 if l.pageno = pageno
1768 then l.pagey != 0
1769 else loop rest
1771 loop state.layout
1773 if move
1774 then gotopage1 pageno 0
1775 else Glut.postRedisplay ();
1776 state.birdseye <- Some (conf, leftx, pageno, hooverpageno)
1778 | Glut.KEY_DOWN ->
1779 let pageno = min (state.pagecount - 1) (pageno + 1) in
1780 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1781 if not (pagevisible state.layout pageno)
1782 then
1783 begin match List.rev state.layout with
1784 | [] -> gotopage pageno 0.0
1785 | l :: _ ->
1786 gotoy (state.y + conf.interpagespace + l.pageh*2 - l.pagevh)
1788 else Glut.postRedisplay ();
1790 | Glut.KEY_PAGE_UP ->
1791 begin match state.layout with
1792 | l :: _ ->
1793 if l.pageno = pageno
1794 then (
1795 match layout (state.y - conf.winh) conf.winh with
1796 | [] -> gotoy (clamp (-conf.winh))
1797 | l :: _ ->
1798 let pageno = max 0 (l.pageno - 1) in
1799 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1800 gotopage pageno 0.0
1802 else (
1803 let pageno = max 0 (l.pageno - 1) in
1804 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1805 gotopage pageno 0.0
1807 | [] -> gotoy (clamp (-conf.winh))
1808 end;
1809 | Glut.KEY_PAGE_DOWN ->
1810 begin match List.rev state.layout with
1811 | l :: _ ->
1812 let pageno = min l.pageno (state.pagecount - 1) in
1813 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1814 gotoy (clamp (l.pagedispy + l.pageh))
1815 | [] -> gotoy (clamp conf.winh)
1816 end;
1818 | Glut.KEY_HOME ->
1819 state.birdseye <- Some (conf, leftx, 0, hooverpageno);
1820 gotopage 0 0.0
1821 | Glut.KEY_END ->
1822 let pageno = state.pagecount - 1 in
1823 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1824 if not (pagevisible state.layout pageno)
1825 then gotopage pageno 0.0
1826 else Glut.postRedisplay ();
1827 | _ -> ()
1830 let special ~key ~x ~y =
1831 match state.outline, state.birdseye with
1832 | None, _ when key = Glut.KEY_F9 ->
1833 togglebirdseye ();
1834 reshape conf.winw conf.winh;
1836 | None, (Some vals) ->
1837 birdseyespecial key x y vals
1839 | None, None ->
1840 begin match state.textentry with
1841 | None ->
1842 let y =
1843 match key with
1844 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1845 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1846 | Glut.KEY_DOWN -> clamp conf.scrollincr
1847 | Glut.KEY_PAGE_UP ->
1848 if Glut.getModifiers () land Glut.active_ctrl != 0
1849 then
1850 match state.layout with
1851 | [] -> state.y
1852 | l :: _ -> state.y - l.pagey
1853 else
1854 clamp (-conf.winh)
1855 | Glut.KEY_PAGE_DOWN ->
1856 if Glut.getModifiers () land Glut.active_ctrl != 0
1857 then
1858 match List.rev state.layout with
1859 | [] -> state.y
1860 | l :: _ -> getpagey l.pageno
1861 else
1862 clamp conf.winh
1863 | Glut.KEY_HOME -> addnav (); 0
1864 | Glut.KEY_END ->
1865 addnav ();
1866 state.maxy - (if conf.maxhfit then conf.winh else 0)
1868 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1869 state.x <- state.x - 10;
1870 state.y
1871 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1872 state.x <- state.x + 10;
1873 state.y
1875 | _ -> state.y
1877 gotoy_and_clear_text y
1879 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1880 let s =
1881 match key with
1882 | Glut.KEY_UP -> action HCprev
1883 | Glut.KEY_DOWN -> action HCnext
1884 | Glut.KEY_HOME -> action HCfirst
1885 | Glut.KEY_END -> action HClast
1886 | _ -> state.text
1888 state.textentry <- Some (c, s, onhist, onkey, ondone);
1889 Glut.postRedisplay ()
1891 | _ -> ()
1894 | Some (allowdel, active, first, outlines, qsearch), _ ->
1895 let maxrows = maxoutlinerows () in
1896 let calcfirst first active =
1897 if active > first
1898 then
1899 let rows = active - first in
1900 if rows > maxrows then active - maxrows else first
1901 else active
1903 let navigate incr =
1904 let active = active + incr in
1905 let active = max 0 (min active (Array.length outlines - 1)) in
1906 let first = calcfirst first active in
1907 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1908 Glut.postRedisplay ()
1910 let updownlevel incr =
1911 let len = Array.length outlines in
1912 let (_, curlevel, _, _) = outlines.(active) in
1913 let rec flow i =
1914 if i = len then i-1 else if i = -1 then 0 else
1915 let (_, l, _, _) = outlines.(i) in
1916 if l != curlevel then i else flow (i+incr)
1918 let active = flow active in
1919 let first = calcfirst first active in
1920 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1921 Glut.postRedisplay ()
1923 match key with
1924 | Glut.KEY_UP -> navigate ~-1
1925 | Glut.KEY_DOWN -> navigate 1
1926 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1927 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1929 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1930 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1932 | Glut.KEY_HOME ->
1933 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1934 Glut.postRedisplay ()
1936 | Glut.KEY_END ->
1937 let active = Array.length outlines - 1 in
1938 let first = max 0 (active - maxrows) in
1939 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1940 Glut.postRedisplay ()
1942 | _ -> ()
1945 let drawplaceholder l =
1946 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1947 GlDraw.rect
1948 (float l.pagex, float l.pagedispy)
1949 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1951 let x = float (if margin < 0 then -margin else l.pagex)
1952 and y = float (l.pagedispy + 13) in
1953 let font = Glut.BITMAP_8_BY_13 in
1954 GlDraw.color (0.0, 0.0, 0.0);
1955 GlPix.raster_pos ~x ~y ();
1956 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1957 ("Loading " ^ string_of_int (l.pageno + 1));
1960 let now () = Unix.gettimeofday ();;
1962 let drawpage l =
1963 if state.textentry = None
1964 then (
1965 match state.birdseye with
1966 | None -> GlDraw.color (scalecolor 1.0);
1967 | Some (_, _, pageno, hooverpageno) ->
1968 let color =
1969 if l.pageno = pageno
1970 then 1.0
1971 else (
1972 if l.pageno = hooverpageno
1973 then 0.9
1974 else 0.8
1977 GlDraw.color (scalecolor color);
1979 else GlDraw.color (scalecolor 0.4);
1980 begin match getopaque l.pageno with
1981 | Some (opaque, _) when validopaque opaque ->
1982 let a = now () in
1983 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1984 opaque;
1985 let b = now () in
1986 let d = b-.a in
1987 vlog "draw %d %f sec" l.pageno d;
1989 | _ ->
1990 drawplaceholder l;
1991 end;
1994 let scrollph y =
1995 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1996 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1997 let sh = float conf.winh /. sh in
1998 let sh = max sh (float conf.scrollh) in
2000 let percent =
2001 if state.y = state.maxy
2002 then 1.0
2003 else float y /. float maxy
2005 let position = (float conf.winh -. sh) *. percent in
2007 let position =
2008 if position +. sh > float conf.winh
2009 then float conf.winh -. sh
2010 else position
2012 position, sh;
2015 let scrollindicator () =
2016 GlDraw.color (0.64 , 0.64, 0.64);
2017 GlDraw.rect
2018 (float (conf.winw - conf.scrollw), 0.)
2019 (float conf.winw, float conf.winh)
2021 GlDraw.color (0.0, 0.0, 0.0);
2023 let position, sh = scrollph state.y in
2024 GlDraw.rect
2025 (float (conf.winw - conf.scrollw), position)
2026 (float conf.winw, position +. sh)
2030 let showsel margin =
2031 match state.mstate with
2032 | Mnone | Mscroll _ | Mpan _ ->
2035 | Msel ((x0, y0), (x1, y1)) ->
2036 let rec loop = function
2037 | l :: ls ->
2038 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2039 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2040 then
2041 match getopaque l.pageno with
2042 | Some (opaque, _) when validopaque opaque ->
2043 let oy = -l.pagey + l.pagedispy in
2044 seltext opaque
2045 (x0 - margin - state.x, y0,
2046 x1 - margin - state.x, y1) oy;
2048 | _ -> ()
2049 else loop ls
2050 | [] -> ()
2052 loop state.layout
2055 let showrects () =
2056 let panx = float state.x in
2057 Gl.enable `blend;
2058 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2059 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2060 List.iter
2061 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2062 List.iter (fun l ->
2063 if l.pageno = pageno
2064 then (
2065 let d = float (l.pagedispy - l.pagey) in
2066 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2067 GlDraw.begins `quads;
2069 GlDraw.vertex2 (x0+.panx, y0+.d);
2070 GlDraw.vertex2 (x1+.panx, y1+.d);
2071 GlDraw.vertex2 (x2+.panx, y2+.d);
2072 GlDraw.vertex2 (x3+.panx, y3+.d);
2074 GlDraw.ends ();
2076 ) state.layout
2077 ) state.rects
2079 Gl.disable `blend;
2082 let showoutline = function
2083 | None -> ()
2084 | Some (allowdel, active, first, outlines, qsearch) ->
2085 Gl.enable `blend;
2086 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2087 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2088 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2089 Gl.disable `blend;
2091 GlDraw.color (1., 1., 1.);
2092 let font = Glut.BITMAP_9_BY_15 in
2093 let draw_string x y s =
2094 GlPix.raster_pos ~x ~y ();
2095 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2097 let rec loop row =
2098 if row = Array.length outlines || (row - first) * 16 > conf.winh
2099 then ()
2100 else (
2101 let (s, l, _, _) = outlines.(row) in
2102 let y = (row - first) * 16 in
2103 let x = 5 + 15*l in
2104 if row = active
2105 then (
2106 Gl.enable `blend;
2107 GlDraw.polygon_mode `both `line;
2108 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2109 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2110 GlDraw.rect (0., float (y + 1))
2111 (float (conf.winw - 1), float (y + 18));
2112 GlDraw.polygon_mode `both `fill;
2113 Gl.disable `blend;
2114 GlDraw.color (1., 1., 1.);
2116 draw_string (float x) (float (y + 16)) s;
2117 loop (row+1)
2120 loop first
2123 let display () =
2124 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2125 GlDraw.viewport margin 0 state.w conf.winh;
2126 pagematrix ();
2127 GlClear.color (scalecolor 0.5);
2128 GlClear.clear [`color];
2129 if state.x != 0
2130 then (
2131 let x = float state.x in
2132 GlMat.translate ~x ();
2134 if conf.zoom > 1.0
2135 then (
2136 Gl.enable `scissor_test;
2137 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2139 List.iter drawpage state.layout;
2140 if conf.zoom > 1.0
2141 then
2142 Gl.disable `scissor_test
2144 if state.x != 0
2145 then (
2146 let x = -.float state.x in
2147 GlMat.translate ~x ();
2149 showrects ();
2150 showsel margin;
2151 GlDraw.viewport 0 0 conf.winw conf.winh;
2152 winmatrix ();
2153 scrollindicator ();
2154 showoutline state.outline;
2155 enttext ();
2156 Glut.swapBuffers ();
2159 let getunder x y =
2160 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2161 let x = x - margin - state.x in
2162 let rec f = function
2163 | l :: rest ->
2164 begin match getopaque l.pageno with
2165 | Some (opaque, _) when validopaque opaque ->
2166 let y = y - l.pagedispy in
2167 if y > 0
2168 then
2169 let y = l.pagey + y in
2170 let x = x - l.pagex in
2171 match whatsunder opaque x y with
2172 | Unone -> f rest
2173 | under -> under
2174 else
2175 f rest
2176 | _ ->
2177 f rest
2179 | [] -> Unone
2181 f state.layout
2184 let mouse ~button ~bstate ~x ~y =
2185 match button with
2186 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2187 let incr =
2188 if n = 3
2189 then
2190 -conf.scrollincr
2191 else
2192 conf.scrollincr
2194 let incr = incr * 2 in
2195 let y = clamp incr in
2196 gotoy_and_clear_text y
2198 | Glut.LEFT_BUTTON when state.outline = None
2199 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2200 if bstate = Glut.DOWN
2201 then (
2202 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2203 state.mstate <- Mpan (x, y)
2205 else
2206 state.mstate <- Mnone
2208 | Glut.LEFT_BUTTON
2209 when state.outline = None && x > conf.winw - conf.scrollw ->
2210 if bstate = Glut.DOWN
2211 then
2212 let position, sh = scrollph state.y in
2213 if y > truncate position && y < truncate (position +. sh)
2214 then
2215 state.mstate <- Mscroll
2216 else
2217 let percent = float y /. float conf.winh in
2218 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2219 gotoy desty;
2220 state.mstate <- Mscroll
2221 else
2222 state.mstate <- Mnone
2224 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2225 begin match state.birdseye with
2226 | Some (conf, leftx, pageno, hooverpageno) ->
2227 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2228 let rec loop = function
2229 | [] -> ()
2230 | l :: rest ->
2231 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2232 && x > margin && x < margin + l.pagew
2233 then (
2234 birdseyeoff (conf, leftx, l.pageno, hooverpageno);
2235 reshape conf.winw conf.winh;
2237 else loop rest
2239 loop state.layout;
2240 | None -> () (* impossible *)
2243 | Glut.LEFT_BUTTON when state.outline = None ->
2244 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2245 begin match dest with
2246 | Ulinkgoto (pageno, top) ->
2247 if pageno >= 0
2248 then
2249 gotopage1 pageno top
2251 | Ulinkuri s ->
2252 print_endline s
2254 | Unone when bstate = Glut.DOWN ->
2255 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2256 state.mstate <- Mpan (x, y);
2258 | Unone | Utext _ ->
2259 if bstate = Glut.DOWN
2260 then (
2261 if conf.angle mod 360 = 0
2262 then (
2263 state.mstate <- Msel ((x, y), (x, y));
2264 Glut.postRedisplay ()
2267 else (
2268 match state.mstate with
2269 | Mnone -> ()
2271 | Mscroll ->
2272 state.mstate <- Mnone
2274 | Mpan _ ->
2275 Glut.setCursor Glut.CURSOR_INHERIT;
2276 state.mstate <- Mnone
2278 | Msel ((x0, y0), (x1, y1)) ->
2279 let f l =
2280 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2281 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2282 then
2283 match getopaque l.pageno with
2284 | Some (opaque, _) when validopaque opaque ->
2285 copysel opaque
2286 | _ -> ()
2288 List.iter f state.layout;
2289 copysel ""; (* ugly *)
2290 Glut.setCursor Glut.CURSOR_INHERIT;
2291 state.mstate <- Mnone;
2295 | _ ->
2298 let mouse ~button ~state ~x ~y = mouse button state x y;;
2300 let motion ~x ~y =
2301 if state.outline = None
2302 then
2303 match state.mstate with
2304 | Mnone -> ()
2306 | Mpan (x0, y0) ->
2307 let dx = x - x0
2308 and dy = y0 - y in
2309 state.mstate <- Mpan (x, y);
2310 if conf.zoom > 1.0 then state.x <- state.x + dx;
2311 let y = clamp dy in
2312 gotoy_and_clear_text y
2314 | Msel (a, _) ->
2315 state.mstate <- Msel (a, (x, y));
2316 Glut.postRedisplay ()
2318 | Mscroll ->
2319 let y = min conf.winh (max 0 y) in
2320 let percent = float y /. float conf.winh in
2321 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2322 gotoy_and_clear_text y
2325 let pmotion ~x ~y =
2326 match state.birdseye with
2327 | Some (conf, leftx, pageno, hooverpageno) ->
2328 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2329 let rec loop = function
2330 | [] ->
2331 if hooverpageno != -1
2332 then (
2333 state.birdseye <- Some (conf, leftx, pageno, -1);
2334 Glut.postRedisplay ();
2336 | l :: rest ->
2337 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2338 && x > margin && x < margin + l.pagew
2339 then (
2340 state.birdseye <- Some (conf, leftx, pageno, l.pageno);
2341 Glut.postRedisplay ();
2343 else loop rest
2345 loop state.layout
2347 | None ->
2348 if state.outline = None
2349 then
2350 match state.mstate with
2351 | Mnone ->
2352 begin match getunder x y with
2353 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2354 | Ulinkuri uri ->
2355 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2356 Glut.setCursor Glut.CURSOR_INFO
2357 | Ulinkgoto (page, y) ->
2358 if conf.underinfo
2359 then showtext 'p' ("age: " ^ string_of_int page);
2360 Glut.setCursor Glut.CURSOR_INFO
2361 | Utext s ->
2362 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2363 Glut.setCursor Glut.CURSOR_TEXT
2366 | Mpan _ | Msel _ | Mscroll ->
2371 module State =
2372 struct
2373 open Parser
2375 let home =
2377 match Sys.os_type with
2378 | "Win32" -> Sys.getenv "HOMEPATH"
2379 | _ -> Sys.getenv "HOME"
2380 with exn ->
2381 prerr_endline
2382 ("Can not determine home directory location: " ^
2383 Printexc.to_string exn);
2387 let config_of c attrs =
2388 let apply c k v =
2390 match k with
2391 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2392 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2393 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2394 | "preload" -> { c with preload = bool_of_string v }
2395 | "page-bias" -> { c with pagebias = int_of_string v }
2396 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2397 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2398 | "crop-hack" -> { c with crophack = bool_of_string v }
2399 | "throttle" -> { c with showall = bool_of_string v }
2400 | "highlight-links" -> { c with hlinks = bool_of_string v }
2401 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2402 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2403 | "zoom" ->
2404 let zoom = float_of_string v /. 100. in
2405 let zoom = max 0.01 (min 2.2 zoom) in
2406 { c with zoom = zoom }
2407 | "presentation" -> { c with presentation = bool_of_string v }
2408 | "rotation-angle" -> { c with angle = int_of_string v }
2409 | "width" -> { c with winw = max 20 (int_of_string v) }
2410 | "height" -> { c with winh = max 20 (int_of_string v) }
2411 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2412 | "proportional-display" -> { c with proportional = bool_of_string v }
2413 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2414 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2415 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2416 | _ -> c
2417 with exn ->
2418 prerr_endline ("Error processing attribute (`" ^
2419 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2422 let rec fold c = function
2423 | [] -> c
2424 | (k, v) :: rest ->
2425 let c = apply c k v in
2426 fold c rest
2428 fold c attrs;
2431 let bookmark_of attrs =
2432 let rec fold title page rely = function
2433 | ("title", v) :: rest -> fold v page rely rest
2434 | ("page", v) :: rest -> fold title v rely rest
2435 | ("rely", v) :: rest -> fold title page v rest
2436 | _ :: rest -> fold title page rely rest
2437 | [] -> title, page, rely
2439 fold "invalid" "0" "0" attrs
2442 let setconf dst src =
2443 dst.scrollw <- src.scrollw;
2444 dst.scrollh <- src.scrollh;
2445 dst.icase <- src.icase;
2446 dst.preload <- src.preload;
2447 dst.pagebias <- src.pagebias;
2448 dst.verbose <- src.verbose;
2449 dst.scrollincr <- src.scrollincr;
2450 dst.maxhfit <- src.maxhfit;
2451 dst.crophack <- src.crophack;
2452 dst.autoscroll <- src.autoscroll;
2453 dst.showall <- src.showall;
2454 dst.hlinks <- src.hlinks;
2455 dst.underinfo <- src.underinfo;
2456 dst.interpagespace <- src.interpagespace;
2457 dst.zoom <- src.zoom;
2458 dst.presentation <- src.presentation;
2459 dst.angle <- src.angle;
2460 dst.winw <- src.winw;
2461 dst.winh <- src.winh;
2462 dst.savebmarks <- src.savebmarks;
2463 dst.memlimit <- src.memlimit;
2464 dst.proportional <- src.proportional;
2465 dst.texcount <- src.texcount;
2466 dst.sliceheight <- src.sliceheight;
2469 let unent s =
2470 let l = String.length s in
2471 let b = Buffer.create l in
2472 unent b s 0 l;
2473 Buffer.contents b;
2476 let get s =
2477 let h = Hashtbl.create 10 in
2478 let dc = { defconf with angle = defconf.angle } in
2479 let rec toplevel v t spos epos =
2480 match t with
2481 | Vdata | Vcdata | Vend -> v
2482 | Vopen ("llppconfig", attrs, closed) ->
2483 if closed
2484 then v
2485 else { v with f = llppconfig }
2486 | Vopen _ ->
2487 error "unexpected subelement at top level" s spos
2488 | Vclose tag -> error "unexpected close at top level" s spos
2490 and llppconfig v t spos epos =
2491 match t with
2492 | Vdata | Vcdata | Vend -> v
2493 | Vopen ("defaults", attrs, closed) ->
2494 let c = config_of dc attrs in
2495 setconf dc c;
2496 if closed
2497 then v
2498 else { v with f = skip "defaults" (fun () -> v) }
2500 | Vopen ("doc", attrs, closed) ->
2501 let pathent =
2503 List.assoc "path" attrs
2504 with Not_found -> error "doc is missing path attribute" s spos
2506 let path = unent pathent in
2507 let c = config_of dc attrs in
2508 let y =
2510 float_of_string (List.assoc "rely" attrs)
2511 with
2512 | Not_found -> 0.0
2513 | exn ->
2514 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2517 let x =
2519 int_of_string (List.assoc "pan" attrs)
2520 with
2521 | Not_found -> 0
2522 | exn ->
2523 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2526 if closed
2527 then (Hashtbl.add h path (c, [], x, y); v)
2528 else { v with f = doc path x y c [] }
2530 | Vopen (tag, _, closed) ->
2531 error "unexpected subelement in llppconfig" s spos
2533 | Vclose "llppconfig" -> { v with f = toplevel }
2534 | Vclose tag -> error "unexpected close in llppconfig" s spos
2536 and doc path x y c bookmarks v t spos epos =
2537 match t with
2538 | Vdata | Vcdata -> v
2539 | Vend -> error "unexpected end of input in doc" s spos
2540 | Vopen ("bookmarks", attrs, closed) ->
2541 { v with f = pbookmarks path x y c bookmarks }
2543 | Vopen (tag, _, _) ->
2544 error "unexpected subelement in doc" s spos
2546 | Vclose "doc" ->
2547 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2548 { v with f = llppconfig }
2550 | Vclose tag -> error "unexpected close in doc" s spos
2552 and pbookmarks path x y c bookmarks v t spos epos =
2553 match t with
2554 | Vdata | Vcdata -> v
2555 | Vend -> error "unexpected end of input in bookmarks" s spos
2556 | Vopen ("item", attrs, closed) ->
2557 let titleent, spage, srely = bookmark_of attrs in
2558 let page =
2560 int_of_string spage
2561 with exn ->
2562 dolog "Failed to convert page %S to integer: %s"
2563 spage (Printexc.to_string exn);
2566 let rely =
2568 float_of_string srely
2569 with exn ->
2570 dolog "Failed to convert rely %S to real: %s"
2571 srely (Printexc.to_string exn);
2574 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2575 if closed
2576 then { v with f = pbookmarks path x y c bookmarks }
2577 else
2578 let f () = v in
2579 { v with f = skip "item" f }
2581 | Vopen _ ->
2582 error "unexpected subelement in bookmarks" s spos
2584 | Vclose "bookmarks" ->
2585 { v with f = doc path x y c bookmarks }
2587 | Vclose tag -> error "unexpected close in bookmarks" s spos
2589 and skip tag f v t spos epos =
2590 match t with
2591 | Vdata | Vcdata -> v
2592 | Vend ->
2593 error ("unexpected end of input in skipped " ^ tag) s spos
2594 | Vopen (tag', _, closed) ->
2595 if closed
2596 then v
2597 else
2598 let f' () = { v with f = skip tag f } in
2599 { v with f = skip tag' f' }
2600 | Vclose ctag ->
2601 if tag = ctag
2602 then f ()
2603 else error ("unexpected close in skipped " ^ tag) s spos
2606 parse { f = toplevel; accu = () } s;
2607 h, dc;
2610 let do_load f ic =
2612 let len = in_channel_length ic in
2613 let s = String.create len in
2614 really_input ic s 0 len;
2615 f s;
2616 with
2617 | Parse_error (msg, s, pos) ->
2618 let subs = subs s pos in
2619 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2620 failwith ("parse error: " ^ s)
2622 | exn ->
2623 failwith ("config load error: " ^ Printexc.to_string exn)
2626 let path =
2627 let dir =
2629 let dir = Filename.concat home ".config" in
2630 if Sys.is_directory dir then dir else home
2631 with _ -> home
2633 Filename.concat dir "llpp.conf"
2636 let load1 f =
2637 if Sys.file_exists path
2638 then
2639 match
2640 (try Some (open_in_bin path)
2641 with exn ->
2642 prerr_endline
2643 ("Error opening configuation file `" ^ path ^ "': " ^
2644 Printexc.to_string exn);
2645 None
2647 with
2648 | Some ic ->
2649 begin try
2650 f (do_load get ic)
2651 with exn ->
2652 prerr_endline
2653 ("Error loading configuation from `" ^ path ^ "': " ^
2654 Printexc.to_string exn);
2655 end;
2656 close_in ic;
2658 | None -> ()
2659 else
2660 f (Hashtbl.create 0, defconf)
2663 let load () =
2664 let f (h, dc) =
2665 let pc, pb, px, py =
2667 Hashtbl.find h state.path
2668 with Not_found -> dc, [], 0, 0.0
2670 setconf defconf dc;
2671 setconf conf pc;
2672 state.bookmarks <- pb;
2673 state.x <- px;
2674 cbput state.hists.nav py;
2676 load1 f
2679 let add_attrs bb always dc c =
2680 let ob s a b =
2681 if always || a != b
2682 then Printf.bprintf bb "\n %s='%b'" s a
2683 and oi s a b =
2684 if always || a != b
2685 then Printf.bprintf bb "\n %s='%d'" s a
2686 and oz s a b =
2687 if always || a <> b
2688 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2690 let w, h =
2691 if always
2692 then dc.winw, dc.winh
2693 else
2694 match state.fullscreen with
2695 | Some wh -> wh
2696 | None -> c.winw, c.winh
2698 let zoom, presentation, interpagespace, showall=
2699 if always
2700 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2701 else
2702 match state.birdseye with
2703 | Some (bc, _, _, _) ->
2704 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2705 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2707 oi "width" w dc.winw;
2708 oi "height" h dc.winh;
2709 oi "scroll-bar-width" c.scrollw dc.scrollw;
2710 oi "scroll-handle-height" c.scrollh dc.scrollh;
2711 ob "case-insensitive-search" c.icase dc.icase;
2712 ob "preload" c.preload dc.preload;
2713 oi "page-bias" c.pagebias dc.pagebias;
2714 oi "scroll-step" c.scrollincr dc.scrollincr;
2715 ob "max-height-fit" c.maxhfit dc.maxhfit;
2716 ob "crop-hack" c.crophack dc.crophack;
2717 ob "throttle" showall dc.showall;
2718 ob "highlight-links" c.hlinks dc.hlinks;
2719 ob "under-cursor-info" c.underinfo dc.underinfo;
2720 oi "vertical-margin" interpagespace dc.interpagespace;
2721 oz "zoom" zoom dc.zoom;
2722 ob "presentation" presentation dc.presentation;
2723 oi "rotation-angle" c.angle dc.angle;
2724 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2725 ob "proportional-display" c.proportional dc.proportional;
2726 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2727 oi "texcount" c.texcount dc.texcount;
2728 oi "slice-height" c.sliceheight dc.sliceheight;
2731 let save () =
2732 let bb = Buffer.create 32768 in
2733 let f (h, dc) =
2734 Buffer.add_string bb "<llppconfig>\n<defaults ";
2735 add_attrs bb true dc dc;
2736 Buffer.add_string bb "/>\n";
2738 let adddoc path x y c bookmarks =
2739 if bookmarks == [] && c = dc && y = 0.0
2740 then ()
2741 else (
2742 Printf.bprintf bb "<doc path='%s'"
2743 (enent path 0 (String.length path));
2745 if y <> 0.0
2746 then Printf.bprintf bb " rely='%f'" y;
2748 if x != 0
2749 then Printf.bprintf bb " pan='%d'" x;
2751 add_attrs bb false dc c;
2753 begin match bookmarks with
2754 | [] -> Buffer.add_string bb "/>\n"
2755 | _ ->
2756 Buffer.add_string bb ">\n<bookmarks>\n";
2757 List.iter (fun (title, _level, page, rely) ->
2758 Printf.bprintf bb
2759 "<item title='%s' page='%d' rely='%f'/>\n"
2760 (enent title 0 (String.length title))
2761 page
2762 rely
2763 ) bookmarks;
2764 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2765 end;
2769 let x =
2770 match state.birdseye with
2771 | Some (_, x, _, _) -> x
2772 | None -> state.x
2774 adddoc state.path x (yratio state.y) conf
2775 (if conf.savebmarks then state.bookmarks else []);
2777 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2778 if path <> state.path
2779 then
2780 adddoc path x y c bookmarks
2781 ) h;
2782 Buffer.add_string bb "</llppconfig>";
2784 load1 f;
2785 if Buffer.length bb > 0
2786 then
2788 let tmp = path ^ ".tmp" in
2789 let oc = open_out_bin tmp in
2790 Buffer.output_buffer oc bb;
2791 close_out oc;
2792 Sys.rename tmp path;
2793 with exn ->
2794 prerr_endline
2795 ("error while saving configuration: " ^ Printexc.to_string exn)
2797 end;;
2799 let () =
2800 Arg.parse
2801 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2802 (fun s -> state.path <- s)
2803 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2805 let path =
2806 if String.length state.path = 0
2807 then (prerr_endline "filename missing"; exit 1)
2808 else (
2809 if Filename.is_relative state.path
2810 then Filename.concat (Sys.getcwd ()) state.path
2811 else state.path
2814 state.path <- path;
2816 State.load ();
2818 let _ = Glut.init Sys.argv in
2819 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2820 let () = Glut.initWindowSize conf.winw conf.winh in
2821 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2823 let csock, ssock =
2824 if Sys.os_type = "Unix"
2825 then
2826 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2827 else
2828 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2829 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2830 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2831 Unix.bind sock addr;
2832 Unix.listen sock 1;
2833 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2834 Unix.connect csock addr;
2835 let ssock, _ = Unix.accept sock in
2836 Unix.close sock;
2837 let opts sock =
2838 Unix.setsockopt sock Unix.TCP_NODELAY true;
2839 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2841 opts ssock;
2842 opts csock;
2843 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2844 ssock, csock
2847 let () = Glut.displayFunc display in
2848 let () = Glut.reshapeFunc reshape in
2849 let () = Glut.keyboardFunc keyboard in
2850 let () = Glut.specialFunc special in
2851 let () = Glut.idleFunc (Some idle) in
2852 let () = Glut.mouseFunc mouse in
2853 let () = Glut.motionFunc motion in
2854 let () = Glut.passiveMotionFunc pmotion in
2856 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2857 state.csock <- csock;
2858 state.ssock <- ssock;
2859 state.text <- "Opening " ^ path;
2860 writeopen state.path state.password;
2862 at_exit State.save;
2864 let rec handlelablglutbug () =
2866 Glut.mainLoop ();
2867 with Glut.BadEnum "key in special_of_int" ->
2868 showtext '!' " LablGlut bug: special key not recognized";
2869 handlelablglutbug ()
2871 handlelablglutbug ();