Proper handling of WM initiated full screen switch
[llpp.git] / main.ml
blobdae723ada4f90fbefc8e6ab3c72c9404efcc396b
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 cwinw : int
319 ; mutable cwinh : 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 winstate : Wsi.winstate list
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
485 ; mutable winw : int
486 ; mutable winh : int
488 and hists =
489 { pat : string circbuf
490 ; pag : string circbuf
491 ; nav : anchor circbuf
492 ; sel : string circbuf
496 let defconf =
497 { scrollbw = 7
498 ; scrollh = 12
499 ; icase = true
500 ; preload = true
501 ; pagebias = 0
502 ; verbose = false
503 ; debug = false
504 ; scrollstep = 24
505 ; hscrollstep = 24
506 ; maxhfit = true
507 ; crophack = false
508 ; autoscrollstep = 2
509 ; maxwait = None
510 ; hlinks = false
511 ; underinfo = false
512 ; interpagespace = 2
513 ; zoom = 1.0
514 ; presentation = false
515 ; angle = 0
516 ; cwinw = 900
517 ; cwinh = 900
518 ; savebmarks = true
519 ; proportional = true
520 ; trimmargins = false
521 ; trimfuzz = (0,0,0,0)
522 ; memlimit = 32 lsl 20
523 ; texcount = 256
524 ; sliceheight = 24
525 ; thumbw = 76
526 ; jumpback = true
527 ; bgcolor = (0.5, 0.5, 0.5)
528 ; bedefault = false
529 ; scrollbarinpm = true
530 ; tilew = 2048
531 ; tileh = 2048
532 ; mustoresize = 256 lsl 20
533 ; checkers = true
534 ; aalevel = 8
535 ; urilauncher =
536 (match platform with
537 | Plinux | Pfreebsd | Pdragonflybsd
538 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
539 | Posx -> "open \"%s\""
540 | Pcygwin -> "cygstart \"%s\""
541 | Punknown -> "echo %s")
542 ; pathlauncher = "lp \"%s\""
543 ; selcmd =
544 (match platform with
545 | Plinux | Pfreebsd | Pdragonflybsd
546 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
547 | Posx -> "pbcopy"
548 | Pcygwin -> "wsel"
549 | Punknown -> "cat")
550 ; colorspace = Rgb
551 ; invert = false
552 ; colorscale = 1.0
553 ; redirectstderr = false
554 ; ghyllscroll = None
555 ; columns = Csingle [||]
556 ; beyecolumns = None
557 ; updatecurs = false
558 ; hfsize = 12
559 ; pgscale = 1.0
560 ; usepbo = false
561 ; wheelbypage = false
562 ; stcmd = "echo SyncTex"
563 ; keyhashes =
564 let mk n = (n, Hashtbl.create 1) in
565 [ mk "global"
566 ; mk "info"
567 ; mk "help"
568 ; mk "outline"
569 ; mk "listview"
570 ; mk "birdseye"
571 ; mk "textentry"
572 ; mk "links"
573 ; mk "view"
578 let wtmode = ref false;;
580 let findkeyhash c name =
581 try List.assoc name c.keyhashes
582 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
585 let conf = { defconf with angle = defconf.angle };;
587 let pgscale h = truncate (float h *. conf.pgscale);;
589 type fontstate =
590 { mutable fontsize : int
591 ; mutable wwidth : float
592 ; mutable maxrows : int
596 let fstate =
597 { fontsize = 14
598 ; wwidth = nan
599 ; maxrows = -1
603 let geturl s =
604 let colonpos = try String.index s ':' with Not_found -> -1 in
605 let len = String.length s in
606 if colonpos >= 0 && colonpos + 3 < len
607 then (
608 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
609 then
610 let schemestartpos =
611 try String.rindex_from s colonpos ' '
612 with Not_found -> -1
614 let scheme =
615 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
617 match scheme with
618 | "http" | "ftp" | "mailto" ->
619 let epos =
620 try String.index_from s colonpos ' '
621 with Not_found -> len
623 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
624 | _ -> ""
625 else ""
627 else ""
630 let gotouri uri =
631 if String.length conf.urilauncher = 0
632 then print_endline uri
633 else (
634 let url = geturl uri in
635 if String.length url = 0
636 then print_endline uri
637 else
638 let re = Str.regexp "%s" in
639 let command = Str.global_replace re url conf.urilauncher in
640 try popen command []
641 with exn ->
642 Printf.eprintf
643 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
644 flush stderr;
648 let version () =
649 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
650 (platform_to_string platform) Sys.word_size Sys.ocaml_version
653 let makehelp () =
654 let strings = version () :: "" :: Help.keys in
655 Array.of_list (
656 List.map (fun s ->
657 let url = geturl s in
658 if String.length url > 0
659 then (s, 0, Action (fun u -> gotouri url; u))
660 else (s, 0, Noaction)
661 ) strings);
664 let noghyll _ = ();;
665 let firstgeomcmds = "", [];;
667 let state =
668 { sr = Unix.stdin
669 ; sw = Unix.stdin
670 ; wsfd = Unix.stdin
671 ; errfd = None
672 ; stderr = Unix.stderr
673 ; errmsgs = Buffer.create 0
674 ; newerrmsgs = false
675 ; x = 0
676 ; y = 0
677 ; w = 0
678 ; scrollw = 0
679 ; hscrollh = 0
680 ; anchor = emptyanchor
681 ; ranchors = []
682 ; layout = []
683 ; maxy = max_int
684 ; tilelru = Queue.create ()
685 ; pagemap = Hashtbl.create 10
686 ; tilemap = Hashtbl.create 10
687 ; pdims = []
688 ; pagecount = 0
689 ; currently = Idle
690 ; mstate = Mnone
691 ; rects = []
692 ; rects1 = []
693 ; text = ""
694 ; mode = View
695 ; winstate = []
696 ; searchpattern = ""
697 ; outlines = [||]
698 ; bookmarks = []
699 ; path = ""
700 ; password = ""
701 ; nameddest = ""
702 ; geomcmds = firstgeomcmds
703 ; hists =
704 { nav = cbnew 10 emptyanchor
705 ; pat = cbnew 10 ""
706 ; pag = cbnew 10 ""
707 ; sel = cbnew 10 ""
709 ; memused = 0
710 ; gen = 0
711 ; throttle = None
712 ; autoscroll = None
713 ; ghyll = noghyll
714 ; help = makehelp ()
715 ; docinfo = []
716 ; texid = None
717 ; prevzoom = 1.0
718 ; progress = -1.0
719 ; uioh = nouioh
720 ; redisplay = true
721 ; mpos = (-1, -1)
722 ; keystate = KSnone
723 ; glinks = false
724 ; prevcolumns = None
725 ; wthack = false
726 ; winw = -1
727 ; winh = -1
731 let setfontsize n =
732 fstate.fontsize <- n;
733 fstate.wwidth <- measurestr fstate.fontsize "w";
734 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
737 let vlog fmt =
738 if conf.verbose
739 then
740 Printf.kprintf prerr_endline fmt
741 else
742 Printf.kprintf ignore fmt
745 let launchpath () =
746 if String.length conf.pathlauncher = 0
747 then print_endline state.path
748 else (
749 let re = Str.regexp "%s" in
750 let command = Str.global_replace re state.path conf.pathlauncher in
751 try popen command []
752 with exn ->
753 Printf.eprintf
754 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
755 flush stderr;
759 module Ne = struct
760 type 'a t = | Res of 'a | Exn of exn;;
762 let pipe () =
763 try Res (Unix.pipe ())
764 with exn -> Exn exn
767 let clo fd f =
768 try tempfailureretry Unix.close fd
769 with exn -> f (Printexc.to_string exn)
772 let dup fd =
773 try Res (tempfailureretry Unix.dup fd)
774 with exn -> Exn exn
777 let dup2 fd1 fd2 =
778 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
779 with exn -> Exn exn
781 end;;
783 let redirectstderr () =
784 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
785 if conf.redirectstderr
786 then
787 match Ne.pipe () with
788 | Ne.Exn exn ->
789 dolog "failed to create stderr redirection pipes: %s"
790 (Printexc.to_string exn)
792 | Ne.Res (r, w) ->
793 begin match Ne.dup Unix.stderr with
794 | Ne.Exn exn ->
795 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
796 Ne.clo r (clofail "pipe/r");
797 Ne.clo w (clofail "pipe/w");
799 | Ne.Res dupstderr ->
800 begin match Ne.dup2 w Unix.stderr with
801 | Ne.Exn exn ->
802 dolog "failed to dup2 to stderr: %s"
803 (Printexc.to_string exn);
804 Ne.clo dupstderr (clofail "stderr duplicate");
805 Ne.clo r (clofail "redir pipe/r");
806 Ne.clo w (clofail "redir pipe/w");
808 | Ne.Res () ->
809 state.stderr <- dupstderr;
810 state.errfd <- Some r;
811 end;
813 else (
814 state.newerrmsgs <- false;
815 begin match state.errfd with
816 | Some fd ->
817 begin match Ne.dup2 state.stderr Unix.stderr with
818 | Ne.Exn exn ->
819 dolog "failed to dup2 original stderr: %s"
820 (Printexc.to_string exn)
821 | Ne.Res () ->
822 Ne.clo fd (clofail "dup of stderr");
823 state.errfd <- None;
824 end;
825 | None -> ()
826 end;
827 prerr_string (Buffer.contents state.errmsgs);
828 flush stderr;
829 Buffer.clear state.errmsgs;
833 module G =
834 struct
835 let postRedisplay who =
836 if conf.verbose
837 then prerr_endline ("redisplay for " ^ who);
838 state.redisplay <- true;
840 end;;
842 let getopaque pageno =
843 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
844 with Not_found -> None
847 let putopaque pageno opaque =
848 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
851 let pagetranslatepoint l x y =
852 let dy = y - l.pagedispy in
853 let y = dy + l.pagey in
854 let dx = x - l.pagedispx in
855 let x = dx + l.pagex in
856 (x, y);
859 let onppundermouse g x y d =
860 let rec f = function
861 | l :: rest ->
862 begin match getopaque l.pageno with
863 | Some opaque ->
864 let x0 = l.pagedispx in
865 let x1 = x0 + l.pagevw in
866 let y0 = l.pagedispy in
867 let y1 = y0 + l.pagevh in
868 if y >= y0 && y <= y1 && x >= x0 && x <= x1
869 then
870 let px, py = pagetranslatepoint l x y in
871 match g opaque l px py with
872 | Some res -> res
873 | None -> f rest
874 else f rest
875 | _ ->
876 f rest
878 | [] -> d
880 f state.layout
883 let getunder x y =
884 let g opaque _ px py =
885 match whatsunder opaque px py with
886 | Unone -> None
887 | under -> Some under
889 onppundermouse g x y Unone
892 let unproject x y =
893 let g opaque l x y =
894 match unproject opaque x y with
895 | Some (x, y) -> Some (Some (l.pageno, x, y))
896 | None -> None
898 onppundermouse g x y None;
901 let showtext c s =
902 state.text <- Printf.sprintf "%c%s" c s;
903 G.postRedisplay "showtext";
906 let undertext = function
907 | Unone -> "none"
908 | Ulinkuri s -> s
909 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
910 | Utext s -> "font: " ^ s
911 | Uunexpected s -> "unexpected: " ^ s
912 | Ulaunch s -> "launch: " ^ s
913 | Unamed s -> "named: " ^ s
914 | Uremote (filename, pageno) ->
915 Printf.sprintf "%s: page %d" filename (pageno+1)
918 let updateunder x y =
919 match getunder x y with
920 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
921 | Ulinkuri uri ->
922 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
923 Wsi.setcursor Wsi.CURSOR_INFO
924 | Ulinkgoto (pageno, _) ->
925 if conf.underinfo
926 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
927 Wsi.setcursor Wsi.CURSOR_INFO
928 | Utext s ->
929 if conf.underinfo then showtext 'f' ("ont: " ^ s);
930 Wsi.setcursor Wsi.CURSOR_TEXT
931 | Uunexpected s ->
932 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
933 Wsi.setcursor Wsi.CURSOR_INHERIT
934 | Ulaunch s ->
935 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
936 Wsi.setcursor Wsi.CURSOR_INHERIT
937 | Unamed s ->
938 if conf.underinfo then showtext 'n' ("amed: " ^ s);
939 Wsi.setcursor Wsi.CURSOR_INHERIT
940 | Uremote (filename, pageno) ->
941 if conf.underinfo then showtext 'r'
942 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
943 Wsi.setcursor Wsi.CURSOR_INFO
946 let showlinktype under =
947 if conf.underinfo
948 then
949 match under with
950 | Unone -> ()
951 | under ->
952 let s = undertext under in
953 showtext ' ' s
956 let addchar s c =
957 let b = Buffer.create (String.length s + 1) in
958 Buffer.add_string b s;
959 Buffer.add_char b c;
960 Buffer.contents b;
963 let colorspace_of_string s =
964 match String.lowercase s with
965 | "rgb" -> Rgb
966 | "bgr" -> Bgr
967 | "gray" -> Gray
968 | _ -> failwith "invalid colorspace"
971 let int_of_colorspace = function
972 | Rgb -> 0
973 | Bgr -> 1
974 | Gray -> 2
977 let colorspace_of_int = function
978 | 0 -> Rgb
979 | 1 -> Bgr
980 | 2 -> Gray
981 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
984 let colorspace_to_string = function
985 | Rgb -> "rgb"
986 | Bgr -> "bgr"
987 | Gray -> "gray"
990 let intentry_with_suffix text key =
991 let c =
992 if key >= 32 && key < 127
993 then Char.chr key
994 else '\000'
996 match Char.lowercase c with
997 | '0' .. '9' ->
998 let text = addchar text c in
999 TEcont text
1001 | 'k' | 'm' | 'g' ->
1002 let text = addchar text c in
1003 TEcont text
1005 | _ ->
1006 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1007 TEcont text
1010 let multicolumns_to_string (n, a, b) =
1011 if a = 0 && b = 0
1012 then Printf.sprintf "%d" n
1013 else Printf.sprintf "%d,%d,%d" n a b;
1016 let multicolumns_of_string s =
1018 (int_of_string s, 0, 0)
1019 with _ ->
1020 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1021 if a > 1 || b > 1
1022 then failwith "subtly broken"; (n, a, b)
1026 let readcmd fd =
1027 let s = "xxxx" in
1028 let n = tempfailureretry (Unix.read fd s 0) 4 in
1029 if n != 4 then failwith "incomplete read(len)";
1030 let len = 0
1031 lor (Char.code s.[0] lsl 24)
1032 lor (Char.code s.[1] lsl 16)
1033 lor (Char.code s.[2] lsl 8)
1034 lor (Char.code s.[3] lsl 0)
1036 let s = String.create len in
1037 let n = tempfailureretry (Unix.read fd s 0) len in
1038 if n != len then failwith "incomplete read(data)";
1042 let btod b = if b then 1 else 0;;
1044 let wcmd fmt =
1045 let b = Buffer.create 16 in
1046 Buffer.add_string b "llll";
1047 Printf.kbprintf
1048 (fun b ->
1049 let s = Buffer.contents b in
1050 let n = String.length s in
1051 let len = n - 4 in
1052 (* dolog "wcmd %S" (String.sub s 4 len); *)
1053 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1054 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1055 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1056 s.[3] <- Char.chr (len land 0xff);
1057 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1058 if n' != n then failwith "write failed";
1059 ) b fmt;
1062 let calcips h =
1063 let d = state.winh - h in
1064 max conf.interpagespace ((d + 1) / 2)
1067 let rowyh (c, coverA, coverB) b n =
1068 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1069 then
1070 let _, _, vy, (_, _, h, _) = b.(n) in
1071 (vy, h)
1072 else
1073 let n' = n - coverA in
1074 let d = n' mod c in
1075 let s = n - d in
1076 let e = min state.pagecount (s + c) in
1077 let rec find m miny maxh = if m = e then miny, maxh else
1078 let _, _, y, (_, _, h, _) = b.(m) in
1079 let miny = min miny y in
1080 let maxh = max maxh h in
1081 find (m+1) miny maxh
1082 in find s max_int 0
1085 let calcheight () =
1086 match conf.columns with
1087 | Cmulti ((_, _, _) as cl, b) ->
1088 if Array.length b > 0
1089 then
1090 let y, h = rowyh cl b (Array.length b - 1) in
1091 y + h + (if conf.presentation then calcips h else 0)
1092 else 0
1093 | Csingle b ->
1094 if Array.length b > 0
1095 then
1096 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1097 y + h + (if conf.presentation then calcips h else 0)
1098 else 0
1099 | Csplit (_, b) ->
1100 if Array.length b > 0
1101 then
1102 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1103 y + h
1104 else 0
1107 let getpageyh pageno =
1108 let pageno = bound pageno 0 (state.pagecount-1) in
1109 match conf.columns with
1110 | Csingle b ->
1111 if Array.length b = 0
1112 then 0, 0
1113 else
1114 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1115 let y =
1116 if conf.presentation
1117 then y - calcips h
1118 else y
1120 y, h
1121 | Cmulti (cl, b) ->
1122 if Array.length b = 0
1123 then 0, 0
1124 else
1125 let y, h = rowyh cl b pageno in
1126 let y =
1127 if conf.presentation
1128 then y - calcips h
1129 else y
1131 y, h
1132 | Csplit (c, b) ->
1133 if Array.length b = 0
1134 then 0, 0
1135 else
1136 let n = pageno*c in
1137 let (_, _, y, (_, _, h, _)) = b.(n) in
1138 y, h
1141 let getpagedim pageno =
1142 let rec f ppdim l =
1143 match l with
1144 | (n, _, _, _) as pdim :: rest ->
1145 if n >= pageno
1146 then (if n = pageno then pdim else ppdim)
1147 else f pdim rest
1149 | [] -> ppdim
1151 f (-1, -1, -1, -1) state.pdims
1154 let getpagey pageno = fst (getpageyh pageno);;
1156 let nogeomcmds cmds =
1157 match cmds with
1158 | s, [] -> String.length s = 0
1159 | _ -> false
1162 let page_of_y y =
1163 let ((c, coverA, coverB) as cl), b =
1164 match conf.columns with
1165 | Csingle b -> (1, 0, 0), b
1166 | Cmulti (c, b) -> c, b
1167 | Csplit (_, b) -> (1, 0, 0), b
1169 if Array.length b = 0
1170 then -1
1171 else
1172 let rec bsearch nmin nmax =
1173 if nmin > nmax
1174 then bound nmin 0 (state.pagecount-1)
1175 else
1176 let n = (nmax + nmin) / 2 in
1177 let vy, h = rowyh cl b n in
1178 let y0, y1 =
1179 if conf.presentation
1180 then
1181 let ips = calcips h in
1182 let y0 = vy - ips in
1183 let y1 = vy + h + ips in
1184 y0, y1
1185 else (
1186 if n = 0
1187 then 0, vy + h + conf.interpagespace
1188 else
1189 let y0 = vy - conf.interpagespace in
1190 y0, y0 + h + conf.interpagespace
1193 if y >= y0 && y < y1
1194 then (
1195 if c = 1
1196 then n
1197 else (
1198 if n > coverA
1199 then
1200 if n < state.pagecount - coverB
1201 then ((n-coverA)/c)*c + coverA
1202 else n
1203 else n
1206 else (
1207 if y > y0
1208 then bsearch (n+1) nmax
1209 else bsearch nmin (n-1)
1212 let r = bsearch 0 (state.pagecount-1) in
1216 let layoutN ((columns, coverA, coverB), b) y sh =
1217 let sh = sh - state.hscrollh in
1218 let rec fold accu n =
1219 if n = Array.length b
1220 then accu
1221 else
1222 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1223 if (vy - y) > sh &&
1224 (n = coverA - 1
1225 || n = state.pagecount - coverB
1226 || (n - coverA) mod columns = columns - 1)
1227 then accu
1228 else
1229 let accu =
1230 if vy + h > y
1231 then
1232 let pagey = max 0 (y - vy) in
1233 let pagedispy = if pagey > 0 then 0 else vy - y in
1234 let pagedispx, pagex =
1235 let pdx =
1236 if n = coverA - 1 || n = state.pagecount - coverB
1237 then state.x + (state.winw - state.scrollw - w) / 2
1238 else dx + xoff + state.x
1240 if pdx < 0
1241 then 0, -pdx
1242 else pdx, 0
1244 let pagevw =
1245 let vw = state.winw - state.scrollw - pagedispx in
1246 let pw = w - pagex in
1247 min vw pw
1249 let pagevh = min (h - pagey) (sh - pagedispy) in
1250 if pagevw > 0 && pagevh > 0
1251 then
1252 let e =
1253 { pageno = n
1254 ; pagedimno = pdimno
1255 ; pagew = w
1256 ; pageh = h
1257 ; pagex = pagex
1258 ; pagey = pagey
1259 ; pagevw = pagevw
1260 ; pagevh = pagevh
1261 ; pagedispx = pagedispx
1262 ; pagedispy = pagedispy
1263 ; pagecol = 0
1266 e :: accu
1267 else
1268 accu
1269 else
1270 accu
1272 fold accu (n+1)
1274 List.rev (fold [] (page_of_y y));
1277 let layoutS (columns, b) y sh =
1278 let sh = sh - state.hscrollh in
1279 let rec fold accu n =
1280 if n = Array.length b
1281 then accu
1282 else
1283 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1284 if (vy - y) > sh
1285 then accu
1286 else
1287 let accu =
1288 if vy + pageh > y
1289 then
1290 let x = xoff + state.x in
1291 let pagey = max 0 (y - vy) in
1292 let pagedispy = if pagey > 0 then 0 else vy - y in
1293 let pagedispx, pagex =
1294 if px = 0
1295 then (
1296 if x < 0
1297 then 0, -x
1298 else x, 0
1300 else (
1301 let px = px - x in
1302 if px < 0
1303 then -px, 0
1304 else 0, px
1307 let pagecolw = pagew/columns in
1308 let pagedispx =
1309 if pagecolw < state.winw
1310 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1311 else pagedispx
1313 let pagevw =
1314 let vw = state.winw - pagedispx - state.scrollw in
1315 let pw = pagew - pagex in
1316 min vw pw
1318 let pagevw = min pagevw pagecolw in
1319 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1320 if pagevw > 0 && pagevh > 0
1321 then
1322 let e =
1323 { pageno = n/columns
1324 ; pagedimno = pdimno
1325 ; pagew = pagew
1326 ; pageh = pageh
1327 ; pagex = pagex
1328 ; pagey = pagey
1329 ; pagevw = pagevw
1330 ; pagevh = pagevh
1331 ; pagedispx = pagedispx
1332 ; pagedispy = pagedispy
1333 ; pagecol = n mod columns
1336 e :: accu
1337 else
1338 accu
1339 else
1340 accu
1342 fold accu (n+1)
1344 List.rev (fold [] 0)
1347 let layout y sh =
1348 if nogeomcmds state.geomcmds
1349 then
1350 match conf.columns with
1351 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1352 | Cmulti c -> layoutN c y sh
1353 | Csplit s -> layoutS s y sh
1354 else []
1357 let clamp incr =
1358 let y = state.y + incr in
1359 let y = max 0 y in
1360 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1364 let itertiles l f =
1365 let tilex = l.pagex mod conf.tilew in
1366 let tiley = l.pagey mod conf.tileh in
1368 let col = l.pagex / conf.tilew in
1369 let row = l.pagey / conf.tileh in
1371 let rec rowloop row y0 dispy h =
1372 if h = 0
1373 then ()
1374 else (
1375 let dh = conf.tileh - y0 in
1376 let dh = min h dh in
1377 let rec colloop col x0 dispx w =
1378 if w = 0
1379 then ()
1380 else (
1381 let dw = conf.tilew - x0 in
1382 let dw = min w dw in
1384 f col row dispx dispy x0 y0 dw dh;
1385 colloop (col+1) 0 (dispx+dw) (w-dw)
1388 colloop col tilex l.pagedispx l.pagevw;
1389 rowloop (row+1) 0 (dispy+dh) (h-dh)
1392 if l.pagevw > 0 && l.pagevh > 0
1393 then rowloop row tiley l.pagedispy l.pagevh;
1396 let gettileopaque l col row =
1397 let key =
1398 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1400 try Some (Hashtbl.find state.tilemap key)
1401 with Not_found -> None
1404 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1405 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1406 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1409 let drawtiles l color =
1410 GlDraw.color color;
1411 let f col row x y tilex tiley w h =
1412 match gettileopaque l col row with
1413 | Some (opaque, _, t) ->
1414 let params = x, y, w, h, tilex, tiley in
1415 if conf.invert
1416 then (
1417 Gl.enable `blend;
1418 GlFunc.blend_func `zero `one_minus_src_color;
1420 drawtile params opaque;
1421 if conf.invert
1422 then Gl.disable `blend;
1423 if conf.debug
1424 then (
1425 let s = Printf.sprintf
1426 "%d[%d,%d] %f sec"
1427 l.pageno col row t
1429 let w = measurestr fstate.fontsize s in
1430 GlMisc.push_attrib [`current];
1431 GlDraw.color (0.0, 0.0, 0.0);
1432 GlDraw.rect
1433 (float (x-2), float (y-2))
1434 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1435 GlDraw.color (1.0, 1.0, 1.0);
1436 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1437 GlMisc.pop_attrib ();
1440 | _ ->
1441 let w =
1442 let lw = state.winw - state.scrollw - x in
1443 min lw w
1444 and h =
1445 let lh = state.winh - y in
1446 min lh h
1448 begin match state.texid with
1449 | Some id ->
1450 Gl.enable `texture_2d;
1451 GlTex.bind_texture `texture_2d id;
1452 let x0 = float x
1453 and y0 = float y
1454 and x1 = float (x+w)
1455 and y1 = float (y+h) in
1457 let tw = float w /. 16.0
1458 and th = float h /. 16.0 in
1459 let tx0 = float tilex /. 16.0
1460 and ty0 = float tiley /. 16.0 in
1461 let tx1 = tx0 +. tw
1462 and ty1 = ty0 +. th in
1463 GlDraw.begins `quads;
1464 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1465 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1466 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1467 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1468 GlDraw.ends ();
1470 Gl.disable `texture_2d;
1471 | None ->
1472 GlDraw.color (1.0, 1.0, 1.0);
1473 GlDraw.rect
1474 (float x, float y)
1475 (float (x+w), float (y+h));
1476 end;
1477 if w > 128 && h > fstate.fontsize + 10
1478 then (
1479 GlDraw.color (0.0, 0.0, 0.0);
1480 let c, r =
1481 if conf.verbose
1482 then (col*conf.tilew, row*conf.tileh)
1483 else col, row
1485 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1487 GlDraw.color color;
1489 itertiles l f
1492 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1494 let tilevisible1 l x y =
1495 let ax0 = l.pagex
1496 and ax1 = l.pagex + l.pagevw
1497 and ay0 = l.pagey
1498 and ay1 = l.pagey + l.pagevh in
1500 let bx0 = x
1501 and by0 = y in
1502 let bx1 = min (bx0 + conf.tilew) l.pagew
1503 and by1 = min (by0 + conf.tileh) l.pageh in
1505 let rx0 = max ax0 bx0
1506 and ry0 = max ay0 by0
1507 and rx1 = min ax1 bx1
1508 and ry1 = min ay1 by1 in
1510 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1511 nonemptyintersection
1514 let tilevisible layout n x y =
1515 let rec findpageinlayout m = function
1516 | l :: rest when l.pageno = n ->
1517 tilevisible1 l x y || (
1518 match conf.columns with
1519 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1520 | _ -> false
1522 | _ :: rest -> findpageinlayout 0 rest
1523 | [] -> false
1525 findpageinlayout 0 layout;
1528 let tileready l x y =
1529 tilevisible1 l x y &&
1530 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1533 let tilepage n p layout =
1534 let rec loop = function
1535 | l :: rest ->
1536 if l.pageno = n
1537 then
1538 let f col row _ _ _ _ _ _ =
1539 if state.currently = Idle
1540 then
1541 match gettileopaque l col row with
1542 | Some _ -> ()
1543 | None ->
1544 let x = col*conf.tilew
1545 and y = row*conf.tileh in
1546 let w =
1547 let w = l.pagew - x in
1548 min w conf.tilew
1550 let h =
1551 let h = l.pageh - y in
1552 min h conf.tileh
1554 let pbo =
1555 if conf.usepbo
1556 then getpbo w h conf.colorspace
1557 else "0"
1559 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1560 state.currently <-
1561 Tiling (
1562 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1563 conf.tilew, conf.tileh
1566 itertiles l f;
1567 else
1568 loop rest
1570 | [] -> ()
1572 if nogeomcmds state.geomcmds
1573 then loop layout;
1576 let preloadlayout y =
1577 let y = if y < state.winh then 0 else y - state.winh in
1578 let h = state.winh*3 in
1579 layout y h;
1582 let load pages =
1583 let rec loop pages =
1584 if state.currently != Idle
1585 then ()
1586 else
1587 match pages with
1588 | l :: rest ->
1589 begin match getopaque l.pageno with
1590 | None ->
1591 wcmd "page %d %d" l.pageno l.pagedimno;
1592 state.currently <- Loading (l, state.gen);
1593 | Some opaque ->
1594 tilepage l.pageno opaque pages;
1595 loop rest
1596 end;
1597 | _ -> ()
1599 if nogeomcmds state.geomcmds
1600 then loop pages
1603 let preload pages =
1604 load pages;
1605 if conf.preload && state.currently = Idle
1606 then load (preloadlayout state.y);
1609 let layoutready layout =
1610 let rec fold all ls =
1611 all && match ls with
1612 | l :: rest ->
1613 let seen = ref false in
1614 let allvisible = ref true in
1615 let foo col row _ _ _ _ _ _ =
1616 seen := true;
1617 allvisible := !allvisible &&
1618 begin match gettileopaque l col row with
1619 | Some _ -> true
1620 | None -> false
1623 itertiles l foo;
1624 fold (!seen && !allvisible) rest
1625 | [] -> true
1627 let alltilesvisible = fold true layout in
1628 alltilesvisible;
1631 let gotoy y =
1632 state.wthack <- false;
1633 let y = bound y 0 state.maxy in
1634 let y, layout, proceed =
1635 match conf.maxwait with
1636 | Some time when state.ghyll == noghyll ->
1637 begin match state.throttle with
1638 | None ->
1639 let layout = layout y state.winh in
1640 let ready = layoutready layout in
1641 if not ready
1642 then (
1643 load layout;
1644 state.throttle <- Some (layout, y, now ());
1646 else G.postRedisplay "gotoy showall (None)";
1647 y, layout, ready
1648 | Some (_, _, started) ->
1649 let dt = now () -. started in
1650 if dt > time
1651 then (
1652 state.throttle <- None;
1653 let layout = layout y state.winh in
1654 load layout;
1655 G.postRedisplay "maxwait";
1656 y, layout, true
1658 else -1, [], false
1661 | _ ->
1662 let layout = layout y state.winh in
1663 G.postRedisplay "gotoy ready";
1664 y, layout, true
1666 if proceed
1667 then (
1668 state.y <- y;
1669 state.layout <- layout;
1670 begin match state.mode with
1671 | LinkNav (Ltexact (pageno, linkno)) ->
1672 let rec loop = function
1673 | [] ->
1674 state.mode <- LinkNav (Ltgendir 0)
1675 | l :: _ when l.pageno = pageno ->
1676 begin match getopaque pageno with
1677 | None ->
1678 state.mode <- LinkNav (Ltgendir 0)
1679 | Some opaque ->
1680 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1681 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1682 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1683 then state.mode <- LinkNav (Ltgendir 0)
1685 | _ :: rest -> loop rest
1687 loop layout
1688 | _ -> ()
1689 end;
1690 begin match state.mode with
1691 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1692 if not (pagevisible layout pageno)
1693 then (
1694 match state.layout with
1695 | [] -> ()
1696 | l :: _ ->
1697 state.mode <- Birdseye (
1698 conf, leftx, l.pageno, hooverpageno, anchor
1701 | LinkNav (Ltgendir dir as lt) ->
1702 let linknav =
1703 let rec loop = function
1704 | [] -> lt
1705 | l :: rest ->
1706 match getopaque l.pageno with
1707 | None -> loop rest
1708 | Some opaque ->
1709 let link =
1710 let ld =
1711 if dir = 0
1712 then LDfirstvisible (l.pagex, l.pagey, dir)
1713 else (
1714 if dir > 0 then LDfirst else LDlast
1717 findlink opaque ld
1719 match link with
1720 | Lnotfound -> loop rest
1721 | Lfound n ->
1722 showlinktype (getlink opaque n);
1723 Ltexact (l.pageno, n)
1725 loop state.layout
1727 state.mode <- LinkNav linknav
1728 | _ -> ()
1729 end;
1730 preload layout;
1732 state.ghyll <- noghyll;
1733 if conf.updatecurs
1734 then (
1735 let mx, my = state.mpos in
1736 updateunder mx my;
1740 let conttiling pageno opaque =
1741 tilepage pageno opaque
1742 (if conf.preload then preloadlayout state.y else state.layout)
1745 let gotoy_and_clear_text y =
1746 if not conf.verbose then state.text <- "";
1747 gotoy y;
1750 let getanchor1 l =
1751 let top =
1752 let coloff = l.pagecol * l.pageh in
1753 float (l.pagey + coloff) /. float l.pageh
1755 let dtop =
1756 if l.pagedispy = 0
1757 then
1759 else
1760 if conf.presentation
1761 then float l.pagedispy /. float (calcips l.pageh)
1762 else float l.pagedispy /. float conf.interpagespace
1764 (l.pageno, top, dtop)
1767 let getanchor () =
1768 match state.layout with
1769 | l :: _ -> getanchor1 l
1770 | [] ->
1771 let n = page_of_y state.y in
1772 if n = -1
1773 then state.anchor
1774 else
1775 let y, h = getpageyh n in
1776 let dy = y - state.y in
1777 let dtop =
1778 if conf.presentation
1779 then
1780 let ips = calcips h in
1781 float (dy + ips) /. float ips
1782 else
1783 float dy /. float conf.interpagespace
1785 (n, 0.0, dtop)
1788 let getanchory (n, top, dtop) =
1789 let y, h = getpageyh n in
1790 if conf.presentation
1791 then
1792 let ips = calcips h in
1793 y + truncate (top*.float h -. dtop*.float ips) + ips;
1794 else
1795 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1798 let gotoanchor anchor =
1799 gotoy (getanchory anchor);
1802 let addnav () =
1803 cbput state.hists.nav (getanchor ());
1806 let getnav dir =
1807 let anchor = cbgetc state.hists.nav dir in
1808 getanchory anchor;
1811 let gotoghyll y =
1812 let scroll f n a b =
1813 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1814 let snake f a b =
1815 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1816 if f < a
1817 then s (float f /. float a)
1818 else (
1819 if f > b
1820 then 1.0 -. s ((float (f-b) /. float (n-b)))
1821 else 1.0
1824 snake f a b
1825 and summa f n a b =
1826 (* courtesy:
1827 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1828 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1829 let iv1 = iv f in
1830 let ins = float a *. iv1
1831 and outs = float (n-b) *. iv1 in
1832 let ones = b - a in
1833 ins +. outs +. float ones
1835 let rec set (_N, _A, _B) y sy =
1836 let sum = summa 1.0 _N _A _B in
1837 let dy = float (y - sy) in
1838 state.ghyll <- (
1839 let rec gf n y1 o =
1840 if n >= _N
1841 then state.ghyll <- noghyll
1842 else
1843 let go n =
1844 let s = scroll n _N _A _B in
1845 let y1 = y1 +. ((s *. dy) /. sum) in
1846 gotoy_and_clear_text (truncate y1);
1847 state.ghyll <- gf (n+1) y1;
1849 match o with
1850 | None -> go n
1851 | Some y' -> set (_N/2, 1, 1) y' state.y
1853 gf 0 (float state.y)
1856 match conf.ghyllscroll with
1857 | None ->
1858 gotoy_and_clear_text y
1859 | Some nab ->
1860 if state.ghyll == noghyll
1861 then set nab y state.y
1862 else state.ghyll (Some y)
1865 let gotopage n top =
1866 let y, h = getpageyh n in
1867 let y = y + (truncate (top *. float h)) in
1868 gotoghyll y
1871 let gotopage1 n top =
1872 let y = getpagey n in
1873 let y = y + top in
1874 gotoghyll y
1877 let invalidate s f =
1878 state.layout <- [];
1879 state.pdims <- [];
1880 state.rects <- [];
1881 state.rects1 <- [];
1882 match state.geomcmds with
1883 | ps, [] when String.length ps = 0 ->
1884 f ();
1885 state.geomcmds <- s, [];
1887 | ps, [] ->
1888 state.geomcmds <- ps, [s, f];
1890 | ps, (s', _) :: rest when s' = s ->
1891 state.geomcmds <- ps, ((s, f) :: rest);
1893 | ps, cmds ->
1894 state.geomcmds <- ps, ((s, f) :: cmds);
1897 let flushpages () =
1898 Hashtbl.iter (fun _ opaque ->
1899 wcmd "freepage %s" opaque;
1900 ) state.pagemap;
1901 Hashtbl.clear state.pagemap;
1904 let opendoc path password =
1905 state.path <- path;
1906 state.password <- password;
1907 state.gen <- state.gen + 1;
1908 state.docinfo <- [];
1910 flushpages ();
1911 setaalevel conf.aalevel;
1912 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1913 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1914 invalidate "reqlayout"
1915 (fun () ->
1916 wcmd "reqlayout %d %d %s\000"
1917 conf.angle (btod conf.proportional) state.nameddest;
1921 let reload () =
1922 state.anchor <- getanchor ();
1923 state.wthack <- !wtmode;
1924 opendoc state.path state.password;
1927 let scalecolor c =
1928 let c = c *. conf.colorscale in
1929 (c, c, c);
1932 let scalecolor2 (r, g, b) =
1933 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1936 let docolumns = function
1937 | Csingle _ ->
1938 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1939 let rec loop pageno pdimno pdim y ph pdims =
1940 if pageno = state.pagecount
1941 then ()
1942 else
1943 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1944 match pdims with
1945 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1946 pdimno+1, pdim, rest
1947 | _ ->
1948 pdimno, pdim, pdims
1950 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
1951 let y = y +
1952 (if conf.presentation
1953 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1954 else (if pageno = 0 then 0 else conf.interpagespace)
1957 a.(pageno) <- (pdimno, x, y, pdim);
1958 loop (pageno+1) pdimno pdim (y + h) h pdims
1960 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1961 conf.columns <- Csingle a;
1963 | Cmulti ((columns, coverA, coverB), _) ->
1964 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1965 let rec loop pageno pdimno pdim x y rowh pdims =
1966 let rec fixrow m = if m = pageno then () else
1967 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1968 if h < rowh
1969 then (
1970 let y = y + (rowh - h) / 2 in
1971 a.(m) <- (pdimno, x, y, pdim);
1973 fixrow (m+1)
1975 if pageno = state.pagecount
1976 then fixrow (((pageno - 1) / columns) * columns)
1977 else
1978 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1979 match pdims with
1980 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1981 pdimno+1, pdim, rest
1982 | _ ->
1983 pdimno, pdim, pdims
1985 let x, y, rowh' =
1986 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1987 then (
1988 let x = (state.winw - state.scrollw - w) / 2 in
1989 let ips =
1990 if conf.presentation then calcips h else conf.interpagespace in
1991 x, y + ips + rowh, h
1993 else (
1994 if (pageno - coverA) mod columns = 0
1995 then (
1996 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
1997 let y =
1998 if conf.presentation
1999 then
2000 let ips = calcips h in
2001 y + (if pageno = 0 then 0 else calcips rowh + ips)
2002 else
2003 y + (if pageno = 0 then 0 else conf.interpagespace)
2005 x, y + rowh, h
2007 else x, y, max rowh h
2010 let y =
2011 if pageno > 1 && (pageno - coverA) mod columns = 0
2012 then (
2013 let y =
2014 if pageno = columns && conf.presentation
2015 then (
2016 let ips = calcips rowh in
2017 for i = 0 to pred columns
2019 let (pdimno, x, y, pdim) = a.(i) in
2020 a.(i) <- (pdimno, x, y+ips, pdim)
2021 done;
2022 y+ips;
2024 else y
2026 fixrow (pageno - columns);
2029 else y
2031 a.(pageno) <- (pdimno, x, y, pdim);
2032 let x = x + w + xoff*2 + conf.interpagespace in
2033 loop (pageno+1) pdimno pdim x y rowh' pdims
2035 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2036 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2038 | Csplit (c, _) ->
2039 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2040 let rec loop pageno pdimno pdim y pdims =
2041 if pageno = state.pagecount
2042 then ()
2043 else
2044 let pdimno, ((_, w, h, _) as pdim), pdims =
2045 match pdims with
2046 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2047 pdimno+1, pdim, rest
2048 | _ ->
2049 pdimno, pdim, pdims
2051 let cw = w / c in
2052 let rec loop1 n x y =
2053 if n = c then y else (
2054 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2055 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2058 let y = loop1 0 0 y in
2059 loop (pageno+1) pdimno pdim y pdims
2061 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2062 conf.columns <- Csplit (c, a);
2065 let represent () =
2066 docolumns conf.columns;
2067 state.maxy <- calcheight ();
2068 state.hscrollh <-
2069 if state.w <= state.winw - state.scrollw
2070 then 0
2071 else state.scrollw
2073 let wthack = state.wthack in
2074 begin match state.mode with
2075 | Birdseye (_, _, pageno, _, _) ->
2076 let y, h = getpageyh pageno in
2077 let top = (state.winh - h) / 2 in
2078 gotoy (max 0 (y - top))
2079 | _ -> gotoanchor state.anchor
2080 end;
2081 state.wthack <- wthack;
2084 let reshape w h =
2085 state.wthack <- false;
2086 GlDraw.viewport 0 0 w h;
2087 let firsttime = state.geomcmds == firstgeomcmds in
2088 if not firsttime && nogeomcmds state.geomcmds
2089 then state.anchor <- getanchor ();
2091 state.winw <- w;
2092 let w = truncate (float w *. conf.zoom) - state.scrollw in
2093 let w = max w 2 in
2094 state.winh <- h;
2095 setfontsize fstate.fontsize;
2096 GlMat.mode `modelview;
2097 GlMat.load_identity ();
2099 GlMat.mode `projection;
2100 GlMat.load_identity ();
2101 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2102 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2103 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2105 let relx =
2106 if conf.zoom <= 1.0
2107 then 0.0
2108 else float state.x /. float state.w
2110 invalidate "geometry"
2111 (fun () ->
2112 state.w <- w;
2113 if not firsttime
2114 then state.x <- truncate (relx *. float w);
2115 let w =
2116 match conf.columns with
2117 | Csingle _ -> w
2118 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2119 | Csplit (c, _) -> w * c
2121 wcmd "geometry %d %d" w h);
2124 let enttext () =
2125 let len = String.length state.text in
2126 let drawstring s =
2127 let hscrollh =
2128 match state.mode with
2129 | Textentry _
2130 | View ->
2131 let h, _, _ = state.uioh#scrollpw in
2133 | _ -> 0
2135 let rect x w =
2136 GlDraw.rect
2137 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2138 (x+.w, float (state.winh - hscrollh))
2141 let w = float (state.winw - state.scrollw - 1) in
2142 if state.progress >= 0.0 && state.progress < 1.0
2143 then (
2144 GlDraw.color (0.3, 0.3, 0.3);
2145 let w1 = w *. state.progress in
2146 rect 0.0 w1;
2147 GlDraw.color (0.0, 0.0, 0.0);
2148 rect w1 (w-.w1)
2150 else (
2151 GlDraw.color (0.0, 0.0, 0.0);
2152 rect 0.0 w;
2155 GlDraw.color (1.0, 1.0, 1.0);
2156 drawstring fstate.fontsize
2157 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2159 let s =
2160 match state.mode with
2161 | Textentry ((prefix, text, _, _, _, _), _) ->
2162 let s =
2163 if len > 0
2164 then
2165 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2166 else
2167 Printf.sprintf "%s%s_" prefix text
2171 | _ -> state.text
2173 let s =
2174 if state.newerrmsgs
2175 then (
2176 if not (istextentry state.mode)
2177 then
2178 let s1 = "(press 'e' to review error messasges)" in
2179 if String.length s > 0 then s ^ " " ^ s1 else s1
2180 else s
2182 else s
2184 if String.length s > 0
2185 then drawstring s
2188 let gctiles () =
2189 let len = Queue.length state.tilelru in
2190 let layout = lazy (
2191 match state.throttle with
2192 | None ->
2193 if conf.preload
2194 then preloadlayout state.y
2195 else state.layout
2196 | Some (layout, _, _) ->
2197 layout
2198 ) in
2199 let rec loop qpos =
2200 if state.memused <= conf.memlimit
2201 then ()
2202 else (
2203 if qpos < len
2204 then
2205 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2206 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2207 let (_, pw, ph, _) = getpagedim n in
2209 gen = state.gen
2210 && colorspace = conf.colorspace
2211 && angle = conf.angle
2212 && pagew = pw
2213 && pageh = ph
2214 && (
2215 let x = col*conf.tilew
2216 and y = row*conf.tileh in
2217 tilevisible (Lazy.force_val layout) n x y
2219 then Queue.push lruitem state.tilelru
2220 else (
2221 freepbo p;
2222 wcmd "freetile %s" p;
2223 state.memused <- state.memused - s;
2224 state.uioh#infochanged Memused;
2225 Hashtbl.remove state.tilemap k;
2227 loop (qpos+1)
2230 loop 0
2233 let flushtiles () =
2234 Queue.iter (fun (k, p, s) ->
2235 wcmd "freetile %s" p;
2236 state.memused <- state.memused - s;
2237 state.uioh#infochanged Memused;
2238 Hashtbl.remove state.tilemap k;
2239 ) state.tilelru;
2240 Queue.clear state.tilelru;
2241 load state.layout;
2244 let logcurrently = function
2245 | Idle -> dolog "Idle"
2246 | Loading (l, gen) ->
2247 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2248 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2249 dolog
2250 "Tiling %d[%d,%d] page=%s cs=%s angle"
2251 l.pageno col row pageopaque
2252 (colorspace_to_string colorspace)
2254 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2255 angle gen conf.angle state.gen
2256 tilew tileh
2257 conf.tilew conf.tileh
2259 | Outlining _ ->
2260 dolog "outlining"
2263 let splitatspace =
2264 let r = Str.regexp " " in
2265 fun s -> Str.bounded_split r s 2;
2268 let act cmds =
2269 (* dolog "%S" cmds; *)
2270 let cl = splitatspace cmds in
2271 let scan s fmt f =
2272 try Scanf.sscanf s fmt f
2273 with exn ->
2274 dolog "error processing '%S': %s" cmds (Printexc.to_string exn);
2275 exit 1
2277 match cl with
2278 | "clear" :: [] ->
2279 state.uioh#infochanged Pdim;
2280 state.pdims <- [];
2282 | "clearrects" :: [] ->
2283 state.rects <- state.rects1;
2284 G.postRedisplay "clearrects";
2286 | "continue" :: args :: [] ->
2287 let n = scan args "%u" (fun n -> n) in
2288 state.pagecount <- n;
2289 begin match state.currently with
2290 | Outlining l ->
2291 state.currently <- Idle;
2292 state.outlines <- Array.of_list (List.rev l)
2293 | _ -> ()
2294 end;
2296 let cur, cmds = state.geomcmds in
2297 if String.length cur = 0
2298 then failwith "umpossible";
2300 begin match List.rev cmds with
2301 | [] ->
2302 state.geomcmds <- "", [];
2303 represent ();
2304 | (s, f) :: rest ->
2305 f ();
2306 state.geomcmds <- s, List.rev rest;
2307 end;
2308 if conf.maxwait = None
2309 then G.postRedisplay "continue";
2311 | "title" :: args :: [] ->
2312 Wsi.settitle args
2314 | "msg" :: args :: [] ->
2315 showtext ' ' args
2317 | "vmsg" :: args :: [] ->
2318 if conf.verbose
2319 then showtext ' ' args
2321 | "emsg" :: args :: [] ->
2322 Buffer.add_string state.errmsgs args;
2323 state.newerrmsgs <- true;
2324 G.postRedisplay "error message"
2326 | "progress" :: args :: [] ->
2327 let progress, text =
2328 scan args "%f %n"
2329 (fun f pos ->
2330 f, String.sub args pos (String.length args - pos))
2332 state.text <- text;
2333 state.progress <- progress;
2334 G.postRedisplay "progress"
2336 | "firstmatch" :: args :: [] ->
2337 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2338 scan args "%u %d %f %f %f %f %f %f %f %f"
2339 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2340 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2342 let y = (getpagey pageno) + truncate y0 in
2343 addnav ();
2344 gotoy y;
2345 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2347 | "match" :: args :: [] ->
2348 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2349 scan args "%u %d %f %f %f %f %f %f %f %f"
2350 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2351 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2353 state.rects1 <-
2354 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2356 | "page" :: args :: [] ->
2357 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2358 begin match state.currently with
2359 | Loading (l, gen) ->
2360 vlog "page %d took %f sec" l.pageno t;
2361 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2362 begin match state.throttle with
2363 | None ->
2364 let preloadedpages =
2365 if conf.preload
2366 then preloadlayout state.y
2367 else state.layout
2369 let evict () =
2370 let module IntSet =
2371 Set.Make (struct type t = int let compare = (-) end) in
2372 let set =
2373 List.fold_left (fun s l -> IntSet.add l.pageno s)
2374 IntSet.empty preloadedpages
2376 let evictedpages =
2377 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2378 if not (IntSet.mem pageno set)
2379 then (
2380 wcmd "freepage %s" opaque;
2381 key :: accu
2383 else accu
2384 ) state.pagemap []
2386 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2388 evict ();
2389 state.currently <- Idle;
2390 if gen = state.gen
2391 then (
2392 tilepage l.pageno pageopaque state.layout;
2393 load state.layout;
2394 load preloadedpages;
2395 if pagevisible state.layout l.pageno
2396 && layoutready state.layout
2397 then G.postRedisplay "page";
2400 | Some (layout, _, _) ->
2401 state.currently <- Idle;
2402 tilepage l.pageno pageopaque layout;
2403 load state.layout
2404 end;
2406 | _ ->
2407 dolog "Inconsistent loading state";
2408 logcurrently state.currently;
2409 exit 1
2412 | "tile" :: args :: [] ->
2413 let (x, y, opaque, size, t) =
2414 scan args "%u %u %s %u %f"
2415 (fun x y p size t -> (x, y, p, size, t))
2417 begin match state.currently with
2418 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2419 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2421 unmappbo opaque;
2422 if tilew != conf.tilew || tileh != conf.tileh
2423 then (
2424 wcmd "freetile %s" opaque;
2425 state.currently <- Idle;
2426 load state.layout;
2428 else (
2429 puttileopaque l col row gen cs angle opaque size t;
2430 state.memused <- state.memused + size;
2431 state.uioh#infochanged Memused;
2432 gctiles ();
2433 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2434 opaque, size) state.tilelru;
2436 let layout =
2437 match state.throttle with
2438 | None -> state.layout
2439 | Some (layout, _, _) -> layout
2442 state.currently <- Idle;
2443 if gen = state.gen
2444 && conf.colorspace = cs
2445 && conf.angle = angle
2446 && tilevisible layout l.pageno x y
2447 then conttiling l.pageno pageopaque;
2449 begin match state.throttle with
2450 | None ->
2451 if state.wthack
2452 then state.wthack <- not (layoutready state.layout);
2453 preload state.layout;
2454 if gen = state.gen
2455 && conf.colorspace = cs
2456 && conf.angle = angle
2457 && tilevisible state.layout l.pageno x y
2458 then G.postRedisplay "tile nothrottle";
2460 | Some (layout, y, _) ->
2461 let ready = layoutready layout in
2462 if ready
2463 then (
2464 state.wthack <- false;
2465 state.y <- y;
2466 state.layout <- layout;
2467 state.throttle <- None;
2468 G.postRedisplay "throttle";
2470 else load layout;
2471 end;
2474 | _ ->
2475 dolog "Inconsistent tiling state";
2476 logcurrently state.currently;
2477 exit 1
2480 | "pdim" :: args :: [] ->
2481 let pdim =
2482 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2484 state.uioh#infochanged Pdim;
2485 state.pdims <- pdim :: state.pdims
2487 | "o" :: args :: [] ->
2488 let (l, n, t, h, pos) =
2489 scan args "%u %u %d %u %n"
2490 (fun l n t h pos -> l, n, t, h, pos)
2492 let s = String.sub args pos (String.length args - pos) in
2493 let outline = (s, l, (n, float t /. float h, 0.0)) in
2494 begin match state.currently with
2495 | Outlining outlines ->
2496 state.currently <- Outlining (outline :: outlines)
2497 | Idle ->
2498 state.currently <- Outlining [outline]
2499 | currently ->
2500 dolog "invalid outlining state";
2501 logcurrently currently
2504 | "a" :: args :: [] ->
2505 let (n, t, h) =
2506 scan args "%u %u %d" (fun n t h -> n, t, h)
2508 let top, dtop =
2509 if conf.presentation
2510 then (0.0, 1.0)
2511 else float t /. float h, 0.0
2513 state.anchor <- (n, top, dtop)
2515 | "info" :: args :: [] ->
2516 state.docinfo <- (1, args) :: state.docinfo
2518 | "infoend" :: [] ->
2519 state.uioh#infochanged Docinfo;
2520 state.docinfo <- List.rev state.docinfo
2522 | _ ->
2523 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2526 let onhist cb =
2527 let rc = cb.rc in
2528 let action = function
2529 | HCprev -> cbget cb ~-1
2530 | HCnext -> cbget cb 1
2531 | HCfirst -> cbget cb ~-(cb.rc)
2532 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2533 and cancel () = cb.rc <- rc
2534 in (action, cancel)
2537 let search pattern forward =
2538 if String.length pattern > 0
2539 then
2540 let pn, py =
2541 match state.layout with
2542 | [] -> 0, 0
2543 | l :: _ ->
2544 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2546 wcmd "search %d %d %d %d,%s\000"
2547 (btod conf.icase) pn py (btod forward) pattern;
2550 let intentry text key =
2551 let c =
2552 if key >= 32 && key < 127
2553 then Char.chr key
2554 else '\000'
2556 match c with
2557 | '0' .. '9' ->
2558 let text = addchar text c in
2559 TEcont text
2561 | _ ->
2562 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2563 TEcont text
2566 let linknentry text key =
2567 let c =
2568 if key >= 32 && key < 127
2569 then Char.chr key
2570 else '\000'
2572 match c with
2573 | 'a' .. 'z' ->
2574 let text = addchar text c in
2575 TEcont text
2577 | _ ->
2578 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2579 TEcont text
2582 let linkndone f s =
2583 if String.length s > 0
2584 then (
2585 let n =
2586 let l = String.length s in
2587 let rec loop pos n = if pos = l then n else
2588 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2589 loop (pos+1) (n*26 + m)
2590 in loop 0 0
2592 let rec loop n = function
2593 | [] -> ()
2594 | l :: rest ->
2595 match getopaque l.pageno with
2596 | None -> loop n rest
2597 | Some opaque ->
2598 let m = getlinkcount opaque in
2599 if n < m
2600 then (
2601 let under = getlink opaque n in
2602 f under
2604 else loop (n-m) rest
2606 loop n state.layout;
2610 let textentry text key =
2611 if key land 0xff00 = 0xff00
2612 then TEcont text
2613 else TEcont (text ^ Wsi.toutf8 key)
2616 let reqlayout angle proportional =
2617 match state.throttle with
2618 | None ->
2619 if nogeomcmds state.geomcmds
2620 then state.anchor <- getanchor ();
2621 conf.angle <- angle mod 360;
2622 if conf.angle != 0
2623 then (
2624 match state.mode with
2625 | LinkNav _ -> state.mode <- View
2626 | _ -> ()
2628 conf.proportional <- proportional;
2629 invalidate "reqlayout"
2630 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2631 | _ -> ()
2634 let settrim trimmargins trimfuzz =
2635 if nogeomcmds state.geomcmds
2636 then state.anchor <- getanchor ();
2637 conf.trimmargins <- trimmargins;
2638 conf.trimfuzz <- trimfuzz;
2639 let x0, y0, x1, y1 = trimfuzz in
2640 invalidate "settrim"
2641 (fun () ->
2642 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2643 flushpages ();
2646 let setzoom zoom =
2647 match state.throttle with
2648 | None ->
2649 let zoom = max 0.01 zoom in
2650 if zoom <> conf.zoom
2651 then (
2652 state.prevzoom <- conf.zoom;
2653 conf.zoom <- zoom;
2654 reshape state.winw state.winh;
2655 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2658 | Some (layout, y, started) ->
2659 let time =
2660 match conf.maxwait with
2661 | None -> 0.0
2662 | Some t -> t
2664 let dt = now () -. started in
2665 if dt > time
2666 then (
2667 state.y <- y;
2668 load layout;
2672 let setcolumns mode columns coverA coverB =
2673 state.prevcolumns <- Some (conf.columns, conf.zoom);
2674 if columns < 0
2675 then (
2676 if isbirdseye mode
2677 then showtext '!' "split mode doesn't work in bird's eye"
2678 else (
2679 conf.columns <- Csplit (-columns, [||]);
2680 state.x <- 0;
2681 conf.zoom <- 1.0;
2684 else (
2685 if columns < 2
2686 then (
2687 conf.columns <- Csingle [||];
2688 state.x <- 0;
2689 setzoom 1.0;
2691 else (
2692 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2693 conf.zoom <- 1.0;
2696 reshape state.winw state.winh;
2699 let enterbirdseye () =
2700 let zoom = float conf.thumbw /. float state.winw in
2701 let birdseyepageno =
2702 let cy = state.winh / 2 in
2703 let fold = function
2704 | [] -> 0
2705 | l :: rest ->
2706 let rec fold best = function
2707 | [] -> best.pageno
2708 | l :: rest ->
2709 let d = cy - (l.pagedispy + l.pagevh/2)
2710 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2711 if abs d < abs dbest
2712 then fold l rest
2713 else best.pageno
2714 in fold l rest
2716 fold state.layout
2718 state.mode <- Birdseye (
2719 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2721 conf.zoom <- zoom;
2722 conf.presentation <- false;
2723 conf.interpagespace <- 10;
2724 conf.hlinks <- false;
2725 state.x <- 0;
2726 state.mstate <- Mnone;
2727 conf.maxwait <- None;
2728 conf.columns <- (
2729 match conf.beyecolumns with
2730 | Some c ->
2731 conf.zoom <- 1.0;
2732 Cmulti ((c, 0, 0), [||])
2733 | None -> Csingle [||]
2735 Wsi.setcursor Wsi.CURSOR_INHERIT;
2736 if conf.verbose
2737 then
2738 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2739 (100.0*.zoom)
2740 else
2741 state.text <- ""
2743 reshape state.winw state.winh;
2746 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2747 state.mode <- View;
2748 conf.zoom <- c.zoom;
2749 conf.presentation <- c.presentation;
2750 conf.interpagespace <- c.interpagespace;
2751 conf.maxwait <- c.maxwait;
2752 conf.hlinks <- c.hlinks;
2753 conf.beyecolumns <- (
2754 match conf.columns with
2755 | Cmulti ((c, _, _), _) -> Some c
2756 | Csingle _ -> None
2757 | Csplit _ -> failwith "leaving bird's eye split mode"
2759 conf.columns <- (
2760 match c.columns with
2761 | Cmulti (c, _) -> Cmulti (c, [||])
2762 | Csingle _ -> Csingle [||]
2763 | Csplit (c, _) -> Csplit (c, [||])
2765 state.x <- leftx;
2766 if conf.verbose
2767 then
2768 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2769 (100.0*.conf.zoom)
2771 reshape state.winw state.winh;
2772 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2775 let togglebirdseye () =
2776 match state.mode with
2777 | Birdseye vals -> leavebirdseye vals true
2778 | View -> enterbirdseye ()
2779 | _ -> ()
2782 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2783 let pageno = max 0 (pageno - incr) in
2784 let rec loop = function
2785 | [] -> gotopage1 pageno 0
2786 | l :: _ when l.pageno = pageno ->
2787 if l.pagedispy >= 0 && l.pagey = 0
2788 then G.postRedisplay "upbirdseye"
2789 else gotopage1 pageno 0
2790 | _ :: rest -> loop rest
2792 loop state.layout;
2793 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2796 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2797 let pageno = min (state.pagecount - 1) (pageno + incr) in
2798 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2799 let rec loop = function
2800 | [] ->
2801 let y, h = getpageyh pageno in
2802 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2803 gotoy (clamp dy)
2804 | l :: _ when l.pageno = pageno ->
2805 if l.pagevh != l.pageh
2806 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2807 else G.postRedisplay "downbirdseye"
2808 | _ :: rest -> loop rest
2810 loop state.layout
2813 let optentry mode _ key =
2814 let btos b = if b then "on" else "off" in
2815 if key >= 32 && key < 127
2816 then
2817 let c = Char.chr key in
2818 match c with
2819 | 's' ->
2820 let ondone s =
2821 try conf.scrollstep <- int_of_string s with exc ->
2822 state.text <- Printf.sprintf "bad integer `%s': %s"
2823 s (Printexc.to_string exc)
2825 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2827 | 'A' ->
2828 let ondone s =
2830 conf.autoscrollstep <- int_of_string s;
2831 if state.autoscroll <> None
2832 then state.autoscroll <- Some conf.autoscrollstep
2833 with exc ->
2834 state.text <- Printf.sprintf "bad integer `%s': %s"
2835 s (Printexc.to_string exc)
2837 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2839 | 'C' ->
2840 let ondone s =
2842 let n, a, b = multicolumns_of_string s in
2843 setcolumns mode n a b;
2844 with exc ->
2845 state.text <- Printf.sprintf "bad columns `%s': %s"
2846 s (Printexc.to_string exc)
2848 TEswitch ("columns: ", "", None, textentry, ondone, true)
2850 | 'Z' ->
2851 let ondone s =
2853 let zoom = float (int_of_string s) /. 100.0 in
2854 setzoom zoom
2855 with exc ->
2856 state.text <- Printf.sprintf "bad integer `%s': %s"
2857 s (Printexc.to_string exc)
2859 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2861 | 't' ->
2862 let ondone s =
2864 conf.thumbw <- bound (int_of_string s) 2 4096;
2865 state.text <-
2866 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2867 begin match mode with
2868 | Birdseye beye ->
2869 leavebirdseye beye false;
2870 enterbirdseye ();
2871 | _ -> ();
2873 with exc ->
2874 state.text <- Printf.sprintf "bad integer `%s': %s"
2875 s (Printexc.to_string exc)
2877 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2879 | 'R' ->
2880 let ondone s =
2881 match try
2882 Some (int_of_string s)
2883 with exc ->
2884 state.text <- Printf.sprintf "bad integer `%s': %s"
2885 s (Printexc.to_string exc);
2886 None
2887 with
2888 | Some angle -> reqlayout angle conf.proportional
2889 | None -> ()
2891 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2893 | 'i' ->
2894 conf.icase <- not conf.icase;
2895 TEdone ("case insensitive search " ^ (btos conf.icase))
2897 | 'p' ->
2898 conf.preload <- not conf.preload;
2899 gotoy state.y;
2900 TEdone ("preload " ^ (btos conf.preload))
2902 | 'v' ->
2903 conf.verbose <- not conf.verbose;
2904 TEdone ("verbose " ^ (btos conf.verbose))
2906 | 'd' ->
2907 conf.debug <- not conf.debug;
2908 TEdone ("debug " ^ (btos conf.debug))
2910 | 'h' ->
2911 conf.maxhfit <- not conf.maxhfit;
2912 state.maxy <- calcheight ();
2913 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2915 | 'c' ->
2916 conf.crophack <- not conf.crophack;
2917 TEdone ("crophack " ^ btos conf.crophack)
2919 | 'a' ->
2920 let s =
2921 match conf.maxwait with
2922 | None ->
2923 conf.maxwait <- Some infinity;
2924 "always wait for page to complete"
2925 | Some _ ->
2926 conf.maxwait <- None;
2927 "show placeholder if page is not ready"
2929 TEdone s
2931 | 'f' ->
2932 conf.underinfo <- not conf.underinfo;
2933 TEdone ("underinfo " ^ btos conf.underinfo)
2935 | 'P' ->
2936 conf.savebmarks <- not conf.savebmarks;
2937 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2939 | 'S' ->
2940 let ondone s =
2942 let pageno, py =
2943 match state.layout with
2944 | [] -> 0, 0
2945 | l :: _ ->
2946 l.pageno, l.pagey
2948 conf.interpagespace <- int_of_string s;
2949 docolumns conf.columns;
2950 state.maxy <- calcheight ();
2951 let y = getpagey pageno in
2952 gotoy (y + py)
2953 with exc ->
2954 state.text <- Printf.sprintf "bad integer `%s': %s"
2955 s (Printexc.to_string exc)
2957 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2959 | 'l' ->
2960 reqlayout conf.angle (not conf.proportional);
2961 TEdone ("proportional display " ^ btos conf.proportional)
2963 | 'T' ->
2964 settrim (not conf.trimmargins) conf.trimfuzz;
2965 TEdone ("trim margins " ^ btos conf.trimmargins)
2967 | 'I' ->
2968 conf.invert <- not conf.invert;
2969 TEdone ("invert colors " ^ btos conf.invert)
2971 | 'x' ->
2972 let ondone s =
2973 cbput state.hists.sel s;
2974 conf.selcmd <- s;
2976 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2977 textentry, ondone, true)
2979 | _ ->
2980 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2981 TEstop
2982 else
2983 TEcont state.text
2986 class type lvsource = object
2987 method getitemcount : int
2988 method getitem : int -> (string * int)
2989 method hasaction : int -> bool
2990 method exit :
2991 uioh:uioh ->
2992 cancel:bool ->
2993 active:int ->
2994 first:int ->
2995 pan:int ->
2996 qsearch:string ->
2997 uioh option
2998 method getactive : int
2999 method getfirst : int
3000 method getqsearch : string
3001 method setqsearch : string -> unit
3002 method getpan : int
3003 end;;
3005 class virtual lvsourcebase = object
3006 val mutable m_active = 0
3007 val mutable m_first = 0
3008 val mutable m_qsearch = ""
3009 val mutable m_pan = 0
3010 method getactive = m_active
3011 method getfirst = m_first
3012 method getqsearch = m_qsearch
3013 method getpan = m_pan
3014 method setqsearch s = m_qsearch <- s
3015 end;;
3017 let withoutlastutf8 s =
3018 let len = String.length s in
3019 if len = 0
3020 then s
3021 else
3022 let rec find pos =
3023 if pos = 0
3024 then pos
3025 else
3026 let b = Char.code s.[pos] in
3027 if b land 0b11000000 = 0b11000000
3028 then pos
3029 else find (pos-1)
3031 let first =
3032 if Char.code s.[len-1] land 0x80 = 0
3033 then len-1
3034 else find (len-1)
3036 String.sub s 0 first;
3039 let textentrykeyboard
3040 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3041 let key =
3042 if key >= 0xffb0 && key <= 0xffb9
3043 then key - 0xffb0 + 48 else key
3045 let enttext te =
3046 state.mode <- Textentry (te, onleave);
3047 state.text <- "";
3048 enttext ();
3049 G.postRedisplay "textentrykeyboard enttext";
3051 let histaction cmd =
3052 match opthist with
3053 | None -> ()
3054 | Some (action, _) ->
3055 state.mode <- Textentry (
3056 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3058 G.postRedisplay "textentry histaction"
3060 match key with
3061 | 0xff08 -> (* backspace *)
3062 let s = withoutlastutf8 text in
3063 let len = String.length s in
3064 if cancelonempty && len = 0
3065 then (
3066 onleave Cancel;
3067 G.postRedisplay "textentrykeyboard after cancel";
3069 else (
3070 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3073 | 0xff0d | 0xff8d -> (* (kp) enter *)
3074 ondone text;
3075 onleave Confirm;
3076 G.postRedisplay "textentrykeyboard after confirm"
3078 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3079 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3080 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3081 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3083 | 0xff1b -> (* escape*)
3084 if String.length text = 0
3085 then (
3086 begin match opthist with
3087 | None -> ()
3088 | Some (_, onhistcancel) -> onhistcancel ()
3089 end;
3090 onleave Cancel;
3091 state.text <- "";
3092 G.postRedisplay "textentrykeyboard after cancel2"
3094 else (
3095 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3098 | 0xff9f | 0xffff -> () (* delete *)
3100 | _ when key != 0
3101 && key land 0xff00 != 0xff00 (* keyboard *)
3102 && key land 0xfe00 != 0xfe00 (* xkb *)
3103 && key land 0xfd00 != 0xfd00 (* 3270 *)
3105 begin match onkey text key with
3106 | TEdone text ->
3107 ondone text;
3108 onleave Confirm;
3109 G.postRedisplay "textentrykeyboard after confirm2";
3111 | TEcont text ->
3112 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3114 | TEstop ->
3115 onleave Cancel;
3116 G.postRedisplay "textentrykeyboard after cancel3"
3118 | TEswitch te ->
3119 state.mode <- Textentry (te, onleave);
3120 G.postRedisplay "textentrykeyboard switch";
3121 end;
3123 | _ ->
3124 vlog "unhandled key %s" (Wsi.keyname key)
3127 let firstof first active =
3128 if first > active || abs (first - active) > fstate.maxrows - 1
3129 then max 0 (active - (fstate.maxrows/2))
3130 else first
3133 let calcfirst first active =
3134 if active > first
3135 then
3136 let rows = active - first in
3137 if rows > fstate.maxrows then active - fstate.maxrows else first
3138 else active
3141 let scrollph y maxy =
3142 let sh = (float (maxy + state.winh) /. float state.winh) in
3143 let sh = float state.winh /. sh in
3144 let sh = max sh (float conf.scrollh) in
3146 let percent =
3147 if y = state.maxy
3148 then 1.0
3149 else float y /. float maxy
3151 let position = (float state.winh -. sh) *. percent in
3153 let position =
3154 if position +. sh > float state.winh
3155 then float state.winh -. sh
3156 else position
3158 position, sh;
3161 let coe s = (s :> uioh);;
3163 class listview ~(source:lvsource) ~trusted ~modehash =
3164 object (self)
3165 val m_pan = source#getpan
3166 val m_first = source#getfirst
3167 val m_active = source#getactive
3168 val m_qsearch = source#getqsearch
3169 val m_prev_uioh = state.uioh
3171 method private elemunder y =
3172 let n = y / (fstate.fontsize+1) in
3173 if m_first + n < source#getitemcount
3174 then (
3175 if source#hasaction (m_first + n)
3176 then Some (m_first + n)
3177 else None
3179 else None
3181 method display =
3182 Gl.enable `blend;
3183 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3184 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3185 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3186 GlDraw.color (1., 1., 1.);
3187 Gl.enable `texture_2d;
3188 let fs = fstate.fontsize in
3189 let nfs = fs + 1 in
3190 let ww = fstate.wwidth in
3191 let tabw = 30.0*.ww in
3192 let itemcount = source#getitemcount in
3193 let rec loop row =
3194 if (row - m_first) > fstate.maxrows
3195 then ()
3196 else (
3197 if row >= 0 && row < itemcount
3198 then (
3199 let (s, level) = source#getitem row in
3200 let y = (row - m_first) * nfs in
3201 let x = 5.0 +. float (level + m_pan) *. ww in
3202 if row = m_active
3203 then (
3204 Gl.disable `texture_2d;
3205 GlDraw.polygon_mode `both `line;
3206 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3207 GlDraw.rect (1., float (y + 1))
3208 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3209 GlDraw.polygon_mode `both `fill;
3210 GlDraw.color (1., 1., 1.);
3211 Gl.enable `texture_2d;
3214 let drawtabularstring s =
3215 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3216 if trusted
3217 then
3218 let tabpos = try String.index s '\t' with Not_found -> -1 in
3219 if tabpos > 0
3220 then
3221 let len = String.length s - tabpos - 1 in
3222 let s1 = String.sub s 0 tabpos
3223 and s2 = String.sub s (tabpos + 1) len in
3224 let nx = drawstr x s1 in
3225 let sw = nx -. x in
3226 let x = x +. (max tabw sw) in
3227 drawstr x s2
3228 else
3229 drawstr x s
3230 else
3231 drawstr x s
3233 let _ = drawtabularstring s in
3234 loop (row+1)
3238 loop m_first;
3239 Gl.disable `blend;
3240 Gl.disable `texture_2d;
3242 method updownlevel incr =
3243 let len = source#getitemcount in
3244 let curlevel =
3245 if m_active >= 0 && m_active < len
3246 then snd (source#getitem m_active)
3247 else -1
3249 let rec flow i =
3250 if i = len then i-1 else if i = -1 then 0 else
3251 let _, l = source#getitem i in
3252 if l != curlevel then i else flow (i+incr)
3254 let active = flow m_active in
3255 let first = calcfirst m_first active in
3256 G.postRedisplay "outline updownlevel";
3257 {< m_active = active; m_first = first >}
3259 method private key1 key mask =
3260 let set1 active first qsearch =
3261 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3263 let search active pattern incr =
3264 let dosearch re =
3265 let rec loop n =
3266 if n >= 0 && n < source#getitemcount
3267 then (
3268 let s, _ = source#getitem n in
3270 (try ignore (Str.search_forward re s 0); true
3271 with Not_found -> false)
3272 then Some n
3273 else loop (n + incr)
3275 else None
3277 loop active
3280 let re = Str.regexp_case_fold pattern in
3281 dosearch re
3282 with Failure s ->
3283 state.text <- s;
3284 None
3286 let itemcount = source#getitemcount in
3287 let find start incr =
3288 let rec find i =
3289 if i = -1 || i = itemcount
3290 then -1
3291 else (
3292 if source#hasaction i
3293 then i
3294 else find (i + incr)
3297 find start
3299 let set active first =
3300 let first = bound first 0 (itemcount - fstate.maxrows) in
3301 state.text <- "";
3302 coe {< m_active = active; m_first = first >}
3304 let navigate incr =
3305 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3306 let active, first =
3307 let incr1 = if incr > 0 then 1 else -1 in
3308 if isvisible m_first m_active
3309 then
3310 let next =
3311 let next = m_active + incr in
3312 let next =
3313 if next < 0 || next >= itemcount
3314 then -1
3315 else find next incr1
3317 if next = -1 || abs (m_active - next) > fstate.maxrows
3318 then -1
3319 else next
3321 if next = -1
3322 then
3323 let first = m_first + incr in
3324 let first = bound first 0 (itemcount - 1) in
3325 let next =
3326 let next = m_active + incr in
3327 let next = bound next 0 (itemcount - 1) in
3328 find next ~-incr1
3330 let active = if next = -1 then m_active else next in
3331 active, first
3332 else
3333 let first = min next m_first in
3334 let first =
3335 if abs (next - first) > fstate.maxrows
3336 then first + incr
3337 else first
3339 next, first
3340 else
3341 let first = m_first + incr in
3342 let first = bound first 0 (itemcount - 1) in
3343 let active =
3344 let next = m_active + incr in
3345 let next = bound next 0 (itemcount - 1) in
3346 let next = find next incr1 in
3347 let active =
3348 if next = -1 || abs (m_active - first) > fstate.maxrows
3349 then (
3350 let active = if m_active = -1 then next else m_active in
3351 active
3353 else next
3355 if isvisible first active
3356 then active
3357 else -1
3359 active, first
3361 G.postRedisplay "listview navigate";
3362 set active first;
3364 match key with
3365 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3366 let incr = if key = 0x72 then -1 else 1 in
3367 let active, first =
3368 match search (m_active + incr) m_qsearch incr with
3369 | None ->
3370 state.text <- m_qsearch ^ " [not found]";
3371 m_active, m_first
3372 | Some active ->
3373 state.text <- m_qsearch;
3374 active, firstof m_first active
3376 G.postRedisplay "listview ctrl-r/s";
3377 set1 active first m_qsearch;
3379 | 0xff08 -> (* backspace *)
3380 if String.length m_qsearch = 0
3381 then coe self
3382 else (
3383 let qsearch = withoutlastutf8 m_qsearch in
3384 let len = String.length qsearch in
3385 if len = 0
3386 then (
3387 state.text <- "";
3388 G.postRedisplay "listview empty qsearch";
3389 set1 m_active m_first "";
3391 else
3392 let active, first =
3393 match search m_active qsearch ~-1 with
3394 | None ->
3395 state.text <- qsearch ^ " [not found]";
3396 m_active, m_first
3397 | Some active ->
3398 state.text <- qsearch;
3399 active, firstof m_first active
3401 G.postRedisplay "listview backspace qsearch";
3402 set1 active first qsearch
3405 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3406 let pattern = m_qsearch ^ Wsi.toutf8 key in
3407 let active, first =
3408 match search m_active pattern 1 with
3409 | None ->
3410 state.text <- pattern ^ " [not found]";
3411 m_active, m_first
3412 | Some active ->
3413 state.text <- pattern;
3414 active, firstof m_first active
3416 G.postRedisplay "listview qsearch add";
3417 set1 active first pattern;
3419 | 0xff1b -> (* escape *)
3420 state.text <- "";
3421 if String.length m_qsearch = 0
3422 then (
3423 G.postRedisplay "list view escape";
3424 begin
3425 match
3426 source#exit (coe self) true m_active m_first m_pan m_qsearch
3427 with
3428 | None -> m_prev_uioh
3429 | Some uioh -> uioh
3432 else (
3433 G.postRedisplay "list view kill qsearch";
3434 source#setqsearch "";
3435 coe {< m_qsearch = "" >}
3438 | 0xff0d | 0xff8d -> (* (kp) enter *)
3439 state.text <- "";
3440 let self = {< m_qsearch = "" >} in
3441 source#setqsearch "";
3442 let opt =
3443 G.postRedisplay "listview enter";
3444 if m_active >= 0 && m_active < source#getitemcount
3445 then (
3446 source#exit (coe self) false m_active m_first m_pan "";
3448 else (
3449 source#exit (coe self) true m_active m_first m_pan "";
3452 begin match opt with
3453 | None -> m_prev_uioh
3454 | Some uioh -> uioh
3457 | 0xff9f | 0xffff -> (* (kp) delete *)
3458 coe self
3460 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3461 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3462 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3463 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3465 | 0xff53 | 0xff98 -> (* (kp) right *)
3466 state.text <- "";
3467 G.postRedisplay "listview right";
3468 coe {< m_pan = m_pan - 1 >}
3470 | 0xff51 | 0xff96 -> (* (kp) left *)
3471 state.text <- "";
3472 G.postRedisplay "listview left";
3473 coe {< m_pan = m_pan + 1 >}
3475 | 0xff50 | 0xff95 -> (* (kp) home *)
3476 let active = find 0 1 in
3477 G.postRedisplay "listview home";
3478 set active 0;
3480 | 0xff57 | 0xff9c -> (* (kp) end *)
3481 let first = max 0 (itemcount - fstate.maxrows) in
3482 let active = find (itemcount - 1) ~-1 in
3483 G.postRedisplay "listview end";
3484 set active first;
3486 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3487 coe self
3489 | _ ->
3490 dolog "listview unknown key %#x" key; coe self
3492 method key key mask =
3493 match state.mode with
3494 | Textentry te -> textentrykeyboard key mask te; coe self
3495 | _ -> self#key1 key mask
3497 method button button down x y _ =
3498 let opt =
3499 match button with
3500 | 1 when x > state.winw - conf.scrollbw ->
3501 G.postRedisplay "listview scroll";
3502 if down
3503 then
3504 let _, position, sh = self#scrollph in
3505 if y > truncate position && y < truncate (position +. sh)
3506 then (
3507 state.mstate <- Mscrolly;
3508 Some (coe self)
3510 else
3511 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3512 let first = truncate (s *. float source#getitemcount) in
3513 let first = min source#getitemcount first in
3514 Some (coe {< m_first = first; m_active = first >})
3515 else (
3516 state.mstate <- Mnone;
3517 Some (coe self);
3519 | 1 when not down ->
3520 begin match self#elemunder y with
3521 | Some n ->
3522 G.postRedisplay "listview click";
3523 source#exit
3524 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3525 | _ ->
3526 Some (coe self)
3528 | n when (n == 4 || n == 5) && not down ->
3529 let len = source#getitemcount in
3530 let first =
3531 if n = 5 && m_first + fstate.maxrows >= len
3532 then
3533 m_first
3534 else
3535 let first = m_first + (if n == 4 then -1 else 1) in
3536 bound first 0 (len - 1)
3538 G.postRedisplay "listview wheel";
3539 Some (coe {< m_first = first >})
3540 | n when (n = 6 || n = 7) && not down ->
3541 let inc = m_first + (if n = 7 then -1 else 1) in
3542 G.postRedisplay "listview hwheel";
3543 Some (coe {< m_pan = m_pan + inc >})
3544 | _ ->
3545 Some (coe self)
3547 match opt with
3548 | None -> m_prev_uioh
3549 | Some uioh -> uioh
3551 method motion _ y =
3552 match state.mstate with
3553 | Mscrolly ->
3554 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3555 let first = truncate (s *. float source#getitemcount) in
3556 let first = min source#getitemcount first in
3557 G.postRedisplay "listview motion";
3558 coe {< m_first = first; m_active = first >}
3559 | _ -> coe self
3561 method pmotion x y =
3562 if x < state.winw - conf.scrollbw
3563 then
3564 let n =
3565 match self#elemunder y with
3566 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3567 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3569 let o =
3570 if n != m_active
3571 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3572 else self
3574 coe o
3575 else (
3576 Wsi.setcursor Wsi.CURSOR_INHERIT;
3577 coe self
3580 method infochanged _ = ()
3582 method scrollpw = (0, 0.0, 0.0)
3583 method scrollph =
3584 let nfs = fstate.fontsize + 1 in
3585 let y = m_first * nfs in
3586 let itemcount = source#getitemcount in
3587 let maxi = max 0 (itemcount - fstate.maxrows) in
3588 let maxy = maxi * nfs in
3589 let p, h = scrollph y maxy in
3590 conf.scrollbw, p, h
3592 method modehash = modehash
3593 end;;
3595 class outlinelistview ~source =
3596 object (self)
3597 inherit listview
3598 ~source:(source :> lvsource)
3599 ~trusted:false
3600 ~modehash:(findkeyhash conf "outline")
3601 as super
3603 method key key mask =
3604 let calcfirst first active =
3605 if active > first
3606 then
3607 let rows = active - first in
3608 let maxrows =
3609 if String.length state.text = 0
3610 then fstate.maxrows
3611 else fstate.maxrows - 2
3613 if rows > maxrows then active - maxrows else first
3614 else active
3616 let navigate incr =
3617 let active = m_active + incr in
3618 let active = bound active 0 (source#getitemcount - 1) in
3619 let first = calcfirst m_first active in
3620 G.postRedisplay "outline navigate";
3621 coe {< m_active = active; m_first = first >}
3623 let ctrl = Wsi.withctrl mask in
3624 match key with
3625 | 110 when ctrl -> (* ctrl-n *)
3626 source#narrow m_qsearch;
3627 G.postRedisplay "outline ctrl-n";
3628 coe {< m_first = 0; m_active = 0 >}
3630 | 117 when ctrl -> (* ctrl-u *)
3631 source#denarrow;
3632 G.postRedisplay "outline ctrl-u";
3633 state.text <- "";
3634 coe {< m_first = 0; m_active = 0 >}
3636 | 108 when ctrl -> (* ctrl-l *)
3637 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3638 G.postRedisplay "outline ctrl-l";
3639 coe {< m_first = first >}
3641 | 0xff9f | 0xffff -> (* (kp) delete *)
3642 source#remove m_active;
3643 G.postRedisplay "outline delete";
3644 let active = max 0 (m_active-1) in
3645 coe {< m_first = firstof m_first active;
3646 m_active = active >}
3648 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3649 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3650 | 0xff55 | 0xff9a -> (* (kp) prior *)
3651 navigate ~-(fstate.maxrows)
3652 | 0xff56 | 0xff9b -> (* (kp) next *)
3653 navigate fstate.maxrows
3655 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3656 let o =
3657 if ctrl
3658 then (
3659 G.postRedisplay "outline ctrl right";
3660 {< m_pan = m_pan + 1 >}
3662 else self#updownlevel 1
3664 coe o
3666 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3667 let o =
3668 if ctrl
3669 then (
3670 G.postRedisplay "outline ctrl left";
3671 {< m_pan = m_pan - 1 >}
3673 else self#updownlevel ~-1
3675 coe o
3677 | 0xff50 | 0xff95 -> (* (kp) home *)
3678 G.postRedisplay "outline home";
3679 coe {< m_first = 0; m_active = 0 >}
3681 | 0xff57 | 0xff9c -> (* (kp) end *)
3682 let active = source#getitemcount - 1 in
3683 let first = max 0 (active - fstate.maxrows) in
3684 G.postRedisplay "outline end";
3685 coe {< m_active = active; m_first = first >}
3687 | _ -> super#key key mask
3690 let outlinesource usebookmarks =
3691 let empty = [||] in
3692 (object
3693 inherit lvsourcebase
3694 val mutable m_items = empty
3695 val mutable m_orig_items = empty
3696 val mutable m_prev_items = empty
3697 val mutable m_narrow_pattern = ""
3698 val mutable m_hadremovals = false
3700 method getitemcount =
3701 Array.length m_items + (if m_hadremovals then 1 else 0)
3703 method getitem n =
3704 if n == Array.length m_items && m_hadremovals
3705 then
3706 ("[Confirm removal]", 0)
3707 else
3708 let s, n, _ = m_items.(n) in
3709 (s, n)
3711 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3712 ignore (uioh, first, qsearch);
3713 let confrimremoval = m_hadremovals && active = Array.length m_items in
3714 let items =
3715 if String.length m_narrow_pattern = 0
3716 then m_orig_items
3717 else m_items
3719 if not cancel
3720 then (
3721 if not confrimremoval
3722 then(
3723 let _, _, anchor = m_items.(active) in
3724 gotoghyll (getanchory anchor);
3725 m_items <- items;
3727 else (
3728 state.bookmarks <- Array.to_list m_items;
3729 m_orig_items <- m_items;
3732 else m_items <- items;
3733 m_pan <- pan;
3734 None
3736 method hasaction _ = true
3738 method greetmsg =
3739 if Array.length m_items != Array.length m_orig_items
3740 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3741 else ""
3743 method narrow pattern =
3744 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3745 match reopt with
3746 | None -> ()
3747 | Some re ->
3748 let rec loop accu n =
3749 if n = -1
3750 then (
3751 m_narrow_pattern <- pattern;
3752 m_items <- Array.of_list accu
3754 else
3755 let (s, _, _) as o = m_items.(n) in
3756 let accu =
3757 if (try ignore (Str.search_forward re s 0); true
3758 with Not_found -> false)
3759 then o :: accu
3760 else accu
3762 loop accu (n-1)
3764 loop [] (Array.length m_items - 1)
3766 method denarrow =
3767 m_orig_items <- (
3768 if usebookmarks
3769 then Array.of_list state.bookmarks
3770 else state.outlines
3772 m_items <- m_orig_items
3774 method remove m =
3775 if usebookmarks
3776 then
3777 if m >= 0 && m < Array.length m_items
3778 then (
3779 m_hadremovals <- true;
3780 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3781 let n = if n >= m then n+1 else n in
3782 m_items.(n)
3786 method reset anchor items =
3787 m_hadremovals <- false;
3788 if m_orig_items == empty || m_prev_items != items
3789 then (
3790 m_orig_items <- items;
3791 if String.length m_narrow_pattern = 0
3792 then m_items <- items;
3794 m_prev_items <- items;
3795 let rely = getanchory anchor in
3796 let active =
3797 let rec loop n best bestd =
3798 if n = Array.length m_items
3799 then best
3800 else
3801 let (_, _, anchor) = m_items.(n) in
3802 let orely = getanchory anchor in
3803 let d = abs (orely - rely) in
3804 if d < bestd
3805 then loop (n+1) n d
3806 else loop (n+1) best bestd
3808 loop 0 ~-1 max_int
3810 m_active <- active;
3811 m_first <- firstof m_first active
3812 end)
3815 let enterselector usebookmarks =
3816 let source = outlinesource usebookmarks in
3817 fun errmsg ->
3818 let outlines =
3819 if usebookmarks
3820 then Array.of_list state.bookmarks
3821 else state.outlines
3823 if Array.length outlines = 0
3824 then (
3825 showtext ' ' errmsg;
3827 else (
3828 state.text <- source#greetmsg;
3829 Wsi.setcursor Wsi.CURSOR_INHERIT;
3830 let anchor = getanchor () in
3831 source#reset anchor outlines;
3832 state.uioh <- coe (new outlinelistview ~source);
3833 G.postRedisplay "enter selector";
3837 let enteroutlinemode =
3838 let f = enterselector false in
3839 fun ()-> f "Document has no outline";
3842 let enterbookmarkmode =
3843 let f = enterselector true in
3844 fun () -> f "Document has no bookmarks (yet)";
3847 let color_of_string s =
3848 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3849 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3853 let color_to_string (r, g, b) =
3854 let r = truncate (r *. 256.0)
3855 and g = truncate (g *. 256.0)
3856 and b = truncate (b *. 256.0) in
3857 Printf.sprintf "%d/%d/%d" r g b
3860 let irect_of_string s =
3861 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3864 let irect_to_string (x0,y0,x1,y1) =
3865 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3868 let makecheckers () =
3869 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3870 following to say:
3871 converted by Issac Trotts. July 25, 2002 *)
3872 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3873 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3874 let id = GlTex.gen_texture () in
3875 GlTex.bind_texture `texture_2d id;
3876 GlPix.store (`unpack_alignment 1);
3877 GlTex.image2d image;
3878 List.iter (GlTex.parameter ~target:`texture_2d)
3879 [ `mag_filter `nearest; `min_filter `nearest ];
3883 let setcheckers enabled =
3884 match state.texid with
3885 | None ->
3886 if enabled then state.texid <- Some (makecheckers ())
3888 | Some texid ->
3889 if not enabled
3890 then (
3891 GlTex.delete_texture texid;
3892 state.texid <- None;
3896 let int_of_string_with_suffix s =
3897 let l = String.length s in
3898 let s1, shift =
3899 if l > 1
3900 then
3901 let suffix = Char.lowercase s.[l-1] in
3902 match suffix with
3903 | 'k' -> String.sub s 0 (l-1), 10
3904 | 'm' -> String.sub s 0 (l-1), 20
3905 | 'g' -> String.sub s 0 (l-1), 30
3906 | _ -> s, 0
3907 else s, 0
3909 let n = int_of_string s1 in
3910 let m = n lsl shift in
3911 if m < 0 || m < n
3912 then raise (Failure "value too large")
3913 else m
3916 let string_with_suffix_of_int n =
3917 if n = 0
3918 then "0"
3919 else
3920 let n, s =
3921 if n land ((1 lsl 30) - 1) = 0
3922 then n lsr 30, "G"
3923 else (
3924 if n land ((1 lsl 20) - 1) = 0
3925 then n lsr 20, "M"
3926 else (
3927 if n land ((1 lsl 10) - 1) = 0
3928 then n lsr 10, "K"
3929 else n, ""
3933 let rec loop s n =
3934 let h = n mod 1000 in
3935 let n = n / 1000 in
3936 if n = 0
3937 then string_of_int h ^ s
3938 else (
3939 let s = Printf.sprintf "_%03d%s" h s in
3940 loop s n
3943 loop "" n ^ s;
3946 let defghyllscroll = (40, 8, 32);;
3947 let ghyllscroll_of_string s =
3948 let (n, a, b) as nab =
3949 if s = "default"
3950 then defghyllscroll
3951 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3953 if n <= a || n <= b || a >= b
3954 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3955 nab;
3958 let ghyllscroll_to_string ((n, a, b) as nab) =
3959 if nab = defghyllscroll
3960 then "default"
3961 else Printf.sprintf "%d,%d,%d" n a b;
3964 let describe_location () =
3965 let f (fn, _) l =
3966 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3968 let fn, ln = List.fold_left f (-1, -1) state.layout in
3969 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3970 let percent =
3971 if maxy <= 0
3972 then 100.
3973 else (100. *. (float state.y /. float maxy))
3975 if fn = ln
3976 then
3977 Printf.sprintf "page %d of %d [%.2f%%]"
3978 (fn+1) state.pagecount percent
3979 else
3980 Printf.sprintf
3981 "pages %d-%d of %d [%.2f%%]"
3982 (fn+1) (ln+1) state.pagecount percent
3985 let setpresentationmode v =
3986 let (n, _, _) = getanchor () in
3987 let _, h = getpageyh n in
3988 let ips = if conf.presentation then calcips h else conf.interpagespace in
3989 state.anchor <- (n, 0.0, float ips);
3990 conf.presentation <- v;
3991 if conf.presentation
3992 then (
3993 if not conf.scrollbarinpm
3994 then state.scrollw <- 0;
3996 else state.scrollw <- conf.scrollbw;
3997 represent ();
4000 let enterinfomode =
4001 let btos b = if b then "\xe2\x88\x9a" else "" in
4002 let showextended = ref false in
4003 let leave mode = function
4004 | Confirm -> state.mode <- mode
4005 | Cancel -> state.mode <- mode in
4006 let src =
4007 (object
4008 val mutable m_first_time = true
4009 val mutable m_l = []
4010 val mutable m_a = [||]
4011 val mutable m_prev_uioh = nouioh
4012 val mutable m_prev_mode = View
4014 inherit lvsourcebase
4016 method reset prev_mode prev_uioh =
4017 m_a <- Array.of_list (List.rev m_l);
4018 m_l <- [];
4019 m_prev_mode <- prev_mode;
4020 m_prev_uioh <- prev_uioh;
4021 if m_first_time
4022 then (
4023 let rec loop n =
4024 if n >= Array.length m_a
4025 then ()
4026 else
4027 match m_a.(n) with
4028 | _, _, _, Action _ -> m_active <- n
4029 | _ -> loop (n+1)
4031 loop 0;
4032 m_first_time <- false;
4035 method int name get set =
4036 m_l <-
4037 (name, `int get, 1, Action (
4038 fun u ->
4039 let ondone s =
4040 try set (int_of_string s)
4041 with exn ->
4042 state.text <- Printf.sprintf "bad integer `%s': %s"
4043 s (Printexc.to_string exn)
4045 state.text <- "";
4046 let te = name ^ ": ", "", None, intentry, ondone, true in
4047 state.mode <- Textentry (te, leave m_prev_mode);
4049 )) :: m_l
4051 method int_with_suffix name get set =
4052 m_l <-
4053 (name, `intws get, 1, Action (
4054 fun u ->
4055 let ondone s =
4056 try set (int_of_string_with_suffix s)
4057 with exn ->
4058 state.text <- Printf.sprintf "bad integer `%s': %s"
4059 s (Printexc.to_string exn)
4061 state.text <- "";
4062 let te =
4063 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4065 state.mode <- Textentry (te, leave m_prev_mode);
4067 )) :: m_l
4069 method bool ?(offset=1) ?(btos=btos) name get set =
4070 m_l <-
4071 (name, `bool (btos, get), offset, Action (
4072 fun u ->
4073 let v = get () in
4074 set (not v);
4076 )) :: m_l
4078 method color name get set =
4079 m_l <-
4080 (name, `color get, 1, Action (
4081 fun u ->
4082 let invalid = (nan, nan, nan) in
4083 let ondone s =
4084 let c =
4085 try color_of_string s
4086 with exn ->
4087 state.text <- Printf.sprintf "bad color `%s': %s"
4088 s (Printexc.to_string exn);
4089 invalid
4091 if c <> invalid
4092 then set c;
4094 let te = name ^ ": ", "", None, textentry, ondone, true in
4095 state.text <- color_to_string (get ());
4096 state.mode <- Textentry (te, leave m_prev_mode);
4098 )) :: m_l
4100 method string name get set =
4101 m_l <-
4102 (name, `string get, 1, Action (
4103 fun u ->
4104 let ondone s = set s in
4105 let te = name ^ ": ", "", None, textentry, ondone, true in
4106 state.mode <- Textentry (te, leave m_prev_mode);
4108 )) :: m_l
4110 method colorspace name get set =
4111 m_l <-
4112 (name, `string get, 1, Action (
4113 fun _ ->
4114 let source =
4115 let vals = [| "rgb"; "bgr"; "gray" |] in
4116 (object
4117 inherit lvsourcebase
4119 initializer
4120 m_active <- int_of_colorspace conf.colorspace;
4121 m_first <- 0;
4123 method getitemcount = Array.length vals
4124 method getitem n = (vals.(n), 0)
4125 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4126 ignore (uioh, first, pan, qsearch);
4127 if not cancel then set active;
4128 None
4129 method hasaction _ = true
4130 end)
4132 state.text <- "";
4133 let modehash = findkeyhash conf "info" in
4134 coe (new listview ~source ~trusted:true ~modehash)
4135 )) :: m_l
4137 method caption s offset =
4138 m_l <- (s, `empty, offset, Noaction) :: m_l
4140 method caption2 s f offset =
4141 m_l <- (s, `string f, offset, Noaction) :: m_l
4143 method getitemcount = Array.length m_a
4145 method getitem n =
4146 let tostr = function
4147 | `int f -> string_of_int (f ())
4148 | `intws f -> string_with_suffix_of_int (f ())
4149 | `string f -> f ()
4150 | `color f -> color_to_string (f ())
4151 | `bool (btos, f) -> btos (f ())
4152 | `empty -> ""
4154 let name, t, offset, _ = m_a.(n) in
4155 ((let s = tostr t in
4156 if String.length s > 0
4157 then Printf.sprintf "%s\t%s" name s
4158 else name),
4159 offset)
4161 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4162 let uiohopt =
4163 if not cancel
4164 then (
4165 m_qsearch <- qsearch;
4166 let uioh =
4167 match m_a.(active) with
4168 | _, _, _, Action f -> f uioh
4169 | _ -> uioh
4171 Some uioh
4173 else None
4175 m_active <- active;
4176 m_first <- first;
4177 m_pan <- pan;
4178 uiohopt
4180 method hasaction n =
4181 match m_a.(n) with
4182 | _, _, _, Action _ -> true
4183 | _ -> false
4184 end)
4186 let rec fillsrc prevmode prevuioh =
4187 let sep () = src#caption "" 0 in
4188 let colorp name get set =
4189 src#string name
4190 (fun () -> color_to_string (get ()))
4191 (fun v ->
4193 let c = color_of_string v in
4194 set c
4195 with exn ->
4196 state.text <- Printf.sprintf "bad color `%s': %s"
4197 v (Printexc.to_string exn);
4200 let oldmode = state.mode in
4201 let birdseye = isbirdseye state.mode in
4203 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4205 src#bool "presentation mode"
4206 (fun () -> conf.presentation)
4207 (fun v -> setpresentationmode v);
4209 src#bool "ignore case in searches"
4210 (fun () -> conf.icase)
4211 (fun v -> conf.icase <- v);
4213 src#bool "preload"
4214 (fun () -> conf.preload)
4215 (fun v -> conf.preload <- v);
4217 src#bool "highlight links"
4218 (fun () -> conf.hlinks)
4219 (fun v -> conf.hlinks <- v);
4221 src#bool "under info"
4222 (fun () -> conf.underinfo)
4223 (fun v -> conf.underinfo <- v);
4225 src#bool "persistent bookmarks"
4226 (fun () -> conf.savebmarks)
4227 (fun v -> conf.savebmarks <- v);
4229 src#bool "proportional display"
4230 (fun () -> conf.proportional)
4231 (fun v -> reqlayout conf.angle v);
4233 src#bool "trim margins"
4234 (fun () -> conf.trimmargins)
4235 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4237 src#bool "persistent location"
4238 (fun () -> conf.jumpback)
4239 (fun v -> conf.jumpback <- v);
4241 sep ();
4242 src#int "inter-page space"
4243 (fun () -> conf.interpagespace)
4244 (fun n ->
4245 conf.interpagespace <- n;
4246 docolumns conf.columns;
4247 let pageno, py =
4248 match state.layout with
4249 | [] -> 0, 0
4250 | l :: _ ->
4251 l.pageno, l.pagey
4253 state.maxy <- calcheight ();
4254 let y = getpagey pageno in
4255 gotoy (y + py)
4258 src#int "page bias"
4259 (fun () -> conf.pagebias)
4260 (fun v -> conf.pagebias <- v);
4262 src#int "scroll step"
4263 (fun () -> conf.scrollstep)
4264 (fun n -> conf.scrollstep <- n);
4266 src#int "horizontal scroll step"
4267 (fun () -> conf.hscrollstep)
4268 (fun v -> conf.hscrollstep <- v);
4270 src#int "auto scroll step"
4271 (fun () ->
4272 match state.autoscroll with
4273 | Some step -> step
4274 | _ -> conf.autoscrollstep)
4275 (fun n ->
4276 if state.autoscroll <> None
4277 then state.autoscroll <- Some n;
4278 conf.autoscrollstep <- n);
4280 src#int "zoom"
4281 (fun () -> truncate (conf.zoom *. 100.))
4282 (fun v -> setzoom ((float v) /. 100.));
4284 src#int "rotation"
4285 (fun () -> conf.angle)
4286 (fun v -> reqlayout v conf.proportional);
4288 src#int "scroll bar width"
4289 (fun () -> state.scrollw)
4290 (fun v ->
4291 state.scrollw <- v;
4292 conf.scrollbw <- v;
4293 reshape state.winw state.winh;
4296 src#int "scroll handle height"
4297 (fun () -> conf.scrollh)
4298 (fun v -> conf.scrollh <- v;);
4300 src#int "thumbnail width"
4301 (fun () -> conf.thumbw)
4302 (fun v ->
4303 conf.thumbw <- min 4096 v;
4304 match oldmode with
4305 | Birdseye beye ->
4306 leavebirdseye beye false;
4307 enterbirdseye ()
4308 | _ -> ()
4311 let mode = state.mode in
4312 src#string "columns"
4313 (fun () ->
4314 match conf.columns with
4315 | Csingle _ -> "1"
4316 | Cmulti (multi, _) -> multicolumns_to_string multi
4317 | Csplit (count, _) -> "-" ^ string_of_int count
4319 (fun v ->
4320 let n, a, b = multicolumns_of_string v in
4321 setcolumns mode n a b);
4323 sep ();
4324 src#caption "Presentation mode" 0;
4325 src#bool "scrollbar visible"
4326 (fun () -> conf.scrollbarinpm)
4327 (fun v ->
4328 if v != conf.scrollbarinpm
4329 then (
4330 conf.scrollbarinpm <- v;
4331 if conf.presentation
4332 then (
4333 state.scrollw <- if v then conf.scrollbw else 0;
4334 reshape state.winw state.winh;
4339 sep ();
4340 src#caption "Pixmap cache" 0;
4341 src#int_with_suffix "size (advisory)"
4342 (fun () -> conf.memlimit)
4343 (fun v -> conf.memlimit <- v);
4345 src#caption2 "used"
4346 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4347 (string_with_suffix_of_int state.memused)
4348 (Hashtbl.length state.tilemap)) 1;
4350 sep ();
4351 src#caption "Layout" 0;
4352 src#caption2 "Dimension"
4353 (fun () ->
4354 Printf.sprintf "%dx%d (virtual %dx%d)"
4355 state.winw state.winh
4356 state.w state.maxy)
4358 if conf.debug
4359 then
4360 src#caption2 "Position" (fun () ->
4361 Printf.sprintf "%dx%d" state.x state.y
4363 else
4364 src#caption2 "Visible" (fun () -> describe_location ()) 1
4367 sep ();
4368 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4369 "Save these parameters as global defaults at exit"
4370 (fun () -> conf.bedefault)
4371 (fun v -> conf.bedefault <- v)
4374 sep ();
4375 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4376 src#bool ~offset:0 ~btos "Extended parameters"
4377 (fun () -> !showextended)
4378 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4379 if !showextended
4380 then (
4381 src#bool "checkers"
4382 (fun () -> conf.checkers)
4383 (fun v -> conf.checkers <- v; setcheckers v);
4384 src#bool "update cursor"
4385 (fun () -> conf.updatecurs)
4386 (fun v -> conf.updatecurs <- v);
4387 src#bool "verbose"
4388 (fun () -> conf.verbose)
4389 (fun v -> conf.verbose <- v);
4390 src#bool "invert colors"
4391 (fun () -> conf.invert)
4392 (fun v -> conf.invert <- v);
4393 src#bool "max fit"
4394 (fun () -> conf.maxhfit)
4395 (fun v -> conf.maxhfit <- v);
4396 src#bool "redirect stderr"
4397 (fun () -> conf.redirectstderr)
4398 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4399 src#string "uri launcher"
4400 (fun () -> conf.urilauncher)
4401 (fun v -> conf.urilauncher <- v);
4402 src#string "path launcher"
4403 (fun () -> conf.pathlauncher)
4404 (fun v -> conf.pathlauncher <- v);
4405 src#string "tile size"
4406 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4407 (fun v ->
4409 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4410 conf.tilew <- max 64 w;
4411 conf.tileh <- max 64 h;
4412 flushtiles ();
4413 with exn ->
4414 state.text <- Printf.sprintf "bad tile size `%s': %s"
4415 v (Printexc.to_string exn));
4416 src#int "texture count"
4417 (fun () -> conf.texcount)
4418 (fun v ->
4419 if realloctexts v
4420 then conf.texcount <- v
4421 else showtext '!' " Failed to set texture count please retry later"
4423 src#int "slice height"
4424 (fun () -> conf.sliceheight)
4425 (fun v ->
4426 conf.sliceheight <- v;
4427 wcmd "sliceh %d" conf.sliceheight;
4429 src#int "anti-aliasing level"
4430 (fun () -> conf.aalevel)
4431 (fun v ->
4432 conf.aalevel <- bound v 0 8;
4433 state.anchor <- getanchor ();
4434 opendoc state.path state.password;
4436 src#string "page scroll scaling factor"
4437 (fun () -> string_of_float conf.pgscale)
4438 (fun v ->
4440 let s = float_of_string v in
4441 conf.pgscale <- s
4442 with exn ->
4443 state.text <- Printf.sprintf
4444 "bad page scroll scaling factor `%s': %s"
4445 v (Printexc.to_string exn)
4448 src#int "ui font size"
4449 (fun () -> fstate.fontsize)
4450 (fun v -> setfontsize (bound v 5 100));
4451 src#int "hint font size"
4452 (fun () -> conf.hfsize)
4453 (fun v -> conf.hfsize <- bound v 5 100);
4454 colorp "background color"
4455 (fun () -> conf.bgcolor)
4456 (fun v -> conf.bgcolor <- v);
4457 src#bool "crop hack"
4458 (fun () -> conf.crophack)
4459 (fun v -> conf.crophack <- v);
4460 src#string "trim fuzz"
4461 (fun () -> irect_to_string conf.trimfuzz)
4462 (fun v ->
4464 conf.trimfuzz <- irect_of_string v;
4465 if conf.trimmargins
4466 then settrim true conf.trimfuzz;
4467 with exn ->
4468 state.text <- Printf.sprintf "bad irect `%s': %s"
4469 v (Printexc.to_string exn)
4471 src#string "throttle"
4472 (fun () ->
4473 match conf.maxwait with
4474 | None -> "show place holder if page is not ready"
4475 | Some time ->
4476 if time = infinity
4477 then "wait for page to fully render"
4478 else
4479 "wait " ^ string_of_float time
4480 ^ " seconds before showing placeholder"
4482 (fun v ->
4484 let f = float_of_string v in
4485 if f <= 0.0
4486 then conf.maxwait <- None
4487 else conf.maxwait <- Some f
4488 with exn ->
4489 state.text <- Printf.sprintf "bad time `%s': %s"
4490 v (Printexc.to_string exn)
4492 src#string "ghyll scroll"
4493 (fun () ->
4494 match conf.ghyllscroll with
4495 | None -> ""
4496 | Some nab -> ghyllscroll_to_string nab
4498 (fun v ->
4500 let gs =
4501 if String.length v = 0
4502 then None
4503 else Some (ghyllscroll_of_string v)
4505 conf.ghyllscroll <- gs
4506 with exn ->
4507 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4508 v (Printexc.to_string exn)
4510 src#string "selection command"
4511 (fun () -> conf.selcmd)
4512 (fun v -> conf.selcmd <- v);
4513 src#string "synctex command"
4514 (fun () -> conf.stcmd)
4515 (fun v -> conf.stcmd <- v);
4516 src#colorspace "color space"
4517 (fun () -> colorspace_to_string conf.colorspace)
4518 (fun v ->
4519 conf.colorspace <- colorspace_of_int v;
4520 wcmd "cs %d" v;
4521 load state.layout;
4523 if pbousable ()
4524 then
4525 src#bool "use PBO"
4526 (fun () -> conf.usepbo)
4527 (fun v -> conf.usepbo <- v);
4528 src#bool "mouse wheel scrolls pages"
4529 (fun () -> conf.wheelbypage)
4530 (fun v -> conf.wheelbypage <- v);
4533 sep ();
4534 src#caption "Document" 0;
4535 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4536 src#caption2 "Pages"
4537 (fun () -> string_of_int state.pagecount) 1;
4538 src#caption2 "Dimensions"
4539 (fun () -> string_of_int (List.length state.pdims)) 1;
4540 if conf.trimmargins
4541 then (
4542 sep ();
4543 src#caption "Trimmed margins" 0;
4544 src#caption2 "Dimensions"
4545 (fun () -> string_of_int (List.length state.pdims)) 1;
4548 sep ();
4549 src#caption "OpenGL" 0;
4550 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4551 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4552 src#reset prevmode prevuioh;
4554 fun () ->
4555 state.text <- "";
4556 let prevmode = state.mode
4557 and prevuioh = state.uioh in
4558 fillsrc prevmode prevuioh;
4559 let source = (src :> lvsource) in
4560 let modehash = findkeyhash conf "info" in
4561 state.uioh <- coe (object (self)
4562 inherit listview ~source ~trusted:true ~modehash as super
4563 val mutable m_prevmemused = 0
4564 method infochanged = function
4565 | Memused ->
4566 if m_prevmemused != state.memused
4567 then (
4568 m_prevmemused <- state.memused;
4569 G.postRedisplay "memusedchanged";
4571 | Pdim -> G.postRedisplay "pdimchanged"
4572 | Docinfo -> fillsrc prevmode prevuioh
4574 method key key mask =
4575 if not (Wsi.withctrl mask)
4576 then
4577 match key with
4578 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4579 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4580 | _ -> super#key key mask
4581 else super#key key mask
4582 end);
4583 G.postRedisplay "info";
4586 let enterhelpmode =
4587 let source =
4588 (object
4589 inherit lvsourcebase
4590 method getitemcount = Array.length state.help
4591 method getitem n =
4592 let s, l, _ = state.help.(n) in
4593 (s, l)
4595 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4596 let optuioh =
4597 if not cancel
4598 then (
4599 m_qsearch <- qsearch;
4600 match state.help.(active) with
4601 | _, _, Action f -> Some (f uioh)
4602 | _ -> Some (uioh)
4604 else None
4606 m_active <- active;
4607 m_first <- first;
4608 m_pan <- pan;
4609 optuioh
4611 method hasaction n =
4612 match state.help.(n) with
4613 | _, _, Action _ -> true
4614 | _ -> false
4616 initializer
4617 m_active <- -1
4618 end)
4619 in fun () ->
4620 let modehash = findkeyhash conf "help" in
4621 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4622 G.postRedisplay "help";
4625 let entermsgsmode =
4626 let msgsource =
4627 let re = Str.regexp "[\r\n]" in
4628 (object
4629 inherit lvsourcebase
4630 val mutable m_items = [||]
4632 method getitemcount = 1 + Array.length m_items
4634 method getitem n =
4635 if n = 0
4636 then "[Clear]", 0
4637 else m_items.(n-1), 0
4639 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4640 ignore uioh;
4641 if not cancel
4642 then (
4643 if active = 0
4644 then Buffer.clear state.errmsgs;
4645 m_qsearch <- qsearch;
4647 m_active <- active;
4648 m_first <- first;
4649 m_pan <- pan;
4650 None
4652 method hasaction n =
4653 n = 0
4655 method reset =
4656 state.newerrmsgs <- false;
4657 let l = Str.split re (Buffer.contents state.errmsgs) in
4658 m_items <- Array.of_list l
4660 initializer
4661 m_active <- 0
4662 end)
4663 in fun () ->
4664 state.text <- "";
4665 msgsource#reset;
4666 let source = (msgsource :> lvsource) in
4667 let modehash = findkeyhash conf "listview" in
4668 state.uioh <- coe (object
4669 inherit listview ~source ~trusted:false ~modehash as super
4670 method display =
4671 if state.newerrmsgs
4672 then msgsource#reset;
4673 super#display
4674 end);
4675 G.postRedisplay "msgs";
4678 let quickbookmark ?title () =
4679 match state.layout with
4680 | [] -> ()
4681 | l :: _ ->
4682 let title =
4683 match title with
4684 | None ->
4685 let sec = Unix.gettimeofday () in
4686 let tm = Unix.localtime sec in
4687 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4688 (l.pageno+1)
4689 tm.Unix.tm_mday
4690 tm.Unix.tm_mon
4691 (tm.Unix.tm_year + 1900)
4692 tm.Unix.tm_hour
4693 tm.Unix.tm_min
4694 | Some title -> title
4696 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4699 let doreshape w h =
4700 Wsi.reshape w h;
4703 let setautoscrollspeed step goingdown =
4704 let incr = max 1 ((abs step) / 2) in
4705 let incr = if goingdown then incr else -incr in
4706 let astep = step + incr in
4707 state.autoscroll <- Some astep;
4710 let gotounder = function
4711 | Ulinkgoto (pageno, top) ->
4712 if pageno >= 0
4713 then (
4714 addnav ();
4715 gotopage1 pageno top;
4718 | Ulinkuri s ->
4719 gotouri s
4721 | Uremote (filename, pageno) ->
4722 let path =
4723 if Sys.file_exists filename
4724 then filename
4725 else
4726 let dir = Filename.dirname state.path in
4727 let path = Filename.concat dir filename in
4728 if Sys.file_exists path
4729 then path
4730 else ""
4732 if String.length path > 0
4733 then (
4734 let anchor = getanchor () in
4735 let ranchor = state.path, state.password, anchor in
4736 state.anchor <- (pageno, 0.0, 0.0);
4737 state.ranchors <- ranchor :: state.ranchors;
4738 opendoc path "";
4740 else showtext '!' ("Could not find " ^ filename)
4742 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4745 let canpan () =
4746 match conf.columns with
4747 | Csplit _ -> true
4748 | _ -> conf.zoom > 1.0
4751 let existsinrow pageno (columns, coverA, coverB) p =
4752 let last = ((pageno - coverA) mod columns) + columns in
4753 let rec any = function
4754 | [] -> false
4755 | l :: rest ->
4756 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4757 then p l
4758 else (
4759 if not (p l)
4760 then (if l.pageno = last then false else any rest)
4761 else true
4764 any state.layout
4767 let nextpage () =
4768 match state.layout with
4769 | [] -> ()
4770 | l :: rest ->
4771 match conf.columns with
4772 | Csingle _ ->
4773 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4774 then
4775 let y = clamp (pgscale state.winh) in
4776 gotoghyll y
4777 else
4778 let pageno = min (l.pageno+1) (state.pagecount-1) in
4779 gotoghyll (getpagey pageno)
4780 | Cmulti ((c, _, _) as cl, _) ->
4781 if conf.presentation
4782 && (existsinrow l.pageno cl
4783 (fun l -> l.pageh > l.pagey + l.pagevh))
4784 then
4785 let y = clamp (pgscale state.winh) in
4786 gotoghyll y
4787 else
4788 let pageno = min (l.pageno+c) (state.pagecount-1) in
4789 gotoghyll (getpagey pageno)
4790 | Csplit (n, _) ->
4791 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4792 then
4793 let pagey, pageh = getpageyh l.pageno in
4794 let pagey = pagey + pageh * l.pagecol in
4795 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4796 gotoghyll (pagey + pageh + ips)
4799 let prevpage () =
4800 match state.layout with
4801 | [] -> ()
4802 | l :: _ ->
4803 match conf.columns with
4804 | Csingle _ ->
4805 if conf.presentation && l.pagey != 0
4806 then
4807 gotoghyll (clamp (pgscale ~-(state.winh)))
4808 else
4809 let pageno = max 0 (l.pageno-1) in
4810 gotoghyll (getpagey pageno)
4811 | Cmulti ((c, _, coverB) as cl, _) ->
4812 if conf.presentation &&
4813 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4814 then
4815 gotoghyll (clamp (pgscale ~-(state.winh)))
4816 else
4817 let decr =
4818 if l.pageno = state.pagecount - coverB
4819 then 1
4820 else c
4822 let pageno = max 0 (l.pageno-decr) in
4823 gotoghyll (getpagey pageno)
4824 | Csplit (n, _) ->
4825 let y =
4826 if l.pagecol = 0
4827 then
4828 if l.pageno = 0
4829 then l.pagey
4830 else
4831 let pageno = max 0 (l.pageno-1) in
4832 let pagey, pageh = getpageyh pageno in
4833 pagey + (n-1)*pageh
4834 else
4835 let pagey, pageh = getpageyh l.pageno in
4836 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4838 gotoghyll y
4841 let viewkeyboard key mask =
4842 let enttext te =
4843 let mode = state.mode in
4844 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4845 state.text <- "";
4846 enttext ();
4847 G.postRedisplay "view:enttext"
4849 let ctrl = Wsi.withctrl mask in
4850 let key =
4851 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4853 match key with
4854 | 81 -> (* Q *)
4855 exit 0
4857 | 0xff63 -> (* insert *)
4858 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4859 then (
4860 state.mode <- LinkNav (Ltgendir 0);
4861 gotoy state.y;
4863 else showtext '!' "Keyboard link navigation does not work under rotation"
4865 | 0xff1b | 113 -> (* escape / q *)
4866 begin match state.mstate with
4867 | Mzoomrect _ ->
4868 state.mstate <- Mnone;
4869 Wsi.setcursor Wsi.CURSOR_INHERIT;
4870 G.postRedisplay "kill zoom rect";
4871 | _ ->
4872 begin match state.mode with
4873 | LinkNav _ ->
4874 state.mode <- View;
4875 G.postRedisplay "esc leave linknav"
4876 | _ ->
4877 match state.ranchors with
4878 | [] -> raise Quit
4879 | (path, password, anchor) :: rest ->
4880 state.ranchors <- rest;
4881 state.anchor <- anchor;
4882 opendoc path password
4883 end;
4884 end;
4886 | 0xff08 -> (* backspace *)
4887 gotoghyll (getnav ~-1)
4889 | 111 -> (* o *)
4890 enteroutlinemode ()
4892 | 117 -> (* u *)
4893 state.rects <- [];
4894 state.text <- "";
4895 G.postRedisplay "dehighlight";
4897 | 47 | 63 -> (* / ? *)
4898 let ondone isforw s =
4899 cbput state.hists.pat s;
4900 state.searchpattern <- s;
4901 search s isforw
4903 let s = String.create 1 in
4904 s.[0] <- Char.chr key;
4905 enttext (s, "", Some (onhist state.hists.pat),
4906 textentry, ondone (key = 47), true)
4908 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4909 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4910 setzoom (conf.zoom +. incr)
4912 | 43 | 0xffab -> (* + *)
4913 let ondone s =
4914 let n =
4915 try int_of_string s with exc ->
4916 state.text <- Printf.sprintf "bad integer `%s': %s"
4917 s (Printexc.to_string exc);
4918 max_int
4920 if n != max_int
4921 then (
4922 conf.pagebias <- n;
4923 state.text <- "page bias is now " ^ string_of_int n;
4926 enttext ("page bias: ", "", None, intentry, ondone, true)
4928 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4929 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4930 setzoom (max 0.01 (conf.zoom -. decr))
4932 | 45 | 0xffad -> (* - *)
4933 let ondone msg = state.text <- msg in
4934 enttext (
4935 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4936 optentry state.mode, ondone, true
4939 | 48 when ctrl -> (* ctrl-0 *)
4940 setzoom 1.0
4942 | 49 when ctrl -> (* ctrl-1 *)
4943 let cols =
4944 match conf.columns with
4945 | Csingle _ | Cmulti _ -> 1
4946 | Csplit (n, _) -> n
4948 let zoom = zoomforh state.winw state.winh state.scrollw cols in
4949 if zoom < 1.0
4950 then setzoom zoom
4952 | 0xffc6 -> (* f9 *)
4953 togglebirdseye ()
4955 | 57 when ctrl -> (* ctrl-9 *)
4956 togglebirdseye ()
4958 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4959 when not ctrl -> (* 0..9 *)
4960 let ondone s =
4961 let n =
4962 try int_of_string s with exc ->
4963 state.text <- Printf.sprintf "bad integer `%s': %s"
4964 s (Printexc.to_string exc);
4967 if n >= 0
4968 then (
4969 addnav ();
4970 cbput state.hists.pag (string_of_int n);
4971 gotopage1 (n + conf.pagebias - 1) 0;
4974 let pageentry text key =
4975 match Char.unsafe_chr key with
4976 | 'g' -> TEdone text
4977 | _ -> intentry text key
4979 let text = "x" in text.[0] <- Char.chr key;
4980 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4982 | 98 -> (* b *)
4983 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4984 reshape state.winw state.winh;
4986 | 108 -> (* l *)
4987 conf.hlinks <- not conf.hlinks;
4988 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4989 G.postRedisplay "toggle highlightlinks";
4991 | 70 -> (* F *)
4992 state.glinks <- true;
4993 let mode = state.mode in
4994 state.mode <- Textentry (
4995 (":", "", None, linknentry, linkndone gotounder, false),
4996 (fun _ ->
4997 state.glinks <- false;
4998 state.mode <- mode)
5000 state.text <- "";
5001 G.postRedisplay "view:linkent(F)"
5003 | 121 -> (* y *)
5004 state.glinks <- true;
5005 let mode = state.mode in
5006 state.mode <- Textentry (
5007 (":", "", None, linknentry, linkndone (fun under ->
5008 match Ne.pipe () with
5009 | Ne.Exn exn ->
5010 showtext '!' (Printf.sprintf "pipe failed: %s"
5011 (Printexc.to_string exn));
5012 | Ne.Res (r, w) ->
5013 let popened =
5014 try popen conf.selcmd [r, 0; w, -1]; true
5015 with exn ->
5016 showtext '!'
5017 (Printf.sprintf "failed to execute %s: %s"
5018 conf.selcmd (Printexc.to_string exn));
5019 false
5021 let clo cap fd =
5022 Ne.clo fd (fun msg ->
5023 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5026 let s = undertext under in
5027 if popened
5028 then
5029 (try
5030 let l = String.length s in
5031 let n = tempfailureretry (Unix.write w s 0) l in
5032 if n != l
5033 then
5034 showtext '!'
5035 (Printf.sprintf
5036 "failed to write %d characters to sel pipe, wrote %d"
5039 with exn ->
5040 showtext '!'
5041 (Printf.sprintf "failed to write to sel pipe: %s"
5042 (Printexc.to_string exn)
5045 else dolog "%s" s;
5046 clo "pipe/r" r;
5047 clo "pipe/w" w;
5048 ), false
5050 fun _ ->
5051 state.glinks <- false;
5052 state.mode <- mode
5054 state.text <- "";
5055 G.postRedisplay "view:linkent"
5057 | 97 -> (* a *)
5058 begin match state.autoscroll with
5059 | Some step ->
5060 conf.autoscrollstep <- step;
5061 state.autoscroll <- None
5062 | None ->
5063 if conf.autoscrollstep = 0
5064 then state.autoscroll <- Some 1
5065 else state.autoscroll <- Some conf.autoscrollstep
5068 | 112 when ctrl -> (* ctrl-p *)
5069 launchpath ()
5071 | 80 -> (* P *)
5072 setpresentationmode (not conf.presentation);
5073 showtext ' ' ("presentation mode " ^
5074 if conf.presentation then "on" else "off");
5076 | 102 -> (* f *)
5077 if List.mem Wsi.Fullscreen state.winstate
5078 then doreshape conf.cwinw conf.cwinh
5079 else Wsi.fullscreen ()
5081 | 112 | 78 -> (* p|N *)
5082 search state.searchpattern false
5084 | 110 | 0xffc0 -> (* n|F3 *)
5085 search state.searchpattern true
5087 | 116 -> (* t *)
5088 begin match state.layout with
5089 | [] -> ()
5090 | l :: _ ->
5091 gotoghyll (getpagey l.pageno)
5094 | 32 -> (* space *)
5095 nextpage ()
5097 | 0xff9f | 0xffff -> (* delete *)
5098 prevpage ()
5100 | 61 -> (* = *)
5101 showtext ' ' (describe_location ());
5103 | 119 -> (* w *)
5104 begin match state.layout with
5105 | [] -> ()
5106 | l :: _ ->
5107 doreshape (l.pagew + state.scrollw) l.pageh;
5108 G.postRedisplay "w"
5111 | 39 -> (* ' *)
5112 enterbookmarkmode ()
5114 | 104 | 0xffbe -> (* h|F1 *)
5115 enterhelpmode ()
5117 | 105 -> (* i *)
5118 enterinfomode ()
5120 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5121 entermsgsmode ()
5123 | 109 -> (* m *)
5124 let ondone s =
5125 match state.layout with
5126 | l :: _ ->
5127 if String.length s > 0
5128 then
5129 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5130 | _ -> ()
5132 enttext ("bookmark: ", "", None, textentry, ondone, true)
5134 | 126 -> (* ~ *)
5135 quickbookmark ();
5136 showtext ' ' "Quick bookmark added";
5138 | 122 -> (* z *)
5139 begin match state.layout with
5140 | l :: _ ->
5141 let rect = getpdimrect l.pagedimno in
5142 let w, h =
5143 if conf.crophack
5144 then
5145 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5146 truncate (1.2 *. (rect.(3) -. rect.(0))))
5147 else
5148 (truncate (rect.(1) -. rect.(0)),
5149 truncate (rect.(3) -. rect.(0)))
5151 let w = truncate ((float w)*.conf.zoom)
5152 and h = truncate ((float h)*.conf.zoom) in
5153 if w != 0 && h != 0
5154 then (
5155 state.anchor <- getanchor ();
5156 doreshape (w + state.scrollw) (h + conf.interpagespace)
5158 G.postRedisplay "z";
5160 | [] -> ()
5163 | 50 when ctrl -> (* ctrl-2 *)
5164 let maxw = getmaxw () in
5165 if maxw > 0.0
5166 then setzoom (maxw /. float state.winw)
5168 | 60 | 62 -> (* < > *)
5169 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5171 | 91 | 93 -> (* [ ] *)
5172 conf.colorscale <-
5173 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5175 G.postRedisplay "brightness";
5177 | 99 when state.mode = View -> (* c *)
5178 let (c, a, b), z =
5179 match state.prevcolumns with
5180 | None -> (1, 0, 0), 1.0
5181 | Some (columns, z) ->
5182 let cab =
5183 match columns with
5184 | Csplit (c, _) -> -c, 0, 0
5185 | Cmulti ((c, a, b), _) -> c, a, b
5186 | Csingle _ -> 1, 0, 0
5188 cab, z
5190 setcolumns View c a b;
5191 setzoom z;
5193 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5194 setzoom state.prevzoom
5196 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5197 begin match state.autoscroll with
5198 | None ->
5199 begin match state.mode with
5200 | Birdseye beye -> upbirdseye 1 beye
5201 | _ ->
5202 if ctrl
5203 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5204 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5206 | Some n ->
5207 setautoscrollspeed n false
5210 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5211 begin match state.autoscroll with
5212 | None ->
5213 begin match state.mode with
5214 | Birdseye beye -> downbirdseye 1 beye
5215 | _ ->
5216 if ctrl
5217 then gotoy_and_clear_text (clamp (state.winh/2))
5218 else gotoy_and_clear_text (clamp conf.scrollstep)
5220 | Some n ->
5221 setautoscrollspeed n true
5224 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5225 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5226 if canpan ()
5227 then
5228 let dx =
5229 if ctrl
5230 then state.winw / 2
5231 else conf.hscrollstep
5233 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5234 state.x <- state.x + dx;
5235 gotoy_and_clear_text state.y
5236 else (
5237 state.text <- "";
5238 G.postRedisplay "lef/right"
5241 | 0xff55 | 0xff9a -> (* (kp) prior *)
5242 let y =
5243 if ctrl
5244 then
5245 match state.layout with
5246 | [] -> state.y
5247 | l :: _ -> state.y - l.pagey
5248 else
5249 clamp (pgscale (-state.winh))
5251 gotoghyll y
5253 | 0xff56 | 0xff9b -> (* (kp) next *)
5254 let y =
5255 if ctrl
5256 then
5257 match List.rev state.layout with
5258 | [] -> state.y
5259 | l :: _ -> getpagey l.pageno
5260 else
5261 clamp (pgscale state.winh)
5263 gotoghyll y
5265 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5266 gotoghyll 0
5267 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5268 gotoghyll (clamp state.maxy)
5270 | 0xff53 | 0xff98
5271 when Wsi.withalt mask -> (* alt-(kp) right *)
5272 gotoghyll (getnav 1)
5273 | 0xff51 | 0xff96
5274 when Wsi.withalt mask -> (* alt-(kp) left *)
5275 gotoghyll (getnav ~-1)
5277 | 114 -> (* r *)
5278 reload ()
5280 | 118 when conf.debug -> (* v *)
5281 state.rects <- [];
5282 List.iter (fun l ->
5283 match getopaque l.pageno with
5284 | None -> ()
5285 | Some opaque ->
5286 let x0, y0, x1, y1 = pagebbox opaque in
5287 let a,b = float x0, float y0 in
5288 let c,d = float x1, float y0 in
5289 let e,f = float x1, float y1 in
5290 let h,j = float x0, float y1 in
5291 let rect = (a,b,c,d,e,f,h,j) in
5292 debugrect rect;
5293 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5294 ) state.layout;
5295 G.postRedisplay "v";
5297 | _ ->
5298 vlog "huh? %s" (Wsi.keyname key)
5301 let linknavkeyboard key mask linknav =
5302 let getpage pageno =
5303 let rec loop = function
5304 | [] -> None
5305 | l :: _ when l.pageno = pageno -> Some l
5306 | _ :: rest -> loop rest
5307 in loop state.layout
5309 let doexact (pageno, n) =
5310 match getopaque pageno, getpage pageno with
5311 | Some opaque, Some l ->
5312 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5313 then
5314 let under = getlink opaque n in
5315 G.postRedisplay "link gotounder";
5316 gotounder under;
5317 state.mode <- View;
5318 else
5319 let opt, dir =
5320 match key with
5321 | 0xff50 -> (* home *)
5322 Some (findlink opaque LDfirst), -1
5324 | 0xff57 -> (* end *)
5325 Some (findlink opaque LDlast), 1
5327 | 0xff51 -> (* left *)
5328 Some (findlink opaque (LDleft n)), -1
5330 | 0xff53 -> (* right *)
5331 Some (findlink opaque (LDright n)), 1
5333 | 0xff52 -> (* up *)
5334 Some (findlink opaque (LDup n)), -1
5336 | 0xff54 -> (* down *)
5337 Some (findlink opaque (LDdown n)), 1
5339 | _ -> None, 0
5341 let pwl l dir =
5342 begin match findpwl l.pageno dir with
5343 | Pwlnotfound -> ()
5344 | Pwl pageno ->
5345 let notfound dir =
5346 state.mode <- LinkNav (Ltgendir dir);
5347 let y, h = getpageyh pageno in
5348 let y =
5349 if dir < 0
5350 then y + h - state.winh
5351 else y
5353 gotoy y
5355 begin match getopaque pageno, getpage pageno with
5356 | Some opaque, Some _ ->
5357 let link =
5358 let ld = if dir > 0 then LDfirst else LDlast in
5359 findlink opaque ld
5361 begin match link with
5362 | Lfound m ->
5363 showlinktype (getlink opaque m);
5364 state.mode <- LinkNav (Ltexact (pageno, m));
5365 G.postRedisplay "linknav jpage";
5366 | _ -> notfound dir
5367 end;
5368 | _ -> notfound dir
5369 end;
5370 end;
5372 begin match opt with
5373 | Some Lnotfound -> pwl l dir;
5374 | Some (Lfound m) ->
5375 if m = n
5376 then pwl l dir
5377 else (
5378 let _, y0, _, y1 = getlinkrect opaque m in
5379 if y0 < l.pagey
5380 then gotopage1 l.pageno y0
5381 else (
5382 let d = fstate.fontsize + 1 in
5383 if y1 - l.pagey > l.pagevh - d
5384 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5385 else G.postRedisplay "linknav";
5387 showlinktype (getlink opaque m);
5388 state.mode <- LinkNav (Ltexact (l.pageno, m));
5391 | None -> viewkeyboard key mask
5392 end;
5393 | _ -> viewkeyboard key mask
5395 if key = 0xff63
5396 then (
5397 state.mode <- View;
5398 G.postRedisplay "leave linknav"
5400 else
5401 match linknav with
5402 | Ltgendir _ -> viewkeyboard key mask
5403 | Ltexact exact -> doexact exact
5406 let keyboard key mask =
5407 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5408 then wcmd "interrupt"
5409 else state.uioh <- state.uioh#key key mask
5412 let birdseyekeyboard key mask
5413 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5414 let incr =
5415 match conf.columns with
5416 | Csingle _ -> 1
5417 | Cmulti ((c, _, _), _) -> c
5418 | Csplit _ -> failwith "bird's eye split mode"
5420 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5421 match key with
5422 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5423 let y, h = getpageyh pageno in
5424 let top = (state.winh - h) / 2 in
5425 gotoy (max 0 (y - top))
5426 | 0xff0d (* enter *)
5427 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5428 | 0xff1b -> leavebirdseye beye true (* escape *)
5429 | 0xff52 -> upbirdseye incr beye (* up *)
5430 | 0xff54 -> downbirdseye incr beye (* down *)
5431 | 0xff51 -> upbirdseye 1 beye (* left *)
5432 | 0xff53 -> downbirdseye 1 beye (* right *)
5434 | 0xff55 -> (* prior *)
5435 begin match state.layout with
5436 | l :: _ ->
5437 if l.pagey != 0
5438 then (
5439 state.mode <- Birdseye (
5440 oconf, leftx, l.pageno, hooverpageno, anchor
5442 gotopage1 l.pageno 0;
5444 else (
5445 let layout = layout (state.y-state.winh) (pgh state.layout) in
5446 match layout with
5447 | [] -> gotoy (clamp (-state.winh))
5448 | l :: _ ->
5449 state.mode <- Birdseye (
5450 oconf, leftx, l.pageno, hooverpageno, anchor
5452 gotopage1 l.pageno 0
5455 | [] -> gotoy (clamp (-state.winh))
5456 end;
5458 | 0xff56 -> (* next *)
5459 begin match List.rev state.layout with
5460 | l :: _ ->
5461 let layout = layout (state.y + (pgh state.layout)) state.winh in
5462 begin match layout with
5463 | [] ->
5464 let incr = l.pageh - l.pagevh in
5465 if incr = 0
5466 then (
5467 state.mode <-
5468 Birdseye (
5469 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5471 G.postRedisplay "birdseye pagedown";
5473 else gotoy (clamp (incr + conf.interpagespace*2));
5475 | l :: _ ->
5476 state.mode <-
5477 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5478 gotopage1 l.pageno 0;
5481 | [] -> gotoy (clamp state.winh)
5482 end;
5484 | 0xff50 -> (* home *)
5485 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5486 gotopage1 0 0
5488 | 0xff57 -> (* end *)
5489 let pageno = state.pagecount - 1 in
5490 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5491 if not (pagevisible state.layout pageno)
5492 then
5493 let h =
5494 match List.rev state.pdims with
5495 | [] -> state.winh
5496 | (_, _, h, _) :: _ -> h
5498 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5499 else G.postRedisplay "birdseye end";
5500 | _ -> viewkeyboard key mask
5503 let drawpage l linkindexbase =
5504 let color =
5505 match state.mode with
5506 | Textentry _ -> scalecolor 0.4
5507 | LinkNav _
5508 | View -> scalecolor 1.0
5509 | Birdseye (_, _, pageno, hooverpageno, _) ->
5510 if l.pageno = hooverpageno
5511 then scalecolor 0.9
5512 else (
5513 if l.pageno = pageno
5514 then scalecolor 1.0
5515 else scalecolor 0.8
5518 drawtiles l color;
5519 begin match getopaque l.pageno with
5520 | Some opaque ->
5521 if tileready l l.pagex l.pagey
5522 then
5523 let x = l.pagedispx - l.pagex
5524 and y = l.pagedispy - l.pagey in
5525 let hlmask =
5526 match conf.columns with
5527 | Csingle _ | Cmulti _ ->
5528 (if conf.hlinks then 1 else 0)
5529 + (if state.glinks
5530 && not (isbirdseye state.mode) then 2 else 0)
5531 | _ -> 0
5533 let s =
5534 match state.mode with
5535 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5536 | _ -> ""
5538 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5539 else 0
5541 | _ -> 0
5542 end;
5545 let scrollindicator () =
5546 let sbw, ph, sh = state.uioh#scrollph in
5547 let sbh, pw, sw = state.uioh#scrollpw in
5549 GlDraw.color (0.64, 0.64, 0.64);
5550 GlDraw.rect
5551 (float (state.winw - sbw), 0.)
5552 (float state.winw, float state.winh)
5554 GlDraw.rect
5555 (0., float (state.winh - sbh))
5556 (float (state.winw - state.scrollw - 1), float state.winh)
5558 GlDraw.color (0.0, 0.0, 0.0);
5560 GlDraw.rect
5561 (float (state.winw - sbw), ph)
5562 (float state.winw, ph +. sh)
5564 GlDraw.rect
5565 (pw, float (state.winh - sbh))
5566 (pw +. sw, float state.winh)
5570 let showsel () =
5571 match state.mstate with
5572 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5575 | Msel ((x0, y0), (x1, y1)) ->
5576 let rec loop = function
5577 | l :: ls ->
5578 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5579 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5580 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5581 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5582 then
5583 match getopaque l.pageno with
5584 | Some opaque ->
5585 let x0, y0 = pagetranslatepoint l x0 y0 in
5586 let x1, y1 = pagetranslatepoint l x1 y1 in
5587 seltext opaque (x0, y0, x1, y1);
5588 | _ -> ()
5589 else loop ls
5590 | [] -> ()
5592 loop state.layout
5595 let showrects rects =
5596 Gl.enable `blend;
5597 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5598 GlDraw.polygon_mode `both `fill;
5599 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5600 List.iter
5601 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5602 List.iter (fun l ->
5603 if l.pageno = pageno
5604 then (
5605 let dx = float (l.pagedispx - l.pagex) in
5606 let dy = float (l.pagedispy - l.pagey) in
5607 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5608 GlDraw.begins `quads;
5610 GlDraw.vertex2 (x0+.dx, y0+.dy);
5611 GlDraw.vertex2 (x1+.dx, y1+.dy);
5612 GlDraw.vertex2 (x2+.dx, y2+.dy);
5613 GlDraw.vertex2 (x3+.dx, y3+.dy);
5615 GlDraw.ends ();
5617 ) state.layout
5618 ) rects
5620 Gl.disable `blend;
5623 let display () =
5624 GlClear.color (scalecolor2 conf.bgcolor);
5625 GlClear.clear [`color];
5626 let rec loop linkindexbase = function
5627 | l :: rest ->
5628 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5629 loop linkindexbase rest
5630 | [] -> ()
5632 loop 0 state.layout;
5633 let rects =
5634 match state.mode with
5635 | LinkNav (Ltexact (pageno, linkno)) ->
5636 begin match getopaque pageno with
5637 | Some opaque ->
5638 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5639 (pageno, 5, (
5640 float x0, float y0,
5641 float x1, float y0,
5642 float x1, float y1,
5643 float x0, float y1)
5644 ) :: state.rects
5645 | None -> state.rects
5647 | _ -> state.rects
5649 showrects rects;
5650 showsel ();
5651 state.uioh#display;
5652 begin match state.mstate with
5653 | Mzoomrect ((x0, y0), (x1, y1)) ->
5654 Gl.enable `blend;
5655 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5656 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5657 GlDraw.rect (float x0, float y0)
5658 (float x1, float y1);
5659 Gl.disable `blend;
5660 | _ -> ()
5661 end;
5662 enttext ();
5663 scrollindicator ();
5664 Wsi.swapb ();
5667 let zoomrect x y x1 y1 =
5668 let x0 = min x x1
5669 and x1 = max x x1
5670 and y0 = min y y1 in
5671 gotoy (state.y + y0);
5672 state.anchor <- getanchor ();
5673 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5674 let margin =
5675 if state.w < state.winw - state.scrollw
5676 then (state.winw - state.scrollw - state.w) / 2
5677 else 0
5679 state.x <- (state.x + margin) - x0;
5680 setzoom zoom;
5681 Wsi.setcursor Wsi.CURSOR_INHERIT;
5682 state.mstate <- Mnone;
5685 let scrollx x =
5686 let winw = state.winw - state.scrollw - 1 in
5687 let s = float x /. float winw in
5688 let destx = truncate (float (state.w + winw) *. s) in
5689 state.x <- winw - destx;
5690 gotoy_and_clear_text state.y;
5691 state.mstate <- Mscrollx;
5694 let scrolly y =
5695 let s = float y /. float state.winh in
5696 let desty = truncate (float (state.maxy - state.winh) *. s) in
5697 gotoy_and_clear_text desty;
5698 state.mstate <- Mscrolly;
5701 let viewmouse button down x y mask =
5702 match button with
5703 | n when (n == 4 || n == 5) && not down ->
5704 if Wsi.withctrl mask
5705 then (
5706 match state.mstate with
5707 | Mzoom (oldn, i) ->
5708 if oldn = n
5709 then (
5710 if i = 2
5711 then
5712 let incr =
5713 match n with
5714 | 5 ->
5715 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5716 | _ ->
5717 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5719 let zoom = conf.zoom -. incr in
5720 setzoom zoom;
5721 state.mstate <- Mzoom (n, 0);
5722 else
5723 state.mstate <- Mzoom (n, i+1);
5725 else state.mstate <- Mzoom (n, 0)
5727 | _ -> state.mstate <- Mzoom (n, 0)
5729 else (
5730 match state.autoscroll with
5731 | Some step -> setautoscrollspeed step (n=4)
5732 | None ->
5733 if conf.wheelbypage
5734 then (
5735 if n = 4
5736 then prevpage ()
5737 else nextpage ()
5739 else
5740 let incr =
5741 if n = 4
5742 then -conf.scrollstep
5743 else conf.scrollstep
5745 let incr = incr * 2 in
5746 let y = clamp incr in
5747 gotoy_and_clear_text y
5750 | n when (n = 6 || n = 7) && not down && canpan () ->
5751 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5752 gotoy_and_clear_text state.y
5754 | 1 when Wsi.withshift mask ->
5755 state.mstate <- Mnone;
5756 if not down then (
5757 match unproject x y with
5758 | Some (pageno, ux, uy) ->
5759 let cmd = Printf.sprintf
5760 "%s %s %d %d %d"
5761 conf.stcmd state.path pageno ux uy
5763 popen cmd []
5764 | None -> ()
5767 | 1 when Wsi.withctrl mask ->
5768 if down
5769 then (
5770 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5771 state.mstate <- Mpan (x, y)
5773 else
5774 state.mstate <- Mnone
5776 | 3 ->
5777 if down
5778 then (
5779 Wsi.setcursor Wsi.CURSOR_CYCLE;
5780 let p = (x, y) in
5781 state.mstate <- Mzoomrect (p, p)
5783 else (
5784 match state.mstate with
5785 | Mzoomrect ((x0, y0), _) ->
5786 if abs (x-x0) > 10 && abs (y - y0) > 10
5787 then zoomrect x0 y0 x y
5788 else (
5789 state.mstate <- Mnone;
5790 Wsi.setcursor Wsi.CURSOR_INHERIT;
5791 G.postRedisplay "kill accidental zoom rect";
5793 | _ ->
5794 Wsi.setcursor Wsi.CURSOR_INHERIT;
5795 state.mstate <- Mnone
5798 | 1 when x > state.winw - state.scrollw ->
5799 if down
5800 then
5801 let _, position, sh = state.uioh#scrollph in
5802 if y > truncate position && y < truncate (position +. sh)
5803 then state.mstate <- Mscrolly
5804 else scrolly y
5805 else
5806 state.mstate <- Mnone
5808 | 1 when y > state.winh - state.hscrollh ->
5809 if down
5810 then
5811 let _, position, sw = state.uioh#scrollpw in
5812 if x > truncate position && x < truncate (position +. sw)
5813 then state.mstate <- Mscrollx
5814 else scrollx x
5815 else
5816 state.mstate <- Mnone
5818 | 1 ->
5819 let dest = if down then getunder x y else Unone in
5820 begin match dest with
5821 | Ulinkgoto _
5822 | Ulinkuri _
5823 | Uremote _
5824 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5825 gotounder dest
5827 | Unone when down ->
5828 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5829 state.mstate <- Mpan (x, y);
5831 | Unone | Utext _ ->
5832 if down
5833 then (
5834 if conf.angle mod 360 = 0
5835 then (
5836 state.mstate <- Msel ((x, y), (x, y));
5837 G.postRedisplay "mouse select";
5840 else (
5841 match state.mstate with
5842 | Mnone -> ()
5844 | Mzoom _ | Mscrollx | Mscrolly ->
5845 state.mstate <- Mnone
5847 | Mzoomrect ((x0, y0), _) ->
5848 zoomrect x0 y0 x y
5850 | Mpan _ ->
5851 Wsi.setcursor Wsi.CURSOR_INHERIT;
5852 state.mstate <- Mnone
5854 | Msel ((x0, y0), (x1, y1)) ->
5855 let rec loop = function
5856 | [] -> ()
5857 | l :: rest ->
5858 let inside =
5859 let a0 = l.pagedispy in
5860 let a1 = a0 + l.pagevh in
5861 let b0 = l.pagedispx in
5862 let b1 = b0 + l.pagevw in
5863 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5864 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5866 if inside
5867 then
5868 match getopaque l.pageno with
5869 | Some opaque ->
5870 begin
5871 match Ne.pipe () with
5872 | Ne.Exn exn ->
5873 showtext '!'
5874 (Printf.sprintf
5875 "can not create sel pipe: %s"
5876 (Printexc.to_string exn));
5877 | Ne.Res (r, w) ->
5878 let doclose what fd =
5879 Ne.clo fd (fun msg ->
5880 dolog "%s close failed: %s" what msg)
5883 popen conf.selcmd [r, 0; w, -1];
5884 copysel w opaque;
5885 doclose "pipe/r" r;
5886 G.postRedisplay "copysel";
5887 with exn ->
5888 dolog "can not execute %S: %s"
5889 conf.selcmd (Printexc.to_string exn);
5890 doclose "pipe/r" r;
5891 doclose "pipe/w" w;
5893 | None -> ()
5894 else loop rest
5896 loop state.layout;
5897 Wsi.setcursor Wsi.CURSOR_INHERIT;
5898 state.mstate <- Mnone;
5902 | _ -> ()
5905 let birdseyemouse button down x y mask
5906 (conf, leftx, _, hooverpageno, anchor) =
5907 match button with
5908 | 1 when down ->
5909 let rec loop = function
5910 | [] -> ()
5911 | l :: rest ->
5912 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5913 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5914 then (
5915 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5917 else loop rest
5919 loop state.layout
5920 | 3 -> ()
5921 | _ -> viewmouse button down x y mask
5924 let mouse button down x y mask =
5925 state.uioh <- state.uioh#button button down x y mask;
5928 let motion ~x ~y =
5929 state.uioh <- state.uioh#motion x y
5932 let pmotion ~x ~y =
5933 state.uioh <- state.uioh#pmotion x y;
5936 let uioh = object
5937 method display = ()
5939 method key key mask =
5940 begin match state.mode with
5941 | Textentry textentry -> textentrykeyboard key mask textentry
5942 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5943 | View -> viewkeyboard key mask
5944 | LinkNav linknav -> linknavkeyboard key mask linknav
5945 end;
5946 state.uioh
5948 method button button bstate x y mask =
5949 begin match state.mode with
5950 | LinkNav _
5951 | View -> viewmouse button bstate x y mask
5952 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5953 | Textentry _ -> ()
5954 end;
5955 state.uioh
5957 method motion x y =
5958 begin match state.mode with
5959 | Textentry _ -> ()
5960 | View | Birdseye _ | LinkNav _ ->
5961 match state.mstate with
5962 | Mzoom _ | Mnone -> ()
5964 | Mpan (x0, y0) ->
5965 let dx = x - x0
5966 and dy = y0 - y in
5967 state.mstate <- Mpan (x, y);
5968 if canpan ()
5969 then state.x <- state.x + dx;
5970 let y = clamp dy in
5971 gotoy_and_clear_text y
5973 | Msel (a, _) ->
5974 state.mstate <- Msel (a, (x, y));
5975 G.postRedisplay "motion select";
5977 | Mscrolly ->
5978 let y = min state.winh (max 0 y) in
5979 scrolly y
5981 | Mscrollx ->
5982 let x = min state.winw (max 0 x) in
5983 scrollx x
5985 | Mzoomrect (p0, _) ->
5986 state.mstate <- Mzoomrect (p0, (x, y));
5987 G.postRedisplay "motion zoomrect";
5988 end;
5989 state.uioh
5991 method pmotion x y =
5992 begin match state.mode with
5993 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5994 let rec loop = function
5995 | [] ->
5996 if hooverpageno != -1
5997 then (
5998 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5999 G.postRedisplay "pmotion birdseye no hoover";
6001 | l :: rest ->
6002 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6003 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6004 then (
6005 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6006 G.postRedisplay "pmotion birdseye hoover";
6008 else loop rest
6010 loop state.layout
6012 | Textentry _ -> ()
6014 | LinkNav _
6015 | View ->
6016 match state.mstate with
6017 | Mnone -> updateunder x y
6018 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6020 end;
6021 state.uioh
6023 method infochanged _ = ()
6025 method scrollph =
6026 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6027 let p, h = scrollph state.y maxy in
6028 state.scrollw, p, h
6030 method scrollpw =
6031 let winw = state.winw - state.scrollw - 1 in
6032 let fwinw = float winw in
6033 let sw =
6034 let sw = fwinw /. float state.w in
6035 let sw = fwinw *. sw in
6036 max sw (float conf.scrollh)
6038 let position, sw =
6039 let f = state.w+winw in
6040 let r = float (winw-state.x) /. float f in
6041 let p = fwinw *. r in
6042 p-.sw/.2., sw
6044 let sw =
6045 if position +. sw > fwinw
6046 then fwinw -. position
6047 else sw
6049 state.hscrollh, position, sw
6051 method modehash =
6052 let modename =
6053 match state.mode with
6054 | LinkNav _ -> "links"
6055 | Textentry _ -> "textentry"
6056 | Birdseye _ -> "birdseye"
6057 | View -> "view"
6059 findkeyhash conf modename
6060 end;;
6062 module Config =
6063 struct
6064 open Parser
6066 let fontpath = ref "";;
6068 module KeyMap =
6069 Map.Make (struct type t = (int * int) let compare = compare end);;
6071 let unent s =
6072 let l = String.length s in
6073 let b = Buffer.create l in
6074 unent b s 0 l;
6075 Buffer.contents b;
6078 let home =
6079 try Sys.getenv "HOME"
6080 with exn ->
6081 prerr_endline
6082 ("Can not determine home directory location: " ^
6083 Printexc.to_string exn);
6087 let modifier_of_string = function
6088 | "alt" -> Wsi.altmask
6089 | "shift" -> Wsi.shiftmask
6090 | "ctrl" | "control" -> Wsi.ctrlmask
6091 | "meta" -> Wsi.metamask
6092 | _ -> 0
6095 let key_of_string =
6096 let r = Str.regexp "-" in
6097 fun s ->
6098 let elems = Str.full_split r s in
6099 let f n k m =
6100 let g s =
6101 let m1 = modifier_of_string s in
6102 if m1 = 0
6103 then (Wsi.namekey s, m)
6104 else (k, m lor m1)
6105 in function
6106 | Str.Delim s when n land 1 = 0 -> g s
6107 | Str.Text s -> g s
6108 | Str.Delim _ -> (k, m)
6110 let rec loop n k m = function
6111 | [] -> (k, m)
6112 | x :: xs ->
6113 let k, m = f n k m x in
6114 loop (n+1) k m xs
6116 loop 0 0 0 elems
6119 let keys_of_string =
6120 let r = Str.regexp "[ \t]" in
6121 fun s ->
6122 let elems = Str.split r s in
6123 List.map key_of_string elems
6126 let copykeyhashes c =
6127 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6130 let config_of c attrs =
6131 let apply c k v =
6133 match k with
6134 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6135 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6136 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6137 | "preload" -> { c with preload = bool_of_string v }
6138 | "page-bias" -> { c with pagebias = int_of_string v }
6139 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6140 | "horizontal-scroll-step" ->
6141 { c with hscrollstep = max (int_of_string v) 1 }
6142 | "auto-scroll-step" ->
6143 { c with autoscrollstep = max 0 (int_of_string v) }
6144 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6145 | "crop-hack" -> { c with crophack = bool_of_string v }
6146 | "throttle" ->
6147 let mw =
6148 match String.lowercase v with
6149 | "true" -> Some infinity
6150 | "false" -> None
6151 | f -> Some (float_of_string f)
6153 { c with maxwait = mw}
6154 | "highlight-links" -> { c with hlinks = bool_of_string v }
6155 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6156 | "vertical-margin" ->
6157 { c with interpagespace = max 0 (int_of_string v) }
6158 | "zoom" ->
6159 let zoom = float_of_string v /. 100. in
6160 let zoom = max zoom 0.0 in
6161 { c with zoom = zoom }
6162 | "presentation" -> { c with presentation = bool_of_string v }
6163 | "rotation-angle" -> { c with angle = int_of_string v }
6164 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6165 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6166 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6167 | "proportional-display" -> { c with proportional = bool_of_string v }
6168 | "pixmap-cache-size" ->
6169 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6170 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6171 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6172 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6173 | "persistent-location" -> { c with jumpback = bool_of_string v }
6174 | "background-color" -> { c with bgcolor = color_of_string v }
6175 | "scrollbar-in-presentation" ->
6176 { c with scrollbarinpm = bool_of_string v }
6177 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6178 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6179 | "mupdf-store-size" ->
6180 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6181 | "checkers" -> { c with checkers = bool_of_string v }
6182 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6183 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6184 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6185 | "uri-launcher" -> { c with urilauncher = unent v }
6186 | "path-launcher" -> { c with pathlauncher = unent v }
6187 | "color-space" -> { c with colorspace = colorspace_of_string v }
6188 | "invert-colors" -> { c with invert = bool_of_string v }
6189 | "brightness" -> { c with colorscale = float_of_string v }
6190 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6191 | "ghyllscroll" ->
6192 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6193 | "columns" ->
6194 let (n, _, _) as nab = multicolumns_of_string v in
6195 if n < 0
6196 then { c with columns = Csplit (-n, [||]) }
6197 else { c with columns = Cmulti (nab, [||]) }
6198 | "birds-eye-columns" ->
6199 { c with beyecolumns = Some (max (int_of_string v) 2) }
6200 | "selection-command" -> { c with selcmd = unent v }
6201 | "synctex-command" -> { c with stcmd = unent v }
6202 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6203 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6204 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6205 | "use-pbo" -> { c with usepbo = bool_of_string v }
6206 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6207 | _ -> c
6208 with exn ->
6209 prerr_endline ("Error processing attribute (`" ^
6210 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6213 let rec fold c = function
6214 | [] -> c
6215 | (k, v) :: rest ->
6216 let c = apply c k v in
6217 fold c rest
6219 fold { c with keyhashes = copykeyhashes c } attrs;
6222 let fromstring f pos n v d =
6223 try f v
6224 with exn ->
6225 dolog "Error processing attribute (%S=%S) at %d\n%s"
6226 n v pos (Printexc.to_string exn)
6231 let bookmark_of attrs =
6232 let rec fold title page rely visy = function
6233 | ("title", v) :: rest -> fold v page rely visy rest
6234 | ("page", v) :: rest -> fold title v rely visy rest
6235 | ("rely", v) :: rest -> fold title page v visy rest
6236 | ("visy", v) :: rest -> fold title page rely v rest
6237 | _ :: rest -> fold title page rely visy rest
6238 | [] -> title, page, rely, visy
6240 fold "invalid" "0" "0" "0" attrs
6243 let doc_of attrs =
6244 let rec fold path page rely pan visy = function
6245 | ("path", v) :: rest -> fold v page rely pan visy rest
6246 | ("page", v) :: rest -> fold path v rely pan visy rest
6247 | ("rely", v) :: rest -> fold path page v pan visy rest
6248 | ("pan", v) :: rest -> fold path page rely v visy rest
6249 | ("visy", v) :: rest -> fold path page rely pan v rest
6250 | _ :: rest -> fold path page rely pan visy rest
6251 | [] -> path, page, rely, pan, visy
6253 fold "" "0" "0" "0" "0" attrs
6256 let map_of attrs =
6257 let rec fold rs ls = function
6258 | ("out", v) :: rest -> fold v ls rest
6259 | ("in", v) :: rest -> fold rs v rest
6260 | _ :: rest -> fold ls rs rest
6261 | [] -> ls, rs
6263 fold "" "" attrs
6266 let setconf dst src =
6267 dst.scrollbw <- src.scrollbw;
6268 dst.scrollh <- src.scrollh;
6269 dst.icase <- src.icase;
6270 dst.preload <- src.preload;
6271 dst.pagebias <- src.pagebias;
6272 dst.verbose <- src.verbose;
6273 dst.scrollstep <- src.scrollstep;
6274 dst.maxhfit <- src.maxhfit;
6275 dst.crophack <- src.crophack;
6276 dst.autoscrollstep <- src.autoscrollstep;
6277 dst.maxwait <- src.maxwait;
6278 dst.hlinks <- src.hlinks;
6279 dst.underinfo <- src.underinfo;
6280 dst.interpagespace <- src.interpagespace;
6281 dst.zoom <- src.zoom;
6282 dst.presentation <- src.presentation;
6283 dst.angle <- src.angle;
6284 dst.cwinw <- src.cwinw;
6285 dst.cwinh <- src.cwinh;
6286 dst.savebmarks <- src.savebmarks;
6287 dst.memlimit <- src.memlimit;
6288 dst.proportional <- src.proportional;
6289 dst.texcount <- src.texcount;
6290 dst.sliceheight <- src.sliceheight;
6291 dst.thumbw <- src.thumbw;
6292 dst.jumpback <- src.jumpback;
6293 dst.bgcolor <- src.bgcolor;
6294 dst.scrollbarinpm <- src.scrollbarinpm;
6295 dst.tilew <- src.tilew;
6296 dst.tileh <- src.tileh;
6297 dst.mustoresize <- src.mustoresize;
6298 dst.checkers <- src.checkers;
6299 dst.aalevel <- src.aalevel;
6300 dst.trimmargins <- src.trimmargins;
6301 dst.trimfuzz <- src.trimfuzz;
6302 dst.urilauncher <- src.urilauncher;
6303 dst.colorspace <- src.colorspace;
6304 dst.invert <- src.invert;
6305 dst.colorscale <- src.colorscale;
6306 dst.redirectstderr <- src.redirectstderr;
6307 dst.ghyllscroll <- src.ghyllscroll;
6308 dst.columns <- src.columns;
6309 dst.beyecolumns <- src.beyecolumns;
6310 dst.selcmd <- src.selcmd;
6311 dst.updatecurs <- src.updatecurs;
6312 dst.pathlauncher <- src.pathlauncher;
6313 dst.keyhashes <- copykeyhashes src;
6314 dst.hfsize <- src.hfsize;
6315 dst.hscrollstep <- src.hscrollstep;
6316 dst.pgscale <- src.pgscale;
6317 dst.usepbo <- src.usepbo;
6318 dst.wheelbypage <- src.wheelbypage;
6319 dst.stcmd <- src.stcmd;
6322 let get s =
6323 let h = Hashtbl.create 10 in
6324 let dc = { defconf with angle = defconf.angle } in
6325 let rec toplevel v t spos _ =
6326 match t with
6327 | Vdata | Vcdata | Vend -> v
6328 | Vopen ("llppconfig", _, closed) ->
6329 if closed
6330 then v
6331 else { v with f = llppconfig }
6332 | Vopen _ ->
6333 error "unexpected subelement at top level" s spos
6334 | Vclose _ -> error "unexpected close at top level" s spos
6336 and llppconfig v t spos _ =
6337 match t with
6338 | Vdata | Vcdata -> v
6339 | Vend -> error "unexpected end of input in llppconfig" s spos
6340 | Vopen ("defaults", attrs, closed) ->
6341 let c = config_of dc attrs in
6342 setconf dc c;
6343 if closed
6344 then v
6345 else { v with f = defaults }
6347 | Vopen ("ui-font", attrs, closed) ->
6348 let rec getsize size = function
6349 | [] -> size
6350 | ("size", v) :: rest ->
6351 let size =
6352 fromstring int_of_string spos "size" v fstate.fontsize in
6353 getsize size rest
6354 | l -> getsize size l
6356 fstate.fontsize <- getsize fstate.fontsize attrs;
6357 if closed
6358 then v
6359 else { v with f = uifont (Buffer.create 10) }
6361 | Vopen ("doc", attrs, closed) ->
6362 let pathent, spage, srely, span, svisy = doc_of attrs in
6363 let path = unent pathent
6364 and pageno = fromstring int_of_string spos "page" spage 0
6365 and rely = fromstring float_of_string spos "rely" srely 0.0
6366 and pan = fromstring int_of_string spos "pan" span 0
6367 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6368 let c = config_of dc attrs in
6369 let anchor = (pageno, rely, visy) in
6370 if closed
6371 then (Hashtbl.add h path (c, [], pan, anchor); v)
6372 else { v with f = doc path pan anchor c [] }
6374 | Vopen _ ->
6375 error "unexpected subelement in llppconfig" s spos
6377 | Vclose "llppconfig" -> { v with f = toplevel }
6378 | Vclose _ -> error "unexpected close in llppconfig" s spos
6380 and defaults v t spos _ =
6381 match t with
6382 | Vdata | Vcdata -> v
6383 | Vend -> error "unexpected end of input in defaults" s spos
6384 | Vopen ("keymap", attrs, closed) ->
6385 let modename =
6386 try List.assoc "mode" attrs
6387 with Not_found -> "global" in
6388 if closed
6389 then v
6390 else
6391 let ret keymap =
6392 let h = findkeyhash dc modename in
6393 KeyMap.iter (Hashtbl.replace h) keymap;
6394 defaults
6396 { v with f = pkeymap ret KeyMap.empty }
6398 | Vopen (_, _, _) ->
6399 error "unexpected subelement in defaults" s spos
6401 | Vclose "defaults" ->
6402 { v with f = llppconfig }
6404 | Vclose _ -> error "unexpected close in defaults" s spos
6406 and uifont b v t spos epos =
6407 match t with
6408 | Vdata | Vcdata ->
6409 Buffer.add_substring b s spos (epos - spos);
6411 | Vopen (_, _, _) ->
6412 error "unexpected subelement in ui-font" s spos
6413 | Vclose "ui-font" ->
6414 if String.length !fontpath = 0
6415 then fontpath := Buffer.contents b;
6416 { v with f = llppconfig }
6417 | Vclose _ -> error "unexpected close in ui-font" s spos
6418 | Vend -> error "unexpected end of input in ui-font" s spos
6420 and doc path pan anchor c bookmarks v t spos _ =
6421 match t with
6422 | Vdata | Vcdata -> v
6423 | Vend -> error "unexpected end of input in doc" s spos
6424 | Vopen ("bookmarks", _, closed) ->
6425 if closed
6426 then v
6427 else { v with f = pbookmarks path pan anchor c bookmarks }
6429 | Vopen ("keymap", attrs, closed) ->
6430 let modename =
6431 try List.assoc "mode" attrs
6432 with Not_found -> "global"
6434 if closed
6435 then v
6436 else
6437 let ret keymap =
6438 let h = findkeyhash c modename in
6439 KeyMap.iter (Hashtbl.replace h) keymap;
6440 doc path pan anchor c bookmarks
6442 { v with f = pkeymap ret KeyMap.empty }
6444 | Vopen (_, _, _) ->
6445 error "unexpected subelement in doc" s spos
6447 | Vclose "doc" ->
6448 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6449 { v with f = llppconfig }
6451 | Vclose _ -> error "unexpected close in doc" s spos
6453 and pkeymap ret keymap v t spos _ =
6454 match t with
6455 | Vdata | Vcdata -> v
6456 | Vend -> error "unexpected end of input in keymap" s spos
6457 | Vopen ("map", attrs, closed) ->
6458 let r, l = map_of attrs in
6459 let kss = fromstring keys_of_string spos "in" r [] in
6460 let lss = fromstring keys_of_string spos "out" l [] in
6461 let keymap =
6462 match kss with
6463 | [] -> keymap
6464 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6465 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6467 if closed
6468 then { v with f = pkeymap ret keymap }
6469 else
6470 let f () = v in
6471 { v with f = skip "map" f }
6473 | Vopen _ ->
6474 error "unexpected subelement in keymap" s spos
6476 | Vclose "keymap" ->
6477 { v with f = ret keymap }
6479 | Vclose _ -> error "unexpected close in keymap" s spos
6481 and pbookmarks path pan anchor c bookmarks v t spos _ =
6482 match t with
6483 | Vdata | Vcdata -> v
6484 | Vend -> error "unexpected end of input in bookmarks" s spos
6485 | Vopen ("item", attrs, closed) ->
6486 let titleent, spage, srely, svisy = bookmark_of attrs in
6487 let page = fromstring int_of_string spos "page" spage 0
6488 and rely = fromstring float_of_string spos "rely" srely 0.0
6489 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6490 let bookmarks =
6491 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6493 if closed
6494 then { v with f = pbookmarks path pan anchor c bookmarks }
6495 else
6496 let f () = v in
6497 { v with f = skip "item" f }
6499 | Vopen _ ->
6500 error "unexpected subelement in bookmarks" s spos
6502 | Vclose "bookmarks" ->
6503 { v with f = doc path pan anchor c bookmarks }
6505 | Vclose _ -> error "unexpected close in bookmarks" s spos
6507 and skip tag f v t spos _ =
6508 match t with
6509 | Vdata | Vcdata -> v
6510 | Vend ->
6511 error ("unexpected end of input in skipped " ^ tag) s spos
6512 | Vopen (tag', _, closed) ->
6513 if closed
6514 then v
6515 else
6516 let f' () = { v with f = skip tag f } in
6517 { v with f = skip tag' f' }
6518 | Vclose ctag ->
6519 if tag = ctag
6520 then f ()
6521 else error ("unexpected close in skipped " ^ tag) s spos
6524 parse { f = toplevel; accu = () } s;
6525 h, dc;
6528 let do_load f ic =
6530 let len = in_channel_length ic in
6531 let s = String.create len in
6532 really_input ic s 0 len;
6533 f s;
6534 with
6535 | Parse_error (msg, s, pos) ->
6536 let subs = subs s pos in
6537 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6538 failwith ("parse error: " ^ s)
6540 | exn ->
6541 failwith ("config load error: " ^ Printexc.to_string exn)
6544 let defconfpath =
6545 let dir =
6547 let dir = Filename.concat home ".config" in
6548 if Sys.is_directory dir then dir else home
6549 with _ -> home
6551 Filename.concat dir "llpp.conf"
6554 let confpath = ref defconfpath;;
6556 let load1 f =
6557 if Sys.file_exists !confpath
6558 then
6559 match
6560 (try Some (open_in_bin !confpath)
6561 with exn ->
6562 prerr_endline
6563 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6564 Printexc.to_string exn);
6565 None
6567 with
6568 | Some ic ->
6569 let success =
6571 f (do_load get ic)
6572 with exn ->
6573 prerr_endline
6574 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6575 Printexc.to_string exn);
6576 false
6578 close_in ic;
6579 success
6581 | None -> false
6582 else
6583 f (Hashtbl.create 0, defconf)
6586 let load () =
6587 let f (h, dc) =
6588 let pc, pb, px, pa =
6590 Hashtbl.find h (Filename.basename state.path)
6591 with Not_found -> dc, [], 0, emptyanchor
6593 setconf defconf dc;
6594 setconf conf pc;
6595 state.bookmarks <- pb;
6596 state.x <- px;
6597 state.scrollw <- conf.scrollbw;
6598 if conf.jumpback
6599 then state.anchor <- pa;
6600 cbput state.hists.nav pa;
6601 true
6603 load1 f
6606 let add_attrs bb always dc c =
6607 let ob s a b =
6608 if always || a != b
6609 then Printf.bprintf bb "\n %s='%b'" s a
6610 and oi s a b =
6611 if always || a != b
6612 then Printf.bprintf bb "\n %s='%d'" s a
6613 and oI s a b =
6614 if always || a != b
6615 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6616 and oz s a b =
6617 if always || a <> b
6618 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6619 and oF s a b =
6620 if always || a <> b
6621 then Printf.bprintf bb "\n %s='%f'" s a
6622 and oc s a b =
6623 if always || a <> b
6624 then
6625 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6626 and oC s a b =
6627 if always || a <> b
6628 then
6629 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6630 and oR s a b =
6631 if always || a <> b
6632 then
6633 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6634 and os s a b =
6635 if always || a <> b
6636 then
6637 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6638 and og s a b =
6639 if always || a <> b
6640 then
6641 match a with
6642 | None -> ()
6643 | Some (_N, _A, _B) ->
6644 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6645 and oW s a b =
6646 if always || a <> b
6647 then
6648 let v =
6649 match a with
6650 | None -> "false"
6651 | Some f ->
6652 if f = infinity
6653 then "true"
6654 else string_of_float f
6656 Printf.bprintf bb "\n %s='%s'" s v
6657 and oco s a b =
6658 if always || a <> b
6659 then
6660 match a with
6661 | Cmulti ((n, a, b), _) when n > 1 ->
6662 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6663 | Csplit (n, _) when n > 1 ->
6664 Printf.bprintf bb "\n %s='%d'" s ~-n
6665 | _ -> ()
6666 and obeco s a b =
6667 if always || a <> b
6668 then
6669 match a with
6670 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6671 | _ -> ()
6673 oi "width" c.cwinw dc.cwinw;
6674 oi "height" c.cwinh dc.cwinh;
6675 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6676 oi "scroll-handle-height" c.scrollh dc.scrollh;
6677 ob "case-insensitive-search" c.icase dc.icase;
6678 ob "preload" c.preload dc.preload;
6679 oi "page-bias" c.pagebias dc.pagebias;
6680 oi "scroll-step" c.scrollstep dc.scrollstep;
6681 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6682 ob "max-height-fit" c.maxhfit dc.maxhfit;
6683 ob "crop-hack" c.crophack dc.crophack;
6684 oW "throttle" c.maxwait dc.maxwait;
6685 ob "highlight-links" c.hlinks dc.hlinks;
6686 ob "under-cursor-info" c.underinfo dc.underinfo;
6687 oi "vertical-margin" c.interpagespace dc.interpagespace;
6688 oz "zoom" c.zoom dc.zoom;
6689 ob "presentation" c.presentation dc.presentation;
6690 oi "rotation-angle" c.angle dc.angle;
6691 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6692 ob "proportional-display" c.proportional dc.proportional;
6693 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6694 oi "tex-count" c.texcount dc.texcount;
6695 oi "slice-height" c.sliceheight dc.sliceheight;
6696 oi "thumbnail-width" c.thumbw dc.thumbw;
6697 ob "persistent-location" c.jumpback dc.jumpback;
6698 oc "background-color" c.bgcolor dc.bgcolor;
6699 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6700 oi "tile-width" c.tilew dc.tilew;
6701 oi "tile-height" c.tileh dc.tileh;
6702 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6703 ob "checkers" c.checkers dc.checkers;
6704 oi "aalevel" c.aalevel dc.aalevel;
6705 ob "trim-margins" c.trimmargins dc.trimmargins;
6706 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6707 os "uri-launcher" c.urilauncher dc.urilauncher;
6708 os "path-launcher" c.pathlauncher dc.pathlauncher;
6709 oC "color-space" c.colorspace dc.colorspace;
6710 ob "invert-colors" c.invert dc.invert;
6711 oF "brightness" c.colorscale dc.colorscale;
6712 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6713 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6714 oco "columns" c.columns dc.columns;
6715 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6716 os "selection-command" c.selcmd dc.selcmd;
6717 os "synctex-command" c.stcmd dc.stcmd;
6718 ob "update-cursor" c.updatecurs dc.updatecurs;
6719 oi "hint-font-size" c.hfsize dc.hfsize;
6720 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6721 oF "page-scroll-scale" c.pgscale dc.pgscale;
6722 ob "use-pbo" c.usepbo dc.usepbo;
6723 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6726 let keymapsbuf always dc c =
6727 let bb = Buffer.create 16 in
6728 let rec loop = function
6729 | [] -> ()
6730 | (modename, h) :: rest ->
6731 let dh = findkeyhash dc modename in
6732 if always || h <> dh
6733 then (
6734 if Hashtbl.length h > 0
6735 then (
6736 if Buffer.length bb > 0
6737 then Buffer.add_char bb '\n';
6738 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6739 Hashtbl.iter (fun i o ->
6740 let isdifferent = always ||
6742 let dO = Hashtbl.find dh i in
6743 dO <> o
6744 with Not_found -> true
6746 if isdifferent
6747 then
6748 let addkm (k, m) =
6749 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6750 if Wsi.withalt m then Buffer.add_string bb "alt-";
6751 if Wsi.withshift m then Buffer.add_string bb "shift-";
6752 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6753 Buffer.add_string bb (Wsi.keyname k);
6755 let addkms l =
6756 let rec loop = function
6757 | [] -> ()
6758 | km :: [] -> addkm km
6759 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6761 loop l
6763 Buffer.add_string bb "<map in='";
6764 addkm i;
6765 match o with
6766 | KMinsrt km ->
6767 Buffer.add_string bb "' out='";
6768 addkm km;
6769 Buffer.add_string bb "'/>\n"
6771 | KMinsrl kms ->
6772 Buffer.add_string bb "' out='";
6773 addkms kms;
6774 Buffer.add_string bb "'/>\n"
6776 | KMmulti (ins, kms) ->
6777 Buffer.add_char bb ' ';
6778 addkms ins;
6779 Buffer.add_string bb "' out='";
6780 addkms kms;
6781 Buffer.add_string bb "'/>\n"
6782 ) h;
6783 Buffer.add_string bb "</keymap>";
6786 loop rest
6788 loop c.keyhashes;
6792 let save () =
6793 let uifontsize = fstate.fontsize in
6794 let bb = Buffer.create 32768 in
6795 let w, h =
6796 List.fold_left
6797 (fun (w, h) ws ->
6798 match ws with
6799 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6800 | Wsi.MaxVert -> (w, conf.cwinh)
6801 | Wsi.MaxHorz -> (conf.cwinw, h)
6803 (state.winw, state.winh) state.winstate
6805 conf.cwinw <- w;
6806 conf.cwinh <- h;
6807 let f (h, dc) =
6808 let dc = if conf.bedefault then conf else dc in
6809 Buffer.add_string bb "<llppconfig>\n";
6811 if String.length !fontpath > 0
6812 then
6813 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6814 uifontsize
6815 !fontpath
6816 else (
6817 if uifontsize <> 14
6818 then
6819 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6822 Buffer.add_string bb "<defaults ";
6823 add_attrs bb true dc dc;
6824 let kb = keymapsbuf true dc dc in
6825 if Buffer.length kb > 0
6826 then (
6827 Buffer.add_string bb ">\n";
6828 Buffer.add_buffer bb kb;
6829 Buffer.add_string bb "\n</defaults>\n";
6831 else Buffer.add_string bb "/>\n";
6833 let adddoc path pan anchor c bookmarks =
6834 if bookmarks == [] && c = dc && anchor = emptyanchor
6835 then ()
6836 else (
6837 Printf.bprintf bb "<doc path='%s'"
6838 (enent path 0 (String.length path));
6840 if anchor <> emptyanchor
6841 then (
6842 let n, rely, visy = anchor in
6843 Printf.bprintf bb " page='%d'" n;
6844 if rely > 1e-6
6845 then
6846 Printf.bprintf bb " rely='%f'" rely
6848 if abs_float visy > 1e-6
6849 then
6850 Printf.bprintf bb " visy='%f'" visy
6854 if pan != 0
6855 then Printf.bprintf bb " pan='%d'" pan;
6857 add_attrs bb false dc c;
6858 let kb = keymapsbuf false dc c in
6860 begin match bookmarks with
6861 | [] ->
6862 if Buffer.length kb > 0
6863 then (
6864 Buffer.add_string bb ">\n";
6865 Buffer.add_buffer bb kb;
6866 Buffer.add_string bb "\n</doc>\n";
6868 else Buffer.add_string bb "/>\n"
6869 | _ ->
6870 Buffer.add_string bb ">\n<bookmarks>\n";
6871 List.iter (fun (title, _level, (page, rely, visy)) ->
6872 Printf.bprintf bb
6873 "<item title='%s' page='%d'"
6874 (enent title 0 (String.length title))
6875 page
6877 if rely > 1e-6
6878 then
6879 Printf.bprintf bb " rely='%f'" rely
6881 if abs_float visy > 1e-6
6882 then
6883 Printf.bprintf bb " visy='%f'" visy
6885 Buffer.add_string bb "/>\n";
6886 ) bookmarks;
6887 Buffer.add_string bb "</bookmarks>";
6888 if Buffer.length kb > 0
6889 then (
6890 Buffer.add_string bb "\n";
6891 Buffer.add_buffer bb kb;
6893 Buffer.add_string bb "\n</doc>\n";
6894 end;
6898 let pan, conf =
6899 match state.mode with
6900 | Birdseye (c, pan, _, _, _) ->
6901 let beyecolumns =
6902 match conf.columns with
6903 | Cmulti ((c, _, _), _) -> Some c
6904 | Csingle _ -> None
6905 | Csplit _ -> None
6906 and columns =
6907 match c.columns with
6908 | Cmulti (c, _) -> Cmulti (c, [||])
6909 | Csingle _ -> Csingle [||]
6910 | Csplit _ -> failwith "quit from bird's eye while split"
6912 pan, { c with beyecolumns = beyecolumns; columns = columns }
6913 | _ -> state.x, conf
6915 let basename = Filename.basename state.path in
6916 adddoc basename pan (getanchor ())
6917 (let conf =
6918 let autoscrollstep =
6919 match state.autoscroll with
6920 | Some step -> step
6921 | None -> conf.autoscrollstep
6923 match state.mode with
6924 | Birdseye (bc, _, _, _, _) ->
6925 { conf with
6926 zoom = bc.zoom;
6927 presentation = bc.presentation;
6928 interpagespace = bc.interpagespace;
6929 maxwait = bc.maxwait;
6930 autoscrollstep = autoscrollstep }
6931 | _ -> { conf with autoscrollstep = autoscrollstep }
6932 in conf)
6933 (if conf.savebmarks then state.bookmarks else []);
6935 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6936 if basename <> path
6937 then adddoc path x anchor c bookmarks
6938 ) h;
6939 Buffer.add_string bb "</llppconfig>\n";
6940 true;
6942 if load1 f && Buffer.length bb > 0
6943 then
6945 let tmp = !confpath ^ ".tmp" in
6946 let oc = open_out_bin tmp in
6947 Buffer.output_buffer oc bb;
6948 close_out oc;
6949 Unix.rename tmp !confpath;
6950 with exn ->
6951 prerr_endline
6952 ("error while saving configuration: " ^ Printexc.to_string exn)
6954 end;;
6956 let adderrmsg src msg =
6957 Buffer.add_string state.errmsgs msg;
6958 state.newerrmsgs <- true;
6959 G.postRedisplay src
6962 let adderrfmt src fmt =
6963 Format.kprintf (fun s -> adderrmsg src s) fmt;
6966 let onpagerect pageno f =
6967 let b =
6968 match conf.columns with
6969 | Cmulti (_, b) -> b
6970 | Csingle b -> b
6971 | Csplit (_, b) -> b
6973 if pageno >= 0 && pageno < Array.length b
6974 then
6975 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
6976 let r = getpdimrect pdimno in
6977 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
6980 let ract cmds =
6981 let cl = splitatspace cmds in
6982 let scan s fmt f =
6983 try Scanf.sscanf s fmt f
6984 with exn ->
6985 adderrfmt "remote exec"
6986 "error processing '%S': %s\n" cmds (Printexc.to_string exn)
6988 match cl with
6989 | "reload" :: [] -> reload ()
6990 | "goto" :: args :: [] ->
6991 scan args "%u %f %f"
6992 (fun pageno _ y ->
6993 onpagerect pageno (fun _ h ->
6994 let top = y /. h in
6995 gotopage pageno top;
6998 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6999 | "rect" :: args :: [] ->
7000 scan args "%u %u %f %f %f %f"
7001 (fun pageno color x0 y0 x1 y1 ->
7002 onpagerect pageno (fun w h ->
7003 let _,w1,h1,_ = getpagedim pageno in
7004 let sw = float w1 /. w
7005 and sh = float h1 /. h in
7006 let x0s = x0 *. sw
7007 and x1s = x1 *. sw
7008 and y0s = y0 *. sh
7009 and y1s = y1 *. sh in
7010 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7011 debugrect rect;
7012 state.rects <- (pageno, color, rect) :: state.rects;
7013 G.postRedisplay "rect";
7016 | "activatewin" :: [] -> Wsi.activatewin ()
7017 | "quit" :: [] -> raise Quit
7018 | _ ->
7019 adderrfmt "remote command"
7020 "error processing remote command: %S\n" cmds;
7023 let remote =
7024 let scratch = String.create 80 in
7025 let buf = Buffer.create 80 in
7026 fun fd ->
7027 let rec tempfr () =
7028 try Some (Unix.read fd scratch 0 80)
7029 with
7030 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7031 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7032 | exn -> raise exn
7034 match tempfr () with
7035 | None -> Some fd
7036 | Some n ->
7037 if n = 0
7038 then (
7039 Unix.close fd;
7040 if Buffer.length buf > 0
7041 then (
7042 let s = Buffer.contents buf in
7043 Buffer.clear buf;
7044 ract s;
7046 None
7048 else
7049 let rec eat ppos =
7050 let nlpos =
7052 let pos = String.index_from scratch ppos '\n' in
7053 if pos >= n then -1 else pos
7054 with Not_found -> -1
7056 if nlpos >= 0
7057 then (
7058 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7059 let s = Buffer.contents buf in
7060 Buffer.clear buf;
7061 ract s;
7062 eat (nlpos+1);
7064 else (
7065 Buffer.add_substring buf scratch ppos (n-ppos);
7066 Some fd
7068 in eat 0
7071 let remoteopen path =
7072 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7073 with exn ->
7074 adderrfmt "remoteopen" "error opening %S: %s" path (Printexc.to_string exn);
7075 None
7078 let () =
7079 let trimcachepath = ref "" in
7080 let rcmdpath = ref "" in
7081 Arg.parse
7082 (Arg.align
7083 [("-p", Arg.String (fun s -> state.password <- s) ,
7084 "<password> Set password");
7086 ("-f", Arg.String (fun s -> Config.fontpath := s),
7087 "<path> Set path to the user interface font");
7089 ("-c", Arg.String (fun s -> Config.confpath := s),
7090 "<path> Set path to the configuration file");
7092 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7093 "<path> Set path to the trim cache file");
7095 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7096 "<named-destination> Set named destination");
7098 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7100 ("-remote", Arg.String (fun s -> rcmdpath := s),
7101 "<path> Set path to the remote commands source");
7103 ("-v", Arg.Unit (fun () ->
7104 Printf.printf
7105 "%s\nconfiguration path: %s\n"
7106 (version ())
7107 Config.defconfpath
7109 exit 0), " Print version and exit");
7112 (fun s -> state.path <- s)
7113 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7115 if String.length state.path = 0
7116 then (prerr_endline "file name missing"; exit 1);
7118 if not (Config.load ())
7119 then prerr_endline "failed to load configuration";
7121 let globalkeyhash = findkeyhash conf "global" in
7122 let wsfd, winw, winh = Wsi.init (object
7123 method expose =
7124 state.wthack <- false;
7125 if nogeomcmds state.geomcmds || platform == Posx
7126 then display ()
7127 else (
7128 GlClear.color (scalecolor2 conf.bgcolor);
7129 GlClear.clear [`color];
7131 method display = display ()
7132 method reshape w h = reshape w h
7133 method mouse b d x y m = mouse b d x y m
7134 method motion x y = state.mpos <- (x, y); motion x y
7135 method pmotion x y = state.mpos <- (x, y); pmotion x y
7136 method key k m =
7137 let mascm = m land (
7138 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7139 ) in
7140 match state.keystate with
7141 | KSnone ->
7142 let km = k, mascm in
7143 begin
7144 match
7145 let modehash = state.uioh#modehash in
7146 try Hashtbl.find modehash km
7147 with Not_found ->
7148 try Hashtbl.find globalkeyhash km
7149 with Not_found -> KMinsrt (k, m)
7150 with
7151 | KMinsrt (k, m) -> keyboard k m
7152 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7153 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7155 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7156 List.iter (fun (k, m) -> keyboard k m) insrt;
7157 state.keystate <- KSnone
7158 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7159 state.keystate <- KSinto (keys, insrt)
7160 | _ ->
7161 state.keystate <- KSnone
7163 method enter x y = state.mpos <- (x, y); pmotion x y
7164 method leave = state.mpos <- (-1, -1)
7165 method winstate wsl = state.winstate <- wsl
7166 method quit = raise Quit
7167 end) conf.cwinw conf.cwinh (platform = Posx) in
7169 state.wsfd <- wsfd;
7171 if not (
7172 List.exists GlMisc.check_extension
7173 [ "GL_ARB_texture_rectangle"
7174 ; "GL_EXT_texture_recangle"
7175 ; "GL_NV_texture_rectangle" ]
7177 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7179 let cr, sw =
7180 match Ne.pipe () with
7181 | Ne.Exn exn ->
7182 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
7183 exit 1
7184 | Ne.Res rw -> rw
7185 and sr, cw =
7186 match Ne.pipe () with
7187 | Ne.Exn exn ->
7188 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7189 exit 1
7190 | Ne.Res rw -> rw
7193 cloexec cr;
7194 cloexec sw;
7195 cloexec sr;
7196 cloexec cw;
7198 setcheckers conf.checkers;
7199 redirectstderr ();
7201 init (cr, cw) (
7202 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7203 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7204 !Config.fontpath, !trimcachepath,
7205 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7207 state.sr <- sr;
7208 state.sw <- sw;
7209 state.text <- "Opening " ^ (mbtoutf8 state.path);
7210 reshape winw winh;
7211 opendoc state.path state.password;
7212 state.uioh <- uioh;
7214 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7215 let optrfd =
7216 ref (
7217 if String.length !rcmdpath > 0
7218 then remoteopen !rcmdpath
7219 else None
7223 let rec loop deadline =
7224 let r =
7225 match state.errfd with
7226 | None -> [state.sr; state.wsfd]
7227 | Some fd -> [state.sr; state.wsfd; fd]
7229 let r =
7230 match !optrfd with
7231 | None -> r
7232 | Some fd -> fd :: r
7234 if state.redisplay && not state.wthack
7235 then (
7236 state.redisplay <- false;
7237 display ();
7239 let timeout =
7240 let now = now () in
7241 if deadline > now
7242 then (
7243 if deadline = infinity
7244 then ~-.1.0
7245 else max 0.0 (deadline -. now)
7247 else 0.0
7249 let r, _, _ =
7250 try Unix.select r [] [] timeout
7251 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7253 begin match r with
7254 | [] ->
7255 state.ghyll None;
7256 let newdeadline =
7257 if state.ghyll == noghyll
7258 then
7259 match state.autoscroll with
7260 | Some step when step != 0 ->
7261 let y = state.y + step in
7262 let y =
7263 if y < 0
7264 then state.maxy
7265 else if y >= state.maxy then 0 else y
7267 gotoy y;
7268 if state.mode = View
7269 then state.text <- "";
7270 deadline +. 0.01
7271 | _ -> infinity
7272 else deadline +. 0.01
7274 loop newdeadline
7276 | l ->
7277 let rec checkfds = function
7278 | [] -> ()
7279 | fd :: rest when fd = state.sr ->
7280 let cmd = readcmd state.sr in
7281 act cmd;
7282 checkfds rest
7284 | fd :: rest when fd = state.wsfd ->
7285 Wsi.readresp fd;
7286 checkfds rest
7288 | fd :: rest when Some fd = !optrfd ->
7289 begin match remote fd with
7290 | None -> optrfd := remoteopen !rcmdpath;
7291 | opt -> optrfd := opt
7292 end;
7293 checkfds rest
7295 | fd :: rest ->
7296 let s = String.create 80 in
7297 let n = tempfailureretry (Unix.read fd s 0) 80 in
7298 if conf.redirectstderr
7299 then (
7300 Buffer.add_substring state.errmsgs s 0 n;
7301 state.newerrmsgs <- true;
7302 state.redisplay <- true;
7304 else (
7305 prerr_string (String.sub s 0 n);
7306 flush stderr;
7308 checkfds rest
7310 checkfds l;
7311 let newdeadline =
7312 let deadline1 =
7313 if deadline = infinity
7314 then now () +. 0.01
7315 else deadline
7317 match state.autoscroll with
7318 | Some step when step != 0 -> deadline1
7319 | _ -> if state.ghyll == noghyll then infinity else deadline1
7321 loop newdeadline
7322 end;
7325 loop infinity;
7326 with Quit ->
7327 Config.save ();