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 <- min
(b
.len + 1) 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
88 ; mutable autoscroll
: bool
89 ; mutable showall
: bool
93 type outline
= string * int * int * int;;
94 type outlines
= Oarray
of outline array
| Olist
of outline list
;;
97 { mutable csock
: Unix.file_descr
98 ; mutable ssock
: Unix.file_descr
101 ; mutable rotate
: int
104 ; mutable prevy
: int
106 ; mutable layout
: layout list
107 ; pagemap
: ((int * int * int), string) Hashtbl.t
108 ; mutable pages
: (int * int * int) list
109 ; mutable pagecount
: int
110 ; pagecache
: string circbuf
111 ; navhist
: float circbuf
112 ; mutable inflight
: int
113 ; mutable mstate
: mstate
114 ; mutable searchpattern
: string
115 ; mutable rects
: (int * int * Gl.point2
* Gl.point2
) list
116 ; mutable rects1
: (int * int * Gl.point2
* Gl.point2
) list
117 ; mutable text
: string
118 ; mutable fullscreen
: (int * int) option
119 ; mutable textentry
: textentry
option
120 ; mutable outlines
: outlines
121 ; mutable outline
: (bool * int * int * outline array
* string) option
122 ; mutable bookmarks
: outline list
123 ; mutable path
: string
155 ; pagemap
= Hashtbl.create
10
156 ; pagecache
= cbnew 10 ""
161 ; navhist
= cbnew 100 0.0
168 ; outlines
= Olist
[]
178 Printf.kprintf prerr_endline fmt
180 Printf.kprintf ignore fmt
184 let len = String.length s
in
186 let b = Buffer.create
n in
187 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
188 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
189 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
190 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
191 Buffer.add_string
b s
;
192 let s'
= Buffer.contents
b in
193 let n'
= Unix.write fd
s'
0 n in
194 if n'
!= n then failwith
"write failed";
199 let n = Unix.read fd
s 0 4 in
200 if n != 4 then failwith
"incomplete read(len)";
202 lor (Char.code
s.[0] lsl 24)
203 lor (Char.code
s.[1] lsl 16)
204 lor (Char.code
s.[2] lsl 8)
205 lor (Char.code
s.[3] lsl 0)
207 let s = String.create
len in
208 let n = Unix.read fd
s 0 len in
209 if n != len then failwith
"incomplete read(data)";
214 if y
= state.maxy
then 1.0
215 else float y
/. float state.maxy
219 let b = Buffer.create
10 in
220 Buffer.add_string
b s;
221 let rec combine = function
224 Buffer.add_char
b ' '
;
227 | `
b b -> if b then "1" else "0"
229 | `i i
-> string_of_int i
230 | `f f
-> string_of_float f
231 | `I f
-> string_of_int
(truncate f
)
233 Buffer.add_string
b s;
240 let cmd = Buffer.contents
(makecmd s l
) in
241 writecmd state.csock
cmd;
245 let rec f pn ph fh l
=
247 | (n, _
, h
) :: rest
->
248 let fh = fh + (n - pn
) * ph
in
252 let fh = fh + (ph
* (state.pagecount
- pn
)) in
255 let fh = f 0 0 0 state.pages
in
259 let getpagey pageno
=
260 let rec f pn ph y l
=
262 | (n, _
, h
) :: rest
->
265 y
+ (pageno
- pn
) * ph
267 let y = y + (n - pn
) * ph
in
271 y + (pageno
- pn
) * ph
277 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
278 if pageno
= state.pagecount
|| cacheleft
= 0
281 let ((_
, w
, h
) as curr
), rest
, pdimno
=
283 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
284 curr
, rest
, pdimno
+ 1
288 let pageno'
= pageno + 1 in
322 let accu = e :: accu in
323 f pageno' pdimno curr
324 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
325 (pred cacheleft
) accu
327 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
329 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
330 state.maxy
<- calcheight ();
335 let y = state.y + incr
in
337 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
341 let getopaque pageno =
342 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
344 with Not_found
-> None
347 let cache pageno opaque
=
348 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
352 let validopaque opaque
= String.length opaque
> 0;;
355 match getopaque l
.pageno with
356 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
357 state.inflight
<- succ
state.inflight
;
359 wcmd "render" [`i
(l
.pageno + 1)
369 let y = min
state.maxy
y in
370 let pages = layout y state.h
in
371 let rec f all
= function
373 begin match getopaque l
.pageno with
374 | None
-> preload l
; f false ls
375 | Some opaque
-> f (all
&& validopaque opaque
) ls
379 if not
conf.showall
|| f true pages
382 state.layout <- pages;
387 Glut.postRedisplay
()
392 cbput state.navhist
(yratio state.y);
393 cbrfollowlen state.navhist
;
397 let y = cbget state.navhist
in
398 truncate
(y *. float state.maxy
)
402 let y = getpagey n in
409 let ratio = float w
/. float state.w
in
410 let fixbookmark (s, l
, pageno, pagey
) =
411 let pagey = truncate
(float pagey *. ratio) in
412 (s, l
, pageno, pagey)
414 state.bookmarks
<- List.map
fixbookmark state.bookmarks
;
417 GlDraw.viewport
0 0 w h
;
418 GlMat.mode `modelview
;
419 GlMat.load_identity
();
420 GlMat.mode `projection
;
421 GlMat.load_identity
();
422 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
423 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
424 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
425 GlClear.color
(1., 1., 1.);
426 GlClear.clear
[`color
];
431 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
435 GlDraw.color
(0.0, 0.0, 0.0);
437 (0.0, float (state.h
- 18))
438 (float (state.w
- conf.scrollw
- 1), float state.h
)
440 let font = Glut.BITMAP_8_BY_13
in
441 GlDraw.color
(1.0, 1.0, 1.0);
442 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
443 Glut.bitmapCharacter ~
font ~c
:(Char.code c
);
444 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s;
448 let len = String.length
state.text
in
449 match state.textentry
with
451 if len > 0 then showtext ' '
state.text
453 | Some
(c
, text
, _
, _
) ->
457 text ^
" [" ^
state.text ^
"]"
468 state.outlines
<- Olist
[]
471 state.rects
<- state.rects1
;
472 Glut.postRedisplay
()
475 state.rects
<- state.rects1
;
476 Glut.postRedisplay
()
479 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
480 state.pagecount
<- n;
481 let rely = yratio state.y in
482 let maxy = calcheight () in
483 state.y <- truncate
(float maxy *. rely);
484 let pages = layout state.y state.h
in
485 state.layout <- pages;
486 Glut.postRedisplay
();
489 let s = Scanf.sscanf
cmd "t %n"
490 (fun n -> String.sub
cmd n (String.length
cmd - n))
492 Glut.setWindowTitle
s
495 let s = Scanf.sscanf
cmd "T %n"
496 (fun n -> String.sub
cmd n (String.length
cmd - n))
505 let s = Scanf.sscanf
cmd "V %n"
506 (fun n -> String.sub
cmd n (String.length
cmd - n))
513 let pageno, c
, x0
, y0
, x1
, y1
=
514 Scanf.sscanf
cmd "F %d %d %f %f %f %f"
515 (fun p c x0 y0 x1 y1
-> (p
, c
, x0
, y0
, x1
, y1
))
517 let y = (getpagey pageno) + truncate y0
in
520 state.rects1
<- [pageno, c
, (x0
, y0
), (x1
, y1
)]
523 let pageno, c
, x0
, y0
, x1
, y1
=
524 Scanf.sscanf
cmd "R %d %d %f %f %f %f"
525 (fun pageno c x0 y0 x1 y1
-> (pageno, c
, x0
, y0
, x1
, y1
))
527 state.rects1
<- (pageno, c
, (x0
, y0
), (x1
, y1
)) :: state.rects1
531 Scanf.sscanf
cmd "r %d %d %d %d %s"
532 (fun n w h r p
-> (n, w
, h
, r
, p
))
534 Hashtbl.replace
state.pagemap
(n, w
, r
) p
;
535 let evicted = cbpeekw state.pagecache
in
536 if String.length
evicted > 0
538 wcmd "free" [`
s evicted];
539 let l = Hashtbl.fold
(fun k p a
->
540 if evicted = p
then k
:: a
else a
) state.pagemap
[]
542 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
544 cbput state.pagecache p
;
545 state.inflight
<- pred
state.inflight
;
546 if conf.showall
then gotoy state.ty
;
547 Glut.postRedisplay
()
550 let (n, w
, h
) as pagelayout
=
551 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
553 state.pages <- pagelayout
:: state.pages
557 Scanf.sscanf
cmd "o %d %d %d %n" (fun l n t pos
-> l, n, t
, pos
)
559 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
560 let outline = (s, l, n, t
) in
562 match state.outlines with
563 | Olist
outlines -> Olist
(outline :: outlines)
564 | Oarray _
-> Olist
[outline]
566 state.outlines <- outlines
569 log "unknown cmd `%S'" cmd
573 if not
conf.redispimm
&& state.y != state.prevy
575 state.prevy
<- state.y;
576 Glut.postRedisplay
();
579 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.001 in
583 if conf.preload then begin
585 let y = if state.y < state.h then 0 else state.y - state.h in
586 let pages = layout y (h*3) in
587 List.iter
preload pages;
589 if conf.autoscroll
then begin
590 let y = state.y + conf.scrollincr
in
591 let y = if y >= state.maxy then 0 else y in
594 state.prevy
<- state.y;
595 Glut.postRedisplay
();
599 let cmd = readcmd state.csock
in
604 let search pattern forward
=
605 if String.length pattern
> 0
608 match state.layout with
611 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
614 let b = makecmd "search"
615 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
617 Buffer.add_char
b '
,'
;
618 Buffer.add_string
b pattern
;
619 Buffer.add_char
b '
\000'
;
622 writecmd state.csock
cmd;
625 let intentry text key
=
626 let c = Char.unsafe_chr key
in
629 let s = "x" in s.[0] <- c;
630 let text = text ^
s in
634 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
639 let b = Buffer.create
(String.length
s + 1) in
640 Buffer.add_string
b s;
645 let textentry text key
=
646 let c = Char.unsafe_chr key
in
648 | _
when key
>= 32 && key
< 127 ->
649 let text = addchar text c in
653 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
657 let optentry text key
=
658 let btos b = if b then "on" else "off" in
659 let c = Char.unsafe_chr key
in
662 conf.rectsel
<- not
conf.rectsel
;
663 TEdone
("rectsel " ^
(btos conf.rectsel
))
667 try conf.scrollincr
<- int_of_string
s with exc
->
668 state.text <- Printf.sprintf
"bad integer `%s': %s"
669 s (Printexc.to_string exc
)
671 TEswitch
('#'
, "", intentry, ondone)
676 state.rotate
<- int_of_string
s;
677 wcmd "rotate" [`i
state.rotate
]
679 state.text <- Printf.sprintf
"bad integer `%s': %s"
680 s (Printexc.to_string exc
)
682 TEswitch
('^'
, "", intentry, ondone)
685 conf.icase
<- not
conf.icase
;
686 TEdone
("case insensitive search " ^
(btos conf.icase
))
689 conf.preload <- not
conf.preload;
690 TEdone
("preload " ^
(btos conf.preload))
693 conf.redispimm
<- not
conf.redispimm
;
694 TEdone
("immediate redisplay " ^
(btos conf.redispimm
))
697 conf.verbose
<- not
conf.verbose
;
698 TEdone
("verbose " ^
(btos conf.verbose
))
701 conf.maxhfit
<- not
conf.maxhfit
;
702 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
703 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
706 conf.crophack
<- not
conf.crophack
;
707 TEdone
("crophack " ^
btos conf.crophack
)
710 conf.showall
<- not
conf.showall
;
711 TEdone
("showall " ^
btos conf.showall
)
714 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
718 let maxoutlinerows () = (state.h - 31) / 16;;
720 let enterselector allowdel
outlines errmsg
=
721 if Array.length
outlines = 0
728 match state.layout with
730 | {pageno=pageno} :: rest
-> pageno
734 if n = Array.length
outlines
737 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
738 if outlinepageno
>= pageno then n else loop (n+1)
743 Some
(allowdel
, active, max
0 (active - maxoutlinerows ()), outlines, "");
744 Glut.postRedisplay
();
747 let enteroutlinemode () =
749 match state.outlines with
752 let a = Array.of_list
(List.rev
l) in
753 state.outlines <- Oarray
a;
756 enterselector false outlines "Documents has no outline";
759 let enterbookmarkmode () =
760 let bookmarks = Array.of_list
state.bookmarks in
761 enterselector true bookmarks "Documents has no bookmarks (yet)";
765 let quickbookmark ?title
() =
766 match state.layout with
772 let sec = Unix.gettimeofday
() in
773 let tm = Unix.localtime
sec in
774 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
778 (tm.Unix.tm_year
+ 1900)
781 | Some
title -> title
784 (title, 0, l.pageno, l.pagey) :: state.bookmarks
787 let viewkeyboard ~key ~x ~
y =
789 state.textentry <- te
;
792 Glut.postRedisplay
()
794 match state.textentry with
796 let c = Char.chr key
in
811 Glut.postRedisplay
()
814 let ondone isforw
s =
815 state.searchpattern
<- s;
818 enttext (Some
(c, "", textentry, ondone (c ='
/'
)))
823 try int_of_string
s with exc
->
824 state.text <- Printf.sprintf
"bad integer `%s': %s"
825 s (Printexc.to_string exc
);
831 state.text <- "page bias is now " ^ string_of_int
n;
834 enttext (Some
('
+'
, "", intentry, ondone))
840 enttext (Some
('
-'
, "", optentry, ondone))
845 try int_of_string
s with exc
->
846 state.text <- Printf.sprintf
"bad integer `%s': %s"
847 s (Printexc.to_string exc
);
854 gotoy (getpagey (n + conf.pagebias
- 1))
857 let pageentry text key
=
858 match Char.unsafe_chr key
with
860 | _
-> intentry text key
862 let text = "x" in text.[0] <- c;
863 enttext (Some
('
:'
, text, pageentry, ondone))
866 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
867 reshape state.w
state.h;
870 conf.autoscroll
<- not
conf.autoscroll
873 begin match state.fullscreen
with
875 state.fullscreen
<- Some
(state.w
, state.h);
878 state.fullscreen
<- None
;
879 Glut.reshapeWindow ~w ~
h
886 search state.searchpattern
true
889 search state.searchpattern
false
892 begin match state.layout with
895 gotoy (state.y - l.pagey);
899 begin match List.rev
state.layout with
902 gotoy (clamp (l.pageh
- l.pagey))
906 begin match state.layout with
909 gotoy (clamp (-l.pageh
));
914 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
916 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
918 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
922 else (100. *. (float state.y /. float maxy)) in
925 Printf.sprintf
"Page %d of %d %.2f%%"
926 (fn+1) state.pagecount
percent
929 "Pages %d-%d of %d %.2f%%"
930 (fn+1) (ln
+1) state.pagecount
percent
936 begin match state.layout with
939 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
940 Glut.postRedisplay
();
948 match state.layout with
950 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
953 enttext (Some
('~'
, "", textentry, ondone))
957 showtext ' '
"Quick bookmark added";
961 begin match state.layout with
963 let a = getpagewh
l.pagedimno
in
967 (truncate
(1.8 *. (a.(1) -. a.(0))),
968 truncate
(1.4 *. (a.(3) -. a.(0))))
970 (truncate
(a.(1) -. a.(0)),
971 truncate
(a.(3) -. a.(0)))
973 Glut.reshapeWindow
(w + conf.scrollw
) h;
974 Glut.postRedisplay
();
980 state.rotate
<- state.rotate
+ (if c = '
>'
then 30 else -30);
981 wcmd "rotate" [`i
state.rotate
]
984 vlog "huh? %d %c" key
(Char.chr key
);
987 | Some
(c, text, onkey
, ondone) when key
= 8 ->
988 let len = String.length
text in
991 state.textentry <- None
;
992 Glut.postRedisplay
();
995 let s = String.sub
text 0 (len - 1) in
996 enttext (Some
(c, s, onkey
, ondone))
999 | Some
(c, text, onkey
, ondone) ->
1000 begin match Char.unsafe_chr key
with
1003 state.textentry <- None
;
1004 Glut.postRedisplay
()
1007 state.textentry <- None
;
1008 Glut.postRedisplay
()
1011 begin match onkey
text key
with
1013 state.textentry <- None
;
1015 Glut.postRedisplay
()
1018 enttext (Some
(c, text, onkey
, ondone));
1021 state.textentry <- None
;
1022 Glut.postRedisplay
()
1025 state.textentry <- Some te
;
1026 Glut.postRedisplay
()
1031 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1032 let search active pattern incr
=
1035 if n = Array.length
outlines || n = -1 then None
else
1036 let (s, _
, _
, _
) = outlines.(n) in
1038 (try ignore
(Str.search_forward re
s 0); true
1039 with Not_found
-> false)
1041 let maxrows = (min
(Array.length
outlines) (maxoutlinerows ())) / 2 in
1043 then Some
(n, max
0 (n - maxrows))
1044 else Some
(n, max first
(n - maxrows))
1046 else loop (n + incr
)
1051 let re = Str.regexp_case_fold pattern
in
1059 if String.length qsearch
= 0
1062 state.outline <- None
;
1063 Glut.postRedisplay
();
1067 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1068 Glut.postRedisplay
();
1072 let incr = if key
= 18 then -1 else 1 in
1074 match search (active + incr) qsearch
incr with
1076 state.text <- qsearch ^
" [not found]";
1079 state.text <- qsearch
;
1082 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1083 Glut.postRedisplay
();
1086 let len = String.length qsearch
in
1093 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1096 let qsearch = String.sub
qsearch 0 (len - 1) in
1098 match search active qsearch ~
-1 with
1100 state.text <- qsearch ^
" [not found]";
1103 state.text <- qsearch;
1106 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1108 Glut.postRedisplay
()
1111 if active < Array.length
outlines
1113 let (_
, _
, n, t
) = outlines.(active) in
1117 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1118 state.outline <- None
;
1119 Glut.postRedisplay
();
1121 | _
when key
>= 32 && key
< 127 ->
1122 let pattern = addchar qsearch (Char.chr key
) in
1124 match search active pattern 1 with
1126 state.text <- pattern ^
" [not found]";
1128 | Some
(active, first
) ->
1129 state.text <- pattern;
1132 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1133 Glut.postRedisplay
()
1135 | 127 when allowdel
->
1136 let len = Array.length
outlines - 1 in
1139 state.outline <- None
;
1140 state.bookmarks <- [];
1143 let bookmarks = Array.init
len
1145 let i = if i >= active then i + 1 else i in
1156 Glut.postRedisplay
()
1158 | _
-> log "unknown key %d" key
1161 let keyboard ~key ~x ~
y =
1162 match state.outline with
1163 | None
-> viewkeyboard ~key ~x ~
y
1164 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1167 let special ~key ~x ~
y =
1168 match state.outline with
1172 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1173 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1174 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1175 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1176 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1177 | Glut.KEY_HOME
-> addnav (); 0
1180 state.maxy - (if conf.maxhfit
then state.h else 0)
1186 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1187 let maxrows = maxoutlinerows () in
1189 let active = active + incr in
1190 let active = max
0 (min
active (Array.length
outlines - 1)) in
1194 let rows = active - first in
1195 if rows > maxrows then first + incr else first
1198 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1199 Glut.postRedisplay
()
1202 | Glut.KEY_UP
-> navigate ~
-1
1203 | Glut.KEY_DOWN
-> navigate 1
1204 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1205 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1208 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1209 Glut.postRedisplay
()
1212 let active = Array.length
outlines - 1 in
1213 let first = max
0 (active - maxrows) in
1214 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1215 Glut.postRedisplay
()
1220 let drawplaceholder l =
1221 GlDraw.color
(1.0, 1.0, 1.0);
1223 (0.0, float l.pagedispy
)
1224 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1227 and y = float (l.pagedispy
+ 13) in
1228 let font = Glut.BITMAP_8_BY_13
in
1229 GlDraw.color
(0.0, 0.0, 0.0);
1230 GlPix.raster_pos ~
x ~
y ();
1231 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1232 ("Loading " ^ string_of_int
l.pageno);
1235 let now () = Unix.gettimeofday
();;
1238 begin match getopaque l.pageno with
1239 | Some opaque
when validopaque opaque
->
1240 if state.textentry = None
1241 then GlDraw.color
(1.0, 1.0, 1.0)
1242 else GlDraw.color
(0.4, 0.4, 0.4);
1244 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1247 vlog "draw %f sec" d;
1254 if state.inflight
< cblen state.pagecache
1256 List.iter
preload state.layout;
1259 vlog "inflight %d" state.inflight
;
1262 GlDraw.color
(0.5, 0.5, 0.5);
1265 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1267 l.pagedispy
+ l.pagevh
;
1270 let scrollindicator () =
1271 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1272 GlDraw.color
(0.64 , 0.64, 0.64);
1274 (float (state.w - conf.scrollw
), 0.)
1275 (float state.w, float state.h)
1277 GlDraw.color
(0.0, 0.0, 0.0);
1278 let sh = (float (maxy + state.h) /. float state.h) in
1279 let sh = float state.h /. sh in
1280 let sh = max
sh (float conf.scrollh
) in
1283 if state.y = state.maxy
1285 else float state.y /. float maxy
1287 let position = (float state.h -. sh) *. percent in
1290 if position +. sh > float state.h
1297 (float (state.w - conf.scrollw
), position)
1298 (float state.w, position +. sh)
1303 match state.mstate
with
1307 | Msel
((x0
, y0
), (x1
, y1
)) ->
1309 and y1
= max
y0 y1
in
1312 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1313 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1315 match getopaque l.pageno with
1316 | Some opaque
when validopaque opaque
->
1317 let oy = -l.pagey + l.pagedispy
in
1318 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1321 List.iter
f state.layout
1326 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1327 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1329 (fun (pageno, c, (x0
, y0), (x1
, y1
)) ->
1331 if l.pageno = pageno
1333 let d = float (l.pagedispy
- l.pagey) in
1334 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1335 GlDraw.rect
(x0
, y0 +. d) (x1
, y1
+. d)
1343 let showoutline = function
1345 | Some
(allowdel
, active, first, outlines, qsearch) ->
1347 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1348 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1349 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1352 GlDraw.color
(1., 1., 1.);
1353 let font = Glut.BITMAP_9_BY_15
in
1354 let draw_string x y s =
1355 GlPix.raster_pos ~
x ~
y ();
1356 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1359 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1362 let (s, l, _
, _
) = outlines.(row
) in
1363 let y = (row
- first) * 16 in
1368 GlDraw.polygon_mode `both `line
;
1369 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1370 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1371 GlDraw.rect
(0., float (y + 1))
1372 (float (state.w - conf.scrollw
- 1), float (y + 18));
1373 GlDraw.polygon_mode `both `fill
;
1375 GlDraw.color
(1., 1., 1.);
1377 draw_string (float x) (float (y + 16)) s;
1385 let lasty = List.fold_left
drawpage 0 (state.layout) in
1386 GlDraw.color
(0.5, 0.5, 0.5);
1389 (float (state.w - conf.scrollw
), float state.h)
1394 showoutline state.outline;
1396 Glut.swapBuffers
();
1400 let rec f = function
1402 begin match getopaque l.pageno with
1403 | Some opaque
when validopaque opaque
->
1404 let y = y - l.pagedispy
in
1407 let y = l.pagey + y in
1408 match getlink opaque
x y with
1422 let rec f = function
1424 begin match getopaque l.pageno with
1425 | Some opaque
when validopaque opaque
->
1426 let y = y - l.pagedispy
in
1429 let y = l.pagey + y in
1430 if checklink opaque
x y then true else f rest
1441 let mouse ~button ~bstate ~
x ~
y =
1443 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1451 let incr = incr * 2 in
1452 let y = clamp incr in
1455 | Glut.LEFT_BUTTON
when state.outline = None
->
1456 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1457 begin match dest with
1458 | Some
(pageno, top
) ->
1462 if bstate
= Glut.DOWN
1464 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1465 state.mstate
<- Msel
((x, y), (x, y));
1466 Glut.postRedisplay
()
1469 Glut.setCursor
Glut.CURSOR_INHERIT
;
1470 state.mstate
<- Mnone
;
1477 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1480 if state.outline = None
1482 match state.mstate
with
1485 state.mstate
<- Msel
(a, (x, y));
1486 Glut.postRedisplay
()
1490 if state.outline = None
1492 match state.mstate
with
1493 | Mnone
when (checklink x y) ->
1494 Glut.setCursor
Glut.CURSOR_INFO
1497 Glut.setCursor
Glut.CURSOR_INHERIT
1506 if Sys.os_type
= "Win32"
1508 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1510 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1512 Filename.concat
home "llpp"
1516 let ic = open_in_bin
statepath in
1517 let hash = input_value
ic in
1523 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1530 match state.fullscreen
with
1531 | None
-> state.w, state.h
1534 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1535 let oc = open_out_bin
statepath in
1536 output_value
oc pstate
1540 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1545 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1548 state.bookmarks <- statebookmarks;
1549 with Not_found
-> ()
1551 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1554 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1556 if String.length
state.path
= 0
1557 then (prerr_endline
"filename missing"; exit
1)
1562 let _ = Glut.init
Sys.argv
in
1563 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1564 let () = Glut.initWindowSize
state.w state.h in
1565 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1568 if Sys.os_type
= "Unix"
1570 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1572 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1573 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1574 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1575 Unix.bind
sock addr;
1577 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1578 Unix.connect
csock addr;
1579 let ssock, _ = Unix.accept
sock in
1582 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1583 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1587 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1591 let () = Glut.displayFunc
display in
1592 let () = Glut.reshapeFunc
reshape in
1593 let () = Glut.keyboardFunc
keyboard in
1594 let () = Glut.specialFunc
special in
1595 let () = Glut.idleFunc
(Some
idle) in
1596 let () = Glut.mouseFunc
mouse in
1597 let () = Glut.motionFunc
motion in
1598 let () = Glut.passiveMotionFunc
pmotion in
1601 state.csock <- csock;
1602 state.ssock <- ssock;
1603 writecmd csock ("open " ^
name ^
"\000");
1607 let rec handlelablglutbug () =
1610 with Glut.BadEnum
"key in special_of_int" ->
1611 showtext '
!'
" LablGlut bug: special key not recognized";
1612 Glut.swapBuffers
();
1613 handlelablglutbug ()
1615 handlelablglutbug ();