1 let log fmt
= Printf.kprintf prerr_endline fmt
;;
2 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
4 external init
: Unix.file_descr
-> unit = "ml_init";;
5 external draw
: int -> int -> int -> int -> string -> unit = "ml_draw";;
6 external gettext
: string -> (int * int * int * int) -> int -> bool -> unit =
8 external checklink
: string -> int -> int -> bool = "ml_checklink";;
9 external getlink
: string -> int -> int -> (int * int) option = "ml_getlink";;
10 external getpagewh
: int -> float array
= "ml_getpagewh";;
12 type mstate
= Msel
of ((int * int) * (int * int)) | Mnone
;;
29 { store
= Array.create n v
36 let cblen b
= Array.length b
.store
;;
41 b
.wc
<- (b
.wc
+ 1) mod len;
42 b
.len <- (b
.len + 1) mod len;
45 let cbpeekw b
= b
.store
.(b
.wc
);;
48 let v = b
.store
.(b
.rc
) in
53 let rc = if b
.rc = 0 then b
.len - 1 else b
.rc - 1 in
75 { mutable scrollw
: int
76 ; mutable scrollh
: int
77 ; mutable rectsel
: bool
78 ; mutable icase
: bool
79 ; mutable preload
: bool
80 ; mutable pagebias
: int
81 ; mutable redispimm
: bool
82 ; mutable verbose
: bool
83 ; mutable scrollincr
: int
84 ; mutable maxhfit
: bool
85 ; mutable crophack
: bool
89 type outline
= string * int * int * int;;
90 type outlines
= Oarray
of outline array
| Olist
of outline list
;;
93 { mutable csock
: Unix.file_descr
94 ; mutable ssock
: Unix.file_descr
100 ; mutable layout
: layout list
101 ; pagemap
: ((int * int), string) Hashtbl.t
102 ; mutable pages
: (int * int * int) list
103 ; mutable pagecount
: int
104 ; pagecache
: string circbuf
105 ; navhist
: float circbuf
106 ; mutable inflight
: int
107 ; mutable mstate
: mstate
108 ; mutable searchpattern
: string
109 ; mutable rects
: (int * int * Gl.point2
* Gl.point2
) list
110 ; mutable rects1
: (int * int * Gl.point2
* Gl.point2
) list
111 ; mutable text
: string
112 ; mutable fullscreen
: (int * int) option
113 ; mutable textentry
:
114 (char
* string * (string -> int -> te
) * (string -> unit)) option
115 ; mutable outlines
: outlines
116 ; mutable outline
: (bool * int * int * outline array
* string) option
117 ; mutable bookmarks
: outline list
118 ; mutable path
: string
146 ; pagemap
= Hashtbl.create
10
147 ; pagecache
= cbnew 10 ""
152 ; navhist
= cbnew 100 0.0
159 ; outlines
= Olist
[]
169 Printf.kprintf prerr_endline fmt
171 Printf.kprintf ignore fmt
175 let len = String.length s
in
177 let b = Buffer.create
n in
178 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
179 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
180 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
181 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
182 Buffer.add_string
b s
;
183 let s'
= Buffer.contents
b in
184 let n'
= Unix.write fd
s'
0 n in
185 if n'
!= n then failwith
"write failed";
190 let n = Unix.read fd
s 0 4 in
191 if n != 4 then failwith
"incomplete read(len)";
193 lor (Char.code
s.[0] lsl 24)
194 lor (Char.code
s.[1] lsl 16)
195 lor (Char.code
s.[2] lsl 8)
196 lor (Char.code
s.[3] lsl 0)
198 let s = String.create
len in
199 let n = Unix.read fd
s 0 len in
200 if n != len then failwith
"incomplete read(data)";
205 if y
= state.maxy
then 1.0
206 else float y
/. float state.maxy
210 let b = Buffer.create
10 in
211 Buffer.add_string
b s;
212 let rec combine = function
215 Buffer.add_char
b ' '
;
218 | `
b b -> if b then "1" else "0"
220 | `i i
-> string_of_int i
221 | `f f
-> string_of_float f
222 | `I f
-> string_of_int
(truncate f
)
224 Buffer.add_string
b s;
231 let cmd = Buffer.contents
(makecmd s l
) in
232 writecmd state.csock
cmd;
236 let rec f pn ph fh l
=
238 | (n, _
, h
) :: rest
->
239 let fh = fh + (n - pn
) * ph
in
243 let fh = fh + (ph
* (state.pagecount
- pn
)) in
246 let fh = f 0 0 0 state.pages
in
250 let getpagey pageno
=
251 let rec f pn ph y l
=
253 | (n, _
, h
) :: rest
->
256 y
+ (pageno
- pn
) * ph
258 let y = y + (n - pn
) * ph
in
262 y + (pageno
- pn
) * ph
268 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
269 if pageno
= state.pagecount
|| cacheleft
= 0
272 let ((_
, w
, h
) as curr
), rest
, pdimno
=
274 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
275 curr
, rest
, pdimno
+ 1
279 let pageno'
= pageno + 1 in
313 let accu = e :: accu in
314 f pageno' pdimno curr
315 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
316 (pred cacheleft
) accu
318 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
320 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
321 state.maxy
<- calcheight ();
326 let y = state.y + incr
in
328 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
334 let y = min
state.maxy
y in
335 let pages = layout y state.h
in
337 state.layout <- pages;
340 Glut.postRedisplay
()
345 cbput state.navhist
(yratio state.y);
346 cbrfollowlen state.navhist
;
350 let y = cbget state.navhist
in
351 truncate
(y *. float state.maxy
)
355 let y = getpagey n in
362 let ratio = float w
/. float state.w
in
363 let fixbookmark (s, l
, pageno, pagey
) =
364 let pagey = truncate
(float pagey *. ratio) in
365 (s, l
, pageno, pagey)
367 state.bookmarks
<- List.map
fixbookmark state.bookmarks
;
370 GlDraw.viewport
0 0 w h
;
371 GlMat.mode `modelview
;
372 GlMat.load_identity
();
373 GlMat.mode `projection
;
374 GlMat.load_identity
();
375 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
376 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
377 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
378 GlClear.color
(1., 1., 1.);
379 GlClear.clear
[`color
];
384 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
388 GlDraw.color
(0.0, 0.0, 0.0);
390 (0.0, float (state.h
- 18))
391 (float (state.w
- conf.scrollw
- 1), float state.h
)
393 let font = Glut.BITMAP_8_BY_13
in
394 GlDraw.color
(1.0, 1.0, 1.0);
395 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
396 Glut.bitmapCharacter ~
font ~c
:(Char.code c
);
397 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s;
401 let len = String.length
state.text
in
402 match state.textentry
with
404 if len > 0 then showtext ' '
state.text
406 | Some
(c
, text
, _
, _
) ->
410 text ^
" [" ^
state.text ^
"]"
421 state.outlines
<- Olist
[]
424 state.rects
<- state.rects1
;
425 Glut.postRedisplay
()
428 state.rects
<- state.rects1
;
429 Glut.postRedisplay
()
432 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
433 state.pagecount
<- n;
434 let rely = yratio state.y in
435 let maxy = calcheight () in
436 state.y <- truncate
(float maxy *. rely);
437 let pages = layout state.y state.h
in
438 state.layout <- pages;
439 Glut.postRedisplay
();
442 let s = Scanf.sscanf
cmd "t %n"
443 (fun n -> String.sub
cmd n (String.length
cmd - n))
445 Glut.setWindowTitle
s
448 let s = Scanf.sscanf
cmd "T %n"
449 (fun n -> String.sub
cmd n (String.length
cmd - n))
458 let s = Scanf.sscanf
cmd "V %n"
459 (fun n -> String.sub
cmd n (String.length
cmd - n))
466 let pageno, c
, x0
, y0
, x1
, y1
=
467 Scanf.sscanf
cmd "F %d %d %f %f %f %f"
468 (fun p c x0 y0 x1 y1
-> (p
, c
, x0
, y0
, x1
, y1
))
470 let y = (getpagey pageno) + truncate y0
in
473 state.rects1
<- [pageno, c
, (x0
, y0
), (x1
, y1
)]
476 let pageno, c
, x0
, y0
, x1
, y1
=
477 Scanf.sscanf
cmd "R %d %d %f %f %f %f"
478 (fun pageno c x0 y0 x1 y1
-> (pageno, c
, x0
, y0
, x1
, y1
))
480 state.rects1
<- (pageno, c
, (x0
, y0
), (x1
, y1
)) :: state.rects1
484 Scanf.sscanf
cmd "r %d %d %d %s"
485 (fun n w h p
-> (n, w
, h
, p
))
487 Hashtbl.replace
state.pagemap
(n, w
) p
;
488 let evicted = cbpeekw state.pagecache
in
489 if String.length
evicted > 0
491 wcmd "free" [`
s evicted];
492 let l = Hashtbl.fold
(fun k p a
->
493 if evicted = p
then k
:: a
else a
) state.pagemap
[]
495 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
497 cbput state.pagecache p
;
498 state.inflight
<- pred
state.inflight
;
499 Glut.postRedisplay
()
502 let (n, w
, h
) as pagelayout
=
503 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
505 state.pages <- pagelayout
:: state.pages
509 Scanf.sscanf
cmd "o %d %d %d %n" (fun l n t pos
-> l, n, t
, pos
)
511 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
512 let outline = (s, l, n, t
) in
514 match state.outlines with
515 | Olist
outlines -> Olist
(outline :: outlines)
516 | Oarray _
-> Olist
[outline]
518 state.outlines <- outlines
521 log "unknown cmd `%S'" cmd
524 let getopaque pageno =
525 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
))
526 with Not_found
-> None
529 let cache pageno opaque
=
530 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
) opaque
533 let validopaque opaque
= String.length opaque
> 0;;
536 match getopaque l.pageno with
537 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
538 state.inflight
<- succ
state.inflight
;
540 wcmd "render" [`i
(l.pageno + 1)
549 if not
conf.redispimm
&& state.y != state.prevy
551 state.prevy
<- state.y;
552 Glut.postRedisplay
();
555 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.001 in
559 if conf.preload then begin
561 let y = if state.y < state.h then 0 else state.y - state.h in
562 let pages = layout y (h*3) in
563 List.iter
preload pages;
567 let cmd = readcmd state.csock
in
572 let search pattern forward
=
573 if String.length pattern
> 0
576 match state.layout with
579 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
582 let b = makecmd "search"
583 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
585 Buffer.add_char
b '
,'
;
586 Buffer.add_string
b pattern
;
587 Buffer.add_char
b '
\000'
;
590 writecmd state.csock
cmd;
593 let intentry text key
=
594 let c = Char.unsafe_chr key
in
597 let s = "x" in s.[0] <- c;
598 let text = text ^
s in
602 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
607 let b = Buffer.create
(String.length
s + 1) in
608 Buffer.add_string
b s;
613 let textentry text key
=
614 let c = Char.unsafe_chr key
in
616 | _
when key
>= 32 && key
< 127 ->
617 let text = addchar text c in
621 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
625 let optentry text key
=
626 let btos b = if b then "on" else "off" in
627 let c = Char.unsafe_chr key
in
630 conf.rectsel
<- not
conf.rectsel
;
631 TEdone
("rectsel " ^
(btos conf.rectsel
))
634 conf.icase
<- not
conf.icase
;
635 TEdone
("case insensitive search " ^
(btos conf.icase
))
638 conf.preload <- not
conf.preload;
639 TEdone
("preload " ^
(btos conf.preload))
642 conf.redispimm
<- not
conf.redispimm
;
643 TEdone
("immediate redisplay " ^
(btos conf.redispimm
))
646 conf.verbose
<- not
conf.verbose
;
647 TEdone
("verbose " ^
(btos conf.verbose
))
650 conf.maxhfit
<- not
conf.maxhfit
;
651 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
652 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
655 conf.crophack
<- not
conf.crophack
;
656 TEdone
("crophack " ^
btos conf.crophack
)
659 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
663 let maxoutlinerows () = (state.h - 31) / 16;;
665 let enterselector allowdel
outlines errmsg
=
666 if Array.length
outlines = 0
673 match state.layout with
675 | {pageno=pageno} :: rest
-> pageno
679 if n = Array.length
outlines
682 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
683 if outlinepageno
>= pageno then n else loop (n+1)
688 Some
(allowdel
, active, max
0 (active - maxoutlinerows ()), outlines, "");
689 Glut.postRedisplay
();
692 let enteroutlinemode () =
694 match state.outlines with
697 let a = Array.of_list
(List.rev
l) in
698 state.outlines <- Oarray
a;
701 enterselector false outlines "Documents has no outline";
704 let enterbookmarkmode () =
705 let bookmarks = Array.of_list
state.bookmarks in
706 enterselector true bookmarks "Documents has no bookmarks (yet)";
710 let quickbookmark ?title
() =
711 match state.layout with
717 let sec = Unix.gettimeofday
() in
718 let tm = Unix.localtime
sec in
719 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
723 (tm.Unix.tm_year
+ 1900)
726 | Some
title -> title
729 (title, 0, l.pageno, l.pagey) :: state.bookmarks
732 let viewkeyboard ~key ~x ~
y =
734 state.textentry <- te
;
739 match state.textentry with
741 let c = Char.chr key
in
756 Glut.postRedisplay
()
759 let ondone isforw
s =
760 state.searchpattern
<- s;
763 enttext (Some
(c, "", textentry, ondone (c ='
/'
)))
768 try int_of_string
s with exc
->
769 state.text <- Printf.sprintf
"bad integer `%s': %s"
770 s (Printexc.to_string exc
);
776 state.text <- "page bias is now " ^ string_of_int
n;
779 enttext (Some
('
+'
, "", intentry, ondone))
785 enttext (Some
('
-'
, "", optentry, ondone))
790 try int_of_string
s with exc
->
791 state.text <- Printf.sprintf
"bad integer `%s': %s"
792 s (Printexc.to_string exc
);
799 gotoy (getpagey (n + conf.pagebias
- 1))
802 let pageentry text key
=
803 match Char.unsafe_chr key
with
805 | _
-> intentry text key
807 let text = "x" in text.[0] <- c;
808 enttext (Some
('
:'
, text, pageentry, ondone))
811 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
812 reshape state.w
state.h;
815 begin match state.fullscreen
with
817 state.fullscreen
<- Some
(state.w
, state.h);
820 state.fullscreen
<- None
;
821 Glut.reshapeWindow ~w ~
h
825 search state.searchpattern
true
828 search state.searchpattern
false
831 begin match state.layout with
834 gotoy (state.y - l.pagey);
838 begin match List.rev
state.layout with
841 gotoy (clamp (l.pageh
- l.pagey))
845 begin match state.layout with
848 gotoy (clamp (-l.pageh
));
853 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
855 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
857 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
858 let percent = (100. *. (float state.y /. float maxy)) in
861 Printf.sprintf
"Page %d of %d %.2f%%"
862 (fn+1) state.pagecount
percent
865 "Pages %d-%d of %d %.2f%%"
866 (fn+1) (ln
+1) state.pagecount
percent
872 begin match state.layout with
875 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
876 Glut.postRedisplay
();
884 match state.layout with
886 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
889 enttext (Some
('~'
, "", textentry, ondone))
893 showtext ' '
"Quick bookmark added";
897 begin match state.layout with
899 let a = getpagewh
l.pagedimno
in
903 (truncate
(1.8 *. (a.(1) -. a.(0))),
904 truncate
(1.4 *. (a.(3) -. a.(0))))
906 (truncate
(a.(1) -. a.(0)),
907 truncate
(a.(3) -. a.(0)))
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
=
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 = (min
(Array.length
outlines) (maxoutlinerows ())) / 2 in
971 then Some
(n, max
0 (n - maxrows))
972 else Some
(n, max first
(n - maxrows))
979 let re = Str.regexp_case_fold pattern
in
987 if String.length qsearch
= 0
990 state.outline <- None
;
991 Glut.postRedisplay
();
995 state.outline <- Some
(allowdel
, active, first
, outlines, "");
996 Glut.postRedisplay
();
1000 let incr = if key
= 18 then -1 else 1 in
1002 match search (active + incr) qsearch
incr with
1004 state.text <- qsearch ^
" [not found]";
1007 state.text <- qsearch
;
1010 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1011 Glut.postRedisplay
();
1014 let len = String.length qsearch
in
1021 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1024 let qsearch = String.sub
qsearch 0 (len - 1) in
1025 state.text <- qsearch;
1026 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1028 Glut.postRedisplay
()
1031 if active < Array.length
outlines
1033 let (_
, _
, n, t
) = outlines.(active) in
1037 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1038 state.outline <- None
;
1039 Glut.postRedisplay
();
1041 | _
when key
>= 32 && key
< 127 ->
1042 let pattern = addchar qsearch (Char.chr key
) in
1044 match search active pattern 1 with
1046 state.text <- pattern ^
" [not found]";
1048 | Some
(active, first
) ->
1049 state.text <- pattern;
1052 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1053 Glut.postRedisplay
()
1055 | 127 when allowdel
->
1056 let len = Array.length
outlines - 1 in
1059 state.outline <- None
;
1060 state.bookmarks <- [];
1063 let bookmarks = Array.init
len
1065 let i = if i >= active then i + 1 else i in
1076 Glut.postRedisplay
()
1078 | _
-> log "unknown key %d" key
1081 let keyboard ~key ~x ~
y =
1082 match state.outline with
1083 | None
-> viewkeyboard ~key ~x ~
y
1084 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1087 let special ~key ~x ~
y =
1088 match state.outline with
1092 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1093 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1094 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1095 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1096 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1097 | Glut.KEY_HOME
-> addnav (); 0
1100 state.maxy - (if conf.maxhfit
then state.h else 0)
1106 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1107 let maxrows = maxoutlinerows () in
1109 let active = active + incr in
1110 let active = max
0 (min
active (Array.length
outlines - 1)) in
1114 let rows = active - first in
1115 if rows > maxrows then first + incr else first
1118 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1119 Glut.postRedisplay
()
1122 | Glut.KEY_UP
-> navigate ~
-1
1123 | Glut.KEY_DOWN
-> navigate 1
1124 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1125 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1128 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1129 Glut.postRedisplay
()
1132 let active = Array.length
outlines - 1 in
1133 let first = max
0 (active - maxrows) in
1134 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1135 Glut.postRedisplay
()
1140 let drawplaceholder l =
1143 GlDraw.color
(0.2, 0.2, 0.2);
1144 GlDraw.color
(1.0, 1.0, 1.0);
1146 (0.0, float l.pagedispy
)
1147 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1150 and y = float (l.pagedispy
+ 13) in
1151 let font = Glut.BITMAP_8_BY_13
in
1152 GlDraw.color
(0.0, 0.0, 0.0);
1153 GlPix.raster_pos ~
x ~
y ();
1154 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1155 ("Loading " ^ string_of_int
l.pageno);
1158 GlDraw.begins `quads
;
1159 GlDraw.vertex2
(0.0, float l.pagedispy
);
1160 GlDraw.vertex2
(float l.pagew
, float l.pagedispy
);
1161 GlDraw.vertex2
(float l.pagew
, float (l.pagedispy
+ l.pagevh
));
1162 GlDraw.vertex2
(0.0, float (l.pagedispy
+ l.pagevh
));
1167 let now () = Unix.gettimeofday
();;
1170 begin match getopaque l.pageno with
1171 | Some opaque
when validopaque opaque
->
1172 GlDraw.color
(1.0, 1.0, 1.0);
1174 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1177 vlog "draw %f sec" d;
1184 if state.inflight
< cblen state.pagecache
1186 List.iter
preload state.layout;
1189 vlog "inflight %d" state.inflight
;
1192 GlDraw.color
(0.5, 0.5, 0.5);
1195 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1197 l.pagedispy
+ l.pagevh
;
1200 let scrollindicator () =
1201 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1202 GlDraw.color
(0.64 , 0.64, 0.64);
1204 (float (state.w - conf.scrollw
), 0.)
1205 (float state.w, float state.h)
1207 GlDraw.color
(0.0, 0.0, 0.0);
1208 let sh = (float (maxy + state.h) /. float state.h) in
1209 let sh = float state.h /. sh in
1210 let sh = max
sh (float conf.scrollh
) in
1213 if state.y = state.maxy
1215 else float state.y /. float maxy
1217 let position = (float state.h -. sh) *. percent in
1220 if position +. sh > float state.h
1227 (float (state.w - conf.scrollw
), position)
1228 (float state.w, position +. sh)
1233 match state.mstate
with
1237 | Msel
((x0
, y0
), (x1
, y1
)) ->
1239 and y1
= max
y0 y1
in
1242 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1243 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1245 match getopaque l.pageno with
1246 | Some opaque
when validopaque opaque
->
1247 let oy = -l.pagey + l.pagedispy
in
1248 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1251 List.iter
f state.layout
1256 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1257 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1259 (fun (pageno, c, (x0
, y0), (x1
, y1
)) ->
1261 if l.pageno = pageno
1263 let d = float (l.pagedispy
- l.pagey) in
1264 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1265 GlDraw.rect
(x0
, y0 +. d) (x1
, y1
+. d)
1273 let showoutline = function
1275 | Some
(allowdel
, active, first, outlines, qsearch) ->
1277 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1278 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1279 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1282 GlDraw.color
(1., 1., 1.);
1283 let font = Glut.BITMAP_9_BY_15
in
1284 let draw_string x y s =
1285 GlPix.raster_pos ~
x ~
y ();
1286 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1289 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1292 let (s, l, _
, _
) = outlines.(row
) in
1293 let y = (row
- first) * 16 in
1298 GlDraw.polygon_mode `both `line
;
1299 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1300 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1301 GlDraw.rect
(0., float (y + 1))
1302 (float (state.w - conf.scrollw
- 1), float (y + 18));
1303 GlDraw.polygon_mode `both `fill
;
1305 GlDraw.color
(1., 1., 1.);
1307 draw_string (float x) (float (y + 16)) s;
1315 let lasty = List.fold_left
drawpage 0 (state.layout) in
1316 GlDraw.color
(0.5, 0.5, 0.5);
1319 (float (state.w - conf.scrollw
), float state.h)
1324 showoutline state.outline;
1326 Glut.swapBuffers
();
1330 let rec f = function
1332 begin match getopaque l.pageno with
1333 | Some opaque
when validopaque opaque
->
1334 let y = y - l.pagedispy
in
1337 let y = l.pagey + y in
1338 match getlink opaque
x y with
1352 let rec f = function
1354 begin match getopaque l.pageno with
1355 | Some opaque
when validopaque opaque
->
1356 let y = y - l.pagedispy
in
1359 let y = l.pagey + y in
1360 if checklink opaque
x y then true else f rest
1371 let mouse ~button ~bstate ~
x ~
y =
1373 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1381 let incr = incr * 2 in
1382 let y = clamp incr in
1385 | Glut.LEFT_BUTTON
when state.outline = None
->
1386 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1387 begin match dest with
1388 | Some
(pageno, top
) ->
1392 if bstate
= Glut.DOWN
1394 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1395 state.mstate
<- Msel
((x, y), (x, y));
1396 Glut.postRedisplay
()
1399 Glut.setCursor
Glut.CURSOR_INHERIT
;
1400 state.mstate
<- Mnone
;
1407 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1410 if state.outline = None
1412 match state.mstate
with
1415 state.mstate
<- Msel
(a, (x, y));
1416 Glut.postRedisplay
()
1420 if state.outline = None
1422 match state.mstate
with
1423 | Mnone
when (checklink x y) ->
1424 Glut.setCursor
Glut.CURSOR_INFO
1427 Glut.setCursor
Glut.CURSOR_INHERIT
1436 if Sys.os_type
= "Win32"
1438 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1440 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1442 Filename.concat
home "llpp"
1446 let ic = open_in_bin
statepath in
1447 let hash = input_value
ic in
1453 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1460 match state.fullscreen
with
1461 | None
-> state.w, state.h
1464 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1465 let oc = open_out_bin
statepath in
1466 output_value
oc pstate
1470 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1475 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1478 state.bookmarks <- statebookmarks;
1479 with Not_found
-> ()
1481 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1484 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1486 if String.length
state.path
= 0
1487 then (prerr_endline
"filename missing"; exit
1)
1492 let _ = Glut.init
Sys.argv
in
1493 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1494 let () = Glut.initWindowSize
state.w state.h in
1495 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1498 if Sys.os_type
= "Unix"
1500 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1502 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1503 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1504 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1505 Unix.bind
sock addr;
1507 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1508 Unix.connect
csock addr;
1509 let ssock, _ = Unix.accept
sock in
1512 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1513 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1517 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1521 let () = Glut.displayFunc
display in
1522 let () = Glut.reshapeFunc
reshape in
1523 let () = Glut.keyboardFunc
keyboard in
1524 let () = Glut.specialFunc
special in
1525 let () = Glut.idleFunc
(Some
idle) in
1526 let () = Glut.mouseFunc
mouse in
1527 let () = Glut.motionFunc
motion in
1528 let () = Glut.passiveMotionFunc
pmotion in
1531 state.csock <- csock;
1532 state.ssock <- ssock;
1533 writecmd csock ("open " ^
name ^
"\000");
1537 let rec handlelablglutbug () =
1540 with Glut.BadEnum
"key in special_of_int" ->
1541 showtext '
!'
" LablGlut bug: special key not recognized";
1542 Glut.swapBuffers
();
1543 handlelablglutbug ()
1545 handlelablglutbug ();