3 let log fmt
= Printf.kprintf prerr_endline fmt
;;
4 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
6 external init
: Unix.file_descr
-> unit = "ml_init";;
7 external draw
: int -> int -> int -> int -> string -> unit = "ml_draw";;
8 external gettext
: string -> (int * int * int * int) -> int -> bool -> unit =
10 external checklink
: string -> int -> int -> bool = "ml_checklink";;
11 external getlink
: string -> int -> int -> (int * int) option = "ml_getlink";;
12 external getpagewh
: int -> float array
= "ml_getpagewh";;
14 type mstate
= Msel
of ((int * int) * (int * int)) | Mnone
;;
31 { store
= Array.create n v
38 let cblen b
= Array.length b
.store
;;
43 b
.wc
<- (b
.wc
+ 1) mod len;
44 b
.len <- (b
.len + 1) mod len;
47 let cbpeekw b
= b
.store
.(b
.wc
);;
50 let v = b
.store
.(b
.rc
) in
55 let rc = if b
.rc = 0 then b
.len - 1 else b
.rc - 1 in
77 { mutable scrollw
: int
78 ; mutable scrollh
: int
79 ; mutable rectsel
: bool
80 ; mutable icase
: bool
81 ; mutable preload
: bool
82 ; mutable titletext
: bool
83 ; mutable pagebias
: int
84 ; mutable redispimm
: bool
85 ; mutable verbose
: bool
86 ; mutable scrollincr
: int
87 ; mutable maxhfit
: bool
88 ; mutable markonquit
: bool
92 type outline
= string * int * int * int;;
93 type outlines
= Oarray
of outline array
| Olist
of outline list
;;
96 { mutable csock
: Unix.file_descr
97 ; mutable ssock
: Unix.file_descr
101 ; mutable prevy
: int
103 ; mutable layout
: layout list
104 ; pagemap
: ((int * int), string) Hashtbl.t
105 ; mutable pages
: (int * int * int) list
106 ; mutable pagecount
: int
107 ; pagecache
: string circbuf
108 ; navhist
: float circbuf
109 ; mutable inflight
: int
110 ; mutable mstate
: mstate
111 ; mutable searchpattern
: string
112 ; mutable rects
: (int * int * Gl.point2
* Gl.point2
) list
113 ; mutable rects1
: (int * int * Gl.point2
* Gl.point2
) list
114 ; mutable text
: string
115 ; mutable fullscreen
: (int * int) option
116 ; mutable textentry
:
117 (char
* string * (string -> int -> te
) * (string -> unit)) option
118 ; mutable outlines
: outlines
119 ; mutable outline
: (bool * int * int * outline array
* string) option
120 ; mutable bookmarks
: outline list
121 ; mutable path
: string
150 ; pagemap
= Hashtbl.create
10
151 ; pagecache
= cbnew 10 ""
156 ; navhist
= cbnew 100 0.0
163 ; outlines
= Olist
[]
173 Printf.kprintf prerr_endline fmt
175 Printf.kprintf ignore fmt
179 let len = String.length s
in
181 let b = Buffer.create
n in
182 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
183 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
184 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
185 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
186 Buffer.add_string
b s
;
187 let s'
= Buffer.contents
b in
188 let n'
= Unix.write fd
s'
0 n in
189 if n'
!= n then failwith
"write failed";
194 let n = Unix.read fd
s 0 4 in
195 if n != 4 then failwith
"incomplete read(len)";
197 lor (Char.code
s.[0] lsl 24)
198 lor (Char.code
s.[1] lsl 16)
199 lor (Char.code
s.[2] lsl 8)
200 lor (Char.code
s.[3] lsl 0)
202 let s = String.create
len in
203 let n = Unix.read fd
s 0 len in
204 if n != len then failwith
"incomplete read(data)";
209 if y
= state.maxy
then 1.0
210 else float y
/. float state.maxy
214 let b = Buffer.create
10 in
215 Buffer.add_string
b s;
216 let rec combine = function
219 Buffer.add_char
b ' '
;
222 | `
b b -> if b then "1" else "0"
224 | `i i
-> string_of_int i
225 | `f f
-> string_of_float f
226 | `I f
-> string_of_int
(truncate f
)
228 Buffer.add_string
b s;
235 let cmd = Buffer.contents
(makecmd s l
) in
236 writecmd state.csock
cmd;
240 let rec f pn ph fh l
=
242 | (n, _
, h
) :: rest
->
243 let fh = fh + (n - pn
) * ph
in
247 let fh = fh + (ph
* (state.pagecount
- pn
)) in
250 let fh = f 0 0 0 state.pages
in
254 let getpagey pageno
=
255 let rec f pn ph y l
=
257 | (n, _
, h
) :: rest
->
260 y
+ (pageno
- pn
) * ph
262 let y = y + (n - pn
) * ph
in
266 y + (pageno
- pn
) * ph
272 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
273 if pageno
= state.pagecount
|| cacheleft
= 0
276 let ((_
, w
, h
) as curr
), rest
, pdimno
=
278 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
279 curr
, rest
, pdimno
+ 1
283 let pageno'
= pageno + 1 in
317 let accu = e :: accu in
318 f pageno' pdimno curr
319 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
320 (pred cacheleft
) accu
322 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
324 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
325 state.maxy
<- calcheight ();
330 let y = state.y + incr
in
332 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
338 let y = min
state.maxy
y in
339 let pages = layout y state.h
in
341 state.layout <- pages;
344 Glut.postRedisplay
()
349 cbput state.navhist
(yratio state.y);
350 cbrfollowlen state.navhist
;
354 let y = cbget state.navhist
in
355 truncate
(y *. float state.maxy
)
359 let y = getpagey n in
366 let ratio = float w
/. float state.w
in
367 let fixbookmark (s, l
, pageno, pagey
) =
368 let pagey = truncate
(float pagey *. ratio) in
369 (s, l
, pageno, pagey)
371 state.bookmarks
<- List.map
fixbookmark state.bookmarks
;
374 GlDraw.viewport
0 0 w h
;
375 GlMat.mode `modelview
;
376 GlMat.load_identity
();
377 GlMat.mode `projection
;
378 GlMat.load_identity
();
379 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
380 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
381 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
382 GlClear.color
(1., 1., 1.);
383 GlClear.clear
[`color
];
388 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
392 if not
conf.titletext
394 GlDraw.color
(0.0, 0.0, 0.0);
396 (0.0, float (state.h
- 18))
397 (float (state.w
- conf.scrollw
- 1), float state.h
)
399 let font = Glut.BITMAP_8_BY_13
in
400 GlDraw.color
(1.0, 1.0, 1.0);
401 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
402 Glut.bitmapCharacter ~
font ~c
:(Char.code c
);
403 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s
406 let len = String.length
s in
407 let dst = String.create
(len + 1) in
416 Glut.setWindowTitle ~title
:dst
420 let len = String.length
state.text
in
421 match state.textentry
with
423 if len > 0 then showtext ' '
state.text
425 | Some
(c
, text
, _
, _
) ->
429 text ^
" [" ^
state.text ^
"]"
442 state.rects
<- state.rects1
;
443 Glut.postRedisplay
()
446 state.rects
<- state.rects1
;
447 Glut.postRedisplay
()
450 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
451 state.pagecount
<- n;
452 let rely = yratio state.y in
453 let maxy = calcheight () in
454 state.y <- truncate
(float maxy *. rely);
455 let pages = layout state.y state.h
in
456 state.layout <- pages;
457 Glut.postRedisplay
();
460 let s = Scanf.sscanf
cmd "T %n"
461 (fun n -> String.sub
cmd n (String.length
cmd - n))
466 (* Glut.postRedisplay () *)
469 let pageno, c
, x0
, y0
, x1
, y1
=
470 Scanf.sscanf
cmd "F %d %d %f %f %f %f"
471 (fun p c x0 y0 x1 y1
-> (p
, c
, x0
, y0
, x1
, y1
))
473 let y = (getpagey pageno) + truncate y0
in
476 state.rects1
<- [pageno, c
, (x0
, y0
), (x1
, y1
)]
479 let pageno, c
, x0
, y0
, x1
, y1
=
480 Scanf.sscanf
cmd "R %d %d %f %f %f %f"
481 (fun pageno c x0 y0 x1 y1
-> (pageno, c
, x0
, y0
, x1
, y1
))
483 state.rects1
<- (pageno, c
, (x0
, y0
), (x1
, y1
)) :: state.rects1
487 Scanf.sscanf
cmd "r %d %d %d %s"
488 (fun n w h p
-> (n, w
, h
, p
))
490 Hashtbl.replace
state.pagemap
(n, w
) p
;
491 let evicted = cbpeekw state.pagecache
in
492 if String.length
evicted > 0
494 wcmd "free" [`
s evicted];
495 let l = Hashtbl.fold
(fun k p a
->
496 if evicted = p
then k
:: a
else a
) state.pagemap
[]
498 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
500 cbput state.pagecache p
;
501 state.inflight
<- pred
state.inflight
;
502 Glut.postRedisplay
()
505 let (n, w
, h
) as pagelayout
=
506 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
508 state.pages <- pagelayout
:: state.pages
512 Scanf.sscanf
cmd "o %d %d %d %n" (fun l n t pos
-> l, n, t
, pos
)
514 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
515 let outline = (s, l, n, t
) in
517 match state.outlines with
518 | Olist
outlines -> Olist
(outline :: outlines)
519 | Oarray _
-> Olist
[outline]
521 state.outlines <- outlines
524 log "unknown cmd `%S'" cmd
527 let getopaque pageno =
528 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
))
529 with Not_found
-> None
532 let cache pageno opaque
=
533 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
) opaque
536 let validopaque opaque
= String.length opaque
> 0;;
539 match getopaque l.pageno with
540 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
541 state.inflight
<- succ
state.inflight
;
543 wcmd "render" [`i
(l.pageno + 1)
552 if not
conf.redispimm
&& state.y != state.prevy
554 state.prevy
<- state.y;
555 Glut.postRedisplay
();
558 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.02 in
562 if conf.preload then begin
564 let y = if state.y < state.h then 0 else state.y - state.h in
565 let pages = layout y (h*3) in
566 List.iter
preload pages;
570 let cmd = readcmd state.csock
in
575 let search pattern forward
=
576 if String.length pattern
> 0
579 match state.layout with
582 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
585 let b = makecmd "search"
586 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
588 Buffer.add_char
b '
,'
;
589 Buffer.add_string
b pattern
;
590 Buffer.add_char
b '
\000'
;
593 writecmd state.csock
cmd;
596 let intentry text key
=
597 let c = Char.unsafe_chr key
in
600 let s = "x" in s.[0] <- c;
601 let text = text ^
s in
605 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
610 let b = Buffer.create
(String.length
s + 1) in
611 Buffer.add_string
b s;
616 let textentry text key
=
617 let c = Char.unsafe_chr key
in
619 | _
when key
>= 32 && key
< 127 ->
620 let text = addchar text c in
624 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
628 let optentry text key
=
629 let btos b = if b then "on" else "off" in
630 let c = Char.unsafe_chr key
in
633 conf.rectsel
<- not
conf.rectsel
;
634 TEdone
("rectsel " ^
(btos conf.rectsel
))
637 conf.icase
<- not
conf.icase
;
638 TEdone
("case insensitive search " ^
(btos conf.icase
))
641 conf.preload <- not
conf.preload;
642 TEdone
("preload " ^
(btos conf.preload))
645 conf.titletext
<- not
conf.titletext
;
646 TEdone
("titletext " ^
(btos conf.titletext
))
649 conf.redispimm
<- not
conf.redispimm
;
650 TEdone
("immediate redisplay " ^
(btos conf.redispimm
))
653 conf.verbose
<- not
conf.verbose
;
654 TEdone
("verbose " ^
(btos conf.verbose
))
657 conf.maxhfit
<- not
conf.maxhfit
;
658 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
659 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
662 conf.markonquit
<- not
conf.markonquit
;
663 TEdone
("bookmark on quit " ^
btos conf.markonquit
)
666 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
670 let maxoutlinerows () = (state.h - 31) / 16;;
672 let enterselector allowdel
outlines errmsg
=
673 if Array.length
outlines = 0
680 match state.layout with
682 | {pageno=pageno} :: rest
-> pageno
686 if n = Array.length
outlines
689 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
690 if outlinepageno
>= pageno then n else loop (n+1)
695 Some
(allowdel
, active, max
0 (active - maxoutlinerows ()), outlines, "");
696 Glut.postRedisplay
();
699 let enteroutlinemode () =
701 match state.outlines with
704 let a = Array.of_list
(List.rev
l) in
705 state.outlines <- Oarray
a;
708 enterselector false outlines "Documents has no outline";
711 let enterbookmarkmode () =
712 let bookmarks = Array.of_list
state.bookmarks in
713 enterselector true bookmarks "Documents has no bookmarks (yet)";
717 let quickbookmark ?title
() =
718 match state.layout with
724 let sec = Unix.gettimeofday
() in
725 let tm = Unix.localtime
sec in
726 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
730 (tm.Unix.tm_year
+ 1900)
733 | Some
title -> title
736 (title, 0, l.pageno, l.pagey) :: state.bookmarks
739 let viewkeyboard ~key ~x ~
y =
741 state.textentry <- te
;
746 match state.textentry with
748 let c = Char.chr key
in
763 Glut.postRedisplay
()
766 let ondone isforw
s =
767 state.searchpattern
<- s;
770 enttext (Some
(c, "", textentry, ondone (c ='
/'
)))
775 try int_of_string
s with exc
->
776 state.text <- Printf.sprintf
"bad integer `%s': %s"
777 s (Printexc.to_string exc
);
783 state.text <- "page bias is now " ^ string_of_int
n;
786 enttext (Some
('
+'
, "", intentry, ondone))
792 enttext (Some
('
-'
, "", optentry, ondone))
797 try int_of_string
s with exc
->
798 state.text <- Printf.sprintf
"bad integer `%s': %s"
799 s (Printexc.to_string exc
);
806 gotoy (getpagey (n + conf.pagebias
- 1))
809 let pageentry text key
=
810 match Char.unsafe_chr key
with
812 | _
-> intentry text key
814 let text = "x" in text.[0] <- c;
815 enttext (Some
('
:'
, text, pageentry, ondone))
818 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
819 reshape state.w
state.h;
822 begin match state.fullscreen
with
824 state.fullscreen
<- Some
(state.w
, state.h);
827 state.fullscreen
<- None
;
828 Glut.reshapeWindow ~w ~
h
832 search state.searchpattern
true
835 search state.searchpattern
false
838 begin match state.layout with
841 gotoy (state.y - l.pagey);
845 begin match List.rev
state.layout with
848 gotoy (clamp (l.pageh
- l.pagey))
852 begin match state.layout with
855 gotoy (clamp (-l.pageh
));
860 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
862 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
864 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
865 let percent = (100. *. (float state.y /. float maxy)) in
868 Printf.sprintf
"Page %d of %d %.2f%%"
869 (fn+1) state.pagecount
percent
872 "Pages %d-%d of %d %.2f%%"
873 (fn+1) (ln
+1) state.pagecount
percent
879 begin match state.layout with
882 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
883 Glut.postRedisplay
();
891 match state.layout with
893 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
896 enttext (Some
('~'
, "", textentry, ondone))
900 showtext ' '
"Quick bookmark added";
904 begin match state.layout with
906 let a = getpagewh
l.pagedimno
in
907 let w = truncate
(a.(1) -. a.(0))
908 and h = truncate
(a.(3) -. a.(0)) in
909 Glut.reshapeWindow
(w + conf.scrollw
) h;
910 Glut.postRedisplay
();
916 vlog "huh? %d %c" key
(Char.chr key
);
919 | Some
(c, text, onkey
, ondone) when key
= 8 ->
920 let len = String.length
text in
921 if len = 0 || len = 1
923 state.textentry <- None
;
924 Glut.postRedisplay
();
927 let s = String.sub
text 0 (len - 1) in
928 enttext (Some
(c, s, onkey
, ondone))
931 | Some
(c, text, onkey
, ondone) ->
932 begin match Char.unsafe_chr key
with
935 state.textentry <- None
;
936 Glut.postRedisplay
()
939 state.textentry <- None
;
940 Glut.postRedisplay
()
943 begin match onkey
text key
with
945 state.textentry <- None
;
947 Glut.postRedisplay
()
950 enttext (Some
(c, text, onkey
, ondone));
953 state.textentry <- None
;
954 Glut.postRedisplay
()
959 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
960 let search active pattern incr
=
961 let re = Str.regexp_case_fold pattern
in
963 if n = Array.length
outlines || n = -1 then None
else
964 let (s, _
, _
, _
) = outlines.(n) in
966 (try ignore
(Str.search_forward
re s 0); true
967 with Not_found
-> false)
969 let maxrows = maxoutlinerows () in
971 then Some
(n, max
0 (n - maxrows))
972 else Some
(n, max first
(n - maxrows))
980 if String.length qsearch
= 0
983 state.outline <- None
;
984 Glut.postRedisplay
();
988 state.outline <- Some
(allowdel
, active, first
, outlines, "");
989 Glut.postRedisplay
();
993 let incr = if key
= 18 then -1 else 1 in
995 match search (active + incr) qsearch
incr with
996 | None
-> active, first
999 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1000 Glut.postRedisplay
();
1003 let len = String.length qsearch
in
1010 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1013 let qsearch = String.sub
qsearch 0 (len - 1) in
1014 state.text <- qsearch;
1015 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1017 Glut.postRedisplay
()
1020 if active < Array.length
outlines
1022 let (_
, _
, n, t
) = outlines.(active) in
1026 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1027 state.outline <- None
;
1028 Glut.postRedisplay
();
1030 | _
when key
>= 32 && key
< 127 ->
1031 let pattern = addchar qsearch (Char.chr key
) in
1032 let pattern, active, first
=
1033 match search active pattern 1 with
1034 | None
-> qsearch, active, first
1035 | Some
(active, first
) -> (pattern, active, first
)
1037 state.text <- pattern;
1038 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1039 Glut.postRedisplay
()
1041 | 127 when allowdel
->
1042 let len = Array.length
outlines - 1 in
1045 state.outline <- None
;
1046 state.bookmarks <- [];
1049 let bookmarks = Array.init
len
1051 let i = if i >= active then i + 1 else i in
1062 Glut.postRedisplay
()
1064 | _
-> log "unknown key %d" key
1067 let keyboard ~key ~x ~
y =
1068 match state.outline with
1069 | None
-> viewkeyboard ~key ~x ~
y
1070 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1073 let special ~key ~x ~
y =
1074 match state.outline with
1078 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1079 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1080 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1081 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1082 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1083 | Glut.KEY_HOME
-> addnav (); 0
1086 state.maxy - (if conf.maxhfit
then state.h else 0)
1092 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1093 let maxrows = maxoutlinerows () in
1095 let active = active + incr in
1096 let active = max
0 (min
active (Array.length
outlines - 1)) in
1100 let rows = active - first in
1101 if rows > maxrows then first + incr else first
1104 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1105 Glut.postRedisplay
()
1108 | Glut.KEY_UP
-> navigate ~
-1
1109 | Glut.KEY_DOWN
-> navigate 1
1110 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1111 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1114 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1115 Glut.postRedisplay
()
1118 let active = Array.length
outlines - 1 in
1119 let first = max
0 (active - maxrows) in
1120 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1121 Glut.postRedisplay
()
1126 let drawplaceholder l =
1129 GlDraw.color
(0.2, 0.2, 0.2);
1130 GlDraw.color
(1.0, 1.0, 1.0);
1132 (0.0, float l.pagedispy
)
1133 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1136 and y = float (l.pagedispy
+ 13) in
1137 let font = Glut.BITMAP_8_BY_13
in
1138 GlDraw.color
(0.0, 0.0, 0.0);
1139 GlPix.raster_pos ~
x ~
y ();
1140 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1141 ("Loading " ^ string_of_int
l.pageno);
1144 GlDraw.begins `quads
;
1145 GlDraw.vertex2
(0.0, float l.pagedispy
);
1146 GlDraw.vertex2
(float l.pagew
, float l.pagedispy
);
1147 GlDraw.vertex2
(float l.pagew
, float (l.pagedispy
+ l.pagevh
));
1148 GlDraw.vertex2
(0.0, float (l.pagedispy
+ l.pagevh
));
1153 let now () = Unix.gettimeofday
();;
1156 begin match getopaque l.pageno with
1157 | Some opaque
when validopaque opaque
->
1158 GlDraw.color
(1.0, 1.0, 1.0);
1160 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1163 vlog "draw %f sec" d;
1170 if state.inflight
< cblen state.pagecache
1172 List.iter
preload state.layout;
1175 vlog "inflight %d" state.inflight
;
1178 GlDraw.color
(0.5, 0.5, 0.5);
1181 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1183 l.pagedispy
+ l.pagevh
;
1186 let scrollindicator () =
1187 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1188 GlDraw.color
(0.64 , 0.64, 0.64);
1190 (float (state.w - conf.scrollw
), 0.)
1191 (float state.w, float state.h)
1193 GlDraw.color
(0.0, 0.0, 0.0);
1194 let sh = (float (maxy + state.h) /. float state.h) in
1195 let sh = float state.h /. sh in
1196 let sh = max
sh (float conf.scrollh
) in
1199 if state.y = state.maxy
1201 else float state.y /. float maxy
1203 let position = (float state.h -. sh) *. percent in
1206 if position +. sh > float state.h
1213 (float (state.w - conf.scrollw
), position)
1214 (float state.w, position +. sh)
1219 match state.mstate
with
1223 | Msel
((x0
, y0
), (x1
, y1
)) ->
1225 and y1
= max
y0 y1
in
1228 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1229 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1231 match getopaque l.pageno with
1232 | Some opaque
when validopaque opaque
->
1233 let oy = -l.pagey + l.pagedispy
in
1234 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1237 List.iter
f state.layout
1242 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1243 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1245 (fun (pageno, c, (x0
, y0), (x1
, y1
)) ->
1247 if l.pageno = pageno
1249 let d = float (l.pagedispy
- l.pagey) in
1250 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1251 GlDraw.rect
(x0
, y0 +. d) (x1
, y1
+. d)
1259 let showoutline = function
1261 | Some
(allowdel
, active, first, outlines, qsearch) ->
1263 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1264 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1265 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1268 GlDraw.color
(1., 1., 1.);
1269 let font = Glut.BITMAP_9_BY_15
in
1270 let draw_string x y s =
1271 GlPix.raster_pos ~
x ~
y ();
1272 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1275 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1278 let (s, l, _
, _
) = outlines.(row
) in
1279 let y = (row
- first) * 16 in
1284 GlDraw.polygon_mode `both `line
;
1285 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1286 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1287 GlDraw.rect
(0., float (y + 1))
1288 (float (state.w - conf.scrollw
- 1), float (y + 18));
1289 GlDraw.polygon_mode `both `fill
;
1291 GlDraw.color
(1., 1., 1.);
1293 draw_string (float x) (float (y + 16)) s;
1301 let lasty = List.fold_left
drawpage 0 (state.layout) in
1302 GlDraw.color
(0.5, 0.5, 0.5);
1305 (float (state.w - conf.scrollw
), float state.h)
1310 showoutline state.outline;
1312 Glut.swapBuffers
();
1316 let rec f = function
1318 begin match getopaque l.pageno with
1319 | Some opaque
when validopaque opaque
->
1320 let y = y - l.pagedispy
in
1323 let y = l.pagey + y in
1324 match getlink opaque
x y with
1338 let rec f = function
1340 begin match getopaque l.pageno with
1341 | Some opaque
when validopaque opaque
->
1342 let y = y - l.pagedispy
in
1345 let y = l.pagey + y in
1346 if checklink opaque
x y then true else f rest
1357 let mouse ~button ~bstate ~
x ~
y =
1359 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1367 let incr = incr * 2 in
1368 let y = clamp incr in
1371 | Glut.LEFT_BUTTON
when state.outline = None
->
1372 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1373 begin match dest with
1374 | Some
(pageno, top
) ->
1378 if bstate
= Glut.DOWN
1380 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1381 state.mstate
<- Msel
((x, y), (x, y));
1382 Glut.postRedisplay
()
1385 Glut.setCursor
Glut.CURSOR_INHERIT
;
1386 state.mstate
<- Mnone
;
1393 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1396 if state.outline = None
1398 match state.mstate
with
1401 state.mstate
<- Msel
(a, (x, y));
1402 Glut.postRedisplay
()
1406 if state.outline = None
1408 match state.mstate
with
1409 | Mnone
when (checklink x y) ->
1410 Glut.setCursor
Glut.CURSOR_INFO
1413 Glut.setCursor
Glut.CURSOR_INHERIT
1420 let statepath = (Sys.getenv
"HOME") ^
"/.config/llpp" in
1423 let ic = open_in_bin
statepath in
1424 let hash = input_value
ic in
1430 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1437 match state.fullscreen
with
1438 | None
-> state.w, state.h
1441 if conf.markonquit
then quickbookmark ();
1442 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1443 let oc = open_out_bin
statepath in
1444 output_value
oc pstate
1448 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1453 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1456 state.bookmarks <- statebookmarks;
1457 with Not_found
-> ()
1459 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1462 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1464 if String.length
state.path
= 0
1465 then (prerr_endline
"filename missing"; exit
1)
1470 let _ = Glut.init
Sys.argv
in
1471 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1472 let () = Glut.initWindowSize
state.w state.h in
1473 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1475 let csock, ssock
= Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0 in
1478 state.csock <- csock;
1479 state.ssock
<- ssock
;
1480 writecmd csock ("open " ^
name ^
"\000");
1482 let () = Glut.displayFunc
display in
1483 let () = Glut.reshapeFunc
reshape in
1484 let () = Glut.keyboardFunc
keyboard in
1485 let () = Glut.specialFunc
special in
1486 let () = Glut.idleFunc
(Some
idle) in
1487 let () = Glut.mouseFunc
mouse in
1488 let () = Glut.motionFunc
motion in
1489 let () = Glut.passiveMotionFunc
pmotion in
1493 let rec handlelablglutbug () =
1496 with Glut.BadEnum
"key in special_of_int" ->
1497 showtext '
!'
" LablGlut bug: special key not recognized";
1498 Glut.swapBuffers
();
1499 handlelablglutbug ()
1501 handlelablglutbug ();