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
;;
14 type textentry
= char
* string * (string -> int -> te
) * (string -> unit)
19 | TEswitch
of textentry
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 pagebias
: int
83 ; mutable redispimm
: bool
84 ; mutable verbose
: bool
85 ; mutable scrollincr
: int
86 ; mutable maxhfit
: bool
87 ; mutable crophack
: bool
91 type outline
= string * int * int * int;;
92 type outlines
= Oarray
of outline array
| Olist
of outline list
;;
95 { mutable csock
: Unix.file_descr
96 ; mutable ssock
: Unix.file_descr
100 ; mutable prevy
: int
102 ; mutable layout
: layout list
103 ; pagemap
: ((int * int), string) Hashtbl.t
104 ; mutable pages
: (int * int * int) list
105 ; mutable pagecount
: int
106 ; pagecache
: string circbuf
107 ; navhist
: float circbuf
108 ; mutable inflight
: int
109 ; mutable mstate
: mstate
110 ; mutable searchpattern
: string
111 ; mutable rects
: (int * int * Gl.point2
* Gl.point2
) list
112 ; mutable rects1
: (int * int * Gl.point2
* Gl.point2
) list
113 ; mutable text
: string
114 ; mutable fullscreen
: (int * int) option
115 ; mutable textentry
: textentry
option
116 ; mutable outlines
: outlines
117 ; mutable outline
: (bool * int * int * outline array
* string) option
118 ; mutable bookmarks
: outline list
119 ; mutable path
: string
147 ; pagemap
= Hashtbl.create
10
148 ; pagecache
= cbnew 10 ""
153 ; navhist
= cbnew 100 0.0
160 ; outlines
= Olist
[]
170 Printf.kprintf prerr_endline fmt
172 Printf.kprintf ignore fmt
176 let len = String.length s
in
178 let b = Buffer.create
n in
179 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
180 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
181 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
182 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
183 Buffer.add_string
b s
;
184 let s'
= Buffer.contents
b in
185 let n'
= Unix.write fd
s'
0 n in
186 if n'
!= n then failwith
"write failed";
191 let n = Unix.read fd
s 0 4 in
192 if n != 4 then failwith
"incomplete read(len)";
194 lor (Char.code
s.[0] lsl 24)
195 lor (Char.code
s.[1] lsl 16)
196 lor (Char.code
s.[2] lsl 8)
197 lor (Char.code
s.[3] lsl 0)
199 let s = String.create
len in
200 let n = Unix.read fd
s 0 len in
201 if n != len then failwith
"incomplete read(data)";
206 if y
= state.maxy
then 1.0
207 else float y
/. float state.maxy
211 let b = Buffer.create
10 in
212 Buffer.add_string
b s;
213 let rec combine = function
216 Buffer.add_char
b ' '
;
219 | `
b b -> if b then "1" else "0"
221 | `i i
-> string_of_int i
222 | `f f
-> string_of_float f
223 | `I f
-> string_of_int
(truncate f
)
225 Buffer.add_string
b s;
232 let cmd = Buffer.contents
(makecmd s l
) in
233 writecmd state.csock
cmd;
237 let rec f pn ph fh l
=
239 | (n, _
, h
) :: rest
->
240 let fh = fh + (n - pn
) * ph
in
244 let fh = fh + (ph
* (state.pagecount
- pn
)) in
247 let fh = f 0 0 0 state.pages
in
251 let getpagey pageno
=
252 let rec f pn ph y l
=
254 | (n, _
, h
) :: rest
->
257 y
+ (pageno
- pn
) * ph
259 let y = y + (n - pn
) * ph
in
263 y + (pageno
- pn
) * ph
269 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
270 if pageno
= state.pagecount
|| cacheleft
= 0
273 let ((_
, w
, h
) as curr
), rest
, pdimno
=
275 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
276 curr
, rest
, pdimno
+ 1
280 let pageno'
= pageno + 1 in
314 let accu = e :: accu in
315 f pageno' pdimno curr
316 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
317 (pred cacheleft
) accu
319 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
321 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
322 state.maxy
<- calcheight ();
327 let y = state.y + incr
in
329 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
335 let y = min
state.maxy
y in
336 let pages = layout y state.h
in
338 state.layout <- pages;
341 Glut.postRedisplay
()
346 cbput state.navhist
(yratio state.y);
347 cbrfollowlen state.navhist
;
351 let y = cbget state.navhist
in
352 truncate
(y *. float state.maxy
)
356 let y = getpagey n in
363 let ratio = float w
/. float state.w
in
364 let fixbookmark (s, l
, pageno, pagey
) =
365 let pagey = truncate
(float pagey *. ratio) in
366 (s, l
, pageno, pagey)
368 state.bookmarks
<- List.map
fixbookmark state.bookmarks
;
371 GlDraw.viewport
0 0 w h
;
372 GlMat.mode `modelview
;
373 GlMat.load_identity
();
374 GlMat.mode `projection
;
375 GlMat.load_identity
();
376 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
377 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
378 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
379 GlClear.color
(1., 1., 1.);
380 GlClear.clear
[`color
];
385 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
389 GlDraw.color
(0.0, 0.0, 0.0);
391 (0.0, float (state.h
- 18))
392 (float (state.w
- conf.scrollw
- 1), float state.h
)
394 let font = Glut.BITMAP_8_BY_13
in
395 GlDraw.color
(1.0, 1.0, 1.0);
396 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
397 Glut.bitmapCharacter ~
font ~c
:(Char.code c
);
398 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s;
402 let len = String.length
state.text
in
403 match state.textentry
with
405 if len > 0 then showtext ' '
state.text
407 | Some
(c
, text
, _
, _
) ->
411 text ^
" [" ^
state.text ^
"]"
422 state.outlines
<- Olist
[]
425 state.rects
<- state.rects1
;
426 Glut.postRedisplay
()
429 state.rects
<- state.rects1
;
430 Glut.postRedisplay
()
433 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
434 state.pagecount
<- n;
435 let rely = yratio state.y in
436 let maxy = calcheight () in
437 state.y <- truncate
(float maxy *. rely);
438 let pages = layout state.y state.h
in
439 state.layout <- pages;
440 Glut.postRedisplay
();
443 let s = Scanf.sscanf
cmd "t %n"
444 (fun n -> String.sub
cmd n (String.length
cmd - n))
446 Glut.setWindowTitle
s
449 let s = Scanf.sscanf
cmd "T %n"
450 (fun n -> String.sub
cmd n (String.length
cmd - n))
459 let s = Scanf.sscanf
cmd "V %n"
460 (fun n -> String.sub
cmd n (String.length
cmd - n))
467 let pageno, c
, x0
, y0
, x1
, y1
=
468 Scanf.sscanf
cmd "F %d %d %f %f %f %f"
469 (fun p c x0 y0 x1 y1
-> (p
, c
, x0
, y0
, x1
, y1
))
471 let y = (getpagey pageno) + truncate y0
in
474 state.rects1
<- [pageno, c
, (x0
, y0
), (x1
, y1
)]
477 let pageno, c
, x0
, y0
, x1
, y1
=
478 Scanf.sscanf
cmd "R %d %d %f %f %f %f"
479 (fun pageno c x0 y0 x1 y1
-> (pageno, c
, x0
, y0
, x1
, y1
))
481 state.rects1
<- (pageno, c
, (x0
, y0
), (x1
, y1
)) :: state.rects1
485 Scanf.sscanf
cmd "r %d %d %d %s"
486 (fun n w h p
-> (n, w
, h
, p
))
488 Hashtbl.replace
state.pagemap
(n, w
) p
;
489 let evicted = cbpeekw state.pagecache
in
490 if String.length
evicted > 0
492 wcmd "free" [`
s evicted];
493 let l = Hashtbl.fold
(fun k p a
->
494 if evicted = p
then k
:: a
else a
) state.pagemap
[]
496 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
498 cbput state.pagecache p
;
499 state.inflight
<- pred
state.inflight
;
500 Glut.postRedisplay
()
503 let (n, w
, h
) as pagelayout
=
504 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
506 state.pages <- pagelayout
:: state.pages
510 Scanf.sscanf
cmd "o %d %d %d %n" (fun l n t pos
-> l, n, t
, pos
)
512 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
513 let outline = (s, l, n, t
) in
515 match state.outlines with
516 | Olist
outlines -> Olist
(outline :: outlines)
517 | Oarray _
-> Olist
[outline]
519 state.outlines <- outlines
522 log "unknown cmd `%S'" cmd
525 let getopaque pageno =
526 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
))
527 with Not_found
-> None
530 let cache pageno opaque
=
531 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
) opaque
534 let validopaque opaque
= String.length opaque
> 0;;
537 match getopaque l.pageno with
538 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
539 state.inflight
<- succ
state.inflight
;
541 wcmd "render" [`i
(l.pageno + 1)
550 if not
conf.redispimm
&& state.y != state.prevy
552 state.prevy
<- state.y;
553 Glut.postRedisplay
();
556 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.001 in
560 if conf.preload then begin
562 let y = if state.y < state.h then 0 else state.y - state.h in
563 let pages = layout y (h*3) in
564 List.iter
preload pages;
568 let cmd = readcmd state.csock
in
573 let search pattern forward
=
574 if String.length pattern
> 0
577 match state.layout with
580 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
583 let b = makecmd "search"
584 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
586 Buffer.add_char
b '
,'
;
587 Buffer.add_string
b pattern
;
588 Buffer.add_char
b '
\000'
;
591 writecmd state.csock
cmd;
594 let intentry text key
=
595 let c = Char.unsafe_chr key
in
598 let s = "x" in s.[0] <- c;
599 let text = text ^
s in
603 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
608 let b = Buffer.create
(String.length
s + 1) in
609 Buffer.add_string
b s;
614 let textentry text key
=
615 let c = Char.unsafe_chr key
in
617 | _
when key
>= 32 && key
< 127 ->
618 let text = addchar text c in
622 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
626 let optentry text key
=
627 let btos b = if b then "on" else "off" in
628 let c = Char.unsafe_chr key
in
631 conf.rectsel
<- not
conf.rectsel
;
632 TEdone
("rectsel " ^
(btos conf.rectsel
))
636 try conf.scrollincr
<- int_of_string
s with exc
->
637 state.text <- Printf.sprintf
"bad integer `%s': %s"
638 s (Printexc.to_string exc
)
640 TEswitch
('#'
, "", intentry, ondone)
643 conf.icase
<- not
conf.icase
;
644 TEdone
("case insensitive search " ^
(btos conf.icase
))
647 conf.preload <- not
conf.preload;
648 TEdone
("preload " ^
(btos conf.preload))
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 conf.crophack
<- not
conf.crophack
;
665 TEdone
("crophack " ^
btos conf.crophack
)
668 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
672 let maxoutlinerows () = (state.h - 31) / 16;;
674 let enterselector allowdel
outlines errmsg
=
675 if Array.length
outlines = 0
682 match state.layout with
684 | {pageno=pageno} :: rest
-> pageno
688 if n = Array.length
outlines
691 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
692 if outlinepageno
>= pageno then n else loop (n+1)
697 Some
(allowdel
, active, max
0 (active - maxoutlinerows ()), outlines, "");
698 Glut.postRedisplay
();
701 let enteroutlinemode () =
703 match state.outlines with
706 let a = Array.of_list
(List.rev
l) in
707 state.outlines <- Oarray
a;
710 enterselector false outlines "Documents has no outline";
713 let enterbookmarkmode () =
714 let bookmarks = Array.of_list
state.bookmarks in
715 enterselector true bookmarks "Documents has no bookmarks (yet)";
719 let quickbookmark ?title
() =
720 match state.layout with
726 let sec = Unix.gettimeofday
() in
727 let tm = Unix.localtime
sec in
728 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
732 (tm.Unix.tm_year
+ 1900)
735 | Some
title -> title
738 (title, 0, l.pageno, l.pagey) :: state.bookmarks
741 let viewkeyboard ~key ~x ~
y =
743 state.textentry <- te
;
746 Glut.postRedisplay
()
748 match state.textentry with
750 let c = Char.chr key
in
765 Glut.postRedisplay
()
768 let ondone isforw
s =
769 state.searchpattern
<- s;
772 enttext (Some
(c, "", textentry, ondone (c ='
/'
)))
777 try int_of_string
s with exc
->
778 state.text <- Printf.sprintf
"bad integer `%s': %s"
779 s (Printexc.to_string exc
);
785 state.text <- "page bias is now " ^ string_of_int
n;
788 enttext (Some
('
+'
, "", intentry, ondone))
794 enttext (Some
('
-'
, "", optentry, ondone))
799 try int_of_string
s with exc
->
800 state.text <- Printf.sprintf
"bad integer `%s': %s"
801 s (Printexc.to_string exc
);
808 gotoy (getpagey (n + conf.pagebias
- 1))
811 let pageentry text key
=
812 match Char.unsafe_chr key
with
814 | _
-> intentry text key
816 let text = "x" in text.[0] <- c;
817 enttext (Some
('
:'
, text, pageentry, ondone))
820 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
821 reshape state.w
state.h;
824 begin match state.fullscreen
with
826 state.fullscreen
<- Some
(state.w
, state.h);
829 state.fullscreen
<- None
;
830 Glut.reshapeWindow ~w ~
h
834 search state.searchpattern
true
837 search state.searchpattern
false
840 begin match state.layout with
843 gotoy (state.y - l.pagey);
847 begin match List.rev
state.layout with
850 gotoy (clamp (l.pageh
- l.pagey))
854 begin match state.layout with
857 gotoy (clamp (-l.pageh
));
862 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
864 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
866 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
870 else (100. *. (float state.y /. float maxy)) in
873 Printf.sprintf
"Page %d of %d %.2f%%"
874 (fn+1) state.pagecount
percent
877 "Pages %d-%d of %d %.2f%%"
878 (fn+1) (ln
+1) state.pagecount
percent
884 begin match state.layout with
887 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
888 Glut.postRedisplay
();
896 match state.layout with
898 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
901 enttext (Some
('~'
, "", textentry, ondone))
905 showtext ' '
"Quick bookmark added";
909 begin match state.layout with
911 let a = getpagewh
l.pagedimno
in
915 (truncate
(1.8 *. (a.(1) -. a.(0))),
916 truncate
(1.4 *. (a.(3) -. a.(0))))
918 (truncate
(a.(1) -. a.(0)),
919 truncate
(a.(3) -. a.(0)))
921 Glut.reshapeWindow
(w + conf.scrollw
) h;
922 Glut.postRedisplay
();
928 vlog "huh? %d %c" key
(Char.chr key
);
931 | Some
(c, text, onkey
, ondone) when key
= 8 ->
932 let len = String.length
text in
933 if len = 0 || len = 1
935 state.textentry <- None
;
936 Glut.postRedisplay
();
939 let s = String.sub
text 0 (len - 1) in
940 enttext (Some
(c, s, onkey
, ondone))
943 | Some
(c, text, onkey
, ondone) ->
944 begin match Char.unsafe_chr key
with
947 state.textentry <- None
;
948 Glut.postRedisplay
()
951 state.textentry <- None
;
952 Glut.postRedisplay
()
955 begin match onkey
text key
with
957 state.textentry <- None
;
959 Glut.postRedisplay
()
962 enttext (Some
(c, text, onkey
, ondone));
965 state.textentry <- None
;
966 Glut.postRedisplay
()
969 state.textentry <- Some te
;
970 Glut.postRedisplay
()
975 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
976 let search active pattern incr
=
979 if n = Array.length
outlines || n = -1 then None
else
980 let (s, _
, _
, _
) = outlines.(n) in
982 (try ignore
(Str.search_forward re
s 0); true
983 with Not_found
-> false)
985 let maxrows = (min
(Array.length
outlines) (maxoutlinerows ())) / 2 in
987 then Some
(n, max
0 (n - maxrows))
988 else Some
(n, max first
(n - maxrows))
995 let re = Str.regexp_case_fold pattern
in
1003 if String.length qsearch
= 0
1006 state.outline <- None
;
1007 Glut.postRedisplay
();
1011 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1012 Glut.postRedisplay
();
1016 let incr = if key
= 18 then -1 else 1 in
1018 match search (active + incr) qsearch
incr with
1020 state.text <- qsearch ^
" [not found]";
1023 state.text <- qsearch
;
1026 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1027 Glut.postRedisplay
();
1030 let len = String.length qsearch
in
1037 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1040 let qsearch = String.sub
qsearch 0 (len - 1) in
1041 state.text <- qsearch;
1042 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1044 Glut.postRedisplay
()
1047 if active < Array.length
outlines
1049 let (_
, _
, n, t
) = outlines.(active) in
1053 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1054 state.outline <- None
;
1055 Glut.postRedisplay
();
1057 | _
when key
>= 32 && key
< 127 ->
1058 let pattern = addchar qsearch (Char.chr key
) in
1060 match search active pattern 1 with
1062 state.text <- pattern ^
" [not found]";
1064 | Some
(active, first
) ->
1065 state.text <- pattern;
1068 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1069 Glut.postRedisplay
()
1071 | 127 when allowdel
->
1072 let len = Array.length
outlines - 1 in
1075 state.outline <- None
;
1076 state.bookmarks <- [];
1079 let bookmarks = Array.init
len
1081 let i = if i >= active then i + 1 else i in
1092 Glut.postRedisplay
()
1094 | _
-> log "unknown key %d" key
1097 let keyboard ~key ~x ~
y =
1098 match state.outline with
1099 | None
-> viewkeyboard ~key ~x ~
y
1100 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1103 let special ~key ~x ~
y =
1104 match state.outline with
1108 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1109 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1110 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1111 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1112 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1113 | Glut.KEY_HOME
-> addnav (); 0
1116 state.maxy - (if conf.maxhfit
then state.h else 0)
1122 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1123 let maxrows = maxoutlinerows () in
1125 let active = active + incr in
1126 let active = max
0 (min
active (Array.length
outlines - 1)) in
1130 let rows = active - first in
1131 if rows > maxrows then first + incr else first
1134 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1135 Glut.postRedisplay
()
1138 | Glut.KEY_UP
-> navigate ~
-1
1139 | Glut.KEY_DOWN
-> navigate 1
1140 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1141 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1144 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1145 Glut.postRedisplay
()
1148 let active = Array.length
outlines - 1 in
1149 let first = max
0 (active - maxrows) in
1150 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1151 Glut.postRedisplay
()
1156 let drawplaceholder l =
1159 GlDraw.color
(0.2, 0.2, 0.2);
1160 GlDraw.color
(1.0, 1.0, 1.0);
1162 (0.0, float l.pagedispy
)
1163 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1166 and y = float (l.pagedispy
+ 13) in
1167 let font = Glut.BITMAP_8_BY_13
in
1168 GlDraw.color
(0.0, 0.0, 0.0);
1169 GlPix.raster_pos ~
x ~
y ();
1170 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1171 ("Loading " ^ string_of_int
l.pageno);
1174 GlDraw.begins `quads
;
1175 GlDraw.vertex2
(0.0, float l.pagedispy
);
1176 GlDraw.vertex2
(float l.pagew
, float l.pagedispy
);
1177 GlDraw.vertex2
(float l.pagew
, float (l.pagedispy
+ l.pagevh
));
1178 GlDraw.vertex2
(0.0, float (l.pagedispy
+ l.pagevh
));
1183 let now () = Unix.gettimeofday
();;
1186 begin match getopaque l.pageno with
1187 | Some opaque
when validopaque opaque
->
1188 if state.textentry = None
1189 then GlDraw.color
(1.0, 1.0, 1.0)
1190 else GlDraw.color
(0.4, 0.4, 0.4);
1192 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1195 vlog "draw %f sec" d;
1202 if state.inflight
< cblen state.pagecache
1204 List.iter
preload state.layout;
1207 vlog "inflight %d" state.inflight
;
1210 GlDraw.color
(0.5, 0.5, 0.5);
1213 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1215 l.pagedispy
+ l.pagevh
;
1218 let scrollindicator () =
1219 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1220 GlDraw.color
(0.64 , 0.64, 0.64);
1222 (float (state.w - conf.scrollw
), 0.)
1223 (float state.w, float state.h)
1225 GlDraw.color
(0.0, 0.0, 0.0);
1226 let sh = (float (maxy + state.h) /. float state.h) in
1227 let sh = float state.h /. sh in
1228 let sh = max
sh (float conf.scrollh
) in
1231 if state.y = state.maxy
1233 else float state.y /. float maxy
1235 let position = (float state.h -. sh) *. percent in
1238 if position +. sh > float state.h
1245 (float (state.w - conf.scrollw
), position)
1246 (float state.w, position +. sh)
1251 match state.mstate
with
1255 | Msel
((x0
, y0
), (x1
, y1
)) ->
1257 and y1
= max
y0 y1
in
1260 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1261 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1263 match getopaque l.pageno with
1264 | Some opaque
when validopaque opaque
->
1265 let oy = -l.pagey + l.pagedispy
in
1266 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1269 List.iter
f state.layout
1274 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1275 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1277 (fun (pageno, c, (x0
, y0), (x1
, y1
)) ->
1279 if l.pageno = pageno
1281 let d = float (l.pagedispy
- l.pagey) in
1282 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1283 GlDraw.rect
(x0
, y0 +. d) (x1
, y1
+. d)
1291 let showoutline = function
1293 | Some
(allowdel
, active, first, outlines, qsearch) ->
1295 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1296 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1297 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1300 GlDraw.color
(1., 1., 1.);
1301 let font = Glut.BITMAP_9_BY_15
in
1302 let draw_string x y s =
1303 GlPix.raster_pos ~
x ~
y ();
1304 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1307 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1310 let (s, l, _
, _
) = outlines.(row
) in
1311 let y = (row
- first) * 16 in
1316 GlDraw.polygon_mode `both `line
;
1317 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1318 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1319 GlDraw.rect
(0., float (y + 1))
1320 (float (state.w - conf.scrollw
- 1), float (y + 18));
1321 GlDraw.polygon_mode `both `fill
;
1323 GlDraw.color
(1., 1., 1.);
1325 draw_string (float x) (float (y + 16)) s;
1333 let lasty = List.fold_left
drawpage 0 (state.layout) in
1334 GlDraw.color
(0.5, 0.5, 0.5);
1337 (float (state.w - conf.scrollw
), float state.h)
1342 showoutline state.outline;
1344 Glut.swapBuffers
();
1348 let rec f = function
1350 begin match getopaque l.pageno with
1351 | Some opaque
when validopaque opaque
->
1352 let y = y - l.pagedispy
in
1355 let y = l.pagey + y in
1356 match getlink opaque
x y with
1370 let rec f = function
1372 begin match getopaque l.pageno with
1373 | Some opaque
when validopaque opaque
->
1374 let y = y - l.pagedispy
in
1377 let y = l.pagey + y in
1378 if checklink opaque
x y then true else f rest
1389 let mouse ~button ~bstate ~
x ~
y =
1391 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1399 let incr = incr * 2 in
1400 let y = clamp incr in
1403 | Glut.LEFT_BUTTON
when state.outline = None
->
1404 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1405 begin match dest with
1406 | Some
(pageno, top
) ->
1410 if bstate
= Glut.DOWN
1412 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1413 state.mstate
<- Msel
((x, y), (x, y));
1414 Glut.postRedisplay
()
1417 Glut.setCursor
Glut.CURSOR_INHERIT
;
1418 state.mstate
<- Mnone
;
1425 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1428 if state.outline = None
1430 match state.mstate
with
1433 state.mstate
<- Msel
(a, (x, y));
1434 Glut.postRedisplay
()
1438 if state.outline = None
1440 match state.mstate
with
1441 | Mnone
when (checklink x y) ->
1442 Glut.setCursor
Glut.CURSOR_INFO
1445 Glut.setCursor
Glut.CURSOR_INHERIT
1454 if Sys.os_type
= "Win32"
1456 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1458 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1460 Filename.concat
home "llpp"
1464 let ic = open_in_bin
statepath in
1465 let hash = input_value
ic in
1471 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1478 match state.fullscreen
with
1479 | None
-> state.w, state.h
1482 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1483 let oc = open_out_bin
statepath in
1484 output_value
oc pstate
1488 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1493 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1496 state.bookmarks <- statebookmarks;
1497 with Not_found
-> ()
1499 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1502 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1504 if String.length
state.path
= 0
1505 then (prerr_endline
"filename missing"; exit
1)
1510 let _ = Glut.init
Sys.argv
in
1511 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1512 let () = Glut.initWindowSize
state.w state.h in
1513 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1516 if Sys.os_type
= "Unix"
1518 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1520 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1521 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1522 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1523 Unix.bind
sock addr;
1525 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1526 Unix.connect
csock addr;
1527 let ssock, _ = Unix.accept
sock in
1530 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1531 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1535 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1539 let () = Glut.displayFunc
display in
1540 let () = Glut.reshapeFunc
reshape in
1541 let () = Glut.keyboardFunc
keyboard in
1542 let () = Glut.specialFunc
special in
1543 let () = Glut.idleFunc
(Some
idle) in
1544 let () = Glut.mouseFunc
mouse in
1545 let () = Glut.motionFunc
motion in
1546 let () = Glut.passiveMotionFunc
pmotion in
1549 state.csock <- csock;
1550 state.ssock <- ssock;
1551 writecmd csock ("open " ^
name ^
"\000");
1555 let rec handlelablglutbug () =
1558 with Glut.BadEnum
"key in special_of_int" ->
1559 showtext '
!'
" LablGlut bug: special key not recognized";
1560 Glut.swapBuffers
();
1561 handlelablglutbug ()
1563 handlelablglutbug ();