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 type params
= angle
* proportional
* texcount
* sliceheight
20 and proportional
= bool
21 and interpagespace
= int
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 =
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";;
39 | Msel
of (mpos
* mpos
)
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
63 | TEswitch
of textentry
67 { store
= Array.create n v
74 let cbcap b
= Array.length b
.store
;;
79 b
.wc
<- (b
.wc
+ 1) mod cap;
81 b
.len
<- min
(b
.len
+ 1) cap;
84 let cbempty b
= b
.len
= 0;;
86 let cbgetg b circular dir
=
90 let rc = b
.rc + dir
in
102 else max
0 (min
rc (b
.len
-1))
108 let cbget b
= cbgetg b
false;;
109 let cbgetc b
= cbgetg b
true;;
112 let rc = b
.wc
- b
.len
in
113 let rc = if rc < 0 then cbcap b
+ rc else rc in
117 let cbdecr b
= b
.len
<- b
.len
- 1;;
132 { mutable scrollw
: int
133 ; mutable scrollh
: int
134 ; mutable icase
: bool
135 ; mutable preload
: bool
136 ; mutable pagebias
: int
137 ; mutable verbose
: bool
138 ; mutable scrollincr
: int
139 ; mutable maxhfit
: bool
140 ; mutable crophack
: bool
141 ; mutable autoscroll
: bool
142 ; mutable showall
: bool
143 ; mutable hlinks
: bool
144 ; mutable underinfo
: bool
145 ; mutable interpagespace
: interpagespace
146 ; mutable zoom
: float
147 ; mutable presentation
: bool
148 ; mutable angle
: angle
151 ; mutable savebmarks
: bool
152 ; mutable proportional
: proportional
153 ; mutable memlimit
: int
154 ; mutable texcount
: texcount
155 ; mutable sliceheight
: sliceheight
156 ; mutable thumbw
: width
160 type outline
= string * int * int * float;;
162 | Oarray
of outline array
163 | Olist
of outline list
164 | Onarrow
of string * outline array
* outline array
167 type rect
= (float * float * float * float * float * float * float * float);;
169 type pagemapkey
= (pageno
* width
* angle
* proportional
* gen
);;
171 type anchor
= pageno
* top
;;
174 { mutable csock
: Unix.file_descr
175 ; mutable ssock
: Unix.file_descr
179 ; mutable anchor
: anchor
181 ; mutable layout
: layout list
182 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
183 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
184 ; mutable pagecount
: int
185 ; pagecache
: string circbuf
186 ; mutable rendering
: bool
187 ; mutable mstate
: mstate
188 ; mutable searchpattern
: string
189 ; mutable rects
: (pageno
* recttype
* rect
) list
190 ; mutable rects1
: (pageno
* recttype
* rect
) list
191 ; mutable text
: string
192 ; mutable fullscreen
: (width
* height
) option
193 ; mutable birdseye
: (conf
* leftx
* pageno
* pageno
) option
194 ; mutable textentry
: textentry
option
195 ; mutable outlines
: outlines
196 ; mutable outline
: (bool * int * int * outline array
* string) option
197 ; mutable bookmarks
: outline list
198 ; mutable path
: string
199 ; mutable password
: string
200 ; mutable invalidated
: int
201 ; mutable colorscale
: float
202 ; mutable memused
: int
204 ; mutable throttle
: layout list
option
208 { pat
: string circbuf
209 ; pag
: string circbuf
210 ; nav
: float circbuf
230 ; presentation
= false
235 ; proportional
= true
236 ; memlimit
= 32*1024*1024
243 let conf = { defconf with angle
= defconf.angle
};;
254 ; pagemap
= Hashtbl.create
10
255 ; pagecache
= cbnew 100 ""
267 ; outlines
= Olist
[]
274 { nav
= cbnew 100 0.0
288 Printf.kprintf prerr_endline fmt
290 Printf.kprintf ignore fmt
294 let len = String.length s
in
296 let b = Buffer.create
n in
297 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
298 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
299 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
300 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
301 Buffer.add_string
b s
;
302 let s'
= Buffer.contents
b in
303 let n'
= Unix.write fd
s'
0 n in
304 if n'
!= n then failwith
"write failed";
309 let n = Unix.read fd
s 0 4 in
310 if n != 4 then failwith
"incomplete read(len)";
312 lor (Char.code
s.[0] lsl 24)
313 lor (Char.code
s.[1] lsl 16)
314 lor (Char.code
s.[2] lsl 8)
315 lor (Char.code
s.[3] lsl 0)
317 let s = String.create
len in
318 let n = Unix.read fd
s 0 len in
319 if n != len then failwith
"incomplete read(data)";
326 else float y
/. float state.maxy
330 let b = Buffer.create
10 in
331 Buffer.add_string
b s;
332 let rec combine = function
335 Buffer.add_char
b ' '
;
338 | `
b b -> if b then "1" else "0"
340 | `i i
-> string_of_int i
341 | `f f
-> string_of_float f
342 | `I f
-> string_of_int
(truncate f
)
344 Buffer.add_string
b s;
351 let cmd = Buffer.contents
(makecmd s l
) in
352 writecmd state.csock
cmd;
358 let d = conf.winh
- h
in
365 let rec f pn ph pi fh l
=
367 | (n, _
, h
, _
) :: rest
->
368 let ips = calcips h
in
373 if state.birdseye
<> None
&& pn
= 0
378 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
383 if conf.presentation
|| (state.birdseye
<> None
&& pn
= 0)
387 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
390 let fh = f 0 0 0 0 state.pdims
in
394 let getpageyh pageno
=
395 let rec f pn ph pi y l
=
397 | (n, _
, h
, _
) :: rest
->
398 let ips = calcips h
in
401 if conf.presentation
&& n = pageno
403 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
405 y
+ (pageno
- pn
) * (ph
+ pi
), h
407 let y = y + (if conf.presentation
then pi
else 0) in
408 let y = y + (n - pn
) * (ph
+ pi
) in
412 y + (pageno
- pn
) * (ph
+ pi
), ph
414 f 0 0 0 0 state.pdims
417 let getpagey pageno
= fst
(getpageyh pageno
);;
420 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
421 let ((w
, h
, ips, x
) as curr
), rest
, pdimno
, yinc
=
423 | (pageno'
, w
, h
, x
) :: rest
when pageno'
= pageno
->
424 let ips = calcips h
in
426 if conf.presentation
|| (state.birdseye
<> None
&& pageno
= 0)
430 (w
, h
, ips, x
), rest
, pdimno
+ 1, yinc
432 prev
, pdims
, pdimno
, 0
434 let dy = dy + yinc in
435 let py = py + yinc in
436 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
441 if py + h
<= vy - yinc
443 let py = py + h
+ ips in
444 let dy = max
0 (py - y) in
454 let pagey = vy - py in
455 let pagevh = h
- pagey in
456 let pagevh = min
(sh
- dy) pagevh in
457 let off = if yinc > 0 then py - vy else 0 in
458 let py = py + h
+ ips in
464 ; pagedispy
= dy + off
465 ; pagey = pagey + off
466 ; pagevh = pagevh - off
470 let accu = e :: accu in
477 ~cacheleft
:(cacheleft
-1)
480 if state.invalidated
= 0
490 ~cacheleft
:(cbcap state.pagecache
)
500 let y = state.y + incr
in
502 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
506 let getopaque pageno
=
507 try Some
(Hashtbl.find
state.pagemap
508 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
509 with Not_found
-> None
512 let cache pageno opaque
=
513 Hashtbl.replace
state.pagemap
514 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
517 let validopaque opaque
= String.length opaque
> 0;;
520 match getopaque l
.pageno
with
521 | None
when not
state.rendering
->
522 state.rendering
<- true;
523 cache l
.pageno
("", -1);
524 wcmd "render" [`i
(l
.pageno
+ 1)
532 let loadlayout layout =
533 let rec f all
= function
535 begin match getopaque l
.pageno
with
536 | None
-> render l
; f false ls
537 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
541 f (layout <> []) layout;
544 let findpageforopaque opaque
=
546 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
550 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
556 let memleft = conf.memlimit
- state.memused
in
559 let opaque = cbpeek state.pagecache
in
560 match findpageforopaque opaque with
561 | Some
((n, _
, _
, _
, _
), size
) ->
562 memleft + size
>= 0 && not
(pagevisible state.layout n)
569 let presentation = conf.presentation in
570 let interpagespace = conf.interpagespace in
571 let maxy = state.maxy in
572 conf.presentation <- false;
573 conf.interpagespace <- 0;
574 state.maxy <- calcheight ();
576 match state.layout with
578 | l
:: _
-> getpagey l
.pageno
580 let y = if y < conf.winh
then 0 else y - conf.winh
in
581 let pages = layout y (conf.winh
*3) in
582 List.iter
render pages;
583 conf.presentation <- presentation;
584 conf.interpagespace <- interpagespace;
590 let y = min
state.maxy y in
591 let pages = layout y conf.winh
in
592 let ready = loadlayout pages in
598 state.layout <- pages;
599 state.throttle
<- None
;
600 Glut.postRedisplay
();
603 state.throttle
<- Some
pages;
608 state.layout <- pages;
609 state.throttle
<- None
;
610 Glut.postRedisplay
();
612 begin match state.birdseye
with
613 | Some
(conf, leftx
, pageno
, hooverpageno
) ->
614 if not
(pagevisible pages pageno
)
616 match state.layout with
619 state.birdseye
<- Some
(conf, leftx
, l
.pageno
, hooverpageno
)
626 let gotoy_and_clear_text y =
628 if not
conf.verbose
then state.text
<- "";
632 cbput state.hists
.nav
(yratio state.y);
636 let y = cbgetc state.hists
.nav ~
-1 in
637 truncate
(y *. float state.maxy)
641 let y, h
= getpageyh n in
643 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
646 let gotopage1 n top
=
647 let y = getpagey n in
649 gotoy_and_clear_text (y + top
);
657 state.invalidated
<- state.invalidated
+ 1;
661 let c = c *. state.colorscale
in
666 let maxy = calcheight () in
668 let (n, top
) = state.anchor
in
669 let y, h
= getpageyh n in
670 y + (truncate
(top
*. float h
))
677 GlMat.mode `projection
;
678 GlMat.load_identity
();
679 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
680 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
681 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
685 GlMat.mode `projection
;
686 GlMat.load_identity
();
687 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
688 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
689 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
693 match state.layout with
695 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
699 if state.invalidated
= 0
700 then state.anchor
<- getanchor ();
703 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
707 GlMat.mode `modelview
;
708 GlMat.load_identity
();
709 GlClear.color
(scalecolor 1.0);
710 GlClear.clear
[`color
];
713 wcmd "geometry" [`i
w; `i h
];
717 GlDraw.color
(0.0, 0.0, 0.0);
719 (0.0, float (conf.winh
- 18))
720 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
722 let font = Glut.BITMAP_8_BY_13
in
723 GlDraw.color
(1.0, 1.0, 1.0);
724 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
725 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
726 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
730 let len = String.length
state.text
in
731 match state.textentry
with
733 if len > 0 then showtext ' '
state.text
735 | Some
(c, text
, _
, _
, _
) ->
739 text ^
" [" ^
state.text ^
"]"
749 state.text
<- Printf.sprintf
"%c%s" c s;
750 Glut.postRedisplay
();
764 state.rects
<- state.rects1
;
765 Glut.postRedisplay
()
768 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
769 state.pagecount
<- n;
770 state.invalidated
<- state.invalidated
- 1;
771 if state.invalidated
= 0
775 let s = Scanf.sscanf
cmd "t %n"
776 (fun n -> String.sub
cmd n (String.length
cmd - n))
778 Glut.setWindowTitle
s
781 let s = Scanf.sscanf
cmd "T %n"
782 (fun n -> String.sub
cmd n (String.length
cmd - n))
784 if state.textentry
= None
791 Glut.postRedisplay
();
797 let s = Scanf.sscanf
cmd "V %n"
798 (fun n -> String.sub
cmd n (String.length
cmd - n))
804 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
805 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
806 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
807 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
809 let y = (getpagey pageno) + truncate y0
in
812 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
815 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
816 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
817 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
818 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
821 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
824 let n, w, h
, r
, l
, s, p
=
825 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
826 (fun n w h r l
s p
->
827 (n-1, w, h
, r
, l
!= 0, s, p
))
830 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
831 state.memused
<- state.memused
+ s;
834 match state.throttle
with
835 | None
-> state.layout
836 | Some
layout -> layout
840 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
843 let evictedopaque = cbpeek state.pagecache
in
844 match findpageforopaque evictedopaque with
845 | None
-> failwith
"bug in gc"
846 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
847 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
849 wcmd "free" [`
s evictedopaque];
850 state.memused
<- state.memused
- evictedsize
;
851 Hashtbl.remove
state.pagemap k
;
852 cbdecr state.pagecache
;
859 cbput state.pagecache p
;
860 state.rendering
<- false;
862 begin match state.throttle
with
864 if pagevisible state.layout n
867 let allvisible = loadlayout state.layout in
868 if allvisible then preload ();
875 let y = getpagey l
.pageno + l
.pagey in
880 let (n, w, h
, x
) as pdim
=
881 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h
, x
)
883 state.pdims
<- pdim
:: state.pdims
886 let (l
, n, t
, h
, pos
) =
887 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
889 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
891 let l = String.length
s in
892 let b = Buffer.create
(String.length
s) in
899 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
902 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
911 let outline = (s, l, n, float t
/. float h
) in
913 match state.outlines with
914 | Olist
outlines -> Olist
(outline :: outlines)
915 | Oarray _
-> Olist
[outline]
916 | Onarrow _
-> Olist
[outline]
918 state.outlines <- outlines
921 log "unknown cmd `%S'" cmd
924 let now = Unix.gettimeofday
;;
928 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
933 let y = state.y + conf.scrollincr
in
934 let y = if y >= state.maxy then 0 else y in
940 let cmd = readcmd state.csock
in
949 let action = function
950 | HCprev
-> cbget cb ~
-1
951 | HCnext
-> cbget cb
1
952 | HCfirst
-> cbget cb ~
-(cb
.rc)
953 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
954 and cancel
() = cb
.rc <- rc
958 let search pattern forward
=
959 if String.length pattern
> 0
962 match state.layout with
965 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
968 let b = makecmd "search"
969 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
971 Buffer.add_char
b '
,'
;
972 Buffer.add_string
b pattern
;
973 Buffer.add_char
b '
\000'
;
976 writecmd state.csock
cmd;
979 let intentry text key
=
980 let c = Char.unsafe_chr key
in
983 let s = "x" in s.[0] <- c;
984 let text = text ^
s in
988 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
993 let b = Buffer.create
(String.length
s + 1) in
994 Buffer.add_string
b s;
999 let textentry text key
=
1000 let c = Char.unsafe_chr key
in
1002 | _
when key
>= 32 && key
< 127 ->
1003 let text = addchar text c in
1007 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1011 let reinit angle proportional
=
1012 conf.angle
<- angle
;
1013 conf.proportional
<- proportional
;
1015 wcmd "reinit" [`i angle
; `
b proportional
];
1018 let optentry text key
=
1019 let btos b = if b then "on" else "off" in
1020 let c = Char.unsafe_chr key
in
1024 try conf.scrollincr
<- int_of_string
s with exc
->
1025 state.text <- Printf.sprintf
"bad integer `%s': %s"
1026 s (Printexc.to_string exc
)
1028 TEswitch
('#'
, "", None
, intentry, ondone)
1033 Some
(int_of_string
s)
1035 state.text <- Printf.sprintf
"bad integer `%s': %s"
1036 s (Printexc.to_string exc
);
1039 | Some angle
-> reinit angle
conf.proportional
1042 TEswitch
('^'
, "", None
, intentry, ondone)
1045 conf.icase
<- not
conf.icase
;
1046 TEdone
("case insensitive search " ^
(btos conf.icase
))
1049 conf.preload <- not
conf.preload;
1051 TEdone
("preload " ^
(btos conf.preload))
1054 conf.verbose
<- not
conf.verbose
;
1055 TEdone
("verbose " ^
(btos conf.verbose
))
1058 conf.maxhfit
<- not
conf.maxhfit
;
1059 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1060 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1063 conf.crophack
<- not
conf.crophack
;
1064 TEdone
("crophack " ^
btos conf.crophack
)
1067 conf.showall
<- not
conf.showall
;
1068 TEdone
("showall " ^
btos conf.showall
)
1071 conf.underinfo
<- not
conf.underinfo
;
1072 TEdone
("underinfo " ^
btos conf.underinfo
)
1075 conf.savebmarks
<- not
conf.savebmarks
;
1076 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1082 match state.layout with
1087 conf.interpagespace <- int_of_string
s;
1088 state.maxy <- calcheight ();
1089 let y = getpagey pageno in
1092 state.text <- Printf.sprintf
"bad integer `%s': %s"
1093 s (Printexc.to_string exc
)
1095 TEswitch
('
%'
, "", None
, intentry, ondone)
1098 reinit conf.angle
(not
conf.proportional
);
1099 TEdone
("proprortional display " ^
btos conf.proportional
)
1102 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1106 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1108 let enterselector allowdel
outlines errmsg msg
=
1109 if Array.length
outlines = 0
1111 showtext ' ' errmsg
;
1115 Glut.setCursor
Glut.CURSOR_INHERIT
;
1117 match state.layout with
1119 | {pageno=pageno} :: rest
-> pageno
1123 if n = Array.length
outlines
1126 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1127 if outlinepageno
>= pageno then n else loop (n+1)
1132 Some
(allowdel
, active,
1133 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1134 Glut.postRedisplay
();
1138 let enteroutlinemode () =
1140 match state.outlines with
1143 let a = Array.of_list
(List.rev
l) in
1144 state.outlines <- Oarray
a;
1146 | Onarrow
(pat
, a, b) ->
1147 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1149 enterselector false outlines "Document has no outline" msg
;
1152 let enterbookmarkmode () =
1153 let bookmarks = Array.of_list
state.bookmarks in
1154 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1157 let quickbookmark ?title
() =
1158 match state.layout with
1164 let sec = Unix.gettimeofday
() in
1165 let tm = Unix.localtime
sec in
1166 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1170 (tm.Unix.tm_year
+ 1900)
1173 | Some
title -> title
1176 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1180 state.fullscreen
<- None
;
1181 Glut.reshapeWindow
w h
;
1184 let writeopen path password
=
1185 writecmd state.csock
("open " ^ path ^
"\000" ^
state.password ^
"\000");
1188 let opendoc path password
=
1191 state.password
<- password
;
1192 state.gen
<- state.gen
+ 1;
1194 writeopen path password
;
1195 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1196 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1200 let zoom = float conf.thumbw
/. float conf.winw
in
1201 let birdseyepageno =
1202 match state.layout with
1204 | l :: _
-> l.pageno
1207 Some
({ conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1);
1209 conf.presentation <- false;
1210 conf.interpagespace <- 10;
1211 conf.hlinks
<- false;
1213 state.mstate
<- Mnone
;
1214 conf.showall
<- false;
1215 Glut.setCursor
Glut.CURSOR_INHERIT
;
1218 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1225 let birdseyeoff (c, leftx
, pageno, _
) =
1226 state.birdseye
<- None
;
1227 conf.zoom <- c.zoom;
1228 conf.presentation <- c.presentation;
1229 conf.interpagespace <- c.interpagespace;
1230 conf.showall
<- c.showall
;
1231 conf.hlinks
<- c.hlinks
;
1235 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1240 let togglebirdseye () =
1241 match state.birdseye
with
1242 | None
-> birdseyeon ()
1243 | Some vals
-> birdseyeoff vals
1246 let viewkeyboard ~key ~x ~
y =
1248 state.textentry <- te
;
1251 Glut.postRedisplay
()
1253 match state.textentry with
1255 let c = Char.chr key
in
1261 let y = getnav () in
1262 gotoy_and_clear_text y
1265 begin match state.birdseye
with
1267 | Some
((_
, _
, pageno, _
) as vals
) ->
1269 reshape conf.winw
conf.winh
;
1270 state.anchor
<- (pageno, 0.0);
1279 Glut.postRedisplay
()
1282 let ondone isforw
s =
1283 cbput state.hists
.pat
s;
1284 state.searchpattern
<- s;
1287 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1288 textentry, ondone (c ='
/'
)))
1290 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1291 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1292 conf.zoom <- min
2.2 (conf.zoom +. incr);
1293 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1294 reshape conf.winw
conf.winh
1299 try int_of_string
s with exc
->
1300 state.text <- Printf.sprintf
"bad integer `%s': %s"
1301 s (Printexc.to_string exc
);
1307 state.text <- "page bias is now " ^ string_of_int
n;
1310 enttext (Some
('
+'
, "", None
, intentry, ondone))
1312 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1313 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1314 conf.zoom <- max
0.01 (conf.zoom -. decr);
1315 if conf.zoom <= 1.0 then state.x
<- 0;
1316 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1317 reshape conf.winw
conf.winh
;
1323 enttext (Some
('
-'
, "", None
, optentry, ondone))
1325 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1328 state.text <- "zoom is 100%";
1329 reshape conf.winw
conf.winh
1331 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1332 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1337 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1338 reshape conf.winw
conf.winh
;
1341 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1343 reshape conf.winw
conf.winh
;
1348 try int_of_string
s with exc
->
1349 state.text <- Printf.sprintf
"bad integer `%s': %s"
1350 s (Printexc.to_string exc
);
1356 cbput state.hists
.pag
(string_of_int
n);
1357 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1360 let pageentry text key
=
1361 match Char.unsafe_chr key
with
1362 | 'g'
-> TEdone
text
1363 | _
-> intentry text key
1365 let text = "x" in text.[0] <- c;
1366 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1370 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1371 reshape conf.winw
conf.winh
;
1374 conf.hlinks
<- not
conf.hlinks
;
1375 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1376 Glut.postRedisplay
()
1379 conf.autoscroll
<- not
conf.autoscroll
1382 conf.presentation <- not
conf.presentation;
1383 showtext ' '
("presentation mode " ^
1384 if conf.presentation then "on" else "off");
1388 begin match state.fullscreen
with
1390 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1393 state.fullscreen
<- None
;
1398 gotoy_and_clear_text 0
1401 search state.searchpattern
true
1404 search state.searchpattern
false
1407 begin match state.layout with
1410 gotoy_and_clear_text (getpagey l.pageno)
1414 begin match List.rev
state.layout with
1417 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1418 gotoy_and_clear_text (getpagey pageno)
1422 begin match state.layout with
1425 let pageno = max
0 (l.pageno-1) in
1426 gotoy_and_clear_text (getpagey pageno)
1431 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1433 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1435 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1439 else (100. *. (float state.y /. float maxy)) in
1442 Printf.sprintf
"Page %d of %d %.2f%%"
1443 (fn+1) state.pagecount
percent
1446 "Pages %d-%d of %d %.2f%%"
1447 (fn+1) (ln
+1) state.pagecount
percent
1452 begin match state.layout with
1455 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1456 Glut.postRedisplay
();
1460 enterbookmarkmode ()
1464 match state.layout with
1467 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1471 enttext (Some
('~'
, "", None
, textentry, ondone))
1475 showtext ' '
"Quick bookmark added";
1478 begin match state.layout with
1480 let rect = getpdimrect
l.pagedimno
in
1484 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1485 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1487 (truncate
(rect.(1) -. rect.(0)),
1488 truncate
(rect.(3) -. rect.(0)))
1492 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1494 Glut.postRedisplay
();
1500 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1505 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1506 Glut.postRedisplay
()
1508 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1509 | 'j'
-> gotoy (clamp conf.scrollincr
)
1511 | '
r'
-> opendoc state.path
state.password
1514 vlog "huh? %d %c" key
(Char.chr key
);
1517 | Some
(c, text, opthist
, onkey
, ondone) when key
= 8 ->
1518 let len = String.length
text in
1521 state.textentry <- None
;
1522 Glut.postRedisplay
();
1525 let s = String.sub
text 0 (len - 1) in
1526 enttext (Some
(c, s, opthist
, onkey
, ondone))
1529 | Some
(c, text, onhist, onkey
, ondone) ->
1530 begin match Char.unsafe_chr key
with
1533 state.textentry <- None
;
1534 Glut.postRedisplay
()
1537 begin match onhist with
1539 | Some
(_
, onhistcancel
) -> onhistcancel
()
1541 state.textentry <- None
;
1542 Glut.postRedisplay
()
1545 begin match onkey
text key
with
1547 state.textentry <- None
;
1549 Glut.postRedisplay
()
1552 enttext (Some
(c, text, onhist, onkey
, ondone));
1555 state.textentry <- None
;
1556 Glut.postRedisplay
()
1559 state.textentry <- Some te
;
1560 Glut.postRedisplay
()
1565 let narrow outlines pattern
=
1566 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1570 let rec fold accu n =
1574 let (s, _
, _
, _
) as o
= outlines.(n) in
1576 if (try ignore
(Str.search_forward re
s 0); true
1577 with Not_found
-> false)
1583 let matched = fold [] (Array.length
outlines - 1) in
1584 if matched = [] then None
else Some
(Array.of_list
matched)
1587 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1588 let search active pattern
incr =
1591 if n = Array.length
outlines || n = -1
1594 let (s, _
, _
, _
) = outlines.(n) in
1596 (try ignore
(Str.search_forward re
s 0); true
1597 with Not_found
-> false)
1599 else loop (n + incr)
1604 let re = Str.regexp_case_fold pattern
in
1610 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1613 if String.length qsearch
= 0
1616 state.outline <- None
;
1617 Glut.postRedisplay
();
1621 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1622 Glut.postRedisplay
();
1626 let incr = if key
= 18 then -1 else 1 in
1628 match search (active + incr) qsearch
incr with
1630 state.text <- qsearch ^
" [not found]";
1633 state.text <- qsearch
;
1634 active, firstof active
1636 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1637 Glut.postRedisplay
();
1640 let len = String.length qsearch
in
1647 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1650 let qsearch = String.sub
qsearch 0 (len - 1) in
1652 match search active qsearch ~
-1 with
1654 state.text <- qsearch ^
" [not found]";
1657 state.text <- qsearch;
1658 active, firstof active
1660 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1662 Glut.postRedisplay
()
1665 if active < Array.length
outlines
1667 let (_
, _
, n, t
) = outlines.(active) in
1671 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1672 state.outline <- None
;
1673 Glut.postRedisplay
();
1675 | _
when key
>= 32 && key
< 127 ->
1676 let pattern = addchar qsearch (Char.chr key
) in
1678 match search active pattern 1 with
1680 state.text <- pattern ^
" [not found]";
1683 state.text <- pattern;
1684 active, firstof active
1686 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1687 Glut.postRedisplay
()
1689 | 14 when not allowdel
-> (* ctrl-n *)
1690 if String.length
qsearch > 0
1692 let optoutlines = narrow outlines qsearch in
1693 begin match optoutlines with
1694 | None
-> state.text <- "can't narrow"
1696 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1697 match state.outlines with
1700 state.outlines <- Onarrow
(qsearch, outlines, a)
1701 | Onarrow
(pat
, a, b) ->
1702 state.outlines <- Onarrow
(qsearch, outlines, b)
1705 Glut.postRedisplay
()
1707 | 21 when not allowdel
-> (* ctrl-u *)
1709 match state.outlines with
1712 let a = Array.of_list
(List.rev
l) in
1713 state.outlines <- Oarray
a;
1715 | Onarrow
(pat
, a, b) ->
1716 state.outlines <- Oarray
b;
1720 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1721 Glut.postRedisplay
()
1725 Some
(allowdel
, active, firstof active, outlines, qsearch);
1726 Glut.postRedisplay
()
1728 | 127 when allowdel
->
1729 let len = Array.length
outlines - 1 in
1732 state.outline <- None
;
1733 state.bookmarks <- [];
1736 let bookmarks = Array.init
len
1738 let i = if i >= active then i + 1 else i in
1749 Glut.postRedisplay
()
1751 | _
-> log "unknown key %d" key
1754 let keyboard ~key ~x ~
y =
1759 match state.outline with
1760 | None
-> viewkeyboard ~key ~x ~
y
1761 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1764 let birdseyespecial key x
y (conf, leftx
, pageno, hooverpageno
) =
1767 let pageno = max
0 (pageno - 1) in
1769 let rec loop = function
1772 if l.pageno = pageno
1779 then gotopage1 pageno 0
1780 else Glut.postRedisplay
();
1781 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
)
1784 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1785 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1786 let rec loop = function
1787 | [] -> gotopage1 pageno 0
1789 if l.pageno = pageno
1791 if l.pagevh != l.pageh
1792 then gotoy (clamp (l.pageh
- l.pagevh + conf.interpagespace))
1793 else Glut.postRedisplay
()
1799 | Glut.KEY_PAGE_UP
->
1800 begin match state.layout with
1802 if l.pageno = pageno
1804 match layout (state.y - conf.winh
) conf.winh
with
1805 | [] -> gotoy (clamp (-conf.winh
))
1807 let pageno = max
0 (l.pageno - 1) in
1808 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1812 let pageno = max
0 (l.pageno - 1) in
1813 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1816 | [] -> gotoy (clamp (-conf.winh
))
1818 | Glut.KEY_PAGE_DOWN
->
1819 begin match List.rev
state.layout with
1821 let pageno = min
l.pageno (state.pagecount
- 1) in
1822 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1823 gotoy (clamp (l.pagedispy
+ l.pageh
))
1824 | [] -> gotoy (clamp conf.winh
)
1828 state.birdseye
<- Some
(conf, leftx
, 0, hooverpageno
);
1831 let pageno = state.pagecount
- 1 in
1832 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1833 if not
(pagevisible state.layout pageno)
1836 match List.rev
state.pdims
with
1838 | (_
, _
, h, _
) :: _
-> h
1840 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
1841 else Glut.postRedisplay
();
1845 let special ~key ~x ~
y =
1846 match state.outline, state.birdseye
with
1847 | None
, _
when key
= Glut.KEY_F9
->
1849 reshape conf.winw
conf.winh
;
1851 | None
, (Some vals
) ->
1852 birdseyespecial key x
y vals
1855 begin match state.textentry with
1859 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1860 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1861 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1862 | Glut.KEY_PAGE_UP
->
1863 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1865 match state.layout with
1867 | l :: _
-> state.y - l.pagey
1870 | Glut.KEY_PAGE_DOWN
->
1871 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1873 match List.rev
state.layout with
1875 | l :: _
-> getpagey l.pageno
1878 | Glut.KEY_HOME
-> addnav (); 0
1881 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1883 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1884 state.x
<- state.x
- 10;
1886 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1887 state.x
<- state.x
+ 10;
1892 gotoy_and_clear_text y
1894 | Some
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1897 | Glut.KEY_UP
-> action HCprev
1898 | Glut.KEY_DOWN
-> action HCnext
1899 | Glut.KEY_HOME
-> action HCfirst
1900 | Glut.KEY_END
-> action HClast
1903 state.textentry <- Some
(c, s, onhist, onkey
, ondone);
1904 Glut.postRedisplay
()
1909 | Some
(allowdel
, active, first
, outlines, qsearch), _
->
1910 let maxrows = maxoutlinerows () in
1911 let calcfirst first
active =
1914 let rows = active - first
in
1915 if rows > maxrows then active - maxrows else first
1919 let active = active + incr in
1920 let active = max
0 (min
active (Array.length
outlines - 1)) in
1921 let first = calcfirst first active in
1922 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1923 Glut.postRedisplay
()
1925 let updownlevel incr =
1926 let len = Array.length
outlines in
1927 let (_
, curlevel
, _
, _
) = outlines.(active) in
1929 if i = len then i-1 else if i = -1 then 0 else
1930 let (_
, l, _
, _
) = outlines.(i) in
1931 if l != curlevel
then i else flow (i+incr)
1933 let active = flow active in
1934 let first = calcfirst first active in
1935 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1936 Glut.postRedisplay
()
1939 | Glut.KEY_UP
-> navigate ~
-1
1940 | Glut.KEY_DOWN
-> navigate 1
1941 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1942 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1944 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1945 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1948 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1949 Glut.postRedisplay
()
1952 let active = Array.length
outlines - 1 in
1953 let first = max
0 (active - maxrows) in
1954 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1955 Glut.postRedisplay
()
1960 let drawplaceholder l =
1961 let margin = state.x
+ (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1963 (float l.pagex
, float l.pagedispy
)
1964 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1966 let x = float (if margin < 0 then -margin else l.pagex
)
1967 and y = float (l.pagedispy
+ 13) in
1968 let font = Glut.BITMAP_8_BY_13
in
1969 GlDraw.color
(0.0, 0.0, 0.0);
1970 GlPix.raster_pos ~
x ~
y ();
1971 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1972 ("Loading " ^ string_of_int
(l.pageno + 1));
1975 let now () = Unix.gettimeofday
();;
1978 if state.textentry = None
1980 match state.birdseye
with
1981 | None
-> GlDraw.color
(scalecolor 1.0);
1982 | Some
(_
, _
, pageno, hooverpageno
) ->
1984 if l.pageno = pageno
1987 if l.pageno = hooverpageno
1992 GlDraw.color (scalecolor color);
1994 else GlDraw.color (scalecolor 0.4);
1995 begin match getopaque l.pageno with
1996 | Some
(opaque, _
) when validopaque opaque ->
1998 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
2002 vlog "draw %d %f sec" l.pageno d;
2010 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2011 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2012 let sh = float conf.winh
/. sh in
2013 let sh = max
sh (float conf.scrollh
) in
2016 if state.y = state.maxy
2018 else float y /. float maxy
2020 let position = (float conf.winh
-. sh) *. percent in
2023 if position +. sh > float conf.winh
2024 then float conf.winh
-. sh
2030 let scrollindicator () =
2031 GlDraw.color (0.64 , 0.64, 0.64);
2033 (float (conf.winw
- conf.scrollw
), 0.)
2034 (float conf.winw
, float conf.winh
)
2036 GlDraw.color (0.0, 0.0, 0.0);
2038 let position, sh = scrollph state.y in
2040 (float (conf.winw
- conf.scrollw
), position)
2041 (float conf.winw
, position +. sh)
2045 let showsel margin =
2046 match state.mstate
with
2047 | Mnone
| Mscroll _
| Mpan _
->
2050 | Msel
((x0
, y0
), (x1
, y1
)) ->
2051 let rec loop = function
2053 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2054 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2056 match getopaque l.pageno with
2057 | Some
(opaque, _
) when validopaque opaque ->
2058 let oy = -l.pagey + l.pagedispy
in
2060 (x0
- margin - state.x, y0
,
2061 x1
- margin - state.x, y1
) oy;
2071 let panx = float state.x in
2073 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2074 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2076 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2078 if l.pageno = pageno
2080 let d = float (l.pagedispy
- l.pagey) in
2081 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2082 GlDraw.begins `quads
;
2084 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2085 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2086 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2087 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2097 let showoutline = function
2099 | Some
(allowdel
, active, first, outlines, qsearch) ->
2101 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2102 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2103 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2106 GlDraw.color (1., 1., 1.);
2107 let font = Glut.BITMAP_9_BY_15
in
2108 let draw_string x y s =
2109 GlPix.raster_pos ~
x ~
y ();
2110 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2113 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2116 let (s, l, _
, _
) = outlines.(row
) in
2117 let y = (row
- first) * 16 in
2122 GlDraw.polygon_mode `both `line
;
2123 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2124 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2125 GlDraw.rect (0., float (y + 1))
2126 (float (conf.winw
- 1), float (y + 18));
2127 GlDraw.polygon_mode `both `fill
;
2129 GlDraw.color (1., 1., 1.);
2131 draw_string (float x) (float (y + 16)) s;
2139 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2140 GlDraw.viewport
margin 0 state.w conf.winh
;
2142 GlClear.color (scalecolor 0.5);
2143 GlClear.clear
[`
color];
2146 let x = float state.x in
2147 GlMat.translate ~
x ();
2151 Gl.enable `scissor_test
;
2152 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2154 List.iter
drawpage state.layout;
2157 Gl.disable `scissor_test
2161 let x = -.float state.x in
2162 GlMat.translate ~
x ();
2166 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2169 showoutline state.outline;
2171 Glut.swapBuffers
();
2175 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2176 let x = x - margin - state.x in
2177 let rec f = function
2179 begin match getopaque l.pageno with
2180 | Some
(opaque, _
) when validopaque opaque ->
2181 let y = y - l.pagedispy
in
2184 let y = l.pagey + y in
2185 let x = x - l.pagex
in
2186 match whatsunder
opaque x y with
2199 let mouse ~button ~bstate ~
x ~
y =
2201 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2209 let incr = incr * 2 in
2210 let y = clamp incr in
2211 gotoy_and_clear_text y
2213 | Glut.LEFT_BUTTON
when state.outline = None
2214 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2215 if bstate
= Glut.DOWN
2217 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2218 state.mstate
<- Mpan
(x, y)
2221 state.mstate
<- Mnone
2224 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
2225 if bstate
= Glut.DOWN
2227 let position, sh = scrollph state.y in
2228 if y > truncate
position && y < truncate
(position +. sh)
2230 state.mstate
<- Mscroll
2232 let percent = float y /. float conf.winh
in
2233 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2235 state.mstate
<- Mscroll
2237 state.mstate
<- Mnone
2239 | Glut.LEFT_BUTTON
when state.outline = None
&& state.birdseye
<> None
->
2240 begin match state.birdseye
with
2241 | Some
(conf, leftx
, pageno, hooverpageno
) ->
2242 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2243 let rec loop = function
2246 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2247 && x > margin && x < margin + l.pagew
2249 birdseyeoff (conf, leftx
, l.pageno, hooverpageno
);
2250 reshape conf.winw
conf.winh
;
2251 state.anchor
<- (l.pageno, 0.0);
2256 | None
-> () (* impossible *)
2259 | Glut.LEFT_BUTTON
when state.outline = None
->
2260 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2261 begin match dest with
2262 | Ulinkgoto
(pageno, top
) ->
2265 gotopage1 pageno top
2270 | Unone
when bstate
= Glut.DOWN
->
2271 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2272 state.mstate
<- Mpan
(x, y);
2274 | Unone
| Utext _
->
2275 if bstate
= Glut.DOWN
2277 if conf.angle
mod 360 = 0
2279 state.mstate
<- Msel
((x, y), (x, y));
2280 Glut.postRedisplay
()
2284 match state.mstate
with
2288 state.mstate
<- Mnone
2291 Glut.setCursor
Glut.CURSOR_INHERIT
;
2292 state.mstate
<- Mnone
2294 | Msel
((x0
, y0
), (x1
, y1
)) ->
2296 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2297 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2299 match getopaque l.pageno with
2300 | Some
(opaque, _
) when validopaque opaque ->
2304 List.iter
f state.layout;
2305 copysel
""; (* ugly *)
2306 Glut.setCursor
Glut.CURSOR_INHERIT
;
2307 state.mstate
<- Mnone
;
2314 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2317 if state.outline = None
2319 match state.mstate
with
2325 state.mstate
<- Mpan
(x, y);
2326 if conf.zoom > 1.0 then state.x <- state.x + dx;
2328 gotoy_and_clear_text y
2331 state.mstate
<- Msel
(a, (x, y));
2332 Glut.postRedisplay
()
2335 let y = min
conf.winh
(max
0 y) in
2336 let percent = float y /. float conf.winh
in
2337 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2338 gotoy_and_clear_text y
2342 match state.birdseye
with
2343 | Some
(conf, leftx
, pageno, hooverpageno
) ->
2344 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2345 let rec loop = function
2347 if hooverpageno
!= -1
2349 state.birdseye
<- Some
(conf, leftx
, pageno, -1);
2350 Glut.postRedisplay
();
2353 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2354 && x > margin && x < margin + l.pagew
2356 state.birdseye
<- Some
(conf, leftx
, pageno, l.pageno);
2357 Glut.postRedisplay
();
2364 if state.outline = None
2366 match state.mstate
with
2368 begin match getunder x y with
2369 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2371 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2372 Glut.setCursor
Glut.CURSOR_INFO
2373 | Ulinkgoto
(page
, y) ->
2375 then showtext 'p'
("age: " ^ string_of_int page
);
2376 Glut.setCursor
Glut.CURSOR_INFO
2378 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2379 Glut.setCursor
Glut.CURSOR_TEXT
2382 | Mpan _
| Msel _
| Mscroll
->
2393 match Sys.os_type
with
2394 | "Win32" -> Sys.getenv
"HOMEPATH"
2395 | _
-> Sys.getenv
"HOME"
2398 ("Can not determine home directory location: " ^
2399 Printexc.to_string exn
);
2403 let config_of c attrs
=
2407 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2408 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2409 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2410 | "preload" -> { c with preload = bool_of_string v
}
2411 | "page-bias" -> { c with pagebias
= int_of_string v
}
2412 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2413 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2414 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2415 | "throttle" -> { c with showall
= bool_of_string v
}
2416 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2417 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2418 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2420 let zoom = float_of_string v
/. 100. in
2421 let zoom = max
0.01 (min
2.2 zoom) in
2422 { c with zoom = zoom }
2423 | "presentation" -> { c with presentation = bool_of_string v
}
2424 | "rotation-angle" -> { c with angle
= int_of_string v
}
2425 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2426 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2427 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2428 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2429 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2430 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2431 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2432 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string v
) }
2435 prerr_endline
("Error processing attribute (`" ^
2436 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2439 let rec fold c = function
2442 let c = apply c k v
in
2448 let bookmark_of attrs
=
2449 let rec fold title page rely
= function
2450 | ("title", v
) :: rest
-> fold v page rely rest
2451 | ("page", v
) :: rest
-> fold title v rely rest
2452 | ("rely", v
) :: rest
-> fold title page v rest
2453 | _
:: rest
-> fold title page rely rest
2454 | [] -> title, page
, rely
2456 fold "invalid" "0" "0" attrs
2459 let setconf dst src
=
2460 dst
.scrollw
<- src
.scrollw
;
2461 dst
.scrollh
<- src
.scrollh
;
2462 dst
.icase
<- src
.icase
;
2463 dst
.preload <- src
.preload;
2464 dst
.pagebias
<- src
.pagebias
;
2465 dst
.verbose
<- src
.verbose
;
2466 dst
.scrollincr
<- src
.scrollincr
;
2467 dst
.maxhfit
<- src
.maxhfit
;
2468 dst
.crophack
<- src
.crophack
;
2469 dst
.autoscroll
<- src
.autoscroll
;
2470 dst
.showall
<- src
.showall
;
2471 dst
.hlinks
<- src
.hlinks
;
2472 dst
.underinfo
<- src
.underinfo
;
2473 dst
.interpagespace <- src
.interpagespace;
2474 dst
.zoom <- src
.zoom;
2475 dst
.presentation <- src
.presentation;
2476 dst
.angle
<- src
.angle
;
2477 dst
.winw
<- src
.winw
;
2478 dst
.winh
<- src
.winh
;
2479 dst
.savebmarks
<- src
.savebmarks
;
2480 dst
.memlimit
<- src
.memlimit
;
2481 dst
.proportional
<- src
.proportional
;
2482 dst
.texcount
<- src
.texcount
;
2483 dst
.sliceheight
<- src
.sliceheight
;
2484 dst
.thumbw
<- src
.thumbw
;
2488 let l = String.length
s in
2489 let b = Buffer.create
l in
2495 let h = Hashtbl.create
10 in
2496 let dc = { defconf with angle
= defconf.angle
} in
2497 let rec toplevel v t spos epos
=
2499 | Vdata
| Vcdata
| Vend
-> v
2500 | Vopen
("llppconfig", attrs
, closed
) ->
2503 else { v
with f = llppconfig
}
2505 error
"unexpected subelement at top level" s spos
2506 | Vclose tag
-> error
"unexpected close at top level" s spos
2508 and llppconfig v t spos epos
=
2510 | Vdata
| Vcdata
| Vend
-> v
2511 | Vopen
("defaults", attrs
, closed
) ->
2512 let c = config_of dc attrs
in
2516 else { v
with f = skip
"defaults" (fun () -> v
) }
2518 | Vopen
("doc", attrs
, closed
) ->
2521 List.assoc
"path" attrs
2522 with Not_found
-> error
"doc is missing path attribute" s spos
2524 let path = unent pathent in
2525 let c = config_of dc attrs
in
2528 float_of_string
(List.assoc
"rely" attrs
)
2532 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2537 int_of_string
(List.assoc
"pan" attrs
)
2541 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2545 then (Hashtbl.add
h path (c, [], x, y); v
)
2546 else { v
with f = doc
path x y c [] }
2548 | Vopen
(tag
, _
, closed
) ->
2549 error
"unexpected subelement in llppconfig" s spos
2551 | Vclose
"llppconfig" -> { v
with f = toplevel }
2552 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2554 and doc
path x y c bookmarks v t spos epos
=
2556 | Vdata
| Vcdata
-> v
2557 | Vend
-> error
"unexpected end of input in doc" s spos
2558 | Vopen
("bookmarks", attrs
, closed
) ->
2559 { v
with f = pbookmarks
path x y c bookmarks }
2561 | Vopen
(tag
, _
, _
) ->
2562 error
"unexpected subelement in doc" s spos
2565 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2566 { v
with f = llppconfig
}
2568 | Vclose tag
-> error
"unexpected close in doc" s spos
2570 and pbookmarks
path x y c bookmarks v t spos epos
=
2572 | Vdata
| Vcdata
-> v
2573 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2574 | Vopen
("item", attrs
, closed
) ->
2575 let titleent, spage
, srely
= bookmark_of attrs
in
2580 dolog "Failed to convert page %S to integer: %s"
2581 spage
(Printexc.to_string exn
);
2586 float_of_string srely
2588 dolog "Failed to convert rely %S to real: %s"
2589 srely
(Printexc.to_string exn
);
2592 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2594 then { v
with f = pbookmarks
path x y c bookmarks }
2597 { v
with f = skip
"item" f }
2600 error
"unexpected subelement in bookmarks" s spos
2602 | Vclose
"bookmarks" ->
2603 { v
with f = doc
path x y c bookmarks }
2605 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2607 and skip tag
f v t spos epos
=
2609 | Vdata
| Vcdata
-> v
2611 error
("unexpected end of input in skipped " ^ tag
) s spos
2612 | Vopen
(tag'
, _
, closed
) ->
2616 let f'
() = { v
with f = skip tag
f } in
2617 { v
with f = skip tag'
f'
}
2621 else error
("unexpected close in skipped " ^ tag
) s spos
2624 parse
{ f = toplevel; accu = () } s;
2630 let len = in_channel_length ic
in
2631 let s = String.create
len in
2632 really_input ic
s 0 len;
2635 | Parse_error
(msg
, s, pos
) ->
2636 let subs = subs s pos
in
2637 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2638 failwith
("parse error: " ^
s)
2641 failwith
("config load error: " ^
Printexc.to_string exn
)
2647 let dir = Filename.concat
home ".config" in
2648 if Sys.is_directory
dir then dir else home
2651 Filename.concat
dir "llpp.conf"
2655 if Sys.file_exists
path
2658 (try Some
(open_in_bin
path)
2661 ("Error opening configuation file `" ^
path ^
"': " ^
2662 Printexc.to_string exn
);
2671 ("Error loading configuation from `" ^
path ^
"': " ^
2672 Printexc.to_string exn
);
2678 f (Hashtbl.create
0, defconf)
2683 let pc, pb
, px
, py =
2685 Hashtbl.find
h state.path
2686 with Not_found
-> dc, [], 0, 0.0
2690 state.bookmarks <- pb
;
2692 cbput state.hists
.nav
py;
2697 let add_attrs bb always
dc c =
2700 then Printf.bprintf bb
"\n %s='%b'" s a
2703 then Printf.bprintf bb
"\n %s='%d'" s a
2706 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2710 then dc.winw
, dc.winh
2712 match state.fullscreen
with
2714 | None
-> c.winw
, c.winh
2716 let zoom, presentation, interpagespace, showall
=
2718 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2720 match state.birdseye
with
2721 | Some
(bc
, _
, _
, _
) ->
2722 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
2723 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
2725 oi
"width" w dc.winw
;
2726 oi
"height" h dc.winh
;
2727 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2728 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2729 ob "case-insensitive-search" c.icase
dc.icase
;
2730 ob "preload" c.preload dc.preload;
2731 oi
"page-bias" c.pagebias
dc.pagebias
;
2732 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2733 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2734 ob "crop-hack" c.crophack
dc.crophack
;
2735 ob "throttle" showall
dc.showall
;
2736 ob "highlight-links" c.hlinks
dc.hlinks
;
2737 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2738 oi
"vertical-margin" interpagespace dc.interpagespace;
2739 oz
"zoom" zoom dc.zoom;
2740 ob "presentation" presentation dc.presentation;
2741 oi
"rotation-angle" c.angle
dc.angle
;
2742 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2743 ob "proportional-display" c.proportional
dc.proportional
;
2744 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2745 oi
"texcount" c.texcount
dc.texcount
;
2746 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2747 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
2751 let bb = Buffer.create
32768 in
2753 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2754 add_attrs bb true dc dc;
2755 Buffer.add_string
bb "/>\n";
2757 let adddoc path x y c bookmarks =
2758 if bookmarks == [] && c = dc && y = 0.0
2761 Printf.bprintf
bb "<doc path='%s'"
2762 (enent
path 0 (String.length
path));
2765 then Printf.bprintf
bb " rely='%f'" y;
2768 then Printf.bprintf
bb " pan='%d'" x;
2770 add_attrs bb false dc c;
2772 begin match bookmarks with
2773 | [] -> Buffer.add_string
bb "/>\n"
2775 Buffer.add_string
bb ">\n<bookmarks>\n";
2776 List.iter
(fun (title, _level
, page, rely) ->
2778 "<item title='%s' page='%d' rely='%f'/>\n"
2779 (enent
title 0 (String.length
title))
2783 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2789 match state.birdseye
with
2790 | Some
(_
, x, _
, _
) -> x
2793 adddoc state.path x (yratio state.y) conf
2794 (if conf.savebmarks
then state.bookmarks else []);
2796 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2797 if path <> state.path
2799 adddoc path x y c bookmarks
2801 Buffer.add_string
bb "</llppconfig>";
2804 if Buffer.length
bb > 0
2807 let tmp = path ^
".tmp" in
2808 let oc = open_out_bin
tmp in
2809 Buffer.output_buffer
oc bb;
2811 Sys.rename
tmp path;
2814 ("error while saving configuration: " ^
Printexc.to_string exn
)
2820 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2821 (fun s -> state.path <- s)
2822 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2825 if String.length
state.path = 0
2826 then (prerr_endline
"filename missing"; exit
1)
2828 if Filename.is_relative
state.path
2829 then Filename.concat
(Sys.getcwd
()) state.path
2837 let _ = Glut.init
Sys.argv
in
2838 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2839 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2840 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2843 if Sys.os_type
= "Unix"
2845 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2847 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2848 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2849 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2850 Unix.bind
sock addr;
2852 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2853 Unix.connect
csock addr;
2854 let ssock, _ = Unix.accept
sock in
2857 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2858 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2862 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2866 let () = Glut.displayFunc
display in
2867 let () = Glut.reshapeFunc
reshape in
2868 let () = Glut.keyboardFunc
keyboard in
2869 let () = Glut.specialFunc
special in
2870 let () = Glut.idleFunc
(Some
idle) in
2871 let () = Glut.mouseFunc
mouse in
2872 let () = Glut.motionFunc
motion in
2873 let () = Glut.passiveMotionFunc
pmotion in
2875 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2876 state.csock <- csock;
2877 state.ssock <- ssock;
2878 state.text <- "Opening " ^
path;
2879 writeopen state.path state.password
;
2883 let rec handlelablglutbug () =
2886 with Glut.BadEnum
"key in special_of_int" ->
2887 showtext '
!'
" LablGlut bug: special key not recognized";
2888 handlelablglutbug ()
2890 handlelablglutbug ();