Do not flash too much on throttled zoom
[llpp.git] / main.ml
blob135916af8db95b95d674e8674927f11764ef8f72
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;
1855 let enterbirdseye () =
1856 let zoom = float conf.thumbw /. float conf.winw in
1857 let birdseyepageno =
1858 let cy = conf.winh / 2 in
1859 let fold = function
1860 | [] -> 0
1861 | l :: rest ->
1862 let rec fold best = function
1863 | [] -> best.pageno
1864 | l :: rest ->
1865 let d = cy - (l.pagedispy + l.pagevh/2)
1866 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1867 if abs d < abs dbest
1868 then fold l rest
1869 else best.pageno
1870 in fold l rest
1872 fold state.layout
1874 state.mode <- Birdseye (
1875 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1877 conf.zoom <- zoom;
1878 conf.presentation <- false;
1879 conf.interpagespace <- 10;
1880 conf.hlinks <- false;
1881 state.x <- 0;
1882 state.mstate <- Mnone;
1883 conf.maxwait <- None;
1884 Glut.setCursor Glut.CURSOR_INHERIT;
1885 if conf.verbose
1886 then
1887 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1888 (100.0*.zoom)
1889 else
1890 state.text <- ""
1892 reshape conf.winw conf.winh;
1895 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1896 state.mode <- View;
1897 conf.zoom <- c.zoom;
1898 conf.presentation <- c.presentation;
1899 conf.interpagespace <- c.interpagespace;
1900 conf.maxwait <- c.maxwait;
1901 conf.hlinks <- c.hlinks;
1902 state.x <- leftx;
1903 if conf.verbose
1904 then
1905 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1906 (100.0*.conf.zoom)
1908 reshape conf.winw conf.winh;
1909 state.anchor <- if goback then anchor else (pageno, 0.0);
1912 let togglebirdseye () =
1913 match state.mode with
1914 | Birdseye vals -> leavebirdseye vals true
1915 | View -> enterbirdseye ()
1916 | _ -> ()
1919 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1920 let pageno = max 0 (pageno - 1) in
1921 let rec loop = function
1922 | [] -> gotopage1 pageno 0
1923 | l :: _ when l.pageno = pageno ->
1924 if l.pagedispy >= 0 && l.pagey = 0
1925 then G.postRedisplay "upbirdseye"
1926 else gotopage1 pageno 0
1927 | _ :: rest -> loop rest
1929 loop state.layout;
1930 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1933 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1934 let pageno = min (state.pagecount - 1) (pageno + 1) in
1935 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1936 let rec loop = function
1937 | [] ->
1938 let y, h = getpageyh pageno in
1939 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1940 gotoy (clamp dy)
1941 | l :: _ when l.pageno = pageno ->
1942 if l.pagevh != l.pageh
1943 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1944 else G.postRedisplay "downbirdseye"
1945 | _ :: rest -> loop rest
1947 loop state.layout
1950 let optentry mode _ key =
1951 let btos b = if b then "on" else "off" in
1952 let c = Char.unsafe_chr key in
1953 match c with
1954 | 's' ->
1955 let ondone s =
1956 try conf.scrollstep <- int_of_string s with exc ->
1957 state.text <- Printf.sprintf "bad integer `%s': %s"
1958 s (Printexc.to_string exc)
1960 TEswitch ("scroll step: ", "", None, intentry, ondone)
1962 | 'A' ->
1963 let ondone s =
1965 conf.autoscrollstep <- int_of_string s;
1966 if state.autoscroll <> None
1967 then state.autoscroll <- Some conf.autoscrollstep
1968 with exc ->
1969 state.text <- Printf.sprintf "bad integer `%s': %s"
1970 s (Printexc.to_string exc)
1972 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1974 | 'Z' ->
1975 let ondone s =
1977 let zoom = float (int_of_string s) /. 100.0 in
1978 setzoom zoom
1979 with exc ->
1980 state.text <- Printf.sprintf "bad integer `%s': %s"
1981 s (Printexc.to_string exc)
1983 TEswitch ("zoom: ", "", None, intentry, ondone)
1985 | 't' ->
1986 let ondone s =
1988 conf.thumbw <- bound (int_of_string s) 2 4096;
1989 state.text <-
1990 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1991 begin match mode with
1992 | Birdseye beye ->
1993 leavebirdseye beye false;
1994 enterbirdseye ();
1995 | _ -> ();
1997 with exc ->
1998 state.text <- Printf.sprintf "bad integer `%s': %s"
1999 s (Printexc.to_string exc)
2001 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2003 | 'R' ->
2004 let ondone s =
2005 match try
2006 Some (int_of_string s)
2007 with exc ->
2008 state.text <- Printf.sprintf "bad integer `%s': %s"
2009 s (Printexc.to_string exc);
2010 None
2011 with
2012 | Some angle -> reqlayout angle conf.proportional
2013 | None -> ()
2015 TEswitch ("rotation: ", "", None, intentry, ondone)
2017 | 'i' ->
2018 conf.icase <- not conf.icase;
2019 TEdone ("case insensitive search " ^ (btos conf.icase))
2021 | 'p' ->
2022 conf.preload <- not conf.preload;
2023 gotoy state.y;
2024 TEdone ("preload " ^ (btos conf.preload))
2026 | 'v' ->
2027 conf.verbose <- not conf.verbose;
2028 TEdone ("verbose " ^ (btos conf.verbose))
2030 | 'd' ->
2031 conf.debug <- not conf.debug;
2032 TEdone ("debug " ^ (btos conf.debug))
2034 | 'h' ->
2035 conf.maxhfit <- not conf.maxhfit;
2036 state.maxy <-
2037 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2038 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2040 | 'c' ->
2041 conf.crophack <- not conf.crophack;
2042 TEdone ("crophack " ^ btos conf.crophack)
2044 | 'a' ->
2045 let s =
2046 match conf.maxwait with
2047 | None ->
2048 conf.maxwait <- Some infinity;
2049 "always wait for page to complete"
2050 | Some _ ->
2051 conf.maxwait <- None;
2052 "show placeholder if page is not ready"
2054 TEdone s
2056 | 'f' ->
2057 conf.underinfo <- not conf.underinfo;
2058 TEdone ("underinfo " ^ btos conf.underinfo)
2060 | 'P' ->
2061 conf.savebmarks <- not conf.savebmarks;
2062 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2064 | 'S' ->
2065 let ondone s =
2067 let pageno, py =
2068 match state.layout with
2069 | [] -> 0, 0
2070 | l :: _ ->
2071 l.pageno, l.pagey
2073 conf.interpagespace <- int_of_string s;
2074 state.maxy <- calcheight ();
2075 let y = getpagey pageno in
2076 gotoy (y + py)
2077 with exc ->
2078 state.text <- Printf.sprintf "bad integer `%s': %s"
2079 s (Printexc.to_string exc)
2081 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2083 | 'l' ->
2084 reqlayout conf.angle (not conf.proportional);
2085 TEdone ("proportional display " ^ btos conf.proportional)
2087 | 'T' ->
2088 settrim (not conf.trimmargins) conf.trimfuzz;
2089 TEdone ("trim margins " ^ btos conf.trimmargins)
2091 | 'I' ->
2092 conf.invert <- not conf.invert;
2093 TEdone ("invert colors " ^ btos conf.invert)
2095 | _ ->
2096 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2097 TEstop
2100 class type lvsource = object
2101 method getitemcount : int
2102 method getitem : int -> (string * int)
2103 method hasaction : int -> bool
2104 method exit :
2105 uioh:uioh ->
2106 cancel:bool ->
2107 active:int ->
2108 first:int ->
2109 pan:int ->
2110 qsearch:string ->
2111 uioh option
2112 method getactive : int
2113 method getfirst : int
2114 method getqsearch : string
2115 method setqsearch : string -> unit
2116 method getpan : int
2117 end;;
2119 class virtual lvsourcebase = object
2120 val mutable m_active = 0
2121 val mutable m_first = 0
2122 val mutable m_qsearch = ""
2123 val mutable m_pan = 0
2124 method getactive = m_active
2125 method getfirst = m_first
2126 method getqsearch = m_qsearch
2127 method getpan = m_pan
2128 method setqsearch s = m_qsearch <- s
2129 end;;
2131 let textentryspecial key = function
2132 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2133 let s =
2134 match key with
2135 | Glut.KEY_UP -> action HCprev
2136 | Glut.KEY_DOWN -> action HCnext
2137 | Glut.KEY_HOME -> action HCfirst
2138 | Glut.KEY_END -> action HClast
2139 | _ -> state.text
2141 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2142 G.postRedisplay "special textentry";
2143 | _ -> ()
2146 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2147 let enttext te =
2148 state.mode <- Textentry (te, onleave);
2149 state.text <- "";
2150 enttext ();
2151 G.postRedisplay "textentrykeyboard enttext";
2153 match Char.unsafe_chr key with
2154 | '\008' -> (* backspace *)
2155 let len = String.length text in
2156 if len = 0
2157 then (
2158 onleave Cancel;
2159 G.postRedisplay "textentrykeyboard after cancel";
2161 else (
2162 let s = String.sub text 0 (len - 1) in
2163 enttext (c, s, opthist, onkey, ondone)
2166 | '\r' | '\n' ->
2167 ondone text;
2168 onleave Confirm;
2169 G.postRedisplay "textentrykeyboard after confirm"
2171 | '\007' (* ctrl-g *)
2172 | '\027' -> (* escape *)
2173 if String.length text = 0
2174 then (
2175 begin match opthist with
2176 | None -> ()
2177 | Some (_, onhistcancel) -> onhistcancel ()
2178 end;
2179 onleave Cancel;
2180 state.text <- "";
2181 G.postRedisplay "textentrykeyboard after cancel2"
2183 else (
2184 enttext (c, "", opthist, onkey, ondone)
2187 | '\127' -> () (* delete *)
2189 | _ ->
2190 begin match onkey text key with
2191 | TEdone text ->
2192 ondone text;
2193 onleave Confirm;
2194 G.postRedisplay "textentrykeyboard after confirm2";
2196 | TEcont text ->
2197 enttext (c, text, opthist, onkey, ondone);
2199 | TEstop ->
2200 onleave Cancel;
2201 state.text <- "";
2202 G.postRedisplay "textentrykeyboard after cancel3"
2204 | TEswitch te ->
2205 state.mode <- Textentry (te, onleave);
2206 G.postRedisplay "textentrykeyboard switch";
2207 end;
2210 let firstof first active =
2211 if first > active || abs (first - active) > fstate.maxrows - 1
2212 then max 0 (active - (fstate.maxrows/2))
2213 else first
2216 let calcfirst first active =
2217 if active > first
2218 then
2219 let rows = active - first in
2220 if rows > fstate.maxrows then active - fstate.maxrows else first
2221 else active
2224 let coe s = (s :> uioh);;
2226 class listview ~(source:lvsource) ~trusted =
2227 object (self)
2228 val m_pan = source#getpan
2229 val m_first = source#getfirst
2230 val m_active = source#getactive
2231 val m_qsearch = source#getqsearch
2232 val m_prev_uioh = state.uioh
2234 method private elemunder y =
2235 let n = y / (fstate.fontsize+1) in
2236 if m_first + n < source#getitemcount
2237 then (
2238 if source#hasaction (m_first + n)
2239 then Some (m_first + n)
2240 else None
2242 else None
2244 method display =
2245 Gl.enable `blend;
2246 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2247 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2248 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2249 GlDraw.color (1., 1., 1.);
2250 Gl.enable `texture_2d;
2251 let fs = fstate.fontsize in
2252 let nfs = fs + 1 in
2253 let ww = fstate.wwidth in
2254 let tabw = 30.0*.ww in
2255 let rec loop row =
2256 if (row - m_first) * nfs > conf.winh
2257 then ()
2258 else (
2259 if row >= 0 && row < source#getitemcount
2260 then (
2261 let (s, level) = source#getitem row in
2262 let y = (row - m_first) * nfs in
2263 let x = 5.0 +. float (level + m_pan) *. ww in
2264 if row = m_active
2265 then (
2266 Gl.disable `texture_2d;
2267 GlDraw.polygon_mode `both `line;
2268 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2269 GlDraw.rect (1., float (y + 1))
2270 (float (conf.winw - 1), float (y + fs + 3));
2271 GlDraw.polygon_mode `both `fill;
2272 GlDraw.color (1., 1., 1.);
2273 Gl.enable `texture_2d;
2276 let drawtabularstring s =
2277 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2278 if trusted
2279 then
2280 let tabpos = try String.index s '\t' with Not_found -> -1 in
2281 if tabpos > 0
2282 then
2283 let len = String.length s - tabpos - 1 in
2284 let s1 = String.sub s 0 tabpos
2285 and s2 = String.sub s (tabpos + 1) len in
2286 let nx = drawstr x s1 in
2287 let sw = nx -. x in
2288 let x = x +. (max tabw sw) in
2289 drawstr x s2
2290 else
2291 drawstr x s
2292 else
2293 drawstr x s
2295 let _ = drawtabularstring s in
2296 loop (row+1)
2300 loop 0;
2301 Gl.disable `blend;
2302 Gl.disable `texture_2d;
2304 method updownlevel incr =
2305 let len = source#getitemcount in
2306 let _, curlevel = source#getitem m_active in
2307 let rec flow i =
2308 if i = len then i-1 else if i = -1 then 0 else
2309 let _, l = source#getitem i in
2310 if l != curlevel then i else flow (i+incr)
2312 let active = flow m_active in
2313 let first = calcfirst m_first active in
2314 G.postRedisplay "special outline updownlevel";
2315 {< m_active = active; m_first = first >}
2317 method private key1 key =
2318 let set active first qsearch =
2319 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2321 let search active pattern incr =
2322 let dosearch re =
2323 let rec loop n =
2324 if n >= 0 && n < source#getitemcount
2325 then (
2326 let s, _ = source#getitem n in
2328 (try ignore (Str.search_forward re s 0); true
2329 with Not_found -> false)
2330 then Some n
2331 else loop (n + incr)
2333 else None
2335 loop active
2338 let re = Str.regexp_case_fold pattern in
2339 dosearch re
2340 with Failure s ->
2341 state.text <- s;
2342 None
2344 match key with
2345 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2346 let incr = if key = 18 then -1 else 1 in
2347 let active, first =
2348 match search (m_active + incr) m_qsearch incr with
2349 | None ->
2350 state.text <- m_qsearch ^ " [not found]";
2351 m_active, m_first
2352 | Some active ->
2353 state.text <- m_qsearch;
2354 active, firstof m_first active
2356 G.postRedisplay "listview ctrl-r/s";
2357 set active first m_qsearch;
2359 | 8 -> (* backspace *)
2360 let len = String.length m_qsearch in
2361 if len = 0
2362 then coe self
2363 else (
2364 if len = 1
2365 then (
2366 state.text <- "";
2367 G.postRedisplay "listview empty qsearch";
2368 set m_active m_first "";
2370 else
2371 let qsearch = String.sub m_qsearch 0 (len - 1) in
2372 let active, first =
2373 match search m_active qsearch ~-1 with
2374 | None ->
2375 state.text <- qsearch ^ " [not found]";
2376 m_active, m_first
2377 | Some active ->
2378 state.text <- qsearch;
2379 active, firstof m_first active
2381 G.postRedisplay "listview backspace qsearch";
2382 set active first qsearch
2385 | _ when key >= 32 && key < 127 ->
2386 let pattern = addchar m_qsearch (Char.chr key) in
2387 let active, first =
2388 match search m_active pattern 1 with
2389 | None ->
2390 state.text <- pattern ^ " [not found]";
2391 m_active, m_first
2392 | Some active ->
2393 state.text <- pattern;
2394 active, firstof m_first active
2396 G.postRedisplay "listview qsearch add";
2397 set active first pattern;
2399 | 27 -> (* escape *)
2400 state.text <- "";
2401 if String.length m_qsearch = 0
2402 then (
2403 G.postRedisplay "list view escape";
2404 begin
2405 match
2406 source#exit (coe self) true m_active m_first m_pan m_qsearch
2407 with
2408 | None -> m_prev_uioh
2409 | Some uioh -> uioh
2412 else (
2413 G.postRedisplay "list view kill qsearch";
2414 source#setqsearch "";
2415 coe {< m_qsearch = "" >}
2418 | 13 -> (* enter *)
2419 state.text <- "";
2420 let self = {< m_qsearch = "" >} in
2421 source#setqsearch "";
2422 let opt =
2423 G.postRedisplay "listview enter";
2424 if m_active >= 0 && m_active < source#getitemcount
2425 then (
2426 source#exit (coe self) false m_active m_first m_pan "";
2428 else (
2429 source#exit (coe self) true m_active m_first m_pan "";
2432 begin match opt with
2433 | None -> m_prev_uioh
2434 | Some uioh -> uioh
2437 | 127 -> (* delete *)
2438 coe self
2440 | _ -> dolog "unknown key %d" key; coe self
2442 method private special1 key =
2443 let itemcount = source#getitemcount in
2444 let find start incr =
2445 let rec find i =
2446 if i = -1 || i = itemcount
2447 then -1
2448 else (
2449 if source#hasaction i
2450 then i
2451 else find (i + incr)
2454 find start
2456 let set active first =
2457 let first = bound first 0 (itemcount - fstate.maxrows) in
2458 state.text <- "";
2459 coe {< m_active = active; m_first = first >}
2461 let navigate incr =
2462 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2463 let active, first =
2464 let incr1 = if incr > 0 then 1 else -1 in
2465 if isvisible m_first m_active
2466 then
2467 let next =
2468 let next = m_active + incr in
2469 let next =
2470 if next < 0 || next >= itemcount
2471 then -1
2472 else find next incr1
2474 if next = -1 || abs (m_active - next) > fstate.maxrows
2475 then -1
2476 else next
2478 if next = -1
2479 then
2480 let first = m_first + incr in
2481 let first = bound first 0 (itemcount - 1) in
2482 let next =
2483 let next = m_active + incr in
2484 let next = bound next 0 (itemcount - 1) in
2485 find next ~-incr1
2487 let active = if next = -1 then m_active else next in
2488 active, first
2489 else
2490 let first = min next m_first in
2491 next, first
2492 else
2493 let first = m_first + incr in
2494 let first = bound first 0 (itemcount - 1) in
2495 let active =
2496 let next = m_active + incr in
2497 let next = bound next 0 (itemcount - 1) in
2498 let next = find next incr1 in
2499 if next = -1 || abs (m_active - first) > fstate.maxrows
2500 then m_active
2501 else next
2503 active, first
2505 G.postRedisplay "listview navigate";
2506 set active first;
2508 begin match key with
2509 | Glut.KEY_UP -> navigate ~-1
2510 | Glut.KEY_DOWN -> navigate 1
2511 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2512 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2514 | Glut.KEY_RIGHT ->
2515 state.text <- "";
2516 G.postRedisplay "listview right";
2517 coe {< m_pan = m_pan - 1 >}
2519 | Glut.KEY_LEFT ->
2520 state.text <- "";
2521 G.postRedisplay "listview left";
2522 coe {< m_pan = m_pan + 1 >}
2524 | Glut.KEY_HOME ->
2525 let active = find 0 1 in
2526 G.postRedisplay "listview home";
2527 set active 0;
2529 | Glut.KEY_END ->
2530 let first = max 0 (itemcount - fstate.maxrows) in
2531 let active = find (itemcount - 1) ~-1 in
2532 G.postRedisplay "listview end";
2533 set active first;
2535 | _ -> coe self
2536 end;
2538 method key key =
2539 match state.mode with
2540 | Textentry te -> textentrykeyboard key te; coe self
2541 | _ -> self#key1 key
2543 method special key =
2544 match state.mode with
2545 | Textentry te -> textentryspecial key te; coe self
2546 | _ -> self#special1 key
2548 method button button bstate _ y =
2549 let opt =
2550 match button with
2551 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2552 begin match self#elemunder y with
2553 | Some n ->
2554 G.postRedisplay "listview click";
2555 source#exit
2556 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2557 | _ ->
2558 Some (coe self)
2560 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2561 let len = source#getitemcount in
2562 let first =
2563 if m_first + fstate.maxrows >= len
2564 then
2565 m_first
2566 else
2567 let first = m_first + (if n == 3 then -1 else 1) in
2568 bound first 0 (len - 1)
2570 G.postRedisplay "listview wheel";
2571 Some (coe {< m_first = first >})
2572 | _ ->
2573 Some (coe self)
2575 match opt with
2576 | None -> m_prev_uioh
2577 | Some uioh -> uioh
2579 method motion _ _ = coe self
2581 method pmotion _ y =
2582 let n =
2583 match self#elemunder y with
2584 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2585 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2587 let o =
2588 if n != m_active
2589 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2590 else self
2592 coe o
2594 method infochanged _ = ()
2595 end;;
2597 class outlinelistview ~source =
2598 object (self)
2599 inherit listview ~source:(source :> lvsource) ~trusted:false as super
2601 method key key =
2602 match key with
2603 | 14 -> (* ctrl-n *)
2604 source#narrow m_qsearch;
2605 G.postRedisplay "outline ctrl-n";
2606 coe {< m_first = 0; m_active = 0 >}
2608 | 21 -> (* ctrl-u *)
2609 source#denarrow;
2610 G.postRedisplay "outline ctrl-u";
2611 coe {< m_first = 0; m_active = 0 >}
2613 | 12 -> (* ctrl-l *)
2614 let first = m_active - (fstate.maxrows / 2) in
2615 G.postRedisplay "outline ctrl-l";
2616 coe {< m_first = first >}
2618 | 127 -> (* delete *)
2619 source#remove m_active;
2620 G.postRedisplay "outline delete";
2621 let active = max 0 (m_active-1) in
2622 coe {< m_first = firstof m_first active;
2623 m_active = active >}
2625 | key -> super#key key
2627 method special key =
2628 let calcfirst first active =
2629 if active > first
2630 then
2631 let rows = active - first in
2632 if rows > fstate.maxrows then active - fstate.maxrows else first
2633 else active
2635 let navigate incr =
2636 let active = m_active + incr in
2637 let active = bound active 0 (source#getitemcount - 1) in
2638 let first = calcfirst m_first active in
2639 G.postRedisplay "special outline navigate";
2640 coe {< m_active = active; m_first = first >}
2642 match key with
2643 | Glut.KEY_UP -> navigate ~-1
2644 | Glut.KEY_DOWN -> navigate 1
2645 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2646 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2648 | Glut.KEY_RIGHT ->
2649 let o =
2650 if Glut.getModifiers () land Glut.active_ctrl != 0
2651 then (
2652 G.postRedisplay "special outline right";
2653 {< m_pan = m_pan + 1 >}
2655 else self#updownlevel 1
2657 coe o
2659 | Glut.KEY_LEFT ->
2660 let o =
2661 if Glut.getModifiers () land Glut.active_ctrl != 0
2662 then (
2663 G.postRedisplay "special outline left";
2664 {< m_pan = m_pan - 1 >}
2666 else self#updownlevel ~-1
2668 coe o
2670 | Glut.KEY_HOME ->
2671 G.postRedisplay "special outline home";
2672 coe {< m_first = 0; m_active = 0 >}
2674 | Glut.KEY_END ->
2675 let active = source#getitemcount - 1 in
2676 let first = max 0 (active - fstate.maxrows) in
2677 G.postRedisplay "special outline end";
2678 coe {< m_active = active; m_first = first >}
2680 | _ -> super#special key
2683 let outlinesource usebookmarks =
2684 let empty = [||] in
2685 (object
2686 inherit lvsourcebase
2687 val mutable m_items = empty
2688 val mutable m_orig_items = empty
2689 val mutable m_prev_items = empty
2690 val mutable m_narrow_pattern = ""
2691 val mutable m_hadremovals = false
2693 method getitemcount =
2694 Array.length m_items + (if m_hadremovals then 1 else 0)
2696 method getitem n =
2697 if n == Array.length m_items && m_hadremovals
2698 then
2699 ("[Confirm removal]", 0)
2700 else
2701 let s, n, _ = m_items.(n) in
2702 (s, n)
2704 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
2705 ignore (uioh, first, pan, qsearch);
2706 let confrimremoval = m_hadremovals && active = Array.length m_items in
2707 let items =
2708 if String.length m_narrow_pattern = 0
2709 then m_orig_items
2710 else m_items
2712 if not cancel
2713 then (
2714 if not confrimremoval
2715 then(
2716 let _, _, anchor = m_items.(active) in
2717 gotoanchor anchor;
2718 m_items <- items;
2720 else (
2721 state.bookmarks <- Array.to_list m_items;
2722 m_orig_items <- m_items;
2725 else m_items <- items;
2726 None
2728 method hasaction _ = true
2730 method greetmsg =
2731 if Array.length m_items != Array.length m_orig_items
2732 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
2733 else ""
2735 method narrow pattern =
2736 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2737 match reopt with
2738 | None -> ()
2739 | Some re ->
2740 let rec loop accu n =
2741 if n = -1
2742 then (
2743 m_narrow_pattern <- pattern;
2744 m_items <- Array.of_list accu
2746 else
2747 let (s, _, _) as o = m_items.(n) in
2748 let accu =
2749 if (try ignore (Str.search_forward re s 0); true
2750 with Not_found -> false)
2751 then o :: accu
2752 else accu
2754 loop accu (n-1)
2756 loop [] (Array.length m_items - 1)
2758 method denarrow =
2759 m_orig_items <- (
2760 if usebookmarks
2761 then Array.of_list state.bookmarks
2762 else state.outlines
2764 m_items <- m_orig_items
2766 method remove m =
2767 if usebookmarks
2768 then
2769 if m >= 0 && m < Array.length m_items
2770 then (
2771 m_hadremovals <- true;
2772 m_items <- Array.init (Array.length m_items - 1) (fun n ->
2773 let n = if n >= m then n+1 else n in
2774 m_items.(n)
2778 method reset pageno items =
2779 m_hadremovals <- false;
2780 if m_orig_items == empty || m_prev_items != items
2781 then (
2782 m_orig_items <- items;
2783 if String.length m_narrow_pattern = 0
2784 then m_items <- items;
2786 m_prev_items <- items;
2787 let active =
2788 let rec loop n best bestd =
2789 if n = Array.length m_items
2790 then best
2791 else
2792 let (_, _, (outlinepageno, _)) = m_items.(n) in
2793 let d = abs (outlinepageno - pageno) in
2794 if d < bestd
2795 then loop (n+1) n d
2796 else loop (n+1) best bestd
2798 loop 0 ~-1 max_int
2800 m_active <- active;
2801 m_first <- firstof m_first active
2802 end)
2805 let enterselector usebookmarks =
2806 let source = outlinesource usebookmarks in
2807 fun errmsg ->
2808 let outlines =
2809 if usebookmarks
2810 then Array.of_list state.bookmarks
2811 else state.outlines
2813 if Array.length outlines = 0
2814 then (
2815 showtext ' ' errmsg;
2817 else (
2818 state.text <- source#greetmsg;
2819 Glut.setCursor Glut.CURSOR_INHERIT;
2820 let pageno =
2821 match state.layout with
2822 | [] -> -1
2823 | {pageno=pageno} :: _ -> pageno
2825 source#reset pageno outlines;
2826 state.uioh <- coe (new outlinelistview ~source);
2827 G.postRedisplay "enter selector";
2831 let enteroutlinemode =
2832 let f = enterselector false in
2833 fun ()-> f "Document has no outline";
2836 let enterbookmarkmode =
2837 let f = enterselector true in
2838 fun () -> f "Document has no bookmarks (yet)";
2841 let color_of_string s =
2842 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
2843 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
2847 let color_to_string (r, g, b) =
2848 let r = truncate (r *. 256.0)
2849 and g = truncate (g *. 256.0)
2850 and b = truncate (b *. 256.0) in
2851 Printf.sprintf "%d/%d/%d" r g b
2854 let irect_of_string s =
2855 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
2858 let irect_to_string (x0,y0,x1,y1) =
2859 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
2862 let makecheckers () =
2863 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
2864 following to say:
2865 converted by Issac Trotts. July 25, 2002 *)
2866 let image_height = 64
2867 and image_width = 64 in
2869 let make_image () =
2870 let image =
2871 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
2873 for i = 0 to image_width - 1 do
2874 for j = 0 to image_height - 1 do
2875 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
2876 (if (i land 8 ) lxor (j land 8) = 0
2877 then [|255;255;255|] else [|200;200;200|])
2878 done
2879 done;
2880 image
2882 let image = make_image () in
2883 let id = GlTex.gen_texture () in
2884 GlTex.bind_texture `texture_2d id;
2885 GlPix.store (`unpack_alignment 1);
2886 GlTex.image2d image;
2887 List.iter (GlTex.parameter ~target:`texture_2d)
2888 [ `wrap_s `repeat;
2889 `wrap_t `repeat;
2890 `mag_filter `nearest;
2891 `min_filter `nearest ];
2895 let setcheckers enabled =
2896 match state.texid with
2897 | None ->
2898 if enabled then state.texid <- Some (makecheckers ())
2900 | Some texid ->
2901 if not enabled
2902 then (
2903 GlTex.delete_texture texid;
2904 state.texid <- None;
2908 let int_of_string_with_suffix s =
2909 let l = String.length s in
2910 let s1, shift =
2911 if l > 1
2912 then
2913 let suffix = Char.lowercase s.[l-1] in
2914 match suffix with
2915 | 'k' -> String.sub s 0 (l-1), 10
2916 | 'm' -> String.sub s 0 (l-1), 20
2917 | 'g' -> String.sub s 0 (l-1), 30
2918 | _ -> s, 0
2919 else s, 0
2921 let n = int_of_string s1 in
2922 let m = n lsl shift in
2923 if m < 0 || m < n
2924 then raise (Failure "value too large")
2925 else m
2928 let string_with_suffix_of_int n =
2929 if n = 0
2930 then "0"
2931 else
2932 let n, s =
2933 if n = 0
2934 then 0, ""
2935 else (
2936 if n land ((1 lsl 20) - 1) = 0
2937 then n lsr 20, "M"
2938 else (
2939 if n land ((1 lsl 10) - 1) = 0
2940 then n lsr 10, "K"
2941 else n, ""
2945 let rec loop s n =
2946 let h = n mod 1000 in
2947 let n = n / 1000 in
2948 if n = 0
2949 then string_of_int h ^ s
2950 else (
2951 let s = Printf.sprintf "_%03d%s" h s in
2952 loop s n
2955 loop "" n ^ s;
2958 let describe_location () =
2959 let f (fn, _) l =
2960 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
2962 let fn, ln = List.fold_left f (-1, -1) state.layout in
2963 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2964 let percent =
2965 if maxy <= 0
2966 then 100.
2967 else (100. *. (float state.y /. float maxy))
2969 if fn = ln
2970 then
2971 Printf.sprintf "page %d of %d [%.2f%%]"
2972 (fn+1) state.pagecount percent
2973 else
2974 Printf.sprintf
2975 "pages %d-%d of %d [%.2f%%]"
2976 (fn+1) (ln+1) state.pagecount percent
2979 let enterinfomode =
2980 let btos b = if b then "\xe2\x88\x9a" else "" in
2981 let showextended = ref false in
2982 let leave mode = function
2983 | Confirm -> state.mode <- mode
2984 | Cancel -> state.mode <- mode in
2985 let src =
2986 (object
2987 val mutable m_first_time = true
2988 val mutable m_l = []
2989 val mutable m_a = [||]
2990 val mutable m_prev_uioh = nouioh
2991 val mutable m_prev_mode = View
2993 inherit lvsourcebase
2995 method reset prev_mode prev_uioh =
2996 m_a <- Array.of_list (List.rev m_l);
2997 m_l <- [];
2998 m_prev_mode <- prev_mode;
2999 m_prev_uioh <- prev_uioh;
3000 if m_first_time
3001 then (
3002 let rec loop n =
3003 if n >= Array.length m_a
3004 then ()
3005 else
3006 match m_a.(n) with
3007 | _, _, _, Action _ -> m_active <- n
3008 | _ -> loop (n+1)
3010 loop 0;
3011 m_first_time <- false;
3014 method int name get set =
3015 m_l <-
3016 (name, `int get, 1, Action (
3017 fun u ->
3018 let ondone s =
3019 try set (int_of_string s)
3020 with exn ->
3021 state.text <- Printf.sprintf "bad integer `%s': %s"
3022 s (Printexc.to_string exn)
3024 state.text <- "";
3025 let te = name ^ ": ", "", None, intentry, ondone in
3026 state.mode <- Textentry (te, leave m_prev_mode);
3028 )) :: m_l
3030 method int_with_suffix name get set =
3031 m_l <-
3032 (name, `intws get, 1, Action (
3033 fun u ->
3034 let ondone s =
3035 try set (int_of_string_with_suffix s)
3036 with exn ->
3037 state.text <- Printf.sprintf "bad integer `%s': %s"
3038 s (Printexc.to_string exn)
3040 state.text <- "";
3041 let te =
3042 name ^ ": ", "", None, intentry_with_suffix, ondone
3044 state.mode <- Textentry (te, leave m_prev_mode);
3046 )) :: m_l
3048 method bool ?(offset=1) ?(btos=btos) name get set =
3049 m_l <-
3050 (name, `bool (btos, get), offset, Action (
3051 fun u ->
3052 let v = get () in
3053 set (not v);
3055 )) :: m_l
3057 method color name get set =
3058 m_l <-
3059 (name, `color get, 1, Action (
3060 fun u ->
3061 let invalid = (nan, nan, nan) in
3062 let ondone s =
3063 let c =
3064 try color_of_string s
3065 with exn ->
3066 state.text <- Printf.sprintf "bad color `%s': %s"
3067 s (Printexc.to_string exn);
3068 invalid
3070 if c <> invalid
3071 then set c;
3073 let te = name ^ ": ", "", None, textentry, ondone in
3074 state.text <- color_to_string (get ());
3075 state.mode <- Textentry (te, leave m_prev_mode);
3077 )) :: m_l
3079 method string name get set =
3080 m_l <-
3081 (name, `string get, 1, Action (
3082 fun u ->
3083 let ondone s = set s in
3084 let te = name ^ ": ", "", None, textentry, ondone in
3085 state.mode <- Textentry (te, leave m_prev_mode);
3087 )) :: m_l
3089 method colorspace name get set =
3090 m_l <-
3091 (name, `string get, 1, Action (
3092 fun _ ->
3093 let source =
3094 let vals = [| "rgb"; "bgr"; "gray" |] in
3095 (object
3096 inherit lvsourcebase
3098 initializer
3099 m_active <- int_of_colorspace conf.colorspace;
3100 m_first <- 0;
3102 method getitemcount = Array.length vals
3103 method getitem n = (vals.(n), 0)
3104 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3105 ignore (uioh, first, pan, qsearch);
3106 if not cancel then set active;
3107 None
3108 method hasaction _ = true
3109 end)
3111 state.text <- "";
3112 coe (new listview ~source ~trusted:true)
3113 )) :: m_l
3115 method caption s offset =
3116 m_l <- (s, `empty, offset, Noaction) :: m_l
3118 method caption2 s f offset =
3119 m_l <- (s, `string f, offset, Noaction) :: m_l
3121 method getitemcount = Array.length m_a
3123 method getitem n =
3124 let tostr = function
3125 | `int f -> string_of_int (f ())
3126 | `intws f -> string_with_suffix_of_int (f ())
3127 | `string f -> f ()
3128 | `color f -> color_to_string (f ())
3129 | `bool (btos, f) -> btos (f ())
3130 | `empty -> ""
3132 let name, t, offset, _ = m_a.(n) in
3133 ((let s = tostr t in
3134 if String.length s > 0
3135 then Printf.sprintf "%s\t%s" name s
3136 else name),
3137 offset)
3139 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3140 let uiohopt =
3141 if not cancel
3142 then (
3143 m_qsearch <- qsearch;
3144 let uioh =
3145 match m_a.(active) with
3146 | _, _, _, Action f -> f uioh
3147 | _ -> uioh
3149 Some uioh
3151 else None
3153 m_active <- active;
3154 m_first <- first;
3155 m_pan <- pan;
3156 uiohopt
3158 method hasaction n =
3159 match m_a.(n) with
3160 | _, _, _, Action _ -> true
3161 | _ -> false
3162 end)
3164 let rec fillsrc prevmode prevuioh =
3165 let sep () = src#caption "" 0 in
3166 let colorp name get set =
3167 src#string name
3168 (fun () -> color_to_string (get ()))
3169 (fun v ->
3171 let c = color_of_string v in
3172 set c
3173 with exn ->
3174 state.text <- Printf.sprintf "bad color `%s': %s"
3175 v (Printexc.to_string exn);
3178 let oldmode = state.mode in
3179 let birdseye = isbirdseye state.mode in
3181 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3183 src#bool "presentation mode"
3184 (fun () -> conf.presentation)
3185 (fun v ->
3186 conf.presentation <- v;
3187 state.anchor <- getanchor ();
3188 represent ());
3190 src#bool "ignore case in searches"
3191 (fun () -> conf.icase)
3192 (fun v -> conf.icase <- v);
3194 src#bool "preload"
3195 (fun () -> conf.preload)
3196 (fun v -> conf.preload <- v);
3198 src#bool "highlight links"
3199 (fun () -> conf.hlinks)
3200 (fun v -> conf.hlinks <- v);
3202 src#bool "under info"
3203 (fun () -> conf.underinfo)
3204 (fun v -> conf.underinfo <- v);
3206 src#bool "persistent bookmarks"
3207 (fun () -> conf.savebmarks)
3208 (fun v -> conf.savebmarks <- v);
3210 src#bool "proportional display"
3211 (fun () -> conf.proportional)
3212 (fun v -> reqlayout conf.angle v);
3214 src#bool "trim margins"
3215 (fun () -> conf.trimmargins)
3216 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3218 src#bool "persistent location"
3219 (fun () -> conf.jumpback)
3220 (fun v -> conf.jumpback <- v);
3222 sep ();
3223 src#int "vertical margin"
3224 (fun () -> conf.interpagespace)
3225 (fun n ->
3226 conf.interpagespace <- n;
3227 let pageno, py =
3228 match state.layout with
3229 | [] -> 0, 0
3230 | l :: _ ->
3231 l.pageno, l.pagey
3233 state.maxy <- calcheight ();
3234 let y = getpagey pageno in
3235 gotoy (y + py)
3238 src#int "page bias"
3239 (fun () -> conf.pagebias)
3240 (fun v -> conf.pagebias <- v);
3242 src#int "scroll step"
3243 (fun () -> conf.scrollstep)
3244 (fun n -> conf.scrollstep <- n);
3246 src#int "auto scroll step"
3247 (fun () ->
3248 match state.autoscroll with
3249 | Some step -> step
3250 | _ -> conf.autoscrollstep)
3251 (fun n ->
3252 if state.autoscroll <> None
3253 then state.autoscroll <- Some n;
3254 conf.autoscrollstep <- n);
3256 src#int "zoom"
3257 (fun () -> truncate (conf.zoom *. 100.))
3258 (fun v -> setzoom ((float v) /. 100.));
3260 src#int "rotation"
3261 (fun () -> conf.angle)
3262 (fun v -> reqlayout v conf.proportional);
3264 src#int "scroll bar width"
3265 (fun () -> state.scrollw)
3266 (fun v ->
3267 state.scrollw <- v;
3268 conf.scrollbw <- v;
3269 reshape conf.winw conf.winh;
3272 src#int "scroll handle height"
3273 (fun () -> conf.scrollh)
3274 (fun v -> conf.scrollh <- v;);
3276 src#int "thumbnail width"
3277 (fun () -> conf.thumbw)
3278 (fun v ->
3279 conf.thumbw <- min 4096 v;
3280 match oldmode with
3281 | Birdseye beye ->
3282 leavebirdseye beye false;
3283 enterbirdseye ()
3284 | _ -> ()
3287 sep ();
3288 src#caption "Presentation mode" 0;
3289 src#bool "scrollbar visible"
3290 (fun () -> conf.scrollbarinpm)
3291 (fun v ->
3292 if v != conf.scrollbarinpm
3293 then (
3294 conf.scrollbarinpm <- v;
3295 if conf.presentation
3296 then (
3297 state.scrollw <- if v then conf.scrollbw else 0;
3298 reshape conf.winw conf.winh;
3303 sep ();
3304 src#caption "Pixmap cache" 0;
3305 src#int_with_suffix "size (advisory)"
3306 (fun () -> conf.memlimit)
3307 (fun v -> conf.memlimit <- v);
3309 src#caption2 "used"
3310 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3311 (string_with_suffix_of_int state.memused)
3312 (Hashtbl.length state.tilemap)) 1;
3314 sep ();
3315 src#caption "Layout" 0;
3316 src#caption2 "Dimension"
3317 (fun () ->
3318 Printf.sprintf "%dx%d (virtual %dx%d)"
3319 conf.winw conf.winh
3320 state.w state.maxy)
3322 if conf.debug
3323 then
3324 src#caption2 "Position" (fun () ->
3325 Printf.sprintf "%dx%d" state.x state.y
3327 else
3328 src#caption2 "Visible" (fun () -> describe_location ()) 1
3331 sep ();
3332 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3333 "Save these parameters as global defaults at exit"
3334 (fun () -> conf.bedefault)
3335 (fun v -> conf.bedefault <- v)
3338 sep ();
3339 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3340 src#bool ~offset:0 ~btos "Extended parameters"
3341 (fun () -> !showextended)
3342 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3343 if !showextended
3344 then (
3345 src#bool "checkers"
3346 (fun () -> conf.checkers)
3347 (fun v -> conf.checkers <- v; setcheckers v);
3348 src#bool "verbose"
3349 (fun () -> conf.verbose)
3350 (fun v -> conf.verbose <- v);
3351 src#bool "invert colors"
3352 (fun () -> conf.invert)
3353 (fun v -> conf.invert <- v);
3354 src#bool "max fit"
3355 (fun () -> conf.maxhfit)
3356 (fun v -> conf.maxhfit <- v);
3357 src#string "uri launcher"
3358 (fun () -> conf.urilauncher)
3359 (fun v -> conf.urilauncher <- v);
3360 src#string "tile size"
3361 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3362 (fun v ->
3364 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3365 conf.tileh <- max 64 w;
3366 conf.tilew <- max 64 h;
3367 flushtiles ();
3368 with exn ->
3369 state.text <- Printf.sprintf "bad tile size `%s': %s"
3370 v (Printexc.to_string exn));
3371 src#int "anti-aliasing level"
3372 (fun () -> conf.aalevel)
3373 (fun v ->
3374 conf.aalevel <- bound v 0 8;
3375 state.anchor <- getanchor ();
3376 opendoc state.path state.password;
3378 src#int "ui font size"
3379 (fun () -> fstate.fontsize)
3380 (fun v -> setfontsize (bound v 5 100));
3381 colorp "background color"
3382 (fun () -> conf.bgcolor)
3383 (fun v -> conf.bgcolor <- v);
3384 src#bool "crop hack"
3385 (fun () -> conf.crophack)
3386 (fun v -> conf.crophack <- v);
3387 src#string "trim fuzz"
3388 (fun () -> irect_to_string conf.trimfuzz)
3389 (fun v ->
3391 conf.trimfuzz <- irect_of_string v;
3392 if conf.trimmargins
3393 then settrim true conf.trimfuzz;
3394 with exn ->
3395 state.text <- Printf.sprintf "bad irect `%s': %s"
3396 v (Printexc.to_string exn)
3398 src#string "throttle"
3399 (fun () ->
3400 match conf.maxwait with
3401 | None -> "show place holder if page is not ready"
3402 | Some time ->
3403 if time = infinity
3404 then "wait for page to fully render"
3405 else
3406 "wait " ^ string_of_float time
3407 ^ " seconds before showing placeholder"
3409 (fun v ->
3411 let f = float_of_string v in
3412 if f <= 0.0
3413 then conf.maxwait <- None
3414 else conf.maxwait <- Some f
3415 with exn ->
3416 state.text <- Printf.sprintf "bad time `%s': %s"
3417 v (Printexc.to_string exn)
3419 src#colorspace "color space"
3420 (fun () -> colorspace_to_string conf.colorspace)
3421 (fun v ->
3422 conf.colorspace <- colorspace_of_int v;
3423 wcmd "cs" [`i v];
3424 load state.layout;
3428 sep ();
3429 src#caption "Document" 0;
3430 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3431 if conf.trimmargins
3432 then (
3433 sep ();
3434 src#caption "Trimmed margins" 0;
3435 src#caption2 "Dimensions"
3436 (fun () -> string_of_int (List.length state.pdims)) 1;
3439 src#reset prevmode prevuioh;
3441 fun () ->
3442 state.text <- "";
3443 let prevmode = state.mode
3444 and prevuioh = state.uioh in
3445 fillsrc prevmode prevuioh;
3446 let source = (src :> lvsource) in
3447 state.uioh <- coe (object (self)
3448 inherit listview ~source ~trusted:true as super
3449 val mutable m_prevmemused = 0
3450 method infochanged = function
3451 | Memused ->
3452 if m_prevmemused != state.memused
3453 then (
3454 m_prevmemused <- state.memused;
3455 G.postRedisplay "memusedchanged";
3457 | Pdim -> G.postRedisplay "pdimchanged"
3458 | Docinfo -> fillsrc prevmode prevuioh
3460 method special key =
3461 if Glut.getModifiers () land Glut.active_ctrl = 0
3462 then
3463 match key with
3464 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
3465 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
3466 | _ -> super#special key
3467 else super#special key
3468 end);
3469 G.postRedisplay "info";
3472 let enterhelpmode =
3473 let source =
3474 (object
3475 inherit lvsourcebase
3476 method getitemcount = Array.length state.help
3477 method getitem n =
3478 let s, n, _ = state.help.(n) in
3479 (s, n)
3481 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3482 let optuioh =
3483 if not cancel
3484 then (
3485 m_qsearch <- qsearch;
3486 match state.help.(active) with
3487 | _, _, Action f -> Some (f uioh)
3488 | _ -> Some (uioh)
3490 else None
3492 m_active <- active;
3493 m_first <- first;
3494 m_pan <- pan;
3495 optuioh
3497 method hasaction n =
3498 match state.help.(n) with
3499 | _, _, Action _ -> true
3500 | _ -> false
3502 initializer
3503 m_active <- -1
3504 end)
3505 in fun () ->
3506 state.uioh <- coe (new listview ~source ~trusted:true);
3507 G.postRedisplay "help";
3510 let quickbookmark ?title () =
3511 match state.layout with
3512 | [] -> ()
3513 | l :: _ ->
3514 let title =
3515 match title with
3516 | None ->
3517 let sec = Unix.gettimeofday () in
3518 let tm = Unix.localtime sec in
3519 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
3520 (l.pageno+1)
3521 tm.Unix.tm_mday
3522 tm.Unix.tm_mon
3523 (tm.Unix.tm_year + 1900)
3524 tm.Unix.tm_hour
3525 tm.Unix.tm_min
3526 | Some title -> title
3528 state.bookmarks <-
3529 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
3530 :: state.bookmarks
3533 let doreshape w h =
3534 state.fullscreen <- None;
3535 Glut.reshapeWindow w h;
3538 let viewkeyboard key =
3539 let enttext te =
3540 let mode = state.mode in
3541 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3542 state.text <- "";
3543 enttext ();
3544 G.postRedisplay "view:enttext"
3546 let c = Char.chr key in
3547 match c with
3548 | '\027' | 'q' -> (* escape *)
3549 begin match state.mstate with
3550 | Mzoomrect _ ->
3551 state.mstate <- Mnone;
3552 Glut.setCursor Glut.CURSOR_INHERIT;
3553 G.postRedisplay "kill zoom rect";
3554 | _ ->
3555 raise Quit
3556 end;
3558 | '\008' -> (* backspace *)
3559 let y = getnav ~-1 in
3560 gotoy_and_clear_text y
3562 | 'o' ->
3563 enteroutlinemode ()
3565 | 'u' ->
3566 state.rects <- [];
3567 state.text <- "";
3568 G.postRedisplay "dehighlight";
3570 | '/' | '?' ->
3571 let ondone isforw s =
3572 cbput state.hists.pat s;
3573 state.searchpattern <- s;
3574 search s isforw
3576 let s = String.create 1 in
3577 s.[0] <- c;
3578 enttext (s, "", Some (onhist state.hists.pat),
3579 textentry, ondone (c ='/'))
3581 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3582 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3583 setzoom (conf.zoom +. incr)
3585 | '+' ->
3586 let ondone s =
3587 let n =
3588 try int_of_string s with exc ->
3589 state.text <- Printf.sprintf "bad integer `%s': %s"
3590 s (Printexc.to_string exc);
3591 max_int
3593 if n != max_int
3594 then (
3595 conf.pagebias <- n;
3596 state.text <- "page bias is now " ^ string_of_int n;
3599 enttext ("page bias: ", "", None, intentry, ondone)
3601 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3602 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3603 setzoom (max 0.01 (conf.zoom -. decr))
3605 | '-' ->
3606 let ondone msg = state.text <- msg in
3607 enttext (
3608 "option [acfhilpstvAPRSZTI]: ", "", None,
3609 optentry state.mode, ondone
3612 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3613 setzoom 1.0
3615 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3616 let zoom = zoomforh conf.winw conf.winh state.scrollw in
3617 if zoom < 1.0
3618 then setzoom zoom
3620 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3621 togglebirdseye ()
3623 | '0' .. '9' ->
3624 let ondone s =
3625 let n =
3626 try int_of_string s with exc ->
3627 state.text <- Printf.sprintf "bad integer `%s': %s"
3628 s (Printexc.to_string exc);
3631 if n >= 0
3632 then (
3633 addnav ();
3634 cbput state.hists.pag (string_of_int n);
3635 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
3638 let pageentry text key =
3639 match Char.unsafe_chr key with
3640 | 'g' -> TEdone text
3641 | _ -> intentry text key
3643 let text = "x" in text.[0] <- c;
3644 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
3646 | 'b' ->
3647 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
3648 reshape conf.winw conf.winh;
3650 | 'l' ->
3651 conf.hlinks <- not conf.hlinks;
3652 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3653 G.postRedisplay "toggle highlightlinks";
3655 | 'a' ->
3656 begin match state.autoscroll with
3657 | Some step ->
3658 conf.autoscrollstep <- step;
3659 state.autoscroll <- None
3660 | None ->
3661 if conf.autoscrollstep = 0
3662 then state.autoscroll <- Some 1
3663 else state.autoscroll <- Some conf.autoscrollstep
3666 | 'P' ->
3667 conf.presentation <- not conf.presentation;
3668 if conf.presentation
3669 then (
3670 if not conf.scrollbarinpm
3671 then state.scrollw <- 0;
3673 else
3674 state.scrollw <- conf.scrollbw;
3676 showtext ' ' ("presentation mode " ^
3677 if conf.presentation then "on" else "off");
3678 state.anchor <- getanchor ();
3679 represent ()
3681 | 'f' ->
3682 begin match state.fullscreen with
3683 | None ->
3684 state.fullscreen <- Some (conf.winw, conf.winh);
3685 Glut.fullScreen ()
3686 | Some (w, h) ->
3687 state.fullscreen <- None;
3688 doreshape w h
3691 | 'g' ->
3692 gotoy_and_clear_text 0
3694 | 'G' ->
3695 gotopage1 (state.pagecount - 1) 0
3697 | 'n' ->
3698 search state.searchpattern true
3700 | 'p' | 'N' ->
3701 search state.searchpattern false
3703 | 't' ->
3704 begin match state.layout with
3705 | [] -> ()
3706 | l :: _ ->
3707 gotoy_and_clear_text (getpagey l.pageno)
3710 | ' ' ->
3711 begin match List.rev state.layout with
3712 | [] -> ()
3713 | l :: _ ->
3714 let pageno = min (l.pageno+1) (state.pagecount-1) in
3715 gotoy_and_clear_text (getpagey pageno)
3718 | '\127' -> (* del *)
3719 begin match state.layout with
3720 | [] -> ()
3721 | l :: _ ->
3722 let pageno = max 0 (l.pageno-1) in
3723 gotoy_and_clear_text (getpagey pageno)
3726 | '=' ->
3727 showtext ' ' (describe_location ());
3729 | 'w' ->
3730 begin match state.layout with
3731 | [] -> ()
3732 | l :: _ ->
3733 doreshape (l.pagew + state.scrollw) l.pageh;
3734 G.postRedisplay "w"
3737 | '\'' ->
3738 enterbookmarkmode ()
3740 | 'h' ->
3741 enterhelpmode ()
3743 | 'i' ->
3744 enterinfomode ()
3746 | 'm' ->
3747 let ondone s =
3748 match state.layout with
3749 | l :: _ ->
3750 state.bookmarks <-
3751 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
3752 :: state.bookmarks
3753 | _ -> ()
3755 enttext ("bookmark: ", "", None, textentry, ondone)
3757 | '~' ->
3758 quickbookmark ();
3759 showtext ' ' "Quick bookmark added";
3761 | 'z' ->
3762 begin match state.layout with
3763 | l :: _ ->
3764 let rect = getpdimrect l.pagedimno in
3765 let w, h =
3766 if conf.crophack
3767 then
3768 (truncate (1.8 *. (rect.(1) -. rect.(0))),
3769 truncate (1.2 *. (rect.(3) -. rect.(0))))
3770 else
3771 (truncate (rect.(1) -. rect.(0)),
3772 truncate (rect.(3) -. rect.(0)))
3774 let w = truncate ((float w)*.conf.zoom)
3775 and h = truncate ((float h)*.conf.zoom) in
3776 if w != 0 && h != 0
3777 then (
3778 state.anchor <- getanchor ();
3779 doreshape (w + state.scrollw) (h + conf.interpagespace)
3781 G.postRedisplay "z";
3783 | [] -> ()
3786 | '\000' -> (* ctrl-2 *)
3787 let maxw = getmaxw () in
3788 if maxw > 0.0
3789 then setzoom (maxw /. float conf.winw)
3791 | '<' | '>' ->
3792 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
3794 | '[' | ']' ->
3795 conf.colorscale <-
3796 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
3798 G.postRedisplay "brightness";
3800 | 'k' ->
3801 begin match state.mode with
3802 | Birdseye beye -> upbirdseye beye
3803 | _ -> gotoy (clamp (-conf.scrollstep))
3806 | 'j' ->
3807 begin match state.mode with
3808 | Birdseye beye -> downbirdseye beye
3809 | _ -> gotoy (clamp conf.scrollstep)
3812 | 'r' ->
3813 state.anchor <- getanchor ();
3814 opendoc state.path state.password
3816 | 'v' when conf.debug ->
3817 state.rects <- [];
3818 List.iter (fun l ->
3819 match getopaque l.pageno with
3820 | None -> ()
3821 | Some opaque ->
3822 let x0, y0, x1, y1 = pagebbox opaque in
3823 let a,b = float x0, float y0 in
3824 let c,d = float x1, float y0 in
3825 let e,f = float x1, float y1 in
3826 let h,j = float x0, float y1 in
3827 let rect = (a,b,c,d,e,f,h,j) in
3828 debugrect rect;
3829 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
3830 ) state.layout;
3831 G.postRedisplay "v";
3833 | _ ->
3834 vlog "huh? %d %c" key (Char.chr key);
3837 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
3838 match key with
3839 | 27 -> (* escape *)
3840 leavebirdseye beye true
3842 | 12 -> (* ctrl-l *)
3843 let y, h = getpageyh pageno in
3844 let top = (conf.winh - h) / 2 in
3845 gotoy (max 0 (y - top))
3847 | 13 -> (* enter *)
3848 leavebirdseye beye false
3850 | _ ->
3851 viewkeyboard key
3854 let keyboard ~key ~x ~y =
3855 ignore x;
3856 ignore y;
3857 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
3858 then wcmd "interrupt" []
3859 else state.uioh <- state.uioh#key key
3862 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
3863 match key with
3864 | Glut.KEY_UP -> upbirdseye beye
3865 | Glut.KEY_DOWN -> downbirdseye beye
3867 | Glut.KEY_PAGE_UP ->
3868 begin match state.layout with
3869 | l :: _ ->
3870 if l.pagey != 0
3871 then (
3872 state.mode <- Birdseye (
3873 conf, leftx, l.pageno, hooverpageno, anchor
3875 gotopage1 l.pageno 0;
3877 else (
3878 let layout = layout (state.y-conf.winh) conf.winh in
3879 match layout with
3880 | [] -> gotoy (clamp (-conf.winh))
3881 | l :: _ ->
3882 state.mode <- Birdseye (
3883 conf, leftx, l.pageno, hooverpageno, anchor
3885 gotopage1 l.pageno 0
3888 | [] -> gotoy (clamp (-conf.winh))
3889 end;
3891 | Glut.KEY_PAGE_DOWN ->
3892 begin match List.rev state.layout with
3893 | l :: _ ->
3894 let layout = layout (state.y + conf.winh) conf.winh in
3895 begin match layout with
3896 | [] ->
3897 let incr = l.pageh - l.pagevh in
3898 if incr = 0
3899 then (
3900 state.mode <-
3901 Birdseye (
3902 conf, leftx, state.pagecount - 1, hooverpageno, anchor
3904 G.postRedisplay "birdseye pagedown";
3906 else gotoy (clamp (incr + conf.interpagespace*2));
3908 | l :: _ ->
3909 state.mode <-
3910 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
3911 gotopage1 l.pageno 0;
3914 | [] -> gotoy (clamp conf.winh)
3915 end;
3917 | Glut.KEY_HOME ->
3918 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
3919 gotopage1 0 0
3921 | Glut.KEY_END ->
3922 let pageno = state.pagecount - 1 in
3923 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3924 if not (pagevisible state.layout pageno)
3925 then
3926 let h =
3927 match List.rev state.pdims with
3928 | [] -> conf.winh
3929 | (_, _, h, _) :: _ -> h
3931 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
3932 else G.postRedisplay "birdseye end";
3933 | _ -> ()
3936 let setautoscrollspeed step goingdown =
3937 let incr = max 1 ((abs step) / 2) in
3938 let incr = if goingdown then incr else -incr in
3939 let astep = step + incr in
3940 state.autoscroll <- Some astep;
3943 let special ~key ~x ~y =
3944 ignore x;
3945 ignore y;
3946 state.uioh <- state.uioh#special key
3949 let drawpage l =
3950 let color =
3951 match state.mode with
3952 | Textentry _ -> scalecolor 0.4
3953 | View -> scalecolor 1.0
3954 | Birdseye (_, _, pageno, hooverpageno, _) ->
3955 if l.pageno = hooverpageno
3956 then scalecolor 0.9
3957 else (
3958 if l.pageno = pageno
3959 then scalecolor 1.0
3960 else scalecolor 0.8
3963 drawtiles l color;
3964 begin match getopaque l.pageno with
3965 | Some opaque ->
3966 if tileready l l.pagex l.pagey
3967 then
3968 let x = l.pagedispx - l.pagex
3969 and y = l.pagedispy - l.pagey in
3970 postprocess opaque conf.hlinks x y;
3972 | _ -> ()
3973 end;
3976 let scrollph y =
3977 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3978 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3979 let sh = float conf.winh /. sh in
3980 let sh = max sh (float conf.scrollh) in
3982 let percent =
3983 if y = state.maxy
3984 then 1.0
3985 else float y /. float maxy
3987 let position = (float conf.winh -. sh) *. percent in
3989 let position =
3990 if position +. sh > float conf.winh
3991 then float conf.winh -. sh
3992 else position
3994 position, sh;
3997 let scrollpw x =
3998 let winw = conf.winw - state.scrollw - 1 in
3999 let fwinw = float winw in
4000 let sw =
4001 let sw = fwinw /. float state.w in
4002 let sw = fwinw *. sw in
4003 max sw (float conf.scrollh)
4005 let position, sw =
4006 let f = state.w+winw in
4007 let r = float (winw-x) /. float f in
4008 let p = fwinw *. r in
4009 p-.sw/.2., sw
4011 let sw =
4012 if position +. sw > fwinw
4013 then fwinw -. position
4014 else sw
4016 position, sw;
4019 let scrollindicator () =
4020 GlDraw.color (0.64 , 0.64, 0.64);
4021 GlDraw.rect
4022 (float (conf.winw - state.scrollw), 0.)
4023 (float conf.winw, float conf.winh)
4025 GlDraw.rect
4026 (0., float (conf.winh - state.hscrollh))
4027 (float (conf.winw - state.scrollw - 1), float conf.winh)
4029 GlDraw.color (0.0, 0.0, 0.0);
4031 let position, sh = scrollph state.y in
4032 GlDraw.rect
4033 (float (conf.winw - state.scrollw), position)
4034 (float conf.winw, position +. sh)
4036 let position, sw = scrollpw state.x in
4037 GlDraw.rect
4038 (position, float (conf.winh - state.hscrollh))
4039 (position +. sw, float conf.winh)
4043 let pagetranslatepoint l x y =
4044 let dy = y - l.pagedispy in
4045 let y = dy + l.pagey in
4046 let dx = x - l.pagedispx in
4047 let x = dx + l.pagex in
4048 (x, y);
4051 let showsel () =
4052 match state.mstate with
4053 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4056 | Msel ((x0, y0), (x1, y1)) ->
4057 let rec loop = function
4058 | l :: ls ->
4059 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4060 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4061 then
4062 match getopaque l.pageno with
4063 | Some opaque ->
4064 let dx, dy = pagetranslatepoint l 0 0 in
4065 let x0 = x0 + dx
4066 and y0 = y0 + dy
4067 and x1 = x1 + dx
4068 and y1 = y1 + dy in
4069 GlMat.mode `modelview;
4070 GlMat.push ();
4071 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4072 seltext opaque (x0, y0, x1, y1);
4073 GlMat.pop ();
4074 | _ -> ()
4075 else loop ls
4076 | [] -> ()
4078 loop state.layout
4081 let showrects () =
4082 Gl.enable `blend;
4083 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4084 GlDraw.polygon_mode `both `fill;
4085 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4086 List.iter
4087 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4088 List.iter (fun l ->
4089 if l.pageno = pageno
4090 then (
4091 let dx = float (l.pagedispx - l.pagex) in
4092 let dy = float (l.pagedispy - l.pagey) in
4093 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4094 GlDraw.begins `quads;
4096 GlDraw.vertex2 (x0+.dx, y0+.dy);
4097 GlDraw.vertex2 (x1+.dx, y1+.dy);
4098 GlDraw.vertex2 (x2+.dx, y2+.dy);
4099 GlDraw.vertex2 (x3+.dx, y3+.dy);
4101 GlDraw.ends ();
4103 ) state.layout
4104 ) state.rects
4106 Gl.disable `blend;
4109 let display () =
4110 GlClear.color (scalecolor2 conf.bgcolor);
4111 GlClear.clear [`color];
4112 List.iter drawpage state.layout;
4113 showrects ();
4114 showsel ();
4115 scrollindicator ();
4116 state.uioh#display;
4117 begin match state.mstate with
4118 | Mzoomrect ((x0, y0), (x1, y1)) ->
4119 Gl.enable `blend;
4120 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4121 GlDraw.polygon_mode `both `fill;
4122 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4123 GlDraw.rect (float x0, float y0)
4124 (float x1, float y1);
4125 Gl.disable `blend;
4126 | _ -> ()
4127 end;
4128 enttext ();
4129 Glut.swapBuffers ();
4132 let getunder x y =
4133 let rec f = function
4134 | l :: rest ->
4135 begin match getopaque l.pageno with
4136 | Some opaque ->
4137 let x0 = l.pagedispx in
4138 let x1 = x0 + l.pagevw in
4139 let y0 = l.pagedispy in
4140 let y1 = y0 + l.pagevh in
4141 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4142 then
4143 let px, py = pagetranslatepoint l x y in
4144 match whatsunder opaque px py with
4145 | Unone -> f rest
4146 | under -> under
4147 else f rest
4148 | _ ->
4149 f rest
4151 | [] -> Unone
4153 f state.layout
4156 let zoomrect x y x1 y1 =
4157 let x0 = min x x1
4158 and x1 = max x x1
4159 and y0 = min y y1 in
4160 gotoy (state.y + y0);
4161 state.anchor <- getanchor ();
4162 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4163 let margin =
4164 if state.w < conf.winw - state.scrollw
4165 then (conf.winw - state.scrollw - state.w) / 2
4166 else 0
4168 state.x <- (state.x + margin) - x0;
4169 setzoom zoom;
4170 Glut.setCursor Glut.CURSOR_INHERIT;
4171 state.mstate <- Mnone;
4174 let scrollx x =
4175 let winw = conf.winw - state.scrollw - 1 in
4176 let s = float x /. float winw in
4177 let destx = truncate (float (state.w + winw) *. s) in
4178 state.x <- winw - destx;
4179 gotoy_and_clear_text state.y;
4180 state.mstate <- Mscrollx;
4183 let scrolly y =
4184 let s = float y /. float conf.winh in
4185 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4186 gotoy_and_clear_text desty;
4187 state.mstate <- Mscrolly;
4190 let viewmouse button bstate x y =
4191 match button with
4192 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4193 if Glut.getModifiers () land Glut.active_ctrl != 0
4194 then (
4195 match state.mstate with
4196 | Mzoom (oldn, i) ->
4197 if oldn = n
4198 then (
4199 if i = 2
4200 then
4201 let incr =
4202 match n with
4203 | 4 ->
4204 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4205 | _ ->
4206 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4208 let zoom = conf.zoom -. incr in
4209 setzoom zoom;
4210 state.mstate <- Mzoom (n, 0);
4211 else
4212 state.mstate <- Mzoom (n, i+1);
4214 else state.mstate <- Mzoom (n, 0)
4216 | _ -> state.mstate <- Mzoom (n, 0)
4218 else (
4219 match state.autoscroll with
4220 | Some step -> setautoscrollspeed step (n=4)
4221 | None ->
4222 let incr =
4223 if n = 3
4224 then -conf.scrollstep
4225 else conf.scrollstep
4227 let incr = incr * 2 in
4228 let y = clamp incr in
4229 gotoy_and_clear_text y
4232 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4233 if bstate = Glut.DOWN
4234 then (
4235 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4236 state.mstate <- Mpan (x, y)
4238 else
4239 state.mstate <- Mnone
4241 | Glut.RIGHT_BUTTON ->
4242 if bstate = Glut.DOWN
4243 then (
4244 Glut.setCursor Glut.CURSOR_CYCLE;
4245 let p = (x, y) in
4246 state.mstate <- Mzoomrect (p, p)
4248 else (
4249 match state.mstate with
4250 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4251 | _ ->
4252 Glut.setCursor Glut.CURSOR_INHERIT;
4253 state.mstate <- Mnone
4256 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4257 if bstate = Glut.DOWN
4258 then
4259 let position, sh = scrollph state.y in
4260 if y > truncate position && y < truncate (position +. sh)
4261 then state.mstate <- Mscrolly
4262 else scrolly y
4263 else
4264 state.mstate <- Mnone
4266 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4267 if bstate = Glut.DOWN
4268 then
4269 let position, sw = scrollpw state.x in
4270 if x > truncate position && x < truncate (position +. sw)
4271 then state.mstate <- Mscrollx
4272 else scrollx x
4273 else
4274 state.mstate <- Mnone
4276 | Glut.LEFT_BUTTON ->
4277 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4278 begin match dest with
4279 | Ulinkgoto (pageno, top) ->
4280 if pageno >= 0
4281 then (
4282 addnav ();
4283 gotopage1 pageno top;
4286 | Ulinkuri s ->
4287 gotouri s
4289 | Unone when bstate = Glut.DOWN ->
4290 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4291 state.mstate <- Mpan (x, y);
4293 | Unone | Utext _ ->
4294 if bstate = Glut.DOWN
4295 then (
4296 if conf.angle mod 360 = 0
4297 then (
4298 state.mstate <- Msel ((x, y), (x, y));
4299 G.postRedisplay "mouse select";
4302 else (
4303 match state.mstate with
4304 | Mnone -> ()
4306 | Mzoom _ | Mscrollx | Mscrolly ->
4307 state.mstate <- Mnone
4309 | Mzoomrect ((x0, y0), _) ->
4310 zoomrect x0 y0 x y
4312 | Mpan _ ->
4313 Glut.setCursor Glut.CURSOR_INHERIT;
4314 state.mstate <- Mnone
4316 | Msel ((_, y0), (_, y1)) ->
4317 let f l =
4318 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4319 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4320 then
4321 match getopaque l.pageno with
4322 | Some opaque ->
4323 copysel opaque
4324 | _ -> ()
4326 List.iter f state.layout;
4327 copysel ""; (* ugly *)
4328 Glut.setCursor Glut.CURSOR_INHERIT;
4329 state.mstate <- Mnone;
4333 | _ -> ()
4336 let birdseyemouse button bstate x y
4337 (conf, leftx, _, hooverpageno, anchor) =
4338 match button with
4339 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4340 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4341 let rec loop = function
4342 | [] -> ()
4343 | l :: rest ->
4344 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4345 && x > margin && x < margin + l.pagew
4346 then (
4347 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4349 else loop rest
4351 loop state.layout
4352 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4353 | _ -> ()
4356 let mouse bstate button x y =
4357 state.uioh <- state.uioh#button button bstate x y;
4360 let mouse ~button ~state ~x ~y = mouse state button x y;;
4362 let motion ~x ~y =
4363 state.uioh <- state.uioh#motion x y
4366 let pmotion ~x ~y =
4367 state.uioh <- state.uioh#pmotion x y;
4370 let uioh = object
4371 method display = ()
4373 method key key =
4374 begin match state.mode with
4375 | Textentry textentry -> textentrykeyboard key textentry
4376 | Birdseye birdseye -> birdseyekeyboard key birdseye
4377 | View -> viewkeyboard key
4378 end;
4379 state.uioh
4381 method special key =
4382 begin match state.mode with
4383 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4384 togglebirdseye ()
4386 | Birdseye vals ->
4387 birdseyespecial key vals
4389 | View when key = Glut.KEY_F1 ->
4390 enterhelpmode ()
4392 | View ->
4393 begin match state.autoscroll with
4394 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4395 setautoscrollspeed step (key = Glut.KEY_DOWN)
4397 | _ ->
4398 let y =
4399 match key with
4400 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4401 | Glut.KEY_UP ->
4402 if Glut.getModifiers () land Glut.active_ctrl != 0
4403 then
4404 if Glut.getModifiers () land Glut.active_shift != 0
4405 then (setzoom state.prevzoom; state.y)
4406 else clamp (-conf.winh/2)
4407 else clamp (-conf.scrollstep)
4408 | Glut.KEY_DOWN ->
4409 if Glut.getModifiers () land Glut.active_ctrl != 0
4410 then
4411 if Glut.getModifiers () land Glut.active_shift != 0
4412 then (setzoom state.prevzoom; state.y)
4413 else clamp (conf.winh/2)
4414 else clamp (conf.scrollstep)
4415 | Glut.KEY_PAGE_UP ->
4416 if Glut.getModifiers () land Glut.active_ctrl != 0
4417 then
4418 match state.layout with
4419 | [] -> state.y
4420 | l :: _ -> state.y - l.pagey
4421 else
4422 clamp (-conf.winh)
4423 | Glut.KEY_PAGE_DOWN ->
4424 if Glut.getModifiers () land Glut.active_ctrl != 0
4425 then
4426 match List.rev state.layout with
4427 | [] -> state.y
4428 | l :: _ -> getpagey l.pageno
4429 else
4430 clamp conf.winh
4431 | Glut.KEY_HOME ->
4432 addnav ();
4434 | Glut.KEY_END ->
4435 addnav ();
4436 state.maxy - (if conf.maxhfit then conf.winh else 0)
4438 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4439 Glut.getModifiers () land Glut.active_alt != 0 ->
4440 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4442 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4443 let dx =
4444 if Glut.getModifiers () land Glut.active_ctrl != 0
4445 then (conf.winw / 2)
4446 else 10
4448 state.x <- state.x - dx;
4449 state.y
4450 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4451 let dx =
4452 if Glut.getModifiers () land Glut.active_ctrl != 0
4453 then (conf.winw / 2)
4454 else 10
4456 state.x <- state.x + dx;
4457 state.y
4459 | _ -> state.y
4461 gotoy_and_clear_text y
4464 | Textentry te -> textentryspecial key te
4465 end;
4466 state.uioh
4468 method button button bstate x y =
4469 begin match state.mode with
4470 | View -> viewmouse button bstate x y
4471 | Birdseye beye -> birdseyemouse button bstate x y beye
4472 | Textentry _ -> ()
4473 end;
4474 state.uioh
4476 method motion x y =
4477 begin match state.mode with
4478 | Textentry _ -> ()
4479 | View | Birdseye _ ->
4480 match state.mstate with
4481 | Mzoom _ | Mnone -> ()
4483 | Mpan (x0, y0) ->
4484 let dx = x - x0
4485 and dy = y0 - y in
4486 state.mstate <- Mpan (x, y);
4487 if conf.zoom > 1.0 then state.x <- state.x + dx;
4488 let y = clamp dy in
4489 gotoy_and_clear_text y
4491 | Msel (a, _) ->
4492 state.mstate <- Msel (a, (x, y));
4493 G.postRedisplay "motion select";
4495 | Mscrolly ->
4496 let y = min conf.winh (max 0 y) in
4497 scrolly y
4499 | Mscrollx ->
4500 let x = min conf.winw (max 0 x) in
4501 scrollx x
4503 | Mzoomrect (p0, _) ->
4504 state.mstate <- Mzoomrect (p0, (x, y));
4505 G.postRedisplay "motion zoomrect";
4506 end;
4507 state.uioh
4509 method pmotion x y =
4510 begin match state.mode with
4511 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4512 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4513 let rec loop = function
4514 | [] ->
4515 if hooverpageno != -1
4516 then (
4517 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4518 G.postRedisplay "pmotion birdseye no hoover";
4520 | l :: rest ->
4521 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4522 && x > margin && x < margin + l.pagew
4523 then (
4524 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4525 G.postRedisplay "pmotion birdseye hoover";
4527 else loop rest
4529 loop state.layout
4531 | Textentry _ -> ()
4533 | View ->
4534 match state.mstate with
4535 | Mnone ->
4536 begin match getunder x y with
4537 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
4538 | Ulinkuri uri ->
4539 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
4540 Glut.setCursor Glut.CURSOR_INFO
4541 | Ulinkgoto (page, _) ->
4542 if conf.underinfo
4543 then showtext 'p' ("age: " ^ string_of_int (page+1));
4544 Glut.setCursor Glut.CURSOR_INFO
4545 | Utext s ->
4546 if conf.underinfo then showtext 'f' ("ont: " ^ s);
4547 Glut.setCursor Glut.CURSOR_TEXT
4550 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
4552 end;
4553 state.uioh
4555 method infochanged _ = ()
4556 end;;
4558 module Config =
4559 struct
4560 open Parser
4562 let fontpath = ref "";;
4563 let wmclasshack = ref false;;
4565 let unent s =
4566 let l = String.length s in
4567 let b = Buffer.create l in
4568 unent b s 0 l;
4569 Buffer.contents b;
4572 let home =
4574 match platform with
4575 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
4576 | _ -> Sys.getenv "HOME"
4577 with exn ->
4578 prerr_endline
4579 ("Can not determine home directory location: " ^
4580 Printexc.to_string exn);
4584 let config_of c attrs =
4585 let apply c k v =
4587 match k with
4588 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
4589 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
4590 | "case-insensitive-search" -> { c with icase = bool_of_string v }
4591 | "preload" -> { c with preload = bool_of_string v }
4592 | "page-bias" -> { c with pagebias = int_of_string v }
4593 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
4594 | "auto-scroll-step" ->
4595 { c with autoscrollstep = max 0 (int_of_string v) }
4596 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
4597 | "crop-hack" -> { c with crophack = bool_of_string v }
4598 | "throttle" ->
4599 let mw =
4600 match String.lowercase v with
4601 | "true" -> Some infinity
4602 | "false" -> None
4603 | f -> Some (float_of_string f)
4605 { c with maxwait = mw}
4606 | "highlight-links" -> { c with hlinks = bool_of_string v }
4607 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
4608 | "vertical-margin" ->
4609 { c with interpagespace = max 0 (int_of_string v) }
4610 | "zoom" ->
4611 let zoom = float_of_string v /. 100. in
4612 let zoom = max zoom 0.0 in
4613 { c with zoom = zoom }
4614 | "presentation" -> { c with presentation = bool_of_string v }
4615 | "rotation-angle" -> { c with angle = int_of_string v }
4616 | "width" -> { c with winw = max 20 (int_of_string v) }
4617 | "height" -> { c with winh = max 20 (int_of_string v) }
4618 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
4619 | "proportional-display" -> { c with proportional = bool_of_string v }
4620 | "pixmap-cache-size" ->
4621 { c with memlimit = max 2 (int_of_string_with_suffix v) }
4622 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
4623 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
4624 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
4625 | "persistent-location" -> { c with jumpback = bool_of_string v }
4626 | "background-color" -> { c with bgcolor = color_of_string v }
4627 | "scrollbar-in-presentation" ->
4628 { c with scrollbarinpm = bool_of_string v }
4629 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
4630 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
4631 | "memlimit" ->
4632 { c with mumemlimit = max 1024 (int_of_string_with_suffix v) }
4633 | "checkers" -> { c with checkers = bool_of_string v }
4634 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
4635 | "trim-margins" -> { c with trimmargins = bool_of_string v }
4636 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
4637 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
4638 | "uri-launcher" -> { c with urilauncher = unent v }
4639 | "color-space" -> { c with colorspace = colorspace_of_string v }
4640 | "invert-colors" -> { c with invert = bool_of_string v }
4641 | "brightness" -> { c with colorscale = float_of_string v }
4642 | _ -> c
4643 with exn ->
4644 prerr_endline ("Error processing attribute (`" ^
4645 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
4648 let rec fold c = function
4649 | [] -> c
4650 | (k, v) :: rest ->
4651 let c = apply c k v in
4652 fold c rest
4654 fold c attrs;
4657 let fromstring f pos n v d =
4658 try f v
4659 with exn ->
4660 dolog "Error processing attribute (%S=%S) at %d\n%s"
4661 n v pos (Printexc.to_string exn)
4666 let bookmark_of attrs =
4667 let rec fold title page rely = function
4668 | ("title", v) :: rest -> fold v page rely rest
4669 | ("page", v) :: rest -> fold title v rely rest
4670 | ("rely", v) :: rest -> fold title page v rest
4671 | _ :: rest -> fold title page rely rest
4672 | [] -> title, page, rely
4674 fold "invalid" "0" "0" attrs
4677 let doc_of attrs =
4678 let rec fold path page rely pan = function
4679 | ("path", v) :: rest -> fold v page rely pan rest
4680 | ("page", v) :: rest -> fold path v rely pan rest
4681 | ("rely", v) :: rest -> fold path page v pan rest
4682 | ("pan", v) :: rest -> fold path page rely v rest
4683 | _ :: rest -> fold path page rely pan rest
4684 | [] -> path, page, rely, pan
4686 fold "" "0" "0" "0" attrs
4689 let setconf dst src =
4690 dst.scrollbw <- src.scrollbw;
4691 dst.scrollh <- src.scrollh;
4692 dst.icase <- src.icase;
4693 dst.preload <- src.preload;
4694 dst.pagebias <- src.pagebias;
4695 dst.verbose <- src.verbose;
4696 dst.scrollstep <- src.scrollstep;
4697 dst.maxhfit <- src.maxhfit;
4698 dst.crophack <- src.crophack;
4699 dst.autoscrollstep <- src.autoscrollstep;
4700 dst.maxwait <- src.maxwait;
4701 dst.hlinks <- src.hlinks;
4702 dst.underinfo <- src.underinfo;
4703 dst.interpagespace <- src.interpagespace;
4704 dst.zoom <- src.zoom;
4705 dst.presentation <- src.presentation;
4706 dst.angle <- src.angle;
4707 dst.winw <- src.winw;
4708 dst.winh <- src.winh;
4709 dst.savebmarks <- src.savebmarks;
4710 dst.memlimit <- src.memlimit;
4711 dst.proportional <- src.proportional;
4712 dst.texcount <- src.texcount;
4713 dst.sliceheight <- src.sliceheight;
4714 dst.thumbw <- src.thumbw;
4715 dst.jumpback <- src.jumpback;
4716 dst.bgcolor <- src.bgcolor;
4717 dst.scrollbarinpm <- src.scrollbarinpm;
4718 dst.tilew <- src.tilew;
4719 dst.tileh <- src.tileh;
4720 dst.mumemlimit <- src.mumemlimit;
4721 dst.checkers <- src.checkers;
4722 dst.aalevel <- src.aalevel;
4723 dst.trimmargins <- src.trimmargins;
4724 dst.trimfuzz <- src.trimfuzz;
4725 dst.urilauncher <- src.urilauncher;
4726 dst.colorspace <- src.colorspace;
4727 dst.invert <- src.invert;
4728 dst.colorscale <- src.colorscale;
4731 let get s =
4732 let h = Hashtbl.create 10 in
4733 let dc = { defconf with angle = defconf.angle } in
4734 let rec toplevel v t spos _ =
4735 match t with
4736 | Vdata | Vcdata | Vend -> v
4737 | Vopen ("llppconfig", _, closed) ->
4738 if closed
4739 then v
4740 else { v with f = llppconfig }
4741 | Vopen _ ->
4742 error "unexpected subelement at top level" s spos
4743 | Vclose _ -> error "unexpected close at top level" s spos
4745 and llppconfig v t spos _ =
4746 match t with
4747 | Vdata | Vcdata -> v
4748 | Vend -> error "unexpected end of input in llppconfig" s spos
4749 | Vopen ("defaults", attrs, closed) ->
4750 let c = config_of dc attrs in
4751 setconf dc c;
4752 if closed
4753 then v
4754 else { v with f = skip "defaults" (fun () -> v) }
4756 | Vopen ("ui-font", attrs, closed) ->
4757 let rec getsize size = function
4758 | [] -> size
4759 | ("size", v) :: rest ->
4760 let size =
4761 fromstring int_of_string spos "size" v fstate.fontsize in
4762 getsize size rest
4763 | l -> getsize size l
4765 fstate.fontsize <- getsize fstate.fontsize attrs;
4766 if closed
4767 then v
4768 else { v with f = uifont (Buffer.create 10) }
4770 | Vopen ("doc", attrs, closed) ->
4771 let pathent, spage, srely, span = doc_of attrs in
4772 let path = unent pathent
4773 and pageno = fromstring int_of_string spos "page" spage 0
4774 and rely = fromstring float_of_string spos "rely" srely 0.0
4775 and pan = fromstring int_of_string spos "pan" span 0 in
4776 let c = config_of dc attrs in
4777 let anchor = (pageno, rely) in
4778 if closed
4779 then (Hashtbl.add h path (c, [], pan, anchor); v)
4780 else { v with f = doc path pan anchor c [] }
4782 | Vopen _ ->
4783 error "unexpected subelement in llppconfig" s spos
4785 | Vclose "llppconfig" -> { v with f = toplevel }
4786 | Vclose _ -> error "unexpected close in llppconfig" s spos
4788 and uifont b v t spos epos =
4789 match t with
4790 | Vdata | Vcdata ->
4791 Buffer.add_substring b s spos (epos - spos);
4793 | Vopen (_, _, _) ->
4794 error "unexpected subelement in ui-font" s spos
4795 | Vclose "ui-font" ->
4796 if String.length !fontpath = 0
4797 then fontpath := Buffer.contents b;
4798 { v with f = llppconfig }
4799 | Vclose _ -> error "unexpected close in ui-font" s spos
4800 | Vend -> error "unexpected end of input in ui-font" s spos
4802 and doc path pan anchor c bookmarks v t spos _ =
4803 match t with
4804 | Vdata | Vcdata -> v
4805 | Vend -> error "unexpected end of input in doc" s spos
4806 | Vopen ("bookmarks", _, closed) ->
4807 if closed
4808 then v
4809 else { v with f = pbookmarks path pan anchor c bookmarks }
4811 | Vopen (_, _, _) ->
4812 error "unexpected subelement in doc" s spos
4814 | Vclose "doc" ->
4815 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
4816 { v with f = llppconfig }
4818 | Vclose _ -> error "unexpected close in doc" s spos
4820 and pbookmarks path pan anchor c bookmarks v t spos _ =
4821 match t with
4822 | Vdata | Vcdata -> v
4823 | Vend -> error "unexpected end of input in bookmarks" s spos
4824 | Vopen ("item", attrs, closed) ->
4825 let titleent, spage, srely = bookmark_of attrs in
4826 let page = fromstring int_of_string spos "page" spage 0
4827 and rely = fromstring float_of_string spos "rely" srely 0.0 in
4828 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
4829 if closed
4830 then { v with f = pbookmarks path pan anchor c bookmarks }
4831 else
4832 let f () = v in
4833 { v with f = skip "item" f }
4835 | Vopen _ ->
4836 error "unexpected subelement in bookmarks" s spos
4838 | Vclose "bookmarks" ->
4839 { v with f = doc path pan anchor c bookmarks }
4841 | Vclose _ -> error "unexpected close in bookmarks" s spos
4843 and skip tag f v t spos _ =
4844 match t with
4845 | Vdata | Vcdata -> v
4846 | Vend ->
4847 error ("unexpected end of input in skipped " ^ tag) s spos
4848 | Vopen (tag', _, closed) ->
4849 if closed
4850 then v
4851 else
4852 let f' () = { v with f = skip tag f } in
4853 { v with f = skip tag' f' }
4854 | Vclose ctag ->
4855 if tag = ctag
4856 then f ()
4857 else error ("unexpected close in skipped " ^ tag) s spos
4860 parse { f = toplevel; accu = () } s;
4861 h, dc;
4864 let do_load f ic =
4866 let len = in_channel_length ic in
4867 let s = String.create len in
4868 really_input ic s 0 len;
4869 f s;
4870 with
4871 | Parse_error (msg, s, pos) ->
4872 let subs = subs s pos in
4873 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
4874 failwith ("parse error: " ^ s)
4876 | exn ->
4877 failwith ("config load error: " ^ Printexc.to_string exn)
4880 let defconfpath =
4881 let dir =
4883 let dir = Filename.concat home ".config" in
4884 if Sys.is_directory dir then dir else home
4885 with _ -> home
4887 Filename.concat dir "llpp.conf"
4890 let confpath = ref defconfpath;;
4892 let load1 f =
4893 if Sys.file_exists !confpath
4894 then
4895 match
4896 (try Some (open_in_bin !confpath)
4897 with exn ->
4898 prerr_endline
4899 ("Error opening configuation file `" ^ !confpath ^ "': " ^
4900 Printexc.to_string exn);
4901 None
4903 with
4904 | Some ic ->
4905 begin try
4906 f (do_load get ic)
4907 with exn ->
4908 prerr_endline
4909 ("Error loading configuation from `" ^ !confpath ^ "': " ^
4910 Printexc.to_string exn);
4911 end;
4912 close_in ic;
4914 | None -> ()
4915 else
4916 f (Hashtbl.create 0, defconf)
4919 let load () =
4920 let f (h, dc) =
4921 let pc, pb, px, pa =
4923 Hashtbl.find h (Filename.basename state.path)
4924 with Not_found -> dc, [], 0, (0, 0.0)
4926 setconf defconf dc;
4927 setconf conf pc;
4928 state.bookmarks <- pb;
4929 state.x <- px;
4930 state.scrollw <- conf.scrollbw;
4931 if conf.jumpback
4932 then state.anchor <- pa;
4933 cbput state.hists.nav pa;
4935 load1 f
4938 let add_attrs bb always dc c =
4939 let ob s a b =
4940 if always || a != b
4941 then Printf.bprintf bb "\n %s='%b'" s a
4942 and oi s a b =
4943 if always || a != b
4944 then Printf.bprintf bb "\n %s='%d'" s a
4945 and oI s a b =
4946 if always || a != b
4947 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
4948 and oz s a b =
4949 if always || a <> b
4950 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
4951 and oF s a b =
4952 if always || a <> b
4953 then Printf.bprintf bb "\n %s='%f'" s a
4954 and oc s a b =
4955 if always || a <> b
4956 then
4957 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
4958 and oC s a b =
4959 if always || a <> b
4960 then
4961 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
4962 and oR s a b =
4963 if always || a <> b
4964 then
4965 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
4966 and os s a b =
4967 if always || a <> b
4968 then
4969 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
4970 and oW s a b =
4971 if always || a <> b
4972 then
4973 let v =
4974 match a with
4975 | None -> "false"
4976 | Some f ->
4977 if f = infinity
4978 then "true"
4979 else string_of_float f
4981 Printf.bprintf bb "\n %s='%s'" s v
4983 let w, h =
4984 if always
4985 then dc.winw, dc.winh
4986 else
4987 match state.fullscreen with
4988 | Some wh -> wh
4989 | None -> c.winw, c.winh
4991 let zoom, presentation, interpagespace, maxwait =
4992 if always
4993 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
4994 else
4995 match state.mode with
4996 | Birdseye (bc, _, _, _, _) ->
4997 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
4998 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5000 oi "width" w dc.winw;
5001 oi "height" h dc.winh;
5002 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5003 oi "scroll-handle-height" c.scrollh dc.scrollh;
5004 ob "case-insensitive-search" c.icase dc.icase;
5005 ob "preload" c.preload dc.preload;
5006 oi "page-bias" c.pagebias dc.pagebias;
5007 oi "scroll-step" c.scrollstep dc.scrollstep;
5008 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5009 ob "max-height-fit" c.maxhfit dc.maxhfit;
5010 ob "crop-hack" c.crophack dc.crophack;
5011 oW "throttle" maxwait dc.maxwait;
5012 ob "highlight-links" c.hlinks dc.hlinks;
5013 ob "under-cursor-info" c.underinfo dc.underinfo;
5014 oi "vertical-margin" interpagespace dc.interpagespace;
5015 oz "zoom" zoom dc.zoom;
5016 ob "presentation" presentation dc.presentation;
5017 oi "rotation-angle" c.angle dc.angle;
5018 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5019 ob "proportional-display" c.proportional dc.proportional;
5020 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5021 oi "tex-count" c.texcount dc.texcount;
5022 oi "slice-height" c.sliceheight dc.sliceheight;
5023 oi "thumbnail-width" c.thumbw dc.thumbw;
5024 ob "persistent-location" c.jumpback dc.jumpback;
5025 oc "background-color" c.bgcolor dc.bgcolor;
5026 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5027 oi "tile-width" c.tilew dc.tilew;
5028 oi "tile-height" c.tileh dc.tileh;
5029 oI "mupdf-memlimit" c.mumemlimit dc.mumemlimit;
5030 ob "checkers" c.checkers dc.checkers;
5031 oi "aalevel" c.aalevel dc.aalevel;
5032 ob "trim-margins" c.trimmargins dc.trimmargins;
5033 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5034 os "uri-launcher" c.urilauncher dc.urilauncher;
5035 oC "color-space" c.colorspace dc.colorspace;
5036 ob "invert-colors" c.invert dc.invert;
5037 oF "brightness" c.colorscale dc.colorscale;
5038 if always
5039 then ob "wmclass-hack" !wmclasshack false;
5042 let save () =
5043 let uifontsize = fstate.fontsize in
5044 let bb = Buffer.create 32768 in
5045 let f (h, dc) =
5046 let dc = if conf.bedefault then conf else dc in
5047 Buffer.add_string bb "<llppconfig>\n";
5049 if String.length !fontpath > 0
5050 then
5051 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5052 uifontsize
5053 !fontpath
5054 else (
5055 if uifontsize <> 14
5056 then
5057 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5060 Buffer.add_string bb "<defaults ";
5061 add_attrs bb true dc dc;
5062 Buffer.add_string bb "/>\n";
5064 let adddoc path pan anchor c bookmarks =
5065 if bookmarks == [] && c = dc && anchor = emptyanchor
5066 then ()
5067 else (
5068 Printf.bprintf bb "<doc path='%s'"
5069 (enent path 0 (String.length path));
5071 if anchor <> emptyanchor
5072 then (
5073 let n, y = anchor in
5074 Printf.bprintf bb " page='%d'" n;
5075 if y > 1e-6
5076 then
5077 Printf.bprintf bb " rely='%f'" y
5081 if pan != 0
5082 then Printf.bprintf bb " pan='%d'" pan;
5084 add_attrs bb false dc c;
5086 begin match bookmarks with
5087 | [] -> Buffer.add_string bb "/>\n"
5088 | _ ->
5089 Buffer.add_string bb ">\n<bookmarks>\n";
5090 List.iter (fun (title, _level, (page, rely)) ->
5091 Printf.bprintf bb
5092 "<item title='%s' page='%d'"
5093 (enent title 0 (String.length title))
5094 page
5096 if rely > 1e-6
5097 then
5098 Printf.bprintf bb " rely='%f'" rely
5100 Buffer.add_string bb "/>\n";
5101 ) bookmarks;
5102 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5103 end;
5107 let pan =
5108 match state.mode with
5109 | Birdseye (_, pan, _, _, _) -> pan
5110 | _ -> state.x
5112 let basename = Filename.basename state.path in
5113 adddoc basename pan (getanchor ())
5114 { conf with
5115 autoscrollstep =
5116 match state.autoscroll with
5117 | Some step -> step
5118 | None -> conf.autoscrollstep }
5119 (if conf.savebmarks then state.bookmarks else []);
5121 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5122 if basename <> path
5123 then adddoc path x y c bookmarks
5124 ) h;
5125 Buffer.add_string bb "</llppconfig>";
5127 load1 f;
5128 if Buffer.length bb > 0
5129 then
5131 let tmp = !confpath ^ ".tmp" in
5132 let oc = open_out_bin tmp in
5133 Buffer.output_buffer oc bb;
5134 close_out oc;
5135 Unix.rename tmp !confpath;
5136 with exn ->
5137 prerr_endline
5138 ("error while saving configuration: " ^ Printexc.to_string exn)
5140 end;;
5142 let () =
5143 Arg.parse
5144 (Arg.align
5145 [("-p", Arg.String (fun s -> state.password <- s) ,
5146 "<password> Set password");
5148 ("-f", Arg.String (fun s -> Config.fontpath := s),
5149 "<path> Set path to the user interface font");
5151 ("-c", Arg.String (fun s -> Config.confpath := s),
5152 "<path> Set path to the configuration file");
5154 ("-v", Arg.Unit (fun () ->
5155 Printf.printf
5156 "%s\nconfiguration path: %s\n"
5157 Help.version
5158 Config.defconfpath
5160 exit 0), " Print version and exit");
5163 (fun s -> state.path <- s)
5164 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5166 if String.length state.path = 0
5167 then (prerr_endline "file name missing"; exit 1);
5169 Config.load ();
5171 let _ = Glut.init Sys.argv in
5172 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5173 let () = Glut.initWindowSize conf.winw conf.winh in
5174 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5176 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5177 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5178 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5180 let csock, ssock =
5181 if not is_windows
5182 then
5183 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5184 else
5185 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5186 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5187 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5188 Unix.bind sock addr;
5189 Unix.listen sock 1;
5190 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5191 Unix.connect csock addr;
5192 let ssock, _ = Unix.accept sock in
5193 Unix.close sock;
5194 let opts sock =
5195 Unix.setsockopt sock Unix.TCP_NODELAY true;
5196 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5198 opts ssock;
5199 opts csock;
5200 ssock, csock
5203 let () = Glut.displayFunc display in
5204 let () = Glut.reshapeFunc reshape in
5205 let () = Glut.keyboardFunc keyboard in
5206 let () = Glut.specialFunc special in
5207 let () = Glut.idleFunc (Some idle) in
5208 let () = Glut.mouseFunc mouse in
5209 let () = Glut.motionFunc motion in
5210 let () = Glut.passiveMotionFunc pmotion in
5212 setcheckers conf.checkers;
5213 init ssock (
5214 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5215 conf.texcount, conf.sliceheight, conf.mumemlimit, conf.colorspace,
5216 !Config.wmclasshack, !Config.fontpath
5218 state.csock <- csock;
5219 state.ssock <- ssock;
5220 state.text <- "Opening " ^ state.path;
5221 setaalevel conf.aalevel;
5222 writeopen state.path state.password;
5223 state.uioh <- uioh;
5224 setfontsize fstate.fontsize;
5226 while true do
5228 Glut.mainLoop ();
5229 with
5230 | Glut.BadEnum "key in special_of_int" ->
5231 showtext '!' " LablGlut bug: special key not recognized";
5233 | Quit ->
5234 wcmd "quit" [];
5235 Config.save ();
5236 exit 0
5237 done;