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
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 if conf.presentation
&& n = pageno
336 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
338 y
+ (pageno
- pn
) * (ph
+ pi
), h
340 let y = y + (if conf.presentation
then pi
else 0) in
341 let y = y + (n - pn
) * (ph
+ pi
) in
345 y + (pageno
- pn
) * (ph
+ pi
), ph
347 f 0 0 0 0 state.pages
350 let getpagey pageno
= fst
(getpageyh pageno
);;
353 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
354 let ((w
, h
, ips) as curr
), rest
, pdimno
, yinc
=
356 | (pageno'
, w
, h
) :: rest
when pageno'
= pageno
->
357 let ips = calcips h
in
358 (w
, h
, ips), rest
, pdimno
+ 1,
359 if conf.presentation
then ips else 0
361 prev
, pdims
, pdimno
, 0
363 let dy = dy + yinc
in
364 let py = py + yinc
in
365 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
370 if py + h
<= vy - yinc
372 let py = py + h
+ ips in
373 let dy = max
0 (py - y) in
383 let pagey = vy - py in
384 let pagevh = h
- pagey in
385 let pagevh = min
(sh
- dy) pagevh in
393 let py = py + h
+ ips in
399 ; pagedispy
= dy + off
400 ; pagey = pagey + off
401 ; pagevh = pagevh - off
404 let accu = e :: accu in
411 ~cacheleft
:(cacheleft
-1)
414 if state.invalidated
= 0
424 ~cacheleft
:(cblen state.pagecache
)
434 let y = state.y + incr
in
436 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
440 let getopaque pageno
=
441 try Some
(Hashtbl.find
state.pagemap
(pageno
+ 1, state.w
, state.rotate
))
442 with Not_found
-> None
445 let cache pageno opaque
=
446 Hashtbl.replace
state.pagemap
(pageno
+ 1, state.w
, state.rotate
) opaque
449 let validopaque opaque
= String.length opaque
> 0;;
452 match getopaque l
.pageno
with
453 | None
when not
state.rendering
->
454 state.rendering
<- true;
456 wcmd "render" [`i
(l
.pageno
+ 1)
464 let loadlayout layout =
465 let rec f all
= function
467 begin match getopaque l
.pageno
with
468 | None
-> render l
; f false ls
469 | Some opaque
-> f (all
&& validopaque opaque
) ls
473 f (layout <> []) layout;
480 let evictedopaque = cbpeekw state.pagecache
in
481 List.exists
(fun l
->
482 match getopaque l
.pageno
with
483 | Some opaque
when validopaque opaque
->
484 evictedopaque = opaque
488 if not
evictedvisible
490 let rely = yratio state.y in
491 let presentation = conf.presentation in
492 let interpagespace = conf.interpagespace in
493 let maxy = state.maxy in
494 conf.presentation <- false;
495 conf.interpagespace <- 0;
496 state.maxy <- calcheight ();
497 let y = truncate
(float state.maxy *. rely) in
498 let y = if y < state.h
then 0 else y - state.h
in
499 let pages = layout y (state.h
*3) in
500 List.iter
render pages;
501 conf.presentation <- presentation;
502 conf.interpagespace <- interpagespace;
508 let y = min
state.maxy y in
509 let pages = layout y state.h
in
510 let ready = loadlayout pages in
511 state.ty
<- yratio y;
516 state.layout <- pages;
518 Glut.postRedisplay
();
522 state.layout <- pages;
524 Glut.postRedisplay
();
530 cbput state.hists
.nav
(yratio state.y);
531 cbrfollowlen state.hists
.nav
;
535 let y = cbget state.hists
.nav ~
-1 in
536 truncate
(y *. float state.maxy)
540 let y, h
= getpageyh n in
542 gotoy (y + (truncate
(top
*. float h
)));
545 let gotopage1 n top
=
546 let y = getpagey n in
556 state.invalidated
<- state.invalidated
+ 1;
560 let c = c *. state.colorscale
in
566 match state.layout with
568 let rely = yratio state.y in
569 state.maxy <- calcheight ();
570 truncate
(float state.maxy *. rely)
573 state.maxy <- calcheight ();
580 let margin = truncate
(0.5 *. (float w
-. float w
*. conf.zoom
)) in
582 let w = w - margin * 2 - conf.scrollw
in
585 GlMat.mode `modelview
;
586 GlMat.load_identity
();
587 GlMat.mode `projection
;
588 GlMat.load_identity
();
589 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
590 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
591 GlMat.scale3
(2.0 /. float w, 2.0 /. float state.h
, 1.0);
592 GlClear.color
(scalecolor 1.0);
593 GlClear.clear
[`color
];
596 wcmd "geometry" [`i
state.w; `i h
];
600 GlDraw.viewport
0 0 state.winw
state.h
;
601 GlDraw.color
(0.0, 0.0, 0.0);
602 let sw = float (conf.scrollw
- 1) *. float state.w /. float state.winw
in
604 (0.0, float (state.h
- 18))
605 (float state.w -. sw, float state.h
)
607 let font = Glut.BITMAP_8_BY_13
in
608 GlDraw.color
(1.0, 1.0, 1.0);
609 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
610 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
611 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
615 let len = String.length
state.text
in
616 match state.textentry
with
618 if len > 0 then showtext ' '
state.text
620 | Some
(c, text
, _
, _
, _
) ->
624 text ^
" [" ^
state.text ^
"]"
634 state.text
<- Printf.sprintf
"%c%s" c s;
635 Glut.postRedisplay
();
649 state.rects
<- state.rects1
;
650 Glut.postRedisplay
()
653 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
654 state.pagecount
<- n;
655 state.invalidated
<- state.invalidated
- 1;
656 if state.invalidated
= 0
660 let s = Scanf.sscanf
cmd "t %n"
661 (fun n -> String.sub
cmd n (String.length
cmd - n))
663 Glut.setWindowTitle
s
666 let s = Scanf.sscanf
cmd "T %n"
667 (fun n -> String.sub
cmd n (String.length
cmd - n))
669 if state.textentry
= None
676 Glut.postRedisplay
();
682 let s = Scanf.sscanf
cmd "V %n"
683 (fun n -> String.sub
cmd n (String.length
cmd - n))
689 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
690 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
691 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
692 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
694 let y = (getpagey pageno) + truncate y0
in
697 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
700 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
701 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
702 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
703 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
706 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
710 Scanf.sscanf
cmd "r %d %d %d %d %s"
711 (fun n w h r p
-> (n, w, h
, r
, p
))
713 Hashtbl.replace
state.pagemap
(n, w, r
) p
;
714 let opaque = cbpeekw state.pagecache
in
715 if validopaque opaque
719 (fun k v a
-> if v
= opaque then k else a
)
720 state.pagemap
(-1, -1, -1)
722 wcmd "free" [`
s opaque];
723 Hashtbl.remove
state.pagemap
k
725 cbput state.pagecache p
;
726 state.rendering
<- false;
728 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy)))
730 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
733 else (ignore
(loadlayout state.layout); preload ())
737 let (n, w, h
) as pagelayout
=
738 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w, h
)
740 state.pages <- pagelayout
:: state.pages
743 let (l
, n, t
, h
, pos
) =
744 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
746 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
748 let l = String.length
s in
749 let b = Buffer.create
(String.length
s) in
756 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
759 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
768 let outline = (s, l, n, float t
/. float h
) in
770 match state.outlines with
771 | Olist
outlines -> Olist
(outline :: outlines)
772 | Oarray _
-> Olist
[outline]
773 | Onarrow _
-> Olist
[outline]
775 state.outlines <- outlines
778 log "unknown cmd `%S'" cmd
781 let now = Unix.gettimeofday
;;
785 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
790 let y = state.y + conf.scrollincr
in
791 let y = if y >= state.maxy then 0 else y in
797 let cmd = readcmd state.csock
in
804 let onhist cb
= function
805 | HCprev
-> cbget cb ~
-1
806 | HCnext
-> cbget cb
1
807 | HCfirst
-> cbget cb ~
-(cb
.rc)
808 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
811 let search pattern forward
=
812 if String.length pattern
> 0
815 match state.layout with
818 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
821 let b = makecmd "search"
822 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
824 Buffer.add_char
b '
,'
;
825 Buffer.add_string
b pattern
;
826 Buffer.add_char
b '
\000'
;
829 writecmd state.csock
cmd;
832 let intentry text key
=
833 let c = Char.unsafe_chr key
in
836 let s = "x" in s.[0] <- c;
837 let text = text ^
s in
841 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
846 let b = Buffer.create
(String.length
s + 1) in
847 Buffer.add_string
b s;
852 let textentry text key
=
853 let c = Char.unsafe_chr key
in
855 | _
when key
>= 32 && key
< 127 ->
856 let text = addchar text c in
860 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
865 state.rotate <- angle
;
867 wcmd "rotate" [`i angle
];
870 let optentry text key
=
871 let btos b = if b then "on" else "off" in
872 let c = Char.unsafe_chr key
in
876 try conf.scrollincr
<- int_of_string
s with exc
->
877 state.text <- Printf.sprintf
"bad integer `%s': %s"
878 s (Printexc.to_string exc
)
880 TEswitch
('#'
, "", None
, intentry, ondone)
885 Some
(int_of_string
s)
887 state.text <- Printf.sprintf
"bad integer `%s': %s"
888 s (Printexc.to_string exc
);
891 | Some angle
-> rotate angle
894 TEswitch
('^'
, "", None
, intentry, ondone)
897 conf.icase
<- not
conf.icase
;
898 TEdone
("case insensitive search " ^
(btos conf.icase
))
901 conf.preload <- not
conf.preload;
903 TEdone
("preload " ^
(btos conf.preload))
906 conf.verbose
<- not
conf.verbose
;
907 TEdone
("verbose " ^
(btos conf.verbose
))
910 conf.maxhfit
<- not
conf.maxhfit
;
911 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h
else state.h
);
912 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
915 conf.crophack
<- not
conf.crophack
;
916 TEdone
("crophack " ^
btos conf.crophack
)
919 conf.showall
<- not
conf.showall
;
920 TEdone
("showall " ^
btos conf.showall
)
923 conf.underinfo
<- not
conf.underinfo
;
924 TEdone
("underinfo " ^
btos conf.underinfo
)
929 conf.interpagespace <- int_of_string
s;
930 let rely = yratio state.y in
931 state.maxy <- calcheight ();
932 gotoy (truncate
(float state.maxy *. rely));
934 state.text <- Printf.sprintf
"bad integer `%s': %s"
935 s (Printexc.to_string exc
)
937 TEswitch
('
%'
, "", None
, intentry, ondone)
940 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
944 let maxoutlinerows () = (state.h
- 31) / 16;;
946 let enterselector allowdel
outlines errmsg
=
947 if Array.length
outlines = 0
952 Glut.setCursor
Glut.CURSOR_INHERIT
;
954 match state.layout with
956 | {pageno=pageno} :: rest
-> pageno
960 if n = Array.length
outlines
963 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
964 if outlinepageno
>= pageno then n else loop (n+1)
969 Some
(allowdel
, active,
970 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
971 Glut.postRedisplay
();
975 let enteroutlinemode () =
977 match state.outlines with
980 let a = Array.of_list
(List.rev
l) in
981 state.outlines <- Oarray
a;
983 | Onarrow
(a, b) -> a
985 enterselector false outlines "Document has no outline";
988 let enterbookmarkmode () =
989 let bookmarks = Array.of_list
state.bookmarks in
990 enterselector true bookmarks "Document has no bookmarks (yet)";
994 let quickbookmark ?title
() =
995 match state.layout with
1001 let sec = Unix.gettimeofday
() in
1002 let tm = Unix.localtime
sec in
1003 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
1007 (tm.Unix.tm_year
+ 1900)
1010 | Some
title -> title
1013 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1017 state.fullscreen
<- None
;
1018 Glut.reshapeWindow
w h
;
1021 let opendoc path password
=
1024 state.password
<- password
;
1025 Hashtbl.clear
state.pagemap
;
1027 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1028 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1029 wcmd "geometry" [`i
state.w; `i
state.h
];
1032 let viewkeyboard ~key ~x ~
y =
1034 state.textentry <- te
;
1037 Glut.postRedisplay
()
1039 match state.textentry with
1041 let c = Char.chr key
in
1047 let y = getnav () in
1056 Glut.postRedisplay
()
1059 let ondone isforw
s =
1060 cbput state.hists
.pat
s;
1061 cbrfollowlen state.hists
.pat
;
1062 state.searchpattern
<- s;
1065 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1066 textentry, ondone (c ='
/'
)))
1068 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1069 conf.zoom
<- min
10.0 (conf.zoom
+. 0.1);
1070 reshape state.winw
state.h
1075 try int_of_string
s with exc
->
1076 state.text <- Printf.sprintf
"bad integer `%s': %s"
1077 s (Printexc.to_string exc
);
1083 state.text <- "page bias is now " ^ string_of_int
n;
1086 enttext (Some
('
+'
, "", None
, intentry, ondone))
1088 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1089 conf.zoom
<- max
0.1 (conf.zoom
-. 0.1);
1090 if conf.zoom
<= 1.0 then state.x
<- 0;
1091 reshape state.winw
state.h
;
1097 enttext (Some
('
-'
, "", None
, optentry, ondone))
1099 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1102 reshape state.winw
state.h
1107 try int_of_string
s with exc
->
1108 state.text <- Printf.sprintf
"bad integer `%s': %s"
1109 s (Printexc.to_string exc
);
1115 cbput state.hists
.pag
(string_of_int
n);
1116 cbrfollowlen state.hists
.pag
;
1117 gotoy (getpagey (n + conf.pagebias
- 1))
1120 let pageentry text key
=
1121 match Char.unsafe_chr key
with
1122 | 'g'
-> TEdone
text
1123 | _
-> intentry text key
1125 let text = "x" in text.[0] <- c;
1126 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1130 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
1131 reshape state.winw
state.h
;
1134 conf.hlinks
<- not
conf.hlinks
;
1135 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1136 Glut.postRedisplay
()
1139 conf.autoscroll
<- not
conf.autoscroll
1142 conf.presentation <- not
conf.presentation;
1143 showtext ' '
("presentation mode " ^
1144 if conf.presentation then "on" else "off");
1148 begin match state.fullscreen
with
1150 state.fullscreen
<- Some
(state.w, state.h
);
1153 state.fullscreen
<- None
;
1161 search state.searchpattern
true
1164 search state.searchpattern
false
1167 begin match state.layout with
1170 gotoy (getpagey l.pageno)
1174 begin match List.rev
state.layout with
1177 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1178 gotoy (getpagey pageno)
1182 begin match state.layout with
1185 let pageno = max
0 (l.pageno-1) in
1186 gotoy (getpagey pageno)
1191 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1193 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1195 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1199 else (100. *. (float state.y /. float maxy)) in
1202 Printf.sprintf
"Page %d of %d %.2f%%"
1203 (fn+1) state.pagecount
percent
1206 "Pages %d-%d of %d %.2f%%"
1207 (fn+1) (ln
+1) state.pagecount
percent
1212 begin match state.layout with
1215 doreshape l.pagew
l.pageh
;
1216 Glut.postRedisplay
();
1220 enterbookmarkmode ()
1224 match state.layout with
1227 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1231 enttext (Some
('~'
, "", None
, textentry, ondone))
1235 showtext ' '
"Quick bookmark added";
1238 begin match state.layout with
1240 let a = getpagewh
l.pagedimno
in
1244 (truncate
(1.8 *. (a.(1) -. a.(0))),
1245 truncate
(1.2 *. (a.(3) -. a.(0))))
1247 (truncate
(a.(1) -. a.(0)),
1248 truncate
(a.(3) -. a.(0)))
1251 Glut.postRedisplay
();
1257 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1262 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1263 Glut.postRedisplay
()
1265 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1266 | 'j'
-> gotoy (clamp conf.scrollincr
)
1268 | '
r'
-> opendoc state.path
state.password
1271 vlog "huh? %d %c" key
(Char.chr key
);
1274 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1275 let len = String.length
text in
1278 state.textentry <- None
;
1279 Glut.postRedisplay
();
1282 let s = String.sub
text 0 (len - 1) in
1283 enttext (Some
(c, s, onhist, onkey
, ondone))
1286 | Some
(c, text, onhist, onkey
, ondone) ->
1287 begin match Char.unsafe_chr key
with
1290 state.textentry <- None
;
1291 Glut.postRedisplay
()
1294 state.textentry <- None
;
1295 Glut.postRedisplay
()
1298 begin match onkey
text key
with
1300 state.textentry <- None
;
1302 Glut.postRedisplay
()
1305 enttext (Some
(c, text, onhist, onkey
, ondone));
1308 state.textentry <- None
;
1309 Glut.postRedisplay
()
1312 state.textentry <- Some te
;
1313 Glut.postRedisplay
()
1318 let narrow outlines pattern
=
1319 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1323 let rec fold accu n =
1327 let (s, _
, _
, _
) as o
= outlines.(n) in
1329 if (try ignore
(Str.search_forward re
s 0); true
1330 with Not_found
-> false)
1336 let matched = fold [] (Array.length
outlines - 1) in
1337 if matched = [] then None
else Some
(Array.of_list
matched)
1340 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1341 let search active pattern incr
=
1344 if n = Array.length
outlines || n = -1
1347 let (s, _
, _
, _
) = outlines.(n) in
1349 (try ignore
(Str.search_forward re
s 0); true
1350 with Not_found
-> false)
1352 else loop (n + incr
)
1357 let re = Str.regexp_case_fold pattern
in
1363 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1366 if String.length qsearch
= 0
1369 state.outline <- None
;
1370 Glut.postRedisplay
();
1374 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1375 Glut.postRedisplay
();
1379 let incr = if key
= 18 then -1 else 1 in
1381 match search (active + incr) qsearch
incr with
1383 state.text <- qsearch ^
" [not found]";
1386 state.text <- qsearch
;
1387 active, firstof active
1389 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1390 Glut.postRedisplay
();
1393 let len = String.length qsearch
in
1400 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1403 let qsearch = String.sub
qsearch 0 (len - 1) in
1405 match search active qsearch ~
-1 with
1407 state.text <- qsearch ^
" [not found]";
1410 state.text <- qsearch;
1411 active, firstof active
1413 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1415 Glut.postRedisplay
()
1418 if active < Array.length
outlines
1420 let (_
, _
, n, t
) = outlines.(active) in
1424 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1425 state.outline <- None
;
1426 Glut.postRedisplay
();
1428 | _
when key
>= 32 && key
< 127 ->
1429 let pattern = addchar qsearch (Char.chr key
) in
1431 match search active pattern 1 with
1433 state.text <- pattern ^
" [not found]";
1436 state.text <- pattern;
1437 active, firstof active
1439 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1440 Glut.postRedisplay
()
1442 | 14 when not allowdel
->
1443 let optoutlines = narrow outlines qsearch in
1444 begin match optoutlines with
1445 | None
-> state.text <- "can't narrow"
1447 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1448 match state.outlines with
1450 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1451 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1453 Glut.postRedisplay
()
1455 | 21 when not allowdel
->
1457 match state.outlines with
1460 let a = Array.of_list
(List.rev
l) in
1461 state.outlines <- Oarray
a;
1464 state.outlines <- Oarray
b;
1467 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1468 Glut.postRedisplay
()
1472 Some
(allowdel
, active, firstof active, outlines, qsearch);
1473 Glut.postRedisplay
()
1475 | 127 when allowdel
->
1476 let len = Array.length
outlines - 1 in
1479 state.outline <- None
;
1480 state.bookmarks <- [];
1483 let bookmarks = Array.init
len
1485 let i = if i >= active then i + 1 else i in
1496 Glut.postRedisplay
()
1498 | _
-> log "unknown key %d" key
1501 let keyboard ~key ~x ~
y =
1506 match state.outline with
1507 | None
-> viewkeyboard ~key ~x ~
y
1508 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1511 let special ~key ~x ~
y =
1512 match state.outline with
1514 begin match state.textentry with
1518 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1519 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1520 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1521 | Glut.KEY_PAGE_UP
->
1522 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1524 match state.layout with
1526 | l :: _
-> state.y - l.pagey
1529 | Glut.KEY_PAGE_DOWN
->
1530 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1532 match List.rev
state.layout with
1534 | l :: _
-> getpagey l.pageno
1537 | Glut.KEY_HOME
-> addnav (); 0
1540 state.maxy - (if conf.maxhfit
then state.h
else 0)
1542 | Glut.KEY_RIGHT
when conf.zoom
> 1.0 ->
1543 state.x
<- state.x
- 10;
1545 | Glut.KEY_LEFT
when conf.zoom
> 1.0 ->
1546 state.x
<- state.x
+ 10;
1551 if not
conf.verbose
then state.text <- "";
1554 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1557 | Glut.KEY_UP
-> onhist HCprev
1558 | Glut.KEY_DOWN
-> onhist HCnext
1559 | Glut.KEY_HOME
-> onhist HCfirst
1560 | Glut.KEY_END
-> onhist HClast
1563 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1564 Glut.postRedisplay
()
1569 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1570 let maxrows = maxoutlinerows () in
1572 let active = active + incr in
1573 let active = max
0 (min
active (Array.length
outlines - 1)) in
1577 let rows = active - first in
1578 if rows > maxrows then active - maxrows else first
1581 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1582 Glut.postRedisplay
()
1585 | Glut.KEY_UP
-> navigate ~
-1
1586 | Glut.KEY_DOWN
-> navigate 1
1587 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1588 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1591 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1592 Glut.postRedisplay
()
1595 let active = Array.length
outlines - 1 in
1596 let first = max
0 (active - maxrows) in
1597 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1598 Glut.postRedisplay
()
1603 let drawplaceholder l =
1604 GlDraw.color
(scalecolor 1.0);
1606 (0.0, float l.pagedispy
)
1607 (float l.pagew
, float (l.pagedispy
+ l.pagevh))
1610 and y = float (l.pagedispy
+ 13) in
1611 let font = Glut.BITMAP_8_BY_13
in
1612 GlDraw.color
(0.0, 0.0, 0.0);
1613 GlPix.raster_pos ~
x ~
y ();
1614 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1615 ("Loading " ^ string_of_int
l.pageno);
1618 let now () = Unix.gettimeofday
();;
1621 begin match getopaque l.pageno with
1622 | Some
opaque when validopaque opaque ->
1623 if state.textentry = None
1624 then GlDraw.color
(scalecolor 1.0)
1625 else GlDraw.color
(scalecolor 0.4);
1627 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1631 vlog "draw %d %f sec" l.pageno d;
1636 l.pagedispy
+ l.pagevh;
1639 let scrollindicator () =
1640 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1641 GlDraw.color
(0.64 , 0.64, 0.64);
1644 (float conf.scrollw
, float state.h
)
1646 GlDraw.color
(0.0, 0.0, 0.0);
1647 let sh = (float (maxy + state.h
) /. float state.h
) in
1648 let sh = float state.h
/. sh in
1649 let sh = max
sh (float conf.scrollh
) in
1652 if state.y = state.maxy
1654 else float state.y /. float maxy
1656 let position = (float state.h
-. sh) *. percent in
1659 if position +. sh > float state.h
1667 (float conf.scrollw
, position +. sh)
1671 let showsel margin =
1672 match state.mstate
with
1676 | Msel
((x0
, y0
), (x1
, y1
)) ->
1677 let rec loop = function
1679 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1680 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1682 match getopaque l.pageno with
1683 | Some
opaque when validopaque opaque ->
1684 let oy = -l.pagey + l.pagedispy
in
1686 (x0
- margin - state.x, y0
,
1687 x1
- margin - state.x, y1
) oy;
1697 let panx = float state.x in
1699 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1700 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1702 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1704 if l.pageno = pageno
1706 let d = float (l.pagedispy
- l.pagey) in
1707 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1708 GlDraw.begins `quads
;
1710 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1711 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1712 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1713 GlDraw.vertex2
(x3
+.panx, y3
+.d);
1723 let showoutline = function
1725 | Some
(allowdel
, active, first, outlines, qsearch) ->
1727 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1728 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1729 GlDraw.rect
(0., 0.) (float state.w, float state.h
);
1732 GlDraw.color
(1., 1., 1.);
1733 let font = Glut.BITMAP_9_BY_15
in
1734 let draw_string x y s =
1735 GlPix.raster_pos ~
x ~
y ();
1736 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1739 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1742 let (s, l, _
, _
) = outlines.(row
) in
1743 let y = (row
- first) * 16 in
1748 GlDraw.polygon_mode `both `line
;
1749 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1750 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1751 let sw = float (conf.scrollw
- 1) *. float state.w /. float state.winw
in
1752 GlDraw.rect
(0., float (y + 1))
1753 ((float state.w -. sw), float (y + 18));
1754 GlDraw.polygon_mode `both `fill
;
1756 GlDraw.color
(1., 1., 1.);
1758 draw_string (float x) (float (y + 16)) s;
1766 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1767 GlDraw.viewport
margin 0 state.w state.h
;
1768 GlClear.color
(scalecolor 0.5);
1769 GlClear.clear
[`color
];
1772 let x = float state.x in
1773 GlMat.translate ~
x ();
1777 Gl.enable `scissor_test
;
1778 GlMisc.scissor
0 0 (state.winw
- conf.scrollw
) state.h
;
1780 let _lasty = List.fold_left
drawpage 0 (state.layout) in
1783 Gl.disable `scissor_test
1787 let x = -.float state.x in
1788 GlMat.translate ~
x ();
1791 GlDraw.viewport
(state.winw
- conf.scrollw
) 0 state.winw
state.h
;
1794 GlDraw.viewport
0 0 state.winw
state.h
;
1795 showoutline state.outline;
1797 Glut.swapBuffers
();
1801 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1802 let x = x - margin - state.x in
1803 let rec f = function
1805 begin match getopaque l.pageno with
1806 | Some
opaque when validopaque opaque ->
1807 let y = y - l.pagedispy
in
1810 let y = l.pagey + y in
1811 match whatsunder
opaque x y with
1824 let mouse ~button ~bstate ~
x ~
y =
1826 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1834 let incr = incr * 2 in
1835 let y = clamp incr in
1838 | Glut.LEFT_BUTTON
when state.outline = None
1839 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1840 if bstate
= Glut.DOWN
1842 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1843 state.mstate
<- Mpan
(x, y)
1846 state.mstate
<- Mnone
1848 | Glut.LEFT_BUTTON
when state.outline = None
->
1849 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1850 begin match dest with
1851 | Ulinkgoto
(pageno, top
) ->
1854 gotopage1 pageno top
1859 | Unone
when bstate
= Glut.DOWN
->
1860 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1861 state.mstate
<- Mpan
(x, y);
1863 | Unone
| Utext _
->
1864 if bstate
= Glut.DOWN
1866 if state.rotate mod 360 = 0
1868 state.mstate
<- Msel
((x, y), (x, y));
1869 Glut.postRedisplay
()
1873 match state.mstate
with
1877 Glut.setCursor
Glut.CURSOR_INHERIT
;
1878 state.mstate
<- Mnone
1880 | Msel
((x0
, y0
), (x1
, y1
)) ->
1882 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1883 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1885 match getopaque l.pageno with
1886 | Some
opaque when validopaque opaque ->
1890 List.iter
f state.layout;
1891 copysel
""; (* ugly *)
1892 Glut.setCursor
Glut.CURSOR_INHERIT
;
1893 state.mstate
<- Mnone
;
1900 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1903 if state.outline = None
1905 match state.mstate
with
1911 state.mstate
<- Mpan
(x, y);
1912 if conf.zoom
> 1.0 then state.x <- state.x + dx;
1917 state.mstate
<- Msel
(a, (x, y));
1918 Glut.postRedisplay
()
1922 if state.outline = None
1924 match state.mstate
with
1926 begin match getunder x y with
1927 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
1929 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
1930 Glut.setCursor
Glut.CURSOR_INFO
1931 | Ulinkgoto
(page
, y) ->
1932 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
1933 Glut.setCursor
Glut.CURSOR_INFO
1935 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
1936 Glut.setCursor
Glut.CURSOR_TEXT
1939 | Mpan _
| Msel _
->
1946 if Sys.os_type
= "Win32"
1948 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1950 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1952 Filename.concat
home "llpp"
1956 let ic = open_in_bin
statepath in
1957 let hash = input_value
ic in
1963 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1970 match state.fullscreen
with
1971 | None
-> state.winw
, state.h
1974 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h
);
1975 let oc = open_out_bin
statepath in
1976 output_value
oc pstate
1980 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1985 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1988 state.bookmarks <- statebookmarks;
1989 with Not_found
-> ()
1991 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1995 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
1996 (fun s -> state.path
<- s)
1997 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2000 if String.length
state.path
= 0
2001 then (prerr_endline
"filename missing"; exit
1)
2006 let _ = Glut.init
Sys.argv
in
2007 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2008 let () = Glut.initWindowSize
state.w state.h
in
2009 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
2012 if Sys.os_type
= "Unix"
2014 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2016 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2017 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2018 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2019 Unix.bind
sock addr;
2021 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2022 Unix.connect
csock addr;
2023 let ssock, _ = Unix.accept
sock in
2026 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2027 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2031 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2035 let () = Glut.displayFunc
display in
2036 let () = Glut.reshapeFunc
reshape in
2037 let () = Glut.keyboardFunc
keyboard in
2038 let () = Glut.specialFunc
special in
2039 let () = Glut.idleFunc
(Some
idle) in
2040 let () = Glut.mouseFunc
mouse in
2041 let () = Glut.motionFunc
motion in
2042 let () = Glut.passiveMotionFunc
pmotion in
2045 state.csock <- csock;
2046 state.ssock <- ssock;
2047 state.text <- "Opening " ^
name;
2048 writecmd state.csock ("open " ^
state.path ^
"\000" ^
state.password ^
"\000");
2052 let rec handlelablglutbug () =
2055 with Glut.BadEnum
"key in special_of_int" ->
2056 showtext '
!'
" LablGlut bug: special key not recognized";
2057 handlelablglutbug ()
2059 handlelablglutbug ();