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 redispimm
: bool
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
135 { pat
: string circbuf
136 ; pag
: string circbuf
137 ; nav
: float circbuf
170 ; pagemap
= Hashtbl.create
10
171 ; pagecache
= cbnew 10 ""
182 ; 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 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
349 state.maxy
<- calcheight ();
354 let y = state.y + incr
in
356 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
360 let getopaque pageno =
361 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
363 with Not_found
-> None
366 let cache pageno opaque
=
367 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
371 let validopaque opaque
= String.length opaque
> 0;;
374 match getopaque l
.pageno with
375 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
376 state.inflight
<- succ
state.inflight
;
378 wcmd "render" [`i
(l
.pageno + 1)
388 let y = min
state.maxy
y in
389 let pages = layout y state.h
in
390 let rec f all
= function
392 begin match getopaque l
.pageno with
393 | None
-> preload l
; f false ls
394 | Some opaque
-> f (all
&& validopaque opaque
) ls
398 if not
conf.showall
|| f true pages
401 state.layout <- pages;
406 Glut.postRedisplay
()
411 cbput state.hists
.nav
(yratio state.y);
412 cbrfollowlen state.hists
.nav
;
416 let y = cbget state.hists
.nav ~
-1 in
417 truncate
(y *. float state.maxy
)
421 let y = getpagey n in
427 let ratio = float w
/. float state.w
in
428 let fixbookmark (s, l
, pageno, pagey
) =
429 let pagey = truncate
(float pagey *. ratio) in
430 (s, l
, pageno, pagey)
432 state.bookmarks
<- List.map
fixbookmark state.bookmarks
;
435 GlDraw.viewport
0 0 w h
;
436 GlMat.mode `modelview
;
437 GlMat.load_identity
();
438 GlMat.mode `projection
;
439 GlMat.load_identity
();
440 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
441 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
442 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
443 GlClear.color
(1., 1., 1.);
444 GlClear.clear
[`color
];
449 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
453 GlDraw.color
(0.0, 0.0, 0.0);
455 (0.0, float (state.h
- 18))
456 (float (state.w
- conf.scrollw
- 1), float state.h
)
458 let font = Glut.BITMAP_8_BY_13
in
459 GlDraw.color
(1.0, 1.0, 1.0);
460 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
461 Glut.bitmapCharacter ~
font ~c
:(Char.code c
);
462 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s;
466 let len = String.length
state.text
in
467 match state.textentry
with
469 if len > 0 then showtext ' '
state.text
471 | Some
(c
, text
, _
, _
, _
) ->
475 text ^
" [" ^
state.text ^
"]"
486 state.outlines
<- Olist
[]
489 state.rects
<- state.rects1
;
490 Glut.postRedisplay
()
493 state.rects
<- state.rects1
;
494 Glut.postRedisplay
()
497 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
498 state.pagecount
<- n;
499 let rely = yratio state.y in
500 let maxy = calcheight () in
501 state.y <- truncate
(float maxy *. rely);
503 let pages = layout state.y state.h
in
504 state.layout <- pages;
505 Glut.postRedisplay
();
508 let s = Scanf.sscanf
cmd "t %n"
509 (fun n -> String.sub
cmd n (String.length
cmd - n))
511 Glut.setWindowTitle
s
514 let s = Scanf.sscanf
cmd "T %n"
515 (fun n -> String.sub
cmd n (String.length
cmd - n))
517 if state.textentry
= None
525 Glut.postRedisplay
();
531 let s = Scanf.sscanf
cmd "V %n"
532 (fun n -> String.sub
cmd n (String.length
cmd - n))
539 let pageno, c
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
540 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
541 (fun p c x0 y0 x1 y1 x2 y2 x3 y3
->
542 (p
, c
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
544 let y = (getpagey pageno) + truncate y0
in
547 state.rects1
<- [pageno, c
, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
550 let pageno, c
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
551 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
552 (fun p c x0 y0 x1 y1 x2 y2 x3 y3
->
553 (p
, c
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
556 (pageno, c
, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
560 Scanf.sscanf
cmd "r %d %d %d %d %s"
561 (fun n w h r p
-> (n, w
, h
, r
, p
))
563 Hashtbl.replace
state.pagemap
(n, w
, r
) p
;
564 let evicted = cbpeekw state.pagecache
in
565 if String.length
evicted > 0
567 wcmd "free" [`
s evicted];
568 let l = Hashtbl.fold
(fun k p a
->
569 if evicted = p
then k
:: a
else a
) state.pagemap
[]
571 List.iter
(fun k
-> Hashtbl.remove
state.pagemap k
) l;
573 cbput state.pagecache p
;
574 state.inflight
<- pred
state.inflight
;
575 if conf.showall
then gotoy state.ty
;
576 Glut.postRedisplay
()
579 let (n, w
, h
) as pagelayout
=
580 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
582 state.pages <- pagelayout
:: state.pages
586 Scanf.sscanf
cmd "o %d %d %d %n" (fun l n t pos
-> l, n, t
, pos
)
588 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
589 let outline = (s, l, n, t
) in
591 match state.outlines with
592 | Olist
outlines -> Olist
(outline :: outlines)
593 | Oarray _
-> Olist
[outline]
594 | Onarrow _
-> Olist
[outline]
596 state.outlines <- outlines
599 log "unknown cmd `%S'" cmd
603 if not
conf.redispimm
&& state.y != state.prevy
605 state.prevy
<- state.y;
606 Glut.postRedisplay
();
609 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.001 in
613 if conf.preload then begin
615 let y = if state.y < state.h then 0 else state.y - state.h in
616 let pages = layout y (h*3) in
617 List.iter
preload pages;
619 if conf.autoscroll
then begin
620 let y = state.y + conf.scrollincr
in
621 let y = if y >= state.maxy then 0 else y in
624 state.prevy
<- state.y;
625 Glut.postRedisplay
();
629 let cmd = readcmd state.csock
in
634 let onhist cb
= function
635 | HCprev
-> cbget cb ~
-1
636 | HCnext
-> cbget cb
1
637 | HCfirst
-> cbget cb ~
-(cb
.rc)
638 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
641 let search pattern forward
=
642 if String.length pattern
> 0
645 match state.layout with
648 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
651 let b = makecmd "search"
652 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
654 Buffer.add_char
b '
,'
;
655 Buffer.add_string
b pattern
;
656 Buffer.add_char
b '
\000'
;
659 writecmd state.csock
cmd;
662 let intentry text key
=
663 let c = Char.unsafe_chr key
in
666 let s = "x" in s.[0] <- c;
667 let text = text ^
s in
671 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
676 let b = Buffer.create
(String.length
s + 1) in
677 Buffer.add_string
b s;
682 let textentry text key
=
683 let c = Char.unsafe_chr key
in
685 | _
when key
>= 32 && key
< 127 ->
686 let text = addchar text c in
690 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
697 state.rotate <- angle
;
698 wcmd "rotate" [`i angle
];
701 let optentry text key
=
702 let btos b = if b then "on" else "off" in
703 let c = Char.unsafe_chr key
in
706 conf.rectsel
<- not
conf.rectsel
;
707 TEdone
("rectsel " ^
(btos conf.rectsel
))
711 try conf.scrollincr
<- int_of_string
s with exc
->
712 state.text <- Printf.sprintf
"bad integer `%s': %s"
713 s (Printexc.to_string exc
)
715 TEswitch
('#'
, "", None
, intentry, ondone)
720 Some
(int_of_string
s)
722 state.text <- Printf.sprintf
"bad integer `%s': %s"
723 s (Printexc.to_string exc
);
726 | Some angle
-> rotate angle
729 TEswitch
('^'
, "", None
, intentry, ondone)
732 conf.icase
<- not
conf.icase
;
733 TEdone
("case insensitive search " ^
(btos conf.icase
))
736 conf.preload <- not
conf.preload;
737 TEdone
("preload " ^
(btos conf.preload))
740 conf.redispimm
<- not
conf.redispimm
;
741 TEdone
("immediate redisplay " ^
(btos conf.redispimm
))
744 conf.verbose
<- not
conf.verbose
;
745 TEdone
("verbose " ^
(btos conf.verbose
))
748 conf.maxhfit
<- not
conf.maxhfit
;
749 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
750 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
753 conf.crophack
<- not
conf.crophack
;
754 TEdone
("crophack " ^
btos conf.crophack
)
757 conf.showall
<- not
conf.showall
;
758 TEdone
("showall " ^
btos conf.showall
)
761 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
765 let maxoutlinerows () = (state.h - 31) / 16;;
767 let enterselector allowdel
outlines errmsg
=
768 if Array.length
outlines = 0
775 match state.layout with
777 | {pageno=pageno} :: rest
-> pageno
781 if n = Array.length
outlines
784 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
785 if outlinepageno
>= pageno then n else loop (n+1)
790 Some
(allowdel
, active,
791 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
792 Glut.postRedisplay
();
795 let enteroutlinemode () =
797 match state.outlines with
800 let a = Array.of_list
(List.rev
l) in
801 state.outlines <- Oarray
a;
803 | Onarrow
(a, b) -> a
805 enterselector false outlines "Documents has no outline";
808 let enterbookmarkmode () =
809 let bookmarks = Array.of_list
state.bookmarks in
810 enterselector true bookmarks "Documents has no bookmarks (yet)";
814 let quickbookmark ?title
() =
815 match state.layout with
821 let sec = Unix.gettimeofday
() in
822 let tm = Unix.localtime
sec in
823 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
827 (tm.Unix.tm_year
+ 1900)
830 | Some
title -> title
833 (title, 0, l.pageno, l.pagey) :: state.bookmarks
836 let viewkeyboard ~key ~x ~
y =
838 state.textentry <- te
;
841 Glut.postRedisplay
()
843 match state.textentry with
845 let c = Char.chr key
in
860 Glut.postRedisplay
()
863 let ondone isforw
s =
864 cbput state.hists
.pat
s;
865 cbrfollowlen state.hists
.pat
;
866 state.searchpattern
<- s;
869 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
870 textentry, ondone (c ='
/'
)))
875 try int_of_string
s with exc
->
876 state.text <- Printf.sprintf
"bad integer `%s': %s"
877 s (Printexc.to_string exc
);
883 state.text <- "page bias is now " ^ string_of_int
n;
886 enttext (Some
('
+'
, "", None
, intentry, ondone))
892 enttext (Some
('
-'
, "", None
, optentry, ondone))
897 try int_of_string
s with exc
->
898 state.text <- Printf.sprintf
"bad integer `%s': %s"
899 s (Printexc.to_string exc
);
905 cbput state.hists
.pag
(string_of_int
n);
906 cbrfollowlen state.hists
.pag
;
907 gotoy (getpagey (n + conf.pagebias
- 1))
910 let pageentry text key
=
911 match Char.unsafe_chr key
with
913 | _
-> intentry text key
915 let text = "x" in text.[0] <- c;
916 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
920 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
921 reshape state.w
state.h;
924 conf.hlinks
<- not
conf.hlinks
;
925 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
926 Glut.postRedisplay
()
929 conf.autoscroll
<- not
conf.autoscroll
932 begin match state.fullscreen
with
934 state.fullscreen
<- Some
(state.w
, state.h);
937 state.fullscreen
<- None
;
938 Glut.reshapeWindow ~w ~
h
945 search state.searchpattern
true
948 search state.searchpattern
false
951 begin match state.layout with
954 gotoy (state.y - l.pagey);
958 begin match List.rev
state.layout with
961 gotoy (clamp (l.pageh
- l.pagey))
965 begin match state.layout with
968 gotoy (clamp (-l.pageh
));
973 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
975 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
977 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
981 else (100. *. (float state.y /. float maxy)) in
984 Printf.sprintf
"Page %d of %d %.2f%%"
985 (fn+1) state.pagecount
percent
988 "Pages %d-%d of %d %.2f%%"
989 (fn+1) (ln
+1) state.pagecount
percent
995 begin match state.layout with
998 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
999 Glut.postRedisplay
();
1003 enterbookmarkmode ()
1007 match state.layout with
1009 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1012 enttext (Some
('~'
, "", None
, textentry, ondone))
1016 showtext ' '
"Quick bookmark added";
1020 begin match state.layout with
1022 let a = getpagewh
l.pagedimno
in
1026 (truncate
(1.8 *. (a.(1) -. a.(0))),
1027 truncate
(1.2 *. (a.(3) -. a.(0))))
1029 (truncate
(a.(1) -. a.(0)),
1030 truncate
(a.(3) -. a.(0)))
1032 Glut.reshapeWindow
(w + conf.scrollw
) h;
1033 Glut.postRedisplay
();
1039 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1042 vlog "huh? %d %c" key
(Char.chr key
);
1045 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1046 let len = String.length
text in
1049 state.textentry <- None
;
1050 Glut.postRedisplay
();
1053 let s = String.sub
text 0 (len - 1) in
1054 enttext (Some
(c, s, onhist, onkey
, ondone))
1057 | Some
(c, text, onhist, onkey
, ondone) ->
1058 begin match Char.unsafe_chr key
with
1061 state.textentry <- None
;
1062 Glut.postRedisplay
()
1065 state.textentry <- None
;
1066 Glut.postRedisplay
()
1069 begin match onkey
text key
with
1071 state.textentry <- None
;
1073 Glut.postRedisplay
()
1076 enttext (Some
(c, text, onhist, onkey
, ondone));
1079 state.textentry <- None
;
1080 Glut.postRedisplay
()
1083 state.textentry <- Some te
;
1084 Glut.postRedisplay
()
1089 let narrow outlines pattern
=
1090 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1094 let rec fold accu n =
1095 if n = -1 then accu else
1096 let (s, _
, _
, _
) as o
= outlines.(n) in
1098 if (try ignore
(Str.search_forward re
s 0); true
1099 with Not_found
-> false)
1105 let matched = fold [] (Array.length
outlines - 1) in
1106 if matched = [] then None
else Some
(Array.of_list
matched)
1109 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1110 let search active pattern incr
=
1113 if n = Array.length
outlines || n = -1 then None
else
1114 let (s, _
, _
, _
) = outlines.(n) in
1116 (try ignore
(Str.search_forward re
s 0); true
1117 with Not_found
-> false)
1119 else loop (n + incr
)
1124 let re = Str.regexp_case_fold pattern
in
1130 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1133 if String.length qsearch
= 0
1136 state.outline <- None
;
1137 Glut.postRedisplay
();
1141 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1142 Glut.postRedisplay
();
1146 let incr = if key
= 18 then -1 else 1 in
1148 match search (active + incr) qsearch
incr with
1150 state.text <- qsearch ^
" [not found]";
1153 state.text <- qsearch
;
1154 active, firstof active
1156 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1157 Glut.postRedisplay
();
1160 let len = String.length qsearch
in
1167 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1170 let qsearch = String.sub
qsearch 0 (len - 1) in
1172 match search active qsearch ~
-1 with
1174 state.text <- qsearch ^
" [not found]";
1177 state.text <- qsearch;
1178 active, firstof active
1180 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1182 Glut.postRedisplay
()
1185 if active < Array.length
outlines
1187 let (_
, _
, n, t
) = outlines.(active) in
1191 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1192 state.outline <- None
;
1193 Glut.postRedisplay
();
1195 | _
when key
>= 32 && key
< 127 ->
1196 let pattern = addchar qsearch (Char.chr key
) in
1198 match search active pattern 1 with
1200 state.text <- pattern ^
" [not found]";
1203 state.text <- pattern;
1204 active, firstof active
1206 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1207 Glut.postRedisplay
()
1209 | 14 when not allowdel
->
1210 let optoutlines = narrow outlines qsearch in
1211 begin match optoutlines with
1212 | None
-> state.text <- "can't narrow"
1214 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1215 match state.outlines with
1217 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1218 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1220 Glut.postRedisplay
()
1222 | 21 when not allowdel
->
1224 match state.outlines with
1227 let a = Array.of_list
(List.rev
l) in
1228 state.outlines <- Oarray
a;
1231 state.outlines <- Oarray
b;
1234 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1235 Glut.postRedisplay
()
1239 Some
(allowdel
, active, firstof active, outlines, qsearch);
1240 Glut.postRedisplay
()
1242 | 127 when allowdel
->
1243 let len = Array.length
outlines - 1 in
1246 state.outline <- None
;
1247 state.bookmarks <- [];
1250 let bookmarks = Array.init
len
1252 let i = if i >= active then i + 1 else i in
1263 Glut.postRedisplay
()
1265 | _
-> log "unknown key %d" key
1268 let keyboard ~key ~x ~
y =
1273 match state.outline with
1274 | None
-> viewkeyboard ~key ~x ~
y
1275 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1278 let special ~key ~x ~
y =
1279 match state.outline with
1281 begin match state.textentry with
1285 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1286 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1287 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1288 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1289 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1290 | Glut.KEY_HOME
-> addnav (); 0
1293 state.maxy - (if conf.maxhfit
then state.h else 0)
1299 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1302 | Glut.KEY_UP
-> onhist HCprev
1303 | Glut.KEY_DOWN
-> onhist HCnext
1304 | Glut.KEY_HOME
-> onhist HCfirst
1305 | Glut.KEY_END
-> onhist HClast
1308 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1309 Glut.postRedisplay
()
1314 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1315 let maxrows = maxoutlinerows () in
1317 let active = active + incr in
1318 let active = max
0 (min
active (Array.length
outlines - 1)) in
1322 let rows = active - first in
1323 if rows > maxrows then active - maxrows else first
1326 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1327 Glut.postRedisplay
()
1330 | Glut.KEY_UP
-> navigate ~
-1
1331 | Glut.KEY_DOWN
-> navigate 1
1332 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1333 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1336 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1337 Glut.postRedisplay
()
1340 let active = Array.length
outlines - 1 in
1341 let first = max
0 (active - maxrows) in
1342 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1343 Glut.postRedisplay
()
1348 let drawplaceholder l =
1349 GlDraw.color
(1.0, 1.0, 1.0);
1351 (0.0, float l.pagedispy
)
1352 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1355 and y = float (l.pagedispy
+ 13) in
1356 let font = Glut.BITMAP_8_BY_13
in
1357 GlDraw.color
(0.0, 0.0, 0.0);
1358 GlPix.raster_pos ~
x ~
y ();
1359 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1360 ("Loading " ^ string_of_int
l.pageno);
1363 let now () = Unix.gettimeofday
();;
1366 begin match getopaque l.pageno with
1367 | Some opaque
when validopaque opaque
->
1368 if state.textentry = None
1369 then GlDraw.color
(1.0, 1.0, 1.0)
1370 else GlDraw.color
(0.4, 0.4, 0.4);
1372 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1375 if conf.hlinks
then highlightlinks opaque
(l.pagedispy
- l.pagey);
1376 vlog "draw %f sec" d;
1383 if state.inflight
< cblen state.pagecache
1385 List.iter
preload state.layout;
1388 vlog "inflight %d" state.inflight
;
1391 GlDraw.color
(0.5, 0.5, 0.5);
1394 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1396 l.pagedispy
+ l.pagevh
;
1399 let scrollindicator () =
1400 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1401 GlDraw.color
(0.64 , 0.64, 0.64);
1403 (float (state.w - conf.scrollw
), 0.)
1404 (float state.w, float state.h)
1406 GlDraw.color
(0.0, 0.0, 0.0);
1407 let sh = (float (maxy + state.h) /. float state.h) in
1408 let sh = float state.h /. sh in
1409 let sh = max
sh (float conf.scrollh
) in
1412 if state.y = state.maxy
1414 else float state.y /. float maxy
1416 let position = (float state.h -. sh) *. percent in
1419 if position +. sh > float state.h
1426 (float (state.w - conf.scrollw
), position)
1427 (float state.w, position +. sh)
1432 match state.mstate
with
1436 | Msel
((x0
, y0
), (x1
, y1
)) ->
1438 and y1
= max
y0 y1
in
1441 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1442 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1444 match getopaque l.pageno with
1445 | Some opaque
when validopaque opaque
->
1446 let oy = -l.pagey + l.pagedispy
in
1447 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1450 List.iter
f state.layout
1455 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1456 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1458 (fun (pageno, c, (x0
, y0, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1460 if l.pageno = pageno
1462 let d = float (l.pagedispy
- l.pagey) in
1463 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1464 GlDraw.begins `quads
;
1466 GlDraw.vertex2
(x0
, y0+.d);
1467 GlDraw.vertex2
(x1
, y1
+.d);
1468 GlDraw.vertex2
(x2
, y2
+.d);
1469 GlDraw.vertex2
(x3
, y3
+.d);
1472 (* GlDraw.rect (x0, y0 +. d) (x1, y1 +. d) *)
1480 let showoutline = function
1482 | Some
(allowdel
, active, first, outlines, qsearch) ->
1484 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1485 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1486 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1489 GlDraw.color
(1., 1., 1.);
1490 let font = Glut.BITMAP_9_BY_15
in
1491 let draw_string x y s =
1492 GlPix.raster_pos ~
x ~
y ();
1493 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1496 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1499 let (s, l, _
, _
) = outlines.(row
) in
1500 let y = (row
- first) * 16 in
1505 GlDraw.polygon_mode `both `line
;
1506 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1507 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1508 GlDraw.rect
(0., float (y + 1))
1509 (float (state.w - conf.scrollw
- 1), float (y + 18));
1510 GlDraw.polygon_mode `both `fill
;
1512 GlDraw.color
(1., 1., 1.);
1514 draw_string (float x) (float (y + 16)) s;
1522 let lasty = List.fold_left
drawpage 0 (state.layout) in
1523 GlDraw.color
(0.5, 0.5, 0.5);
1526 (float (state.w - conf.scrollw
), float state.h)
1531 showoutline state.outline;
1533 Glut.swapBuffers
();
1537 let rec f = function
1539 begin match getopaque l.pageno with
1540 | Some opaque
when validopaque opaque
->
1541 let y = y - l.pagedispy
in
1544 let y = l.pagey + y in
1545 match getlink opaque
x y with
1559 let rec f = function
1561 begin match getopaque l.pageno with
1562 | Some opaque
when validopaque opaque
->
1563 let y = y - l.pagedispy
in
1566 let y = l.pagey + y in
1567 if checklink opaque
x y then true else f rest
1578 let mouse ~button ~bstate ~
x ~
y =
1580 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1588 let incr = incr * 2 in
1589 let y = clamp incr in
1592 | Glut.LEFT_BUTTON
when state.outline = None
->
1593 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1594 begin match dest with
1595 | Some
(pageno, top
) ->
1601 if bstate
= Glut.DOWN
1603 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1604 state.mstate
<- Msel
((x, y), (x, y));
1605 Glut.postRedisplay
()
1608 Glut.setCursor
Glut.CURSOR_INHERIT
;
1609 state.mstate
<- Mnone
;
1616 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1619 if state.outline = None
1621 match state.mstate
with
1624 state.mstate
<- Msel
(a, (x, y));
1625 Glut.postRedisplay
()
1629 if state.outline = None
1631 match state.mstate
with
1632 | Mnone
when (checklink x y) ->
1633 Glut.setCursor
Glut.CURSOR_INFO
1636 Glut.setCursor
Glut.CURSOR_INHERIT
1645 if Sys.os_type
= "Win32"
1647 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1649 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1651 Filename.concat
home "llpp"
1655 let ic = open_in_bin
statepath in
1656 let hash = input_value
ic in
1662 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1669 match state.fullscreen
with
1670 | None
-> state.w, state.h
1673 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1674 let oc = open_out_bin
statepath in
1675 output_value
oc pstate
1679 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1684 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1687 state.bookmarks <- statebookmarks;
1688 with Not_found
-> ()
1690 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1693 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1695 if String.length
state.path
= 0
1696 then (prerr_endline
"filename missing"; exit
1)
1701 let _ = Glut.init
Sys.argv
in
1702 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1703 let () = Glut.initWindowSize
state.w state.h in
1704 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1707 if Sys.os_type
= "Unix"
1709 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1711 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1712 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1713 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1714 Unix.bind
sock addr;
1716 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1717 Unix.connect
csock addr;
1718 let ssock, _ = Unix.accept
sock in
1721 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1722 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1726 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1730 let () = Glut.displayFunc
display in
1731 let () = Glut.reshapeFunc
reshape in
1732 let () = Glut.keyboardFunc
keyboard in
1733 let () = Glut.specialFunc
special in
1734 let () = Glut.idleFunc
(Some
idle) in
1735 let () = Glut.mouseFunc
mouse in
1736 let () = Glut.motionFunc
motion in
1737 let () = Glut.passiveMotionFunc
pmotion in
1740 state.csock <- csock;
1741 state.ssock <- ssock;
1742 writecmd csock ("open " ^
name ^
"\000");
1746 let rec handlelablglutbug () =
1749 with Glut.BadEnum
"key in special_of_int" ->
1750 showtext '
!'
" LablGlut bug: special key not recognized";
1751 Glut.swapBuffers
();
1752 handlelablglutbug ()
1754 handlelablglutbug ();