4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let log fmt
= Printf.kprintf prerr_endline fmt
;;
9 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
11 external init
: Unix.file_descr
-> unit = "ml_init";;
12 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
13 external seltext
: string -> (int * int * int * int) -> int -> unit =
15 external copysel
: string -> unit = "ml_copysel";;
16 external getpagewh
: int -> float array
= "ml_getpagewh";;
17 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
20 type mstate
= Msel
of (mpos
* mpos
) | Mpan
of mpos
| Mnone
;;
30 type textentry
= (char
* string * onhist
option * onkey
* ondone
)
31 and onkey
= string -> int -> te
32 and ondone
= string -> unit
33 and onhist
= histcmd
-> string
34 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
39 | TEswitch
of textentry
43 { store
= Array.create n v
50 let cblen b
= Array.length b
.store
;;
55 b
.wc
<- (b
.wc
+ 1) mod len;
56 b
.len <- min
(b
.len + 1) len;
59 let cbpeekw b
= b
.store
.(b
.wc
);;
65 let rc = b
.rc + dir
in
66 let rc = if rc = -1 then b
.len - 1 else rc in
67 let rc = if rc = b
.len then 0 else rc in
78 Array.fill b
.store
0 (Array.length b
.store
) v
;
93 { mutable scrollw
: int
94 ; mutable scrollh
: int
95 ; mutable icase
: bool
96 ; mutable preload
: bool
97 ; mutable pagebias
: int
98 ; mutable verbose
: bool
99 ; mutable scrollincr
: int
100 ; mutable maxhfit
: bool
101 ; mutable crophack
: bool
102 ; mutable autoscroll
: bool
103 ; mutable showall
: bool
104 ; mutable hlinks
: bool
105 ; mutable underinfo
: bool
106 ; mutable interpagespace
: int
107 ; mutable zoom
: float
108 ; mutable presentation
: bool
112 type outline
= string * int * int * float;;
114 | Oarray
of outline array
115 | Olist
of outline list
116 | Onarrow
of outline array
* outline array
119 type rect
= (float * float * float * float * float * float * float * float);;
122 { mutable csock
: Unix.file_descr
123 ; mutable ssock
: Unix.file_descr
127 ; mutable rotate
: int
132 ; mutable layout
: layout list
133 ; pagemap
: ((int * int * int), string) Hashtbl.t
134 ; mutable pages
: (int * int * int) list
135 ; mutable pagecount
: int
136 ; pagecache
: string circbuf
137 ; mutable rendering
: bool
138 ; mutable mstate
: mstate
139 ; mutable searchpattern
: string
140 ; mutable rects
: (int * int * rect
) list
141 ; mutable rects1
: (int * int * rect
) list
142 ; mutable text
: string
143 ; mutable fullscreen
: (int * int) option
144 ; mutable textentry
: textentry
option
145 ; mutable outlines
: outlines
146 ; mutable outline
: (bool * int * int * outline array
* string) option
147 ; mutable bookmarks
: outline list
148 ; mutable path
: string
149 ; mutable password
: string
150 ; mutable invalidated
: int
151 ; mutable colorscale
: float
155 { pat
: string circbuf
156 ; pag
: string circbuf
157 ; nav
: float circbuf
177 ; presentation
= false
193 ; pagemap
= Hashtbl.create
10
194 ; pagecache
= cbnew 10 ""
205 ; outlines
= Olist
[]
212 { nav
= cbnew 100 0.0
223 Printf.kprintf prerr_endline fmt
225 Printf.kprintf ignore fmt
229 let len = String.length s
in
231 let b = Buffer.create
n in
232 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
233 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
234 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
235 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
236 Buffer.add_string
b s
;
237 let s'
= Buffer.contents
b in
238 let n'
= Unix.write fd
s'
0 n in
239 if n'
!= n then failwith
"write failed";
244 let n = Unix.read fd
s 0 4 in
245 if n != 4 then failwith
"incomplete read(len)";
247 lor (Char.code
s.[0] lsl 24)
248 lor (Char.code
s.[1] lsl 16)
249 lor (Char.code
s.[2] lsl 8)
250 lor (Char.code
s.[3] lsl 0)
252 let s = String.create
len in
253 let n = Unix.read fd
s 0 len in
254 if n != len then failwith
"incomplete read(data)";
261 else float y
/. float state.maxy
265 let b = Buffer.create
10 in
266 Buffer.add_string
b s;
267 let rec combine = function
270 Buffer.add_char
b ' '
;
273 | `
b b -> if b then "1" else "0"
275 | `i i
-> string_of_int i
276 | `f f
-> string_of_float f
277 | `I f
-> string_of_int
(truncate f
)
279 Buffer.add_string
b s;
286 let cmd = Buffer.contents
(makecmd s l
) in
287 writecmd state.csock
cmd;
293 let d = state.h
- h
in
300 let rec f pn ph pi fh l
=
302 | (n, _
, h
) :: rest
->
303 let ips = calcips h
in
305 if n = 0 && conf.presentation
311 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
320 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
323 let fh = f 0 0 0 0 state.pages
in
327 let getpageyh pageno
=
328 let rec f pn ph pi y l
=
330 | (n, _
, h
) :: rest
->
331 let ips = calcips h
in
334 y
+ (pageno
- pn
) * (ph
+ pi
), h
336 let y = y + (n - pn
) * (ph
+ pi
) in
340 y + (pageno
- pn
) * (ph
+ pi
), ph
342 f 0 0 0 0 state.pages
345 let getpagey pageno
= fst
(getpageyh pageno
);;
348 let rec f ~pageno ~pdimno ~prev ~py ~vh ~pdims ~cacheleft ~accu
=
349 let ((w
, h
, ips) as curr
), rest
, pdimno
, p0a
=
351 | (pageno'
, w
, h
) :: rest
when pageno'
= pageno
->
352 let ips = calcips h
in
353 (w
, h
, ips), rest
, pdimno
+ 1,
354 if conf.presentation
then ips else 0
356 prev
, pdims
, pdimno
, 0
358 if pageno
= state.pagecount
|| cacheleft
= 0 || vh
>= sh
366 let py = py + h
+ ips in
367 let vh = max
0 (py - y) in
378 let left = h
- top in
379 let left = min
(sh
- vh) left in
380 let py = py + h
+ ips in
381 let p0y = if top < 0 then -top else 0 in
392 let accu = e :: accu in
399 ~cacheleft
:(cacheleft
-1)
402 if state.invalidated
= 0
412 ~cacheleft
:(cblen state.pagecache
)
422 let y = state.y + incr
in
424 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
428 let getopaque pageno
=
429 try Some
(Hashtbl.find
state.pagemap
(pageno
+ 1, state.w
, state.rotate
))
430 with Not_found
-> None
433 let cache pageno opaque
=
434 Hashtbl.replace
state.pagemap
(pageno
+ 1, state.w
, state.rotate
) opaque
437 let validopaque opaque
= String.length opaque
> 0;;
440 match getopaque l
.pageno
with
441 | None
when not
state.rendering
->
442 state.rendering
<- true;
444 wcmd "render" [`i
(l
.pageno
+ 1)
452 let loadlayout layout =
453 let rec f all
= function
455 begin match getopaque l
.pageno
with
456 | None
-> render l
; f false ls
457 | Some opaque
-> f (all
&& validopaque opaque
) ls
461 f (layout <> []) layout;
468 let evictedopaque = cbpeekw state.pagecache
in
469 List.exists
(fun l
->
470 match getopaque l
.pageno
with
471 | Some opaque
when validopaque opaque
->
472 evictedopaque = opaque
476 if not
evictedvisible
478 let y = if state.y < state.h
then 0 else state.y - state.h
in
479 let pages = layout y (state.h
*3) in
480 List.iter
render pages;
485 let y = min
state.maxy
y in
486 let pages = layout y state.h
in
487 let ready = loadlayout pages in
488 state.ty
<- yratio y;
493 state.layout <- pages;
495 Glut.postRedisplay
();
499 state.layout <- pages;
501 Glut.postRedisplay
();
507 cbput state.hists
.nav
(yratio state.y);
508 cbrfollowlen state.hists
.nav
;
512 let y = cbget state.hists
.nav ~
-1 in
513 truncate
(y *. float state.maxy
)
517 let y, h
= getpageyh n in
519 gotoy (y + (truncate
(top *. float h
)));
522 let gotopage1 n top =
523 let y = getpagey n in
533 state.invalidated
<- state.invalidated
+ 1;
537 let c = c *. state.colorscale
in
543 match state.layout with
545 let rely = yratio state.y in
546 state.maxy
<- calcheight ();
547 truncate
(float state.maxy
*. rely)
550 state.maxy
<- calcheight ();
557 let margin = truncate
(0.5 *. (float w
-. float w
*. conf.zoom
)) in
559 let w = w - margin * 2 - conf.scrollw
in
562 GlMat.mode `modelview
;
563 GlMat.load_identity
();
564 GlMat.mode `projection
;
565 GlMat.load_identity
();
566 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
567 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
568 GlMat.scale3
(2.0 /. float w, 2.0 /. float state.h
, 1.0);
569 GlClear.color
(scalecolor 1.0);
570 GlClear.clear
[`color
];
573 wcmd "geometry" [`i
state.w; `i h
];
577 GlDraw.viewport
0 0 state.winw
state.h
;
578 GlDraw.color
(0.0, 0.0, 0.0);
579 let sw = float (conf.scrollw
- 1) *. float state.w /. float state.winw
in
581 (0.0, float (state.h
- 18))
582 (float state.w -. sw, float state.h
)
584 let font = Glut.BITMAP_8_BY_13
in
585 GlDraw.color
(1.0, 1.0, 1.0);
586 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
587 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
588 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
592 let len = String.length
state.text
in
593 match state.textentry
with
595 if len > 0 then showtext ' '
state.text
597 | Some
(c, text
, _
, _
, _
) ->
601 text ^
" [" ^
state.text ^
"]"
611 state.text
<- Printf.sprintf
"%c%s" c s;
612 Glut.postRedisplay
();
626 state.rects
<- state.rects1
;
627 Glut.postRedisplay
()
630 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
631 state.pagecount
<- n;
632 state.invalidated
<- state.invalidated
- 1;
633 if state.invalidated
= 0
637 let s = Scanf.sscanf
cmd "t %n"
638 (fun n -> String.sub
cmd n (String.length
cmd - n))
640 Glut.setWindowTitle
s
643 let s = Scanf.sscanf
cmd "T %n"
644 (fun n -> String.sub
cmd n (String.length
cmd - n))
646 if state.textentry
= None
653 Glut.postRedisplay
();
659 let s = Scanf.sscanf
cmd "V %n"
660 (fun n -> String.sub
cmd n (String.length
cmd - n))
666 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
667 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
668 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
669 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
671 let y = (getpagey pageno) + truncate y0
in
674 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
677 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
678 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
679 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
680 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
683 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
687 Scanf.sscanf
cmd "r %d %d %d %d %s"
688 (fun n w h r p
-> (n, w, h
, r
, p
))
690 Hashtbl.replace
state.pagemap
(n, w, r
) p
;
691 let opaque = cbpeekw state.pagecache
in
692 if validopaque opaque
696 (fun k v a
-> if v
= opaque then k else a
)
697 state.pagemap
(-1, -1, -1)
699 wcmd "free" [`
s opaque];
700 Hashtbl.remove
state.pagemap
k
702 cbput state.pagecache p
;
703 state.rendering
<- false;
705 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy
)))
707 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
710 else (ignore
(loadlayout state.layout); preload ())
714 let (n, w, h
) as pagelayout
=
715 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w, h
)
717 state.pages <- pagelayout
:: state.pages
720 let (l
, n, t
, h
, pos
) =
721 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
723 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
725 let l = String.length
s in
726 let b = Buffer.create
(String.length
s) in
733 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
736 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
745 let outline = (s, l, n, float t
/. float h
) in
747 match state.outlines with
748 | Olist
outlines -> Olist
(outline :: outlines)
749 | Oarray _
-> Olist
[outline]
750 | Onarrow _
-> Olist
[outline]
752 state.outlines <- outlines
755 log "unknown cmd `%S'" cmd
758 let now = Unix.gettimeofday
;;
762 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
767 let y = state.y + conf.scrollincr
in
768 let y = if y >= state.maxy
then 0 else y in
774 let cmd = readcmd state.csock
in
781 let onhist cb
= function
782 | HCprev
-> cbget cb ~
-1
783 | HCnext
-> cbget cb
1
784 | HCfirst
-> cbget cb ~
-(cb
.rc)
785 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
788 let search pattern forward
=
789 if String.length pattern
> 0
792 match state.layout with
795 l.pageno, (l.pagey
+ if forward
then 0 else 0*l.pagevh
)
798 let b = makecmd "search"
799 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
801 Buffer.add_char
b '
,'
;
802 Buffer.add_string
b pattern
;
803 Buffer.add_char
b '
\000'
;
806 writecmd state.csock
cmd;
809 let intentry text key
=
810 let c = Char.unsafe_chr key
in
813 let s = "x" in s.[0] <- c;
814 let text = text ^
s in
818 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
823 let b = Buffer.create
(String.length
s + 1) in
824 Buffer.add_string
b s;
829 let textentry text key
=
830 let c = Char.unsafe_chr key
in
832 | _
when key
>= 32 && key
< 127 ->
833 let text = addchar text c in
837 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
842 state.rotate <- angle
;
844 wcmd "rotate" [`i angle
];
847 let optentry text key
=
848 let btos b = if b then "on" else "off" in
849 let c = Char.unsafe_chr key
in
853 try conf.scrollincr
<- int_of_string
s with exc
->
854 state.text <- Printf.sprintf
"bad integer `%s': %s"
855 s (Printexc.to_string exc
)
857 TEswitch
('#'
, "", None
, intentry, ondone)
862 Some
(int_of_string
s)
864 state.text <- Printf.sprintf
"bad integer `%s': %s"
865 s (Printexc.to_string exc
);
868 | Some angle
-> rotate angle
871 TEswitch
('^'
, "", None
, intentry, ondone)
874 conf.icase
<- not
conf.icase
;
875 TEdone
("case insensitive search " ^
(btos conf.icase
))
878 conf.preload <- not
conf.preload;
880 TEdone
("preload " ^
(btos conf.preload))
883 conf.verbose
<- not
conf.verbose
;
884 TEdone
("verbose " ^
(btos conf.verbose
))
887 conf.maxhfit
<- not
conf.maxhfit
;
888 state.maxy
<- state.maxy
+ (if conf.maxhfit
then -state.h
else state.h
);
889 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
892 conf.crophack
<- not
conf.crophack
;
893 TEdone
("crophack " ^
btos conf.crophack
)
896 conf.showall
<- not
conf.showall
;
897 TEdone
("showall " ^
btos conf.showall
)
900 conf.underinfo
<- not
conf.underinfo
;
901 TEdone
("underinfo " ^
btos conf.underinfo
)
906 conf.interpagespace
<- int_of_string
s;
907 let rely = yratio state.y in
908 state.maxy
<- calcheight ();
909 gotoy (truncate
(float state.maxy
*. rely));
911 state.text <- Printf.sprintf
"bad integer `%s': %s"
912 s (Printexc.to_string exc
)
914 TEswitch
('
%'
, "", None
, intentry, ondone)
917 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
921 let maxoutlinerows () = (state.h
- 31) / 16;;
923 let enterselector allowdel
outlines errmsg
=
924 if Array.length
outlines = 0
929 Glut.setCursor
Glut.CURSOR_INHERIT
;
931 match state.layout with
933 | {pageno=pageno} :: rest
-> pageno
937 if n = Array.length
outlines
940 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
941 if outlinepageno
>= pageno then n else loop (n+1)
946 Some
(allowdel
, active,
947 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
948 Glut.postRedisplay
();
952 let enteroutlinemode () =
954 match state.outlines with
957 let a = Array.of_list
(List.rev
l) in
958 state.outlines <- Oarray
a;
960 | Onarrow
(a, b) -> a
962 enterselector false outlines "Document has no outline";
965 let enterbookmarkmode () =
966 let bookmarks = Array.of_list
state.bookmarks in
967 enterselector true bookmarks "Document has no bookmarks (yet)";
971 let quickbookmark ?title
() =
972 match state.layout with
978 let sec = Unix.gettimeofday
() in
979 let tm = Unix.localtime
sec in
980 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
984 (tm.Unix.tm_year
+ 1900)
987 | Some
title -> title
990 (title, 0, l.pageno, float l.pagey
/. float l.pageh
) :: state.bookmarks
994 state.fullscreen
<- None
;
995 Glut.reshapeWindow
w h
;
998 let opendoc path password
=
1001 state.password
<- password
;
1002 Hashtbl.clear
state.pagemap
;
1004 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1005 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1006 wcmd "geometry" [`i
state.w; `i
state.h
];
1009 let viewkeyboard ~key ~x ~
y =
1011 state.textentry <- te
;
1014 Glut.postRedisplay
()
1016 match state.textentry with
1018 let c = Char.chr key
in
1024 let y = getnav () in
1033 Glut.postRedisplay
()
1036 let ondone isforw
s =
1037 cbput state.hists
.pat
s;
1038 cbrfollowlen state.hists
.pat
;
1039 state.searchpattern
<- s;
1042 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1043 textentry, ondone (c ='
/'
)))
1045 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1046 conf.zoom
<- min
10.0 (conf.zoom
+. 0.1);
1047 reshape state.winw
state.h
1052 try int_of_string
s with exc
->
1053 state.text <- Printf.sprintf
"bad integer `%s': %s"
1054 s (Printexc.to_string exc
);
1060 state.text <- "page bias is now " ^ string_of_int
n;
1063 enttext (Some
('
+'
, "", None
, intentry, ondone))
1065 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1066 conf.zoom
<- max
0.1 (conf.zoom
-. 0.1);
1067 if conf.zoom
<= 1.0 then state.x
<- 0;
1068 reshape state.winw
state.h
;
1074 enttext (Some
('
-'
, "", None
, optentry, ondone))
1076 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1079 reshape state.winw
state.h
1084 try int_of_string
s with exc
->
1085 state.text <- Printf.sprintf
"bad integer `%s': %s"
1086 s (Printexc.to_string exc
);
1092 cbput state.hists
.pag
(string_of_int
n);
1093 cbrfollowlen state.hists
.pag
;
1094 gotoy (getpagey (n + conf.pagebias
- 1))
1097 let pageentry text key
=
1098 match Char.unsafe_chr key
with
1099 | 'g'
-> TEdone
text
1100 | _
-> intentry text key
1102 let text = "x" in text.[0] <- c;
1103 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1107 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
1108 reshape state.winw
state.h
;
1111 conf.hlinks
<- not
conf.hlinks
;
1112 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1113 Glut.postRedisplay
()
1116 conf.autoscroll
<- not
conf.autoscroll
1119 conf.presentation
<- not
conf.presentation
;
1123 begin match state.fullscreen
with
1125 state.fullscreen
<- Some
(state.w, state.h
);
1128 state.fullscreen
<- None
;
1136 search state.searchpattern
true
1139 search state.searchpattern
false
1142 begin match state.layout with
1145 gotoy (getpagey l.pageno)
1149 begin match List.rev
state.layout with
1152 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1153 gotoy (getpagey pageno)
1157 begin match state.layout with
1160 let pageno = max
0 (l.pageno-1) in
1161 gotoy (getpagey pageno)
1166 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1168 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1170 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1174 else (100. *. (float state.y /. float maxy)) in
1177 Printf.sprintf
"Page %d of %d %.2f%%"
1178 (fn+1) state.pagecount
percent
1181 "Pages %d-%d of %d %.2f%%"
1182 (fn+1) (ln
+1) state.pagecount
percent
1187 begin match state.layout with
1190 doreshape l.pagew
l.pageh
;
1191 Glut.postRedisplay
();
1195 enterbookmarkmode ()
1199 match state.layout with
1202 (s, 0, l.pageno, float l.pagey
/. float l.pageh
)
1206 enttext (Some
('~'
, "", None
, textentry, ondone))
1210 showtext ' '
"Quick bookmark added";
1213 begin match state.layout with
1215 let a = getpagewh
l.pagedimno
in
1219 (truncate
(1.8 *. (a.(1) -. a.(0))),
1220 truncate
(1.2 *. (a.(3) -. a.(0))))
1222 (truncate
(a.(1) -. a.(0)),
1223 truncate
(a.(3) -. a.(0)))
1226 Glut.postRedisplay
();
1232 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1237 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1238 Glut.postRedisplay
()
1240 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1241 | 'j'
-> gotoy (clamp conf.scrollincr
)
1243 | '
r'
-> opendoc state.path
state.password
1246 vlog "huh? %d %c" key
(Char.chr key
);
1249 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1250 let len = String.length
text in
1253 state.textentry <- None
;
1254 Glut.postRedisplay
();
1257 let s = String.sub
text 0 (len - 1) in
1258 enttext (Some
(c, s, onhist, onkey
, ondone))
1261 | Some
(c, text, onhist, onkey
, ondone) ->
1262 begin match Char.unsafe_chr key
with
1265 state.textentry <- None
;
1266 Glut.postRedisplay
()
1269 state.textentry <- None
;
1270 Glut.postRedisplay
()
1273 begin match onkey
text key
with
1275 state.textentry <- None
;
1277 Glut.postRedisplay
()
1280 enttext (Some
(c, text, onhist, onkey
, ondone));
1283 state.textentry <- None
;
1284 Glut.postRedisplay
()
1287 state.textentry <- Some te
;
1288 Glut.postRedisplay
()
1293 let narrow outlines pattern
=
1294 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1298 let rec fold accu n =
1302 let (s, _
, _
, _
) as o
= outlines.(n) in
1304 if (try ignore
(Str.search_forward re
s 0); true
1305 with Not_found
-> false)
1311 let matched = fold [] (Array.length
outlines - 1) in
1312 if matched = [] then None
else Some
(Array.of_list
matched)
1315 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1316 let search active pattern incr
=
1319 if n = Array.length
outlines || n = -1
1322 let (s, _
, _
, _
) = outlines.(n) in
1324 (try ignore
(Str.search_forward re
s 0); true
1325 with Not_found
-> false)
1327 else loop (n + incr
)
1332 let re = Str.regexp_case_fold pattern
in
1338 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1341 if String.length qsearch
= 0
1344 state.outline <- None
;
1345 Glut.postRedisplay
();
1349 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1350 Glut.postRedisplay
();
1354 let incr = if key
= 18 then -1 else 1 in
1356 match search (active + incr) qsearch
incr with
1358 state.text <- qsearch ^
" [not found]";
1361 state.text <- qsearch
;
1362 active, firstof active
1364 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1365 Glut.postRedisplay
();
1368 let len = String.length qsearch
in
1375 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1378 let qsearch = String.sub
qsearch 0 (len - 1) in
1380 match search active qsearch ~
-1 with
1382 state.text <- qsearch ^
" [not found]";
1385 state.text <- qsearch;
1386 active, firstof active
1388 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1390 Glut.postRedisplay
()
1393 if active < Array.length
outlines
1395 let (_
, _
, n, t
) = outlines.(active) in
1399 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1400 state.outline <- None
;
1401 Glut.postRedisplay
();
1403 | _
when key
>= 32 && key
< 127 ->
1404 let pattern = addchar qsearch (Char.chr key
) in
1406 match search active pattern 1 with
1408 state.text <- pattern ^
" [not found]";
1411 state.text <- pattern;
1412 active, firstof active
1414 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1415 Glut.postRedisplay
()
1417 | 14 when not allowdel
->
1418 let optoutlines = narrow outlines qsearch in
1419 begin match optoutlines with
1420 | None
-> state.text <- "can't narrow"
1422 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1423 match state.outlines with
1425 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1426 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1428 Glut.postRedisplay
()
1430 | 21 when not allowdel
->
1432 match state.outlines with
1435 let a = Array.of_list
(List.rev
l) in
1436 state.outlines <- Oarray
a;
1439 state.outlines <- Oarray
b;
1442 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1443 Glut.postRedisplay
()
1447 Some
(allowdel
, active, firstof active, outlines, qsearch);
1448 Glut.postRedisplay
()
1450 | 127 when allowdel
->
1451 let len = Array.length
outlines - 1 in
1454 state.outline <- None
;
1455 state.bookmarks <- [];
1458 let bookmarks = Array.init
len
1460 let i = if i >= active then i + 1 else i in
1471 Glut.postRedisplay
()
1473 | _
-> log "unknown key %d" key
1476 let keyboard ~key ~x ~
y =
1481 match state.outline with
1482 | None
-> viewkeyboard ~key ~x ~
y
1483 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1486 let special ~key ~x ~
y =
1487 match state.outline with
1489 begin match state.textentry with
1493 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1494 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1495 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1496 | Glut.KEY_PAGE_UP
->
1497 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1499 match state.layout with
1501 | l :: _
-> state.y - l.pagey
1504 | Glut.KEY_PAGE_DOWN
->
1505 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1507 match List.rev
state.layout with
1509 | l :: _
-> getpagey l.pageno
1512 | Glut.KEY_HOME
-> addnav (); 0
1515 state.maxy - (if conf.maxhfit
then state.h
else 0)
1517 | Glut.KEY_RIGHT
when conf.zoom
> 1.0 ->
1518 state.x
<- state.x
- 10;
1520 | Glut.KEY_LEFT
when conf.zoom
> 1.0 ->
1521 state.x
<- state.x
+ 10;
1526 if not
conf.verbose
then state.text <- "";
1529 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1532 | Glut.KEY_UP
-> onhist HCprev
1533 | Glut.KEY_DOWN
-> onhist HCnext
1534 | Glut.KEY_HOME
-> onhist HCfirst
1535 | Glut.KEY_END
-> onhist HClast
1538 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1539 Glut.postRedisplay
()
1544 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1545 let maxrows = maxoutlinerows () in
1547 let active = active + incr in
1548 let active = max
0 (min
active (Array.length
outlines - 1)) in
1552 let rows = active - first in
1553 if rows > maxrows then active - maxrows else first
1556 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1557 Glut.postRedisplay
()
1560 | Glut.KEY_UP
-> navigate ~
-1
1561 | Glut.KEY_DOWN
-> navigate 1
1562 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1563 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1566 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1567 Glut.postRedisplay
()
1570 let active = Array.length
outlines - 1 in
1571 let first = max
0 (active - maxrows) in
1572 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1573 Glut.postRedisplay
()
1578 let drawplaceholder l =
1579 GlDraw.color
(scalecolor 1.0);
1581 (0.0, float l.pagedispy
)
1582 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1585 and y = float (l.pagedispy
+ 13) in
1586 let font = Glut.BITMAP_8_BY_13
in
1587 GlDraw.color
(0.0, 0.0, 0.0);
1588 GlPix.raster_pos ~
x ~
y ();
1589 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1590 ("Loading " ^ string_of_int
l.pageno);
1593 let now () = Unix.gettimeofday
();;
1596 begin match getopaque l.pageno with
1597 | Some
opaque when validopaque opaque ->
1598 if state.textentry = None
1599 then GlDraw.color
(scalecolor 1.0)
1600 else GlDraw.color
(scalecolor 0.4);
1602 draw
(l.pagedispy
, l.pagew
, l.pagevh
, l.pagey
, conf.hlinks
)
1606 vlog "draw %d %f sec" l.pageno d;
1611 l.pagedispy
+ l.pagevh
;
1614 let scrollindicator () =
1615 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1616 GlDraw.color
(0.64 , 0.64, 0.64);
1619 (float conf.scrollw
, float state.h
)
1621 GlDraw.color
(0.0, 0.0, 0.0);
1622 let sh = (float (maxy + state.h
) /. float state.h
) in
1623 let sh = float state.h
/. sh in
1624 let sh = max
sh (float conf.scrollh
) in
1627 if state.y = state.maxy
1629 else float state.y /. float maxy
1631 let position = (float state.h
-. sh) *. percent in
1634 if position +. sh > float state.h
1642 (float conf.scrollw
, position +. sh)
1646 let showsel margin =
1647 match state.mstate
with
1651 | Msel
((x0
, y0
), (x1
, y1
)) ->
1652 let rec loop = function
1654 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1655 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1657 match getopaque l.pageno with
1658 | Some
opaque when validopaque opaque ->
1659 let oy = -l.pagey
+ l.pagedispy
in
1661 (x0
- margin - state.x, y0
,
1662 x1
- margin - state.x, y1
) oy;
1672 let panx = float state.x in
1674 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1675 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1677 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1679 if l.pageno = pageno
1681 let d = float (l.pagedispy
- l.pagey
) in
1682 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1683 GlDraw.begins `quads
;
1685 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1686 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1687 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1688 GlDraw.vertex2
(x3
+.panx, y3
+.d);
1698 let showoutline = function
1700 | Some
(allowdel
, active, first, outlines, qsearch) ->
1702 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1703 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1704 GlDraw.rect
(0., 0.) (float state.w, float state.h
);
1707 GlDraw.color
(1., 1., 1.);
1708 let font = Glut.BITMAP_9_BY_15
in
1709 let draw_string x y s =
1710 GlPix.raster_pos ~
x ~
y ();
1711 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1714 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1717 let (s, l, _
, _
) = outlines.(row
) in
1718 let y = (row
- first) * 16 in
1723 GlDraw.polygon_mode `both `line
;
1724 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1725 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1726 let sw = float (conf.scrollw
- 1) *. float state.w /. float state.winw
in
1727 GlDraw.rect
(0., float (y + 1))
1728 ((float state.w -. sw), float (y + 18));
1729 GlDraw.polygon_mode `both `fill
;
1731 GlDraw.color
(1., 1., 1.);
1733 draw_string (float x) (float (y + 16)) s;
1741 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1742 GlDraw.viewport
margin 0 state.w state.h
;
1743 GlClear.color
(scalecolor 0.5);
1744 GlClear.clear
[`color
];
1747 let x = float state.x in
1748 GlMat.translate ~
x ();
1752 Gl.enable `scissor_test
;
1753 GlMisc.scissor
0 0 (state.winw
- conf.scrollw
) state.h
;
1755 let _lasty = List.fold_left
drawpage 0 (state.layout) in
1758 Gl.disable `scissor_test
1762 let x = -.float state.x in
1763 GlMat.translate ~
x ();
1766 GlDraw.viewport
(state.winw
- conf.scrollw
) 0 state.winw
state.h
;
1769 GlDraw.viewport
0 0 state.winw
state.h
;
1770 showoutline state.outline;
1772 Glut.swapBuffers
();
1776 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1777 let x = x - margin - state.x in
1778 let rec f = function
1780 begin match getopaque l.pageno with
1781 | Some
opaque when validopaque opaque ->
1782 let y = y - l.pagedispy
in
1785 let y = l.pagey
+ y in
1786 match whatsunder
opaque x y with
1799 let mouse ~button ~bstate ~
x ~
y =
1801 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1809 let incr = incr * 2 in
1810 let y = clamp incr in
1813 | Glut.LEFT_BUTTON
when state.outline = None
1814 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1815 if bstate
= Glut.DOWN
1817 state.mstate
<- Mpan
(x, y)
1819 state.mstate
<- Mnone
1821 | Glut.LEFT_BUTTON
when state.outline = None
->
1822 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1823 begin match dest with
1824 | Ulinkgoto
(pageno, top) ->
1827 gotopage1 pageno top
1832 | Unone
when bstate
= Glut.DOWN
->
1833 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1834 state.mstate
<- Mpan
(x, y);
1836 | Unone
| Utext _
->
1837 if bstate
= Glut.DOWN
1839 if state.rotate mod 360 = 0
1841 state.mstate
<- Msel
((x, y), (x, y));
1842 Glut.postRedisplay
()
1846 match state.mstate
with
1850 Glut.setCursor
Glut.CURSOR_INHERIT
;
1851 state.mstate
<- Mnone
1853 | Msel
((x0
, y0
), (x1
, y1
)) ->
1855 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1856 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1858 match getopaque l.pageno with
1859 | Some
opaque when validopaque opaque ->
1863 List.iter
f state.layout;
1864 copysel
""; (* ugly *)
1865 Glut.setCursor
Glut.CURSOR_INHERIT
;
1866 state.mstate
<- Mnone
;
1873 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1876 if state.outline = None
1878 match state.mstate
with
1884 state.mstate
<- Mpan
(x, y);
1885 if conf.zoom
> 1.0 then state.x <- state.x + dx;
1890 state.mstate
<- Msel
(a, (x, y));
1891 Glut.postRedisplay
()
1895 if state.outline = None
1897 match state.mstate
with
1899 begin match getunder x y with
1900 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
1902 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
1903 Glut.setCursor
Glut.CURSOR_INFO
1904 | Ulinkgoto
(page
, y) ->
1905 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
1906 Glut.setCursor
Glut.CURSOR_INFO
1908 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
1909 Glut.setCursor
Glut.CURSOR_TEXT
1912 | Mpan _
| Msel _
->
1919 if Sys.os_type
= "Win32"
1921 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1923 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1925 Filename.concat
home "llpp"
1929 let ic = open_in_bin
statepath in
1930 let hash = input_value
ic in
1936 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1943 match state.fullscreen
with
1944 | None
-> state.winw
, state.h
1947 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h
);
1948 let oc = open_out_bin
statepath in
1949 output_value
oc pstate
1953 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1958 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1961 state.bookmarks <- statebookmarks;
1962 with Not_found
-> ()
1964 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1968 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
1969 (fun s -> state.path
<- s)
1970 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
1973 if String.length
state.path
= 0
1974 then (prerr_endline
"filename missing"; exit
1)
1979 let _ = Glut.init
Sys.argv
in
1980 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1981 let () = Glut.initWindowSize
state.w state.h
in
1982 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1985 if Sys.os_type
= "Unix"
1987 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1989 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1990 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1991 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1992 Unix.bind
sock addr;
1994 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1995 Unix.connect
csock addr;
1996 let ssock, _ = Unix.accept
sock in
1999 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2000 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2004 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2008 let () = Glut.displayFunc
display in
2009 let () = Glut.reshapeFunc
reshape in
2010 let () = Glut.keyboardFunc
keyboard in
2011 let () = Glut.specialFunc
special in
2012 let () = Glut.idleFunc
(Some
idle) in
2013 let () = Glut.mouseFunc
mouse in
2014 let () = Glut.motionFunc
motion in
2015 let () = Glut.passiveMotionFunc
pmotion in
2018 state.csock <- csock;
2019 state.ssock <- ssock;
2020 state.text <- "Opening " ^
name;
2021 writecmd state.csock ("open " ^
state.path ^
"\000" ^
state.password ^
"\000");
2025 let rec handlelablglutbug () =
2028 with Glut.BadEnum
"key in special_of_int" ->
2029 showtext '
!'
" LablGlut bug: special key not recognized";
2030 handlelablglutbug ()
2032 handlelablglutbug ();