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 state.outlines
<- Olist
[];
458 Glut.postRedisplay
();
461 let s = Scanf.sscanf
cmd "T %n"
462 (fun n -> String.sub
cmd n (String.length
cmd - n))
467 (* Glut.postRedisplay () *)
470 let pageno, c
, x0
, y0
, x1
, y1
=
471 Scanf.sscanf
cmd "F %d %d %f %f %f %f"
472 (fun p c x0 y0 x1 y1
-> (p
, c
, x0
, y0
, x1
, y1
))
474 let y = (getpagey pageno) + truncate y0
in
477 state.rects1
<- [pageno, c
, (x0
, y0
), (x1
, y1
)]
480 let pageno, c
, x0
, y0
, x1
, y1
=
481 Scanf.sscanf
cmd "R %d %d %f %f %f %f"
482 (fun pageno c x0 y0 x1 y1
-> (pageno, c
, x0
, y0
, x1
, y1
))
484 state.rects1
<- (pageno, c
, (x0
, y0
), (x1
, y1
)) :: state.rects1
488 Scanf.sscanf
cmd "r %d %d %d %s"
489 (fun n w h p
-> (n, w
, h
, p
))
491 Hashtbl.replace
state.pagemap
(n, w
) p
;
492 let evicted = cbpeekw state.pagecache
in
493 if String.length
evicted > 0
495 wcmd "free" [`
s evicted];
496 let l = Hashtbl.fold
(fun k p a
->
497 if evicted = p
then k
:: a
else a
) state.pagemap
[]
499 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
501 cbput state.pagecache p
;
502 state.inflight
<- pred
state.inflight
;
503 Glut.postRedisplay
()
506 let (n, w
, h
) as pagelayout
=
507 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
509 state.pages <- pagelayout
:: state.pages
513 Scanf.sscanf
cmd "o %d %d %d %n" (fun l n t pos
-> l, n, t
, pos
)
515 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
516 let outline = (s, l, n, t
) in
518 match state.outlines with
519 | Olist
outlines -> Olist
(outline :: outlines)
520 | Oarray _
-> Olist
[outline]
522 state.outlines <- outlines
525 log "unknown cmd `%S'" cmd
528 let getopaque pageno =
529 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
))
530 with Not_found
-> None
533 let cache pageno opaque
=
534 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
) opaque
537 let validopaque opaque
= String.length opaque
> 0;;
540 match getopaque l.pageno with
541 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
542 state.inflight
<- succ
state.inflight
;
544 wcmd "render" [`i
(l.pageno + 1)
553 if not
conf.redispimm
&& state.y != state.prevy
555 state.prevy
<- state.y;
556 Glut.postRedisplay
();
559 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.02 in
563 if conf.preload then begin
565 let y = if state.y < state.h then 0 else state.y - state.h in
566 let pages = layout y (h*3) in
567 List.iter
preload pages;
571 let cmd = readcmd state.csock
in
576 let search pattern forward
=
577 if String.length pattern
> 0
580 match state.layout with
583 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
586 let b = makecmd "search"
587 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
589 Buffer.add_char
b '
,'
;
590 Buffer.add_string
b pattern
;
591 Buffer.add_char
b '
\000'
;
594 writecmd state.csock
cmd;
597 let intentry text key
=
598 let c = Char.unsafe_chr key
in
601 let s = "x" in s.[0] <- c;
602 let text = text ^
s in
606 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
611 let b = Buffer.create
(String.length
s + 1) in
612 Buffer.add_string
b s;
617 let textentry text key
=
618 let c = Char.unsafe_chr key
in
620 | _
when key
>= 32 && key
< 127 ->
621 let text = addchar text c in
625 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
629 let optentry text key
=
630 let btos b = if b then "on" else "off" in
631 let c = Char.unsafe_chr key
in
634 conf.rectsel
<- not
conf.rectsel
;
635 TEdone
("rectsel " ^
(btos conf.rectsel
))
638 conf.icase
<- not
conf.icase
;
639 TEdone
("case insensitive search " ^
(btos conf.icase
))
642 conf.preload <- not
conf.preload;
643 TEdone
("preload " ^
(btos conf.preload))
646 conf.titletext
<- not
conf.titletext
;
647 TEdone
("titletext " ^
(btos conf.titletext
))
650 conf.redispimm
<- not
conf.redispimm
;
651 TEdone
("immediate redisplay " ^
(btos conf.redispimm
))
654 conf.verbose
<- not
conf.verbose
;
655 TEdone
("verbose " ^
(btos conf.verbose
))
658 conf.maxhfit
<- not
conf.maxhfit
;
659 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
660 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
663 conf.markonquit
<- not
conf.markonquit
;
664 TEdone
("bookmark on quit " ^
btos conf.markonquit
)
667 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
671 let maxoutlinerows () = (state.h - 31) / 16;;
673 let enterselector allowdel
outlines errmsg
=
674 if Array.length
outlines = 0
681 match state.layout with
683 | {pageno=pageno} :: rest
-> pageno
687 if n = Array.length
outlines
690 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
691 if outlinepageno
>= pageno then n else loop (n+1)
696 Some
(allowdel
, active, max
0 (active - maxoutlinerows ()), outlines, "");
697 Glut.postRedisplay
();
700 let enteroutlinemode () =
702 match state.outlines with
705 let a = Array.of_list
(List.rev
l) in
706 state.outlines <- Oarray
a;
709 enterselector false outlines "Documents has no outline";
712 let enterbookmarkmode () =
713 let bookmarks = Array.of_list
state.bookmarks in
714 enterselector true bookmarks "Documents has no bookmarks (yet)";
718 let quickbookmark ?title
() =
719 match state.layout with
725 let sec = Unix.gettimeofday
() in
726 let tm = Unix.localtime
sec in
727 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
731 (tm.Unix.tm_year
+ 1900)
734 | Some
title -> title
737 (title, 0, l.pageno, l.pagey) :: state.bookmarks
740 let viewkeyboard ~key ~x ~
y =
742 state.textentry <- te
;
747 match state.textentry with
749 let c = Char.chr key
in
764 Glut.postRedisplay
()
767 let ondone isforw
s =
768 state.searchpattern
<- s;
771 enttext (Some
(c, "", textentry, ondone (c ='
/'
)))
776 try int_of_string
s with exc
->
777 state.text <- Printf.sprintf
"bad integer `%s': %s"
778 s (Printexc.to_string exc
);
784 state.text <- "page bias is now " ^ string_of_int
n;
787 enttext (Some
('
+'
, "", intentry, ondone))
793 enttext (Some
('
-'
, "", optentry, ondone))
798 try int_of_string
s with exc
->
799 state.text <- Printf.sprintf
"bad integer `%s': %s"
800 s (Printexc.to_string exc
);
807 gotoy (getpagey (n + conf.pagebias
- 1))
810 let pageentry text key
=
811 match Char.unsafe_chr key
with
813 | _
-> intentry text key
815 let text = "x" in text.[0] <- c;
816 enttext (Some
('
:'
, text, pageentry, ondone))
819 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
820 reshape state.w
state.h;
823 begin match state.fullscreen
with
825 state.fullscreen
<- Some
(state.w
, state.h);
828 state.fullscreen
<- None
;
829 Glut.reshapeWindow ~w ~
h
833 search state.searchpattern
true
836 search state.searchpattern
false
839 begin match state.layout with
842 gotoy (state.y - l.pagey);
846 begin match List.rev
state.layout with
849 gotoy (clamp (l.pageh
- l.pagey))
853 begin match state.layout with
856 gotoy (clamp (-l.pageh
));
861 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
863 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
865 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
866 let percent = (100. *. (float state.y /. float maxy)) in
869 Printf.sprintf
"Page %d of %d %.2f%%"
870 (fn+1) state.pagecount
percent
873 "Pages %d-%d of %d %.2f%%"
874 (fn+1) (ln
+1) state.pagecount
percent
880 begin match state.layout with
883 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
884 Glut.postRedisplay
();
892 match state.layout with
894 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
897 enttext (Some
('~'
, "", textentry, ondone))
901 showtext ' '
"Quick bookmark added";
905 begin match state.layout with
907 let a = getpagewh
l.pagedimno
in
908 let w = truncate
(a.(1) -. a.(0))
909 and h = truncate
(a.(3) -. a.(0)) in
910 Glut.reshapeWindow
(w + conf.scrollw
) h;
911 Glut.postRedisplay
();
917 vlog "huh? %d %c" key
(Char.chr key
);
920 | Some
(c, text, onkey
, ondone) when key
= 8 ->
921 let len = String.length
text in
922 if len = 0 || len = 1
924 state.textentry <- None
;
925 Glut.postRedisplay
();
928 let s = String.sub
text 0 (len - 1) in
929 enttext (Some
(c, s, onkey
, ondone))
932 | Some
(c, text, onkey
, ondone) ->
933 begin match Char.unsafe_chr key
with
936 state.textentry <- None
;
937 Glut.postRedisplay
()
940 state.textentry <- None
;
941 Glut.postRedisplay
()
944 begin match onkey
text key
with
946 state.textentry <- None
;
948 Glut.postRedisplay
()
951 enttext (Some
(c, text, onkey
, ondone));
954 state.textentry <- None
;
955 Glut.postRedisplay
()
960 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
961 let search active pattern incr
=
962 let re = Str.regexp_case_fold pattern
in
964 if n = Array.length
outlines || n = -1 then None
else
965 let (s, _
, _
, _
) = outlines.(n) in
967 (try ignore
(Str.search_forward
re s 0); true
968 with Not_found
-> false)
970 let maxrows = maxoutlinerows () in
972 then Some
(n, max
0 (n - maxrows))
973 else Some
(n, max first
(n - maxrows))
981 if String.length qsearch
= 0
984 state.outline <- None
;
985 Glut.postRedisplay
();
989 state.outline <- Some
(allowdel
, active, first
, outlines, "");
990 Glut.postRedisplay
();
994 let incr = if key
= 18 then -1 else 1 in
996 match search (active + incr) qsearch
incr with
997 | None
-> active, first
1000 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1001 Glut.postRedisplay
();
1004 let len = String.length qsearch
in
1011 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1014 let qsearch = String.sub
qsearch 0 (len - 1) in
1015 state.text <- qsearch;
1016 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1018 Glut.postRedisplay
()
1021 if active < Array.length
outlines
1023 let (_
, _
, n, t
) = outlines.(active) in
1027 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1028 state.outline <- None
;
1029 Glut.postRedisplay
();
1031 | _
when key
>= 32 && key
< 127 ->
1032 let pattern = addchar qsearch (Char.chr key
) in
1033 let pattern, active, first
=
1034 match search active pattern 1 with
1035 | None
-> qsearch, active, first
1036 | Some
(active, first
) -> (pattern, active, first
)
1038 state.text <- pattern;
1039 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1040 Glut.postRedisplay
()
1042 | 127 when allowdel
->
1043 let len = Array.length
outlines - 1 in
1046 state.outline <- None
;
1047 state.bookmarks <- [];
1050 let bookmarks = Array.init
len
1052 let i = if i >= active then i + 1 else i in
1063 Glut.postRedisplay
()
1065 | _
-> log "unknown key %d" key
1068 let keyboard ~key ~x ~
y =
1069 match state.outline with
1070 | None
-> viewkeyboard ~key ~x ~
y
1071 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1074 let special ~key ~x ~
y =
1075 match state.outline with
1079 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1080 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1081 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1082 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1083 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1084 | Glut.KEY_HOME
-> addnav (); 0
1087 state.maxy - (if conf.maxhfit
then state.h else 0)
1093 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1094 let maxrows = maxoutlinerows () in
1096 let active = active + incr in
1097 let active = max
0 (min
active (Array.length
outlines - 1)) in
1101 let rows = active - first in
1102 if rows > maxrows then first + incr else first
1105 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1106 Glut.postRedisplay
()
1109 | Glut.KEY_UP
-> navigate ~
-1
1110 | Glut.KEY_DOWN
-> navigate 1
1111 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1112 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1115 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1116 Glut.postRedisplay
()
1119 let active = Array.length
outlines - 1 in
1120 let first = max
0 (active - maxrows) in
1121 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1122 Glut.postRedisplay
()
1127 let drawplaceholder l =
1130 GlDraw.color
(0.2, 0.2, 0.2);
1131 GlDraw.color
(1.0, 1.0, 1.0);
1133 (0.0, float l.pagedispy
)
1134 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1137 and y = float (l.pagedispy
+ 13) in
1138 let font = Glut.BITMAP_8_BY_13
in
1139 GlDraw.color
(0.0, 0.0, 0.0);
1140 GlPix.raster_pos ~
x ~
y ();
1141 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1142 ("Loading " ^ string_of_int
l.pageno);
1145 GlDraw.begins `quads
;
1146 GlDraw.vertex2
(0.0, float l.pagedispy
);
1147 GlDraw.vertex2
(float l.pagew
, float l.pagedispy
);
1148 GlDraw.vertex2
(float l.pagew
, float (l.pagedispy
+ l.pagevh
));
1149 GlDraw.vertex2
(0.0, float (l.pagedispy
+ l.pagevh
));
1154 let now () = Unix.gettimeofday
();;
1157 begin match getopaque l.pageno with
1158 | Some opaque
when validopaque opaque
->
1159 GlDraw.color
(1.0, 1.0, 1.0);
1161 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1164 vlog "draw %f sec" d;
1171 if state.inflight
< cblen state.pagecache
1173 List.iter
preload state.layout;
1176 vlog "inflight %d" state.inflight
;
1179 GlDraw.color
(0.5, 0.5, 0.5);
1182 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1184 l.pagedispy
+ l.pagevh
;
1187 let scrollindicator () =
1188 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1189 GlDraw.color
(0.64 , 0.64, 0.64);
1191 (float (state.w - conf.scrollw
), 0.)
1192 (float state.w, float state.h)
1194 GlDraw.color
(0.0, 0.0, 0.0);
1195 let sh = (float (maxy + state.h) /. float state.h) in
1196 let sh = float state.h /. sh in
1197 let sh = max
sh (float conf.scrollh
) in
1200 if state.y = state.maxy
1202 else float state.y /. float maxy
1204 let position = (float state.h -. sh) *. percent in
1207 if position +. sh > float state.h
1214 (float (state.w - conf.scrollw
), position)
1215 (float state.w, position +. sh)
1220 match state.mstate
with
1224 | Msel
((x0
, y0
), (x1
, y1
)) ->
1226 and y1
= max
y0 y1
in
1229 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1230 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1232 match getopaque l.pageno with
1233 | Some opaque
when validopaque opaque
->
1234 let oy = -l.pagey + l.pagedispy
in
1235 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1238 List.iter
f state.layout
1243 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1244 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1246 (fun (pageno, c, (x0
, y0), (x1
, y1
)) ->
1248 if l.pageno = pageno
1250 let d = float (l.pagedispy
- l.pagey) in
1251 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1252 GlDraw.rect
(x0
, y0 +. d) (x1
, y1
+. d)
1260 let showoutline = function
1262 | Some
(allowdel
, active, first, outlines, qsearch) ->
1264 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1265 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1266 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1269 GlDraw.color
(1., 1., 1.);
1270 let font = Glut.BITMAP_9_BY_15
in
1271 let draw_string x y s =
1272 GlPix.raster_pos ~
x ~
y ();
1273 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1276 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1279 let (s, l, _
, _
) = outlines.(row
) in
1280 let y = (row
- first) * 16 in
1285 GlDraw.polygon_mode `both `line
;
1286 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1287 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1288 GlDraw.rect
(0., float (y + 1))
1289 (float (state.w - conf.scrollw
- 1), float (y + 18));
1290 GlDraw.polygon_mode `both `fill
;
1292 GlDraw.color
(1., 1., 1.);
1294 draw_string (float x) (float (y + 16)) s;
1302 let lasty = List.fold_left
drawpage 0 (state.layout) in
1303 GlDraw.color
(0.5, 0.5, 0.5);
1306 (float (state.w - conf.scrollw
), float state.h)
1311 showoutline state.outline;
1313 Glut.swapBuffers
();
1317 let rec f = function
1319 begin match getopaque l.pageno with
1320 | Some opaque
when validopaque opaque
->
1321 let y = y - l.pagedispy
in
1324 let y = l.pagey + y in
1325 match getlink opaque
x y with
1339 let rec f = function
1341 begin match getopaque l.pageno with
1342 | Some opaque
when validopaque opaque
->
1343 let y = y - l.pagedispy
in
1346 let y = l.pagey + y in
1347 if checklink opaque
x y then true else f rest
1358 let mouse ~button ~bstate ~
x ~
y =
1360 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1368 let incr = incr * 2 in
1369 let y = clamp incr in
1372 | Glut.LEFT_BUTTON
when state.outline = None
->
1373 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1374 begin match dest with
1375 | Some
(pageno, top
) ->
1379 if bstate
= Glut.DOWN
1381 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1382 state.mstate
<- Msel
((x, y), (x, y));
1383 Glut.postRedisplay
()
1386 Glut.setCursor
Glut.CURSOR_INHERIT
;
1387 state.mstate
<- Mnone
;
1394 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1397 if state.outline = None
1399 match state.mstate
with
1402 state.mstate
<- Msel
(a, (x, y));
1403 Glut.postRedisplay
()
1407 if state.outline = None
1409 match state.mstate
with
1410 | Mnone
when (checklink x y) ->
1411 Glut.setCursor
Glut.CURSOR_INFO
1414 Glut.setCursor
Glut.CURSOR_INHERIT
1421 let statepath = (Sys.getenv
"HOME") ^
"/.config/llpp" in
1424 let ic = open_in_bin
statepath in
1425 let hash = input_value
ic in
1431 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1438 match state.fullscreen
with
1439 | None
-> state.w, state.h
1442 if conf.markonquit
then quickbookmark ();
1443 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1444 let oc = open_out_bin
statepath in
1445 output_value
oc pstate
1449 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1454 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1457 state.bookmarks <- statebookmarks;
1458 with Not_found
-> ()
1460 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1463 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1465 if String.length
state.path
= 0
1466 then (prerr_endline
"filename missing"; exit
1)
1471 let _ = Glut.init
Sys.argv
in
1472 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1473 let () = Glut.initWindowSize
state.w state.h in
1474 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1476 let csock, ssock
= Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0 in
1479 state.csock <- csock;
1480 state.ssock
<- ssock
;
1481 writecmd csock ("open " ^
name ^
"\000");
1483 let () = Glut.displayFunc
display in
1484 let () = Glut.reshapeFunc
reshape in
1485 let () = Glut.keyboardFunc
keyboard in
1486 let () = Glut.specialFunc
special in
1487 let () = Glut.idleFunc
(Some
idle) in
1488 let () = Glut.mouseFunc
mouse in
1489 let () = Glut.motionFunc
motion in
1490 let () = Glut.passiveMotionFunc
pmotion in
1494 let rec handlelablglutbug () =
1497 with Glut.BadEnum
"key in special_of_int" ->
1498 showtext '
!'
" LablGlut bug: special key not recognized";
1499 Glut.swapBuffers
();
1500 handlelablglutbug ()
1502 handlelablglutbug ();