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
110 type outline
= string * int * int * float;;
112 | Oarray
of outline array
113 | Olist
of outline list
114 | Onarrow
of outline array
* outline array
117 type rect
= (float * float * float * float * float * float * float * float);;
120 { mutable csock
: Unix.file_descr
121 ; mutable ssock
: Unix.file_descr
125 ; mutable rotate
: int
129 ; mutable layout
: layout list
130 ; pagemap
: ((int * int * int), string) Hashtbl.t
131 ; mutable pages
: (int * int * int) list
132 ; mutable pagecount
: int
133 ; pagecache
: string circbuf
134 ; mutable rendering
: bool
135 ; mutable mstate
: mstate
136 ; mutable searchpattern
: string
137 ; mutable rects
: (int * int * rect
) list
138 ; mutable rects1
: (int * int * rect
) list
139 ; mutable text
: string
140 ; mutable fullscreen
: (int * int) option
141 ; mutable textentry
: textentry
option
142 ; mutable outlines
: outlines
143 ; mutable outline
: (bool * int * int * outline array
* string) option
144 ; mutable bookmarks
: outline list
145 ; mutable path
: string
146 ; mutable password
: string
147 ; mutable invalidated
: int
148 ; mutable colorscale
: float
152 { pat
: string circbuf
153 ; pag
: string circbuf
154 ; nav
: float circbuf
188 ; pagemap
= Hashtbl.create
10
189 ; pagecache
= cbnew 10 ""
200 ; outlines
= Olist
[]
207 { nav
= cbnew 100 0.0
218 Printf.kprintf prerr_endline fmt
220 Printf.kprintf ignore fmt
224 let len = String.length s
in
226 let b = Buffer.create
n in
227 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
228 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
229 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
230 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
231 Buffer.add_string
b s
;
232 let s'
= Buffer.contents
b in
233 let n'
= Unix.write fd
s'
0 n in
234 if n'
!= n then failwith
"write failed";
239 let n = Unix.read fd
s 0 4 in
240 if n != 4 then failwith
"incomplete read(len)";
242 lor (Char.code
s.[0] lsl 24)
243 lor (Char.code
s.[1] lsl 16)
244 lor (Char.code
s.[2] lsl 8)
245 lor (Char.code
s.[3] lsl 0)
247 let s = String.create
len in
248 let n = Unix.read fd
s 0 len in
249 if n != len then failwith
"incomplete read(data)";
256 else float y
/. float state.maxy
260 let b = Buffer.create
10 in
261 Buffer.add_string
b s;
262 let rec combine = function
265 Buffer.add_char
b ' '
;
268 | `
b b -> if b then "1" else "0"
270 | `i i
-> string_of_int i
271 | `f f
-> string_of_float f
272 | `I f
-> string_of_int
(truncate f
)
274 Buffer.add_string
b s;
281 let cmd = Buffer.contents
(makecmd s l
) in
282 writecmd state.csock
cmd;
286 let rec f pn ph fh l
=
288 | (n, _
, h
) :: rest
->
289 let fh = fh + (n - pn
) * (ph
+ conf.interpagespace
) in
293 let fh = fh + ((ph
+ conf.interpagespace
) * (state.pagecount
- pn
)) in
296 let fh = f 0 0 0 state.pages
in
300 let getpageyh pageno
=
301 let rec f pn ph y l
=
303 | (n, _
, h
) :: rest
->
306 y
+ (pageno
- pn
) * (ph
+ conf.interpagespace
), h
308 let y = y + (n - pn
) * (ph
+ conf.interpagespace
) in
312 y + (pageno
- pn
) * (ph
+ conf.interpagespace
), ph
317 let getpagey pageno
= fst
(getpageyh pageno
);;
320 let ips = conf.interpagespace
in
321 let rec f ~pageno ~pdimno ~prev ~vy ~py ~dy ~pdims ~cacheleft ~accu
=
322 if pageno
= state.pagecount
|| cacheleft
= 0
325 let ((_
, w
, h
) as curr
), rest
, pdimno
=
327 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
328 curr
, rest
, pdimno
+ 1
332 let pageno'
= pageno + 1 in
366 let accu = e :: accu in
374 ~cacheleft
:(pred cacheleft
)
380 let dy, py = if t < 0 then 0, py + h
+ ips else t, py + h
- vh in
392 if state.invalidated
= 0
403 ~cacheleft
:(cblen state.pagecache
)
406 state.maxy
<- calcheight ();
413 let y = state.y + incr
in
415 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
419 let getopaque pageno =
420 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
, state.rotate
))
421 with Not_found
-> None
424 let cache pageno opaque
=
425 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
, state.rotate
) opaque
428 let validopaque opaque
= String.length opaque
> 0;;
431 match getopaque l
.pageno with
432 | None
when not
state.rendering
->
433 state.rendering
<- true;
435 wcmd "render" [`i
(l
.pageno + 1)
443 let loadlayout layout =
444 let rec f all
= function
446 begin match getopaque l
.pageno with
447 | None
-> render l
; f false ls
448 | Some opaque
-> f (all
&& validopaque opaque
) ls
452 f (layout <> []) layout;
459 let evictedopaque = cbpeekw state.pagecache
in
460 List.exists
(fun l
->
461 match getopaque l
.pageno with
462 | Some opaque
when validopaque opaque
->
463 evictedopaque = opaque
467 if not
evictedvisible
469 let y = if state.y < state.h
then 0 else state.y - state.h
in
470 let pages = layout y (state.h
*3) in
471 List.iter
render pages;
476 let y = min
state.maxy
y in
477 let pages = layout y state.h
in
478 let ready = loadlayout pages in
479 state.ty
<- yratio y;
484 state.layout <- pages;
486 Glut.postRedisplay
();
490 state.layout <- pages;
492 Glut.postRedisplay
();
498 cbput state.hists
.nav
(yratio state.y);
499 cbrfollowlen state.hists
.nav
;
503 let y = cbget state.hists
.nav ~
-1 in
504 truncate
(y *. float state.maxy
)
508 let y, h
= getpageyh n in
510 gotoy (y + (truncate
(top
*. float h
)));
513 let gotopage1 n top
=
514 let y = getpagey n in
524 state.invalidated
<- state.invalidated
+ 1;
528 let c = c *. state.colorscale
in
534 let m = float conf.margin in
535 let m = m *. (float w
/. 20.) in
536 let m = truncate
m in
537 if m*2 > (w
- conf.scrollw
) then 0 else m
540 let w = w - margin * 2 - conf.scrollw
in
543 GlMat.mode `modelview
;
544 GlMat.load_identity
();
545 GlMat.mode `projection
;
546 GlMat.load_identity
();
547 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
548 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
549 GlMat.scale3
(2.0 /. float w, 2.0 /. float state.h
, 1.0);
550 GlClear.color
(scalecolor 1.0);
551 GlClear.clear
[`color
];
554 wcmd "geometry" [`i
state.w; `i h
];
558 GlDraw.color
(0.0, 0.0, 0.0);
560 (0.0, float (state.h
- 18))
561 (float (state.winw
- conf.scrollw
- 1), float state.h
)
563 let font = Glut.BITMAP_8_BY_13
in
564 GlDraw.color
(1.0, 1.0, 1.0);
565 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
566 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
567 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
571 let len = String.length
state.text
in
572 match state.textentry
with
574 if len > 0 then showtext ' '
state.text
576 | Some
(c, text
, _
, _
, _
) ->
580 text ^
" [" ^
state.text ^
"]"
590 state.text
<- Printf.sprintf
"%c%s" c s;
591 Glut.postRedisplay
();
605 state.rects
<- state.rects1
;
606 Glut.postRedisplay
()
609 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
610 state.pagecount
<- n;
611 state.invalidated
<- state.invalidated
- 1;
612 if state.invalidated
= 0
614 let rely = yratio state.y in
615 state.maxy
<- calcheight ();
616 gotoy (truncate
(float state.maxy
*. rely));
620 let s = Scanf.sscanf
cmd "t %n"
621 (fun n -> String.sub
cmd n (String.length
cmd - n))
623 Glut.setWindowTitle
s
626 let s = Scanf.sscanf
cmd "T %n"
627 (fun n -> String.sub
cmd n (String.length
cmd - n))
629 if state.textentry
= None
636 Glut.postRedisplay
();
642 let s = Scanf.sscanf
cmd "V %n"
643 (fun n -> String.sub
cmd n (String.length
cmd - n))
649 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
650 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
651 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
652 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
654 let y = (getpagey pageno) + truncate y0
in
657 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
660 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
661 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
662 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
663 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
666 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
670 Scanf.sscanf
cmd "r %d %d %d %d %s"
671 (fun n w h r p
-> (n, w, h
, r
, p
))
673 Hashtbl.replace
state.pagemap
(n, w, r
) p
;
674 let opaque = cbpeekw state.pagecache
in
675 if validopaque opaque
679 (fun k v a
-> if v
= opaque then k else a
)
680 state.pagemap
(-1, -1, -1)
682 wcmd "free" [`
s opaque];
683 Hashtbl.remove
state.pagemap
k
685 cbput state.pagecache p
;
686 state.rendering
<- false;
688 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy
)))
690 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
693 else (ignore
(loadlayout state.layout); preload ())
697 let (n, w, h
) as pagelayout
=
698 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w, h
)
700 state.pages <- pagelayout
:: state.pages
703 let (l
, n, t, h
, pos
) =
704 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t, h
, pos
)
706 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
708 let l = String.length
s in
709 let b = Buffer.create
(String.length
s) in
716 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
719 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
728 let outline = (s, l, n, float t /. float h
) in
730 match state.outlines with
731 | Olist
outlines -> Olist
(outline :: outlines)
732 | Oarray _
-> Olist
[outline]
733 | Onarrow _
-> Olist
[outline]
735 state.outlines <- outlines
738 log "unknown cmd `%S'" cmd
741 let now = Unix.gettimeofday
;;
745 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
750 let y = state.y + conf.scrollincr
in
751 let y = if y >= state.maxy
then 0 else y in
757 let cmd = readcmd state.csock
in
764 let onhist cb
= function
765 | HCprev
-> cbget cb ~
-1
766 | HCnext
-> cbget cb
1
767 | HCfirst
-> cbget cb ~
-(cb
.rc)
768 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
771 let search pattern forward
=
772 if String.length pattern
> 0
775 match state.layout with
778 l.pageno, (l.pagey
+ if forward
then 0 else 0*l.pagevh
)
781 let b = makecmd "search"
782 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
784 Buffer.add_char
b '
,'
;
785 Buffer.add_string
b pattern
;
786 Buffer.add_char
b '
\000'
;
789 writecmd state.csock
cmd;
792 let intentry text key
=
793 let c = Char.unsafe_chr key
in
796 let s = "x" in s.[0] <- c;
797 let text = text ^
s in
801 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
806 let b = Buffer.create
(String.length
s + 1) in
807 Buffer.add_string
b s;
812 let textentry text key
=
813 let c = Char.unsafe_chr key
in
815 | _
when key
>= 32 && key
< 127 ->
816 let text = addchar text c in
820 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
825 state.rotate <- angle
;
827 wcmd "rotate" [`i angle
];
830 let optentry text key
=
831 let btos b = if b then "on" else "off" in
832 let c = Char.unsafe_chr key
in
836 try conf.scrollincr
<- int_of_string
s with exc
->
837 state.text <- Printf.sprintf
"bad integer `%s': %s"
838 s (Printexc.to_string exc
)
840 TEswitch
('#'
, "", None
, intentry, ondone)
845 Some
(int_of_string
s)
847 state.text <- Printf.sprintf
"bad integer `%s': %s"
848 s (Printexc.to_string exc
);
851 | Some angle
-> rotate angle
854 TEswitch
('^'
, "", None
, intentry, ondone)
857 conf.icase
<- not
conf.icase
;
858 TEdone
("case insensitive search " ^
(btos conf.icase
))
861 conf.preload <- not
conf.preload;
863 TEdone
("preload " ^
(btos conf.preload))
866 conf.verbose
<- not
conf.verbose
;
867 TEdone
("verbose " ^
(btos conf.verbose
))
870 conf.maxhfit
<- not
conf.maxhfit
;
871 state.maxy
<- state.maxy
+ (if conf.maxhfit
then -state.h
else state.h
);
872 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
875 conf.crophack
<- not
conf.crophack
;
876 TEdone
("crophack " ^
btos conf.crophack
)
879 conf.showall
<- not
conf.showall
;
880 TEdone
("showall " ^
btos conf.showall
)
883 conf.underinfo
<- not
conf.underinfo
;
884 TEdone
("underinfo " ^
btos conf.underinfo
)
889 conf.interpagespace
<- int_of_string
s;
890 let rely = yratio state.y in
891 state.maxy
<- calcheight ();
892 gotoy (truncate
(float state.maxy
*. rely));
894 state.text <- Printf.sprintf
"bad integer `%s': %s"
895 s (Printexc.to_string exc
)
897 TEswitch
('
%'
, "", None
, intentry, ondone)
900 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
904 let maxoutlinerows () = (state.h
- 31) / 16;;
906 let enterselector allowdel
outlines errmsg
=
907 if Array.length
outlines = 0
912 Glut.setCursor
Glut.CURSOR_INHERIT
;
914 match state.layout with
916 | {pageno=pageno} :: rest
-> pageno
920 if n = Array.length
outlines
923 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
924 if outlinepageno
>= pageno then n else loop (n+1)
929 Some
(allowdel
, active,
930 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
931 Glut.postRedisplay
();
935 let enteroutlinemode () =
937 match state.outlines with
940 let a = Array.of_list
(List.rev
l) in
941 state.outlines <- Oarray
a;
943 | Onarrow
(a, b) -> a
945 enterselector false outlines "Document has no outline";
948 let enterbookmarkmode () =
949 let bookmarks = Array.of_list
state.bookmarks in
950 enterselector true bookmarks "Document has no bookmarks (yet)";
954 let quickbookmark ?title
() =
955 match state.layout with
961 let sec = Unix.gettimeofday
() in
962 let tm = Unix.localtime
sec in
963 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
967 (tm.Unix.tm_year
+ 1900)
970 | Some
title -> title
973 (title, 0, l.pageno, float l.pagey
/. float l.pageh
) :: state.bookmarks
977 state.fullscreen
<- None
;
978 Glut.reshapeWindow
w h
;
981 let opendoc path password
=
984 state.password
<- password
;
985 Hashtbl.clear
state.pagemap
;
987 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
988 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
989 wcmd "geometry" [`i
state.w; `i
state.h
];
992 let viewkeyboard ~key ~x ~
y =
994 state.textentry <- te
;
997 Glut.postRedisplay
()
999 match state.textentry with
1001 let c = Char.chr key
in
1007 let y = getnav () in
1016 Glut.postRedisplay
()
1019 let ondone isforw
s =
1020 cbput state.hists
.pat
s;
1021 cbrfollowlen state.hists
.pat
;
1022 state.searchpattern
<- s;
1025 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1026 textentry, ondone (c ='
/'
)))
1029 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1031 let margin = min
8 (conf.margin + 1) in
1032 conf.margin <- margin;
1033 reshape state.winw
state.h
;
1038 try int_of_string
s with exc
->
1039 state.text <- Printf.sprintf
"bad integer `%s': %s"
1040 s (Printexc.to_string exc
);
1046 state.text <- "page bias is now " ^ string_of_int
n;
1049 enttext (Some
('
+'
, "", None
, intentry, ondone))
1052 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1054 let margin = max
0 (conf.margin - 1) in
1055 conf.margin <- margin;
1056 reshape state.winw
state.h
;
1062 enttext (Some
('
-'
, "", None
, optentry, ondone))
1067 try int_of_string
s with exc
->
1068 state.text <- Printf.sprintf
"bad integer `%s': %s"
1069 s (Printexc.to_string exc
);
1075 cbput state.hists
.pag
(string_of_int
n);
1076 cbrfollowlen state.hists
.pag
;
1077 gotoy (getpagey (n + conf.pagebias
- 1))
1080 let pageentry text key
=
1081 match Char.unsafe_chr key
with
1082 | 'g'
-> TEdone
text
1083 | _
-> intentry text key
1085 let text = "x" in text.[0] <- c;
1086 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1090 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
1091 reshape state.winw
state.h
;
1094 conf.hlinks
<- not
conf.hlinks
;
1095 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1096 Glut.postRedisplay
()
1099 conf.autoscroll
<- not
conf.autoscroll
1102 begin match state.layout with
1106 let d = state.h
- l.pageh
in
1109 let rely = yratio state.y in
1110 conf.interpagespace
<- ips;
1111 state.maxy
<- calcheight ();
1112 gotoy (truncate
(float state.maxy
*. rely));
1116 begin match state.fullscreen
with
1118 state.fullscreen
<- Some
(state.w, state.h
);
1121 state.fullscreen
<- None
;
1129 search state.searchpattern
true
1132 search state.searchpattern
false
1135 begin match state.layout with
1138 gotoy (state.y - l.pagey
- conf.interpagespace
);
1142 begin match List.rev
state.layout with
1145 gotoy (clamp (l.pageh
- l.pagey
+ conf.interpagespace
))
1149 begin match state.layout with
1152 gotoy (clamp (-l.pageh
- conf.interpagespace
));
1157 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1159 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1161 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1165 else (100. *. (float state.y /. float maxy)) in
1168 Printf.sprintf
"Page %d of %d %.2f%%"
1169 (fn+1) state.pagecount
percent
1172 "Pages %d-%d of %d %.2f%%"
1173 (fn+1) (ln
+1) state.pagecount
percent
1178 begin match state.layout with
1181 doreshape l.pagew
l.pageh
;
1182 Glut.postRedisplay
();
1186 enterbookmarkmode ()
1190 match state.layout with
1193 (s, 0, l.pageno, float l.pagey
/. float l.pageh
)
1197 enttext (Some
('~'
, "", None
, textentry, ondone))
1201 showtext ' '
"Quick bookmark added";
1204 begin match state.layout with
1206 let a = getpagewh
l.pagedimno
in
1210 (truncate
(1.8 *. (a.(1) -. a.(0))),
1211 truncate
(1.2 *. (a.(3) -. a.(0))))
1213 (truncate
(a.(1) -. a.(0)),
1214 truncate
(a.(3) -. a.(0)))
1217 Glut.postRedisplay
();
1223 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1228 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1229 Glut.postRedisplay
()
1231 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1232 | 'j'
-> gotoy (clamp conf.scrollincr
)
1234 | '
r'
-> opendoc state.path
state.password
1237 vlog "huh? %d %c" key
(Char.chr key
);
1240 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1241 let len = String.length
text in
1244 state.textentry <- None
;
1245 Glut.postRedisplay
();
1248 let s = String.sub
text 0 (len - 1) in
1249 enttext (Some
(c, s, onhist, onkey
, ondone))
1252 | Some
(c, text, onhist, onkey
, ondone) ->
1253 begin match Char.unsafe_chr key
with
1256 state.textentry <- None
;
1257 Glut.postRedisplay
()
1260 state.textentry <- None
;
1261 Glut.postRedisplay
()
1264 begin match onkey
text key
with
1266 state.textentry <- None
;
1268 Glut.postRedisplay
()
1271 enttext (Some
(c, text, onhist, onkey
, ondone));
1274 state.textentry <- None
;
1275 Glut.postRedisplay
()
1278 state.textentry <- Some te
;
1279 Glut.postRedisplay
()
1284 let narrow outlines pattern
=
1285 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1289 let rec fold accu n =
1293 let (s, _
, _
, _
) as o
= outlines.(n) in
1295 if (try ignore
(Str.search_forward re
s 0); true
1296 with Not_found
-> false)
1302 let matched = fold [] (Array.length
outlines - 1) in
1303 if matched = [] then None
else Some
(Array.of_list
matched)
1306 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1307 let search active pattern incr
=
1310 if n = Array.length
outlines || n = -1
1313 let (s, _
, _
, _
) = outlines.(n) in
1315 (try ignore
(Str.search_forward re
s 0); true
1316 with Not_found
-> false)
1318 else loop (n + incr
)
1323 let re = Str.regexp_case_fold pattern
in
1329 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1332 if String.length qsearch
= 0
1335 state.outline <- None
;
1336 Glut.postRedisplay
();
1340 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1341 Glut.postRedisplay
();
1345 let incr = if key
= 18 then -1 else 1 in
1347 match search (active + incr) qsearch
incr with
1349 state.text <- qsearch ^
" [not found]";
1352 state.text <- qsearch
;
1353 active, firstof active
1355 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1356 Glut.postRedisplay
();
1359 let len = String.length qsearch
in
1366 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1369 let qsearch = String.sub
qsearch 0 (len - 1) in
1371 match search active qsearch ~
-1 with
1373 state.text <- qsearch ^
" [not found]";
1376 state.text <- qsearch;
1377 active, firstof active
1379 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1381 Glut.postRedisplay
()
1384 if active < Array.length
outlines
1386 let (_
, _
, n, t) = outlines.(active) in
1390 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1391 state.outline <- None
;
1392 Glut.postRedisplay
();
1394 | _
when key
>= 32 && key
< 127 ->
1395 let pattern = addchar qsearch (Char.chr key
) in
1397 match search active pattern 1 with
1399 state.text <- pattern ^
" [not found]";
1402 state.text <- pattern;
1403 active, firstof active
1405 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1406 Glut.postRedisplay
()
1408 | 14 when not allowdel
->
1409 let optoutlines = narrow outlines qsearch in
1410 begin match optoutlines with
1411 | None
-> state.text <- "can't narrow"
1413 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1414 match state.outlines with
1416 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1417 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1419 Glut.postRedisplay
()
1421 | 21 when not allowdel
->
1423 match state.outlines with
1426 let a = Array.of_list
(List.rev
l) in
1427 state.outlines <- Oarray
a;
1430 state.outlines <- Oarray
b;
1433 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1434 Glut.postRedisplay
()
1438 Some
(allowdel
, active, firstof active, outlines, qsearch);
1439 Glut.postRedisplay
()
1441 | 127 when allowdel
->
1442 let len = Array.length
outlines - 1 in
1445 state.outline <- None
;
1446 state.bookmarks <- [];
1449 let bookmarks = Array.init
len
1451 let i = if i >= active then i + 1 else i in
1462 Glut.postRedisplay
()
1464 | _
-> log "unknown key %d" key
1467 let keyboard ~key ~x ~
y =
1472 match state.outline with
1473 | None
-> viewkeyboard ~key ~x ~
y
1474 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1477 let special ~key ~x ~
y =
1478 match state.outline with
1480 begin match state.textentry with
1484 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1485 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1486 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1487 | Glut.KEY_PAGE_UP
->
1488 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1490 match state.layout with
1492 | l :: _
-> state.y - l.pagey
1495 | Glut.KEY_PAGE_DOWN
->
1496 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1498 match List.rev
state.layout with
1500 | l :: _
-> getpagey l.pageno
1503 | Glut.KEY_HOME
-> addnav (); 0
1506 state.maxy - (if conf.maxhfit
then state.h
else 0)
1509 if not
conf.verbose
then state.text <- "";
1512 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1515 | Glut.KEY_UP
-> onhist HCprev
1516 | Glut.KEY_DOWN
-> onhist HCnext
1517 | Glut.KEY_HOME
-> onhist HCfirst
1518 | Glut.KEY_END
-> onhist HClast
1521 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1522 Glut.postRedisplay
()
1527 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1528 let maxrows = maxoutlinerows () in
1530 let active = active + incr in
1531 let active = max
0 (min
active (Array.length
outlines - 1)) in
1535 let rows = active - first in
1536 if rows > maxrows then active - maxrows else first
1539 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1540 Glut.postRedisplay
()
1543 | Glut.KEY_UP
-> navigate ~
-1
1544 | Glut.KEY_DOWN
-> navigate 1
1545 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1546 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1549 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1550 Glut.postRedisplay
()
1553 let active = Array.length
outlines - 1 in
1554 let first = max
0 (active - maxrows) in
1555 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1556 Glut.postRedisplay
()
1561 let drawplaceholder l =
1562 GlDraw.color
(scalecolor 1.0);
1564 (0.0, float l.pagedispy
)
1565 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1568 and y = float (l.pagedispy
+ 13) in
1569 let font = Glut.BITMAP_8_BY_13
in
1570 GlDraw.color
(0.0, 0.0, 0.0);
1571 GlPix.raster_pos ~
x ~
y ();
1572 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1573 ("Loading " ^ string_of_int
l.pageno);
1576 let now () = Unix.gettimeofday
();;
1579 begin match getopaque l.pageno with
1580 | Some
opaque when validopaque opaque ->
1581 if state.textentry = None
1582 then GlDraw.color
(scalecolor 1.0)
1583 else GlDraw.color
(scalecolor 0.4);
1585 draw
(l.pagedispy
, l.pagew
, l.pagevh
, l.pagey
, conf.hlinks
)
1589 vlog "draw %f sec" d;
1594 l.pagedispy
+ l.pagevh
;
1597 let scrollindicator () =
1598 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1599 GlDraw.color
(0.64 , 0.64, 0.64);
1602 (float conf.scrollw
, float state.h
)
1604 GlDraw.color
(0.0, 0.0, 0.0);
1605 let sh = (float (maxy + state.h
) /. float state.h
) in
1606 let sh = float state.h
/. sh in
1607 let sh = max
sh (float conf.scrollh
) in
1610 if state.y = state.maxy
1612 else float state.y /. float maxy
1614 let position = (float state.h
-. sh) *. percent in
1617 if position +. sh > float state.h
1625 (float conf.scrollw
, position +. sh)
1629 let showsel margin =
1630 match state.mstate
with
1634 | Msel
((x0
, y0
), (x1
, y1
)) ->
1635 let rec loop = function
1637 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1638 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1640 match getopaque l.pageno with
1641 | Some
opaque when validopaque opaque ->
1642 let oy = -l.pagey
+ l.pagedispy
in
1643 seltext
opaque (x0
- margin, y0
, x1
- margin, y1
) oy;
1654 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1655 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1657 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1659 if l.pageno = pageno
1661 let d = float (l.pagedispy
- l.pagey
) in
1662 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1663 GlDraw.begins `quads
;
1665 GlDraw.vertex2
(x0
, y0
+.d);
1666 GlDraw.vertex2
(x1
, y1
+.d);
1667 GlDraw.vertex2
(x2
, y2
+.d);
1668 GlDraw.vertex2
(x3
, y3
+.d);
1678 let showoutline = function
1680 | Some
(allowdel
, active, first, outlines, qsearch) ->
1682 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1683 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1684 GlDraw.rect
(0., 0.) (float state.w, float state.h
);
1687 GlDraw.color
(1., 1., 1.);
1688 let font = Glut.BITMAP_9_BY_15
in
1689 let draw_string x y s =
1690 GlPix.raster_pos ~
x ~
y ();
1691 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1694 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1697 let (s, l, _
, _
) = outlines.(row
) in
1698 let y = (row
- first) * 16 in
1703 GlDraw.polygon_mode `both `line
;
1704 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1705 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1706 GlDraw.rect
(0., float (y + 1))
1707 (float (state.winw
- conf.scrollw
- 1), float (y + 18));
1708 GlDraw.polygon_mode `both `fill
;
1710 GlDraw.color
(1., 1., 1.);
1712 draw_string (float x) (float (y + 16)) s;
1720 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1721 GlDraw.viewport
margin 0 state.w state.h
;
1722 GlClear.color
(scalecolor 0.5);
1723 GlClear.clear
[`color
];
1724 let lasty = List.fold_left
drawpage 0 (state.layout) in
1726 GlDraw.viewport
(state.winw
- conf.scrollw
) 0 state.winw
state.h
;
1729 GlDraw.viewport
0 0 state.winw
state.h
;
1730 showoutline state.outline;
1732 Glut.swapBuffers
();
1736 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1737 let x = x - margin in
1738 let rec f = function
1740 begin match getopaque l.pageno with
1741 | Some
opaque when validopaque opaque ->
1742 let y = y - l.pagedispy
in
1745 let y = l.pagey
+ y in
1746 match whatsunder
opaque x y with
1759 let mouse ~button ~bstate ~
x ~
y =
1761 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1769 let incr = incr * 2 in
1770 let y = clamp incr in
1773 | Glut.LEFT_BUTTON
when state.outline = None
->
1774 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1775 begin match dest with
1776 | Ulinkgoto
(pageno, top
) ->
1779 gotopage1 pageno top
1784 | Unone
when bstate
= Glut.DOWN
->
1785 Glut.setCursor
Glut.CURSOR_INHERIT
;
1786 state.mstate
<- Mnone
1788 | Unone
| Utext _
->
1789 if bstate
= Glut.DOWN
1791 if state.rotate mod 360 = 0
1793 state.mstate
<- Msel
((x, y), (x, y));
1794 Glut.postRedisplay
()
1798 match state.mstate
with
1800 | Msel
((x0
, y0
), (x1
, y1
)) ->
1802 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1803 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1805 match getopaque l.pageno with
1806 | Some
opaque when validopaque opaque ->
1810 List.iter
f state.layout;
1811 copysel
""; (* ugly *)
1812 Glut.setCursor
Glut.CURSOR_INHERIT
;
1813 state.mstate
<- Mnone
;
1820 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1823 if state.outline = None
1825 match state.mstate
with
1828 state.mstate
<- Msel
(a, (x, y));
1829 Glut.postRedisplay
()
1833 if state.outline = None
1835 match state.mstate
with
1837 begin match getunder x y with
1838 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
1840 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
1841 Glut.setCursor
Glut.CURSOR_INFO
1842 | Ulinkgoto
(page
, y) ->
1843 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
1844 Glut.setCursor
Glut.CURSOR_INFO
1846 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
1847 Glut.setCursor
Glut.CURSOR_TEXT
1857 if Sys.os_type
= "Win32"
1859 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1861 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1863 Filename.concat
home "llpp"
1867 let ic = open_in_bin
statepath in
1868 let hash = input_value
ic in
1874 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1881 match state.fullscreen
with
1882 | None
-> state.winw
, state.h
1885 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h
);
1886 let oc = open_out_bin
statepath in
1887 output_value
oc pstate
1891 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1896 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1899 state.bookmarks <- statebookmarks;
1900 with Not_found
-> ()
1902 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1906 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
1907 (fun s -> state.path
<- s)
1908 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
1911 if String.length
state.path
= 0
1912 then (prerr_endline
"filename missing"; exit
1)
1917 let _ = Glut.init
Sys.argv
in
1918 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1919 let () = Glut.initWindowSize
state.w state.h
in
1920 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1923 if Sys.os_type
= "Unix"
1925 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1927 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1928 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1929 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1930 Unix.bind
sock addr;
1932 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1933 Unix.connect
csock addr;
1934 let ssock, _ = Unix.accept
sock in
1937 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1938 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1942 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1946 let () = Glut.displayFunc
display in
1947 let () = Glut.reshapeFunc
reshape in
1948 let () = Glut.keyboardFunc
keyboard in
1949 let () = Glut.specialFunc
special in
1950 let () = Glut.idleFunc
(Some
idle) in
1951 let () = Glut.mouseFunc
mouse in
1952 let () = Glut.motionFunc
motion in
1953 let () = Glut.passiveMotionFunc
pmotion in
1956 state.csock <- csock;
1957 state.ssock <- ssock;
1958 state.text <- "Opening " ^
name;
1959 writecmd state.csock ("open " ^
state.path ^
"\000" ^
state.password ^
"\000");
1963 let rec handlelablglutbug () =
1966 with Glut.BadEnum
"key in special_of_int" ->
1967 showtext '
!'
" LablGlut bug: special key not recognized";
1968 handlelablglutbug ()
1970 handlelablglutbug ();