1 type link
= | LNone
| LUri
of string | LGoto
of (int * int);;
3 let log fmt
= Printf.kprintf prerr_endline fmt
;;
4 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
6 external init
: Unix.file_descr
-> unit = "ml_init";;
7 external draw
: int -> int -> int -> int -> string -> unit = "ml_draw";;
8 external seltext
: string -> (int * int * int * int) -> int -> unit =
10 external copysel
: string -> unit = "ml_copysel";;
11 external getlink
: string -> int -> int -> link
= "ml_getlink";;
12 external highlightlinks
: string -> int -> unit = "ml_highlightlinks";;
13 external getpagewh
: int -> float array
= "ml_getpagewh";;
15 type mstate
= Msel
of ((int * int) * (int * int)) | Mnone
;;
25 type textentry
= (char
* string * onhist
option * onkey
* ondone
)
26 and onkey
= string -> int -> te
27 and ondone
= string -> unit
28 and onhist
= histcmd
-> string
29 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
34 | TEswitch
of textentry
38 { store
= Array.create n v
45 let cblen b
= Array.length b
.store
;;
50 b
.wc
<- (b
.wc
+ 1) mod len;
51 b
.len <- min
(b
.len + 1) len;
54 let cbpeekw b
= b
.store
.(b
.wc
);;
57 if b
.len = 0 then b
.store
.(0) else
58 let rc = b
.rc + dir
in
59 let rc = if rc = -1 then b
.len - 1 else rc in
60 let rc = if rc = b
.len then 0 else rc in
81 { mutable scrollw
: int
82 ; mutable scrollh
: int
83 ; mutable icase
: bool
84 ; mutable preload
: bool
85 ; mutable pagebias
: int
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
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
715 try conf.scrollincr
<- int_of_string
s with exc
->
716 state.text <- Printf.sprintf
"bad integer `%s': %s"
717 s (Printexc.to_string exc
)
719 TEswitch
('#'
, "", None
, intentry, ondone)
724 Some
(int_of_string
s)
726 state.text <- Printf.sprintf
"bad integer `%s': %s"
727 s (Printexc.to_string exc
);
730 | Some angle
-> rotate angle
733 TEswitch
('^'
, "", None
, intentry, ondone)
736 conf.icase
<- not
conf.icase
;
737 TEdone
("case insensitive search " ^
(btos conf.icase
))
740 conf.preload <- not
conf.preload;
742 TEdone
("preload " ^
(btos conf.preload))
745 conf.verbose
<- not
conf.verbose
;
746 TEdone
("verbose " ^
(btos conf.verbose
))
749 conf.maxhfit
<- not
conf.maxhfit
;
750 state.maxy <- state.maxy + (if conf.maxhfit
then -state.h else state.h);
751 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
754 conf.crophack
<- not
conf.crophack
;
755 TEdone
("crophack " ^
btos conf.crophack
)
758 conf.showall
<- not
conf.showall
;
759 TEdone
("showall " ^
btos conf.showall
)
762 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
766 let maxoutlinerows () = (state.h - 31) / 16;;
768 let enterselector allowdel
outlines errmsg
=
769 if Array.length
outlines = 0
776 match state.layout with
778 | {pageno=pageno} :: rest
-> pageno
782 if n = Array.length
outlines
785 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
786 if outlinepageno
>= pageno then n else loop (n+1)
791 Some
(allowdel
, active,
792 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
793 Glut.postRedisplay
();
796 let enteroutlinemode () =
798 match state.outlines with
801 let a = Array.of_list
(List.rev
l) in
802 state.outlines <- Oarray
a;
804 | Onarrow
(a, b) -> a
806 enterselector false outlines "Document has no outline";
809 let enterbookmarkmode () =
810 let bookmarks = Array.of_list
state.bookmarks in
811 enterselector true bookmarks "Document has no bookmarks (yet)";
815 let quickbookmark ?title
() =
816 match state.layout with
822 let sec = Unix.gettimeofday
() in
823 let tm = Unix.localtime
sec in
824 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
828 (tm.Unix.tm_year
+ 1900)
831 | Some
title -> title
834 (title, 0, l.pageno, l.pagey) :: state.bookmarks
837 let viewkeyboard ~key ~x ~
y =
839 state.textentry <- te
;
842 Glut.postRedisplay
()
844 match state.textentry with
846 let c = Char.chr key
in
861 Glut.postRedisplay
()
864 let ondone isforw
s =
865 cbput state.hists
.pat
s;
866 cbrfollowlen state.hists
.pat
;
867 state.searchpattern
<- s;
870 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
871 textentry, ondone (c ='
/'
)))
876 try int_of_string
s with exc
->
877 state.text <- Printf.sprintf
"bad integer `%s': %s"
878 s (Printexc.to_string exc
);
884 state.text <- "page bias is now " ^ string_of_int
n;
887 enttext (Some
('
+'
, "", None
, intentry, ondone))
893 enttext (Some
('
-'
, "", None
, optentry, ondone))
898 try int_of_string
s with exc
->
899 state.text <- Printf.sprintf
"bad integer `%s': %s"
900 s (Printexc.to_string exc
);
906 cbput state.hists
.pag
(string_of_int
n);
907 cbrfollowlen state.hists
.pag
;
908 gotoy (getpagey (n + conf.pagebias
- 1))
911 let pageentry text key
=
912 match Char.unsafe_chr key
with
914 | _
-> intentry text key
916 let text = "x" in text.[0] <- c;
917 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
921 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
922 reshape state.w
state.h;
925 conf.hlinks
<- not
conf.hlinks
;
926 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
927 Glut.postRedisplay
()
930 conf.autoscroll
<- not
conf.autoscroll
933 begin match state.fullscreen
with
935 state.fullscreen
<- Some
(state.w
, state.h);
938 state.fullscreen
<- None
;
939 Glut.reshapeWindow ~w ~
h
946 search state.searchpattern
true
949 search state.searchpattern
false
952 begin match state.layout with
955 gotoy (state.y - l.pagey);
959 begin match List.rev
state.layout with
962 gotoy (clamp (l.pageh
- l.pagey))
966 begin match state.layout with
969 gotoy (clamp (-l.pageh
));
974 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
976 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
978 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
982 else (100. *. (float state.y /. float maxy)) in
985 Printf.sprintf
"Page %d of %d %.2f%%"
986 (fn+1) state.pagecount
percent
989 "Pages %d-%d of %d %.2f%%"
990 (fn+1) (ln
+1) state.pagecount
percent
996 begin match state.layout with
999 Glut.reshapeWindow
(l.pagew
+ conf.scrollw
) l.pageh
;
1000 Glut.postRedisplay
();
1004 enterbookmarkmode ()
1008 match state.layout with
1010 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1013 enttext (Some
('~'
, "", None
, textentry, ondone))
1017 showtext ' '
"Quick bookmark added";
1021 begin match state.layout with
1023 let a = getpagewh
l.pagedimno
in
1027 (truncate
(1.8 *. (a.(1) -. a.(0))),
1028 truncate
(1.2 *. (a.(3) -. a.(0))))
1030 (truncate
(a.(1) -. a.(0)),
1031 truncate
(a.(3) -. a.(0)))
1033 Glut.reshapeWindow
(w + conf.scrollw
) h;
1034 Glut.postRedisplay
();
1040 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1043 vlog "huh? %d %c" key
(Char.chr key
);
1046 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1047 let len = String.length
text in
1050 state.textentry <- None
;
1051 Glut.postRedisplay
();
1054 let s = String.sub
text 0 (len - 1) in
1055 enttext (Some
(c, s, onhist, onkey
, ondone))
1058 | Some
(c, text, onhist, onkey
, ondone) ->
1059 begin match Char.unsafe_chr key
with
1062 state.textentry <- None
;
1063 Glut.postRedisplay
()
1066 state.textentry <- None
;
1067 Glut.postRedisplay
()
1070 begin match onkey
text key
with
1072 state.textentry <- None
;
1074 Glut.postRedisplay
()
1077 enttext (Some
(c, text, onhist, onkey
, ondone));
1080 state.textentry <- None
;
1081 Glut.postRedisplay
()
1084 state.textentry <- Some te
;
1085 Glut.postRedisplay
()
1090 let narrow outlines pattern
=
1091 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1095 let rec fold accu n =
1096 if n = -1 then accu else
1097 let (s, _
, _
, _
) as o
= outlines.(n) in
1099 if (try ignore
(Str.search_forward re
s 0); true
1100 with Not_found
-> false)
1106 let matched = fold [] (Array.length
outlines - 1) in
1107 if matched = [] then None
else Some
(Array.of_list
matched)
1110 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1111 let search active pattern incr
=
1114 if n = Array.length
outlines || n = -1 then None
else
1115 let (s, _
, _
, _
) = outlines.(n) in
1117 (try ignore
(Str.search_forward re
s 0); true
1118 with Not_found
-> false)
1120 else loop (n + incr
)
1125 let re = Str.regexp_case_fold pattern
in
1131 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1134 if String.length qsearch
= 0
1137 state.outline <- None
;
1138 Glut.postRedisplay
();
1142 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1143 Glut.postRedisplay
();
1147 let incr = if key
= 18 then -1 else 1 in
1149 match search (active + incr) qsearch
incr with
1151 state.text <- qsearch ^
" [not found]";
1154 state.text <- qsearch
;
1155 active, firstof active
1157 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1158 Glut.postRedisplay
();
1161 let len = String.length qsearch
in
1168 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1171 let qsearch = String.sub
qsearch 0 (len - 1) in
1173 match search active qsearch ~
-1 with
1175 state.text <- qsearch ^
" [not found]";
1178 state.text <- qsearch;
1179 active, firstof active
1181 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1183 Glut.postRedisplay
()
1186 if active < Array.length
outlines
1188 let (_
, _
, n, t
) = outlines.(active) in
1192 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1193 state.outline <- None
;
1194 Glut.postRedisplay
();
1196 | _
when key
>= 32 && key
< 127 ->
1197 let pattern = addchar qsearch (Char.chr key
) in
1199 match search active pattern 1 with
1201 state.text <- pattern ^
" [not found]";
1204 state.text <- pattern;
1205 active, firstof active
1207 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1208 Glut.postRedisplay
()
1210 | 14 when not allowdel
->
1211 let optoutlines = narrow outlines qsearch in
1212 begin match optoutlines with
1213 | None
-> state.text <- "can't narrow"
1215 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1216 match state.outlines with
1218 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1219 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1221 Glut.postRedisplay
()
1223 | 21 when not allowdel
->
1225 match state.outlines with
1228 let a = Array.of_list
(List.rev
l) in
1229 state.outlines <- Oarray
a;
1232 state.outlines <- Oarray
b;
1235 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1236 Glut.postRedisplay
()
1240 Some
(allowdel
, active, firstof active, outlines, qsearch);
1241 Glut.postRedisplay
()
1243 | 127 when allowdel
->
1244 let len = Array.length
outlines - 1 in
1247 state.outline <- None
;
1248 state.bookmarks <- [];
1251 let bookmarks = Array.init
len
1253 let i = if i >= active then i + 1 else i in
1264 Glut.postRedisplay
()
1266 | _
-> log "unknown key %d" key
1269 let keyboard ~key ~x ~
y =
1274 match state.outline with
1275 | None
-> viewkeyboard ~key ~x ~
y
1276 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1279 let special ~key ~x ~
y =
1280 match state.outline with
1282 begin match state.textentry with
1286 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1287 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1288 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1289 | Glut.KEY_PAGE_UP
-> clamp (-state.h)
1290 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1291 | Glut.KEY_HOME
-> addnav (); 0
1294 state.maxy - (if conf.maxhfit
then state.h else 0)
1300 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1303 | Glut.KEY_UP
-> onhist HCprev
1304 | Glut.KEY_DOWN
-> onhist HCnext
1305 | Glut.KEY_HOME
-> onhist HCfirst
1306 | Glut.KEY_END
-> onhist HClast
1309 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1310 Glut.postRedisplay
()
1315 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1316 let maxrows = maxoutlinerows () in
1318 let active = active + incr in
1319 let active = max
0 (min
active (Array.length
outlines - 1)) in
1323 let rows = active - first in
1324 if rows > maxrows then active - maxrows else first
1327 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1328 Glut.postRedisplay
()
1331 | Glut.KEY_UP
-> navigate ~
-1
1332 | Glut.KEY_DOWN
-> navigate 1
1333 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1334 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1337 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1338 Glut.postRedisplay
()
1341 let active = Array.length
outlines - 1 in
1342 let first = max
0 (active - maxrows) in
1343 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1344 Glut.postRedisplay
()
1349 let drawplaceholder l =
1350 GlDraw.color
(1.0, 1.0, 1.0);
1352 (0.0, float l.pagedispy
)
1353 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1356 and y = float (l.pagedispy
+ 13) in
1357 let font = Glut.BITMAP_8_BY_13
in
1358 GlDraw.color
(0.0, 0.0, 0.0);
1359 GlPix.raster_pos ~
x ~
y ();
1360 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1361 ("Loading " ^ string_of_int
l.pageno);
1364 let now () = Unix.gettimeofday
();;
1367 begin match getopaque l.pageno with
1368 | Some opaque
when validopaque opaque
->
1369 if state.textentry = None
1370 then GlDraw.color
(1.0, 1.0, 1.0)
1371 else GlDraw.color
(0.4, 0.4, 0.4);
1373 draw
l.pagedispy
l.pagew
l.pagevh
l.pagey opaque
;
1376 if conf.hlinks
then highlightlinks opaque
(l.pagedispy
- l.pagey);
1377 vlog "draw %f sec" d;
1384 if state.inflight
< cblen state.pagecache
1386 List.iter
preload state.layout;
1389 vlog "inflight %d" state.inflight
;
1392 GlDraw.color
(0.5, 0.5, 0.5);
1395 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1397 l.pagedispy
+ l.pagevh
;
1400 let scrollindicator () =
1401 let maxy = state.maxy - (if conf.maxhfit
then state.h else 0) in
1402 GlDraw.color
(0.64 , 0.64, 0.64);
1404 (float (state.w - conf.scrollw
), 0.)
1405 (float state.w, float state.h)
1407 GlDraw.color
(0.0, 0.0, 0.0);
1408 let sh = (float (maxy + state.h) /. float state.h) in
1409 let sh = float state.h /. sh in
1410 let sh = max
sh (float conf.scrollh
) in
1413 if state.y = state.maxy
1415 else float state.y /. float maxy
1417 let position = (float state.h -. sh) *. percent in
1420 if position +. sh > float state.h
1427 (float (state.w - conf.scrollw
), position)
1428 (float state.w, position +. sh)
1433 match state.mstate
with
1437 | Msel
((x0
, y0
), (x1
, y1
)) ->
1439 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1440 || ((y1
>= l.pagedispy
))
1442 match getopaque l.pageno with
1443 | Some opaque
when validopaque opaque
->
1444 let oy = -l.pagey + l.pagedispy
in
1445 seltext opaque
(x0
, y0
, x1
, y1
) oy
1448 List.iter
f state.layout
1453 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1454 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1456 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1458 if l.pageno = pageno
1460 let d = float (l.pagedispy
- l.pagey) in
1461 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1462 GlDraw.begins `quads
;
1464 GlDraw.vertex2
(x0
, y0
+.d);
1465 GlDraw.vertex2
(x1
, y1
+.d);
1466 GlDraw.vertex2
(x2
, y2
+.d);
1467 GlDraw.vertex2
(x3
, y3
+.d);
1477 let showoutline = function
1479 | Some
(allowdel
, active, first, outlines, qsearch) ->
1481 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1482 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1483 GlDraw.rect
(0., 0.) (float state.w, float state.h);
1486 GlDraw.color
(1., 1., 1.);
1487 let font = Glut.BITMAP_9_BY_15
in
1488 let draw_string x y s =
1489 GlPix.raster_pos ~
x ~
y ();
1490 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1493 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1496 let (s, l, _
, _
) = outlines.(row
) in
1497 let y = (row
- first) * 16 in
1502 GlDraw.polygon_mode `both `line
;
1503 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1504 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1505 GlDraw.rect
(0., float (y + 1))
1506 (float (state.w - conf.scrollw
- 1), float (y + 18));
1507 GlDraw.polygon_mode `both `fill
;
1509 GlDraw.color
(1., 1., 1.);
1511 draw_string (float x) (float (y + 16)) s;
1519 let lasty = List.fold_left
drawpage 0 (state.layout) in
1520 GlDraw.color
(0.5, 0.5, 0.5);
1523 (float (state.w - conf.scrollw
), float state.h)
1528 showoutline state.outline;
1530 Glut.swapBuffers
();
1534 let rec f = function
1536 begin match getopaque l.pageno with
1537 | Some opaque
when validopaque opaque
->
1538 let y = y - l.pagedispy
in
1541 let y = l.pagey + y in
1542 match getlink opaque
x y with
1555 let mouse ~button ~bstate ~
x ~
y =
1557 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1565 let incr = incr * 2 in
1566 let y = clamp incr in
1569 | Glut.LEFT_BUTTON
when state.outline = None
->
1570 let dest = if bstate
= Glut.DOWN
then getlink x y else LNone
in
1571 begin match dest with
1572 | LGoto
(pageno, top
) ->
1581 if bstate
= Glut.DOWN
1583 if state.rotate mod 360 = 0 then (
1584 Glut.setCursor
Glut.CURSOR_TEXT
;
1585 state.mstate
<- Msel
((x, y), (x, y));
1586 Glut.postRedisplay
()
1590 match state.mstate
with
1592 | Msel
((x0
, y0
), (x1
, y1
)) ->
1594 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1595 || ((y1
>= l.pagedispy
))
1597 match getopaque l.pageno with
1598 | Some opaque
when validopaque opaque
->
1602 List.iter
f state.layout;
1603 copysel
""; (* ugly *)
1604 Glut.setCursor
Glut.CURSOR_INHERIT
;
1605 state.mstate
<- Mnone
;
1612 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1615 if state.outline = None
1617 match state.mstate
with
1620 state.mstate
<- Msel
(a, (x, y));
1621 Glut.postRedisplay
()
1625 if state.outline = None
1627 match state.mstate
with
1629 if getlink x y != LNone
1630 then Glut.setCursor
Glut.CURSOR_INFO
1631 else Glut.setCursor
Glut.CURSOR_INHERIT
1640 if Sys.os_type
= "Win32"
1642 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1644 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1646 Filename.concat
home "llpp"
1650 let ic = open_in_bin
statepath in
1651 let hash = input_value
ic in
1657 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1664 match state.fullscreen
with
1665 | None
-> state.w, state.h
1668 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h);
1669 let oc = open_out_bin
statepath in
1670 output_value
oc pstate
1674 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1679 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1682 state.bookmarks <- statebookmarks;
1683 with Not_found
-> ()
1685 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1688 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1690 if String.length
state.path
= 0
1691 then (prerr_endline
"filename missing"; exit
1)
1696 let _ = Glut.init
Sys.argv
in
1697 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1698 let () = Glut.initWindowSize
state.w state.h in
1699 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1702 if Sys.os_type
= "Unix"
1704 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1706 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1707 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1708 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1709 Unix.bind
sock addr;
1711 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1712 Unix.connect
csock addr;
1713 let ssock, _ = Unix.accept
sock in
1716 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1717 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1721 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1725 let () = Glut.displayFunc
display in
1726 let () = Glut.reshapeFunc
reshape in
1727 let () = Glut.keyboardFunc
keyboard in
1728 let () = Glut.specialFunc
special in
1729 let () = Glut.idleFunc
(Some
idle) in
1730 let () = Glut.mouseFunc
mouse in
1731 let () = Glut.motionFunc
motion in
1732 let () = Glut.passiveMotionFunc
pmotion in
1735 state.csock <- csock;
1736 state.ssock <- ssock;
1737 writecmd csock ("open " ^
name ^
"\000");
1741 let rec handlelablglutbug () =
1744 with Glut.BadEnum
"key in special_of_int" ->
1745 showtext '
!'
" LablGlut bug: special key not recognized";
1746 Glut.swapBuffers
();
1747 handlelablglutbug ()
1749 handlelablglutbug ();