Expand
[llpp.git] / main.ml
blob963e23119b6dd9e0c1bb352b4f7ea35c9966a216
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and fontpath = string
36 and memsize = int
37 and aalevel = int
38 and irect = (int * int * int * int)
39 and trimparams = (trimmargins * irect)
40 and colorspace = | Rgb | Bgr | Gray
43 type link =
44 | Lnotfound
45 | Lfound of int
46 and linkdir =
47 | LDfirst
48 | LDlast
49 | LDfirstvisible of (int * int * int)
50 | LDleft of int
51 | LDright of int
52 | LDdown of int
53 | LDup of int
56 type pagewithlinks =
57 | Pwlnotfound
58 | Pwl of int
61 type keymap =
62 | KMinsrt of key
63 | KMinsrl of key list
64 | KMmulti of key list * key list
65 and key = int * int
66 and keyhash = (key, keymap) Hashtbl.t
67 and keystate =
68 | KSnone
69 | KSinto of (key list * key list)
72 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
73 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
75 type pipe = (Unix.file_descr * Unix.file_descr);;
77 external init : pipe -> params -> unit = "ml_init";;
78 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
79 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
80 external getpdimrect : int -> float array = "ml_getpdimrect";;
81 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
82 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
83 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
84 external measurestr : int -> string -> float = "ml_measure_string";;
85 external getmaxw : unit -> float = "ml_getmaxw";;
86 external postprocess :
87 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
88 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
89 external platform : unit -> platform = "ml_platform";;
90 external setaalevel : int -> unit = "ml_setaalevel";;
91 external realloctexts : int -> bool = "ml_realloctexts";;
92 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
93 external findlink : opaque -> linkdir -> link = "ml_findlink";;
94 external getlink : opaque -> int -> under = "ml_getlink";;
95 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
96 external getlinkcount : opaque -> int = "ml_getlinkcount";;
97 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
98 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
100 let platform_to_string = function
101 | Punknown -> "unknown"
102 | Plinux -> "Linux"
103 | Posx -> "OSX"
104 | Psun -> "Sun"
105 | Pfreebsd -> "FreeBSD"
106 | Pdragonflybsd -> "DragonflyBSD"
107 | Popenbsd -> "OpenBSD"
108 | Pnetbsd -> "NetBSD"
109 | Pcygwin -> "Cygwin"
112 let platform = platform ();;
114 let popen cmd fda =
115 if platform = Pcygwin
116 then (
117 let sh = "/bin/sh" in
118 let args = [|sh; "-c"; cmd|] in
119 let rec std si so se = function
120 | [] -> si, so, se
121 | (fd, 0) :: rest -> std fd so se rest
122 | (fd, -1) :: rest ->
123 Unix.set_close_on_exec fd;
124 std si so se rest
125 | (_, n) :: _ ->
126 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
128 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
129 ignore (Unix.create_process sh args si so se)
131 else popen cmd fda;
134 type x = int
135 and y = int
136 and tilex = int
137 and tiley = int
138 and tileparams = (x * y * width * height * tilex * tiley)
141 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
143 type mpos = int * int
144 and mstate =
145 | Msel of (mpos * mpos)
146 | Mpan of mpos
147 | Mscrolly | Mscrollx
148 | Mzoom of (int * int)
149 | Mzoomrect of (mpos * mpos)
150 | Mnone
153 type textentry = string * string * onhist option * onkey * ondone
154 and onkey = string -> int -> te
155 and ondone = string -> unit
156 and histcancel = unit -> unit
157 and onhist = ((histcmd -> string) * histcancel)
158 and histcmd = HCnext | HCprev | HCfirst | HClast
159 and te =
160 | TEstop
161 | TEdone of string
162 | TEcont of string
163 | TEswitch of textentry
166 type 'a circbuf =
167 { store : 'a array
168 ; mutable rc : int
169 ; mutable wc : int
170 ; mutable len : int
174 let bound v minv maxv =
175 max minv (min maxv v);
178 let cbnew n v =
179 { store = Array.create n v
180 ; rc = 0
181 ; wc = 0
182 ; len = 0
186 let drawstring size x y s =
187 Gl.enable `blend;
188 Gl.enable `texture_2d;
189 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
190 ignore (drawstr size x y s);
191 Gl.disable `blend;
192 Gl.disable `texture_2d;
195 let drawstring1 size x y s =
196 drawstr size x y s;
199 let drawstring2 size x y fmt =
200 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
203 let cbcap b = Array.length b.store;;
205 let cbput b v =
206 let cap = cbcap b in
207 b.store.(b.wc) <- v;
208 b.wc <- (b.wc + 1) mod cap;
209 b.rc <- b.wc;
210 b.len <- min (b.len + 1) cap;
213 let cbempty b = b.len = 0;;
215 let cbgetg b circular dir =
216 if cbempty b
217 then b.store.(0)
218 else
219 let rc = b.rc + dir in
220 let rc =
221 if circular
222 then (
223 if rc = -1
224 then b.len-1
225 else (
226 if rc = b.len
227 then 0
228 else rc
231 else max 0 (min rc (b.len-1))
233 b.rc <- rc;
234 b.store.(rc);
237 let cbget b = cbgetg b false;;
238 let cbgetc b = cbgetg b true;;
240 type page =
241 { pageno : int
242 ; pagedimno : int
243 ; pagew : int
244 ; pageh : int
245 ; pagex : int
246 ; pagey : int
247 ; pagevw : int
248 ; pagevh : int
249 ; pagedispx : int
250 ; pagedispy : int
251 ; pagecol : int
255 let debugl l =
256 dolog "l %d dim=%d {" l.pageno l.pagedimno;
257 dolog " WxH %dx%d" l.pagew l.pageh;
258 dolog " vWxH %dx%d" l.pagevw l.pagevh;
259 dolog " pagex,y %d,%d" l.pagex l.pagey;
260 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
261 dolog " column %d" l.pagecol;
262 dolog "}";
265 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
266 dolog "rect {";
267 dolog " x0,y0=(% f, % f)" x0 y0;
268 dolog " x1,y1=(% f, % f)" x1 y1;
269 dolog " x2,y2=(% f, % f)" x2 y2;
270 dolog " x3,y3=(% f, % f)" x3 y3;
271 dolog "}";
274 type multicolumns = multicol * pagegeom
275 and splitcolumns = columncount * pagegeom
276 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
277 and multicol = columncount * covercount * covercount
278 and pdimno = int
279 and columncount = int
280 and covercount = int;;
282 type conf =
283 { mutable scrollbw : int
284 ; mutable scrollh : int
285 ; mutable icase : bool
286 ; mutable preload : bool
287 ; mutable pagebias : int
288 ; mutable verbose : bool
289 ; mutable debug : bool
290 ; mutable scrollstep : int
291 ; mutable maxhfit : bool
292 ; mutable crophack : bool
293 ; mutable autoscrollstep : int
294 ; mutable maxwait : float option
295 ; mutable hlinks : bool
296 ; mutable underinfo : bool
297 ; mutable interpagespace : interpagespace
298 ; mutable zoom : float
299 ; mutable presentation : bool
300 ; mutable angle : angle
301 ; mutable winw : int
302 ; mutable winh : int
303 ; mutable savebmarks : bool
304 ; mutable proportional : proportional
305 ; mutable trimmargins : trimmargins
306 ; mutable trimfuzz : irect
307 ; mutable memlimit : memsize
308 ; mutable texcount : texcount
309 ; mutable sliceheight : sliceheight
310 ; mutable thumbw : width
311 ; mutable jumpback : bool
312 ; mutable bgcolor : float * float * float
313 ; mutable bedefault : bool
314 ; mutable scrollbarinpm : bool
315 ; mutable tilew : int
316 ; mutable tileh : int
317 ; mutable mustoresize : memsize
318 ; mutable checkers : bool
319 ; mutable aalevel : int
320 ; mutable urilauncher : string
321 ; mutable pathlauncher : string
322 ; mutable colorspace : colorspace
323 ; mutable invert : bool
324 ; mutable colorscale : float
325 ; mutable redirectstderr : bool
326 ; mutable ghyllscroll : (int * int * int) option
327 ; mutable columns : columns
328 ; mutable beyecolumns : columncount option
329 ; mutable selcmd : string
330 ; mutable updatecurs : bool
331 ; mutable keyhashes : (string * keyhash) list
332 ; mutable hfsize : int
334 and columns =
335 | Csingle
336 | Cmulti of multicolumns
337 | Csplit of splitcolumns
340 type anchor = pageno * top;;
342 type outline = string * int * anchor;;
344 type rect = float * float * float * float * float * float * float * float;;
346 type tile = opaque * pixmapsize * elapsed
347 and elapsed = float;;
348 type pagemapkey = pageno * gen;;
349 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
350 and row = int
351 and col = int;;
353 let emptyanchor = (0, 0.0);;
355 type infochange = | Memused | Docinfo | Pdim;;
357 class type uioh = object
358 method display : unit
359 method key : int -> int -> uioh
360 method button : int -> bool -> int -> int -> int -> uioh
361 method motion : int -> int -> uioh
362 method pmotion : int -> int -> uioh
363 method infochanged : infochange -> unit
364 method scrollpw : (int * float * float)
365 method scrollph : (int * float * float)
366 method modehash : keyhash
367 end;;
369 type mode =
370 | Birdseye of (conf * leftx * pageno * pageno * anchor)
371 | Textentry of (textentry * onleave)
372 | View
373 | LinkNav of linktarget
374 and onleave = leavetextentrystatus -> unit
375 and leavetextentrystatus = | Cancel | Confirm
376 and helpitem = string * int * action
377 and action =
378 | Noaction
379 | Action of (uioh -> uioh)
380 and linktarget =
381 | Ltexact of (pageno * int)
382 | Ltgendir of int
385 let isbirdseye = function Birdseye _ -> true | _ -> false;;
386 let istextentry = function Textentry _ -> true | _ -> false;;
388 type currently =
389 | Idle
390 | Loading of (page * gen)
391 | Tiling of (
392 page * opaque * colorspace * angle * gen * col * row * width * height
394 | Outlining of outline list
397 let emptykeyhash = Hashtbl.create 0;;
398 let nouioh : uioh = object (self)
399 method display = ()
400 method key _ _ = self
401 method button _ _ _ _ _ = self
402 method motion _ _ = self
403 method pmotion _ _ = self
404 method infochanged _ = ()
405 method scrollpw = (0, nan, nan)
406 method scrollph = (0, nan, nan)
407 method modehash = emptykeyhash
408 end;;
410 type state =
411 { mutable sr : Unix.file_descr
412 ; mutable sw : Unix.file_descr
413 ; mutable wsfd : Unix.file_descr
414 ; mutable errfd : Unix.file_descr option
415 ; mutable stderr : Unix.file_descr
416 ; mutable errmsgs : Buffer.t
417 ; mutable newerrmsgs : bool
418 ; mutable w : int
419 ; mutable x : int
420 ; mutable y : int
421 ; mutable scrollw : int
422 ; mutable hscrollh : int
423 ; mutable anchor : anchor
424 ; mutable ranchors : (string * string * anchor) list
425 ; mutable maxy : int
426 ; mutable layout : page list
427 ; pagemap : (pagemapkey, opaque) Hashtbl.t
428 ; tilemap : (tilemapkey, tile) Hashtbl.t
429 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
430 ; mutable pdims : (pageno * width * height * leftx) list
431 ; mutable pagecount : int
432 ; mutable currently : currently
433 ; mutable mstate : mstate
434 ; mutable searchpattern : string
435 ; mutable rects : (pageno * recttype * rect) list
436 ; mutable rects1 : (pageno * recttype * rect) list
437 ; mutable text : string
438 ; mutable fullscreen : (width * height) option
439 ; mutable mode : mode
440 ; mutable uioh : uioh
441 ; mutable outlines : outline array
442 ; mutable bookmarks : outline list
443 ; mutable path : string
444 ; mutable password : string
445 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
446 ; mutable memused : memsize
447 ; mutable gen : gen
448 ; mutable throttle : (page list * int * float) option
449 ; mutable autoscroll : int option
450 ; mutable ghyll : (int option -> unit)
451 ; mutable help : helpitem array
452 ; mutable docinfo : (int * string) list
453 ; mutable texid : GlTex.texture_id option
454 ; hists : hists
455 ; mutable prevzoom : float
456 ; mutable progress : float
457 ; mutable redisplay : bool
458 ; mutable mpos : mpos
459 ; mutable keystate : keystate
460 ; mutable glinks : bool
462 and hists =
463 { pat : string circbuf
464 ; pag : string circbuf
465 ; nav : anchor circbuf
466 ; sel : string circbuf
470 let defconf =
471 { scrollbw = 7
472 ; scrollh = 12
473 ; icase = true
474 ; preload = true
475 ; pagebias = 0
476 ; verbose = false
477 ; debug = false
478 ; scrollstep = 24
479 ; maxhfit = true
480 ; crophack = false
481 ; autoscrollstep = 2
482 ; maxwait = None
483 ; hlinks = false
484 ; underinfo = false
485 ; interpagespace = 2
486 ; zoom = 1.0
487 ; presentation = false
488 ; angle = 0
489 ; winw = 900
490 ; winh = 900
491 ; savebmarks = true
492 ; proportional = true
493 ; trimmargins = false
494 ; trimfuzz = (0,0,0,0)
495 ; memlimit = 32 lsl 20
496 ; texcount = 256
497 ; sliceheight = 24
498 ; thumbw = 76
499 ; jumpback = true
500 ; bgcolor = (0.5, 0.5, 0.5)
501 ; bedefault = false
502 ; scrollbarinpm = true
503 ; tilew = 2048
504 ; tileh = 2048
505 ; mustoresize = 256 lsl 20
506 ; checkers = true
507 ; aalevel = 8
508 ; urilauncher =
509 (match platform with
510 | Plinux | Pfreebsd | Pdragonflybsd
511 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
512 | Posx -> "open \"%s\""
513 | Pcygwin -> "cygstart \"%s\""
514 | Punknown -> "echo %s")
515 ; pathlauncher = "lp \"%s\""
516 ; selcmd =
517 (match platform with
518 | Plinux | Pfreebsd | Pdragonflybsd
519 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
520 | Posx -> "pbcopy"
521 | Pcygwin -> "wsel"
522 | Punknown -> "cat")
523 ; colorspace = Rgb
524 ; invert = false
525 ; colorscale = 1.0
526 ; redirectstderr = false
527 ; ghyllscroll = None
528 ; columns = Csingle
529 ; beyecolumns = None
530 ; updatecurs = false
531 ; hfsize = 12
532 ; keyhashes =
533 let mk n = (n, Hashtbl.create 1) in
534 [ mk "global"
535 ; mk "info"
536 ; mk "help"
537 ; mk "outline"
538 ; mk "listview"
539 ; mk "birdseye"
540 ; mk "textentry"
541 ; mk "links"
542 ; mk "view"
547 let findkeyhash c name =
548 try List.assoc name c.keyhashes
549 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
552 let conf = { defconf with angle = defconf.angle };;
554 type fontstate =
555 { mutable fontsize : int
556 ; mutable wwidth : float
557 ; mutable maxrows : int
561 let fstate =
562 { fontsize = 14
563 ; wwidth = nan
564 ; maxrows = -1
568 let setfontsize n =
569 fstate.fontsize <- n;
570 fstate.wwidth <- measurestr fstate.fontsize "w";
571 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
574 let geturl s =
575 let colonpos = try String.index s ':' with Not_found -> -1 in
576 let len = String.length s in
577 if colonpos >= 0 && colonpos + 3 < len
578 then (
579 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
580 then
581 let schemestartpos =
582 try String.rindex_from s colonpos ' '
583 with Not_found -> -1
585 let scheme =
586 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
588 match scheme with
589 | "http" | "ftp" | "mailto" ->
590 let epos =
591 try String.index_from s colonpos ' '
592 with Not_found -> len
594 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
595 | _ -> ""
596 else ""
598 else ""
601 let gotouri uri =
602 if String.length conf.urilauncher = 0
603 then print_endline uri
604 else (
605 let url = geturl uri in
606 if String.length url = 0
607 then print_endline uri
608 else
609 let re = Str.regexp "%s" in
610 let command = Str.global_replace re url conf.urilauncher in
611 try popen command []
612 with exn ->
613 Printf.eprintf
614 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
615 flush stderr;
619 let version () =
620 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
621 (platform_to_string platform) Sys.word_size Sys.ocaml_version
624 let makehelp () =
625 let strings = version () :: "" :: Help.keys in
626 Array.of_list (
627 List.map (fun s ->
628 let url = geturl s in
629 if String.length url > 0
630 then (s, 0, Action (fun u -> gotouri url; u))
631 else (s, 0, Noaction)
632 ) strings);
635 let noghyll _ = ();;
636 let firstgeomcmds = "", [];;
638 let state =
639 { sr = Unix.stdin
640 ; sw = Unix.stdin
641 ; wsfd = Unix.stdin
642 ; errfd = None
643 ; stderr = Unix.stderr
644 ; errmsgs = Buffer.create 0
645 ; newerrmsgs = false
646 ; x = 0
647 ; y = 0
648 ; w = 0
649 ; scrollw = 0
650 ; hscrollh = 0
651 ; anchor = emptyanchor
652 ; ranchors = []
653 ; layout = []
654 ; maxy = max_int
655 ; tilelru = Queue.create ()
656 ; pagemap = Hashtbl.create 10
657 ; tilemap = Hashtbl.create 10
658 ; pdims = []
659 ; pagecount = 0
660 ; currently = Idle
661 ; mstate = Mnone
662 ; rects = []
663 ; rects1 = []
664 ; text = ""
665 ; mode = View
666 ; fullscreen = None
667 ; searchpattern = ""
668 ; outlines = [||]
669 ; bookmarks = []
670 ; path = ""
671 ; password = ""
672 ; geomcmds = firstgeomcmds
673 ; hists =
674 { nav = cbnew 10 (0, 0.0)
675 ; pat = cbnew 10 ""
676 ; pag = cbnew 10 ""
677 ; sel = cbnew 10 ""
679 ; memused = 0
680 ; gen = 0
681 ; throttle = None
682 ; autoscroll = None
683 ; ghyll = noghyll
684 ; help = makehelp ()
685 ; docinfo = []
686 ; texid = None
687 ; prevzoom = 1.0
688 ; progress = -1.0
689 ; uioh = nouioh
690 ; redisplay = true
691 ; mpos = (-1, -1)
692 ; keystate = KSnone
693 ; glinks = false
697 let vlog fmt =
698 if conf.verbose
699 then
700 Printf.kprintf prerr_endline fmt
701 else
702 Printf.kprintf ignore fmt
705 let launchpath () =
706 if String.length conf.pathlauncher = 0
707 then print_endline state.path
708 else (
709 let re = Str.regexp "%s" in
710 let command = Str.global_replace re state.path conf.pathlauncher in
711 try popen command []
712 with exn ->
713 Printf.eprintf
714 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
715 flush stderr;
719 module Ne = struct
720 type 'a t = | Res of 'a | Exn of exn;;
722 let pipe () =
723 try Res (Unix.pipe ())
724 with exn -> Exn exn
727 let clo fd f =
728 try Unix.close fd
729 with exn -> f (Printexc.to_string exn)
732 let dup fd =
733 try Res (Unix.dup fd)
734 with exn -> Exn exn
737 let dup2 fd1 fd2 =
738 try Res (Unix.dup2 fd1 fd2)
739 with exn -> Exn exn
741 end;;
743 let redirectstderr () =
744 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
745 if conf.redirectstderr
746 then
747 match Ne.pipe () with
748 | Ne.Exn exn ->
749 dolog "failed to create stderr redirection pipes: %s"
750 (Printexc.to_string exn)
752 | Ne.Res (r, w) ->
753 begin match Ne.dup Unix.stderr with
754 | Ne.Exn exn ->
755 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
756 Ne.clo r (clofail "pipe/r");
757 Ne.clo w (clofail "pipe/w");
759 | Ne.Res dupstderr ->
760 begin match Ne.dup2 w Unix.stderr with
761 | Ne.Exn exn ->
762 dolog "failed to dup2 to stderr: %s"
763 (Printexc.to_string exn);
764 Ne.clo dupstderr (clofail "stderr duplicate");
765 Ne.clo r (clofail "redir pipe/r");
766 Ne.clo w (clofail "redir pipe/w");
768 | Ne.Res () ->
769 state.stderr <- dupstderr;
770 state.errfd <- Some r;
771 end;
773 else (
774 state.newerrmsgs <- false;
775 begin match state.errfd with
776 | Some fd ->
777 begin match Ne.dup2 state.stderr Unix.stderr with
778 | Ne.Exn exn ->
779 dolog "failed to dup2 original stderr: %s"
780 (Printexc.to_string exn)
781 | Ne.Res () ->
782 Ne.clo fd (clofail "dup of stderr");
783 Unix.dup2 state.stderr Unix.stderr;
784 state.errfd <- None;
785 end;
786 | None -> ()
787 end;
788 prerr_string (Buffer.contents state.errmsgs);
789 flush stderr;
790 Buffer.clear state.errmsgs;
794 module G =
795 struct
796 let postRedisplay who =
797 if conf.verbose
798 then prerr_endline ("redisplay for " ^ who);
799 state.redisplay <- true;
801 end;;
803 let getopaque pageno =
804 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
805 with Not_found -> None
808 let putopaque pageno opaque =
809 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
812 let pagetranslatepoint l x y =
813 let dy = y - l.pagedispy in
814 let y = dy + l.pagey in
815 let dx = x - l.pagedispx in
816 let x = dx + l.pagex in
817 (x, y);
820 let getunder x y =
821 let rec f = function
822 | l :: rest ->
823 begin match getopaque l.pageno with
824 | Some opaque ->
825 let x0 = l.pagedispx in
826 let x1 = x0 + l.pagevw in
827 let y0 = l.pagedispy in
828 let y1 = y0 + l.pagevh in
829 if y >= y0 && y <= y1 && x >= x0 && x <= x1
830 then
831 let px, py = pagetranslatepoint l x y in
832 match whatsunder opaque px py with
833 | Unone -> f rest
834 | under -> under
835 else f rest
836 | _ ->
837 f rest
839 | [] -> Unone
841 f state.layout
844 let showtext c s =
845 state.text <- Printf.sprintf "%c%s" c s;
846 G.postRedisplay "showtext";
849 let undertext = function
850 | Unone -> "none"
851 | Ulinkuri s -> s
852 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
853 | Utext s -> "font: " ^ s
854 | Uunexpected s -> "unexpected: " ^ s
855 | Ulaunch s -> "launch: " ^ s
856 | Unamed s -> "named: " ^ s
857 | Uremote (filename, pageno) ->
858 Printf.sprintf "%s: page %d" filename (pageno+1)
861 let updateunder x y =
862 match getunder x y with
863 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
864 | Ulinkuri uri ->
865 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
866 Wsi.setcursor Wsi.CURSOR_INFO
867 | Ulinkgoto (pageno, _) ->
868 if conf.underinfo
869 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
870 Wsi.setcursor Wsi.CURSOR_INFO
871 | Utext s ->
872 if conf.underinfo then showtext 'f' ("ont: " ^ s);
873 Wsi.setcursor Wsi.CURSOR_TEXT
874 | Uunexpected s ->
875 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
876 Wsi.setcursor Wsi.CURSOR_INHERIT
877 | Ulaunch s ->
878 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
879 Wsi.setcursor Wsi.CURSOR_INHERIT
880 | Unamed s ->
881 if conf.underinfo then showtext 'n' ("amed: " ^ s);
882 Wsi.setcursor Wsi.CURSOR_INHERIT
883 | Uremote (filename, pageno) ->
884 if conf.underinfo then showtext 'r'
885 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
886 Wsi.setcursor Wsi.CURSOR_INFO
889 let showlinktype under =
890 if conf.underinfo
891 then
892 match under with
893 | Unone -> ()
894 | under ->
895 let s = undertext under in
896 showtext ' ' s
899 let addchar s c =
900 let b = Buffer.create (String.length s + 1) in
901 Buffer.add_string b s;
902 Buffer.add_char b c;
903 Buffer.contents b;
906 let colorspace_of_string s =
907 match String.lowercase s with
908 | "rgb" -> Rgb
909 | "bgr" -> Bgr
910 | "gray" -> Gray
911 | _ -> failwith "invalid colorspace"
914 let int_of_colorspace = function
915 | Rgb -> 0
916 | Bgr -> 1
917 | Gray -> 2
920 let colorspace_of_int = function
921 | 0 -> Rgb
922 | 1 -> Bgr
923 | 2 -> Gray
924 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
927 let colorspace_to_string = function
928 | Rgb -> "rgb"
929 | Bgr -> "bgr"
930 | Gray -> "gray"
933 let intentry_with_suffix text key =
934 let c =
935 if key >= 32 && key < 127
936 then Char.chr key
937 else '\000'
939 match Char.lowercase c with
940 | '0' .. '9' ->
941 let text = addchar text c in
942 TEcont text
944 | 'k' | 'm' | 'g' ->
945 let text = addchar text c in
946 TEcont text
948 | _ ->
949 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
950 TEcont text
953 let multicolumns_to_string (n, a, b) =
954 if a = 0 && b = 0
955 then Printf.sprintf "%d" n
956 else Printf.sprintf "%d,%d,%d" n a b;
959 let multicolumns_of_string s =
961 (int_of_string s, 0, 0)
962 with _ ->
963 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
966 let readcmd fd =
967 let s = "xxxx" in
968 let n = Unix.read fd s 0 4 in
969 if n != 4 then failwith "incomplete read(len)";
970 let len = 0
971 lor (Char.code s.[0] lsl 24)
972 lor (Char.code s.[1] lsl 16)
973 lor (Char.code s.[2] lsl 8)
974 lor (Char.code s.[3] lsl 0)
976 let s = String.create len in
977 let n = Unix.read fd s 0 len in
978 if n != len then failwith "incomplete read(data)";
982 let btod b = if b then 1 else 0;;
984 let wcmd fmt =
985 let b = Buffer.create 16 in
986 Buffer.add_string b "llll";
987 Printf.kbprintf
988 (fun b ->
989 let s = Buffer.contents b in
990 let n = String.length s in
991 let len = n - 4 in
992 (* dolog "wcmd %S" (String.sub s 4 len); *)
993 s.[0] <- Char.chr ((len lsr 24) land 0xff);
994 s.[1] <- Char.chr ((len lsr 16) land 0xff);
995 s.[2] <- Char.chr ((len lsr 8) land 0xff);
996 s.[3] <- Char.chr (len land 0xff);
997 let n' = Unix.write state.sw s 0 n in
998 if n' != n then failwith "write failed";
999 ) b fmt;
1002 let calcips h =
1003 if conf.presentation
1004 then
1005 let d = conf.winh - h in
1006 max 0 ((d + 1) / 2)
1007 else
1008 conf.interpagespace
1011 let calcheight () =
1012 let rec f pn ph pi fh l =
1013 match l with
1014 | (n, _, h, _) :: rest ->
1015 let ips = calcips h in
1016 let fh =
1017 if conf.presentation
1018 then fh+ips
1019 else (
1020 if isbirdseye state.mode && pn = 0
1021 then fh + ips
1022 else fh
1025 let fh = fh + ((n - pn) * (ph + pi)) in
1026 f n h ips fh rest;
1028 | [] ->
1029 let inc =
1030 if conf.presentation || (isbirdseye state.mode && pn = 0)
1031 then 0
1032 else -pi
1034 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
1035 max 0 fh
1037 let fh = f 0 0 0 0 state.pdims in
1041 let calcheight () =
1042 match conf.columns with
1043 | Csingle -> calcheight ()
1044 | Cmulti ((c, _, _), b) ->
1045 let rec loop y h n =
1046 if n < 0
1047 then loop y h (n+1)
1048 else (
1049 if n = Array.length b
1050 then y + h
1051 else
1052 let (_, _, y', (_, _, h', _)) = b.(n) in
1053 let y = min y y'
1054 and h = max h h' in
1055 loop y h (n+1)
1058 loop max_int 0 (((Array.length b - 1) / c) * c)
1059 | Csplit (_, b) ->
1060 if Array.length b > 0
1061 then
1062 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1063 y + h
1064 else 0
1067 let getpageyh pageno =
1068 let rec f pn ph pi y l =
1069 match l with
1070 | (n, _, h, _) :: rest ->
1071 let ips = calcips h in
1072 if n >= pageno
1073 then
1074 let h = if n = pageno then h else ph in
1075 if conf.presentation && n = pageno
1076 then
1077 y + (pageno - pn) * (ph + pi) + pi, h
1078 else
1079 y + (pageno - pn) * (ph + pi), h
1080 else
1081 let y = y + (if conf.presentation then pi else 0) in
1082 let y = y + (n - pn) * (ph + pi) in
1083 f n h ips y rest
1085 | [] ->
1086 y + (pageno - pn) * (ph + pi), ph
1088 f 0 0 0 0 state.pdims
1091 let getpageyh pageno =
1092 match conf.columns with
1093 | Csingle -> getpageyh pageno
1094 | Cmulti (_, b) ->
1095 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1096 y, h
1097 | Csplit (c, b) ->
1098 let n = pageno*c in
1099 let (_, _, y, (_, _, h, _)) = b.(n) in
1100 y, h
1103 let getpagedim pageno =
1104 let rec f ppdim l =
1105 match l with
1106 | (n, _, _, _) as pdim :: rest ->
1107 if n >= pageno
1108 then (if n = pageno then pdim else ppdim)
1109 else f pdim rest
1111 | [] -> ppdim
1113 f (-1, -1, -1, -1) state.pdims
1116 let getpagey pageno = fst (getpageyh pageno);;
1118 let nogeomcmds cmds =
1119 match cmds with
1120 | s, [] -> String.length s = 0
1121 | _ -> false
1124 let layout1 y sh =
1125 let sh = sh - state.hscrollh in
1126 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1127 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1128 match pdims with
1129 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1130 let ips = calcips h in
1131 let yinc =
1132 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1133 then ips
1134 else 0
1136 (w, h, ips, xoff), rest, pdimno + 1, yinc
1137 | _ ->
1138 prev, pdims, pdimno, 0
1140 let dy = dy + yinc in
1141 let py = py + yinc in
1142 if pageno = state.pagecount || dy >= sh
1143 then
1144 accu
1145 else
1146 let vy = y + dy in
1147 if py + h <= vy - yinc
1148 then
1149 let py = py + h + ips in
1150 let dy = max 0 (py - y) in
1151 f ~pageno:(pageno+1)
1152 ~pdimno
1153 ~prev:curr
1156 ~pdims:rest
1157 ~accu
1158 else
1159 let pagey = vy - py in
1160 let pagevh = h - pagey in
1161 let pagevh = min (sh - dy) pagevh in
1162 let off = if yinc > 0 then py - vy else 0 in
1163 let py = py + h + ips in
1164 let pagex, dx =
1165 let xoff = xoff +
1166 if state.w < conf.winw - state.scrollw
1167 then (conf.winw - state.scrollw - state.w) / 2
1168 else 0
1170 let dispx = xoff + state.x in
1171 if dispx < 0
1172 then (-dispx, 0)
1173 else (0, dispx)
1175 let pagevw =
1176 let lw = w - pagex in
1177 min lw (conf.winw - state.scrollw)
1179 let e =
1180 { pageno = pageno
1181 ; pagedimno = pdimno
1182 ; pagew = w
1183 ; pageh = h
1184 ; pagex = pagex
1185 ; pagey = pagey + off
1186 ; pagevw = pagevw
1187 ; pagevh = pagevh - off
1188 ; pagedispx = dx
1189 ; pagedispy = dy + off
1190 ; pagecol = 0
1193 let accu = e :: accu in
1194 f ~pageno:(pageno+1)
1195 ~pdimno
1196 ~prev:curr
1198 ~dy:(dy+pagevh+ips)
1199 ~pdims:rest
1200 ~accu
1202 let accu =
1204 ~pageno:0
1205 ~pdimno:~-1
1206 ~prev:(0,0,0,0)
1207 ~py:0
1208 ~dy:0
1209 ~pdims:state.pdims
1210 ~accu:[]
1212 List.rev accu
1215 let layoutN ((columns, coverA, coverB), b) y sh =
1216 let sh = sh - state.hscrollh in
1217 let rec fold accu n =
1218 if n = Array.length b
1219 then accu
1220 else
1221 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1222 if (vy - y) > sh &&
1223 (n = coverA - 1
1224 || n = state.pagecount - coverB
1225 || (n - coverA) mod columns = columns - 1)
1226 then accu
1227 else
1228 let accu =
1229 if vy + h > y
1230 then
1231 let pagey = max 0 (y - vy) in
1232 let pagedispy = if pagey > 0 then 0 else vy - y in
1233 let pagedispx, pagex =
1234 let pdx =
1235 if n = coverA - 1 || n = state.pagecount - coverB
1236 then state.x + (conf.winw - state.scrollw - w) / 2
1237 else dx + xoff + state.x
1239 if pdx < 0
1240 then 0, -pdx
1241 else pdx, 0
1243 let pagevw =
1244 let vw = conf.winw - state.scrollw - pagedispx in
1245 let pw = w - pagex in
1246 min vw pw
1248 let pagevh = min (h - pagey) (sh - pagedispy) in
1249 if pagevw > 0 && pagevh > 0
1250 then
1251 let e =
1252 { pageno = n
1253 ; pagedimno = pdimno
1254 ; pagew = w
1255 ; pageh = h
1256 ; pagex = pagex
1257 ; pagey = pagey
1258 ; pagevw = pagevw
1259 ; pagevh = pagevh
1260 ; pagedispx = pagedispx
1261 ; pagedispy = pagedispy
1262 ; pagecol = 0
1265 e :: accu
1266 else
1267 accu
1268 else
1269 accu
1271 fold accu (n+1)
1273 List.rev (fold [] 0)
1276 let layoutS (columns, b) y sh =
1277 let sh = sh - state.hscrollh in
1278 let rec fold accu n =
1279 if n = Array.length b
1280 then accu
1281 else
1282 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1283 if (vy - y) > sh
1284 then accu
1285 else
1286 let accu =
1287 if vy + pageh > y
1288 then
1289 let x = xoff + state.x in
1290 let pagey = max 0 (y - vy) in
1291 let pagedispy = if pagey > 0 then 0 else vy - y in
1292 let pagedispx, pagex =
1293 if px = 0
1294 then (
1295 if x < 0
1296 then 0, -x
1297 else x, 0
1299 else (
1300 let px = px - x in
1301 if px < 0
1302 then -px, 0
1303 else 0, px
1306 let pagecolw = pagew/columns in
1307 let pagedispx =
1308 if pagecolw < conf.winw
1309 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1310 else pagedispx
1312 let pagevw =
1313 let vw = conf.winw - pagedispx - state.scrollw in
1314 let pw = pagew - pagex in
1315 min vw pw
1317 let pagevw = min pagevw pagecolw in
1318 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1319 if pagevw > 0 && pagevh > 0
1320 then
1321 let e =
1322 { pageno = n/columns
1323 ; pagedimno = pdimno
1324 ; pagew = pagew
1325 ; pageh = pageh
1326 ; pagex = pagex
1327 ; pagey = pagey
1328 ; pagevw = pagevw
1329 ; pagevh = pagevh
1330 ; pagedispx = pagedispx
1331 ; pagedispy = pagedispy
1332 ; pagecol = n mod columns
1335 e :: accu
1336 else
1337 accu
1338 else
1339 accu
1341 fold accu (n+1)
1343 List.rev (fold [] 0)
1346 let layout y sh =
1347 if nogeomcmds state.geomcmds
1348 then
1349 match conf.columns with
1350 | Csingle -> layout1 y sh
1351 | Cmulti c -> layoutN c y sh
1352 | Csplit s -> layoutS s y sh
1353 else []
1356 let clamp incr =
1357 let y = state.y + incr in
1358 let y = max 0 y in
1359 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1363 let itertiles l f =
1364 let tilex = l.pagex mod conf.tilew in
1365 let tiley = l.pagey mod conf.tileh in
1367 let col = l.pagex / conf.tilew in
1368 let row = l.pagey / conf.tileh in
1370 let rec rowloop row y0 dispy h =
1371 if h = 0
1372 then ()
1373 else (
1374 let dh = conf.tileh - y0 in
1375 let dh = min h dh in
1376 let rec colloop col x0 dispx w =
1377 if w = 0
1378 then ()
1379 else (
1380 let dw = conf.tilew - x0 in
1381 let dw = min w dw in
1383 f col row dispx dispy x0 y0 dw dh;
1384 colloop (col+1) 0 (dispx+dw) (w-dw)
1387 colloop col tilex l.pagedispx l.pagevw;
1388 rowloop (row+1) 0 (dispy+dh) (h-dh)
1391 if l.pagevw > 0 && l.pagevh > 0
1392 then rowloop row tiley l.pagedispy l.pagevh;
1395 let gettileopaque l col row =
1396 let key =
1397 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1399 try Some (Hashtbl.find state.tilemap key)
1400 with Not_found -> None
1403 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1404 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1405 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1408 let drawtiles l color =
1409 GlDraw.color color;
1410 let f col row x y tilex tiley w h =
1411 match gettileopaque l col row with
1412 | Some (opaque, _, t) ->
1413 let params = x, y, w, h, tilex, tiley in
1414 if conf.invert
1415 then (
1416 Gl.enable `blend;
1417 GlFunc.blend_func `zero `one_minus_src_color;
1419 drawtile params opaque;
1420 if conf.invert
1421 then Gl.disable `blend;
1422 if conf.debug
1423 then (
1424 let s = Printf.sprintf
1425 "%d[%d,%d] %f sec"
1426 l.pageno col row t
1428 let w = measurestr fstate.fontsize s in
1429 GlMisc.push_attrib [`current];
1430 GlDraw.color (0.0, 0.0, 0.0);
1431 GlDraw.rect
1432 (float (x-2), float (y-2))
1433 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1434 GlDraw.color (1.0, 1.0, 1.0);
1435 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1436 GlMisc.pop_attrib ();
1439 | _ ->
1440 let w =
1441 let lw = conf.winw - state.scrollw - x in
1442 min lw w
1443 and h =
1444 let lh = conf.winh - y in
1445 min lh h
1447 Gl.enable `texture_2d;
1448 begin match state.texid with
1449 | Some id ->
1450 GlTex.bind_texture `texture_2d id;
1451 let x0 = float x
1452 and y0 = float y
1453 and x1 = float (x+w)
1454 and y1 = float (y+h) in
1456 let tw = float w /. 64.0
1457 and th = float h /. 64.0 in
1458 let tx0 = float tilex /. 64.0
1459 and ty0 = float tiley /. 64.0 in
1460 let tx1 = tx0 +. tw
1461 and ty1 = ty0 +. th in
1462 GlDraw.begins `quads;
1463 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1464 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1465 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1466 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1467 GlDraw.ends ();
1469 Gl.disable `texture_2d;
1470 | None ->
1471 GlDraw.color (1.0, 1.0, 1.0);
1472 GlDraw.rect
1473 (float x, float y)
1474 (float (x+w), float (y+h));
1475 end;
1476 if w > 128 && h > fstate.fontsize + 10
1477 then (
1478 GlDraw.color (0.0, 0.0, 0.0);
1479 let c, r =
1480 if conf.verbose
1481 then (col*conf.tilew, row*conf.tileh)
1482 else col, row
1484 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1486 GlDraw.color color;
1488 itertiles l f
1491 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1493 let tilevisible1 l x y =
1494 let ax0 = l.pagex
1495 and ax1 = l.pagex + l.pagevw
1496 and ay0 = l.pagey
1497 and ay1 = l.pagey + l.pagevh in
1499 let bx0 = x
1500 and by0 = y in
1501 let bx1 = min (bx0 + conf.tilew) l.pagew
1502 and by1 = min (by0 + conf.tileh) l.pageh in
1504 let rx0 = max ax0 bx0
1505 and ry0 = max ay0 by0
1506 and rx1 = min ax1 bx1
1507 and ry1 = min ay1 by1 in
1509 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1510 nonemptyintersection
1513 let tilevisible layout n x y =
1514 let rec findpageinlayout m = function
1515 | l :: rest when l.pageno = n ->
1516 tilevisible1 l x y || (
1517 match conf.columns with
1518 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1519 | _ -> false
1521 | _ :: rest -> findpageinlayout 0 rest
1522 | [] -> false
1524 findpageinlayout 0 layout;
1527 let tileready l x y =
1528 tilevisible1 l x y &&
1529 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1532 let tilepage n p layout =
1533 let rec loop = function
1534 | l :: rest ->
1535 if l.pageno = n
1536 then
1537 let f col row _ _ _ _ _ _ =
1538 if state.currently = Idle
1539 then
1540 match gettileopaque l col row with
1541 | Some _ -> ()
1542 | None ->
1543 let x = col*conf.tilew
1544 and y = row*conf.tileh in
1545 let w =
1546 let w = l.pagew - x in
1547 min w conf.tilew
1549 let h =
1550 let h = l.pageh - y in
1551 min h conf.tileh
1553 wcmd "tile %s %d %d %d %d" p x y w h;
1554 state.currently <-
1555 Tiling (
1556 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1557 conf.tilew, conf.tileh
1560 itertiles l f;
1561 else
1562 loop rest
1564 | [] -> ()
1566 if nogeomcmds state.geomcmds
1567 then loop layout;
1570 let preloadlayout visiblepages =
1571 let presentation = conf.presentation in
1572 let interpagespace = conf.interpagespace in
1573 let maxy = state.maxy in
1574 conf.presentation <- false;
1575 conf.interpagespace <- 0;
1576 state.maxy <- calcheight ();
1577 let y =
1578 match visiblepages with
1579 | [] -> if state.y >= maxy then maxy else 0
1580 | l :: _ -> getpagey l.pageno + l.pagey
1582 let y = if y < conf.winh then 0 else y - conf.winh in
1583 let h = state.y - y + conf.winh*3 in
1584 let pages = layout y h in
1585 conf.presentation <- presentation;
1586 conf.interpagespace <- interpagespace;
1587 state.maxy <- maxy;
1588 pages;
1591 let load pages =
1592 let rec loop pages =
1593 if state.currently != Idle
1594 then ()
1595 else
1596 match pages with
1597 | l :: rest ->
1598 begin match getopaque l.pageno with
1599 | None ->
1600 wcmd "page %d %d" l.pageno l.pagedimno;
1601 state.currently <- Loading (l, state.gen);
1602 | Some opaque ->
1603 tilepage l.pageno opaque pages;
1604 loop rest
1605 end;
1606 | _ -> ()
1608 if nogeomcmds state.geomcmds
1609 then loop pages
1612 let preload pages =
1613 load pages;
1614 if conf.preload && state.currently = Idle
1615 then load (preloadlayout pages);
1618 let layoutready layout =
1619 let rec fold all ls =
1620 all && match ls with
1621 | l :: rest ->
1622 let seen = ref false in
1623 let allvisible = ref true in
1624 let foo col row _ _ _ _ _ _ =
1625 seen := true;
1626 allvisible := !allvisible &&
1627 begin match gettileopaque l col row with
1628 | Some _ -> true
1629 | None -> false
1632 itertiles l foo;
1633 fold (!seen && !allvisible) rest
1634 | [] -> true
1636 let alltilesvisible = fold true layout in
1637 alltilesvisible;
1640 let gotoy y =
1641 let y = bound y 0 state.maxy in
1642 let y, layout, proceed =
1643 match conf.maxwait with
1644 | Some time when state.ghyll == noghyll ->
1645 begin match state.throttle with
1646 | None ->
1647 let layout = layout y conf.winh in
1648 let ready = layoutready layout in
1649 if not ready
1650 then (
1651 load layout;
1652 state.throttle <- Some (layout, y, now ());
1654 else G.postRedisplay "gotoy showall (None)";
1655 y, layout, ready
1656 | Some (_, _, started) ->
1657 let dt = now () -. started in
1658 if dt > time
1659 then (
1660 state.throttle <- None;
1661 let layout = layout y conf.winh in
1662 load layout;
1663 G.postRedisplay "maxwait";
1664 y, layout, true
1666 else -1, [], false
1669 | _ ->
1670 let layout = layout y conf.winh in
1671 if true || layoutready layout
1672 then G.postRedisplay "gotoy ready";
1673 y, layout, true
1675 if proceed
1676 then (
1677 state.y <- y;
1678 state.layout <- layout;
1679 begin match state.mode with
1680 | LinkNav (Ltexact (pageno, linkno)) ->
1681 let rec loop = function
1682 | [] ->
1683 state.mode <- LinkNav (Ltgendir 0)
1684 | l :: _ when l.pageno = pageno ->
1685 begin match getopaque pageno with
1686 | None ->
1687 state.mode <- LinkNav (Ltgendir 0)
1688 | Some opaque ->
1689 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1690 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1691 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1692 then state.mode <- LinkNav (Ltgendir 0)
1694 | _ :: rest -> loop rest
1696 loop layout
1697 | _ -> ()
1698 end;
1699 begin match state.mode with
1700 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1701 if not (pagevisible layout pageno)
1702 then (
1703 match state.layout with
1704 | [] -> ()
1705 | l :: _ ->
1706 state.mode <- Birdseye (
1707 conf, leftx, l.pageno, hooverpageno, anchor
1710 | LinkNav (Ltgendir dir as lt) ->
1711 let linknav =
1712 let rec loop = function
1713 | [] -> lt
1714 | l :: rest ->
1715 match getopaque l.pageno with
1716 | None -> loop rest
1717 | Some opaque ->
1718 let link =
1719 let ld =
1720 if dir = 0
1721 then LDfirstvisible (l.pagex, l.pagey, dir)
1722 else (
1723 if dir > 0 then LDfirst else LDlast
1726 findlink opaque ld
1728 match link with
1729 | Lnotfound -> loop rest
1730 | Lfound n ->
1731 showlinktype (getlink opaque n);
1732 Ltexact (l.pageno, n)
1734 loop state.layout
1736 state.mode <- LinkNav linknav
1737 | _ -> ()
1738 end;
1739 preload layout;
1741 state.ghyll <- noghyll;
1742 if conf.updatecurs
1743 then (
1744 let mx, my = state.mpos in
1745 updateunder mx my;
1749 let conttiling pageno opaque =
1750 tilepage pageno opaque
1751 (if conf.preload then preloadlayout state.layout else state.layout)
1754 let gotoy_and_clear_text y =
1755 if not conf.verbose then state.text <- "";
1756 gotoy y;
1759 let getanchor () =
1760 match state.layout with
1761 | [] -> emptyanchor
1762 | l :: _ ->
1763 let coloff = l.pagecol * l.pageh in
1764 (l.pageno, (float l.pagey +. float coloff) /. float l.pageh)
1767 let getanchory (n, top) =
1768 let y, h = getpageyh n in
1769 y + (truncate (top *. float h));
1772 let gotoanchor anchor =
1773 gotoy (getanchory anchor);
1776 let addnav () =
1777 cbput state.hists.nav (getanchor ());
1780 let getnav dir =
1781 let anchor = cbgetc state.hists.nav dir in
1782 getanchory anchor;
1785 let gotoghyll y =
1786 let rec scroll f n a b =
1787 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1788 let snake f a b =
1789 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1790 if f < a
1791 then s (float f /. float a)
1792 else (
1793 if f > b
1794 then 1.0 -. s ((float (f-b) /. float (n-b)))
1795 else 1.0
1798 snake f a b
1799 and summa f n a b =
1800 (* courtesy:
1801 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1802 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1803 let iv1 = iv f in
1804 let ins = float a *. iv1
1805 and outs = float (n-b) *. iv1 in
1806 let ones = b - a in
1807 ins +. outs +. float ones
1809 let rec set (_N, _A, _B) y sy =
1810 let sum = summa 1.0 _N _A _B in
1811 let dy = float (y - sy) in
1812 state.ghyll <- (
1813 let rec gf n y1 o =
1814 if n >= _N
1815 then state.ghyll <- noghyll
1816 else
1817 let go n =
1818 let s = scroll n _N _A _B in
1819 let y1 = y1 +. ((s *. dy) /. sum) in
1820 gotoy_and_clear_text (truncate y1);
1821 state.ghyll <- gf (n+1) y1;
1823 match o with
1824 | None -> go n
1825 | Some y' -> set (_N/2, 0, 0) y' state.y
1827 gf 0 (float state.y)
1830 match conf.ghyllscroll with
1831 | None ->
1832 gotoy_and_clear_text y
1833 | Some nab ->
1834 if state.ghyll == noghyll
1835 then set nab y state.y
1836 else state.ghyll (Some y)
1839 let gotopage n top =
1840 let y, h = getpageyh n in
1841 let y = y + (truncate (top *. float h)) in
1842 gotoghyll y
1845 let gotopage1 n top =
1846 let y = getpagey n in
1847 let y = y + top in
1848 gotoghyll y
1851 let invalidate s f =
1852 state.layout <- [];
1853 state.pdims <- [];
1854 state.rects <- [];
1855 state.rects1 <- [];
1856 match state.geomcmds with
1857 | ps, [] when String.length ps = 0 ->
1858 f ();
1859 state.geomcmds <- s, [];
1861 | ps, [] ->
1862 state.geomcmds <- ps, [s, f];
1864 | ps, (s', _) :: rest when s' = s ->
1865 state.geomcmds <- ps, ((s, f) :: rest);
1867 | ps, cmds ->
1868 state.geomcmds <- ps, ((s, f) :: cmds);
1871 let opendoc path password =
1872 state.path <- path;
1873 state.password <- password;
1874 state.gen <- state.gen + 1;
1875 state.docinfo <- [];
1877 setaalevel conf.aalevel;
1878 Wsi.settitle ("llpp " ^ Filename.basename path);
1879 wcmd "open %s\000%s\000" path password;
1880 invalidate "reqlayout"
1881 (fun () ->
1882 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1885 let scalecolor c =
1886 let c = c *. conf.colorscale in
1887 (c, c, c);
1890 let scalecolor2 (r, g, b) =
1891 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1894 let docolumns = function
1895 | Csingle -> ()
1897 | Cmulti ((columns, coverA, coverB), _) ->
1898 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1899 let rec loop pageno pdimno pdim x y rowh pdims =
1900 let rec fixrow m = if m = pageno then () else
1901 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1902 if h < rowh
1903 then (
1904 let y = y + (rowh - h) / 2 in
1905 a.(m) <- (pdimno, x, y, pdim);
1907 fixrow (m+1)
1909 if pageno = state.pagecount
1910 then fixrow (((pageno - 1) / columns) * columns)
1911 else
1912 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1913 match pdims with
1914 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1915 pdimno+1, pdim, rest
1916 | _ ->
1917 pdimno, pdim, pdims
1919 let x, y, rowh' =
1920 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1921 then (
1922 (conf.winw - state.scrollw - w) / 2,
1923 y + rowh + conf.interpagespace, h
1925 else (
1926 if (pageno - coverA) mod columns = 0
1927 then 0, y + rowh + conf.interpagespace, h
1928 else x, y, max rowh h
1931 if pageno > 1 && (pageno - coverA) mod columns = 0
1932 then fixrow (pageno - columns);
1933 a.(pageno) <- (pdimno, x, y, pdim);
1934 let x = x + w + xoff*2 + conf.interpagespace in
1935 loop (pageno+1) pdimno pdim x y rowh' pdims
1937 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1938 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1940 | Csplit (c, _) ->
1941 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1942 let rec loop pageno pdimno pdim y pdims =
1943 if pageno = state.pagecount
1944 then ()
1945 else
1946 let pdimno, ((_, w, h, _) as pdim), pdims =
1947 match pdims with
1948 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1949 pdimno+1, pdim, rest
1950 | _ ->
1951 pdimno, pdim, pdims
1953 let cw = w / c in
1954 let rec loop1 n x y =
1955 if n = c then y else (
1956 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1957 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1960 let y = loop1 0 0 y in
1961 loop (pageno+1) pdimno pdim y pdims
1963 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1964 conf.columns <- Csplit (c, a);
1967 let represent () =
1968 docolumns conf.columns;
1969 state.maxy <- calcheight ();
1970 state.hscrollh <-
1971 if state.w <= conf.winw - state.scrollw
1972 then 0
1973 else state.scrollw
1975 match state.mode with
1976 | Birdseye (_, _, pageno, _, _) ->
1977 let y, h = getpageyh pageno in
1978 let top = (conf.winh - h) / 2 in
1979 gotoy (max 0 (y - top))
1980 | _ -> gotoanchor state.anchor
1983 let reshape w h =
1984 GlDraw.viewport 0 0 w h;
1985 let firsttime = state.geomcmds == firstgeomcmds in
1986 if not firsttime && nogeomcmds state.geomcmds
1987 then state.anchor <- getanchor ();
1989 conf.winw <- w;
1990 let w = truncate (float w *. conf.zoom) - state.scrollw in
1991 let w = max w 2 in
1992 conf.winh <- h;
1993 setfontsize fstate.fontsize;
1994 GlMat.mode `modelview;
1995 GlMat.load_identity ();
1997 GlMat.mode `projection;
1998 GlMat.load_identity ();
1999 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2000 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2001 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2003 let relx =
2004 if conf.zoom <= 1.0
2005 then 0.0
2006 else float state.x /. float state.w
2008 invalidate "geometry"
2009 (fun () ->
2010 state.w <- w;
2011 if not firsttime
2012 then state.x <- truncate (relx *. float w);
2013 let w =
2014 match conf.columns with
2015 | Csingle -> w
2016 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2017 | Csplit (c, _) -> w * c
2019 wcmd "geometry %d %d" w h);
2022 let enttext () =
2023 let len = String.length state.text in
2024 let drawstring s =
2025 let hscrollh =
2026 match state.mode with
2027 | Textentry _
2028 | View ->
2029 let h, _, _ = state.uioh#scrollpw in
2031 | _ -> 0
2033 let rect x w =
2034 GlDraw.rect
2035 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2036 (x+.w, float (conf.winh - hscrollh))
2039 let w = float (conf.winw - state.scrollw - 1) in
2040 if state.progress >= 0.0 && state.progress < 1.0
2041 then (
2042 GlDraw.color (0.3, 0.3, 0.3);
2043 let w1 = w *. state.progress in
2044 rect 0.0 w1;
2045 GlDraw.color (0.0, 0.0, 0.0);
2046 rect w1 (w-.w1)
2048 else (
2049 GlDraw.color (0.0, 0.0, 0.0);
2050 rect 0.0 w;
2053 GlDraw.color (1.0, 1.0, 1.0);
2054 drawstring fstate.fontsize
2055 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2057 let s =
2058 match state.mode with
2059 | Textentry ((prefix, text, _, _, _), _) ->
2060 let s =
2061 if len > 0
2062 then
2063 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2064 else
2065 Printf.sprintf "%s%s_" prefix text
2069 | _ -> state.text
2071 let s =
2072 if state.newerrmsgs
2073 then (
2074 if not (istextentry state.mode)
2075 then
2076 let s1 = "(press 'e' to review error messasges)" in
2077 if String.length s > 0 then s ^ " " ^ s1 else s1
2078 else s
2080 else s
2082 if String.length s > 0
2083 then drawstring s
2086 let gctiles () =
2087 let len = Queue.length state.tilelru in
2088 let rec loop qpos =
2089 if state.memused <= conf.memlimit
2090 then ()
2091 else (
2092 if qpos < len
2093 then
2094 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2095 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2096 let (_, pw, ph, _) = getpagedim n in
2098 gen = state.gen
2099 && colorspace = conf.colorspace
2100 && angle = conf.angle
2101 && pagew = pw
2102 && pageh = ph
2103 && (
2104 let layout =
2105 match state.throttle with
2106 | None ->
2107 if conf.preload
2108 then preloadlayout state.layout
2109 else state.layout
2110 | Some (layout, _, _) ->
2111 layout
2113 let x = col*conf.tilew
2114 and y = row*conf.tileh in
2115 tilevisible layout n x y
2117 then Queue.push lruitem state.tilelru
2118 else (
2119 wcmd "freetile %s" p;
2120 state.memused <- state.memused - s;
2121 state.uioh#infochanged Memused;
2122 Hashtbl.remove state.tilemap k;
2124 loop (qpos+1)
2127 loop 0
2130 let flushtiles () =
2131 Queue.iter (fun (k, p, s) ->
2132 wcmd "freetile %s" p;
2133 state.memused <- state.memused - s;
2134 state.uioh#infochanged Memused;
2135 Hashtbl.remove state.tilemap k;
2136 ) state.tilelru;
2137 Queue.clear state.tilelru;
2138 load state.layout;
2141 let logcurrently = function
2142 | Idle -> dolog "Idle"
2143 | Loading (l, gen) ->
2144 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2145 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2146 dolog
2147 "Tiling %d[%d,%d] page=%s cs=%s angle"
2148 l.pageno col row pageopaque
2149 (colorspace_to_string colorspace)
2151 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2152 angle gen conf.angle state.gen
2153 tilew tileh
2154 conf.tilew conf.tileh
2156 | Outlining _ ->
2157 dolog "outlining"
2160 let act cmds =
2161 (* dolog "%S" cmds; *)
2162 let op, args =
2163 let spacepos =
2164 try String.index cmds ' '
2165 with Not_found -> -1
2167 if spacepos = -1
2168 then cmds, ""
2169 else
2170 let l = String.length cmds in
2171 let op = String.sub cmds 0 spacepos in
2172 op, begin
2173 if l - spacepos < 2 then ""
2174 else String.sub cmds (spacepos+1) (l-spacepos-1)
2177 match op with
2178 | "clear" ->
2179 state.uioh#infochanged Pdim;
2180 state.pdims <- [];
2182 | "clearrects" ->
2183 state.rects <- state.rects1;
2184 G.postRedisplay "clearrects";
2186 | "continue" ->
2187 let n =
2188 try Scanf.sscanf args "%u" (fun n -> n)
2189 with exn ->
2190 dolog "error processing 'continue' %S: %s"
2191 cmds (Printexc.to_string exn);
2192 exit 1;
2194 state.pagecount <- n;
2195 begin match state.currently with
2196 | Outlining l ->
2197 state.currently <- Idle;
2198 state.outlines <- Array.of_list (List.rev l)
2199 | _ -> ()
2200 end;
2202 let cur, cmds = state.geomcmds in
2203 if String.length cur = 0
2204 then failwith "umpossible";
2206 begin match List.rev cmds with
2207 | [] ->
2208 state.geomcmds <- "", [];
2209 represent ();
2210 | (s, f) :: rest ->
2211 f ();
2212 state.geomcmds <- s, List.rev rest;
2213 end;
2214 if conf.maxwait = None
2215 then G.postRedisplay "continue";
2217 | "title" ->
2218 Wsi.settitle args
2220 | "msg" ->
2221 showtext ' ' args
2223 | "vmsg" ->
2224 if conf.verbose
2225 then showtext ' ' args
2227 | "progress" ->
2228 let progress, text =
2230 Scanf.sscanf args "%f %n"
2231 (fun f pos ->
2232 f, String.sub args pos (String.length args - pos))
2233 with exn ->
2234 dolog "error processing 'progress' %S: %s"
2235 cmds (Printexc.to_string exn);
2236 exit 1;
2238 state.text <- text;
2239 state.progress <- progress;
2240 G.postRedisplay "progress"
2242 | "firstmatch" ->
2243 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2245 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2246 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2247 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2248 with exn ->
2249 dolog "error processing 'firstmatch' %S: %s"
2250 cmds (Printexc.to_string exn);
2251 exit 1;
2253 let y = (getpagey pageno) + truncate y0 in
2254 addnav ();
2255 gotoy y;
2256 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2258 | "match" ->
2259 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2261 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2262 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2263 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2264 with exn ->
2265 dolog "error processing 'match' %S: %s"
2266 cmds (Printexc.to_string exn);
2267 exit 1;
2269 state.rects1 <-
2270 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2272 | "page" ->
2273 let pageopaque, t =
2275 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2276 with exn ->
2277 dolog "error processing 'page' %S: %s"
2278 cmds (Printexc.to_string exn);
2279 exit 1;
2281 begin match state.currently with
2282 | Loading (l, gen) ->
2283 vlog "page %d took %f sec" l.pageno t;
2284 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2285 begin match state.throttle with
2286 | None ->
2287 let preloadedpages =
2288 if conf.preload
2289 then preloadlayout state.layout
2290 else state.layout
2292 let evict () =
2293 let module IntSet =
2294 Set.Make (struct type t = int let compare = (-) end) in
2295 let set =
2296 List.fold_left (fun s l -> IntSet.add l.pageno s)
2297 IntSet.empty preloadedpages
2299 let evictedpages =
2300 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2301 if not (IntSet.mem pageno set)
2302 then (
2303 wcmd "freepage %s" opaque;
2304 key :: accu
2306 else accu
2307 ) state.pagemap []
2309 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2311 evict ();
2312 state.currently <- Idle;
2313 if gen = state.gen
2314 then (
2315 tilepage l.pageno pageopaque state.layout;
2316 load state.layout;
2317 load preloadedpages;
2318 if pagevisible state.layout l.pageno
2319 && layoutready state.layout
2320 then G.postRedisplay "page";
2323 | Some (layout, _, _) ->
2324 state.currently <- Idle;
2325 tilepage l.pageno pageopaque layout;
2326 load state.layout
2327 end;
2329 | _ ->
2330 dolog "Inconsistent loading state";
2331 logcurrently state.currently;
2332 exit 1
2335 | "tile" ->
2336 let (x, y, opaque, size, t) =
2338 Scanf.sscanf args "%u %u %s %u %f"
2339 (fun x y p size t -> (x, y, p, size, t))
2340 with exn ->
2341 dolog "error processing 'tile' %S: %s"
2342 cmds (Printexc.to_string exn);
2343 exit 1;
2345 begin match state.currently with
2346 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2347 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2349 if tilew != conf.tilew || tileh != conf.tileh
2350 then (
2351 wcmd "freetile %s" opaque;
2352 state.currently <- Idle;
2353 load state.layout;
2355 else (
2356 puttileopaque l col row gen cs angle opaque size t;
2357 state.memused <- state.memused + size;
2358 state.uioh#infochanged Memused;
2359 gctiles ();
2360 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2361 opaque, size) state.tilelru;
2363 let layout =
2364 match state.throttle with
2365 | None -> state.layout
2366 | Some (layout, _, _) -> layout
2369 state.currently <- Idle;
2370 if gen = state.gen
2371 && conf.colorspace = cs
2372 && conf.angle = angle
2373 && tilevisible layout l.pageno x y
2374 then conttiling l.pageno pageopaque;
2376 begin match state.throttle with
2377 | None ->
2378 preload state.layout;
2379 if gen = state.gen
2380 && conf.colorspace = cs
2381 && conf.angle = angle
2382 && tilevisible state.layout l.pageno x y
2383 then G.postRedisplay "tile nothrottle";
2385 | Some (layout, y, _) ->
2386 let ready = layoutready layout in
2387 if ready
2388 then (
2389 state.y <- y;
2390 state.layout <- layout;
2391 state.throttle <- None;
2392 G.postRedisplay "throttle";
2394 else load layout;
2395 end;
2398 | _ ->
2399 dolog "Inconsistent tiling state";
2400 logcurrently state.currently;
2401 exit 1
2404 | "pdim" ->
2405 let pdim =
2407 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2408 with exn ->
2409 dolog "error processing 'pdim' %S: %s"
2410 cmds (Printexc.to_string exn);
2411 exit 1;
2413 state.uioh#infochanged Pdim;
2414 state.pdims <- pdim :: state.pdims
2416 | "o" ->
2417 let (l, n, t, h, pos) =
2419 Scanf.sscanf args "%u %u %d %u %n"
2420 (fun l n t h pos -> l, n, t, h, pos)
2421 with exn ->
2422 dolog "error processing 'o' %S: %s"
2423 cmds (Printexc.to_string exn);
2424 exit 1;
2426 let s = String.sub args pos (String.length args - pos) in
2427 let outline = (s, l, (n, float t /. float h)) in
2428 begin match state.currently with
2429 | Outlining outlines ->
2430 state.currently <- Outlining (outline :: outlines)
2431 | Idle ->
2432 state.currently <- Outlining [outline]
2433 | currently ->
2434 dolog "invalid outlining state";
2435 logcurrently currently
2438 | "info" ->
2439 state.docinfo <- (1, args) :: state.docinfo
2441 | "infoend" ->
2442 state.uioh#infochanged Docinfo;
2443 state.docinfo <- List.rev state.docinfo
2445 | _ ->
2446 dolog "unknown cmd `%S'" cmds
2449 let onhist cb =
2450 let rc = cb.rc in
2451 let action = function
2452 | HCprev -> cbget cb ~-1
2453 | HCnext -> cbget cb 1
2454 | HCfirst -> cbget cb ~-(cb.rc)
2455 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2456 and cancel () = cb.rc <- rc
2457 in (action, cancel)
2460 let search pattern forward =
2461 if String.length pattern > 0
2462 then
2463 let pn, py =
2464 match state.layout with
2465 | [] -> 0, 0
2466 | l :: _ ->
2467 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2469 wcmd "search %d %d %d %d,%s\000"
2470 (btod conf.icase) pn py (btod forward) pattern;
2473 let intentry text key =
2474 let c =
2475 if key >= 32 && key < 127
2476 then Char.chr key
2477 else '\000'
2479 match c with
2480 | '0' .. '9' ->
2481 let text = addchar text c in
2482 TEcont text
2484 | _ ->
2485 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2486 TEcont text
2489 let linknentry text key =
2490 let c =
2491 if key >= 32 && key < 127
2492 then Char.chr key
2493 else '\000'
2495 match c with
2496 | 'a' .. 'z' ->
2497 let text = addchar text c in
2498 TEcont text
2500 | _ ->
2501 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2502 TEcont text
2505 let linkndone f s =
2506 if String.length s > 0
2507 then (
2508 let n =
2509 let l = String.length s in
2510 let rec loop pos n = if pos = l then n else
2511 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2512 loop (pos+1) (n*26 + m)
2513 in loop 0 0
2515 let rec loop n = function
2516 | [] -> ()
2517 | l :: rest ->
2518 match getopaque l.pageno with
2519 | None -> loop n rest
2520 | Some opaque ->
2521 let m = getlinkcount opaque in
2522 if n < m
2523 then (
2524 let under = getlink opaque n in
2525 f under
2527 else loop (n-m) rest
2529 loop n state.layout;
2533 let textentry text key =
2534 if key land 0xff00 = 0xff00
2535 then TEcont text
2536 else TEcont (text ^ Wsi.toutf8 key)
2539 let reqlayout angle proportional =
2540 match state.throttle with
2541 | None ->
2542 if nogeomcmds state.geomcmds
2543 then state.anchor <- getanchor ();
2544 conf.angle <- angle mod 360;
2545 if conf.angle != 0
2546 then (
2547 match state.mode with
2548 | LinkNav _ -> state.mode <- View
2549 | _ -> ()
2551 conf.proportional <- proportional;
2552 invalidate "reqlayout"
2553 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2554 | _ -> ()
2557 let settrim trimmargins trimfuzz =
2558 if nogeomcmds state.geomcmds
2559 then state.anchor <- getanchor ();
2560 conf.trimmargins <- trimmargins;
2561 conf.trimfuzz <- trimfuzz;
2562 let x0, y0, x1, y1 = trimfuzz in
2563 invalidate "settrim"
2564 (fun () ->
2565 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2566 Hashtbl.iter (fun _ opaque ->
2567 wcmd "freepage %s" opaque;
2568 ) state.pagemap;
2569 Hashtbl.clear state.pagemap;
2572 let setzoom zoom =
2573 match state.throttle with
2574 | None ->
2575 let zoom = max 0.01 zoom in
2576 if zoom <> conf.zoom
2577 then (
2578 state.prevzoom <- conf.zoom;
2579 conf.zoom <- zoom;
2580 reshape conf.winw conf.winh;
2581 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2584 | Some (layout, y, started) ->
2585 let time =
2586 match conf.maxwait with
2587 | None -> 0.0
2588 | Some t -> t
2590 let dt = now () -. started in
2591 if dt > time
2592 then (
2593 state.y <- y;
2594 load layout;
2598 let setcolumns mode columns coverA coverB =
2599 if columns < 0
2600 then (
2601 if isbirdseye mode
2602 then showtext '!' "split mode doesn't work in bird's eye"
2603 else (
2604 conf.columns <- Csplit (-columns, [||]);
2605 state.x <- 0;
2606 conf.zoom <- 1.0;
2609 else (
2610 if columns < 2
2611 then (
2612 conf.columns <- Csingle;
2613 state.x <- 0;
2614 setzoom 1.0;
2616 else (
2617 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2618 conf.zoom <- 1.0;
2621 reshape conf.winw conf.winh;
2624 let enterbirdseye () =
2625 let zoom = float conf.thumbw /. float conf.winw in
2626 let birdseyepageno =
2627 let cy = conf.winh / 2 in
2628 let fold = function
2629 | [] -> 0
2630 | l :: rest ->
2631 let rec fold best = function
2632 | [] -> best.pageno
2633 | l :: rest ->
2634 let d = cy - (l.pagedispy + l.pagevh/2)
2635 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2636 if abs d < abs dbest
2637 then fold l rest
2638 else best.pageno
2639 in fold l rest
2641 fold state.layout
2643 state.mode <- Birdseye (
2644 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2646 conf.zoom <- zoom;
2647 conf.presentation <- false;
2648 conf.interpagespace <- 10;
2649 conf.hlinks <- false;
2650 state.x <- 0;
2651 state.mstate <- Mnone;
2652 conf.maxwait <- None;
2653 conf.columns <- (
2654 match conf.beyecolumns with
2655 | Some c ->
2656 conf.zoom <- 1.0;
2657 Cmulti ((c, 0, 0), [||])
2658 | None -> Csingle
2660 Wsi.setcursor Wsi.CURSOR_INHERIT;
2661 if conf.verbose
2662 then
2663 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2664 (100.0*.zoom)
2665 else
2666 state.text <- ""
2668 reshape conf.winw conf.winh;
2671 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2672 state.mode <- View;
2673 conf.zoom <- c.zoom;
2674 conf.presentation <- c.presentation;
2675 conf.interpagespace <- c.interpagespace;
2676 conf.maxwait <- c.maxwait;
2677 conf.hlinks <- c.hlinks;
2678 conf.beyecolumns <- (
2679 match conf.columns with
2680 | Cmulti ((c, _, _), _) -> Some c
2681 | Csingle -> None
2682 | Csplit _ -> failwith "leaving bird's eye split mode"
2684 conf.columns <- (
2685 match c.columns with
2686 | Cmulti (c, _) -> Cmulti (c, [||])
2687 | Csingle -> Csingle
2688 | Csplit (c, _) -> Csplit (c, [||])
2690 state.x <- leftx;
2691 if conf.verbose
2692 then
2693 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2694 (100.0*.conf.zoom)
2696 reshape conf.winw conf.winh;
2697 state.anchor <- if goback then anchor else (pageno, 0.0);
2700 let togglebirdseye () =
2701 match state.mode with
2702 | Birdseye vals -> leavebirdseye vals true
2703 | View -> enterbirdseye ()
2704 | _ -> ()
2707 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2708 let pageno = max 0 (pageno - incr) in
2709 let rec loop = function
2710 | [] -> gotopage1 pageno 0
2711 | l :: _ when l.pageno = pageno ->
2712 if l.pagedispy >= 0 && l.pagey = 0
2713 then G.postRedisplay "upbirdseye"
2714 else gotopage1 pageno 0
2715 | _ :: rest -> loop rest
2717 loop state.layout;
2718 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2721 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2722 let pageno = min (state.pagecount - 1) (pageno + incr) in
2723 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2724 let rec loop = function
2725 | [] ->
2726 let y, h = getpageyh pageno in
2727 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2728 gotoy (clamp dy)
2729 | l :: _ when l.pageno = pageno ->
2730 if l.pagevh != l.pageh
2731 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2732 else G.postRedisplay "downbirdseye"
2733 | _ :: rest -> loop rest
2735 loop state.layout
2738 let optentry mode _ key =
2739 let btos b = if b then "on" else "off" in
2740 if key >= 32 && key < 127
2741 then
2742 let c = Char.chr key in
2743 match c with
2744 | 's' ->
2745 let ondone s =
2746 try conf.scrollstep <- int_of_string s with exc ->
2747 state.text <- Printf.sprintf "bad integer `%s': %s"
2748 s (Printexc.to_string exc)
2750 TEswitch ("scroll step: ", "", None, intentry, ondone)
2752 | 'A' ->
2753 let ondone s =
2755 conf.autoscrollstep <- int_of_string s;
2756 if state.autoscroll <> None
2757 then state.autoscroll <- Some conf.autoscrollstep
2758 with exc ->
2759 state.text <- Printf.sprintf "bad integer `%s': %s"
2760 s (Printexc.to_string exc)
2762 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2764 | 'C' ->
2765 let mode = state.mode in
2766 let ondone s =
2768 let n, a, b = multicolumns_of_string s in
2769 setcolumns mode n a b;
2770 with exc ->
2771 state.text <- Printf.sprintf "bad columns `%s': %s"
2772 s (Printexc.to_string exc)
2774 TEswitch ("columns: ", "", None, textentry, ondone)
2776 | 'Z' ->
2777 let ondone s =
2779 let zoom = float (int_of_string s) /. 100.0 in
2780 setzoom zoom
2781 with exc ->
2782 state.text <- Printf.sprintf "bad integer `%s': %s"
2783 s (Printexc.to_string exc)
2785 TEswitch ("zoom: ", "", None, intentry, ondone)
2787 | 't' ->
2788 let ondone s =
2790 conf.thumbw <- bound (int_of_string s) 2 4096;
2791 state.text <-
2792 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2793 begin match mode with
2794 | Birdseye beye ->
2795 leavebirdseye beye false;
2796 enterbirdseye ();
2797 | _ -> ();
2799 with exc ->
2800 state.text <- Printf.sprintf "bad integer `%s': %s"
2801 s (Printexc.to_string exc)
2803 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2805 | 'R' ->
2806 let ondone s =
2807 match try
2808 Some (int_of_string s)
2809 with exc ->
2810 state.text <- Printf.sprintf "bad integer `%s': %s"
2811 s (Printexc.to_string exc);
2812 None
2813 with
2814 | Some angle -> reqlayout angle conf.proportional
2815 | None -> ()
2817 TEswitch ("rotation: ", "", None, intentry, ondone)
2819 | 'i' ->
2820 conf.icase <- not conf.icase;
2821 TEdone ("case insensitive search " ^ (btos conf.icase))
2823 | 'p' ->
2824 conf.preload <- not conf.preload;
2825 gotoy state.y;
2826 TEdone ("preload " ^ (btos conf.preload))
2828 | 'v' ->
2829 conf.verbose <- not conf.verbose;
2830 TEdone ("verbose " ^ (btos conf.verbose))
2832 | 'd' ->
2833 conf.debug <- not conf.debug;
2834 TEdone ("debug " ^ (btos conf.debug))
2836 | 'h' ->
2837 conf.maxhfit <- not conf.maxhfit;
2838 state.maxy <- calcheight ();
2839 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2841 | 'c' ->
2842 conf.crophack <- not conf.crophack;
2843 TEdone ("crophack " ^ btos conf.crophack)
2845 | 'a' ->
2846 let s =
2847 match conf.maxwait with
2848 | None ->
2849 conf.maxwait <- Some infinity;
2850 "always wait for page to complete"
2851 | Some _ ->
2852 conf.maxwait <- None;
2853 "show placeholder if page is not ready"
2855 TEdone s
2857 | 'f' ->
2858 conf.underinfo <- not conf.underinfo;
2859 TEdone ("underinfo " ^ btos conf.underinfo)
2861 | 'P' ->
2862 conf.savebmarks <- not conf.savebmarks;
2863 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2865 | 'S' ->
2866 let ondone s =
2868 let pageno, py =
2869 match state.layout with
2870 | [] -> 0, 0
2871 | l :: _ ->
2872 l.pageno, l.pagey
2874 conf.interpagespace <- int_of_string s;
2875 docolumns conf.columns;
2876 state.maxy <- calcheight ();
2877 let y = getpagey pageno in
2878 gotoy (y + py)
2879 with exc ->
2880 state.text <- Printf.sprintf "bad integer `%s': %s"
2881 s (Printexc.to_string exc)
2883 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2885 | 'l' ->
2886 reqlayout conf.angle (not conf.proportional);
2887 TEdone ("proportional display " ^ btos conf.proportional)
2889 | 'T' ->
2890 settrim (not conf.trimmargins) conf.trimfuzz;
2891 TEdone ("trim margins " ^ btos conf.trimmargins)
2893 | 'I' ->
2894 conf.invert <- not conf.invert;
2895 TEdone ("invert colors " ^ btos conf.invert)
2897 | 'x' ->
2898 let ondone s =
2899 cbput state.hists.sel s;
2900 conf.selcmd <- s;
2902 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2903 textentry, ondone)
2905 | _ ->
2906 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2907 TEstop
2908 else
2909 TEcont state.text
2912 class type lvsource = object
2913 method getitemcount : int
2914 method getitem : int -> (string * int)
2915 method hasaction : int -> bool
2916 method exit :
2917 uioh:uioh ->
2918 cancel:bool ->
2919 active:int ->
2920 first:int ->
2921 pan:int ->
2922 qsearch:string ->
2923 uioh option
2924 method getactive : int
2925 method getfirst : int
2926 method getqsearch : string
2927 method setqsearch : string -> unit
2928 method getpan : int
2929 end;;
2931 class virtual lvsourcebase = object
2932 val mutable m_active = 0
2933 val mutable m_first = 0
2934 val mutable m_qsearch = ""
2935 val mutable m_pan = 0
2936 method getactive = m_active
2937 method getfirst = m_first
2938 method getqsearch = m_qsearch
2939 method getpan = m_pan
2940 method setqsearch s = m_qsearch <- s
2941 end;;
2943 let withoutlastutf8 s =
2944 let len = String.length s in
2945 if len = 0
2946 then s
2947 else
2948 let rec find pos =
2949 if pos = 0
2950 then pos
2951 else
2952 let b = Char.code s.[pos] in
2953 if b land 0b110000 = 0b11000000
2954 then find (pos-1)
2955 else pos-1
2957 let first =
2958 if Char.code s.[len-1] land 0x80 = 0
2959 then len-1
2960 else find (len-1)
2962 String.sub s 0 first;
2965 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2966 let enttext te =
2967 state.mode <- Textentry (te, onleave);
2968 state.text <- "";
2969 enttext ();
2970 G.postRedisplay "textentrykeyboard enttext";
2972 let histaction cmd =
2973 match opthist with
2974 | None -> ()
2975 | Some (action, _) ->
2976 state.mode <- Textentry (
2977 (c, action cmd, opthist, onkey, ondone), onleave
2979 G.postRedisplay "textentry histaction"
2981 match key with
2982 | 0xff08 -> (* backspace *)
2983 let s = withoutlastutf8 text in
2984 let len = String.length s in
2985 if len = 0
2986 then (
2987 onleave Cancel;
2988 G.postRedisplay "textentrykeyboard after cancel";
2990 else (
2991 enttext (c, s, opthist, onkey, ondone)
2994 | 0xff0d ->
2995 ondone text;
2996 onleave Confirm;
2997 G.postRedisplay "textentrykeyboard after confirm"
2999 | 0xff52 -> histaction HCprev
3000 | 0xff54 -> histaction HCnext
3001 | 0xff50 -> histaction HCfirst
3002 | 0xff57 -> histaction HClast
3004 | 0xff1b -> (* escape*)
3005 if String.length text = 0
3006 then (
3007 begin match opthist with
3008 | None -> ()
3009 | Some (_, onhistcancel) -> onhistcancel ()
3010 end;
3011 onleave Cancel;
3012 state.text <- "";
3013 G.postRedisplay "textentrykeyboard after cancel2"
3015 else (
3016 enttext (c, "", opthist, onkey, ondone)
3019 | 0xff9f | 0xffff -> () (* delete *)
3021 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3022 begin match onkey text key with
3023 | TEdone text ->
3024 ondone text;
3025 onleave Confirm;
3026 G.postRedisplay "textentrykeyboard after confirm2";
3028 | TEcont text ->
3029 enttext (c, text, opthist, onkey, ondone);
3031 | TEstop ->
3032 onleave Cancel;
3033 G.postRedisplay "textentrykeyboard after cancel3"
3035 | TEswitch te ->
3036 state.mode <- Textentry (te, onleave);
3037 G.postRedisplay "textentrykeyboard switch";
3038 end;
3040 | _ ->
3041 vlog "unhandled key %s" (Wsi.keyname key)
3044 let firstof first active =
3045 if first > active || abs (first - active) > fstate.maxrows - 1
3046 then max 0 (active - (fstate.maxrows/2))
3047 else first
3050 let calcfirst first active =
3051 if active > first
3052 then
3053 let rows = active - first in
3054 if rows > fstate.maxrows then active - fstate.maxrows else first
3055 else active
3058 let scrollph y maxy =
3059 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3060 let sh = float conf.winh /. sh in
3061 let sh = max sh (float conf.scrollh) in
3063 let percent =
3064 if y = state.maxy
3065 then 1.0
3066 else float y /. float maxy
3068 let position = (float conf.winh -. sh) *. percent in
3070 let position =
3071 if position +. sh > float conf.winh
3072 then float conf.winh -. sh
3073 else position
3075 position, sh;
3078 let coe s = (s :> uioh);;
3080 class listview ~(source:lvsource) ~trusted ~modehash =
3081 object (self)
3082 val m_pan = source#getpan
3083 val m_first = source#getfirst
3084 val m_active = source#getactive
3085 val m_qsearch = source#getqsearch
3086 val m_prev_uioh = state.uioh
3088 method private elemunder y =
3089 let n = y / (fstate.fontsize+1) in
3090 if m_first + n < source#getitemcount
3091 then (
3092 if source#hasaction (m_first + n)
3093 then Some (m_first + n)
3094 else None
3096 else None
3098 method display =
3099 Gl.enable `blend;
3100 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3101 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3102 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3103 GlDraw.color (1., 1., 1.);
3104 Gl.enable `texture_2d;
3105 let fs = fstate.fontsize in
3106 let nfs = fs + 1 in
3107 let ww = fstate.wwidth in
3108 let tabw = 30.0*.ww in
3109 let itemcount = source#getitemcount in
3110 let rec loop row =
3111 if (row - m_first) * nfs > conf.winh
3112 then ()
3113 else (
3114 if row >= 0 && row < itemcount
3115 then (
3116 let (s, level) = source#getitem row in
3117 let y = (row - m_first) * nfs in
3118 let x = 5.0 +. float (level + m_pan) *. ww in
3119 if row = m_active
3120 then (
3121 Gl.disable `texture_2d;
3122 GlDraw.polygon_mode `both `line;
3123 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3124 GlDraw.rect (1., float (y + 1))
3125 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3126 GlDraw.polygon_mode `both `fill;
3127 GlDraw.color (1., 1., 1.);
3128 Gl.enable `texture_2d;
3131 let drawtabularstring s =
3132 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3133 if trusted
3134 then
3135 let tabpos = try String.index s '\t' with Not_found -> -1 in
3136 if tabpos > 0
3137 then
3138 let len = String.length s - tabpos - 1 in
3139 let s1 = String.sub s 0 tabpos
3140 and s2 = String.sub s (tabpos + 1) len in
3141 let nx = drawstr x s1 in
3142 let sw = nx -. x in
3143 let x = x +. (max tabw sw) in
3144 drawstr x s2
3145 else
3146 drawstr x s
3147 else
3148 drawstr x s
3150 let _ = drawtabularstring s in
3151 loop (row+1)
3155 loop m_first;
3156 Gl.disable `blend;
3157 Gl.disable `texture_2d;
3159 method updownlevel incr =
3160 let len = source#getitemcount in
3161 let curlevel =
3162 if m_active >= 0 && m_active < len
3163 then snd (source#getitem m_active)
3164 else -1
3166 let rec flow i =
3167 if i = len then i-1 else if i = -1 then 0 else
3168 let _, l = source#getitem i in
3169 if l != curlevel then i else flow (i+incr)
3171 let active = flow m_active in
3172 let first = calcfirst m_first active in
3173 G.postRedisplay "outline updownlevel";
3174 {< m_active = active; m_first = first >}
3176 method private key1 key mask =
3177 let set1 active first qsearch =
3178 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3180 let search active pattern incr =
3181 let dosearch re =
3182 let rec loop n =
3183 if n >= 0 && n < source#getitemcount
3184 then (
3185 let s, _ = source#getitem n in
3187 (try ignore (Str.search_forward re s 0); true
3188 with Not_found -> false)
3189 then Some n
3190 else loop (n + incr)
3192 else None
3194 loop active
3197 let re = Str.regexp_case_fold pattern in
3198 dosearch re
3199 with Failure s ->
3200 state.text <- s;
3201 None
3203 let itemcount = source#getitemcount in
3204 let find start incr =
3205 let rec find i =
3206 if i = -1 || i = itemcount
3207 then -1
3208 else (
3209 if source#hasaction i
3210 then i
3211 else find (i + incr)
3214 find start
3216 let set active first =
3217 let first = bound first 0 (itemcount - fstate.maxrows) in
3218 state.text <- "";
3219 coe {< m_active = active; m_first = first >}
3221 let navigate incr =
3222 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3223 let active, first =
3224 let incr1 = if incr > 0 then 1 else -1 in
3225 if isvisible m_first m_active
3226 then
3227 let next =
3228 let next = m_active + incr in
3229 let next =
3230 if next < 0 || next >= itemcount
3231 then -1
3232 else find next incr1
3234 if next = -1 || abs (m_active - next) > fstate.maxrows
3235 then -1
3236 else next
3238 if next = -1
3239 then
3240 let first = m_first + incr in
3241 let first = bound first 0 (itemcount - 1) in
3242 let next =
3243 let next = m_active + incr in
3244 let next = bound next 0 (itemcount - 1) in
3245 find next ~-incr1
3247 let active = if next = -1 then m_active else next in
3248 active, first
3249 else
3250 let first = min next m_first in
3251 let first =
3252 if abs (next - first) > fstate.maxrows
3253 then first + incr
3254 else first
3256 next, first
3257 else
3258 let first = m_first + incr in
3259 let first = bound first 0 (itemcount - 1) in
3260 let active =
3261 let next = m_active + incr in
3262 let next = bound next 0 (itemcount - 1) in
3263 let next = find next incr1 in
3264 let active =
3265 if next = -1 || abs (m_active - first) > fstate.maxrows
3266 then (
3267 let active = if m_active = -1 then next else m_active in
3268 active
3270 else next
3272 if isvisible first active
3273 then active
3274 else -1
3276 active, first
3278 G.postRedisplay "listview navigate";
3279 set active first;
3281 match key with
3282 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3283 let incr = if key = 0x72 then -1 else 1 in
3284 let active, first =
3285 match search (m_active + incr) m_qsearch incr with
3286 | None ->
3287 state.text <- m_qsearch ^ " [not found]";
3288 m_active, m_first
3289 | Some active ->
3290 state.text <- m_qsearch;
3291 active, firstof m_first active
3293 G.postRedisplay "listview ctrl-r/s";
3294 set1 active first m_qsearch;
3296 | 0xff08 -> (* backspace *)
3297 if String.length m_qsearch = 0
3298 then coe self
3299 else (
3300 let qsearch = withoutlastutf8 m_qsearch in
3301 let len = String.length qsearch in
3302 if len = 0
3303 then (
3304 state.text <- "";
3305 G.postRedisplay "listview empty qsearch";
3306 set1 m_active m_first "";
3308 else
3309 let active, first =
3310 match search m_active qsearch ~-1 with
3311 | None ->
3312 state.text <- qsearch ^ " [not found]";
3313 m_active, m_first
3314 | Some active ->
3315 state.text <- qsearch;
3316 active, firstof m_first active
3318 G.postRedisplay "listview backspace qsearch";
3319 set1 active first qsearch
3322 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3323 let pattern = m_qsearch ^ Wsi.toutf8 key in
3324 let active, first =
3325 match search m_active pattern 1 with
3326 | None ->
3327 state.text <- pattern ^ " [not found]";
3328 m_active, m_first
3329 | Some active ->
3330 state.text <- pattern;
3331 active, firstof m_first active
3333 G.postRedisplay "listview qsearch add";
3334 set1 active first pattern;
3336 | 0xff1b -> (* escape *)
3337 state.text <- "";
3338 if String.length m_qsearch = 0
3339 then (
3340 G.postRedisplay "list view escape";
3341 begin
3342 match
3343 source#exit (coe self) true m_active m_first m_pan m_qsearch
3344 with
3345 | None -> m_prev_uioh
3346 | Some uioh -> uioh
3349 else (
3350 G.postRedisplay "list view kill qsearch";
3351 source#setqsearch "";
3352 coe {< m_qsearch = "" >}
3355 | 0xff0d -> (* return *)
3356 state.text <- "";
3357 let self = {< m_qsearch = "" >} in
3358 source#setqsearch "";
3359 let opt =
3360 G.postRedisplay "listview enter";
3361 if m_active >= 0 && m_active < source#getitemcount
3362 then (
3363 source#exit (coe self) false m_active m_first m_pan "";
3365 else (
3366 source#exit (coe self) true m_active m_first m_pan "";
3369 begin match opt with
3370 | None -> m_prev_uioh
3371 | Some uioh -> uioh
3374 | 0xff9f | 0xffff -> (* delete *)
3375 coe self
3377 | 0xff52 -> navigate ~-1 (* up *)
3378 | 0xff54 -> navigate 1 (* down *)
3379 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3380 | 0xff56 -> navigate fstate.maxrows (* next *)
3382 | 0xff53 -> (* right *)
3383 state.text <- "";
3384 G.postRedisplay "listview right";
3385 coe {< m_pan = m_pan - 1 >}
3387 | 0xff51 -> (* left *)
3388 state.text <- "";
3389 G.postRedisplay "listview left";
3390 coe {< m_pan = m_pan + 1 >}
3392 | 0xff50 -> (* home *)
3393 let active = find 0 1 in
3394 G.postRedisplay "listview home";
3395 set active 0;
3397 | 0xff57 -> (* end *)
3398 let first = max 0 (itemcount - fstate.maxrows) in
3399 let active = find (itemcount - 1) ~-1 in
3400 G.postRedisplay "listview end";
3401 set active first;
3403 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3404 coe self
3406 | _ ->
3407 dolog "listview unknown key %#x" key; coe self
3409 method key key mask =
3410 match state.mode with
3411 | Textentry te -> textentrykeyboard key mask te; coe self
3412 | _ -> self#key1 key mask
3414 method button button down x y _ =
3415 let opt =
3416 match button with
3417 | 1 when x > conf.winw - conf.scrollbw ->
3418 G.postRedisplay "listview scroll";
3419 if down
3420 then
3421 let _, position, sh = self#scrollph in
3422 if y > truncate position && y < truncate (position +. sh)
3423 then (
3424 state.mstate <- Mscrolly;
3425 Some (coe self)
3427 else
3428 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3429 let first = truncate (s *. float source#getitemcount) in
3430 let first = min source#getitemcount first in
3431 Some (coe {< m_first = first; m_active = first >})
3432 else (
3433 state.mstate <- Mnone;
3434 Some (coe self);
3436 | 1 when not down ->
3437 begin match self#elemunder y with
3438 | Some n ->
3439 G.postRedisplay "listview click";
3440 source#exit
3441 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3442 | _ ->
3443 Some (coe self)
3445 | n when (n == 4 || n == 5) && not down ->
3446 let len = source#getitemcount in
3447 let first =
3448 if n = 5 && m_first + fstate.maxrows >= len
3449 then
3450 m_first
3451 else
3452 let first = m_first + (if n == 4 then -1 else 1) in
3453 bound first 0 (len - 1)
3455 G.postRedisplay "listview wheel";
3456 Some (coe {< m_first = first >})
3457 | _ ->
3458 Some (coe self)
3460 match opt with
3461 | None -> m_prev_uioh
3462 | Some uioh -> uioh
3464 method motion _ y =
3465 match state.mstate with
3466 | Mscrolly ->
3467 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3468 let first = truncate (s *. float source#getitemcount) in
3469 let first = min source#getitemcount first in
3470 G.postRedisplay "listview motion";
3471 coe {< m_first = first; m_active = first >}
3472 | _ -> coe self
3474 method pmotion x y =
3475 if x < conf.winw - conf.scrollbw
3476 then
3477 let n =
3478 match self#elemunder y with
3479 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3480 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3482 let o =
3483 if n != m_active
3484 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3485 else self
3487 coe o
3488 else (
3489 Wsi.setcursor Wsi.CURSOR_INHERIT;
3490 coe self
3493 method infochanged _ = ()
3495 method scrollpw = (0, 0.0, 0.0)
3496 method scrollph =
3497 let nfs = fstate.fontsize + 1 in
3498 let y = m_first * nfs in
3499 let itemcount = source#getitemcount in
3500 let maxi = max 0 (itemcount - fstate.maxrows) in
3501 let maxy = maxi * nfs in
3502 let p, h = scrollph y maxy in
3503 conf.scrollbw, p, h
3505 method modehash = modehash
3506 end;;
3508 class outlinelistview ~source =
3509 object (self)
3510 inherit listview
3511 ~source:(source :> lvsource)
3512 ~trusted:false
3513 ~modehash:(findkeyhash conf "outline")
3514 as super
3516 method key key mask =
3517 let calcfirst first active =
3518 if active > first
3519 then
3520 let rows = active - first in
3521 if rows > fstate.maxrows then active - fstate.maxrows else first
3522 else active
3524 let navigate incr =
3525 let active = m_active + incr in
3526 let active = bound active 0 (source#getitemcount - 1) in
3527 let first = calcfirst m_first active in
3528 G.postRedisplay "outline navigate";
3529 coe {< m_active = active; m_first = first >}
3531 let ctrl = Wsi.withctrl mask in
3532 match key with
3533 | 110 when ctrl -> (* ctrl-n *)
3534 source#narrow m_qsearch;
3535 G.postRedisplay "outline ctrl-n";
3536 coe {< m_first = 0; m_active = 0 >}
3538 | 117 when ctrl -> (* ctrl-u *)
3539 source#denarrow;
3540 G.postRedisplay "outline ctrl-u";
3541 state.text <- "";
3542 coe {< m_first = 0; m_active = 0 >}
3544 | 108 when ctrl -> (* ctrl-l *)
3545 let first = m_active - (fstate.maxrows / 2) in
3546 G.postRedisplay "outline ctrl-l";
3547 coe {< m_first = first >}
3549 | 0xff9f | 0xffff -> (* delete *)
3550 source#remove m_active;
3551 G.postRedisplay "outline delete";
3552 let active = max 0 (m_active-1) in
3553 coe {< m_first = firstof m_first active;
3554 m_active = active >}
3556 | 0xff52 -> navigate ~-1 (* up *)
3557 | 0xff54 -> navigate 1 (* down *)
3558 | 0xff55 -> (* prior *)
3559 navigate ~-(fstate.maxrows)
3560 | 0xff56 -> (* next *)
3561 navigate fstate.maxrows
3563 | 0xff53 -> (* [ctrl-]right *)
3564 let o =
3565 if ctrl
3566 then (
3567 G.postRedisplay "outline ctrl right";
3568 {< m_pan = m_pan + 1 >}
3570 else self#updownlevel 1
3572 coe o
3574 | 0xff51 -> (* [ctrl-]left *)
3575 let o =
3576 if ctrl
3577 then (
3578 G.postRedisplay "outline ctrl left";
3579 {< m_pan = m_pan - 1 >}
3581 else self#updownlevel ~-1
3583 coe o
3585 | 0xff50 -> (* home *)
3586 G.postRedisplay "outline home";
3587 coe {< m_first = 0; m_active = 0 >}
3589 | 0xff57 -> (* end *)
3590 let active = source#getitemcount - 1 in
3591 let first = max 0 (active - fstate.maxrows) in
3592 G.postRedisplay "outline end";
3593 coe {< m_active = active; m_first = first >}
3595 | _ -> super#key key mask
3598 let outlinesource usebookmarks =
3599 let empty = [||] in
3600 (object
3601 inherit lvsourcebase
3602 val mutable m_items = empty
3603 val mutable m_orig_items = empty
3604 val mutable m_prev_items = empty
3605 val mutable m_narrow_pattern = ""
3606 val mutable m_hadremovals = false
3608 method getitemcount =
3609 Array.length m_items + (if m_hadremovals then 1 else 0)
3611 method getitem n =
3612 if n == Array.length m_items && m_hadremovals
3613 then
3614 ("[Confirm removal]", 0)
3615 else
3616 let s, n, _ = m_items.(n) in
3617 (s, n)
3619 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3620 ignore (uioh, first, qsearch);
3621 let confrimremoval = m_hadremovals && active = Array.length m_items in
3622 let items =
3623 if String.length m_narrow_pattern = 0
3624 then m_orig_items
3625 else m_items
3627 if not cancel
3628 then (
3629 if not confrimremoval
3630 then(
3631 let _, _, anchor = m_items.(active) in
3632 gotoanchor anchor;
3633 m_items <- items;
3635 else (
3636 state.bookmarks <- Array.to_list m_items;
3637 m_orig_items <- m_items;
3640 else m_items <- items;
3641 m_pan <- pan;
3642 None
3644 method hasaction _ = true
3646 method greetmsg =
3647 if Array.length m_items != Array.length m_orig_items
3648 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3649 else ""
3651 method narrow pattern =
3652 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3653 match reopt with
3654 | None -> ()
3655 | Some re ->
3656 let rec loop accu n =
3657 if n = -1
3658 then (
3659 m_narrow_pattern <- pattern;
3660 m_items <- Array.of_list accu
3662 else
3663 let (s, _, _) as o = m_items.(n) in
3664 let accu =
3665 if (try ignore (Str.search_forward re s 0); true
3666 with Not_found -> false)
3667 then o :: accu
3668 else accu
3670 loop accu (n-1)
3672 loop [] (Array.length m_items - 1)
3674 method denarrow =
3675 m_orig_items <- (
3676 if usebookmarks
3677 then Array.of_list state.bookmarks
3678 else state.outlines
3680 m_items <- m_orig_items
3682 method remove m =
3683 if usebookmarks
3684 then
3685 if m >= 0 && m < Array.length m_items
3686 then (
3687 m_hadremovals <- true;
3688 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3689 let n = if n >= m then n+1 else n in
3690 m_items.(n)
3694 method reset anchor items =
3695 m_hadremovals <- false;
3696 if m_orig_items == empty || m_prev_items != items
3697 then (
3698 m_orig_items <- items;
3699 if String.length m_narrow_pattern = 0
3700 then m_items <- items;
3702 m_prev_items <- items;
3703 let rely = getanchory anchor in
3704 let active =
3705 let rec loop n best bestd =
3706 if n = Array.length m_items
3707 then best
3708 else
3709 let (_, _, anchor) = m_items.(n) in
3710 let orely = getanchory anchor in
3711 let d = abs (orely - rely) in
3712 if d < bestd
3713 then loop (n+1) n d
3714 else loop (n+1) best bestd
3716 loop 0 ~-1 max_int
3718 m_active <- active;
3719 m_first <- firstof m_first active
3720 end)
3723 let enterselector usebookmarks =
3724 let source = outlinesource usebookmarks in
3725 fun errmsg ->
3726 let outlines =
3727 if usebookmarks
3728 then Array.of_list state.bookmarks
3729 else state.outlines
3731 if Array.length outlines = 0
3732 then (
3733 showtext ' ' errmsg;
3735 else (
3736 state.text <- source#greetmsg;
3737 Wsi.setcursor Wsi.CURSOR_INHERIT;
3738 let anchor = getanchor () in
3739 source#reset anchor outlines;
3740 state.uioh <- coe (new outlinelistview ~source);
3741 G.postRedisplay "enter selector";
3745 let enteroutlinemode =
3746 let f = enterselector false in
3747 fun ()-> f "Document has no outline";
3750 let enterbookmarkmode =
3751 let f = enterselector true in
3752 fun () -> f "Document has no bookmarks (yet)";
3755 let color_of_string s =
3756 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3757 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3761 let color_to_string (r, g, b) =
3762 let r = truncate (r *. 256.0)
3763 and g = truncate (g *. 256.0)
3764 and b = truncate (b *. 256.0) in
3765 Printf.sprintf "%d/%d/%d" r g b
3768 let irect_of_string s =
3769 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3772 let irect_to_string (x0,y0,x1,y1) =
3773 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3776 let makecheckers () =
3777 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3778 following to say:
3779 converted by Issac Trotts. July 25, 2002 *)
3780 let image_height = 64
3781 and image_width = 64 in
3783 let make_image () =
3784 let image =
3785 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3787 for i = 0 to image_width - 1 do
3788 for j = 0 to image_height - 1 do
3789 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3790 (if (i land 8 ) lxor (j land 8) = 0
3791 then [|255;255;255|] else [|200;200;200|])
3792 done
3793 done;
3794 image
3796 let image = make_image () in
3797 let id = GlTex.gen_texture () in
3798 GlTex.bind_texture `texture_2d id;
3799 GlPix.store (`unpack_alignment 1);
3800 GlTex.image2d image;
3801 List.iter (GlTex.parameter ~target:`texture_2d)
3802 [ `wrap_s `repeat;
3803 `wrap_t `repeat;
3804 `mag_filter `nearest;
3805 `min_filter `nearest ];
3809 let setcheckers enabled =
3810 match state.texid with
3811 | None ->
3812 if enabled then state.texid <- Some (makecheckers ())
3814 | Some texid ->
3815 if not enabled
3816 then (
3817 GlTex.delete_texture texid;
3818 state.texid <- None;
3822 let int_of_string_with_suffix s =
3823 let l = String.length s in
3824 let s1, shift =
3825 if l > 1
3826 then
3827 let suffix = Char.lowercase s.[l-1] in
3828 match suffix with
3829 | 'k' -> String.sub s 0 (l-1), 10
3830 | 'm' -> String.sub s 0 (l-1), 20
3831 | 'g' -> String.sub s 0 (l-1), 30
3832 | _ -> s, 0
3833 else s, 0
3835 let n = int_of_string s1 in
3836 let m = n lsl shift in
3837 if m < 0 || m < n
3838 then raise (Failure "value too large")
3839 else m
3842 let string_with_suffix_of_int n =
3843 if n = 0
3844 then "0"
3845 else
3846 let n, s =
3847 if n = 0
3848 then 0, ""
3849 else (
3850 if n land ((1 lsl 20) - 1) = 0
3851 then n lsr 20, "M"
3852 else (
3853 if n land ((1 lsl 10) - 1) = 0
3854 then n lsr 10, "K"
3855 else n, ""
3859 let rec loop s n =
3860 let h = n mod 1000 in
3861 let n = n / 1000 in
3862 if n = 0
3863 then string_of_int h ^ s
3864 else (
3865 let s = Printf.sprintf "_%03d%s" h s in
3866 loop s n
3869 loop "" n ^ s;
3872 let defghyllscroll = (40, 8, 32);;
3873 let ghyllscroll_of_string s =
3874 let (n, a, b) as nab =
3875 if s = "default"
3876 then defghyllscroll
3877 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3879 if n <= a || n <= b || a >= b
3880 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3881 nab;
3884 let ghyllscroll_to_string ((n, a, b) as nab) =
3885 if nab = defghyllscroll
3886 then "default"
3887 else Printf.sprintf "%d,%d,%d" n a b;
3890 let describe_location () =
3891 let f (fn, _) l =
3892 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3894 let fn, ln = List.fold_left f (-1, -1) state.layout in
3895 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3896 let percent =
3897 if maxy <= 0
3898 then 100.
3899 else (100. *. (float state.y /. float maxy))
3901 if fn = ln
3902 then
3903 Printf.sprintf "page %d of %d [%.2f%%]"
3904 (fn+1) state.pagecount percent
3905 else
3906 Printf.sprintf
3907 "pages %d-%d of %d [%.2f%%]"
3908 (fn+1) (ln+1) state.pagecount percent
3911 let enterinfomode =
3912 let btos b = if b then "\xe2\x88\x9a" else "" in
3913 let showextended = ref false in
3914 let leave mode = function
3915 | Confirm -> state.mode <- mode
3916 | Cancel -> state.mode <- mode in
3917 let src =
3918 (object
3919 val mutable m_first_time = true
3920 val mutable m_l = []
3921 val mutable m_a = [||]
3922 val mutable m_prev_uioh = nouioh
3923 val mutable m_prev_mode = View
3925 inherit lvsourcebase
3927 method reset prev_mode prev_uioh =
3928 m_a <- Array.of_list (List.rev m_l);
3929 m_l <- [];
3930 m_prev_mode <- prev_mode;
3931 m_prev_uioh <- prev_uioh;
3932 if m_first_time
3933 then (
3934 let rec loop n =
3935 if n >= Array.length m_a
3936 then ()
3937 else
3938 match m_a.(n) with
3939 | _, _, _, Action _ -> m_active <- n
3940 | _ -> loop (n+1)
3942 loop 0;
3943 m_first_time <- false;
3946 method int name get set =
3947 m_l <-
3948 (name, `int get, 1, Action (
3949 fun u ->
3950 let ondone s =
3951 try set (int_of_string s)
3952 with exn ->
3953 state.text <- Printf.sprintf "bad integer `%s': %s"
3954 s (Printexc.to_string exn)
3956 state.text <- "";
3957 let te = name ^ ": ", "", None, intentry, ondone in
3958 state.mode <- Textentry (te, leave m_prev_mode);
3960 )) :: m_l
3962 method int_with_suffix name get set =
3963 m_l <-
3964 (name, `intws get, 1, Action (
3965 fun u ->
3966 let ondone s =
3967 try set (int_of_string_with_suffix s)
3968 with exn ->
3969 state.text <- Printf.sprintf "bad integer `%s': %s"
3970 s (Printexc.to_string exn)
3972 state.text <- "";
3973 let te =
3974 name ^ ": ", "", None, intentry_with_suffix, ondone
3976 state.mode <- Textentry (te, leave m_prev_mode);
3978 )) :: m_l
3980 method bool ?(offset=1) ?(btos=btos) name get set =
3981 m_l <-
3982 (name, `bool (btos, get), offset, Action (
3983 fun u ->
3984 let v = get () in
3985 set (not v);
3987 )) :: m_l
3989 method color name get set =
3990 m_l <-
3991 (name, `color get, 1, Action (
3992 fun u ->
3993 let invalid = (nan, nan, nan) in
3994 let ondone s =
3995 let c =
3996 try color_of_string s
3997 with exn ->
3998 state.text <- Printf.sprintf "bad color `%s': %s"
3999 s (Printexc.to_string exn);
4000 invalid
4002 if c <> invalid
4003 then set c;
4005 let te = name ^ ": ", "", None, textentry, ondone in
4006 state.text <- color_to_string (get ());
4007 state.mode <- Textentry (te, leave m_prev_mode);
4009 )) :: m_l
4011 method string name get set =
4012 m_l <-
4013 (name, `string get, 1, Action (
4014 fun u ->
4015 let ondone s = set s in
4016 let te = name ^ ": ", "", None, textentry, ondone in
4017 state.mode <- Textentry (te, leave m_prev_mode);
4019 )) :: m_l
4021 method colorspace name get set =
4022 m_l <-
4023 (name, `string get, 1, Action (
4024 fun _ ->
4025 let source =
4026 let vals = [| "rgb"; "bgr"; "gray" |] in
4027 (object
4028 inherit lvsourcebase
4030 initializer
4031 m_active <- int_of_colorspace conf.colorspace;
4032 m_first <- 0;
4034 method getitemcount = Array.length vals
4035 method getitem n = (vals.(n), 0)
4036 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4037 ignore (uioh, first, pan, qsearch);
4038 if not cancel then set active;
4039 None
4040 method hasaction _ = true
4041 end)
4043 state.text <- "";
4044 let modehash = findkeyhash conf "info" in
4045 coe (new listview ~source ~trusted:true ~modehash)
4046 )) :: m_l
4048 method caption s offset =
4049 m_l <- (s, `empty, offset, Noaction) :: m_l
4051 method caption2 s f offset =
4052 m_l <- (s, `string f, offset, Noaction) :: m_l
4054 method getitemcount = Array.length m_a
4056 method getitem n =
4057 let tostr = function
4058 | `int f -> string_of_int (f ())
4059 | `intws f -> string_with_suffix_of_int (f ())
4060 | `string f -> f ()
4061 | `color f -> color_to_string (f ())
4062 | `bool (btos, f) -> btos (f ())
4063 | `empty -> ""
4065 let name, t, offset, _ = m_a.(n) in
4066 ((let s = tostr t in
4067 if String.length s > 0
4068 then Printf.sprintf "%s\t%s" name s
4069 else name),
4070 offset)
4072 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4073 let uiohopt =
4074 if not cancel
4075 then (
4076 m_qsearch <- qsearch;
4077 let uioh =
4078 match m_a.(active) with
4079 | _, _, _, Action f -> f uioh
4080 | _ -> uioh
4082 Some uioh
4084 else None
4086 m_active <- active;
4087 m_first <- first;
4088 m_pan <- pan;
4089 uiohopt
4091 method hasaction n =
4092 match m_a.(n) with
4093 | _, _, _, Action _ -> true
4094 | _ -> false
4095 end)
4097 let rec fillsrc prevmode prevuioh =
4098 let sep () = src#caption "" 0 in
4099 let colorp name get set =
4100 src#string name
4101 (fun () -> color_to_string (get ()))
4102 (fun v ->
4104 let c = color_of_string v in
4105 set c
4106 with exn ->
4107 state.text <- Printf.sprintf "bad color `%s': %s"
4108 v (Printexc.to_string exn);
4111 let oldmode = state.mode in
4112 let birdseye = isbirdseye state.mode in
4114 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4116 src#bool "presentation mode"
4117 (fun () -> conf.presentation)
4118 (fun v ->
4119 conf.presentation <- v;
4120 state.anchor <- getanchor ();
4121 represent ());
4123 src#bool "ignore case in searches"
4124 (fun () -> conf.icase)
4125 (fun v -> conf.icase <- v);
4127 src#bool "preload"
4128 (fun () -> conf.preload)
4129 (fun v -> conf.preload <- v);
4131 src#bool "highlight links"
4132 (fun () -> conf.hlinks)
4133 (fun v -> conf.hlinks <- v);
4135 src#bool "under info"
4136 (fun () -> conf.underinfo)
4137 (fun v -> conf.underinfo <- v);
4139 src#bool "persistent bookmarks"
4140 (fun () -> conf.savebmarks)
4141 (fun v -> conf.savebmarks <- v);
4143 src#bool "proportional display"
4144 (fun () -> conf.proportional)
4145 (fun v -> reqlayout conf.angle v);
4147 src#bool "trim margins"
4148 (fun () -> conf.trimmargins)
4149 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4151 src#bool "persistent location"
4152 (fun () -> conf.jumpback)
4153 (fun v -> conf.jumpback <- v);
4155 sep ();
4156 src#int "inter-page space"
4157 (fun () -> conf.interpagespace)
4158 (fun n ->
4159 conf.interpagespace <- n;
4160 docolumns conf.columns;
4161 let pageno, py =
4162 match state.layout with
4163 | [] -> 0, 0
4164 | l :: _ ->
4165 l.pageno, l.pagey
4167 state.maxy <- calcheight ();
4168 let y = getpagey pageno in
4169 gotoy (y + py)
4172 src#int "page bias"
4173 (fun () -> conf.pagebias)
4174 (fun v -> conf.pagebias <- v);
4176 src#int "scroll step"
4177 (fun () -> conf.scrollstep)
4178 (fun n -> conf.scrollstep <- n);
4180 src#int "auto scroll step"
4181 (fun () ->
4182 match state.autoscroll with
4183 | Some step -> step
4184 | _ -> conf.autoscrollstep)
4185 (fun n ->
4186 if state.autoscroll <> None
4187 then state.autoscroll <- Some n;
4188 conf.autoscrollstep <- n);
4190 src#int "zoom"
4191 (fun () -> truncate (conf.zoom *. 100.))
4192 (fun v -> setzoom ((float v) /. 100.));
4194 src#int "rotation"
4195 (fun () -> conf.angle)
4196 (fun v -> reqlayout v conf.proportional);
4198 src#int "scroll bar width"
4199 (fun () -> state.scrollw)
4200 (fun v ->
4201 state.scrollw <- v;
4202 conf.scrollbw <- v;
4203 reshape conf.winw conf.winh;
4206 src#int "scroll handle height"
4207 (fun () -> conf.scrollh)
4208 (fun v -> conf.scrollh <- v;);
4210 src#int "thumbnail width"
4211 (fun () -> conf.thumbw)
4212 (fun v ->
4213 conf.thumbw <- min 4096 v;
4214 match oldmode with
4215 | Birdseye beye ->
4216 leavebirdseye beye false;
4217 enterbirdseye ()
4218 | _ -> ()
4221 let mode = state.mode in
4222 src#string "columns"
4223 (fun () ->
4224 match conf.columns with
4225 | Csingle -> "1"
4226 | Cmulti (multi, _) -> multicolumns_to_string multi
4227 | Csplit (count, _) -> "-" ^ string_of_int count
4229 (fun v ->
4230 let n, a, b = multicolumns_of_string v in
4231 setcolumns mode n a b);
4233 sep ();
4234 src#caption "Presentation mode" 0;
4235 src#bool "scrollbar visible"
4236 (fun () -> conf.scrollbarinpm)
4237 (fun v ->
4238 if v != conf.scrollbarinpm
4239 then (
4240 conf.scrollbarinpm <- v;
4241 if conf.presentation
4242 then (
4243 state.scrollw <- if v then conf.scrollbw else 0;
4244 reshape conf.winw conf.winh;
4249 sep ();
4250 src#caption "Pixmap cache" 0;
4251 src#int_with_suffix "size (advisory)"
4252 (fun () -> conf.memlimit)
4253 (fun v -> conf.memlimit <- v);
4255 src#caption2 "used"
4256 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4257 (string_with_suffix_of_int state.memused)
4258 (Hashtbl.length state.tilemap)) 1;
4260 sep ();
4261 src#caption "Layout" 0;
4262 src#caption2 "Dimension"
4263 (fun () ->
4264 Printf.sprintf "%dx%d (virtual %dx%d)"
4265 conf.winw conf.winh
4266 state.w state.maxy)
4268 if conf.debug
4269 then
4270 src#caption2 "Position" (fun () ->
4271 Printf.sprintf "%dx%d" state.x state.y
4273 else
4274 src#caption2 "Visible" (fun () -> describe_location ()) 1
4277 sep ();
4278 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4279 "Save these parameters as global defaults at exit"
4280 (fun () -> conf.bedefault)
4281 (fun v -> conf.bedefault <- v)
4284 sep ();
4285 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4286 src#bool ~offset:0 ~btos "Extended parameters"
4287 (fun () -> !showextended)
4288 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4289 if !showextended
4290 then (
4291 src#bool "checkers"
4292 (fun () -> conf.checkers)
4293 (fun v -> conf.checkers <- v; setcheckers v);
4294 src#bool "update cursor"
4295 (fun () -> conf.updatecurs)
4296 (fun v -> conf.updatecurs <- v);
4297 src#bool "verbose"
4298 (fun () -> conf.verbose)
4299 (fun v -> conf.verbose <- v);
4300 src#bool "invert colors"
4301 (fun () -> conf.invert)
4302 (fun v -> conf.invert <- v);
4303 src#bool "max fit"
4304 (fun () -> conf.maxhfit)
4305 (fun v -> conf.maxhfit <- v);
4306 src#bool "redirect stderr"
4307 (fun () -> conf.redirectstderr)
4308 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4309 src#string "uri launcher"
4310 (fun () -> conf.urilauncher)
4311 (fun v -> conf.urilauncher <- v);
4312 src#string "path launcher"
4313 (fun () -> conf.pathlauncher)
4314 (fun v -> conf.pathlauncher <- v);
4315 src#string "tile size"
4316 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4317 (fun v ->
4319 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4320 conf.tilew <- max 64 w;
4321 conf.tileh <- max 64 h;
4322 flushtiles ();
4323 with exn ->
4324 state.text <- Printf.sprintf "bad tile size `%s': %s"
4325 v (Printexc.to_string exn));
4326 src#int "texture count"
4327 (fun () -> conf.texcount)
4328 (fun v ->
4329 if realloctexts v
4330 then conf.texcount <- v
4331 else showtext '!' " Failed to set texture count please retry later"
4333 src#int "slice height"
4334 (fun () -> conf.sliceheight)
4335 (fun v ->
4336 conf.sliceheight <- v;
4337 wcmd "sliceh %d" conf.sliceheight;
4339 src#int "anti-aliasing level"
4340 (fun () -> conf.aalevel)
4341 (fun v ->
4342 conf.aalevel <- bound v 0 8;
4343 state.anchor <- getanchor ();
4344 opendoc state.path state.password;
4346 src#int "ui font size"
4347 (fun () -> fstate.fontsize)
4348 (fun v -> setfontsize (bound v 5 100));
4349 src#int "hint font size"
4350 (fun () -> conf.hfsize)
4351 (fun v -> conf.hfsize <- bound v 5 100);
4352 colorp "background color"
4353 (fun () -> conf.bgcolor)
4354 (fun v -> conf.bgcolor <- v);
4355 src#bool "crop hack"
4356 (fun () -> conf.crophack)
4357 (fun v -> conf.crophack <- v);
4358 src#string "trim fuzz"
4359 (fun () -> irect_to_string conf.trimfuzz)
4360 (fun v ->
4362 conf.trimfuzz <- irect_of_string v;
4363 if conf.trimmargins
4364 then settrim true conf.trimfuzz;
4365 with exn ->
4366 state.text <- Printf.sprintf "bad irect `%s': %s"
4367 v (Printexc.to_string exn)
4369 src#string "throttle"
4370 (fun () ->
4371 match conf.maxwait with
4372 | None -> "show place holder if page is not ready"
4373 | Some time ->
4374 if time = infinity
4375 then "wait for page to fully render"
4376 else
4377 "wait " ^ string_of_float time
4378 ^ " seconds before showing placeholder"
4380 (fun v ->
4382 let f = float_of_string v in
4383 if f <= 0.0
4384 then conf.maxwait <- None
4385 else conf.maxwait <- Some f
4386 with exn ->
4387 state.text <- Printf.sprintf "bad time `%s': %s"
4388 v (Printexc.to_string exn)
4390 src#string "ghyll scroll"
4391 (fun () ->
4392 match conf.ghyllscroll with
4393 | None -> ""
4394 | Some nab -> ghyllscroll_to_string nab
4396 (fun v ->
4398 let gs =
4399 if String.length v = 0
4400 then None
4401 else Some (ghyllscroll_of_string v)
4403 conf.ghyllscroll <- gs
4404 with exn ->
4405 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4406 v (Printexc.to_string exn)
4408 src#string "selection command"
4409 (fun () -> conf.selcmd)
4410 (fun v -> conf.selcmd <- v);
4411 src#colorspace "color space"
4412 (fun () -> colorspace_to_string conf.colorspace)
4413 (fun v ->
4414 conf.colorspace <- colorspace_of_int v;
4415 wcmd "cs %d" v;
4416 load state.layout;
4420 sep ();
4421 src#caption "Document" 0;
4422 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4423 src#caption2 "Pages"
4424 (fun () -> string_of_int state.pagecount) 1;
4425 src#caption2 "Dimensions"
4426 (fun () -> string_of_int (List.length state.pdims)) 1;
4427 if conf.trimmargins
4428 then (
4429 sep ();
4430 src#caption "Trimmed margins" 0;
4431 src#caption2 "Dimensions"
4432 (fun () -> string_of_int (List.length state.pdims)) 1;
4435 src#reset prevmode prevuioh;
4437 fun () ->
4438 state.text <- "";
4439 let prevmode = state.mode
4440 and prevuioh = state.uioh in
4441 fillsrc prevmode prevuioh;
4442 let source = (src :> lvsource) in
4443 let modehash = findkeyhash conf "info" in
4444 state.uioh <- coe (object (self)
4445 inherit listview ~source ~trusted:true ~modehash as super
4446 val mutable m_prevmemused = 0
4447 method infochanged = function
4448 | Memused ->
4449 if m_prevmemused != state.memused
4450 then (
4451 m_prevmemused <- state.memused;
4452 G.postRedisplay "memusedchanged";
4454 | Pdim -> G.postRedisplay "pdimchanged"
4455 | Docinfo -> fillsrc prevmode prevuioh
4457 method key key mask =
4458 if not (Wsi.withctrl mask)
4459 then
4460 match key with
4461 | 0xff51 -> coe (self#updownlevel ~-1)
4462 | 0xff53 -> coe (self#updownlevel 1)
4463 | _ -> super#key key mask
4464 else super#key key mask
4465 end);
4466 G.postRedisplay "info";
4469 let enterhelpmode =
4470 let source =
4471 (object
4472 inherit lvsourcebase
4473 method getitemcount = Array.length state.help
4474 method getitem n =
4475 let s, n, _ = state.help.(n) in
4476 (s, n)
4478 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4479 let optuioh =
4480 if not cancel
4481 then (
4482 m_qsearch <- qsearch;
4483 match state.help.(active) with
4484 | _, _, Action f -> Some (f uioh)
4485 | _ -> Some (uioh)
4487 else None
4489 m_active <- active;
4490 m_first <- first;
4491 m_pan <- pan;
4492 optuioh
4494 method hasaction n =
4495 match state.help.(n) with
4496 | _, _, Action _ -> true
4497 | _ -> false
4499 initializer
4500 m_active <- -1
4501 end)
4502 in fun () ->
4503 let modehash = findkeyhash conf "help" in
4504 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4505 G.postRedisplay "help";
4508 let entermsgsmode =
4509 let msgsource =
4510 let re = Str.regexp "[\r\n]" in
4511 (object
4512 inherit lvsourcebase
4513 val mutable m_items = [||]
4515 method getitemcount = 1 + Array.length m_items
4517 method getitem n =
4518 if n = 0
4519 then "[Clear]", 0
4520 else m_items.(n-1), 0
4522 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4523 ignore uioh;
4524 if not cancel
4525 then (
4526 if active = 0
4527 then Buffer.clear state.errmsgs;
4528 m_qsearch <- qsearch;
4530 m_active <- active;
4531 m_first <- first;
4532 m_pan <- pan;
4533 None
4535 method hasaction n =
4536 n = 0
4538 method reset =
4539 state.newerrmsgs <- false;
4540 let l = Str.split re (Buffer.contents state.errmsgs) in
4541 m_items <- Array.of_list l
4543 initializer
4544 m_active <- 0
4545 end)
4546 in fun () ->
4547 state.text <- "";
4548 msgsource#reset;
4549 let source = (msgsource :> lvsource) in
4550 let modehash = findkeyhash conf "listview" in
4551 state.uioh <- coe (object
4552 inherit listview ~source ~trusted:false ~modehash as super
4553 method display =
4554 if state.newerrmsgs
4555 then msgsource#reset;
4556 super#display
4557 end);
4558 G.postRedisplay "msgs";
4561 let quickbookmark ?title () =
4562 match state.layout with
4563 | [] -> ()
4564 | l :: _ ->
4565 let title =
4566 match title with
4567 | None ->
4568 let sec = Unix.gettimeofday () in
4569 let tm = Unix.localtime sec in
4570 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4571 (l.pageno+1)
4572 tm.Unix.tm_mday
4573 tm.Unix.tm_mon
4574 (tm.Unix.tm_year + 1900)
4575 tm.Unix.tm_hour
4576 tm.Unix.tm_min
4577 | Some title -> title
4579 state.bookmarks <-
4580 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4581 :: state.bookmarks
4584 let doreshape w h =
4585 state.fullscreen <- None;
4586 Wsi.reshape w h;
4589 let setautoscrollspeed step goingdown =
4590 let incr = max 1 ((abs step) / 2) in
4591 let incr = if goingdown then incr else -incr in
4592 let astep = step + incr in
4593 state.autoscroll <- Some astep;
4596 let gotounder = function
4597 | Ulinkgoto (pageno, top) ->
4598 if pageno >= 0
4599 then (
4600 addnav ();
4601 gotopage1 pageno top;
4604 | Ulinkuri s ->
4605 gotouri s
4607 | Uremote (filename, pageno) ->
4608 let path =
4609 if Sys.file_exists filename
4610 then filename
4611 else
4612 let dir = Filename.dirname state.path in
4613 let path = Filename.concat dir filename in
4614 if Sys.file_exists path
4615 then path
4616 else ""
4618 if String.length path > 0
4619 then (
4620 let anchor = getanchor () in
4621 let ranchor = state.path, state.password, anchor in
4622 state.anchor <- (pageno, 0.0);
4623 state.ranchors <- ranchor :: state.ranchors;
4624 opendoc path "";
4626 else showtext '!' ("Could not find " ^ filename)
4628 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4631 let canpan () =
4632 match conf.columns with
4633 | Csplit _ -> true
4634 | _ -> conf.zoom > 1.0
4637 let viewkeyboard key mask =
4638 let enttext te =
4639 let mode = state.mode in
4640 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4641 state.text <- "";
4642 enttext ();
4643 G.postRedisplay "view:enttext"
4645 let ctrl = Wsi.withctrl mask in
4646 match key with
4647 | 81 -> (* Q *)
4648 exit 0
4650 | 0xff63 -> (* insert *)
4651 if conf.angle mod 360 = 0
4652 then (
4653 state.mode <- LinkNav (Ltgendir 0);
4654 gotoy state.y;
4656 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4658 | 0xff1b | 113 -> (* escape / q *)
4659 begin match state.mstate with
4660 | Mzoomrect _ ->
4661 state.mstate <- Mnone;
4662 Wsi.setcursor Wsi.CURSOR_INHERIT;
4663 G.postRedisplay "kill zoom rect";
4664 | _ ->
4665 match state.ranchors with
4666 | [] -> raise Quit
4667 | (path, password, anchor) :: rest ->
4668 state.ranchors <- rest;
4669 state.anchor <- anchor;
4670 opendoc path password
4671 end;
4673 | 0xff08 -> (* backspace *)
4674 let y = getnav ~-1 in
4675 gotoy_and_clear_text y
4677 | 111 -> (* o *)
4678 enteroutlinemode ()
4680 | 117 -> (* u *)
4681 state.rects <- [];
4682 state.text <- "";
4683 G.postRedisplay "dehighlight";
4685 | 47 | 63 -> (* / ? *)
4686 let ondone isforw s =
4687 cbput state.hists.pat s;
4688 state.searchpattern <- s;
4689 search s isforw
4691 let s = String.create 1 in
4692 s.[0] <- Char.chr key;
4693 enttext (s, "", Some (onhist state.hists.pat),
4694 textentry, ondone (key = 47))
4696 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4697 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4698 setzoom (conf.zoom +. incr)
4700 | 43 | 0xffab -> (* + *)
4701 let ondone s =
4702 let n =
4703 try int_of_string s with exc ->
4704 state.text <- Printf.sprintf "bad integer `%s': %s"
4705 s (Printexc.to_string exc);
4706 max_int
4708 if n != max_int
4709 then (
4710 conf.pagebias <- n;
4711 state.text <- "page bias is now " ^ string_of_int n;
4714 enttext ("page bias: ", "", None, intentry, ondone)
4716 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4717 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4718 setzoom (max 0.01 (conf.zoom -. decr))
4720 | 45 | 0xffad -> (* - *)
4721 let ondone msg = state.text <- msg in
4722 enttext (
4723 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4724 optentry state.mode, ondone
4727 | 48 when ctrl -> (* ctrl-0 *)
4728 setzoom 1.0
4730 | 49 when ctrl -> (* 1 *)
4731 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4732 if zoom < 1.0
4733 then setzoom zoom
4735 | 0xffc6 -> (* f9 *)
4736 togglebirdseye ()
4738 | 57 when ctrl -> (* ctrl-9 *)
4739 togglebirdseye ()
4741 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4742 when not ctrl -> (* 0..9 *)
4743 let ondone s =
4744 let n =
4745 try int_of_string s with exc ->
4746 state.text <- Printf.sprintf "bad integer `%s': %s"
4747 s (Printexc.to_string exc);
4750 if n >= 0
4751 then (
4752 addnav ();
4753 cbput state.hists.pag (string_of_int n);
4754 gotopage1 (n + conf.pagebias - 1) 0;
4757 let pageentry text key =
4758 match Char.unsafe_chr key with
4759 | 'g' -> TEdone text
4760 | _ -> intentry text key
4762 let text = "x" in text.[0] <- Char.chr key;
4763 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4765 | 98 -> (* b *)
4766 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4767 reshape conf.winw conf.winh;
4769 | 108 -> (* l *)
4770 conf.hlinks <- not conf.hlinks;
4771 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4772 G.postRedisplay "toggle highlightlinks";
4774 | 70 -> (* F *)
4775 state.glinks <- true;
4776 let mode = state.mode in
4777 state.mode <- Textentry (
4778 (":", "", None, linknentry, linkndone (fun under ->
4779 addnav ();
4780 gotounder under
4782 ), fun _ ->
4783 state.glinks <- false;
4784 state.mode <- mode
4786 state.text <- "";
4787 G.postRedisplay "view:linkent(F)"
4789 | 121 -> (* y *)
4790 state.glinks <- true;
4791 let mode = state.mode in
4792 state.mode <- Textentry (
4793 (":", "", None, linknentry, linkndone (fun under ->
4794 match Ne.pipe () with
4795 | Ne.Exn exn ->
4796 showtext '!' (Printf.sprintf "pipe failed: %s"
4797 (Printexc.to_string exn));
4798 | Ne.Res (r, w) ->
4799 let popened =
4800 try popen conf.selcmd [r, 0; w, -1]; true
4801 with exn ->
4802 showtext '!'
4803 (Printf.sprintf "failed to execute %s: %s"
4804 conf.selcmd (Printexc.to_string exn));
4805 false
4807 let clo cap fd =
4808 Ne.clo fd (fun msg ->
4809 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4812 let s = undertext under in
4813 if popened
4814 then
4815 (try
4816 let l = String.length s in
4817 let n = Unix.write w s 0 l in
4818 if n != l
4819 then
4820 showtext '!'
4821 (Printf.sprintf
4822 "failed to write %d characters to sel pipe, wrote %d"
4825 with exn ->
4826 showtext '!'
4827 (Printf.sprintf "failed to write to sel pipe: %s"
4828 (Printexc.to_string exn)
4831 else dolog "%s" s;
4832 clo "pipe/r" r;
4833 clo "pipe/w" w;
4836 fun _ ->
4837 state.glinks <- false;
4838 state.mode <- mode
4840 state.text <- "";
4841 G.postRedisplay "view:linkent"
4843 | 97 -> (* a *)
4844 begin match state.autoscroll with
4845 | Some step ->
4846 conf.autoscrollstep <- step;
4847 state.autoscroll <- None
4848 | None ->
4849 if conf.autoscrollstep = 0
4850 then state.autoscroll <- Some 1
4851 else state.autoscroll <- Some conf.autoscrollstep
4854 | 112 when ctrl -> (* ctrl-p *)
4855 launchpath ()
4857 | 80 -> (* P *)
4858 conf.presentation <- not conf.presentation;
4859 if conf.presentation
4860 then (
4861 if not conf.scrollbarinpm
4862 then state.scrollw <- 0;
4864 else
4865 state.scrollw <- conf.scrollbw;
4867 showtext ' ' ("presentation mode " ^
4868 if conf.presentation then "on" else "off");
4869 state.anchor <- getanchor ();
4870 represent ()
4872 | 102 -> (* f *)
4873 begin match state.fullscreen with
4874 | None ->
4875 state.fullscreen <- Some (conf.winw, conf.winh);
4876 Wsi.fullscreen ()
4877 | Some (w, h) ->
4878 state.fullscreen <- None;
4879 doreshape w h
4882 | 103 -> (* g *)
4883 gotoy_and_clear_text 0
4885 | 71 -> (* G *)
4886 gotopage1 (state.pagecount - 1) 0
4888 | 112 | 78 -> (* p|N *)
4889 search state.searchpattern false
4891 | 110 | 0xffc0 -> (* n|F3 *)
4892 search state.searchpattern true
4894 | 116 -> (* t *)
4895 begin match state.layout with
4896 | [] -> ()
4897 | l :: _ ->
4898 gotoy_and_clear_text (getpagey l.pageno)
4901 | 32 -> (* ' ' *)
4902 begin match List.rev state.layout with
4903 | [] -> ()
4904 | l :: _ ->
4905 let pageno = min (l.pageno+1) (state.pagecount-1) in
4906 gotoy_and_clear_text (getpagey pageno)
4909 | 0xff9f | 0xffff -> (* delete *)
4910 begin match state.layout with
4911 | [] -> ()
4912 | l :: _ ->
4913 let pageno = max 0 (l.pageno-1) in
4914 gotoy_and_clear_text (getpagey pageno)
4917 | 61 -> (* = *)
4918 showtext ' ' (describe_location ());
4920 | 119 -> (* w *)
4921 begin match state.layout with
4922 | [] -> ()
4923 | l :: _ ->
4924 doreshape (l.pagew + state.scrollw) l.pageh;
4925 G.postRedisplay "w"
4928 | 39 -> (* ' *)
4929 enterbookmarkmode ()
4931 | 104 | 0xffbe -> (* h|F1 *)
4932 enterhelpmode ()
4934 | 105 -> (* i *)
4935 enterinfomode ()
4937 | 101 when conf.redirectstderr -> (* e *)
4938 entermsgsmode ()
4940 | 109 -> (* m *)
4941 let ondone s =
4942 match state.layout with
4943 | l :: _ ->
4944 state.bookmarks <-
4945 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4946 :: state.bookmarks
4947 | _ -> ()
4949 enttext ("bookmark: ", "", None, textentry, ondone)
4951 | 126 -> (* ~ *)
4952 quickbookmark ();
4953 showtext ' ' "Quick bookmark added";
4955 | 122 -> (* z *)
4956 begin match state.layout with
4957 | l :: _ ->
4958 let rect = getpdimrect l.pagedimno in
4959 let w, h =
4960 if conf.crophack
4961 then
4962 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4963 truncate (1.2 *. (rect.(3) -. rect.(0))))
4964 else
4965 (truncate (rect.(1) -. rect.(0)),
4966 truncate (rect.(3) -. rect.(0)))
4968 let w = truncate ((float w)*.conf.zoom)
4969 and h = truncate ((float h)*.conf.zoom) in
4970 if w != 0 && h != 0
4971 then (
4972 state.anchor <- getanchor ();
4973 doreshape (w + state.scrollw) (h + conf.interpagespace)
4975 G.postRedisplay "z";
4977 | [] -> ()
4980 | 50 when ctrl -> (* ctrl-2 *)
4981 let maxw = getmaxw () in
4982 if maxw > 0.0
4983 then setzoom (maxw /. float conf.winw)
4985 | 60 | 62 -> (* < > *)
4986 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4988 | 91 | 93 -> (* [ ] *)
4989 conf.colorscale <-
4990 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4992 G.postRedisplay "brightness";
4994 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4995 setzoom state.prevzoom
4997 | 107 | 0xff52 -> (* k up *)
4998 begin match state.autoscroll with
4999 | None ->
5000 begin match state.mode with
5001 | Birdseye beye -> upbirdseye 1 beye
5002 | _ ->
5003 if ctrl
5004 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5005 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5007 | Some n ->
5008 setautoscrollspeed n false
5011 | 106 | 0xff54 -> (* j down *)
5012 begin match state.autoscroll with
5013 | None ->
5014 begin match state.mode with
5015 | Birdseye beye -> downbirdseye 1 beye
5016 | _ ->
5017 if ctrl
5018 then gotoy_and_clear_text (clamp (conf.winh/2))
5019 else gotoy_and_clear_text (clamp conf.scrollstep)
5021 | Some n ->
5022 setautoscrollspeed n true
5025 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5026 if canpan ()
5027 then
5028 let dx =
5029 if ctrl
5030 then conf.winw / 2
5031 else 10
5033 let dx = if key = 0xff51 then dx else -dx in
5034 state.x <- state.x + dx;
5035 gotoy_and_clear_text state.y
5036 else (
5037 state.text <- "";
5038 G.postRedisplay "lef/right"
5041 | 0xff55 -> (* prior *)
5042 let y =
5043 if ctrl
5044 then
5045 match state.layout with
5046 | [] -> state.y
5047 | l :: _ -> state.y - l.pagey
5048 else
5049 clamp (-conf.winh)
5051 gotoghyll y
5053 | 0xff56 -> (* next *)
5054 let y =
5055 if ctrl
5056 then
5057 match List.rev state.layout with
5058 | [] -> state.y
5059 | l :: _ -> getpagey l.pageno
5060 else
5061 clamp conf.winh
5063 gotoghyll y
5065 | 0xff50 -> gotoghyll 0
5066 | 0xff57 -> gotoghyll (clamp state.maxy)
5067 | 0xff53 when Wsi.withalt mask ->
5068 gotoghyll (getnav ~-1)
5069 | 0xff51 when Wsi.withalt mask ->
5070 gotoghyll (getnav 1)
5072 | 114 -> (* r *)
5073 state.anchor <- getanchor ();
5074 opendoc state.path state.password
5076 | 118 when conf.debug -> (* v *)
5077 state.rects <- [];
5078 List.iter (fun l ->
5079 match getopaque l.pageno with
5080 | None -> ()
5081 | Some opaque ->
5082 let x0, y0, x1, y1 = pagebbox opaque in
5083 let a,b = float x0, float y0 in
5084 let c,d = float x1, float y0 in
5085 let e,f = float x1, float y1 in
5086 let h,j = float x0, float y1 in
5087 let rect = (a,b,c,d,e,f,h,j) in
5088 debugrect rect;
5089 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5090 ) state.layout;
5091 G.postRedisplay "v";
5093 | _ ->
5094 vlog "huh? %s" (Wsi.keyname key)
5097 let linknavkeyboard key mask linknav =
5098 let getpage pageno =
5099 let rec loop = function
5100 | [] -> None
5101 | l :: _ when l.pageno = pageno -> Some l
5102 | _ :: rest -> loop rest
5103 in loop state.layout
5105 let doexact (pageno, n) =
5106 match getopaque pageno, getpage pageno with
5107 | Some opaque, Some l ->
5108 if key = 0xff0d
5109 then
5110 let under = getlink opaque n in
5111 G.postRedisplay "link gotounder";
5112 gotounder under;
5113 state.mode <- View;
5114 else
5115 let opt, dir =
5116 match key with
5117 | 0xff50 -> (* home *)
5118 Some (findlink opaque LDfirst), -1
5120 | 0xff57 -> (* end *)
5121 Some (findlink opaque LDlast), 1
5123 | 0xff51 -> (* left *)
5124 Some (findlink opaque (LDleft n)), -1
5126 | 0xff53 -> (* right *)
5127 Some (findlink opaque (LDright n)), 1
5129 | 0xff52 -> (* up *)
5130 Some (findlink opaque (LDup n)), -1
5132 | 0xff54 -> (* down *)
5133 Some (findlink opaque (LDdown n)), 1
5135 | _ -> None, 0
5137 let pwl l dir =
5138 begin match findpwl l.pageno dir with
5139 | Pwlnotfound -> ()
5140 | Pwl pageno ->
5141 let notfound dir =
5142 state.mode <- LinkNav (Ltgendir dir);
5143 let y, h = getpageyh pageno in
5144 let y =
5145 if dir < 0
5146 then y + h - conf.winh
5147 else y
5149 gotoy y
5151 begin match getopaque pageno, getpage pageno with
5152 | Some opaque, Some _ ->
5153 let link =
5154 let ld = if dir > 0 then LDfirst else LDlast in
5155 findlink opaque ld
5157 begin match link with
5158 | Lfound m ->
5159 showlinktype (getlink opaque m);
5160 state.mode <- LinkNav (Ltexact (pageno, m));
5161 G.postRedisplay "linknav jpage";
5162 | _ -> notfound dir
5163 end;
5164 | _ -> notfound dir
5165 end;
5166 end;
5168 begin match opt with
5169 | Some Lnotfound -> pwl l dir;
5170 | Some (Lfound m) ->
5171 if m = n
5172 then pwl l dir
5173 else (
5174 let _, y0, _, y1 = getlinkrect opaque m in
5175 if y0 < l.pagey
5176 then gotopage1 l.pageno y0
5177 else (
5178 let d = fstate.fontsize + 1 in
5179 if y1 - l.pagey > l.pagevh - d
5180 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5181 else G.postRedisplay "linknav";
5183 showlinktype (getlink opaque m);
5184 state.mode <- LinkNav (Ltexact (l.pageno, m));
5187 | None -> viewkeyboard key mask
5188 end;
5189 | _ -> viewkeyboard key mask
5191 if key = 0xff63
5192 then (
5193 state.mode <- View;
5194 G.postRedisplay "leave linknav"
5196 else
5197 match linknav with
5198 | Ltgendir _ -> viewkeyboard key mask
5199 | Ltexact exact -> doexact exact
5202 let keyboard key mask =
5203 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5204 then wcmd "interrupt"
5205 else state.uioh <- state.uioh#key key mask
5208 let birdseyekeyboard key mask
5209 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5210 let incr =
5211 match conf.columns with
5212 | Csingle -> 1
5213 | Cmulti ((c, _, _), _) -> c
5214 | Csplit _ -> failwith "bird's eye split mode"
5216 match key with
5217 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5218 let y, h = getpageyh pageno in
5219 let top = (conf.winh - h) / 2 in
5220 gotoy (max 0 (y - top))
5221 | 0xff0d -> leavebirdseye beye false
5222 | 0xff1b -> leavebirdseye beye true (* escape *)
5223 | 0xff52 -> upbirdseye incr beye (* prior *)
5224 | 0xff54 -> downbirdseye incr beye (* next *)
5225 | 0xff51 -> upbirdseye 1 beye (* up *)
5226 | 0xff53 -> downbirdseye 1 beye (* down *)
5228 | 0xff55 ->
5229 begin match state.layout with
5230 | l :: _ ->
5231 if l.pagey != 0
5232 then (
5233 state.mode <- Birdseye (
5234 oconf, leftx, l.pageno, hooverpageno, anchor
5236 gotopage1 l.pageno 0;
5238 else (
5239 let layout = layout (state.y-conf.winh) conf.winh in
5240 match layout with
5241 | [] -> gotoy (clamp (-conf.winh))
5242 | l :: _ ->
5243 state.mode <- Birdseye (
5244 oconf, leftx, l.pageno, hooverpageno, anchor
5246 gotopage1 l.pageno 0
5249 | [] -> gotoy (clamp (-conf.winh))
5250 end;
5252 | 0xff56 ->
5253 begin match List.rev state.layout with
5254 | l :: _ ->
5255 let layout = layout (state.y + conf.winh) conf.winh in
5256 begin match layout with
5257 | [] ->
5258 let incr = l.pageh - l.pagevh in
5259 if incr = 0
5260 then (
5261 state.mode <-
5262 Birdseye (
5263 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5265 G.postRedisplay "birdseye pagedown";
5267 else gotoy (clamp (incr + conf.interpagespace*2));
5269 | l :: _ ->
5270 state.mode <-
5271 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5272 gotopage1 l.pageno 0;
5275 | [] -> gotoy (clamp conf.winh)
5276 end;
5278 | 0xff50 ->
5279 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5280 gotopage1 0 0
5282 | 0xff57 ->
5283 let pageno = state.pagecount - 1 in
5284 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5285 if not (pagevisible state.layout pageno)
5286 then
5287 let h =
5288 match List.rev state.pdims with
5289 | [] -> conf.winh
5290 | (_, _, h, _) :: _ -> h
5292 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5293 else G.postRedisplay "birdseye end";
5294 | _ -> viewkeyboard key mask
5297 let drawpage l linkindexbase =
5298 let color =
5299 match state.mode with
5300 | Textentry _ -> scalecolor 0.4
5301 | LinkNav _
5302 | View -> scalecolor 1.0
5303 | Birdseye (_, _, pageno, hooverpageno, _) ->
5304 if l.pageno = hooverpageno
5305 then scalecolor 0.9
5306 else (
5307 if l.pageno = pageno
5308 then scalecolor 1.0
5309 else scalecolor 0.8
5312 drawtiles l color;
5313 begin match getopaque l.pageno with
5314 | Some opaque ->
5315 if tileready l l.pagex l.pagey
5316 then
5317 let x = l.pagedispx - l.pagex
5318 and y = l.pagedispy - l.pagey in
5319 let hlmask = (if conf.hlinks then 1 else 0)
5320 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5322 let s =
5323 match state.mode with
5324 | Textentry ((_, s, _, _, _), _) when state.glinks -> s
5325 | _ -> ""
5327 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5328 else 0
5330 | _ -> 0
5331 end;
5334 let scrollindicator () =
5335 let sbw, ph, sh = state.uioh#scrollph in
5336 let sbh, pw, sw = state.uioh#scrollpw in
5338 GlDraw.color (0.64, 0.64, 0.64);
5339 GlDraw.rect
5340 (float (conf.winw - sbw), 0.)
5341 (float conf.winw, float conf.winh)
5343 GlDraw.rect
5344 (0., float (conf.winh - sbh))
5345 (float (conf.winw - state.scrollw - 1), float conf.winh)
5347 GlDraw.color (0.0, 0.0, 0.0);
5349 GlDraw.rect
5350 (float (conf.winw - sbw), ph)
5351 (float conf.winw, ph +. sh)
5353 GlDraw.rect
5354 (pw, float (conf.winh - sbh))
5355 (pw +. sw, float conf.winh)
5359 let showsel () =
5360 match state.mstate with
5361 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5364 | Msel ((x0, y0), (x1, y1)) ->
5365 let rec loop = function
5366 | l :: ls ->
5367 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5368 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5369 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5370 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5371 then
5372 match getopaque l.pageno with
5373 | Some opaque ->
5374 let x0, y0 = pagetranslatepoint l x0 y0 in
5375 let x1, y1 = pagetranslatepoint l x1 y1 in
5376 seltext opaque (x0, y0, x1, y1);
5377 | _ -> ()
5378 else loop ls
5379 | [] -> ()
5381 loop state.layout
5384 let showrects rects =
5385 Gl.enable `blend;
5386 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5387 GlDraw.polygon_mode `both `fill;
5388 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5389 List.iter
5390 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5391 List.iter (fun l ->
5392 if l.pageno = pageno
5393 then (
5394 let dx = float (l.pagedispx - l.pagex) in
5395 let dy = float (l.pagedispy - l.pagey) in
5396 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5397 GlDraw.begins `quads;
5399 GlDraw.vertex2 (x0+.dx, y0+.dy);
5400 GlDraw.vertex2 (x1+.dx, y1+.dy);
5401 GlDraw.vertex2 (x2+.dx, y2+.dy);
5402 GlDraw.vertex2 (x3+.dx, y3+.dy);
5404 GlDraw.ends ();
5406 ) state.layout
5407 ) rects
5409 Gl.disable `blend;
5412 let display () =
5413 GlClear.color (scalecolor2 conf.bgcolor);
5414 GlClear.clear [`color];
5415 let rec loop linkindexbase = function
5416 | l :: rest ->
5417 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5418 loop linkindexbase rest
5419 | [] -> ()
5421 loop 0 state.layout;
5422 let rects =
5423 match state.mode with
5424 | LinkNav (Ltexact (pageno, linkno)) ->
5425 begin match getopaque pageno with
5426 | Some opaque ->
5427 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5428 (pageno, 5, (
5429 float x0, float y0,
5430 float x1, float y0,
5431 float x1, float y1,
5432 float x0, float y1)
5433 ) :: state.rects
5434 | None -> state.rects
5436 | _ -> state.rects
5438 showrects rects;
5439 showsel ();
5440 state.uioh#display;
5441 begin match state.mstate with
5442 | Mzoomrect ((x0, y0), (x1, y1)) ->
5443 Gl.enable `blend;
5444 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5445 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5446 GlDraw.rect (float x0, float y0)
5447 (float x1, float y1);
5448 Gl.disable `blend;
5449 | _ -> ()
5450 end;
5451 enttext ();
5452 scrollindicator ();
5453 Wsi.swapb ();
5456 let zoomrect x y x1 y1 =
5457 let x0 = min x x1
5458 and x1 = max x x1
5459 and y0 = min y y1 in
5460 gotoy (state.y + y0);
5461 state.anchor <- getanchor ();
5462 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5463 let margin =
5464 if state.w < conf.winw - state.scrollw
5465 then (conf.winw - state.scrollw - state.w) / 2
5466 else 0
5468 state.x <- (state.x + margin) - x0;
5469 setzoom zoom;
5470 Wsi.setcursor Wsi.CURSOR_INHERIT;
5471 state.mstate <- Mnone;
5474 let scrollx x =
5475 let winw = conf.winw - state.scrollw - 1 in
5476 let s = float x /. float winw in
5477 let destx = truncate (float (state.w + winw) *. s) in
5478 state.x <- winw - destx;
5479 gotoy_and_clear_text state.y;
5480 state.mstate <- Mscrollx;
5483 let scrolly y =
5484 let s = float y /. float conf.winh in
5485 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5486 gotoy_and_clear_text desty;
5487 state.mstate <- Mscrolly;
5490 let viewmouse button down x y mask =
5491 match button with
5492 | n when (n == 4 || n == 5) && not down ->
5493 if Wsi.withctrl mask
5494 then (
5495 match state.mstate with
5496 | Mzoom (oldn, i) ->
5497 if oldn = n
5498 then (
5499 if i = 2
5500 then
5501 let incr =
5502 match n with
5503 | 5 ->
5504 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5505 | _ ->
5506 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5508 let zoom = conf.zoom -. incr in
5509 setzoom zoom;
5510 state.mstate <- Mzoom (n, 0);
5511 else
5512 state.mstate <- Mzoom (n, i+1);
5514 else state.mstate <- Mzoom (n, 0)
5516 | _ -> state.mstate <- Mzoom (n, 0)
5518 else (
5519 match state.autoscroll with
5520 | Some step -> setautoscrollspeed step (n=4)
5521 | None ->
5522 let incr =
5523 if n = 4
5524 then -conf.scrollstep
5525 else conf.scrollstep
5527 let incr = incr * 2 in
5528 let y = clamp incr in
5529 gotoy_and_clear_text y
5532 | 1 when Wsi.withctrl mask ->
5533 if down
5534 then (
5535 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5536 state.mstate <- Mpan (x, y)
5538 else
5539 state.mstate <- Mnone
5541 | 3 ->
5542 if down
5543 then (
5544 Wsi.setcursor Wsi.CURSOR_CYCLE;
5545 let p = (x, y) in
5546 state.mstate <- Mzoomrect (p, p)
5548 else (
5549 match state.mstate with
5550 | Mzoomrect ((x0, y0), _) ->
5551 if abs (x-x0) > 10 && abs (y - y0) > 10
5552 then zoomrect x0 y0 x y
5553 else (
5554 state.mstate <- Mnone;
5555 Wsi.setcursor Wsi.CURSOR_INHERIT;
5556 G.postRedisplay "kill accidental zoom rect";
5558 | _ ->
5559 Wsi.setcursor Wsi.CURSOR_INHERIT;
5560 state.mstate <- Mnone
5563 | 1 when x > conf.winw - state.scrollw ->
5564 if down
5565 then
5566 let _, position, sh = state.uioh#scrollph in
5567 if y > truncate position && y < truncate (position +. sh)
5568 then state.mstate <- Mscrolly
5569 else scrolly y
5570 else
5571 state.mstate <- Mnone
5573 | 1 when y > conf.winh - state.hscrollh ->
5574 if down
5575 then
5576 let _, position, sw = state.uioh#scrollpw in
5577 if x > truncate position && x < truncate (position +. sw)
5578 then state.mstate <- Mscrollx
5579 else scrollx x
5580 else
5581 state.mstate <- Mnone
5583 | 1 ->
5584 let dest = if down then getunder x y else Unone in
5585 begin match dest with
5586 | Ulinkgoto _
5587 | Ulinkuri _
5588 | Uremote _
5589 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5590 gotounder dest
5592 | Unone when down ->
5593 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5594 state.mstate <- Mpan (x, y);
5596 | Unone | Utext _ ->
5597 if down
5598 then (
5599 if conf.angle mod 360 = 0
5600 then (
5601 state.mstate <- Msel ((x, y), (x, y));
5602 G.postRedisplay "mouse select";
5605 else (
5606 match state.mstate with
5607 | Mnone -> ()
5609 | Mzoom _ | Mscrollx | Mscrolly ->
5610 state.mstate <- Mnone
5612 | Mzoomrect ((x0, y0), _) ->
5613 zoomrect x0 y0 x y
5615 | Mpan _ ->
5616 Wsi.setcursor Wsi.CURSOR_INHERIT;
5617 state.mstate <- Mnone
5619 | Msel ((_, y0), (_, y1)) ->
5620 let rec loop = function
5621 | [] -> ()
5622 | l :: rest ->
5623 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5624 || ((y1 >= l.pagedispy
5625 && y1 <= (l.pagedispy + l.pagevh)))
5626 then
5627 match getopaque l.pageno with
5628 | Some opaque ->
5629 begin
5630 match Ne.pipe () with
5631 | Ne.Exn exn ->
5632 showtext '!'
5633 (Printf.sprintf
5634 "can not create sel pipe: %s"
5635 (Printexc.to_string exn));
5636 | Ne.Res (r, w) ->
5637 let doclose what fd =
5638 Ne.clo fd (fun msg ->
5639 dolog "%s close failed: %s" what msg)
5642 popen conf.selcmd [r, 0; w, -1];
5643 copysel w opaque;
5644 doclose "pipe/r" r;
5645 G.postRedisplay "copysel";
5646 with exn ->
5647 dolog "can not exectute %S: %s"
5648 conf.selcmd (Printexc.to_string exn);
5649 doclose "pipe/r" r;
5650 doclose "pipe/w" w;
5652 | None -> ()
5653 else loop rest
5655 loop state.layout;
5656 Wsi.setcursor Wsi.CURSOR_INHERIT;
5657 state.mstate <- Mnone;
5661 | _ -> ()
5664 let birdseyemouse button down x y mask
5665 (conf, leftx, _, hooverpageno, anchor) =
5666 match button with
5667 | 1 when down ->
5668 let rec loop = function
5669 | [] -> ()
5670 | l :: rest ->
5671 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5672 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5673 then (
5674 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5676 else loop rest
5678 loop state.layout
5679 | 3 -> ()
5680 | _ -> viewmouse button down x y mask
5683 let mouse button down x y mask =
5684 state.uioh <- state.uioh#button button down x y mask;
5687 let motion ~x ~y =
5688 state.uioh <- state.uioh#motion x y
5691 let pmotion ~x ~y =
5692 state.uioh <- state.uioh#pmotion x y;
5695 let uioh = object
5696 method display = ()
5698 method key key mask =
5699 begin match state.mode with
5700 | Textentry textentry -> textentrykeyboard key mask textentry
5701 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5702 | View -> viewkeyboard key mask
5703 | LinkNav linknav -> linknavkeyboard key mask linknav
5704 end;
5705 state.uioh
5707 method button button bstate x y mask =
5708 begin match state.mode with
5709 | LinkNav _
5710 | View -> viewmouse button bstate x y mask
5711 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5712 | Textentry _ -> ()
5713 end;
5714 state.uioh
5716 method motion x y =
5717 begin match state.mode with
5718 | Textentry _ -> ()
5719 | View | Birdseye _ | LinkNav _ ->
5720 match state.mstate with
5721 | Mzoom _ | Mnone -> ()
5723 | Mpan (x0, y0) ->
5724 let dx = x - x0
5725 and dy = y0 - y in
5726 state.mstate <- Mpan (x, y);
5727 if canpan ()
5728 then state.x <- state.x + dx;
5729 let y = clamp dy in
5730 gotoy_and_clear_text y
5732 | Msel (a, _) ->
5733 state.mstate <- Msel (a, (x, y));
5734 G.postRedisplay "motion select";
5736 | Mscrolly ->
5737 let y = min conf.winh (max 0 y) in
5738 scrolly y
5740 | Mscrollx ->
5741 let x = min conf.winw (max 0 x) in
5742 scrollx x
5744 | Mzoomrect (p0, _) ->
5745 state.mstate <- Mzoomrect (p0, (x, y));
5746 G.postRedisplay "motion zoomrect";
5747 end;
5748 state.uioh
5750 method pmotion x y =
5751 begin match state.mode with
5752 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5753 let rec loop = function
5754 | [] ->
5755 if hooverpageno != -1
5756 then (
5757 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5758 G.postRedisplay "pmotion birdseye no hoover";
5760 | l :: rest ->
5761 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5762 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5763 then (
5764 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5765 G.postRedisplay "pmotion birdseye hoover";
5767 else loop rest
5769 loop state.layout
5771 | Textentry _ -> ()
5773 | LinkNav _
5774 | View ->
5775 match state.mstate with
5776 | Mnone -> updateunder x y
5777 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5779 end;
5780 state.uioh
5782 method infochanged _ = ()
5784 method scrollph =
5785 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5786 let p, h = scrollph state.y maxy in
5787 state.scrollw, p, h
5789 method scrollpw =
5790 let winw = conf.winw - state.scrollw - 1 in
5791 let fwinw = float winw in
5792 let sw =
5793 let sw = fwinw /. float state.w in
5794 let sw = fwinw *. sw in
5795 max sw (float conf.scrollh)
5797 let position, sw =
5798 let f = state.w+winw in
5799 let r = float (winw-state.x) /. float f in
5800 let p = fwinw *. r in
5801 p-.sw/.2., sw
5803 let sw =
5804 if position +. sw > fwinw
5805 then fwinw -. position
5806 else sw
5808 state.hscrollh, position, sw
5810 method modehash =
5811 let modename =
5812 match state.mode with
5813 | LinkNav _ -> "links"
5814 | Textentry _ -> "textentry"
5815 | Birdseye _ -> "birdseye"
5816 | View -> "view"
5818 findkeyhash conf modename
5819 end;;
5821 module Config =
5822 struct
5823 open Parser
5825 let fontpath = ref "";;
5827 module KeyMap =
5828 Map.Make (struct type t = (int * int) let compare = compare end);;
5830 let unent s =
5831 let l = String.length s in
5832 let b = Buffer.create l in
5833 unent b s 0 l;
5834 Buffer.contents b;
5837 let home =
5838 try Sys.getenv "HOME"
5839 with exn ->
5840 prerr_endline
5841 ("Can not determine home directory location: " ^
5842 Printexc.to_string exn);
5846 let modifier_of_string = function
5847 | "alt" -> Wsi.altmask
5848 | "shift" -> Wsi.shiftmask
5849 | "ctrl" | "control" -> Wsi.ctrlmask
5850 | "meta" -> Wsi.metamask
5851 | _ -> 0
5854 let key_of_string =
5855 let r = Str.regexp "-" in
5856 fun s ->
5857 let elems = Str.full_split r s in
5858 let f n k m =
5859 let g s =
5860 let m1 = modifier_of_string s in
5861 if m1 = 0
5862 then (Wsi.namekey s, m)
5863 else (k, m lor m1)
5864 in function
5865 | Str.Delim s when n land 1 = 0 -> g s
5866 | Str.Text s -> g s
5867 | Str.Delim _ -> (k, m)
5869 let rec loop n k m = function
5870 | [] -> (k, m)
5871 | x :: xs ->
5872 let k, m = f n k m x in
5873 loop (n+1) k m xs
5875 loop 0 0 0 elems
5878 let keys_of_string =
5879 let r = Str.regexp "[ \t]" in
5880 fun s ->
5881 let elems = Str.split r s in
5882 List.map key_of_string elems
5885 let copykeyhashes c =
5886 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5889 let config_of c attrs =
5890 let apply c k v =
5892 match k with
5893 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5894 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5895 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5896 | "preload" -> { c with preload = bool_of_string v }
5897 | "page-bias" -> { c with pagebias = int_of_string v }
5898 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5899 | "auto-scroll-step" ->
5900 { c with autoscrollstep = max 0 (int_of_string v) }
5901 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5902 | "crop-hack" -> { c with crophack = bool_of_string v }
5903 | "throttle" ->
5904 let mw =
5905 match String.lowercase v with
5906 | "true" -> Some infinity
5907 | "false" -> None
5908 | f -> Some (float_of_string f)
5910 { c with maxwait = mw}
5911 | "highlight-links" -> { c with hlinks = bool_of_string v }
5912 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5913 | "vertical-margin" ->
5914 { c with interpagespace = max 0 (int_of_string v) }
5915 | "zoom" ->
5916 let zoom = float_of_string v /. 100. in
5917 let zoom = max zoom 0.0 in
5918 { c with zoom = zoom }
5919 | "presentation" -> { c with presentation = bool_of_string v }
5920 | "rotation-angle" -> { c with angle = int_of_string v }
5921 | "width" -> { c with winw = max 20 (int_of_string v) }
5922 | "height" -> { c with winh = max 20 (int_of_string v) }
5923 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5924 | "proportional-display" -> { c with proportional = bool_of_string v }
5925 | "pixmap-cache-size" ->
5926 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5927 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5928 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5929 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5930 | "persistent-location" -> { c with jumpback = bool_of_string v }
5931 | "background-color" -> { c with bgcolor = color_of_string v }
5932 | "scrollbar-in-presentation" ->
5933 { c with scrollbarinpm = bool_of_string v }
5934 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5935 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5936 | "mupdf-store-size" ->
5937 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5938 | "checkers" -> { c with checkers = bool_of_string v }
5939 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5940 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5941 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5942 | "uri-launcher" -> { c with urilauncher = unent v }
5943 | "path-launcher" -> { c with pathlauncher = unent v }
5944 | "color-space" -> { c with colorspace = colorspace_of_string v }
5945 | "invert-colors" -> { c with invert = bool_of_string v }
5946 | "brightness" -> { c with colorscale = float_of_string v }
5947 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5948 | "ghyllscroll" ->
5949 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5950 | "columns" ->
5951 let (n, _, _) as nab = multicolumns_of_string v in
5952 if n < 0
5953 then { c with columns = Csplit (-n, [||]) }
5954 else { c with columns = Cmulti (nab, [||]) }
5955 | "birds-eye-columns" ->
5956 { c with beyecolumns = Some (max (int_of_string v) 2) }
5957 | "selection-command" -> { c with selcmd = unent v }
5958 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5959 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
5960 | _ -> c
5961 with exn ->
5962 prerr_endline ("Error processing attribute (`" ^
5963 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5966 let rec fold c = function
5967 | [] -> c
5968 | (k, v) :: rest ->
5969 let c = apply c k v in
5970 fold c rest
5972 fold { c with keyhashes = copykeyhashes c } attrs;
5975 let fromstring f pos n v d =
5976 try f v
5977 with exn ->
5978 dolog "Error processing attribute (%S=%S) at %d\n%s"
5979 n v pos (Printexc.to_string exn)
5984 let bookmark_of attrs =
5985 let rec fold title page rely = function
5986 | ("title", v) :: rest -> fold v page rely rest
5987 | ("page", v) :: rest -> fold title v rely rest
5988 | ("rely", v) :: rest -> fold title page v rest
5989 | _ :: rest -> fold title page rely rest
5990 | [] -> title, page, rely
5992 fold "invalid" "0" "0" attrs
5995 let doc_of attrs =
5996 let rec fold path page rely pan = function
5997 | ("path", v) :: rest -> fold v page rely pan rest
5998 | ("page", v) :: rest -> fold path v rely pan rest
5999 | ("rely", v) :: rest -> fold path page v pan rest
6000 | ("pan", v) :: rest -> fold path page rely v rest
6001 | _ :: rest -> fold path page rely pan rest
6002 | [] -> path, page, rely, pan
6004 fold "" "0" "0" "0" attrs
6007 let map_of attrs =
6008 let rec fold rs ls = function
6009 | ("out", v) :: rest -> fold v ls rest
6010 | ("in", v) :: rest -> fold rs v rest
6011 | _ :: rest -> fold ls rs rest
6012 | [] -> ls, rs
6014 fold "" "" attrs
6017 let setconf dst src =
6018 dst.scrollbw <- src.scrollbw;
6019 dst.scrollh <- src.scrollh;
6020 dst.icase <- src.icase;
6021 dst.preload <- src.preload;
6022 dst.pagebias <- src.pagebias;
6023 dst.verbose <- src.verbose;
6024 dst.scrollstep <- src.scrollstep;
6025 dst.maxhfit <- src.maxhfit;
6026 dst.crophack <- src.crophack;
6027 dst.autoscrollstep <- src.autoscrollstep;
6028 dst.maxwait <- src.maxwait;
6029 dst.hlinks <- src.hlinks;
6030 dst.underinfo <- src.underinfo;
6031 dst.interpagespace <- src.interpagespace;
6032 dst.zoom <- src.zoom;
6033 dst.presentation <- src.presentation;
6034 dst.angle <- src.angle;
6035 dst.winw <- src.winw;
6036 dst.winh <- src.winh;
6037 dst.savebmarks <- src.savebmarks;
6038 dst.memlimit <- src.memlimit;
6039 dst.proportional <- src.proportional;
6040 dst.texcount <- src.texcount;
6041 dst.sliceheight <- src.sliceheight;
6042 dst.thumbw <- src.thumbw;
6043 dst.jumpback <- src.jumpback;
6044 dst.bgcolor <- src.bgcolor;
6045 dst.scrollbarinpm <- src.scrollbarinpm;
6046 dst.tilew <- src.tilew;
6047 dst.tileh <- src.tileh;
6048 dst.mustoresize <- src.mustoresize;
6049 dst.checkers <- src.checkers;
6050 dst.aalevel <- src.aalevel;
6051 dst.trimmargins <- src.trimmargins;
6052 dst.trimfuzz <- src.trimfuzz;
6053 dst.urilauncher <- src.urilauncher;
6054 dst.colorspace <- src.colorspace;
6055 dst.invert <- src.invert;
6056 dst.colorscale <- src.colorscale;
6057 dst.redirectstderr <- src.redirectstderr;
6058 dst.ghyllscroll <- src.ghyllscroll;
6059 dst.columns <- src.columns;
6060 dst.beyecolumns <- src.beyecolumns;
6061 dst.selcmd <- src.selcmd;
6062 dst.updatecurs <- src.updatecurs;
6063 dst.pathlauncher <- src.pathlauncher;
6064 dst.keyhashes <- copykeyhashes src;
6065 dst.hfsize <- src.hfsize;
6068 let get s =
6069 let h = Hashtbl.create 10 in
6070 let dc = { defconf with angle = defconf.angle } in
6071 let rec toplevel v t spos _ =
6072 match t with
6073 | Vdata | Vcdata | Vend -> v
6074 | Vopen ("llppconfig", _, closed) ->
6075 if closed
6076 then v
6077 else { v with f = llppconfig }
6078 | Vopen _ ->
6079 error "unexpected subelement at top level" s spos
6080 | Vclose _ -> error "unexpected close at top level" s spos
6082 and llppconfig v t spos _ =
6083 match t with
6084 | Vdata | Vcdata -> v
6085 | Vend -> error "unexpected end of input in llppconfig" s spos
6086 | Vopen ("defaults", attrs, closed) ->
6087 let c = config_of dc attrs in
6088 setconf dc c;
6089 if closed
6090 then v
6091 else { v with f = defaults }
6093 | Vopen ("ui-font", attrs, closed) ->
6094 let rec getsize size = function
6095 | [] -> size
6096 | ("size", v) :: rest ->
6097 let size =
6098 fromstring int_of_string spos "size" v fstate.fontsize in
6099 getsize size rest
6100 | l -> getsize size l
6102 fstate.fontsize <- getsize fstate.fontsize attrs;
6103 if closed
6104 then v
6105 else { v with f = uifont (Buffer.create 10) }
6107 | Vopen ("doc", attrs, closed) ->
6108 let pathent, spage, srely, span = doc_of attrs in
6109 let path = unent pathent
6110 and pageno = fromstring int_of_string spos "page" spage 0
6111 and rely = fromstring float_of_string spos "rely" srely 0.0
6112 and pan = fromstring int_of_string spos "pan" span 0 in
6113 let c = config_of dc attrs in
6114 let anchor = (pageno, rely) in
6115 if closed
6116 then (Hashtbl.add h path (c, [], pan, anchor); v)
6117 else { v with f = doc path pan anchor c [] }
6119 | Vopen _ ->
6120 error "unexpected subelement in llppconfig" s spos
6122 | Vclose "llppconfig" -> { v with f = toplevel }
6123 | Vclose _ -> error "unexpected close in llppconfig" s spos
6125 and defaults v t spos _ =
6126 match t with
6127 | Vdata | Vcdata -> v
6128 | Vend -> error "unexpected end of input in defaults" s spos
6129 | Vopen ("keymap", attrs, closed) ->
6130 let modename =
6131 try List.assoc "mode" attrs
6132 with Not_found -> "global" in
6133 if closed
6134 then v
6135 else
6136 let ret keymap =
6137 let h = findkeyhash dc modename in
6138 KeyMap.iter (Hashtbl.replace h) keymap;
6139 defaults
6141 { v with f = pkeymap ret KeyMap.empty }
6143 | Vopen (_, _, _) ->
6144 error "unexpected subelement in defaults" s spos
6146 | Vclose "defaults" ->
6147 { v with f = llppconfig }
6149 | Vclose _ -> error "unexpected close in defaults" s spos
6151 and uifont b v t spos epos =
6152 match t with
6153 | Vdata | Vcdata ->
6154 Buffer.add_substring b s spos (epos - spos);
6156 | Vopen (_, _, _) ->
6157 error "unexpected subelement in ui-font" s spos
6158 | Vclose "ui-font" ->
6159 if String.length !fontpath = 0
6160 then fontpath := Buffer.contents b;
6161 { v with f = llppconfig }
6162 | Vclose _ -> error "unexpected close in ui-font" s spos
6163 | Vend -> error "unexpected end of input in ui-font" s spos
6165 and doc path pan anchor c bookmarks v t spos _ =
6166 match t with
6167 | Vdata | Vcdata -> v
6168 | Vend -> error "unexpected end of input in doc" s spos
6169 | Vopen ("bookmarks", _, closed) ->
6170 if closed
6171 then v
6172 else { v with f = pbookmarks path pan anchor c bookmarks }
6174 | Vopen ("keymap", attrs, closed) ->
6175 let modename =
6176 try List.assoc "mode" attrs
6177 with Not_found -> "global"
6179 if closed
6180 then v
6181 else
6182 let ret keymap =
6183 let h = findkeyhash c modename in
6184 KeyMap.iter (Hashtbl.replace h) keymap;
6185 doc path pan anchor c bookmarks
6187 { v with f = pkeymap ret KeyMap.empty }
6189 | Vopen (_, _, _) ->
6190 error "unexpected subelement in doc" s spos
6192 | Vclose "doc" ->
6193 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6194 { v with f = llppconfig }
6196 | Vclose _ -> error "unexpected close in doc" s spos
6198 and pkeymap ret keymap v t spos _ =
6199 match t with
6200 | Vdata | Vcdata -> v
6201 | Vend -> error "unexpected end of input in keymap" s spos
6202 | Vopen ("map", attrs, closed) ->
6203 let r, l = map_of attrs in
6204 let kss = fromstring keys_of_string spos "in" r [] in
6205 let lss = fromstring keys_of_string spos "out" l [] in
6206 let keymap =
6207 match kss with
6208 | [] -> keymap
6209 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6210 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6212 if closed
6213 then { v with f = pkeymap ret keymap }
6214 else
6215 let f () = v in
6216 { v with f = skip "map" f }
6218 | Vopen _ ->
6219 error "unexpected subelement in keymap" s spos
6221 | Vclose "keymap" ->
6222 { v with f = ret keymap }
6224 | Vclose _ -> error "unexpected close in keymap" s spos
6226 and pbookmarks path pan anchor c bookmarks v t spos _ =
6227 match t with
6228 | Vdata | Vcdata -> v
6229 | Vend -> error "unexpected end of input in bookmarks" s spos
6230 | Vopen ("item", attrs, closed) ->
6231 let titleent, spage, srely = bookmark_of attrs in
6232 let page = fromstring int_of_string spos "page" spage 0
6233 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6234 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6235 if closed
6236 then { v with f = pbookmarks path pan anchor c bookmarks }
6237 else
6238 let f () = v in
6239 { v with f = skip "item" f }
6241 | Vopen _ ->
6242 error "unexpected subelement in bookmarks" s spos
6244 | Vclose "bookmarks" ->
6245 { v with f = doc path pan anchor c bookmarks }
6247 | Vclose _ -> error "unexpected close in bookmarks" s spos
6249 and skip tag f v t spos _ =
6250 match t with
6251 | Vdata | Vcdata -> v
6252 | Vend ->
6253 error ("unexpected end of input in skipped " ^ tag) s spos
6254 | Vopen (tag', _, closed) ->
6255 if closed
6256 then v
6257 else
6258 let f' () = { v with f = skip tag f } in
6259 { v with f = skip tag' f' }
6260 | Vclose ctag ->
6261 if tag = ctag
6262 then f ()
6263 else error ("unexpected close in skipped " ^ tag) s spos
6266 parse { f = toplevel; accu = () } s;
6267 h, dc;
6270 let do_load f ic =
6272 let len = in_channel_length ic in
6273 let s = String.create len in
6274 really_input ic s 0 len;
6275 f s;
6276 with
6277 | Parse_error (msg, s, pos) ->
6278 let subs = subs s pos in
6279 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6280 failwith ("parse error: " ^ s)
6282 | exn ->
6283 failwith ("config load error: " ^ Printexc.to_string exn)
6286 let defconfpath =
6287 let dir =
6289 let dir = Filename.concat home ".config" in
6290 if Sys.is_directory dir then dir else home
6291 with _ -> home
6293 Filename.concat dir "llpp.conf"
6296 let confpath = ref defconfpath;;
6298 let load1 f =
6299 if Sys.file_exists !confpath
6300 then
6301 match
6302 (try Some (open_in_bin !confpath)
6303 with exn ->
6304 prerr_endline
6305 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6306 Printexc.to_string exn);
6307 None
6309 with
6310 | Some ic ->
6311 begin try
6312 f (do_load get ic)
6313 with exn ->
6314 prerr_endline
6315 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6316 Printexc.to_string exn);
6317 end;
6318 close_in ic;
6320 | None -> ()
6321 else
6322 f (Hashtbl.create 0, defconf)
6325 let load () =
6326 let f (h, dc) =
6327 let pc, pb, px, pa =
6329 Hashtbl.find h (Filename.basename state.path)
6330 with Not_found -> dc, [], 0, (0, 0.0)
6332 setconf defconf dc;
6333 setconf conf pc;
6334 state.bookmarks <- pb;
6335 state.x <- px;
6336 state.scrollw <- conf.scrollbw;
6337 if conf.jumpback
6338 then state.anchor <- pa;
6339 cbput state.hists.nav pa;
6341 load1 f
6344 let add_attrs bb always dc c =
6345 let ob s a b =
6346 if always || a != b
6347 then Printf.bprintf bb "\n %s='%b'" s a
6348 and oi s a b =
6349 if always || a != b
6350 then Printf.bprintf bb "\n %s='%d'" s a
6351 and oI s a b =
6352 if always || a != b
6353 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6354 and oz s a b =
6355 if always || a <> b
6356 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6357 and oF s a b =
6358 if always || a <> b
6359 then Printf.bprintf bb "\n %s='%f'" s a
6360 and oc s a b =
6361 if always || a <> b
6362 then
6363 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6364 and oC s a b =
6365 if always || a <> b
6366 then
6367 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6368 and oR s a b =
6369 if always || a <> b
6370 then
6371 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6372 and os s a b =
6373 if always || a <> b
6374 then
6375 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6376 and og s a b =
6377 if always || a <> b
6378 then
6379 match a with
6380 | None -> ()
6381 | Some (_N, _A, _B) ->
6382 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6383 and oW s a b =
6384 if always || a <> b
6385 then
6386 let v =
6387 match a with
6388 | None -> "false"
6389 | Some f ->
6390 if f = infinity
6391 then "true"
6392 else string_of_float f
6394 Printf.bprintf bb "\n %s='%s'" s v
6395 and oco s a b =
6396 if always || a <> b
6397 then
6398 match a with
6399 | Cmulti ((n, a, b), _) when n > 1 ->
6400 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6401 | Csplit (n, _) when n > 1 ->
6402 Printf.bprintf bb "\n %s='%d'" s ~-n
6403 | _ -> ()
6404 and obeco s a b =
6405 if always || a <> b
6406 then
6407 match a with
6408 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6409 | _ -> ()
6411 let w, h =
6412 if always
6413 then dc.winw, dc.winh
6414 else
6415 match state.fullscreen with
6416 | Some wh -> wh
6417 | None -> c.winw, c.winh
6419 let zoom, presentation, interpagespace, maxwait =
6420 if always
6421 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6422 else
6423 match state.mode with
6424 | Birdseye (bc, _, _, _, _) ->
6425 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6426 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6428 oi "width" w dc.winw;
6429 oi "height" h dc.winh;
6430 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6431 oi "scroll-handle-height" c.scrollh dc.scrollh;
6432 ob "case-insensitive-search" c.icase dc.icase;
6433 ob "preload" c.preload dc.preload;
6434 oi "page-bias" c.pagebias dc.pagebias;
6435 oi "scroll-step" c.scrollstep dc.scrollstep;
6436 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6437 ob "max-height-fit" c.maxhfit dc.maxhfit;
6438 ob "crop-hack" c.crophack dc.crophack;
6439 oW "throttle" maxwait dc.maxwait;
6440 ob "highlight-links" c.hlinks dc.hlinks;
6441 ob "under-cursor-info" c.underinfo dc.underinfo;
6442 oi "vertical-margin" interpagespace dc.interpagespace;
6443 oz "zoom" zoom dc.zoom;
6444 ob "presentation" presentation dc.presentation;
6445 oi "rotation-angle" c.angle dc.angle;
6446 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6447 ob "proportional-display" c.proportional dc.proportional;
6448 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6449 oi "tex-count" c.texcount dc.texcount;
6450 oi "slice-height" c.sliceheight dc.sliceheight;
6451 oi "thumbnail-width" c.thumbw dc.thumbw;
6452 ob "persistent-location" c.jumpback dc.jumpback;
6453 oc "background-color" c.bgcolor dc.bgcolor;
6454 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6455 oi "tile-width" c.tilew dc.tilew;
6456 oi "tile-height" c.tileh dc.tileh;
6457 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6458 ob "checkers" c.checkers dc.checkers;
6459 oi "aalevel" c.aalevel dc.aalevel;
6460 ob "trim-margins" c.trimmargins dc.trimmargins;
6461 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6462 os "uri-launcher" c.urilauncher dc.urilauncher;
6463 os "path-launcher" c.pathlauncher dc.pathlauncher;
6464 oC "color-space" c.colorspace dc.colorspace;
6465 ob "invert-colors" c.invert dc.invert;
6466 oF "brightness" c.colorscale dc.colorscale;
6467 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6468 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6469 oco "columns" c.columns dc.columns;
6470 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6471 os "selection-command" c.selcmd dc.selcmd;
6472 ob "update-cursor" c.updatecurs dc.updatecurs;
6473 oi "hint-font-size" c.hfsize dc.hfsize;
6476 let keymapsbuf always dc c =
6477 let bb = Buffer.create 16 in
6478 let rec loop = function
6479 | [] -> ()
6480 | (modename, h) :: rest ->
6481 let dh = findkeyhash dc modename in
6482 if always || h <> dh
6483 then (
6484 if Hashtbl.length h > 0
6485 then (
6486 if Buffer.length bb > 0
6487 then Buffer.add_char bb '\n';
6488 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6489 Hashtbl.iter (fun i o ->
6490 let isdifferent = always ||
6492 let dO = Hashtbl.find dh i in
6493 dO <> o
6494 with Not_found -> true
6496 if isdifferent
6497 then
6498 let addkm (k, m) =
6499 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6500 if Wsi.withalt m then Buffer.add_string bb "alt-";
6501 if Wsi.withshift m then Buffer.add_string bb "shift-";
6502 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6503 Buffer.add_string bb (Wsi.keyname k);
6505 let addkms l =
6506 let rec loop = function
6507 | [] -> ()
6508 | km :: [] -> addkm km
6509 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6511 loop l
6513 Buffer.add_string bb "<map in='";
6514 addkm i;
6515 match o with
6516 | KMinsrt km ->
6517 Buffer.add_string bb "' out='";
6518 addkm km;
6519 Buffer.add_string bb "'/>\n"
6521 | KMinsrl kms ->
6522 Buffer.add_string bb "' out='";
6523 addkms kms;
6524 Buffer.add_string bb "'/>\n"
6526 | KMmulti (ins, kms) ->
6527 Buffer.add_char bb ' ';
6528 addkms ins;
6529 Buffer.add_string bb "' out='";
6530 addkms kms;
6531 Buffer.add_string bb "'/>\n"
6532 ) h;
6533 Buffer.add_string bb "</keymap>";
6536 loop rest
6538 loop c.keyhashes;
6542 let save () =
6543 let uifontsize = fstate.fontsize in
6544 let bb = Buffer.create 32768 in
6545 let f (h, dc) =
6546 let dc = if conf.bedefault then conf else dc in
6547 Buffer.add_string bb "<llppconfig>\n";
6549 if String.length !fontpath > 0
6550 then
6551 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6552 uifontsize
6553 !fontpath
6554 else (
6555 if uifontsize <> 14
6556 then
6557 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6560 Buffer.add_string bb "<defaults ";
6561 add_attrs bb true dc dc;
6562 let kb = keymapsbuf true dc dc in
6563 if Buffer.length kb > 0
6564 then (
6565 Buffer.add_string bb ">\n";
6566 Buffer.add_buffer bb kb;
6567 Buffer.add_string bb "\n</defaults>\n";
6569 else Buffer.add_string bb "/>\n";
6571 let adddoc path pan anchor c bookmarks =
6572 if bookmarks == [] && c = dc && anchor = emptyanchor
6573 then ()
6574 else (
6575 Printf.bprintf bb "<doc path='%s'"
6576 (enent path 0 (String.length path));
6578 if anchor <> emptyanchor
6579 then (
6580 let n, y = anchor in
6581 Printf.bprintf bb " page='%d'" n;
6582 if y > 1e-6
6583 then
6584 Printf.bprintf bb " rely='%f'" y
6588 if pan != 0
6589 then Printf.bprintf bb " pan='%d'" pan;
6591 add_attrs bb false dc c;
6592 let kb = keymapsbuf false dc c in
6594 begin match bookmarks with
6595 | [] ->
6596 if Buffer.length kb > 0
6597 then (
6598 Buffer.add_string bb ">\n";
6599 Buffer.add_buffer bb kb;
6600 Buffer.add_string bb "\n</doc>\n";
6602 else Buffer.add_string bb "/>\n"
6603 | _ ->
6604 Buffer.add_string bb ">\n<bookmarks>\n";
6605 List.iter (fun (title, _level, (page, rely)) ->
6606 Printf.bprintf bb
6607 "<item title='%s' page='%d'"
6608 (enent title 0 (String.length title))
6609 page
6611 if rely > 1e-6
6612 then
6613 Printf.bprintf bb " rely='%f'" rely
6615 Buffer.add_string bb "/>\n";
6616 ) bookmarks;
6617 Buffer.add_string bb "</bookmarks>";
6618 if Buffer.length kb > 0
6619 then (
6620 Buffer.add_string bb "\n";
6621 Buffer.add_buffer bb kb;
6623 Buffer.add_string bb "\n</doc>\n";
6624 end;
6628 let pan, conf =
6629 match state.mode with
6630 | Birdseye (c, pan, _, _, _) ->
6631 let beyecolumns =
6632 match conf.columns with
6633 | Cmulti ((c, _, _), _) -> Some c
6634 | Csingle -> None
6635 | Csplit _ -> None
6636 and columns =
6637 match c.columns with
6638 | Cmulti (c, _) -> Cmulti (c, [||])
6639 | Csingle -> Csingle
6640 | Csplit _ -> failwith "quit from bird's eye while split"
6642 pan, { c with beyecolumns = beyecolumns; columns = columns }
6643 | _ -> state.x, conf
6645 let basename = Filename.basename state.path in
6646 adddoc basename pan (getanchor ())
6647 { conf with
6648 autoscrollstep =
6649 match state.autoscroll with
6650 | Some step -> step
6651 | None -> conf.autoscrollstep }
6652 (if conf.savebmarks then state.bookmarks else []);
6654 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6655 if basename <> path
6656 then adddoc path x y c bookmarks
6657 ) h;
6658 Buffer.add_string bb "</llppconfig>";
6660 load1 f;
6661 if Buffer.length bb > 0
6662 then
6664 let tmp = !confpath ^ ".tmp" in
6665 let oc = open_out_bin tmp in
6666 Buffer.output_buffer oc bb;
6667 close_out oc;
6668 Unix.rename tmp !confpath;
6669 with exn ->
6670 prerr_endline
6671 ("error while saving configuration: " ^ Printexc.to_string exn)
6673 end;;
6675 let () =
6676 Arg.parse
6677 (Arg.align
6678 [("-p", Arg.String (fun s -> state.password <- s) ,
6679 "<password> Set password");
6681 ("-f", Arg.String (fun s -> Config.fontpath := s),
6682 "<path> Set path to the user interface font");
6684 ("-c", Arg.String (fun s -> Config.confpath := s),
6685 "<path> Set path to the configuration file");
6687 ("-v", Arg.Unit (fun () ->
6688 Printf.printf
6689 "%s\nconfiguration path: %s\n"
6690 (version ())
6691 Config.defconfpath
6693 exit 0), " Print version and exit");
6696 (fun s -> state.path <- s)
6697 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6699 if String.length state.path = 0
6700 then (prerr_endline "file name missing"; exit 1);
6702 Config.load ();
6704 let globalkeyhash = findkeyhash conf "global" in
6705 let wsfd, winw, winh = Wsi.init (object
6706 method expose =
6707 if nogeomcmds state.geomcmds || platform == Posx
6708 then display ()
6709 else (
6710 GlFunc.draw_buffer `front;
6711 GlClear.color (scalecolor2 conf.bgcolor);
6712 GlClear.clear [`color];
6713 GlFunc.draw_buffer `back;
6715 method display = display ()
6716 method reshape w h = reshape w h
6717 method mouse b d x y m = mouse b d x y m
6718 method motion x y = state.mpos <- (x, y); motion x y
6719 method pmotion x y = state.mpos <- (x, y); pmotion x y
6720 method key k m =
6721 let mascm = m land (
6722 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6723 ) in
6724 match state.keystate with
6725 | KSnone ->
6726 let km = k, mascm in
6727 begin
6728 match
6729 let modehash = state.uioh#modehash in
6730 try Hashtbl.find modehash km
6731 with Not_found ->
6732 try Hashtbl.find globalkeyhash km
6733 with Not_found -> KMinsrt (k, m)
6734 with
6735 | KMinsrt (k, m) -> keyboard k m
6736 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6737 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6739 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6740 List.iter (fun (k, m) -> keyboard k m) insrt;
6741 state.keystate <- KSnone
6742 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6743 state.keystate <- KSinto (keys, insrt)
6744 | _ ->
6745 state.keystate <- KSnone
6747 method enter x y = state.mpos <- (x, y); pmotion x y
6748 method leave = state.mpos <- (-1, -1)
6749 method quit = raise Quit
6750 end) conf.winw conf.winh (platform = Posx) in
6752 state.wsfd <- wsfd;
6754 if not (
6755 List.exists GlMisc.check_extension
6756 [ "GL_ARB_texture_rectangle"
6757 ; "GL_EXT_texture_recangle"
6758 ; "GL_NV_texture_rectangle" ]
6760 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6762 let cr, sw =
6763 match Ne.pipe () with
6764 | Ne.Exn exn ->
6765 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6766 exit 1
6767 | Ne.Res rw -> rw
6768 and sr, cw =
6769 match Ne.pipe () with
6770 | Ne.Exn exn ->
6771 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6772 exit 1
6773 | Ne.Res rw -> rw
6776 cloexec cr;
6777 cloexec sw;
6778 cloexec sr;
6779 cloexec cw;
6781 setcheckers conf.checkers;
6782 redirectstderr ();
6784 init (cr, cw) (
6785 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6786 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6787 !Config.fontpath
6789 state.sr <- sr;
6790 state.sw <- sw;
6791 state.text <- "Opening " ^ state.path;
6792 reshape winw winh;
6793 opendoc state.path state.password;
6794 state.uioh <- uioh;
6796 let rec loop deadline =
6797 let r =
6798 match state.errfd with
6799 | None -> [state.sr; state.wsfd]
6800 | Some fd -> [state.sr; state.wsfd; fd]
6802 if state.redisplay
6803 then (
6804 state.redisplay <- false;
6805 display ();
6807 let timeout =
6808 let now = now () in
6809 if deadline > now
6810 then (
6811 if deadline = infinity
6812 then ~-.1.0
6813 else max 0.0 (deadline -. now)
6815 else 0.0
6817 let r, _, _ =
6818 try Unix.select r [] [] timeout
6819 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6821 begin match r with
6822 | [] ->
6823 state.ghyll None;
6824 let newdeadline =
6825 if state.ghyll == noghyll
6826 then
6827 match state.autoscroll with
6828 | Some step when step != 0 ->
6829 let y = state.y + step in
6830 let y =
6831 if y < 0
6832 then state.maxy
6833 else if y >= state.maxy then 0 else y
6835 gotoy y;
6836 if state.mode = View
6837 then state.text <- "";
6838 deadline +. 0.01
6839 | _ -> infinity
6840 else deadline +. 0.01
6842 loop newdeadline
6844 | l ->
6845 let rec checkfds = function
6846 | [] -> ()
6847 | fd :: rest when fd = state.sr ->
6848 let cmd = readcmd state.sr in
6849 act cmd;
6850 checkfds rest
6852 | fd :: rest when fd = state.wsfd ->
6853 Wsi.readresp fd;
6854 checkfds rest
6856 | fd :: rest ->
6857 let s = String.create 80 in
6858 let n = Unix.read fd s 0 80 in
6859 if conf.redirectstderr
6860 then (
6861 Buffer.add_substring state.errmsgs s 0 n;
6862 state.newerrmsgs <- true;
6863 state.redisplay <- true;
6865 else (
6866 prerr_string (String.sub s 0 n);
6867 flush stderr;
6869 checkfds rest
6871 checkfds l;
6872 let newdeadline =
6873 let deadline1 =
6874 if deadline = infinity
6875 then now () +. 0.01
6876 else deadline
6878 match state.autoscroll with
6879 | Some step when step != 0 -> deadline1
6880 | _ -> if state.ghyll == noghyll then infinity else deadline1
6882 loop newdeadline
6883 end;
6886 loop infinity;
6887 with Quit ->
6888 Config.save ();
6889 exit 0;