Refactor
[llpp.git] / main.ml
blobcd8c03fcb3e8756391117274e5bbc83195c7e716
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 splitatspace =
2260 let r = Str.regexp " " in
2261 fun s -> Str.bounded_split r s 2;
2264 let act cmds =
2265 (* dolog "%S" cmds; *)
2266 let cl = splitatspace cmds in
2267 let scan s fmt f =
2268 try Scanf.sscanf s fmt f
2269 with exn ->
2270 dolog "error processing '%S': %s" cmds (Printexc.to_string exn);
2271 exit 1
2273 match cl with
2274 | "clear" :: [] ->
2275 state.uioh#infochanged Pdim;
2276 state.pdims <- [];
2278 | "clearrects" :: [] ->
2279 state.rects <- state.rects1;
2280 G.postRedisplay "clearrects";
2282 | "continue" :: args :: [] ->
2283 let n = scan args "%u" (fun n -> n) in
2284 state.pagecount <- n;
2285 begin match state.currently with
2286 | Outlining l ->
2287 state.currently <- Idle;
2288 state.outlines <- Array.of_list (List.rev l)
2289 | _ -> ()
2290 end;
2292 let cur, cmds = state.geomcmds in
2293 if String.length cur = 0
2294 then failwith "umpossible";
2296 begin match List.rev cmds with
2297 | [] ->
2298 state.geomcmds <- "", [];
2299 represent ();
2300 | (s, f) :: rest ->
2301 f ();
2302 state.geomcmds <- s, List.rev rest;
2303 end;
2304 if conf.maxwait = None
2305 then G.postRedisplay "continue";
2307 | "title" :: args :: [] ->
2308 Wsi.settitle args
2310 | "msg" :: args :: [] ->
2311 showtext ' ' args
2313 | "vmsg" :: args :: [] ->
2314 if conf.verbose
2315 then showtext ' ' args
2317 | "emsg" :: args :: [] ->
2318 Buffer.add_string state.errmsgs args;
2319 state.newerrmsgs <- true;
2320 G.postRedisplay "error message"
2322 | "progress" :: args :: [] ->
2323 let progress, text =
2324 scan args "%f %n"
2325 (fun f pos ->
2326 f, String.sub args pos (String.length args - pos))
2328 state.text <- text;
2329 state.progress <- progress;
2330 G.postRedisplay "progress"
2332 | "firstmatch" :: args :: [] ->
2333 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2334 scan args "%u %d %f %f %f %f %f %f %f %f"
2335 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2336 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2338 let y = (getpagey pageno) + truncate y0 in
2339 addnav ();
2340 gotoy y;
2341 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2343 | "match" :: args :: [] ->
2344 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2345 scan args "%u %d %f %f %f %f %f %f %f %f"
2346 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2347 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2349 state.rects1 <-
2350 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2352 | "page" :: args :: [] ->
2353 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2354 begin match state.currently with
2355 | Loading (l, gen) ->
2356 vlog "page %d took %f sec" l.pageno t;
2357 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2358 begin match state.throttle with
2359 | None ->
2360 let preloadedpages =
2361 if conf.preload
2362 then preloadlayout state.y
2363 else state.layout
2365 let evict () =
2366 let module IntSet =
2367 Set.Make (struct type t = int let compare = (-) end) in
2368 let set =
2369 List.fold_left (fun s l -> IntSet.add l.pageno s)
2370 IntSet.empty preloadedpages
2372 let evictedpages =
2373 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2374 if not (IntSet.mem pageno set)
2375 then (
2376 wcmd "freepage %s" opaque;
2377 key :: accu
2379 else accu
2380 ) state.pagemap []
2382 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2384 evict ();
2385 state.currently <- Idle;
2386 if gen = state.gen
2387 then (
2388 tilepage l.pageno pageopaque state.layout;
2389 load state.layout;
2390 load preloadedpages;
2391 if pagevisible state.layout l.pageno
2392 && layoutready state.layout
2393 then G.postRedisplay "page";
2396 | Some (layout, _, _) ->
2397 state.currently <- Idle;
2398 tilepage l.pageno pageopaque layout;
2399 load state.layout
2400 end;
2402 | _ ->
2403 dolog "Inconsistent loading state";
2404 logcurrently state.currently;
2405 exit 1
2408 | "tile" :: args :: [] ->
2409 let (x, y, opaque, size, t) =
2410 scan args "%u %u %s %u %f"
2411 (fun x y p size t -> (x, y, p, size, t))
2413 begin match state.currently with
2414 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2415 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2417 unmappbo opaque;
2418 if tilew != conf.tilew || tileh != conf.tileh
2419 then (
2420 wcmd "freetile %s" opaque;
2421 state.currently <- Idle;
2422 load state.layout;
2424 else (
2425 puttileopaque l col row gen cs angle opaque size t;
2426 state.memused <- state.memused + size;
2427 state.uioh#infochanged Memused;
2428 gctiles ();
2429 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2430 opaque, size) state.tilelru;
2432 let layout =
2433 match state.throttle with
2434 | None -> state.layout
2435 | Some (layout, _, _) -> layout
2438 state.currently <- Idle;
2439 if gen = state.gen
2440 && conf.colorspace = cs
2441 && conf.angle = angle
2442 && tilevisible layout l.pageno x y
2443 then conttiling l.pageno pageopaque;
2445 begin match state.throttle with
2446 | None ->
2447 if state.wthack
2448 then state.wthack <- not (layoutready state.layout);
2449 preload state.layout;
2450 if gen = state.gen
2451 && conf.colorspace = cs
2452 && conf.angle = angle
2453 && tilevisible state.layout l.pageno x y
2454 then G.postRedisplay "tile nothrottle";
2456 | Some (layout, y, _) ->
2457 let ready = layoutready layout in
2458 if ready
2459 then (
2460 state.wthack <- false;
2461 state.y <- y;
2462 state.layout <- layout;
2463 state.throttle <- None;
2464 G.postRedisplay "throttle";
2466 else load layout;
2467 end;
2470 | _ ->
2471 dolog "Inconsistent tiling state";
2472 logcurrently state.currently;
2473 exit 1
2476 | "pdim" :: args :: [] ->
2477 let pdim =
2478 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2480 state.uioh#infochanged Pdim;
2481 state.pdims <- pdim :: state.pdims
2483 | "o" :: args :: [] ->
2484 let (l, n, t, h, pos) =
2485 scan args "%u %u %d %u %n"
2486 (fun l n t h pos -> l, n, t, h, pos)
2488 let s = String.sub args pos (String.length args - pos) in
2489 let outline = (s, l, (n, float t /. float h, 0.0)) in
2490 begin match state.currently with
2491 | Outlining outlines ->
2492 state.currently <- Outlining (outline :: outlines)
2493 | Idle ->
2494 state.currently <- Outlining [outline]
2495 | currently ->
2496 dolog "invalid outlining state";
2497 logcurrently currently
2500 | "a" :: args :: [] ->
2501 let (n, t, h) =
2502 scan args "%u %u %d" (fun n t h -> n, t, h)
2504 let top, dtop =
2505 if conf.presentation
2506 then (0.0, 1.0)
2507 else float t /. float h, 0.0
2509 state.anchor <- (n, top, dtop)
2511 | "info" :: args :: [] ->
2512 state.docinfo <- (1, args) :: state.docinfo
2514 | "infoend" :: [] ->
2515 state.uioh#infochanged Docinfo;
2516 state.docinfo <- List.rev state.docinfo
2518 | _ ->
2519 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2522 let onhist cb =
2523 let rc = cb.rc in
2524 let action = function
2525 | HCprev -> cbget cb ~-1
2526 | HCnext -> cbget cb 1
2527 | HCfirst -> cbget cb ~-(cb.rc)
2528 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2529 and cancel () = cb.rc <- rc
2530 in (action, cancel)
2533 let search pattern forward =
2534 if String.length pattern > 0
2535 then
2536 let pn, py =
2537 match state.layout with
2538 | [] -> 0, 0
2539 | l :: _ ->
2540 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2542 wcmd "search %d %d %d %d,%s\000"
2543 (btod conf.icase) pn py (btod forward) pattern;
2546 let intentry text key =
2547 let c =
2548 if key >= 32 && key < 127
2549 then Char.chr key
2550 else '\000'
2552 match c with
2553 | '0' .. '9' ->
2554 let text = addchar text c in
2555 TEcont text
2557 | _ ->
2558 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2559 TEcont text
2562 let linknentry text key =
2563 let c =
2564 if key >= 32 && key < 127
2565 then Char.chr key
2566 else '\000'
2568 match c with
2569 | 'a' .. 'z' ->
2570 let text = addchar text c in
2571 TEcont text
2573 | _ ->
2574 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2575 TEcont text
2578 let linkndone f s =
2579 if String.length s > 0
2580 then (
2581 let n =
2582 let l = String.length s in
2583 let rec loop pos n = if pos = l then n else
2584 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2585 loop (pos+1) (n*26 + m)
2586 in loop 0 0
2588 let rec loop n = function
2589 | [] -> ()
2590 | l :: rest ->
2591 match getopaque l.pageno with
2592 | None -> loop n rest
2593 | Some opaque ->
2594 let m = getlinkcount opaque in
2595 if n < m
2596 then (
2597 let under = getlink opaque n in
2598 f under
2600 else loop (n-m) rest
2602 loop n state.layout;
2606 let textentry text key =
2607 if key land 0xff00 = 0xff00
2608 then TEcont text
2609 else TEcont (text ^ Wsi.toutf8 key)
2612 let reqlayout angle proportional =
2613 match state.throttle with
2614 | None ->
2615 if nogeomcmds state.geomcmds
2616 then state.anchor <- getanchor ();
2617 conf.angle <- angle mod 360;
2618 if conf.angle != 0
2619 then (
2620 match state.mode with
2621 | LinkNav _ -> state.mode <- View
2622 | _ -> ()
2624 conf.proportional <- proportional;
2625 invalidate "reqlayout"
2626 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2627 | _ -> ()
2630 let settrim trimmargins trimfuzz =
2631 if nogeomcmds state.geomcmds
2632 then state.anchor <- getanchor ();
2633 conf.trimmargins <- trimmargins;
2634 conf.trimfuzz <- trimfuzz;
2635 let x0, y0, x1, y1 = trimfuzz in
2636 invalidate "settrim"
2637 (fun () ->
2638 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2639 flushpages ();
2642 let setzoom zoom =
2643 match state.throttle with
2644 | None ->
2645 let zoom = max 0.01 zoom in
2646 if zoom <> conf.zoom
2647 then (
2648 state.prevzoom <- conf.zoom;
2649 conf.zoom <- zoom;
2650 reshape conf.winw conf.winh;
2651 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2654 | Some (layout, y, started) ->
2655 let time =
2656 match conf.maxwait with
2657 | None -> 0.0
2658 | Some t -> t
2660 let dt = now () -. started in
2661 if dt > time
2662 then (
2663 state.y <- y;
2664 load layout;
2668 let setcolumns mode columns coverA coverB =
2669 state.prevcolumns <- Some (conf.columns, conf.zoom);
2670 if columns < 0
2671 then (
2672 if isbirdseye mode
2673 then showtext '!' "split mode doesn't work in bird's eye"
2674 else (
2675 conf.columns <- Csplit (-columns, [||]);
2676 state.x <- 0;
2677 conf.zoom <- 1.0;
2680 else (
2681 if columns < 2
2682 then (
2683 conf.columns <- Csingle [||];
2684 state.x <- 0;
2685 setzoom 1.0;
2687 else (
2688 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2689 conf.zoom <- 1.0;
2692 reshape conf.winw conf.winh;
2695 let enterbirdseye () =
2696 let zoom = float conf.thumbw /. float conf.winw in
2697 let birdseyepageno =
2698 let cy = conf.winh / 2 in
2699 let fold = function
2700 | [] -> 0
2701 | l :: rest ->
2702 let rec fold best = function
2703 | [] -> best.pageno
2704 | l :: rest ->
2705 let d = cy - (l.pagedispy + l.pagevh/2)
2706 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2707 if abs d < abs dbest
2708 then fold l rest
2709 else best.pageno
2710 in fold l rest
2712 fold state.layout
2714 state.mode <- Birdseye (
2715 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2717 conf.zoom <- zoom;
2718 conf.presentation <- false;
2719 conf.interpagespace <- 10;
2720 conf.hlinks <- false;
2721 state.x <- 0;
2722 state.mstate <- Mnone;
2723 conf.maxwait <- None;
2724 conf.columns <- (
2725 match conf.beyecolumns with
2726 | Some c ->
2727 conf.zoom <- 1.0;
2728 Cmulti ((c, 0, 0), [||])
2729 | None -> Csingle [||]
2731 Wsi.setcursor Wsi.CURSOR_INHERIT;
2732 if conf.verbose
2733 then
2734 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2735 (100.0*.zoom)
2736 else
2737 state.text <- ""
2739 reshape conf.winw conf.winh;
2742 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2743 state.mode <- View;
2744 conf.zoom <- c.zoom;
2745 conf.presentation <- c.presentation;
2746 conf.interpagespace <- c.interpagespace;
2747 conf.maxwait <- c.maxwait;
2748 conf.hlinks <- c.hlinks;
2749 conf.beyecolumns <- (
2750 match conf.columns with
2751 | Cmulti ((c, _, _), _) -> Some c
2752 | Csingle _ -> None
2753 | Csplit _ -> failwith "leaving bird's eye split mode"
2755 conf.columns <- (
2756 match c.columns with
2757 | Cmulti (c, _) -> Cmulti (c, [||])
2758 | Csingle _ -> Csingle [||]
2759 | Csplit (c, _) -> Csplit (c, [||])
2761 state.x <- leftx;
2762 if conf.verbose
2763 then
2764 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2765 (100.0*.conf.zoom)
2767 reshape conf.winw conf.winh;
2768 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2771 let togglebirdseye () =
2772 match state.mode with
2773 | Birdseye vals -> leavebirdseye vals true
2774 | View -> enterbirdseye ()
2775 | _ -> ()
2778 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2779 let pageno = max 0 (pageno - incr) in
2780 let rec loop = function
2781 | [] -> gotopage1 pageno 0
2782 | l :: _ when l.pageno = pageno ->
2783 if l.pagedispy >= 0 && l.pagey = 0
2784 then G.postRedisplay "upbirdseye"
2785 else gotopage1 pageno 0
2786 | _ :: rest -> loop rest
2788 loop state.layout;
2789 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2792 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2793 let pageno = min (state.pagecount - 1) (pageno + incr) in
2794 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2795 let rec loop = function
2796 | [] ->
2797 let y, h = getpageyh pageno in
2798 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2799 gotoy (clamp dy)
2800 | l :: _ when l.pageno = pageno ->
2801 if l.pagevh != l.pageh
2802 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2803 else G.postRedisplay "downbirdseye"
2804 | _ :: rest -> loop rest
2806 loop state.layout
2809 let optentry mode _ key =
2810 let btos b = if b then "on" else "off" in
2811 if key >= 32 && key < 127
2812 then
2813 let c = Char.chr key in
2814 match c with
2815 | 's' ->
2816 let ondone s =
2817 try conf.scrollstep <- int_of_string s with exc ->
2818 state.text <- Printf.sprintf "bad integer `%s': %s"
2819 s (Printexc.to_string exc)
2821 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2823 | 'A' ->
2824 let ondone s =
2826 conf.autoscrollstep <- int_of_string s;
2827 if state.autoscroll <> None
2828 then state.autoscroll <- Some conf.autoscrollstep
2829 with exc ->
2830 state.text <- Printf.sprintf "bad integer `%s': %s"
2831 s (Printexc.to_string exc)
2833 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2835 | 'C' ->
2836 let ondone s =
2838 let n, a, b = multicolumns_of_string s in
2839 setcolumns mode n a b;
2840 with exc ->
2841 state.text <- Printf.sprintf "bad columns `%s': %s"
2842 s (Printexc.to_string exc)
2844 TEswitch ("columns: ", "", None, textentry, ondone, true)
2846 | 'Z' ->
2847 let ondone s =
2849 let zoom = float (int_of_string s) /. 100.0 in
2850 setzoom zoom
2851 with exc ->
2852 state.text <- Printf.sprintf "bad integer `%s': %s"
2853 s (Printexc.to_string exc)
2855 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2857 | 't' ->
2858 let ondone s =
2860 conf.thumbw <- bound (int_of_string s) 2 4096;
2861 state.text <-
2862 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2863 begin match mode with
2864 | Birdseye beye ->
2865 leavebirdseye beye false;
2866 enterbirdseye ();
2867 | _ -> ();
2869 with exc ->
2870 state.text <- Printf.sprintf "bad integer `%s': %s"
2871 s (Printexc.to_string exc)
2873 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2875 | 'R' ->
2876 let ondone s =
2877 match try
2878 Some (int_of_string s)
2879 with exc ->
2880 state.text <- Printf.sprintf "bad integer `%s': %s"
2881 s (Printexc.to_string exc);
2882 None
2883 with
2884 | Some angle -> reqlayout angle conf.proportional
2885 | None -> ()
2887 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2889 | 'i' ->
2890 conf.icase <- not conf.icase;
2891 TEdone ("case insensitive search " ^ (btos conf.icase))
2893 | 'p' ->
2894 conf.preload <- not conf.preload;
2895 gotoy state.y;
2896 TEdone ("preload " ^ (btos conf.preload))
2898 | 'v' ->
2899 conf.verbose <- not conf.verbose;
2900 TEdone ("verbose " ^ (btos conf.verbose))
2902 | 'd' ->
2903 conf.debug <- not conf.debug;
2904 TEdone ("debug " ^ (btos conf.debug))
2906 | 'h' ->
2907 conf.maxhfit <- not conf.maxhfit;
2908 state.maxy <- calcheight ();
2909 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2911 | 'c' ->
2912 conf.crophack <- not conf.crophack;
2913 TEdone ("crophack " ^ btos conf.crophack)
2915 | 'a' ->
2916 let s =
2917 match conf.maxwait with
2918 | None ->
2919 conf.maxwait <- Some infinity;
2920 "always wait for page to complete"
2921 | Some _ ->
2922 conf.maxwait <- None;
2923 "show placeholder if page is not ready"
2925 TEdone s
2927 | 'f' ->
2928 conf.underinfo <- not conf.underinfo;
2929 TEdone ("underinfo " ^ btos conf.underinfo)
2931 | 'P' ->
2932 conf.savebmarks <- not conf.savebmarks;
2933 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2935 | 'S' ->
2936 let ondone s =
2938 let pageno, py =
2939 match state.layout with
2940 | [] -> 0, 0
2941 | l :: _ ->
2942 l.pageno, l.pagey
2944 conf.interpagespace <- int_of_string s;
2945 docolumns conf.columns;
2946 state.maxy <- calcheight ();
2947 let y = getpagey pageno in
2948 gotoy (y + py)
2949 with exc ->
2950 state.text <- Printf.sprintf "bad integer `%s': %s"
2951 s (Printexc.to_string exc)
2953 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2955 | 'l' ->
2956 reqlayout conf.angle (not conf.proportional);
2957 TEdone ("proportional display " ^ btos conf.proportional)
2959 | 'T' ->
2960 settrim (not conf.trimmargins) conf.trimfuzz;
2961 TEdone ("trim margins " ^ btos conf.trimmargins)
2963 | 'I' ->
2964 conf.invert <- not conf.invert;
2965 TEdone ("invert colors " ^ btos conf.invert)
2967 | 'x' ->
2968 let ondone s =
2969 cbput state.hists.sel s;
2970 conf.selcmd <- s;
2972 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2973 textentry, ondone, true)
2975 | _ ->
2976 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2977 TEstop
2978 else
2979 TEcont state.text
2982 class type lvsource = object
2983 method getitemcount : int
2984 method getitem : int -> (string * int)
2985 method hasaction : int -> bool
2986 method exit :
2987 uioh:uioh ->
2988 cancel:bool ->
2989 active:int ->
2990 first:int ->
2991 pan:int ->
2992 qsearch:string ->
2993 uioh option
2994 method getactive : int
2995 method getfirst : int
2996 method getqsearch : string
2997 method setqsearch : string -> unit
2998 method getpan : int
2999 end;;
3001 class virtual lvsourcebase = object
3002 val mutable m_active = 0
3003 val mutable m_first = 0
3004 val mutable m_qsearch = ""
3005 val mutable m_pan = 0
3006 method getactive = m_active
3007 method getfirst = m_first
3008 method getqsearch = m_qsearch
3009 method getpan = m_pan
3010 method setqsearch s = m_qsearch <- s
3011 end;;
3013 let withoutlastutf8 s =
3014 let len = String.length s in
3015 if len = 0
3016 then s
3017 else
3018 let rec find pos =
3019 if pos = 0
3020 then pos
3021 else
3022 let b = Char.code s.[pos] in
3023 if b land 0b11000000 = 0b11000000
3024 then pos
3025 else find (pos-1)
3027 let first =
3028 if Char.code s.[len-1] land 0x80 = 0
3029 then len-1
3030 else find (len-1)
3032 String.sub s 0 first;
3035 let textentrykeyboard
3036 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3037 let key =
3038 if key >= 0xffb0 && key <= 0xffb9
3039 then key - 0xffb0 + 48 else key
3041 let enttext te =
3042 state.mode <- Textentry (te, onleave);
3043 state.text <- "";
3044 enttext ();
3045 G.postRedisplay "textentrykeyboard enttext";
3047 let histaction cmd =
3048 match opthist with
3049 | None -> ()
3050 | Some (action, _) ->
3051 state.mode <- Textentry (
3052 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3054 G.postRedisplay "textentry histaction"
3056 match key with
3057 | 0xff08 -> (* backspace *)
3058 let s = withoutlastutf8 text in
3059 let len = String.length s in
3060 if cancelonempty && len = 0
3061 then (
3062 onleave Cancel;
3063 G.postRedisplay "textentrykeyboard after cancel";
3065 else (
3066 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3069 | 0xff0d | 0xff8d -> (* (kp) enter *)
3070 ondone text;
3071 onleave Confirm;
3072 G.postRedisplay "textentrykeyboard after confirm"
3074 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3075 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3076 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3077 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3079 | 0xff1b -> (* escape*)
3080 if String.length text = 0
3081 then (
3082 begin match opthist with
3083 | None -> ()
3084 | Some (_, onhistcancel) -> onhistcancel ()
3085 end;
3086 onleave Cancel;
3087 state.text <- "";
3088 G.postRedisplay "textentrykeyboard after cancel2"
3090 else (
3091 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3094 | 0xff9f | 0xffff -> () (* delete *)
3096 | _ when key != 0
3097 && key land 0xff00 != 0xff00 (* keyboard *)
3098 && key land 0xfe00 != 0xfe00 (* xkb *)
3099 && key land 0xfd00 != 0xfd00 (* 3270 *)
3101 begin match onkey text key with
3102 | TEdone text ->
3103 ondone text;
3104 onleave Confirm;
3105 G.postRedisplay "textentrykeyboard after confirm2";
3107 | TEcont text ->
3108 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3110 | TEstop ->
3111 onleave Cancel;
3112 G.postRedisplay "textentrykeyboard after cancel3"
3114 | TEswitch te ->
3115 state.mode <- Textentry (te, onleave);
3116 G.postRedisplay "textentrykeyboard switch";
3117 end;
3119 | _ ->
3120 vlog "unhandled key %s" (Wsi.keyname key)
3123 let firstof first active =
3124 if first > active || abs (first - active) > fstate.maxrows - 1
3125 then max 0 (active - (fstate.maxrows/2))
3126 else first
3129 let calcfirst first active =
3130 if active > first
3131 then
3132 let rows = active - first in
3133 if rows > fstate.maxrows then active - fstate.maxrows else first
3134 else active
3137 let scrollph y maxy =
3138 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3139 let sh = float conf.winh /. sh in
3140 let sh = max sh (float conf.scrollh) in
3142 let percent =
3143 if y = state.maxy
3144 then 1.0
3145 else float y /. float maxy
3147 let position = (float conf.winh -. sh) *. percent in
3149 let position =
3150 if position +. sh > float conf.winh
3151 then float conf.winh -. sh
3152 else position
3154 position, sh;
3157 let coe s = (s :> uioh);;
3159 class listview ~(source:lvsource) ~trusted ~modehash =
3160 object (self)
3161 val m_pan = source#getpan
3162 val m_first = source#getfirst
3163 val m_active = source#getactive
3164 val m_qsearch = source#getqsearch
3165 val m_prev_uioh = state.uioh
3167 method private elemunder y =
3168 let n = y / (fstate.fontsize+1) in
3169 if m_first + n < source#getitemcount
3170 then (
3171 if source#hasaction (m_first + n)
3172 then Some (m_first + n)
3173 else None
3175 else None
3177 method display =
3178 Gl.enable `blend;
3179 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3180 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3181 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3182 GlDraw.color (1., 1., 1.);
3183 Gl.enable `texture_2d;
3184 let fs = fstate.fontsize in
3185 let nfs = fs + 1 in
3186 let ww = fstate.wwidth in
3187 let tabw = 30.0*.ww in
3188 let itemcount = source#getitemcount in
3189 let rec loop row =
3190 if (row - m_first) > fstate.maxrows
3191 then ()
3192 else (
3193 if row >= 0 && row < itemcount
3194 then (
3195 let (s, level) = source#getitem row in
3196 let y = (row - m_first) * nfs in
3197 let x = 5.0 +. float (level + m_pan) *. ww in
3198 if row = m_active
3199 then (
3200 Gl.disable `texture_2d;
3201 GlDraw.polygon_mode `both `line;
3202 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3203 GlDraw.rect (1., float (y + 1))
3204 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3205 GlDraw.polygon_mode `both `fill;
3206 GlDraw.color (1., 1., 1.);
3207 Gl.enable `texture_2d;
3210 let drawtabularstring s =
3211 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3212 if trusted
3213 then
3214 let tabpos = try String.index s '\t' with Not_found -> -1 in
3215 if tabpos > 0
3216 then
3217 let len = String.length s - tabpos - 1 in
3218 let s1 = String.sub s 0 tabpos
3219 and s2 = String.sub s (tabpos + 1) len in
3220 let nx = drawstr x s1 in
3221 let sw = nx -. x in
3222 let x = x +. (max tabw sw) in
3223 drawstr x s2
3224 else
3225 drawstr x s
3226 else
3227 drawstr x s
3229 let _ = drawtabularstring s in
3230 loop (row+1)
3234 loop m_first;
3235 Gl.disable `blend;
3236 Gl.disable `texture_2d;
3238 method updownlevel incr =
3239 let len = source#getitemcount in
3240 let curlevel =
3241 if m_active >= 0 && m_active < len
3242 then snd (source#getitem m_active)
3243 else -1
3245 let rec flow i =
3246 if i = len then i-1 else if i = -1 then 0 else
3247 let _, l = source#getitem i in
3248 if l != curlevel then i else flow (i+incr)
3250 let active = flow m_active in
3251 let first = calcfirst m_first active in
3252 G.postRedisplay "outline updownlevel";
3253 {< m_active = active; m_first = first >}
3255 method private key1 key mask =
3256 let set1 active first qsearch =
3257 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3259 let search active pattern incr =
3260 let dosearch re =
3261 let rec loop n =
3262 if n >= 0 && n < source#getitemcount
3263 then (
3264 let s, _ = source#getitem n in
3266 (try ignore (Str.search_forward re s 0); true
3267 with Not_found -> false)
3268 then Some n
3269 else loop (n + incr)
3271 else None
3273 loop active
3276 let re = Str.regexp_case_fold pattern in
3277 dosearch re
3278 with Failure s ->
3279 state.text <- s;
3280 None
3282 let itemcount = source#getitemcount in
3283 let find start incr =
3284 let rec find i =
3285 if i = -1 || i = itemcount
3286 then -1
3287 else (
3288 if source#hasaction i
3289 then i
3290 else find (i + incr)
3293 find start
3295 let set active first =
3296 let first = bound first 0 (itemcount - fstate.maxrows) in
3297 state.text <- "";
3298 coe {< m_active = active; m_first = first >}
3300 let navigate incr =
3301 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3302 let active, first =
3303 let incr1 = if incr > 0 then 1 else -1 in
3304 if isvisible m_first m_active
3305 then
3306 let next =
3307 let next = m_active + incr in
3308 let next =
3309 if next < 0 || next >= itemcount
3310 then -1
3311 else find next incr1
3313 if next = -1 || abs (m_active - next) > fstate.maxrows
3314 then -1
3315 else next
3317 if next = -1
3318 then
3319 let first = m_first + incr in
3320 let first = bound first 0 (itemcount - 1) in
3321 let next =
3322 let next = m_active + incr in
3323 let next = bound next 0 (itemcount - 1) in
3324 find next ~-incr1
3326 let active = if next = -1 then m_active else next in
3327 active, first
3328 else
3329 let first = min next m_first in
3330 let first =
3331 if abs (next - first) > fstate.maxrows
3332 then first + incr
3333 else first
3335 next, first
3336 else
3337 let first = m_first + incr in
3338 let first = bound first 0 (itemcount - 1) in
3339 let active =
3340 let next = m_active + incr in
3341 let next = bound next 0 (itemcount - 1) in
3342 let next = find next incr1 in
3343 let active =
3344 if next = -1 || abs (m_active - first) > fstate.maxrows
3345 then (
3346 let active = if m_active = -1 then next else m_active in
3347 active
3349 else next
3351 if isvisible first active
3352 then active
3353 else -1
3355 active, first
3357 G.postRedisplay "listview navigate";
3358 set active first;
3360 match key with
3361 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3362 let incr = if key = 0x72 then -1 else 1 in
3363 let active, first =
3364 match search (m_active + incr) m_qsearch incr with
3365 | None ->
3366 state.text <- m_qsearch ^ " [not found]";
3367 m_active, m_first
3368 | Some active ->
3369 state.text <- m_qsearch;
3370 active, firstof m_first active
3372 G.postRedisplay "listview ctrl-r/s";
3373 set1 active first m_qsearch;
3375 | 0xff08 -> (* backspace *)
3376 if String.length m_qsearch = 0
3377 then coe self
3378 else (
3379 let qsearch = withoutlastutf8 m_qsearch in
3380 let len = String.length qsearch in
3381 if len = 0
3382 then (
3383 state.text <- "";
3384 G.postRedisplay "listview empty qsearch";
3385 set1 m_active m_first "";
3387 else
3388 let active, first =
3389 match search m_active qsearch ~-1 with
3390 | None ->
3391 state.text <- qsearch ^ " [not found]";
3392 m_active, m_first
3393 | Some active ->
3394 state.text <- qsearch;
3395 active, firstof m_first active
3397 G.postRedisplay "listview backspace qsearch";
3398 set1 active first qsearch
3401 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3402 let pattern = m_qsearch ^ Wsi.toutf8 key in
3403 let active, first =
3404 match search m_active pattern 1 with
3405 | None ->
3406 state.text <- pattern ^ " [not found]";
3407 m_active, m_first
3408 | Some active ->
3409 state.text <- pattern;
3410 active, firstof m_first active
3412 G.postRedisplay "listview qsearch add";
3413 set1 active first pattern;
3415 | 0xff1b -> (* escape *)
3416 state.text <- "";
3417 if String.length m_qsearch = 0
3418 then (
3419 G.postRedisplay "list view escape";
3420 begin
3421 match
3422 source#exit (coe self) true m_active m_first m_pan m_qsearch
3423 with
3424 | None -> m_prev_uioh
3425 | Some uioh -> uioh
3428 else (
3429 G.postRedisplay "list view kill qsearch";
3430 source#setqsearch "";
3431 coe {< m_qsearch = "" >}
3434 | 0xff0d | 0xff8d -> (* (kp) enter *)
3435 state.text <- "";
3436 let self = {< m_qsearch = "" >} in
3437 source#setqsearch "";
3438 let opt =
3439 G.postRedisplay "listview enter";
3440 if m_active >= 0 && m_active < source#getitemcount
3441 then (
3442 source#exit (coe self) false m_active m_first m_pan "";
3444 else (
3445 source#exit (coe self) true m_active m_first m_pan "";
3448 begin match opt with
3449 | None -> m_prev_uioh
3450 | Some uioh -> uioh
3453 | 0xff9f | 0xffff -> (* (kp) delete *)
3454 coe self
3456 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3457 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3458 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3459 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3461 | 0xff53 | 0xff98 -> (* (kp) right *)
3462 state.text <- "";
3463 G.postRedisplay "listview right";
3464 coe {< m_pan = m_pan - 1 >}
3466 | 0xff51 | 0xff96 -> (* (kp) left *)
3467 state.text <- "";
3468 G.postRedisplay "listview left";
3469 coe {< m_pan = m_pan + 1 >}
3471 | 0xff50 | 0xff95 -> (* (kp) home *)
3472 let active = find 0 1 in
3473 G.postRedisplay "listview home";
3474 set active 0;
3476 | 0xff57 | 0xff9c -> (* (kp) end *)
3477 let first = max 0 (itemcount - fstate.maxrows) in
3478 let active = find (itemcount - 1) ~-1 in
3479 G.postRedisplay "listview end";
3480 set active first;
3482 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3483 coe self
3485 | _ ->
3486 dolog "listview unknown key %#x" key; coe self
3488 method key key mask =
3489 match state.mode with
3490 | Textentry te -> textentrykeyboard key mask te; coe self
3491 | _ -> self#key1 key mask
3493 method button button down x y _ =
3494 let opt =
3495 match button with
3496 | 1 when x > conf.winw - conf.scrollbw ->
3497 G.postRedisplay "listview scroll";
3498 if down
3499 then
3500 let _, position, sh = self#scrollph in
3501 if y > truncate position && y < truncate (position +. sh)
3502 then (
3503 state.mstate <- Mscrolly;
3504 Some (coe self)
3506 else
3507 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3508 let first = truncate (s *. float source#getitemcount) in
3509 let first = min source#getitemcount first in
3510 Some (coe {< m_first = first; m_active = first >})
3511 else (
3512 state.mstate <- Mnone;
3513 Some (coe self);
3515 | 1 when not down ->
3516 begin match self#elemunder y with
3517 | Some n ->
3518 G.postRedisplay "listview click";
3519 source#exit
3520 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3521 | _ ->
3522 Some (coe self)
3524 | n when (n == 4 || n == 5) && not down ->
3525 let len = source#getitemcount in
3526 let first =
3527 if n = 5 && m_first + fstate.maxrows >= len
3528 then
3529 m_first
3530 else
3531 let first = m_first + (if n == 4 then -1 else 1) in
3532 bound first 0 (len - 1)
3534 G.postRedisplay "listview wheel";
3535 Some (coe {< m_first = first >})
3536 | n when (n = 6 || n = 7) && not down ->
3537 let inc = m_first + (if n = 7 then -1 else 1) in
3538 G.postRedisplay "listview hwheel";
3539 Some (coe {< m_pan = m_pan + inc >})
3540 | _ ->
3541 Some (coe self)
3543 match opt with
3544 | None -> m_prev_uioh
3545 | Some uioh -> uioh
3547 method motion _ y =
3548 match state.mstate with
3549 | Mscrolly ->
3550 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3551 let first = truncate (s *. float source#getitemcount) in
3552 let first = min source#getitemcount first in
3553 G.postRedisplay "listview motion";
3554 coe {< m_first = first; m_active = first >}
3555 | _ -> coe self
3557 method pmotion x y =
3558 if x < conf.winw - conf.scrollbw
3559 then
3560 let n =
3561 match self#elemunder y with
3562 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3563 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3565 let o =
3566 if n != m_active
3567 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3568 else self
3570 coe o
3571 else (
3572 Wsi.setcursor Wsi.CURSOR_INHERIT;
3573 coe self
3576 method infochanged _ = ()
3578 method scrollpw = (0, 0.0, 0.0)
3579 method scrollph =
3580 let nfs = fstate.fontsize + 1 in
3581 let y = m_first * nfs in
3582 let itemcount = source#getitemcount in
3583 let maxi = max 0 (itemcount - fstate.maxrows) in
3584 let maxy = maxi * nfs in
3585 let p, h = scrollph y maxy in
3586 conf.scrollbw, p, h
3588 method modehash = modehash
3589 end;;
3591 class outlinelistview ~source =
3592 object (self)
3593 inherit listview
3594 ~source:(source :> lvsource)
3595 ~trusted:false
3596 ~modehash:(findkeyhash conf "outline")
3597 as super
3599 method key key mask =
3600 let calcfirst first active =
3601 if active > first
3602 then
3603 let rows = active - first in
3604 let maxrows =
3605 if String.length state.text = 0
3606 then fstate.maxrows
3607 else fstate.maxrows - 2
3609 if rows > maxrows then active - maxrows else first
3610 else active
3612 let navigate incr =
3613 let active = m_active + incr in
3614 let active = bound active 0 (source#getitemcount - 1) in
3615 let first = calcfirst m_first active in
3616 G.postRedisplay "outline navigate";
3617 coe {< m_active = active; m_first = first >}
3619 let ctrl = Wsi.withctrl mask in
3620 match key with
3621 | 110 when ctrl -> (* ctrl-n *)
3622 source#narrow m_qsearch;
3623 G.postRedisplay "outline ctrl-n";
3624 coe {< m_first = 0; m_active = 0 >}
3626 | 117 when ctrl -> (* ctrl-u *)
3627 source#denarrow;
3628 G.postRedisplay "outline ctrl-u";
3629 state.text <- "";
3630 coe {< m_first = 0; m_active = 0 >}
3632 | 108 when ctrl -> (* ctrl-l *)
3633 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3634 G.postRedisplay "outline ctrl-l";
3635 coe {< m_first = first >}
3637 | 0xff9f | 0xffff -> (* (kp) delete *)
3638 source#remove m_active;
3639 G.postRedisplay "outline delete";
3640 let active = max 0 (m_active-1) in
3641 coe {< m_first = firstof m_first active;
3642 m_active = active >}
3644 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3645 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3646 | 0xff55 | 0xff9a -> (* (kp) prior *)
3647 navigate ~-(fstate.maxrows)
3648 | 0xff56 | 0xff9b -> (* (kp) next *)
3649 navigate fstate.maxrows
3651 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3652 let o =
3653 if ctrl
3654 then (
3655 G.postRedisplay "outline ctrl right";
3656 {< m_pan = m_pan + 1 >}
3658 else self#updownlevel 1
3660 coe o
3662 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3663 let o =
3664 if ctrl
3665 then (
3666 G.postRedisplay "outline ctrl left";
3667 {< m_pan = m_pan - 1 >}
3669 else self#updownlevel ~-1
3671 coe o
3673 | 0xff50 | 0xff95 -> (* (kp) home *)
3674 G.postRedisplay "outline home";
3675 coe {< m_first = 0; m_active = 0 >}
3677 | 0xff57 | 0xff9c -> (* (kp) end *)
3678 let active = source#getitemcount - 1 in
3679 let first = max 0 (active - fstate.maxrows) in
3680 G.postRedisplay "outline end";
3681 coe {< m_active = active; m_first = first >}
3683 | _ -> super#key key mask
3686 let outlinesource usebookmarks =
3687 let empty = [||] in
3688 (object
3689 inherit lvsourcebase
3690 val mutable m_items = empty
3691 val mutable m_orig_items = empty
3692 val mutable m_prev_items = empty
3693 val mutable m_narrow_pattern = ""
3694 val mutable m_hadremovals = false
3696 method getitemcount =
3697 Array.length m_items + (if m_hadremovals then 1 else 0)
3699 method getitem n =
3700 if n == Array.length m_items && m_hadremovals
3701 then
3702 ("[Confirm removal]", 0)
3703 else
3704 let s, n, _ = m_items.(n) in
3705 (s, n)
3707 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3708 ignore (uioh, first, qsearch);
3709 let confrimremoval = m_hadremovals && active = Array.length m_items in
3710 let items =
3711 if String.length m_narrow_pattern = 0
3712 then m_orig_items
3713 else m_items
3715 if not cancel
3716 then (
3717 if not confrimremoval
3718 then(
3719 let _, _, anchor = m_items.(active) in
3720 gotoghyll (getanchory anchor);
3721 m_items <- items;
3723 else (
3724 state.bookmarks <- Array.to_list m_items;
3725 m_orig_items <- m_items;
3728 else m_items <- items;
3729 m_pan <- pan;
3730 None
3732 method hasaction _ = true
3734 method greetmsg =
3735 if Array.length m_items != Array.length m_orig_items
3736 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3737 else ""
3739 method narrow pattern =
3740 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3741 match reopt with
3742 | None -> ()
3743 | Some re ->
3744 let rec loop accu n =
3745 if n = -1
3746 then (
3747 m_narrow_pattern <- pattern;
3748 m_items <- Array.of_list accu
3750 else
3751 let (s, _, _) as o = m_items.(n) in
3752 let accu =
3753 if (try ignore (Str.search_forward re s 0); true
3754 with Not_found -> false)
3755 then o :: accu
3756 else accu
3758 loop accu (n-1)
3760 loop [] (Array.length m_items - 1)
3762 method denarrow =
3763 m_orig_items <- (
3764 if usebookmarks
3765 then Array.of_list state.bookmarks
3766 else state.outlines
3768 m_items <- m_orig_items
3770 method remove m =
3771 if usebookmarks
3772 then
3773 if m >= 0 && m < Array.length m_items
3774 then (
3775 m_hadremovals <- true;
3776 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3777 let n = if n >= m then n+1 else n in
3778 m_items.(n)
3782 method reset anchor items =
3783 m_hadremovals <- false;
3784 if m_orig_items == empty || m_prev_items != items
3785 then (
3786 m_orig_items <- items;
3787 if String.length m_narrow_pattern = 0
3788 then m_items <- items;
3790 m_prev_items <- items;
3791 let rely = getanchory anchor in
3792 let active =
3793 let rec loop n best bestd =
3794 if n = Array.length m_items
3795 then best
3796 else
3797 let (_, _, anchor) = m_items.(n) in
3798 let orely = getanchory anchor in
3799 let d = abs (orely - rely) in
3800 if d < bestd
3801 then loop (n+1) n d
3802 else loop (n+1) best bestd
3804 loop 0 ~-1 max_int
3806 m_active <- active;
3807 m_first <- firstof m_first active
3808 end)
3811 let enterselector usebookmarks =
3812 let source = outlinesource usebookmarks in
3813 fun errmsg ->
3814 let outlines =
3815 if usebookmarks
3816 then Array.of_list state.bookmarks
3817 else state.outlines
3819 if Array.length outlines = 0
3820 then (
3821 showtext ' ' errmsg;
3823 else (
3824 state.text <- source#greetmsg;
3825 Wsi.setcursor Wsi.CURSOR_INHERIT;
3826 let anchor = getanchor () in
3827 source#reset anchor outlines;
3828 state.uioh <- coe (new outlinelistview ~source);
3829 G.postRedisplay "enter selector";
3833 let enteroutlinemode =
3834 let f = enterselector false in
3835 fun ()-> f "Document has no outline";
3838 let enterbookmarkmode =
3839 let f = enterselector true in
3840 fun () -> f "Document has no bookmarks (yet)";
3843 let color_of_string s =
3844 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3845 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3849 let color_to_string (r, g, b) =
3850 let r = truncate (r *. 256.0)
3851 and g = truncate (g *. 256.0)
3852 and b = truncate (b *. 256.0) in
3853 Printf.sprintf "%d/%d/%d" r g b
3856 let irect_of_string s =
3857 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3860 let irect_to_string (x0,y0,x1,y1) =
3861 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3864 let makecheckers () =
3865 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3866 following to say:
3867 converted by Issac Trotts. July 25, 2002 *)
3868 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3869 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3870 let id = GlTex.gen_texture () in
3871 GlTex.bind_texture `texture_2d id;
3872 GlPix.store (`unpack_alignment 1);
3873 GlTex.image2d image;
3874 List.iter (GlTex.parameter ~target:`texture_2d)
3875 [ `mag_filter `nearest; `min_filter `nearest ];
3879 let setcheckers enabled =
3880 match state.texid with
3881 | None ->
3882 if enabled then state.texid <- Some (makecheckers ())
3884 | Some texid ->
3885 if not enabled
3886 then (
3887 GlTex.delete_texture texid;
3888 state.texid <- None;
3892 let int_of_string_with_suffix s =
3893 let l = String.length s in
3894 let s1, shift =
3895 if l > 1
3896 then
3897 let suffix = Char.lowercase s.[l-1] in
3898 match suffix with
3899 | 'k' -> String.sub s 0 (l-1), 10
3900 | 'm' -> String.sub s 0 (l-1), 20
3901 | 'g' -> String.sub s 0 (l-1), 30
3902 | _ -> s, 0
3903 else s, 0
3905 let n = int_of_string s1 in
3906 let m = n lsl shift in
3907 if m < 0 || m < n
3908 then raise (Failure "value too large")
3909 else m
3912 let string_with_suffix_of_int n =
3913 if n = 0
3914 then "0"
3915 else
3916 let n, s =
3917 if n land ((1 lsl 30) - 1) = 0
3918 then n lsr 30, "G"
3919 else (
3920 if n land ((1 lsl 20) - 1) = 0
3921 then n lsr 20, "M"
3922 else (
3923 if n land ((1 lsl 10) - 1) = 0
3924 then n lsr 10, "K"
3925 else n, ""
3929 let rec loop s n =
3930 let h = n mod 1000 in
3931 let n = n / 1000 in
3932 if n = 0
3933 then string_of_int h ^ s
3934 else (
3935 let s = Printf.sprintf "_%03d%s" h s in
3936 loop s n
3939 loop "" n ^ s;
3942 let defghyllscroll = (40, 8, 32);;
3943 let ghyllscroll_of_string s =
3944 let (n, a, b) as nab =
3945 if s = "default"
3946 then defghyllscroll
3947 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3949 if n <= a || n <= b || a >= b
3950 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3951 nab;
3954 let ghyllscroll_to_string ((n, a, b) as nab) =
3955 if nab = defghyllscroll
3956 then "default"
3957 else Printf.sprintf "%d,%d,%d" n a b;
3960 let describe_location () =
3961 let f (fn, _) l =
3962 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3964 let fn, ln = List.fold_left f (-1, -1) state.layout in
3965 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3966 let percent =
3967 if maxy <= 0
3968 then 100.
3969 else (100. *. (float state.y /. float maxy))
3971 if fn = ln
3972 then
3973 Printf.sprintf "page %d of %d [%.2f%%]"
3974 (fn+1) state.pagecount percent
3975 else
3976 Printf.sprintf
3977 "pages %d-%d of %d [%.2f%%]"
3978 (fn+1) (ln+1) state.pagecount percent
3981 let setpresentationmode v =
3982 let (n, _, _) = getanchor () in
3983 let _, h = getpageyh n in
3984 let ips = if conf.presentation then calcips h else conf.interpagespace in
3985 state.anchor <- (n, 0.0, float ips);
3986 conf.presentation <- v;
3987 if conf.presentation
3988 then (
3989 if not conf.scrollbarinpm
3990 then state.scrollw <- 0;
3992 else state.scrollw <- conf.scrollbw;
3993 represent ();
3996 let enterinfomode =
3997 let btos b = if b then "\xe2\x88\x9a" else "" in
3998 let showextended = ref false in
3999 let leave mode = function
4000 | Confirm -> state.mode <- mode
4001 | Cancel -> state.mode <- mode in
4002 let src =
4003 (object
4004 val mutable m_first_time = true
4005 val mutable m_l = []
4006 val mutable m_a = [||]
4007 val mutable m_prev_uioh = nouioh
4008 val mutable m_prev_mode = View
4010 inherit lvsourcebase
4012 method reset prev_mode prev_uioh =
4013 m_a <- Array.of_list (List.rev m_l);
4014 m_l <- [];
4015 m_prev_mode <- prev_mode;
4016 m_prev_uioh <- prev_uioh;
4017 if m_first_time
4018 then (
4019 let rec loop n =
4020 if n >= Array.length m_a
4021 then ()
4022 else
4023 match m_a.(n) with
4024 | _, _, _, Action _ -> m_active <- n
4025 | _ -> loop (n+1)
4027 loop 0;
4028 m_first_time <- false;
4031 method int name get set =
4032 m_l <-
4033 (name, `int get, 1, Action (
4034 fun u ->
4035 let ondone s =
4036 try set (int_of_string s)
4037 with exn ->
4038 state.text <- Printf.sprintf "bad integer `%s': %s"
4039 s (Printexc.to_string exn)
4041 state.text <- "";
4042 let te = name ^ ": ", "", None, intentry, ondone, true in
4043 state.mode <- Textentry (te, leave m_prev_mode);
4045 )) :: m_l
4047 method int_with_suffix name get set =
4048 m_l <-
4049 (name, `intws get, 1, Action (
4050 fun u ->
4051 let ondone s =
4052 try set (int_of_string_with_suffix s)
4053 with exn ->
4054 state.text <- Printf.sprintf "bad integer `%s': %s"
4055 s (Printexc.to_string exn)
4057 state.text <- "";
4058 let te =
4059 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4061 state.mode <- Textentry (te, leave m_prev_mode);
4063 )) :: m_l
4065 method bool ?(offset=1) ?(btos=btos) name get set =
4066 m_l <-
4067 (name, `bool (btos, get), offset, Action (
4068 fun u ->
4069 let v = get () in
4070 set (not v);
4072 )) :: m_l
4074 method color name get set =
4075 m_l <-
4076 (name, `color get, 1, Action (
4077 fun u ->
4078 let invalid = (nan, nan, nan) in
4079 let ondone s =
4080 let c =
4081 try color_of_string s
4082 with exn ->
4083 state.text <- Printf.sprintf "bad color `%s': %s"
4084 s (Printexc.to_string exn);
4085 invalid
4087 if c <> invalid
4088 then set c;
4090 let te = name ^ ": ", "", None, textentry, ondone, true in
4091 state.text <- color_to_string (get ());
4092 state.mode <- Textentry (te, leave m_prev_mode);
4094 )) :: m_l
4096 method string name get set =
4097 m_l <-
4098 (name, `string get, 1, Action (
4099 fun u ->
4100 let ondone s = set s in
4101 let te = name ^ ": ", "", None, textentry, ondone, true in
4102 state.mode <- Textentry (te, leave m_prev_mode);
4104 )) :: m_l
4106 method colorspace name get set =
4107 m_l <-
4108 (name, `string get, 1, Action (
4109 fun _ ->
4110 let source =
4111 let vals = [| "rgb"; "bgr"; "gray" |] in
4112 (object
4113 inherit lvsourcebase
4115 initializer
4116 m_active <- int_of_colorspace conf.colorspace;
4117 m_first <- 0;
4119 method getitemcount = Array.length vals
4120 method getitem n = (vals.(n), 0)
4121 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4122 ignore (uioh, first, pan, qsearch);
4123 if not cancel then set active;
4124 None
4125 method hasaction _ = true
4126 end)
4128 state.text <- "";
4129 let modehash = findkeyhash conf "info" in
4130 coe (new listview ~source ~trusted:true ~modehash)
4131 )) :: m_l
4133 method caption s offset =
4134 m_l <- (s, `empty, offset, Noaction) :: m_l
4136 method caption2 s f offset =
4137 m_l <- (s, `string f, offset, Noaction) :: m_l
4139 method getitemcount = Array.length m_a
4141 method getitem n =
4142 let tostr = function
4143 | `int f -> string_of_int (f ())
4144 | `intws f -> string_with_suffix_of_int (f ())
4145 | `string f -> f ()
4146 | `color f -> color_to_string (f ())
4147 | `bool (btos, f) -> btos (f ())
4148 | `empty -> ""
4150 let name, t, offset, _ = m_a.(n) in
4151 ((let s = tostr t in
4152 if String.length s > 0
4153 then Printf.sprintf "%s\t%s" name s
4154 else name),
4155 offset)
4157 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4158 let uiohopt =
4159 if not cancel
4160 then (
4161 m_qsearch <- qsearch;
4162 let uioh =
4163 match m_a.(active) with
4164 | _, _, _, Action f -> f uioh
4165 | _ -> uioh
4167 Some uioh
4169 else None
4171 m_active <- active;
4172 m_first <- first;
4173 m_pan <- pan;
4174 uiohopt
4176 method hasaction n =
4177 match m_a.(n) with
4178 | _, _, _, Action _ -> true
4179 | _ -> false
4180 end)
4182 let rec fillsrc prevmode prevuioh =
4183 let sep () = src#caption "" 0 in
4184 let colorp name get set =
4185 src#string name
4186 (fun () -> color_to_string (get ()))
4187 (fun v ->
4189 let c = color_of_string v in
4190 set c
4191 with exn ->
4192 state.text <- Printf.sprintf "bad color `%s': %s"
4193 v (Printexc.to_string exn);
4196 let oldmode = state.mode in
4197 let birdseye = isbirdseye state.mode in
4199 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4201 src#bool "presentation mode"
4202 (fun () -> conf.presentation)
4203 (fun v -> setpresentationmode v);
4205 src#bool "ignore case in searches"
4206 (fun () -> conf.icase)
4207 (fun v -> conf.icase <- v);
4209 src#bool "preload"
4210 (fun () -> conf.preload)
4211 (fun v -> conf.preload <- v);
4213 src#bool "highlight links"
4214 (fun () -> conf.hlinks)
4215 (fun v -> conf.hlinks <- v);
4217 src#bool "under info"
4218 (fun () -> conf.underinfo)
4219 (fun v -> conf.underinfo <- v);
4221 src#bool "persistent bookmarks"
4222 (fun () -> conf.savebmarks)
4223 (fun v -> conf.savebmarks <- v);
4225 src#bool "proportional display"
4226 (fun () -> conf.proportional)
4227 (fun v -> reqlayout conf.angle v);
4229 src#bool "trim margins"
4230 (fun () -> conf.trimmargins)
4231 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4233 src#bool "persistent location"
4234 (fun () -> conf.jumpback)
4235 (fun v -> conf.jumpback <- v);
4237 sep ();
4238 src#int "inter-page space"
4239 (fun () -> conf.interpagespace)
4240 (fun n ->
4241 conf.interpagespace <- n;
4242 docolumns conf.columns;
4243 let pageno, py =
4244 match state.layout with
4245 | [] -> 0, 0
4246 | l :: _ ->
4247 l.pageno, l.pagey
4249 state.maxy <- calcheight ();
4250 let y = getpagey pageno in
4251 gotoy (y + py)
4254 src#int "page bias"
4255 (fun () -> conf.pagebias)
4256 (fun v -> conf.pagebias <- v);
4258 src#int "scroll step"
4259 (fun () -> conf.scrollstep)
4260 (fun n -> conf.scrollstep <- n);
4262 src#int "horizontal scroll step"
4263 (fun () -> conf.hscrollstep)
4264 (fun v -> conf.hscrollstep <- v);
4266 src#int "auto scroll step"
4267 (fun () ->
4268 match state.autoscroll with
4269 | Some step -> step
4270 | _ -> conf.autoscrollstep)
4271 (fun n ->
4272 if state.autoscroll <> None
4273 then state.autoscroll <- Some n;
4274 conf.autoscrollstep <- n);
4276 src#int "zoom"
4277 (fun () -> truncate (conf.zoom *. 100.))
4278 (fun v -> setzoom ((float v) /. 100.));
4280 src#int "rotation"
4281 (fun () -> conf.angle)
4282 (fun v -> reqlayout v conf.proportional);
4284 src#int "scroll bar width"
4285 (fun () -> state.scrollw)
4286 (fun v ->
4287 state.scrollw <- v;
4288 conf.scrollbw <- v;
4289 reshape conf.winw conf.winh;
4292 src#int "scroll handle height"
4293 (fun () -> conf.scrollh)
4294 (fun v -> conf.scrollh <- v;);
4296 src#int "thumbnail width"
4297 (fun () -> conf.thumbw)
4298 (fun v ->
4299 conf.thumbw <- min 4096 v;
4300 match oldmode with
4301 | Birdseye beye ->
4302 leavebirdseye beye false;
4303 enterbirdseye ()
4304 | _ -> ()
4307 let mode = state.mode in
4308 src#string "columns"
4309 (fun () ->
4310 match conf.columns with
4311 | Csingle _ -> "1"
4312 | Cmulti (multi, _) -> multicolumns_to_string multi
4313 | Csplit (count, _) -> "-" ^ string_of_int count
4315 (fun v ->
4316 let n, a, b = multicolumns_of_string v in
4317 setcolumns mode n a b);
4319 sep ();
4320 src#caption "Presentation mode" 0;
4321 src#bool "scrollbar visible"
4322 (fun () -> conf.scrollbarinpm)
4323 (fun v ->
4324 if v != conf.scrollbarinpm
4325 then (
4326 conf.scrollbarinpm <- v;
4327 if conf.presentation
4328 then (
4329 state.scrollw <- if v then conf.scrollbw else 0;
4330 reshape conf.winw conf.winh;
4335 sep ();
4336 src#caption "Pixmap cache" 0;
4337 src#int_with_suffix "size (advisory)"
4338 (fun () -> conf.memlimit)
4339 (fun v -> conf.memlimit <- v);
4341 src#caption2 "used"
4342 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4343 (string_with_suffix_of_int state.memused)
4344 (Hashtbl.length state.tilemap)) 1;
4346 sep ();
4347 src#caption "Layout" 0;
4348 src#caption2 "Dimension"
4349 (fun () ->
4350 Printf.sprintf "%dx%d (virtual %dx%d)"
4351 conf.winw conf.winh
4352 state.w state.maxy)
4354 if conf.debug
4355 then
4356 src#caption2 "Position" (fun () ->
4357 Printf.sprintf "%dx%d" state.x state.y
4359 else
4360 src#caption2 "Visible" (fun () -> describe_location ()) 1
4363 sep ();
4364 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4365 "Save these parameters as global defaults at exit"
4366 (fun () -> conf.bedefault)
4367 (fun v -> conf.bedefault <- v)
4370 sep ();
4371 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4372 src#bool ~offset:0 ~btos "Extended parameters"
4373 (fun () -> !showextended)
4374 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4375 if !showextended
4376 then (
4377 src#bool "checkers"
4378 (fun () -> conf.checkers)
4379 (fun v -> conf.checkers <- v; setcheckers v);
4380 src#bool "update cursor"
4381 (fun () -> conf.updatecurs)
4382 (fun v -> conf.updatecurs <- v);
4383 src#bool "verbose"
4384 (fun () -> conf.verbose)
4385 (fun v -> conf.verbose <- v);
4386 src#bool "invert colors"
4387 (fun () -> conf.invert)
4388 (fun v -> conf.invert <- v);
4389 src#bool "max fit"
4390 (fun () -> conf.maxhfit)
4391 (fun v -> conf.maxhfit <- v);
4392 src#bool "redirect stderr"
4393 (fun () -> conf.redirectstderr)
4394 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4395 src#string "uri launcher"
4396 (fun () -> conf.urilauncher)
4397 (fun v -> conf.urilauncher <- v);
4398 src#string "path launcher"
4399 (fun () -> conf.pathlauncher)
4400 (fun v -> conf.pathlauncher <- v);
4401 src#string "tile size"
4402 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4403 (fun v ->
4405 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4406 conf.tilew <- max 64 w;
4407 conf.tileh <- max 64 h;
4408 flushtiles ();
4409 with exn ->
4410 state.text <- Printf.sprintf "bad tile size `%s': %s"
4411 v (Printexc.to_string exn));
4412 src#int "texture count"
4413 (fun () -> conf.texcount)
4414 (fun v ->
4415 if realloctexts v
4416 then conf.texcount <- v
4417 else showtext '!' " Failed to set texture count please retry later"
4419 src#int "slice height"
4420 (fun () -> conf.sliceheight)
4421 (fun v ->
4422 conf.sliceheight <- v;
4423 wcmd "sliceh %d" conf.sliceheight;
4425 src#int "anti-aliasing level"
4426 (fun () -> conf.aalevel)
4427 (fun v ->
4428 conf.aalevel <- bound v 0 8;
4429 state.anchor <- getanchor ();
4430 opendoc state.path state.password;
4432 src#string "page scroll scaling factor"
4433 (fun () -> string_of_float conf.pgscale)
4434 (fun v ->
4436 let s = float_of_string v in
4437 conf.pgscale <- s
4438 with exn ->
4439 state.text <- Printf.sprintf
4440 "bad page scroll scaling factor `%s': %s"
4441 v (Printexc.to_string exn)
4444 src#int "ui font size"
4445 (fun () -> fstate.fontsize)
4446 (fun v -> setfontsize (bound v 5 100));
4447 src#int "hint font size"
4448 (fun () -> conf.hfsize)
4449 (fun v -> conf.hfsize <- bound v 5 100);
4450 colorp "background color"
4451 (fun () -> conf.bgcolor)
4452 (fun v -> conf.bgcolor <- v);
4453 src#bool "crop hack"
4454 (fun () -> conf.crophack)
4455 (fun v -> conf.crophack <- v);
4456 src#string "trim fuzz"
4457 (fun () -> irect_to_string conf.trimfuzz)
4458 (fun v ->
4460 conf.trimfuzz <- irect_of_string v;
4461 if conf.trimmargins
4462 then settrim true conf.trimfuzz;
4463 with exn ->
4464 state.text <- Printf.sprintf "bad irect `%s': %s"
4465 v (Printexc.to_string exn)
4467 src#string "throttle"
4468 (fun () ->
4469 match conf.maxwait with
4470 | None -> "show place holder if page is not ready"
4471 | Some time ->
4472 if time = infinity
4473 then "wait for page to fully render"
4474 else
4475 "wait " ^ string_of_float time
4476 ^ " seconds before showing placeholder"
4478 (fun v ->
4480 let f = float_of_string v in
4481 if f <= 0.0
4482 then conf.maxwait <- None
4483 else conf.maxwait <- Some f
4484 with exn ->
4485 state.text <- Printf.sprintf "bad time `%s': %s"
4486 v (Printexc.to_string exn)
4488 src#string "ghyll scroll"
4489 (fun () ->
4490 match conf.ghyllscroll with
4491 | None -> ""
4492 | Some nab -> ghyllscroll_to_string nab
4494 (fun v ->
4496 let gs =
4497 if String.length v = 0
4498 then None
4499 else Some (ghyllscroll_of_string v)
4501 conf.ghyllscroll <- gs
4502 with exn ->
4503 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4504 v (Printexc.to_string exn)
4506 src#string "selection command"
4507 (fun () -> conf.selcmd)
4508 (fun v -> conf.selcmd <- v);
4509 src#string "synctex command"
4510 (fun () -> conf.stcmd)
4511 (fun v -> conf.stcmd <- v);
4512 src#colorspace "color space"
4513 (fun () -> colorspace_to_string conf.colorspace)
4514 (fun v ->
4515 conf.colorspace <- colorspace_of_int v;
4516 wcmd "cs %d" v;
4517 load state.layout;
4519 if pbousable ()
4520 then
4521 src#bool "use PBO"
4522 (fun () -> conf.usepbo)
4523 (fun v -> conf.usepbo <- v);
4524 src#bool "mouse wheel scrolls pages"
4525 (fun () -> conf.wheelbypage)
4526 (fun v -> conf.wheelbypage <- v);
4529 sep ();
4530 src#caption "Document" 0;
4531 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4532 src#caption2 "Pages"
4533 (fun () -> string_of_int state.pagecount) 1;
4534 src#caption2 "Dimensions"
4535 (fun () -> string_of_int (List.length state.pdims)) 1;
4536 if conf.trimmargins
4537 then (
4538 sep ();
4539 src#caption "Trimmed margins" 0;
4540 src#caption2 "Dimensions"
4541 (fun () -> string_of_int (List.length state.pdims)) 1;
4544 sep ();
4545 src#caption "OpenGL" 0;
4546 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4547 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4548 src#reset prevmode prevuioh;
4550 fun () ->
4551 state.text <- "";
4552 let prevmode = state.mode
4553 and prevuioh = state.uioh in
4554 fillsrc prevmode prevuioh;
4555 let source = (src :> lvsource) in
4556 let modehash = findkeyhash conf "info" in
4557 state.uioh <- coe (object (self)
4558 inherit listview ~source ~trusted:true ~modehash as super
4559 val mutable m_prevmemused = 0
4560 method infochanged = function
4561 | Memused ->
4562 if m_prevmemused != state.memused
4563 then (
4564 m_prevmemused <- state.memused;
4565 G.postRedisplay "memusedchanged";
4567 | Pdim -> G.postRedisplay "pdimchanged"
4568 | Docinfo -> fillsrc prevmode prevuioh
4570 method key key mask =
4571 if not (Wsi.withctrl mask)
4572 then
4573 match key with
4574 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4575 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4576 | _ -> super#key key mask
4577 else super#key key mask
4578 end);
4579 G.postRedisplay "info";
4582 let enterhelpmode =
4583 let source =
4584 (object
4585 inherit lvsourcebase
4586 method getitemcount = Array.length state.help
4587 method getitem n =
4588 let s, l, _ = state.help.(n) in
4589 (s, l)
4591 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4592 let optuioh =
4593 if not cancel
4594 then (
4595 m_qsearch <- qsearch;
4596 match state.help.(active) with
4597 | _, _, Action f -> Some (f uioh)
4598 | _ -> Some (uioh)
4600 else None
4602 m_active <- active;
4603 m_first <- first;
4604 m_pan <- pan;
4605 optuioh
4607 method hasaction n =
4608 match state.help.(n) with
4609 | _, _, Action _ -> true
4610 | _ -> false
4612 initializer
4613 m_active <- -1
4614 end)
4615 in fun () ->
4616 let modehash = findkeyhash conf "help" in
4617 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4618 G.postRedisplay "help";
4621 let entermsgsmode =
4622 let msgsource =
4623 let re = Str.regexp "[\r\n]" in
4624 (object
4625 inherit lvsourcebase
4626 val mutable m_items = [||]
4628 method getitemcount = 1 + Array.length m_items
4630 method getitem n =
4631 if n = 0
4632 then "[Clear]", 0
4633 else m_items.(n-1), 0
4635 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4636 ignore uioh;
4637 if not cancel
4638 then (
4639 if active = 0
4640 then Buffer.clear state.errmsgs;
4641 m_qsearch <- qsearch;
4643 m_active <- active;
4644 m_first <- first;
4645 m_pan <- pan;
4646 None
4648 method hasaction n =
4649 n = 0
4651 method reset =
4652 state.newerrmsgs <- false;
4653 let l = Str.split re (Buffer.contents state.errmsgs) in
4654 m_items <- Array.of_list l
4656 initializer
4657 m_active <- 0
4658 end)
4659 in fun () ->
4660 state.text <- "";
4661 msgsource#reset;
4662 let source = (msgsource :> lvsource) in
4663 let modehash = findkeyhash conf "listview" in
4664 state.uioh <- coe (object
4665 inherit listview ~source ~trusted:false ~modehash as super
4666 method display =
4667 if state.newerrmsgs
4668 then msgsource#reset;
4669 super#display
4670 end);
4671 G.postRedisplay "msgs";
4674 let quickbookmark ?title () =
4675 match state.layout with
4676 | [] -> ()
4677 | l :: _ ->
4678 let title =
4679 match title with
4680 | None ->
4681 let sec = Unix.gettimeofday () in
4682 let tm = Unix.localtime sec in
4683 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4684 (l.pageno+1)
4685 tm.Unix.tm_mday
4686 tm.Unix.tm_mon
4687 (tm.Unix.tm_year + 1900)
4688 tm.Unix.tm_hour
4689 tm.Unix.tm_min
4690 | Some title -> title
4692 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4695 let doreshape w h =
4696 state.fullscreen <- None;
4697 Wsi.reshape w h;
4700 let setautoscrollspeed step goingdown =
4701 let incr = max 1 ((abs step) / 2) in
4702 let incr = if goingdown then incr else -incr in
4703 let astep = step + incr in
4704 state.autoscroll <- Some astep;
4707 let gotounder = function
4708 | Ulinkgoto (pageno, top) ->
4709 if pageno >= 0
4710 then (
4711 addnav ();
4712 gotopage1 pageno top;
4715 | Ulinkuri s ->
4716 gotouri s
4718 | Uremote (filename, pageno) ->
4719 let path =
4720 if Sys.file_exists filename
4721 then filename
4722 else
4723 let dir = Filename.dirname state.path in
4724 let path = Filename.concat dir filename in
4725 if Sys.file_exists path
4726 then path
4727 else ""
4729 if String.length path > 0
4730 then (
4731 let anchor = getanchor () in
4732 let ranchor = state.path, state.password, anchor in
4733 state.anchor <- (pageno, 0.0, 0.0);
4734 state.ranchors <- ranchor :: state.ranchors;
4735 opendoc path "";
4737 else showtext '!' ("Could not find " ^ filename)
4739 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4742 let canpan () =
4743 match conf.columns with
4744 | Csplit _ -> true
4745 | _ -> conf.zoom > 1.0
4748 let existsinrow pageno (columns, coverA, coverB) p =
4749 let last = ((pageno - coverA) mod columns) + columns in
4750 let rec any = function
4751 | [] -> false
4752 | l :: rest ->
4753 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4754 then p l
4755 else (
4756 if not (p l)
4757 then (if l.pageno = last then false else any rest)
4758 else true
4761 any state.layout
4764 let nextpage () =
4765 match state.layout with
4766 | [] -> ()
4767 | l :: rest ->
4768 match conf.columns with
4769 | Csingle _ ->
4770 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4771 then
4772 let y = clamp (pgscale conf.winh) in
4773 gotoghyll y
4774 else
4775 let pageno = min (l.pageno+1) (state.pagecount-1) in
4776 gotoghyll (getpagey pageno)
4777 | Cmulti ((c, _, _) as cl, _) ->
4778 if conf.presentation
4779 && (existsinrow l.pageno cl
4780 (fun l -> 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+c) (state.pagecount-1) in
4786 gotoghyll (getpagey pageno)
4787 | Csplit (n, _) ->
4788 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4789 then
4790 let pagey, pageh = getpageyh l.pageno in
4791 let pagey = pagey + pageh * l.pagecol in
4792 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4793 gotoghyll (pagey + pageh + ips)
4796 let prevpage () =
4797 match state.layout with
4798 | [] -> ()
4799 | l :: _ ->
4800 match conf.columns with
4801 | Csingle _ ->
4802 if conf.presentation && l.pagey != 0
4803 then
4804 gotoghyll (clamp (pgscale ~-(conf.winh)))
4805 else
4806 let pageno = max 0 (l.pageno-1) in
4807 gotoghyll (getpagey pageno)
4808 | Cmulti ((c, _, coverB) as cl, _) ->
4809 if conf.presentation &&
4810 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4811 then
4812 gotoghyll (clamp (pgscale ~-(conf.winh)))
4813 else
4814 let decr =
4815 if l.pageno = state.pagecount - coverB
4816 then 1
4817 else c
4819 let pageno = max 0 (l.pageno-decr) in
4820 gotoghyll (getpagey pageno)
4821 | Csplit (n, _) ->
4822 let y =
4823 if l.pagecol = 0
4824 then
4825 if l.pageno = 0
4826 then l.pagey
4827 else
4828 let pageno = max 0 (l.pageno-1) in
4829 let pagey, pageh = getpageyh pageno in
4830 pagey + (n-1)*pageh
4831 else
4832 let pagey, pageh = getpageyh l.pageno in
4833 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4835 gotoghyll y
4838 let viewkeyboard key mask =
4839 let enttext te =
4840 let mode = state.mode in
4841 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4842 state.text <- "";
4843 enttext ();
4844 G.postRedisplay "view:enttext"
4846 let ctrl = Wsi.withctrl mask in
4847 let key =
4848 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4850 match key with
4851 | 81 -> (* Q *)
4852 exit 0
4854 | 0xff63 -> (* insert *)
4855 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4856 then (
4857 state.mode <- LinkNav (Ltgendir 0);
4858 gotoy state.y;
4860 else showtext '!' "Keyboard link navigation does not work under rotation"
4862 | 0xff1b | 113 -> (* escape / q *)
4863 begin match state.mstate with
4864 | Mzoomrect _ ->
4865 state.mstate <- Mnone;
4866 Wsi.setcursor Wsi.CURSOR_INHERIT;
4867 G.postRedisplay "kill zoom rect";
4868 | _ ->
4869 begin match state.mode with
4870 | LinkNav _ ->
4871 state.mode <- View;
4872 G.postRedisplay "esc leave linknav"
4873 | _ ->
4874 match state.ranchors with
4875 | [] -> raise Quit
4876 | (path, password, anchor) :: rest ->
4877 state.ranchors <- rest;
4878 state.anchor <- anchor;
4879 opendoc path password
4880 end;
4881 end;
4883 | 0xff08 -> (* backspace *)
4884 gotoghyll (getnav ~-1)
4886 | 111 -> (* o *)
4887 enteroutlinemode ()
4889 | 117 -> (* u *)
4890 state.rects <- [];
4891 state.text <- "";
4892 G.postRedisplay "dehighlight";
4894 | 47 | 63 -> (* / ? *)
4895 let ondone isforw s =
4896 cbput state.hists.pat s;
4897 state.searchpattern <- s;
4898 search s isforw
4900 let s = String.create 1 in
4901 s.[0] <- Char.chr key;
4902 enttext (s, "", Some (onhist state.hists.pat),
4903 textentry, ondone (key = 47), true)
4905 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4906 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4907 setzoom (conf.zoom +. incr)
4909 | 43 | 0xffab -> (* + *)
4910 let ondone s =
4911 let n =
4912 try int_of_string s with exc ->
4913 state.text <- Printf.sprintf "bad integer `%s': %s"
4914 s (Printexc.to_string exc);
4915 max_int
4917 if n != max_int
4918 then (
4919 conf.pagebias <- n;
4920 state.text <- "page bias is now " ^ string_of_int n;
4923 enttext ("page bias: ", "", None, intentry, ondone, true)
4925 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4926 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4927 setzoom (max 0.01 (conf.zoom -. decr))
4929 | 45 | 0xffad -> (* - *)
4930 let ondone msg = state.text <- msg in
4931 enttext (
4932 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4933 optentry state.mode, ondone, true
4936 | 48 when ctrl -> (* ctrl-0 *)
4937 setzoom 1.0
4939 | 49 when ctrl -> (* ctrl-1 *)
4940 let cols =
4941 match conf.columns with
4942 | Csingle _ | Cmulti _ -> 1
4943 | Csplit (n, _) -> n
4945 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4946 if zoom < 1.0
4947 then setzoom zoom
4949 | 0xffc6 -> (* f9 *)
4950 togglebirdseye ()
4952 | 57 when ctrl -> (* ctrl-9 *)
4953 togglebirdseye ()
4955 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4956 when not ctrl -> (* 0..9 *)
4957 let ondone s =
4958 let n =
4959 try int_of_string s with exc ->
4960 state.text <- Printf.sprintf "bad integer `%s': %s"
4961 s (Printexc.to_string exc);
4964 if n >= 0
4965 then (
4966 addnav ();
4967 cbput state.hists.pag (string_of_int n);
4968 gotopage1 (n + conf.pagebias - 1) 0;
4971 let pageentry text key =
4972 match Char.unsafe_chr key with
4973 | 'g' -> TEdone text
4974 | _ -> intentry text key
4976 let text = "x" in text.[0] <- Char.chr key;
4977 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4979 | 98 -> (* b *)
4980 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4981 reshape conf.winw conf.winh;
4983 | 108 -> (* l *)
4984 conf.hlinks <- not conf.hlinks;
4985 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4986 G.postRedisplay "toggle highlightlinks";
4988 | 70 -> (* F *)
4989 state.glinks <- true;
4990 let mode = state.mode in
4991 state.mode <- Textentry (
4992 (":", "", None, linknentry, linkndone gotounder, false),
4993 (fun _ ->
4994 state.glinks <- false;
4995 state.mode <- mode)
4997 state.text <- "";
4998 G.postRedisplay "view:linkent(F)"
5000 | 121 -> (* y *)
5001 state.glinks <- true;
5002 let mode = state.mode in
5003 state.mode <- Textentry (
5004 (":", "", None, linknentry, linkndone (fun under ->
5005 match Ne.pipe () with
5006 | Ne.Exn exn ->
5007 showtext '!' (Printf.sprintf "pipe failed: %s"
5008 (Printexc.to_string exn));
5009 | Ne.Res (r, w) ->
5010 let popened =
5011 try popen conf.selcmd [r, 0; w, -1]; true
5012 with exn ->
5013 showtext '!'
5014 (Printf.sprintf "failed to execute %s: %s"
5015 conf.selcmd (Printexc.to_string exn));
5016 false
5018 let clo cap fd =
5019 Ne.clo fd (fun msg ->
5020 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5023 let s = undertext under in
5024 if popened
5025 then
5026 (try
5027 let l = String.length s in
5028 let n = tempfailureretry (Unix.write w s 0) l in
5029 if n != l
5030 then
5031 showtext '!'
5032 (Printf.sprintf
5033 "failed to write %d characters to sel pipe, wrote %d"
5036 with exn ->
5037 showtext '!'
5038 (Printf.sprintf "failed to write to sel pipe: %s"
5039 (Printexc.to_string exn)
5042 else dolog "%s" s;
5043 clo "pipe/r" r;
5044 clo "pipe/w" w;
5045 ), false
5047 fun _ ->
5048 state.glinks <- false;
5049 state.mode <- mode
5051 state.text <- "";
5052 G.postRedisplay "view:linkent"
5054 | 97 -> (* a *)
5055 begin match state.autoscroll with
5056 | Some step ->
5057 conf.autoscrollstep <- step;
5058 state.autoscroll <- None
5059 | None ->
5060 if conf.autoscrollstep = 0
5061 then state.autoscroll <- Some 1
5062 else state.autoscroll <- Some conf.autoscrollstep
5065 | 112 when ctrl -> (* ctrl-p *)
5066 launchpath ()
5068 | 80 -> (* P *)
5069 setpresentationmode (not conf.presentation);
5070 showtext ' ' ("presentation mode " ^
5071 if conf.presentation then "on" else "off");
5073 | 102 -> (* f *)
5074 begin match state.fullscreen with
5075 | None ->
5076 state.fullscreen <- Some (conf.winw, conf.winh);
5077 Wsi.fullscreen ()
5078 | Some (w, h) ->
5079 state.fullscreen <- None;
5080 doreshape w h
5083 | 112 | 78 -> (* p|N *)
5084 search state.searchpattern false
5086 | 110 | 0xffc0 -> (* n|F3 *)
5087 search state.searchpattern true
5089 | 116 -> (* t *)
5090 begin match state.layout with
5091 | [] -> ()
5092 | l :: _ ->
5093 gotoghyll (getpagey l.pageno)
5096 | 32 -> (* space *)
5097 nextpage ()
5099 | 0xff9f | 0xffff -> (* delete *)
5100 prevpage ()
5102 | 61 -> (* = *)
5103 showtext ' ' (describe_location ());
5105 | 119 -> (* w *)
5106 begin match state.layout with
5107 | [] -> ()
5108 | l :: _ ->
5109 doreshape (l.pagew + state.scrollw) l.pageh;
5110 G.postRedisplay "w"
5113 | 39 -> (* ' *)
5114 enterbookmarkmode ()
5116 | 104 | 0xffbe -> (* h|F1 *)
5117 enterhelpmode ()
5119 | 105 -> (* i *)
5120 enterinfomode ()
5122 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5123 entermsgsmode ()
5125 | 109 -> (* m *)
5126 let ondone s =
5127 match state.layout with
5128 | l :: _ ->
5129 if String.length s > 0
5130 then
5131 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5132 | _ -> ()
5134 enttext ("bookmark: ", "", None, textentry, ondone, true)
5136 | 126 -> (* ~ *)
5137 quickbookmark ();
5138 showtext ' ' "Quick bookmark added";
5140 | 122 -> (* z *)
5141 begin match state.layout with
5142 | l :: _ ->
5143 let rect = getpdimrect l.pagedimno in
5144 let w, h =
5145 if conf.crophack
5146 then
5147 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5148 truncate (1.2 *. (rect.(3) -. rect.(0))))
5149 else
5150 (truncate (rect.(1) -. rect.(0)),
5151 truncate (rect.(3) -. rect.(0)))
5153 let w = truncate ((float w)*.conf.zoom)
5154 and h = truncate ((float h)*.conf.zoom) in
5155 if w != 0 && h != 0
5156 then (
5157 state.anchor <- getanchor ();
5158 doreshape (w + state.scrollw) (h + conf.interpagespace)
5160 G.postRedisplay "z";
5162 | [] -> ()
5165 | 50 when ctrl -> (* ctrl-2 *)
5166 let maxw = getmaxw () in
5167 if maxw > 0.0
5168 then setzoom (maxw /. float conf.winw)
5170 | 60 | 62 -> (* < > *)
5171 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5173 | 91 | 93 -> (* [ ] *)
5174 conf.colorscale <-
5175 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5177 G.postRedisplay "brightness";
5179 | 99 when state.mode = View -> (* c *)
5180 let (c, a, b), z =
5181 match state.prevcolumns with
5182 | None -> (1, 0, 0), 1.0
5183 | Some (columns, z) ->
5184 let cab =
5185 match columns with
5186 | Csplit (c, _) -> -c, 0, 0
5187 | Cmulti ((c, a, b), _) -> c, a, b
5188 | Csingle _ -> 1, 0, 0
5190 cab, z
5192 setcolumns View c a b;
5193 setzoom z;
5195 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5196 setzoom state.prevzoom
5198 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5199 begin match state.autoscroll with
5200 | None ->
5201 begin match state.mode with
5202 | Birdseye beye -> upbirdseye 1 beye
5203 | _ ->
5204 if ctrl
5205 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5206 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5208 | Some n ->
5209 setautoscrollspeed n false
5212 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5213 begin match state.autoscroll with
5214 | None ->
5215 begin match state.mode with
5216 | Birdseye beye -> downbirdseye 1 beye
5217 | _ ->
5218 if ctrl
5219 then gotoy_and_clear_text (clamp (conf.winh/2))
5220 else gotoy_and_clear_text (clamp conf.scrollstep)
5222 | Some n ->
5223 setautoscrollspeed n true
5226 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5227 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5228 if canpan ()
5229 then
5230 let dx =
5231 if ctrl
5232 then conf.winw / 2
5233 else conf.hscrollstep
5235 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5236 state.x <- state.x + dx;
5237 gotoy_and_clear_text state.y
5238 else (
5239 state.text <- "";
5240 G.postRedisplay "lef/right"
5243 | 0xff55 | 0xff9a -> (* (kp) prior *)
5244 let y =
5245 if ctrl
5246 then
5247 match state.layout with
5248 | [] -> state.y
5249 | l :: _ -> state.y - l.pagey
5250 else
5251 clamp (pgscale (-conf.winh))
5253 gotoghyll y
5255 | 0xff56 | 0xff9b -> (* (kp) next *)
5256 let y =
5257 if ctrl
5258 then
5259 match List.rev state.layout with
5260 | [] -> state.y
5261 | l :: _ -> getpagey l.pageno
5262 else
5263 clamp (pgscale conf.winh)
5265 gotoghyll y
5267 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5268 gotoghyll 0
5269 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5270 gotoghyll (clamp state.maxy)
5272 | 0xff53 | 0xff98
5273 when Wsi.withalt mask -> (* alt-(kp) right *)
5274 gotoghyll (getnav 1)
5275 | 0xff51 | 0xff96
5276 when Wsi.withalt mask -> (* alt-(kp) left *)
5277 gotoghyll (getnav ~-1)
5279 | 114 -> (* r *)
5280 reload ()
5282 | 118 when conf.debug -> (* v *)
5283 state.rects <- [];
5284 List.iter (fun l ->
5285 match getopaque l.pageno with
5286 | None -> ()
5287 | Some opaque ->
5288 let x0, y0, x1, y1 = pagebbox opaque in
5289 let a,b = float x0, float y0 in
5290 let c,d = float x1, float y0 in
5291 let e,f = float x1, float y1 in
5292 let h,j = float x0, float y1 in
5293 let rect = (a,b,c,d,e,f,h,j) in
5294 debugrect rect;
5295 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5296 ) state.layout;
5297 G.postRedisplay "v";
5299 | _ ->
5300 vlog "huh? %s" (Wsi.keyname key)
5303 let linknavkeyboard key mask linknav =
5304 let getpage pageno =
5305 let rec loop = function
5306 | [] -> None
5307 | l :: _ when l.pageno = pageno -> Some l
5308 | _ :: rest -> loop rest
5309 in loop state.layout
5311 let doexact (pageno, n) =
5312 match getopaque pageno, getpage pageno with
5313 | Some opaque, Some l ->
5314 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5315 then
5316 let under = getlink opaque n in
5317 G.postRedisplay "link gotounder";
5318 gotounder under;
5319 state.mode <- View;
5320 else
5321 let opt, dir =
5322 match key with
5323 | 0xff50 -> (* home *)
5324 Some (findlink opaque LDfirst), -1
5326 | 0xff57 -> (* end *)
5327 Some (findlink opaque LDlast), 1
5329 | 0xff51 -> (* left *)
5330 Some (findlink opaque (LDleft n)), -1
5332 | 0xff53 -> (* right *)
5333 Some (findlink opaque (LDright n)), 1
5335 | 0xff52 -> (* up *)
5336 Some (findlink opaque (LDup n)), -1
5338 | 0xff54 -> (* down *)
5339 Some (findlink opaque (LDdown n)), 1
5341 | _ -> None, 0
5343 let pwl l dir =
5344 begin match findpwl l.pageno dir with
5345 | Pwlnotfound -> ()
5346 | Pwl pageno ->
5347 let notfound dir =
5348 state.mode <- LinkNav (Ltgendir dir);
5349 let y, h = getpageyh pageno in
5350 let y =
5351 if dir < 0
5352 then y + h - conf.winh
5353 else y
5355 gotoy y
5357 begin match getopaque pageno, getpage pageno with
5358 | Some opaque, Some _ ->
5359 let link =
5360 let ld = if dir > 0 then LDfirst else LDlast in
5361 findlink opaque ld
5363 begin match link with
5364 | Lfound m ->
5365 showlinktype (getlink opaque m);
5366 state.mode <- LinkNav (Ltexact (pageno, m));
5367 G.postRedisplay "linknav jpage";
5368 | _ -> notfound dir
5369 end;
5370 | _ -> notfound dir
5371 end;
5372 end;
5374 begin match opt with
5375 | Some Lnotfound -> pwl l dir;
5376 | Some (Lfound m) ->
5377 if m = n
5378 then pwl l dir
5379 else (
5380 let _, y0, _, y1 = getlinkrect opaque m in
5381 if y0 < l.pagey
5382 then gotopage1 l.pageno y0
5383 else (
5384 let d = fstate.fontsize + 1 in
5385 if y1 - l.pagey > l.pagevh - d
5386 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5387 else G.postRedisplay "linknav";
5389 showlinktype (getlink opaque m);
5390 state.mode <- LinkNav (Ltexact (l.pageno, m));
5393 | None -> viewkeyboard key mask
5394 end;
5395 | _ -> viewkeyboard key mask
5397 if key = 0xff63
5398 then (
5399 state.mode <- View;
5400 G.postRedisplay "leave linknav"
5402 else
5403 match linknav with
5404 | Ltgendir _ -> viewkeyboard key mask
5405 | Ltexact exact -> doexact exact
5408 let keyboard key mask =
5409 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5410 then wcmd "interrupt"
5411 else state.uioh <- state.uioh#key key mask
5414 let birdseyekeyboard key mask
5415 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5416 let incr =
5417 match conf.columns with
5418 | Csingle _ -> 1
5419 | Cmulti ((c, _, _), _) -> c
5420 | Csplit _ -> failwith "bird's eye split mode"
5422 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5423 match key with
5424 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5425 let y, h = getpageyh pageno in
5426 let top = (conf.winh - h) / 2 in
5427 gotoy (max 0 (y - top))
5428 | 0xff0d (* enter *)
5429 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5430 | 0xff1b -> leavebirdseye beye true (* escape *)
5431 | 0xff52 -> upbirdseye incr beye (* up *)
5432 | 0xff54 -> downbirdseye incr beye (* down *)
5433 | 0xff51 -> upbirdseye 1 beye (* left *)
5434 | 0xff53 -> downbirdseye 1 beye (* right *)
5436 | 0xff55 -> (* prior *)
5437 begin match state.layout with
5438 | l :: _ ->
5439 if l.pagey != 0
5440 then (
5441 state.mode <- Birdseye (
5442 oconf, leftx, l.pageno, hooverpageno, anchor
5444 gotopage1 l.pageno 0;
5446 else (
5447 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5448 match layout with
5449 | [] -> gotoy (clamp (-conf.winh))
5450 | l :: _ ->
5451 state.mode <- Birdseye (
5452 oconf, leftx, l.pageno, hooverpageno, anchor
5454 gotopage1 l.pageno 0
5457 | [] -> gotoy (clamp (-conf.winh))
5458 end;
5460 | 0xff56 -> (* next *)
5461 begin match List.rev state.layout with
5462 | l :: _ ->
5463 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5464 begin match layout with
5465 | [] ->
5466 let incr = l.pageh - l.pagevh in
5467 if incr = 0
5468 then (
5469 state.mode <-
5470 Birdseye (
5471 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5473 G.postRedisplay "birdseye pagedown";
5475 else gotoy (clamp (incr + conf.interpagespace*2));
5477 | l :: _ ->
5478 state.mode <-
5479 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5480 gotopage1 l.pageno 0;
5483 | [] -> gotoy (clamp conf.winh)
5484 end;
5486 | 0xff50 -> (* home *)
5487 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5488 gotopage1 0 0
5490 | 0xff57 -> (* end *)
5491 let pageno = state.pagecount - 1 in
5492 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5493 if not (pagevisible state.layout pageno)
5494 then
5495 let h =
5496 match List.rev state.pdims with
5497 | [] -> conf.winh
5498 | (_, _, h, _) :: _ -> h
5500 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5501 else G.postRedisplay "birdseye end";
5502 | _ -> viewkeyboard key mask
5505 let drawpage l linkindexbase =
5506 let color =
5507 match state.mode with
5508 | Textentry _ -> scalecolor 0.4
5509 | LinkNav _
5510 | View -> scalecolor 1.0
5511 | Birdseye (_, _, pageno, hooverpageno, _) ->
5512 if l.pageno = hooverpageno
5513 then scalecolor 0.9
5514 else (
5515 if l.pageno = pageno
5516 then scalecolor 1.0
5517 else scalecolor 0.8
5520 drawtiles l color;
5521 begin match getopaque l.pageno with
5522 | Some opaque ->
5523 if tileready l l.pagex l.pagey
5524 then
5525 let x = l.pagedispx - l.pagex
5526 and y = l.pagedispy - l.pagey in
5527 let hlmask =
5528 match conf.columns with
5529 | Csingle _ | Cmulti _ ->
5530 (if conf.hlinks then 1 else 0)
5531 + (if state.glinks
5532 && not (isbirdseye state.mode) then 2 else 0)
5533 | _ -> 0
5535 let s =
5536 match state.mode with
5537 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5538 | _ -> ""
5540 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5541 else 0
5543 | _ -> 0
5544 end;
5547 let scrollindicator () =
5548 let sbw, ph, sh = state.uioh#scrollph in
5549 let sbh, pw, sw = state.uioh#scrollpw in
5551 GlDraw.color (0.64, 0.64, 0.64);
5552 GlDraw.rect
5553 (float (conf.winw - sbw), 0.)
5554 (float conf.winw, float conf.winh)
5556 GlDraw.rect
5557 (0., float (conf.winh - sbh))
5558 (float (conf.winw - state.scrollw - 1), float conf.winh)
5560 GlDraw.color (0.0, 0.0, 0.0);
5562 GlDraw.rect
5563 (float (conf.winw - sbw), ph)
5564 (float conf.winw, ph +. sh)
5566 GlDraw.rect
5567 (pw, float (conf.winh - sbh))
5568 (pw +. sw, float conf.winh)
5572 let showsel () =
5573 match state.mstate with
5574 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5577 | Msel ((x0, y0), (x1, y1)) ->
5578 let rec loop = function
5579 | l :: ls ->
5580 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5581 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5582 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5583 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5584 then
5585 match getopaque l.pageno with
5586 | Some opaque ->
5587 let x0, y0 = pagetranslatepoint l x0 y0 in
5588 let x1, y1 = pagetranslatepoint l x1 y1 in
5589 seltext opaque (x0, y0, x1, y1);
5590 | _ -> ()
5591 else loop ls
5592 | [] -> ()
5594 loop state.layout
5597 let showrects rects =
5598 Gl.enable `blend;
5599 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5600 GlDraw.polygon_mode `both `fill;
5601 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5602 List.iter
5603 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5604 List.iter (fun l ->
5605 if l.pageno = pageno
5606 then (
5607 let dx = float (l.pagedispx - l.pagex) in
5608 let dy = float (l.pagedispy - l.pagey) in
5609 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5610 GlDraw.begins `quads;
5612 GlDraw.vertex2 (x0+.dx, y0+.dy);
5613 GlDraw.vertex2 (x1+.dx, y1+.dy);
5614 GlDraw.vertex2 (x2+.dx, y2+.dy);
5615 GlDraw.vertex2 (x3+.dx, y3+.dy);
5617 GlDraw.ends ();
5619 ) state.layout
5620 ) rects
5622 Gl.disable `blend;
5625 let display () =
5626 GlClear.color (scalecolor2 conf.bgcolor);
5627 GlClear.clear [`color];
5628 let rec loop linkindexbase = function
5629 | l :: rest ->
5630 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5631 loop linkindexbase rest
5632 | [] -> ()
5634 loop 0 state.layout;
5635 let rects =
5636 match state.mode with
5637 | LinkNav (Ltexact (pageno, linkno)) ->
5638 begin match getopaque pageno with
5639 | Some opaque ->
5640 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5641 (pageno, 5, (
5642 float x0, float y0,
5643 float x1, float y0,
5644 float x1, float y1,
5645 float x0, float y1)
5646 ) :: state.rects
5647 | None -> state.rects
5649 | _ -> state.rects
5651 showrects rects;
5652 showsel ();
5653 state.uioh#display;
5654 begin match state.mstate with
5655 | Mzoomrect ((x0, y0), (x1, y1)) ->
5656 Gl.enable `blend;
5657 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5658 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5659 GlDraw.rect (float x0, float y0)
5660 (float x1, float y1);
5661 Gl.disable `blend;
5662 | _ -> ()
5663 end;
5664 enttext ();
5665 scrollindicator ();
5666 Wsi.swapb ();
5669 let zoomrect x y x1 y1 =
5670 let x0 = min x x1
5671 and x1 = max x x1
5672 and y0 = min y y1 in
5673 gotoy (state.y + y0);
5674 state.anchor <- getanchor ();
5675 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5676 let margin =
5677 if state.w < conf.winw - state.scrollw
5678 then (conf.winw - state.scrollw - state.w) / 2
5679 else 0
5681 state.x <- (state.x + margin) - x0;
5682 setzoom zoom;
5683 Wsi.setcursor Wsi.CURSOR_INHERIT;
5684 state.mstate <- Mnone;
5687 let scrollx x =
5688 let winw = conf.winw - state.scrollw - 1 in
5689 let s = float x /. float winw in
5690 let destx = truncate (float (state.w + winw) *. s) in
5691 state.x <- winw - destx;
5692 gotoy_and_clear_text state.y;
5693 state.mstate <- Mscrollx;
5696 let scrolly y =
5697 let s = float y /. float conf.winh in
5698 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5699 gotoy_and_clear_text desty;
5700 state.mstate <- Mscrolly;
5703 let viewmouse button down x y mask =
5704 match button with
5705 | n when (n == 4 || n == 5) && not down ->
5706 if Wsi.withctrl mask
5707 then (
5708 match state.mstate with
5709 | Mzoom (oldn, i) ->
5710 if oldn = n
5711 then (
5712 if i = 2
5713 then
5714 let incr =
5715 match n with
5716 | 5 ->
5717 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5718 | _ ->
5719 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5721 let zoom = conf.zoom -. incr in
5722 setzoom zoom;
5723 state.mstate <- Mzoom (n, 0);
5724 else
5725 state.mstate <- Mzoom (n, i+1);
5727 else state.mstate <- Mzoom (n, 0)
5729 | _ -> state.mstate <- Mzoom (n, 0)
5731 else (
5732 match state.autoscroll with
5733 | Some step -> setautoscrollspeed step (n=4)
5734 | None ->
5735 if conf.wheelbypage
5736 then (
5737 if n = 4
5738 then prevpage ()
5739 else nextpage ()
5741 else
5742 let incr =
5743 if n = 4
5744 then -conf.scrollstep
5745 else conf.scrollstep
5747 let incr = incr * 2 in
5748 let y = clamp incr in
5749 gotoy_and_clear_text y
5752 | n when (n = 6 || n = 7) && not down && canpan () ->
5753 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5754 gotoy_and_clear_text state.y
5756 | 1 when Wsi.withshift mask ->
5757 state.mstate <- Mnone;
5758 if not down then (
5759 match unproject x y with
5760 | Some (pageno, ux, uy) ->
5761 let cmd = Printf.sprintf
5762 "%s %s %d %d %d"
5763 conf.stcmd state.path pageno ux uy
5765 popen cmd []
5766 | None -> ()
5769 | 1 when Wsi.withctrl mask ->
5770 if down
5771 then (
5772 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5773 state.mstate <- Mpan (x, y)
5775 else
5776 state.mstate <- Mnone
5778 | 3 ->
5779 if down
5780 then (
5781 Wsi.setcursor Wsi.CURSOR_CYCLE;
5782 let p = (x, y) in
5783 state.mstate <- Mzoomrect (p, p)
5785 else (
5786 match state.mstate with
5787 | Mzoomrect ((x0, y0), _) ->
5788 if abs (x-x0) > 10 && abs (y - y0) > 10
5789 then zoomrect x0 y0 x y
5790 else (
5791 state.mstate <- Mnone;
5792 Wsi.setcursor Wsi.CURSOR_INHERIT;
5793 G.postRedisplay "kill accidental zoom rect";
5795 | _ ->
5796 Wsi.setcursor Wsi.CURSOR_INHERIT;
5797 state.mstate <- Mnone
5800 | 1 when x > conf.winw - state.scrollw ->
5801 if down
5802 then
5803 let _, position, sh = state.uioh#scrollph in
5804 if y > truncate position && y < truncate (position +. sh)
5805 then state.mstate <- Mscrolly
5806 else scrolly y
5807 else
5808 state.mstate <- Mnone
5810 | 1 when y > conf.winh - state.hscrollh ->
5811 if down
5812 then
5813 let _, position, sw = state.uioh#scrollpw in
5814 if x > truncate position && x < truncate (position +. sw)
5815 then state.mstate <- Mscrollx
5816 else scrollx x
5817 else
5818 state.mstate <- Mnone
5820 | 1 ->
5821 let dest = if down then getunder x y else Unone in
5822 begin match dest with
5823 | Ulinkgoto _
5824 | Ulinkuri _
5825 | Uremote _
5826 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5827 gotounder dest
5829 | Unone when down ->
5830 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5831 state.mstate <- Mpan (x, y);
5833 | Unone | Utext _ ->
5834 if down
5835 then (
5836 if conf.angle mod 360 = 0
5837 then (
5838 state.mstate <- Msel ((x, y), (x, y));
5839 G.postRedisplay "mouse select";
5842 else (
5843 match state.mstate with
5844 | Mnone -> ()
5846 | Mzoom _ | Mscrollx | Mscrolly ->
5847 state.mstate <- Mnone
5849 | Mzoomrect ((x0, y0), _) ->
5850 zoomrect x0 y0 x y
5852 | Mpan _ ->
5853 Wsi.setcursor Wsi.CURSOR_INHERIT;
5854 state.mstate <- Mnone
5856 | Msel ((x0, y0), (x1, y1)) ->
5857 let rec loop = function
5858 | [] -> ()
5859 | l :: rest ->
5860 let inside =
5861 let a0 = l.pagedispy in
5862 let a1 = a0 + l.pagevh in
5863 let b0 = l.pagedispx in
5864 let b1 = b0 + l.pagevw in
5865 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5866 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5868 if inside
5869 then
5870 match getopaque l.pageno with
5871 | Some opaque ->
5872 begin
5873 match Ne.pipe () with
5874 | Ne.Exn exn ->
5875 showtext '!'
5876 (Printf.sprintf
5877 "can not create sel pipe: %s"
5878 (Printexc.to_string exn));
5879 | Ne.Res (r, w) ->
5880 let doclose what fd =
5881 Ne.clo fd (fun msg ->
5882 dolog "%s close failed: %s" what msg)
5885 popen conf.selcmd [r, 0; w, -1];
5886 copysel w opaque;
5887 doclose "pipe/r" r;
5888 G.postRedisplay "copysel";
5889 with exn ->
5890 dolog "can not execute %S: %s"
5891 conf.selcmd (Printexc.to_string exn);
5892 doclose "pipe/r" r;
5893 doclose "pipe/w" w;
5895 | None -> ()
5896 else loop rest
5898 loop state.layout;
5899 Wsi.setcursor Wsi.CURSOR_INHERIT;
5900 state.mstate <- Mnone;
5904 | _ -> ()
5907 let birdseyemouse button down x y mask
5908 (conf, leftx, _, hooverpageno, anchor) =
5909 match button with
5910 | 1 when down ->
5911 let rec loop = function
5912 | [] -> ()
5913 | l :: rest ->
5914 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5915 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5916 then (
5917 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5919 else loop rest
5921 loop state.layout
5922 | 3 -> ()
5923 | _ -> viewmouse button down x y mask
5926 let mouse button down x y mask =
5927 state.uioh <- state.uioh#button button down x y mask;
5930 let motion ~x ~y =
5931 state.uioh <- state.uioh#motion x y
5934 let pmotion ~x ~y =
5935 state.uioh <- state.uioh#pmotion x y;
5938 let uioh = object
5939 method display = ()
5941 method key key mask =
5942 begin match state.mode with
5943 | Textentry textentry -> textentrykeyboard key mask textentry
5944 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5945 | View -> viewkeyboard key mask
5946 | LinkNav linknav -> linknavkeyboard key mask linknav
5947 end;
5948 state.uioh
5950 method button button bstate x y mask =
5951 begin match state.mode with
5952 | LinkNav _
5953 | View -> viewmouse button bstate x y mask
5954 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5955 | Textentry _ -> ()
5956 end;
5957 state.uioh
5959 method motion x y =
5960 begin match state.mode with
5961 | Textentry _ -> ()
5962 | View | Birdseye _ | LinkNav _ ->
5963 match state.mstate with
5964 | Mzoom _ | Mnone -> ()
5966 | Mpan (x0, y0) ->
5967 let dx = x - x0
5968 and dy = y0 - y in
5969 state.mstate <- Mpan (x, y);
5970 if canpan ()
5971 then state.x <- state.x + dx;
5972 let y = clamp dy in
5973 gotoy_and_clear_text y
5975 | Msel (a, _) ->
5976 state.mstate <- Msel (a, (x, y));
5977 G.postRedisplay "motion select";
5979 | Mscrolly ->
5980 let y = min conf.winh (max 0 y) in
5981 scrolly y
5983 | Mscrollx ->
5984 let x = min conf.winw (max 0 x) in
5985 scrollx x
5987 | Mzoomrect (p0, _) ->
5988 state.mstate <- Mzoomrect (p0, (x, y));
5989 G.postRedisplay "motion zoomrect";
5990 end;
5991 state.uioh
5993 method pmotion x y =
5994 begin match state.mode with
5995 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5996 let rec loop = function
5997 | [] ->
5998 if hooverpageno != -1
5999 then (
6000 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6001 G.postRedisplay "pmotion birdseye no hoover";
6003 | l :: rest ->
6004 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6005 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6006 then (
6007 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6008 G.postRedisplay "pmotion birdseye hoover";
6010 else loop rest
6012 loop state.layout
6014 | Textentry _ -> ()
6016 | LinkNav _
6017 | View ->
6018 match state.mstate with
6019 | Mnone -> updateunder x y
6020 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6022 end;
6023 state.uioh
6025 method infochanged _ = ()
6027 method scrollph =
6028 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
6029 let p, h = scrollph state.y maxy in
6030 state.scrollw, p, h
6032 method scrollpw =
6033 let winw = conf.winw - state.scrollw - 1 in
6034 let fwinw = float winw in
6035 let sw =
6036 let sw = fwinw /. float state.w in
6037 let sw = fwinw *. sw in
6038 max sw (float conf.scrollh)
6040 let position, sw =
6041 let f = state.w+winw in
6042 let r = float (winw-state.x) /. float f in
6043 let p = fwinw *. r in
6044 p-.sw/.2., sw
6046 let sw =
6047 if position +. sw > fwinw
6048 then fwinw -. position
6049 else sw
6051 state.hscrollh, position, sw
6053 method modehash =
6054 let modename =
6055 match state.mode with
6056 | LinkNav _ -> "links"
6057 | Textentry _ -> "textentry"
6058 | Birdseye _ -> "birdseye"
6059 | View -> "view"
6061 findkeyhash conf modename
6062 end;;
6064 module Config =
6065 struct
6066 open Parser
6068 let fontpath = ref "";;
6070 module KeyMap =
6071 Map.Make (struct type t = (int * int) let compare = compare end);;
6073 let unent s =
6074 let l = String.length s in
6075 let b = Buffer.create l in
6076 unent b s 0 l;
6077 Buffer.contents b;
6080 let home =
6081 try Sys.getenv "HOME"
6082 with exn ->
6083 prerr_endline
6084 ("Can not determine home directory location: " ^
6085 Printexc.to_string exn);
6089 let modifier_of_string = function
6090 | "alt" -> Wsi.altmask
6091 | "shift" -> Wsi.shiftmask
6092 | "ctrl" | "control" -> Wsi.ctrlmask
6093 | "meta" -> Wsi.metamask
6094 | _ -> 0
6097 let key_of_string =
6098 let r = Str.regexp "-" in
6099 fun s ->
6100 let elems = Str.full_split r s in
6101 let f n k m =
6102 let g s =
6103 let m1 = modifier_of_string s in
6104 if m1 = 0
6105 then (Wsi.namekey s, m)
6106 else (k, m lor m1)
6107 in function
6108 | Str.Delim s when n land 1 = 0 -> g s
6109 | Str.Text s -> g s
6110 | Str.Delim _ -> (k, m)
6112 let rec loop n k m = function
6113 | [] -> (k, m)
6114 | x :: xs ->
6115 let k, m = f n k m x in
6116 loop (n+1) k m xs
6118 loop 0 0 0 elems
6121 let keys_of_string =
6122 let r = Str.regexp "[ \t]" in
6123 fun s ->
6124 let elems = Str.split r s in
6125 List.map key_of_string elems
6128 let copykeyhashes c =
6129 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6132 let config_of c attrs =
6133 let apply c k v =
6135 match k with
6136 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6137 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6138 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6139 | "preload" -> { c with preload = bool_of_string v }
6140 | "page-bias" -> { c with pagebias = int_of_string v }
6141 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6142 | "horizontal-scroll-step" ->
6143 { c with hscrollstep = max (int_of_string v) 1 }
6144 | "auto-scroll-step" ->
6145 { c with autoscrollstep = max 0 (int_of_string v) }
6146 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6147 | "crop-hack" -> { c with crophack = bool_of_string v }
6148 | "throttle" ->
6149 let mw =
6150 match String.lowercase v with
6151 | "true" -> Some infinity
6152 | "false" -> None
6153 | f -> Some (float_of_string f)
6155 { c with maxwait = mw}
6156 | "highlight-links" -> { c with hlinks = bool_of_string v }
6157 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6158 | "vertical-margin" ->
6159 { c with interpagespace = max 0 (int_of_string v) }
6160 | "zoom" ->
6161 let zoom = float_of_string v /. 100. in
6162 let zoom = max zoom 0.0 in
6163 { c with zoom = zoom }
6164 | "presentation" -> { c with presentation = bool_of_string v }
6165 | "rotation-angle" -> { c with angle = int_of_string v }
6166 | "width" -> { c with winw = max 20 (int_of_string v) }
6167 | "height" -> { c with winh = max 20 (int_of_string v) }
6168 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6169 | "proportional-display" -> { c with proportional = bool_of_string v }
6170 | "pixmap-cache-size" ->
6171 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6172 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6173 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6174 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6175 | "persistent-location" -> { c with jumpback = bool_of_string v }
6176 | "background-color" -> { c with bgcolor = color_of_string v }
6177 | "scrollbar-in-presentation" ->
6178 { c with scrollbarinpm = bool_of_string v }
6179 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6180 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6181 | "mupdf-store-size" ->
6182 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6183 | "checkers" -> { c with checkers = bool_of_string v }
6184 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6185 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6186 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6187 | "uri-launcher" -> { c with urilauncher = unent v }
6188 | "path-launcher" -> { c with pathlauncher = unent v }
6189 | "color-space" -> { c with colorspace = colorspace_of_string v }
6190 | "invert-colors" -> { c with invert = bool_of_string v }
6191 | "brightness" -> { c with colorscale = float_of_string v }
6192 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6193 | "ghyllscroll" ->
6194 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6195 | "columns" ->
6196 let (n, _, _) as nab = multicolumns_of_string v in
6197 if n < 0
6198 then { c with columns = Csplit (-n, [||]) }
6199 else { c with columns = Cmulti (nab, [||]) }
6200 | "birds-eye-columns" ->
6201 { c with beyecolumns = Some (max (int_of_string v) 2) }
6202 | "selection-command" -> { c with selcmd = unent v }
6203 | "synctex-command" -> { c with stcmd = unent v }
6204 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6205 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6206 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6207 | "use-pbo" -> { c with usepbo = bool_of_string v }
6208 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6209 | _ -> c
6210 with exn ->
6211 prerr_endline ("Error processing attribute (`" ^
6212 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6215 let rec fold c = function
6216 | [] -> c
6217 | (k, v) :: rest ->
6218 let c = apply c k v in
6219 fold c rest
6221 fold { c with keyhashes = copykeyhashes c } attrs;
6224 let fromstring f pos n v d =
6225 try f v
6226 with exn ->
6227 dolog "Error processing attribute (%S=%S) at %d\n%s"
6228 n v pos (Printexc.to_string exn)
6233 let bookmark_of attrs =
6234 let rec fold title page rely visy = function
6235 | ("title", v) :: rest -> fold v page rely visy rest
6236 | ("page", v) :: rest -> fold title v rely visy rest
6237 | ("rely", v) :: rest -> fold title page v visy rest
6238 | ("visy", v) :: rest -> fold title page rely v rest
6239 | _ :: rest -> fold title page rely visy rest
6240 | [] -> title, page, rely, visy
6242 fold "invalid" "0" "0" "0" attrs
6245 let doc_of attrs =
6246 let rec fold path page rely pan visy = function
6247 | ("path", v) :: rest -> fold v page rely pan visy rest
6248 | ("page", v) :: rest -> fold path v rely pan visy rest
6249 | ("rely", v) :: rest -> fold path page v pan visy rest
6250 | ("pan", v) :: rest -> fold path page rely v visy rest
6251 | ("visy", v) :: rest -> fold path page rely pan v rest
6252 | _ :: rest -> fold path page rely pan visy rest
6253 | [] -> path, page, rely, pan, visy
6255 fold "" "0" "0" "0" "0" attrs
6258 let map_of attrs =
6259 let rec fold rs ls = function
6260 | ("out", v) :: rest -> fold v ls rest
6261 | ("in", v) :: rest -> fold rs v rest
6262 | _ :: rest -> fold ls rs rest
6263 | [] -> ls, rs
6265 fold "" "" attrs
6268 let setconf dst src =
6269 dst.scrollbw <- src.scrollbw;
6270 dst.scrollh <- src.scrollh;
6271 dst.icase <- src.icase;
6272 dst.preload <- src.preload;
6273 dst.pagebias <- src.pagebias;
6274 dst.verbose <- src.verbose;
6275 dst.scrollstep <- src.scrollstep;
6276 dst.maxhfit <- src.maxhfit;
6277 dst.crophack <- src.crophack;
6278 dst.autoscrollstep <- src.autoscrollstep;
6279 dst.maxwait <- src.maxwait;
6280 dst.hlinks <- src.hlinks;
6281 dst.underinfo <- src.underinfo;
6282 dst.interpagespace <- src.interpagespace;
6283 dst.zoom <- src.zoom;
6284 dst.presentation <- src.presentation;
6285 dst.angle <- src.angle;
6286 dst.winw <- src.winw;
6287 dst.winh <- src.winh;
6288 dst.savebmarks <- src.savebmarks;
6289 dst.memlimit <- src.memlimit;
6290 dst.proportional <- src.proportional;
6291 dst.texcount <- src.texcount;
6292 dst.sliceheight <- src.sliceheight;
6293 dst.thumbw <- src.thumbw;
6294 dst.jumpback <- src.jumpback;
6295 dst.bgcolor <- src.bgcolor;
6296 dst.scrollbarinpm <- src.scrollbarinpm;
6297 dst.tilew <- src.tilew;
6298 dst.tileh <- src.tileh;
6299 dst.mustoresize <- src.mustoresize;
6300 dst.checkers <- src.checkers;
6301 dst.aalevel <- src.aalevel;
6302 dst.trimmargins <- src.trimmargins;
6303 dst.trimfuzz <- src.trimfuzz;
6304 dst.urilauncher <- src.urilauncher;
6305 dst.colorspace <- src.colorspace;
6306 dst.invert <- src.invert;
6307 dst.colorscale <- src.colorscale;
6308 dst.redirectstderr <- src.redirectstderr;
6309 dst.ghyllscroll <- src.ghyllscroll;
6310 dst.columns <- src.columns;
6311 dst.beyecolumns <- src.beyecolumns;
6312 dst.selcmd <- src.selcmd;
6313 dst.updatecurs <- src.updatecurs;
6314 dst.pathlauncher <- src.pathlauncher;
6315 dst.keyhashes <- copykeyhashes src;
6316 dst.hfsize <- src.hfsize;
6317 dst.hscrollstep <- src.hscrollstep;
6318 dst.pgscale <- src.pgscale;
6319 dst.usepbo <- src.usepbo;
6320 dst.wheelbypage <- src.wheelbypage;
6321 dst.stcmd <- src.stcmd;
6324 let get s =
6325 let h = Hashtbl.create 10 in
6326 let dc = { defconf with angle = defconf.angle } in
6327 let rec toplevel v t spos _ =
6328 match t with
6329 | Vdata | Vcdata | Vend -> v
6330 | Vopen ("llppconfig", _, closed) ->
6331 if closed
6332 then v
6333 else { v with f = llppconfig }
6334 | Vopen _ ->
6335 error "unexpected subelement at top level" s spos
6336 | Vclose _ -> error "unexpected close at top level" s spos
6338 and llppconfig v t spos _ =
6339 match t with
6340 | Vdata | Vcdata -> v
6341 | Vend -> error "unexpected end of input in llppconfig" s spos
6342 | Vopen ("defaults", attrs, closed) ->
6343 let c = config_of dc attrs in
6344 setconf dc c;
6345 if closed
6346 then v
6347 else { v with f = defaults }
6349 | Vopen ("ui-font", attrs, closed) ->
6350 let rec getsize size = function
6351 | [] -> size
6352 | ("size", v) :: rest ->
6353 let size =
6354 fromstring int_of_string spos "size" v fstate.fontsize in
6355 getsize size rest
6356 | l -> getsize size l
6358 fstate.fontsize <- getsize fstate.fontsize attrs;
6359 if closed
6360 then v
6361 else { v with f = uifont (Buffer.create 10) }
6363 | Vopen ("doc", attrs, closed) ->
6364 let pathent, spage, srely, span, svisy = doc_of attrs in
6365 let path = unent pathent
6366 and pageno = fromstring int_of_string spos "page" spage 0
6367 and rely = fromstring float_of_string spos "rely" srely 0.0
6368 and pan = fromstring int_of_string spos "pan" span 0
6369 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6370 let c = config_of dc attrs in
6371 let anchor = (pageno, rely, visy) in
6372 if closed
6373 then (Hashtbl.add h path (c, [], pan, anchor); v)
6374 else { v with f = doc path pan anchor c [] }
6376 | Vopen _ ->
6377 error "unexpected subelement in llppconfig" s spos
6379 | Vclose "llppconfig" -> { v with f = toplevel }
6380 | Vclose _ -> error "unexpected close in llppconfig" s spos
6382 and defaults v t spos _ =
6383 match t with
6384 | Vdata | Vcdata -> v
6385 | Vend -> error "unexpected end of input in defaults" s spos
6386 | Vopen ("keymap", attrs, closed) ->
6387 let modename =
6388 try List.assoc "mode" attrs
6389 with Not_found -> "global" in
6390 if closed
6391 then v
6392 else
6393 let ret keymap =
6394 let h = findkeyhash dc modename in
6395 KeyMap.iter (Hashtbl.replace h) keymap;
6396 defaults
6398 { v with f = pkeymap ret KeyMap.empty }
6400 | Vopen (_, _, _) ->
6401 error "unexpected subelement in defaults" s spos
6403 | Vclose "defaults" ->
6404 { v with f = llppconfig }
6406 | Vclose _ -> error "unexpected close in defaults" s spos
6408 and uifont b v t spos epos =
6409 match t with
6410 | Vdata | Vcdata ->
6411 Buffer.add_substring b s spos (epos - spos);
6413 | Vopen (_, _, _) ->
6414 error "unexpected subelement in ui-font" s spos
6415 | Vclose "ui-font" ->
6416 if String.length !fontpath = 0
6417 then fontpath := Buffer.contents b;
6418 { v with f = llppconfig }
6419 | Vclose _ -> error "unexpected close in ui-font" s spos
6420 | Vend -> error "unexpected end of input in ui-font" s spos
6422 and doc path pan anchor c bookmarks v t spos _ =
6423 match t with
6424 | Vdata | Vcdata -> v
6425 | Vend -> error "unexpected end of input in doc" s spos
6426 | Vopen ("bookmarks", _, closed) ->
6427 if closed
6428 then v
6429 else { v with f = pbookmarks path pan anchor c bookmarks }
6431 | Vopen ("keymap", attrs, closed) ->
6432 let modename =
6433 try List.assoc "mode" attrs
6434 with Not_found -> "global"
6436 if closed
6437 then v
6438 else
6439 let ret keymap =
6440 let h = findkeyhash c modename in
6441 KeyMap.iter (Hashtbl.replace h) keymap;
6442 doc path pan anchor c bookmarks
6444 { v with f = pkeymap ret KeyMap.empty }
6446 | Vopen (_, _, _) ->
6447 error "unexpected subelement in doc" s spos
6449 | Vclose "doc" ->
6450 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6451 { v with f = llppconfig }
6453 | Vclose _ -> error "unexpected close in doc" s spos
6455 and pkeymap ret keymap v t spos _ =
6456 match t with
6457 | Vdata | Vcdata -> v
6458 | Vend -> error "unexpected end of input in keymap" s spos
6459 | Vopen ("map", attrs, closed) ->
6460 let r, l = map_of attrs in
6461 let kss = fromstring keys_of_string spos "in" r [] in
6462 let lss = fromstring keys_of_string spos "out" l [] in
6463 let keymap =
6464 match kss with
6465 | [] -> keymap
6466 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6467 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6469 if closed
6470 then { v with f = pkeymap ret keymap }
6471 else
6472 let f () = v in
6473 { v with f = skip "map" f }
6475 | Vopen _ ->
6476 error "unexpected subelement in keymap" s spos
6478 | Vclose "keymap" ->
6479 { v with f = ret keymap }
6481 | Vclose _ -> error "unexpected close in keymap" s spos
6483 and pbookmarks path pan anchor c bookmarks v t spos _ =
6484 match t with
6485 | Vdata | Vcdata -> v
6486 | Vend -> error "unexpected end of input in bookmarks" s spos
6487 | Vopen ("item", attrs, closed) ->
6488 let titleent, spage, srely, svisy = bookmark_of attrs in
6489 let page = fromstring int_of_string spos "page" spage 0
6490 and rely = fromstring float_of_string spos "rely" srely 0.0
6491 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6492 let bookmarks =
6493 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6495 if closed
6496 then { v with f = pbookmarks path pan anchor c bookmarks }
6497 else
6498 let f () = v in
6499 { v with f = skip "item" f }
6501 | Vopen _ ->
6502 error "unexpected subelement in bookmarks" s spos
6504 | Vclose "bookmarks" ->
6505 { v with f = doc path pan anchor c bookmarks }
6507 | Vclose _ -> error "unexpected close in bookmarks" s spos
6509 and skip tag f v t spos _ =
6510 match t with
6511 | Vdata | Vcdata -> v
6512 | Vend ->
6513 error ("unexpected end of input in skipped " ^ tag) s spos
6514 | Vopen (tag', _, closed) ->
6515 if closed
6516 then v
6517 else
6518 let f' () = { v with f = skip tag f } in
6519 { v with f = skip tag' f' }
6520 | Vclose ctag ->
6521 if tag = ctag
6522 then f ()
6523 else error ("unexpected close in skipped " ^ tag) s spos
6526 parse { f = toplevel; accu = () } s;
6527 h, dc;
6530 let do_load f ic =
6532 let len = in_channel_length ic in
6533 let s = String.create len in
6534 really_input ic s 0 len;
6535 f s;
6536 with
6537 | Parse_error (msg, s, pos) ->
6538 let subs = subs s pos in
6539 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6540 failwith ("parse error: " ^ s)
6542 | exn ->
6543 failwith ("config load error: " ^ Printexc.to_string exn)
6546 let defconfpath =
6547 let dir =
6549 let dir = Filename.concat home ".config" in
6550 if Sys.is_directory dir then dir else home
6551 with _ -> home
6553 Filename.concat dir "llpp.conf"
6556 let confpath = ref defconfpath;;
6558 let load1 f =
6559 if Sys.file_exists !confpath
6560 then
6561 match
6562 (try Some (open_in_bin !confpath)
6563 with exn ->
6564 prerr_endline
6565 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6566 Printexc.to_string exn);
6567 None
6569 with
6570 | Some ic ->
6571 let success =
6573 f (do_load get ic)
6574 with exn ->
6575 prerr_endline
6576 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6577 Printexc.to_string exn);
6578 false
6580 close_in ic;
6581 success
6583 | None -> false
6584 else
6585 f (Hashtbl.create 0, defconf)
6588 let load () =
6589 let f (h, dc) =
6590 let pc, pb, px, pa =
6592 Hashtbl.find h (Filename.basename state.path)
6593 with Not_found -> dc, [], 0, emptyanchor
6595 setconf defconf dc;
6596 setconf conf pc;
6597 state.bookmarks <- pb;
6598 state.x <- px;
6599 state.scrollw <- conf.scrollbw;
6600 if conf.jumpback
6601 then state.anchor <- pa;
6602 cbput state.hists.nav pa;
6603 true
6605 load1 f
6608 let add_attrs bb always dc c =
6609 let ob s a b =
6610 if always || a != b
6611 then Printf.bprintf bb "\n %s='%b'" s a
6612 and oi s a b =
6613 if always || a != b
6614 then Printf.bprintf bb "\n %s='%d'" s a
6615 and oI s a b =
6616 if always || a != b
6617 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6618 and oz s a b =
6619 if always || a <> b
6620 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6621 and oF s a b =
6622 if always || a <> b
6623 then Printf.bprintf bb "\n %s='%f'" s a
6624 and oc s a b =
6625 if always || a <> b
6626 then
6627 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6628 and oC s a b =
6629 if always || a <> b
6630 then
6631 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6632 and oR s a b =
6633 if always || a <> b
6634 then
6635 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6636 and os s a b =
6637 if always || a <> b
6638 then
6639 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6640 and og s a b =
6641 if always || a <> b
6642 then
6643 match a with
6644 | None -> ()
6645 | Some (_N, _A, _B) ->
6646 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6647 and oW s a b =
6648 if always || a <> b
6649 then
6650 let v =
6651 match a with
6652 | None -> "false"
6653 | Some f ->
6654 if f = infinity
6655 then "true"
6656 else string_of_float f
6658 Printf.bprintf bb "\n %s='%s'" s v
6659 and oco s a b =
6660 if always || a <> b
6661 then
6662 match a with
6663 | Cmulti ((n, a, b), _) when n > 1 ->
6664 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6665 | Csplit (n, _) when n > 1 ->
6666 Printf.bprintf bb "\n %s='%d'" s ~-n
6667 | _ -> ()
6668 and obeco s a b =
6669 if always || a <> b
6670 then
6671 match a with
6672 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6673 | _ -> ()
6675 let w, h =
6676 if always
6677 then dc.winw, dc.winh
6678 else
6679 match state.fullscreen with
6680 | Some wh -> wh
6681 | None -> c.winw, c.winh
6683 oi "width" w dc.winw;
6684 oi "height" h dc.winh;
6685 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6686 oi "scroll-handle-height" c.scrollh dc.scrollh;
6687 ob "case-insensitive-search" c.icase dc.icase;
6688 ob "preload" c.preload dc.preload;
6689 oi "page-bias" c.pagebias dc.pagebias;
6690 oi "scroll-step" c.scrollstep dc.scrollstep;
6691 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6692 ob "max-height-fit" c.maxhfit dc.maxhfit;
6693 ob "crop-hack" c.crophack dc.crophack;
6694 oW "throttle" c.maxwait dc.maxwait;
6695 ob "highlight-links" c.hlinks dc.hlinks;
6696 ob "under-cursor-info" c.underinfo dc.underinfo;
6697 oi "vertical-margin" c.interpagespace dc.interpagespace;
6698 oz "zoom" c.zoom dc.zoom;
6699 ob "presentation" c.presentation dc.presentation;
6700 oi "rotation-angle" c.angle dc.angle;
6701 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6702 ob "proportional-display" c.proportional dc.proportional;
6703 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6704 oi "tex-count" c.texcount dc.texcount;
6705 oi "slice-height" c.sliceheight dc.sliceheight;
6706 oi "thumbnail-width" c.thumbw dc.thumbw;
6707 ob "persistent-location" c.jumpback dc.jumpback;
6708 oc "background-color" c.bgcolor dc.bgcolor;
6709 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6710 oi "tile-width" c.tilew dc.tilew;
6711 oi "tile-height" c.tileh dc.tileh;
6712 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6713 ob "checkers" c.checkers dc.checkers;
6714 oi "aalevel" c.aalevel dc.aalevel;
6715 ob "trim-margins" c.trimmargins dc.trimmargins;
6716 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6717 os "uri-launcher" c.urilauncher dc.urilauncher;
6718 os "path-launcher" c.pathlauncher dc.pathlauncher;
6719 oC "color-space" c.colorspace dc.colorspace;
6720 ob "invert-colors" c.invert dc.invert;
6721 oF "brightness" c.colorscale dc.colorscale;
6722 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6723 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6724 oco "columns" c.columns dc.columns;
6725 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6726 os "selection-command" c.selcmd dc.selcmd;
6727 os "synctex-command" c.stcmd dc.stcmd;
6728 ob "update-cursor" c.updatecurs dc.updatecurs;
6729 oi "hint-font-size" c.hfsize dc.hfsize;
6730 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6731 oF "page-scroll-scale" c.pgscale dc.pgscale;
6732 ob "use-pbo" c.usepbo dc.usepbo;
6733 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6736 let keymapsbuf always dc c =
6737 let bb = Buffer.create 16 in
6738 let rec loop = function
6739 | [] -> ()
6740 | (modename, h) :: rest ->
6741 let dh = findkeyhash dc modename in
6742 if always || h <> dh
6743 then (
6744 if Hashtbl.length h > 0
6745 then (
6746 if Buffer.length bb > 0
6747 then Buffer.add_char bb '\n';
6748 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6749 Hashtbl.iter (fun i o ->
6750 let isdifferent = always ||
6752 let dO = Hashtbl.find dh i in
6753 dO <> o
6754 with Not_found -> true
6756 if isdifferent
6757 then
6758 let addkm (k, m) =
6759 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6760 if Wsi.withalt m then Buffer.add_string bb "alt-";
6761 if Wsi.withshift m then Buffer.add_string bb "shift-";
6762 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6763 Buffer.add_string bb (Wsi.keyname k);
6765 let addkms l =
6766 let rec loop = function
6767 | [] -> ()
6768 | km :: [] -> addkm km
6769 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6771 loop l
6773 Buffer.add_string bb "<map in='";
6774 addkm i;
6775 match o with
6776 | KMinsrt km ->
6777 Buffer.add_string bb "' out='";
6778 addkm km;
6779 Buffer.add_string bb "'/>\n"
6781 | KMinsrl kms ->
6782 Buffer.add_string bb "' out='";
6783 addkms kms;
6784 Buffer.add_string bb "'/>\n"
6786 | KMmulti (ins, kms) ->
6787 Buffer.add_char bb ' ';
6788 addkms ins;
6789 Buffer.add_string bb "' out='";
6790 addkms kms;
6791 Buffer.add_string bb "'/>\n"
6792 ) h;
6793 Buffer.add_string bb "</keymap>";
6796 loop rest
6798 loop c.keyhashes;
6802 let save () =
6803 let uifontsize = fstate.fontsize in
6804 let bb = Buffer.create 32768 in
6805 let f (h, dc) =
6806 let dc = if conf.bedefault then conf else dc in
6807 Buffer.add_string bb "<llppconfig>\n";
6809 if String.length !fontpath > 0
6810 then
6811 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6812 uifontsize
6813 !fontpath
6814 else (
6815 if uifontsize <> 14
6816 then
6817 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6820 Buffer.add_string bb "<defaults ";
6821 add_attrs bb true dc dc;
6822 let kb = keymapsbuf true dc dc in
6823 if Buffer.length kb > 0
6824 then (
6825 Buffer.add_string bb ">\n";
6826 Buffer.add_buffer bb kb;
6827 Buffer.add_string bb "\n</defaults>\n";
6829 else Buffer.add_string bb "/>\n";
6831 let adddoc path pan anchor c bookmarks =
6832 if bookmarks == [] && c = dc && anchor = emptyanchor
6833 then ()
6834 else (
6835 Printf.bprintf bb "<doc path='%s'"
6836 (enent path 0 (String.length path));
6838 if anchor <> emptyanchor
6839 then (
6840 let n, rely, visy = anchor in
6841 Printf.bprintf bb " page='%d'" n;
6842 if rely > 1e-6
6843 then
6844 Printf.bprintf bb " rely='%f'" rely
6846 if abs_float visy > 1e-6
6847 then
6848 Printf.bprintf bb " visy='%f'" visy
6852 if pan != 0
6853 then Printf.bprintf bb " pan='%d'" pan;
6855 add_attrs bb false dc c;
6856 let kb = keymapsbuf false dc c in
6858 begin match bookmarks with
6859 | [] ->
6860 if Buffer.length kb > 0
6861 then (
6862 Buffer.add_string bb ">\n";
6863 Buffer.add_buffer bb kb;
6864 Buffer.add_string bb "\n</doc>\n";
6866 else Buffer.add_string bb "/>\n"
6867 | _ ->
6868 Buffer.add_string bb ">\n<bookmarks>\n";
6869 List.iter (fun (title, _level, (page, rely, visy)) ->
6870 Printf.bprintf bb
6871 "<item title='%s' page='%d'"
6872 (enent title 0 (String.length title))
6873 page
6875 if rely > 1e-6
6876 then
6877 Printf.bprintf bb " rely='%f'" rely
6879 if abs_float visy > 1e-6
6880 then
6881 Printf.bprintf bb " visy='%f'" visy
6883 Buffer.add_string bb "/>\n";
6884 ) bookmarks;
6885 Buffer.add_string bb "</bookmarks>";
6886 if Buffer.length kb > 0
6887 then (
6888 Buffer.add_string bb "\n";
6889 Buffer.add_buffer bb kb;
6891 Buffer.add_string bb "\n</doc>\n";
6892 end;
6896 let pan, conf =
6897 match state.mode with
6898 | Birdseye (c, pan, _, _, _) ->
6899 let beyecolumns =
6900 match conf.columns with
6901 | Cmulti ((c, _, _), _) -> Some c
6902 | Csingle _ -> None
6903 | Csplit _ -> None
6904 and columns =
6905 match c.columns with
6906 | Cmulti (c, _) -> Cmulti (c, [||])
6907 | Csingle _ -> Csingle [||]
6908 | Csplit _ -> failwith "quit from bird's eye while split"
6910 pan, { c with beyecolumns = beyecolumns; columns = columns }
6911 | _ -> state.x, conf
6913 let basename = Filename.basename state.path in
6914 adddoc basename pan (getanchor ())
6915 (let conf =
6916 let autoscrollstep =
6917 match state.autoscroll with
6918 | Some step -> step
6919 | None -> conf.autoscrollstep
6921 match state.mode with
6922 | Birdseye (bc, _, _, _, _) ->
6923 { conf with
6924 zoom = bc.zoom;
6925 presentation = bc.presentation;
6926 interpagespace = bc.interpagespace;
6927 maxwait = bc.maxwait;
6928 autoscrollstep = autoscrollstep }
6929 | _ -> { conf with autoscrollstep = autoscrollstep }
6930 in conf)
6931 (if conf.savebmarks then state.bookmarks else []);
6933 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6934 if basename <> path
6935 then adddoc path x anchor c bookmarks
6936 ) h;
6937 Buffer.add_string bb "</llppconfig>\n";
6938 true;
6940 if load1 f && Buffer.length bb > 0
6941 then
6943 let tmp = !confpath ^ ".tmp" in
6944 let oc = open_out_bin tmp in
6945 Buffer.output_buffer oc bb;
6946 close_out oc;
6947 Unix.rename tmp !confpath;
6948 with exn ->
6949 prerr_endline
6950 ("error while saving configuration: " ^ Printexc.to_string exn)
6952 end;;
6954 let adderrmsg src msg =
6955 Buffer.add_string state.errmsgs msg;
6956 state.newerrmsgs <- true;
6957 G.postRedisplay src
6960 let adderrfmt src fmt =
6961 Format.kprintf (fun s -> adderrmsg src s) fmt;
6964 let onpagerect pageno f =
6965 let b =
6966 match conf.columns with
6967 | Cmulti (_, b) -> b
6968 | Csingle b -> b
6969 | Csplit (_, b) -> b
6971 if pageno >= 0 && pageno < Array.length b
6972 then
6973 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
6974 let r = getpdimrect pdimno in
6975 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
6978 let ract cmds =
6979 let cl = splitatspace cmds in
6980 let scan s fmt f =
6981 try Scanf.sscanf s fmt f
6982 with exn ->
6983 adderrfmt "remote exec"
6984 "error processing '%S': %s\n" cmds (Printexc.to_string exn)
6986 match cl with
6987 | "reload" :: [] -> reload ()
6988 | "goto" :: args :: [] ->
6989 scan args "%u %f %f"
6990 (fun pageno _ y ->
6991 onpagerect pageno (fun _ h ->
6992 let top = y /. h in
6993 gotopage pageno top;
6996 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6997 | "rect" :: args :: [] ->
6998 scan args "%u %u %f %f %f %f"
6999 (fun pageno color x0 y0 x1 y1 ->
7000 onpagerect pageno (fun w h ->
7001 let _,w1,h1,_ = getpagedim pageno in
7002 let sw = float w1 /. w
7003 and sh = float h1 /. h in
7004 let x0s = x0 *. sw
7005 and x1s = x1 *. sw
7006 and y0s = y0 *. sh
7007 and y1s = y1 *. sh in
7008 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7009 debugrect rect;
7010 state.rects <- (pageno, color, rect) :: state.rects;
7011 G.postRedisplay "rect";
7014 | "quit" :: [] -> raise Quit
7015 | _ ->
7016 adderrfmt "remote command"
7017 "error processing remote command: %S\n" cmds;
7020 let remote =
7021 let scratch = String.create 80 in
7022 let buf = Buffer.create 80 in
7023 fun fd ->
7024 let rec tempfr () =
7025 try Some (Unix.read fd scratch 0 80)
7026 with
7027 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7028 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7029 | exn -> raise exn
7031 match tempfr () with
7032 | None -> Some fd
7033 | Some n ->
7034 if n = 0
7035 then (
7036 Unix.close fd;
7037 if Buffer.length buf > 0
7038 then (
7039 let s = Buffer.contents buf in
7040 Buffer.clear buf;
7041 ract s;
7043 None
7045 else
7046 let rec eat ppos =
7047 let nlpos =
7049 let pos = String.index_from scratch ppos '\n' in
7050 if pos >= n then -1 else pos
7051 with Not_found -> -1
7053 if nlpos >= 0
7054 then (
7055 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7056 let s = Buffer.contents buf in
7057 Buffer.clear buf;
7058 ract s;
7059 eat (nlpos+1);
7061 else (
7062 Buffer.add_substring buf scratch ppos (n-ppos);
7063 Some fd
7065 in eat 0
7068 let remoteopen path =
7069 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7070 with exn ->
7071 adderrfmt "remoteopen" "error opening %S: %s" path (Printexc.to_string exn);
7072 None
7075 let () =
7076 let trimcachepath = ref "" in
7077 let rcmdpath = ref "" in
7078 Arg.parse
7079 (Arg.align
7080 [("-p", Arg.String (fun s -> state.password <- s) ,
7081 "<password> Set password");
7083 ("-f", Arg.String (fun s -> Config.fontpath := s),
7084 "<path> Set path to the user interface font");
7086 ("-c", Arg.String (fun s -> Config.confpath := s),
7087 "<path> Set path to the configuration file");
7089 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7090 "<path> Set path to the trim cache file");
7092 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7093 "<named-destination> Set named destination");
7095 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7097 ("-remote", Arg.String (fun s -> rcmdpath := s),
7098 "<path> Set path to the remote commands source");
7100 ("-v", Arg.Unit (fun () ->
7101 Printf.printf
7102 "%s\nconfiguration path: %s\n"
7103 (version ())
7104 Config.defconfpath
7106 exit 0), " Print version and exit");
7109 (fun s -> state.path <- s)
7110 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7112 if String.length state.path = 0
7113 then (prerr_endline "file name missing"; exit 1);
7115 if not (Config.load ())
7116 then prerr_endline "failed to load configuration";
7118 let globalkeyhash = findkeyhash conf "global" in
7119 let wsfd, winw, winh = Wsi.init (object
7120 method expose =
7121 state.wthack <- false;
7122 if nogeomcmds state.geomcmds || platform == Posx
7123 then display ()
7124 else (
7125 GlClear.color (scalecolor2 conf.bgcolor);
7126 GlClear.clear [`color];
7128 method display = display ()
7129 method reshape w h = reshape w h
7130 method mouse b d x y m = mouse b d x y m
7131 method motion x y = state.mpos <- (x, y); motion x y
7132 method pmotion x y = state.mpos <- (x, y); pmotion x y
7133 method key k m =
7134 let mascm = m land (
7135 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7136 ) in
7137 match state.keystate with
7138 | KSnone ->
7139 let km = k, mascm in
7140 begin
7141 match
7142 let modehash = state.uioh#modehash in
7143 try Hashtbl.find modehash km
7144 with Not_found ->
7145 try Hashtbl.find globalkeyhash km
7146 with Not_found -> KMinsrt (k, m)
7147 with
7148 | KMinsrt (k, m) -> keyboard k m
7149 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7150 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7152 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7153 List.iter (fun (k, m) -> keyboard k m) insrt;
7154 state.keystate <- KSnone
7155 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7156 state.keystate <- KSinto (keys, insrt)
7157 | _ ->
7158 state.keystate <- KSnone
7160 method enter x y = state.mpos <- (x, y); pmotion x y
7161 method leave = state.mpos <- (-1, -1)
7162 method quit = raise Quit
7163 end) conf.winw conf.winh (platform = Posx) in
7165 state.wsfd <- wsfd;
7167 if not (
7168 List.exists GlMisc.check_extension
7169 [ "GL_ARB_texture_rectangle"
7170 ; "GL_EXT_texture_recangle"
7171 ; "GL_NV_texture_rectangle" ]
7173 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7175 let cr, sw =
7176 match Ne.pipe () with
7177 | Ne.Exn exn ->
7178 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
7179 exit 1
7180 | Ne.Res rw -> rw
7181 and sr, cw =
7182 match Ne.pipe () with
7183 | Ne.Exn exn ->
7184 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7185 exit 1
7186 | Ne.Res rw -> rw
7189 cloexec cr;
7190 cloexec sw;
7191 cloexec sr;
7192 cloexec cw;
7194 setcheckers conf.checkers;
7195 redirectstderr ();
7197 init (cr, cw) (
7198 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7199 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7200 !Config.fontpath, !trimcachepath,
7201 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7203 state.sr <- sr;
7204 state.sw <- sw;
7205 state.text <- "Opening " ^ (mbtoutf8 state.path);
7206 reshape winw winh;
7207 opendoc state.path state.password;
7208 state.uioh <- uioh;
7210 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7211 let optrfd =
7212 ref (
7213 if String.length !rcmdpath > 0
7214 then remoteopen !rcmdpath
7215 else None
7219 let rec loop deadline =
7220 let r =
7221 match state.errfd with
7222 | None -> [state.sr; state.wsfd]
7223 | Some fd -> [state.sr; state.wsfd; fd]
7225 let r =
7226 match !optrfd with
7227 | None -> r
7228 | Some fd -> fd :: r
7230 if state.redisplay && not state.wthack
7231 then (
7232 state.redisplay <- false;
7233 display ();
7235 let timeout =
7236 let now = now () in
7237 if deadline > now
7238 then (
7239 if deadline = infinity
7240 then ~-.1.0
7241 else max 0.0 (deadline -. now)
7243 else 0.0
7245 let r, _, _ =
7246 try Unix.select r [] [] timeout
7247 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7249 begin match r with
7250 | [] ->
7251 state.ghyll None;
7252 let newdeadline =
7253 if state.ghyll == noghyll
7254 then
7255 match state.autoscroll with
7256 | Some step when step != 0 ->
7257 let y = state.y + step in
7258 let y =
7259 if y < 0
7260 then state.maxy
7261 else if y >= state.maxy then 0 else y
7263 gotoy y;
7264 if state.mode = View
7265 then state.text <- "";
7266 deadline +. 0.01
7267 | _ -> infinity
7268 else deadline +. 0.01
7270 loop newdeadline
7272 | l ->
7273 let rec checkfds = function
7274 | [] -> ()
7275 | fd :: rest when fd = state.sr ->
7276 let cmd = readcmd state.sr in
7277 act cmd;
7278 checkfds rest
7280 | fd :: rest when fd = state.wsfd ->
7281 Wsi.readresp fd;
7282 checkfds rest
7284 | fd :: rest when Some fd = !optrfd ->
7285 begin match remote fd with
7286 | None -> optrfd := remoteopen !rcmdpath;
7287 | opt -> optrfd := opt
7288 end;
7289 checkfds rest
7291 | fd :: rest ->
7292 let s = String.create 80 in
7293 let n = tempfailureretry (Unix.read fd s 0) 80 in
7294 if conf.redirectstderr
7295 then (
7296 Buffer.add_substring state.errmsgs s 0 n;
7297 state.newerrmsgs <- true;
7298 state.redisplay <- true;
7300 else (
7301 prerr_string (String.sub s 0 n);
7302 flush stderr;
7304 checkfds rest
7306 checkfds l;
7307 let newdeadline =
7308 let deadline1 =
7309 if deadline = infinity
7310 then now () +. 0.01
7311 else deadline
7313 match state.autoscroll with
7314 | Some step when step != 0 -> deadline1
7315 | _ -> if state.ghyll == noghyll then infinity else deadline1
7317 loop newdeadline
7318 end;
7321 loop infinity;
7322 with Quit ->
7323 Config.save ();