Reset cursor and mstate when entering bird's eye
[llpp.git] / main.ml
blob03ee514b7a013c934cac582022b13ea497e8d1cb
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 presentation = bool
22 and interpagespace = int
23 and texcount = int
24 and sliceheight = int
25 and zoom = 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 : zoom
147 ; mutable presentation : presentation
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
159 type outline = string * int * int * float;;
160 type outlines =
161 | Oarray of outline array
162 | Olist of outline list
163 | Onarrow of string * outline array * outline array
166 type rect = (float * float * float * float * float * float * float * float);;
168 type state =
169 { mutable csock : Unix.file_descr
170 ; mutable ssock : Unix.file_descr
171 ; mutable w : int
172 ; mutable x : int
173 ; mutable y : int
174 ; mutable ty : float
175 ; mutable maxy : int
176 ; mutable layout : layout list
177 ; pagemap :
178 ((pageno * width * angle * proportional), (opaque * pixmapsize)) Hashtbl.t
179 ; mutable pdims : (pageno * width * height * leftx) list
180 ; mutable pagecount : int
181 ; pagecache : string circbuf
182 ; mutable rendering : bool
183 ; mutable mstate : mstate
184 ; mutable searchpattern : string
185 ; mutable rects : (pageno * recttype * rect) list
186 ; mutable rects1 : (pageno * recttype * rect) list
187 ; mutable text : string
188 ; mutable fullscreen : (width * height) option
189 ; mutable birdseye : (zoom * leftx * presentation * interpagespace) option
190 ; mutable textentry : textentry option
191 ; mutable outlines : outlines
192 ; mutable outline : (bool * int * int * outline array * string) option
193 ; mutable bookmarks : outline list
194 ; mutable path : string
195 ; mutable password : string
196 ; mutable invalidated : int
197 ; mutable colorscale : float
198 ; mutable memused : int
199 ; mutable birdseyepageno : pageno
200 ; 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 ; ty = 0.0
246 ; layout = []
247 ; maxy = max_int
248 ; pagemap = Hashtbl.create 10
249 ; pagecache = cbnew 100 ""
250 ; pdims = []
251 ; pagecount = 0
252 ; rendering = false
253 ; mstate = Mnone
254 ; rects = []
255 ; rects1 = []
256 ; text = ""
257 ; fullscreen = None
258 ; birdseye = None
259 ; textentry = None
260 ; searchpattern = ""
261 ; outlines = Olist []
262 ; outline = None
263 ; bookmarks = []
264 ; path = ""
265 ; password = ""
266 ; invalidated = 0
267 ; hists =
268 { nav = cbnew 100 0.0
269 ; pat = cbnew 20 ""
270 ; pag = cbnew 10 ""
272 ; colorscale = 1.0
273 ; memused = 0
274 ; birdseyepageno = 0
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))
502 with Not_found -> None
505 let cache pageno opaque =
506 Hashtbl.replace state.pagemap
507 (pageno, state.w, conf.angle, conf.proportional) 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 n = List.exists (fun l -> l.pageno = n) state.layout;;
545 let preload () =
546 if conf.preload
547 then
548 let oktopreload =
549 let opaque = cbpeek state.pagecache in
550 match findpageforopaque opaque with
551 | Some ((n, _, _, _), size) ->
552 not (pagevisible n) && state.memused - size <= conf.memlimit
553 | None -> false
555 if oktopreload
556 then
557 let rely = yratio state.y in
558 let presentation = conf.presentation in
559 let interpagespace = conf.interpagespace in
560 let maxy = state.maxy in
561 conf.presentation <- false;
562 conf.interpagespace <- 0;
563 state.maxy <- calcheight ();
564 let y = truncate (float state.maxy *. rely) in
565 let y = if y < conf.winh then 0 else y - conf.winh in
566 let pages = layout y (conf.winh*3) in
567 List.iter render pages;
568 conf.presentation <- presentation;
569 conf.interpagespace <- interpagespace;
570 state.maxy <- maxy;
573 let gotoy y =
574 let y = max 0 y in
575 let y = min state.maxy y in
576 let pages = layout y conf.winh in
577 let ready = loadlayout pages in
578 state.ty <- yratio y;
579 if conf.showall
580 then (
581 if ready
582 then (
583 state.layout <- pages;
584 state.y <- y;
585 Glut.postRedisplay ();
588 else (
589 state.layout <- pages;
590 state.y <- y;
591 Glut.postRedisplay ();
593 if state.birdseye <> None
594 then (
595 if not (pagevisible state.birdseyepageno)
596 then
597 match state.layout with
598 | [] -> ()
599 | l :: _ -> state.birdseyepageno <- l.pageno
601 preload ();
604 let gotoy_and_clear_text y =
605 gotoy y;
606 if not conf.verbose then state.text <- "";
609 let addnav () =
610 cbput state.hists.nav (yratio state.y);
613 let getnav () =
614 let y = cbgetc state.hists.nav ~-1 in
615 truncate (y *. float state.maxy)
618 let gotopage n top =
619 let y, h = getpageyh n in
620 addnav ();
621 gotoy_and_clear_text (y + (truncate (top *. float h)));
624 let gotopage1 n top =
625 let y = getpagey n in
626 addnav ();
627 gotoy_and_clear_text (y + top);
630 let invalidate () =
631 state.layout <- [];
632 state.pdims <- [];
633 state.rects <- [];
634 state.rects1 <- [];
635 state.invalidated <- state.invalidated + 1;
638 let scalecolor c =
639 let c = c *. state.colorscale in
640 (c, c, c);
643 let represent () =
644 let y =
645 match state.layout with
646 | [] ->
647 let rely = yratio state.y in
648 state.maxy <- calcheight ();
649 truncate (float state.maxy *. rely)
651 | l :: _ ->
652 state.maxy <- calcheight ();
653 getpagey l.pageno
655 gotoy y
658 let pagematrix () =
659 GlMat.mode `projection;
660 GlMat.load_identity ();
661 GlMat.rotate ~x:1.0 ~angle:180.0 ();
662 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
663 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
666 let winmatrix () =
667 GlMat.mode `projection;
668 GlMat.load_identity ();
669 GlMat.rotate ~x:1.0 ~angle:180.0 ();
670 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
671 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
674 let reshape ~w ~h =
675 conf.winw <- w;
676 let w = truncate (float w *. conf.zoom) - conf.scrollw in
677 let w = max w 2 in
678 state.w <- w;
679 conf.winh <- h;
680 GlMat.mode `modelview;
681 GlMat.load_identity ();
682 GlClear.color (scalecolor 1.0);
683 GlClear.clear [`color];
685 invalidate ();
686 wcmd "geometry" [`i w; `i h];
689 let showtext c s =
690 GlDraw.color (0.0, 0.0, 0.0);
691 GlDraw.rect
692 (0.0, float (conf.winh - 18))
693 (float (conf.winw - conf.scrollw - 1), float conf.winh)
695 let font = Glut.BITMAP_8_BY_13 in
696 GlDraw.color (1.0, 1.0, 1.0);
697 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
698 Glut.bitmapCharacter ~font ~c:(Char.code c);
699 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
702 let enttext () =
703 let len = String.length state.text in
704 match state.textentry with
705 | None ->
706 if len > 0 then showtext ' ' state.text
708 | Some (c, text, _, _, _) ->
709 let s =
710 if len > 0
711 then
712 text ^ " [" ^ state.text ^ "]"
713 else
714 text
716 showtext c s;
719 let showtext c s =
720 if true
721 then (
722 state.text <- Printf.sprintf "%c%s" c s;
723 Glut.postRedisplay ();
725 else (
726 showtext c s;
727 Glut.swapBuffers ();
731 let act cmd =
732 match cmd.[0] with
733 | 'c' ->
734 state.pdims <- [];
736 | 'D' ->
737 state.rects <- state.rects1;
738 Glut.postRedisplay ()
740 | 'C' ->
741 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
742 state.pagecount <- n;
743 state.invalidated <- state.invalidated - 1;
744 if state.invalidated = 0
745 then represent ()
747 | 't' ->
748 let s = Scanf.sscanf cmd "t %n"
749 (fun n -> String.sub cmd n (String.length cmd - n))
751 Glut.setWindowTitle s
753 | 'T' ->
754 let s = Scanf.sscanf cmd "T %n"
755 (fun n -> String.sub cmd n (String.length cmd - n))
757 if state.textentry = None
758 then (
759 state.text <- s;
760 showtext ' ' s;
762 else (
763 state.text <- s;
764 Glut.postRedisplay ();
767 | 'V' ->
768 if conf.verbose
769 then
770 let s = Scanf.sscanf cmd "V %n"
771 (fun n -> String.sub cmd n (String.length cmd - n))
773 state.text <- s;
774 showtext ' ' s;
776 | 'F' ->
777 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
778 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
779 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
780 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
782 let y = (getpagey pageno) + truncate y0 in
783 addnav ();
784 gotoy y;
785 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
787 | 'R' ->
788 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
789 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
790 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
791 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
793 state.rects1 <-
794 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
796 | 'r' ->
797 let n, w, h, r, l, s, p =
798 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
799 (fun n w h r l s p ->
800 (n-1, w, h, r, l != 0, s, p))
803 Hashtbl.replace state.pagemap (n, w, r, l) (p, s);
804 state.memused <- state.memused + s;
806 let rec gc () =
807 if (state.memused <= conf.memlimit) || cbempty state.pagecache
808 then ()
809 else (
810 let evictedopaque = cbpeek state.pagecache in
811 match findpageforopaque evictedopaque with
812 | None -> failwith "bug in gc"
813 | Some ((evictedn, _, _, _) as k, evictedsize) ->
814 if not (pagevisible evictedn)
815 then (
816 wcmd "free" [`s evictedopaque];
817 state.memused <- state.memused - evictedsize;
818 Hashtbl.remove state.pagemap k;
819 cbdecr state.pagecache;
820 gc ();
824 gc ();
826 cbput state.pagecache p;
827 state.rendering <- false;
829 if conf.showall
830 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
831 else (
832 if pagevisible n
833 then gotoy state.y
834 else (ignore (loadlayout state.layout); preload ())
837 | 'l' ->
838 let (n, w, h, x) as pdim =
839 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
841 state.pdims <- pdim :: state.pdims
843 | 'o' ->
844 let (l, n, t, h, pos) =
845 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
847 let s = String.sub cmd pos (String.length cmd - pos) in
848 let s =
849 let l = String.length s in
850 let b = Buffer.create (String.length s) in
851 let rec loop pc2 i =
852 if i = l
853 then ()
854 else
855 let pc2 =
856 match s.[i] with
857 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
858 | '\xc2' -> true
859 | c ->
860 let c = if Char.code c land 0x80 = 0 then c else '?' in
861 Buffer.add_char b c;
862 false
864 loop pc2 (i+1)
866 loop false 0;
867 Buffer.contents b
869 let outline = (s, l, n, float t /. float h) in
870 let outlines =
871 match state.outlines with
872 | Olist outlines -> Olist (outline :: outlines)
873 | Oarray _ -> Olist [outline]
874 | Onarrow _ -> Olist [outline]
876 state.outlines <- outlines
878 | _ ->
879 log "unknown cmd `%S'" cmd
882 let now = Unix.gettimeofday;;
884 let idle () =
885 let rec loop delay =
886 let r, _, _ = Unix.select [state.csock] [] [] delay in
887 begin match r with
888 | [] ->
889 if conf.autoscroll
890 then begin
891 let y = state.y + conf.scrollincr in
892 let y = if y >= state.maxy then 0 else y in
893 gotoy y;
894 state.text <- "";
895 end;
897 | _ ->
898 let cmd = readcmd state.csock in
899 act cmd;
900 loop 0.0
901 end;
902 in loop 0.001
905 let onhist cb =
906 let rc = cb.rc in
907 let action = function
908 | HCprev -> cbget cb ~-1
909 | HCnext -> cbget cb 1
910 | HCfirst -> cbget cb ~-(cb.rc)
911 | HClast -> cbget cb (cb.len - 1 - cb.rc)
912 and cancel () = cb.rc <- rc
913 in (action, cancel)
916 let search pattern forward =
917 if String.length pattern > 0
918 then
919 let pn, py =
920 match state.layout with
921 | [] -> 0, 0
922 | l :: _ ->
923 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
925 let cmd =
926 let b = makecmd "search"
927 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
929 Buffer.add_char b ',';
930 Buffer.add_string b pattern;
931 Buffer.add_char b '\000';
932 Buffer.contents b;
934 writecmd state.csock cmd;
937 let intentry text key =
938 let c = Char.unsafe_chr key in
939 match c with
940 | '0' .. '9' ->
941 let s = "x" in s.[0] <- c;
942 let text = text ^ s in
943 TEcont text
945 | _ ->
946 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
947 TEcont text
950 let addchar s c =
951 let b = Buffer.create (String.length s + 1) in
952 Buffer.add_string b s;
953 Buffer.add_char b c;
954 Buffer.contents b;
957 let textentry text key =
958 let c = Char.unsafe_chr key in
959 match c with
960 | _ when key >= 32 && key < 127 ->
961 let text = addchar text c in
962 TEcont text
964 | _ ->
965 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
966 TEcont text
969 let reinit angle proportional =
970 conf.angle <- angle;
971 conf.proportional <- proportional;
972 invalidate ();
973 wcmd "reinit" [`i angle; `b proportional];
976 let optentry text key =
977 let btos b = if b then "on" else "off" in
978 let c = Char.unsafe_chr key in
979 match c with
980 | 's' ->
981 let ondone s =
982 try conf.scrollincr <- int_of_string s with exc ->
983 state.text <- Printf.sprintf "bad integer `%s': %s"
984 s (Printexc.to_string exc)
986 TEswitch ('#', "", None, intentry, ondone)
988 | 'R' ->
989 let ondone s =
990 match try
991 Some (int_of_string s)
992 with exc ->
993 state.text <- Printf.sprintf "bad integer `%s': %s"
994 s (Printexc.to_string exc);
995 None
996 with
997 | Some angle -> reinit angle conf.proportional
998 | None -> ()
1000 TEswitch ('^', "", None, intentry, ondone)
1002 | 'i' ->
1003 conf.icase <- not conf.icase;
1004 TEdone ("case insensitive search " ^ (btos conf.icase))
1006 | 'p' ->
1007 conf.preload <- not conf.preload;
1008 gotoy state.y;
1009 TEdone ("preload " ^ (btos conf.preload))
1011 | 'v' ->
1012 conf.verbose <- not conf.verbose;
1013 TEdone ("verbose " ^ (btos conf.verbose))
1015 | 'h' ->
1016 conf.maxhfit <- not conf.maxhfit;
1017 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1018 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1020 | 'c' ->
1021 conf.crophack <- not conf.crophack;
1022 TEdone ("crophack " ^ btos conf.crophack)
1024 | 'a' ->
1025 conf.showall <- not conf.showall;
1026 TEdone ("showall " ^ btos conf.showall)
1028 | 'f' ->
1029 conf.underinfo <- not conf.underinfo;
1030 TEdone ("underinfo " ^ btos conf.underinfo)
1032 | 'P' ->
1033 conf.savebmarks <- not conf.savebmarks;
1034 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1036 | 'S' ->
1037 let ondone s =
1039 conf.interpagespace <- int_of_string s;
1040 let rely = yratio state.y in
1041 state.maxy <- calcheight ();
1042 gotoy (truncate (float state.maxy *. rely));
1043 with exc ->
1044 state.text <- Printf.sprintf "bad integer `%s': %s"
1045 s (Printexc.to_string exc)
1047 TEswitch ('%', "", None, intentry, ondone)
1049 | 'l' ->
1050 reinit conf.angle (not conf.proportional);
1051 TEdone ("proprortional display " ^ btos conf.proportional)
1053 | _ ->
1054 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1055 TEstop
1058 let maxoutlinerows () = (conf.winh - 31) / 16;;
1060 let enterselector allowdel outlines errmsg msg =
1061 if Array.length outlines = 0
1062 then (
1063 showtext ' ' errmsg;
1065 else (
1066 state.text <- msg;
1067 Glut.setCursor Glut.CURSOR_INHERIT;
1068 let pageno =
1069 match state.layout with
1070 | [] -> -1
1071 | {pageno=pageno} :: rest -> pageno
1073 let active =
1074 let rec loop n =
1075 if n = Array.length outlines
1076 then 0
1077 else
1078 let (_, _, outlinepageno, _) = outlines.(n) in
1079 if outlinepageno >= pageno then n else loop (n+1)
1081 loop 0
1083 state.outline <-
1084 Some (allowdel, active,
1085 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1086 Glut.postRedisplay ();
1090 let enteroutlinemode () =
1091 let outlines, msg =
1092 match state.outlines with
1093 | Oarray a -> a, ""
1094 | Olist l ->
1095 let a = Array.of_list (List.rev l) in
1096 state.outlines <- Oarray a;
1097 a, ""
1098 | Onarrow (pat, a, b) ->
1099 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1101 enterselector false outlines "Document has no outline" msg;
1104 let enterbookmarkmode () =
1105 let bookmarks = Array.of_list state.bookmarks in
1106 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1109 let quickbookmark ?title () =
1110 match state.layout with
1111 | [] -> ()
1112 | l :: _ ->
1113 let title =
1114 match title with
1115 | None ->
1116 let sec = Unix.gettimeofday () in
1117 let tm = Unix.localtime sec in
1118 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1119 (l.pageno+1)
1120 tm.Unix.tm_mday
1121 tm.Unix.tm_mon
1122 (tm.Unix.tm_year + 1900)
1123 tm.Unix.tm_hour
1124 tm.Unix.tm_min
1125 | Some title -> title
1127 state.bookmarks <-
1128 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1131 let doreshape w h =
1132 state.fullscreen <- None;
1133 Glut.reshapeWindow w h;
1136 let writeopen path password =
1137 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1140 let opendoc path password =
1141 invalidate ();
1142 state.path <- path;
1143 state.password <- password;
1144 Hashtbl.clear state.pagemap;
1146 writeopen path password;
1147 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1148 wcmd "geometry" [`i state.w; `i conf.winh];
1151 let birdseyeoff (zoom, x, presentation, interpagespace) =
1152 state.birdseye <- None;
1153 conf.zoom <- zoom;
1154 conf.presentation <- presentation;
1155 conf.interpagespace <- interpagespace;
1156 state.x <- x;
1157 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1158 (100.0*.zoom);
1161 let viewkeyboard ~key ~x ~y =
1162 let enttext te =
1163 state.textentry <- te;
1164 state.text <- "";
1165 enttext ();
1166 Glut.postRedisplay ()
1168 match state.textentry with
1169 | None ->
1170 let c = Char.chr key in
1171 begin match c with
1172 | '\027' | 'q' ->
1173 exit 0
1175 | '\008' ->
1176 let y = getnav () in
1177 gotoy_and_clear_text y
1179 | '\013' ->
1180 begin match state.birdseye with
1181 | None -> ()
1182 | Some vals ->
1183 let y = getpagey state.birdseyepageno in
1184 state.y <- y;
1185 birdseyeoff vals;
1186 reshape conf.winw conf.winh;
1187 end;
1189 | 'o' ->
1190 enteroutlinemode ()
1192 | 'u' ->
1193 state.rects <- [];
1194 state.text <- "";
1195 Glut.postRedisplay ()
1197 | '/' | '?' ->
1198 let ondone isforw s =
1199 cbput state.hists.pat s;
1200 state.searchpattern <- s;
1201 search s isforw
1203 enttext (Some (c, "", Some (onhist state.hists.pat),
1204 textentry, ondone (c ='/')))
1206 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1207 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1208 conf.zoom <- min 2.2 (conf.zoom +. incr);
1209 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1210 reshape conf.winw conf.winh
1212 | '+' ->
1213 let ondone s =
1214 let n =
1215 try int_of_string s with exc ->
1216 state.text <- Printf.sprintf "bad integer `%s': %s"
1217 s (Printexc.to_string exc);
1218 max_int
1220 if n != max_int
1221 then (
1222 conf.pagebias <- n;
1223 state.text <- "page bias is now " ^ string_of_int n;
1226 enttext (Some ('+', "", None, intentry, ondone))
1228 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1229 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1230 conf.zoom <- max 0.01 (conf.zoom -. decr);
1231 if conf.zoom <= 1.0 then state.x <- 0;
1232 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1233 reshape conf.winw conf.winh;
1235 | '-' ->
1236 let ondone msg =
1237 state.text <- msg;
1239 enttext (Some ('-', "", None, optentry, ondone))
1241 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1242 state.x <- 0;
1243 conf.zoom <- 1.0;
1244 state.text <- "zoom is 100%";
1245 reshape conf.winw conf.winh
1247 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1248 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1249 if zoom < 1.0
1250 then (
1251 conf.zoom <- zoom;
1252 state.x <- 0;
1253 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1254 reshape conf.winw conf.winh;
1257 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1258 begin match state.birdseye with
1259 | None ->
1260 let zoom = 50.0 /. float state.w in
1261 state.birdseye <- Some (
1262 conf.zoom,
1263 state.x,
1264 conf.presentation,
1265 conf.interpagespace;
1267 conf.zoom <- zoom;
1268 conf.presentation <- false;
1269 conf.interpagespace <- 10;
1270 state.x <- 0;
1271 state.mstate <- Mnone;
1272 Glut.setCursor Glut.CURSOR_INHERIT;
1273 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1274 (100.0*.zoom)
1276 | Some vals ->
1277 birdseyeoff vals;
1278 end;
1279 reshape conf.winw conf.winh
1281 | '0' .. '9' ->
1282 let ondone s =
1283 let n =
1284 try int_of_string s with exc ->
1285 state.text <- Printf.sprintf "bad integer `%s': %s"
1286 s (Printexc.to_string exc);
1289 if n >= 0
1290 then (
1291 addnav ();
1292 cbput state.hists.pag (string_of_int n);
1293 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1296 let pageentry text key =
1297 match Char.unsafe_chr key with
1298 | 'g' -> TEdone text
1299 | _ -> intentry text key
1301 let text = "x" in text.[0] <- c;
1302 enttext (Some (':', text, Some (onhist state.hists.pag),
1303 pageentry, ondone))
1305 | 'b' ->
1306 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1307 reshape conf.winw conf.winh;
1309 | 'l' ->
1310 conf.hlinks <- not conf.hlinks;
1311 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1312 Glut.postRedisplay ()
1314 | 'a' ->
1315 conf.autoscroll <- not conf.autoscroll
1317 | 'P' ->
1318 conf.presentation <- not conf.presentation;
1319 showtext ' ' ("presentation mode " ^
1320 if conf.presentation then "on" else "off");
1321 represent ()
1323 | 'f' ->
1324 begin match state.fullscreen with
1325 | None ->
1326 state.fullscreen <- Some (conf.winw, conf.winh);
1327 Glut.fullScreen ()
1328 | Some (w, h) ->
1329 state.fullscreen <- None;
1330 doreshape w h
1333 | 'g' ->
1334 gotoy_and_clear_text 0
1336 | 'n' ->
1337 search state.searchpattern true
1339 | 'p' | 'N' ->
1340 search state.searchpattern false
1342 | 't' ->
1343 begin match state.layout with
1344 | [] -> ()
1345 | l :: _ ->
1346 gotoy_and_clear_text (getpagey l.pageno)
1349 | ' ' ->
1350 begin match List.rev state.layout with
1351 | [] -> ()
1352 | l :: _ ->
1353 let pageno = min (l.pageno+1) (state.pagecount-1) in
1354 gotoy_and_clear_text (getpagey pageno)
1357 | '\127' ->
1358 begin match state.layout with
1359 | [] -> ()
1360 | l :: _ ->
1361 let pageno = max 0 (l.pageno-1) in
1362 gotoy_and_clear_text (getpagey pageno)
1365 | '=' ->
1366 let f (fn, ln) l =
1367 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1369 let fn, ln = List.fold_left f (-1, -1) state.layout in
1370 let s =
1371 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1372 let percent =
1373 if maxy <= 0
1374 then 100.
1375 else (100. *. (float state.y /. float maxy)) in
1376 if fn = ln
1377 then
1378 Printf.sprintf "Page %d of %d %.2f%%"
1379 (fn+1) state.pagecount percent
1380 else
1381 Printf.sprintf
1382 "Pages %d-%d of %d %.2f%%"
1383 (fn+1) (ln+1) state.pagecount percent
1385 showtext ' ' s;
1387 | 'w' ->
1388 begin match state.layout with
1389 | [] -> ()
1390 | l :: _ ->
1391 doreshape (l.pagew + conf.scrollw) l.pageh;
1392 Glut.postRedisplay ();
1395 | '\'' ->
1396 enterbookmarkmode ()
1398 | 'm' ->
1399 let ondone s =
1400 match state.layout with
1401 | l :: _ ->
1402 state.bookmarks <-
1403 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1404 :: state.bookmarks
1405 | _ -> ()
1407 enttext (Some ('~', "", None, textentry, ondone))
1409 | '~' ->
1410 quickbookmark ();
1411 showtext ' ' "Quick bookmark added";
1413 | 'z' ->
1414 begin match state.layout with
1415 | l :: _ ->
1416 let rect = getpdimrect l.pagedimno in
1417 let w, h =
1418 if conf.crophack
1419 then
1420 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1421 truncate (1.2 *. (rect.(3) -. rect.(0))))
1422 else
1423 (truncate (rect.(1) -. rect.(0)),
1424 truncate (rect.(3) -. rect.(0)))
1426 if w != 0 && h != 0
1427 then
1428 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1430 Glut.postRedisplay ();
1432 | [] -> ()
1435 | '<' | '>' ->
1436 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1438 | '[' | ']' ->
1439 state.colorscale <-
1440 max 0.0
1441 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1442 Glut.postRedisplay ()
1444 | 'k' -> gotoy (clamp (-conf.scrollincr))
1445 | 'j' -> gotoy (clamp conf.scrollincr)
1447 | 'r' -> opendoc state.path state.password
1449 | _ ->
1450 vlog "huh? %d %c" key (Char.chr key);
1453 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1454 let len = String.length text in
1455 if len = 0
1456 then (
1457 state.textentry <- None;
1458 Glut.postRedisplay ();
1460 else (
1461 let s = String.sub text 0 (len - 1) in
1462 enttext (Some (c, s, opthist, onkey, ondone))
1465 | Some (c, text, onhist, onkey, ondone) ->
1466 begin match Char.unsafe_chr key with
1467 | '\r' | '\n' ->
1468 ondone text;
1469 state.textentry <- None;
1470 Glut.postRedisplay ()
1472 | '\027' ->
1473 begin match onhist with
1474 | None -> ()
1475 | Some (_, onhistcancel) -> onhistcancel ()
1476 end;
1477 state.textentry <- None;
1478 Glut.postRedisplay ()
1480 | _ ->
1481 begin match onkey text key with
1482 | TEdone text ->
1483 state.textentry <- None;
1484 ondone text;
1485 Glut.postRedisplay ()
1487 | TEcont text ->
1488 enttext (Some (c, text, onhist, onkey, ondone));
1490 | TEstop ->
1491 state.textentry <- None;
1492 Glut.postRedisplay ()
1494 | TEswitch te ->
1495 state.textentry <- Some te;
1496 Glut.postRedisplay ()
1497 end;
1498 end;
1501 let narrow outlines pattern =
1502 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1503 match reopt with
1504 | None -> None
1505 | Some re ->
1506 let rec fold accu n =
1507 if n = -1
1508 then accu
1509 else
1510 let (s, _, _, _) as o = outlines.(n) in
1511 let accu =
1512 if (try ignore (Str.search_forward re s 0); true
1513 with Not_found -> false)
1514 then (o :: accu)
1515 else accu
1517 fold accu (n-1)
1519 let matched = fold [] (Array.length outlines - 1) in
1520 if matched = [] then None else Some (Array.of_list matched)
1523 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1524 let search active pattern incr =
1525 let dosearch re =
1526 let rec loop n =
1527 if n = Array.length outlines || n = -1
1528 then None
1529 else
1530 let (s, _, _, _) = outlines.(n) in
1532 (try ignore (Str.search_forward re s 0); true
1533 with Not_found -> false)
1534 then Some n
1535 else loop (n + incr)
1537 loop active
1540 let re = Str.regexp_case_fold pattern in
1541 dosearch re
1542 with Failure s ->
1543 state.text <- s;
1544 None
1546 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1547 match key with
1548 | 27 ->
1549 if String.length qsearch = 0
1550 then (
1551 state.text <- "";
1552 state.outline <- None;
1553 Glut.postRedisplay ();
1555 else (
1556 state.text <- "";
1557 state.outline <- Some (allowdel, active, first, outlines, "");
1558 Glut.postRedisplay ();
1561 | 18 | 19 ->
1562 let incr = if key = 18 then -1 else 1 in
1563 let active, first =
1564 match search (active + incr) qsearch incr with
1565 | None ->
1566 state.text <- qsearch ^ " [not found]";
1567 active, first
1568 | Some active ->
1569 state.text <- qsearch;
1570 active, firstof active
1572 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1573 Glut.postRedisplay ();
1575 | 8 ->
1576 let len = String.length qsearch in
1577 if len = 0
1578 then ()
1579 else (
1580 if len = 1
1581 then (
1582 state.text <- "";
1583 state.outline <- Some (allowdel, active, first, outlines, "");
1585 else
1586 let qsearch = String.sub qsearch 0 (len - 1) in
1587 let active, first =
1588 match search active qsearch ~-1 with
1589 | None ->
1590 state.text <- qsearch ^ " [not found]";
1591 active, first
1592 | Some active ->
1593 state.text <- qsearch;
1594 active, firstof active
1596 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1598 Glut.postRedisplay ()
1600 | 13 ->
1601 if active < Array.length outlines
1602 then (
1603 let (_, _, n, t) = outlines.(active) in
1604 gotopage n t;
1606 state.text <- "";
1607 if allowdel then state.bookmarks <- Array.to_list outlines;
1608 state.outline <- None;
1609 Glut.postRedisplay ();
1611 | _ when key >= 32 && key < 127 ->
1612 let pattern = addchar qsearch (Char.chr key) in
1613 let active, first =
1614 match search active pattern 1 with
1615 | None ->
1616 state.text <- pattern ^ " [not found]";
1617 active, first
1618 | Some active ->
1619 state.text <- pattern;
1620 active, firstof active
1622 state.outline <- Some (allowdel, active, first, outlines, pattern);
1623 Glut.postRedisplay ()
1625 | 14 when not allowdel -> (* ctrl-n *)
1626 if String.length qsearch > 0
1627 then (
1628 let optoutlines = narrow outlines qsearch in
1629 begin match optoutlines with
1630 | None -> state.text <- "can't narrow"
1631 | Some outlines ->
1632 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1633 match state.outlines with
1634 | Olist l -> ()
1635 | Oarray a ->
1636 state.outlines <- Onarrow (qsearch, outlines, a)
1637 | Onarrow (pat, a, b) ->
1638 state.outlines <- Onarrow (qsearch, outlines, b)
1639 end;
1641 Glut.postRedisplay ()
1643 | 21 when not allowdel -> (* ctrl-u *)
1644 let outline =
1645 match state.outlines with
1646 | Oarray a -> a
1647 | Olist l ->
1648 let a = Array.of_list (List.rev l) in
1649 state.outlines <- Oarray a;
1651 | Onarrow (pat, a, b) ->
1652 state.outlines <- Oarray b;
1653 state.text <- "";
1656 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1657 Glut.postRedisplay ()
1659 | 12 ->
1660 state.outline <-
1661 Some (allowdel, active, firstof active, outlines, qsearch);
1662 Glut.postRedisplay ()
1664 | 127 when allowdel ->
1665 let len = Array.length outlines - 1 in
1666 if len = 0
1667 then (
1668 state.outline <- None;
1669 state.bookmarks <- [];
1671 else (
1672 let bookmarks = Array.init len
1673 (fun i ->
1674 let i = if i >= active then i + 1 else i in
1675 outlines.(i)
1678 state.outline <-
1679 Some (allowdel,
1680 min active (len-1),
1681 min first (len-1),
1682 bookmarks, qsearch)
1685 Glut.postRedisplay ()
1687 | _ -> log "unknown key %d" key
1690 let keyboard ~key ~x ~y =
1691 if key = 7
1692 then
1693 wcmd "interrupt" []
1694 else
1695 match state.outline with
1696 | None -> viewkeyboard ~key ~x ~y
1697 | Some outline -> outlinekeyboard ~key ~x ~y outline
1700 let special ~key ~x ~y =
1701 match state.outline with
1702 | None when state.birdseye <> None ->
1703 begin match key with
1704 | Glut.KEY_UP ->
1705 let pageno = max 0 (state.birdseyepageno - 1) in
1706 state.birdseyepageno <- pageno;
1707 if not (pagevisible pageno)
1708 then gotopage pageno 0.0
1709 else Glut.postRedisplay ();
1711 | Glut.KEY_DOWN ->
1712 let pageno = min (state.pagecount - 1) (state.birdseyepageno + 1) in
1713 state.birdseyepageno <- pageno;
1714 if not (pagevisible pageno)
1715 then
1716 begin match List.rev state.layout with
1717 | [] -> gotopage pageno 0.0
1718 | l :: _ ->
1719 gotoy (state.y + conf.interpagespace + l.pageh*2 - l.pagevh)
1721 else Glut.postRedisplay ();
1723 | Glut.KEY_PAGE_UP ->
1724 begin match state.layout with
1725 | l :: _ ->
1726 if l.pageno = state.birdseyepageno
1727 then gotoy (clamp (-conf.winh))
1728 else (
1729 state.birdseyepageno <- max 0 (l.pageno - 1);
1730 gotopage state.birdseyepageno 0.0
1732 | [] -> gotoy (clamp (-conf.winh))
1733 end;
1734 | Glut.KEY_PAGE_DOWN ->
1735 begin match List.rev state.layout with
1736 | l :: _ ->
1737 state.birdseyepageno <- min (state.pagecount - 1) (l.pageno + 1);
1738 gotoy (clamp (l.pagedispy + conf.interpagespace + l.pageh))
1739 | [] -> gotoy (clamp conf.winh)
1740 end;
1742 | Glut.KEY_HOME ->
1743 state.birdseyepageno <- 0;
1744 gotopage 0 0.0
1745 | Glut.KEY_END ->
1746 state.birdseyepageno <- state.pagecount - 1;
1747 if not (pagevisible state.birdseyepageno)
1748 then
1749 gotopage state.birdseyepageno 0.0
1750 else
1751 Glut.postRedisplay ()
1753 | _ -> ()
1756 | None ->
1757 begin match state.textentry with
1758 | None ->
1759 let y =
1760 match key with
1761 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1762 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1763 | Glut.KEY_DOWN -> clamp conf.scrollincr
1764 | Glut.KEY_PAGE_UP ->
1765 if Glut.getModifiers () land Glut.active_ctrl != 0
1766 then
1767 match state.layout with
1768 | [] -> state.y
1769 | l :: _ -> state.y - l.pagey
1770 else
1771 clamp (-conf.winh)
1772 | Glut.KEY_PAGE_DOWN ->
1773 if Glut.getModifiers () land Glut.active_ctrl != 0
1774 then
1775 match List.rev state.layout with
1776 | [] -> state.y
1777 | l :: _ -> getpagey l.pageno
1778 else
1779 clamp conf.winh
1780 | Glut.KEY_HOME -> addnav (); 0
1781 | Glut.KEY_END ->
1782 addnav ();
1783 state.maxy - (if conf.maxhfit then conf.winh else 0)
1785 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1786 state.x <- state.x - 10;
1787 state.y
1788 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1789 state.x <- state.x + 10;
1790 state.y
1792 | _ -> state.y
1794 gotoy_and_clear_text y
1796 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1797 let s =
1798 match key with
1799 | Glut.KEY_UP -> action HCprev
1800 | Glut.KEY_DOWN -> action HCnext
1801 | Glut.KEY_HOME -> action HCfirst
1802 | Glut.KEY_END -> action HClast
1803 | _ -> state.text
1805 state.textentry <- Some (c, s, onhist, onkey, ondone);
1806 Glut.postRedisplay ()
1808 | _ -> ()
1811 | Some (allowdel, active, first, outlines, qsearch) ->
1812 let maxrows = maxoutlinerows () in
1813 let calcfirst first active =
1814 if active > first
1815 then
1816 let rows = active - first in
1817 if rows > maxrows then active - maxrows else first
1818 else active
1820 let navigate incr =
1821 let active = active + incr in
1822 let active = max 0 (min active (Array.length outlines - 1)) in
1823 let first = calcfirst first active in
1824 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1825 Glut.postRedisplay ()
1827 let updownlevel incr =
1828 let len = Array.length outlines in
1829 let (_, curlevel, _, _) = outlines.(active) in
1830 let rec flow i =
1831 if i = len then i-1 else if i = -1 then 0 else
1832 let (_, l, _, _) = outlines.(i) in
1833 if l != curlevel then i else flow (i+incr)
1835 let active = flow active in
1836 let first = calcfirst first active in
1837 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1838 Glut.postRedisplay ()
1840 match key with
1841 | Glut.KEY_UP -> navigate ~-1
1842 | Glut.KEY_DOWN -> navigate 1
1843 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1844 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1846 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1847 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1849 | Glut.KEY_HOME ->
1850 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1851 Glut.postRedisplay ()
1853 | Glut.KEY_END ->
1854 let active = Array.length outlines - 1 in
1855 let first = max 0 (active - maxrows) in
1856 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1857 Glut.postRedisplay ()
1859 | _ -> ()
1862 let drawplaceholder l =
1863 GlDraw.color (scalecolor 1.0);
1864 GlDraw.rect
1865 (float l.pagex, float l.pagedispy)
1866 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1868 let x = float l.pagex
1869 and y = float (l.pagedispy + 13) in
1870 let font = Glut.BITMAP_8_BY_13 in
1871 GlDraw.color (0.0, 0.0, 0.0);
1872 GlPix.raster_pos ~x ~y ();
1873 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1874 ("Loading " ^ string_of_int (l.pageno + 1));
1877 let now () = Unix.gettimeofday ();;
1879 let drawpage l =
1880 begin match getopaque l.pageno with
1881 | Some (opaque, _) when validopaque opaque ->
1882 if state.textentry = None
1883 then GlDraw.color (scalecolor 1.0)
1884 else GlDraw.color (scalecolor 0.4);
1885 let a = now () in
1886 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1887 opaque;
1888 let b = now () in
1889 let d = b-.a in
1890 vlog "draw %d %f sec" l.pageno d;
1892 | _ ->
1893 drawplaceholder l;
1894 end;
1895 if state.birdseye <> None && state.birdseyepageno = l.pageno
1896 then (
1897 GlDraw.polygon_mode `both `line;
1898 GlDraw.line_width 4.0;
1899 GlDraw.color (0.8, 0.0, 0.0);
1900 GlDraw.rect
1901 (float (l.pagex - 1), float (l.pagedispy - 1))
1902 (float (l.pagew + l.pagex + 1), float (l.pagedispy + l.pagevh + 1))
1904 GlDraw.line_width 1.0;
1905 GlDraw.polygon_mode `both `fill;
1909 let scrollph y =
1910 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1911 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1912 let sh = float conf.winh /. sh in
1913 let sh = max sh (float conf.scrollh) in
1915 let percent =
1916 if state.y = state.maxy
1917 then 1.0
1918 else float y /. float maxy
1920 let position = (float conf.winh -. sh) *. percent in
1922 let position =
1923 if position +. sh > float conf.winh
1924 then float conf.winh -. sh
1925 else position
1927 position, sh;
1930 let scrollindicator () =
1931 GlDraw.color (0.64 , 0.64, 0.64);
1932 GlDraw.rect
1933 (float (conf.winw - conf.scrollw), 0.)
1934 (float conf.winw, float conf.winh)
1936 GlDraw.color (0.0, 0.0, 0.0);
1938 let position, sh = scrollph state.y in
1939 GlDraw.rect
1940 (float (conf.winw - conf.scrollw), position)
1941 (float conf.winw, position +. sh)
1945 let showsel margin =
1946 match state.mstate with
1947 | Mnone | Mscroll _ | Mpan _ ->
1950 | Msel ((x0, y0), (x1, y1)) ->
1951 let rec loop = function
1952 | l :: ls ->
1953 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1954 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1955 then
1956 match getopaque l.pageno with
1957 | Some (opaque, _) when validopaque opaque ->
1958 let oy = -l.pagey + l.pagedispy in
1959 seltext opaque
1960 (x0 - margin - state.x, y0,
1961 x1 - margin - state.x, y1) oy;
1963 | _ -> ()
1964 else loop ls
1965 | [] -> ()
1967 loop state.layout
1970 let showrects () =
1971 let panx = float state.x in
1972 Gl.enable `blend;
1973 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1974 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1975 List.iter
1976 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1977 List.iter (fun l ->
1978 if l.pageno = pageno
1979 then (
1980 let d = float (l.pagedispy - l.pagey) in
1981 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1982 GlDraw.begins `quads;
1984 GlDraw.vertex2 (x0+.panx, y0+.d);
1985 GlDraw.vertex2 (x1+.panx, y1+.d);
1986 GlDraw.vertex2 (x2+.panx, y2+.d);
1987 GlDraw.vertex2 (x3+.panx, y3+.d);
1989 GlDraw.ends ();
1991 ) state.layout
1992 ) state.rects
1994 Gl.disable `blend;
1997 let showoutline = function
1998 | None -> ()
1999 | Some (allowdel, active, first, outlines, qsearch) ->
2000 Gl.enable `blend;
2001 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2002 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2003 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2004 Gl.disable `blend;
2006 GlDraw.color (1., 1., 1.);
2007 let font = Glut.BITMAP_9_BY_15 in
2008 let draw_string x y s =
2009 GlPix.raster_pos ~x ~y ();
2010 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2012 let rec loop row =
2013 if row = Array.length outlines || (row - first) * 16 > conf.winh
2014 then ()
2015 else (
2016 let (s, l, _, _) = outlines.(row) in
2017 let y = (row - first) * 16 in
2018 let x = 5 + 15*l in
2019 if row = active
2020 then (
2021 Gl.enable `blend;
2022 GlDraw.polygon_mode `both `line;
2023 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2024 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2025 GlDraw.rect (0., float (y + 1))
2026 (float (conf.winw - 1), float (y + 18));
2027 GlDraw.polygon_mode `both `fill;
2028 Gl.disable `blend;
2029 GlDraw.color (1., 1., 1.);
2031 draw_string (float x) (float (y + 16)) s;
2032 loop (row+1)
2035 loop first
2038 let display () =
2039 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2040 GlDraw.viewport margin 0 state.w conf.winh;
2041 pagematrix ();
2042 if state.birdseye <> None
2043 then
2044 GlClear.color (0.5, 0.5, 0.55)
2045 else
2046 GlClear.color (scalecolor 0.5)
2048 GlClear.clear [`color];
2049 if state.x != 0
2050 then (
2051 let x = float state.x in
2052 GlMat.translate ~x ();
2054 if conf.zoom > 1.0
2055 then (
2056 Gl.enable `scissor_test;
2057 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2059 List.iter drawpage state.layout;
2060 if conf.zoom > 1.0
2061 then
2062 Gl.disable `scissor_test
2064 if state.x != 0
2065 then (
2066 let x = -.float state.x in
2067 GlMat.translate ~x ();
2069 showrects ();
2070 showsel margin;
2071 GlDraw.viewport 0 0 conf.winw conf.winh;
2072 winmatrix ();
2073 scrollindicator ();
2074 showoutline state.outline;
2075 enttext ();
2076 Glut.swapBuffers ();
2079 let getunder x y =
2080 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2081 let x = x - margin - state.x in
2082 let rec f = function
2083 | l :: rest ->
2084 begin match getopaque l.pageno with
2085 | Some (opaque, _) when validopaque opaque ->
2086 let y = y - l.pagedispy in
2087 if y > 0
2088 then
2089 let y = l.pagey + y in
2090 let x = x - l.pagex in
2091 match whatsunder opaque x y with
2092 | Unone -> f rest
2093 | under -> under
2094 else
2095 f rest
2096 | _ ->
2097 f rest
2099 | [] -> Unone
2101 f state.layout
2104 let mouse ~button ~bstate ~x ~y =
2105 match button with
2106 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2107 let incr =
2108 if n = 3
2109 then
2110 -conf.scrollincr
2111 else
2112 conf.scrollincr
2114 let incr = incr * 2 in
2115 let y = clamp incr in
2116 gotoy_and_clear_text y
2118 | Glut.LEFT_BUTTON when state.outline = None
2119 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2120 if bstate = Glut.DOWN
2121 then (
2122 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2123 state.mstate <- Mpan (x, y)
2125 else
2126 state.mstate <- Mnone
2128 | Glut.LEFT_BUTTON
2129 when state.outline = None && x > conf.winw - conf.scrollw ->
2130 if bstate = Glut.DOWN
2131 then
2132 let position, sh = scrollph state.y in
2133 if y > truncate position && y < truncate (position +. sh)
2134 then
2135 state.mstate <- Mscroll
2136 else
2137 let percent = float y /. float conf.winh in
2138 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2139 gotoy desty;
2140 state.mstate <- Mscroll
2141 else
2142 state.mstate <- Mnone
2144 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2145 begin match state.birdseye with
2146 | Some vals ->
2147 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2148 let rec loop = function
2149 | [] -> ()
2150 | l :: rest ->
2151 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2152 && x > margin && x < margin + l.pagew
2153 then (
2154 let y = getpagey l.pageno in
2155 state.y <- y;
2156 birdseyeoff vals;
2157 reshape conf.winw conf.winh;
2159 else loop rest
2161 loop state.layout;
2162 | None -> () (* impossible *)
2165 | Glut.LEFT_BUTTON when state.outline = None ->
2166 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2167 begin match dest with
2168 | Ulinkgoto (pageno, top) ->
2169 if pageno >= 0
2170 then
2171 gotopage1 pageno top
2173 | Ulinkuri s ->
2174 print_endline s
2176 | Unone when bstate = Glut.DOWN ->
2177 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2178 state.mstate <- Mpan (x, y);
2180 | Unone | Utext _ ->
2181 if bstate = Glut.DOWN
2182 then (
2183 if conf.angle mod 360 = 0
2184 then (
2185 state.mstate <- Msel ((x, y), (x, y));
2186 Glut.postRedisplay ()
2189 else (
2190 match state.mstate with
2191 | Mnone -> ()
2193 | Mscroll ->
2194 state.mstate <- Mnone
2196 | Mpan _ ->
2197 Glut.setCursor Glut.CURSOR_INHERIT;
2198 state.mstate <- Mnone
2200 | Msel ((x0, y0), (x1, y1)) ->
2201 let f l =
2202 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2203 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2204 then
2205 match getopaque l.pageno with
2206 | Some (opaque, _) when validopaque opaque ->
2207 copysel opaque
2208 | _ -> ()
2210 List.iter f state.layout;
2211 copysel ""; (* ugly *)
2212 Glut.setCursor Glut.CURSOR_INHERIT;
2213 state.mstate <- Mnone;
2217 | _ ->
2220 let mouse ~button ~state ~x ~y = mouse button state x y;;
2222 let motion ~x ~y =
2223 if state.outline = None
2224 then
2225 match state.mstate with
2226 | Mnone -> ()
2228 | Mpan (x0, y0) ->
2229 let dx = x - x0
2230 and dy = y0 - y in
2231 state.mstate <- Mpan (x, y);
2232 if conf.zoom > 1.0 then state.x <- state.x + dx;
2233 let y = clamp dy in
2234 gotoy_and_clear_text y
2236 | Msel (a, _) ->
2237 state.mstate <- Msel (a, (x, y));
2238 Glut.postRedisplay ()
2240 | Mscroll ->
2241 let y = min conf.winh (max 0 y) in
2242 let percent = float y /. float conf.winh in
2243 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2244 gotoy_and_clear_text y
2247 let pmotion ~x ~y =
2248 if state.outline = None && state.birdseye = None
2249 then
2250 match state.mstate with
2251 | Mnone ->
2252 begin match getunder x y with
2253 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2254 | Ulinkuri uri ->
2255 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2256 Glut.setCursor Glut.CURSOR_INFO
2257 | Ulinkgoto (page, y) ->
2258 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
2259 Glut.setCursor Glut.CURSOR_INFO
2260 | Utext s ->
2261 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2262 Glut.setCursor Glut.CURSOR_TEXT
2265 | Mpan _ | Msel _ | Mscroll ->
2269 module State =
2270 struct
2271 open Parser
2273 let home =
2275 match Sys.os_type with
2276 | "Win32" -> Sys.getenv "HOMEPATH"
2277 | _ -> Sys.getenv "HOME"
2278 with exn ->
2279 prerr_endline
2280 ("Can not determine home directory location: " ^
2281 Printexc.to_string exn);
2285 let config_of c attrs =
2286 let apply c k v =
2288 match k with
2289 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2290 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2291 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2292 | "preload" -> { c with preload = bool_of_string v }
2293 | "page-bias" -> { c with pagebias = int_of_string v }
2294 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2295 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2296 | "crop-hack" -> { c with crophack = bool_of_string v }
2297 | "throttle" -> { c with showall = bool_of_string v }
2298 | "highlight-links" -> { c with hlinks = bool_of_string v }
2299 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2300 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2301 | "zoom" ->
2302 let zoom = float_of_string v /. 100. in
2303 let zoom = max 0.01 (min 2.2 zoom) in
2304 { c with zoom = zoom }
2305 | "presentation" -> { c with presentation = bool_of_string v }
2306 | "rotation-angle" -> { c with angle = int_of_string v }
2307 | "width" -> { c with winw = max 20 (int_of_string v) }
2308 | "height" -> { c with winh = max 20 (int_of_string v) }
2309 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2310 | "proportional-display" -> { c with proportional = bool_of_string v }
2311 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2312 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2313 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2314 | _ -> c
2315 with exn ->
2316 prerr_endline ("Error processing attribute (`" ^
2317 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2320 let rec fold c = function
2321 | [] -> c
2322 | (k, v) :: rest ->
2323 let c = apply c k v in
2324 fold c rest
2326 fold c attrs;
2329 let bookmark_of attrs =
2330 let rec fold title page rely = function
2331 | ("title", v) :: rest -> fold v page rely rest
2332 | ("page", v) :: rest -> fold title v rely rest
2333 | ("rely", v) :: rest -> fold title page v rest
2334 | _ :: rest -> fold title page rely rest
2335 | [] -> title, page, rely
2337 fold "invalid" "0" "0" attrs
2340 let setconf dst src =
2341 dst.scrollw <- src.scrollw;
2342 dst.scrollh <- src.scrollh;
2343 dst.icase <- src.icase;
2344 dst.preload <- src.preload;
2345 dst.pagebias <- src.pagebias;
2346 dst.verbose <- src.verbose;
2347 dst.scrollincr <- src.scrollincr;
2348 dst.maxhfit <- src.maxhfit;
2349 dst.crophack <- src.crophack;
2350 dst.autoscroll <- src.autoscroll;
2351 dst.showall <- src.showall;
2352 dst.hlinks <- src.hlinks;
2353 dst.underinfo <- src.underinfo;
2354 dst.interpagespace <- src.interpagespace;
2355 dst.zoom <- src.zoom;
2356 dst.presentation <- src.presentation;
2357 dst.angle <- src.angle;
2358 dst.winw <- src.winw;
2359 dst.winh <- src.winh;
2360 dst.savebmarks <- src.savebmarks;
2361 dst.memlimit <- src.memlimit;
2362 dst.proportional <- src.proportional;
2363 dst.texcount <- src.texcount;
2364 dst.sliceheight <- src.sliceheight;
2367 let unent s =
2368 let l = String.length s in
2369 let b = Buffer.create l in
2370 unent b s 0 l;
2371 Buffer.contents b;
2374 let get s =
2375 let h = Hashtbl.create 10 in
2376 let dc = { defconf with angle = defconf.angle } in
2377 let rec toplevel v t spos epos =
2378 match t with
2379 | Vdata | Vcdata | Vend -> v
2380 | Vopen ("llppconfig", attrs, closed) ->
2381 if closed
2382 then v
2383 else { v with f = llppconfig }
2384 | Vopen _ ->
2385 error "unexpected subelement at top level" s spos
2386 | Vclose tag -> error "unexpected close at top level" s spos
2388 and llppconfig v t spos epos =
2389 match t with
2390 | Vdata | Vcdata | Vend -> v
2391 | Vopen ("defaults", attrs, closed) ->
2392 let c = config_of dc attrs in
2393 setconf dc c;
2394 if closed
2395 then v
2396 else { v with f = skip "defaults" (fun () -> v) }
2398 | Vopen ("doc", attrs, closed) ->
2399 let pathent =
2401 List.assoc "path" attrs
2402 with Not_found -> error "doc is missing path attribute" s spos
2404 let path = unent pathent in
2405 let c = config_of dc attrs in
2406 let y =
2408 float_of_string (List.assoc "rely" attrs)
2409 with
2410 | Not_found -> 0.0
2411 | exn ->
2412 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2415 let x =
2417 int_of_string (List.assoc "pan" attrs)
2418 with
2419 | Not_found -> 0
2420 | exn ->
2421 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2424 if closed
2425 then (Hashtbl.add h path (c, [], x, y); v)
2426 else { v with f = doc path x y c [] }
2428 | Vopen (tag, _, closed) ->
2429 error "unexpected subelement in llppconfig" s spos
2431 | Vclose "llppconfig" -> { v with f = toplevel }
2432 | Vclose tag -> error "unexpected close in llppconfig" s spos
2434 and doc path x y c bookmarks v t spos epos =
2435 match t with
2436 | Vdata | Vcdata -> v
2437 | Vend -> error "unexpected end of input in doc" s spos
2438 | Vopen ("bookmarks", attrs, closed) ->
2439 { v with f = pbookmarks path x y c bookmarks }
2441 | Vopen (tag, _, _) ->
2442 error "unexpected subelement in doc" s spos
2444 | Vclose "doc" ->
2445 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2446 { v with f = llppconfig }
2448 | Vclose tag -> error "unexpected close in doc" s spos
2450 and pbookmarks path x y c bookmarks v t spos epos =
2451 match t with
2452 | Vdata | Vcdata -> v
2453 | Vend -> error "unexpected end of input in bookmarks" s spos
2454 | Vopen ("item", attrs, closed) ->
2455 let titleent, spage, srely = bookmark_of attrs in
2456 let page =
2458 int_of_string spage
2459 with exn ->
2460 dolog "Failed to convert page %S to integer: %s"
2461 spage (Printexc.to_string exn);
2464 let rely =
2466 float_of_string srely
2467 with exn ->
2468 dolog "Failed to convert rely %S to real: %s"
2469 srely (Printexc.to_string exn);
2472 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2473 if closed
2474 then { v with f = pbookmarks path x y c bookmarks }
2475 else
2476 let f () = v in
2477 { v with f = skip "item" f }
2479 | Vopen _ ->
2480 error "unexpected subelement in bookmarks" s spos
2482 | Vclose "bookmarks" ->
2483 { v with f = doc path x y c bookmarks }
2485 | Vclose tag -> error "unexpected close in bookmarks" s spos
2487 and skip tag f v t spos epos =
2488 match t with
2489 | Vdata | Vcdata -> v
2490 | Vend ->
2491 error ("unexpected end of input in skipped " ^ tag) s spos
2492 | Vopen (tag', _, closed) ->
2493 if closed
2494 then v
2495 else
2496 let f' () = { v with f = skip tag f } in
2497 { v with f = skip tag' f' }
2498 | Vclose ctag ->
2499 if tag = ctag
2500 then f ()
2501 else error ("unexpected close in skipped " ^ tag) s spos
2504 parse { f = toplevel; accu = () } s;
2505 h, dc;
2508 let do_load f ic =
2510 let len = in_channel_length ic in
2511 let s = String.create len in
2512 really_input ic s 0 len;
2513 f s;
2514 with
2515 | Parse_error (msg, s, pos) ->
2516 let subs = subs s pos in
2517 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2518 failwith ("parse error: " ^ s)
2520 | exn ->
2521 failwith ("config load error: " ^ Printexc.to_string exn)
2524 let path =
2525 let dir =
2527 let dir = Filename.concat home ".config" in
2528 if Sys.is_directory dir then dir else home
2529 with _ -> home
2531 Filename.concat dir "llpp.conf"
2534 let load1 f =
2535 if Sys.file_exists path
2536 then
2537 match
2538 (try Some (open_in_bin path)
2539 with exn ->
2540 prerr_endline
2541 ("Error opening configuation file `" ^ path ^ "': " ^
2542 Printexc.to_string exn);
2543 None
2545 with
2546 | Some ic ->
2547 begin try
2548 f (do_load get ic)
2549 with exn ->
2550 prerr_endline
2551 ("Error loading configuation from `" ^ path ^ "': " ^
2552 Printexc.to_string exn);
2553 end;
2554 close_in ic;
2556 | None -> ()
2557 else
2558 f (Hashtbl.create 0, defconf)
2561 let load () =
2562 let f (h, dc) =
2563 let pc, pb, px, py =
2565 Hashtbl.find h state.path
2566 with Not_found -> dc, [], 0, 0.0
2568 setconf defconf dc;
2569 setconf conf pc;
2570 state.bookmarks <- pb;
2571 state.x <- px;
2572 cbput state.hists.nav py;
2574 load1 f
2577 let add_attrs bb always dc c =
2578 let ob s a b =
2579 if always || a != b
2580 then Printf.bprintf bb "\n %s='%b'" s a
2581 and oi s a b =
2582 if always || a != b
2583 then Printf.bprintf bb "\n %s='%d'" s a
2584 and oz s a b =
2585 if always || a <> b
2586 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2588 let w, h =
2589 if always
2590 then dc.winw, dc.winh
2591 else
2592 match state.fullscreen with
2593 | Some wh -> wh
2594 | None -> c.winw, c.winh
2596 let zoom, presentation, interpagespace =
2597 if always
2598 then dc.zoom, dc.presentation, dc.interpagespace
2599 else
2600 match state.birdseye with
2601 | Some (zoom, _, presentation, interpagespace) ->
2602 (zoom, presentation, interpagespace)
2603 | None -> c.zoom, c.presentation, c.interpagespace
2605 oi "width" w dc.winw;
2606 oi "height" h dc.winh;
2607 oi "scroll-bar-width" c.scrollw dc.scrollw;
2608 oi "scroll-handle-height" c.scrollh dc.scrollh;
2609 ob "case-insensitive-search" c.icase dc.icase;
2610 ob "preload" c.preload dc.preload;
2611 oi "page-bias" c.pagebias dc.pagebias;
2612 oi "scroll-step" c.scrollincr dc.scrollincr;
2613 ob "max-height-fit" c.maxhfit dc.maxhfit;
2614 ob "crop-hack" c.crophack dc.crophack;
2615 ob "throttle" c.showall dc.showall;
2616 ob "highlight-links" c.hlinks dc.hlinks;
2617 ob "under-cursor-info" c.underinfo dc.underinfo;
2618 oi "vertical-margin" interpagespace dc.interpagespace;
2619 oz "zoom" zoom dc.zoom;
2620 ob "presentation" presentation dc.presentation;
2621 oi "rotation-angle" c.angle dc.angle;
2622 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2623 ob "proportional-display" c.proportional dc.proportional;
2624 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2625 oi "texcount" c.texcount dc.texcount;
2626 oi "slice-height" c.sliceheight dc.sliceheight;
2629 let save () =
2630 let bb = Buffer.create 32768 in
2631 let f (h, dc) =
2632 Buffer.add_string bb "<llppconfig>\n<defaults ";
2633 add_attrs bb true dc dc;
2634 Buffer.add_string bb "/>\n";
2636 let adddoc path x y c bookmarks =
2637 if bookmarks == [] && c = dc && y = 0.0
2638 then ()
2639 else (
2640 Printf.bprintf bb "<doc path='%s'"
2641 (enent path 0 (String.length path));
2643 if y <> 0.0
2644 then Printf.bprintf bb " rely='%f'" y;
2646 if x != 0
2647 then Printf.bprintf bb " pan='%d'" x;
2649 add_attrs bb false dc c;
2651 begin match bookmarks with
2652 | [] -> Buffer.add_string bb "/>\n"
2653 | _ ->
2654 Buffer.add_string bb ">\n<bookmarks>\n";
2655 List.iter (fun (title, _level, page, rely) ->
2656 Printf.bprintf bb
2657 "<item title='%s' page='%d' rely='%f'/>\n"
2658 (enent title 0 (String.length title))
2659 page
2660 rely
2661 ) bookmarks;
2662 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2663 end;
2667 let x =
2668 match state.birdseye with
2669 | Some (_, x, _, _) -> x
2670 | None -> state.x
2672 adddoc state.path x (yratio state.y) conf
2673 (if conf.savebmarks then state.bookmarks else []);
2675 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2676 if path <> state.path
2677 then
2678 adddoc path x y c bookmarks
2679 ) h;
2680 Buffer.add_string bb "</llppconfig>";
2682 load1 f;
2683 if Buffer.length bb > 0
2684 then
2686 let tmp = path ^ ".tmp" in
2687 let oc = open_out_bin tmp in
2688 Buffer.output_buffer oc bb;
2689 close_out oc;
2690 Sys.rename tmp path;
2691 with exn ->
2692 prerr_endline
2693 ("error while saving configuration: " ^ Printexc.to_string exn)
2695 end;;
2697 let () =
2698 Arg.parse
2699 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2700 (fun s -> state.path <- s)
2701 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2703 let path =
2704 if String.length state.path = 0
2705 then (prerr_endline "filename missing"; exit 1)
2706 else (
2707 if Filename.is_relative state.path
2708 then Filename.concat (Sys.getcwd ()) state.path
2709 else state.path
2712 state.path <- path;
2714 State.load ();
2716 let _ = Glut.init Sys.argv in
2717 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2718 let () = Glut.initWindowSize conf.winw conf.winh in
2719 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2721 let csock, ssock =
2722 if Sys.os_type = "Unix"
2723 then
2724 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2725 else
2726 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2727 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2728 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2729 Unix.bind sock addr;
2730 Unix.listen sock 1;
2731 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2732 Unix.connect csock addr;
2733 let ssock, _ = Unix.accept sock in
2734 Unix.close sock;
2735 let opts sock =
2736 Unix.setsockopt sock Unix.TCP_NODELAY true;
2737 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2739 opts ssock;
2740 opts csock;
2741 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2742 ssock, csock
2745 let () = Glut.displayFunc display in
2746 let () = Glut.reshapeFunc reshape in
2747 let () = Glut.keyboardFunc keyboard in
2748 let () = Glut.specialFunc special in
2749 let () = Glut.idleFunc (Some idle) in
2750 let () = Glut.mouseFunc mouse in
2751 let () = Glut.motionFunc motion in
2752 let () = Glut.passiveMotionFunc pmotion in
2754 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2755 state.csock <- csock;
2756 state.ssock <- ssock;
2757 state.text <- "Opening " ^ path;
2758 writeopen state.path state.password;
2760 at_exit State.save;
2762 let rec handlelablglutbug () =
2764 Glut.mainLoop ();
2765 with Glut.BadEnum "key in special_of_int" ->
2766 showtext '!' " LablGlut bug: special key not recognized";
2767 handlelablglutbug ()
2769 handlelablglutbug ();