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 preload
: string -> unit = "ml_preload";;
9 external gettext
: string -> (int * int * int * int) -> int -> bool -> unit =
11 external checklink
: string -> int -> int -> bool = "ml_checklink";;
12 external getlink
: string -> int -> int -> (int * int) option = "ml_getlink";;
13 external getpagewh
: int -> float array
= "ml_getpagewh";;
15 type mstate
= Msel
of ((int * int) * (int * int)) | Mnone
;;
32 { store
= Array.create n v
39 let cblen b
= Array.length b
.store
;;
44 b
.wc
<- (b
.wc
+ 1) mod len;
45 b
.len <- (b
.len + 1) mod len;
48 let cbpeekw b
= b
.store
.(b
.wc
);;
51 let v = b
.store
.(b
.rc
) in
56 let rc = if b
.rc = 0 then b
.len - 1 else b
.rc - 1 in
78 { mutable scrollw
: int
79 ; mutable scrollh
: int
80 ; mutable rectsel
: bool
81 ; mutable icase
: bool
82 ; mutable preload
: bool
83 ; mutable titletext
: bool
84 ; mutable pagebias
: int
85 ; mutable redispimm
: bool
86 ; mutable verbose
: bool
87 ; mutable scrollincr
: int
88 ; mutable maxhfit
: 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
149 ; pagemap
= Hashtbl.create
10
150 ; pagecache
= cbnew 10 ""
155 ; navhist
= cbnew 100 0.0
162 ; outlines
= Olist
[]
172 Printf.kprintf prerr_endline fmt
174 Printf.kprintf ignore fmt
178 let len = String.length s
in
180 let b = Buffer.create
n in
181 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
182 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
183 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
184 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
185 Buffer.add_string
b s
;
186 let s'
= Buffer.contents
b in
187 let n'
= Unix.write fd
s'
0 n in
188 if n'
!= n then failwith
"write failed";
193 let n = Unix.read fd
s 0 4 in
194 if n != 4 then failwith
"incomplete read(len)";
196 lor (Char.code
s.[0] lsl 24)
197 lor (Char.code
s.[1] lsl 16)
198 lor (Char.code
s.[2] lsl 8)
199 lor (Char.code
s.[3] lsl 0)
201 let s = String.create
len in
202 let n = Unix.read fd
s 0 len in
203 if n != len then failwith
"incomplete read(data)";
208 if y
= state.maxy
then 1.0
209 else float y
/. float state.maxy
213 let b = Buffer.create
10 in
214 Buffer.add_string
b s;
215 let rec combine = function
218 Buffer.add_char
b ' '
;
221 | `
b b -> if b then "1" else "0"
223 | `i i
-> string_of_int i
224 | `f f
-> string_of_float f
225 | `I f
-> string_of_int
(truncate f
)
227 Buffer.add_string
b s;
234 let cmd = Buffer.contents
(makecmd s l
) in
235 writecmd state.csock
cmd;
239 let rec f pn ph fh l
=
241 | (n, _
, h
) :: rest
->
242 let fh = fh + (n - pn
) * ph
in
246 let fh = fh + (ph
* (state.pagecount
- pn
)) in
249 let fh = f 0 0 0 state.pages
in
253 let getpagey pageno
=
254 let rec f pn ph y l
=
256 | (n, _
, h
) :: rest
->
259 y
+ (pageno
- pn
) * ph
261 let y = y + (n - pn
) * ph
in
265 y + (pageno
- pn
) * ph
271 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
272 if pageno
= state.pagecount
|| cacheleft
= 0
275 let ((_
, w
, h
) as curr
), rest
, pdimno
=
277 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
278 curr
, rest
, pdimno
+ 1
282 let pageno'
= pageno + 1 in
316 let accu = e :: accu in
317 f pageno' pdimno curr
318 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
319 (pred cacheleft
) accu
321 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
323 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
324 state.maxy
<- calcheight ();
329 let y = state.y + incr
in
331 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
337 let y = min
state.maxy
y in
338 let pages = layout y state.h
in
340 state.layout <- pages;
343 Glut.postRedisplay
()
348 cbput state.navhist
(yratio state.y);
349 cbrfollowlen state.navhist
;
353 let y = cbget state.navhist
in
354 truncate
(y *. float state.maxy
)
358 let y = getpagey n in
365 let ratio = float w
/. float state.w
in
366 let fixbookmark (s, l
, pageno, pagey
) =
367 let pagey = truncate
(float pagey *. ratio) in
368 (s, l
, pageno, pagey)
370 state.bookmarks
<- List.map
fixbookmark state.bookmarks
;
373 GlDraw.viewport
0 0 w h
;
374 GlMat.mode `modelview
;
375 GlMat.load_identity
();
376 GlMat.mode `projection
;
377 GlMat.load_identity
();
378 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
379 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
380 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
381 GlClear.color
(1., 1., 1.);
382 GlClear.clear
[`color
];
387 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
391 if not
conf.titletext
393 GlDraw.color
(0.0, 0.0, 0.0);
395 (0.0, float (state.h
- 18))
396 (float (state.w
- conf.scrollw
- 1), float state.h
)
398 let font = Glut.BITMAP_8_BY_13
in
399 GlDraw.color
(1.0, 1.0, 1.0);
400 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
401 Glut.bitmapCharacter ~
font ~c
:(Char.code c
);
402 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s
405 let len = String.length
s in
406 let dst = String.create
(len + 1) in
415 Glut.setWindowTitle ~title
:dst
419 let len = String.length
state.text
in
420 match state.textentry
with
422 if len > 0 then showtext ' '
state.text
424 | Some
(c
, text
, _
, _
) ->
428 text ^
" [" ^
state.text ^
"]"
441 state.rects
<- state.rects1
;
442 Glut.postRedisplay
()
445 state.rects
<- state.rects1
;
446 Glut.postRedisplay
()
449 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
450 state.pagecount
<- n;
451 let rely = yratio state.y in
452 let maxy = calcheight () in
453 state.y <- truncate
(float maxy *. rely);
454 let pages = layout state.y state.h
in
455 state.layout <- pages;
456 Glut.postRedisplay
();
459 let s = Scanf.sscanf
cmd "T %n"
460 (fun n -> String.sub
cmd n (String.length
cmd - n))
465 (* Glut.postRedisplay () *)
468 let pageno, c
, x0
, y0
, x1
, y1
=
469 Scanf.sscanf
cmd "F %d %d %f %f %f %f"
470 (fun p c x0 y0 x1 y1
-> (p
, c
, x0
, y0
, x1
, y1
))
472 let y = (getpagey pageno) + truncate y0
in
475 state.rects1
<- [pageno, c
, (x0
, y0
), (x1
, y1
)]
478 let pageno, c
, x0
, y0
, x1
, y1
=
479 Scanf.sscanf
cmd "R %d %d %f %f %f %f"
480 (fun pageno c x0 y0 x1 y1
-> (pageno, c
, x0
, y0
, x1
, y1
))
482 state.rects1
<- (pageno, c
, (x0
, y0
), (x1
, y1
)) :: state.rects1
486 Scanf.sscanf
cmd "r %d %d %d %s"
487 (fun n w h p
-> (n, w
, h
, p
))
489 Hashtbl.replace
state.pagemap
(n, w
) p
;
490 let evicted = cbpeekw state.pagecache
in
491 if String.length
evicted > 0
493 wcmd "free" [`
s evicted];
494 let l = Hashtbl.fold
(fun k p a
->
495 if evicted = p
then k
:: a
else a
) state.pagemap
[]
497 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
499 cbput state.pagecache p
;
500 state.inflight
<- pred
state.inflight
;
501 Glut.postRedisplay
()
504 let (n, w
, h
) as pagelayout
=
505 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
507 state.pages <- pagelayout
:: state.pages
511 Scanf.sscanf
cmd "o %d %d %d %n" (fun l n t pos
-> l, n, t
, pos
)
513 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
514 let outline = (s, l, n, t
) in
516 match state.outlines with
517 | Olist
outlines -> Olist
(outline :: outlines)
518 | Oarray _
-> Olist
[outline]
520 state.outlines <- outlines
523 log "unknown cmd `%S'" cmd
526 let getopaque pageno =
527 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
))
528 with Not_found
-> None
531 let cache pageno opaque
=
532 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
) opaque
535 let validopaque opaque
= String.length opaque
> 0;;
538 match getopaque l.pageno with
539 | Some opaque
when validopaque opaque
->
542 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
543 state.inflight
<- succ
state.inflight
;
545 wcmd "render" [`i
(l.pageno + 1)
554 if not
conf.redispimm
&& state.y != state.prevy
556 state.prevy
<- state.y;
557 Glut.postRedisplay
();
560 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.02 in
564 if conf.preload then begin
566 let y = if state.y < state.h then 0 else state.y - state.h in
567 let pages = layout y (h*3) in
568 List.iter
preload pages;
572 let cmd = readcmd state.csock
in
577 let search pattern forward
=
578 if String.length pattern
> 0
581 match state.layout with
584 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
587 let b = makecmd "search"
588 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
590 Buffer.add_char
b '
,'
;
591 Buffer.add_string
b pattern
;
592 Buffer.add_char
b '
\000'
;
595 writecmd state.csock
cmd;
598 let intentry text key
=
599 let c = Char.unsafe_chr key
in
602 let s = "x" in s.[0] <- c;
603 let text = text ^
s in
607 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
612 let b = Buffer.create
(String.length
s + 1) in
613 Buffer.add_string
b s;
618 let textentry text key
=
619 let c = Char.unsafe_chr key
in
621 | _
when key
>= 32 && key
< 127 ->
622 let text = addchar text c in
626 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
630 let optentry text key
=
631 let btos b = if b then "on" else "off" in
632 let c = Char.unsafe_chr key
in
635 conf.rectsel
<- not
conf.rectsel
;
636 TEdone
("rectsel " ^
(btos conf.rectsel
))
639 conf.icase
<- not
conf.icase
;
640 TEdone
("case insensitive search " ^
(btos conf.icase
))
643 conf.preload <- not
conf.preload;
644 TEdone
("preload " ^
(btos conf.preload))
647 conf.titletext
<- not
conf.titletext
;
648 TEdone
("titletext " ^
(btos conf.titletext
))
651 conf.redispimm
<- not
conf.redispimm
;
652 TEdone
("immediate redisplay " ^
(btos conf.redispimm
))
655 conf.verbose
<- not
conf.verbose
;
656 TEdone
("verbose " ^
(btos conf.verbose
))
659 conf.maxhfit
<- not
conf.maxhfit
;
660 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
661 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
664 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
668 let maxoutlinerows () = (state.h - 31) / 16;;
670 let enterselector allowdel
outlines errmsg
=
671 if Array.length
outlines = 0
678 match state.layout with
680 | {pageno=pageno} :: rest
-> pageno
684 if n = Array.length
outlines
687 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
688 if outlinepageno
>= pageno then n else loop (n+1)
693 Some
(allowdel
, active, max
0 (active - maxoutlinerows ()), outlines, "");
694 Glut.postRedisplay
();
697 let enteroutlinemode () =
699 match state.outlines with
702 let a = Array.of_list
(List.rev
l) in
703 state.outlines <- Oarray
a;
706 enterselector false outlines "Documents has no outline";
709 let enterbookmarkmode () =
710 let bookmarks = Array.of_list
state.bookmarks in
711 enterselector true bookmarks "Documents has no bookmarks (yet)";
714 let viewkeyboard ~key ~x ~
y =
716 state.textentry <- te
;
721 match state.textentry with
723 let c = Char.chr key
in
738 Glut.postRedisplay
()
741 let ondone isforw
s =
742 state.searchpattern
<- s;
745 enttext (Some
(c, "", textentry, ondone (c ='
/'
)))
750 try int_of_string
s with exc
->
751 state.text <- Printf.sprintf
"bad integer `%s': %s"
752 s (Printexc.to_string exc
);
758 state.text <- "page bias is now " ^ string_of_int
n;
761 enttext (Some
('
+'
, "", intentry, ondone))
767 enttext (Some
('
-'
, "", optentry, ondone))
772 try int_of_string
s with exc
->
773 state.text <- Printf.sprintf
"bad integer `%s': %s"
774 s (Printexc.to_string exc
);
781 gotoy (getpagey (n + conf.pagebias
- 1))
784 let pageentry text key
=
785 match Char.unsafe_chr key
with
787 | _
-> intentry text key
789 let text = "x" in text.[0] <- c;
790 enttext (Some
('
:'
, text, pageentry, ondone))
793 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
794 reshape state.w
state.h;
797 begin match state.fullscreen
with
799 state.fullscreen
<- Some
(state.w
, state.h);
802 state.fullscreen
<- None
;
803 Glut.reshapeWindow ~w ~
h
807 search state.searchpattern
true
810 search state.searchpattern
false
813 begin match state.layout with
816 gotoy (state.y - l.pagey);
820 begin match List.rev
state.layout with
823 gotoy (clamp (l.pageh
- l.pagey))
827 begin match state.layout with
830 gotoy (clamp (-l.pageh
));
835 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
837 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
839 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
840 let percent = (100. *. (float state.y /. float maxy)) in
843 Printf.sprintf
"Page %d of %d %.2f%%"
844 (fn+1) state.pagecount
percent
847 "Pages %d-%d of %d %.2f%%"
848 (fn+1) (ln
+1) state.pagecount
percent
854 begin match state.layout with
857 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
858 Glut.postRedisplay
();
866 match state.layout with
868 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
871 enttext (Some
('~'
, "", textentry, ondone))
875 begin match state.layout with
877 let a = getpagewh
l.pagedimno
in
878 let w = truncate
(a.(1) -. a.(0))
879 and h = truncate
(a.(3) -. a.(0)) in
880 Glut.reshapeWindow
(w + conf.scrollw
) h;
881 Glut.postRedisplay
();
887 vlog "huh? %d %c" key
(Char.chr key
);
890 | Some
(c, text, onkey
, ondone) when key
= 8 ->
891 let len = String.length
text in
892 if len = 0 || len = 1
894 state.textentry <- None
;
895 Glut.postRedisplay
();
898 let s = String.sub
text 0 (len - 1) in
899 enttext (Some
(c, s, onkey
, ondone))
902 | Some
(c, text, onkey
, ondone) ->
903 begin match Char.unsafe_chr key
with
906 state.textentry <- None
;
907 Glut.postRedisplay
()
910 state.textentry <- None
;
911 Glut.postRedisplay
()
914 begin match onkey
text key
with
916 state.textentry <- None
;
918 Glut.postRedisplay
()
921 enttext (Some
(c, text, onkey
, ondone));
924 state.textentry <- None
;
925 Glut.postRedisplay
()
930 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
931 let search active pattern incr
=
932 let re = Str.regexp_case_fold pattern
in
934 if n = Array.length
outlines || n = -1 then None
else
935 let (s, _
, _
, _
) = outlines.(n) in
937 (try ignore
(Str.search_forward
re s 0); true
938 with Not_found
-> false)
940 let maxrows = maxoutlinerows () in
942 then Some
(n, max
0 (n - maxrows))
943 else Some
(n, max first
(n - maxrows))
951 if String.length qsearch
= 0
954 state.outline <- None
;
955 Glut.postRedisplay
();
959 state.outline <- Some
(allowdel
, active, first
, outlines, "");
960 Glut.postRedisplay
();
964 let incr = if key
= 18 then -1 else 1 in
966 match search (active + incr) qsearch
incr with
967 | None
-> active, first
970 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
971 Glut.postRedisplay
();
974 let len = String.length qsearch
in
981 state.outline <- Some
(allowdel
, active, first
, outlines, "");
984 let qsearch = String.sub
qsearch 0 (len - 1) in
985 state.text <- qsearch;
986 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
988 Glut.postRedisplay
()
991 if active < Array.length
outlines
993 let (_
, _
, n, t
) = outlines.(active) in
997 if allowdel
then state.bookmarks <- Array.to_list
outlines;
998 state.outline <- None
;
999 Glut.postRedisplay
();
1001 | _
when key
>= 32 && key
< 127 ->
1002 let pattern = addchar qsearch (Char.chr key
) in
1003 let pattern, active, first
=
1004 match search active pattern 1 with
1005 | None
-> qsearch, active, first
1006 | Some
(active, first
) -> (pattern, active, first
)
1008 state.text <- pattern;
1009 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1010 Glut.postRedisplay
()
1012 | 127 when allowdel
->
1013 let len = Array.length
outlines - 1 in
1016 state.outline <- None
;
1017 state.bookmarks <- [];
1020 let bookmarks = Array.init
len
1022 let i = if i >= active then i + 1 else i in
1033 Glut.postRedisplay
()
1035 | _
-> log "unknown key %d" key
1038 let keyboard ~key ~x ~
y =
1039 match state.outline with
1040 | None
-> viewkeyboard ~key ~x ~
y
1041 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1044 let special ~key ~x ~
y =
1045 match state.outline with
1049 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1050 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1051 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1052 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1053 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1054 | Glut.KEY_HOME
-> addnav (); 0
1057 state.maxy - (if conf.maxhfit
then state.h else 0)
1063 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1064 let maxrows = maxoutlinerows () in
1066 let active = active + incr in
1067 let active = max
0 (min
active (Array.length
outlines - 1)) in
1071 let rows = active - first in
1072 if rows > maxrows then first + incr else first
1075 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1076 Glut.postRedisplay
()
1079 | Glut.KEY_UP
-> navigate ~
-1
1080 | Glut.KEY_DOWN
-> navigate 1
1081 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1082 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1085 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1086 Glut.postRedisplay
()
1089 let active = Array.length
outlines - 1 in
1090 let first = max
0 (active - maxrows) in
1091 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1092 Glut.postRedisplay
()
1097 let drawplaceholder l =
1100 GlDraw.color
(0.2, 0.2, 0.2);
1101 GlDraw.color
(1.0, 1.0, 1.0);
1103 (0.0, float l.pagedispy
)
1104 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1107 and y = float (l.pagedispy
+ 13) in
1108 let font = Glut.BITMAP_8_BY_13
in
1109 GlDraw.color
(0.0, 0.0, 0.0);
1110 GlPix.raster_pos ~
x ~
y ();
1111 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1112 ("Loading " ^ string_of_int
l.pageno);
1115 GlDraw.begins `quads
;
1116 GlDraw.vertex2
(0.0, float l.pagedispy
);
1117 GlDraw.vertex2
(float l.pagew
, float l.pagedispy
);
1118 GlDraw.vertex2
(float l.pagew
, float (l.pagedispy
+ l.pagevh
));
1119 GlDraw.vertex2
(0.0, float (l.pagedispy
+ l.pagevh
));
1124 let now () = Unix.gettimeofday
();;
1127 begin match getopaque l.pageno with
1128 | Some opaque
when validopaque opaque
->
1129 GlDraw.color
(1.0, 1.0, 1.0);
1131 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1134 vlog "draw %f sec" d;
1141 if state.inflight
< cblen state.pagecache
1143 List.iter
preload state.layout;
1146 vlog "inflight %d" state.inflight
;
1149 GlDraw.color
(0.5, 0.5, 0.5);
1152 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1154 l.pagedispy
+ l.pagevh
;
1157 let scrollindicator () =
1158 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1159 GlDraw.color
(0.64 , 0.64, 0.64);
1161 (float (state.w - conf.scrollw
), 0.)
1162 (float state.w, float state.h)
1164 GlDraw.color
(0.0, 0.0, 0.0);
1165 let sh = (float (maxy + state.h) /. float state.h) in
1166 let sh = float state.h /. sh in
1167 let sh = max
sh (float conf.scrollh
) in
1170 if state.y = state.maxy
1172 else float state.y /. float maxy
1174 let position = (float state.h -. sh) *. percent in
1177 if position +. sh > float state.h
1184 (float (state.w - conf.scrollw
), position)
1185 (float state.w, position +. sh)
1190 match state.mstate
with
1194 | Msel
((x0
, y0
), (x1
, y1
)) ->
1196 and y1
= max
y0 y1
in
1199 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1200 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1202 match getopaque l.pageno with
1203 | Some opaque
when validopaque opaque
->
1204 let oy = -l.pagey + l.pagedispy
in
1205 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1208 List.iter
f state.layout
1213 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1214 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1216 (fun (pageno, c, (x0
, y0), (x1
, y1
)) ->
1218 if l.pageno = pageno
1220 let d = float (l.pagedispy
- l.pagey) in
1221 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1222 GlDraw.rect
(x0
, y0 +. d) (x1
, y1
+. d)
1230 let showoutline = function
1232 | Some
(allowdel
, active, first, outlines, qsearch) ->
1234 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1235 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1236 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1239 GlDraw.color
(1., 1., 1.);
1240 let font = Glut.BITMAP_9_BY_15
in
1241 let draw_string x y s =
1242 GlPix.raster_pos ~
x ~
y ();
1243 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1246 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1249 let (s, l, _
, _
) = outlines.(row
) in
1250 let y = (row
- first) * 16 in
1255 GlDraw.polygon_mode `both `line
;
1256 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1257 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1258 GlDraw.rect
(0., float (y + 1))
1259 (float (state.w - conf.scrollw
- 1), float (y + 18));
1260 GlDraw.polygon_mode `both `fill
;
1262 GlDraw.color
(1., 1., 1.);
1264 draw_string (float x) (float (y + 16)) s;
1272 let lasty = List.fold_left
drawpage 0 (state.layout) in
1273 GlDraw.color
(0.5, 0.5, 0.5);
1276 (float (state.w - conf.scrollw
), float state.h)
1281 showoutline state.outline;
1283 Glut.swapBuffers
();
1287 let rec f = function
1289 begin match getopaque l.pageno with
1290 | Some opaque
when validopaque opaque
->
1291 let y = y - l.pagedispy
in
1294 let y = l.pagey + y in
1295 match getlink opaque
x y with
1309 let rec f = function
1311 begin match getopaque l.pageno with
1312 | Some opaque
when validopaque opaque
->
1313 let y = y - l.pagedispy
in
1316 let y = l.pagey + y in
1317 if checklink opaque
x y then true else f rest
1328 let mouse ~button ~bstate ~
x ~
y =
1330 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1338 let incr = incr * 2 in
1339 let y = clamp incr in
1342 | Glut.LEFT_BUTTON
when state.outline = None
->
1343 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1344 begin match dest with
1345 | Some
(pageno, top
) ->
1349 if bstate
= Glut.DOWN
1351 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1352 state.mstate
<- Msel
((x, y), (x, y));
1353 Glut.postRedisplay
()
1356 Glut.setCursor
Glut.CURSOR_RIGHT_ARROW
;
1357 state.mstate
<- Mnone
;
1364 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1367 if state.outline = None
1369 match state.mstate
with
1372 state.mstate
<- Msel
(a, (x, y));
1373 Glut.postRedisplay
()
1377 if state.outline = None
1379 match state.mstate
with
1380 | Mnone
when (checklink x y) ->
1381 Glut.setCursor
Glut.CURSOR_INFO
1384 Glut.setCursor
Glut.CURSOR_RIGHT_ARROW
1391 let statepath = (Sys.getenv
"HOME") ^
"/.config/llpp" in
1394 let ic = open_in_bin
statepath in
1395 let hash = input_value
ic in
1401 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1408 match state.fullscreen
with
1409 | None
-> state.w, state.h
1412 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1413 let oc = open_out_bin
statepath in
1414 output_value
oc pstate
1418 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1423 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1426 state.bookmarks <- statebookmarks;
1427 with Not_found
-> ()
1429 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1432 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1434 if String.length
state.path
= 0
1435 then (prerr_endline
"filename missing"; exit
1)
1440 let _ = Glut.init
Sys.argv
in
1441 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1442 let () = Glut.initWindowSize
state.w state.h in
1443 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1445 let csock, ssock
= Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0 in
1448 state.csock <- csock;
1449 state.ssock
<- ssock
;
1450 writecmd csock ("open " ^
name ^
"\000");
1452 let () = Glut.displayFunc
display in
1453 let () = Glut.reshapeFunc
reshape in
1454 let () = Glut.keyboardFunc
keyboard in
1455 let () = Glut.specialFunc
special in
1456 let () = Glut.idleFunc
(Some
idle) in
1457 let () = Glut.mouseFunc
mouse in
1458 let () = Glut.motionFunc
motion in
1459 let () = Glut.passiveMotionFunc
pmotion in
1463 let rec handlelablglutbug () =
1466 with Glut.BadEnum
"key in special_of_int" ->
1467 showtext '
!'
" LablGlut bug: special key not recognized";
1468 Glut.swapBuffers
();
1469 handlelablglutbug ()
1471 handlelablglutbug ();