Do not leak memory when reloading document
[llpp.git] / main.ml
blobd7b3a1a8cb0f0634f8bba80d340a15a1e02bb4a4
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
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 * bool) -> string -> unit = "ml_draw";;
13 external seltext : string -> (int * int * int * int) -> int -> unit =
14 "ml_seltext";;
15 external copysel : string -> unit = "ml_copysel";;
16 external getpagewh : int -> float array = "ml_getpagewh";;
17 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
19 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
21 type 'a circbuf =
22 { store : 'a array
23 ; mutable rc : int
24 ; mutable wc : int
25 ; mutable len : int
29 type textentry = (char * string * onhist option * onkey * ondone)
30 and onkey = string -> int -> te
31 and ondone = string -> unit
32 and onhist = histcmd -> string
33 and histcmd = HCnext | HCprev | HCfirst | HClast
34 and te =
35 | TEstop
36 | TEdone of string
37 | TEcont of string
38 | TEswitch of textentry
41 let cbnew n v =
42 { store = Array.create n v
43 ; rc = 0
44 ; wc = 0
45 ; len = 0
49 let cblen b = Array.length b.store;;
51 let cbput b v =
52 let len = cblen b in
53 b.store.(b.wc) <- v;
54 b.wc <- (b.wc + 1) mod len;
55 b.len <- min (b.len + 1) len;
58 let cbpeekw b = b.store.(b.wc);;
60 let cbget b dir =
61 if b.len = 0 then b.store.(0) else
62 let rc = b.rc + dir in
63 let rc = if rc = -1 then b.len - 1 else rc in
64 let rc = if rc = b.len then 0 else rc in
65 b.rc <- rc;
66 b.store.(rc);
69 let cbrfollowlen b =
70 b.rc <- b.len;
73 let cbclear b v =
74 b.len <- 0;
75 Array.fill b.store 0 (Array.length b.store) v;
78 type layout =
79 { pageno : int
80 ; pagedimno : int
81 ; pagew : int
82 ; pageh : int
83 ; pagedispy : int
84 ; pagey : int
85 ; pagevh : int
89 type conf =
90 { mutable scrollw : int
91 ; mutable scrollh : int
92 ; mutable icase : bool
93 ; mutable preload : bool
94 ; mutable pagebias : int
95 ; mutable verbose : bool
96 ; mutable scrollincr : int
97 ; mutable maxhfit : bool
98 ; mutable crophack : bool
99 ; mutable autoscroll : bool
100 ; mutable showall : bool
101 ; mutable hlinks : bool
102 ; mutable underinfo : bool
106 type outline = string * int * int * float;;
107 type outlines =
108 | Oarray of outline array
109 | Olist of outline list
110 | Onarrow of outline array * outline array
113 type rect = (float * float * float * float * float * float * float * float);;
115 type state =
116 { mutable csock : Unix.file_descr
117 ; mutable ssock : Unix.file_descr
118 ; mutable w : int
119 ; mutable h : int
120 ; mutable rotate : int
121 ; mutable y : int
122 ; mutable ty : float
123 ; mutable maxy : int
124 ; mutable layout : layout list
125 ; pagemap : ((int * int * int), string) Hashtbl.t
126 ; mutable pages : (int * int * int) list
127 ; mutable pagecount : int
128 ; pagecache : string circbuf
129 ; mutable rendering : bool
130 ; mutable mstate : mstate
131 ; mutable searchpattern : string
132 ; mutable rects : (int * int * rect) list
133 ; mutable rects1 : (int * int * rect) list
134 ; mutable text : string
135 ; mutable fullscreen : (int * int) option
136 ; mutable textentry : textentry option
137 ; mutable outlines : outlines
138 ; mutable outline : (bool * int * int * outline array * string) option
139 ; mutable bookmarks : outline list
140 ; mutable path : string
141 ; mutable invalidated : int
142 ; mutable colorscale : float
143 ; hists : hists
145 and hists =
146 { pat : string circbuf
147 ; pag : string circbuf
148 ; nav : float circbuf
152 let conf =
153 { scrollw = 5
154 ; scrollh = 12
155 ; icase = true
156 ; preload = true
157 ; pagebias = 0
158 ; verbose = false
159 ; scrollincr = 24
160 ; maxhfit = true
161 ; crophack = false
162 ; autoscroll = false
163 ; showall = false
164 ; hlinks = false
165 ; underinfo = false
169 let state =
170 { csock = Unix.stdin
171 ; ssock = Unix.stdin
172 ; w = 900
173 ; h = 900
174 ; rotate = 0
175 ; y = 0
176 ; ty = 0.0
177 ; layout = []
178 ; maxy = max_int
179 ; pagemap = Hashtbl.create 10
180 ; pagecache = cbnew 10 ""
181 ; pages = []
182 ; pagecount = 0
183 ; rendering = false
184 ; mstate = Mnone
185 ; rects = []
186 ; rects1 = []
187 ; text = ""
188 ; fullscreen = None
189 ; textentry = None
190 ; searchpattern = ""
191 ; outlines = Olist []
192 ; outline = None
193 ; bookmarks = []
194 ; path = ""
195 ; invalidated = 0
196 ; hists =
197 { nav = cbnew 100 0.0
198 ; pat = cbnew 20 ""
199 ; pag = cbnew 10 ""
201 ; colorscale = 1.0
205 let vlog fmt =
206 if conf.verbose
207 then
208 Printf.kprintf prerr_endline fmt
209 else
210 Printf.kprintf ignore fmt
213 let writecmd fd s =
214 let len = String.length s in
215 let n = 4 + len in
216 let b = Buffer.create n in
217 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
218 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
219 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
220 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
221 Buffer.add_string b s;
222 let s' = Buffer.contents b in
223 let n' = Unix.write fd s' 0 n in
224 if n' != n then failwith "write failed";
227 let readcmd fd =
228 let s = "xxxx" in
229 let n = Unix.read fd s 0 4 in
230 if n != 4 then failwith "incomplete read(len)";
231 let len = 0
232 lor (Char.code s.[0] lsl 24)
233 lor (Char.code s.[1] lsl 16)
234 lor (Char.code s.[2] lsl 8)
235 lor (Char.code s.[3] lsl 0)
237 let s = String.create len in
238 let n = Unix.read fd s 0 len in
239 if n != len then failwith "incomplete read(data)";
243 let yratio y =
244 if y = state.maxy then 1.0
245 else float y /. float state.maxy
248 let makecmd s l =
249 let b = Buffer.create 10 in
250 Buffer.add_string b s;
251 let rec combine = function
252 | [] -> b
253 | x :: xs ->
254 Buffer.add_char b ' ';
255 let s =
256 match x with
257 | `b b -> if b then "1" else "0"
258 | `s s -> s
259 | `i i -> string_of_int i
260 | `f f -> string_of_float f
261 | `I f -> string_of_int (truncate f)
263 Buffer.add_string b s;
264 combine xs;
266 combine l;
269 let wcmd s l =
270 let cmd = Buffer.contents (makecmd s l) in
271 writecmd state.csock cmd;
274 let calcheight () =
275 let rec f pn ph fh l =
276 match l with
277 | (n, _, h) :: rest ->
278 let fh = fh + (n - pn) * ph in
279 f n h fh rest
281 | [] ->
282 let fh = fh + (ph * (state.pagecount - pn)) in
283 max 0 fh
285 let fh = f 0 0 0 state.pages in
289 let getpageyh pageno =
290 let rec f pn ph y l =
291 match l with
292 | (n, _, h) :: rest ->
293 if n >= pageno
294 then
295 y + (pageno - pn) * ph, h
296 else
297 let y = y + (n - pn) * ph in
298 f n h y rest
300 | [] ->
301 y + (pageno - pn) * ph, ph
303 f 0 0 0 state.pages;
306 let getpagey pageno = fst (getpageyh pageno);;
308 let layout y sh =
309 let rec f pageno pdimno prev vy py dy l cacheleft accu =
310 if pageno = state.pagecount || cacheleft = 0
311 then accu
312 else
313 let ((_, w, h) as curr), rest, pdimno =
314 match l with
315 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
316 curr, rest, pdimno + 1
317 | _ ->
318 prev, l, pdimno
320 let pageno' = pageno + 1 in
321 if py + h > vy
322 then
323 let py' = vy - py in
324 let vh = h - py' in
325 if dy + vh > sh
326 then
327 let vh = sh - dy in
328 if vh <= 0
329 then
330 accu
331 else
332 let e =
333 { pageno = pageno
334 ; pagedimno = pdimno
335 ; pagew = w
336 ; pageh = h
337 ; pagedispy = dy
338 ; pagey = py'
339 ; pagevh = vh
342 e :: accu
343 else
344 let e =
345 { pageno = pageno
346 ; pagedimno = pdimno
347 ; pagew = w
348 ; pageh = h
349 ; pagedispy = dy
350 ; pagey = py'
351 ; pagevh = vh
354 let accu = e :: accu in
355 f pageno' pdimno curr
356 (vy + vh) (py + h) (dy + vh + 2) rest
357 (pred cacheleft) accu
358 else
359 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
361 if state.invalidated = 0
362 then
363 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
364 state.maxy <- calcheight ();
365 List.rev accu
366 else
370 let clamp incr =
371 let y = state.y + incr in
372 let y = max 0 y in
373 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
377 let getopaque pageno =
378 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw,
379 state.rotate))
380 with Not_found -> None
383 let cache pageno opaque =
384 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw,
385 state.rotate) opaque
388 let validopaque opaque = String.length opaque > 0;;
390 let render l =
391 match getopaque l.pageno with
392 | None when not state.rendering ->
393 state.rendering <- true;
394 cache l.pageno "";
395 wcmd "render" [`i (l.pageno + 1)
396 ;`i l.pagedimno
397 ;`i l.pagew
398 ;`i l.pageh];
400 | _ -> ()
403 let loadlayout layout =
404 let rec f all = function
405 | l :: ls ->
406 begin match getopaque l.pageno with
407 | None -> render l; f false ls
408 | Some opaque -> f (all && validopaque opaque) ls
410 | [] -> all
412 f (layout <> []) layout;
415 let preload () =
416 if conf.preload then
417 let evictedvisible =
418 let evictedopaque = cbpeekw state.pagecache in
419 List.exists (fun l ->
420 match getopaque l.pageno with
421 | Some opaque when validopaque opaque ->
422 evictedopaque = opaque
423 | otherwise -> false
424 ) state.layout
426 if not evictedvisible then
427 let y = if state.y < state.h then 0 else state.y - state.h in
428 let pages = layout y (state.h*3) in
429 List.iter render pages;
432 let gotoy y =
433 let y = max 0 y in
434 let y = min state.maxy y in
435 let pages = layout y state.h in
436 let ready = loadlayout pages in
437 state.ty <- yratio y;
438 if conf.showall then (
439 if ready then (
440 state.layout <- pages;
441 state.y <- y;
442 Glut.postRedisplay ();
445 else (
446 state.layout <- pages;
447 state.y <- y;
448 Glut.postRedisplay ();
450 preload ();
453 let addnav () =
454 cbput state.hists.nav (yratio state.y);
455 cbrfollowlen state.hists.nav;
458 let getnav () =
459 let y = cbget state.hists.nav ~-1 in
460 truncate (y *. float state.maxy)
463 let gotopage n top =
464 let y, h = getpageyh n in
465 addnav ();
466 gotoy (y + (truncate (top *. float h)));
469 let gotopage1 n top =
470 let y = getpagey n in
471 addnav ();
472 gotoy (y + top);
475 let invalidate () =
476 state.layout <- [];
477 state.pages <- [];
478 state.rects <- [];
479 state.rects1 <- [];
480 state.invalidated <- state.invalidated + 1;
483 let scalecolor c =
484 let c = c *. state.colorscale in
485 (c, c, c);
488 let reshape ~w ~h =
489 state.w <- w;
490 state.h <- h;
491 GlDraw.viewport 0 0 w h;
492 GlMat.mode `modelview;
493 GlMat.load_identity ();
494 GlMat.mode `projection;
495 GlMat.load_identity ();
496 GlMat.rotate ~x:1.0 ~angle:180.0 ();
497 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
498 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
499 GlClear.color (scalecolor 1.0);
500 GlClear.clear [`color];
502 invalidate ();
503 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
506 let showtext c s =
507 GlDraw.color (0.0, 0.0, 0.0);
508 GlDraw.rect
509 (0.0, float (state.h - 18))
510 (float (state.w - conf.scrollw - 1), float state.h)
512 let font = Glut.BITMAP_8_BY_13 in
513 GlDraw.color (1.0, 1.0, 1.0);
514 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
515 Glut.bitmapCharacter ~font ~c:(Char.code c);
516 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
519 let enttext () =
520 let len = String.length state.text in
521 match state.textentry with
522 | None ->
523 if len > 0 then showtext ' ' state.text
525 | Some (c, text, _, _, _) ->
526 let s =
527 if len > 0
528 then
529 text ^ " [" ^ state.text ^ "]"
530 else
531 text
533 showtext c s;
536 let showtext c s =
537 if true
538 then (
539 state.text <- Printf.sprintf "%c%s" c s;
540 Glut.postRedisplay ();
542 else (
543 showtext c s;
544 Glut.swapBuffers ();
548 let act cmd =
549 match cmd.[0] with
550 | 'c' ->
551 state.pages <- [];
553 | 'D' ->
554 state.rects <- state.rects1;
555 Glut.postRedisplay ()
557 | 'C' ->
558 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
559 state.pagecount <- n;
560 state.invalidated <- state.invalidated - 1;
561 if state.invalidated = 0
562 then (
563 let rely = yratio state.y in
564 state.maxy <- calcheight ();
565 gotoy (truncate (float state.maxy *. rely));
568 | 't' ->
569 let s = Scanf.sscanf cmd "t %n"
570 (fun n -> String.sub cmd n (String.length cmd - n))
572 Glut.setWindowTitle s
574 | 'T' ->
575 let s = Scanf.sscanf cmd "T %n"
576 (fun n -> String.sub cmd n (String.length cmd - n))
578 if state.textentry = None
579 then (
580 state.text <- s;
581 showtext ' ' s;
583 else (
584 state.text <- s;
585 Glut.postRedisplay ();
588 | 'V' ->
589 if conf.verbose
590 then
591 let s = Scanf.sscanf cmd "V %n"
592 (fun n -> String.sub cmd n (String.length cmd - n))
594 state.text <- s;
595 showtext ' ' s;
597 | 'F' ->
598 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
599 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
600 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
601 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
603 let y = (getpagey pageno) + truncate y0 in
604 addnav ();
605 gotoy y;
606 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
608 | 'R' ->
609 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
610 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
611 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
612 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
614 state.rects1 <-
615 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
617 | 'r' ->
618 let n, w, h, r, p =
619 Scanf.sscanf cmd "r %d %d %d %d %s"
620 (fun n w h r p -> (n, w, h, r, p))
622 Hashtbl.replace state.pagemap (n, w, r) p;
623 let opaque = cbpeekw state.pagecache in
624 if validopaque opaque
625 then (
626 let k =
627 Hashtbl.fold
628 (fun k v a -> if v = opaque then k else a)
629 state.pagemap (-1, -1, -1)
631 wcmd "free" [`s opaque];
632 Hashtbl.remove state.pagemap k
634 cbput state.pagecache p;
635 state.rendering <- false;
636 if conf.showall
637 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
638 else (
639 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
640 if visible then gotoy state.y
641 else (ignore (loadlayout state.layout); preload ())
644 | 'l' ->
645 let (n, w, h) as pagelayout =
646 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
648 state.pages <- pagelayout :: state.pages
650 | 'o' ->
651 let (l, n, t, h, pos) =
652 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
654 let s = String.sub cmd pos (String.length cmd - pos) in
655 let s =
656 let l = String.length s in
657 let b = Buffer.create (String.length s) in
658 let rec loop pc2 i =
659 if i = l then () else
660 let pc2 =
661 match s.[i] with
662 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
663 | '\xc2' -> true
664 | c ->
665 let c = if Char.code c land 0x80 = 0 then c else '?' in
666 Buffer.add_char b c;
667 false
669 loop pc2 (i+1)
671 loop false 0;
672 Buffer.contents b
674 let outline = (s, l, n, float t /. float h) in
675 let outlines =
676 match state.outlines with
677 | Olist outlines -> Olist (outline :: outlines)
678 | Oarray _ -> Olist [outline]
679 | Onarrow _ -> Olist [outline]
681 state.outlines <- outlines
683 | _ ->
684 log "unknown cmd `%S'" cmd
687 let now = Unix.gettimeofday;;
689 let idle () =
690 let rec loop delay =
691 let r, _, _ = Unix.select [state.csock] [] [] delay in
692 begin match r with
693 | [] ->
694 if conf.autoscroll then begin
695 let y = state.y + conf.scrollincr in
696 let y = if y >= state.maxy then 0 else y in
697 gotoy y;
698 state.text <- "";
699 end;
701 | _ ->
702 let cmd = readcmd state.csock in
703 act cmd;
704 loop 0.0
705 end;
706 in loop 0.001
709 let onhist cb = function
710 | HCprev -> cbget cb ~-1
711 | HCnext -> cbget cb 1
712 | HCfirst -> cbget cb ~-(cb.rc)
713 | HClast -> cbget cb (cb.len - 1 - cb.rc)
716 let search pattern forward =
717 if String.length pattern > 0
718 then
719 let pn, py =
720 match state.layout with
721 | [] -> 0, 0
722 | l :: _ ->
723 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
725 let cmd =
726 let b = makecmd "search"
727 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
729 Buffer.add_char b ',';
730 Buffer.add_string b pattern;
731 Buffer.add_char b '\000';
732 Buffer.contents b;
734 writecmd state.csock cmd;
737 let intentry text key =
738 let c = Char.unsafe_chr key in
739 match c with
740 | '0' .. '9' ->
741 let s = "x" in s.[0] <- c;
742 let text = text ^ s in
743 TEcont text
745 | _ ->
746 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
747 TEcont text
750 let addchar s c =
751 let b = Buffer.create (String.length s + 1) in
752 Buffer.add_string b s;
753 Buffer.add_char b c;
754 Buffer.contents b;
757 let textentry text key =
758 let c = Char.unsafe_chr key in
759 match c with
760 | _ when key >= 32 && key < 127 ->
761 let text = addchar text c in
762 TEcont text
764 | _ ->
765 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
766 TEcont text
769 let rotate angle =
770 state.rotate <- angle;
771 invalidate ();
772 wcmd "rotate" [`i angle];
775 let optentry text key =
776 let btos b = if b then "on" else "off" in
777 let c = Char.unsafe_chr key in
778 match c with
779 | 's' ->
780 let ondone s =
781 try conf.scrollincr <- int_of_string s with exc ->
782 state.text <- Printf.sprintf "bad integer `%s': %s"
783 s (Printexc.to_string exc)
785 TEswitch ('#', "", None, intentry, ondone)
787 | 'R' ->
788 let ondone s =
789 match try
790 Some (int_of_string s)
791 with exc ->
792 state.text <- Printf.sprintf "bad integer `%s': %s"
793 s (Printexc.to_string exc);
794 None
795 with
796 | Some angle -> rotate angle
797 | None -> ()
799 TEswitch ('^', "", None, intentry, ondone)
801 | 'i' ->
802 conf.icase <- not conf.icase;
803 TEdone ("case insensitive search " ^ (btos conf.icase))
805 | 'p' ->
806 conf.preload <- not conf.preload;
807 gotoy state.y;
808 TEdone ("preload " ^ (btos conf.preload))
810 | 'v' ->
811 conf.verbose <- not conf.verbose;
812 TEdone ("verbose " ^ (btos conf.verbose))
814 | 'h' ->
815 conf.maxhfit <- not conf.maxhfit;
816 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
817 TEdone ("maxhfit " ^ (btos conf.maxhfit))
819 | 'c' ->
820 conf.crophack <- not conf.crophack;
821 TEdone ("crophack " ^ btos conf.crophack)
823 | 'a' ->
824 conf.showall <- not conf.showall;
825 TEdone ("showall " ^ btos conf.showall)
827 | 'f' ->
828 conf.underinfo <- not conf.underinfo;
829 TEdone ("underinfo " ^ btos conf.underinfo)
831 | _ ->
832 state.text <- Printf.sprintf "bad option %d `%c'" key c;
833 TEstop
836 let maxoutlinerows () = (state.h - 31) / 16;;
838 let enterselector allowdel outlines errmsg =
839 if Array.length outlines = 0
840 then (
841 showtext ' ' errmsg;
843 else (
844 Glut.setCursor Glut.CURSOR_INHERIT;
845 let pageno =
846 match state.layout with
847 | [] -> -1
848 | {pageno=pageno} :: rest -> pageno
850 let active =
851 let rec loop n =
852 if n = Array.length outlines
853 then 0
854 else
855 let (_, _, outlinepageno, _) = outlines.(n) in
856 if outlinepageno >= pageno then n else loop (n+1)
858 loop 0
860 state.outline <-
861 Some (allowdel, active,
862 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
863 Glut.postRedisplay ();
867 let enteroutlinemode () =
868 let outlines =
869 match state.outlines with
870 | Oarray a -> a
871 | Olist l ->
872 let a = Array.of_list (List.rev l) in
873 state.outlines <- Oarray a;
875 | Onarrow (a, b) -> a
877 enterselector false outlines "Document has no outline";
880 let enterbookmarkmode () =
881 let bookmarks = Array.of_list state.bookmarks in
882 enterselector true bookmarks "Document has no bookmarks (yet)";
886 let quickbookmark ?title () =
887 match state.layout with
888 | [] -> ()
889 | l :: _ ->
890 let title =
891 match title with
892 | None ->
893 let sec = Unix.gettimeofday () in
894 let tm = Unix.localtime sec in
895 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
896 l.pageno
897 tm.Unix.tm_mday
898 tm.Unix.tm_mon
899 (tm.Unix.tm_year + 1900)
900 tm.Unix.tm_hour
901 tm.Unix.tm_min
902 | Some title -> title
904 state.bookmarks <-
905 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
908 let doreshape w h =
909 state.fullscreen <- None;
910 Glut.reshapeWindow w h;
913 let opendoc path =
914 invalidate ();
915 state.path <- path;
916 Hashtbl.iter
917 (fun k v -> if validopaque v then wcmd "drop" [`s v]) state.pagemap;
918 Hashtbl.clear state.pagemap;
919 cbclear state.pagecache "";
921 writecmd state.csock ("open " ^ path ^ "\000");
922 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
923 wcmd "geometry" [`i (state.w - conf.scrollw); `i state.h];
926 let viewkeyboard ~key ~x ~y =
927 let enttext te =
928 state.textentry <- te;
929 state.text <- "";
930 enttext ();
931 Glut.postRedisplay ()
933 match state.textentry with
934 | None ->
935 let c = Char.chr key in
936 begin match c with
937 | '\027' | 'q' ->
938 exit 0
940 | '\008' ->
941 let y = getnav () in
942 gotoy y
944 | 'o' ->
945 enteroutlinemode ()
947 | 'u' ->
948 state.rects <- [];
949 state.text <- "";
950 Glut.postRedisplay ()
952 | '/' | '?' ->
953 let ondone isforw s =
954 cbput state.hists.pat s;
955 cbrfollowlen state.hists.pat;
956 state.searchpattern <- s;
957 search s isforw
959 enttext (Some (c, "", Some (onhist state.hists.pat),
960 textentry, ondone (c ='/')))
962 | '+' ->
963 let ondone s =
964 let n =
965 try int_of_string s with exc ->
966 state.text <- Printf.sprintf "bad integer `%s': %s"
967 s (Printexc.to_string exc);
968 max_int
970 if n != max_int
971 then (
972 conf.pagebias <- n;
973 state.text <- "page bias is now " ^ string_of_int n;
976 enttext (Some ('+', "", None, intentry, ondone))
978 | '-' ->
979 let ondone msg =
980 state.text <- msg;
982 enttext (Some ('-', "", None, optentry, ondone))
984 | '0' .. '9' ->
985 let ondone s =
986 let n =
987 try int_of_string s with exc ->
988 state.text <- Printf.sprintf "bad integer `%s': %s"
989 s (Printexc.to_string exc);
992 if n >= 0
993 then (
994 addnav ();
995 cbput state.hists.pag (string_of_int n);
996 cbrfollowlen state.hists.pag;
997 gotoy (getpagey (n + conf.pagebias - 1))
1000 let pageentry text key =
1001 match Char.unsafe_chr key with
1002 | 'g' -> TEdone text
1003 | _ -> intentry text key
1005 let text = "x" in text.[0] <- c;
1006 enttext (Some (':', text, Some (onhist state.hists.pag),
1007 pageentry, ondone))
1009 | 'b' ->
1010 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
1011 reshape state.w state.h;
1013 | 'l' ->
1014 conf.hlinks <- not conf.hlinks;
1015 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1016 Glut.postRedisplay ()
1018 | 'a' ->
1019 conf.autoscroll <- not conf.autoscroll
1021 | 'f' ->
1022 begin match state.fullscreen with
1023 | None ->
1024 state.fullscreen <- Some (state.w, state.h);
1025 Glut.fullScreen ()
1026 | Some (w, h) ->
1027 state.fullscreen <- None;
1028 doreshape w h
1031 | 'g' ->
1032 gotoy 0
1034 | 'n' ->
1035 search state.searchpattern true
1037 | 'p' | 'N' ->
1038 search state.searchpattern false
1040 | 't' ->
1041 begin match state.layout with
1042 | [] -> ()
1043 | l :: _ ->
1044 gotoy (state.y - l.pagey);
1047 | ' ' ->
1048 begin match List.rev state.layout with
1049 | [] -> ()
1050 | l :: _ ->
1051 gotoy (clamp (l.pageh - l.pagey))
1054 | '\127' ->
1055 begin match state.layout with
1056 | [] -> ()
1057 | l :: _ ->
1058 gotoy (clamp (-l.pageh));
1061 | '=' ->
1062 let f (fn, ln) l =
1063 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1065 let fn, ln = List.fold_left f (-1, -1) state.layout in
1066 let s =
1067 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1068 let percent =
1069 if maxy <= 0
1070 then 100.
1071 else (100. *. (float state.y /. float maxy)) in
1072 if fn = ln
1073 then
1074 Printf.sprintf "Page %d of %d %.2f%%"
1075 (fn+1) state.pagecount percent
1076 else
1077 Printf.sprintf
1078 "Pages %d-%d of %d %.2f%%"
1079 (fn+1) (ln+1) state.pagecount percent
1081 showtext ' ' s;
1083 | 'w' ->
1084 begin match state.layout with
1085 | [] -> ()
1086 | l :: _ ->
1087 doreshape (l.pagew + conf.scrollw) l.pageh;
1088 Glut.postRedisplay ();
1091 | '\'' ->
1092 enterbookmarkmode ()
1094 | 'm' ->
1095 let ondone s =
1096 match state.layout with
1097 | l :: _ ->
1098 state.bookmarks <-
1099 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1100 :: state.bookmarks
1101 | _ -> ()
1103 enttext (Some ('~', "", None, textentry, ondone))
1105 | '~' ->
1106 quickbookmark ();
1107 showtext ' ' "Quick bookmark added";
1109 | 'z' ->
1110 begin match state.layout with
1111 | l :: _ ->
1112 let a = getpagewh l.pagedimno in
1113 let w, h =
1114 if conf.crophack
1115 then
1116 (truncate (1.8 *. (a.(1) -. a.(0))),
1117 truncate (1.2 *. (a.(3) -. a.(0))))
1118 else
1119 (truncate (a.(1) -. a.(0)),
1120 truncate (a.(3) -. a.(0)))
1122 doreshape (w + conf.scrollw) h;
1123 Glut.postRedisplay ();
1125 | [] -> ()
1128 | '<' | '>' ->
1129 rotate (state.rotate + (if c = '>' then 30 else -30));
1131 | '[' | ']' ->
1132 state.colorscale <-
1133 max 0.0
1134 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1135 Glut.postRedisplay ()
1137 | 'k' -> gotoy (clamp (-conf.scrollincr))
1138 | 'j' -> gotoy (clamp conf.scrollincr)
1140 | 'r' -> opendoc state.path
1142 | _ ->
1143 vlog "huh? %d %c" key (Char.chr key);
1146 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1147 let len = String.length text in
1148 if len = 0
1149 then (
1150 state.textentry <- None;
1151 Glut.postRedisplay ();
1153 else (
1154 let s = String.sub text 0 (len - 1) in
1155 enttext (Some (c, s, onhist, onkey, ondone))
1158 | Some (c, text, onhist, onkey, ondone) ->
1159 begin match Char.unsafe_chr key with
1160 | '\r' | '\n' ->
1161 ondone text;
1162 state.textentry <- None;
1163 Glut.postRedisplay ()
1165 | '\027' ->
1166 state.textentry <- None;
1167 Glut.postRedisplay ()
1169 | _ ->
1170 begin match onkey text key with
1171 | TEdone text ->
1172 state.textentry <- None;
1173 ondone text;
1174 Glut.postRedisplay ()
1176 | TEcont text ->
1177 enttext (Some (c, text, onhist, onkey, ondone));
1179 | TEstop ->
1180 state.textentry <- None;
1181 Glut.postRedisplay ()
1183 | TEswitch te ->
1184 state.textentry <- Some te;
1185 Glut.postRedisplay ()
1186 end;
1187 end;
1190 let narrow outlines pattern =
1191 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1192 match reopt with
1193 | None -> None
1194 | Some re ->
1195 let rec fold accu n =
1196 if n = -1 then accu else
1197 let (s, _, _, _) as o = outlines.(n) in
1198 let accu =
1199 if (try ignore (Str.search_forward re s 0); true
1200 with Not_found -> false)
1201 then (o :: accu)
1202 else accu
1204 fold accu (n-1)
1206 let matched = fold [] (Array.length outlines - 1) in
1207 if matched = [] then None else Some (Array.of_list matched)
1210 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1211 let search active pattern incr =
1212 let dosearch re =
1213 let rec loop n =
1214 if n = Array.length outlines || n = -1 then None else
1215 let (s, _, _, _) = outlines.(n) in
1217 (try ignore (Str.search_forward re s 0); true
1218 with Not_found -> false)
1219 then Some n
1220 else loop (n + incr)
1222 loop active
1225 let re = Str.regexp_case_fold pattern in
1226 dosearch re
1227 with Failure s ->
1228 state.text <- s;
1229 None
1231 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1232 match key with
1233 | 27 ->
1234 if String.length qsearch = 0
1235 then (
1236 state.text <- "";
1237 state.outline <- None;
1238 Glut.postRedisplay ();
1240 else (
1241 state.text <- "";
1242 state.outline <- Some (allowdel, active, first, outlines, "");
1243 Glut.postRedisplay ();
1246 | 18 | 19 ->
1247 let incr = if key = 18 then -1 else 1 in
1248 let active, first =
1249 match search (active + incr) qsearch incr with
1250 | None ->
1251 state.text <- qsearch ^ " [not found]";
1252 active, first
1253 | Some active ->
1254 state.text <- qsearch;
1255 active, firstof active
1257 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1258 Glut.postRedisplay ();
1260 | 8 ->
1261 let len = String.length qsearch in
1262 if len = 0
1263 then ()
1264 else (
1265 if len = 1
1266 then (
1267 state.text <- "";
1268 state.outline <- Some (allowdel, active, first, outlines, "");
1270 else
1271 let qsearch = String.sub qsearch 0 (len - 1) in
1272 let active, first =
1273 match search active qsearch ~-1 with
1274 | None ->
1275 state.text <- qsearch ^ " [not found]";
1276 active, first
1277 | Some active ->
1278 state.text <- qsearch;
1279 active, firstof active
1281 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1283 Glut.postRedisplay ()
1285 | 13 ->
1286 if active < Array.length outlines
1287 then (
1288 let (_, _, n, t) = outlines.(active) in
1289 gotopage n t;
1291 state.text <- "";
1292 if allowdel then state.bookmarks <- Array.to_list outlines;
1293 state.outline <- None;
1294 Glut.postRedisplay ();
1296 | _ when key >= 32 && key < 127 ->
1297 let pattern = addchar qsearch (Char.chr key) in
1298 let active, first =
1299 match search active pattern 1 with
1300 | None ->
1301 state.text <- pattern ^ " [not found]";
1302 active, first
1303 | Some active ->
1304 state.text <- pattern;
1305 active, firstof active
1307 state.outline <- Some (allowdel, active, first, outlines, pattern);
1308 Glut.postRedisplay ()
1310 | 14 when not allowdel ->
1311 let optoutlines = narrow outlines qsearch in
1312 begin match optoutlines with
1313 | None -> state.text <- "can't narrow"
1314 | Some outlines ->
1315 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1316 match state.outlines with
1317 | Olist l -> ()
1318 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1319 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1320 end;
1321 Glut.postRedisplay ()
1323 | 21 when not allowdel ->
1324 let outline =
1325 match state.outlines with
1326 | Oarray a -> a
1327 | Olist l ->
1328 let a = Array.of_list (List.rev l) in
1329 state.outlines <- Oarray a;
1331 | Onarrow (a, b) ->
1332 state.outlines <- Oarray b;
1335 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1336 Glut.postRedisplay ()
1338 | 12 ->
1339 state.outline <-
1340 Some (allowdel, active, firstof active, outlines, qsearch);
1341 Glut.postRedisplay ()
1343 | 127 when allowdel ->
1344 let len = Array.length outlines - 1 in
1345 if len = 0
1346 then (
1347 state.outline <- None;
1348 state.bookmarks <- [];
1350 else (
1351 let bookmarks = Array.init len
1352 (fun i ->
1353 let i = if i >= active then i + 1 else i in
1354 outlines.(i)
1357 state.outline <-
1358 Some (allowdel,
1359 min active (len-1),
1360 min first (len-1),
1361 bookmarks, qsearch)
1364 Glut.postRedisplay ()
1366 | _ -> log "unknown key %d" key
1369 let keyboard ~key ~x ~y =
1370 if key = 7
1371 then
1372 wcmd "interrupt" []
1373 else
1374 match state.outline with
1375 | None -> viewkeyboard ~key ~x ~y
1376 | Some outline -> outlinekeyboard ~key ~x ~y outline
1379 let special ~key ~x ~y =
1380 match state.outline with
1381 | None ->
1382 begin match state.textentry with
1383 | None ->
1384 let y =
1385 match key with
1386 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1387 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1388 | Glut.KEY_DOWN -> clamp conf.scrollincr
1389 | Glut.KEY_PAGE_UP ->
1390 if Glut.getModifiers () land Glut.active_ctrl != 0
1391 then
1392 match state.layout with
1393 | [] -> state.y
1394 | l :: _ -> state.y - l.pagey
1395 else
1396 clamp (-state.h)
1397 | Glut.KEY_PAGE_DOWN ->
1398 if Glut.getModifiers () land Glut.active_ctrl != 0
1399 then
1400 match List.rev state.layout with
1401 | [] -> state.y
1402 | l :: _ -> getpagey l.pageno
1403 else
1404 clamp state.h
1405 | Glut.KEY_HOME -> addnav (); 0
1406 | Glut.KEY_END ->
1407 addnav ();
1408 state.maxy - (if conf.maxhfit then state.h else 0)
1409 | _ -> state.y
1411 if not conf.verbose then state.text <- "";
1412 gotoy y
1414 | Some (c, s, Some onhist, onkey, ondone) ->
1415 let s =
1416 match key with
1417 | Glut.KEY_UP -> onhist HCprev
1418 | Glut.KEY_DOWN -> onhist HCnext
1419 | Glut.KEY_HOME -> onhist HCfirst
1420 | Glut.KEY_END -> onhist HClast
1421 | _ -> state.text
1423 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1424 Glut.postRedisplay ()
1426 | _ -> ()
1429 | Some (allowdel, active, first, outlines, qsearch) ->
1430 let maxrows = maxoutlinerows () in
1431 let navigate incr =
1432 let active = active + incr in
1433 let active = max 0 (min active (Array.length outlines - 1)) in
1434 let first =
1435 if active > first
1436 then
1437 let rows = active - first in
1438 if rows > maxrows then active - maxrows else first
1439 else active
1441 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1442 Glut.postRedisplay ()
1444 match key with
1445 | Glut.KEY_UP -> navigate ~-1
1446 | Glut.KEY_DOWN -> navigate 1
1447 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1448 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1450 | Glut.KEY_HOME ->
1451 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1452 Glut.postRedisplay ()
1454 | Glut.KEY_END ->
1455 let active = Array.length outlines - 1 in
1456 let first = max 0 (active - maxrows) in
1457 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1458 Glut.postRedisplay ()
1460 | _ -> ()
1463 let drawplaceholder l =
1464 GlDraw.color (scalecolor 1.0);
1465 GlDraw.rect
1466 (0.0, float l.pagedispy)
1467 (float l.pagew, float (l.pagedispy + l.pagevh))
1469 let x = 0.0
1470 and y = float (l.pagedispy + 13) in
1471 let font = Glut.BITMAP_8_BY_13 in
1472 GlDraw.color (0.0, 0.0, 0.0);
1473 GlPix.raster_pos ~x ~y ();
1474 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1475 ("Loading " ^ string_of_int l.pageno);
1478 let now () = Unix.gettimeofday ();;
1480 let drawpage i l =
1481 begin match getopaque l.pageno with
1482 | Some opaque when validopaque opaque ->
1483 if state.textentry = None
1484 then GlDraw.color (scalecolor 1.0)
1485 else GlDraw.color (scalecolor 0.4);
1486 let a = now () in
1487 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks) opaque;
1488 let b = now () in
1489 let d = b-.a in
1490 vlog "draw %f sec" d;
1492 | _ ->
1493 drawplaceholder l;
1494 end;
1495 GlDraw.color (0.5, 0.5, 0.5);
1496 GlDraw.rect
1497 (0., float i)
1498 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1500 l.pagedispy + l.pagevh;
1503 let scrollindicator () =
1504 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1505 GlDraw.color (0.64 , 0.64, 0.64);
1506 GlDraw.rect
1507 (float (state.w - conf.scrollw), 0.)
1508 (float state.w, float state.h)
1510 GlDraw.color (0.0, 0.0, 0.0);
1511 let sh = (float (maxy + state.h) /. float state.h) in
1512 let sh = float state.h /. sh in
1513 let sh = max sh (float conf.scrollh) in
1515 let percent =
1516 if state.y = state.maxy
1517 then 1.0
1518 else float state.y /. float maxy
1520 let position = (float state.h -. sh) *. percent in
1522 let position =
1523 if position +. sh > float state.h
1524 then
1525 float state.h -. sh
1526 else
1527 position
1529 GlDraw.rect
1530 (float (state.w - conf.scrollw), position)
1531 (float state.w, position +. sh)
1535 let showsel () =
1536 match state.mstate with
1537 | Mnone ->
1540 | Msel ((x0, y0), (x1, y1)) ->
1541 let rec loop = function
1542 | l :: ls ->
1543 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1544 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1545 then
1546 match getopaque l.pageno with
1547 | Some opaque when validopaque opaque ->
1548 let oy = -l.pagey + l.pagedispy in
1549 seltext opaque (x0, y0, x1, y1) oy;
1551 | _ -> ()
1552 else loop ls
1553 | [] -> ()
1555 loop state.layout
1558 let showrects () =
1559 Gl.enable `blend;
1560 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1561 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1562 List.iter
1563 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1564 List.iter (fun l ->
1565 if l.pageno = pageno
1566 then (
1567 let d = float (l.pagedispy - l.pagey) in
1568 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1569 GlDraw.begins `quads;
1571 GlDraw.vertex2 (x0, y0+.d);
1572 GlDraw.vertex2 (x1, y1+.d);
1573 GlDraw.vertex2 (x2, y2+.d);
1574 GlDraw.vertex2 (x3, y3+.d);
1576 GlDraw.ends ();
1578 ) state.layout
1579 ) state.rects
1581 Gl.disable `blend;
1584 let showoutline = function
1585 | None -> ()
1586 | Some (allowdel, active, first, outlines, qsearch) ->
1587 Gl.enable `blend;
1588 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1589 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1590 GlDraw.rect (0., 0.) (float state.w, float state.h);
1591 Gl.disable `blend;
1593 GlDraw.color (1., 1., 1.);
1594 let font = Glut.BITMAP_9_BY_15 in
1595 let draw_string x y s =
1596 GlPix.raster_pos ~x ~y ();
1597 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1599 let rec loop row =
1600 if row = Array.length outlines || (row - first) * 16 > state.h
1601 then ()
1602 else (
1603 let (s, l, _, _) = outlines.(row) in
1604 let y = (row - first) * 16 in
1605 let x = 5 + 15*l in
1606 if row = active
1607 then (
1608 Gl.enable `blend;
1609 GlDraw.polygon_mode `both `line;
1610 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1611 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1612 GlDraw.rect (0., float (y + 1))
1613 (float (state.w - conf.scrollw - 1), float (y + 18));
1614 GlDraw.polygon_mode `both `fill;
1615 Gl.disable `blend;
1616 GlDraw.color (1., 1., 1.);
1618 draw_string (float x) (float (y + 16)) s;
1619 loop (row+1)
1622 loop first
1625 let display () =
1626 let lasty = List.fold_left drawpage 0 (state.layout) in
1627 GlDraw.color (scalecolor 0.5);
1628 GlDraw.rect
1629 (0., float lasty)
1630 (float (state.w - conf.scrollw), float state.h)
1632 showrects ();
1633 scrollindicator ();
1634 showsel ();
1635 showoutline state.outline;
1636 enttext ();
1637 Glut.swapBuffers ();
1640 let getunder x y =
1641 let rec f = function
1642 | l :: rest ->
1643 begin match getopaque l.pageno with
1644 | Some opaque when validopaque opaque ->
1645 let y = y - l.pagedispy in
1646 if y > 0
1647 then
1648 let y = l.pagey + y in
1649 match whatsunder opaque x y with
1650 | Unone -> f rest
1651 | under -> under
1652 else
1653 f rest
1654 | _ ->
1655 f rest
1657 | [] -> Unone
1659 f state.layout
1662 let mouse ~button ~bstate ~x ~y =
1663 match button with
1664 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1665 let incr =
1666 if n = 3
1667 then
1668 -conf.scrollincr
1669 else
1670 conf.scrollincr
1672 let incr = incr * 2 in
1673 let y = clamp incr in
1674 gotoy y
1676 | Glut.LEFT_BUTTON when state.outline = None ->
1677 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1678 begin match dest with
1679 | Ulinkgoto (pageno, top) ->
1680 if pageno >= 0
1681 then
1682 gotopage1 pageno top
1684 | Ulinkuri s ->
1685 print_endline s
1687 | Unone when bstate = Glut.DOWN ->
1688 Glut.setCursor Glut.CURSOR_INHERIT;
1689 state.mstate <- Mnone
1691 | Unone | Utext _ ->
1692 if bstate = Glut.DOWN
1693 then (
1694 if state.rotate mod 360 = 0 then (
1695 state.mstate <- Msel ((x, y), (x, y));
1696 Glut.postRedisplay ()
1699 else (
1700 match state.mstate with
1701 | Mnone -> ()
1702 | Msel ((x0, y0), (x1, y1)) ->
1703 let f l =
1704 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1705 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1706 then
1707 match getopaque l.pageno with
1708 | Some opaque when validopaque opaque ->
1709 copysel opaque
1710 | _ -> ()
1712 List.iter f state.layout;
1713 copysel ""; (* ugly *)
1714 Glut.setCursor Glut.CURSOR_INHERIT;
1715 state.mstate <- Mnone;
1719 | _ ->
1722 let mouse ~button ~state ~x ~y = mouse button state x y;;
1724 let motion ~x ~y =
1725 if state.outline = None
1726 then
1727 match state.mstate with
1728 | Mnone -> ()
1729 | Msel (a, _) ->
1730 state.mstate <- Msel (a, (x, y));
1731 Glut.postRedisplay ()
1734 let pmotion ~x ~y =
1735 if state.outline = None
1736 then
1737 match state.mstate with
1738 | Mnone ->
1739 begin match getunder x y with
1740 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
1741 | Ulinkuri uri ->
1742 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1743 Glut.setCursor Glut.CURSOR_INFO
1744 | Ulinkgoto (page, y) ->
1745 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
1746 Glut.setCursor Glut.CURSOR_INFO
1747 | Utext s ->
1748 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1749 Glut.setCursor Glut.CURSOR_TEXT
1752 | Msel (a, _) ->
1756 let () =
1757 let statepath =
1758 let home =
1759 if Sys.os_type = "Win32"
1760 then
1761 try Sys.getenv "HOMEPATH" with Not_found -> ""
1762 else
1763 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1765 Filename.concat home "llpp"
1767 let pstate =
1769 let ic = open_in_bin statepath in
1770 let hash = input_value ic in
1771 close_in ic;
1772 hash
1773 with exn ->
1774 if false
1775 then
1776 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1778 Hashtbl.create 1
1780 let savestate () =
1782 let w, h =
1783 match state.fullscreen with
1784 | None -> state.w, state.h
1785 | Some wh -> wh
1787 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1788 let oc = open_out_bin statepath in
1789 output_value oc pstate
1790 with exn ->
1791 if false
1792 then
1793 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1796 let setstate () =
1798 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1799 state.w <- statew;
1800 state.h <- stateh;
1801 state.bookmarks <- statebookmarks;
1802 with Not_found -> ()
1803 | exn ->
1804 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1807 Arg.parse [] (fun s -> state.path <- s) "options:";
1808 let name =
1809 if String.length state.path = 0
1810 then (prerr_endline "filename missing"; exit 1)
1811 else state.path
1814 setstate ();
1815 let _ = Glut.init Sys.argv in
1816 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1817 let () = Glut.initWindowSize state.w state.h in
1818 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1820 let csock, ssock =
1821 if Sys.os_type = "Unix"
1822 then
1823 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1824 else
1825 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1826 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1827 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1828 Unix.bind sock addr;
1829 Unix.listen sock 1;
1830 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1831 Unix.connect csock addr;
1832 let ssock, _ = Unix.accept sock in
1833 Unix.close sock;
1834 let opts sock =
1835 Unix.setsockopt sock Unix.TCP_NODELAY true;
1836 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1838 opts ssock;
1839 opts csock;
1840 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1841 ssock, csock
1844 let () = Glut.displayFunc display in
1845 let () = Glut.reshapeFunc reshape in
1846 let () = Glut.keyboardFunc keyboard in
1847 let () = Glut.specialFunc special in
1848 let () = Glut.idleFunc (Some idle) in
1849 let () = Glut.mouseFunc mouse in
1850 let () = Glut.motionFunc motion in
1851 let () = Glut.passiveMotionFunc pmotion in
1853 init ssock;
1854 state.csock <- csock;
1855 state.ssock <- ssock;
1856 state.text <- "Opening " ^ name;
1857 writecmd csock ("open " ^ name ^ "\000");
1859 at_exit savestate;
1861 let rec handlelablglutbug () =
1863 Glut.mainLoop ();
1864 with Glut.BadEnum "key in special_of_int" ->
1865 showtext '!' " LablGlut bug: special key not recognized";
1866 handlelablglutbug ()
1868 handlelablglutbug ();