Safety nets
[llpp.git] / main.ml
blobbff53478ecc91d9576a61c7109906928123b9c08
1 exception Quit;;
3 let tempfailureretry = Wsi.tempfailureretry;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 let dolog fmt = Printf.kprintf prerr_endline fmt;;
17 let now = Unix.gettimeofday;;
19 type params = (angle * proportional * trimparams
20 * texcount * sliceheight * memsize
21 * colorspace * fontpath * trimcachepath
22 * haspbo)
23 and pageno = int
24 and width = int
25 and height = int
26 and leftx = int
27 and opaque = string
28 and recttype = int
29 and pixmapsize = int
30 and angle = int
31 and proportional = bool
32 and trimmargins = bool
33 and interpagespace = int
34 and texcount = int
35 and sliceheight = int
36 and gen = int
37 and top = float
38 and dtop = float
39 and fontpath = string
40 and trimcachepath = string
41 and memsize = int
42 and aalevel = int
43 and irect = (int * int * int * int)
44 and trimparams = (trimmargins * irect)
45 and colorspace = | Rgb | Bgr | Gray
46 and haspbo = bool
49 type link =
50 | Lnotfound
51 | Lfound of int
52 and linkdir =
53 | LDfirst
54 | LDlast
55 | LDfirstvisible of (int * int * int)
56 | LDleft of int
57 | LDright of int
58 | LDdown of int
59 | LDup of int
62 type pagewithlinks =
63 | Pwlnotfound
64 | Pwl of int
67 type keymap =
68 | KMinsrt of key
69 | KMinsrl of key list
70 | KMmulti of key list * key list
71 and key = int * int
72 and keyhash = (key, keymap) Hashtbl.t
73 and keystate =
74 | KSnone
75 | KSinto of (key list * key list)
78 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
79 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
81 type pipe = (Unix.file_descr * Unix.file_descr);;
83 external init : pipe -> params -> unit = "ml_init";;
84 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
85 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
86 external getpdimrect : int -> float array = "ml_getpdimrect";;
87 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
88 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
89 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
90 external measurestr : int -> string -> float = "ml_measure_string";;
91 external getmaxw : unit -> float = "ml_getmaxw";;
92 external postprocess :
93 opaque -> int -> int -> int -> (int * string * int) -> int
94 = "ml_postprocess";;
95 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
96 external platform : unit -> platform = "ml_platform";;
97 external setaalevel : int -> unit = "ml_setaalevel";;
98 external realloctexts : int -> bool = "ml_realloctexts";;
99 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
100 external findlink : opaque -> linkdir -> link = "ml_findlink";;
101 external getlink : opaque -> int -> under = "ml_getlink";;
102 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
103 external getlinkcount : opaque -> int = "ml_getlinkcount";;
104 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
105 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
106 external mbtoutf8 : string -> string = "ml_mbtoutf8";;
107 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
108 external freepbo : string -> unit = "ml_freepbo";;
109 external unmappbo : string -> unit = "ml_unmappbo";;
110 external pbousable : unit -> bool = "ml_pbo_usable";;
111 external unproject : opaque -> int -> int -> (int * int) option
112 = "ml_unproject";;
114 let platform_to_string = function
115 | Punknown -> "unknown"
116 | Plinux -> "Linux"
117 | Posx -> "OSX"
118 | Psun -> "Sun"
119 | Pfreebsd -> "FreeBSD"
120 | Pdragonflybsd -> "DragonflyBSD"
121 | Popenbsd -> "OpenBSD"
122 | Pnetbsd -> "NetBSD"
123 | Pcygwin -> "Cygwin"
126 let platform = platform ();;
128 let popen cmd fda =
129 if platform = Pcygwin
130 then (
131 let sh = "/bin/sh" in
132 let args = [|sh; "-c"; cmd|] in
133 let rec std si so se = function
134 | [] -> si, so, se
135 | (fd, 0) :: rest -> std fd so se rest
136 | (fd, -1) :: rest ->
137 Unix.set_close_on_exec fd;
138 std si so se rest
139 | (_, n) :: _ ->
140 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
142 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
143 ignore (Unix.create_process sh args si so se)
145 else popen cmd fda;
148 type x = int
149 and y = int
150 and tilex = int
151 and tiley = int
152 and tileparams = (x * y * width * height * tilex * tiley)
155 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
157 type mpos = int * int
158 and mstate =
159 | Msel of (mpos * mpos)
160 | Mpan of mpos
161 | Mscrolly | Mscrollx
162 | Mzoom of (int * int)
163 | Mzoomrect of (mpos * mpos)
164 | Mnone
167 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
168 and onkey = string -> int -> te
169 and ondone = string -> unit
170 and histcancel = unit -> unit
171 and onhist = ((histcmd -> string) * histcancel)
172 and histcmd = HCnext | HCprev | HCfirst | HClast
173 and cancelonempty = bool
174 and te =
175 | TEstop
176 | TEdone of string
177 | TEcont of string
178 | TEswitch of textentry
181 type 'a circbuf =
182 { store : 'a array
183 ; mutable rc : int
184 ; mutable wc : int
185 ; mutable len : int
189 let bound v minv maxv =
190 max minv (min maxv v);
193 let cbnew n v =
194 { store = Array.create n v
195 ; rc = 0
196 ; wc = 0
197 ; len = 0
201 let cbcap b = Array.length b.store;;
203 let cbput b v =
204 let cap = cbcap b in
205 b.store.(b.wc) <- v;
206 b.wc <- (b.wc + 1) mod cap;
207 b.rc <- b.wc;
208 b.len <- min (b.len + 1) cap;
211 let cbempty b = b.len = 0;;
213 let cbgetg b circular dir =
214 if cbempty b
215 then b.store.(0)
216 else
217 let rc = b.rc + dir in
218 let rc =
219 if circular
220 then (
221 if rc = -1
222 then b.len-1
223 else (
224 if rc >= b.len
225 then 0
226 else rc
229 else bound rc 0 (b.len-1)
231 b.rc <- rc;
232 b.store.(rc);
235 let cbget b = cbgetg b false;;
236 let cbgetc b = cbgetg b true;;
238 let drawstring size x y s =
239 Gl.enable `blend;
240 Gl.enable `texture_2d;
241 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
242 ignore (drawstr size x y s);
243 Gl.disable `blend;
244 Gl.disable `texture_2d;
247 let drawstring1 size x y s =
248 drawstr size x y s;
251 let drawstring2 size x y fmt =
252 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
255 type page =
256 { pageno : int
257 ; pagedimno : int
258 ; pagew : int
259 ; pageh : int
260 ; pagex : int
261 ; pagey : int
262 ; pagevw : int
263 ; pagevh : int
264 ; pagedispx : int
265 ; pagedispy : int
266 ; pagecol : int
270 let debugl l =
271 dolog "l %d dim=%d {" l.pageno l.pagedimno;
272 dolog " WxH %dx%d" l.pagew l.pageh;
273 dolog " vWxH %dx%d" l.pagevw l.pagevh;
274 dolog " pagex,y %d,%d" l.pagex l.pagey;
275 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
276 dolog " column %d" l.pagecol;
277 dolog "}";
280 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
281 dolog "rect {";
282 dolog " x0,y0=(% f, % f)" x0 y0;
283 dolog " x1,y1=(% f, % f)" x1 y1;
284 dolog " x2,y2=(% f, % f)" x2 y2;
285 dolog " x3,y3=(% f, % f)" x3 y3;
286 dolog "}";
289 type multicolumns = multicol * pagegeom
290 and singlecolumn = pagegeom
291 and splitcolumns = columncount * pagegeom
292 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
293 and multicol = columncount * covercount * covercount
294 and pdimno = int
295 and columncount = int
296 and covercount = int;;
298 type conf =
299 { mutable scrollbw : int
300 ; mutable scrollh : int
301 ; mutable icase : bool
302 ; mutable preload : bool
303 ; mutable pagebias : int
304 ; mutable verbose : bool
305 ; mutable debug : bool
306 ; mutable scrollstep : int
307 ; mutable hscrollstep : int
308 ; mutable maxhfit : bool
309 ; mutable crophack : bool
310 ; mutable autoscrollstep : int
311 ; mutable maxwait : float option
312 ; mutable hlinks : bool
313 ; mutable underinfo : bool
314 ; mutable interpagespace : interpagespace
315 ; mutable zoom : float
316 ; mutable presentation : bool
317 ; mutable angle : angle
318 ; mutable winw : int
319 ; mutable winh : int
320 ; mutable savebmarks : bool
321 ; mutable proportional : proportional
322 ; mutable trimmargins : trimmargins
323 ; mutable trimfuzz : irect
324 ; mutable memlimit : memsize
325 ; mutable texcount : texcount
326 ; mutable sliceheight : sliceheight
327 ; mutable thumbw : width
328 ; mutable jumpback : bool
329 ; mutable bgcolor : float * float * float
330 ; mutable bedefault : bool
331 ; mutable scrollbarinpm : bool
332 ; mutable tilew : int
333 ; mutable tileh : int
334 ; mutable mustoresize : memsize
335 ; mutable checkers : bool
336 ; mutable aalevel : int
337 ; mutable urilauncher : string
338 ; mutable pathlauncher : string
339 ; mutable colorspace : colorspace
340 ; mutable invert : bool
341 ; mutable colorscale : float
342 ; mutable redirectstderr : bool
343 ; mutable ghyllscroll : (int * int * int) option
344 ; mutable columns : columns
345 ; mutable beyecolumns : columncount option
346 ; mutable selcmd : string
347 ; mutable updatecurs : bool
348 ; mutable keyhashes : (string * keyhash) list
349 ; mutable hfsize : int
350 ; mutable pgscale : float
351 ; mutable usepbo : bool
352 ; mutable wheelbypage : bool
353 ; mutable stcmd : string
355 and columns =
356 | Csingle of singlecolumn
357 | Cmulti of multicolumns
358 | Csplit of splitcolumns
361 type anchor = pageno * top * dtop;;
363 type outline = string * int * anchor;;
365 type rect = float * float * float * float * float * float * float * float;;
367 type tile = opaque * pixmapsize * elapsed
368 and elapsed = float;;
369 type pagemapkey = pageno * gen;;
370 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
371 and row = int
372 and col = int;;
374 let emptyanchor = (0, 0.0, 0.0);;
376 type infochange = | Memused | Docinfo | Pdim;;
378 class type uioh = object
379 method display : unit
380 method key : int -> int -> uioh
381 method button : int -> bool -> int -> int -> int -> uioh
382 method motion : int -> int -> uioh
383 method pmotion : int -> int -> uioh
384 method infochanged : infochange -> unit
385 method scrollpw : (int * float * float)
386 method scrollph : (int * float * float)
387 method modehash : keyhash
388 end;;
390 type mode =
391 | Birdseye of (conf * leftx * pageno * pageno * anchor)
392 | Textentry of (textentry * onleave)
393 | View
394 | LinkNav of linktarget
395 and onleave = leavetextentrystatus -> unit
396 and leavetextentrystatus = | Cancel | Confirm
397 and helpitem = string * int * action
398 and action =
399 | Noaction
400 | Action of (uioh -> uioh)
401 and linktarget =
402 | Ltexact of (pageno * int)
403 | Ltgendir of int
406 let isbirdseye = function Birdseye _ -> true | _ -> false;;
407 let istextentry = function Textentry _ -> true | _ -> false;;
409 type currently =
410 | Idle
411 | Loading of (page * gen)
412 | Tiling of (
413 page * opaque * colorspace * angle * gen * col * row * width * height
415 | Outlining of outline list
418 let emptykeyhash = Hashtbl.create 0;;
419 let nouioh : uioh = object (self)
420 method display = ()
421 method key _ _ = self
422 method button _ _ _ _ _ = self
423 method motion _ _ = self
424 method pmotion _ _ = self
425 method infochanged _ = ()
426 method scrollpw = (0, nan, nan)
427 method scrollph = (0, nan, nan)
428 method modehash = emptykeyhash
429 end;;
431 type state =
432 { mutable sr : Unix.file_descr
433 ; mutable sw : Unix.file_descr
434 ; mutable wsfd : Unix.file_descr
435 ; mutable errfd : Unix.file_descr option
436 ; mutable stderr : Unix.file_descr
437 ; mutable errmsgs : Buffer.t
438 ; mutable newerrmsgs : bool
439 ; mutable w : int
440 ; mutable x : int
441 ; mutable y : int
442 ; mutable scrollw : int
443 ; mutable hscrollh : int
444 ; mutable anchor : anchor
445 ; mutable ranchors : (string * string * anchor) list
446 ; mutable maxy : int
447 ; mutable layout : page list
448 ; pagemap : (pagemapkey, opaque) Hashtbl.t
449 ; tilemap : (tilemapkey, tile) Hashtbl.t
450 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
451 ; mutable pdims : (pageno * width * height * leftx) list
452 ; mutable pagecount : int
453 ; mutable currently : currently
454 ; mutable mstate : mstate
455 ; mutable searchpattern : string
456 ; mutable rects : (pageno * recttype * rect) list
457 ; mutable rects1 : (pageno * recttype * rect) list
458 ; mutable text : string
459 ; mutable fullscreen : (width * height) option
460 ; mutable mode : mode
461 ; mutable uioh : uioh
462 ; mutable outlines : outline array
463 ; mutable bookmarks : outline list
464 ; mutable path : string
465 ; mutable password : string
466 ; mutable nameddest : string
467 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
468 ; mutable memused : memsize
469 ; mutable gen : gen
470 ; mutable throttle : (page list * int * float) option
471 ; mutable autoscroll : int option
472 ; mutable ghyll : (int option -> unit)
473 ; mutable help : helpitem array
474 ; mutable docinfo : (int * string) list
475 ; mutable texid : GlTex.texture_id option
476 ; hists : hists
477 ; mutable prevzoom : float
478 ; mutable progress : float
479 ; mutable redisplay : bool
480 ; mutable mpos : mpos
481 ; mutable keystate : keystate
482 ; mutable glinks : bool
483 ; mutable prevcolumns : (columns * float) option
484 ; mutable wthack : bool
486 and hists =
487 { pat : string circbuf
488 ; pag : string circbuf
489 ; nav : anchor circbuf
490 ; sel : string circbuf
494 let defconf =
495 { scrollbw = 7
496 ; scrollh = 12
497 ; icase = true
498 ; preload = true
499 ; pagebias = 0
500 ; verbose = false
501 ; debug = false
502 ; scrollstep = 24
503 ; hscrollstep = 24
504 ; maxhfit = true
505 ; crophack = false
506 ; autoscrollstep = 2
507 ; maxwait = None
508 ; hlinks = false
509 ; underinfo = false
510 ; interpagespace = 2
511 ; zoom = 1.0
512 ; presentation = false
513 ; angle = 0
514 ; winw = 900
515 ; winh = 900
516 ; savebmarks = true
517 ; proportional = true
518 ; trimmargins = false
519 ; trimfuzz = (0,0,0,0)
520 ; memlimit = 32 lsl 20
521 ; texcount = 256
522 ; sliceheight = 24
523 ; thumbw = 76
524 ; jumpback = true
525 ; bgcolor = (0.5, 0.5, 0.5)
526 ; bedefault = false
527 ; scrollbarinpm = true
528 ; tilew = 2048
529 ; tileh = 2048
530 ; mustoresize = 256 lsl 20
531 ; checkers = true
532 ; aalevel = 8
533 ; urilauncher =
534 (match platform with
535 | Plinux | Pfreebsd | Pdragonflybsd
536 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
537 | Posx -> "open \"%s\""
538 | Pcygwin -> "cygstart \"%s\""
539 | Punknown -> "echo %s")
540 ; pathlauncher = "lp \"%s\""
541 ; selcmd =
542 (match platform with
543 | Plinux | Pfreebsd | Pdragonflybsd
544 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
545 | Posx -> "pbcopy"
546 | Pcygwin -> "wsel"
547 | Punknown -> "cat")
548 ; colorspace = Rgb
549 ; invert = false
550 ; colorscale = 1.0
551 ; redirectstderr = false
552 ; ghyllscroll = None
553 ; columns = Csingle [||]
554 ; beyecolumns = None
555 ; updatecurs = false
556 ; hfsize = 12
557 ; pgscale = 1.0
558 ; usepbo = false
559 ; wheelbypage = false
560 ; stcmd = "echo SyncTex"
561 ; keyhashes =
562 let mk n = (n, Hashtbl.create 1) in
563 [ mk "global"
564 ; mk "info"
565 ; mk "help"
566 ; mk "outline"
567 ; mk "listview"
568 ; mk "birdseye"
569 ; mk "textentry"
570 ; mk "links"
571 ; mk "view"
576 let wtmode = ref false;;
578 let findkeyhash c name =
579 try List.assoc name c.keyhashes
580 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
583 let conf = { defconf with angle = defconf.angle };;
585 let pgscale h = truncate (float h *. conf.pgscale);;
587 type fontstate =
588 { mutable fontsize : int
589 ; mutable wwidth : float
590 ; mutable maxrows : int
594 let fstate =
595 { fontsize = 14
596 ; wwidth = nan
597 ; maxrows = -1
601 let setfontsize n =
602 fstate.fontsize <- n;
603 fstate.wwidth <- measurestr fstate.fontsize "w";
604 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
607 let geturl s =
608 let colonpos = try String.index s ':' with Not_found -> -1 in
609 let len = String.length s in
610 if colonpos >= 0 && colonpos + 3 < len
611 then (
612 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
613 then
614 let schemestartpos =
615 try String.rindex_from s colonpos ' '
616 with Not_found -> -1
618 let scheme =
619 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
621 match scheme with
622 | "http" | "ftp" | "mailto" ->
623 let epos =
624 try String.index_from s colonpos ' '
625 with Not_found -> len
627 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
628 | _ -> ""
629 else ""
631 else ""
634 let gotouri uri =
635 if String.length conf.urilauncher = 0
636 then print_endline uri
637 else (
638 let url = geturl uri in
639 if String.length url = 0
640 then print_endline uri
641 else
642 let re = Str.regexp "%s" in
643 let command = Str.global_replace re url conf.urilauncher in
644 try popen command []
645 with exn ->
646 Printf.eprintf
647 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
648 flush stderr;
652 let version () =
653 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
654 (platform_to_string platform) Sys.word_size Sys.ocaml_version
657 let makehelp () =
658 let strings = version () :: "" :: Help.keys in
659 Array.of_list (
660 List.map (fun s ->
661 let url = geturl s in
662 if String.length url > 0
663 then (s, 0, Action (fun u -> gotouri url; u))
664 else (s, 0, Noaction)
665 ) strings);
668 let noghyll _ = ();;
669 let firstgeomcmds = "", [];;
671 let state =
672 { sr = Unix.stdin
673 ; sw = Unix.stdin
674 ; wsfd = Unix.stdin
675 ; errfd = None
676 ; stderr = Unix.stderr
677 ; errmsgs = Buffer.create 0
678 ; newerrmsgs = false
679 ; x = 0
680 ; y = 0
681 ; w = 0
682 ; scrollw = 0
683 ; hscrollh = 0
684 ; anchor = emptyanchor
685 ; ranchors = []
686 ; layout = []
687 ; maxy = max_int
688 ; tilelru = Queue.create ()
689 ; pagemap = Hashtbl.create 10
690 ; tilemap = Hashtbl.create 10
691 ; pdims = []
692 ; pagecount = 0
693 ; currently = Idle
694 ; mstate = Mnone
695 ; rects = []
696 ; rects1 = []
697 ; text = ""
698 ; mode = View
699 ; fullscreen = None
700 ; searchpattern = ""
701 ; outlines = [||]
702 ; bookmarks = []
703 ; path = ""
704 ; password = ""
705 ; nameddest = ""
706 ; geomcmds = firstgeomcmds
707 ; hists =
708 { nav = cbnew 10 emptyanchor
709 ; pat = cbnew 10 ""
710 ; pag = cbnew 10 ""
711 ; sel = cbnew 10 ""
713 ; memused = 0
714 ; gen = 0
715 ; throttle = None
716 ; autoscroll = None
717 ; ghyll = noghyll
718 ; help = makehelp ()
719 ; docinfo = []
720 ; texid = None
721 ; prevzoom = 1.0
722 ; progress = -1.0
723 ; uioh = nouioh
724 ; redisplay = true
725 ; mpos = (-1, -1)
726 ; keystate = KSnone
727 ; glinks = false
728 ; prevcolumns = None
729 ; wthack = false
733 let vlog fmt =
734 if conf.verbose
735 then
736 Printf.kprintf prerr_endline fmt
737 else
738 Printf.kprintf ignore fmt
741 let launchpath () =
742 if String.length conf.pathlauncher = 0
743 then print_endline state.path
744 else (
745 let re = Str.regexp "%s" in
746 let command = Str.global_replace re state.path conf.pathlauncher in
747 try popen command []
748 with exn ->
749 Printf.eprintf
750 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
751 flush stderr;
755 module Ne = struct
756 type 'a t = | Res of 'a | Exn of exn;;
758 let pipe () =
759 try Res (Unix.pipe ())
760 with exn -> Exn exn
763 let clo fd f =
764 try tempfailureretry Unix.close fd
765 with exn -> f (Printexc.to_string exn)
768 let dup fd =
769 try Res (tempfailureretry Unix.dup fd)
770 with exn -> Exn exn
773 let dup2 fd1 fd2 =
774 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
775 with exn -> Exn exn
777 end;;
779 let redirectstderr () =
780 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
781 if conf.redirectstderr
782 then
783 match Ne.pipe () with
784 | Ne.Exn exn ->
785 dolog "failed to create stderr redirection pipes: %s"
786 (Printexc.to_string exn)
788 | Ne.Res (r, w) ->
789 begin match Ne.dup Unix.stderr with
790 | Ne.Exn exn ->
791 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
792 Ne.clo r (clofail "pipe/r");
793 Ne.clo w (clofail "pipe/w");
795 | Ne.Res dupstderr ->
796 begin match Ne.dup2 w Unix.stderr with
797 | Ne.Exn exn ->
798 dolog "failed to dup2 to stderr: %s"
799 (Printexc.to_string exn);
800 Ne.clo dupstderr (clofail "stderr duplicate");
801 Ne.clo r (clofail "redir pipe/r");
802 Ne.clo w (clofail "redir pipe/w");
804 | Ne.Res () ->
805 state.stderr <- dupstderr;
806 state.errfd <- Some r;
807 end;
809 else (
810 state.newerrmsgs <- false;
811 begin match state.errfd with
812 | Some fd ->
813 begin match Ne.dup2 state.stderr Unix.stderr with
814 | Ne.Exn exn ->
815 dolog "failed to dup2 original stderr: %s"
816 (Printexc.to_string exn)
817 | Ne.Res () ->
818 Ne.clo fd (clofail "dup of stderr");
819 state.errfd <- None;
820 end;
821 | None -> ()
822 end;
823 prerr_string (Buffer.contents state.errmsgs);
824 flush stderr;
825 Buffer.clear state.errmsgs;
829 module G =
830 struct
831 let postRedisplay who =
832 if conf.verbose
833 then prerr_endline ("redisplay for " ^ who);
834 state.redisplay <- true;
836 end;;
838 let getopaque pageno =
839 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
840 with Not_found -> None
843 let putopaque pageno opaque =
844 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
847 let pagetranslatepoint l x y =
848 let dy = y - l.pagedispy in
849 let y = dy + l.pagey in
850 let dx = x - l.pagedispx in
851 let x = dx + l.pagex in
852 (x, y);
855 let onppundermouse g x y d =
856 let rec f = function
857 | l :: rest ->
858 begin match getopaque l.pageno with
859 | Some opaque ->
860 let x0 = l.pagedispx in
861 let x1 = x0 + l.pagevw in
862 let y0 = l.pagedispy in
863 let y1 = y0 + l.pagevh in
864 if y >= y0 && y <= y1 && x >= x0 && x <= x1
865 then
866 let px, py = pagetranslatepoint l x y in
867 match g opaque l px py with
868 | Some res -> res
869 | None -> f rest
870 else f rest
871 | _ ->
872 f rest
874 | [] -> d
876 f state.layout
879 let getunder x y =
880 let g opaque _ px py =
881 match whatsunder opaque px py with
882 | Unone -> None
883 | under -> Some under
885 onppundermouse g x y Unone
888 let unproject x y =
889 let g opaque l x y =
890 match unproject opaque x y with
891 | Some (x, y) -> Some (Some (l.pageno, x, y))
892 | None -> None
894 onppundermouse g x y None;
897 let showtext c s =
898 state.text <- Printf.sprintf "%c%s" c s;
899 G.postRedisplay "showtext";
902 let undertext = function
903 | Unone -> "none"
904 | Ulinkuri s -> s
905 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
906 | Utext s -> "font: " ^ s
907 | Uunexpected s -> "unexpected: " ^ s
908 | Ulaunch s -> "launch: " ^ s
909 | Unamed s -> "named: " ^ s
910 | Uremote (filename, pageno) ->
911 Printf.sprintf "%s: page %d" filename (pageno+1)
914 let updateunder x y =
915 match getunder x y with
916 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
917 | Ulinkuri uri ->
918 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
919 Wsi.setcursor Wsi.CURSOR_INFO
920 | Ulinkgoto (pageno, _) ->
921 if conf.underinfo
922 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
923 Wsi.setcursor Wsi.CURSOR_INFO
924 | Utext s ->
925 if conf.underinfo then showtext 'f' ("ont: " ^ s);
926 Wsi.setcursor Wsi.CURSOR_TEXT
927 | Uunexpected s ->
928 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
929 Wsi.setcursor Wsi.CURSOR_INHERIT
930 | Ulaunch s ->
931 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
932 Wsi.setcursor Wsi.CURSOR_INHERIT
933 | Unamed s ->
934 if conf.underinfo then showtext 'n' ("amed: " ^ s);
935 Wsi.setcursor Wsi.CURSOR_INHERIT
936 | Uremote (filename, pageno) ->
937 if conf.underinfo then showtext 'r'
938 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
939 Wsi.setcursor Wsi.CURSOR_INFO
942 let showlinktype under =
943 if conf.underinfo
944 then
945 match under with
946 | Unone -> ()
947 | under ->
948 let s = undertext under in
949 showtext ' ' s
952 let addchar s c =
953 let b = Buffer.create (String.length s + 1) in
954 Buffer.add_string b s;
955 Buffer.add_char b c;
956 Buffer.contents b;
959 let colorspace_of_string s =
960 match String.lowercase s with
961 | "rgb" -> Rgb
962 | "bgr" -> Bgr
963 | "gray" -> Gray
964 | _ -> failwith "invalid colorspace"
967 let int_of_colorspace = function
968 | Rgb -> 0
969 | Bgr -> 1
970 | Gray -> 2
973 let colorspace_of_int = function
974 | 0 -> Rgb
975 | 1 -> Bgr
976 | 2 -> Gray
977 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
980 let colorspace_to_string = function
981 | Rgb -> "rgb"
982 | Bgr -> "bgr"
983 | Gray -> "gray"
986 let intentry_with_suffix text key =
987 let c =
988 if key >= 32 && key < 127
989 then Char.chr key
990 else '\000'
992 match Char.lowercase c with
993 | '0' .. '9' ->
994 let text = addchar text c in
995 TEcont text
997 | 'k' | 'm' | 'g' ->
998 let text = addchar text c in
999 TEcont text
1001 | _ ->
1002 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1003 TEcont text
1006 let multicolumns_to_string (n, a, b) =
1007 if a = 0 && b = 0
1008 then Printf.sprintf "%d" n
1009 else Printf.sprintf "%d,%d,%d" n a b;
1012 let multicolumns_of_string s =
1014 (int_of_string s, 0, 0)
1015 with _ ->
1016 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1017 if a > 1 || b > 1
1018 then failwith "subtly broken"; (n, a, b)
1022 let readcmd fd =
1023 let s = "xxxx" in
1024 let n = tempfailureretry (Unix.read fd s 0) 4 in
1025 if n != 4 then failwith "incomplete read(len)";
1026 let len = 0
1027 lor (Char.code s.[0] lsl 24)
1028 lor (Char.code s.[1] lsl 16)
1029 lor (Char.code s.[2] lsl 8)
1030 lor (Char.code s.[3] lsl 0)
1032 let s = String.create len in
1033 let n = tempfailureretry (Unix.read fd s 0) len in
1034 if n != len then failwith "incomplete read(data)";
1038 let btod b = if b then 1 else 0;;
1040 let wcmd fmt =
1041 let b = Buffer.create 16 in
1042 Buffer.add_string b "llll";
1043 Printf.kbprintf
1044 (fun b ->
1045 let s = Buffer.contents b in
1046 let n = String.length s in
1047 let len = n - 4 in
1048 (* dolog "wcmd %S" (String.sub s 4 len); *)
1049 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1050 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1051 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1052 s.[3] <- Char.chr (len land 0xff);
1053 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1054 if n' != n then failwith "write failed";
1055 ) b fmt;
1058 let calcips h =
1059 let d = conf.winh - h in
1060 max conf.interpagespace ((d + 1) / 2)
1063 let rowyh (c, coverA, coverB) b n =
1064 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1065 then
1066 let _, _, vy, (_, _, h, _) = b.(n) in
1067 (vy, h)
1068 else
1069 let n' = n - coverA in
1070 let d = n' mod c in
1071 let s = n - d in
1072 let e = min state.pagecount (s + c) in
1073 let rec find m miny maxh = if m = e then miny, maxh else
1074 let _, _, y, (_, _, h, _) = b.(m) in
1075 let miny = min miny y in
1076 let maxh = max maxh h in
1077 find (m+1) miny maxh
1078 in find s max_int 0
1081 let calcheight () =
1082 match conf.columns with
1083 | Cmulti ((_, _, _) as cl, b) ->
1084 if Array.length b > 0
1085 then
1086 let y, h = rowyh cl b (Array.length b - 1) in
1087 y + h + (if conf.presentation then calcips h else 0)
1088 else 0
1089 | Csingle b ->
1090 if Array.length b > 0
1091 then
1092 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1093 y + h + (if conf.presentation then calcips h else 0)
1094 else 0
1095 | Csplit (_, b) ->
1096 if Array.length b > 0
1097 then
1098 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1099 y + h
1100 else 0
1103 let getpageyh pageno =
1104 let pageno = bound pageno 0 (state.pagecount-1) in
1105 match conf.columns with
1106 | Csingle b ->
1107 if Array.length b = 0
1108 then 0, 0
1109 else
1110 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1111 let y =
1112 if conf.presentation
1113 then y - calcips h
1114 else y
1116 y, h
1117 | Cmulti (cl, b) ->
1118 if Array.length b = 0
1119 then 0, 0
1120 else
1121 let y, h = rowyh cl b pageno in
1122 let y =
1123 if conf.presentation
1124 then y - calcips h
1125 else y
1127 y, h
1128 | Csplit (c, b) ->
1129 if Array.length b = 0
1130 then 0, 0
1131 else
1132 let n = pageno*c in
1133 let (_, _, y, (_, _, h, _)) = b.(n) in
1134 y, h
1137 let getpagedim pageno =
1138 let rec f ppdim l =
1139 match l with
1140 | (n, _, _, _) as pdim :: rest ->
1141 if n >= pageno
1142 then (if n = pageno then pdim else ppdim)
1143 else f pdim rest
1145 | [] -> ppdim
1147 f (-1, -1, -1, -1) state.pdims
1150 let getpagey pageno = fst (getpageyh pageno);;
1152 let nogeomcmds cmds =
1153 match cmds with
1154 | s, [] -> String.length s = 0
1155 | _ -> false
1158 let page_of_y y =
1159 let ((c, coverA, coverB) as cl), b =
1160 match conf.columns with
1161 | Csingle b -> (1, 0, 0), b
1162 | Cmulti (c, b) -> c, b
1163 | Csplit (_, b) -> (1, 0, 0), b
1165 if Array.length b = 0
1166 then -1
1167 else
1168 let rec bsearch nmin nmax =
1169 if nmin > nmax
1170 then bound nmin 0 (state.pagecount-1)
1171 else
1172 let n = (nmax + nmin) / 2 in
1173 let vy, h = rowyh cl b n in
1174 let y0, y1 =
1175 if conf.presentation
1176 then
1177 let ips = calcips h in
1178 let y0 = vy - ips in
1179 let y1 = vy + h + ips in
1180 y0, y1
1181 else (
1182 if n = 0
1183 then 0, vy + h + conf.interpagespace
1184 else
1185 let y0 = vy - conf.interpagespace in
1186 y0, y0 + h + conf.interpagespace
1189 if y >= y0 && y < y1
1190 then (
1191 if c = 1
1192 then n
1193 else (
1194 if n > coverA
1195 then
1196 if n < state.pagecount - coverB
1197 then ((n-coverA)/c)*c + coverA
1198 else n
1199 else n
1202 else (
1203 if y > y0
1204 then bsearch (n+1) nmax
1205 else bsearch nmin (n-1)
1208 let r = bsearch 0 (state.pagecount-1) in
1212 let layoutN ((columns, coverA, coverB), b) y sh =
1213 let sh = sh - state.hscrollh in
1214 let rec fold accu n =
1215 if n = Array.length b
1216 then accu
1217 else
1218 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1219 if (vy - y) > sh &&
1220 (n = coverA - 1
1221 || n = state.pagecount - coverB
1222 || (n - coverA) mod columns = columns - 1)
1223 then accu
1224 else
1225 let accu =
1226 if vy + h > y
1227 then
1228 let pagey = max 0 (y - vy) in
1229 let pagedispy = if pagey > 0 then 0 else vy - y in
1230 let pagedispx, pagex =
1231 let pdx =
1232 if n = coverA - 1 || n = state.pagecount - coverB
1233 then state.x + (conf.winw - state.scrollw - w) / 2
1234 else dx + xoff + state.x
1236 if pdx < 0
1237 then 0, -pdx
1238 else pdx, 0
1240 let pagevw =
1241 let vw = conf.winw - state.scrollw - pagedispx in
1242 let pw = w - pagex in
1243 min vw pw
1245 let pagevh = min (h - pagey) (sh - pagedispy) in
1246 if pagevw > 0 && pagevh > 0
1247 then
1248 let e =
1249 { pageno = n
1250 ; pagedimno = pdimno
1251 ; pagew = w
1252 ; pageh = h
1253 ; pagex = pagex
1254 ; pagey = pagey
1255 ; pagevw = pagevw
1256 ; pagevh = pagevh
1257 ; pagedispx = pagedispx
1258 ; pagedispy = pagedispy
1259 ; pagecol = 0
1262 e :: accu
1263 else
1264 accu
1265 else
1266 accu
1268 fold accu (n+1)
1270 List.rev (fold [] (page_of_y y));
1273 let layoutS (columns, b) y sh =
1274 let sh = sh - state.hscrollh in
1275 let rec fold accu n =
1276 if n = Array.length b
1277 then accu
1278 else
1279 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1280 if (vy - y) > sh
1281 then accu
1282 else
1283 let accu =
1284 if vy + pageh > y
1285 then
1286 let x = xoff + state.x in
1287 let pagey = max 0 (y - vy) in
1288 let pagedispy = if pagey > 0 then 0 else vy - y in
1289 let pagedispx, pagex =
1290 if px = 0
1291 then (
1292 if x < 0
1293 then 0, -x
1294 else x, 0
1296 else (
1297 let px = px - x in
1298 if px < 0
1299 then -px, 0
1300 else 0, px
1303 let pagecolw = pagew/columns in
1304 let pagedispx =
1305 if pagecolw < conf.winw
1306 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1307 else pagedispx
1309 let pagevw =
1310 let vw = conf.winw - pagedispx - state.scrollw in
1311 let pw = pagew - pagex in
1312 min vw pw
1314 let pagevw = min pagevw pagecolw in
1315 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1316 if pagevw > 0 && pagevh > 0
1317 then
1318 let e =
1319 { pageno = n/columns
1320 ; pagedimno = pdimno
1321 ; pagew = pagew
1322 ; pageh = pageh
1323 ; pagex = pagex
1324 ; pagey = pagey
1325 ; pagevw = pagevw
1326 ; pagevh = pagevh
1327 ; pagedispx = pagedispx
1328 ; pagedispy = pagedispy
1329 ; pagecol = n mod columns
1332 e :: accu
1333 else
1334 accu
1335 else
1336 accu
1338 fold accu (n+1)
1340 List.rev (fold [] 0)
1343 let layout y sh =
1344 if nogeomcmds state.geomcmds
1345 then
1346 match conf.columns with
1347 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1348 | Cmulti c -> layoutN c y sh
1349 | Csplit s -> layoutS s y sh
1350 else []
1353 let clamp incr =
1354 let y = state.y + incr in
1355 let y = max 0 y in
1356 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1360 let itertiles l f =
1361 let tilex = l.pagex mod conf.tilew in
1362 let tiley = l.pagey mod conf.tileh in
1364 let col = l.pagex / conf.tilew in
1365 let row = l.pagey / conf.tileh in
1367 let rec rowloop row y0 dispy h =
1368 if h = 0
1369 then ()
1370 else (
1371 let dh = conf.tileh - y0 in
1372 let dh = min h dh in
1373 let rec colloop col x0 dispx w =
1374 if w = 0
1375 then ()
1376 else (
1377 let dw = conf.tilew - x0 in
1378 let dw = min w dw in
1380 f col row dispx dispy x0 y0 dw dh;
1381 colloop (col+1) 0 (dispx+dw) (w-dw)
1384 colloop col tilex l.pagedispx l.pagevw;
1385 rowloop (row+1) 0 (dispy+dh) (h-dh)
1388 if l.pagevw > 0 && l.pagevh > 0
1389 then rowloop row tiley l.pagedispy l.pagevh;
1392 let gettileopaque l col row =
1393 let key =
1394 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1396 try Some (Hashtbl.find state.tilemap key)
1397 with Not_found -> None
1400 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1401 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1402 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1405 let drawtiles l color =
1406 GlDraw.color color;
1407 let f col row x y tilex tiley w h =
1408 match gettileopaque l col row with
1409 | Some (opaque, _, t) ->
1410 let params = x, y, w, h, tilex, tiley in
1411 if conf.invert
1412 then (
1413 Gl.enable `blend;
1414 GlFunc.blend_func `zero `one_minus_src_color;
1416 drawtile params opaque;
1417 if conf.invert
1418 then Gl.disable `blend;
1419 if conf.debug
1420 then (
1421 let s = Printf.sprintf
1422 "%d[%d,%d] %f sec"
1423 l.pageno col row t
1425 let w = measurestr fstate.fontsize s in
1426 GlMisc.push_attrib [`current];
1427 GlDraw.color (0.0, 0.0, 0.0);
1428 GlDraw.rect
1429 (float (x-2), float (y-2))
1430 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1431 GlDraw.color (1.0, 1.0, 1.0);
1432 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1433 GlMisc.pop_attrib ();
1436 | _ ->
1437 let w =
1438 let lw = conf.winw - state.scrollw - x in
1439 min lw w
1440 and h =
1441 let lh = conf.winh - y in
1442 min lh h
1444 begin match state.texid with
1445 | Some id ->
1446 Gl.enable `texture_2d;
1447 GlTex.bind_texture `texture_2d id;
1448 let x0 = float x
1449 and y0 = float y
1450 and x1 = float (x+w)
1451 and y1 = float (y+h) in
1453 let tw = float w /. 16.0
1454 and th = float h /. 16.0 in
1455 let tx0 = float tilex /. 16.0
1456 and ty0 = float tiley /. 16.0 in
1457 let tx1 = tx0 +. tw
1458 and ty1 = ty0 +. th in
1459 GlDraw.begins `quads;
1460 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1461 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1462 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1463 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1464 GlDraw.ends ();
1466 Gl.disable `texture_2d;
1467 | None ->
1468 GlDraw.color (1.0, 1.0, 1.0);
1469 GlDraw.rect
1470 (float x, float y)
1471 (float (x+w), float (y+h));
1472 end;
1473 if w > 128 && h > fstate.fontsize + 10
1474 then (
1475 GlDraw.color (0.0, 0.0, 0.0);
1476 let c, r =
1477 if conf.verbose
1478 then (col*conf.tilew, row*conf.tileh)
1479 else col, row
1481 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1483 GlDraw.color color;
1485 itertiles l f
1488 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1490 let tilevisible1 l x y =
1491 let ax0 = l.pagex
1492 and ax1 = l.pagex + l.pagevw
1493 and ay0 = l.pagey
1494 and ay1 = l.pagey + l.pagevh in
1496 let bx0 = x
1497 and by0 = y in
1498 let bx1 = min (bx0 + conf.tilew) l.pagew
1499 and by1 = min (by0 + conf.tileh) l.pageh in
1501 let rx0 = max ax0 bx0
1502 and ry0 = max ay0 by0
1503 and rx1 = min ax1 bx1
1504 and ry1 = min ay1 by1 in
1506 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1507 nonemptyintersection
1510 let tilevisible layout n x y =
1511 let rec findpageinlayout m = function
1512 | l :: rest when l.pageno = n ->
1513 tilevisible1 l x y || (
1514 match conf.columns with
1515 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1516 | _ -> false
1518 | _ :: rest -> findpageinlayout 0 rest
1519 | [] -> false
1521 findpageinlayout 0 layout;
1524 let tileready l x y =
1525 tilevisible1 l x y &&
1526 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1529 let tilepage n p layout =
1530 let rec loop = function
1531 | l :: rest ->
1532 if l.pageno = n
1533 then
1534 let f col row _ _ _ _ _ _ =
1535 if state.currently = Idle
1536 then
1537 match gettileopaque l col row with
1538 | Some _ -> ()
1539 | None ->
1540 let x = col*conf.tilew
1541 and y = row*conf.tileh in
1542 let w =
1543 let w = l.pagew - x in
1544 min w conf.tilew
1546 let h =
1547 let h = l.pageh - y in
1548 min h conf.tileh
1550 let pbo =
1551 if conf.usepbo
1552 then getpbo w h conf.colorspace
1553 else "0"
1555 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1556 state.currently <-
1557 Tiling (
1558 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1559 conf.tilew, conf.tileh
1562 itertiles l f;
1563 else
1564 loop rest
1566 | [] -> ()
1568 if nogeomcmds state.geomcmds
1569 then loop layout;
1572 let preloadlayout y =
1573 let y = if y < conf.winh then 0 else y - conf.winh in
1574 let h = conf.winh*3 in
1575 layout y h;
1578 let load pages =
1579 let rec loop pages =
1580 if state.currently != Idle
1581 then ()
1582 else
1583 match pages with
1584 | l :: rest ->
1585 begin match getopaque l.pageno with
1586 | None ->
1587 wcmd "page %d %d" l.pageno l.pagedimno;
1588 state.currently <- Loading (l, state.gen);
1589 | Some opaque ->
1590 tilepage l.pageno opaque pages;
1591 loop rest
1592 end;
1593 | _ -> ()
1595 if nogeomcmds state.geomcmds
1596 then loop pages
1599 let preload pages =
1600 load pages;
1601 if conf.preload && state.currently = Idle
1602 then load (preloadlayout state.y);
1605 let layoutready layout =
1606 let rec fold all ls =
1607 all && match ls with
1608 | l :: rest ->
1609 let seen = ref false in
1610 let allvisible = ref true in
1611 let foo col row _ _ _ _ _ _ =
1612 seen := true;
1613 allvisible := !allvisible &&
1614 begin match gettileopaque l col row with
1615 | Some _ -> true
1616 | None -> false
1619 itertiles l foo;
1620 fold (!seen && !allvisible) rest
1621 | [] -> true
1623 let alltilesvisible = fold true layout in
1624 alltilesvisible;
1627 let gotoy y =
1628 state.wthack <- false;
1629 let y = bound y 0 state.maxy in
1630 let y, layout, proceed =
1631 match conf.maxwait with
1632 | Some time when state.ghyll == noghyll ->
1633 begin match state.throttle with
1634 | None ->
1635 let layout = layout y conf.winh in
1636 let ready = layoutready layout in
1637 if not ready
1638 then (
1639 load layout;
1640 state.throttle <- Some (layout, y, now ());
1642 else G.postRedisplay "gotoy showall (None)";
1643 y, layout, ready
1644 | Some (_, _, started) ->
1645 let dt = now () -. started in
1646 if dt > time
1647 then (
1648 state.throttle <- None;
1649 let layout = layout y conf.winh in
1650 load layout;
1651 G.postRedisplay "maxwait";
1652 y, layout, true
1654 else -1, [], false
1657 | _ ->
1658 let layout = layout y conf.winh in
1659 G.postRedisplay "gotoy ready";
1660 y, layout, true
1662 if proceed
1663 then (
1664 state.y <- y;
1665 state.layout <- layout;
1666 begin match state.mode with
1667 | LinkNav (Ltexact (pageno, linkno)) ->
1668 let rec loop = function
1669 | [] ->
1670 state.mode <- LinkNav (Ltgendir 0)
1671 | l :: _ when l.pageno = pageno ->
1672 begin match getopaque pageno with
1673 | None ->
1674 state.mode <- LinkNav (Ltgendir 0)
1675 | Some opaque ->
1676 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1677 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1678 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1679 then state.mode <- LinkNav (Ltgendir 0)
1681 | _ :: rest -> loop rest
1683 loop layout
1684 | _ -> ()
1685 end;
1686 begin match state.mode with
1687 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1688 if not (pagevisible layout pageno)
1689 then (
1690 match state.layout with
1691 | [] -> ()
1692 | l :: _ ->
1693 state.mode <- Birdseye (
1694 conf, leftx, l.pageno, hooverpageno, anchor
1697 | LinkNav (Ltgendir dir as lt) ->
1698 let linknav =
1699 let rec loop = function
1700 | [] -> lt
1701 | l :: rest ->
1702 match getopaque l.pageno with
1703 | None -> loop rest
1704 | Some opaque ->
1705 let link =
1706 let ld =
1707 if dir = 0
1708 then LDfirstvisible (l.pagex, l.pagey, dir)
1709 else (
1710 if dir > 0 then LDfirst else LDlast
1713 findlink opaque ld
1715 match link with
1716 | Lnotfound -> loop rest
1717 | Lfound n ->
1718 showlinktype (getlink opaque n);
1719 Ltexact (l.pageno, n)
1721 loop state.layout
1723 state.mode <- LinkNav linknav
1724 | _ -> ()
1725 end;
1726 preload layout;
1728 state.ghyll <- noghyll;
1729 if conf.updatecurs
1730 then (
1731 let mx, my = state.mpos in
1732 updateunder mx my;
1736 let conttiling pageno opaque =
1737 tilepage pageno opaque
1738 (if conf.preload then preloadlayout state.y else state.layout)
1741 let gotoy_and_clear_text y =
1742 if not conf.verbose then state.text <- "";
1743 gotoy y;
1746 let getanchor1 l =
1747 let top =
1748 let coloff = l.pagecol * l.pageh in
1749 float (l.pagey + coloff) /. float l.pageh
1751 let dtop =
1752 if l.pagedispy = 0
1753 then
1755 else
1756 if conf.presentation
1757 then float l.pagedispy /. float (calcips l.pageh)
1758 else float l.pagedispy /. float conf.interpagespace
1760 (l.pageno, top, dtop)
1763 let getanchor () =
1764 match state.layout with
1765 | l :: _ -> getanchor1 l
1766 | [] ->
1767 let n = page_of_y state.y in
1768 if n = -1
1769 then state.anchor
1770 else
1771 let y, h = getpageyh n in
1772 let dy = y - state.y in
1773 let dtop =
1774 if conf.presentation
1775 then
1776 let ips = calcips h in
1777 float (dy + ips) /. float ips
1778 else
1779 float dy /. float conf.interpagespace
1781 (n, 0.0, dtop)
1784 let getanchory (n, top, dtop) =
1785 let y, h = getpageyh n in
1786 if conf.presentation
1787 then
1788 let ips = calcips h in
1789 y + truncate (top*.float h -. dtop*.float ips) + ips;
1790 else
1791 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1794 let gotoanchor anchor =
1795 gotoy (getanchory anchor);
1798 let addnav () =
1799 cbput state.hists.nav (getanchor ());
1802 let getnav dir =
1803 let anchor = cbgetc state.hists.nav dir in
1804 getanchory anchor;
1807 let gotoghyll y =
1808 let scroll f n a b =
1809 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1810 let snake f a b =
1811 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1812 if f < a
1813 then s (float f /. float a)
1814 else (
1815 if f > b
1816 then 1.0 -. s ((float (f-b) /. float (n-b)))
1817 else 1.0
1820 snake f a b
1821 and summa f n a b =
1822 (* courtesy:
1823 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1824 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1825 let iv1 = iv f in
1826 let ins = float a *. iv1
1827 and outs = float (n-b) *. iv1 in
1828 let ones = b - a in
1829 ins +. outs +. float ones
1831 let rec set (_N, _A, _B) y sy =
1832 let sum = summa 1.0 _N _A _B in
1833 let dy = float (y - sy) in
1834 state.ghyll <- (
1835 let rec gf n y1 o =
1836 if n >= _N
1837 then state.ghyll <- noghyll
1838 else
1839 let go n =
1840 let s = scroll n _N _A _B in
1841 let y1 = y1 +. ((s *. dy) /. sum) in
1842 gotoy_and_clear_text (truncate y1);
1843 state.ghyll <- gf (n+1) y1;
1845 match o with
1846 | None -> go n
1847 | Some y' -> set (_N/2, 1, 1) y' state.y
1849 gf 0 (float state.y)
1852 match conf.ghyllscroll with
1853 | None ->
1854 gotoy_and_clear_text y
1855 | Some nab ->
1856 if state.ghyll == noghyll
1857 then set nab y state.y
1858 else state.ghyll (Some y)
1861 let gotopage n top =
1862 let y, h = getpageyh n in
1863 let y = y + (truncate (top *. float h)) in
1864 gotoghyll y
1867 let gotopage1 n top =
1868 let y = getpagey n in
1869 let y = y + top in
1870 gotoghyll y
1873 let invalidate s f =
1874 state.layout <- [];
1875 state.pdims <- [];
1876 state.rects <- [];
1877 state.rects1 <- [];
1878 match state.geomcmds with
1879 | ps, [] when String.length ps = 0 ->
1880 f ();
1881 state.geomcmds <- s, [];
1883 | ps, [] ->
1884 state.geomcmds <- ps, [s, f];
1886 | ps, (s', _) :: rest when s' = s ->
1887 state.geomcmds <- ps, ((s, f) :: rest);
1889 | ps, cmds ->
1890 state.geomcmds <- ps, ((s, f) :: cmds);
1893 let flushpages () =
1894 Hashtbl.iter (fun _ opaque ->
1895 wcmd "freepage %s" opaque;
1896 ) state.pagemap;
1897 Hashtbl.clear state.pagemap;
1900 let opendoc path password =
1901 state.path <- path;
1902 state.password <- password;
1903 state.gen <- state.gen + 1;
1904 state.docinfo <- [];
1906 flushpages ();
1907 setaalevel conf.aalevel;
1908 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1909 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1910 invalidate "reqlayout"
1911 (fun () ->
1912 wcmd "reqlayout %d %d %s\000"
1913 conf.angle (btod conf.proportional) state.nameddest;
1917 let reload () =
1918 state.anchor <- getanchor ();
1919 state.wthack <- !wtmode;
1920 opendoc state.path state.password;
1923 let scalecolor c =
1924 let c = c *. conf.colorscale in
1925 (c, c, c);
1928 let scalecolor2 (r, g, b) =
1929 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1932 let docolumns = function
1933 | Csingle _ ->
1934 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1935 let rec loop pageno pdimno pdim y ph pdims =
1936 if pageno = state.pagecount
1937 then ()
1938 else
1939 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1940 match pdims with
1941 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1942 pdimno+1, pdim, rest
1943 | _ ->
1944 pdimno, pdim, pdims
1946 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1947 let y = y +
1948 (if conf.presentation
1949 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1950 else (if pageno = 0 then 0 else conf.interpagespace)
1953 a.(pageno) <- (pdimno, x, y, pdim);
1954 loop (pageno+1) pdimno pdim (y + h) h pdims
1956 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1957 conf.columns <- Csingle a;
1959 | Cmulti ((columns, coverA, coverB), _) ->
1960 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1961 let rec loop pageno pdimno pdim x y rowh pdims =
1962 let rec fixrow m = if m = pageno then () else
1963 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1964 if h < rowh
1965 then (
1966 let y = y + (rowh - h) / 2 in
1967 a.(m) <- (pdimno, x, y, pdim);
1969 fixrow (m+1)
1971 if pageno = state.pagecount
1972 then fixrow (((pageno - 1) / columns) * columns)
1973 else
1974 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1975 match pdims with
1976 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1977 pdimno+1, pdim, rest
1978 | _ ->
1979 pdimno, pdim, pdims
1981 let x, y, rowh' =
1982 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1983 then (
1984 let x = (conf.winw - state.scrollw - w) / 2 in
1985 let ips =
1986 if conf.presentation then calcips h else conf.interpagespace in
1987 x, y + ips + rowh, h
1989 else (
1990 if (pageno - coverA) mod columns = 0
1991 then (
1992 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1993 let y =
1994 if conf.presentation
1995 then
1996 let ips = calcips h in
1997 y + (if pageno = 0 then 0 else calcips rowh + ips)
1998 else
1999 y + (if pageno = 0 then 0 else conf.interpagespace)
2001 x, y + rowh, h
2003 else x, y, max rowh h
2006 let y =
2007 if pageno > 1 && (pageno - coverA) mod columns = 0
2008 then (
2009 let y =
2010 if pageno = columns && conf.presentation
2011 then (
2012 let ips = calcips rowh in
2013 for i = 0 to pred columns
2015 let (pdimno, x, y, pdim) = a.(i) in
2016 a.(i) <- (pdimno, x, y+ips, pdim)
2017 done;
2018 y+ips;
2020 else y
2022 fixrow (pageno - columns);
2025 else y
2027 a.(pageno) <- (pdimno, x, y, pdim);
2028 let x = x + w + xoff*2 + conf.interpagespace in
2029 loop (pageno+1) pdimno pdim x y rowh' pdims
2031 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2032 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2034 | Csplit (c, _) ->
2035 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2036 let rec loop pageno pdimno pdim y pdims =
2037 if pageno = state.pagecount
2038 then ()
2039 else
2040 let pdimno, ((_, w, h, _) as pdim), pdims =
2041 match pdims with
2042 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2043 pdimno+1, pdim, rest
2044 | _ ->
2045 pdimno, pdim, pdims
2047 let cw = w / c in
2048 let rec loop1 n x y =
2049 if n = c then y else (
2050 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2051 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2054 let y = loop1 0 0 y in
2055 loop (pageno+1) pdimno pdim y pdims
2057 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2058 conf.columns <- Csplit (c, a);
2061 let represent () =
2062 docolumns conf.columns;
2063 state.maxy <- calcheight ();
2064 state.hscrollh <-
2065 if state.w <= conf.winw - state.scrollw
2066 then 0
2067 else state.scrollw
2069 let wthack = state.wthack in
2070 begin match state.mode with
2071 | Birdseye (_, _, pageno, _, _) ->
2072 let y, h = getpageyh pageno in
2073 let top = (conf.winh - h) / 2 in
2074 gotoy (max 0 (y - top))
2075 | _ -> gotoanchor state.anchor
2076 end;
2077 state.wthack <- wthack;
2080 let reshape w h =
2081 state.wthack <- false;
2082 GlDraw.viewport 0 0 w h;
2083 let firsttime = state.geomcmds == firstgeomcmds in
2084 if not firsttime && nogeomcmds state.geomcmds
2085 then state.anchor <- getanchor ();
2087 conf.winw <- w;
2088 let w = truncate (float w *. conf.zoom) - state.scrollw in
2089 let w = max w 2 in
2090 conf.winh <- h;
2091 setfontsize fstate.fontsize;
2092 GlMat.mode `modelview;
2093 GlMat.load_identity ();
2095 GlMat.mode `projection;
2096 GlMat.load_identity ();
2097 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2098 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2099 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2101 let relx =
2102 if conf.zoom <= 1.0
2103 then 0.0
2104 else float state.x /. float state.w
2106 invalidate "geometry"
2107 (fun () ->
2108 state.w <- w;
2109 if not firsttime
2110 then state.x <- truncate (relx *. float w);
2111 let w =
2112 match conf.columns with
2113 | Csingle _ -> w
2114 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2115 | Csplit (c, _) -> w * c
2117 wcmd "geometry %d %d" w h);
2120 let enttext () =
2121 let len = String.length state.text in
2122 let drawstring s =
2123 let hscrollh =
2124 match state.mode with
2125 | Textentry _
2126 | View ->
2127 let h, _, _ = state.uioh#scrollpw in
2129 | _ -> 0
2131 let rect x w =
2132 GlDraw.rect
2133 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2134 (x+.w, float (conf.winh - hscrollh))
2137 let w = float (conf.winw - state.scrollw - 1) in
2138 if state.progress >= 0.0 && state.progress < 1.0
2139 then (
2140 GlDraw.color (0.3, 0.3, 0.3);
2141 let w1 = w *. state.progress in
2142 rect 0.0 w1;
2143 GlDraw.color (0.0, 0.0, 0.0);
2144 rect w1 (w-.w1)
2146 else (
2147 GlDraw.color (0.0, 0.0, 0.0);
2148 rect 0.0 w;
2151 GlDraw.color (1.0, 1.0, 1.0);
2152 drawstring fstate.fontsize
2153 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2155 let s =
2156 match state.mode with
2157 | Textentry ((prefix, text, _, _, _, _), _) ->
2158 let s =
2159 if len > 0
2160 then
2161 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2162 else
2163 Printf.sprintf "%s%s_" prefix text
2167 | _ -> state.text
2169 let s =
2170 if state.newerrmsgs
2171 then (
2172 if not (istextentry state.mode)
2173 then
2174 let s1 = "(press 'e' to review error messasges)" in
2175 if String.length s > 0 then s ^ " " ^ s1 else s1
2176 else s
2178 else s
2180 if String.length s > 0
2181 then drawstring s
2184 let gctiles () =
2185 let len = Queue.length state.tilelru in
2186 let layout = lazy (
2187 match state.throttle with
2188 | None ->
2189 if conf.preload
2190 then preloadlayout state.y
2191 else state.layout
2192 | Some (layout, _, _) ->
2193 layout
2194 ) in
2195 let rec loop qpos =
2196 if state.memused <= conf.memlimit
2197 then ()
2198 else (
2199 if qpos < len
2200 then
2201 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2202 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2203 let (_, pw, ph, _) = getpagedim n in
2205 gen = state.gen
2206 && colorspace = conf.colorspace
2207 && angle = conf.angle
2208 && pagew = pw
2209 && pageh = ph
2210 && (
2211 let x = col*conf.tilew
2212 and y = row*conf.tileh in
2213 tilevisible (Lazy.force_val layout) n x y
2215 then Queue.push lruitem state.tilelru
2216 else (
2217 freepbo p;
2218 wcmd "freetile %s" p;
2219 state.memused <- state.memused - s;
2220 state.uioh#infochanged Memused;
2221 Hashtbl.remove state.tilemap k;
2223 loop (qpos+1)
2226 loop 0
2229 let flushtiles () =
2230 Queue.iter (fun (k, p, s) ->
2231 wcmd "freetile %s" p;
2232 state.memused <- state.memused - s;
2233 state.uioh#infochanged Memused;
2234 Hashtbl.remove state.tilemap k;
2235 ) state.tilelru;
2236 Queue.clear state.tilelru;
2237 load state.layout;
2240 let logcurrently = function
2241 | Idle -> dolog "Idle"
2242 | Loading (l, gen) ->
2243 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2244 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2245 dolog
2246 "Tiling %d[%d,%d] page=%s cs=%s angle"
2247 l.pageno col row pageopaque
2248 (colorspace_to_string colorspace)
2250 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2251 angle gen conf.angle state.gen
2252 tilew tileh
2253 conf.tilew conf.tileh
2255 | Outlining _ ->
2256 dolog "outlining"
2259 let act cmds =
2260 (* dolog "%S" cmds; *)
2261 let op, args =
2262 let spacepos =
2263 try String.index cmds ' '
2264 with Not_found -> -1
2266 if spacepos = -1
2267 then cmds, ""
2268 else
2269 let l = String.length cmds in
2270 let op = String.sub cmds 0 spacepos in
2271 op, begin
2272 if l - spacepos < 2 then ""
2273 else String.sub cmds (spacepos+1) (l-spacepos-1)
2276 let scan s fmt f =
2277 try Scanf.sscanf s fmt f
2278 with exn ->
2279 dolog "error processing '%S' %S: %s"
2280 op cmds (Printexc.to_string exn);
2281 exit 1
2283 match op with
2284 | "clear" ->
2285 state.uioh#infochanged Pdim;
2286 state.pdims <- [];
2288 | "clearrects" ->
2289 state.rects <- state.rects1;
2290 G.postRedisplay "clearrects";
2292 | "continue" ->
2293 let n = scan args "%u" (fun n -> n) in
2294 state.pagecount <- n;
2295 begin match state.currently with
2296 | Outlining l ->
2297 state.currently <- Idle;
2298 state.outlines <- Array.of_list (List.rev l)
2299 | _ -> ()
2300 end;
2302 let cur, cmds = state.geomcmds in
2303 if String.length cur = 0
2304 then failwith "umpossible";
2306 begin match List.rev cmds with
2307 | [] ->
2308 state.geomcmds <- "", [];
2309 represent ();
2310 | (s, f) :: rest ->
2311 f ();
2312 state.geomcmds <- s, List.rev rest;
2313 end;
2314 if conf.maxwait = None
2315 then G.postRedisplay "continue";
2317 | "title" ->
2318 Wsi.settitle args
2320 | "msg" ->
2321 showtext ' ' args
2323 | "vmsg" ->
2324 if conf.verbose
2325 then showtext ' ' args
2327 | "emsg" ->
2328 Buffer.add_string state.errmsgs args;
2329 state.newerrmsgs <- true;
2330 G.postRedisplay "error message"
2332 | "progress" ->
2333 let progress, text =
2334 scan args "%f %n"
2335 (fun f pos ->
2336 f, String.sub args pos (String.length args - pos))
2338 state.text <- text;
2339 state.progress <- progress;
2340 G.postRedisplay "progress"
2342 | "firstmatch" ->
2343 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2344 scan args "%u %d %f %f %f %f %f %f %f %f"
2345 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2346 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2348 let y = (getpagey pageno) + truncate y0 in
2349 addnav ();
2350 gotoy y;
2351 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2353 | "match" ->
2354 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2355 scan args "%u %d %f %f %f %f %f %f %f %f"
2356 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2357 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2359 state.rects1 <-
2360 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2362 | "page" ->
2363 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2364 begin match state.currently with
2365 | Loading (l, gen) ->
2366 vlog "page %d took %f sec" l.pageno t;
2367 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2368 begin match state.throttle with
2369 | None ->
2370 let preloadedpages =
2371 if conf.preload
2372 then preloadlayout state.y
2373 else state.layout
2375 let evict () =
2376 let module IntSet =
2377 Set.Make (struct type t = int let compare = (-) end) in
2378 let set =
2379 List.fold_left (fun s l -> IntSet.add l.pageno s)
2380 IntSet.empty preloadedpages
2382 let evictedpages =
2383 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2384 if not (IntSet.mem pageno set)
2385 then (
2386 wcmd "freepage %s" opaque;
2387 key :: accu
2389 else accu
2390 ) state.pagemap []
2392 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2394 evict ();
2395 state.currently <- Idle;
2396 if gen = state.gen
2397 then (
2398 tilepage l.pageno pageopaque state.layout;
2399 load state.layout;
2400 load preloadedpages;
2401 if pagevisible state.layout l.pageno
2402 && layoutready state.layout
2403 then G.postRedisplay "page";
2406 | Some (layout, _, _) ->
2407 state.currently <- Idle;
2408 tilepage l.pageno pageopaque layout;
2409 load state.layout
2410 end;
2412 | _ ->
2413 dolog "Inconsistent loading state";
2414 logcurrently state.currently;
2415 exit 1
2418 | "tile" ->
2419 let (x, y, opaque, size, t) =
2420 scan args "%u %u %s %u %f"
2421 (fun x y p size t -> (x, y, p, size, t))
2423 begin match state.currently with
2424 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2425 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2427 unmappbo opaque;
2428 if tilew != conf.tilew || tileh != conf.tileh
2429 then (
2430 wcmd "freetile %s" opaque;
2431 state.currently <- Idle;
2432 load state.layout;
2434 else (
2435 puttileopaque l col row gen cs angle opaque size t;
2436 state.memused <- state.memused + size;
2437 state.uioh#infochanged Memused;
2438 gctiles ();
2439 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2440 opaque, size) state.tilelru;
2442 let layout =
2443 match state.throttle with
2444 | None -> state.layout
2445 | Some (layout, _, _) -> layout
2448 state.currently <- Idle;
2449 if gen = state.gen
2450 && conf.colorspace = cs
2451 && conf.angle = angle
2452 && tilevisible layout l.pageno x y
2453 then conttiling l.pageno pageopaque;
2455 begin match state.throttle with
2456 | None ->
2457 if state.wthack
2458 then state.wthack <- not (layoutready state.layout);
2459 preload state.layout;
2460 if gen = state.gen
2461 && conf.colorspace = cs
2462 && conf.angle = angle
2463 && tilevisible state.layout l.pageno x y
2464 then G.postRedisplay "tile nothrottle";
2466 | Some (layout, y, _) ->
2467 let ready = layoutready layout in
2468 if ready
2469 then (
2470 state.wthack <- false;
2471 state.y <- y;
2472 state.layout <- layout;
2473 state.throttle <- None;
2474 G.postRedisplay "throttle";
2476 else load layout;
2477 end;
2480 | _ ->
2481 dolog "Inconsistent tiling state";
2482 logcurrently state.currently;
2483 exit 1
2486 | "pdim" ->
2487 let pdim =
2488 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2490 state.uioh#infochanged Pdim;
2491 state.pdims <- pdim :: state.pdims
2493 | "o" ->
2494 let (l, n, t, h, pos) =
2495 scan args "%u %u %d %u %n"
2496 (fun l n t h pos -> l, n, t, h, pos)
2498 let s = String.sub args pos (String.length args - pos) in
2499 let outline = (s, l, (n, float t /. float h, 0.0)) in
2500 begin match state.currently with
2501 | Outlining outlines ->
2502 state.currently <- Outlining (outline :: outlines)
2503 | Idle ->
2504 state.currently <- Outlining [outline]
2505 | currently ->
2506 dolog "invalid outlining state";
2507 logcurrently currently
2510 | "a" ->
2511 let (n, t, h) =
2512 scan args "%u %u %d" (fun n t h -> n, t, h)
2514 let top, dtop =
2515 if conf.presentation
2516 then (0.0, 1.0)
2517 else float t /. float h, 0.0
2519 state.anchor <- (n, top, dtop)
2521 | "info" ->
2522 state.docinfo <- (1, args) :: state.docinfo
2524 | "infoend" ->
2525 state.uioh#infochanged Docinfo;
2526 state.docinfo <- List.rev state.docinfo
2528 | _ ->
2529 dolog "unknown cmd `%S'" cmds
2532 let onhist cb =
2533 let rc = cb.rc in
2534 let action = function
2535 | HCprev -> cbget cb ~-1
2536 | HCnext -> cbget cb 1
2537 | HCfirst -> cbget cb ~-(cb.rc)
2538 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2539 and cancel () = cb.rc <- rc
2540 in (action, cancel)
2543 let search pattern forward =
2544 if String.length pattern > 0
2545 then
2546 let pn, py =
2547 match state.layout with
2548 | [] -> 0, 0
2549 | l :: _ ->
2550 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2552 wcmd "search %d %d %d %d,%s\000"
2553 (btod conf.icase) pn py (btod forward) pattern;
2556 let intentry text key =
2557 let c =
2558 if key >= 32 && key < 127
2559 then Char.chr key
2560 else '\000'
2562 match c with
2563 | '0' .. '9' ->
2564 let text = addchar text c in
2565 TEcont text
2567 | _ ->
2568 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2569 TEcont text
2572 let linknentry text key =
2573 let c =
2574 if key >= 32 && key < 127
2575 then Char.chr key
2576 else '\000'
2578 match c with
2579 | 'a' .. 'z' ->
2580 let text = addchar text c in
2581 TEcont text
2583 | _ ->
2584 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2585 TEcont text
2588 let linkndone f s =
2589 if String.length s > 0
2590 then (
2591 let n =
2592 let l = String.length s in
2593 let rec loop pos n = if pos = l then n else
2594 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2595 loop (pos+1) (n*26 + m)
2596 in loop 0 0
2598 let rec loop n = function
2599 | [] -> ()
2600 | l :: rest ->
2601 match getopaque l.pageno with
2602 | None -> loop n rest
2603 | Some opaque ->
2604 let m = getlinkcount opaque in
2605 if n < m
2606 then (
2607 let under = getlink opaque n in
2608 f under
2610 else loop (n-m) rest
2612 loop n state.layout;
2616 let textentry text key =
2617 if key land 0xff00 = 0xff00
2618 then TEcont text
2619 else TEcont (text ^ Wsi.toutf8 key)
2622 let reqlayout angle proportional =
2623 match state.throttle with
2624 | None ->
2625 if nogeomcmds state.geomcmds
2626 then state.anchor <- getanchor ();
2627 conf.angle <- angle mod 360;
2628 if conf.angle != 0
2629 then (
2630 match state.mode with
2631 | LinkNav _ -> state.mode <- View
2632 | _ -> ()
2634 conf.proportional <- proportional;
2635 invalidate "reqlayout"
2636 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2637 | _ -> ()
2640 let settrim trimmargins trimfuzz =
2641 if nogeomcmds state.geomcmds
2642 then state.anchor <- getanchor ();
2643 conf.trimmargins <- trimmargins;
2644 conf.trimfuzz <- trimfuzz;
2645 let x0, y0, x1, y1 = trimfuzz in
2646 invalidate "settrim"
2647 (fun () ->
2648 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2649 flushpages ();
2652 let setzoom zoom =
2653 match state.throttle with
2654 | None ->
2655 let zoom = max 0.01 zoom in
2656 if zoom <> conf.zoom
2657 then (
2658 state.prevzoom <- conf.zoom;
2659 conf.zoom <- zoom;
2660 reshape conf.winw conf.winh;
2661 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2664 | Some (layout, y, started) ->
2665 let time =
2666 match conf.maxwait with
2667 | None -> 0.0
2668 | Some t -> t
2670 let dt = now () -. started in
2671 if dt > time
2672 then (
2673 state.y <- y;
2674 load layout;
2678 let setcolumns mode columns coverA coverB =
2679 state.prevcolumns <- Some (conf.columns, conf.zoom);
2680 if columns < 0
2681 then (
2682 if isbirdseye mode
2683 then showtext '!' "split mode doesn't work in bird's eye"
2684 else (
2685 conf.columns <- Csplit (-columns, [||]);
2686 state.x <- 0;
2687 conf.zoom <- 1.0;
2690 else (
2691 if columns < 2
2692 then (
2693 conf.columns <- Csingle [||];
2694 state.x <- 0;
2695 setzoom 1.0;
2697 else (
2698 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2699 conf.zoom <- 1.0;
2702 reshape conf.winw conf.winh;
2705 let enterbirdseye () =
2706 let zoom = float conf.thumbw /. float conf.winw in
2707 let birdseyepageno =
2708 let cy = conf.winh / 2 in
2709 let fold = function
2710 | [] -> 0
2711 | l :: rest ->
2712 let rec fold best = function
2713 | [] -> best.pageno
2714 | l :: rest ->
2715 let d = cy - (l.pagedispy + l.pagevh/2)
2716 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2717 if abs d < abs dbest
2718 then fold l rest
2719 else best.pageno
2720 in fold l rest
2722 fold state.layout
2724 state.mode <- Birdseye (
2725 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2727 conf.zoom <- zoom;
2728 conf.presentation <- false;
2729 conf.interpagespace <- 10;
2730 conf.hlinks <- false;
2731 state.x <- 0;
2732 state.mstate <- Mnone;
2733 conf.maxwait <- None;
2734 conf.columns <- (
2735 match conf.beyecolumns with
2736 | Some c ->
2737 conf.zoom <- 1.0;
2738 Cmulti ((c, 0, 0), [||])
2739 | None -> Csingle [||]
2741 Wsi.setcursor Wsi.CURSOR_INHERIT;
2742 if conf.verbose
2743 then
2744 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2745 (100.0*.zoom)
2746 else
2747 state.text <- ""
2749 reshape conf.winw conf.winh;
2752 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2753 state.mode <- View;
2754 conf.zoom <- c.zoom;
2755 conf.presentation <- c.presentation;
2756 conf.interpagespace <- c.interpagespace;
2757 conf.maxwait <- c.maxwait;
2758 conf.hlinks <- c.hlinks;
2759 conf.beyecolumns <- (
2760 match conf.columns with
2761 | Cmulti ((c, _, _), _) -> Some c
2762 | Csingle _ -> None
2763 | Csplit _ -> failwith "leaving bird's eye split mode"
2765 conf.columns <- (
2766 match c.columns with
2767 | Cmulti (c, _) -> Cmulti (c, [||])
2768 | Csingle _ -> Csingle [||]
2769 | Csplit (c, _) -> Csplit (c, [||])
2771 state.x <- leftx;
2772 if conf.verbose
2773 then
2774 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2775 (100.0*.conf.zoom)
2777 reshape conf.winw conf.winh;
2778 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2781 let togglebirdseye () =
2782 match state.mode with
2783 | Birdseye vals -> leavebirdseye vals true
2784 | View -> enterbirdseye ()
2785 | _ -> ()
2788 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2789 let pageno = max 0 (pageno - incr) in
2790 let rec loop = function
2791 | [] -> gotopage1 pageno 0
2792 | l :: _ when l.pageno = pageno ->
2793 if l.pagedispy >= 0 && l.pagey = 0
2794 then G.postRedisplay "upbirdseye"
2795 else gotopage1 pageno 0
2796 | _ :: rest -> loop rest
2798 loop state.layout;
2799 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2802 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2803 let pageno = min (state.pagecount - 1) (pageno + incr) in
2804 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2805 let rec loop = function
2806 | [] ->
2807 let y, h = getpageyh pageno in
2808 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2809 gotoy (clamp dy)
2810 | l :: _ when l.pageno = pageno ->
2811 if l.pagevh != l.pageh
2812 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2813 else G.postRedisplay "downbirdseye"
2814 | _ :: rest -> loop rest
2816 loop state.layout
2819 let optentry mode _ key =
2820 let btos b = if b then "on" else "off" in
2821 if key >= 32 && key < 127
2822 then
2823 let c = Char.chr key in
2824 match c with
2825 | 's' ->
2826 let ondone s =
2827 try conf.scrollstep <- int_of_string s with exc ->
2828 state.text <- Printf.sprintf "bad integer `%s': %s"
2829 s (Printexc.to_string exc)
2831 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2833 | 'A' ->
2834 let ondone s =
2836 conf.autoscrollstep <- int_of_string s;
2837 if state.autoscroll <> None
2838 then state.autoscroll <- Some conf.autoscrollstep
2839 with exc ->
2840 state.text <- Printf.sprintf "bad integer `%s': %s"
2841 s (Printexc.to_string exc)
2843 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2845 | 'C' ->
2846 let ondone s =
2848 let n, a, b = multicolumns_of_string s in
2849 setcolumns mode n a b;
2850 with exc ->
2851 state.text <- Printf.sprintf "bad columns `%s': %s"
2852 s (Printexc.to_string exc)
2854 TEswitch ("columns: ", "", None, textentry, ondone, true)
2856 | 'Z' ->
2857 let ondone s =
2859 let zoom = float (int_of_string s) /. 100.0 in
2860 setzoom zoom
2861 with exc ->
2862 state.text <- Printf.sprintf "bad integer `%s': %s"
2863 s (Printexc.to_string exc)
2865 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2867 | 't' ->
2868 let ondone s =
2870 conf.thumbw <- bound (int_of_string s) 2 4096;
2871 state.text <-
2872 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2873 begin match mode with
2874 | Birdseye beye ->
2875 leavebirdseye beye false;
2876 enterbirdseye ();
2877 | _ -> ();
2879 with exc ->
2880 state.text <- Printf.sprintf "bad integer `%s': %s"
2881 s (Printexc.to_string exc)
2883 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2885 | 'R' ->
2886 let ondone s =
2887 match try
2888 Some (int_of_string s)
2889 with exc ->
2890 state.text <- Printf.sprintf "bad integer `%s': %s"
2891 s (Printexc.to_string exc);
2892 None
2893 with
2894 | Some angle -> reqlayout angle conf.proportional
2895 | None -> ()
2897 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2899 | 'i' ->
2900 conf.icase <- not conf.icase;
2901 TEdone ("case insensitive search " ^ (btos conf.icase))
2903 | 'p' ->
2904 conf.preload <- not conf.preload;
2905 gotoy state.y;
2906 TEdone ("preload " ^ (btos conf.preload))
2908 | 'v' ->
2909 conf.verbose <- not conf.verbose;
2910 TEdone ("verbose " ^ (btos conf.verbose))
2912 | 'd' ->
2913 conf.debug <- not conf.debug;
2914 TEdone ("debug " ^ (btos conf.debug))
2916 | 'h' ->
2917 conf.maxhfit <- not conf.maxhfit;
2918 state.maxy <- calcheight ();
2919 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2921 | 'c' ->
2922 conf.crophack <- not conf.crophack;
2923 TEdone ("crophack " ^ btos conf.crophack)
2925 | 'a' ->
2926 let s =
2927 match conf.maxwait with
2928 | None ->
2929 conf.maxwait <- Some infinity;
2930 "always wait for page to complete"
2931 | Some _ ->
2932 conf.maxwait <- None;
2933 "show placeholder if page is not ready"
2935 TEdone s
2937 | 'f' ->
2938 conf.underinfo <- not conf.underinfo;
2939 TEdone ("underinfo " ^ btos conf.underinfo)
2941 | 'P' ->
2942 conf.savebmarks <- not conf.savebmarks;
2943 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2945 | 'S' ->
2946 let ondone s =
2948 let pageno, py =
2949 match state.layout with
2950 | [] -> 0, 0
2951 | l :: _ ->
2952 l.pageno, l.pagey
2954 conf.interpagespace <- int_of_string s;
2955 docolumns conf.columns;
2956 state.maxy <- calcheight ();
2957 let y = getpagey pageno in
2958 gotoy (y + py)
2959 with exc ->
2960 state.text <- Printf.sprintf "bad integer `%s': %s"
2961 s (Printexc.to_string exc)
2963 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2965 | 'l' ->
2966 reqlayout conf.angle (not conf.proportional);
2967 TEdone ("proportional display " ^ btos conf.proportional)
2969 | 'T' ->
2970 settrim (not conf.trimmargins) conf.trimfuzz;
2971 TEdone ("trim margins " ^ btos conf.trimmargins)
2973 | 'I' ->
2974 conf.invert <- not conf.invert;
2975 TEdone ("invert colors " ^ btos conf.invert)
2977 | 'x' ->
2978 let ondone s =
2979 cbput state.hists.sel s;
2980 conf.selcmd <- s;
2982 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2983 textentry, ondone, true)
2985 | _ ->
2986 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2987 TEstop
2988 else
2989 TEcont state.text
2992 class type lvsource = object
2993 method getitemcount : int
2994 method getitem : int -> (string * int)
2995 method hasaction : int -> bool
2996 method exit :
2997 uioh:uioh ->
2998 cancel:bool ->
2999 active:int ->
3000 first:int ->
3001 pan:int ->
3002 qsearch:string ->
3003 uioh option
3004 method getactive : int
3005 method getfirst : int
3006 method getqsearch : string
3007 method setqsearch : string -> unit
3008 method getpan : int
3009 end;;
3011 class virtual lvsourcebase = object
3012 val mutable m_active = 0
3013 val mutable m_first = 0
3014 val mutable m_qsearch = ""
3015 val mutable m_pan = 0
3016 method getactive = m_active
3017 method getfirst = m_first
3018 method getqsearch = m_qsearch
3019 method getpan = m_pan
3020 method setqsearch s = m_qsearch <- s
3021 end;;
3023 let withoutlastutf8 s =
3024 let len = String.length s in
3025 if len = 0
3026 then s
3027 else
3028 let rec find pos =
3029 if pos = 0
3030 then pos
3031 else
3032 let b = Char.code s.[pos] in
3033 if b land 0b11000000 = 0b11000000
3034 then pos
3035 else find (pos-1)
3037 let first =
3038 if Char.code s.[len-1] land 0x80 = 0
3039 then len-1
3040 else find (len-1)
3042 String.sub s 0 first;
3045 let textentrykeyboard
3046 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3047 let key =
3048 if key >= 0xffb0 && key <= 0xffb9
3049 then key - 0xffb0 + 48 else key
3051 let enttext te =
3052 state.mode <- Textentry (te, onleave);
3053 state.text <- "";
3054 enttext ();
3055 G.postRedisplay "textentrykeyboard enttext";
3057 let histaction cmd =
3058 match opthist with
3059 | None -> ()
3060 | Some (action, _) ->
3061 state.mode <- Textentry (
3062 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3064 G.postRedisplay "textentry histaction"
3066 match key with
3067 | 0xff08 -> (* backspace *)
3068 let s = withoutlastutf8 text in
3069 let len = String.length s in
3070 if cancelonempty && len = 0
3071 then (
3072 onleave Cancel;
3073 G.postRedisplay "textentrykeyboard after cancel";
3075 else (
3076 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3079 | 0xff0d | 0xff8d -> (* (kp) enter *)
3080 ondone text;
3081 onleave Confirm;
3082 G.postRedisplay "textentrykeyboard after confirm"
3084 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3085 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3086 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3087 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3089 | 0xff1b -> (* escape*)
3090 if String.length text = 0
3091 then (
3092 begin match opthist with
3093 | None -> ()
3094 | Some (_, onhistcancel) -> onhistcancel ()
3095 end;
3096 onleave Cancel;
3097 state.text <- "";
3098 G.postRedisplay "textentrykeyboard after cancel2"
3100 else (
3101 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3104 | 0xff9f | 0xffff -> () (* delete *)
3106 | _ when key != 0
3107 && key land 0xff00 != 0xff00 (* keyboard *)
3108 && key land 0xfe00 != 0xfe00 (* xkb *)
3109 && key land 0xfd00 != 0xfd00 (* 3270 *)
3111 begin match onkey text key with
3112 | TEdone text ->
3113 ondone text;
3114 onleave Confirm;
3115 G.postRedisplay "textentrykeyboard after confirm2";
3117 | TEcont text ->
3118 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3120 | TEstop ->
3121 onleave Cancel;
3122 G.postRedisplay "textentrykeyboard after cancel3"
3124 | TEswitch te ->
3125 state.mode <- Textentry (te, onleave);
3126 G.postRedisplay "textentrykeyboard switch";
3127 end;
3129 | _ ->
3130 vlog "unhandled key %s" (Wsi.keyname key)
3133 let firstof first active =
3134 if first > active || abs (first - active) > fstate.maxrows - 1
3135 then max 0 (active - (fstate.maxrows/2))
3136 else first
3139 let calcfirst first active =
3140 if active > first
3141 then
3142 let rows = active - first in
3143 if rows > fstate.maxrows then active - fstate.maxrows else first
3144 else active
3147 let scrollph y maxy =
3148 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3149 let sh = float conf.winh /. sh in
3150 let sh = max sh (float conf.scrollh) in
3152 let percent =
3153 if y = state.maxy
3154 then 1.0
3155 else float y /. float maxy
3157 let position = (float conf.winh -. sh) *. percent in
3159 let position =
3160 if position +. sh > float conf.winh
3161 then float conf.winh -. sh
3162 else position
3164 position, sh;
3167 let coe s = (s :> uioh);;
3169 class listview ~(source:lvsource) ~trusted ~modehash =
3170 object (self)
3171 val m_pan = source#getpan
3172 val m_first = source#getfirst
3173 val m_active = source#getactive
3174 val m_qsearch = source#getqsearch
3175 val m_prev_uioh = state.uioh
3177 method private elemunder y =
3178 let n = y / (fstate.fontsize+1) in
3179 if m_first + n < source#getitemcount
3180 then (
3181 if source#hasaction (m_first + n)
3182 then Some (m_first + n)
3183 else None
3185 else None
3187 method display =
3188 Gl.enable `blend;
3189 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3190 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3191 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3192 GlDraw.color (1., 1., 1.);
3193 Gl.enable `texture_2d;
3194 let fs = fstate.fontsize in
3195 let nfs = fs + 1 in
3196 let ww = fstate.wwidth in
3197 let tabw = 30.0*.ww in
3198 let itemcount = source#getitemcount in
3199 let rec loop row =
3200 if (row - m_first) > fstate.maxrows
3201 then ()
3202 else (
3203 if row >= 0 && row < itemcount
3204 then (
3205 let (s, level) = source#getitem row in
3206 let y = (row - m_first) * nfs in
3207 let x = 5.0 +. float (level + m_pan) *. ww in
3208 if row = m_active
3209 then (
3210 Gl.disable `texture_2d;
3211 GlDraw.polygon_mode `both `line;
3212 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3213 GlDraw.rect (1., float (y + 1))
3214 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3215 GlDraw.polygon_mode `both `fill;
3216 GlDraw.color (1., 1., 1.);
3217 Gl.enable `texture_2d;
3220 let drawtabularstring s =
3221 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3222 if trusted
3223 then
3224 let tabpos = try String.index s '\t' with Not_found -> -1 in
3225 if tabpos > 0
3226 then
3227 let len = String.length s - tabpos - 1 in
3228 let s1 = String.sub s 0 tabpos
3229 and s2 = String.sub s (tabpos + 1) len in
3230 let nx = drawstr x s1 in
3231 let sw = nx -. x in
3232 let x = x +. (max tabw sw) in
3233 drawstr x s2
3234 else
3235 drawstr x s
3236 else
3237 drawstr x s
3239 let _ = drawtabularstring s in
3240 loop (row+1)
3244 loop m_first;
3245 Gl.disable `blend;
3246 Gl.disable `texture_2d;
3248 method updownlevel incr =
3249 let len = source#getitemcount in
3250 let curlevel =
3251 if m_active >= 0 && m_active < len
3252 then snd (source#getitem m_active)
3253 else -1
3255 let rec flow i =
3256 if i = len then i-1 else if i = -1 then 0 else
3257 let _, l = source#getitem i in
3258 if l != curlevel then i else flow (i+incr)
3260 let active = flow m_active in
3261 let first = calcfirst m_first active in
3262 G.postRedisplay "outline updownlevel";
3263 {< m_active = active; m_first = first >}
3265 method private key1 key mask =
3266 let set1 active first qsearch =
3267 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3269 let search active pattern incr =
3270 let dosearch re =
3271 let rec loop n =
3272 if n >= 0 && n < source#getitemcount
3273 then (
3274 let s, _ = source#getitem n in
3276 (try ignore (Str.search_forward re s 0); true
3277 with Not_found -> false)
3278 then Some n
3279 else loop (n + incr)
3281 else None
3283 loop active
3286 let re = Str.regexp_case_fold pattern in
3287 dosearch re
3288 with Failure s ->
3289 state.text <- s;
3290 None
3292 let itemcount = source#getitemcount in
3293 let find start incr =
3294 let rec find i =
3295 if i = -1 || i = itemcount
3296 then -1
3297 else (
3298 if source#hasaction i
3299 then i
3300 else find (i + incr)
3303 find start
3305 let set active first =
3306 let first = bound first 0 (itemcount - fstate.maxrows) in
3307 state.text <- "";
3308 coe {< m_active = active; m_first = first >}
3310 let navigate incr =
3311 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3312 let active, first =
3313 let incr1 = if incr > 0 then 1 else -1 in
3314 if isvisible m_first m_active
3315 then
3316 let next =
3317 let next = m_active + incr in
3318 let next =
3319 if next < 0 || next >= itemcount
3320 then -1
3321 else find next incr1
3323 if next = -1 || abs (m_active - next) > fstate.maxrows
3324 then -1
3325 else next
3327 if next = -1
3328 then
3329 let first = m_first + incr in
3330 let first = bound first 0 (itemcount - 1) in
3331 let next =
3332 let next = m_active + incr in
3333 let next = bound next 0 (itemcount - 1) in
3334 find next ~-incr1
3336 let active = if next = -1 then m_active else next in
3337 active, first
3338 else
3339 let first = min next m_first in
3340 let first =
3341 if abs (next - first) > fstate.maxrows
3342 then first + incr
3343 else first
3345 next, first
3346 else
3347 let first = m_first + incr in
3348 let first = bound first 0 (itemcount - 1) in
3349 let active =
3350 let next = m_active + incr in
3351 let next = bound next 0 (itemcount - 1) in
3352 let next = find next incr1 in
3353 let active =
3354 if next = -1 || abs (m_active - first) > fstate.maxrows
3355 then (
3356 let active = if m_active = -1 then next else m_active in
3357 active
3359 else next
3361 if isvisible first active
3362 then active
3363 else -1
3365 active, first
3367 G.postRedisplay "listview navigate";
3368 set active first;
3370 match key with
3371 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3372 let incr = if key = 0x72 then -1 else 1 in
3373 let active, first =
3374 match search (m_active + incr) m_qsearch incr with
3375 | None ->
3376 state.text <- m_qsearch ^ " [not found]";
3377 m_active, m_first
3378 | Some active ->
3379 state.text <- m_qsearch;
3380 active, firstof m_first active
3382 G.postRedisplay "listview ctrl-r/s";
3383 set1 active first m_qsearch;
3385 | 0xff08 -> (* backspace *)
3386 if String.length m_qsearch = 0
3387 then coe self
3388 else (
3389 let qsearch = withoutlastutf8 m_qsearch in
3390 let len = String.length qsearch in
3391 if len = 0
3392 then (
3393 state.text <- "";
3394 G.postRedisplay "listview empty qsearch";
3395 set1 m_active m_first "";
3397 else
3398 let active, first =
3399 match search m_active qsearch ~-1 with
3400 | None ->
3401 state.text <- qsearch ^ " [not found]";
3402 m_active, m_first
3403 | Some active ->
3404 state.text <- qsearch;
3405 active, firstof m_first active
3407 G.postRedisplay "listview backspace qsearch";
3408 set1 active first qsearch
3411 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3412 let pattern = m_qsearch ^ Wsi.toutf8 key in
3413 let active, first =
3414 match search m_active pattern 1 with
3415 | None ->
3416 state.text <- pattern ^ " [not found]";
3417 m_active, m_first
3418 | Some active ->
3419 state.text <- pattern;
3420 active, firstof m_first active
3422 G.postRedisplay "listview qsearch add";
3423 set1 active first pattern;
3425 | 0xff1b -> (* escape *)
3426 state.text <- "";
3427 if String.length m_qsearch = 0
3428 then (
3429 G.postRedisplay "list view escape";
3430 begin
3431 match
3432 source#exit (coe self) true m_active m_first m_pan m_qsearch
3433 with
3434 | None -> m_prev_uioh
3435 | Some uioh -> uioh
3438 else (
3439 G.postRedisplay "list view kill qsearch";
3440 source#setqsearch "";
3441 coe {< m_qsearch = "" >}
3444 | 0xff0d | 0xff8d -> (* (kp) enter *)
3445 state.text <- "";
3446 let self = {< m_qsearch = "" >} in
3447 source#setqsearch "";
3448 let opt =
3449 G.postRedisplay "listview enter";
3450 if m_active >= 0 && m_active < source#getitemcount
3451 then (
3452 source#exit (coe self) false m_active m_first m_pan "";
3454 else (
3455 source#exit (coe self) true m_active m_first m_pan "";
3458 begin match opt with
3459 | None -> m_prev_uioh
3460 | Some uioh -> uioh
3463 | 0xff9f | 0xffff -> (* (kp) delete *)
3464 coe self
3466 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3467 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3468 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3469 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3471 | 0xff53 | 0xff98 -> (* (kp) right *)
3472 state.text <- "";
3473 G.postRedisplay "listview right";
3474 coe {< m_pan = m_pan - 1 >}
3476 | 0xff51 | 0xff96 -> (* (kp) left *)
3477 state.text <- "";
3478 G.postRedisplay "listview left";
3479 coe {< m_pan = m_pan + 1 >}
3481 | 0xff50 | 0xff95 -> (* (kp) home *)
3482 let active = find 0 1 in
3483 G.postRedisplay "listview home";
3484 set active 0;
3486 | 0xff57 | 0xff9c -> (* (kp) end *)
3487 let first = max 0 (itemcount - fstate.maxrows) in
3488 let active = find (itemcount - 1) ~-1 in
3489 G.postRedisplay "listview end";
3490 set active first;
3492 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3493 coe self
3495 | _ ->
3496 dolog "listview unknown key %#x" key; coe self
3498 method key key mask =
3499 match state.mode with
3500 | Textentry te -> textentrykeyboard key mask te; coe self
3501 | _ -> self#key1 key mask
3503 method button button down x y _ =
3504 let opt =
3505 match button with
3506 | 1 when x > conf.winw - conf.scrollbw ->
3507 G.postRedisplay "listview scroll";
3508 if down
3509 then
3510 let _, position, sh = self#scrollph in
3511 if y > truncate position && y < truncate (position +. sh)
3512 then (
3513 state.mstate <- Mscrolly;
3514 Some (coe self)
3516 else
3517 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3518 let first = truncate (s *. float source#getitemcount) in
3519 let first = min source#getitemcount first in
3520 Some (coe {< m_first = first; m_active = first >})
3521 else (
3522 state.mstate <- Mnone;
3523 Some (coe self);
3525 | 1 when not down ->
3526 begin match self#elemunder y with
3527 | Some n ->
3528 G.postRedisplay "listview click";
3529 source#exit
3530 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3531 | _ ->
3532 Some (coe self)
3534 | n when (n == 4 || n == 5) && not down ->
3535 let len = source#getitemcount in
3536 let first =
3537 if n = 5 && m_first + fstate.maxrows >= len
3538 then
3539 m_first
3540 else
3541 let first = m_first + (if n == 4 then -1 else 1) in
3542 bound first 0 (len - 1)
3544 G.postRedisplay "listview wheel";
3545 Some (coe {< m_first = first >})
3546 | n when (n = 6 || n = 7) && not down ->
3547 let inc = m_first + (if n = 7 then -1 else 1) in
3548 G.postRedisplay "listview hwheel";
3549 Some (coe {< m_pan = m_pan + inc >})
3550 | _ ->
3551 Some (coe self)
3553 match opt with
3554 | None -> m_prev_uioh
3555 | Some uioh -> uioh
3557 method motion _ y =
3558 match state.mstate with
3559 | Mscrolly ->
3560 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3561 let first = truncate (s *. float source#getitemcount) in
3562 let first = min source#getitemcount first in
3563 G.postRedisplay "listview motion";
3564 coe {< m_first = first; m_active = first >}
3565 | _ -> coe self
3567 method pmotion x y =
3568 if x < conf.winw - conf.scrollbw
3569 then
3570 let n =
3571 match self#elemunder y with
3572 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3573 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3575 let o =
3576 if n != m_active
3577 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3578 else self
3580 coe o
3581 else (
3582 Wsi.setcursor Wsi.CURSOR_INHERIT;
3583 coe self
3586 method infochanged _ = ()
3588 method scrollpw = (0, 0.0, 0.0)
3589 method scrollph =
3590 let nfs = fstate.fontsize + 1 in
3591 let y = m_first * nfs in
3592 let itemcount = source#getitemcount in
3593 let maxi = max 0 (itemcount - fstate.maxrows) in
3594 let maxy = maxi * nfs in
3595 let p, h = scrollph y maxy in
3596 conf.scrollbw, p, h
3598 method modehash = modehash
3599 end;;
3601 class outlinelistview ~source =
3602 object (self)
3603 inherit listview
3604 ~source:(source :> lvsource)
3605 ~trusted:false
3606 ~modehash:(findkeyhash conf "outline")
3607 as super
3609 method key key mask =
3610 let calcfirst first active =
3611 if active > first
3612 then
3613 let rows = active - first in
3614 let maxrows =
3615 if String.length state.text = 0
3616 then fstate.maxrows
3617 else fstate.maxrows - 2
3619 if rows > maxrows then active - maxrows else first
3620 else active
3622 let navigate incr =
3623 let active = m_active + incr in
3624 let active = bound active 0 (source#getitemcount - 1) in
3625 let first = calcfirst m_first active in
3626 G.postRedisplay "outline navigate";
3627 coe {< m_active = active; m_first = first >}
3629 let ctrl = Wsi.withctrl mask in
3630 match key with
3631 | 110 when ctrl -> (* ctrl-n *)
3632 source#narrow m_qsearch;
3633 G.postRedisplay "outline ctrl-n";
3634 coe {< m_first = 0; m_active = 0 >}
3636 | 117 when ctrl -> (* ctrl-u *)
3637 source#denarrow;
3638 G.postRedisplay "outline ctrl-u";
3639 state.text <- "";
3640 coe {< m_first = 0; m_active = 0 >}
3642 | 108 when ctrl -> (* ctrl-l *)
3643 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3644 G.postRedisplay "outline ctrl-l";
3645 coe {< m_first = first >}
3647 | 0xff9f | 0xffff -> (* (kp) delete *)
3648 source#remove m_active;
3649 G.postRedisplay "outline delete";
3650 let active = max 0 (m_active-1) in
3651 coe {< m_first = firstof m_first active;
3652 m_active = active >}
3654 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3655 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3656 | 0xff55 | 0xff9a -> (* (kp) prior *)
3657 navigate ~-(fstate.maxrows)
3658 | 0xff56 | 0xff9b -> (* (kp) next *)
3659 navigate fstate.maxrows
3661 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3662 let o =
3663 if ctrl
3664 then (
3665 G.postRedisplay "outline ctrl right";
3666 {< m_pan = m_pan + 1 >}
3668 else self#updownlevel 1
3670 coe o
3672 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3673 let o =
3674 if ctrl
3675 then (
3676 G.postRedisplay "outline ctrl left";
3677 {< m_pan = m_pan - 1 >}
3679 else self#updownlevel ~-1
3681 coe o
3683 | 0xff50 | 0xff95 -> (* (kp) home *)
3684 G.postRedisplay "outline home";
3685 coe {< m_first = 0; m_active = 0 >}
3687 | 0xff57 | 0xff9c -> (* (kp) end *)
3688 let active = source#getitemcount - 1 in
3689 let first = max 0 (active - fstate.maxrows) in
3690 G.postRedisplay "outline end";
3691 coe {< m_active = active; m_first = first >}
3693 | _ -> super#key key mask
3696 let outlinesource usebookmarks =
3697 let empty = [||] in
3698 (object
3699 inherit lvsourcebase
3700 val mutable m_items = empty
3701 val mutable m_orig_items = empty
3702 val mutable m_prev_items = empty
3703 val mutable m_narrow_pattern = ""
3704 val mutable m_hadremovals = false
3706 method getitemcount =
3707 Array.length m_items + (if m_hadremovals then 1 else 0)
3709 method getitem n =
3710 if n == Array.length m_items && m_hadremovals
3711 then
3712 ("[Confirm removal]", 0)
3713 else
3714 let s, n, _ = m_items.(n) in
3715 (s, n)
3717 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3718 ignore (uioh, first, qsearch);
3719 let confrimremoval = m_hadremovals && active = Array.length m_items in
3720 let items =
3721 if String.length m_narrow_pattern = 0
3722 then m_orig_items
3723 else m_items
3725 if not cancel
3726 then (
3727 if not confrimremoval
3728 then(
3729 let _, _, anchor = m_items.(active) in
3730 gotoghyll (getanchory anchor);
3731 m_items <- items;
3733 else (
3734 state.bookmarks <- Array.to_list m_items;
3735 m_orig_items <- m_items;
3738 else m_items <- items;
3739 m_pan <- pan;
3740 None
3742 method hasaction _ = true
3744 method greetmsg =
3745 if Array.length m_items != Array.length m_orig_items
3746 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3747 else ""
3749 method narrow pattern =
3750 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3751 match reopt with
3752 | None -> ()
3753 | Some re ->
3754 let rec loop accu n =
3755 if n = -1
3756 then (
3757 m_narrow_pattern <- pattern;
3758 m_items <- Array.of_list accu
3760 else
3761 let (s, _, _) as o = m_items.(n) in
3762 let accu =
3763 if (try ignore (Str.search_forward re s 0); true
3764 with Not_found -> false)
3765 then o :: accu
3766 else accu
3768 loop accu (n-1)
3770 loop [] (Array.length m_items - 1)
3772 method denarrow =
3773 m_orig_items <- (
3774 if usebookmarks
3775 then Array.of_list state.bookmarks
3776 else state.outlines
3778 m_items <- m_orig_items
3780 method remove m =
3781 if usebookmarks
3782 then
3783 if m >= 0 && m < Array.length m_items
3784 then (
3785 m_hadremovals <- true;
3786 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3787 let n = if n >= m then n+1 else n in
3788 m_items.(n)
3792 method reset anchor items =
3793 m_hadremovals <- false;
3794 if m_orig_items == empty || m_prev_items != items
3795 then (
3796 m_orig_items <- items;
3797 if String.length m_narrow_pattern = 0
3798 then m_items <- items;
3800 m_prev_items <- items;
3801 let rely = getanchory anchor in
3802 let active =
3803 let rec loop n best bestd =
3804 if n = Array.length m_items
3805 then best
3806 else
3807 let (_, _, anchor) = m_items.(n) in
3808 let orely = getanchory anchor in
3809 let d = abs (orely - rely) in
3810 if d < bestd
3811 then loop (n+1) n d
3812 else loop (n+1) best bestd
3814 loop 0 ~-1 max_int
3816 m_active <- active;
3817 m_first <- firstof m_first active
3818 end)
3821 let enterselector usebookmarks =
3822 let source = outlinesource usebookmarks in
3823 fun errmsg ->
3824 let outlines =
3825 if usebookmarks
3826 then Array.of_list state.bookmarks
3827 else state.outlines
3829 if Array.length outlines = 0
3830 then (
3831 showtext ' ' errmsg;
3833 else (
3834 state.text <- source#greetmsg;
3835 Wsi.setcursor Wsi.CURSOR_INHERIT;
3836 let anchor = getanchor () in
3837 source#reset anchor outlines;
3838 state.uioh <- coe (new outlinelistview ~source);
3839 G.postRedisplay "enter selector";
3843 let enteroutlinemode =
3844 let f = enterselector false in
3845 fun ()-> f "Document has no outline";
3848 let enterbookmarkmode =
3849 let f = enterselector true in
3850 fun () -> f "Document has no bookmarks (yet)";
3853 let color_of_string s =
3854 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3855 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3859 let color_to_string (r, g, b) =
3860 let r = truncate (r *. 256.0)
3861 and g = truncate (g *. 256.0)
3862 and b = truncate (b *. 256.0) in
3863 Printf.sprintf "%d/%d/%d" r g b
3866 let irect_of_string s =
3867 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3870 let irect_to_string (x0,y0,x1,y1) =
3871 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3874 let makecheckers () =
3875 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3876 following to say:
3877 converted by Issac Trotts. July 25, 2002 *)
3878 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3879 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3880 let id = GlTex.gen_texture () in
3881 GlTex.bind_texture `texture_2d id;
3882 GlPix.store (`unpack_alignment 1);
3883 GlTex.image2d image;
3884 List.iter (GlTex.parameter ~target:`texture_2d)
3885 [ `mag_filter `nearest; `min_filter `nearest ];
3889 let setcheckers enabled =
3890 match state.texid with
3891 | None ->
3892 if enabled then state.texid <- Some (makecheckers ())
3894 | Some texid ->
3895 if not enabled
3896 then (
3897 GlTex.delete_texture texid;
3898 state.texid <- None;
3902 let int_of_string_with_suffix s =
3903 let l = String.length s in
3904 let s1, shift =
3905 if l > 1
3906 then
3907 let suffix = Char.lowercase s.[l-1] in
3908 match suffix with
3909 | 'k' -> String.sub s 0 (l-1), 10
3910 | 'm' -> String.sub s 0 (l-1), 20
3911 | 'g' -> String.sub s 0 (l-1), 30
3912 | _ -> s, 0
3913 else s, 0
3915 let n = int_of_string s1 in
3916 let m = n lsl shift in
3917 if m < 0 || m < n
3918 then raise (Failure "value too large")
3919 else m
3922 let string_with_suffix_of_int n =
3923 if n = 0
3924 then "0"
3925 else
3926 let n, s =
3927 if n land ((1 lsl 30) - 1) = 0
3928 then n lsr 30, "G"
3929 else (
3930 if n land ((1 lsl 20) - 1) = 0
3931 then n lsr 20, "M"
3932 else (
3933 if n land ((1 lsl 10) - 1) = 0
3934 then n lsr 10, "K"
3935 else n, ""
3939 let rec loop s n =
3940 let h = n mod 1000 in
3941 let n = n / 1000 in
3942 if n = 0
3943 then string_of_int h ^ s
3944 else (
3945 let s = Printf.sprintf "_%03d%s" h s in
3946 loop s n
3949 loop "" n ^ s;
3952 let defghyllscroll = (40, 8, 32);;
3953 let ghyllscroll_of_string s =
3954 let (n, a, b) as nab =
3955 if s = "default"
3956 then defghyllscroll
3957 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3959 if n <= a || n <= b || a >= b
3960 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3961 nab;
3964 let ghyllscroll_to_string ((n, a, b) as nab) =
3965 if nab = defghyllscroll
3966 then "default"
3967 else Printf.sprintf "%d,%d,%d" n a b;
3970 let describe_location () =
3971 let f (fn, _) l =
3972 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3974 let fn, ln = List.fold_left f (-1, -1) state.layout in
3975 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3976 let percent =
3977 if maxy <= 0
3978 then 100.
3979 else (100. *. (float state.y /. float maxy))
3981 if fn = ln
3982 then
3983 Printf.sprintf "page %d of %d [%.2f%%]"
3984 (fn+1) state.pagecount percent
3985 else
3986 Printf.sprintf
3987 "pages %d-%d of %d [%.2f%%]"
3988 (fn+1) (ln+1) state.pagecount percent
3991 let setpresentationmode v =
3992 let (n, _, _) = getanchor () in
3993 let _, h = getpageyh n in
3994 let ips = if conf.presentation then calcips h else conf.interpagespace in
3995 state.anchor <- (n, 0.0, float ips);
3996 conf.presentation <- v;
3997 if conf.presentation
3998 then (
3999 if not conf.scrollbarinpm
4000 then state.scrollw <- 0;
4002 else state.scrollw <- conf.scrollbw;
4003 represent ();
4006 let enterinfomode =
4007 let btos b = if b then "\xe2\x88\x9a" else "" in
4008 let showextended = ref false in
4009 let leave mode = function
4010 | Confirm -> state.mode <- mode
4011 | Cancel -> state.mode <- mode in
4012 let src =
4013 (object
4014 val mutable m_first_time = true
4015 val mutable m_l = []
4016 val mutable m_a = [||]
4017 val mutable m_prev_uioh = nouioh
4018 val mutable m_prev_mode = View
4020 inherit lvsourcebase
4022 method reset prev_mode prev_uioh =
4023 m_a <- Array.of_list (List.rev m_l);
4024 m_l <- [];
4025 m_prev_mode <- prev_mode;
4026 m_prev_uioh <- prev_uioh;
4027 if m_first_time
4028 then (
4029 let rec loop n =
4030 if n >= Array.length m_a
4031 then ()
4032 else
4033 match m_a.(n) with
4034 | _, _, _, Action _ -> m_active <- n
4035 | _ -> loop (n+1)
4037 loop 0;
4038 m_first_time <- false;
4041 method int name get set =
4042 m_l <-
4043 (name, `int get, 1, Action (
4044 fun u ->
4045 let ondone s =
4046 try set (int_of_string s)
4047 with exn ->
4048 state.text <- Printf.sprintf "bad integer `%s': %s"
4049 s (Printexc.to_string exn)
4051 state.text <- "";
4052 let te = name ^ ": ", "", None, intentry, ondone, true in
4053 state.mode <- Textentry (te, leave m_prev_mode);
4055 )) :: m_l
4057 method int_with_suffix name get set =
4058 m_l <-
4059 (name, `intws get, 1, Action (
4060 fun u ->
4061 let ondone s =
4062 try set (int_of_string_with_suffix s)
4063 with exn ->
4064 state.text <- Printf.sprintf "bad integer `%s': %s"
4065 s (Printexc.to_string exn)
4067 state.text <- "";
4068 let te =
4069 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4071 state.mode <- Textentry (te, leave m_prev_mode);
4073 )) :: m_l
4075 method bool ?(offset=1) ?(btos=btos) name get set =
4076 m_l <-
4077 (name, `bool (btos, get), offset, Action (
4078 fun u ->
4079 let v = get () in
4080 set (not v);
4082 )) :: m_l
4084 method color name get set =
4085 m_l <-
4086 (name, `color get, 1, Action (
4087 fun u ->
4088 let invalid = (nan, nan, nan) in
4089 let ondone s =
4090 let c =
4091 try color_of_string s
4092 with exn ->
4093 state.text <- Printf.sprintf "bad color `%s': %s"
4094 s (Printexc.to_string exn);
4095 invalid
4097 if c <> invalid
4098 then set c;
4100 let te = name ^ ": ", "", None, textentry, ondone, true in
4101 state.text <- color_to_string (get ());
4102 state.mode <- Textentry (te, leave m_prev_mode);
4104 )) :: m_l
4106 method string name get set =
4107 m_l <-
4108 (name, `string get, 1, Action (
4109 fun u ->
4110 let ondone s = set s in
4111 let te = name ^ ": ", "", None, textentry, ondone, true in
4112 state.mode <- Textentry (te, leave m_prev_mode);
4114 )) :: m_l
4116 method colorspace name get set =
4117 m_l <-
4118 (name, `string get, 1, Action (
4119 fun _ ->
4120 let source =
4121 let vals = [| "rgb"; "bgr"; "gray" |] in
4122 (object
4123 inherit lvsourcebase
4125 initializer
4126 m_active <- int_of_colorspace conf.colorspace;
4127 m_first <- 0;
4129 method getitemcount = Array.length vals
4130 method getitem n = (vals.(n), 0)
4131 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4132 ignore (uioh, first, pan, qsearch);
4133 if not cancel then set active;
4134 None
4135 method hasaction _ = true
4136 end)
4138 state.text <- "";
4139 let modehash = findkeyhash conf "info" in
4140 coe (new listview ~source ~trusted:true ~modehash)
4141 )) :: m_l
4143 method caption s offset =
4144 m_l <- (s, `empty, offset, Noaction) :: m_l
4146 method caption2 s f offset =
4147 m_l <- (s, `string f, offset, Noaction) :: m_l
4149 method getitemcount = Array.length m_a
4151 method getitem n =
4152 let tostr = function
4153 | `int f -> string_of_int (f ())
4154 | `intws f -> string_with_suffix_of_int (f ())
4155 | `string f -> f ()
4156 | `color f -> color_to_string (f ())
4157 | `bool (btos, f) -> btos (f ())
4158 | `empty -> ""
4160 let name, t, offset, _ = m_a.(n) in
4161 ((let s = tostr t in
4162 if String.length s > 0
4163 then Printf.sprintf "%s\t%s" name s
4164 else name),
4165 offset)
4167 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4168 let uiohopt =
4169 if not cancel
4170 then (
4171 m_qsearch <- qsearch;
4172 let uioh =
4173 match m_a.(active) with
4174 | _, _, _, Action f -> f uioh
4175 | _ -> uioh
4177 Some uioh
4179 else None
4181 m_active <- active;
4182 m_first <- first;
4183 m_pan <- pan;
4184 uiohopt
4186 method hasaction n =
4187 match m_a.(n) with
4188 | _, _, _, Action _ -> true
4189 | _ -> false
4190 end)
4192 let rec fillsrc prevmode prevuioh =
4193 let sep () = src#caption "" 0 in
4194 let colorp name get set =
4195 src#string name
4196 (fun () -> color_to_string (get ()))
4197 (fun v ->
4199 let c = color_of_string v in
4200 set c
4201 with exn ->
4202 state.text <- Printf.sprintf "bad color `%s': %s"
4203 v (Printexc.to_string exn);
4206 let oldmode = state.mode in
4207 let birdseye = isbirdseye state.mode in
4209 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4211 src#bool "presentation mode"
4212 (fun () -> conf.presentation)
4213 (fun v -> setpresentationmode v);
4215 src#bool "ignore case in searches"
4216 (fun () -> conf.icase)
4217 (fun v -> conf.icase <- v);
4219 src#bool "preload"
4220 (fun () -> conf.preload)
4221 (fun v -> conf.preload <- v);
4223 src#bool "highlight links"
4224 (fun () -> conf.hlinks)
4225 (fun v -> conf.hlinks <- v);
4227 src#bool "under info"
4228 (fun () -> conf.underinfo)
4229 (fun v -> conf.underinfo <- v);
4231 src#bool "persistent bookmarks"
4232 (fun () -> conf.savebmarks)
4233 (fun v -> conf.savebmarks <- v);
4235 src#bool "proportional display"
4236 (fun () -> conf.proportional)
4237 (fun v -> reqlayout conf.angle v);
4239 src#bool "trim margins"
4240 (fun () -> conf.trimmargins)
4241 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4243 src#bool "persistent location"
4244 (fun () -> conf.jumpback)
4245 (fun v -> conf.jumpback <- v);
4247 sep ();
4248 src#int "inter-page space"
4249 (fun () -> conf.interpagespace)
4250 (fun n ->
4251 conf.interpagespace <- n;
4252 docolumns conf.columns;
4253 let pageno, py =
4254 match state.layout with
4255 | [] -> 0, 0
4256 | l :: _ ->
4257 l.pageno, l.pagey
4259 state.maxy <- calcheight ();
4260 let y = getpagey pageno in
4261 gotoy (y + py)
4264 src#int "page bias"
4265 (fun () -> conf.pagebias)
4266 (fun v -> conf.pagebias <- v);
4268 src#int "scroll step"
4269 (fun () -> conf.scrollstep)
4270 (fun n -> conf.scrollstep <- n);
4272 src#int "horizontal scroll step"
4273 (fun () -> conf.hscrollstep)
4274 (fun v -> conf.hscrollstep <- v);
4276 src#int "auto scroll step"
4277 (fun () ->
4278 match state.autoscroll with
4279 | Some step -> step
4280 | _ -> conf.autoscrollstep)
4281 (fun n ->
4282 if state.autoscroll <> None
4283 then state.autoscroll <- Some n;
4284 conf.autoscrollstep <- n);
4286 src#int "zoom"
4287 (fun () -> truncate (conf.zoom *. 100.))
4288 (fun v -> setzoom ((float v) /. 100.));
4290 src#int "rotation"
4291 (fun () -> conf.angle)
4292 (fun v -> reqlayout v conf.proportional);
4294 src#int "scroll bar width"
4295 (fun () -> state.scrollw)
4296 (fun v ->
4297 state.scrollw <- v;
4298 conf.scrollbw <- v;
4299 reshape conf.winw conf.winh;
4302 src#int "scroll handle height"
4303 (fun () -> conf.scrollh)
4304 (fun v -> conf.scrollh <- v;);
4306 src#int "thumbnail width"
4307 (fun () -> conf.thumbw)
4308 (fun v ->
4309 conf.thumbw <- min 4096 v;
4310 match oldmode with
4311 | Birdseye beye ->
4312 leavebirdseye beye false;
4313 enterbirdseye ()
4314 | _ -> ()
4317 let mode = state.mode in
4318 src#string "columns"
4319 (fun () ->
4320 match conf.columns with
4321 | Csingle _ -> "1"
4322 | Cmulti (multi, _) -> multicolumns_to_string multi
4323 | Csplit (count, _) -> "-" ^ string_of_int count
4325 (fun v ->
4326 let n, a, b = multicolumns_of_string v in
4327 setcolumns mode n a b);
4329 sep ();
4330 src#caption "Presentation mode" 0;
4331 src#bool "scrollbar visible"
4332 (fun () -> conf.scrollbarinpm)
4333 (fun v ->
4334 if v != conf.scrollbarinpm
4335 then (
4336 conf.scrollbarinpm <- v;
4337 if conf.presentation
4338 then (
4339 state.scrollw <- if v then conf.scrollbw else 0;
4340 reshape conf.winw conf.winh;
4345 sep ();
4346 src#caption "Pixmap cache" 0;
4347 src#int_with_suffix "size (advisory)"
4348 (fun () -> conf.memlimit)
4349 (fun v -> conf.memlimit <- v);
4351 src#caption2 "used"
4352 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4353 (string_with_suffix_of_int state.memused)
4354 (Hashtbl.length state.tilemap)) 1;
4356 sep ();
4357 src#caption "Layout" 0;
4358 src#caption2 "Dimension"
4359 (fun () ->
4360 Printf.sprintf "%dx%d (virtual %dx%d)"
4361 conf.winw conf.winh
4362 state.w state.maxy)
4364 if conf.debug
4365 then
4366 src#caption2 "Position" (fun () ->
4367 Printf.sprintf "%dx%d" state.x state.y
4369 else
4370 src#caption2 "Visible" (fun () -> describe_location ()) 1
4373 sep ();
4374 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4375 "Save these parameters as global defaults at exit"
4376 (fun () -> conf.bedefault)
4377 (fun v -> conf.bedefault <- v)
4380 sep ();
4381 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4382 src#bool ~offset:0 ~btos "Extended parameters"
4383 (fun () -> !showextended)
4384 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4385 if !showextended
4386 then (
4387 src#bool "checkers"
4388 (fun () -> conf.checkers)
4389 (fun v -> conf.checkers <- v; setcheckers v);
4390 src#bool "update cursor"
4391 (fun () -> conf.updatecurs)
4392 (fun v -> conf.updatecurs <- v);
4393 src#bool "verbose"
4394 (fun () -> conf.verbose)
4395 (fun v -> conf.verbose <- v);
4396 src#bool "invert colors"
4397 (fun () -> conf.invert)
4398 (fun v -> conf.invert <- v);
4399 src#bool "max fit"
4400 (fun () -> conf.maxhfit)
4401 (fun v -> conf.maxhfit <- v);
4402 src#bool "redirect stderr"
4403 (fun () -> conf.redirectstderr)
4404 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4405 src#string "uri launcher"
4406 (fun () -> conf.urilauncher)
4407 (fun v -> conf.urilauncher <- v);
4408 src#string "path launcher"
4409 (fun () -> conf.pathlauncher)
4410 (fun v -> conf.pathlauncher <- v);
4411 src#string "tile size"
4412 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4413 (fun v ->
4415 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4416 conf.tilew <- max 64 w;
4417 conf.tileh <- max 64 h;
4418 flushtiles ();
4419 with exn ->
4420 state.text <- Printf.sprintf "bad tile size `%s': %s"
4421 v (Printexc.to_string exn));
4422 src#int "texture count"
4423 (fun () -> conf.texcount)
4424 (fun v ->
4425 if realloctexts v
4426 then conf.texcount <- v
4427 else showtext '!' " Failed to set texture count please retry later"
4429 src#int "slice height"
4430 (fun () -> conf.sliceheight)
4431 (fun v ->
4432 conf.sliceheight <- v;
4433 wcmd "sliceh %d" conf.sliceheight;
4435 src#int "anti-aliasing level"
4436 (fun () -> conf.aalevel)
4437 (fun v ->
4438 conf.aalevel <- bound v 0 8;
4439 state.anchor <- getanchor ();
4440 opendoc state.path state.password;
4442 src#string "page scroll scaling factor"
4443 (fun () -> string_of_float conf.pgscale)
4444 (fun v ->
4446 let s = float_of_string v in
4447 conf.pgscale <- s
4448 with exn ->
4449 state.text <- Printf.sprintf
4450 "bad page scroll scaling factor `%s': %s"
4451 v (Printexc.to_string exn)
4454 src#int "ui font size"
4455 (fun () -> fstate.fontsize)
4456 (fun v -> setfontsize (bound v 5 100));
4457 src#int "hint font size"
4458 (fun () -> conf.hfsize)
4459 (fun v -> conf.hfsize <- bound v 5 100);
4460 colorp "background color"
4461 (fun () -> conf.bgcolor)
4462 (fun v -> conf.bgcolor <- v);
4463 src#bool "crop hack"
4464 (fun () -> conf.crophack)
4465 (fun v -> conf.crophack <- v);
4466 src#string "trim fuzz"
4467 (fun () -> irect_to_string conf.trimfuzz)
4468 (fun v ->
4470 conf.trimfuzz <- irect_of_string v;
4471 if conf.trimmargins
4472 then settrim true conf.trimfuzz;
4473 with exn ->
4474 state.text <- Printf.sprintf "bad irect `%s': %s"
4475 v (Printexc.to_string exn)
4477 src#string "throttle"
4478 (fun () ->
4479 match conf.maxwait with
4480 | None -> "show place holder if page is not ready"
4481 | Some time ->
4482 if time = infinity
4483 then "wait for page to fully render"
4484 else
4485 "wait " ^ string_of_float time
4486 ^ " seconds before showing placeholder"
4488 (fun v ->
4490 let f = float_of_string v in
4491 if f <= 0.0
4492 then conf.maxwait <- None
4493 else conf.maxwait <- Some f
4494 with exn ->
4495 state.text <- Printf.sprintf "bad time `%s': %s"
4496 v (Printexc.to_string exn)
4498 src#string "ghyll scroll"
4499 (fun () ->
4500 match conf.ghyllscroll with
4501 | None -> ""
4502 | Some nab -> ghyllscroll_to_string nab
4504 (fun v ->
4506 let gs =
4507 if String.length v = 0
4508 then None
4509 else Some (ghyllscroll_of_string v)
4511 conf.ghyllscroll <- gs
4512 with exn ->
4513 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4514 v (Printexc.to_string exn)
4516 src#string "selection command"
4517 (fun () -> conf.selcmd)
4518 (fun v -> conf.selcmd <- v);
4519 src#string "synctex command"
4520 (fun () -> conf.stcmd)
4521 (fun v -> conf.stcmd <- v);
4522 src#colorspace "color space"
4523 (fun () -> colorspace_to_string conf.colorspace)
4524 (fun v ->
4525 conf.colorspace <- colorspace_of_int v;
4526 wcmd "cs %d" v;
4527 load state.layout;
4529 if pbousable ()
4530 then
4531 src#bool "use PBO"
4532 (fun () -> conf.usepbo)
4533 (fun v -> conf.usepbo <- v);
4534 src#bool "mouse wheel scrolls pages"
4535 (fun () -> conf.wheelbypage)
4536 (fun v -> conf.wheelbypage <- v);
4539 sep ();
4540 src#caption "Document" 0;
4541 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4542 src#caption2 "Pages"
4543 (fun () -> string_of_int state.pagecount) 1;
4544 src#caption2 "Dimensions"
4545 (fun () -> string_of_int (List.length state.pdims)) 1;
4546 if conf.trimmargins
4547 then (
4548 sep ();
4549 src#caption "Trimmed margins" 0;
4550 src#caption2 "Dimensions"
4551 (fun () -> string_of_int (List.length state.pdims)) 1;
4554 sep ();
4555 src#caption "OpenGL" 0;
4556 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4557 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4558 src#reset prevmode prevuioh;
4560 fun () ->
4561 state.text <- "";
4562 let prevmode = state.mode
4563 and prevuioh = state.uioh in
4564 fillsrc prevmode prevuioh;
4565 let source = (src :> lvsource) in
4566 let modehash = findkeyhash conf "info" in
4567 state.uioh <- coe (object (self)
4568 inherit listview ~source ~trusted:true ~modehash as super
4569 val mutable m_prevmemused = 0
4570 method infochanged = function
4571 | Memused ->
4572 if m_prevmemused != state.memused
4573 then (
4574 m_prevmemused <- state.memused;
4575 G.postRedisplay "memusedchanged";
4577 | Pdim -> G.postRedisplay "pdimchanged"
4578 | Docinfo -> fillsrc prevmode prevuioh
4580 method key key mask =
4581 if not (Wsi.withctrl mask)
4582 then
4583 match key with
4584 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4585 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4586 | _ -> super#key key mask
4587 else super#key key mask
4588 end);
4589 G.postRedisplay "info";
4592 let enterhelpmode =
4593 let source =
4594 (object
4595 inherit lvsourcebase
4596 method getitemcount = Array.length state.help
4597 method getitem n =
4598 let s, l, _ = state.help.(n) in
4599 (s, l)
4601 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4602 let optuioh =
4603 if not cancel
4604 then (
4605 m_qsearch <- qsearch;
4606 match state.help.(active) with
4607 | _, _, Action f -> Some (f uioh)
4608 | _ -> Some (uioh)
4610 else None
4612 m_active <- active;
4613 m_first <- first;
4614 m_pan <- pan;
4615 optuioh
4617 method hasaction n =
4618 match state.help.(n) with
4619 | _, _, Action _ -> true
4620 | _ -> false
4622 initializer
4623 m_active <- -1
4624 end)
4625 in fun () ->
4626 let modehash = findkeyhash conf "help" in
4627 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4628 G.postRedisplay "help";
4631 let entermsgsmode =
4632 let msgsource =
4633 let re = Str.regexp "[\r\n]" in
4634 (object
4635 inherit lvsourcebase
4636 val mutable m_items = [||]
4638 method getitemcount = 1 + Array.length m_items
4640 method getitem n =
4641 if n = 0
4642 then "[Clear]", 0
4643 else m_items.(n-1), 0
4645 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4646 ignore uioh;
4647 if not cancel
4648 then (
4649 if active = 0
4650 then Buffer.clear state.errmsgs;
4651 m_qsearch <- qsearch;
4653 m_active <- active;
4654 m_first <- first;
4655 m_pan <- pan;
4656 None
4658 method hasaction n =
4659 n = 0
4661 method reset =
4662 state.newerrmsgs <- false;
4663 let l = Str.split re (Buffer.contents state.errmsgs) in
4664 m_items <- Array.of_list l
4666 initializer
4667 m_active <- 0
4668 end)
4669 in fun () ->
4670 state.text <- "";
4671 msgsource#reset;
4672 let source = (msgsource :> lvsource) in
4673 let modehash = findkeyhash conf "listview" in
4674 state.uioh <- coe (object
4675 inherit listview ~source ~trusted:false ~modehash as super
4676 method display =
4677 if state.newerrmsgs
4678 then msgsource#reset;
4679 super#display
4680 end);
4681 G.postRedisplay "msgs";
4684 let quickbookmark ?title () =
4685 match state.layout with
4686 | [] -> ()
4687 | l :: _ ->
4688 let title =
4689 match title with
4690 | None ->
4691 let sec = Unix.gettimeofday () in
4692 let tm = Unix.localtime sec in
4693 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4694 (l.pageno+1)
4695 tm.Unix.tm_mday
4696 tm.Unix.tm_mon
4697 (tm.Unix.tm_year + 1900)
4698 tm.Unix.tm_hour
4699 tm.Unix.tm_min
4700 | Some title -> title
4702 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4705 let doreshape w h =
4706 state.fullscreen <- None;
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 conf.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 conf.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 ~-(conf.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 ~-(conf.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 conf.winw conf.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 conf.winw conf.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 begin match state.fullscreen with
5085 | None ->
5086 state.fullscreen <- Some (conf.winw, conf.winh);
5087 Wsi.fullscreen ()
5088 | Some (w, h) ->
5089 state.fullscreen <- None;
5090 doreshape w h
5093 | 112 | 78 -> (* p|N *)
5094 search state.searchpattern false
5096 | 110 | 0xffc0 -> (* n|F3 *)
5097 search state.searchpattern true
5099 | 116 -> (* t *)
5100 begin match state.layout with
5101 | [] -> ()
5102 | l :: _ ->
5103 gotoghyll (getpagey l.pageno)
5106 | 32 -> (* space *)
5107 nextpage ()
5109 | 0xff9f | 0xffff -> (* delete *)
5110 prevpage ()
5112 | 61 -> (* = *)
5113 showtext ' ' (describe_location ());
5115 | 119 -> (* w *)
5116 begin match state.layout with
5117 | [] -> ()
5118 | l :: _ ->
5119 doreshape (l.pagew + state.scrollw) l.pageh;
5120 G.postRedisplay "w"
5123 | 39 -> (* ' *)
5124 enterbookmarkmode ()
5126 | 104 | 0xffbe -> (* h|F1 *)
5127 enterhelpmode ()
5129 | 105 -> (* i *)
5130 enterinfomode ()
5132 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5133 entermsgsmode ()
5135 | 109 -> (* m *)
5136 let ondone s =
5137 match state.layout with
5138 | l :: _ ->
5139 if String.length s > 0
5140 then
5141 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5142 | _ -> ()
5144 enttext ("bookmark: ", "", None, textentry, ondone, true)
5146 | 126 -> (* ~ *)
5147 quickbookmark ();
5148 showtext ' ' "Quick bookmark added";
5150 | 122 -> (* z *)
5151 begin match state.layout with
5152 | l :: _ ->
5153 let rect = getpdimrect l.pagedimno in
5154 let w, h =
5155 if conf.crophack
5156 then
5157 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5158 truncate (1.2 *. (rect.(3) -. rect.(0))))
5159 else
5160 (truncate (rect.(1) -. rect.(0)),
5161 truncate (rect.(3) -. rect.(0)))
5163 let w = truncate ((float w)*.conf.zoom)
5164 and h = truncate ((float h)*.conf.zoom) in
5165 if w != 0 && h != 0
5166 then (
5167 state.anchor <- getanchor ();
5168 doreshape (w + state.scrollw) (h + conf.interpagespace)
5170 G.postRedisplay "z";
5172 | [] -> ()
5175 | 50 when ctrl -> (* ctrl-2 *)
5176 let maxw = getmaxw () in
5177 if maxw > 0.0
5178 then setzoom (maxw /. float conf.winw)
5180 | 60 | 62 -> (* < > *)
5181 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5183 | 91 | 93 -> (* [ ] *)
5184 conf.colorscale <-
5185 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5187 G.postRedisplay "brightness";
5189 | 99 when state.mode = View -> (* c *)
5190 let (c, a, b), z =
5191 match state.prevcolumns with
5192 | None -> (1, 0, 0), 1.0
5193 | Some (columns, z) ->
5194 let cab =
5195 match columns with
5196 | Csplit (c, _) -> -c, 0, 0
5197 | Cmulti ((c, a, b), _) -> c, a, b
5198 | Csingle _ -> 1, 0, 0
5200 cab, z
5202 setcolumns View c a b;
5203 setzoom z;
5205 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5206 setzoom state.prevzoom
5208 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5209 begin match state.autoscroll with
5210 | None ->
5211 begin match state.mode with
5212 | Birdseye beye -> upbirdseye 1 beye
5213 | _ ->
5214 if ctrl
5215 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5216 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5218 | Some n ->
5219 setautoscrollspeed n false
5222 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5223 begin match state.autoscroll with
5224 | None ->
5225 begin match state.mode with
5226 | Birdseye beye -> downbirdseye 1 beye
5227 | _ ->
5228 if ctrl
5229 then gotoy_and_clear_text (clamp (conf.winh/2))
5230 else gotoy_and_clear_text (clamp conf.scrollstep)
5232 | Some n ->
5233 setautoscrollspeed n true
5236 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5237 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5238 if canpan ()
5239 then
5240 let dx =
5241 if ctrl
5242 then conf.winw / 2
5243 else conf.hscrollstep
5245 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5246 state.x <- state.x + dx;
5247 gotoy_and_clear_text state.y
5248 else (
5249 state.text <- "";
5250 G.postRedisplay "lef/right"
5253 | 0xff55 | 0xff9a -> (* (kp) prior *)
5254 let y =
5255 if ctrl
5256 then
5257 match state.layout with
5258 | [] -> state.y
5259 | l :: _ -> state.y - l.pagey
5260 else
5261 clamp (pgscale (-conf.winh))
5263 gotoghyll y
5265 | 0xff56 | 0xff9b -> (* (kp) next *)
5266 let y =
5267 if ctrl
5268 then
5269 match List.rev state.layout with
5270 | [] -> state.y
5271 | l :: _ -> getpagey l.pageno
5272 else
5273 clamp (pgscale conf.winh)
5275 gotoghyll y
5277 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5278 gotoghyll 0
5279 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5280 gotoghyll (clamp state.maxy)
5282 | 0xff53 | 0xff98
5283 when Wsi.withalt mask -> (* alt-(kp) right *)
5284 gotoghyll (getnav 1)
5285 | 0xff51 | 0xff96
5286 when Wsi.withalt mask -> (* alt-(kp) left *)
5287 gotoghyll (getnav ~-1)
5289 | 114 -> (* r *)
5290 reload ()
5292 | 118 when conf.debug -> (* v *)
5293 state.rects <- [];
5294 List.iter (fun l ->
5295 match getopaque l.pageno with
5296 | None -> ()
5297 | Some opaque ->
5298 let x0, y0, x1, y1 = pagebbox opaque in
5299 let a,b = float x0, float y0 in
5300 let c,d = float x1, float y0 in
5301 let e,f = float x1, float y1 in
5302 let h,j = float x0, float y1 in
5303 let rect = (a,b,c,d,e,f,h,j) in
5304 debugrect rect;
5305 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5306 ) state.layout;
5307 G.postRedisplay "v";
5309 | _ ->
5310 vlog "huh? %s" (Wsi.keyname key)
5313 let linknavkeyboard key mask linknav =
5314 let getpage pageno =
5315 let rec loop = function
5316 | [] -> None
5317 | l :: _ when l.pageno = pageno -> Some l
5318 | _ :: rest -> loop rest
5319 in loop state.layout
5321 let doexact (pageno, n) =
5322 match getopaque pageno, getpage pageno with
5323 | Some opaque, Some l ->
5324 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5325 then
5326 let under = getlink opaque n in
5327 G.postRedisplay "link gotounder";
5328 gotounder under;
5329 state.mode <- View;
5330 else
5331 let opt, dir =
5332 match key with
5333 | 0xff50 -> (* home *)
5334 Some (findlink opaque LDfirst), -1
5336 | 0xff57 -> (* end *)
5337 Some (findlink opaque LDlast), 1
5339 | 0xff51 -> (* left *)
5340 Some (findlink opaque (LDleft n)), -1
5342 | 0xff53 -> (* right *)
5343 Some (findlink opaque (LDright n)), 1
5345 | 0xff52 -> (* up *)
5346 Some (findlink opaque (LDup n)), -1
5348 | 0xff54 -> (* down *)
5349 Some (findlink opaque (LDdown n)), 1
5351 | _ -> None, 0
5353 let pwl l dir =
5354 begin match findpwl l.pageno dir with
5355 | Pwlnotfound -> ()
5356 | Pwl pageno ->
5357 let notfound dir =
5358 state.mode <- LinkNav (Ltgendir dir);
5359 let y, h = getpageyh pageno in
5360 let y =
5361 if dir < 0
5362 then y + h - conf.winh
5363 else y
5365 gotoy y
5367 begin match getopaque pageno, getpage pageno with
5368 | Some opaque, Some _ ->
5369 let link =
5370 let ld = if dir > 0 then LDfirst else LDlast in
5371 findlink opaque ld
5373 begin match link with
5374 | Lfound m ->
5375 showlinktype (getlink opaque m);
5376 state.mode <- LinkNav (Ltexact (pageno, m));
5377 G.postRedisplay "linknav jpage";
5378 | _ -> notfound dir
5379 end;
5380 | _ -> notfound dir
5381 end;
5382 end;
5384 begin match opt with
5385 | Some Lnotfound -> pwl l dir;
5386 | Some (Lfound m) ->
5387 if m = n
5388 then pwl l dir
5389 else (
5390 let _, y0, _, y1 = getlinkrect opaque m in
5391 if y0 < l.pagey
5392 then gotopage1 l.pageno y0
5393 else (
5394 let d = fstate.fontsize + 1 in
5395 if y1 - l.pagey > l.pagevh - d
5396 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5397 else G.postRedisplay "linknav";
5399 showlinktype (getlink opaque m);
5400 state.mode <- LinkNav (Ltexact (l.pageno, m));
5403 | None -> viewkeyboard key mask
5404 end;
5405 | _ -> viewkeyboard key mask
5407 if key = 0xff63
5408 then (
5409 state.mode <- View;
5410 G.postRedisplay "leave linknav"
5412 else
5413 match linknav with
5414 | Ltgendir _ -> viewkeyboard key mask
5415 | Ltexact exact -> doexact exact
5418 let keyboard key mask =
5419 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5420 then wcmd "interrupt"
5421 else state.uioh <- state.uioh#key key mask
5424 let birdseyekeyboard key mask
5425 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5426 let incr =
5427 match conf.columns with
5428 | Csingle _ -> 1
5429 | Cmulti ((c, _, _), _) -> c
5430 | Csplit _ -> failwith "bird's eye split mode"
5432 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5433 match key with
5434 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5435 let y, h = getpageyh pageno in
5436 let top = (conf.winh - h) / 2 in
5437 gotoy (max 0 (y - top))
5438 | 0xff0d (* enter *)
5439 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5440 | 0xff1b -> leavebirdseye beye true (* escape *)
5441 | 0xff52 -> upbirdseye incr beye (* up *)
5442 | 0xff54 -> downbirdseye incr beye (* down *)
5443 | 0xff51 -> upbirdseye 1 beye (* left *)
5444 | 0xff53 -> downbirdseye 1 beye (* right *)
5446 | 0xff55 -> (* prior *)
5447 begin match state.layout with
5448 | l :: _ ->
5449 if l.pagey != 0
5450 then (
5451 state.mode <- Birdseye (
5452 oconf, leftx, l.pageno, hooverpageno, anchor
5454 gotopage1 l.pageno 0;
5456 else (
5457 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5458 match layout with
5459 | [] -> gotoy (clamp (-conf.winh))
5460 | l :: _ ->
5461 state.mode <- Birdseye (
5462 oconf, leftx, l.pageno, hooverpageno, anchor
5464 gotopage1 l.pageno 0
5467 | [] -> gotoy (clamp (-conf.winh))
5468 end;
5470 | 0xff56 -> (* next *)
5471 begin match List.rev state.layout with
5472 | l :: _ ->
5473 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5474 begin match layout with
5475 | [] ->
5476 let incr = l.pageh - l.pagevh in
5477 if incr = 0
5478 then (
5479 state.mode <-
5480 Birdseye (
5481 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5483 G.postRedisplay "birdseye pagedown";
5485 else gotoy (clamp (incr + conf.interpagespace*2));
5487 | l :: _ ->
5488 state.mode <-
5489 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5490 gotopage1 l.pageno 0;
5493 | [] -> gotoy (clamp conf.winh)
5494 end;
5496 | 0xff50 -> (* home *)
5497 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5498 gotopage1 0 0
5500 | 0xff57 -> (* end *)
5501 let pageno = state.pagecount - 1 in
5502 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5503 if not (pagevisible state.layout pageno)
5504 then
5505 let h =
5506 match List.rev state.pdims with
5507 | [] -> conf.winh
5508 | (_, _, h, _) :: _ -> h
5510 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5511 else G.postRedisplay "birdseye end";
5512 | _ -> viewkeyboard key mask
5515 let drawpage l linkindexbase =
5516 let color =
5517 match state.mode with
5518 | Textentry _ -> scalecolor 0.4
5519 | LinkNav _
5520 | View -> scalecolor 1.0
5521 | Birdseye (_, _, pageno, hooverpageno, _) ->
5522 if l.pageno = hooverpageno
5523 then scalecolor 0.9
5524 else (
5525 if l.pageno = pageno
5526 then scalecolor 1.0
5527 else scalecolor 0.8
5530 drawtiles l color;
5531 begin match getopaque l.pageno with
5532 | Some opaque ->
5533 if tileready l l.pagex l.pagey
5534 then
5535 let x = l.pagedispx - l.pagex
5536 and y = l.pagedispy - l.pagey in
5537 let hlmask =
5538 match conf.columns with
5539 | Csingle _ | Cmulti _ ->
5540 (if conf.hlinks then 1 else 0)
5541 + (if state.glinks
5542 && not (isbirdseye state.mode) then 2 else 0)
5543 | _ -> 0
5545 let s =
5546 match state.mode with
5547 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5548 | _ -> ""
5550 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5551 else 0
5553 | _ -> 0
5554 end;
5557 let scrollindicator () =
5558 let sbw, ph, sh = state.uioh#scrollph in
5559 let sbh, pw, sw = state.uioh#scrollpw in
5561 GlDraw.color (0.64, 0.64, 0.64);
5562 GlDraw.rect
5563 (float (conf.winw - sbw), 0.)
5564 (float conf.winw, float conf.winh)
5566 GlDraw.rect
5567 (0., float (conf.winh - sbh))
5568 (float (conf.winw - state.scrollw - 1), float conf.winh)
5570 GlDraw.color (0.0, 0.0, 0.0);
5572 GlDraw.rect
5573 (float (conf.winw - sbw), ph)
5574 (float conf.winw, ph +. sh)
5576 GlDraw.rect
5577 (pw, float (conf.winh - sbh))
5578 (pw +. sw, float conf.winh)
5582 let showsel () =
5583 match state.mstate with
5584 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5587 | Msel ((x0, y0), (x1, y1)) ->
5588 let rec loop = function
5589 | l :: ls ->
5590 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5591 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5592 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5593 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5594 then
5595 match getopaque l.pageno with
5596 | Some opaque ->
5597 let x0, y0 = pagetranslatepoint l x0 y0 in
5598 let x1, y1 = pagetranslatepoint l x1 y1 in
5599 seltext opaque (x0, y0, x1, y1);
5600 | _ -> ()
5601 else loop ls
5602 | [] -> ()
5604 loop state.layout
5607 let showrects rects =
5608 Gl.enable `blend;
5609 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5610 GlDraw.polygon_mode `both `fill;
5611 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5612 List.iter
5613 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5614 List.iter (fun l ->
5615 if l.pageno = pageno
5616 then (
5617 let dx = float (l.pagedispx - l.pagex) in
5618 let dy = float (l.pagedispy - l.pagey) in
5619 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5620 GlDraw.begins `quads;
5622 GlDraw.vertex2 (x0+.dx, y0+.dy);
5623 GlDraw.vertex2 (x1+.dx, y1+.dy);
5624 GlDraw.vertex2 (x2+.dx, y2+.dy);
5625 GlDraw.vertex2 (x3+.dx, y3+.dy);
5627 GlDraw.ends ();
5629 ) state.layout
5630 ) rects
5632 Gl.disable `blend;
5635 let display () =
5636 GlClear.color (scalecolor2 conf.bgcolor);
5637 GlClear.clear [`color];
5638 let rec loop linkindexbase = function
5639 | l :: rest ->
5640 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5641 loop linkindexbase rest
5642 | [] -> ()
5644 loop 0 state.layout;
5645 let rects =
5646 match state.mode with
5647 | LinkNav (Ltexact (pageno, linkno)) ->
5648 begin match getopaque pageno with
5649 | Some opaque ->
5650 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5651 (pageno, 5, (
5652 float x0, float y0,
5653 float x1, float y0,
5654 float x1, float y1,
5655 float x0, float y1)
5656 ) :: state.rects
5657 | None -> state.rects
5659 | _ -> state.rects
5661 showrects rects;
5662 showsel ();
5663 state.uioh#display;
5664 begin match state.mstate with
5665 | Mzoomrect ((x0, y0), (x1, y1)) ->
5666 Gl.enable `blend;
5667 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5668 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5669 GlDraw.rect (float x0, float y0)
5670 (float x1, float y1);
5671 Gl.disable `blend;
5672 | _ -> ()
5673 end;
5674 enttext ();
5675 scrollindicator ();
5676 Wsi.swapb ();
5679 let zoomrect x y x1 y1 =
5680 let x0 = min x x1
5681 and x1 = max x x1
5682 and y0 = min y y1 in
5683 gotoy (state.y + y0);
5684 state.anchor <- getanchor ();
5685 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5686 let margin =
5687 if state.w < conf.winw - state.scrollw
5688 then (conf.winw - state.scrollw - state.w) / 2
5689 else 0
5691 state.x <- (state.x + margin) - x0;
5692 setzoom zoom;
5693 Wsi.setcursor Wsi.CURSOR_INHERIT;
5694 state.mstate <- Mnone;
5697 let scrollx x =
5698 let winw = conf.winw - state.scrollw - 1 in
5699 let s = float x /. float winw in
5700 let destx = truncate (float (state.w + winw) *. s) in
5701 state.x <- winw - destx;
5702 gotoy_and_clear_text state.y;
5703 state.mstate <- Mscrollx;
5706 let scrolly y =
5707 let s = float y /. float conf.winh in
5708 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5709 gotoy_and_clear_text desty;
5710 state.mstate <- Mscrolly;
5713 let viewmouse button down x y mask =
5714 match button with
5715 | n when (n == 4 || n == 5) && not down ->
5716 if Wsi.withctrl mask
5717 then (
5718 match state.mstate with
5719 | Mzoom (oldn, i) ->
5720 if oldn = n
5721 then (
5722 if i = 2
5723 then
5724 let incr =
5725 match n with
5726 | 5 ->
5727 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5728 | _ ->
5729 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5731 let zoom = conf.zoom -. incr in
5732 setzoom zoom;
5733 state.mstate <- Mzoom (n, 0);
5734 else
5735 state.mstate <- Mzoom (n, i+1);
5737 else state.mstate <- Mzoom (n, 0)
5739 | _ -> state.mstate <- Mzoom (n, 0)
5741 else (
5742 match state.autoscroll with
5743 | Some step -> setautoscrollspeed step (n=4)
5744 | None ->
5745 if conf.wheelbypage
5746 then (
5747 if n = 4
5748 then prevpage ()
5749 else nextpage ()
5751 else
5752 let incr =
5753 if n = 4
5754 then -conf.scrollstep
5755 else conf.scrollstep
5757 let incr = incr * 2 in
5758 let y = clamp incr in
5759 gotoy_and_clear_text y
5762 | n when (n = 6 || n = 7) && not down && canpan () ->
5763 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5764 gotoy_and_clear_text state.y
5766 | 1 when Wsi.withshift mask ->
5767 state.mstate <- Mnone;
5768 if not down then (
5769 match unproject x y with
5770 | Some (pageno, ux, uy) ->
5771 let cmd = Printf.sprintf
5772 "%s %s %d %d %d"
5773 conf.stcmd state.path pageno ux uy
5775 popen cmd []
5776 | None -> ()
5779 | 1 when Wsi.withctrl mask ->
5780 if down
5781 then (
5782 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5783 state.mstate <- Mpan (x, y)
5785 else
5786 state.mstate <- Mnone
5788 | 3 ->
5789 if down
5790 then (
5791 Wsi.setcursor Wsi.CURSOR_CYCLE;
5792 let p = (x, y) in
5793 state.mstate <- Mzoomrect (p, p)
5795 else (
5796 match state.mstate with
5797 | Mzoomrect ((x0, y0), _) ->
5798 if abs (x-x0) > 10 && abs (y - y0) > 10
5799 then zoomrect x0 y0 x y
5800 else (
5801 state.mstate <- Mnone;
5802 Wsi.setcursor Wsi.CURSOR_INHERIT;
5803 G.postRedisplay "kill accidental zoom rect";
5805 | _ ->
5806 Wsi.setcursor Wsi.CURSOR_INHERIT;
5807 state.mstate <- Mnone
5810 | 1 when x > conf.winw - state.scrollw ->
5811 if down
5812 then
5813 let _, position, sh = state.uioh#scrollph in
5814 if y > truncate position && y < truncate (position +. sh)
5815 then state.mstate <- Mscrolly
5816 else scrolly y
5817 else
5818 state.mstate <- Mnone
5820 | 1 when y > conf.winh - state.hscrollh ->
5821 if down
5822 then
5823 let _, position, sw = state.uioh#scrollpw in
5824 if x > truncate position && x < truncate (position +. sw)
5825 then state.mstate <- Mscrollx
5826 else scrollx x
5827 else
5828 state.mstate <- Mnone
5830 | 1 ->
5831 let dest = if down then getunder x y else Unone in
5832 begin match dest with
5833 | Ulinkgoto _
5834 | Ulinkuri _
5835 | Uremote _
5836 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5837 gotounder dest
5839 | Unone when down ->
5840 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5841 state.mstate <- Mpan (x, y);
5843 | Unone | Utext _ ->
5844 if down
5845 then (
5846 if conf.angle mod 360 = 0
5847 then (
5848 state.mstate <- Msel ((x, y), (x, y));
5849 G.postRedisplay "mouse select";
5852 else (
5853 match state.mstate with
5854 | Mnone -> ()
5856 | Mzoom _ | Mscrollx | Mscrolly ->
5857 state.mstate <- Mnone
5859 | Mzoomrect ((x0, y0), _) ->
5860 zoomrect x0 y0 x y
5862 | Mpan _ ->
5863 Wsi.setcursor Wsi.CURSOR_INHERIT;
5864 state.mstate <- Mnone
5866 | Msel ((x0, y0), (x1, y1)) ->
5867 let rec loop = function
5868 | [] -> ()
5869 | l :: rest ->
5870 let inside =
5871 let a0 = l.pagedispy in
5872 let a1 = a0 + l.pagevh in
5873 let b0 = l.pagedispx in
5874 let b1 = b0 + l.pagevw in
5875 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5876 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5878 if inside
5879 then
5880 match getopaque l.pageno with
5881 | Some opaque ->
5882 begin
5883 match Ne.pipe () with
5884 | Ne.Exn exn ->
5885 showtext '!'
5886 (Printf.sprintf
5887 "can not create sel pipe: %s"
5888 (Printexc.to_string exn));
5889 | Ne.Res (r, w) ->
5890 let doclose what fd =
5891 Ne.clo fd (fun msg ->
5892 dolog "%s close failed: %s" what msg)
5895 popen conf.selcmd [r, 0; w, -1];
5896 copysel w opaque;
5897 doclose "pipe/r" r;
5898 G.postRedisplay "copysel";
5899 with exn ->
5900 dolog "can not execute %S: %s"
5901 conf.selcmd (Printexc.to_string exn);
5902 doclose "pipe/r" r;
5903 doclose "pipe/w" w;
5905 | None -> ()
5906 else loop rest
5908 loop state.layout;
5909 Wsi.setcursor Wsi.CURSOR_INHERIT;
5910 state.mstate <- Mnone;
5914 | _ -> ()
5917 let birdseyemouse button down x y mask
5918 (conf, leftx, _, hooverpageno, anchor) =
5919 match button with
5920 | 1 when down ->
5921 let rec loop = function
5922 | [] -> ()
5923 | l :: rest ->
5924 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5925 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5926 then (
5927 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5929 else loop rest
5931 loop state.layout
5932 | 3 -> ()
5933 | _ -> viewmouse button down x y mask
5936 let mouse button down x y mask =
5937 state.uioh <- state.uioh#button button down x y mask;
5940 let motion ~x ~y =
5941 state.uioh <- state.uioh#motion x y
5944 let pmotion ~x ~y =
5945 state.uioh <- state.uioh#pmotion x y;
5948 let uioh = object
5949 method display = ()
5951 method key key mask =
5952 begin match state.mode with
5953 | Textentry textentry -> textentrykeyboard key mask textentry
5954 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5955 | View -> viewkeyboard key mask
5956 | LinkNav linknav -> linknavkeyboard key mask linknav
5957 end;
5958 state.uioh
5960 method button button bstate x y mask =
5961 begin match state.mode with
5962 | LinkNav _
5963 | View -> viewmouse button bstate x y mask
5964 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5965 | Textentry _ -> ()
5966 end;
5967 state.uioh
5969 method motion x y =
5970 begin match state.mode with
5971 | Textentry _ -> ()
5972 | View | Birdseye _ | LinkNav _ ->
5973 match state.mstate with
5974 | Mzoom _ | Mnone -> ()
5976 | Mpan (x0, y0) ->
5977 let dx = x - x0
5978 and dy = y0 - y in
5979 state.mstate <- Mpan (x, y);
5980 if canpan ()
5981 then state.x <- state.x + dx;
5982 let y = clamp dy in
5983 gotoy_and_clear_text y
5985 | Msel (a, _) ->
5986 state.mstate <- Msel (a, (x, y));
5987 G.postRedisplay "motion select";
5989 | Mscrolly ->
5990 let y = min conf.winh (max 0 y) in
5991 scrolly y
5993 | Mscrollx ->
5994 let x = min conf.winw (max 0 x) in
5995 scrollx x
5997 | Mzoomrect (p0, _) ->
5998 state.mstate <- Mzoomrect (p0, (x, y));
5999 G.postRedisplay "motion zoomrect";
6000 end;
6001 state.uioh
6003 method pmotion x y =
6004 begin match state.mode with
6005 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6006 let rec loop = function
6007 | [] ->
6008 if hooverpageno != -1
6009 then (
6010 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6011 G.postRedisplay "pmotion birdseye no hoover";
6013 | l :: rest ->
6014 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6015 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6016 then (
6017 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6018 G.postRedisplay "pmotion birdseye hoover";
6020 else loop rest
6022 loop state.layout
6024 | Textentry _ -> ()
6026 | LinkNav _
6027 | View ->
6028 match state.mstate with
6029 | Mnone -> updateunder x y
6030 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6032 end;
6033 state.uioh
6035 method infochanged _ = ()
6037 method scrollph =
6038 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
6039 let p, h = scrollph state.y maxy in
6040 state.scrollw, p, h
6042 method scrollpw =
6043 let winw = conf.winw - state.scrollw - 1 in
6044 let fwinw = float winw in
6045 let sw =
6046 let sw = fwinw /. float state.w in
6047 let sw = fwinw *. sw in
6048 max sw (float conf.scrollh)
6050 let position, sw =
6051 let f = state.w+winw in
6052 let r = float (winw-state.x) /. float f in
6053 let p = fwinw *. r in
6054 p-.sw/.2., sw
6056 let sw =
6057 if position +. sw > fwinw
6058 then fwinw -. position
6059 else sw
6061 state.hscrollh, position, sw
6063 method modehash =
6064 let modename =
6065 match state.mode with
6066 | LinkNav _ -> "links"
6067 | Textentry _ -> "textentry"
6068 | Birdseye _ -> "birdseye"
6069 | View -> "view"
6071 findkeyhash conf modename
6072 end;;
6074 module Config =
6075 struct
6076 open Parser
6078 let fontpath = ref "";;
6080 module KeyMap =
6081 Map.Make (struct type t = (int * int) let compare = compare end);;
6083 let unent s =
6084 let l = String.length s in
6085 let b = Buffer.create l in
6086 unent b s 0 l;
6087 Buffer.contents b;
6090 let home =
6091 try Sys.getenv "HOME"
6092 with exn ->
6093 prerr_endline
6094 ("Can not determine home directory location: " ^
6095 Printexc.to_string exn);
6099 let modifier_of_string = function
6100 | "alt" -> Wsi.altmask
6101 | "shift" -> Wsi.shiftmask
6102 | "ctrl" | "control" -> Wsi.ctrlmask
6103 | "meta" -> Wsi.metamask
6104 | _ -> 0
6107 let key_of_string =
6108 let r = Str.regexp "-" in
6109 fun s ->
6110 let elems = Str.full_split r s in
6111 let f n k m =
6112 let g s =
6113 let m1 = modifier_of_string s in
6114 if m1 = 0
6115 then (Wsi.namekey s, m)
6116 else (k, m lor m1)
6117 in function
6118 | Str.Delim s when n land 1 = 0 -> g s
6119 | Str.Text s -> g s
6120 | Str.Delim _ -> (k, m)
6122 let rec loop n k m = function
6123 | [] -> (k, m)
6124 | x :: xs ->
6125 let k, m = f n k m x in
6126 loop (n+1) k m xs
6128 loop 0 0 0 elems
6131 let keys_of_string =
6132 let r = Str.regexp "[ \t]" in
6133 fun s ->
6134 let elems = Str.split r s in
6135 List.map key_of_string elems
6138 let copykeyhashes c =
6139 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6142 let config_of c attrs =
6143 let apply c k v =
6145 match k with
6146 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6147 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6148 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6149 | "preload" -> { c with preload = bool_of_string v }
6150 | "page-bias" -> { c with pagebias = int_of_string v }
6151 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6152 | "horizontal-scroll-step" ->
6153 { c with hscrollstep = max (int_of_string v) 1 }
6154 | "auto-scroll-step" ->
6155 { c with autoscrollstep = max 0 (int_of_string v) }
6156 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6157 | "crop-hack" -> { c with crophack = bool_of_string v }
6158 | "throttle" ->
6159 let mw =
6160 match String.lowercase v with
6161 | "true" -> Some infinity
6162 | "false" -> None
6163 | f -> Some (float_of_string f)
6165 { c with maxwait = mw}
6166 | "highlight-links" -> { c with hlinks = bool_of_string v }
6167 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6168 | "vertical-margin" ->
6169 { c with interpagespace = max 0 (int_of_string v) }
6170 | "zoom" ->
6171 let zoom = float_of_string v /. 100. in
6172 let zoom = max zoom 0.0 in
6173 { c with zoom = zoom }
6174 | "presentation" -> { c with presentation = bool_of_string v }
6175 | "rotation-angle" -> { c with angle = int_of_string v }
6176 | "width" -> { c with winw = max 20 (int_of_string v) }
6177 | "height" -> { c with winh = max 20 (int_of_string v) }
6178 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6179 | "proportional-display" -> { c with proportional = bool_of_string v }
6180 | "pixmap-cache-size" ->
6181 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6182 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6183 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6184 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6185 | "persistent-location" -> { c with jumpback = bool_of_string v }
6186 | "background-color" -> { c with bgcolor = color_of_string v }
6187 | "scrollbar-in-presentation" ->
6188 { c with scrollbarinpm = bool_of_string v }
6189 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6190 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6191 | "mupdf-store-size" ->
6192 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6193 | "checkers" -> { c with checkers = bool_of_string v }
6194 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6195 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6196 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6197 | "uri-launcher" -> { c with urilauncher = unent v }
6198 | "path-launcher" -> { c with pathlauncher = unent v }
6199 | "color-space" -> { c with colorspace = colorspace_of_string v }
6200 | "invert-colors" -> { c with invert = bool_of_string v }
6201 | "brightness" -> { c with colorscale = float_of_string v }
6202 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6203 | "ghyllscroll" ->
6204 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6205 | "columns" ->
6206 let (n, _, _) as nab = multicolumns_of_string v in
6207 if n < 0
6208 then { c with columns = Csplit (-n, [||]) }
6209 else { c with columns = Cmulti (nab, [||]) }
6210 | "birds-eye-columns" ->
6211 { c with beyecolumns = Some (max (int_of_string v) 2) }
6212 | "selection-command" -> { c with selcmd = unent v }
6213 | "synctex-command" -> { c with stcmd = unent v }
6214 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6215 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6216 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6217 | "use-pbo" -> { c with usepbo = bool_of_string v }
6218 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6219 | _ -> c
6220 with exn ->
6221 prerr_endline ("Error processing attribute (`" ^
6222 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6225 let rec fold c = function
6226 | [] -> c
6227 | (k, v) :: rest ->
6228 let c = apply c k v in
6229 fold c rest
6231 fold { c with keyhashes = copykeyhashes c } attrs;
6234 let fromstring f pos n v d =
6235 try f v
6236 with exn ->
6237 dolog "Error processing attribute (%S=%S) at %d\n%s"
6238 n v pos (Printexc.to_string exn)
6243 let bookmark_of attrs =
6244 let rec fold title page rely visy = function
6245 | ("title", v) :: rest -> fold v page rely visy rest
6246 | ("page", v) :: rest -> fold title v rely visy rest
6247 | ("rely", v) :: rest -> fold title page v visy rest
6248 | ("visy", v) :: rest -> fold title page rely v rest
6249 | _ :: rest -> fold title page rely visy rest
6250 | [] -> title, page, rely, visy
6252 fold "invalid" "0" "0" "0" attrs
6255 let doc_of attrs =
6256 let rec fold path page rely pan visy = function
6257 | ("path", v) :: rest -> fold v page rely pan visy rest
6258 | ("page", v) :: rest -> fold path v rely pan visy rest
6259 | ("rely", v) :: rest -> fold path page v pan visy rest
6260 | ("pan", v) :: rest -> fold path page rely v visy rest
6261 | ("visy", v) :: rest -> fold path page rely pan v rest
6262 | _ :: rest -> fold path page rely pan visy rest
6263 | [] -> path, page, rely, pan, visy
6265 fold "" "0" "0" "0" "0" attrs
6268 let map_of attrs =
6269 let rec fold rs ls = function
6270 | ("out", v) :: rest -> fold v ls rest
6271 | ("in", v) :: rest -> fold rs v rest
6272 | _ :: rest -> fold ls rs rest
6273 | [] -> ls, rs
6275 fold "" "" attrs
6278 let setconf dst src =
6279 dst.scrollbw <- src.scrollbw;
6280 dst.scrollh <- src.scrollh;
6281 dst.icase <- src.icase;
6282 dst.preload <- src.preload;
6283 dst.pagebias <- src.pagebias;
6284 dst.verbose <- src.verbose;
6285 dst.scrollstep <- src.scrollstep;
6286 dst.maxhfit <- src.maxhfit;
6287 dst.crophack <- src.crophack;
6288 dst.autoscrollstep <- src.autoscrollstep;
6289 dst.maxwait <- src.maxwait;
6290 dst.hlinks <- src.hlinks;
6291 dst.underinfo <- src.underinfo;
6292 dst.interpagespace <- src.interpagespace;
6293 dst.zoom <- src.zoom;
6294 dst.presentation <- src.presentation;
6295 dst.angle <- src.angle;
6296 dst.winw <- src.winw;
6297 dst.winh <- src.winh;
6298 dst.savebmarks <- src.savebmarks;
6299 dst.memlimit <- src.memlimit;
6300 dst.proportional <- src.proportional;
6301 dst.texcount <- src.texcount;
6302 dst.sliceheight <- src.sliceheight;
6303 dst.thumbw <- src.thumbw;
6304 dst.jumpback <- src.jumpback;
6305 dst.bgcolor <- src.bgcolor;
6306 dst.scrollbarinpm <- src.scrollbarinpm;
6307 dst.tilew <- src.tilew;
6308 dst.tileh <- src.tileh;
6309 dst.mustoresize <- src.mustoresize;
6310 dst.checkers <- src.checkers;
6311 dst.aalevel <- src.aalevel;
6312 dst.trimmargins <- src.trimmargins;
6313 dst.trimfuzz <- src.trimfuzz;
6314 dst.urilauncher <- src.urilauncher;
6315 dst.colorspace <- src.colorspace;
6316 dst.invert <- src.invert;
6317 dst.colorscale <- src.colorscale;
6318 dst.redirectstderr <- src.redirectstderr;
6319 dst.ghyllscroll <- src.ghyllscroll;
6320 dst.columns <- src.columns;
6321 dst.beyecolumns <- src.beyecolumns;
6322 dst.selcmd <- src.selcmd;
6323 dst.updatecurs <- src.updatecurs;
6324 dst.pathlauncher <- src.pathlauncher;
6325 dst.keyhashes <- copykeyhashes src;
6326 dst.hfsize <- src.hfsize;
6327 dst.hscrollstep <- src.hscrollstep;
6328 dst.pgscale <- src.pgscale;
6329 dst.usepbo <- src.usepbo;
6330 dst.wheelbypage <- src.wheelbypage;
6331 dst.stcmd <- src.stcmd;
6334 let get s =
6335 let h = Hashtbl.create 10 in
6336 let dc = { defconf with angle = defconf.angle } in
6337 let rec toplevel v t spos _ =
6338 match t with
6339 | Vdata | Vcdata | Vend -> v
6340 | Vopen ("llppconfig", _, closed) ->
6341 if closed
6342 then v
6343 else { v with f = llppconfig }
6344 | Vopen _ ->
6345 error "unexpected subelement at top level" s spos
6346 | Vclose _ -> error "unexpected close at top level" s spos
6348 and llppconfig v t spos _ =
6349 match t with
6350 | Vdata | Vcdata -> v
6351 | Vend -> error "unexpected end of input in llppconfig" s spos
6352 | Vopen ("defaults", attrs, closed) ->
6353 let c = config_of dc attrs in
6354 setconf dc c;
6355 if closed
6356 then v
6357 else { v with f = defaults }
6359 | Vopen ("ui-font", attrs, closed) ->
6360 let rec getsize size = function
6361 | [] -> size
6362 | ("size", v) :: rest ->
6363 let size =
6364 fromstring int_of_string spos "size" v fstate.fontsize in
6365 getsize size rest
6366 | l -> getsize size l
6368 fstate.fontsize <- getsize fstate.fontsize attrs;
6369 if closed
6370 then v
6371 else { v with f = uifont (Buffer.create 10) }
6373 | Vopen ("doc", attrs, closed) ->
6374 let pathent, spage, srely, span, svisy = doc_of attrs in
6375 let path = unent pathent
6376 and pageno = fromstring int_of_string spos "page" spage 0
6377 and rely = fromstring float_of_string spos "rely" srely 0.0
6378 and pan = fromstring int_of_string spos "pan" span 0
6379 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6380 let c = config_of dc attrs in
6381 let anchor = (pageno, rely, visy) in
6382 if closed
6383 then (Hashtbl.add h path (c, [], pan, anchor); v)
6384 else { v with f = doc path pan anchor c [] }
6386 | Vopen _ ->
6387 error "unexpected subelement in llppconfig" s spos
6389 | Vclose "llppconfig" -> { v with f = toplevel }
6390 | Vclose _ -> error "unexpected close in llppconfig" s spos
6392 and defaults v t spos _ =
6393 match t with
6394 | Vdata | Vcdata -> v
6395 | Vend -> error "unexpected end of input in defaults" s spos
6396 | Vopen ("keymap", attrs, closed) ->
6397 let modename =
6398 try List.assoc "mode" attrs
6399 with Not_found -> "global" in
6400 if closed
6401 then v
6402 else
6403 let ret keymap =
6404 let h = findkeyhash dc modename in
6405 KeyMap.iter (Hashtbl.replace h) keymap;
6406 defaults
6408 { v with f = pkeymap ret KeyMap.empty }
6410 | Vopen (_, _, _) ->
6411 error "unexpected subelement in defaults" s spos
6413 | Vclose "defaults" ->
6414 { v with f = llppconfig }
6416 | Vclose _ -> error "unexpected close in defaults" s spos
6418 and uifont b v t spos epos =
6419 match t with
6420 | Vdata | Vcdata ->
6421 Buffer.add_substring b s spos (epos - spos);
6423 | Vopen (_, _, _) ->
6424 error "unexpected subelement in ui-font" s spos
6425 | Vclose "ui-font" ->
6426 if String.length !fontpath = 0
6427 then fontpath := Buffer.contents b;
6428 { v with f = llppconfig }
6429 | Vclose _ -> error "unexpected close in ui-font" s spos
6430 | Vend -> error "unexpected end of input in ui-font" s spos
6432 and doc path pan anchor c bookmarks v t spos _ =
6433 match t with
6434 | Vdata | Vcdata -> v
6435 | Vend -> error "unexpected end of input in doc" s spos
6436 | Vopen ("bookmarks", _, closed) ->
6437 if closed
6438 then v
6439 else { v with f = pbookmarks path pan anchor c bookmarks }
6441 | Vopen ("keymap", attrs, closed) ->
6442 let modename =
6443 try List.assoc "mode" attrs
6444 with Not_found -> "global"
6446 if closed
6447 then v
6448 else
6449 let ret keymap =
6450 let h = findkeyhash c modename in
6451 KeyMap.iter (Hashtbl.replace h) keymap;
6452 doc path pan anchor c bookmarks
6454 { v with f = pkeymap ret KeyMap.empty }
6456 | Vopen (_, _, _) ->
6457 error "unexpected subelement in doc" s spos
6459 | Vclose "doc" ->
6460 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6461 { v with f = llppconfig }
6463 | Vclose _ -> error "unexpected close in doc" s spos
6465 and pkeymap ret keymap v t spos _ =
6466 match t with
6467 | Vdata | Vcdata -> v
6468 | Vend -> error "unexpected end of input in keymap" s spos
6469 | Vopen ("map", attrs, closed) ->
6470 let r, l = map_of attrs in
6471 let kss = fromstring keys_of_string spos "in" r [] in
6472 let lss = fromstring keys_of_string spos "out" l [] in
6473 let keymap =
6474 match kss with
6475 | [] -> keymap
6476 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6477 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6479 if closed
6480 then { v with f = pkeymap ret keymap }
6481 else
6482 let f () = v in
6483 { v with f = skip "map" f }
6485 | Vopen _ ->
6486 error "unexpected subelement in keymap" s spos
6488 | Vclose "keymap" ->
6489 { v with f = ret keymap }
6491 | Vclose _ -> error "unexpected close in keymap" s spos
6493 and pbookmarks path pan anchor c bookmarks v t spos _ =
6494 match t with
6495 | Vdata | Vcdata -> v
6496 | Vend -> error "unexpected end of input in bookmarks" s spos
6497 | Vopen ("item", attrs, closed) ->
6498 let titleent, spage, srely, svisy = bookmark_of attrs in
6499 let page = fromstring int_of_string spos "page" spage 0
6500 and rely = fromstring float_of_string spos "rely" srely 0.0
6501 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6502 let bookmarks =
6503 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6505 if closed
6506 then { v with f = pbookmarks path pan anchor c bookmarks }
6507 else
6508 let f () = v in
6509 { v with f = skip "item" f }
6511 | Vopen _ ->
6512 error "unexpected subelement in bookmarks" s spos
6514 | Vclose "bookmarks" ->
6515 { v with f = doc path pan anchor c bookmarks }
6517 | Vclose _ -> error "unexpected close in bookmarks" s spos
6519 and skip tag f v t spos _ =
6520 match t with
6521 | Vdata | Vcdata -> v
6522 | Vend ->
6523 error ("unexpected end of input in skipped " ^ tag) s spos
6524 | Vopen (tag', _, closed) ->
6525 if closed
6526 then v
6527 else
6528 let f' () = { v with f = skip tag f } in
6529 { v with f = skip tag' f' }
6530 | Vclose ctag ->
6531 if tag = ctag
6532 then f ()
6533 else error ("unexpected close in skipped " ^ tag) s spos
6536 parse { f = toplevel; accu = () } s;
6537 h, dc;
6540 let do_load f ic =
6542 let len = in_channel_length ic in
6543 let s = String.create len in
6544 really_input ic s 0 len;
6545 f s;
6546 with
6547 | Parse_error (msg, s, pos) ->
6548 let subs = subs s pos in
6549 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6550 failwith ("parse error: " ^ s)
6552 | exn ->
6553 failwith ("config load error: " ^ Printexc.to_string exn)
6556 let defconfpath =
6557 let dir =
6559 let dir = Filename.concat home ".config" in
6560 if Sys.is_directory dir then dir else home
6561 with _ -> home
6563 Filename.concat dir "llpp.conf"
6566 let confpath = ref defconfpath;;
6568 let load1 f =
6569 if Sys.file_exists !confpath
6570 then
6571 match
6572 (try Some (open_in_bin !confpath)
6573 with exn ->
6574 prerr_endline
6575 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6576 Printexc.to_string exn);
6577 None
6579 with
6580 | Some ic ->
6581 let success =
6583 f (do_load get ic)
6584 with exn ->
6585 prerr_endline
6586 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6587 Printexc.to_string exn);
6588 false
6590 close_in ic;
6591 success
6593 | None -> false
6594 else
6595 f (Hashtbl.create 0, defconf)
6598 let load () =
6599 let f (h, dc) =
6600 let pc, pb, px, pa =
6602 Hashtbl.find h (Filename.basename state.path)
6603 with Not_found -> dc, [], 0, emptyanchor
6605 setconf defconf dc;
6606 setconf conf pc;
6607 state.bookmarks <- pb;
6608 state.x <- px;
6609 state.scrollw <- conf.scrollbw;
6610 if conf.jumpback
6611 then state.anchor <- pa;
6612 cbput state.hists.nav pa;
6613 true
6615 load1 f
6618 let add_attrs bb always dc c =
6619 let ob s a b =
6620 if always || a != b
6621 then Printf.bprintf bb "\n %s='%b'" s a
6622 and oi s a b =
6623 if always || a != b
6624 then Printf.bprintf bb "\n %s='%d'" s a
6625 and oI s a b =
6626 if always || a != b
6627 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6628 and oz s a b =
6629 if always || a <> b
6630 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6631 and oF s a b =
6632 if always || a <> b
6633 then Printf.bprintf bb "\n %s='%f'" s a
6634 and oc s a b =
6635 if always || a <> b
6636 then
6637 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6638 and oC s a b =
6639 if always || a <> b
6640 then
6641 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6642 and oR s a b =
6643 if always || a <> b
6644 then
6645 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6646 and os s a b =
6647 if always || a <> b
6648 then
6649 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6650 and og s a b =
6651 if always || a <> b
6652 then
6653 match a with
6654 | None -> ()
6655 | Some (_N, _A, _B) ->
6656 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6657 and oW s a b =
6658 if always || a <> b
6659 then
6660 let v =
6661 match a with
6662 | None -> "false"
6663 | Some f ->
6664 if f = infinity
6665 then "true"
6666 else string_of_float f
6668 Printf.bprintf bb "\n %s='%s'" s v
6669 and oco s a b =
6670 if always || a <> b
6671 then
6672 match a with
6673 | Cmulti ((n, a, b), _) when n > 1 ->
6674 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6675 | Csplit (n, _) when n > 1 ->
6676 Printf.bprintf bb "\n %s='%d'" s ~-n
6677 | _ -> ()
6678 and obeco s a b =
6679 if always || a <> b
6680 then
6681 match a with
6682 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6683 | _ -> ()
6685 let w, h =
6686 if always
6687 then dc.winw, dc.winh
6688 else
6689 match state.fullscreen with
6690 | Some wh -> wh
6691 | None -> c.winw, c.winh
6693 oi "width" w dc.winw;
6694 oi "height" h dc.winh;
6695 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6696 oi "scroll-handle-height" c.scrollh dc.scrollh;
6697 ob "case-insensitive-search" c.icase dc.icase;
6698 ob "preload" c.preload dc.preload;
6699 oi "page-bias" c.pagebias dc.pagebias;
6700 oi "scroll-step" c.scrollstep dc.scrollstep;
6701 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6702 ob "max-height-fit" c.maxhfit dc.maxhfit;
6703 ob "crop-hack" c.crophack dc.crophack;
6704 oW "throttle" c.maxwait dc.maxwait;
6705 ob "highlight-links" c.hlinks dc.hlinks;
6706 ob "under-cursor-info" c.underinfo dc.underinfo;
6707 oi "vertical-margin" c.interpagespace dc.interpagespace;
6708 oz "zoom" c.zoom dc.zoom;
6709 ob "presentation" c.presentation dc.presentation;
6710 oi "rotation-angle" c.angle dc.angle;
6711 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6712 ob "proportional-display" c.proportional dc.proportional;
6713 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6714 oi "tex-count" c.texcount dc.texcount;
6715 oi "slice-height" c.sliceheight dc.sliceheight;
6716 oi "thumbnail-width" c.thumbw dc.thumbw;
6717 ob "persistent-location" c.jumpback dc.jumpback;
6718 oc "background-color" c.bgcolor dc.bgcolor;
6719 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6720 oi "tile-width" c.tilew dc.tilew;
6721 oi "tile-height" c.tileh dc.tileh;
6722 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6723 ob "checkers" c.checkers dc.checkers;
6724 oi "aalevel" c.aalevel dc.aalevel;
6725 ob "trim-margins" c.trimmargins dc.trimmargins;
6726 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6727 os "uri-launcher" c.urilauncher dc.urilauncher;
6728 os "path-launcher" c.pathlauncher dc.pathlauncher;
6729 oC "color-space" c.colorspace dc.colorspace;
6730 ob "invert-colors" c.invert dc.invert;
6731 oF "brightness" c.colorscale dc.colorscale;
6732 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6733 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6734 oco "columns" c.columns dc.columns;
6735 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6736 os "selection-command" c.selcmd dc.selcmd;
6737 os "synctex-command" c.stcmd dc.stcmd;
6738 ob "update-cursor" c.updatecurs dc.updatecurs;
6739 oi "hint-font-size" c.hfsize dc.hfsize;
6740 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6741 oF "page-scroll-scale" c.pgscale dc.pgscale;
6742 ob "use-pbo" c.usepbo dc.usepbo;
6743 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6746 let keymapsbuf always dc c =
6747 let bb = Buffer.create 16 in
6748 let rec loop = function
6749 | [] -> ()
6750 | (modename, h) :: rest ->
6751 let dh = findkeyhash dc modename in
6752 if always || h <> dh
6753 then (
6754 if Hashtbl.length h > 0
6755 then (
6756 if Buffer.length bb > 0
6757 then Buffer.add_char bb '\n';
6758 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6759 Hashtbl.iter (fun i o ->
6760 let isdifferent = always ||
6762 let dO = Hashtbl.find dh i in
6763 dO <> o
6764 with Not_found -> true
6766 if isdifferent
6767 then
6768 let addkm (k, m) =
6769 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6770 if Wsi.withalt m then Buffer.add_string bb "alt-";
6771 if Wsi.withshift m then Buffer.add_string bb "shift-";
6772 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6773 Buffer.add_string bb (Wsi.keyname k);
6775 let addkms l =
6776 let rec loop = function
6777 | [] -> ()
6778 | km :: [] -> addkm km
6779 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6781 loop l
6783 Buffer.add_string bb "<map in='";
6784 addkm i;
6785 match o with
6786 | KMinsrt km ->
6787 Buffer.add_string bb "' out='";
6788 addkm km;
6789 Buffer.add_string bb "'/>\n"
6791 | KMinsrl kms ->
6792 Buffer.add_string bb "' out='";
6793 addkms kms;
6794 Buffer.add_string bb "'/>\n"
6796 | KMmulti (ins, kms) ->
6797 Buffer.add_char bb ' ';
6798 addkms ins;
6799 Buffer.add_string bb "' out='";
6800 addkms kms;
6801 Buffer.add_string bb "'/>\n"
6802 ) h;
6803 Buffer.add_string bb "</keymap>";
6806 loop rest
6808 loop c.keyhashes;
6812 let save () =
6813 let uifontsize = fstate.fontsize in
6814 let bb = Buffer.create 32768 in
6815 let f (h, dc) =
6816 let dc = if conf.bedefault then conf else dc in
6817 Buffer.add_string bb "<llppconfig>\n";
6819 if String.length !fontpath > 0
6820 then
6821 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6822 uifontsize
6823 !fontpath
6824 else (
6825 if uifontsize <> 14
6826 then
6827 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6830 Buffer.add_string bb "<defaults ";
6831 add_attrs bb true dc dc;
6832 let kb = keymapsbuf true dc dc in
6833 if Buffer.length kb > 0
6834 then (
6835 Buffer.add_string bb ">\n";
6836 Buffer.add_buffer bb kb;
6837 Buffer.add_string bb "\n</defaults>\n";
6839 else Buffer.add_string bb "/>\n";
6841 let adddoc path pan anchor c bookmarks =
6842 if bookmarks == [] && c = dc && anchor = emptyanchor
6843 then ()
6844 else (
6845 Printf.bprintf bb "<doc path='%s'"
6846 (enent path 0 (String.length path));
6848 if anchor <> emptyanchor
6849 then (
6850 let n, rely, visy = anchor in
6851 Printf.bprintf bb " page='%d'" n;
6852 if rely > 1e-6
6853 then
6854 Printf.bprintf bb " rely='%f'" rely
6856 if abs_float visy > 1e-6
6857 then
6858 Printf.bprintf bb " visy='%f'" visy
6862 if pan != 0
6863 then Printf.bprintf bb " pan='%d'" pan;
6865 add_attrs bb false dc c;
6866 let kb = keymapsbuf false dc c in
6868 begin match bookmarks with
6869 | [] ->
6870 if Buffer.length kb > 0
6871 then (
6872 Buffer.add_string bb ">\n";
6873 Buffer.add_buffer bb kb;
6874 Buffer.add_string bb "\n</doc>\n";
6876 else Buffer.add_string bb "/>\n"
6877 | _ ->
6878 Buffer.add_string bb ">\n<bookmarks>\n";
6879 List.iter (fun (title, _level, (page, rely, visy)) ->
6880 Printf.bprintf bb
6881 "<item title='%s' page='%d'"
6882 (enent title 0 (String.length title))
6883 page
6885 if rely > 1e-6
6886 then
6887 Printf.bprintf bb " rely='%f'" rely
6889 if abs_float visy > 1e-6
6890 then
6891 Printf.bprintf bb " visy='%f'" visy
6893 Buffer.add_string bb "/>\n";
6894 ) bookmarks;
6895 Buffer.add_string bb "</bookmarks>";
6896 if Buffer.length kb > 0
6897 then (
6898 Buffer.add_string bb "\n";
6899 Buffer.add_buffer bb kb;
6901 Buffer.add_string bb "\n</doc>\n";
6902 end;
6906 let pan, conf =
6907 match state.mode with
6908 | Birdseye (c, pan, _, _, _) ->
6909 let beyecolumns =
6910 match conf.columns with
6911 | Cmulti ((c, _, _), _) -> Some c
6912 | Csingle _ -> None
6913 | Csplit _ -> None
6914 and columns =
6915 match c.columns with
6916 | Cmulti (c, _) -> Cmulti (c, [||])
6917 | Csingle _ -> Csingle [||]
6918 | Csplit _ -> failwith "quit from bird's eye while split"
6920 pan, { c with beyecolumns = beyecolumns; columns = columns }
6921 | _ -> state.x, conf
6923 let basename = Filename.basename state.path in
6924 adddoc basename pan (getanchor ())
6925 (let conf =
6926 let autoscrollstep =
6927 match state.autoscroll with
6928 | Some step -> step
6929 | None -> conf.autoscrollstep
6931 match state.mode with
6932 | Birdseye (bc, _, _, _, _) ->
6933 { conf with
6934 zoom = bc.zoom;
6935 presentation = bc.presentation;
6936 interpagespace = bc.interpagespace;
6937 maxwait = bc.maxwait;
6938 autoscrollstep = autoscrollstep }
6939 | _ -> { conf with autoscrollstep = autoscrollstep }
6940 in conf)
6941 (if conf.savebmarks then state.bookmarks else []);
6943 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6944 if basename <> path
6945 then adddoc path x anchor c bookmarks
6946 ) h;
6947 Buffer.add_string bb "</llppconfig>\n";
6948 true;
6950 if load1 f && Buffer.length bb > 0
6951 then
6953 let tmp = !confpath ^ ".tmp" in
6954 let oc = open_out_bin tmp in
6955 Buffer.output_buffer oc bb;
6956 close_out oc;
6957 Unix.rename tmp !confpath;
6958 with exn ->
6959 prerr_endline
6960 ("error while saving configuration: " ^ Printexc.to_string exn)
6962 end;;
6964 let adderrmsg src msg =
6965 Buffer.add_string state.errmsgs msg;
6966 state.newerrmsgs <- true;
6967 G.postRedisplay src
6970 let adderrfmt src fmt =
6971 Format.kprintf (fun s -> adderrmsg src s) fmt;
6974 let onpagerect pageno f =
6975 let b =
6976 match conf.columns with
6977 | Cmulti (_, b) -> b
6978 | Csingle b -> b
6979 | Csplit (_, b) -> b
6981 if pageno >= 0 && pageno < Array.length b
6982 then
6983 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
6984 let r = getpdimrect pdimno in
6985 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
6988 let ract cmds =
6989 let op, args =
6990 let spacepos =
6991 try String.index cmds ' '
6992 with Not_found -> -1
6994 if spacepos = -1
6995 then cmds, ""
6996 else
6997 let l = String.length cmds in
6998 let op = String.sub cmds 0 spacepos in
6999 op, begin
7000 if l - spacepos < 2 then ""
7001 else String.sub cmds (spacepos+1) (l-spacepos-1)
7004 let scan s fmt f =
7005 try Scanf.sscanf s fmt f
7006 with exn ->
7007 adderrfmt "remote exec"
7008 "error processing '%S' %S: %s\n" op cmds (Printexc.to_string exn)
7010 match op with
7011 | "reload" -> reload ()
7012 | "goto" ->
7013 scan args "%u %f %f"
7014 (fun pageno _ y ->
7015 onpagerect pageno (fun _ h ->
7016 let top = y /. h in
7017 gotopage pageno top;
7020 | "goto1" -> scan args "%u %f" gotopage
7021 | "rect" ->
7022 scan args "%u %u %f %f %f %f"
7023 (fun pageno color x0 y0 x1 y1 ->
7024 onpagerect pageno (fun w h ->
7025 let _,w1,h1,_ = getpagedim pageno in
7026 let sw = float w1 /. w
7027 and sh = float h1 /. h in
7028 let x0s = x0 *. sw
7029 and x1s = x1 *. sw
7030 and y0s = y0 *. sh
7031 and y1s = y1 *. sh in
7032 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7033 debugrect rect;
7034 state.rects <- (pageno, color, rect) :: state.rects;
7035 G.postRedisplay "rect";
7038 | "quit" -> raise Quit
7039 | _ ->
7040 adderrfmt "remote command"
7041 "error processing remote command: %S\n" cmds;
7044 let remote =
7045 let scratch = String.create 80 in
7046 let buf = Buffer.create 80 in
7047 fun fd ->
7048 let rec tempfr () =
7049 try Some (Unix.read fd scratch 0 80)
7050 with
7051 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7052 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7053 | exn -> raise exn
7055 match tempfr () with
7056 | None -> Some fd
7057 | Some n ->
7058 if n = 0
7059 then (
7060 Unix.close fd;
7061 if Buffer.length buf > 0
7062 then (
7063 let s = Buffer.contents buf in
7064 Buffer.clear buf;
7065 ract s;
7067 None
7069 else
7070 let rec eat ppos =
7071 let nlpos =
7073 let pos = String.index_from scratch ppos '\n' in
7074 if pos >= n then -1 else pos
7075 with Not_found -> -1
7077 if nlpos >= 0
7078 then (
7079 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7080 let s = Buffer.contents buf in
7081 Buffer.clear buf;
7082 ract s;
7083 eat (nlpos+1);
7085 else (
7086 Buffer.add_substring buf scratch ppos (n-ppos);
7087 Some fd
7089 in eat 0
7092 let remoteopen path =
7093 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7094 with exn ->
7095 adderrfmt "remoteopen" "error opening %S: %s" path (Printexc.to_string exn);
7096 None
7099 let () =
7100 let trimcachepath = ref "" in
7101 let rcmdpath = ref "" in
7102 Arg.parse
7103 (Arg.align
7104 [("-p", Arg.String (fun s -> state.password <- s) ,
7105 "<password> Set password");
7107 ("-f", Arg.String (fun s -> Config.fontpath := s),
7108 "<path> Set path to the user interface font");
7110 ("-c", Arg.String (fun s -> Config.confpath := s),
7111 "<path> Set path to the configuration file");
7113 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7114 "<path> Set path to the trim cache file");
7116 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7117 "<named-destination> Set named destination");
7119 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7121 ("-remote", Arg.String (fun s -> rcmdpath := s),
7122 "<path> Set path to the remote commands source");
7124 ("-v", Arg.Unit (fun () ->
7125 Printf.printf
7126 "%s\nconfiguration path: %s\n"
7127 (version ())
7128 Config.defconfpath
7130 exit 0), " Print version and exit");
7133 (fun s -> state.path <- s)
7134 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7136 if String.length state.path = 0
7137 then (prerr_endline "file name missing"; exit 1);
7139 if not (Config.load ())
7140 then prerr_endline "failed to load configuration";
7142 let globalkeyhash = findkeyhash conf "global" in
7143 let wsfd, winw, winh = Wsi.init (object
7144 method expose =
7145 state.wthack <- false;
7146 if nogeomcmds state.geomcmds || platform == Posx
7147 then display ()
7148 else (
7149 GlClear.color (scalecolor2 conf.bgcolor);
7150 GlClear.clear [`color];
7152 method display = display ()
7153 method reshape w h = reshape w h
7154 method mouse b d x y m = mouse b d x y m
7155 method motion x y = state.mpos <- (x, y); motion x y
7156 method pmotion x y = state.mpos <- (x, y); pmotion x y
7157 method key k m =
7158 let mascm = m land (
7159 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7160 ) in
7161 match state.keystate with
7162 | KSnone ->
7163 let km = k, mascm in
7164 begin
7165 match
7166 let modehash = state.uioh#modehash in
7167 try Hashtbl.find modehash km
7168 with Not_found ->
7169 try Hashtbl.find globalkeyhash km
7170 with Not_found -> KMinsrt (k, m)
7171 with
7172 | KMinsrt (k, m) -> keyboard k m
7173 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7174 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7176 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7177 List.iter (fun (k, m) -> keyboard k m) insrt;
7178 state.keystate <- KSnone
7179 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7180 state.keystate <- KSinto (keys, insrt)
7181 | _ ->
7182 state.keystate <- KSnone
7184 method enter x y = state.mpos <- (x, y); pmotion x y
7185 method leave = state.mpos <- (-1, -1)
7186 method quit = raise Quit
7187 end) conf.winw conf.winh (platform = Posx) in
7189 state.wsfd <- wsfd;
7191 if not (
7192 List.exists GlMisc.check_extension
7193 [ "GL_ARB_texture_rectangle"
7194 ; "GL_EXT_texture_recangle"
7195 ; "GL_NV_texture_rectangle" ]
7197 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7199 let cr, sw =
7200 match Ne.pipe () with
7201 | Ne.Exn exn ->
7202 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
7203 exit 1
7204 | Ne.Res rw -> rw
7205 and sr, cw =
7206 match Ne.pipe () with
7207 | Ne.Exn exn ->
7208 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7209 exit 1
7210 | Ne.Res rw -> rw
7213 cloexec cr;
7214 cloexec sw;
7215 cloexec sr;
7216 cloexec cw;
7218 setcheckers conf.checkers;
7219 redirectstderr ();
7221 init (cr, cw) (
7222 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7223 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7224 !Config.fontpath, !trimcachepath,
7225 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7227 state.sr <- sr;
7228 state.sw <- sw;
7229 state.text <- "Opening " ^ (mbtoutf8 state.path);
7230 reshape winw winh;
7231 opendoc state.path state.password;
7232 state.uioh <- uioh;
7234 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7235 let optrfd =
7236 ref (
7237 if String.length !rcmdpath > 0
7238 then remoteopen !rcmdpath
7239 else None
7243 let rec loop deadline =
7244 let r =
7245 match state.errfd with
7246 | None -> [state.sr; state.wsfd]
7247 | Some fd -> [state.sr; state.wsfd; fd]
7249 let r =
7250 match !optrfd with
7251 | None -> r
7252 | Some fd -> fd :: r
7254 if state.redisplay && not state.wthack
7255 then (
7256 state.redisplay <- false;
7257 display ();
7259 let timeout =
7260 let now = now () in
7261 if deadline > now
7262 then (
7263 if deadline = infinity
7264 then ~-.1.0
7265 else max 0.0 (deadline -. now)
7267 else 0.0
7269 let r, _, _ =
7270 try Unix.select r [] [] timeout
7271 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7273 begin match r with
7274 | [] ->
7275 state.ghyll None;
7276 let newdeadline =
7277 if state.ghyll == noghyll
7278 then
7279 match state.autoscroll with
7280 | Some step when step != 0 ->
7281 let y = state.y + step in
7282 let y =
7283 if y < 0
7284 then state.maxy
7285 else if y >= state.maxy then 0 else y
7287 gotoy y;
7288 if state.mode = View
7289 then state.text <- "";
7290 deadline +. 0.01
7291 | _ -> infinity
7292 else deadline +. 0.01
7294 loop newdeadline
7296 | l ->
7297 let rec checkfds = function
7298 | [] -> ()
7299 | fd :: rest when fd = state.sr ->
7300 let cmd = readcmd state.sr in
7301 act cmd;
7302 checkfds rest
7304 | fd :: rest when fd = state.wsfd ->
7305 Wsi.readresp fd;
7306 checkfds rest
7308 | fd :: rest when Some fd = !optrfd ->
7309 begin match remote fd with
7310 | None -> optrfd := remoteopen !rcmdpath;
7311 | opt -> optrfd := opt
7312 end;
7313 checkfds rest
7315 | fd :: rest ->
7316 let s = String.create 80 in
7317 let n = tempfailureretry (Unix.read fd s 0) 80 in
7318 if conf.redirectstderr
7319 then (
7320 Buffer.add_substring state.errmsgs s 0 n;
7321 state.newerrmsgs <- true;
7322 state.redisplay <- true;
7324 else (
7325 prerr_string (String.sub s 0 n);
7326 flush stderr;
7328 checkfds rest
7330 checkfds l;
7331 let newdeadline =
7332 let deadline1 =
7333 if deadline = infinity
7334 then now () +. 0.01
7335 else deadline
7337 match state.autoscroll with
7338 | Some step when step != 0 -> deadline1
7339 | _ -> if state.ghyll == noghyll then infinity else deadline1
7341 loop newdeadline
7342 end;
7345 loop infinity;
7346 with Quit ->
7347 Config.save ();