Consistency
[llpp.git] / main.ml
blob3c05c1c85b5e25165a1829ae27d97669728a2165
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
487 ; mutable reprf : (unit -> unit)
489 and hists =
490 { pat : string circbuf
491 ; pag : string circbuf
492 ; nav : anchor circbuf
493 ; sel : string circbuf
497 let defconf =
498 { scrollbw = 7
499 ; scrollh = 12
500 ; icase = true
501 ; preload = true
502 ; pagebias = 0
503 ; verbose = false
504 ; debug = false
505 ; scrollstep = 24
506 ; hscrollstep = 24
507 ; maxhfit = true
508 ; crophack = false
509 ; autoscrollstep = 2
510 ; maxwait = None
511 ; hlinks = false
512 ; underinfo = false
513 ; interpagespace = 2
514 ; zoom = 1.0
515 ; presentation = false
516 ; angle = 0
517 ; cwinw = 900
518 ; cwinh = 900
519 ; savebmarks = true
520 ; proportional = true
521 ; trimmargins = false
522 ; trimfuzz = (0,0,0,0)
523 ; memlimit = 32 lsl 20
524 ; texcount = 256
525 ; sliceheight = 24
526 ; thumbw = 76
527 ; jumpback = true
528 ; bgcolor = (0.5, 0.5, 0.5)
529 ; bedefault = false
530 ; scrollbarinpm = true
531 ; tilew = 2048
532 ; tileh = 2048
533 ; mustoresize = 256 lsl 20
534 ; checkers = true
535 ; aalevel = 8
536 ; urilauncher =
537 (match platform with
538 | Plinux | Pfreebsd | Pdragonflybsd
539 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
540 | Posx -> "open \"%s\""
541 | Pcygwin -> "cygstart \"%s\""
542 | Punknown -> "echo %s")
543 ; pathlauncher = "lp \"%s\""
544 ; selcmd =
545 (match platform with
546 | Plinux | Pfreebsd | Pdragonflybsd
547 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
548 | Posx -> "pbcopy"
549 | Pcygwin -> "wsel"
550 | Punknown -> "cat")
551 ; colorspace = Rgb
552 ; invert = false
553 ; colorscale = 1.0
554 ; redirectstderr = false
555 ; ghyllscroll = None
556 ; columns = Csingle [||]
557 ; beyecolumns = None
558 ; updatecurs = false
559 ; hfsize = 12
560 ; pgscale = 1.0
561 ; usepbo = false
562 ; wheelbypage = false
563 ; stcmd = "echo SyncTex"
564 ; keyhashes =
565 let mk n = (n, Hashtbl.create 1) in
566 [ mk "global"
567 ; mk "info"
568 ; mk "help"
569 ; mk "outline"
570 ; mk "listview"
571 ; mk "birdseye"
572 ; mk "textentry"
573 ; mk "links"
574 ; mk "view"
579 let wtmode = ref false;;
581 let findkeyhash c name =
582 try List.assoc name c.keyhashes
583 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
586 let conf = { defconf with angle = defconf.angle };;
588 let pgscale h = truncate (float h *. conf.pgscale);;
590 type fontstate =
591 { mutable fontsize : int
592 ; mutable wwidth : float
593 ; mutable maxrows : int
597 let fstate =
598 { fontsize = 14
599 ; wwidth = nan
600 ; maxrows = -1
604 let geturl s =
605 let colonpos = try String.index s ':' with Not_found -> -1 in
606 let len = String.length s in
607 if colonpos >= 0 && colonpos + 3 < len
608 then (
609 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
610 then
611 let schemestartpos =
612 try String.rindex_from s colonpos ' '
613 with Not_found -> -1
615 let scheme =
616 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
618 match scheme with
619 | "http" | "ftp" | "mailto" ->
620 let epos =
621 try String.index_from s colonpos ' '
622 with Not_found -> len
624 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
625 | _ -> ""
626 else ""
628 else ""
631 let gotouri uri =
632 if String.length conf.urilauncher = 0
633 then print_endline uri
634 else (
635 let url = geturl uri in
636 if String.length url = 0
637 then print_endline uri
638 else
639 let re = Str.regexp "%s" in
640 let command = Str.global_replace re url conf.urilauncher in
641 try popen command []
642 with exn ->
643 Printf.eprintf
644 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
645 flush stderr;
649 let version () =
650 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
651 (platform_to_string platform) Sys.word_size Sys.ocaml_version
654 let makehelp () =
655 let strings = version () :: "" :: Help.keys in
656 Array.of_list (
657 List.map (fun s ->
658 let url = geturl s in
659 if String.length url > 0
660 then (s, 0, Action (fun u -> gotouri url; u))
661 else (s, 0, Noaction)
662 ) strings);
665 let noghyll _ = ();;
666 let firstgeomcmds = "", [];;
667 let noreprf () = ();;
669 let state =
670 { sr = Unix.stdin
671 ; sw = Unix.stdin
672 ; wsfd = Unix.stdin
673 ; errfd = None
674 ; stderr = Unix.stderr
675 ; errmsgs = Buffer.create 0
676 ; newerrmsgs = false
677 ; x = 0
678 ; y = 0
679 ; w = 0
680 ; scrollw = 0
681 ; hscrollh = 0
682 ; anchor = emptyanchor
683 ; ranchors = []
684 ; layout = []
685 ; maxy = max_int
686 ; tilelru = Queue.create ()
687 ; pagemap = Hashtbl.create 10
688 ; tilemap = Hashtbl.create 10
689 ; pdims = []
690 ; pagecount = 0
691 ; currently = Idle
692 ; mstate = Mnone
693 ; rects = []
694 ; rects1 = []
695 ; text = ""
696 ; mode = View
697 ; winstate = []
698 ; searchpattern = ""
699 ; outlines = [||]
700 ; bookmarks = []
701 ; path = ""
702 ; password = ""
703 ; nameddest = ""
704 ; geomcmds = firstgeomcmds
705 ; hists =
706 { nav = cbnew 10 emptyanchor
707 ; pat = cbnew 10 ""
708 ; pag = cbnew 10 ""
709 ; sel = cbnew 10 ""
711 ; memused = 0
712 ; gen = 0
713 ; throttle = None
714 ; autoscroll = None
715 ; ghyll = noghyll
716 ; help = makehelp ()
717 ; docinfo = []
718 ; texid = None
719 ; prevzoom = 1.0
720 ; progress = -1.0
721 ; uioh = nouioh
722 ; redisplay = true
723 ; mpos = (-1, -1)
724 ; keystate = KSnone
725 ; glinks = false
726 ; prevcolumns = None
727 ; wthack = false
728 ; winw = -1
729 ; winh = -1
730 ; reprf = noreprf
734 let setfontsize n =
735 fstate.fontsize <- n;
736 fstate.wwidth <- measurestr fstate.fontsize "w";
737 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
740 let vlog fmt =
741 if conf.verbose
742 then
743 Printf.kprintf prerr_endline fmt
744 else
745 Printf.kprintf ignore fmt
748 let launchpath () =
749 if String.length conf.pathlauncher = 0
750 then print_endline state.path
751 else (
752 let re = Str.regexp "%s" in
753 let command = Str.global_replace re state.path conf.pathlauncher in
754 try popen command []
755 with exn ->
756 Printf.eprintf
757 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
758 flush stderr;
762 module Ne = struct
763 type 'a t = | Res of 'a | Exn of exn;;
765 let pipe () =
766 try Res (Unix.pipe ())
767 with exn -> Exn exn
770 let clo fd f =
771 try tempfailureretry Unix.close fd
772 with exn -> f (Printexc.to_string exn)
775 let dup fd =
776 try Res (tempfailureretry Unix.dup fd)
777 with exn -> Exn exn
780 let dup2 fd1 fd2 =
781 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
782 with exn -> Exn exn
784 end;;
786 let redirectstderr () =
787 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
788 if conf.redirectstderr
789 then
790 match Ne.pipe () with
791 | Ne.Exn exn ->
792 dolog "failed to create stderr redirection pipes: %s"
793 (Printexc.to_string exn)
795 | Ne.Res (r, w) ->
796 begin match Ne.dup Unix.stderr with
797 | Ne.Exn exn ->
798 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
799 Ne.clo r (clofail "pipe/r");
800 Ne.clo w (clofail "pipe/w");
802 | Ne.Res dupstderr ->
803 begin match Ne.dup2 w Unix.stderr with
804 | Ne.Exn exn ->
805 dolog "failed to dup2 to stderr: %s"
806 (Printexc.to_string exn);
807 Ne.clo dupstderr (clofail "stderr duplicate");
808 Ne.clo r (clofail "redir pipe/r");
809 Ne.clo w (clofail "redir pipe/w");
811 | Ne.Res () ->
812 state.stderr <- dupstderr;
813 state.errfd <- Some r;
814 end;
816 else (
817 state.newerrmsgs <- false;
818 begin match state.errfd with
819 | Some fd ->
820 begin match Ne.dup2 state.stderr Unix.stderr with
821 | Ne.Exn exn ->
822 dolog "failed to dup2 original stderr: %s"
823 (Printexc.to_string exn)
824 | Ne.Res () ->
825 Ne.clo fd (clofail "dup of stderr");
826 state.errfd <- None;
827 end;
828 | None -> ()
829 end;
830 prerr_string (Buffer.contents state.errmsgs);
831 flush stderr;
832 Buffer.clear state.errmsgs;
836 module G =
837 struct
838 let postRedisplay who =
839 if conf.verbose
840 then prerr_endline ("redisplay for " ^ who);
841 state.redisplay <- true;
843 end;;
845 let getopaque pageno =
846 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
847 with Not_found -> None
850 let putopaque pageno opaque =
851 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
854 let pagetranslatepoint l x y =
855 let dy = y - l.pagedispy in
856 let y = dy + l.pagey in
857 let dx = x - l.pagedispx in
858 let x = dx + l.pagex in
859 (x, y);
862 let onppundermouse g x y d =
863 let rec f = function
864 | l :: rest ->
865 begin match getopaque l.pageno with
866 | Some opaque ->
867 let x0 = l.pagedispx in
868 let x1 = x0 + l.pagevw in
869 let y0 = l.pagedispy in
870 let y1 = y0 + l.pagevh in
871 if y >= y0 && y <= y1 && x >= x0 && x <= x1
872 then
873 let px, py = pagetranslatepoint l x y in
874 match g opaque l px py with
875 | Some res -> res
876 | None -> f rest
877 else f rest
878 | _ ->
879 f rest
881 | [] -> d
883 f state.layout
886 let getunder x y =
887 let g opaque _ px py =
888 match whatsunder opaque px py with
889 | Unone -> None
890 | under -> Some under
892 onppundermouse g x y Unone
895 let unproject x y =
896 let g opaque l x y =
897 match unproject opaque x y with
898 | Some (x, y) -> Some (Some (l.pageno, x, y))
899 | None -> None
901 onppundermouse g x y None;
904 let showtext c s =
905 state.text <- Printf.sprintf "%c%s" c s;
906 G.postRedisplay "showtext";
909 let undertext = function
910 | Unone -> "none"
911 | Ulinkuri s -> s
912 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
913 | Utext s -> "font: " ^ s
914 | Uunexpected s -> "unexpected: " ^ s
915 | Ulaunch s -> "launch: " ^ s
916 | Unamed s -> "named: " ^ s
917 | Uremote (filename, pageno) ->
918 Printf.sprintf "%s: page %d" filename (pageno+1)
921 let updateunder x y =
922 match getunder x y with
923 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
924 | Ulinkuri uri ->
925 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
926 Wsi.setcursor Wsi.CURSOR_INFO
927 | Ulinkgoto (pageno, _) ->
928 if conf.underinfo
929 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
930 Wsi.setcursor Wsi.CURSOR_INFO
931 | Utext s ->
932 if conf.underinfo then showtext 'f' ("ont: " ^ s);
933 Wsi.setcursor Wsi.CURSOR_TEXT
934 | Uunexpected s ->
935 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
936 Wsi.setcursor Wsi.CURSOR_INHERIT
937 | Ulaunch s ->
938 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
939 Wsi.setcursor Wsi.CURSOR_INHERIT
940 | Unamed s ->
941 if conf.underinfo then showtext 'n' ("amed: " ^ s);
942 Wsi.setcursor Wsi.CURSOR_INHERIT
943 | Uremote (filename, pageno) ->
944 if conf.underinfo then showtext 'r'
945 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
946 Wsi.setcursor Wsi.CURSOR_INFO
949 let showlinktype under =
950 if conf.underinfo
951 then
952 match under with
953 | Unone -> ()
954 | under ->
955 let s = undertext under in
956 showtext ' ' s
959 let addchar s c =
960 let b = Buffer.create (String.length s + 1) in
961 Buffer.add_string b s;
962 Buffer.add_char b c;
963 Buffer.contents b;
966 let colorspace_of_string s =
967 match String.lowercase s with
968 | "rgb" -> Rgb
969 | "bgr" -> Bgr
970 | "gray" -> Gray
971 | _ -> failwith "invalid colorspace"
974 let int_of_colorspace = function
975 | Rgb -> 0
976 | Bgr -> 1
977 | Gray -> 2
980 let colorspace_of_int = function
981 | 0 -> Rgb
982 | 1 -> Bgr
983 | 2 -> Gray
984 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
987 let colorspace_to_string = function
988 | Rgb -> "rgb"
989 | Bgr -> "bgr"
990 | Gray -> "gray"
993 let intentry_with_suffix text key =
994 let c =
995 if key >= 32 && key < 127
996 then Char.chr key
997 else '\000'
999 match Char.lowercase c with
1000 | '0' .. '9' ->
1001 let text = addchar text c in
1002 TEcont text
1004 | 'k' | 'm' | 'g' ->
1005 let text = addchar text c in
1006 TEcont text
1008 | _ ->
1009 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1010 TEcont text
1013 let multicolumns_to_string (n, a, b) =
1014 if a = 0 && b = 0
1015 then Printf.sprintf "%d" n
1016 else Printf.sprintf "%d,%d,%d" n a b;
1019 let multicolumns_of_string s =
1021 (int_of_string s, 0, 0)
1022 with _ ->
1023 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1024 if a > 1 || b > 1
1025 then failwith "subtly broken"; (n, a, b)
1029 let readcmd fd =
1030 let s = "xxxx" in
1031 let n = tempfailureretry (Unix.read fd s 0) 4 in
1032 if n != 4 then failwith "incomplete read(len)";
1033 let len = 0
1034 lor (Char.code s.[0] lsl 24)
1035 lor (Char.code s.[1] lsl 16)
1036 lor (Char.code s.[2] lsl 8)
1037 lor (Char.code s.[3] lsl 0)
1039 let s = String.create len in
1040 let n = tempfailureretry (Unix.read fd s 0) len in
1041 if n != len then failwith "incomplete read(data)";
1045 let btod b = if b then 1 else 0;;
1047 let wcmd fmt =
1048 let b = Buffer.create 16 in
1049 Buffer.add_string b "llll";
1050 Printf.kbprintf
1051 (fun b ->
1052 let s = Buffer.contents b in
1053 let n = String.length s in
1054 let len = n - 4 in
1055 (* dolog "wcmd %S" (String.sub s 4 len); *)
1056 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1057 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1058 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1059 s.[3] <- Char.chr (len land 0xff);
1060 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1061 if n' != n then failwith "write failed";
1062 ) b fmt;
1065 let calcips h =
1066 let d = state.winh - h in
1067 max conf.interpagespace ((d + 1) / 2)
1070 let rowyh (c, coverA, coverB) b n =
1071 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1072 then
1073 let _, _, vy, (_, _, h, _) = b.(n) in
1074 (vy, h)
1075 else
1076 let n' = n - coverA in
1077 let d = n' mod c in
1078 let s = n - d in
1079 let e = min state.pagecount (s + c) in
1080 let rec find m miny maxh = if m = e then miny, maxh else
1081 let _, _, y, (_, _, h, _) = b.(m) in
1082 let miny = min miny y in
1083 let maxh = max maxh h in
1084 find (m+1) miny maxh
1085 in find s max_int 0
1088 let calcheight () =
1089 match conf.columns with
1090 | Cmulti ((_, _, _) as cl, b) ->
1091 if Array.length b > 0
1092 then
1093 let y, h = rowyh cl b (Array.length b - 1) in
1094 y + h + (if conf.presentation then calcips h else 0)
1095 else 0
1096 | Csingle b ->
1097 if Array.length b > 0
1098 then
1099 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1100 y + h + (if conf.presentation then calcips h else 0)
1101 else 0
1102 | Csplit (_, b) ->
1103 if Array.length b > 0
1104 then
1105 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1106 y + h
1107 else 0
1110 let getpageyh pageno =
1111 let pageno = bound pageno 0 (state.pagecount-1) in
1112 match conf.columns with
1113 | Csingle b ->
1114 if Array.length b = 0
1115 then 0, 0
1116 else
1117 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1118 let y =
1119 if conf.presentation
1120 then y - calcips h
1121 else y
1123 y, h
1124 | Cmulti (cl, b) ->
1125 if Array.length b = 0
1126 then 0, 0
1127 else
1128 let y, h = rowyh cl b pageno in
1129 let y =
1130 if conf.presentation
1131 then y - calcips h
1132 else y
1134 y, h
1135 | Csplit (c, b) ->
1136 if Array.length b = 0
1137 then 0, 0
1138 else
1139 let n = pageno*c in
1140 let (_, _, y, (_, _, h, _)) = b.(n) in
1141 y, h
1144 let getpagedim pageno =
1145 let rec f ppdim l =
1146 match l with
1147 | (n, _, _, _) as pdim :: rest ->
1148 if n >= pageno
1149 then (if n = pageno then pdim else ppdim)
1150 else f pdim rest
1152 | [] -> ppdim
1154 f (-1, -1, -1, -1) state.pdims
1157 let getpagey pageno = fst (getpageyh pageno);;
1159 let nogeomcmds cmds =
1160 match cmds with
1161 | s, [] -> String.length s = 0
1162 | _ -> false
1165 let page_of_y y =
1166 let ((c, coverA, coverB) as cl), b =
1167 match conf.columns with
1168 | Csingle b -> (1, 0, 0), b
1169 | Cmulti (c, b) -> c, b
1170 | Csplit (_, b) -> (1, 0, 0), b
1172 if Array.length b = 0
1173 then -1
1174 else
1175 let rec bsearch nmin nmax =
1176 if nmin > nmax
1177 then bound nmin 0 (state.pagecount-1)
1178 else
1179 let n = (nmax + nmin) / 2 in
1180 let vy, h = rowyh cl b n in
1181 let y0, y1 =
1182 if conf.presentation
1183 then
1184 let ips = calcips h in
1185 let y0 = vy - ips in
1186 let y1 = vy + h + ips in
1187 y0, y1
1188 else (
1189 if n = 0
1190 then 0, vy + h + conf.interpagespace
1191 else
1192 let y0 = vy - conf.interpagespace in
1193 y0, y0 + h + conf.interpagespace
1196 if y >= y0 && y < y1
1197 then (
1198 if c = 1
1199 then n
1200 else (
1201 if n > coverA
1202 then
1203 if n < state.pagecount - coverB
1204 then ((n-coverA)/c)*c + coverA
1205 else n
1206 else n
1209 else (
1210 if y > y0
1211 then bsearch (n+1) nmax
1212 else bsearch nmin (n-1)
1215 let r = bsearch 0 (state.pagecount-1) in
1219 let layoutN ((columns, coverA, coverB), b) y sh =
1220 let sh = sh - state.hscrollh in
1221 let rec fold accu n =
1222 if n = Array.length b
1223 then accu
1224 else
1225 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1226 if (vy - y) > sh &&
1227 (n = coverA - 1
1228 || n = state.pagecount - coverB
1229 || (n - coverA) mod columns = columns - 1)
1230 then accu
1231 else
1232 let accu =
1233 if vy + h > y
1234 then
1235 let pagey = max 0 (y - vy) in
1236 let pagedispy = if pagey > 0 then 0 else vy - y in
1237 let pagedispx, pagex =
1238 let pdx =
1239 if n = coverA - 1 || n = state.pagecount - coverB
1240 then state.x + (state.winw - state.scrollw - w) / 2
1241 else dx + xoff + state.x
1243 if pdx < 0
1244 then 0, -pdx
1245 else pdx, 0
1247 let pagevw =
1248 let vw = state.winw - state.scrollw - pagedispx in
1249 let pw = w - pagex in
1250 min vw pw
1252 let pagevh = min (h - pagey) (sh - pagedispy) in
1253 if pagevw > 0 && pagevh > 0
1254 then
1255 let e =
1256 { pageno = n
1257 ; pagedimno = pdimno
1258 ; pagew = w
1259 ; pageh = h
1260 ; pagex = pagex
1261 ; pagey = pagey
1262 ; pagevw = pagevw
1263 ; pagevh = pagevh
1264 ; pagedispx = pagedispx
1265 ; pagedispy = pagedispy
1266 ; pagecol = 0
1269 e :: accu
1270 else
1271 accu
1272 else
1273 accu
1275 fold accu (n+1)
1277 List.rev (fold [] (page_of_y y));
1280 let layoutS (columns, b) y sh =
1281 let sh = sh - state.hscrollh in
1282 let rec fold accu n =
1283 if n = Array.length b
1284 then accu
1285 else
1286 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1287 if (vy - y) > sh
1288 then accu
1289 else
1290 let accu =
1291 if vy + pageh > y
1292 then
1293 let x = xoff + state.x in
1294 let pagey = max 0 (y - vy) in
1295 let pagedispy = if pagey > 0 then 0 else vy - y in
1296 let pagedispx, pagex =
1297 if px = 0
1298 then (
1299 if x < 0
1300 then 0, -x
1301 else x, 0
1303 else (
1304 let px = px - x in
1305 if px < 0
1306 then -px, 0
1307 else 0, px
1310 let pagecolw = pagew/columns in
1311 let pagedispx =
1312 if pagecolw < state.winw
1313 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1314 else pagedispx
1316 let pagevw =
1317 let vw = state.winw - pagedispx - state.scrollw in
1318 let pw = pagew - pagex in
1319 min vw pw
1321 let pagevw = min pagevw pagecolw in
1322 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1323 if pagevw > 0 && pagevh > 0
1324 then
1325 let e =
1326 { pageno = n/columns
1327 ; pagedimno = pdimno
1328 ; pagew = pagew
1329 ; pageh = pageh
1330 ; pagex = pagex
1331 ; pagey = pagey
1332 ; pagevw = pagevw
1333 ; pagevh = pagevh
1334 ; pagedispx = pagedispx
1335 ; pagedispy = pagedispy
1336 ; pagecol = n mod columns
1339 e :: accu
1340 else
1341 accu
1342 else
1343 accu
1345 fold accu (n+1)
1347 List.rev (fold [] 0)
1350 let layout y sh =
1351 if nogeomcmds state.geomcmds
1352 then
1353 match conf.columns with
1354 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1355 | Cmulti c -> layoutN c y sh
1356 | Csplit s -> layoutS s y sh
1357 else []
1360 let clamp incr =
1361 let y = state.y + incr in
1362 let y = max 0 y in
1363 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1367 let itertiles l f =
1368 let tilex = l.pagex mod conf.tilew in
1369 let tiley = l.pagey mod conf.tileh in
1371 let col = l.pagex / conf.tilew in
1372 let row = l.pagey / conf.tileh in
1374 let rec rowloop row y0 dispy h =
1375 if h = 0
1376 then ()
1377 else (
1378 let dh = conf.tileh - y0 in
1379 let dh = min h dh in
1380 let rec colloop col x0 dispx w =
1381 if w = 0
1382 then ()
1383 else (
1384 let dw = conf.tilew - x0 in
1385 let dw = min w dw in
1387 f col row dispx dispy x0 y0 dw dh;
1388 colloop (col+1) 0 (dispx+dw) (w-dw)
1391 colloop col tilex l.pagedispx l.pagevw;
1392 rowloop (row+1) 0 (dispy+dh) (h-dh)
1395 if l.pagevw > 0 && l.pagevh > 0
1396 then rowloop row tiley l.pagedispy l.pagevh;
1399 let gettileopaque l col row =
1400 let key =
1401 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1403 try Some (Hashtbl.find state.tilemap key)
1404 with Not_found -> None
1407 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1408 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1409 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1412 let drawtiles l color =
1413 GlDraw.color color;
1414 let f col row x y tilex tiley w h =
1415 match gettileopaque l col row with
1416 | Some (opaque, _, t) ->
1417 let params = x, y, w, h, tilex, tiley in
1418 if conf.invert
1419 then (
1420 Gl.enable `blend;
1421 GlFunc.blend_func `zero `one_minus_src_color;
1423 drawtile params opaque;
1424 if conf.invert
1425 then Gl.disable `blend;
1426 if conf.debug
1427 then (
1428 let s = Printf.sprintf
1429 "%d[%d,%d] %f sec"
1430 l.pageno col row t
1432 let w = measurestr fstate.fontsize s in
1433 GlMisc.push_attrib [`current];
1434 GlDraw.color (0.0, 0.0, 0.0);
1435 GlDraw.rect
1436 (float (x-2), float (y-2))
1437 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1438 GlDraw.color (1.0, 1.0, 1.0);
1439 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1440 GlMisc.pop_attrib ();
1443 | _ ->
1444 let w =
1445 let lw = state.winw - state.scrollw - x in
1446 min lw w
1447 and h =
1448 let lh = state.winh - y in
1449 min lh h
1451 begin match state.texid with
1452 | Some id ->
1453 Gl.enable `texture_2d;
1454 GlTex.bind_texture `texture_2d id;
1455 let x0 = float x
1456 and y0 = float y
1457 and x1 = float (x+w)
1458 and y1 = float (y+h) in
1460 let tw = float w /. 16.0
1461 and th = float h /. 16.0 in
1462 let tx0 = float tilex /. 16.0
1463 and ty0 = float tiley /. 16.0 in
1464 let tx1 = tx0 +. tw
1465 and ty1 = ty0 +. th in
1466 GlDraw.begins `quads;
1467 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1468 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1469 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1470 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1471 GlDraw.ends ();
1473 Gl.disable `texture_2d;
1474 | None ->
1475 GlDraw.color (1.0, 1.0, 1.0);
1476 GlDraw.rect
1477 (float x, float y)
1478 (float (x+w), float (y+h));
1479 end;
1480 if w > 128 && h > fstate.fontsize + 10
1481 then (
1482 GlDraw.color (0.0, 0.0, 0.0);
1483 let c, r =
1484 if conf.verbose
1485 then (col*conf.tilew, row*conf.tileh)
1486 else col, row
1488 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1490 GlDraw.color color;
1492 itertiles l f
1495 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1497 let tilevisible1 l x y =
1498 let ax0 = l.pagex
1499 and ax1 = l.pagex + l.pagevw
1500 and ay0 = l.pagey
1501 and ay1 = l.pagey + l.pagevh in
1503 let bx0 = x
1504 and by0 = y in
1505 let bx1 = min (bx0 + conf.tilew) l.pagew
1506 and by1 = min (by0 + conf.tileh) l.pageh in
1508 let rx0 = max ax0 bx0
1509 and ry0 = max ay0 by0
1510 and rx1 = min ax1 bx1
1511 and ry1 = min ay1 by1 in
1513 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1514 nonemptyintersection
1517 let tilevisible layout n x y =
1518 let rec findpageinlayout m = function
1519 | l :: rest when l.pageno = n ->
1520 tilevisible1 l x y || (
1521 match conf.columns with
1522 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1523 | _ -> false
1525 | _ :: rest -> findpageinlayout 0 rest
1526 | [] -> false
1528 findpageinlayout 0 layout;
1531 let tileready l x y =
1532 tilevisible1 l x y &&
1533 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1536 let tilepage n p layout =
1537 let rec loop = function
1538 | l :: rest ->
1539 if l.pageno = n
1540 then
1541 let f col row _ _ _ _ _ _ =
1542 if state.currently = Idle
1543 then
1544 match gettileopaque l col row with
1545 | Some _ -> ()
1546 | None ->
1547 let x = col*conf.tilew
1548 and y = row*conf.tileh in
1549 let w =
1550 let w = l.pagew - x in
1551 min w conf.tilew
1553 let h =
1554 let h = l.pageh - y in
1555 min h conf.tileh
1557 let pbo =
1558 if conf.usepbo
1559 then getpbo w h conf.colorspace
1560 else "0"
1562 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1563 state.currently <-
1564 Tiling (
1565 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1566 conf.tilew, conf.tileh
1569 itertiles l f;
1570 else
1571 loop rest
1573 | [] -> ()
1575 if nogeomcmds state.geomcmds
1576 then loop layout;
1579 let preloadlayout y =
1580 let y = if y < state.winh then 0 else y - state.winh in
1581 let h = state.winh*3 in
1582 layout y h;
1585 let load pages =
1586 let rec loop pages =
1587 if state.currently != Idle
1588 then ()
1589 else
1590 match pages with
1591 | l :: rest ->
1592 begin match getopaque l.pageno with
1593 | None ->
1594 wcmd "page %d %d" l.pageno l.pagedimno;
1595 state.currently <- Loading (l, state.gen);
1596 | Some opaque ->
1597 tilepage l.pageno opaque pages;
1598 loop rest
1599 end;
1600 | _ -> ()
1602 if nogeomcmds state.geomcmds
1603 then loop pages
1606 let preload pages =
1607 load pages;
1608 if conf.preload && state.currently = Idle
1609 then load (preloadlayout state.y);
1612 let layoutready layout =
1613 let rec fold all ls =
1614 all && match ls with
1615 | l :: rest ->
1616 let seen = ref false in
1617 let allvisible = ref true in
1618 let foo col row _ _ _ _ _ _ =
1619 seen := true;
1620 allvisible := !allvisible &&
1621 begin match gettileopaque l col row with
1622 | Some _ -> true
1623 | None -> false
1626 itertiles l foo;
1627 fold (!seen && !allvisible) rest
1628 | [] -> true
1630 let alltilesvisible = fold true layout in
1631 alltilesvisible;
1634 let gotoy y =
1635 state.wthack <- false;
1636 let y = bound y 0 state.maxy in
1637 let y, layout, proceed =
1638 match conf.maxwait with
1639 | Some time when state.ghyll == noghyll ->
1640 begin match state.throttle with
1641 | None ->
1642 let layout = layout y state.winh in
1643 let ready = layoutready layout in
1644 if not ready
1645 then (
1646 load layout;
1647 state.throttle <- Some (layout, y, now ());
1649 else G.postRedisplay "gotoy showall (None)";
1650 y, layout, ready
1651 | Some (_, _, started) ->
1652 let dt = now () -. started in
1653 if dt > time
1654 then (
1655 state.throttle <- None;
1656 let layout = layout y state.winh in
1657 load layout;
1658 G.postRedisplay "maxwait";
1659 y, layout, true
1661 else -1, [], false
1664 | _ ->
1665 let layout = layout y state.winh in
1666 G.postRedisplay "gotoy ready";
1667 y, layout, true
1669 if proceed
1670 then (
1671 state.y <- y;
1672 state.layout <- layout;
1673 begin match state.mode with
1674 | LinkNav (Ltexact (pageno, linkno)) ->
1675 let rec loop = function
1676 | [] ->
1677 state.mode <- LinkNav (Ltgendir 0)
1678 | l :: _ when l.pageno = pageno ->
1679 begin match getopaque pageno with
1680 | None ->
1681 state.mode <- LinkNav (Ltgendir 0)
1682 | Some opaque ->
1683 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1684 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1685 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1686 then state.mode <- LinkNav (Ltgendir 0)
1688 | _ :: rest -> loop rest
1690 loop layout
1691 | _ -> ()
1692 end;
1693 begin match state.mode with
1694 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1695 if not (pagevisible layout pageno)
1696 then (
1697 match state.layout with
1698 | [] -> ()
1699 | l :: _ ->
1700 state.mode <- Birdseye (
1701 conf, leftx, l.pageno, hooverpageno, anchor
1704 | LinkNav (Ltgendir dir as lt) ->
1705 let linknav =
1706 let rec loop = function
1707 | [] -> lt
1708 | l :: rest ->
1709 match getopaque l.pageno with
1710 | None -> loop rest
1711 | Some opaque ->
1712 let link =
1713 let ld =
1714 if dir = 0
1715 then LDfirstvisible (l.pagex, l.pagey, dir)
1716 else (
1717 if dir > 0 then LDfirst else LDlast
1720 findlink opaque ld
1722 match link with
1723 | Lnotfound -> loop rest
1724 | Lfound n ->
1725 showlinktype (getlink opaque n);
1726 Ltexact (l.pageno, n)
1728 loop state.layout
1730 state.mode <- LinkNav linknav
1731 | _ -> ()
1732 end;
1733 preload layout;
1735 state.ghyll <- noghyll;
1736 if conf.updatecurs
1737 then (
1738 let mx, my = state.mpos in
1739 updateunder mx my;
1743 let conttiling pageno opaque =
1744 tilepage pageno opaque
1745 (if conf.preload then preloadlayout state.y else state.layout)
1748 let gotoy_and_clear_text y =
1749 if not conf.verbose then state.text <- "";
1750 gotoy y;
1753 let getanchor1 l =
1754 let top =
1755 let coloff = l.pagecol * l.pageh in
1756 float (l.pagey + coloff) /. float l.pageh
1758 let dtop =
1759 if l.pagedispy = 0
1760 then
1762 else
1763 if conf.presentation
1764 then float l.pagedispy /. float (calcips l.pageh)
1765 else float l.pagedispy /. float conf.interpagespace
1767 (l.pageno, top, dtop)
1770 let getanchor () =
1771 match state.layout with
1772 | l :: _ -> getanchor1 l
1773 | [] ->
1774 let n = page_of_y state.y in
1775 if n = -1
1776 then state.anchor
1777 else
1778 let y, h = getpageyh n in
1779 let dy = y - state.y in
1780 let dtop =
1781 if conf.presentation
1782 then
1783 let ips = calcips h in
1784 float (dy + ips) /. float ips
1785 else
1786 float dy /. float conf.interpagespace
1788 (n, 0.0, dtop)
1791 let getanchory (n, top, dtop) =
1792 let y, h = getpageyh n in
1793 if conf.presentation
1794 then
1795 let ips = calcips h in
1796 y + truncate (top*.float h -. dtop*.float ips) + ips;
1797 else
1798 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1801 let gotoanchor anchor =
1802 gotoy (getanchory anchor);
1805 let addnav () =
1806 cbput state.hists.nav (getanchor ());
1809 let getnav dir =
1810 let anchor = cbgetc state.hists.nav dir in
1811 getanchory anchor;
1814 let gotoghyll y =
1815 let scroll f n a b =
1816 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1817 let snake f a b =
1818 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1819 if f < a
1820 then s (float f /. float a)
1821 else (
1822 if f > b
1823 then 1.0 -. s ((float (f-b) /. float (n-b)))
1824 else 1.0
1827 snake f a b
1828 and summa f n a b =
1829 (* courtesy:
1830 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1831 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1832 let iv1 = iv f in
1833 let ins = float a *. iv1
1834 and outs = float (n-b) *. iv1 in
1835 let ones = b - a in
1836 ins +. outs +. float ones
1838 let rec set (_N, _A, _B) y sy =
1839 let sum = summa 1.0 _N _A _B in
1840 let dy = float (y - sy) in
1841 state.ghyll <- (
1842 let rec gf n y1 o =
1843 if n >= _N
1844 then state.ghyll <- noghyll
1845 else
1846 let go n =
1847 let s = scroll n _N _A _B in
1848 let y1 = y1 +. ((s *. dy) /. sum) in
1849 gotoy_and_clear_text (truncate y1);
1850 state.ghyll <- gf (n+1) y1;
1852 match o with
1853 | None -> go n
1854 | Some y' -> set (_N/2, 1, 1) y' state.y
1856 gf 0 (float state.y)
1859 match conf.ghyllscroll with
1860 | None ->
1861 gotoy_and_clear_text y
1862 | Some nab ->
1863 if state.ghyll == noghyll
1864 then set nab y state.y
1865 else state.ghyll (Some y)
1868 let gotopage n top =
1869 let y, h = getpageyh n in
1870 let y = y + (truncate (top *. float h)) in
1871 gotoghyll y
1874 let gotopage1 n top =
1875 let y = getpagey n in
1876 let y = y + top in
1877 gotoghyll y
1880 let invalidate s f =
1881 state.layout <- [];
1882 state.pdims <- [];
1883 state.rects <- [];
1884 state.rects1 <- [];
1885 match state.geomcmds with
1886 | ps, [] when String.length ps = 0 ->
1887 f ();
1888 state.geomcmds <- s, [];
1890 | ps, [] ->
1891 state.geomcmds <- ps, [s, f];
1893 | ps, (s', _) :: rest when s' = s ->
1894 state.geomcmds <- ps, ((s, f) :: rest);
1896 | ps, cmds ->
1897 state.geomcmds <- ps, ((s, f) :: cmds);
1900 let flushpages () =
1901 Hashtbl.iter (fun _ opaque ->
1902 wcmd "freepage %s" opaque;
1903 ) state.pagemap;
1904 Hashtbl.clear state.pagemap;
1907 let opendoc path password =
1908 state.path <- path;
1909 state.password <- password;
1910 state.gen <- state.gen + 1;
1911 state.docinfo <- [];
1913 flushpages ();
1914 setaalevel conf.aalevel;
1915 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1916 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1917 invalidate "reqlayout"
1918 (fun () ->
1919 wcmd "reqlayout %d %d %s\000"
1920 conf.angle (btod conf.proportional) state.nameddest;
1924 let reload () =
1925 state.anchor <- getanchor ();
1926 state.wthack <- !wtmode;
1927 opendoc state.path state.password;
1930 let scalecolor c =
1931 let c = c *. conf.colorscale in
1932 (c, c, c);
1935 let scalecolor2 (r, g, b) =
1936 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1939 let docolumns = function
1940 | Csingle _ ->
1941 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1942 let rec loop pageno pdimno pdim y ph pdims =
1943 if pageno = state.pagecount
1944 then ()
1945 else
1946 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1947 match pdims with
1948 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1949 pdimno+1, pdim, rest
1950 | _ ->
1951 pdimno, pdim, pdims
1953 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
1954 let y = y +
1955 (if conf.presentation
1956 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1957 else (if pageno = 0 then 0 else conf.interpagespace)
1960 a.(pageno) <- (pdimno, x, y, pdim);
1961 loop (pageno+1) pdimno pdim (y + h) h pdims
1963 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1964 conf.columns <- Csingle a;
1966 | Cmulti ((columns, coverA, coverB), _) ->
1967 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1968 let rec loop pageno pdimno pdim x y rowh pdims =
1969 let rec fixrow m = if m = pageno then () else
1970 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1971 if h < rowh
1972 then (
1973 let y = y + (rowh - h) / 2 in
1974 a.(m) <- (pdimno, x, y, pdim);
1976 fixrow (m+1)
1978 if pageno = state.pagecount
1979 then fixrow (((pageno - 1) / columns) * columns)
1980 else
1981 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1982 match pdims with
1983 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1984 pdimno+1, pdim, rest
1985 | _ ->
1986 pdimno, pdim, pdims
1988 let x, y, rowh' =
1989 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1990 then (
1991 let x = (state.winw - state.scrollw - w) / 2 in
1992 let ips =
1993 if conf.presentation then calcips h else conf.interpagespace in
1994 x, y + ips + rowh, h
1996 else (
1997 if (pageno - coverA) mod columns = 0
1998 then (
1999 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2000 let y =
2001 if conf.presentation
2002 then
2003 let ips = calcips h in
2004 y + (if pageno = 0 then 0 else calcips rowh + ips)
2005 else
2006 y + (if pageno = 0 then 0 else conf.interpagespace)
2008 x, y + rowh, h
2010 else x, y, max rowh h
2013 let y =
2014 if pageno > 1 && (pageno - coverA) mod columns = 0
2015 then (
2016 let y =
2017 if pageno = columns && conf.presentation
2018 then (
2019 let ips = calcips rowh in
2020 for i = 0 to pred columns
2022 let (pdimno, x, y, pdim) = a.(i) in
2023 a.(i) <- (pdimno, x, y+ips, pdim)
2024 done;
2025 y+ips;
2027 else y
2029 fixrow (pageno - columns);
2032 else y
2034 a.(pageno) <- (pdimno, x, y, pdim);
2035 let x = x + w + xoff*2 + conf.interpagespace in
2036 loop (pageno+1) pdimno pdim x y rowh' pdims
2038 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2039 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2041 | Csplit (c, _) ->
2042 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2043 let rec loop pageno pdimno pdim y pdims =
2044 if pageno = state.pagecount
2045 then ()
2046 else
2047 let pdimno, ((_, w, h, _) as pdim), pdims =
2048 match pdims with
2049 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2050 pdimno+1, pdim, rest
2051 | _ ->
2052 pdimno, pdim, pdims
2054 let cw = w / c in
2055 let rec loop1 n x y =
2056 if n = c then y else (
2057 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2058 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2061 let y = loop1 0 0 y in
2062 loop (pageno+1) pdimno pdim y pdims
2064 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2065 conf.columns <- Csplit (c, a);
2068 let represent () =
2069 docolumns conf.columns;
2070 state.maxy <- calcheight ();
2071 state.hscrollh <-
2072 if state.w <= state.winw - state.scrollw
2073 then 0
2074 else state.scrollw
2076 if state.reprf == noreprf
2077 then
2078 let wthack = state.wthack in
2079 begin match state.mode with
2080 | Birdseye (_, _, pageno, _, _) ->
2081 let y, h = getpageyh pageno in
2082 let top = (state.winh - h) / 2 in
2083 gotoy (max 0 (y - top))
2084 | _ -> gotoanchor state.anchor
2085 end;
2086 state.wthack <- wthack;
2087 state.reprf <- noreprf;
2088 else state.reprf ()
2091 let reshape w h =
2092 state.wthack <- false;
2093 GlDraw.viewport 0 0 w h;
2094 let firsttime = state.geomcmds == firstgeomcmds in
2095 if not firsttime && nogeomcmds state.geomcmds
2096 then state.anchor <- getanchor ();
2098 state.winw <- w;
2099 let w = truncate (float w *. conf.zoom) - state.scrollw in
2100 let w = max w 2 in
2101 state.winh <- h;
2102 setfontsize fstate.fontsize;
2103 GlMat.mode `modelview;
2104 GlMat.load_identity ();
2106 GlMat.mode `projection;
2107 GlMat.load_identity ();
2108 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2109 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2110 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2112 let relx =
2113 if conf.zoom <= 1.0
2114 then 0.0
2115 else float state.x /. float state.w
2117 invalidate "geometry"
2118 (fun () ->
2119 state.w <- w;
2120 if not firsttime
2121 then state.x <- truncate (relx *. float w);
2122 let w =
2123 match conf.columns with
2124 | Csingle _ -> w
2125 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2126 | Csplit (c, _) -> w * c
2128 wcmd "geometry %d %d" w h);
2131 let enttext () =
2132 let len = String.length state.text in
2133 let drawstring s =
2134 let hscrollh =
2135 match state.mode with
2136 | Textentry _
2137 | View ->
2138 let h, _, _ = state.uioh#scrollpw in
2140 | _ -> 0
2142 let rect x w =
2143 GlDraw.rect
2144 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2145 (x+.w, float (state.winh - hscrollh))
2148 let w = float (state.winw - state.scrollw - 1) in
2149 if state.progress >= 0.0 && state.progress < 1.0
2150 then (
2151 GlDraw.color (0.3, 0.3, 0.3);
2152 let w1 = w *. state.progress in
2153 rect 0.0 w1;
2154 GlDraw.color (0.0, 0.0, 0.0);
2155 rect w1 (w-.w1)
2157 else (
2158 GlDraw.color (0.0, 0.0, 0.0);
2159 rect 0.0 w;
2162 GlDraw.color (1.0, 1.0, 1.0);
2163 drawstring fstate.fontsize
2164 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2166 let s =
2167 match state.mode with
2168 | Textentry ((prefix, text, _, _, _, _), _) ->
2169 let s =
2170 if len > 0
2171 then
2172 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2173 else
2174 Printf.sprintf "%s%s_" prefix text
2178 | _ -> state.text
2180 let s =
2181 if state.newerrmsgs
2182 then (
2183 if not (istextentry state.mode)
2184 then
2185 let s1 = "(press 'e' to review error messasges)" in
2186 if String.length s > 0 then s ^ " " ^ s1 else s1
2187 else s
2189 else s
2191 if String.length s > 0
2192 then drawstring s
2195 let gctiles () =
2196 let len = Queue.length state.tilelru in
2197 let layout = lazy (
2198 match state.throttle with
2199 | None ->
2200 if conf.preload
2201 then preloadlayout state.y
2202 else state.layout
2203 | Some (layout, _, _) ->
2204 layout
2205 ) in
2206 let rec loop qpos =
2207 if state.memused <= conf.memlimit
2208 then ()
2209 else (
2210 if qpos < len
2211 then
2212 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2213 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2214 let (_, pw, ph, _) = getpagedim n in
2216 gen = state.gen
2217 && colorspace = conf.colorspace
2218 && angle = conf.angle
2219 && pagew = pw
2220 && pageh = ph
2221 && (
2222 let x = col*conf.tilew
2223 and y = row*conf.tileh in
2224 tilevisible (Lazy.force_val layout) n x y
2226 then Queue.push lruitem state.tilelru
2227 else (
2228 freepbo p;
2229 wcmd "freetile %s" p;
2230 state.memused <- state.memused - s;
2231 state.uioh#infochanged Memused;
2232 Hashtbl.remove state.tilemap k;
2234 loop (qpos+1)
2237 loop 0
2240 let flushtiles () =
2241 Queue.iter (fun (k, p, s) ->
2242 wcmd "freetile %s" p;
2243 state.memused <- state.memused - s;
2244 state.uioh#infochanged Memused;
2245 Hashtbl.remove state.tilemap k;
2246 ) state.tilelru;
2247 Queue.clear state.tilelru;
2248 load state.layout;
2251 let logcurrently = function
2252 | Idle -> dolog "Idle"
2253 | Loading (l, gen) ->
2254 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2255 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2256 dolog
2257 "Tiling %d[%d,%d] page=%s cs=%s angle"
2258 l.pageno col row pageopaque
2259 (colorspace_to_string colorspace)
2261 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2262 angle gen conf.angle state.gen
2263 tilew tileh
2264 conf.tilew conf.tileh
2266 | Outlining _ ->
2267 dolog "outlining"
2270 let splitatspace =
2271 let r = Str.regexp " " in
2272 fun s -> Str.bounded_split r s 2;
2275 let act cmds =
2276 (* dolog "%S" cmds; *)
2277 let cl = splitatspace cmds in
2278 let scan s fmt f =
2279 try Scanf.sscanf s fmt f
2280 with exn ->
2281 dolog "error processing '%S': %s" cmds (Printexc.to_string exn);
2282 exit 1
2284 match cl with
2285 | "clear" :: [] ->
2286 state.uioh#infochanged Pdim;
2287 state.pdims <- [];
2289 | "clearrects" :: [] ->
2290 state.rects <- state.rects1;
2291 G.postRedisplay "clearrects";
2293 | "continue" :: args :: [] ->
2294 let n = scan args "%u" (fun n -> n) in
2295 state.pagecount <- n;
2296 begin match state.currently with
2297 | Outlining l ->
2298 state.currently <- Idle;
2299 state.outlines <- Array.of_list (List.rev l)
2300 | _ -> ()
2301 end;
2303 let cur, cmds = state.geomcmds in
2304 if String.length cur = 0
2305 then failwith "umpossible";
2307 begin match List.rev cmds with
2308 | [] ->
2309 state.geomcmds <- "", [];
2310 represent ();
2311 | (s, f) :: rest ->
2312 f ();
2313 state.geomcmds <- s, List.rev rest;
2314 end;
2315 if conf.maxwait = None
2316 then G.postRedisplay "continue";
2318 | "title" :: args :: [] ->
2319 Wsi.settitle args
2321 | "msg" :: args :: [] ->
2322 showtext ' ' args
2324 | "vmsg" :: args :: [] ->
2325 if conf.verbose
2326 then showtext ' ' args
2328 | "emsg" :: args :: [] ->
2329 Buffer.add_string state.errmsgs args;
2330 state.newerrmsgs <- true;
2331 G.postRedisplay "error message"
2333 | "progress" :: args :: [] ->
2334 let progress, text =
2335 scan args "%f %n"
2336 (fun f pos ->
2337 f, String.sub args pos (String.length args - pos))
2339 state.text <- text;
2340 state.progress <- progress;
2341 G.postRedisplay "progress"
2343 | "firstmatch" :: args :: [] ->
2344 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2345 scan args "%u %d %f %f %f %f %f %f %f %f"
2346 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2347 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2349 let y = (getpagey pageno) + truncate y0 in
2350 addnav ();
2351 gotoy y;
2352 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2354 | "match" :: args :: [] ->
2355 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2356 scan args "%u %d %f %f %f %f %f %f %f %f"
2357 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2358 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2360 state.rects1 <-
2361 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2363 | "page" :: args :: [] ->
2364 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2365 begin match state.currently with
2366 | Loading (l, gen) ->
2367 vlog "page %d took %f sec" l.pageno t;
2368 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2369 begin match state.throttle with
2370 | None ->
2371 let preloadedpages =
2372 if conf.preload
2373 then preloadlayout state.y
2374 else state.layout
2376 let evict () =
2377 let module IntSet =
2378 Set.Make (struct type t = int let compare = (-) end) in
2379 let set =
2380 List.fold_left (fun s l -> IntSet.add l.pageno s)
2381 IntSet.empty preloadedpages
2383 let evictedpages =
2384 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2385 if not (IntSet.mem pageno set)
2386 then (
2387 wcmd "freepage %s" opaque;
2388 key :: accu
2390 else accu
2391 ) state.pagemap []
2393 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2395 evict ();
2396 state.currently <- Idle;
2397 if gen = state.gen
2398 then (
2399 tilepage l.pageno pageopaque state.layout;
2400 load state.layout;
2401 load preloadedpages;
2402 if pagevisible state.layout l.pageno
2403 && layoutready state.layout
2404 then G.postRedisplay "page";
2407 | Some (layout, _, _) ->
2408 state.currently <- Idle;
2409 tilepage l.pageno pageopaque layout;
2410 load state.layout
2411 end;
2413 | _ ->
2414 dolog "Inconsistent loading state";
2415 logcurrently state.currently;
2416 exit 1
2419 | "tile" :: args :: [] ->
2420 let (x, y, opaque, size, t) =
2421 scan args "%u %u %s %u %f"
2422 (fun x y p size t -> (x, y, p, size, t))
2424 begin match state.currently with
2425 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2426 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2428 unmappbo opaque;
2429 if tilew != conf.tilew || tileh != conf.tileh
2430 then (
2431 wcmd "freetile %s" opaque;
2432 state.currently <- Idle;
2433 load state.layout;
2435 else (
2436 puttileopaque l col row gen cs angle opaque size t;
2437 state.memused <- state.memused + size;
2438 state.uioh#infochanged Memused;
2439 gctiles ();
2440 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2441 opaque, size) state.tilelru;
2443 let layout =
2444 match state.throttle with
2445 | None -> state.layout
2446 | Some (layout, _, _) -> layout
2449 state.currently <- Idle;
2450 if gen = state.gen
2451 && conf.colorspace = cs
2452 && conf.angle = angle
2453 && tilevisible layout l.pageno x y
2454 then conttiling l.pageno pageopaque;
2456 begin match state.throttle with
2457 | None ->
2458 if state.wthack
2459 then state.wthack <- not (layoutready state.layout);
2460 preload state.layout;
2461 if gen = state.gen
2462 && conf.colorspace = cs
2463 && conf.angle = angle
2464 && tilevisible state.layout l.pageno x y
2465 then G.postRedisplay "tile nothrottle";
2467 | Some (layout, y, _) ->
2468 let ready = layoutready layout in
2469 if ready
2470 then (
2471 state.wthack <- false;
2472 state.y <- y;
2473 state.layout <- layout;
2474 state.throttle <- None;
2475 G.postRedisplay "throttle";
2477 else load layout;
2478 end;
2481 | _ ->
2482 dolog "Inconsistent tiling state";
2483 logcurrently state.currently;
2484 exit 1
2487 | "pdim" :: args :: [] ->
2488 let pdim =
2489 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2491 state.uioh#infochanged Pdim;
2492 state.pdims <- pdim :: state.pdims
2494 | "o" :: args :: [] ->
2495 let (l, n, t, h, pos) =
2496 scan args "%u %u %d %u %n"
2497 (fun l n t h pos -> l, n, t, h, pos)
2499 let s = String.sub args pos (String.length args - pos) in
2500 let outline = (s, l, (n, float t /. float h, 0.0)) in
2501 begin match state.currently with
2502 | Outlining outlines ->
2503 state.currently <- Outlining (outline :: outlines)
2504 | Idle ->
2505 state.currently <- Outlining [outline]
2506 | currently ->
2507 dolog "invalid outlining state";
2508 logcurrently currently
2511 | "a" :: args :: [] ->
2512 let (n, t, h) =
2513 scan args "%u %u %d" (fun n t h -> n, t, h)
2515 let top, dtop =
2516 if conf.presentation
2517 then (0.0, 1.0)
2518 else (float t /. float h, 0.0)
2520 state.anchor <- (n, top, dtop)
2522 | "info" :: args :: [] ->
2523 state.docinfo <- (1, args) :: state.docinfo
2525 | "infoend" :: [] ->
2526 state.uioh#infochanged Docinfo;
2527 state.docinfo <- List.rev state.docinfo
2529 | _ ->
2530 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2533 let onhist cb =
2534 let rc = cb.rc in
2535 let action = function
2536 | HCprev -> cbget cb ~-1
2537 | HCnext -> cbget cb 1
2538 | HCfirst -> cbget cb ~-(cb.rc)
2539 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2540 and cancel () = cb.rc <- rc
2541 in (action, cancel)
2544 let search pattern forward =
2545 if String.length pattern > 0
2546 then
2547 let pn, py =
2548 match state.layout with
2549 | [] -> 0, 0
2550 | l :: _ ->
2551 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2553 wcmd "search %d %d %d %d,%s\000"
2554 (btod conf.icase) pn py (btod forward) pattern;
2557 let intentry text key =
2558 let c =
2559 if key >= 32 && key < 127
2560 then Char.chr key
2561 else '\000'
2563 match c with
2564 | '0' .. '9' ->
2565 let text = addchar text c in
2566 TEcont text
2568 | _ ->
2569 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2570 TEcont text
2573 let linknentry text key =
2574 let c =
2575 if key >= 32 && key < 127
2576 then Char.chr key
2577 else '\000'
2579 match c with
2580 | 'a' .. 'z' ->
2581 let text = addchar text c in
2582 TEcont text
2584 | _ ->
2585 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2586 TEcont text
2589 let linkndone f s =
2590 if String.length s > 0
2591 then (
2592 let n =
2593 let l = String.length s in
2594 let rec loop pos n = if pos = l then n else
2595 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2596 loop (pos+1) (n*26 + m)
2597 in loop 0 0
2599 let rec loop n = function
2600 | [] -> ()
2601 | l :: rest ->
2602 match getopaque l.pageno with
2603 | None -> loop n rest
2604 | Some opaque ->
2605 let m = getlinkcount opaque in
2606 if n < m
2607 then (
2608 let under = getlink opaque n in
2609 f under
2611 else loop (n-m) rest
2613 loop n state.layout;
2617 let textentry text key =
2618 if key land 0xff00 = 0xff00
2619 then TEcont text
2620 else TEcont (text ^ Wsi.toutf8 key)
2623 let reqlayout angle proportional =
2624 match state.throttle with
2625 | None ->
2626 if nogeomcmds state.geomcmds
2627 then state.anchor <- getanchor ();
2628 conf.angle <- angle mod 360;
2629 if conf.angle != 0
2630 then (
2631 match state.mode with
2632 | LinkNav _ -> state.mode <- View
2633 | _ -> ()
2635 conf.proportional <- proportional;
2636 invalidate "reqlayout"
2637 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2638 | _ -> ()
2641 let settrim trimmargins trimfuzz =
2642 if nogeomcmds state.geomcmds
2643 then state.anchor <- getanchor ();
2644 conf.trimmargins <- trimmargins;
2645 conf.trimfuzz <- trimfuzz;
2646 let x0, y0, x1, y1 = trimfuzz in
2647 invalidate "settrim"
2648 (fun () ->
2649 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2650 flushpages ();
2653 let setzoom zoom =
2654 match state.throttle with
2655 | None ->
2656 let zoom = max 0.01 zoom in
2657 if zoom <> conf.zoom
2658 then (
2659 state.prevzoom <- conf.zoom;
2660 conf.zoom <- zoom;
2661 reshape state.winw state.winh;
2662 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2665 | Some (layout, y, started) ->
2666 let time =
2667 match conf.maxwait with
2668 | None -> 0.0
2669 | Some t -> t
2671 let dt = now () -. started in
2672 if dt > time
2673 then (
2674 state.y <- y;
2675 load layout;
2679 let setcolumns mode columns coverA coverB =
2680 state.prevcolumns <- Some (conf.columns, conf.zoom);
2681 if columns < 0
2682 then (
2683 if isbirdseye mode
2684 then showtext '!' "split mode doesn't work in bird's eye"
2685 else (
2686 conf.columns <- Csplit (-columns, [||]);
2687 state.x <- 0;
2688 conf.zoom <- 1.0;
2691 else (
2692 if columns < 2
2693 then (
2694 conf.columns <- Csingle [||];
2695 state.x <- 0;
2696 setzoom 1.0;
2698 else (
2699 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2700 conf.zoom <- 1.0;
2703 reshape state.winw state.winh;
2706 let enterbirdseye () =
2707 let zoom = float conf.thumbw /. float state.winw in
2708 let birdseyepageno =
2709 let cy = state.winh / 2 in
2710 let fold = function
2711 | [] -> 0
2712 | l :: rest ->
2713 let rec fold best = function
2714 | [] -> best.pageno
2715 | l :: rest ->
2716 let d = cy - (l.pagedispy + l.pagevh/2)
2717 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2718 if abs d < abs dbest
2719 then fold l rest
2720 else best.pageno
2721 in fold l rest
2723 fold state.layout
2725 state.mode <- Birdseye (
2726 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2728 conf.zoom <- zoom;
2729 conf.presentation <- false;
2730 conf.interpagespace <- 10;
2731 conf.hlinks <- false;
2732 state.x <- 0;
2733 state.mstate <- Mnone;
2734 conf.maxwait <- None;
2735 conf.columns <- (
2736 match conf.beyecolumns with
2737 | Some c ->
2738 conf.zoom <- 1.0;
2739 Cmulti ((c, 0, 0), [||])
2740 | None -> Csingle [||]
2742 Wsi.setcursor Wsi.CURSOR_INHERIT;
2743 if conf.verbose
2744 then
2745 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2746 (100.0*.zoom)
2747 else
2748 state.text <- ""
2750 reshape state.winw state.winh;
2753 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2754 state.mode <- View;
2755 conf.zoom <- c.zoom;
2756 conf.presentation <- c.presentation;
2757 conf.interpagespace <- c.interpagespace;
2758 conf.maxwait <- c.maxwait;
2759 conf.hlinks <- c.hlinks;
2760 conf.beyecolumns <- (
2761 match conf.columns with
2762 | Cmulti ((c, _, _), _) -> Some c
2763 | Csingle _ -> None
2764 | Csplit _ -> failwith "leaving bird's eye split mode"
2766 conf.columns <- (
2767 match c.columns with
2768 | Cmulti (c, _) -> Cmulti (c, [||])
2769 | Csingle _ -> Csingle [||]
2770 | Csplit (c, _) -> Csplit (c, [||])
2772 state.x <- leftx;
2773 if conf.verbose
2774 then
2775 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2776 (100.0*.conf.zoom)
2778 reshape state.winw state.winh;
2779 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2782 let togglebirdseye () =
2783 match state.mode with
2784 | Birdseye vals -> leavebirdseye vals true
2785 | View -> enterbirdseye ()
2786 | _ -> ()
2789 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2790 let pageno = max 0 (pageno - incr) in
2791 let rec loop = function
2792 | [] -> gotopage1 pageno 0
2793 | l :: _ when l.pageno = pageno ->
2794 if l.pagedispy >= 0 && l.pagey = 0
2795 then G.postRedisplay "upbirdseye"
2796 else gotopage1 pageno 0
2797 | _ :: rest -> loop rest
2799 loop state.layout;
2800 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2803 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2804 let pageno = min (state.pagecount - 1) (pageno + incr) in
2805 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2806 let rec loop = function
2807 | [] ->
2808 let y, h = getpageyh pageno in
2809 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2810 gotoy (clamp dy)
2811 | l :: _ when l.pageno = pageno ->
2812 if l.pagevh != l.pageh
2813 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2814 else G.postRedisplay "downbirdseye"
2815 | _ :: rest -> loop rest
2817 loop state.layout
2820 let optentry mode _ key =
2821 let btos b = if b then "on" else "off" in
2822 if key >= 32 && key < 127
2823 then
2824 let c = Char.chr key in
2825 match c with
2826 | 's' ->
2827 let ondone s =
2828 try conf.scrollstep <- int_of_string s with exc ->
2829 state.text <- Printf.sprintf "bad integer `%s': %s"
2830 s (Printexc.to_string exc)
2832 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2834 | 'A' ->
2835 let ondone s =
2837 conf.autoscrollstep <- int_of_string s;
2838 if state.autoscroll <> None
2839 then state.autoscroll <- Some conf.autoscrollstep
2840 with exc ->
2841 state.text <- Printf.sprintf "bad integer `%s': %s"
2842 s (Printexc.to_string exc)
2844 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2846 | 'C' ->
2847 let ondone s =
2849 let n, a, b = multicolumns_of_string s in
2850 setcolumns mode n a b;
2851 with exc ->
2852 state.text <- Printf.sprintf "bad columns `%s': %s"
2853 s (Printexc.to_string exc)
2855 TEswitch ("columns: ", "", None, textentry, ondone, true)
2857 | 'Z' ->
2858 let ondone s =
2860 let zoom = float (int_of_string s) /. 100.0 in
2861 setzoom zoom
2862 with exc ->
2863 state.text <- Printf.sprintf "bad integer `%s': %s"
2864 s (Printexc.to_string exc)
2866 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2868 | 't' ->
2869 let ondone s =
2871 conf.thumbw <- bound (int_of_string s) 2 4096;
2872 state.text <-
2873 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2874 begin match mode with
2875 | Birdseye beye ->
2876 leavebirdseye beye false;
2877 enterbirdseye ();
2878 | _ -> ();
2880 with exc ->
2881 state.text <- Printf.sprintf "bad integer `%s': %s"
2882 s (Printexc.to_string exc)
2884 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2886 | 'R' ->
2887 let ondone s =
2888 match try
2889 Some (int_of_string s)
2890 with exc ->
2891 state.text <- Printf.sprintf "bad integer `%s': %s"
2892 s (Printexc.to_string exc);
2893 None
2894 with
2895 | Some angle -> reqlayout angle conf.proportional
2896 | None -> ()
2898 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2900 | 'i' ->
2901 conf.icase <- not conf.icase;
2902 TEdone ("case insensitive search " ^ (btos conf.icase))
2904 | 'p' ->
2905 conf.preload <- not conf.preload;
2906 gotoy state.y;
2907 TEdone ("preload " ^ (btos conf.preload))
2909 | 'v' ->
2910 conf.verbose <- not conf.verbose;
2911 TEdone ("verbose " ^ (btos conf.verbose))
2913 | 'd' ->
2914 conf.debug <- not conf.debug;
2915 TEdone ("debug " ^ (btos conf.debug))
2917 | 'h' ->
2918 conf.maxhfit <- not conf.maxhfit;
2919 state.maxy <- calcheight ();
2920 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2922 | 'c' ->
2923 conf.crophack <- not conf.crophack;
2924 TEdone ("crophack " ^ btos conf.crophack)
2926 | 'a' ->
2927 let s =
2928 match conf.maxwait with
2929 | None ->
2930 conf.maxwait <- Some infinity;
2931 "always wait for page to complete"
2932 | Some _ ->
2933 conf.maxwait <- None;
2934 "show placeholder if page is not ready"
2936 TEdone s
2938 | 'f' ->
2939 conf.underinfo <- not conf.underinfo;
2940 TEdone ("underinfo " ^ btos conf.underinfo)
2942 | 'P' ->
2943 conf.savebmarks <- not conf.savebmarks;
2944 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2946 | 'S' ->
2947 let ondone s =
2949 let pageno, py =
2950 match state.layout with
2951 | [] -> 0, 0
2952 | l :: _ ->
2953 l.pageno, l.pagey
2955 conf.interpagespace <- int_of_string s;
2956 docolumns conf.columns;
2957 state.maxy <- calcheight ();
2958 let y = getpagey pageno in
2959 gotoy (y + py)
2960 with exc ->
2961 state.text <- Printf.sprintf "bad integer `%s': %s"
2962 s (Printexc.to_string exc)
2964 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2966 | 'l' ->
2967 reqlayout conf.angle (not conf.proportional);
2968 TEdone ("proportional display " ^ btos conf.proportional)
2970 | 'T' ->
2971 settrim (not conf.trimmargins) conf.trimfuzz;
2972 TEdone ("trim margins " ^ btos conf.trimmargins)
2974 | 'I' ->
2975 conf.invert <- not conf.invert;
2976 TEdone ("invert colors " ^ btos conf.invert)
2978 | 'x' ->
2979 let ondone s =
2980 cbput state.hists.sel s;
2981 conf.selcmd <- s;
2983 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2984 textentry, ondone, true)
2986 | _ ->
2987 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2988 TEstop
2989 else
2990 TEcont state.text
2993 class type lvsource = object
2994 method getitemcount : int
2995 method getitem : int -> (string * int)
2996 method hasaction : int -> bool
2997 method exit :
2998 uioh:uioh ->
2999 cancel:bool ->
3000 active:int ->
3001 first:int ->
3002 pan:int ->
3003 qsearch:string ->
3004 uioh option
3005 method getactive : int
3006 method getfirst : int
3007 method getqsearch : string
3008 method setqsearch : string -> unit
3009 method getpan : int
3010 end;;
3012 class virtual lvsourcebase = object
3013 val mutable m_active = 0
3014 val mutable m_first = 0
3015 val mutable m_qsearch = ""
3016 val mutable m_pan = 0
3017 method getactive = m_active
3018 method getfirst = m_first
3019 method getqsearch = m_qsearch
3020 method getpan = m_pan
3021 method setqsearch s = m_qsearch <- s
3022 end;;
3024 let withoutlastutf8 s =
3025 let len = String.length s in
3026 if len = 0
3027 then s
3028 else
3029 let rec find pos =
3030 if pos = 0
3031 then pos
3032 else
3033 let b = Char.code s.[pos] in
3034 if b land 0b11000000 = 0b11000000
3035 then pos
3036 else find (pos-1)
3038 let first =
3039 if Char.code s.[len-1] land 0x80 = 0
3040 then len-1
3041 else find (len-1)
3043 String.sub s 0 first;
3046 let textentrykeyboard
3047 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3048 let key =
3049 if key >= 0xffb0 && key <= 0xffb9
3050 then key - 0xffb0 + 48 else key
3052 let enttext te =
3053 state.mode <- Textentry (te, onleave);
3054 state.text <- "";
3055 enttext ();
3056 G.postRedisplay "textentrykeyboard enttext";
3058 let histaction cmd =
3059 match opthist with
3060 | None -> ()
3061 | Some (action, _) ->
3062 state.mode <- Textentry (
3063 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3065 G.postRedisplay "textentry histaction"
3067 match key with
3068 | 0xff08 -> (* backspace *)
3069 let s = withoutlastutf8 text in
3070 let len = String.length s in
3071 if cancelonempty && len = 0
3072 then (
3073 onleave Cancel;
3074 G.postRedisplay "textentrykeyboard after cancel";
3076 else (
3077 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3080 | 0xff0d | 0xff8d -> (* (kp) enter *)
3081 ondone text;
3082 onleave Confirm;
3083 G.postRedisplay "textentrykeyboard after confirm"
3085 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3086 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3087 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3088 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3090 | 0xff1b -> (* escape*)
3091 if String.length text = 0
3092 then (
3093 begin match opthist with
3094 | None -> ()
3095 | Some (_, onhistcancel) -> onhistcancel ()
3096 end;
3097 onleave Cancel;
3098 state.text <- "";
3099 G.postRedisplay "textentrykeyboard after cancel2"
3101 else (
3102 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3105 | 0xff9f | 0xffff -> () (* delete *)
3107 | _ when key != 0
3108 && key land 0xff00 != 0xff00 (* keyboard *)
3109 && key land 0xfe00 != 0xfe00 (* xkb *)
3110 && key land 0xfd00 != 0xfd00 (* 3270 *)
3112 begin match onkey text key with
3113 | TEdone text ->
3114 ondone text;
3115 onleave Confirm;
3116 G.postRedisplay "textentrykeyboard after confirm2";
3118 | TEcont text ->
3119 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3121 | TEstop ->
3122 onleave Cancel;
3123 G.postRedisplay "textentrykeyboard after cancel3"
3125 | TEswitch te ->
3126 state.mode <- Textentry (te, onleave);
3127 G.postRedisplay "textentrykeyboard switch";
3128 end;
3130 | _ ->
3131 vlog "unhandled key %s" (Wsi.keyname key)
3134 let firstof first active =
3135 if first > active || abs (first - active) > fstate.maxrows - 1
3136 then max 0 (active - (fstate.maxrows/2))
3137 else first
3140 let calcfirst first active =
3141 if active > first
3142 then
3143 let rows = active - first in
3144 if rows > fstate.maxrows then active - fstate.maxrows else first
3145 else active
3148 let scrollph y maxy =
3149 let sh = (float (maxy + state.winh) /. float state.winh) in
3150 let sh = float state.winh /. sh in
3151 let sh = max sh (float conf.scrollh) in
3153 let percent =
3154 if y = state.maxy
3155 then 1.0
3156 else float y /. float maxy
3158 let position = (float state.winh -. sh) *. percent in
3160 let position =
3161 if position +. sh > float state.winh
3162 then float state.winh -. sh
3163 else position
3165 position, sh;
3168 let coe s = (s :> uioh);;
3170 class listview ~(source:lvsource) ~trusted ~modehash =
3171 object (self)
3172 val m_pan = source#getpan
3173 val m_first = source#getfirst
3174 val m_active = source#getactive
3175 val m_qsearch = source#getqsearch
3176 val m_prev_uioh = state.uioh
3178 method private elemunder y =
3179 let n = y / (fstate.fontsize+1) in
3180 if m_first + n < source#getitemcount
3181 then (
3182 if source#hasaction (m_first + n)
3183 then Some (m_first + n)
3184 else None
3186 else None
3188 method display =
3189 Gl.enable `blend;
3190 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3191 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3192 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3193 GlDraw.color (1., 1., 1.);
3194 Gl.enable `texture_2d;
3195 let fs = fstate.fontsize in
3196 let nfs = fs + 1 in
3197 let ww = fstate.wwidth in
3198 let tabw = 30.0*.ww in
3199 let itemcount = source#getitemcount in
3200 let rec loop row =
3201 if (row - m_first) > fstate.maxrows
3202 then ()
3203 else (
3204 if row >= 0 && row < itemcount
3205 then (
3206 let (s, level) = source#getitem row in
3207 let y = (row - m_first) * nfs in
3208 let x = 5.0 +. float (level + m_pan) *. ww in
3209 if row = m_active
3210 then (
3211 Gl.disable `texture_2d;
3212 GlDraw.polygon_mode `both `line;
3213 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3214 GlDraw.rect (1., float (y + 1))
3215 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3216 GlDraw.polygon_mode `both `fill;
3217 GlDraw.color (1., 1., 1.);
3218 Gl.enable `texture_2d;
3221 let drawtabularstring s =
3222 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3223 if trusted
3224 then
3225 let tabpos = try String.index s '\t' with Not_found -> -1 in
3226 if tabpos > 0
3227 then
3228 let len = String.length s - tabpos - 1 in
3229 let s1 = String.sub s 0 tabpos
3230 and s2 = String.sub s (tabpos + 1) len in
3231 let nx = drawstr x s1 in
3232 let sw = nx -. x in
3233 let x = x +. (max tabw sw) in
3234 drawstr x s2
3235 else
3236 drawstr x s
3237 else
3238 drawstr x s
3240 let _ = drawtabularstring s in
3241 loop (row+1)
3245 loop m_first;
3246 Gl.disable `blend;
3247 Gl.disable `texture_2d;
3249 method updownlevel incr =
3250 let len = source#getitemcount in
3251 let curlevel =
3252 if m_active >= 0 && m_active < len
3253 then snd (source#getitem m_active)
3254 else -1
3256 let rec flow i =
3257 if i = len then i-1 else if i = -1 then 0 else
3258 let _, l = source#getitem i in
3259 if l != curlevel then i else flow (i+incr)
3261 let active = flow m_active in
3262 let first = calcfirst m_first active in
3263 G.postRedisplay "outline updownlevel";
3264 {< m_active = active; m_first = first >}
3266 method private key1 key mask =
3267 let set1 active first qsearch =
3268 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3270 let search active pattern incr =
3271 let dosearch re =
3272 let rec loop n =
3273 if n >= 0 && n < source#getitemcount
3274 then (
3275 let s, _ = source#getitem n in
3277 (try ignore (Str.search_forward re s 0); true
3278 with Not_found -> false)
3279 then Some n
3280 else loop (n + incr)
3282 else None
3284 loop active
3287 let re = Str.regexp_case_fold pattern in
3288 dosearch re
3289 with Failure s ->
3290 state.text <- s;
3291 None
3293 let itemcount = source#getitemcount in
3294 let find start incr =
3295 let rec find i =
3296 if i = -1 || i = itemcount
3297 then -1
3298 else (
3299 if source#hasaction i
3300 then i
3301 else find (i + incr)
3304 find start
3306 let set active first =
3307 let first = bound first 0 (itemcount - fstate.maxrows) in
3308 state.text <- "";
3309 coe {< m_active = active; m_first = first >}
3311 let navigate incr =
3312 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3313 let active, first =
3314 let incr1 = if incr > 0 then 1 else -1 in
3315 if isvisible m_first m_active
3316 then
3317 let next =
3318 let next = m_active + incr in
3319 let next =
3320 if next < 0 || next >= itemcount
3321 then -1
3322 else find next incr1
3324 if next = -1 || abs (m_active - next) > fstate.maxrows
3325 then -1
3326 else next
3328 if next = -1
3329 then
3330 let first = m_first + incr in
3331 let first = bound first 0 (itemcount - 1) in
3332 let next =
3333 let next = m_active + incr in
3334 let next = bound next 0 (itemcount - 1) in
3335 find next ~-incr1
3337 let active = if next = -1 then m_active else next in
3338 active, first
3339 else
3340 let first = min next m_first in
3341 let first =
3342 if abs (next - first) > fstate.maxrows
3343 then first + incr
3344 else first
3346 next, first
3347 else
3348 let first = m_first + incr in
3349 let first = bound first 0 (itemcount - 1) in
3350 let active =
3351 let next = m_active + incr in
3352 let next = bound next 0 (itemcount - 1) in
3353 let next = find next incr1 in
3354 let active =
3355 if next = -1 || abs (m_active - first) > fstate.maxrows
3356 then (
3357 let active = if m_active = -1 then next else m_active in
3358 active
3360 else next
3362 if isvisible first active
3363 then active
3364 else -1
3366 active, first
3368 G.postRedisplay "listview navigate";
3369 set active first;
3371 match key with
3372 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3373 let incr = if key = 0x72 then -1 else 1 in
3374 let active, first =
3375 match search (m_active + incr) m_qsearch incr with
3376 | None ->
3377 state.text <- m_qsearch ^ " [not found]";
3378 m_active, m_first
3379 | Some active ->
3380 state.text <- m_qsearch;
3381 active, firstof m_first active
3383 G.postRedisplay "listview ctrl-r/s";
3384 set1 active first m_qsearch;
3386 | 0xff08 -> (* backspace *)
3387 if String.length m_qsearch = 0
3388 then coe self
3389 else (
3390 let qsearch = withoutlastutf8 m_qsearch in
3391 let len = String.length qsearch in
3392 if len = 0
3393 then (
3394 state.text <- "";
3395 G.postRedisplay "listview empty qsearch";
3396 set1 m_active m_first "";
3398 else
3399 let active, first =
3400 match search m_active qsearch ~-1 with
3401 | None ->
3402 state.text <- qsearch ^ " [not found]";
3403 m_active, m_first
3404 | Some active ->
3405 state.text <- qsearch;
3406 active, firstof m_first active
3408 G.postRedisplay "listview backspace qsearch";
3409 set1 active first qsearch
3412 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3413 let pattern = m_qsearch ^ Wsi.toutf8 key in
3414 let active, first =
3415 match search m_active pattern 1 with
3416 | None ->
3417 state.text <- pattern ^ " [not found]";
3418 m_active, m_first
3419 | Some active ->
3420 state.text <- pattern;
3421 active, firstof m_first active
3423 G.postRedisplay "listview qsearch add";
3424 set1 active first pattern;
3426 | 0xff1b -> (* escape *)
3427 state.text <- "";
3428 if String.length m_qsearch = 0
3429 then (
3430 G.postRedisplay "list view escape";
3431 begin
3432 match
3433 source#exit (coe self) true m_active m_first m_pan m_qsearch
3434 with
3435 | None -> m_prev_uioh
3436 | Some uioh -> uioh
3439 else (
3440 G.postRedisplay "list view kill qsearch";
3441 source#setqsearch "";
3442 coe {< m_qsearch = "" >}
3445 | 0xff0d | 0xff8d -> (* (kp) enter *)
3446 state.text <- "";
3447 let self = {< m_qsearch = "" >} in
3448 source#setqsearch "";
3449 let opt =
3450 G.postRedisplay "listview enter";
3451 if m_active >= 0 && m_active < source#getitemcount
3452 then (
3453 source#exit (coe self) false m_active m_first m_pan "";
3455 else (
3456 source#exit (coe self) true m_active m_first m_pan "";
3459 begin match opt with
3460 | None -> m_prev_uioh
3461 | Some uioh -> uioh
3464 | 0xff9f | 0xffff -> (* (kp) delete *)
3465 coe self
3467 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3468 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3469 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3470 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3472 | 0xff53 | 0xff98 -> (* (kp) right *)
3473 state.text <- "";
3474 G.postRedisplay "listview right";
3475 coe {< m_pan = m_pan - 1 >}
3477 | 0xff51 | 0xff96 -> (* (kp) left *)
3478 state.text <- "";
3479 G.postRedisplay "listview left";
3480 coe {< m_pan = m_pan + 1 >}
3482 | 0xff50 | 0xff95 -> (* (kp) home *)
3483 let active = find 0 1 in
3484 G.postRedisplay "listview home";
3485 set active 0;
3487 | 0xff57 | 0xff9c -> (* (kp) end *)
3488 let first = max 0 (itemcount - fstate.maxrows) in
3489 let active = find (itemcount - 1) ~-1 in
3490 G.postRedisplay "listview end";
3491 set active first;
3493 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3494 coe self
3496 | _ ->
3497 dolog "listview unknown key %#x" key; coe self
3499 method key key mask =
3500 match state.mode with
3501 | Textentry te -> textentrykeyboard key mask te; coe self
3502 | _ -> self#key1 key mask
3504 method button button down x y _ =
3505 let opt =
3506 match button with
3507 | 1 when x > state.winw - conf.scrollbw ->
3508 G.postRedisplay "listview scroll";
3509 if down
3510 then
3511 let _, position, sh = self#scrollph in
3512 if y > truncate position && y < truncate (position +. sh)
3513 then (
3514 state.mstate <- Mscrolly;
3515 Some (coe self)
3517 else
3518 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3519 let first = truncate (s *. float source#getitemcount) in
3520 let first = min source#getitemcount first in
3521 Some (coe {< m_first = first; m_active = first >})
3522 else (
3523 state.mstate <- Mnone;
3524 Some (coe self);
3526 | 1 when not down ->
3527 begin match self#elemunder y with
3528 | Some n ->
3529 G.postRedisplay "listview click";
3530 source#exit
3531 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3532 | _ ->
3533 Some (coe self)
3535 | n when (n == 4 || n == 5) && not down ->
3536 let len = source#getitemcount in
3537 let first =
3538 if n = 5 && m_first + fstate.maxrows >= len
3539 then
3540 m_first
3541 else
3542 let first = m_first + (if n == 4 then -1 else 1) in
3543 bound first 0 (len - 1)
3545 G.postRedisplay "listview wheel";
3546 Some (coe {< m_first = first >})
3547 | n when (n = 6 || n = 7) && not down ->
3548 let inc = m_first + (if n = 7 then -1 else 1) in
3549 G.postRedisplay "listview hwheel";
3550 Some (coe {< m_pan = m_pan + inc >})
3551 | _ ->
3552 Some (coe self)
3554 match opt with
3555 | None -> m_prev_uioh
3556 | Some uioh -> uioh
3558 method motion _ y =
3559 match state.mstate with
3560 | Mscrolly ->
3561 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3562 let first = truncate (s *. float source#getitemcount) in
3563 let first = min source#getitemcount first in
3564 G.postRedisplay "listview motion";
3565 coe {< m_first = first; m_active = first >}
3566 | _ -> coe self
3568 method pmotion x y =
3569 if x < state.winw - conf.scrollbw
3570 then
3571 let n =
3572 match self#elemunder y with
3573 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3574 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3576 let o =
3577 if n != m_active
3578 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3579 else self
3581 coe o
3582 else (
3583 Wsi.setcursor Wsi.CURSOR_INHERIT;
3584 coe self
3587 method infochanged _ = ()
3589 method scrollpw = (0, 0.0, 0.0)
3590 method scrollph =
3591 let nfs = fstate.fontsize + 1 in
3592 let y = m_first * nfs in
3593 let itemcount = source#getitemcount in
3594 let maxi = max 0 (itemcount - fstate.maxrows) in
3595 let maxy = maxi * nfs in
3596 let p, h = scrollph y maxy in
3597 conf.scrollbw, p, h
3599 method modehash = modehash
3600 end;;
3602 class outlinelistview ~source =
3603 object (self)
3604 inherit listview
3605 ~source:(source :> lvsource)
3606 ~trusted:false
3607 ~modehash:(findkeyhash conf "outline")
3608 as super
3610 method key key mask =
3611 let calcfirst first active =
3612 if active > first
3613 then
3614 let rows = active - first in
3615 let maxrows =
3616 if String.length state.text = 0
3617 then fstate.maxrows
3618 else fstate.maxrows - 2
3620 if rows > maxrows then active - maxrows else first
3621 else active
3623 let navigate incr =
3624 let active = m_active + incr in
3625 let active = bound active 0 (source#getitemcount - 1) in
3626 let first = calcfirst m_first active in
3627 G.postRedisplay "outline navigate";
3628 coe {< m_active = active; m_first = first >}
3630 let ctrl = Wsi.withctrl mask in
3631 match key with
3632 | 110 when ctrl -> (* ctrl-n *)
3633 source#narrow m_qsearch;
3634 G.postRedisplay "outline ctrl-n";
3635 coe {< m_first = 0; m_active = 0 >}
3637 | 117 when ctrl -> (* ctrl-u *)
3638 source#denarrow;
3639 G.postRedisplay "outline ctrl-u";
3640 state.text <- "";
3641 coe {< m_first = 0; m_active = 0 >}
3643 | 108 when ctrl -> (* ctrl-l *)
3644 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3645 G.postRedisplay "outline ctrl-l";
3646 coe {< m_first = first >}
3648 | 0xff9f | 0xffff -> (* (kp) delete *)
3649 source#remove m_active;
3650 G.postRedisplay "outline delete";
3651 let active = max 0 (m_active-1) in
3652 coe {< m_first = firstof m_first active;
3653 m_active = active >}
3655 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3656 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3657 | 0xff55 | 0xff9a -> (* (kp) prior *)
3658 navigate ~-(fstate.maxrows)
3659 | 0xff56 | 0xff9b -> (* (kp) next *)
3660 navigate fstate.maxrows
3662 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3663 let o =
3664 if ctrl
3665 then (
3666 G.postRedisplay "outline ctrl right";
3667 {< m_pan = m_pan + 1 >}
3669 else self#updownlevel 1
3671 coe o
3673 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3674 let o =
3675 if ctrl
3676 then (
3677 G.postRedisplay "outline ctrl left";
3678 {< m_pan = m_pan - 1 >}
3680 else self#updownlevel ~-1
3682 coe o
3684 | 0xff50 | 0xff95 -> (* (kp) home *)
3685 G.postRedisplay "outline home";
3686 coe {< m_first = 0; m_active = 0 >}
3688 | 0xff57 | 0xff9c -> (* (kp) end *)
3689 let active = source#getitemcount - 1 in
3690 let first = max 0 (active - fstate.maxrows) in
3691 G.postRedisplay "outline end";
3692 coe {< m_active = active; m_first = first >}
3694 | _ -> super#key key mask
3697 let outlinesource usebookmarks =
3698 let empty = [||] in
3699 (object
3700 inherit lvsourcebase
3701 val mutable m_items = empty
3702 val mutable m_orig_items = empty
3703 val mutable m_prev_items = empty
3704 val mutable m_narrow_pattern = ""
3705 val mutable m_hadremovals = false
3707 method getitemcount =
3708 Array.length m_items + (if m_hadremovals then 1 else 0)
3710 method getitem n =
3711 if n == Array.length m_items && m_hadremovals
3712 then
3713 ("[Confirm removal]", 0)
3714 else
3715 let s, n, _ = m_items.(n) in
3716 (s, n)
3718 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3719 ignore (uioh, first, qsearch);
3720 let confrimremoval = m_hadremovals && active = Array.length m_items in
3721 let items =
3722 if String.length m_narrow_pattern = 0
3723 then m_orig_items
3724 else m_items
3726 if not cancel
3727 then (
3728 if not confrimremoval
3729 then(
3730 let _, _, anchor = m_items.(active) in
3731 gotoghyll (getanchory anchor);
3732 m_items <- items;
3734 else (
3735 state.bookmarks <- Array.to_list m_items;
3736 m_orig_items <- m_items;
3739 else m_items <- items;
3740 m_pan <- pan;
3741 None
3743 method hasaction _ = true
3745 method greetmsg =
3746 if Array.length m_items != Array.length m_orig_items
3747 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3748 else ""
3750 method narrow pattern =
3751 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3752 match reopt with
3753 | None -> ()
3754 | Some re ->
3755 let rec loop accu n =
3756 if n = -1
3757 then (
3758 m_narrow_pattern <- pattern;
3759 m_items <- Array.of_list accu
3761 else
3762 let (s, _, _) as o = m_items.(n) in
3763 let accu =
3764 if (try ignore (Str.search_forward re s 0); true
3765 with Not_found -> false)
3766 then o :: accu
3767 else accu
3769 loop accu (n-1)
3771 loop [] (Array.length m_items - 1)
3773 method denarrow =
3774 m_orig_items <- (
3775 if usebookmarks
3776 then Array.of_list state.bookmarks
3777 else state.outlines
3779 m_items <- m_orig_items
3781 method remove m =
3782 if usebookmarks
3783 then
3784 if m >= 0 && m < Array.length m_items
3785 then (
3786 m_hadremovals <- true;
3787 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3788 let n = if n >= m then n+1 else n in
3789 m_items.(n)
3793 method reset anchor items =
3794 m_hadremovals <- false;
3795 if m_orig_items == empty || m_prev_items != items
3796 then (
3797 m_orig_items <- items;
3798 if String.length m_narrow_pattern = 0
3799 then m_items <- items;
3801 m_prev_items <- items;
3802 let rely = getanchory anchor in
3803 let active =
3804 let rec loop n best bestd =
3805 if n = Array.length m_items
3806 then best
3807 else
3808 let (_, _, anchor) = m_items.(n) in
3809 let orely = getanchory anchor in
3810 let d = abs (orely - rely) in
3811 if d < bestd
3812 then loop (n+1) n d
3813 else loop (n+1) best bestd
3815 loop 0 ~-1 max_int
3817 m_active <- active;
3818 m_first <- firstof m_first active
3819 end)
3822 let enterselector usebookmarks =
3823 let source = outlinesource usebookmarks in
3824 fun errmsg ->
3825 let outlines =
3826 if usebookmarks
3827 then Array.of_list state.bookmarks
3828 else state.outlines
3830 if Array.length outlines = 0
3831 then (
3832 showtext ' ' errmsg;
3834 else (
3835 state.text <- source#greetmsg;
3836 Wsi.setcursor Wsi.CURSOR_INHERIT;
3837 let anchor = getanchor () in
3838 source#reset anchor outlines;
3839 state.uioh <- coe (new outlinelistview ~source);
3840 G.postRedisplay "enter selector";
3844 let enteroutlinemode =
3845 let f = enterselector false in
3846 fun ()-> f "Document has no outline";
3849 let enterbookmarkmode =
3850 let f = enterselector true in
3851 fun () -> f "Document has no bookmarks (yet)";
3854 let color_of_string s =
3855 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3856 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3860 let color_to_string (r, g, b) =
3861 let r = truncate (r *. 256.0)
3862 and g = truncate (g *. 256.0)
3863 and b = truncate (b *. 256.0) in
3864 Printf.sprintf "%d/%d/%d" r g b
3867 let irect_of_string s =
3868 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3871 let irect_to_string (x0,y0,x1,y1) =
3872 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3875 let makecheckers () =
3876 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3877 following to say:
3878 converted by Issac Trotts. July 25, 2002 *)
3879 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3880 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3881 let id = GlTex.gen_texture () in
3882 GlTex.bind_texture `texture_2d id;
3883 GlPix.store (`unpack_alignment 1);
3884 GlTex.image2d image;
3885 List.iter (GlTex.parameter ~target:`texture_2d)
3886 [ `mag_filter `nearest; `min_filter `nearest ];
3890 let setcheckers enabled =
3891 match state.texid with
3892 | None ->
3893 if enabled then state.texid <- Some (makecheckers ())
3895 | Some texid ->
3896 if not enabled
3897 then (
3898 GlTex.delete_texture texid;
3899 state.texid <- None;
3903 let int_of_string_with_suffix s =
3904 let l = String.length s in
3905 let s1, shift =
3906 if l > 1
3907 then
3908 let suffix = Char.lowercase s.[l-1] in
3909 match suffix with
3910 | 'k' -> String.sub s 0 (l-1), 10
3911 | 'm' -> String.sub s 0 (l-1), 20
3912 | 'g' -> String.sub s 0 (l-1), 30
3913 | _ -> s, 0
3914 else s, 0
3916 let n = int_of_string s1 in
3917 let m = n lsl shift in
3918 if m < 0 || m < n
3919 then raise (Failure "value too large")
3920 else m
3923 let string_with_suffix_of_int n =
3924 if n = 0
3925 then "0"
3926 else
3927 let n, s =
3928 if n land ((1 lsl 30) - 1) = 0
3929 then n lsr 30, "G"
3930 else (
3931 if n land ((1 lsl 20) - 1) = 0
3932 then n lsr 20, "M"
3933 else (
3934 if n land ((1 lsl 10) - 1) = 0
3935 then n lsr 10, "K"
3936 else n, ""
3940 let rec loop s n =
3941 let h = n mod 1000 in
3942 let n = n / 1000 in
3943 if n = 0
3944 then string_of_int h ^ s
3945 else (
3946 let s = Printf.sprintf "_%03d%s" h s in
3947 loop s n
3950 loop "" n ^ s;
3953 let defghyllscroll = (40, 8, 32);;
3954 let ghyllscroll_of_string s =
3955 let (n, a, b) as nab =
3956 if s = "default"
3957 then defghyllscroll
3958 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3960 if n <= a || n <= b || a >= b
3961 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3962 nab;
3965 let ghyllscroll_to_string ((n, a, b) as nab) =
3966 if nab = defghyllscroll
3967 then "default"
3968 else Printf.sprintf "%d,%d,%d" n a b;
3971 let describe_location () =
3972 let f (fn, _) l =
3973 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3975 let fn, ln = List.fold_left f (-1, -1) state.layout in
3976 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3977 let percent =
3978 if maxy <= 0
3979 then 100.
3980 else (100. *. (float state.y /. float maxy))
3982 if fn = ln
3983 then
3984 Printf.sprintf "page %d of %d [%.2f%%]"
3985 (fn+1) state.pagecount percent
3986 else
3987 Printf.sprintf
3988 "pages %d-%d of %d [%.2f%%]"
3989 (fn+1) (ln+1) state.pagecount percent
3992 let setpresentationmode v =
3993 let (n, _, _) = getanchor () in
3994 let _, h = getpageyh n in
3995 let ips = if conf.presentation then calcips h else conf.interpagespace in
3996 state.anchor <- (n, 0.0, float ips);
3997 conf.presentation <- v;
3998 if conf.presentation
3999 then (
4000 if not conf.scrollbarinpm
4001 then state.scrollw <- 0;
4003 else state.scrollw <- conf.scrollbw;
4004 represent ();
4007 let enterinfomode =
4008 let btos b = if b then "\xe2\x88\x9a" else "" in
4009 let showextended = ref false in
4010 let leave mode = function
4011 | Confirm -> state.mode <- mode
4012 | Cancel -> state.mode <- mode in
4013 let src =
4014 (object
4015 val mutable m_first_time = true
4016 val mutable m_l = []
4017 val mutable m_a = [||]
4018 val mutable m_prev_uioh = nouioh
4019 val mutable m_prev_mode = View
4021 inherit lvsourcebase
4023 method reset prev_mode prev_uioh =
4024 m_a <- Array.of_list (List.rev m_l);
4025 m_l <- [];
4026 m_prev_mode <- prev_mode;
4027 m_prev_uioh <- prev_uioh;
4028 if m_first_time
4029 then (
4030 let rec loop n =
4031 if n >= Array.length m_a
4032 then ()
4033 else
4034 match m_a.(n) with
4035 | _, _, _, Action _ -> m_active <- n
4036 | _ -> loop (n+1)
4038 loop 0;
4039 m_first_time <- false;
4042 method int name get set =
4043 m_l <-
4044 (name, `int get, 1, Action (
4045 fun u ->
4046 let ondone s =
4047 try set (int_of_string s)
4048 with exn ->
4049 state.text <- Printf.sprintf "bad integer `%s': %s"
4050 s (Printexc.to_string exn)
4052 state.text <- "";
4053 let te = name ^ ": ", "", None, intentry, ondone, true in
4054 state.mode <- Textentry (te, leave m_prev_mode);
4056 )) :: m_l
4058 method int_with_suffix name get set =
4059 m_l <-
4060 (name, `intws get, 1, Action (
4061 fun u ->
4062 let ondone s =
4063 try set (int_of_string_with_suffix s)
4064 with exn ->
4065 state.text <- Printf.sprintf "bad integer `%s': %s"
4066 s (Printexc.to_string exn)
4068 state.text <- "";
4069 let te =
4070 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4072 state.mode <- Textentry (te, leave m_prev_mode);
4074 )) :: m_l
4076 method bool ?(offset=1) ?(btos=btos) name get set =
4077 m_l <-
4078 (name, `bool (btos, get), offset, Action (
4079 fun u ->
4080 let v = get () in
4081 set (not v);
4083 )) :: m_l
4085 method color name get set =
4086 m_l <-
4087 (name, `color get, 1, Action (
4088 fun u ->
4089 let invalid = (nan, nan, nan) in
4090 let ondone s =
4091 let c =
4092 try color_of_string s
4093 with exn ->
4094 state.text <- Printf.sprintf "bad color `%s': %s"
4095 s (Printexc.to_string exn);
4096 invalid
4098 if c <> invalid
4099 then set c;
4101 let te = name ^ ": ", "", None, textentry, ondone, true in
4102 state.text <- color_to_string (get ());
4103 state.mode <- Textentry (te, leave m_prev_mode);
4105 )) :: m_l
4107 method string name get set =
4108 m_l <-
4109 (name, `string get, 1, Action (
4110 fun u ->
4111 let ondone s = set s in
4112 let te = name ^ ": ", "", None, textentry, ondone, true in
4113 state.mode <- Textentry (te, leave m_prev_mode);
4115 )) :: m_l
4117 method colorspace name get set =
4118 m_l <-
4119 (name, `string get, 1, Action (
4120 fun _ ->
4121 let source =
4122 let vals = [| "rgb"; "bgr"; "gray" |] in
4123 (object
4124 inherit lvsourcebase
4126 initializer
4127 m_active <- int_of_colorspace conf.colorspace;
4128 m_first <- 0;
4130 method getitemcount = Array.length vals
4131 method getitem n = (vals.(n), 0)
4132 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4133 ignore (uioh, first, pan, qsearch);
4134 if not cancel then set active;
4135 None
4136 method hasaction _ = true
4137 end)
4139 state.text <- "";
4140 let modehash = findkeyhash conf "info" in
4141 coe (new listview ~source ~trusted:true ~modehash)
4142 )) :: m_l
4144 method caption s offset =
4145 m_l <- (s, `empty, offset, Noaction) :: m_l
4147 method caption2 s f offset =
4148 m_l <- (s, `string f, offset, Noaction) :: m_l
4150 method getitemcount = Array.length m_a
4152 method getitem n =
4153 let tostr = function
4154 | `int f -> string_of_int (f ())
4155 | `intws f -> string_with_suffix_of_int (f ())
4156 | `string f -> f ()
4157 | `color f -> color_to_string (f ())
4158 | `bool (btos, f) -> btos (f ())
4159 | `empty -> ""
4161 let name, t, offset, _ = m_a.(n) in
4162 ((let s = tostr t in
4163 if String.length s > 0
4164 then Printf.sprintf "%s\t%s" name s
4165 else name),
4166 offset)
4168 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4169 let uiohopt =
4170 if not cancel
4171 then (
4172 m_qsearch <- qsearch;
4173 let uioh =
4174 match m_a.(active) with
4175 | _, _, _, Action f -> f uioh
4176 | _ -> uioh
4178 Some uioh
4180 else None
4182 m_active <- active;
4183 m_first <- first;
4184 m_pan <- pan;
4185 uiohopt
4187 method hasaction n =
4188 match m_a.(n) with
4189 | _, _, _, Action _ -> true
4190 | _ -> false
4191 end)
4193 let rec fillsrc prevmode prevuioh =
4194 let sep () = src#caption "" 0 in
4195 let colorp name get set =
4196 src#string name
4197 (fun () -> color_to_string (get ()))
4198 (fun v ->
4200 let c = color_of_string v in
4201 set c
4202 with exn ->
4203 state.text <- Printf.sprintf "bad color `%s': %s"
4204 v (Printexc.to_string exn);
4207 let oldmode = state.mode in
4208 let birdseye = isbirdseye state.mode in
4210 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4212 src#bool "presentation mode"
4213 (fun () -> conf.presentation)
4214 (fun v -> setpresentationmode v);
4216 src#bool "ignore case in searches"
4217 (fun () -> conf.icase)
4218 (fun v -> conf.icase <- v);
4220 src#bool "preload"
4221 (fun () -> conf.preload)
4222 (fun v -> conf.preload <- v);
4224 src#bool "highlight links"
4225 (fun () -> conf.hlinks)
4226 (fun v -> conf.hlinks <- v);
4228 src#bool "under info"
4229 (fun () -> conf.underinfo)
4230 (fun v -> conf.underinfo <- v);
4232 src#bool "persistent bookmarks"
4233 (fun () -> conf.savebmarks)
4234 (fun v -> conf.savebmarks <- v);
4236 src#bool "proportional display"
4237 (fun () -> conf.proportional)
4238 (fun v -> reqlayout conf.angle v);
4240 src#bool "trim margins"
4241 (fun () -> conf.trimmargins)
4242 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4244 src#bool "persistent location"
4245 (fun () -> conf.jumpback)
4246 (fun v -> conf.jumpback <- v);
4248 sep ();
4249 src#int "inter-page space"
4250 (fun () -> conf.interpagespace)
4251 (fun n ->
4252 conf.interpagespace <- n;
4253 docolumns conf.columns;
4254 let pageno, py =
4255 match state.layout with
4256 | [] -> 0, 0
4257 | l :: _ ->
4258 l.pageno, l.pagey
4260 state.maxy <- calcheight ();
4261 let y = getpagey pageno in
4262 gotoy (y + py)
4265 src#int "page bias"
4266 (fun () -> conf.pagebias)
4267 (fun v -> conf.pagebias <- v);
4269 src#int "scroll step"
4270 (fun () -> conf.scrollstep)
4271 (fun n -> conf.scrollstep <- n);
4273 src#int "horizontal scroll step"
4274 (fun () -> conf.hscrollstep)
4275 (fun v -> conf.hscrollstep <- v);
4277 src#int "auto scroll step"
4278 (fun () ->
4279 match state.autoscroll with
4280 | Some step -> step
4281 | _ -> conf.autoscrollstep)
4282 (fun n ->
4283 if state.autoscroll <> None
4284 then state.autoscroll <- Some n;
4285 conf.autoscrollstep <- n);
4287 src#int "zoom"
4288 (fun () -> truncate (conf.zoom *. 100.))
4289 (fun v -> setzoom ((float v) /. 100.));
4291 src#int "rotation"
4292 (fun () -> conf.angle)
4293 (fun v -> reqlayout v conf.proportional);
4295 src#int "scroll bar width"
4296 (fun () -> state.scrollw)
4297 (fun v ->
4298 state.scrollw <- v;
4299 conf.scrollbw <- v;
4300 reshape state.winw state.winh;
4303 src#int "scroll handle height"
4304 (fun () -> conf.scrollh)
4305 (fun v -> conf.scrollh <- v;);
4307 src#int "thumbnail width"
4308 (fun () -> conf.thumbw)
4309 (fun v ->
4310 conf.thumbw <- min 4096 v;
4311 match oldmode with
4312 | Birdseye beye ->
4313 leavebirdseye beye false;
4314 enterbirdseye ()
4315 | _ -> ()
4318 let mode = state.mode in
4319 src#string "columns"
4320 (fun () ->
4321 match conf.columns with
4322 | Csingle _ -> "1"
4323 | Cmulti (multi, _) -> multicolumns_to_string multi
4324 | Csplit (count, _) -> "-" ^ string_of_int count
4326 (fun v ->
4327 let n, a, b = multicolumns_of_string v in
4328 setcolumns mode n a b);
4330 sep ();
4331 src#caption "Presentation mode" 0;
4332 src#bool "scrollbar visible"
4333 (fun () -> conf.scrollbarinpm)
4334 (fun v ->
4335 if v != conf.scrollbarinpm
4336 then (
4337 conf.scrollbarinpm <- v;
4338 if conf.presentation
4339 then (
4340 state.scrollw <- if v then conf.scrollbw else 0;
4341 reshape state.winw state.winh;
4346 sep ();
4347 src#caption "Pixmap cache" 0;
4348 src#int_with_suffix "size (advisory)"
4349 (fun () -> conf.memlimit)
4350 (fun v -> conf.memlimit <- v);
4352 src#caption2 "used"
4353 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4354 (string_with_suffix_of_int state.memused)
4355 (Hashtbl.length state.tilemap)) 1;
4357 sep ();
4358 src#caption "Layout" 0;
4359 src#caption2 "Dimension"
4360 (fun () ->
4361 Printf.sprintf "%dx%d (virtual %dx%d)"
4362 state.winw state.winh
4363 state.w state.maxy)
4365 if conf.debug
4366 then
4367 src#caption2 "Position" (fun () ->
4368 Printf.sprintf "%dx%d" state.x state.y
4370 else
4371 src#caption2 "Visible" (fun () -> describe_location ()) 1
4374 sep ();
4375 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4376 "Save these parameters as global defaults at exit"
4377 (fun () -> conf.bedefault)
4378 (fun v -> conf.bedefault <- v)
4381 sep ();
4382 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4383 src#bool ~offset:0 ~btos "Extended parameters"
4384 (fun () -> !showextended)
4385 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4386 if !showextended
4387 then (
4388 src#bool "checkers"
4389 (fun () -> conf.checkers)
4390 (fun v -> conf.checkers <- v; setcheckers v);
4391 src#bool "update cursor"
4392 (fun () -> conf.updatecurs)
4393 (fun v -> conf.updatecurs <- v);
4394 src#bool "verbose"
4395 (fun () -> conf.verbose)
4396 (fun v -> conf.verbose <- v);
4397 src#bool "invert colors"
4398 (fun () -> conf.invert)
4399 (fun v -> conf.invert <- v);
4400 src#bool "max fit"
4401 (fun () -> conf.maxhfit)
4402 (fun v -> conf.maxhfit <- v);
4403 src#bool "redirect stderr"
4404 (fun () -> conf.redirectstderr)
4405 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4406 src#string "uri launcher"
4407 (fun () -> conf.urilauncher)
4408 (fun v -> conf.urilauncher <- v);
4409 src#string "path launcher"
4410 (fun () -> conf.pathlauncher)
4411 (fun v -> conf.pathlauncher <- v);
4412 src#string "tile size"
4413 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4414 (fun v ->
4416 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4417 conf.tilew <- max 64 w;
4418 conf.tileh <- max 64 h;
4419 flushtiles ();
4420 with exn ->
4421 state.text <- Printf.sprintf "bad tile size `%s': %s"
4422 v (Printexc.to_string exn));
4423 src#int "texture count"
4424 (fun () -> conf.texcount)
4425 (fun v ->
4426 if realloctexts v
4427 then conf.texcount <- v
4428 else showtext '!' " Failed to set texture count please retry later"
4430 src#int "slice height"
4431 (fun () -> conf.sliceheight)
4432 (fun v ->
4433 conf.sliceheight <- v;
4434 wcmd "sliceh %d" conf.sliceheight;
4436 src#int "anti-aliasing level"
4437 (fun () -> conf.aalevel)
4438 (fun v ->
4439 conf.aalevel <- bound v 0 8;
4440 state.anchor <- getanchor ();
4441 opendoc state.path state.password;
4443 src#string "page scroll scaling factor"
4444 (fun () -> string_of_float conf.pgscale)
4445 (fun v ->
4447 let s = float_of_string v in
4448 conf.pgscale <- s
4449 with exn ->
4450 state.text <- Printf.sprintf
4451 "bad page scroll scaling factor `%s': %s"
4452 v (Printexc.to_string exn)
4455 src#int "ui font size"
4456 (fun () -> fstate.fontsize)
4457 (fun v -> setfontsize (bound v 5 100));
4458 src#int "hint font size"
4459 (fun () -> conf.hfsize)
4460 (fun v -> conf.hfsize <- bound v 5 100);
4461 colorp "background color"
4462 (fun () -> conf.bgcolor)
4463 (fun v -> conf.bgcolor <- v);
4464 src#bool "crop hack"
4465 (fun () -> conf.crophack)
4466 (fun v -> conf.crophack <- v);
4467 src#string "trim fuzz"
4468 (fun () -> irect_to_string conf.trimfuzz)
4469 (fun v ->
4471 conf.trimfuzz <- irect_of_string v;
4472 if conf.trimmargins
4473 then settrim true conf.trimfuzz;
4474 with exn ->
4475 state.text <- Printf.sprintf "bad irect `%s': %s"
4476 v (Printexc.to_string exn)
4478 src#string "throttle"
4479 (fun () ->
4480 match conf.maxwait with
4481 | None -> "show place holder if page is not ready"
4482 | Some time ->
4483 if time = infinity
4484 then "wait for page to fully render"
4485 else
4486 "wait " ^ string_of_float time
4487 ^ " seconds before showing placeholder"
4489 (fun v ->
4491 let f = float_of_string v in
4492 if f <= 0.0
4493 then conf.maxwait <- None
4494 else conf.maxwait <- Some f
4495 with exn ->
4496 state.text <- Printf.sprintf "bad time `%s': %s"
4497 v (Printexc.to_string exn)
4499 src#string "ghyll scroll"
4500 (fun () ->
4501 match conf.ghyllscroll with
4502 | None -> ""
4503 | Some nab -> ghyllscroll_to_string nab
4505 (fun v ->
4507 let gs =
4508 if String.length v = 0
4509 then None
4510 else Some (ghyllscroll_of_string v)
4512 conf.ghyllscroll <- gs
4513 with exn ->
4514 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4515 v (Printexc.to_string exn)
4517 src#string "selection command"
4518 (fun () -> conf.selcmd)
4519 (fun v -> conf.selcmd <- v);
4520 src#string "synctex command"
4521 (fun () -> conf.stcmd)
4522 (fun v -> conf.stcmd <- v);
4523 src#colorspace "color space"
4524 (fun () -> colorspace_to_string conf.colorspace)
4525 (fun v ->
4526 conf.colorspace <- colorspace_of_int v;
4527 wcmd "cs %d" v;
4528 load state.layout;
4530 if pbousable ()
4531 then
4532 src#bool "use PBO"
4533 (fun () -> conf.usepbo)
4534 (fun v -> conf.usepbo <- v);
4535 src#bool "mouse wheel scrolls pages"
4536 (fun () -> conf.wheelbypage)
4537 (fun v -> conf.wheelbypage <- v);
4540 sep ();
4541 src#caption "Document" 0;
4542 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4543 src#caption2 "Pages"
4544 (fun () -> string_of_int state.pagecount) 1;
4545 src#caption2 "Dimensions"
4546 (fun () -> string_of_int (List.length state.pdims)) 1;
4547 if conf.trimmargins
4548 then (
4549 sep ();
4550 src#caption "Trimmed margins" 0;
4551 src#caption2 "Dimensions"
4552 (fun () -> string_of_int (List.length state.pdims)) 1;
4555 sep ();
4556 src#caption "OpenGL" 0;
4557 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4558 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4559 src#reset prevmode prevuioh;
4561 fun () ->
4562 state.text <- "";
4563 let prevmode = state.mode
4564 and prevuioh = state.uioh in
4565 fillsrc prevmode prevuioh;
4566 let source = (src :> lvsource) in
4567 let modehash = findkeyhash conf "info" in
4568 state.uioh <- coe (object (self)
4569 inherit listview ~source ~trusted:true ~modehash as super
4570 val mutable m_prevmemused = 0
4571 method infochanged = function
4572 | Memused ->
4573 if m_prevmemused != state.memused
4574 then (
4575 m_prevmemused <- state.memused;
4576 G.postRedisplay "memusedchanged";
4578 | Pdim -> G.postRedisplay "pdimchanged"
4579 | Docinfo -> fillsrc prevmode prevuioh
4581 method key key mask =
4582 if not (Wsi.withctrl mask)
4583 then
4584 match key with
4585 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4586 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4587 | _ -> super#key key mask
4588 else super#key key mask
4589 end);
4590 G.postRedisplay "info";
4593 let enterhelpmode =
4594 let source =
4595 (object
4596 inherit lvsourcebase
4597 method getitemcount = Array.length state.help
4598 method getitem n =
4599 let s, l, _ = state.help.(n) in
4600 (s, l)
4602 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4603 let optuioh =
4604 if not cancel
4605 then (
4606 m_qsearch <- qsearch;
4607 match state.help.(active) with
4608 | _, _, Action f -> Some (f uioh)
4609 | _ -> Some (uioh)
4611 else None
4613 m_active <- active;
4614 m_first <- first;
4615 m_pan <- pan;
4616 optuioh
4618 method hasaction n =
4619 match state.help.(n) with
4620 | _, _, Action _ -> true
4621 | _ -> false
4623 initializer
4624 m_active <- -1
4625 end)
4626 in fun () ->
4627 let modehash = findkeyhash conf "help" in
4628 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4629 G.postRedisplay "help";
4632 let entermsgsmode =
4633 let msgsource =
4634 let re = Str.regexp "[\r\n]" in
4635 (object
4636 inherit lvsourcebase
4637 val mutable m_items = [||]
4639 method getitemcount = 1 + Array.length m_items
4641 method getitem n =
4642 if n = 0
4643 then "[Clear]", 0
4644 else m_items.(n-1), 0
4646 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4647 ignore uioh;
4648 if not cancel
4649 then (
4650 if active = 0
4651 then Buffer.clear state.errmsgs;
4652 m_qsearch <- qsearch;
4654 m_active <- active;
4655 m_first <- first;
4656 m_pan <- pan;
4657 None
4659 method hasaction n =
4660 n = 0
4662 method reset =
4663 state.newerrmsgs <- false;
4664 let l = Str.split re (Buffer.contents state.errmsgs) in
4665 m_items <- Array.of_list l
4667 initializer
4668 m_active <- 0
4669 end)
4670 in fun () ->
4671 state.text <- "";
4672 msgsource#reset;
4673 let source = (msgsource :> lvsource) in
4674 let modehash = findkeyhash conf "listview" in
4675 state.uioh <- coe (object
4676 inherit listview ~source ~trusted:false ~modehash as super
4677 method display =
4678 if state.newerrmsgs
4679 then msgsource#reset;
4680 super#display
4681 end);
4682 G.postRedisplay "msgs";
4685 let quickbookmark ?title () =
4686 match state.layout with
4687 | [] -> ()
4688 | l :: _ ->
4689 let title =
4690 match title with
4691 | None ->
4692 let sec = Unix.gettimeofday () in
4693 let tm = Unix.localtime sec in
4694 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4695 (l.pageno+1)
4696 tm.Unix.tm_mday
4697 tm.Unix.tm_mon
4698 (tm.Unix.tm_year + 1900)
4699 tm.Unix.tm_hour
4700 tm.Unix.tm_min
4701 | Some title -> title
4703 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4706 let doreshape w h =
4707 Wsi.reshape w h;
4710 let setautoscrollspeed step goingdown =
4711 let incr = max 1 ((abs step) / 2) in
4712 let incr = if goingdown then incr else -incr in
4713 let astep = step + incr in
4714 state.autoscroll <- Some astep;
4717 let gotounder = function
4718 | Ulinkgoto (pageno, top) ->
4719 if pageno >= 0
4720 then (
4721 addnav ();
4722 gotopage1 pageno top;
4725 | Ulinkuri s ->
4726 gotouri s
4728 | Uremote (filename, pageno) ->
4729 let path =
4730 if Sys.file_exists filename
4731 then filename
4732 else
4733 let dir = Filename.dirname state.path in
4734 let path = Filename.concat dir filename in
4735 if Sys.file_exists path
4736 then path
4737 else ""
4739 if String.length path > 0
4740 then (
4741 let anchor = getanchor () in
4742 let ranchor = state.path, state.password, anchor in
4743 state.anchor <- (pageno, 0.0, 0.0);
4744 state.ranchors <- ranchor :: state.ranchors;
4745 opendoc path "";
4747 else showtext '!' ("Could not find " ^ filename)
4749 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4752 let canpan () =
4753 match conf.columns with
4754 | Csplit _ -> true
4755 | _ -> conf.zoom > 1.0
4758 let existsinrow pageno (columns, coverA, coverB) p =
4759 let last = ((pageno - coverA) mod columns) + columns in
4760 let rec any = function
4761 | [] -> false
4762 | l :: rest ->
4763 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4764 then p l
4765 else (
4766 if not (p l)
4767 then (if l.pageno = last then false else any rest)
4768 else true
4771 any state.layout
4774 let nextpage () =
4775 match state.layout with
4776 | [] -> ()
4777 | l :: rest ->
4778 match conf.columns with
4779 | Csingle _ ->
4780 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4781 then
4782 let y = clamp (pgscale state.winh) in
4783 gotoghyll y
4784 else
4785 let pageno = min (l.pageno+1) (state.pagecount-1) in
4786 gotoghyll (getpagey pageno)
4787 | Cmulti ((c, _, _) as cl, _) ->
4788 if conf.presentation
4789 && (existsinrow l.pageno cl
4790 (fun l -> l.pageh > l.pagey + l.pagevh))
4791 then
4792 let y = clamp (pgscale state.winh) in
4793 gotoghyll y
4794 else
4795 let pageno = min (l.pageno+c) (state.pagecount-1) in
4796 gotoghyll (getpagey pageno)
4797 | Csplit (n, _) ->
4798 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4799 then
4800 let pagey, pageh = getpageyh l.pageno in
4801 let pagey = pagey + pageh * l.pagecol in
4802 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4803 gotoghyll (pagey + pageh + ips)
4806 let prevpage () =
4807 match state.layout with
4808 | [] -> ()
4809 | l :: _ ->
4810 match conf.columns with
4811 | Csingle _ ->
4812 if conf.presentation && l.pagey != 0
4813 then
4814 gotoghyll (clamp (pgscale ~-(state.winh)))
4815 else
4816 let pageno = max 0 (l.pageno-1) in
4817 gotoghyll (getpagey pageno)
4818 | Cmulti ((c, _, coverB) as cl, _) ->
4819 if conf.presentation &&
4820 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4821 then
4822 gotoghyll (clamp (pgscale ~-(state.winh)))
4823 else
4824 let decr =
4825 if l.pageno = state.pagecount - coverB
4826 then 1
4827 else c
4829 let pageno = max 0 (l.pageno-decr) in
4830 gotoghyll (getpagey pageno)
4831 | Csplit (n, _) ->
4832 let y =
4833 if l.pagecol = 0
4834 then
4835 if l.pageno = 0
4836 then l.pagey
4837 else
4838 let pageno = max 0 (l.pageno-1) in
4839 let pagey, pageh = getpageyh pageno in
4840 pagey + (n-1)*pageh
4841 else
4842 let pagey, pageh = getpageyh l.pageno in
4843 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4845 gotoghyll y
4848 let viewkeyboard key mask =
4849 let enttext te =
4850 let mode = state.mode in
4851 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4852 state.text <- "";
4853 enttext ();
4854 G.postRedisplay "view:enttext"
4856 let ctrl = Wsi.withctrl mask in
4857 let key =
4858 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4860 match key with
4861 | 81 -> (* Q *)
4862 exit 0
4864 | 0xff63 -> (* insert *)
4865 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4866 then (
4867 state.mode <- LinkNav (Ltgendir 0);
4868 gotoy state.y;
4870 else showtext '!' "Keyboard link navigation does not work under rotation"
4872 | 0xff1b | 113 -> (* escape / q *)
4873 begin match state.mstate with
4874 | Mzoomrect _ ->
4875 state.mstate <- Mnone;
4876 Wsi.setcursor Wsi.CURSOR_INHERIT;
4877 G.postRedisplay "kill zoom rect";
4878 | _ ->
4879 begin match state.mode with
4880 | LinkNav _ ->
4881 state.mode <- View;
4882 G.postRedisplay "esc leave linknav"
4883 | _ ->
4884 match state.ranchors with
4885 | [] -> raise Quit
4886 | (path, password, anchor) :: rest ->
4887 state.ranchors <- rest;
4888 state.anchor <- anchor;
4889 opendoc path password
4890 end;
4891 end;
4893 | 0xff08 -> (* backspace *)
4894 gotoghyll (getnav ~-1)
4896 | 111 -> (* o *)
4897 enteroutlinemode ()
4899 | 117 -> (* u *)
4900 state.rects <- [];
4901 state.text <- "";
4902 G.postRedisplay "dehighlight";
4904 | 47 | 63 -> (* / ? *)
4905 let ondone isforw s =
4906 cbput state.hists.pat s;
4907 state.searchpattern <- s;
4908 search s isforw
4910 let s = String.create 1 in
4911 s.[0] <- Char.chr key;
4912 enttext (s, "", Some (onhist state.hists.pat),
4913 textentry, ondone (key = 47), true)
4915 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4916 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4917 setzoom (conf.zoom +. incr)
4919 | 43 | 0xffab -> (* + *)
4920 let ondone s =
4921 let n =
4922 try int_of_string s with exc ->
4923 state.text <- Printf.sprintf "bad integer `%s': %s"
4924 s (Printexc.to_string exc);
4925 max_int
4927 if n != max_int
4928 then (
4929 conf.pagebias <- n;
4930 state.text <- "page bias is now " ^ string_of_int n;
4933 enttext ("page bias: ", "", None, intentry, ondone, true)
4935 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4936 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4937 setzoom (max 0.01 (conf.zoom -. decr))
4939 | 45 | 0xffad -> (* - *)
4940 let ondone msg = state.text <- msg in
4941 enttext (
4942 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4943 optentry state.mode, ondone, true
4946 | 48 when ctrl -> (* ctrl-0 *)
4947 setzoom 1.0
4949 | 49 when ctrl -> (* ctrl-1 *)
4950 let cols =
4951 match conf.columns with
4952 | Csingle _ | Cmulti _ -> 1
4953 | Csplit (n, _) -> n
4955 let zoom = zoomforh state.winw state.winh state.scrollw cols in
4956 if zoom < 1.0
4957 then setzoom zoom
4959 | 0xffc6 -> (* f9 *)
4960 togglebirdseye ()
4962 | 57 when ctrl -> (* ctrl-9 *)
4963 togglebirdseye ()
4965 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4966 when not ctrl -> (* 0..9 *)
4967 let ondone s =
4968 let n =
4969 try int_of_string s with exc ->
4970 state.text <- Printf.sprintf "bad integer `%s': %s"
4971 s (Printexc.to_string exc);
4974 if n >= 0
4975 then (
4976 addnav ();
4977 cbput state.hists.pag (string_of_int n);
4978 gotopage1 (n + conf.pagebias - 1) 0;
4981 let pageentry text key =
4982 match Char.unsafe_chr key with
4983 | 'g' -> TEdone text
4984 | _ -> intentry text key
4986 let text = "x" in text.[0] <- Char.chr key;
4987 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4989 | 98 -> (* b *)
4990 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4991 reshape state.winw state.winh;
4993 | 108 -> (* l *)
4994 conf.hlinks <- not conf.hlinks;
4995 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4996 G.postRedisplay "toggle highlightlinks";
4998 | 70 -> (* F *)
4999 state.glinks <- true;
5000 let mode = state.mode in
5001 state.mode <- Textentry (
5002 (":", "", None, linknentry, linkndone gotounder, false),
5003 (fun _ ->
5004 state.glinks <- false;
5005 state.mode <- mode)
5007 state.text <- "";
5008 G.postRedisplay "view:linkent(F)"
5010 | 121 -> (* y *)
5011 state.glinks <- true;
5012 let mode = state.mode in
5013 state.mode <- Textentry (
5014 (":", "", None, linknentry, linkndone (fun under ->
5015 match Ne.pipe () with
5016 | Ne.Exn exn ->
5017 showtext '!' (Printf.sprintf "pipe failed: %s"
5018 (Printexc.to_string exn));
5019 | Ne.Res (r, w) ->
5020 let popened =
5021 try popen conf.selcmd [r, 0; w, -1]; true
5022 with exn ->
5023 showtext '!'
5024 (Printf.sprintf "failed to execute %s: %s"
5025 conf.selcmd (Printexc.to_string exn));
5026 false
5028 let clo cap fd =
5029 Ne.clo fd (fun msg ->
5030 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5033 let s = undertext under in
5034 if popened
5035 then
5036 (try
5037 let l = String.length s in
5038 let n = tempfailureretry (Unix.write w s 0) l in
5039 if n != l
5040 then
5041 showtext '!'
5042 (Printf.sprintf
5043 "failed to write %d characters to sel pipe, wrote %d"
5046 with exn ->
5047 showtext '!'
5048 (Printf.sprintf "failed to write to sel pipe: %s"
5049 (Printexc.to_string exn)
5052 else dolog "%s" s;
5053 clo "pipe/r" r;
5054 clo "pipe/w" w;
5055 ), false
5057 fun _ ->
5058 state.glinks <- false;
5059 state.mode <- mode
5061 state.text <- "";
5062 G.postRedisplay "view:linkent"
5064 | 97 -> (* a *)
5065 begin match state.autoscroll with
5066 | Some step ->
5067 conf.autoscrollstep <- step;
5068 state.autoscroll <- None
5069 | None ->
5070 if conf.autoscrollstep = 0
5071 then state.autoscroll <- Some 1
5072 else state.autoscroll <- Some conf.autoscrollstep
5075 | 112 when ctrl -> (* ctrl-p *)
5076 launchpath ()
5078 | 80 -> (* P *)
5079 setpresentationmode (not conf.presentation);
5080 showtext ' ' ("presentation mode " ^
5081 if conf.presentation then "on" else "off");
5083 | 102 -> (* f *)
5084 if List.mem Wsi.Fullscreen state.winstate
5085 then doreshape conf.cwinw conf.cwinh
5086 else Wsi.fullscreen ()
5088 | 112 | 78 -> (* p|N *)
5089 search state.searchpattern false
5091 | 110 | 0xffc0 -> (* n|F3 *)
5092 search state.searchpattern true
5094 | 116 -> (* t *)
5095 begin match state.layout with
5096 | [] -> ()
5097 | l :: _ ->
5098 gotoghyll (getpagey l.pageno)
5101 | 32 -> (* space *)
5102 nextpage ()
5104 | 0xff9f | 0xffff -> (* delete *)
5105 prevpage ()
5107 | 61 -> (* = *)
5108 showtext ' ' (describe_location ());
5110 | 119 -> (* w *)
5111 begin match state.layout with
5112 | [] -> ()
5113 | l :: _ ->
5114 doreshape (l.pagew + state.scrollw) l.pageh;
5115 G.postRedisplay "w"
5118 | 39 -> (* ' *)
5119 enterbookmarkmode ()
5121 | 104 | 0xffbe -> (* h|F1 *)
5122 enterhelpmode ()
5124 | 105 -> (* i *)
5125 enterinfomode ()
5127 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5128 entermsgsmode ()
5130 | 109 -> (* m *)
5131 let ondone s =
5132 match state.layout with
5133 | l :: _ ->
5134 if String.length s > 0
5135 then
5136 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5137 | _ -> ()
5139 enttext ("bookmark: ", "", None, textentry, ondone, true)
5141 | 126 -> (* ~ *)
5142 quickbookmark ();
5143 showtext ' ' "Quick bookmark added";
5145 | 122 -> (* z *)
5146 begin match state.layout with
5147 | l :: _ ->
5148 let rect = getpdimrect l.pagedimno in
5149 let w, h =
5150 if conf.crophack
5151 then
5152 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5153 truncate (1.2 *. (rect.(3) -. rect.(0))))
5154 else
5155 (truncate (rect.(1) -. rect.(0)),
5156 truncate (rect.(3) -. rect.(0)))
5158 let w = truncate ((float w)*.conf.zoom)
5159 and h = truncate ((float h)*.conf.zoom) in
5160 if w != 0 && h != 0
5161 then (
5162 state.anchor <- getanchor ();
5163 doreshape (w + state.scrollw) (h + conf.interpagespace)
5165 G.postRedisplay "z";
5167 | [] -> ()
5170 | 50 when ctrl -> (* ctrl-2 *)
5171 let maxw = getmaxw () in
5172 if maxw > 0.0
5173 then setzoom (maxw /. float state.winw)
5175 | 60 | 62 -> (* < > *)
5176 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5178 | 91 | 93 -> (* [ ] *)
5179 conf.colorscale <-
5180 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5182 G.postRedisplay "brightness";
5184 | 99 when state.mode = View -> (* c *)
5185 let (c, a, b), z =
5186 match state.prevcolumns with
5187 | None -> (1, 0, 0), 1.0
5188 | Some (columns, z) ->
5189 let cab =
5190 match columns with
5191 | Csplit (c, _) -> -c, 0, 0
5192 | Cmulti ((c, a, b), _) -> c, a, b
5193 | Csingle _ -> 1, 0, 0
5195 cab, z
5197 setcolumns View c a b;
5198 setzoom z;
5200 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5201 setzoom state.prevzoom
5203 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5204 begin match state.autoscroll with
5205 | None ->
5206 begin match state.mode with
5207 | Birdseye beye -> upbirdseye 1 beye
5208 | _ ->
5209 if ctrl
5210 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5211 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5213 | Some n ->
5214 setautoscrollspeed n false
5217 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5218 begin match state.autoscroll with
5219 | None ->
5220 begin match state.mode with
5221 | Birdseye beye -> downbirdseye 1 beye
5222 | _ ->
5223 if ctrl
5224 then gotoy_and_clear_text (clamp (state.winh/2))
5225 else gotoy_and_clear_text (clamp conf.scrollstep)
5227 | Some n ->
5228 setautoscrollspeed n true
5231 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5232 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5233 if canpan ()
5234 then
5235 let dx =
5236 if ctrl
5237 then state.winw / 2
5238 else conf.hscrollstep
5240 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5241 state.x <- state.x + dx;
5242 gotoy_and_clear_text state.y
5243 else (
5244 state.text <- "";
5245 G.postRedisplay "lef/right"
5248 | 0xff55 | 0xff9a -> (* (kp) prior *)
5249 let y =
5250 if ctrl
5251 then
5252 match state.layout with
5253 | [] -> state.y
5254 | l :: _ -> state.y - l.pagey
5255 else
5256 clamp (pgscale (-state.winh))
5258 gotoghyll y
5260 | 0xff56 | 0xff9b -> (* (kp) next *)
5261 let y =
5262 if ctrl
5263 then
5264 match List.rev state.layout with
5265 | [] -> state.y
5266 | l :: _ -> getpagey l.pageno
5267 else
5268 clamp (pgscale state.winh)
5270 gotoghyll y
5272 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5273 gotoghyll 0
5274 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5275 gotoghyll (clamp state.maxy)
5277 | 0xff53 | 0xff98
5278 when Wsi.withalt mask -> (* alt-(kp) right *)
5279 gotoghyll (getnav 1)
5280 | 0xff51 | 0xff96
5281 when Wsi.withalt mask -> (* alt-(kp) left *)
5282 gotoghyll (getnav ~-1)
5284 | 114 -> (* r *)
5285 reload ()
5287 | 118 when conf.debug -> (* v *)
5288 state.rects <- [];
5289 List.iter (fun l ->
5290 match getopaque l.pageno with
5291 | None -> ()
5292 | Some opaque ->
5293 let x0, y0, x1, y1 = pagebbox opaque in
5294 let a,b = float x0, float y0 in
5295 let c,d = float x1, float y0 in
5296 let e,f = float x1, float y1 in
5297 let h,j = float x0, float y1 in
5298 let rect = (a,b,c,d,e,f,h,j) in
5299 debugrect rect;
5300 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5301 ) state.layout;
5302 G.postRedisplay "v";
5304 | _ ->
5305 vlog "huh? %s" (Wsi.keyname key)
5308 let linknavkeyboard key mask linknav =
5309 let getpage pageno =
5310 let rec loop = function
5311 | [] -> None
5312 | l :: _ when l.pageno = pageno -> Some l
5313 | _ :: rest -> loop rest
5314 in loop state.layout
5316 let doexact (pageno, n) =
5317 match getopaque pageno, getpage pageno with
5318 | Some opaque, Some l ->
5319 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5320 then
5321 let under = getlink opaque n in
5322 G.postRedisplay "link gotounder";
5323 gotounder under;
5324 state.mode <- View;
5325 else
5326 let opt, dir =
5327 match key with
5328 | 0xff50 -> (* home *)
5329 Some (findlink opaque LDfirst), -1
5331 | 0xff57 -> (* end *)
5332 Some (findlink opaque LDlast), 1
5334 | 0xff51 -> (* left *)
5335 Some (findlink opaque (LDleft n)), -1
5337 | 0xff53 -> (* right *)
5338 Some (findlink opaque (LDright n)), 1
5340 | 0xff52 -> (* up *)
5341 Some (findlink opaque (LDup n)), -1
5343 | 0xff54 -> (* down *)
5344 Some (findlink opaque (LDdown n)), 1
5346 | _ -> None, 0
5348 let pwl l dir =
5349 begin match findpwl l.pageno dir with
5350 | Pwlnotfound -> ()
5351 | Pwl pageno ->
5352 let notfound dir =
5353 state.mode <- LinkNav (Ltgendir dir);
5354 let y, h = getpageyh pageno in
5355 let y =
5356 if dir < 0
5357 then y + h - state.winh
5358 else y
5360 gotoy y
5362 begin match getopaque pageno, getpage pageno with
5363 | Some opaque, Some _ ->
5364 let link =
5365 let ld = if dir > 0 then LDfirst else LDlast in
5366 findlink opaque ld
5368 begin match link with
5369 | Lfound m ->
5370 showlinktype (getlink opaque m);
5371 state.mode <- LinkNav (Ltexact (pageno, m));
5372 G.postRedisplay "linknav jpage";
5373 | _ -> notfound dir
5374 end;
5375 | _ -> notfound dir
5376 end;
5377 end;
5379 begin match opt with
5380 | Some Lnotfound -> pwl l dir;
5381 | Some (Lfound m) ->
5382 if m = n
5383 then pwl l dir
5384 else (
5385 let _, y0, _, y1 = getlinkrect opaque m in
5386 if y0 < l.pagey
5387 then gotopage1 l.pageno y0
5388 else (
5389 let d = fstate.fontsize + 1 in
5390 if y1 - l.pagey > l.pagevh - d
5391 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5392 else G.postRedisplay "linknav";
5394 showlinktype (getlink opaque m);
5395 state.mode <- LinkNav (Ltexact (l.pageno, m));
5398 | None -> viewkeyboard key mask
5399 end;
5400 | _ -> viewkeyboard key mask
5402 if key = 0xff63
5403 then (
5404 state.mode <- View;
5405 G.postRedisplay "leave linknav"
5407 else
5408 match linknav with
5409 | Ltgendir _ -> viewkeyboard key mask
5410 | Ltexact exact -> doexact exact
5413 let keyboard key mask =
5414 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5415 then wcmd "interrupt"
5416 else state.uioh <- state.uioh#key key mask
5419 let birdseyekeyboard key mask
5420 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5421 let incr =
5422 match conf.columns with
5423 | Csingle _ -> 1
5424 | Cmulti ((c, _, _), _) -> c
5425 | Csplit _ -> failwith "bird's eye split mode"
5427 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5428 match key with
5429 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5430 let y, h = getpageyh pageno in
5431 let top = (state.winh - h) / 2 in
5432 gotoy (max 0 (y - top))
5433 | 0xff0d (* enter *)
5434 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5435 | 0xff1b -> leavebirdseye beye true (* escape *)
5436 | 0xff52 -> upbirdseye incr beye (* up *)
5437 | 0xff54 -> downbirdseye incr beye (* down *)
5438 | 0xff51 -> upbirdseye 1 beye (* left *)
5439 | 0xff53 -> downbirdseye 1 beye (* right *)
5441 | 0xff55 -> (* prior *)
5442 begin match state.layout with
5443 | l :: _ ->
5444 if l.pagey != 0
5445 then (
5446 state.mode <- Birdseye (
5447 oconf, leftx, l.pageno, hooverpageno, anchor
5449 gotopage1 l.pageno 0;
5451 else (
5452 let layout = layout (state.y-state.winh) (pgh state.layout) in
5453 match layout with
5454 | [] -> gotoy (clamp (-state.winh))
5455 | l :: _ ->
5456 state.mode <- Birdseye (
5457 oconf, leftx, l.pageno, hooverpageno, anchor
5459 gotopage1 l.pageno 0
5462 | [] -> gotoy (clamp (-state.winh))
5463 end;
5465 | 0xff56 -> (* next *)
5466 begin match List.rev state.layout with
5467 | l :: _ ->
5468 let layout = layout (state.y + (pgh state.layout)) state.winh in
5469 begin match layout with
5470 | [] ->
5471 let incr = l.pageh - l.pagevh in
5472 if incr = 0
5473 then (
5474 state.mode <-
5475 Birdseye (
5476 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5478 G.postRedisplay "birdseye pagedown";
5480 else gotoy (clamp (incr + conf.interpagespace*2));
5482 | l :: _ ->
5483 state.mode <-
5484 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5485 gotopage1 l.pageno 0;
5488 | [] -> gotoy (clamp state.winh)
5489 end;
5491 | 0xff50 -> (* home *)
5492 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5493 gotopage1 0 0
5495 | 0xff57 -> (* end *)
5496 let pageno = state.pagecount - 1 in
5497 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5498 if not (pagevisible state.layout pageno)
5499 then
5500 let h =
5501 match List.rev state.pdims with
5502 | [] -> state.winh
5503 | (_, _, h, _) :: _ -> h
5505 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5506 else G.postRedisplay "birdseye end";
5507 | _ -> viewkeyboard key mask
5510 let drawpage l linkindexbase =
5511 let color =
5512 match state.mode with
5513 | Textentry _ -> scalecolor 0.4
5514 | LinkNav _
5515 | View -> scalecolor 1.0
5516 | Birdseye (_, _, pageno, hooverpageno, _) ->
5517 if l.pageno = hooverpageno
5518 then scalecolor 0.9
5519 else (
5520 if l.pageno = pageno
5521 then scalecolor 1.0
5522 else scalecolor 0.8
5525 drawtiles l color;
5526 begin match getopaque l.pageno with
5527 | Some opaque ->
5528 if tileready l l.pagex l.pagey
5529 then
5530 let x = l.pagedispx - l.pagex
5531 and y = l.pagedispy - l.pagey in
5532 let hlmask =
5533 match conf.columns with
5534 | Csingle _ | Cmulti _ ->
5535 (if conf.hlinks then 1 else 0)
5536 + (if state.glinks
5537 && not (isbirdseye state.mode) then 2 else 0)
5538 | _ -> 0
5540 let s =
5541 match state.mode with
5542 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5543 | _ -> ""
5545 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5546 else 0
5548 | _ -> 0
5549 end;
5552 let scrollindicator () =
5553 let sbw, ph, sh = state.uioh#scrollph in
5554 let sbh, pw, sw = state.uioh#scrollpw in
5556 GlDraw.color (0.64, 0.64, 0.64);
5557 GlDraw.rect
5558 (float (state.winw - sbw), 0.)
5559 (float state.winw, float state.winh)
5561 GlDraw.rect
5562 (0., float (state.winh - sbh))
5563 (float (state.winw - state.scrollw - 1), float state.winh)
5565 GlDraw.color (0.0, 0.0, 0.0);
5567 GlDraw.rect
5568 (float (state.winw - sbw), ph)
5569 (float state.winw, ph +. sh)
5571 GlDraw.rect
5572 (pw, float (state.winh - sbh))
5573 (pw +. sw, float state.winh)
5577 let showsel () =
5578 match state.mstate with
5579 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5582 | Msel ((x0, y0), (x1, y1)) ->
5583 let rec loop = function
5584 | l :: ls ->
5585 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5586 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5587 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5588 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5589 then
5590 match getopaque l.pageno with
5591 | Some opaque ->
5592 let x0, y0 = pagetranslatepoint l x0 y0 in
5593 let x1, y1 = pagetranslatepoint l x1 y1 in
5594 seltext opaque (x0, y0, x1, y1);
5595 | _ -> ()
5596 else loop ls
5597 | [] -> ()
5599 loop state.layout
5602 let showrects rects =
5603 Gl.enable `blend;
5604 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5605 GlDraw.polygon_mode `both `fill;
5606 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5607 List.iter
5608 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5609 List.iter (fun l ->
5610 if l.pageno = pageno
5611 then (
5612 let dx = float (l.pagedispx - l.pagex) in
5613 let dy = float (l.pagedispy - l.pagey) in
5614 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5615 GlDraw.begins `quads;
5617 GlDraw.vertex2 (x0+.dx, y0+.dy);
5618 GlDraw.vertex2 (x1+.dx, y1+.dy);
5619 GlDraw.vertex2 (x2+.dx, y2+.dy);
5620 GlDraw.vertex2 (x3+.dx, y3+.dy);
5622 GlDraw.ends ();
5624 ) state.layout
5625 ) rects
5627 Gl.disable `blend;
5630 let display () =
5631 GlClear.color (scalecolor2 conf.bgcolor);
5632 GlClear.clear [`color];
5633 let rec loop linkindexbase = function
5634 | l :: rest ->
5635 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5636 loop linkindexbase rest
5637 | [] -> ()
5639 loop 0 state.layout;
5640 let rects =
5641 match state.mode with
5642 | LinkNav (Ltexact (pageno, linkno)) ->
5643 begin match getopaque pageno with
5644 | Some opaque ->
5645 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5646 (pageno, 5, (
5647 float x0, float y0,
5648 float x1, float y0,
5649 float x1, float y1,
5650 float x0, float y1)
5651 ) :: state.rects
5652 | None -> state.rects
5654 | _ -> state.rects
5656 showrects rects;
5657 showsel ();
5658 state.uioh#display;
5659 begin match state.mstate with
5660 | Mzoomrect ((x0, y0), (x1, y1)) ->
5661 Gl.enable `blend;
5662 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5663 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5664 GlDraw.rect (float x0, float y0)
5665 (float x1, float y1);
5666 Gl.disable `blend;
5667 | _ -> ()
5668 end;
5669 enttext ();
5670 scrollindicator ();
5671 Wsi.swapb ();
5674 let zoomrect x y x1 y1 =
5675 let x0 = min x x1
5676 and x1 = max x x1
5677 and y0 = min y y1 in
5678 gotoy (state.y + y0);
5679 state.anchor <- getanchor ();
5680 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5681 let margin =
5682 if state.w < state.winw - state.scrollw
5683 then (state.winw - state.scrollw - state.w) / 2
5684 else 0
5686 state.x <- (state.x + margin) - x0;
5687 setzoom zoom;
5688 Wsi.setcursor Wsi.CURSOR_INHERIT;
5689 state.mstate <- Mnone;
5692 let scrollx x =
5693 let winw = state.winw - state.scrollw - 1 in
5694 let s = float x /. float winw in
5695 let destx = truncate (float (state.w + winw) *. s) in
5696 state.x <- winw - destx;
5697 gotoy_and_clear_text state.y;
5698 state.mstate <- Mscrollx;
5701 let scrolly y =
5702 let s = float y /. float state.winh in
5703 let desty = truncate (float (state.maxy - state.winh) *. s) in
5704 gotoy_and_clear_text desty;
5705 state.mstate <- Mscrolly;
5708 let viewmouse button down x y mask =
5709 match button with
5710 | n when (n == 4 || n == 5) && not down ->
5711 if Wsi.withctrl mask
5712 then (
5713 match state.mstate with
5714 | Mzoom (oldn, i) ->
5715 if oldn = n
5716 then (
5717 if i = 2
5718 then
5719 let incr =
5720 match n with
5721 | 5 ->
5722 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5723 | _ ->
5724 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5726 let zoom = conf.zoom -. incr in
5727 setzoom zoom;
5728 state.mstate <- Mzoom (n, 0);
5729 else
5730 state.mstate <- Mzoom (n, i+1);
5732 else state.mstate <- Mzoom (n, 0)
5734 | _ -> state.mstate <- Mzoom (n, 0)
5736 else (
5737 match state.autoscroll with
5738 | Some step -> setautoscrollspeed step (n=4)
5739 | None ->
5740 if conf.wheelbypage
5741 then (
5742 if n = 4
5743 then prevpage ()
5744 else nextpage ()
5746 else
5747 let incr =
5748 if n = 4
5749 then -conf.scrollstep
5750 else conf.scrollstep
5752 let incr = incr * 2 in
5753 let y = clamp incr in
5754 gotoy_and_clear_text y
5757 | n when (n = 6 || n = 7) && not down && canpan () ->
5758 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5759 gotoy_and_clear_text state.y
5761 | 1 when Wsi.withshift mask ->
5762 state.mstate <- Mnone;
5763 if not down then (
5764 match unproject x y with
5765 | Some (pageno, ux, uy) ->
5766 let cmd = Printf.sprintf
5767 "%s %s %d %d %d"
5768 conf.stcmd state.path pageno ux uy
5770 popen cmd []
5771 | None -> ()
5774 | 1 when Wsi.withctrl mask ->
5775 if down
5776 then (
5777 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5778 state.mstate <- Mpan (x, y)
5780 else
5781 state.mstate <- Mnone
5783 | 3 ->
5784 if down
5785 then (
5786 Wsi.setcursor Wsi.CURSOR_CYCLE;
5787 let p = (x, y) in
5788 state.mstate <- Mzoomrect (p, p)
5790 else (
5791 match state.mstate with
5792 | Mzoomrect ((x0, y0), _) ->
5793 if abs (x-x0) > 10 && abs (y - y0) > 10
5794 then zoomrect x0 y0 x y
5795 else (
5796 state.mstate <- Mnone;
5797 Wsi.setcursor Wsi.CURSOR_INHERIT;
5798 G.postRedisplay "kill accidental zoom rect";
5800 | _ ->
5801 Wsi.setcursor Wsi.CURSOR_INHERIT;
5802 state.mstate <- Mnone
5805 | 1 when x > state.winw - state.scrollw ->
5806 if down
5807 then
5808 let _, position, sh = state.uioh#scrollph in
5809 if y > truncate position && y < truncate (position +. sh)
5810 then state.mstate <- Mscrolly
5811 else scrolly y
5812 else
5813 state.mstate <- Mnone
5815 | 1 when y > state.winh - state.hscrollh ->
5816 if down
5817 then
5818 let _, position, sw = state.uioh#scrollpw in
5819 if x > truncate position && x < truncate (position +. sw)
5820 then state.mstate <- Mscrollx
5821 else scrollx x
5822 else
5823 state.mstate <- Mnone
5825 | 1 ->
5826 let dest = if down then getunder x y else Unone in
5827 begin match dest with
5828 | Ulinkgoto _
5829 | Ulinkuri _
5830 | Uremote _
5831 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5832 gotounder dest
5834 | Unone when down ->
5835 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5836 state.mstate <- Mpan (x, y);
5838 | Unone | Utext _ ->
5839 if down
5840 then (
5841 if conf.angle mod 360 = 0
5842 then (
5843 state.mstate <- Msel ((x, y), (x, y));
5844 G.postRedisplay "mouse select";
5847 else (
5848 match state.mstate with
5849 | Mnone -> ()
5851 | Mzoom _ | Mscrollx | Mscrolly ->
5852 state.mstate <- Mnone
5854 | Mzoomrect ((x0, y0), _) ->
5855 zoomrect x0 y0 x y
5857 | Mpan _ ->
5858 Wsi.setcursor Wsi.CURSOR_INHERIT;
5859 state.mstate <- Mnone
5861 | Msel ((x0, y0), (x1, y1)) ->
5862 let rec loop = function
5863 | [] -> ()
5864 | l :: rest ->
5865 let inside =
5866 let a0 = l.pagedispy in
5867 let a1 = a0 + l.pagevh in
5868 let b0 = l.pagedispx in
5869 let b1 = b0 + l.pagevw in
5870 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5871 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5873 if inside
5874 then
5875 match getopaque l.pageno with
5876 | Some opaque ->
5877 begin
5878 match Ne.pipe () with
5879 | Ne.Exn exn ->
5880 showtext '!'
5881 (Printf.sprintf
5882 "can not create sel pipe: %s"
5883 (Printexc.to_string exn));
5884 | Ne.Res (r, w) ->
5885 let doclose what fd =
5886 Ne.clo fd (fun msg ->
5887 dolog "%s close failed: %s" what msg)
5890 popen conf.selcmd [r, 0; w, -1];
5891 copysel w opaque;
5892 doclose "pipe/r" r;
5893 G.postRedisplay "copysel";
5894 with exn ->
5895 dolog "can not execute %S: %s"
5896 conf.selcmd (Printexc.to_string exn);
5897 doclose "pipe/r" r;
5898 doclose "pipe/w" w;
5900 | None -> ()
5901 else loop rest
5903 loop state.layout;
5904 Wsi.setcursor Wsi.CURSOR_INHERIT;
5905 state.mstate <- Mnone;
5909 | _ -> ()
5912 let birdseyemouse button down x y mask
5913 (conf, leftx, _, hooverpageno, anchor) =
5914 match button with
5915 | 1 when down ->
5916 let rec loop = function
5917 | [] -> ()
5918 | l :: rest ->
5919 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5920 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5921 then (
5922 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5924 else loop rest
5926 loop state.layout
5927 | 3 -> ()
5928 | _ -> viewmouse button down x y mask
5931 let mouse button down x y mask =
5932 state.uioh <- state.uioh#button button down x y mask;
5935 let motion ~x ~y =
5936 state.uioh <- state.uioh#motion x y
5939 let pmotion ~x ~y =
5940 state.uioh <- state.uioh#pmotion x y;
5943 let uioh = object
5944 method display = ()
5946 method key key mask =
5947 begin match state.mode with
5948 | Textentry textentry -> textentrykeyboard key mask textentry
5949 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5950 | View -> viewkeyboard key mask
5951 | LinkNav linknav -> linknavkeyboard key mask linknav
5952 end;
5953 state.uioh
5955 method button button bstate x y mask =
5956 begin match state.mode with
5957 | LinkNav _
5958 | View -> viewmouse button bstate x y mask
5959 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5960 | Textentry _ -> ()
5961 end;
5962 state.uioh
5964 method motion x y =
5965 begin match state.mode with
5966 | Textentry _ -> ()
5967 | View | Birdseye _ | LinkNav _ ->
5968 match state.mstate with
5969 | Mzoom _ | Mnone -> ()
5971 | Mpan (x0, y0) ->
5972 let dx = x - x0
5973 and dy = y0 - y in
5974 state.mstate <- Mpan (x, y);
5975 if canpan ()
5976 then state.x <- state.x + dx;
5977 let y = clamp dy in
5978 gotoy_and_clear_text y
5980 | Msel (a, _) ->
5981 state.mstate <- Msel (a, (x, y));
5982 G.postRedisplay "motion select";
5984 | Mscrolly ->
5985 let y = min state.winh (max 0 y) in
5986 scrolly y
5988 | Mscrollx ->
5989 let x = min state.winw (max 0 x) in
5990 scrollx x
5992 | Mzoomrect (p0, _) ->
5993 state.mstate <- Mzoomrect (p0, (x, y));
5994 G.postRedisplay "motion zoomrect";
5995 end;
5996 state.uioh
5998 method pmotion x y =
5999 begin match state.mode with
6000 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6001 let rec loop = function
6002 | [] ->
6003 if hooverpageno != -1
6004 then (
6005 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6006 G.postRedisplay "pmotion birdseye no hoover";
6008 | l :: rest ->
6009 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6010 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6011 then (
6012 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6013 G.postRedisplay "pmotion birdseye hoover";
6015 else loop rest
6017 loop state.layout
6019 | Textentry _ -> ()
6021 | LinkNav _
6022 | View ->
6023 match state.mstate with
6024 | Mnone -> updateunder x y
6025 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6027 end;
6028 state.uioh
6030 method infochanged _ = ()
6032 method scrollph =
6033 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6034 let p, h = scrollph state.y maxy in
6035 state.scrollw, p, h
6037 method scrollpw =
6038 let winw = state.winw - state.scrollw - 1 in
6039 let fwinw = float winw in
6040 let sw =
6041 let sw = fwinw /. float state.w in
6042 let sw = fwinw *. sw in
6043 max sw (float conf.scrollh)
6045 let position, sw =
6046 let f = state.w+winw in
6047 let r = float (winw-state.x) /. float f in
6048 let p = fwinw *. r in
6049 p-.sw/.2., sw
6051 let sw =
6052 if position +. sw > fwinw
6053 then fwinw -. position
6054 else sw
6056 state.hscrollh, position, sw
6058 method modehash =
6059 let modename =
6060 match state.mode with
6061 | LinkNav _ -> "links"
6062 | Textentry _ -> "textentry"
6063 | Birdseye _ -> "birdseye"
6064 | View -> "view"
6066 findkeyhash conf modename
6067 end;;
6069 module Config =
6070 struct
6071 open Parser
6073 let fontpath = ref "";;
6075 module KeyMap =
6076 Map.Make (struct type t = (int * int) let compare = compare end);;
6078 let unent s =
6079 let l = String.length s in
6080 let b = Buffer.create l in
6081 unent b s 0 l;
6082 Buffer.contents b;
6085 let home =
6086 try Sys.getenv "HOME"
6087 with exn ->
6088 prerr_endline
6089 ("Can not determine home directory location: " ^
6090 Printexc.to_string exn);
6094 let modifier_of_string = function
6095 | "alt" -> Wsi.altmask
6096 | "shift" -> Wsi.shiftmask
6097 | "ctrl" | "control" -> Wsi.ctrlmask
6098 | "meta" -> Wsi.metamask
6099 | _ -> 0
6102 let key_of_string =
6103 let r = Str.regexp "-" in
6104 fun s ->
6105 let elems = Str.full_split r s in
6106 let f n k m =
6107 let g s =
6108 let m1 = modifier_of_string s in
6109 if m1 = 0
6110 then (Wsi.namekey s, m)
6111 else (k, m lor m1)
6112 in function
6113 | Str.Delim s when n land 1 = 0 -> g s
6114 | Str.Text s -> g s
6115 | Str.Delim _ -> (k, m)
6117 let rec loop n k m = function
6118 | [] -> (k, m)
6119 | x :: xs ->
6120 let k, m = f n k m x in
6121 loop (n+1) k m xs
6123 loop 0 0 0 elems
6126 let keys_of_string =
6127 let r = Str.regexp "[ \t]" in
6128 fun s ->
6129 let elems = Str.split r s in
6130 List.map key_of_string elems
6133 let copykeyhashes c =
6134 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6137 let config_of c attrs =
6138 let apply c k v =
6140 match k with
6141 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6142 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6143 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6144 | "preload" -> { c with preload = bool_of_string v }
6145 | "page-bias" -> { c with pagebias = int_of_string v }
6146 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6147 | "horizontal-scroll-step" ->
6148 { c with hscrollstep = max (int_of_string v) 1 }
6149 | "auto-scroll-step" ->
6150 { c with autoscrollstep = max 0 (int_of_string v) }
6151 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6152 | "crop-hack" -> { c with crophack = bool_of_string v }
6153 | "throttle" ->
6154 let mw =
6155 match String.lowercase v with
6156 | "true" -> Some infinity
6157 | "false" -> None
6158 | f -> Some (float_of_string f)
6160 { c with maxwait = mw}
6161 | "highlight-links" -> { c with hlinks = bool_of_string v }
6162 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6163 | "vertical-margin" ->
6164 { c with interpagespace = max 0 (int_of_string v) }
6165 | "zoom" ->
6166 let zoom = float_of_string v /. 100. in
6167 let zoom = max zoom 0.0 in
6168 { c with zoom = zoom }
6169 | "presentation" -> { c with presentation = bool_of_string v }
6170 | "rotation-angle" -> { c with angle = int_of_string v }
6171 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6172 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6173 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6174 | "proportional-display" -> { c with proportional = bool_of_string v }
6175 | "pixmap-cache-size" ->
6176 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6177 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6178 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6179 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6180 | "persistent-location" -> { c with jumpback = bool_of_string v }
6181 | "background-color" -> { c with bgcolor = color_of_string v }
6182 | "scrollbar-in-presentation" ->
6183 { c with scrollbarinpm = bool_of_string v }
6184 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6185 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6186 | "mupdf-store-size" ->
6187 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6188 | "checkers" -> { c with checkers = bool_of_string v }
6189 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6190 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6191 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6192 | "uri-launcher" -> { c with urilauncher = unent v }
6193 | "path-launcher" -> { c with pathlauncher = unent v }
6194 | "color-space" -> { c with colorspace = colorspace_of_string v }
6195 | "invert-colors" -> { c with invert = bool_of_string v }
6196 | "brightness" -> { c with colorscale = float_of_string v }
6197 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6198 | "ghyllscroll" ->
6199 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6200 | "columns" ->
6201 let (n, _, _) as nab = multicolumns_of_string v in
6202 if n < 0
6203 then { c with columns = Csplit (-n, [||]) }
6204 else { c with columns = Cmulti (nab, [||]) }
6205 | "birds-eye-columns" ->
6206 { c with beyecolumns = Some (max (int_of_string v) 2) }
6207 | "selection-command" -> { c with selcmd = unent v }
6208 | "synctex-command" -> { c with stcmd = unent v }
6209 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6210 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6211 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6212 | "use-pbo" -> { c with usepbo = bool_of_string v }
6213 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6214 | _ -> c
6215 with exn ->
6216 prerr_endline ("Error processing attribute (`" ^
6217 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6220 let rec fold c = function
6221 | [] -> c
6222 | (k, v) :: rest ->
6223 let c = apply c k v in
6224 fold c rest
6226 fold { c with keyhashes = copykeyhashes c } attrs;
6229 let fromstring f pos n v d =
6230 try f v
6231 with exn ->
6232 dolog "Error processing attribute (%S=%S) at %d\n%s"
6233 n v pos (Printexc.to_string exn)
6238 let bookmark_of attrs =
6239 let rec fold title page rely visy = function
6240 | ("title", v) :: rest -> fold v page rely visy rest
6241 | ("page", v) :: rest -> fold title v rely visy rest
6242 | ("rely", v) :: rest -> fold title page v visy rest
6243 | ("visy", v) :: rest -> fold title page rely v rest
6244 | _ :: rest -> fold title page rely visy rest
6245 | [] -> title, page, rely, visy
6247 fold "invalid" "0" "0" "0" attrs
6250 let doc_of attrs =
6251 let rec fold path page rely pan visy = function
6252 | ("path", v) :: rest -> fold v page rely pan visy rest
6253 | ("page", v) :: rest -> fold path v rely pan visy rest
6254 | ("rely", v) :: rest -> fold path page v pan visy rest
6255 | ("pan", v) :: rest -> fold path page rely v visy rest
6256 | ("visy", v) :: rest -> fold path page rely pan v rest
6257 | _ :: rest -> fold path page rely pan visy rest
6258 | [] -> path, page, rely, pan, visy
6260 fold "" "0" "0" "0" "0" attrs
6263 let map_of attrs =
6264 let rec fold rs ls = function
6265 | ("out", v) :: rest -> fold v ls rest
6266 | ("in", v) :: rest -> fold rs v rest
6267 | _ :: rest -> fold ls rs rest
6268 | [] -> ls, rs
6270 fold "" "" attrs
6273 let setconf dst src =
6274 dst.scrollbw <- src.scrollbw;
6275 dst.scrollh <- src.scrollh;
6276 dst.icase <- src.icase;
6277 dst.preload <- src.preload;
6278 dst.pagebias <- src.pagebias;
6279 dst.verbose <- src.verbose;
6280 dst.scrollstep <- src.scrollstep;
6281 dst.maxhfit <- src.maxhfit;
6282 dst.crophack <- src.crophack;
6283 dst.autoscrollstep <- src.autoscrollstep;
6284 dst.maxwait <- src.maxwait;
6285 dst.hlinks <- src.hlinks;
6286 dst.underinfo <- src.underinfo;
6287 dst.interpagespace <- src.interpagespace;
6288 dst.zoom <- src.zoom;
6289 dst.presentation <- src.presentation;
6290 dst.angle <- src.angle;
6291 dst.cwinw <- src.cwinw;
6292 dst.cwinh <- src.cwinh;
6293 dst.savebmarks <- src.savebmarks;
6294 dst.memlimit <- src.memlimit;
6295 dst.proportional <- src.proportional;
6296 dst.texcount <- src.texcount;
6297 dst.sliceheight <- src.sliceheight;
6298 dst.thumbw <- src.thumbw;
6299 dst.jumpback <- src.jumpback;
6300 dst.bgcolor <- src.bgcolor;
6301 dst.scrollbarinpm <- src.scrollbarinpm;
6302 dst.tilew <- src.tilew;
6303 dst.tileh <- src.tileh;
6304 dst.mustoresize <- src.mustoresize;
6305 dst.checkers <- src.checkers;
6306 dst.aalevel <- src.aalevel;
6307 dst.trimmargins <- src.trimmargins;
6308 dst.trimfuzz <- src.trimfuzz;
6309 dst.urilauncher <- src.urilauncher;
6310 dst.colorspace <- src.colorspace;
6311 dst.invert <- src.invert;
6312 dst.colorscale <- src.colorscale;
6313 dst.redirectstderr <- src.redirectstderr;
6314 dst.ghyllscroll <- src.ghyllscroll;
6315 dst.columns <- src.columns;
6316 dst.beyecolumns <- src.beyecolumns;
6317 dst.selcmd <- src.selcmd;
6318 dst.updatecurs <- src.updatecurs;
6319 dst.pathlauncher <- src.pathlauncher;
6320 dst.keyhashes <- copykeyhashes src;
6321 dst.hfsize <- src.hfsize;
6322 dst.hscrollstep <- src.hscrollstep;
6323 dst.pgscale <- src.pgscale;
6324 dst.usepbo <- src.usepbo;
6325 dst.wheelbypage <- src.wheelbypage;
6326 dst.stcmd <- src.stcmd;
6329 let get s =
6330 let h = Hashtbl.create 10 in
6331 let dc = { defconf with angle = defconf.angle } in
6332 let rec toplevel v t spos _ =
6333 match t with
6334 | Vdata | Vcdata | Vend -> v
6335 | Vopen ("llppconfig", _, closed) ->
6336 if closed
6337 then v
6338 else { v with f = llppconfig }
6339 | Vopen _ ->
6340 error "unexpected subelement at top level" s spos
6341 | Vclose _ -> error "unexpected close at top level" s spos
6343 and llppconfig v t spos _ =
6344 match t with
6345 | Vdata | Vcdata -> v
6346 | Vend -> error "unexpected end of input in llppconfig" s spos
6347 | Vopen ("defaults", attrs, closed) ->
6348 let c = config_of dc attrs in
6349 setconf dc c;
6350 if closed
6351 then v
6352 else { v with f = defaults }
6354 | Vopen ("ui-font", attrs, closed) ->
6355 let rec getsize size = function
6356 | [] -> size
6357 | ("size", v) :: rest ->
6358 let size =
6359 fromstring int_of_string spos "size" v fstate.fontsize in
6360 getsize size rest
6361 | l -> getsize size l
6363 fstate.fontsize <- getsize fstate.fontsize attrs;
6364 if closed
6365 then v
6366 else { v with f = uifont (Buffer.create 10) }
6368 | Vopen ("doc", attrs, closed) ->
6369 let pathent, spage, srely, span, svisy = doc_of attrs in
6370 let path = unent pathent
6371 and pageno = fromstring int_of_string spos "page" spage 0
6372 and rely = fromstring float_of_string spos "rely" srely 0.0
6373 and pan = fromstring int_of_string spos "pan" span 0
6374 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6375 let c = config_of dc attrs in
6376 let anchor = (pageno, rely, visy) in
6377 if closed
6378 then (Hashtbl.add h path (c, [], pan, anchor); v)
6379 else { v with f = doc path pan anchor c [] }
6381 | Vopen _ ->
6382 error "unexpected subelement in llppconfig" s spos
6384 | Vclose "llppconfig" -> { v with f = toplevel }
6385 | Vclose _ -> error "unexpected close in llppconfig" s spos
6387 and defaults v t spos _ =
6388 match t with
6389 | Vdata | Vcdata -> v
6390 | Vend -> error "unexpected end of input in defaults" s spos
6391 | Vopen ("keymap", attrs, closed) ->
6392 let modename =
6393 try List.assoc "mode" attrs
6394 with Not_found -> "global" in
6395 if closed
6396 then v
6397 else
6398 let ret keymap =
6399 let h = findkeyhash dc modename in
6400 KeyMap.iter (Hashtbl.replace h) keymap;
6401 defaults
6403 { v with f = pkeymap ret KeyMap.empty }
6405 | Vopen (_, _, _) ->
6406 error "unexpected subelement in defaults" s spos
6408 | Vclose "defaults" ->
6409 { v with f = llppconfig }
6411 | Vclose _ -> error "unexpected close in defaults" s spos
6413 and uifont b v t spos epos =
6414 match t with
6415 | Vdata | Vcdata ->
6416 Buffer.add_substring b s spos (epos - spos);
6418 | Vopen (_, _, _) ->
6419 error "unexpected subelement in ui-font" s spos
6420 | Vclose "ui-font" ->
6421 if String.length !fontpath = 0
6422 then fontpath := Buffer.contents b;
6423 { v with f = llppconfig }
6424 | Vclose _ -> error "unexpected close in ui-font" s spos
6425 | Vend -> error "unexpected end of input in ui-font" s spos
6427 and doc path pan anchor c bookmarks v t spos _ =
6428 match t with
6429 | Vdata | Vcdata -> v
6430 | Vend -> error "unexpected end of input in doc" s spos
6431 | Vopen ("bookmarks", _, closed) ->
6432 if closed
6433 then v
6434 else { v with f = pbookmarks path pan anchor c bookmarks }
6436 | Vopen ("keymap", attrs, closed) ->
6437 let modename =
6438 try List.assoc "mode" attrs
6439 with Not_found -> "global"
6441 if closed
6442 then v
6443 else
6444 let ret keymap =
6445 let h = findkeyhash c modename in
6446 KeyMap.iter (Hashtbl.replace h) keymap;
6447 doc path pan anchor c bookmarks
6449 { v with f = pkeymap ret KeyMap.empty }
6451 | Vopen (_, _, _) ->
6452 error "unexpected subelement in doc" s spos
6454 | Vclose "doc" ->
6455 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6456 { v with f = llppconfig }
6458 | Vclose _ -> error "unexpected close in doc" s spos
6460 and pkeymap ret keymap v t spos _ =
6461 match t with
6462 | Vdata | Vcdata -> v
6463 | Vend -> error "unexpected end of input in keymap" s spos
6464 | Vopen ("map", attrs, closed) ->
6465 let r, l = map_of attrs in
6466 let kss = fromstring keys_of_string spos "in" r [] in
6467 let lss = fromstring keys_of_string spos "out" l [] in
6468 let keymap =
6469 match kss with
6470 | [] -> keymap
6471 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6472 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6474 if closed
6475 then { v with f = pkeymap ret keymap }
6476 else
6477 let f () = v in
6478 { v with f = skip "map" f }
6480 | Vopen _ ->
6481 error "unexpected subelement in keymap" s spos
6483 | Vclose "keymap" ->
6484 { v with f = ret keymap }
6486 | Vclose _ -> error "unexpected close in keymap" s spos
6488 and pbookmarks path pan anchor c bookmarks v t spos _ =
6489 match t with
6490 | Vdata | Vcdata -> v
6491 | Vend -> error "unexpected end of input in bookmarks" s spos
6492 | Vopen ("item", attrs, closed) ->
6493 let titleent, spage, srely, svisy = bookmark_of attrs in
6494 let page = fromstring int_of_string spos "page" spage 0
6495 and rely = fromstring float_of_string spos "rely" srely 0.0
6496 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6497 let bookmarks =
6498 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6500 if closed
6501 then { v with f = pbookmarks path pan anchor c bookmarks }
6502 else
6503 let f () = v in
6504 { v with f = skip "item" f }
6506 | Vopen _ ->
6507 error "unexpected subelement in bookmarks" s spos
6509 | Vclose "bookmarks" ->
6510 { v with f = doc path pan anchor c bookmarks }
6512 | Vclose _ -> error "unexpected close in bookmarks" s spos
6514 and skip tag f v t spos _ =
6515 match t with
6516 | Vdata | Vcdata -> v
6517 | Vend ->
6518 error ("unexpected end of input in skipped " ^ tag) s spos
6519 | Vopen (tag', _, closed) ->
6520 if closed
6521 then v
6522 else
6523 let f' () = { v with f = skip tag f } in
6524 { v with f = skip tag' f' }
6525 | Vclose ctag ->
6526 if tag = ctag
6527 then f ()
6528 else error ("unexpected close in skipped " ^ tag) s spos
6531 parse { f = toplevel; accu = () } s;
6532 h, dc;
6535 let do_load f ic =
6537 let len = in_channel_length ic in
6538 let s = String.create len in
6539 really_input ic s 0 len;
6540 f s;
6541 with
6542 | Parse_error (msg, s, pos) ->
6543 let subs = subs s pos in
6544 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6545 failwith ("parse error: " ^ s)
6547 | exn ->
6548 failwith ("config load error: " ^ Printexc.to_string exn)
6551 let defconfpath =
6552 let dir =
6554 let dir = Filename.concat home ".config" in
6555 if Sys.is_directory dir then dir else home
6556 with _ -> home
6558 Filename.concat dir "llpp.conf"
6561 let confpath = ref defconfpath;;
6563 let load1 f =
6564 if Sys.file_exists !confpath
6565 then
6566 match
6567 (try Some (open_in_bin !confpath)
6568 with exn ->
6569 prerr_endline
6570 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6571 Printexc.to_string exn);
6572 None
6574 with
6575 | Some ic ->
6576 let success =
6578 f (do_load get ic)
6579 with exn ->
6580 prerr_endline
6581 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6582 Printexc.to_string exn);
6583 false
6585 close_in ic;
6586 success
6588 | None -> false
6589 else
6590 f (Hashtbl.create 0, defconf)
6593 let load () =
6594 let f (h, dc) =
6595 let pc, pb, px, pa =
6597 Hashtbl.find h (Filename.basename state.path)
6598 with Not_found -> dc, [], 0, emptyanchor
6600 setconf defconf dc;
6601 setconf conf pc;
6602 state.bookmarks <- pb;
6603 state.x <- px;
6604 state.scrollw <- conf.scrollbw;
6605 if conf.jumpback
6606 then state.anchor <- pa;
6607 cbput state.hists.nav pa;
6608 true
6610 load1 f
6613 let add_attrs bb always dc c =
6614 let ob s a b =
6615 if always || a != b
6616 then Printf.bprintf bb "\n %s='%b'" s a
6617 and oi s a b =
6618 if always || a != b
6619 then Printf.bprintf bb "\n %s='%d'" s a
6620 and oI s a b =
6621 if always || a != b
6622 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6623 and oz s a b =
6624 if always || a <> b
6625 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6626 and oF s a b =
6627 if always || a <> b
6628 then Printf.bprintf bb "\n %s='%f'" s a
6629 and oc s a b =
6630 if always || a <> b
6631 then
6632 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6633 and oC s a b =
6634 if always || a <> b
6635 then
6636 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6637 and oR s a b =
6638 if always || a <> b
6639 then
6640 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6641 and os s a b =
6642 if always || a <> b
6643 then
6644 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6645 and og s a b =
6646 if always || a <> b
6647 then
6648 match a with
6649 | None -> ()
6650 | Some (_N, _A, _B) ->
6651 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6652 and oW s a b =
6653 if always || a <> b
6654 then
6655 let v =
6656 match a with
6657 | None -> "false"
6658 | Some f ->
6659 if f = infinity
6660 then "true"
6661 else string_of_float f
6663 Printf.bprintf bb "\n %s='%s'" s v
6664 and oco s a b =
6665 if always || a <> b
6666 then
6667 match a with
6668 | Cmulti ((n, a, b), _) when n > 1 ->
6669 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6670 | Csplit (n, _) when n > 1 ->
6671 Printf.bprintf bb "\n %s='%d'" s ~-n
6672 | _ -> ()
6673 and obeco s a b =
6674 if always || a <> b
6675 then
6676 match a with
6677 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6678 | _ -> ()
6680 oi "width" c.cwinw dc.cwinw;
6681 oi "height" c.cwinh dc.cwinh;
6682 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6683 oi "scroll-handle-height" c.scrollh dc.scrollh;
6684 ob "case-insensitive-search" c.icase dc.icase;
6685 ob "preload" c.preload dc.preload;
6686 oi "page-bias" c.pagebias dc.pagebias;
6687 oi "scroll-step" c.scrollstep dc.scrollstep;
6688 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6689 ob "max-height-fit" c.maxhfit dc.maxhfit;
6690 ob "crop-hack" c.crophack dc.crophack;
6691 oW "throttle" c.maxwait dc.maxwait;
6692 ob "highlight-links" c.hlinks dc.hlinks;
6693 ob "under-cursor-info" c.underinfo dc.underinfo;
6694 oi "vertical-margin" c.interpagespace dc.interpagespace;
6695 oz "zoom" c.zoom dc.zoom;
6696 ob "presentation" c.presentation dc.presentation;
6697 oi "rotation-angle" c.angle dc.angle;
6698 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6699 ob "proportional-display" c.proportional dc.proportional;
6700 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6701 oi "tex-count" c.texcount dc.texcount;
6702 oi "slice-height" c.sliceheight dc.sliceheight;
6703 oi "thumbnail-width" c.thumbw dc.thumbw;
6704 ob "persistent-location" c.jumpback dc.jumpback;
6705 oc "background-color" c.bgcolor dc.bgcolor;
6706 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6707 oi "tile-width" c.tilew dc.tilew;
6708 oi "tile-height" c.tileh dc.tileh;
6709 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6710 ob "checkers" c.checkers dc.checkers;
6711 oi "aalevel" c.aalevel dc.aalevel;
6712 ob "trim-margins" c.trimmargins dc.trimmargins;
6713 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6714 os "uri-launcher" c.urilauncher dc.urilauncher;
6715 os "path-launcher" c.pathlauncher dc.pathlauncher;
6716 oC "color-space" c.colorspace dc.colorspace;
6717 ob "invert-colors" c.invert dc.invert;
6718 oF "brightness" c.colorscale dc.colorscale;
6719 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6720 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6721 oco "columns" c.columns dc.columns;
6722 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6723 os "selection-command" c.selcmd dc.selcmd;
6724 os "synctex-command" c.stcmd dc.stcmd;
6725 ob "update-cursor" c.updatecurs dc.updatecurs;
6726 oi "hint-font-size" c.hfsize dc.hfsize;
6727 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6728 oF "page-scroll-scale" c.pgscale dc.pgscale;
6729 ob "use-pbo" c.usepbo dc.usepbo;
6730 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6733 let keymapsbuf always dc c =
6734 let bb = Buffer.create 16 in
6735 let rec loop = function
6736 | [] -> ()
6737 | (modename, h) :: rest ->
6738 let dh = findkeyhash dc modename in
6739 if always || h <> dh
6740 then (
6741 if Hashtbl.length h > 0
6742 then (
6743 if Buffer.length bb > 0
6744 then Buffer.add_char bb '\n';
6745 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6746 Hashtbl.iter (fun i o ->
6747 let isdifferent = always ||
6749 let dO = Hashtbl.find dh i in
6750 dO <> o
6751 with Not_found -> true
6753 if isdifferent
6754 then
6755 let addkm (k, m) =
6756 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6757 if Wsi.withalt m then Buffer.add_string bb "alt-";
6758 if Wsi.withshift m then Buffer.add_string bb "shift-";
6759 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6760 Buffer.add_string bb (Wsi.keyname k);
6762 let addkms l =
6763 let rec loop = function
6764 | [] -> ()
6765 | km :: [] -> addkm km
6766 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6768 loop l
6770 Buffer.add_string bb "<map in='";
6771 addkm i;
6772 match o with
6773 | KMinsrt km ->
6774 Buffer.add_string bb "' out='";
6775 addkm km;
6776 Buffer.add_string bb "'/>\n"
6778 | KMinsrl kms ->
6779 Buffer.add_string bb "' out='";
6780 addkms kms;
6781 Buffer.add_string bb "'/>\n"
6783 | KMmulti (ins, kms) ->
6784 Buffer.add_char bb ' ';
6785 addkms ins;
6786 Buffer.add_string bb "' out='";
6787 addkms kms;
6788 Buffer.add_string bb "'/>\n"
6789 ) h;
6790 Buffer.add_string bb "</keymap>";
6793 loop rest
6795 loop c.keyhashes;
6799 let save () =
6800 let uifontsize = fstate.fontsize in
6801 let bb = Buffer.create 32768 in
6802 let w, h =
6803 List.fold_left
6804 (fun (w, h) ws ->
6805 match ws with
6806 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6807 | Wsi.MaxVert -> (w, conf.cwinh)
6808 | Wsi.MaxHorz -> (conf.cwinw, h)
6810 (state.winw, state.winh) state.winstate
6812 conf.cwinw <- w;
6813 conf.cwinh <- h;
6814 let f (h, dc) =
6815 let dc = if conf.bedefault then conf else dc in
6816 Buffer.add_string bb "<llppconfig>\n";
6818 if String.length !fontpath > 0
6819 then
6820 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6821 uifontsize
6822 !fontpath
6823 else (
6824 if uifontsize <> 14
6825 then
6826 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6829 Buffer.add_string bb "<defaults ";
6830 add_attrs bb true dc dc;
6831 let kb = keymapsbuf true dc dc in
6832 if Buffer.length kb > 0
6833 then (
6834 Buffer.add_string bb ">\n";
6835 Buffer.add_buffer bb kb;
6836 Buffer.add_string bb "\n</defaults>\n";
6838 else Buffer.add_string bb "/>\n";
6840 let adddoc path pan anchor c bookmarks =
6841 if bookmarks == [] && c = dc && anchor = emptyanchor
6842 then ()
6843 else (
6844 Printf.bprintf bb "<doc path='%s'"
6845 (enent path 0 (String.length path));
6847 if anchor <> emptyanchor
6848 then (
6849 let n, rely, visy = anchor in
6850 Printf.bprintf bb " page='%d'" n;
6851 if rely > 1e-6
6852 then
6853 Printf.bprintf bb " rely='%f'" rely
6855 if abs_float visy > 1e-6
6856 then
6857 Printf.bprintf bb " visy='%f'" visy
6861 if pan != 0
6862 then Printf.bprintf bb " pan='%d'" pan;
6864 add_attrs bb false dc c;
6865 let kb = keymapsbuf false dc c in
6867 begin match bookmarks with
6868 | [] ->
6869 if Buffer.length kb > 0
6870 then (
6871 Buffer.add_string bb ">\n";
6872 Buffer.add_buffer bb kb;
6873 Buffer.add_string bb "\n</doc>\n";
6875 else Buffer.add_string bb "/>\n"
6876 | _ ->
6877 Buffer.add_string bb ">\n<bookmarks>\n";
6878 List.iter (fun (title, _level, (page, rely, visy)) ->
6879 Printf.bprintf bb
6880 "<item title='%s' page='%d'"
6881 (enent title 0 (String.length title))
6882 page
6884 if rely > 1e-6
6885 then
6886 Printf.bprintf bb " rely='%f'" rely
6888 if abs_float visy > 1e-6
6889 then
6890 Printf.bprintf bb " visy='%f'" visy
6892 Buffer.add_string bb "/>\n";
6893 ) bookmarks;
6894 Buffer.add_string bb "</bookmarks>";
6895 if Buffer.length kb > 0
6896 then (
6897 Buffer.add_string bb "\n";
6898 Buffer.add_buffer bb kb;
6900 Buffer.add_string bb "\n</doc>\n";
6901 end;
6905 let pan, conf =
6906 match state.mode with
6907 | Birdseye (c, pan, _, _, _) ->
6908 let beyecolumns =
6909 match conf.columns with
6910 | Cmulti ((c, _, _), _) -> Some c
6911 | Csingle _ -> None
6912 | Csplit _ -> None
6913 and columns =
6914 match c.columns with
6915 | Cmulti (c, _) -> Cmulti (c, [||])
6916 | Csingle _ -> Csingle [||]
6917 | Csplit _ -> failwith "quit from bird's eye while split"
6919 pan, { c with beyecolumns = beyecolumns; columns = columns }
6920 | _ -> state.x, conf
6922 let basename = Filename.basename state.path in
6923 adddoc basename pan (getanchor ())
6924 (let conf =
6925 let autoscrollstep =
6926 match state.autoscroll with
6927 | Some step -> step
6928 | None -> conf.autoscrollstep
6930 match state.mode with
6931 | Birdseye (bc, _, _, _, _) ->
6932 { conf with
6933 zoom = bc.zoom;
6934 presentation = bc.presentation;
6935 interpagespace = bc.interpagespace;
6936 maxwait = bc.maxwait;
6937 autoscrollstep = autoscrollstep }
6938 | _ -> { conf with autoscrollstep = autoscrollstep }
6939 in conf)
6940 (if conf.savebmarks then state.bookmarks else []);
6942 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6943 if basename <> path
6944 then adddoc path x anchor c bookmarks
6945 ) h;
6946 Buffer.add_string bb "</llppconfig>\n";
6947 true;
6949 if load1 f && Buffer.length bb > 0
6950 then
6952 let tmp = !confpath ^ ".tmp" in
6953 let oc = open_out_bin tmp in
6954 Buffer.output_buffer oc bb;
6955 close_out oc;
6956 Unix.rename tmp !confpath;
6957 with exn ->
6958 prerr_endline
6959 ("error while saving configuration: " ^ Printexc.to_string exn)
6961 end;;
6963 let adderrmsg src msg =
6964 Buffer.add_string state.errmsgs msg;
6965 state.newerrmsgs <- true;
6966 G.postRedisplay src
6969 let adderrfmt src fmt =
6970 Format.kprintf (fun s -> adderrmsg src s) fmt;
6973 let onpagerect pageno f =
6974 let b =
6975 match conf.columns with
6976 | Cmulti (_, b) -> b
6977 | Csingle b -> b
6978 | Csplit (_, b) -> b
6980 if pageno >= 0 && pageno < Array.length b
6981 then
6982 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
6983 let r = getpdimrect pdimno in
6984 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
6987 let ract cmds =
6988 let cl = splitatspace cmds in
6989 let scan s fmt f =
6990 try Scanf.sscanf s fmt f
6991 with exn ->
6992 adderrfmt "remote exec"
6993 "error processing '%S': %s\n" cmds (Printexc.to_string exn)
6995 match cl with
6996 | "reload" :: [] -> reload ()
6997 | "goto" :: args :: [] ->
6998 let cmd, _ = state.geomcmds in
6999 scan args "%u %f %f"
7000 (fun pageno x y ->
7001 let dogo prevf () =
7002 onpagerect pageno (fun w h ->
7003 let top = y /. h in
7004 let _,w1,_,leftx = getpagedim pageno in
7005 let sw = float w1 /. w in
7006 let x = sw *. x in
7007 let x = leftx + state.x + truncate x in
7008 let newpan =
7009 if x < 0 || x >= state.winw - state.scrollw
7010 then (state.x <- state.x - x; true)
7011 else false
7013 let y, h = getpageyh pageno in
7014 let y' = y + truncate (top *. float h) in
7015 let dy = y' - state.y in
7016 if newpan || not (dy > 0 && dy < state.winh - state.hscrollh)
7017 then (
7018 let y =
7019 if conf.presentation
7020 then
7021 if abs (y - y') > state.winh - state.hscrollh
7022 then y'
7023 else y
7024 else y';
7026 gotoy y;
7027 state.wthack <- !wtmode && not (layoutready state.layout);
7030 prevf ();
7032 if String.length cmd = 0
7033 then dogo noreprf ()
7034 else state.reprf <- dogo state.reprf
7036 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7037 | "rect" :: args :: [] ->
7038 scan args "%u %u %f %f %f %f"
7039 (fun pageno color x0 y0 x1 y1 ->
7040 onpagerect pageno (fun w h ->
7041 let _,w1,h1,_ = getpagedim pageno in
7042 let sw = float w1 /. w
7043 and sh = float h1 /. h in
7044 let x0s = x0 *. sw
7045 and x1s = x1 *. sw
7046 and y0s = y0 *. sh
7047 and y1s = y1 *. sh in
7048 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7049 debugrect rect;
7050 state.rects <- (pageno, color, rect) :: state.rects;
7051 G.postRedisplay "rect";
7054 | "activatewin" :: [] -> Wsi.activatewin ()
7055 | "quit" :: [] -> raise Quit
7056 | _ ->
7057 adderrfmt "remote command"
7058 "error processing remote command: %S\n" cmds;
7061 let remote =
7062 let scratch = String.create 80 in
7063 let buf = Buffer.create 80 in
7064 fun fd ->
7065 let rec tempfr () =
7066 try Some (Unix.read fd scratch 0 80)
7067 with
7068 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7069 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7070 | exn -> raise exn
7072 match tempfr () with
7073 | None -> Some fd
7074 | Some n ->
7075 if n = 0
7076 then (
7077 Unix.close fd;
7078 if Buffer.length buf > 0
7079 then (
7080 let s = Buffer.contents buf in
7081 Buffer.clear buf;
7082 ract s;
7084 None
7086 else
7087 let rec eat ppos =
7088 let nlpos =
7090 let pos = String.index_from scratch ppos '\n' in
7091 if pos >= n then -1 else pos
7092 with Not_found -> -1
7094 if nlpos >= 0
7095 then (
7096 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7097 let s = Buffer.contents buf in
7098 Buffer.clear buf;
7099 ract s;
7100 eat (nlpos+1);
7102 else (
7103 Buffer.add_substring buf scratch ppos (n-ppos);
7104 Some fd
7106 in eat 0
7109 let remoteopen path =
7110 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7111 with exn ->
7112 adderrfmt "remoteopen" "error opening %S: %s" path (Printexc.to_string exn);
7113 None
7116 let () =
7117 let trimcachepath = ref "" in
7118 let rcmdpath = ref "" in
7119 Arg.parse
7120 (Arg.align
7121 [("-p", Arg.String (fun s -> state.password <- s) ,
7122 "<password> Set password");
7124 ("-f", Arg.String (fun s -> Config.fontpath := s),
7125 "<path> Set path to the user interface font");
7127 ("-c", Arg.String (fun s -> Config.confpath := s),
7128 "<path> Set path to the configuration file");
7130 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7131 "<path> Set path to the trim cache file");
7133 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7134 "<named-destination> Set named destination");
7136 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7138 ("-remote", Arg.String (fun s -> rcmdpath := s),
7139 "<path> Set path to the remote commands source");
7141 ("-v", Arg.Unit (fun () ->
7142 Printf.printf
7143 "%s\nconfiguration path: %s\n"
7144 (version ())
7145 Config.defconfpath
7147 exit 0), " Print version and exit");
7150 (fun s -> state.path <- s)
7151 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7153 if String.length state.path = 0
7154 then (prerr_endline "file name missing"; exit 1);
7156 if not (Config.load ())
7157 then prerr_endline "failed to load configuration";
7159 let globalkeyhash = findkeyhash conf "global" in
7160 let wsfd, winw, winh = Wsi.init (object
7161 method expose =
7162 state.wthack <- false;
7163 if nogeomcmds state.geomcmds || platform == Posx
7164 then display ()
7165 else (
7166 GlClear.color (scalecolor2 conf.bgcolor);
7167 GlClear.clear [`color];
7169 method display = display ()
7170 method reshape w h = reshape w h
7171 method mouse b d x y m = mouse b d x y m
7172 method motion x y = state.mpos <- (x, y); motion x y
7173 method pmotion x y = state.mpos <- (x, y); pmotion x y
7174 method key k m =
7175 let mascm = m land (
7176 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7177 ) in
7178 match state.keystate with
7179 | KSnone ->
7180 let km = k, mascm in
7181 begin
7182 match
7183 let modehash = state.uioh#modehash in
7184 try Hashtbl.find modehash km
7185 with Not_found ->
7186 try Hashtbl.find globalkeyhash km
7187 with Not_found -> KMinsrt (k, m)
7188 with
7189 | KMinsrt (k, m) -> keyboard k m
7190 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7191 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7193 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7194 List.iter (fun (k, m) -> keyboard k m) insrt;
7195 state.keystate <- KSnone
7196 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7197 state.keystate <- KSinto (keys, insrt)
7198 | _ ->
7199 state.keystate <- KSnone
7201 method enter x y = state.mpos <- (x, y); pmotion x y
7202 method leave = state.mpos <- (-1, -1)
7203 method winstate wsl = state.winstate <- wsl
7204 method quit = raise Quit
7205 end) conf.cwinw conf.cwinh (platform = Posx) in
7207 state.wsfd <- wsfd;
7209 if not (
7210 List.exists GlMisc.check_extension
7211 [ "GL_ARB_texture_rectangle"
7212 ; "GL_EXT_texture_recangle"
7213 ; "GL_NV_texture_rectangle" ]
7215 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7217 let cr, sw =
7218 match Ne.pipe () with
7219 | Ne.Exn exn ->
7220 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
7221 exit 1
7222 | Ne.Res rw -> rw
7223 and sr, cw =
7224 match Ne.pipe () with
7225 | Ne.Exn exn ->
7226 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7227 exit 1
7228 | Ne.Res rw -> rw
7231 cloexec cr;
7232 cloexec sw;
7233 cloexec sr;
7234 cloexec cw;
7236 setcheckers conf.checkers;
7237 redirectstderr ();
7239 init (cr, cw) (
7240 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7241 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7242 !Config.fontpath, !trimcachepath,
7243 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7245 state.sr <- sr;
7246 state.sw <- sw;
7247 state.text <- "Opening " ^ (mbtoutf8 state.path);
7248 reshape winw winh;
7249 opendoc state.path state.password;
7250 state.uioh <- uioh;
7252 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7253 let optrfd =
7254 ref (
7255 if String.length !rcmdpath > 0
7256 then remoteopen !rcmdpath
7257 else None
7261 let rec loop deadline =
7262 let r =
7263 match state.errfd with
7264 | None -> [state.sr; state.wsfd]
7265 | Some fd -> [state.sr; state.wsfd; fd]
7267 let r =
7268 match !optrfd with
7269 | None -> r
7270 | Some fd -> fd :: r
7272 if state.redisplay && not state.wthack
7273 then (
7274 state.redisplay <- false;
7275 display ();
7277 let timeout =
7278 let now = now () in
7279 if deadline > now
7280 then (
7281 if deadline = infinity
7282 then ~-.1.0
7283 else max 0.0 (deadline -. now)
7285 else 0.0
7287 let r, _, _ =
7288 try Unix.select r [] [] timeout
7289 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7291 begin match r with
7292 | [] ->
7293 state.ghyll None;
7294 let newdeadline =
7295 if state.ghyll == noghyll
7296 then
7297 match state.autoscroll with
7298 | Some step when step != 0 ->
7299 let y = state.y + step in
7300 let y =
7301 if y < 0
7302 then state.maxy
7303 else if y >= state.maxy then 0 else y
7305 gotoy y;
7306 if state.mode = View
7307 then state.text <- "";
7308 deadline +. 0.01
7309 | _ -> infinity
7310 else deadline +. 0.01
7312 loop newdeadline
7314 | l ->
7315 let rec checkfds = function
7316 | [] -> ()
7317 | fd :: rest when fd = state.sr ->
7318 let cmd = readcmd state.sr in
7319 act cmd;
7320 checkfds rest
7322 | fd :: rest when fd = state.wsfd ->
7323 Wsi.readresp fd;
7324 checkfds rest
7326 | fd :: rest when Some fd = !optrfd ->
7327 begin match remote fd with
7328 | None -> optrfd := remoteopen !rcmdpath;
7329 | opt -> optrfd := opt
7330 end;
7331 checkfds rest
7333 | fd :: rest ->
7334 let s = String.create 80 in
7335 let n = tempfailureretry (Unix.read fd s 0) 80 in
7336 if conf.redirectstderr
7337 then (
7338 Buffer.add_substring state.errmsgs s 0 n;
7339 state.newerrmsgs <- true;
7340 state.redisplay <- true;
7342 else (
7343 prerr_string (String.sub s 0 n);
7344 flush stderr;
7346 checkfds rest
7348 checkfds l;
7349 let newdeadline =
7350 let deadline1 =
7351 if deadline = infinity
7352 then now () +. 0.01
7353 else deadline
7355 match state.autoscroll with
7356 | Some step when step != 0 -> deadline1
7357 | _ -> if state.ghyll == noghyll then infinity else deadline1
7359 loop newdeadline
7360 end;
7363 loop infinity;
7364 with Quit ->
7365 Config.save ();