Performance over bells and whistles
[llpp.git] / main.ml
blobe34c226cb828d9e38c582ec4850bc3b50f55272e
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 * cancelonempty
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 cancelonempty = bool
160 and te =
161 | TEstop
162 | TEdone of string
163 | TEcont of string
164 | TEswitch of textentry
167 type 'a circbuf =
168 { store : 'a array
169 ; mutable rc : int
170 ; mutable wc : int
171 ; mutable len : int
175 let bound v minv maxv =
176 max minv (min maxv v);
179 let cbnew n v =
180 { store = Array.create n v
181 ; rc = 0
182 ; wc = 0
183 ; len = 0
187 let drawstring size x y s =
188 Gl.enable `blend;
189 Gl.enable `texture_2d;
190 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
191 ignore (drawstr size x y s);
192 Gl.disable `blend;
193 Gl.disable `texture_2d;
196 let drawstring1 size x y s =
197 drawstr size x y s;
200 let drawstring2 size x y fmt =
201 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
204 let cbcap b = Array.length b.store;;
206 let cbput b v =
207 let cap = cbcap b in
208 b.store.(b.wc) <- v;
209 b.wc <- (b.wc + 1) mod cap;
210 b.rc <- b.wc;
211 b.len <- min (b.len + 1) cap;
214 let cbempty b = b.len = 0;;
216 let cbgetg b circular dir =
217 if cbempty b
218 then b.store.(0)
219 else
220 let rc = b.rc + dir in
221 let rc =
222 if circular
223 then (
224 if rc = -1
225 then b.len-1
226 else (
227 if rc = b.len
228 then 0
229 else rc
232 else max 0 (min rc (b.len-1))
234 b.rc <- rc;
235 b.store.(rc);
238 let cbget b = cbgetg b false;;
239 let cbgetc b = cbgetg b true;;
241 type page =
242 { pageno : int
243 ; pagedimno : int
244 ; pagew : int
245 ; pageh : int
246 ; pagex : int
247 ; pagey : int
248 ; pagevw : int
249 ; pagevh : int
250 ; pagedispx : int
251 ; pagedispy : int
252 ; pagecol : int
256 let debugl l =
257 dolog "l %d dim=%d {" l.pageno l.pagedimno;
258 dolog " WxH %dx%d" l.pagew l.pageh;
259 dolog " vWxH %dx%d" l.pagevw l.pagevh;
260 dolog " pagex,y %d,%d" l.pagex l.pagey;
261 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
262 dolog " column %d" l.pagecol;
263 dolog "}";
266 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
267 dolog "rect {";
268 dolog " x0,y0=(% f, % f)" x0 y0;
269 dolog " x1,y1=(% f, % f)" x1 y1;
270 dolog " x2,y2=(% f, % f)" x2 y2;
271 dolog " x3,y3=(% f, % f)" x3 y3;
272 dolog "}";
275 type multicolumns = multicol * pagegeom
276 and splitcolumns = columncount * pagegeom
277 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
278 and multicol = columncount * covercount * covercount
279 and pdimno = int
280 and columncount = int
281 and covercount = int;;
283 type conf =
284 { mutable scrollbw : int
285 ; mutable scrollh : int
286 ; mutable icase : bool
287 ; mutable preload : bool
288 ; mutable pagebias : int
289 ; mutable verbose : bool
290 ; mutable debug : bool
291 ; mutable scrollstep : int
292 ; mutable maxhfit : bool
293 ; mutable crophack : bool
294 ; mutable autoscrollstep : int
295 ; mutable maxwait : float option
296 ; mutable hlinks : bool
297 ; mutable underinfo : bool
298 ; mutable interpagespace : interpagespace
299 ; mutable zoom : float
300 ; mutable presentation : bool
301 ; mutable angle : angle
302 ; mutable winw : int
303 ; mutable winh : int
304 ; mutable savebmarks : bool
305 ; mutable proportional : proportional
306 ; mutable trimmargins : trimmargins
307 ; mutable trimfuzz : irect
308 ; mutable memlimit : memsize
309 ; mutable texcount : texcount
310 ; mutable sliceheight : sliceheight
311 ; mutable thumbw : width
312 ; mutable jumpback : bool
313 ; mutable bgcolor : float * float * float
314 ; mutable bedefault : bool
315 ; mutable scrollbarinpm : bool
316 ; mutable tilew : int
317 ; mutable tileh : int
318 ; mutable mustoresize : memsize
319 ; mutable checkers : bool
320 ; mutable aalevel : int
321 ; mutable urilauncher : string
322 ; mutable pathlauncher : string
323 ; mutable colorspace : colorspace
324 ; mutable invert : bool
325 ; mutable colorscale : float
326 ; mutable redirectstderr : bool
327 ; mutable ghyllscroll : (int * int * int) option
328 ; mutable columns : columns
329 ; mutable beyecolumns : columncount option
330 ; mutable selcmd : string
331 ; mutable updatecurs : bool
332 ; mutable keyhashes : (string * keyhash) list
333 ; mutable hfsize : int
335 and columns =
336 | Csingle
337 | Cmulti of multicolumns
338 | Csplit of splitcolumns
341 type anchor = pageno * top;;
343 type outline = string * int * anchor;;
345 type rect = float * float * float * float * float * float * float * float;;
347 type tile = opaque * pixmapsize * elapsed
348 and elapsed = float;;
349 type pagemapkey = pageno * gen;;
350 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
351 and row = int
352 and col = int;;
354 let emptyanchor = (0, 0.0);;
356 type infochange = | Memused | Docinfo | Pdim;;
358 class type uioh = object
359 method display : unit
360 method key : int -> int -> uioh
361 method button : int -> bool -> int -> int -> int -> uioh
362 method motion : int -> int -> uioh
363 method pmotion : int -> int -> uioh
364 method infochanged : infochange -> unit
365 method scrollpw : (int * float * float)
366 method scrollph : (int * float * float)
367 method modehash : keyhash
368 end;;
370 type mode =
371 | Birdseye of (conf * leftx * pageno * pageno * anchor)
372 | Textentry of (textentry * onleave)
373 | View
374 | LinkNav of linktarget
375 and onleave = leavetextentrystatus -> unit
376 and leavetextentrystatus = | Cancel | Confirm
377 and helpitem = string * int * action
378 and action =
379 | Noaction
380 | Action of (uioh -> uioh)
381 and linktarget =
382 | Ltexact of (pageno * int)
383 | Ltgendir of int
386 let isbirdseye = function Birdseye _ -> true | _ -> false;;
387 let istextentry = function Textentry _ -> true | _ -> false;;
389 type currently =
390 | Idle
391 | Loading of (page * gen)
392 | Tiling of (
393 page * opaque * colorspace * angle * gen * col * row * width * height
395 | Outlining of outline list
398 let emptykeyhash = Hashtbl.create 0;;
399 let nouioh : uioh = object (self)
400 method display = ()
401 method key _ _ = self
402 method button _ _ _ _ _ = self
403 method motion _ _ = self
404 method pmotion _ _ = self
405 method infochanged _ = ()
406 method scrollpw = (0, nan, nan)
407 method scrollph = (0, nan, nan)
408 method modehash = emptykeyhash
409 end;;
411 type state =
412 { mutable sr : Unix.file_descr
413 ; mutable sw : Unix.file_descr
414 ; mutable wsfd : Unix.file_descr
415 ; mutable errfd : Unix.file_descr option
416 ; mutable stderr : Unix.file_descr
417 ; mutable errmsgs : Buffer.t
418 ; mutable newerrmsgs : bool
419 ; mutable w : int
420 ; mutable x : int
421 ; mutable y : int
422 ; mutable scrollw : int
423 ; mutable hscrollh : int
424 ; mutable anchor : anchor
425 ; mutable ranchors : (string * string * anchor) list
426 ; mutable maxy : int
427 ; mutable layout : page list
428 ; pagemap : (pagemapkey, opaque) Hashtbl.t
429 ; tilemap : (tilemapkey, tile) Hashtbl.t
430 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
431 ; mutable pdims : (pageno * width * height * leftx) list
432 ; mutable pagecount : int
433 ; mutable currently : currently
434 ; mutable mstate : mstate
435 ; mutable searchpattern : string
436 ; mutable rects : (pageno * recttype * rect) list
437 ; mutable rects1 : (pageno * recttype * rect) list
438 ; mutable text : string
439 ; mutable fullscreen : (width * height) option
440 ; mutable mode : mode
441 ; mutable uioh : uioh
442 ; mutable outlines : outline array
443 ; mutable bookmarks : outline list
444 ; mutable path : string
445 ; mutable password : string
446 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
447 ; mutable memused : memsize
448 ; mutable gen : gen
449 ; mutable throttle : (page list * int * float) option
450 ; mutable autoscroll : int option
451 ; mutable ghyll : (int option -> unit)
452 ; mutable help : helpitem array
453 ; mutable docinfo : (int * string) list
454 ; mutable texid : GlTex.texture_id option
455 ; hists : hists
456 ; mutable prevzoom : float
457 ; mutable progress : float
458 ; mutable redisplay : bool
459 ; mutable mpos : mpos
460 ; mutable keystate : keystate
461 ; mutable glinks : bool
462 ; mutable prevcolumns : (columns * float) option
464 and hists =
465 { pat : string circbuf
466 ; pag : string circbuf
467 ; nav : anchor circbuf
468 ; sel : string circbuf
472 let defconf =
473 { scrollbw = 7
474 ; scrollh = 12
475 ; icase = true
476 ; preload = true
477 ; pagebias = 0
478 ; verbose = false
479 ; debug = false
480 ; scrollstep = 24
481 ; maxhfit = true
482 ; crophack = false
483 ; autoscrollstep = 2
484 ; maxwait = None
485 ; hlinks = false
486 ; underinfo = false
487 ; interpagespace = 2
488 ; zoom = 1.0
489 ; presentation = false
490 ; angle = 0
491 ; winw = 900
492 ; winh = 900
493 ; savebmarks = true
494 ; proportional = true
495 ; trimmargins = false
496 ; trimfuzz = (0,0,0,0)
497 ; memlimit = 32 lsl 20
498 ; texcount = 256
499 ; sliceheight = 24
500 ; thumbw = 76
501 ; jumpback = true
502 ; bgcolor = (0.5, 0.5, 0.5)
503 ; bedefault = false
504 ; scrollbarinpm = true
505 ; tilew = 2048
506 ; tileh = 2048
507 ; mustoresize = 256 lsl 20
508 ; checkers = true
509 ; aalevel = 8
510 ; urilauncher =
511 (match platform with
512 | Plinux | Pfreebsd | Pdragonflybsd
513 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
514 | Posx -> "open \"%s\""
515 | Pcygwin -> "cygstart \"%s\""
516 | Punknown -> "echo %s")
517 ; pathlauncher = "lp \"%s\""
518 ; selcmd =
519 (match platform with
520 | Plinux | Pfreebsd | Pdragonflybsd
521 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
522 | Posx -> "pbcopy"
523 | Pcygwin -> "wsel"
524 | Punknown -> "cat")
525 ; colorspace = Rgb
526 ; invert = false
527 ; colorscale = 1.0
528 ; redirectstderr = false
529 ; ghyllscroll = None
530 ; columns = Csingle
531 ; beyecolumns = None
532 ; updatecurs = false
533 ; hfsize = 12
534 ; keyhashes =
535 let mk n = (n, Hashtbl.create 1) in
536 [ mk "global"
537 ; mk "info"
538 ; mk "help"
539 ; mk "outline"
540 ; mk "listview"
541 ; mk "birdseye"
542 ; mk "textentry"
543 ; mk "links"
544 ; mk "view"
549 let findkeyhash c name =
550 try List.assoc name c.keyhashes
551 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
554 let conf = { defconf with angle = defconf.angle };;
556 type fontstate =
557 { mutable fontsize : int
558 ; mutable wwidth : float
559 ; mutable maxrows : int
563 let fstate =
564 { fontsize = 14
565 ; wwidth = nan
566 ; maxrows = -1
570 let setfontsize n =
571 fstate.fontsize <- n;
572 fstate.wwidth <- measurestr fstate.fontsize "w";
573 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
576 let geturl s =
577 let colonpos = try String.index s ':' with Not_found -> -1 in
578 let len = String.length s in
579 if colonpos >= 0 && colonpos + 3 < len
580 then (
581 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
582 then
583 let schemestartpos =
584 try String.rindex_from s colonpos ' '
585 with Not_found -> -1
587 let scheme =
588 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
590 match scheme with
591 | "http" | "ftp" | "mailto" ->
592 let epos =
593 try String.index_from s colonpos ' '
594 with Not_found -> len
596 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
597 | _ -> ""
598 else ""
600 else ""
603 let gotouri uri =
604 if String.length conf.urilauncher = 0
605 then print_endline uri
606 else (
607 let url = geturl uri in
608 if String.length url = 0
609 then print_endline uri
610 else
611 let re = Str.regexp "%s" in
612 let command = Str.global_replace re url conf.urilauncher in
613 try popen command []
614 with exn ->
615 Printf.eprintf
616 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
617 flush stderr;
621 let version () =
622 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
623 (platform_to_string platform) Sys.word_size Sys.ocaml_version
626 let makehelp () =
627 let strings = version () :: "" :: Help.keys in
628 Array.of_list (
629 List.map (fun s ->
630 let url = geturl s in
631 if String.length url > 0
632 then (s, 0, Action (fun u -> gotouri url; u))
633 else (s, 0, Noaction)
634 ) strings);
637 let noghyll _ = ();;
638 let firstgeomcmds = "", [];;
640 let state =
641 { sr = Unix.stdin
642 ; sw = Unix.stdin
643 ; wsfd = Unix.stdin
644 ; errfd = None
645 ; stderr = Unix.stderr
646 ; errmsgs = Buffer.create 0
647 ; newerrmsgs = false
648 ; x = 0
649 ; y = 0
650 ; w = 0
651 ; scrollw = 0
652 ; hscrollh = 0
653 ; anchor = emptyanchor
654 ; ranchors = []
655 ; layout = []
656 ; maxy = max_int
657 ; tilelru = Queue.create ()
658 ; pagemap = Hashtbl.create 10
659 ; tilemap = Hashtbl.create 10
660 ; pdims = []
661 ; pagecount = 0
662 ; currently = Idle
663 ; mstate = Mnone
664 ; rects = []
665 ; rects1 = []
666 ; text = ""
667 ; mode = View
668 ; fullscreen = None
669 ; searchpattern = ""
670 ; outlines = [||]
671 ; bookmarks = []
672 ; path = ""
673 ; password = ""
674 ; geomcmds = firstgeomcmds
675 ; hists =
676 { nav = cbnew 10 (0, 0.0)
677 ; pat = cbnew 10 ""
678 ; pag = cbnew 10 ""
679 ; sel = cbnew 10 ""
681 ; memused = 0
682 ; gen = 0
683 ; throttle = None
684 ; autoscroll = None
685 ; ghyll = noghyll
686 ; help = makehelp ()
687 ; docinfo = []
688 ; texid = None
689 ; prevzoom = 1.0
690 ; progress = -1.0
691 ; uioh = nouioh
692 ; redisplay = true
693 ; mpos = (-1, -1)
694 ; keystate = KSnone
695 ; glinks = false
696 ; prevcolumns = None
700 let vlog fmt =
701 if conf.verbose
702 then
703 Printf.kprintf prerr_endline fmt
704 else
705 Printf.kprintf ignore fmt
708 let launchpath () =
709 if String.length conf.pathlauncher = 0
710 then print_endline state.path
711 else (
712 let re = Str.regexp "%s" in
713 let command = Str.global_replace re state.path conf.pathlauncher in
714 try popen command []
715 with exn ->
716 Printf.eprintf
717 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
718 flush stderr;
722 module Ne = struct
723 type 'a t = | Res of 'a | Exn of exn;;
725 let pipe () =
726 try Res (Unix.pipe ())
727 with exn -> Exn exn
730 let clo fd f =
731 try Unix.close fd
732 with exn -> f (Printexc.to_string exn)
735 let dup fd =
736 try Res (Unix.dup fd)
737 with exn -> Exn exn
740 let dup2 fd1 fd2 =
741 try Res (Unix.dup2 fd1 fd2)
742 with exn -> Exn exn
744 end;;
746 let redirectstderr () =
747 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
748 if conf.redirectstderr
749 then
750 match Ne.pipe () with
751 | Ne.Exn exn ->
752 dolog "failed to create stderr redirection pipes: %s"
753 (Printexc.to_string exn)
755 | Ne.Res (r, w) ->
756 begin match Ne.dup Unix.stderr with
757 | Ne.Exn exn ->
758 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
759 Ne.clo r (clofail "pipe/r");
760 Ne.clo w (clofail "pipe/w");
762 | Ne.Res dupstderr ->
763 begin match Ne.dup2 w Unix.stderr with
764 | Ne.Exn exn ->
765 dolog "failed to dup2 to stderr: %s"
766 (Printexc.to_string exn);
767 Ne.clo dupstderr (clofail "stderr duplicate");
768 Ne.clo r (clofail "redir pipe/r");
769 Ne.clo w (clofail "redir pipe/w");
771 | Ne.Res () ->
772 state.stderr <- dupstderr;
773 state.errfd <- Some r;
774 end;
776 else (
777 state.newerrmsgs <- false;
778 begin match state.errfd with
779 | Some fd ->
780 begin match Ne.dup2 state.stderr Unix.stderr with
781 | Ne.Exn exn ->
782 dolog "failed to dup2 original stderr: %s"
783 (Printexc.to_string exn)
784 | Ne.Res () ->
785 Ne.clo fd (clofail "dup of stderr");
786 Unix.dup2 state.stderr Unix.stderr;
787 state.errfd <- None;
788 end;
789 | None -> ()
790 end;
791 prerr_string (Buffer.contents state.errmsgs);
792 flush stderr;
793 Buffer.clear state.errmsgs;
797 module G =
798 struct
799 let postRedisplay who =
800 if conf.verbose
801 then prerr_endline ("redisplay for " ^ who);
802 state.redisplay <- true;
804 end;;
806 let getopaque pageno =
807 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
808 with Not_found -> None
811 let putopaque pageno opaque =
812 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
815 let pagetranslatepoint l x y =
816 let dy = y - l.pagedispy in
817 let y = dy + l.pagey in
818 let dx = x - l.pagedispx in
819 let x = dx + l.pagex in
820 (x, y);
823 let getunder x y =
824 let rec f = function
825 | l :: rest ->
826 begin match getopaque l.pageno with
827 | Some opaque ->
828 let x0 = l.pagedispx in
829 let x1 = x0 + l.pagevw in
830 let y0 = l.pagedispy in
831 let y1 = y0 + l.pagevh in
832 if y >= y0 && y <= y1 && x >= x0 && x <= x1
833 then
834 let px, py = pagetranslatepoint l x y in
835 match whatsunder opaque px py with
836 | Unone -> f rest
837 | under -> under
838 else f rest
839 | _ ->
840 f rest
842 | [] -> Unone
844 f state.layout
847 let showtext c s =
848 state.text <- Printf.sprintf "%c%s" c s;
849 G.postRedisplay "showtext";
852 let undertext = function
853 | Unone -> "none"
854 | Ulinkuri s -> s
855 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
856 | Utext s -> "font: " ^ s
857 | Uunexpected s -> "unexpected: " ^ s
858 | Ulaunch s -> "launch: " ^ s
859 | Unamed s -> "named: " ^ s
860 | Uremote (filename, pageno) ->
861 Printf.sprintf "%s: page %d" filename (pageno+1)
864 let updateunder x y =
865 match getunder x y with
866 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
867 | Ulinkuri uri ->
868 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
869 Wsi.setcursor Wsi.CURSOR_INFO
870 | Ulinkgoto (pageno, _) ->
871 if conf.underinfo
872 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
873 Wsi.setcursor Wsi.CURSOR_INFO
874 | Utext s ->
875 if conf.underinfo then showtext 'f' ("ont: " ^ s);
876 Wsi.setcursor Wsi.CURSOR_TEXT
877 | Uunexpected s ->
878 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
879 Wsi.setcursor Wsi.CURSOR_INHERIT
880 | Ulaunch s ->
881 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
882 Wsi.setcursor Wsi.CURSOR_INHERIT
883 | Unamed s ->
884 if conf.underinfo then showtext 'n' ("amed: " ^ s);
885 Wsi.setcursor Wsi.CURSOR_INHERIT
886 | Uremote (filename, pageno) ->
887 if conf.underinfo then showtext 'r'
888 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
889 Wsi.setcursor Wsi.CURSOR_INFO
892 let showlinktype under =
893 if conf.underinfo
894 then
895 match under with
896 | Unone -> ()
897 | under ->
898 let s = undertext under in
899 showtext ' ' s
902 let addchar s c =
903 let b = Buffer.create (String.length s + 1) in
904 Buffer.add_string b s;
905 Buffer.add_char b c;
906 Buffer.contents b;
909 let colorspace_of_string s =
910 match String.lowercase s with
911 | "rgb" -> Rgb
912 | "bgr" -> Bgr
913 | "gray" -> Gray
914 | _ -> failwith "invalid colorspace"
917 let int_of_colorspace = function
918 | Rgb -> 0
919 | Bgr -> 1
920 | Gray -> 2
923 let colorspace_of_int = function
924 | 0 -> Rgb
925 | 1 -> Bgr
926 | 2 -> Gray
927 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
930 let colorspace_to_string = function
931 | Rgb -> "rgb"
932 | Bgr -> "bgr"
933 | Gray -> "gray"
936 let intentry_with_suffix text key =
937 let c =
938 if key >= 32 && key < 127
939 then Char.chr key
940 else '\000'
942 match Char.lowercase c with
943 | '0' .. '9' ->
944 let text = addchar text c in
945 TEcont text
947 | 'k' | 'm' | 'g' ->
948 let text = addchar text c in
949 TEcont text
951 | _ ->
952 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
953 TEcont text
956 let multicolumns_to_string (n, a, b) =
957 if a = 0 && b = 0
958 then Printf.sprintf "%d" n
959 else Printf.sprintf "%d,%d,%d" n a b;
962 let multicolumns_of_string s =
964 (int_of_string s, 0, 0)
965 with _ ->
966 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
969 let readcmd fd =
970 let s = "xxxx" in
971 let n = Unix.read fd s 0 4 in
972 if n != 4 then failwith "incomplete read(len)";
973 let len = 0
974 lor (Char.code s.[0] lsl 24)
975 lor (Char.code s.[1] lsl 16)
976 lor (Char.code s.[2] lsl 8)
977 lor (Char.code s.[3] lsl 0)
979 let s = String.create len in
980 let n = Unix.read fd s 0 len in
981 if n != len then failwith "incomplete read(data)";
985 let btod b = if b then 1 else 0;;
987 let wcmd fmt =
988 let b = Buffer.create 16 in
989 Buffer.add_string b "llll";
990 Printf.kbprintf
991 (fun b ->
992 let s = Buffer.contents b in
993 let n = String.length s in
994 let len = n - 4 in
995 (* dolog "wcmd %S" (String.sub s 4 len); *)
996 s.[0] <- Char.chr ((len lsr 24) land 0xff);
997 s.[1] <- Char.chr ((len lsr 16) land 0xff);
998 s.[2] <- Char.chr ((len lsr 8) land 0xff);
999 s.[3] <- Char.chr (len land 0xff);
1000 let n' = Unix.write state.sw s 0 n in
1001 if n' != n then failwith "write failed";
1002 ) b fmt;
1005 let calcips h =
1006 if conf.presentation
1007 then
1008 let d = conf.winh - h in
1009 max 0 ((d + 1) / 2)
1010 else
1011 conf.interpagespace
1014 let calcheight () =
1015 let rec f pn ph pi fh l =
1016 match l with
1017 | (n, _, h, _) :: rest ->
1018 let ips = calcips h in
1019 let fh =
1020 if conf.presentation
1021 then fh+ips
1022 else (
1023 if isbirdseye state.mode && pn = 0
1024 then fh + ips
1025 else fh
1028 let fh = fh + ((n - pn) * (ph + pi)) in
1029 f n h ips fh rest;
1031 | [] ->
1032 let inc =
1033 if conf.presentation || (isbirdseye state.mode && pn = 0)
1034 then 0
1035 else -pi
1037 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
1038 max 0 fh
1040 let fh = f 0 0 0 0 state.pdims in
1044 let calcheight () =
1045 match conf.columns with
1046 | Csingle -> calcheight ()
1047 | Cmulti ((c, _, _), b) ->
1048 let rec loop y h n =
1049 if n < 0
1050 then loop y h (n+1)
1051 else (
1052 if n = Array.length b
1053 then y + h
1054 else
1055 let (_, _, y', (_, _, h', _)) = b.(n) in
1056 let y = min y y'
1057 and h = max h h' in
1058 loop y h (n+1)
1061 loop max_int 0 (((Array.length b - 1) / c) * c)
1062 | Csplit (_, b) ->
1063 if Array.length b > 0
1064 then
1065 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1066 y + h
1067 else 0
1070 let getpageyh pageno =
1071 let rec f pn ph pi y l =
1072 match l with
1073 | (n, _, h, _) :: rest ->
1074 let ips = calcips h in
1075 if n >= pageno
1076 then
1077 let h = if n = pageno then h else ph in
1078 if conf.presentation && n = pageno
1079 then
1080 y + (pageno - pn) * (ph + pi) + pi, h
1081 else
1082 y + (pageno - pn) * (ph + pi), h
1083 else
1084 let y = y + (if conf.presentation then pi else 0) in
1085 let y = y + (n - pn) * (ph + pi) in
1086 f n h ips y rest
1088 | [] ->
1089 y + (pageno - pn) * (ph + pi), ph
1091 f 0 0 0 0 state.pdims
1094 let getpageyh pageno =
1095 match conf.columns with
1096 | Csingle -> getpageyh pageno
1097 | Cmulti (_, b) ->
1098 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1099 y, h
1100 | Csplit (c, b) ->
1101 let n = pageno*c in
1102 let (_, _, y, (_, _, h, _)) = b.(n) in
1103 y, h
1106 let getpagedim pageno =
1107 let rec f ppdim l =
1108 match l with
1109 | (n, _, _, _) as pdim :: rest ->
1110 if n >= pageno
1111 then (if n = pageno then pdim else ppdim)
1112 else f pdim rest
1114 | [] -> ppdim
1116 f (-1, -1, -1, -1) state.pdims
1119 let getpagey pageno = fst (getpageyh pageno);;
1121 let nogeomcmds cmds =
1122 match cmds with
1123 | s, [] -> String.length s = 0
1124 | _ -> false
1127 let layout1 y sh =
1128 let sh = sh - state.hscrollh in
1129 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1130 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1131 match pdims with
1132 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1133 let ips = calcips h in
1134 let yinc =
1135 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1136 then ips
1137 else 0
1139 (w, h, ips, xoff), rest, pdimno + 1, yinc
1140 | _ ->
1141 prev, pdims, pdimno, 0
1143 let dy = dy + yinc in
1144 let py = py + yinc in
1145 if pageno = state.pagecount || dy >= sh
1146 then
1147 accu
1148 else
1149 let vy = y + dy in
1150 if py + h <= vy - yinc
1151 then
1152 let py = py + h + ips in
1153 let dy = max 0 (py - y) in
1154 f ~pageno:(pageno+1)
1155 ~pdimno
1156 ~prev:curr
1159 ~pdims:rest
1160 ~accu
1161 else
1162 let pagey = vy - py in
1163 let pagevh = h - pagey in
1164 let pagevh = min (sh - dy) pagevh in
1165 let off = if yinc > 0 then py - vy else 0 in
1166 let py = py + h + ips in
1167 let pagex, dx =
1168 let xoff = xoff +
1169 if state.w < conf.winw - state.scrollw
1170 then (conf.winw - state.scrollw - state.w) / 2
1171 else 0
1173 let dispx = xoff + state.x in
1174 if dispx < 0
1175 then (-dispx, 0)
1176 else (0, dispx)
1178 let pagevw =
1179 let lw = w - pagex in
1180 min lw (conf.winw - state.scrollw)
1182 let e =
1183 { pageno = pageno
1184 ; pagedimno = pdimno
1185 ; pagew = w
1186 ; pageh = h
1187 ; pagex = pagex
1188 ; pagey = pagey + off
1189 ; pagevw = pagevw
1190 ; pagevh = pagevh - off
1191 ; pagedispx = dx
1192 ; pagedispy = dy + off
1193 ; pagecol = 0
1196 let accu = e :: accu in
1197 f ~pageno:(pageno+1)
1198 ~pdimno
1199 ~prev:curr
1201 ~dy:(dy+pagevh+ips)
1202 ~pdims:rest
1203 ~accu
1205 let accu =
1207 ~pageno:0
1208 ~pdimno:~-1
1209 ~prev:(0,0,0,0)
1210 ~py:0
1211 ~dy:0
1212 ~pdims:state.pdims
1213 ~accu:[]
1215 List.rev accu
1218 let layoutN ((columns, coverA, coverB), b) y sh =
1219 let sh = sh - state.hscrollh in
1220 let rec fold accu n =
1221 if n = Array.length b
1222 then accu
1223 else
1224 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1225 if (vy - y) > sh &&
1226 (n = coverA - 1
1227 || n = state.pagecount - coverB
1228 || (n - coverA) mod columns = columns - 1)
1229 then accu
1230 else
1231 let accu =
1232 if vy + h > y
1233 then
1234 let pagey = max 0 (y - vy) in
1235 let pagedispy = if pagey > 0 then 0 else vy - y in
1236 let pagedispx, pagex =
1237 let pdx =
1238 if n = coverA - 1 || n = state.pagecount - coverB
1239 then state.x + (conf.winw - state.scrollw - w) / 2
1240 else dx + xoff + state.x
1242 if pdx < 0
1243 then 0, -pdx
1244 else pdx, 0
1246 let pagevw =
1247 let vw = conf.winw - state.scrollw - pagedispx in
1248 let pw = w - pagex in
1249 min vw pw
1251 let pagevh = min (h - pagey) (sh - pagedispy) in
1252 if pagevw > 0 && pagevh > 0
1253 then
1254 let e =
1255 { pageno = n
1256 ; pagedimno = pdimno
1257 ; pagew = w
1258 ; pageh = h
1259 ; pagex = pagex
1260 ; pagey = pagey
1261 ; pagevw = pagevw
1262 ; pagevh = pagevh
1263 ; pagedispx = pagedispx
1264 ; pagedispy = pagedispy
1265 ; pagecol = 0
1268 e :: accu
1269 else
1270 accu
1271 else
1272 accu
1274 fold accu (n+1)
1276 List.rev (fold [] 0)
1279 let layoutS (columns, b) y sh =
1280 let sh = sh - state.hscrollh in
1281 let rec fold accu n =
1282 if n = Array.length b
1283 then accu
1284 else
1285 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1286 if (vy - y) > sh
1287 then accu
1288 else
1289 let accu =
1290 if vy + pageh > y
1291 then
1292 let x = xoff + state.x in
1293 let pagey = max 0 (y - vy) in
1294 let pagedispy = if pagey > 0 then 0 else vy - y in
1295 let pagedispx, pagex =
1296 if px = 0
1297 then (
1298 if x < 0
1299 then 0, -x
1300 else x, 0
1302 else (
1303 let px = px - x in
1304 if px < 0
1305 then -px, 0
1306 else 0, px
1309 let pagecolw = pagew/columns in
1310 let pagedispx =
1311 if pagecolw < conf.winw
1312 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1313 else pagedispx
1315 let pagevw =
1316 let vw = conf.winw - pagedispx - state.scrollw in
1317 let pw = pagew - pagex in
1318 min vw pw
1320 let pagevw = min pagevw pagecolw in
1321 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1322 if pagevw > 0 && pagevh > 0
1323 then
1324 let e =
1325 { pageno = n/columns
1326 ; pagedimno = pdimno
1327 ; pagew = pagew
1328 ; pageh = pageh
1329 ; pagex = pagex
1330 ; pagey = pagey
1331 ; pagevw = pagevw
1332 ; pagevh = pagevh
1333 ; pagedispx = pagedispx
1334 ; pagedispy = pagedispy
1335 ; pagecol = n mod columns
1338 e :: accu
1339 else
1340 accu
1341 else
1342 accu
1344 fold accu (n+1)
1346 List.rev (fold [] 0)
1349 let layout y sh =
1350 if nogeomcmds state.geomcmds
1351 then
1352 match conf.columns with
1353 | Csingle -> layout1 y sh
1354 | Cmulti c -> layoutN c y sh
1355 | Csplit s -> layoutS s y sh
1356 else []
1359 let clamp incr =
1360 let y = state.y + incr in
1361 let y = max 0 y in
1362 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1366 let itertiles l f =
1367 let tilex = l.pagex mod conf.tilew in
1368 let tiley = l.pagey mod conf.tileh in
1370 let col = l.pagex / conf.tilew in
1371 let row = l.pagey / conf.tileh in
1373 let rec rowloop row y0 dispy h =
1374 if h = 0
1375 then ()
1376 else (
1377 let dh = conf.tileh - y0 in
1378 let dh = min h dh in
1379 let rec colloop col x0 dispx w =
1380 if w = 0
1381 then ()
1382 else (
1383 let dw = conf.tilew - x0 in
1384 let dw = min w dw in
1386 f col row dispx dispy x0 y0 dw dh;
1387 colloop (col+1) 0 (dispx+dw) (w-dw)
1390 colloop col tilex l.pagedispx l.pagevw;
1391 rowloop (row+1) 0 (dispy+dh) (h-dh)
1394 if l.pagevw > 0 && l.pagevh > 0
1395 then rowloop row tiley l.pagedispy l.pagevh;
1398 let gettileopaque l col row =
1399 let key =
1400 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1402 try Some (Hashtbl.find state.tilemap key)
1403 with Not_found -> None
1406 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1407 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1408 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1411 let drawtiles l color =
1412 GlDraw.color color;
1413 let f col row x y tilex tiley w h =
1414 match gettileopaque l col row with
1415 | Some (opaque, _, t) ->
1416 let params = x, y, w, h, tilex, tiley in
1417 if conf.invert
1418 then (
1419 Gl.enable `blend;
1420 GlFunc.blend_func `zero `one_minus_src_color;
1422 drawtile params opaque;
1423 if conf.invert
1424 then Gl.disable `blend;
1425 if conf.debug
1426 then (
1427 let s = Printf.sprintf
1428 "%d[%d,%d] %f sec"
1429 l.pageno col row t
1431 let w = measurestr fstate.fontsize s in
1432 GlMisc.push_attrib [`current];
1433 GlDraw.color (0.0, 0.0, 0.0);
1434 GlDraw.rect
1435 (float (x-2), float (y-2))
1436 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1437 GlDraw.color (1.0, 1.0, 1.0);
1438 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1439 GlMisc.pop_attrib ();
1442 | _ ->
1443 let w =
1444 let lw = conf.winw - state.scrollw - x in
1445 min lw w
1446 and h =
1447 let lh = conf.winh - y in
1448 min lh h
1450 Gl.enable `texture_2d;
1451 begin match state.texid with
1452 | Some id ->
1453 GlTex.bind_texture `texture_2d id;
1454 let x0 = float x
1455 and y0 = float y
1456 and x1 = float (x+w)
1457 and y1 = float (y+h) in
1459 let tw = float w /. 64.0
1460 and th = float h /. 64.0 in
1461 let tx0 = float tilex /. 64.0
1462 and ty0 = float tiley /. 64.0 in
1463 let tx1 = tx0 +. tw
1464 and ty1 = ty0 +. th in
1465 GlDraw.begins `quads;
1466 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1467 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1468 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1469 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1470 GlDraw.ends ();
1472 Gl.disable `texture_2d;
1473 | None ->
1474 GlDraw.color (1.0, 1.0, 1.0);
1475 GlDraw.rect
1476 (float x, float y)
1477 (float (x+w), float (y+h));
1478 end;
1479 if w > 128 && h > fstate.fontsize + 10
1480 then (
1481 GlDraw.color (0.0, 0.0, 0.0);
1482 let c, r =
1483 if conf.verbose
1484 then (col*conf.tilew, row*conf.tileh)
1485 else col, row
1487 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1489 GlDraw.color color;
1491 itertiles l f
1494 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1496 let tilevisible1 l x y =
1497 let ax0 = l.pagex
1498 and ax1 = l.pagex + l.pagevw
1499 and ay0 = l.pagey
1500 and ay1 = l.pagey + l.pagevh in
1502 let bx0 = x
1503 and by0 = y in
1504 let bx1 = min (bx0 + conf.tilew) l.pagew
1505 and by1 = min (by0 + conf.tileh) l.pageh in
1507 let rx0 = max ax0 bx0
1508 and ry0 = max ay0 by0
1509 and rx1 = min ax1 bx1
1510 and ry1 = min ay1 by1 in
1512 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1513 nonemptyintersection
1516 let tilevisible layout n x y =
1517 let rec findpageinlayout m = function
1518 | l :: rest when l.pageno = n ->
1519 tilevisible1 l x y || (
1520 match conf.columns with
1521 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1522 | _ -> false
1524 | _ :: rest -> findpageinlayout 0 rest
1525 | [] -> false
1527 findpageinlayout 0 layout;
1530 let tileready l x y =
1531 tilevisible1 l x y &&
1532 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1535 let tilepage n p layout =
1536 let rec loop = function
1537 | l :: rest ->
1538 if l.pageno = n
1539 then
1540 let f col row _ _ _ _ _ _ =
1541 if state.currently = Idle
1542 then
1543 match gettileopaque l col row with
1544 | Some _ -> ()
1545 | None ->
1546 let x = col*conf.tilew
1547 and y = row*conf.tileh in
1548 let w =
1549 let w = l.pagew - x in
1550 min w conf.tilew
1552 let h =
1553 let h = l.pageh - y in
1554 min h conf.tileh
1556 wcmd "tile %s %d %d %d %d" p x y w h;
1557 state.currently <-
1558 Tiling (
1559 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1560 conf.tilew, conf.tileh
1563 itertiles l f;
1564 else
1565 loop rest
1567 | [] -> ()
1569 if nogeomcmds state.geomcmds
1570 then loop layout;
1573 let preloadlayout visiblepages =
1574 let presentation = conf.presentation in
1575 let interpagespace = conf.interpagespace in
1576 let maxy = state.maxy in
1577 conf.presentation <- false;
1578 conf.interpagespace <- 0;
1579 state.maxy <- calcheight ();
1580 let y =
1581 match visiblepages with
1582 | [] -> if state.y >= maxy then maxy else 0
1583 | l :: _ -> getpagey l.pageno + l.pagey
1585 let y = if y < conf.winh then 0 else y - conf.winh in
1586 let h = state.y - y + conf.winh*3 in
1587 let pages = layout y h in
1588 conf.presentation <- presentation;
1589 conf.interpagespace <- interpagespace;
1590 state.maxy <- maxy;
1591 pages;
1594 let load pages =
1595 let rec loop pages =
1596 if state.currently != Idle
1597 then ()
1598 else
1599 match pages with
1600 | l :: rest ->
1601 begin match getopaque l.pageno with
1602 | None ->
1603 wcmd "page %d %d" l.pageno l.pagedimno;
1604 state.currently <- Loading (l, state.gen);
1605 | Some opaque ->
1606 tilepage l.pageno opaque pages;
1607 loop rest
1608 end;
1609 | _ -> ()
1611 if nogeomcmds state.geomcmds
1612 then loop pages
1615 let preload pages =
1616 load pages;
1617 if conf.preload && state.currently = Idle
1618 then load (preloadlayout pages);
1621 let layoutready layout =
1622 let rec fold all ls =
1623 all && match ls with
1624 | l :: rest ->
1625 let seen = ref false in
1626 let allvisible = ref true in
1627 let foo col row _ _ _ _ _ _ =
1628 seen := true;
1629 allvisible := !allvisible &&
1630 begin match gettileopaque l col row with
1631 | Some _ -> true
1632 | None -> false
1635 itertiles l foo;
1636 fold (!seen && !allvisible) rest
1637 | [] -> true
1639 let alltilesvisible = fold true layout in
1640 alltilesvisible;
1643 let gotoy y =
1644 let y = bound y 0 state.maxy in
1645 let y, layout, proceed =
1646 match conf.maxwait with
1647 | Some time when state.ghyll == noghyll ->
1648 begin match state.throttle with
1649 | None ->
1650 let layout = layout y conf.winh in
1651 let ready = layoutready layout in
1652 if not ready
1653 then (
1654 load layout;
1655 state.throttle <- Some (layout, y, now ());
1657 else G.postRedisplay "gotoy showall (None)";
1658 y, layout, ready
1659 | Some (_, _, started) ->
1660 let dt = now () -. started in
1661 if dt > time
1662 then (
1663 state.throttle <- None;
1664 let layout = layout y conf.winh in
1665 load layout;
1666 G.postRedisplay "maxwait";
1667 y, layout, true
1669 else -1, [], false
1672 | _ ->
1673 let layout = layout y conf.winh in
1674 if true || layoutready layout
1675 then G.postRedisplay "gotoy ready";
1676 y, layout, true
1678 if proceed
1679 then (
1680 state.y <- y;
1681 state.layout <- layout;
1682 begin match state.mode with
1683 | LinkNav (Ltexact (pageno, linkno)) ->
1684 let rec loop = function
1685 | [] ->
1686 state.mode <- LinkNav (Ltgendir 0)
1687 | l :: _ when l.pageno = pageno ->
1688 begin match getopaque pageno with
1689 | None ->
1690 state.mode <- LinkNav (Ltgendir 0)
1691 | Some opaque ->
1692 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1693 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1694 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1695 then state.mode <- LinkNav (Ltgendir 0)
1697 | _ :: rest -> loop rest
1699 loop layout
1700 | _ -> ()
1701 end;
1702 begin match state.mode with
1703 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1704 if not (pagevisible layout pageno)
1705 then (
1706 match state.layout with
1707 | [] -> ()
1708 | l :: _ ->
1709 state.mode <- Birdseye (
1710 conf, leftx, l.pageno, hooverpageno, anchor
1713 | LinkNav (Ltgendir dir as lt) ->
1714 let linknav =
1715 let rec loop = function
1716 | [] -> lt
1717 | l :: rest ->
1718 match getopaque l.pageno with
1719 | None -> loop rest
1720 | Some opaque ->
1721 let link =
1722 let ld =
1723 if dir = 0
1724 then LDfirstvisible (l.pagex, l.pagey, dir)
1725 else (
1726 if dir > 0 then LDfirst else LDlast
1729 findlink opaque ld
1731 match link with
1732 | Lnotfound -> loop rest
1733 | Lfound n ->
1734 showlinktype (getlink opaque n);
1735 Ltexact (l.pageno, n)
1737 loop state.layout
1739 state.mode <- LinkNav linknav
1740 | _ -> ()
1741 end;
1742 preload layout;
1744 state.ghyll <- noghyll;
1745 if conf.updatecurs
1746 then (
1747 let mx, my = state.mpos in
1748 updateunder mx my;
1752 let conttiling pageno opaque =
1753 tilepage pageno opaque
1754 (if conf.preload then preloadlayout state.layout else state.layout)
1757 let gotoy_and_clear_text y =
1758 if not conf.verbose then state.text <- "";
1759 gotoy y;
1762 let getanchor () =
1763 match state.layout with
1764 | [] -> emptyanchor
1765 | l :: _ ->
1766 let coloff = l.pagecol * l.pageh in
1767 (l.pageno, (float l.pagey +. float coloff) /. float l.pageh)
1770 let getanchory (n, top) =
1771 let y, h = getpageyh n in
1772 y + (truncate (top *. float h));
1775 let gotoanchor anchor =
1776 gotoy (getanchory anchor);
1779 let addnav () =
1780 cbput state.hists.nav (getanchor ());
1783 let getnav dir =
1784 let anchor = cbgetc state.hists.nav dir in
1785 getanchory anchor;
1788 let gotoghyll y =
1789 let rec scroll f n a b =
1790 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1791 let snake f a b =
1792 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1793 if f < a
1794 then s (float f /. float a)
1795 else (
1796 if f > b
1797 then 1.0 -. s ((float (f-b) /. float (n-b)))
1798 else 1.0
1801 snake f a b
1802 and summa f n a b =
1803 (* courtesy:
1804 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1805 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1806 let iv1 = iv f in
1807 let ins = float a *. iv1
1808 and outs = float (n-b) *. iv1 in
1809 let ones = b - a in
1810 ins +. outs +. float ones
1812 let rec set (_N, _A, _B) y sy =
1813 let sum = summa 1.0 _N _A _B in
1814 let dy = float (y - sy) in
1815 state.ghyll <- (
1816 let rec gf n y1 o =
1817 if n >= _N
1818 then state.ghyll <- noghyll
1819 else
1820 let go n =
1821 let s = scroll n _N _A _B in
1822 let y1 = y1 +. ((s *. dy) /. sum) in
1823 gotoy_and_clear_text (truncate y1);
1824 state.ghyll <- gf (n+1) y1;
1826 match o with
1827 | None -> go n
1828 | Some y' -> set (_N/2, 0, 0) y' state.y
1830 gf 0 (float state.y)
1833 match conf.ghyllscroll with
1834 | None ->
1835 gotoy_and_clear_text y
1836 | Some nab ->
1837 if state.ghyll == noghyll
1838 then set nab y state.y
1839 else state.ghyll (Some y)
1842 let gotopage n top =
1843 let y, h = getpageyh n in
1844 let y = y + (truncate (top *. float h)) in
1845 gotoghyll y
1848 let gotopage1 n top =
1849 let y = getpagey n in
1850 let y = y + top in
1851 gotoghyll y
1854 let invalidate s f =
1855 state.layout <- [];
1856 state.pdims <- [];
1857 state.rects <- [];
1858 state.rects1 <- [];
1859 match state.geomcmds with
1860 | ps, [] when String.length ps = 0 ->
1861 f ();
1862 state.geomcmds <- s, [];
1864 | ps, [] ->
1865 state.geomcmds <- ps, [s, f];
1867 | ps, (s', _) :: rest when s' = s ->
1868 state.geomcmds <- ps, ((s, f) :: rest);
1870 | ps, cmds ->
1871 state.geomcmds <- ps, ((s, f) :: cmds);
1874 let opendoc path password =
1875 state.path <- path;
1876 state.password <- password;
1877 state.gen <- state.gen + 1;
1878 state.docinfo <- [];
1880 setaalevel conf.aalevel;
1881 Wsi.settitle ("llpp " ^ Filename.basename path);
1882 wcmd "open %s\000%s\000" path password;
1883 invalidate "reqlayout"
1884 (fun () ->
1885 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1888 let scalecolor c =
1889 let c = c *. conf.colorscale in
1890 (c, c, c);
1893 let scalecolor2 (r, g, b) =
1894 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1897 let docolumns = function
1898 | Csingle -> ()
1900 | Cmulti ((columns, coverA, coverB), _) ->
1901 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1902 let rec loop pageno pdimno pdim x y rowh pdims =
1903 let rec fixrow m = if m = pageno then () else
1904 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1905 if h < rowh
1906 then (
1907 let y = y + (rowh - h) / 2 in
1908 a.(m) <- (pdimno, x, y, pdim);
1910 fixrow (m+1)
1912 if pageno = state.pagecount
1913 then fixrow (((pageno - 1) / columns) * columns)
1914 else
1915 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1916 match pdims with
1917 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1918 pdimno+1, pdim, rest
1919 | _ ->
1920 pdimno, pdim, pdims
1922 let x, y, rowh' =
1923 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1924 then (
1925 (conf.winw - state.scrollw - w) / 2,
1926 y + rowh + conf.interpagespace, h
1928 else (
1929 if (pageno - coverA) mod columns = 0
1930 then 0, y + rowh + conf.interpagespace, h
1931 else x, y, max rowh h
1934 if pageno > 1 && (pageno - coverA) mod columns = 0
1935 then fixrow (pageno - columns);
1936 a.(pageno) <- (pdimno, x, y, pdim);
1937 let x = x + w + xoff*2 + conf.interpagespace in
1938 loop (pageno+1) pdimno pdim x y rowh' pdims
1940 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1941 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1943 | Csplit (c, _) ->
1944 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1945 let rec loop pageno pdimno pdim y pdims =
1946 if pageno = state.pagecount
1947 then ()
1948 else
1949 let pdimno, ((_, w, h, _) as pdim), pdims =
1950 match pdims with
1951 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1952 pdimno+1, pdim, rest
1953 | _ ->
1954 pdimno, pdim, pdims
1956 let cw = w / c in
1957 let rec loop1 n x y =
1958 if n = c then y else (
1959 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1960 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1963 let y = loop1 0 0 y in
1964 loop (pageno+1) pdimno pdim y pdims
1966 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1967 conf.columns <- Csplit (c, a);
1970 let represent () =
1971 docolumns conf.columns;
1972 state.maxy <- calcheight ();
1973 state.hscrollh <-
1974 if state.w <= conf.winw - state.scrollw
1975 then 0
1976 else state.scrollw
1978 match state.mode with
1979 | Birdseye (_, _, pageno, _, _) ->
1980 let y, h = getpageyh pageno in
1981 let top = (conf.winh - h) / 2 in
1982 gotoy (max 0 (y - top))
1983 | _ -> gotoanchor state.anchor
1986 let reshape w h =
1987 GlDraw.viewport 0 0 w h;
1988 let firsttime = state.geomcmds == firstgeomcmds in
1989 if not firsttime && nogeomcmds state.geomcmds
1990 then state.anchor <- getanchor ();
1992 conf.winw <- w;
1993 let w = truncate (float w *. conf.zoom) - state.scrollw in
1994 let w = max w 2 in
1995 conf.winh <- h;
1996 setfontsize fstate.fontsize;
1997 GlMat.mode `modelview;
1998 GlMat.load_identity ();
2000 GlMat.mode `projection;
2001 GlMat.load_identity ();
2002 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2003 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2004 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2006 let relx =
2007 if conf.zoom <= 1.0
2008 then 0.0
2009 else float state.x /. float state.w
2011 invalidate "geometry"
2012 (fun () ->
2013 state.w <- w;
2014 if not firsttime
2015 then state.x <- truncate (relx *. float w);
2016 let w =
2017 match conf.columns with
2018 | Csingle -> w
2019 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2020 | Csplit (c, _) -> w * c
2022 wcmd "geometry %d %d" w h);
2025 let enttext () =
2026 let len = String.length state.text in
2027 let drawstring s =
2028 let hscrollh =
2029 match state.mode with
2030 | Textentry _
2031 | View ->
2032 let h, _, _ = state.uioh#scrollpw in
2034 | _ -> 0
2036 let rect x w =
2037 GlDraw.rect
2038 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2039 (x+.w, float (conf.winh - hscrollh))
2042 let w = float (conf.winw - state.scrollw - 1) in
2043 if state.progress >= 0.0 && state.progress < 1.0
2044 then (
2045 GlDraw.color (0.3, 0.3, 0.3);
2046 let w1 = w *. state.progress in
2047 rect 0.0 w1;
2048 GlDraw.color (0.0, 0.0, 0.0);
2049 rect w1 (w-.w1)
2051 else (
2052 GlDraw.color (0.0, 0.0, 0.0);
2053 rect 0.0 w;
2056 GlDraw.color (1.0, 1.0, 1.0);
2057 drawstring fstate.fontsize
2058 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2060 let s =
2061 match state.mode with
2062 | Textentry ((prefix, text, _, _, _, _), _) ->
2063 let s =
2064 if len > 0
2065 then
2066 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2067 else
2068 Printf.sprintf "%s%s_" prefix text
2072 | _ -> state.text
2074 let s =
2075 if state.newerrmsgs
2076 then (
2077 if not (istextentry state.mode)
2078 then
2079 let s1 = "(press 'e' to review error messasges)" in
2080 if String.length s > 0 then s ^ " " ^ s1 else s1
2081 else s
2083 else s
2085 if String.length s > 0
2086 then drawstring s
2089 let gctiles () =
2090 let len = Queue.length state.tilelru in
2091 let rec loop qpos =
2092 if state.memused <= conf.memlimit
2093 then ()
2094 else (
2095 if qpos < len
2096 then
2097 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2098 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2099 let (_, pw, ph, _) = getpagedim n in
2101 gen = state.gen
2102 && colorspace = conf.colorspace
2103 && angle = conf.angle
2104 && pagew = pw
2105 && pageh = ph
2106 && (
2107 let layout =
2108 match state.throttle with
2109 | None ->
2110 if conf.preload
2111 then preloadlayout state.layout
2112 else state.layout
2113 | Some (layout, _, _) ->
2114 layout
2116 let x = col*conf.tilew
2117 and y = row*conf.tileh in
2118 tilevisible layout n x y
2120 then Queue.push lruitem state.tilelru
2121 else (
2122 wcmd "freetile %s" p;
2123 state.memused <- state.memused - s;
2124 state.uioh#infochanged Memused;
2125 Hashtbl.remove state.tilemap k;
2127 loop (qpos+1)
2130 loop 0
2133 let flushtiles () =
2134 Queue.iter (fun (k, p, s) ->
2135 wcmd "freetile %s" p;
2136 state.memused <- state.memused - s;
2137 state.uioh#infochanged Memused;
2138 Hashtbl.remove state.tilemap k;
2139 ) state.tilelru;
2140 Queue.clear state.tilelru;
2141 load state.layout;
2144 let logcurrently = function
2145 | Idle -> dolog "Idle"
2146 | Loading (l, gen) ->
2147 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2148 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2149 dolog
2150 "Tiling %d[%d,%d] page=%s cs=%s angle"
2151 l.pageno col row pageopaque
2152 (colorspace_to_string colorspace)
2154 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2155 angle gen conf.angle state.gen
2156 tilew tileh
2157 conf.tilew conf.tileh
2159 | Outlining _ ->
2160 dolog "outlining"
2163 let act cmds =
2164 (* dolog "%S" cmds; *)
2165 let op, args =
2166 let spacepos =
2167 try String.index cmds ' '
2168 with Not_found -> -1
2170 if spacepos = -1
2171 then cmds, ""
2172 else
2173 let l = String.length cmds in
2174 let op = String.sub cmds 0 spacepos in
2175 op, begin
2176 if l - spacepos < 2 then ""
2177 else String.sub cmds (spacepos+1) (l-spacepos-1)
2180 match op with
2181 | "clear" ->
2182 state.uioh#infochanged Pdim;
2183 state.pdims <- [];
2185 | "clearrects" ->
2186 state.rects <- state.rects1;
2187 G.postRedisplay "clearrects";
2189 | "continue" ->
2190 let n =
2191 try Scanf.sscanf args "%u" (fun n -> n)
2192 with exn ->
2193 dolog "error processing 'continue' %S: %s"
2194 cmds (Printexc.to_string exn);
2195 exit 1;
2197 state.pagecount <- n;
2198 begin match state.currently with
2199 | Outlining l ->
2200 state.currently <- Idle;
2201 state.outlines <- Array.of_list (List.rev l)
2202 | _ -> ()
2203 end;
2205 let cur, cmds = state.geomcmds in
2206 if String.length cur = 0
2207 then failwith "umpossible";
2209 begin match List.rev cmds with
2210 | [] ->
2211 state.geomcmds <- "", [];
2212 represent ();
2213 | (s, f) :: rest ->
2214 f ();
2215 state.geomcmds <- s, List.rev rest;
2216 end;
2217 if conf.maxwait = None
2218 then G.postRedisplay "continue";
2220 | "title" ->
2221 Wsi.settitle args
2223 | "msg" ->
2224 showtext ' ' args
2226 | "vmsg" ->
2227 if conf.verbose
2228 then showtext ' ' args
2230 | "progress" ->
2231 let progress, text =
2233 Scanf.sscanf args "%f %n"
2234 (fun f pos ->
2235 f, String.sub args pos (String.length args - pos))
2236 with exn ->
2237 dolog "error processing 'progress' %S: %s"
2238 cmds (Printexc.to_string exn);
2239 exit 1;
2241 state.text <- text;
2242 state.progress <- progress;
2243 G.postRedisplay "progress"
2245 | "firstmatch" ->
2246 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2248 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2249 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2250 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2251 with exn ->
2252 dolog "error processing 'firstmatch' %S: %s"
2253 cmds (Printexc.to_string exn);
2254 exit 1;
2256 let y = (getpagey pageno) + truncate y0 in
2257 addnav ();
2258 gotoy y;
2259 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2261 | "match" ->
2262 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2264 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2265 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2266 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2267 with exn ->
2268 dolog "error processing 'match' %S: %s"
2269 cmds (Printexc.to_string exn);
2270 exit 1;
2272 state.rects1 <-
2273 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2275 | "page" ->
2276 let pageopaque, t =
2278 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2279 with exn ->
2280 dolog "error processing 'page' %S: %s"
2281 cmds (Printexc.to_string exn);
2282 exit 1;
2284 begin match state.currently with
2285 | Loading (l, gen) ->
2286 vlog "page %d took %f sec" l.pageno t;
2287 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2288 begin match state.throttle with
2289 | None ->
2290 let preloadedpages =
2291 if conf.preload
2292 then preloadlayout state.layout
2293 else state.layout
2295 let evict () =
2296 let module IntSet =
2297 Set.Make (struct type t = int let compare = (-) end) in
2298 let set =
2299 List.fold_left (fun s l -> IntSet.add l.pageno s)
2300 IntSet.empty preloadedpages
2302 let evictedpages =
2303 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2304 if not (IntSet.mem pageno set)
2305 then (
2306 wcmd "freepage %s" opaque;
2307 key :: accu
2309 else accu
2310 ) state.pagemap []
2312 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2314 evict ();
2315 state.currently <- Idle;
2316 if gen = state.gen
2317 then (
2318 tilepage l.pageno pageopaque state.layout;
2319 load state.layout;
2320 load preloadedpages;
2321 if pagevisible state.layout l.pageno
2322 && layoutready state.layout
2323 then G.postRedisplay "page";
2326 | Some (layout, _, _) ->
2327 state.currently <- Idle;
2328 tilepage l.pageno pageopaque layout;
2329 load state.layout
2330 end;
2332 | _ ->
2333 dolog "Inconsistent loading state";
2334 logcurrently state.currently;
2335 exit 1
2338 | "tile" ->
2339 let (x, y, opaque, size, t) =
2341 Scanf.sscanf args "%u %u %s %u %f"
2342 (fun x y p size t -> (x, y, p, size, t))
2343 with exn ->
2344 dolog "error processing 'tile' %S: %s"
2345 cmds (Printexc.to_string exn);
2346 exit 1;
2348 begin match state.currently with
2349 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2350 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2352 if tilew != conf.tilew || tileh != conf.tileh
2353 then (
2354 wcmd "freetile %s" opaque;
2355 state.currently <- Idle;
2356 load state.layout;
2358 else (
2359 puttileopaque l col row gen cs angle opaque size t;
2360 state.memused <- state.memused + size;
2361 state.uioh#infochanged Memused;
2362 gctiles ();
2363 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2364 opaque, size) state.tilelru;
2366 let layout =
2367 match state.throttle with
2368 | None -> state.layout
2369 | Some (layout, _, _) -> layout
2372 state.currently <- Idle;
2373 if gen = state.gen
2374 && conf.colorspace = cs
2375 && conf.angle = angle
2376 && tilevisible layout l.pageno x y
2377 then conttiling l.pageno pageopaque;
2379 begin match state.throttle with
2380 | None ->
2381 preload state.layout;
2382 if gen = state.gen
2383 && conf.colorspace = cs
2384 && conf.angle = angle
2385 && tilevisible state.layout l.pageno x y
2386 then G.postRedisplay "tile nothrottle";
2388 | Some (layout, y, _) ->
2389 let ready = layoutready layout in
2390 if ready
2391 then (
2392 state.y <- y;
2393 state.layout <- layout;
2394 state.throttle <- None;
2395 G.postRedisplay "throttle";
2397 else load layout;
2398 end;
2401 | _ ->
2402 dolog "Inconsistent tiling state";
2403 logcurrently state.currently;
2404 exit 1
2407 | "pdim" ->
2408 let pdim =
2410 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2411 with exn ->
2412 dolog "error processing 'pdim' %S: %s"
2413 cmds (Printexc.to_string exn);
2414 exit 1;
2416 state.uioh#infochanged Pdim;
2417 state.pdims <- pdim :: state.pdims
2419 | "o" ->
2420 let (l, n, t, h, pos) =
2422 Scanf.sscanf args "%u %u %d %u %n"
2423 (fun l n t h pos -> l, n, t, h, pos)
2424 with exn ->
2425 dolog "error processing 'o' %S: %s"
2426 cmds (Printexc.to_string exn);
2427 exit 1;
2429 let s = String.sub args pos (String.length args - pos) in
2430 let outline = (s, l, (n, float t /. float h)) in
2431 begin match state.currently with
2432 | Outlining outlines ->
2433 state.currently <- Outlining (outline :: outlines)
2434 | Idle ->
2435 state.currently <- Outlining [outline]
2436 | currently ->
2437 dolog "invalid outlining state";
2438 logcurrently currently
2441 | "info" ->
2442 state.docinfo <- (1, args) :: state.docinfo
2444 | "infoend" ->
2445 state.uioh#infochanged Docinfo;
2446 state.docinfo <- List.rev state.docinfo
2448 | _ ->
2449 dolog "unknown cmd `%S'" cmds
2452 let onhist cb =
2453 let rc = cb.rc in
2454 let action = function
2455 | HCprev -> cbget cb ~-1
2456 | HCnext -> cbget cb 1
2457 | HCfirst -> cbget cb ~-(cb.rc)
2458 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2459 and cancel () = cb.rc <- rc
2460 in (action, cancel)
2463 let search pattern forward =
2464 if String.length pattern > 0
2465 then
2466 let pn, py =
2467 match state.layout with
2468 | [] -> 0, 0
2469 | l :: _ ->
2470 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2472 wcmd "search %d %d %d %d,%s\000"
2473 (btod conf.icase) pn py (btod forward) pattern;
2476 let intentry text key =
2477 let c =
2478 if key >= 32 && key < 127
2479 then Char.chr key
2480 else '\000'
2482 match c with
2483 | '0' .. '9' ->
2484 let text = addchar text c in
2485 TEcont text
2487 | _ ->
2488 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2489 TEcont text
2492 let linknentry text key =
2493 let c =
2494 if key >= 32 && key < 127
2495 then Char.chr key
2496 else '\000'
2498 match c with
2499 | 'a' .. 'z' ->
2500 let text = addchar text c in
2501 TEcont text
2503 | _ ->
2504 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2505 TEcont text
2508 let linkndone f s =
2509 if String.length s > 0
2510 then (
2511 let n =
2512 let l = String.length s in
2513 let rec loop pos n = if pos = l then n else
2514 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2515 loop (pos+1) (n*26 + m)
2516 in loop 0 0
2518 let rec loop n = function
2519 | [] -> ()
2520 | l :: rest ->
2521 match getopaque l.pageno with
2522 | None -> loop n rest
2523 | Some opaque ->
2524 let m = getlinkcount opaque in
2525 if n < m
2526 then (
2527 let under = getlink opaque n in
2528 f under
2530 else loop (n-m) rest
2532 loop n state.layout;
2536 let textentry text key =
2537 if key land 0xff00 = 0xff00
2538 then TEcont text
2539 else TEcont (text ^ Wsi.toutf8 key)
2542 let reqlayout angle proportional =
2543 match state.throttle with
2544 | None ->
2545 if nogeomcmds state.geomcmds
2546 then state.anchor <- getanchor ();
2547 conf.angle <- angle mod 360;
2548 if conf.angle != 0
2549 then (
2550 match state.mode with
2551 | LinkNav _ -> state.mode <- View
2552 | _ -> ()
2554 conf.proportional <- proportional;
2555 invalidate "reqlayout"
2556 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2557 | _ -> ()
2560 let settrim trimmargins trimfuzz =
2561 if nogeomcmds state.geomcmds
2562 then state.anchor <- getanchor ();
2563 conf.trimmargins <- trimmargins;
2564 conf.trimfuzz <- trimfuzz;
2565 let x0, y0, x1, y1 = trimfuzz in
2566 invalidate "settrim"
2567 (fun () ->
2568 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2569 Hashtbl.iter (fun _ opaque ->
2570 wcmd "freepage %s" opaque;
2571 ) state.pagemap;
2572 Hashtbl.clear state.pagemap;
2575 let setzoom zoom =
2576 match state.throttle with
2577 | None ->
2578 let zoom = max 0.01 zoom in
2579 if zoom <> conf.zoom
2580 then (
2581 state.prevzoom <- conf.zoom;
2582 conf.zoom <- zoom;
2583 reshape conf.winw conf.winh;
2584 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2587 | Some (layout, y, started) ->
2588 let time =
2589 match conf.maxwait with
2590 | None -> 0.0
2591 | Some t -> t
2593 let dt = now () -. started in
2594 if dt > time
2595 then (
2596 state.y <- y;
2597 load layout;
2601 let setcolumns mode columns coverA coverB =
2602 state.prevcolumns <- Some (conf.columns, conf.zoom);
2603 if columns < 0
2604 then (
2605 if isbirdseye mode
2606 then showtext '!' "split mode doesn't work in bird's eye"
2607 else (
2608 conf.columns <- Csplit (-columns, [||]);
2609 state.x <- 0;
2610 conf.zoom <- 1.0;
2613 else (
2614 if columns < 2
2615 then (
2616 conf.columns <- Csingle;
2617 state.x <- 0;
2618 setzoom 1.0;
2620 else (
2621 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2622 conf.zoom <- 1.0;
2625 reshape conf.winw conf.winh;
2628 let enterbirdseye () =
2629 let zoom = float conf.thumbw /. float conf.winw in
2630 let birdseyepageno =
2631 let cy = conf.winh / 2 in
2632 let fold = function
2633 | [] -> 0
2634 | l :: rest ->
2635 let rec fold best = function
2636 | [] -> best.pageno
2637 | l :: rest ->
2638 let d = cy - (l.pagedispy + l.pagevh/2)
2639 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2640 if abs d < abs dbest
2641 then fold l rest
2642 else best.pageno
2643 in fold l rest
2645 fold state.layout
2647 state.mode <- Birdseye (
2648 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2650 conf.zoom <- zoom;
2651 conf.presentation <- false;
2652 conf.interpagespace <- 10;
2653 conf.hlinks <- false;
2654 state.x <- 0;
2655 state.mstate <- Mnone;
2656 conf.maxwait <- None;
2657 conf.columns <- (
2658 match conf.beyecolumns with
2659 | Some c ->
2660 conf.zoom <- 1.0;
2661 Cmulti ((c, 0, 0), [||])
2662 | None -> Csingle
2664 Wsi.setcursor Wsi.CURSOR_INHERIT;
2665 if conf.verbose
2666 then
2667 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2668 (100.0*.zoom)
2669 else
2670 state.text <- ""
2672 reshape conf.winw conf.winh;
2675 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2676 state.mode <- View;
2677 conf.zoom <- c.zoom;
2678 conf.presentation <- c.presentation;
2679 conf.interpagespace <- c.interpagespace;
2680 conf.maxwait <- c.maxwait;
2681 conf.hlinks <- c.hlinks;
2682 conf.beyecolumns <- (
2683 match conf.columns with
2684 | Cmulti ((c, _, _), _) -> Some c
2685 | Csingle -> None
2686 | Csplit _ -> failwith "leaving bird's eye split mode"
2688 conf.columns <- (
2689 match c.columns with
2690 | Cmulti (c, _) -> Cmulti (c, [||])
2691 | Csingle -> Csingle
2692 | Csplit (c, _) -> Csplit (c, [||])
2694 state.x <- leftx;
2695 if conf.verbose
2696 then
2697 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2698 (100.0*.conf.zoom)
2700 reshape conf.winw conf.winh;
2701 state.anchor <- if goback then anchor else (pageno, 0.0);
2704 let togglebirdseye () =
2705 match state.mode with
2706 | Birdseye vals -> leavebirdseye vals true
2707 | View -> enterbirdseye ()
2708 | _ -> ()
2711 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2712 let pageno = max 0 (pageno - incr) in
2713 let rec loop = function
2714 | [] -> gotopage1 pageno 0
2715 | l :: _ when l.pageno = pageno ->
2716 if l.pagedispy >= 0 && l.pagey = 0
2717 then G.postRedisplay "upbirdseye"
2718 else gotopage1 pageno 0
2719 | _ :: rest -> loop rest
2721 loop state.layout;
2722 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2725 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2726 let pageno = min (state.pagecount - 1) (pageno + incr) in
2727 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2728 let rec loop = function
2729 | [] ->
2730 let y, h = getpageyh pageno in
2731 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2732 gotoy (clamp dy)
2733 | l :: _ when l.pageno = pageno ->
2734 if l.pagevh != l.pageh
2735 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2736 else G.postRedisplay "downbirdseye"
2737 | _ :: rest -> loop rest
2739 loop state.layout
2742 let optentry mode _ key =
2743 let btos b = if b then "on" else "off" in
2744 if key >= 32 && key < 127
2745 then
2746 let c = Char.chr key in
2747 match c with
2748 | 's' ->
2749 let ondone s =
2750 try conf.scrollstep <- int_of_string s with exc ->
2751 state.text <- Printf.sprintf "bad integer `%s': %s"
2752 s (Printexc.to_string exc)
2754 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2756 | 'A' ->
2757 let ondone s =
2759 conf.autoscrollstep <- int_of_string s;
2760 if state.autoscroll <> None
2761 then state.autoscroll <- Some conf.autoscrollstep
2762 with exc ->
2763 state.text <- Printf.sprintf "bad integer `%s': %s"
2764 s (Printexc.to_string exc)
2766 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2768 | 'C' ->
2769 let mode = state.mode in
2770 let ondone s =
2772 let n, a, b = multicolumns_of_string s in
2773 setcolumns mode n a b;
2774 with exc ->
2775 state.text <- Printf.sprintf "bad columns `%s': %s"
2776 s (Printexc.to_string exc)
2778 TEswitch ("columns: ", "", None, textentry, ondone, true)
2780 | 'Z' ->
2781 let ondone s =
2783 let zoom = float (int_of_string s) /. 100.0 in
2784 setzoom zoom
2785 with exc ->
2786 state.text <- Printf.sprintf "bad integer `%s': %s"
2787 s (Printexc.to_string exc)
2789 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2791 | 't' ->
2792 let ondone s =
2794 conf.thumbw <- bound (int_of_string s) 2 4096;
2795 state.text <-
2796 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2797 begin match mode with
2798 | Birdseye beye ->
2799 leavebirdseye beye false;
2800 enterbirdseye ();
2801 | _ -> ();
2803 with exc ->
2804 state.text <- Printf.sprintf "bad integer `%s': %s"
2805 s (Printexc.to_string exc)
2807 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2809 | 'R' ->
2810 let ondone s =
2811 match try
2812 Some (int_of_string s)
2813 with exc ->
2814 state.text <- Printf.sprintf "bad integer `%s': %s"
2815 s (Printexc.to_string exc);
2816 None
2817 with
2818 | Some angle -> reqlayout angle conf.proportional
2819 | None -> ()
2821 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2823 | 'i' ->
2824 conf.icase <- not conf.icase;
2825 TEdone ("case insensitive search " ^ (btos conf.icase))
2827 | 'p' ->
2828 conf.preload <- not conf.preload;
2829 gotoy state.y;
2830 TEdone ("preload " ^ (btos conf.preload))
2832 | 'v' ->
2833 conf.verbose <- not conf.verbose;
2834 TEdone ("verbose " ^ (btos conf.verbose))
2836 | 'd' ->
2837 conf.debug <- not conf.debug;
2838 TEdone ("debug " ^ (btos conf.debug))
2840 | 'h' ->
2841 conf.maxhfit <- not conf.maxhfit;
2842 state.maxy <- calcheight ();
2843 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2845 | 'c' ->
2846 conf.crophack <- not conf.crophack;
2847 TEdone ("crophack " ^ btos conf.crophack)
2849 | 'a' ->
2850 let s =
2851 match conf.maxwait with
2852 | None ->
2853 conf.maxwait <- Some infinity;
2854 "always wait for page to complete"
2855 | Some _ ->
2856 conf.maxwait <- None;
2857 "show placeholder if page is not ready"
2859 TEdone s
2861 | 'f' ->
2862 conf.underinfo <- not conf.underinfo;
2863 TEdone ("underinfo " ^ btos conf.underinfo)
2865 | 'P' ->
2866 conf.savebmarks <- not conf.savebmarks;
2867 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2869 | 'S' ->
2870 let ondone s =
2872 let pageno, py =
2873 match state.layout with
2874 | [] -> 0, 0
2875 | l :: _ ->
2876 l.pageno, l.pagey
2878 conf.interpagespace <- int_of_string s;
2879 docolumns conf.columns;
2880 state.maxy <- calcheight ();
2881 let y = getpagey pageno in
2882 gotoy (y + py)
2883 with exc ->
2884 state.text <- Printf.sprintf "bad integer `%s': %s"
2885 s (Printexc.to_string exc)
2887 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2889 | 'l' ->
2890 reqlayout conf.angle (not conf.proportional);
2891 TEdone ("proportional display " ^ btos conf.proportional)
2893 | 'T' ->
2894 settrim (not conf.trimmargins) conf.trimfuzz;
2895 TEdone ("trim margins " ^ btos conf.trimmargins)
2897 | 'I' ->
2898 conf.invert <- not conf.invert;
2899 TEdone ("invert colors " ^ btos conf.invert)
2901 | 'x' ->
2902 let ondone s =
2903 cbput state.hists.sel s;
2904 conf.selcmd <- s;
2906 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2907 textentry, ondone, true)
2909 | _ ->
2910 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2911 TEstop
2912 else
2913 TEcont state.text
2916 class type lvsource = object
2917 method getitemcount : int
2918 method getitem : int -> (string * int)
2919 method hasaction : int -> bool
2920 method exit :
2921 uioh:uioh ->
2922 cancel:bool ->
2923 active:int ->
2924 first:int ->
2925 pan:int ->
2926 qsearch:string ->
2927 uioh option
2928 method getactive : int
2929 method getfirst : int
2930 method getqsearch : string
2931 method setqsearch : string -> unit
2932 method getpan : int
2933 end;;
2935 class virtual lvsourcebase = object
2936 val mutable m_active = 0
2937 val mutable m_first = 0
2938 val mutable m_qsearch = ""
2939 val mutable m_pan = 0
2940 method getactive = m_active
2941 method getfirst = m_first
2942 method getqsearch = m_qsearch
2943 method getpan = m_pan
2944 method setqsearch s = m_qsearch <- s
2945 end;;
2947 let withoutlastutf8 s =
2948 let len = String.length s in
2949 if len = 0
2950 then s
2951 else
2952 let rec find pos =
2953 if pos = 0
2954 then pos
2955 else
2956 let b = Char.code s.[pos] in
2957 if b land 0b110000 = 0b11000000
2958 then find (pos-1)
2959 else pos-1
2961 let first =
2962 if Char.code s.[len-1] land 0x80 = 0
2963 then len-1
2964 else find (len-1)
2966 String.sub s 0 first;
2969 let textentrykeyboard
2970 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2971 let enttext te =
2972 state.mode <- Textentry (te, onleave);
2973 state.text <- "";
2974 enttext ();
2975 G.postRedisplay "textentrykeyboard enttext";
2977 let histaction cmd =
2978 match opthist with
2979 | None -> ()
2980 | Some (action, _) ->
2981 state.mode <- Textentry (
2982 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2984 G.postRedisplay "textentry histaction"
2986 match key with
2987 | 0xff08 -> (* backspace *)
2988 let s = withoutlastutf8 text in
2989 let len = String.length s in
2990 if cancelonempty && len = 0
2991 then (
2992 onleave Cancel;
2993 G.postRedisplay "textentrykeyboard after cancel";
2995 else (
2996 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2999 | 0xff0d ->
3000 ondone text;
3001 onleave Confirm;
3002 G.postRedisplay "textentrykeyboard after confirm"
3004 | 0xff52 -> histaction HCprev
3005 | 0xff54 -> histaction HCnext
3006 | 0xff50 -> histaction HCfirst
3007 | 0xff57 -> histaction HClast
3009 | 0xff1b -> (* escape*)
3010 if String.length text = 0
3011 then (
3012 begin match opthist with
3013 | None -> ()
3014 | Some (_, onhistcancel) -> onhistcancel ()
3015 end;
3016 onleave Cancel;
3017 state.text <- "";
3018 G.postRedisplay "textentrykeyboard after cancel2"
3020 else (
3021 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3024 | 0xff9f | 0xffff -> () (* delete *)
3026 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3027 begin match onkey text key with
3028 | TEdone text ->
3029 ondone text;
3030 onleave Confirm;
3031 G.postRedisplay "textentrykeyboard after confirm2";
3033 | TEcont text ->
3034 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3036 | TEstop ->
3037 onleave Cancel;
3038 G.postRedisplay "textentrykeyboard after cancel3"
3040 | TEswitch te ->
3041 state.mode <- Textentry (te, onleave);
3042 G.postRedisplay "textentrykeyboard switch";
3043 end;
3045 | _ ->
3046 vlog "unhandled key %s" (Wsi.keyname key)
3049 let firstof first active =
3050 if first > active || abs (first - active) > fstate.maxrows - 1
3051 then max 0 (active - (fstate.maxrows/2))
3052 else first
3055 let calcfirst first active =
3056 if active > first
3057 then
3058 let rows = active - first in
3059 if rows > fstate.maxrows then active - fstate.maxrows else first
3060 else active
3063 let scrollph y maxy =
3064 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3065 let sh = float conf.winh /. sh in
3066 let sh = max sh (float conf.scrollh) in
3068 let percent =
3069 if y = state.maxy
3070 then 1.0
3071 else float y /. float maxy
3073 let position = (float conf.winh -. sh) *. percent in
3075 let position =
3076 if position +. sh > float conf.winh
3077 then float conf.winh -. sh
3078 else position
3080 position, sh;
3083 let coe s = (s :> uioh);;
3085 class listview ~(source:lvsource) ~trusted ~modehash =
3086 object (self)
3087 val m_pan = source#getpan
3088 val m_first = source#getfirst
3089 val m_active = source#getactive
3090 val m_qsearch = source#getqsearch
3091 val m_prev_uioh = state.uioh
3093 method private elemunder y =
3094 let n = y / (fstate.fontsize+1) in
3095 if m_first + n < source#getitemcount
3096 then (
3097 if source#hasaction (m_first + n)
3098 then Some (m_first + n)
3099 else None
3101 else None
3103 method display =
3104 Gl.enable `blend;
3105 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3106 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3107 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3108 GlDraw.color (1., 1., 1.);
3109 Gl.enable `texture_2d;
3110 let fs = fstate.fontsize in
3111 let nfs = fs + 1 in
3112 let ww = fstate.wwidth in
3113 let tabw = 30.0*.ww in
3114 let itemcount = source#getitemcount in
3115 let rec loop row =
3116 if (row - m_first) * nfs > conf.winh
3117 then ()
3118 else (
3119 if row >= 0 && row < itemcount
3120 then (
3121 let (s, level) = source#getitem row in
3122 let y = (row - m_first) * nfs in
3123 let x = 5.0 +. float (level + m_pan) *. ww in
3124 if row = m_active
3125 then (
3126 Gl.disable `texture_2d;
3127 GlDraw.polygon_mode `both `line;
3128 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3129 GlDraw.rect (1., float (y + 1))
3130 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3131 GlDraw.polygon_mode `both `fill;
3132 GlDraw.color (1., 1., 1.);
3133 Gl.enable `texture_2d;
3136 let drawtabularstring s =
3137 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3138 if trusted
3139 then
3140 let tabpos = try String.index s '\t' with Not_found -> -1 in
3141 if tabpos > 0
3142 then
3143 let len = String.length s - tabpos - 1 in
3144 let s1 = String.sub s 0 tabpos
3145 and s2 = String.sub s (tabpos + 1) len in
3146 let nx = drawstr x s1 in
3147 let sw = nx -. x in
3148 let x = x +. (max tabw sw) in
3149 drawstr x s2
3150 else
3151 drawstr x s
3152 else
3153 drawstr x s
3155 let _ = drawtabularstring s in
3156 loop (row+1)
3160 loop m_first;
3161 Gl.disable `blend;
3162 Gl.disable `texture_2d;
3164 method updownlevel incr =
3165 let len = source#getitemcount in
3166 let curlevel =
3167 if m_active >= 0 && m_active < len
3168 then snd (source#getitem m_active)
3169 else -1
3171 let rec flow i =
3172 if i = len then i-1 else if i = -1 then 0 else
3173 let _, l = source#getitem i in
3174 if l != curlevel then i else flow (i+incr)
3176 let active = flow m_active in
3177 let first = calcfirst m_first active in
3178 G.postRedisplay "outline updownlevel";
3179 {< m_active = active; m_first = first >}
3181 method private key1 key mask =
3182 let set1 active first qsearch =
3183 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3185 let search active pattern incr =
3186 let dosearch re =
3187 let rec loop n =
3188 if n >= 0 && n < source#getitemcount
3189 then (
3190 let s, _ = source#getitem n in
3192 (try ignore (Str.search_forward re s 0); true
3193 with Not_found -> false)
3194 then Some n
3195 else loop (n + incr)
3197 else None
3199 loop active
3202 let re = Str.regexp_case_fold pattern in
3203 dosearch re
3204 with Failure s ->
3205 state.text <- s;
3206 None
3208 let itemcount = source#getitemcount in
3209 let find start incr =
3210 let rec find i =
3211 if i = -1 || i = itemcount
3212 then -1
3213 else (
3214 if source#hasaction i
3215 then i
3216 else find (i + incr)
3219 find start
3221 let set active first =
3222 let first = bound first 0 (itemcount - fstate.maxrows) in
3223 state.text <- "";
3224 coe {< m_active = active; m_first = first >}
3226 let navigate incr =
3227 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3228 let active, first =
3229 let incr1 = if incr > 0 then 1 else -1 in
3230 if isvisible m_first m_active
3231 then
3232 let next =
3233 let next = m_active + incr in
3234 let next =
3235 if next < 0 || next >= itemcount
3236 then -1
3237 else find next incr1
3239 if next = -1 || abs (m_active - next) > fstate.maxrows
3240 then -1
3241 else next
3243 if next = -1
3244 then
3245 let first = m_first + incr in
3246 let first = bound first 0 (itemcount - 1) in
3247 let next =
3248 let next = m_active + incr in
3249 let next = bound next 0 (itemcount - 1) in
3250 find next ~-incr1
3252 let active = if next = -1 then m_active else next in
3253 active, first
3254 else
3255 let first = min next m_first in
3256 let first =
3257 if abs (next - first) > fstate.maxrows
3258 then first + incr
3259 else first
3261 next, first
3262 else
3263 let first = m_first + incr in
3264 let first = bound first 0 (itemcount - 1) in
3265 let active =
3266 let next = m_active + incr in
3267 let next = bound next 0 (itemcount - 1) in
3268 let next = find next incr1 in
3269 let active =
3270 if next = -1 || abs (m_active - first) > fstate.maxrows
3271 then (
3272 let active = if m_active = -1 then next else m_active in
3273 active
3275 else next
3277 if isvisible first active
3278 then active
3279 else -1
3281 active, first
3283 G.postRedisplay "listview navigate";
3284 set active first;
3286 match key with
3287 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3288 let incr = if key = 0x72 then -1 else 1 in
3289 let active, first =
3290 match search (m_active + incr) m_qsearch incr with
3291 | None ->
3292 state.text <- m_qsearch ^ " [not found]";
3293 m_active, m_first
3294 | Some active ->
3295 state.text <- m_qsearch;
3296 active, firstof m_first active
3298 G.postRedisplay "listview ctrl-r/s";
3299 set1 active first m_qsearch;
3301 | 0xff08 -> (* backspace *)
3302 if String.length m_qsearch = 0
3303 then coe self
3304 else (
3305 let qsearch = withoutlastutf8 m_qsearch in
3306 let len = String.length qsearch in
3307 if len = 0
3308 then (
3309 state.text <- "";
3310 G.postRedisplay "listview empty qsearch";
3311 set1 m_active m_first "";
3313 else
3314 let active, first =
3315 match search m_active qsearch ~-1 with
3316 | None ->
3317 state.text <- qsearch ^ " [not found]";
3318 m_active, m_first
3319 | Some active ->
3320 state.text <- qsearch;
3321 active, firstof m_first active
3323 G.postRedisplay "listview backspace qsearch";
3324 set1 active first qsearch
3327 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3328 let pattern = m_qsearch ^ Wsi.toutf8 key in
3329 let active, first =
3330 match search m_active pattern 1 with
3331 | None ->
3332 state.text <- pattern ^ " [not found]";
3333 m_active, m_first
3334 | Some active ->
3335 state.text <- pattern;
3336 active, firstof m_first active
3338 G.postRedisplay "listview qsearch add";
3339 set1 active first pattern;
3341 | 0xff1b -> (* escape *)
3342 state.text <- "";
3343 if String.length m_qsearch = 0
3344 then (
3345 G.postRedisplay "list view escape";
3346 begin
3347 match
3348 source#exit (coe self) true m_active m_first m_pan m_qsearch
3349 with
3350 | None -> m_prev_uioh
3351 | Some uioh -> uioh
3354 else (
3355 G.postRedisplay "list view kill qsearch";
3356 source#setqsearch "";
3357 coe {< m_qsearch = "" >}
3360 | 0xff0d -> (* return *)
3361 state.text <- "";
3362 let self = {< m_qsearch = "" >} in
3363 source#setqsearch "";
3364 let opt =
3365 G.postRedisplay "listview enter";
3366 if m_active >= 0 && m_active < source#getitemcount
3367 then (
3368 source#exit (coe self) false m_active m_first m_pan "";
3370 else (
3371 source#exit (coe self) true m_active m_first m_pan "";
3374 begin match opt with
3375 | None -> m_prev_uioh
3376 | Some uioh -> uioh
3379 | 0xff9f | 0xffff -> (* delete *)
3380 coe self
3382 | 0xff52 -> navigate ~-1 (* up *)
3383 | 0xff54 -> navigate 1 (* down *)
3384 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3385 | 0xff56 -> navigate fstate.maxrows (* next *)
3387 | 0xff53 -> (* right *)
3388 state.text <- "";
3389 G.postRedisplay "listview right";
3390 coe {< m_pan = m_pan - 1 >}
3392 | 0xff51 -> (* left *)
3393 state.text <- "";
3394 G.postRedisplay "listview left";
3395 coe {< m_pan = m_pan + 1 >}
3397 | 0xff50 -> (* home *)
3398 let active = find 0 1 in
3399 G.postRedisplay "listview home";
3400 set active 0;
3402 | 0xff57 -> (* end *)
3403 let first = max 0 (itemcount - fstate.maxrows) in
3404 let active = find (itemcount - 1) ~-1 in
3405 G.postRedisplay "listview end";
3406 set active first;
3408 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3409 coe self
3411 | _ ->
3412 dolog "listview unknown key %#x" key; coe self
3414 method key key mask =
3415 match state.mode with
3416 | Textentry te -> textentrykeyboard key mask te; coe self
3417 | _ -> self#key1 key mask
3419 method button button down x y _ =
3420 let opt =
3421 match button with
3422 | 1 when x > conf.winw - conf.scrollbw ->
3423 G.postRedisplay "listview scroll";
3424 if down
3425 then
3426 let _, position, sh = self#scrollph in
3427 if y > truncate position && y < truncate (position +. sh)
3428 then (
3429 state.mstate <- Mscrolly;
3430 Some (coe self)
3432 else
3433 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3434 let first = truncate (s *. float source#getitemcount) in
3435 let first = min source#getitemcount first in
3436 Some (coe {< m_first = first; m_active = first >})
3437 else (
3438 state.mstate <- Mnone;
3439 Some (coe self);
3441 | 1 when not down ->
3442 begin match self#elemunder y with
3443 | Some n ->
3444 G.postRedisplay "listview click";
3445 source#exit
3446 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3447 | _ ->
3448 Some (coe self)
3450 | n when (n == 4 || n == 5) && not down ->
3451 let len = source#getitemcount in
3452 let first =
3453 if n = 5 && m_first + fstate.maxrows >= len
3454 then
3455 m_first
3456 else
3457 let first = m_first + (if n == 4 then -1 else 1) in
3458 bound first 0 (len - 1)
3460 G.postRedisplay "listview wheel";
3461 Some (coe {< m_first = first >})
3462 | _ ->
3463 Some (coe self)
3465 match opt with
3466 | None -> m_prev_uioh
3467 | Some uioh -> uioh
3469 method motion _ y =
3470 match state.mstate with
3471 | Mscrolly ->
3472 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3473 let first = truncate (s *. float source#getitemcount) in
3474 let first = min source#getitemcount first in
3475 G.postRedisplay "listview motion";
3476 coe {< m_first = first; m_active = first >}
3477 | _ -> coe self
3479 method pmotion x y =
3480 if x < conf.winw - conf.scrollbw
3481 then
3482 let n =
3483 match self#elemunder y with
3484 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3485 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3487 let o =
3488 if n != m_active
3489 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3490 else self
3492 coe o
3493 else (
3494 Wsi.setcursor Wsi.CURSOR_INHERIT;
3495 coe self
3498 method infochanged _ = ()
3500 method scrollpw = (0, 0.0, 0.0)
3501 method scrollph =
3502 let nfs = fstate.fontsize + 1 in
3503 let y = m_first * nfs in
3504 let itemcount = source#getitemcount in
3505 let maxi = max 0 (itemcount - fstate.maxrows) in
3506 let maxy = maxi * nfs in
3507 let p, h = scrollph y maxy in
3508 conf.scrollbw, p, h
3510 method modehash = modehash
3511 end;;
3513 class outlinelistview ~source =
3514 object (self)
3515 inherit listview
3516 ~source:(source :> lvsource)
3517 ~trusted:false
3518 ~modehash:(findkeyhash conf "outline")
3519 as super
3521 method key key mask =
3522 let calcfirst first active =
3523 if active > first
3524 then
3525 let rows = active - first in
3526 if rows > fstate.maxrows then active - fstate.maxrows else first
3527 else active
3529 let navigate incr =
3530 let active = m_active + incr in
3531 let active = bound active 0 (source#getitemcount - 1) in
3532 let first = calcfirst m_first active in
3533 G.postRedisplay "outline navigate";
3534 coe {< m_active = active; m_first = first >}
3536 let ctrl = Wsi.withctrl mask in
3537 match key with
3538 | 110 when ctrl -> (* ctrl-n *)
3539 source#narrow m_qsearch;
3540 G.postRedisplay "outline ctrl-n";
3541 coe {< m_first = 0; m_active = 0 >}
3543 | 117 when ctrl -> (* ctrl-u *)
3544 source#denarrow;
3545 G.postRedisplay "outline ctrl-u";
3546 state.text <- "";
3547 coe {< m_first = 0; m_active = 0 >}
3549 | 108 when ctrl -> (* ctrl-l *)
3550 let first = m_active - (fstate.maxrows / 2) in
3551 G.postRedisplay "outline ctrl-l";
3552 coe {< m_first = first >}
3554 | 0xff9f | 0xffff -> (* delete *)
3555 source#remove m_active;
3556 G.postRedisplay "outline delete";
3557 let active = max 0 (m_active-1) in
3558 coe {< m_first = firstof m_first active;
3559 m_active = active >}
3561 | 0xff52 -> navigate ~-1 (* up *)
3562 | 0xff54 -> navigate 1 (* down *)
3563 | 0xff55 -> (* prior *)
3564 navigate ~-(fstate.maxrows)
3565 | 0xff56 -> (* next *)
3566 navigate fstate.maxrows
3568 | 0xff53 -> (* [ctrl-]right *)
3569 let o =
3570 if ctrl
3571 then (
3572 G.postRedisplay "outline ctrl right";
3573 {< m_pan = m_pan + 1 >}
3575 else self#updownlevel 1
3577 coe o
3579 | 0xff51 -> (* [ctrl-]left *)
3580 let o =
3581 if ctrl
3582 then (
3583 G.postRedisplay "outline ctrl left";
3584 {< m_pan = m_pan - 1 >}
3586 else self#updownlevel ~-1
3588 coe o
3590 | 0xff50 -> (* home *)
3591 G.postRedisplay "outline home";
3592 coe {< m_first = 0; m_active = 0 >}
3594 | 0xff57 -> (* end *)
3595 let active = source#getitemcount - 1 in
3596 let first = max 0 (active - fstate.maxrows) in
3597 G.postRedisplay "outline end";
3598 coe {< m_active = active; m_first = first >}
3600 | _ -> super#key key mask
3603 let outlinesource usebookmarks =
3604 let empty = [||] in
3605 (object
3606 inherit lvsourcebase
3607 val mutable m_items = empty
3608 val mutable m_orig_items = empty
3609 val mutable m_prev_items = empty
3610 val mutable m_narrow_pattern = ""
3611 val mutable m_hadremovals = false
3613 method getitemcount =
3614 Array.length m_items + (if m_hadremovals then 1 else 0)
3616 method getitem n =
3617 if n == Array.length m_items && m_hadremovals
3618 then
3619 ("[Confirm removal]", 0)
3620 else
3621 let s, n, _ = m_items.(n) in
3622 (s, n)
3624 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3625 ignore (uioh, first, qsearch);
3626 let confrimremoval = m_hadremovals && active = Array.length m_items in
3627 let items =
3628 if String.length m_narrow_pattern = 0
3629 then m_orig_items
3630 else m_items
3632 if not cancel
3633 then (
3634 if not confrimremoval
3635 then(
3636 let _, _, anchor = m_items.(active) in
3637 gotoanchor anchor;
3638 m_items <- items;
3640 else (
3641 state.bookmarks <- Array.to_list m_items;
3642 m_orig_items <- m_items;
3645 else m_items <- items;
3646 m_pan <- pan;
3647 None
3649 method hasaction _ = true
3651 method greetmsg =
3652 if Array.length m_items != Array.length m_orig_items
3653 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3654 else ""
3656 method narrow pattern =
3657 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3658 match reopt with
3659 | None -> ()
3660 | Some re ->
3661 let rec loop accu n =
3662 if n = -1
3663 then (
3664 m_narrow_pattern <- pattern;
3665 m_items <- Array.of_list accu
3667 else
3668 let (s, _, _) as o = m_items.(n) in
3669 let accu =
3670 if (try ignore (Str.search_forward re s 0); true
3671 with Not_found -> false)
3672 then o :: accu
3673 else accu
3675 loop accu (n-1)
3677 loop [] (Array.length m_items - 1)
3679 method denarrow =
3680 m_orig_items <- (
3681 if usebookmarks
3682 then Array.of_list state.bookmarks
3683 else state.outlines
3685 m_items <- m_orig_items
3687 method remove m =
3688 if usebookmarks
3689 then
3690 if m >= 0 && m < Array.length m_items
3691 then (
3692 m_hadremovals <- true;
3693 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3694 let n = if n >= m then n+1 else n in
3695 m_items.(n)
3699 method reset anchor items =
3700 m_hadremovals <- false;
3701 if m_orig_items == empty || m_prev_items != items
3702 then (
3703 m_orig_items <- items;
3704 if String.length m_narrow_pattern = 0
3705 then m_items <- items;
3707 m_prev_items <- items;
3708 let rely = getanchory anchor in
3709 let active =
3710 let rec loop n best bestd =
3711 if n = Array.length m_items
3712 then best
3713 else
3714 let (_, _, anchor) = m_items.(n) in
3715 let orely = getanchory anchor in
3716 let d = abs (orely - rely) in
3717 if d < bestd
3718 then loop (n+1) n d
3719 else loop (n+1) best bestd
3721 loop 0 ~-1 max_int
3723 m_active <- active;
3724 m_first <- firstof m_first active
3725 end)
3728 let enterselector usebookmarks =
3729 let source = outlinesource usebookmarks in
3730 fun errmsg ->
3731 let outlines =
3732 if usebookmarks
3733 then Array.of_list state.bookmarks
3734 else state.outlines
3736 if Array.length outlines = 0
3737 then (
3738 showtext ' ' errmsg;
3740 else (
3741 state.text <- source#greetmsg;
3742 Wsi.setcursor Wsi.CURSOR_INHERIT;
3743 let anchor = getanchor () in
3744 source#reset anchor outlines;
3745 state.uioh <- coe (new outlinelistview ~source);
3746 G.postRedisplay "enter selector";
3750 let enteroutlinemode =
3751 let f = enterselector false in
3752 fun ()-> f "Document has no outline";
3755 let enterbookmarkmode =
3756 let f = enterselector true in
3757 fun () -> f "Document has no bookmarks (yet)";
3760 let color_of_string s =
3761 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3762 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3766 let color_to_string (r, g, b) =
3767 let r = truncate (r *. 256.0)
3768 and g = truncate (g *. 256.0)
3769 and b = truncate (b *. 256.0) in
3770 Printf.sprintf "%d/%d/%d" r g b
3773 let irect_of_string s =
3774 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3777 let irect_to_string (x0,y0,x1,y1) =
3778 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3781 let makecheckers () =
3782 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3783 following to say:
3784 converted by Issac Trotts. July 25, 2002 *)
3785 let image_height = 64
3786 and image_width = 64 in
3788 let make_image () =
3789 let image =
3790 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3792 for i = 0 to image_width - 1 do
3793 for j = 0 to image_height - 1 do
3794 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3795 (if (i land 8 ) lxor (j land 8) = 0
3796 then [|255;255;255|] else [|200;200;200|])
3797 done
3798 done;
3799 image
3801 let image = make_image () in
3802 let id = GlTex.gen_texture () in
3803 GlTex.bind_texture `texture_2d id;
3804 GlPix.store (`unpack_alignment 1);
3805 GlTex.image2d image;
3806 List.iter (GlTex.parameter ~target:`texture_2d)
3807 [ `wrap_s `repeat;
3808 `wrap_t `repeat;
3809 `mag_filter `nearest;
3810 `min_filter `nearest ];
3814 let setcheckers enabled =
3815 match state.texid with
3816 | None ->
3817 if enabled then state.texid <- Some (makecheckers ())
3819 | Some texid ->
3820 if not enabled
3821 then (
3822 GlTex.delete_texture texid;
3823 state.texid <- None;
3827 let int_of_string_with_suffix s =
3828 let l = String.length s in
3829 let s1, shift =
3830 if l > 1
3831 then
3832 let suffix = Char.lowercase s.[l-1] in
3833 match suffix with
3834 | 'k' -> String.sub s 0 (l-1), 10
3835 | 'm' -> String.sub s 0 (l-1), 20
3836 | 'g' -> String.sub s 0 (l-1), 30
3837 | _ -> s, 0
3838 else s, 0
3840 let n = int_of_string s1 in
3841 let m = n lsl shift in
3842 if m < 0 || m < n
3843 then raise (Failure "value too large")
3844 else m
3847 let string_with_suffix_of_int n =
3848 if n = 0
3849 then "0"
3850 else
3851 let n, s =
3852 if n = 0
3853 then 0, ""
3854 else (
3855 if n land ((1 lsl 20) - 1) = 0
3856 then n lsr 20, "M"
3857 else (
3858 if n land ((1 lsl 10) - 1) = 0
3859 then n lsr 10, "K"
3860 else n, ""
3864 let rec loop s n =
3865 let h = n mod 1000 in
3866 let n = n / 1000 in
3867 if n = 0
3868 then string_of_int h ^ s
3869 else (
3870 let s = Printf.sprintf "_%03d%s" h s in
3871 loop s n
3874 loop "" n ^ s;
3877 let defghyllscroll = (40, 8, 32);;
3878 let ghyllscroll_of_string s =
3879 let (n, a, b) as nab =
3880 if s = "default"
3881 then defghyllscroll
3882 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3884 if n <= a || n <= b || a >= b
3885 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3886 nab;
3889 let ghyllscroll_to_string ((n, a, b) as nab) =
3890 if nab = defghyllscroll
3891 then "default"
3892 else Printf.sprintf "%d,%d,%d" n a b;
3895 let describe_location () =
3896 let f (fn, _) l =
3897 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3899 let fn, ln = List.fold_left f (-1, -1) state.layout in
3900 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3901 let percent =
3902 if maxy <= 0
3903 then 100.
3904 else (100. *. (float state.y /. float maxy))
3906 if fn = ln
3907 then
3908 Printf.sprintf "page %d of %d [%.2f%%]"
3909 (fn+1) state.pagecount percent
3910 else
3911 Printf.sprintf
3912 "pages %d-%d of %d [%.2f%%]"
3913 (fn+1) (ln+1) state.pagecount percent
3916 let enterinfomode =
3917 let btos b = if b then "\xe2\x88\x9a" else "" in
3918 let showextended = ref false in
3919 let leave mode = function
3920 | Confirm -> state.mode <- mode
3921 | Cancel -> state.mode <- mode in
3922 let src =
3923 (object
3924 val mutable m_first_time = true
3925 val mutable m_l = []
3926 val mutable m_a = [||]
3927 val mutable m_prev_uioh = nouioh
3928 val mutable m_prev_mode = View
3930 inherit lvsourcebase
3932 method reset prev_mode prev_uioh =
3933 m_a <- Array.of_list (List.rev m_l);
3934 m_l <- [];
3935 m_prev_mode <- prev_mode;
3936 m_prev_uioh <- prev_uioh;
3937 if m_first_time
3938 then (
3939 let rec loop n =
3940 if n >= Array.length m_a
3941 then ()
3942 else
3943 match m_a.(n) with
3944 | _, _, _, Action _ -> m_active <- n
3945 | _ -> loop (n+1)
3947 loop 0;
3948 m_first_time <- false;
3951 method int name get set =
3952 m_l <-
3953 (name, `int get, 1, Action (
3954 fun u ->
3955 let ondone s =
3956 try set (int_of_string s)
3957 with exn ->
3958 state.text <- Printf.sprintf "bad integer `%s': %s"
3959 s (Printexc.to_string exn)
3961 state.text <- "";
3962 let te = name ^ ": ", "", None, intentry, ondone, true in
3963 state.mode <- Textentry (te, leave m_prev_mode);
3965 )) :: m_l
3967 method int_with_suffix name get set =
3968 m_l <-
3969 (name, `intws get, 1, Action (
3970 fun u ->
3971 let ondone s =
3972 try set (int_of_string_with_suffix s)
3973 with exn ->
3974 state.text <- Printf.sprintf "bad integer `%s': %s"
3975 s (Printexc.to_string exn)
3977 state.text <- "";
3978 let te =
3979 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3981 state.mode <- Textentry (te, leave m_prev_mode);
3983 )) :: m_l
3985 method bool ?(offset=1) ?(btos=btos) name get set =
3986 m_l <-
3987 (name, `bool (btos, get), offset, Action (
3988 fun u ->
3989 let v = get () in
3990 set (not v);
3992 )) :: m_l
3994 method color name get set =
3995 m_l <-
3996 (name, `color get, 1, Action (
3997 fun u ->
3998 let invalid = (nan, nan, nan) in
3999 let ondone s =
4000 let c =
4001 try color_of_string s
4002 with exn ->
4003 state.text <- Printf.sprintf "bad color `%s': %s"
4004 s (Printexc.to_string exn);
4005 invalid
4007 if c <> invalid
4008 then set c;
4010 let te = name ^ ": ", "", None, textentry, ondone, true in
4011 state.text <- color_to_string (get ());
4012 state.mode <- Textentry (te, leave m_prev_mode);
4014 )) :: m_l
4016 method string name get set =
4017 m_l <-
4018 (name, `string get, 1, Action (
4019 fun u ->
4020 let ondone s = set s in
4021 let te = name ^ ": ", "", None, textentry, ondone, true in
4022 state.mode <- Textentry (te, leave m_prev_mode);
4024 )) :: m_l
4026 method colorspace name get set =
4027 m_l <-
4028 (name, `string get, 1, Action (
4029 fun _ ->
4030 let source =
4031 let vals = [| "rgb"; "bgr"; "gray" |] in
4032 (object
4033 inherit lvsourcebase
4035 initializer
4036 m_active <- int_of_colorspace conf.colorspace;
4037 m_first <- 0;
4039 method getitemcount = Array.length vals
4040 method getitem n = (vals.(n), 0)
4041 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4042 ignore (uioh, first, pan, qsearch);
4043 if not cancel then set active;
4044 None
4045 method hasaction _ = true
4046 end)
4048 state.text <- "";
4049 let modehash = findkeyhash conf "info" in
4050 coe (new listview ~source ~trusted:true ~modehash)
4051 )) :: m_l
4053 method caption s offset =
4054 m_l <- (s, `empty, offset, Noaction) :: m_l
4056 method caption2 s f offset =
4057 m_l <- (s, `string f, offset, Noaction) :: m_l
4059 method getitemcount = Array.length m_a
4061 method getitem n =
4062 let tostr = function
4063 | `int f -> string_of_int (f ())
4064 | `intws f -> string_with_suffix_of_int (f ())
4065 | `string f -> f ()
4066 | `color f -> color_to_string (f ())
4067 | `bool (btos, f) -> btos (f ())
4068 | `empty -> ""
4070 let name, t, offset, _ = m_a.(n) in
4071 ((let s = tostr t in
4072 if String.length s > 0
4073 then Printf.sprintf "%s\t%s" name s
4074 else name),
4075 offset)
4077 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4078 let uiohopt =
4079 if not cancel
4080 then (
4081 m_qsearch <- qsearch;
4082 let uioh =
4083 match m_a.(active) with
4084 | _, _, _, Action f -> f uioh
4085 | _ -> uioh
4087 Some uioh
4089 else None
4091 m_active <- active;
4092 m_first <- first;
4093 m_pan <- pan;
4094 uiohopt
4096 method hasaction n =
4097 match m_a.(n) with
4098 | _, _, _, Action _ -> true
4099 | _ -> false
4100 end)
4102 let rec fillsrc prevmode prevuioh =
4103 let sep () = src#caption "" 0 in
4104 let colorp name get set =
4105 src#string name
4106 (fun () -> color_to_string (get ()))
4107 (fun v ->
4109 let c = color_of_string v in
4110 set c
4111 with exn ->
4112 state.text <- Printf.sprintf "bad color `%s': %s"
4113 v (Printexc.to_string exn);
4116 let oldmode = state.mode in
4117 let birdseye = isbirdseye state.mode in
4119 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4121 src#bool "presentation mode"
4122 (fun () -> conf.presentation)
4123 (fun v ->
4124 conf.presentation <- v;
4125 state.anchor <- getanchor ();
4126 represent ());
4128 src#bool "ignore case in searches"
4129 (fun () -> conf.icase)
4130 (fun v -> conf.icase <- v);
4132 src#bool "preload"
4133 (fun () -> conf.preload)
4134 (fun v -> conf.preload <- v);
4136 src#bool "highlight links"
4137 (fun () -> conf.hlinks)
4138 (fun v -> conf.hlinks <- v);
4140 src#bool "under info"
4141 (fun () -> conf.underinfo)
4142 (fun v -> conf.underinfo <- v);
4144 src#bool "persistent bookmarks"
4145 (fun () -> conf.savebmarks)
4146 (fun v -> conf.savebmarks <- v);
4148 src#bool "proportional display"
4149 (fun () -> conf.proportional)
4150 (fun v -> reqlayout conf.angle v);
4152 src#bool "trim margins"
4153 (fun () -> conf.trimmargins)
4154 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4156 src#bool "persistent location"
4157 (fun () -> conf.jumpback)
4158 (fun v -> conf.jumpback <- v);
4160 sep ();
4161 src#int "inter-page space"
4162 (fun () -> conf.interpagespace)
4163 (fun n ->
4164 conf.interpagespace <- n;
4165 docolumns conf.columns;
4166 let pageno, py =
4167 match state.layout with
4168 | [] -> 0, 0
4169 | l :: _ ->
4170 l.pageno, l.pagey
4172 state.maxy <- calcheight ();
4173 let y = getpagey pageno in
4174 gotoy (y + py)
4177 src#int "page bias"
4178 (fun () -> conf.pagebias)
4179 (fun v -> conf.pagebias <- v);
4181 src#int "scroll step"
4182 (fun () -> conf.scrollstep)
4183 (fun n -> conf.scrollstep <- n);
4185 src#int "auto scroll step"
4186 (fun () ->
4187 match state.autoscroll with
4188 | Some step -> step
4189 | _ -> conf.autoscrollstep)
4190 (fun n ->
4191 if state.autoscroll <> None
4192 then state.autoscroll <- Some n;
4193 conf.autoscrollstep <- n);
4195 src#int "zoom"
4196 (fun () -> truncate (conf.zoom *. 100.))
4197 (fun v -> setzoom ((float v) /. 100.));
4199 src#int "rotation"
4200 (fun () -> conf.angle)
4201 (fun v -> reqlayout v conf.proportional);
4203 src#int "scroll bar width"
4204 (fun () -> state.scrollw)
4205 (fun v ->
4206 state.scrollw <- v;
4207 conf.scrollbw <- v;
4208 reshape conf.winw conf.winh;
4211 src#int "scroll handle height"
4212 (fun () -> conf.scrollh)
4213 (fun v -> conf.scrollh <- v;);
4215 src#int "thumbnail width"
4216 (fun () -> conf.thumbw)
4217 (fun v ->
4218 conf.thumbw <- min 4096 v;
4219 match oldmode with
4220 | Birdseye beye ->
4221 leavebirdseye beye false;
4222 enterbirdseye ()
4223 | _ -> ()
4226 let mode = state.mode in
4227 src#string "columns"
4228 (fun () ->
4229 match conf.columns with
4230 | Csingle -> "1"
4231 | Cmulti (multi, _) -> multicolumns_to_string multi
4232 | Csplit (count, _) -> "-" ^ string_of_int count
4234 (fun v ->
4235 let n, a, b = multicolumns_of_string v in
4236 setcolumns mode n a b);
4238 sep ();
4239 src#caption "Presentation mode" 0;
4240 src#bool "scrollbar visible"
4241 (fun () -> conf.scrollbarinpm)
4242 (fun v ->
4243 if v != conf.scrollbarinpm
4244 then (
4245 conf.scrollbarinpm <- v;
4246 if conf.presentation
4247 then (
4248 state.scrollw <- if v then conf.scrollbw else 0;
4249 reshape conf.winw conf.winh;
4254 sep ();
4255 src#caption "Pixmap cache" 0;
4256 src#int_with_suffix "size (advisory)"
4257 (fun () -> conf.memlimit)
4258 (fun v -> conf.memlimit <- v);
4260 src#caption2 "used"
4261 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4262 (string_with_suffix_of_int state.memused)
4263 (Hashtbl.length state.tilemap)) 1;
4265 sep ();
4266 src#caption "Layout" 0;
4267 src#caption2 "Dimension"
4268 (fun () ->
4269 Printf.sprintf "%dx%d (virtual %dx%d)"
4270 conf.winw conf.winh
4271 state.w state.maxy)
4273 if conf.debug
4274 then
4275 src#caption2 "Position" (fun () ->
4276 Printf.sprintf "%dx%d" state.x state.y
4278 else
4279 src#caption2 "Visible" (fun () -> describe_location ()) 1
4282 sep ();
4283 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4284 "Save these parameters as global defaults at exit"
4285 (fun () -> conf.bedefault)
4286 (fun v -> conf.bedefault <- v)
4289 sep ();
4290 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4291 src#bool ~offset:0 ~btos "Extended parameters"
4292 (fun () -> !showextended)
4293 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4294 if !showextended
4295 then (
4296 src#bool "checkers"
4297 (fun () -> conf.checkers)
4298 (fun v -> conf.checkers <- v; setcheckers v);
4299 src#bool "update cursor"
4300 (fun () -> conf.updatecurs)
4301 (fun v -> conf.updatecurs <- v);
4302 src#bool "verbose"
4303 (fun () -> conf.verbose)
4304 (fun v -> conf.verbose <- v);
4305 src#bool "invert colors"
4306 (fun () -> conf.invert)
4307 (fun v -> conf.invert <- v);
4308 src#bool "max fit"
4309 (fun () -> conf.maxhfit)
4310 (fun v -> conf.maxhfit <- v);
4311 src#bool "redirect stderr"
4312 (fun () -> conf.redirectstderr)
4313 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4314 src#string "uri launcher"
4315 (fun () -> conf.urilauncher)
4316 (fun v -> conf.urilauncher <- v);
4317 src#string "path launcher"
4318 (fun () -> conf.pathlauncher)
4319 (fun v -> conf.pathlauncher <- v);
4320 src#string "tile size"
4321 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4322 (fun v ->
4324 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4325 conf.tilew <- max 64 w;
4326 conf.tileh <- max 64 h;
4327 flushtiles ();
4328 with exn ->
4329 state.text <- Printf.sprintf "bad tile size `%s': %s"
4330 v (Printexc.to_string exn));
4331 src#int "texture count"
4332 (fun () -> conf.texcount)
4333 (fun v ->
4334 if realloctexts v
4335 then conf.texcount <- v
4336 else showtext '!' " Failed to set texture count please retry later"
4338 src#int "slice height"
4339 (fun () -> conf.sliceheight)
4340 (fun v ->
4341 conf.sliceheight <- v;
4342 wcmd "sliceh %d" conf.sliceheight;
4344 src#int "anti-aliasing level"
4345 (fun () -> conf.aalevel)
4346 (fun v ->
4347 conf.aalevel <- bound v 0 8;
4348 state.anchor <- getanchor ();
4349 opendoc state.path state.password;
4351 src#int "ui font size"
4352 (fun () -> fstate.fontsize)
4353 (fun v -> setfontsize (bound v 5 100));
4354 src#int "hint font size"
4355 (fun () -> conf.hfsize)
4356 (fun v -> conf.hfsize <- bound v 5 100);
4357 colorp "background color"
4358 (fun () -> conf.bgcolor)
4359 (fun v -> conf.bgcolor <- v);
4360 src#bool "crop hack"
4361 (fun () -> conf.crophack)
4362 (fun v -> conf.crophack <- v);
4363 src#string "trim fuzz"
4364 (fun () -> irect_to_string conf.trimfuzz)
4365 (fun v ->
4367 conf.trimfuzz <- irect_of_string v;
4368 if conf.trimmargins
4369 then settrim true conf.trimfuzz;
4370 with exn ->
4371 state.text <- Printf.sprintf "bad irect `%s': %s"
4372 v (Printexc.to_string exn)
4374 src#string "throttle"
4375 (fun () ->
4376 match conf.maxwait with
4377 | None -> "show place holder if page is not ready"
4378 | Some time ->
4379 if time = infinity
4380 then "wait for page to fully render"
4381 else
4382 "wait " ^ string_of_float time
4383 ^ " seconds before showing placeholder"
4385 (fun v ->
4387 let f = float_of_string v in
4388 if f <= 0.0
4389 then conf.maxwait <- None
4390 else conf.maxwait <- Some f
4391 with exn ->
4392 state.text <- Printf.sprintf "bad time `%s': %s"
4393 v (Printexc.to_string exn)
4395 src#string "ghyll scroll"
4396 (fun () ->
4397 match conf.ghyllscroll with
4398 | None -> ""
4399 | Some nab -> ghyllscroll_to_string nab
4401 (fun v ->
4403 let gs =
4404 if String.length v = 0
4405 then None
4406 else Some (ghyllscroll_of_string v)
4408 conf.ghyllscroll <- gs
4409 with exn ->
4410 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4411 v (Printexc.to_string exn)
4413 src#string "selection command"
4414 (fun () -> conf.selcmd)
4415 (fun v -> conf.selcmd <- v);
4416 src#colorspace "color space"
4417 (fun () -> colorspace_to_string conf.colorspace)
4418 (fun v ->
4419 conf.colorspace <- colorspace_of_int v;
4420 wcmd "cs %d" v;
4421 load state.layout;
4425 sep ();
4426 src#caption "Document" 0;
4427 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4428 src#caption2 "Pages"
4429 (fun () -> string_of_int state.pagecount) 1;
4430 src#caption2 "Dimensions"
4431 (fun () -> string_of_int (List.length state.pdims)) 1;
4432 if conf.trimmargins
4433 then (
4434 sep ();
4435 src#caption "Trimmed margins" 0;
4436 src#caption2 "Dimensions"
4437 (fun () -> string_of_int (List.length state.pdims)) 1;
4440 src#reset prevmode prevuioh;
4442 fun () ->
4443 state.text <- "";
4444 let prevmode = state.mode
4445 and prevuioh = state.uioh in
4446 fillsrc prevmode prevuioh;
4447 let source = (src :> lvsource) in
4448 let modehash = findkeyhash conf "info" in
4449 state.uioh <- coe (object (self)
4450 inherit listview ~source ~trusted:true ~modehash as super
4451 val mutable m_prevmemused = 0
4452 method infochanged = function
4453 | Memused ->
4454 if m_prevmemused != state.memused
4455 then (
4456 m_prevmemused <- state.memused;
4457 G.postRedisplay "memusedchanged";
4459 | Pdim -> G.postRedisplay "pdimchanged"
4460 | Docinfo -> fillsrc prevmode prevuioh
4462 method key key mask =
4463 if not (Wsi.withctrl mask)
4464 then
4465 match key with
4466 | 0xff51 -> coe (self#updownlevel ~-1)
4467 | 0xff53 -> coe (self#updownlevel 1)
4468 | _ -> super#key key mask
4469 else super#key key mask
4470 end);
4471 G.postRedisplay "info";
4474 let enterhelpmode =
4475 let source =
4476 (object
4477 inherit lvsourcebase
4478 method getitemcount = Array.length state.help
4479 method getitem n =
4480 let s, n, _ = state.help.(n) in
4481 (s, n)
4483 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4484 let optuioh =
4485 if not cancel
4486 then (
4487 m_qsearch <- qsearch;
4488 match state.help.(active) with
4489 | _, _, Action f -> Some (f uioh)
4490 | _ -> Some (uioh)
4492 else None
4494 m_active <- active;
4495 m_first <- first;
4496 m_pan <- pan;
4497 optuioh
4499 method hasaction n =
4500 match state.help.(n) with
4501 | _, _, Action _ -> true
4502 | _ -> false
4504 initializer
4505 m_active <- -1
4506 end)
4507 in fun () ->
4508 let modehash = findkeyhash conf "help" in
4509 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4510 G.postRedisplay "help";
4513 let entermsgsmode =
4514 let msgsource =
4515 let re = Str.regexp "[\r\n]" in
4516 (object
4517 inherit lvsourcebase
4518 val mutable m_items = [||]
4520 method getitemcount = 1 + Array.length m_items
4522 method getitem n =
4523 if n = 0
4524 then "[Clear]", 0
4525 else m_items.(n-1), 0
4527 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4528 ignore uioh;
4529 if not cancel
4530 then (
4531 if active = 0
4532 then Buffer.clear state.errmsgs;
4533 m_qsearch <- qsearch;
4535 m_active <- active;
4536 m_first <- first;
4537 m_pan <- pan;
4538 None
4540 method hasaction n =
4541 n = 0
4543 method reset =
4544 state.newerrmsgs <- false;
4545 let l = Str.split re (Buffer.contents state.errmsgs) in
4546 m_items <- Array.of_list l
4548 initializer
4549 m_active <- 0
4550 end)
4551 in fun () ->
4552 state.text <- "";
4553 msgsource#reset;
4554 let source = (msgsource :> lvsource) in
4555 let modehash = findkeyhash conf "listview" in
4556 state.uioh <- coe (object
4557 inherit listview ~source ~trusted:false ~modehash as super
4558 method display =
4559 if state.newerrmsgs
4560 then msgsource#reset;
4561 super#display
4562 end);
4563 G.postRedisplay "msgs";
4566 let quickbookmark ?title () =
4567 match state.layout with
4568 | [] -> ()
4569 | l :: _ ->
4570 let title =
4571 match title with
4572 | None ->
4573 let sec = Unix.gettimeofday () in
4574 let tm = Unix.localtime sec in
4575 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4576 (l.pageno+1)
4577 tm.Unix.tm_mday
4578 tm.Unix.tm_mon
4579 (tm.Unix.tm_year + 1900)
4580 tm.Unix.tm_hour
4581 tm.Unix.tm_min
4582 | Some title -> title
4584 state.bookmarks <-
4585 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4586 :: state.bookmarks
4589 let doreshape w h =
4590 state.fullscreen <- None;
4591 Wsi.reshape w h;
4594 let setautoscrollspeed step goingdown =
4595 let incr = max 1 ((abs step) / 2) in
4596 let incr = if goingdown then incr else -incr in
4597 let astep = step + incr in
4598 state.autoscroll <- Some astep;
4601 let gotounder = function
4602 | Ulinkgoto (pageno, top) ->
4603 if pageno >= 0
4604 then (
4605 addnav ();
4606 gotopage1 pageno top;
4609 | Ulinkuri s ->
4610 gotouri s
4612 | Uremote (filename, pageno) ->
4613 let path =
4614 if Sys.file_exists filename
4615 then filename
4616 else
4617 let dir = Filename.dirname state.path in
4618 let path = Filename.concat dir filename in
4619 if Sys.file_exists path
4620 then path
4621 else ""
4623 if String.length path > 0
4624 then (
4625 let anchor = getanchor () in
4626 let ranchor = state.path, state.password, anchor in
4627 state.anchor <- (pageno, 0.0);
4628 state.ranchors <- ranchor :: state.ranchors;
4629 opendoc path "";
4631 else showtext '!' ("Could not find " ^ filename)
4633 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4636 let canpan () =
4637 match conf.columns with
4638 | Csplit _ -> true
4639 | _ -> conf.zoom > 1.0
4642 let viewkeyboard key mask =
4643 let enttext te =
4644 let mode = state.mode in
4645 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4646 state.text <- "";
4647 enttext ();
4648 G.postRedisplay "view:enttext"
4650 let ctrl = Wsi.withctrl mask in
4651 match key with
4652 | 81 -> (* Q *)
4653 exit 0
4655 | 0xff63 -> (* insert *)
4656 if conf.angle mod 360 = 0
4657 then (
4658 state.mode <- LinkNav (Ltgendir 0);
4659 gotoy state.y;
4661 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4663 | 0xff1b | 113 -> (* escape / q *)
4664 begin match state.mstate with
4665 | Mzoomrect _ ->
4666 state.mstate <- Mnone;
4667 Wsi.setcursor Wsi.CURSOR_INHERIT;
4668 G.postRedisplay "kill zoom rect";
4669 | _ ->
4670 match state.ranchors with
4671 | [] -> raise Quit
4672 | (path, password, anchor) :: rest ->
4673 state.ranchors <- rest;
4674 state.anchor <- anchor;
4675 opendoc path password
4676 end;
4678 | 0xff08 -> (* backspace *)
4679 let y = getnav ~-1 in
4680 gotoy_and_clear_text y
4682 | 111 -> (* o *)
4683 enteroutlinemode ()
4685 | 117 -> (* u *)
4686 state.rects <- [];
4687 state.text <- "";
4688 G.postRedisplay "dehighlight";
4690 | 47 | 63 -> (* / ? *)
4691 let ondone isforw s =
4692 cbput state.hists.pat s;
4693 state.searchpattern <- s;
4694 search s isforw
4696 let s = String.create 1 in
4697 s.[0] <- Char.chr key;
4698 enttext (s, "", Some (onhist state.hists.pat),
4699 textentry, ondone (key = 47), true)
4701 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4702 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4703 setzoom (conf.zoom +. incr)
4705 | 43 | 0xffab -> (* + *)
4706 let ondone s =
4707 let n =
4708 try int_of_string s with exc ->
4709 state.text <- Printf.sprintf "bad integer `%s': %s"
4710 s (Printexc.to_string exc);
4711 max_int
4713 if n != max_int
4714 then (
4715 conf.pagebias <- n;
4716 state.text <- "page bias is now " ^ string_of_int n;
4719 enttext ("page bias: ", "", None, intentry, ondone, true)
4721 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4722 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4723 setzoom (max 0.01 (conf.zoom -. decr))
4725 | 45 | 0xffad -> (* - *)
4726 let ondone msg = state.text <- msg in
4727 enttext (
4728 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4729 optentry state.mode, ondone, true
4732 | 48 when ctrl -> (* ctrl-0 *)
4733 setzoom 1.0
4735 | 49 when ctrl -> (* 1 *)
4736 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4737 if zoom < 1.0
4738 then setzoom zoom
4740 | 0xffc6 -> (* f9 *)
4741 togglebirdseye ()
4743 | 57 when ctrl -> (* ctrl-9 *)
4744 togglebirdseye ()
4746 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4747 when not ctrl -> (* 0..9 *)
4748 let ondone s =
4749 let n =
4750 try int_of_string s with exc ->
4751 state.text <- Printf.sprintf "bad integer `%s': %s"
4752 s (Printexc.to_string exc);
4755 if n >= 0
4756 then (
4757 addnav ();
4758 cbput state.hists.pag (string_of_int n);
4759 gotopage1 (n + conf.pagebias - 1) 0;
4762 let pageentry text key =
4763 match Char.unsafe_chr key with
4764 | 'g' -> TEdone text
4765 | _ -> intentry text key
4767 let text = "x" in text.[0] <- Char.chr key;
4768 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4770 | 98 -> (* b *)
4771 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4772 reshape conf.winw conf.winh;
4774 | 108 -> (* l *)
4775 conf.hlinks <- not conf.hlinks;
4776 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4777 G.postRedisplay "toggle highlightlinks";
4779 | 70 -> (* F *)
4780 state.glinks <- true;
4781 let mode = state.mode in
4782 state.mode <- Textentry (
4783 (":", "", None, linknentry, linkndone (fun under ->
4784 addnav ();
4785 gotounder under
4786 ), false
4787 ), fun _ ->
4788 state.glinks <- false;
4789 state.mode <- mode
4791 state.text <- "";
4792 G.postRedisplay "view:linkent(F)"
4794 | 121 -> (* y *)
4795 state.glinks <- true;
4796 let mode = state.mode in
4797 state.mode <- Textentry (
4798 (":", "", None, linknentry, linkndone (fun under ->
4799 match Ne.pipe () with
4800 | Ne.Exn exn ->
4801 showtext '!' (Printf.sprintf "pipe failed: %s"
4802 (Printexc.to_string exn));
4803 | Ne.Res (r, w) ->
4804 let popened =
4805 try popen conf.selcmd [r, 0; w, -1]; true
4806 with exn ->
4807 showtext '!'
4808 (Printf.sprintf "failed to execute %s: %s"
4809 conf.selcmd (Printexc.to_string exn));
4810 false
4812 let clo cap fd =
4813 Ne.clo fd (fun msg ->
4814 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4817 let s = undertext under in
4818 if popened
4819 then
4820 (try
4821 let l = String.length s in
4822 let n = Unix.write w s 0 l in
4823 if n != l
4824 then
4825 showtext '!'
4826 (Printf.sprintf
4827 "failed to write %d characters to sel pipe, wrote %d"
4830 with exn ->
4831 showtext '!'
4832 (Printf.sprintf "failed to write to sel pipe: %s"
4833 (Printexc.to_string exn)
4836 else dolog "%s" s;
4837 clo "pipe/r" r;
4838 clo "pipe/w" w;
4839 ), false
4841 fun _ ->
4842 state.glinks <- false;
4843 state.mode <- mode
4845 state.text <- "";
4846 G.postRedisplay "view:linkent"
4848 | 97 -> (* a *)
4849 begin match state.autoscroll with
4850 | Some step ->
4851 conf.autoscrollstep <- step;
4852 state.autoscroll <- None
4853 | None ->
4854 if conf.autoscrollstep = 0
4855 then state.autoscroll <- Some 1
4856 else state.autoscroll <- Some conf.autoscrollstep
4859 | 112 when ctrl -> (* ctrl-p *)
4860 launchpath ()
4862 | 80 -> (* P *)
4863 conf.presentation <- not conf.presentation;
4864 if conf.presentation
4865 then (
4866 if not conf.scrollbarinpm
4867 then state.scrollw <- 0;
4869 else
4870 state.scrollw <- conf.scrollbw;
4872 showtext ' ' ("presentation mode " ^
4873 if conf.presentation then "on" else "off");
4874 state.anchor <- getanchor ();
4875 represent ()
4877 | 102 -> (* f *)
4878 begin match state.fullscreen with
4879 | None ->
4880 state.fullscreen <- Some (conf.winw, conf.winh);
4881 Wsi.fullscreen ()
4882 | Some (w, h) ->
4883 state.fullscreen <- None;
4884 doreshape w h
4887 | 103 -> (* g *)
4888 gotoy_and_clear_text 0
4890 | 71 -> (* G *)
4891 gotopage1 (state.pagecount - 1) 0
4893 | 112 | 78 -> (* p|N *)
4894 search state.searchpattern false
4896 | 110 | 0xffc0 -> (* n|F3 *)
4897 search state.searchpattern true
4899 | 116 -> (* t *)
4900 begin match state.layout with
4901 | [] -> ()
4902 | l :: _ ->
4903 gotoy_and_clear_text (getpagey l.pageno)
4906 | 32 -> (* ' ' *)
4907 begin match List.rev state.layout with
4908 | [] -> ()
4909 | l :: _ ->
4910 let pageno = min (l.pageno+1) (state.pagecount-1) in
4911 gotoy_and_clear_text (getpagey pageno)
4914 | 0xff9f | 0xffff -> (* delete *)
4915 begin match state.layout with
4916 | [] -> ()
4917 | l :: _ ->
4918 let pageno = max 0 (l.pageno-1) in
4919 gotoy_and_clear_text (getpagey pageno)
4922 | 61 -> (* = *)
4923 showtext ' ' (describe_location ());
4925 | 119 -> (* w *)
4926 begin match state.layout with
4927 | [] -> ()
4928 | l :: _ ->
4929 doreshape (l.pagew + state.scrollw) l.pageh;
4930 G.postRedisplay "w"
4933 | 39 -> (* ' *)
4934 enterbookmarkmode ()
4936 | 104 | 0xffbe -> (* h|F1 *)
4937 enterhelpmode ()
4939 | 105 -> (* i *)
4940 enterinfomode ()
4942 | 101 when conf.redirectstderr -> (* e *)
4943 entermsgsmode ()
4945 | 109 -> (* m *)
4946 let ondone s =
4947 match state.layout with
4948 | l :: _ ->
4949 state.bookmarks <-
4950 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4951 :: state.bookmarks
4952 | _ -> ()
4954 enttext ("bookmark: ", "", None, textentry, ondone, true)
4956 | 126 -> (* ~ *)
4957 quickbookmark ();
4958 showtext ' ' "Quick bookmark added";
4960 | 122 -> (* z *)
4961 begin match state.layout with
4962 | l :: _ ->
4963 let rect = getpdimrect l.pagedimno in
4964 let w, h =
4965 if conf.crophack
4966 then
4967 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4968 truncate (1.2 *. (rect.(3) -. rect.(0))))
4969 else
4970 (truncate (rect.(1) -. rect.(0)),
4971 truncate (rect.(3) -. rect.(0)))
4973 let w = truncate ((float w)*.conf.zoom)
4974 and h = truncate ((float h)*.conf.zoom) in
4975 if w != 0 && h != 0
4976 then (
4977 state.anchor <- getanchor ();
4978 doreshape (w + state.scrollw) (h + conf.interpagespace)
4980 G.postRedisplay "z";
4982 | [] -> ()
4985 | 50 when ctrl -> (* ctrl-2 *)
4986 let maxw = getmaxw () in
4987 if maxw > 0.0
4988 then setzoom (maxw /. float conf.winw)
4990 | 60 | 62 -> (* < > *)
4991 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4993 | 91 | 93 -> (* [ ] *)
4994 conf.colorscale <-
4995 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4997 G.postRedisplay "brightness";
4999 | 99 when state.mode = View -> (* c *)
5000 let (c, a, b), z =
5001 match state.prevcolumns with
5002 | None -> (1, 0, 0), 1.0
5003 | Some (columns, z) ->
5004 let cab =
5005 match columns with
5006 | Csplit (c, _) -> -c, 0, 0
5007 | Cmulti ((c, a, b), _) -> c, a, b
5008 | Csingle -> 1, 0, 0
5010 cab, z
5012 setcolumns View c a b;
5013 setzoom z;
5015 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5016 setzoom state.prevzoom
5018 | 107 | 0xff52 -> (* k up *)
5019 begin match state.autoscroll with
5020 | None ->
5021 begin match state.mode with
5022 | Birdseye beye -> upbirdseye 1 beye
5023 | _ ->
5024 if ctrl
5025 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5026 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5028 | Some n ->
5029 setautoscrollspeed n false
5032 | 106 | 0xff54 -> (* j down *)
5033 begin match state.autoscroll with
5034 | None ->
5035 begin match state.mode with
5036 | Birdseye beye -> downbirdseye 1 beye
5037 | _ ->
5038 if ctrl
5039 then gotoy_and_clear_text (clamp (conf.winh/2))
5040 else gotoy_and_clear_text (clamp conf.scrollstep)
5042 | Some n ->
5043 setautoscrollspeed n true
5046 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5047 if canpan ()
5048 then
5049 let dx =
5050 if ctrl
5051 then conf.winw / 2
5052 else 10
5054 let dx = if key = 0xff51 then dx else -dx in
5055 state.x <- state.x + dx;
5056 gotoy_and_clear_text state.y
5057 else (
5058 state.text <- "";
5059 G.postRedisplay "lef/right"
5062 | 0xff55 -> (* prior *)
5063 let y =
5064 if ctrl
5065 then
5066 match state.layout with
5067 | [] -> state.y
5068 | l :: _ -> state.y - l.pagey
5069 else
5070 clamp (-conf.winh)
5072 gotoghyll y
5074 | 0xff56 -> (* next *)
5075 let y =
5076 if ctrl
5077 then
5078 match List.rev state.layout with
5079 | [] -> state.y
5080 | l :: _ -> getpagey l.pageno
5081 else
5082 clamp conf.winh
5084 gotoghyll y
5086 | 0xff50 -> gotoghyll 0
5087 | 0xff57 -> gotoghyll (clamp state.maxy)
5088 | 0xff53 when Wsi.withalt mask ->
5089 gotoghyll (getnav ~-1)
5090 | 0xff51 when Wsi.withalt mask ->
5091 gotoghyll (getnav 1)
5093 | 114 -> (* r *)
5094 state.anchor <- getanchor ();
5095 opendoc state.path state.password
5097 | 118 when conf.debug -> (* v *)
5098 state.rects <- [];
5099 List.iter (fun l ->
5100 match getopaque l.pageno with
5101 | None -> ()
5102 | Some opaque ->
5103 let x0, y0, x1, y1 = pagebbox opaque in
5104 let a,b = float x0, float y0 in
5105 let c,d = float x1, float y0 in
5106 let e,f = float x1, float y1 in
5107 let h,j = float x0, float y1 in
5108 let rect = (a,b,c,d,e,f,h,j) in
5109 debugrect rect;
5110 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5111 ) state.layout;
5112 G.postRedisplay "v";
5114 | _ ->
5115 vlog "huh? %s" (Wsi.keyname key)
5118 let linknavkeyboard key mask linknav =
5119 let getpage pageno =
5120 let rec loop = function
5121 | [] -> None
5122 | l :: _ when l.pageno = pageno -> Some l
5123 | _ :: rest -> loop rest
5124 in loop state.layout
5126 let doexact (pageno, n) =
5127 match getopaque pageno, getpage pageno with
5128 | Some opaque, Some l ->
5129 if key = 0xff0d
5130 then
5131 let under = getlink opaque n in
5132 G.postRedisplay "link gotounder";
5133 gotounder under;
5134 state.mode <- View;
5135 else
5136 let opt, dir =
5137 match key with
5138 | 0xff50 -> (* home *)
5139 Some (findlink opaque LDfirst), -1
5141 | 0xff57 -> (* end *)
5142 Some (findlink opaque LDlast), 1
5144 | 0xff51 -> (* left *)
5145 Some (findlink opaque (LDleft n)), -1
5147 | 0xff53 -> (* right *)
5148 Some (findlink opaque (LDright n)), 1
5150 | 0xff52 -> (* up *)
5151 Some (findlink opaque (LDup n)), -1
5153 | 0xff54 -> (* down *)
5154 Some (findlink opaque (LDdown n)), 1
5156 | _ -> None, 0
5158 let pwl l dir =
5159 begin match findpwl l.pageno dir with
5160 | Pwlnotfound -> ()
5161 | Pwl pageno ->
5162 let notfound dir =
5163 state.mode <- LinkNav (Ltgendir dir);
5164 let y, h = getpageyh pageno in
5165 let y =
5166 if dir < 0
5167 then y + h - conf.winh
5168 else y
5170 gotoy y
5172 begin match getopaque pageno, getpage pageno with
5173 | Some opaque, Some _ ->
5174 let link =
5175 let ld = if dir > 0 then LDfirst else LDlast in
5176 findlink opaque ld
5178 begin match link with
5179 | Lfound m ->
5180 showlinktype (getlink opaque m);
5181 state.mode <- LinkNav (Ltexact (pageno, m));
5182 G.postRedisplay "linknav jpage";
5183 | _ -> notfound dir
5184 end;
5185 | _ -> notfound dir
5186 end;
5187 end;
5189 begin match opt with
5190 | Some Lnotfound -> pwl l dir;
5191 | Some (Lfound m) ->
5192 if m = n
5193 then pwl l dir
5194 else (
5195 let _, y0, _, y1 = getlinkrect opaque m in
5196 if y0 < l.pagey
5197 then gotopage1 l.pageno y0
5198 else (
5199 let d = fstate.fontsize + 1 in
5200 if y1 - l.pagey > l.pagevh - d
5201 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5202 else G.postRedisplay "linknav";
5204 showlinktype (getlink opaque m);
5205 state.mode <- LinkNav (Ltexact (l.pageno, m));
5208 | None -> viewkeyboard key mask
5209 end;
5210 | _ -> viewkeyboard key mask
5212 if key = 0xff63
5213 then (
5214 state.mode <- View;
5215 G.postRedisplay "leave linknav"
5217 else
5218 match linknav with
5219 | Ltgendir _ -> viewkeyboard key mask
5220 | Ltexact exact -> doexact exact
5223 let keyboard key mask =
5224 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5225 then wcmd "interrupt"
5226 else state.uioh <- state.uioh#key key mask
5229 let birdseyekeyboard key mask
5230 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5231 let incr =
5232 match conf.columns with
5233 | Csingle -> 1
5234 | Cmulti ((c, _, _), _) -> c
5235 | Csplit _ -> failwith "bird's eye split mode"
5237 match key with
5238 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5239 let y, h = getpageyh pageno in
5240 let top = (conf.winh - h) / 2 in
5241 gotoy (max 0 (y - top))
5242 | 0xff0d -> leavebirdseye beye false
5243 | 0xff1b -> leavebirdseye beye true (* escape *)
5244 | 0xff52 -> upbirdseye incr beye (* prior *)
5245 | 0xff54 -> downbirdseye incr beye (* next *)
5246 | 0xff51 -> upbirdseye 1 beye (* up *)
5247 | 0xff53 -> downbirdseye 1 beye (* down *)
5249 | 0xff55 ->
5250 begin match state.layout with
5251 | l :: _ ->
5252 if l.pagey != 0
5253 then (
5254 state.mode <- Birdseye (
5255 oconf, leftx, l.pageno, hooverpageno, anchor
5257 gotopage1 l.pageno 0;
5259 else (
5260 let layout = layout (state.y-conf.winh) conf.winh in
5261 match layout with
5262 | [] -> gotoy (clamp (-conf.winh))
5263 | l :: _ ->
5264 state.mode <- Birdseye (
5265 oconf, leftx, l.pageno, hooverpageno, anchor
5267 gotopage1 l.pageno 0
5270 | [] -> gotoy (clamp (-conf.winh))
5271 end;
5273 | 0xff56 ->
5274 begin match List.rev state.layout with
5275 | l :: _ ->
5276 let layout = layout (state.y + conf.winh) conf.winh in
5277 begin match layout with
5278 | [] ->
5279 let incr = l.pageh - l.pagevh in
5280 if incr = 0
5281 then (
5282 state.mode <-
5283 Birdseye (
5284 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5286 G.postRedisplay "birdseye pagedown";
5288 else gotoy (clamp (incr + conf.interpagespace*2));
5290 | l :: _ ->
5291 state.mode <-
5292 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5293 gotopage1 l.pageno 0;
5296 | [] -> gotoy (clamp conf.winh)
5297 end;
5299 | 0xff50 ->
5300 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5301 gotopage1 0 0
5303 | 0xff57 ->
5304 let pageno = state.pagecount - 1 in
5305 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5306 if not (pagevisible state.layout pageno)
5307 then
5308 let h =
5309 match List.rev state.pdims with
5310 | [] -> conf.winh
5311 | (_, _, h, _) :: _ -> h
5313 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5314 else G.postRedisplay "birdseye end";
5315 | _ -> viewkeyboard key mask
5318 let drawpage l linkindexbase =
5319 let color =
5320 match state.mode with
5321 | Textentry _ -> scalecolor 0.4
5322 | LinkNav _
5323 | View -> scalecolor 1.0
5324 | Birdseye (_, _, pageno, hooverpageno, _) ->
5325 if l.pageno = hooverpageno
5326 then scalecolor 0.9
5327 else (
5328 if l.pageno = pageno
5329 then scalecolor 1.0
5330 else scalecolor 0.8
5333 drawtiles l color;
5334 begin match getopaque l.pageno with
5335 | Some opaque ->
5336 if tileready l l.pagex l.pagey
5337 then
5338 let x = l.pagedispx - l.pagex
5339 and y = l.pagedispy - l.pagey in
5340 let hlmask =
5341 match conf.columns with
5342 | Csingle | Cmulti _ ->
5343 (if conf.hlinks then 1 else 0)
5344 + (if state.glinks
5345 && not (isbirdseye state.mode) then 2 else 0)
5346 | _ -> 0
5348 let s =
5349 match state.mode with
5350 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5351 | _ -> ""
5353 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5354 else 0
5356 | _ -> 0
5357 end;
5360 let scrollindicator () =
5361 let sbw, ph, sh = state.uioh#scrollph in
5362 let sbh, pw, sw = state.uioh#scrollpw in
5364 GlDraw.color (0.64, 0.64, 0.64);
5365 GlDraw.rect
5366 (float (conf.winw - sbw), 0.)
5367 (float conf.winw, float conf.winh)
5369 GlDraw.rect
5370 (0., float (conf.winh - sbh))
5371 (float (conf.winw - state.scrollw - 1), float conf.winh)
5373 GlDraw.color (0.0, 0.0, 0.0);
5375 GlDraw.rect
5376 (float (conf.winw - sbw), ph)
5377 (float conf.winw, ph +. sh)
5379 GlDraw.rect
5380 (pw, float (conf.winh - sbh))
5381 (pw +. sw, float conf.winh)
5385 let showsel () =
5386 match state.mstate with
5387 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5390 | Msel ((x0, y0), (x1, y1)) ->
5391 let rec loop = function
5392 | l :: ls ->
5393 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5394 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5395 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5396 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5397 then
5398 match getopaque l.pageno with
5399 | Some opaque ->
5400 let x0, y0 = pagetranslatepoint l x0 y0 in
5401 let x1, y1 = pagetranslatepoint l x1 y1 in
5402 seltext opaque (x0, y0, x1, y1);
5403 | _ -> ()
5404 else loop ls
5405 | [] -> ()
5407 loop state.layout
5410 let showrects rects =
5411 Gl.enable `blend;
5412 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5413 GlDraw.polygon_mode `both `fill;
5414 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5415 List.iter
5416 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5417 List.iter (fun l ->
5418 if l.pageno = pageno
5419 then (
5420 let dx = float (l.pagedispx - l.pagex) in
5421 let dy = float (l.pagedispy - l.pagey) in
5422 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5423 GlDraw.begins `quads;
5425 GlDraw.vertex2 (x0+.dx, y0+.dy);
5426 GlDraw.vertex2 (x1+.dx, y1+.dy);
5427 GlDraw.vertex2 (x2+.dx, y2+.dy);
5428 GlDraw.vertex2 (x3+.dx, y3+.dy);
5430 GlDraw.ends ();
5432 ) state.layout
5433 ) rects
5435 Gl.disable `blend;
5438 let display () =
5439 GlClear.color (scalecolor2 conf.bgcolor);
5440 GlClear.clear [`color];
5441 let rec loop linkindexbase = function
5442 | l :: rest ->
5443 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5444 loop linkindexbase rest
5445 | [] -> ()
5447 loop 0 state.layout;
5448 let rects =
5449 match state.mode with
5450 | LinkNav (Ltexact (pageno, linkno)) ->
5451 begin match getopaque pageno with
5452 | Some opaque ->
5453 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5454 (pageno, 5, (
5455 float x0, float y0,
5456 float x1, float y0,
5457 float x1, float y1,
5458 float x0, float y1)
5459 ) :: state.rects
5460 | None -> state.rects
5462 | _ -> state.rects
5464 showrects rects;
5465 showsel ();
5466 state.uioh#display;
5467 begin match state.mstate with
5468 | Mzoomrect ((x0, y0), (x1, y1)) ->
5469 Gl.enable `blend;
5470 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5471 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5472 GlDraw.rect (float x0, float y0)
5473 (float x1, float y1);
5474 Gl.disable `blend;
5475 | _ -> ()
5476 end;
5477 enttext ();
5478 scrollindicator ();
5479 Wsi.swapb ();
5482 let zoomrect x y x1 y1 =
5483 let x0 = min x x1
5484 and x1 = max x x1
5485 and y0 = min y y1 in
5486 gotoy (state.y + y0);
5487 state.anchor <- getanchor ();
5488 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5489 let margin =
5490 if state.w < conf.winw - state.scrollw
5491 then (conf.winw - state.scrollw - state.w) / 2
5492 else 0
5494 state.x <- (state.x + margin) - x0;
5495 setzoom zoom;
5496 Wsi.setcursor Wsi.CURSOR_INHERIT;
5497 state.mstate <- Mnone;
5500 let scrollx x =
5501 let winw = conf.winw - state.scrollw - 1 in
5502 let s = float x /. float winw in
5503 let destx = truncate (float (state.w + winw) *. s) in
5504 state.x <- winw - destx;
5505 gotoy_and_clear_text state.y;
5506 state.mstate <- Mscrollx;
5509 let scrolly y =
5510 let s = float y /. float conf.winh in
5511 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5512 gotoy_and_clear_text desty;
5513 state.mstate <- Mscrolly;
5516 let viewmouse button down x y mask =
5517 match button with
5518 | n when (n == 4 || n == 5) && not down ->
5519 if Wsi.withctrl mask
5520 then (
5521 match state.mstate with
5522 | Mzoom (oldn, i) ->
5523 if oldn = n
5524 then (
5525 if i = 2
5526 then
5527 let incr =
5528 match n with
5529 | 5 ->
5530 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5531 | _ ->
5532 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5534 let zoom = conf.zoom -. incr in
5535 setzoom zoom;
5536 state.mstate <- Mzoom (n, 0);
5537 else
5538 state.mstate <- Mzoom (n, i+1);
5540 else state.mstate <- Mzoom (n, 0)
5542 | _ -> state.mstate <- Mzoom (n, 0)
5544 else (
5545 match state.autoscroll with
5546 | Some step -> setautoscrollspeed step (n=4)
5547 | None ->
5548 let incr =
5549 if n = 4
5550 then -conf.scrollstep
5551 else conf.scrollstep
5553 let incr = incr * 2 in
5554 let y = clamp incr in
5555 gotoy_and_clear_text y
5558 | 1 when Wsi.withctrl mask ->
5559 if down
5560 then (
5561 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5562 state.mstate <- Mpan (x, y)
5564 else
5565 state.mstate <- Mnone
5567 | 3 ->
5568 if down
5569 then (
5570 Wsi.setcursor Wsi.CURSOR_CYCLE;
5571 let p = (x, y) in
5572 state.mstate <- Mzoomrect (p, p)
5574 else (
5575 match state.mstate with
5576 | Mzoomrect ((x0, y0), _) ->
5577 if abs (x-x0) > 10 && abs (y - y0) > 10
5578 then zoomrect x0 y0 x y
5579 else (
5580 state.mstate <- Mnone;
5581 Wsi.setcursor Wsi.CURSOR_INHERIT;
5582 G.postRedisplay "kill accidental zoom rect";
5584 | _ ->
5585 Wsi.setcursor Wsi.CURSOR_INHERIT;
5586 state.mstate <- Mnone
5589 | 1 when x > conf.winw - state.scrollw ->
5590 if down
5591 then
5592 let _, position, sh = state.uioh#scrollph in
5593 if y > truncate position && y < truncate (position +. sh)
5594 then state.mstate <- Mscrolly
5595 else scrolly y
5596 else
5597 state.mstate <- Mnone
5599 | 1 when y > conf.winh - state.hscrollh ->
5600 if down
5601 then
5602 let _, position, sw = state.uioh#scrollpw in
5603 if x > truncate position && x < truncate (position +. sw)
5604 then state.mstate <- Mscrollx
5605 else scrollx x
5606 else
5607 state.mstate <- Mnone
5609 | 1 ->
5610 let dest = if down then getunder x y else Unone in
5611 begin match dest with
5612 | Ulinkgoto _
5613 | Ulinkuri _
5614 | Uremote _
5615 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5616 gotounder dest
5618 | Unone when down ->
5619 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5620 state.mstate <- Mpan (x, y);
5622 | Unone | Utext _ ->
5623 if down
5624 then (
5625 if conf.angle mod 360 = 0
5626 then (
5627 state.mstate <- Msel ((x, y), (x, y));
5628 G.postRedisplay "mouse select";
5631 else (
5632 match state.mstate with
5633 | Mnone -> ()
5635 | Mzoom _ | Mscrollx | Mscrolly ->
5636 state.mstate <- Mnone
5638 | Mzoomrect ((x0, y0), _) ->
5639 zoomrect x0 y0 x y
5641 | Mpan _ ->
5642 Wsi.setcursor Wsi.CURSOR_INHERIT;
5643 state.mstate <- Mnone
5645 | Msel ((_, y0), (_, y1)) ->
5646 let rec loop = function
5647 | [] -> ()
5648 | l :: rest ->
5649 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5650 || ((y1 >= l.pagedispy
5651 && y1 <= (l.pagedispy + l.pagevh)))
5652 then
5653 match getopaque l.pageno with
5654 | Some opaque ->
5655 begin
5656 match Ne.pipe () with
5657 | Ne.Exn exn ->
5658 showtext '!'
5659 (Printf.sprintf
5660 "can not create sel pipe: %s"
5661 (Printexc.to_string exn));
5662 | Ne.Res (r, w) ->
5663 let doclose what fd =
5664 Ne.clo fd (fun msg ->
5665 dolog "%s close failed: %s" what msg)
5668 popen conf.selcmd [r, 0; w, -1];
5669 copysel w opaque;
5670 doclose "pipe/r" r;
5671 G.postRedisplay "copysel";
5672 with exn ->
5673 dolog "can not exectute %S: %s"
5674 conf.selcmd (Printexc.to_string exn);
5675 doclose "pipe/r" r;
5676 doclose "pipe/w" w;
5678 | None -> ()
5679 else loop rest
5681 loop state.layout;
5682 Wsi.setcursor Wsi.CURSOR_INHERIT;
5683 state.mstate <- Mnone;
5687 | _ -> ()
5690 let birdseyemouse button down x y mask
5691 (conf, leftx, _, hooverpageno, anchor) =
5692 match button with
5693 | 1 when down ->
5694 let rec loop = function
5695 | [] -> ()
5696 | l :: rest ->
5697 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5698 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5699 then (
5700 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5702 else loop rest
5704 loop state.layout
5705 | 3 -> ()
5706 | _ -> viewmouse button down x y mask
5709 let mouse button down x y mask =
5710 state.uioh <- state.uioh#button button down x y mask;
5713 let motion ~x ~y =
5714 state.uioh <- state.uioh#motion x y
5717 let pmotion ~x ~y =
5718 state.uioh <- state.uioh#pmotion x y;
5721 let uioh = object
5722 method display = ()
5724 method key key mask =
5725 begin match state.mode with
5726 | Textentry textentry -> textentrykeyboard key mask textentry
5727 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5728 | View -> viewkeyboard key mask
5729 | LinkNav linknav -> linknavkeyboard key mask linknav
5730 end;
5731 state.uioh
5733 method button button bstate x y mask =
5734 begin match state.mode with
5735 | LinkNav _
5736 | View -> viewmouse button bstate x y mask
5737 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5738 | Textentry _ -> ()
5739 end;
5740 state.uioh
5742 method motion x y =
5743 begin match state.mode with
5744 | Textentry _ -> ()
5745 | View | Birdseye _ | LinkNav _ ->
5746 match state.mstate with
5747 | Mzoom _ | Mnone -> ()
5749 | Mpan (x0, y0) ->
5750 let dx = x - x0
5751 and dy = y0 - y in
5752 state.mstate <- Mpan (x, y);
5753 if canpan ()
5754 then state.x <- state.x + dx;
5755 let y = clamp dy in
5756 gotoy_and_clear_text y
5758 | Msel (a, _) ->
5759 state.mstate <- Msel (a, (x, y));
5760 G.postRedisplay "motion select";
5762 | Mscrolly ->
5763 let y = min conf.winh (max 0 y) in
5764 scrolly y
5766 | Mscrollx ->
5767 let x = min conf.winw (max 0 x) in
5768 scrollx x
5770 | Mzoomrect (p0, _) ->
5771 state.mstate <- Mzoomrect (p0, (x, y));
5772 G.postRedisplay "motion zoomrect";
5773 end;
5774 state.uioh
5776 method pmotion x y =
5777 begin match state.mode with
5778 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5779 let rec loop = function
5780 | [] ->
5781 if hooverpageno != -1
5782 then (
5783 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5784 G.postRedisplay "pmotion birdseye no hoover";
5786 | l :: rest ->
5787 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5788 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5789 then (
5790 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5791 G.postRedisplay "pmotion birdseye hoover";
5793 else loop rest
5795 loop state.layout
5797 | Textentry _ -> ()
5799 | LinkNav _
5800 | View ->
5801 match state.mstate with
5802 | Mnone -> updateunder x y
5803 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5805 end;
5806 state.uioh
5808 method infochanged _ = ()
5810 method scrollph =
5811 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5812 let p, h = scrollph state.y maxy in
5813 state.scrollw, p, h
5815 method scrollpw =
5816 let winw = conf.winw - state.scrollw - 1 in
5817 let fwinw = float winw in
5818 let sw =
5819 let sw = fwinw /. float state.w in
5820 let sw = fwinw *. sw in
5821 max sw (float conf.scrollh)
5823 let position, sw =
5824 let f = state.w+winw in
5825 let r = float (winw-state.x) /. float f in
5826 let p = fwinw *. r in
5827 p-.sw/.2., sw
5829 let sw =
5830 if position +. sw > fwinw
5831 then fwinw -. position
5832 else sw
5834 state.hscrollh, position, sw
5836 method modehash =
5837 let modename =
5838 match state.mode with
5839 | LinkNav _ -> "links"
5840 | Textentry _ -> "textentry"
5841 | Birdseye _ -> "birdseye"
5842 | View -> "view"
5844 findkeyhash conf modename
5845 end;;
5847 module Config =
5848 struct
5849 open Parser
5851 let fontpath = ref "";;
5853 module KeyMap =
5854 Map.Make (struct type t = (int * int) let compare = compare end);;
5856 let unent s =
5857 let l = String.length s in
5858 let b = Buffer.create l in
5859 unent b s 0 l;
5860 Buffer.contents b;
5863 let home =
5864 try Sys.getenv "HOME"
5865 with exn ->
5866 prerr_endline
5867 ("Can not determine home directory location: " ^
5868 Printexc.to_string exn);
5872 let modifier_of_string = function
5873 | "alt" -> Wsi.altmask
5874 | "shift" -> Wsi.shiftmask
5875 | "ctrl" | "control" -> Wsi.ctrlmask
5876 | "meta" -> Wsi.metamask
5877 | _ -> 0
5880 let key_of_string =
5881 let r = Str.regexp "-" in
5882 fun s ->
5883 let elems = Str.full_split r s in
5884 let f n k m =
5885 let g s =
5886 let m1 = modifier_of_string s in
5887 if m1 = 0
5888 then (Wsi.namekey s, m)
5889 else (k, m lor m1)
5890 in function
5891 | Str.Delim s when n land 1 = 0 -> g s
5892 | Str.Text s -> g s
5893 | Str.Delim _ -> (k, m)
5895 let rec loop n k m = function
5896 | [] -> (k, m)
5897 | x :: xs ->
5898 let k, m = f n k m x in
5899 loop (n+1) k m xs
5901 loop 0 0 0 elems
5904 let keys_of_string =
5905 let r = Str.regexp "[ \t]" in
5906 fun s ->
5907 let elems = Str.split r s in
5908 List.map key_of_string elems
5911 let copykeyhashes c =
5912 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5915 let config_of c attrs =
5916 let apply c k v =
5918 match k with
5919 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5920 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5921 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5922 | "preload" -> { c with preload = bool_of_string v }
5923 | "page-bias" -> { c with pagebias = int_of_string v }
5924 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5925 | "auto-scroll-step" ->
5926 { c with autoscrollstep = max 0 (int_of_string v) }
5927 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5928 | "crop-hack" -> { c with crophack = bool_of_string v }
5929 | "throttle" ->
5930 let mw =
5931 match String.lowercase v with
5932 | "true" -> Some infinity
5933 | "false" -> None
5934 | f -> Some (float_of_string f)
5936 { c with maxwait = mw}
5937 | "highlight-links" -> { c with hlinks = bool_of_string v }
5938 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5939 | "vertical-margin" ->
5940 { c with interpagespace = max 0 (int_of_string v) }
5941 | "zoom" ->
5942 let zoom = float_of_string v /. 100. in
5943 let zoom = max zoom 0.0 in
5944 { c with zoom = zoom }
5945 | "presentation" -> { c with presentation = bool_of_string v }
5946 | "rotation-angle" -> { c with angle = int_of_string v }
5947 | "width" -> { c with winw = max 20 (int_of_string v) }
5948 | "height" -> { c with winh = max 20 (int_of_string v) }
5949 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5950 | "proportional-display" -> { c with proportional = bool_of_string v }
5951 | "pixmap-cache-size" ->
5952 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5953 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5954 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5955 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5956 | "persistent-location" -> { c with jumpback = bool_of_string v }
5957 | "background-color" -> { c with bgcolor = color_of_string v }
5958 | "scrollbar-in-presentation" ->
5959 { c with scrollbarinpm = bool_of_string v }
5960 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5961 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5962 | "mupdf-store-size" ->
5963 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5964 | "checkers" -> { c with checkers = bool_of_string v }
5965 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5966 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5967 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5968 | "uri-launcher" -> { c with urilauncher = unent v }
5969 | "path-launcher" -> { c with pathlauncher = unent v }
5970 | "color-space" -> { c with colorspace = colorspace_of_string v }
5971 | "invert-colors" -> { c with invert = bool_of_string v }
5972 | "brightness" -> { c with colorscale = float_of_string v }
5973 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5974 | "ghyllscroll" ->
5975 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5976 | "columns" ->
5977 let (n, _, _) as nab = multicolumns_of_string v in
5978 if n < 0
5979 then { c with columns = Csplit (-n, [||]) }
5980 else { c with columns = Cmulti (nab, [||]) }
5981 | "birds-eye-columns" ->
5982 { c with beyecolumns = Some (max (int_of_string v) 2) }
5983 | "selection-command" -> { c with selcmd = unent v }
5984 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5985 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
5986 | _ -> c
5987 with exn ->
5988 prerr_endline ("Error processing attribute (`" ^
5989 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5992 let rec fold c = function
5993 | [] -> c
5994 | (k, v) :: rest ->
5995 let c = apply c k v in
5996 fold c rest
5998 fold { c with keyhashes = copykeyhashes c } attrs;
6001 let fromstring f pos n v d =
6002 try f v
6003 with exn ->
6004 dolog "Error processing attribute (%S=%S) at %d\n%s"
6005 n v pos (Printexc.to_string exn)
6010 let bookmark_of attrs =
6011 let rec fold title page rely = function
6012 | ("title", v) :: rest -> fold v page rely rest
6013 | ("page", v) :: rest -> fold title v rely rest
6014 | ("rely", v) :: rest -> fold title page v rest
6015 | _ :: rest -> fold title page rely rest
6016 | [] -> title, page, rely
6018 fold "invalid" "0" "0" attrs
6021 let doc_of attrs =
6022 let rec fold path page rely pan = function
6023 | ("path", v) :: rest -> fold v page rely pan rest
6024 | ("page", v) :: rest -> fold path v rely pan rest
6025 | ("rely", v) :: rest -> fold path page v pan rest
6026 | ("pan", v) :: rest -> fold path page rely v rest
6027 | _ :: rest -> fold path page rely pan rest
6028 | [] -> path, page, rely, pan
6030 fold "" "0" "0" "0" attrs
6033 let map_of attrs =
6034 let rec fold rs ls = function
6035 | ("out", v) :: rest -> fold v ls rest
6036 | ("in", v) :: rest -> fold rs v rest
6037 | _ :: rest -> fold ls rs rest
6038 | [] -> ls, rs
6040 fold "" "" attrs
6043 let setconf dst src =
6044 dst.scrollbw <- src.scrollbw;
6045 dst.scrollh <- src.scrollh;
6046 dst.icase <- src.icase;
6047 dst.preload <- src.preload;
6048 dst.pagebias <- src.pagebias;
6049 dst.verbose <- src.verbose;
6050 dst.scrollstep <- src.scrollstep;
6051 dst.maxhfit <- src.maxhfit;
6052 dst.crophack <- src.crophack;
6053 dst.autoscrollstep <- src.autoscrollstep;
6054 dst.maxwait <- src.maxwait;
6055 dst.hlinks <- src.hlinks;
6056 dst.underinfo <- src.underinfo;
6057 dst.interpagespace <- src.interpagespace;
6058 dst.zoom <- src.zoom;
6059 dst.presentation <- src.presentation;
6060 dst.angle <- src.angle;
6061 dst.winw <- src.winw;
6062 dst.winh <- src.winh;
6063 dst.savebmarks <- src.savebmarks;
6064 dst.memlimit <- src.memlimit;
6065 dst.proportional <- src.proportional;
6066 dst.texcount <- src.texcount;
6067 dst.sliceheight <- src.sliceheight;
6068 dst.thumbw <- src.thumbw;
6069 dst.jumpback <- src.jumpback;
6070 dst.bgcolor <- src.bgcolor;
6071 dst.scrollbarinpm <- src.scrollbarinpm;
6072 dst.tilew <- src.tilew;
6073 dst.tileh <- src.tileh;
6074 dst.mustoresize <- src.mustoresize;
6075 dst.checkers <- src.checkers;
6076 dst.aalevel <- src.aalevel;
6077 dst.trimmargins <- src.trimmargins;
6078 dst.trimfuzz <- src.trimfuzz;
6079 dst.urilauncher <- src.urilauncher;
6080 dst.colorspace <- src.colorspace;
6081 dst.invert <- src.invert;
6082 dst.colorscale <- src.colorscale;
6083 dst.redirectstderr <- src.redirectstderr;
6084 dst.ghyllscroll <- src.ghyllscroll;
6085 dst.columns <- src.columns;
6086 dst.beyecolumns <- src.beyecolumns;
6087 dst.selcmd <- src.selcmd;
6088 dst.updatecurs <- src.updatecurs;
6089 dst.pathlauncher <- src.pathlauncher;
6090 dst.keyhashes <- copykeyhashes src;
6091 dst.hfsize <- src.hfsize;
6094 let get s =
6095 let h = Hashtbl.create 10 in
6096 let dc = { defconf with angle = defconf.angle } in
6097 let rec toplevel v t spos _ =
6098 match t with
6099 | Vdata | Vcdata | Vend -> v
6100 | Vopen ("llppconfig", _, closed) ->
6101 if closed
6102 then v
6103 else { v with f = llppconfig }
6104 | Vopen _ ->
6105 error "unexpected subelement at top level" s spos
6106 | Vclose _ -> error "unexpected close at top level" s spos
6108 and llppconfig v t spos _ =
6109 match t with
6110 | Vdata | Vcdata -> v
6111 | Vend -> error "unexpected end of input in llppconfig" s spos
6112 | Vopen ("defaults", attrs, closed) ->
6113 let c = config_of dc attrs in
6114 setconf dc c;
6115 if closed
6116 then v
6117 else { v with f = defaults }
6119 | Vopen ("ui-font", attrs, closed) ->
6120 let rec getsize size = function
6121 | [] -> size
6122 | ("size", v) :: rest ->
6123 let size =
6124 fromstring int_of_string spos "size" v fstate.fontsize in
6125 getsize size rest
6126 | l -> getsize size l
6128 fstate.fontsize <- getsize fstate.fontsize attrs;
6129 if closed
6130 then v
6131 else { v with f = uifont (Buffer.create 10) }
6133 | Vopen ("doc", attrs, closed) ->
6134 let pathent, spage, srely, span = doc_of attrs in
6135 let path = unent pathent
6136 and pageno = fromstring int_of_string spos "page" spage 0
6137 and rely = fromstring float_of_string spos "rely" srely 0.0
6138 and pan = fromstring int_of_string spos "pan" span 0 in
6139 let c = config_of dc attrs in
6140 let anchor = (pageno, rely) in
6141 if closed
6142 then (Hashtbl.add h path (c, [], pan, anchor); v)
6143 else { v with f = doc path pan anchor c [] }
6145 | Vopen _ ->
6146 error "unexpected subelement in llppconfig" s spos
6148 | Vclose "llppconfig" -> { v with f = toplevel }
6149 | Vclose _ -> error "unexpected close in llppconfig" s spos
6151 and defaults v t spos _ =
6152 match t with
6153 | Vdata | Vcdata -> v
6154 | Vend -> error "unexpected end of input in defaults" s spos
6155 | Vopen ("keymap", attrs, closed) ->
6156 let modename =
6157 try List.assoc "mode" attrs
6158 with Not_found -> "global" in
6159 if closed
6160 then v
6161 else
6162 let ret keymap =
6163 let h = findkeyhash dc modename in
6164 KeyMap.iter (Hashtbl.replace h) keymap;
6165 defaults
6167 { v with f = pkeymap ret KeyMap.empty }
6169 | Vopen (_, _, _) ->
6170 error "unexpected subelement in defaults" s spos
6172 | Vclose "defaults" ->
6173 { v with f = llppconfig }
6175 | Vclose _ -> error "unexpected close in defaults" s spos
6177 and uifont b v t spos epos =
6178 match t with
6179 | Vdata | Vcdata ->
6180 Buffer.add_substring b s spos (epos - spos);
6182 | Vopen (_, _, _) ->
6183 error "unexpected subelement in ui-font" s spos
6184 | Vclose "ui-font" ->
6185 if String.length !fontpath = 0
6186 then fontpath := Buffer.contents b;
6187 { v with f = llppconfig }
6188 | Vclose _ -> error "unexpected close in ui-font" s spos
6189 | Vend -> error "unexpected end of input in ui-font" s spos
6191 and doc path pan anchor c bookmarks v t spos _ =
6192 match t with
6193 | Vdata | Vcdata -> v
6194 | Vend -> error "unexpected end of input in doc" s spos
6195 | Vopen ("bookmarks", _, closed) ->
6196 if closed
6197 then v
6198 else { v with f = pbookmarks path pan anchor c bookmarks }
6200 | Vopen ("keymap", attrs, closed) ->
6201 let modename =
6202 try List.assoc "mode" attrs
6203 with Not_found -> "global"
6205 if closed
6206 then v
6207 else
6208 let ret keymap =
6209 let h = findkeyhash c modename in
6210 KeyMap.iter (Hashtbl.replace h) keymap;
6211 doc path pan anchor c bookmarks
6213 { v with f = pkeymap ret KeyMap.empty }
6215 | Vopen (_, _, _) ->
6216 error "unexpected subelement in doc" s spos
6218 | Vclose "doc" ->
6219 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6220 { v with f = llppconfig }
6222 | Vclose _ -> error "unexpected close in doc" s spos
6224 and pkeymap ret keymap v t spos _ =
6225 match t with
6226 | Vdata | Vcdata -> v
6227 | Vend -> error "unexpected end of input in keymap" s spos
6228 | Vopen ("map", attrs, closed) ->
6229 let r, l = map_of attrs in
6230 let kss = fromstring keys_of_string spos "in" r [] in
6231 let lss = fromstring keys_of_string spos "out" l [] in
6232 let keymap =
6233 match kss with
6234 | [] -> keymap
6235 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6236 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6238 if closed
6239 then { v with f = pkeymap ret keymap }
6240 else
6241 let f () = v in
6242 { v with f = skip "map" f }
6244 | Vopen _ ->
6245 error "unexpected subelement in keymap" s spos
6247 | Vclose "keymap" ->
6248 { v with f = ret keymap }
6250 | Vclose _ -> error "unexpected close in keymap" s spos
6252 and pbookmarks path pan anchor c bookmarks v t spos _ =
6253 match t with
6254 | Vdata | Vcdata -> v
6255 | Vend -> error "unexpected end of input in bookmarks" s spos
6256 | Vopen ("item", attrs, closed) ->
6257 let titleent, spage, srely = bookmark_of attrs in
6258 let page = fromstring int_of_string spos "page" spage 0
6259 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6260 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6261 if closed
6262 then { v with f = pbookmarks path pan anchor c bookmarks }
6263 else
6264 let f () = v in
6265 { v with f = skip "item" f }
6267 | Vopen _ ->
6268 error "unexpected subelement in bookmarks" s spos
6270 | Vclose "bookmarks" ->
6271 { v with f = doc path pan anchor c bookmarks }
6273 | Vclose _ -> error "unexpected close in bookmarks" s spos
6275 and skip tag f v t spos _ =
6276 match t with
6277 | Vdata | Vcdata -> v
6278 | Vend ->
6279 error ("unexpected end of input in skipped " ^ tag) s spos
6280 | Vopen (tag', _, closed) ->
6281 if closed
6282 then v
6283 else
6284 let f' () = { v with f = skip tag f } in
6285 { v with f = skip tag' f' }
6286 | Vclose ctag ->
6287 if tag = ctag
6288 then f ()
6289 else error ("unexpected close in skipped " ^ tag) s spos
6292 parse { f = toplevel; accu = () } s;
6293 h, dc;
6296 let do_load f ic =
6298 let len = in_channel_length ic in
6299 let s = String.create len in
6300 really_input ic s 0 len;
6301 f s;
6302 with
6303 | Parse_error (msg, s, pos) ->
6304 let subs = subs s pos in
6305 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6306 failwith ("parse error: " ^ s)
6308 | exn ->
6309 failwith ("config load error: " ^ Printexc.to_string exn)
6312 let defconfpath =
6313 let dir =
6315 let dir = Filename.concat home ".config" in
6316 if Sys.is_directory dir then dir else home
6317 with _ -> home
6319 Filename.concat dir "llpp.conf"
6322 let confpath = ref defconfpath;;
6324 let load1 f =
6325 if Sys.file_exists !confpath
6326 then
6327 match
6328 (try Some (open_in_bin !confpath)
6329 with exn ->
6330 prerr_endline
6331 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6332 Printexc.to_string exn);
6333 None
6335 with
6336 | Some ic ->
6337 begin try
6338 f (do_load get ic)
6339 with exn ->
6340 prerr_endline
6341 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6342 Printexc.to_string exn);
6343 end;
6344 close_in ic;
6346 | None -> ()
6347 else
6348 f (Hashtbl.create 0, defconf)
6351 let load () =
6352 let f (h, dc) =
6353 let pc, pb, px, pa =
6355 Hashtbl.find h (Filename.basename state.path)
6356 with Not_found -> dc, [], 0, (0, 0.0)
6358 setconf defconf dc;
6359 setconf conf pc;
6360 state.bookmarks <- pb;
6361 state.x <- px;
6362 state.scrollw <- conf.scrollbw;
6363 if conf.jumpback
6364 then state.anchor <- pa;
6365 cbput state.hists.nav pa;
6367 load1 f
6370 let add_attrs bb always dc c =
6371 let ob s a b =
6372 if always || a != b
6373 then Printf.bprintf bb "\n %s='%b'" s a
6374 and oi s a b =
6375 if always || a != b
6376 then Printf.bprintf bb "\n %s='%d'" s a
6377 and oI s a b =
6378 if always || a != b
6379 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6380 and oz s a b =
6381 if always || a <> b
6382 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6383 and oF s a b =
6384 if always || a <> b
6385 then Printf.bprintf bb "\n %s='%f'" s a
6386 and oc s a b =
6387 if always || a <> b
6388 then
6389 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6390 and oC s a b =
6391 if always || a <> b
6392 then
6393 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6394 and oR s a b =
6395 if always || a <> b
6396 then
6397 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6398 and os s a b =
6399 if always || a <> b
6400 then
6401 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6402 and og s a b =
6403 if always || a <> b
6404 then
6405 match a with
6406 | None -> ()
6407 | Some (_N, _A, _B) ->
6408 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6409 and oW s a b =
6410 if always || a <> b
6411 then
6412 let v =
6413 match a with
6414 | None -> "false"
6415 | Some f ->
6416 if f = infinity
6417 then "true"
6418 else string_of_float f
6420 Printf.bprintf bb "\n %s='%s'" s v
6421 and oco s a b =
6422 if always || a <> b
6423 then
6424 match a with
6425 | Cmulti ((n, a, b), _) when n > 1 ->
6426 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6427 | Csplit (n, _) when n > 1 ->
6428 Printf.bprintf bb "\n %s='%d'" s ~-n
6429 | _ -> ()
6430 and obeco s a b =
6431 if always || a <> b
6432 then
6433 match a with
6434 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6435 | _ -> ()
6437 let w, h =
6438 if always
6439 then dc.winw, dc.winh
6440 else
6441 match state.fullscreen with
6442 | Some wh -> wh
6443 | None -> c.winw, c.winh
6445 let zoom, presentation, interpagespace, maxwait =
6446 if always
6447 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6448 else
6449 match state.mode with
6450 | Birdseye (bc, _, _, _, _) ->
6451 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6452 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6454 oi "width" w dc.winw;
6455 oi "height" h dc.winh;
6456 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6457 oi "scroll-handle-height" c.scrollh dc.scrollh;
6458 ob "case-insensitive-search" c.icase dc.icase;
6459 ob "preload" c.preload dc.preload;
6460 oi "page-bias" c.pagebias dc.pagebias;
6461 oi "scroll-step" c.scrollstep dc.scrollstep;
6462 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6463 ob "max-height-fit" c.maxhfit dc.maxhfit;
6464 ob "crop-hack" c.crophack dc.crophack;
6465 oW "throttle" maxwait dc.maxwait;
6466 ob "highlight-links" c.hlinks dc.hlinks;
6467 ob "under-cursor-info" c.underinfo dc.underinfo;
6468 oi "vertical-margin" interpagespace dc.interpagespace;
6469 oz "zoom" zoom dc.zoom;
6470 ob "presentation" presentation dc.presentation;
6471 oi "rotation-angle" c.angle dc.angle;
6472 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6473 ob "proportional-display" c.proportional dc.proportional;
6474 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6475 oi "tex-count" c.texcount dc.texcount;
6476 oi "slice-height" c.sliceheight dc.sliceheight;
6477 oi "thumbnail-width" c.thumbw dc.thumbw;
6478 ob "persistent-location" c.jumpback dc.jumpback;
6479 oc "background-color" c.bgcolor dc.bgcolor;
6480 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6481 oi "tile-width" c.tilew dc.tilew;
6482 oi "tile-height" c.tileh dc.tileh;
6483 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6484 ob "checkers" c.checkers dc.checkers;
6485 oi "aalevel" c.aalevel dc.aalevel;
6486 ob "trim-margins" c.trimmargins dc.trimmargins;
6487 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6488 os "uri-launcher" c.urilauncher dc.urilauncher;
6489 os "path-launcher" c.pathlauncher dc.pathlauncher;
6490 oC "color-space" c.colorspace dc.colorspace;
6491 ob "invert-colors" c.invert dc.invert;
6492 oF "brightness" c.colorscale dc.colorscale;
6493 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6494 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6495 oco "columns" c.columns dc.columns;
6496 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6497 os "selection-command" c.selcmd dc.selcmd;
6498 ob "update-cursor" c.updatecurs dc.updatecurs;
6499 oi "hint-font-size" c.hfsize dc.hfsize;
6502 let keymapsbuf always dc c =
6503 let bb = Buffer.create 16 in
6504 let rec loop = function
6505 | [] -> ()
6506 | (modename, h) :: rest ->
6507 let dh = findkeyhash dc modename in
6508 if always || h <> dh
6509 then (
6510 if Hashtbl.length h > 0
6511 then (
6512 if Buffer.length bb > 0
6513 then Buffer.add_char bb '\n';
6514 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6515 Hashtbl.iter (fun i o ->
6516 let isdifferent = always ||
6518 let dO = Hashtbl.find dh i in
6519 dO <> o
6520 with Not_found -> true
6522 if isdifferent
6523 then
6524 let addkm (k, m) =
6525 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6526 if Wsi.withalt m then Buffer.add_string bb "alt-";
6527 if Wsi.withshift m then Buffer.add_string bb "shift-";
6528 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6529 Buffer.add_string bb (Wsi.keyname k);
6531 let addkms l =
6532 let rec loop = function
6533 | [] -> ()
6534 | km :: [] -> addkm km
6535 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6537 loop l
6539 Buffer.add_string bb "<map in='";
6540 addkm i;
6541 match o with
6542 | KMinsrt km ->
6543 Buffer.add_string bb "' out='";
6544 addkm km;
6545 Buffer.add_string bb "'/>\n"
6547 | KMinsrl kms ->
6548 Buffer.add_string bb "' out='";
6549 addkms kms;
6550 Buffer.add_string bb "'/>\n"
6552 | KMmulti (ins, kms) ->
6553 Buffer.add_char bb ' ';
6554 addkms ins;
6555 Buffer.add_string bb "' out='";
6556 addkms kms;
6557 Buffer.add_string bb "'/>\n"
6558 ) h;
6559 Buffer.add_string bb "</keymap>";
6562 loop rest
6564 loop c.keyhashes;
6568 let save () =
6569 let uifontsize = fstate.fontsize in
6570 let bb = Buffer.create 32768 in
6571 let f (h, dc) =
6572 let dc = if conf.bedefault then conf else dc in
6573 Buffer.add_string bb "<llppconfig>\n";
6575 if String.length !fontpath > 0
6576 then
6577 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6578 uifontsize
6579 !fontpath
6580 else (
6581 if uifontsize <> 14
6582 then
6583 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6586 Buffer.add_string bb "<defaults ";
6587 add_attrs bb true dc dc;
6588 let kb = keymapsbuf true dc dc in
6589 if Buffer.length kb > 0
6590 then (
6591 Buffer.add_string bb ">\n";
6592 Buffer.add_buffer bb kb;
6593 Buffer.add_string bb "\n</defaults>\n";
6595 else Buffer.add_string bb "/>\n";
6597 let adddoc path pan anchor c bookmarks =
6598 if bookmarks == [] && c = dc && anchor = emptyanchor
6599 then ()
6600 else (
6601 Printf.bprintf bb "<doc path='%s'"
6602 (enent path 0 (String.length path));
6604 if anchor <> emptyanchor
6605 then (
6606 let n, y = anchor in
6607 Printf.bprintf bb " page='%d'" n;
6608 if y > 1e-6
6609 then
6610 Printf.bprintf bb " rely='%f'" y
6614 if pan != 0
6615 then Printf.bprintf bb " pan='%d'" pan;
6617 add_attrs bb false dc c;
6618 let kb = keymapsbuf false dc c in
6620 begin match bookmarks with
6621 | [] ->
6622 if Buffer.length kb > 0
6623 then (
6624 Buffer.add_string bb ">\n";
6625 Buffer.add_buffer bb kb;
6626 Buffer.add_string bb "\n</doc>\n";
6628 else Buffer.add_string bb "/>\n"
6629 | _ ->
6630 Buffer.add_string bb ">\n<bookmarks>\n";
6631 List.iter (fun (title, _level, (page, rely)) ->
6632 Printf.bprintf bb
6633 "<item title='%s' page='%d'"
6634 (enent title 0 (String.length title))
6635 page
6637 if rely > 1e-6
6638 then
6639 Printf.bprintf bb " rely='%f'" rely
6641 Buffer.add_string bb "/>\n";
6642 ) bookmarks;
6643 Buffer.add_string bb "</bookmarks>";
6644 if Buffer.length kb > 0
6645 then (
6646 Buffer.add_string bb "\n";
6647 Buffer.add_buffer bb kb;
6649 Buffer.add_string bb "\n</doc>\n";
6650 end;
6654 let pan, conf =
6655 match state.mode with
6656 | Birdseye (c, pan, _, _, _) ->
6657 let beyecolumns =
6658 match conf.columns with
6659 | Cmulti ((c, _, _), _) -> Some c
6660 | Csingle -> None
6661 | Csplit _ -> None
6662 and columns =
6663 match c.columns with
6664 | Cmulti (c, _) -> Cmulti (c, [||])
6665 | Csingle -> Csingle
6666 | Csplit _ -> failwith "quit from bird's eye while split"
6668 pan, { c with beyecolumns = beyecolumns; columns = columns }
6669 | _ -> state.x, conf
6671 let basename = Filename.basename state.path in
6672 adddoc basename pan (getanchor ())
6673 { conf with
6674 autoscrollstep =
6675 match state.autoscroll with
6676 | Some step -> step
6677 | None -> conf.autoscrollstep }
6678 (if conf.savebmarks then state.bookmarks else []);
6680 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6681 if basename <> path
6682 then adddoc path x y c bookmarks
6683 ) h;
6684 Buffer.add_string bb "</llppconfig>";
6686 load1 f;
6687 if Buffer.length bb > 0
6688 then
6690 let tmp = !confpath ^ ".tmp" in
6691 let oc = open_out_bin tmp in
6692 Buffer.output_buffer oc bb;
6693 close_out oc;
6694 Unix.rename tmp !confpath;
6695 with exn ->
6696 prerr_endline
6697 ("error while saving configuration: " ^ Printexc.to_string exn)
6699 end;;
6701 let () =
6702 Arg.parse
6703 (Arg.align
6704 [("-p", Arg.String (fun s -> state.password <- s) ,
6705 "<password> Set password");
6707 ("-f", Arg.String (fun s -> Config.fontpath := s),
6708 "<path> Set path to the user interface font");
6710 ("-c", Arg.String (fun s -> Config.confpath := s),
6711 "<path> Set path to the configuration file");
6713 ("-v", Arg.Unit (fun () ->
6714 Printf.printf
6715 "%s\nconfiguration path: %s\n"
6716 (version ())
6717 Config.defconfpath
6719 exit 0), " Print version and exit");
6722 (fun s -> state.path <- s)
6723 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6725 if String.length state.path = 0
6726 then (prerr_endline "file name missing"; exit 1);
6728 Config.load ();
6730 let globalkeyhash = findkeyhash conf "global" in
6731 let wsfd, winw, winh = Wsi.init (object
6732 method expose =
6733 if nogeomcmds state.geomcmds || platform == Posx
6734 then display ()
6735 else (
6736 GlFunc.draw_buffer `front;
6737 GlClear.color (scalecolor2 conf.bgcolor);
6738 GlClear.clear [`color];
6739 GlFunc.draw_buffer `back;
6741 method display = display ()
6742 method reshape w h = reshape w h
6743 method mouse b d x y m = mouse b d x y m
6744 method motion x y = state.mpos <- (x, y); motion x y
6745 method pmotion x y = state.mpos <- (x, y); pmotion x y
6746 method key k m =
6747 let mascm = m land (
6748 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6749 ) in
6750 match state.keystate with
6751 | KSnone ->
6752 let km = k, mascm in
6753 begin
6754 match
6755 let modehash = state.uioh#modehash in
6756 try Hashtbl.find modehash km
6757 with Not_found ->
6758 try Hashtbl.find globalkeyhash km
6759 with Not_found -> KMinsrt (k, m)
6760 with
6761 | KMinsrt (k, m) -> keyboard k m
6762 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6763 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6765 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6766 List.iter (fun (k, m) -> keyboard k m) insrt;
6767 state.keystate <- KSnone
6768 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6769 state.keystate <- KSinto (keys, insrt)
6770 | _ ->
6771 state.keystate <- KSnone
6773 method enter x y = state.mpos <- (x, y); pmotion x y
6774 method leave = state.mpos <- (-1, -1)
6775 method quit = raise Quit
6776 end) conf.winw conf.winh (platform = Posx) in
6778 state.wsfd <- wsfd;
6780 if not (
6781 List.exists GlMisc.check_extension
6782 [ "GL_ARB_texture_rectangle"
6783 ; "GL_EXT_texture_recangle"
6784 ; "GL_NV_texture_rectangle" ]
6786 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6788 let cr, sw =
6789 match Ne.pipe () with
6790 | Ne.Exn exn ->
6791 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6792 exit 1
6793 | Ne.Res rw -> rw
6794 and sr, cw =
6795 match Ne.pipe () with
6796 | Ne.Exn exn ->
6797 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6798 exit 1
6799 | Ne.Res rw -> rw
6802 cloexec cr;
6803 cloexec sw;
6804 cloexec sr;
6805 cloexec cw;
6807 setcheckers conf.checkers;
6808 redirectstderr ();
6810 init (cr, cw) (
6811 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6812 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6813 !Config.fontpath
6815 state.sr <- sr;
6816 state.sw <- sw;
6817 state.text <- "Opening " ^ state.path;
6818 reshape winw winh;
6819 opendoc state.path state.password;
6820 state.uioh <- uioh;
6822 let rec loop deadline =
6823 let r =
6824 match state.errfd with
6825 | None -> [state.sr; state.wsfd]
6826 | Some fd -> [state.sr; state.wsfd; fd]
6828 if state.redisplay
6829 then (
6830 state.redisplay <- false;
6831 display ();
6833 let timeout =
6834 let now = now () in
6835 if deadline > now
6836 then (
6837 if deadline = infinity
6838 then ~-.1.0
6839 else max 0.0 (deadline -. now)
6841 else 0.0
6843 let r, _, _ =
6844 try Unix.select r [] [] timeout
6845 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6847 begin match r with
6848 | [] ->
6849 state.ghyll None;
6850 let newdeadline =
6851 if state.ghyll == noghyll
6852 then
6853 match state.autoscroll with
6854 | Some step when step != 0 ->
6855 let y = state.y + step in
6856 let y =
6857 if y < 0
6858 then state.maxy
6859 else if y >= state.maxy then 0 else y
6861 gotoy y;
6862 if state.mode = View
6863 then state.text <- "";
6864 deadline +. 0.01
6865 | _ -> infinity
6866 else deadline +. 0.01
6868 loop newdeadline
6870 | l ->
6871 let rec checkfds = function
6872 | [] -> ()
6873 | fd :: rest when fd = state.sr ->
6874 let cmd = readcmd state.sr in
6875 act cmd;
6876 checkfds rest
6878 | fd :: rest when fd = state.wsfd ->
6879 Wsi.readresp fd;
6880 checkfds rest
6882 | fd :: rest ->
6883 let s = String.create 80 in
6884 let n = Unix.read fd s 0 80 in
6885 if conf.redirectstderr
6886 then (
6887 Buffer.add_substring state.errmsgs s 0 n;
6888 state.newerrmsgs <- true;
6889 state.redisplay <- true;
6891 else (
6892 prerr_string (String.sub s 0 n);
6893 flush stderr;
6895 checkfds rest
6897 checkfds l;
6898 let newdeadline =
6899 let deadline1 =
6900 if deadline = infinity
6901 then now () +. 0.01
6902 else deadline
6904 match state.autoscroll with
6905 | Some step when step != 0 -> deadline1
6906 | _ -> if state.ghyll == noghyll then infinity else deadline1
6908 loop newdeadline
6909 end;
6912 loop infinity;
6913 with Quit ->
6914 Config.save ();
6915 exit 0;