4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let log fmt
= Printf.kprintf prerr_endline fmt
;;
9 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
11 external init
: Unix.file_descr
-> unit = "ml_init";;
12 external draw
: int -> int -> int -> int -> string -> unit = "ml_draw";;
13 external seltext
: string -> (int * int * int * int) -> int -> unit =
15 external copysel
: string -> unit = "ml_copysel";;
16 external highlightlinks
: string -> int -> unit = "ml_highlightlinks";;
17 external getpagewh
: int -> float array
= "ml_getpagewh";;
18 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
20 type mstate
= Msel
of ((int * int) * (int * int)) | Mnone
;;
30 type textentry
= (char
* string * onhist
option * onkey
* ondone
)
31 and onkey
= string -> int -> te
32 and ondone
= string -> unit
33 and onhist
= histcmd
-> string
34 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
39 | TEswitch
of textentry
43 { store
= Array.create n v
50 let cblen b
= Array.length b
.store
;;
55 b
.wc
<- (b
.wc
+ 1) mod len;
56 b
.len <- min
(b
.len + 1) len;
59 let cbpeekw b
= b
.store
.(b
.wc
);;
62 if b
.len = 0 then b
.store
.(0) else
63 let rc = b
.rc + dir
in
64 let rc = if rc = -1 then b
.len - 1 else rc in
65 let rc = if rc = b
.len then 0 else rc in
86 { mutable scrollw
: int
87 ; mutable scrollh
: int
88 ; mutable icase
: bool
89 ; mutable preload
: bool
90 ; mutable pagebias
: int
91 ; mutable verbose
: bool
92 ; mutable scrollincr
: int
93 ; mutable maxhfit
: bool
94 ; mutable crophack
: bool
95 ; mutable autoscroll
: bool
96 ; mutable showall
: bool
97 ; mutable hlinks
: bool
98 ; mutable underinfo
: bool
102 type outline
= string * int * int * int;;
104 | Oarray
of outline array
105 | Olist
of outline list
106 | Onarrow
of outline array
* outline array
109 type rect
= (float * float * float * float * float * float * float * float);;
112 { mutable csock
: Unix.file_descr
113 ; mutable ssock
: Unix.file_descr
116 ; mutable rotate
: int
120 ; mutable layout
: layout list
121 ; pagemap
: ((int * int * int), string) Hashtbl.t
122 ; mutable pages
: (int * int * int) list
123 ; mutable pagecount
: int
124 ; pagecache
: string circbuf
125 ; mutable rendering
: bool
126 ; mutable mstate
: mstate
127 ; mutable searchpattern
: string
128 ; mutable rects
: (int * int * rect
) list
129 ; mutable rects1
: (int * int * rect
) list
130 ; mutable text
: string
131 ; mutable fullscreen
: (int * int) option
132 ; mutable textentry
: textentry
option
133 ; mutable outlines
: outlines
134 ; mutable outline
: (bool * int * int * outline array
* string) option
135 ; mutable bookmarks
: outline list
136 ; mutable path
: string
137 ; mutable invalidated
: int
138 ; mutable colorscale
: float
142 { pat
: string circbuf
143 ; pag
: string circbuf
144 ; nav
: float circbuf
175 ; pagemap
= Hashtbl.create
10
176 ; pagecache
= cbnew 10 ""
187 ; outlines
= Olist
[]
193 { nav
= cbnew 100 0.0
204 Printf.kprintf prerr_endline fmt
206 Printf.kprintf ignore fmt
210 let len = String.length s
in
212 let b = Buffer.create
n in
213 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
214 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
215 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
216 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
217 Buffer.add_string
b s
;
218 let s'
= Buffer.contents
b in
219 let n'
= Unix.write fd
s'
0 n in
220 if n'
!= n then failwith
"write failed";
225 let n = Unix.read fd
s 0 4 in
226 if n != 4 then failwith
"incomplete read(len)";
228 lor (Char.code
s.[0] lsl 24)
229 lor (Char.code
s.[1] lsl 16)
230 lor (Char.code
s.[2] lsl 8)
231 lor (Char.code
s.[3] lsl 0)
233 let s = String.create
len in
234 let n = Unix.read fd
s 0 len in
235 if n != len then failwith
"incomplete read(data)";
240 if y
= state.maxy
then 1.0
241 else float y
/. float state.maxy
245 let b = Buffer.create
10 in
246 Buffer.add_string
b s;
247 let rec combine = function
250 Buffer.add_char
b ' '
;
253 | `
b b -> if b then "1" else "0"
255 | `i i
-> string_of_int i
256 | `f f
-> string_of_float f
257 | `I f
-> string_of_int
(truncate f
)
259 Buffer.add_string
b s;
266 let cmd = Buffer.contents
(makecmd s l
) in
267 writecmd state.csock
cmd;
271 let rec f pn ph fh l
=
273 | (n, _
, h
) :: rest
->
274 let fh = fh + (n - pn
) * ph
in
278 let fh = fh + (ph
* (state.pagecount
- pn
)) in
281 let fh = f 0 0 0 state.pages
in
285 let getpagey pageno
=
286 let rec f pn ph y l
=
288 | (n, _
, h
) :: rest
->
291 y
+ (pageno
- pn
) * ph
293 let y = y + (n - pn
) * ph
in
297 y + (pageno
- pn
) * ph
303 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
304 if pageno
= state.pagecount
|| cacheleft
= 0
307 let ((_
, w
, h
) as curr
), rest
, pdimno
=
309 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
310 curr
, rest
, pdimno
+ 1
314 let pageno'
= pageno + 1 in
348 let accu = e :: accu in
349 f pageno' pdimno curr
350 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
351 (pred cacheleft
) accu
353 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
355 if state.invalidated
= 0
357 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
358 state.maxy
<- calcheight ();
365 let y = state.y + incr
in
367 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
371 let getopaque pageno =
372 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
374 with Not_found
-> None
377 let cache pageno opaque
=
378 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
382 let validopaque opaque
= String.length opaque
> 0;;
385 match getopaque l
.pageno with
386 | None
when not
state.rendering
->
387 state.rendering
<- true;
389 wcmd "render" [`i
(l
.pageno + 1)
397 let loadlayout layout =
398 let rec f all
= function
400 begin match getopaque l
.pageno with
401 | None
-> render l
; f false ls
402 | Some opaque
-> f (all
&& validopaque opaque
) ls
406 f (layout <> []) layout;
411 let y = min
state.maxy
y in
412 let pages = layout y state.h
in
413 let ready = loadlayout pages in
414 state.ty
<- yratio y;
415 if conf.showall
then (
417 state.layout <- pages;
419 Glut.postRedisplay
();
423 state.layout <- pages;
425 Glut.postRedisplay
();
427 if conf.preload
then begin
428 let y = if state.y < state.h
then 0 else state.y - state.h
in
429 let pages = layout y (state.h
*3) in
430 List.iter
render pages;
435 cbput state.hists
.nav
(yratio state.y);
436 cbrfollowlen state.hists
.nav
;
440 let y = cbget state.hists
.nav ~
-1 in
441 truncate
(y *. float state.maxy
)
445 let y = getpagey n in
455 state.invalidated
<- state.invalidated
+ 1;
459 let c = c *. state.colorscale
in
464 let ratio = float w
/. float state.w
in
465 let fixbookmark (s, l
, pageno, pagey
) =
466 let pagey = truncate
(float pagey *. ratio) in
467 (s, l
, pageno, pagey)
469 state.bookmarks
<- List.map
fixbookmark state.bookmarks
;
472 GlDraw.viewport
0 0 w h
;
473 GlMat.mode `modelview
;
474 GlMat.load_identity
();
475 GlMat.mode `projection
;
476 GlMat.load_identity
();
477 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
478 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
479 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
480 GlClear.color
(scalecolor 1.0);
481 GlClear.clear
[`color
];
484 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
488 GlDraw.color
(0.0, 0.0, 0.0);
490 (0.0, float (state.h
- 18))
491 (float (state.w
- conf.scrollw
- 1), float state.h
)
493 let font = Glut.BITMAP_8_BY_13
in
494 GlDraw.color
(1.0, 1.0, 1.0);
495 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
496 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
497 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
501 let len = String.length
state.text
in
502 match state.textentry
with
504 if len > 0 then showtext ' '
state.text
506 | Some
(c, text
, _
, _
, _
) ->
510 text ^
" [" ^
state.text ^
"]"
520 state.text
<- Printf.sprintf
"%c%s" c s;
521 Glut.postRedisplay
();
534 state.outlines
<- Olist
[]
537 state.rects
<- state.rects1
;
538 Glut.postRedisplay
()
541 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
542 state.pagecount
<- n;
543 state.invalidated
<- state.invalidated
- 1;
544 if state.invalidated
= 0
546 let rely = yratio state.y in
547 state.maxy
<- calcheight ();
548 gotoy (truncate
(float state.maxy
*. rely));
552 let s = Scanf.sscanf
cmd "t %n"
553 (fun n -> String.sub
cmd n (String.length
cmd - n))
555 Glut.setWindowTitle
s
558 let s = Scanf.sscanf
cmd "T %n"
559 (fun n -> String.sub
cmd n (String.length
cmd - n))
561 if state.textentry
= None
568 Glut.postRedisplay
();
574 let s = Scanf.sscanf
cmd "V %n"
575 (fun n -> String.sub
cmd n (String.length
cmd - n))
581 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
582 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
583 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
584 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
586 let y = (getpagey pageno) + truncate y0
in
589 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
592 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
593 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
594 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
595 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
598 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
602 Scanf.sscanf
cmd "r %d %d %d %d %s"
603 (fun n w h r p
-> (n, w
, h
, r
, p
))
605 Hashtbl.replace
state.pagemap
(n, w
, r
) p
;
606 let opaque = cbpeekw state.pagecache
in
607 if validopaque opaque
611 (fun k v a
-> if v
= opaque then k else a
)
612 state.pagemap
(-1, -1, -1)
614 wcmd "free" [`
s opaque];
615 Hashtbl.remove
state.pagemap
k
617 cbput state.pagecache p
;
618 state.rendering
<- false;
620 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy
)))
624 let (n, w
, h
) as pagelayout
=
625 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
627 state.pages <- pagelayout
:: state.pages
631 Scanf.sscanf
cmd "o %d %d %d %n" (fun l
n t pos
-> l
, n, t
, pos
)
633 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
634 let outline = (s, l
, n, t
) in
636 match state.outlines with
637 | Olist
outlines -> Olist
(outline :: outlines)
638 | Oarray _
-> Olist
[outline]
639 | Onarrow _
-> Olist
[outline]
641 state.outlines <- outlines
644 log "unknown cmd `%S'" cmd
647 let now = Unix.gettimeofday
;;
650 let r, _
, _
= Unix.select
[state.csock
] [] [] 0.001 in
653 if conf.autoscroll
then begin
654 let y = state.y + conf.scrollincr
in
655 let y = if y >= state.maxy
then 0 else y in
661 let cmd = readcmd state.csock
in
666 let onhist cb
= function
667 | HCprev
-> cbget cb ~
-1
668 | HCnext
-> cbget cb
1
669 | HCfirst
-> cbget cb ~
-(cb
.rc)
670 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
673 let search pattern forward
=
674 if String.length pattern
> 0
677 match state.layout with
680 l
.pageno, (l
.pagey + if forward
then 0 else 0*l
.pagevh
)
683 let b = makecmd "search"
684 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
686 Buffer.add_char
b '
,'
;
687 Buffer.add_string
b pattern
;
688 Buffer.add_char
b '
\000'
;
691 writecmd state.csock
cmd;
694 let intentry text key
=
695 let c = Char.unsafe_chr key
in
698 let s = "x" in s.[0] <- c;
699 let text = text ^
s in
703 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
708 let b = Buffer.create
(String.length
s + 1) in
709 Buffer.add_string
b s;
714 let textentry text key
=
715 let c = Char.unsafe_chr key
in
717 | _
when key
>= 32 && key
< 127 ->
718 let text = addchar text c in
722 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
727 state.rotate <- angle
;
729 wcmd "rotate" [`i angle
];
732 let optentry text key
=
733 let btos b = if b then "on" else "off" in
734 let c = Char.unsafe_chr key
in
738 try conf.scrollincr
<- int_of_string
s with exc
->
739 state.text <- Printf.sprintf
"bad integer `%s': %s"
740 s (Printexc.to_string exc
)
742 TEswitch
('#'
, "", None
, intentry, ondone)
747 Some
(int_of_string
s)
749 state.text <- Printf.sprintf
"bad integer `%s': %s"
750 s (Printexc.to_string exc
);
753 | Some angle
-> rotate angle
756 TEswitch
('^'
, "", None
, intentry, ondone)
759 conf.icase
<- not
conf.icase
;
760 TEdone
("case insensitive search " ^
(btos conf.icase
))
763 conf.preload
<- not
conf.preload
;
765 TEdone
("preload " ^
(btos conf.preload
))
768 conf.verbose
<- not
conf.verbose
;
769 TEdone
("verbose " ^
(btos conf.verbose
))
772 conf.maxhfit
<- not
conf.maxhfit
;
773 state.maxy
<- state.maxy
+ (if conf.maxhfit
then -state.h
else state.h
);
774 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
777 conf.crophack
<- not
conf.crophack
;
778 TEdone
("crophack " ^
btos conf.crophack
)
781 conf.showall
<- not
conf.showall
;
782 TEdone
("showall " ^
btos conf.showall
)
785 conf.underinfo
<- not
conf.underinfo
;
786 TEdone
("underinfo " ^
btos conf.underinfo
)
789 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
793 let maxoutlinerows () = (state.h
- 31) / 16;;
795 let enterselector allowdel
outlines errmsg
=
796 if Array.length
outlines = 0
802 match state.layout with
804 | {pageno=pageno} :: rest
-> pageno
808 if n = Array.length
outlines
811 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
812 if outlinepageno
>= pageno then n else loop (n+1)
817 Some
(allowdel
, active,
818 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
819 Glut.postRedisplay
();
822 let enteroutlinemode () =
824 match state.outlines with
827 let a = Array.of_list
(List.rev l
) in
828 state.outlines <- Oarray
a;
830 | Onarrow
(a, b) -> a
832 enterselector false outlines "Document has no outline";
835 let enterbookmarkmode () =
836 let bookmarks = Array.of_list
state.bookmarks in
837 enterselector true bookmarks "Document has no bookmarks (yet)";
841 let quickbookmark ?title
() =
842 match state.layout with
848 let sec = Unix.gettimeofday
() in
849 let tm = Unix.localtime
sec in
850 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
854 (tm.Unix.tm_year
+ 1900)
857 | Some
title -> title
860 (title, 0, l
.pageno, l
.pagey) :: state.bookmarks
863 let viewkeyboard ~key ~x ~
y =
865 state.textentry <- te
;
868 Glut.postRedisplay
()
870 match state.textentry with
872 let c = Char.chr key
in
887 Glut.postRedisplay
()
890 let ondone isforw
s =
891 cbput state.hists
.pat
s;
892 cbrfollowlen state.hists
.pat
;
893 state.searchpattern
<- s;
896 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
897 textentry, ondone (c ='
/'
)))
902 try int_of_string
s with exc
->
903 state.text <- Printf.sprintf
"bad integer `%s': %s"
904 s (Printexc.to_string exc
);
910 state.text <- "page bias is now " ^ string_of_int
n;
913 enttext (Some
('
+'
, "", None
, intentry, ondone))
919 enttext (Some
('
-'
, "", None
, optentry, ondone))
924 try int_of_string
s with exc
->
925 state.text <- Printf.sprintf
"bad integer `%s': %s"
926 s (Printexc.to_string exc
);
932 cbput state.hists
.pag
(string_of_int
n);
933 cbrfollowlen state.hists
.pag
;
934 gotoy (getpagey (n + conf.pagebias
- 1))
937 let pageentry text key
=
938 match Char.unsafe_chr key
with
940 | _
-> intentry text key
942 let text = "x" in text.[0] <- c;
943 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
947 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
948 reshape state.w
state.h
;
951 conf.hlinks
<- not
conf.hlinks
;
952 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
953 Glut.postRedisplay
()
956 conf.autoscroll
<- not
conf.autoscroll
959 begin match state.fullscreen
with
961 state.fullscreen
<- Some
(state.w
, state.h
);
964 state.fullscreen
<- None
;
965 Glut.reshapeWindow ~w ~h
972 search state.searchpattern
true
975 search state.searchpattern
false
978 begin match state.layout with
981 gotoy (state.y - l
.pagey);
985 begin match List.rev
state.layout with
988 gotoy (clamp (l
.pageh
- l
.pagey))
992 begin match state.layout with
995 gotoy (clamp (-l
.pageh
));
1000 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1002 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1004 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1008 else (100. *. (float state.y /. float maxy)) in
1011 Printf.sprintf
"Page %d of %d %.2f%%"
1012 (fn+1) state.pagecount
percent
1015 "Pages %d-%d of %d %.2f%%"
1016 (fn+1) (ln
+1) state.pagecount
percent
1021 begin match state.layout with
1024 Glut.reshapeWindow
(l
.pagew
+ conf.scrollw
) l
.pageh
;
1025 Glut.postRedisplay
();
1029 enterbookmarkmode ()
1033 match state.layout with
1035 state.bookmarks <- (s, 0, l
.pageno, l
.pagey) :: state.bookmarks
1038 enttext (Some
('~'
, "", None
, textentry, ondone))
1042 showtext ' '
"Quick bookmark added";
1045 begin match state.layout with
1047 let a = getpagewh l
.pagedimno
in
1051 (truncate
(1.8 *. (a.(1) -. a.(0))),
1052 truncate
(1.2 *. (a.(3) -. a.(0))))
1054 (truncate
(a.(1) -. a.(0)),
1055 truncate
(a.(3) -. a.(0)))
1057 Glut.reshapeWindow
(w + conf.scrollw
) h
;
1058 Glut.postRedisplay
();
1064 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1069 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1070 Glut.postRedisplay
()
1073 vlog "huh? %d %c" key
(Char.chr key
);
1076 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1077 let len = String.length
text in
1080 state.textentry <- None
;
1081 Glut.postRedisplay
();
1084 let s = String.sub
text 0 (len - 1) in
1085 enttext (Some
(c, s, onhist, onkey
, ondone))
1088 | Some
(c, text, onhist, onkey
, ondone) ->
1089 begin match Char.unsafe_chr key
with
1092 state.textentry <- None
;
1093 Glut.postRedisplay
()
1096 state.textentry <- None
;
1097 Glut.postRedisplay
()
1100 begin match onkey
text key
with
1102 state.textentry <- None
;
1104 Glut.postRedisplay
()
1107 enttext (Some
(c, text, onhist, onkey
, ondone));
1110 state.textentry <- None
;
1111 Glut.postRedisplay
()
1114 state.textentry <- Some te
;
1115 Glut.postRedisplay
()
1120 let narrow outlines pattern
=
1121 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1125 let rec fold accu n =
1126 if n = -1 then accu else
1127 let (s, _
, _
, _
) as o
= outlines.(n) in
1129 if (try ignore
(Str.search_forward re
s 0); true
1130 with Not_found
-> false)
1136 let matched = fold [] (Array.length
outlines - 1) in
1137 if matched = [] then None
else Some
(Array.of_list
matched)
1140 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1141 let search active pattern incr
=
1144 if n = Array.length
outlines || n = -1 then None
else
1145 let (s, _
, _
, _
) = outlines.(n) in
1147 (try ignore
(Str.search_forward re
s 0); true
1148 with Not_found
-> false)
1150 else loop (n + incr
)
1155 let re = Str.regexp_case_fold pattern
in
1161 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1164 if String.length qsearch
= 0
1167 state.outline <- None
;
1168 Glut.postRedisplay
();
1172 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1173 Glut.postRedisplay
();
1177 let incr = if key
= 18 then -1 else 1 in
1179 match search (active + incr) qsearch
incr with
1181 state.text <- qsearch ^
" [not found]";
1184 state.text <- qsearch
;
1185 active, firstof active
1187 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1188 Glut.postRedisplay
();
1191 let len = String.length qsearch
in
1198 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1201 let qsearch = String.sub
qsearch 0 (len - 1) in
1203 match search active qsearch ~
-1 with
1205 state.text <- qsearch ^
" [not found]";
1208 state.text <- qsearch;
1209 active, firstof active
1211 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1213 Glut.postRedisplay
()
1216 if active < Array.length
outlines
1218 let (_
, _
, n, t
) = outlines.(active) in
1222 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1223 state.outline <- None
;
1224 Glut.postRedisplay
();
1226 | _
when key
>= 32 && key
< 127 ->
1227 let pattern = addchar qsearch (Char.chr key
) in
1229 match search active pattern 1 with
1231 state.text <- pattern ^
" [not found]";
1234 state.text <- pattern;
1235 active, firstof active
1237 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1238 Glut.postRedisplay
()
1240 | 14 when not allowdel
->
1241 let optoutlines = narrow outlines qsearch in
1242 begin match optoutlines with
1243 | None
-> state.text <- "can't narrow"
1245 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1246 match state.outlines with
1248 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1249 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1251 Glut.postRedisplay
()
1253 | 21 when not allowdel
->
1255 match state.outlines with
1258 let a = Array.of_list
(List.rev l
) in
1259 state.outlines <- Oarray
a;
1262 state.outlines <- Oarray
b;
1265 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1266 Glut.postRedisplay
()
1270 Some
(allowdel
, active, firstof active, outlines, qsearch);
1271 Glut.postRedisplay
()
1273 | 127 when allowdel
->
1274 let len = Array.length
outlines - 1 in
1277 state.outline <- None
;
1278 state.bookmarks <- [];
1281 let bookmarks = Array.init
len
1283 let i = if i >= active then i + 1 else i in
1294 Glut.postRedisplay
()
1296 | _
-> log "unknown key %d" key
1299 let keyboard ~key ~x ~
y =
1304 match state.outline with
1305 | None
-> viewkeyboard ~key ~x ~
y
1306 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1309 let special ~key ~x ~
y =
1310 match state.outline with
1312 begin match state.textentry with
1316 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1317 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1318 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1319 | Glut.KEY_PAGE_UP
-> clamp (-state.h
)
1320 | Glut.KEY_PAGE_DOWN
-> clamp state.h
1321 | Glut.KEY_HOME
-> addnav (); 0
1324 state.maxy - (if conf.maxhfit
then state.h
else 0)
1330 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1333 | Glut.KEY_UP
-> onhist HCprev
1334 | Glut.KEY_DOWN
-> onhist HCnext
1335 | Glut.KEY_HOME
-> onhist HCfirst
1336 | Glut.KEY_END
-> onhist HClast
1339 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1340 Glut.postRedisplay
()
1345 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1346 let maxrows = maxoutlinerows () in
1348 let active = active + incr in
1349 let active = max
0 (min
active (Array.length
outlines - 1)) in
1353 let rows = active - first in
1354 if rows > maxrows then active - maxrows else first
1357 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1358 Glut.postRedisplay
()
1361 | Glut.KEY_UP
-> navigate ~
-1
1362 | Glut.KEY_DOWN
-> navigate 1
1363 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1364 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1367 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1368 Glut.postRedisplay
()
1371 let active = Array.length
outlines - 1 in
1372 let first = max
0 (active - maxrows) in
1373 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1374 Glut.postRedisplay
()
1379 let drawplaceholder l
=
1380 GlDraw.color
(scalecolor 1.0);
1382 (0.0, float l
.pagedispy
)
1383 (float l
.pagew
, float (l
.pagedispy
+ l
.pagevh
))
1386 and y = float (l
.pagedispy
+ 13) in
1387 let font = Glut.BITMAP_8_BY_13
in
1388 GlDraw.color
(0.0, 0.0, 0.0);
1389 GlPix.raster_pos ~
x ~
y ();
1390 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1391 ("Loading " ^ string_of_int l
.pageno);
1394 let now () = Unix.gettimeofday
();;
1397 begin match getopaque l
.pageno with
1398 | Some
opaque when validopaque opaque ->
1399 if state.textentry = None
1400 then GlDraw.color
(scalecolor 1.0)
1401 else GlDraw.color
(scalecolor 0.4);
1403 draw l
.pagedispy l
.pagew l
.pagevh l
.pagey opaque;
1406 if conf.hlinks
then highlightlinks
opaque (l
.pagedispy
- l
.pagey);
1407 vlog "draw %f sec" d;
1412 GlDraw.color
(0.5, 0.5, 0.5);
1415 (float (state.w - conf.scrollw
), float (i + (l
.pagedispy
- i)))
1417 l
.pagedispy
+ l
.pagevh
;
1420 let scrollindicator () =
1421 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1422 GlDraw.color
(0.64 , 0.64, 0.64);
1424 (float (state.w - conf.scrollw
), 0.)
1425 (float state.w, float state.h
)
1427 GlDraw.color
(0.0, 0.0, 0.0);
1428 let sh = (float (maxy + state.h
) /. float state.h
) in
1429 let sh = float state.h
/. sh in
1430 let sh = max
sh (float conf.scrollh
) in
1433 if state.y = state.maxy
1435 else float state.y /. float maxy
1437 let position = (float state.h
-. sh) *. percent in
1440 if position +. sh > float state.h
1447 (float (state.w - conf.scrollw
), position)
1448 (float state.w, position +. sh)
1453 match state.mstate
with
1457 | Msel
((x0
, y0
), (x1
, y1
)) ->
1458 let rec loop = function
1460 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh
))
1461 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh
)))
1463 match getopaque l
.pageno with
1464 | Some
opaque when validopaque opaque ->
1465 let oy = -l
.pagey + l
.pagedispy
in
1466 seltext
opaque (x0
, y0
, x1
, y1
) oy;
1477 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1478 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1480 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1482 if l
.pageno = pageno
1484 let d = float (l
.pagedispy
- l
.pagey) in
1485 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1486 GlDraw.begins `quads
;
1488 GlDraw.vertex2
(x0
, y0
+.d);
1489 GlDraw.vertex2
(x1
, y1
+.d);
1490 GlDraw.vertex2
(x2
, y2
+.d);
1491 GlDraw.vertex2
(x3
, y3
+.d);
1501 let showoutline = function
1503 | Some
(allowdel
, active, first, outlines, qsearch) ->
1505 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1506 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1507 GlDraw.rect
(0., 0.) (float state.w, float state.h
);
1510 GlDraw.color
(1., 1., 1.);
1511 let font = Glut.BITMAP_9_BY_15
in
1512 let draw_string x y s =
1513 GlPix.raster_pos ~
x ~
y ();
1514 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1517 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1520 let (s, l
, _
, _
) = outlines.(row
) in
1521 let y = (row
- first) * 16 in
1526 GlDraw.polygon_mode `both `line
;
1527 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1528 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1529 GlDraw.rect
(0., float (y + 1))
1530 (float (state.w - conf.scrollw
- 1), float (y + 18));
1531 GlDraw.polygon_mode `both `fill
;
1533 GlDraw.color
(1., 1., 1.);
1535 draw_string (float x) (float (y + 16)) s;
1543 let lasty = List.fold_left
drawpage 0 (state.layout) in
1544 GlDraw.color
(scalecolor 0.5);
1547 (float (state.w - conf.scrollw
), float state.h
)
1552 showoutline state.outline;
1554 Glut.swapBuffers
();
1558 let rec f = function
1560 begin match getopaque l
.pageno with
1561 | Some
opaque when validopaque opaque ->
1562 let y = y - l
.pagedispy
in
1565 let y = l
.pagey + y in
1566 match whatsunder
opaque x y with
1579 let mouse ~button ~bstate ~
x ~
y =
1581 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1589 let incr = incr * 2 in
1590 let y = clamp incr in
1593 | Glut.LEFT_BUTTON
when state.outline = None
->
1594 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1595 begin match dest with
1596 | Ulinkgoto
(pageno, top
) ->
1604 | Unone
when bstate
= Glut.DOWN
->
1605 Glut.setCursor
Glut.CURSOR_INHERIT
;
1606 state.mstate
<- Mnone
1608 | Unone
| Utext _
->
1609 if bstate
= Glut.DOWN
1611 if state.rotate mod 360 = 0 then (
1612 state.mstate
<- Msel
((x, y), (x, y));
1613 Glut.postRedisplay
()
1617 match state.mstate
with
1619 | Msel
((x0
, y0
), (x1
, y1
)) ->
1621 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh
))
1622 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh
)))
1624 match getopaque l
.pageno with
1625 | Some
opaque when validopaque opaque ->
1629 List.iter
f state.layout;
1630 copysel
""; (* ugly *)
1631 Glut.setCursor
Glut.CURSOR_INHERIT
;
1632 state.mstate
<- Mnone
;
1639 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1642 if state.outline = None
1644 match state.mstate
with
1647 state.mstate
<- Msel
(a, (x, y));
1648 Glut.postRedisplay
()
1652 if state.outline = None
1654 match state.mstate
with
1656 begin match getunder x y with
1657 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
1658 | Ulinkuri _
| Ulinkgoto _
-> Glut.setCursor
Glut.CURSOR_INFO
1660 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
1661 Glut.setCursor
Glut.CURSOR_TEXT
1671 if Sys.os_type
= "Win32"
1673 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1675 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1677 Filename.concat
home "llpp"
1681 let ic = open_in_bin
statepath in
1682 let hash = input_value
ic in
1688 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1695 match state.fullscreen
with
1696 | None
-> state.w, state.h
1699 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h
);
1700 let oc = open_out_bin
statepath in
1701 output_value
oc pstate
1705 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1710 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1713 state.bookmarks <- statebookmarks;
1714 with Not_found
-> ()
1716 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1719 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1721 if String.length
state.path
= 0
1722 then (prerr_endline
"filename missing"; exit
1)
1727 let _ = Glut.init
Sys.argv
in
1728 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1729 let () = Glut.initWindowSize
state.w state.h
in
1730 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1733 if Sys.os_type
= "Unix"
1735 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1737 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1738 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1739 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1740 Unix.bind
sock addr;
1742 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1743 Unix.connect
csock addr;
1744 let ssock, _ = Unix.accept
sock in
1747 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1748 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1752 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1756 let () = Glut.displayFunc
display in
1757 let () = Glut.reshapeFunc
reshape in
1758 let () = Glut.keyboardFunc
keyboard in
1759 let () = Glut.specialFunc
special in
1760 let () = Glut.idleFunc
(Some
idle) in
1761 let () = Glut.mouseFunc
mouse in
1762 let () = Glut.motionFunc
motion in
1763 let () = Glut.passiveMotionFunc
pmotion in
1766 state.csock <- csock;
1767 state.ssock <- ssock;
1768 state.text <- "Opening " ^
name;
1769 writecmd csock ("open " ^
name ^
"\000");
1773 let rec handlelablglutbug () =
1776 with Glut.BadEnum
"key in special_of_int" ->
1777 showtext '
!'
" LablGlut bug: special key not recognized";
1778 handlelablglutbug ()
1780 handlelablglutbug ();