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
;;
32 { store
= Array.create n v
39 let cblen b
= Array.length b
.store
;;
44 b
.wc
<- (b
.wc
+ 1) mod len;
45 b
.len <- (b
.len + 1) mod len;
48 let cbpeekw b
= b
.store
.(b
.wc
);;
51 let v = b
.store
.(b
.rc
) in
56 let rc = if b
.rc = 0 then b
.len - 1 else b
.rc - 1 in
78 { mutable scrollw
: int
79 ; mutable scrollh
: int
80 ; mutable rectsel
: bool
81 ; mutable icase
: bool
82 ; mutable preload
: bool
83 ; mutable titletext
: bool
84 ; mutable pagebias
: int
85 ; mutable redispimm
: bool
86 ; mutable verbose
: bool
87 ; mutable scrollincr
: int
88 ; mutable maxhfit
: bool
92 type outline
= string * int * int * int;;
93 type outlines
= Oarray
of outline array
| Olist
of outline list
;;
96 { mutable csock
: Unix.file_descr
97 ; mutable ssock
: Unix.file_descr
101 ; mutable prevy
: int
103 ; mutable layout
: layout list
104 ; pagemap
: ((int * int), string) Hashtbl.t
105 ; mutable pages
: (int * int * int) list
106 ; mutable pagecount
: int
107 ; pagecache
: string circbuf
108 ; navhist
: float circbuf
109 ; mutable inflight
: int
111 ; mutable mstate
: mstate
112 ; mutable searchpattern
: string
113 ; mutable rects
: (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
: (int * int * outline array
* string) option
147 ; pagemap
= Hashtbl.create
10
148 ; pagecache
= cbnew 10 ""
154 ; navhist
= cbnew 100 0.0
160 ; outlines
= Olist
[]
168 Printf.kprintf prerr_endline fmt
170 Printf.kprintf ignore fmt
174 let len = String.length s
in
176 let b = Buffer.create
n in
177 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
178 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
179 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
180 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
181 Buffer.add_string
b s
;
182 let s'
= Buffer.contents
b in
183 let n'
= Unix.write fd
s'
0 n in
184 if n'
!= n then failwith
"write failed";
189 let n = Unix.read fd
s 0 4 in
190 if n != 4 then failwith
"incomplete read(len)";
192 lor (Char.code
s.[0] lsl 24)
193 lor (Char.code
s.[1] lsl 16)
194 lor (Char.code
s.[2] lsl 8)
195 lor (Char.code
s.[3] lsl 0)
197 let s = String.create
len in
198 let n = Unix.read fd
s 0 len in
199 if n != len then failwith
"incomplete read(data)";
204 if y
= state.maxy
then 1.0
205 else float y
/. float state.maxy
209 let b = Buffer.create
10 in
210 Buffer.add_string
b s;
211 let rec combine = function
212 | [] -> Buffer.contents
b
214 Buffer.add_char
b ' '
;
217 | `
b b -> if b then "1" else "0"
219 | `i i
-> string_of_int i
220 | `f f
-> string_of_float f
221 | `I f
-> string_of_int
(truncate f
)
223 Buffer.add_string
b s;
227 writecmd state.csock
s;
231 let rec f pn ph fh l
=
233 | (n, _
, h
) :: rest
->
234 let fh = fh + (n - pn
) * ph
in
238 let fh = fh + (ph
* (state.pagecount
- pn
)) in
239 max
0 (fh - (if conf.maxhfit
then state.h
else 0))
241 let fh = f 0 0 0 state.pages
in
245 let getpagey pageno
=
246 let rec f pn ph y l
=
248 | (n, _
, h
) :: rest
->
251 y
+ (pageno
- pn
) * ph
253 let y = y + (n - pn
) * ph
in
257 y + (pageno
- pn
) * ph
263 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
264 if pageno
= state.pagecount
|| cacheleft
= 0
267 let ((_
, w
, h
) as curr
), rest
, pdimno
=
269 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
270 curr
, rest
, pdimno
+ 1
274 let pageno'
= pageno + 1 in
308 let accu = e :: accu in
309 f pageno' pdimno curr
310 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
311 (pred cacheleft
) accu
313 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
315 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
316 state.maxy
<- calcheight ();
321 let y = state.y + incr
in
323 let y = min
y state.maxy
in
329 let y = min
state.maxy
y in
330 let pages = layout y state.h
in
332 state.layout <- pages;
335 Glut.postRedisplay
()
340 cbput state.navhist
(yratio state.y);
341 cbrfollowlen state.navhist
;
345 let y = cbget state.navhist
in
346 truncate
(y *. float state.maxy
)
350 let y = getpagey n in
359 GlDraw.viewport
0 0 w h
;
360 GlMat.mode `modelview
;
361 GlMat.load_identity
();
362 GlMat.mode `projection
;
363 GlMat.load_identity
();
364 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
365 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
366 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
367 GlClear.color
(1., 1., 1.);
368 GlClear.clear
[`color
];
373 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
377 if not
conf.titletext
379 GlDraw.color
(0.0, 0.0, 0.0);
381 (0.0, float (state.h
- 18))
382 (float (state.w
- conf.scrollw
- 1), float state.h
)
384 let font = Glut.BITMAP_8_BY_13
in
385 GlDraw.color
(1.0, 1.0, 1.0);
386 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
387 Glut.bitmapCharacter ~
font ~c
:(Char.code c
);
388 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s
391 let len = String.length
s in
392 let dst = String.create
(len + 1) in
401 Glut.setWindowTitle ~title
:dst
405 let len = String.length
state.text
in
406 match state.textentry
with
408 if len > 0 then showtext ' '
state.text
410 | Some
(c
, text
, _
, _
) ->
414 text ^
" [" ^
state.text ^
"]"
427 Glut.postRedisplay
()
430 let n = Scanf.sscanf cmd
"C %d" (fun n -> n) in
431 state.pagecount
<- n;
432 let rely = yratio state.y in
433 let maxy = calcheight () in
434 state.y <- truncate
(float maxy *. rely);
435 let pages = layout state.y state.h
in
436 state.layout <- pages;
437 Glut.postRedisplay
();
440 state.safe
<- state.safe
- 1;
443 Glut.postRedisplay
()
447 let s = Scanf.sscanf cmd
"T %S" (fun s -> s) in
451 (* Glut.postRedisplay () *)
454 let pageno, c
, x0
, y0
, x1
, y1
=
455 Scanf.sscanf cmd
"F %d %d %f %f %f %f"
456 (fun p c x0 y0 x1 y1
-> (p
, c
, x0
, y0
, x1
, y1
))
458 let y = (getpagey pageno) + truncate y0
in
461 state.rects
<- [pageno, c
, (x0
, y0
), (x1
, y1
)]
464 let pageno, c
, x0
, y0
, x1
, y1
=
465 Scanf.sscanf cmd
"R %d %d %f %f %f %f"
466 (fun pageno c x0 y0 x1 y1
-> (pageno, c
, x0
, y0
, x1
, y1
))
468 state.rects
<- (pageno, c
, (x0
, y0
), (x1
, y1
)) :: state.rects
472 Scanf.sscanf cmd
"r %d %d %d %s"
473 (fun n w h p
-> (n, w
, h
, p
))
475 Hashtbl.replace
state.pagemap
(n, w
) p
;
476 let evicted = cbpeekw state.pagecache
in
477 if String.length
evicted > 0
479 state.safe
<- state.safe
+ 1;
480 wcmd "free" [`
s evicted];
481 let l = Hashtbl.fold
(fun k p a
->
482 if evicted = p
then k
:: a
else a
) state.pagemap
[]
484 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
486 cbput state.pagecache p
;
487 state.inflight
<- pred
state.inflight
;
488 Glut.postRedisplay
()
491 let (n, w
, h
) as pagelayout
=
492 Scanf.sscanf cmd
"l %d %d %d" (fun n w h
-> n, w
, h
)
494 state.pages <- pagelayout
:: state.pages
497 let (s, l, n, t
) as outline
=
498 Scanf.sscanf cmd
"o %S %d %d %d" (fun s l n t
-> s, l, n, t
)
501 match state.outlines with
502 | Olist
outlines -> Olist
(outline
:: outlines)
503 | Oarray _
-> Olist
[outline
]
505 state.outlines <- outlines
508 log "unknown cmd `%S'" cmd
511 let getopaque pageno =
512 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
))
513 with Not_found
-> None
516 let cache pageno opaque
=
517 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
) opaque
520 let validopaque opaque
= String.length opaque
> 0;;
523 match getopaque l.pageno with
524 | Some opaque
when validopaque opaque
->
527 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
528 state.inflight
<- succ
state.inflight
;
530 wcmd "render" [`i
(l.pageno + 1)
539 if not
conf.redispimm
&& state.y != state.prevy
541 state.prevy
<- state.y;
542 Glut.postRedisplay
();
545 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.02 in
549 if conf.preload then begin
551 let y = if state.y < state.h then 0 else state.y - state.h in
552 let pages = layout y (h*3) in
553 List.iter
preload pages;
557 let cmd = readcmd state.csock
in
562 let search pattern forward
=
563 if String.length pattern
> 0
566 match state.layout with
569 l.pageno, (l.pagey
+ if forward
then 0 else 0*l.pagevh
)
571 wcmd "search" [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)
572 ; `
s (pattern ^
"\000")]
575 let intentry text key
=
576 let c = Char.unsafe_chr key
in
579 let s = "x" in s.[0] <- c;
580 let text = text ^
s in
584 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
589 let b = Buffer.create
(String.length
s + 1) in
590 Buffer.add_string
b s;
595 let textentry text key
=
596 let c = Char.unsafe_chr key
in
598 | _
when key
>= 32 && key
<= 127 ->
599 let text = addchar text c in
603 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
607 let optentry text key
=
608 let btos b = if b then "on" else "off" in
609 let c = Char.unsafe_chr key
in
612 conf.rectsel
<- not
conf.rectsel
;
613 TEdone
("rectsel " ^
(btos conf.rectsel
))
616 conf.icase
<- not
conf.icase
;
617 TEdone
("case insensitive search " ^
(btos conf.icase
))
620 conf.preload <- not
conf.preload;
621 TEdone
("preload " ^
(btos conf.preload))
624 conf.titletext
<- not
conf.titletext
;
625 TEdone
("titletext " ^
(btos conf.titletext
))
628 conf.redispimm
<- not
conf.redispimm
;
629 TEdone
("immediate redisplay " ^
(btos conf.redispimm
))
632 conf.verbose
<- not
conf.verbose
;
633 TEdone
("verbose " ^
(btos conf.verbose
))
636 conf.maxhfit
<- not
conf.maxhfit
;
637 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
638 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
641 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
645 let maxoutlinerows () = (state.h - 31) / 16;;
647 let enteroutlinemode () =
649 match state.layout with
651 | {pageno=pageno} :: rest
-> pageno
654 match state.outlines with
657 let a = Array.of_list
(List.rev
l) in
658 state.outlines <- Oarray
a;
663 if n = Array.length
outlines
666 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
667 if outlinepageno
>= pageno then n else loop (n+1)
672 Some
(active, max
0 (active - maxoutlinerows ()), outlines, "");
673 Glut.postRedisplay
();
676 let viewkeyboard ~key ~x ~
y =
678 state.textentry <- te
;
683 match state.textentry with
685 let c = Char.chr key
in
700 Glut.postRedisplay
()
703 let ondone isforw
s =
704 state.searchpattern
<- s;
707 enttext (Some
(c, "", textentry, ondone (c ='
/'
)))
712 try int_of_string
s with exc
->
713 state.text <- Printf.sprintf
"bad integer `%s': %s"
714 s (Printexc.to_string exc
);
720 state.text <- "page bias is now " ^ string_of_int
n;
723 enttext (Some
('
+'
, "", intentry, ondone))
729 enttext (Some
('
-'
, "", optentry, ondone))
734 try int_of_string
s with exc
->
735 state.text <- Printf.sprintf
"bad integer `%s': %s"
736 s (Printexc.to_string exc
);
743 gotoy (getpagey (n + conf.pagebias
- 1))
746 let pageentry text key
=
747 match Char.unsafe_chr key
with
749 | _
-> intentry text key
751 let text = "x" in text.[0] <- c;
752 enttext (Some
('
:'
, text, pageentry, ondone))
755 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
756 reshape state.w
state.h;
759 begin match state.fullscreen
with
761 state.fullscreen
<- Some
(state.w
, state.h);
764 state.fullscreen
<- None
;
765 Glut.reshapeWindow ~w ~
h
769 search state.searchpattern
true
772 search state.searchpattern
false
775 begin match state.layout with
778 gotoy (state.y - l.pagey
);
782 begin match List.rev
state.layout with
785 gotoy (state.y + l.pageh
- l.pagey
)
789 begin match state.layout with
792 gotoy (state.y-l.pageh
);
797 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
799 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
801 let percent = (100. *. yratio state.y) in
804 Printf.sprintf
"Page %d of %d %.2f%%"
805 (fn+1) state.pagecount
percent
808 "Pages %d-%d of %d %.2f%%"
809 (fn+1) (ln
+1) state.pagecount
percent
815 begin match state.layout with
818 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
819 Glut.postRedisplay
();
823 vlog "huh? %d %c" key
(Char.chr key
);
826 | Some
(c, text, onkey
, ondone) when key
= 8 ->
827 let len = String.length
text in
828 if len = 0 || len = 1
830 state.textentry <- None
;
831 Glut.postRedisplay
();
834 let s = String.sub
text 0 (len - 1) in
835 enttext (Some
(c, s, onkey
, ondone))
838 | Some
(c, text, onkey
, ondone) ->
839 begin match Char.unsafe_chr key
with
842 state.textentry <- None
;
843 Glut.postRedisplay
()
846 state.textentry <- None
;
847 Glut.postRedisplay
()
850 begin match onkey
text key
with
852 state.textentry <- None
;
854 Glut.postRedisplay
()
857 enttext (Some
(c, text, onkey
, ondone));
860 state.textentry <- None
;
861 Glut.postRedisplay
()
866 let outlinekeyboard ~key ~x ~
y (active, first
, outlines, qsearch
) =
867 let search active pattern incr
=
868 let re = Str.regexp_case_fold pattern
in
870 if n = Array.length
outlines || n = -1 then None
else
871 let (s, _
, _
, _
) = outlines.(n) in
873 (try ignore
(Str.search_forward
re s 0); true
874 with Not_found
-> false)
876 let maxrows = maxoutlinerows () in
878 then Some
(n, max
0 (n - maxrows))
879 else Some
(n, max first
(n - maxrows))
887 if String.length qsearch
= 0
890 state.outline
<- None
;
891 Glut.postRedisplay
();
895 state.outline
<- Some
(active, first
, outlines, "");
896 Glut.postRedisplay
();
900 let incr = if key
= 18 then -1 else 1 in
902 match search (active + incr) qsearch
incr with
903 | None
-> active, first
906 state.outline
<- Some
(active, first
, outlines, qsearch
);
907 Glut.postRedisplay
();
910 let len = String.length qsearch
in
917 state.outline
<- Some
(active, first
, outlines, "");
920 let qsearch = String.sub
qsearch 0 (len - 1) in
921 state.text <- qsearch;
922 state.outline
<- Some
(active, first
, outlines, qsearch);
924 Glut.postRedisplay
()
927 let (_
, _
, n, t
) = outlines.(active) in
930 state.outline
<- None
;
931 Glut.postRedisplay
();
933 | _
when key
>= 32 && key
<= 127 ->
934 let pattern = addchar qsearch (Char.chr key
) in
936 match search active pattern 1 with
937 | None
-> active, first
940 state.text <- pattern;
941 state.outline
<- Some
(active, first
, outlines, pattern);
942 Glut.postRedisplay
()
944 | _
-> log "unknown key %d" key
947 let keyboard ~key ~x ~
y =
948 match state.outline
with
949 | None
-> viewkeyboard ~key ~x ~
y
950 | Some outline
-> outlinekeyboard ~key ~x ~
y outline
953 let special ~key ~x ~
y =
954 match state.outline
with
958 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
959 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
960 | Glut.KEY_DOWN
-> clamp conf.scrollincr
961 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
962 | Glut.KEY_PAGE_DOWN
-> clamp state.h
963 | Glut.KEY_HOME
-> addnav (); 0
966 state.maxy - (if conf.maxhfit
then 0 else state.h)
972 | Some
(active, first
, outlines, qsearch) ->
973 let maxrows = maxoutlinerows () in
975 let active = active + incr in
976 let active = max
0 (min
active (Array.length
outlines - 1)) in
980 let rows = active - first in
981 if rows > maxrows then first + incr else first
984 state.outline
<- Some
(active, first, outlines, qsearch);
985 Glut.postRedisplay
()
988 | Glut.KEY_UP
-> navigate ~
-1
989 | Glut.KEY_DOWN
-> navigate 1
990 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
991 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
994 state.outline
<- Some
(0, 0, outlines, qsearch);
995 Glut.postRedisplay
()
998 let active = Array.length
outlines - 1 in
999 let first = max
0 (active - maxrows) in
1000 state.outline
<- Some
(active, first, outlines, qsearch);
1001 Glut.postRedisplay
()
1006 let drawplaceholder l =
1009 GlDraw.color
(0.2, 0.2, 0.2);
1010 GlDraw.color
(1.0, 1.0, 1.0);
1012 (0.0, float l.pagedispy
)
1013 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1016 and y = float (l.pagedispy
+ l.pagevh
- 5) in
1017 let font = Glut.BITMAP_8_BY_13
in
1018 GlDraw.color
(0.0, 0.0, 0.0);
1019 GlPix.raster_pos ~
x ~
y ();
1020 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1021 ("Loading " ^ string_of_int
l.pageno);
1024 GlDraw.begins `quads
;
1025 GlDraw.vertex2
(0.0, float l.pagedispy
);
1026 GlDraw.vertex2
(float l.pagew
, float l.pagedispy
);
1027 GlDraw.vertex2
(float l.pagew
, float (l.pagedispy
+ l.pagevh
));
1028 GlDraw.vertex2
(0.0, float (l.pagedispy
+ l.pagevh
));
1033 let now () = Unix.gettimeofday
();;
1036 begin match getopaque l.pageno with
1037 | Some opaque
when validopaque opaque
->
1038 GlDraw.color
(1.0, 1.0, 1.0);
1042 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
1046 vlog "draw %f sec safe %d" d state.safe
;
1052 if state.inflight
< cblen state.pagecache
1054 List.iter
preload state.layout;
1058 vlog "inflight %d" state.inflight
;
1061 GlDraw.color
(0.5, 0.5, 0.5);
1064 (float (state.w
- conf.scrollw
), float (i
+ (l.pagedispy
- i
)))
1066 l.pagedispy
+ l.pagevh
;
1069 let scrollindicator () =
1070 GlDraw.color
(0.64 , 0.64, 0.64);
1072 (float (state.w
- conf.scrollw
), 0.)
1073 (float state.w
, float state.h)
1075 GlDraw.color
(0.0, 0.0, 0.0);
1076 let sh = (float (state.maxy + state.h) /. float state.h) in
1077 let sh = float state.h /. sh in
1078 let sh = max
sh (float conf.scrollh
) in
1080 let percent = yratio state.y in
1081 let position = (float state.h -. sh) *. percent in
1084 if position +. sh > float state.h
1091 (float (state.w
- conf.scrollw
), position)
1092 (float state.w
, position +. sh)
1097 match state.mstate
with
1101 | Msel
((x0
, y0
), (x1
, y1
)) ->
1103 and y1
= max
y0 y1
in
1106 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1107 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1109 match getopaque l.pageno with
1110 | Some opaque
when validopaque opaque
->
1111 let oy = -l.pagey
+ l.pagedispy
in
1112 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1115 List.iter
f state.layout
1120 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1121 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1123 (fun (pageno, c, (x0
, y0), (x1
, y1
)) ->
1125 if l.pageno = pageno
1127 let d = float (l.pagedispy
- l.pagey
) in
1128 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1129 GlDraw.rect
(x0
, y0 +. d) (x1
, y1
+. d)
1137 let showoutline = function
1139 | Some
(active, first, outlines, qsearch) ->
1141 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1142 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1143 GlDraw.rect
(0., 0.) (float state.w
, float state.h);
1146 GlDraw.color
(1., 1., 1.);
1147 let font = Glut.BITMAP_9_BY_15
in
1148 let draw_string x y s =
1149 GlPix.raster_pos ~
x ~
y ();
1150 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1153 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1156 let (s, l, _
, _
) = outlines.(row
) in
1157 let y = (row
- first) * 16 in
1162 GlDraw.polygon_mode `both `line
;
1163 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1164 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1165 GlDraw.rect
(0., float (y + 1))
1166 (float (state.w
- conf.scrollw
- 1), float (y + 18));
1167 GlDraw.polygon_mode `both `fill
;
1169 GlDraw.color
(1., 1., 1.);
1171 draw_string (float x) (float (y + 16)) s;
1179 let lasty = List.fold_left
drawpage 0 (state.layout) in
1180 GlDraw.color
(0.5, 0.5, 0.5);
1183 (float (state.w
- conf.scrollw
), float state.h)
1188 showoutline state.outline
;
1190 Glut.swapBuffers
();
1194 let rec f = function
1196 begin match getopaque l.pageno with
1197 | Some opaque
when validopaque opaque
->
1198 let y = y - l.pagedispy
in
1201 let y = l.pagey
+ y in
1202 match getlink opaque
x y with
1216 let rec f = function
1218 begin match getopaque l.pageno with
1219 | Some opaque
when validopaque opaque
->
1220 let y = y - l.pagedispy
in
1223 let y = l.pagey
+ y in
1224 if checklink opaque
x y then true else f rest
1235 let mouse ~button ~bstate ~
x ~
y =
1237 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1245 let incr = incr * 2 in
1246 let y = clamp incr in
1249 | Glut.LEFT_BUTTON
when state.outline
= None
->
1250 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1251 begin match dest with
1252 | Some
(pageno, top
) ->
1256 if bstate
= Glut.DOWN
1258 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1259 state.mstate
<- Msel
((x, y), (x, y));
1260 Glut.postRedisplay
()
1263 Glut.setCursor
Glut.CURSOR_RIGHT_ARROW
;
1264 state.mstate
<- Mnone
;
1271 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1274 if state.outline
= None
1276 match state.mstate
with
1279 state.mstate
<- Msel
(a, (x, y));
1280 Glut.postRedisplay
()
1284 if state.outline
= None
1286 match state.mstate
with
1287 | Mnone
when (checklink x y) ->
1288 Glut.setCursor
Glut.CURSOR_INFO
1291 Glut.setCursor
Glut.CURSOR_RIGHT_ARROW
1298 let name = ref "" in
1299 Arg.parse
[] (fun s -> name := s) "options:";
1301 if String.length
!name = 0
1302 then (prerr_endline
"filename missing"; exit
1)
1308 let _ = Glut.init
Sys.argv
in
1309 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1310 let () = Glut.initWindowSize
w h in
1311 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1313 let csock, ssock
= Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0 in
1318 state.csock <- csock;
1319 state.ssock
<- ssock
;
1320 writecmd csock ("open " ^
name ^
"\000");
1322 let () = Glut.displayFunc
display in
1323 let () = Glut.reshapeFunc
reshape in
1324 let () = Glut.keyboardFunc
keyboard in
1325 let () = Glut.specialFunc
special in
1326 let () = Glut.idleFunc
(Some
idle) in
1327 let () = Glut.mouseFunc
mouse in
1328 let () = Glut.motionFunc
motion in
1329 let () = Glut.passiveMotionFunc
pmotion in
1330 let rec handlelablglutbug () =
1333 with Glut.BadEnum
"key in special_of_int" ->
1334 showtext '
!'
" LablGlut bug: special key not recognized";
1335 Glut.swapBuffers
();
1336 handlelablglutbug ()
1338 handlelablglutbug ()