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 highlightlinks
: string -> int -> unit = "ml_highlightlinks";;
11 external getpagewh
: int -> float array
= "ml_getpagewh";;
13 type mstate
= Msel
of ((int * int) * (int * int)) | Mnone
;;
23 type textentry
= (char
* string * onhist
option * onkey
* ondone
)
24 and onkey
= string -> int -> te
25 and ondone
= string -> unit
26 and onhist
= histcmd
-> string
27 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
32 | TEswitch
of textentry
36 { store
= Array.create n v
43 let cblen b
= Array.length b
.store
;;
48 b
.wc
<- (b
.wc
+ 1) mod len;
49 b
.len <- min
(b
.len + 1) len;
52 let cbpeekw b
= b
.store
.(b
.wc
);;
55 if b
.len = 0 then b
.store
.(0) else
56 let rc = b
.rc + dir
in
57 let rc = if rc = -1 then b
.len - 1 else rc in
58 let rc = if rc = b
.len then 0 else rc in
79 { mutable scrollw
: int
80 ; mutable scrollh
: int
81 ; mutable rectsel
: bool
82 ; mutable icase
: bool
83 ; mutable preload
: bool
84 ; mutable pagebias
: int
85 ; mutable verbose
: bool
86 ; mutable scrollincr
: int
87 ; mutable maxhfit
: bool
88 ; mutable crophack
: bool
89 ; mutable autoscroll
: bool
90 ; mutable showall
: bool
91 ; mutable hlinks
: bool
95 type outline
= string * int * int * int;;
97 | Oarray
of outline array
98 | Olist
of outline list
99 | Onarrow
of outline array
* outline array
102 type rect
= (float * float * float * float * float * float * float * float);;
105 { mutable csock
: Unix.file_descr
106 ; mutable ssock
: Unix.file_descr
109 ; mutable rotate
: int
112 ; mutable prevy
: int
114 ; mutable layout
: layout list
115 ; pagemap
: ((int * int * int), string) Hashtbl.t
116 ; mutable pages
: (int * int * int) list
117 ; mutable pagecount
: int
118 ; pagecache
: string circbuf
119 ; mutable inflight
: int
120 ; mutable mstate
: mstate
121 ; mutable searchpattern
: string
122 ; mutable rects
: (int * int * rect
) list
123 ; mutable rects1
: (int * int * rect
) list
124 ; mutable text
: string
125 ; mutable fullscreen
: (int * int) option
126 ; mutable textentry
: textentry
option
127 ; mutable outlines
: outlines
128 ; mutable outline
: (bool * int * int * outline array
* string) option
129 ; mutable bookmarks
: outline list
130 ; mutable path
: string
131 ; mutable invalidated
: int
135 { pat
: string circbuf
136 ; pag
: string circbuf
137 ; nav
: float circbuf
169 ; pagemap
= Hashtbl.create
10
170 ; pagecache
= cbnew 10 ""
181 ; outlines
= Olist
[]
187 { nav
= cbnew 100 0.0
197 Printf.kprintf prerr_endline fmt
199 Printf.kprintf ignore fmt
203 let len = String.length s
in
205 let b = Buffer.create
n in
206 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
207 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
208 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
209 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
210 Buffer.add_string
b s
;
211 let s'
= Buffer.contents
b in
212 let n'
= Unix.write fd
s'
0 n in
213 if n'
!= n then failwith
"write failed";
218 let n = Unix.read fd
s 0 4 in
219 if n != 4 then failwith
"incomplete read(len)";
221 lor (Char.code
s.[0] lsl 24)
222 lor (Char.code
s.[1] lsl 16)
223 lor (Char.code
s.[2] lsl 8)
224 lor (Char.code
s.[3] lsl 0)
226 let s = String.create
len in
227 let n = Unix.read fd
s 0 len in
228 if n != len then failwith
"incomplete read(data)";
233 if y
= state.maxy
then 1.0
234 else float y
/. float state.maxy
238 let b = Buffer.create
10 in
239 Buffer.add_string
b s;
240 let rec combine = function
243 Buffer.add_char
b ' '
;
246 | `
b b -> if b then "1" else "0"
248 | `i i
-> string_of_int i
249 | `f f
-> string_of_float f
250 | `I f
-> string_of_int
(truncate f
)
252 Buffer.add_string
b s;
259 let cmd = Buffer.contents
(makecmd s l
) in
260 writecmd state.csock
cmd;
264 let rec f pn ph fh l
=
266 | (n, _
, h
) :: rest
->
267 let fh = fh + (n - pn
) * ph
in
271 let fh = fh + (ph
* (state.pagecount
- pn
)) in
274 let fh = f 0 0 0 state.pages
in
278 let getpagey pageno
=
279 let rec f pn ph y l
=
281 | (n, _
, h
) :: rest
->
284 y
+ (pageno
- pn
) * ph
286 let y = y + (n - pn
) * ph
in
290 y + (pageno
- pn
) * ph
296 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
297 if pageno
= state.pagecount
|| cacheleft
= 0
300 let ((_
, w
, h
) as curr
), rest
, pdimno
=
302 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
303 curr
, rest
, pdimno
+ 1
307 let pageno'
= pageno + 1 in
341 let accu = e :: accu in
342 f pageno' pdimno curr
343 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
344 (pred cacheleft
) accu
346 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
348 if state.invalidated
= 0
350 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
351 state.maxy
<- calcheight ();
358 let y = state.y + incr
in
360 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
364 let getopaque pageno =
365 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
367 with Not_found
-> None
370 let cache pageno opaque
=
371 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
375 let validopaque opaque
= String.length opaque
> 0;;
378 match getopaque l
.pageno with
379 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
380 state.inflight
<- succ
state.inflight
;
382 wcmd "render" [`i
(l
.pageno + 1)
392 let y = min
state.maxy
y in
393 let pages = layout y state.h
in
394 let rec f all
= function
396 begin match getopaque l
.pageno with
397 | None
-> preload l
; f false ls
398 | Some opaque
-> f (all
&& validopaque opaque
) ls
402 if not
conf.showall
|| f true pages
405 state.layout <- pages;
408 if conf.preload then begin
410 let y = if state.y < state.h then 0 else state.y - state.h in
411 let pages = layout y (h*3) in
412 List.iter
preload pages;
417 cbput state.hists
.nav
(yratio state.y);
418 cbrfollowlen state.hists
.nav
;
422 let y = cbget state.hists
.nav ~
-1 in
423 truncate
(y *. float state.maxy
)
427 let y = getpagey n in
437 state.invalidated
<- state.invalidated
+ 1;
441 let ratio = float w
/. float state.w
in
442 let fixbookmark (s, l
, pageno, pagey
) =
443 let pagey = truncate
(float pagey *. ratio) in
444 (s, l
, pageno, pagey)
446 state.bookmarks
<- List.map
fixbookmark state.bookmarks
;
449 GlDraw.viewport
0 0 w
h;
450 GlMat.mode `modelview
;
451 GlMat.load_identity
();
452 GlMat.mode `projection
;
453 GlMat.load_identity
();
454 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
455 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
456 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h, 1.0);
457 GlClear.color
(1., 1., 1.);
458 GlClear.clear
[`color
];
460 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i
h];
464 GlDraw.color
(0.0, 0.0, 0.0);
466 (0.0, float (state.h - 18))
467 (float (state.w
- conf.scrollw
- 1), float state.h)
469 let font = Glut.BITMAP_8_BY_13
in
470 GlDraw.color
(1.0, 1.0, 1.0);
471 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h - 5)) ();
472 Glut.bitmapCharacter ~
font ~c
:(Char.code c
);
473 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s;
477 let len = String.length
state.text
in
478 match state.textentry
with
480 if len > 0 then showtext ' '
state.text
482 | Some
(c
, text
, _
, _
, _
) ->
486 text ^
" [" ^
state.text ^
"]"
497 state.outlines
<- Olist
[]
500 state.rects
<- state.rects1
;
501 Glut.postRedisplay
()
504 state.rects
<- state.rects1
;
505 Glut.postRedisplay
()
508 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
509 state.pagecount
<- n;
510 state.invalidated
<- state.invalidated
- 1;
511 if state.invalidated
= 0
513 let rely = yratio state.y in
514 let maxy = calcheight () in
515 state.y <- truncate
(float maxy *. rely);
517 let pages = layout state.y state.h in
518 state.layout <- pages;
519 Glut.postRedisplay
();
523 let s = Scanf.sscanf
cmd "t %n"
524 (fun n -> String.sub
cmd n (String.length
cmd - n))
526 Glut.setWindowTitle
s
529 let s = Scanf.sscanf
cmd "T %n"
530 (fun n -> String.sub
cmd n (String.length
cmd - n))
532 if state.textentry
= None
540 Glut.postRedisplay
();
546 let s = Scanf.sscanf
cmd "V %n"
547 (fun n -> String.sub
cmd n (String.length
cmd - n))
554 let pageno, c
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
555 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
556 (fun p c x0 y0 x1 y1 x2 y2 x3 y3
->
557 (p
, c
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
559 let y = (getpagey pageno) + truncate y0
in
562 state.rects1
<- [pageno, c
, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
565 let pageno, c
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
566 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
567 (fun p c x0 y0 x1 y1 x2 y2 x3 y3
->
568 (p
, c
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
571 (pageno, c
, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
575 Scanf.sscanf
cmd "r %d %d %d %d %s"
576 (fun n w
h r p
-> (n, w
, h, r
, p
))
578 Hashtbl.replace
state.pagemap
(n, w
, r
) p
;
579 let evicted = cbpeekw state.pagecache
in
580 if String.length
evicted > 0
582 wcmd "free" [`
s evicted];
583 let l = Hashtbl.fold
(fun k p a
->
584 if evicted = p
then k
:: a
else a
) state.pagemap
[]
586 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
588 cbput state.pagecache p
;
589 state.inflight
<- pred
state.inflight
;
590 if conf.showall
then gotoy state.ty
;
591 Glut.postRedisplay
()
594 let (n, w
, h) as pagelayout
=
595 Scanf.sscanf
cmd "l %d %d %d" (fun n w
h -> n, w
, h)
597 state.pages <- pagelayout
:: state.pages
601 Scanf.sscanf
cmd "o %d %d %d %n" (fun l n t pos
-> l, n, t
, pos
)
603 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
604 let outline = (s, l, n, t
) in
606 match state.outlines with
607 | Olist
outlines -> Olist
(outline :: outlines)
608 | Oarray _
-> Olist
[outline]
609 | Onarrow _
-> Olist
[outline]
611 state.outlines <- outlines
614 log "unknown cmd `%S'" cmd
618 if state.y != state.prevy
620 state.prevy
<- state.y;
621 Glut.postRedisplay
();
624 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.001 in
628 if conf.autoscroll
then begin
629 let y = state.y + conf.scrollincr
in
630 let y = if y >= state.maxy then 0 else y in
633 state.prevy
<- state.y;
634 Glut.postRedisplay
();
638 let cmd = readcmd state.csock
in
643 let onhist cb
= function
644 | HCprev
-> cbget cb ~
-1
645 | HCnext
-> cbget cb
1
646 | HCfirst
-> cbget cb ~
-(cb
.rc)
647 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
650 let search pattern forward
=
651 if String.length pattern
> 0
654 match state.layout with
657 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
660 let b = makecmd "search"
661 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
663 Buffer.add_char
b '
,'
;
664 Buffer.add_string
b pattern
;
665 Buffer.add_char
b '
\000'
;
668 writecmd state.csock
cmd;
671 let intentry text key
=
672 let c = Char.unsafe_chr key
in
675 let s = "x" in s.[0] <- c;
676 let text = text ^
s in
680 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
685 let b = Buffer.create
(String.length
s + 1) in
686 Buffer.add_string
b s;
691 let textentry text key
=
692 let c = Char.unsafe_chr key
in
694 | _
when key
>= 32 && key
< 127 ->
695 let text = addchar text c in
699 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
704 state.rotate <- angle
;
706 wcmd "rotate" [`i angle
];
709 let optentry text key
=
710 let btos b = if b then "on" else "off" in
711 let c = Char.unsafe_chr key
in
714 conf.rectsel
<- not
conf.rectsel
;
715 TEdone
("rectsel " ^
(btos conf.rectsel
))
719 try conf.scrollincr
<- int_of_string
s with exc
->
720 state.text <- Printf.sprintf
"bad integer `%s': %s"
721 s (Printexc.to_string exc
)
723 TEswitch
('#'
, "", None
, intentry, ondone)
728 Some
(int_of_string
s)
730 state.text <- Printf.sprintf
"bad integer `%s': %s"
731 s (Printexc.to_string exc
);
734 | Some angle
-> rotate angle
737 TEswitch
('^'
, "", None
, intentry, ondone)
740 conf.icase
<- not
conf.icase
;
741 TEdone
("case insensitive search " ^
(btos conf.icase
))
744 conf.preload <- not
conf.preload;
746 TEdone
("preload " ^
(btos conf.preload))
749 conf.verbose
<- not
conf.verbose
;
750 TEdone
("verbose " ^
(btos conf.verbose
))
753 conf.maxhfit
<- not
conf.maxhfit
;
754 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
755 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
758 conf.crophack
<- not
conf.crophack
;
759 TEdone
("crophack " ^
btos conf.crophack
)
762 conf.showall
<- not
conf.showall
;
763 TEdone
("showall " ^
btos conf.showall
)
766 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
770 let maxoutlinerows () = (state.h - 31) / 16;;
772 let enterselector allowdel
outlines errmsg
=
773 if Array.length
outlines = 0
780 match state.layout with
782 | {pageno=pageno} :: rest
-> pageno
786 if n = Array.length
outlines
789 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
790 if outlinepageno
>= pageno then n else loop (n+1)
795 Some
(allowdel
, active,
796 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
797 Glut.postRedisplay
();
800 let enteroutlinemode () =
802 match state.outlines with
805 let a = Array.of_list
(List.rev
l) in
806 state.outlines <- Oarray
a;
808 | Onarrow
(a, b) -> a
810 enterselector false outlines "Documents has no outline";
813 let enterbookmarkmode () =
814 let bookmarks = Array.of_list
state.bookmarks in
815 enterselector true bookmarks "Documents has no bookmarks (yet)";
819 let quickbookmark ?title
() =
820 match state.layout with
826 let sec = Unix.gettimeofday
() in
827 let tm = Unix.localtime
sec in
828 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
832 (tm.Unix.tm_year
+ 1900)
835 | Some
title -> title
838 (title, 0, l.pageno, l.pagey) :: state.bookmarks
841 let viewkeyboard ~key ~x ~
y =
843 state.textentry <- te
;
846 Glut.postRedisplay
()
848 match state.textentry with
850 let c = Char.chr key
in
865 Glut.postRedisplay
()
868 let ondone isforw
s =
869 cbput state.hists
.pat
s;
870 cbrfollowlen state.hists
.pat
;
871 state.searchpattern
<- s;
874 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
875 textentry, ondone (c ='
/'
)))
880 try int_of_string
s with exc
->
881 state.text <- Printf.sprintf
"bad integer `%s': %s"
882 s (Printexc.to_string exc
);
888 state.text <- "page bias is now " ^ string_of_int
n;
891 enttext (Some
('
+'
, "", None
, intentry, ondone))
897 enttext (Some
('
-'
, "", None
, optentry, ondone))
902 try int_of_string
s with exc
->
903 state.text <- Printf.sprintf
"bad integer `%s': %s"
904 s (Printexc.to_string exc
);
910 cbput state.hists
.pag
(string_of_int
n);
911 cbrfollowlen state.hists
.pag
;
912 gotoy (getpagey (n + conf.pagebias
- 1))
915 let pageentry text key
=
916 match Char.unsafe_chr key
with
918 | _
-> intentry text key
920 let text = "x" in text.[0] <- c;
921 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
925 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
926 reshape state.w
state.h;
929 conf.hlinks
<- not
conf.hlinks
;
930 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
931 Glut.postRedisplay
()
934 conf.autoscroll
<- not
conf.autoscroll
937 begin match state.fullscreen
with
939 state.fullscreen
<- Some
(state.w
, state.h);
942 state.fullscreen
<- None
;
943 Glut.reshapeWindow ~w ~
h
950 search state.searchpattern
true
953 search state.searchpattern
false
956 begin match state.layout with
959 gotoy (state.y - l.pagey);
963 begin match List.rev
state.layout with
966 gotoy (clamp (l.pageh
- l.pagey))
970 begin match state.layout with
973 gotoy (clamp (-l.pageh
));
978 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
980 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
982 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
986 else (100. *. (float state.y /. float maxy)) in
989 Printf.sprintf
"Page %d of %d %.2f%%"
990 (fn+1) state.pagecount
percent
993 "Pages %d-%d of %d %.2f%%"
994 (fn+1) (ln
+1) state.pagecount
percent
1000 begin match state.layout with
1003 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
1004 Glut.postRedisplay
();
1008 enterbookmarkmode ()
1012 match state.layout with
1014 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1017 enttext (Some
('~'
, "", None
, textentry, ondone))
1021 showtext ' '
"Quick bookmark added";
1025 begin match state.layout with
1027 let a = getpagewh
l.pagedimno
in
1031 (truncate
(1.8 *. (a.(1) -. a.(0))),
1032 truncate
(1.2 *. (a.(3) -. a.(0))))
1034 (truncate
(a.(1) -. a.(0)),
1035 truncate
(a.(3) -. a.(0)))
1037 Glut.reshapeWindow
(w + conf.scrollw
) h;
1038 Glut.postRedisplay
();
1044 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1047 vlog "huh? %d %c" key
(Char.chr key
);
1050 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1051 let len = String.length
text in
1054 state.textentry <- None
;
1055 Glut.postRedisplay
();
1058 let s = String.sub
text 0 (len - 1) in
1059 enttext (Some
(c, s, onhist, onkey
, ondone))
1062 | Some
(c, text, onhist, onkey
, ondone) ->
1063 begin match Char.unsafe_chr key
with
1066 state.textentry <- None
;
1067 Glut.postRedisplay
()
1070 state.textentry <- None
;
1071 Glut.postRedisplay
()
1074 begin match onkey
text key
with
1076 state.textentry <- None
;
1078 Glut.postRedisplay
()
1081 enttext (Some
(c, text, onhist, onkey
, ondone));
1084 state.textentry <- None
;
1085 Glut.postRedisplay
()
1088 state.textentry <- Some te
;
1089 Glut.postRedisplay
()
1094 let narrow outlines pattern
=
1095 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1099 let rec fold accu n =
1100 if n = -1 then accu else
1101 let (s, _
, _
, _
) as o
= outlines.(n) in
1103 if (try ignore
(Str.search_forward re
s 0); true
1104 with Not_found
-> false)
1110 let matched = fold [] (Array.length
outlines - 1) in
1111 if matched = [] then None
else Some
(Array.of_list
matched)
1114 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1115 let search active pattern incr
=
1118 if n = Array.length
outlines || n = -1 then None
else
1119 let (s, _
, _
, _
) = outlines.(n) in
1121 (try ignore
(Str.search_forward re
s 0); true
1122 with Not_found
-> false)
1124 else loop (n + incr
)
1129 let re = Str.regexp_case_fold pattern
in
1135 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1138 if String.length qsearch
= 0
1141 state.outline <- None
;
1142 Glut.postRedisplay
();
1146 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1147 Glut.postRedisplay
();
1151 let incr = if key
= 18 then -1 else 1 in
1153 match search (active + incr) qsearch
incr with
1155 state.text <- qsearch ^
" [not found]";
1158 state.text <- qsearch
;
1159 active, firstof active
1161 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1162 Glut.postRedisplay
();
1165 let len = String.length qsearch
in
1172 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1175 let qsearch = String.sub
qsearch 0 (len - 1) in
1177 match search active qsearch ~
-1 with
1179 state.text <- qsearch ^
" [not found]";
1182 state.text <- qsearch;
1183 active, firstof active
1185 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1187 Glut.postRedisplay
()
1190 if active < Array.length
outlines
1192 let (_
, _
, n, t
) = outlines.(active) in
1196 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1197 state.outline <- None
;
1198 Glut.postRedisplay
();
1200 | _
when key
>= 32 && key
< 127 ->
1201 let pattern = addchar qsearch (Char.chr key
) in
1203 match search active pattern 1 with
1205 state.text <- pattern ^
" [not found]";
1208 state.text <- pattern;
1209 active, firstof active
1211 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1212 Glut.postRedisplay
()
1214 | 14 when not allowdel
->
1215 let optoutlines = narrow outlines qsearch in
1216 begin match optoutlines with
1217 | None
-> state.text <- "can't narrow"
1219 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1220 match state.outlines with
1222 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1223 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1225 Glut.postRedisplay
()
1227 | 21 when not allowdel
->
1229 match state.outlines with
1232 let a = Array.of_list
(List.rev
l) in
1233 state.outlines <- Oarray
a;
1236 state.outlines <- Oarray
b;
1239 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1240 Glut.postRedisplay
()
1244 Some
(allowdel
, active, firstof active, outlines, qsearch);
1245 Glut.postRedisplay
()
1247 | 127 when allowdel
->
1248 let len = Array.length
outlines - 1 in
1251 state.outline <- None
;
1252 state.bookmarks <- [];
1255 let bookmarks = Array.init
len
1257 let i = if i >= active then i + 1 else i in
1268 Glut.postRedisplay
()
1270 | _
-> log "unknown key %d" key
1273 let keyboard ~key ~x ~
y =
1278 match state.outline with
1279 | None
-> viewkeyboard ~key ~x ~
y
1280 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1283 let special ~key ~x ~
y =
1284 match state.outline with
1286 begin match state.textentry with
1290 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1291 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1292 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1293 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1294 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1295 | Glut.KEY_HOME
-> addnav (); 0
1298 state.maxy - (if conf.maxhfit
then state.h else 0)
1304 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1307 | Glut.KEY_UP
-> onhist HCprev
1308 | Glut.KEY_DOWN
-> onhist HCnext
1309 | Glut.KEY_HOME
-> onhist HCfirst
1310 | Glut.KEY_END
-> onhist HClast
1313 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1314 Glut.postRedisplay
()
1319 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1320 let maxrows = maxoutlinerows () in
1322 let active = active + incr in
1323 let active = max
0 (min
active (Array.length
outlines - 1)) in
1327 let rows = active - first in
1328 if rows > maxrows then active - maxrows else first
1331 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1332 Glut.postRedisplay
()
1335 | Glut.KEY_UP
-> navigate ~
-1
1336 | Glut.KEY_DOWN
-> navigate 1
1337 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1338 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1341 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1342 Glut.postRedisplay
()
1345 let active = Array.length
outlines - 1 in
1346 let first = max
0 (active - maxrows) in
1347 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1348 Glut.postRedisplay
()
1353 let drawplaceholder l =
1354 GlDraw.color
(1.0, 1.0, 1.0);
1356 (0.0, float l.pagedispy
)
1357 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1360 and y = float (l.pagedispy
+ 13) in
1361 let font = Glut.BITMAP_8_BY_13
in
1362 GlDraw.color
(0.0, 0.0, 0.0);
1363 GlPix.raster_pos ~
x ~
y ();
1364 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1365 ("Loading " ^ string_of_int
l.pageno);
1368 let now () = Unix.gettimeofday
();;
1371 begin match getopaque l.pageno with
1372 | Some opaque
when validopaque opaque
->
1373 if state.textentry = None
1374 then GlDraw.color
(1.0, 1.0, 1.0)
1375 else GlDraw.color
(0.4, 0.4, 0.4);
1377 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1380 if conf.hlinks
then highlightlinks opaque
(l.pagedispy
- l.pagey);
1381 vlog "draw %f sec" d;
1388 if state.inflight
< cblen state.pagecache
1390 List.iter
preload state.layout;
1393 vlog "inflight %d" state.inflight
;
1396 GlDraw.color
(0.5, 0.5, 0.5);
1399 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1401 l.pagedispy
+ l.pagevh
;
1404 let scrollindicator () =
1405 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1406 GlDraw.color
(0.64 , 0.64, 0.64);
1408 (float (state.w - conf.scrollw
), 0.)
1409 (float state.w, float state.h)
1411 GlDraw.color
(0.0, 0.0, 0.0);
1412 let sh = (float (maxy + state.h) /. float state.h) in
1413 let sh = float state.h /. sh in
1414 let sh = max
sh (float conf.scrollh
) in
1417 if state.y = state.maxy
1419 else float state.y /. float maxy
1421 let position = (float state.h -. sh) *. percent in
1424 if position +. sh > float state.h
1431 (float (state.w - conf.scrollw
), position)
1432 (float state.w, position +. sh)
1437 match state.mstate
with
1441 | Msel
((x0
, y0
), (x1
, y1
)) ->
1443 and y1
= max
y0 y1
in
1446 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1447 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1449 match getopaque l.pageno with
1450 | Some opaque
when validopaque opaque
->
1451 let oy = -l.pagey + l.pagedispy
in
1452 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1455 List.iter
f state.layout
1460 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1461 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1463 (fun (pageno, c, (x0
, y0, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1465 if l.pageno = pageno
1467 let d = float (l.pagedispy
- l.pagey) in
1468 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1469 GlDraw.begins `quads
;
1471 GlDraw.vertex2
(x0
, y0+.d);
1472 GlDraw.vertex2
(x1
, y1
+.d);
1473 GlDraw.vertex2
(x2
, y2
+.d);
1474 GlDraw.vertex2
(x3
, y3
+.d);
1477 (* GlDraw.rect (x0, y0 +. d) (x1, y1 +. d) *)
1485 let showoutline = function
1487 | Some
(allowdel
, active, first, outlines, qsearch) ->
1489 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1490 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1491 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1494 GlDraw.color
(1., 1., 1.);
1495 let font = Glut.BITMAP_9_BY_15
in
1496 let draw_string x y s =
1497 GlPix.raster_pos ~
x ~
y ();
1498 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1501 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1504 let (s, l, _
, _
) = outlines.(row
) in
1505 let y = (row
- first) * 16 in
1510 GlDraw.polygon_mode `both `line
;
1511 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1512 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1513 GlDraw.rect
(0., float (y + 1))
1514 (float (state.w - conf.scrollw
- 1), float (y + 18));
1515 GlDraw.polygon_mode `both `fill
;
1517 GlDraw.color
(1., 1., 1.);
1519 draw_string (float x) (float (y + 16)) s;
1527 let lasty = List.fold_left
drawpage 0 (state.layout) in
1528 GlDraw.color
(0.5, 0.5, 0.5);
1531 (float (state.w - conf.scrollw
), float state.h)
1536 showoutline state.outline;
1538 Glut.swapBuffers
();
1542 let rec f = function
1544 begin match getopaque l.pageno with
1545 | Some opaque
when validopaque opaque
->
1546 let y = y - l.pagedispy
in
1549 let y = l.pagey + y in
1550 match getlink opaque
x y with
1564 let rec f = function
1566 begin match getopaque l.pageno with
1567 | Some opaque
when validopaque opaque
->
1568 let y = y - l.pagedispy
in
1571 let y = l.pagey + y in
1572 if checklink opaque
x y then true else f rest
1583 let mouse ~button ~bstate ~
x ~
y =
1585 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1593 let incr = incr * 2 in
1594 let y = clamp incr in
1597 | Glut.LEFT_BUTTON
when state.outline = None
->
1598 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1599 begin match dest with
1600 | Some
(pageno, top
) ->
1606 if bstate
= Glut.DOWN
1608 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1609 state.mstate
<- Msel
((x, y), (x, y));
1610 Glut.postRedisplay
()
1613 Glut.setCursor
Glut.CURSOR_INHERIT
;
1614 state.mstate
<- Mnone
;
1621 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1624 if state.outline = None
1626 match state.mstate
with
1629 state.mstate
<- Msel
(a, (x, y));
1630 Glut.postRedisplay
()
1634 if state.outline = None
1636 match state.mstate
with
1637 | Mnone
when (checklink x y) ->
1638 Glut.setCursor
Glut.CURSOR_INFO
1641 Glut.setCursor
Glut.CURSOR_INHERIT
1650 if Sys.os_type
= "Win32"
1652 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1654 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1656 Filename.concat
home "llpp"
1660 let ic = open_in_bin
statepath in
1661 let hash = input_value
ic in
1667 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1674 match state.fullscreen
with
1675 | None
-> state.w, state.h
1678 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1679 let oc = open_out_bin
statepath in
1680 output_value
oc pstate
1684 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1689 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1692 state.bookmarks <- statebookmarks;
1693 with Not_found
-> ()
1695 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1698 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1700 if String.length
state.path
= 0
1701 then (prerr_endline
"filename missing"; exit
1)
1706 let _ = Glut.init
Sys.argv
in
1707 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1708 let () = Glut.initWindowSize
state.w state.h in
1709 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1712 if Sys.os_type
= "Unix"
1714 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1716 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1717 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1718 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1719 Unix.bind
sock addr;
1721 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1722 Unix.connect
csock addr;
1723 let ssock, _ = Unix.accept
sock in
1726 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1727 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1731 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1735 let () = Glut.displayFunc
display in
1736 let () = Glut.reshapeFunc
reshape in
1737 let () = Glut.keyboardFunc
keyboard in
1738 let () = Glut.specialFunc
special in
1739 let () = Glut.idleFunc
(Some
idle) in
1740 let () = Glut.mouseFunc
mouse in
1741 let () = Glut.motionFunc
motion in
1742 let () = Glut.passiveMotionFunc
pmotion in
1745 state.csock <- csock;
1746 state.ssock <- ssock;
1747 writecmd csock ("open " ^
name ^
"\000");
1751 let rec handlelablglutbug () =
1754 with Glut.BadEnum
"key in special_of_int" ->
1755 showtext '
!'
" LablGlut bug: special key not recognized";
1756 Glut.swapBuffers
();
1757 handlelablglutbug ()
1759 handlelablglutbug ();