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
132 ; mutable invalidated
: int
136 { pat
: string circbuf
137 ; pag
: string circbuf
138 ; nav
: float circbuf
171 ; pagemap
= Hashtbl.create
10
172 ; pagecache
= cbnew 10 ""
183 ; outlines
= Olist
[]
189 { nav
= cbnew 100 0.0
199 Printf.kprintf prerr_endline fmt
201 Printf.kprintf ignore fmt
205 let len = String.length s
in
207 let b = Buffer.create
n in
208 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
209 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
210 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
211 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
212 Buffer.add_string
b s
;
213 let s'
= Buffer.contents
b in
214 let n'
= Unix.write fd
s'
0 n in
215 if n'
!= n then failwith
"write failed";
220 let n = Unix.read fd
s 0 4 in
221 if n != 4 then failwith
"incomplete read(len)";
223 lor (Char.code
s.[0] lsl 24)
224 lor (Char.code
s.[1] lsl 16)
225 lor (Char.code
s.[2] lsl 8)
226 lor (Char.code
s.[3] lsl 0)
228 let s = String.create
len in
229 let n = Unix.read fd
s 0 len in
230 if n != len then failwith
"incomplete read(data)";
235 if y
= state.maxy
then 1.0
236 else float y
/. float state.maxy
240 let b = Buffer.create
10 in
241 Buffer.add_string
b s;
242 let rec combine = function
245 Buffer.add_char
b ' '
;
248 | `
b b -> if b then "1" else "0"
250 | `i i
-> string_of_int i
251 | `f f
-> string_of_float f
252 | `I f
-> string_of_int
(truncate f
)
254 Buffer.add_string
b s;
261 let cmd = Buffer.contents
(makecmd s l
) in
262 writecmd state.csock
cmd;
266 let rec f pn ph fh l
=
268 | (n, _
, h
) :: rest
->
269 let fh = fh + (n - pn
) * ph
in
273 let fh = fh + (ph
* (state.pagecount
- pn
)) in
276 let fh = f 0 0 0 state.pages
in
280 let getpagey pageno
=
281 let rec f pn ph y l
=
283 | (n, _
, h
) :: rest
->
286 y
+ (pageno
- pn
) * ph
288 let y = y + (n - pn
) * ph
in
292 y + (pageno
- pn
) * ph
298 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
299 if pageno
= state.pagecount
|| cacheleft
= 0
302 let ((_
, w
, h
) as curr
), rest
, pdimno
=
304 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
305 curr
, rest
, pdimno
+ 1
309 let pageno'
= pageno + 1 in
343 let accu = e :: accu in
344 f pageno' pdimno curr
345 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
346 (pred cacheleft
) accu
348 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
350 if state.invalidated
= 0
352 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
353 state.maxy
<- calcheight ();
360 let y = state.y + incr
in
362 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
366 let getopaque pageno =
367 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
369 with Not_found
-> None
372 let cache pageno opaque
=
373 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
377 let validopaque opaque
= String.length opaque
> 0;;
380 match getopaque l
.pageno with
381 | None
when state.inflight
< 2+0*(cblen state.pagecache
) ->
382 state.inflight
<- succ
state.inflight
;
384 wcmd "render" [`i
(l
.pageno + 1)
394 let y = min
state.maxy
y in
395 let pages = layout y state.h
in
396 let rec f all
= function
398 begin match getopaque l
.pageno with
399 | None
-> preload l
; f false ls
400 | Some opaque
-> f (all
&& validopaque opaque
) ls
404 if not
conf.showall
|| f true pages
407 state.layout <- pages;
412 Glut.postRedisplay
()
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 not
conf.redispimm
&& 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.preload then begin
630 let y = if state.y < state.h then 0 else state.y - state.h in
631 let pages = layout y (h*3) in
632 List.iter
preload pages;
634 if conf.autoscroll
then begin
635 let y = state.y + conf.scrollincr
in
636 let y = if y >= state.maxy then 0 else y in
639 state.prevy
<- state.y;
640 Glut.postRedisplay
();
644 let cmd = readcmd state.csock
in
649 let onhist cb
= function
650 | HCprev
-> cbget cb ~
-1
651 | HCnext
-> cbget cb
1
652 | HCfirst
-> cbget cb ~
-(cb
.rc)
653 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
656 let search pattern forward
=
657 if String.length pattern
> 0
660 match state.layout with
663 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh
)
666 let b = makecmd "search"
667 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
669 Buffer.add_char
b '
,'
;
670 Buffer.add_string
b pattern
;
671 Buffer.add_char
b '
\000'
;
674 writecmd state.csock
cmd;
677 let intentry text key
=
678 let c = Char.unsafe_chr key
in
681 let s = "x" in s.[0] <- c;
682 let text = text ^
s in
686 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
691 let b = Buffer.create
(String.length
s + 1) in
692 Buffer.add_string
b s;
697 let textentry text key
=
698 let c = Char.unsafe_chr key
in
700 | _
when key
>= 32 && key
< 127 ->
701 let text = addchar text c in
705 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
710 state.rotate <- angle
;
712 wcmd "rotate" [`i angle
];
715 let optentry text key
=
716 let btos b = if b then "on" else "off" in
717 let c = Char.unsafe_chr key
in
720 conf.rectsel
<- not
conf.rectsel
;
721 TEdone
("rectsel " ^
(btos conf.rectsel
))
725 try conf.scrollincr
<- int_of_string
s with exc
->
726 state.text <- Printf.sprintf
"bad integer `%s': %s"
727 s (Printexc.to_string exc
)
729 TEswitch
('#'
, "", None
, intentry, ondone)
734 Some
(int_of_string
s)
736 state.text <- Printf.sprintf
"bad integer `%s': %s"
737 s (Printexc.to_string exc
);
740 | Some angle
-> rotate angle
743 TEswitch
('^'
, "", None
, intentry, ondone)
746 conf.icase
<- not
conf.icase
;
747 TEdone
("case insensitive search " ^
(btos conf.icase
))
750 conf.preload <- not
conf.preload;
751 TEdone
("preload " ^
(btos conf.preload))
754 conf.redispimm
<- not
conf.redispimm
;
755 TEdone
("immediate redisplay " ^
(btos conf.redispimm
))
758 conf.verbose
<- not
conf.verbose
;
759 TEdone
("verbose " ^
(btos conf.verbose
))
762 conf.maxhfit
<- not
conf.maxhfit
;
763 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
764 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
767 conf.crophack
<- not
conf.crophack
;
768 TEdone
("crophack " ^
btos conf.crophack
)
771 conf.showall
<- not
conf.showall
;
772 TEdone
("showall " ^
btos conf.showall
)
775 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
779 let maxoutlinerows () = (state.h - 31) / 16;;
781 let enterselector allowdel
outlines errmsg
=
782 if Array.length
outlines = 0
789 match state.layout with
791 | {pageno=pageno} :: rest
-> pageno
795 if n = Array.length
outlines
798 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
799 if outlinepageno
>= pageno then n else loop (n+1)
804 Some
(allowdel
, active,
805 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
806 Glut.postRedisplay
();
809 let enteroutlinemode () =
811 match state.outlines with
814 let a = Array.of_list
(List.rev
l) in
815 state.outlines <- Oarray
a;
817 | Onarrow
(a, b) -> a
819 enterselector false outlines "Documents has no outline";
822 let enterbookmarkmode () =
823 let bookmarks = Array.of_list
state.bookmarks in
824 enterselector true bookmarks "Documents has no bookmarks (yet)";
828 let quickbookmark ?title
() =
829 match state.layout with
835 let sec = Unix.gettimeofday
() in
836 let tm = Unix.localtime
sec in
837 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
841 (tm.Unix.tm_year
+ 1900)
844 | Some
title -> title
847 (title, 0, l.pageno, l.pagey) :: state.bookmarks
850 let viewkeyboard ~key ~x ~
y =
852 state.textentry <- te
;
855 Glut.postRedisplay
()
857 match state.textentry with
859 let c = Char.chr key
in
874 Glut.postRedisplay
()
877 let ondone isforw
s =
878 cbput state.hists
.pat
s;
879 cbrfollowlen state.hists
.pat
;
880 state.searchpattern
<- s;
883 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
884 textentry, ondone (c ='
/'
)))
889 try int_of_string
s with exc
->
890 state.text <- Printf.sprintf
"bad integer `%s': %s"
891 s (Printexc.to_string exc
);
897 state.text <- "page bias is now " ^ string_of_int
n;
900 enttext (Some
('
+'
, "", None
, intentry, ondone))
906 enttext (Some
('
-'
, "", None
, optentry, ondone))
911 try int_of_string
s with exc
->
912 state.text <- Printf.sprintf
"bad integer `%s': %s"
913 s (Printexc.to_string exc
);
919 cbput state.hists
.pag
(string_of_int
n);
920 cbrfollowlen state.hists
.pag
;
921 gotoy (getpagey (n + conf.pagebias
- 1))
924 let pageentry text key
=
925 match Char.unsafe_chr key
with
927 | _
-> intentry text key
929 let text = "x" in text.[0] <- c;
930 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
934 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
935 reshape state.w
state.h;
938 conf.hlinks
<- not
conf.hlinks
;
939 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
940 Glut.postRedisplay
()
943 conf.autoscroll
<- not
conf.autoscroll
946 begin match state.fullscreen
with
948 state.fullscreen
<- Some
(state.w
, state.h);
951 state.fullscreen
<- None
;
952 Glut.reshapeWindow ~w ~
h
959 search state.searchpattern
true
962 search state.searchpattern
false
965 begin match state.layout with
968 gotoy (state.y - l.pagey);
972 begin match List.rev
state.layout with
975 gotoy (clamp (l.pageh
- l.pagey))
979 begin match state.layout with
982 gotoy (clamp (-l.pageh
));
987 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
989 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
991 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
995 else (100. *. (float state.y /. float maxy)) in
998 Printf.sprintf
"Page %d of %d %.2f%%"
999 (fn+1) state.pagecount
percent
1002 "Pages %d-%d of %d %.2f%%"
1003 (fn+1) (ln
+1) state.pagecount
percent
1009 begin match state.layout with
1012 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
1013 Glut.postRedisplay
();
1017 enterbookmarkmode ()
1021 match state.layout with
1023 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1026 enttext (Some
('~'
, "", None
, textentry, ondone))
1030 showtext ' '
"Quick bookmark added";
1034 begin match state.layout with
1036 let a = getpagewh
l.pagedimno
in
1040 (truncate
(1.8 *. (a.(1) -. a.(0))),
1041 truncate
(1.2 *. (a.(3) -. a.(0))))
1043 (truncate
(a.(1) -. a.(0)),
1044 truncate
(a.(3) -. a.(0)))
1046 Glut.reshapeWindow
(w + conf.scrollw
) h;
1047 Glut.postRedisplay
();
1053 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1056 vlog "huh? %d %c" key
(Char.chr key
);
1059 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1060 let len = String.length
text in
1063 state.textentry <- None
;
1064 Glut.postRedisplay
();
1067 let s = String.sub
text 0 (len - 1) in
1068 enttext (Some
(c, s, onhist, onkey
, ondone))
1071 | Some
(c, text, onhist, onkey
, ondone) ->
1072 begin match Char.unsafe_chr key
with
1075 state.textentry <- None
;
1076 Glut.postRedisplay
()
1079 state.textentry <- None
;
1080 Glut.postRedisplay
()
1083 begin match onkey
text key
with
1085 state.textentry <- None
;
1087 Glut.postRedisplay
()
1090 enttext (Some
(c, text, onhist, onkey
, ondone));
1093 state.textentry <- None
;
1094 Glut.postRedisplay
()
1097 state.textentry <- Some te
;
1098 Glut.postRedisplay
()
1103 let narrow outlines pattern
=
1104 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1108 let rec fold accu n =
1109 if n = -1 then accu else
1110 let (s, _
, _
, _
) as o
= outlines.(n) in
1112 if (try ignore
(Str.search_forward re
s 0); true
1113 with Not_found
-> false)
1119 let matched = fold [] (Array.length
outlines - 1) in
1120 if matched = [] then None
else Some
(Array.of_list
matched)
1123 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1124 let search active pattern incr
=
1127 if n = Array.length
outlines || n = -1 then None
else
1128 let (s, _
, _
, _
) = outlines.(n) in
1130 (try ignore
(Str.search_forward re
s 0); true
1131 with Not_found
-> false)
1133 else loop (n + incr
)
1138 let re = Str.regexp_case_fold pattern
in
1144 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1147 if String.length qsearch
= 0
1150 state.outline <- None
;
1151 Glut.postRedisplay
();
1155 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1156 Glut.postRedisplay
();
1160 let incr = if key
= 18 then -1 else 1 in
1162 match search (active + incr) qsearch
incr with
1164 state.text <- qsearch ^
" [not found]";
1167 state.text <- qsearch
;
1168 active, firstof active
1170 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1171 Glut.postRedisplay
();
1174 let len = String.length qsearch
in
1181 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1184 let qsearch = String.sub
qsearch 0 (len - 1) in
1186 match search active qsearch ~
-1 with
1188 state.text <- qsearch ^
" [not found]";
1191 state.text <- qsearch;
1192 active, firstof active
1194 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1196 Glut.postRedisplay
()
1199 if active < Array.length
outlines
1201 let (_
, _
, n, t
) = outlines.(active) in
1205 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1206 state.outline <- None
;
1207 Glut.postRedisplay
();
1209 | _
when key
>= 32 && key
< 127 ->
1210 let pattern = addchar qsearch (Char.chr key
) in
1212 match search active pattern 1 with
1214 state.text <- pattern ^
" [not found]";
1217 state.text <- pattern;
1218 active, firstof active
1220 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1221 Glut.postRedisplay
()
1223 | 14 when not allowdel
->
1224 let optoutlines = narrow outlines qsearch in
1225 begin match optoutlines with
1226 | None
-> state.text <- "can't narrow"
1228 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1229 match state.outlines with
1231 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1232 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1234 Glut.postRedisplay
()
1236 | 21 when not allowdel
->
1238 match state.outlines with
1241 let a = Array.of_list
(List.rev
l) in
1242 state.outlines <- Oarray
a;
1245 state.outlines <- Oarray
b;
1248 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1249 Glut.postRedisplay
()
1253 Some
(allowdel
, active, firstof active, outlines, qsearch);
1254 Glut.postRedisplay
()
1256 | 127 when allowdel
->
1257 let len = Array.length
outlines - 1 in
1260 state.outline <- None
;
1261 state.bookmarks <- [];
1264 let bookmarks = Array.init
len
1266 let i = if i >= active then i + 1 else i in
1277 Glut.postRedisplay
()
1279 | _
-> log "unknown key %d" key
1282 let keyboard ~key ~x ~
y =
1287 match state.outline with
1288 | None
-> viewkeyboard ~key ~x ~
y
1289 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1292 let special ~key ~x ~
y =
1293 match state.outline with
1295 begin match state.textentry with
1299 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1300 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1301 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1302 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1303 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1304 | Glut.KEY_HOME
-> addnav (); 0
1307 state.maxy - (if conf.maxhfit
then state.h else 0)
1313 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1316 | Glut.KEY_UP
-> onhist HCprev
1317 | Glut.KEY_DOWN
-> onhist HCnext
1318 | Glut.KEY_HOME
-> onhist HCfirst
1319 | Glut.KEY_END
-> onhist HClast
1322 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1323 Glut.postRedisplay
()
1328 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1329 let maxrows = maxoutlinerows () in
1331 let active = active + incr in
1332 let active = max
0 (min
active (Array.length
outlines - 1)) in
1336 let rows = active - first in
1337 if rows > maxrows then active - maxrows else first
1340 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1341 Glut.postRedisplay
()
1344 | Glut.KEY_UP
-> navigate ~
-1
1345 | Glut.KEY_DOWN
-> navigate 1
1346 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1347 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1350 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1351 Glut.postRedisplay
()
1354 let active = Array.length
outlines - 1 in
1355 let first = max
0 (active - maxrows) in
1356 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1357 Glut.postRedisplay
()
1362 let drawplaceholder l =
1363 GlDraw.color
(1.0, 1.0, 1.0);
1365 (0.0, float l.pagedispy
)
1366 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1369 and y = float (l.pagedispy
+ 13) in
1370 let font = Glut.BITMAP_8_BY_13
in
1371 GlDraw.color
(0.0, 0.0, 0.0);
1372 GlPix.raster_pos ~
x ~
y ();
1373 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1374 ("Loading " ^ string_of_int
l.pageno);
1377 let now () = Unix.gettimeofday
();;
1380 begin match getopaque l.pageno with
1381 | Some opaque
when validopaque opaque
->
1382 if state.textentry = None
1383 then GlDraw.color
(1.0, 1.0, 1.0)
1384 else GlDraw.color
(0.4, 0.4, 0.4);
1386 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1389 if conf.hlinks
then highlightlinks opaque
(l.pagedispy
- l.pagey);
1390 vlog "draw %f sec" d;
1397 if state.inflight
< cblen state.pagecache
1399 List.iter
preload state.layout;
1402 vlog "inflight %d" state.inflight
;
1405 GlDraw.color
(0.5, 0.5, 0.5);
1408 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1410 l.pagedispy
+ l.pagevh
;
1413 let scrollindicator () =
1414 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1415 GlDraw.color
(0.64 , 0.64, 0.64);
1417 (float (state.w - conf.scrollw
), 0.)
1418 (float state.w, float state.h)
1420 GlDraw.color
(0.0, 0.0, 0.0);
1421 let sh = (float (maxy + state.h) /. float state.h) in
1422 let sh = float state.h /. sh in
1423 let sh = max
sh (float conf.scrollh
) in
1426 if state.y = state.maxy
1428 else float state.y /. float maxy
1430 let position = (float state.h -. sh) *. percent in
1433 if position +. sh > float state.h
1440 (float (state.w - conf.scrollw
), position)
1441 (float state.w, position +. sh)
1446 match state.mstate
with
1450 | Msel
((x0
, y0
), (x1
, y1
)) ->
1452 and y1
= max
y0 y1
in
1455 if (y0 >= l.pagedispy
&& y0 <= (l.pagedispy
+ l.pagevh
))
1456 || ((y1
>= l.pagedispy
)) (* && y1 <= (dy + vh))) *)
1458 match getopaque l.pageno with
1459 | Some opaque
when validopaque opaque
->
1460 let oy = -l.pagey + l.pagedispy
in
1461 gettext opaque
(min x0 x1
, y0, max x1 x0
, y1
) oy conf.rectsel
1464 List.iter
f state.layout
1469 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1470 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1472 (fun (pageno, c, (x0
, y0, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1474 if l.pageno = pageno
1476 let d = float (l.pagedispy
- l.pagey) in
1477 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1478 GlDraw.begins `quads
;
1480 GlDraw.vertex2
(x0
, y0+.d);
1481 GlDraw.vertex2
(x1
, y1
+.d);
1482 GlDraw.vertex2
(x2
, y2
+.d);
1483 GlDraw.vertex2
(x3
, y3
+.d);
1486 (* GlDraw.rect (x0, y0 +. d) (x1, y1 +. d) *)
1494 let showoutline = function
1496 | Some
(allowdel
, active, first, outlines, qsearch) ->
1498 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1499 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1500 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1503 GlDraw.color
(1., 1., 1.);
1504 let font = Glut.BITMAP_9_BY_15
in
1505 let draw_string x y s =
1506 GlPix.raster_pos ~
x ~
y ();
1507 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1510 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1513 let (s, l, _
, _
) = outlines.(row
) in
1514 let y = (row
- first) * 16 in
1519 GlDraw.polygon_mode `both `line
;
1520 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1521 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1522 GlDraw.rect
(0., float (y + 1))
1523 (float (state.w - conf.scrollw
- 1), float (y + 18));
1524 GlDraw.polygon_mode `both `fill
;
1526 GlDraw.color
(1., 1., 1.);
1528 draw_string (float x) (float (y + 16)) s;
1536 let lasty = List.fold_left
drawpage 0 (state.layout) in
1537 GlDraw.color
(0.5, 0.5, 0.5);
1540 (float (state.w - conf.scrollw
), float state.h)
1545 showoutline state.outline;
1547 Glut.swapBuffers
();
1551 let rec f = function
1553 begin match getopaque l.pageno with
1554 | Some opaque
when validopaque opaque
->
1555 let y = y - l.pagedispy
in
1558 let y = l.pagey + y in
1559 match getlink opaque
x y with
1573 let rec f = function
1575 begin match getopaque l.pageno with
1576 | Some opaque
when validopaque opaque
->
1577 let y = y - l.pagedispy
in
1580 let y = l.pagey + y in
1581 if checklink opaque
x y then true else f rest
1592 let mouse ~button ~bstate ~
x ~
y =
1594 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1602 let incr = incr * 2 in
1603 let y = clamp incr in
1606 | Glut.LEFT_BUTTON
when state.outline = None
->
1607 let dest = if bstate
= Glut.DOWN
then getlink x y else None
in
1608 begin match dest with
1609 | Some
(pageno, top
) ->
1615 if bstate
= Glut.DOWN
1617 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1618 state.mstate
<- Msel
((x, y), (x, y));
1619 Glut.postRedisplay
()
1622 Glut.setCursor
Glut.CURSOR_INHERIT
;
1623 state.mstate
<- Mnone
;
1630 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1633 if state.outline = None
1635 match state.mstate
with
1638 state.mstate
<- Msel
(a, (x, y));
1639 Glut.postRedisplay
()
1643 if state.outline = None
1645 match state.mstate
with
1646 | Mnone
when (checklink x y) ->
1647 Glut.setCursor
Glut.CURSOR_INFO
1650 Glut.setCursor
Glut.CURSOR_INHERIT
1659 if Sys.os_type
= "Win32"
1661 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1663 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1665 Filename.concat
home "llpp"
1669 let ic = open_in_bin
statepath in
1670 let hash = input_value
ic in
1676 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1683 match state.fullscreen
with
1684 | None
-> state.w, state.h
1687 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1688 let oc = open_out_bin
statepath in
1689 output_value
oc pstate
1693 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1698 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1701 state.bookmarks <- statebookmarks;
1702 with Not_found
-> ()
1704 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1707 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1709 if String.length
state.path
= 0
1710 then (prerr_endline
"filename missing"; exit
1)
1715 let _ = Glut.init
Sys.argv
in
1716 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1717 let () = Glut.initWindowSize
state.w state.h in
1718 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1721 if Sys.os_type
= "Unix"
1723 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1725 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1726 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1727 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1728 Unix.bind
sock addr;
1730 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1731 Unix.connect
csock addr;
1732 let ssock, _ = Unix.accept
sock in
1735 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1736 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1740 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1744 let () = Glut.displayFunc
display in
1745 let () = Glut.reshapeFunc
reshape in
1746 let () = Glut.keyboardFunc
keyboard in
1747 let () = Glut.specialFunc
special in
1748 let () = Glut.idleFunc
(Some
idle) in
1749 let () = Glut.mouseFunc
mouse in
1750 let () = Glut.motionFunc
motion in
1751 let () = Glut.passiveMotionFunc
pmotion in
1754 state.csock <- csock;
1755 state.ssock <- ssock;
1756 writecmd csock ("open " ^
name ^
"\000");
1760 let rec handlelablglutbug () =
1763 with Glut.BadEnum
"key in special_of_int" ->
1764 showtext '
!'
" LablGlut bug: special key not recognized";
1765 Glut.swapBuffers
();
1766 handlelablglutbug ()
1768 handlelablglutbug ();