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 -> int = "ml_checklink";;
13 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 rectsel
: bool
79 ; mutable icase
: bool
80 ; mutable preload
: bool
81 ; mutable titletext
: bool
82 ; mutable pagebias
: int
83 ; mutable redispimm
: bool
84 ; mutable verbose
: bool
89 { mutable csock
: Unix.file_descr
90 ; mutable ssock
: Unix.file_descr
96 ; mutable layout
: layout list
97 ; pagemap
: ((int * int), string) Hashtbl.t
98 ; mutable pages
: (int * int * int) list
99 ; mutable pagecount
: int
100 ; pagecache
: string circbuf
101 ; navhist
: float circbuf
102 ; mutable inflight
: int
104 ; mutable mstate
: mstate
105 ; mutable searchpattern
: string
106 ; mutable rects
: (int * int * Gl.point2
* Gl.point2
) list
107 ; mutable text
: string
108 ; mutable fullscreen
: (int * int) option
109 ; mutable textentry
:
110 (char
* string * (string -> int -> te
) * (string -> unit)) option
135 ; pagemap
= Hashtbl.create
10
136 ; pagecache
= cbnew 10 ""
142 ; navhist
= cbnew 100 0.0
153 (* let aincr = 40;; *)
159 Printf.kprintf prerr_endline fmt
161 Printf.kprintf ignore fmt
165 let len = String.length s
in
167 let b = Buffer.create
n in
168 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
169 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
170 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
171 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
172 Buffer.add_string
b s
;
173 let s'
= Buffer.contents
b in
174 let n'
= Unix.write fd
s'
0 n in
175 if n'
!= n then failwith
"write failed";
180 let n = Unix.read fd
s 0 4 in
181 if n != 4 then failwith
"incomplete read(len)";
183 lor (Char.code
s.[0] lsl 24)
184 lor (Char.code
s.[1] lsl 16)
185 lor (Char.code
s.[2] lsl 8)
186 lor (Char.code
s.[3] lsl 0)
188 let s = String.create
len in
189 let n = Unix.read fd
s 0 len in
190 if n != len then failwith
"incomplete read(data)";
195 if y
= state.maxy
then 1.0
196 else float y
/. float state.maxy
200 let b = Buffer.create
10 in
201 Buffer.add_string
b s;
202 let rec combine = function
203 | [] -> Buffer.contents
b
205 Buffer.add_char
b ' '
;
208 | `
b b -> if b then "1" else "0"
210 | `i i
-> string_of_int i
211 | `f f
-> string_of_float f
212 | `I f
-> string_of_int
(truncate f
)
214 Buffer.add_string
b s;
218 writecmd state.csock
s;
222 let rec f pn ph fh l
=
224 | (n, _
, h
) :: rest
->
225 let fh = fh + (n - pn
) * ph
in
229 let fh = fh + (ph
* (state.pagecount
- pn
)) in
232 let fh = f 0 0 0 state.pages
in
236 let getpagey pageno
=
237 let rec f pn ph y l
=
239 | (n, _
, h
) :: rest
->
242 y
+ (pageno
- pn
) * ph
244 let y = y + (n - pn
) * ph
in
248 y + (pageno
- pn
) * ph
254 let rec f pageno pdimno prev vy py dy l accu
=
255 if pageno
= state.pagecount
258 let ((_
, w
, h
) as curr
), rest
, pdimno
=
260 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
261 curr
, rest
, pdimno
+ 1
265 let pageno'
= pageno + 1 in
299 let accu = e :: accu in
300 f pageno' pdimno curr
(vy
+ vh) (py + h
) (dy
+ vh + 2) rest
accu
302 f pageno' pdimno curr vy
(py + h
) dy rest
accu
304 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
[] in
305 state.maxy
<- calcheight ();
310 let y = state.y + incr
in
312 let y = min
y state.maxy
in
318 let y = min
state.maxy
y in
319 let pages = layout y state.h
in
321 state.layout <- pages;
324 Glut.postRedisplay
()
329 cbput state.navhist
(yratio state.y);
330 cbrfollowlen state.navhist
;
334 let y = cbget state.navhist
in
335 truncate
(y *. float state.maxy
)
341 GlDraw.viewport
0 0 w h
;
342 GlMat.mode `modelview
;
343 GlMat.load_identity
();
344 GlMat.mode `projection
;
345 GlMat.load_identity
();
346 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
347 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
348 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
349 GlClear.color
(1., 1., 1.);
350 GlClear.clear
[`color
];
355 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
359 if not
conf.titletext
361 GlDraw.color
(0.0, 0.0, 0.0);
363 (0.0, float (state.h
- 18))
364 (float (state.w
- conf.scrollw
- 1), float state.h
)
366 let font = Glut.BITMAP_8_BY_13
in
367 GlDraw.color
(1.0, 1.0, 1.0);
368 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
369 Glut.bitmapCharacter ~
font ~c
:(Char.code c
);
370 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s
373 let len = String.length
s in
374 let dst = String.create
(len + 1) in
383 Glut.setWindowTitle ~title
:dst
392 Glut.postRedisplay
()
395 let n = Scanf.sscanf cmd
"C %d" (fun n -> n) in
396 state.pagecount
<- n;
397 let rely = yratio state.y in
398 let maxy = calcheight () in
399 state.y <- truncate
(float maxy *. rely);
400 let pages = layout state.y state.h
in
401 state.layout <- pages;
402 Glut.postRedisplay
();
405 state.safe
<- state.safe
- 1;
408 Glut.postRedisplay
()
412 let s = Scanf.sscanf cmd
"T %S" (fun s -> s) in
416 (* Glut.postRedisplay () *)
419 let pageno, c
, x0
, y0
, x1
, y1
=
420 Scanf.sscanf cmd
"F %d %d %f %f %f %f"
421 (fun p c x0 y0 x1 y1
-> (p
, c
, x0
, y0
, x1
, y1
))
423 let y = (getpagey pageno) + truncate y0
in
426 state.rects
<- [pageno, c
, (x0
, y0
), (x1
, y1
)]
429 let pageno, c
, x0
, y0
, x1
, y1
=
430 Scanf.sscanf cmd
"R %d %d %f %f %f %f"
431 (fun pageno c x0 y0 x1 y1
-> (pageno, c
, x0
, y0
, x1
, y1
))
433 state.rects
<- (pageno, c
, (x0
, y0
), (x1
, y1
)) :: state.rects
437 Scanf.sscanf cmd
"r %d %d %d %s"
438 (fun n w h p
-> (n, w
, h
, p
))
440 Hashtbl.replace
state.pagemap
(n, w
) p
;
441 let evicted = cbpeekw state.pagecache
in
442 if String.length
evicted > 0
444 state.safe
<- state.safe
+ 1;
445 wcmd "free" [`
s evicted];
446 let l = Hashtbl.fold
(fun k p a
->
447 if evicted = p
then k
:: a
else a
) state.pagemap
[]
449 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
451 cbput state.pagecache p
;
452 state.inflight
<- pred
state.inflight
;
453 Glut.postRedisplay
()
456 let (n, w
, h
) as pagelayout
=
457 Scanf.sscanf cmd
"l %d %d %d" (fun n w h
-> n, w
, h
)
459 state.pages <- pagelayout
:: state.pages
462 log "unknown cmd `%S'" cmd
465 let getopaque pageno =
466 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
))
467 with Not_found
-> None
470 let cache pageno opaque
=
471 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
) opaque
474 let validopaque opaque
= String.length opaque
> 0;;
477 match getopaque l.pageno with
478 | Some opaque
when validopaque opaque
->
481 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
482 state.inflight
<- succ
state.inflight
;
484 wcmd "render" [`i
(l.pageno + 1)
493 if not
conf.redispimm
&& state.y != state.prevy
495 state.prevy
<- state.y;
496 Glut.postRedisplay
();
499 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.02 in
503 if conf.preload then begin
505 let y = if state.y < state.h then 0 else state.y - state.h in
506 let pages = layout y (h*3) in
507 List.iter
preload pages;
511 let cmd = readcmd state.csock
in
516 let search pattern forward
=
517 if String.length pattern
> 0
520 match state.layout with
523 l.pageno, (l.pagey
+ if forward
then 0 else 0*l.pagevh
)
525 wcmd "search" [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)
526 ; `
s (pattern ^
"\000")]
529 let intentry text key
=
530 let c = Char.unsafe_chr key
in
533 let s = "x" in s.[0] <- c;
534 let text = text ^
s in
538 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
542 let textentry text key
=
543 let c = Char.unsafe_chr key
in
545 | _
when key
>= 32 && key
<= 127 ->
546 let s = "x" in s.[0] <- c;
547 let text = text ^
s in
551 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
555 let optentry text key
=
556 let btos b = if b then "on" else "off" in
557 let c = Char.unsafe_chr key
in
560 conf.rectsel
<- not
conf.rectsel
;
561 TEdone
("rectsel " ^
(btos conf.rectsel
))
564 conf.icase
<- not
conf.icase
;
565 TEdone
("case insensitive search " ^
(btos conf.icase
))
568 conf.preload <- not
conf.preload;
569 TEdone
("preload " ^
(btos conf.preload))
572 conf.titletext
<- not
conf.titletext
;
573 TEdone
("titletext " ^
(btos conf.titletext
))
576 conf.redispimm
<- not
conf.redispimm
;
577 TEdone
("immediate redisplay " ^
(btos conf.redispimm
))
580 conf.verbose
<- not
conf.verbose
;
581 TEdone
("verbose " ^
(btos conf.verbose
))
584 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
588 let keyboard ~key ~x ~
y =
589 match state.textentry with
591 let c = Char.chr key
in
603 Glut.postRedisplay
()
606 let ondone isforw
s =
607 state.searchpattern
<- s;
610 state.textentry <- Some
(c, "", textentry, ondone (c ='
/'
));
612 Glut.postRedisplay
()
617 try int_of_string
s with exc
->
618 state.text <- Printf.sprintf
"bad integer `%s': %s"
619 s (Printexc.to_string exc
);
625 state.text <- "page bias is now " ^ string_of_int
n;
628 state.textentry <- Some
('
+'
, "", intentry, ondone);
630 Glut.postRedisplay
()
636 Glut.postRedisplay
();
638 state.textentry <- Some
('
-'
, "", optentry, ondone);
640 Glut.postRedisplay
()
645 try int_of_string
s with exc
->
646 state.text <- Printf.sprintf
"bad integer `%s': %s"
647 s (Printexc.to_string exc
);
652 gotoy (getpagey (n + conf.pagebias
- 1))
654 let pageentry text key
=
655 match Char.unsafe_chr key
with
657 | _
-> intentry text key
659 let text = "x" in text.[0] <- c;
660 state.textentry <- Some
('
:'
, text, pageentry, ondone);
662 Glut.postRedisplay
()
665 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
666 reshape state.w
state.h;
669 begin match state.fullscreen
with
671 state.fullscreen
<- Some
(state.w
, state.h);
674 state.fullscreen
<- None
;
675 Glut.reshapeWindow ~w ~
h
679 search state.searchpattern
true
682 search state.searchpattern
false
685 begin match state.layout with
688 gotoy (state.y - l.pagey
);
692 begin match List.rev
state.layout with
695 gotoy (state.y + l.pageh
- l.pagey
)
699 begin match state.layout with
702 gotoy (state.y-l.pageh
);
707 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
709 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
711 let percent = (100. *. yratio state.y) in
714 Printf.sprintf
"Page %d of %d %.2f%%"
715 (fn+1) state.pagecount
percent
718 "Pages %d-%d of %d %.2f%%"
719 (fn+1) (ln
+1) state.pagecount
percent
725 begin match state.layout with
728 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
729 Glut.postRedisplay
();
733 vlog "huh? %d %c" key
(Char.chr key
);
736 | Some
(c, text, onkey
, ondone) when c = '
\008'
->
737 let len = String.length
text in
739 if len = 0 || len = 1
743 let s = String.sub
text 0 (len - 1) in
744 Some
(c, s, onkey
, ondone)
747 state.textentry <- te;
748 Glut.postRedisplay
()
750 | Some
(c, text, onkey
, ondone) ->
751 begin match Char.unsafe_chr key
with
753 state.textentry <- None
;
757 let len = String.length
text in
760 state.textentry <- None
763 let text = String.sub
text 0 (len - 1) in
764 state.textentry <- Some
(c, text, onkey
, ondone)
768 state.textentry <- None
771 begin match onkey
text key
with
773 state.textentry <- None
;
777 state.textentry <- Some
(c, text, onkey
, ondone);
780 state.textentry <- None
;
783 Glut.postRedisplay
()
786 let special ~key ~x ~
y =
789 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
790 | Glut.KEY_UP
-> clamp ~
-aincr
791 | Glut.KEY_DOWN
-> clamp aincr
792 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
793 | Glut.KEY_PAGE_DOWN
-> clamp state.h
794 | Glut.KEY_HOME
-> addnav (); 0
795 | Glut.KEY_END
-> addnav (); state.maxy (* - state.h *)
802 let drawplaceholder l =
805 GlDraw.color
(0.2, 0.2, 0.2);
806 GlDraw.color
(1.0, 1.0, 1.0);
808 (0.0, float l.pagedispy
)
809 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
812 and y = float (l.pagedispy
+ l.pagevh
- 5) in
813 let font = Glut.BITMAP_8_BY_13
in
814 GlDraw.color
(0.0, 0.0, 0.0);
815 GlPix.raster_pos ~
x ~
y ();
816 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
817 ("Loading " ^ string_of_int
l.pageno);
820 GlDraw.begins `quads
;
821 GlDraw.vertex2
(0.0, float l.pagedispy
);
822 GlDraw.vertex2
(float l.pagew
, float l.pagedispy
);
823 GlDraw.vertex2
(float l.pagew
, float (l.pagedispy
+ l.pagevh
));
824 GlDraw.vertex2
(0.0, float (l.pagedispy
+ l.pagevh
));
829 let now () = Unix.gettimeofday
();;
832 begin match getopaque l.pageno with
833 | Some opaque
when validopaque opaque
->
834 GlDraw.color
(1.0, 1.0, 1.0);
838 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
842 vlog "draw %f sec safe %d" d state.safe
;
848 if state.inflight
< cblen state.pagecache
850 List.iter
preload state.layout;
854 vlog "inflight %d" state.inflight
;
857 GlDraw.color
(0.5, 0.5, 0.5);
860 (float (state.w
- conf.scrollw
), float (i
+ (l.pagedispy
- i
)))
862 l.pagedispy
+ l.pagevh
;
865 let scrollindicator () =
866 GlDraw.color
(0.64 , 0.64, 0.64);
868 (float (state.w
- conf.scrollw
), 0.)
869 (float state.w
, float state.h)
871 GlDraw.color
(0.0, 0.0, 0.0);
872 let sh = (float (state.maxy + state.h) /. float state.h) in
873 let sh = float state.h /. sh in
874 let sh = max
sh (float scrollh) in
876 let percent = yratio state.y in
877 let position = (float state.h -. sh) *. percent in
880 if position +. sh > float state.h
887 (float (state.w
- conf.scrollw
), position)
888 (float state.w
, position +. sh)
893 match state.mstate
with
897 | Msel
((x0
, y0
), (x1
, y1
)) ->
899 and y1
= max
y0 y1
in
902 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
903 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
905 match getopaque l.pageno with
906 | Some opaque
when validopaque opaque
->
907 let oy = -l.pagey
+ l.pagedispy
in
908 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
911 List.iter
f state.layout
916 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
917 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
919 (fun (pageno, c, (x0
, y0), (x1
, y1
)) ->
923 let d = float (l.pagedispy
- l.pagey
) in
924 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
925 GlDraw.rect
(x0
, y0 +. d) (x1
, y1
+. d)
934 ignore
(List.fold_left
drawpage 0 (state.layout));
938 let len = String.length
state.text in
939 begin match state.textentry with
941 if len> 0 then showtext ' '
state.text
943 | Some
(c, text, _
, _
) ->
947 text ^
" [" ^
state.text ^
"]"
959 begin match getopaque l.pageno with
960 | Some opaque
when validopaque opaque
->
961 let y = y - l.pagedispy
in
964 let y = l.pagey
+ y in
965 let destpage = checklink opaque
x y in
982 let y = getpagey n in
988 let mouse ~button ~bstate ~
x ~
y =
990 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
998 let incr = incr * 2 in
999 let y = clamp incr in
1002 | Glut.LEFT_BUTTON
->
1003 let destpage = if bstate
= Glut.DOWN
then getlink x y else - 1 in
1008 if bstate
= Glut.DOWN
1010 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1011 state.mstate
<- Msel
((x, y), (x, y));
1012 Glut.postRedisplay
()
1015 Glut.setCursor
Glut.CURSOR_RIGHT_ARROW
;
1016 state.mstate
<- Mnone
;
1023 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1026 match state.mstate
with
1029 state.mstate
<- Msel
(a, (x, y));
1030 Glut.postRedisplay
()
1034 match state.mstate
with
1036 let pageno = getlink x y in
1039 Glut.setCursor
Glut.CURSOR_RIGHT_ARROW
1041 Glut.setCursor
Glut.CURSOR_INFO
1048 let name = ref "" in
1049 Arg.parse
[] (fun s -> name := s) "options:";
1051 if String.length
!name = 0
1052 then (prerr_endline
"filename missing"; exit
1)
1058 let _ = Glut.init
Sys.argv
in
1059 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1060 let () = Glut.initWindowSize
w h in
1061 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1063 let csock, ssock
= Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0 in
1068 state.csock <- csock;
1069 state.ssock
<- ssock
;
1070 writecmd csock ("open " ^
name ^
"\000");
1072 let () = Glut.displayFunc
display in
1073 let () = Glut.reshapeFunc
reshape in
1074 let () = Glut.keyboardFunc
keyboard in
1075 let () = Glut.specialFunc
special in
1076 let () = Glut.idleFunc
(Some
idle) in
1077 let () = Glut.mouseFunc
mouse in
1078 let () = Glut.motionFunc
motion in
1079 let () = Glut.passiveMotionFunc
pmotion in