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
&& pageno
= 0 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 rely = yratio state.y in
479 let presentation = conf.presentation in
480 let interpagespace = conf.interpagespace in
481 let maxy = state.maxy in
482 conf.presentation <- false;
483 conf.interpagespace <- 0;
484 state.maxy <- calcheight ();
485 let y = truncate
(float state.maxy *. rely) in
486 let y = if y < state.h
then 0 else y - state.h
in
487 let pages = layout y (state.h
*3) in
488 List.iter
render pages;
489 conf.presentation <- presentation;
490 conf.interpagespace <- interpagespace;
496 let y = min
state.maxy y in
497 let pages = layout y state.h
in
498 let ready = loadlayout pages in
499 state.ty
<- yratio y;
504 state.layout <- pages;
506 Glut.postRedisplay
();
510 state.layout <- pages;
512 Glut.postRedisplay
();
518 cbput state.hists
.nav
(yratio state.y);
519 cbrfollowlen state.hists
.nav
;
523 let y = cbget state.hists
.nav ~
-1 in
524 truncate
(y *. float state.maxy)
528 let y, h
= getpageyh n in
530 gotoy (y + (truncate
(top *. float h
)));
533 let gotopage1 n top =
534 let y = getpagey n in
544 state.invalidated
<- state.invalidated
+ 1;
548 let c = c *. state.colorscale
in
554 match state.layout with
556 let rely = yratio state.y in
557 state.maxy <- calcheight ();
558 truncate
(float state.maxy *. rely)
561 state.maxy <- calcheight ();
568 let margin = truncate
(0.5 *. (float w
-. float w
*. conf.zoom
)) in
570 let w = w - margin * 2 - conf.scrollw
in
573 GlMat.mode `modelview
;
574 GlMat.load_identity
();
575 GlMat.mode `projection
;
576 GlMat.load_identity
();
577 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
578 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
579 GlMat.scale3
(2.0 /. float w, 2.0 /. float state.h
, 1.0);
580 GlClear.color
(scalecolor 1.0);
581 GlClear.clear
[`color
];
584 wcmd "geometry" [`i
state.w; `i h
];
588 GlDraw.viewport
0 0 state.winw
state.h
;
589 GlDraw.color
(0.0, 0.0, 0.0);
590 let sw = float (conf.scrollw
- 1) *. float state.w /. float state.winw
in
592 (0.0, float (state.h
- 18))
593 (float state.w -. sw, float state.h
)
595 let font = Glut.BITMAP_8_BY_13
in
596 GlDraw.color
(1.0, 1.0, 1.0);
597 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
598 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
599 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
603 let len = String.length
state.text
in
604 match state.textentry
with
606 if len > 0 then showtext ' '
state.text
608 | Some
(c, text
, _
, _
, _
) ->
612 text ^
" [" ^
state.text ^
"]"
622 state.text
<- Printf.sprintf
"%c%s" c s;
623 Glut.postRedisplay
();
637 state.rects
<- state.rects1
;
638 Glut.postRedisplay
()
641 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
642 state.pagecount
<- n;
643 state.invalidated
<- state.invalidated
- 1;
644 if state.invalidated
= 0
648 let s = Scanf.sscanf
cmd "t %n"
649 (fun n -> String.sub
cmd n (String.length
cmd - n))
651 Glut.setWindowTitle
s
654 let s = Scanf.sscanf
cmd "T %n"
655 (fun n -> String.sub
cmd n (String.length
cmd - n))
657 if state.textentry
= None
664 Glut.postRedisplay
();
670 let s = Scanf.sscanf
cmd "V %n"
671 (fun n -> String.sub
cmd n (String.length
cmd - n))
677 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
678 Scanf.sscanf
cmd "F %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
))
682 let y = (getpagey pageno) + truncate y0
in
685 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
688 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
689 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
690 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
691 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
694 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
698 Scanf.sscanf
cmd "r %d %d %d %d %s"
699 (fun n w h r p
-> (n, w, h
, r
, p
))
701 Hashtbl.replace
state.pagemap
(n, w, r
) p
;
702 let opaque = cbpeekw state.pagecache
in
703 if validopaque opaque
707 (fun k v a
-> if v
= opaque then k else a
)
708 state.pagemap
(-1, -1, -1)
710 wcmd "free" [`
s opaque];
711 Hashtbl.remove
state.pagemap
k
713 cbput state.pagecache p
;
714 state.rendering
<- false;
716 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy)))
718 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
721 else (ignore
(loadlayout state.layout); preload ())
725 let (n, w, h
) as pagelayout
=
726 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w, h
)
728 state.pages <- pagelayout
:: state.pages
731 let (l
, n, t
, h
, pos
) =
732 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
734 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
736 let l = String.length
s in
737 let b = Buffer.create
(String.length
s) in
744 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
747 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
756 let outline = (s, l, n, float t
/. float h
) in
758 match state.outlines with
759 | Olist
outlines -> Olist
(outline :: outlines)
760 | Oarray _
-> Olist
[outline]
761 | Onarrow _
-> Olist
[outline]
763 state.outlines <- outlines
766 log "unknown cmd `%S'" cmd
769 let now = Unix.gettimeofday
;;
773 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
778 let y = state.y + conf.scrollincr
in
779 let y = if y >= state.maxy then 0 else y in
785 let cmd = readcmd state.csock
in
792 let onhist cb
= function
793 | HCprev
-> cbget cb ~
-1
794 | HCnext
-> cbget cb
1
795 | HCfirst
-> cbget cb ~
-(cb
.rc)
796 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
799 let search pattern forward
=
800 if String.length pattern
> 0
803 match state.layout with
806 l.pageno, (l.pagey
+ if forward
then 0 else 0*l.pagevh
)
809 let b = makecmd "search"
810 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
812 Buffer.add_char
b '
,'
;
813 Buffer.add_string
b pattern
;
814 Buffer.add_char
b '
\000'
;
817 writecmd state.csock
cmd;
820 let intentry text key
=
821 let c = Char.unsafe_chr key
in
824 let s = "x" in s.[0] <- c;
825 let text = text ^
s in
829 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
834 let b = Buffer.create
(String.length
s + 1) in
835 Buffer.add_string
b s;
840 let textentry text key
=
841 let c = Char.unsafe_chr key
in
843 | _
when key
>= 32 && key
< 127 ->
844 let text = addchar text c in
848 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
853 state.rotate <- angle
;
855 wcmd "rotate" [`i angle
];
858 let optentry text key
=
859 let btos b = if b then "on" else "off" in
860 let c = Char.unsafe_chr key
in
864 try conf.scrollincr
<- int_of_string
s with exc
->
865 state.text <- Printf.sprintf
"bad integer `%s': %s"
866 s (Printexc.to_string exc
)
868 TEswitch
('#'
, "", None
, intentry, ondone)
873 Some
(int_of_string
s)
875 state.text <- Printf.sprintf
"bad integer `%s': %s"
876 s (Printexc.to_string exc
);
879 | Some angle
-> rotate angle
882 TEswitch
('^'
, "", None
, intentry, ondone)
885 conf.icase
<- not
conf.icase
;
886 TEdone
("case insensitive search " ^
(btos conf.icase
))
889 conf.preload <- not
conf.preload;
891 TEdone
("preload " ^
(btos conf.preload))
894 conf.verbose
<- not
conf.verbose
;
895 TEdone
("verbose " ^
(btos conf.verbose
))
898 conf.maxhfit
<- not
conf.maxhfit
;
899 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h
else state.h
);
900 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
903 conf.crophack
<- not
conf.crophack
;
904 TEdone
("crophack " ^
btos conf.crophack
)
907 conf.showall
<- not
conf.showall
;
908 TEdone
("showall " ^
btos conf.showall
)
911 conf.underinfo
<- not
conf.underinfo
;
912 TEdone
("underinfo " ^
btos conf.underinfo
)
917 conf.interpagespace <- int_of_string
s;
918 let rely = yratio state.y in
919 state.maxy <- calcheight ();
920 gotoy (truncate
(float state.maxy *. rely));
922 state.text <- Printf.sprintf
"bad integer `%s': %s"
923 s (Printexc.to_string exc
)
925 TEswitch
('
%'
, "", None
, intentry, ondone)
928 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
932 let maxoutlinerows () = (state.h
- 31) / 16;;
934 let enterselector allowdel
outlines errmsg
=
935 if Array.length
outlines = 0
940 Glut.setCursor
Glut.CURSOR_INHERIT
;
942 match state.layout with
944 | {pageno=pageno} :: rest
-> pageno
948 if n = Array.length
outlines
951 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
952 if outlinepageno
>= pageno then n else loop (n+1)
957 Some
(allowdel
, active,
958 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
959 Glut.postRedisplay
();
963 let enteroutlinemode () =
965 match state.outlines with
968 let a = Array.of_list
(List.rev
l) in
969 state.outlines <- Oarray
a;
971 | Onarrow
(a, b) -> a
973 enterselector false outlines "Document has no outline";
976 let enterbookmarkmode () =
977 let bookmarks = Array.of_list
state.bookmarks in
978 enterselector true bookmarks "Document has no bookmarks (yet)";
982 let quickbookmark ?title
() =
983 match state.layout with
989 let sec = Unix.gettimeofday
() in
990 let tm = Unix.localtime
sec in
991 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
995 (tm.Unix.tm_year
+ 1900)
998 | Some
title -> title
1001 (title, 0, l.pageno, float l.pagey
/. float l.pageh
) :: state.bookmarks
1005 state.fullscreen
<- None
;
1006 Glut.reshapeWindow
w h
;
1009 let opendoc path password
=
1012 state.password
<- password
;
1013 Hashtbl.clear
state.pagemap
;
1015 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1016 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1017 wcmd "geometry" [`i
state.w; `i
state.h
];
1020 let viewkeyboard ~key ~x ~
y =
1022 state.textentry <- te
;
1025 Glut.postRedisplay
()
1027 match state.textentry with
1029 let c = Char.chr key
in
1035 let y = getnav () in
1044 Glut.postRedisplay
()
1047 let ondone isforw
s =
1048 cbput state.hists
.pat
s;
1049 cbrfollowlen state.hists
.pat
;
1050 state.searchpattern
<- s;
1053 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1054 textentry, ondone (c ='
/'
)))
1056 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1057 conf.zoom
<- min
10.0 (conf.zoom
+. 0.1);
1058 reshape state.winw
state.h
1063 try int_of_string
s with exc
->
1064 state.text <- Printf.sprintf
"bad integer `%s': %s"
1065 s (Printexc.to_string exc
);
1071 state.text <- "page bias is now " ^ string_of_int
n;
1074 enttext (Some
('
+'
, "", None
, intentry, ondone))
1076 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1077 conf.zoom
<- max
0.1 (conf.zoom
-. 0.1);
1078 if conf.zoom
<= 1.0 then state.x
<- 0;
1079 reshape state.winw
state.h
;
1085 enttext (Some
('
-'
, "", None
, optentry, ondone))
1087 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1090 reshape state.winw
state.h
1095 try int_of_string
s with exc
->
1096 state.text <- Printf.sprintf
"bad integer `%s': %s"
1097 s (Printexc.to_string exc
);
1103 cbput state.hists
.pag
(string_of_int
n);
1104 cbrfollowlen state.hists
.pag
;
1105 gotoy (getpagey (n + conf.pagebias
- 1))
1108 let pageentry text key
=
1109 match Char.unsafe_chr key
with
1110 | 'g'
-> TEdone
text
1111 | _
-> intentry text key
1113 let text = "x" in text.[0] <- c;
1114 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1118 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
1119 reshape state.winw
state.h
;
1122 conf.hlinks
<- not
conf.hlinks
;
1123 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1124 Glut.postRedisplay
()
1127 conf.autoscroll
<- not
conf.autoscroll
1130 conf.presentation <- not
conf.presentation;
1131 showtext ' '
("Presnetation mode " ^
1132 if conf.presentation then "on" else "off");
1136 begin match state.fullscreen
with
1138 state.fullscreen
<- Some
(state.w, state.h
);
1141 state.fullscreen
<- None
;
1149 search state.searchpattern
true
1152 search state.searchpattern
false
1155 begin match state.layout with
1158 gotoy (getpagey l.pageno)
1162 begin match List.rev
state.layout with
1165 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1166 gotoy (getpagey pageno)
1170 begin match state.layout with
1173 let pageno = max
0 (l.pageno-1) in
1174 gotoy (getpagey pageno)
1179 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1181 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1183 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1187 else (100. *. (float state.y /. float maxy)) in
1190 Printf.sprintf
"Page %d of %d %.2f%%"
1191 (fn+1) state.pagecount
percent
1194 "Pages %d-%d of %d %.2f%%"
1195 (fn+1) (ln
+1) state.pagecount
percent
1200 begin match state.layout with
1203 doreshape l.pagew
l.pageh
;
1204 Glut.postRedisplay
();
1208 enterbookmarkmode ()
1212 match state.layout with
1215 (s, 0, l.pageno, float l.pagey
/. float l.pageh
)
1219 enttext (Some
('~'
, "", None
, textentry, ondone))
1223 showtext ' '
"Quick bookmark added";
1226 begin match state.layout with
1228 let a = getpagewh
l.pagedimno
in
1232 (truncate
(1.8 *. (a.(1) -. a.(0))),
1233 truncate
(1.2 *. (a.(3) -. a.(0))))
1235 (truncate
(a.(1) -. a.(0)),
1236 truncate
(a.(3) -. a.(0)))
1239 Glut.postRedisplay
();
1245 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1250 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1251 Glut.postRedisplay
()
1253 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1254 | 'j'
-> gotoy (clamp conf.scrollincr
)
1256 | '
r'
-> opendoc state.path
state.password
1259 vlog "huh? %d %c" key
(Char.chr key
);
1262 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1263 let len = String.length
text in
1266 state.textentry <- None
;
1267 Glut.postRedisplay
();
1270 let s = String.sub
text 0 (len - 1) in
1271 enttext (Some
(c, s, onhist, onkey
, ondone))
1274 | Some
(c, text, onhist, onkey
, ondone) ->
1275 begin match Char.unsafe_chr key
with
1278 state.textentry <- None
;
1279 Glut.postRedisplay
()
1282 state.textentry <- None
;
1283 Glut.postRedisplay
()
1286 begin match onkey
text key
with
1288 state.textentry <- None
;
1290 Glut.postRedisplay
()
1293 enttext (Some
(c, text, onhist, onkey
, ondone));
1296 state.textentry <- None
;
1297 Glut.postRedisplay
()
1300 state.textentry <- Some te
;
1301 Glut.postRedisplay
()
1306 let narrow outlines pattern
=
1307 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1311 let rec fold accu n =
1315 let (s, _
, _
, _
) as o
= outlines.(n) in
1317 if (try ignore
(Str.search_forward re
s 0); true
1318 with Not_found
-> false)
1324 let matched = fold [] (Array.length
outlines - 1) in
1325 if matched = [] then None
else Some
(Array.of_list
matched)
1328 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1329 let search active pattern incr
=
1332 if n = Array.length
outlines || n = -1
1335 let (s, _
, _
, _
) = outlines.(n) in
1337 (try ignore
(Str.search_forward re
s 0); true
1338 with Not_found
-> false)
1340 else loop (n + incr
)
1345 let re = Str.regexp_case_fold pattern
in
1351 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1354 if String.length qsearch
= 0
1357 state.outline <- None
;
1358 Glut.postRedisplay
();
1362 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1363 Glut.postRedisplay
();
1367 let incr = if key
= 18 then -1 else 1 in
1369 match search (active + incr) qsearch
incr with
1371 state.text <- qsearch ^
" [not found]";
1374 state.text <- qsearch
;
1375 active, firstof active
1377 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1378 Glut.postRedisplay
();
1381 let len = String.length qsearch
in
1388 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1391 let qsearch = String.sub
qsearch 0 (len - 1) in
1393 match search active qsearch ~
-1 with
1395 state.text <- qsearch ^
" [not found]";
1398 state.text <- qsearch;
1399 active, firstof active
1401 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1403 Glut.postRedisplay
()
1406 if active < Array.length
outlines
1408 let (_
, _
, n, t
) = outlines.(active) in
1412 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1413 state.outline <- None
;
1414 Glut.postRedisplay
();
1416 | _
when key
>= 32 && key
< 127 ->
1417 let pattern = addchar qsearch (Char.chr key
) in
1419 match search active pattern 1 with
1421 state.text <- pattern ^
" [not found]";
1424 state.text <- pattern;
1425 active, firstof active
1427 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1428 Glut.postRedisplay
()
1430 | 14 when not allowdel
->
1431 let optoutlines = narrow outlines qsearch in
1432 begin match optoutlines with
1433 | None
-> state.text <- "can't narrow"
1435 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1436 match state.outlines with
1438 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1439 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1441 Glut.postRedisplay
()
1443 | 21 when not allowdel
->
1445 match state.outlines with
1448 let a = Array.of_list
(List.rev
l) in
1449 state.outlines <- Oarray
a;
1452 state.outlines <- Oarray
b;
1455 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1456 Glut.postRedisplay
()
1460 Some
(allowdel
, active, firstof active, outlines, qsearch);
1461 Glut.postRedisplay
()
1463 | 127 when allowdel
->
1464 let len = Array.length
outlines - 1 in
1467 state.outline <- None
;
1468 state.bookmarks <- [];
1471 let bookmarks = Array.init
len
1473 let i = if i >= active then i + 1 else i in
1484 Glut.postRedisplay
()
1486 | _
-> log "unknown key %d" key
1489 let keyboard ~key ~x ~
y =
1494 match state.outline with
1495 | None
-> viewkeyboard ~key ~x ~
y
1496 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1499 let special ~key ~x ~
y =
1500 match state.outline with
1502 begin match state.textentry with
1506 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1507 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1508 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1509 | Glut.KEY_PAGE_UP
->
1510 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1512 match state.layout with
1514 | l :: _
-> state.y - l.pagey
1517 | Glut.KEY_PAGE_DOWN
->
1518 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1520 match List.rev
state.layout with
1522 | l :: _
-> getpagey l.pageno
1525 | Glut.KEY_HOME
-> addnav (); 0
1528 state.maxy - (if conf.maxhfit
then state.h
else 0)
1530 | Glut.KEY_RIGHT
when conf.zoom
> 1.0 ->
1531 state.x
<- state.x
- 10;
1533 | Glut.KEY_LEFT
when conf.zoom
> 1.0 ->
1534 state.x
<- state.x
+ 10;
1539 if not
conf.verbose
then state.text <- "";
1542 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1545 | Glut.KEY_UP
-> onhist HCprev
1546 | Glut.KEY_DOWN
-> onhist HCnext
1547 | Glut.KEY_HOME
-> onhist HCfirst
1548 | Glut.KEY_END
-> onhist HClast
1551 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1552 Glut.postRedisplay
()
1557 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1558 let maxrows = maxoutlinerows () in
1560 let active = active + incr in
1561 let active = max
0 (min
active (Array.length
outlines - 1)) in
1565 let rows = active - first in
1566 if rows > maxrows then active - maxrows else first
1569 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1570 Glut.postRedisplay
()
1573 | Glut.KEY_UP
-> navigate ~
-1
1574 | Glut.KEY_DOWN
-> navigate 1
1575 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1576 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1579 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1580 Glut.postRedisplay
()
1583 let active = Array.length
outlines - 1 in
1584 let first = max
0 (active - maxrows) in
1585 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1586 Glut.postRedisplay
()
1591 let drawplaceholder l =
1592 GlDraw.color
(scalecolor 1.0);
1594 (0.0, float l.pagedispy
)
1595 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1598 and y = float (l.pagedispy
+ 13) in
1599 let font = Glut.BITMAP_8_BY_13
in
1600 GlDraw.color
(0.0, 0.0, 0.0);
1601 GlPix.raster_pos ~
x ~
y ();
1602 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1603 ("Loading " ^ string_of_int
l.pageno);
1606 let now () = Unix.gettimeofday
();;
1609 begin match getopaque l.pageno with
1610 | Some
opaque when validopaque opaque ->
1611 if state.textentry = None
1612 then GlDraw.color
(scalecolor 1.0)
1613 else GlDraw.color
(scalecolor 0.4);
1615 draw
(l.pagedispy
, l.pagew
, l.pagevh
, l.pagey
, conf.hlinks
)
1619 vlog "draw %d %f sec" l.pageno d;
1624 l.pagedispy
+ l.pagevh
;
1627 let scrollindicator () =
1628 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1629 GlDraw.color
(0.64 , 0.64, 0.64);
1632 (float conf.scrollw
, float state.h
)
1634 GlDraw.color
(0.0, 0.0, 0.0);
1635 let sh = (float (maxy + state.h
) /. float state.h
) in
1636 let sh = float state.h
/. sh in
1637 let sh = max
sh (float conf.scrollh
) in
1640 if state.y = state.maxy
1642 else float state.y /. float maxy
1644 let position = (float state.h
-. sh) *. percent in
1647 if position +. sh > float state.h
1655 (float conf.scrollw
, position +. sh)
1659 let showsel margin =
1660 match state.mstate
with
1664 | Msel
((x0
, y0
), (x1
, y1
)) ->
1665 let rec loop = function
1667 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1668 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1670 match getopaque l.pageno with
1671 | Some
opaque when validopaque opaque ->
1672 let oy = -l.pagey
+ l.pagedispy
in
1674 (x0
- margin - state.x, y0
,
1675 x1
- margin - state.x, y1
) oy;
1685 let panx = float state.x in
1687 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1688 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1690 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1692 if l.pageno = pageno
1694 let d = float (l.pagedispy
- l.pagey
) in
1695 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1696 GlDraw.begins `quads
;
1698 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1699 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1700 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1701 GlDraw.vertex2
(x3
+.panx, y3
+.d);
1711 let showoutline = function
1713 | Some
(allowdel
, active, first, outlines, qsearch) ->
1715 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1716 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1717 GlDraw.rect
(0., 0.) (float state.w, float state.h
);
1720 GlDraw.color
(1., 1., 1.);
1721 let font = Glut.BITMAP_9_BY_15
in
1722 let draw_string x y s =
1723 GlPix.raster_pos ~
x ~
y ();
1724 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1727 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1730 let (s, l, _
, _
) = outlines.(row
) in
1731 let y = (row
- first) * 16 in
1736 GlDraw.polygon_mode `both `line
;
1737 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1738 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1739 let sw = float (conf.scrollw
- 1) *. float state.w /. float state.winw
in
1740 GlDraw.rect
(0., float (y + 1))
1741 ((float state.w -. sw), float (y + 18));
1742 GlDraw.polygon_mode `both `fill
;
1744 GlDraw.color
(1., 1., 1.);
1746 draw_string (float x) (float (y + 16)) s;
1754 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1755 GlDraw.viewport
margin 0 state.w state.h
;
1756 GlClear.color
(scalecolor 0.5);
1757 GlClear.clear
[`color
];
1760 let x = float state.x in
1761 GlMat.translate ~
x ();
1765 Gl.enable `scissor_test
;
1766 GlMisc.scissor
0 0 (state.winw
- conf.scrollw
) state.h
;
1768 let _lasty = List.fold_left
drawpage 0 (state.layout) in
1771 Gl.disable `scissor_test
1775 let x = -.float state.x in
1776 GlMat.translate ~
x ();
1779 GlDraw.viewport
(state.winw
- conf.scrollw
) 0 state.winw
state.h
;
1782 GlDraw.viewport
0 0 state.winw
state.h
;
1783 showoutline state.outline;
1785 Glut.swapBuffers
();
1789 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1790 let x = x - margin - state.x in
1791 let rec f = function
1793 begin match getopaque l.pageno with
1794 | Some
opaque when validopaque opaque ->
1795 let y = y - l.pagedispy
in
1798 let y = l.pagey
+ y in
1799 match whatsunder
opaque x y with
1812 let mouse ~button ~bstate ~
x ~
y =
1814 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1822 let incr = incr * 2 in
1823 let y = clamp incr in
1826 | Glut.LEFT_BUTTON
when state.outline = None
1827 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1828 if bstate
= Glut.DOWN
1830 state.mstate
<- Mpan
(x, y)
1832 state.mstate
<- Mnone
1834 | Glut.LEFT_BUTTON
when state.outline = None
->
1835 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1836 begin match dest with
1837 | Ulinkgoto
(pageno, top) ->
1840 gotopage1 pageno top
1845 | Unone
when bstate
= Glut.DOWN
->
1846 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1847 state.mstate
<- Mpan
(x, y);
1849 | Unone
| Utext _
->
1850 if bstate
= Glut.DOWN
1852 if state.rotate mod 360 = 0
1854 state.mstate
<- Msel
((x, y), (x, y));
1855 Glut.postRedisplay
()
1859 match state.mstate
with
1863 Glut.setCursor
Glut.CURSOR_INHERIT
;
1864 state.mstate
<- Mnone
1866 | Msel
((x0
, y0
), (x1
, y1
)) ->
1868 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1869 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1871 match getopaque l.pageno with
1872 | Some
opaque when validopaque opaque ->
1876 List.iter
f state.layout;
1877 copysel
""; (* ugly *)
1878 Glut.setCursor
Glut.CURSOR_INHERIT
;
1879 state.mstate
<- Mnone
;
1886 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1889 if state.outline = None
1891 match state.mstate
with
1897 state.mstate
<- Mpan
(x, y);
1898 if conf.zoom
> 1.0 then state.x <- state.x + dx;
1903 state.mstate
<- Msel
(a, (x, y));
1904 Glut.postRedisplay
()
1908 if state.outline = None
1910 match state.mstate
with
1912 begin match getunder x y with
1913 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
1915 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
1916 Glut.setCursor
Glut.CURSOR_INFO
1917 | Ulinkgoto
(page
, y) ->
1918 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
1919 Glut.setCursor
Glut.CURSOR_INFO
1921 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
1922 Glut.setCursor
Glut.CURSOR_TEXT
1925 | Mpan _
| Msel _
->
1932 if Sys.os_type
= "Win32"
1934 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1936 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1938 Filename.concat
home "llpp"
1942 let ic = open_in_bin
statepath in
1943 let hash = input_value
ic in
1949 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1956 match state.fullscreen
with
1957 | None
-> state.winw
, state.h
1960 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h
);
1961 let oc = open_out_bin
statepath in
1962 output_value
oc pstate
1966 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1971 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1974 state.bookmarks <- statebookmarks;
1975 with Not_found
-> ()
1977 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1981 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
1982 (fun s -> state.path
<- s)
1983 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
1986 if String.length
state.path
= 0
1987 then (prerr_endline
"filename missing"; exit
1)
1992 let _ = Glut.init
Sys.argv
in
1993 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1994 let () = Glut.initWindowSize
state.w state.h
in
1995 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1998 if Sys.os_type
= "Unix"
2000 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2002 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2003 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2004 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2005 Unix.bind
sock addr;
2007 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2008 Unix.connect
csock addr;
2009 let ssock, _ = Unix.accept
sock in
2012 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2013 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2017 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2021 let () = Glut.displayFunc
display in
2022 let () = Glut.reshapeFunc
reshape in
2023 let () = Glut.keyboardFunc
keyboard in
2024 let () = Glut.specialFunc
special in
2025 let () = Glut.idleFunc
(Some
idle) in
2026 let () = Glut.mouseFunc
mouse in
2027 let () = Glut.motionFunc
motion in
2028 let () = Glut.passiveMotionFunc
pmotion in
2031 state.csock <- csock;
2032 state.ssock <- ssock;
2033 state.text <- "Opening " ^
name;
2034 writecmd state.csock ("open " ^
state.path ^
"\000" ^
state.password ^
"\000");
2038 let rec handlelablglutbug () =
2041 with Glut.BadEnum
"key in special_of_int" ->
2042 showtext '
!'
" LablGlut bug: special key not recognized";
2043 handlelablglutbug ()
2045 handlelablglutbug ();