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";;
19 type mstate
= Msel
of ((int * int) * (int * int)) | Mnone
;;
29 type textentry
= (char
* string * onhist
option * onkey
* ondone
)
30 and onkey
= string -> int -> te
31 and ondone
= string -> unit
32 and onhist
= histcmd
-> string
33 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
38 | TEswitch
of textentry
42 { store
= Array.create n v
49 let cblen b
= Array.length b
.store
;;
54 b
.wc
<- (b
.wc
+ 1) mod len;
55 b
.len <- min
(b
.len + 1) len;
58 let cbpeekw b
= b
.store
.(b
.wc
);;
64 let rc = b
.rc + dir
in
65 let rc = if rc = -1 then b
.len - 1 else rc in
66 let rc = if rc = b
.len then 0 else rc in
77 Array.fill b
.store
0 (Array.length b
.store
) v
;
92 { mutable scrollw
: int
93 ; mutable scrollh
: int
94 ; mutable icase
: bool
95 ; mutable preload
: bool
96 ; mutable pagebias
: int
97 ; mutable verbose
: bool
98 ; mutable scrollincr
: int
99 ; mutable maxhfit
: bool
100 ; mutable crophack
: bool
101 ; mutable autoscroll
: bool
102 ; mutable showall
: bool
103 ; mutable hlinks
: bool
104 ; mutable underinfo
: bool
105 ; mutable interpagespace
: int
106 ; mutable margin
: int
107 ; mutable presentation
: bool
111 type outline
= string * int * int * float;;
113 | Oarray
of outline array
114 | Olist
of outline list
115 | Onarrow
of outline array
* outline array
118 type rect
= (float * float * float * float * float * float * float * float);;
121 { mutable csock
: Unix.file_descr
122 ; mutable ssock
: Unix.file_descr
126 ; mutable rotate
: int
131 ; mutable layout
: layout list
132 ; pagemap
: ((int * int * int), string) Hashtbl.t
133 ; mutable pages
: (int * int * int) list
134 ; mutable pagecount
: int
135 ; pagecache
: string circbuf
136 ; mutable rendering
: bool
137 ; mutable mstate
: mstate
138 ; mutable searchpattern
: string
139 ; mutable rects
: (int * int * rect
) list
140 ; mutable rects1
: (int * int * rect
) list
141 ; mutable text
: string
142 ; mutable fullscreen
: (int * int) option
143 ; mutable textentry
: textentry
option
144 ; mutable outlines
: outlines
145 ; mutable outline
: (bool * int * int * outline array
* string) option
146 ; mutable bookmarks
: outline list
147 ; mutable path
: string
148 ; mutable password
: string
149 ; mutable invalidated
: int
150 ; mutable colorscale
: float
154 { pat
: string circbuf
155 ; pag
: string circbuf
156 ; nav
: float circbuf
176 ; presentation
= false
192 ; pagemap
= Hashtbl.create
10
193 ; pagecache
= cbnew 10 ""
204 ; outlines
= Olist
[]
211 { nav
= cbnew 100 0.0
222 Printf.kprintf prerr_endline fmt
224 Printf.kprintf ignore fmt
228 let len = String.length s
in
230 let b = Buffer.create
n in
231 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
232 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
233 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
234 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
235 Buffer.add_string
b s
;
236 let s'
= Buffer.contents
b in
237 let n'
= Unix.write fd
s'
0 n in
238 if n'
!= n then failwith
"write failed";
243 let n = Unix.read fd
s 0 4 in
244 if n != 4 then failwith
"incomplete read(len)";
246 lor (Char.code
s.[0] lsl 24)
247 lor (Char.code
s.[1] lsl 16)
248 lor (Char.code
s.[2] lsl 8)
249 lor (Char.code
s.[3] lsl 0)
251 let s = String.create
len in
252 let n = Unix.read fd
s 0 len in
253 if n != len then failwith
"incomplete read(data)";
260 else float y
/. float state.maxy
264 let b = Buffer.create
10 in
265 Buffer.add_string
b s;
266 let rec combine = function
269 Buffer.add_char
b ' '
;
272 | `
b b -> if b then "1" else "0"
274 | `i i
-> string_of_int i
275 | `f f
-> string_of_float f
276 | `I f
-> string_of_int
(truncate f
)
278 Buffer.add_string
b s;
285 let cmd = Buffer.contents
(makecmd s l
) in
286 writecmd state.csock
cmd;
290 let rec f pn ph fh l
=
292 | (n, _
, h
) :: rest
->
293 let fh = fh + (n - pn
) * (ph
+ conf.interpagespace
) in
297 let fh = fh + ((ph
+ conf.interpagespace
) * (state.pagecount
- pn
)) in
300 let fh = f 0 0 0 state.pages
in
301 fh + (if conf.presentation
then conf.interpagespace
else 0);
304 let getpageyh pageno
=
305 let inc = if conf.presentation
then conf.interpagespace
else 0 in
306 let rec f pn ph y l
=
308 | (n, _
, h
) :: rest
->
311 y
+ (pageno
- pn
) * (ph
+ conf.interpagespace
), h
313 let y = y + (n - pn
) * (ph
+ conf.interpagespace
) in
317 y + (pageno
- pn
) * (ph
+ conf.interpagespace
) + inc, ph
322 let getpagey pageno
= fst
(getpageyh pageno
);;
325 let ips = conf.interpagespace
in
326 let rec f ~pageno ~pdimno ~prev ~vy ~py ~dy ~pdims ~cacheleft ~accu
=
327 if pageno
= state.pagecount
|| cacheleft
= 0
330 let ((_
, w
, h
) as curr
), rest
, pdimno
=
332 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
333 curr
, rest
, pdimno
+ 1
337 let pageno'
= pageno + 1 in
371 let accu = e :: accu in
379 ~cacheleft
:(pred cacheleft
)
385 let dy, py = if t < 0 then 0, py + h
+ ips else t, py + h
- vh in
397 if state.invalidated
= 0
420 ~cacheleft
:(cblen state.pagecache
)
423 state.maxy
<- calcheight ();
430 let y = state.y + incr
in
432 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
436 let getopaque pageno =
437 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
, state.rotate
))
438 with Not_found
-> None
441 let cache pageno opaque
=
442 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
, state.rotate
) opaque
445 let validopaque opaque
= String.length opaque
> 0;;
448 match getopaque l
.pageno with
449 | None
when not
state.rendering
->
450 state.rendering
<- true;
452 wcmd "render" [`i
(l
.pageno + 1)
460 let loadlayout layout =
461 let rec f all
= function
463 begin match getopaque l
.pageno with
464 | None
-> render l
; f false ls
465 | Some opaque
-> f (all
&& validopaque opaque
) ls
469 f (layout <> []) layout;
476 let evictedopaque = cbpeekw state.pagecache
in
477 List.exists
(fun l
->
478 match getopaque l
.pageno with
479 | Some opaque
when validopaque opaque
->
480 evictedopaque = opaque
484 if not
evictedvisible
486 let y = if state.y < state.h
then 0 else state.y - state.h
in
487 let pages = layout y (state.h
*3) in
488 List.iter
render pages;
493 let y = min
state.maxy
y in
494 let pages = layout y state.h
in
495 let ready = loadlayout pages in
496 state.ty
<- yratio y;
501 state.layout <- pages;
503 Glut.postRedisplay
();
507 state.layout <- pages;
509 Glut.postRedisplay
();
515 cbput state.hists
.nav
(yratio state.y);
516 cbrfollowlen state.hists
.nav
;
520 let y = cbget state.hists
.nav ~
-1 in
521 truncate
(y *. float state.maxy
)
525 let y, h
= getpageyh n in
527 gotoy (y + (truncate
(top
*. float h
)));
530 let gotopage1 n top
=
531 let y = getpagey n in
541 state.invalidated
<- state.invalidated
+ 1;
545 let c = c *. state.colorscale
in
553 match state.pages with
554 | [] -> yratio state.y
557 let d = state.h
- h
in
560 let rely = yratio state.y in
561 conf.interpagespace
<- ips;
564 let rely = yratio state.y in
565 conf.interpagespace
<- 2;
568 state.maxy
<- calcheight ();
569 gotoy (truncate
(float state.maxy
*. rely));
574 let m = float conf.margin in
575 let m = m *. (float w
/. 20.) in
576 let m = truncate
m in
577 if m*2 > (w
- conf.scrollw
) then 0 else m
580 let w = w - margin * 2 - conf.scrollw
in
583 GlMat.mode `modelview
;
584 GlMat.load_identity
();
585 GlMat.mode `projection
;
586 GlMat.load_identity
();
587 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
588 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
589 GlMat.scale3
(2.0 /. float w, 2.0 /. float state.h
, 1.0);
590 GlClear.color
(scalecolor 1.0);
591 GlClear.clear
[`color
];
594 wcmd "geometry" [`i
state.w; `i h
];
598 GlDraw.viewport
0 0 state.winw
state.h
;
599 GlDraw.color
(0.0, 0.0, 0.0);
600 let sw = float (conf.scrollw
- 1) *. float state.w /. float state.winw
in
602 (0.0, float (state.h
- 18))
603 (float state.w -. sw, float state.h
)
605 let font = Glut.BITMAP_8_BY_13
in
606 GlDraw.color
(1.0, 1.0, 1.0);
607 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
608 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
609 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
613 let len = String.length
state.text
in
614 match state.textentry
with
616 if len > 0 then showtext ' '
state.text
618 | Some
(c, text
, _
, _
, _
) ->
622 text ^
" [" ^
state.text ^
"]"
632 state.text
<- Printf.sprintf
"%c%s" c s;
633 Glut.postRedisplay
();
647 state.rects
<- state.rects1
;
648 Glut.postRedisplay
()
651 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
652 state.pagecount
<- n;
653 state.invalidated
<- state.invalidated
- 1;
654 if state.invalidated
= 0
658 let s = Scanf.sscanf
cmd "t %n"
659 (fun n -> String.sub
cmd n (String.length
cmd - n))
661 Glut.setWindowTitle
s
664 let s = Scanf.sscanf
cmd "T %n"
665 (fun n -> String.sub
cmd n (String.length
cmd - n))
667 if state.textentry
= None
674 Glut.postRedisplay
();
680 let s = Scanf.sscanf
cmd "V %n"
681 (fun n -> String.sub
cmd n (String.length
cmd - n))
687 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
688 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
689 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
690 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
692 let y = (getpagey pageno) + truncate y0
in
695 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
698 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
699 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
700 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
701 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
704 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
708 Scanf.sscanf
cmd "r %d %d %d %d %s"
709 (fun n w h r p
-> (n, w, h
, r
, p
))
711 Hashtbl.replace
state.pagemap
(n, w, r
) p
;
712 let opaque = cbpeekw state.pagecache
in
713 if validopaque opaque
717 (fun k v a
-> if v
= opaque then k else a
)
718 state.pagemap
(-1, -1, -1)
720 wcmd "free" [`
s opaque];
721 Hashtbl.remove
state.pagemap
k
723 cbput state.pagecache p
;
724 state.rendering
<- false;
726 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy
)))
728 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
731 else (ignore
(loadlayout state.layout); preload ())
735 let (n, w, h
) as pagelayout
=
736 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w, h
)
738 state.pages <- pagelayout
:: state.pages
741 let (l
, n, t, h
, pos
) =
742 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t, h
, pos
)
744 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
746 let l = String.length
s in
747 let b = Buffer.create
(String.length
s) in
754 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
757 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
766 let outline = (s, l, n, float t /. float h
) in
768 match state.outlines with
769 | Olist
outlines -> Olist
(outline :: outlines)
770 | Oarray _
-> Olist
[outline]
771 | Onarrow _
-> Olist
[outline]
773 state.outlines <- outlines
776 log "unknown cmd `%S'" cmd
779 let now = Unix.gettimeofday
;;
783 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
788 let y = state.y + conf.scrollincr
in
789 let y = if y >= state.maxy
then 0 else y in
795 let cmd = readcmd state.csock
in
802 let onhist cb
= function
803 | HCprev
-> cbget cb ~
-1
804 | HCnext
-> cbget cb
1
805 | HCfirst
-> cbget cb ~
-(cb
.rc)
806 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
809 let search pattern forward
=
810 if String.length pattern
> 0
813 match state.layout with
816 l.pageno, (l.pagey
+ if forward
then 0 else 0*l.pagevh
)
819 let b = makecmd "search"
820 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
822 Buffer.add_char
b '
,'
;
823 Buffer.add_string
b pattern
;
824 Buffer.add_char
b '
\000'
;
827 writecmd state.csock
cmd;
830 let intentry text key
=
831 let c = Char.unsafe_chr key
in
834 let s = "x" in s.[0] <- c;
835 let text = text ^
s in
839 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
844 let b = Buffer.create
(String.length
s + 1) in
845 Buffer.add_string
b s;
850 let textentry text key
=
851 let c = Char.unsafe_chr key
in
853 | _
when key
>= 32 && key
< 127 ->
854 let text = addchar text c in
858 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
863 state.rotate <- angle
;
865 wcmd "rotate" [`i angle
];
868 let optentry text key
=
869 let btos b = if b then "on" else "off" in
870 let c = Char.unsafe_chr key
in
874 try conf.scrollincr
<- int_of_string
s with exc
->
875 state.text <- Printf.sprintf
"bad integer `%s': %s"
876 s (Printexc.to_string exc
)
878 TEswitch
('#'
, "", None
, intentry, ondone)
883 Some
(int_of_string
s)
885 state.text <- Printf.sprintf
"bad integer `%s': %s"
886 s (Printexc.to_string exc
);
889 | Some angle
-> rotate angle
892 TEswitch
('^'
, "", None
, intentry, ondone)
895 conf.icase
<- not
conf.icase
;
896 TEdone
("case insensitive search " ^
(btos conf.icase
))
899 conf.preload <- not
conf.preload;
901 TEdone
("preload " ^
(btos conf.preload))
904 conf.verbose
<- not
conf.verbose
;
905 TEdone
("verbose " ^
(btos conf.verbose
))
908 conf.maxhfit
<- not
conf.maxhfit
;
909 state.maxy
<- state.maxy
+ (if conf.maxhfit
then -state.h
else state.h
);
910 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
913 conf.crophack
<- not
conf.crophack
;
914 TEdone
("crophack " ^
btos conf.crophack
)
917 conf.showall
<- not
conf.showall
;
918 TEdone
("showall " ^
btos conf.showall
)
921 conf.underinfo
<- not
conf.underinfo
;
922 TEdone
("underinfo " ^
btos conf.underinfo
)
927 conf.interpagespace
<- int_of_string
s;
928 let rely = yratio state.y in
929 state.maxy
<- calcheight ();
930 gotoy (truncate
(float state.maxy
*. rely));
932 state.text <- Printf.sprintf
"bad integer `%s': %s"
933 s (Printexc.to_string exc
)
935 TEswitch
('
%'
, "", None
, intentry, ondone)
938 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
942 let maxoutlinerows () = (state.h
- 31) / 16;;
944 let enterselector allowdel
outlines errmsg
=
945 if Array.length
outlines = 0
950 Glut.setCursor
Glut.CURSOR_INHERIT
;
952 match state.layout with
954 | {pageno=pageno} :: rest
-> pageno
958 if n = Array.length
outlines
961 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
962 if outlinepageno
>= pageno then n else loop (n+1)
967 Some
(allowdel
, active,
968 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
969 Glut.postRedisplay
();
973 let enteroutlinemode () =
975 match state.outlines with
978 let a = Array.of_list
(List.rev
l) in
979 state.outlines <- Oarray
a;
981 | Onarrow
(a, b) -> a
983 enterselector false outlines "Document has no outline";
986 let enterbookmarkmode () =
987 let bookmarks = Array.of_list
state.bookmarks in
988 enterselector true bookmarks "Document has no bookmarks (yet)";
992 let quickbookmark ?title
() =
993 match state.layout with
999 let sec = Unix.gettimeofday
() in
1000 let tm = Unix.localtime
sec in
1001 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
1005 (tm.Unix.tm_year
+ 1900)
1008 | Some
title -> title
1011 (title, 0, l.pageno, float l.pagey
/. float l.pageh
) :: state.bookmarks
1015 state.fullscreen
<- None
;
1016 Glut.reshapeWindow
w h
;
1019 let opendoc path password
=
1022 state.password
<- password
;
1023 Hashtbl.clear
state.pagemap
;
1025 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1026 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1027 wcmd "geometry" [`i
state.w; `i
state.h
];
1030 let viewkeyboard ~key ~x ~
y =
1032 state.textentry <- te
;
1035 Glut.postRedisplay
()
1037 match state.textentry with
1039 let c = Char.chr key
in
1045 let y = getnav () in
1054 Glut.postRedisplay
()
1057 let ondone isforw
s =
1058 cbput state.hists
.pat
s;
1059 cbrfollowlen state.hists
.pat
;
1060 state.searchpattern
<- s;
1063 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1064 textentry, ondone (c ='
/'
)))
1067 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1069 let margin = max ~
-16 (conf.margin - 1) in
1070 conf.margin <- margin;
1071 reshape state.winw
state.h
;
1076 try int_of_string
s with exc
->
1077 state.text <- Printf.sprintf
"bad integer `%s': %s"
1078 s (Printexc.to_string exc
);
1084 state.text <- "page bias is now " ^ string_of_int
n;
1087 enttext (Some
('
+'
, "", None
, intentry, ondone))
1090 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1092 let margin = min
8 (conf.margin + 1) in
1093 conf.margin <- margin;
1094 reshape state.winw
state.h
;
1100 enttext (Some
('
-'
, "", None
, optentry, ondone))
1102 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1105 reshape state.winw
state.h
1110 try int_of_string
s with exc
->
1111 state.text <- Printf.sprintf
"bad integer `%s': %s"
1112 s (Printexc.to_string exc
);
1118 cbput state.hists
.pag
(string_of_int
n);
1119 cbrfollowlen state.hists
.pag
;
1120 gotoy (getpagey (n + conf.pagebias
- 1))
1123 let pageentry text key
=
1124 match Char.unsafe_chr key
with
1125 | 'g'
-> TEdone
text
1126 | _
-> intentry text key
1128 let text = "x" in text.[0] <- c;
1129 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1133 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
1134 reshape state.winw
state.h
;
1137 conf.hlinks
<- not
conf.hlinks
;
1138 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1139 Glut.postRedisplay
()
1142 conf.autoscroll
<- not
conf.autoscroll
1145 conf.presentation
<- not
conf.presentation
;
1149 begin match state.fullscreen
with
1151 state.fullscreen
<- Some
(state.w, state.h
);
1154 state.fullscreen
<- None
;
1162 search state.searchpattern
true
1165 search state.searchpattern
false
1168 begin match state.layout with
1171 gotoy (getpagey l.pageno - conf.interpagespace
)
1175 begin match List.rev
state.layout with
1178 gotoy (clamp (l.pageh
- l.pagey
+ conf.interpagespace
))
1182 begin match state.layout with
1185 gotoy (clamp (-l.pageh
- conf.interpagespace
));
1190 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1192 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1194 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1198 else (100. *. (float state.y /. float maxy)) in
1201 Printf.sprintf
"Page %d of %d %.2f%%"
1202 (fn+1) state.pagecount
percent
1205 "Pages %d-%d of %d %.2f%%"
1206 (fn+1) (ln
+1) state.pagecount
percent
1211 begin match state.layout with
1214 doreshape l.pagew
l.pageh
;
1215 Glut.postRedisplay
();
1219 enterbookmarkmode ()
1223 match state.layout with
1226 (s, 0, l.pageno, float l.pagey
/. float l.pageh
)
1230 enttext (Some
('~'
, "", None
, textentry, ondone))
1234 showtext ' '
"Quick bookmark added";
1237 begin match state.layout with
1239 let a = getpagewh
l.pagedimno
in
1243 (truncate
(1.8 *. (a.(1) -. a.(0))),
1244 truncate
(1.2 *. (a.(3) -. a.(0))))
1246 (truncate
(a.(1) -. a.(0)),
1247 truncate
(a.(3) -. a.(0)))
1250 Glut.postRedisplay
();
1256 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1261 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1262 Glut.postRedisplay
()
1264 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1265 | 'j'
-> gotoy (clamp conf.scrollincr
)
1267 | '
r'
-> opendoc state.path
state.password
1270 vlog "huh? %d %c" key
(Char.chr key
);
1273 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1274 let len = String.length
text in
1277 state.textentry <- None
;
1278 Glut.postRedisplay
();
1281 let s = String.sub
text 0 (len - 1) in
1282 enttext (Some
(c, s, onhist, onkey
, ondone))
1285 | Some
(c, text, onhist, onkey
, ondone) ->
1286 begin match Char.unsafe_chr key
with
1289 state.textentry <- None
;
1290 Glut.postRedisplay
()
1293 state.textentry <- None
;
1294 Glut.postRedisplay
()
1297 begin match onkey
text key
with
1299 state.textentry <- None
;
1301 Glut.postRedisplay
()
1304 enttext (Some
(c, text, onhist, onkey
, ondone));
1307 state.textentry <- None
;
1308 Glut.postRedisplay
()
1311 state.textentry <- Some te
;
1312 Glut.postRedisplay
()
1317 let narrow outlines pattern
=
1318 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1322 let rec fold accu n =
1326 let (s, _
, _
, _
) as o
= outlines.(n) in
1328 if (try ignore
(Str.search_forward re
s 0); true
1329 with Not_found
-> false)
1335 let matched = fold [] (Array.length
outlines - 1) in
1336 if matched = [] then None
else Some
(Array.of_list
matched)
1339 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1340 let search active pattern incr
=
1343 if n = Array.length
outlines || n = -1
1346 let (s, _
, _
, _
) = outlines.(n) in
1348 (try ignore
(Str.search_forward re
s 0); true
1349 with Not_found
-> false)
1351 else loop (n + incr
)
1356 let re = Str.regexp_case_fold pattern
in
1362 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1365 if String.length qsearch
= 0
1368 state.outline <- None
;
1369 Glut.postRedisplay
();
1373 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1374 Glut.postRedisplay
();
1378 let incr = if key
= 18 then -1 else 1 in
1380 match search (active + incr) qsearch
incr with
1382 state.text <- qsearch ^
" [not found]";
1385 state.text <- qsearch
;
1386 active, firstof active
1388 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1389 Glut.postRedisplay
();
1392 let len = String.length qsearch
in
1399 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1402 let qsearch = String.sub
qsearch 0 (len - 1) in
1404 match search active qsearch ~
-1 with
1406 state.text <- qsearch ^
" [not found]";
1409 state.text <- qsearch;
1410 active, firstof active
1412 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1414 Glut.postRedisplay
()
1417 if active < Array.length
outlines
1419 let (_
, _
, n, t) = outlines.(active) in
1423 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1424 state.outline <- None
;
1425 Glut.postRedisplay
();
1427 | _
when key
>= 32 && key
< 127 ->
1428 let pattern = addchar qsearch (Char.chr key
) in
1430 match search active pattern 1 with
1432 state.text <- pattern ^
" [not found]";
1435 state.text <- pattern;
1436 active, firstof active
1438 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1439 Glut.postRedisplay
()
1441 | 14 when not allowdel
->
1442 let optoutlines = narrow outlines qsearch in
1443 begin match optoutlines with
1444 | None
-> state.text <- "can't narrow"
1446 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1447 match state.outlines with
1449 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1450 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1452 Glut.postRedisplay
()
1454 | 21 when not allowdel
->
1456 match state.outlines with
1459 let a = Array.of_list
(List.rev
l) in
1460 state.outlines <- Oarray
a;
1463 state.outlines <- Oarray
b;
1466 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1467 Glut.postRedisplay
()
1471 Some
(allowdel
, active, firstof active, outlines, qsearch);
1472 Glut.postRedisplay
()
1474 | 127 when allowdel
->
1475 let len = Array.length
outlines - 1 in
1478 state.outline <- None
;
1479 state.bookmarks <- [];
1482 let bookmarks = Array.init
len
1484 let i = if i >= active then i + 1 else i in
1495 Glut.postRedisplay
()
1497 | _
-> log "unknown key %d" key
1500 let keyboard ~key ~x ~
y =
1505 match state.outline with
1506 | None
-> viewkeyboard ~key ~x ~
y
1507 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1510 let special ~key ~x ~
y =
1511 match state.outline with
1513 begin match state.textentry with
1517 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1518 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1519 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1520 | Glut.KEY_PAGE_UP
->
1521 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1523 match state.layout with
1525 | l :: _
-> state.y - l.pagey
1528 | Glut.KEY_PAGE_DOWN
->
1529 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1531 match List.rev
state.layout with
1533 | l :: _
-> getpagey l.pageno
1536 | Glut.KEY_HOME
-> addnav (); 0
1539 state.maxy - (if conf.maxhfit
then state.h
else 0)
1541 | Glut.KEY_RIGHT
when conf.margin < 0 ->
1542 state.x
<- state.x
- 10;
1544 | Glut.KEY_LEFT
when conf.margin < 0 ->
1545 state.x
<- state.x
+ 10;
1550 if not
conf.verbose
then state.text <- "";
1553 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1556 | Glut.KEY_UP
-> onhist HCprev
1557 | Glut.KEY_DOWN
-> onhist HCnext
1558 | Glut.KEY_HOME
-> onhist HCfirst
1559 | Glut.KEY_END
-> onhist HClast
1562 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1563 Glut.postRedisplay
()
1568 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1569 let maxrows = maxoutlinerows () in
1571 let active = active + incr in
1572 let active = max
0 (min
active (Array.length
outlines - 1)) in
1576 let rows = active - first in
1577 if rows > maxrows then active - maxrows else first
1580 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1581 Glut.postRedisplay
()
1584 | Glut.KEY_UP
-> navigate ~
-1
1585 | Glut.KEY_DOWN
-> navigate 1
1586 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1587 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1590 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1591 Glut.postRedisplay
()
1594 let active = Array.length
outlines - 1 in
1595 let first = max
0 (active - maxrows) in
1596 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1597 Glut.postRedisplay
()
1602 let drawplaceholder l =
1603 GlDraw.color
(scalecolor 1.0);
1605 (0.0, float l.pagedispy
)
1606 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1609 and y = float (l.pagedispy
+ 13) in
1610 let font = Glut.BITMAP_8_BY_13
in
1611 GlDraw.color
(0.0, 0.0, 0.0);
1612 GlPix.raster_pos ~
x ~
y ();
1613 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1614 ("Loading " ^ string_of_int
l.pageno);
1617 let now () = Unix.gettimeofday
();;
1620 begin match getopaque l.pageno with
1621 | Some
opaque when validopaque opaque ->
1622 if state.textentry = None
1623 then GlDraw.color
(scalecolor 1.0)
1624 else GlDraw.color
(scalecolor 0.4);
1626 draw
(l.pagedispy
, l.pagew
, l.pagevh
, l.pagey
, conf.hlinks
)
1630 vlog "draw %f sec" d;
1635 l.pagedispy
+ l.pagevh
;
1638 let scrollindicator () =
1639 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1640 GlDraw.color
(0.64 , 0.64, 0.64);
1643 (float conf.scrollw
, float state.h
)
1645 GlDraw.color
(0.0, 0.0, 0.0);
1646 let sh = (float (maxy + state.h
) /. float state.h
) in
1647 let sh = float state.h
/. sh in
1648 let sh = max
sh (float conf.scrollh
) in
1651 if state.y = state.maxy
1653 else float state.y /. float maxy
1655 let position = (float state.h
-. sh) *. percent in
1658 if position +. sh > float state.h
1666 (float conf.scrollw
, position +. sh)
1670 let showsel margin =
1671 match state.mstate
with
1675 | Msel
((x0
, y0
), (x1
, y1
)) ->
1676 let rec loop = function
1678 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1679 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1681 match getopaque l.pageno with
1682 | Some
opaque when validopaque opaque ->
1683 let oy = -l.pagey
+ l.pagedispy
in
1685 (x0
- margin - state.x, y0
,
1686 x1
- margin - state.x, y1
) oy;
1697 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1698 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1700 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1702 if l.pageno = pageno
1704 let d = float (l.pagedispy
- l.pagey
) in
1705 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1706 GlDraw.begins `quads
;
1708 GlDraw.vertex2
(x0
, y0
+.d);
1709 GlDraw.vertex2
(x1
, y1
+.d);
1710 GlDraw.vertex2
(x2
, y2
+.d);
1711 GlDraw.vertex2
(x3
, y3
+.d);
1721 let showoutline = function
1723 | Some
(allowdel
, active, first, outlines, qsearch) ->
1725 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1726 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1727 GlDraw.rect
(0., 0.) (float state.w, float state.h
);
1730 GlDraw.color
(1., 1., 1.);
1731 let font = Glut.BITMAP_9_BY_15
in
1732 let draw_string x y s =
1733 GlPix.raster_pos ~
x ~
y ();
1734 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1737 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1740 let (s, l, _
, _
) = outlines.(row
) in
1741 let y = (row
- first) * 16 in
1746 GlDraw.polygon_mode `both `line
;
1747 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1748 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1749 let sw = float (conf.scrollw
- 1) *. float state.w /. float state.winw
in
1750 GlDraw.rect
(0., float (y + 1))
1751 ((float state.w -. sw), float (y + 18));
1752 GlDraw.polygon_mode `both `fill
;
1754 GlDraw.color
(1., 1., 1.);
1756 draw_string (float x) (float (y + 16)) s;
1764 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1765 GlDraw.viewport
margin 0 state.w state.h
;
1766 GlClear.color
(scalecolor 0.5);
1767 GlClear.clear
[`color
];
1770 let x = float state.x in
1771 GlMat.translate ~
x ();
1775 Gl.enable `scissor_test
;
1776 GlMisc.scissor
0 0 (state.winw
- conf.scrollw
) state.h
;
1778 let _lasty = List.fold_left
drawpage 0 (state.layout) in
1781 Gl.disable `scissor_test
1785 let x = -.float state.x in
1786 GlMat.translate ~
x ();
1789 GlDraw.viewport
(state.winw
- conf.scrollw
) 0 state.winw
state.h
;
1792 GlDraw.viewport
0 0 state.winw
state.h
;
1793 showoutline state.outline;
1795 Glut.swapBuffers
();
1799 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1800 let x = x - margin - state.x in
1801 let rec f = function
1803 begin match getopaque l.pageno with
1804 | Some
opaque when validopaque opaque ->
1805 let y = y - l.pagedispy
in
1808 let y = l.pagey
+ y in
1809 match whatsunder
opaque x y with
1822 let mouse ~button ~bstate ~
x ~
y =
1824 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1832 let incr = incr * 2 in
1833 let y = clamp incr in
1836 | Glut.LEFT_BUTTON
when state.outline = None
->
1837 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1838 begin match dest with
1839 | Ulinkgoto
(pageno, top
) ->
1842 gotopage1 pageno top
1847 | Unone
when bstate
= Glut.DOWN
->
1848 Glut.setCursor
Glut.CURSOR_INHERIT
;
1849 state.mstate
<- Mnone
1851 | Unone
| Utext _
->
1852 if bstate
= Glut.DOWN
1854 if state.rotate mod 360 = 0
1856 state.mstate
<- Msel
((x, y), (x, y));
1857 Glut.postRedisplay
()
1861 match state.mstate
with
1863 | Msel
((x0
, y0
), (x1
, y1
)) ->
1865 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1866 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1868 match getopaque l.pageno with
1869 | Some
opaque when validopaque opaque ->
1873 List.iter
f state.layout;
1874 copysel
""; (* ugly *)
1875 Glut.setCursor
Glut.CURSOR_INHERIT
;
1876 state.mstate
<- Mnone
;
1883 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1886 if state.outline = None
1888 match state.mstate
with
1891 state.mstate
<- Msel
(a, (x, y));
1892 Glut.postRedisplay
()
1896 if state.outline = None
1898 match state.mstate
with
1900 begin match getunder x y with
1901 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
1903 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
1904 Glut.setCursor
Glut.CURSOR_INFO
1905 | Ulinkgoto
(page
, y) ->
1906 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
1907 Glut.setCursor
Glut.CURSOR_INFO
1909 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
1910 Glut.setCursor
Glut.CURSOR_TEXT
1920 if Sys.os_type
= "Win32"
1922 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1924 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1926 Filename.concat
home "llpp"
1930 let ic = open_in_bin
statepath in
1931 let hash = input_value
ic in
1937 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1944 match state.fullscreen
with
1945 | None
-> state.winw
, state.h
1948 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h
);
1949 let oc = open_out_bin
statepath in
1950 output_value
oc pstate
1954 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1959 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1962 state.bookmarks <- statebookmarks;
1963 with Not_found
-> ()
1965 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1969 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
1970 (fun s -> state.path
<- s)
1971 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
1974 if String.length
state.path
= 0
1975 then (prerr_endline
"filename missing"; exit
1)
1980 let _ = Glut.init
Sys.argv
in
1981 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1982 let () = Glut.initWindowSize
state.w state.h
in
1983 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1986 if Sys.os_type
= "Unix"
1988 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1990 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1991 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1992 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1993 Unix.bind
sock addr;
1995 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1996 Unix.connect
csock addr;
1997 let ssock, _ = Unix.accept
sock in
2000 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2001 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2005 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2009 let () = Glut.displayFunc
display in
2010 let () = Glut.reshapeFunc
reshape in
2011 let () = Glut.keyboardFunc
keyboard in
2012 let () = Glut.specialFunc
special in
2013 let () = Glut.idleFunc
(Some
idle) in
2014 let () = Glut.mouseFunc
mouse in
2015 let () = Glut.motionFunc
motion in
2016 let () = Glut.passiveMotionFunc
pmotion in
2019 state.csock <- csock;
2020 state.ssock <- ssock;
2021 state.text <- "Opening " ^
name;
2022 writecmd state.csock ("open " ^
state.path ^
"\000" ^
state.password ^
"\000");
2026 let rec handlelablglutbug () =
2029 with Glut.BadEnum
"key in special_of_int" ->
2030 showtext '
!'
" LablGlut bug: special key not recognized";
2031 handlelablglutbug ()
2033 handlelablglutbug ();