Robustness
[llpp/slowscroll.git] / main.ml
blob706bfdd4584561c065621b183484b570abf8cff6
1 exception Quit;;
3 let tempfailureretry = Wsi.tempfailureretry;;
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 let dolog fmt = Printf.kprintf prerr_endline fmt;;
17 let now = Unix.gettimeofday;;
19 type params = (angle * proportional * trimparams
20 * texcount * sliceheight * memsize
21 * colorspace * fontpath * trimcachepath
22 * haspbo)
23 and pageno = int
24 and width = int
25 and height = int
26 and leftx = int
27 and opaque = string
28 and recttype = int
29 and pixmapsize = int
30 and angle = int
31 and proportional = bool
32 and trimmargins = bool
33 and interpagespace = int
34 and texcount = int
35 and sliceheight = int
36 and gen = int
37 and top = float
38 and dtop = float
39 and fontpath = string
40 and trimcachepath = string
41 and memsize = int
42 and aalevel = int
43 and irect = (int * int * int * int)
44 and trimparams = (trimmargins * irect)
45 and colorspace = | Rgb | Bgr | Gray
46 and haspbo = bool
49 type link =
50 | Lnotfound
51 | Lfound of int
52 and linkdir =
53 | LDfirst
54 | LDlast
55 | LDfirstvisible of (int * int * int)
56 | LDleft of int
57 | LDright of int
58 | LDdown of int
59 | LDup of int
62 type pagewithlinks =
63 | Pwlnotfound
64 | Pwl of int
67 type keymap =
68 | KMinsrt of key
69 | KMinsrl of key list
70 | KMmulti of key list * key list
71 and key = int * int
72 and keyhash = (key, keymap) Hashtbl.t
73 and keystate =
74 | KSnone
75 | KSinto of (key list * key list)
78 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
79 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
81 type pipe = (Unix.file_descr * Unix.file_descr);;
83 external init : pipe -> params -> unit = "ml_init";;
84 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
85 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
86 external getpdimrect : int -> float array = "ml_getpdimrect";;
87 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
88 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
89 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
90 external measurestr : int -> string -> float = "ml_measure_string";;
91 external getmaxw : unit -> float = "ml_getmaxw";;
92 external postprocess :
93 opaque -> int -> int -> int -> (int * string * int) -> int
94 = "ml_postprocess";;
95 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
96 external platform : unit -> platform = "ml_platform";;
97 external setaalevel : int -> unit = "ml_setaalevel";;
98 external realloctexts : int -> bool = "ml_realloctexts";;
99 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
100 external findlink : opaque -> linkdir -> link = "ml_findlink";;
101 external getlink : opaque -> int -> under = "ml_getlink";;
102 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
103 external getlinkcount : opaque -> int = "ml_getlinkcount";;
104 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
105 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
106 external mbtoutf8 : string -> string = "ml_mbtoutf8";;
107 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
108 external freepbo : string -> unit = "ml_freepbo";;
109 external unmappbo : string -> unit = "ml_unmappbo";;
110 external pbousable : unit -> bool = "ml_pbo_usable";;
111 external unproject : opaque -> int -> int -> (int * int) option
112 = "ml_unproject";;
114 let platform_to_string = function
115 | Punknown -> "unknown"
116 | Plinux -> "Linux"
117 | Posx -> "OSX"
118 | Psun -> "Sun"
119 | Pfreebsd -> "FreeBSD"
120 | Pdragonflybsd -> "DragonflyBSD"
121 | Popenbsd -> "OpenBSD"
122 | Pnetbsd -> "NetBSD"
123 | Pcygwin -> "Cygwin"
126 let platform = platform ();;
128 let popen cmd fda =
129 if platform = Pcygwin
130 then (
131 let sh = "/bin/sh" in
132 let args = [|sh; "-c"; cmd|] in
133 let rec std si so se = function
134 | [] -> si, so, se
135 | (fd, 0) :: rest -> std fd so se rest
136 | (fd, -1) :: rest ->
137 Unix.set_close_on_exec fd;
138 std si so se rest
139 | (_, n) :: _ ->
140 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
142 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
143 ignore (Unix.create_process sh args si so se)
145 else popen cmd fda;
148 type x = int
149 and y = int
150 and tilex = int
151 and tiley = int
152 and tileparams = (x * y * width * height * tilex * tiley)
155 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
157 type mpos = int * int
158 and mstate =
159 | Msel of (mpos * mpos)
160 | Mpan of mpos
161 | Mscrolly | Mscrollx
162 | Mzoom of (int * int)
163 | Mzoomrect of (mpos * mpos)
164 | Mnone
167 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
168 and onkey = string -> int -> te
169 and ondone = string -> unit
170 and histcancel = unit -> unit
171 and onhist = ((histcmd -> string) * histcancel)
172 and histcmd = HCnext | HCprev | HCfirst | HClast
173 and cancelonempty = bool
174 and te =
175 | TEstop
176 | TEdone of string
177 | TEcont of string
178 | TEswitch of textentry
181 type 'a circbuf =
182 { store : 'a array
183 ; mutable rc : int
184 ; mutable wc : int
185 ; mutable len : int
189 let bound v minv maxv =
190 max minv (min maxv v);
193 let cbnew n v =
194 { store = Array.create n v
195 ; rc = 0
196 ; wc = 0
197 ; len = 0
201 let cbcap b = Array.length b.store;;
203 let cbput b v =
204 let cap = cbcap b in
205 b.store.(b.wc) <- v;
206 b.wc <- (b.wc + 1) mod cap;
207 b.rc <- b.wc;
208 b.len <- min (b.len + 1) cap;
211 let cbempty b = b.len = 0;;
213 let cbgetg b circular dir =
214 if cbempty b
215 then b.store.(0)
216 else
217 let rc = b.rc + dir in
218 let rc =
219 if circular
220 then (
221 if rc = -1
222 then b.len-1
223 else (
224 if rc >= b.len
225 then 0
226 else rc
229 else bound rc 0 (b.len-1)
231 b.rc <- rc;
232 b.store.(rc);
235 let cbget b = cbgetg b false;;
236 let cbgetc b = cbgetg b true;;
238 let drawstring size x y s =
239 Gl.enable `blend;
240 Gl.enable `texture_2d;
241 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
242 ignore (drawstr size x y s);
243 Gl.disable `blend;
244 Gl.disable `texture_2d;
247 let drawstring1 size x y s =
248 drawstr size x y s;
251 let drawstring2 size x y fmt =
252 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
255 type page =
256 { pageno : int
257 ; pagedimno : int
258 ; pagew : int
259 ; pageh : int
260 ; pagex : int
261 ; pagey : int
262 ; pagevw : int
263 ; pagevh : int
264 ; pagedispx : int
265 ; pagedispy : int
266 ; pagecol : int
270 let debugl l =
271 dolog "l %d dim=%d {" l.pageno l.pagedimno;
272 dolog " WxH %dx%d" l.pagew l.pageh;
273 dolog " vWxH %dx%d" l.pagevw l.pagevh;
274 dolog " pagex,y %d,%d" l.pagex l.pagey;
275 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
276 dolog " column %d" l.pagecol;
277 dolog "}";
280 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
281 dolog "rect {";
282 dolog " x0,y0=(% f, % f)" x0 y0;
283 dolog " x1,y1=(% f, % f)" x1 y1;
284 dolog " x2,y2=(% f, % f)" x2 y2;
285 dolog " x3,y3=(% f, % f)" x3 y3;
286 dolog "}";
289 type multicolumns = multicol * pagegeom
290 and singlecolumn = pagegeom
291 and splitcolumns = columncount * pagegeom
292 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
293 and multicol = columncount * covercount * covercount
294 and pdimno = int
295 and columncount = int
296 and covercount = int;;
298 type conf =
299 { mutable scrollbw : int
300 ; mutable scrollh : int
301 ; mutable icase : bool
302 ; mutable preload : bool
303 ; mutable pagebias : int
304 ; mutable verbose : bool
305 ; mutable debug : bool
306 ; mutable scrollstep : int
307 ; mutable hscrollstep : int
308 ; mutable maxhfit : bool
309 ; mutable crophack : bool
310 ; mutable autoscrollstep : int
311 ; mutable maxwait : float option
312 ; mutable hlinks : bool
313 ; mutable underinfo : bool
314 ; mutable interpagespace : interpagespace
315 ; mutable zoom : float
316 ; mutable presentation : bool
317 ; mutable angle : angle
318 ; mutable winw : int
319 ; mutable winh : int
320 ; mutable savebmarks : bool
321 ; mutable proportional : proportional
322 ; mutable trimmargins : trimmargins
323 ; mutable trimfuzz : irect
324 ; mutable memlimit : memsize
325 ; mutable texcount : texcount
326 ; mutable sliceheight : sliceheight
327 ; mutable thumbw : width
328 ; mutable jumpback : bool
329 ; mutable bgcolor : float * float * float
330 ; mutable bedefault : bool
331 ; mutable scrollbarinpm : bool
332 ; mutable tilew : int
333 ; mutable tileh : int
334 ; mutable mustoresize : memsize
335 ; mutable checkers : bool
336 ; mutable aalevel : int
337 ; mutable urilauncher : string
338 ; mutable pathlauncher : string
339 ; mutable colorspace : colorspace
340 ; mutable invert : bool
341 ; mutable colorscale : float
342 ; mutable redirectstderr : bool
343 ; mutable ghyllscroll : (int * int * int) option
344 ; mutable columns : columns
345 ; mutable beyecolumns : columncount option
346 ; mutable selcmd : string
347 ; mutable updatecurs : bool
348 ; mutable keyhashes : (string * keyhash) list
349 ; mutable hfsize : int
350 ; mutable pgscale : float
351 ; mutable usepbo : bool
352 ; mutable wheelbypage : bool
353 ; mutable stcmd : string
355 and columns =
356 | Csingle of singlecolumn
357 | Cmulti of multicolumns
358 | Csplit of splitcolumns
361 type anchor = pageno * top * dtop;;
363 type outline = string * int * anchor;;
365 type rect = float * float * float * float * float * float * float * float;;
367 type tile = opaque * pixmapsize * elapsed
368 and elapsed = float;;
369 type pagemapkey = pageno * gen;;
370 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
371 and row = int
372 and col = int;;
374 let emptyanchor = (0, 0.0, 0.0);;
376 type infochange = | Memused | Docinfo | Pdim;;
378 class type uioh = object
379 method display : unit
380 method key : int -> int -> uioh
381 method button : int -> bool -> int -> int -> int -> uioh
382 method motion : int -> int -> uioh
383 method pmotion : int -> int -> uioh
384 method infochanged : infochange -> unit
385 method scrollpw : (int * float * float)
386 method scrollph : (int * float * float)
387 method modehash : keyhash
388 end;;
390 type mode =
391 | Birdseye of (conf * leftx * pageno * pageno * anchor)
392 | Textentry of (textentry * onleave)
393 | View
394 | LinkNav of linktarget
395 and onleave = leavetextentrystatus -> unit
396 and leavetextentrystatus = | Cancel | Confirm
397 and helpitem = string * int * action
398 and action =
399 | Noaction
400 | Action of (uioh -> uioh)
401 and linktarget =
402 | Ltexact of (pageno * int)
403 | Ltgendir of int
406 let isbirdseye = function Birdseye _ -> true | _ -> false;;
407 let istextentry = function Textentry _ -> true | _ -> false;;
409 type currently =
410 | Idle
411 | Loading of (page * gen)
412 | Tiling of (
413 page * opaque * colorspace * angle * gen * col * row * width * height
415 | Outlining of outline list
418 let emptykeyhash = Hashtbl.create 0;;
419 let nouioh : uioh = object (self)
420 method display = ()
421 method key _ _ = self
422 method button _ _ _ _ _ = self
423 method motion _ _ = self
424 method pmotion _ _ = self
425 method infochanged _ = ()
426 method scrollpw = (0, nan, nan)
427 method scrollph = (0, nan, nan)
428 method modehash = emptykeyhash
429 end;;
431 type state =
432 { mutable sr : Unix.file_descr
433 ; mutable sw : Unix.file_descr
434 ; mutable wsfd : Unix.file_descr
435 ; mutable errfd : Unix.file_descr option
436 ; mutable stderr : Unix.file_descr
437 ; mutable errmsgs : Buffer.t
438 ; mutable newerrmsgs : bool
439 ; mutable w : int
440 ; mutable x : int
441 ; mutable y : int
442 ; mutable scrollw : int
443 ; mutable hscrollh : int
444 ; mutable anchor : anchor
445 ; mutable ranchors : (string * string * anchor) list
446 ; mutable maxy : int
447 ; mutable layout : page list
448 ; pagemap : (pagemapkey, opaque) Hashtbl.t
449 ; tilemap : (tilemapkey, tile) Hashtbl.t
450 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
451 ; mutable pdims : (pageno * width * height * leftx) list
452 ; mutable pagecount : int
453 ; mutable currently : currently
454 ; mutable mstate : mstate
455 ; mutable searchpattern : string
456 ; mutable rects : (pageno * recttype * rect) list
457 ; mutable rects1 : (pageno * recttype * rect) list
458 ; mutable text : string
459 ; mutable fullscreen : (width * height) option
460 ; mutable mode : mode
461 ; mutable uioh : uioh
462 ; mutable outlines : outline array
463 ; mutable bookmarks : outline list
464 ; mutable path : string
465 ; mutable password : string
466 ; mutable nameddest : string
467 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
468 ; mutable memused : memsize
469 ; mutable gen : gen
470 ; mutable throttle : (page list * int * float) option
471 ; mutable autoscroll : int option
472 ; mutable ghyll : (int option -> unit)
473 ; mutable help : helpitem array
474 ; mutable docinfo : (int * string) list
475 ; mutable texid : GlTex.texture_id option
476 ; hists : hists
477 ; mutable prevzoom : float
478 ; mutable progress : float
479 ; mutable redisplay : bool
480 ; mutable mpos : mpos
481 ; mutable keystate : keystate
482 ; mutable glinks : bool
483 ; mutable prevcolumns : (columns * float) option
484 ; mutable wthack : bool
486 and hists =
487 { pat : string circbuf
488 ; pag : string circbuf
489 ; nav : anchor circbuf
490 ; sel : string circbuf
494 let defconf =
495 { scrollbw = 7
496 ; scrollh = 12
497 ; icase = true
498 ; preload = true
499 ; pagebias = 0
500 ; verbose = false
501 ; debug = false
502 ; scrollstep = 24
503 ; hscrollstep = 24
504 ; maxhfit = true
505 ; crophack = false
506 ; autoscrollstep = 2
507 ; maxwait = None
508 ; hlinks = false
509 ; underinfo = false
510 ; interpagespace = 2
511 ; zoom = 1.0
512 ; presentation = false
513 ; angle = 0
514 ; winw = 900
515 ; winh = 900
516 ; savebmarks = true
517 ; proportional = true
518 ; trimmargins = false
519 ; trimfuzz = (0,0,0,0)
520 ; memlimit = 32 lsl 20
521 ; texcount = 256
522 ; sliceheight = 24
523 ; thumbw = 76
524 ; jumpback = true
525 ; bgcolor = (0.5, 0.5, 0.5)
526 ; bedefault = false
527 ; scrollbarinpm = true
528 ; tilew = 2048
529 ; tileh = 2048
530 ; mustoresize = 256 lsl 20
531 ; checkers = true
532 ; aalevel = 8
533 ; urilauncher =
534 (match platform with
535 | Plinux | Pfreebsd | Pdragonflybsd
536 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
537 | Posx -> "open \"%s\""
538 | Pcygwin -> "cygstart \"%s\""
539 | Punknown -> "echo %s")
540 ; pathlauncher = "lp \"%s\""
541 ; selcmd =
542 (match platform with
543 | Plinux | Pfreebsd | Pdragonflybsd
544 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
545 | Posx -> "pbcopy"
546 | Pcygwin -> "wsel"
547 | Punknown -> "cat")
548 ; colorspace = Rgb
549 ; invert = false
550 ; colorscale = 1.0
551 ; redirectstderr = false
552 ; ghyllscroll = None
553 ; columns = Csingle [||]
554 ; beyecolumns = None
555 ; updatecurs = false
556 ; hfsize = 12
557 ; pgscale = 1.0
558 ; usepbo = false
559 ; wheelbypage = false
560 ; stcmd = "echo SyncTex"
561 ; keyhashes =
562 let mk n = (n, Hashtbl.create 1) in
563 [ mk "global"
564 ; mk "info"
565 ; mk "help"
566 ; mk "outline"
567 ; mk "listview"
568 ; mk "birdseye"
569 ; mk "textentry"
570 ; mk "links"
571 ; mk "view"
576 let wtmode = ref false;;
578 let findkeyhash c name =
579 try List.assoc name c.keyhashes
580 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
583 let conf = { defconf with angle = defconf.angle };;
585 let pgscale h = truncate (float h *. conf.pgscale);;
587 type fontstate =
588 { mutable fontsize : int
589 ; mutable wwidth : float
590 ; mutable maxrows : int
594 let fstate =
595 { fontsize = 14
596 ; wwidth = nan
597 ; maxrows = -1
601 let setfontsize n =
602 fstate.fontsize <- n;
603 fstate.wwidth <- measurestr fstate.fontsize "w";
604 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
607 let geturl s =
608 let colonpos = try String.index s ':' with Not_found -> -1 in
609 let len = String.length s in
610 if colonpos >= 0 && colonpos + 3 < len
611 then (
612 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
613 then
614 let schemestartpos =
615 try String.rindex_from s colonpos ' '
616 with Not_found -> -1
618 let scheme =
619 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
621 match scheme with
622 | "http" | "ftp" | "mailto" ->
623 let epos =
624 try String.index_from s colonpos ' '
625 with Not_found -> len
627 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
628 | _ -> ""
629 else ""
631 else ""
634 let gotouri uri =
635 if String.length conf.urilauncher = 0
636 then print_endline uri
637 else (
638 let url = geturl uri in
639 if String.length url = 0
640 then print_endline uri
641 else
642 let re = Str.regexp "%s" in
643 let command = Str.global_replace re url conf.urilauncher in
644 try popen command []
645 with exn ->
646 Printf.eprintf
647 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
648 flush stderr;
652 let version () =
653 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
654 (platform_to_string platform) Sys.word_size Sys.ocaml_version
657 let makehelp () =
658 let strings = version () :: "" :: Help.keys in
659 Array.of_list (
660 List.map (fun s ->
661 let url = geturl s in
662 if String.length url > 0
663 then (s, 0, Action (fun u -> gotouri url; u))
664 else (s, 0, Noaction)
665 ) strings);
668 let noghyll _ = ();;
669 let firstgeomcmds = "", [];;
671 let state =
672 { sr = Unix.stdin
673 ; sw = Unix.stdin
674 ; wsfd = Unix.stdin
675 ; errfd = None
676 ; stderr = Unix.stderr
677 ; errmsgs = Buffer.create 0
678 ; newerrmsgs = false
679 ; x = 0
680 ; y = 0
681 ; w = 0
682 ; scrollw = 0
683 ; hscrollh = 0
684 ; anchor = emptyanchor
685 ; ranchors = []
686 ; layout = []
687 ; maxy = max_int
688 ; tilelru = Queue.create ()
689 ; pagemap = Hashtbl.create 10
690 ; tilemap = Hashtbl.create 10
691 ; pdims = []
692 ; pagecount = 0
693 ; currently = Idle
694 ; mstate = Mnone
695 ; rects = []
696 ; rects1 = []
697 ; text = ""
698 ; mode = View
699 ; fullscreen = None
700 ; searchpattern = ""
701 ; outlines = [||]
702 ; bookmarks = []
703 ; path = ""
704 ; password = ""
705 ; nameddest = ""
706 ; geomcmds = firstgeomcmds
707 ; hists =
708 { nav = cbnew 10 emptyanchor
709 ; pat = cbnew 10 ""
710 ; pag = cbnew 10 ""
711 ; sel = cbnew 10 ""
713 ; memused = 0
714 ; gen = 0
715 ; throttle = None
716 ; autoscroll = None
717 ; ghyll = noghyll
718 ; help = makehelp ()
719 ; docinfo = []
720 ; texid = None
721 ; prevzoom = 1.0
722 ; progress = -1.0
723 ; uioh = nouioh
724 ; redisplay = true
725 ; mpos = (-1, -1)
726 ; keystate = KSnone
727 ; glinks = false
728 ; prevcolumns = None
729 ; wthack = false
733 let vlog fmt =
734 if conf.verbose
735 then
736 Printf.kprintf prerr_endline fmt
737 else
738 Printf.kprintf ignore fmt
741 let launchpath () =
742 if String.length conf.pathlauncher = 0
743 then print_endline state.path
744 else (
745 let re = Str.regexp "%s" in
746 let command = Str.global_replace re state.path conf.pathlauncher in
747 try popen command []
748 with exn ->
749 Printf.eprintf
750 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
751 flush stderr;
755 module Ne = struct
756 type 'a t = | Res of 'a | Exn of exn;;
758 let pipe () =
759 try Res (Unix.pipe ())
760 with exn -> Exn exn
763 let clo fd f =
764 try tempfailureretry Unix.close fd
765 with exn -> f (Printexc.to_string exn)
768 let dup fd =
769 try Res (tempfailureretry Unix.dup fd)
770 with exn -> Exn exn
773 let dup2 fd1 fd2 =
774 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
775 with exn -> Exn exn
777 end;;
779 let redirectstderr () =
780 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
781 if conf.redirectstderr
782 then
783 match Ne.pipe () with
784 | Ne.Exn exn ->
785 dolog "failed to create stderr redirection pipes: %s"
786 (Printexc.to_string exn)
788 | Ne.Res (r, w) ->
789 begin match Ne.dup Unix.stderr with
790 | Ne.Exn exn ->
791 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
792 Ne.clo r (clofail "pipe/r");
793 Ne.clo w (clofail "pipe/w");
795 | Ne.Res dupstderr ->
796 begin match Ne.dup2 w Unix.stderr with
797 | Ne.Exn exn ->
798 dolog "failed to dup2 to stderr: %s"
799 (Printexc.to_string 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"
816 (Printexc.to_string exn)
817 | Ne.Res () ->
818 Ne.clo fd (clofail "dup of stderr");
819 state.errfd <- None;
820 end;
821 | None -> ()
822 end;
823 prerr_string (Buffer.contents state.errmsgs);
824 flush stderr;
825 Buffer.clear state.errmsgs;
829 module G =
830 struct
831 let postRedisplay who =
832 if conf.verbose
833 then prerr_endline ("redisplay for " ^ who);
834 state.redisplay <- true;
836 end;;
838 let getopaque pageno =
839 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
840 with Not_found -> None
843 let putopaque pageno opaque =
844 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
847 let pagetranslatepoint l x y =
848 let dy = y - l.pagedispy in
849 let y = dy + l.pagey in
850 let dx = x - l.pagedispx in
851 let x = dx + l.pagex in
852 (x, y);
855 let onppundermouse g x y d =
856 let rec f = function
857 | l :: rest ->
858 begin match getopaque l.pageno with
859 | Some opaque ->
860 let x0 = l.pagedispx in
861 let x1 = x0 + l.pagevw in
862 let y0 = l.pagedispy in
863 let y1 = y0 + l.pagevh in
864 if y >= y0 && y <= y1 && x >= x0 && x <= x1
865 then
866 let px, py = pagetranslatepoint l x y in
867 match g opaque l px py with
868 | Some res -> res
869 | None -> f rest
870 else f rest
871 | _ ->
872 f rest
874 | [] -> d
876 f state.layout
879 let getunder x y =
880 let g opaque _ px py =
881 match whatsunder opaque px py with
882 | Unone -> None
883 | under -> Some under
885 onppundermouse g x y Unone
888 let unproject x y =
889 let g opaque l x y =
890 match unproject opaque x y with
891 | Some (x, y) -> Some (Some (l.pageno, x, y))
892 | None -> None
894 onppundermouse g x y None;
897 let showtext c s =
898 state.text <- Printf.sprintf "%c%s" c s;
899 G.postRedisplay "showtext";
902 let undertext = function
903 | Unone -> "none"
904 | Ulinkuri s -> s
905 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
906 | Utext s -> "font: " ^ s
907 | Uunexpected s -> "unexpected: " ^ s
908 | Ulaunch s -> "launch: " ^ s
909 | Unamed s -> "named: " ^ s
910 | Uremote (filename, pageno) ->
911 Printf.sprintf "%s: page %d" filename (pageno+1)
914 let updateunder x y =
915 match getunder x y with
916 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
917 | Ulinkuri uri ->
918 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
919 Wsi.setcursor Wsi.CURSOR_INFO
920 | Ulinkgoto (pageno, _) ->
921 if conf.underinfo
922 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
923 Wsi.setcursor Wsi.CURSOR_INFO
924 | Utext s ->
925 if conf.underinfo then showtext 'f' ("ont: " ^ s);
926 Wsi.setcursor Wsi.CURSOR_TEXT
927 | Uunexpected s ->
928 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
929 Wsi.setcursor Wsi.CURSOR_INHERIT
930 | Ulaunch s ->
931 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
932 Wsi.setcursor Wsi.CURSOR_INHERIT
933 | Unamed s ->
934 if conf.underinfo then showtext 'n' ("amed: " ^ s);
935 Wsi.setcursor Wsi.CURSOR_INHERIT
936 | Uremote (filename, pageno) ->
937 if conf.underinfo then showtext 'r'
938 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
939 Wsi.setcursor Wsi.CURSOR_INFO
942 let showlinktype under =
943 if conf.underinfo
944 then
945 match under with
946 | Unone -> ()
947 | under ->
948 let s = undertext under in
949 showtext ' ' s
952 let addchar s c =
953 let b = Buffer.create (String.length s + 1) in
954 Buffer.add_string b s;
955 Buffer.add_char b c;
956 Buffer.contents b;
959 let colorspace_of_string s =
960 match String.lowercase s with
961 | "rgb" -> Rgb
962 | "bgr" -> Bgr
963 | "gray" -> Gray
964 | _ -> failwith "invalid colorspace"
967 let int_of_colorspace = function
968 | Rgb -> 0
969 | Bgr -> 1
970 | Gray -> 2
973 let colorspace_of_int = function
974 | 0 -> Rgb
975 | 1 -> Bgr
976 | 2 -> Gray
977 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
980 let colorspace_to_string = function
981 | Rgb -> "rgb"
982 | Bgr -> "bgr"
983 | Gray -> "gray"
986 let intentry_with_suffix text key =
987 let c =
988 if key >= 32 && key < 127
989 then Char.chr key
990 else '\000'
992 match Char.lowercase c with
993 | '0' .. '9' ->
994 let text = addchar text c in
995 TEcont text
997 | 'k' | 'm' | 'g' ->
998 let text = addchar text c in
999 TEcont text
1001 | _ ->
1002 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1003 TEcont text
1006 let multicolumns_to_string (n, a, b) =
1007 if a = 0 && b = 0
1008 then Printf.sprintf "%d" n
1009 else Printf.sprintf "%d,%d,%d" n a b;
1012 let multicolumns_of_string s =
1014 (int_of_string s, 0, 0)
1015 with _ ->
1016 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1017 if a > 1 || b > 1
1018 then failwith "subtly broken"; (n, a, b)
1022 let readcmd fd =
1023 let s = "xxxx" in
1024 let n = tempfailureretry (Unix.read fd s 0) 4 in
1025 if n != 4 then failwith "incomplete read(len)";
1026 let len = 0
1027 lor (Char.code s.[0] lsl 24)
1028 lor (Char.code s.[1] lsl 16)
1029 lor (Char.code s.[2] lsl 8)
1030 lor (Char.code s.[3] lsl 0)
1032 let s = String.create len in
1033 let n = tempfailureretry (Unix.read fd s 0) len in
1034 if n != len then failwith "incomplete read(data)";
1038 let btod b = if b then 1 else 0;;
1040 let wcmd fmt =
1041 let b = Buffer.create 16 in
1042 Buffer.add_string b "llll";
1043 Printf.kbprintf
1044 (fun b ->
1045 let s = Buffer.contents b in
1046 let n = String.length s in
1047 let len = n - 4 in
1048 (* dolog "wcmd %S" (String.sub s 4 len); *)
1049 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1050 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1051 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1052 s.[3] <- Char.chr (len land 0xff);
1053 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1054 if n' != n then failwith "write failed";
1055 ) b fmt;
1058 let calcips h =
1059 let d = conf.winh - h in
1060 max conf.interpagespace ((d + 1) / 2)
1063 let rowyh (c, coverA, coverB) b n =
1064 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1065 then
1066 let _, _, vy, (_, _, h, _) = b.(n) in
1067 (vy, h)
1068 else
1069 let n' = n - coverA in
1070 let d = n' mod c in
1071 let s = n - d in
1072 let e = min state.pagecount (s + c) in
1073 let rec find m miny maxh = if m = e then miny, maxh else
1074 let _, _, y, (_, _, h, _) = b.(m) in
1075 let miny = min miny y in
1076 let maxh = max maxh h in
1077 find (m+1) miny maxh
1078 in find s max_int 0
1081 let calcheight () =
1082 match conf.columns with
1083 | Cmulti ((_, _, _) as cl, b) ->
1084 if Array.length b > 0
1085 then
1086 let y, h = rowyh cl b (Array.length b - 1) in
1087 y + h + (if conf.presentation then calcips h else 0)
1088 else 0
1089 | Csingle b ->
1090 if Array.length b > 0
1091 then
1092 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1093 y + h + (if conf.presentation then calcips h else 0)
1094 else 0
1095 | Csplit (_, b) ->
1096 if Array.length b > 0
1097 then
1098 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1099 y + h
1100 else 0
1103 let getpageyh pageno =
1104 let pageno = bound pageno 0 (state.pagecount-1) in
1105 match conf.columns with
1106 | Csingle b ->
1107 if Array.length b = 0
1108 then 0, 0
1109 else
1110 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1111 let y =
1112 if conf.presentation
1113 then y - calcips h
1114 else y
1116 y, h
1117 | Cmulti (cl, b) ->
1118 if Array.length b = 0
1119 then 0, 0
1120 else
1121 let y, h = rowyh cl b pageno in
1122 let y =
1123 if conf.presentation
1124 then y - calcips h
1125 else y
1127 y, h
1128 | Csplit (c, b) ->
1129 if Array.length b = 0
1130 then 0, 0
1131 else
1132 let n = pageno*c in
1133 let (_, _, y, (_, _, h, _)) = b.(n) in
1134 y, h
1137 let getpagedim pageno =
1138 let rec f ppdim l =
1139 match l with
1140 | (n, _, _, _) as pdim :: rest ->
1141 if n >= pageno
1142 then (if n = pageno then pdim else ppdim)
1143 else f pdim rest
1145 | [] -> ppdim
1147 f (-1, -1, -1, -1) state.pdims
1150 let getpagey pageno = fst (getpageyh pageno);;
1152 let nogeomcmds cmds =
1153 match cmds with
1154 | s, [] -> String.length s = 0
1155 | _ -> false
1158 let page_of_y y =
1159 let ((c, coverA, coverB) as cl), b =
1160 match conf.columns with
1161 | Csingle b -> (1, 0, 0), b
1162 | Cmulti (c, b) -> c, b
1163 | Csplit (_, b) -> (1, 0, 0), b
1165 let rec bsearch nmin nmax =
1166 if nmin > nmax
1167 then bound nmin 0 (state.pagecount-1)
1168 else
1169 let n = (nmax + nmin) / 2 in
1170 let vy, h = rowyh cl b n in
1171 let y0, y1 =
1172 if conf.presentation
1173 then
1174 let ips = calcips h in
1175 let y0 = vy - ips in
1176 let y1 = vy + h + ips in
1177 y0, y1
1178 else (
1179 if n = 0
1180 then 0, vy + h + conf.interpagespace
1181 else
1182 let y0 = vy - conf.interpagespace in
1183 y0, y0 + h + conf.interpagespace
1186 if y >= y0 && y < y1
1187 then (
1188 if c = 1
1189 then n
1190 else (
1191 if n > coverA
1192 then
1193 if n < state.pagecount - coverB
1194 then ((n-coverA)/c)*c + coverA
1195 else n
1196 else n
1199 else (
1200 if y > y0
1201 then bsearch (n+1) nmax
1202 else bsearch nmin (n-1)
1205 let r = bsearch 0 (state.pagecount-1) in
1209 let layoutN ((columns, coverA, coverB), b) y sh =
1210 let sh = sh - state.hscrollh in
1211 let rec fold accu n =
1212 if n = Array.length b
1213 then accu
1214 else
1215 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1216 if (vy - y) > sh &&
1217 (n = coverA - 1
1218 || n = state.pagecount - coverB
1219 || (n - coverA) mod columns = columns - 1)
1220 then accu
1221 else
1222 let accu =
1223 if vy + h > y
1224 then
1225 let pagey = max 0 (y - vy) in
1226 let pagedispy = if pagey > 0 then 0 else vy - y in
1227 let pagedispx, pagex =
1228 let pdx =
1229 if n = coverA - 1 || n = state.pagecount - coverB
1230 then state.x + (conf.winw - state.scrollw - w) / 2
1231 else dx + xoff + state.x
1233 if pdx < 0
1234 then 0, -pdx
1235 else pdx, 0
1237 let pagevw =
1238 let vw = conf.winw - state.scrollw - pagedispx in
1239 let pw = w - pagex in
1240 min vw pw
1242 let pagevh = min (h - pagey) (sh - pagedispy) in
1243 if pagevw > 0 && pagevh > 0
1244 then
1245 let e =
1246 { pageno = n
1247 ; pagedimno = pdimno
1248 ; pagew = w
1249 ; pageh = h
1250 ; pagex = pagex
1251 ; pagey = pagey
1252 ; pagevw = pagevw
1253 ; pagevh = pagevh
1254 ; pagedispx = pagedispx
1255 ; pagedispy = pagedispy
1256 ; pagecol = 0
1259 e :: accu
1260 else
1261 accu
1262 else
1263 accu
1265 fold accu (n+1)
1267 List.rev (fold [] (page_of_y y));
1270 let layoutS (columns, b) y sh =
1271 let sh = sh - state.hscrollh in
1272 let rec fold accu n =
1273 if n = Array.length b
1274 then accu
1275 else
1276 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1277 if (vy - y) > sh
1278 then accu
1279 else
1280 let accu =
1281 if vy + pageh > y
1282 then
1283 let x = xoff + state.x in
1284 let pagey = max 0 (y - vy) in
1285 let pagedispy = if pagey > 0 then 0 else vy - y in
1286 let pagedispx, pagex =
1287 if px = 0
1288 then (
1289 if x < 0
1290 then 0, -x
1291 else x, 0
1293 else (
1294 let px = px - x in
1295 if px < 0
1296 then -px, 0
1297 else 0, px
1300 let pagecolw = pagew/columns in
1301 let pagedispx =
1302 if pagecolw < conf.winw
1303 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1304 else pagedispx
1306 let pagevw =
1307 let vw = conf.winw - pagedispx - state.scrollw in
1308 let pw = pagew - pagex in
1309 min vw pw
1311 let pagevw = min pagevw pagecolw in
1312 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1313 if pagevw > 0 && pagevh > 0
1314 then
1315 let e =
1316 { pageno = n/columns
1317 ; pagedimno = pdimno
1318 ; pagew = pagew
1319 ; pageh = pageh
1320 ; pagex = pagex
1321 ; pagey = pagey
1322 ; pagevw = pagevw
1323 ; pagevh = pagevh
1324 ; pagedispx = pagedispx
1325 ; pagedispy = pagedispy
1326 ; pagecol = n mod columns
1329 e :: accu
1330 else
1331 accu
1332 else
1333 accu
1335 fold accu (n+1)
1337 List.rev (fold [] 0)
1340 let layout y sh =
1341 if nogeomcmds state.geomcmds
1342 then
1343 match conf.columns with
1344 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1345 | Cmulti c -> layoutN c y sh
1346 | Csplit s -> layoutS s y sh
1347 else []
1350 let clamp incr =
1351 let y = state.y + incr in
1352 let y = max 0 y in
1353 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1357 let itertiles l f =
1358 let tilex = l.pagex mod conf.tilew in
1359 let tiley = l.pagey mod conf.tileh in
1361 let col = l.pagex / conf.tilew in
1362 let row = l.pagey / conf.tileh in
1364 let rec rowloop row y0 dispy h =
1365 if h = 0
1366 then ()
1367 else (
1368 let dh = conf.tileh - y0 in
1369 let dh = min h dh in
1370 let rec colloop col x0 dispx w =
1371 if w = 0
1372 then ()
1373 else (
1374 let dw = conf.tilew - x0 in
1375 let dw = min w dw in
1377 f col row dispx dispy x0 y0 dw dh;
1378 colloop (col+1) 0 (dispx+dw) (w-dw)
1381 colloop col tilex l.pagedispx l.pagevw;
1382 rowloop (row+1) 0 (dispy+dh) (h-dh)
1385 if l.pagevw > 0 && l.pagevh > 0
1386 then rowloop row tiley l.pagedispy l.pagevh;
1389 let gettileopaque l col row =
1390 let key =
1391 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1393 try Some (Hashtbl.find state.tilemap key)
1394 with Not_found -> None
1397 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1398 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1399 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1402 let drawtiles l color =
1403 GlDraw.color color;
1404 let f col row x y tilex tiley w h =
1405 match gettileopaque l col row with
1406 | Some (opaque, _, t) ->
1407 let params = x, y, w, h, tilex, tiley in
1408 if conf.invert
1409 then (
1410 Gl.enable `blend;
1411 GlFunc.blend_func `zero `one_minus_src_color;
1413 drawtile params opaque;
1414 if conf.invert
1415 then Gl.disable `blend;
1416 if conf.debug
1417 then (
1418 let s = Printf.sprintf
1419 "%d[%d,%d] %f sec"
1420 l.pageno col row t
1422 let w = measurestr fstate.fontsize s in
1423 GlMisc.push_attrib [`current];
1424 GlDraw.color (0.0, 0.0, 0.0);
1425 GlDraw.rect
1426 (float (x-2), float (y-2))
1427 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1428 GlDraw.color (1.0, 1.0, 1.0);
1429 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1430 GlMisc.pop_attrib ();
1433 | _ ->
1434 let w =
1435 let lw = conf.winw - state.scrollw - x in
1436 min lw w
1437 and h =
1438 let lh = conf.winh - y in
1439 min lh h
1441 begin match state.texid with
1442 | Some id ->
1443 Gl.enable `texture_2d;
1444 GlTex.bind_texture `texture_2d id;
1445 let x0 = float x
1446 and y0 = float y
1447 and x1 = float (x+w)
1448 and y1 = float (y+h) in
1450 let tw = float w /. 16.0
1451 and th = float h /. 16.0 in
1452 let tx0 = float tilex /. 16.0
1453 and ty0 = float tiley /. 16.0 in
1454 let tx1 = tx0 +. tw
1455 and ty1 = ty0 +. th in
1456 GlDraw.begins `quads;
1457 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1458 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1459 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1460 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1461 GlDraw.ends ();
1463 Gl.disable `texture_2d;
1464 | None ->
1465 GlDraw.color (1.0, 1.0, 1.0);
1466 GlDraw.rect
1467 (float x, float y)
1468 (float (x+w), float (y+h));
1469 end;
1470 if w > 128 && h > fstate.fontsize + 10
1471 then (
1472 GlDraw.color (0.0, 0.0, 0.0);
1473 let c, r =
1474 if conf.verbose
1475 then (col*conf.tilew, row*conf.tileh)
1476 else col, row
1478 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1480 GlDraw.color color;
1482 itertiles l f
1485 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1487 let tilevisible1 l x y =
1488 let ax0 = l.pagex
1489 and ax1 = l.pagex + l.pagevw
1490 and ay0 = l.pagey
1491 and ay1 = l.pagey + l.pagevh in
1493 let bx0 = x
1494 and by0 = y in
1495 let bx1 = min (bx0 + conf.tilew) l.pagew
1496 and by1 = min (by0 + conf.tileh) l.pageh in
1498 let rx0 = max ax0 bx0
1499 and ry0 = max ay0 by0
1500 and rx1 = min ax1 bx1
1501 and ry1 = min ay1 by1 in
1503 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1504 nonemptyintersection
1507 let tilevisible layout n x y =
1508 let rec findpageinlayout m = function
1509 | l :: rest when l.pageno = n ->
1510 tilevisible1 l x y || (
1511 match conf.columns with
1512 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1513 | _ -> false
1515 | _ :: rest -> findpageinlayout 0 rest
1516 | [] -> false
1518 findpageinlayout 0 layout;
1521 let tileready l x y =
1522 tilevisible1 l x y &&
1523 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1526 let tilepage n p layout =
1527 let rec loop = function
1528 | l :: rest ->
1529 if l.pageno = n
1530 then
1531 let f col row _ _ _ _ _ _ =
1532 if state.currently = Idle
1533 then
1534 match gettileopaque l col row with
1535 | Some _ -> ()
1536 | None ->
1537 let x = col*conf.tilew
1538 and y = row*conf.tileh in
1539 let w =
1540 let w = l.pagew - x in
1541 min w conf.tilew
1543 let h =
1544 let h = l.pageh - y in
1545 min h conf.tileh
1547 let pbo =
1548 if conf.usepbo
1549 then getpbo w h conf.colorspace
1550 else "0"
1552 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1553 state.currently <-
1554 Tiling (
1555 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1556 conf.tilew, conf.tileh
1559 itertiles l f;
1560 else
1561 loop rest
1563 | [] -> ()
1565 if nogeomcmds state.geomcmds
1566 then loop layout;
1569 let preloadlayout y =
1570 let y = if y < conf.winh then 0 else y - conf.winh in
1571 let h = conf.winh*3 in
1572 layout y h;
1575 let load pages =
1576 let rec loop pages =
1577 if state.currently != Idle
1578 then ()
1579 else
1580 match pages with
1581 | l :: rest ->
1582 begin match getopaque l.pageno with
1583 | None ->
1584 wcmd "page %d %d" l.pageno l.pagedimno;
1585 state.currently <- Loading (l, state.gen);
1586 | Some opaque ->
1587 tilepage l.pageno opaque pages;
1588 loop rest
1589 end;
1590 | _ -> ()
1592 if nogeomcmds state.geomcmds
1593 then loop pages
1596 let preload pages =
1597 load pages;
1598 if conf.preload && state.currently = Idle
1599 then load (preloadlayout state.y);
1602 let layoutready layout =
1603 let rec fold all ls =
1604 all && match ls with
1605 | l :: rest ->
1606 let seen = ref false in
1607 let allvisible = ref true in
1608 let foo col row _ _ _ _ _ _ =
1609 seen := true;
1610 allvisible := !allvisible &&
1611 begin match gettileopaque l col row with
1612 | Some _ -> true
1613 | None -> false
1616 itertiles l foo;
1617 fold (!seen && !allvisible) rest
1618 | [] -> true
1620 let alltilesvisible = fold true layout in
1621 alltilesvisible;
1624 let gotoy y =
1625 state.wthack <- false;
1626 let y = bound y 0 state.maxy in
1627 let y, layout, proceed =
1628 match conf.maxwait with
1629 | Some time when state.ghyll == noghyll ->
1630 begin match state.throttle with
1631 | None ->
1632 let layout = layout y conf.winh in
1633 let ready = layoutready layout in
1634 if not ready
1635 then (
1636 load layout;
1637 state.throttle <- Some (layout, y, now ());
1639 else G.postRedisplay "gotoy showall (None)";
1640 y, layout, ready
1641 | Some (_, _, started) ->
1642 let dt = now () -. started in
1643 if dt > time
1644 then (
1645 state.throttle <- None;
1646 let layout = layout y conf.winh in
1647 load layout;
1648 G.postRedisplay "maxwait";
1649 y, layout, true
1651 else -1, [], false
1654 | _ ->
1655 let layout = layout y conf.winh in
1656 G.postRedisplay "gotoy ready";
1657 y, layout, true
1659 if proceed
1660 then (
1661 state.y <- y;
1662 state.layout <- layout;
1663 begin match state.mode with
1664 | LinkNav (Ltexact (pageno, linkno)) ->
1665 let rec loop = function
1666 | [] ->
1667 state.mode <- LinkNav (Ltgendir 0)
1668 | l :: _ when l.pageno = pageno ->
1669 begin match getopaque pageno with
1670 | None ->
1671 state.mode <- LinkNav (Ltgendir 0)
1672 | Some opaque ->
1673 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1674 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1675 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1676 then state.mode <- LinkNav (Ltgendir 0)
1678 | _ :: rest -> loop rest
1680 loop layout
1681 | _ -> ()
1682 end;
1683 begin match state.mode with
1684 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1685 if not (pagevisible layout pageno)
1686 then (
1687 match state.layout with
1688 | [] -> ()
1689 | l :: _ ->
1690 state.mode <- Birdseye (
1691 conf, leftx, l.pageno, hooverpageno, anchor
1694 | LinkNav (Ltgendir dir as lt) ->
1695 let linknav =
1696 let rec loop = function
1697 | [] -> lt
1698 | l :: rest ->
1699 match getopaque l.pageno with
1700 | None -> loop rest
1701 | Some opaque ->
1702 let link =
1703 let ld =
1704 if dir = 0
1705 then LDfirstvisible (l.pagex, l.pagey, dir)
1706 else (
1707 if dir > 0 then LDfirst else LDlast
1710 findlink opaque ld
1712 match link with
1713 | Lnotfound -> loop rest
1714 | Lfound n ->
1715 showlinktype (getlink opaque n);
1716 Ltexact (l.pageno, n)
1718 loop state.layout
1720 state.mode <- LinkNav linknav
1721 | _ -> ()
1722 end;
1723 preload layout;
1725 state.ghyll <- noghyll;
1726 if conf.updatecurs
1727 then (
1728 let mx, my = state.mpos in
1729 updateunder mx my;
1733 let conttiling pageno opaque =
1734 tilepage pageno opaque
1735 (if conf.preload then preloadlayout state.y else state.layout)
1738 let gotoy_and_clear_text y =
1739 if not conf.verbose then state.text <- "";
1740 gotoy y;
1743 let getanchor1 l =
1744 let top =
1745 let coloff = l.pagecol * l.pageh in
1746 float (l.pagey + coloff) /. float l.pageh
1748 let dtop =
1749 if l.pagedispy = 0
1750 then
1752 else
1753 if conf.presentation
1754 then float l.pagedispy /. float (calcips l.pageh)
1755 else float l.pagedispy /. float conf.interpagespace
1757 (l.pageno, top, dtop)
1760 let getanchor () =
1761 match state.layout with
1762 | l :: _ -> getanchor1 l
1763 | [] ->
1764 let n = page_of_y state.y in
1765 let y, h = getpageyh n in
1766 let dy = y - state.y in
1767 let dtop =
1768 if conf.presentation
1769 then
1770 let ips = calcips h in
1771 float (dy + ips) /. float ips
1772 else
1773 float dy /. float conf.interpagespace
1775 (n, 0.0, dtop)
1778 let getanchory (n, top, dtop) =
1779 let y, h = getpageyh n in
1780 if conf.presentation
1781 then
1782 let ips = calcips h in
1783 y + truncate (top*.float h -. dtop*.float ips) + ips;
1784 else
1785 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1788 let gotoanchor anchor =
1789 gotoy (getanchory anchor);
1792 let addnav () =
1793 cbput state.hists.nav (getanchor ());
1796 let getnav dir =
1797 let anchor = cbgetc state.hists.nav dir in
1798 getanchory anchor;
1801 let gotoghyll y =
1802 let scroll f n a b =
1803 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1804 let snake f a b =
1805 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1806 if f < a
1807 then s (float f /. float a)
1808 else (
1809 if f > b
1810 then 1.0 -. s ((float (f-b) /. float (n-b)))
1811 else 1.0
1814 snake f a b
1815 and summa f n a b =
1816 (* courtesy:
1817 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1818 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1819 let iv1 = iv f in
1820 let ins = float a *. iv1
1821 and outs = float (n-b) *. iv1 in
1822 let ones = b - a in
1823 ins +. outs +. float ones
1825 let rec set (_N, _A, _B) y sy =
1826 let sum = summa 1.0 _N _A _B in
1827 let dy = float (y - sy) in
1828 state.ghyll <- (
1829 let rec gf n y1 o =
1830 if n >= _N
1831 then state.ghyll <- noghyll
1832 else
1833 let go n =
1834 let s = scroll n _N _A _B in
1835 let y1 = y1 +. ((s *. dy) /. sum) in
1836 gotoy_and_clear_text (truncate y1);
1837 state.ghyll <- gf (n+1) y1;
1839 match o with
1840 | None -> go n
1841 | Some y' -> set (_N/2, 1, 1) y' state.y
1843 gf 0 (float state.y)
1846 match conf.ghyllscroll with
1847 | None ->
1848 gotoy_and_clear_text y
1849 | Some nab ->
1850 if state.ghyll == noghyll
1851 then set nab y state.y
1852 else state.ghyll (Some y)
1855 let gotopage n top =
1856 let y, h = getpageyh n in
1857 let y = y + (truncate (top *. float h)) in
1858 gotoghyll y
1861 let gotopage1 n top =
1862 let y = getpagey n in
1863 let y = y + top in
1864 gotoghyll y
1867 let invalidate s f =
1868 state.layout <- [];
1869 state.pdims <- [];
1870 state.rects <- [];
1871 state.rects1 <- [];
1872 match state.geomcmds with
1873 | ps, [] when String.length ps = 0 ->
1874 f ();
1875 state.geomcmds <- s, [];
1877 | ps, [] ->
1878 state.geomcmds <- ps, [s, f];
1880 | ps, (s', _) :: rest when s' = s ->
1881 state.geomcmds <- ps, ((s, f) :: rest);
1883 | ps, cmds ->
1884 state.geomcmds <- ps, ((s, f) :: cmds);
1887 let flushpages () =
1888 Hashtbl.iter (fun _ opaque ->
1889 wcmd "freepage %s" opaque;
1890 ) state.pagemap;
1891 Hashtbl.clear state.pagemap;
1894 let opendoc path password =
1895 state.path <- path;
1896 state.password <- password;
1897 state.gen <- state.gen + 1;
1898 state.docinfo <- [];
1900 flushpages ();
1901 setaalevel conf.aalevel;
1902 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1903 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1904 invalidate "reqlayout"
1905 (fun () ->
1906 wcmd "reqlayout %d %d %s\000"
1907 conf.angle (btod conf.proportional) state.nameddest;
1911 let reload () =
1912 state.anchor <- getanchor ();
1913 state.wthack <- !wtmode;
1914 opendoc state.path state.password;
1917 let scalecolor c =
1918 let c = c *. conf.colorscale in
1919 (c, c, c);
1922 let scalecolor2 (r, g, b) =
1923 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1926 let docolumns = function
1927 | Csingle _ ->
1928 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1929 let rec loop pageno pdimno pdim y ph pdims =
1930 if pageno = state.pagecount
1931 then ()
1932 else
1933 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1934 match pdims with
1935 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1936 pdimno+1, pdim, rest
1937 | _ ->
1938 pdimno, pdim, pdims
1940 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1941 let y = y +
1942 (if conf.presentation
1943 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1944 else (if pageno = 0 then 0 else conf.interpagespace)
1947 a.(pageno) <- (pdimno, x, y, pdim);
1948 loop (pageno+1) pdimno pdim (y + h) h pdims
1950 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1951 conf.columns <- Csingle a;
1953 | Cmulti ((columns, coverA, coverB), _) ->
1954 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1955 let rec loop pageno pdimno pdim x y rowh pdims =
1956 let rec fixrow m = if m = pageno then () else
1957 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1958 if h < rowh
1959 then (
1960 let y = y + (rowh - h) / 2 in
1961 a.(m) <- (pdimno, x, y, pdim);
1963 fixrow (m+1)
1965 if pageno = state.pagecount
1966 then fixrow (((pageno - 1) / columns) * columns)
1967 else
1968 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1969 match pdims with
1970 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1971 pdimno+1, pdim, rest
1972 | _ ->
1973 pdimno, pdim, pdims
1975 let x, y, rowh' =
1976 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1977 then (
1978 let x = (conf.winw - state.scrollw - w) / 2 in
1979 let ips =
1980 if conf.presentation then calcips h else conf.interpagespace in
1981 x, y + ips + rowh, h
1983 else (
1984 if (pageno - coverA) mod columns = 0
1985 then (
1986 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1987 let y =
1988 if conf.presentation
1989 then
1990 let ips = calcips h in
1991 y + (if pageno = 0 then 0 else calcips rowh + ips)
1992 else
1993 y + (if pageno = 0 then 0 else conf.interpagespace)
1995 x, y + rowh, h
1997 else x, y, max rowh h
2000 let y =
2001 if pageno > 1 && (pageno - coverA) mod columns = 0
2002 then (
2003 let y =
2004 if pageno = columns && conf.presentation
2005 then (
2006 let ips = calcips rowh in
2007 for i = 0 to pred columns
2009 let (pdimno, x, y, pdim) = a.(i) in
2010 a.(i) <- (pdimno, x, y+ips, pdim)
2011 done;
2012 y+ips;
2014 else y
2016 fixrow (pageno - columns);
2019 else y
2021 a.(pageno) <- (pdimno, x, y, pdim);
2022 let x = x + w + xoff*2 + conf.interpagespace in
2023 loop (pageno+1) pdimno pdim x y rowh' pdims
2025 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2026 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2028 | Csplit (c, _) ->
2029 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2030 let rec loop pageno pdimno pdim y pdims =
2031 if pageno = state.pagecount
2032 then ()
2033 else
2034 let pdimno, ((_, w, h, _) as pdim), pdims =
2035 match pdims with
2036 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2037 pdimno+1, pdim, rest
2038 | _ ->
2039 pdimno, pdim, pdims
2041 let cw = w / c in
2042 let rec loop1 n x y =
2043 if n = c then y else (
2044 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2045 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2048 let y = loop1 0 0 y in
2049 loop (pageno+1) pdimno pdim y pdims
2051 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2052 conf.columns <- Csplit (c, a);
2055 let represent () =
2056 docolumns conf.columns;
2057 state.maxy <- calcheight ();
2058 state.hscrollh <-
2059 if state.w <= conf.winw - state.scrollw
2060 then 0
2061 else state.scrollw
2063 let wthack = state.wthack in
2064 begin match state.mode with
2065 | Birdseye (_, _, pageno, _, _) ->
2066 let y, h = getpageyh pageno in
2067 let top = (conf.winh - h) / 2 in
2068 gotoy (max 0 (y - top))
2069 | _ -> gotoanchor state.anchor
2070 end;
2071 state.wthack <- wthack;
2074 let reshape w h =
2075 state.wthack <- false;
2076 GlDraw.viewport 0 0 w h;
2077 let firsttime = state.geomcmds == firstgeomcmds in
2078 if not firsttime && nogeomcmds state.geomcmds
2079 then state.anchor <- getanchor ();
2081 conf.winw <- w;
2082 let w = truncate (float w *. conf.zoom) - state.scrollw in
2083 let w = max w 2 in
2084 conf.winh <- h;
2085 setfontsize fstate.fontsize;
2086 GlMat.mode `modelview;
2087 GlMat.load_identity ();
2089 GlMat.mode `projection;
2090 GlMat.load_identity ();
2091 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2092 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2093 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2095 let relx =
2096 if conf.zoom <= 1.0
2097 then 0.0
2098 else float state.x /. float state.w
2100 invalidate "geometry"
2101 (fun () ->
2102 state.w <- w;
2103 if not firsttime
2104 then state.x <- truncate (relx *. float w);
2105 let w =
2106 match conf.columns with
2107 | Csingle _ -> w
2108 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2109 | Csplit (c, _) -> w * c
2111 wcmd "geometry %d %d" w h);
2114 let enttext () =
2115 let len = String.length state.text in
2116 let drawstring s =
2117 let hscrollh =
2118 match state.mode with
2119 | Textentry _
2120 | View ->
2121 let h, _, _ = state.uioh#scrollpw in
2123 | _ -> 0
2125 let rect x w =
2126 GlDraw.rect
2127 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2128 (x+.w, float (conf.winh - hscrollh))
2131 let w = float (conf.winw - state.scrollw - 1) in
2132 if state.progress >= 0.0 && state.progress < 1.0
2133 then (
2134 GlDraw.color (0.3, 0.3, 0.3);
2135 let w1 = w *. state.progress in
2136 rect 0.0 w1;
2137 GlDraw.color (0.0, 0.0, 0.0);
2138 rect w1 (w-.w1)
2140 else (
2141 GlDraw.color (0.0, 0.0, 0.0);
2142 rect 0.0 w;
2145 GlDraw.color (1.0, 1.0, 1.0);
2146 drawstring fstate.fontsize
2147 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2149 let s =
2150 match state.mode with
2151 | Textentry ((prefix, text, _, _, _, _), _) ->
2152 let s =
2153 if len > 0
2154 then
2155 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2156 else
2157 Printf.sprintf "%s%s_" prefix text
2161 | _ -> state.text
2163 let s =
2164 if state.newerrmsgs
2165 then (
2166 if not (istextentry state.mode)
2167 then
2168 let s1 = "(press 'e' to review error messasges)" in
2169 if String.length s > 0 then s ^ " " ^ s1 else s1
2170 else s
2172 else s
2174 if String.length s > 0
2175 then drawstring s
2178 let gctiles () =
2179 let len = Queue.length state.tilelru in
2180 let layout = lazy (
2181 match state.throttle with
2182 | None ->
2183 if conf.preload
2184 then preloadlayout state.y
2185 else state.layout
2186 | Some (layout, _, _) ->
2187 layout
2188 ) in
2189 let rec loop qpos =
2190 if state.memused <= conf.memlimit
2191 then ()
2192 else (
2193 if qpos < len
2194 then
2195 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2196 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2197 let (_, pw, ph, _) = getpagedim n in
2199 gen = state.gen
2200 && colorspace = conf.colorspace
2201 && angle = conf.angle
2202 && pagew = pw
2203 && pageh = ph
2204 && (
2205 let x = col*conf.tilew
2206 and y = row*conf.tileh in
2207 tilevisible (Lazy.force_val layout) n x y
2209 then Queue.push lruitem state.tilelru
2210 else (
2211 freepbo p;
2212 wcmd "freetile %s" p;
2213 state.memused <- state.memused - s;
2214 state.uioh#infochanged Memused;
2215 Hashtbl.remove state.tilemap k;
2217 loop (qpos+1)
2220 loop 0
2223 let flushtiles () =
2224 Queue.iter (fun (k, p, s) ->
2225 wcmd "freetile %s" p;
2226 state.memused <- state.memused - s;
2227 state.uioh#infochanged Memused;
2228 Hashtbl.remove state.tilemap k;
2229 ) state.tilelru;
2230 Queue.clear state.tilelru;
2231 load state.layout;
2234 let logcurrently = function
2235 | Idle -> dolog "Idle"
2236 | Loading (l, gen) ->
2237 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2238 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2239 dolog
2240 "Tiling %d[%d,%d] page=%s cs=%s angle"
2241 l.pageno col row pageopaque
2242 (colorspace_to_string colorspace)
2244 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2245 angle gen conf.angle state.gen
2246 tilew tileh
2247 conf.tilew conf.tileh
2249 | Outlining _ ->
2250 dolog "outlining"
2253 let act cmds =
2254 (* dolog "%S" cmds; *)
2255 let op, args =
2256 let spacepos =
2257 try String.index cmds ' '
2258 with Not_found -> -1
2260 if spacepos = -1
2261 then cmds, ""
2262 else
2263 let l = String.length cmds in
2264 let op = String.sub cmds 0 spacepos in
2265 op, begin
2266 if l - spacepos < 2 then ""
2267 else String.sub cmds (spacepos+1) (l-spacepos-1)
2270 match op with
2271 | "clear" ->
2272 state.uioh#infochanged Pdim;
2273 state.pdims <- [];
2275 | "clearrects" ->
2276 state.rects <- state.rects1;
2277 G.postRedisplay "clearrects";
2279 | "continue" ->
2280 let n =
2281 try Scanf.sscanf args "%u" (fun n -> n)
2282 with exn ->
2283 dolog "error processing 'continue' %S: %s"
2284 cmds (Printexc.to_string exn);
2285 exit 1;
2287 state.pagecount <- n;
2288 begin match state.currently with
2289 | Outlining l ->
2290 state.currently <- Idle;
2291 state.outlines <- Array.of_list (List.rev l)
2292 | _ -> ()
2293 end;
2295 let cur, cmds = state.geomcmds in
2296 if String.length cur = 0
2297 then failwith "umpossible";
2299 begin match List.rev cmds with
2300 | [] ->
2301 state.geomcmds <- "", [];
2302 represent ();
2303 | (s, f) :: rest ->
2304 f ();
2305 state.geomcmds <- s, List.rev rest;
2306 end;
2307 if conf.maxwait = None
2308 then G.postRedisplay "continue";
2310 | "title" ->
2311 Wsi.settitle args
2313 | "msg" ->
2314 showtext ' ' args
2316 | "vmsg" ->
2317 if conf.verbose
2318 then showtext ' ' args
2320 | "emsg" ->
2321 Buffer.add_string state.errmsgs args;
2322 state.newerrmsgs <- true;
2323 G.postRedisplay "error message"
2325 | "progress" ->
2326 let progress, text =
2328 Scanf.sscanf args "%f %n"
2329 (fun f pos ->
2330 f, String.sub args pos (String.length args - pos))
2331 with exn ->
2332 dolog "error processing 'progress' %S: %s"
2333 cmds (Printexc.to_string exn);
2334 exit 1;
2336 state.text <- text;
2337 state.progress <- progress;
2338 G.postRedisplay "progress"
2340 | "firstmatch" ->
2341 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2343 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2344 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2345 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2346 with exn ->
2347 dolog "error processing 'firstmatch' %S: %s"
2348 cmds (Printexc.to_string exn);
2349 exit 1;
2351 let y = (getpagey pageno) + truncate y0 in
2352 addnav ();
2353 gotoy y;
2354 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2356 | "match" ->
2357 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2359 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2360 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2361 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2362 with exn ->
2363 dolog "error processing 'match' %S: %s"
2364 cmds (Printexc.to_string exn);
2365 exit 1;
2367 state.rects1 <-
2368 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2370 | "page" ->
2371 let pageopaque, t =
2373 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2374 with exn ->
2375 dolog "error processing 'page' %S: %s"
2376 cmds (Printexc.to_string exn);
2377 exit 1;
2379 begin match state.currently with
2380 | Loading (l, gen) ->
2381 vlog "page %d took %f sec" l.pageno t;
2382 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2383 begin match state.throttle with
2384 | None ->
2385 let preloadedpages =
2386 if conf.preload
2387 then preloadlayout state.y
2388 else state.layout
2390 let evict () =
2391 let module IntSet =
2392 Set.Make (struct type t = int let compare = (-) end) in
2393 let set =
2394 List.fold_left (fun s l -> IntSet.add l.pageno s)
2395 IntSet.empty preloadedpages
2397 let evictedpages =
2398 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2399 if not (IntSet.mem pageno set)
2400 then (
2401 wcmd "freepage %s" opaque;
2402 key :: accu
2404 else accu
2405 ) state.pagemap []
2407 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2409 evict ();
2410 state.currently <- Idle;
2411 if gen = state.gen
2412 then (
2413 tilepage l.pageno pageopaque state.layout;
2414 load state.layout;
2415 load preloadedpages;
2416 if pagevisible state.layout l.pageno
2417 && layoutready state.layout
2418 then G.postRedisplay "page";
2421 | Some (layout, _, _) ->
2422 state.currently <- Idle;
2423 tilepage l.pageno pageopaque layout;
2424 load state.layout
2425 end;
2427 | _ ->
2428 dolog "Inconsistent loading state";
2429 logcurrently state.currently;
2430 exit 1
2433 | "tile" ->
2434 let (x, y, opaque, size, t) =
2436 Scanf.sscanf args "%u %u %s %u %f"
2437 (fun x y p size t -> (x, y, p, size, t))
2438 with exn ->
2439 dolog "error processing 'tile' %S: %s"
2440 cmds (Printexc.to_string exn);
2441 exit 1;
2443 begin match state.currently with
2444 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2445 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2447 unmappbo opaque;
2448 if tilew != conf.tilew || tileh != conf.tileh
2449 then (
2450 wcmd "freetile %s" opaque;
2451 state.currently <- Idle;
2452 load state.layout;
2454 else (
2455 puttileopaque l col row gen cs angle opaque size t;
2456 state.memused <- state.memused + size;
2457 state.uioh#infochanged Memused;
2458 gctiles ();
2459 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2460 opaque, size) state.tilelru;
2462 let layout =
2463 match state.throttle with
2464 | None -> state.layout
2465 | Some (layout, _, _) -> layout
2468 state.currently <- Idle;
2469 if gen = state.gen
2470 && conf.colorspace = cs
2471 && conf.angle = angle
2472 && tilevisible layout l.pageno x y
2473 then conttiling l.pageno pageopaque;
2475 begin match state.throttle with
2476 | None ->
2477 if state.wthack
2478 then state.wthack <- not (layoutready state.layout);
2479 preload state.layout;
2480 if gen = state.gen
2481 && conf.colorspace = cs
2482 && conf.angle = angle
2483 && tilevisible state.layout l.pageno x y
2484 then G.postRedisplay "tile nothrottle";
2486 | Some (layout, y, _) ->
2487 let ready = layoutready layout in
2488 if ready
2489 then (
2490 state.wthack <- false;
2491 state.y <- y;
2492 state.layout <- layout;
2493 state.throttle <- None;
2494 G.postRedisplay "throttle";
2496 else load layout;
2497 end;
2500 | _ ->
2501 dolog "Inconsistent tiling state";
2502 logcurrently state.currently;
2503 exit 1
2506 | "pdim" ->
2507 let pdim =
2509 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2510 with exn ->
2511 dolog "error processing 'pdim' %S: %s"
2512 cmds (Printexc.to_string exn);
2513 exit 1;
2515 state.uioh#infochanged Pdim;
2516 state.pdims <- pdim :: state.pdims
2518 | "o" ->
2519 let (l, n, t, h, pos) =
2521 Scanf.sscanf args "%u %u %d %u %n"
2522 (fun l n t h pos -> l, n, t, h, pos)
2523 with exn ->
2524 dolog "error processing 'o' %S: %s"
2525 cmds (Printexc.to_string exn);
2526 exit 1;
2528 let s = String.sub args pos (String.length args - pos) in
2529 let outline = (s, l, (n, float t /. float h, 0.0)) in
2530 begin match state.currently with
2531 | Outlining outlines ->
2532 state.currently <- Outlining (outline :: outlines)
2533 | Idle ->
2534 state.currently <- Outlining [outline]
2535 | currently ->
2536 dolog "invalid outlining state";
2537 logcurrently currently
2540 | "a" ->
2541 let (n, t, h) =
2543 Scanf.sscanf args "%u %u %d"
2544 (fun n t h -> n, t, h)
2545 with exn ->
2546 dolog "error processing 'a' %S: %s"
2547 cmds (Printexc.to_string exn);
2548 exit 1;
2550 let top, dtop =
2551 if conf.presentation
2552 then (0.0, 1.0)
2553 else float t /. float h, 0.0
2555 state.anchor <- (n, top, dtop)
2557 | "info" ->
2558 state.docinfo <- (1, args) :: state.docinfo
2560 | "infoend" ->
2561 state.uioh#infochanged Docinfo;
2562 state.docinfo <- List.rev state.docinfo
2564 | _ ->
2565 dolog "unknown cmd `%S'" cmds
2568 let onhist cb =
2569 let rc = cb.rc in
2570 let action = function
2571 | HCprev -> cbget cb ~-1
2572 | HCnext -> cbget cb 1
2573 | HCfirst -> cbget cb ~-(cb.rc)
2574 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2575 and cancel () = cb.rc <- rc
2576 in (action, cancel)
2579 let search pattern forward =
2580 if String.length pattern > 0
2581 then
2582 let pn, py =
2583 match state.layout with
2584 | [] -> 0, 0
2585 | l :: _ ->
2586 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2588 wcmd "search %d %d %d %d,%s\000"
2589 (btod conf.icase) pn py (btod forward) pattern;
2592 let intentry text key =
2593 let c =
2594 if key >= 32 && key < 127
2595 then Char.chr key
2596 else '\000'
2598 match c with
2599 | '0' .. '9' ->
2600 let text = addchar text c in
2601 TEcont text
2603 | _ ->
2604 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2605 TEcont text
2608 let linknentry text key =
2609 let c =
2610 if key >= 32 && key < 127
2611 then Char.chr key
2612 else '\000'
2614 match c with
2615 | 'a' .. 'z' ->
2616 let text = addchar text c in
2617 TEcont text
2619 | _ ->
2620 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2621 TEcont text
2624 let linkndone f s =
2625 if String.length s > 0
2626 then (
2627 let n =
2628 let l = String.length s in
2629 let rec loop pos n = if pos = l then n else
2630 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2631 loop (pos+1) (n*26 + m)
2632 in loop 0 0
2634 let rec loop n = function
2635 | [] -> ()
2636 | l :: rest ->
2637 match getopaque l.pageno with
2638 | None -> loop n rest
2639 | Some opaque ->
2640 let m = getlinkcount opaque in
2641 if n < m
2642 then (
2643 let under = getlink opaque n in
2644 f under
2646 else loop (n-m) rest
2648 loop n state.layout;
2652 let textentry text key =
2653 if key land 0xff00 = 0xff00
2654 then TEcont text
2655 else TEcont (text ^ Wsi.toutf8 key)
2658 let reqlayout angle proportional =
2659 match state.throttle with
2660 | None ->
2661 if nogeomcmds state.geomcmds
2662 then state.anchor <- getanchor ();
2663 conf.angle <- angle mod 360;
2664 if conf.angle != 0
2665 then (
2666 match state.mode with
2667 | LinkNav _ -> state.mode <- View
2668 | _ -> ()
2670 conf.proportional <- proportional;
2671 invalidate "reqlayout"
2672 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2673 | _ -> ()
2676 let settrim trimmargins trimfuzz =
2677 if nogeomcmds state.geomcmds
2678 then state.anchor <- getanchor ();
2679 conf.trimmargins <- trimmargins;
2680 conf.trimfuzz <- trimfuzz;
2681 let x0, y0, x1, y1 = trimfuzz in
2682 invalidate "settrim"
2683 (fun () ->
2684 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2685 flushpages ();
2688 let setzoom zoom =
2689 match state.throttle with
2690 | None ->
2691 let zoom = max 0.01 zoom in
2692 if zoom <> conf.zoom
2693 then (
2694 state.prevzoom <- conf.zoom;
2695 conf.zoom <- zoom;
2696 reshape conf.winw conf.winh;
2697 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2700 | Some (layout, y, started) ->
2701 let time =
2702 match conf.maxwait with
2703 | None -> 0.0
2704 | Some t -> t
2706 let dt = now () -. started in
2707 if dt > time
2708 then (
2709 state.y <- y;
2710 load layout;
2714 let setcolumns mode columns coverA coverB =
2715 state.prevcolumns <- Some (conf.columns, conf.zoom);
2716 if columns < 0
2717 then (
2718 if isbirdseye mode
2719 then showtext '!' "split mode doesn't work in bird's eye"
2720 else (
2721 conf.columns <- Csplit (-columns, [||]);
2722 state.x <- 0;
2723 conf.zoom <- 1.0;
2726 else (
2727 if columns < 2
2728 then (
2729 conf.columns <- Csingle [||];
2730 state.x <- 0;
2731 setzoom 1.0;
2733 else (
2734 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2735 conf.zoom <- 1.0;
2738 reshape conf.winw conf.winh;
2741 let enterbirdseye () =
2742 let zoom = float conf.thumbw /. float conf.winw in
2743 let birdseyepageno =
2744 let cy = conf.winh / 2 in
2745 let fold = function
2746 | [] -> 0
2747 | l :: rest ->
2748 let rec fold best = function
2749 | [] -> best.pageno
2750 | l :: rest ->
2751 let d = cy - (l.pagedispy + l.pagevh/2)
2752 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2753 if abs d < abs dbest
2754 then fold l rest
2755 else best.pageno
2756 in fold l rest
2758 fold state.layout
2760 state.mode <- Birdseye (
2761 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2763 conf.zoom <- zoom;
2764 conf.presentation <- false;
2765 conf.interpagespace <- 10;
2766 conf.hlinks <- false;
2767 state.x <- 0;
2768 state.mstate <- Mnone;
2769 conf.maxwait <- None;
2770 conf.columns <- (
2771 match conf.beyecolumns with
2772 | Some c ->
2773 conf.zoom <- 1.0;
2774 Cmulti ((c, 0, 0), [||])
2775 | None -> Csingle [||]
2777 Wsi.setcursor Wsi.CURSOR_INHERIT;
2778 if conf.verbose
2779 then
2780 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2781 (100.0*.zoom)
2782 else
2783 state.text <- ""
2785 reshape conf.winw conf.winh;
2788 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2789 state.mode <- View;
2790 conf.zoom <- c.zoom;
2791 conf.presentation <- c.presentation;
2792 conf.interpagespace <- c.interpagespace;
2793 conf.maxwait <- c.maxwait;
2794 conf.hlinks <- c.hlinks;
2795 conf.beyecolumns <- (
2796 match conf.columns with
2797 | Cmulti ((c, _, _), _) -> Some c
2798 | Csingle _ -> None
2799 | Csplit _ -> failwith "leaving bird's eye split mode"
2801 conf.columns <- (
2802 match c.columns with
2803 | Cmulti (c, _) -> Cmulti (c, [||])
2804 | Csingle _ -> Csingle [||]
2805 | Csplit (c, _) -> Csplit (c, [||])
2807 state.x <- leftx;
2808 if conf.verbose
2809 then
2810 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2811 (100.0*.conf.zoom)
2813 reshape conf.winw conf.winh;
2814 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2817 let togglebirdseye () =
2818 match state.mode with
2819 | Birdseye vals -> leavebirdseye vals true
2820 | View -> enterbirdseye ()
2821 | _ -> ()
2824 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2825 let pageno = max 0 (pageno - incr) in
2826 let rec loop = function
2827 | [] -> gotopage1 pageno 0
2828 | l :: _ when l.pageno = pageno ->
2829 if l.pagedispy >= 0 && l.pagey = 0
2830 then G.postRedisplay "upbirdseye"
2831 else gotopage1 pageno 0
2832 | _ :: rest -> loop rest
2834 loop state.layout;
2835 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2838 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2839 let pageno = min (state.pagecount - 1) (pageno + incr) in
2840 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2841 let rec loop = function
2842 | [] ->
2843 let y, h = getpageyh pageno in
2844 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2845 gotoy (clamp dy)
2846 | l :: _ when l.pageno = pageno ->
2847 if l.pagevh != l.pageh
2848 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2849 else G.postRedisplay "downbirdseye"
2850 | _ :: rest -> loop rest
2852 loop state.layout
2855 let optentry mode _ key =
2856 let btos b = if b then "on" else "off" in
2857 if key >= 32 && key < 127
2858 then
2859 let c = Char.chr key in
2860 match c with
2861 | 's' ->
2862 let ondone s =
2863 try conf.scrollstep <- int_of_string s with exc ->
2864 state.text <- Printf.sprintf "bad integer `%s': %s"
2865 s (Printexc.to_string exc)
2867 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2869 | 'A' ->
2870 let ondone s =
2872 conf.autoscrollstep <- int_of_string s;
2873 if state.autoscroll <> None
2874 then state.autoscroll <- Some conf.autoscrollstep
2875 with exc ->
2876 state.text <- Printf.sprintf "bad integer `%s': %s"
2877 s (Printexc.to_string exc)
2879 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2881 | 'C' ->
2882 let ondone s =
2884 let n, a, b = multicolumns_of_string s in
2885 setcolumns mode n a b;
2886 with exc ->
2887 state.text <- Printf.sprintf "bad columns `%s': %s"
2888 s (Printexc.to_string exc)
2890 TEswitch ("columns: ", "", None, textentry, ondone, true)
2892 | 'Z' ->
2893 let ondone s =
2895 let zoom = float (int_of_string s) /. 100.0 in
2896 setzoom zoom
2897 with exc ->
2898 state.text <- Printf.sprintf "bad integer `%s': %s"
2899 s (Printexc.to_string exc)
2901 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2903 | 't' ->
2904 let ondone s =
2906 conf.thumbw <- bound (int_of_string s) 2 4096;
2907 state.text <-
2908 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2909 begin match mode with
2910 | Birdseye beye ->
2911 leavebirdseye beye false;
2912 enterbirdseye ();
2913 | _ -> ();
2915 with exc ->
2916 state.text <- Printf.sprintf "bad integer `%s': %s"
2917 s (Printexc.to_string exc)
2919 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2921 | 'R' ->
2922 let ondone s =
2923 match try
2924 Some (int_of_string s)
2925 with exc ->
2926 state.text <- Printf.sprintf "bad integer `%s': %s"
2927 s (Printexc.to_string exc);
2928 None
2929 with
2930 | Some angle -> reqlayout angle conf.proportional
2931 | None -> ()
2933 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2935 | 'i' ->
2936 conf.icase <- not conf.icase;
2937 TEdone ("case insensitive search " ^ (btos conf.icase))
2939 | 'p' ->
2940 conf.preload <- not conf.preload;
2941 gotoy state.y;
2942 TEdone ("preload " ^ (btos conf.preload))
2944 | 'v' ->
2945 conf.verbose <- not conf.verbose;
2946 TEdone ("verbose " ^ (btos conf.verbose))
2948 | 'd' ->
2949 conf.debug <- not conf.debug;
2950 TEdone ("debug " ^ (btos conf.debug))
2952 | 'h' ->
2953 conf.maxhfit <- not conf.maxhfit;
2954 state.maxy <- calcheight ();
2955 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2957 | 'c' ->
2958 conf.crophack <- not conf.crophack;
2959 TEdone ("crophack " ^ btos conf.crophack)
2961 | 'a' ->
2962 let s =
2963 match conf.maxwait with
2964 | None ->
2965 conf.maxwait <- Some infinity;
2966 "always wait for page to complete"
2967 | Some _ ->
2968 conf.maxwait <- None;
2969 "show placeholder if page is not ready"
2971 TEdone s
2973 | 'f' ->
2974 conf.underinfo <- not conf.underinfo;
2975 TEdone ("underinfo " ^ btos conf.underinfo)
2977 | 'P' ->
2978 conf.savebmarks <- not conf.savebmarks;
2979 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2981 | 'S' ->
2982 let ondone s =
2984 let pageno, py =
2985 match state.layout with
2986 | [] -> 0, 0
2987 | l :: _ ->
2988 l.pageno, l.pagey
2990 conf.interpagespace <- int_of_string s;
2991 docolumns conf.columns;
2992 state.maxy <- calcheight ();
2993 let y = getpagey pageno in
2994 gotoy (y + py)
2995 with exc ->
2996 state.text <- Printf.sprintf "bad integer `%s': %s"
2997 s (Printexc.to_string exc)
2999 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3001 | 'l' ->
3002 reqlayout conf.angle (not conf.proportional);
3003 TEdone ("proportional display " ^ btos conf.proportional)
3005 | 'T' ->
3006 settrim (not conf.trimmargins) conf.trimfuzz;
3007 TEdone ("trim margins " ^ btos conf.trimmargins)
3009 | 'I' ->
3010 conf.invert <- not conf.invert;
3011 TEdone ("invert colors " ^ btos conf.invert)
3013 | 'x' ->
3014 let ondone s =
3015 cbput state.hists.sel s;
3016 conf.selcmd <- s;
3018 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3019 textentry, ondone, true)
3021 | _ ->
3022 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3023 TEstop
3024 else
3025 TEcont state.text
3028 class type lvsource = object
3029 method getitemcount : int
3030 method getitem : int -> (string * int)
3031 method hasaction : int -> bool
3032 method exit :
3033 uioh:uioh ->
3034 cancel:bool ->
3035 active:int ->
3036 first:int ->
3037 pan:int ->
3038 qsearch:string ->
3039 uioh option
3040 method getactive : int
3041 method getfirst : int
3042 method getqsearch : string
3043 method setqsearch : string -> unit
3044 method getpan : int
3045 end;;
3047 class virtual lvsourcebase = object
3048 val mutable m_active = 0
3049 val mutable m_first = 0
3050 val mutable m_qsearch = ""
3051 val mutable m_pan = 0
3052 method getactive = m_active
3053 method getfirst = m_first
3054 method getqsearch = m_qsearch
3055 method getpan = m_pan
3056 method setqsearch s = m_qsearch <- s
3057 end;;
3059 let withoutlastutf8 s =
3060 let len = String.length s in
3061 if len = 0
3062 then s
3063 else
3064 let rec find pos =
3065 if pos = 0
3066 then pos
3067 else
3068 let b = Char.code s.[pos] in
3069 if b land 0b11000000 = 0b11000000
3070 then pos
3071 else find (pos-1)
3073 let first =
3074 if Char.code s.[len-1] land 0x80 = 0
3075 then len-1
3076 else find (len-1)
3078 String.sub s 0 first;
3081 let textentrykeyboard
3082 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3083 let key =
3084 if key >= 0xffb0 && key <= 0xffb9
3085 then key - 0xffb0 + 48 else key
3087 let enttext te =
3088 state.mode <- Textentry (te, onleave);
3089 state.text <- "";
3090 enttext ();
3091 G.postRedisplay "textentrykeyboard enttext";
3093 let histaction cmd =
3094 match opthist with
3095 | None -> ()
3096 | Some (action, _) ->
3097 state.mode <- Textentry (
3098 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3100 G.postRedisplay "textentry histaction"
3102 match key with
3103 | 0xff08 -> (* backspace *)
3104 let s = withoutlastutf8 text in
3105 let len = String.length s in
3106 if cancelonempty && len = 0
3107 then (
3108 onleave Cancel;
3109 G.postRedisplay "textentrykeyboard after cancel";
3111 else (
3112 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3115 | 0xff0d | 0xff8d -> (* (kp) enter *)
3116 ondone text;
3117 onleave Confirm;
3118 G.postRedisplay "textentrykeyboard after confirm"
3120 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3121 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3122 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3123 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3125 | 0xff1b -> (* escape*)
3126 if String.length text = 0
3127 then (
3128 begin match opthist with
3129 | None -> ()
3130 | Some (_, onhistcancel) -> onhistcancel ()
3131 end;
3132 onleave Cancel;
3133 state.text <- "";
3134 G.postRedisplay "textentrykeyboard after cancel2"
3136 else (
3137 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3140 | 0xff9f | 0xffff -> () (* delete *)
3142 | _ when key != 0
3143 && key land 0xff00 != 0xff00 (* keyboard *)
3144 && key land 0xfe00 != 0xfe00 (* xkb *)
3145 && key land 0xfd00 != 0xfd00 (* 3270 *)
3147 begin match onkey text key with
3148 | TEdone text ->
3149 ondone text;
3150 onleave Confirm;
3151 G.postRedisplay "textentrykeyboard after confirm2";
3153 | TEcont text ->
3154 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3156 | TEstop ->
3157 onleave Cancel;
3158 G.postRedisplay "textentrykeyboard after cancel3"
3160 | TEswitch te ->
3161 state.mode <- Textentry (te, onleave);
3162 G.postRedisplay "textentrykeyboard switch";
3163 end;
3165 | _ ->
3166 vlog "unhandled key %s" (Wsi.keyname key)
3169 let firstof first active =
3170 if first > active || abs (first - active) > fstate.maxrows - 1
3171 then max 0 (active - (fstate.maxrows/2))
3172 else first
3175 let calcfirst first active =
3176 if active > first
3177 then
3178 let rows = active - first in
3179 if rows > fstate.maxrows then active - fstate.maxrows else first
3180 else active
3183 let scrollph y maxy =
3184 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3185 let sh = float conf.winh /. sh in
3186 let sh = max sh (float conf.scrollh) in
3188 let percent =
3189 if y = state.maxy
3190 then 1.0
3191 else float y /. float maxy
3193 let position = (float conf.winh -. sh) *. percent in
3195 let position =
3196 if position +. sh > float conf.winh
3197 then float conf.winh -. sh
3198 else position
3200 position, sh;
3203 let coe s = (s :> uioh);;
3205 class listview ~(source:lvsource) ~trusted ~modehash =
3206 object (self)
3207 val m_pan = source#getpan
3208 val m_first = source#getfirst
3209 val m_active = source#getactive
3210 val m_qsearch = source#getqsearch
3211 val m_prev_uioh = state.uioh
3213 method private elemunder y =
3214 let n = y / (fstate.fontsize+1) in
3215 if m_first + n < source#getitemcount
3216 then (
3217 if source#hasaction (m_first + n)
3218 then Some (m_first + n)
3219 else None
3221 else None
3223 method display =
3224 Gl.enable `blend;
3225 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3226 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3227 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3228 GlDraw.color (1., 1., 1.);
3229 Gl.enable `texture_2d;
3230 let fs = fstate.fontsize in
3231 let nfs = fs + 1 in
3232 let ww = fstate.wwidth in
3233 let tabw = 30.0*.ww in
3234 let itemcount = source#getitemcount in
3235 let rec loop row =
3236 if (row - m_first) > fstate.maxrows
3237 then ()
3238 else (
3239 if row >= 0 && row < itemcount
3240 then (
3241 let (s, level) = source#getitem row in
3242 let y = (row - m_first) * nfs in
3243 let x = 5.0 +. float (level + m_pan) *. ww in
3244 if row = m_active
3245 then (
3246 Gl.disable `texture_2d;
3247 GlDraw.polygon_mode `both `line;
3248 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3249 GlDraw.rect (1., float (y + 1))
3250 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3251 GlDraw.polygon_mode `both `fill;
3252 GlDraw.color (1., 1., 1.);
3253 Gl.enable `texture_2d;
3256 let drawtabularstring s =
3257 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3258 if trusted
3259 then
3260 let tabpos = try String.index s '\t' with Not_found -> -1 in
3261 if tabpos > 0
3262 then
3263 let len = String.length s - tabpos - 1 in
3264 let s1 = String.sub s 0 tabpos
3265 and s2 = String.sub s (tabpos + 1) len in
3266 let nx = drawstr x s1 in
3267 let sw = nx -. x in
3268 let x = x +. (max tabw sw) in
3269 drawstr x s2
3270 else
3271 drawstr x s
3272 else
3273 drawstr x s
3275 let _ = drawtabularstring s in
3276 loop (row+1)
3280 loop m_first;
3281 Gl.disable `blend;
3282 Gl.disable `texture_2d;
3284 method updownlevel incr =
3285 let len = source#getitemcount in
3286 let curlevel =
3287 if m_active >= 0 && m_active < len
3288 then snd (source#getitem m_active)
3289 else -1
3291 let rec flow i =
3292 if i = len then i-1 else if i = -1 then 0 else
3293 let _, l = source#getitem i in
3294 if l != curlevel then i else flow (i+incr)
3296 let active = flow m_active in
3297 let first = calcfirst m_first active in
3298 G.postRedisplay "outline updownlevel";
3299 {< m_active = active; m_first = first >}
3301 method private key1 key mask =
3302 let set1 active first qsearch =
3303 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3305 let search active pattern incr =
3306 let dosearch re =
3307 let rec loop n =
3308 if n >= 0 && n < source#getitemcount
3309 then (
3310 let s, _ = source#getitem n in
3312 (try ignore (Str.search_forward re s 0); true
3313 with Not_found -> false)
3314 then Some n
3315 else loop (n + incr)
3317 else None
3319 loop active
3322 let re = Str.regexp_case_fold pattern in
3323 dosearch re
3324 with Failure s ->
3325 state.text <- s;
3326 None
3328 let itemcount = source#getitemcount in
3329 let find start incr =
3330 let rec find i =
3331 if i = -1 || i = itemcount
3332 then -1
3333 else (
3334 if source#hasaction i
3335 then i
3336 else find (i + incr)
3339 find start
3341 let set active first =
3342 let first = bound first 0 (itemcount - fstate.maxrows) in
3343 state.text <- "";
3344 coe {< m_active = active; m_first = first >}
3346 let navigate incr =
3347 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3348 let active, first =
3349 let incr1 = if incr > 0 then 1 else -1 in
3350 if isvisible m_first m_active
3351 then
3352 let next =
3353 let next = m_active + incr in
3354 let next =
3355 if next < 0 || next >= itemcount
3356 then -1
3357 else find next incr1
3359 if next = -1 || abs (m_active - next) > fstate.maxrows
3360 then -1
3361 else next
3363 if next = -1
3364 then
3365 let first = m_first + incr in
3366 let first = bound first 0 (itemcount - 1) in
3367 let next =
3368 let next = m_active + incr in
3369 let next = bound next 0 (itemcount - 1) in
3370 find next ~-incr1
3372 let active = if next = -1 then m_active else next in
3373 active, first
3374 else
3375 let first = min next m_first in
3376 let first =
3377 if abs (next - first) > fstate.maxrows
3378 then first + incr
3379 else first
3381 next, first
3382 else
3383 let first = m_first + incr in
3384 let first = bound first 0 (itemcount - 1) in
3385 let active =
3386 let next = m_active + incr in
3387 let next = bound next 0 (itemcount - 1) in
3388 let next = find next incr1 in
3389 let active =
3390 if next = -1 || abs (m_active - first) > fstate.maxrows
3391 then (
3392 let active = if m_active = -1 then next else m_active in
3393 active
3395 else next
3397 if isvisible first active
3398 then active
3399 else -1
3401 active, first
3403 G.postRedisplay "listview navigate";
3404 set active first;
3406 match key with
3407 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3408 let incr = if key = 0x72 then -1 else 1 in
3409 let active, first =
3410 match search (m_active + incr) m_qsearch incr with
3411 | None ->
3412 state.text <- m_qsearch ^ " [not found]";
3413 m_active, m_first
3414 | Some active ->
3415 state.text <- m_qsearch;
3416 active, firstof m_first active
3418 G.postRedisplay "listview ctrl-r/s";
3419 set1 active first m_qsearch;
3421 | 0xff08 -> (* backspace *)
3422 if String.length m_qsearch = 0
3423 then coe self
3424 else (
3425 let qsearch = withoutlastutf8 m_qsearch in
3426 let len = String.length qsearch in
3427 if len = 0
3428 then (
3429 state.text <- "";
3430 G.postRedisplay "listview empty qsearch";
3431 set1 m_active m_first "";
3433 else
3434 let active, first =
3435 match search m_active qsearch ~-1 with
3436 | None ->
3437 state.text <- qsearch ^ " [not found]";
3438 m_active, m_first
3439 | Some active ->
3440 state.text <- qsearch;
3441 active, firstof m_first active
3443 G.postRedisplay "listview backspace qsearch";
3444 set1 active first qsearch
3447 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3448 let pattern = m_qsearch ^ Wsi.toutf8 key in
3449 let active, first =
3450 match search m_active pattern 1 with
3451 | None ->
3452 state.text <- pattern ^ " [not found]";
3453 m_active, m_first
3454 | Some active ->
3455 state.text <- pattern;
3456 active, firstof m_first active
3458 G.postRedisplay "listview qsearch add";
3459 set1 active first pattern;
3461 | 0xff1b -> (* escape *)
3462 state.text <- "";
3463 if String.length m_qsearch = 0
3464 then (
3465 G.postRedisplay "list view escape";
3466 begin
3467 match
3468 source#exit (coe self) true m_active m_first m_pan m_qsearch
3469 with
3470 | None -> m_prev_uioh
3471 | Some uioh -> uioh
3474 else (
3475 G.postRedisplay "list view kill qsearch";
3476 source#setqsearch "";
3477 coe {< m_qsearch = "" >}
3480 | 0xff0d | 0xff8d -> (* (kp) enter *)
3481 state.text <- "";
3482 let self = {< m_qsearch = "" >} in
3483 source#setqsearch "";
3484 let opt =
3485 G.postRedisplay "listview enter";
3486 if m_active >= 0 && m_active < source#getitemcount
3487 then (
3488 source#exit (coe self) false m_active m_first m_pan "";
3490 else (
3491 source#exit (coe self) true m_active m_first m_pan "";
3494 begin match opt with
3495 | None -> m_prev_uioh
3496 | Some uioh -> uioh
3499 | 0xff9f | 0xffff -> (* (kp) delete *)
3500 coe self
3502 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3503 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3504 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3505 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3507 | 0xff53 | 0xff98 -> (* (kp) right *)
3508 state.text <- "";
3509 G.postRedisplay "listview right";
3510 coe {< m_pan = m_pan - 1 >}
3512 | 0xff51 | 0xff96 -> (* (kp) left *)
3513 state.text <- "";
3514 G.postRedisplay "listview left";
3515 coe {< m_pan = m_pan + 1 >}
3517 | 0xff50 | 0xff95 -> (* (kp) home *)
3518 let active = find 0 1 in
3519 G.postRedisplay "listview home";
3520 set active 0;
3522 | 0xff57 | 0xff9c -> (* (kp) end *)
3523 let first = max 0 (itemcount - fstate.maxrows) in
3524 let active = find (itemcount - 1) ~-1 in
3525 G.postRedisplay "listview end";
3526 set active first;
3528 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3529 coe self
3531 | _ ->
3532 dolog "listview unknown key %#x" key; coe self
3534 method key key mask =
3535 match state.mode with
3536 | Textentry te -> textentrykeyboard key mask te; coe self
3537 | _ -> self#key1 key mask
3539 method button button down x y _ =
3540 let opt =
3541 match button with
3542 | 1 when x > conf.winw - conf.scrollbw ->
3543 G.postRedisplay "listview scroll";
3544 if down
3545 then
3546 let _, position, sh = self#scrollph in
3547 if y > truncate position && y < truncate (position +. sh)
3548 then (
3549 state.mstate <- Mscrolly;
3550 Some (coe self)
3552 else
3553 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3554 let first = truncate (s *. float source#getitemcount) in
3555 let first = min source#getitemcount first in
3556 Some (coe {< m_first = first; m_active = first >})
3557 else (
3558 state.mstate <- Mnone;
3559 Some (coe self);
3561 | 1 when not down ->
3562 begin match self#elemunder y with
3563 | Some n ->
3564 G.postRedisplay "listview click";
3565 source#exit
3566 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3567 | _ ->
3568 Some (coe self)
3570 | n when (n == 4 || n == 5) && not down ->
3571 let len = source#getitemcount in
3572 let first =
3573 if n = 5 && m_first + fstate.maxrows >= len
3574 then
3575 m_first
3576 else
3577 let first = m_first + (if n == 4 then -1 else 1) in
3578 bound first 0 (len - 1)
3580 G.postRedisplay "listview wheel";
3581 Some (coe {< m_first = first >})
3582 | n when (n = 6 || n = 7) && not down ->
3583 let inc = m_first + (if n = 7 then -1 else 1) in
3584 G.postRedisplay "listview hwheel";
3585 Some (coe {< m_pan = m_pan + inc >})
3586 | _ ->
3587 Some (coe self)
3589 match opt with
3590 | None -> m_prev_uioh
3591 | Some uioh -> uioh
3593 method motion _ y =
3594 match state.mstate with
3595 | Mscrolly ->
3596 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3597 let first = truncate (s *. float source#getitemcount) in
3598 let first = min source#getitemcount first in
3599 G.postRedisplay "listview motion";
3600 coe {< m_first = first; m_active = first >}
3601 | _ -> coe self
3603 method pmotion x y =
3604 if x < conf.winw - conf.scrollbw
3605 then
3606 let n =
3607 match self#elemunder y with
3608 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3609 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3611 let o =
3612 if n != m_active
3613 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3614 else self
3616 coe o
3617 else (
3618 Wsi.setcursor Wsi.CURSOR_INHERIT;
3619 coe self
3622 method infochanged _ = ()
3624 method scrollpw = (0, 0.0, 0.0)
3625 method scrollph =
3626 let nfs = fstate.fontsize + 1 in
3627 let y = m_first * nfs in
3628 let itemcount = source#getitemcount in
3629 let maxi = max 0 (itemcount - fstate.maxrows) in
3630 let maxy = maxi * nfs in
3631 let p, h = scrollph y maxy in
3632 conf.scrollbw, p, h
3634 method modehash = modehash
3635 end;;
3637 class outlinelistview ~source =
3638 object (self)
3639 inherit listview
3640 ~source:(source :> lvsource)
3641 ~trusted:false
3642 ~modehash:(findkeyhash conf "outline")
3643 as super
3645 method key key mask =
3646 let calcfirst first active =
3647 if active > first
3648 then
3649 let rows = active - first in
3650 let maxrows =
3651 if String.length state.text = 0
3652 then fstate.maxrows
3653 else fstate.maxrows - 2
3655 if rows > maxrows then active - maxrows else first
3656 else active
3658 let navigate incr =
3659 let active = m_active + incr in
3660 let active = bound active 0 (source#getitemcount - 1) in
3661 let first = calcfirst m_first active in
3662 G.postRedisplay "outline navigate";
3663 coe {< m_active = active; m_first = first >}
3665 let ctrl = Wsi.withctrl mask in
3666 match key with
3667 | 110 when ctrl -> (* ctrl-n *)
3668 source#narrow m_qsearch;
3669 G.postRedisplay "outline ctrl-n";
3670 coe {< m_first = 0; m_active = 0 >}
3672 | 117 when ctrl -> (* ctrl-u *)
3673 source#denarrow;
3674 G.postRedisplay "outline ctrl-u";
3675 state.text <- "";
3676 coe {< m_first = 0; m_active = 0 >}
3678 | 108 when ctrl -> (* ctrl-l *)
3679 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3680 G.postRedisplay "outline ctrl-l";
3681 coe {< m_first = first >}
3683 | 0xff9f | 0xffff -> (* (kp) delete *)
3684 source#remove m_active;
3685 G.postRedisplay "outline delete";
3686 let active = max 0 (m_active-1) in
3687 coe {< m_first = firstof m_first active;
3688 m_active = active >}
3690 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3691 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3692 | 0xff55 | 0xff9a -> (* (kp) prior *)
3693 navigate ~-(fstate.maxrows)
3694 | 0xff56 | 0xff9b -> (* (kp) next *)
3695 navigate fstate.maxrows
3697 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3698 let o =
3699 if ctrl
3700 then (
3701 G.postRedisplay "outline ctrl right";
3702 {< m_pan = m_pan + 1 >}
3704 else self#updownlevel 1
3706 coe o
3708 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3709 let o =
3710 if ctrl
3711 then (
3712 G.postRedisplay "outline ctrl left";
3713 {< m_pan = m_pan - 1 >}
3715 else self#updownlevel ~-1
3717 coe o
3719 | 0xff50 | 0xff95 -> (* (kp) home *)
3720 G.postRedisplay "outline home";
3721 coe {< m_first = 0; m_active = 0 >}
3723 | 0xff57 | 0xff9c -> (* (kp) end *)
3724 let active = source#getitemcount - 1 in
3725 let first = max 0 (active - fstate.maxrows) in
3726 G.postRedisplay "outline end";
3727 coe {< m_active = active; m_first = first >}
3729 | _ -> super#key key mask
3732 let outlinesource usebookmarks =
3733 let empty = [||] in
3734 (object
3735 inherit lvsourcebase
3736 val mutable m_items = empty
3737 val mutable m_orig_items = empty
3738 val mutable m_prev_items = empty
3739 val mutable m_narrow_pattern = ""
3740 val mutable m_hadremovals = false
3742 method getitemcount =
3743 Array.length m_items + (if m_hadremovals then 1 else 0)
3745 method getitem n =
3746 if n == Array.length m_items && m_hadremovals
3747 then
3748 ("[Confirm removal]", 0)
3749 else
3750 let s, n, _ = m_items.(n) in
3751 (s, n)
3753 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3754 ignore (uioh, first, qsearch);
3755 let confrimremoval = m_hadremovals && active = Array.length m_items in
3756 let items =
3757 if String.length m_narrow_pattern = 0
3758 then m_orig_items
3759 else m_items
3761 if not cancel
3762 then (
3763 if not confrimremoval
3764 then(
3765 let _, _, anchor = m_items.(active) in
3766 gotoghyll (getanchory anchor);
3767 m_items <- items;
3769 else (
3770 state.bookmarks <- Array.to_list m_items;
3771 m_orig_items <- m_items;
3774 else m_items <- items;
3775 m_pan <- pan;
3776 None
3778 method hasaction _ = true
3780 method greetmsg =
3781 if Array.length m_items != Array.length m_orig_items
3782 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3783 else ""
3785 method narrow pattern =
3786 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3787 match reopt with
3788 | None -> ()
3789 | Some re ->
3790 let rec loop accu n =
3791 if n = -1
3792 then (
3793 m_narrow_pattern <- pattern;
3794 m_items <- Array.of_list accu
3796 else
3797 let (s, _, _) as o = m_items.(n) in
3798 let accu =
3799 if (try ignore (Str.search_forward re s 0); true
3800 with Not_found -> false)
3801 then o :: accu
3802 else accu
3804 loop accu (n-1)
3806 loop [] (Array.length m_items - 1)
3808 method denarrow =
3809 m_orig_items <- (
3810 if usebookmarks
3811 then Array.of_list state.bookmarks
3812 else state.outlines
3814 m_items <- m_orig_items
3816 method remove m =
3817 if usebookmarks
3818 then
3819 if m >= 0 && m < Array.length m_items
3820 then (
3821 m_hadremovals <- true;
3822 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3823 let n = if n >= m then n+1 else n in
3824 m_items.(n)
3828 method reset anchor items =
3829 m_hadremovals <- false;
3830 if m_orig_items == empty || m_prev_items != items
3831 then (
3832 m_orig_items <- items;
3833 if String.length m_narrow_pattern = 0
3834 then m_items <- items;
3836 m_prev_items <- items;
3837 let rely = getanchory anchor in
3838 let active =
3839 let rec loop n best bestd =
3840 if n = Array.length m_items
3841 then best
3842 else
3843 let (_, _, anchor) = m_items.(n) in
3844 let orely = getanchory anchor in
3845 let d = abs (orely - rely) in
3846 if d < bestd
3847 then loop (n+1) n d
3848 else loop (n+1) best bestd
3850 loop 0 ~-1 max_int
3852 m_active <- active;
3853 m_first <- firstof m_first active
3854 end)
3857 let enterselector usebookmarks =
3858 let source = outlinesource usebookmarks in
3859 fun errmsg ->
3860 let outlines =
3861 if usebookmarks
3862 then Array.of_list state.bookmarks
3863 else state.outlines
3865 if Array.length outlines = 0
3866 then (
3867 showtext ' ' errmsg;
3869 else (
3870 state.text <- source#greetmsg;
3871 Wsi.setcursor Wsi.CURSOR_INHERIT;
3872 let anchor = getanchor () in
3873 source#reset anchor outlines;
3874 state.uioh <- coe (new outlinelistview ~source);
3875 G.postRedisplay "enter selector";
3879 let enteroutlinemode =
3880 let f = enterselector false in
3881 fun ()-> f "Document has no outline";
3884 let enterbookmarkmode =
3885 let f = enterselector true in
3886 fun () -> f "Document has no bookmarks (yet)";
3889 let color_of_string s =
3890 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3891 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3895 let color_to_string (r, g, b) =
3896 let r = truncate (r *. 256.0)
3897 and g = truncate (g *. 256.0)
3898 and b = truncate (b *. 256.0) in
3899 Printf.sprintf "%d/%d/%d" r g b
3902 let irect_of_string s =
3903 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3906 let irect_to_string (x0,y0,x1,y1) =
3907 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3910 let makecheckers () =
3911 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3912 following to say:
3913 converted by Issac Trotts. July 25, 2002 *)
3914 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3915 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3916 let id = GlTex.gen_texture () in
3917 GlTex.bind_texture `texture_2d id;
3918 GlPix.store (`unpack_alignment 1);
3919 GlTex.image2d image;
3920 List.iter (GlTex.parameter ~target:`texture_2d)
3921 [ `mag_filter `nearest; `min_filter `nearest ];
3925 let setcheckers enabled =
3926 match state.texid with
3927 | None ->
3928 if enabled then state.texid <- Some (makecheckers ())
3930 | Some texid ->
3931 if not enabled
3932 then (
3933 GlTex.delete_texture texid;
3934 state.texid <- None;
3938 let int_of_string_with_suffix s =
3939 let l = String.length s in
3940 let s1, shift =
3941 if l > 1
3942 then
3943 let suffix = Char.lowercase s.[l-1] in
3944 match suffix with
3945 | 'k' -> String.sub s 0 (l-1), 10
3946 | 'm' -> String.sub s 0 (l-1), 20
3947 | 'g' -> String.sub s 0 (l-1), 30
3948 | _ -> s, 0
3949 else s, 0
3951 let n = int_of_string s1 in
3952 let m = n lsl shift in
3953 if m < 0 || m < n
3954 then raise (Failure "value too large")
3955 else m
3958 let string_with_suffix_of_int n =
3959 if n = 0
3960 then "0"
3961 else
3962 let n, s =
3963 if n land ((1 lsl 30) - 1) = 0
3964 then n lsr 30, "G"
3965 else (
3966 if n land ((1 lsl 20) - 1) = 0
3967 then n lsr 20, "M"
3968 else (
3969 if n land ((1 lsl 10) - 1) = 0
3970 then n lsr 10, "K"
3971 else n, ""
3975 let rec loop s n =
3976 let h = n mod 1000 in
3977 let n = n / 1000 in
3978 if n = 0
3979 then string_of_int h ^ s
3980 else (
3981 let s = Printf.sprintf "_%03d%s" h s in
3982 loop s n
3985 loop "" n ^ s;
3988 let defghyllscroll = (40, 8, 32);;
3989 let ghyllscroll_of_string s =
3990 let (n, a, b) as nab =
3991 if s = "default"
3992 then defghyllscroll
3993 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3995 if n <= a || n <= b || a >= b
3996 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3997 nab;
4000 let ghyllscroll_to_string ((n, a, b) as nab) =
4001 if nab = defghyllscroll
4002 then "default"
4003 else Printf.sprintf "%d,%d,%d" n a b;
4006 let describe_location () =
4007 let f (fn, _) l =
4008 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
4010 let fn, ln = List.fold_left f (-1, -1) state.layout in
4011 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
4012 let percent =
4013 if maxy <= 0
4014 then 100.
4015 else (100. *. (float state.y /. float maxy))
4017 if fn = ln
4018 then
4019 Printf.sprintf "page %d of %d [%.2f%%]"
4020 (fn+1) state.pagecount percent
4021 else
4022 Printf.sprintf
4023 "pages %d-%d of %d [%.2f%%]"
4024 (fn+1) (ln+1) state.pagecount percent
4027 let setpresentationmode v =
4028 let (n, _, _) = getanchor () in
4029 let _, h = getpageyh n in
4030 let ips = if conf.presentation then calcips h else conf.interpagespace in
4031 state.anchor <- (n, 0.0, float ips);
4032 conf.presentation <- v;
4033 if conf.presentation
4034 then (
4035 if not conf.scrollbarinpm
4036 then state.scrollw <- 0;
4038 else state.scrollw <- conf.scrollbw;
4039 represent ();
4042 let enterinfomode =
4043 let btos b = if b then "\xe2\x88\x9a" else "" in
4044 let showextended = ref false in
4045 let leave mode = function
4046 | Confirm -> state.mode <- mode
4047 | Cancel -> state.mode <- mode in
4048 let src =
4049 (object
4050 val mutable m_first_time = true
4051 val mutable m_l = []
4052 val mutable m_a = [||]
4053 val mutable m_prev_uioh = nouioh
4054 val mutable m_prev_mode = View
4056 inherit lvsourcebase
4058 method reset prev_mode prev_uioh =
4059 m_a <- Array.of_list (List.rev m_l);
4060 m_l <- [];
4061 m_prev_mode <- prev_mode;
4062 m_prev_uioh <- prev_uioh;
4063 if m_first_time
4064 then (
4065 let rec loop n =
4066 if n >= Array.length m_a
4067 then ()
4068 else
4069 match m_a.(n) with
4070 | _, _, _, Action _ -> m_active <- n
4071 | _ -> loop (n+1)
4073 loop 0;
4074 m_first_time <- false;
4077 method int name get set =
4078 m_l <-
4079 (name, `int get, 1, Action (
4080 fun u ->
4081 let ondone s =
4082 try set (int_of_string s)
4083 with exn ->
4084 state.text <- Printf.sprintf "bad integer `%s': %s"
4085 s (Printexc.to_string exn)
4087 state.text <- "";
4088 let te = name ^ ": ", "", None, intentry, ondone, true in
4089 state.mode <- Textentry (te, leave m_prev_mode);
4091 )) :: m_l
4093 method int_with_suffix name get set =
4094 m_l <-
4095 (name, `intws get, 1, Action (
4096 fun u ->
4097 let ondone s =
4098 try set (int_of_string_with_suffix s)
4099 with exn ->
4100 state.text <- Printf.sprintf "bad integer `%s': %s"
4101 s (Printexc.to_string exn)
4103 state.text <- "";
4104 let te =
4105 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4107 state.mode <- Textentry (te, leave m_prev_mode);
4109 )) :: m_l
4111 method bool ?(offset=1) ?(btos=btos) name get set =
4112 m_l <-
4113 (name, `bool (btos, get), offset, Action (
4114 fun u ->
4115 let v = get () in
4116 set (not v);
4118 )) :: m_l
4120 method color name get set =
4121 m_l <-
4122 (name, `color get, 1, Action (
4123 fun u ->
4124 let invalid = (nan, nan, nan) in
4125 let ondone s =
4126 let c =
4127 try color_of_string s
4128 with exn ->
4129 state.text <- Printf.sprintf "bad color `%s': %s"
4130 s (Printexc.to_string exn);
4131 invalid
4133 if c <> invalid
4134 then set c;
4136 let te = name ^ ": ", "", None, textentry, ondone, true in
4137 state.text <- color_to_string (get ());
4138 state.mode <- Textentry (te, leave m_prev_mode);
4140 )) :: m_l
4142 method string name get set =
4143 m_l <-
4144 (name, `string get, 1, Action (
4145 fun u ->
4146 let ondone s = set s in
4147 let te = name ^ ": ", "", None, textentry, ondone, true in
4148 state.mode <- Textentry (te, leave m_prev_mode);
4150 )) :: m_l
4152 method colorspace name get set =
4153 m_l <-
4154 (name, `string get, 1, Action (
4155 fun _ ->
4156 let source =
4157 let vals = [| "rgb"; "bgr"; "gray" |] in
4158 (object
4159 inherit lvsourcebase
4161 initializer
4162 m_active <- int_of_colorspace conf.colorspace;
4163 m_first <- 0;
4165 method getitemcount = Array.length vals
4166 method getitem n = (vals.(n), 0)
4167 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4168 ignore (uioh, first, pan, qsearch);
4169 if not cancel then set active;
4170 None
4171 method hasaction _ = true
4172 end)
4174 state.text <- "";
4175 let modehash = findkeyhash conf "info" in
4176 coe (new listview ~source ~trusted:true ~modehash)
4177 )) :: m_l
4179 method caption s offset =
4180 m_l <- (s, `empty, offset, Noaction) :: m_l
4182 method caption2 s f offset =
4183 m_l <- (s, `string f, offset, Noaction) :: m_l
4185 method getitemcount = Array.length m_a
4187 method getitem n =
4188 let tostr = function
4189 | `int f -> string_of_int (f ())
4190 | `intws f -> string_with_suffix_of_int (f ())
4191 | `string f -> f ()
4192 | `color f -> color_to_string (f ())
4193 | `bool (btos, f) -> btos (f ())
4194 | `empty -> ""
4196 let name, t, offset, _ = m_a.(n) in
4197 ((let s = tostr t in
4198 if String.length s > 0
4199 then Printf.sprintf "%s\t%s" name s
4200 else name),
4201 offset)
4203 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4204 let uiohopt =
4205 if not cancel
4206 then (
4207 m_qsearch <- qsearch;
4208 let uioh =
4209 match m_a.(active) with
4210 | _, _, _, Action f -> f uioh
4211 | _ -> uioh
4213 Some uioh
4215 else None
4217 m_active <- active;
4218 m_first <- first;
4219 m_pan <- pan;
4220 uiohopt
4222 method hasaction n =
4223 match m_a.(n) with
4224 | _, _, _, Action _ -> true
4225 | _ -> false
4226 end)
4228 let rec fillsrc prevmode prevuioh =
4229 let sep () = src#caption "" 0 in
4230 let colorp name get set =
4231 src#string name
4232 (fun () -> color_to_string (get ()))
4233 (fun v ->
4235 let c = color_of_string v in
4236 set c
4237 with exn ->
4238 state.text <- Printf.sprintf "bad color `%s': %s"
4239 v (Printexc.to_string exn);
4242 let oldmode = state.mode in
4243 let birdseye = isbirdseye state.mode in
4245 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4247 src#bool "presentation mode"
4248 (fun () -> conf.presentation)
4249 (fun v -> setpresentationmode v);
4251 src#bool "ignore case in searches"
4252 (fun () -> conf.icase)
4253 (fun v -> conf.icase <- v);
4255 src#bool "preload"
4256 (fun () -> conf.preload)
4257 (fun v -> conf.preload <- v);
4259 src#bool "highlight links"
4260 (fun () -> conf.hlinks)
4261 (fun v -> conf.hlinks <- v);
4263 src#bool "under info"
4264 (fun () -> conf.underinfo)
4265 (fun v -> conf.underinfo <- v);
4267 src#bool "persistent bookmarks"
4268 (fun () -> conf.savebmarks)
4269 (fun v -> conf.savebmarks <- v);
4271 src#bool "proportional display"
4272 (fun () -> conf.proportional)
4273 (fun v -> reqlayout conf.angle v);
4275 src#bool "trim margins"
4276 (fun () -> conf.trimmargins)
4277 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4279 src#bool "persistent location"
4280 (fun () -> conf.jumpback)
4281 (fun v -> conf.jumpback <- v);
4283 sep ();
4284 src#int "inter-page space"
4285 (fun () -> conf.interpagespace)
4286 (fun n ->
4287 conf.interpagespace <- n;
4288 docolumns conf.columns;
4289 let pageno, py =
4290 match state.layout with
4291 | [] -> 0, 0
4292 | l :: _ ->
4293 l.pageno, l.pagey
4295 state.maxy <- calcheight ();
4296 let y = getpagey pageno in
4297 gotoy (y + py)
4300 src#int "page bias"
4301 (fun () -> conf.pagebias)
4302 (fun v -> conf.pagebias <- v);
4304 src#int "scroll step"
4305 (fun () -> conf.scrollstep)
4306 (fun n -> conf.scrollstep <- n);
4308 src#int "horizontal scroll step"
4309 (fun () -> conf.hscrollstep)
4310 (fun v -> conf.hscrollstep <- v);
4312 src#int "auto scroll step"
4313 (fun () ->
4314 match state.autoscroll with
4315 | Some step -> step
4316 | _ -> conf.autoscrollstep)
4317 (fun n ->
4318 if state.autoscroll <> None
4319 then state.autoscroll <- Some n;
4320 conf.autoscrollstep <- n);
4322 src#int "zoom"
4323 (fun () -> truncate (conf.zoom *. 100.))
4324 (fun v -> setzoom ((float v) /. 100.));
4326 src#int "rotation"
4327 (fun () -> conf.angle)
4328 (fun v -> reqlayout v conf.proportional);
4330 src#int "scroll bar width"
4331 (fun () -> state.scrollw)
4332 (fun v ->
4333 state.scrollw <- v;
4334 conf.scrollbw <- v;
4335 reshape conf.winw conf.winh;
4338 src#int "scroll handle height"
4339 (fun () -> conf.scrollh)
4340 (fun v -> conf.scrollh <- v;);
4342 src#int "thumbnail width"
4343 (fun () -> conf.thumbw)
4344 (fun v ->
4345 conf.thumbw <- min 4096 v;
4346 match oldmode with
4347 | Birdseye beye ->
4348 leavebirdseye beye false;
4349 enterbirdseye ()
4350 | _ -> ()
4353 let mode = state.mode in
4354 src#string "columns"
4355 (fun () ->
4356 match conf.columns with
4357 | Csingle _ -> "1"
4358 | Cmulti (multi, _) -> multicolumns_to_string multi
4359 | Csplit (count, _) -> "-" ^ string_of_int count
4361 (fun v ->
4362 let n, a, b = multicolumns_of_string v in
4363 setcolumns mode n a b);
4365 sep ();
4366 src#caption "Presentation mode" 0;
4367 src#bool "scrollbar visible"
4368 (fun () -> conf.scrollbarinpm)
4369 (fun v ->
4370 if v != conf.scrollbarinpm
4371 then (
4372 conf.scrollbarinpm <- v;
4373 if conf.presentation
4374 then (
4375 state.scrollw <- if v then conf.scrollbw else 0;
4376 reshape conf.winw conf.winh;
4381 sep ();
4382 src#caption "Pixmap cache" 0;
4383 src#int_with_suffix "size (advisory)"
4384 (fun () -> conf.memlimit)
4385 (fun v -> conf.memlimit <- v);
4387 src#caption2 "used"
4388 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4389 (string_with_suffix_of_int state.memused)
4390 (Hashtbl.length state.tilemap)) 1;
4392 sep ();
4393 src#caption "Layout" 0;
4394 src#caption2 "Dimension"
4395 (fun () ->
4396 Printf.sprintf "%dx%d (virtual %dx%d)"
4397 conf.winw conf.winh
4398 state.w state.maxy)
4400 if conf.debug
4401 then
4402 src#caption2 "Position" (fun () ->
4403 Printf.sprintf "%dx%d" state.x state.y
4405 else
4406 src#caption2 "Visible" (fun () -> describe_location ()) 1
4409 sep ();
4410 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4411 "Save these parameters as global defaults at exit"
4412 (fun () -> conf.bedefault)
4413 (fun v -> conf.bedefault <- v)
4416 sep ();
4417 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4418 src#bool ~offset:0 ~btos "Extended parameters"
4419 (fun () -> !showextended)
4420 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4421 if !showextended
4422 then (
4423 src#bool "checkers"
4424 (fun () -> conf.checkers)
4425 (fun v -> conf.checkers <- v; setcheckers v);
4426 src#bool "update cursor"
4427 (fun () -> conf.updatecurs)
4428 (fun v -> conf.updatecurs <- v);
4429 src#bool "verbose"
4430 (fun () -> conf.verbose)
4431 (fun v -> conf.verbose <- v);
4432 src#bool "invert colors"
4433 (fun () -> conf.invert)
4434 (fun v -> conf.invert <- v);
4435 src#bool "max fit"
4436 (fun () -> conf.maxhfit)
4437 (fun v -> conf.maxhfit <- v);
4438 src#bool "redirect stderr"
4439 (fun () -> conf.redirectstderr)
4440 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4441 src#string "uri launcher"
4442 (fun () -> conf.urilauncher)
4443 (fun v -> conf.urilauncher <- v);
4444 src#string "path launcher"
4445 (fun () -> conf.pathlauncher)
4446 (fun v -> conf.pathlauncher <- v);
4447 src#string "tile size"
4448 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4449 (fun v ->
4451 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4452 conf.tilew <- max 64 w;
4453 conf.tileh <- max 64 h;
4454 flushtiles ();
4455 with exn ->
4456 state.text <- Printf.sprintf "bad tile size `%s': %s"
4457 v (Printexc.to_string exn));
4458 src#int "texture count"
4459 (fun () -> conf.texcount)
4460 (fun v ->
4461 if realloctexts v
4462 then conf.texcount <- v
4463 else showtext '!' " Failed to set texture count please retry later"
4465 src#int "slice height"
4466 (fun () -> conf.sliceheight)
4467 (fun v ->
4468 conf.sliceheight <- v;
4469 wcmd "sliceh %d" conf.sliceheight;
4471 src#int "anti-aliasing level"
4472 (fun () -> conf.aalevel)
4473 (fun v ->
4474 conf.aalevel <- bound v 0 8;
4475 state.anchor <- getanchor ();
4476 opendoc state.path state.password;
4478 src#string "page scroll scaling factor"
4479 (fun () -> string_of_float conf.pgscale)
4480 (fun v ->
4482 let s = float_of_string v in
4483 conf.pgscale <- s
4484 with exn ->
4485 state.text <- Printf.sprintf
4486 "bad page scroll scaling factor `%s': %s"
4487 v (Printexc.to_string exn)
4490 src#int "ui font size"
4491 (fun () -> fstate.fontsize)
4492 (fun v -> setfontsize (bound v 5 100));
4493 src#int "hint font size"
4494 (fun () -> conf.hfsize)
4495 (fun v -> conf.hfsize <- bound v 5 100);
4496 colorp "background color"
4497 (fun () -> conf.bgcolor)
4498 (fun v -> conf.bgcolor <- v);
4499 src#bool "crop hack"
4500 (fun () -> conf.crophack)
4501 (fun v -> conf.crophack <- v);
4502 src#string "trim fuzz"
4503 (fun () -> irect_to_string conf.trimfuzz)
4504 (fun v ->
4506 conf.trimfuzz <- irect_of_string v;
4507 if conf.trimmargins
4508 then settrim true conf.trimfuzz;
4509 with exn ->
4510 state.text <- Printf.sprintf "bad irect `%s': %s"
4511 v (Printexc.to_string exn)
4513 src#string "throttle"
4514 (fun () ->
4515 match conf.maxwait with
4516 | None -> "show place holder if page is not ready"
4517 | Some time ->
4518 if time = infinity
4519 then "wait for page to fully render"
4520 else
4521 "wait " ^ string_of_float time
4522 ^ " seconds before showing placeholder"
4524 (fun v ->
4526 let f = float_of_string v in
4527 if f <= 0.0
4528 then conf.maxwait <- None
4529 else conf.maxwait <- Some f
4530 with exn ->
4531 state.text <- Printf.sprintf "bad time `%s': %s"
4532 v (Printexc.to_string exn)
4534 src#string "ghyll scroll"
4535 (fun () ->
4536 match conf.ghyllscroll with
4537 | None -> ""
4538 | Some nab -> ghyllscroll_to_string nab
4540 (fun v ->
4542 let gs =
4543 if String.length v = 0
4544 then None
4545 else Some (ghyllscroll_of_string v)
4547 conf.ghyllscroll <- gs
4548 with exn ->
4549 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4550 v (Printexc.to_string exn)
4552 src#string "selection command"
4553 (fun () -> conf.selcmd)
4554 (fun v -> conf.selcmd <- v);
4555 src#string "synctex command"
4556 (fun () -> conf.stcmd)
4557 (fun v -> conf.stcmd <- v);
4558 src#colorspace "color space"
4559 (fun () -> colorspace_to_string conf.colorspace)
4560 (fun v ->
4561 conf.colorspace <- colorspace_of_int v;
4562 wcmd "cs %d" v;
4563 load state.layout;
4565 if pbousable ()
4566 then
4567 src#bool "use PBO"
4568 (fun () -> conf.usepbo)
4569 (fun v -> conf.usepbo <- v);
4570 src#bool "mouse wheel scrolls pages"
4571 (fun () -> conf.wheelbypage)
4572 (fun v -> conf.wheelbypage <- v);
4575 sep ();
4576 src#caption "Document" 0;
4577 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4578 src#caption2 "Pages"
4579 (fun () -> string_of_int state.pagecount) 1;
4580 src#caption2 "Dimensions"
4581 (fun () -> string_of_int (List.length state.pdims)) 1;
4582 if conf.trimmargins
4583 then (
4584 sep ();
4585 src#caption "Trimmed margins" 0;
4586 src#caption2 "Dimensions"
4587 (fun () -> string_of_int (List.length state.pdims)) 1;
4590 sep ();
4591 src#caption "OpenGL" 0;
4592 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4593 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4594 src#reset prevmode prevuioh;
4596 fun () ->
4597 state.text <- "";
4598 let prevmode = state.mode
4599 and prevuioh = state.uioh in
4600 fillsrc prevmode prevuioh;
4601 let source = (src :> lvsource) in
4602 let modehash = findkeyhash conf "info" in
4603 state.uioh <- coe (object (self)
4604 inherit listview ~source ~trusted:true ~modehash as super
4605 val mutable m_prevmemused = 0
4606 method infochanged = function
4607 | Memused ->
4608 if m_prevmemused != state.memused
4609 then (
4610 m_prevmemused <- state.memused;
4611 G.postRedisplay "memusedchanged";
4613 | Pdim -> G.postRedisplay "pdimchanged"
4614 | Docinfo -> fillsrc prevmode prevuioh
4616 method key key mask =
4617 if not (Wsi.withctrl mask)
4618 then
4619 match key with
4620 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4621 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4622 | _ -> super#key key mask
4623 else super#key key mask
4624 end);
4625 G.postRedisplay "info";
4628 let enterhelpmode =
4629 let source =
4630 (object
4631 inherit lvsourcebase
4632 method getitemcount = Array.length state.help
4633 method getitem n =
4634 let s, l, _ = state.help.(n) in
4635 (s, l)
4637 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4638 let optuioh =
4639 if not cancel
4640 then (
4641 m_qsearch <- qsearch;
4642 match state.help.(active) with
4643 | _, _, Action f -> Some (f uioh)
4644 | _ -> Some (uioh)
4646 else None
4648 m_active <- active;
4649 m_first <- first;
4650 m_pan <- pan;
4651 optuioh
4653 method hasaction n =
4654 match state.help.(n) with
4655 | _, _, Action _ -> true
4656 | _ -> false
4658 initializer
4659 m_active <- -1
4660 end)
4661 in fun () ->
4662 let modehash = findkeyhash conf "help" in
4663 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4664 G.postRedisplay "help";
4667 let entermsgsmode =
4668 let msgsource =
4669 let re = Str.regexp "[\r\n]" in
4670 (object
4671 inherit lvsourcebase
4672 val mutable m_items = [||]
4674 method getitemcount = 1 + Array.length m_items
4676 method getitem n =
4677 if n = 0
4678 then "[Clear]", 0
4679 else m_items.(n-1), 0
4681 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4682 ignore uioh;
4683 if not cancel
4684 then (
4685 if active = 0
4686 then Buffer.clear state.errmsgs;
4687 m_qsearch <- qsearch;
4689 m_active <- active;
4690 m_first <- first;
4691 m_pan <- pan;
4692 None
4694 method hasaction n =
4695 n = 0
4697 method reset =
4698 state.newerrmsgs <- false;
4699 let l = Str.split re (Buffer.contents state.errmsgs) in
4700 m_items <- Array.of_list l
4702 initializer
4703 m_active <- 0
4704 end)
4705 in fun () ->
4706 state.text <- "";
4707 msgsource#reset;
4708 let source = (msgsource :> lvsource) in
4709 let modehash = findkeyhash conf "listview" in
4710 state.uioh <- coe (object
4711 inherit listview ~source ~trusted:false ~modehash as super
4712 method display =
4713 if state.newerrmsgs
4714 then msgsource#reset;
4715 super#display
4716 end);
4717 G.postRedisplay "msgs";
4720 let quickbookmark ?title () =
4721 match state.layout with
4722 | [] -> ()
4723 | l :: _ ->
4724 let title =
4725 match title with
4726 | None ->
4727 let sec = Unix.gettimeofday () in
4728 let tm = Unix.localtime sec in
4729 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4730 (l.pageno+1)
4731 tm.Unix.tm_mday
4732 tm.Unix.tm_mon
4733 (tm.Unix.tm_year + 1900)
4734 tm.Unix.tm_hour
4735 tm.Unix.tm_min
4736 | Some title -> title
4738 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4741 let doreshape w h =
4742 state.fullscreen <- None;
4743 Wsi.reshape w h;
4746 let setautoscrollspeed step goingdown =
4747 let incr = max 1 ((abs step) / 2) in
4748 let incr = if goingdown then incr else -incr in
4749 let astep = step + incr in
4750 state.autoscroll <- Some astep;
4753 let gotounder = function
4754 | Ulinkgoto (pageno, top) ->
4755 if pageno >= 0
4756 then (
4757 addnav ();
4758 gotopage1 pageno top;
4761 | Ulinkuri s ->
4762 gotouri s
4764 | Uremote (filename, pageno) ->
4765 let path =
4766 if Sys.file_exists filename
4767 then filename
4768 else
4769 let dir = Filename.dirname state.path in
4770 let path = Filename.concat dir filename in
4771 if Sys.file_exists path
4772 then path
4773 else ""
4775 if String.length path > 0
4776 then (
4777 let anchor = getanchor () in
4778 let ranchor = state.path, state.password, anchor in
4779 state.anchor <- (pageno, 0.0, 0.0);
4780 state.ranchors <- ranchor :: state.ranchors;
4781 opendoc path "";
4783 else showtext '!' ("Could not find " ^ filename)
4785 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4788 let canpan () =
4789 match conf.columns with
4790 | Csplit _ -> true
4791 | _ -> conf.zoom > 1.0
4794 let existsinrow pageno (columns, coverA, coverB) p =
4795 let last = ((pageno - coverA) mod columns) + columns in
4796 let rec any = function
4797 | [] -> false
4798 | l :: rest ->
4799 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4800 then p l
4801 else (
4802 if not (p l)
4803 then (if l.pageno = last then false else any rest)
4804 else true
4807 any state.layout
4810 let nextpage () =
4811 match state.layout with
4812 | [] -> ()
4813 | l :: rest ->
4814 match conf.columns with
4815 | Csingle _ ->
4816 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4817 then
4818 let y = clamp (pgscale conf.winh) in
4819 gotoghyll y
4820 else
4821 let pageno = min (l.pageno+1) (state.pagecount-1) in
4822 gotoghyll (getpagey pageno)
4823 | Cmulti ((c, _, _) as cl, _) ->
4824 if conf.presentation
4825 && (existsinrow l.pageno cl
4826 (fun l -> l.pageh > l.pagey + l.pagevh))
4827 then
4828 let y = clamp (pgscale conf.winh) in
4829 gotoghyll y
4830 else
4831 let pageno = min (l.pageno+c) (state.pagecount-1) in
4832 gotoghyll (getpagey pageno)
4833 | Csplit (n, _) ->
4834 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4835 then
4836 let pagey, pageh = getpageyh l.pageno in
4837 let pagey = pagey + pageh * l.pagecol in
4838 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4839 gotoghyll (pagey + pageh + ips)
4842 let prevpage () =
4843 match state.layout with
4844 | [] -> ()
4845 | l :: _ ->
4846 match conf.columns with
4847 | Csingle _ ->
4848 if conf.presentation && l.pagey != 0
4849 then
4850 gotoghyll (clamp (pgscale ~-(conf.winh)))
4851 else
4852 let pageno = max 0 (l.pageno-1) in
4853 gotoghyll (getpagey pageno)
4854 | Cmulti ((c, _, coverB) as cl, _) ->
4855 if conf.presentation &&
4856 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4857 then
4858 gotoghyll (clamp (pgscale ~-(conf.winh)))
4859 else
4860 let decr =
4861 if l.pageno = state.pagecount - coverB
4862 then 1
4863 else c
4865 let pageno = max 0 (l.pageno-decr) in
4866 gotoghyll (getpagey pageno)
4867 | Csplit (n, _) ->
4868 let y =
4869 if l.pagecol = 0
4870 then
4871 if l.pageno = 0
4872 then l.pagey
4873 else
4874 let pageno = max 0 (l.pageno-1) in
4875 let pagey, pageh = getpageyh pageno in
4876 pagey + (n-1)*pageh
4877 else
4878 let pagey, pageh = getpageyh l.pageno in
4879 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4881 gotoghyll y
4884 let viewkeyboard key mask =
4885 let enttext te =
4886 let mode = state.mode in
4887 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4888 state.text <- "";
4889 enttext ();
4890 G.postRedisplay "view:enttext"
4892 let ctrl = Wsi.withctrl mask in
4893 let key =
4894 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4896 match key with
4897 | 81 -> (* Q *)
4898 exit 0
4900 | 0xff63 -> (* insert *)
4901 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4902 then (
4903 state.mode <- LinkNav (Ltgendir 0);
4904 gotoy state.y;
4906 else showtext '!' "Keyboard link navigation does not work under rotation"
4908 | 0xff1b | 113 -> (* escape / q *)
4909 begin match state.mstate with
4910 | Mzoomrect _ ->
4911 state.mstate <- Mnone;
4912 Wsi.setcursor Wsi.CURSOR_INHERIT;
4913 G.postRedisplay "kill zoom rect";
4914 | _ ->
4915 begin match state.mode with
4916 | LinkNav _ ->
4917 state.mode <- View;
4918 G.postRedisplay "esc leave linknav"
4919 | _ ->
4920 match state.ranchors with
4921 | [] -> raise Quit
4922 | (path, password, anchor) :: rest ->
4923 state.ranchors <- rest;
4924 state.anchor <- anchor;
4925 opendoc path password
4926 end;
4927 end;
4929 | 0xff08 -> (* backspace *)
4930 gotoghyll (getnav ~-1)
4932 | 111 -> (* o *)
4933 enteroutlinemode ()
4935 | 117 -> (* u *)
4936 state.rects <- [];
4937 state.text <- "";
4938 G.postRedisplay "dehighlight";
4940 | 47 | 63 -> (* / ? *)
4941 let ondone isforw s =
4942 cbput state.hists.pat s;
4943 state.searchpattern <- s;
4944 search s isforw
4946 let s = String.create 1 in
4947 s.[0] <- Char.chr key;
4948 enttext (s, "", Some (onhist state.hists.pat),
4949 textentry, ondone (key = 47), true)
4951 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4952 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4953 setzoom (conf.zoom +. incr)
4955 | 43 | 0xffab -> (* + *)
4956 let ondone s =
4957 let n =
4958 try int_of_string s with exc ->
4959 state.text <- Printf.sprintf "bad integer `%s': %s"
4960 s (Printexc.to_string exc);
4961 max_int
4963 if n != max_int
4964 then (
4965 conf.pagebias <- n;
4966 state.text <- "page bias is now " ^ string_of_int n;
4969 enttext ("page bias: ", "", None, intentry, ondone, true)
4971 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4972 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4973 setzoom (max 0.01 (conf.zoom -. decr))
4975 | 45 | 0xffad -> (* - *)
4976 let ondone msg = state.text <- msg in
4977 enttext (
4978 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4979 optentry state.mode, ondone, true
4982 | 48 when ctrl -> (* ctrl-0 *)
4983 setzoom 1.0
4985 | 49 when ctrl -> (* ctrl-1 *)
4986 let cols =
4987 match conf.columns with
4988 | Csingle _ | Cmulti _ -> 1
4989 | Csplit (n, _) -> n
4991 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4992 if zoom < 1.0
4993 then setzoom zoom
4995 | 0xffc6 -> (* f9 *)
4996 togglebirdseye ()
4998 | 57 when ctrl -> (* ctrl-9 *)
4999 togglebirdseye ()
5001 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5002 when not ctrl -> (* 0..9 *)
5003 let ondone s =
5004 let n =
5005 try int_of_string s with exc ->
5006 state.text <- Printf.sprintf "bad integer `%s': %s"
5007 s (Printexc.to_string exc);
5010 if n >= 0
5011 then (
5012 addnav ();
5013 cbput state.hists.pag (string_of_int n);
5014 gotopage1 (n + conf.pagebias - 1) 0;
5017 let pageentry text key =
5018 match Char.unsafe_chr key with
5019 | 'g' -> TEdone text
5020 | _ -> intentry text key
5022 let text = "x" in text.[0] <- Char.chr key;
5023 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5025 | 98 -> (* b *)
5026 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5027 reshape conf.winw conf.winh;
5029 | 108 -> (* l *)
5030 conf.hlinks <- not conf.hlinks;
5031 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5032 G.postRedisplay "toggle highlightlinks";
5034 | 70 -> (* F *)
5035 state.glinks <- true;
5036 let mode = state.mode in
5037 state.mode <- Textentry (
5038 (":", "", None, linknentry, linkndone gotounder, false),
5039 (fun _ ->
5040 state.glinks <- false;
5041 state.mode <- mode)
5043 state.text <- "";
5044 G.postRedisplay "view:linkent(F)"
5046 | 121 -> (* y *)
5047 state.glinks <- true;
5048 let mode = state.mode in
5049 state.mode <- Textentry (
5050 (":", "", None, linknentry, linkndone (fun under ->
5051 match Ne.pipe () with
5052 | Ne.Exn exn ->
5053 showtext '!' (Printf.sprintf "pipe failed: %s"
5054 (Printexc.to_string exn));
5055 | Ne.Res (r, w) ->
5056 let popened =
5057 try popen conf.selcmd [r, 0; w, -1]; true
5058 with exn ->
5059 showtext '!'
5060 (Printf.sprintf "failed to execute %s: %s"
5061 conf.selcmd (Printexc.to_string exn));
5062 false
5064 let clo cap fd =
5065 Ne.clo fd (fun msg ->
5066 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5069 let s = undertext under in
5070 if popened
5071 then
5072 (try
5073 let l = String.length s in
5074 let n = tempfailureretry (Unix.write w s 0) l in
5075 if n != l
5076 then
5077 showtext '!'
5078 (Printf.sprintf
5079 "failed to write %d characters to sel pipe, wrote %d"
5082 with exn ->
5083 showtext '!'
5084 (Printf.sprintf "failed to write to sel pipe: %s"
5085 (Printexc.to_string exn)
5088 else dolog "%s" s;
5089 clo "pipe/r" r;
5090 clo "pipe/w" w;
5091 ), false
5093 fun _ ->
5094 state.glinks <- false;
5095 state.mode <- mode
5097 state.text <- "";
5098 G.postRedisplay "view:linkent"
5100 | 97 -> (* a *)
5101 begin match state.autoscroll with
5102 | Some step ->
5103 conf.autoscrollstep <- step;
5104 state.autoscroll <- None
5105 | None ->
5106 if conf.autoscrollstep = 0
5107 then state.autoscroll <- Some 1
5108 else state.autoscroll <- Some conf.autoscrollstep
5111 | 112 when ctrl -> (* ctrl-p *)
5112 launchpath ()
5114 | 80 -> (* P *)
5115 setpresentationmode (not conf.presentation);
5116 showtext ' ' ("presentation mode " ^
5117 if conf.presentation then "on" else "off");
5119 | 102 -> (* f *)
5120 begin match state.fullscreen with
5121 | None ->
5122 state.fullscreen <- Some (conf.winw, conf.winh);
5123 Wsi.fullscreen ()
5124 | Some (w, h) ->
5125 state.fullscreen <- None;
5126 doreshape w h
5129 | 112 | 78 -> (* p|N *)
5130 search state.searchpattern false
5132 | 110 | 0xffc0 -> (* n|F3 *)
5133 search state.searchpattern true
5135 | 116 -> (* t *)
5136 begin match state.layout with
5137 | [] -> ()
5138 | l :: _ ->
5139 gotoy_and_clear_text (getpagey l.pageno)
5142 | 32 -> (* space *)
5143 nextpage ()
5145 | 0xff9f | 0xffff -> (* delete *)
5146 prevpage ()
5148 | 61 -> (* = *)
5149 showtext ' ' (describe_location ());
5151 | 119 -> (* w *)
5152 begin match state.layout with
5153 | [] -> ()
5154 | l :: _ ->
5155 doreshape (l.pagew + state.scrollw) l.pageh;
5156 G.postRedisplay "w"
5159 | 39 -> (* ' *)
5160 enterbookmarkmode ()
5162 | 104 | 0xffbe -> (* h|F1 *)
5163 enterhelpmode ()
5165 | 105 -> (* i *)
5166 enterinfomode ()
5168 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5169 entermsgsmode ()
5171 | 109 -> (* m *)
5172 let ondone s =
5173 match state.layout with
5174 | l :: _ ->
5175 if String.length s > 0
5176 then
5177 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5178 | _ -> ()
5180 enttext ("bookmark: ", "", None, textentry, ondone, true)
5182 | 126 -> (* ~ *)
5183 quickbookmark ();
5184 showtext ' ' "Quick bookmark added";
5186 | 122 -> (* z *)
5187 begin match state.layout with
5188 | l :: _ ->
5189 let rect = getpdimrect l.pagedimno in
5190 let w, h =
5191 if conf.crophack
5192 then
5193 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5194 truncate (1.2 *. (rect.(3) -. rect.(0))))
5195 else
5196 (truncate (rect.(1) -. rect.(0)),
5197 truncate (rect.(3) -. rect.(0)))
5199 let w = truncate ((float w)*.conf.zoom)
5200 and h = truncate ((float h)*.conf.zoom) in
5201 if w != 0 && h != 0
5202 then (
5203 state.anchor <- getanchor ();
5204 doreshape (w + state.scrollw) (h + conf.interpagespace)
5206 G.postRedisplay "z";
5208 | [] -> ()
5211 | 50 when ctrl -> (* ctrl-2 *)
5212 let maxw = getmaxw () in
5213 if maxw > 0.0
5214 then setzoom (maxw /. float conf.winw)
5216 | 60 | 62 -> (* < > *)
5217 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5219 | 91 | 93 -> (* [ ] *)
5220 conf.colorscale <-
5221 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5223 G.postRedisplay "brightness";
5225 | 99 when state.mode = View -> (* c *)
5226 let (c, a, b), z =
5227 match state.prevcolumns with
5228 | None -> (1, 0, 0), 1.0
5229 | Some (columns, z) ->
5230 let cab =
5231 match columns with
5232 | Csplit (c, _) -> -c, 0, 0
5233 | Cmulti ((c, a, b), _) -> c, a, b
5234 | Csingle _ -> 1, 0, 0
5236 cab, z
5238 setcolumns View c a b;
5239 setzoom z;
5241 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5242 setzoom state.prevzoom
5244 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5245 begin match state.autoscroll with
5246 | None ->
5247 begin match state.mode with
5248 | Birdseye beye -> upbirdseye 1 beye
5249 | _ ->
5250 if ctrl
5251 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5252 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5254 | Some n ->
5255 setautoscrollspeed n false
5258 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5259 begin match state.autoscroll with
5260 | None ->
5261 begin match state.mode with
5262 | Birdseye beye -> downbirdseye 1 beye
5263 | _ ->
5264 if ctrl
5265 then gotoy_and_clear_text (clamp (conf.winh/2))
5266 else gotoy_and_clear_text (clamp conf.scrollstep)
5268 | Some n ->
5269 setautoscrollspeed n true
5272 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5273 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5274 if canpan ()
5275 then
5276 let dx =
5277 if ctrl
5278 then conf.winw / 2
5279 else conf.hscrollstep
5281 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5282 state.x <- state.x + dx;
5283 gotoy_and_clear_text state.y
5284 else (
5285 state.text <- "";
5286 G.postRedisplay "lef/right"
5289 | 0xff55 | 0xff9a -> (* (kp) prior *)
5290 let y =
5291 if ctrl
5292 then
5293 match state.layout with
5294 | [] -> state.y
5295 | l :: _ -> state.y - l.pagey
5296 else
5297 clamp (pgscale (-conf.winh))
5299 gotoghyll y
5301 | 0xff56 | 0xff9b -> (* (kp) next *)
5302 let y =
5303 if ctrl
5304 then
5305 match List.rev state.layout with
5306 | [] -> state.y
5307 | l :: _ -> getpagey l.pageno
5308 else
5309 clamp (pgscale conf.winh)
5311 gotoghyll y
5313 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5314 gotoghyll 0
5315 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5316 gotoghyll (clamp state.maxy)
5318 | 0xff53 | 0xff98
5319 when Wsi.withalt mask -> (* alt-(kp) right *)
5320 gotoghyll (getnav 1)
5321 | 0xff51 | 0xff96
5322 when Wsi.withalt mask -> (* alt-(kp) left *)
5323 gotoghyll (getnav ~-1)
5325 | 114 -> (* r *)
5326 reload ()
5328 | 118 when conf.debug -> (* v *)
5329 state.rects <- [];
5330 List.iter (fun l ->
5331 match getopaque l.pageno with
5332 | None -> ()
5333 | Some opaque ->
5334 let x0, y0, x1, y1 = pagebbox opaque in
5335 let a,b = float x0, float y0 in
5336 let c,d = float x1, float y0 in
5337 let e,f = float x1, float y1 in
5338 let h,j = float x0, float y1 in
5339 let rect = (a,b,c,d,e,f,h,j) in
5340 debugrect rect;
5341 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5342 ) state.layout;
5343 G.postRedisplay "v";
5345 | _ ->
5346 vlog "huh? %s" (Wsi.keyname key)
5349 let linknavkeyboard key mask linknav =
5350 let getpage pageno =
5351 let rec loop = function
5352 | [] -> None
5353 | l :: _ when l.pageno = pageno -> Some l
5354 | _ :: rest -> loop rest
5355 in loop state.layout
5357 let doexact (pageno, n) =
5358 match getopaque pageno, getpage pageno with
5359 | Some opaque, Some l ->
5360 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5361 then
5362 let under = getlink opaque n in
5363 G.postRedisplay "link gotounder";
5364 gotounder under;
5365 state.mode <- View;
5366 else
5367 let opt, dir =
5368 match key with
5369 | 0xff50 -> (* home *)
5370 Some (findlink opaque LDfirst), -1
5372 | 0xff57 -> (* end *)
5373 Some (findlink opaque LDlast), 1
5375 | 0xff51 -> (* left *)
5376 Some (findlink opaque (LDleft n)), -1
5378 | 0xff53 -> (* right *)
5379 Some (findlink opaque (LDright n)), 1
5381 | 0xff52 -> (* up *)
5382 Some (findlink opaque (LDup n)), -1
5384 | 0xff54 -> (* down *)
5385 Some (findlink opaque (LDdown n)), 1
5387 | _ -> None, 0
5389 let pwl l dir =
5390 begin match findpwl l.pageno dir with
5391 | Pwlnotfound -> ()
5392 | Pwl pageno ->
5393 let notfound dir =
5394 state.mode <- LinkNav (Ltgendir dir);
5395 let y, h = getpageyh pageno in
5396 let y =
5397 if dir < 0
5398 then y + h - conf.winh
5399 else y
5401 gotoy y
5403 begin match getopaque pageno, getpage pageno with
5404 | Some opaque, Some _ ->
5405 let link =
5406 let ld = if dir > 0 then LDfirst else LDlast in
5407 findlink opaque ld
5409 begin match link with
5410 | Lfound m ->
5411 showlinktype (getlink opaque m);
5412 state.mode <- LinkNav (Ltexact (pageno, m));
5413 G.postRedisplay "linknav jpage";
5414 | _ -> notfound dir
5415 end;
5416 | _ -> notfound dir
5417 end;
5418 end;
5420 begin match opt with
5421 | Some Lnotfound -> pwl l dir;
5422 | Some (Lfound m) ->
5423 if m = n
5424 then pwl l dir
5425 else (
5426 let _, y0, _, y1 = getlinkrect opaque m in
5427 if y0 < l.pagey
5428 then gotopage1 l.pageno y0
5429 else (
5430 let d = fstate.fontsize + 1 in
5431 if y1 - l.pagey > l.pagevh - d
5432 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5433 else G.postRedisplay "linknav";
5435 showlinktype (getlink opaque m);
5436 state.mode <- LinkNav (Ltexact (l.pageno, m));
5439 | None -> viewkeyboard key mask
5440 end;
5441 | _ -> viewkeyboard key mask
5443 if key = 0xff63
5444 then (
5445 state.mode <- View;
5446 G.postRedisplay "leave linknav"
5448 else
5449 match linknav with
5450 | Ltgendir _ -> viewkeyboard key mask
5451 | Ltexact exact -> doexact exact
5454 let keyboard key mask =
5455 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5456 then wcmd "interrupt"
5457 else state.uioh <- state.uioh#key key mask
5460 let birdseyekeyboard key mask
5461 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5462 let incr =
5463 match conf.columns with
5464 | Csingle _ -> 1
5465 | Cmulti ((c, _, _), _) -> c
5466 | Csplit _ -> failwith "bird's eye split mode"
5468 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5469 match key with
5470 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5471 let y, h = getpageyh pageno in
5472 let top = (conf.winh - h) / 2 in
5473 gotoy (max 0 (y - top))
5474 | 0xff0d (* enter *)
5475 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5476 | 0xff1b -> leavebirdseye beye true (* escape *)
5477 | 0xff52 -> upbirdseye incr beye (* up *)
5478 | 0xff54 -> downbirdseye incr beye (* down *)
5479 | 0xff51 -> upbirdseye 1 beye (* left *)
5480 | 0xff53 -> downbirdseye 1 beye (* right *)
5482 | 0xff55 -> (* prior *)
5483 begin match state.layout with
5484 | l :: _ ->
5485 if l.pagey != 0
5486 then (
5487 state.mode <- Birdseye (
5488 oconf, leftx, l.pageno, hooverpageno, anchor
5490 gotopage1 l.pageno 0;
5492 else (
5493 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5494 match layout with
5495 | [] -> gotoy (clamp (-conf.winh))
5496 | l :: _ ->
5497 state.mode <- Birdseye (
5498 oconf, leftx, l.pageno, hooverpageno, anchor
5500 gotopage1 l.pageno 0
5503 | [] -> gotoy (clamp (-conf.winh))
5504 end;
5506 | 0xff56 -> (* next *)
5507 begin match List.rev state.layout with
5508 | l :: _ ->
5509 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5510 begin match layout with
5511 | [] ->
5512 let incr = l.pageh - l.pagevh in
5513 if incr = 0
5514 then (
5515 state.mode <-
5516 Birdseye (
5517 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5519 G.postRedisplay "birdseye pagedown";
5521 else gotoy (clamp (incr + conf.interpagespace*2));
5523 | l :: _ ->
5524 state.mode <-
5525 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5526 gotopage1 l.pageno 0;
5529 | [] -> gotoy (clamp conf.winh)
5530 end;
5532 | 0xff50 -> (* home *)
5533 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5534 gotopage1 0 0
5536 | 0xff57 -> (* end *)
5537 let pageno = state.pagecount - 1 in
5538 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5539 if not (pagevisible state.layout pageno)
5540 then
5541 let h =
5542 match List.rev state.pdims with
5543 | [] -> conf.winh
5544 | (_, _, h, _) :: _ -> h
5546 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5547 else G.postRedisplay "birdseye end";
5548 | _ -> viewkeyboard key mask
5551 let drawpage l linkindexbase =
5552 let color =
5553 match state.mode with
5554 | Textentry _ -> scalecolor 0.4
5555 | LinkNav _
5556 | View -> scalecolor 1.0
5557 | Birdseye (_, _, pageno, hooverpageno, _) ->
5558 if l.pageno = hooverpageno
5559 then scalecolor 0.9
5560 else (
5561 if l.pageno = pageno
5562 then scalecolor 1.0
5563 else scalecolor 0.8
5566 drawtiles l color;
5567 begin match getopaque l.pageno with
5568 | Some opaque ->
5569 if tileready l l.pagex l.pagey
5570 then
5571 let x = l.pagedispx - l.pagex
5572 and y = l.pagedispy - l.pagey in
5573 let hlmask =
5574 match conf.columns with
5575 | Csingle _ | Cmulti _ ->
5576 (if conf.hlinks then 1 else 0)
5577 + (if state.glinks
5578 && not (isbirdseye state.mode) then 2 else 0)
5579 | _ -> 0
5581 let s =
5582 match state.mode with
5583 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5584 | _ -> ""
5586 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5587 else 0
5589 | _ -> 0
5590 end;
5593 let scrollindicator () =
5594 let sbw, ph, sh = state.uioh#scrollph in
5595 let sbh, pw, sw = state.uioh#scrollpw in
5597 GlDraw.color (0.64, 0.64, 0.64);
5598 GlDraw.rect
5599 (float (conf.winw - sbw), 0.)
5600 (float conf.winw, float conf.winh)
5602 GlDraw.rect
5603 (0., float (conf.winh - sbh))
5604 (float (conf.winw - state.scrollw - 1), float conf.winh)
5606 GlDraw.color (0.0, 0.0, 0.0);
5608 GlDraw.rect
5609 (float (conf.winw - sbw), ph)
5610 (float conf.winw, ph +. sh)
5612 GlDraw.rect
5613 (pw, float (conf.winh - sbh))
5614 (pw +. sw, float conf.winh)
5618 let showsel () =
5619 match state.mstate with
5620 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5623 | Msel ((x0, y0), (x1, y1)) ->
5624 let rec loop = function
5625 | l :: ls ->
5626 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5627 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5628 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5629 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5630 then
5631 match getopaque l.pageno with
5632 | Some opaque ->
5633 let x0, y0 = pagetranslatepoint l x0 y0 in
5634 let x1, y1 = pagetranslatepoint l x1 y1 in
5635 seltext opaque (x0, y0, x1, y1);
5636 | _ -> ()
5637 else loop ls
5638 | [] -> ()
5640 loop state.layout
5643 let showrects rects =
5644 Gl.enable `blend;
5645 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5646 GlDraw.polygon_mode `both `fill;
5647 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5648 List.iter
5649 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5650 List.iter (fun l ->
5651 if l.pageno = pageno
5652 then (
5653 let dx = float (l.pagedispx - l.pagex) in
5654 let dy = float (l.pagedispy - l.pagey) in
5655 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5656 GlDraw.begins `quads;
5658 GlDraw.vertex2 (x0+.dx, y0+.dy);
5659 GlDraw.vertex2 (x1+.dx, y1+.dy);
5660 GlDraw.vertex2 (x2+.dx, y2+.dy);
5661 GlDraw.vertex2 (x3+.dx, y3+.dy);
5663 GlDraw.ends ();
5665 ) state.layout
5666 ) rects
5668 Gl.disable `blend;
5671 let display () =
5672 GlClear.color (scalecolor2 conf.bgcolor);
5673 GlClear.clear [`color];
5674 let rec loop linkindexbase = function
5675 | l :: rest ->
5676 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5677 loop linkindexbase rest
5678 | [] -> ()
5680 loop 0 state.layout;
5681 let rects =
5682 match state.mode with
5683 | LinkNav (Ltexact (pageno, linkno)) ->
5684 begin match getopaque pageno with
5685 | Some opaque ->
5686 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5687 (pageno, 5, (
5688 float x0, float y0,
5689 float x1, float y0,
5690 float x1, float y1,
5691 float x0, float y1)
5692 ) :: state.rects
5693 | None -> state.rects
5695 | _ -> state.rects
5697 showrects rects;
5698 showsel ();
5699 state.uioh#display;
5700 begin match state.mstate with
5701 | Mzoomrect ((x0, y0), (x1, y1)) ->
5702 Gl.enable `blend;
5703 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5704 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5705 GlDraw.rect (float x0, float y0)
5706 (float x1, float y1);
5707 Gl.disable `blend;
5708 | _ -> ()
5709 end;
5710 enttext ();
5711 scrollindicator ();
5712 Wsi.swapb ();
5715 let zoomrect x y x1 y1 =
5716 let x0 = min x x1
5717 and x1 = max x x1
5718 and y0 = min y y1 in
5719 gotoy (state.y + y0);
5720 state.anchor <- getanchor ();
5721 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5722 let margin =
5723 if state.w < conf.winw - state.scrollw
5724 then (conf.winw - state.scrollw - state.w) / 2
5725 else 0
5727 state.x <- (state.x + margin) - x0;
5728 setzoom zoom;
5729 Wsi.setcursor Wsi.CURSOR_INHERIT;
5730 state.mstate <- Mnone;
5733 let scrollx x =
5734 let winw = conf.winw - state.scrollw - 1 in
5735 let s = float x /. float winw in
5736 let destx = truncate (float (state.w + winw) *. s) in
5737 state.x <- winw - destx;
5738 gotoy_and_clear_text state.y;
5739 state.mstate <- Mscrollx;
5742 let scrolly y =
5743 let s = float y /. float conf.winh in
5744 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5745 gotoy_and_clear_text desty;
5746 state.mstate <- Mscrolly;
5749 let viewmouse button down x y mask =
5750 match button with
5751 | n when (n == 4 || n == 5) && not down ->
5752 if Wsi.withctrl mask
5753 then (
5754 match state.mstate with
5755 | Mzoom (oldn, i) ->
5756 if oldn = n
5757 then (
5758 if i = 2
5759 then
5760 let incr =
5761 match n with
5762 | 5 ->
5763 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5764 | _ ->
5765 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5767 let zoom = conf.zoom -. incr in
5768 setzoom zoom;
5769 state.mstate <- Mzoom (n, 0);
5770 else
5771 state.mstate <- Mzoom (n, i+1);
5773 else state.mstate <- Mzoom (n, 0)
5775 | _ -> state.mstate <- Mzoom (n, 0)
5777 else (
5778 match state.autoscroll with
5779 | Some step -> setautoscrollspeed step (n=4)
5780 | None ->
5781 if conf.wheelbypage
5782 then (
5783 if n = 4
5784 then prevpage ()
5785 else nextpage ()
5787 else
5788 let incr =
5789 if n = 4
5790 then -conf.scrollstep
5791 else conf.scrollstep
5793 let incr = incr * 2 in
5794 let y = clamp incr in
5795 gotoy_and_clear_text y
5798 | n when (n = 6 || n = 7) && not down && canpan () ->
5799 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5800 gotoy_and_clear_text state.y
5802 | 1 when Wsi.withshift mask ->
5803 state.mstate <- Mnone;
5804 if not down then (
5805 match unproject x y with
5806 | Some (pageno, ux, uy) ->
5807 let cmd = Printf.sprintf
5808 "%s %s %d %d %d"
5809 conf.stcmd state.path pageno ux uy
5811 popen cmd []
5812 | None -> ()
5815 | 1 when Wsi.withctrl mask ->
5816 if down
5817 then (
5818 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5819 state.mstate <- Mpan (x, y)
5821 else
5822 state.mstate <- Mnone
5824 | 3 ->
5825 if down
5826 then (
5827 Wsi.setcursor Wsi.CURSOR_CYCLE;
5828 let p = (x, y) in
5829 state.mstate <- Mzoomrect (p, p)
5831 else (
5832 match state.mstate with
5833 | Mzoomrect ((x0, y0), _) ->
5834 if abs (x-x0) > 10 && abs (y - y0) > 10
5835 then zoomrect x0 y0 x y
5836 else (
5837 state.mstate <- Mnone;
5838 Wsi.setcursor Wsi.CURSOR_INHERIT;
5839 G.postRedisplay "kill accidental zoom rect";
5841 | _ ->
5842 Wsi.setcursor Wsi.CURSOR_INHERIT;
5843 state.mstate <- Mnone
5846 | 1 when x > conf.winw - state.scrollw ->
5847 if down
5848 then
5849 let _, position, sh = state.uioh#scrollph in
5850 if y > truncate position && y < truncate (position +. sh)
5851 then state.mstate <- Mscrolly
5852 else scrolly y
5853 else
5854 state.mstate <- Mnone
5856 | 1 when y > conf.winh - state.hscrollh ->
5857 if down
5858 then
5859 let _, position, sw = state.uioh#scrollpw in
5860 if x > truncate position && x < truncate (position +. sw)
5861 then state.mstate <- Mscrollx
5862 else scrollx x
5863 else
5864 state.mstate <- Mnone
5866 | 1 ->
5867 let dest = if down then getunder x y else Unone in
5868 begin match dest with
5869 | Ulinkgoto _
5870 | Ulinkuri _
5871 | Uremote _
5872 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5873 gotounder dest
5875 | Unone when down ->
5876 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5877 state.mstate <- Mpan (x, y);
5879 | Unone | Utext _ ->
5880 if down
5881 then (
5882 if conf.angle mod 360 = 0
5883 then (
5884 state.mstate <- Msel ((x, y), (x, y));
5885 G.postRedisplay "mouse select";
5888 else (
5889 match state.mstate with
5890 | Mnone -> ()
5892 | Mzoom _ | Mscrollx | Mscrolly ->
5893 state.mstate <- Mnone
5895 | Mzoomrect ((x0, y0), _) ->
5896 zoomrect x0 y0 x y
5898 | Mpan _ ->
5899 Wsi.setcursor Wsi.CURSOR_INHERIT;
5900 state.mstate <- Mnone
5902 | Msel ((x0, y0), (x1, y1)) ->
5903 let rec loop = function
5904 | [] -> ()
5905 | l :: rest ->
5906 let inside =
5907 let a0 = l.pagedispy in
5908 let a1 = a0 + l.pagevh in
5909 let b0 = l.pagedispx in
5910 let b1 = b0 + l.pagevw in
5911 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5912 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5914 if inside
5915 then
5916 match getopaque l.pageno with
5917 | Some opaque ->
5918 begin
5919 match Ne.pipe () with
5920 | Ne.Exn exn ->
5921 showtext '!'
5922 (Printf.sprintf
5923 "can not create sel pipe: %s"
5924 (Printexc.to_string exn));
5925 | Ne.Res (r, w) ->
5926 let doclose what fd =
5927 Ne.clo fd (fun msg ->
5928 dolog "%s close failed: %s" what msg)
5931 popen conf.selcmd [r, 0; w, -1];
5932 copysel w opaque;
5933 doclose "pipe/r" r;
5934 G.postRedisplay "copysel";
5935 with exn ->
5936 dolog "can not execute %S: %s"
5937 conf.selcmd (Printexc.to_string exn);
5938 doclose "pipe/r" r;
5939 doclose "pipe/w" w;
5941 | None -> ()
5942 else loop rest
5944 loop state.layout;
5945 Wsi.setcursor Wsi.CURSOR_INHERIT;
5946 state.mstate <- Mnone;
5950 | _ -> ()
5953 let birdseyemouse button down x y mask
5954 (conf, leftx, _, hooverpageno, anchor) =
5955 match button with
5956 | 1 when down ->
5957 let rec loop = function
5958 | [] -> ()
5959 | l :: rest ->
5960 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5961 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5962 then (
5963 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5965 else loop rest
5967 loop state.layout
5968 | 3 -> ()
5969 | _ -> viewmouse button down x y mask
5972 let mouse button down x y mask =
5973 state.uioh <- state.uioh#button button down x y mask;
5976 let motion ~x ~y =
5977 state.uioh <- state.uioh#motion x y
5980 let pmotion ~x ~y =
5981 state.uioh <- state.uioh#pmotion x y;
5984 let uioh = object
5985 method display = ()
5987 method key key mask =
5988 begin match state.mode with
5989 | Textentry textentry -> textentrykeyboard key mask textentry
5990 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5991 | View -> viewkeyboard key mask
5992 | LinkNav linknav -> linknavkeyboard key mask linknav
5993 end;
5994 state.uioh
5996 method button button bstate x y mask =
5997 begin match state.mode with
5998 | LinkNav _
5999 | View -> viewmouse button bstate x y mask
6000 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6001 | Textentry _ -> ()
6002 end;
6003 state.uioh
6005 method motion x y =
6006 begin match state.mode with
6007 | Textentry _ -> ()
6008 | View | Birdseye _ | LinkNav _ ->
6009 match state.mstate with
6010 | Mzoom _ | Mnone -> ()
6012 | Mpan (x0, y0) ->
6013 let dx = x - x0
6014 and dy = y0 - y in
6015 state.mstate <- Mpan (x, y);
6016 if canpan ()
6017 then state.x <- state.x + dx;
6018 let y = clamp dy in
6019 gotoy_and_clear_text y
6021 | Msel (a, _) ->
6022 state.mstate <- Msel (a, (x, y));
6023 G.postRedisplay "motion select";
6025 | Mscrolly ->
6026 let y = min conf.winh (max 0 y) in
6027 scrolly y
6029 | Mscrollx ->
6030 let x = min conf.winw (max 0 x) in
6031 scrollx x
6033 | Mzoomrect (p0, _) ->
6034 state.mstate <- Mzoomrect (p0, (x, y));
6035 G.postRedisplay "motion zoomrect";
6036 end;
6037 state.uioh
6039 method pmotion x y =
6040 begin match state.mode with
6041 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6042 let rec loop = function
6043 | [] ->
6044 if hooverpageno != -1
6045 then (
6046 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6047 G.postRedisplay "pmotion birdseye no hoover";
6049 | l :: rest ->
6050 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6051 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6052 then (
6053 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6054 G.postRedisplay "pmotion birdseye hoover";
6056 else loop rest
6058 loop state.layout
6060 | Textentry _ -> ()
6062 | LinkNav _
6063 | View ->
6064 match state.mstate with
6065 | Mnone -> updateunder x y
6066 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6068 end;
6069 state.uioh
6071 method infochanged _ = ()
6073 method scrollph =
6074 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
6075 let p, h = scrollph state.y maxy in
6076 state.scrollw, p, h
6078 method scrollpw =
6079 let winw = conf.winw - state.scrollw - 1 in
6080 let fwinw = float winw in
6081 let sw =
6082 let sw = fwinw /. float state.w in
6083 let sw = fwinw *. sw in
6084 max sw (float conf.scrollh)
6086 let position, sw =
6087 let f = state.w+winw in
6088 let r = float (winw-state.x) /. float f in
6089 let p = fwinw *. r in
6090 p-.sw/.2., sw
6092 let sw =
6093 if position +. sw > fwinw
6094 then fwinw -. position
6095 else sw
6097 state.hscrollh, position, sw
6099 method modehash =
6100 let modename =
6101 match state.mode with
6102 | LinkNav _ -> "links"
6103 | Textentry _ -> "textentry"
6104 | Birdseye _ -> "birdseye"
6105 | View -> "view"
6107 findkeyhash conf modename
6108 end;;
6110 module Config =
6111 struct
6112 open Parser
6114 let fontpath = ref "";;
6116 module KeyMap =
6117 Map.Make (struct type t = (int * int) let compare = compare end);;
6119 let unent s =
6120 let l = String.length s in
6121 let b = Buffer.create l in
6122 unent b s 0 l;
6123 Buffer.contents b;
6126 let home =
6127 try Sys.getenv "HOME"
6128 with exn ->
6129 prerr_endline
6130 ("Can not determine home directory location: " ^
6131 Printexc.to_string exn);
6135 let modifier_of_string = function
6136 | "alt" -> Wsi.altmask
6137 | "shift" -> Wsi.shiftmask
6138 | "ctrl" | "control" -> Wsi.ctrlmask
6139 | "meta" -> Wsi.metamask
6140 | _ -> 0
6143 let key_of_string =
6144 let r = Str.regexp "-" in
6145 fun s ->
6146 let elems = Str.full_split r s in
6147 let f n k m =
6148 let g s =
6149 let m1 = modifier_of_string s in
6150 if m1 = 0
6151 then (Wsi.namekey s, m)
6152 else (k, m lor m1)
6153 in function
6154 | Str.Delim s when n land 1 = 0 -> g s
6155 | Str.Text s -> g s
6156 | Str.Delim _ -> (k, m)
6158 let rec loop n k m = function
6159 | [] -> (k, m)
6160 | x :: xs ->
6161 let k, m = f n k m x in
6162 loop (n+1) k m xs
6164 loop 0 0 0 elems
6167 let keys_of_string =
6168 let r = Str.regexp "[ \t]" in
6169 fun s ->
6170 let elems = Str.split r s in
6171 List.map key_of_string elems
6174 let copykeyhashes c =
6175 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6178 let config_of c attrs =
6179 let apply c k v =
6181 match k with
6182 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6183 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6184 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6185 | "preload" -> { c with preload = bool_of_string v }
6186 | "page-bias" -> { c with pagebias = int_of_string v }
6187 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6188 | "horizontal-scroll-step" ->
6189 { c with hscrollstep = max (int_of_string v) 1 }
6190 | "auto-scroll-step" ->
6191 { c with autoscrollstep = max 0 (int_of_string v) }
6192 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6193 | "crop-hack" -> { c with crophack = bool_of_string v }
6194 | "throttle" ->
6195 let mw =
6196 match String.lowercase v with
6197 | "true" -> Some infinity
6198 | "false" -> None
6199 | f -> Some (float_of_string f)
6201 { c with maxwait = mw}
6202 | "highlight-links" -> { c with hlinks = bool_of_string v }
6203 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6204 | "vertical-margin" ->
6205 { c with interpagespace = max 0 (int_of_string v) }
6206 | "zoom" ->
6207 let zoom = float_of_string v /. 100. in
6208 let zoom = max zoom 0.0 in
6209 { c with zoom = zoom }
6210 | "presentation" -> { c with presentation = bool_of_string v }
6211 | "rotation-angle" -> { c with angle = int_of_string v }
6212 | "width" -> { c with winw = max 20 (int_of_string v) }
6213 | "height" -> { c with winh = max 20 (int_of_string v) }
6214 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6215 | "proportional-display" -> { c with proportional = bool_of_string v }
6216 | "pixmap-cache-size" ->
6217 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6218 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6219 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6220 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6221 | "persistent-location" -> { c with jumpback = bool_of_string v }
6222 | "background-color" -> { c with bgcolor = color_of_string v }
6223 | "scrollbar-in-presentation" ->
6224 { c with scrollbarinpm = bool_of_string v }
6225 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6226 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6227 | "mupdf-store-size" ->
6228 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6229 | "checkers" -> { c with checkers = bool_of_string v }
6230 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6231 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6232 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6233 | "uri-launcher" -> { c with urilauncher = unent v }
6234 | "path-launcher" -> { c with pathlauncher = unent v }
6235 | "color-space" -> { c with colorspace = colorspace_of_string v }
6236 | "invert-colors" -> { c with invert = bool_of_string v }
6237 | "brightness" -> { c with colorscale = float_of_string v }
6238 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6239 | "ghyllscroll" ->
6240 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6241 | "columns" ->
6242 let (n, _, _) as nab = multicolumns_of_string v in
6243 if n < 0
6244 then { c with columns = Csplit (-n, [||]) }
6245 else { c with columns = Cmulti (nab, [||]) }
6246 | "birds-eye-columns" ->
6247 { c with beyecolumns = Some (max (int_of_string v) 2) }
6248 | "selection-command" -> { c with selcmd = unent v }
6249 | "synctex-command" -> { c with stcmd = unent v }
6250 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6251 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6252 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6253 | "use-pbo" -> { c with usepbo = bool_of_string v }
6254 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6255 | _ -> c
6256 with exn ->
6257 prerr_endline ("Error processing attribute (`" ^
6258 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6261 let rec fold c = function
6262 | [] -> c
6263 | (k, v) :: rest ->
6264 let c = apply c k v in
6265 fold c rest
6267 fold { c with keyhashes = copykeyhashes c } attrs;
6270 let fromstring f pos n v d =
6271 try f v
6272 with exn ->
6273 dolog "Error processing attribute (%S=%S) at %d\n%s"
6274 n v pos (Printexc.to_string exn)
6279 let bookmark_of attrs =
6280 let rec fold title page rely visy = function
6281 | ("title", v) :: rest -> fold v page rely visy rest
6282 | ("page", v) :: rest -> fold title v rely visy rest
6283 | ("rely", v) :: rest -> fold title page v visy rest
6284 | ("visy", v) :: rest -> fold title page rely v rest
6285 | _ :: rest -> fold title page rely visy rest
6286 | [] -> title, page, rely, visy
6288 fold "invalid" "0" "0" "0" attrs
6291 let doc_of attrs =
6292 let rec fold path page rely pan visy = function
6293 | ("path", v) :: rest -> fold v page rely pan visy rest
6294 | ("page", v) :: rest -> fold path v rely pan visy rest
6295 | ("rely", v) :: rest -> fold path page v pan visy rest
6296 | ("pan", v) :: rest -> fold path page rely v visy rest
6297 | ("visy", v) :: rest -> fold path page rely pan v rest
6298 | _ :: rest -> fold path page rely pan visy rest
6299 | [] -> path, page, rely, pan, visy
6301 fold "" "0" "0" "0" "0" attrs
6304 let map_of attrs =
6305 let rec fold rs ls = function
6306 | ("out", v) :: rest -> fold v ls rest
6307 | ("in", v) :: rest -> fold rs v rest
6308 | _ :: rest -> fold ls rs rest
6309 | [] -> ls, rs
6311 fold "" "" attrs
6314 let setconf dst src =
6315 dst.scrollbw <- src.scrollbw;
6316 dst.scrollh <- src.scrollh;
6317 dst.icase <- src.icase;
6318 dst.preload <- src.preload;
6319 dst.pagebias <- src.pagebias;
6320 dst.verbose <- src.verbose;
6321 dst.scrollstep <- src.scrollstep;
6322 dst.maxhfit <- src.maxhfit;
6323 dst.crophack <- src.crophack;
6324 dst.autoscrollstep <- src.autoscrollstep;
6325 dst.maxwait <- src.maxwait;
6326 dst.hlinks <- src.hlinks;
6327 dst.underinfo <- src.underinfo;
6328 dst.interpagespace <- src.interpagespace;
6329 dst.zoom <- src.zoom;
6330 dst.presentation <- src.presentation;
6331 dst.angle <- src.angle;
6332 dst.winw <- src.winw;
6333 dst.winh <- src.winh;
6334 dst.savebmarks <- src.savebmarks;
6335 dst.memlimit <- src.memlimit;
6336 dst.proportional <- src.proportional;
6337 dst.texcount <- src.texcount;
6338 dst.sliceheight <- src.sliceheight;
6339 dst.thumbw <- src.thumbw;
6340 dst.jumpback <- src.jumpback;
6341 dst.bgcolor <- src.bgcolor;
6342 dst.scrollbarinpm <- src.scrollbarinpm;
6343 dst.tilew <- src.tilew;
6344 dst.tileh <- src.tileh;
6345 dst.mustoresize <- src.mustoresize;
6346 dst.checkers <- src.checkers;
6347 dst.aalevel <- src.aalevel;
6348 dst.trimmargins <- src.trimmargins;
6349 dst.trimfuzz <- src.trimfuzz;
6350 dst.urilauncher <- src.urilauncher;
6351 dst.colorspace <- src.colorspace;
6352 dst.invert <- src.invert;
6353 dst.colorscale <- src.colorscale;
6354 dst.redirectstderr <- src.redirectstderr;
6355 dst.ghyllscroll <- src.ghyllscroll;
6356 dst.columns <- src.columns;
6357 dst.beyecolumns <- src.beyecolumns;
6358 dst.selcmd <- src.selcmd;
6359 dst.updatecurs <- src.updatecurs;
6360 dst.pathlauncher <- src.pathlauncher;
6361 dst.keyhashes <- copykeyhashes src;
6362 dst.hfsize <- src.hfsize;
6363 dst.hscrollstep <- src.hscrollstep;
6364 dst.pgscale <- src.pgscale;
6365 dst.usepbo <- src.usepbo;
6366 dst.wheelbypage <- src.wheelbypage;
6367 dst.stcmd <- src.stcmd;
6370 let get s =
6371 let h = Hashtbl.create 10 in
6372 let dc = { defconf with angle = defconf.angle } in
6373 let rec toplevel v t spos _ =
6374 match t with
6375 | Vdata | Vcdata | Vend -> v
6376 | Vopen ("llppconfig", _, closed) ->
6377 if closed
6378 then v
6379 else { v with f = llppconfig }
6380 | Vopen _ ->
6381 error "unexpected subelement at top level" s spos
6382 | Vclose _ -> error "unexpected close at top level" s spos
6384 and llppconfig v t spos _ =
6385 match t with
6386 | Vdata | Vcdata -> v
6387 | Vend -> error "unexpected end of input in llppconfig" s spos
6388 | Vopen ("defaults", attrs, closed) ->
6389 let c = config_of dc attrs in
6390 setconf dc c;
6391 if closed
6392 then v
6393 else { v with f = defaults }
6395 | Vopen ("ui-font", attrs, closed) ->
6396 let rec getsize size = function
6397 | [] -> size
6398 | ("size", v) :: rest ->
6399 let size =
6400 fromstring int_of_string spos "size" v fstate.fontsize in
6401 getsize size rest
6402 | l -> getsize size l
6404 fstate.fontsize <- getsize fstate.fontsize attrs;
6405 if closed
6406 then v
6407 else { v with f = uifont (Buffer.create 10) }
6409 | Vopen ("doc", attrs, closed) ->
6410 let pathent, spage, srely, span, svisy = doc_of attrs in
6411 let path = unent pathent
6412 and pageno = fromstring int_of_string spos "page" spage 0
6413 and rely = fromstring float_of_string spos "rely" srely 0.0
6414 and pan = fromstring int_of_string spos "pan" span 0
6415 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6416 let c = config_of dc attrs in
6417 let anchor = (pageno, rely, visy) in
6418 if closed
6419 then (Hashtbl.add h path (c, [], pan, anchor); v)
6420 else { v with f = doc path pan anchor c [] }
6422 | Vopen _ ->
6423 error "unexpected subelement in llppconfig" s spos
6425 | Vclose "llppconfig" -> { v with f = toplevel }
6426 | Vclose _ -> error "unexpected close in llppconfig" s spos
6428 and defaults v t spos _ =
6429 match t with
6430 | Vdata | Vcdata -> v
6431 | Vend -> error "unexpected end of input in defaults" s spos
6432 | Vopen ("keymap", attrs, closed) ->
6433 let modename =
6434 try List.assoc "mode" attrs
6435 with Not_found -> "global" in
6436 if closed
6437 then v
6438 else
6439 let ret keymap =
6440 let h = findkeyhash dc modename in
6441 KeyMap.iter (Hashtbl.replace h) keymap;
6442 defaults
6444 { v with f = pkeymap ret KeyMap.empty }
6446 | Vopen (_, _, _) ->
6447 error "unexpected subelement in defaults" s spos
6449 | Vclose "defaults" ->
6450 { v with f = llppconfig }
6452 | Vclose _ -> error "unexpected close in defaults" s spos
6454 and uifont b v t spos epos =
6455 match t with
6456 | Vdata | Vcdata ->
6457 Buffer.add_substring b s spos (epos - spos);
6459 | Vopen (_, _, _) ->
6460 error "unexpected subelement in ui-font" s spos
6461 | Vclose "ui-font" ->
6462 if String.length !fontpath = 0
6463 then fontpath := Buffer.contents b;
6464 { v with f = llppconfig }
6465 | Vclose _ -> error "unexpected close in ui-font" s spos
6466 | Vend -> error "unexpected end of input in ui-font" s spos
6468 and doc path pan anchor c bookmarks v t spos _ =
6469 match t with
6470 | Vdata | Vcdata -> v
6471 | Vend -> error "unexpected end of input in doc" s spos
6472 | Vopen ("bookmarks", _, closed) ->
6473 if closed
6474 then v
6475 else { v with f = pbookmarks path pan anchor c bookmarks }
6477 | Vopen ("keymap", attrs, closed) ->
6478 let modename =
6479 try List.assoc "mode" attrs
6480 with Not_found -> "global"
6482 if closed
6483 then v
6484 else
6485 let ret keymap =
6486 let h = findkeyhash c modename in
6487 KeyMap.iter (Hashtbl.replace h) keymap;
6488 doc path pan anchor c bookmarks
6490 { v with f = pkeymap ret KeyMap.empty }
6492 | Vopen (_, _, _) ->
6493 error "unexpected subelement in doc" s spos
6495 | Vclose "doc" ->
6496 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6497 { v with f = llppconfig }
6499 | Vclose _ -> error "unexpected close in doc" s spos
6501 and pkeymap ret keymap v t spos _ =
6502 match t with
6503 | Vdata | Vcdata -> v
6504 | Vend -> error "unexpected end of input in keymap" s spos
6505 | Vopen ("map", attrs, closed) ->
6506 let r, l = map_of attrs in
6507 let kss = fromstring keys_of_string spos "in" r [] in
6508 let lss = fromstring keys_of_string spos "out" l [] in
6509 let keymap =
6510 match kss with
6511 | [] -> keymap
6512 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6513 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6515 if closed
6516 then { v with f = pkeymap ret keymap }
6517 else
6518 let f () = v in
6519 { v with f = skip "map" f }
6521 | Vopen _ ->
6522 error "unexpected subelement in keymap" s spos
6524 | Vclose "keymap" ->
6525 { v with f = ret keymap }
6527 | Vclose _ -> error "unexpected close in keymap" s spos
6529 and pbookmarks path pan anchor c bookmarks v t spos _ =
6530 match t with
6531 | Vdata | Vcdata -> v
6532 | Vend -> error "unexpected end of input in bookmarks" s spos
6533 | Vopen ("item", attrs, closed) ->
6534 let titleent, spage, srely, svisy = bookmark_of attrs in
6535 let page = fromstring int_of_string spos "page" spage 0
6536 and rely = fromstring float_of_string spos "rely" srely 0.0
6537 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6538 let bookmarks =
6539 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6541 if closed
6542 then { v with f = pbookmarks path pan anchor c bookmarks }
6543 else
6544 let f () = v in
6545 { v with f = skip "item" f }
6547 | Vopen _ ->
6548 error "unexpected subelement in bookmarks" s spos
6550 | Vclose "bookmarks" ->
6551 { v with f = doc path pan anchor c bookmarks }
6553 | Vclose _ -> error "unexpected close in bookmarks" s spos
6555 and skip tag f v t spos _ =
6556 match t with
6557 | Vdata | Vcdata -> v
6558 | Vend ->
6559 error ("unexpected end of input in skipped " ^ tag) s spos
6560 | Vopen (tag', _, closed) ->
6561 if closed
6562 then v
6563 else
6564 let f' () = { v with f = skip tag f } in
6565 { v with f = skip tag' f' }
6566 | Vclose ctag ->
6567 if tag = ctag
6568 then f ()
6569 else error ("unexpected close in skipped " ^ tag) s spos
6572 parse { f = toplevel; accu = () } s;
6573 h, dc;
6576 let do_load f ic =
6578 let len = in_channel_length ic in
6579 let s = String.create len in
6580 really_input ic s 0 len;
6581 f s;
6582 with
6583 | Parse_error (msg, s, pos) ->
6584 let subs = subs s pos in
6585 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6586 failwith ("parse error: " ^ s)
6588 | exn ->
6589 failwith ("config load error: " ^ Printexc.to_string exn)
6592 let defconfpath =
6593 let dir =
6595 let dir = Filename.concat home ".config" in
6596 if Sys.is_directory dir then dir else home
6597 with _ -> home
6599 Filename.concat dir "llpp.conf"
6602 let confpath = ref defconfpath;;
6604 let load1 f =
6605 if Sys.file_exists !confpath
6606 then
6607 match
6608 (try Some (open_in_bin !confpath)
6609 with exn ->
6610 prerr_endline
6611 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6612 Printexc.to_string exn);
6613 None
6615 with
6616 | Some ic ->
6617 let success =
6619 f (do_load get ic)
6620 with exn ->
6621 prerr_endline
6622 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6623 Printexc.to_string exn);
6624 false
6626 close_in ic;
6627 success
6629 | None -> false
6630 else
6631 f (Hashtbl.create 0, defconf)
6634 let load () =
6635 let f (h, dc) =
6636 let pc, pb, px, pa =
6638 Hashtbl.find h (Filename.basename state.path)
6639 with Not_found -> dc, [], 0, emptyanchor
6641 setconf defconf dc;
6642 setconf conf pc;
6643 state.bookmarks <- pb;
6644 state.x <- px;
6645 state.scrollw <- conf.scrollbw;
6646 if conf.jumpback
6647 then state.anchor <- pa;
6648 cbput state.hists.nav pa;
6649 true
6651 load1 f
6654 let add_attrs bb always dc c =
6655 let ob s a b =
6656 if always || a != b
6657 then Printf.bprintf bb "\n %s='%b'" s a
6658 and oi s a b =
6659 if always || a != b
6660 then Printf.bprintf bb "\n %s='%d'" s a
6661 and oI s a b =
6662 if always || a != b
6663 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6664 and oz s a b =
6665 if always || a <> b
6666 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6667 and oF s a b =
6668 if always || a <> b
6669 then Printf.bprintf bb "\n %s='%f'" s a
6670 and oc s a b =
6671 if always || a <> b
6672 then
6673 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6674 and oC s a b =
6675 if always || a <> b
6676 then
6677 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6678 and oR s a b =
6679 if always || a <> b
6680 then
6681 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6682 and os s a b =
6683 if always || a <> b
6684 then
6685 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6686 and og s a b =
6687 if always || a <> b
6688 then
6689 match a with
6690 | None -> ()
6691 | Some (_N, _A, _B) ->
6692 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6693 and oW s a b =
6694 if always || a <> b
6695 then
6696 let v =
6697 match a with
6698 | None -> "false"
6699 | Some f ->
6700 if f = infinity
6701 then "true"
6702 else string_of_float f
6704 Printf.bprintf bb "\n %s='%s'" s v
6705 and oco s a b =
6706 if always || a <> b
6707 then
6708 match a with
6709 | Cmulti ((n, a, b), _) when n > 1 ->
6710 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6711 | Csplit (n, _) when n > 1 ->
6712 Printf.bprintf bb "\n %s='%d'" s ~-n
6713 | _ -> ()
6714 and obeco s a b =
6715 if always || a <> b
6716 then
6717 match a with
6718 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6719 | _ -> ()
6721 let w, h =
6722 if always
6723 then dc.winw, dc.winh
6724 else
6725 match state.fullscreen with
6726 | Some wh -> wh
6727 | None -> c.winw, c.winh
6729 oi "width" w dc.winw;
6730 oi "height" h dc.winh;
6731 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6732 oi "scroll-handle-height" c.scrollh dc.scrollh;
6733 ob "case-insensitive-search" c.icase dc.icase;
6734 ob "preload" c.preload dc.preload;
6735 oi "page-bias" c.pagebias dc.pagebias;
6736 oi "scroll-step" c.scrollstep dc.scrollstep;
6737 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6738 ob "max-height-fit" c.maxhfit dc.maxhfit;
6739 ob "crop-hack" c.crophack dc.crophack;
6740 oW "throttle" c.maxwait dc.maxwait;
6741 ob "highlight-links" c.hlinks dc.hlinks;
6742 ob "under-cursor-info" c.underinfo dc.underinfo;
6743 oi "vertical-margin" c.interpagespace dc.interpagespace;
6744 oz "zoom" c.zoom dc.zoom;
6745 ob "presentation" c.presentation dc.presentation;
6746 oi "rotation-angle" c.angle dc.angle;
6747 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6748 ob "proportional-display" c.proportional dc.proportional;
6749 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6750 oi "tex-count" c.texcount dc.texcount;
6751 oi "slice-height" c.sliceheight dc.sliceheight;
6752 oi "thumbnail-width" c.thumbw dc.thumbw;
6753 ob "persistent-location" c.jumpback dc.jumpback;
6754 oc "background-color" c.bgcolor dc.bgcolor;
6755 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6756 oi "tile-width" c.tilew dc.tilew;
6757 oi "tile-height" c.tileh dc.tileh;
6758 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6759 ob "checkers" c.checkers dc.checkers;
6760 oi "aalevel" c.aalevel dc.aalevel;
6761 ob "trim-margins" c.trimmargins dc.trimmargins;
6762 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6763 os "uri-launcher" c.urilauncher dc.urilauncher;
6764 os "path-launcher" c.pathlauncher dc.pathlauncher;
6765 oC "color-space" c.colorspace dc.colorspace;
6766 ob "invert-colors" c.invert dc.invert;
6767 oF "brightness" c.colorscale dc.colorscale;
6768 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6769 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6770 oco "columns" c.columns dc.columns;
6771 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6772 os "selection-command" c.selcmd dc.selcmd;
6773 os "synctex-command" c.stcmd dc.stcmd;
6774 ob "update-cursor" c.updatecurs dc.updatecurs;
6775 oi "hint-font-size" c.hfsize dc.hfsize;
6776 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6777 oF "page-scroll-scale" c.pgscale dc.pgscale;
6778 ob "use-pbo" c.usepbo dc.usepbo;
6779 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6782 let keymapsbuf always dc c =
6783 let bb = Buffer.create 16 in
6784 let rec loop = function
6785 | [] -> ()
6786 | (modename, h) :: rest ->
6787 let dh = findkeyhash dc modename in
6788 if always || h <> dh
6789 then (
6790 if Hashtbl.length h > 0
6791 then (
6792 if Buffer.length bb > 0
6793 then Buffer.add_char bb '\n';
6794 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6795 Hashtbl.iter (fun i o ->
6796 let isdifferent = always ||
6798 let dO = Hashtbl.find dh i in
6799 dO <> o
6800 with Not_found -> true
6802 if isdifferent
6803 then
6804 let addkm (k, m) =
6805 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6806 if Wsi.withalt m then Buffer.add_string bb "alt-";
6807 if Wsi.withshift m then Buffer.add_string bb "shift-";
6808 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6809 Buffer.add_string bb (Wsi.keyname k);
6811 let addkms l =
6812 let rec loop = function
6813 | [] -> ()
6814 | km :: [] -> addkm km
6815 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6817 loop l
6819 Buffer.add_string bb "<map in='";
6820 addkm i;
6821 match o with
6822 | KMinsrt km ->
6823 Buffer.add_string bb "' out='";
6824 addkm km;
6825 Buffer.add_string bb "'/>\n"
6827 | KMinsrl kms ->
6828 Buffer.add_string bb "' out='";
6829 addkms kms;
6830 Buffer.add_string bb "'/>\n"
6832 | KMmulti (ins, kms) ->
6833 Buffer.add_char bb ' ';
6834 addkms ins;
6835 Buffer.add_string bb "' out='";
6836 addkms kms;
6837 Buffer.add_string bb "'/>\n"
6838 ) h;
6839 Buffer.add_string bb "</keymap>";
6842 loop rest
6844 loop c.keyhashes;
6848 let save () =
6849 let uifontsize = fstate.fontsize in
6850 let bb = Buffer.create 32768 in
6851 let f (h, dc) =
6852 let dc = if conf.bedefault then conf else dc in
6853 Buffer.add_string bb "<llppconfig>\n";
6855 if String.length !fontpath > 0
6856 then
6857 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6858 uifontsize
6859 !fontpath
6860 else (
6861 if uifontsize <> 14
6862 then
6863 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6866 Buffer.add_string bb "<defaults ";
6867 add_attrs bb true dc dc;
6868 let kb = keymapsbuf true dc dc in
6869 if Buffer.length kb > 0
6870 then (
6871 Buffer.add_string bb ">\n";
6872 Buffer.add_buffer bb kb;
6873 Buffer.add_string bb "\n</defaults>\n";
6875 else Buffer.add_string bb "/>\n";
6877 let adddoc path pan anchor c bookmarks =
6878 if bookmarks == [] && c = dc && anchor = emptyanchor
6879 then ()
6880 else (
6881 Printf.bprintf bb "<doc path='%s'"
6882 (enent path 0 (String.length path));
6884 if anchor <> emptyanchor
6885 then (
6886 let n, rely, visy = anchor in
6887 Printf.bprintf bb " page='%d'" n;
6888 if rely > 1e-6
6889 then
6890 Printf.bprintf bb " rely='%f'" rely
6892 if abs_float visy > 1e-6
6893 then
6894 Printf.bprintf bb " visy='%f'" visy
6898 if pan != 0
6899 then Printf.bprintf bb " pan='%d'" pan;
6901 add_attrs bb false dc c;
6902 let kb = keymapsbuf false dc c in
6904 begin match bookmarks with
6905 | [] ->
6906 if Buffer.length kb > 0
6907 then (
6908 Buffer.add_string bb ">\n";
6909 Buffer.add_buffer bb kb;
6910 Buffer.add_string bb "\n</doc>\n";
6912 else Buffer.add_string bb "/>\n"
6913 | _ ->
6914 Buffer.add_string bb ">\n<bookmarks>\n";
6915 List.iter (fun (title, _level, (page, rely, visy)) ->
6916 Printf.bprintf bb
6917 "<item title='%s' page='%d'"
6918 (enent title 0 (String.length title))
6919 page
6921 if rely > 1e-6
6922 then
6923 Printf.bprintf bb " rely='%f'" rely
6925 if abs_float visy > 1e-6
6926 then
6927 Printf.bprintf bb " visy='%f'" visy
6929 Buffer.add_string bb "/>\n";
6930 ) bookmarks;
6931 Buffer.add_string bb "</bookmarks>";
6932 if Buffer.length kb > 0
6933 then (
6934 Buffer.add_string bb "\n";
6935 Buffer.add_buffer bb kb;
6937 Buffer.add_string bb "\n</doc>\n";
6938 end;
6942 let pan, conf =
6943 match state.mode with
6944 | Birdseye (c, pan, _, _, _) ->
6945 let beyecolumns =
6946 match conf.columns with
6947 | Cmulti ((c, _, _), _) -> Some c
6948 | Csingle _ -> None
6949 | Csplit _ -> None
6950 and columns =
6951 match c.columns with
6952 | Cmulti (c, _) -> Cmulti (c, [||])
6953 | Csingle _ -> Csingle [||]
6954 | Csplit _ -> failwith "quit from bird's eye while split"
6956 pan, { c with beyecolumns = beyecolumns; columns = columns }
6957 | _ -> state.x, conf
6959 let basename = Filename.basename state.path in
6960 adddoc basename pan (getanchor ())
6961 (let conf =
6962 let autoscrollstep =
6963 match state.autoscroll with
6964 | Some step -> step
6965 | None -> conf.autoscrollstep
6967 match state.mode with
6968 | Birdseye (bc, _, _, _, _) ->
6969 { conf with
6970 zoom = bc.zoom;
6971 presentation = bc.presentation;
6972 interpagespace = bc.interpagespace;
6973 maxwait = bc.maxwait;
6974 autoscrollstep = autoscrollstep }
6975 | _ -> { conf with autoscrollstep = autoscrollstep }
6976 in conf)
6977 (if conf.savebmarks then state.bookmarks else []);
6979 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6980 if basename <> path
6981 then adddoc path x anchor c bookmarks
6982 ) h;
6983 Buffer.add_string bb "</llppconfig>\n";
6984 true;
6986 if load1 f && Buffer.length bb > 0
6987 then
6989 let tmp = !confpath ^ ".tmp" in
6990 let oc = open_out_bin tmp in
6991 Buffer.output_buffer oc bb;
6992 close_out oc;
6993 Unix.rename tmp !confpath;
6994 with exn ->
6995 prerr_endline
6996 ("error while saving configuration: " ^ Printexc.to_string exn)
6998 end;;
7000 let () =
7001 let trimcachepath = ref "" in
7002 Arg.parse
7003 (Arg.align
7004 [("-p", Arg.String (fun s -> state.password <- s) ,
7005 "<password> Set password");
7007 ("-f", Arg.String (fun s -> Config.fontpath := s),
7008 "<path> Set path to the user interface font");
7010 ("-c", Arg.String (fun s -> Config.confpath := s),
7011 "<path> Set path to the configuration file");
7013 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7014 "<path> Set path to the trim cache file");
7016 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7017 "<named destination> Set named destination");
7019 ("-wtmode", Arg.Set wtmode, "wt mode");
7021 ("-v", Arg.Unit (fun () ->
7022 Printf.printf
7023 "%s\nconfiguration path: %s\n"
7024 (version ())
7025 Config.defconfpath
7027 exit 0), " Print version and exit");
7030 (fun s -> state.path <- s)
7031 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7033 if String.length state.path = 0
7034 then (prerr_endline "file name missing"; exit 1);
7036 if not (Config.load ())
7037 then prerr_endline "failed to load configuration";
7039 let globalkeyhash = findkeyhash conf "global" in
7040 let wsfd, winw, winh = Wsi.init (object
7041 method expose =
7042 state.wthack <- false;
7043 if nogeomcmds state.geomcmds || platform == Posx
7044 then display ()
7045 else (
7046 GlClear.color (scalecolor2 conf.bgcolor);
7047 GlClear.clear [`color];
7049 method display = display ()
7050 method reshape w h = reshape w h
7051 method mouse b d x y m = mouse b d x y m
7052 method motion x y = state.mpos <- (x, y); motion x y
7053 method pmotion x y = state.mpos <- (x, y); pmotion x y
7054 method key k m =
7055 let mascm = m land (
7056 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7057 ) in
7058 match state.keystate with
7059 | KSnone ->
7060 let km = k, mascm in
7061 begin
7062 match
7063 let modehash = state.uioh#modehash in
7064 try Hashtbl.find modehash km
7065 with Not_found ->
7066 try Hashtbl.find globalkeyhash km
7067 with Not_found -> KMinsrt (k, m)
7068 with
7069 | KMinsrt (k, m) -> keyboard k m
7070 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7071 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7073 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7074 List.iter (fun (k, m) -> keyboard k m) insrt;
7075 state.keystate <- KSnone
7076 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7077 state.keystate <- KSinto (keys, insrt)
7078 | _ ->
7079 state.keystate <- KSnone
7081 method enter x y = state.mpos <- (x, y); pmotion x y
7082 method leave = state.mpos <- (-1, -1)
7083 method quit = raise Quit
7084 end) conf.winw conf.winh (platform = Posx) in
7086 state.wsfd <- wsfd;
7088 if not (
7089 List.exists GlMisc.check_extension
7090 [ "GL_ARB_texture_rectangle"
7091 ; "GL_EXT_texture_recangle"
7092 ; "GL_NV_texture_rectangle" ]
7094 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7096 let cr, sw =
7097 match Ne.pipe () with
7098 | Ne.Exn exn ->
7099 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
7100 exit 1
7101 | Ne.Res rw -> rw
7102 and sr, cw =
7103 match Ne.pipe () with
7104 | Ne.Exn exn ->
7105 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7106 exit 1
7107 | Ne.Res rw -> rw
7110 cloexec cr;
7111 cloexec sw;
7112 cloexec sr;
7113 cloexec cw;
7115 setcheckers conf.checkers;
7116 redirectstderr ();
7118 init (cr, cw) (
7119 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7120 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7121 !Config.fontpath, !trimcachepath,
7122 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7124 state.sr <- sr;
7125 state.sw <- sw;
7126 state.text <- "Opening " ^ (mbtoutf8 state.path);
7127 reshape winw winh;
7128 opendoc state.path state.password;
7129 state.uioh <- uioh;
7131 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7133 let rec loop deadline =
7134 let r =
7135 match state.errfd with
7136 | None -> [state.sr; state.wsfd]
7137 | Some fd -> [state.sr; state.wsfd; fd]
7139 if state.redisplay && not state.wthack
7140 then (
7141 state.redisplay <- false;
7142 display ();
7144 let timeout =
7145 let now = now () in
7146 if deadline > now
7147 then (
7148 if deadline = infinity
7149 then ~-.1.0
7150 else max 0.0 (deadline -. now)
7152 else 0.0
7154 let r, _, _ =
7155 try Unix.select r [] [] timeout
7156 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7158 begin match r with
7159 | [] ->
7160 state.ghyll None;
7161 let newdeadline =
7162 if state.ghyll == noghyll
7163 then
7164 match state.autoscroll with
7165 | Some step when step != 0 ->
7166 let y = state.y + step in
7167 let y =
7168 if y < 0
7169 then state.maxy
7170 else if y >= state.maxy then 0 else y
7172 gotoy y;
7173 if state.mode = View
7174 then state.text <- "";
7175 deadline +. 0.01
7176 | _ -> infinity
7177 else deadline +. 0.01
7179 loop newdeadline
7181 | l ->
7182 let rec checkfds = function
7183 | [] -> ()
7184 | fd :: rest when fd = state.sr ->
7185 let cmd = readcmd state.sr in
7186 act cmd;
7187 checkfds rest
7189 | fd :: rest when fd = state.wsfd ->
7190 Wsi.readresp fd;
7191 checkfds rest
7193 | fd :: rest ->
7194 let s = String.create 80 in
7195 let n = tempfailureretry (Unix.read fd s 0) 80 in
7196 if conf.redirectstderr
7197 then (
7198 Buffer.add_substring state.errmsgs s 0 n;
7199 state.newerrmsgs <- true;
7200 state.redisplay <- true;
7202 else (
7203 prerr_string (String.sub s 0 n);
7204 flush stderr;
7206 checkfds rest
7208 checkfds l;
7209 let newdeadline =
7210 let deadline1 =
7211 if deadline = infinity
7212 then now () +. 0.01
7213 else deadline
7215 match state.autoscroll with
7216 | Some step when step != 0 -> deadline1
7217 | _ -> if state.ghyll == noghyll then infinity else deadline1
7219 loop newdeadline
7220 end;
7223 loop infinity;
7224 with Quit ->
7225 Config.save ();