4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let log fmt
= Printf.kprintf prerr_endline fmt
;;
9 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
11 external init
: Unix.file_descr
-> unit = "ml_init";;
12 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
13 external seltext
: string -> (int * int * int * int) -> int -> unit =
15 external copysel
: string -> unit = "ml_copysel";;
16 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
17 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
18 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
22 | Msel
of (mpos
* mpos
)
36 type textentry
= (char
* string * onhist
* onkey
* ondone
)
37 and onkey
= string -> int -> te
38 and ondone
= string -> unit
39 and onhist
= (histcmd
-> string) option
40 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
| HCcancel
45 | TEswitch
of textentry
49 { store
= Array.create n v
56 let cbcap b
= Array.length b
.store
;;
61 b
.wc
<- (b
.wc
+ 1) mod cap;
63 b
.len
<- min
(b
.len
+ 1) cap;
66 let cbempty b
= b
.len
= 0;;
72 let rc = b
.rc + dir
in
73 let rc = max
0 (min
rc (b
.len
-1)) in
79 let rc = b
.wc
- b
.len
in
80 let rc = if rc < 0 then cbcap b
+ rc else rc in
84 let cbdecr b
= b
.len
<- b
.len
- 1;;
99 { mutable scrollw
: int
100 ; mutable scrollh
: int
101 ; mutable icase
: bool
102 ; mutable preload
: bool
103 ; mutable pagebias
: int
104 ; mutable verbose
: bool
105 ; mutable scrollincr
: int
106 ; mutable maxhfit
: bool
107 ; mutable crophack
: bool
108 ; mutable autoscroll
: bool
109 ; mutable showall
: bool
110 ; mutable hlinks
: bool
111 ; mutable underinfo
: bool
112 ; mutable interpagespace
: int
113 ; mutable zoom
: float
114 ; mutable presentation
: bool
115 ; mutable angle
: int
118 ; mutable savebmarks
: bool
119 ; mutable proportional
: bool
120 ; mutable memlimit
: int
129 and proportional
= bool
135 type outline
= string * int * int * float;;
137 | Oarray
of outline array
138 | Olist
of outline list
139 | Onarrow
of string * outline array
* outline array
142 type rect
= (float * float * float * float * float * float * float * float);;
145 { mutable csock
: Unix.file_descr
146 ; mutable ssock
: Unix.file_descr
152 ; mutable layout
: layout list
154 ((pageno
* width
* angle
* proportional
), (opaque
* pixmapsize
)) Hashtbl.t
155 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
156 ; mutable pagecount
: int
157 ; pagecache
: string circbuf
158 ; mutable rendering
: bool
159 ; mutable mstate
: mstate
160 ; mutable searchpattern
: string
161 ; mutable rects
: (pageno
* recttype
* rect
) list
162 ; mutable rects1
: (pageno
* recttype
* rect
) list
163 ; mutable text
: string
164 ; mutable fullscreen
: (width
* height
) option
165 ; mutable textentry
: textentry
option
166 ; mutable outlines
: outlines
167 ; mutable outline
: (bool * int * int * outline array
* string) option
168 ; mutable bookmarks
: outline list
169 ; mutable path
: string
170 ; mutable password
: string
171 ; mutable invalidated
: int
172 ; mutable colorscale
: float
173 ; mutable memused
: int
177 { pat
: string circbuf
178 ; pag
: string circbuf
179 ; nav
: float circbuf
199 ; presentation
= false
204 ; proportional
= true
205 ; memlimit
= 32*1024*1024
209 let conf = { defconf with angle
= defconf.angle
};;
220 ; pagemap
= Hashtbl.create
10
221 ; pagecache
= cbnew 100 ""
232 ; outlines
= Olist
[]
239 { nav
= cbnew 100 0.0
251 Printf.kprintf prerr_endline fmt
253 Printf.kprintf ignore fmt
257 let len = String.length s
in
259 let b = Buffer.create
n in
260 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
261 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
262 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
263 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
264 Buffer.add_string
b s
;
265 let s'
= Buffer.contents
b in
266 let n'
= Unix.write fd
s'
0 n in
267 if n'
!= n then failwith
"write failed";
272 let n = Unix.read fd
s 0 4 in
273 if n != 4 then failwith
"incomplete read(len)";
275 lor (Char.code
s.[0] lsl 24)
276 lor (Char.code
s.[1] lsl 16)
277 lor (Char.code
s.[2] lsl 8)
278 lor (Char.code
s.[3] lsl 0)
280 let s = String.create
len in
281 let n = Unix.read fd
s 0 len in
282 if n != len then failwith
"incomplete read(data)";
289 else float y
/. float state.maxy
293 let b = Buffer.create
10 in
294 Buffer.add_string
b s;
295 let rec combine = function
298 Buffer.add_char
b ' '
;
301 | `
b b -> if b then "1" else "0"
303 | `i i
-> string_of_int i
304 | `f f
-> string_of_float f
305 | `I f
-> string_of_int
(truncate f
)
307 Buffer.add_string
b s;
314 let cmd = Buffer.contents
(makecmd s l
) in
315 writecmd state.csock
cmd;
321 let d = conf.winh
- h
in
328 let rec f pn ph pi fh l
=
330 | (n, _
, h
, _
) :: rest
->
331 let ips = calcips h
in
337 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
346 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
349 let fh = f 0 0 0 0 state.pdims
in
353 let getpageyh pageno
=
354 let rec f pn ph pi y l
=
356 | (n, _
, h
, _
) :: rest
->
357 let ips = calcips h
in
360 if conf.presentation
&& n = pageno
362 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
364 y
+ (pageno
- pn
) * (ph
+ pi
), h
366 let y = y + (if conf.presentation
then pi
else 0) in
367 let y = y + (n - pn
) * (ph
+ pi
) in
371 y + (pageno
- pn
) * (ph
+ pi
), ph
373 f 0 0 0 0 state.pdims
376 let getpagey pageno
= fst
(getpageyh pageno
);;
379 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
380 let ((w
, h
, ips, x
) as curr
), rest
, pdimno
, yinc
=
382 | (pageno'
, w
, h
, x
) :: rest
when pageno'
= pageno
->
383 let ips = calcips h
in
384 let yinc = if conf.presentation
then ips else 0 in
385 (w
, h
, ips, x
), rest
, pdimno
+ 1, yinc
387 prev
, pdims
, pdimno
, 0
389 let dy = dy + yinc in
390 let py = py + yinc in
391 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
396 if py + h
<= vy - yinc
398 let py = py + h
+ ips in
399 let dy = max
0 (py - y) in
409 let pagey = vy - py in
410 let pagevh = h
- pagey in
411 let pagevh = min
(sh
- dy) pagevh in
412 let off = if yinc > 0 then py - vy else 0 in
413 let py = py + h
+ ips in
419 ; pagedispy
= dy + off
420 ; pagey = pagey + off
421 ; pagevh = pagevh - off
425 let accu = e :: accu in
432 ~cacheleft
:(cacheleft
-1)
435 if state.invalidated
= 0
445 ~cacheleft
:(cbcap state.pagecache
)
455 let y = state.y + incr
in
457 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
461 let getopaque pageno
=
462 try Some
(Hashtbl.find
state.pagemap
463 (pageno
+ 1, state.w
, conf.angle
, conf.proportional
))
464 with Not_found
-> None
467 let cache pageno opaque
=
468 Hashtbl.replace
state.pagemap
469 (pageno
+ 1, state.w
, conf.angle
, conf.proportional
) opaque
472 let validopaque opaque
= String.length opaque
> 0;;
475 match getopaque l
.pageno
with
476 | None
when not
state.rendering
->
477 state.rendering
<- true;
478 cache l
.pageno
("", -1);
479 wcmd "render" [`i
(l
.pageno
+ 1)
487 let loadlayout layout =
488 let rec f all
= function
490 begin match getopaque l
.pageno
with
491 | None
-> render l
; f false ls
492 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
496 f (layout <> []) layout;
499 let findpageforopaque opaque
=
501 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
505 let pagevisible n = List.exists
(fun l
-> l
.pageno
+ 1 = n) state.layout;;
511 let opaque = cbpeek state.pagecache
in
512 match findpageforopaque opaque with
513 | Some
((n, _
, _
, _
), size
) ->
514 not
(pagevisible n) && state.memused
- size
<= conf.memlimit
519 let rely = yratio state.y in
520 let presentation = conf.presentation in
521 let interpagespace = conf.interpagespace in
522 let maxy = state.maxy in
523 conf.presentation <- false;
524 conf.interpagespace <- 0;
525 state.maxy <- calcheight ();
526 let y = truncate
(float state.maxy *. rely) in
527 let y = if y < conf.winh
then 0 else y - conf.winh
in
528 let pages = layout y (conf.winh
*3) in
529 List.iter
render pages;
530 conf.presentation <- presentation;
531 conf.interpagespace <- interpagespace;
537 let y = min
state.maxy y in
538 let pages = layout y conf.winh
in
539 let ready = loadlayout pages in
540 state.ty
<- yratio y;
545 state.layout <- pages;
547 Glut.postRedisplay
();
551 state.layout <- pages;
553 Glut.postRedisplay
();
558 let gotoy_and_clear_text y =
560 if not
conf.verbose
then state.text
<- "";
564 cbput state.hists
.nav
(yratio state.y);
568 let y = cbget state.hists
.nav ~
-1 in
569 truncate
(y *. float state.maxy)
573 let y, h
= getpageyh n in
575 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
578 let gotopage1 n top
=
579 let y = getpagey n in
581 gotoy_and_clear_text (y + top
);
589 state.invalidated
<- state.invalidated
+ 1;
593 let c = c *. state.colorscale
in
599 match state.layout with
601 let rely = yratio state.y in
602 state.maxy <- calcheight ();
603 truncate
(float state.maxy *. rely)
606 state.maxy <- calcheight ();
613 GlMat.mode `projection
;
614 GlMat.load_identity
();
615 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
616 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
617 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
621 GlMat.mode `projection
;
622 GlMat.load_identity
();
623 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
624 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
625 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
630 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
634 GlMat.mode `modelview
;
635 GlMat.load_identity
();
636 GlClear.color
(scalecolor 1.0);
637 GlClear.clear
[`color
];
640 wcmd "geometry" [`i
w; `i h
];
644 GlDraw.color
(0.0, 0.0, 0.0);
646 (0.0, float (conf.winh
- 18))
647 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
649 let font = Glut.BITMAP_8_BY_13
in
650 GlDraw.color
(1.0, 1.0, 1.0);
651 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
652 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
653 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
657 let len = String.length
state.text
in
658 match state.textentry
with
660 if len > 0 then showtext ' '
state.text
662 | Some
(c, text
, _
, _
, _
) ->
666 text ^
" [" ^
state.text ^
"]"
676 state.text
<- Printf.sprintf
"%c%s" c s;
677 Glut.postRedisplay
();
691 state.rects
<- state.rects1
;
692 Glut.postRedisplay
()
695 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
696 state.pagecount
<- n;
697 state.invalidated
<- state.invalidated
- 1;
698 if state.invalidated
= 0
702 let s = Scanf.sscanf
cmd "t %n"
703 (fun n -> String.sub
cmd n (String.length
cmd - n))
705 Glut.setWindowTitle
s
708 let s = Scanf.sscanf
cmd "T %n"
709 (fun n -> String.sub
cmd n (String.length
cmd - n))
711 if state.textentry
= None
718 Glut.postRedisplay
();
724 let s = Scanf.sscanf
cmd "V %n"
725 (fun n -> String.sub
cmd n (String.length
cmd - n))
731 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
732 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
733 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
734 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
736 let y = (getpagey pageno) + truncate y0
in
739 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
742 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
743 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
744 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
745 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
748 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
751 let n, w, h
, r
, l
, s, p
=
752 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
753 (fun n w h r l
s p
-> (n, w, h
, r
, l
!= 0, s, p
))
756 Hashtbl.replace
state.pagemap
(n, w, r
, l
) (p
, s);
757 state.memused
<- state.memused
+ s;
760 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
763 let evictedopaque = cbpeek state.pagecache
in
764 match findpageforopaque evictedopaque with
765 | None
-> failwith
"bug in gc"
766 | Some
((evictedn
, _
, _
, _
) as k
, evictedsize
) ->
767 if not
(pagevisible evictedn
)
769 wcmd "free" [`
s evictedopaque];
770 state.memused
<- state.memused
- evictedsize
;
771 Hashtbl.remove
state.pagemap k
;
772 cbdecr state.pagecache
;
779 cbput state.pagecache p
;
780 state.rendering
<- false;
783 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy)))
787 else (ignore
(loadlayout state.layout); preload ())
791 let (n, w, h
, x
) as pdim
=
792 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h
, x
)
794 state.pdims
<- pdim
:: state.pdims
797 let (l
, n, t
, h
, pos
) =
798 Scanf.sscanf
cmd "o %u %u %u %u %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
800 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
802 let l = String.length
s in
803 let b = Buffer.create
(String.length
s) in
810 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
813 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
822 let outline = (s, l, n, float t
/. float h
) in
824 match state.outlines with
825 | Olist
outlines -> Olist
(outline :: outlines)
826 | Oarray _
-> Olist
[outline]
827 | Onarrow _
-> Olist
[outline]
829 state.outlines <- outlines
832 log "unknown cmd `%S'" cmd
835 let now = Unix.gettimeofday
;;
839 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
844 let y = state.y + conf.scrollincr
in
845 let y = if y >= state.maxy then 0 else y in
851 let cmd = readcmd state.csock
in
861 | HCprev
-> cbget cb ~
-1
862 | HCnext
-> cbget cb
1
863 | HCfirst
-> cbget cb ~
-(cb
.rc)
864 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
865 | HCcancel
-> cb
.rc <- rc; cb
.store
.(0) (* ugly *)
868 let search pattern forward
=
869 if String.length pattern
> 0
872 match state.layout with
875 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
878 let b = makecmd "search"
879 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
881 Buffer.add_char
b '
,'
;
882 Buffer.add_string
b pattern
;
883 Buffer.add_char
b '
\000'
;
886 writecmd state.csock
cmd;
889 let intentry text key
=
890 let c = Char.unsafe_chr key
in
893 let s = "x" in s.[0] <- c;
894 let text = text ^
s in
898 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
903 let b = Buffer.create
(String.length
s + 1) in
904 Buffer.add_string
b s;
909 let textentry text key
=
910 let c = Char.unsafe_chr key
in
912 | _
when key
>= 32 && key
< 127 ->
913 let text = addchar text c in
917 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
921 let reinit angle proportional
=
923 conf.proportional
<- proportional
;
925 wcmd "reinit" [`i angle
; `
b proportional
];
928 let optentry text key
=
929 let btos b = if b then "on" else "off" in
930 let c = Char.unsafe_chr key
in
934 try conf.scrollincr
<- int_of_string
s with exc
->
935 state.text <- Printf.sprintf
"bad integer `%s': %s"
936 s (Printexc.to_string exc
)
938 TEswitch
('#'
, "", None
, intentry, ondone)
943 Some
(int_of_string
s)
945 state.text <- Printf.sprintf
"bad integer `%s': %s"
946 s (Printexc.to_string exc
);
949 | Some angle
-> reinit angle
conf.proportional
952 TEswitch
('^'
, "", None
, intentry, ondone)
955 conf.icase
<- not
conf.icase
;
956 TEdone
("case insensitive search " ^
(btos conf.icase
))
959 conf.preload <- not
conf.preload;
961 TEdone
("preload " ^
(btos conf.preload))
964 conf.verbose
<- not
conf.verbose
;
965 TEdone
("verbose " ^
(btos conf.verbose
))
968 conf.maxhfit
<- not
conf.maxhfit
;
969 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
970 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
973 conf.crophack
<- not
conf.crophack
;
974 TEdone
("crophack " ^
btos conf.crophack
)
977 conf.showall
<- not
conf.showall
;
978 TEdone
("showall " ^
btos conf.showall
)
981 conf.underinfo
<- not
conf.underinfo
;
982 TEdone
("underinfo " ^
btos conf.underinfo
)
985 conf.savebmarks
<- not
conf.savebmarks
;
986 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
991 conf.interpagespace <- int_of_string
s;
992 let rely = yratio state.y in
993 state.maxy <- calcheight ();
994 gotoy (truncate
(float state.maxy *. rely));
996 state.text <- Printf.sprintf
"bad integer `%s': %s"
997 s (Printexc.to_string exc
)
999 TEswitch
('
%'
, "", None
, intentry, ondone)
1002 reinit conf.angle
(not
conf.proportional
);
1003 TEdone
("proprortional display " ^
btos conf.proportional
)
1006 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1010 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1012 let enterselector allowdel
outlines errmsg msg
=
1013 if Array.length
outlines = 0
1015 showtext ' ' errmsg
;
1019 Glut.setCursor
Glut.CURSOR_INHERIT
;
1021 match state.layout with
1023 | {pageno=pageno} :: rest
-> pageno
1027 if n = Array.length
outlines
1030 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1031 if outlinepageno
>= pageno then n else loop (n+1)
1036 Some
(allowdel
, active,
1037 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1038 Glut.postRedisplay
();
1042 let enteroutlinemode () =
1044 match state.outlines with
1047 let a = Array.of_list
(List.rev
l) in
1048 state.outlines <- Oarray
a;
1050 | Onarrow
(pat
, a, b) ->
1051 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1053 enterselector false outlines "Document has no outline" msg
;
1056 let enterbookmarkmode () =
1057 let bookmarks = Array.of_list
state.bookmarks in
1058 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1061 let quickbookmark ?title
() =
1062 match state.layout with
1068 let sec = Unix.gettimeofday
() in
1069 let tm = Unix.localtime
sec in
1070 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1074 (tm.Unix.tm_year
+ 1900)
1077 | Some
title -> title
1080 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1084 state.fullscreen
<- None
;
1085 Glut.reshapeWindow
w h
;
1088 let writeopen path password
=
1089 writecmd state.csock
1090 ("open " ^ path ^
"\000"
1091 ^
state.password ^
"\000"
1092 ^ string_of_int
conf.angle ^
" "
1093 ^
(if conf.proportional
then "1" else "0"))
1097 let opendoc path password
=
1100 state.password
<- password
;
1101 Hashtbl.clear
state.pagemap
;
1103 writeopen path password
;
1104 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1105 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1108 let viewkeyboard ~key ~x ~
y =
1110 state.textentry <- te
;
1113 Glut.postRedisplay
()
1115 match state.textentry with
1117 let c = Char.chr key
in
1123 let y = getnav () in
1124 gotoy_and_clear_text y
1132 Glut.postRedisplay
()
1135 let ondone isforw
s =
1136 cbput state.hists
.pat
s;
1137 state.searchpattern
<- s;
1140 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1141 textentry, ondone (c ='
/'
)))
1143 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1144 let incr = if conf.zoom
+. 0.01 > 0.1 then 0.1 else 0.01 in
1145 conf.zoom
<- min
2.2 (conf.zoom
+. incr);
1146 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1147 reshape conf.winw
conf.winh
1152 try int_of_string
s with exc
->
1153 state.text <- Printf.sprintf
"bad integer `%s': %s"
1154 s (Printexc.to_string exc
);
1160 state.text <- "page bias is now " ^ string_of_int
n;
1163 enttext (Some
('
+'
, "", None
, intentry, ondone))
1165 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1166 let decr = if conf.zoom
-. 0.1 < 0.1 then 0.01 else 0.1 in
1167 conf.zoom
<- max
0.01 (conf.zoom
-. decr);
1168 if conf.zoom
<= 1.0 then state.x
<- 0;
1169 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1170 reshape conf.winw
conf.winh
;
1176 enttext (Some
('
-'
, "", None
, optentry, ondone))
1178 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1181 state.text <- "zoom is 100%";
1182 reshape conf.winw
conf.winh
1184 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1185 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1190 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1191 reshape conf.winw
conf.winh
;
1197 try int_of_string
s with exc
->
1198 state.text <- Printf.sprintf
"bad integer `%s': %s"
1199 s (Printexc.to_string exc
);
1205 cbput state.hists
.pag
(string_of_int
n);
1206 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1209 let pageentry text key
=
1210 match Char.unsafe_chr key
with
1211 | 'g'
-> TEdone
text
1212 | _
-> intentry text key
1214 let text = "x" in text.[0] <- c;
1215 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1219 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1220 reshape conf.winw
conf.winh
;
1223 conf.hlinks
<- not
conf.hlinks
;
1224 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1225 Glut.postRedisplay
()
1228 conf.autoscroll
<- not
conf.autoscroll
1231 conf.presentation <- not
conf.presentation;
1232 showtext ' '
("presentation mode " ^
1233 if conf.presentation then "on" else "off");
1237 begin match state.fullscreen
with
1239 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1242 state.fullscreen
<- None
;
1247 gotoy_and_clear_text 0
1250 search state.searchpattern
true
1253 search state.searchpattern
false
1256 begin match state.layout with
1259 gotoy_and_clear_text (getpagey l.pageno)
1263 begin match List.rev
state.layout with
1266 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1267 gotoy_and_clear_text (getpagey pageno)
1271 begin match state.layout with
1274 let pageno = max
0 (l.pageno-1) in
1275 gotoy_and_clear_text (getpagey pageno)
1280 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1282 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1284 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1288 else (100. *. (float state.y /. float maxy)) in
1291 Printf.sprintf
"Page %d of %d %.2f%%"
1292 (fn+1) state.pagecount
percent
1295 "Pages %d-%d of %d %.2f%%"
1296 (fn+1) (ln
+1) state.pagecount
percent
1301 begin match state.layout with
1304 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1305 Glut.postRedisplay
();
1309 enterbookmarkmode ()
1313 match state.layout with
1316 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1320 enttext (Some
('~'
, "", None
, textentry, ondone))
1324 showtext ' '
"Quick bookmark added";
1327 begin match state.layout with
1329 let rect = getpdimrect
l.pagedimno
in
1333 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1334 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1336 (truncate
(rect.(1) -. rect.(0)),
1337 truncate
(rect.(3) -. rect.(0)))
1341 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1343 Glut.postRedisplay
();
1349 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1354 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1355 Glut.postRedisplay
()
1357 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1358 | 'j'
-> gotoy (clamp conf.scrollincr
)
1360 | '
r'
-> opendoc state.path
state.password
1363 vlog "huh? %d %c" key
(Char.chr key
);
1366 | Some
(c, text, opthist
, onkey
, ondone) when key
= 8 ->
1367 let len = String.length
text in
1370 state.textentry <- None
;
1371 Glut.postRedisplay
();
1374 let s = String.sub
text 0 (len - 1) in
1375 enttext (Some
(c, s, opthist
, onkey
, ondone))
1378 | Some
(c, text, onhist, onkey
, ondone) ->
1379 begin match Char.unsafe_chr key
with
1382 state.textentry <- None
;
1383 Glut.postRedisplay
()
1386 begin match onhist with
1388 | Some
onhist -> ignore
(onhist HCcancel
)
1390 state.textentry <- None
;
1391 Glut.postRedisplay
()
1394 begin match onkey
text key
with
1396 state.textentry <- None
;
1398 Glut.postRedisplay
()
1401 enttext (Some
(c, text, onhist, onkey
, ondone));
1404 state.textentry <- None
;
1405 Glut.postRedisplay
()
1408 state.textentry <- Some te
;
1409 Glut.postRedisplay
()
1414 let narrow outlines pattern
=
1415 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1419 let rec fold accu n =
1423 let (s, _
, _
, _
) as o
= outlines.(n) in
1425 if (try ignore
(Str.search_forward re
s 0); true
1426 with Not_found
-> false)
1432 let matched = fold [] (Array.length
outlines - 1) in
1433 if matched = [] then None
else Some
(Array.of_list
matched)
1436 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1437 let search active pattern
incr =
1440 if n = Array.length
outlines || n = -1
1443 let (s, _
, _
, _
) = outlines.(n) in
1445 (try ignore
(Str.search_forward re
s 0); true
1446 with Not_found
-> false)
1448 else loop (n + incr)
1453 let re = Str.regexp_case_fold pattern
in
1459 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1462 if String.length qsearch
= 0
1465 state.outline <- None
;
1466 Glut.postRedisplay
();
1470 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1471 Glut.postRedisplay
();
1475 let incr = if key
= 18 then -1 else 1 in
1477 match search (active + incr) qsearch
incr with
1479 state.text <- qsearch ^
" [not found]";
1482 state.text <- qsearch
;
1483 active, firstof active
1485 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1486 Glut.postRedisplay
();
1489 let len = String.length qsearch
in
1496 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1499 let qsearch = String.sub
qsearch 0 (len - 1) in
1501 match search active qsearch ~
-1 with
1503 state.text <- qsearch ^
" [not found]";
1506 state.text <- qsearch;
1507 active, firstof active
1509 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1511 Glut.postRedisplay
()
1514 if active < Array.length
outlines
1516 let (_
, _
, n, t
) = outlines.(active) in
1520 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1521 state.outline <- None
;
1522 Glut.postRedisplay
();
1524 | _
when key
>= 32 && key
< 127 ->
1525 let pattern = addchar qsearch (Char.chr key
) in
1527 match search active pattern 1 with
1529 state.text <- pattern ^
" [not found]";
1532 state.text <- pattern;
1533 active, firstof active
1535 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1536 Glut.postRedisplay
()
1538 | 14 when not allowdel
-> (* ctrl-n *)
1539 if String.length
qsearch > 0
1541 let optoutlines = narrow outlines qsearch in
1542 begin match optoutlines with
1543 | None
-> state.text <- "can't narrow"
1545 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1546 match state.outlines with
1549 state.outlines <- Onarrow
(qsearch, outlines, a)
1550 | Onarrow
(pat
, a, b) ->
1551 state.outlines <- Onarrow
(qsearch, outlines, b)
1554 Glut.postRedisplay
()
1556 | 21 when not allowdel
-> (* ctrl-u *)
1558 match state.outlines with
1561 let a = Array.of_list
(List.rev
l) in
1562 state.outlines <- Oarray
a;
1564 | Onarrow
(pat
, a, b) ->
1565 state.outlines <- Oarray
b;
1569 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1570 Glut.postRedisplay
()
1574 Some
(allowdel
, active, firstof active, outlines, qsearch);
1575 Glut.postRedisplay
()
1577 | 127 when allowdel
->
1578 let len = Array.length
outlines - 1 in
1581 state.outline <- None
;
1582 state.bookmarks <- [];
1585 let bookmarks = Array.init
len
1587 let i = if i >= active then i + 1 else i in
1598 Glut.postRedisplay
()
1600 | _
-> log "unknown key %d" key
1603 let keyboard ~key ~x ~
y =
1608 match state.outline with
1609 | None
-> viewkeyboard ~key ~x ~
y
1610 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1613 let special ~key ~x ~
y =
1614 match state.outline with
1616 begin match state.textentry with
1620 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1621 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1622 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1623 | Glut.KEY_PAGE_UP
->
1624 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1626 match state.layout with
1628 | l :: _
-> state.y - l.pagey
1631 | Glut.KEY_PAGE_DOWN
->
1632 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1634 match List.rev
state.layout with
1636 | l :: _
-> getpagey l.pageno
1639 | Glut.KEY_HOME
-> addnav (); 0
1642 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1644 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1645 state.x
<- state.x
- 10;
1647 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1648 state.x
<- state.x
+ 10;
1653 gotoy_and_clear_text y
1655 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1658 | Glut.KEY_UP
-> onhist HCprev
1659 | Glut.KEY_DOWN
-> onhist HCnext
1660 | Glut.KEY_HOME
-> onhist HCfirst
1661 | Glut.KEY_END
-> onhist HClast
1664 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1665 Glut.postRedisplay
()
1670 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1671 let maxrows = maxoutlinerows () in
1672 let calcfirst first
active =
1675 let rows = active - first
in
1676 if rows > maxrows then active - maxrows else first
1680 let active = active + incr in
1681 let active = max
0 (min
active (Array.length
outlines - 1)) in
1682 let first = calcfirst first active in
1683 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1684 Glut.postRedisplay
()
1686 let updownlevel incr =
1687 let len = Array.length
outlines in
1688 let (_
, curlevel
, _
, _
) = outlines.(active) in
1690 if i = len then i-1 else if i = -1 then 0 else
1691 let (_
, l, _
, _
) = outlines.(i) in
1692 if l != curlevel
then i else flow (i+incr)
1694 let active = flow active in
1695 let first = calcfirst first active in
1696 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1697 Glut.postRedisplay
()
1700 | Glut.KEY_UP
-> navigate ~
-1
1701 | Glut.KEY_DOWN
-> navigate 1
1702 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1703 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1705 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1706 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1709 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1710 Glut.postRedisplay
()
1713 let active = Array.length
outlines - 1 in
1714 let first = max
0 (active - maxrows) in
1715 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1716 Glut.postRedisplay
()
1721 let drawplaceholder l =
1722 GlDraw.color
(scalecolor 1.0);
1724 (float l.pagex
, float l.pagedispy
)
1725 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1727 let x = float l.pagex
1728 and y = float (l.pagedispy
+ 13) in
1729 let font = Glut.BITMAP_8_BY_13
in
1730 GlDraw.color
(0.0, 0.0, 0.0);
1731 GlPix.raster_pos ~
x ~
y ();
1732 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1733 ("Loading " ^ string_of_int
(l.pageno + 1));
1736 let now () = Unix.gettimeofday
();;
1739 begin match getopaque l.pageno with
1740 | Some
(opaque, _
) when validopaque opaque ->
1741 if state.textentry = None
1742 then GlDraw.color
(scalecolor 1.0)
1743 else GlDraw.color
(scalecolor 0.4);
1745 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1749 vlog "draw %d %f sec" l.pageno d;
1754 l.pagedispy
+ l.pagevh;
1758 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1759 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1760 let sh = float conf.winh
/. sh in
1761 let sh = max
sh (float conf.scrollh
) in
1764 if state.y = state.maxy
1766 else float y /. float maxy
1768 let position = (float conf.winh
-. sh) *. percent in
1771 if position +. sh > float conf.winh
1772 then float conf.winh
-. sh
1778 let scrollindicator () =
1779 GlDraw.color
(0.64 , 0.64, 0.64);
1781 (float (conf.winw
- conf.scrollw
), 0.)
1782 (float conf.winw
, float conf.winh
)
1784 GlDraw.color
(0.0, 0.0, 0.0);
1786 let position, sh = scrollph state.y in
1788 (float (conf.winw
- conf.scrollw
), position)
1789 (float conf.winw
, position +. sh)
1793 let showsel margin
=
1794 match state.mstate
with
1795 | Mnone
| Mscroll _
| Mpan _
->
1798 | Msel
((x0
, y0
), (x1
, y1
)) ->
1799 let rec loop = function
1801 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1802 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1804 match getopaque l.pageno with
1805 | Some
(opaque, _
) when validopaque opaque ->
1806 let oy = -l.pagey + l.pagedispy
in
1808 (x0
- margin
- state.x, y0
,
1809 x1
- margin
- state.x, y1
) oy;
1819 let panx = float state.x in
1821 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1822 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1824 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1826 if l.pageno = pageno
1828 let d = float (l.pagedispy
- l.pagey) in
1829 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1830 GlDraw.begins `quads
;
1832 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1833 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1834 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1835 GlDraw.vertex2
(x3
+.panx, y3
+.d);
1845 let showoutline = function
1847 | Some
(allowdel
, active, first, outlines, qsearch) ->
1849 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1850 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1851 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
1854 GlDraw.color
(1., 1., 1.);
1855 let font = Glut.BITMAP_9_BY_15
in
1856 let draw_string x y s =
1857 GlPix.raster_pos ~
x ~
y ();
1858 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1861 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
1864 let (s, l, _
, _
) = outlines.(row
) in
1865 let y = (row
- first) * 16 in
1870 GlDraw.polygon_mode `both `line
;
1871 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1872 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1873 GlDraw.rect (0., float (y + 1))
1874 (float (conf.winw
- 1), float (y + 18));
1875 GlDraw.polygon_mode `both `fill
;
1877 GlDraw.color
(1., 1., 1.);
1879 draw_string (float x) (float (y + 16)) s;
1887 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1888 GlDraw.viewport
margin 0 state.w conf.winh
;
1890 GlClear.color
(scalecolor 0.5);
1891 GlClear.clear
[`color
];
1894 let x = float state.x in
1895 GlMat.translate ~
x ();
1899 Gl.enable `scissor_test
;
1900 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
1902 let _lasty = List.fold_left
drawpage 0 (state.layout) in
1905 Gl.disable `scissor_test
1909 let x = -.float state.x in
1910 GlMat.translate ~
x ();
1914 GlDraw.viewport
0 0 conf.winw
conf.winh
;
1917 showoutline state.outline;
1919 Glut.swapBuffers
();
1923 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1924 let x = x - margin - state.x in
1925 let rec f = function
1927 begin match getopaque l.pageno with
1928 | Some
(opaque, _
) when validopaque opaque ->
1929 let y = y - l.pagedispy
in
1932 let y = l.pagey + y in
1933 let x = x - l.pagex
in
1934 match whatsunder
opaque x y with
1947 let mouse ~button ~bstate ~
x ~
y =
1949 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1957 let incr = incr * 2 in
1958 let y = clamp incr in
1959 gotoy_and_clear_text y
1961 | Glut.LEFT_BUTTON
when state.outline = None
1962 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1963 if bstate
= Glut.DOWN
1965 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1966 state.mstate
<- Mpan
(x, y)
1969 state.mstate
<- Mnone
1972 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
1973 if bstate
= Glut.DOWN
1975 let position, sh = scrollph state.y in
1976 if y > truncate
position && y < truncate
(position +. sh)
1978 state.mstate
<- Mscroll
1980 let percent = float y /. float conf.winh
in
1981 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
1983 state.mstate
<- Mscroll
1985 state.mstate
<- Mnone
1987 | Glut.LEFT_BUTTON
when state.outline = None
->
1988 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1989 begin match dest with
1990 | Ulinkgoto
(pageno, top
) ->
1993 gotopage1 pageno top
1998 | Unone
when bstate
= Glut.DOWN
->
1999 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2000 state.mstate
<- Mpan
(x, y);
2002 | Unone
| Utext _
->
2003 if bstate
= Glut.DOWN
2005 if conf.angle
mod 360 = 0
2007 state.mstate
<- Msel
((x, y), (x, y));
2008 Glut.postRedisplay
()
2012 match state.mstate
with
2016 state.mstate
<- Mnone
2019 Glut.setCursor
Glut.CURSOR_INHERIT
;
2020 state.mstate
<- Mnone
2022 | Msel
((x0
, y0
), (x1
, y1
)) ->
2024 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2025 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2027 match getopaque l.pageno with
2028 | Some
(opaque, _
) when validopaque opaque ->
2032 List.iter
f state.layout;
2033 copysel
""; (* ugly *)
2034 Glut.setCursor
Glut.CURSOR_INHERIT
;
2035 state.mstate
<- Mnone
;
2042 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2045 if state.outline = None
2047 match state.mstate
with
2053 state.mstate
<- Mpan
(x, y);
2054 if conf.zoom > 1.0 then state.x <- state.x + dx;
2056 gotoy_and_clear_text y
2059 state.mstate
<- Msel
(a, (x, y));
2060 Glut.postRedisplay
()
2063 let y = min
conf.winh
(max
0 y) in
2064 let percent = float y /. float conf.winh
in
2065 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2066 gotoy_and_clear_text y
2070 if state.outline = None
2072 match state.mstate
with
2074 begin match getunder x y with
2075 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2077 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2078 Glut.setCursor
Glut.CURSOR_INFO
2079 | Ulinkgoto
(page
, y) ->
2080 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
2081 Glut.setCursor
Glut.CURSOR_INFO
2083 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2084 Glut.setCursor
Glut.CURSOR_TEXT
2087 | Mpan _
| Msel _
| Mscroll
->
2097 match Sys.os_type
with
2098 | "Win32" -> Sys.getenv
"HOMEPATH"
2099 | _
-> Sys.getenv
"HOME"
2102 ("Can not determine home directory location: " ^
2103 Printexc.to_string exn
);
2107 let config_of c attrs
=
2111 | "scroll-bar-width" -> { c with scrollw
= int_of_string v
}
2112 | "scroll-handle-height" -> { c with scrollh
= int_of_string v
}
2113 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2114 | "preload" -> { c with preload = bool_of_string v
}
2115 | "page-bias" -> { c with pagebias
= int_of_string v
}
2116 | "scroll-step" -> { c with scrollincr
= int_of_string v
}
2117 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2118 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2119 | "throttle" -> { c with showall
= bool_of_string v
}
2120 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2121 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2122 | "vertical-margin" -> { c with interpagespace = int_of_string v
}
2124 let zoom = float_of_string v
/. 100. in
2125 let zoom = max
0.01 (min
2.2 zoom) in
2126 { c with zoom = zoom }
2127 | "presentation" -> { c with presentation = bool_of_string v
}
2128 | "rotation-angle" -> { c with angle
= int_of_string v
}
2129 | "width" -> { c with winw
= int_of_string v
}
2130 | "height" -> { c with winh
= int_of_string v
}
2131 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2132 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2133 | "pixmap-cache-size" -> { c with memlimit
= int_of_string v
}
2136 prerr_endline
("Error processing attribute (`" ^
2137 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2140 let rec fold c = function
2143 let c = apply c k v
in
2149 let bookmark_of attrs
=
2150 let rec fold title page
rely = function
2151 | ("title", v
) :: rest
-> fold v page
rely rest
2152 | ("page", v
) :: rest
-> fold title v
rely rest
2153 | ("rely", v
) :: rest
-> fold title page v rest
2154 | _
:: rest
-> fold title page
rely rest
2155 | [] -> title, page
, rely
2157 fold "invalid" "0" "0" attrs
2160 let setconf dst src
=
2161 dst
.scrollw
<- src
.scrollw
;
2162 dst
.scrollh
<- src
.scrollh
;
2163 dst
.icase
<- src
.icase
;
2164 dst
.preload <- src
.preload;
2165 dst
.pagebias
<- src
.pagebias
;
2166 dst
.verbose
<- src
.verbose
;
2167 dst
.scrollincr
<- src
.scrollincr
;
2168 dst
.maxhfit
<- src
.maxhfit
;
2169 dst
.crophack
<- src
.crophack
;
2170 dst
.autoscroll
<- src
.autoscroll
;
2171 dst
.showall
<- src
.showall
;
2172 dst
.hlinks
<- src
.hlinks
;
2173 dst
.underinfo
<- src
.underinfo
;
2174 dst
.interpagespace <- src
.interpagespace;
2175 dst
.zoom <- src
.zoom;
2176 dst
.presentation <- src
.presentation;
2177 dst
.angle
<- src
.angle
;
2178 dst
.winw
<- src
.winw
;
2179 dst
.winh
<- src
.winh
;
2180 dst
.savebmarks
<- src
.savebmarks
;
2181 dst
.memlimit
<- src
.memlimit
;
2182 dst
.proportional
<- src
.proportional
;
2186 let l = String.length
s in
2187 let b = Buffer.create
l in
2193 let h = Hashtbl.create
10 in
2194 let dc = { defconf with angle
= defconf.angle
} in
2195 let rec toplevel v t spos epos
=
2197 | Vdata
| Vcdata
| Vend
-> v
2198 | Vopen
("llppconfig", attrs
, closed
) ->
2201 else { v
with f = llppconfig
}
2203 error
"unexpected subelement at top level" s spos
2204 | Vclose tag
-> error
"unexpected close at top level" s spos
2206 and llppconfig v t spos epos
=
2208 | Vdata
| Vcdata
| Vend
-> v
2209 | Vopen
("defaults", attrs
, closed
) ->
2210 let c = config_of dc attrs
in
2214 else { v
with f = skip
"defaults" (fun () -> v
) }
2216 | Vopen
("doc", attrs
, closed
) ->
2219 List.assoc
"path" attrs
2220 with Not_found
-> error
"doc is missing path attribute" s spos
2222 let path = unent pathent in
2223 let c = config_of dc attrs
in
2226 float_of_string
(List.assoc
"rely" attrs
)
2230 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2235 int_of_string
(List.assoc
"pan" attrs
)
2239 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2243 then (Hashtbl.add
h path (c, [], x, y); v
)
2244 else { v
with f = doc
path x y c [] }
2246 | Vopen
(tag
, _
, closed
) ->
2247 error
"unexpected subelement in llppconfig" s spos
2249 | Vclose
"llppconfig" -> { v
with f = toplevel }
2250 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2252 and doc
path x y c bookmarks v t spos epos
=
2254 | Vdata
| Vcdata
-> v
2255 | Vend
-> error
"unexpected end of input in doc" s spos
2256 | Vopen
("bookmarks", attrs
, closed
) ->
2257 { v
with f = pbookmarks
path x y c bookmarks }
2259 | Vopen
(tag
, _
, _
) ->
2260 error
"unexpected subelement in doc" s spos
2263 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2264 { v
with f = llppconfig
}
2266 | Vclose tag
-> error
"unexpected close in doc" s spos
2268 and pbookmarks
path x y c bookmarks v t spos epos
=
2270 | Vdata
| Vcdata
-> v
2271 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2272 | Vopen
("item", attrs
, closed
) ->
2273 let titleent, spage
, srely
= bookmark_of attrs
in
2278 dolog "Failed to convert page %S to integer: %s"
2279 spage
(Printexc.to_string exn
);
2284 float_of_string srely
2286 dolog "Failed to convert rely %S to real: %s"
2287 srely
(Printexc.to_string exn
);
2290 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2292 then { v
with f = pbookmarks
path x y c bookmarks }
2295 { v
with f = skip
"item" f }
2298 error
"unexpected subelement in bookmarks" s spos
2300 | Vclose
"bookmarks" ->
2301 { v
with f = doc
path x y c bookmarks }
2303 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2305 and skip tag
f v t spos epos
=
2307 | Vdata
| Vcdata
-> v
2309 error
("unexpected end of input in skipped " ^ tag
) s spos
2310 | Vopen
(tag'
, _
, closed
) ->
2314 let f'
() = { v
with f = skip tag
f } in
2315 { v
with f = skip tag'
f'
}
2319 else error
("unexpected close in skipped " ^ tag
) s spos
2322 parse
{ f = toplevel; accu = () } s;
2328 let len = in_channel_length ic
in
2329 let s = String.create
len in
2330 really_input ic
s 0 len;
2333 | Parse_error
(msg
, s, pos
) ->
2334 let subs = subs s pos
in
2335 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2336 failwith
("parse error: " ^
s)
2339 failwith
("config load error: " ^
Printexc.to_string exn
)
2345 let dir = Filename.concat
home ".config" in
2346 if Sys.is_directory
dir then dir else home
2349 Filename.concat
dir "llpp.conf"
2353 if Sys.file_exists
path
2356 (try Some
(open_in_bin
path)
2359 ("Error opening configuation file `" ^
path ^
"': " ^
2360 Printexc.to_string exn
);
2369 ("Error loading configuation from `" ^
path ^
"': " ^
2370 Printexc.to_string exn
);
2376 f (Hashtbl.create
0, defconf)
2381 let pc, pb
, px
, py =
2383 Hashtbl.find
h state.path
2384 with Not_found
-> dc, [], 0, 0.0
2388 state.bookmarks <- pb
;
2390 cbput state.hists
.nav
py;
2395 let add_attrs bb always
dc c =
2398 then Printf.bprintf bb
"\n %s='%b'" s a
2401 then Printf.bprintf bb
"\n %s='%d'" s a
2404 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2408 then dc.winw
, dc.winh
2410 match state.fullscreen
with
2412 | None
-> c.winw
, c.winh
2414 oi
"width" w dc.winw
;
2415 oi
"height" h dc.winh
;
2416 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2417 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2418 ob "case-insensitive-search" c.icase
dc.icase
;
2419 ob "preload" c.preload dc.preload;
2420 oi
"page-bias" c.pagebias
dc.pagebias
;
2421 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2422 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2423 ob "crop-hack" c.crophack
dc.crophack
;
2424 ob "throttle" c.showall
dc.showall
;
2425 ob "highlight-links" c.hlinks
dc.hlinks
;
2426 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2427 oi
"vertical-margin" c.interpagespace dc.interpagespace;
2428 oz
"zoom" c.zoom dc.zoom;
2429 ob "presentation" c.presentation dc.presentation;
2430 oi
"rotation-angle" c.angle
dc.angle
;
2431 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2432 ob "proportional-display" c.proportional
dc.proportional
;
2433 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2437 let bb = Buffer.create
32768 in
2439 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2440 add_attrs bb true dc dc;
2441 Buffer.add_string
bb "/>\n";
2443 let adddoc path x y c bookmarks =
2444 if bookmarks == [] && c = dc && y = 0.0
2447 Printf.bprintf
bb "<doc path='%s'"
2448 (enent
path 0 (String.length
path));
2451 then Printf.bprintf
bb " rely='%f'" y;
2454 then Printf.bprintf
bb " pan='%d'" x;
2456 add_attrs bb false dc c;
2458 begin match bookmarks with
2459 | [] -> Buffer.add_string
bb "/>\n"
2461 Buffer.add_string
bb ">\n<bookmarks>\n";
2462 List.iter
(fun (title, _level
, page, rely) ->
2464 "<item title='%s' page='%d' rely='%f'/>\n"
2465 (enent
title 0 (String.length
title))
2469 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2474 adddoc state.path state.x (yratio state.y) conf
2475 (if conf.savebmarks
then state.bookmarks else []);
2477 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2478 if path <> state.path
2480 adddoc path x y c bookmarks
2482 Buffer.add_string
bb "</llppconfig>";
2485 if Buffer.length
bb > 0
2488 let tmp = path ^
".tmp" in
2489 let oc = open_out_bin
tmp in
2490 Buffer.output_buffer
oc bb;
2492 Sys.rename
tmp path;
2495 ("error while saving configuration: " ^
Printexc.to_string exn
)
2501 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2502 (fun s -> state.path <- s)
2503 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2506 if String.length
state.path = 0
2507 then (prerr_endline
"filename missing"; exit
1)
2509 if Filename.is_relative
state.path
2510 then Filename.concat
(Sys.getcwd
()) state.path
2518 let _ = Glut.init
Sys.argv
in
2519 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2520 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2521 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2524 if Sys.os_type
= "Unix"
2526 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2528 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2529 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2530 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2531 Unix.bind
sock addr;
2533 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2534 Unix.connect
csock addr;
2535 let ssock, _ = Unix.accept
sock in
2538 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2539 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2543 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2547 let () = Glut.displayFunc
display in
2548 let () = Glut.reshapeFunc
reshape in
2549 let () = Glut.keyboardFunc
keyboard in
2550 let () = Glut.specialFunc
special in
2551 let () = Glut.idleFunc
(Some
idle) in
2552 let () = Glut.mouseFunc
mouse in
2553 let () = Glut.motionFunc
motion in
2554 let () = Glut.passiveMotionFunc
pmotion in
2557 state.csock <- csock;
2558 state.ssock <- ssock;
2559 state.text <- "Opening " ^
path;
2560 writeopen state.path state.password
;
2564 let rec handlelablglutbug () =
2567 with Glut.BadEnum
"key in special_of_int" ->
2568 showtext '
!'
" LablGlut bug: special key not recognized";
2569 handlelablglutbug ()
2571 handlelablglutbug ();