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";;
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
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 text
: string
113 ; mutable fullscreen
: (int * int) option
114 ; mutable textentry
:
115 (char
* string * (string -> int -> te
) * (string -> unit)) option
116 ; mutable outlines
: outlines
117 ; mutable outline
: (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
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 if not
conf.titletext
390 GlDraw.color
(0.0, 0.0, 0.0);
392 (0.0, float (state.h
- 18))
393 (float (state.w
- conf.scrollw
- 1), float state.h
)
395 let font = Glut.BITMAP_8_BY_13
in
396 GlDraw.color
(1.0, 1.0, 1.0);
397 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
398 Glut.bitmapCharacter ~
font ~c
:(Char.code c
);
399 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s
402 let len = String.length
s in
403 let dst = String.create
(len + 1) in
412 Glut.setWindowTitle ~title
:dst
416 let len = String.length
state.text
in
417 match state.textentry
with
419 if len > 0 then showtext ' '
state.text
421 | Some
(c
, text
, _
, _
) ->
425 text ^
" [" ^
state.text ^
"]"
438 Glut.postRedisplay
()
441 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
442 state.pagecount
<- n;
443 let rely = yratio state.y in
444 let maxy = calcheight () in
445 state.y <- truncate
(float maxy *. rely);
446 let pages = layout state.y state.h
in
447 state.layout <- pages;
448 Glut.postRedisplay
();
451 let s = Scanf.sscanf
cmd "T %n"
452 (fun n -> String.sub
cmd n (String.length
cmd - n))
457 (* Glut.postRedisplay () *)
460 let pageno, c
, x0
, y0
, x1
, y1
=
461 Scanf.sscanf
cmd "F %d %d %f %f %f %f"
462 (fun p c x0 y0 x1 y1
-> (p
, c
, x0
, y0
, x1
, y1
))
464 let y = (getpagey pageno) + truncate y0
in
467 state.rects
<- [pageno, c
, (x0
, y0
), (x1
, y1
)]
470 let pageno, c
, x0
, y0
, x1
, y1
=
471 Scanf.sscanf
cmd "R %d %d %f %f %f %f"
472 (fun pageno c x0 y0 x1 y1
-> (pageno, c
, x0
, y0
, x1
, y1
))
474 state.rects
<- (pageno, c
, (x0
, y0
), (x1
, y1
)) :: state.rects
478 Scanf.sscanf
cmd "r %d %d %d %s"
479 (fun n w h p
-> (n, w
, h
, p
))
481 Hashtbl.replace
state.pagemap
(n, w
) p
;
482 let evicted = cbpeekw state.pagecache
in
483 if String.length
evicted > 0
485 wcmd "free" [`
s evicted];
486 let l = Hashtbl.fold
(fun k p a
->
487 if evicted = p
then k
:: a
else a
) state.pagemap
[]
489 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
491 cbput state.pagecache p
;
492 state.inflight
<- pred
state.inflight
;
493 Glut.postRedisplay
()
496 let (n, w
, h
) as pagelayout
=
497 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
499 state.pages <- pagelayout
:: state.pages
503 Scanf.sscanf
cmd "o %d %d %d %n" (fun l n t pos
-> l, n, t
, pos
)
505 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
506 let outline = (s, l, n, t
) in
508 match state.outlines with
509 | Olist
outlines -> Olist
(outline :: outlines)
510 | Oarray _
-> Olist
[outline]
512 state.outlines <- outlines
515 log "unknown cmd `%S'" cmd
518 let getopaque pageno =
519 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
))
520 with Not_found
-> None
523 let cache pageno opaque
=
524 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
) opaque
527 let validopaque opaque
= String.length opaque
> 0;;
530 match getopaque l.pageno with
531 | Some opaque
when validopaque opaque
->
534 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
535 state.inflight
<- succ
state.inflight
;
537 wcmd "render" [`i
(l.pageno + 1)
546 if not
conf.redispimm
&& state.y != state.prevy
548 state.prevy
<- state.y;
549 Glut.postRedisplay
();
552 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.02 in
556 if conf.preload then begin
558 let y = if state.y < state.h then 0 else state.y - state.h in
559 let pages = layout y (h*3) in
560 List.iter
preload pages;
564 let cmd = readcmd state.csock
in
569 let search pattern forward
=
570 if String.length pattern
> 0
573 match state.layout with
576 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
579 let b = makecmd "search"
580 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
582 Buffer.add_char
b '
,'
;
583 Buffer.add_string
b pattern
;
584 Buffer.add_char
b '
\000'
;
587 writecmd state.csock
cmd;
590 let intentry text key
=
591 let c = Char.unsafe_chr key
in
594 let s = "x" in s.[0] <- c;
595 let text = text ^
s in
599 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
604 let b = Buffer.create
(String.length
s + 1) in
605 Buffer.add_string
b s;
610 let textentry text key
=
611 let c = Char.unsafe_chr key
in
613 | _
when key
>= 32 && key
<= 127 ->
614 let text = addchar text c in
618 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
622 let optentry text key
=
623 let btos b = if b then "on" else "off" in
624 let c = Char.unsafe_chr key
in
627 conf.rectsel
<- not
conf.rectsel
;
628 TEdone
("rectsel " ^
(btos conf.rectsel
))
631 conf.icase
<- not
conf.icase
;
632 TEdone
("case insensitive search " ^
(btos conf.icase
))
635 conf.preload <- not
conf.preload;
636 TEdone
("preload " ^
(btos conf.preload))
639 conf.titletext
<- not
conf.titletext
;
640 TEdone
("titletext " ^
(btos conf.titletext
))
643 conf.redispimm
<- not
conf.redispimm
;
644 TEdone
("immediate redisplay " ^
(btos conf.redispimm
))
647 conf.verbose
<- not
conf.verbose
;
648 TEdone
("verbose " ^
(btos conf.verbose
))
651 conf.maxhfit
<- not
conf.maxhfit
;
652 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
653 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
656 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
660 let maxoutlinerows () = (state.h - 31) / 16;;
662 let enterselector outlines errmsg
=
663 if Array.length
outlines = 0
670 match state.layout with
672 | {pageno=pageno} :: rest
-> pageno
676 if n = Array.length
outlines
679 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
680 if outlinepageno
>= pageno then n else loop (n+1)
685 Some
(active, max
0 (active - maxoutlinerows ()), outlines, "");
686 Glut.postRedisplay
();
689 let enteroutlinemode () =
691 match state.outlines with
694 let a = Array.of_list
(List.rev
l) in
695 state.outlines <- Oarray
a;
698 enterselector outlines "Documents has no outline";
701 let enterbookmarkmode () =
702 let bookmarks = Array.of_list
state.bookmarks in
703 enterselector bookmarks "Documents has no bookmarks (yet)";
706 let viewkeyboard ~key ~x ~
y =
708 state.textentry <- te
;
713 match state.textentry with
715 let c = Char.chr key
in
730 Glut.postRedisplay
()
733 let ondone isforw
s =
734 state.searchpattern
<- s;
737 enttext (Some
(c, "", textentry, ondone (c ='
/'
)))
742 try int_of_string
s with exc
->
743 state.text <- Printf.sprintf
"bad integer `%s': %s"
744 s (Printexc.to_string exc
);
750 state.text <- "page bias is now " ^ string_of_int
n;
753 enttext (Some
('
+'
, "", intentry, ondone))
759 enttext (Some
('
-'
, "", optentry, ondone))
764 try int_of_string
s with exc
->
765 state.text <- Printf.sprintf
"bad integer `%s': %s"
766 s (Printexc.to_string exc
);
773 gotoy (getpagey (n + conf.pagebias
- 1))
776 let pageentry text key
=
777 match Char.unsafe_chr key
with
779 | _
-> intentry text key
781 let text = "x" in text.[0] <- c;
782 enttext (Some
('
:'
, text, pageentry, ondone))
785 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
786 reshape state.w
state.h;
789 begin match state.fullscreen
with
791 state.fullscreen
<- Some
(state.w
, state.h);
794 state.fullscreen
<- None
;
795 Glut.reshapeWindow ~w ~
h
799 search state.searchpattern
true
802 search state.searchpattern
false
805 begin match state.layout with
808 gotoy (state.y - l.pagey);
812 begin match List.rev
state.layout with
815 gotoy (clamp (l.pageh
- l.pagey))
819 begin match state.layout with
822 gotoy (clamp (-l.pageh
));
827 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
829 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
831 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
832 let percent = (100. *. (float state.y /. float maxy)) in
835 Printf.sprintf
"Page %d of %d %.2f%%"
836 (fn+1) state.pagecount
percent
839 "Pages %d-%d of %d %.2f%%"
840 (fn+1) (ln
+1) state.pagecount
percent
846 begin match state.layout with
849 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
850 Glut.postRedisplay
();
858 match state.layout with
860 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
863 enttext (Some
('~'
, "", textentry, ondone))
866 vlog "huh? %d %c" key
(Char.chr key
);
869 | Some
(c, text, onkey
, ondone) when key
= 8 ->
870 let len = String.length
text in
871 if len = 0 || len = 1
873 state.textentry <- None
;
874 Glut.postRedisplay
();
877 let s = String.sub
text 0 (len - 1) in
878 enttext (Some
(c, s, onkey
, ondone))
881 | Some
(c, text, onkey
, ondone) ->
882 begin match Char.unsafe_chr key
with
885 state.textentry <- None
;
886 Glut.postRedisplay
()
889 state.textentry <- None
;
890 Glut.postRedisplay
()
893 begin match onkey
text key
with
895 state.textentry <- None
;
897 Glut.postRedisplay
()
900 enttext (Some
(c, text, onkey
, ondone));
903 state.textentry <- None
;
904 Glut.postRedisplay
()
909 let outlinekeyboard ~key ~x ~
y (active, first
, outlines, qsearch
) =
910 let search active pattern incr
=
911 let re = Str.regexp_case_fold pattern
in
913 if n = Array.length
outlines || n = -1 then None
else
914 let (s, _
, _
, _
) = outlines.(n) in
916 (try ignore
(Str.search_forward
re s 0); true
917 with Not_found
-> false)
919 let maxrows = maxoutlinerows () in
921 then Some
(n, max
0 (n - maxrows))
922 else Some
(n, max first
(n - maxrows))
930 if String.length qsearch
= 0
933 state.outline <- None
;
934 Glut.postRedisplay
();
938 state.outline <- Some
(active, first
, outlines, "");
939 Glut.postRedisplay
();
943 let incr = if key
= 18 then -1 else 1 in
945 match search (active + incr) qsearch
incr with
946 | None
-> active, first
949 state.outline <- Some
(active, first
, outlines, qsearch
);
950 Glut.postRedisplay
();
953 let len = String.length qsearch
in
960 state.outline <- Some
(active, first
, outlines, "");
963 let qsearch = String.sub
qsearch 0 (len - 1) in
964 state.text <- qsearch;
965 state.outline <- Some
(active, first
, outlines, qsearch);
967 Glut.postRedisplay
()
970 let (_
, _
, n, t
) = outlines.(active) in
973 state.outline <- None
;
974 Glut.postRedisplay
();
976 | _
when key
>= 32 && key
<= 127 ->
977 let pattern = addchar qsearch (Char.chr key
) in
978 let pattern, active, first
=
979 match search active pattern 1 with
980 | None
-> qsearch, active, first
981 | Some
(active, first
) -> (pattern, active, first
)
983 state.text <- pattern;
984 state.outline <- Some
(active, first
, outlines, pattern);
985 Glut.postRedisplay
()
987 | _
-> log "unknown key %d" key
990 let keyboard ~key ~x ~
y =
991 match state.outline with
992 | None
-> viewkeyboard ~key ~x ~
y
993 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
996 let special ~key ~x ~
y =
997 match state.outline with
1001 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1002 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1003 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1004 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1005 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1006 | Glut.KEY_HOME
-> addnav (); 0
1009 state.maxy - (if conf.maxhfit
then state.h else 0)
1015 | Some
(active, first
, outlines, qsearch) ->
1016 let maxrows = maxoutlinerows () in
1018 let active = active + incr in
1019 let active = max
0 (min
active (Array.length
outlines - 1)) in
1023 let rows = active - first in
1024 if rows > maxrows then first + incr else first
1027 state.outline <- Some
(active, first, outlines, qsearch);
1028 Glut.postRedisplay
()
1031 | Glut.KEY_UP
-> navigate ~
-1
1032 | Glut.KEY_DOWN
-> navigate 1
1033 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1034 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1037 state.outline <- Some
(0, 0, outlines, qsearch);
1038 Glut.postRedisplay
()
1041 let active = Array.length
outlines - 1 in
1042 let first = max
0 (active - maxrows) in
1043 state.outline <- Some
(active, first, outlines, qsearch);
1044 Glut.postRedisplay
()
1049 let drawplaceholder l =
1052 GlDraw.color
(0.2, 0.2, 0.2);
1053 GlDraw.color
(1.0, 1.0, 1.0);
1055 (0.0, float l.pagedispy
)
1056 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1059 and y = float (l.pagedispy
+ l.pagevh
- 5) in
1060 let font = Glut.BITMAP_8_BY_13
in
1061 GlDraw.color
(0.0, 0.0, 0.0);
1062 GlPix.raster_pos ~
x ~
y ();
1063 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1064 ("Loading " ^ string_of_int
l.pageno);
1067 GlDraw.begins `quads
;
1068 GlDraw.vertex2
(0.0, float l.pagedispy
);
1069 GlDraw.vertex2
(float l.pagew
, float l.pagedispy
);
1070 GlDraw.vertex2
(float l.pagew
, float (l.pagedispy
+ l.pagevh
));
1071 GlDraw.vertex2
(0.0, float (l.pagedispy
+ l.pagevh
));
1076 let now () = Unix.gettimeofday
();;
1079 begin match getopaque l.pageno with
1080 | Some opaque
when validopaque opaque
->
1081 GlDraw.color
(1.0, 1.0, 1.0);
1083 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1086 vlog "draw %f sec" d;
1092 if state.inflight
< cblen state.pagecache
1094 List.iter
preload state.layout;
1098 vlog "inflight %d" state.inflight
;
1101 GlDraw.color
(0.5, 0.5, 0.5);
1104 (float (state.w
- conf.scrollw
), float (i
+ (l.pagedispy
- i
)))
1106 l.pagedispy
+ l.pagevh
;
1109 let scrollindicator () =
1110 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1111 GlDraw.color
(0.64 , 0.64, 0.64);
1113 (float (state.w
- conf.scrollw
), 0.)
1114 (float state.w
, float state.h)
1116 GlDraw.color
(0.0, 0.0, 0.0);
1117 let sh = (float (maxy + state.h) /. float state.h) in
1118 let sh = float state.h /. sh in
1119 let sh = max
sh (float conf.scrollh
) in
1122 if state.y = state.maxy
1124 else float state.y /. float maxy
1126 let position = (float state.h -. sh) *. percent in
1129 if position +. sh > float state.h
1136 (float (state.w
- conf.scrollw
), position)
1137 (float state.w
, position +. sh)
1142 match state.mstate
with
1146 | Msel
((x0
, y0
), (x1
, y1
)) ->
1148 and y1
= max
y0 y1
in
1151 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1152 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1154 match getopaque l.pageno with
1155 | Some opaque
when validopaque opaque
->
1156 let oy = -l.pagey + l.pagedispy
in
1157 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1160 List.iter
f state.layout
1165 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1166 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1168 (fun (pageno, c, (x0
, y0), (x1
, y1
)) ->
1170 if l.pageno = pageno
1172 let d = float (l.pagedispy
- l.pagey) in
1173 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1174 GlDraw.rect
(x0
, y0 +. d) (x1
, y1
+. d)
1182 let showoutline = function
1184 | Some
(active, first, outlines, qsearch) ->
1186 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1187 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1188 GlDraw.rect
(0., 0.) (float state.w
, float state.h);
1191 GlDraw.color
(1., 1., 1.);
1192 let font = Glut.BITMAP_9_BY_15
in
1193 let draw_string x y s =
1194 GlPix.raster_pos ~
x ~
y ();
1195 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1198 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1201 let (s, l, _
, _
) = outlines.(row
) in
1202 let y = (row
- first) * 16 in
1207 GlDraw.polygon_mode `both `line
;
1208 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1209 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1210 GlDraw.rect
(0., float (y + 1))
1211 (float (state.w
- conf.scrollw
- 1), float (y + 18));
1212 GlDraw.polygon_mode `both `fill
;
1214 GlDraw.color
(1., 1., 1.);
1216 draw_string (float x) (float (y + 16)) s;
1224 let lasty = List.fold_left
drawpage 0 (state.layout) in
1225 GlDraw.color
(0.5, 0.5, 0.5);
1228 (float (state.w
- conf.scrollw
), float state.h)
1233 showoutline state.outline;
1235 Glut.swapBuffers
();
1239 let rec f = function
1241 begin match getopaque l.pageno with
1242 | Some opaque
when validopaque opaque
->
1243 let y = y - l.pagedispy
in
1246 let y = l.pagey + y in
1247 match getlink opaque
x y with
1261 let rec f = function
1263 begin match getopaque l.pageno with
1264 | Some opaque
when validopaque opaque
->
1265 let y = y - l.pagedispy
in
1268 let y = l.pagey + y in
1269 if checklink opaque
x y then true else f rest
1280 let mouse ~button ~bstate ~
x ~
y =
1282 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1290 let incr = incr * 2 in
1291 let y = clamp incr in
1294 | Glut.LEFT_BUTTON
when state.outline = None
->
1295 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1296 begin match dest with
1297 | Some
(pageno, top
) ->
1301 if bstate
= Glut.DOWN
1303 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1304 state.mstate
<- Msel
((x, y), (x, y));
1305 Glut.postRedisplay
()
1308 Glut.setCursor
Glut.CURSOR_RIGHT_ARROW
;
1309 state.mstate
<- Mnone
;
1316 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1319 if state.outline = None
1321 match state.mstate
with
1324 state.mstate
<- Msel
(a, (x, y));
1325 Glut.postRedisplay
()
1329 if state.outline = None
1331 match state.mstate
with
1332 | Mnone
when (checklink x y) ->
1333 Glut.setCursor
Glut.CURSOR_INFO
1336 Glut.setCursor
Glut.CURSOR_RIGHT_ARROW
1343 let statepath = (Sys.getenv
"HOME") ^
"/.config/llpp" in
1346 let ic = open_in_bin
statepath in
1347 let hash = input_value
ic in
1353 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1359 Hashtbl.replace
pstate state.path
(state.bookmarks, state.w
, state.h);
1360 let oc = open_out_bin
statepath in
1361 output_value
oc pstate
1365 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1370 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1373 state.bookmarks <- statebookmarks;
1375 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1378 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1380 if String.length
state.path
= 0
1381 then (prerr_endline
"filename missing"; exit
1)
1386 let _ = Glut.init
Sys.argv
in
1387 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1388 let () = Glut.initWindowSize
state.w
state.h in
1389 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1391 let csock, ssock
= Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0 in
1394 state.csock <- csock;
1395 state.ssock
<- ssock
;
1396 writecmd csock ("open " ^
name ^
"\000");
1398 let () = Glut.displayFunc
display in
1399 let () = Glut.reshapeFunc
reshape in
1400 let () = Glut.keyboardFunc
keyboard in
1401 let () = Glut.specialFunc
special in
1402 let () = Glut.idleFunc
(Some
idle) in
1403 let () = Glut.mouseFunc
mouse in
1404 let () = Glut.motionFunc
motion in
1405 let () = Glut.passiveMotionFunc
pmotion in
1409 let rec handlelablglutbug () =
1412 with Glut.BadEnum
"key in special_of_int" ->
1413 showtext '
!'
" LablGlut bug: special key not recognized";
1414 Glut.swapBuffers
();
1415 handlelablglutbug ()
1417 handlelablglutbug ();