1 type link
= | LNone
| LUri
of string | LGoto
of (int * int);;
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 gettext
: string -> (int * int * int * int) -> int -> bool -> unit =
10 external getlink
: string -> int -> int -> link
= "ml_getlink";;
11 external highlightlinks
: string -> int -> unit = "ml_highlightlinks";;
12 external getpagewh
: int -> float array
= "ml_getpagewh";;
14 type mstate
= Msel
of ((int * int) * (int * int)) | Mnone
;;
24 type textentry
= (char
* string * onhist
option * onkey
* ondone
)
25 and onkey
= string -> int -> te
26 and ondone
= string -> unit
27 and onhist
= histcmd
-> string
28 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
33 | TEswitch
of textentry
37 { store
= Array.create n v
44 let cblen b
= Array.length b
.store
;;
49 b
.wc
<- (b
.wc
+ 1) mod len;
50 b
.len <- min
(b
.len + 1) len;
53 let cbpeekw b
= b
.store
.(b
.wc
);;
56 if b
.len = 0 then b
.store
.(0) else
57 let rc = b
.rc + dir
in
58 let rc = if rc = -1 then b
.len - 1 else rc in
59 let rc = if rc = b
.len then 0 else rc in
80 { mutable scrollw
: int
81 ; mutable scrollh
: int
82 ; mutable rectsel
: bool
83 ; mutable icase
: bool
84 ; mutable preload
: bool
85 ; mutable pagebias
: int
86 ; mutable verbose
: bool
87 ; mutable scrollincr
: int
88 ; mutable maxhfit
: bool
89 ; mutable crophack
: bool
90 ; mutable autoscroll
: bool
91 ; mutable showall
: bool
92 ; mutable hlinks
: bool
96 type outline
= string * int * int * int;;
98 | Oarray
of outline array
99 | Olist
of outline list
100 | Onarrow
of outline array
* outline array
103 type rect
= (float * float * float * float * float * float * float * float);;
106 { mutable csock
: Unix.file_descr
107 ; mutable ssock
: Unix.file_descr
110 ; mutable rotate
: int
113 ; mutable prevy
: int
115 ; mutable layout
: layout list
116 ; pagemap
: ((int * int * int), string) Hashtbl.t
117 ; mutable pages
: (int * int * int) list
118 ; mutable pagecount
: int
119 ; pagecache
: string circbuf
120 ; mutable inflight
: int
121 ; mutable mstate
: mstate
122 ; mutable searchpattern
: string
123 ; mutable rects
: (int * int * rect
) list
124 ; mutable rects1
: (int * int * rect
) list
125 ; mutable text
: string
126 ; mutable fullscreen
: (int * int) option
127 ; mutable textentry
: textentry
option
128 ; mutable outlines
: outlines
129 ; mutable outline
: (bool * int * int * outline array
* string) option
130 ; mutable bookmarks
: outline list
131 ; mutable path
: string
132 ; mutable invalidated
: int
136 { pat
: string circbuf
137 ; pag
: string circbuf
138 ; nav
: float circbuf
170 ; pagemap
= Hashtbl.create
10
171 ; pagecache
= cbnew 10 ""
182 ; outlines
= Olist
[]
188 { nav
= cbnew 100 0.0
198 Printf.kprintf prerr_endline fmt
200 Printf.kprintf ignore fmt
204 let len = String.length s
in
206 let b = Buffer.create
n in
207 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
208 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
209 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
210 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
211 Buffer.add_string
b s
;
212 let s'
= Buffer.contents
b in
213 let n'
= Unix.write fd
s'
0 n in
214 if n'
!= n then failwith
"write failed";
219 let n = Unix.read fd
s 0 4 in
220 if n != 4 then failwith
"incomplete read(len)";
222 lor (Char.code
s.[0] lsl 24)
223 lor (Char.code
s.[1] lsl 16)
224 lor (Char.code
s.[2] lsl 8)
225 lor (Char.code
s.[3] lsl 0)
227 let s = String.create
len in
228 let n = Unix.read fd
s 0 len in
229 if n != len then failwith
"incomplete read(data)";
234 if y
= state.maxy
then 1.0
235 else float y
/. float state.maxy
239 let b = Buffer.create
10 in
240 Buffer.add_string
b s;
241 let rec combine = function
244 Buffer.add_char
b ' '
;
247 | `
b b -> if b then "1" else "0"
249 | `i i
-> string_of_int i
250 | `f f
-> string_of_float f
251 | `I f
-> string_of_int
(truncate f
)
253 Buffer.add_string
b s;
260 let cmd = Buffer.contents
(makecmd s l
) in
261 writecmd state.csock
cmd;
265 let rec f pn ph fh l
=
267 | (n, _
, h
) :: rest
->
268 let fh = fh + (n - pn
) * ph
in
272 let fh = fh + (ph
* (state.pagecount
- pn
)) in
275 let fh = f 0 0 0 state.pages
in
279 let getpagey pageno
=
280 let rec f pn ph y l
=
282 | (n, _
, h
) :: rest
->
285 y
+ (pageno
- pn
) * ph
287 let y = y + (n - pn
) * ph
in
291 y + (pageno
- pn
) * ph
297 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
298 if pageno
= state.pagecount
|| cacheleft
= 0
301 let ((_
, w
, h
) as curr
), rest
, pdimno
=
303 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
304 curr
, rest
, pdimno
+ 1
308 let pageno'
= pageno + 1 in
342 let accu = e :: accu in
343 f pageno' pdimno curr
344 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
345 (pred cacheleft
) accu
347 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
349 if state.invalidated
= 0
351 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
352 state.maxy
<- calcheight ();
359 let y = state.y + incr
in
361 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
365 let getopaque pageno =
366 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
368 with Not_found
-> None
371 let cache pageno opaque
=
372 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
376 let validopaque opaque
= String.length opaque
> 0;;
379 match getopaque l
.pageno with
380 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
381 state.inflight
<- succ
state.inflight
;
383 wcmd "render" [`i
(l
.pageno + 1)
393 let y = min
state.maxy
y in
394 let pages = layout y state.h
in
395 let rec f all
= function
397 begin match getopaque l
.pageno with
398 | None
-> preload l
; f false ls
399 | Some opaque
-> f (all
&& validopaque opaque
) ls
403 if not
conf.showall
|| f true pages
406 state.layout <- pages;
409 if conf.preload then begin
411 let y = if state.y < state.h then 0 else state.y - state.h in
412 let pages = layout y (h*3) in
413 List.iter
preload pages;
418 cbput state.hists
.nav
(yratio state.y);
419 cbrfollowlen state.hists
.nav
;
423 let y = cbget state.hists
.nav ~
-1 in
424 truncate
(y *. float state.maxy
)
428 let y = getpagey n in
438 state.invalidated
<- state.invalidated
+ 1;
442 let ratio = float w
/. float state.w
in
443 let fixbookmark (s, l
, pageno, pagey
) =
444 let pagey = truncate
(float pagey *. ratio) in
445 (s, l
, pageno, pagey)
447 state.bookmarks
<- List.map
fixbookmark state.bookmarks
;
450 GlDraw.viewport
0 0 w
h;
451 GlMat.mode `modelview
;
452 GlMat.load_identity
();
453 GlMat.mode `projection
;
454 GlMat.load_identity
();
455 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
456 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
457 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h, 1.0);
458 GlClear.color
(1., 1., 1.);
459 GlClear.clear
[`color
];
461 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i
h];
465 GlDraw.color
(0.0, 0.0, 0.0);
467 (0.0, float (state.h - 18))
468 (float (state.w
- conf.scrollw
- 1), float state.h)
470 let font = Glut.BITMAP_8_BY_13
in
471 GlDraw.color
(1.0, 1.0, 1.0);
472 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h - 5)) ();
473 Glut.bitmapCharacter ~
font ~c
:(Char.code c
);
474 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s;
478 let len = String.length
state.text
in
479 match state.textentry
with
481 if len > 0 then showtext ' '
state.text
483 | Some
(c
, text
, _
, _
, _
) ->
487 text ^
" [" ^
state.text ^
"]"
498 state.outlines
<- Olist
[]
501 state.rects
<- state.rects1
;
502 Glut.postRedisplay
()
505 state.rects
<- state.rects1
;
506 Glut.postRedisplay
()
509 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
510 state.pagecount
<- n;
511 state.invalidated
<- state.invalidated
- 1;
512 if state.invalidated
= 0
514 let rely = yratio state.y in
515 let maxy = calcheight () in
516 state.y <- truncate
(float maxy *. rely);
518 let pages = layout state.y state.h in
519 state.layout <- pages;
520 Glut.postRedisplay
();
524 let s = Scanf.sscanf
cmd "t %n"
525 (fun n -> String.sub
cmd n (String.length
cmd - n))
527 Glut.setWindowTitle
s
530 let s = Scanf.sscanf
cmd "T %n"
531 (fun n -> String.sub
cmd n (String.length
cmd - n))
533 if state.textentry
= None
541 Glut.postRedisplay
();
547 let s = Scanf.sscanf
cmd "V %n"
548 (fun n -> String.sub
cmd n (String.length
cmd - n))
555 let pageno, c
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
556 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
557 (fun p c x0 y0 x1 y1 x2 y2 x3 y3
->
558 (p
, c
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
560 let y = (getpagey pageno) + truncate y0
in
563 state.rects1
<- [pageno, c
, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
566 let pageno, c
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
567 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
568 (fun p c x0 y0 x1 y1 x2 y2 x3 y3
->
569 (p
, c
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
572 (pageno, c
, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
576 Scanf.sscanf
cmd "r %d %d %d %d %s"
577 (fun n w
h r p
-> (n, w
, h, r
, p
))
579 Hashtbl.replace
state.pagemap
(n, w
, r
) p
;
580 let evicted = cbpeekw state.pagecache
in
581 if String.length
evicted > 0
583 wcmd "free" [`
s evicted];
584 let l = Hashtbl.fold
(fun k p a
->
585 if evicted = p
then k
:: a
else a
) state.pagemap
[]
587 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
589 cbput state.pagecache p
;
590 state.inflight
<- pred
state.inflight
;
591 if conf.showall
then gotoy state.ty
;
592 Glut.postRedisplay
()
595 let (n, w
, h) as pagelayout
=
596 Scanf.sscanf
cmd "l %d %d %d" (fun n w
h -> n, w
, h)
598 state.pages <- pagelayout
:: state.pages
602 Scanf.sscanf
cmd "o %d %d %d %n" (fun l n t pos
-> l, n, t
, pos
)
604 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
605 let outline = (s, l, n, t
) in
607 match state.outlines with
608 | Olist
outlines -> Olist
(outline :: outlines)
609 | Oarray _
-> Olist
[outline]
610 | Onarrow _
-> Olist
[outline]
612 state.outlines <- outlines
615 log "unknown cmd `%S'" cmd
619 if state.y != state.prevy
621 state.prevy
<- state.y;
622 Glut.postRedisplay
();
625 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.001 in
629 if conf.autoscroll
then begin
630 let y = state.y + conf.scrollincr
in
631 let y = if y >= state.maxy then 0 else y in
634 state.prevy
<- state.y;
635 Glut.postRedisplay
();
639 let cmd = readcmd state.csock
in
644 let onhist cb
= function
645 | HCprev
-> cbget cb ~
-1
646 | HCnext
-> cbget cb
1
647 | HCfirst
-> cbget cb ~
-(cb
.rc)
648 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
651 let search pattern forward
=
652 if String.length pattern
> 0
655 match state.layout with
658 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
661 let b = makecmd "search"
662 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
664 Buffer.add_char
b '
,'
;
665 Buffer.add_string
b pattern
;
666 Buffer.add_char
b '
\000'
;
669 writecmd state.csock
cmd;
672 let intentry text key
=
673 let c = Char.unsafe_chr key
in
676 let s = "x" in s.[0] <- c;
677 let text = text ^
s in
681 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
686 let b = Buffer.create
(String.length
s + 1) in
687 Buffer.add_string
b s;
692 let textentry text key
=
693 let c = Char.unsafe_chr key
in
695 | _
when key
>= 32 && key
< 127 ->
696 let text = addchar text c in
700 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
705 state.rotate <- angle
;
707 wcmd "rotate" [`i angle
];
710 let optentry text key
=
711 let btos b = if b then "on" else "off" in
712 let c = Char.unsafe_chr key
in
715 conf.rectsel
<- not
conf.rectsel
;
716 TEdone
("rectsel " ^
(btos conf.rectsel
))
720 try conf.scrollincr
<- int_of_string
s with exc
->
721 state.text <- Printf.sprintf
"bad integer `%s': %s"
722 s (Printexc.to_string exc
)
724 TEswitch
('#'
, "", None
, intentry, ondone)
729 Some
(int_of_string
s)
731 state.text <- Printf.sprintf
"bad integer `%s': %s"
732 s (Printexc.to_string exc
);
735 | Some angle
-> rotate angle
738 TEswitch
('^'
, "", None
, intentry, ondone)
741 conf.icase
<- not
conf.icase
;
742 TEdone
("case insensitive search " ^
(btos conf.icase
))
745 conf.preload <- not
conf.preload;
747 TEdone
("preload " ^
(btos conf.preload))
750 conf.verbose
<- not
conf.verbose
;
751 TEdone
("verbose " ^
(btos conf.verbose
))
754 conf.maxhfit
<- not
conf.maxhfit
;
755 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
756 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
759 conf.crophack
<- not
conf.crophack
;
760 TEdone
("crophack " ^
btos conf.crophack
)
763 conf.showall
<- not
conf.showall
;
764 TEdone
("showall " ^
btos conf.showall
)
767 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
771 let maxoutlinerows () = (state.h - 31) / 16;;
773 let enterselector allowdel
outlines errmsg
=
774 if Array.length
outlines = 0
781 match state.layout with
783 | {pageno=pageno} :: rest
-> pageno
787 if n = Array.length
outlines
790 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
791 if outlinepageno
>= pageno then n else loop (n+1)
796 Some
(allowdel
, active,
797 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
798 Glut.postRedisplay
();
801 let enteroutlinemode () =
803 match state.outlines with
806 let a = Array.of_list
(List.rev
l) in
807 state.outlines <- Oarray
a;
809 | Onarrow
(a, b) -> a
811 enterselector false outlines "Documents has no outline";
814 let enterbookmarkmode () =
815 let bookmarks = Array.of_list
state.bookmarks in
816 enterselector true bookmarks "Documents has no bookmarks (yet)";
820 let quickbookmark ?title
() =
821 match state.layout with
827 let sec = Unix.gettimeofday
() in
828 let tm = Unix.localtime
sec in
829 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
833 (tm.Unix.tm_year
+ 1900)
836 | Some
title -> title
839 (title, 0, l.pageno, l.pagey) :: state.bookmarks
842 let viewkeyboard ~key ~x ~
y =
844 state.textentry <- te
;
847 Glut.postRedisplay
()
849 match state.textentry with
851 let c = Char.chr key
in
866 Glut.postRedisplay
()
869 let ondone isforw
s =
870 cbput state.hists
.pat
s;
871 cbrfollowlen state.hists
.pat
;
872 state.searchpattern
<- s;
875 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
876 textentry, ondone (c ='
/'
)))
881 try int_of_string
s with exc
->
882 state.text <- Printf.sprintf
"bad integer `%s': %s"
883 s (Printexc.to_string exc
);
889 state.text <- "page bias is now " ^ string_of_int
n;
892 enttext (Some
('
+'
, "", None
, intentry, ondone))
898 enttext (Some
('
-'
, "", None
, optentry, ondone))
903 try int_of_string
s with exc
->
904 state.text <- Printf.sprintf
"bad integer `%s': %s"
905 s (Printexc.to_string exc
);
911 cbput state.hists
.pag
(string_of_int
n);
912 cbrfollowlen state.hists
.pag
;
913 gotoy (getpagey (n + conf.pagebias
- 1))
916 let pageentry text key
=
917 match Char.unsafe_chr key
with
919 | _
-> intentry text key
921 let text = "x" in text.[0] <- c;
922 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
926 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
927 reshape state.w
state.h;
930 conf.hlinks
<- not
conf.hlinks
;
931 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
932 Glut.postRedisplay
()
935 conf.autoscroll
<- not
conf.autoscroll
938 begin match state.fullscreen
with
940 state.fullscreen
<- Some
(state.w
, state.h);
943 state.fullscreen
<- None
;
944 Glut.reshapeWindow ~w ~
h
951 search state.searchpattern
true
954 search state.searchpattern
false
957 begin match state.layout with
960 gotoy (state.y - l.pagey);
964 begin match List.rev
state.layout with
967 gotoy (clamp (l.pageh
- l.pagey))
971 begin match state.layout with
974 gotoy (clamp (-l.pageh
));
979 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
981 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
983 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
987 else (100. *. (float state.y /. float maxy)) in
990 Printf.sprintf
"Page %d of %d %.2f%%"
991 (fn+1) state.pagecount
percent
994 "Pages %d-%d of %d %.2f%%"
995 (fn+1) (ln
+1) state.pagecount
percent
1001 begin match state.layout with
1004 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
1005 Glut.postRedisplay
();
1009 enterbookmarkmode ()
1013 match state.layout with
1015 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1018 enttext (Some
('~'
, "", None
, textentry, ondone))
1022 showtext ' '
"Quick bookmark added";
1026 begin match state.layout with
1028 let a = getpagewh
l.pagedimno
in
1032 (truncate
(1.8 *. (a.(1) -. a.(0))),
1033 truncate
(1.2 *. (a.(3) -. a.(0))))
1035 (truncate
(a.(1) -. a.(0)),
1036 truncate
(a.(3) -. a.(0)))
1038 Glut.reshapeWindow
(w + conf.scrollw
) h;
1039 Glut.postRedisplay
();
1045 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1048 vlog "huh? %d %c" key
(Char.chr key
);
1051 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1052 let len = String.length
text in
1055 state.textentry <- None
;
1056 Glut.postRedisplay
();
1059 let s = String.sub
text 0 (len - 1) in
1060 enttext (Some
(c, s, onhist, onkey
, ondone))
1063 | Some
(c, text, onhist, onkey
, ondone) ->
1064 begin match Char.unsafe_chr key
with
1067 state.textentry <- None
;
1068 Glut.postRedisplay
()
1071 state.textentry <- None
;
1072 Glut.postRedisplay
()
1075 begin match onkey
text key
with
1077 state.textentry <- None
;
1079 Glut.postRedisplay
()
1082 enttext (Some
(c, text, onhist, onkey
, ondone));
1085 state.textentry <- None
;
1086 Glut.postRedisplay
()
1089 state.textentry <- Some te
;
1090 Glut.postRedisplay
()
1095 let narrow outlines pattern
=
1096 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1100 let rec fold accu n =
1101 if n = -1 then accu else
1102 let (s, _
, _
, _
) as o
= outlines.(n) in
1104 if (try ignore
(Str.search_forward re
s 0); true
1105 with Not_found
-> false)
1111 let matched = fold [] (Array.length
outlines - 1) in
1112 if matched = [] then None
else Some
(Array.of_list
matched)
1115 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1116 let search active pattern incr
=
1119 if n = Array.length
outlines || n = -1 then None
else
1120 let (s, _
, _
, _
) = outlines.(n) in
1122 (try ignore
(Str.search_forward re
s 0); true
1123 with Not_found
-> false)
1125 else loop (n + incr
)
1130 let re = Str.regexp_case_fold pattern
in
1136 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1139 if String.length qsearch
= 0
1142 state.outline <- None
;
1143 Glut.postRedisplay
();
1147 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1148 Glut.postRedisplay
();
1152 let incr = if key
= 18 then -1 else 1 in
1154 match search (active + incr) qsearch
incr with
1156 state.text <- qsearch ^
" [not found]";
1159 state.text <- qsearch
;
1160 active, firstof active
1162 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1163 Glut.postRedisplay
();
1166 let len = String.length qsearch
in
1173 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1176 let qsearch = String.sub
qsearch 0 (len - 1) in
1178 match search active qsearch ~
-1 with
1180 state.text <- qsearch ^
" [not found]";
1183 state.text <- qsearch;
1184 active, firstof active
1186 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1188 Glut.postRedisplay
()
1191 if active < Array.length
outlines
1193 let (_
, _
, n, t
) = outlines.(active) in
1197 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1198 state.outline <- None
;
1199 Glut.postRedisplay
();
1201 | _
when key
>= 32 && key
< 127 ->
1202 let pattern = addchar qsearch (Char.chr key
) in
1204 match search active pattern 1 with
1206 state.text <- pattern ^
" [not found]";
1209 state.text <- pattern;
1210 active, firstof active
1212 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1213 Glut.postRedisplay
()
1215 | 14 when not allowdel
->
1216 let optoutlines = narrow outlines qsearch in
1217 begin match optoutlines with
1218 | None
-> state.text <- "can't narrow"
1220 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1221 match state.outlines with
1223 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1224 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1226 Glut.postRedisplay
()
1228 | 21 when not allowdel
->
1230 match state.outlines with
1233 let a = Array.of_list
(List.rev
l) in
1234 state.outlines <- Oarray
a;
1237 state.outlines <- Oarray
b;
1240 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1241 Glut.postRedisplay
()
1245 Some
(allowdel
, active, firstof active, outlines, qsearch);
1246 Glut.postRedisplay
()
1248 | 127 when allowdel
->
1249 let len = Array.length
outlines - 1 in
1252 state.outline <- None
;
1253 state.bookmarks <- [];
1256 let bookmarks = Array.init
len
1258 let i = if i >= active then i + 1 else i in
1269 Glut.postRedisplay
()
1271 | _
-> log "unknown key %d" key
1274 let keyboard ~key ~x ~
y =
1279 match state.outline with
1280 | None
-> viewkeyboard ~key ~x ~
y
1281 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1284 let special ~key ~x ~
y =
1285 match state.outline with
1287 begin match state.textentry with
1291 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1292 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1293 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1294 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1295 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1296 | Glut.KEY_HOME
-> addnav (); 0
1299 state.maxy - (if conf.maxhfit
then state.h else 0)
1305 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1308 | Glut.KEY_UP
-> onhist HCprev
1309 | Glut.KEY_DOWN
-> onhist HCnext
1310 | Glut.KEY_HOME
-> onhist HCfirst
1311 | Glut.KEY_END
-> onhist HClast
1314 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1315 Glut.postRedisplay
()
1320 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1321 let maxrows = maxoutlinerows () in
1323 let active = active + incr in
1324 let active = max
0 (min
active (Array.length
outlines - 1)) in
1328 let rows = active - first in
1329 if rows > maxrows then active - maxrows else first
1332 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1333 Glut.postRedisplay
()
1336 | Glut.KEY_UP
-> navigate ~
-1
1337 | Glut.KEY_DOWN
-> navigate 1
1338 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1339 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1342 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1343 Glut.postRedisplay
()
1346 let active = Array.length
outlines - 1 in
1347 let first = max
0 (active - maxrows) in
1348 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1349 Glut.postRedisplay
()
1354 let drawplaceholder l =
1355 GlDraw.color
(1.0, 1.0, 1.0);
1357 (0.0, float l.pagedispy
)
1358 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1361 and y = float (l.pagedispy
+ 13) in
1362 let font = Glut.BITMAP_8_BY_13
in
1363 GlDraw.color
(0.0, 0.0, 0.0);
1364 GlPix.raster_pos ~
x ~
y ();
1365 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1366 ("Loading " ^ string_of_int
l.pageno);
1369 let now () = Unix.gettimeofday
();;
1372 begin match getopaque l.pageno with
1373 | Some opaque
when validopaque opaque
->
1374 if state.textentry = None
1375 then GlDraw.color
(1.0, 1.0, 1.0)
1376 else GlDraw.color
(0.4, 0.4, 0.4);
1378 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1381 if conf.hlinks
then highlightlinks opaque
(l.pagedispy
- l.pagey);
1382 vlog "draw %f sec" d;
1389 if state.inflight
< cblen state.pagecache
1391 List.iter
preload state.layout;
1394 vlog "inflight %d" state.inflight
;
1397 GlDraw.color
(0.5, 0.5, 0.5);
1400 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1402 l.pagedispy
+ l.pagevh
;
1405 let scrollindicator () =
1406 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1407 GlDraw.color
(0.64 , 0.64, 0.64);
1409 (float (state.w - conf.scrollw
), 0.)
1410 (float state.w, float state.h)
1412 GlDraw.color
(0.0, 0.0, 0.0);
1413 let sh = (float (maxy + state.h) /. float state.h) in
1414 let sh = float state.h /. sh in
1415 let sh = max
sh (float conf.scrollh
) in
1418 if state.y = state.maxy
1420 else float state.y /. float maxy
1422 let position = (float state.h -. sh) *. percent in
1425 if position +. sh > float state.h
1432 (float (state.w - conf.scrollw
), position)
1433 (float state.w, position +. sh)
1438 match state.mstate
with
1442 | Msel
((x0
, y0
), (x1
, y1
)) ->
1444 and y1
= max
y0 y1
in
1447 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1448 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1450 match getopaque l.pageno with
1451 | Some opaque
when validopaque opaque
->
1452 let oy = -l.pagey + l.pagedispy
in
1453 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1456 List.iter
f state.layout
1461 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1462 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1464 (fun (pageno, c, (x0
, y0, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1466 if l.pageno = pageno
1468 let d = float (l.pagedispy
- l.pagey) in
1469 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1470 GlDraw.begins `quads
;
1472 GlDraw.vertex2
(x0
, y0+.d);
1473 GlDraw.vertex2
(x1
, y1
+.d);
1474 GlDraw.vertex2
(x2
, y2
+.d);
1475 GlDraw.vertex2
(x3
, y3
+.d);
1478 (* GlDraw.rect (x0, y0 +. d) (x1, y1 +. d) *)
1486 let showoutline = function
1488 | Some
(allowdel
, active, first, outlines, qsearch) ->
1490 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1491 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1492 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1495 GlDraw.color
(1., 1., 1.);
1496 let font = Glut.BITMAP_9_BY_15
in
1497 let draw_string x y s =
1498 GlPix.raster_pos ~
x ~
y ();
1499 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1502 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1505 let (s, l, _
, _
) = outlines.(row
) in
1506 let y = (row
- first) * 16 in
1511 GlDraw.polygon_mode `both `line
;
1512 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1513 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1514 GlDraw.rect
(0., float (y + 1))
1515 (float (state.w - conf.scrollw
- 1), float (y + 18));
1516 GlDraw.polygon_mode `both `fill
;
1518 GlDraw.color
(1., 1., 1.);
1520 draw_string (float x) (float (y + 16)) s;
1528 let lasty = List.fold_left
drawpage 0 (state.layout) in
1529 GlDraw.color
(0.5, 0.5, 0.5);
1532 (float (state.w - conf.scrollw
), float state.h)
1537 showoutline state.outline;
1539 Glut.swapBuffers
();
1543 let rec f = function
1545 begin match getopaque l.pageno with
1546 | Some opaque
when validopaque opaque
->
1547 let y = y - l.pagedispy
in
1550 let y = l.pagey + y in
1551 match getlink opaque
x y with
1564 let mouse ~button ~bstate ~
x ~
y =
1566 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1574 let incr = incr * 2 in
1575 let y = clamp incr in
1578 | Glut.LEFT_BUTTON
when state.outline = None
->
1579 let dest = if bstate
= Glut.DOWN
then getlink x y else LNone
in
1580 begin match dest with
1581 | LGoto
(pageno, top
) ->
1590 if bstate
= Glut.DOWN
1592 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1593 state.mstate
<- Msel
((x, y), (x, y));
1594 Glut.postRedisplay
()
1597 Glut.setCursor
Glut.CURSOR_INHERIT
;
1598 state.mstate
<- Mnone
;
1605 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1608 if state.outline = None
1610 match state.mstate
with
1613 state.mstate
<- Msel
(a, (x, y));
1614 Glut.postRedisplay
()
1618 if state.outline = None
1620 match state.mstate
with
1622 if getlink x y != LNone
1623 then Glut.setCursor
Glut.CURSOR_INFO
1624 else Glut.setCursor
Glut.CURSOR_INHERIT
1633 if Sys.os_type
= "Win32"
1635 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1637 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1639 Filename.concat
home "llpp"
1643 let ic = open_in_bin
statepath in
1644 let hash = input_value
ic in
1650 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1657 match state.fullscreen
with
1658 | None
-> state.w, state.h
1661 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1662 let oc = open_out_bin
statepath in
1663 output_value
oc pstate
1667 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1672 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1675 state.bookmarks <- statebookmarks;
1676 with Not_found
-> ()
1678 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1681 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1683 if String.length
state.path
= 0
1684 then (prerr_endline
"filename missing"; exit
1)
1689 let _ = Glut.init
Sys.argv
in
1690 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1691 let () = Glut.initWindowSize
state.w state.h in
1692 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1695 if Sys.os_type
= "Unix"
1697 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1699 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1700 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1701 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1702 Unix.bind
sock addr;
1704 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1705 Unix.connect
csock addr;
1706 let ssock, _ = Unix.accept
sock in
1709 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1710 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1714 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1718 let () = Glut.displayFunc
display in
1719 let () = Glut.reshapeFunc
reshape in
1720 let () = Glut.keyboardFunc
keyboard in
1721 let () = Glut.specialFunc
special in
1722 let () = Glut.idleFunc
(Some
idle) in
1723 let () = Glut.mouseFunc
mouse in
1724 let () = Glut.motionFunc
motion in
1725 let () = Glut.passiveMotionFunc
pmotion in
1728 state.csock <- csock;
1729 state.ssock <- ssock;
1730 writecmd csock ("open " ^
name ^
"\000");
1734 let rec handlelablglutbug () =
1737 with Glut.BadEnum
"key in special_of_int" ->
1738 showtext '
!'
" LablGlut bug: special key not recognized";
1739 Glut.swapBuffers
();
1740 handlelablglutbug ()
1742 handlelablglutbug ();