Properly handle throttled setzoom
[llpp.git] / main.ml
blobaf3c14ce99aba902e7f6a83b10baad5f196492c8
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
9 let now = Unix.gettimeofday;;
11 exception Quit;;
13 type params = (angle * proportional * trimparams
14 * texcount * sliceheight * memsize
15 * colorspace * wmclasshack * fontpath)
16 and pageno = int
17 and width = int
18 and height = int
19 and leftx = int
20 and opaque = string
21 and recttype = int
22 and pixmapsize = int
23 and angle = int
24 and proportional = bool
25 and trimmargins = bool
26 and interpagespace = int
27 and texcount = int
28 and sliceheight = int
29 and gen = int
30 and top = float
31 and fontpath = string
32 and memsize = int
33 and aalevel = int
34 and wmclasshack = bool
35 and irect = (int * int * int * int)
36 and trimparams = (trimmargins * irect)
37 and colorspace = | Rgb | Bgr | Gray
40 type platform = | Punknown | Plinux | Pwindows | Posx | Psun
41 | Pfreebsd | Pdragonflybsd | Popenbsd | Pmingw | Pcygwin;;
43 external init : Unix.file_descr -> params -> unit = "ml_init";;
44 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
45 external copysel : string -> unit = "ml_copysel";;
46 external getpdimrect : int -> float array = "ml_getpdimrect";;
47 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
48 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
49 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
50 external measurestr : int -> string -> float = "ml_measure_string";;
51 external getmaxw : unit -> float = "ml_getmaxw";;
52 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
53 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
54 external platform : unit -> platform = "ml_platform";;
55 external setaalevel : int -> unit = "ml_setaalevel";;
57 let platform_to_string = function
58 | Punknown -> "unknown"
59 | Plinux -> "Linux"
60 | Pwindows -> "Windows"
61 | Posx -> "OSX"
62 | Psun -> "Sun"
63 | Pfreebsd -> "FreeBSD"
64 | Pdragonflybsd -> "DragonflyBSD"
65 | Popenbsd -> "OpenBSD"
66 | Pcygwin -> "Cygwin"
67 | Pmingw -> "MingW"
70 let platform = platform ();;
72 let is_windows =
73 match platform with
74 | Pwindows | Pmingw -> true
75 | _ -> false
78 type x = int
79 and y = int
80 and tilex = int
81 and tiley = int
82 and tileparams = (x * y * width * height * tilex * tiley)
85 external drawtile : tileparams -> string -> unit = "ml_drawtile";;
87 type mpos = int * int
88 and mstate =
89 | Msel of (mpos * mpos)
90 | Mpan of mpos
91 | Mscrolly | Mscrollx
92 | Mzoom of (int * int)
93 | Mzoomrect of (mpos * mpos)
94 | Mnone
97 type textentry = string * string * onhist option * onkey * ondone
98 and onkey = string -> int -> te
99 and ondone = string -> unit
100 and histcancel = unit -> unit
101 and onhist = ((histcmd -> string) * histcancel)
102 and histcmd = HCnext | HCprev | HCfirst | HClast
103 and te =
104 | TEstop
105 | TEdone of string
106 | TEcont of string
107 | TEswitch of textentry
110 type 'a circbuf =
111 { store : 'a array
112 ; mutable rc : int
113 ; mutable wc : int
114 ; mutable len : int
118 let bound v minv maxv =
119 max minv (min maxv v);
122 let cbnew n v =
123 { store = Array.create n v
124 ; rc = 0
125 ; wc = 0
126 ; len = 0
130 let drawstring size x y s =
131 Gl.enable `blend;
132 Gl.enable `texture_2d;
133 ignore (drawstr size x y s);
134 Gl.disable `blend;
135 Gl.disable `texture_2d;
138 let drawstring1 size x y s =
139 drawstr size x y s;
142 let drawstring2 size x y fmt =
143 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
146 let cbcap b = Array.length b.store;;
148 let cbput b v =
149 let cap = cbcap b in
150 b.store.(b.wc) <- v;
151 b.wc <- (b.wc + 1) mod cap;
152 b.rc <- b.wc;
153 b.len <- min (b.len + 1) cap;
156 let cbempty b = b.len = 0;;
158 let cbgetg b circular dir =
159 if cbempty b
160 then b.store.(0)
161 else
162 let rc = b.rc + dir in
163 let rc =
164 if circular
165 then (
166 if rc = -1
167 then b.len-1
168 else (
169 if rc = b.len
170 then 0
171 else rc
174 else max 0 (min rc (b.len-1))
176 b.rc <- rc;
177 b.store.(rc);
180 let cbget b = cbgetg b false;;
181 let cbgetc b = cbgetg b true;;
183 type page =
184 { pageno : int
185 ; pagedimno : int
186 ; pagew : int
187 ; pageh : int
188 ; pagex : int
189 ; pagey : int
190 ; pagevw : int
191 ; pagevh : int
192 ; pagedispx : int
193 ; pagedispy : int
197 let debugl l =
198 dolog "l %d dim=%d {" l.pageno l.pagedimno;
199 dolog " WxH %dx%d" l.pagew l.pageh;
200 dolog " vWxH %dx%d" l.pagevw l.pagevh;
201 dolog " pagex,y %d,%d" l.pagex l.pagey;
202 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
203 dolog "}";
206 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
207 dolog "rect {";
208 dolog " x0,y0=(% f, % f)" x0 y0;
209 dolog " x1,y1=(% f, % f)" x1 y1;
210 dolog " x2,y2=(% f, % f)" x2 y2;
211 dolog " x3,y3=(% f, % f)" x3 y3;
212 dolog "}";
215 type conf =
216 { mutable scrollbw : int
217 ; mutable scrollh : int
218 ; mutable icase : bool
219 ; mutable preload : bool
220 ; mutable pagebias : int
221 ; mutable verbose : bool
222 ; mutable debug : bool
223 ; mutable scrollstep : int
224 ; mutable maxhfit : bool
225 ; mutable crophack : bool
226 ; mutable autoscrollstep : int
227 ; mutable maxwait : float option
228 ; mutable hlinks : bool
229 ; mutable underinfo : bool
230 ; mutable interpagespace : interpagespace
231 ; mutable zoom : float
232 ; mutable presentation : bool
233 ; mutable angle : angle
234 ; mutable winw : int
235 ; mutable winh : int
236 ; mutable savebmarks : bool
237 ; mutable proportional : proportional
238 ; mutable trimmargins : trimmargins
239 ; mutable trimfuzz : irect
240 ; mutable memlimit : memsize
241 ; mutable texcount : texcount
242 ; mutable sliceheight : sliceheight
243 ; mutable thumbw : width
244 ; mutable jumpback : bool
245 ; mutable bgcolor : float * float * float
246 ; mutable bedefault : bool
247 ; mutable scrollbarinpm : bool
248 ; mutable tilew : int
249 ; mutable tileh : int
250 ; mutable mumemlimit : memsize
251 ; mutable checkers : bool
252 ; mutable aalevel : int
253 ; mutable urilauncher : string
254 ; mutable colorspace : colorspace
255 ; mutable invert : bool
256 ; mutable colorscale : float
260 type anchor = pageno * top;;
262 type outline = string * int * anchor;;
264 type rect = float * float * float * float * float * float * float * float;;
266 type tile = opaque * pixmapsize * elapsed
267 and elapsed = float;;
268 type pagemapkey = pageno * gen;;
269 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
270 and row = int
271 and col = int;;
273 let emptyanchor = (0, 0.0);;
275 type infochange = | Memused | Docinfo | Pdim;;
277 class type uioh = object
278 method display : unit
279 method key : int -> uioh
280 method special : Glut.special_key_t -> uioh
281 method button :
282 Glut.button_t -> Glut.mouse_button_state_t -> int -> int -> uioh
283 method motion : int -> int -> uioh
284 method pmotion : int -> int -> uioh
285 method infochanged : infochange -> unit
286 end;;
288 type mode =
289 | Birdseye of (conf * leftx * pageno * pageno * anchor)
290 | Textentry of (textentry * onleave)
291 | View
292 and onleave = leavetextentrystatus -> unit
293 and leavetextentrystatus = | Cancel | Confirm
294 and helpitem = string * int * action
295 and action =
296 | Noaction
297 | Action of (uioh -> uioh)
300 let isbirdseye = function Birdseye _ -> true | _ -> false;;
301 let istextentry = function Textentry _ -> true | _ -> false;;
303 type currently =
304 | Idle
305 | Loading of (page * gen)
306 | Tiling of (
307 page * opaque * colorspace * angle * gen * col * row * width * height
309 | Outlining of outline list
312 let nouioh : uioh = object (self)
313 method display = ()
314 method key _ = self
315 method special _ = self
316 method button _ _ _ _ = self
317 method motion _ _ = self
318 method pmotion _ _ = self
319 method infochanged _ = ()
320 end;;
322 type state =
323 { mutable csock : Unix.file_descr
324 ; mutable ssock : Unix.file_descr
325 ; mutable w : int
326 ; mutable x : int
327 ; mutable y : int
328 ; mutable scrollw : int
329 ; mutable hscrollh : int
330 ; mutable anchor : anchor
331 ; mutable maxy : int
332 ; mutable layout : page list
333 ; pagemap : (pagemapkey, opaque) Hashtbl.t
334 ; tilemap : (tilemapkey, tile) Hashtbl.t
335 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
336 ; mutable pdims : (pageno * width * height * leftx) list
337 ; mutable pagecount : int
338 ; mutable currently : currently
339 ; mutable mstate : mstate
340 ; mutable searchpattern : string
341 ; mutable rects : (pageno * recttype * rect) list
342 ; mutable rects1 : (pageno * recttype * rect) list
343 ; mutable text : string
344 ; mutable fullscreen : (width * height) option
345 ; mutable mode : mode
346 ; mutable uioh : uioh
347 ; mutable outlines : outline array
348 ; mutable bookmarks : outline list
349 ; mutable path : string
350 ; mutable password : string
351 ; mutable invalidated : int
352 ; mutable memused : memsize
353 ; mutable gen : gen
354 ; mutable throttle : (page list * int * float) option
355 ; mutable autoscroll : int option
356 ; mutable help : helpitem array
357 ; mutable docinfo : (int * string) list
358 ; mutable deadline : float
359 ; mutable texid : GlTex.texture_id option
360 ; hists : hists
361 ; mutable prevzoom : float
362 ; mutable progress : float
364 and hists =
365 { pat : string circbuf
366 ; pag : string circbuf
367 ; nav : anchor circbuf
371 let defconf =
372 { scrollbw = 7
373 ; scrollh = 12
374 ; icase = true
375 ; preload = true
376 ; pagebias = 0
377 ; verbose = false
378 ; debug = false
379 ; scrollstep = 24
380 ; maxhfit = true
381 ; crophack = false
382 ; autoscrollstep = 2
383 ; maxwait = None
384 ; hlinks = false
385 ; underinfo = false
386 ; interpagespace = 2
387 ; zoom = 1.0
388 ; presentation = false
389 ; angle = 0
390 ; winw = 900
391 ; winh = 900
392 ; savebmarks = true
393 ; proportional = true
394 ; trimmargins = false
395 ; trimfuzz = (0,0,0,0)
396 ; memlimit = 32 lsl 20
397 ; texcount = 256
398 ; sliceheight = 24
399 ; thumbw = 76
400 ; jumpback = true
401 ; bgcolor = (0.5, 0.5, 0.5)
402 ; bedefault = false
403 ; scrollbarinpm = true
404 ; tilew = 2048
405 ; tileh = 2048
406 ; mumemlimit = 128 lsl 20
407 ; checkers = true
408 ; aalevel = 8
409 ; urilauncher =
410 (match platform with
411 | Plinux | Pfreebsd | Pdragonflybsd | Popenbsd | Psun -> "xdg-open \"%s\""
412 | Posx -> "open \"%s\""
413 | Pwindows | Pcygwin | Pmingw -> "iexplore \"%s\""
414 | _ -> "")
415 ; colorspace = Rgb
416 ; invert = false
417 ; colorscale = 1.0
421 let conf = { defconf with angle = defconf.angle };;
423 type fontstate =
424 { mutable fontsize : int
425 ; mutable wwidth : float
426 ; mutable maxrows : int
430 let fstate =
431 { fontsize = 14
432 ; wwidth = nan
433 ; maxrows = -1
437 let setfontsize n =
438 fstate.fontsize <- n;
439 fstate.wwidth <- measurestr fstate.fontsize "w";
440 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
443 let gotouri uri =
444 if String.length conf.urilauncher = 0
445 then print_endline uri
446 else
447 let re = Str.regexp "%s" in
448 let command = Str.global_replace re uri conf.urilauncher in
449 let optic =
450 try Some (Unix.open_process_in command)
451 with exn ->
452 Printf.eprintf
453 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
454 flush stderr;
455 None
457 match optic with
458 | Some ic -> close_in ic
459 | None -> ()
462 let makehelp () =
463 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
464 Array.of_list (
465 let r = Str.regexp "\\(http://[^ ]+\\)" in
466 List.map (fun s ->
467 if (try Str.search_forward r s 0 with Not_found -> -1) >= 0
468 then
469 let uri = Str.matched_string s in
470 (s, 0, Action (fun u -> gotouri uri; u))
471 else s, 0, Noaction) strings
475 let state =
476 { csock = Unix.stdin
477 ; ssock = Unix.stdin
478 ; x = 0
479 ; y = 0
480 ; w = 0
481 ; scrollw = 0
482 ; hscrollh = 0
483 ; anchor = emptyanchor
484 ; layout = []
485 ; maxy = max_int
486 ; tilelru = Queue.create ()
487 ; pagemap = Hashtbl.create 10
488 ; tilemap = Hashtbl.create 10
489 ; pdims = []
490 ; pagecount = 0
491 ; currently = Idle
492 ; mstate = Mnone
493 ; rects = []
494 ; rects1 = []
495 ; text = ""
496 ; mode = View
497 ; fullscreen = None
498 ; searchpattern = ""
499 ; outlines = [||]
500 ; bookmarks = []
501 ; path = ""
502 ; password = ""
503 ; invalidated = 0
504 ; hists =
505 { nav = cbnew 10 (0, 0.0)
506 ; pat = cbnew 1 ""
507 ; pag = cbnew 1 ""
509 ; memused = 0
510 ; gen = 0
511 ; throttle = None
512 ; autoscroll = None
513 ; help = makehelp ()
514 ; docinfo = []
515 ; deadline = nan
516 ; texid = None
517 ; prevzoom = 1.0
518 ; progress = -1.0
519 ; uioh = nouioh
523 let vlog fmt =
524 if conf.verbose
525 then
526 Printf.kprintf prerr_endline fmt
527 else
528 Printf.kprintf ignore fmt
531 module G =
532 struct
533 let postRedisplay who =
534 if conf.verbose
535 then prerr_endline ("redisplay for " ^ who);
536 Glut.postRedisplay ();
538 end;;
540 let addchar s c =
541 let b = Buffer.create (String.length s + 1) in
542 Buffer.add_string b s;
543 Buffer.add_char b c;
544 Buffer.contents b;
547 let colorspace_of_string s =
548 match String.lowercase s with
549 | "rgb" -> Rgb
550 | "bgr" -> Bgr
551 | "gray" -> Gray
552 | _ -> failwith "invalid colorspace"
555 let int_of_colorspace = function
556 | Rgb -> 0
557 | Bgr -> 1
558 | Gray -> 2
561 let colorspace_of_int = function
562 | 0 -> Rgb
563 | 1 -> Bgr
564 | 2 -> Gray
565 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
568 let colorspace_to_string = function
569 | Rgb -> "rgb"
570 | Bgr -> "bgr"
571 | Gray -> "gray"
574 let intentry_with_suffix text key =
575 let c = Char.unsafe_chr key in
576 match Char.lowercase c with
577 | '0' .. '9' ->
578 let text = addchar text c in
579 TEcont text
581 | 'k' | 'm' | 'g' ->
582 let text = addchar text c in
583 TEcont text
585 | _ ->
586 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
587 TEcont text
590 let writecmd fd s =
591 let len = String.length s in
592 let n = 4 + len in
593 let b = Buffer.create n in
594 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
595 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
596 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
597 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
598 Buffer.add_string b s;
599 let s' = Buffer.contents b in
600 let n' = Unix.write fd s' 0 n in
601 if n' != n then failwith "write failed";
604 let readcmd fd =
605 let s = "xxxx" in
606 let n = Unix.read fd s 0 4 in
607 if n != 4 then failwith "incomplete read(len)";
608 let len = 0
609 lor (Char.code s.[0] lsl 24)
610 lor (Char.code s.[1] lsl 16)
611 lor (Char.code s.[2] lsl 8)
612 lor (Char.code s.[3] lsl 0)
614 let s = String.create len in
615 let n = Unix.read fd s 0 len in
616 if n != len then failwith "incomplete read(data)";
620 let makecmd s l =
621 let b = Buffer.create 10 in
622 Buffer.add_string b s;
623 let rec combine = function
624 | [] -> b
625 | x :: xs ->
626 Buffer.add_char b ' ';
627 let s =
628 match x with
629 | `b b -> if b then "1" else "0"
630 | `s s -> s
631 | `i i -> string_of_int i
632 | `f f -> string_of_float f
633 | `I f -> string_of_int (truncate f)
635 Buffer.add_string b s;
636 combine xs;
638 combine l;
641 let wcmd s l =
642 let cmd = Buffer.contents (makecmd s l) in
643 writecmd state.csock cmd;
646 let calcips h =
647 if conf.presentation
648 then
649 let d = conf.winh - h in
650 max 0 ((d + 1) / 2)
651 else
652 conf.interpagespace
655 let calcheight () =
656 let rec f pn ph pi fh l =
657 match l with
658 | (n, _, h, _) :: rest ->
659 let ips = calcips h in
660 let fh =
661 if conf.presentation
662 then fh+ips
663 else (
664 if isbirdseye state.mode && pn = 0
665 then fh + ips
666 else fh
669 let fh = fh + ((n - pn) * (ph + pi)) in
670 f n h ips fh rest;
672 | [] ->
673 let inc =
674 if conf.presentation || (isbirdseye state.mode && pn = 0)
675 then 0
676 else -pi
678 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
679 max 0 fh
681 let fh = f 0 0 0 0 state.pdims in
685 let getpageyh pageno =
686 let rec f pn ph pi y l =
687 match l with
688 | (n, _, h, _) :: rest ->
689 let ips = calcips h in
690 if n >= pageno
691 then
692 let h = if n = pageno then h else ph in
693 if conf.presentation && n = pageno
694 then
695 y + (pageno - pn) * (ph + pi) + pi, h
696 else
697 y + (pageno - pn) * (ph + pi), h
698 else
699 let y = y + (if conf.presentation then pi else 0) in
700 let y = y + (n - pn) * (ph + pi) in
701 f n h ips y rest
703 | [] ->
704 y + (pageno - pn) * (ph + pi), ph
706 f 0 0 0 0 state.pdims
709 let getpagedim pageno =
710 let rec f ppdim l =
711 match l with
712 | (n, _, _, _) as pdim :: rest ->
713 if n >= pageno
714 then (if n = pageno then pdim else ppdim)
715 else f pdim rest
717 | [] -> ppdim
719 f (-1, -1, -1, -1) state.pdims
722 let getpageh pageno =
723 let _, _, h, _ = getpagedim pageno in
727 let getpagew pageno =
728 let _, w, _, _ = getpagedim pageno in
732 let getpagey pageno = fst (getpageyh pageno);;
734 let layout y sh =
735 let sh = sh - state.hscrollh in
736 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
737 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
738 match pdims with
739 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
740 let ips = calcips h in
741 let yinc =
742 if conf.presentation || (isbirdseye state.mode && pageno = 0)
743 then ips
744 else 0
746 (w, h, ips, xoff), rest, pdimno + 1, yinc
747 | _ ->
748 prev, pdims, pdimno, 0
750 let dy = dy + yinc in
751 let py = py + yinc in
752 if pageno = state.pagecount || dy >= sh
753 then
754 accu
755 else
756 let vy = y + dy in
757 if py + h <= vy - yinc
758 then
759 let py = py + h + ips in
760 let dy = max 0 (py - y) in
761 f ~pageno:(pageno+1)
762 ~pdimno
763 ~prev:curr
766 ~pdims:rest
767 ~accu
768 else
769 let pagey = vy - py in
770 let pagevh = h - pagey in
771 let pagevh = min (sh - dy) pagevh in
772 let off = if yinc > 0 then py - vy else 0 in
773 let py = py + h + ips in
774 let pagex, dx =
775 let xoff = xoff +
776 if state.w < conf.winw - state.scrollw
777 then (conf.winw - state.scrollw - state.w) / 2
778 else 0
780 let dispx = xoff + state.x in
781 if dispx < 0
782 then (-dispx, 0)
783 else (0, dispx)
785 let pagevw =
786 let lw = w - pagex in
787 min lw (conf.winw - state.scrollw)
789 let e =
790 { pageno = pageno
791 ; pagedimno = pdimno
792 ; pagew = w
793 ; pageh = h
794 ; pagex = pagex
795 ; pagey = pagey + off
796 ; pagevw = pagevw
797 ; pagevh = pagevh - off
798 ; pagedispx = dx
799 ; pagedispy = dy + off
802 let accu = e :: accu in
803 f ~pageno:(pageno+1)
804 ~pdimno
805 ~prev:curr
807 ~dy:(dy+pagevh+ips)
808 ~pdims:rest
809 ~accu
811 if state.invalidated = 0
812 then (
813 let accu =
815 ~pageno:0
816 ~pdimno:~-1
817 ~prev:(0,0,0,0)
818 ~py:0
819 ~dy:0
820 ~pdims:state.pdims
821 ~accu:[]
823 List.rev accu
825 else
829 let clamp incr =
830 let y = state.y + incr in
831 let y = max 0 y in
832 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
836 let getopaque pageno =
837 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
838 with Not_found -> None
841 let putopaque pageno opaque =
842 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
845 let itertiles l f =
846 let tilex = l.pagex mod conf.tilew in
847 let tiley = l.pagey mod conf.tileh in
849 let col = l.pagex / conf.tilew in
850 let row = l.pagey / conf.tileh in
852 let vw =
853 let a = l.pagew - l.pagex in
854 let b = conf.winw - state.scrollw in
855 min a b
856 and vh = l.pagevh in
858 let rec rowloop row y0 dispy h =
859 if h = 0
860 then ()
861 else (
862 let dh = conf.tileh - y0 in
863 let dh = min h dh in
864 let rec colloop col x0 dispx w =
865 if w = 0
866 then ()
867 else (
868 let dw = conf.tilew - x0 in
869 let dw = min w dw in
871 f col row dispx dispy x0 y0 dw dh;
872 colloop (col+1) 0 (dispx+dw) (w-dw)
875 colloop col tilex l.pagedispx vw;
876 rowloop (row+1) 0 (dispy+dh) (h-dh)
879 if vw > 0 && vh > 0
880 then rowloop row tiley l.pagedispy vh;
883 let gettileopaque l col row =
884 let key =
885 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
887 try Some (Hashtbl.find state.tilemap key)
888 with Not_found -> None
891 let puttileopaque l col row gen colorspace angle opaque size elapsed =
892 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
893 Hashtbl.add state.tilemap key (opaque, size, elapsed)
896 let drawtiles l color =
897 GlDraw.color color;
898 let f col row x y tilex tiley w h =
899 match gettileopaque l col row with
900 | Some (opaque, _, t) ->
901 let params = x, y, w, h, tilex, tiley in
902 if conf.invert
903 then (
904 Gl.enable `blend;
905 GlFunc.blend_func `zero `one_minus_src_color;
907 drawtile params opaque;
908 if conf.invert
909 then Gl.disable `blend;
910 if conf.debug
911 then (
912 let s = Printf.sprintf
913 "%d[%d,%d] %f sec"
914 l.pageno col row t
916 let ww = fstate.wwidth in
917 GlMisc.push_attrib [`current];
918 GlDraw.color (0.0, 0.0, 0.0);
919 GlDraw.rect
920 (float (x-2), float (y-2))
921 (float (x+2) +. ww, float (y + fstate.fontsize + 2));
922 GlDraw.color (1.0, 1.0, 1.0);
923 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
924 GlMisc.pop_attrib ();
927 | _ ->
928 let w =
929 let lw = conf.winw - state.scrollw - x in
930 min lw w
931 and h =
932 let lh = conf.winh - y in
933 min lh h
935 Gl.enable `texture_2d;
936 begin match state.texid with
937 | Some id ->
938 GlTex.bind_texture `texture_2d id;
939 let x0 = float x
940 and y0 = float y
941 and x1 = float (x+w)
942 and y1 = float (y+h) in
944 let tw = float w /. 64.0
945 and th = float h /. 64.0 in
946 let tx0 = float tilex /. 64.0
947 and ty0 = float tiley /. 64.0 in
948 let tx1 = tx0 +. tw
949 and ty1 = ty0 +. th in
950 GlDraw.begins `quads;
951 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
952 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
953 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
954 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
955 GlDraw.ends ();
957 Gl.disable `texture_2d;
958 | None ->
959 GlDraw.color (1.0, 1.0, 1.0);
960 GlDraw.rect
961 (float x, float y)
962 (float (x+w), float (y+h));
963 end;
964 if w > 128 && h > fstate.fontsize + 10
965 then (
966 GlDraw.color (0.0, 0.0, 0.0);
967 let c, r =
968 if conf.verbose
969 then (col*conf.tilew, row*conf.tileh)
970 else col, row
972 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
974 GlDraw.color color;
976 itertiles l f
979 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
981 let tilevisible1 l x y =
982 let ax0 = l.pagex
983 and ax1 = l.pagex + l.pagevw
984 and ay0 = l.pagey
985 and ay1 = l.pagey + l.pagevh in
987 let bx0 = x
988 and by0 = y in
989 let bx1 = min (bx0 + conf.tilew) l.pagew
990 and by1 = min (by0 + conf.tileh) l.pageh in
992 let rx0 = max ax0 bx0
993 and ry0 = max ay0 by0
994 and rx1 = min ax1 bx1
995 and ry1 = min ay1 by1 in
997 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
998 nonemptyintersection
1001 let tilevisible layout n x y =
1002 let rec findpageinlayout = function
1003 | l :: _ when l.pageno = n -> tilevisible1 l x y
1004 | _ :: rest -> findpageinlayout rest
1005 | [] -> false
1007 findpageinlayout layout
1010 let tileready l x y =
1011 tilevisible1 l x y &&
1012 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1015 let tilepage n p layout =
1016 let rec loop = function
1017 | l :: rest ->
1018 if l.pageno = n
1019 then
1020 let f col row _ _ _ _ _ _ =
1021 if state.currently = Idle
1022 then
1023 match gettileopaque l col row with
1024 | Some _ -> ()
1025 | None ->
1026 let x = col*conf.tilew
1027 and y = row*conf.tileh in
1028 let w =
1029 let w = l.pagew - x in
1030 min w conf.tilew
1032 let h =
1033 let h = l.pageh - y in
1034 min h conf.tileh
1036 wcmd "tile"
1037 [`s p
1038 ;`i x
1039 ;`i y
1040 ;`i w
1041 ;`i h
1043 state.currently <-
1044 Tiling (
1045 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1046 conf.tilew, conf.tileh
1049 itertiles l f;
1050 else
1051 loop rest
1053 | [] -> ()
1055 if state.invalidated = 0 then loop layout;
1058 let preloadlayout visiblepages =
1059 let presentation = conf.presentation in
1060 let interpagespace = conf.interpagespace in
1061 let maxy = state.maxy in
1062 conf.presentation <- false;
1063 conf.interpagespace <- 0;
1064 state.maxy <- calcheight ();
1065 let y =
1066 match visiblepages with
1067 | [] -> 0
1068 | l :: _ -> getpagey l.pageno + l.pagey
1070 let y = if y < conf.winh then 0 else y - conf.winh in
1071 let h = state.y - y + conf.winh*3 in
1072 let pages = layout y h in
1073 conf.presentation <- presentation;
1074 conf.interpagespace <- interpagespace;
1075 state.maxy <- maxy;
1076 pages
1079 let load pages =
1080 let rec loop pages =
1081 if state.currently != Idle
1082 then ()
1083 else
1084 match pages with
1085 | l :: rest ->
1086 begin match getopaque l.pageno with
1087 | None ->
1088 wcmd "page" [`i l.pageno; `i l.pagedimno];
1089 state.currently <- Loading (l, state.gen);
1090 | Some opaque ->
1091 tilepage l.pageno opaque pages;
1092 loop rest
1093 end;
1094 | _ -> ()
1096 if state.invalidated = 0 then loop pages
1099 let preload pages =
1100 load pages;
1101 if conf.preload && state.currently = Idle
1102 then load (preloadlayout pages);
1105 let layoutready layout =
1106 let rec fold all ls =
1107 all && match ls with
1108 | l :: rest ->
1109 let seen = ref false in
1110 let allvisible = ref true in
1111 let foo col row _ _ _ _ _ _ =
1112 seen := true;
1113 allvisible := !allvisible &&
1114 begin match gettileopaque l col row with
1115 | Some _ -> true
1116 | None -> false
1119 itertiles l foo;
1120 fold (!seen && !allvisible) rest
1121 | [] -> true
1123 let alltilesvisible = fold true layout in
1124 alltilesvisible;
1127 let gotoy y =
1128 let y = bound y 0 state.maxy in
1129 let y, layout, proceed =
1130 match conf.maxwait with
1131 | Some time ->
1132 begin match state.throttle with
1133 | None ->
1134 let layout = layout y conf.winh in
1135 let ready = layoutready layout in
1136 if not ready
1137 then (
1138 load layout;
1139 state.throttle <- Some (layout, y, now ());
1141 else G.postRedisplay "gotoy showall (None)";
1142 y, layout, ready
1143 | Some (_, _, started) ->
1144 let dt = now () -. started in
1145 if dt > time
1146 then (
1147 state.throttle <- None;
1148 let layout = layout y conf.winh in
1149 load layout;
1150 G.postRedisplay "maxwait";
1151 y, layout, true
1153 else -1, [], false
1156 | None ->
1157 let layout = layout y conf.winh in
1158 if true || layoutready layout
1159 then G.postRedisplay "gotoy ready";
1160 y, layout, true
1162 if proceed
1163 then (
1164 state.y <- y;
1165 state.layout <- layout;
1166 begin match state.mode with
1167 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1168 if not (pagevisible layout pageno)
1169 then (
1170 match state.layout with
1171 | [] -> ()
1172 | l :: _ ->
1173 state.mode <- Birdseye (
1174 conf, leftx, l.pageno, hooverpageno, anchor
1177 | _ -> ()
1178 end;
1179 preload layout;
1183 let conttiling pageno opaque =
1184 tilepage pageno opaque
1185 (if conf.preload then preloadlayout state.layout else state.layout)
1188 let gotoy_and_clear_text y =
1189 gotoy y;
1190 if not conf.verbose then state.text <- "";
1193 let getanchor () =
1194 match state.layout with
1195 | [] -> emptyanchor
1196 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1199 let getanchory (n, top) =
1200 let y, h = getpageyh n in
1201 y + (truncate (top *. float h));
1204 let gotoanchor anchor =
1205 gotoy (getanchory anchor);
1208 let addnav () =
1209 cbput state.hists.nav (getanchor ());
1212 let getnav dir =
1213 let anchor = cbgetc state.hists.nav dir in
1214 getanchory anchor;
1217 let gotopage n top =
1218 let y, h = getpageyh n in
1219 gotoy_and_clear_text (y + (truncate (top *. float h)));
1222 let gotopage1 n top =
1223 let y = getpagey n in
1224 gotoy_and_clear_text (y + top);
1227 let invalidate () =
1228 state.layout <- [];
1229 state.pdims <- [];
1230 state.rects <- [];
1231 state.rects1 <- [];
1232 state.invalidated <- state.invalidated + 1;
1235 let writeopen path password =
1236 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1239 let opendoc path password =
1240 invalidate ();
1241 state.path <- path;
1242 state.password <- password;
1243 state.gen <- state.gen + 1;
1244 state.docinfo <- [];
1246 setaalevel conf.aalevel;
1247 writeopen path password;
1248 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1249 wcmd "geometry" [`i state.w; `i conf.winh];
1252 let scalecolor c =
1253 let c = c *. conf.colorscale in
1254 (c, c, c);
1257 let scalecolor2 (r, g, b) =
1258 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1261 let represent () =
1262 state.maxy <- calcheight ();
1263 state.hscrollh <-
1264 if state.w <= conf.winw - state.scrollw
1265 then 0
1266 else state.scrollw
1268 match state.mode with
1269 | Birdseye (_, _, pageno, _, _) ->
1270 let y, h = getpageyh pageno in
1271 let top = (conf.winh - h) / 2 in
1272 gotoy (max 0 (y - top))
1273 | _ -> gotoanchor state.anchor
1276 let reshape =
1277 let firsttime = ref true in
1278 fun ~w ~h ->
1279 GlDraw.viewport 0 0 w h;
1280 if state.invalidated = 0 && not !firsttime
1281 then state.anchor <- getanchor ();
1283 firsttime := false;
1284 conf.winw <- w;
1285 let w = truncate (float w *. conf.zoom) - state.scrollw in
1286 let w = max w 2 in
1287 state.w <- w;
1288 conf.winh <- h;
1289 setfontsize fstate.fontsize;
1290 GlMat.mode `modelview;
1291 GlMat.load_identity ();
1293 GlMat.mode `projection;
1294 GlMat.load_identity ();
1295 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1296 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1297 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1299 invalidate ();
1300 wcmd "geometry" [`i w; `i h];
1303 let enttext () =
1304 let len = String.length state.text in
1305 let drawstring s =
1306 let hscrollh =
1307 match state.mode with
1308 | View -> state.hscrollh
1309 | _ -> 0
1311 let rect x w =
1312 GlDraw.rect
1313 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1314 (x+.w, float (conf.winh - hscrollh))
1317 let w = float (conf.winw - state.scrollw - 1) in
1318 if state.progress >= 0.0 && state.progress < 1.0
1319 then (
1320 GlDraw.color (0.3, 0.3, 0.3);
1321 let w1 = w *. state.progress in
1322 rect 0.0 w1;
1323 GlDraw.color (0.0, 0.0, 0.0);
1324 rect w1 (w-.w1)
1326 else (
1327 GlDraw.color (0.0, 0.0, 0.0);
1328 rect 0.0 w;
1331 GlDraw.color (1.0, 1.0, 1.0);
1332 drawstring fstate.fontsize
1333 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1335 match state.mode with
1336 | Textentry ((prefix, text, _, _, _), _) ->
1337 let s =
1338 if len > 0
1339 then
1340 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1341 else
1342 Printf.sprintf "%s%s_" prefix text
1344 drawstring s
1346 | _ ->
1347 if len > 0 then drawstring state.text
1350 let showtext c s =
1351 state.text <- Printf.sprintf "%c%s" c s;
1352 G.postRedisplay "showtext";
1355 let gctiles () =
1356 let len = Queue.length state.tilelru in
1357 let rec loop qpos =
1358 if state.memused <= conf.memlimit
1359 then ()
1360 else (
1361 if qpos < len
1362 then
1363 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1364 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1366 gen = state.gen
1367 && colorspace = conf.colorspace
1368 && angle = conf.angle
1369 && pagew = getpagew n
1370 && pageh = getpageh n
1371 && (
1372 let layout =
1373 if conf.preload
1374 then preloadlayout state.layout
1375 else state.layout
1377 let x = col*conf.tilew
1378 and y = row*conf.tileh in
1379 tilevisible layout n x y
1381 then Queue.push lruitem state.tilelru
1382 else (
1383 wcmd "freetile" [`s p];
1384 state.memused <- state.memused - s;
1385 state.uioh#infochanged Memused;
1386 Hashtbl.remove state.tilemap k;
1388 loop (qpos+1)
1391 loop 0
1394 let flushtiles () =
1395 Queue.iter (fun (k, p, s) ->
1396 wcmd "freetile" [`s p];
1397 state.memused <- state.memused - s;
1398 state.uioh#infochanged Memused;
1399 Hashtbl.remove state.tilemap k;
1400 ) state.tilelru;
1401 Queue.clear state.tilelru;
1402 load state.layout;
1405 let logcurrently = function
1406 | Idle -> dolog "Idle"
1407 | Loading (l, gen) ->
1408 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1409 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1410 dolog
1411 "Tiling %d[%d,%d] page=%s cs=%s angle"
1412 l.pageno col row pageopaque
1413 (colorspace_to_string colorspace)
1415 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1416 angle gen conf.angle state.gen
1417 tilew tileh
1418 conf.tilew conf.tileh
1420 | Outlining _ ->
1421 dolog "outlining"
1424 let act cmds =
1425 (* dolog "%S" cmds; *)
1426 let op, args =
1427 let spacepos =
1428 try String.index cmds ' '
1429 with Not_found -> -1
1431 if spacepos = -1
1432 then cmds, ""
1433 else
1434 let l = String.length cmds in
1435 let op = String.sub cmds 0 spacepos in
1436 op, begin
1437 if l - spacepos < 2 then ""
1438 else String.sub cmds (spacepos+1) (l-spacepos-1)
1441 match op with
1442 | "clear" ->
1443 state.uioh#infochanged Pdim;
1444 state.pdims <- [];
1446 | "clearrects" ->
1447 state.rects <- state.rects1;
1448 G.postRedisplay "clearrects";
1450 | "continue" ->
1451 let n =
1452 try Scanf.sscanf args "%u" (fun n -> n)
1453 with exn ->
1454 dolog "error processing 'continue' %S: %s"
1455 cmds (Printexc.to_string exn);
1456 exit 1;
1458 state.pagecount <- n;
1459 state.invalidated <- state.invalidated - 1;
1460 begin match state.currently with
1461 | Outlining l ->
1462 state.currently <- Idle;
1463 state.outlines <- Array.of_list (List.rev l)
1464 | _ -> ()
1465 end;
1466 if state.invalidated = 0
1467 then represent ();
1468 if conf.maxwait = None
1469 then G.postRedisplay "continue";
1471 | "title" ->
1472 Glut.setWindowTitle args
1474 | "msg" ->
1475 showtext ' ' args
1477 | "vmsg" ->
1478 if conf.verbose
1479 then showtext ' ' args
1481 | "progress" ->
1482 let progress, text =
1484 Scanf.sscanf args "%f %n"
1485 (fun f pos ->
1486 f, String.sub args pos (String.length args - pos))
1487 with exn ->
1488 dolog "error processing 'progress' %S: %s"
1489 cmds (Printexc.to_string exn);
1490 exit 1;
1492 state.text <- text;
1493 state.progress <- progress;
1494 G.postRedisplay "progress"
1496 | "firstmatch" ->
1497 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1499 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1500 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1501 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1502 with exn ->
1503 dolog "error processing 'firstmatch' %S: %s"
1504 cmds (Printexc.to_string exn);
1505 exit 1;
1507 let y = (getpagey pageno) + truncate y0 in
1508 addnav ();
1509 gotoy y;
1510 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1512 | "match" ->
1513 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1515 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1516 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1517 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1518 with exn ->
1519 dolog "error processing 'match' %S: %s"
1520 cmds (Printexc.to_string exn);
1521 exit 1;
1523 state.rects1 <-
1524 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1526 | "page" ->
1527 let pageopaque, t =
1529 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1530 with exn ->
1531 dolog "error processing 'page' %S: %s"
1532 cmds (Printexc.to_string exn);
1533 exit 1;
1535 begin match state.currently with
1536 | Loading (l, gen) ->
1537 vlog "page %d took %f sec" l.pageno t;
1538 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1539 begin match state.throttle with
1540 | None ->
1541 let preloadedpages =
1542 if conf.preload
1543 then preloadlayout state.layout
1544 else state.layout
1546 let evict () =
1547 let module IntSet =
1548 Set.Make (struct type t = int let compare = (-) end) in
1549 let set =
1550 List.fold_left (fun s l -> IntSet.add l.pageno s)
1551 IntSet.empty preloadedpages
1553 let evictedpages =
1554 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1555 if not (IntSet.mem pageno set)
1556 then (
1557 wcmd "freepage" [`s opaque];
1558 key :: accu
1560 else accu
1561 ) state.pagemap []
1563 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1565 evict ();
1566 state.currently <- Idle;
1567 if gen = state.gen
1568 then (
1569 tilepage l.pageno pageopaque state.layout;
1570 load state.layout;
1571 load preloadedpages;
1572 if pagevisible state.layout l.pageno
1573 && layoutready state.layout
1574 then G.postRedisplay "page";
1577 | Some (layout, _, _) ->
1578 state.currently <- Idle;
1579 tilepage l.pageno pageopaque layout;
1580 load state.layout
1581 end;
1583 | _ ->
1584 dolog "Inconsistent loading state";
1585 logcurrently state.currently;
1586 raise Quit;
1589 | "tile" ->
1590 let (x, y, opaque, size, t) =
1592 Scanf.sscanf args "%u %u %s %u %f"
1593 (fun x y p size t -> (x, y, p, size, t))
1594 with exn ->
1595 dolog "error processing 'tile' %S: %s"
1596 cmds (Printexc.to_string exn);
1597 exit 1;
1599 begin match state.currently with
1600 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1601 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1603 if tilew != conf.tilew || tileh != conf.tileh
1604 then (
1605 wcmd "freetile" [`s opaque];
1606 state.currently <- Idle;
1607 load state.layout;
1609 else (
1610 puttileopaque l col row gen cs angle opaque size t;
1611 state.memused <- state.memused + size;
1612 state.uioh#infochanged Memused;
1613 gctiles ();
1614 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1615 opaque, size) state.tilelru;
1617 state.currently <- Idle;
1618 if gen = state.gen
1619 && conf.colorspace = cs
1620 && conf.angle = angle
1621 && tilevisible state.layout l.pageno x y
1622 then conttiling l.pageno pageopaque;
1624 begin match state.throttle with
1625 | None ->
1626 preload state.layout;
1627 if gen = state.gen
1628 && conf.colorspace = cs
1629 && conf.angle = angle
1630 && tilevisible state.layout l.pageno x y
1631 then G.postRedisplay "tile nothrottle";
1633 | Some (layout, y, _) ->
1634 let ready = layoutready layout in
1635 if ready
1636 then (
1637 state.y <- y;
1638 state.layout <- layout;
1639 state.throttle <- None;
1640 G.postRedisplay "throttle";
1642 else load layout;
1643 end;
1646 | _ ->
1647 dolog "Inconsistent tiling state";
1648 logcurrently state.currently;
1649 raise Quit;
1652 | "pdim" ->
1653 let pdim =
1655 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1656 with exn ->
1657 dolog "error processing 'pdim' %S: %s"
1658 cmds (Printexc.to_string exn);
1659 exit 1;
1661 state.uioh#infochanged Pdim;
1662 state.pdims <- pdim :: state.pdims
1664 | "o" ->
1665 let (l, n, t, h, pos) =
1667 Scanf.sscanf args "%u %u %d %u %n"
1668 (fun l n t h pos -> l, n, t, h, pos)
1669 with exn ->
1670 dolog "error processing 'o' %S: %s"
1671 cmds (Printexc.to_string exn);
1672 exit 1;
1674 let s = String.sub args pos (String.length args - pos) in
1675 let outline = (s, l, (n, float t /. float h)) in
1676 begin match state.currently with
1677 | Outlining outlines ->
1678 state.currently <- Outlining (outline :: outlines)
1679 | Idle ->
1680 state.currently <- Outlining [outline]
1681 | currently ->
1682 dolog "invalid outlining state";
1683 logcurrently currently
1686 | "info" ->
1687 state.docinfo <- (1, args) :: state.docinfo
1689 | "infoend" ->
1690 state.uioh#infochanged Docinfo;
1691 state.docinfo <- List.rev state.docinfo
1693 | _ ->
1694 dolog "unknown cmd `%S'" cmds
1697 let idle () =
1698 if state.deadline == nan then state.deadline <- now ();
1699 let rec loop delay =
1700 let timeout =
1701 if delay > 0.0
1702 then max 0.0 (state.deadline -. now ())
1703 else 0.0
1705 let r, _, _ = Unix.select [state.csock] [] [] timeout in
1706 begin match r with
1707 | [] ->
1708 begin match state.autoscroll with
1709 | Some step when step != 0 ->
1710 let y = state.y + step in
1711 let y =
1712 if y < 0
1713 then state.maxy
1714 else if y >= state.maxy then 0 else y
1716 gotoy y;
1717 if state.mode = View
1718 then state.text <- "";
1719 state.deadline <- state.deadline +. 0.005;
1721 | _ ->
1722 state.deadline <- state.deadline +. delay;
1723 end;
1725 | _ ->
1726 let cmd = readcmd state.csock in
1727 act cmd;
1728 loop 0.0
1729 end;
1730 in loop 0.007
1733 let onhist cb =
1734 let rc = cb.rc in
1735 let action = function
1736 | HCprev -> cbget cb ~-1
1737 | HCnext -> cbget cb 1
1738 | HCfirst -> cbget cb ~-(cb.rc)
1739 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1740 and cancel () = cb.rc <- rc
1741 in (action, cancel)
1744 let search pattern forward =
1745 if String.length pattern > 0
1746 then
1747 let pn, py =
1748 match state.layout with
1749 | [] -> 0, 0
1750 | l :: _ ->
1751 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1753 let cmd =
1754 let b = makecmd "search"
1755 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1757 Buffer.add_char b ',';
1758 Buffer.add_string b pattern;
1759 Buffer.add_char b '\000';
1760 Buffer.contents b;
1762 writecmd state.csock cmd;
1765 let intentry text key =
1766 let c = Char.unsafe_chr key in
1767 match c with
1768 | '0' .. '9' ->
1769 let text = addchar text c in
1770 TEcont text
1772 | _ ->
1773 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1774 TEcont text
1777 let textentry text key =
1778 let c = Char.unsafe_chr key in
1779 match c with
1780 | _ when key >= 32 && key < 127 ->
1781 let text = addchar text c in
1782 TEcont text
1784 | _ ->
1785 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1786 TEcont text
1789 let reqlayout angle proportional =
1790 match state.throttle with
1791 | None ->
1792 if state.invalidated = 0 then state.anchor <- getanchor ();
1793 conf.angle <- angle mod 360;
1794 conf.proportional <- proportional;
1795 invalidate ();
1796 wcmd "reqlayout" [`i conf.angle; `b proportional];
1797 | _ -> ()
1800 let settrim trimmargins trimfuzz =
1801 if state.invalidated = 0 then state.anchor <- getanchor ();
1802 conf.trimmargins <- trimmargins;
1803 conf.trimfuzz <- trimfuzz;
1804 let x0, y0, x1, y1 = trimfuzz in
1805 invalidate ();
1806 wcmd "settrim" [
1807 `b conf.trimmargins;
1808 `i x0;
1809 `i y0;
1810 `i x1;
1811 `i y1;
1813 Hashtbl.iter (fun _ opaque ->
1814 wcmd "freepage" [`s opaque];
1815 ) state.pagemap;
1816 Hashtbl.clear state.pagemap;
1819 let setzoom zoom =
1820 match state.throttle with
1821 | None ->
1822 let zoom = max 0.01 zoom in
1823 if zoom <> conf.zoom
1824 then (
1825 state.prevzoom <- conf.zoom;
1826 let relx =
1827 if zoom <= 1.0
1828 then (state.x <- 0; 0.0)
1829 else float state.x /. float state.w
1831 conf.zoom <- zoom;
1832 reshape conf.winw conf.winh;
1833 if zoom > 1.0
1834 then (
1835 let x = relx *. float state.w in
1836 state.x <- truncate x;
1838 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1841 | Some (layout, y, started) ->
1842 let time =
1843 match conf.maxwait with
1844 | None -> 0.0
1845 | Some t -> t
1847 let dt = now () -. started in
1848 if dt > time
1849 then (
1850 state.y <- y;
1851 load layout;
1852 G.postRedisplay "zoom maxwait";
1856 let enterbirdseye () =
1857 let zoom = float conf.thumbw /. float conf.winw in
1858 let birdseyepageno =
1859 let cy = conf.winh / 2 in
1860 let fold = function
1861 | [] -> 0
1862 | l :: rest ->
1863 let rec fold best = function
1864 | [] -> best.pageno
1865 | l :: rest ->
1866 let d = cy - (l.pagedispy + l.pagevh/2)
1867 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1868 if abs d < abs dbest
1869 then fold l rest
1870 else best.pageno
1871 in fold l rest
1873 fold state.layout
1875 state.mode <- Birdseye (
1876 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1878 conf.zoom <- zoom;
1879 conf.presentation <- false;
1880 conf.interpagespace <- 10;
1881 conf.hlinks <- false;
1882 state.x <- 0;
1883 state.mstate <- Mnone;
1884 conf.maxwait <- None;
1885 Glut.setCursor Glut.CURSOR_INHERIT;
1886 if conf.verbose
1887 then
1888 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1889 (100.0*.zoom)
1890 else
1891 state.text <- ""
1893 reshape conf.winw conf.winh;
1896 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1897 state.mode <- View;
1898 conf.zoom <- c.zoom;
1899 conf.presentation <- c.presentation;
1900 conf.interpagespace <- c.interpagespace;
1901 conf.maxwait <- c.maxwait;
1902 conf.hlinks <- c.hlinks;
1903 state.x <- leftx;
1904 if conf.verbose
1905 then
1906 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1907 (100.0*.conf.zoom)
1909 reshape conf.winw conf.winh;
1910 state.anchor <- if goback then anchor else (pageno, 0.0);
1913 let togglebirdseye () =
1914 match state.mode with
1915 | Birdseye vals -> leavebirdseye vals true
1916 | View -> enterbirdseye ()
1917 | _ -> ()
1920 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1921 let pageno = max 0 (pageno - 1) in
1922 let rec loop = function
1923 | [] -> gotopage1 pageno 0
1924 | l :: _ when l.pageno = pageno ->
1925 if l.pagedispy >= 0 && l.pagey = 0
1926 then G.postRedisplay "upbirdseye"
1927 else gotopage1 pageno 0
1928 | _ :: rest -> loop rest
1930 loop state.layout;
1931 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1934 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1935 let pageno = min (state.pagecount - 1) (pageno + 1) in
1936 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1937 let rec loop = function
1938 | [] ->
1939 let y, h = getpageyh pageno in
1940 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1941 gotoy (clamp dy)
1942 | l :: _ when l.pageno = pageno ->
1943 if l.pagevh != l.pageh
1944 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1945 else G.postRedisplay "downbirdseye"
1946 | _ :: rest -> loop rest
1948 loop state.layout
1951 let optentry mode _ key =
1952 let btos b = if b then "on" else "off" in
1953 let c = Char.unsafe_chr key in
1954 match c with
1955 | 's' ->
1956 let ondone s =
1957 try conf.scrollstep <- int_of_string s with exc ->
1958 state.text <- Printf.sprintf "bad integer `%s': %s"
1959 s (Printexc.to_string exc)
1961 TEswitch ("scroll step: ", "", None, intentry, ondone)
1963 | 'A' ->
1964 let ondone s =
1966 conf.autoscrollstep <- int_of_string s;
1967 if state.autoscroll <> None
1968 then state.autoscroll <- Some conf.autoscrollstep
1969 with exc ->
1970 state.text <- Printf.sprintf "bad integer `%s': %s"
1971 s (Printexc.to_string exc)
1973 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1975 | 'Z' ->
1976 let ondone s =
1978 let zoom = float (int_of_string s) /. 100.0 in
1979 setzoom zoom
1980 with exc ->
1981 state.text <- Printf.sprintf "bad integer `%s': %s"
1982 s (Printexc.to_string exc)
1984 TEswitch ("zoom: ", "", None, intentry, ondone)
1986 | 't' ->
1987 let ondone s =
1989 conf.thumbw <- bound (int_of_string s) 2 4096;
1990 state.text <-
1991 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1992 begin match mode with
1993 | Birdseye beye ->
1994 leavebirdseye beye false;
1995 enterbirdseye ();
1996 | _ -> ();
1998 with exc ->
1999 state.text <- Printf.sprintf "bad integer `%s': %s"
2000 s (Printexc.to_string exc)
2002 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2004 | 'R' ->
2005 let ondone s =
2006 match try
2007 Some (int_of_string s)
2008 with exc ->
2009 state.text <- Printf.sprintf "bad integer `%s': %s"
2010 s (Printexc.to_string exc);
2011 None
2012 with
2013 | Some angle -> reqlayout angle conf.proportional
2014 | None -> ()
2016 TEswitch ("rotation: ", "", None, intentry, ondone)
2018 | 'i' ->
2019 conf.icase <- not conf.icase;
2020 TEdone ("case insensitive search " ^ (btos conf.icase))
2022 | 'p' ->
2023 conf.preload <- not conf.preload;
2024 gotoy state.y;
2025 TEdone ("preload " ^ (btos conf.preload))
2027 | 'v' ->
2028 conf.verbose <- not conf.verbose;
2029 TEdone ("verbose " ^ (btos conf.verbose))
2031 | 'd' ->
2032 conf.debug <- not conf.debug;
2033 TEdone ("debug " ^ (btos conf.debug))
2035 | 'h' ->
2036 conf.maxhfit <- not conf.maxhfit;
2037 state.maxy <-
2038 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2039 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2041 | 'c' ->
2042 conf.crophack <- not conf.crophack;
2043 TEdone ("crophack " ^ btos conf.crophack)
2045 | 'a' ->
2046 let s =
2047 match conf.maxwait with
2048 | None ->
2049 conf.maxwait <- Some infinity;
2050 "always wait for page to complete"
2051 | Some _ ->
2052 conf.maxwait <- None;
2053 "show placeholder if page is not ready"
2055 TEdone s
2057 | 'f' ->
2058 conf.underinfo <- not conf.underinfo;
2059 TEdone ("underinfo " ^ btos conf.underinfo)
2061 | 'P' ->
2062 conf.savebmarks <- not conf.savebmarks;
2063 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2065 | 'S' ->
2066 let ondone s =
2068 let pageno, py =
2069 match state.layout with
2070 | [] -> 0, 0
2071 | l :: _ ->
2072 l.pageno, l.pagey
2074 conf.interpagespace <- int_of_string s;
2075 state.maxy <- calcheight ();
2076 let y = getpagey pageno in
2077 gotoy (y + py)
2078 with exc ->
2079 state.text <- Printf.sprintf "bad integer `%s': %s"
2080 s (Printexc.to_string exc)
2082 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2084 | 'l' ->
2085 reqlayout conf.angle (not conf.proportional);
2086 TEdone ("proportional display " ^ btos conf.proportional)
2088 | 'T' ->
2089 settrim (not conf.trimmargins) conf.trimfuzz;
2090 TEdone ("trim margins " ^ btos conf.trimmargins)
2092 | 'I' ->
2093 conf.invert <- not conf.invert;
2094 TEdone ("invert colors " ^ btos conf.invert)
2096 | _ ->
2097 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2098 TEstop
2101 class type lvsource = object
2102 method getitemcount : int
2103 method getitem : int -> (string * int)
2104 method hasaction : int -> bool
2105 method exit :
2106 uioh:uioh ->
2107 cancel:bool ->
2108 active:int ->
2109 first:int ->
2110 pan:int ->
2111 qsearch:string ->
2112 uioh option
2113 method getactive : int
2114 method getfirst : int
2115 method getqsearch : string
2116 method setqsearch : string -> unit
2117 method getpan : int
2118 end;;
2120 class virtual lvsourcebase = object
2121 val mutable m_active = 0
2122 val mutable m_first = 0
2123 val mutable m_qsearch = ""
2124 val mutable m_pan = 0
2125 method getactive = m_active
2126 method getfirst = m_first
2127 method getqsearch = m_qsearch
2128 method getpan = m_pan
2129 method setqsearch s = m_qsearch <- s
2130 end;;
2132 let textentryspecial key = function
2133 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2134 let s =
2135 match key with
2136 | Glut.KEY_UP -> action HCprev
2137 | Glut.KEY_DOWN -> action HCnext
2138 | Glut.KEY_HOME -> action HCfirst
2139 | Glut.KEY_END -> action HClast
2140 | _ -> state.text
2142 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2143 G.postRedisplay "special textentry";
2144 | _ -> ()
2147 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2148 let enttext te =
2149 state.mode <- Textentry (te, onleave);
2150 state.text <- "";
2151 enttext ();
2152 G.postRedisplay "textentrykeyboard enttext";
2154 match Char.unsafe_chr key with
2155 | '\008' -> (* backspace *)
2156 let len = String.length text in
2157 if len = 0
2158 then (
2159 onleave Cancel;
2160 G.postRedisplay "textentrykeyboard after cancel";
2162 else (
2163 let s = String.sub text 0 (len - 1) in
2164 enttext (c, s, opthist, onkey, ondone)
2167 | '\r' | '\n' ->
2168 ondone text;
2169 onleave Confirm;
2170 G.postRedisplay "textentrykeyboard after confirm"
2172 | '\007' (* ctrl-g *)
2173 | '\027' -> (* escape *)
2174 if String.length text = 0
2175 then (
2176 begin match opthist with
2177 | None -> ()
2178 | Some (_, onhistcancel) -> onhistcancel ()
2179 end;
2180 onleave Cancel;
2181 state.text <- "";
2182 G.postRedisplay "textentrykeyboard after cancel2"
2184 else (
2185 enttext (c, "", opthist, onkey, ondone)
2188 | '\127' -> () (* delete *)
2190 | _ ->
2191 begin match onkey text key with
2192 | TEdone text ->
2193 ondone text;
2194 onleave Confirm;
2195 G.postRedisplay "textentrykeyboard after confirm2";
2197 | TEcont text ->
2198 enttext (c, text, opthist, onkey, ondone);
2200 | TEstop ->
2201 onleave Cancel;
2202 state.text <- "";
2203 G.postRedisplay "textentrykeyboard after cancel3"
2205 | TEswitch te ->
2206 state.mode <- Textentry (te, onleave);
2207 G.postRedisplay "textentrykeyboard switch";
2208 end;
2211 let firstof first active =
2212 if first > active || abs (first - active) > fstate.maxrows - 1
2213 then max 0 (active - (fstate.maxrows/2))
2214 else first
2217 let calcfirst first active =
2218 if active > first
2219 then
2220 let rows = active - first in
2221 if rows > fstate.maxrows then active - fstate.maxrows else first
2222 else active
2225 let coe s = (s :> uioh);;
2227 class listview ~(source:lvsource) ~trusted =
2228 object (self)
2229 val m_pan = source#getpan
2230 val m_first = source#getfirst
2231 val m_active = source#getactive
2232 val m_qsearch = source#getqsearch
2233 val m_prev_uioh = state.uioh
2235 method private elemunder y =
2236 let n = y / (fstate.fontsize+1) in
2237 if m_first + n < source#getitemcount
2238 then (
2239 if source#hasaction (m_first + n)
2240 then Some (m_first + n)
2241 else None
2243 else None
2245 method display =
2246 Gl.enable `blend;
2247 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2248 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2249 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2250 GlDraw.color (1., 1., 1.);
2251 Gl.enable `texture_2d;
2252 let fs = fstate.fontsize in
2253 let nfs = fs + 1 in
2254 let ww = fstate.wwidth in
2255 let tabw = 30.0*.ww in
2256 let rec loop row =
2257 if (row - m_first) * nfs > conf.winh
2258 then ()
2259 else (
2260 if row >= 0 && row < source#getitemcount
2261 then (
2262 let (s, level) = source#getitem row in
2263 let y = (row - m_first) * nfs in
2264 let x = 5.0 +. float (level + m_pan) *. ww in
2265 if row = m_active
2266 then (
2267 Gl.disable `texture_2d;
2268 GlDraw.polygon_mode `both `line;
2269 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2270 GlDraw.rect (1., float (y + 1))
2271 (float (conf.winw - 1), float (y + fs + 3));
2272 GlDraw.polygon_mode `both `fill;
2273 GlDraw.color (1., 1., 1.);
2274 Gl.enable `texture_2d;
2277 let drawtabularstring s =
2278 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2279 if trusted
2280 then
2281 let tabpos = try String.index s '\t' with Not_found -> -1 in
2282 if tabpos > 0
2283 then
2284 let len = String.length s - tabpos - 1 in
2285 let s1 = String.sub s 0 tabpos
2286 and s2 = String.sub s (tabpos + 1) len in
2287 let nx = drawstr x s1 in
2288 let sw = nx -. x in
2289 let x = x +. (max tabw sw) in
2290 drawstr x s2
2291 else
2292 drawstr x s
2293 else
2294 drawstr x s
2296 let _ = drawtabularstring s in
2297 loop (row+1)
2301 loop 0;
2302 Gl.disable `blend;
2303 Gl.disable `texture_2d;
2305 method updownlevel incr =
2306 let len = source#getitemcount in
2307 let _, curlevel = source#getitem m_active in
2308 let rec flow i =
2309 if i = len then i-1 else if i = -1 then 0 else
2310 let _, l = source#getitem i in
2311 if l != curlevel then i else flow (i+incr)
2313 let active = flow m_active in
2314 let first = calcfirst m_first active in
2315 G.postRedisplay "special outline updownlevel";
2316 {< m_active = active; m_first = first >}
2318 method private key1 key =
2319 let set active first qsearch =
2320 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2322 let search active pattern incr =
2323 let dosearch re =
2324 let rec loop n =
2325 if n >= 0 && n < source#getitemcount
2326 then (
2327 let s, _ = source#getitem n in
2329 (try ignore (Str.search_forward re s 0); true
2330 with Not_found -> false)
2331 then Some n
2332 else loop (n + incr)
2334 else None
2336 loop active
2339 let re = Str.regexp_case_fold pattern in
2340 dosearch re
2341 with Failure s ->
2342 state.text <- s;
2343 None
2345 match key with
2346 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2347 let incr = if key = 18 then -1 else 1 in
2348 let active, first =
2349 match search (m_active + incr) m_qsearch incr with
2350 | None ->
2351 state.text <- m_qsearch ^ " [not found]";
2352 m_active, m_first
2353 | Some active ->
2354 state.text <- m_qsearch;
2355 active, firstof m_first active
2357 G.postRedisplay "listview ctrl-r/s";
2358 set active first m_qsearch;
2360 | 8 -> (* backspace *)
2361 let len = String.length m_qsearch in
2362 if len = 0
2363 then coe self
2364 else (
2365 if len = 1
2366 then (
2367 state.text <- "";
2368 G.postRedisplay "listview empty qsearch";
2369 set m_active m_first "";
2371 else
2372 let qsearch = String.sub m_qsearch 0 (len - 1) in
2373 let active, first =
2374 match search m_active qsearch ~-1 with
2375 | None ->
2376 state.text <- qsearch ^ " [not found]";
2377 m_active, m_first
2378 | Some active ->
2379 state.text <- qsearch;
2380 active, firstof m_first active
2382 G.postRedisplay "listview backspace qsearch";
2383 set active first qsearch
2386 | _ when key >= 32 && key < 127 ->
2387 let pattern = addchar m_qsearch (Char.chr key) in
2388 let active, first =
2389 match search m_active pattern 1 with
2390 | None ->
2391 state.text <- pattern ^ " [not found]";
2392 m_active, m_first
2393 | Some active ->
2394 state.text <- pattern;
2395 active, firstof m_first active
2397 G.postRedisplay "listview qsearch add";
2398 set active first pattern;
2400 | 27 -> (* escape *)
2401 state.text <- "";
2402 if String.length m_qsearch = 0
2403 then (
2404 G.postRedisplay "list view escape";
2405 begin
2406 match
2407 source#exit (coe self) true m_active m_first m_pan m_qsearch
2408 with
2409 | None -> m_prev_uioh
2410 | Some uioh -> uioh
2413 else (
2414 G.postRedisplay "list view kill qsearch";
2415 source#setqsearch "";
2416 coe {< m_qsearch = "" >}
2419 | 13 -> (* enter *)
2420 state.text <- "";
2421 let self = {< m_qsearch = "" >} in
2422 source#setqsearch "";
2423 let opt =
2424 G.postRedisplay "listview enter";
2425 if m_active >= 0 && m_active < source#getitemcount
2426 then (
2427 source#exit (coe self) false m_active m_first m_pan "";
2429 else (
2430 source#exit (coe self) true m_active m_first m_pan "";
2433 begin match opt with
2434 | None -> m_prev_uioh
2435 | Some uioh -> uioh
2438 | 127 -> (* delete *)
2439 coe self
2441 | _ -> dolog "unknown key %d" key; coe self
2443 method private special1 key =
2444 let itemcount = source#getitemcount in
2445 let find start incr =
2446 let rec find i =
2447 if i = -1 || i = itemcount
2448 then -1
2449 else (
2450 if source#hasaction i
2451 then i
2452 else find (i + incr)
2455 find start
2457 let set active first =
2458 let first = bound first 0 (itemcount - fstate.maxrows) in
2459 state.text <- "";
2460 coe {< m_active = active; m_first = first >}
2462 let navigate incr =
2463 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2464 let active, first =
2465 let incr1 = if incr > 0 then 1 else -1 in
2466 if isvisible m_first m_active
2467 then
2468 let next =
2469 let next = m_active + incr in
2470 let next =
2471 if next < 0 || next >= itemcount
2472 then -1
2473 else find next incr1
2475 if next = -1 || abs (m_active - next) > fstate.maxrows
2476 then -1
2477 else next
2479 if next = -1
2480 then
2481 let first = m_first + incr in
2482 let first = bound first 0 (itemcount - 1) in
2483 let next =
2484 let next = m_active + incr in
2485 let next = bound next 0 (itemcount - 1) in
2486 find next ~-incr1
2488 let active = if next = -1 then m_active else next in
2489 active, first
2490 else
2491 let first = min next m_first in
2492 next, first
2493 else
2494 let first = m_first + incr in
2495 let first = bound first 0 (itemcount - 1) in
2496 let active =
2497 let next = m_active + incr in
2498 let next = bound next 0 (itemcount - 1) in
2499 let next = find next incr1 in
2500 if next = -1 || abs (m_active - first) > fstate.maxrows
2501 then m_active
2502 else next
2504 active, first
2506 G.postRedisplay "listview navigate";
2507 set active first;
2509 begin match key with
2510 | Glut.KEY_UP -> navigate ~-1
2511 | Glut.KEY_DOWN -> navigate 1
2512 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2513 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2515 | Glut.KEY_RIGHT ->
2516 state.text <- "";
2517 G.postRedisplay "listview right";
2518 coe {< m_pan = m_pan - 1 >}
2520 | Glut.KEY_LEFT ->
2521 state.text <- "";
2522 G.postRedisplay "listview left";
2523 coe {< m_pan = m_pan + 1 >}
2525 | Glut.KEY_HOME ->
2526 let active = find 0 1 in
2527 G.postRedisplay "listview home";
2528 set active 0;
2530 | Glut.KEY_END ->
2531 let first = max 0 (itemcount - fstate.maxrows) in
2532 let active = find (itemcount - 1) ~-1 in
2533 G.postRedisplay "listview end";
2534 set active first;
2536 | _ -> coe self
2537 end;
2539 method key key =
2540 match state.mode with
2541 | Textentry te -> textentrykeyboard key te; coe self
2542 | _ -> self#key1 key
2544 method special key =
2545 match state.mode with
2546 | Textentry te -> textentryspecial key te; coe self
2547 | _ -> self#special1 key
2549 method button button bstate _ y =
2550 let opt =
2551 match button with
2552 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2553 begin match self#elemunder y with
2554 | Some n ->
2555 G.postRedisplay "listview click";
2556 source#exit
2557 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2558 | _ ->
2559 Some (coe self)
2561 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2562 let len = source#getitemcount in
2563 let first =
2564 if m_first + fstate.maxrows >= len
2565 then
2566 m_first
2567 else
2568 let first = m_first + (if n == 3 then -1 else 1) in
2569 bound first 0 (len - 1)
2571 G.postRedisplay "listview wheel";
2572 Some (coe {< m_first = first >})
2573 | _ ->
2574 Some (coe self)
2576 match opt with
2577 | None -> m_prev_uioh
2578 | Some uioh -> uioh
2580 method motion _ _ = coe self
2582 method pmotion _ y =
2583 let n =
2584 match self#elemunder y with
2585 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2586 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2588 let o =
2589 if n != m_active
2590 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2591 else self
2593 coe o
2595 method infochanged _ = ()
2596 end;;
2598 class outlinelistview ~source =
2599 object (self)
2600 inherit listview ~source:(source :> lvsource) ~trusted:false as super
2602 method key key =
2603 match key with
2604 | 14 -> (* ctrl-n *)
2605 source#narrow m_qsearch;
2606 G.postRedisplay "outline ctrl-n";
2607 coe {< m_first = 0; m_active = 0 >}
2609 | 21 -> (* ctrl-u *)
2610 source#denarrow;
2611 G.postRedisplay "outline ctrl-u";
2612 coe {< m_first = 0; m_active = 0 >}
2614 | 12 -> (* ctrl-l *)
2615 let first = m_active - (fstate.maxrows / 2) in
2616 G.postRedisplay "outline ctrl-l";
2617 coe {< m_first = first >}
2619 | 127 -> (* delete *)
2620 source#remove m_active;
2621 G.postRedisplay "outline delete";
2622 let active = max 0 (m_active-1) in
2623 coe {< m_first = firstof m_first active;
2624 m_active = active >}
2626 | key -> super#key key
2628 method special key =
2629 let calcfirst first active =
2630 if active > first
2631 then
2632 let rows = active - first in
2633 if rows > fstate.maxrows then active - fstate.maxrows else first
2634 else active
2636 let navigate incr =
2637 let active = m_active + incr in
2638 let active = bound active 0 (source#getitemcount - 1) in
2639 let first = calcfirst m_first active in
2640 G.postRedisplay "special outline navigate";
2641 coe {< m_active = active; m_first = first >}
2643 match key with
2644 | Glut.KEY_UP -> navigate ~-1
2645 | Glut.KEY_DOWN -> navigate 1
2646 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2647 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2649 | Glut.KEY_RIGHT ->
2650 let o =
2651 if Glut.getModifiers () land Glut.active_ctrl != 0
2652 then (
2653 G.postRedisplay "special outline right";
2654 {< m_pan = m_pan + 1 >}
2656 else self#updownlevel 1
2658 coe o
2660 | Glut.KEY_LEFT ->
2661 let o =
2662 if Glut.getModifiers () land Glut.active_ctrl != 0
2663 then (
2664 G.postRedisplay "special outline left";
2665 {< m_pan = m_pan - 1 >}
2667 else self#updownlevel ~-1
2669 coe o
2671 | Glut.KEY_HOME ->
2672 G.postRedisplay "special outline home";
2673 coe {< m_first = 0; m_active = 0 >}
2675 | Glut.KEY_END ->
2676 let active = source#getitemcount - 1 in
2677 let first = max 0 (active - fstate.maxrows) in
2678 G.postRedisplay "special outline end";
2679 coe {< m_active = active; m_first = first >}
2681 | _ -> super#special key
2684 let outlinesource usebookmarks =
2685 let empty = [||] in
2686 (object
2687 inherit lvsourcebase
2688 val mutable m_items = empty
2689 val mutable m_orig_items = empty
2690 val mutable m_prev_items = empty
2691 val mutable m_narrow_pattern = ""
2692 val mutable m_hadremovals = false
2694 method getitemcount =
2695 Array.length m_items + (if m_hadremovals then 1 else 0)
2697 method getitem n =
2698 if n == Array.length m_items && m_hadremovals
2699 then
2700 ("[Confirm removal]", 0)
2701 else
2702 let s, n, _ = m_items.(n) in
2703 (s, n)
2705 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
2706 ignore (uioh, first, pan, qsearch);
2707 let confrimremoval = m_hadremovals && active = Array.length m_items in
2708 let items =
2709 if String.length m_narrow_pattern = 0
2710 then m_orig_items
2711 else m_items
2713 if not cancel
2714 then (
2715 if not confrimremoval
2716 then(
2717 let _, _, anchor = m_items.(active) in
2718 gotoanchor anchor;
2719 m_items <- items;
2721 else (
2722 state.bookmarks <- Array.to_list m_items;
2723 m_orig_items <- m_items;
2726 else m_items <- items;
2727 None
2729 method hasaction _ = true
2731 method greetmsg =
2732 if Array.length m_items != Array.length m_orig_items
2733 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
2734 else ""
2736 method narrow pattern =
2737 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2738 match reopt with
2739 | None -> ()
2740 | Some re ->
2741 let rec loop accu n =
2742 if n = -1
2743 then (
2744 m_narrow_pattern <- pattern;
2745 m_items <- Array.of_list accu
2747 else
2748 let (s, _, _) as o = m_items.(n) in
2749 let accu =
2750 if (try ignore (Str.search_forward re s 0); true
2751 with Not_found -> false)
2752 then o :: accu
2753 else accu
2755 loop accu (n-1)
2757 loop [] (Array.length m_items - 1)
2759 method denarrow =
2760 m_orig_items <- (
2761 if usebookmarks
2762 then Array.of_list state.bookmarks
2763 else state.outlines
2765 m_items <- m_orig_items
2767 method remove m =
2768 if usebookmarks
2769 then
2770 if m >= 0 && m < Array.length m_items
2771 then (
2772 m_hadremovals <- true;
2773 m_items <- Array.init (Array.length m_items - 1) (fun n ->
2774 let n = if n >= m then n+1 else n in
2775 m_items.(n)
2779 method reset pageno items =
2780 m_hadremovals <- false;
2781 if m_orig_items == empty || m_prev_items != items
2782 then (
2783 m_orig_items <- items;
2784 if String.length m_narrow_pattern = 0
2785 then m_items <- items;
2787 m_prev_items <- items;
2788 let active =
2789 let rec loop n best bestd =
2790 if n = Array.length m_items
2791 then best
2792 else
2793 let (_, _, (outlinepageno, _)) = m_items.(n) in
2794 let d = abs (outlinepageno - pageno) in
2795 if d < bestd
2796 then loop (n+1) n d
2797 else loop (n+1) best bestd
2799 loop 0 ~-1 max_int
2801 m_active <- active;
2802 m_first <- firstof m_first active
2803 end)
2806 let enterselector usebookmarks =
2807 let source = outlinesource usebookmarks in
2808 fun errmsg ->
2809 let outlines =
2810 if usebookmarks
2811 then Array.of_list state.bookmarks
2812 else state.outlines
2814 if Array.length outlines = 0
2815 then (
2816 showtext ' ' errmsg;
2818 else (
2819 state.text <- source#greetmsg;
2820 Glut.setCursor Glut.CURSOR_INHERIT;
2821 let pageno =
2822 match state.layout with
2823 | [] -> -1
2824 | {pageno=pageno} :: _ -> pageno
2826 source#reset pageno outlines;
2827 state.uioh <- coe (new outlinelistview ~source);
2828 G.postRedisplay "enter selector";
2832 let enteroutlinemode =
2833 let f = enterselector false in
2834 fun ()-> f "Document has no outline";
2837 let enterbookmarkmode =
2838 let f = enterselector true in
2839 fun () -> f "Document has no bookmarks (yet)";
2842 let color_of_string s =
2843 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
2844 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
2848 let color_to_string (r, g, b) =
2849 let r = truncate (r *. 256.0)
2850 and g = truncate (g *. 256.0)
2851 and b = truncate (b *. 256.0) in
2852 Printf.sprintf "%d/%d/%d" r g b
2855 let irect_of_string s =
2856 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
2859 let irect_to_string (x0,y0,x1,y1) =
2860 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
2863 let makecheckers () =
2864 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
2865 following to say:
2866 converted by Issac Trotts. July 25, 2002 *)
2867 let image_height = 64
2868 and image_width = 64 in
2870 let make_image () =
2871 let image =
2872 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
2874 for i = 0 to image_width - 1 do
2875 for j = 0 to image_height - 1 do
2876 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
2877 (if (i land 8 ) lxor (j land 8) = 0
2878 then [|255;255;255|] else [|200;200;200|])
2879 done
2880 done;
2881 image
2883 let image = make_image () in
2884 let id = GlTex.gen_texture () in
2885 GlTex.bind_texture `texture_2d id;
2886 GlPix.store (`unpack_alignment 1);
2887 GlTex.image2d image;
2888 List.iter (GlTex.parameter ~target:`texture_2d)
2889 [ `wrap_s `repeat;
2890 `wrap_t `repeat;
2891 `mag_filter `nearest;
2892 `min_filter `nearest ];
2896 let setcheckers enabled =
2897 match state.texid with
2898 | None ->
2899 if enabled then state.texid <- Some (makecheckers ())
2901 | Some texid ->
2902 if not enabled
2903 then (
2904 GlTex.delete_texture texid;
2905 state.texid <- None;
2909 let int_of_string_with_suffix s =
2910 let l = String.length s in
2911 let s1, shift =
2912 if l > 1
2913 then
2914 let suffix = Char.lowercase s.[l-1] in
2915 match suffix with
2916 | 'k' -> String.sub s 0 (l-1), 10
2917 | 'm' -> String.sub s 0 (l-1), 20
2918 | 'g' -> String.sub s 0 (l-1), 30
2919 | _ -> s, 0
2920 else s, 0
2922 let n = int_of_string s1 in
2923 let m = n lsl shift in
2924 if m < 0 || m < n
2925 then raise (Failure "value too large")
2926 else m
2929 let string_with_suffix_of_int n =
2930 if n = 0
2931 then "0"
2932 else
2933 let n, s =
2934 if n = 0
2935 then 0, ""
2936 else (
2937 if n land ((1 lsl 20) - 1) = 0
2938 then n lsr 20, "M"
2939 else (
2940 if n land ((1 lsl 10) - 1) = 0
2941 then n lsr 10, "K"
2942 else n, ""
2946 let rec loop s n =
2947 let h = n mod 1000 in
2948 let n = n / 1000 in
2949 if n = 0
2950 then string_of_int h ^ s
2951 else (
2952 let s = Printf.sprintf "_%03d%s" h s in
2953 loop s n
2956 loop "" n ^ s;
2959 let describe_location () =
2960 let f (fn, _) l =
2961 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
2963 let fn, ln = List.fold_left f (-1, -1) state.layout in
2964 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2965 let percent =
2966 if maxy <= 0
2967 then 100.
2968 else (100. *. (float state.y /. float maxy))
2970 if fn = ln
2971 then
2972 Printf.sprintf "page %d of %d [%.2f%%]"
2973 (fn+1) state.pagecount percent
2974 else
2975 Printf.sprintf
2976 "pages %d-%d of %d [%.2f%%]"
2977 (fn+1) (ln+1) state.pagecount percent
2980 let enterinfomode =
2981 let btos b = if b then "\xe2\x88\x9a" else "" in
2982 let showextended = ref false in
2983 let leave mode = function
2984 | Confirm -> state.mode <- mode
2985 | Cancel -> state.mode <- mode in
2986 let src =
2987 (object
2988 val mutable m_first_time = true
2989 val mutable m_l = []
2990 val mutable m_a = [||]
2991 val mutable m_prev_uioh = nouioh
2992 val mutable m_prev_mode = View
2994 inherit lvsourcebase
2996 method reset prev_mode prev_uioh =
2997 m_a <- Array.of_list (List.rev m_l);
2998 m_l <- [];
2999 m_prev_mode <- prev_mode;
3000 m_prev_uioh <- prev_uioh;
3001 if m_first_time
3002 then (
3003 let rec loop n =
3004 if n >= Array.length m_a
3005 then ()
3006 else
3007 match m_a.(n) with
3008 | _, _, _, Action _ -> m_active <- n
3009 | _ -> loop (n+1)
3011 loop 0;
3012 m_first_time <- false;
3015 method int name get set =
3016 m_l <-
3017 (name, `int get, 1, Action (
3018 fun u ->
3019 let ondone s =
3020 try set (int_of_string s)
3021 with exn ->
3022 state.text <- Printf.sprintf "bad integer `%s': %s"
3023 s (Printexc.to_string exn)
3025 state.text <- "";
3026 let te = name ^ ": ", "", None, intentry, ondone in
3027 state.mode <- Textentry (te, leave m_prev_mode);
3029 )) :: m_l
3031 method int_with_suffix name get set =
3032 m_l <-
3033 (name, `intws get, 1, Action (
3034 fun u ->
3035 let ondone s =
3036 try set (int_of_string_with_suffix s)
3037 with exn ->
3038 state.text <- Printf.sprintf "bad integer `%s': %s"
3039 s (Printexc.to_string exn)
3041 state.text <- "";
3042 let te =
3043 name ^ ": ", "", None, intentry_with_suffix, ondone
3045 state.mode <- Textentry (te, leave m_prev_mode);
3047 )) :: m_l
3049 method bool ?(offset=1) ?(btos=btos) name get set =
3050 m_l <-
3051 (name, `bool (btos, get), offset, Action (
3052 fun u ->
3053 let v = get () in
3054 set (not v);
3056 )) :: m_l
3058 method color name get set =
3059 m_l <-
3060 (name, `color get, 1, Action (
3061 fun u ->
3062 let invalid = (nan, nan, nan) in
3063 let ondone s =
3064 let c =
3065 try color_of_string s
3066 with exn ->
3067 state.text <- Printf.sprintf "bad color `%s': %s"
3068 s (Printexc.to_string exn);
3069 invalid
3071 if c <> invalid
3072 then set c;
3074 let te = name ^ ": ", "", None, textentry, ondone in
3075 state.text <- color_to_string (get ());
3076 state.mode <- Textentry (te, leave m_prev_mode);
3078 )) :: m_l
3080 method string name get set =
3081 m_l <-
3082 (name, `string get, 1, Action (
3083 fun u ->
3084 let ondone s = set s in
3085 let te = name ^ ": ", "", None, textentry, ondone in
3086 state.mode <- Textentry (te, leave m_prev_mode);
3088 )) :: m_l
3090 method colorspace name get set =
3091 m_l <-
3092 (name, `string get, 1, Action (
3093 fun _ ->
3094 let source =
3095 let vals = [| "rgb"; "bgr"; "gray" |] in
3096 (object
3097 inherit lvsourcebase
3099 initializer
3100 m_active <- int_of_colorspace conf.colorspace;
3101 m_first <- 0;
3103 method getitemcount = Array.length vals
3104 method getitem n = (vals.(n), 0)
3105 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3106 ignore (uioh, first, pan, qsearch);
3107 if not cancel then set active;
3108 None
3109 method hasaction _ = true
3110 end)
3112 state.text <- "";
3113 coe (new listview ~source ~trusted:true)
3114 )) :: m_l
3116 method caption s offset =
3117 m_l <- (s, `empty, offset, Noaction) :: m_l
3119 method caption2 s f offset =
3120 m_l <- (s, `string f, offset, Noaction) :: m_l
3122 method getitemcount = Array.length m_a
3124 method getitem n =
3125 let tostr = function
3126 | `int f -> string_of_int (f ())
3127 | `intws f -> string_with_suffix_of_int (f ())
3128 | `string f -> f ()
3129 | `color f -> color_to_string (f ())
3130 | `bool (btos, f) -> btos (f ())
3131 | `empty -> ""
3133 let name, t, offset, _ = m_a.(n) in
3134 ((let s = tostr t in
3135 if String.length s > 0
3136 then Printf.sprintf "%s\t%s" name s
3137 else name),
3138 offset)
3140 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3141 let uiohopt =
3142 if not cancel
3143 then (
3144 m_qsearch <- qsearch;
3145 let uioh =
3146 match m_a.(active) with
3147 | _, _, _, Action f -> f uioh
3148 | _ -> uioh
3150 Some uioh
3152 else None
3154 m_active <- active;
3155 m_first <- first;
3156 m_pan <- pan;
3157 uiohopt
3159 method hasaction n =
3160 match m_a.(n) with
3161 | _, _, _, Action _ -> true
3162 | _ -> false
3163 end)
3165 let rec fillsrc prevmode prevuioh =
3166 let sep () = src#caption "" 0 in
3167 let colorp name get set =
3168 src#string name
3169 (fun () -> color_to_string (get ()))
3170 (fun v ->
3172 let c = color_of_string v in
3173 set c
3174 with exn ->
3175 state.text <- Printf.sprintf "bad color `%s': %s"
3176 v (Printexc.to_string exn);
3179 let oldmode = state.mode in
3180 let birdseye = isbirdseye state.mode in
3182 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3184 src#bool "presentation mode"
3185 (fun () -> conf.presentation)
3186 (fun v ->
3187 conf.presentation <- v;
3188 state.anchor <- getanchor ();
3189 represent ());
3191 src#bool "ignore case in searches"
3192 (fun () -> conf.icase)
3193 (fun v -> conf.icase <- v);
3195 src#bool "preload"
3196 (fun () -> conf.preload)
3197 (fun v -> conf.preload <- v);
3199 src#bool "highlight links"
3200 (fun () -> conf.hlinks)
3201 (fun v -> conf.hlinks <- v);
3203 src#bool "under info"
3204 (fun () -> conf.underinfo)
3205 (fun v -> conf.underinfo <- v);
3207 src#bool "persistent bookmarks"
3208 (fun () -> conf.savebmarks)
3209 (fun v -> conf.savebmarks <- v);
3211 src#bool "proportional display"
3212 (fun () -> conf.proportional)
3213 (fun v -> reqlayout conf.angle v);
3215 src#bool "trim margins"
3216 (fun () -> conf.trimmargins)
3217 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3219 src#bool "persistent location"
3220 (fun () -> conf.jumpback)
3221 (fun v -> conf.jumpback <- v);
3223 sep ();
3224 src#int "vertical margin"
3225 (fun () -> conf.interpagespace)
3226 (fun n ->
3227 conf.interpagespace <- n;
3228 let pageno, py =
3229 match state.layout with
3230 | [] -> 0, 0
3231 | l :: _ ->
3232 l.pageno, l.pagey
3234 state.maxy <- calcheight ();
3235 let y = getpagey pageno in
3236 gotoy (y + py)
3239 src#int "page bias"
3240 (fun () -> conf.pagebias)
3241 (fun v -> conf.pagebias <- v);
3243 src#int "scroll step"
3244 (fun () -> conf.scrollstep)
3245 (fun n -> conf.scrollstep <- n);
3247 src#int "auto scroll step"
3248 (fun () ->
3249 match state.autoscroll with
3250 | Some step -> step
3251 | _ -> conf.autoscrollstep)
3252 (fun n ->
3253 if state.autoscroll <> None
3254 then state.autoscroll <- Some n;
3255 conf.autoscrollstep <- n);
3257 src#int "zoom"
3258 (fun () -> truncate (conf.zoom *. 100.))
3259 (fun v -> setzoom ((float v) /. 100.));
3261 src#int "rotation"
3262 (fun () -> conf.angle)
3263 (fun v -> reqlayout v conf.proportional);
3265 src#int "scroll bar width"
3266 (fun () -> state.scrollw)
3267 (fun v ->
3268 state.scrollw <- v;
3269 conf.scrollbw <- v;
3270 reshape conf.winw conf.winh;
3273 src#int "scroll handle height"
3274 (fun () -> conf.scrollh)
3275 (fun v -> conf.scrollh <- v;);
3277 src#int "thumbnail width"
3278 (fun () -> conf.thumbw)
3279 (fun v ->
3280 conf.thumbw <- min 4096 v;
3281 match oldmode with
3282 | Birdseye beye ->
3283 leavebirdseye beye false;
3284 enterbirdseye ()
3285 | _ -> ()
3288 sep ();
3289 src#caption "Presentation mode" 0;
3290 src#bool "scrollbar visible"
3291 (fun () -> conf.scrollbarinpm)
3292 (fun v ->
3293 if v != conf.scrollbarinpm
3294 then (
3295 conf.scrollbarinpm <- v;
3296 if conf.presentation
3297 then (
3298 state.scrollw <- if v then conf.scrollbw else 0;
3299 reshape conf.winw conf.winh;
3304 sep ();
3305 src#caption "Pixmap cache" 0;
3306 src#int_with_suffix "size (advisory)"
3307 (fun () -> conf.memlimit)
3308 (fun v -> conf.memlimit <- v);
3310 src#caption2 "used"
3311 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3312 (string_with_suffix_of_int state.memused)
3313 (Hashtbl.length state.tilemap)) 1;
3315 sep ();
3316 src#caption "Layout" 0;
3317 src#caption2 "Dimension"
3318 (fun () ->
3319 Printf.sprintf "%dx%d (virtual %dx%d)"
3320 conf.winw conf.winh
3321 state.w state.maxy)
3323 if conf.debug
3324 then
3325 src#caption2 "Position" (fun () ->
3326 Printf.sprintf "%dx%d" state.x state.y
3328 else
3329 src#caption2 "Visible" (fun () -> describe_location ()) 1
3332 sep ();
3333 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3334 "Save these parameters as global defaults at exit"
3335 (fun () -> conf.bedefault)
3336 (fun v -> conf.bedefault <- v)
3339 sep ();
3340 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3341 src#bool ~offset:0 ~btos "Extended parameters"
3342 (fun () -> !showextended)
3343 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3344 if !showextended
3345 then (
3346 src#bool "checkers"
3347 (fun () -> conf.checkers)
3348 (fun v -> conf.checkers <- v; setcheckers v);
3349 src#bool "verbose"
3350 (fun () -> conf.verbose)
3351 (fun v -> conf.verbose <- v);
3352 src#bool "invert colors"
3353 (fun () -> conf.invert)
3354 (fun v -> conf.invert <- v);
3355 src#bool "max fit"
3356 (fun () -> conf.maxhfit)
3357 (fun v -> conf.maxhfit <- v);
3358 src#string "uri launcher"
3359 (fun () -> conf.urilauncher)
3360 (fun v -> conf.urilauncher <- v);
3361 src#string "tile size"
3362 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3363 (fun v ->
3365 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3366 conf.tileh <- max 64 w;
3367 conf.tilew <- max 64 h;
3368 flushtiles ();
3369 with exn ->
3370 state.text <- Printf.sprintf "bad tile size `%s': %s"
3371 v (Printexc.to_string exn));
3372 src#int "anti-aliasing level"
3373 (fun () -> conf.aalevel)
3374 (fun v ->
3375 conf.aalevel <- bound v 0 8;
3376 state.anchor <- getanchor ();
3377 opendoc state.path state.password;
3379 src#int "ui font size"
3380 (fun () -> fstate.fontsize)
3381 (fun v -> setfontsize (bound v 5 100));
3382 colorp "background color"
3383 (fun () -> conf.bgcolor)
3384 (fun v -> conf.bgcolor <- v);
3385 src#bool "crop hack"
3386 (fun () -> conf.crophack)
3387 (fun v -> conf.crophack <- v);
3388 src#string "trim fuzz"
3389 (fun () -> irect_to_string conf.trimfuzz)
3390 (fun v ->
3392 conf.trimfuzz <- irect_of_string v;
3393 if conf.trimmargins
3394 then settrim true conf.trimfuzz;
3395 with exn ->
3396 state.text <- Printf.sprintf "bad irect `%s': %s"
3397 v (Printexc.to_string exn)
3399 src#string "throttle"
3400 (fun () ->
3401 match conf.maxwait with
3402 | None -> "show place holder if page is not ready"
3403 | Some time ->
3404 if time = infinity
3405 then "wait for page to fully render"
3406 else
3407 "wait " ^ string_of_float time
3408 ^ " seconds before showing placeholder"
3410 (fun v ->
3412 let f = float_of_string v in
3413 if f <= 0.0
3414 then conf.maxwait <- None
3415 else conf.maxwait <- Some f
3416 with exn ->
3417 state.text <- Printf.sprintf "bad time `%s': %s"
3418 v (Printexc.to_string exn)
3420 src#colorspace "color space"
3421 (fun () -> colorspace_to_string conf.colorspace)
3422 (fun v ->
3423 conf.colorspace <- colorspace_of_int v;
3424 wcmd "cs" [`i v];
3425 load state.layout;
3429 sep ();
3430 src#caption "Document" 0;
3431 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3432 if conf.trimmargins
3433 then (
3434 sep ();
3435 src#caption "Trimmed margins" 0;
3436 src#caption2 "Dimensions"
3437 (fun () -> string_of_int (List.length state.pdims)) 1;
3440 src#reset prevmode prevuioh;
3442 fun () ->
3443 state.text <- "";
3444 let prevmode = state.mode
3445 and prevuioh = state.uioh in
3446 fillsrc prevmode prevuioh;
3447 let source = (src :> lvsource) in
3448 state.uioh <- coe (object (self)
3449 inherit listview ~source ~trusted:true as super
3450 val mutable m_prevmemused = 0
3451 method infochanged = function
3452 | Memused ->
3453 if m_prevmemused != state.memused
3454 then (
3455 m_prevmemused <- state.memused;
3456 G.postRedisplay "memusedchanged";
3458 | Pdim -> G.postRedisplay "pdimchanged"
3459 | Docinfo -> fillsrc prevmode prevuioh
3461 method special key =
3462 if Glut.getModifiers () land Glut.active_ctrl = 0
3463 then
3464 match key with
3465 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
3466 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
3467 | _ -> super#special key
3468 else super#special key
3469 end);
3470 G.postRedisplay "info";
3473 let enterhelpmode =
3474 let source =
3475 (object
3476 inherit lvsourcebase
3477 method getitemcount = Array.length state.help
3478 method getitem n =
3479 let s, n, _ = state.help.(n) in
3480 (s, n)
3482 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3483 let optuioh =
3484 if not cancel
3485 then (
3486 m_qsearch <- qsearch;
3487 match state.help.(active) with
3488 | _, _, Action f -> Some (f uioh)
3489 | _ -> Some (uioh)
3491 else None
3493 m_active <- active;
3494 m_first <- first;
3495 m_pan <- pan;
3496 optuioh
3498 method hasaction n =
3499 match state.help.(n) with
3500 | _, _, Action _ -> true
3501 | _ -> false
3503 initializer
3504 m_active <- -1
3505 end)
3506 in fun () ->
3507 state.uioh <- coe (new listview ~source ~trusted:true);
3508 G.postRedisplay "help";
3511 let quickbookmark ?title () =
3512 match state.layout with
3513 | [] -> ()
3514 | l :: _ ->
3515 let title =
3516 match title with
3517 | None ->
3518 let sec = Unix.gettimeofday () in
3519 let tm = Unix.localtime sec in
3520 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
3521 (l.pageno+1)
3522 tm.Unix.tm_mday
3523 tm.Unix.tm_mon
3524 (tm.Unix.tm_year + 1900)
3525 tm.Unix.tm_hour
3526 tm.Unix.tm_min
3527 | Some title -> title
3529 state.bookmarks <-
3530 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
3531 :: state.bookmarks
3534 let doreshape w h =
3535 state.fullscreen <- None;
3536 Glut.reshapeWindow w h;
3539 let viewkeyboard key =
3540 let enttext te =
3541 let mode = state.mode in
3542 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3543 state.text <- "";
3544 enttext ();
3545 G.postRedisplay "view:enttext"
3547 let c = Char.chr key in
3548 match c with
3549 | '\027' | 'q' -> (* escape *)
3550 begin match state.mstate with
3551 | Mzoomrect _ ->
3552 state.mstate <- Mnone;
3553 Glut.setCursor Glut.CURSOR_INHERIT;
3554 G.postRedisplay "kill zoom rect";
3555 | _ ->
3556 raise Quit
3557 end;
3559 | '\008' -> (* backspace *)
3560 let y = getnav ~-1 in
3561 gotoy_and_clear_text y
3563 | 'o' ->
3564 enteroutlinemode ()
3566 | 'u' ->
3567 state.rects <- [];
3568 state.text <- "";
3569 G.postRedisplay "dehighlight";
3571 | '/' | '?' ->
3572 let ondone isforw s =
3573 cbput state.hists.pat s;
3574 state.searchpattern <- s;
3575 search s isforw
3577 let s = String.create 1 in
3578 s.[0] <- c;
3579 enttext (s, "", Some (onhist state.hists.pat),
3580 textentry, ondone (c ='/'))
3582 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3583 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3584 setzoom (conf.zoom +. incr)
3586 | '+' ->
3587 let ondone s =
3588 let n =
3589 try int_of_string s with exc ->
3590 state.text <- Printf.sprintf "bad integer `%s': %s"
3591 s (Printexc.to_string exc);
3592 max_int
3594 if n != max_int
3595 then (
3596 conf.pagebias <- n;
3597 state.text <- "page bias is now " ^ string_of_int n;
3600 enttext ("page bias: ", "", None, intentry, ondone)
3602 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3603 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3604 setzoom (max 0.01 (conf.zoom -. decr))
3606 | '-' ->
3607 let ondone msg = state.text <- msg in
3608 enttext (
3609 "option [acfhilpstvAPRSZTI]: ", "", None,
3610 optentry state.mode, ondone
3613 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3614 setzoom 1.0
3616 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3617 let zoom = zoomforh conf.winw conf.winh state.scrollw in
3618 if zoom < 1.0
3619 then setzoom zoom
3621 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3622 togglebirdseye ()
3624 | '0' .. '9' ->
3625 let ondone s =
3626 let n =
3627 try int_of_string s with exc ->
3628 state.text <- Printf.sprintf "bad integer `%s': %s"
3629 s (Printexc.to_string exc);
3632 if n >= 0
3633 then (
3634 addnav ();
3635 cbput state.hists.pag (string_of_int n);
3636 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
3639 let pageentry text key =
3640 match Char.unsafe_chr key with
3641 | 'g' -> TEdone text
3642 | _ -> intentry text key
3644 let text = "x" in text.[0] <- c;
3645 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
3647 | 'b' ->
3648 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
3649 reshape conf.winw conf.winh;
3651 | 'l' ->
3652 conf.hlinks <- not conf.hlinks;
3653 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3654 G.postRedisplay "toggle highlightlinks";
3656 | 'a' ->
3657 begin match state.autoscroll with
3658 | Some step ->
3659 conf.autoscrollstep <- step;
3660 state.autoscroll <- None
3661 | None ->
3662 if conf.autoscrollstep = 0
3663 then state.autoscroll <- Some 1
3664 else state.autoscroll <- Some conf.autoscrollstep
3667 | 'P' ->
3668 conf.presentation <- not conf.presentation;
3669 if conf.presentation
3670 then (
3671 if not conf.scrollbarinpm
3672 then state.scrollw <- 0;
3674 else
3675 state.scrollw <- conf.scrollbw;
3677 showtext ' ' ("presentation mode " ^
3678 if conf.presentation then "on" else "off");
3679 state.anchor <- getanchor ();
3680 represent ()
3682 | 'f' ->
3683 begin match state.fullscreen with
3684 | None ->
3685 state.fullscreen <- Some (conf.winw, conf.winh);
3686 Glut.fullScreen ()
3687 | Some (w, h) ->
3688 state.fullscreen <- None;
3689 doreshape w h
3692 | 'g' ->
3693 gotoy_and_clear_text 0
3695 | 'G' ->
3696 gotopage1 (state.pagecount - 1) 0
3698 | 'n' ->
3699 search state.searchpattern true
3701 | 'p' | 'N' ->
3702 search state.searchpattern false
3704 | 't' ->
3705 begin match state.layout with
3706 | [] -> ()
3707 | l :: _ ->
3708 gotoy_and_clear_text (getpagey l.pageno)
3711 | ' ' ->
3712 begin match List.rev state.layout with
3713 | [] -> ()
3714 | l :: _ ->
3715 let pageno = min (l.pageno+1) (state.pagecount-1) in
3716 gotoy_and_clear_text (getpagey pageno)
3719 | '\127' -> (* del *)
3720 begin match state.layout with
3721 | [] -> ()
3722 | l :: _ ->
3723 let pageno = max 0 (l.pageno-1) in
3724 gotoy_and_clear_text (getpagey pageno)
3727 | '=' ->
3728 showtext ' ' (describe_location ());
3730 | 'w' ->
3731 begin match state.layout with
3732 | [] -> ()
3733 | l :: _ ->
3734 doreshape (l.pagew + state.scrollw) l.pageh;
3735 G.postRedisplay "w"
3738 | '\'' ->
3739 enterbookmarkmode ()
3741 | 'h' ->
3742 enterhelpmode ()
3744 | 'i' ->
3745 enterinfomode ()
3747 | 'm' ->
3748 let ondone s =
3749 match state.layout with
3750 | l :: _ ->
3751 state.bookmarks <-
3752 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
3753 :: state.bookmarks
3754 | _ -> ()
3756 enttext ("bookmark: ", "", None, textentry, ondone)
3758 | '~' ->
3759 quickbookmark ();
3760 showtext ' ' "Quick bookmark added";
3762 | 'z' ->
3763 begin match state.layout with
3764 | l :: _ ->
3765 let rect = getpdimrect l.pagedimno in
3766 let w, h =
3767 if conf.crophack
3768 then
3769 (truncate (1.8 *. (rect.(1) -. rect.(0))),
3770 truncate (1.2 *. (rect.(3) -. rect.(0))))
3771 else
3772 (truncate (rect.(1) -. rect.(0)),
3773 truncate (rect.(3) -. rect.(0)))
3775 let w = truncate ((float w)*.conf.zoom)
3776 and h = truncate ((float h)*.conf.zoom) in
3777 if w != 0 && h != 0
3778 then (
3779 state.anchor <- getanchor ();
3780 doreshape (w + state.scrollw) (h + conf.interpagespace)
3782 G.postRedisplay "z";
3784 | [] -> ()
3787 | '\000' -> (* ctrl-2 *)
3788 let maxw = getmaxw () in
3789 if maxw > 0.0
3790 then setzoom (maxw /. float conf.winw)
3792 | '<' | '>' ->
3793 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
3795 | '[' | ']' ->
3796 conf.colorscale <-
3797 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
3799 G.postRedisplay "brightness";
3801 | 'k' ->
3802 begin match state.mode with
3803 | Birdseye beye -> upbirdseye beye
3804 | _ -> gotoy (clamp (-conf.scrollstep))
3807 | 'j' ->
3808 begin match state.mode with
3809 | Birdseye beye -> downbirdseye beye
3810 | _ -> gotoy (clamp conf.scrollstep)
3813 | 'r' ->
3814 state.anchor <- getanchor ();
3815 opendoc state.path state.password
3817 | 'v' when conf.debug ->
3818 state.rects <- [];
3819 List.iter (fun l ->
3820 match getopaque l.pageno with
3821 | None -> ()
3822 | Some opaque ->
3823 let x0, y0, x1, y1 = pagebbox opaque in
3824 let a,b = float x0, float y0 in
3825 let c,d = float x1, float y0 in
3826 let e,f = float x1, float y1 in
3827 let h,j = float x0, float y1 in
3828 let rect = (a,b,c,d,e,f,h,j) in
3829 debugrect rect;
3830 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
3831 ) state.layout;
3832 G.postRedisplay "v";
3834 | _ ->
3835 vlog "huh? %d %c" key (Char.chr key);
3838 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
3839 match key with
3840 | 27 -> (* escape *)
3841 leavebirdseye beye true
3843 | 12 -> (* ctrl-l *)
3844 let y, h = getpageyh pageno in
3845 let top = (conf.winh - h) / 2 in
3846 gotoy (max 0 (y - top))
3848 | 13 -> (* enter *)
3849 leavebirdseye beye false
3851 | _ ->
3852 viewkeyboard key
3855 let keyboard ~key ~x ~y =
3856 ignore x;
3857 ignore y;
3858 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
3859 then wcmd "interrupt" []
3860 else state.uioh <- state.uioh#key key
3863 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
3864 match key with
3865 | Glut.KEY_UP -> upbirdseye beye
3866 | Glut.KEY_DOWN -> downbirdseye beye
3868 | Glut.KEY_PAGE_UP ->
3869 begin match state.layout with
3870 | l :: _ ->
3871 if l.pagey != 0
3872 then (
3873 state.mode <- Birdseye (
3874 conf, leftx, l.pageno, hooverpageno, anchor
3876 gotopage1 l.pageno 0;
3878 else (
3879 let layout = layout (state.y-conf.winh) conf.winh in
3880 match layout with
3881 | [] -> gotoy (clamp (-conf.winh))
3882 | l :: _ ->
3883 state.mode <- Birdseye (
3884 conf, leftx, l.pageno, hooverpageno, anchor
3886 gotopage1 l.pageno 0
3889 | [] -> gotoy (clamp (-conf.winh))
3890 end;
3892 | Glut.KEY_PAGE_DOWN ->
3893 begin match List.rev state.layout with
3894 | l :: _ ->
3895 let layout = layout (state.y + conf.winh) conf.winh in
3896 begin match layout with
3897 | [] ->
3898 let incr = l.pageh - l.pagevh in
3899 if incr = 0
3900 then (
3901 state.mode <-
3902 Birdseye (
3903 conf, leftx, state.pagecount - 1, hooverpageno, anchor
3905 G.postRedisplay "birdseye pagedown";
3907 else gotoy (clamp (incr + conf.interpagespace*2));
3909 | l :: _ ->
3910 state.mode <-
3911 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
3912 gotopage1 l.pageno 0;
3915 | [] -> gotoy (clamp conf.winh)
3916 end;
3918 | Glut.KEY_HOME ->
3919 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
3920 gotopage1 0 0
3922 | Glut.KEY_END ->
3923 let pageno = state.pagecount - 1 in
3924 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3925 if not (pagevisible state.layout pageno)
3926 then
3927 let h =
3928 match List.rev state.pdims with
3929 | [] -> conf.winh
3930 | (_, _, h, _) :: _ -> h
3932 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
3933 else G.postRedisplay "birdseye end";
3934 | _ -> ()
3937 let setautoscrollspeed step goingdown =
3938 let incr = max 1 ((abs step) / 2) in
3939 let incr = if goingdown then incr else -incr in
3940 let astep = step + incr in
3941 state.autoscroll <- Some astep;
3944 let special ~key ~x ~y =
3945 ignore x;
3946 ignore y;
3947 state.uioh <- state.uioh#special key
3950 let drawpage l =
3951 let color =
3952 match state.mode with
3953 | Textentry _ -> scalecolor 0.4
3954 | View -> scalecolor 1.0
3955 | Birdseye (_, _, pageno, hooverpageno, _) ->
3956 if l.pageno = hooverpageno
3957 then scalecolor 0.9
3958 else (
3959 if l.pageno = pageno
3960 then scalecolor 1.0
3961 else scalecolor 0.8
3964 drawtiles l color;
3965 begin match getopaque l.pageno with
3966 | Some opaque ->
3967 if tileready l l.pagex l.pagey
3968 then
3969 let x = l.pagedispx - l.pagex
3970 and y = l.pagedispy - l.pagey in
3971 postprocess opaque conf.hlinks x y;
3973 | _ -> ()
3974 end;
3977 let scrollph y =
3978 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3979 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3980 let sh = float conf.winh /. sh in
3981 let sh = max sh (float conf.scrollh) in
3983 let percent =
3984 if y = state.maxy
3985 then 1.0
3986 else float y /. float maxy
3988 let position = (float conf.winh -. sh) *. percent in
3990 let position =
3991 if position +. sh > float conf.winh
3992 then float conf.winh -. sh
3993 else position
3995 position, sh;
3998 let scrollpw x =
3999 let winw = conf.winw - state.scrollw - 1 in
4000 let fwinw = float winw in
4001 let sw =
4002 let sw = fwinw /. float state.w in
4003 let sw = fwinw *. sw in
4004 max sw (float conf.scrollh)
4006 let position, sw =
4007 let f = state.w+winw in
4008 let r = float (winw-x) /. float f in
4009 let p = fwinw *. r in
4010 p-.sw/.2., sw
4012 let sw =
4013 if position +. sw > fwinw
4014 then fwinw -. position
4015 else sw
4017 position, sw;
4020 let scrollindicator () =
4021 GlDraw.color (0.64 , 0.64, 0.64);
4022 GlDraw.rect
4023 (float (conf.winw - state.scrollw), 0.)
4024 (float conf.winw, float conf.winh)
4026 GlDraw.rect
4027 (0., float (conf.winh - state.hscrollh))
4028 (float (conf.winw - state.scrollw - 1), float conf.winh)
4030 GlDraw.color (0.0, 0.0, 0.0);
4032 let position, sh = scrollph state.y in
4033 GlDraw.rect
4034 (float (conf.winw - state.scrollw), position)
4035 (float conf.winw, position +. sh)
4037 let position, sw = scrollpw state.x in
4038 GlDraw.rect
4039 (position, float (conf.winh - state.hscrollh))
4040 (position +. sw, float conf.winh)
4044 let pagetranslatepoint l x y =
4045 let dy = y - l.pagedispy in
4046 let y = dy + l.pagey in
4047 let dx = x - l.pagedispx in
4048 let x = dx + l.pagex in
4049 (x, y);
4052 let showsel () =
4053 match state.mstate with
4054 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4057 | Msel ((x0, y0), (x1, y1)) ->
4058 let rec loop = function
4059 | l :: ls ->
4060 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4061 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4062 then
4063 match getopaque l.pageno with
4064 | Some opaque ->
4065 let dx, dy = pagetranslatepoint l 0 0 in
4066 let x0 = x0 + dx
4067 and y0 = y0 + dy
4068 and x1 = x1 + dx
4069 and y1 = y1 + dy in
4070 GlMat.mode `modelview;
4071 GlMat.push ();
4072 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4073 seltext opaque (x0, y0, x1, y1);
4074 GlMat.pop ();
4075 | _ -> ()
4076 else loop ls
4077 | [] -> ()
4079 loop state.layout
4082 let showrects () =
4083 Gl.enable `blend;
4084 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4085 GlDraw.polygon_mode `both `fill;
4086 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4087 List.iter
4088 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4089 List.iter (fun l ->
4090 if l.pageno = pageno
4091 then (
4092 let dx = float (l.pagedispx - l.pagex) in
4093 let dy = float (l.pagedispy - l.pagey) in
4094 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4095 GlDraw.begins `quads;
4097 GlDraw.vertex2 (x0+.dx, y0+.dy);
4098 GlDraw.vertex2 (x1+.dx, y1+.dy);
4099 GlDraw.vertex2 (x2+.dx, y2+.dy);
4100 GlDraw.vertex2 (x3+.dx, y3+.dy);
4102 GlDraw.ends ();
4104 ) state.layout
4105 ) state.rects
4107 Gl.disable `blend;
4110 let display () =
4111 GlClear.color (scalecolor2 conf.bgcolor);
4112 GlClear.clear [`color];
4113 List.iter drawpage state.layout;
4114 showrects ();
4115 showsel ();
4116 scrollindicator ();
4117 state.uioh#display;
4118 begin match state.mstate with
4119 | Mzoomrect ((x0, y0), (x1, y1)) ->
4120 Gl.enable `blend;
4121 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4122 GlDraw.polygon_mode `both `fill;
4123 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4124 GlDraw.rect (float x0, float y0)
4125 (float x1, float y1);
4126 Gl.disable `blend;
4127 | _ -> ()
4128 end;
4129 enttext ();
4130 Glut.swapBuffers ();
4133 let getunder x y =
4134 let rec f = function
4135 | l :: rest ->
4136 begin match getopaque l.pageno with
4137 | Some opaque ->
4138 let x0 = l.pagedispx in
4139 let x1 = x0 + l.pagevw in
4140 let y0 = l.pagedispy in
4141 let y1 = y0 + l.pagevh in
4142 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4143 then
4144 let px, py = pagetranslatepoint l x y in
4145 match whatsunder opaque px py with
4146 | Unone -> f rest
4147 | under -> under
4148 else f rest
4149 | _ ->
4150 f rest
4152 | [] -> Unone
4154 f state.layout
4157 let zoomrect x y x1 y1 =
4158 let x0 = min x x1
4159 and x1 = max x x1
4160 and y0 = min y y1 in
4161 gotoy (state.y + y0);
4162 state.anchor <- getanchor ();
4163 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4164 let margin =
4165 if state.w < conf.winw - state.scrollw
4166 then (conf.winw - state.scrollw - state.w) / 2
4167 else 0
4169 state.x <- (state.x + margin) - x0;
4170 setzoom zoom;
4171 Glut.setCursor Glut.CURSOR_INHERIT;
4172 state.mstate <- Mnone;
4175 let scrollx x =
4176 let winw = conf.winw - state.scrollw - 1 in
4177 let s = float x /. float winw in
4178 let destx = truncate (float (state.w + winw) *. s) in
4179 state.x <- winw - destx;
4180 gotoy_and_clear_text state.y;
4181 state.mstate <- Mscrollx;
4184 let scrolly y =
4185 let s = float y /. float conf.winh in
4186 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4187 gotoy_and_clear_text desty;
4188 state.mstate <- Mscrolly;
4191 let viewmouse button bstate x y =
4192 match button with
4193 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4194 if Glut.getModifiers () land Glut.active_ctrl != 0
4195 then (
4196 match state.mstate with
4197 | Mzoom (oldn, i) ->
4198 if oldn = n
4199 then (
4200 if i = 2
4201 then
4202 let incr =
4203 match n with
4204 | 4 ->
4205 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4206 | _ ->
4207 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4209 let zoom = conf.zoom -. incr in
4210 setzoom zoom;
4211 state.mstate <- Mzoom (n, 0);
4212 else
4213 state.mstate <- Mzoom (n, i+1);
4215 else state.mstate <- Mzoom (n, 0)
4217 | _ -> state.mstate <- Mzoom (n, 0)
4219 else (
4220 match state.autoscroll with
4221 | Some step -> setautoscrollspeed step (n=4)
4222 | None ->
4223 let incr =
4224 if n = 3
4225 then -conf.scrollstep
4226 else conf.scrollstep
4228 let incr = incr * 2 in
4229 let y = clamp incr in
4230 gotoy_and_clear_text y
4233 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4234 if bstate = Glut.DOWN
4235 then (
4236 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4237 state.mstate <- Mpan (x, y)
4239 else
4240 state.mstate <- Mnone
4242 | Glut.RIGHT_BUTTON ->
4243 if bstate = Glut.DOWN
4244 then (
4245 Glut.setCursor Glut.CURSOR_CYCLE;
4246 let p = (x, y) in
4247 state.mstate <- Mzoomrect (p, p)
4249 else (
4250 match state.mstate with
4251 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4252 | _ ->
4253 Glut.setCursor Glut.CURSOR_INHERIT;
4254 state.mstate <- Mnone
4257 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4258 if bstate = Glut.DOWN
4259 then
4260 let position, sh = scrollph state.y in
4261 if y > truncate position && y < truncate (position +. sh)
4262 then state.mstate <- Mscrolly
4263 else scrolly y
4264 else
4265 state.mstate <- Mnone
4267 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4268 if bstate = Glut.DOWN
4269 then
4270 let position, sw = scrollpw state.x in
4271 if x > truncate position && x < truncate (position +. sw)
4272 then state.mstate <- Mscrollx
4273 else scrollx x
4274 else
4275 state.mstate <- Mnone
4277 | Glut.LEFT_BUTTON ->
4278 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4279 begin match dest with
4280 | Ulinkgoto (pageno, top) ->
4281 if pageno >= 0
4282 then (
4283 addnav ();
4284 gotopage1 pageno top;
4287 | Ulinkuri s ->
4288 gotouri s
4290 | Unone when bstate = Glut.DOWN ->
4291 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4292 state.mstate <- Mpan (x, y);
4294 | Unone | Utext _ ->
4295 if bstate = Glut.DOWN
4296 then (
4297 if conf.angle mod 360 = 0
4298 then (
4299 state.mstate <- Msel ((x, y), (x, y));
4300 G.postRedisplay "mouse select";
4303 else (
4304 match state.mstate with
4305 | Mnone -> ()
4307 | Mzoom _ | Mscrollx | Mscrolly ->
4308 state.mstate <- Mnone
4310 | Mzoomrect ((x0, y0), _) ->
4311 zoomrect x0 y0 x y
4313 | Mpan _ ->
4314 Glut.setCursor Glut.CURSOR_INHERIT;
4315 state.mstate <- Mnone
4317 | Msel ((_, y0), (_, y1)) ->
4318 let f l =
4319 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4320 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4321 then
4322 match getopaque l.pageno with
4323 | Some opaque ->
4324 copysel opaque
4325 | _ -> ()
4327 List.iter f state.layout;
4328 copysel ""; (* ugly *)
4329 Glut.setCursor Glut.CURSOR_INHERIT;
4330 state.mstate <- Mnone;
4334 | _ -> ()
4337 let birdseyemouse button bstate x y
4338 (conf, leftx, _, hooverpageno, anchor) =
4339 match button with
4340 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4341 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4342 let rec loop = function
4343 | [] -> ()
4344 | l :: rest ->
4345 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4346 && x > margin && x < margin + l.pagew
4347 then (
4348 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4350 else loop rest
4352 loop state.layout
4353 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4354 | _ -> ()
4357 let mouse bstate button x y =
4358 state.uioh <- state.uioh#button button bstate x y;
4361 let mouse ~button ~state ~x ~y = mouse state button x y;;
4363 let motion ~x ~y =
4364 state.uioh <- state.uioh#motion x y
4367 let pmotion ~x ~y =
4368 state.uioh <- state.uioh#pmotion x y;
4371 let uioh = object
4372 method display = ()
4374 method key key =
4375 begin match state.mode with
4376 | Textentry textentry -> textentrykeyboard key textentry
4377 | Birdseye birdseye -> birdseyekeyboard key birdseye
4378 | View -> viewkeyboard key
4379 end;
4380 state.uioh
4382 method special key =
4383 begin match state.mode with
4384 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4385 togglebirdseye ()
4387 | Birdseye vals ->
4388 birdseyespecial key vals
4390 | View when key = Glut.KEY_F1 ->
4391 enterhelpmode ()
4393 | View ->
4394 begin match state.autoscroll with
4395 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4396 setautoscrollspeed step (key = Glut.KEY_DOWN)
4398 | _ ->
4399 let y =
4400 match key with
4401 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4402 | Glut.KEY_UP ->
4403 if Glut.getModifiers () land Glut.active_ctrl != 0
4404 then
4405 if Glut.getModifiers () land Glut.active_shift != 0
4406 then (setzoom state.prevzoom; state.y)
4407 else clamp (-conf.winh/2)
4408 else clamp (-conf.scrollstep)
4409 | Glut.KEY_DOWN ->
4410 if Glut.getModifiers () land Glut.active_ctrl != 0
4411 then
4412 if Glut.getModifiers () land Glut.active_shift != 0
4413 then (setzoom state.prevzoom; state.y)
4414 else clamp (conf.winh/2)
4415 else clamp (conf.scrollstep)
4416 | Glut.KEY_PAGE_UP ->
4417 if Glut.getModifiers () land Glut.active_ctrl != 0
4418 then
4419 match state.layout with
4420 | [] -> state.y
4421 | l :: _ -> state.y - l.pagey
4422 else
4423 clamp (-conf.winh)
4424 | Glut.KEY_PAGE_DOWN ->
4425 if Glut.getModifiers () land Glut.active_ctrl != 0
4426 then
4427 match List.rev state.layout with
4428 | [] -> state.y
4429 | l :: _ -> getpagey l.pageno
4430 else
4431 clamp conf.winh
4432 | Glut.KEY_HOME ->
4433 addnav ();
4435 | Glut.KEY_END ->
4436 addnav ();
4437 state.maxy - (if conf.maxhfit then conf.winh else 0)
4439 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4440 Glut.getModifiers () land Glut.active_alt != 0 ->
4441 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4443 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4444 let dx =
4445 if Glut.getModifiers () land Glut.active_ctrl != 0
4446 then (conf.winw / 2)
4447 else 10
4449 state.x <- state.x - dx;
4450 state.y
4451 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4452 let dx =
4453 if Glut.getModifiers () land Glut.active_ctrl != 0
4454 then (conf.winw / 2)
4455 else 10
4457 state.x <- state.x + dx;
4458 state.y
4460 | _ -> state.y
4462 gotoy_and_clear_text y
4465 | Textentry te -> textentryspecial key te
4466 end;
4467 state.uioh
4469 method button button bstate x y =
4470 begin match state.mode with
4471 | View -> viewmouse button bstate x y
4472 | Birdseye beye -> birdseyemouse button bstate x y beye
4473 | Textentry _ -> ()
4474 end;
4475 state.uioh
4477 method motion x y =
4478 begin match state.mode with
4479 | Textentry _ -> ()
4480 | View | Birdseye _ ->
4481 match state.mstate with
4482 | Mzoom _ | Mnone -> ()
4484 | Mpan (x0, y0) ->
4485 let dx = x - x0
4486 and dy = y0 - y in
4487 state.mstate <- Mpan (x, y);
4488 if conf.zoom > 1.0 then state.x <- state.x + dx;
4489 let y = clamp dy in
4490 gotoy_and_clear_text y
4492 | Msel (a, _) ->
4493 state.mstate <- Msel (a, (x, y));
4494 G.postRedisplay "motion select";
4496 | Mscrolly ->
4497 let y = min conf.winh (max 0 y) in
4498 scrolly y
4500 | Mscrollx ->
4501 let x = min conf.winw (max 0 x) in
4502 scrollx x
4504 | Mzoomrect (p0, _) ->
4505 state.mstate <- Mzoomrect (p0, (x, y));
4506 G.postRedisplay "motion zoomrect";
4507 end;
4508 state.uioh
4510 method pmotion x y =
4511 begin match state.mode with
4512 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4513 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4514 let rec loop = function
4515 | [] ->
4516 if hooverpageno != -1
4517 then (
4518 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4519 G.postRedisplay "pmotion birdseye no hoover";
4521 | l :: rest ->
4522 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4523 && x > margin && x < margin + l.pagew
4524 then (
4525 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4526 G.postRedisplay "pmotion birdseye hoover";
4528 else loop rest
4530 loop state.layout
4532 | Textentry _ -> ()
4534 | View ->
4535 match state.mstate with
4536 | Mnone ->
4537 begin match getunder x y with
4538 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
4539 | Ulinkuri uri ->
4540 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
4541 Glut.setCursor Glut.CURSOR_INFO
4542 | Ulinkgoto (page, _) ->
4543 if conf.underinfo
4544 then showtext 'p' ("age: " ^ string_of_int (page+1));
4545 Glut.setCursor Glut.CURSOR_INFO
4546 | Utext s ->
4547 if conf.underinfo then showtext 'f' ("ont: " ^ s);
4548 Glut.setCursor Glut.CURSOR_TEXT
4551 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
4553 end;
4554 state.uioh
4556 method infochanged _ = ()
4557 end;;
4559 module Config =
4560 struct
4561 open Parser
4563 let fontpath = ref "";;
4564 let wmclasshack = ref false;;
4566 let unent s =
4567 let l = String.length s in
4568 let b = Buffer.create l in
4569 unent b s 0 l;
4570 Buffer.contents b;
4573 let home =
4575 match platform with
4576 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
4577 | _ -> Sys.getenv "HOME"
4578 with exn ->
4579 prerr_endline
4580 ("Can not determine home directory location: " ^
4581 Printexc.to_string exn);
4585 let config_of c attrs =
4586 let apply c k v =
4588 match k with
4589 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
4590 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
4591 | "case-insensitive-search" -> { c with icase = bool_of_string v }
4592 | "preload" -> { c with preload = bool_of_string v }
4593 | "page-bias" -> { c with pagebias = int_of_string v }
4594 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
4595 | "auto-scroll-step" ->
4596 { c with autoscrollstep = max 0 (int_of_string v) }
4597 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
4598 | "crop-hack" -> { c with crophack = bool_of_string v }
4599 | "throttle" ->
4600 let mw =
4601 match String.lowercase v with
4602 | "true" -> Some infinity
4603 | "false" -> None
4604 | f -> Some (float_of_string f)
4606 { c with maxwait = mw}
4607 | "highlight-links" -> { c with hlinks = bool_of_string v }
4608 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
4609 | "vertical-margin" ->
4610 { c with interpagespace = max 0 (int_of_string v) }
4611 | "zoom" ->
4612 let zoom = float_of_string v /. 100. in
4613 let zoom = max zoom 0.0 in
4614 { c with zoom = zoom }
4615 | "presentation" -> { c with presentation = bool_of_string v }
4616 | "rotation-angle" -> { c with angle = int_of_string v }
4617 | "width" -> { c with winw = max 20 (int_of_string v) }
4618 | "height" -> { c with winh = max 20 (int_of_string v) }
4619 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
4620 | "proportional-display" -> { c with proportional = bool_of_string v }
4621 | "pixmap-cache-size" ->
4622 { c with memlimit = max 2 (int_of_string_with_suffix v) }
4623 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
4624 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
4625 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
4626 | "persistent-location" -> { c with jumpback = bool_of_string v }
4627 | "background-color" -> { c with bgcolor = color_of_string v }
4628 | "scrollbar-in-presentation" ->
4629 { c with scrollbarinpm = bool_of_string v }
4630 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
4631 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
4632 | "memlimit" ->
4633 { c with mumemlimit = max 1024 (int_of_string_with_suffix v) }
4634 | "checkers" -> { c with checkers = bool_of_string v }
4635 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
4636 | "trim-margins" -> { c with trimmargins = bool_of_string v }
4637 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
4638 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
4639 | "uri-launcher" -> { c with urilauncher = unent v }
4640 | "color-space" -> { c with colorspace = colorspace_of_string v }
4641 | "invert-colors" -> { c with invert = bool_of_string v }
4642 | "brightness" -> { c with colorscale = float_of_string v }
4643 | _ -> c
4644 with exn ->
4645 prerr_endline ("Error processing attribute (`" ^
4646 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
4649 let rec fold c = function
4650 | [] -> c
4651 | (k, v) :: rest ->
4652 let c = apply c k v in
4653 fold c rest
4655 fold c attrs;
4658 let fromstring f pos n v d =
4659 try f v
4660 with exn ->
4661 dolog "Error processing attribute (%S=%S) at %d\n%s"
4662 n v pos (Printexc.to_string exn)
4667 let bookmark_of attrs =
4668 let rec fold title page rely = function
4669 | ("title", v) :: rest -> fold v page rely rest
4670 | ("page", v) :: rest -> fold title v rely rest
4671 | ("rely", v) :: rest -> fold title page v rest
4672 | _ :: rest -> fold title page rely rest
4673 | [] -> title, page, rely
4675 fold "invalid" "0" "0" attrs
4678 let doc_of attrs =
4679 let rec fold path page rely pan = function
4680 | ("path", v) :: rest -> fold v page rely pan rest
4681 | ("page", v) :: rest -> fold path v rely pan rest
4682 | ("rely", v) :: rest -> fold path page v pan rest
4683 | ("pan", v) :: rest -> fold path page rely v rest
4684 | _ :: rest -> fold path page rely pan rest
4685 | [] -> path, page, rely, pan
4687 fold "" "0" "0" "0" attrs
4690 let setconf dst src =
4691 dst.scrollbw <- src.scrollbw;
4692 dst.scrollh <- src.scrollh;
4693 dst.icase <- src.icase;
4694 dst.preload <- src.preload;
4695 dst.pagebias <- src.pagebias;
4696 dst.verbose <- src.verbose;
4697 dst.scrollstep <- src.scrollstep;
4698 dst.maxhfit <- src.maxhfit;
4699 dst.crophack <- src.crophack;
4700 dst.autoscrollstep <- src.autoscrollstep;
4701 dst.maxwait <- src.maxwait;
4702 dst.hlinks <- src.hlinks;
4703 dst.underinfo <- src.underinfo;
4704 dst.interpagespace <- src.interpagespace;
4705 dst.zoom <- src.zoom;
4706 dst.presentation <- src.presentation;
4707 dst.angle <- src.angle;
4708 dst.winw <- src.winw;
4709 dst.winh <- src.winh;
4710 dst.savebmarks <- src.savebmarks;
4711 dst.memlimit <- src.memlimit;
4712 dst.proportional <- src.proportional;
4713 dst.texcount <- src.texcount;
4714 dst.sliceheight <- src.sliceheight;
4715 dst.thumbw <- src.thumbw;
4716 dst.jumpback <- src.jumpback;
4717 dst.bgcolor <- src.bgcolor;
4718 dst.scrollbarinpm <- src.scrollbarinpm;
4719 dst.tilew <- src.tilew;
4720 dst.tileh <- src.tileh;
4721 dst.mumemlimit <- src.mumemlimit;
4722 dst.checkers <- src.checkers;
4723 dst.aalevel <- src.aalevel;
4724 dst.trimmargins <- src.trimmargins;
4725 dst.trimfuzz <- src.trimfuzz;
4726 dst.urilauncher <- src.urilauncher;
4727 dst.colorspace <- src.colorspace;
4728 dst.invert <- src.invert;
4729 dst.colorscale <- src.colorscale;
4732 let get s =
4733 let h = Hashtbl.create 10 in
4734 let dc = { defconf with angle = defconf.angle } in
4735 let rec toplevel v t spos _ =
4736 match t with
4737 | Vdata | Vcdata | Vend -> v
4738 | Vopen ("llppconfig", _, closed) ->
4739 if closed
4740 then v
4741 else { v with f = llppconfig }
4742 | Vopen _ ->
4743 error "unexpected subelement at top level" s spos
4744 | Vclose _ -> error "unexpected close at top level" s spos
4746 and llppconfig v t spos _ =
4747 match t with
4748 | Vdata | Vcdata -> v
4749 | Vend -> error "unexpected end of input in llppconfig" s spos
4750 | Vopen ("defaults", attrs, closed) ->
4751 let c = config_of dc attrs in
4752 setconf dc c;
4753 if closed
4754 then v
4755 else { v with f = skip "defaults" (fun () -> v) }
4757 | Vopen ("ui-font", attrs, closed) ->
4758 let rec getsize size = function
4759 | [] -> size
4760 | ("size", v) :: rest ->
4761 let size =
4762 fromstring int_of_string spos "size" v fstate.fontsize in
4763 getsize size rest
4764 | l -> getsize size l
4766 fstate.fontsize <- getsize fstate.fontsize attrs;
4767 if closed
4768 then v
4769 else { v with f = uifont (Buffer.create 10) }
4771 | Vopen ("doc", attrs, closed) ->
4772 let pathent, spage, srely, span = doc_of attrs in
4773 let path = unent pathent
4774 and pageno = fromstring int_of_string spos "page" spage 0
4775 and rely = fromstring float_of_string spos "rely" srely 0.0
4776 and pan = fromstring int_of_string spos "pan" span 0 in
4777 let c = config_of dc attrs in
4778 let anchor = (pageno, rely) in
4779 if closed
4780 then (Hashtbl.add h path (c, [], pan, anchor); v)
4781 else { v with f = doc path pan anchor c [] }
4783 | Vopen _ ->
4784 error "unexpected subelement in llppconfig" s spos
4786 | Vclose "llppconfig" -> { v with f = toplevel }
4787 | Vclose _ -> error "unexpected close in llppconfig" s spos
4789 and uifont b v t spos epos =
4790 match t with
4791 | Vdata | Vcdata ->
4792 Buffer.add_substring b s spos (epos - spos);
4794 | Vopen (_, _, _) ->
4795 error "unexpected subelement in ui-font" s spos
4796 | Vclose "ui-font" ->
4797 if String.length !fontpath = 0
4798 then fontpath := Buffer.contents b;
4799 { v with f = llppconfig }
4800 | Vclose _ -> error "unexpected close in ui-font" s spos
4801 | Vend -> error "unexpected end of input in ui-font" s spos
4803 and doc path pan anchor c bookmarks v t spos _ =
4804 match t with
4805 | Vdata | Vcdata -> v
4806 | Vend -> error "unexpected end of input in doc" s spos
4807 | Vopen ("bookmarks", _, closed) ->
4808 if closed
4809 then v
4810 else { v with f = pbookmarks path pan anchor c bookmarks }
4812 | Vopen (_, _, _) ->
4813 error "unexpected subelement in doc" s spos
4815 | Vclose "doc" ->
4816 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
4817 { v with f = llppconfig }
4819 | Vclose _ -> error "unexpected close in doc" s spos
4821 and pbookmarks path pan anchor c bookmarks v t spos _ =
4822 match t with
4823 | Vdata | Vcdata -> v
4824 | Vend -> error "unexpected end of input in bookmarks" s spos
4825 | Vopen ("item", attrs, closed) ->
4826 let titleent, spage, srely = bookmark_of attrs in
4827 let page = fromstring int_of_string spos "page" spage 0
4828 and rely = fromstring float_of_string spos "rely" srely 0.0 in
4829 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
4830 if closed
4831 then { v with f = pbookmarks path pan anchor c bookmarks }
4832 else
4833 let f () = v in
4834 { v with f = skip "item" f }
4836 | Vopen _ ->
4837 error "unexpected subelement in bookmarks" s spos
4839 | Vclose "bookmarks" ->
4840 { v with f = doc path pan anchor c bookmarks }
4842 | Vclose _ -> error "unexpected close in bookmarks" s spos
4844 and skip tag f v t spos _ =
4845 match t with
4846 | Vdata | Vcdata -> v
4847 | Vend ->
4848 error ("unexpected end of input in skipped " ^ tag) s spos
4849 | Vopen (tag', _, closed) ->
4850 if closed
4851 then v
4852 else
4853 let f' () = { v with f = skip tag f } in
4854 { v with f = skip tag' f' }
4855 | Vclose ctag ->
4856 if tag = ctag
4857 then f ()
4858 else error ("unexpected close in skipped " ^ tag) s spos
4861 parse { f = toplevel; accu = () } s;
4862 h, dc;
4865 let do_load f ic =
4867 let len = in_channel_length ic in
4868 let s = String.create len in
4869 really_input ic s 0 len;
4870 f s;
4871 with
4872 | Parse_error (msg, s, pos) ->
4873 let subs = subs s pos in
4874 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
4875 failwith ("parse error: " ^ s)
4877 | exn ->
4878 failwith ("config load error: " ^ Printexc.to_string exn)
4881 let defconfpath =
4882 let dir =
4884 let dir = Filename.concat home ".config" in
4885 if Sys.is_directory dir then dir else home
4886 with _ -> home
4888 Filename.concat dir "llpp.conf"
4891 let confpath = ref defconfpath;;
4893 let load1 f =
4894 if Sys.file_exists !confpath
4895 then
4896 match
4897 (try Some (open_in_bin !confpath)
4898 with exn ->
4899 prerr_endline
4900 ("Error opening configuation file `" ^ !confpath ^ "': " ^
4901 Printexc.to_string exn);
4902 None
4904 with
4905 | Some ic ->
4906 begin try
4907 f (do_load get ic)
4908 with exn ->
4909 prerr_endline
4910 ("Error loading configuation from `" ^ !confpath ^ "': " ^
4911 Printexc.to_string exn);
4912 end;
4913 close_in ic;
4915 | None -> ()
4916 else
4917 f (Hashtbl.create 0, defconf)
4920 let load () =
4921 let f (h, dc) =
4922 let pc, pb, px, pa =
4924 Hashtbl.find h (Filename.basename state.path)
4925 with Not_found -> dc, [], 0, (0, 0.0)
4927 setconf defconf dc;
4928 setconf conf pc;
4929 state.bookmarks <- pb;
4930 state.x <- px;
4931 state.scrollw <- conf.scrollbw;
4932 if conf.jumpback
4933 then state.anchor <- pa;
4934 cbput state.hists.nav pa;
4936 load1 f
4939 let add_attrs bb always dc c =
4940 let ob s a b =
4941 if always || a != b
4942 then Printf.bprintf bb "\n %s='%b'" s a
4943 and oi s a b =
4944 if always || a != b
4945 then Printf.bprintf bb "\n %s='%d'" s a
4946 and oI s a b =
4947 if always || a != b
4948 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
4949 and oz s a b =
4950 if always || a <> b
4951 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
4952 and oF s a b =
4953 if always || a <> b
4954 then Printf.bprintf bb "\n %s='%f'" s a
4955 and oc s a b =
4956 if always || a <> b
4957 then
4958 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
4959 and oC s a b =
4960 if always || a <> b
4961 then
4962 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
4963 and oR s a b =
4964 if always || a <> b
4965 then
4966 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
4967 and os s a b =
4968 if always || a <> b
4969 then
4970 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
4971 and oW s a b =
4972 if always || a <> b
4973 then
4974 let v =
4975 match a with
4976 | None -> "false"
4977 | Some f ->
4978 if f = infinity
4979 then "true"
4980 else string_of_float f
4982 Printf.bprintf bb "\n %s='%s'" s v
4984 let w, h =
4985 if always
4986 then dc.winw, dc.winh
4987 else
4988 match state.fullscreen with
4989 | Some wh -> wh
4990 | None -> c.winw, c.winh
4992 let zoom, presentation, interpagespace, maxwait =
4993 if always
4994 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
4995 else
4996 match state.mode with
4997 | Birdseye (bc, _, _, _, _) ->
4998 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
4999 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5001 oi "width" w dc.winw;
5002 oi "height" h dc.winh;
5003 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5004 oi "scroll-handle-height" c.scrollh dc.scrollh;
5005 ob "case-insensitive-search" c.icase dc.icase;
5006 ob "preload" c.preload dc.preload;
5007 oi "page-bias" c.pagebias dc.pagebias;
5008 oi "scroll-step" c.scrollstep dc.scrollstep;
5009 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5010 ob "max-height-fit" c.maxhfit dc.maxhfit;
5011 ob "crop-hack" c.crophack dc.crophack;
5012 oW "throttle" maxwait dc.maxwait;
5013 ob "highlight-links" c.hlinks dc.hlinks;
5014 ob "under-cursor-info" c.underinfo dc.underinfo;
5015 oi "vertical-margin" interpagespace dc.interpagespace;
5016 oz "zoom" zoom dc.zoom;
5017 ob "presentation" presentation dc.presentation;
5018 oi "rotation-angle" c.angle dc.angle;
5019 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5020 ob "proportional-display" c.proportional dc.proportional;
5021 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5022 oi "tex-count" c.texcount dc.texcount;
5023 oi "slice-height" c.sliceheight dc.sliceheight;
5024 oi "thumbnail-width" c.thumbw dc.thumbw;
5025 ob "persistent-location" c.jumpback dc.jumpback;
5026 oc "background-color" c.bgcolor dc.bgcolor;
5027 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5028 oi "tile-width" c.tilew dc.tilew;
5029 oi "tile-height" c.tileh dc.tileh;
5030 oI "mupdf-memlimit" c.mumemlimit dc.mumemlimit;
5031 ob "checkers" c.checkers dc.checkers;
5032 oi "aalevel" c.aalevel dc.aalevel;
5033 ob "trim-margins" c.trimmargins dc.trimmargins;
5034 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5035 os "uri-launcher" c.urilauncher dc.urilauncher;
5036 oC "color-space" c.colorspace dc.colorspace;
5037 ob "invert-colors" c.invert dc.invert;
5038 oF "brightness" c.colorscale dc.colorscale;
5039 if always
5040 then ob "wmclass-hack" !wmclasshack false;
5043 let save () =
5044 let uifontsize = fstate.fontsize in
5045 let bb = Buffer.create 32768 in
5046 let f (h, dc) =
5047 let dc = if conf.bedefault then conf else dc in
5048 Buffer.add_string bb "<llppconfig>\n";
5050 if String.length !fontpath > 0
5051 then
5052 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5053 uifontsize
5054 !fontpath
5055 else (
5056 if uifontsize <> 14
5057 then
5058 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5061 Buffer.add_string bb "<defaults ";
5062 add_attrs bb true dc dc;
5063 Buffer.add_string bb "/>\n";
5065 let adddoc path pan anchor c bookmarks =
5066 if bookmarks == [] && c = dc && anchor = emptyanchor
5067 then ()
5068 else (
5069 Printf.bprintf bb "<doc path='%s'"
5070 (enent path 0 (String.length path));
5072 if anchor <> emptyanchor
5073 then (
5074 let n, y = anchor in
5075 Printf.bprintf bb " page='%d'" n;
5076 if y > 1e-6
5077 then
5078 Printf.bprintf bb " rely='%f'" y
5082 if pan != 0
5083 then Printf.bprintf bb " pan='%d'" pan;
5085 add_attrs bb false dc c;
5087 begin match bookmarks with
5088 | [] -> Buffer.add_string bb "/>\n"
5089 | _ ->
5090 Buffer.add_string bb ">\n<bookmarks>\n";
5091 List.iter (fun (title, _level, (page, rely)) ->
5092 Printf.bprintf bb
5093 "<item title='%s' page='%d'"
5094 (enent title 0 (String.length title))
5095 page
5097 if rely > 1e-6
5098 then
5099 Printf.bprintf bb " rely='%f'" rely
5101 Buffer.add_string bb "/>\n";
5102 ) bookmarks;
5103 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5104 end;
5108 let pan =
5109 match state.mode with
5110 | Birdseye (_, pan, _, _, _) -> pan
5111 | _ -> state.x
5113 let basename = Filename.basename state.path in
5114 adddoc basename pan (getanchor ())
5115 { conf with
5116 autoscrollstep =
5117 match state.autoscroll with
5118 | Some step -> step
5119 | None -> conf.autoscrollstep }
5120 (if conf.savebmarks then state.bookmarks else []);
5122 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5123 if basename <> path
5124 then adddoc path x y c bookmarks
5125 ) h;
5126 Buffer.add_string bb "</llppconfig>";
5128 load1 f;
5129 if Buffer.length bb > 0
5130 then
5132 let tmp = !confpath ^ ".tmp" in
5133 let oc = open_out_bin tmp in
5134 Buffer.output_buffer oc bb;
5135 close_out oc;
5136 Unix.rename tmp !confpath;
5137 with exn ->
5138 prerr_endline
5139 ("error while saving configuration: " ^ Printexc.to_string exn)
5141 end;;
5143 let () =
5144 Arg.parse
5145 (Arg.align
5146 [("-p", Arg.String (fun s -> state.password <- s) ,
5147 "<password> Set password");
5149 ("-f", Arg.String (fun s -> Config.fontpath := s),
5150 "<path> Set path to the user interface font");
5152 ("-c", Arg.String (fun s -> Config.confpath := s),
5153 "<path> Set path to the configuration file");
5155 ("-v", Arg.Unit (fun () ->
5156 Printf.printf
5157 "%s\nconfiguration path: %s\n"
5158 Help.version
5159 Config.defconfpath
5161 exit 0), " Print version and exit");
5164 (fun s -> state.path <- s)
5165 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5167 if String.length state.path = 0
5168 then (prerr_endline "file name missing"; exit 1);
5170 Config.load ();
5172 let _ = Glut.init Sys.argv in
5173 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5174 let () = Glut.initWindowSize conf.winw conf.winh in
5175 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5177 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5178 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5179 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5181 let csock, ssock =
5182 if not is_windows
5183 then
5184 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5185 else
5186 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5187 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5188 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5189 Unix.bind sock addr;
5190 Unix.listen sock 1;
5191 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5192 Unix.connect csock addr;
5193 let ssock, _ = Unix.accept sock in
5194 Unix.close sock;
5195 let opts sock =
5196 Unix.setsockopt sock Unix.TCP_NODELAY true;
5197 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5199 opts ssock;
5200 opts csock;
5201 ssock, csock
5204 let () = Glut.displayFunc display in
5205 let () = Glut.reshapeFunc reshape in
5206 let () = Glut.keyboardFunc keyboard in
5207 let () = Glut.specialFunc special in
5208 let () = Glut.idleFunc (Some idle) in
5209 let () = Glut.mouseFunc mouse in
5210 let () = Glut.motionFunc motion in
5211 let () = Glut.passiveMotionFunc pmotion in
5213 setcheckers conf.checkers;
5214 init ssock (
5215 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5216 conf.texcount, conf.sliceheight, conf.mumemlimit, conf.colorspace,
5217 !Config.wmclasshack, !Config.fontpath
5219 state.csock <- csock;
5220 state.ssock <- ssock;
5221 state.text <- "Opening " ^ state.path;
5222 setaalevel conf.aalevel;
5223 writeopen state.path state.password;
5224 state.uioh <- uioh;
5225 setfontsize fstate.fontsize;
5227 while true do
5229 Glut.mainLoop ();
5230 with
5231 | Glut.BadEnum "key in special_of_int" ->
5232 showtext '!' " LablGlut bug: special key not recognized";
5234 | Quit ->
5235 wcmd "quit" [];
5236 Config.save ();
5237 exit 0
5238 done;