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 getpagewh
: int -> float array
= "ml_getpagewh";;
17 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
20 type mstate
= Msel
of (mpos
* mpos
) | Mpan
of mpos
| Mnone
;;
30 type textentry
= (char
* string * onhist
option * onkey
* ondone
)
31 and onkey
= string -> int -> te
32 and ondone
= string -> unit
33 and onhist
= histcmd
-> string
34 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
39 | TEswitch
of textentry
43 { store
= Array.create n v
50 let cblen b
= Array.length b
.store
;;
55 b
.wc
<- (b
.wc
+ 1) mod len;
56 b
.len <- min
(b
.len + 1) len;
59 let cbpeekw b
= b
.store
.(b
.wc
);;
65 let rc = b
.rc + dir
in
66 let rc = if rc = -1 then b
.len - 1 else rc in
67 let rc = if rc = b
.len then 0 else rc in
78 Array.fill b
.store
0 (Array.length b
.store
) v
;
93 { mutable scrollw
: int
94 ; mutable scrollh
: int
95 ; mutable icase
: bool
96 ; mutable preload
: bool
97 ; mutable pagebias
: int
98 ; mutable verbose
: bool
99 ; mutable scrollincr
: int
100 ; mutable maxhfit
: bool
101 ; mutable crophack
: bool
102 ; mutable autoscroll
: bool
103 ; mutable showall
: bool
104 ; mutable hlinks
: bool
105 ; mutable underinfo
: bool
106 ; mutable interpagespace
: int
107 ; mutable margin
: int
108 ; mutable presentation
: bool
112 type outline
= string * int * int * float;;
114 | Oarray
of outline array
115 | Olist
of outline list
116 | Onarrow
of outline array
* outline array
119 type rect
= (float * float * float * float * float * float * float * float);;
122 { mutable csock
: Unix.file_descr
123 ; mutable ssock
: Unix.file_descr
127 ; mutable rotate
: int
132 ; mutable layout
: layout list
133 ; pagemap
: ((int * int * int), string) Hashtbl.t
134 ; mutable pages
: (int * int * int) list
135 ; mutable pagecount
: int
136 ; pagecache
: string circbuf
137 ; mutable rendering
: bool
138 ; mutable mstate
: mstate
139 ; mutable searchpattern
: string
140 ; mutable rects
: (int * int * rect
) list
141 ; mutable rects1
: (int * int * rect
) list
142 ; mutable text
: string
143 ; mutable fullscreen
: (int * int) option
144 ; mutable textentry
: textentry
option
145 ; mutable outlines
: outlines
146 ; mutable outline
: (bool * int * int * outline array
* string) option
147 ; mutable bookmarks
: outline list
148 ; mutable path
: string
149 ; mutable password
: string
150 ; mutable invalidated
: int
151 ; mutable colorscale
: float
155 { pat
: string circbuf
156 ; pag
: string circbuf
157 ; nav
: float circbuf
177 ; presentation
= false
193 ; pagemap
= Hashtbl.create
10
194 ; pagecache
= cbnew 10 ""
205 ; outlines
= Olist
[]
212 { nav
= cbnew 100 0.0
223 Printf.kprintf prerr_endline fmt
225 Printf.kprintf ignore fmt
229 let len = String.length s
in
231 let b = Buffer.create
n in
232 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
233 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
234 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
235 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
236 Buffer.add_string
b s
;
237 let s'
= Buffer.contents
b in
238 let n'
= Unix.write fd
s'
0 n in
239 if n'
!= n then failwith
"write failed";
244 let n = Unix.read fd
s 0 4 in
245 if n != 4 then failwith
"incomplete read(len)";
247 lor (Char.code
s.[0] lsl 24)
248 lor (Char.code
s.[1] lsl 16)
249 lor (Char.code
s.[2] lsl 8)
250 lor (Char.code
s.[3] lsl 0)
252 let s = String.create
len in
253 let n = Unix.read fd
s 0 len in
254 if n != len then failwith
"incomplete read(data)";
261 else float y
/. float state.maxy
265 let b = Buffer.create
10 in
266 Buffer.add_string
b s;
267 let rec combine = function
270 Buffer.add_char
b ' '
;
273 | `
b b -> if b then "1" else "0"
275 | `i i
-> string_of_int i
276 | `f f
-> string_of_float f
277 | `I f
-> string_of_int
(truncate f
)
279 Buffer.add_string
b s;
286 let cmd = Buffer.contents
(makecmd s l
) in
287 writecmd state.csock
cmd;
291 let rec f pn ph fh l
=
293 | (n, _
, h
) :: rest
->
294 let fh = fh + (n - pn
) * (ph
+ conf.interpagespace
) in
298 let fh = fh + ((ph
+ conf.interpagespace
) * (state.pagecount
- pn
)) in
301 let fh = f 0 0 0 state.pages
in
302 fh + (if conf.presentation
then conf.interpagespace
else -conf.interpagespace
);
305 let getpageyh pageno
=
306 let inc = if conf.presentation
then conf.interpagespace
else 0 in
307 let rec f pn ph y l
=
309 | (n, _
, h
) :: rest
->
312 y
+ (pageno
- pn
) * (ph
+ conf.interpagespace
), h
314 let y = y + (n - pn
) * (ph
+ conf.interpagespace
) in
318 y + (pageno
- pn
) * (ph
+ conf.interpagespace
) + inc, ph
323 let getpagey pageno
= fst
(getpageyh pageno
);;
326 let ips = conf.interpagespace
in
327 let rec f ~pageno ~pdimno ~prev ~vy ~py ~dy ~pdims ~cacheleft ~accu
=
328 if pageno
= state.pagecount
|| cacheleft
= 0
331 let ((_
, w
, h
) as curr
), rest
, pdimno
=
333 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
334 curr
, rest
, pdimno
+ 1
338 let pageno'
= pageno + 1 in
372 let accu = e :: accu in
380 ~cacheleft
:(pred cacheleft
)
386 let dy, py = if t < 0 then 0, py + h
+ ips else t, py + h
- vh in
398 if state.invalidated
= 0
421 ~cacheleft
:(cblen state.pagecache
)
424 state.maxy
<- calcheight ();
431 let y = state.y + incr
in
433 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
437 let getopaque pageno =
438 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
, state.rotate
))
439 with Not_found
-> None
442 let cache pageno opaque
=
443 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
, state.rotate
) opaque
446 let validopaque opaque
= String.length opaque
> 0;;
449 match getopaque l
.pageno with
450 | None
when not
state.rendering
->
451 state.rendering
<- true;
453 wcmd "render" [`i
(l
.pageno + 1)
461 let loadlayout layout =
462 let rec f all
= function
464 begin match getopaque l
.pageno with
465 | None
-> render l
; f false ls
466 | Some opaque
-> f (all
&& validopaque opaque
) ls
470 f (layout <> []) layout;
477 let evictedopaque = cbpeekw state.pagecache
in
478 List.exists
(fun l
->
479 match getopaque l
.pageno with
480 | Some opaque
when validopaque opaque
->
481 evictedopaque = opaque
485 if not
evictedvisible
487 let y = if state.y < state.h
then 0 else state.y - state.h
in
488 let pages = layout y (state.h
*3) in
489 List.iter
render pages;
494 let y = min
state.maxy
y in
495 let pages = layout y state.h
in
496 let ready = loadlayout pages in
497 state.ty
<- yratio y;
502 state.layout <- pages;
504 Glut.postRedisplay
();
508 state.layout <- pages;
510 Glut.postRedisplay
();
516 cbput state.hists
.nav
(yratio state.y);
517 cbrfollowlen state.hists
.nav
;
521 let y = cbget state.hists
.nav ~
-1 in
522 truncate
(y *. float state.maxy
)
526 let y, h
= getpageyh n in
528 gotoy (y + (truncate
(top
*. float h
)));
531 let gotopage1 n top
=
532 let y = getpagey n in
542 state.invalidated
<- state.invalidated
+ 1;
546 let c = c *. state.colorscale
in
554 match state.pages with
555 | [] -> yratio state.y
558 let d = state.h
- h
in
561 let rely = yratio state.y in
562 conf.interpagespace
<- ips;
565 let rely = yratio state.y in
566 conf.interpagespace
<- 2;
569 state.maxy
<- calcheight ();
570 gotoy (truncate
(float state.maxy
*. rely));
575 let m = float conf.margin in
576 let m = m *. (float w
/. 20.) in
577 let m = truncate
m in
578 if m*2 > (w
- conf.scrollw
) then 0 else m
581 let w = w - margin * 2 - conf.scrollw
in
584 GlMat.mode `modelview
;
585 GlMat.load_identity
();
586 GlMat.mode `projection
;
587 GlMat.load_identity
();
588 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
589 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
590 GlMat.scale3
(2.0 /. float w, 2.0 /. float state.h
, 1.0);
591 GlClear.color
(scalecolor 1.0);
592 GlClear.clear
[`color
];
595 wcmd "geometry" [`i
state.w; `i h
];
599 GlDraw.viewport
0 0 state.winw
state.h
;
600 GlDraw.color
(0.0, 0.0, 0.0);
601 let sw = float (conf.scrollw
- 1) *. float state.w /. float state.winw
in
603 (0.0, float (state.h
- 18))
604 (float state.w -. sw, float state.h
)
606 let font = Glut.BITMAP_8_BY_13
in
607 GlDraw.color
(1.0, 1.0, 1.0);
608 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
609 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
610 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
614 let len = String.length
state.text
in
615 match state.textentry
with
617 if len > 0 then showtext ' '
state.text
619 | Some
(c, text
, _
, _
, _
) ->
623 text ^
" [" ^
state.text ^
"]"
633 state.text
<- Printf.sprintf
"%c%s" c s;
634 Glut.postRedisplay
();
648 state.rects
<- state.rects1
;
649 Glut.postRedisplay
()
652 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
653 state.pagecount
<- n;
654 state.invalidated
<- state.invalidated
- 1;
655 if state.invalidated
= 0
659 let s = Scanf.sscanf
cmd "t %n"
660 (fun n -> String.sub
cmd n (String.length
cmd - n))
662 Glut.setWindowTitle
s
665 let s = Scanf.sscanf
cmd "T %n"
666 (fun n -> String.sub
cmd n (String.length
cmd - n))
668 if state.textentry
= None
675 Glut.postRedisplay
();
681 let s = Scanf.sscanf
cmd "V %n"
682 (fun n -> String.sub
cmd n (String.length
cmd - n))
688 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
689 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
690 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
691 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
693 let y = (getpagey pageno) + truncate y0
in
696 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
699 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
700 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
701 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
702 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
705 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
709 Scanf.sscanf
cmd "r %d %d %d %d %s"
710 (fun n w h r p
-> (n, w, h
, r
, p
))
712 Hashtbl.replace
state.pagemap
(n, w, r
) p
;
713 let opaque = cbpeekw state.pagecache
in
714 if validopaque opaque
718 (fun k v a
-> if v
= opaque then k else a
)
719 state.pagemap
(-1, -1, -1)
721 wcmd "free" [`
s opaque];
722 Hashtbl.remove
state.pagemap
k
724 cbput state.pagecache p
;
725 state.rendering
<- false;
727 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy
)))
729 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
732 else (ignore
(loadlayout state.layout); preload ())
736 let (n, w, h
) as pagelayout
=
737 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w, h
)
739 state.pages <- pagelayout
:: state.pages
742 let (l
, n, t, h
, pos
) =
743 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t, h
, pos
)
745 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
747 let l = String.length
s in
748 let b = Buffer.create
(String.length
s) in
755 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
758 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
767 let outline = (s, l, n, float t /. float h
) in
769 match state.outlines with
770 | Olist
outlines -> Olist
(outline :: outlines)
771 | Oarray _
-> Olist
[outline]
772 | Onarrow _
-> Olist
[outline]
774 state.outlines <- outlines
777 log "unknown cmd `%S'" cmd
780 let now = Unix.gettimeofday
;;
784 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
789 let y = state.y + conf.scrollincr
in
790 let y = if y >= state.maxy
then 0 else y in
796 let cmd = readcmd state.csock
in
803 let onhist cb
= function
804 | HCprev
-> cbget cb ~
-1
805 | HCnext
-> cbget cb
1
806 | HCfirst
-> cbget cb ~
-(cb
.rc)
807 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
810 let search pattern forward
=
811 if String.length pattern
> 0
814 match state.layout with
817 l.pageno, (l.pagey
+ if forward
then 0 else 0*l.pagevh
)
820 let b = makecmd "search"
821 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
823 Buffer.add_char
b '
,'
;
824 Buffer.add_string
b pattern
;
825 Buffer.add_char
b '
\000'
;
828 writecmd state.csock
cmd;
831 let intentry text key
=
832 let c = Char.unsafe_chr key
in
835 let s = "x" in s.[0] <- c;
836 let text = text ^
s in
840 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
845 let b = Buffer.create
(String.length
s + 1) in
846 Buffer.add_string
b s;
851 let textentry text key
=
852 let c = Char.unsafe_chr key
in
854 | _
when key
>= 32 && key
< 127 ->
855 let text = addchar text c in
859 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
864 state.rotate <- angle
;
866 wcmd "rotate" [`i angle
];
869 let optentry text key
=
870 let btos b = if b then "on" else "off" in
871 let c = Char.unsafe_chr key
in
875 try conf.scrollincr
<- int_of_string
s with exc
->
876 state.text <- Printf.sprintf
"bad integer `%s': %s"
877 s (Printexc.to_string exc
)
879 TEswitch
('#'
, "", None
, intentry, ondone)
884 Some
(int_of_string
s)
886 state.text <- Printf.sprintf
"bad integer `%s': %s"
887 s (Printexc.to_string exc
);
890 | Some angle
-> rotate angle
893 TEswitch
('^'
, "", None
, intentry, ondone)
896 conf.icase
<- not
conf.icase
;
897 TEdone
("case insensitive search " ^
(btos conf.icase
))
900 conf.preload <- not
conf.preload;
902 TEdone
("preload " ^
(btos conf.preload))
905 conf.verbose
<- not
conf.verbose
;
906 TEdone
("verbose " ^
(btos conf.verbose
))
909 conf.maxhfit
<- not
conf.maxhfit
;
910 state.maxy
<- state.maxy
+ (if conf.maxhfit
then -state.h
else state.h
);
911 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
914 conf.crophack
<- not
conf.crophack
;
915 TEdone
("crophack " ^
btos conf.crophack
)
918 conf.showall
<- not
conf.showall
;
919 TEdone
("showall " ^
btos conf.showall
)
922 conf.underinfo
<- not
conf.underinfo
;
923 TEdone
("underinfo " ^
btos conf.underinfo
)
928 conf.interpagespace
<- int_of_string
s;
929 let rely = yratio state.y in
930 state.maxy
<- calcheight ();
931 gotoy (truncate
(float state.maxy
*. rely));
933 state.text <- Printf.sprintf
"bad integer `%s': %s"
934 s (Printexc.to_string exc
)
936 TEswitch
('
%'
, "", None
, intentry, ondone)
939 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
943 let maxoutlinerows () = (state.h
- 31) / 16;;
945 let enterselector allowdel
outlines errmsg
=
946 if Array.length
outlines = 0
951 Glut.setCursor
Glut.CURSOR_INHERIT
;
953 match state.layout with
955 | {pageno=pageno} :: rest
-> pageno
959 if n = Array.length
outlines
962 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
963 if outlinepageno
>= pageno then n else loop (n+1)
968 Some
(allowdel
, active,
969 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
970 Glut.postRedisplay
();
974 let enteroutlinemode () =
976 match state.outlines with
979 let a = Array.of_list
(List.rev
l) in
980 state.outlines <- Oarray
a;
982 | Onarrow
(a, b) -> a
984 enterselector false outlines "Document has no outline";
987 let enterbookmarkmode () =
988 let bookmarks = Array.of_list
state.bookmarks in
989 enterselector true bookmarks "Document has no bookmarks (yet)";
993 let quickbookmark ?title
() =
994 match state.layout with
1000 let sec = Unix.gettimeofday
() in
1001 let tm = Unix.localtime
sec in
1002 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
1006 (tm.Unix.tm_year
+ 1900)
1009 | Some
title -> title
1012 (title, 0, l.pageno, float l.pagey
/. float l.pageh
) :: state.bookmarks
1016 state.fullscreen
<- None
;
1017 Glut.reshapeWindow
w h
;
1020 let opendoc path password
=
1023 state.password
<- password
;
1024 Hashtbl.clear
state.pagemap
;
1026 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1027 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1028 wcmd "geometry" [`i
state.w; `i
state.h
];
1031 let viewkeyboard ~key ~x ~
y =
1033 state.textentry <- te
;
1036 Glut.postRedisplay
()
1038 match state.textentry with
1040 let c = Char.chr key
in
1046 let y = getnav () in
1055 Glut.postRedisplay
()
1058 let ondone isforw
s =
1059 cbput state.hists
.pat
s;
1060 cbrfollowlen state.hists
.pat
;
1061 state.searchpattern
<- s;
1064 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1065 textentry, ondone (c ='
/'
)))
1067 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1068 let margin = max ~
-16 (conf.margin - 1) in
1069 conf.margin <- margin;
1070 reshape state.winw
state.h
;
1075 try int_of_string
s with exc
->
1076 state.text <- Printf.sprintf
"bad integer `%s': %s"
1077 s (Printexc.to_string exc
);
1083 state.text <- "page bias is now " ^ string_of_int
n;
1086 enttext (Some
('
+'
, "", None
, intentry, ondone))
1088 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1089 let margin = min
8 (conf.margin + 1) in
1090 conf.margin <- margin;
1091 if margin > 0 then state.x
<- 0;
1092 reshape state.winw
state.h
;
1098 enttext (Some
('
-'
, "", None
, optentry, ondone))
1100 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1103 reshape state.winw
state.h
1108 try int_of_string
s with exc
->
1109 state.text <- Printf.sprintf
"bad integer `%s': %s"
1110 s (Printexc.to_string exc
);
1116 cbput state.hists
.pag
(string_of_int
n);
1117 cbrfollowlen state.hists
.pag
;
1118 gotoy (getpagey (n + conf.pagebias
- 1))
1121 let pageentry text key
=
1122 match Char.unsafe_chr key
with
1123 | 'g'
-> TEdone
text
1124 | _
-> intentry text key
1126 let text = "x" in text.[0] <- c;
1127 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1131 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
1132 reshape state.winw
state.h
;
1135 conf.hlinks
<- not
conf.hlinks
;
1136 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1137 Glut.postRedisplay
()
1140 conf.autoscroll
<- not
conf.autoscroll
1143 conf.presentation
<- not
conf.presentation
;
1147 begin match state.fullscreen
with
1149 state.fullscreen
<- Some
(state.w, state.h
);
1152 state.fullscreen
<- None
;
1160 search state.searchpattern
true
1163 search state.searchpattern
false
1166 begin match state.layout with
1169 gotoy (getpagey l.pageno - conf.interpagespace
)
1173 begin match List.rev
state.layout with
1176 gotoy (clamp (l.pageh
- l.pagey
+ conf.interpagespace
))
1180 begin match state.layout with
1183 gotoy (clamp (-l.pageh
- conf.interpagespace
));
1188 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1190 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1192 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1196 else (100. *. (float state.y /. float maxy)) in
1199 Printf.sprintf
"Page %d of %d %.2f%%"
1200 (fn+1) state.pagecount
percent
1203 "Pages %d-%d of %d %.2f%%"
1204 (fn+1) (ln
+1) state.pagecount
percent
1209 begin match state.layout with
1212 doreshape l.pagew
l.pageh
;
1213 Glut.postRedisplay
();
1217 enterbookmarkmode ()
1221 match state.layout with
1224 (s, 0, l.pageno, float l.pagey
/. float l.pageh
)
1228 enttext (Some
('~'
, "", None
, textentry, ondone))
1232 showtext ' '
"Quick bookmark added";
1235 begin match state.layout with
1237 let a = getpagewh
l.pagedimno
in
1241 (truncate
(1.8 *. (a.(1) -. a.(0))),
1242 truncate
(1.2 *. (a.(3) -. a.(0))))
1244 (truncate
(a.(1) -. a.(0)),
1245 truncate
(a.(3) -. a.(0)))
1248 Glut.postRedisplay
();
1254 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1259 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1260 Glut.postRedisplay
()
1262 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1263 | 'j'
-> gotoy (clamp conf.scrollincr
)
1265 | '
r'
-> opendoc state.path
state.password
1268 vlog "huh? %d %c" key
(Char.chr key
);
1271 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1272 let len = String.length
text in
1275 state.textentry <- None
;
1276 Glut.postRedisplay
();
1279 let s = String.sub
text 0 (len - 1) in
1280 enttext (Some
(c, s, onhist, onkey
, ondone))
1283 | Some
(c, text, onhist, onkey
, ondone) ->
1284 begin match Char.unsafe_chr key
with
1287 state.textentry <- None
;
1288 Glut.postRedisplay
()
1291 state.textentry <- None
;
1292 Glut.postRedisplay
()
1295 begin match onkey
text key
with
1297 state.textentry <- None
;
1299 Glut.postRedisplay
()
1302 enttext (Some
(c, text, onhist, onkey
, ondone));
1305 state.textentry <- None
;
1306 Glut.postRedisplay
()
1309 state.textentry <- Some te
;
1310 Glut.postRedisplay
()
1315 let narrow outlines pattern
=
1316 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1320 let rec fold accu n =
1324 let (s, _
, _
, _
) as o
= outlines.(n) in
1326 if (try ignore
(Str.search_forward re
s 0); true
1327 with Not_found
-> false)
1333 let matched = fold [] (Array.length
outlines - 1) in
1334 if matched = [] then None
else Some
(Array.of_list
matched)
1337 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1338 let search active pattern incr
=
1341 if n = Array.length
outlines || n = -1
1344 let (s, _
, _
, _
) = outlines.(n) in
1346 (try ignore
(Str.search_forward re
s 0); true
1347 with Not_found
-> false)
1349 else loop (n + incr
)
1354 let re = Str.regexp_case_fold pattern
in
1360 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1363 if String.length qsearch
= 0
1366 state.outline <- None
;
1367 Glut.postRedisplay
();
1371 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1372 Glut.postRedisplay
();
1376 let incr = if key
= 18 then -1 else 1 in
1378 match search (active + incr) qsearch
incr with
1380 state.text <- qsearch ^
" [not found]";
1383 state.text <- qsearch
;
1384 active, firstof active
1386 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1387 Glut.postRedisplay
();
1390 let len = String.length qsearch
in
1397 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1400 let qsearch = String.sub
qsearch 0 (len - 1) in
1402 match search active qsearch ~
-1 with
1404 state.text <- qsearch ^
" [not found]";
1407 state.text <- qsearch;
1408 active, firstof active
1410 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1412 Glut.postRedisplay
()
1415 if active < Array.length
outlines
1417 let (_
, _
, n, t) = outlines.(active) in
1421 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1422 state.outline <- None
;
1423 Glut.postRedisplay
();
1425 | _
when key
>= 32 && key
< 127 ->
1426 let pattern = addchar qsearch (Char.chr key
) in
1428 match search active pattern 1 with
1430 state.text <- pattern ^
" [not found]";
1433 state.text <- pattern;
1434 active, firstof active
1436 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1437 Glut.postRedisplay
()
1439 | 14 when not allowdel
->
1440 let optoutlines = narrow outlines qsearch in
1441 begin match optoutlines with
1442 | None
-> state.text <- "can't narrow"
1444 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1445 match state.outlines with
1447 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1448 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1450 Glut.postRedisplay
()
1452 | 21 when not allowdel
->
1454 match state.outlines with
1457 let a = Array.of_list
(List.rev
l) in
1458 state.outlines <- Oarray
a;
1461 state.outlines <- Oarray
b;
1464 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1465 Glut.postRedisplay
()
1469 Some
(allowdel
, active, firstof active, outlines, qsearch);
1470 Glut.postRedisplay
()
1472 | 127 when allowdel
->
1473 let len = Array.length
outlines - 1 in
1476 state.outline <- None
;
1477 state.bookmarks <- [];
1480 let bookmarks = Array.init
len
1482 let i = if i >= active then i + 1 else i in
1493 Glut.postRedisplay
()
1495 | _
-> log "unknown key %d" key
1498 let keyboard ~key ~x ~
y =
1503 match state.outline with
1504 | None
-> viewkeyboard ~key ~x ~
y
1505 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1508 let special ~key ~x ~
y =
1509 match state.outline with
1511 begin match state.textentry with
1515 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1516 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1517 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1518 | Glut.KEY_PAGE_UP
->
1519 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1521 match state.layout with
1523 | l :: _
-> state.y - l.pagey
1526 | Glut.KEY_PAGE_DOWN
->
1527 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1529 match List.rev
state.layout with
1531 | l :: _
-> getpagey l.pageno
1534 | Glut.KEY_HOME
-> addnav (); 0
1537 state.maxy - (if conf.maxhfit
then state.h
else 0)
1539 | Glut.KEY_RIGHT
when conf.margin < 0 ->
1540 state.x
<- state.x
- 10;
1542 | Glut.KEY_LEFT
when conf.margin < 0 ->
1543 state.x
<- state.x
+ 10;
1548 if not
conf.verbose
then state.text <- "";
1551 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1554 | Glut.KEY_UP
-> onhist HCprev
1555 | Glut.KEY_DOWN
-> onhist HCnext
1556 | Glut.KEY_HOME
-> onhist HCfirst
1557 | Glut.KEY_END
-> onhist HClast
1560 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1561 Glut.postRedisplay
()
1566 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1567 let maxrows = maxoutlinerows () in
1569 let active = active + incr in
1570 let active = max
0 (min
active (Array.length
outlines - 1)) in
1574 let rows = active - first in
1575 if rows > maxrows then active - maxrows else first
1578 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1579 Glut.postRedisplay
()
1582 | Glut.KEY_UP
-> navigate ~
-1
1583 | Glut.KEY_DOWN
-> navigate 1
1584 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1585 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1588 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1589 Glut.postRedisplay
()
1592 let active = Array.length
outlines - 1 in
1593 let first = max
0 (active - maxrows) in
1594 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1595 Glut.postRedisplay
()
1600 let drawplaceholder l =
1601 GlDraw.color
(scalecolor 1.0);
1603 (0.0, float l.pagedispy
)
1604 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1607 and y = float (l.pagedispy
+ 13) in
1608 let font = Glut.BITMAP_8_BY_13
in
1609 GlDraw.color
(0.0, 0.0, 0.0);
1610 GlPix.raster_pos ~
x ~
y ();
1611 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1612 ("Loading " ^ string_of_int
l.pageno);
1615 let now () = Unix.gettimeofday
();;
1618 begin match getopaque l.pageno with
1619 | Some
opaque when validopaque opaque ->
1620 if state.textentry = None
1621 then GlDraw.color
(scalecolor 1.0)
1622 else GlDraw.color
(scalecolor 0.4);
1624 draw
(l.pagedispy
, l.pagew
, l.pagevh
, l.pagey
, conf.hlinks
)
1628 vlog "draw %f sec" d;
1633 l.pagedispy
+ l.pagevh
;
1636 let scrollindicator () =
1637 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1638 GlDraw.color
(0.64 , 0.64, 0.64);
1641 (float conf.scrollw
, float state.h
)
1643 GlDraw.color
(0.0, 0.0, 0.0);
1644 let sh = (float (maxy + state.h
) /. float state.h
) in
1645 let sh = float state.h
/. sh in
1646 let sh = max
sh (float conf.scrollh
) in
1649 if state.y = state.maxy
1651 else float state.y /. float maxy
1653 let position = (float state.h
-. sh) *. percent in
1656 if position +. sh > float state.h
1664 (float conf.scrollw
, position +. sh)
1668 let showsel margin =
1669 match state.mstate
with
1673 | Msel
((x0
, y0
), (x1
, y1
)) ->
1674 let rec loop = function
1676 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1677 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1679 match getopaque l.pageno with
1680 | Some
opaque when validopaque opaque ->
1681 let oy = -l.pagey
+ l.pagedispy
in
1683 (x0
- margin - state.x, y0
,
1684 x1
- margin - state.x, y1
) oy;
1695 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1696 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1698 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1700 if l.pageno = pageno
1702 let d = float (l.pagedispy
- l.pagey
) in
1703 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1704 GlDraw.begins `quads
;
1706 GlDraw.vertex2
(x0
, y0
+.d);
1707 GlDraw.vertex2
(x1
, y1
+.d);
1708 GlDraw.vertex2
(x2
, y2
+.d);
1709 GlDraw.vertex2
(x3
, y3
+.d);
1719 let showoutline = function
1721 | Some
(allowdel
, active, first, outlines, qsearch) ->
1723 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1724 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1725 GlDraw.rect
(0., 0.) (float state.w, float state.h
);
1728 GlDraw.color
(1., 1., 1.);
1729 let font = Glut.BITMAP_9_BY_15
in
1730 let draw_string x y s =
1731 GlPix.raster_pos ~
x ~
y ();
1732 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1735 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1738 let (s, l, _
, _
) = outlines.(row
) in
1739 let y = (row
- first) * 16 in
1744 GlDraw.polygon_mode `both `line
;
1745 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1746 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1747 let sw = float (conf.scrollw
- 1) *. float state.w /. float state.winw
in
1748 GlDraw.rect
(0., float (y + 1))
1749 ((float state.w -. sw), float (y + 18));
1750 GlDraw.polygon_mode `both `fill
;
1752 GlDraw.color
(1., 1., 1.);
1754 draw_string (float x) (float (y + 16)) s;
1762 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1763 GlDraw.viewport
margin 0 state.w state.h
;
1764 GlClear.color
(scalecolor 0.5);
1765 GlClear.clear
[`color
];
1768 let x = float state.x in
1769 GlMat.translate ~
x ();
1773 Gl.enable `scissor_test
;
1774 GlMisc.scissor
0 0 (state.winw
- conf.scrollw
) state.h
;
1776 let _lasty = List.fold_left
drawpage 0 (state.layout) in
1779 Gl.disable `scissor_test
1783 let x = -.float state.x in
1784 GlMat.translate ~
x ();
1787 GlDraw.viewport
(state.winw
- conf.scrollw
) 0 state.winw
state.h
;
1790 GlDraw.viewport
0 0 state.winw
state.h
;
1791 showoutline state.outline;
1793 Glut.swapBuffers
();
1797 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1798 let x = x - margin - state.x in
1799 let rec f = function
1801 begin match getopaque l.pageno with
1802 | Some
opaque when validopaque opaque ->
1803 let y = y - l.pagedispy
in
1806 let y = l.pagey
+ y in
1807 match whatsunder
opaque x y with
1820 let mouse ~button ~bstate ~
x ~
y =
1822 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1830 let incr = incr * 2 in
1831 let y = clamp incr in
1834 | Glut.LEFT_BUTTON
when state.outline = None
1835 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1836 if bstate
= Glut.DOWN
1838 state.mstate
<- Mpan
(x, y)
1840 state.mstate
<- Mnone
1842 | Glut.LEFT_BUTTON
when state.outline = None
->
1843 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1844 begin match dest with
1845 | Ulinkgoto
(pageno, top
) ->
1848 gotopage1 pageno top
1853 | Unone
when bstate
= Glut.DOWN
->
1854 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1855 state.mstate
<- Mpan
(x, y);
1857 | Unone
| Utext _
->
1858 if bstate
= Glut.DOWN
1860 if state.rotate mod 360 = 0
1862 state.mstate
<- Msel
((x, y), (x, y));
1863 Glut.postRedisplay
()
1867 match state.mstate
with
1871 Glut.setCursor
Glut.CURSOR_INHERIT
;
1872 state.mstate
<- Mnone
1874 | Msel
((x0
, y0
), (x1
, y1
)) ->
1876 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1877 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1879 match getopaque l.pageno with
1880 | Some
opaque when validopaque opaque ->
1884 List.iter
f state.layout;
1885 copysel
""; (* ugly *)
1886 Glut.setCursor
Glut.CURSOR_INHERIT
;
1887 state.mstate
<- Mnone
;
1894 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1897 if state.outline = None
1899 match state.mstate
with
1905 state.mstate
<- Mpan
(x, y);
1906 if conf.margin < 0 then state.x <- state.x + dx;
1911 state.mstate
<- Msel
(a, (x, y));
1912 Glut.postRedisplay
()
1916 if state.outline = None
1918 match state.mstate
with
1920 begin match getunder x y with
1921 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
1923 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
1924 Glut.setCursor
Glut.CURSOR_INFO
1925 | Ulinkgoto
(page
, y) ->
1926 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
1927 Glut.setCursor
Glut.CURSOR_INFO
1929 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
1930 Glut.setCursor
Glut.CURSOR_TEXT
1933 | Mpan _
| Msel _
->
1940 if Sys.os_type
= "Win32"
1942 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1944 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1946 Filename.concat
home "llpp"
1950 let ic = open_in_bin
statepath in
1951 let hash = input_value
ic in
1957 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1964 match state.fullscreen
with
1965 | None
-> state.winw
, state.h
1968 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h
);
1969 let oc = open_out_bin
statepath in
1970 output_value
oc pstate
1974 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1979 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1982 state.bookmarks <- statebookmarks;
1983 with Not_found
-> ()
1985 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1989 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
1990 (fun s -> state.path
<- s)
1991 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
1994 if String.length
state.path
= 0
1995 then (prerr_endline
"filename missing"; exit
1)
2000 let _ = Glut.init
Sys.argv
in
2001 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2002 let () = Glut.initWindowSize
state.w state.h
in
2003 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
2006 if Sys.os_type
= "Unix"
2008 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2010 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2011 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2012 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2013 Unix.bind
sock addr;
2015 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2016 Unix.connect
csock addr;
2017 let ssock, _ = Unix.accept
sock in
2020 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2021 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2025 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2029 let () = Glut.displayFunc
display in
2030 let () = Glut.reshapeFunc
reshape in
2031 let () = Glut.keyboardFunc
keyboard in
2032 let () = Glut.specialFunc
special in
2033 let () = Glut.idleFunc
(Some
idle) in
2034 let () = Glut.mouseFunc
mouse in
2035 let () = Glut.motionFunc
motion in
2036 let () = Glut.passiveMotionFunc
pmotion in
2039 state.csock <- csock;
2040 state.ssock <- ssock;
2041 state.text <- "Opening " ^
name;
2042 writecmd state.csock ("open " ^
state.path ^
"\000" ^
state.password ^
"\000");
2046 let rec handlelablglutbug () =
2049 with Glut.BadEnum
"key in special_of_int" ->
2050 showtext '
!'
" LablGlut bug: special key not recognized";
2051 handlelablglutbug ()
2053 handlelablglutbug ();