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;
410 if conf.preload && List.length
state.layout < cblen state.pagecache
then begin
411 let y = if state.y < state.h
then 0 else state.y - state.h
in
412 let pages = layout y (state.h
*3) in
413 List.iter
render pages;
419 let y = min
state.maxy
y in
420 let pages = layout y state.h
in
421 let ready = loadlayout pages in
422 state.ty
<- yratio y;
423 if conf.showall
then (
425 state.layout <- pages;
427 Glut.postRedisplay
();
431 state.layout <- pages;
433 Glut.postRedisplay
();
439 cbput state.hists
.nav
(yratio state.y);
440 cbrfollowlen state.hists
.nav
;
444 let y = cbget state.hists
.nav ~
-1 in
445 truncate
(y *. float state.maxy
)
449 let y = getpagey n in
459 state.invalidated
<- state.invalidated
+ 1;
463 let c = c *. state.colorscale
in
468 let ratio = float w
/. float state.w
in
469 let fixbookmark (s, l
, pageno, pagey
) =
470 let pagey = truncate
(float pagey *. ratio) in
471 (s, l
, pageno, pagey)
473 state.bookmarks
<- List.map
fixbookmark state.bookmarks
;
476 GlDraw.viewport
0 0 w h
;
477 GlMat.mode `modelview
;
478 GlMat.load_identity
();
479 GlMat.mode `projection
;
480 GlMat.load_identity
();
481 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
482 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
483 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
484 GlClear.color
(scalecolor 1.0);
485 GlClear.clear
[`color
];
488 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
492 GlDraw.color
(0.0, 0.0, 0.0);
494 (0.0, float (state.h
- 18))
495 (float (state.w
- conf.scrollw
- 1), float state.h
)
497 let font = Glut.BITMAP_8_BY_13
in
498 GlDraw.color
(1.0, 1.0, 1.0);
499 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
500 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
501 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
505 let len = String.length
state.text
in
506 match state.textentry
with
508 if len > 0 then showtext ' '
state.text
510 | Some
(c, text
, _
, _
, _
) ->
514 text ^
" [" ^
state.text ^
"]"
524 state.text
<- Printf.sprintf
"%c%s" c s;
525 Glut.postRedisplay
();
538 state.outlines
<- Olist
[]
541 state.rects
<- state.rects1
;
542 Glut.postRedisplay
()
545 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
546 state.pagecount
<- n;
547 state.invalidated
<- state.invalidated
- 1;
548 if state.invalidated
= 0
550 let rely = yratio state.y in
551 state.maxy
<- calcheight ();
552 gotoy (truncate
(float state.maxy
*. rely));
556 let s = Scanf.sscanf
cmd "t %n"
557 (fun n -> String.sub
cmd n (String.length
cmd - n))
559 Glut.setWindowTitle
s
562 let s = Scanf.sscanf
cmd "T %n"
563 (fun n -> String.sub
cmd n (String.length
cmd - n))
565 if state.textentry
= None
572 Glut.postRedisplay
();
578 let s = Scanf.sscanf
cmd "V %n"
579 (fun n -> String.sub
cmd n (String.length
cmd - n))
585 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
586 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
587 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
588 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
590 let y = (getpagey pageno) + truncate y0
in
593 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
596 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
597 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
598 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
599 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
602 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
606 Scanf.sscanf
cmd "r %d %d %d %d %s"
607 (fun n w h r p
-> (n, w
, h
, r
, p
))
609 Hashtbl.replace
state.pagemap
(n, w
, r
) p
;
610 let opaque = cbpeekw state.pagecache
in
611 if validopaque opaque
615 (fun k v a
-> if v
= opaque then k else a
)
616 state.pagemap
(-1, -1, -1)
618 wcmd "free" [`
s opaque];
619 Hashtbl.remove
state.pagemap
k
621 cbput state.pagecache p
;
622 state.rendering
<- false;
624 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy
)))
626 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
627 if visible then gotoy state.y
628 else (ignore
(loadlayout state.layout); preload ())
632 let (n, w
, h
) as pagelayout
=
633 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
635 state.pages <- pagelayout
:: state.pages
639 Scanf.sscanf
cmd "o %d %d %d %n" (fun l
n t pos
-> l
, n, t
, pos
)
641 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
642 let outline = (s, l
, n, t
) in
644 match state.outlines with
645 | Olist
outlines -> Olist
(outline :: outlines)
646 | Oarray _
-> Olist
[outline]
647 | Onarrow _
-> Olist
[outline]
649 state.outlines <- outlines
652 log "unknown cmd `%S'" cmd
655 let now = Unix.gettimeofday
;;
659 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
662 if conf.autoscroll
then begin
663 let y = state.y + conf.scrollincr
in
664 let y = if y >= state.maxy
then 0 else y in
670 let cmd = readcmd state.csock
in
677 let onhist cb
= function
678 | HCprev
-> cbget cb ~
-1
679 | HCnext
-> cbget cb
1
680 | HCfirst
-> cbget cb ~
-(cb
.rc)
681 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
684 let search pattern forward
=
685 if String.length pattern
> 0
688 match state.layout with
691 l
.pageno, (l
.pagey + if forward
then 0 else 0*l
.pagevh
)
694 let b = makecmd "search"
695 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
697 Buffer.add_char
b '
,'
;
698 Buffer.add_string
b pattern
;
699 Buffer.add_char
b '
\000'
;
702 writecmd state.csock
cmd;
705 let intentry text key
=
706 let c = Char.unsafe_chr key
in
709 let s = "x" in s.[0] <- c;
710 let text = text ^
s in
714 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
719 let b = Buffer.create
(String.length
s + 1) in
720 Buffer.add_string
b s;
725 let textentry text key
=
726 let c = Char.unsafe_chr key
in
728 | _
when key
>= 32 && key
< 127 ->
729 let text = addchar text c in
733 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
738 state.rotate <- angle
;
740 wcmd "rotate" [`i angle
];
743 let optentry text key
=
744 let btos b = if b then "on" else "off" in
745 let c = Char.unsafe_chr key
in
749 try conf.scrollincr
<- int_of_string
s with exc
->
750 state.text <- Printf.sprintf
"bad integer `%s': %s"
751 s (Printexc.to_string exc
)
753 TEswitch
('#'
, "", None
, intentry, ondone)
758 Some
(int_of_string
s)
760 state.text <- Printf.sprintf
"bad integer `%s': %s"
761 s (Printexc.to_string exc
);
764 | Some angle
-> rotate angle
767 TEswitch
('^'
, "", None
, intentry, ondone)
770 conf.icase
<- not
conf.icase
;
771 TEdone
("case insensitive search " ^
(btos conf.icase
))
774 conf.preload <- not
conf.preload;
776 TEdone
("preload " ^
(btos conf.preload))
779 conf.verbose
<- not
conf.verbose
;
780 TEdone
("verbose " ^
(btos conf.verbose
))
783 conf.maxhfit
<- not
conf.maxhfit
;
784 state.maxy
<- state.maxy
+ (if conf.maxhfit
then -state.h
else state.h
);
785 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
788 conf.crophack
<- not
conf.crophack
;
789 TEdone
("crophack " ^
btos conf.crophack
)
792 conf.showall
<- not
conf.showall
;
793 TEdone
("showall " ^
btos conf.showall
)
796 conf.underinfo
<- not
conf.underinfo
;
797 TEdone
("underinfo " ^
btos conf.underinfo
)
800 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
804 let maxoutlinerows () = (state.h
- 31) / 16;;
806 let enterselector allowdel
outlines errmsg
=
807 if Array.length
outlines = 0
812 Glut.setCursor
Glut.CURSOR_INHERIT
;
814 match state.layout with
816 | {pageno=pageno} :: rest
-> pageno
820 if n = Array.length
outlines
823 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
824 if outlinepageno
>= pageno then n else loop (n+1)
829 Some
(allowdel
, active,
830 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
831 Glut.postRedisplay
();
835 let enteroutlinemode () =
837 match state.outlines with
840 let a = Array.of_list
(List.rev l
) in
841 state.outlines <- Oarray
a;
843 | Onarrow
(a, b) -> a
845 enterselector false outlines "Document has no outline";
848 let enterbookmarkmode () =
849 let bookmarks = Array.of_list
state.bookmarks in
850 enterselector true bookmarks "Document has no bookmarks (yet)";
854 let quickbookmark ?title
() =
855 match state.layout with
861 let sec = Unix.gettimeofday
() in
862 let tm = Unix.localtime
sec in
863 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
867 (tm.Unix.tm_year
+ 1900)
870 | Some
title -> title
873 (title, 0, l
.pageno, l
.pagey) :: state.bookmarks
877 state.fullscreen
<- None
;
878 Glut.reshapeWindow w h
;
881 let viewkeyboard ~key ~x ~
y =
883 state.textentry <- te
;
886 Glut.postRedisplay
()
888 match state.textentry with
890 let c = Char.chr key
in
905 Glut.postRedisplay
()
908 let ondone isforw
s =
909 cbput state.hists
.pat
s;
910 cbrfollowlen state.hists
.pat
;
911 state.searchpattern
<- s;
914 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
915 textentry, ondone (c ='
/'
)))
920 try int_of_string
s with exc
->
921 state.text <- Printf.sprintf
"bad integer `%s': %s"
922 s (Printexc.to_string exc
);
928 state.text <- "page bias is now " ^ string_of_int
n;
931 enttext (Some
('
+'
, "", None
, intentry, ondone))
937 enttext (Some
('
-'
, "", None
, optentry, ondone))
942 try int_of_string
s with exc
->
943 state.text <- Printf.sprintf
"bad integer `%s': %s"
944 s (Printexc.to_string exc
);
950 cbput state.hists
.pag
(string_of_int
n);
951 cbrfollowlen state.hists
.pag
;
952 gotoy (getpagey (n + conf.pagebias
- 1))
955 let pageentry text key
=
956 match Char.unsafe_chr key
with
958 | _
-> intentry text key
960 let text = "x" in text.[0] <- c;
961 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
965 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
966 reshape state.w
state.h
;
969 conf.hlinks
<- not
conf.hlinks
;
970 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
971 Glut.postRedisplay
()
974 conf.autoscroll
<- not
conf.autoscroll
977 begin match state.fullscreen
with
979 state.fullscreen
<- Some
(state.w
, state.h
);
982 state.fullscreen
<- None
;
990 search state.searchpattern
true
993 search state.searchpattern
false
996 begin match state.layout with
999 gotoy (state.y - l
.pagey);
1003 begin match List.rev
state.layout with
1006 gotoy (clamp (l
.pageh
- l
.pagey))
1010 begin match state.layout with
1013 gotoy (clamp (-l
.pageh
));
1018 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1020 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1022 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1026 else (100. *. (float state.y /. float maxy)) in
1029 Printf.sprintf
"Page %d of %d %.2f%%"
1030 (fn+1) state.pagecount
percent
1033 "Pages %d-%d of %d %.2f%%"
1034 (fn+1) (ln
+1) state.pagecount
percent
1039 begin match state.layout with
1042 doreshape (l
.pagew
+ conf.scrollw
) l
.pageh
;
1043 Glut.postRedisplay
();
1047 enterbookmarkmode ()
1051 match state.layout with
1053 state.bookmarks <- (s, 0, l
.pageno, l
.pagey) :: state.bookmarks
1056 enttext (Some
('~'
, "", None
, textentry, ondone))
1060 showtext ' '
"Quick bookmark added";
1063 begin match state.layout with
1065 let a = getpagewh l
.pagedimno
in
1069 (truncate
(1.8 *. (a.(1) -. a.(0))),
1070 truncate
(1.2 *. (a.(3) -. a.(0))))
1072 (truncate
(a.(1) -. a.(0)),
1073 truncate
(a.(3) -. a.(0)))
1075 doreshape (w + conf.scrollw
) h
;
1076 Glut.postRedisplay
();
1082 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1087 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1088 Glut.postRedisplay
()
1090 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1091 | 'j'
-> gotoy (clamp conf.scrollincr
)
1094 vlog "huh? %d %c" key
(Char.chr key
);
1097 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1098 let len = String.length
text in
1101 state.textentry <- None
;
1102 Glut.postRedisplay
();
1105 let s = String.sub
text 0 (len - 1) in
1106 enttext (Some
(c, s, onhist, onkey
, ondone))
1109 | Some
(c, text, onhist, onkey
, ondone) ->
1110 begin match Char.unsafe_chr key
with
1113 state.textentry <- None
;
1114 Glut.postRedisplay
()
1117 state.textentry <- None
;
1118 Glut.postRedisplay
()
1121 begin match onkey
text key
with
1123 state.textentry <- None
;
1125 Glut.postRedisplay
()
1128 enttext (Some
(c, text, onhist, onkey
, ondone));
1131 state.textentry <- None
;
1132 Glut.postRedisplay
()
1135 state.textentry <- Some te
;
1136 Glut.postRedisplay
()
1141 let narrow outlines pattern
=
1142 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1146 let rec fold accu n =
1147 if n = -1 then accu else
1148 let (s, _
, _
, _
) as o
= outlines.(n) in
1150 if (try ignore
(Str.search_forward re
s 0); true
1151 with Not_found
-> false)
1157 let matched = fold [] (Array.length
outlines - 1) in
1158 if matched = [] then None
else Some
(Array.of_list
matched)
1161 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1162 let search active pattern incr
=
1165 if n = Array.length
outlines || n = -1 then None
else
1166 let (s, _
, _
, _
) = outlines.(n) in
1168 (try ignore
(Str.search_forward re
s 0); true
1169 with Not_found
-> false)
1171 else loop (n + incr
)
1176 let re = Str.regexp_case_fold pattern
in
1182 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1185 if String.length qsearch
= 0
1188 state.outline <- None
;
1189 Glut.postRedisplay
();
1193 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1194 Glut.postRedisplay
();
1198 let incr = if key
= 18 then -1 else 1 in
1200 match search (active + incr) qsearch
incr with
1202 state.text <- qsearch ^
" [not found]";
1205 state.text <- qsearch
;
1206 active, firstof active
1208 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1209 Glut.postRedisplay
();
1212 let len = String.length qsearch
in
1219 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1222 let qsearch = String.sub
qsearch 0 (len - 1) in
1224 match search active qsearch ~
-1 with
1226 state.text <- qsearch ^
" [not found]";
1229 state.text <- qsearch;
1230 active, firstof active
1232 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1234 Glut.postRedisplay
()
1237 if active < Array.length
outlines
1239 let (_
, _
, n, t
) = outlines.(active) in
1243 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1244 state.outline <- None
;
1245 Glut.postRedisplay
();
1247 | _
when key
>= 32 && key
< 127 ->
1248 let pattern = addchar qsearch (Char.chr key
) in
1250 match search active pattern 1 with
1252 state.text <- pattern ^
" [not found]";
1255 state.text <- pattern;
1256 active, firstof active
1258 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1259 Glut.postRedisplay
()
1261 | 14 when not allowdel
->
1262 let optoutlines = narrow outlines qsearch in
1263 begin match optoutlines with
1264 | None
-> state.text <- "can't narrow"
1266 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1267 match state.outlines with
1269 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1270 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1272 Glut.postRedisplay
()
1274 | 21 when not allowdel
->
1276 match state.outlines with
1279 let a = Array.of_list
(List.rev l
) in
1280 state.outlines <- Oarray
a;
1283 state.outlines <- Oarray
b;
1286 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1287 Glut.postRedisplay
()
1291 Some
(allowdel
, active, firstof active, outlines, qsearch);
1292 Glut.postRedisplay
()
1294 | 127 when allowdel
->
1295 let len = Array.length
outlines - 1 in
1298 state.outline <- None
;
1299 state.bookmarks <- [];
1302 let bookmarks = Array.init
len
1304 let i = if i >= active then i + 1 else i in
1315 Glut.postRedisplay
()
1317 | _
-> log "unknown key %d" key
1320 let keyboard ~key ~x ~
y =
1325 match state.outline with
1326 | None
-> viewkeyboard ~key ~x ~
y
1327 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1330 let special ~key ~x ~
y =
1331 match state.outline with
1333 begin match state.textentry with
1337 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1338 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1339 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1340 | Glut.KEY_PAGE_UP
->
1341 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1343 match state.layout with
1345 | l
:: _
-> state.y - l
.pagey
1348 | Glut.KEY_PAGE_DOWN
->
1349 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1351 match List.rev
state.layout with
1353 | l
:: _
-> getpagey l
.pageno
1356 | Glut.KEY_HOME
-> addnav (); 0
1359 state.maxy - (if conf.maxhfit
then state.h
else 0)
1365 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1368 | Glut.KEY_UP
-> onhist HCprev
1369 | Glut.KEY_DOWN
-> onhist HCnext
1370 | Glut.KEY_HOME
-> onhist HCfirst
1371 | Glut.KEY_END
-> onhist HClast
1374 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1375 Glut.postRedisplay
()
1380 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1381 let maxrows = maxoutlinerows () in
1383 let active = active + incr in
1384 let active = max
0 (min
active (Array.length
outlines - 1)) in
1388 let rows = active - first in
1389 if rows > maxrows then active - maxrows else first
1392 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1393 Glut.postRedisplay
()
1396 | Glut.KEY_UP
-> navigate ~
-1
1397 | Glut.KEY_DOWN
-> navigate 1
1398 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1399 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1402 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1403 Glut.postRedisplay
()
1406 let active = Array.length
outlines - 1 in
1407 let first = max
0 (active - maxrows) in
1408 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1409 Glut.postRedisplay
()
1414 let drawplaceholder l
=
1415 GlDraw.color
(scalecolor 1.0);
1417 (0.0, float l
.pagedispy
)
1418 (float l
.pagew
, float (l
.pagedispy
+ l
.pagevh
))
1421 and y = float (l
.pagedispy
+ 13) in
1422 let font = Glut.BITMAP_8_BY_13
in
1423 GlDraw.color
(0.0, 0.0, 0.0);
1424 GlPix.raster_pos ~
x ~
y ();
1425 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1426 ("Loading " ^ string_of_int l
.pageno);
1429 let now () = Unix.gettimeofday
();;
1432 begin match getopaque l
.pageno with
1433 | Some
opaque when validopaque opaque ->
1434 if state.textentry = None
1435 then GlDraw.color
(scalecolor 1.0)
1436 else GlDraw.color
(scalecolor 0.4);
1438 draw l
.pagedispy l
.pagew l
.pagevh l
.pagey opaque;
1441 if conf.hlinks
then highlightlinks
opaque (l
.pagedispy
- l
.pagey);
1442 vlog "draw %f sec" d;
1447 GlDraw.color
(0.5, 0.5, 0.5);
1450 (float (state.w - conf.scrollw
), float (i + (l
.pagedispy
- i)))
1452 l
.pagedispy
+ l
.pagevh
;
1455 let scrollindicator () =
1456 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1457 GlDraw.color
(0.64 , 0.64, 0.64);
1459 (float (state.w - conf.scrollw
), 0.)
1460 (float state.w, float state.h
)
1462 GlDraw.color
(0.0, 0.0, 0.0);
1463 let sh = (float (maxy + state.h
) /. float state.h
) in
1464 let sh = float state.h
/. sh in
1465 let sh = max
sh (float conf.scrollh
) in
1468 if state.y = state.maxy
1470 else float state.y /. float maxy
1472 let position = (float state.h
-. sh) *. percent in
1475 if position +. sh > float state.h
1482 (float (state.w - conf.scrollw
), position)
1483 (float state.w, position +. sh)
1488 match state.mstate
with
1492 | Msel
((x0
, y0
), (x1
, y1
)) ->
1493 let rec loop = function
1495 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh
))
1496 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh
)))
1498 match getopaque l
.pageno with
1499 | Some
opaque when validopaque opaque ->
1500 let oy = -l
.pagey + l
.pagedispy
in
1501 seltext
opaque (x0
, y0
, x1
, y1
) oy;
1512 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1513 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1515 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1517 if l
.pageno = pageno
1519 let d = float (l
.pagedispy
- l
.pagey) in
1520 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1521 GlDraw.begins `quads
;
1523 GlDraw.vertex2
(x0
, y0
+.d);
1524 GlDraw.vertex2
(x1
, y1
+.d);
1525 GlDraw.vertex2
(x2
, y2
+.d);
1526 GlDraw.vertex2
(x3
, y3
+.d);
1536 let showoutline = function
1538 | Some
(allowdel
, active, first, outlines, qsearch) ->
1540 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1541 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1542 GlDraw.rect
(0., 0.) (float state.w, float state.h
);
1545 GlDraw.color
(1., 1., 1.);
1546 let font = Glut.BITMAP_9_BY_15
in
1547 let draw_string x y s =
1548 GlPix.raster_pos ~
x ~
y ();
1549 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1552 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1555 let (s, l
, _
, _
) = outlines.(row
) in
1556 let y = (row
- first) * 16 in
1561 GlDraw.polygon_mode `both `line
;
1562 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1563 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1564 GlDraw.rect
(0., float (y + 1))
1565 (float (state.w - conf.scrollw
- 1), float (y + 18));
1566 GlDraw.polygon_mode `both `fill
;
1568 GlDraw.color
(1., 1., 1.);
1570 draw_string (float x) (float (y + 16)) s;
1578 let lasty = List.fold_left
drawpage 0 (state.layout) in
1579 GlDraw.color
(scalecolor 0.5);
1582 (float (state.w - conf.scrollw
), float state.h
)
1587 showoutline state.outline;
1589 Glut.swapBuffers
();
1593 let rec f = function
1595 begin match getopaque l
.pageno with
1596 | Some
opaque when validopaque opaque ->
1597 let y = y - l
.pagedispy
in
1600 let y = l
.pagey + y in
1601 match whatsunder
opaque x y with
1614 let mouse ~button ~bstate ~
x ~
y =
1616 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1624 let incr = incr * 2 in
1625 let y = clamp incr in
1628 | Glut.LEFT_BUTTON
when state.outline = None
->
1629 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1630 begin match dest with
1631 | Ulinkgoto
(pageno, top
) ->
1639 | Unone
when bstate
= Glut.DOWN
->
1640 Glut.setCursor
Glut.CURSOR_INHERIT
;
1641 state.mstate
<- Mnone
1643 | Unone
| Utext _
->
1644 if bstate
= Glut.DOWN
1646 if state.rotate mod 360 = 0 then (
1647 state.mstate
<- Msel
((x, y), (x, y));
1648 Glut.postRedisplay
()
1652 match state.mstate
with
1654 | Msel
((x0
, y0
), (x1
, y1
)) ->
1656 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh
))
1657 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh
)))
1659 match getopaque l
.pageno with
1660 | Some
opaque when validopaque opaque ->
1664 List.iter
f state.layout;
1665 copysel
""; (* ugly *)
1666 Glut.setCursor
Glut.CURSOR_INHERIT
;
1667 state.mstate
<- Mnone
;
1674 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1677 if state.outline = None
1679 match state.mstate
with
1682 state.mstate
<- Msel
(a, (x, y));
1683 Glut.postRedisplay
()
1687 if state.outline = None
1689 match state.mstate
with
1691 begin match getunder x y with
1692 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
1694 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
1695 Glut.setCursor
Glut.CURSOR_INFO
1696 | Ulinkgoto
(page
, y) ->
1697 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
1698 Glut.setCursor
Glut.CURSOR_INFO
1700 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
1701 Glut.setCursor
Glut.CURSOR_TEXT
1711 if Sys.os_type
= "Win32"
1713 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1715 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1717 Filename.concat
home "llpp"
1721 let ic = open_in_bin
statepath in
1722 let hash = input_value
ic in
1728 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1735 match state.fullscreen
with
1736 | None
-> state.w, state.h
1739 let bookmarks = List.map
(fun (t
, l
, n, y) ->
1740 let y = float y *. float state.w in
1741 (t
, l
, n, truncate
y)) state.bookmarks
1743 Hashtbl.replace
pstate state.path
(bookmarks, w, h
);
1744 let oc = open_out_bin
statepath in
1745 output_value
oc pstate
1749 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1754 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1755 let bookmarks = List.map
(fun (t
, l
, n, y) ->
1756 let y = float y /. float statew
in
1757 (t
, l
, n, truncate
y)) statebookmarks
1761 state.bookmarks <- bookmarks;
1762 with Not_found
-> ()
1764 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1767 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1769 if String.length
state.path
= 0
1770 then (prerr_endline
"filename missing"; exit
1)
1775 let _ = Glut.init
Sys.argv
in
1776 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1777 let () = Glut.initWindowSize
state.w state.h
in
1778 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1781 if Sys.os_type
= "Unix"
1783 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1785 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1786 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1787 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1788 Unix.bind
sock addr;
1790 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1791 Unix.connect
csock addr;
1792 let ssock, _ = Unix.accept
sock in
1795 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1796 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1800 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1804 let () = Glut.displayFunc
display in
1805 let () = Glut.reshapeFunc
reshape in
1806 let () = Glut.keyboardFunc
keyboard in
1807 let () = Glut.specialFunc
special in
1808 let () = Glut.idleFunc
(Some
idle) in
1809 let () = Glut.mouseFunc
mouse in
1810 let () = Glut.motionFunc
motion in
1811 let () = Glut.passiveMotionFunc
pmotion in
1814 state.csock <- csock;
1815 state.ssock <- ssock;
1816 state.text <- "Opening " ^
name;
1817 writecmd csock ("open " ^
name ^
"\000");
1821 let rec handlelablglutbug () =
1824 with Glut.BadEnum
"key in special_of_int" ->
1825 showtext '
!'
" LablGlut bug: special key not recognized";
1826 handlelablglutbug ()
1828 handlelablglutbug ();