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
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
631 let search pattern forward
=
632 if String.length pattern
> 0
635 match state.layout with
638 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
641 let b = makecmd "search"
642 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
644 Buffer.add_char
b '
,'
;
645 Buffer.add_string
b pattern
;
646 Buffer.add_char
b '
\000'
;
649 writecmd state.csock
cmd;
652 let intentry text key
=
653 let c = Char.unsafe_chr key
in
656 let s = "x" in s.[0] <- c;
657 let text = text ^
s in
661 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
666 let b = Buffer.create
(String.length
s + 1) in
667 Buffer.add_string
b s;
672 let textentry text key
=
673 let c = Char.unsafe_chr key
in
675 | _
when key
>= 32 && key
< 127 ->
676 let text = addchar text c in
680 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
684 let optentry text key
=
685 let btos b = if b then "on" else "off" in
686 let c = Char.unsafe_chr key
in
689 conf.rectsel
<- not
conf.rectsel
;
690 TEdone
("rectsel " ^
(btos conf.rectsel
))
694 try conf.scrollincr
<- int_of_string
s with exc
->
695 state.text <- Printf.sprintf
"bad integer `%s': %s"
696 s (Printexc.to_string exc
)
698 TEswitch
('#'
, "", None
, intentry, ondone)
703 state.rotate
<- int_of_string
s;
704 wcmd "rotate" [`i
state.rotate
]
706 state.text <- Printf.sprintf
"bad integer `%s': %s"
707 s (Printexc.to_string exc
)
709 TEswitch
('^'
, "", None
, intentry, ondone)
712 conf.icase
<- not
conf.icase
;
713 TEdone
("case insensitive search " ^
(btos conf.icase
))
716 conf.preload <- not
conf.preload;
717 TEdone
("preload " ^
(btos conf.preload))
720 conf.redispimm
<- not
conf.redispimm
;
721 TEdone
("immediate redisplay " ^
(btos conf.redispimm
))
724 conf.verbose
<- not
conf.verbose
;
725 TEdone
("verbose " ^
(btos conf.verbose
))
728 conf.maxhfit
<- not
conf.maxhfit
;
729 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
730 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
733 conf.crophack
<- not
conf.crophack
;
734 TEdone
("crophack " ^
btos conf.crophack
)
737 conf.showall
<- not
conf.showall
;
738 TEdone
("showall " ^
btos conf.showall
)
741 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
745 let maxoutlinerows () = (state.h - 31) / 16;;
747 let enterselector allowdel
outlines errmsg
=
748 if Array.length
outlines = 0
755 match state.layout with
757 | {pageno=pageno} :: rest
-> pageno
761 if n = Array.length
outlines
764 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
765 if outlinepageno
>= pageno then n else loop (n+1)
770 Some
(allowdel
, active, max
0 (active - maxoutlinerows ()), outlines, "");
771 Glut.postRedisplay
();
774 let enteroutlinemode () =
776 match state.outlines with
779 let a = Array.of_list
(List.rev
l) in
780 state.outlines <- Oarray
a;
782 | Onarrow
(a, b) -> a
784 enterselector false outlines "Documents has no outline";
787 let enterbookmarkmode () =
788 let bookmarks = Array.of_list
state.bookmarks in
789 enterselector true bookmarks "Documents has no bookmarks (yet)";
793 let quickbookmark ?title
() =
794 match state.layout with
800 let sec = Unix.gettimeofday
() in
801 let tm = Unix.localtime
sec in
802 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
806 (tm.Unix.tm_year
+ 1900)
809 | Some
title -> title
812 (title, 0, l.pageno, l.pagey) :: state.bookmarks
815 let viewkeyboard ~key ~x ~
y =
817 state.textentry <- te
;
820 Glut.postRedisplay
()
822 match state.textentry with
824 let c = Char.chr key
in
839 Glut.postRedisplay
()
842 let ondone isforw
s =
843 cbput state.hists
.pat
s;
844 cbrfollowlen state.hists
.pat
;
845 state.searchpattern
<- s;
848 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
849 textentry, ondone (c ='
/'
)))
854 try int_of_string
s with exc
->
855 state.text <- Printf.sprintf
"bad integer `%s': %s"
856 s (Printexc.to_string exc
);
862 state.text <- "page bias is now " ^ string_of_int
n;
865 enttext (Some
('
+'
, "", None
, intentry, ondone))
871 enttext (Some
('
-'
, "", None
, optentry, ondone))
876 try int_of_string
s with exc
->
877 state.text <- Printf.sprintf
"bad integer `%s': %s"
878 s (Printexc.to_string exc
);
884 cbput state.hists
.pag
(string_of_int
n);
885 cbrfollowlen state.hists
.pag
;
886 gotoy (getpagey (n + conf.pagebias
- 1))
889 let pageentry text key
=
890 match Char.unsafe_chr key
with
892 | _
-> intentry text key
894 let text = "x" in text.[0] <- c;
895 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
899 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
900 reshape state.w
state.h;
903 conf.autoscroll
<- not
conf.autoscroll
906 begin match state.fullscreen
with
908 state.fullscreen
<- Some
(state.w
, state.h);
911 state.fullscreen
<- None
;
912 Glut.reshapeWindow ~w ~
h
919 search state.searchpattern
true
922 search state.searchpattern
false
925 begin match state.layout with
928 gotoy (state.y - l.pagey);
932 begin match List.rev
state.layout with
935 gotoy (clamp (l.pageh
- l.pagey))
939 begin match state.layout with
942 gotoy (clamp (-l.pageh
));
947 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
949 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
951 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
955 else (100. *. (float state.y /. float maxy)) in
958 Printf.sprintf
"Page %d of %d %.2f%%"
959 (fn+1) state.pagecount
percent
962 "Pages %d-%d of %d %.2f%%"
963 (fn+1) (ln
+1) state.pagecount
percent
969 begin match state.layout with
972 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
973 Glut.postRedisplay
();
981 match state.layout with
983 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
986 enttext (Some
('~'
, "", None
, textentry, ondone))
990 showtext ' '
"Quick bookmark added";
994 begin match state.layout with
996 let a = getpagewh
l.pagedimno
in
1000 (truncate
(1.8 *. (a.(1) -. a.(0))),
1001 truncate
(1.4 *. (a.(3) -. a.(0))))
1003 (truncate
(a.(1) -. a.(0)),
1004 truncate
(a.(3) -. a.(0)))
1006 Glut.reshapeWindow
(w + conf.scrollw
) h;
1007 Glut.postRedisplay
();
1013 state.rotate
<- state.rotate
+ (if c = '
>'
then 30 else -30);
1014 wcmd "rotate" [`i
state.rotate
]
1017 vlog "huh? %d %c" key
(Char.chr key
);
1020 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1021 let len = String.length
text in
1024 state.textentry <- None
;
1025 Glut.postRedisplay
();
1028 let s = String.sub
text 0 (len - 1) in
1029 enttext (Some
(c, s, onhist, onkey
, ondone))
1032 | Some
(c, text, onhist, onkey
, ondone) ->
1033 begin match Char.unsafe_chr key
with
1036 state.textentry <- None
;
1037 Glut.postRedisplay
()
1040 state.textentry <- None
;
1041 Glut.postRedisplay
()
1044 begin match onkey
text key
with
1046 state.textentry <- None
;
1048 Glut.postRedisplay
()
1051 enttext (Some
(c, text, onhist, onkey
, ondone));
1054 state.textentry <- None
;
1055 Glut.postRedisplay
()
1058 state.textentry <- Some te
;
1059 Glut.postRedisplay
()
1064 let narrow outlines pattern
=
1065 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1069 let rec fold accu n =
1070 if n = -1 then accu else
1071 let (s, _
, _
, _
) as o
= outlines.(n) in
1073 if (try ignore
(Str.search_forward re
s 0); true
1074 with Not_found
-> false)
1080 let matched = fold [] (Array.length
outlines - 1) in
1081 if matched = [] then None
else Some
(Array.of_list
matched)
1084 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1085 let search active pattern incr
=
1088 if n = Array.length
outlines || n = -1 then None
else
1089 let (s, _
, _
, _
) = outlines.(n) in
1091 (try ignore
(Str.search_forward re
s 0); true
1092 with Not_found
-> false)
1094 let maxrows = (min
(Array.length
outlines) (maxoutlinerows ())) / 2 in
1096 then Some
(n, max
0 (n - maxrows))
1097 else Some
(n, max first
(n - maxrows))
1099 else loop (n + incr
)
1104 let re = Str.regexp_case_fold pattern
in
1112 if String.length qsearch
= 0
1115 state.outline <- None
;
1116 Glut.postRedisplay
();
1120 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1121 Glut.postRedisplay
();
1125 let incr = if key
= 18 then -1 else 1 in
1127 match search (active + incr) qsearch
incr with
1129 state.text <- qsearch ^
" [not found]";
1132 state.text <- qsearch
;
1135 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1136 Glut.postRedisplay
();
1139 let len = String.length qsearch
in
1146 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1149 let qsearch = String.sub
qsearch 0 (len - 1) in
1151 match search active qsearch ~
-1 with
1153 state.text <- qsearch ^
" [not found]";
1156 state.text <- qsearch;
1159 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1161 Glut.postRedisplay
()
1164 if active < Array.length
outlines
1166 let (_
, _
, n, t
) = outlines.(active) in
1170 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1171 state.outline <- None
;
1172 Glut.postRedisplay
();
1174 | _
when key
>= 32 && key
< 127 ->
1175 let pattern = addchar qsearch (Char.chr key
) in
1177 match search active pattern 1 with
1179 state.text <- pattern ^
" [not found]";
1181 | Some
(active, first
) ->
1182 state.text <- pattern;
1185 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1186 Glut.postRedisplay
()
1188 | 14 when not allowdel
->
1189 let optoutlines = narrow outlines qsearch in
1190 begin match optoutlines with
1191 | None
-> state.text <- "can't narrow"
1193 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1194 match state.outlines with
1196 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1197 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1199 Glut.postRedisplay
()
1201 | 21 when not allowdel
->
1203 match state.outlines with
1206 let a = Array.of_list
(List.rev
l) in
1207 state.outlines <- Oarray
a;
1209 | Onarrow
(a, b) -> b
1211 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1212 Glut.postRedisplay
()
1214 | 127 when allowdel
->
1215 let len = Array.length
outlines - 1 in
1218 state.outline <- None
;
1219 state.bookmarks <- [];
1222 let bookmarks = Array.init
len
1224 let i = if i >= active then i + 1 else i in
1235 Glut.postRedisplay
()
1237 | _
-> log "unknown key %d" key
1240 let keyboard ~key ~x ~
y =
1245 match state.outline with
1246 | None
-> viewkeyboard ~key ~x ~
y
1247 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1250 let special ~key ~x ~
y =
1251 match state.outline with
1253 begin match state.textentry with
1257 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1258 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1259 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1260 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1261 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1262 | Glut.KEY_HOME
-> addnav (); 0
1265 state.maxy - (if conf.maxhfit
then state.h else 0)
1271 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1274 | Glut.KEY_UP
-> onhist HCprev
1275 | Glut.KEY_DOWN
-> onhist HCnext
1278 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1279 Glut.postRedisplay
()
1284 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1285 let maxrows = maxoutlinerows () in
1287 let active = active + incr in
1288 let active = max
0 (min
active (Array.length
outlines - 1)) in
1292 let rows = active - first in
1293 if rows > maxrows then first + incr else first
1296 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1297 Glut.postRedisplay
()
1300 | Glut.KEY_UP
-> navigate ~
-1
1301 | Glut.KEY_DOWN
-> navigate 1
1302 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1303 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1306 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1307 Glut.postRedisplay
()
1310 let active = Array.length
outlines - 1 in
1311 let first = max
0 (active - maxrows) in
1312 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1313 Glut.postRedisplay
()
1318 let drawplaceholder l =
1319 GlDraw.color
(1.0, 1.0, 1.0);
1321 (0.0, float l.pagedispy
)
1322 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1325 and y = float (l.pagedispy
+ 13) in
1326 let font = Glut.BITMAP_8_BY_13
in
1327 GlDraw.color
(0.0, 0.0, 0.0);
1328 GlPix.raster_pos ~
x ~
y ();
1329 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1330 ("Loading " ^ string_of_int
l.pageno);
1333 let now () = Unix.gettimeofday
();;
1336 begin match getopaque l.pageno with
1337 | Some opaque
when validopaque opaque
->
1338 if state.textentry = None
1339 then GlDraw.color
(1.0, 1.0, 1.0)
1340 else GlDraw.color
(0.4, 0.4, 0.4);
1342 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1345 vlog "draw %f sec" d;
1352 if state.inflight
< cblen state.pagecache
1354 List.iter
preload state.layout;
1357 vlog "inflight %d" state.inflight
;
1360 GlDraw.color
(0.5, 0.5, 0.5);
1363 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1365 l.pagedispy
+ l.pagevh
;
1368 let scrollindicator () =
1369 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1370 GlDraw.color
(0.64 , 0.64, 0.64);
1372 (float (state.w - conf.scrollw
), 0.)
1373 (float state.w, float state.h)
1375 GlDraw.color
(0.0, 0.0, 0.0);
1376 let sh = (float (maxy + state.h) /. float state.h) in
1377 let sh = float state.h /. sh in
1378 let sh = max
sh (float conf.scrollh
) in
1381 if state.y = state.maxy
1383 else float state.y /. float maxy
1385 let position = (float state.h -. sh) *. percent in
1388 if position +. sh > float state.h
1395 (float (state.w - conf.scrollw
), position)
1396 (float state.w, position +. sh)
1401 match state.mstate
with
1405 | Msel
((x0
, y0
), (x1
, y1
)) ->
1407 and y1
= max
y0 y1
in
1410 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1411 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1413 match getopaque l.pageno with
1414 | Some opaque
when validopaque opaque
->
1415 let oy = -l.pagey + l.pagedispy
in
1416 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1419 List.iter
f state.layout
1424 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1425 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1427 (fun (pageno, c, (x0
, y0), (x1
, y1
)) ->
1429 if l.pageno = pageno
1431 let d = float (l.pagedispy
- l.pagey) in
1432 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1433 GlDraw.rect
(x0
, y0 +. d) (x1
, y1
+. d)
1441 let showoutline = function
1443 | Some
(allowdel
, active, first, outlines, qsearch) ->
1445 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1446 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1447 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1450 GlDraw.color
(1., 1., 1.);
1451 let font = Glut.BITMAP_9_BY_15
in
1452 let draw_string x y s =
1453 GlPix.raster_pos ~
x ~
y ();
1454 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1457 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1460 let (s, l, _
, _
) = outlines.(row
) in
1461 let y = (row
- first) * 16 in
1466 GlDraw.polygon_mode `both `line
;
1467 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1468 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1469 GlDraw.rect
(0., float (y + 1))
1470 (float (state.w - conf.scrollw
- 1), float (y + 18));
1471 GlDraw.polygon_mode `both `fill
;
1473 GlDraw.color
(1., 1., 1.);
1475 draw_string (float x) (float (y + 16)) s;
1483 let lasty = List.fold_left
drawpage 0 (state.layout) in
1484 GlDraw.color
(0.5, 0.5, 0.5);
1487 (float (state.w - conf.scrollw
), float state.h)
1492 showoutline state.outline;
1494 Glut.swapBuffers
();
1498 let rec f = function
1500 begin match getopaque l.pageno with
1501 | Some opaque
when validopaque opaque
->
1502 let y = y - l.pagedispy
in
1505 let y = l.pagey + y in
1506 match getlink opaque
x y with
1520 let rec f = function
1522 begin match getopaque l.pageno with
1523 | Some opaque
when validopaque opaque
->
1524 let y = y - l.pagedispy
in
1527 let y = l.pagey + y in
1528 if checklink opaque
x y then true else f rest
1539 let mouse ~button ~bstate ~
x ~
y =
1541 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1549 let incr = incr * 2 in
1550 let y = clamp incr in
1553 | Glut.LEFT_BUTTON
when state.outline = None
->
1554 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1555 begin match dest with
1556 | Some
(pageno, top
) ->
1560 if bstate
= Glut.DOWN
1562 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1563 state.mstate
<- Msel
((x, y), (x, y));
1564 Glut.postRedisplay
()
1567 Glut.setCursor
Glut.CURSOR_INHERIT
;
1568 state.mstate
<- Mnone
;
1575 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1578 if state.outline = None
1580 match state.mstate
with
1583 state.mstate
<- Msel
(a, (x, y));
1584 Glut.postRedisplay
()
1588 if state.outline = None
1590 match state.mstate
with
1591 | Mnone
when (checklink x y) ->
1592 Glut.setCursor
Glut.CURSOR_INFO
1595 Glut.setCursor
Glut.CURSOR_INHERIT
1604 if Sys.os_type
= "Win32"
1606 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1608 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1610 Filename.concat
home "llpp"
1614 let ic = open_in_bin
statepath in
1615 let hash = input_value
ic in
1621 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1628 match state.fullscreen
with
1629 | None
-> state.w, state.h
1632 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1633 let oc = open_out_bin
statepath in
1634 output_value
oc pstate
1638 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1643 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1646 state.bookmarks <- statebookmarks;
1647 with Not_found
-> ()
1649 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1652 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1654 if String.length
state.path
= 0
1655 then (prerr_endline
"filename missing"; exit
1)
1660 let _ = Glut.init
Sys.argv
in
1661 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1662 let () = Glut.initWindowSize
state.w state.h in
1663 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1666 if Sys.os_type
= "Unix"
1668 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1670 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1671 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1672 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1673 Unix.bind
sock addr;
1675 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1676 Unix.connect
csock addr;
1677 let ssock, _ = Unix.accept
sock in
1680 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1681 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1685 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1689 let () = Glut.displayFunc
display in
1690 let () = Glut.reshapeFunc
reshape in
1691 let () = Glut.keyboardFunc
keyboard in
1692 let () = Glut.specialFunc
special in
1693 let () = Glut.idleFunc
(Some
idle) in
1694 let () = Glut.mouseFunc
mouse in
1695 let () = Glut.motionFunc
motion in
1696 let () = Glut.passiveMotionFunc
pmotion in
1699 state.csock <- csock;
1700 state.ssock <- ssock;
1701 writecmd csock ("open " ^
name ^
"\000");
1705 let rec handlelablglutbug () =
1708 with Glut.BadEnum
"key in special_of_int" ->
1709 showtext '
!'
" LablGlut bug: special key not recognized";
1710 Glut.swapBuffers
();
1711 handlelablglutbug ()
1713 handlelablglutbug ();