Avoid doing unnecessary work in flushtiles
[llpp.git] / main.ml
blob5fb7d28fd98f83ba4eec47cb471cda78fb708f4e
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 type params = (angle * proportional * trimparams
17 * texcount * sliceheight * memsize
18 * colorspace * fontpath * trimcachepath
19 * haspbo)
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 dtop = float
36 and fontpath = string
37 and trimcachepath = string
38 and memsize = int
39 and aalevel = int
40 and irect = (int * int * int * int)
41 and trimparams = (trimmargins * irect)
42 and colorspace = | Rgb | Bgr | Gray
43 and haspbo = bool
46 type x = int
47 and y = int
48 and tilex = int
49 and tiley = int
50 and tileparams = (x * y * width * height * tilex * tiley)
53 type link =
54 | Lnotfound
55 | Lfound of int
56 and linkdir =
57 | LDfirst
58 | LDlast
59 | LDfirstvisible of (int * int * int)
60 | LDleft of int
61 | LDright of int
62 | LDdown of int
63 | LDup of int
66 type pagewithlinks =
67 | Pwlnotfound
68 | Pwl of int
71 type keymap =
72 | KMinsrt of key
73 | KMinsrl of key list
74 | KMmulti of key list * key list
75 and key = int * int
76 and keyhash = (key, keymap) Hashtbl.t
77 and keystate =
78 | KSnone
79 | KSinto of (key list * key list)
82 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
83 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
85 type pipe = (Unix.file_descr * Unix.file_descr);;
87 external init : pipe -> params -> unit = "ml_init";;
88 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
89 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
90 external getpdimrect : int -> float array = "ml_getpdimrect";;
91 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
92 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
93 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
94 external measurestr : int -> string -> float = "ml_measure_string";;
95 external getmaxw : unit -> float = "ml_getmaxw";;
96 external postprocess :
97 opaque -> int -> int -> int -> (int * string * int) -> int
98 = "ml_postprocess";;
99 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
100 external platform : unit -> platform = "ml_platform";;
101 external setaalevel : int -> unit = "ml_setaalevel";;
102 external realloctexts : int -> bool = "ml_realloctexts";;
103 external findlink : opaque -> linkdir -> link = "ml_findlink";;
104 external getlink : opaque -> int -> under = "ml_getlink";;
105 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
106 external getlinkcount : opaque -> int = "ml_getlinkcount";;
107 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
108 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
109 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
110 external freepbo : string -> unit = "ml_freepbo";;
111 external unmappbo : string -> unit = "ml_unmappbo";;
112 external pbousable : unit -> bool = "ml_pbo_usable";;
113 external unproject : opaque -> int -> int -> (int * int) option
114 = "ml_unproject";;
115 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
117 let platform_to_string = function
118 | Punknown -> "unknown"
119 | Plinux -> "Linux"
120 | Posx -> "OSX"
121 | Psun -> "Sun"
122 | Pfreebsd -> "FreeBSD"
123 | Pdragonflybsd -> "DragonflyBSD"
124 | Popenbsd -> "OpenBSD"
125 | Pnetbsd -> "NetBSD"
126 | Pcygwin -> "Cygwin"
129 let platform = platform ();;
131 let now = Unix.gettimeofday;;
133 let popen cmd fda =
134 if platform = Pcygwin
135 then (
136 let sh = "/bin/sh" in
137 let args = [|sh; "-c"; cmd|] in
138 let rec std si so se = function
139 | [] -> si, so, se
140 | (fd, 0) :: rest -> std fd so se rest
141 | (fd, -1) :: rest ->
142 Unix.set_close_on_exec fd;
143 std si so se rest
144 | (_, n) :: _ ->
145 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
147 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
148 ignore (Unix.create_process sh args si so se)
150 else popen cmd fda;
153 type mpos = int * int
154 and mstate =
155 | Msel of (mpos * mpos)
156 | Mpan of mpos
157 | Mscrolly | Mscrollx
158 | Mzoom of (int * int)
159 | Mzoomrect of (mpos * mpos)
160 | Mnone
163 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
164 and onkey = string -> int -> te
165 and ondone = string -> unit
166 and histcancel = unit -> unit
167 and onhist = ((histcmd -> string) * histcancel)
168 and histcmd = HCnext | HCprev | HCfirst | HClast
169 and cancelonempty = bool
170 and te =
171 | TEstop
172 | TEdone of string
173 | TEcont of string
174 | TEswitch of textentry
177 type 'a circbuf =
178 { store : 'a array
179 ; mutable rc : int
180 ; mutable wc : int
181 ; mutable len : int
185 let bound v minv maxv =
186 max minv (min maxv v);
189 let cbnew n v =
190 { store = Array.create n v
191 ; rc = 0
192 ; wc = 0
193 ; len = 0
197 let cbcap b = Array.length b.store;;
199 let cbput b v =
200 let cap = cbcap b in
201 b.store.(b.wc) <- v;
202 b.wc <- (b.wc + 1) mod cap;
203 b.rc <- b.wc;
204 b.len <- min (b.len + 1) cap;
207 let cbempty b = b.len = 0;;
209 let cbgetg b circular dir =
210 if cbempty b
211 then b.store.(0)
212 else
213 let rc = b.rc + dir in
214 let rc =
215 if circular
216 then (
217 if rc = -1
218 then b.len-1
219 else (
220 if rc >= b.len
221 then 0
222 else rc
225 else bound rc 0 (b.len-1)
227 b.rc <- rc;
228 b.store.(rc);
231 let cbget b = cbgetg b false;;
232 let cbgetc b = cbgetg b true;;
234 let drawstring size x y s =
235 Gl.enable `blend;
236 Gl.enable `texture_2d;
237 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
238 ignore (drawstr size x y s);
239 Gl.disable `blend;
240 Gl.disable `texture_2d;
243 let drawstring1 size x y s =
244 drawstr size x y s;
247 let drawstring2 size x y fmt =
248 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
251 type page =
252 { pageno : int
253 ; pagedimno : int
254 ; pagew : int
255 ; pageh : int
256 ; pagex : int
257 ; pagey : int
258 ; pagevw : int
259 ; pagevh : int
260 ; pagedispx : int
261 ; pagedispy : int
262 ; pagecol : int
266 let debugl l =
267 dolog "l %d dim=%d {" l.pageno l.pagedimno;
268 dolog " WxH %dx%d" l.pagew l.pageh;
269 dolog " vWxH %dx%d" l.pagevw l.pagevh;
270 dolog " pagex,y %d,%d" l.pagex l.pagey;
271 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
272 dolog " column %d" l.pagecol;
273 dolog "}";
276 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
277 dolog "rect {";
278 dolog " x0,y0=(% f, % f)" x0 y0;
279 dolog " x1,y1=(% f, % f)" x1 y1;
280 dolog " x2,y2=(% f, % f)" x2 y2;
281 dolog " x3,y3=(% f, % f)" x3 y3;
282 dolog "}";
285 type multicolumns = multicol * pagegeom
286 and singlecolumn = pagegeom
287 and splitcolumns = columncount * pagegeom
288 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
289 and multicol = columncount * covercount * covercount
290 and pdimno = int
291 and columncount = int
292 and covercount = int;;
294 type conf =
295 { mutable scrollbw : int
296 ; mutable scrollh : int
297 ; mutable icase : bool
298 ; mutable preload : bool
299 ; mutable pagebias : int
300 ; mutable verbose : bool
301 ; mutable debug : bool
302 ; mutable scrollstep : int
303 ; mutable hscrollstep : int
304 ; mutable maxhfit : bool
305 ; mutable crophack : bool
306 ; mutable autoscrollstep : int
307 ; mutable maxwait : float option
308 ; mutable hlinks : bool
309 ; mutable underinfo : bool
310 ; mutable interpagespace : interpagespace
311 ; mutable zoom : float
312 ; mutable presentation : bool
313 ; mutable angle : angle
314 ; mutable cwinw : int
315 ; mutable cwinh : int
316 ; mutable savebmarks : bool
317 ; mutable proportional : proportional
318 ; mutable trimmargins : trimmargins
319 ; mutable trimfuzz : irect
320 ; mutable memlimit : memsize
321 ; mutable texcount : texcount
322 ; mutable sliceheight : sliceheight
323 ; mutable thumbw : width
324 ; mutable jumpback : bool
325 ; mutable bgcolor : float * float * float
326 ; mutable bedefault : bool
327 ; mutable scrollbarinpm : bool
328 ; mutable tilew : int
329 ; mutable tileh : int
330 ; mutable mustoresize : memsize
331 ; mutable checkers : bool
332 ; mutable aalevel : int
333 ; mutable urilauncher : string
334 ; mutable pathlauncher : string
335 ; mutable colorspace : colorspace
336 ; mutable invert : bool
337 ; mutable colorscale : float
338 ; mutable redirectstderr : bool
339 ; mutable ghyllscroll : (int * int * int) option
340 ; mutable columns : columns
341 ; mutable beyecolumns : columncount option
342 ; mutable selcmd : string
343 ; mutable updatecurs : bool
344 ; mutable keyhashes : (string * keyhash) list
345 ; mutable hfsize : int
346 ; mutable pgscale : float
347 ; mutable usepbo : bool
348 ; mutable wheelbypage : bool
349 ; mutable stcmd : string
351 and columns =
352 | Csingle of singlecolumn
353 | Cmulti of multicolumns
354 | Csplit of splitcolumns
357 type anchor = pageno * top * dtop;;
359 type outline = string * int * anchor;;
361 type rect = float * float * float * float * float * float * float * float;;
363 type tile = opaque * pixmapsize * elapsed
364 and elapsed = float;;
365 type pagemapkey = pageno * gen;;
366 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
367 and row = int
368 and col = int;;
370 let emptyanchor = (0, 0.0, 0.0);;
372 type infochange = | Memused | Docinfo | Pdim;;
374 class type uioh = object
375 method display : unit
376 method key : int -> int -> uioh
377 method button : int -> bool -> int -> int -> int -> uioh
378 method motion : int -> int -> uioh
379 method pmotion : int -> int -> uioh
380 method infochanged : infochange -> unit
381 method scrollpw : (int * float * float)
382 method scrollph : (int * float * float)
383 method modehash : keyhash
384 end;;
386 type mode =
387 | Birdseye of (conf * leftx * pageno * pageno * anchor)
388 | Textentry of (textentry * onleave)
389 | View
390 | LinkNav of linktarget
391 and onleave = leavetextentrystatus -> unit
392 and leavetextentrystatus = | Cancel | Confirm
393 and helpitem = string * int * action
394 and action =
395 | Noaction
396 | Action of (uioh -> uioh)
397 and linktarget =
398 | Ltexact of (pageno * int)
399 | Ltgendir of int
402 let isbirdseye = function Birdseye _ -> true | _ -> false;;
403 let istextentry = function Textentry _ -> true | _ -> false;;
405 type currently =
406 | Idle
407 | Loading of (page * gen)
408 | Tiling of (
409 page * opaque * colorspace * angle * gen * col * row * width * height
411 | Outlining of outline list
414 let emptykeyhash = Hashtbl.create 0;;
415 let nouioh : uioh = object (self)
416 method display = ()
417 method key _ _ = self
418 method button _ _ _ _ _ = self
419 method motion _ _ = self
420 method pmotion _ _ = self
421 method infochanged _ = ()
422 method scrollpw = (0, nan, nan)
423 method scrollph = (0, nan, nan)
424 method modehash = emptykeyhash
425 end;;
427 type state =
428 { mutable sr : Unix.file_descr
429 ; mutable sw : Unix.file_descr
430 ; mutable wsfd : Unix.file_descr
431 ; mutable errfd : Unix.file_descr option
432 ; mutable stderr : Unix.file_descr
433 ; mutable errmsgs : Buffer.t
434 ; mutable newerrmsgs : bool
435 ; mutable w : int
436 ; mutable x : int
437 ; mutable y : int
438 ; mutable scrollw : int
439 ; mutable hscrollh : int
440 ; mutable anchor : anchor
441 ; mutable ranchors : (string * string * anchor) list
442 ; mutable maxy : int
443 ; mutable layout : page list
444 ; pagemap : (pagemapkey, opaque) Hashtbl.t
445 ; tilemap : (tilemapkey, tile) Hashtbl.t
446 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
447 ; mutable pdims : (pageno * width * height * leftx) list
448 ; mutable pagecount : int
449 ; mutable currently : currently
450 ; mutable mstate : mstate
451 ; mutable searchpattern : string
452 ; mutable rects : (pageno * recttype * rect) list
453 ; mutable rects1 : (pageno * recttype * rect) list
454 ; mutable text : string
455 ; mutable winstate : Wsi.winstate list
456 ; mutable mode : mode
457 ; mutable uioh : uioh
458 ; mutable outlines : outline array
459 ; mutable bookmarks : outline list
460 ; mutable path : string
461 ; mutable password : string
462 ; mutable nameddest : string
463 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
464 ; mutable memused : memsize
465 ; mutable gen : gen
466 ; mutable throttle : (page list * int * float) option
467 ; mutable autoscroll : int option
468 ; mutable ghyll : (int option -> unit)
469 ; mutable help : helpitem array
470 ; mutable docinfo : (int * string) list
471 ; mutable texid : GlTex.texture_id option
472 ; hists : hists
473 ; mutable prevzoom : float
474 ; mutable progress : float
475 ; mutable redisplay : bool
476 ; mutable mpos : mpos
477 ; mutable keystate : keystate
478 ; mutable glinks : bool
479 ; mutable prevcolumns : (columns * float) option
480 ; mutable wthack : bool
481 ; mutable winw : int
482 ; mutable winh : int
483 ; mutable reprf : (unit -> unit)
485 and hists =
486 { pat : string circbuf
487 ; pag : string circbuf
488 ; nav : anchor circbuf
489 ; sel : string circbuf
493 let defconf =
494 { scrollbw = 7
495 ; scrollh = 12
496 ; icase = true
497 ; preload = true
498 ; pagebias = 0
499 ; verbose = false
500 ; debug = false
501 ; scrollstep = 24
502 ; hscrollstep = 24
503 ; maxhfit = true
504 ; crophack = false
505 ; autoscrollstep = 2
506 ; maxwait = None
507 ; hlinks = false
508 ; underinfo = false
509 ; interpagespace = 2
510 ; zoom = 1.0
511 ; presentation = false
512 ; angle = 0
513 ; cwinw = 900
514 ; cwinh = 900
515 ; savebmarks = true
516 ; proportional = true
517 ; trimmargins = false
518 ; trimfuzz = (0,0,0,0)
519 ; memlimit = 32 lsl 20
520 ; texcount = 256
521 ; sliceheight = 24
522 ; thumbw = 76
523 ; jumpback = true
524 ; bgcolor = (0.5, 0.5, 0.5)
525 ; bedefault = false
526 ; scrollbarinpm = true
527 ; tilew = 2048
528 ; tileh = 2048
529 ; mustoresize = 256 lsl 20
530 ; checkers = true
531 ; aalevel = 8
532 ; urilauncher =
533 (match platform with
534 | Plinux | Pfreebsd | Pdragonflybsd
535 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
536 | Posx -> "open \"%s\""
537 | Pcygwin -> "cygstart \"%s\""
538 | Punknown -> "echo %s")
539 ; pathlauncher = "lp \"%s\""
540 ; selcmd =
541 (match platform with
542 | Plinux | Pfreebsd | Pdragonflybsd
543 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
544 | Posx -> "pbcopy"
545 | Pcygwin -> "wsel"
546 | Punknown -> "cat")
547 ; colorspace = Rgb
548 ; invert = false
549 ; colorscale = 1.0
550 ; redirectstderr = false
551 ; ghyllscroll = None
552 ; columns = Csingle [||]
553 ; beyecolumns = None
554 ; updatecurs = false
555 ; hfsize = 12
556 ; pgscale = 1.0
557 ; usepbo = false
558 ; wheelbypage = false
559 ; stcmd = "echo SyncTex"
560 ; keyhashes =
561 let mk n = (n, Hashtbl.create 1) in
562 [ mk "global"
563 ; mk "info"
564 ; mk "help"
565 ; mk "outline"
566 ; mk "listview"
567 ; mk "birdseye"
568 ; mk "textentry"
569 ; mk "links"
570 ; mk "view"
575 let wtmode = ref false;;
577 let findkeyhash c name =
578 try List.assoc name c.keyhashes
579 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
582 let conf = { defconf with angle = defconf.angle };;
584 let pgscale h = truncate (float h *. conf.pgscale);;
586 type fontstate =
587 { mutable fontsize : int
588 ; mutable wwidth : float
589 ; mutable maxrows : int
593 let fstate =
594 { fontsize = 14
595 ; wwidth = nan
596 ; maxrows = -1
600 let geturl s =
601 let colonpos = try String.index s ':' with Not_found -> -1 in
602 let len = String.length s in
603 if colonpos >= 0 && colonpos + 3 < len
604 then (
605 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
606 then
607 let schemestartpos =
608 try String.rindex_from s colonpos ' '
609 with Not_found -> -1
611 let scheme =
612 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
614 match scheme with
615 | "http" | "ftp" | "mailto" ->
616 let epos =
617 try String.index_from s colonpos ' '
618 with Not_found -> len
620 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
621 | _ -> ""
622 else ""
624 else ""
627 let gotouri uri =
628 if String.length conf.urilauncher = 0
629 then print_endline uri
630 else (
631 let url = geturl uri in
632 if String.length url = 0
633 then print_endline uri
634 else
635 let re = Str.regexp "%s" in
636 let command = Str.global_replace re url conf.urilauncher in
637 try popen command []
638 with exn ->
639 Printf.eprintf
640 "failed to execute `%s': %s\n" command (exntos exn);
641 flush stderr;
645 let version () =
646 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
647 (platform_to_string platform) Sys.word_size Sys.ocaml_version
650 let makehelp () =
651 let strings = version () :: "" :: Help.keys in
652 Array.of_list (
653 List.map (fun s ->
654 let url = geturl s in
655 if String.length url > 0
656 then (s, 0, Action (fun u -> gotouri url; u))
657 else (s, 0, Noaction)
658 ) strings);
661 let noghyll _ = ();;
662 let firstgeomcmds = "", [];;
663 let noreprf () = ();;
665 let state =
666 { sr = Unix.stdin
667 ; sw = Unix.stdin
668 ; wsfd = Unix.stdin
669 ; errfd = None
670 ; stderr = Unix.stderr
671 ; errmsgs = Buffer.create 0
672 ; newerrmsgs = false
673 ; x = 0
674 ; y = 0
675 ; w = 0
676 ; scrollw = 0
677 ; hscrollh = 0
678 ; anchor = emptyanchor
679 ; ranchors = []
680 ; layout = []
681 ; maxy = max_int
682 ; tilelru = Queue.create ()
683 ; pagemap = Hashtbl.create 10
684 ; tilemap = Hashtbl.create 10
685 ; pdims = []
686 ; pagecount = 0
687 ; currently = Idle
688 ; mstate = Mnone
689 ; rects = []
690 ; rects1 = []
691 ; text = ""
692 ; mode = View
693 ; winstate = []
694 ; searchpattern = ""
695 ; outlines = [||]
696 ; bookmarks = []
697 ; path = ""
698 ; password = ""
699 ; nameddest = ""
700 ; geomcmds = firstgeomcmds
701 ; hists =
702 { nav = cbnew 10 emptyanchor
703 ; pat = cbnew 10 ""
704 ; pag = cbnew 10 ""
705 ; sel = cbnew 10 ""
707 ; memused = 0
708 ; gen = 0
709 ; throttle = None
710 ; autoscroll = None
711 ; ghyll = noghyll
712 ; help = makehelp ()
713 ; docinfo = []
714 ; texid = None
715 ; prevzoom = 1.0
716 ; progress = -1.0
717 ; uioh = nouioh
718 ; redisplay = true
719 ; mpos = (-1, -1)
720 ; keystate = KSnone
721 ; glinks = false
722 ; prevcolumns = None
723 ; wthack = false
724 ; winw = -1
725 ; winh = -1
726 ; reprf = noreprf
730 let setfontsize n =
731 fstate.fontsize <- n;
732 fstate.wwidth <- measurestr fstate.fontsize "w";
733 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
736 let vlog fmt =
737 if conf.verbose
738 then
739 Printf.kprintf prerr_endline fmt
740 else
741 Printf.kprintf ignore fmt
744 let launchpath () =
745 if String.length conf.pathlauncher = 0
746 then print_endline state.path
747 else (
748 let re = Str.regexp "%s" in
749 let command = Str.global_replace re state.path conf.pathlauncher in
750 try popen command []
751 with exn ->
752 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
753 flush stderr;
757 module Ne = struct
758 type 'a t = | Res of 'a | Exn of exn;;
760 let pipe () =
761 try Res (Unix.pipe ())
762 with exn -> Exn exn
765 let clo fd f =
766 try tempfailureretry Unix.close fd
767 with exn -> f (exntos exn)
770 let dup fd =
771 try Res (tempfailureretry Unix.dup fd)
772 with exn -> Exn exn
775 let dup2 fd1 fd2 =
776 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
777 with exn -> Exn exn
779 end;;
781 let redirectstderr () =
782 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
783 if conf.redirectstderr
784 then
785 match Ne.pipe () with
786 | Ne.Exn exn ->
787 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
789 | Ne.Res (r, w) ->
790 begin match Ne.dup Unix.stderr with
791 | Ne.Exn exn ->
792 dolog "failed to dup stderr: %s" (exntos exn);
793 Ne.clo r (clofail "pipe/r");
794 Ne.clo w (clofail "pipe/w");
796 | Ne.Res dupstderr ->
797 begin match Ne.dup2 w Unix.stderr with
798 | Ne.Exn exn ->
799 dolog "failed to dup2 to stderr: %s" (exntos exn);
800 Ne.clo dupstderr (clofail "stderr duplicate");
801 Ne.clo r (clofail "redir pipe/r");
802 Ne.clo w (clofail "redir pipe/w");
804 | Ne.Res () ->
805 state.stderr <- dupstderr;
806 state.errfd <- Some r;
807 end;
809 else (
810 state.newerrmsgs <- false;
811 begin match state.errfd with
812 | Some fd ->
813 begin match Ne.dup2 state.stderr Unix.stderr with
814 | Ne.Exn exn ->
815 dolog "failed to dup2 original stderr: %s" (exntos exn)
816 | Ne.Res () ->
817 Ne.clo fd (clofail "dup of stderr");
818 state.errfd <- None;
819 end;
820 | None -> ()
821 end;
822 prerr_string (Buffer.contents state.errmsgs);
823 flush stderr;
824 Buffer.clear state.errmsgs;
828 module G =
829 struct
830 let postRedisplay who =
831 if conf.verbose
832 then prerr_endline ("redisplay for " ^ who);
833 state.redisplay <- true;
835 end;;
837 let getopaque pageno =
838 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
839 with Not_found -> None
842 let putopaque pageno opaque =
843 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
846 let pagetranslatepoint l x y =
847 let dy = y - l.pagedispy in
848 let y = dy + l.pagey in
849 let dx = x - l.pagedispx in
850 let x = dx + l.pagex in
851 (x, y);
854 let onppundermouse g x y d =
855 let rec f = function
856 | l :: rest ->
857 begin match getopaque l.pageno with
858 | Some opaque ->
859 let x0 = l.pagedispx in
860 let x1 = x0 + l.pagevw in
861 let y0 = l.pagedispy in
862 let y1 = y0 + l.pagevh in
863 if y >= y0 && y <= y1 && x >= x0 && x <= x1
864 then
865 let px, py = pagetranslatepoint l x y in
866 match g opaque l px py with
867 | Some res -> res
868 | None -> f rest
869 else f rest
870 | _ ->
871 f rest
873 | [] -> d
875 f state.layout
878 let getunder x y =
879 let g opaque _ px py =
880 match whatsunder opaque px py with
881 | Unone -> None
882 | under -> Some under
884 onppundermouse g x y Unone
887 let unproject x y =
888 let g opaque l x y =
889 match unproject opaque x y with
890 | Some (x, y) -> Some (Some (l.pageno, x, y))
891 | None -> None
893 onppundermouse g x y None;
896 let showtext c s =
897 state.text <- Printf.sprintf "%c%s" c s;
898 G.postRedisplay "showtext";
901 let undertext = function
902 | Unone -> "none"
903 | Ulinkuri s -> s
904 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
905 | Utext s -> "font: " ^ s
906 | Uunexpected s -> "unexpected: " ^ s
907 | Ulaunch s -> "launch: " ^ s
908 | Unamed s -> "named: " ^ s
909 | Uremote (filename, pageno) ->
910 Printf.sprintf "%s: page %d" filename (pageno+1)
913 let updateunder x y =
914 match getunder x y with
915 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
916 | Ulinkuri uri ->
917 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
918 Wsi.setcursor Wsi.CURSOR_INFO
919 | Ulinkgoto (pageno, _) ->
920 if conf.underinfo
921 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
922 Wsi.setcursor Wsi.CURSOR_INFO
923 | Utext s ->
924 if conf.underinfo then showtext 'f' ("ont: " ^ s);
925 Wsi.setcursor Wsi.CURSOR_TEXT
926 | Uunexpected s ->
927 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
928 Wsi.setcursor Wsi.CURSOR_INHERIT
929 | Ulaunch s ->
930 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
931 Wsi.setcursor Wsi.CURSOR_INHERIT
932 | Unamed s ->
933 if conf.underinfo then showtext 'n' ("amed: " ^ s);
934 Wsi.setcursor Wsi.CURSOR_INHERIT
935 | Uremote (filename, pageno) ->
936 if conf.underinfo then showtext 'r'
937 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
938 Wsi.setcursor Wsi.CURSOR_INFO
941 let showlinktype under =
942 if conf.underinfo
943 then
944 match under with
945 | Unone -> ()
946 | under ->
947 let s = undertext under in
948 showtext ' ' s
951 let addchar s c =
952 let b = Buffer.create (String.length s + 1) in
953 Buffer.add_string b s;
954 Buffer.add_char b c;
955 Buffer.contents b;
958 let colorspace_of_string s =
959 match String.lowercase s with
960 | "rgb" -> Rgb
961 | "bgr" -> Bgr
962 | "gray" -> Gray
963 | _ -> failwith "invalid colorspace"
966 let int_of_colorspace = function
967 | Rgb -> 0
968 | Bgr -> 1
969 | Gray -> 2
972 let colorspace_of_int = function
973 | 0 -> Rgb
974 | 1 -> Bgr
975 | 2 -> Gray
976 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
979 let colorspace_to_string = function
980 | Rgb -> "rgb"
981 | Bgr -> "bgr"
982 | Gray -> "gray"
985 let intentry_with_suffix text key =
986 let c =
987 if key >= 32 && key < 127
988 then Char.chr key
989 else '\000'
991 match Char.lowercase c with
992 | '0' .. '9' ->
993 let text = addchar text c in
994 TEcont text
996 | 'k' | 'm' | 'g' ->
997 let text = addchar text c in
998 TEcont text
1000 | _ ->
1001 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1002 TEcont text
1005 let multicolumns_to_string (n, a, b) =
1006 if a = 0 && b = 0
1007 then Printf.sprintf "%d" n
1008 else Printf.sprintf "%d,%d,%d" n a b;
1011 let multicolumns_of_string s =
1013 (int_of_string s, 0, 0)
1014 with _ ->
1015 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1016 if a > 1 || b > 1
1017 then failwith "subtly broken"; (n, a, b)
1021 let readcmd fd =
1022 let s = "xxxx" in
1023 let n = tempfailureretry (Unix.read fd s 0) 4 in
1024 if n != 4 then failwith "incomplete read(len)";
1025 let len = 0
1026 lor (Char.code s.[0] lsl 24)
1027 lor (Char.code s.[1] lsl 16)
1028 lor (Char.code s.[2] lsl 8)
1029 lor (Char.code s.[3] lsl 0)
1031 let s = String.create len in
1032 let n = tempfailureretry (Unix.read fd s 0) len in
1033 if n != len then failwith "incomplete read(data)";
1037 let btod b = if b then 1 else 0;;
1039 let wcmd fmt =
1040 let b = Buffer.create 16 in
1041 Buffer.add_string b "llll";
1042 Printf.kbprintf
1043 (fun b ->
1044 let s = Buffer.contents b in
1045 let n = String.length s in
1046 let len = n - 4 in
1047 (* dolog "wcmd %S" (String.sub s 4 len); *)
1048 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1049 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1050 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1051 s.[3] <- Char.chr (len land 0xff);
1052 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1053 if n' != n then failwith "write failed";
1054 ) b fmt;
1057 let calcips h =
1058 let d = state.winh - h in
1059 max conf.interpagespace ((d + 1) / 2)
1062 let rowyh (c, coverA, coverB) b n =
1063 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1064 then
1065 let _, _, vy, (_, _, h, _) = b.(n) in
1066 (vy, h)
1067 else
1068 let n' = n - coverA in
1069 let d = n' mod c in
1070 let s = n - d in
1071 let e = min state.pagecount (s + c) in
1072 let rec find m miny maxh = if m = e then miny, maxh else
1073 let _, _, y, (_, _, h, _) = b.(m) in
1074 let miny = min miny y in
1075 let maxh = max maxh h in
1076 find (m+1) miny maxh
1077 in find s max_int 0
1080 let calcheight () =
1081 match conf.columns with
1082 | Cmulti ((_, _, _) as cl, b) ->
1083 if Array.length b > 0
1084 then
1085 let y, h = rowyh cl b (Array.length b - 1) in
1086 y + h + (if conf.presentation then calcips h else 0)
1087 else 0
1088 | Csingle b ->
1089 if Array.length b > 0
1090 then
1091 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1092 y + h + (if conf.presentation then calcips h else 0)
1093 else 0
1094 | Csplit (_, b) ->
1095 if Array.length b > 0
1096 then
1097 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1098 y + h
1099 else 0
1102 let getpageyh pageno =
1103 let pageno = bound pageno 0 (state.pagecount-1) in
1104 match conf.columns with
1105 | Csingle b ->
1106 if Array.length b = 0
1107 then 0, 0
1108 else
1109 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1110 let y =
1111 if conf.presentation
1112 then y - calcips h
1113 else y
1115 y, h
1116 | Cmulti (cl, b) ->
1117 if Array.length b = 0
1118 then 0, 0
1119 else
1120 let y, h = rowyh cl b pageno in
1121 let y =
1122 if conf.presentation
1123 then y - calcips h
1124 else y
1126 y, h
1127 | Csplit (c, b) ->
1128 if Array.length b = 0
1129 then 0, 0
1130 else
1131 let n = pageno*c in
1132 let (_, _, y, (_, _, h, _)) = b.(n) in
1133 y, h
1136 let getpagedim pageno =
1137 let rec f ppdim l =
1138 match l with
1139 | (n, _, _, _) as pdim :: rest ->
1140 if n >= pageno
1141 then (if n = pageno then pdim else ppdim)
1142 else f pdim rest
1144 | [] -> ppdim
1146 f (-1, -1, -1, -1) state.pdims
1149 let getpagey pageno = fst (getpageyh pageno);;
1151 let nogeomcmds cmds =
1152 match cmds with
1153 | s, [] -> String.length s = 0
1154 | _ -> false
1157 let page_of_y y =
1158 let ((c, coverA, coverB) as cl), b =
1159 match conf.columns with
1160 | Csingle b -> (1, 0, 0), b
1161 | Cmulti (c, b) -> c, b
1162 | Csplit (_, b) -> (1, 0, 0), b
1164 if Array.length b = 0
1165 then -1
1166 else
1167 let rec bsearch nmin nmax =
1168 if nmin > nmax
1169 then bound nmin 0 (state.pagecount-1)
1170 else
1171 let n = (nmax + nmin) / 2 in
1172 let vy, h = rowyh cl b n in
1173 let y0, y1 =
1174 if conf.presentation
1175 then
1176 let ips = calcips h in
1177 let y0 = vy - ips in
1178 let y1 = vy + h + ips in
1179 y0, y1
1180 else (
1181 if n = 0
1182 then 0, vy + h + conf.interpagespace
1183 else
1184 let y0 = vy - conf.interpagespace in
1185 y0, y0 + h + conf.interpagespace
1188 if y >= y0 && y < y1
1189 then (
1190 if c = 1
1191 then n
1192 else (
1193 if n > coverA
1194 then
1195 if n < state.pagecount - coverB
1196 then ((n-coverA)/c)*c + coverA
1197 else n
1198 else n
1201 else (
1202 if y > y0
1203 then bsearch (n+1) nmax
1204 else bsearch nmin (n-1)
1207 let r = bsearch 0 (state.pagecount-1) in
1211 let layoutN ((columns, coverA, coverB), b) y sh =
1212 let sh = sh - state.hscrollh in
1213 let rec fold accu n =
1214 if n = Array.length b
1215 then accu
1216 else
1217 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1218 if (vy - y) > sh &&
1219 (n = coverA - 1
1220 || n = state.pagecount - coverB
1221 || (n - coverA) mod columns = columns - 1)
1222 then accu
1223 else
1224 let accu =
1225 if vy + h > y
1226 then
1227 let pagey = max 0 (y - vy) in
1228 let pagedispy = if pagey > 0 then 0 else vy - y in
1229 let pagedispx, pagex =
1230 let pdx =
1231 if n = coverA - 1 || n = state.pagecount - coverB
1232 then state.x + (state.winw - state.scrollw - w) / 2
1233 else dx + xoff + state.x
1235 if pdx < 0
1236 then 0, -pdx
1237 else pdx, 0
1239 let pagevw =
1240 let vw = state.winw - state.scrollw - pagedispx in
1241 let pw = w - pagex in
1242 min vw pw
1244 let pagevh = min (h - pagey) (sh - pagedispy) in
1245 if pagevw > 0 && pagevh > 0
1246 then
1247 let e =
1248 { pageno = n
1249 ; pagedimno = pdimno
1250 ; pagew = w
1251 ; pageh = h
1252 ; pagex = pagex
1253 ; pagey = pagey
1254 ; pagevw = pagevw
1255 ; pagevh = pagevh
1256 ; pagedispx = pagedispx
1257 ; pagedispy = pagedispy
1258 ; pagecol = 0
1261 e :: accu
1262 else
1263 accu
1264 else
1265 accu
1267 fold accu (n+1)
1269 List.rev (fold [] (page_of_y y));
1272 let layoutS (columns, b) y sh =
1273 let sh = sh - state.hscrollh in
1274 let rec fold accu n =
1275 if n = Array.length b
1276 then accu
1277 else
1278 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1279 if (vy - y) > sh
1280 then accu
1281 else
1282 let accu =
1283 if vy + pageh > y
1284 then
1285 let x = xoff + state.x in
1286 let pagey = max 0 (y - vy) in
1287 let pagedispy = if pagey > 0 then 0 else vy - y in
1288 let pagedispx, pagex =
1289 if px = 0
1290 then (
1291 if x < 0
1292 then 0, -x
1293 else x, 0
1295 else (
1296 let px = px - x in
1297 if px < 0
1298 then -px, 0
1299 else 0, px
1302 let pagecolw = pagew/columns in
1303 let pagedispx =
1304 if pagecolw < state.winw
1305 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1306 else pagedispx
1308 let pagevw =
1309 let vw = state.winw - pagedispx - state.scrollw in
1310 let pw = pagew - pagex in
1311 min vw pw
1313 let pagevw = min pagevw pagecolw in
1314 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1315 if pagevw > 0 && pagevh > 0
1316 then
1317 let e =
1318 { pageno = n/columns
1319 ; pagedimno = pdimno
1320 ; pagew = pagew
1321 ; pageh = pageh
1322 ; pagex = pagex
1323 ; pagey = pagey
1324 ; pagevw = pagevw
1325 ; pagevh = pagevh
1326 ; pagedispx = pagedispx
1327 ; pagedispy = pagedispy
1328 ; pagecol = n mod columns
1331 e :: accu
1332 else
1333 accu
1334 else
1335 accu
1337 fold accu (n+1)
1339 List.rev (fold [] 0)
1342 let layout y sh =
1343 if nogeomcmds state.geomcmds
1344 then
1345 match conf.columns with
1346 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1347 | Cmulti c -> layoutN c y sh
1348 | Csplit s -> layoutS s y sh
1349 else []
1352 let clamp incr =
1353 let y = state.y + incr in
1354 let y = max 0 y in
1355 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1359 let itertiles l f =
1360 let tilex = l.pagex mod conf.tilew in
1361 let tiley = l.pagey mod conf.tileh in
1363 let col = l.pagex / conf.tilew in
1364 let row = l.pagey / conf.tileh in
1366 let rec rowloop row y0 dispy h =
1367 if h = 0
1368 then ()
1369 else (
1370 let dh = conf.tileh - y0 in
1371 let dh = min h dh in
1372 let rec colloop col x0 dispx w =
1373 if w = 0
1374 then ()
1375 else (
1376 let dw = conf.tilew - x0 in
1377 let dw = min w dw in
1379 f col row dispx dispy x0 y0 dw dh;
1380 colloop (col+1) 0 (dispx+dw) (w-dw)
1383 colloop col tilex l.pagedispx l.pagevw;
1384 rowloop (row+1) 0 (dispy+dh) (h-dh)
1387 if l.pagevw > 0 && l.pagevh > 0
1388 then rowloop row tiley l.pagedispy l.pagevh;
1391 let gettileopaque l col row =
1392 let key =
1393 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1395 try Some (Hashtbl.find state.tilemap key)
1396 with Not_found -> None
1399 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1400 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1401 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1404 let drawtiles l color =
1405 GlDraw.color color;
1406 let f col row x y tilex tiley w h =
1407 match gettileopaque l col row with
1408 | Some (opaque, _, t) ->
1409 let params = x, y, w, h, tilex, tiley in
1410 if conf.invert
1411 then (
1412 Gl.enable `blend;
1413 GlFunc.blend_func `zero `one_minus_src_color;
1415 drawtile params opaque;
1416 if conf.invert
1417 then Gl.disable `blend;
1418 if conf.debug
1419 then (
1420 let s = Printf.sprintf
1421 "%d[%d,%d] %f sec"
1422 l.pageno col row t
1424 let w = measurestr fstate.fontsize s in
1425 GlMisc.push_attrib [`current];
1426 GlDraw.color (0.0, 0.0, 0.0);
1427 GlDraw.rect
1428 (float (x-2), float (y-2))
1429 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1430 GlDraw.color (1.0, 1.0, 1.0);
1431 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1432 GlMisc.pop_attrib ();
1435 | _ ->
1436 let w =
1437 let lw = state.winw - state.scrollw - x in
1438 min lw w
1439 and h =
1440 let lh = state.winh - y in
1441 min lh h
1443 begin match state.texid with
1444 | Some id ->
1445 Gl.enable `texture_2d;
1446 GlTex.bind_texture `texture_2d id;
1447 let x0 = float x
1448 and y0 = float y
1449 and x1 = float (x+w)
1450 and y1 = float (y+h) in
1452 let tw = float w /. 16.0
1453 and th = float h /. 16.0 in
1454 let tx0 = float tilex /. 16.0
1455 and ty0 = float tiley /. 16.0 in
1456 let tx1 = tx0 +. tw
1457 and ty1 = ty0 +. th in
1458 GlDraw.begins `quads;
1459 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1460 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1461 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1462 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1463 GlDraw.ends ();
1465 Gl.disable `texture_2d;
1466 | None ->
1467 GlDraw.color (1.0, 1.0, 1.0);
1468 GlDraw.rect
1469 (float x, float y)
1470 (float (x+w), float (y+h));
1471 end;
1472 if w > 128 && h > fstate.fontsize + 10
1473 then (
1474 GlDraw.color (0.0, 0.0, 0.0);
1475 let c, r =
1476 if conf.verbose
1477 then (col*conf.tilew, row*conf.tileh)
1478 else col, row
1480 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1482 GlDraw.color color;
1484 itertiles l f
1487 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1489 let tilevisible1 l x y =
1490 let ax0 = l.pagex
1491 and ax1 = l.pagex + l.pagevw
1492 and ay0 = l.pagey
1493 and ay1 = l.pagey + l.pagevh in
1495 let bx0 = x
1496 and by0 = y in
1497 let bx1 = min (bx0 + conf.tilew) l.pagew
1498 and by1 = min (by0 + conf.tileh) l.pageh in
1500 let rx0 = max ax0 bx0
1501 and ry0 = max ay0 by0
1502 and rx1 = min ax1 bx1
1503 and ry1 = min ay1 by1 in
1505 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1506 nonemptyintersection
1509 let tilevisible layout n x y =
1510 let rec findpageinlayout m = function
1511 | l :: rest when l.pageno = n ->
1512 tilevisible1 l x y || (
1513 match conf.columns with
1514 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1515 | _ -> false
1517 | _ :: rest -> findpageinlayout 0 rest
1518 | [] -> false
1520 findpageinlayout 0 layout;
1523 let tileready l x y =
1524 tilevisible1 l x y &&
1525 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1528 let tilepage n p layout =
1529 let rec loop = function
1530 | l :: rest ->
1531 if l.pageno = n
1532 then
1533 let f col row _ _ _ _ _ _ =
1534 if state.currently = Idle
1535 then
1536 match gettileopaque l col row with
1537 | Some _ -> ()
1538 | None ->
1539 let x = col*conf.tilew
1540 and y = row*conf.tileh in
1541 let w =
1542 let w = l.pagew - x in
1543 min w conf.tilew
1545 let h =
1546 let h = l.pageh - y in
1547 min h conf.tileh
1549 let pbo =
1550 if conf.usepbo
1551 then getpbo w h conf.colorspace
1552 else "0"
1554 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1555 state.currently <-
1556 Tiling (
1557 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1558 conf.tilew, conf.tileh
1561 itertiles l f;
1562 else
1563 loop rest
1565 | [] -> ()
1567 if nogeomcmds state.geomcmds
1568 then loop layout;
1571 let preloadlayout y =
1572 let y = if y < state.winh then 0 else y - state.winh in
1573 let h = state.winh*3 in
1574 layout y h;
1577 let load pages =
1578 let rec loop pages =
1579 if state.currently != Idle
1580 then ()
1581 else
1582 match pages with
1583 | l :: rest ->
1584 begin match getopaque l.pageno with
1585 | None ->
1586 wcmd "page %d %d" l.pageno l.pagedimno;
1587 state.currently <- Loading (l, state.gen);
1588 | Some opaque ->
1589 tilepage l.pageno opaque pages;
1590 loop rest
1591 end;
1592 | _ -> ()
1594 if nogeomcmds state.geomcmds
1595 then loop pages
1598 let preload pages =
1599 load pages;
1600 if conf.preload && state.currently = Idle
1601 then load (preloadlayout state.y);
1604 let layoutready layout =
1605 let rec fold all ls =
1606 all && match ls with
1607 | l :: rest ->
1608 let seen = ref false in
1609 let allvisible = ref true in
1610 let foo col row _ _ _ _ _ _ =
1611 seen := true;
1612 allvisible := !allvisible &&
1613 begin match gettileopaque l col row with
1614 | Some _ -> true
1615 | None -> false
1618 itertiles l foo;
1619 fold (!seen && !allvisible) rest
1620 | [] -> true
1622 let alltilesvisible = fold true layout in
1623 alltilesvisible;
1626 let gotoy y =
1627 state.wthack <- false;
1628 let y = bound y 0 state.maxy in
1629 let y, layout, proceed =
1630 match conf.maxwait with
1631 | Some time when state.ghyll == noghyll ->
1632 begin match state.throttle with
1633 | None ->
1634 let layout = layout y state.winh in
1635 let ready = layoutready layout in
1636 if not ready
1637 then (
1638 load layout;
1639 state.throttle <- Some (layout, y, now ());
1641 else G.postRedisplay "gotoy showall (None)";
1642 y, layout, ready
1643 | Some (_, _, started) ->
1644 let dt = now () -. started in
1645 if dt > time
1646 then (
1647 state.throttle <- None;
1648 let layout = layout y state.winh in
1649 load layout;
1650 G.postRedisplay "maxwait";
1651 y, layout, true
1653 else -1, [], false
1656 | _ ->
1657 let layout = layout y state.winh in
1658 G.postRedisplay "gotoy ready";
1659 y, layout, true
1661 if proceed
1662 then (
1663 state.y <- y;
1664 state.layout <- layout;
1665 begin match state.mode with
1666 | LinkNav (Ltexact (pageno, linkno)) ->
1667 let rec loop = function
1668 | [] ->
1669 state.mode <- LinkNav (Ltgendir 0)
1670 | l :: _ when l.pageno = pageno ->
1671 begin match getopaque pageno with
1672 | None ->
1673 state.mode <- LinkNav (Ltgendir 0)
1674 | Some opaque ->
1675 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1676 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1677 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1678 then state.mode <- LinkNav (Ltgendir 0)
1680 | _ :: rest -> loop rest
1682 loop layout
1683 | _ -> ()
1684 end;
1685 begin match state.mode with
1686 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1687 if not (pagevisible layout pageno)
1688 then (
1689 match state.layout with
1690 | [] -> ()
1691 | l :: _ ->
1692 state.mode <- Birdseye (
1693 conf, leftx, l.pageno, hooverpageno, anchor
1696 | LinkNav (Ltgendir dir as lt) ->
1697 let linknav =
1698 let rec loop = function
1699 | [] -> lt
1700 | l :: rest ->
1701 match getopaque l.pageno with
1702 | None -> loop rest
1703 | Some opaque ->
1704 let link =
1705 let ld =
1706 if dir = 0
1707 then LDfirstvisible (l.pagex, l.pagey, dir)
1708 else (
1709 if dir > 0 then LDfirst else LDlast
1712 findlink opaque ld
1714 match link with
1715 | Lnotfound -> loop rest
1716 | Lfound n ->
1717 showlinktype (getlink opaque n);
1718 Ltexact (l.pageno, n)
1720 loop state.layout
1722 state.mode <- LinkNav linknav
1723 | _ -> ()
1724 end;
1725 preload layout;
1727 state.ghyll <- noghyll;
1728 if conf.updatecurs
1729 then (
1730 let mx, my = state.mpos in
1731 updateunder mx my;
1735 let conttiling pageno opaque =
1736 tilepage pageno opaque
1737 (if conf.preload then preloadlayout state.y else state.layout)
1740 let gotoy_and_clear_text y =
1741 if not conf.verbose then state.text <- "";
1742 gotoy y;
1745 let getanchor1 l =
1746 let top =
1747 let coloff = l.pagecol * l.pageh in
1748 float (l.pagey + coloff) /. float l.pageh
1750 let dtop =
1751 if l.pagedispy = 0
1752 then
1754 else
1755 if conf.presentation
1756 then float l.pagedispy /. float (calcips l.pageh)
1757 else float l.pagedispy /. float conf.interpagespace
1759 (l.pageno, top, dtop)
1762 let getanchor () =
1763 match state.layout with
1764 | l :: _ -> getanchor1 l
1765 | [] ->
1766 let n = page_of_y state.y in
1767 if n = -1
1768 then state.anchor
1769 else
1770 let y, h = getpageyh n in
1771 let dy = y - state.y in
1772 let dtop =
1773 if conf.presentation
1774 then
1775 let ips = calcips h in
1776 float (dy + ips) /. float ips
1777 else
1778 float dy /. float conf.interpagespace
1780 (n, 0.0, dtop)
1783 let getanchory (n, top, dtop) =
1784 let y, h = getpageyh n in
1785 if conf.presentation
1786 then
1787 let ips = calcips h in
1788 y + truncate (top*.float h -. dtop*.float ips) + ips;
1789 else
1790 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1793 let gotoanchor anchor =
1794 gotoy (getanchory anchor);
1797 let addnav () =
1798 cbput state.hists.nav (getanchor ());
1801 let getnav dir =
1802 let anchor = cbgetc state.hists.nav dir in
1803 getanchory anchor;
1806 let gotoghyll y =
1807 let scroll f n a b =
1808 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1809 let snake f a b =
1810 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1811 if f < a
1812 then s (float f /. float a)
1813 else (
1814 if f > b
1815 then 1.0 -. s ((float (f-b) /. float (n-b)))
1816 else 1.0
1819 snake f a b
1820 and summa f n a b =
1821 (* courtesy:
1822 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1823 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1824 let iv1 = iv f in
1825 let ins = float a *. iv1
1826 and outs = float (n-b) *. iv1 in
1827 let ones = b - a in
1828 ins +. outs +. float ones
1830 let rec set (_N, _A, _B) y sy =
1831 let sum = summa 1.0 _N _A _B in
1832 let dy = float (y - sy) in
1833 state.ghyll <- (
1834 let rec gf n y1 o =
1835 if n >= _N
1836 then state.ghyll <- noghyll
1837 else
1838 let go n =
1839 let s = scroll n _N _A _B in
1840 let y1 = y1 +. ((s *. dy) /. sum) in
1841 gotoy_and_clear_text (truncate y1);
1842 state.ghyll <- gf (n+1) y1;
1844 match o with
1845 | None -> go n
1846 | Some y' -> set (_N/2, 1, 1) y' state.y
1848 gf 0 (float state.y)
1851 match conf.ghyllscroll with
1852 | None ->
1853 gotoy_and_clear_text y
1854 | Some nab ->
1855 if state.ghyll == noghyll
1856 then set nab y state.y
1857 else state.ghyll (Some y)
1860 let gotopage n top =
1861 let y, h = getpageyh n in
1862 let y = y + (truncate (top *. float h)) in
1863 gotoghyll y
1866 let gotopage1 n top =
1867 let y = getpagey n in
1868 let y = y + top in
1869 gotoghyll y
1872 let invalidate s f =
1873 state.layout <- [];
1874 state.pdims <- [];
1875 state.rects <- [];
1876 state.rects1 <- [];
1877 match state.geomcmds with
1878 | ps, [] when String.length ps = 0 ->
1879 f ();
1880 state.geomcmds <- s, [];
1882 | ps, [] ->
1883 state.geomcmds <- ps, [s, f];
1885 | ps, (s', _) :: rest when s' = s ->
1886 state.geomcmds <- ps, ((s, f) :: rest);
1888 | ps, cmds ->
1889 state.geomcmds <- ps, ((s, f) :: cmds);
1892 let flushpages () =
1893 Hashtbl.iter (fun _ opaque ->
1894 wcmd "freepage %s" opaque;
1895 ) state.pagemap;
1896 Hashtbl.clear state.pagemap;
1899 let flushtiles () =
1900 if not (Queue.is_empty state.tilelru)
1901 then (
1902 Queue.iter (fun (k, p, s) ->
1903 wcmd "freetile %s" p;
1904 state.memused <- state.memused - s;
1905 Hashtbl.remove state.tilemap k;
1906 ) state.tilelru;
1907 state.uioh#infochanged Memused;
1908 Queue.clear state.tilelru;
1910 load state.layout;
1913 let opendoc path password =
1914 state.path <- path;
1915 state.password <- password;
1916 state.gen <- state.gen + 1;
1917 state.docinfo <- [];
1919 flushpages ();
1920 flushtiles ();
1921 setaalevel conf.aalevel;
1922 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1923 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1924 invalidate "reqlayout"
1925 (fun () ->
1926 wcmd "reqlayout %d %d %s\000"
1927 conf.angle (btod conf.proportional) state.nameddest;
1931 let reload () =
1932 state.anchor <- getanchor ();
1933 state.wthack <- !wtmode;
1934 opendoc state.path state.password;
1937 let scalecolor c =
1938 let c = c *. conf.colorscale in
1939 (c, c, c);
1942 let scalecolor2 (r, g, b) =
1943 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1946 let docolumns = function
1947 | Csingle _ ->
1948 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1949 let rec loop pageno pdimno pdim y ph pdims =
1950 if pageno = state.pagecount
1951 then ()
1952 else
1953 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1954 match pdims with
1955 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1956 pdimno+1, pdim, rest
1957 | _ ->
1958 pdimno, pdim, pdims
1960 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
1961 let y = y +
1962 (if conf.presentation
1963 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1964 else (if pageno = 0 then 0 else conf.interpagespace)
1967 a.(pageno) <- (pdimno, x, y, pdim);
1968 loop (pageno+1) pdimno pdim (y + h) h pdims
1970 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1971 conf.columns <- Csingle a;
1973 | Cmulti ((columns, coverA, coverB), _) ->
1974 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1975 let rec loop pageno pdimno pdim x y rowh pdims =
1976 let rec fixrow m = if m = pageno then () else
1977 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1978 if h < rowh
1979 then (
1980 let y = y + (rowh - h) / 2 in
1981 a.(m) <- (pdimno, x, y, pdim);
1983 fixrow (m+1)
1985 if pageno = state.pagecount
1986 then fixrow (((pageno - 1) / columns) * columns)
1987 else
1988 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1989 match pdims with
1990 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1991 pdimno+1, pdim, rest
1992 | _ ->
1993 pdimno, pdim, pdims
1995 let x, y, rowh' =
1996 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1997 then (
1998 let x = (state.winw - state.scrollw - w) / 2 in
1999 let ips =
2000 if conf.presentation then calcips h else conf.interpagespace in
2001 x, y + ips + rowh, h
2003 else (
2004 if (pageno - coverA) mod columns = 0
2005 then (
2006 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2007 let y =
2008 if conf.presentation
2009 then
2010 let ips = calcips h in
2011 y + (if pageno = 0 then 0 else calcips rowh + ips)
2012 else
2013 y + (if pageno = 0 then 0 else conf.interpagespace)
2015 x, y + rowh, h
2017 else x, y, max rowh h
2020 let y =
2021 if pageno > 1 && (pageno - coverA) mod columns = 0
2022 then (
2023 let y =
2024 if pageno = columns && conf.presentation
2025 then (
2026 let ips = calcips rowh in
2027 for i = 0 to pred columns
2029 let (pdimno, x, y, pdim) = a.(i) in
2030 a.(i) <- (pdimno, x, y+ips, pdim)
2031 done;
2032 y+ips;
2034 else y
2036 fixrow (pageno - columns);
2039 else y
2041 a.(pageno) <- (pdimno, x, y, pdim);
2042 let x = x + w + xoff*2 + conf.interpagespace in
2043 loop (pageno+1) pdimno pdim x y rowh' pdims
2045 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2046 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2048 | Csplit (c, _) ->
2049 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2050 let rec loop pageno pdimno pdim y pdims =
2051 if pageno = state.pagecount
2052 then ()
2053 else
2054 let pdimno, ((_, w, h, _) as pdim), pdims =
2055 match pdims with
2056 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2057 pdimno+1, pdim, rest
2058 | _ ->
2059 pdimno, pdim, pdims
2061 let cw = w / c in
2062 let rec loop1 n x y =
2063 if n = c then y else (
2064 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2065 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2068 let y = loop1 0 0 y in
2069 loop (pageno+1) pdimno pdim y pdims
2071 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2072 conf.columns <- Csplit (c, a);
2075 let represent () =
2076 docolumns conf.columns;
2077 state.maxy <- calcheight ();
2078 state.hscrollh <-
2079 if state.x = 0 && state.w <= state.winw - state.scrollw
2080 then 0
2081 else state.scrollw
2083 if state.reprf == noreprf
2084 then (
2085 match state.mode with
2086 | Birdseye (_, _, pageno, _, _) ->
2087 let y, h = getpageyh pageno in
2088 let top = (state.winh - h) / 2 in
2089 gotoy (max 0 (y - top))
2090 | _ -> gotoanchor state.anchor
2092 else (
2093 state.reprf ();
2094 state.reprf <- noreprf;
2098 let reshape w h =
2099 state.wthack <- false;
2100 GlDraw.viewport 0 0 w h;
2101 let firsttime = state.geomcmds == firstgeomcmds in
2102 if not firsttime && nogeomcmds state.geomcmds
2103 then state.anchor <- getanchor ();
2105 state.winw <- w;
2106 let w = truncate (float w *. conf.zoom) - state.scrollw in
2107 let w = max w 2 in
2108 state.winh <- h;
2109 setfontsize fstate.fontsize;
2110 GlMat.mode `modelview;
2111 GlMat.load_identity ();
2113 GlMat.mode `projection;
2114 GlMat.load_identity ();
2115 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2116 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2117 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2119 let relx =
2120 if conf.zoom <= 1.0
2121 then 0.0
2122 else float state.x /. float state.w
2124 invalidate "geometry"
2125 (fun () ->
2126 state.w <- w;
2127 if not firsttime
2128 then state.x <- truncate (relx *. float w);
2129 let w =
2130 match conf.columns with
2131 | Csingle _ -> w
2132 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2133 | Csplit (c, _) -> w * c
2135 wcmd "geometry %d %d" w h);
2138 let enttext () =
2139 let len = String.length state.text in
2140 let drawstring s =
2141 let hscrollh =
2142 match state.mode with
2143 | Textentry _
2144 | View ->
2145 let h, _, _ = state.uioh#scrollpw in
2147 | _ -> 0
2149 let rect x w =
2150 GlDraw.rect
2151 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2152 (x+.w, float (state.winh - hscrollh))
2155 let w = float (state.winw - state.scrollw - 1) in
2156 if state.progress >= 0.0 && state.progress < 1.0
2157 then (
2158 GlDraw.color (0.3, 0.3, 0.3);
2159 let w1 = w *. state.progress in
2160 rect 0.0 w1;
2161 GlDraw.color (0.0, 0.0, 0.0);
2162 rect w1 (w-.w1)
2164 else (
2165 GlDraw.color (0.0, 0.0, 0.0);
2166 rect 0.0 w;
2169 GlDraw.color (1.0, 1.0, 1.0);
2170 drawstring fstate.fontsize
2171 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2173 let s =
2174 match state.mode with
2175 | Textentry ((prefix, text, _, _, _, _), _) ->
2176 let s =
2177 if len > 0
2178 then
2179 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2180 else
2181 Printf.sprintf "%s%s_" prefix text
2185 | _ -> state.text
2187 let s =
2188 if state.newerrmsgs
2189 then (
2190 if not (istextentry state.mode)
2191 then
2192 let s1 = "(press 'e' to review error messasges)" in
2193 if String.length s > 0 then s ^ " " ^ s1 else s1
2194 else s
2196 else s
2198 if String.length s > 0
2199 then drawstring s
2202 let gctiles () =
2203 let len = Queue.length state.tilelru in
2204 let layout = lazy (
2205 match state.throttle with
2206 | None ->
2207 if conf.preload
2208 then preloadlayout state.y
2209 else state.layout
2210 | Some (layout, _, _) ->
2211 layout
2212 ) in
2213 let rec loop qpos =
2214 if state.memused <= conf.memlimit
2215 then ()
2216 else (
2217 if qpos < len
2218 then
2219 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2220 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2221 let (_, pw, ph, _) = getpagedim n in
2223 gen = state.gen
2224 && colorspace = conf.colorspace
2225 && angle = conf.angle
2226 && pagew = pw
2227 && pageh = ph
2228 && (
2229 let x = col*conf.tilew
2230 and y = row*conf.tileh in
2231 tilevisible (Lazy.force_val layout) n x y
2233 then Queue.push lruitem state.tilelru
2234 else (
2235 freepbo p;
2236 wcmd "freetile %s" p;
2237 state.memused <- state.memused - s;
2238 state.uioh#infochanged Memused;
2239 Hashtbl.remove state.tilemap k;
2241 loop (qpos+1)
2244 loop 0
2247 let logcurrently = function
2248 | Idle -> dolog "Idle"
2249 | Loading (l, gen) ->
2250 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2251 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2252 dolog
2253 "Tiling %d[%d,%d] page=%s cs=%s angle"
2254 l.pageno col row pageopaque
2255 (colorspace_to_string colorspace)
2257 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2258 angle gen conf.angle state.gen
2259 tilew tileh
2260 conf.tilew conf.tileh
2262 | Outlining _ ->
2263 dolog "outlining"
2266 let splitatspace =
2267 let r = Str.regexp " " in
2268 fun s -> Str.bounded_split r s 2;
2271 let onpagerect pageno f =
2272 let b =
2273 match conf.columns with
2274 | Cmulti (_, b) -> b
2275 | Csingle b -> b
2276 | Csplit (_, b) -> b
2278 if pageno >= 0 && pageno < Array.length b
2279 then
2280 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2281 let r = getpdimrect pdimno in
2282 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2285 let gotopagexy pageno x y =
2286 onpagerect pageno (fun w h ->
2287 let top = y /. h in
2288 let _,w1,_,leftx = getpagedim pageno in
2289 let wh = state.winh - state.hscrollh in
2290 let sw = float w1 /. w in
2291 let x = sw *. x in
2292 let x = leftx + state.x + truncate x in
2293 let sx =
2294 if x < 0 || x >= state.winw - state.scrollw
2295 then state.x - x
2296 else state.x
2298 let py, h = getpageyh pageno in
2299 let y' = py + truncate (top *. float h) in
2300 let dy = y' - state.y in
2301 let sy =
2302 if x != state.x || not (dy > 0 && dy < wh)
2303 then (
2304 if conf.presentation
2305 then
2306 if abs (py - y') > wh
2307 then y'
2308 else py
2309 else y';
2311 else state.y
2313 if state.x != sx || state.y != sy
2314 then (
2315 let x, y =
2316 if !wtmode
2317 then (
2318 let ww = state.winw - state.scrollw in
2319 let qx = sx / ww
2320 and qy = sy / wh in
2321 let x = qx * ww
2322 and y = qy * wh in
2323 let x = if -x + ww > w1 then -(w1-ww) else x
2324 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2325 let y =
2326 if conf.presentation
2327 then
2328 if abs (py - y') > wh
2329 then y'
2330 else py
2331 else y';
2333 (x, y)
2335 else (sx, sy)
2337 state.x <- x;
2338 state.hscrollh <-
2339 if x = 0 && state.w <= state.winw - state.scrollw
2340 then 0
2341 else state.scrollw
2343 gotoy_and_clear_text y;
2345 else gotoy_and_clear_text state.y;
2346 state.wthack <- !wtmode && not (layoutready state.layout);
2350 let act cmds =
2351 (* dolog "%S" cmds; *)
2352 let cl = splitatspace cmds in
2353 let scan s fmt f =
2354 try Scanf.sscanf s fmt f
2355 with exn ->
2356 dolog "error processing '%S': %s" cmds (exntos exn);
2357 exit 1
2359 match cl with
2360 | "clear" :: [] ->
2361 state.uioh#infochanged Pdim;
2362 state.pdims <- [];
2364 | "clearrects" :: [] ->
2365 state.rects <- state.rects1;
2366 G.postRedisplay "clearrects";
2368 | "continue" :: args :: [] ->
2369 let n = scan args "%u" (fun n -> n) in
2370 state.pagecount <- n;
2371 begin match state.currently with
2372 | Outlining l ->
2373 state.currently <- Idle;
2374 state.outlines <- Array.of_list (List.rev l)
2375 | _ -> ()
2376 end;
2378 let cur, cmds = state.geomcmds in
2379 if String.length cur = 0
2380 then failwith "umpossible";
2382 begin match List.rev cmds with
2383 | [] ->
2384 state.geomcmds <- "", [];
2385 represent ();
2386 | (s, f) :: rest ->
2387 f ();
2388 state.geomcmds <- s, List.rev rest;
2389 end;
2390 if conf.maxwait = None
2391 then G.postRedisplay "continue";
2393 | "title" :: args :: [] ->
2394 Wsi.settitle args
2396 | "msg" :: args :: [] ->
2397 showtext ' ' args
2399 | "vmsg" :: args :: [] ->
2400 if conf.verbose
2401 then showtext ' ' args
2403 | "emsg" :: args :: [] ->
2404 Buffer.add_string state.errmsgs args;
2405 state.newerrmsgs <- true;
2406 G.postRedisplay "error message"
2408 | "progress" :: args :: [] ->
2409 let progress, text =
2410 scan args "%f %n"
2411 (fun f pos ->
2412 f, String.sub args pos (String.length args - pos))
2414 state.text <- text;
2415 state.progress <- progress;
2416 G.postRedisplay "progress"
2418 | "firstmatch" :: args :: [] ->
2419 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2420 scan args "%u %d %f %f %f %f %f %f %f %f"
2421 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2422 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2424 let y = (getpagey pageno) + truncate y0 in
2425 addnav ();
2426 gotoy y;
2427 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2429 | "match" :: args :: [] ->
2430 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2431 scan args "%u %d %f %f %f %f %f %f %f %f"
2432 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2433 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2435 state.rects1 <-
2436 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2438 | "page" :: args :: [] ->
2439 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2440 begin match state.currently with
2441 | Loading (l, gen) ->
2442 vlog "page %d took %f sec" l.pageno t;
2443 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2444 begin match state.throttle with
2445 | None ->
2446 let preloadedpages =
2447 if conf.preload
2448 then preloadlayout state.y
2449 else state.layout
2451 let evict () =
2452 let module IntSet =
2453 Set.Make (struct type t = int let compare = (-) end) in
2454 let set =
2455 List.fold_left (fun s l -> IntSet.add l.pageno s)
2456 IntSet.empty preloadedpages
2458 let evictedpages =
2459 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2460 if not (IntSet.mem pageno set)
2461 then (
2462 wcmd "freepage %s" opaque;
2463 key :: accu
2465 else accu
2466 ) state.pagemap []
2468 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2470 evict ();
2471 state.currently <- Idle;
2472 if gen = state.gen
2473 then (
2474 tilepage l.pageno pageopaque state.layout;
2475 load state.layout;
2476 load preloadedpages;
2477 if pagevisible state.layout l.pageno
2478 && layoutready state.layout
2479 then G.postRedisplay "page";
2482 | Some (layout, _, _) ->
2483 state.currently <- Idle;
2484 tilepage l.pageno pageopaque layout;
2485 load state.layout
2486 end;
2488 | _ ->
2489 dolog "Inconsistent loading state";
2490 logcurrently state.currently;
2491 exit 1
2494 | "tile" :: args :: [] ->
2495 let (x, y, opaque, size, t) =
2496 scan args "%u %u %s %u %f"
2497 (fun x y p size t -> (x, y, p, size, t))
2499 begin match state.currently with
2500 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2501 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2503 unmappbo opaque;
2504 if tilew != conf.tilew || tileh != conf.tileh
2505 then (
2506 wcmd "freetile %s" opaque;
2507 state.currently <- Idle;
2508 load state.layout;
2510 else (
2511 puttileopaque l col row gen cs angle opaque size t;
2512 state.memused <- state.memused + size;
2513 state.uioh#infochanged Memused;
2514 gctiles ();
2515 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2516 opaque, size) state.tilelru;
2518 let layout =
2519 match state.throttle with
2520 | None -> state.layout
2521 | Some (layout, _, _) -> layout
2524 state.currently <- Idle;
2525 if gen = state.gen
2526 && conf.colorspace = cs
2527 && conf.angle = angle
2528 && tilevisible layout l.pageno x y
2529 then conttiling l.pageno pageopaque;
2531 begin match state.throttle with
2532 | None ->
2533 if state.wthack
2534 then state.wthack <- not (layoutready state.layout);
2535 preload state.layout;
2536 if gen = state.gen
2537 && conf.colorspace = cs
2538 && conf.angle = angle
2539 && tilevisible state.layout l.pageno x y
2540 then G.postRedisplay "tile nothrottle";
2542 | Some (layout, y, _) ->
2543 let ready = layoutready layout in
2544 if ready
2545 then (
2546 state.wthack <- false;
2547 state.y <- y;
2548 state.layout <- layout;
2549 state.throttle <- None;
2550 G.postRedisplay "throttle";
2552 else load layout;
2553 end;
2556 | _ ->
2557 dolog "Inconsistent tiling state";
2558 logcurrently state.currently;
2559 exit 1
2562 | "pdim" :: args :: [] ->
2563 let pdim =
2564 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2566 state.uioh#infochanged Pdim;
2567 state.pdims <- pdim :: state.pdims
2569 | "o" :: args :: [] ->
2570 let (l, n, t, h, pos) =
2571 scan args "%u %u %d %u %n"
2572 (fun l n t h pos -> l, n, t, h, pos)
2574 let s = String.sub args pos (String.length args - pos) in
2575 let outline = (s, l, (n, float t /. float h, 0.0)) in
2576 begin match state.currently with
2577 | Outlining outlines ->
2578 state.currently <- Outlining (outline :: outlines)
2579 | Idle ->
2580 state.currently <- Outlining [outline]
2581 | currently ->
2582 dolog "invalid outlining state";
2583 logcurrently currently
2586 | "a" :: args :: [] ->
2587 let (n, l, t) =
2588 scan args "%u %d %d" (fun n l t -> n, l, t)
2590 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2592 | "info" :: args :: [] ->
2593 state.docinfo <- (1, args) :: state.docinfo
2595 | "infoend" :: [] ->
2596 state.uioh#infochanged Docinfo;
2597 state.docinfo <- List.rev state.docinfo
2599 | _ ->
2600 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2603 let onhist cb =
2604 let rc = cb.rc in
2605 let action = function
2606 | HCprev -> cbget cb ~-1
2607 | HCnext -> cbget cb 1
2608 | HCfirst -> cbget cb ~-(cb.rc)
2609 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2610 and cancel () = cb.rc <- rc
2611 in (action, cancel)
2614 let search pattern forward =
2615 if String.length pattern > 0
2616 then
2617 let pn, py =
2618 match state.layout with
2619 | [] -> 0, 0
2620 | l :: _ ->
2621 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2623 wcmd "search %d %d %d %d,%s\000"
2624 (btod conf.icase) pn py (btod forward) pattern;
2627 let intentry text key =
2628 let c =
2629 if key >= 32 && key < 127
2630 then Char.chr key
2631 else '\000'
2633 match c with
2634 | '0' .. '9' ->
2635 let text = addchar text c in
2636 TEcont text
2638 | _ ->
2639 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2640 TEcont text
2643 let linknentry text key =
2644 let c =
2645 if key >= 32 && key < 127
2646 then Char.chr key
2647 else '\000'
2649 match c with
2650 | 'a' .. 'z' ->
2651 let text = addchar text c in
2652 TEcont text
2654 | _ ->
2655 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2656 TEcont text
2659 let linkndone f s =
2660 if String.length s > 0
2661 then (
2662 let n =
2663 let l = String.length s in
2664 let rec loop pos n = if pos = l then n else
2665 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2666 loop (pos+1) (n*26 + m)
2667 in loop 0 0
2669 let rec loop n = function
2670 | [] -> ()
2671 | l :: rest ->
2672 match getopaque l.pageno with
2673 | None -> loop n rest
2674 | Some opaque ->
2675 let m = getlinkcount opaque in
2676 if n < m
2677 then (
2678 let under = getlink opaque n in
2679 f under
2681 else loop (n-m) rest
2683 loop n state.layout;
2687 let textentry text key =
2688 if key land 0xff00 = 0xff00
2689 then TEcont text
2690 else TEcont (text ^ toutf8 key)
2693 let reqlayout angle proportional =
2694 match state.throttle with
2695 | None ->
2696 if nogeomcmds state.geomcmds
2697 then state.anchor <- getanchor ();
2698 conf.angle <- angle mod 360;
2699 if conf.angle != 0
2700 then (
2701 match state.mode with
2702 | LinkNav _ -> state.mode <- View
2703 | _ -> ()
2705 conf.proportional <- proportional;
2706 invalidate "reqlayout"
2707 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2708 | _ -> ()
2711 let settrim trimmargins trimfuzz =
2712 if nogeomcmds state.geomcmds
2713 then state.anchor <- getanchor ();
2714 conf.trimmargins <- trimmargins;
2715 conf.trimfuzz <- trimfuzz;
2716 let x0, y0, x1, y1 = trimfuzz in
2717 invalidate "settrim"
2718 (fun () ->
2719 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2720 flushpages ();
2723 let setzoom zoom =
2724 match state.throttle with
2725 | None ->
2726 let zoom = max 0.01 zoom in
2727 if zoom <> conf.zoom
2728 then (
2729 state.prevzoom <- conf.zoom;
2730 conf.zoom <- zoom;
2731 reshape state.winw state.winh;
2732 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2735 | Some (layout, y, started) ->
2736 let time =
2737 match conf.maxwait with
2738 | None -> 0.0
2739 | Some t -> t
2741 let dt = now () -. started in
2742 if dt > time
2743 then (
2744 state.y <- y;
2745 load layout;
2749 let setcolumns mode columns coverA coverB =
2750 state.prevcolumns <- Some (conf.columns, conf.zoom);
2751 if columns < 0
2752 then (
2753 if isbirdseye mode
2754 then showtext '!' "split mode doesn't work in bird's eye"
2755 else (
2756 conf.columns <- Csplit (-columns, [||]);
2757 state.x <- 0;
2758 conf.zoom <- 1.0;
2761 else (
2762 if columns < 2
2763 then (
2764 conf.columns <- Csingle [||];
2765 state.x <- 0;
2766 setzoom 1.0;
2768 else (
2769 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2770 conf.zoom <- 1.0;
2773 reshape state.winw state.winh;
2776 let enterbirdseye () =
2777 let zoom = float conf.thumbw /. float state.winw in
2778 let birdseyepageno =
2779 let cy = state.winh / 2 in
2780 let fold = function
2781 | [] -> 0
2782 | l :: rest ->
2783 let rec fold best = function
2784 | [] -> best.pageno
2785 | l :: rest ->
2786 let d = cy - (l.pagedispy + l.pagevh/2)
2787 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2788 if abs d < abs dbest
2789 then fold l rest
2790 else best.pageno
2791 in fold l rest
2793 fold state.layout
2795 state.mode <- Birdseye (
2796 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2798 conf.zoom <- zoom;
2799 conf.presentation <- false;
2800 conf.interpagespace <- 10;
2801 conf.hlinks <- false;
2802 state.x <- 0;
2803 state.mstate <- Mnone;
2804 conf.maxwait <- None;
2805 conf.columns <- (
2806 match conf.beyecolumns with
2807 | Some c ->
2808 conf.zoom <- 1.0;
2809 Cmulti ((c, 0, 0), [||])
2810 | None -> Csingle [||]
2812 Wsi.setcursor Wsi.CURSOR_INHERIT;
2813 if conf.verbose
2814 then
2815 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2816 (100.0*.zoom)
2817 else
2818 state.text <- ""
2820 reshape state.winw state.winh;
2823 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2824 state.mode <- View;
2825 conf.zoom <- c.zoom;
2826 conf.presentation <- c.presentation;
2827 conf.interpagespace <- c.interpagespace;
2828 conf.maxwait <- c.maxwait;
2829 conf.hlinks <- c.hlinks;
2830 conf.beyecolumns <- (
2831 match conf.columns with
2832 | Cmulti ((c, _, _), _) -> Some c
2833 | Csingle _ -> None
2834 | Csplit _ -> failwith "leaving bird's eye split mode"
2836 conf.columns <- (
2837 match c.columns with
2838 | Cmulti (c, _) -> Cmulti (c, [||])
2839 | Csingle _ -> Csingle [||]
2840 | Csplit (c, _) -> Csplit (c, [||])
2842 state.x <- leftx;
2843 if conf.verbose
2844 then
2845 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2846 (100.0*.conf.zoom)
2848 reshape state.winw state.winh;
2849 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2852 let togglebirdseye () =
2853 match state.mode with
2854 | Birdseye vals -> leavebirdseye vals true
2855 | View -> enterbirdseye ()
2856 | _ -> ()
2859 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2860 let pageno = max 0 (pageno - incr) in
2861 let rec loop = function
2862 | [] -> gotopage1 pageno 0
2863 | l :: _ when l.pageno = pageno ->
2864 if l.pagedispy >= 0 && l.pagey = 0
2865 then G.postRedisplay "upbirdseye"
2866 else gotopage1 pageno 0
2867 | _ :: rest -> loop rest
2869 loop state.layout;
2870 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2873 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2874 let pageno = min (state.pagecount - 1) (pageno + incr) in
2875 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2876 let rec loop = function
2877 | [] ->
2878 let y, h = getpageyh pageno in
2879 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2880 gotoy (clamp dy)
2881 | l :: _ when l.pageno = pageno ->
2882 if l.pagevh != l.pageh
2883 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2884 else G.postRedisplay "downbirdseye"
2885 | _ :: rest -> loop rest
2887 loop state.layout
2890 let optentry mode _ key =
2891 let btos b = if b then "on" else "off" in
2892 if key >= 32 && key < 127
2893 then
2894 let c = Char.chr key in
2895 match c with
2896 | 's' ->
2897 let ondone s =
2898 try conf.scrollstep <- int_of_string s with exc ->
2899 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2901 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2903 | 'A' ->
2904 let ondone s =
2906 conf.autoscrollstep <- int_of_string s;
2907 if state.autoscroll <> None
2908 then state.autoscroll <- Some conf.autoscrollstep
2909 with exc ->
2910 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2912 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2914 | 'C' ->
2915 let ondone s =
2917 let n, a, b = multicolumns_of_string s in
2918 setcolumns mode n a b;
2919 with exc ->
2920 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2922 TEswitch ("columns: ", "", None, textentry, ondone, true)
2924 | 'Z' ->
2925 let ondone s =
2927 let zoom = float (int_of_string s) /. 100.0 in
2928 setzoom zoom
2929 with exc ->
2930 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2932 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2934 | 't' ->
2935 let ondone s =
2937 conf.thumbw <- bound (int_of_string s) 2 4096;
2938 state.text <-
2939 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2940 begin match mode with
2941 | Birdseye beye ->
2942 leavebirdseye beye false;
2943 enterbirdseye ();
2944 | _ -> ();
2946 with exc ->
2947 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2949 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2951 | 'R' ->
2952 let ondone s =
2953 match try
2954 Some (int_of_string s)
2955 with exc ->
2956 state.text <- Printf.sprintf "bad integer `%s': %s"
2957 s (exntos exc);
2958 None
2959 with
2960 | Some angle -> reqlayout angle conf.proportional
2961 | None -> ()
2963 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2965 | 'i' ->
2966 conf.icase <- not conf.icase;
2967 TEdone ("case insensitive search " ^ (btos conf.icase))
2969 | 'p' ->
2970 conf.preload <- not conf.preload;
2971 gotoy state.y;
2972 TEdone ("preload " ^ (btos conf.preload))
2974 | 'v' ->
2975 conf.verbose <- not conf.verbose;
2976 TEdone ("verbose " ^ (btos conf.verbose))
2978 | 'd' ->
2979 conf.debug <- not conf.debug;
2980 TEdone ("debug " ^ (btos conf.debug))
2982 | 'h' ->
2983 conf.maxhfit <- not conf.maxhfit;
2984 state.maxy <- calcheight ();
2985 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2987 | 'c' ->
2988 conf.crophack <- not conf.crophack;
2989 TEdone ("crophack " ^ btos conf.crophack)
2991 | 'a' ->
2992 let s =
2993 match conf.maxwait with
2994 | None ->
2995 conf.maxwait <- Some infinity;
2996 "always wait for page to complete"
2997 | Some _ ->
2998 conf.maxwait <- None;
2999 "show placeholder if page is not ready"
3001 TEdone s
3003 | 'f' ->
3004 conf.underinfo <- not conf.underinfo;
3005 TEdone ("underinfo " ^ btos conf.underinfo)
3007 | 'P' ->
3008 conf.savebmarks <- not conf.savebmarks;
3009 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3011 | 'S' ->
3012 let ondone s =
3014 let pageno, py =
3015 match state.layout with
3016 | [] -> 0, 0
3017 | l :: _ ->
3018 l.pageno, l.pagey
3020 conf.interpagespace <- int_of_string s;
3021 docolumns conf.columns;
3022 state.maxy <- calcheight ();
3023 let y = getpagey pageno in
3024 gotoy (y + py)
3025 with exc ->
3026 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3028 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3030 | 'l' ->
3031 reqlayout conf.angle (not conf.proportional);
3032 TEdone ("proportional display " ^ btos conf.proportional)
3034 | 'T' ->
3035 settrim (not conf.trimmargins) conf.trimfuzz;
3036 TEdone ("trim margins " ^ btos conf.trimmargins)
3038 | 'I' ->
3039 conf.invert <- not conf.invert;
3040 TEdone ("invert colors " ^ btos conf.invert)
3042 | 'x' ->
3043 let ondone s =
3044 cbput state.hists.sel s;
3045 conf.selcmd <- s;
3047 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3048 textentry, ondone, true)
3050 | _ ->
3051 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3052 TEstop
3053 else
3054 TEcont state.text
3057 class type lvsource = object
3058 method getitemcount : int
3059 method getitem : int -> (string * int)
3060 method hasaction : int -> bool
3061 method exit :
3062 uioh:uioh ->
3063 cancel:bool ->
3064 active:int ->
3065 first:int ->
3066 pan:int ->
3067 qsearch:string ->
3068 uioh option
3069 method getactive : int
3070 method getfirst : int
3071 method getqsearch : string
3072 method setqsearch : string -> unit
3073 method getpan : int
3074 end;;
3076 class virtual lvsourcebase = object
3077 val mutable m_active = 0
3078 val mutable m_first = 0
3079 val mutable m_qsearch = ""
3080 val mutable m_pan = 0
3081 method getactive = m_active
3082 method getfirst = m_first
3083 method getqsearch = m_qsearch
3084 method getpan = m_pan
3085 method setqsearch s = m_qsearch <- s
3086 end;;
3088 let withoutlastutf8 s =
3089 let len = String.length s in
3090 if len = 0
3091 then s
3092 else
3093 let rec find pos =
3094 if pos = 0
3095 then pos
3096 else
3097 let b = Char.code s.[pos] in
3098 if b land 0b11000000 = 0b11000000
3099 then pos
3100 else find (pos-1)
3102 let first =
3103 if Char.code s.[len-1] land 0x80 = 0
3104 then len-1
3105 else find (len-1)
3107 String.sub s 0 first;
3110 let textentrykeyboard
3111 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3112 let key =
3113 if key >= 0xffb0 && key <= 0xffb9
3114 then key - 0xffb0 + 48 else key
3116 let enttext te =
3117 state.mode <- Textentry (te, onleave);
3118 state.text <- "";
3119 enttext ();
3120 G.postRedisplay "textentrykeyboard enttext";
3122 let histaction cmd =
3123 match opthist with
3124 | None -> ()
3125 | Some (action, _) ->
3126 state.mode <- Textentry (
3127 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3129 G.postRedisplay "textentry histaction"
3131 match key with
3132 | 0xff08 -> (* backspace *)
3133 let s = withoutlastutf8 text in
3134 let len = String.length s in
3135 if cancelonempty && len = 0
3136 then (
3137 onleave Cancel;
3138 G.postRedisplay "textentrykeyboard after cancel";
3140 else (
3141 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3144 | 0xff0d | 0xff8d -> (* (kp) enter *)
3145 ondone text;
3146 onleave Confirm;
3147 G.postRedisplay "textentrykeyboard after confirm"
3149 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3150 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3151 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3152 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3154 | 0xff1b -> (* escape*)
3155 if String.length text = 0
3156 then (
3157 begin match opthist with
3158 | None -> ()
3159 | Some (_, onhistcancel) -> onhistcancel ()
3160 end;
3161 onleave Cancel;
3162 state.text <- "";
3163 G.postRedisplay "textentrykeyboard after cancel2"
3165 else (
3166 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3169 | 0xff9f | 0xffff -> () (* delete *)
3171 | _ when key != 0
3172 && key land 0xff00 != 0xff00 (* keyboard *)
3173 && key land 0xfe00 != 0xfe00 (* xkb *)
3174 && key land 0xfd00 != 0xfd00 (* 3270 *)
3176 begin match onkey text key with
3177 | TEdone text ->
3178 ondone text;
3179 onleave Confirm;
3180 G.postRedisplay "textentrykeyboard after confirm2";
3182 | TEcont text ->
3183 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3185 | TEstop ->
3186 onleave Cancel;
3187 G.postRedisplay "textentrykeyboard after cancel3"
3189 | TEswitch te ->
3190 state.mode <- Textentry (te, onleave);
3191 G.postRedisplay "textentrykeyboard switch";
3192 end;
3194 | _ ->
3195 vlog "unhandled key %s" (Wsi.keyname key)
3198 let firstof first active =
3199 if first > active || abs (first - active) > fstate.maxrows - 1
3200 then max 0 (active - (fstate.maxrows/2))
3201 else first
3204 let calcfirst first active =
3205 if active > first
3206 then
3207 let rows = active - first in
3208 if rows > fstate.maxrows then active - fstate.maxrows else first
3209 else active
3212 let scrollph y maxy =
3213 let sh = (float (maxy + state.winh) /. float state.winh) in
3214 let sh = float state.winh /. sh in
3215 let sh = max sh (float conf.scrollh) in
3217 let percent =
3218 if y = state.maxy
3219 then 1.0
3220 else float y /. float maxy
3222 let position = (float state.winh -. sh) *. percent in
3224 let position =
3225 if position +. sh > float state.winh
3226 then float state.winh -. sh
3227 else position
3229 position, sh;
3232 let coe s = (s :> uioh);;
3234 class listview ~(source:lvsource) ~trusted ~modehash =
3235 object (self)
3236 val m_pan = source#getpan
3237 val m_first = source#getfirst
3238 val m_active = source#getactive
3239 val m_qsearch = source#getqsearch
3240 val m_prev_uioh = state.uioh
3242 method private elemunder y =
3243 let n = y / (fstate.fontsize+1) in
3244 if m_first + n < source#getitemcount
3245 then (
3246 if source#hasaction (m_first + n)
3247 then Some (m_first + n)
3248 else None
3250 else None
3252 method display =
3253 Gl.enable `blend;
3254 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3255 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3256 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3257 GlDraw.color (1., 1., 1.);
3258 Gl.enable `texture_2d;
3259 let fs = fstate.fontsize in
3260 let nfs = fs + 1 in
3261 let ww = fstate.wwidth in
3262 let tabw = 30.0*.ww in
3263 let itemcount = source#getitemcount in
3264 let rec loop row =
3265 if (row - m_first) > fstate.maxrows
3266 then ()
3267 else (
3268 if row >= 0 && row < itemcount
3269 then (
3270 let (s, level) = source#getitem row in
3271 let y = (row - m_first) * nfs in
3272 let x = 5.0 +. float (level + m_pan) *. ww in
3273 if row = m_active
3274 then (
3275 Gl.disable `texture_2d;
3276 GlDraw.polygon_mode `both `line;
3277 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3278 GlDraw.rect (1., float (y + 1))
3279 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3280 GlDraw.polygon_mode `both `fill;
3281 GlDraw.color (1., 1., 1.);
3282 Gl.enable `texture_2d;
3285 let drawtabularstring s =
3286 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3287 if trusted
3288 then
3289 let tabpos = try String.index s '\t' with Not_found -> -1 in
3290 if tabpos > 0
3291 then
3292 let len = String.length s - tabpos - 1 in
3293 let s1 = String.sub s 0 tabpos
3294 and s2 = String.sub s (tabpos + 1) len in
3295 let nx = drawstr x s1 in
3296 let sw = nx -. x in
3297 let x = x +. (max tabw sw) in
3298 drawstr x s2
3299 else
3300 drawstr x s
3301 else
3302 drawstr x s
3304 let _ = drawtabularstring s in
3305 loop (row+1)
3309 loop m_first;
3310 Gl.disable `blend;
3311 Gl.disable `texture_2d;
3313 method updownlevel incr =
3314 let len = source#getitemcount in
3315 let curlevel =
3316 if m_active >= 0 && m_active < len
3317 then snd (source#getitem m_active)
3318 else -1
3320 let rec flow i =
3321 if i = len then i-1 else if i = -1 then 0 else
3322 let _, l = source#getitem i in
3323 if l != curlevel then i else flow (i+incr)
3325 let active = flow m_active in
3326 let first = calcfirst m_first active in
3327 G.postRedisplay "outline updownlevel";
3328 {< m_active = active; m_first = first >}
3330 method private key1 key mask =
3331 let set1 active first qsearch =
3332 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3334 let search active pattern incr =
3335 let dosearch re =
3336 let rec loop n =
3337 if n >= 0 && n < source#getitemcount
3338 then (
3339 let s, _ = source#getitem n in
3341 (try ignore (Str.search_forward re s 0); true
3342 with Not_found -> false)
3343 then Some n
3344 else loop (n + incr)
3346 else None
3348 loop active
3351 let re = Str.regexp_case_fold pattern in
3352 dosearch re
3353 with Failure s ->
3354 state.text <- s;
3355 None
3357 let itemcount = source#getitemcount in
3358 let find start incr =
3359 let rec find i =
3360 if i = -1 || i = itemcount
3361 then -1
3362 else (
3363 if source#hasaction i
3364 then i
3365 else find (i + incr)
3368 find start
3370 let set active first =
3371 let first = bound first 0 (itemcount - fstate.maxrows) in
3372 state.text <- "";
3373 coe {< m_active = active; m_first = first >}
3375 let navigate incr =
3376 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3377 let active, first =
3378 let incr1 = if incr > 0 then 1 else -1 in
3379 if isvisible m_first m_active
3380 then
3381 let next =
3382 let next = m_active + incr in
3383 let next =
3384 if next < 0 || next >= itemcount
3385 then -1
3386 else find next incr1
3388 if next = -1 || abs (m_active - next) > fstate.maxrows
3389 then -1
3390 else next
3392 if next = -1
3393 then
3394 let first = m_first + incr in
3395 let first = bound first 0 (itemcount - 1) in
3396 let next =
3397 let next = m_active + incr in
3398 let next = bound next 0 (itemcount - 1) in
3399 find next ~-incr1
3401 let active = if next = -1 then m_active else next in
3402 active, first
3403 else
3404 let first = min next m_first in
3405 let first =
3406 if abs (next - first) > fstate.maxrows
3407 then first + incr
3408 else first
3410 next, first
3411 else
3412 let first = m_first + incr in
3413 let first = bound first 0 (itemcount - 1) in
3414 let active =
3415 let next = m_active + incr in
3416 let next = bound next 0 (itemcount - 1) in
3417 let next = find next incr1 in
3418 let active =
3419 if next = -1 || abs (m_active - first) > fstate.maxrows
3420 then (
3421 let active = if m_active = -1 then next else m_active in
3422 active
3424 else next
3426 if isvisible first active
3427 then active
3428 else -1
3430 active, first
3432 G.postRedisplay "listview navigate";
3433 set active first;
3435 match key with
3436 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3437 let incr = if key = 0x72 then -1 else 1 in
3438 let active, first =
3439 match search (m_active + incr) m_qsearch incr with
3440 | None ->
3441 state.text <- m_qsearch ^ " [not found]";
3442 m_active, m_first
3443 | Some active ->
3444 state.text <- m_qsearch;
3445 active, firstof m_first active
3447 G.postRedisplay "listview ctrl-r/s";
3448 set1 active first m_qsearch;
3450 | 0xff08 -> (* backspace *)
3451 if String.length m_qsearch = 0
3452 then coe self
3453 else (
3454 let qsearch = withoutlastutf8 m_qsearch in
3455 let len = String.length qsearch in
3456 if len = 0
3457 then (
3458 state.text <- "";
3459 G.postRedisplay "listview empty qsearch";
3460 set1 m_active m_first "";
3462 else
3463 let active, first =
3464 match search m_active qsearch ~-1 with
3465 | None ->
3466 state.text <- qsearch ^ " [not found]";
3467 m_active, m_first
3468 | Some active ->
3469 state.text <- qsearch;
3470 active, firstof m_first active
3472 G.postRedisplay "listview backspace qsearch";
3473 set1 active first qsearch
3476 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3477 let pattern = m_qsearch ^ toutf8 key in
3478 let active, first =
3479 match search m_active pattern 1 with
3480 | None ->
3481 state.text <- pattern ^ " [not found]";
3482 m_active, m_first
3483 | Some active ->
3484 state.text <- pattern;
3485 active, firstof m_first active
3487 G.postRedisplay "listview qsearch add";
3488 set1 active first pattern;
3490 | 0xff1b -> (* escape *)
3491 state.text <- "";
3492 if String.length m_qsearch = 0
3493 then (
3494 G.postRedisplay "list view escape";
3495 begin
3496 match
3497 source#exit (coe self) true m_active m_first m_pan m_qsearch
3498 with
3499 | None -> m_prev_uioh
3500 | Some uioh -> uioh
3503 else (
3504 G.postRedisplay "list view kill qsearch";
3505 source#setqsearch "";
3506 coe {< m_qsearch = "" >}
3509 | 0xff0d | 0xff8d -> (* (kp) enter *)
3510 state.text <- "";
3511 let self = {< m_qsearch = "" >} in
3512 source#setqsearch "";
3513 let opt =
3514 G.postRedisplay "listview enter";
3515 if m_active >= 0 && m_active < source#getitemcount
3516 then (
3517 source#exit (coe self) false m_active m_first m_pan "";
3519 else (
3520 source#exit (coe self) true m_active m_first m_pan "";
3523 begin match opt with
3524 | None -> m_prev_uioh
3525 | Some uioh -> uioh
3528 | 0xff9f | 0xffff -> (* (kp) delete *)
3529 coe self
3531 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3532 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3533 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3534 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3536 | 0xff53 | 0xff98 -> (* (kp) right *)
3537 state.text <- "";
3538 G.postRedisplay "listview right";
3539 coe {< m_pan = m_pan - 1 >}
3541 | 0xff51 | 0xff96 -> (* (kp) left *)
3542 state.text <- "";
3543 G.postRedisplay "listview left";
3544 coe {< m_pan = m_pan + 1 >}
3546 | 0xff50 | 0xff95 -> (* (kp) home *)
3547 let active = find 0 1 in
3548 G.postRedisplay "listview home";
3549 set active 0;
3551 | 0xff57 | 0xff9c -> (* (kp) end *)
3552 let first = max 0 (itemcount - fstate.maxrows) in
3553 let active = find (itemcount - 1) ~-1 in
3554 G.postRedisplay "listview end";
3555 set active first;
3557 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3558 coe self
3560 | _ ->
3561 dolog "listview unknown key %#x" key; coe self
3563 method key key mask =
3564 match state.mode with
3565 | Textentry te -> textentrykeyboard key mask te; coe self
3566 | _ -> self#key1 key mask
3568 method button button down x y _ =
3569 let opt =
3570 match button with
3571 | 1 when x > state.winw - conf.scrollbw ->
3572 G.postRedisplay "listview scroll";
3573 if down
3574 then
3575 let _, position, sh = self#scrollph in
3576 if y > truncate position && y < truncate (position +. sh)
3577 then (
3578 state.mstate <- Mscrolly;
3579 Some (coe self)
3581 else
3582 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3583 let first = truncate (s *. float source#getitemcount) in
3584 let first = min source#getitemcount first in
3585 Some (coe {< m_first = first; m_active = first >})
3586 else (
3587 state.mstate <- Mnone;
3588 Some (coe self);
3590 | 1 when not down ->
3591 begin match self#elemunder y with
3592 | Some n ->
3593 G.postRedisplay "listview click";
3594 source#exit
3595 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3596 | _ ->
3597 Some (coe self)
3599 | n when (n == 4 || n == 5) && not down ->
3600 let len = source#getitemcount in
3601 let first =
3602 if n = 5 && m_first + fstate.maxrows >= len
3603 then
3604 m_first
3605 else
3606 let first = m_first + (if n == 4 then -1 else 1) in
3607 bound first 0 (len - 1)
3609 G.postRedisplay "listview wheel";
3610 Some (coe {< m_first = first >})
3611 | n when (n = 6 || n = 7) && not down ->
3612 let inc = m_first + (if n = 7 then -1 else 1) in
3613 G.postRedisplay "listview hwheel";
3614 Some (coe {< m_pan = m_pan + inc >})
3615 | _ ->
3616 Some (coe self)
3618 match opt with
3619 | None -> m_prev_uioh
3620 | Some uioh -> uioh
3622 method motion _ y =
3623 match state.mstate with
3624 | Mscrolly ->
3625 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3626 let first = truncate (s *. float source#getitemcount) in
3627 let first = min source#getitemcount first in
3628 G.postRedisplay "listview motion";
3629 coe {< m_first = first; m_active = first >}
3630 | _ -> coe self
3632 method pmotion x y =
3633 if x < state.winw - conf.scrollbw
3634 then
3635 let n =
3636 match self#elemunder y with
3637 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3638 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3640 let o =
3641 if n != m_active
3642 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3643 else self
3645 coe o
3646 else (
3647 Wsi.setcursor Wsi.CURSOR_INHERIT;
3648 coe self
3651 method infochanged _ = ()
3653 method scrollpw = (0, 0.0, 0.0)
3654 method scrollph =
3655 let nfs = fstate.fontsize + 1 in
3656 let y = m_first * nfs in
3657 let itemcount = source#getitemcount in
3658 let maxi = max 0 (itemcount - fstate.maxrows) in
3659 let maxy = maxi * nfs in
3660 let p, h = scrollph y maxy in
3661 conf.scrollbw, p, h
3663 method modehash = modehash
3664 end;;
3666 class outlinelistview ~source =
3667 object (self)
3668 inherit listview
3669 ~source:(source :> lvsource)
3670 ~trusted:false
3671 ~modehash:(findkeyhash conf "outline")
3672 as super
3674 method key key mask =
3675 let calcfirst first active =
3676 if active > first
3677 then
3678 let rows = active - first in
3679 let maxrows =
3680 if String.length state.text = 0
3681 then fstate.maxrows
3682 else fstate.maxrows - 2
3684 if rows > maxrows then active - maxrows else first
3685 else active
3687 let navigate incr =
3688 let active = m_active + incr in
3689 let active = bound active 0 (source#getitemcount - 1) in
3690 let first = calcfirst m_first active in
3691 G.postRedisplay "outline navigate";
3692 coe {< m_active = active; m_first = first >}
3694 let ctrl = Wsi.withctrl mask in
3695 match key with
3696 | 110 when ctrl -> (* ctrl-n *)
3697 source#narrow m_qsearch;
3698 G.postRedisplay "outline ctrl-n";
3699 coe {< m_first = 0; m_active = 0 >}
3701 | 117 when ctrl -> (* ctrl-u *)
3702 source#denarrow;
3703 G.postRedisplay "outline ctrl-u";
3704 state.text <- "";
3705 coe {< m_first = 0; m_active = 0 >}
3707 | 108 when ctrl -> (* ctrl-l *)
3708 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3709 G.postRedisplay "outline ctrl-l";
3710 coe {< m_first = first >}
3712 | 0xff9f | 0xffff -> (* (kp) delete *)
3713 source#remove m_active;
3714 G.postRedisplay "outline delete";
3715 let active = max 0 (m_active-1) in
3716 coe {< m_first = firstof m_first active;
3717 m_active = active >}
3719 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3720 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3721 | 0xff55 | 0xff9a -> (* (kp) prior *)
3722 navigate ~-(fstate.maxrows)
3723 | 0xff56 | 0xff9b -> (* (kp) next *)
3724 navigate fstate.maxrows
3726 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3727 let o =
3728 if ctrl
3729 then (
3730 G.postRedisplay "outline ctrl right";
3731 {< m_pan = m_pan + 1 >}
3733 else self#updownlevel 1
3735 coe o
3737 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3738 let o =
3739 if ctrl
3740 then (
3741 G.postRedisplay "outline ctrl left";
3742 {< m_pan = m_pan - 1 >}
3744 else self#updownlevel ~-1
3746 coe o
3748 | 0xff50 | 0xff95 -> (* (kp) home *)
3749 G.postRedisplay "outline home";
3750 coe {< m_first = 0; m_active = 0 >}
3752 | 0xff57 | 0xff9c -> (* (kp) end *)
3753 let active = source#getitemcount - 1 in
3754 let first = max 0 (active - fstate.maxrows) in
3755 G.postRedisplay "outline end";
3756 coe {< m_active = active; m_first = first >}
3758 | _ -> super#key key mask
3761 let outlinesource usebookmarks =
3762 let empty = [||] in
3763 (object
3764 inherit lvsourcebase
3765 val mutable m_items = empty
3766 val mutable m_orig_items = empty
3767 val mutable m_prev_items = empty
3768 val mutable m_narrow_pattern = ""
3769 val mutable m_hadremovals = false
3771 method getitemcount =
3772 Array.length m_items + (if m_hadremovals then 1 else 0)
3774 method getitem n =
3775 if n == Array.length m_items && m_hadremovals
3776 then
3777 ("[Confirm removal]", 0)
3778 else
3779 let s, n, _ = m_items.(n) in
3780 (s, n)
3782 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3783 ignore (uioh, first, qsearch);
3784 let confrimremoval = m_hadremovals && active = Array.length m_items in
3785 let items =
3786 if String.length m_narrow_pattern = 0
3787 then m_orig_items
3788 else m_items
3790 if not cancel
3791 then (
3792 if not confrimremoval
3793 then(
3794 let _, _, anchor = m_items.(active) in
3795 gotoghyll (getanchory anchor);
3796 m_items <- items;
3798 else (
3799 state.bookmarks <- Array.to_list m_items;
3800 m_orig_items <- m_items;
3803 else m_items <- items;
3804 m_pan <- pan;
3805 None
3807 method hasaction _ = true
3809 method greetmsg =
3810 if Array.length m_items != Array.length m_orig_items
3811 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3812 else ""
3814 method narrow pattern =
3815 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3816 match reopt with
3817 | None -> ()
3818 | Some re ->
3819 let rec loop accu n =
3820 if n = -1
3821 then (
3822 m_narrow_pattern <- pattern;
3823 m_items <- Array.of_list accu
3825 else
3826 let (s, _, _) as o = m_items.(n) in
3827 let accu =
3828 if (try ignore (Str.search_forward re s 0); true
3829 with Not_found -> false)
3830 then o :: accu
3831 else accu
3833 loop accu (n-1)
3835 loop [] (Array.length m_items - 1)
3837 method denarrow =
3838 m_orig_items <- (
3839 if usebookmarks
3840 then Array.of_list state.bookmarks
3841 else state.outlines
3843 m_items <- m_orig_items
3845 method remove m =
3846 if usebookmarks
3847 then
3848 if m >= 0 && m < Array.length m_items
3849 then (
3850 m_hadremovals <- true;
3851 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3852 let n = if n >= m then n+1 else n in
3853 m_items.(n)
3857 method reset anchor items =
3858 m_hadremovals <- false;
3859 if m_orig_items == empty || m_prev_items != items
3860 then (
3861 m_orig_items <- items;
3862 if String.length m_narrow_pattern = 0
3863 then m_items <- items;
3865 m_prev_items <- items;
3866 let rely = getanchory anchor in
3867 let active =
3868 let rec loop n best bestd =
3869 if n = Array.length m_items
3870 then best
3871 else
3872 let (_, _, anchor) = m_items.(n) in
3873 let orely = getanchory anchor in
3874 let d = abs (orely - rely) in
3875 if d < bestd
3876 then loop (n+1) n d
3877 else loop (n+1) best bestd
3879 loop 0 ~-1 max_int
3881 m_active <- active;
3882 m_first <- firstof m_first active
3883 end)
3886 let enterselector usebookmarks =
3887 let source = outlinesource usebookmarks in
3888 fun errmsg ->
3889 let outlines =
3890 if usebookmarks
3891 then Array.of_list state.bookmarks
3892 else state.outlines
3894 if Array.length outlines = 0
3895 then (
3896 showtext ' ' errmsg;
3898 else (
3899 state.text <- source#greetmsg;
3900 Wsi.setcursor Wsi.CURSOR_INHERIT;
3901 let anchor = getanchor () in
3902 source#reset anchor outlines;
3903 state.uioh <- coe (new outlinelistview ~source);
3904 G.postRedisplay "enter selector";
3908 let enteroutlinemode =
3909 let f = enterselector false in
3910 fun ()-> f "Document has no outline";
3913 let enterbookmarkmode =
3914 let f = enterselector true in
3915 fun () -> f "Document has no bookmarks (yet)";
3918 let color_of_string s =
3919 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3920 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3924 let color_to_string (r, g, b) =
3925 let r = truncate (r *. 256.0)
3926 and g = truncate (g *. 256.0)
3927 and b = truncate (b *. 256.0) in
3928 Printf.sprintf "%d/%d/%d" r g b
3931 let irect_of_string s =
3932 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3935 let irect_to_string (x0,y0,x1,y1) =
3936 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3939 let makecheckers () =
3940 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3941 following to say:
3942 converted by Issac Trotts. July 25, 2002 *)
3943 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3944 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3945 let id = GlTex.gen_texture () in
3946 GlTex.bind_texture `texture_2d id;
3947 GlPix.store (`unpack_alignment 1);
3948 GlTex.image2d image;
3949 List.iter (GlTex.parameter ~target:`texture_2d)
3950 [ `mag_filter `nearest; `min_filter `nearest ];
3954 let setcheckers enabled =
3955 match state.texid with
3956 | None ->
3957 if enabled then state.texid <- Some (makecheckers ())
3959 | Some texid ->
3960 if not enabled
3961 then (
3962 GlTex.delete_texture texid;
3963 state.texid <- None;
3967 let int_of_string_with_suffix s =
3968 let l = String.length s in
3969 let s1, shift =
3970 if l > 1
3971 then
3972 let suffix = Char.lowercase s.[l-1] in
3973 match suffix with
3974 | 'k' -> String.sub s 0 (l-1), 10
3975 | 'm' -> String.sub s 0 (l-1), 20
3976 | 'g' -> String.sub s 0 (l-1), 30
3977 | _ -> s, 0
3978 else s, 0
3980 let n = int_of_string s1 in
3981 let m = n lsl shift in
3982 if m < 0 || m < n
3983 then raise (Failure "value too large")
3984 else m
3987 let string_with_suffix_of_int n =
3988 if n = 0
3989 then "0"
3990 else
3991 let n, s =
3992 if n land ((1 lsl 30) - 1) = 0
3993 then n lsr 30, "G"
3994 else (
3995 if n land ((1 lsl 20) - 1) = 0
3996 then n lsr 20, "M"
3997 else (
3998 if n land ((1 lsl 10) - 1) = 0
3999 then n lsr 10, "K"
4000 else n, ""
4004 let rec loop s n =
4005 let h = n mod 1000 in
4006 let n = n / 1000 in
4007 if n = 0
4008 then string_of_int h ^ s
4009 else (
4010 let s = Printf.sprintf "_%03d%s" h s in
4011 loop s n
4014 loop "" n ^ s;
4017 let defghyllscroll = (40, 8, 32);;
4018 let ghyllscroll_of_string s =
4019 let (n, a, b) as nab =
4020 if s = "default"
4021 then defghyllscroll
4022 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4024 if n <= a || n <= b || a >= b
4025 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4026 nab;
4029 let ghyllscroll_to_string ((n, a, b) as nab) =
4030 if nab = defghyllscroll
4031 then "default"
4032 else Printf.sprintf "%d,%d,%d" n a b;
4035 let describe_location () =
4036 let fn = page_of_y state.y in
4037 let ln = page_of_y (state.y + state.winh - state.hscrollh) in
4038 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4039 let percent =
4040 if maxy <= 0
4041 then 100.
4042 else (100. *. (float state.y /. float maxy))
4044 if fn = ln
4045 then
4046 Printf.sprintf "page %d of %d [%.2f%%]"
4047 (fn+1) state.pagecount percent
4048 else
4049 Printf.sprintf
4050 "pages %d-%d of %d [%.2f%%]"
4051 (fn+1) (ln+1) state.pagecount percent
4054 let setpresentationmode v =
4055 let n = page_of_y state.y in
4056 state.anchor <- (n, 0.0, 1.0);
4057 conf.presentation <- v;
4058 if conf.presentation
4059 then (
4060 if not conf.scrollbarinpm
4061 then state.scrollw <- 0;
4063 else state.scrollw <- conf.scrollbw;
4064 represent ();
4067 let enterinfomode =
4068 let btos b = if b then "\xe2\x88\x9a" else "" in
4069 let showextended = ref false in
4070 let leave mode = function
4071 | Confirm -> state.mode <- mode
4072 | Cancel -> state.mode <- mode in
4073 let src =
4074 (object
4075 val mutable m_first_time = true
4076 val mutable m_l = []
4077 val mutable m_a = [||]
4078 val mutable m_prev_uioh = nouioh
4079 val mutable m_prev_mode = View
4081 inherit lvsourcebase
4083 method reset prev_mode prev_uioh =
4084 m_a <- Array.of_list (List.rev m_l);
4085 m_l <- [];
4086 m_prev_mode <- prev_mode;
4087 m_prev_uioh <- prev_uioh;
4088 if m_first_time
4089 then (
4090 let rec loop n =
4091 if n >= Array.length m_a
4092 then ()
4093 else
4094 match m_a.(n) with
4095 | _, _, _, Action _ -> m_active <- n
4096 | _ -> loop (n+1)
4098 loop 0;
4099 m_first_time <- false;
4102 method int name get set =
4103 m_l <-
4104 (name, `int get, 1, Action (
4105 fun u ->
4106 let ondone s =
4107 try set (int_of_string s)
4108 with exn ->
4109 state.text <- Printf.sprintf "bad integer `%s': %s"
4110 s (exntos exn)
4112 state.text <- "";
4113 let te = name ^ ": ", "", None, intentry, ondone, true in
4114 state.mode <- Textentry (te, leave m_prev_mode);
4116 )) :: m_l
4118 method int_with_suffix name get set =
4119 m_l <-
4120 (name, `intws get, 1, Action (
4121 fun u ->
4122 let ondone s =
4123 try set (int_of_string_with_suffix s)
4124 with exn ->
4125 state.text <- Printf.sprintf "bad integer `%s': %s"
4126 s (exntos exn)
4128 state.text <- "";
4129 let te =
4130 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4132 state.mode <- Textentry (te, leave m_prev_mode);
4134 )) :: m_l
4136 method bool ?(offset=1) ?(btos=btos) name get set =
4137 m_l <-
4138 (name, `bool (btos, get), offset, Action (
4139 fun u ->
4140 let v = get () in
4141 set (not v);
4143 )) :: m_l
4145 method color name get set =
4146 m_l <-
4147 (name, `color get, 1, Action (
4148 fun u ->
4149 let invalid = (nan, nan, nan) in
4150 let ondone s =
4151 let c =
4152 try color_of_string s
4153 with exn ->
4154 state.text <- Printf.sprintf "bad color `%s': %s"
4155 s (exntos exn);
4156 invalid
4158 if c <> invalid
4159 then set c;
4161 let te = name ^ ": ", "", None, textentry, ondone, true in
4162 state.text <- color_to_string (get ());
4163 state.mode <- Textentry (te, leave m_prev_mode);
4165 )) :: m_l
4167 method string name get set =
4168 m_l <-
4169 (name, `string get, 1, Action (
4170 fun u ->
4171 let ondone s = set s in
4172 let te = name ^ ": ", "", None, textentry, ondone, true in
4173 state.mode <- Textentry (te, leave m_prev_mode);
4175 )) :: m_l
4177 method colorspace name get set =
4178 m_l <-
4179 (name, `string get, 1, Action (
4180 fun _ ->
4181 let source =
4182 let vals = [| "rgb"; "bgr"; "gray" |] in
4183 (object
4184 inherit lvsourcebase
4186 initializer
4187 m_active <- int_of_colorspace conf.colorspace;
4188 m_first <- 0;
4190 method getitemcount = Array.length vals
4191 method getitem n = (vals.(n), 0)
4192 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4193 ignore (uioh, first, pan, qsearch);
4194 if not cancel then set active;
4195 None
4196 method hasaction _ = true
4197 end)
4199 state.text <- "";
4200 let modehash = findkeyhash conf "info" in
4201 coe (new listview ~source ~trusted:true ~modehash)
4202 )) :: m_l
4204 method caption s offset =
4205 m_l <- (s, `empty, offset, Noaction) :: m_l
4207 method caption2 s f offset =
4208 m_l <- (s, `string f, offset, Noaction) :: m_l
4210 method getitemcount = Array.length m_a
4212 method getitem n =
4213 let tostr = function
4214 | `int f -> string_of_int (f ())
4215 | `intws f -> string_with_suffix_of_int (f ())
4216 | `string f -> f ()
4217 | `color f -> color_to_string (f ())
4218 | `bool (btos, f) -> btos (f ())
4219 | `empty -> ""
4221 let name, t, offset, _ = m_a.(n) in
4222 ((let s = tostr t in
4223 if String.length s > 0
4224 then Printf.sprintf "%s\t%s" name s
4225 else name),
4226 offset)
4228 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4229 let uiohopt =
4230 if not cancel
4231 then (
4232 m_qsearch <- qsearch;
4233 let uioh =
4234 match m_a.(active) with
4235 | _, _, _, Action f -> f uioh
4236 | _ -> uioh
4238 Some uioh
4240 else None
4242 m_active <- active;
4243 m_first <- first;
4244 m_pan <- pan;
4245 uiohopt
4247 method hasaction n =
4248 match m_a.(n) with
4249 | _, _, _, Action _ -> true
4250 | _ -> false
4251 end)
4253 let rec fillsrc prevmode prevuioh =
4254 let sep () = src#caption "" 0 in
4255 let colorp name get set =
4256 src#string name
4257 (fun () -> color_to_string (get ()))
4258 (fun v ->
4260 let c = color_of_string v in
4261 set c
4262 with exn ->
4263 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4266 let oldmode = state.mode in
4267 let birdseye = isbirdseye state.mode in
4269 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4271 src#bool "presentation mode"
4272 (fun () -> conf.presentation)
4273 (fun v -> setpresentationmode v);
4275 src#bool "ignore case in searches"
4276 (fun () -> conf.icase)
4277 (fun v -> conf.icase <- v);
4279 src#bool "preload"
4280 (fun () -> conf.preload)
4281 (fun v -> conf.preload <- v);
4283 src#bool "highlight links"
4284 (fun () -> conf.hlinks)
4285 (fun v -> conf.hlinks <- v);
4287 src#bool "under info"
4288 (fun () -> conf.underinfo)
4289 (fun v -> conf.underinfo <- v);
4291 src#bool "persistent bookmarks"
4292 (fun () -> conf.savebmarks)
4293 (fun v -> conf.savebmarks <- v);
4295 src#bool "proportional display"
4296 (fun () -> conf.proportional)
4297 (fun v -> reqlayout conf.angle v);
4299 src#bool "trim margins"
4300 (fun () -> conf.trimmargins)
4301 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4303 src#bool "persistent location"
4304 (fun () -> conf.jumpback)
4305 (fun v -> conf.jumpback <- v);
4307 sep ();
4308 src#int "inter-page space"
4309 (fun () -> conf.interpagespace)
4310 (fun n ->
4311 conf.interpagespace <- n;
4312 docolumns conf.columns;
4313 let pageno, py =
4314 match state.layout with
4315 | [] -> 0, 0
4316 | l :: _ ->
4317 l.pageno, l.pagey
4319 state.maxy <- calcheight ();
4320 let y = getpagey pageno in
4321 gotoy (y + py)
4324 src#int "page bias"
4325 (fun () -> conf.pagebias)
4326 (fun v -> conf.pagebias <- v);
4328 src#int "scroll step"
4329 (fun () -> conf.scrollstep)
4330 (fun n -> conf.scrollstep <- n);
4332 src#int "horizontal scroll step"
4333 (fun () -> conf.hscrollstep)
4334 (fun v -> conf.hscrollstep <- v);
4336 src#int "auto scroll step"
4337 (fun () ->
4338 match state.autoscroll with
4339 | Some step -> step
4340 | _ -> conf.autoscrollstep)
4341 (fun n ->
4342 if state.autoscroll <> None
4343 then state.autoscroll <- Some n;
4344 conf.autoscrollstep <- n);
4346 src#int "zoom"
4347 (fun () -> truncate (conf.zoom *. 100.))
4348 (fun v -> setzoom ((float v) /. 100.));
4350 src#int "rotation"
4351 (fun () -> conf.angle)
4352 (fun v -> reqlayout v conf.proportional);
4354 src#int "scroll bar width"
4355 (fun () -> state.scrollw)
4356 (fun v ->
4357 state.scrollw <- v;
4358 conf.scrollbw <- v;
4359 reshape state.winw state.winh;
4362 src#int "scroll handle height"
4363 (fun () -> conf.scrollh)
4364 (fun v -> conf.scrollh <- v;);
4366 src#int "thumbnail width"
4367 (fun () -> conf.thumbw)
4368 (fun v ->
4369 conf.thumbw <- min 4096 v;
4370 match oldmode with
4371 | Birdseye beye ->
4372 leavebirdseye beye false;
4373 enterbirdseye ()
4374 | _ -> ()
4377 let mode = state.mode in
4378 src#string "columns"
4379 (fun () ->
4380 match conf.columns with
4381 | Csingle _ -> "1"
4382 | Cmulti (multi, _) -> multicolumns_to_string multi
4383 | Csplit (count, _) -> "-" ^ string_of_int count
4385 (fun v ->
4386 let n, a, b = multicolumns_of_string v in
4387 setcolumns mode n a b);
4389 sep ();
4390 src#caption "Presentation mode" 0;
4391 src#bool "scrollbar visible"
4392 (fun () -> conf.scrollbarinpm)
4393 (fun v ->
4394 if v != conf.scrollbarinpm
4395 then (
4396 conf.scrollbarinpm <- v;
4397 if conf.presentation
4398 then (
4399 state.scrollw <- if v then conf.scrollbw else 0;
4400 reshape state.winw state.winh;
4405 sep ();
4406 src#caption "Pixmap cache" 0;
4407 src#int_with_suffix "size (advisory)"
4408 (fun () -> conf.memlimit)
4409 (fun v -> conf.memlimit <- v);
4411 src#caption2 "used"
4412 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4413 (string_with_suffix_of_int state.memused)
4414 (Hashtbl.length state.tilemap)) 1;
4416 sep ();
4417 src#caption "Layout" 0;
4418 src#caption2 "Dimension"
4419 (fun () ->
4420 Printf.sprintf "%dx%d (virtual %dx%d)"
4421 state.winw state.winh
4422 state.w state.maxy)
4424 if conf.debug
4425 then
4426 src#caption2 "Position" (fun () ->
4427 Printf.sprintf "%dx%d" state.x state.y
4429 else
4430 src#caption2 "Position" (fun () -> describe_location ()) 1
4433 sep ();
4434 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4435 "Save these parameters as global defaults at exit"
4436 (fun () -> conf.bedefault)
4437 (fun v -> conf.bedefault <- v)
4440 sep ();
4441 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4442 src#bool ~offset:0 ~btos "Extended parameters"
4443 (fun () -> !showextended)
4444 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4445 if !showextended
4446 then (
4447 src#bool "checkers"
4448 (fun () -> conf.checkers)
4449 (fun v -> conf.checkers <- v; setcheckers v);
4450 src#bool "update cursor"
4451 (fun () -> conf.updatecurs)
4452 (fun v -> conf.updatecurs <- v);
4453 src#bool "verbose"
4454 (fun () -> conf.verbose)
4455 (fun v -> conf.verbose <- v);
4456 src#bool "invert colors"
4457 (fun () -> conf.invert)
4458 (fun v -> conf.invert <- v);
4459 src#bool "max fit"
4460 (fun () -> conf.maxhfit)
4461 (fun v -> conf.maxhfit <- v);
4462 src#bool "redirect stderr"
4463 (fun () -> conf.redirectstderr)
4464 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4465 src#string "uri launcher"
4466 (fun () -> conf.urilauncher)
4467 (fun v -> conf.urilauncher <- v);
4468 src#string "path launcher"
4469 (fun () -> conf.pathlauncher)
4470 (fun v -> conf.pathlauncher <- v);
4471 src#string "tile size"
4472 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4473 (fun v ->
4475 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4476 conf.tilew <- max 64 w;
4477 conf.tileh <- max 64 h;
4478 flushtiles ();
4479 with exn ->
4480 state.text <- Printf.sprintf "bad tile size `%s': %s"
4481 v (exntos exn)
4483 src#int "texture count"
4484 (fun () -> conf.texcount)
4485 (fun v ->
4486 if realloctexts v
4487 then conf.texcount <- v
4488 else showtext '!' " Failed to set texture count please retry later"
4490 src#int "slice height"
4491 (fun () -> conf.sliceheight)
4492 (fun v ->
4493 conf.sliceheight <- v;
4494 wcmd "sliceh %d" conf.sliceheight;
4496 src#int "anti-aliasing level"
4497 (fun () -> conf.aalevel)
4498 (fun v ->
4499 conf.aalevel <- bound v 0 8;
4500 state.anchor <- getanchor ();
4501 opendoc state.path state.password;
4503 src#string "page scroll scaling factor"
4504 (fun () -> string_of_float conf.pgscale)
4505 (fun v ->
4507 let s = float_of_string v in
4508 conf.pgscale <- s
4509 with exn ->
4510 state.text <- Printf.sprintf
4511 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4514 src#int "ui font size"
4515 (fun () -> fstate.fontsize)
4516 (fun v -> setfontsize (bound v 5 100));
4517 src#int "hint font size"
4518 (fun () -> conf.hfsize)
4519 (fun v -> conf.hfsize <- bound v 5 100);
4520 colorp "background color"
4521 (fun () -> conf.bgcolor)
4522 (fun v -> conf.bgcolor <- v);
4523 src#bool "crop hack"
4524 (fun () -> conf.crophack)
4525 (fun v -> conf.crophack <- v);
4526 src#string "trim fuzz"
4527 (fun () -> irect_to_string conf.trimfuzz)
4528 (fun v ->
4530 conf.trimfuzz <- irect_of_string v;
4531 if conf.trimmargins
4532 then settrim true conf.trimfuzz;
4533 with exn ->
4534 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4536 src#string "throttle"
4537 (fun () ->
4538 match conf.maxwait with
4539 | None -> "show place holder if page is not ready"
4540 | Some time ->
4541 if time = infinity
4542 then "wait for page to fully render"
4543 else
4544 "wait " ^ string_of_float time
4545 ^ " seconds before showing placeholder"
4547 (fun v ->
4549 let f = float_of_string v in
4550 if f <= 0.0
4551 then conf.maxwait <- None
4552 else conf.maxwait <- Some f
4553 with exn ->
4554 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4556 src#string "ghyll scroll"
4557 (fun () ->
4558 match conf.ghyllscroll with
4559 | None -> ""
4560 | Some nab -> ghyllscroll_to_string nab
4562 (fun v ->
4564 let gs =
4565 if String.length v = 0
4566 then None
4567 else Some (ghyllscroll_of_string v)
4569 conf.ghyllscroll <- gs
4570 with exn ->
4571 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4573 src#string "selection command"
4574 (fun () -> conf.selcmd)
4575 (fun v -> conf.selcmd <- v);
4576 src#string "synctex command"
4577 (fun () -> conf.stcmd)
4578 (fun v -> conf.stcmd <- v);
4579 src#colorspace "color space"
4580 (fun () -> colorspace_to_string conf.colorspace)
4581 (fun v ->
4582 conf.colorspace <- colorspace_of_int v;
4583 wcmd "cs %d" v;
4584 load state.layout;
4586 if pbousable ()
4587 then
4588 src#bool "use PBO"
4589 (fun () -> conf.usepbo)
4590 (fun v -> conf.usepbo <- v);
4591 src#bool "mouse wheel scrolls pages"
4592 (fun () -> conf.wheelbypage)
4593 (fun v -> conf.wheelbypage <- v);
4596 sep ();
4597 src#caption "Document" 0;
4598 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4599 src#caption2 "Pages"
4600 (fun () -> string_of_int state.pagecount) 1;
4601 src#caption2 "Dimensions"
4602 (fun () -> string_of_int (List.length state.pdims)) 1;
4603 if conf.trimmargins
4604 then (
4605 sep ();
4606 src#caption "Trimmed margins" 0;
4607 src#caption2 "Dimensions"
4608 (fun () -> string_of_int (List.length state.pdims)) 1;
4611 sep ();
4612 src#caption "OpenGL" 0;
4613 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4614 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4615 src#reset prevmode prevuioh;
4617 fun () ->
4618 state.text <- "";
4619 let prevmode = state.mode
4620 and prevuioh = state.uioh in
4621 fillsrc prevmode prevuioh;
4622 let source = (src :> lvsource) in
4623 let modehash = findkeyhash conf "info" in
4624 state.uioh <- coe (object (self)
4625 inherit listview ~source ~trusted:true ~modehash as super
4626 val mutable m_prevmemused = 0
4627 method infochanged = function
4628 | Memused ->
4629 if m_prevmemused != state.memused
4630 then (
4631 m_prevmemused <- state.memused;
4632 G.postRedisplay "memusedchanged";
4634 | Pdim -> G.postRedisplay "pdimchanged"
4635 | Docinfo -> fillsrc prevmode prevuioh
4637 method key key mask =
4638 if not (Wsi.withctrl mask)
4639 then
4640 match key with
4641 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4642 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4643 | _ -> super#key key mask
4644 else super#key key mask
4645 end);
4646 G.postRedisplay "info";
4649 let enterhelpmode =
4650 let source =
4651 (object
4652 inherit lvsourcebase
4653 method getitemcount = Array.length state.help
4654 method getitem n =
4655 let s, l, _ = state.help.(n) in
4656 (s, l)
4658 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4659 let optuioh =
4660 if not cancel
4661 then (
4662 m_qsearch <- qsearch;
4663 match state.help.(active) with
4664 | _, _, Action f -> Some (f uioh)
4665 | _ -> Some (uioh)
4667 else None
4669 m_active <- active;
4670 m_first <- first;
4671 m_pan <- pan;
4672 optuioh
4674 method hasaction n =
4675 match state.help.(n) with
4676 | _, _, Action _ -> true
4677 | _ -> false
4679 initializer
4680 m_active <- -1
4681 end)
4682 in fun () ->
4683 let modehash = findkeyhash conf "help" in
4684 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4685 G.postRedisplay "help";
4688 let entermsgsmode =
4689 let msgsource =
4690 let re = Str.regexp "[\r\n]" in
4691 (object
4692 inherit lvsourcebase
4693 val mutable m_items = [||]
4695 method getitemcount = 1 + Array.length m_items
4697 method getitem n =
4698 if n = 0
4699 then "[Clear]", 0
4700 else m_items.(n-1), 0
4702 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4703 ignore uioh;
4704 if not cancel
4705 then (
4706 if active = 0
4707 then Buffer.clear state.errmsgs;
4708 m_qsearch <- qsearch;
4710 m_active <- active;
4711 m_first <- first;
4712 m_pan <- pan;
4713 None
4715 method hasaction n =
4716 n = 0
4718 method reset =
4719 state.newerrmsgs <- false;
4720 let l = Str.split re (Buffer.contents state.errmsgs) in
4721 m_items <- Array.of_list l
4723 initializer
4724 m_active <- 0
4725 end)
4726 in fun () ->
4727 state.text <- "";
4728 msgsource#reset;
4729 let source = (msgsource :> lvsource) in
4730 let modehash = findkeyhash conf "listview" in
4731 state.uioh <- coe (object
4732 inherit listview ~source ~trusted:false ~modehash as super
4733 method display =
4734 if state.newerrmsgs
4735 then msgsource#reset;
4736 super#display
4737 end);
4738 G.postRedisplay "msgs";
4741 let quickbookmark ?title () =
4742 match state.layout with
4743 | [] -> ()
4744 | l :: _ ->
4745 let title =
4746 match title with
4747 | None ->
4748 let sec = Unix.gettimeofday () in
4749 let tm = Unix.localtime sec in
4750 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4751 (l.pageno+1)
4752 tm.Unix.tm_mday
4753 tm.Unix.tm_mon
4754 (tm.Unix.tm_year + 1900)
4755 tm.Unix.tm_hour
4756 tm.Unix.tm_min
4757 | Some title -> title
4759 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4762 let doreshape w h =
4763 Wsi.reshape w h;
4766 let setautoscrollspeed step goingdown =
4767 let incr = max 1 ((abs step) / 2) in
4768 let incr = if goingdown then incr else -incr in
4769 let astep = step + incr in
4770 state.autoscroll <- Some astep;
4773 let gotounder = function
4774 | Ulinkgoto (pageno, top) ->
4775 if pageno >= 0
4776 then (
4777 addnav ();
4778 gotopage1 pageno top;
4781 | Ulinkuri s ->
4782 gotouri s
4784 | Uremote (filename, pageno) ->
4785 let path =
4786 if Sys.file_exists filename
4787 then filename
4788 else
4789 let dir = Filename.dirname state.path in
4790 let path = Filename.concat dir filename in
4791 if Sys.file_exists path
4792 then path
4793 else ""
4795 if String.length path > 0
4796 then (
4797 let anchor = getanchor () in
4798 let ranchor = state.path, state.password, anchor in
4799 state.anchor <- (pageno, 0.0, 0.0);
4800 state.ranchors <- ranchor :: state.ranchors;
4801 opendoc path "";
4803 else showtext '!' ("Could not find " ^ filename)
4805 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4808 let canpan () =
4809 match conf.columns with
4810 | Csplit _ -> true
4811 | _ -> state.x != 0 || conf.zoom > 1.0
4814 let existsinrow pageno (columns, coverA, coverB) p =
4815 let last = ((pageno - coverA) mod columns) + columns in
4816 let rec any = function
4817 | [] -> false
4818 | l :: rest ->
4819 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4820 then p l
4821 else (
4822 if not (p l)
4823 then (if l.pageno = last then false else any rest)
4824 else true
4827 any state.layout
4830 let nextpage () =
4831 match state.layout with
4832 | [] ->
4833 let pageno = page_of_y state.y in
4834 gotoghyll (getpagey (pageno+1))
4835 | l :: rest ->
4836 match conf.columns with
4837 | Csingle _ ->
4838 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4839 then
4840 let y = clamp (pgscale state.winh) in
4841 gotoghyll y
4842 else
4843 let pageno = min (l.pageno+1) (state.pagecount-1) in
4844 gotoghyll (getpagey pageno)
4845 | Cmulti ((c, _, _) as cl, _) ->
4846 if conf.presentation
4847 && (existsinrow l.pageno cl
4848 (fun l -> l.pageh > l.pagey + l.pagevh))
4849 then
4850 let y = clamp (pgscale state.winh) in
4851 gotoghyll y
4852 else
4853 let pageno = min (l.pageno+c) (state.pagecount-1) in
4854 gotoghyll (getpagey pageno)
4855 | Csplit (n, _) ->
4856 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4857 then
4858 let pagey, pageh = getpageyh l.pageno in
4859 let pagey = pagey + pageh * l.pagecol in
4860 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4861 gotoghyll (pagey + pageh + ips)
4864 let prevpage () =
4865 match state.layout with
4866 | [] ->
4867 let pageno = page_of_y state.y in
4868 gotoghyll (getpagey (pageno-1))
4869 | l :: _ ->
4870 match conf.columns with
4871 | Csingle _ ->
4872 if conf.presentation && l.pagey != 0
4873 then
4874 gotoghyll (clamp (pgscale ~-(state.winh)))
4875 else
4876 let pageno = max 0 (l.pageno-1) in
4877 gotoghyll (getpagey pageno)
4878 | Cmulti ((c, _, coverB) as cl, _) ->
4879 if conf.presentation &&
4880 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4881 then
4882 gotoghyll (clamp (pgscale ~-(state.winh)))
4883 else
4884 let decr =
4885 if l.pageno = state.pagecount - coverB
4886 then 1
4887 else c
4889 let pageno = max 0 (l.pageno-decr) in
4890 gotoghyll (getpagey pageno)
4891 | Csplit (n, _) ->
4892 let y =
4893 if l.pagecol = 0
4894 then
4895 if l.pageno = 0
4896 then l.pagey
4897 else
4898 let pageno = max 0 (l.pageno-1) in
4899 let pagey, pageh = getpageyh pageno in
4900 pagey + (n-1)*pageh
4901 else
4902 let pagey, pageh = getpageyh l.pageno in
4903 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4905 gotoghyll y
4908 let viewkeyboard key mask =
4909 let enttext te =
4910 let mode = state.mode in
4911 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4912 state.text <- "";
4913 enttext ();
4914 G.postRedisplay "view:enttext"
4916 let ctrl = Wsi.withctrl mask in
4917 let key =
4918 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4920 match key with
4921 | 81 -> (* Q *)
4922 exit 0
4924 | 0xff63 -> (* insert *)
4925 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4926 then (
4927 state.mode <- LinkNav (Ltgendir 0);
4928 gotoy state.y;
4930 else showtext '!' "Keyboard link navigation does not work under rotation"
4932 | 0xff1b | 113 -> (* escape / q *)
4933 begin match state.mstate with
4934 | Mzoomrect _ ->
4935 state.mstate <- Mnone;
4936 Wsi.setcursor Wsi.CURSOR_INHERIT;
4937 G.postRedisplay "kill zoom rect";
4938 | _ ->
4939 begin match state.mode with
4940 | LinkNav _ ->
4941 state.mode <- View;
4942 G.postRedisplay "esc leave linknav"
4943 | _ ->
4944 match state.ranchors with
4945 | [] -> raise Quit
4946 | (path, password, anchor) :: rest ->
4947 state.ranchors <- rest;
4948 state.anchor <- anchor;
4949 opendoc path password
4950 end;
4951 end;
4953 | 0xff08 -> (* backspace *)
4954 gotoghyll (getnav ~-1)
4956 | 111 -> (* o *)
4957 enteroutlinemode ()
4959 | 117 -> (* u *)
4960 state.rects <- [];
4961 state.text <- "";
4962 G.postRedisplay "dehighlight";
4964 | 47 | 63 -> (* / ? *)
4965 let ondone isforw s =
4966 cbput state.hists.pat s;
4967 state.searchpattern <- s;
4968 search s isforw
4970 let s = String.create 1 in
4971 s.[0] <- Char.chr key;
4972 enttext (s, "", Some (onhist state.hists.pat),
4973 textentry, ondone (key = 47), true)
4975 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4976 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4977 setzoom (conf.zoom +. incr)
4979 | 43 | 0xffab -> (* + *)
4980 let ondone s =
4981 let n =
4982 try int_of_string s with exc ->
4983 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4984 max_int
4986 if n != max_int
4987 then (
4988 conf.pagebias <- n;
4989 state.text <- "page bias is now " ^ string_of_int n;
4992 enttext ("page bias: ", "", None, intentry, ondone, true)
4994 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4995 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4996 setzoom (max 0.01 (conf.zoom -. decr))
4998 | 45 | 0xffad -> (* - *)
4999 let ondone msg = state.text <- msg in
5000 enttext (
5001 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5002 optentry state.mode, ondone, true
5005 | 48 when ctrl -> (* ctrl-0 *)
5006 if conf.zoom = 1.0
5007 then (
5008 state.x <- 0;
5009 state.hscrollh <-
5010 if state.w <= state.winw - state.scrollw
5011 then 0
5012 else state.scrollw
5014 gotoy state.y
5016 else setzoom 1.0
5018 | 49 when ctrl -> (* ctrl-1 *)
5019 let cols =
5020 match conf.columns with
5021 | Csingle _ | Cmulti _ -> 1
5022 | Csplit (n, _) -> n
5024 let zoom = zoomforh state.winw state.winh state.scrollw cols in
5025 if zoom < 1.0
5026 then setzoom zoom
5028 | 0xffc6 -> (* f9 *)
5029 togglebirdseye ()
5031 | 57 when ctrl -> (* ctrl-9 *)
5032 togglebirdseye ()
5034 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5035 when not ctrl -> (* 0..9 *)
5036 let ondone s =
5037 let n =
5038 try int_of_string s with exc ->
5039 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5042 if n >= 0
5043 then (
5044 addnav ();
5045 cbput state.hists.pag (string_of_int n);
5046 gotopage1 (n + conf.pagebias - 1) 0;
5049 let pageentry text key =
5050 match Char.unsafe_chr key with
5051 | 'g' -> TEdone text
5052 | _ -> intentry text key
5054 let text = "x" in text.[0] <- Char.chr key;
5055 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5057 | 98 -> (* b *)
5058 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5059 reshape state.winw state.winh;
5061 | 108 -> (* l *)
5062 conf.hlinks <- not conf.hlinks;
5063 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5064 G.postRedisplay "toggle highlightlinks";
5066 | 70 -> (* F *)
5067 state.glinks <- true;
5068 let mode = state.mode in
5069 state.mode <- Textentry (
5070 (":", "", None, linknentry, linkndone gotounder, false),
5071 (fun _ ->
5072 state.glinks <- false;
5073 state.mode <- mode)
5075 state.text <- "";
5076 G.postRedisplay "view:linkent(F)"
5078 | 121 -> (* y *)
5079 state.glinks <- true;
5080 let mode = state.mode in
5081 state.mode <- Textentry (
5082 (":", "", None, linknentry, linkndone (fun under ->
5083 match Ne.pipe () with
5084 | Ne.Exn exn ->
5085 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
5086 | Ne.Res (r, w) ->
5087 let popened =
5088 try popen conf.selcmd [r, 0; w, -1]; true
5089 with exn ->
5090 showtext '!'
5091 (Printf.sprintf "failed to execute %s: %s"
5092 conf.selcmd (exntos exn));
5093 false
5095 let clo cap fd =
5096 Ne.clo fd (fun msg ->
5097 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5100 let s = undertext under in
5101 if popened
5102 then
5103 (try
5104 let l = String.length s in
5105 let n = tempfailureretry (Unix.write w s 0) l in
5106 if n != l
5107 then
5108 showtext '!'
5109 (Printf.sprintf
5110 "failed to write %d characters to sel pipe, wrote %d"
5113 with exn ->
5114 showtext '!'
5115 (Printf.sprintf "failed to write to sel pipe: %s"
5116 (exntos exn)
5119 else dolog "%s" s;
5120 clo "pipe/r" r;
5121 clo "pipe/w" w;
5122 ), false
5124 fun _ ->
5125 state.glinks <- false;
5126 state.mode <- mode
5128 state.text <- "";
5129 G.postRedisplay "view:linkent"
5131 | 97 -> (* a *)
5132 begin match state.autoscroll with
5133 | Some step ->
5134 conf.autoscrollstep <- step;
5135 state.autoscroll <- None
5136 | None ->
5137 if conf.autoscrollstep = 0
5138 then state.autoscroll <- Some 1
5139 else state.autoscroll <- Some conf.autoscrollstep
5142 | 112 when ctrl -> (* ctrl-p *)
5143 launchpath ()
5145 | 80 -> (* P *)
5146 setpresentationmode (not conf.presentation);
5147 showtext ' ' ("presentation mode " ^
5148 if conf.presentation then "on" else "off");
5150 | 102 -> (* f *)
5151 if List.mem Wsi.Fullscreen state.winstate
5152 then doreshape conf.cwinw conf.cwinh
5153 else Wsi.fullscreen ()
5155 | 112 | 78 -> (* p|N *)
5156 search state.searchpattern false
5158 | 110 | 0xffc0 -> (* n|F3 *)
5159 search state.searchpattern true
5161 | 116 -> (* t *)
5162 begin match state.layout with
5163 | [] -> ()
5164 | l :: _ ->
5165 gotoghyll (getpagey l.pageno)
5168 | 32 -> (* space *)
5169 nextpage ()
5171 | 0xff9f | 0xffff -> (* delete *)
5172 prevpage ()
5174 | 61 -> (* = *)
5175 showtext ' ' (describe_location ());
5177 | 119 -> (* w *)
5178 begin match state.layout with
5179 | [] -> ()
5180 | l :: _ ->
5181 doreshape (l.pagew + state.scrollw) l.pageh;
5182 G.postRedisplay "w"
5185 | 39 -> (* ' *)
5186 enterbookmarkmode ()
5188 | 104 | 0xffbe -> (* h|F1 *)
5189 enterhelpmode ()
5191 | 105 -> (* i *)
5192 enterinfomode ()
5194 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5195 entermsgsmode ()
5197 | 109 -> (* m *)
5198 let ondone s =
5199 match state.layout with
5200 | l :: _ ->
5201 if String.length s > 0
5202 then
5203 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5204 | _ -> ()
5206 enttext ("bookmark: ", "", None, textentry, ondone, true)
5208 | 126 -> (* ~ *)
5209 quickbookmark ();
5210 showtext ' ' "Quick bookmark added";
5212 | 122 -> (* z *)
5213 begin match state.layout with
5214 | l :: _ ->
5215 let rect = getpdimrect l.pagedimno in
5216 let w, h =
5217 if conf.crophack
5218 then
5219 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5220 truncate (1.2 *. (rect.(3) -. rect.(0))))
5221 else
5222 (truncate (rect.(1) -. rect.(0)),
5223 truncate (rect.(3) -. rect.(0)))
5225 let w = truncate ((float w)*.conf.zoom)
5226 and h = truncate ((float h)*.conf.zoom) in
5227 if w != 0 && h != 0
5228 then (
5229 state.anchor <- getanchor ();
5230 doreshape (w + state.scrollw) (h + conf.interpagespace)
5232 G.postRedisplay "z";
5234 | [] -> ()
5237 | 50 when ctrl -> (* ctrl-2 *)
5238 let maxw = getmaxw () in
5239 if maxw > 0.0
5240 then setzoom (maxw /. float state.winw)
5242 | 60 | 62 -> (* < > *)
5243 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5245 | 91 | 93 -> (* [ ] *)
5246 conf.colorscale <-
5247 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5249 G.postRedisplay "brightness";
5251 | 99 when state.mode = View -> (* c *)
5252 let (c, a, b), z =
5253 match state.prevcolumns with
5254 | None -> (1, 0, 0), 1.0
5255 | Some (columns, z) ->
5256 let cab =
5257 match columns with
5258 | Csplit (c, _) -> -c, 0, 0
5259 | Cmulti ((c, a, b), _) -> c, a, b
5260 | Csingle _ -> 1, 0, 0
5262 cab, z
5264 setcolumns View c a b;
5265 setzoom z;
5267 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5268 setzoom state.prevzoom
5270 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5271 begin match state.autoscroll with
5272 | None ->
5273 begin match state.mode with
5274 | Birdseye beye -> upbirdseye 1 beye
5275 | _ ->
5276 if ctrl
5277 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5278 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5280 | Some n ->
5281 setautoscrollspeed n false
5284 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5285 begin match state.autoscroll with
5286 | None ->
5287 begin match state.mode with
5288 | Birdseye beye -> downbirdseye 1 beye
5289 | _ ->
5290 if ctrl
5291 then gotoy_and_clear_text (clamp (state.winh/2))
5292 else gotoy_and_clear_text (clamp conf.scrollstep)
5294 | Some n ->
5295 setautoscrollspeed n true
5298 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5299 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5300 if canpan ()
5301 then
5302 let dx =
5303 if ctrl
5304 then state.winw / 2
5305 else conf.hscrollstep
5307 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5308 state.x <- state.x + dx;
5309 gotoy_and_clear_text state.y
5310 else (
5311 state.text <- "";
5312 G.postRedisplay "lef/right"
5315 | 0xff55 | 0xff9a -> (* (kp) prior *)
5316 let y =
5317 if ctrl
5318 then
5319 match state.layout with
5320 | [] -> state.y
5321 | l :: _ -> state.y - l.pagey
5322 else
5323 clamp (pgscale (-state.winh))
5325 gotoghyll y
5327 | 0xff56 | 0xff9b -> (* (kp) next *)
5328 let y =
5329 if ctrl
5330 then
5331 match List.rev state.layout with
5332 | [] -> state.y
5333 | l :: _ -> getpagey l.pageno
5334 else
5335 clamp (pgscale state.winh)
5337 gotoghyll y
5339 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5340 gotoghyll 0
5341 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5342 gotoghyll (clamp state.maxy)
5344 | 0xff53 | 0xff98
5345 when Wsi.withalt mask -> (* alt-(kp) right *)
5346 gotoghyll (getnav 1)
5347 | 0xff51 | 0xff96
5348 when Wsi.withalt mask -> (* alt-(kp) left *)
5349 gotoghyll (getnav ~-1)
5351 | 114 -> (* r *)
5352 reload ()
5354 | 118 when conf.debug -> (* v *)
5355 state.rects <- [];
5356 List.iter (fun l ->
5357 match getopaque l.pageno with
5358 | None -> ()
5359 | Some opaque ->
5360 let x0, y0, x1, y1 = pagebbox opaque in
5361 let a,b = float x0, float y0 in
5362 let c,d = float x1, float y0 in
5363 let e,f = float x1, float y1 in
5364 let h,j = float x0, float y1 in
5365 let rect = (a,b,c,d,e,f,h,j) in
5366 debugrect rect;
5367 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5368 ) state.layout;
5369 G.postRedisplay "v";
5371 | _ ->
5372 vlog "huh? %s" (Wsi.keyname key)
5375 let linknavkeyboard key mask linknav =
5376 let getpage pageno =
5377 let rec loop = function
5378 | [] -> None
5379 | l :: _ when l.pageno = pageno -> Some l
5380 | _ :: rest -> loop rest
5381 in loop state.layout
5383 let doexact (pageno, n) =
5384 match getopaque pageno, getpage pageno with
5385 | Some opaque, Some l ->
5386 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5387 then
5388 let under = getlink opaque n in
5389 G.postRedisplay "link gotounder";
5390 gotounder under;
5391 state.mode <- View;
5392 else
5393 let opt, dir =
5394 match key with
5395 | 0xff50 -> (* home *)
5396 Some (findlink opaque LDfirst), -1
5398 | 0xff57 -> (* end *)
5399 Some (findlink opaque LDlast), 1
5401 | 0xff51 -> (* left *)
5402 Some (findlink opaque (LDleft n)), -1
5404 | 0xff53 -> (* right *)
5405 Some (findlink opaque (LDright n)), 1
5407 | 0xff52 -> (* up *)
5408 Some (findlink opaque (LDup n)), -1
5410 | 0xff54 -> (* down *)
5411 Some (findlink opaque (LDdown n)), 1
5413 | _ -> None, 0
5415 let pwl l dir =
5416 begin match findpwl l.pageno dir with
5417 | Pwlnotfound -> ()
5418 | Pwl pageno ->
5419 let notfound dir =
5420 state.mode <- LinkNav (Ltgendir dir);
5421 let y, h = getpageyh pageno in
5422 let y =
5423 if dir < 0
5424 then y + h - state.winh
5425 else y
5427 gotoy y
5429 begin match getopaque pageno, getpage pageno with
5430 | Some opaque, Some _ ->
5431 let link =
5432 let ld = if dir > 0 then LDfirst else LDlast in
5433 findlink opaque ld
5435 begin match link with
5436 | Lfound m ->
5437 showlinktype (getlink opaque m);
5438 state.mode <- LinkNav (Ltexact (pageno, m));
5439 G.postRedisplay "linknav jpage";
5440 | _ -> notfound dir
5441 end;
5442 | _ -> notfound dir
5443 end;
5444 end;
5446 begin match opt with
5447 | Some Lnotfound -> pwl l dir;
5448 | Some (Lfound m) ->
5449 if m = n
5450 then pwl l dir
5451 else (
5452 let _, y0, _, y1 = getlinkrect opaque m in
5453 if y0 < l.pagey
5454 then gotopage1 l.pageno y0
5455 else (
5456 let d = fstate.fontsize + 1 in
5457 if y1 - l.pagey > l.pagevh - d
5458 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5459 else G.postRedisplay "linknav";
5461 showlinktype (getlink opaque m);
5462 state.mode <- LinkNav (Ltexact (l.pageno, m));
5465 | None -> viewkeyboard key mask
5466 end;
5467 | _ -> viewkeyboard key mask
5469 if key = 0xff63
5470 then (
5471 state.mode <- View;
5472 G.postRedisplay "leave linknav"
5474 else
5475 match linknav with
5476 | Ltgendir _ -> viewkeyboard key mask
5477 | Ltexact exact -> doexact exact
5480 let keyboard key mask =
5481 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5482 then wcmd "interrupt"
5483 else state.uioh <- state.uioh#key key mask
5486 let birdseyekeyboard key mask
5487 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5488 let incr =
5489 match conf.columns with
5490 | Csingle _ -> 1
5491 | Cmulti ((c, _, _), _) -> c
5492 | Csplit _ -> failwith "bird's eye split mode"
5494 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5495 match key with
5496 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5497 let y, h = getpageyh pageno in
5498 let top = (state.winh - h) / 2 in
5499 gotoy (max 0 (y - top))
5500 | 0xff0d (* enter *)
5501 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5502 | 0xff1b -> leavebirdseye beye true (* escape *)
5503 | 0xff52 -> upbirdseye incr beye (* up *)
5504 | 0xff54 -> downbirdseye incr beye (* down *)
5505 | 0xff51 -> upbirdseye 1 beye (* left *)
5506 | 0xff53 -> downbirdseye 1 beye (* right *)
5508 | 0xff55 -> (* prior *)
5509 begin match state.layout with
5510 | l :: _ ->
5511 if l.pagey != 0
5512 then (
5513 state.mode <- Birdseye (
5514 oconf, leftx, l.pageno, hooverpageno, anchor
5516 gotopage1 l.pageno 0;
5518 else (
5519 let layout = layout (state.y-state.winh) (pgh state.layout) in
5520 match layout with
5521 | [] -> gotoy (clamp (-state.winh))
5522 | l :: _ ->
5523 state.mode <- Birdseye (
5524 oconf, leftx, l.pageno, hooverpageno, anchor
5526 gotopage1 l.pageno 0
5529 | [] -> gotoy (clamp (-state.winh))
5530 end;
5532 | 0xff56 -> (* next *)
5533 begin match List.rev state.layout with
5534 | l :: _ ->
5535 let layout = layout (state.y + (pgh state.layout)) state.winh in
5536 begin match layout with
5537 | [] ->
5538 let incr = l.pageh - l.pagevh in
5539 if incr = 0
5540 then (
5541 state.mode <-
5542 Birdseye (
5543 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5545 G.postRedisplay "birdseye pagedown";
5547 else gotoy (clamp (incr + conf.interpagespace*2));
5549 | l :: _ ->
5550 state.mode <-
5551 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5552 gotopage1 l.pageno 0;
5555 | [] -> gotoy (clamp state.winh)
5556 end;
5558 | 0xff50 -> (* home *)
5559 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5560 gotopage1 0 0
5562 | 0xff57 -> (* end *)
5563 let pageno = state.pagecount - 1 in
5564 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5565 if not (pagevisible state.layout pageno)
5566 then
5567 let h =
5568 match List.rev state.pdims with
5569 | [] -> state.winh
5570 | (_, _, h, _) :: _ -> h
5572 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5573 else G.postRedisplay "birdseye end";
5574 | _ -> viewkeyboard key mask
5577 let drawpage l linkindexbase =
5578 let color =
5579 match state.mode with
5580 | Textentry _ -> scalecolor 0.4
5581 | LinkNav _
5582 | View -> scalecolor 1.0
5583 | Birdseye (_, _, pageno, hooverpageno, _) ->
5584 if l.pageno = hooverpageno
5585 then scalecolor 0.9
5586 else (
5587 if l.pageno = pageno
5588 then scalecolor 1.0
5589 else scalecolor 0.8
5592 drawtiles l color;
5593 begin match getopaque l.pageno with
5594 | Some opaque ->
5595 if tileready l l.pagex l.pagey
5596 then
5597 let x = l.pagedispx - l.pagex
5598 and y = l.pagedispy - l.pagey in
5599 let hlmask =
5600 match conf.columns with
5601 | Csingle _ | Cmulti _ ->
5602 (if conf.hlinks then 1 else 0)
5603 + (if state.glinks
5604 && not (isbirdseye state.mode) then 2 else 0)
5605 | _ -> 0
5607 let s =
5608 match state.mode with
5609 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5610 | _ -> ""
5612 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5613 else 0
5615 | _ -> 0
5616 end;
5619 let scrollindicator () =
5620 let sbw, ph, sh = state.uioh#scrollph in
5621 let sbh, pw, sw = state.uioh#scrollpw in
5623 GlDraw.color (0.64, 0.64, 0.64);
5624 GlDraw.rect
5625 (float (state.winw - sbw), 0.)
5626 (float state.winw, float state.winh)
5628 GlDraw.rect
5629 (0., float (state.winh - sbh))
5630 (float (state.winw - state.scrollw - 1), float state.winh)
5632 GlDraw.color (0.0, 0.0, 0.0);
5634 GlDraw.rect
5635 (float (state.winw - sbw), ph)
5636 (float state.winw, ph +. sh)
5638 GlDraw.rect
5639 (pw, float (state.winh - sbh))
5640 (pw +. sw, float state.winh)
5644 let showsel () =
5645 match state.mstate with
5646 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5649 | Msel ((x0, y0), (x1, y1)) ->
5650 let rec loop = function
5651 | l :: ls ->
5652 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5653 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5654 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5655 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5656 then
5657 match getopaque l.pageno with
5658 | Some opaque ->
5659 let x0, y0 = pagetranslatepoint l x0 y0 in
5660 let x1, y1 = pagetranslatepoint l x1 y1 in
5661 seltext opaque (x0, y0, x1, y1);
5662 | _ -> ()
5663 else loop ls
5664 | [] -> ()
5666 loop state.layout
5669 let showrects rects =
5670 Gl.enable `blend;
5671 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5672 GlDraw.polygon_mode `both `fill;
5673 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5674 List.iter
5675 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5676 List.iter (fun l ->
5677 if l.pageno = pageno
5678 then (
5679 let dx = float (l.pagedispx - l.pagex) in
5680 let dy = float (l.pagedispy - l.pagey) in
5681 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5682 GlDraw.begins `quads;
5684 GlDraw.vertex2 (x0+.dx, y0+.dy);
5685 GlDraw.vertex2 (x1+.dx, y1+.dy);
5686 GlDraw.vertex2 (x2+.dx, y2+.dy);
5687 GlDraw.vertex2 (x3+.dx, y3+.dy);
5689 GlDraw.ends ();
5691 ) state.layout
5692 ) rects
5694 Gl.disable `blend;
5697 let display () =
5698 GlClear.color (scalecolor2 conf.bgcolor);
5699 GlClear.clear [`color];
5700 let rec loop linkindexbase = function
5701 | l :: rest ->
5702 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5703 loop linkindexbase rest
5704 | [] -> ()
5706 loop 0 state.layout;
5707 let rects =
5708 match state.mode with
5709 | LinkNav (Ltexact (pageno, linkno)) ->
5710 begin match getopaque pageno with
5711 | Some opaque ->
5712 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5713 (pageno, 5, (
5714 float x0, float y0,
5715 float x1, float y0,
5716 float x1, float y1,
5717 float x0, float y1)
5718 ) :: state.rects
5719 | None -> state.rects
5721 | _ -> state.rects
5723 showrects rects;
5724 showsel ();
5725 state.uioh#display;
5726 begin match state.mstate with
5727 | Mzoomrect ((x0, y0), (x1, y1)) ->
5728 Gl.enable `blend;
5729 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5730 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5731 GlDraw.rect (float x0, float y0)
5732 (float x1, float y1);
5733 Gl.disable `blend;
5734 | _ -> ()
5735 end;
5736 enttext ();
5737 scrollindicator ();
5738 Wsi.swapb ();
5741 let zoomrect x y x1 y1 =
5742 let x0 = min x x1
5743 and x1 = max x x1
5744 and y0 = min y y1 in
5745 gotoy (state.y + y0);
5746 state.anchor <- getanchor ();
5747 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5748 let margin =
5749 if state.w < state.winw - state.scrollw
5750 then (state.winw - state.scrollw - state.w) / 2
5751 else 0
5753 state.x <- (state.x + margin) - x0;
5754 setzoom zoom;
5755 Wsi.setcursor Wsi.CURSOR_INHERIT;
5756 state.mstate <- Mnone;
5759 let scrollx x =
5760 let winw = state.winw - state.scrollw - 1 in
5761 let s = float x /. float winw in
5762 let destx = truncate (float (state.w + winw) *. s) in
5763 state.x <- winw - destx;
5764 gotoy_and_clear_text state.y;
5765 state.mstate <- Mscrollx;
5768 let scrolly y =
5769 let s = float y /. float state.winh in
5770 let desty = truncate (float (state.maxy - state.winh) *. s) in
5771 gotoy_and_clear_text desty;
5772 state.mstate <- Mscrolly;
5775 let viewmouse button down x y mask =
5776 match button with
5777 | n when (n == 4 || n == 5) && not down ->
5778 if Wsi.withctrl mask
5779 then (
5780 match state.mstate with
5781 | Mzoom (oldn, i) ->
5782 if oldn = n
5783 then (
5784 if i = 2
5785 then
5786 let incr =
5787 match n with
5788 | 5 ->
5789 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5790 | _ ->
5791 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5793 let zoom = conf.zoom -. incr in
5794 setzoom zoom;
5795 state.mstate <- Mzoom (n, 0);
5796 else
5797 state.mstate <- Mzoom (n, i+1);
5799 else state.mstate <- Mzoom (n, 0)
5801 | _ -> state.mstate <- Mzoom (n, 0)
5803 else (
5804 match state.autoscroll with
5805 | Some step -> setautoscrollspeed step (n=4)
5806 | None ->
5807 if conf.wheelbypage
5808 then (
5809 if n = 4
5810 then prevpage ()
5811 else nextpage ()
5813 else
5814 let incr =
5815 if n = 4
5816 then -conf.scrollstep
5817 else conf.scrollstep
5819 let incr = incr * 2 in
5820 let y = clamp incr in
5821 gotoy_and_clear_text y
5824 | n when (n = 6 || n = 7) && not down && canpan () ->
5825 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5826 gotoy_and_clear_text state.y
5828 | 1 when Wsi.withshift mask ->
5829 state.mstate <- Mnone;
5830 if not down then (
5831 match unproject x y with
5832 | Some (pageno, ux, uy) ->
5833 let cmd = Printf.sprintf
5834 "%s %s %d %d %d"
5835 conf.stcmd state.path pageno ux uy
5837 popen cmd []
5838 | None -> ()
5841 | 1 when Wsi.withctrl mask ->
5842 if down
5843 then (
5844 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5845 state.mstate <- Mpan (x, y)
5847 else
5848 state.mstate <- Mnone
5850 | 3 ->
5851 if down
5852 then (
5853 Wsi.setcursor Wsi.CURSOR_CYCLE;
5854 let p = (x, y) in
5855 state.mstate <- Mzoomrect (p, p)
5857 else (
5858 match state.mstate with
5859 | Mzoomrect ((x0, y0), _) ->
5860 if abs (x-x0) > 10 && abs (y - y0) > 10
5861 then zoomrect x0 y0 x y
5862 else (
5863 state.mstate <- Mnone;
5864 Wsi.setcursor Wsi.CURSOR_INHERIT;
5865 G.postRedisplay "kill accidental zoom rect";
5867 | _ ->
5868 Wsi.setcursor Wsi.CURSOR_INHERIT;
5869 state.mstate <- Mnone
5872 | 1 when x > state.winw - state.scrollw ->
5873 if down
5874 then
5875 let _, position, sh = state.uioh#scrollph in
5876 if y > truncate position && y < truncate (position +. sh)
5877 then state.mstate <- Mscrolly
5878 else scrolly y
5879 else
5880 state.mstate <- Mnone
5882 | 1 when y > state.winh - state.hscrollh ->
5883 if down
5884 then
5885 let _, position, sw = state.uioh#scrollpw in
5886 if x > truncate position && x < truncate (position +. sw)
5887 then state.mstate <- Mscrollx
5888 else scrollx x
5889 else
5890 state.mstate <- Mnone
5892 | 1 ->
5893 let dest = if down then getunder x y else Unone in
5894 begin match dest with
5895 | Ulinkgoto _
5896 | Ulinkuri _
5897 | Uremote _
5898 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5899 gotounder dest
5901 | Unone when down ->
5902 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5903 state.mstate <- Mpan (x, y);
5905 | Unone | Utext _ ->
5906 if down
5907 then (
5908 if conf.angle mod 360 = 0
5909 then (
5910 state.mstate <- Msel ((x, y), (x, y));
5911 G.postRedisplay "mouse select";
5914 else (
5915 match state.mstate with
5916 | Mnone -> ()
5918 | Mzoom _ | Mscrollx | Mscrolly ->
5919 state.mstate <- Mnone
5921 | Mzoomrect ((x0, y0), _) ->
5922 zoomrect x0 y0 x y
5924 | Mpan _ ->
5925 Wsi.setcursor Wsi.CURSOR_INHERIT;
5926 state.mstate <- Mnone
5928 | Msel ((x0, y0), (x1, y1)) ->
5929 let rec loop = function
5930 | [] -> ()
5931 | l :: rest ->
5932 let inside =
5933 let a0 = l.pagedispy in
5934 let a1 = a0 + l.pagevh in
5935 let b0 = l.pagedispx in
5936 let b1 = b0 + l.pagevw in
5937 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5938 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5940 if inside
5941 then
5942 match getopaque l.pageno with
5943 | Some opaque ->
5944 begin
5945 match Ne.pipe () with
5946 | Ne.Exn exn ->
5947 showtext '!'
5948 (Printf.sprintf
5949 "can not create sel pipe: %s"
5950 (exntos exn));
5951 | Ne.Res (r, w) ->
5952 let doclose what fd =
5953 Ne.clo fd (fun msg ->
5954 dolog "%s close failed: %s" what msg)
5957 popen conf.selcmd [r, 0; w, -1];
5958 copysel w opaque;
5959 doclose "pipe/r" r;
5960 G.postRedisplay "copysel";
5961 with exn ->
5962 dolog "can not execute %S: %s"
5963 conf.selcmd (exntos exn);
5964 doclose "pipe/r" r;
5965 doclose "pipe/w" w;
5967 | None -> ()
5968 else loop rest
5970 loop state.layout;
5971 Wsi.setcursor Wsi.CURSOR_INHERIT;
5972 state.mstate <- Mnone;
5976 | _ -> ()
5979 let birdseyemouse button down x y mask
5980 (conf, leftx, _, hooverpageno, anchor) =
5981 match button with
5982 | 1 when down ->
5983 let rec loop = function
5984 | [] -> ()
5985 | l :: rest ->
5986 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5987 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5988 then (
5989 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5991 else loop rest
5993 loop state.layout
5994 | 3 -> ()
5995 | _ -> viewmouse button down x y mask
5998 let mouse button down x y mask =
5999 state.uioh <- state.uioh#button button down x y mask;
6002 let motion ~x ~y =
6003 state.uioh <- state.uioh#motion x y
6006 let pmotion ~x ~y =
6007 state.uioh <- state.uioh#pmotion x y;
6010 let uioh = object
6011 method display = ()
6013 method key key mask =
6014 begin match state.mode with
6015 | Textentry textentry -> textentrykeyboard key mask textentry
6016 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6017 | View -> viewkeyboard key mask
6018 | LinkNav linknav -> linknavkeyboard key mask linknav
6019 end;
6020 state.uioh
6022 method button button bstate x y mask =
6023 begin match state.mode with
6024 | LinkNav _
6025 | View -> viewmouse button bstate x y mask
6026 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6027 | Textentry _ -> ()
6028 end;
6029 state.uioh
6031 method motion x y =
6032 begin match state.mode with
6033 | Textentry _ -> ()
6034 | View | Birdseye _ | LinkNav _ ->
6035 match state.mstate with
6036 | Mzoom _ | Mnone -> ()
6038 | Mpan (x0, y0) ->
6039 let dx = x - x0
6040 and dy = y0 - y in
6041 state.mstate <- Mpan (x, y);
6042 if canpan ()
6043 then state.x <- state.x + dx;
6044 let y = clamp dy in
6045 gotoy_and_clear_text y
6047 | Msel (a, _) ->
6048 state.mstate <- Msel (a, (x, y));
6049 G.postRedisplay "motion select";
6051 | Mscrolly ->
6052 let y = min state.winh (max 0 y) in
6053 scrolly y
6055 | Mscrollx ->
6056 let x = min state.winw (max 0 x) in
6057 scrollx x
6059 | Mzoomrect (p0, _) ->
6060 state.mstate <- Mzoomrect (p0, (x, y));
6061 G.postRedisplay "motion zoomrect";
6062 end;
6063 state.uioh
6065 method pmotion x y =
6066 begin match state.mode with
6067 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6068 let rec loop = function
6069 | [] ->
6070 if hooverpageno != -1
6071 then (
6072 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6073 G.postRedisplay "pmotion birdseye no hoover";
6075 | l :: rest ->
6076 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6077 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6078 then (
6079 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6080 G.postRedisplay "pmotion birdseye hoover";
6082 else loop rest
6084 loop state.layout
6086 | Textentry _ -> ()
6088 | LinkNav _
6089 | View ->
6090 match state.mstate with
6091 | Mnone -> updateunder x y
6092 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6094 end;
6095 state.uioh
6097 method infochanged _ = ()
6099 method scrollph =
6100 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6101 let p, h = scrollph state.y maxy in
6102 state.scrollw, p, h
6104 method scrollpw =
6105 let winw = state.winw - state.scrollw - 1 in
6106 let fwinw = float winw in
6107 let sw =
6108 let sw = fwinw /. float state.w in
6109 let sw = fwinw *. sw in
6110 max sw (float conf.scrollh)
6112 let position, sw =
6113 let f = state.w+winw in
6114 let r = float (winw-state.x) /. float f in
6115 let p = fwinw *. r in
6116 p-.sw/.2., sw
6118 let sw =
6119 if position +. sw > fwinw
6120 then fwinw -. position
6121 else sw
6123 state.hscrollh, position, sw
6125 method modehash =
6126 let modename =
6127 match state.mode with
6128 | LinkNav _ -> "links"
6129 | Textentry _ -> "textentry"
6130 | Birdseye _ -> "birdseye"
6131 | View -> "view"
6133 findkeyhash conf modename
6134 end;;
6136 module Config =
6137 struct
6138 open Parser
6140 let fontpath = ref "";;
6142 module KeyMap =
6143 Map.Make (struct type t = (int * int) let compare = compare end);;
6145 let unent s =
6146 let l = String.length s in
6147 let b = Buffer.create l in
6148 unent b s 0 l;
6149 Buffer.contents b;
6152 let home =
6153 try Sys.getenv "HOME"
6154 with exn ->
6155 prerr_endline
6156 ("Can not determine home directory location: " ^ exntos exn);
6160 let modifier_of_string = function
6161 | "alt" -> Wsi.altmask
6162 | "shift" -> Wsi.shiftmask
6163 | "ctrl" | "control" -> Wsi.ctrlmask
6164 | "meta" -> Wsi.metamask
6165 | _ -> 0
6168 let key_of_string =
6169 let r = Str.regexp "-" in
6170 fun s ->
6171 let elems = Str.full_split r s in
6172 let f n k m =
6173 let g s =
6174 let m1 = modifier_of_string s in
6175 if m1 = 0
6176 then (Wsi.namekey s, m)
6177 else (k, m lor m1)
6178 in function
6179 | Str.Delim s when n land 1 = 0 -> g s
6180 | Str.Text s -> g s
6181 | Str.Delim _ -> (k, m)
6183 let rec loop n k m = function
6184 | [] -> (k, m)
6185 | x :: xs ->
6186 let k, m = f n k m x in
6187 loop (n+1) k m xs
6189 loop 0 0 0 elems
6192 let keys_of_string =
6193 let r = Str.regexp "[ \t]" in
6194 fun s ->
6195 let elems = Str.split r s in
6196 List.map key_of_string elems
6199 let copykeyhashes c =
6200 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6203 let config_of c attrs =
6204 let apply c k v =
6206 match k with
6207 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6208 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6209 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6210 | "preload" -> { c with preload = bool_of_string v }
6211 | "page-bias" -> { c with pagebias = int_of_string v }
6212 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6213 | "horizontal-scroll-step" ->
6214 { c with hscrollstep = max (int_of_string v) 1 }
6215 | "auto-scroll-step" ->
6216 { c with autoscrollstep = max 0 (int_of_string v) }
6217 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6218 | "crop-hack" -> { c with crophack = bool_of_string v }
6219 | "throttle" ->
6220 let mw =
6221 match String.lowercase v with
6222 | "true" -> Some infinity
6223 | "false" -> None
6224 | f -> Some (float_of_string f)
6226 { c with maxwait = mw}
6227 | "highlight-links" -> { c with hlinks = bool_of_string v }
6228 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6229 | "vertical-margin" ->
6230 { c with interpagespace = max 0 (int_of_string v) }
6231 | "zoom" ->
6232 let zoom = float_of_string v /. 100. in
6233 let zoom = max zoom 0.0 in
6234 { c with zoom = zoom }
6235 | "presentation" -> { c with presentation = bool_of_string v }
6236 | "rotation-angle" -> { c with angle = int_of_string v }
6237 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6238 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6239 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6240 | "proportional-display" -> { c with proportional = bool_of_string v }
6241 | "pixmap-cache-size" ->
6242 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6243 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6244 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6245 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6246 | "persistent-location" -> { c with jumpback = bool_of_string v }
6247 | "background-color" -> { c with bgcolor = color_of_string v }
6248 | "scrollbar-in-presentation" ->
6249 { c with scrollbarinpm = bool_of_string v }
6250 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6251 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6252 | "mupdf-store-size" ->
6253 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6254 | "checkers" -> { c with checkers = bool_of_string v }
6255 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6256 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6257 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6258 | "uri-launcher" -> { c with urilauncher = unent v }
6259 | "path-launcher" -> { c with pathlauncher = unent v }
6260 | "color-space" -> { c with colorspace = colorspace_of_string v }
6261 | "invert-colors" -> { c with invert = bool_of_string v }
6262 | "brightness" -> { c with colorscale = float_of_string v }
6263 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6264 | "ghyllscroll" ->
6265 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6266 | "columns" ->
6267 let (n, _, _) as nab = multicolumns_of_string v in
6268 if n < 0
6269 then { c with columns = Csplit (-n, [||]) }
6270 else { c with columns = Cmulti (nab, [||]) }
6271 | "birds-eye-columns" ->
6272 { c with beyecolumns = Some (max (int_of_string v) 2) }
6273 | "selection-command" -> { c with selcmd = unent v }
6274 | "synctex-command" -> { c with stcmd = unent v }
6275 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6276 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6277 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6278 | "use-pbo" -> { c with usepbo = bool_of_string v }
6279 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6280 | _ -> c
6281 with exn ->
6282 prerr_endline ("Error processing attribute (`" ^
6283 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6286 let rec fold c = function
6287 | [] -> c
6288 | (k, v) :: rest ->
6289 let c = apply c k v in
6290 fold c rest
6292 fold { c with keyhashes = copykeyhashes c } attrs;
6295 let fromstring f pos n v d =
6296 try f v
6297 with exn ->
6298 dolog "Error processing attribute (%S=%S) at %d\n%s"
6299 n v pos (exntos exn)
6304 let bookmark_of attrs =
6305 let rec fold title page rely visy = function
6306 | ("title", v) :: rest -> fold v page rely visy rest
6307 | ("page", v) :: rest -> fold title v rely visy rest
6308 | ("rely", v) :: rest -> fold title page v visy rest
6309 | ("visy", v) :: rest -> fold title page rely v rest
6310 | _ :: rest -> fold title page rely visy rest
6311 | [] -> title, page, rely, visy
6313 fold "invalid" "0" "0" "0" attrs
6316 let doc_of attrs =
6317 let rec fold path page rely pan visy = function
6318 | ("path", v) :: rest -> fold v page rely pan visy rest
6319 | ("page", v) :: rest -> fold path v rely pan visy rest
6320 | ("rely", v) :: rest -> fold path page v pan visy rest
6321 | ("pan", v) :: rest -> fold path page rely v visy rest
6322 | ("visy", v) :: rest -> fold path page rely pan v rest
6323 | _ :: rest -> fold path page rely pan visy rest
6324 | [] -> path, page, rely, pan, visy
6326 fold "" "0" "0" "0" "0" attrs
6329 let map_of attrs =
6330 let rec fold rs ls = function
6331 | ("out", v) :: rest -> fold v ls rest
6332 | ("in", v) :: rest -> fold rs v rest
6333 | _ :: rest -> fold ls rs rest
6334 | [] -> ls, rs
6336 fold "" "" attrs
6339 let setconf dst src =
6340 dst.scrollbw <- src.scrollbw;
6341 dst.scrollh <- src.scrollh;
6342 dst.icase <- src.icase;
6343 dst.preload <- src.preload;
6344 dst.pagebias <- src.pagebias;
6345 dst.verbose <- src.verbose;
6346 dst.scrollstep <- src.scrollstep;
6347 dst.maxhfit <- src.maxhfit;
6348 dst.crophack <- src.crophack;
6349 dst.autoscrollstep <- src.autoscrollstep;
6350 dst.maxwait <- src.maxwait;
6351 dst.hlinks <- src.hlinks;
6352 dst.underinfo <- src.underinfo;
6353 dst.interpagespace <- src.interpagespace;
6354 dst.zoom <- src.zoom;
6355 dst.presentation <- src.presentation;
6356 dst.angle <- src.angle;
6357 dst.cwinw <- src.cwinw;
6358 dst.cwinh <- src.cwinh;
6359 dst.savebmarks <- src.savebmarks;
6360 dst.memlimit <- src.memlimit;
6361 dst.proportional <- src.proportional;
6362 dst.texcount <- src.texcount;
6363 dst.sliceheight <- src.sliceheight;
6364 dst.thumbw <- src.thumbw;
6365 dst.jumpback <- src.jumpback;
6366 dst.bgcolor <- src.bgcolor;
6367 dst.scrollbarinpm <- src.scrollbarinpm;
6368 dst.tilew <- src.tilew;
6369 dst.tileh <- src.tileh;
6370 dst.mustoresize <- src.mustoresize;
6371 dst.checkers <- src.checkers;
6372 dst.aalevel <- src.aalevel;
6373 dst.trimmargins <- src.trimmargins;
6374 dst.trimfuzz <- src.trimfuzz;
6375 dst.urilauncher <- src.urilauncher;
6376 dst.colorspace <- src.colorspace;
6377 dst.invert <- src.invert;
6378 dst.colorscale <- src.colorscale;
6379 dst.redirectstderr <- src.redirectstderr;
6380 dst.ghyllscroll <- src.ghyllscroll;
6381 dst.columns <- src.columns;
6382 dst.beyecolumns <- src.beyecolumns;
6383 dst.selcmd <- src.selcmd;
6384 dst.updatecurs <- src.updatecurs;
6385 dst.pathlauncher <- src.pathlauncher;
6386 dst.keyhashes <- copykeyhashes src;
6387 dst.hfsize <- src.hfsize;
6388 dst.hscrollstep <- src.hscrollstep;
6389 dst.pgscale <- src.pgscale;
6390 dst.usepbo <- src.usepbo;
6391 dst.wheelbypage <- src.wheelbypage;
6392 dst.stcmd <- src.stcmd;
6395 let get s =
6396 let h = Hashtbl.create 10 in
6397 let dc = { defconf with angle = defconf.angle } in
6398 let rec toplevel v t spos _ =
6399 match t with
6400 | Vdata | Vcdata | Vend -> v
6401 | Vopen ("llppconfig", _, closed) ->
6402 if closed
6403 then v
6404 else { v with f = llppconfig }
6405 | Vopen _ ->
6406 error "unexpected subelement at top level" s spos
6407 | Vclose _ -> error "unexpected close at top level" s spos
6409 and llppconfig v t spos _ =
6410 match t with
6411 | Vdata | Vcdata -> v
6412 | Vend -> error "unexpected end of input in llppconfig" s spos
6413 | Vopen ("defaults", attrs, closed) ->
6414 let c = config_of dc attrs in
6415 setconf dc c;
6416 if closed
6417 then v
6418 else { v with f = defaults }
6420 | Vopen ("ui-font", attrs, closed) ->
6421 let rec getsize size = function
6422 | [] -> size
6423 | ("size", v) :: rest ->
6424 let size =
6425 fromstring int_of_string spos "size" v fstate.fontsize in
6426 getsize size rest
6427 | l -> getsize size l
6429 fstate.fontsize <- getsize fstate.fontsize attrs;
6430 if closed
6431 then v
6432 else { v with f = uifont (Buffer.create 10) }
6434 | Vopen ("doc", attrs, closed) ->
6435 let pathent, spage, srely, span, svisy = doc_of attrs in
6436 let path = unent pathent
6437 and pageno = fromstring int_of_string spos "page" spage 0
6438 and rely = fromstring float_of_string spos "rely" srely 0.0
6439 and pan = fromstring int_of_string spos "pan" span 0
6440 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6441 let c = config_of dc attrs in
6442 let anchor = (pageno, rely, visy) in
6443 if closed
6444 then (Hashtbl.add h path (c, [], pan, anchor); v)
6445 else { v with f = doc path pan anchor c [] }
6447 | Vopen _ ->
6448 error "unexpected subelement in llppconfig" s spos
6450 | Vclose "llppconfig" -> { v with f = toplevel }
6451 | Vclose _ -> error "unexpected close in llppconfig" s spos
6453 and defaults v t spos _ =
6454 match t with
6455 | Vdata | Vcdata -> v
6456 | Vend -> error "unexpected end of input in defaults" s spos
6457 | Vopen ("keymap", attrs, closed) ->
6458 let modename =
6459 try List.assoc "mode" attrs
6460 with Not_found -> "global" in
6461 if closed
6462 then v
6463 else
6464 let ret keymap =
6465 let h = findkeyhash dc modename in
6466 KeyMap.iter (Hashtbl.replace h) keymap;
6467 defaults
6469 { v with f = pkeymap ret KeyMap.empty }
6471 | Vopen (_, _, _) ->
6472 error "unexpected subelement in defaults" s spos
6474 | Vclose "defaults" ->
6475 { v with f = llppconfig }
6477 | Vclose _ -> error "unexpected close in defaults" s spos
6479 and uifont b v t spos epos =
6480 match t with
6481 | Vdata | Vcdata ->
6482 Buffer.add_substring b s spos (epos - spos);
6484 | Vopen (_, _, _) ->
6485 error "unexpected subelement in ui-font" s spos
6486 | Vclose "ui-font" ->
6487 if String.length !fontpath = 0
6488 then fontpath := Buffer.contents b;
6489 { v with f = llppconfig }
6490 | Vclose _ -> error "unexpected close in ui-font" s spos
6491 | Vend -> error "unexpected end of input in ui-font" s spos
6493 and doc path pan anchor c bookmarks v t spos _ =
6494 match t with
6495 | Vdata | Vcdata -> v
6496 | Vend -> error "unexpected end of input in doc" s spos
6497 | Vopen ("bookmarks", _, closed) ->
6498 if closed
6499 then v
6500 else { v with f = pbookmarks path pan anchor c bookmarks }
6502 | Vopen ("keymap", attrs, closed) ->
6503 let modename =
6504 try List.assoc "mode" attrs
6505 with Not_found -> "global"
6507 if closed
6508 then v
6509 else
6510 let ret keymap =
6511 let h = findkeyhash c modename in
6512 KeyMap.iter (Hashtbl.replace h) keymap;
6513 doc path pan anchor c bookmarks
6515 { v with f = pkeymap ret KeyMap.empty }
6517 | Vopen (_, _, _) ->
6518 error "unexpected subelement in doc" s spos
6520 | Vclose "doc" ->
6521 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6522 { v with f = llppconfig }
6524 | Vclose _ -> error "unexpected close in doc" s spos
6526 and pkeymap ret keymap v t spos _ =
6527 match t with
6528 | Vdata | Vcdata -> v
6529 | Vend -> error "unexpected end of input in keymap" s spos
6530 | Vopen ("map", attrs, closed) ->
6531 let r, l = map_of attrs in
6532 let kss = fromstring keys_of_string spos "in" r [] in
6533 let lss = fromstring keys_of_string spos "out" l [] in
6534 let keymap =
6535 match kss with
6536 | [] -> keymap
6537 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6538 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6540 if closed
6541 then { v with f = pkeymap ret keymap }
6542 else
6543 let f () = v in
6544 { v with f = skip "map" f }
6546 | Vopen _ ->
6547 error "unexpected subelement in keymap" s spos
6549 | Vclose "keymap" ->
6550 { v with f = ret keymap }
6552 | Vclose _ -> error "unexpected close in keymap" s spos
6554 and pbookmarks path pan anchor c bookmarks v t spos _ =
6555 match t with
6556 | Vdata | Vcdata -> v
6557 | Vend -> error "unexpected end of input in bookmarks" s spos
6558 | Vopen ("item", attrs, closed) ->
6559 let titleent, spage, srely, svisy = bookmark_of attrs in
6560 let page = fromstring int_of_string spos "page" spage 0
6561 and rely = fromstring float_of_string spos "rely" srely 0.0
6562 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6563 let bookmarks =
6564 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6566 if closed
6567 then { v with f = pbookmarks path pan anchor c bookmarks }
6568 else
6569 let f () = v in
6570 { v with f = skip "item" f }
6572 | Vopen _ ->
6573 error "unexpected subelement in bookmarks" s spos
6575 | Vclose "bookmarks" ->
6576 { v with f = doc path pan anchor c bookmarks }
6578 | Vclose _ -> error "unexpected close in bookmarks" s spos
6580 and skip tag f v t spos _ =
6581 match t with
6582 | Vdata | Vcdata -> v
6583 | Vend ->
6584 error ("unexpected end of input in skipped " ^ tag) s spos
6585 | Vopen (tag', _, closed) ->
6586 if closed
6587 then v
6588 else
6589 let f' () = { v with f = skip tag f } in
6590 { v with f = skip tag' f' }
6591 | Vclose ctag ->
6592 if tag = ctag
6593 then f ()
6594 else error ("unexpected close in skipped " ^ tag) s spos
6597 parse { f = toplevel; accu = () } s;
6598 h, dc;
6601 let do_load f ic =
6603 let len = in_channel_length ic in
6604 let s = String.create len in
6605 really_input ic s 0 len;
6606 f s;
6607 with
6608 | Parse_error (msg, s, pos) ->
6609 let subs = subs s pos in
6610 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6611 failwith ("parse error: " ^ s)
6613 | exn ->
6614 failwith ("config load error: " ^ exntos exn)
6617 let defconfpath =
6618 let dir =
6620 let dir = Filename.concat home ".config" in
6621 if Sys.is_directory dir then dir else home
6622 with _ -> home
6624 Filename.concat dir "llpp.conf"
6627 let confpath = ref defconfpath;;
6629 let load1 f =
6630 if Sys.file_exists !confpath
6631 then
6632 match
6633 (try Some (open_in_bin !confpath)
6634 with exn ->
6635 prerr_endline
6636 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6637 exntos exn);
6638 None
6640 with
6641 | Some ic ->
6642 let success =
6644 f (do_load get ic)
6645 with exn ->
6646 prerr_endline
6647 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6648 exntos exn);
6649 false
6651 close_in ic;
6652 success
6654 | None -> false
6655 else
6656 f (Hashtbl.create 0, defconf)
6659 let load () =
6660 let f (h, dc) =
6661 let pc, pb, px, pa =
6663 Hashtbl.find h (Filename.basename state.path)
6664 with Not_found -> dc, [], 0, emptyanchor
6666 setconf defconf dc;
6667 setconf conf pc;
6668 state.bookmarks <- pb;
6669 state.x <- px;
6670 state.scrollw <- conf.scrollbw;
6671 if conf.jumpback
6672 then state.anchor <- pa;
6673 cbput state.hists.nav pa;
6674 true
6676 load1 f
6679 let add_attrs bb always dc c =
6680 let ob s a b =
6681 if always || a != b
6682 then Printf.bprintf bb "\n %s='%b'" s a
6683 and oi s a b =
6684 if always || a != b
6685 then Printf.bprintf bb "\n %s='%d'" s a
6686 and oI s a b =
6687 if always || a != b
6688 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6689 and oz s a b =
6690 if always || a <> b
6691 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6692 and oF s a b =
6693 if always || a <> b
6694 then Printf.bprintf bb "\n %s='%f'" s a
6695 and oc s a b =
6696 if always || a <> b
6697 then
6698 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6699 and oC s a b =
6700 if always || a <> b
6701 then
6702 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6703 and oR s a b =
6704 if always || a <> b
6705 then
6706 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6707 and os s a b =
6708 if always || a <> b
6709 then
6710 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6711 and og s a b =
6712 if always || a <> b
6713 then
6714 match a with
6715 | None -> ()
6716 | Some (_N, _A, _B) ->
6717 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6718 and oW s a b =
6719 if always || a <> b
6720 then
6721 let v =
6722 match a with
6723 | None -> "false"
6724 | Some f ->
6725 if f = infinity
6726 then "true"
6727 else string_of_float f
6729 Printf.bprintf bb "\n %s='%s'" s v
6730 and oco s a b =
6731 if always || a <> b
6732 then
6733 match a with
6734 | Cmulti ((n, a, b), _) when n > 1 ->
6735 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6736 | Csplit (n, _) when n > 1 ->
6737 Printf.bprintf bb "\n %s='%d'" s ~-n
6738 | _ -> ()
6739 and obeco s a b =
6740 if always || a <> b
6741 then
6742 match a with
6743 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6744 | _ -> ()
6746 oi "width" c.cwinw dc.cwinw;
6747 oi "height" c.cwinh dc.cwinh;
6748 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6749 oi "scroll-handle-height" c.scrollh dc.scrollh;
6750 ob "case-insensitive-search" c.icase dc.icase;
6751 ob "preload" c.preload dc.preload;
6752 oi "page-bias" c.pagebias dc.pagebias;
6753 oi "scroll-step" c.scrollstep dc.scrollstep;
6754 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6755 ob "max-height-fit" c.maxhfit dc.maxhfit;
6756 ob "crop-hack" c.crophack dc.crophack;
6757 oW "throttle" c.maxwait dc.maxwait;
6758 ob "highlight-links" c.hlinks dc.hlinks;
6759 ob "under-cursor-info" c.underinfo dc.underinfo;
6760 oi "vertical-margin" c.interpagespace dc.interpagespace;
6761 oz "zoom" c.zoom dc.zoom;
6762 ob "presentation" c.presentation dc.presentation;
6763 oi "rotation-angle" c.angle dc.angle;
6764 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6765 ob "proportional-display" c.proportional dc.proportional;
6766 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6767 oi "tex-count" c.texcount dc.texcount;
6768 oi "slice-height" c.sliceheight dc.sliceheight;
6769 oi "thumbnail-width" c.thumbw dc.thumbw;
6770 ob "persistent-location" c.jumpback dc.jumpback;
6771 oc "background-color" c.bgcolor dc.bgcolor;
6772 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6773 oi "tile-width" c.tilew dc.tilew;
6774 oi "tile-height" c.tileh dc.tileh;
6775 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6776 ob "checkers" c.checkers dc.checkers;
6777 oi "aalevel" c.aalevel dc.aalevel;
6778 ob "trim-margins" c.trimmargins dc.trimmargins;
6779 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6780 os "uri-launcher" c.urilauncher dc.urilauncher;
6781 os "path-launcher" c.pathlauncher dc.pathlauncher;
6782 oC "color-space" c.colorspace dc.colorspace;
6783 ob "invert-colors" c.invert dc.invert;
6784 oF "brightness" c.colorscale dc.colorscale;
6785 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6786 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6787 oco "columns" c.columns dc.columns;
6788 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6789 os "selection-command" c.selcmd dc.selcmd;
6790 os "synctex-command" c.stcmd dc.stcmd;
6791 ob "update-cursor" c.updatecurs dc.updatecurs;
6792 oi "hint-font-size" c.hfsize dc.hfsize;
6793 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6794 oF "page-scroll-scale" c.pgscale dc.pgscale;
6795 ob "use-pbo" c.usepbo dc.usepbo;
6796 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6799 let keymapsbuf always dc c =
6800 let bb = Buffer.create 16 in
6801 let rec loop = function
6802 | [] -> ()
6803 | (modename, h) :: rest ->
6804 let dh = findkeyhash dc modename in
6805 if always || h <> dh
6806 then (
6807 if Hashtbl.length h > 0
6808 then (
6809 if Buffer.length bb > 0
6810 then Buffer.add_char bb '\n';
6811 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6812 Hashtbl.iter (fun i o ->
6813 let isdifferent = always ||
6815 let dO = Hashtbl.find dh i in
6816 dO <> o
6817 with Not_found -> true
6819 if isdifferent
6820 then
6821 let addkm (k, m) =
6822 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6823 if Wsi.withalt m then Buffer.add_string bb "alt-";
6824 if Wsi.withshift m then Buffer.add_string bb "shift-";
6825 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6826 Buffer.add_string bb (Wsi.keyname k);
6828 let addkms l =
6829 let rec loop = function
6830 | [] -> ()
6831 | km :: [] -> addkm km
6832 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6834 loop l
6836 Buffer.add_string bb "<map in='";
6837 addkm i;
6838 match o with
6839 | KMinsrt km ->
6840 Buffer.add_string bb "' out='";
6841 addkm km;
6842 Buffer.add_string bb "'/>\n"
6844 | KMinsrl kms ->
6845 Buffer.add_string bb "' out='";
6846 addkms kms;
6847 Buffer.add_string bb "'/>\n"
6849 | KMmulti (ins, kms) ->
6850 Buffer.add_char bb ' ';
6851 addkms ins;
6852 Buffer.add_string bb "' out='";
6853 addkms kms;
6854 Buffer.add_string bb "'/>\n"
6855 ) h;
6856 Buffer.add_string bb "</keymap>";
6859 loop rest
6861 loop c.keyhashes;
6865 let save () =
6866 let uifontsize = fstate.fontsize in
6867 let bb = Buffer.create 32768 in
6868 let w, h =
6869 List.fold_left
6870 (fun (w, h) ws ->
6871 match ws with
6872 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6873 | Wsi.MaxVert -> (w, conf.cwinh)
6874 | Wsi.MaxHorz -> (conf.cwinw, h)
6876 (state.winw, state.winh) state.winstate
6878 conf.cwinw <- w;
6879 conf.cwinh <- h;
6880 let f (h, dc) =
6881 let dc = if conf.bedefault then conf else dc in
6882 Buffer.add_string bb "<llppconfig>\n";
6884 if String.length !fontpath > 0
6885 then
6886 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6887 uifontsize
6888 !fontpath
6889 else (
6890 if uifontsize <> 14
6891 then
6892 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6895 Buffer.add_string bb "<defaults ";
6896 add_attrs bb true dc dc;
6897 let kb = keymapsbuf true dc dc in
6898 if Buffer.length kb > 0
6899 then (
6900 Buffer.add_string bb ">\n";
6901 Buffer.add_buffer bb kb;
6902 Buffer.add_string bb "\n</defaults>\n";
6904 else Buffer.add_string bb "/>\n";
6906 let adddoc path pan anchor c bookmarks =
6907 if bookmarks == [] && c = dc && anchor = emptyanchor
6908 then ()
6909 else (
6910 Printf.bprintf bb "<doc path='%s'"
6911 (enent path 0 (String.length path));
6913 if anchor <> emptyanchor
6914 then (
6915 let n, rely, visy = anchor in
6916 Printf.bprintf bb " page='%d'" n;
6917 if rely > 1e-6
6918 then
6919 Printf.bprintf bb " rely='%f'" rely
6921 if abs_float visy > 1e-6
6922 then
6923 Printf.bprintf bb " visy='%f'" visy
6927 if pan != 0
6928 then Printf.bprintf bb " pan='%d'" pan;
6930 add_attrs bb false dc c;
6931 let kb = keymapsbuf false dc c in
6933 begin match bookmarks with
6934 | [] ->
6935 if Buffer.length kb > 0
6936 then (
6937 Buffer.add_string bb ">\n";
6938 Buffer.add_buffer bb kb;
6939 Buffer.add_string bb "\n</doc>\n";
6941 else Buffer.add_string bb "/>\n"
6942 | _ ->
6943 Buffer.add_string bb ">\n<bookmarks>\n";
6944 List.iter (fun (title, _level, (page, rely, visy)) ->
6945 Printf.bprintf bb
6946 "<item title='%s' page='%d'"
6947 (enent title 0 (String.length title))
6948 page
6950 if rely > 1e-6
6951 then
6952 Printf.bprintf bb " rely='%f'" rely
6954 if abs_float visy > 1e-6
6955 then
6956 Printf.bprintf bb " visy='%f'" visy
6958 Buffer.add_string bb "/>\n";
6959 ) bookmarks;
6960 Buffer.add_string bb "</bookmarks>";
6961 if Buffer.length kb > 0
6962 then (
6963 Buffer.add_string bb "\n";
6964 Buffer.add_buffer bb kb;
6966 Buffer.add_string bb "\n</doc>\n";
6967 end;
6971 let pan, conf =
6972 match state.mode with
6973 | Birdseye (c, pan, _, _, _) ->
6974 let beyecolumns =
6975 match conf.columns with
6976 | Cmulti ((c, _, _), _) -> Some c
6977 | Csingle _ -> None
6978 | Csplit _ -> None
6979 and columns =
6980 match c.columns with
6981 | Cmulti (c, _) -> Cmulti (c, [||])
6982 | Csingle _ -> Csingle [||]
6983 | Csplit _ -> failwith "quit from bird's eye while split"
6985 pan, { c with beyecolumns = beyecolumns; columns = columns }
6986 | _ -> state.x, conf
6988 let basename = Filename.basename state.path in
6989 adddoc basename pan (getanchor ())
6990 (let conf =
6991 let autoscrollstep =
6992 match state.autoscroll with
6993 | Some step -> step
6994 | None -> conf.autoscrollstep
6996 match state.mode with
6997 | Birdseye (bc, _, _, _, _) ->
6998 { conf with
6999 zoom = bc.zoom;
7000 presentation = bc.presentation;
7001 interpagespace = bc.interpagespace;
7002 maxwait = bc.maxwait;
7003 autoscrollstep = autoscrollstep }
7004 | _ -> { conf with autoscrollstep = autoscrollstep }
7005 in conf)
7006 (if conf.savebmarks then state.bookmarks else []);
7008 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7009 if basename <> path
7010 then adddoc path x anchor c bookmarks
7011 ) h;
7012 Buffer.add_string bb "</llppconfig>\n";
7013 true;
7015 if load1 f && Buffer.length bb > 0
7016 then
7018 let tmp = !confpath ^ ".tmp" in
7019 let oc = open_out_bin tmp in
7020 Buffer.output_buffer oc bb;
7021 close_out oc;
7022 Unix.rename tmp !confpath;
7023 with exn ->
7024 prerr_endline
7025 ("error while saving configuration: " ^ exntos exn)
7027 end;;
7029 let adderrmsg src msg =
7030 Buffer.add_string state.errmsgs msg;
7031 state.newerrmsgs <- true;
7032 G.postRedisplay src
7035 let adderrfmt src fmt =
7036 Format.kprintf (fun s -> adderrmsg src s) fmt;
7039 let ract cmds =
7040 let cl = splitatspace cmds in
7041 let scan s fmt f =
7042 try Scanf.sscanf s fmt f
7043 with exn ->
7044 adderrfmt "remote exec"
7045 "error processing '%S': %s\n" cmds (exntos exn)
7047 match cl with
7048 | "reload" :: [] -> reload ()
7049 | "goto" :: args :: [] ->
7050 scan args "%u %f %f"
7051 (fun pageno x y ->
7052 let cmd, _ = state.geomcmds in
7053 if String.length cmd = 0
7054 then gotopagexy pageno x y
7055 else
7056 let f prevf () =
7057 gotopagexy pageno x y;
7058 prevf ()
7060 state.reprf <- f state.reprf
7062 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7063 | "rect" :: args :: [] ->
7064 scan args "%u %u %f %f %f %f"
7065 (fun pageno color x0 y0 x1 y1 ->
7066 onpagerect pageno (fun w h ->
7067 let _,w1,h1,_ = getpagedim pageno in
7068 let sw = float w1 /. w
7069 and sh = float h1 /. h in
7070 let x0s = x0 *. sw
7071 and x1s = x1 *. sw
7072 and y0s = y0 *. sh
7073 and y1s = y1 *. sh in
7074 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7075 debugrect rect;
7076 state.rects <- (pageno, color, rect) :: state.rects;
7077 G.postRedisplay "rect";
7080 | "activatewin" :: [] -> Wsi.activatewin ()
7081 | "quit" :: [] -> raise Quit
7082 | _ ->
7083 adderrfmt "remote command"
7084 "error processing remote command: %S\n" cmds;
7087 let remote =
7088 let scratch = String.create 80 in
7089 let buf = Buffer.create 80 in
7090 fun fd ->
7091 let rec tempfr () =
7092 try Some (Unix.read fd scratch 0 80)
7093 with
7094 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7095 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7096 | exn -> raise exn
7098 match tempfr () with
7099 | None -> Some fd
7100 | Some n ->
7101 if n = 0
7102 then (
7103 Unix.close fd;
7104 if Buffer.length buf > 0
7105 then (
7106 let s = Buffer.contents buf in
7107 Buffer.clear buf;
7108 ract s;
7110 None
7112 else
7113 let rec eat ppos =
7114 let nlpos =
7116 let pos = String.index_from scratch ppos '\n' in
7117 if pos >= n then -1 else pos
7118 with Not_found -> -1
7120 if nlpos >= 0
7121 then (
7122 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7123 let s = Buffer.contents buf in
7124 Buffer.clear buf;
7125 ract s;
7126 eat (nlpos+1);
7128 else (
7129 Buffer.add_substring buf scratch ppos (n-ppos);
7130 Some fd
7132 in eat 0
7135 let remoteopen path =
7136 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7137 with exn ->
7138 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7139 None
7142 let () =
7143 let trimcachepath = ref "" in
7144 let rcmdpath = ref "" in
7145 Arg.parse
7146 (Arg.align
7147 [("-p", Arg.String (fun s -> state.password <- s),
7148 "<password> Set password");
7150 ("-f", Arg.String (fun s -> Config.fontpath := s),
7151 "<path> Set path to the user interface font");
7153 ("-c", Arg.String (fun s -> Config.confpath := s),
7154 "<path> Set path to the configuration file");
7156 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7157 "<path> Set path to the trim cache file");
7159 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7160 "<named-destination> Set named destination");
7162 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7164 ("-remote", Arg.String (fun s -> rcmdpath := s),
7165 "<path> Set path to the remote commands source");
7167 ("-v", Arg.Unit (fun () ->
7168 Printf.printf
7169 "%s\nconfiguration path: %s\n"
7170 (version ())
7171 Config.defconfpath
7173 exit 0), " Print version and exit");
7176 (fun s -> state.path <- s)
7177 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7179 if String.length state.path = 0
7180 then (prerr_endline "file name missing"; exit 1);
7182 if not (Config.load ())
7183 then prerr_endline "failed to load configuration";
7185 let globalkeyhash = findkeyhash conf "global" in
7186 let wsfd, winw, winh = Wsi.init (object
7187 method expose =
7188 state.wthack <- false;
7189 if nogeomcmds state.geomcmds || platform == Posx
7190 then display ()
7191 else (
7192 GlClear.color (scalecolor2 conf.bgcolor);
7193 GlClear.clear [`color];
7195 method display = display ()
7196 method reshape w h = reshape w h
7197 method mouse b d x y m = mouse b d x y m
7198 method motion x y = state.mpos <- (x, y); motion x y
7199 method pmotion x y = state.mpos <- (x, y); pmotion x y
7200 method key k m =
7201 let mascm = m land (
7202 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7203 ) in
7204 match state.keystate with
7205 | KSnone ->
7206 let km = k, mascm in
7207 begin
7208 match
7209 let modehash = state.uioh#modehash in
7210 try Hashtbl.find modehash km
7211 with Not_found ->
7212 try Hashtbl.find globalkeyhash km
7213 with Not_found -> KMinsrt (k, m)
7214 with
7215 | KMinsrt (k, m) -> keyboard k m
7216 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7217 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7219 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7220 List.iter (fun (k, m) -> keyboard k m) insrt;
7221 state.keystate <- KSnone
7222 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7223 state.keystate <- KSinto (keys, insrt)
7224 | _ ->
7225 state.keystate <- KSnone
7227 method enter x y = state.mpos <- (x, y); pmotion x y
7228 method leave = state.mpos <- (-1, -1)
7229 method winstate wsl = state.winstate <- wsl
7230 method quit = raise Quit
7231 end) conf.cwinw conf.cwinh (platform = Posx) in
7233 state.wsfd <- wsfd;
7235 if not (
7236 List.exists GlMisc.check_extension
7237 [ "GL_ARB_texture_rectangle"
7238 ; "GL_EXT_texture_recangle"
7239 ; "GL_NV_texture_rectangle" ]
7241 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7243 let cr, sw =
7244 match Ne.pipe () with
7245 | Ne.Exn exn ->
7246 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7247 exit 1
7248 | Ne.Res rw -> rw
7249 and sr, cw =
7250 match Ne.pipe () with
7251 | Ne.Exn exn ->
7252 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7253 exit 1
7254 | Ne.Res rw -> rw
7257 cloexec cr;
7258 cloexec sw;
7259 cloexec sr;
7260 cloexec cw;
7262 setcheckers conf.checkers;
7263 redirectstderr ();
7265 init (cr, cw) (
7266 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7267 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7268 !Config.fontpath, !trimcachepath,
7269 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7271 state.sr <- sr;
7272 state.sw <- sw;
7273 state.text <- "Opening " ^ (mbtoutf8 state.path);
7274 reshape winw winh;
7275 opendoc state.path state.password;
7276 state.uioh <- uioh;
7278 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7279 let optrfd =
7280 ref (
7281 if String.length !rcmdpath > 0
7282 then remoteopen !rcmdpath
7283 else None
7287 let rec loop deadline =
7288 let r =
7289 match state.errfd with
7290 | None -> [state.sr; state.wsfd]
7291 | Some fd -> [state.sr; state.wsfd; fd]
7293 let r =
7294 match !optrfd with
7295 | None -> r
7296 | Some fd -> fd :: r
7298 if state.redisplay && not state.wthack
7299 then (
7300 state.redisplay <- false;
7301 display ();
7303 let timeout =
7304 let now = now () in
7305 if deadline > now
7306 then (
7307 if deadline = infinity
7308 then ~-.1.0
7309 else max 0.0 (deadline -. now)
7311 else 0.0
7313 let r, _, _ =
7314 try Unix.select r [] [] timeout
7315 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7317 begin match r with
7318 | [] ->
7319 state.ghyll None;
7320 let newdeadline =
7321 if state.ghyll == noghyll
7322 then
7323 match state.autoscroll with
7324 | Some step when step != 0 ->
7325 let y = state.y + step in
7326 let y =
7327 if y < 0
7328 then state.maxy
7329 else if y >= state.maxy then 0 else y
7331 gotoy y;
7332 if state.mode = View
7333 then state.text <- "";
7334 deadline +. 0.01
7335 | _ -> infinity
7336 else deadline +. 0.01
7338 loop newdeadline
7340 | l ->
7341 let rec checkfds = function
7342 | [] -> ()
7343 | fd :: rest when fd = state.sr ->
7344 let cmd = readcmd state.sr in
7345 act cmd;
7346 checkfds rest
7348 | fd :: rest when fd = state.wsfd ->
7349 Wsi.readresp fd;
7350 checkfds rest
7352 | fd :: rest when Some fd = !optrfd ->
7353 begin match remote fd with
7354 | None -> optrfd := remoteopen !rcmdpath;
7355 | opt -> optrfd := opt
7356 end;
7357 checkfds rest
7359 | fd :: rest ->
7360 let s = String.create 80 in
7361 let n = tempfailureretry (Unix.read fd s 0) 80 in
7362 if conf.redirectstderr
7363 then (
7364 Buffer.add_substring state.errmsgs s 0 n;
7365 state.newerrmsgs <- true;
7366 state.redisplay <- true;
7368 else (
7369 prerr_string (String.sub s 0 n);
7370 flush stderr;
7372 checkfds rest
7374 checkfds l;
7375 let newdeadline =
7376 let deadline1 =
7377 if deadline = infinity
7378 then now () +. 0.01
7379 else deadline
7381 match state.autoscroll with
7382 | Some step when step != 0 -> deadline1
7383 | _ -> if state.ghyll == noghyll then infinity else deadline1
7385 loop newdeadline
7386 end;
7389 loop infinity;
7390 with Quit ->
7391 Config.save ();