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
309 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
318 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
321 let fh = f 0 0 0 0 state.pages
in
325 let getpageyh pageno
=
326 let rec f pn ph pi y l
=
328 | (n, _
, h
) :: rest
->
329 let ips = calcips h
in
332 if conf.presentation
&& n = pageno
334 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
336 y
+ (pageno
- pn
) * (ph
+ pi
), h
338 let y = y + (if conf.presentation
then pi
else 0) in
339 let y = y + (n - pn
) * (ph
+ pi
) in
343 y + (pageno
- pn
) * (ph
+ pi
), ph
345 f 0 0 0 0 state.pages
348 let getpagey pageno
= fst
(getpageyh pageno
);;
351 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
352 let ((w
, h
, ips) as curr
), rest
, pdimno
, yinc
=
354 | (pageno'
, w
, h
) :: rest
when pageno'
= pageno
->
355 let ips = calcips h
in
356 let yinc = if conf.presentation
then ips else 0 in
357 (w
, h
, ips), rest
, pdimno
+ 1, yinc
359 prev
, pdims
, pdimno
, 0
361 let dy = dy + yinc in
362 let py = py + yinc in
363 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
368 if py + h
<= vy - yinc
370 let py = py + h
+ ips in
371 let dy = max
0 (py - y) in
381 let pagey = vy - py in
382 let pagevh = h
- pagey in
383 let pagevh = min
(sh
- dy) pagevh in
384 let off = if yinc > 0 then py - vy else 0
386 let py = py + h
+ ips in
392 ; pagedispy
= dy + off
393 ; pagey = pagey + off
394 ; pagevh = pagevh - off
397 let accu = e :: accu in
404 ~cacheleft
:(cacheleft
-1)
407 if state.invalidated
= 0
417 ~cacheleft
:(cblen state.pagecache
)
427 let y = state.y + incr
in
429 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
433 let getopaque pageno
=
434 try Some
(Hashtbl.find
state.pagemap
(pageno
+ 1, state.w
, state.rotate
))
435 with Not_found
-> None
438 let cache pageno opaque
=
439 Hashtbl.replace
state.pagemap
(pageno
+ 1, state.w
, state.rotate
) opaque
442 let validopaque opaque
= String.length opaque
> 0;;
445 match getopaque l
.pageno
with
446 | None
when not
state.rendering
->
447 state.rendering
<- true;
449 wcmd "render" [`i
(l
.pageno
+ 1)
457 let loadlayout layout =
458 let rec f all
= function
460 begin match getopaque l
.pageno
with
461 | None
-> render l
; f false ls
462 | Some opaque
-> f (all
&& validopaque opaque
) ls
466 f (layout <> []) layout;
473 let evictedopaque = cbpeekw state.pagecache
in
474 List.exists
(fun l
->
475 match getopaque l
.pageno
with
476 | Some opaque
when validopaque opaque
->
477 evictedopaque = opaque
481 if not
evictedvisible
483 let rely = yratio state.y in
484 let presentation = conf.presentation in
485 let interpagespace = conf.interpagespace in
486 let maxy = state.maxy in
487 conf.presentation <- false;
488 conf.interpagespace <- 0;
489 state.maxy <- calcheight ();
490 let y = truncate
(float state.maxy *. rely) in
491 let y = if y < state.h
then 0 else y - state.h
in
492 let pages = layout y (state.h
*3) in
493 List.iter
render pages;
494 conf.presentation <- presentation;
495 conf.interpagespace <- interpagespace;
501 let y = min
state.maxy y in
502 let pages = layout y state.h
in
503 let ready = loadlayout pages in
504 state.ty
<- yratio y;
509 state.layout <- pages;
511 Glut.postRedisplay
();
515 state.layout <- pages;
517 Glut.postRedisplay
();
522 let gotoy_and_clear_text y =
524 if not
conf.verbose
then state.text
<- "";
528 cbput state.hists
.nav
(yratio state.y);
529 cbrfollowlen state.hists
.nav
;
533 let y = cbget state.hists
.nav ~
-1 in
534 truncate
(y *. float state.maxy)
538 let y, h
= getpageyh n in
540 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
543 let gotopage1 n top
=
544 let y = getpagey n in
546 gotoy_and_clear_text (y + top
);
554 state.invalidated
<- state.invalidated
+ 1;
558 let c = c *. state.colorscale
in
564 match state.layout with
566 let rely = yratio state.y in
567 state.maxy <- calcheight ();
568 truncate
(float state.maxy *. rely)
571 state.maxy <- calcheight ();
578 GlMat.mode `projection
;
579 GlMat.load_identity
();
580 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
581 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
582 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float state.h
, 1.0);
586 GlMat.mode `projection
;
587 GlMat.load_identity
();
588 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
589 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
590 GlMat.scale3
(2.0 /. float state.winw
, 2.0 /. float state.h
, 1.0);
595 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
598 GlMat.mode `modelview
;
599 GlMat.load_identity
();
600 GlClear.color
(scalecolor 1.0);
601 GlClear.clear
[`color
];
604 wcmd "geometry" [`i
state.w; `i h
];
608 GlDraw.color
(0.0, 0.0, 0.0);
610 (0.0, float (state.h
- 18))
611 (float (state.winw
- conf.scrollw
- 1), float state.h
)
613 let font = Glut.BITMAP_8_BY_13
in
614 GlDraw.color
(1.0, 1.0, 1.0);
615 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
616 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
617 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
621 let len = String.length
state.text
in
622 match state.textentry
with
624 if len > 0 then showtext ' '
state.text
626 | Some
(c, text
, _
, _
, _
) ->
630 text ^
" [" ^
state.text ^
"]"
640 state.text
<- Printf.sprintf
"%c%s" c s;
641 Glut.postRedisplay
();
655 state.rects
<- state.rects1
;
656 Glut.postRedisplay
()
659 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
660 state.pagecount
<- n;
661 state.invalidated
<- state.invalidated
- 1;
662 if state.invalidated
= 0
666 let s = Scanf.sscanf
cmd "t %n"
667 (fun n -> String.sub
cmd n (String.length
cmd - n))
669 Glut.setWindowTitle
s
672 let s = Scanf.sscanf
cmd "T %n"
673 (fun n -> String.sub
cmd n (String.length
cmd - n))
675 if state.textentry
= None
682 Glut.postRedisplay
();
688 let s = Scanf.sscanf
cmd "V %n"
689 (fun n -> String.sub
cmd n (String.length
cmd - n))
695 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
696 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
697 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
698 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
700 let y = (getpagey pageno) + truncate y0
in
703 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
706 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
707 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
708 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
709 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
712 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
716 Scanf.sscanf
cmd "r %d %d %d %d %s"
717 (fun n w h r p
-> (n, w, h
, r
, p
))
719 Hashtbl.replace
state.pagemap
(n, w, r
) p
;
720 let opaque = cbpeekw state.pagecache
in
721 if validopaque opaque
725 (fun k v a
-> if v
= opaque then k else a
)
726 state.pagemap
(-1, -1, -1)
728 wcmd "free" [`
s opaque];
729 Hashtbl.remove
state.pagemap
k
731 cbput state.pagecache p
;
732 state.rendering
<- false;
734 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy)))
736 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
739 else (ignore
(loadlayout state.layout); preload ())
743 let (n, w, h
) as pagelayout
=
744 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w, h
)
746 state.pages <- pagelayout
:: state.pages
749 let (l
, n, t
, h
, pos
) =
750 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
752 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
754 let l = String.length
s in
755 let b = Buffer.create
(String.length
s) in
762 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
765 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
774 let outline = (s, l, n, float t
/. float h
) in
776 match state.outlines with
777 | Olist
outlines -> Olist
(outline :: outlines)
778 | Oarray _
-> Olist
[outline]
779 | Onarrow _
-> Olist
[outline]
781 state.outlines <- outlines
784 log "unknown cmd `%S'" cmd
787 let now = Unix.gettimeofday
;;
791 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
796 let y = state.y + conf.scrollincr
in
797 let y = if y >= state.maxy then 0 else y in
803 let cmd = readcmd state.csock
in
810 let onhist cb
= function
811 | HCprev
-> cbget cb ~
-1
812 | HCnext
-> cbget cb
1
813 | HCfirst
-> cbget cb ~
-(cb
.rc)
814 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
817 let search pattern forward
=
818 if String.length pattern
> 0
821 match state.layout with
824 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
827 let b = makecmd "search"
828 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
830 Buffer.add_char
b '
,'
;
831 Buffer.add_string
b pattern
;
832 Buffer.add_char
b '
\000'
;
835 writecmd state.csock
cmd;
838 let intentry text key
=
839 let c = Char.unsafe_chr key
in
842 let s = "x" in s.[0] <- c;
843 let text = text ^
s in
847 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
852 let b = Buffer.create
(String.length
s + 1) in
853 Buffer.add_string
b s;
858 let textentry text key
=
859 let c = Char.unsafe_chr key
in
861 | _
when key
>= 32 && key
< 127 ->
862 let text = addchar text c in
866 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
871 state.rotate <- angle
;
873 wcmd "rotate" [`i angle
];
876 let optentry text key
=
877 let btos b = if b then "on" else "off" in
878 let c = Char.unsafe_chr key
in
882 try conf.scrollincr
<- int_of_string
s with exc
->
883 state.text <- Printf.sprintf
"bad integer `%s': %s"
884 s (Printexc.to_string exc
)
886 TEswitch
('#'
, "", None
, intentry, ondone)
891 Some
(int_of_string
s)
893 state.text <- Printf.sprintf
"bad integer `%s': %s"
894 s (Printexc.to_string exc
);
897 | Some angle
-> rotate angle
900 TEswitch
('^'
, "", None
, intentry, ondone)
903 conf.icase
<- not
conf.icase
;
904 TEdone
("case insensitive search " ^
(btos conf.icase
))
907 conf.preload <- not
conf.preload;
909 TEdone
("preload " ^
(btos conf.preload))
912 conf.verbose
<- not
conf.verbose
;
913 TEdone
("verbose " ^
(btos conf.verbose
))
916 conf.maxhfit
<- not
conf.maxhfit
;
917 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h
else state.h
);
918 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
921 conf.crophack
<- not
conf.crophack
;
922 TEdone
("crophack " ^
btos conf.crophack
)
925 conf.showall
<- not
conf.showall
;
926 TEdone
("showall " ^
btos conf.showall
)
929 conf.underinfo
<- not
conf.underinfo
;
930 TEdone
("underinfo " ^
btos conf.underinfo
)
935 conf.interpagespace <- int_of_string
s;
936 let rely = yratio state.y in
937 state.maxy <- calcheight ();
938 gotoy (truncate
(float state.maxy *. rely));
940 state.text <- Printf.sprintf
"bad integer `%s': %s"
941 s (Printexc.to_string exc
)
943 TEswitch
('
%'
, "", None
, intentry, ondone)
946 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
950 let maxoutlinerows () = (state.h
- 31) / 16;;
952 let enterselector allowdel
outlines errmsg
=
953 if Array.length
outlines = 0
959 Glut.setCursor
Glut.CURSOR_INHERIT
;
961 match state.layout with
963 | {pageno=pageno} :: rest
-> pageno
967 if n = Array.length
outlines
970 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
971 if outlinepageno
>= pageno then n else loop (n+1)
976 Some
(allowdel
, active,
977 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
978 Glut.postRedisplay
();
982 let enteroutlinemode () =
984 match state.outlines with
987 let a = Array.of_list
(List.rev
l) in
988 state.outlines <- Oarray
a;
990 | Onarrow
(a, b) -> a
992 enterselector false outlines "Document has no outline";
995 let enterbookmarkmode () =
996 let bookmarks = Array.of_list
state.bookmarks in
997 enterselector true bookmarks "Document has no bookmarks (yet)";
1000 let quickbookmark ?title
() =
1001 match state.layout with
1007 let sec = Unix.gettimeofday
() in
1008 let tm = Unix.localtime
sec in
1009 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
1013 (tm.Unix.tm_year
+ 1900)
1016 | Some
title -> title
1019 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1023 state.fullscreen
<- None
;
1024 Glut.reshapeWindow
w h
;
1027 let opendoc path password
=
1030 state.password
<- password
;
1031 Hashtbl.clear
state.pagemap
;
1033 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1034 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1035 wcmd "geometry" [`i
state.w; `i
state.h
];
1038 let viewkeyboard ~key ~x ~
y =
1040 state.textentry <- te
;
1043 Glut.postRedisplay
()
1045 match state.textentry with
1047 let c = Char.chr key
in
1053 let y = getnav () in
1054 gotoy_and_clear_text y
1062 Glut.postRedisplay
()
1065 let ondone isforw
s =
1066 cbput state.hists
.pat
s;
1067 cbrfollowlen state.hists
.pat
;
1068 state.searchpattern
<- s;
1071 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1072 textentry, ondone (c ='
/'
)))
1074 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1075 conf.zoom
<- min
2.2 (conf.zoom
+. 0.1);
1076 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1077 reshape state.winw
state.h
1082 try int_of_string
s with exc
->
1083 state.text <- Printf.sprintf
"bad integer `%s': %s"
1084 s (Printexc.to_string exc
);
1090 state.text <- "page bias is now " ^ string_of_int
n;
1093 enttext (Some
('
+'
, "", None
, intentry, ondone))
1095 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1096 conf.zoom
<- max
0.1 (conf.zoom
-. 0.1);
1097 if conf.zoom
<= 1.0 then state.x
<- 0;
1098 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1099 reshape state.winw
state.h
;
1105 enttext (Some
('
-'
, "", None
, optentry, ondone))
1107 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1110 state.text <- "zoom is 100%";
1111 reshape state.winw
state.h
1116 try int_of_string
s with exc
->
1117 state.text <- Printf.sprintf
"bad integer `%s': %s"
1118 s (Printexc.to_string exc
);
1124 cbput state.hists
.pag
(string_of_int
n);
1125 cbrfollowlen state.hists
.pag
;
1126 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1129 let pageentry text key
=
1130 match Char.unsafe_chr key
with
1131 | 'g'
-> TEdone
text
1132 | _
-> intentry text key
1134 let text = "x" in text.[0] <- c;
1135 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1139 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
1140 reshape state.winw
state.h
;
1143 conf.hlinks
<- not
conf.hlinks
;
1144 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1145 Glut.postRedisplay
()
1148 conf.autoscroll
<- not
conf.autoscroll
1151 conf.presentation <- not
conf.presentation;
1152 showtext ' '
("presentation mode " ^
1153 if conf.presentation then "on" else "off");
1157 begin match state.fullscreen
with
1159 state.fullscreen
<- Some
(state.winw
, state.h
);
1162 state.fullscreen
<- None
;
1167 gotoy_and_clear_text 0
1170 search state.searchpattern
true
1173 search state.searchpattern
false
1176 begin match state.layout with
1179 gotoy_and_clear_text (getpagey l.pageno)
1183 begin match List.rev
state.layout with
1186 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1187 gotoy_and_clear_text (getpagey pageno)
1191 begin match state.layout with
1194 let pageno = max
0 (l.pageno-1) in
1195 gotoy_and_clear_text (getpagey pageno)
1200 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1202 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1204 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1208 else (100. *. (float state.y /. float maxy)) in
1211 Printf.sprintf
"Page %d of %d %.2f%%"
1212 (fn+1) state.pagecount
percent
1215 "Pages %d-%d of %d %.2f%%"
1216 (fn+1) (ln
+1) state.pagecount
percent
1221 begin match state.layout with
1224 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1225 Glut.postRedisplay
();
1229 enterbookmarkmode ()
1233 match state.layout with
1236 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1240 enttext (Some
('~'
, "", None
, textentry, ondone))
1244 showtext ' '
"Quick bookmark added";
1247 begin match state.layout with
1249 let a = getpagewh
l.pagedimno
in
1253 (truncate
(1.8 *. (a.(1) -. a.(0))),
1254 truncate
(1.2 *. (a.(3) -. a.(0))))
1256 (truncate
(a.(1) -. a.(0)),
1257 truncate
(a.(3) -. a.(0)))
1259 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace);
1260 Glut.postRedisplay
();
1266 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1271 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1272 Glut.postRedisplay
()
1274 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1275 | 'j'
-> gotoy (clamp conf.scrollincr
)
1277 | '
r'
-> opendoc state.path
state.password
1280 vlog "huh? %d %c" key
(Char.chr key
);
1283 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1284 let len = String.length
text in
1287 state.textentry <- None
;
1288 Glut.postRedisplay
();
1291 let s = String.sub
text 0 (len - 1) in
1292 enttext (Some
(c, s, onhist, onkey
, ondone))
1295 | Some
(c, text, onhist, onkey
, ondone) ->
1296 begin match Char.unsafe_chr key
with
1299 state.textentry <- None
;
1300 Glut.postRedisplay
()
1303 state.textentry <- None
;
1304 Glut.postRedisplay
()
1307 begin match onkey
text key
with
1309 state.textentry <- None
;
1311 Glut.postRedisplay
()
1314 enttext (Some
(c, text, onhist, onkey
, ondone));
1317 state.textentry <- None
;
1318 Glut.postRedisplay
()
1321 state.textentry <- Some te
;
1322 Glut.postRedisplay
()
1327 let narrow outlines pattern
=
1328 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1332 let rec fold accu n =
1336 let (s, _
, _
, _
) as o
= outlines.(n) in
1338 if (try ignore
(Str.search_forward re
s 0); true
1339 with Not_found
-> false)
1345 let matched = fold [] (Array.length
outlines - 1) in
1346 if matched = [] then None
else Some
(Array.of_list
matched)
1349 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1350 let search active pattern incr
=
1353 if n = Array.length
outlines || n = -1
1356 let (s, _
, _
, _
) = outlines.(n) in
1358 (try ignore
(Str.search_forward re
s 0); true
1359 with Not_found
-> false)
1361 else loop (n + incr
)
1366 let re = Str.regexp_case_fold pattern
in
1372 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1375 if String.length qsearch
= 0
1378 state.outline <- None
;
1379 Glut.postRedisplay
();
1383 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1384 Glut.postRedisplay
();
1388 let incr = if key
= 18 then -1 else 1 in
1390 match search (active + incr) qsearch
incr with
1392 state.text <- qsearch ^
" [not found]";
1395 state.text <- qsearch
;
1396 active, firstof active
1398 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1399 Glut.postRedisplay
();
1402 let len = String.length qsearch
in
1409 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1412 let qsearch = String.sub
qsearch 0 (len - 1) in
1414 match search active qsearch ~
-1 with
1416 state.text <- qsearch ^
" [not found]";
1419 state.text <- qsearch;
1420 active, firstof active
1422 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1424 Glut.postRedisplay
()
1427 if active < Array.length
outlines
1429 let (_
, _
, n, t
) = outlines.(active) in
1433 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1434 state.outline <- None
;
1435 Glut.postRedisplay
();
1437 | _
when key
>= 32 && key
< 127 ->
1438 let pattern = addchar qsearch (Char.chr key
) in
1440 match search active pattern 1 with
1442 state.text <- pattern ^
" [not found]";
1445 state.text <- pattern;
1446 active, firstof active
1448 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1449 Glut.postRedisplay
()
1451 | 14 when not allowdel
->
1452 let optoutlines = narrow outlines qsearch in
1453 begin match optoutlines with
1454 | None
-> state.text <- "can't narrow"
1456 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1457 match state.outlines with
1459 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1460 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1462 Glut.postRedisplay
()
1464 | 21 when not allowdel
->
1466 match state.outlines with
1469 let a = Array.of_list
(List.rev
l) in
1470 state.outlines <- Oarray
a;
1473 state.outlines <- Oarray
b;
1476 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1477 Glut.postRedisplay
()
1481 Some
(allowdel
, active, firstof active, outlines, qsearch);
1482 Glut.postRedisplay
()
1484 | 127 when allowdel
->
1485 let len = Array.length
outlines - 1 in
1488 state.outline <- None
;
1489 state.bookmarks <- [];
1492 let bookmarks = Array.init
len
1494 let i = if i >= active then i + 1 else i in
1505 Glut.postRedisplay
()
1507 | _
-> log "unknown key %d" key
1510 let keyboard ~key ~x ~
y =
1515 match state.outline with
1516 | None
-> viewkeyboard ~key ~x ~
y
1517 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1520 let special ~key ~x ~
y =
1521 match state.outline with
1523 begin match state.textentry with
1527 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1528 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1529 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1530 | Glut.KEY_PAGE_UP
->
1531 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1533 match state.layout with
1535 | l :: _
-> state.y - l.pagey
1538 | Glut.KEY_PAGE_DOWN
->
1539 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1541 match List.rev
state.layout with
1543 | l :: _
-> getpagey l.pageno
1546 | Glut.KEY_HOME
-> addnav (); 0
1549 state.maxy - (if conf.maxhfit
then state.h
else 0)
1551 | Glut.KEY_RIGHT
when conf.zoom
> 1.0 ->
1552 state.x
<- state.x
- 10;
1554 | Glut.KEY_LEFT
when conf.zoom
> 1.0 ->
1555 state.x
<- state.x
+ 10;
1560 gotoy_and_clear_text y
1562 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1565 | Glut.KEY_UP
-> onhist HCprev
1566 | Glut.KEY_DOWN
-> onhist HCnext
1567 | Glut.KEY_HOME
-> onhist HCfirst
1568 | Glut.KEY_END
-> onhist HClast
1571 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1572 Glut.postRedisplay
()
1577 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1578 let maxrows = maxoutlinerows () in
1580 let active = active + incr in
1581 let active = max
0 (min
active (Array.length
outlines - 1)) in
1585 let rows = active - first in
1586 if rows > maxrows then active - maxrows else first
1589 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1590 Glut.postRedisplay
()
1593 | Glut.KEY_UP
-> navigate ~
-1
1594 | Glut.KEY_DOWN
-> navigate 1
1595 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1596 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1599 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1600 Glut.postRedisplay
()
1603 let active = Array.length
outlines - 1 in
1604 let first = max
0 (active - maxrows) in
1605 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1606 Glut.postRedisplay
()
1611 let drawplaceholder l =
1612 GlDraw.color
(scalecolor 1.0);
1614 (0.0, float l.pagedispy
)
1615 (float l.pagew
, float (l.pagedispy
+ l.pagevh))
1618 and y = float (l.pagedispy
+ 13) in
1619 let font = Glut.BITMAP_8_BY_13
in
1620 GlDraw.color
(0.0, 0.0, 0.0);
1621 GlPix.raster_pos ~
x ~
y ();
1622 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1623 ("Loading " ^ string_of_int
l.pageno);
1626 let now () = Unix.gettimeofday
();;
1629 begin match getopaque l.pageno with
1630 | Some
opaque when validopaque opaque ->
1631 if state.textentry = None
1632 then GlDraw.color
(scalecolor 1.0)
1633 else GlDraw.color
(scalecolor 0.4);
1635 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1639 vlog "draw %d %f sec" l.pageno d;
1644 l.pagedispy
+ l.pagevh;
1647 let scrollindicator () =
1648 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1649 GlDraw.color
(0.64 , 0.64, 0.64);
1651 (float (state.winw
- conf.scrollw
), 0.)
1652 (float state.winw
, float state.h
)
1654 GlDraw.color
(0.0, 0.0, 0.0);
1655 let sh = (float (maxy + state.h
) /. float state.h
) in
1656 let sh = float state.h
/. sh in
1657 let sh = max
sh (float conf.scrollh
) in
1660 if state.y = state.maxy
1662 else float state.y /. float maxy
1664 let position = (float state.h
-. sh) *. percent in
1667 if position +. sh > float state.h
1674 (float (state.winw
- conf.scrollw
), position)
1675 (float state.winw
, position +. sh)
1679 let showsel margin
=
1680 match state.mstate
with
1684 | Msel
((x0
, y0
), (x1
, y1
)) ->
1685 let rec loop = function
1687 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1688 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1690 match getopaque l.pageno with
1691 | Some
opaque when validopaque opaque ->
1692 let oy = -l.pagey + l.pagedispy
in
1694 (x0
- margin
- state.x, y0
,
1695 x1
- margin
- state.x, y1
) oy;
1705 let panx = float state.x in
1707 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1708 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1710 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1712 if l.pageno = pageno
1714 let d = float (l.pagedispy
- l.pagey) in
1715 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1716 GlDraw.begins `quads
;
1718 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1719 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1720 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1721 GlDraw.vertex2
(x3
+.panx, y3
+.d);
1731 let showoutline = function
1733 | Some
(allowdel
, active, first, outlines, qsearch) ->
1735 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1736 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1737 GlDraw.rect
(0., 0.) (float state.w, float state.h
);
1740 GlDraw.color
(1., 1., 1.);
1741 let font = Glut.BITMAP_9_BY_15
in
1742 let draw_string x y s =
1743 GlPix.raster_pos ~
x ~
y ();
1744 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1747 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1750 let (s, l, _
, _
) = outlines.(row
) in
1751 let y = (row
- first) * 16 in
1756 GlDraw.polygon_mode `both `line
;
1757 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1758 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1759 GlDraw.rect
(0., float (y + 1))
1760 (float (state.w - 1), float (y + 18));
1761 GlDraw.polygon_mode `both `fill
;
1763 GlDraw.color
(1., 1., 1.);
1765 draw_string (float x) (float (y + 16)) s;
1773 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1774 GlDraw.viewport
margin 0 state.w state.h
;
1776 GlClear.color
(scalecolor 0.5);
1777 GlClear.clear
[`color
];
1780 let x = float state.x in
1781 GlMat.translate ~
x ();
1785 Gl.enable `scissor_test
;
1786 GlMisc.scissor
0 0 (state.winw
- conf.scrollw
) state.h
;
1788 let _lasty = List.fold_left
drawpage 0 (state.layout) in
1791 Gl.disable `scissor_test
1795 let x = -.float state.x in
1796 GlMat.translate ~
x ();
1800 GlDraw.viewport
0 0 state.winw
state.h
;
1803 showoutline state.outline;
1805 Glut.swapBuffers
();
1809 let margin = (state.winw
- (state.w + conf.scrollw
)) / 2 in
1810 let x = x - margin - state.x in
1811 let rec f = function
1813 begin match getopaque l.pageno with
1814 | Some
opaque when validopaque opaque ->
1815 let y = y - l.pagedispy
in
1818 let y = l.pagey + y in
1819 match whatsunder
opaque x y with
1832 let mouse ~button ~bstate ~
x ~
y =
1834 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1842 let incr = incr * 2 in
1843 let y = clamp incr in
1844 gotoy_and_clear_text y
1846 | Glut.LEFT_BUTTON
when state.outline = None
1847 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1848 if bstate
= Glut.DOWN
1850 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1851 state.mstate
<- Mpan
(x, y)
1854 state.mstate
<- Mnone
1856 | Glut.LEFT_BUTTON
when state.outline = None
->
1857 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1858 begin match dest with
1859 | Ulinkgoto
(pageno, top
) ->
1862 gotopage1 pageno top
1867 | Unone
when bstate
= Glut.DOWN
->
1868 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1869 state.mstate
<- Mpan
(x, y);
1871 | Unone
| Utext _
->
1872 if bstate
= Glut.DOWN
1874 if state.rotate mod 360 = 0
1876 state.mstate
<- Msel
((x, y), (x, y));
1877 Glut.postRedisplay
()
1881 match state.mstate
with
1885 Glut.setCursor
Glut.CURSOR_INHERIT
;
1886 state.mstate
<- Mnone
1888 | Msel
((x0
, y0
), (x1
, y1
)) ->
1890 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1891 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1893 match getopaque l.pageno with
1894 | Some
opaque when validopaque opaque ->
1898 List.iter
f state.layout;
1899 copysel
""; (* ugly *)
1900 Glut.setCursor
Glut.CURSOR_INHERIT
;
1901 state.mstate
<- Mnone
;
1908 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1911 if state.outline = None
1913 match state.mstate
with
1919 state.mstate
<- Mpan
(x, y);
1920 if conf.zoom
> 1.0 then state.x <- state.x + dx;
1922 gotoy_and_clear_text y
1925 state.mstate
<- Msel
(a, (x, y));
1926 Glut.postRedisplay
()
1930 if state.outline = None
1932 match state.mstate
with
1934 begin match getunder x y with
1935 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
1937 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
1938 Glut.setCursor
Glut.CURSOR_INFO
1939 | Ulinkgoto
(page
, y) ->
1940 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
1941 Glut.setCursor
Glut.CURSOR_INFO
1943 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
1944 Glut.setCursor
Glut.CURSOR_TEXT
1947 | Mpan _
| Msel _
->
1954 if Sys.os_type
= "Win32"
1956 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1958 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1960 Filename.concat
home "llpp"
1964 let ic = open_in_bin
statepath in
1965 let hash = input_value
ic in
1971 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1978 match state.fullscreen
with
1979 | None
-> state.winw
, state.h
1982 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h
);
1983 let oc = open_out_bin
statepath in
1984 output_value
oc pstate
1988 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1993 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1996 state.bookmarks <- statebookmarks;
1997 with Not_found
-> ()
1999 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
2003 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2004 (fun s -> state.path
<- s)
2005 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2008 if String.length
state.path
= 0
2009 then (prerr_endline
"filename missing"; exit
1)
2014 let _ = Glut.init
Sys.argv
in
2015 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2016 let () = Glut.initWindowSize
state.w state.h
in
2017 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
2020 if Sys.os_type
= "Unix"
2022 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2024 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2025 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2026 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2027 Unix.bind
sock addr;
2029 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2030 Unix.connect
csock addr;
2031 let ssock, _ = Unix.accept
sock in
2034 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2035 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2039 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2043 let () = Glut.displayFunc
display in
2044 let () = Glut.reshapeFunc
reshape in
2045 let () = Glut.keyboardFunc
keyboard in
2046 let () = Glut.specialFunc
special in
2047 let () = Glut.idleFunc
(Some
idle) in
2048 let () = Glut.mouseFunc
mouse in
2049 let () = Glut.motionFunc
motion in
2050 let () = Glut.passiveMotionFunc
pmotion in
2053 state.csock <- csock;
2054 state.ssock <- ssock;
2055 state.text <- "Opening " ^
name;
2056 writecmd state.csock ("open " ^
state.path ^
"\000" ^
state.password ^
"\000");
2060 let rec handlelablglutbug () =
2063 with Glut.BadEnum
"key in special_of_int" ->
2064 showtext '
!'
" LablGlut bug: special key not recognized";
2065 handlelablglutbug ()
2067 handlelablglutbug ();