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
108 type outline
= string * int * int * float;;
110 | Oarray
of outline array
111 | Olist
of outline list
112 | Onarrow
of outline array
* outline array
115 type rect
= (float * float * float * float * float * float * float * float);;
118 { mutable csock
: Unix.file_descr
119 ; mutable ssock
: Unix.file_descr
122 ; mutable rotate
: int
126 ; mutable layout
: layout list
127 ; pagemap
: ((int * int * int), string) Hashtbl.t
128 ; mutable pages
: (int * int * int) list
129 ; mutable pagecount
: int
130 ; pagecache
: string circbuf
131 ; mutable rendering
: bool
132 ; mutable mstate
: mstate
133 ; mutable searchpattern
: string
134 ; mutable rects
: (int * int * rect
) list
135 ; mutable rects1
: (int * int * rect
) list
136 ; mutable text
: string
137 ; mutable fullscreen
: (int * int) option
138 ; mutable textentry
: textentry
option
139 ; mutable outlines
: outlines
140 ; mutable outline
: (bool * int * int * outline array
* string) option
141 ; mutable bookmarks
: outline list
142 ; mutable path
: string
143 ; mutable password
: string
144 ; mutable invalidated
: int
145 ; mutable colorscale
: float
149 { pat
: string circbuf
150 ; pag
: string circbuf
151 ; nav
: float circbuf
182 ; pagemap
= Hashtbl.create
10
183 ; pagecache
= cbnew 10 ""
194 ; outlines
= Olist
[]
201 { nav
= cbnew 100 0.0
212 Printf.kprintf prerr_endline fmt
214 Printf.kprintf ignore fmt
218 let len = String.length s
in
220 let b = Buffer.create
n in
221 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
222 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
223 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
224 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
225 Buffer.add_string
b s
;
226 let s'
= Buffer.contents
b in
227 let n'
= Unix.write fd
s'
0 n in
228 if n'
!= n then failwith
"write failed";
233 let n = Unix.read fd
s 0 4 in
234 if n != 4 then failwith
"incomplete read(len)";
236 lor (Char.code
s.[0] lsl 24)
237 lor (Char.code
s.[1] lsl 16)
238 lor (Char.code
s.[2] lsl 8)
239 lor (Char.code
s.[3] lsl 0)
241 let s = String.create
len in
242 let n = Unix.read fd
s 0 len in
243 if n != len then failwith
"incomplete read(data)";
250 else float y
/. float state.maxy
254 let b = Buffer.create
10 in
255 Buffer.add_string
b s;
256 let rec combine = function
259 Buffer.add_char
b ' '
;
262 | `
b b -> if b then "1" else "0"
264 | `i i
-> string_of_int i
265 | `f f
-> string_of_float f
266 | `I f
-> string_of_int
(truncate f
)
268 Buffer.add_string
b s;
275 let cmd = Buffer.contents
(makecmd s l
) in
276 writecmd state.csock
cmd;
280 let rec f pn ph fh l
=
282 | (n, _
, h
) :: rest
->
283 let fh = fh + (n - pn
) * ph
in
287 let fh = fh + (ph
* (state.pagecount
- pn
)) in
290 let fh = f 0 0 0 state.pages
in
294 let getpageyh pageno
=
295 let rec f pn ph y l
=
297 | (n, _
, h
) :: rest
->
300 y
+ (pageno
- pn
) * ph
, h
302 let y = y + (n - pn
) * ph
in
306 y + (pageno
- pn
) * ph
, ph
311 let getpagey pageno
= fst
(getpageyh pageno
);;
314 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
315 if pageno
= state.pagecount
|| cacheleft
= 0
318 let ((_
, w
, h
) as curr
), rest
, pdimno
=
320 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
321 curr
, rest
, pdimno
+ 1
325 let pageno'
= pageno + 1 in
359 let accu = e :: accu in
360 f pageno' pdimno curr
361 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
362 (pred cacheleft
) accu
364 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
366 if state.invalidated
= 0
368 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
369 state.maxy
<- calcheight ();
376 let y = state.y + incr
in
378 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
382 let getopaque pageno =
383 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
385 with Not_found
-> None
388 let cache pageno opaque
=
389 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
393 let validopaque opaque
= String.length opaque
> 0;;
396 match getopaque l
.pageno with
397 | None
when not
state.rendering
->
398 state.rendering
<- true;
400 wcmd "render" [`i
(l
.pageno + 1)
408 let loadlayout layout =
409 let rec f all
= function
411 begin match getopaque l
.pageno with
412 | None
-> render l
; f false ls
413 | Some opaque
-> f (all
&& validopaque opaque
) ls
417 f (layout <> []) layout;
424 let evictedopaque = cbpeekw state.pagecache
in
425 List.exists
(fun l
->
426 match getopaque l
.pageno with
427 | Some opaque
when validopaque opaque
->
428 evictedopaque = opaque
432 if not
evictedvisible
434 let y = if state.y < state.h
then 0 else state.y - state.h
in
435 let pages = layout y (state.h
*3) in
436 List.iter
render pages;
441 let y = min
state.maxy
y in
442 let pages = layout y state.h
in
443 let ready = loadlayout pages in
444 state.ty
<- yratio y;
449 state.layout <- pages;
451 Glut.postRedisplay
();
455 state.layout <- pages;
457 Glut.postRedisplay
();
463 cbput state.hists
.nav
(yratio state.y);
464 cbrfollowlen state.hists
.nav
;
468 let y = cbget state.hists
.nav ~
-1 in
469 truncate
(y *. float state.maxy
)
473 let y, h
= getpageyh n in
475 gotoy (y + (truncate
(top
*. float h
)));
478 let gotopage1 n top
=
479 let y = getpagey n in
489 state.invalidated
<- state.invalidated
+ 1;
493 let c = c *. state.colorscale
in
500 GlDraw.viewport
0 0 w h
;
501 GlMat.mode `modelview
;
502 GlMat.load_identity
();
503 GlMat.mode `projection
;
504 GlMat.load_identity
();
505 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
506 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
507 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
508 GlClear.color
(scalecolor 1.0);
509 GlClear.clear
[`color
];
512 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
516 GlDraw.color
(0.0, 0.0, 0.0);
518 (0.0, float (state.h
- 18))
519 (float (state.w
- conf.scrollw
- 1), float state.h
)
521 let font = Glut.BITMAP_8_BY_13
in
522 GlDraw.color
(1.0, 1.0, 1.0);
523 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
524 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
525 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
529 let len = String.length
state.text
in
530 match state.textentry
with
532 if len > 0 then showtext ' '
state.text
534 | Some
(c, text
, _
, _
, _
) ->
538 text ^
" [" ^
state.text ^
"]"
548 state.text
<- Printf.sprintf
"%c%s" c s;
549 Glut.postRedisplay
();
563 state.rects
<- state.rects1
;
564 Glut.postRedisplay
()
567 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
568 state.pagecount
<- n;
569 state.invalidated
<- state.invalidated
- 1;
570 if state.invalidated
= 0
572 let rely = yratio state.y in
573 state.maxy
<- calcheight ();
574 gotoy (truncate
(float state.maxy
*. rely));
578 let s = Scanf.sscanf
cmd "t %n"
579 (fun n -> String.sub
cmd n (String.length
cmd - n))
581 Glut.setWindowTitle
s
584 let s = Scanf.sscanf
cmd "T %n"
585 (fun n -> String.sub
cmd n (String.length
cmd - n))
587 if state.textentry
= None
594 Glut.postRedisplay
();
600 let s = Scanf.sscanf
cmd "V %n"
601 (fun n -> String.sub
cmd n (String.length
cmd - n))
607 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
608 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
609 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
610 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
612 let y = (getpagey pageno) + truncate y0
in
615 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
618 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
619 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
620 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
621 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
624 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
628 Scanf.sscanf
cmd "r %d %d %d %d %s"
629 (fun n w h r p
-> (n, w
, h
, r
, p
))
631 Hashtbl.replace
state.pagemap
(n, w
, r
) p
;
632 let opaque = cbpeekw state.pagecache
in
633 if validopaque opaque
637 (fun k v a
-> if v
= opaque then k else a
)
638 state.pagemap
(-1, -1, -1)
640 wcmd "free" [`
s opaque];
641 Hashtbl.remove
state.pagemap
k
643 cbput state.pagecache p
;
644 state.rendering
<- false;
646 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy
)))
648 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
651 else (ignore
(loadlayout state.layout); preload ())
655 let (n, w
, h
) as pagelayout
=
656 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
658 state.pages <- pagelayout
:: state.pages
661 let (l
, n, t
, h
, pos
) =
662 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
664 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
666 let l = String.length
s in
667 let b = Buffer.create
(String.length
s) in
674 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
677 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
686 let outline = (s, l, n, float t
/. float h
) in
688 match state.outlines with
689 | Olist
outlines -> Olist
(outline :: outlines)
690 | Oarray _
-> Olist
[outline]
691 | Onarrow _
-> Olist
[outline]
693 state.outlines <- outlines
696 log "unknown cmd `%S'" cmd
699 let now = Unix.gettimeofday
;;
703 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
708 let y = state.y + conf.scrollincr
in
709 let y = if y >= state.maxy
then 0 else y in
715 let cmd = readcmd state.csock
in
722 let onhist cb
= function
723 | HCprev
-> cbget cb ~
-1
724 | HCnext
-> cbget cb
1
725 | HCfirst
-> cbget cb ~
-(cb
.rc)
726 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
729 let search pattern forward
=
730 if String.length pattern
> 0
733 match state.layout with
736 l.pageno, (l.pagey
+ if forward
then 0 else 0*l.pagevh
)
739 let b = makecmd "search"
740 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
742 Buffer.add_char
b '
,'
;
743 Buffer.add_string
b pattern
;
744 Buffer.add_char
b '
\000'
;
747 writecmd state.csock
cmd;
750 let intentry text key
=
751 let c = Char.unsafe_chr key
in
754 let s = "x" in s.[0] <- c;
755 let text = text ^
s in
759 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
764 let b = Buffer.create
(String.length
s + 1) in
765 Buffer.add_string
b s;
770 let textentry text key
=
771 let c = Char.unsafe_chr key
in
773 | _
when key
>= 32 && key
< 127 ->
774 let text = addchar text c in
778 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
783 state.rotate <- angle
;
785 wcmd "rotate" [`i angle
];
788 let optentry text key
=
789 let btos b = if b then "on" else "off" in
790 let c = Char.unsafe_chr key
in
794 try conf.scrollincr
<- int_of_string
s with exc
->
795 state.text <- Printf.sprintf
"bad integer `%s': %s"
796 s (Printexc.to_string exc
)
798 TEswitch
('#'
, "", None
, intentry, ondone)
803 Some
(int_of_string
s)
805 state.text <- Printf.sprintf
"bad integer `%s': %s"
806 s (Printexc.to_string exc
);
809 | Some angle
-> rotate angle
812 TEswitch
('^'
, "", None
, intentry, ondone)
815 conf.icase
<- not
conf.icase
;
816 TEdone
("case insensitive search " ^
(btos conf.icase
))
819 conf.preload <- not
conf.preload;
821 TEdone
("preload " ^
(btos conf.preload))
824 conf.verbose
<- not
conf.verbose
;
825 TEdone
("verbose " ^
(btos conf.verbose
))
828 conf.maxhfit
<- not
conf.maxhfit
;
829 state.maxy
<- state.maxy
+ (if conf.maxhfit
then -state.h
else state.h
);
830 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
833 conf.crophack
<- not
conf.crophack
;
834 TEdone
("crophack " ^
btos conf.crophack
)
837 conf.showall
<- not
conf.showall
;
838 TEdone
("showall " ^
btos conf.showall
)
841 conf.underinfo
<- not
conf.underinfo
;
842 TEdone
("underinfo " ^
btos conf.underinfo
)
845 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
849 let maxoutlinerows () = (state.h
- 31) / 16;;
851 let enterselector allowdel
outlines errmsg
=
852 if Array.length
outlines = 0
857 Glut.setCursor
Glut.CURSOR_INHERIT
;
859 match state.layout with
861 | {pageno=pageno} :: rest
-> pageno
865 if n = Array.length
outlines
868 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
869 if outlinepageno
>= pageno then n else loop (n+1)
874 Some
(allowdel
, active,
875 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
876 Glut.postRedisplay
();
880 let enteroutlinemode () =
882 match state.outlines with
885 let a = Array.of_list
(List.rev
l) in
886 state.outlines <- Oarray
a;
888 | Onarrow
(a, b) -> a
890 enterselector false outlines "Document has no outline";
893 let enterbookmarkmode () =
894 let bookmarks = Array.of_list
state.bookmarks in
895 enterselector true bookmarks "Document has no bookmarks (yet)";
899 let quickbookmark ?title
() =
900 match state.layout with
906 let sec = Unix.gettimeofday
() in
907 let tm = Unix.localtime
sec in
908 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
912 (tm.Unix.tm_year
+ 1900)
915 | Some
title -> title
918 (title, 0, l.pageno, float l.pagey
/. float l.pageh
) :: state.bookmarks
922 state.fullscreen
<- None
;
923 Glut.reshapeWindow w h
;
926 let opendoc path password
=
929 state.password
<- password
;
930 Hashtbl.clear
state.pagemap
;
932 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
933 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
934 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i
state.h
];
937 let viewkeyboard ~key ~x ~
y =
939 state.textentry <- te
;
942 Glut.postRedisplay
()
944 match state.textentry with
946 let c = Char.chr key
in
961 Glut.postRedisplay
()
964 let ondone isforw
s =
965 cbput state.hists
.pat
s;
966 cbrfollowlen state.hists
.pat
;
967 state.searchpattern
<- s;
970 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
971 textentry, ondone (c ='
/'
)))
976 try int_of_string
s with exc
->
977 state.text <- Printf.sprintf
"bad integer `%s': %s"
978 s (Printexc.to_string exc
);
984 state.text <- "page bias is now " ^ string_of_int
n;
987 enttext (Some
('
+'
, "", None
, intentry, ondone))
993 enttext (Some
('
-'
, "", None
, optentry, ondone))
998 try int_of_string
s with exc
->
999 state.text <- Printf.sprintf
"bad integer `%s': %s"
1000 s (Printexc.to_string exc
);
1006 cbput state.hists
.pag
(string_of_int
n);
1007 cbrfollowlen state.hists
.pag
;
1008 gotoy (getpagey (n + conf.pagebias
- 1))
1011 let pageentry text key
=
1012 match Char.unsafe_chr key
with
1013 | 'g'
-> TEdone
text
1014 | _
-> intentry text key
1016 let text = "x" in text.[0] <- c;
1017 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1021 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
1022 reshape state.w
state.h
;
1025 conf.hlinks
<- not
conf.hlinks
;
1026 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1027 Glut.postRedisplay
()
1030 conf.autoscroll
<- not
conf.autoscroll
1033 begin match state.fullscreen
with
1035 state.fullscreen
<- Some
(state.w
, state.h
);
1038 state.fullscreen
<- None
;
1046 search state.searchpattern
true
1049 search state.searchpattern
false
1052 begin match state.layout with
1055 gotoy (state.y - l.pagey
);
1059 begin match List.rev
state.layout with
1062 gotoy (clamp (l.pageh
- l.pagey
))
1066 begin match state.layout with
1069 gotoy (clamp (-l.pageh
));
1074 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1076 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1078 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1082 else (100. *. (float state.y /. float maxy)) in
1085 Printf.sprintf
"Page %d of %d %.2f%%"
1086 (fn+1) state.pagecount
percent
1089 "Pages %d-%d of %d %.2f%%"
1090 (fn+1) (ln
+1) state.pagecount
percent
1095 begin match state.layout with
1098 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1099 Glut.postRedisplay
();
1103 enterbookmarkmode ()
1107 match state.layout with
1110 (s, 0, l.pageno, float l.pagey
/. float l.pageh
)
1114 enttext (Some
('~'
, "", None
, textentry, ondone))
1118 showtext ' '
"Quick bookmark added";
1121 begin match state.layout with
1123 let a = getpagewh
l.pagedimno
in
1127 (truncate
(1.8 *. (a.(1) -. a.(0))),
1128 truncate
(1.2 *. (a.(3) -. a.(0))))
1130 (truncate
(a.(1) -. a.(0)),
1131 truncate
(a.(3) -. a.(0)))
1133 doreshape (w + conf.scrollw
) h
;
1134 Glut.postRedisplay
();
1140 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1145 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1146 Glut.postRedisplay
()
1148 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1149 | 'j'
-> gotoy (clamp conf.scrollincr
)
1151 | '
r'
-> opendoc state.path
state.password
1154 vlog "huh? %d %c" key
(Char.chr key
);
1157 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1158 let len = String.length
text in
1161 state.textentry <- None
;
1162 Glut.postRedisplay
();
1165 let s = String.sub
text 0 (len - 1) in
1166 enttext (Some
(c, s, onhist, onkey
, ondone))
1169 | Some
(c, text, onhist, onkey
, ondone) ->
1170 begin match Char.unsafe_chr key
with
1173 state.textentry <- None
;
1174 Glut.postRedisplay
()
1177 state.textentry <- None
;
1178 Glut.postRedisplay
()
1181 begin match onkey
text key
with
1183 state.textentry <- None
;
1185 Glut.postRedisplay
()
1188 enttext (Some
(c, text, onhist, onkey
, ondone));
1191 state.textentry <- None
;
1192 Glut.postRedisplay
()
1195 state.textentry <- Some te
;
1196 Glut.postRedisplay
()
1201 let narrow outlines pattern
=
1202 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1206 let rec fold accu n =
1210 let (s, _
, _
, _
) as o
= outlines.(n) in
1212 if (try ignore
(Str.search_forward re
s 0); true
1213 with Not_found
-> false)
1219 let matched = fold [] (Array.length
outlines - 1) in
1220 if matched = [] then None
else Some
(Array.of_list
matched)
1223 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1224 let search active pattern incr
=
1227 if n = Array.length
outlines || n = -1
1230 let (s, _
, _
, _
) = outlines.(n) in
1232 (try ignore
(Str.search_forward re
s 0); true
1233 with Not_found
-> false)
1235 else loop (n + incr
)
1240 let re = Str.regexp_case_fold pattern
in
1246 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1249 if String.length qsearch
= 0
1252 state.outline <- None
;
1253 Glut.postRedisplay
();
1257 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1258 Glut.postRedisplay
();
1262 let incr = if key
= 18 then -1 else 1 in
1264 match search (active + incr) qsearch
incr with
1266 state.text <- qsearch ^
" [not found]";
1269 state.text <- qsearch
;
1270 active, firstof active
1272 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1273 Glut.postRedisplay
();
1276 let len = String.length qsearch
in
1283 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1286 let qsearch = String.sub
qsearch 0 (len - 1) in
1288 match search active qsearch ~
-1 with
1290 state.text <- qsearch ^
" [not found]";
1293 state.text <- qsearch;
1294 active, firstof active
1296 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1298 Glut.postRedisplay
()
1301 if active < Array.length
outlines
1303 let (_
, _
, n, t
) = outlines.(active) in
1307 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1308 state.outline <- None
;
1309 Glut.postRedisplay
();
1311 | _
when key
>= 32 && key
< 127 ->
1312 let pattern = addchar qsearch (Char.chr key
) in
1314 match search active pattern 1 with
1316 state.text <- pattern ^
" [not found]";
1319 state.text <- pattern;
1320 active, firstof active
1322 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1323 Glut.postRedisplay
()
1325 | 14 when not allowdel
->
1326 let optoutlines = narrow outlines qsearch in
1327 begin match optoutlines with
1328 | None
-> state.text <- "can't narrow"
1330 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1331 match state.outlines with
1333 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1334 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1336 Glut.postRedisplay
()
1338 | 21 when not allowdel
->
1340 match state.outlines with
1343 let a = Array.of_list
(List.rev
l) in
1344 state.outlines <- Oarray
a;
1347 state.outlines <- Oarray
b;
1350 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1351 Glut.postRedisplay
()
1355 Some
(allowdel
, active, firstof active, outlines, qsearch);
1356 Glut.postRedisplay
()
1358 | 127 when allowdel
->
1359 let len = Array.length
outlines - 1 in
1362 state.outline <- None
;
1363 state.bookmarks <- [];
1366 let bookmarks = Array.init
len
1368 let i = if i >= active then i + 1 else i in
1379 Glut.postRedisplay
()
1381 | _
-> log "unknown key %d" key
1384 let keyboard ~key ~x ~
y =
1389 match state.outline with
1390 | None
-> viewkeyboard ~key ~x ~
y
1391 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1394 let special ~key ~x ~
y =
1395 match state.outline with
1397 begin match state.textentry with
1401 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1402 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1403 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1404 | Glut.KEY_PAGE_UP
->
1405 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1407 match state.layout with
1409 | l :: _
-> state.y - l.pagey
1412 | Glut.KEY_PAGE_DOWN
->
1413 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1415 match List.rev
state.layout with
1417 | l :: _
-> getpagey l.pageno
1420 | Glut.KEY_HOME
-> addnav (); 0
1423 state.maxy - (if conf.maxhfit
then state.h
else 0)
1426 if not
conf.verbose
then state.text <- "";
1429 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1432 | Glut.KEY_UP
-> onhist HCprev
1433 | Glut.KEY_DOWN
-> onhist HCnext
1434 | Glut.KEY_HOME
-> onhist HCfirst
1435 | Glut.KEY_END
-> onhist HClast
1438 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1439 Glut.postRedisplay
()
1444 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1445 let maxrows = maxoutlinerows () in
1447 let active = active + incr in
1448 let active = max
0 (min
active (Array.length
outlines - 1)) in
1452 let rows = active - first in
1453 if rows > maxrows then active - maxrows else first
1456 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1457 Glut.postRedisplay
()
1460 | Glut.KEY_UP
-> navigate ~
-1
1461 | Glut.KEY_DOWN
-> navigate 1
1462 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1463 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1466 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1467 Glut.postRedisplay
()
1470 let active = Array.length
outlines - 1 in
1471 let first = max
0 (active - maxrows) in
1472 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1473 Glut.postRedisplay
()
1478 let drawplaceholder l =
1479 GlDraw.color
(scalecolor 1.0);
1481 (0.0, float l.pagedispy
)
1482 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1485 and y = float (l.pagedispy
+ 13) in
1486 let font = Glut.BITMAP_8_BY_13
in
1487 GlDraw.color
(0.0, 0.0, 0.0);
1488 GlPix.raster_pos ~
x ~
y ();
1489 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1490 ("Loading " ^ string_of_int
l.pageno);
1493 let now () = Unix.gettimeofday
();;
1496 begin match getopaque l.pageno with
1497 | Some
opaque when validopaque opaque ->
1498 if state.textentry = None
1499 then GlDraw.color
(scalecolor 1.0)
1500 else GlDraw.color
(scalecolor 0.4);
1502 draw
(l.pagedispy
, l.pagew
, l.pagevh
, l.pagey
, conf.hlinks
) opaque;
1505 vlog "draw %f sec" d;
1510 GlDraw.color
(0.5, 0.5, 0.5);
1513 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1515 l.pagedispy
+ l.pagevh
;
1518 let scrollindicator () =
1519 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1520 GlDraw.color
(0.64 , 0.64, 0.64);
1522 (float (state.w - conf.scrollw
), 0.)
1523 (float state.w, float state.h
)
1525 GlDraw.color
(0.0, 0.0, 0.0);
1526 let sh = (float (maxy + state.h
) /. float state.h
) in
1527 let sh = float state.h
/. sh in
1528 let sh = max
sh (float conf.scrollh
) in
1531 if state.y = state.maxy
1533 else float state.y /. float maxy
1535 let position = (float state.h
-. sh) *. percent in
1538 if position +. sh > float state.h
1545 (float (state.w - conf.scrollw
), position)
1546 (float state.w, position +. sh)
1551 match state.mstate
with
1555 | Msel
((x0
, y0
), (x1
, y1
)) ->
1556 let rec loop = function
1558 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1559 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1561 match getopaque l.pageno with
1562 | Some
opaque when validopaque opaque ->
1563 let oy = -l.pagey
+ l.pagedispy
in
1564 seltext
opaque (x0
, y0
, x1
, y1
) oy;
1575 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1576 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1578 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1580 if l.pageno = pageno
1582 let d = float (l.pagedispy
- l.pagey
) in
1583 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1584 GlDraw.begins `quads
;
1586 GlDraw.vertex2
(x0
, y0
+.d);
1587 GlDraw.vertex2
(x1
, y1
+.d);
1588 GlDraw.vertex2
(x2
, y2
+.d);
1589 GlDraw.vertex2
(x3
, y3
+.d);
1599 let showoutline = function
1601 | Some
(allowdel
, active, first, outlines, qsearch) ->
1603 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1604 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1605 GlDraw.rect
(0., 0.) (float state.w, float state.h
);
1608 GlDraw.color
(1., 1., 1.);
1609 let font = Glut.BITMAP_9_BY_15
in
1610 let draw_string x y s =
1611 GlPix.raster_pos ~
x ~
y ();
1612 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1615 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1618 let (s, l, _
, _
) = outlines.(row
) in
1619 let y = (row
- first) * 16 in
1624 GlDraw.polygon_mode `both `line
;
1625 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1626 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1627 GlDraw.rect
(0., float (y + 1))
1628 (float (state.w - conf.scrollw
- 1), float (y + 18));
1629 GlDraw.polygon_mode `both `fill
;
1631 GlDraw.color
(1., 1., 1.);
1633 draw_string (float x) (float (y + 16)) s;
1641 let lasty = List.fold_left
drawpage 0 (state.layout) in
1642 GlDraw.color
(scalecolor 0.5);
1645 (float (state.w - conf.scrollw
), float state.h
)
1650 showoutline state.outline;
1652 Glut.swapBuffers
();
1656 let rec f = function
1658 begin match getopaque l.pageno with
1659 | Some
opaque when validopaque opaque ->
1660 let y = y - l.pagedispy
in
1663 let y = l.pagey
+ y in
1664 match whatsunder
opaque x y with
1677 let mouse ~button ~bstate ~
x ~
y =
1679 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1687 let incr = incr * 2 in
1688 let y = clamp incr in
1691 | Glut.LEFT_BUTTON
when state.outline = None
->
1692 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1693 begin match dest with
1694 | Ulinkgoto
(pageno, top
) ->
1697 gotopage1 pageno top
1702 | Unone
when bstate
= Glut.DOWN
->
1703 Glut.setCursor
Glut.CURSOR_INHERIT
;
1704 state.mstate
<- Mnone
1706 | Unone
| Utext _
->
1707 if bstate
= Glut.DOWN
1709 if state.rotate mod 360 = 0
1711 state.mstate
<- Msel
((x, y), (x, y));
1712 Glut.postRedisplay
()
1716 match state.mstate
with
1718 | Msel
((x0
, y0
), (x1
, y1
)) ->
1720 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1721 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1723 match getopaque l.pageno with
1724 | Some
opaque when validopaque opaque ->
1728 List.iter
f state.layout;
1729 copysel
""; (* ugly *)
1730 Glut.setCursor
Glut.CURSOR_INHERIT
;
1731 state.mstate
<- Mnone
;
1738 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1741 if state.outline = None
1743 match state.mstate
with
1746 state.mstate
<- Msel
(a, (x, y));
1747 Glut.postRedisplay
()
1751 if state.outline = None
1753 match state.mstate
with
1755 begin match getunder x y with
1756 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
1758 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
1759 Glut.setCursor
Glut.CURSOR_INFO
1760 | Ulinkgoto
(page
, y) ->
1761 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
1762 Glut.setCursor
Glut.CURSOR_INFO
1764 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
1765 Glut.setCursor
Glut.CURSOR_TEXT
1775 if Sys.os_type
= "Win32"
1777 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1779 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1781 Filename.concat
home "llpp"
1785 let ic = open_in_bin
statepath in
1786 let hash = input_value
ic in
1792 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1799 match state.fullscreen
with
1800 | None
-> state.w, state.h
1803 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h
);
1804 let oc = open_out_bin
statepath in
1805 output_value
oc pstate
1809 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1814 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1817 state.bookmarks <- statebookmarks;
1818 with Not_found
-> ()
1820 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1824 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
1825 (fun s -> state.path
<- s)
1826 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
1829 if String.length
state.path
= 0
1830 then (prerr_endline
"filename missing"; exit
1)
1835 let _ = Glut.init
Sys.argv
in
1836 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1837 let () = Glut.initWindowSize
state.w state.h
in
1838 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1841 if Sys.os_type
= "Unix"
1843 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1845 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1846 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1847 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1848 Unix.bind
sock addr;
1850 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1851 Unix.connect
csock addr;
1852 let ssock, _ = Unix.accept
sock in
1855 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1856 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1860 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1864 let () = Glut.displayFunc
display in
1865 let () = Glut.reshapeFunc
reshape in
1866 let () = Glut.keyboardFunc
keyboard in
1867 let () = Glut.specialFunc
special in
1868 let () = Glut.idleFunc
(Some
idle) in
1869 let () = Glut.mouseFunc
mouse in
1870 let () = Glut.motionFunc
motion in
1871 let () = Glut.passiveMotionFunc
pmotion in
1874 state.csock <- csock;
1875 state.ssock <- ssock;
1876 state.text <- "Opening " ^
name;
1877 writecmd state.csock ("open " ^
state.path ^
"\000" ^
state.password ^
"\000");
1881 let rec handlelablglutbug () =
1884 with Glut.BadEnum
"key in special_of_int" ->
1885 showtext '
!'
" LablGlut bug: special key not recognized";
1886 handlelablglutbug ()
1888 handlelablglutbug ();