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
;;
22 type textentry
= (char
* string * onhist
option * onkey
* ondone
)
23 and onkey
= string -> int -> te
24 and ondone
= string -> unit
25 and onhist
= histcmd
-> string
26 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
31 | TEswitch
of textentry
35 { store
= Array.create n v
42 let cblen b
= Array.length b
.store
;;
47 b
.wc
<- (b
.wc
+ 1) mod len;
48 b
.len <- min
(b
.len + 1) len;
51 let cbpeekw b
= b
.store
.(b
.wc
);;
54 if b
.len = 0 then b
.store
.(0) else
55 let rc = b
.rc + dir
in
56 let rc = if rc = -1 then b
.len - 1 else rc in
57 let rc = if rc = b
.len then 0 else rc in
78 { mutable scrollw
: int
79 ; mutable scrollh
: int
80 ; mutable rectsel
: bool
81 ; mutable icase
: bool
82 ; mutable preload
: bool
83 ; mutable pagebias
: int
84 ; mutable redispimm
: bool
85 ; mutable verbose
: bool
86 ; mutable scrollincr
: int
87 ; mutable maxhfit
: bool
88 ; mutable crophack
: bool
89 ; mutable autoscroll
: bool
90 ; mutable showall
: bool
94 type outline
= string * int * int * int;;
96 | Oarray
of outline array
97 | Olist
of outline list
98 | Onarrow
of outline array
* outline array
102 { mutable csock
: Unix.file_descr
103 ; mutable ssock
: Unix.file_descr
106 ; mutable rotate
: int
109 ; mutable prevy
: int
111 ; mutable layout
: layout list
112 ; pagemap
: ((int * int * int), string) Hashtbl.t
113 ; mutable pages
: (int * int * int) list
114 ; mutable pagecount
: int
115 ; pagecache
: string circbuf
116 ; mutable inflight
: int
117 ; mutable mstate
: mstate
118 ; mutable searchpattern
: string
119 ; mutable rects
: (int * int * Gl.point2
* Gl.point2
) list
120 ; mutable rects1
: (int * int * Gl.point2
* Gl.point2
) list
121 ; mutable text
: string
122 ; mutable fullscreen
: (int * int) option
123 ; mutable textentry
: textentry
option
124 ; mutable outlines
: outlines
125 ; mutable outline
: (bool * int * int * outline array
* string) option
126 ; mutable bookmarks
: outline list
127 ; mutable path
: string
131 { pat
: string circbuf
132 ; pag
: string circbuf
133 ; nav
: float circbuf
165 ; pagemap
= Hashtbl.create
10
166 ; pagecache
= cbnew 10 ""
177 ; outlines
= Olist
[]
182 { nav
= cbnew 100 0.0
192 Printf.kprintf prerr_endline fmt
194 Printf.kprintf ignore fmt
198 let len = String.length s
in
200 let b = Buffer.create
n in
201 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
202 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
203 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
204 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
205 Buffer.add_string
b s
;
206 let s'
= Buffer.contents
b in
207 let n'
= Unix.write fd
s'
0 n in
208 if n'
!= n then failwith
"write failed";
213 let n = Unix.read fd
s 0 4 in
214 if n != 4 then failwith
"incomplete read(len)";
216 lor (Char.code
s.[0] lsl 24)
217 lor (Char.code
s.[1] lsl 16)
218 lor (Char.code
s.[2] lsl 8)
219 lor (Char.code
s.[3] lsl 0)
221 let s = String.create
len in
222 let n = Unix.read fd
s 0 len in
223 if n != len then failwith
"incomplete read(data)";
228 if y
= state.maxy
then 1.0
229 else float y
/. float state.maxy
233 let b = Buffer.create
10 in
234 Buffer.add_string
b s;
235 let rec combine = function
238 Buffer.add_char
b ' '
;
241 | `
b b -> if b then "1" else "0"
243 | `i i
-> string_of_int i
244 | `f f
-> string_of_float f
245 | `I f
-> string_of_int
(truncate f
)
247 Buffer.add_string
b s;
254 let cmd = Buffer.contents
(makecmd s l
) in
255 writecmd state.csock
cmd;
259 let rec f pn ph fh l
=
261 | (n, _
, h
) :: rest
->
262 let fh = fh + (n - pn
) * ph
in
266 let fh = fh + (ph
* (state.pagecount
- pn
)) in
269 let fh = f 0 0 0 state.pages
in
273 let getpagey pageno
=
274 let rec f pn ph y l
=
276 | (n, _
, h
) :: rest
->
279 y
+ (pageno
- pn
) * ph
281 let y = y + (n - pn
) * ph
in
285 y + (pageno
- pn
) * ph
291 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
292 if pageno
= state.pagecount
|| cacheleft
= 0
295 let ((_
, w
, h
) as curr
), rest
, pdimno
=
297 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
298 curr
, rest
, pdimno
+ 1
302 let pageno'
= pageno + 1 in
336 let accu = e :: accu in
337 f pageno' pdimno curr
338 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
339 (pred cacheleft
) accu
341 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
343 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
344 state.maxy
<- calcheight ();
349 let y = state.y + incr
in
351 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
355 let getopaque pageno =
356 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
358 with Not_found
-> None
361 let cache pageno opaque
=
362 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
366 let validopaque opaque
= String.length opaque
> 0;;
369 match getopaque l
.pageno with
370 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
371 state.inflight
<- succ
state.inflight
;
373 wcmd "render" [`i
(l
.pageno + 1)
383 let y = min
state.maxy
y in
384 let pages = layout y state.h
in
385 let rec f all
= function
387 begin match getopaque l
.pageno with
388 | None
-> preload l
; f false ls
389 | Some opaque
-> f (all
&& validopaque opaque
) ls
393 if not
conf.showall
|| f true pages
396 state.layout <- pages;
401 Glut.postRedisplay
()
406 cbput state.hists
.nav
(yratio state.y);
407 cbrfollowlen state.hists
.nav
;
411 let y = cbget state.hists
.nav ~
-1 in
412 truncate
(y *. float state.maxy
)
416 let y = getpagey n in
422 let ratio = float w
/. float state.w
in
423 let fixbookmark (s, l
, pageno, pagey
) =
424 let pagey = truncate
(float pagey *. ratio) in
425 (s, l
, pageno, pagey)
427 state.bookmarks
<- List.map
fixbookmark state.bookmarks
;
430 GlDraw.viewport
0 0 w h
;
431 GlMat.mode `modelview
;
432 GlMat.load_identity
();
433 GlMat.mode `projection
;
434 GlMat.load_identity
();
435 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
436 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
437 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
438 GlClear.color
(1., 1., 1.);
439 GlClear.clear
[`color
];
444 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
448 GlDraw.color
(0.0, 0.0, 0.0);
450 (0.0, float (state.h
- 18))
451 (float (state.w
- conf.scrollw
- 1), float state.h
)
453 let font = Glut.BITMAP_8_BY_13
in
454 GlDraw.color
(1.0, 1.0, 1.0);
455 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
456 Glut.bitmapCharacter ~
font ~c
:(Char.code c
);
457 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s;
461 let len = String.length
state.text
in
462 match state.textentry
with
464 if len > 0 then showtext ' '
state.text
466 | Some
(c
, text
, _
, _
, _
) ->
470 text ^
" [" ^
state.text ^
"]"
481 state.outlines
<- Olist
[]
484 state.rects
<- state.rects1
;
485 Glut.postRedisplay
()
488 state.rects
<- state.rects1
;
489 Glut.postRedisplay
()
492 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
493 state.pagecount
<- n;
494 let rely = yratio state.y in
495 let maxy = calcheight () in
496 state.y <- truncate
(float maxy *. rely);
498 let pages = layout state.y state.h
in
499 state.layout <- pages;
500 Glut.postRedisplay
();
503 let s = Scanf.sscanf
cmd "t %n"
504 (fun n -> String.sub
cmd n (String.length
cmd - n))
506 Glut.setWindowTitle
s
509 let s = Scanf.sscanf
cmd "T %n"
510 (fun n -> String.sub
cmd n (String.length
cmd - n))
512 if state.textentry
= None
520 Glut.postRedisplay
();
526 let s = Scanf.sscanf
cmd "V %n"
527 (fun n -> String.sub
cmd n (String.length
cmd - n))
534 let pageno, c
, x0
, y0
, x1
, y1
=
535 Scanf.sscanf
cmd "F %d %d %f %f %f %f"
536 (fun p c x0 y0 x1 y1
-> (p
, c
, x0
, y0
, x1
, y1
))
538 let y = (getpagey pageno) + truncate y0
in
541 state.rects1
<- [pageno, c
, (x0
, y0
), (x1
, y1
)]
544 let pageno, c
, x0
, y0
, x1
, y1
=
545 Scanf.sscanf
cmd "R %d %d %f %f %f %f"
546 (fun pageno c x0 y0 x1 y1
-> (pageno, c
, x0
, y0
, x1
, y1
))
548 state.rects1
<- (pageno, c
, (x0
, y0
), (x1
, y1
)) :: state.rects1
552 Scanf.sscanf
cmd "r %d %d %d %d %s"
553 (fun n w h r p
-> (n, w
, h
, r
, p
))
555 Hashtbl.replace
state.pagemap
(n, w
, r
) p
;
556 let evicted = cbpeekw state.pagecache
in
557 if String.length
evicted > 0
559 wcmd "free" [`
s evicted];
560 let l = Hashtbl.fold
(fun k p a
->
561 if evicted = p
then k
:: a
else a
) state.pagemap
[]
563 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
565 cbput state.pagecache p
;
566 state.inflight
<- pred
state.inflight
;
567 if conf.showall
then gotoy state.ty
;
568 Glut.postRedisplay
()
571 let (n, w
, h
) as pagelayout
=
572 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
574 state.pages <- pagelayout
:: state.pages
578 Scanf.sscanf
cmd "o %d %d %d %n" (fun l n t pos
-> l, n, t
, pos
)
580 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
581 let outline = (s, l, n, t
) in
583 match state.outlines with
584 | Olist
outlines -> Olist
(outline :: outlines)
585 | Oarray _
-> Olist
[outline]
586 | Onarrow _
-> Olist
[outline]
588 state.outlines <- outlines
591 log "unknown cmd `%S'" cmd
595 if not
conf.redispimm
&& state.y != state.prevy
597 state.prevy
<- state.y;
598 Glut.postRedisplay
();
601 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.001 in
605 if conf.preload then begin
607 let y = if state.y < state.h then 0 else state.y - state.h in
608 let pages = layout y (h*3) in
609 List.iter
preload pages;
611 if conf.autoscroll
then begin
612 let y = state.y + conf.scrollincr
in
613 let y = if y >= state.maxy then 0 else y in
616 state.prevy
<- state.y;
617 Glut.postRedisplay
();
621 let cmd = readcmd state.csock
in
626 let onhist cb
= function
627 | HCprev
-> cbget cb ~
-1
628 | HCnext
-> cbget cb
1
629 | HCfirst
-> cbget cb ~
-(cb
.rc)
630 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
633 let search pattern forward
=
634 if String.length pattern
> 0
637 match state.layout with
640 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
643 let b = makecmd "search"
644 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
646 Buffer.add_char
b '
,'
;
647 Buffer.add_string
b pattern
;
648 Buffer.add_char
b '
\000'
;
651 writecmd state.csock
cmd;
654 let intentry text key
=
655 let c = Char.unsafe_chr key
in
658 let s = "x" in s.[0] <- c;
659 let text = text ^
s in
663 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
668 let b = Buffer.create
(String.length
s + 1) in
669 Buffer.add_string
b s;
674 let textentry text key
=
675 let c = Char.unsafe_chr key
in
677 | _
when key
>= 32 && key
< 127 ->
678 let text = addchar text c in
682 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
686 let optentry text key
=
687 let btos b = if b then "on" else "off" in
688 let c = Char.unsafe_chr key
in
691 conf.rectsel
<- not
conf.rectsel
;
692 TEdone
("rectsel " ^
(btos conf.rectsel
))
696 try conf.scrollincr
<- int_of_string
s with exc
->
697 state.text <- Printf.sprintf
"bad integer `%s': %s"
698 s (Printexc.to_string exc
)
700 TEswitch
('#'
, "", None
, intentry, ondone)
705 state.rotate
<- int_of_string
s;
706 wcmd "rotate" [`i
state.rotate
]
708 state.text <- Printf.sprintf
"bad integer `%s': %s"
709 s (Printexc.to_string exc
)
711 TEswitch
('^'
, "", None
, intentry, ondone)
714 conf.icase
<- not
conf.icase
;
715 TEdone
("case insensitive search " ^
(btos conf.icase
))
718 conf.preload <- not
conf.preload;
719 TEdone
("preload " ^
(btos conf.preload))
722 conf.redispimm
<- not
conf.redispimm
;
723 TEdone
("immediate redisplay " ^
(btos conf.redispimm
))
726 conf.verbose
<- not
conf.verbose
;
727 TEdone
("verbose " ^
(btos conf.verbose
))
730 conf.maxhfit
<- not
conf.maxhfit
;
731 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
732 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
735 conf.crophack
<- not
conf.crophack
;
736 TEdone
("crophack " ^
btos conf.crophack
)
739 conf.showall
<- not
conf.showall
;
740 TEdone
("showall " ^
btos conf.showall
)
743 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
747 let maxoutlinerows () = (state.h - 31) / 16;;
749 let enterselector allowdel
outlines errmsg
=
750 if Array.length
outlines = 0
757 match state.layout with
759 | {pageno=pageno} :: rest
-> pageno
763 if n = Array.length
outlines
766 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
767 if outlinepageno
>= pageno then n else loop (n+1)
772 Some
(allowdel
, active, max
0 (active - maxoutlinerows ()), outlines, "");
773 Glut.postRedisplay
();
776 let enteroutlinemode () =
778 match state.outlines with
781 let a = Array.of_list
(List.rev
l) in
782 state.outlines <- Oarray
a;
784 | Onarrow
(a, b) -> a
786 enterselector false outlines "Documents has no outline";
789 let enterbookmarkmode () =
790 let bookmarks = Array.of_list
state.bookmarks in
791 enterselector true bookmarks "Documents has no bookmarks (yet)";
795 let quickbookmark ?title
() =
796 match state.layout with
802 let sec = Unix.gettimeofday
() in
803 let tm = Unix.localtime
sec in
804 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
808 (tm.Unix.tm_year
+ 1900)
811 | Some
title -> title
814 (title, 0, l.pageno, l.pagey) :: state.bookmarks
817 let viewkeyboard ~key ~x ~
y =
819 state.textentry <- te
;
822 Glut.postRedisplay
()
824 match state.textentry with
826 let c = Char.chr key
in
841 Glut.postRedisplay
()
844 let ondone isforw
s =
845 cbput state.hists
.pat
s;
846 cbrfollowlen state.hists
.pat
;
847 state.searchpattern
<- s;
850 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
851 textentry, ondone (c ='
/'
)))
856 try int_of_string
s with exc
->
857 state.text <- Printf.sprintf
"bad integer `%s': %s"
858 s (Printexc.to_string exc
);
864 state.text <- "page bias is now " ^ string_of_int
n;
867 enttext (Some
('
+'
, "", None
, intentry, ondone))
873 enttext (Some
('
-'
, "", None
, optentry, ondone))
878 try int_of_string
s with exc
->
879 state.text <- Printf.sprintf
"bad integer `%s': %s"
880 s (Printexc.to_string exc
);
886 cbput state.hists
.pag
(string_of_int
n);
887 cbrfollowlen state.hists
.pag
;
888 gotoy (getpagey (n + conf.pagebias
- 1))
891 let pageentry text key
=
892 match Char.unsafe_chr key
with
894 | _
-> intentry text key
896 let text = "x" in text.[0] <- c;
897 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
901 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
902 reshape state.w
state.h;
905 conf.autoscroll
<- not
conf.autoscroll
908 begin match state.fullscreen
with
910 state.fullscreen
<- Some
(state.w
, state.h);
913 state.fullscreen
<- None
;
914 Glut.reshapeWindow ~w ~
h
921 search state.searchpattern
true
924 search state.searchpattern
false
927 begin match state.layout with
930 gotoy (state.y - l.pagey);
934 begin match List.rev
state.layout with
937 gotoy (clamp (l.pageh
- l.pagey))
941 begin match state.layout with
944 gotoy (clamp (-l.pageh
));
949 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
951 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
953 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
957 else (100. *. (float state.y /. float maxy)) in
960 Printf.sprintf
"Page %d of %d %.2f%%"
961 (fn+1) state.pagecount
percent
964 "Pages %d-%d of %d %.2f%%"
965 (fn+1) (ln
+1) state.pagecount
percent
971 begin match state.layout with
974 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
975 Glut.postRedisplay
();
983 match state.layout with
985 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
988 enttext (Some
('~'
, "", None
, textentry, ondone))
992 showtext ' '
"Quick bookmark added";
996 begin match state.layout with
998 let a = getpagewh
l.pagedimno
in
1002 (truncate
(1.8 *. (a.(1) -. a.(0))),
1003 truncate
(1.4 *. (a.(3) -. a.(0))))
1005 (truncate
(a.(1) -. a.(0)),
1006 truncate
(a.(3) -. a.(0)))
1008 Glut.reshapeWindow
(w + conf.scrollw
) h;
1009 Glut.postRedisplay
();
1015 state.rotate
<- state.rotate
+ (if c = '
>'
then 30 else -30);
1016 wcmd "rotate" [`i
state.rotate
]
1019 vlog "huh? %d %c" key
(Char.chr key
);
1022 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1023 let len = String.length
text in
1026 state.textentry <- None
;
1027 Glut.postRedisplay
();
1030 let s = String.sub
text 0 (len - 1) in
1031 enttext (Some
(c, s, onhist, onkey
, ondone))
1034 | Some
(c, text, onhist, onkey
, ondone) ->
1035 begin match Char.unsafe_chr key
with
1038 state.textentry <- None
;
1039 Glut.postRedisplay
()
1042 state.textentry <- None
;
1043 Glut.postRedisplay
()
1046 begin match onkey
text key
with
1048 state.textentry <- None
;
1050 Glut.postRedisplay
()
1053 enttext (Some
(c, text, onhist, onkey
, ondone));
1056 state.textentry <- None
;
1057 Glut.postRedisplay
()
1060 state.textentry <- Some te
;
1061 Glut.postRedisplay
()
1066 let narrow outlines pattern
=
1067 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1071 let rec fold accu n =
1072 if n = -1 then accu else
1073 let (s, _
, _
, _
) as o
= outlines.(n) in
1075 if (try ignore
(Str.search_forward re
s 0); true
1076 with Not_found
-> false)
1082 let matched = fold [] (Array.length
outlines - 1) in
1083 if matched = [] then None
else Some
(Array.of_list
matched)
1086 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1087 let search active pattern incr
=
1090 if n = Array.length
outlines || n = -1 then None
else
1091 let (s, _
, _
, _
) = outlines.(n) in
1093 (try ignore
(Str.search_forward re
s 0); true
1094 with Not_found
-> false)
1096 else loop (n + incr
)
1101 let re = Str.regexp_case_fold pattern
in
1107 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1110 if String.length qsearch
= 0
1113 state.outline <- None
;
1114 Glut.postRedisplay
();
1118 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1119 Glut.postRedisplay
();
1123 let incr = if key
= 18 then -1 else 1 in
1125 match search (active + incr) qsearch
incr with
1127 state.text <- qsearch ^
" [not found]";
1130 state.text <- qsearch
;
1131 active, firstof active
1133 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1134 Glut.postRedisplay
();
1137 let len = String.length qsearch
in
1144 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1147 let qsearch = String.sub
qsearch 0 (len - 1) in
1149 match search active qsearch ~
-1 with
1151 state.text <- qsearch ^
" [not found]";
1154 state.text <- qsearch;
1155 active, firstof active
1157 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1159 Glut.postRedisplay
()
1162 if active < Array.length
outlines
1164 let (_
, _
, n, t
) = outlines.(active) in
1168 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1169 state.outline <- None
;
1170 Glut.postRedisplay
();
1172 | _
when key
>= 32 && key
< 127 ->
1173 let pattern = addchar qsearch (Char.chr key
) in
1175 match search active pattern 1 with
1177 state.text <- pattern ^
" [not found]";
1180 state.text <- pattern;
1181 active, firstof active
1183 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1184 Glut.postRedisplay
()
1186 | 14 when not allowdel
->
1187 let optoutlines = narrow outlines qsearch in
1188 begin match optoutlines with
1189 | None
-> state.text <- "can't narrow"
1191 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1192 match state.outlines with
1194 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1195 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1197 Glut.postRedisplay
()
1199 | 21 when not allowdel
->
1201 match state.outlines with
1204 let a = Array.of_list
(List.rev
l) in
1205 state.outlines <- Oarray
a;
1208 state.outlines <- Oarray
b;
1211 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1212 Glut.postRedisplay
()
1216 Some
(allowdel
, active, firstof active, outlines, qsearch);
1217 Glut.postRedisplay
()
1219 | 127 when allowdel
->
1220 let len = Array.length
outlines - 1 in
1223 state.outline <- None
;
1224 state.bookmarks <- [];
1227 let bookmarks = Array.init
len
1229 let i = if i >= active then i + 1 else i in
1240 Glut.postRedisplay
()
1242 | _
-> log "unknown key %d" key
1245 let keyboard ~key ~x ~
y =
1250 match state.outline with
1251 | None
-> viewkeyboard ~key ~x ~
y
1252 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1255 let special ~key ~x ~
y =
1256 match state.outline with
1258 begin match state.textentry with
1262 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1263 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1264 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1265 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1266 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1267 | Glut.KEY_HOME
-> addnav (); 0
1270 state.maxy - (if conf.maxhfit
then state.h else 0)
1276 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1279 | Glut.KEY_UP
-> onhist HCprev
1280 | Glut.KEY_DOWN
-> onhist HCnext
1281 | Glut.KEY_HOME
-> onhist HCfirst
1282 | Glut.KEY_END
-> onhist HClast
1285 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1286 Glut.postRedisplay
()
1291 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1292 let maxrows = maxoutlinerows () in
1294 let active = active + incr in
1295 let active = max
0 (min
active (Array.length
outlines - 1)) in
1299 let rows = active - first in
1300 if rows > maxrows then first + incr else first
1303 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1304 Glut.postRedisplay
()
1307 | Glut.KEY_UP
-> navigate ~
-1
1308 | Glut.KEY_DOWN
-> navigate 1
1309 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1310 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1313 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1314 Glut.postRedisplay
()
1317 let active = Array.length
outlines - 1 in
1318 let first = max
0 (active - maxrows) in
1319 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1320 Glut.postRedisplay
()
1325 let drawplaceholder l =
1326 GlDraw.color
(1.0, 1.0, 1.0);
1328 (0.0, float l.pagedispy
)
1329 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1332 and y = float (l.pagedispy
+ 13) in
1333 let font = Glut.BITMAP_8_BY_13
in
1334 GlDraw.color
(0.0, 0.0, 0.0);
1335 GlPix.raster_pos ~
x ~
y ();
1336 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1337 ("Loading " ^ string_of_int
l.pageno);
1340 let now () = Unix.gettimeofday
();;
1343 begin match getopaque l.pageno with
1344 | Some opaque
when validopaque opaque
->
1345 if state.textentry = None
1346 then GlDraw.color
(1.0, 1.0, 1.0)
1347 else GlDraw.color
(0.4, 0.4, 0.4);
1349 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1352 vlog "draw %f sec" d;
1359 if state.inflight
< cblen state.pagecache
1361 List.iter
preload state.layout;
1364 vlog "inflight %d" state.inflight
;
1367 GlDraw.color
(0.5, 0.5, 0.5);
1370 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1372 l.pagedispy
+ l.pagevh
;
1375 let scrollindicator () =
1376 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1377 GlDraw.color
(0.64 , 0.64, 0.64);
1379 (float (state.w - conf.scrollw
), 0.)
1380 (float state.w, float state.h)
1382 GlDraw.color
(0.0, 0.0, 0.0);
1383 let sh = (float (maxy + state.h) /. float state.h) in
1384 let sh = float state.h /. sh in
1385 let sh = max
sh (float conf.scrollh
) in
1388 if state.y = state.maxy
1390 else float state.y /. float maxy
1392 let position = (float state.h -. sh) *. percent in
1395 if position +. sh > float state.h
1402 (float (state.w - conf.scrollw
), position)
1403 (float state.w, position +. sh)
1408 match state.mstate
with
1412 | Msel
((x0
, y0
), (x1
, y1
)) ->
1414 and y1
= max
y0 y1
in
1417 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1418 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1420 match getopaque l.pageno with
1421 | Some opaque
when validopaque opaque
->
1422 let oy = -l.pagey + l.pagedispy
in
1423 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1426 List.iter
f state.layout
1431 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1432 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1434 (fun (pageno, c, (x0
, y0), (x1
, y1
)) ->
1436 if l.pageno = pageno
1438 let d = float (l.pagedispy
- l.pagey) in
1439 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1440 GlDraw.rect
(x0
, y0 +. d) (x1
, y1
+. d)
1448 let showoutline = function
1450 | Some
(allowdel
, active, first, outlines, qsearch) ->
1452 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1453 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1454 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1457 GlDraw.color
(1., 1., 1.);
1458 let font = Glut.BITMAP_9_BY_15
in
1459 let draw_string x y s =
1460 GlPix.raster_pos ~
x ~
y ();
1461 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1464 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1467 let (s, l, _
, _
) = outlines.(row
) in
1468 let y = (row
- first) * 16 in
1473 GlDraw.polygon_mode `both `line
;
1474 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1475 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1476 GlDraw.rect
(0., float (y + 1))
1477 (float (state.w - conf.scrollw
- 1), float (y + 18));
1478 GlDraw.polygon_mode `both `fill
;
1480 GlDraw.color
(1., 1., 1.);
1482 draw_string (float x) (float (y + 16)) s;
1490 let lasty = List.fold_left
drawpage 0 (state.layout) in
1491 GlDraw.color
(0.5, 0.5, 0.5);
1494 (float (state.w - conf.scrollw
), float state.h)
1499 showoutline state.outline;
1501 Glut.swapBuffers
();
1505 let rec f = function
1507 begin match getopaque l.pageno with
1508 | Some opaque
when validopaque opaque
->
1509 let y = y - l.pagedispy
in
1512 let y = l.pagey + y in
1513 match getlink opaque
x y with
1527 let rec f = function
1529 begin match getopaque l.pageno with
1530 | Some opaque
when validopaque opaque
->
1531 let y = y - l.pagedispy
in
1534 let y = l.pagey + y in
1535 if checklink opaque
x y then true else f rest
1546 let mouse ~button ~bstate ~
x ~
y =
1548 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1556 let incr = incr * 2 in
1557 let y = clamp incr in
1560 | Glut.LEFT_BUTTON
when state.outline = None
->
1561 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1562 begin match dest with
1563 | Some
(pageno, top
) ->
1567 if bstate
= Glut.DOWN
1569 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1570 state.mstate
<- Msel
((x, y), (x, y));
1571 Glut.postRedisplay
()
1574 Glut.setCursor
Glut.CURSOR_INHERIT
;
1575 state.mstate
<- Mnone
;
1582 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1585 if state.outline = None
1587 match state.mstate
with
1590 state.mstate
<- Msel
(a, (x, y));
1591 Glut.postRedisplay
()
1595 if state.outline = None
1597 match state.mstate
with
1598 | Mnone
when (checklink x y) ->
1599 Glut.setCursor
Glut.CURSOR_INFO
1602 Glut.setCursor
Glut.CURSOR_INHERIT
1611 if Sys.os_type
= "Win32"
1613 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1615 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1617 Filename.concat
home "llpp"
1621 let ic = open_in_bin
statepath in
1622 let hash = input_value
ic in
1628 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1635 match state.fullscreen
with
1636 | None
-> state.w, state.h
1639 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1640 let oc = open_out_bin
statepath in
1641 output_value
oc pstate
1645 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1650 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1653 state.bookmarks <- statebookmarks;
1654 with Not_found
-> ()
1656 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1659 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1661 if String.length
state.path
= 0
1662 then (prerr_endline
"filename missing"; exit
1)
1667 let _ = Glut.init
Sys.argv
in
1668 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1669 let () = Glut.initWindowSize
state.w state.h in
1670 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1673 if Sys.os_type
= "Unix"
1675 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1677 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1678 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1679 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1680 Unix.bind
sock addr;
1682 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1683 Unix.connect
csock addr;
1684 let ssock, _ = Unix.accept
sock in
1687 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1688 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1692 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1696 let () = Glut.displayFunc
display in
1697 let () = Glut.reshapeFunc
reshape in
1698 let () = Glut.keyboardFunc
keyboard in
1699 let () = Glut.specialFunc
special in
1700 let () = Glut.idleFunc
(Some
idle) in
1701 let () = Glut.mouseFunc
mouse in
1702 let () = Glut.motionFunc
motion in
1703 let () = Glut.passiveMotionFunc
pmotion in
1706 state.csock <- csock;
1707 state.ssock <- ssock;
1708 writecmd csock ("open " ^
name ^
"\000");
1712 let rec handlelablglutbug () =
1715 with Glut.BadEnum
"key in special_of_int" ->
1716 showtext '
!'
" LablGlut bug: special key not recognized";
1717 Glut.swapBuffers
();
1718 handlelablglutbug ()
1720 handlelablglutbug ();