Reverse the meaning of Ctrl-+/-
[llpp.git] / main.ml
blob0f4e3e810eeb069624f9e04abdf403eccd8b9a54
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
62 then b.store.(0)
63 else
64 let rc = b.rc + dir in
65 let rc = if rc = -1 then b.len - 1 else rc in
66 let rc = if rc = b.len then 0 else rc in
67 b.rc <- rc;
68 b.store.(rc);
71 let cbrfollowlen b =
72 b.rc <- b.len;
75 let cbclear b v =
76 b.len <- 0;
77 Array.fill b.store 0 (Array.length b.store) v;
80 type layout =
81 { pageno : int
82 ; pagedimno : int
83 ; pagew : int
84 ; pageh : int
85 ; pagedispy : int
86 ; pagey : int
87 ; pagevh : int
91 type conf =
92 { mutable scrollw : int
93 ; mutable scrollh : int
94 ; mutable icase : bool
95 ; mutable preload : bool
96 ; mutable pagebias : int
97 ; mutable verbose : bool
98 ; mutable scrollincr : int
99 ; mutable maxhfit : bool
100 ; mutable crophack : bool
101 ; mutable autoscroll : bool
102 ; mutable showall : bool
103 ; mutable hlinks : bool
104 ; mutable underinfo : bool
105 ; mutable interpagespace : int
106 ; mutable margin : int
110 type outline = string * int * int * float;;
111 type outlines =
112 | Oarray of outline array
113 | Olist of outline list
114 | Onarrow of outline array * outline array
117 type rect = (float * float * float * float * float * float * float * float);;
119 type state =
120 { mutable csock : Unix.file_descr
121 ; mutable ssock : Unix.file_descr
122 ; mutable w : int
123 ; mutable h : int
124 ; mutable winw : int
125 ; mutable rotate : int
126 ; mutable y : int
127 ; mutable ty : float
128 ; mutable maxy : int
129 ; mutable layout : layout list
130 ; pagemap : ((int * int * int), string) Hashtbl.t
131 ; mutable pages : (int * int * int) list
132 ; mutable pagecount : int
133 ; pagecache : string circbuf
134 ; mutable rendering : bool
135 ; mutable mstate : mstate
136 ; mutable searchpattern : string
137 ; mutable rects : (int * int * rect) list
138 ; mutable rects1 : (int * int * rect) list
139 ; mutable text : string
140 ; mutable fullscreen : (int * int) option
141 ; mutable textentry : textentry option
142 ; mutable outlines : outlines
143 ; mutable outline : (bool * int * int * outline array * string) option
144 ; mutable bookmarks : outline list
145 ; mutable path : string
146 ; mutable password : string
147 ; mutable invalidated : int
148 ; mutable colorscale : float
149 ; hists : hists
151 and hists =
152 { pat : string circbuf
153 ; pag : string circbuf
154 ; nav : float circbuf
158 let conf =
159 { scrollw = 5
160 ; scrollh = 12
161 ; icase = true
162 ; preload = true
163 ; pagebias = 0
164 ; verbose = false
165 ; scrollincr = 24
166 ; maxhfit = true
167 ; crophack = false
168 ; autoscroll = false
169 ; showall = false
170 ; hlinks = false
171 ; underinfo = false
172 ; interpagespace = 2
173 ; margin = 0
177 let state =
178 { csock = Unix.stdin
179 ; ssock = Unix.stdin
180 ; w = 900
181 ; h = 900
182 ; winw = 900
183 ; rotate = 0
184 ; y = 0
185 ; ty = 0.0
186 ; layout = []
187 ; maxy = max_int
188 ; pagemap = Hashtbl.create 10
189 ; pagecache = cbnew 10 ""
190 ; pages = []
191 ; pagecount = 0
192 ; rendering = false
193 ; mstate = Mnone
194 ; rects = []
195 ; rects1 = []
196 ; text = ""
197 ; fullscreen = None
198 ; textentry = None
199 ; searchpattern = ""
200 ; outlines = Olist []
201 ; outline = None
202 ; bookmarks = []
203 ; path = ""
204 ; password = ""
205 ; invalidated = 0
206 ; hists =
207 { nav = cbnew 100 0.0
208 ; pat = cbnew 20 ""
209 ; pag = cbnew 10 ""
211 ; colorscale = 1.0
215 let vlog fmt =
216 if conf.verbose
217 then
218 Printf.kprintf prerr_endline fmt
219 else
220 Printf.kprintf ignore fmt
223 let writecmd fd s =
224 let len = String.length s in
225 let n = 4 + len in
226 let b = Buffer.create n in
227 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
228 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
229 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
230 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
231 Buffer.add_string b s;
232 let s' = Buffer.contents b in
233 let n' = Unix.write fd s' 0 n in
234 if n' != n then failwith "write failed";
237 let readcmd fd =
238 let s = "xxxx" in
239 let n = Unix.read fd s 0 4 in
240 if n != 4 then failwith "incomplete read(len)";
241 let len = 0
242 lor (Char.code s.[0] lsl 24)
243 lor (Char.code s.[1] lsl 16)
244 lor (Char.code s.[2] lsl 8)
245 lor (Char.code s.[3] lsl 0)
247 let s = String.create len in
248 let n = Unix.read fd s 0 len in
249 if n != len then failwith "incomplete read(data)";
253 let yratio y =
254 if y = state.maxy
255 then 1.0
256 else float y /. float state.maxy
259 let makecmd s l =
260 let b = Buffer.create 10 in
261 Buffer.add_string b s;
262 let rec combine = function
263 | [] -> b
264 | x :: xs ->
265 Buffer.add_char b ' ';
266 let s =
267 match x with
268 | `b b -> if b then "1" else "0"
269 | `s s -> s
270 | `i i -> string_of_int i
271 | `f f -> string_of_float f
272 | `I f -> string_of_int (truncate f)
274 Buffer.add_string b s;
275 combine xs;
277 combine l;
280 let wcmd s l =
281 let cmd = Buffer.contents (makecmd s l) in
282 writecmd state.csock cmd;
285 let calcheight () =
286 let rec f pn ph fh l =
287 match l with
288 | (n, _, h) :: rest ->
289 let fh = fh + (n - pn) * (ph + conf.interpagespace) in
290 f n h fh rest
292 | [] ->
293 let fh = fh + ((ph + conf.interpagespace) * (state.pagecount - pn)) in
294 max 0 fh
296 let fh = f 0 0 0 state.pages in
300 let getpageyh pageno =
301 let rec f pn ph y l =
302 match l with
303 | (n, _, h) :: rest ->
304 if n >= pageno
305 then
306 y + (pageno - pn) * (ph + conf.interpagespace), h
307 else
308 let y = y + (n - pn) * (ph + conf.interpagespace) in
309 f n h y rest
311 | [] ->
312 y + (pageno - pn) * (ph + conf.interpagespace), ph
314 f 0 0 0 state.pages;
317 let getpagey pageno = fst (getpageyh pageno);;
319 let layout y sh =
320 let ips = conf.interpagespace in
321 let rec f ~pageno ~pdimno ~prev ~vy ~py ~dy ~pdims ~cacheleft ~accu =
322 if pageno = state.pagecount || cacheleft = 0
323 then accu
324 else
325 let ((_, w, h) as curr), rest, pdimno =
326 match pdims with
327 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
328 curr, rest, pdimno + 1
329 | _ ->
330 prev, pdims, pdimno
332 let pageno' = pageno + 1 in
333 if py + h > vy
334 then
335 let py' = vy - py in
336 let vh = h - py' in
337 if dy + vh > sh
338 then
339 let vh = sh - dy in
340 if vh <= 0
341 then
342 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 e :: accu
355 else
356 let e =
357 { pageno = pageno
358 ; pagedimno = pdimno
359 ; pagew = w
360 ; pageh = h
361 ; pagedispy = dy
362 ; pagey = py'
363 ; pagevh = vh
366 let accu = e :: accu in
367 f ~pageno:pageno'
368 ~pdimno
369 ~prev:curr
370 ~vy:(vy + vh)
371 ~py:(py + h)
372 ~dy:(dy + vh + ips)
373 ~pdims:rest
374 ~cacheleft:(pred cacheleft)
375 ~accu
376 else (
377 let py' = vy - py in
378 let vh = h - py' in
379 let t = ips + vh in
380 let dy, py = if t < 0 then 0, py + h + ips else t, py + h - vh in
381 f ~pageno:pageno'
382 ~pdimno
383 ~prev:curr
387 ~pdims:rest
388 ~cacheleft
389 ~accu
392 if state.invalidated = 0
393 then
394 let accu =
396 ~pageno:0
397 ~pdimno:~-1
398 ~prev:(0,0,0)
399 ~vy:y
400 ~py:0
401 ~dy:0
402 ~pdims:state.pages
403 ~cacheleft:(cblen state.pagecache)
404 ~accu:[]
406 state.maxy <- calcheight ();
407 List.rev accu
408 else
412 let clamp incr =
413 let y = state.y + incr in
414 let y = max 0 y in
415 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
419 let getopaque pageno =
420 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w, state.rotate))
421 with Not_found -> None
424 let cache pageno opaque =
425 Hashtbl.replace state.pagemap (pageno + 1, state.w, state.rotate) opaque
428 let validopaque opaque = String.length opaque > 0;;
430 let render l =
431 match getopaque l.pageno with
432 | None when not state.rendering ->
433 state.rendering <- true;
434 cache l.pageno "";
435 wcmd "render" [`i (l.pageno + 1)
436 ;`i l.pagedimno
437 ;`i l.pagew
438 ;`i l.pageh];
440 | _ -> ()
443 let loadlayout layout =
444 let rec f all = function
445 | l :: ls ->
446 begin match getopaque l.pageno with
447 | None -> render l; f false ls
448 | Some opaque -> f (all && validopaque opaque) ls
450 | [] -> all
452 f (layout <> []) layout;
455 let preload () =
456 if conf.preload
457 then
458 let evictedvisible =
459 let evictedopaque = cbpeekw state.pagecache in
460 List.exists (fun l ->
461 match getopaque l.pageno with
462 | Some opaque when validopaque opaque ->
463 evictedopaque = opaque
464 | otherwise -> false
465 ) state.layout
467 if not evictedvisible
468 then
469 let y = if state.y < state.h then 0 else state.y - state.h in
470 let pages = layout y (state.h*3) in
471 List.iter render pages;
474 let gotoy y =
475 let y = max 0 y in
476 let y = min state.maxy y in
477 let pages = layout y state.h in
478 let ready = loadlayout pages in
479 state.ty <- yratio y;
480 if conf.showall
481 then (
482 if ready
483 then (
484 state.layout <- pages;
485 state.y <- y;
486 Glut.postRedisplay ();
489 else (
490 state.layout <- pages;
491 state.y <- y;
492 Glut.postRedisplay ();
494 preload ();
497 let addnav () =
498 cbput state.hists.nav (yratio state.y);
499 cbrfollowlen state.hists.nav;
502 let getnav () =
503 let y = cbget state.hists.nav ~-1 in
504 truncate (y *. float state.maxy)
507 let gotopage n top =
508 let y, h = getpageyh n in
509 addnav ();
510 gotoy (y + (truncate (top *. float h)));
513 let gotopage1 n top =
514 let y = getpagey n in
515 addnav ();
516 gotoy (y + top);
519 let invalidate () =
520 state.layout <- [];
521 state.pages <- [];
522 state.rects <- [];
523 state.rects1 <- [];
524 state.invalidated <- state.invalidated + 1;
527 let scalecolor c =
528 let c = c *. state.colorscale in
529 (c, c, c);
532 let reshape ~w ~h =
533 let margin =
534 let m = float conf.margin in
535 let m = m *. (float w /. 20.) in
536 let m = truncate m in
537 if m*2 > (w - conf.scrollw) then 0 else m
539 state.winw <- w;
540 let w = w - margin * 2 - conf.scrollw in
541 state.w <- w;
542 state.h <- h;
543 GlMat.mode `modelview;
544 GlMat.load_identity ();
545 GlMat.mode `projection;
546 GlMat.load_identity ();
547 GlMat.rotate ~x:1.0 ~angle:180.0 ();
548 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
549 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
550 GlClear.color (scalecolor 1.0);
551 GlClear.clear [`color];
553 invalidate ();
554 wcmd "geometry" [`i state.w; `i h];
557 let showtext c s =
558 GlDraw.viewport 0 0 state.winw state.h;
559 GlDraw.color (0.0, 0.0, 0.0);
560 GlMat.push ();
561 GlMat.load_identity ();
562 GlMat.rotate ~x:1.0 ~angle:180.0 ();
563 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
564 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.h, 1.0);
565 GlDraw.rect
566 (0.0, float (state.h - 18))
567 (float (state.winw - conf.scrollw), float state.h)
569 GlMat.pop ();
570 let font = Glut.BITMAP_8_BY_13 in
571 GlDraw.color (1.0, 1.0, 1.0);
572 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
573 Glut.bitmapCharacter ~font ~c:(Char.code c);
574 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
577 let enttext () =
578 let len = String.length state.text in
579 match state.textentry with
580 | None ->
581 if len > 0 then showtext ' ' state.text
583 | Some (c, text, _, _, _) ->
584 let s =
585 if len > 0
586 then
587 text ^ " [" ^ state.text ^ "]"
588 else
589 text
591 showtext c s;
594 let showtext c s =
595 if true
596 then (
597 state.text <- Printf.sprintf "%c%s" c s;
598 Glut.postRedisplay ();
600 else (
601 showtext c s;
602 Glut.swapBuffers ();
606 let act cmd =
607 match cmd.[0] with
608 | 'c' ->
609 state.pages <- [];
611 | 'D' ->
612 state.rects <- state.rects1;
613 Glut.postRedisplay ()
615 | 'C' ->
616 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
617 state.pagecount <- n;
618 state.invalidated <- state.invalidated - 1;
619 if state.invalidated = 0
620 then (
621 let rely = yratio state.y in
622 state.maxy <- calcheight ();
623 gotoy (truncate (float state.maxy *. rely));
626 | 't' ->
627 let s = Scanf.sscanf cmd "t %n"
628 (fun n -> String.sub cmd n (String.length cmd - n))
630 Glut.setWindowTitle s
632 | 'T' ->
633 let s = Scanf.sscanf cmd "T %n"
634 (fun n -> String.sub cmd n (String.length cmd - n))
636 if state.textentry = None
637 then (
638 state.text <- s;
639 showtext ' ' s;
641 else (
642 state.text <- s;
643 Glut.postRedisplay ();
646 | 'V' ->
647 if conf.verbose
648 then
649 let s = Scanf.sscanf cmd "V %n"
650 (fun n -> String.sub cmd n (String.length cmd - n))
652 state.text <- s;
653 showtext ' ' s;
655 | 'F' ->
656 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
657 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
658 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
659 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
661 let y = (getpagey pageno) + truncate y0 in
662 addnav ();
663 gotoy y;
664 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
666 | 'R' ->
667 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
668 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
669 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
670 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
672 state.rects1 <-
673 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
675 | 'r' ->
676 let n, w, h, r, p =
677 Scanf.sscanf cmd "r %d %d %d %d %s"
678 (fun n w h r p -> (n, w, h, r, p))
680 Hashtbl.replace state.pagemap (n, w, r) p;
681 let opaque = cbpeekw state.pagecache in
682 if validopaque opaque
683 then (
684 let k =
685 Hashtbl.fold
686 (fun k v a -> if v = opaque then k else a)
687 state.pagemap (-1, -1, -1)
689 wcmd "free" [`s opaque];
690 Hashtbl.remove state.pagemap k
692 cbput state.pagecache p;
693 state.rendering <- false;
694 if conf.showall
695 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
696 else (
697 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
698 if visible
699 then gotoy state.y
700 else (ignore (loadlayout state.layout); preload ())
703 | 'l' ->
704 let (n, w, h) as pagelayout =
705 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
707 state.pages <- pagelayout :: state.pages
709 | 'o' ->
710 let (l, n, t, h, pos) =
711 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
713 let s = String.sub cmd pos (String.length cmd - pos) in
714 let s =
715 let l = String.length s in
716 let b = Buffer.create (String.length s) in
717 let rec loop pc2 i =
718 if i = l
719 then ()
720 else
721 let pc2 =
722 match s.[i] with
723 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
724 | '\xc2' -> true
725 | c ->
726 let c = if Char.code c land 0x80 = 0 then c else '?' in
727 Buffer.add_char b c;
728 false
730 loop pc2 (i+1)
732 loop false 0;
733 Buffer.contents b
735 let outline = (s, l, n, float t /. float h) in
736 let outlines =
737 match state.outlines with
738 | Olist outlines -> Olist (outline :: outlines)
739 | Oarray _ -> Olist [outline]
740 | Onarrow _ -> Olist [outline]
742 state.outlines <- outlines
744 | _ ->
745 log "unknown cmd `%S'" cmd
748 let now = Unix.gettimeofday;;
750 let idle () =
751 let rec loop delay =
752 let r, _, _ = Unix.select [state.csock] [] [] delay in
753 begin match r with
754 | [] ->
755 if conf.autoscroll
756 then begin
757 let y = state.y + conf.scrollincr in
758 let y = if y >= state.maxy then 0 else y in
759 gotoy y;
760 state.text <- "";
761 end;
763 | _ ->
764 let cmd = readcmd state.csock in
765 act cmd;
766 loop 0.0
767 end;
768 in loop 0.001
771 let onhist cb = function
772 | HCprev -> cbget cb ~-1
773 | HCnext -> cbget cb 1
774 | HCfirst -> cbget cb ~-(cb.rc)
775 | HClast -> cbget cb (cb.len - 1 - cb.rc)
778 let search pattern forward =
779 if String.length pattern > 0
780 then
781 let pn, py =
782 match state.layout with
783 | [] -> 0, 0
784 | l :: _ ->
785 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
787 let cmd =
788 let b = makecmd "search"
789 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
791 Buffer.add_char b ',';
792 Buffer.add_string b pattern;
793 Buffer.add_char b '\000';
794 Buffer.contents b;
796 writecmd state.csock cmd;
799 let intentry text key =
800 let c = Char.unsafe_chr key in
801 match c with
802 | '0' .. '9' ->
803 let s = "x" in s.[0] <- c;
804 let text = text ^ s in
805 TEcont text
807 | _ ->
808 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
809 TEcont text
812 let addchar s c =
813 let b = Buffer.create (String.length s + 1) in
814 Buffer.add_string b s;
815 Buffer.add_char b c;
816 Buffer.contents b;
819 let textentry text key =
820 let c = Char.unsafe_chr key in
821 match c with
822 | _ when key >= 32 && key < 127 ->
823 let text = addchar text c in
824 TEcont text
826 | _ ->
827 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
828 TEcont text
831 let rotate angle =
832 state.rotate <- angle;
833 invalidate ();
834 wcmd "rotate" [`i angle];
837 let optentry text key =
838 let btos b = if b then "on" else "off" in
839 let c = Char.unsafe_chr key in
840 match c with
841 | 's' ->
842 let ondone s =
843 try conf.scrollincr <- int_of_string s with exc ->
844 state.text <- Printf.sprintf "bad integer `%s': %s"
845 s (Printexc.to_string exc)
847 TEswitch ('#', "", None, intentry, ondone)
849 | 'R' ->
850 let ondone s =
851 match try
852 Some (int_of_string s)
853 with exc ->
854 state.text <- Printf.sprintf "bad integer `%s': %s"
855 s (Printexc.to_string exc);
856 None
857 with
858 | Some angle -> rotate angle
859 | None -> ()
861 TEswitch ('^', "", None, intentry, ondone)
863 | 'i' ->
864 conf.icase <- not conf.icase;
865 TEdone ("case insensitive search " ^ (btos conf.icase))
867 | 'p' ->
868 conf.preload <- not conf.preload;
869 gotoy state.y;
870 TEdone ("preload " ^ (btos conf.preload))
872 | 'v' ->
873 conf.verbose <- not conf.verbose;
874 TEdone ("verbose " ^ (btos conf.verbose))
876 | 'h' ->
877 conf.maxhfit <- not conf.maxhfit;
878 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
879 TEdone ("maxhfit " ^ (btos conf.maxhfit))
881 | 'c' ->
882 conf.crophack <- not conf.crophack;
883 TEdone ("crophack " ^ btos conf.crophack)
885 | 'a' ->
886 conf.showall <- not conf.showall;
887 TEdone ("showall " ^ btos conf.showall)
889 | 'f' ->
890 conf.underinfo <- not conf.underinfo;
891 TEdone ("underinfo " ^ btos conf.underinfo)
893 | 'S' ->
894 let ondone s =
896 conf.interpagespace <- int_of_string s;
897 let rely = yratio state.y in
898 state.maxy <- calcheight ();
899 gotoy (truncate (float state.maxy *. rely));
900 with exc ->
901 state.text <- Printf.sprintf "bad integer `%s': %s"
902 s (Printexc.to_string exc)
904 TEswitch ('%', "", None, intentry, ondone)
906 | _ ->
907 state.text <- Printf.sprintf "bad option %d `%c'" key c;
908 TEstop
911 let maxoutlinerows () = (state.h - 31) / 16;;
913 let enterselector allowdel outlines errmsg =
914 if Array.length outlines = 0
915 then (
916 showtext ' ' errmsg;
918 else (
919 Glut.setCursor Glut.CURSOR_INHERIT;
920 let pageno =
921 match state.layout with
922 | [] -> -1
923 | {pageno=pageno} :: rest -> pageno
925 let active =
926 let rec loop n =
927 if n = Array.length outlines
928 then 0
929 else
930 let (_, _, outlinepageno, _) = outlines.(n) in
931 if outlinepageno >= pageno then n else loop (n+1)
933 loop 0
935 state.outline <-
936 Some (allowdel, active,
937 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
938 Glut.postRedisplay ();
942 let enteroutlinemode () =
943 let outlines =
944 match state.outlines with
945 | Oarray a -> a
946 | Olist l ->
947 let a = Array.of_list (List.rev l) in
948 state.outlines <- Oarray a;
950 | Onarrow (a, b) -> a
952 enterselector false outlines "Document has no outline";
955 let enterbookmarkmode () =
956 let bookmarks = Array.of_list state.bookmarks in
957 enterselector true bookmarks "Document has no bookmarks (yet)";
961 let quickbookmark ?title () =
962 match state.layout with
963 | [] -> ()
964 | l :: _ ->
965 let title =
966 match title with
967 | None ->
968 let sec = Unix.gettimeofday () in
969 let tm = Unix.localtime sec in
970 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
971 l.pageno
972 tm.Unix.tm_mday
973 tm.Unix.tm_mon
974 (tm.Unix.tm_year + 1900)
975 tm.Unix.tm_hour
976 tm.Unix.tm_min
977 | Some title -> title
979 state.bookmarks <-
980 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
983 let doreshape w h =
984 state.fullscreen <- None;
985 Glut.reshapeWindow w h;
988 let opendoc path password =
989 invalidate ();
990 state.path <- path;
991 state.password <- password;
992 Hashtbl.clear state.pagemap;
994 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
995 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
996 wcmd "geometry" [`i state.w; `i state.h];
999 let viewkeyboard ~key ~x ~y =
1000 let enttext te =
1001 state.textentry <- te;
1002 state.text <- "";
1003 enttext ();
1004 Glut.postRedisplay ()
1006 match state.textentry with
1007 | None ->
1008 let c = Char.chr key in
1009 begin match c with
1010 | '\027' | 'q' ->
1011 exit 0
1013 | '\008' ->
1014 let y = getnav () in
1015 gotoy y
1017 | 'o' ->
1018 enteroutlinemode ()
1020 | 'u' ->
1021 state.rects <- [];
1022 state.text <- "";
1023 Glut.postRedisplay ()
1025 | '/' | '?' ->
1026 let ondone isforw s =
1027 cbput state.hists.pat s;
1028 cbrfollowlen state.hists.pat;
1029 state.searchpattern <- s;
1030 search s isforw
1032 enttext (Some (c, "", Some (onhist state.hists.pat),
1033 textentry, ondone (c ='/')))
1035 | '+' ->
1036 if Glut.getModifiers () land Glut.active_ctrl != 0
1037 then (
1038 let margin = max 0 (conf.margin - 1) in
1039 conf.margin <- margin;
1040 reshape state.winw state.h;
1042 else
1043 let ondone s =
1044 let n =
1045 try int_of_string s with exc ->
1046 state.text <- Printf.sprintf "bad integer `%s': %s"
1047 s (Printexc.to_string exc);
1048 max_int
1050 if n != max_int
1051 then (
1052 conf.pagebias <- n;
1053 state.text <- "page bias is now " ^ string_of_int n;
1056 enttext (Some ('+', "", None, intentry, ondone))
1058 | '-' ->
1059 if Glut.getModifiers () land Glut.active_ctrl != 0
1060 then (
1061 let margin = min 8 (conf.margin + 1) in
1062 conf.margin <- margin;
1063 reshape state.winw state.h;
1065 else
1066 let ondone msg =
1067 state.text <- msg;
1069 enttext (Some ('-', "", None, optentry, ondone))
1071 | '0' .. '9' ->
1072 let ondone s =
1073 let n =
1074 try int_of_string s with exc ->
1075 state.text <- Printf.sprintf "bad integer `%s': %s"
1076 s (Printexc.to_string exc);
1079 if n >= 0
1080 then (
1081 addnav ();
1082 cbput state.hists.pag (string_of_int n);
1083 cbrfollowlen state.hists.pag;
1084 gotoy (getpagey (n + conf.pagebias - 1))
1087 let pageentry text key =
1088 match Char.unsafe_chr key with
1089 | 'g' -> TEdone text
1090 | _ -> intentry text key
1092 let text = "x" in text.[0] <- c;
1093 enttext (Some (':', text, Some (onhist state.hists.pag),
1094 pageentry, ondone))
1096 | 'b' ->
1097 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
1098 reshape state.winw state.h;
1100 | 'l' ->
1101 conf.hlinks <- not conf.hlinks;
1102 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1103 Glut.postRedisplay ()
1105 | 'a' ->
1106 conf.autoscroll <- not conf.autoscroll
1108 | 'P' ->
1109 begin match state.layout with
1110 | [] -> ()
1111 | l :: _ ->
1112 let ips =
1113 let d = state.h - l.pageh in
1114 max 0 (d / 2)
1116 let rely = yratio state.y in
1117 conf.interpagespace <- ips;
1118 state.maxy <- calcheight ();
1119 gotoy (truncate (float state.maxy *. rely));
1120 end;
1122 | 'f' ->
1123 begin match state.fullscreen with
1124 | None ->
1125 state.fullscreen <- Some (state.w, state.h);
1126 Glut.fullScreen ()
1127 | Some (w, h) ->
1128 state.fullscreen <- None;
1129 doreshape w h
1132 | 'g' ->
1133 gotoy 0
1135 | 'n' ->
1136 search state.searchpattern true
1138 | 'p' | 'N' ->
1139 search state.searchpattern false
1141 | 't' ->
1142 begin match state.layout with
1143 | [] -> ()
1144 | l :: _ ->
1145 gotoy (state.y - l.pagey - conf.interpagespace);
1148 | ' ' ->
1149 begin match List.rev state.layout with
1150 | [] -> ()
1151 | l :: _ ->
1152 gotoy (clamp (l.pageh - l.pagey + conf.interpagespace))
1155 | '\127' ->
1156 begin match state.layout with
1157 | [] -> ()
1158 | l :: _ ->
1159 gotoy (clamp (-l.pageh - conf.interpagespace));
1162 | '=' ->
1163 let f (fn, ln) l =
1164 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1166 let fn, ln = List.fold_left f (-1, -1) state.layout in
1167 let s =
1168 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1169 let percent =
1170 if maxy <= 0
1171 then 100.
1172 else (100. *. (float state.y /. float maxy)) in
1173 if fn = ln
1174 then
1175 Printf.sprintf "Page %d of %d %.2f%%"
1176 (fn+1) state.pagecount percent
1177 else
1178 Printf.sprintf
1179 "Pages %d-%d of %d %.2f%%"
1180 (fn+1) (ln+1) state.pagecount percent
1182 showtext ' ' s;
1184 | 'w' ->
1185 begin match state.layout with
1186 | [] -> ()
1187 | l :: _ ->
1188 doreshape l.pagew l.pageh;
1189 Glut.postRedisplay ();
1192 | '\'' ->
1193 enterbookmarkmode ()
1195 | 'm' ->
1196 let ondone s =
1197 match state.layout with
1198 | l :: _ ->
1199 state.bookmarks <-
1200 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1201 :: state.bookmarks
1202 | _ -> ()
1204 enttext (Some ('~', "", None, textentry, ondone))
1206 | '~' ->
1207 quickbookmark ();
1208 showtext ' ' "Quick bookmark added";
1210 | 'z' ->
1211 begin match state.layout with
1212 | l :: _ ->
1213 let a = getpagewh l.pagedimno in
1214 let w, h =
1215 if conf.crophack
1216 then
1217 (truncate (1.8 *. (a.(1) -. a.(0))),
1218 truncate (1.2 *. (a.(3) -. a.(0))))
1219 else
1220 (truncate (a.(1) -. a.(0)),
1221 truncate (a.(3) -. a.(0)))
1223 doreshape w h;
1224 Glut.postRedisplay ();
1226 | [] -> ()
1229 | '<' | '>' ->
1230 rotate (state.rotate + (if c = '>' then 30 else -30));
1232 | '[' | ']' ->
1233 state.colorscale <-
1234 max 0.0
1235 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1236 Glut.postRedisplay ()
1238 | 'k' -> gotoy (clamp (-conf.scrollincr))
1239 | 'j' -> gotoy (clamp conf.scrollincr)
1241 | 'r' -> opendoc state.path state.password
1243 | _ ->
1244 vlog "huh? %d %c" key (Char.chr key);
1247 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1248 let len = String.length text in
1249 if len = 0
1250 then (
1251 state.textentry <- None;
1252 Glut.postRedisplay ();
1254 else (
1255 let s = String.sub text 0 (len - 1) in
1256 enttext (Some (c, s, onhist, onkey, ondone))
1259 | Some (c, text, onhist, onkey, ondone) ->
1260 begin match Char.unsafe_chr key with
1261 | '\r' | '\n' ->
1262 ondone text;
1263 state.textentry <- None;
1264 Glut.postRedisplay ()
1266 | '\027' ->
1267 state.textentry <- None;
1268 Glut.postRedisplay ()
1270 | _ ->
1271 begin match onkey text key with
1272 | TEdone text ->
1273 state.textentry <- None;
1274 ondone text;
1275 Glut.postRedisplay ()
1277 | TEcont text ->
1278 enttext (Some (c, text, onhist, onkey, ondone));
1280 | TEstop ->
1281 state.textentry <- None;
1282 Glut.postRedisplay ()
1284 | TEswitch te ->
1285 state.textentry <- Some te;
1286 Glut.postRedisplay ()
1287 end;
1288 end;
1291 let narrow outlines pattern =
1292 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1293 match reopt with
1294 | None -> None
1295 | Some re ->
1296 let rec fold accu n =
1297 if n = -1
1298 then accu
1299 else
1300 let (s, _, _, _) as o = outlines.(n) in
1301 let accu =
1302 if (try ignore (Str.search_forward re s 0); true
1303 with Not_found -> false)
1304 then (o :: accu)
1305 else accu
1307 fold accu (n-1)
1309 let matched = fold [] (Array.length outlines - 1) in
1310 if matched = [] then None else Some (Array.of_list matched)
1313 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1314 let search active pattern incr =
1315 let dosearch re =
1316 let rec loop n =
1317 if n = Array.length outlines || n = -1
1318 then None
1319 else
1320 let (s, _, _, _) = outlines.(n) in
1322 (try ignore (Str.search_forward re s 0); true
1323 with Not_found -> false)
1324 then Some n
1325 else loop (n + incr)
1327 loop active
1330 let re = Str.regexp_case_fold pattern in
1331 dosearch re
1332 with Failure s ->
1333 state.text <- s;
1334 None
1336 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1337 match key with
1338 | 27 ->
1339 if String.length qsearch = 0
1340 then (
1341 state.text <- "";
1342 state.outline <- None;
1343 Glut.postRedisplay ();
1345 else (
1346 state.text <- "";
1347 state.outline <- Some (allowdel, active, first, outlines, "");
1348 Glut.postRedisplay ();
1351 | 18 | 19 ->
1352 let incr = if key = 18 then -1 else 1 in
1353 let active, first =
1354 match search (active + incr) qsearch incr with
1355 | None ->
1356 state.text <- qsearch ^ " [not found]";
1357 active, first
1358 | Some active ->
1359 state.text <- qsearch;
1360 active, firstof active
1362 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1363 Glut.postRedisplay ();
1365 | 8 ->
1366 let len = String.length qsearch in
1367 if len = 0
1368 then ()
1369 else (
1370 if len = 1
1371 then (
1372 state.text <- "";
1373 state.outline <- Some (allowdel, active, first, outlines, "");
1375 else
1376 let qsearch = String.sub qsearch 0 (len - 1) in
1377 let active, first =
1378 match search active qsearch ~-1 with
1379 | None ->
1380 state.text <- qsearch ^ " [not found]";
1381 active, first
1382 | Some active ->
1383 state.text <- qsearch;
1384 active, firstof active
1386 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1388 Glut.postRedisplay ()
1390 | 13 ->
1391 if active < Array.length outlines
1392 then (
1393 let (_, _, n, t) = outlines.(active) in
1394 gotopage n t;
1396 state.text <- "";
1397 if allowdel then state.bookmarks <- Array.to_list outlines;
1398 state.outline <- None;
1399 Glut.postRedisplay ();
1401 | _ when key >= 32 && key < 127 ->
1402 let pattern = addchar qsearch (Char.chr key) in
1403 let active, first =
1404 match search active pattern 1 with
1405 | None ->
1406 state.text <- pattern ^ " [not found]";
1407 active, first
1408 | Some active ->
1409 state.text <- pattern;
1410 active, firstof active
1412 state.outline <- Some (allowdel, active, first, outlines, pattern);
1413 Glut.postRedisplay ()
1415 | 14 when not allowdel ->
1416 let optoutlines = narrow outlines qsearch in
1417 begin match optoutlines with
1418 | None -> state.text <- "can't narrow"
1419 | Some outlines ->
1420 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1421 match state.outlines with
1422 | Olist l -> ()
1423 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1424 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1425 end;
1426 Glut.postRedisplay ()
1428 | 21 when not allowdel ->
1429 let outline =
1430 match state.outlines with
1431 | Oarray a -> a
1432 | Olist l ->
1433 let a = Array.of_list (List.rev l) in
1434 state.outlines <- Oarray a;
1436 | Onarrow (a, b) ->
1437 state.outlines <- Oarray b;
1440 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1441 Glut.postRedisplay ()
1443 | 12 ->
1444 state.outline <-
1445 Some (allowdel, active, firstof active, outlines, qsearch);
1446 Glut.postRedisplay ()
1448 | 127 when allowdel ->
1449 let len = Array.length outlines - 1 in
1450 if len = 0
1451 then (
1452 state.outline <- None;
1453 state.bookmarks <- [];
1455 else (
1456 let bookmarks = Array.init len
1457 (fun i ->
1458 let i = if i >= active then i + 1 else i in
1459 outlines.(i)
1462 state.outline <-
1463 Some (allowdel,
1464 min active (len-1),
1465 min first (len-1),
1466 bookmarks, qsearch)
1469 Glut.postRedisplay ()
1471 | _ -> log "unknown key %d" key
1474 let keyboard ~key ~x ~y =
1475 if key = 7
1476 then
1477 wcmd "interrupt" []
1478 else
1479 match state.outline with
1480 | None -> viewkeyboard ~key ~x ~y
1481 | Some outline -> outlinekeyboard ~key ~x ~y outline
1484 let special ~key ~x ~y =
1485 match state.outline with
1486 | None ->
1487 begin match state.textentry with
1488 | None ->
1489 let y =
1490 match key with
1491 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1492 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1493 | Glut.KEY_DOWN -> clamp conf.scrollincr
1494 | Glut.KEY_PAGE_UP ->
1495 if Glut.getModifiers () land Glut.active_ctrl != 0
1496 then
1497 match state.layout with
1498 | [] -> state.y
1499 | l :: _ -> state.y - l.pagey
1500 else
1501 clamp (-state.h)
1502 | Glut.KEY_PAGE_DOWN ->
1503 if Glut.getModifiers () land Glut.active_ctrl != 0
1504 then
1505 match List.rev state.layout with
1506 | [] -> state.y
1507 | l :: _ -> getpagey l.pageno
1508 else
1509 clamp state.h
1510 | Glut.KEY_HOME -> addnav (); 0
1511 | Glut.KEY_END ->
1512 addnav ();
1513 state.maxy - (if conf.maxhfit then state.h else 0)
1514 | _ -> state.y
1516 if not conf.verbose then state.text <- "";
1517 gotoy y
1519 | Some (c, s, Some onhist, onkey, ondone) ->
1520 let s =
1521 match key with
1522 | Glut.KEY_UP -> onhist HCprev
1523 | Glut.KEY_DOWN -> onhist HCnext
1524 | Glut.KEY_HOME -> onhist HCfirst
1525 | Glut.KEY_END -> onhist HClast
1526 | _ -> state.text
1528 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1529 Glut.postRedisplay ()
1531 | _ -> ()
1534 | Some (allowdel, active, first, outlines, qsearch) ->
1535 let maxrows = maxoutlinerows () in
1536 let navigate incr =
1537 let active = active + incr in
1538 let active = max 0 (min active (Array.length outlines - 1)) in
1539 let first =
1540 if active > first
1541 then
1542 let rows = active - first in
1543 if rows > maxrows then active - maxrows else first
1544 else active
1546 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1547 Glut.postRedisplay ()
1549 match key with
1550 | Glut.KEY_UP -> navigate ~-1
1551 | Glut.KEY_DOWN -> navigate 1
1552 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1553 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1555 | Glut.KEY_HOME ->
1556 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1557 Glut.postRedisplay ()
1559 | Glut.KEY_END ->
1560 let active = Array.length outlines - 1 in
1561 let first = max 0 (active - maxrows) in
1562 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1563 Glut.postRedisplay ()
1565 | _ -> ()
1568 let drawplaceholder l =
1569 GlDraw.color (scalecolor 1.0);
1570 GlDraw.rect
1571 (0.0, float l.pagedispy)
1572 (float l.pagew, float (l.pagedispy + l.pagevh))
1574 let x = 0.0
1575 and y = float (l.pagedispy + 13) in
1576 let font = Glut.BITMAP_8_BY_13 in
1577 GlDraw.color (0.0, 0.0, 0.0);
1578 GlPix.raster_pos ~x ~y ();
1579 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1580 ("Loading " ^ string_of_int l.pageno);
1583 let now () = Unix.gettimeofday ();;
1585 let drawpage i l =
1586 begin match getopaque l.pageno with
1587 | Some opaque when validopaque opaque ->
1588 if state.textentry = None
1589 then GlDraw.color (scalecolor 1.0)
1590 else GlDraw.color (scalecolor 0.4);
1591 let a = now () in
1592 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1593 opaque;
1594 let b = now () in
1595 let d = b-.a in
1596 vlog "draw %f sec" d;
1598 | _ ->
1599 drawplaceholder l;
1600 end;
1601 l.pagedispy + l.pagevh;
1604 let scrollindicator () =
1605 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1606 GlDraw.color (0.64 , 0.64, 0.64);
1607 GlDraw.rect
1608 (0., 0.)
1609 (float conf.scrollw, float state.h)
1611 GlDraw.color (0.0, 0.0, 0.0);
1612 let sh = (float (maxy + state.h) /. float state.h) in
1613 let sh = float state.h /. sh in
1614 let sh = max sh (float conf.scrollh) in
1616 let percent =
1617 if state.y = state.maxy
1618 then 1.0
1619 else float state.y /. float maxy
1621 let position = (float state.h -. sh) *. percent in
1623 let position =
1624 if position +. sh > float state.h
1625 then
1626 float state.h -. sh
1627 else
1628 position
1630 GlDraw.rect
1631 (0.0, position)
1632 (float conf.scrollw, position +. sh)
1636 let showsel margin =
1637 match state.mstate with
1638 | Mnone ->
1641 | Msel ((x0, y0), (x1, y1)) ->
1642 let rec loop = function
1643 | l :: ls ->
1644 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1645 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1646 then
1647 match getopaque l.pageno with
1648 | Some opaque when validopaque opaque ->
1649 let oy = -l.pagey + l.pagedispy in
1650 seltext opaque (x0 - margin, y0, x1 - margin, y1) oy;
1652 | _ -> ()
1653 else loop ls
1654 | [] -> ()
1656 loop state.layout
1659 let showrects () =
1660 Gl.enable `blend;
1661 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1662 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1663 List.iter
1664 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1665 List.iter (fun l ->
1666 if l.pageno = pageno
1667 then (
1668 let d = float (l.pagedispy - l.pagey) in
1669 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1670 GlDraw.begins `quads;
1672 GlDraw.vertex2 (x0, y0+.d);
1673 GlDraw.vertex2 (x1, y1+.d);
1674 GlDraw.vertex2 (x2, y2+.d);
1675 GlDraw.vertex2 (x3, y3+.d);
1677 GlDraw.ends ();
1679 ) state.layout
1680 ) state.rects
1682 Gl.disable `blend;
1685 let showoutline = function
1686 | None -> ()
1687 | Some (allowdel, active, first, outlines, qsearch) ->
1688 Gl.enable `blend;
1689 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1690 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1691 GlDraw.rect (0., 0.) (float state.w, float state.h);
1692 Gl.disable `blend;
1694 GlDraw.color (1., 1., 1.);
1695 let font = Glut.BITMAP_9_BY_15 in
1696 let draw_string x y s =
1697 GlPix.raster_pos ~x ~y ();
1698 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1700 let rec loop row =
1701 if row = Array.length outlines || (row - first) * 16 > state.h
1702 then ()
1703 else (
1704 let (s, l, _, _) = outlines.(row) in
1705 let y = (row - first) * 16 in
1706 let x = 5 + 15*l in
1707 if row = active
1708 then (
1709 Gl.enable `blend;
1710 GlDraw.polygon_mode `both `line;
1711 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1712 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1713 GlDraw.rect (0., float (y + 1))
1714 (float (state.winw - conf.scrollw - 1), float (y + 18));
1715 GlDraw.polygon_mode `both `fill;
1716 Gl.disable `blend;
1717 GlDraw.color (1., 1., 1.);
1719 draw_string (float x) (float (y + 16)) s;
1720 loop (row+1)
1723 loop first
1726 let display () =
1727 let margin = (state.winw - (state.w + conf.scrollw)) / 2 in
1728 GlDraw.viewport margin 0 state.w state.h;
1729 GlClear.color (scalecolor 0.5);
1730 GlClear.clear [`color];
1731 let lasty = List.fold_left drawpage 0 (state.layout) in
1732 showrects ();
1733 GlDraw.viewport (state.winw - conf.scrollw) 0 state.winw state.h;
1734 scrollindicator ();
1735 showsel margin;
1736 GlDraw.viewport 0 0 state.winw state.h;
1737 showoutline state.outline;
1738 enttext ();
1739 Glut.swapBuffers ();
1742 let getunder x y =
1743 let margin = (state.winw - (state.w + conf.scrollw)) / 2 in
1744 let x = x - margin in
1745 let rec f = function
1746 | l :: rest ->
1747 begin match getopaque l.pageno with
1748 | Some opaque when validopaque opaque ->
1749 let y = y - l.pagedispy in
1750 if y > 0
1751 then
1752 let y = l.pagey + y in
1753 match whatsunder opaque x y with
1754 | Unone -> f rest
1755 | under -> under
1756 else
1757 f rest
1758 | _ ->
1759 f rest
1761 | [] -> Unone
1763 f state.layout
1766 let mouse ~button ~bstate ~x ~y =
1767 match button with
1768 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1769 let incr =
1770 if n = 3
1771 then
1772 -conf.scrollincr
1773 else
1774 conf.scrollincr
1776 let incr = incr * 2 in
1777 let y = clamp incr in
1778 gotoy y
1780 | Glut.LEFT_BUTTON when state.outline = None ->
1781 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1782 begin match dest with
1783 | Ulinkgoto (pageno, top) ->
1784 if pageno >= 0
1785 then
1786 gotopage1 pageno top
1788 | Ulinkuri s ->
1789 print_endline s
1791 | Unone when bstate = Glut.DOWN ->
1792 Glut.setCursor Glut.CURSOR_INHERIT;
1793 state.mstate <- Mnone
1795 | Unone | Utext _ ->
1796 if bstate = Glut.DOWN
1797 then (
1798 if state.rotate mod 360 = 0
1799 then (
1800 state.mstate <- Msel ((x, y), (x, y));
1801 Glut.postRedisplay ()
1804 else (
1805 match state.mstate with
1806 | Mnone -> ()
1807 | Msel ((x0, y0), (x1, y1)) ->
1808 let f l =
1809 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1810 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1811 then
1812 match getopaque l.pageno with
1813 | Some opaque when validopaque opaque ->
1814 copysel opaque
1815 | _ -> ()
1817 List.iter f state.layout;
1818 copysel ""; (* ugly *)
1819 Glut.setCursor Glut.CURSOR_INHERIT;
1820 state.mstate <- Mnone;
1824 | _ ->
1827 let mouse ~button ~state ~x ~y = mouse button state x y;;
1829 let motion ~x ~y =
1830 if state.outline = None
1831 then
1832 match state.mstate with
1833 | Mnone -> ()
1834 | Msel (a, _) ->
1835 state.mstate <- Msel (a, (x, y));
1836 Glut.postRedisplay ()
1839 let pmotion ~x ~y =
1840 if state.outline = None
1841 then
1842 match state.mstate with
1843 | Mnone ->
1844 begin match getunder x y with
1845 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
1846 | Ulinkuri uri ->
1847 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1848 Glut.setCursor Glut.CURSOR_INFO
1849 | Ulinkgoto (page, y) ->
1850 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
1851 Glut.setCursor Glut.CURSOR_INFO
1852 | Utext s ->
1853 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1854 Glut.setCursor Glut.CURSOR_TEXT
1857 | Msel (a, _) ->
1861 let () =
1862 let statepath =
1863 let home =
1864 if Sys.os_type = "Win32"
1865 then
1866 try Sys.getenv "HOMEPATH" with Not_found -> ""
1867 else
1868 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1870 Filename.concat home "llpp"
1872 let pstate =
1874 let ic = open_in_bin statepath in
1875 let hash = input_value ic in
1876 close_in ic;
1877 hash
1878 with exn ->
1879 if false
1880 then
1881 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1883 Hashtbl.create 1
1885 let savestate () =
1887 let w, h =
1888 match state.fullscreen with
1889 | None -> state.winw, state.h
1890 | Some wh -> wh
1892 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1893 let oc = open_out_bin statepath in
1894 output_value oc pstate
1895 with exn ->
1896 if false
1897 then
1898 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1901 let setstate () =
1903 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1904 state.w <- statew;
1905 state.h <- stateh;
1906 state.bookmarks <- statebookmarks;
1907 with Not_found -> ()
1908 | exn ->
1909 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1912 Arg.parse
1913 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
1914 (fun s -> state.path <- s)
1915 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
1917 let name =
1918 if String.length state.path = 0
1919 then (prerr_endline "filename missing"; exit 1)
1920 else state.path
1923 setstate ();
1924 let _ = Glut.init Sys.argv in
1925 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1926 let () = Glut.initWindowSize state.w state.h in
1927 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1929 let csock, ssock =
1930 if Sys.os_type = "Unix"
1931 then
1932 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1933 else
1934 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1935 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1936 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1937 Unix.bind sock addr;
1938 Unix.listen sock 1;
1939 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1940 Unix.connect csock addr;
1941 let ssock, _ = Unix.accept sock in
1942 Unix.close sock;
1943 let opts sock =
1944 Unix.setsockopt sock Unix.TCP_NODELAY true;
1945 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1947 opts ssock;
1948 opts csock;
1949 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1950 ssock, csock
1953 let () = Glut.displayFunc display in
1954 let () = Glut.reshapeFunc reshape in
1955 let () = Glut.keyboardFunc keyboard in
1956 let () = Glut.specialFunc special in
1957 let () = Glut.idleFunc (Some idle) in
1958 let () = Glut.mouseFunc mouse in
1959 let () = Glut.motionFunc motion in
1960 let () = Glut.passiveMotionFunc pmotion in
1962 init ssock;
1963 state.csock <- csock;
1964 state.ssock <- ssock;
1965 state.text <- "Opening " ^ name;
1966 writecmd state.csock ("open " ^ state.path ^ "\000" ^ state.password ^ "\000");
1968 at_exit savestate;
1970 let rec handlelablglutbug () =
1972 Glut.mainLoop ();
1973 with Glut.BadEnum "key in special_of_int" ->
1974 showtext '!' " LablGlut bug: special key not recognized";
1975 handlelablglutbug ()
1977 handlelablglutbug ();