Flush all the tiles on opendoc
[llpp.git] / main.ml
blobdf2e1ddf180d5c3827a168d36a81a1896134a533
1 open Utils;;
3 exception Quit;;
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 type params = (angle * proportional * trimparams
17 * texcount * sliceheight * memsize
18 * colorspace * fontpath * trimcachepath
19 * haspbo)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and dtop = float
36 and fontpath = string
37 and trimcachepath = string
38 and memsize = int
39 and aalevel = int
40 and irect = (int * int * int * int)
41 and trimparams = (trimmargins * irect)
42 and colorspace = | Rgb | Bgr | Gray
43 and haspbo = bool
46 type x = int
47 and y = int
48 and tilex = int
49 and tiley = int
50 and tileparams = (x * y * width * height * tilex * tiley)
53 type link =
54 | Lnotfound
55 | Lfound of int
56 and linkdir =
57 | LDfirst
58 | LDlast
59 | LDfirstvisible of (int * int * int)
60 | LDleft of int
61 | LDright of int
62 | LDdown of int
63 | LDup of int
66 type pagewithlinks =
67 | Pwlnotfound
68 | Pwl of int
71 type keymap =
72 | KMinsrt of key
73 | KMinsrl of key list
74 | KMmulti of key list * key list
75 and key = int * int
76 and keyhash = (key, keymap) Hashtbl.t
77 and keystate =
78 | KSnone
79 | KSinto of (key list * key list)
82 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
83 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
85 type pipe = (Unix.file_descr * Unix.file_descr);;
87 external init : pipe -> params -> unit = "ml_init";;
88 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
89 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
90 external getpdimrect : int -> float array = "ml_getpdimrect";;
91 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
92 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
93 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
94 external measurestr : int -> string -> float = "ml_measure_string";;
95 external getmaxw : unit -> float = "ml_getmaxw";;
96 external postprocess :
97 opaque -> int -> int -> int -> (int * string * int) -> int
98 = "ml_postprocess";;
99 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
100 external platform : unit -> platform = "ml_platform";;
101 external setaalevel : int -> unit = "ml_setaalevel";;
102 external realloctexts : int -> bool = "ml_realloctexts";;
103 external findlink : opaque -> linkdir -> link = "ml_findlink";;
104 external getlink : opaque -> int -> under = "ml_getlink";;
105 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
106 external getlinkcount : opaque -> int = "ml_getlinkcount";;
107 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
108 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
109 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
110 external freepbo : string -> unit = "ml_freepbo";;
111 external unmappbo : string -> unit = "ml_unmappbo";;
112 external pbousable : unit -> bool = "ml_pbo_usable";;
113 external unproject : opaque -> int -> int -> (int * int) option
114 = "ml_unproject";;
115 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
117 let platform_to_string = function
118 | Punknown -> "unknown"
119 | Plinux -> "Linux"
120 | Posx -> "OSX"
121 | Psun -> "Sun"
122 | Pfreebsd -> "FreeBSD"
123 | Pdragonflybsd -> "DragonflyBSD"
124 | Popenbsd -> "OpenBSD"
125 | Pnetbsd -> "NetBSD"
126 | Pcygwin -> "Cygwin"
129 let platform = platform ();;
131 let now = Unix.gettimeofday;;
133 let popen cmd fda =
134 if platform = Pcygwin
135 then (
136 let sh = "/bin/sh" in
137 let args = [|sh; "-c"; cmd|] in
138 let rec std si so se = function
139 | [] -> si, so, se
140 | (fd, 0) :: rest -> std fd so se rest
141 | (fd, -1) :: rest ->
142 Unix.set_close_on_exec fd;
143 std si so se rest
144 | (_, n) :: _ ->
145 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
147 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
148 ignore (Unix.create_process sh args si so se)
150 else popen cmd fda;
153 type mpos = int * int
154 and mstate =
155 | Msel of (mpos * mpos)
156 | Mpan of mpos
157 | Mscrolly | Mscrollx
158 | Mzoom of (int * int)
159 | Mzoomrect of (mpos * mpos)
160 | Mnone
163 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
164 and onkey = string -> int -> te
165 and ondone = string -> unit
166 and histcancel = unit -> unit
167 and onhist = ((histcmd -> string) * histcancel)
168 and histcmd = HCnext | HCprev | HCfirst | HClast
169 and cancelonempty = bool
170 and te =
171 | TEstop
172 | TEdone of string
173 | TEcont of string
174 | TEswitch of textentry
177 type 'a circbuf =
178 { store : 'a array
179 ; mutable rc : int
180 ; mutable wc : int
181 ; mutable len : int
185 let bound v minv maxv =
186 max minv (min maxv v);
189 let cbnew n v =
190 { store = Array.create n v
191 ; rc = 0
192 ; wc = 0
193 ; len = 0
197 let cbcap b = Array.length b.store;;
199 let cbput b v =
200 let cap = cbcap b in
201 b.store.(b.wc) <- v;
202 b.wc <- (b.wc + 1) mod cap;
203 b.rc <- b.wc;
204 b.len <- min (b.len + 1) cap;
207 let cbempty b = b.len = 0;;
209 let cbgetg b circular dir =
210 if cbempty b
211 then b.store.(0)
212 else
213 let rc = b.rc + dir in
214 let rc =
215 if circular
216 then (
217 if rc = -1
218 then b.len-1
219 else (
220 if rc >= b.len
221 then 0
222 else rc
225 else bound rc 0 (b.len-1)
227 b.rc <- rc;
228 b.store.(rc);
231 let cbget b = cbgetg b false;;
232 let cbgetc b = cbgetg b true;;
234 let drawstring size x y s =
235 Gl.enable `blend;
236 Gl.enable `texture_2d;
237 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
238 ignore (drawstr size x y s);
239 Gl.disable `blend;
240 Gl.disable `texture_2d;
243 let drawstring1 size x y s =
244 drawstr size x y s;
247 let drawstring2 size x y fmt =
248 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
251 type page =
252 { pageno : int
253 ; pagedimno : int
254 ; pagew : int
255 ; pageh : int
256 ; pagex : int
257 ; pagey : int
258 ; pagevw : int
259 ; pagevh : int
260 ; pagedispx : int
261 ; pagedispy : int
262 ; pagecol : int
266 let debugl l =
267 dolog "l %d dim=%d {" l.pageno l.pagedimno;
268 dolog " WxH %dx%d" l.pagew l.pageh;
269 dolog " vWxH %dx%d" l.pagevw l.pagevh;
270 dolog " pagex,y %d,%d" l.pagex l.pagey;
271 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
272 dolog " column %d" l.pagecol;
273 dolog "}";
276 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
277 dolog "rect {";
278 dolog " x0,y0=(% f, % f)" x0 y0;
279 dolog " x1,y1=(% f, % f)" x1 y1;
280 dolog " x2,y2=(% f, % f)" x2 y2;
281 dolog " x3,y3=(% f, % f)" x3 y3;
282 dolog "}";
285 type multicolumns = multicol * pagegeom
286 and singlecolumn = pagegeom
287 and splitcolumns = columncount * pagegeom
288 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
289 and multicol = columncount * covercount * covercount
290 and pdimno = int
291 and columncount = int
292 and covercount = int;;
294 type conf =
295 { mutable scrollbw : int
296 ; mutable scrollh : int
297 ; mutable icase : bool
298 ; mutable preload : bool
299 ; mutable pagebias : int
300 ; mutable verbose : bool
301 ; mutable debug : bool
302 ; mutable scrollstep : int
303 ; mutable hscrollstep : int
304 ; mutable maxhfit : bool
305 ; mutable crophack : bool
306 ; mutable autoscrollstep : int
307 ; mutable maxwait : float option
308 ; mutable hlinks : bool
309 ; mutable underinfo : bool
310 ; mutable interpagespace : interpagespace
311 ; mutable zoom : float
312 ; mutable presentation : bool
313 ; mutable angle : angle
314 ; mutable cwinw : int
315 ; mutable cwinh : int
316 ; mutable savebmarks : bool
317 ; mutable proportional : proportional
318 ; mutable trimmargins : trimmargins
319 ; mutable trimfuzz : irect
320 ; mutable memlimit : memsize
321 ; mutable texcount : texcount
322 ; mutable sliceheight : sliceheight
323 ; mutable thumbw : width
324 ; mutable jumpback : bool
325 ; mutable bgcolor : float * float * float
326 ; mutable bedefault : bool
327 ; mutable scrollbarinpm : bool
328 ; mutable tilew : int
329 ; mutable tileh : int
330 ; mutable mustoresize : memsize
331 ; mutable checkers : bool
332 ; mutable aalevel : int
333 ; mutable urilauncher : string
334 ; mutable pathlauncher : string
335 ; mutable colorspace : colorspace
336 ; mutable invert : bool
337 ; mutable colorscale : float
338 ; mutable redirectstderr : bool
339 ; mutable ghyllscroll : (int * int * int) option
340 ; mutable columns : columns
341 ; mutable beyecolumns : columncount option
342 ; mutable selcmd : string
343 ; mutable updatecurs : bool
344 ; mutable keyhashes : (string * keyhash) list
345 ; mutable hfsize : int
346 ; mutable pgscale : float
347 ; mutable usepbo : bool
348 ; mutable wheelbypage : bool
349 ; mutable stcmd : string
351 and columns =
352 | Csingle of singlecolumn
353 | Cmulti of multicolumns
354 | Csplit of splitcolumns
357 type anchor = pageno * top * dtop;;
359 type outline = string * int * anchor;;
361 type rect = float * float * float * float * float * float * float * float;;
363 type tile = opaque * pixmapsize * elapsed
364 and elapsed = float;;
365 type pagemapkey = pageno * gen;;
366 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
367 and row = int
368 and col = int;;
370 let emptyanchor = (0, 0.0, 0.0);;
372 type infochange = | Memused | Docinfo | Pdim;;
374 class type uioh = object
375 method display : unit
376 method key : int -> int -> uioh
377 method button : int -> bool -> int -> int -> int -> uioh
378 method motion : int -> int -> uioh
379 method pmotion : int -> int -> uioh
380 method infochanged : infochange -> unit
381 method scrollpw : (int * float * float)
382 method scrollph : (int * float * float)
383 method modehash : keyhash
384 end;;
386 type mode =
387 | Birdseye of (conf * leftx * pageno * pageno * anchor)
388 | Textentry of (textentry * onleave)
389 | View
390 | LinkNav of linktarget
391 and onleave = leavetextentrystatus -> unit
392 and leavetextentrystatus = | Cancel | Confirm
393 and helpitem = string * int * action
394 and action =
395 | Noaction
396 | Action of (uioh -> uioh)
397 and linktarget =
398 | Ltexact of (pageno * int)
399 | Ltgendir of int
402 let isbirdseye = function Birdseye _ -> true | _ -> false;;
403 let istextentry = function Textentry _ -> true | _ -> false;;
405 type currently =
406 | Idle
407 | Loading of (page * gen)
408 | Tiling of (
409 page * opaque * colorspace * angle * gen * col * row * width * height
411 | Outlining of outline list
414 let emptykeyhash = Hashtbl.create 0;;
415 let nouioh : uioh = object (self)
416 method display = ()
417 method key _ _ = self
418 method button _ _ _ _ _ = self
419 method motion _ _ = self
420 method pmotion _ _ = self
421 method infochanged _ = ()
422 method scrollpw = (0, nan, nan)
423 method scrollph = (0, nan, nan)
424 method modehash = emptykeyhash
425 end;;
427 type state =
428 { mutable sr : Unix.file_descr
429 ; mutable sw : Unix.file_descr
430 ; mutable wsfd : Unix.file_descr
431 ; mutable errfd : Unix.file_descr option
432 ; mutable stderr : Unix.file_descr
433 ; mutable errmsgs : Buffer.t
434 ; mutable newerrmsgs : bool
435 ; mutable w : int
436 ; mutable x : int
437 ; mutable y : int
438 ; mutable scrollw : int
439 ; mutable hscrollh : int
440 ; mutable anchor : anchor
441 ; mutable ranchors : (string * string * anchor) list
442 ; mutable maxy : int
443 ; mutable layout : page list
444 ; pagemap : (pagemapkey, opaque) Hashtbl.t
445 ; tilemap : (tilemapkey, tile) Hashtbl.t
446 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
447 ; mutable pdims : (pageno * width * height * leftx) list
448 ; mutable pagecount : int
449 ; mutable currently : currently
450 ; mutable mstate : mstate
451 ; mutable searchpattern : string
452 ; mutable rects : (pageno * recttype * rect) list
453 ; mutable rects1 : (pageno * recttype * rect) list
454 ; mutable text : string
455 ; mutable winstate : Wsi.winstate list
456 ; mutable mode : mode
457 ; mutable uioh : uioh
458 ; mutable outlines : outline array
459 ; mutable bookmarks : outline list
460 ; mutable path : string
461 ; mutable password : string
462 ; mutable nameddest : string
463 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
464 ; mutable memused : memsize
465 ; mutable gen : gen
466 ; mutable throttle : (page list * int * float) option
467 ; mutable autoscroll : int option
468 ; mutable ghyll : (int option -> unit)
469 ; mutable help : helpitem array
470 ; mutable docinfo : (int * string) list
471 ; mutable texid : GlTex.texture_id option
472 ; hists : hists
473 ; mutable prevzoom : float
474 ; mutable progress : float
475 ; mutable redisplay : bool
476 ; mutable mpos : mpos
477 ; mutable keystate : keystate
478 ; mutable glinks : bool
479 ; mutable prevcolumns : (columns * float) option
480 ; mutable wthack : bool
481 ; mutable winw : int
482 ; mutable winh : int
483 ; mutable reprf : (unit -> unit)
485 and hists =
486 { pat : string circbuf
487 ; pag : string circbuf
488 ; nav : anchor circbuf
489 ; sel : string circbuf
493 let defconf =
494 { scrollbw = 7
495 ; scrollh = 12
496 ; icase = true
497 ; preload = true
498 ; pagebias = 0
499 ; verbose = false
500 ; debug = false
501 ; scrollstep = 24
502 ; hscrollstep = 24
503 ; maxhfit = true
504 ; crophack = false
505 ; autoscrollstep = 2
506 ; maxwait = None
507 ; hlinks = false
508 ; underinfo = false
509 ; interpagespace = 2
510 ; zoom = 1.0
511 ; presentation = false
512 ; angle = 0
513 ; cwinw = 900
514 ; cwinh = 900
515 ; savebmarks = true
516 ; proportional = true
517 ; trimmargins = false
518 ; trimfuzz = (0,0,0,0)
519 ; memlimit = 32 lsl 20
520 ; texcount = 256
521 ; sliceheight = 24
522 ; thumbw = 76
523 ; jumpback = true
524 ; bgcolor = (0.5, 0.5, 0.5)
525 ; bedefault = false
526 ; scrollbarinpm = true
527 ; tilew = 2048
528 ; tileh = 2048
529 ; mustoresize = 256 lsl 20
530 ; checkers = true
531 ; aalevel = 8
532 ; urilauncher =
533 (match platform with
534 | Plinux | Pfreebsd | Pdragonflybsd
535 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
536 | Posx -> "open \"%s\""
537 | Pcygwin -> "cygstart \"%s\""
538 | Punknown -> "echo %s")
539 ; pathlauncher = "lp \"%s\""
540 ; selcmd =
541 (match platform with
542 | Plinux | Pfreebsd | Pdragonflybsd
543 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
544 | Posx -> "pbcopy"
545 | Pcygwin -> "wsel"
546 | Punknown -> "cat")
547 ; colorspace = Rgb
548 ; invert = false
549 ; colorscale = 1.0
550 ; redirectstderr = false
551 ; ghyllscroll = None
552 ; columns = Csingle [||]
553 ; beyecolumns = None
554 ; updatecurs = false
555 ; hfsize = 12
556 ; pgscale = 1.0
557 ; usepbo = false
558 ; wheelbypage = false
559 ; stcmd = "echo SyncTex"
560 ; keyhashes =
561 let mk n = (n, Hashtbl.create 1) in
562 [ mk "global"
563 ; mk "info"
564 ; mk "help"
565 ; mk "outline"
566 ; mk "listview"
567 ; mk "birdseye"
568 ; mk "textentry"
569 ; mk "links"
570 ; mk "view"
575 let wtmode = ref false;;
577 let findkeyhash c name =
578 try List.assoc name c.keyhashes
579 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
582 let conf = { defconf with angle = defconf.angle };;
584 let pgscale h = truncate (float h *. conf.pgscale);;
586 type fontstate =
587 { mutable fontsize : int
588 ; mutable wwidth : float
589 ; mutable maxrows : int
593 let fstate =
594 { fontsize = 14
595 ; wwidth = nan
596 ; maxrows = -1
600 let geturl s =
601 let colonpos = try String.index s ':' with Not_found -> -1 in
602 let len = String.length s in
603 if colonpos >= 0 && colonpos + 3 < len
604 then (
605 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
606 then
607 let schemestartpos =
608 try String.rindex_from s colonpos ' '
609 with Not_found -> -1
611 let scheme =
612 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
614 match scheme with
615 | "http" | "ftp" | "mailto" ->
616 let epos =
617 try String.index_from s colonpos ' '
618 with Not_found -> len
620 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
621 | _ -> ""
622 else ""
624 else ""
627 let gotouri uri =
628 if String.length conf.urilauncher = 0
629 then print_endline uri
630 else (
631 let url = geturl uri in
632 if String.length url = 0
633 then print_endline uri
634 else
635 let re = Str.regexp "%s" in
636 let command = Str.global_replace re url conf.urilauncher in
637 try popen command []
638 with exn ->
639 Printf.eprintf
640 "failed to execute `%s': %s\n" command (exntos exn);
641 flush stderr;
645 let version () =
646 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
647 (platform_to_string platform) Sys.word_size Sys.ocaml_version
650 let makehelp () =
651 let strings = version () :: "" :: Help.keys in
652 Array.of_list (
653 List.map (fun s ->
654 let url = geturl s in
655 if String.length url > 0
656 then (s, 0, Action (fun u -> gotouri url; u))
657 else (s, 0, Noaction)
658 ) strings);
661 let noghyll _ = ();;
662 let firstgeomcmds = "", [];;
663 let noreprf () = ();;
665 let state =
666 { sr = Unix.stdin
667 ; sw = Unix.stdin
668 ; wsfd = Unix.stdin
669 ; errfd = None
670 ; stderr = Unix.stderr
671 ; errmsgs = Buffer.create 0
672 ; newerrmsgs = false
673 ; x = 0
674 ; y = 0
675 ; w = 0
676 ; scrollw = 0
677 ; hscrollh = 0
678 ; anchor = emptyanchor
679 ; ranchors = []
680 ; layout = []
681 ; maxy = max_int
682 ; tilelru = Queue.create ()
683 ; pagemap = Hashtbl.create 10
684 ; tilemap = Hashtbl.create 10
685 ; pdims = []
686 ; pagecount = 0
687 ; currently = Idle
688 ; mstate = Mnone
689 ; rects = []
690 ; rects1 = []
691 ; text = ""
692 ; mode = View
693 ; winstate = []
694 ; searchpattern = ""
695 ; outlines = [||]
696 ; bookmarks = []
697 ; path = ""
698 ; password = ""
699 ; nameddest = ""
700 ; geomcmds = firstgeomcmds
701 ; hists =
702 { nav = cbnew 10 emptyanchor
703 ; pat = cbnew 10 ""
704 ; pag = cbnew 10 ""
705 ; sel = cbnew 10 ""
707 ; memused = 0
708 ; gen = 0
709 ; throttle = None
710 ; autoscroll = None
711 ; ghyll = noghyll
712 ; help = makehelp ()
713 ; docinfo = []
714 ; texid = None
715 ; prevzoom = 1.0
716 ; progress = -1.0
717 ; uioh = nouioh
718 ; redisplay = true
719 ; mpos = (-1, -1)
720 ; keystate = KSnone
721 ; glinks = false
722 ; prevcolumns = None
723 ; wthack = false
724 ; winw = -1
725 ; winh = -1
726 ; reprf = noreprf
730 let setfontsize n =
731 fstate.fontsize <- n;
732 fstate.wwidth <- measurestr fstate.fontsize "w";
733 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
736 let vlog fmt =
737 if conf.verbose
738 then
739 Printf.kprintf prerr_endline fmt
740 else
741 Printf.kprintf ignore fmt
744 let launchpath () =
745 if String.length conf.pathlauncher = 0
746 then print_endline state.path
747 else (
748 let re = Str.regexp "%s" in
749 let command = Str.global_replace re state.path conf.pathlauncher in
750 try popen command []
751 with exn ->
752 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
753 flush stderr;
757 module Ne = struct
758 type 'a t = | Res of 'a | Exn of exn;;
760 let pipe () =
761 try Res (Unix.pipe ())
762 with exn -> Exn exn
765 let clo fd f =
766 try tempfailureretry Unix.close fd
767 with exn -> f (exntos exn)
770 let dup fd =
771 try Res (tempfailureretry Unix.dup fd)
772 with exn -> Exn exn
775 let dup2 fd1 fd2 =
776 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
777 with exn -> Exn exn
779 end;;
781 let redirectstderr () =
782 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
783 if conf.redirectstderr
784 then
785 match Ne.pipe () with
786 | Ne.Exn exn ->
787 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
789 | Ne.Res (r, w) ->
790 begin match Ne.dup Unix.stderr with
791 | Ne.Exn exn ->
792 dolog "failed to dup stderr: %s" (exntos exn);
793 Ne.clo r (clofail "pipe/r");
794 Ne.clo w (clofail "pipe/w");
796 | Ne.Res dupstderr ->
797 begin match Ne.dup2 w Unix.stderr with
798 | Ne.Exn exn ->
799 dolog "failed to dup2 to stderr: %s" (exntos 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" (exntos exn)
816 | Ne.Res () ->
817 Ne.clo fd (clofail "dup of stderr");
818 state.errfd <- None;
819 end;
820 | None -> ()
821 end;
822 prerr_string (Buffer.contents state.errmsgs);
823 flush stderr;
824 Buffer.clear state.errmsgs;
828 module G =
829 struct
830 let postRedisplay who =
831 if conf.verbose
832 then prerr_endline ("redisplay for " ^ who);
833 state.redisplay <- true;
835 end;;
837 let getopaque pageno =
838 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
839 with Not_found -> None
842 let putopaque pageno opaque =
843 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
846 let pagetranslatepoint l x y =
847 let dy = y - l.pagedispy in
848 let y = dy + l.pagey in
849 let dx = x - l.pagedispx in
850 let x = dx + l.pagex in
851 (x, y);
854 let onppundermouse g x y d =
855 let rec f = function
856 | l :: rest ->
857 begin match getopaque l.pageno with
858 | Some opaque ->
859 let x0 = l.pagedispx in
860 let x1 = x0 + l.pagevw in
861 let y0 = l.pagedispy in
862 let y1 = y0 + l.pagevh in
863 if y >= y0 && y <= y1 && x >= x0 && x <= x1
864 then
865 let px, py = pagetranslatepoint l x y in
866 match g opaque l px py with
867 | Some res -> res
868 | None -> f rest
869 else f rest
870 | _ ->
871 f rest
873 | [] -> d
875 f state.layout
878 let getunder x y =
879 let g opaque _ px py =
880 match whatsunder opaque px py with
881 | Unone -> None
882 | under -> Some under
884 onppundermouse g x y Unone
887 let unproject x y =
888 let g opaque l x y =
889 match unproject opaque x y with
890 | Some (x, y) -> Some (Some (l.pageno, x, y))
891 | None -> None
893 onppundermouse g x y None;
896 let showtext c s =
897 state.text <- Printf.sprintf "%c%s" c s;
898 G.postRedisplay "showtext";
901 let undertext = function
902 | Unone -> "none"
903 | Ulinkuri s -> s
904 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
905 | Utext s -> "font: " ^ s
906 | Uunexpected s -> "unexpected: " ^ s
907 | Ulaunch s -> "launch: " ^ s
908 | Unamed s -> "named: " ^ s
909 | Uremote (filename, pageno) ->
910 Printf.sprintf "%s: page %d" filename (pageno+1)
913 let updateunder x y =
914 match getunder x y with
915 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
916 | Ulinkuri uri ->
917 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
918 Wsi.setcursor Wsi.CURSOR_INFO
919 | Ulinkgoto (pageno, _) ->
920 if conf.underinfo
921 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
922 Wsi.setcursor Wsi.CURSOR_INFO
923 | Utext s ->
924 if conf.underinfo then showtext 'f' ("ont: " ^ s);
925 Wsi.setcursor Wsi.CURSOR_TEXT
926 | Uunexpected s ->
927 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
928 Wsi.setcursor Wsi.CURSOR_INHERIT
929 | Ulaunch s ->
930 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
931 Wsi.setcursor Wsi.CURSOR_INHERIT
932 | Unamed s ->
933 if conf.underinfo then showtext 'n' ("amed: " ^ s);
934 Wsi.setcursor Wsi.CURSOR_INHERIT
935 | Uremote (filename, pageno) ->
936 if conf.underinfo then showtext 'r'
937 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
938 Wsi.setcursor Wsi.CURSOR_INFO
941 let showlinktype under =
942 if conf.underinfo
943 then
944 match under with
945 | Unone -> ()
946 | under ->
947 let s = undertext under in
948 showtext ' ' s
951 let addchar s c =
952 let b = Buffer.create (String.length s + 1) in
953 Buffer.add_string b s;
954 Buffer.add_char b c;
955 Buffer.contents b;
958 let colorspace_of_string s =
959 match String.lowercase s with
960 | "rgb" -> Rgb
961 | "bgr" -> Bgr
962 | "gray" -> Gray
963 | _ -> failwith "invalid colorspace"
966 let int_of_colorspace = function
967 | Rgb -> 0
968 | Bgr -> 1
969 | Gray -> 2
972 let colorspace_of_int = function
973 | 0 -> Rgb
974 | 1 -> Bgr
975 | 2 -> Gray
976 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
979 let colorspace_to_string = function
980 | Rgb -> "rgb"
981 | Bgr -> "bgr"
982 | Gray -> "gray"
985 let intentry_with_suffix text key =
986 let c =
987 if key >= 32 && key < 127
988 then Char.chr key
989 else '\000'
991 match Char.lowercase c with
992 | '0' .. '9' ->
993 let text = addchar text c in
994 TEcont text
996 | 'k' | 'm' | 'g' ->
997 let text = addchar text c in
998 TEcont text
1000 | _ ->
1001 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1002 TEcont text
1005 let multicolumns_to_string (n, a, b) =
1006 if a = 0 && b = 0
1007 then Printf.sprintf "%d" n
1008 else Printf.sprintf "%d,%d,%d" n a b;
1011 let multicolumns_of_string s =
1013 (int_of_string s, 0, 0)
1014 with _ ->
1015 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1016 if a > 1 || b > 1
1017 then failwith "subtly broken"; (n, a, b)
1021 let readcmd fd =
1022 let s = "xxxx" in
1023 let n = tempfailureretry (Unix.read fd s 0) 4 in
1024 if n != 4 then failwith "incomplete read(len)";
1025 let len = 0
1026 lor (Char.code s.[0] lsl 24)
1027 lor (Char.code s.[1] lsl 16)
1028 lor (Char.code s.[2] lsl 8)
1029 lor (Char.code s.[3] lsl 0)
1031 let s = String.create len in
1032 let n = tempfailureretry (Unix.read fd s 0) len in
1033 if n != len then failwith "incomplete read(data)";
1037 let btod b = if b then 1 else 0;;
1039 let wcmd fmt =
1040 let b = Buffer.create 16 in
1041 Buffer.add_string b "llll";
1042 Printf.kbprintf
1043 (fun b ->
1044 let s = Buffer.contents b in
1045 let n = String.length s in
1046 let len = n - 4 in
1047 (* dolog "wcmd %S" (String.sub s 4 len); *)
1048 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1049 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1050 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1051 s.[3] <- Char.chr (len land 0xff);
1052 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1053 if n' != n then failwith "write failed";
1054 ) b fmt;
1057 let calcips h =
1058 let d = state.winh - h in
1059 max conf.interpagespace ((d + 1) / 2)
1062 let rowyh (c, coverA, coverB) b n =
1063 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1064 then
1065 let _, _, vy, (_, _, h, _) = b.(n) in
1066 (vy, h)
1067 else
1068 let n' = n - coverA in
1069 let d = n' mod c in
1070 let s = n - d in
1071 let e = min state.pagecount (s + c) in
1072 let rec find m miny maxh = if m = e then miny, maxh else
1073 let _, _, y, (_, _, h, _) = b.(m) in
1074 let miny = min miny y in
1075 let maxh = max maxh h in
1076 find (m+1) miny maxh
1077 in find s max_int 0
1080 let calcheight () =
1081 match conf.columns with
1082 | Cmulti ((_, _, _) as cl, b) ->
1083 if Array.length b > 0
1084 then
1085 let y, h = rowyh cl b (Array.length b - 1) in
1086 y + h + (if conf.presentation then calcips h else 0)
1087 else 0
1088 | Csingle b ->
1089 if Array.length b > 0
1090 then
1091 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1092 y + h + (if conf.presentation then calcips h else 0)
1093 else 0
1094 | Csplit (_, b) ->
1095 if Array.length b > 0
1096 then
1097 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1098 y + h
1099 else 0
1102 let getpageyh pageno =
1103 let pageno = bound pageno 0 (state.pagecount-1) in
1104 match conf.columns with
1105 | Csingle b ->
1106 if Array.length b = 0
1107 then 0, 0
1108 else
1109 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1110 let y =
1111 if conf.presentation
1112 then y - calcips h
1113 else y
1115 y, h
1116 | Cmulti (cl, b) ->
1117 if Array.length b = 0
1118 then 0, 0
1119 else
1120 let y, h = rowyh cl b pageno in
1121 let y =
1122 if conf.presentation
1123 then y - calcips h
1124 else y
1126 y, h
1127 | Csplit (c, b) ->
1128 if Array.length b = 0
1129 then 0, 0
1130 else
1131 let n = pageno*c in
1132 let (_, _, y, (_, _, h, _)) = b.(n) in
1133 y, h
1136 let getpagedim pageno =
1137 let rec f ppdim l =
1138 match l with
1139 | (n, _, _, _) as pdim :: rest ->
1140 if n >= pageno
1141 then (if n = pageno then pdim else ppdim)
1142 else f pdim rest
1144 | [] -> ppdim
1146 f (-1, -1, -1, -1) state.pdims
1149 let getpagey pageno = fst (getpageyh pageno);;
1151 let nogeomcmds cmds =
1152 match cmds with
1153 | s, [] -> String.length s = 0
1154 | _ -> false
1157 let page_of_y y =
1158 let ((c, coverA, coverB) as cl), b =
1159 match conf.columns with
1160 | Csingle b -> (1, 0, 0), b
1161 | Cmulti (c, b) -> c, b
1162 | Csplit (_, b) -> (1, 0, 0), b
1164 if Array.length b = 0
1165 then -1
1166 else
1167 let rec bsearch nmin nmax =
1168 if nmin > nmax
1169 then bound nmin 0 (state.pagecount-1)
1170 else
1171 let n = (nmax + nmin) / 2 in
1172 let vy, h = rowyh cl b n in
1173 let y0, y1 =
1174 if conf.presentation
1175 then
1176 let ips = calcips h in
1177 let y0 = vy - ips in
1178 let y1 = vy + h + ips in
1179 y0, y1
1180 else (
1181 if n = 0
1182 then 0, vy + h + conf.interpagespace
1183 else
1184 let y0 = vy - conf.interpagespace in
1185 y0, y0 + h + conf.interpagespace
1188 if y >= y0 && y < y1
1189 then (
1190 if c = 1
1191 then n
1192 else (
1193 if n > coverA
1194 then
1195 if n < state.pagecount - coverB
1196 then ((n-coverA)/c)*c + coverA
1197 else n
1198 else n
1201 else (
1202 if y > y0
1203 then bsearch (n+1) nmax
1204 else bsearch nmin (n-1)
1207 let r = bsearch 0 (state.pagecount-1) in
1211 let layoutN ((columns, coverA, coverB), b) y sh =
1212 let sh = sh - state.hscrollh in
1213 let rec fold accu n =
1214 if n = Array.length b
1215 then accu
1216 else
1217 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1218 if (vy - y) > sh &&
1219 (n = coverA - 1
1220 || n = state.pagecount - coverB
1221 || (n - coverA) mod columns = columns - 1)
1222 then accu
1223 else
1224 let accu =
1225 if vy + h > y
1226 then
1227 let pagey = max 0 (y - vy) in
1228 let pagedispy = if pagey > 0 then 0 else vy - y in
1229 let pagedispx, pagex =
1230 let pdx =
1231 if n = coverA - 1 || n = state.pagecount - coverB
1232 then state.x + (state.winw - state.scrollw - w) / 2
1233 else dx + xoff + state.x
1235 if pdx < 0
1236 then 0, -pdx
1237 else pdx, 0
1239 let pagevw =
1240 let vw = state.winw - state.scrollw - pagedispx in
1241 let pw = w - pagex in
1242 min vw pw
1244 let pagevh = min (h - pagey) (sh - pagedispy) in
1245 if pagevw > 0 && pagevh > 0
1246 then
1247 let e =
1248 { pageno = n
1249 ; pagedimno = pdimno
1250 ; pagew = w
1251 ; pageh = h
1252 ; pagex = pagex
1253 ; pagey = pagey
1254 ; pagevw = pagevw
1255 ; pagevh = pagevh
1256 ; pagedispx = pagedispx
1257 ; pagedispy = pagedispy
1258 ; pagecol = 0
1261 e :: accu
1262 else
1263 accu
1264 else
1265 accu
1267 fold accu (n+1)
1269 List.rev (fold [] (page_of_y y));
1272 let layoutS (columns, b) y sh =
1273 let sh = sh - state.hscrollh in
1274 let rec fold accu n =
1275 if n = Array.length b
1276 then accu
1277 else
1278 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1279 if (vy - y) > sh
1280 then accu
1281 else
1282 let accu =
1283 if vy + pageh > y
1284 then
1285 let x = xoff + state.x in
1286 let pagey = max 0 (y - vy) in
1287 let pagedispy = if pagey > 0 then 0 else vy - y in
1288 let pagedispx, pagex =
1289 if px = 0
1290 then (
1291 if x < 0
1292 then 0, -x
1293 else x, 0
1295 else (
1296 let px = px - x in
1297 if px < 0
1298 then -px, 0
1299 else 0, px
1302 let pagecolw = pagew/columns in
1303 let pagedispx =
1304 if pagecolw < state.winw
1305 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1306 else pagedispx
1308 let pagevw =
1309 let vw = state.winw - pagedispx - state.scrollw in
1310 let pw = pagew - pagex in
1311 min vw pw
1313 let pagevw = min pagevw pagecolw in
1314 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1315 if pagevw > 0 && pagevh > 0
1316 then
1317 let e =
1318 { pageno = n/columns
1319 ; pagedimno = pdimno
1320 ; pagew = pagew
1321 ; pageh = pageh
1322 ; pagex = pagex
1323 ; pagey = pagey
1324 ; pagevw = pagevw
1325 ; pagevh = pagevh
1326 ; pagedispx = pagedispx
1327 ; pagedispy = pagedispy
1328 ; pagecol = n mod columns
1331 e :: accu
1332 else
1333 accu
1334 else
1335 accu
1337 fold accu (n+1)
1339 List.rev (fold [] 0)
1342 let layout y sh =
1343 if nogeomcmds state.geomcmds
1344 then
1345 match conf.columns with
1346 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1347 | Cmulti c -> layoutN c y sh
1348 | Csplit s -> layoutS s y sh
1349 else []
1352 let clamp incr =
1353 let y = state.y + incr in
1354 let y = max 0 y in
1355 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1359 let itertiles l f =
1360 let tilex = l.pagex mod conf.tilew in
1361 let tiley = l.pagey mod conf.tileh in
1363 let col = l.pagex / conf.tilew in
1364 let row = l.pagey / conf.tileh in
1366 let rec rowloop row y0 dispy h =
1367 if h = 0
1368 then ()
1369 else (
1370 let dh = conf.tileh - y0 in
1371 let dh = min h dh in
1372 let rec colloop col x0 dispx w =
1373 if w = 0
1374 then ()
1375 else (
1376 let dw = conf.tilew - x0 in
1377 let dw = min w dw in
1379 f col row dispx dispy x0 y0 dw dh;
1380 colloop (col+1) 0 (dispx+dw) (w-dw)
1383 colloop col tilex l.pagedispx l.pagevw;
1384 rowloop (row+1) 0 (dispy+dh) (h-dh)
1387 if l.pagevw > 0 && l.pagevh > 0
1388 then rowloop row tiley l.pagedispy l.pagevh;
1391 let gettileopaque l col row =
1392 let key =
1393 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1395 try Some (Hashtbl.find state.tilemap key)
1396 with Not_found -> None
1399 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1400 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1401 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1404 let drawtiles l color =
1405 GlDraw.color color;
1406 let f col row x y tilex tiley w h =
1407 match gettileopaque l col row with
1408 | Some (opaque, _, t) ->
1409 let params = x, y, w, h, tilex, tiley in
1410 if conf.invert
1411 then (
1412 Gl.enable `blend;
1413 GlFunc.blend_func `zero `one_minus_src_color;
1415 drawtile params opaque;
1416 if conf.invert
1417 then Gl.disable `blend;
1418 if conf.debug
1419 then (
1420 let s = Printf.sprintf
1421 "%d[%d,%d] %f sec"
1422 l.pageno col row t
1424 let w = measurestr fstate.fontsize s in
1425 GlMisc.push_attrib [`current];
1426 GlDraw.color (0.0, 0.0, 0.0);
1427 GlDraw.rect
1428 (float (x-2), float (y-2))
1429 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1430 GlDraw.color (1.0, 1.0, 1.0);
1431 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1432 GlMisc.pop_attrib ();
1435 | _ ->
1436 let w =
1437 let lw = state.winw - state.scrollw - x in
1438 min lw w
1439 and h =
1440 let lh = state.winh - y in
1441 min lh h
1443 begin match state.texid with
1444 | Some id ->
1445 Gl.enable `texture_2d;
1446 GlTex.bind_texture `texture_2d id;
1447 let x0 = float x
1448 and y0 = float y
1449 and x1 = float (x+w)
1450 and y1 = float (y+h) in
1452 let tw = float w /. 16.0
1453 and th = float h /. 16.0 in
1454 let tx0 = float tilex /. 16.0
1455 and ty0 = float tiley /. 16.0 in
1456 let tx1 = tx0 +. tw
1457 and ty1 = ty0 +. th in
1458 GlDraw.begins `quads;
1459 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1460 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1461 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1462 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1463 GlDraw.ends ();
1465 Gl.disable `texture_2d;
1466 | None ->
1467 GlDraw.color (1.0, 1.0, 1.0);
1468 GlDraw.rect
1469 (float x, float y)
1470 (float (x+w), float (y+h));
1471 end;
1472 if w > 128 && h > fstate.fontsize + 10
1473 then (
1474 GlDraw.color (0.0, 0.0, 0.0);
1475 let c, r =
1476 if conf.verbose
1477 then (col*conf.tilew, row*conf.tileh)
1478 else col, row
1480 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1482 GlDraw.color color;
1484 itertiles l f
1487 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1489 let tilevisible1 l x y =
1490 let ax0 = l.pagex
1491 and ax1 = l.pagex + l.pagevw
1492 and ay0 = l.pagey
1493 and ay1 = l.pagey + l.pagevh in
1495 let bx0 = x
1496 and by0 = y in
1497 let bx1 = min (bx0 + conf.tilew) l.pagew
1498 and by1 = min (by0 + conf.tileh) l.pageh in
1500 let rx0 = max ax0 bx0
1501 and ry0 = max ay0 by0
1502 and rx1 = min ax1 bx1
1503 and ry1 = min ay1 by1 in
1505 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1506 nonemptyintersection
1509 let tilevisible layout n x y =
1510 let rec findpageinlayout m = function
1511 | l :: rest when l.pageno = n ->
1512 tilevisible1 l x y || (
1513 match conf.columns with
1514 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1515 | _ -> false
1517 | _ :: rest -> findpageinlayout 0 rest
1518 | [] -> false
1520 findpageinlayout 0 layout;
1523 let tileready l x y =
1524 tilevisible1 l x y &&
1525 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1528 let tilepage n p layout =
1529 let rec loop = function
1530 | l :: rest ->
1531 if l.pageno = n
1532 then
1533 let f col row _ _ _ _ _ _ =
1534 if state.currently = Idle
1535 then
1536 match gettileopaque l col row with
1537 | Some _ -> ()
1538 | None ->
1539 let x = col*conf.tilew
1540 and y = row*conf.tileh in
1541 let w =
1542 let w = l.pagew - x in
1543 min w conf.tilew
1545 let h =
1546 let h = l.pageh - y in
1547 min h conf.tileh
1549 let pbo =
1550 if conf.usepbo
1551 then getpbo w h conf.colorspace
1552 else "0"
1554 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1555 state.currently <-
1556 Tiling (
1557 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1558 conf.tilew, conf.tileh
1561 itertiles l f;
1562 else
1563 loop rest
1565 | [] -> ()
1567 if nogeomcmds state.geomcmds
1568 then loop layout;
1571 let preloadlayout y =
1572 let y = if y < state.winh then 0 else y - state.winh in
1573 let h = state.winh*3 in
1574 layout y h;
1577 let load pages =
1578 let rec loop pages =
1579 if state.currently != Idle
1580 then ()
1581 else
1582 match pages with
1583 | l :: rest ->
1584 begin match getopaque l.pageno with
1585 | None ->
1586 wcmd "page %d %d" l.pageno l.pagedimno;
1587 state.currently <- Loading (l, state.gen);
1588 | Some opaque ->
1589 tilepage l.pageno opaque pages;
1590 loop rest
1591 end;
1592 | _ -> ()
1594 if nogeomcmds state.geomcmds
1595 then loop pages
1598 let preload pages =
1599 load pages;
1600 if conf.preload && state.currently = Idle
1601 then load (preloadlayout state.y);
1604 let layoutready layout =
1605 let rec fold all ls =
1606 all && match ls with
1607 | l :: rest ->
1608 let seen = ref false in
1609 let allvisible = ref true in
1610 let foo col row _ _ _ _ _ _ =
1611 seen := true;
1612 allvisible := !allvisible &&
1613 begin match gettileopaque l col row with
1614 | Some _ -> true
1615 | None -> false
1618 itertiles l foo;
1619 fold (!seen && !allvisible) rest
1620 | [] -> true
1622 let alltilesvisible = fold true layout in
1623 alltilesvisible;
1626 let gotoy y =
1627 state.wthack <- false;
1628 let y = bound y 0 state.maxy in
1629 let y, layout, proceed =
1630 match conf.maxwait with
1631 | Some time when state.ghyll == noghyll ->
1632 begin match state.throttle with
1633 | None ->
1634 let layout = layout y state.winh in
1635 let ready = layoutready layout in
1636 if not ready
1637 then (
1638 load layout;
1639 state.throttle <- Some (layout, y, now ());
1641 else G.postRedisplay "gotoy showall (None)";
1642 y, layout, ready
1643 | Some (_, _, started) ->
1644 let dt = now () -. started in
1645 if dt > time
1646 then (
1647 state.throttle <- None;
1648 let layout = layout y state.winh in
1649 load layout;
1650 G.postRedisplay "maxwait";
1651 y, layout, true
1653 else -1, [], false
1656 | _ ->
1657 let layout = layout y state.winh in
1658 G.postRedisplay "gotoy ready";
1659 y, layout, true
1661 if proceed
1662 then (
1663 state.y <- y;
1664 state.layout <- layout;
1665 begin match state.mode with
1666 | LinkNav (Ltexact (pageno, linkno)) ->
1667 let rec loop = function
1668 | [] ->
1669 state.mode <- LinkNav (Ltgendir 0)
1670 | l :: _ when l.pageno = pageno ->
1671 begin match getopaque pageno with
1672 | None ->
1673 state.mode <- LinkNav (Ltgendir 0)
1674 | Some opaque ->
1675 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1676 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1677 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1678 then state.mode <- LinkNav (Ltgendir 0)
1680 | _ :: rest -> loop rest
1682 loop layout
1683 | _ -> ()
1684 end;
1685 begin match state.mode with
1686 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1687 if not (pagevisible layout pageno)
1688 then (
1689 match state.layout with
1690 | [] -> ()
1691 | l :: _ ->
1692 state.mode <- Birdseye (
1693 conf, leftx, l.pageno, hooverpageno, anchor
1696 | LinkNav (Ltgendir dir as lt) ->
1697 let linknav =
1698 let rec loop = function
1699 | [] -> lt
1700 | l :: rest ->
1701 match getopaque l.pageno with
1702 | None -> loop rest
1703 | Some opaque ->
1704 let link =
1705 let ld =
1706 if dir = 0
1707 then LDfirstvisible (l.pagex, l.pagey, dir)
1708 else (
1709 if dir > 0 then LDfirst else LDlast
1712 findlink opaque ld
1714 match link with
1715 | Lnotfound -> loop rest
1716 | Lfound n ->
1717 showlinktype (getlink opaque n);
1718 Ltexact (l.pageno, n)
1720 loop state.layout
1722 state.mode <- LinkNav linknav
1723 | _ -> ()
1724 end;
1725 preload layout;
1727 state.ghyll <- noghyll;
1728 if conf.updatecurs
1729 then (
1730 let mx, my = state.mpos in
1731 updateunder mx my;
1735 let conttiling pageno opaque =
1736 tilepage pageno opaque
1737 (if conf.preload then preloadlayout state.y else state.layout)
1740 let gotoy_and_clear_text y =
1741 if not conf.verbose then state.text <- "";
1742 gotoy y;
1745 let getanchor1 l =
1746 let top =
1747 let coloff = l.pagecol * l.pageh in
1748 float (l.pagey + coloff) /. float l.pageh
1750 let dtop =
1751 if l.pagedispy = 0
1752 then
1754 else
1755 if conf.presentation
1756 then float l.pagedispy /. float (calcips l.pageh)
1757 else float l.pagedispy /. float conf.interpagespace
1759 (l.pageno, top, dtop)
1762 let getanchor () =
1763 match state.layout with
1764 | l :: _ -> getanchor1 l
1765 | [] ->
1766 let n = page_of_y state.y in
1767 if n = -1
1768 then state.anchor
1769 else
1770 let y, h = getpageyh n in
1771 let dy = y - state.y in
1772 let dtop =
1773 if conf.presentation
1774 then
1775 let ips = calcips h in
1776 float (dy + ips) /. float ips
1777 else
1778 float dy /. float conf.interpagespace
1780 (n, 0.0, dtop)
1783 let getanchory (n, top, dtop) =
1784 let y, h = getpageyh n in
1785 if conf.presentation
1786 then
1787 let ips = calcips h in
1788 y + truncate (top*.float h -. dtop*.float ips) + ips;
1789 else
1790 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1793 let gotoanchor anchor =
1794 gotoy (getanchory anchor);
1797 let addnav () =
1798 cbput state.hists.nav (getanchor ());
1801 let getnav dir =
1802 let anchor = cbgetc state.hists.nav dir in
1803 getanchory anchor;
1806 let gotoghyll y =
1807 let scroll f n a b =
1808 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1809 let snake f a b =
1810 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1811 if f < a
1812 then s (float f /. float a)
1813 else (
1814 if f > b
1815 then 1.0 -. s ((float (f-b) /. float (n-b)))
1816 else 1.0
1819 snake f a b
1820 and summa f n a b =
1821 (* courtesy:
1822 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1823 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1824 let iv1 = iv f in
1825 let ins = float a *. iv1
1826 and outs = float (n-b) *. iv1 in
1827 let ones = b - a in
1828 ins +. outs +. float ones
1830 let rec set (_N, _A, _B) y sy =
1831 let sum = summa 1.0 _N _A _B in
1832 let dy = float (y - sy) in
1833 state.ghyll <- (
1834 let rec gf n y1 o =
1835 if n >= _N
1836 then state.ghyll <- noghyll
1837 else
1838 let go n =
1839 let s = scroll n _N _A _B in
1840 let y1 = y1 +. ((s *. dy) /. sum) in
1841 gotoy_and_clear_text (truncate y1);
1842 state.ghyll <- gf (n+1) y1;
1844 match o with
1845 | None -> go n
1846 | Some y' -> set (_N/2, 1, 1) y' state.y
1848 gf 0 (float state.y)
1851 match conf.ghyllscroll with
1852 | None ->
1853 gotoy_and_clear_text y
1854 | Some nab ->
1855 if state.ghyll == noghyll
1856 then set nab y state.y
1857 else state.ghyll (Some y)
1860 let gotopage n top =
1861 let y, h = getpageyh n in
1862 let y = y + (truncate (top *. float h)) in
1863 gotoghyll y
1866 let gotopage1 n top =
1867 let y = getpagey n in
1868 let y = y + top in
1869 gotoghyll y
1872 let invalidate s f =
1873 state.layout <- [];
1874 state.pdims <- [];
1875 state.rects <- [];
1876 state.rects1 <- [];
1877 match state.geomcmds with
1878 | ps, [] when String.length ps = 0 ->
1879 f ();
1880 state.geomcmds <- s, [];
1882 | ps, [] ->
1883 state.geomcmds <- ps, [s, f];
1885 | ps, (s', _) :: rest when s' = s ->
1886 state.geomcmds <- ps, ((s, f) :: rest);
1888 | ps, cmds ->
1889 state.geomcmds <- ps, ((s, f) :: cmds);
1892 let flushpages () =
1893 Hashtbl.iter (fun _ opaque ->
1894 wcmd "freepage %s" opaque;
1895 ) state.pagemap;
1896 Hashtbl.clear state.pagemap;
1899 let flushtiles () =
1900 Queue.iter (fun (k, p, s) ->
1901 wcmd "freetile %s" p;
1902 state.memused <- state.memused - s;
1903 state.uioh#infochanged Memused;
1904 Hashtbl.remove state.tilemap k;
1905 ) state.tilelru;
1906 Queue.clear state.tilelru;
1907 load state.layout;
1910 let opendoc path password =
1911 state.path <- path;
1912 state.password <- password;
1913 state.gen <- state.gen + 1;
1914 state.docinfo <- [];
1916 flushpages ();
1917 flushtiles ();
1918 setaalevel conf.aalevel;
1919 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1920 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1921 invalidate "reqlayout"
1922 (fun () ->
1923 wcmd "reqlayout %d %d %s\000"
1924 conf.angle (btod conf.proportional) state.nameddest;
1928 let reload () =
1929 state.anchor <- getanchor ();
1930 state.wthack <- !wtmode;
1931 opendoc state.path state.password;
1934 let scalecolor c =
1935 let c = c *. conf.colorscale in
1936 (c, c, c);
1939 let scalecolor2 (r, g, b) =
1940 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1943 let docolumns = function
1944 | Csingle _ ->
1945 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1946 let rec loop pageno pdimno pdim y ph pdims =
1947 if pageno = state.pagecount
1948 then ()
1949 else
1950 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1951 match pdims with
1952 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1953 pdimno+1, pdim, rest
1954 | _ ->
1955 pdimno, pdim, pdims
1957 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
1958 let y = y +
1959 (if conf.presentation
1960 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1961 else (if pageno = 0 then 0 else conf.interpagespace)
1964 a.(pageno) <- (pdimno, x, y, pdim);
1965 loop (pageno+1) pdimno pdim (y + h) h pdims
1967 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1968 conf.columns <- Csingle a;
1970 | Cmulti ((columns, coverA, coverB), _) ->
1971 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1972 let rec loop pageno pdimno pdim x y rowh pdims =
1973 let rec fixrow m = if m = pageno then () else
1974 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1975 if h < rowh
1976 then (
1977 let y = y + (rowh - h) / 2 in
1978 a.(m) <- (pdimno, x, y, pdim);
1980 fixrow (m+1)
1982 if pageno = state.pagecount
1983 then fixrow (((pageno - 1) / columns) * columns)
1984 else
1985 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1986 match pdims with
1987 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1988 pdimno+1, pdim, rest
1989 | _ ->
1990 pdimno, pdim, pdims
1992 let x, y, rowh' =
1993 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1994 then (
1995 let x = (state.winw - state.scrollw - w) / 2 in
1996 let ips =
1997 if conf.presentation then calcips h else conf.interpagespace in
1998 x, y + ips + rowh, h
2000 else (
2001 if (pageno - coverA) mod columns = 0
2002 then (
2003 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2004 let y =
2005 if conf.presentation
2006 then
2007 let ips = calcips h in
2008 y + (if pageno = 0 then 0 else calcips rowh + ips)
2009 else
2010 y + (if pageno = 0 then 0 else conf.interpagespace)
2012 x, y + rowh, h
2014 else x, y, max rowh h
2017 let y =
2018 if pageno > 1 && (pageno - coverA) mod columns = 0
2019 then (
2020 let y =
2021 if pageno = columns && conf.presentation
2022 then (
2023 let ips = calcips rowh in
2024 for i = 0 to pred columns
2026 let (pdimno, x, y, pdim) = a.(i) in
2027 a.(i) <- (pdimno, x, y+ips, pdim)
2028 done;
2029 y+ips;
2031 else y
2033 fixrow (pageno - columns);
2036 else y
2038 a.(pageno) <- (pdimno, x, y, pdim);
2039 let x = x + w + xoff*2 + conf.interpagespace in
2040 loop (pageno+1) pdimno pdim x y rowh' pdims
2042 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2043 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2045 | Csplit (c, _) ->
2046 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2047 let rec loop pageno pdimno pdim y pdims =
2048 if pageno = state.pagecount
2049 then ()
2050 else
2051 let pdimno, ((_, w, h, _) as pdim), pdims =
2052 match pdims with
2053 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2054 pdimno+1, pdim, rest
2055 | _ ->
2056 pdimno, pdim, pdims
2058 let cw = w / c in
2059 let rec loop1 n x y =
2060 if n = c then y else (
2061 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2062 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2065 let y = loop1 0 0 y in
2066 loop (pageno+1) pdimno pdim y pdims
2068 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2069 conf.columns <- Csplit (c, a);
2072 let represent () =
2073 docolumns conf.columns;
2074 state.maxy <- calcheight ();
2075 state.hscrollh <-
2076 if state.x = 0 && state.w <= state.winw - state.scrollw
2077 then 0
2078 else state.scrollw
2080 if state.reprf == noreprf
2081 then (
2082 match state.mode with
2083 | Birdseye (_, _, pageno, _, _) ->
2084 let y, h = getpageyh pageno in
2085 let top = (state.winh - h) / 2 in
2086 gotoy (max 0 (y - top))
2087 | _ -> gotoanchor state.anchor
2089 else (
2090 state.reprf ();
2091 state.reprf <- noreprf;
2095 let reshape w h =
2096 state.wthack <- false;
2097 GlDraw.viewport 0 0 w h;
2098 let firsttime = state.geomcmds == firstgeomcmds in
2099 if not firsttime && nogeomcmds state.geomcmds
2100 then state.anchor <- getanchor ();
2102 state.winw <- w;
2103 let w = truncate (float w *. conf.zoom) - state.scrollw in
2104 let w = max w 2 in
2105 state.winh <- h;
2106 setfontsize fstate.fontsize;
2107 GlMat.mode `modelview;
2108 GlMat.load_identity ();
2110 GlMat.mode `projection;
2111 GlMat.load_identity ();
2112 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2113 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2114 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2116 let relx =
2117 if conf.zoom <= 1.0
2118 then 0.0
2119 else float state.x /. float state.w
2121 invalidate "geometry"
2122 (fun () ->
2123 state.w <- w;
2124 if not firsttime
2125 then state.x <- truncate (relx *. float w);
2126 let w =
2127 match conf.columns with
2128 | Csingle _ -> w
2129 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2130 | Csplit (c, _) -> w * c
2132 wcmd "geometry %d %d" w h);
2135 let enttext () =
2136 let len = String.length state.text in
2137 let drawstring s =
2138 let hscrollh =
2139 match state.mode with
2140 | Textentry _
2141 | View ->
2142 let h, _, _ = state.uioh#scrollpw in
2144 | _ -> 0
2146 let rect x w =
2147 GlDraw.rect
2148 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2149 (x+.w, float (state.winh - hscrollh))
2152 let w = float (state.winw - state.scrollw - 1) in
2153 if state.progress >= 0.0 && state.progress < 1.0
2154 then (
2155 GlDraw.color (0.3, 0.3, 0.3);
2156 let w1 = w *. state.progress in
2157 rect 0.0 w1;
2158 GlDraw.color (0.0, 0.0, 0.0);
2159 rect w1 (w-.w1)
2161 else (
2162 GlDraw.color (0.0, 0.0, 0.0);
2163 rect 0.0 w;
2166 GlDraw.color (1.0, 1.0, 1.0);
2167 drawstring fstate.fontsize
2168 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2170 let s =
2171 match state.mode with
2172 | Textentry ((prefix, text, _, _, _, _), _) ->
2173 let s =
2174 if len > 0
2175 then
2176 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2177 else
2178 Printf.sprintf "%s%s_" prefix text
2182 | _ -> state.text
2184 let s =
2185 if state.newerrmsgs
2186 then (
2187 if not (istextentry state.mode)
2188 then
2189 let s1 = "(press 'e' to review error messasges)" in
2190 if String.length s > 0 then s ^ " " ^ s1 else s1
2191 else s
2193 else s
2195 if String.length s > 0
2196 then drawstring s
2199 let gctiles () =
2200 let len = Queue.length state.tilelru in
2201 let layout = lazy (
2202 match state.throttle with
2203 | None ->
2204 if conf.preload
2205 then preloadlayout state.y
2206 else state.layout
2207 | Some (layout, _, _) ->
2208 layout
2209 ) in
2210 let rec loop qpos =
2211 if state.memused <= conf.memlimit
2212 then ()
2213 else (
2214 if qpos < len
2215 then
2216 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2217 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2218 let (_, pw, ph, _) = getpagedim n in
2220 gen = state.gen
2221 && colorspace = conf.colorspace
2222 && angle = conf.angle
2223 && pagew = pw
2224 && pageh = ph
2225 && (
2226 let x = col*conf.tilew
2227 and y = row*conf.tileh in
2228 tilevisible (Lazy.force_val layout) n x y
2230 then Queue.push lruitem state.tilelru
2231 else (
2232 freepbo p;
2233 wcmd "freetile %s" p;
2234 state.memused <- state.memused - s;
2235 state.uioh#infochanged Memused;
2236 Hashtbl.remove state.tilemap k;
2238 loop (qpos+1)
2241 loop 0
2244 let logcurrently = function
2245 | Idle -> dolog "Idle"
2246 | Loading (l, gen) ->
2247 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2248 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2249 dolog
2250 "Tiling %d[%d,%d] page=%s cs=%s angle"
2251 l.pageno col row pageopaque
2252 (colorspace_to_string colorspace)
2254 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2255 angle gen conf.angle state.gen
2256 tilew tileh
2257 conf.tilew conf.tileh
2259 | Outlining _ ->
2260 dolog "outlining"
2263 let splitatspace =
2264 let r = Str.regexp " " in
2265 fun s -> Str.bounded_split r s 2;
2268 let onpagerect pageno f =
2269 let b =
2270 match conf.columns with
2271 | Cmulti (_, b) -> b
2272 | Csingle b -> b
2273 | Csplit (_, b) -> b
2275 if pageno >= 0 && pageno < Array.length b
2276 then
2277 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2278 let r = getpdimrect pdimno in
2279 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2282 let gotopagexy pageno x y =
2283 onpagerect pageno (fun w h ->
2284 let top = y /. h in
2285 let _,w1,_,leftx = getpagedim pageno in
2286 let wh = state.winh - state.hscrollh in
2287 let sw = float w1 /. w in
2288 let x = sw *. x in
2289 let x = leftx + state.x + truncate x in
2290 let sx =
2291 if x < 0 || x >= state.winw - state.scrollw
2292 then state.x - x
2293 else state.x
2295 let py, h = getpageyh pageno in
2296 let y' = py + truncate (top *. float h) in
2297 let dy = y' - state.y in
2298 let sy =
2299 if x != state.x || not (dy > 0 && dy < wh)
2300 then (
2301 if conf.presentation
2302 then
2303 if abs (py - y') > wh
2304 then y'
2305 else py
2306 else y';
2308 else state.y
2310 if state.x != sx || state.y != sy
2311 then (
2312 let x, y =
2313 if !wtmode
2314 then (
2315 let ww = state.winw - state.scrollw in
2316 let qx = sx / ww
2317 and qy = sy / wh in
2318 let x = qx * ww
2319 and y = qy * wh in
2320 let x = if -x + ww > w1 then -(w1-ww) else x
2321 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2322 let y =
2323 if conf.presentation
2324 then
2325 if abs (py - y') > wh
2326 then y'
2327 else py
2328 else y';
2330 (x, y)
2332 else (sx, sy)
2334 state.x <- x;
2335 state.hscrollh <-
2336 if x = 0 && state.w <= state.winw - state.scrollw
2337 then 0
2338 else state.scrollw
2340 gotoy_and_clear_text y;
2342 else gotoy_and_clear_text state.y;
2343 state.wthack <- !wtmode && not (layoutready state.layout);
2347 let act cmds =
2348 (* dolog "%S" cmds; *)
2349 let cl = splitatspace cmds in
2350 let scan s fmt f =
2351 try Scanf.sscanf s fmt f
2352 with exn ->
2353 dolog "error processing '%S': %s" cmds (exntos exn);
2354 exit 1
2356 match cl with
2357 | "clear" :: [] ->
2358 state.uioh#infochanged Pdim;
2359 state.pdims <- [];
2361 | "clearrects" :: [] ->
2362 state.rects <- state.rects1;
2363 G.postRedisplay "clearrects";
2365 | "continue" :: args :: [] ->
2366 let n = scan args "%u" (fun n -> n) in
2367 state.pagecount <- n;
2368 begin match state.currently with
2369 | Outlining l ->
2370 state.currently <- Idle;
2371 state.outlines <- Array.of_list (List.rev l)
2372 | _ -> ()
2373 end;
2375 let cur, cmds = state.geomcmds in
2376 if String.length cur = 0
2377 then failwith "umpossible";
2379 begin match List.rev cmds with
2380 | [] ->
2381 state.geomcmds <- "", [];
2382 represent ();
2383 | (s, f) :: rest ->
2384 f ();
2385 state.geomcmds <- s, List.rev rest;
2386 end;
2387 if conf.maxwait = None
2388 then G.postRedisplay "continue";
2390 | "title" :: args :: [] ->
2391 Wsi.settitle args
2393 | "msg" :: args :: [] ->
2394 showtext ' ' args
2396 | "vmsg" :: args :: [] ->
2397 if conf.verbose
2398 then showtext ' ' args
2400 | "emsg" :: args :: [] ->
2401 Buffer.add_string state.errmsgs args;
2402 state.newerrmsgs <- true;
2403 G.postRedisplay "error message"
2405 | "progress" :: args :: [] ->
2406 let progress, text =
2407 scan args "%f %n"
2408 (fun f pos ->
2409 f, String.sub args pos (String.length args - pos))
2411 state.text <- text;
2412 state.progress <- progress;
2413 G.postRedisplay "progress"
2415 | "firstmatch" :: args :: [] ->
2416 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2417 scan args "%u %d %f %f %f %f %f %f %f %f"
2418 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2419 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2421 let y = (getpagey pageno) + truncate y0 in
2422 addnav ();
2423 gotoy y;
2424 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2426 | "match" :: args :: [] ->
2427 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2428 scan args "%u %d %f %f %f %f %f %f %f %f"
2429 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2430 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2432 state.rects1 <-
2433 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2435 | "page" :: args :: [] ->
2436 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2437 begin match state.currently with
2438 | Loading (l, gen) ->
2439 vlog "page %d took %f sec" l.pageno t;
2440 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2441 begin match state.throttle with
2442 | None ->
2443 let preloadedpages =
2444 if conf.preload
2445 then preloadlayout state.y
2446 else state.layout
2448 let evict () =
2449 let module IntSet =
2450 Set.Make (struct type t = int let compare = (-) end) in
2451 let set =
2452 List.fold_left (fun s l -> IntSet.add l.pageno s)
2453 IntSet.empty preloadedpages
2455 let evictedpages =
2456 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2457 if not (IntSet.mem pageno set)
2458 then (
2459 wcmd "freepage %s" opaque;
2460 key :: accu
2462 else accu
2463 ) state.pagemap []
2465 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2467 evict ();
2468 state.currently <- Idle;
2469 if gen = state.gen
2470 then (
2471 tilepage l.pageno pageopaque state.layout;
2472 load state.layout;
2473 load preloadedpages;
2474 if pagevisible state.layout l.pageno
2475 && layoutready state.layout
2476 then G.postRedisplay "page";
2479 | Some (layout, _, _) ->
2480 state.currently <- Idle;
2481 tilepage l.pageno pageopaque layout;
2482 load state.layout
2483 end;
2485 | _ ->
2486 dolog "Inconsistent loading state";
2487 logcurrently state.currently;
2488 exit 1
2491 | "tile" :: args :: [] ->
2492 let (x, y, opaque, size, t) =
2493 scan args "%u %u %s %u %f"
2494 (fun x y p size t -> (x, y, p, size, t))
2496 begin match state.currently with
2497 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2498 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2500 unmappbo opaque;
2501 if tilew != conf.tilew || tileh != conf.tileh
2502 then (
2503 wcmd "freetile %s" opaque;
2504 state.currently <- Idle;
2505 load state.layout;
2507 else (
2508 puttileopaque l col row gen cs angle opaque size t;
2509 state.memused <- state.memused + size;
2510 state.uioh#infochanged Memused;
2511 gctiles ();
2512 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2513 opaque, size) state.tilelru;
2515 let layout =
2516 match state.throttle with
2517 | None -> state.layout
2518 | Some (layout, _, _) -> layout
2521 state.currently <- Idle;
2522 if gen = state.gen
2523 && conf.colorspace = cs
2524 && conf.angle = angle
2525 && tilevisible layout l.pageno x y
2526 then conttiling l.pageno pageopaque;
2528 begin match state.throttle with
2529 | None ->
2530 if state.wthack
2531 then state.wthack <- not (layoutready state.layout);
2532 preload state.layout;
2533 if gen = state.gen
2534 && conf.colorspace = cs
2535 && conf.angle = angle
2536 && tilevisible state.layout l.pageno x y
2537 then G.postRedisplay "tile nothrottle";
2539 | Some (layout, y, _) ->
2540 let ready = layoutready layout in
2541 if ready
2542 then (
2543 state.wthack <- false;
2544 state.y <- y;
2545 state.layout <- layout;
2546 state.throttle <- None;
2547 G.postRedisplay "throttle";
2549 else load layout;
2550 end;
2553 | _ ->
2554 dolog "Inconsistent tiling state";
2555 logcurrently state.currently;
2556 exit 1
2559 | "pdim" :: args :: [] ->
2560 let pdim =
2561 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2563 state.uioh#infochanged Pdim;
2564 state.pdims <- pdim :: state.pdims
2566 | "o" :: args :: [] ->
2567 let (l, n, t, h, pos) =
2568 scan args "%u %u %d %u %n"
2569 (fun l n t h pos -> l, n, t, h, pos)
2571 let s = String.sub args pos (String.length args - pos) in
2572 let outline = (s, l, (n, float t /. float h, 0.0)) in
2573 begin match state.currently with
2574 | Outlining outlines ->
2575 state.currently <- Outlining (outline :: outlines)
2576 | Idle ->
2577 state.currently <- Outlining [outline]
2578 | currently ->
2579 dolog "invalid outlining state";
2580 logcurrently currently
2583 | "a" :: args :: [] ->
2584 let (n, l, t) =
2585 scan args "%u %d %d" (fun n l t -> n, l, t)
2587 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2589 | "info" :: args :: [] ->
2590 state.docinfo <- (1, args) :: state.docinfo
2592 | "infoend" :: [] ->
2593 state.uioh#infochanged Docinfo;
2594 state.docinfo <- List.rev state.docinfo
2596 | _ ->
2597 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2600 let onhist cb =
2601 let rc = cb.rc in
2602 let action = function
2603 | HCprev -> cbget cb ~-1
2604 | HCnext -> cbget cb 1
2605 | HCfirst -> cbget cb ~-(cb.rc)
2606 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2607 and cancel () = cb.rc <- rc
2608 in (action, cancel)
2611 let search pattern forward =
2612 if String.length pattern > 0
2613 then
2614 let pn, py =
2615 match state.layout with
2616 | [] -> 0, 0
2617 | l :: _ ->
2618 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2620 wcmd "search %d %d %d %d,%s\000"
2621 (btod conf.icase) pn py (btod forward) pattern;
2624 let intentry text key =
2625 let c =
2626 if key >= 32 && key < 127
2627 then Char.chr key
2628 else '\000'
2630 match c with
2631 | '0' .. '9' ->
2632 let text = addchar text c in
2633 TEcont text
2635 | _ ->
2636 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2637 TEcont text
2640 let linknentry text key =
2641 let c =
2642 if key >= 32 && key < 127
2643 then Char.chr key
2644 else '\000'
2646 match c with
2647 | 'a' .. 'z' ->
2648 let text = addchar text c in
2649 TEcont text
2651 | _ ->
2652 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2653 TEcont text
2656 let linkndone f s =
2657 if String.length s > 0
2658 then (
2659 let n =
2660 let l = String.length s in
2661 let rec loop pos n = if pos = l then n else
2662 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2663 loop (pos+1) (n*26 + m)
2664 in loop 0 0
2666 let rec loop n = function
2667 | [] -> ()
2668 | l :: rest ->
2669 match getopaque l.pageno with
2670 | None -> loop n rest
2671 | Some opaque ->
2672 let m = getlinkcount opaque in
2673 if n < m
2674 then (
2675 let under = getlink opaque n in
2676 f under
2678 else loop (n-m) rest
2680 loop n state.layout;
2684 let textentry text key =
2685 if key land 0xff00 = 0xff00
2686 then TEcont text
2687 else TEcont (text ^ toutf8 key)
2690 let reqlayout angle proportional =
2691 match state.throttle with
2692 | None ->
2693 if nogeomcmds state.geomcmds
2694 then state.anchor <- getanchor ();
2695 conf.angle <- angle mod 360;
2696 if conf.angle != 0
2697 then (
2698 match state.mode with
2699 | LinkNav _ -> state.mode <- View
2700 | _ -> ()
2702 conf.proportional <- proportional;
2703 invalidate "reqlayout"
2704 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2705 | _ -> ()
2708 let settrim trimmargins trimfuzz =
2709 if nogeomcmds state.geomcmds
2710 then state.anchor <- getanchor ();
2711 conf.trimmargins <- trimmargins;
2712 conf.trimfuzz <- trimfuzz;
2713 let x0, y0, x1, y1 = trimfuzz in
2714 invalidate "settrim"
2715 (fun () ->
2716 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2717 flushpages ();
2720 let setzoom zoom =
2721 match state.throttle with
2722 | None ->
2723 let zoom = max 0.01 zoom in
2724 if zoom <> conf.zoom
2725 then (
2726 state.prevzoom <- conf.zoom;
2727 conf.zoom <- zoom;
2728 reshape state.winw state.winh;
2729 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2732 | Some (layout, y, started) ->
2733 let time =
2734 match conf.maxwait with
2735 | None -> 0.0
2736 | Some t -> t
2738 let dt = now () -. started in
2739 if dt > time
2740 then (
2741 state.y <- y;
2742 load layout;
2746 let setcolumns mode columns coverA coverB =
2747 state.prevcolumns <- Some (conf.columns, conf.zoom);
2748 if columns < 0
2749 then (
2750 if isbirdseye mode
2751 then showtext '!' "split mode doesn't work in bird's eye"
2752 else (
2753 conf.columns <- Csplit (-columns, [||]);
2754 state.x <- 0;
2755 conf.zoom <- 1.0;
2758 else (
2759 if columns < 2
2760 then (
2761 conf.columns <- Csingle [||];
2762 state.x <- 0;
2763 setzoom 1.0;
2765 else (
2766 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2767 conf.zoom <- 1.0;
2770 reshape state.winw state.winh;
2773 let enterbirdseye () =
2774 let zoom = float conf.thumbw /. float state.winw in
2775 let birdseyepageno =
2776 let cy = state.winh / 2 in
2777 let fold = function
2778 | [] -> 0
2779 | l :: rest ->
2780 let rec fold best = function
2781 | [] -> best.pageno
2782 | l :: rest ->
2783 let d = cy - (l.pagedispy + l.pagevh/2)
2784 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2785 if abs d < abs dbest
2786 then fold l rest
2787 else best.pageno
2788 in fold l rest
2790 fold state.layout
2792 state.mode <- Birdseye (
2793 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2795 conf.zoom <- zoom;
2796 conf.presentation <- false;
2797 conf.interpagespace <- 10;
2798 conf.hlinks <- false;
2799 state.x <- 0;
2800 state.mstate <- Mnone;
2801 conf.maxwait <- None;
2802 conf.columns <- (
2803 match conf.beyecolumns with
2804 | Some c ->
2805 conf.zoom <- 1.0;
2806 Cmulti ((c, 0, 0), [||])
2807 | None -> Csingle [||]
2809 Wsi.setcursor Wsi.CURSOR_INHERIT;
2810 if conf.verbose
2811 then
2812 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2813 (100.0*.zoom)
2814 else
2815 state.text <- ""
2817 reshape state.winw state.winh;
2820 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2821 state.mode <- View;
2822 conf.zoom <- c.zoom;
2823 conf.presentation <- c.presentation;
2824 conf.interpagespace <- c.interpagespace;
2825 conf.maxwait <- c.maxwait;
2826 conf.hlinks <- c.hlinks;
2827 conf.beyecolumns <- (
2828 match conf.columns with
2829 | Cmulti ((c, _, _), _) -> Some c
2830 | Csingle _ -> None
2831 | Csplit _ -> failwith "leaving bird's eye split mode"
2833 conf.columns <- (
2834 match c.columns with
2835 | Cmulti (c, _) -> Cmulti (c, [||])
2836 | Csingle _ -> Csingle [||]
2837 | Csplit (c, _) -> Csplit (c, [||])
2839 state.x <- leftx;
2840 if conf.verbose
2841 then
2842 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2843 (100.0*.conf.zoom)
2845 reshape state.winw state.winh;
2846 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2849 let togglebirdseye () =
2850 match state.mode with
2851 | Birdseye vals -> leavebirdseye vals true
2852 | View -> enterbirdseye ()
2853 | _ -> ()
2856 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2857 let pageno = max 0 (pageno - incr) in
2858 let rec loop = function
2859 | [] -> gotopage1 pageno 0
2860 | l :: _ when l.pageno = pageno ->
2861 if l.pagedispy >= 0 && l.pagey = 0
2862 then G.postRedisplay "upbirdseye"
2863 else gotopage1 pageno 0
2864 | _ :: rest -> loop rest
2866 loop state.layout;
2867 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2870 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2871 let pageno = min (state.pagecount - 1) (pageno + incr) in
2872 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2873 let rec loop = function
2874 | [] ->
2875 let y, h = getpageyh pageno in
2876 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2877 gotoy (clamp dy)
2878 | l :: _ when l.pageno = pageno ->
2879 if l.pagevh != l.pageh
2880 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2881 else G.postRedisplay "downbirdseye"
2882 | _ :: rest -> loop rest
2884 loop state.layout
2887 let optentry mode _ key =
2888 let btos b = if b then "on" else "off" in
2889 if key >= 32 && key < 127
2890 then
2891 let c = Char.chr key in
2892 match c with
2893 | 's' ->
2894 let ondone s =
2895 try conf.scrollstep <- int_of_string s with exc ->
2896 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2898 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2900 | 'A' ->
2901 let ondone s =
2903 conf.autoscrollstep <- int_of_string s;
2904 if state.autoscroll <> None
2905 then state.autoscroll <- Some conf.autoscrollstep
2906 with exc ->
2907 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2909 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2911 | 'C' ->
2912 let ondone s =
2914 let n, a, b = multicolumns_of_string s in
2915 setcolumns mode n a b;
2916 with exc ->
2917 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2919 TEswitch ("columns: ", "", None, textentry, ondone, true)
2921 | 'Z' ->
2922 let ondone s =
2924 let zoom = float (int_of_string s) /. 100.0 in
2925 setzoom zoom
2926 with exc ->
2927 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2929 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2931 | 't' ->
2932 let ondone s =
2934 conf.thumbw <- bound (int_of_string s) 2 4096;
2935 state.text <-
2936 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2937 begin match mode with
2938 | Birdseye beye ->
2939 leavebirdseye beye false;
2940 enterbirdseye ();
2941 | _ -> ();
2943 with exc ->
2944 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2946 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2948 | 'R' ->
2949 let ondone s =
2950 match try
2951 Some (int_of_string s)
2952 with exc ->
2953 state.text <- Printf.sprintf "bad integer `%s': %s"
2954 s (exntos exc);
2955 None
2956 with
2957 | Some angle -> reqlayout angle conf.proportional
2958 | None -> ()
2960 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2962 | 'i' ->
2963 conf.icase <- not conf.icase;
2964 TEdone ("case insensitive search " ^ (btos conf.icase))
2966 | 'p' ->
2967 conf.preload <- not conf.preload;
2968 gotoy state.y;
2969 TEdone ("preload " ^ (btos conf.preload))
2971 | 'v' ->
2972 conf.verbose <- not conf.verbose;
2973 TEdone ("verbose " ^ (btos conf.verbose))
2975 | 'd' ->
2976 conf.debug <- not conf.debug;
2977 TEdone ("debug " ^ (btos conf.debug))
2979 | 'h' ->
2980 conf.maxhfit <- not conf.maxhfit;
2981 state.maxy <- calcheight ();
2982 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2984 | 'c' ->
2985 conf.crophack <- not conf.crophack;
2986 TEdone ("crophack " ^ btos conf.crophack)
2988 | 'a' ->
2989 let s =
2990 match conf.maxwait with
2991 | None ->
2992 conf.maxwait <- Some infinity;
2993 "always wait for page to complete"
2994 | Some _ ->
2995 conf.maxwait <- None;
2996 "show placeholder if page is not ready"
2998 TEdone s
3000 | 'f' ->
3001 conf.underinfo <- not conf.underinfo;
3002 TEdone ("underinfo " ^ btos conf.underinfo)
3004 | 'P' ->
3005 conf.savebmarks <- not conf.savebmarks;
3006 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3008 | 'S' ->
3009 let ondone s =
3011 let pageno, py =
3012 match state.layout with
3013 | [] -> 0, 0
3014 | l :: _ ->
3015 l.pageno, l.pagey
3017 conf.interpagespace <- int_of_string s;
3018 docolumns conf.columns;
3019 state.maxy <- calcheight ();
3020 let y = getpagey pageno in
3021 gotoy (y + py)
3022 with exc ->
3023 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3025 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3027 | 'l' ->
3028 reqlayout conf.angle (not conf.proportional);
3029 TEdone ("proportional display " ^ btos conf.proportional)
3031 | 'T' ->
3032 settrim (not conf.trimmargins) conf.trimfuzz;
3033 TEdone ("trim margins " ^ btos conf.trimmargins)
3035 | 'I' ->
3036 conf.invert <- not conf.invert;
3037 TEdone ("invert colors " ^ btos conf.invert)
3039 | 'x' ->
3040 let ondone s =
3041 cbput state.hists.sel s;
3042 conf.selcmd <- s;
3044 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3045 textentry, ondone, true)
3047 | _ ->
3048 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3049 TEstop
3050 else
3051 TEcont state.text
3054 class type lvsource = object
3055 method getitemcount : int
3056 method getitem : int -> (string * int)
3057 method hasaction : int -> bool
3058 method exit :
3059 uioh:uioh ->
3060 cancel:bool ->
3061 active:int ->
3062 first:int ->
3063 pan:int ->
3064 qsearch:string ->
3065 uioh option
3066 method getactive : int
3067 method getfirst : int
3068 method getqsearch : string
3069 method setqsearch : string -> unit
3070 method getpan : int
3071 end;;
3073 class virtual lvsourcebase = object
3074 val mutable m_active = 0
3075 val mutable m_first = 0
3076 val mutable m_qsearch = ""
3077 val mutable m_pan = 0
3078 method getactive = m_active
3079 method getfirst = m_first
3080 method getqsearch = m_qsearch
3081 method getpan = m_pan
3082 method setqsearch s = m_qsearch <- s
3083 end;;
3085 let withoutlastutf8 s =
3086 let len = String.length s in
3087 if len = 0
3088 then s
3089 else
3090 let rec find pos =
3091 if pos = 0
3092 then pos
3093 else
3094 let b = Char.code s.[pos] in
3095 if b land 0b11000000 = 0b11000000
3096 then pos
3097 else find (pos-1)
3099 let first =
3100 if Char.code s.[len-1] land 0x80 = 0
3101 then len-1
3102 else find (len-1)
3104 String.sub s 0 first;
3107 let textentrykeyboard
3108 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3109 let key =
3110 if key >= 0xffb0 && key <= 0xffb9
3111 then key - 0xffb0 + 48 else key
3113 let enttext te =
3114 state.mode <- Textentry (te, onleave);
3115 state.text <- "";
3116 enttext ();
3117 G.postRedisplay "textentrykeyboard enttext";
3119 let histaction cmd =
3120 match opthist with
3121 | None -> ()
3122 | Some (action, _) ->
3123 state.mode <- Textentry (
3124 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3126 G.postRedisplay "textentry histaction"
3128 match key with
3129 | 0xff08 -> (* backspace *)
3130 let s = withoutlastutf8 text in
3131 let len = String.length s in
3132 if cancelonempty && len = 0
3133 then (
3134 onleave Cancel;
3135 G.postRedisplay "textentrykeyboard after cancel";
3137 else (
3138 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3141 | 0xff0d | 0xff8d -> (* (kp) enter *)
3142 ondone text;
3143 onleave Confirm;
3144 G.postRedisplay "textentrykeyboard after confirm"
3146 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3147 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3148 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3149 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3151 | 0xff1b -> (* escape*)
3152 if String.length text = 0
3153 then (
3154 begin match opthist with
3155 | None -> ()
3156 | Some (_, onhistcancel) -> onhistcancel ()
3157 end;
3158 onleave Cancel;
3159 state.text <- "";
3160 G.postRedisplay "textentrykeyboard after cancel2"
3162 else (
3163 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3166 | 0xff9f | 0xffff -> () (* delete *)
3168 | _ when key != 0
3169 && key land 0xff00 != 0xff00 (* keyboard *)
3170 && key land 0xfe00 != 0xfe00 (* xkb *)
3171 && key land 0xfd00 != 0xfd00 (* 3270 *)
3173 begin match onkey text key with
3174 | TEdone text ->
3175 ondone text;
3176 onleave Confirm;
3177 G.postRedisplay "textentrykeyboard after confirm2";
3179 | TEcont text ->
3180 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3182 | TEstop ->
3183 onleave Cancel;
3184 G.postRedisplay "textentrykeyboard after cancel3"
3186 | TEswitch te ->
3187 state.mode <- Textentry (te, onleave);
3188 G.postRedisplay "textentrykeyboard switch";
3189 end;
3191 | _ ->
3192 vlog "unhandled key %s" (Wsi.keyname key)
3195 let firstof first active =
3196 if first > active || abs (first - active) > fstate.maxrows - 1
3197 then max 0 (active - (fstate.maxrows/2))
3198 else first
3201 let calcfirst first active =
3202 if active > first
3203 then
3204 let rows = active - first in
3205 if rows > fstate.maxrows then active - fstate.maxrows else first
3206 else active
3209 let scrollph y maxy =
3210 let sh = (float (maxy + state.winh) /. float state.winh) in
3211 let sh = float state.winh /. sh in
3212 let sh = max sh (float conf.scrollh) in
3214 let percent =
3215 if y = state.maxy
3216 then 1.0
3217 else float y /. float maxy
3219 let position = (float state.winh -. sh) *. percent in
3221 let position =
3222 if position +. sh > float state.winh
3223 then float state.winh -. sh
3224 else position
3226 position, sh;
3229 let coe s = (s :> uioh);;
3231 class listview ~(source:lvsource) ~trusted ~modehash =
3232 object (self)
3233 val m_pan = source#getpan
3234 val m_first = source#getfirst
3235 val m_active = source#getactive
3236 val m_qsearch = source#getqsearch
3237 val m_prev_uioh = state.uioh
3239 method private elemunder y =
3240 let n = y / (fstate.fontsize+1) in
3241 if m_first + n < source#getitemcount
3242 then (
3243 if source#hasaction (m_first + n)
3244 then Some (m_first + n)
3245 else None
3247 else None
3249 method display =
3250 Gl.enable `blend;
3251 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3252 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3253 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3254 GlDraw.color (1., 1., 1.);
3255 Gl.enable `texture_2d;
3256 let fs = fstate.fontsize in
3257 let nfs = fs + 1 in
3258 let ww = fstate.wwidth in
3259 let tabw = 30.0*.ww in
3260 let itemcount = source#getitemcount in
3261 let rec loop row =
3262 if (row - m_first) > fstate.maxrows
3263 then ()
3264 else (
3265 if row >= 0 && row < itemcount
3266 then (
3267 let (s, level) = source#getitem row in
3268 let y = (row - m_first) * nfs in
3269 let x = 5.0 +. float (level + m_pan) *. ww in
3270 if row = m_active
3271 then (
3272 Gl.disable `texture_2d;
3273 GlDraw.polygon_mode `both `line;
3274 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3275 GlDraw.rect (1., float (y + 1))
3276 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3277 GlDraw.polygon_mode `both `fill;
3278 GlDraw.color (1., 1., 1.);
3279 Gl.enable `texture_2d;
3282 let drawtabularstring s =
3283 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3284 if trusted
3285 then
3286 let tabpos = try String.index s '\t' with Not_found -> -1 in
3287 if tabpos > 0
3288 then
3289 let len = String.length s - tabpos - 1 in
3290 let s1 = String.sub s 0 tabpos
3291 and s2 = String.sub s (tabpos + 1) len in
3292 let nx = drawstr x s1 in
3293 let sw = nx -. x in
3294 let x = x +. (max tabw sw) in
3295 drawstr x s2
3296 else
3297 drawstr x s
3298 else
3299 drawstr x s
3301 let _ = drawtabularstring s in
3302 loop (row+1)
3306 loop m_first;
3307 Gl.disable `blend;
3308 Gl.disable `texture_2d;
3310 method updownlevel incr =
3311 let len = source#getitemcount in
3312 let curlevel =
3313 if m_active >= 0 && m_active < len
3314 then snd (source#getitem m_active)
3315 else -1
3317 let rec flow i =
3318 if i = len then i-1 else if i = -1 then 0 else
3319 let _, l = source#getitem i in
3320 if l != curlevel then i else flow (i+incr)
3322 let active = flow m_active in
3323 let first = calcfirst m_first active in
3324 G.postRedisplay "outline updownlevel";
3325 {< m_active = active; m_first = first >}
3327 method private key1 key mask =
3328 let set1 active first qsearch =
3329 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3331 let search active pattern incr =
3332 let dosearch re =
3333 let rec loop n =
3334 if n >= 0 && n < source#getitemcount
3335 then (
3336 let s, _ = source#getitem n in
3338 (try ignore (Str.search_forward re s 0); true
3339 with Not_found -> false)
3340 then Some n
3341 else loop (n + incr)
3343 else None
3345 loop active
3348 let re = Str.regexp_case_fold pattern in
3349 dosearch re
3350 with Failure s ->
3351 state.text <- s;
3352 None
3354 let itemcount = source#getitemcount in
3355 let find start incr =
3356 let rec find i =
3357 if i = -1 || i = itemcount
3358 then -1
3359 else (
3360 if source#hasaction i
3361 then i
3362 else find (i + incr)
3365 find start
3367 let set active first =
3368 let first = bound first 0 (itemcount - fstate.maxrows) in
3369 state.text <- "";
3370 coe {< m_active = active; m_first = first >}
3372 let navigate incr =
3373 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3374 let active, first =
3375 let incr1 = if incr > 0 then 1 else -1 in
3376 if isvisible m_first m_active
3377 then
3378 let next =
3379 let next = m_active + incr in
3380 let next =
3381 if next < 0 || next >= itemcount
3382 then -1
3383 else find next incr1
3385 if next = -1 || abs (m_active - next) > fstate.maxrows
3386 then -1
3387 else next
3389 if next = -1
3390 then
3391 let first = m_first + incr in
3392 let first = bound first 0 (itemcount - 1) in
3393 let next =
3394 let next = m_active + incr in
3395 let next = bound next 0 (itemcount - 1) in
3396 find next ~-incr1
3398 let active = if next = -1 then m_active else next in
3399 active, first
3400 else
3401 let first = min next m_first in
3402 let first =
3403 if abs (next - first) > fstate.maxrows
3404 then first + incr
3405 else first
3407 next, first
3408 else
3409 let first = m_first + incr in
3410 let first = bound first 0 (itemcount - 1) in
3411 let active =
3412 let next = m_active + incr in
3413 let next = bound next 0 (itemcount - 1) in
3414 let next = find next incr1 in
3415 let active =
3416 if next = -1 || abs (m_active - first) > fstate.maxrows
3417 then (
3418 let active = if m_active = -1 then next else m_active in
3419 active
3421 else next
3423 if isvisible first active
3424 then active
3425 else -1
3427 active, first
3429 G.postRedisplay "listview navigate";
3430 set active first;
3432 match key with
3433 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3434 let incr = if key = 0x72 then -1 else 1 in
3435 let active, first =
3436 match search (m_active + incr) m_qsearch incr with
3437 | None ->
3438 state.text <- m_qsearch ^ " [not found]";
3439 m_active, m_first
3440 | Some active ->
3441 state.text <- m_qsearch;
3442 active, firstof m_first active
3444 G.postRedisplay "listview ctrl-r/s";
3445 set1 active first m_qsearch;
3447 | 0xff08 -> (* backspace *)
3448 if String.length m_qsearch = 0
3449 then coe self
3450 else (
3451 let qsearch = withoutlastutf8 m_qsearch in
3452 let len = String.length qsearch in
3453 if len = 0
3454 then (
3455 state.text <- "";
3456 G.postRedisplay "listview empty qsearch";
3457 set1 m_active m_first "";
3459 else
3460 let active, first =
3461 match search m_active qsearch ~-1 with
3462 | None ->
3463 state.text <- qsearch ^ " [not found]";
3464 m_active, m_first
3465 | Some active ->
3466 state.text <- qsearch;
3467 active, firstof m_first active
3469 G.postRedisplay "listview backspace qsearch";
3470 set1 active first qsearch
3473 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3474 let pattern = m_qsearch ^ toutf8 key in
3475 let active, first =
3476 match search m_active pattern 1 with
3477 | None ->
3478 state.text <- pattern ^ " [not found]";
3479 m_active, m_first
3480 | Some active ->
3481 state.text <- pattern;
3482 active, firstof m_first active
3484 G.postRedisplay "listview qsearch add";
3485 set1 active first pattern;
3487 | 0xff1b -> (* escape *)
3488 state.text <- "";
3489 if String.length m_qsearch = 0
3490 then (
3491 G.postRedisplay "list view escape";
3492 begin
3493 match
3494 source#exit (coe self) true m_active m_first m_pan m_qsearch
3495 with
3496 | None -> m_prev_uioh
3497 | Some uioh -> uioh
3500 else (
3501 G.postRedisplay "list view kill qsearch";
3502 source#setqsearch "";
3503 coe {< m_qsearch = "" >}
3506 | 0xff0d | 0xff8d -> (* (kp) enter *)
3507 state.text <- "";
3508 let self = {< m_qsearch = "" >} in
3509 source#setqsearch "";
3510 let opt =
3511 G.postRedisplay "listview enter";
3512 if m_active >= 0 && m_active < source#getitemcount
3513 then (
3514 source#exit (coe self) false m_active m_first m_pan "";
3516 else (
3517 source#exit (coe self) true m_active m_first m_pan "";
3520 begin match opt with
3521 | None -> m_prev_uioh
3522 | Some uioh -> uioh
3525 | 0xff9f | 0xffff -> (* (kp) delete *)
3526 coe self
3528 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3529 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3530 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3531 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3533 | 0xff53 | 0xff98 -> (* (kp) right *)
3534 state.text <- "";
3535 G.postRedisplay "listview right";
3536 coe {< m_pan = m_pan - 1 >}
3538 | 0xff51 | 0xff96 -> (* (kp) left *)
3539 state.text <- "";
3540 G.postRedisplay "listview left";
3541 coe {< m_pan = m_pan + 1 >}
3543 | 0xff50 | 0xff95 -> (* (kp) home *)
3544 let active = find 0 1 in
3545 G.postRedisplay "listview home";
3546 set active 0;
3548 | 0xff57 | 0xff9c -> (* (kp) end *)
3549 let first = max 0 (itemcount - fstate.maxrows) in
3550 let active = find (itemcount - 1) ~-1 in
3551 G.postRedisplay "listview end";
3552 set active first;
3554 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3555 coe self
3557 | _ ->
3558 dolog "listview unknown key %#x" key; coe self
3560 method key key mask =
3561 match state.mode with
3562 | Textentry te -> textentrykeyboard key mask te; coe self
3563 | _ -> self#key1 key mask
3565 method button button down x y _ =
3566 let opt =
3567 match button with
3568 | 1 when x > state.winw - conf.scrollbw ->
3569 G.postRedisplay "listview scroll";
3570 if down
3571 then
3572 let _, position, sh = self#scrollph in
3573 if y > truncate position && y < truncate (position +. sh)
3574 then (
3575 state.mstate <- Mscrolly;
3576 Some (coe self)
3578 else
3579 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3580 let first = truncate (s *. float source#getitemcount) in
3581 let first = min source#getitemcount first in
3582 Some (coe {< m_first = first; m_active = first >})
3583 else (
3584 state.mstate <- Mnone;
3585 Some (coe self);
3587 | 1 when not down ->
3588 begin match self#elemunder y with
3589 | Some n ->
3590 G.postRedisplay "listview click";
3591 source#exit
3592 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3593 | _ ->
3594 Some (coe self)
3596 | n when (n == 4 || n == 5) && not down ->
3597 let len = source#getitemcount in
3598 let first =
3599 if n = 5 && m_first + fstate.maxrows >= len
3600 then
3601 m_first
3602 else
3603 let first = m_first + (if n == 4 then -1 else 1) in
3604 bound first 0 (len - 1)
3606 G.postRedisplay "listview wheel";
3607 Some (coe {< m_first = first >})
3608 | n when (n = 6 || n = 7) && not down ->
3609 let inc = m_first + (if n = 7 then -1 else 1) in
3610 G.postRedisplay "listview hwheel";
3611 Some (coe {< m_pan = m_pan + inc >})
3612 | _ ->
3613 Some (coe self)
3615 match opt with
3616 | None -> m_prev_uioh
3617 | Some uioh -> uioh
3619 method motion _ y =
3620 match state.mstate with
3621 | Mscrolly ->
3622 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3623 let first = truncate (s *. float source#getitemcount) in
3624 let first = min source#getitemcount first in
3625 G.postRedisplay "listview motion";
3626 coe {< m_first = first; m_active = first >}
3627 | _ -> coe self
3629 method pmotion x y =
3630 if x < state.winw - conf.scrollbw
3631 then
3632 let n =
3633 match self#elemunder y with
3634 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3635 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3637 let o =
3638 if n != m_active
3639 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3640 else self
3642 coe o
3643 else (
3644 Wsi.setcursor Wsi.CURSOR_INHERIT;
3645 coe self
3648 method infochanged _ = ()
3650 method scrollpw = (0, 0.0, 0.0)
3651 method scrollph =
3652 let nfs = fstate.fontsize + 1 in
3653 let y = m_first * nfs in
3654 let itemcount = source#getitemcount in
3655 let maxi = max 0 (itemcount - fstate.maxrows) in
3656 let maxy = maxi * nfs in
3657 let p, h = scrollph y maxy in
3658 conf.scrollbw, p, h
3660 method modehash = modehash
3661 end;;
3663 class outlinelistview ~source =
3664 object (self)
3665 inherit listview
3666 ~source:(source :> lvsource)
3667 ~trusted:false
3668 ~modehash:(findkeyhash conf "outline")
3669 as super
3671 method key key mask =
3672 let calcfirst first active =
3673 if active > first
3674 then
3675 let rows = active - first in
3676 let maxrows =
3677 if String.length state.text = 0
3678 then fstate.maxrows
3679 else fstate.maxrows - 2
3681 if rows > maxrows then active - maxrows else first
3682 else active
3684 let navigate incr =
3685 let active = m_active + incr in
3686 let active = bound active 0 (source#getitemcount - 1) in
3687 let first = calcfirst m_first active in
3688 G.postRedisplay "outline navigate";
3689 coe {< m_active = active; m_first = first >}
3691 let ctrl = Wsi.withctrl mask in
3692 match key with
3693 | 110 when ctrl -> (* ctrl-n *)
3694 source#narrow m_qsearch;
3695 G.postRedisplay "outline ctrl-n";
3696 coe {< m_first = 0; m_active = 0 >}
3698 | 117 when ctrl -> (* ctrl-u *)
3699 source#denarrow;
3700 G.postRedisplay "outline ctrl-u";
3701 state.text <- "";
3702 coe {< m_first = 0; m_active = 0 >}
3704 | 108 when ctrl -> (* ctrl-l *)
3705 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3706 G.postRedisplay "outline ctrl-l";
3707 coe {< m_first = first >}
3709 | 0xff9f | 0xffff -> (* (kp) delete *)
3710 source#remove m_active;
3711 G.postRedisplay "outline delete";
3712 let active = max 0 (m_active-1) in
3713 coe {< m_first = firstof m_first active;
3714 m_active = active >}
3716 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3717 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3718 | 0xff55 | 0xff9a -> (* (kp) prior *)
3719 navigate ~-(fstate.maxrows)
3720 | 0xff56 | 0xff9b -> (* (kp) next *)
3721 navigate fstate.maxrows
3723 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3724 let o =
3725 if ctrl
3726 then (
3727 G.postRedisplay "outline ctrl right";
3728 {< m_pan = m_pan + 1 >}
3730 else self#updownlevel 1
3732 coe o
3734 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3735 let o =
3736 if ctrl
3737 then (
3738 G.postRedisplay "outline ctrl left";
3739 {< m_pan = m_pan - 1 >}
3741 else self#updownlevel ~-1
3743 coe o
3745 | 0xff50 | 0xff95 -> (* (kp) home *)
3746 G.postRedisplay "outline home";
3747 coe {< m_first = 0; m_active = 0 >}
3749 | 0xff57 | 0xff9c -> (* (kp) end *)
3750 let active = source#getitemcount - 1 in
3751 let first = max 0 (active - fstate.maxrows) in
3752 G.postRedisplay "outline end";
3753 coe {< m_active = active; m_first = first >}
3755 | _ -> super#key key mask
3758 let outlinesource usebookmarks =
3759 let empty = [||] in
3760 (object
3761 inherit lvsourcebase
3762 val mutable m_items = empty
3763 val mutable m_orig_items = empty
3764 val mutable m_prev_items = empty
3765 val mutable m_narrow_pattern = ""
3766 val mutable m_hadremovals = false
3768 method getitemcount =
3769 Array.length m_items + (if m_hadremovals then 1 else 0)
3771 method getitem n =
3772 if n == Array.length m_items && m_hadremovals
3773 then
3774 ("[Confirm removal]", 0)
3775 else
3776 let s, n, _ = m_items.(n) in
3777 (s, n)
3779 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3780 ignore (uioh, first, qsearch);
3781 let confrimremoval = m_hadremovals && active = Array.length m_items in
3782 let items =
3783 if String.length m_narrow_pattern = 0
3784 then m_orig_items
3785 else m_items
3787 if not cancel
3788 then (
3789 if not confrimremoval
3790 then(
3791 let _, _, anchor = m_items.(active) in
3792 gotoghyll (getanchory anchor);
3793 m_items <- items;
3795 else (
3796 state.bookmarks <- Array.to_list m_items;
3797 m_orig_items <- m_items;
3800 else m_items <- items;
3801 m_pan <- pan;
3802 None
3804 method hasaction _ = true
3806 method greetmsg =
3807 if Array.length m_items != Array.length m_orig_items
3808 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3809 else ""
3811 method narrow pattern =
3812 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3813 match reopt with
3814 | None -> ()
3815 | Some re ->
3816 let rec loop accu n =
3817 if n = -1
3818 then (
3819 m_narrow_pattern <- pattern;
3820 m_items <- Array.of_list accu
3822 else
3823 let (s, _, _) as o = m_items.(n) in
3824 let accu =
3825 if (try ignore (Str.search_forward re s 0); true
3826 with Not_found -> false)
3827 then o :: accu
3828 else accu
3830 loop accu (n-1)
3832 loop [] (Array.length m_items - 1)
3834 method denarrow =
3835 m_orig_items <- (
3836 if usebookmarks
3837 then Array.of_list state.bookmarks
3838 else state.outlines
3840 m_items <- m_orig_items
3842 method remove m =
3843 if usebookmarks
3844 then
3845 if m >= 0 && m < Array.length m_items
3846 then (
3847 m_hadremovals <- true;
3848 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3849 let n = if n >= m then n+1 else n in
3850 m_items.(n)
3854 method reset anchor items =
3855 m_hadremovals <- false;
3856 if m_orig_items == empty || m_prev_items != items
3857 then (
3858 m_orig_items <- items;
3859 if String.length m_narrow_pattern = 0
3860 then m_items <- items;
3862 m_prev_items <- items;
3863 let rely = getanchory anchor in
3864 let active =
3865 let rec loop n best bestd =
3866 if n = Array.length m_items
3867 then best
3868 else
3869 let (_, _, anchor) = m_items.(n) in
3870 let orely = getanchory anchor in
3871 let d = abs (orely - rely) in
3872 if d < bestd
3873 then loop (n+1) n d
3874 else loop (n+1) best bestd
3876 loop 0 ~-1 max_int
3878 m_active <- active;
3879 m_first <- firstof m_first active
3880 end)
3883 let enterselector usebookmarks =
3884 let source = outlinesource usebookmarks in
3885 fun errmsg ->
3886 let outlines =
3887 if usebookmarks
3888 then Array.of_list state.bookmarks
3889 else state.outlines
3891 if Array.length outlines = 0
3892 then (
3893 showtext ' ' errmsg;
3895 else (
3896 state.text <- source#greetmsg;
3897 Wsi.setcursor Wsi.CURSOR_INHERIT;
3898 let anchor = getanchor () in
3899 source#reset anchor outlines;
3900 state.uioh <- coe (new outlinelistview ~source);
3901 G.postRedisplay "enter selector";
3905 let enteroutlinemode =
3906 let f = enterselector false in
3907 fun ()-> f "Document has no outline";
3910 let enterbookmarkmode =
3911 let f = enterselector true in
3912 fun () -> f "Document has no bookmarks (yet)";
3915 let color_of_string s =
3916 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3917 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3921 let color_to_string (r, g, b) =
3922 let r = truncate (r *. 256.0)
3923 and g = truncate (g *. 256.0)
3924 and b = truncate (b *. 256.0) in
3925 Printf.sprintf "%d/%d/%d" r g b
3928 let irect_of_string s =
3929 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3932 let irect_to_string (x0,y0,x1,y1) =
3933 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3936 let makecheckers () =
3937 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3938 following to say:
3939 converted by Issac Trotts. July 25, 2002 *)
3940 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3941 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3942 let id = GlTex.gen_texture () in
3943 GlTex.bind_texture `texture_2d id;
3944 GlPix.store (`unpack_alignment 1);
3945 GlTex.image2d image;
3946 List.iter (GlTex.parameter ~target:`texture_2d)
3947 [ `mag_filter `nearest; `min_filter `nearest ];
3951 let setcheckers enabled =
3952 match state.texid with
3953 | None ->
3954 if enabled then state.texid <- Some (makecheckers ())
3956 | Some texid ->
3957 if not enabled
3958 then (
3959 GlTex.delete_texture texid;
3960 state.texid <- None;
3964 let int_of_string_with_suffix s =
3965 let l = String.length s in
3966 let s1, shift =
3967 if l > 1
3968 then
3969 let suffix = Char.lowercase s.[l-1] in
3970 match suffix with
3971 | 'k' -> String.sub s 0 (l-1), 10
3972 | 'm' -> String.sub s 0 (l-1), 20
3973 | 'g' -> String.sub s 0 (l-1), 30
3974 | _ -> s, 0
3975 else s, 0
3977 let n = int_of_string s1 in
3978 let m = n lsl shift in
3979 if m < 0 || m < n
3980 then raise (Failure "value too large")
3981 else m
3984 let string_with_suffix_of_int n =
3985 if n = 0
3986 then "0"
3987 else
3988 let n, s =
3989 if n land ((1 lsl 30) - 1) = 0
3990 then n lsr 30, "G"
3991 else (
3992 if n land ((1 lsl 20) - 1) = 0
3993 then n lsr 20, "M"
3994 else (
3995 if n land ((1 lsl 10) - 1) = 0
3996 then n lsr 10, "K"
3997 else n, ""
4001 let rec loop s n =
4002 let h = n mod 1000 in
4003 let n = n / 1000 in
4004 if n = 0
4005 then string_of_int h ^ s
4006 else (
4007 let s = Printf.sprintf "_%03d%s" h s in
4008 loop s n
4011 loop "" n ^ s;
4014 let defghyllscroll = (40, 8, 32);;
4015 let ghyllscroll_of_string s =
4016 let (n, a, b) as nab =
4017 if s = "default"
4018 then defghyllscroll
4019 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4021 if n <= a || n <= b || a >= b
4022 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4023 nab;
4026 let ghyllscroll_to_string ((n, a, b) as nab) =
4027 if nab = defghyllscroll
4028 then "default"
4029 else Printf.sprintf "%d,%d,%d" n a b;
4032 let describe_location () =
4033 let fn = page_of_y state.y in
4034 let ln = page_of_y (state.y + state.winh - state.hscrollh) in
4035 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4036 let percent =
4037 if maxy <= 0
4038 then 100.
4039 else (100. *. (float state.y /. float maxy))
4041 if fn = ln
4042 then
4043 Printf.sprintf "page %d of %d [%.2f%%]"
4044 (fn+1) state.pagecount percent
4045 else
4046 Printf.sprintf
4047 "pages %d-%d of %d [%.2f%%]"
4048 (fn+1) (ln+1) state.pagecount percent
4051 let setpresentationmode v =
4052 let n = page_of_y state.y in
4053 state.anchor <- (n, 0.0, 1.0);
4054 conf.presentation <- v;
4055 if conf.presentation
4056 then (
4057 if not conf.scrollbarinpm
4058 then state.scrollw <- 0;
4060 else state.scrollw <- conf.scrollbw;
4061 represent ();
4064 let enterinfomode =
4065 let btos b = if b then "\xe2\x88\x9a" else "" in
4066 let showextended = ref false in
4067 let leave mode = function
4068 | Confirm -> state.mode <- mode
4069 | Cancel -> state.mode <- mode in
4070 let src =
4071 (object
4072 val mutable m_first_time = true
4073 val mutable m_l = []
4074 val mutable m_a = [||]
4075 val mutable m_prev_uioh = nouioh
4076 val mutable m_prev_mode = View
4078 inherit lvsourcebase
4080 method reset prev_mode prev_uioh =
4081 m_a <- Array.of_list (List.rev m_l);
4082 m_l <- [];
4083 m_prev_mode <- prev_mode;
4084 m_prev_uioh <- prev_uioh;
4085 if m_first_time
4086 then (
4087 let rec loop n =
4088 if n >= Array.length m_a
4089 then ()
4090 else
4091 match m_a.(n) with
4092 | _, _, _, Action _ -> m_active <- n
4093 | _ -> loop (n+1)
4095 loop 0;
4096 m_first_time <- false;
4099 method int name get set =
4100 m_l <-
4101 (name, `int get, 1, Action (
4102 fun u ->
4103 let ondone s =
4104 try set (int_of_string s)
4105 with exn ->
4106 state.text <- Printf.sprintf "bad integer `%s': %s"
4107 s (exntos exn)
4109 state.text <- "";
4110 let te = name ^ ": ", "", None, intentry, ondone, true in
4111 state.mode <- Textentry (te, leave m_prev_mode);
4113 )) :: m_l
4115 method int_with_suffix name get set =
4116 m_l <-
4117 (name, `intws get, 1, Action (
4118 fun u ->
4119 let ondone s =
4120 try set (int_of_string_with_suffix s)
4121 with exn ->
4122 state.text <- Printf.sprintf "bad integer `%s': %s"
4123 s (exntos exn)
4125 state.text <- "";
4126 let te =
4127 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4129 state.mode <- Textentry (te, leave m_prev_mode);
4131 )) :: m_l
4133 method bool ?(offset=1) ?(btos=btos) name get set =
4134 m_l <-
4135 (name, `bool (btos, get), offset, Action (
4136 fun u ->
4137 let v = get () in
4138 set (not v);
4140 )) :: m_l
4142 method color name get set =
4143 m_l <-
4144 (name, `color get, 1, Action (
4145 fun u ->
4146 let invalid = (nan, nan, nan) in
4147 let ondone s =
4148 let c =
4149 try color_of_string s
4150 with exn ->
4151 state.text <- Printf.sprintf "bad color `%s': %s"
4152 s (exntos exn);
4153 invalid
4155 if c <> invalid
4156 then set c;
4158 let te = name ^ ": ", "", None, textentry, ondone, true in
4159 state.text <- color_to_string (get ());
4160 state.mode <- Textentry (te, leave m_prev_mode);
4162 )) :: m_l
4164 method string name get set =
4165 m_l <-
4166 (name, `string get, 1, Action (
4167 fun u ->
4168 let ondone s = set s in
4169 let te = name ^ ": ", "", None, textentry, ondone, true in
4170 state.mode <- Textentry (te, leave m_prev_mode);
4172 )) :: m_l
4174 method colorspace name get set =
4175 m_l <-
4176 (name, `string get, 1, Action (
4177 fun _ ->
4178 let source =
4179 let vals = [| "rgb"; "bgr"; "gray" |] in
4180 (object
4181 inherit lvsourcebase
4183 initializer
4184 m_active <- int_of_colorspace conf.colorspace;
4185 m_first <- 0;
4187 method getitemcount = Array.length vals
4188 method getitem n = (vals.(n), 0)
4189 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4190 ignore (uioh, first, pan, qsearch);
4191 if not cancel then set active;
4192 None
4193 method hasaction _ = true
4194 end)
4196 state.text <- "";
4197 let modehash = findkeyhash conf "info" in
4198 coe (new listview ~source ~trusted:true ~modehash)
4199 )) :: m_l
4201 method caption s offset =
4202 m_l <- (s, `empty, offset, Noaction) :: m_l
4204 method caption2 s f offset =
4205 m_l <- (s, `string f, offset, Noaction) :: m_l
4207 method getitemcount = Array.length m_a
4209 method getitem n =
4210 let tostr = function
4211 | `int f -> string_of_int (f ())
4212 | `intws f -> string_with_suffix_of_int (f ())
4213 | `string f -> f ()
4214 | `color f -> color_to_string (f ())
4215 | `bool (btos, f) -> btos (f ())
4216 | `empty -> ""
4218 let name, t, offset, _ = m_a.(n) in
4219 ((let s = tostr t in
4220 if String.length s > 0
4221 then Printf.sprintf "%s\t%s" name s
4222 else name),
4223 offset)
4225 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4226 let uiohopt =
4227 if not cancel
4228 then (
4229 m_qsearch <- qsearch;
4230 let uioh =
4231 match m_a.(active) with
4232 | _, _, _, Action f -> f uioh
4233 | _ -> uioh
4235 Some uioh
4237 else None
4239 m_active <- active;
4240 m_first <- first;
4241 m_pan <- pan;
4242 uiohopt
4244 method hasaction n =
4245 match m_a.(n) with
4246 | _, _, _, Action _ -> true
4247 | _ -> false
4248 end)
4250 let rec fillsrc prevmode prevuioh =
4251 let sep () = src#caption "" 0 in
4252 let colorp name get set =
4253 src#string name
4254 (fun () -> color_to_string (get ()))
4255 (fun v ->
4257 let c = color_of_string v in
4258 set c
4259 with exn ->
4260 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4263 let oldmode = state.mode in
4264 let birdseye = isbirdseye state.mode in
4266 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4268 src#bool "presentation mode"
4269 (fun () -> conf.presentation)
4270 (fun v -> setpresentationmode v);
4272 src#bool "ignore case in searches"
4273 (fun () -> conf.icase)
4274 (fun v -> conf.icase <- v);
4276 src#bool "preload"
4277 (fun () -> conf.preload)
4278 (fun v -> conf.preload <- v);
4280 src#bool "highlight links"
4281 (fun () -> conf.hlinks)
4282 (fun v -> conf.hlinks <- v);
4284 src#bool "under info"
4285 (fun () -> conf.underinfo)
4286 (fun v -> conf.underinfo <- v);
4288 src#bool "persistent bookmarks"
4289 (fun () -> conf.savebmarks)
4290 (fun v -> conf.savebmarks <- v);
4292 src#bool "proportional display"
4293 (fun () -> conf.proportional)
4294 (fun v -> reqlayout conf.angle v);
4296 src#bool "trim margins"
4297 (fun () -> conf.trimmargins)
4298 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4300 src#bool "persistent location"
4301 (fun () -> conf.jumpback)
4302 (fun v -> conf.jumpback <- v);
4304 sep ();
4305 src#int "inter-page space"
4306 (fun () -> conf.interpagespace)
4307 (fun n ->
4308 conf.interpagespace <- n;
4309 docolumns conf.columns;
4310 let pageno, py =
4311 match state.layout with
4312 | [] -> 0, 0
4313 | l :: _ ->
4314 l.pageno, l.pagey
4316 state.maxy <- calcheight ();
4317 let y = getpagey pageno in
4318 gotoy (y + py)
4321 src#int "page bias"
4322 (fun () -> conf.pagebias)
4323 (fun v -> conf.pagebias <- v);
4325 src#int "scroll step"
4326 (fun () -> conf.scrollstep)
4327 (fun n -> conf.scrollstep <- n);
4329 src#int "horizontal scroll step"
4330 (fun () -> conf.hscrollstep)
4331 (fun v -> conf.hscrollstep <- v);
4333 src#int "auto scroll step"
4334 (fun () ->
4335 match state.autoscroll with
4336 | Some step -> step
4337 | _ -> conf.autoscrollstep)
4338 (fun n ->
4339 if state.autoscroll <> None
4340 then state.autoscroll <- Some n;
4341 conf.autoscrollstep <- n);
4343 src#int "zoom"
4344 (fun () -> truncate (conf.zoom *. 100.))
4345 (fun v -> setzoom ((float v) /. 100.));
4347 src#int "rotation"
4348 (fun () -> conf.angle)
4349 (fun v -> reqlayout v conf.proportional);
4351 src#int "scroll bar width"
4352 (fun () -> state.scrollw)
4353 (fun v ->
4354 state.scrollw <- v;
4355 conf.scrollbw <- v;
4356 reshape state.winw state.winh;
4359 src#int "scroll handle height"
4360 (fun () -> conf.scrollh)
4361 (fun v -> conf.scrollh <- v;);
4363 src#int "thumbnail width"
4364 (fun () -> conf.thumbw)
4365 (fun v ->
4366 conf.thumbw <- min 4096 v;
4367 match oldmode with
4368 | Birdseye beye ->
4369 leavebirdseye beye false;
4370 enterbirdseye ()
4371 | _ -> ()
4374 let mode = state.mode in
4375 src#string "columns"
4376 (fun () ->
4377 match conf.columns with
4378 | Csingle _ -> "1"
4379 | Cmulti (multi, _) -> multicolumns_to_string multi
4380 | Csplit (count, _) -> "-" ^ string_of_int count
4382 (fun v ->
4383 let n, a, b = multicolumns_of_string v in
4384 setcolumns mode n a b);
4386 sep ();
4387 src#caption "Presentation mode" 0;
4388 src#bool "scrollbar visible"
4389 (fun () -> conf.scrollbarinpm)
4390 (fun v ->
4391 if v != conf.scrollbarinpm
4392 then (
4393 conf.scrollbarinpm <- v;
4394 if conf.presentation
4395 then (
4396 state.scrollw <- if v then conf.scrollbw else 0;
4397 reshape state.winw state.winh;
4402 sep ();
4403 src#caption "Pixmap cache" 0;
4404 src#int_with_suffix "size (advisory)"
4405 (fun () -> conf.memlimit)
4406 (fun v -> conf.memlimit <- v);
4408 src#caption2 "used"
4409 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4410 (string_with_suffix_of_int state.memused)
4411 (Hashtbl.length state.tilemap)) 1;
4413 sep ();
4414 src#caption "Layout" 0;
4415 src#caption2 "Dimension"
4416 (fun () ->
4417 Printf.sprintf "%dx%d (virtual %dx%d)"
4418 state.winw state.winh
4419 state.w state.maxy)
4421 if conf.debug
4422 then
4423 src#caption2 "Position" (fun () ->
4424 Printf.sprintf "%dx%d" state.x state.y
4426 else
4427 src#caption2 "Position" (fun () -> describe_location ()) 1
4430 sep ();
4431 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4432 "Save these parameters as global defaults at exit"
4433 (fun () -> conf.bedefault)
4434 (fun v -> conf.bedefault <- v)
4437 sep ();
4438 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4439 src#bool ~offset:0 ~btos "Extended parameters"
4440 (fun () -> !showextended)
4441 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4442 if !showextended
4443 then (
4444 src#bool "checkers"
4445 (fun () -> conf.checkers)
4446 (fun v -> conf.checkers <- v; setcheckers v);
4447 src#bool "update cursor"
4448 (fun () -> conf.updatecurs)
4449 (fun v -> conf.updatecurs <- v);
4450 src#bool "verbose"
4451 (fun () -> conf.verbose)
4452 (fun v -> conf.verbose <- v);
4453 src#bool "invert colors"
4454 (fun () -> conf.invert)
4455 (fun v -> conf.invert <- v);
4456 src#bool "max fit"
4457 (fun () -> conf.maxhfit)
4458 (fun v -> conf.maxhfit <- v);
4459 src#bool "redirect stderr"
4460 (fun () -> conf.redirectstderr)
4461 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4462 src#string "uri launcher"
4463 (fun () -> conf.urilauncher)
4464 (fun v -> conf.urilauncher <- v);
4465 src#string "path launcher"
4466 (fun () -> conf.pathlauncher)
4467 (fun v -> conf.pathlauncher <- v);
4468 src#string "tile size"
4469 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4470 (fun v ->
4472 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4473 conf.tilew <- max 64 w;
4474 conf.tileh <- max 64 h;
4475 flushtiles ();
4476 with exn ->
4477 state.text <- Printf.sprintf "bad tile size `%s': %s"
4478 v (exntos exn)
4480 src#int "texture count"
4481 (fun () -> conf.texcount)
4482 (fun v ->
4483 if realloctexts v
4484 then conf.texcount <- v
4485 else showtext '!' " Failed to set texture count please retry later"
4487 src#int "slice height"
4488 (fun () -> conf.sliceheight)
4489 (fun v ->
4490 conf.sliceheight <- v;
4491 wcmd "sliceh %d" conf.sliceheight;
4493 src#int "anti-aliasing level"
4494 (fun () -> conf.aalevel)
4495 (fun v ->
4496 conf.aalevel <- bound v 0 8;
4497 state.anchor <- getanchor ();
4498 opendoc state.path state.password;
4500 src#string "page scroll scaling factor"
4501 (fun () -> string_of_float conf.pgscale)
4502 (fun v ->
4504 let s = float_of_string v in
4505 conf.pgscale <- s
4506 with exn ->
4507 state.text <- Printf.sprintf
4508 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4511 src#int "ui font size"
4512 (fun () -> fstate.fontsize)
4513 (fun v -> setfontsize (bound v 5 100));
4514 src#int "hint font size"
4515 (fun () -> conf.hfsize)
4516 (fun v -> conf.hfsize <- bound v 5 100);
4517 colorp "background color"
4518 (fun () -> conf.bgcolor)
4519 (fun v -> conf.bgcolor <- v);
4520 src#bool "crop hack"
4521 (fun () -> conf.crophack)
4522 (fun v -> conf.crophack <- v);
4523 src#string "trim fuzz"
4524 (fun () -> irect_to_string conf.trimfuzz)
4525 (fun v ->
4527 conf.trimfuzz <- irect_of_string v;
4528 if conf.trimmargins
4529 then settrim true conf.trimfuzz;
4530 with exn ->
4531 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4533 src#string "throttle"
4534 (fun () ->
4535 match conf.maxwait with
4536 | None -> "show place holder if page is not ready"
4537 | Some time ->
4538 if time = infinity
4539 then "wait for page to fully render"
4540 else
4541 "wait " ^ string_of_float time
4542 ^ " seconds before showing placeholder"
4544 (fun v ->
4546 let f = float_of_string v in
4547 if f <= 0.0
4548 then conf.maxwait <- None
4549 else conf.maxwait <- Some f
4550 with exn ->
4551 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4553 src#string "ghyll scroll"
4554 (fun () ->
4555 match conf.ghyllscroll with
4556 | None -> ""
4557 | Some nab -> ghyllscroll_to_string nab
4559 (fun v ->
4561 let gs =
4562 if String.length v = 0
4563 then None
4564 else Some (ghyllscroll_of_string v)
4566 conf.ghyllscroll <- gs
4567 with exn ->
4568 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4570 src#string "selection command"
4571 (fun () -> conf.selcmd)
4572 (fun v -> conf.selcmd <- v);
4573 src#string "synctex command"
4574 (fun () -> conf.stcmd)
4575 (fun v -> conf.stcmd <- v);
4576 src#colorspace "color space"
4577 (fun () -> colorspace_to_string conf.colorspace)
4578 (fun v ->
4579 conf.colorspace <- colorspace_of_int v;
4580 wcmd "cs %d" v;
4581 load state.layout;
4583 if pbousable ()
4584 then
4585 src#bool "use PBO"
4586 (fun () -> conf.usepbo)
4587 (fun v -> conf.usepbo <- v);
4588 src#bool "mouse wheel scrolls pages"
4589 (fun () -> conf.wheelbypage)
4590 (fun v -> conf.wheelbypage <- v);
4593 sep ();
4594 src#caption "Document" 0;
4595 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4596 src#caption2 "Pages"
4597 (fun () -> string_of_int state.pagecount) 1;
4598 src#caption2 "Dimensions"
4599 (fun () -> string_of_int (List.length state.pdims)) 1;
4600 if conf.trimmargins
4601 then (
4602 sep ();
4603 src#caption "Trimmed margins" 0;
4604 src#caption2 "Dimensions"
4605 (fun () -> string_of_int (List.length state.pdims)) 1;
4608 sep ();
4609 src#caption "OpenGL" 0;
4610 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4611 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4612 src#reset prevmode prevuioh;
4614 fun () ->
4615 state.text <- "";
4616 let prevmode = state.mode
4617 and prevuioh = state.uioh in
4618 fillsrc prevmode prevuioh;
4619 let source = (src :> lvsource) in
4620 let modehash = findkeyhash conf "info" in
4621 state.uioh <- coe (object (self)
4622 inherit listview ~source ~trusted:true ~modehash as super
4623 val mutable m_prevmemused = 0
4624 method infochanged = function
4625 | Memused ->
4626 if m_prevmemused != state.memused
4627 then (
4628 m_prevmemused <- state.memused;
4629 G.postRedisplay "memusedchanged";
4631 | Pdim -> G.postRedisplay "pdimchanged"
4632 | Docinfo -> fillsrc prevmode prevuioh
4634 method key key mask =
4635 if not (Wsi.withctrl mask)
4636 then
4637 match key with
4638 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4639 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4640 | _ -> super#key key mask
4641 else super#key key mask
4642 end);
4643 G.postRedisplay "info";
4646 let enterhelpmode =
4647 let source =
4648 (object
4649 inherit lvsourcebase
4650 method getitemcount = Array.length state.help
4651 method getitem n =
4652 let s, l, _ = state.help.(n) in
4653 (s, l)
4655 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4656 let optuioh =
4657 if not cancel
4658 then (
4659 m_qsearch <- qsearch;
4660 match state.help.(active) with
4661 | _, _, Action f -> Some (f uioh)
4662 | _ -> Some (uioh)
4664 else None
4666 m_active <- active;
4667 m_first <- first;
4668 m_pan <- pan;
4669 optuioh
4671 method hasaction n =
4672 match state.help.(n) with
4673 | _, _, Action _ -> true
4674 | _ -> false
4676 initializer
4677 m_active <- -1
4678 end)
4679 in fun () ->
4680 let modehash = findkeyhash conf "help" in
4681 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4682 G.postRedisplay "help";
4685 let entermsgsmode =
4686 let msgsource =
4687 let re = Str.regexp "[\r\n]" in
4688 (object
4689 inherit lvsourcebase
4690 val mutable m_items = [||]
4692 method getitemcount = 1 + Array.length m_items
4694 method getitem n =
4695 if n = 0
4696 then "[Clear]", 0
4697 else m_items.(n-1), 0
4699 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4700 ignore uioh;
4701 if not cancel
4702 then (
4703 if active = 0
4704 then Buffer.clear state.errmsgs;
4705 m_qsearch <- qsearch;
4707 m_active <- active;
4708 m_first <- first;
4709 m_pan <- pan;
4710 None
4712 method hasaction n =
4713 n = 0
4715 method reset =
4716 state.newerrmsgs <- false;
4717 let l = Str.split re (Buffer.contents state.errmsgs) in
4718 m_items <- Array.of_list l
4720 initializer
4721 m_active <- 0
4722 end)
4723 in fun () ->
4724 state.text <- "";
4725 msgsource#reset;
4726 let source = (msgsource :> lvsource) in
4727 let modehash = findkeyhash conf "listview" in
4728 state.uioh <- coe (object
4729 inherit listview ~source ~trusted:false ~modehash as super
4730 method display =
4731 if state.newerrmsgs
4732 then msgsource#reset;
4733 super#display
4734 end);
4735 G.postRedisplay "msgs";
4738 let quickbookmark ?title () =
4739 match state.layout with
4740 | [] -> ()
4741 | l :: _ ->
4742 let title =
4743 match title with
4744 | None ->
4745 let sec = Unix.gettimeofday () in
4746 let tm = Unix.localtime sec in
4747 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4748 (l.pageno+1)
4749 tm.Unix.tm_mday
4750 tm.Unix.tm_mon
4751 (tm.Unix.tm_year + 1900)
4752 tm.Unix.tm_hour
4753 tm.Unix.tm_min
4754 | Some title -> title
4756 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4759 let doreshape w h =
4760 Wsi.reshape w h;
4763 let setautoscrollspeed step goingdown =
4764 let incr = max 1 ((abs step) / 2) in
4765 let incr = if goingdown then incr else -incr in
4766 let astep = step + incr in
4767 state.autoscroll <- Some astep;
4770 let gotounder = function
4771 | Ulinkgoto (pageno, top) ->
4772 if pageno >= 0
4773 then (
4774 addnav ();
4775 gotopage1 pageno top;
4778 | Ulinkuri s ->
4779 gotouri s
4781 | Uremote (filename, pageno) ->
4782 let path =
4783 if Sys.file_exists filename
4784 then filename
4785 else
4786 let dir = Filename.dirname state.path in
4787 let path = Filename.concat dir filename in
4788 if Sys.file_exists path
4789 then path
4790 else ""
4792 if String.length path > 0
4793 then (
4794 let anchor = getanchor () in
4795 let ranchor = state.path, state.password, anchor in
4796 state.anchor <- (pageno, 0.0, 0.0);
4797 state.ranchors <- ranchor :: state.ranchors;
4798 opendoc path "";
4800 else showtext '!' ("Could not find " ^ filename)
4802 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4805 let canpan () =
4806 match conf.columns with
4807 | Csplit _ -> true
4808 | _ -> state.x != 0 || conf.zoom > 1.0
4811 let existsinrow pageno (columns, coverA, coverB) p =
4812 let last = ((pageno - coverA) mod columns) + columns in
4813 let rec any = function
4814 | [] -> false
4815 | l :: rest ->
4816 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4817 then p l
4818 else (
4819 if not (p l)
4820 then (if l.pageno = last then false else any rest)
4821 else true
4824 any state.layout
4827 let nextpage () =
4828 match state.layout with
4829 | [] ->
4830 let pageno = page_of_y state.y in
4831 gotoghyll (getpagey (pageno+1))
4832 | l :: rest ->
4833 match conf.columns with
4834 | Csingle _ ->
4835 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4836 then
4837 let y = clamp (pgscale state.winh) in
4838 gotoghyll y
4839 else
4840 let pageno = min (l.pageno+1) (state.pagecount-1) in
4841 gotoghyll (getpagey pageno)
4842 | Cmulti ((c, _, _) as cl, _) ->
4843 if conf.presentation
4844 && (existsinrow l.pageno cl
4845 (fun l -> l.pageh > l.pagey + l.pagevh))
4846 then
4847 let y = clamp (pgscale state.winh) in
4848 gotoghyll y
4849 else
4850 let pageno = min (l.pageno+c) (state.pagecount-1) in
4851 gotoghyll (getpagey pageno)
4852 | Csplit (n, _) ->
4853 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4854 then
4855 let pagey, pageh = getpageyh l.pageno in
4856 let pagey = pagey + pageh * l.pagecol in
4857 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4858 gotoghyll (pagey + pageh + ips)
4861 let prevpage () =
4862 match state.layout with
4863 | [] ->
4864 let pageno = page_of_y state.y in
4865 gotoghyll (getpagey (pageno-1))
4866 | l :: _ ->
4867 match conf.columns with
4868 | Csingle _ ->
4869 if conf.presentation && l.pagey != 0
4870 then
4871 gotoghyll (clamp (pgscale ~-(state.winh)))
4872 else
4873 let pageno = max 0 (l.pageno-1) in
4874 gotoghyll (getpagey pageno)
4875 | Cmulti ((c, _, coverB) as cl, _) ->
4876 if conf.presentation &&
4877 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4878 then
4879 gotoghyll (clamp (pgscale ~-(state.winh)))
4880 else
4881 let decr =
4882 if l.pageno = state.pagecount - coverB
4883 then 1
4884 else c
4886 let pageno = max 0 (l.pageno-decr) in
4887 gotoghyll (getpagey pageno)
4888 | Csplit (n, _) ->
4889 let y =
4890 if l.pagecol = 0
4891 then
4892 if l.pageno = 0
4893 then l.pagey
4894 else
4895 let pageno = max 0 (l.pageno-1) in
4896 let pagey, pageh = getpageyh pageno in
4897 pagey + (n-1)*pageh
4898 else
4899 let pagey, pageh = getpageyh l.pageno in
4900 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4902 gotoghyll y
4905 let viewkeyboard key mask =
4906 let enttext te =
4907 let mode = state.mode in
4908 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4909 state.text <- "";
4910 enttext ();
4911 G.postRedisplay "view:enttext"
4913 let ctrl = Wsi.withctrl mask in
4914 let key =
4915 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4917 match key with
4918 | 81 -> (* Q *)
4919 exit 0
4921 | 0xff63 -> (* insert *)
4922 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4923 then (
4924 state.mode <- LinkNav (Ltgendir 0);
4925 gotoy state.y;
4927 else showtext '!' "Keyboard link navigation does not work under rotation"
4929 | 0xff1b | 113 -> (* escape / q *)
4930 begin match state.mstate with
4931 | Mzoomrect _ ->
4932 state.mstate <- Mnone;
4933 Wsi.setcursor Wsi.CURSOR_INHERIT;
4934 G.postRedisplay "kill zoom rect";
4935 | _ ->
4936 begin match state.mode with
4937 | LinkNav _ ->
4938 state.mode <- View;
4939 G.postRedisplay "esc leave linknav"
4940 | _ ->
4941 match state.ranchors with
4942 | [] -> raise Quit
4943 | (path, password, anchor) :: rest ->
4944 state.ranchors <- rest;
4945 state.anchor <- anchor;
4946 opendoc path password
4947 end;
4948 end;
4950 | 0xff08 -> (* backspace *)
4951 gotoghyll (getnav ~-1)
4953 | 111 -> (* o *)
4954 enteroutlinemode ()
4956 | 117 -> (* u *)
4957 state.rects <- [];
4958 state.text <- "";
4959 G.postRedisplay "dehighlight";
4961 | 47 | 63 -> (* / ? *)
4962 let ondone isforw s =
4963 cbput state.hists.pat s;
4964 state.searchpattern <- s;
4965 search s isforw
4967 let s = String.create 1 in
4968 s.[0] <- Char.chr key;
4969 enttext (s, "", Some (onhist state.hists.pat),
4970 textentry, ondone (key = 47), true)
4972 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4973 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4974 setzoom (conf.zoom +. incr)
4976 | 43 | 0xffab -> (* + *)
4977 let ondone s =
4978 let n =
4979 try int_of_string s with exc ->
4980 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4981 max_int
4983 if n != max_int
4984 then (
4985 conf.pagebias <- n;
4986 state.text <- "page bias is now " ^ string_of_int n;
4989 enttext ("page bias: ", "", None, intentry, ondone, true)
4991 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4992 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4993 setzoom (max 0.01 (conf.zoom -. decr))
4995 | 45 | 0xffad -> (* - *)
4996 let ondone msg = state.text <- msg in
4997 enttext (
4998 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4999 optentry state.mode, ondone, true
5002 | 48 when ctrl -> (* ctrl-0 *)
5003 if conf.zoom = 1.0
5004 then (
5005 state.x <- 0;
5006 state.hscrollh <-
5007 if state.w <= state.winw - state.scrollw
5008 then 0
5009 else state.scrollw
5011 gotoy state.y
5013 else setzoom 1.0
5015 | 49 when ctrl -> (* ctrl-1 *)
5016 let cols =
5017 match conf.columns with
5018 | Csingle _ | Cmulti _ -> 1
5019 | Csplit (n, _) -> n
5021 let zoom = zoomforh state.winw state.winh state.scrollw cols in
5022 if zoom < 1.0
5023 then setzoom zoom
5025 | 0xffc6 -> (* f9 *)
5026 togglebirdseye ()
5028 | 57 when ctrl -> (* ctrl-9 *)
5029 togglebirdseye ()
5031 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5032 when not ctrl -> (* 0..9 *)
5033 let ondone s =
5034 let n =
5035 try int_of_string s with exc ->
5036 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5039 if n >= 0
5040 then (
5041 addnav ();
5042 cbput state.hists.pag (string_of_int n);
5043 gotopage1 (n + conf.pagebias - 1) 0;
5046 let pageentry text key =
5047 match Char.unsafe_chr key with
5048 | 'g' -> TEdone text
5049 | _ -> intentry text key
5051 let text = "x" in text.[0] <- Char.chr key;
5052 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5054 | 98 -> (* b *)
5055 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5056 reshape state.winw state.winh;
5058 | 108 -> (* l *)
5059 conf.hlinks <- not conf.hlinks;
5060 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5061 G.postRedisplay "toggle highlightlinks";
5063 | 70 -> (* F *)
5064 state.glinks <- true;
5065 let mode = state.mode in
5066 state.mode <- Textentry (
5067 (":", "", None, linknentry, linkndone gotounder, false),
5068 (fun _ ->
5069 state.glinks <- false;
5070 state.mode <- mode)
5072 state.text <- "";
5073 G.postRedisplay "view:linkent(F)"
5075 | 121 -> (* y *)
5076 state.glinks <- true;
5077 let mode = state.mode in
5078 state.mode <- Textentry (
5079 (":", "", None, linknentry, linkndone (fun under ->
5080 match Ne.pipe () with
5081 | Ne.Exn exn ->
5082 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
5083 | Ne.Res (r, w) ->
5084 let popened =
5085 try popen conf.selcmd [r, 0; w, -1]; true
5086 with exn ->
5087 showtext '!'
5088 (Printf.sprintf "failed to execute %s: %s"
5089 conf.selcmd (exntos exn));
5090 false
5092 let clo cap fd =
5093 Ne.clo fd (fun msg ->
5094 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5097 let s = undertext under in
5098 if popened
5099 then
5100 (try
5101 let l = String.length s in
5102 let n = tempfailureretry (Unix.write w s 0) l in
5103 if n != l
5104 then
5105 showtext '!'
5106 (Printf.sprintf
5107 "failed to write %d characters to sel pipe, wrote %d"
5110 with exn ->
5111 showtext '!'
5112 (Printf.sprintf "failed to write to sel pipe: %s"
5113 (exntos exn)
5116 else dolog "%s" s;
5117 clo "pipe/r" r;
5118 clo "pipe/w" w;
5119 ), false
5121 fun _ ->
5122 state.glinks <- false;
5123 state.mode <- mode
5125 state.text <- "";
5126 G.postRedisplay "view:linkent"
5128 | 97 -> (* a *)
5129 begin match state.autoscroll with
5130 | Some step ->
5131 conf.autoscrollstep <- step;
5132 state.autoscroll <- None
5133 | None ->
5134 if conf.autoscrollstep = 0
5135 then state.autoscroll <- Some 1
5136 else state.autoscroll <- Some conf.autoscrollstep
5139 | 112 when ctrl -> (* ctrl-p *)
5140 launchpath ()
5142 | 80 -> (* P *)
5143 setpresentationmode (not conf.presentation);
5144 showtext ' ' ("presentation mode " ^
5145 if conf.presentation then "on" else "off");
5147 | 102 -> (* f *)
5148 if List.mem Wsi.Fullscreen state.winstate
5149 then doreshape conf.cwinw conf.cwinh
5150 else Wsi.fullscreen ()
5152 | 112 | 78 -> (* p|N *)
5153 search state.searchpattern false
5155 | 110 | 0xffc0 -> (* n|F3 *)
5156 search state.searchpattern true
5158 | 116 -> (* t *)
5159 begin match state.layout with
5160 | [] -> ()
5161 | l :: _ ->
5162 gotoghyll (getpagey l.pageno)
5165 | 32 -> (* space *)
5166 nextpage ()
5168 | 0xff9f | 0xffff -> (* delete *)
5169 prevpage ()
5171 | 61 -> (* = *)
5172 showtext ' ' (describe_location ());
5174 | 119 -> (* w *)
5175 begin match state.layout with
5176 | [] -> ()
5177 | l :: _ ->
5178 doreshape (l.pagew + state.scrollw) l.pageh;
5179 G.postRedisplay "w"
5182 | 39 -> (* ' *)
5183 enterbookmarkmode ()
5185 | 104 | 0xffbe -> (* h|F1 *)
5186 enterhelpmode ()
5188 | 105 -> (* i *)
5189 enterinfomode ()
5191 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5192 entermsgsmode ()
5194 | 109 -> (* m *)
5195 let ondone s =
5196 match state.layout with
5197 | l :: _ ->
5198 if String.length s > 0
5199 then
5200 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5201 | _ -> ()
5203 enttext ("bookmark: ", "", None, textentry, ondone, true)
5205 | 126 -> (* ~ *)
5206 quickbookmark ();
5207 showtext ' ' "Quick bookmark added";
5209 | 122 -> (* z *)
5210 begin match state.layout with
5211 | l :: _ ->
5212 let rect = getpdimrect l.pagedimno in
5213 let w, h =
5214 if conf.crophack
5215 then
5216 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5217 truncate (1.2 *. (rect.(3) -. rect.(0))))
5218 else
5219 (truncate (rect.(1) -. rect.(0)),
5220 truncate (rect.(3) -. rect.(0)))
5222 let w = truncate ((float w)*.conf.zoom)
5223 and h = truncate ((float h)*.conf.zoom) in
5224 if w != 0 && h != 0
5225 then (
5226 state.anchor <- getanchor ();
5227 doreshape (w + state.scrollw) (h + conf.interpagespace)
5229 G.postRedisplay "z";
5231 | [] -> ()
5234 | 50 when ctrl -> (* ctrl-2 *)
5235 let maxw = getmaxw () in
5236 if maxw > 0.0
5237 then setzoom (maxw /. float state.winw)
5239 | 60 | 62 -> (* < > *)
5240 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5242 | 91 | 93 -> (* [ ] *)
5243 conf.colorscale <-
5244 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5246 G.postRedisplay "brightness";
5248 | 99 when state.mode = View -> (* c *)
5249 let (c, a, b), z =
5250 match state.prevcolumns with
5251 | None -> (1, 0, 0), 1.0
5252 | Some (columns, z) ->
5253 let cab =
5254 match columns with
5255 | Csplit (c, _) -> -c, 0, 0
5256 | Cmulti ((c, a, b), _) -> c, a, b
5257 | Csingle _ -> 1, 0, 0
5259 cab, z
5261 setcolumns View c a b;
5262 setzoom z;
5264 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5265 setzoom state.prevzoom
5267 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5268 begin match state.autoscroll with
5269 | None ->
5270 begin match state.mode with
5271 | Birdseye beye -> upbirdseye 1 beye
5272 | _ ->
5273 if ctrl
5274 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5275 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5277 | Some n ->
5278 setautoscrollspeed n false
5281 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5282 begin match state.autoscroll with
5283 | None ->
5284 begin match state.mode with
5285 | Birdseye beye -> downbirdseye 1 beye
5286 | _ ->
5287 if ctrl
5288 then gotoy_and_clear_text (clamp (state.winh/2))
5289 else gotoy_and_clear_text (clamp conf.scrollstep)
5291 | Some n ->
5292 setautoscrollspeed n true
5295 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5296 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5297 if canpan ()
5298 then
5299 let dx =
5300 if ctrl
5301 then state.winw / 2
5302 else conf.hscrollstep
5304 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5305 state.x <- state.x + dx;
5306 gotoy_and_clear_text state.y
5307 else (
5308 state.text <- "";
5309 G.postRedisplay "lef/right"
5312 | 0xff55 | 0xff9a -> (* (kp) prior *)
5313 let y =
5314 if ctrl
5315 then
5316 match state.layout with
5317 | [] -> state.y
5318 | l :: _ -> state.y - l.pagey
5319 else
5320 clamp (pgscale (-state.winh))
5322 gotoghyll y
5324 | 0xff56 | 0xff9b -> (* (kp) next *)
5325 let y =
5326 if ctrl
5327 then
5328 match List.rev state.layout with
5329 | [] -> state.y
5330 | l :: _ -> getpagey l.pageno
5331 else
5332 clamp (pgscale state.winh)
5334 gotoghyll y
5336 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5337 gotoghyll 0
5338 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5339 gotoghyll (clamp state.maxy)
5341 | 0xff53 | 0xff98
5342 when Wsi.withalt mask -> (* alt-(kp) right *)
5343 gotoghyll (getnav 1)
5344 | 0xff51 | 0xff96
5345 when Wsi.withalt mask -> (* alt-(kp) left *)
5346 gotoghyll (getnav ~-1)
5348 | 114 -> (* r *)
5349 reload ()
5351 | 118 when conf.debug -> (* v *)
5352 state.rects <- [];
5353 List.iter (fun l ->
5354 match getopaque l.pageno with
5355 | None -> ()
5356 | Some opaque ->
5357 let x0, y0, x1, y1 = pagebbox opaque in
5358 let a,b = float x0, float y0 in
5359 let c,d = float x1, float y0 in
5360 let e,f = float x1, float y1 in
5361 let h,j = float x0, float y1 in
5362 let rect = (a,b,c,d,e,f,h,j) in
5363 debugrect rect;
5364 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5365 ) state.layout;
5366 G.postRedisplay "v";
5368 | _ ->
5369 vlog "huh? %s" (Wsi.keyname key)
5372 let linknavkeyboard key mask linknav =
5373 let getpage pageno =
5374 let rec loop = function
5375 | [] -> None
5376 | l :: _ when l.pageno = pageno -> Some l
5377 | _ :: rest -> loop rest
5378 in loop state.layout
5380 let doexact (pageno, n) =
5381 match getopaque pageno, getpage pageno with
5382 | Some opaque, Some l ->
5383 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5384 then
5385 let under = getlink opaque n in
5386 G.postRedisplay "link gotounder";
5387 gotounder under;
5388 state.mode <- View;
5389 else
5390 let opt, dir =
5391 match key with
5392 | 0xff50 -> (* home *)
5393 Some (findlink opaque LDfirst), -1
5395 | 0xff57 -> (* end *)
5396 Some (findlink opaque LDlast), 1
5398 | 0xff51 -> (* left *)
5399 Some (findlink opaque (LDleft n)), -1
5401 | 0xff53 -> (* right *)
5402 Some (findlink opaque (LDright n)), 1
5404 | 0xff52 -> (* up *)
5405 Some (findlink opaque (LDup n)), -1
5407 | 0xff54 -> (* down *)
5408 Some (findlink opaque (LDdown n)), 1
5410 | _ -> None, 0
5412 let pwl l dir =
5413 begin match findpwl l.pageno dir with
5414 | Pwlnotfound -> ()
5415 | Pwl pageno ->
5416 let notfound dir =
5417 state.mode <- LinkNav (Ltgendir dir);
5418 let y, h = getpageyh pageno in
5419 let y =
5420 if dir < 0
5421 then y + h - state.winh
5422 else y
5424 gotoy y
5426 begin match getopaque pageno, getpage pageno with
5427 | Some opaque, Some _ ->
5428 let link =
5429 let ld = if dir > 0 then LDfirst else LDlast in
5430 findlink opaque ld
5432 begin match link with
5433 | Lfound m ->
5434 showlinktype (getlink opaque m);
5435 state.mode <- LinkNav (Ltexact (pageno, m));
5436 G.postRedisplay "linknav jpage";
5437 | _ -> notfound dir
5438 end;
5439 | _ -> notfound dir
5440 end;
5441 end;
5443 begin match opt with
5444 | Some Lnotfound -> pwl l dir;
5445 | Some (Lfound m) ->
5446 if m = n
5447 then pwl l dir
5448 else (
5449 let _, y0, _, y1 = getlinkrect opaque m in
5450 if y0 < l.pagey
5451 then gotopage1 l.pageno y0
5452 else (
5453 let d = fstate.fontsize + 1 in
5454 if y1 - l.pagey > l.pagevh - d
5455 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5456 else G.postRedisplay "linknav";
5458 showlinktype (getlink opaque m);
5459 state.mode <- LinkNav (Ltexact (l.pageno, m));
5462 | None -> viewkeyboard key mask
5463 end;
5464 | _ -> viewkeyboard key mask
5466 if key = 0xff63
5467 then (
5468 state.mode <- View;
5469 G.postRedisplay "leave linknav"
5471 else
5472 match linknav with
5473 | Ltgendir _ -> viewkeyboard key mask
5474 | Ltexact exact -> doexact exact
5477 let keyboard key mask =
5478 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5479 then wcmd "interrupt"
5480 else state.uioh <- state.uioh#key key mask
5483 let birdseyekeyboard key mask
5484 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5485 let incr =
5486 match conf.columns with
5487 | Csingle _ -> 1
5488 | Cmulti ((c, _, _), _) -> c
5489 | Csplit _ -> failwith "bird's eye split mode"
5491 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5492 match key with
5493 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5494 let y, h = getpageyh pageno in
5495 let top = (state.winh - h) / 2 in
5496 gotoy (max 0 (y - top))
5497 | 0xff0d (* enter *)
5498 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5499 | 0xff1b -> leavebirdseye beye true (* escape *)
5500 | 0xff52 -> upbirdseye incr beye (* up *)
5501 | 0xff54 -> downbirdseye incr beye (* down *)
5502 | 0xff51 -> upbirdseye 1 beye (* left *)
5503 | 0xff53 -> downbirdseye 1 beye (* right *)
5505 | 0xff55 -> (* prior *)
5506 begin match state.layout with
5507 | l :: _ ->
5508 if l.pagey != 0
5509 then (
5510 state.mode <- Birdseye (
5511 oconf, leftx, l.pageno, hooverpageno, anchor
5513 gotopage1 l.pageno 0;
5515 else (
5516 let layout = layout (state.y-state.winh) (pgh state.layout) in
5517 match layout with
5518 | [] -> gotoy (clamp (-state.winh))
5519 | l :: _ ->
5520 state.mode <- Birdseye (
5521 oconf, leftx, l.pageno, hooverpageno, anchor
5523 gotopage1 l.pageno 0
5526 | [] -> gotoy (clamp (-state.winh))
5527 end;
5529 | 0xff56 -> (* next *)
5530 begin match List.rev state.layout with
5531 | l :: _ ->
5532 let layout = layout (state.y + (pgh state.layout)) state.winh in
5533 begin match layout with
5534 | [] ->
5535 let incr = l.pageh - l.pagevh in
5536 if incr = 0
5537 then (
5538 state.mode <-
5539 Birdseye (
5540 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5542 G.postRedisplay "birdseye pagedown";
5544 else gotoy (clamp (incr + conf.interpagespace*2));
5546 | l :: _ ->
5547 state.mode <-
5548 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5549 gotopage1 l.pageno 0;
5552 | [] -> gotoy (clamp state.winh)
5553 end;
5555 | 0xff50 -> (* home *)
5556 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5557 gotopage1 0 0
5559 | 0xff57 -> (* end *)
5560 let pageno = state.pagecount - 1 in
5561 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5562 if not (pagevisible state.layout pageno)
5563 then
5564 let h =
5565 match List.rev state.pdims with
5566 | [] -> state.winh
5567 | (_, _, h, _) :: _ -> h
5569 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5570 else G.postRedisplay "birdseye end";
5571 | _ -> viewkeyboard key mask
5574 let drawpage l linkindexbase =
5575 let color =
5576 match state.mode with
5577 | Textentry _ -> scalecolor 0.4
5578 | LinkNav _
5579 | View -> scalecolor 1.0
5580 | Birdseye (_, _, pageno, hooverpageno, _) ->
5581 if l.pageno = hooverpageno
5582 then scalecolor 0.9
5583 else (
5584 if l.pageno = pageno
5585 then scalecolor 1.0
5586 else scalecolor 0.8
5589 drawtiles l color;
5590 begin match getopaque l.pageno with
5591 | Some opaque ->
5592 if tileready l l.pagex l.pagey
5593 then
5594 let x = l.pagedispx - l.pagex
5595 and y = l.pagedispy - l.pagey in
5596 let hlmask =
5597 match conf.columns with
5598 | Csingle _ | Cmulti _ ->
5599 (if conf.hlinks then 1 else 0)
5600 + (if state.glinks
5601 && not (isbirdseye state.mode) then 2 else 0)
5602 | _ -> 0
5604 let s =
5605 match state.mode with
5606 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5607 | _ -> ""
5609 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5610 else 0
5612 | _ -> 0
5613 end;
5616 let scrollindicator () =
5617 let sbw, ph, sh = state.uioh#scrollph in
5618 let sbh, pw, sw = state.uioh#scrollpw in
5620 GlDraw.color (0.64, 0.64, 0.64);
5621 GlDraw.rect
5622 (float (state.winw - sbw), 0.)
5623 (float state.winw, float state.winh)
5625 GlDraw.rect
5626 (0., float (state.winh - sbh))
5627 (float (state.winw - state.scrollw - 1), float state.winh)
5629 GlDraw.color (0.0, 0.0, 0.0);
5631 GlDraw.rect
5632 (float (state.winw - sbw), ph)
5633 (float state.winw, ph +. sh)
5635 GlDraw.rect
5636 (pw, float (state.winh - sbh))
5637 (pw +. sw, float state.winh)
5641 let showsel () =
5642 match state.mstate with
5643 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5646 | Msel ((x0, y0), (x1, y1)) ->
5647 let rec loop = function
5648 | l :: ls ->
5649 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5650 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5651 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5652 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5653 then
5654 match getopaque l.pageno with
5655 | Some opaque ->
5656 let x0, y0 = pagetranslatepoint l x0 y0 in
5657 let x1, y1 = pagetranslatepoint l x1 y1 in
5658 seltext opaque (x0, y0, x1, y1);
5659 | _ -> ()
5660 else loop ls
5661 | [] -> ()
5663 loop state.layout
5666 let showrects rects =
5667 Gl.enable `blend;
5668 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5669 GlDraw.polygon_mode `both `fill;
5670 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5671 List.iter
5672 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5673 List.iter (fun l ->
5674 if l.pageno = pageno
5675 then (
5676 let dx = float (l.pagedispx - l.pagex) in
5677 let dy = float (l.pagedispy - l.pagey) in
5678 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5679 GlDraw.begins `quads;
5681 GlDraw.vertex2 (x0+.dx, y0+.dy);
5682 GlDraw.vertex2 (x1+.dx, y1+.dy);
5683 GlDraw.vertex2 (x2+.dx, y2+.dy);
5684 GlDraw.vertex2 (x3+.dx, y3+.dy);
5686 GlDraw.ends ();
5688 ) state.layout
5689 ) rects
5691 Gl.disable `blend;
5694 let display () =
5695 GlClear.color (scalecolor2 conf.bgcolor);
5696 GlClear.clear [`color];
5697 let rec loop linkindexbase = function
5698 | l :: rest ->
5699 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5700 loop linkindexbase rest
5701 | [] -> ()
5703 loop 0 state.layout;
5704 let rects =
5705 match state.mode with
5706 | LinkNav (Ltexact (pageno, linkno)) ->
5707 begin match getopaque pageno with
5708 | Some opaque ->
5709 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5710 (pageno, 5, (
5711 float x0, float y0,
5712 float x1, float y0,
5713 float x1, float y1,
5714 float x0, float y1)
5715 ) :: state.rects
5716 | None -> state.rects
5718 | _ -> state.rects
5720 showrects rects;
5721 showsel ();
5722 state.uioh#display;
5723 begin match state.mstate with
5724 | Mzoomrect ((x0, y0), (x1, y1)) ->
5725 Gl.enable `blend;
5726 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5727 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5728 GlDraw.rect (float x0, float y0)
5729 (float x1, float y1);
5730 Gl.disable `blend;
5731 | _ -> ()
5732 end;
5733 enttext ();
5734 scrollindicator ();
5735 Wsi.swapb ();
5738 let zoomrect x y x1 y1 =
5739 let x0 = min x x1
5740 and x1 = max x x1
5741 and y0 = min y y1 in
5742 gotoy (state.y + y0);
5743 state.anchor <- getanchor ();
5744 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5745 let margin =
5746 if state.w < state.winw - state.scrollw
5747 then (state.winw - state.scrollw - state.w) / 2
5748 else 0
5750 state.x <- (state.x + margin) - x0;
5751 setzoom zoom;
5752 Wsi.setcursor Wsi.CURSOR_INHERIT;
5753 state.mstate <- Mnone;
5756 let scrollx x =
5757 let winw = state.winw - state.scrollw - 1 in
5758 let s = float x /. float winw in
5759 let destx = truncate (float (state.w + winw) *. s) in
5760 state.x <- winw - destx;
5761 gotoy_and_clear_text state.y;
5762 state.mstate <- Mscrollx;
5765 let scrolly y =
5766 let s = float y /. float state.winh in
5767 let desty = truncate (float (state.maxy - state.winh) *. s) in
5768 gotoy_and_clear_text desty;
5769 state.mstate <- Mscrolly;
5772 let viewmouse button down x y mask =
5773 match button with
5774 | n when (n == 4 || n == 5) && not down ->
5775 if Wsi.withctrl mask
5776 then (
5777 match state.mstate with
5778 | Mzoom (oldn, i) ->
5779 if oldn = n
5780 then (
5781 if i = 2
5782 then
5783 let incr =
5784 match n with
5785 | 5 ->
5786 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5787 | _ ->
5788 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5790 let zoom = conf.zoom -. incr in
5791 setzoom zoom;
5792 state.mstate <- Mzoom (n, 0);
5793 else
5794 state.mstate <- Mzoom (n, i+1);
5796 else state.mstate <- Mzoom (n, 0)
5798 | _ -> state.mstate <- Mzoom (n, 0)
5800 else (
5801 match state.autoscroll with
5802 | Some step -> setautoscrollspeed step (n=4)
5803 | None ->
5804 if conf.wheelbypage
5805 then (
5806 if n = 4
5807 then prevpage ()
5808 else nextpage ()
5810 else
5811 let incr =
5812 if n = 4
5813 then -conf.scrollstep
5814 else conf.scrollstep
5816 let incr = incr * 2 in
5817 let y = clamp incr in
5818 gotoy_and_clear_text y
5821 | n when (n = 6 || n = 7) && not down && canpan () ->
5822 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5823 gotoy_and_clear_text state.y
5825 | 1 when Wsi.withshift mask ->
5826 state.mstate <- Mnone;
5827 if not down then (
5828 match unproject x y with
5829 | Some (pageno, ux, uy) ->
5830 let cmd = Printf.sprintf
5831 "%s %s %d %d %d"
5832 conf.stcmd state.path pageno ux uy
5834 popen cmd []
5835 | None -> ()
5838 | 1 when Wsi.withctrl mask ->
5839 if down
5840 then (
5841 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5842 state.mstate <- Mpan (x, y)
5844 else
5845 state.mstate <- Mnone
5847 | 3 ->
5848 if down
5849 then (
5850 Wsi.setcursor Wsi.CURSOR_CYCLE;
5851 let p = (x, y) in
5852 state.mstate <- Mzoomrect (p, p)
5854 else (
5855 match state.mstate with
5856 | Mzoomrect ((x0, y0), _) ->
5857 if abs (x-x0) > 10 && abs (y - y0) > 10
5858 then zoomrect x0 y0 x y
5859 else (
5860 state.mstate <- Mnone;
5861 Wsi.setcursor Wsi.CURSOR_INHERIT;
5862 G.postRedisplay "kill accidental zoom rect";
5864 | _ ->
5865 Wsi.setcursor Wsi.CURSOR_INHERIT;
5866 state.mstate <- Mnone
5869 | 1 when x > state.winw - state.scrollw ->
5870 if down
5871 then
5872 let _, position, sh = state.uioh#scrollph in
5873 if y > truncate position && y < truncate (position +. sh)
5874 then state.mstate <- Mscrolly
5875 else scrolly y
5876 else
5877 state.mstate <- Mnone
5879 | 1 when y > state.winh - state.hscrollh ->
5880 if down
5881 then
5882 let _, position, sw = state.uioh#scrollpw in
5883 if x > truncate position && x < truncate (position +. sw)
5884 then state.mstate <- Mscrollx
5885 else scrollx x
5886 else
5887 state.mstate <- Mnone
5889 | 1 ->
5890 let dest = if down then getunder x y else Unone in
5891 begin match dest with
5892 | Ulinkgoto _
5893 | Ulinkuri _
5894 | Uremote _
5895 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5896 gotounder dest
5898 | Unone when down ->
5899 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5900 state.mstate <- Mpan (x, y);
5902 | Unone | Utext _ ->
5903 if down
5904 then (
5905 if conf.angle mod 360 = 0
5906 then (
5907 state.mstate <- Msel ((x, y), (x, y));
5908 G.postRedisplay "mouse select";
5911 else (
5912 match state.mstate with
5913 | Mnone -> ()
5915 | Mzoom _ | Mscrollx | Mscrolly ->
5916 state.mstate <- Mnone
5918 | Mzoomrect ((x0, y0), _) ->
5919 zoomrect x0 y0 x y
5921 | Mpan _ ->
5922 Wsi.setcursor Wsi.CURSOR_INHERIT;
5923 state.mstate <- Mnone
5925 | Msel ((x0, y0), (x1, y1)) ->
5926 let rec loop = function
5927 | [] -> ()
5928 | l :: rest ->
5929 let inside =
5930 let a0 = l.pagedispy in
5931 let a1 = a0 + l.pagevh in
5932 let b0 = l.pagedispx in
5933 let b1 = b0 + l.pagevw in
5934 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5935 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5937 if inside
5938 then
5939 match getopaque l.pageno with
5940 | Some opaque ->
5941 begin
5942 match Ne.pipe () with
5943 | Ne.Exn exn ->
5944 showtext '!'
5945 (Printf.sprintf
5946 "can not create sel pipe: %s"
5947 (exntos exn));
5948 | Ne.Res (r, w) ->
5949 let doclose what fd =
5950 Ne.clo fd (fun msg ->
5951 dolog "%s close failed: %s" what msg)
5954 popen conf.selcmd [r, 0; w, -1];
5955 copysel w opaque;
5956 doclose "pipe/r" r;
5957 G.postRedisplay "copysel";
5958 with exn ->
5959 dolog "can not execute %S: %s"
5960 conf.selcmd (exntos exn);
5961 doclose "pipe/r" r;
5962 doclose "pipe/w" w;
5964 | None -> ()
5965 else loop rest
5967 loop state.layout;
5968 Wsi.setcursor Wsi.CURSOR_INHERIT;
5969 state.mstate <- Mnone;
5973 | _ -> ()
5976 let birdseyemouse button down x y mask
5977 (conf, leftx, _, hooverpageno, anchor) =
5978 match button with
5979 | 1 when down ->
5980 let rec loop = function
5981 | [] -> ()
5982 | l :: rest ->
5983 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5984 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5985 then (
5986 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5988 else loop rest
5990 loop state.layout
5991 | 3 -> ()
5992 | _ -> viewmouse button down x y mask
5995 let mouse button down x y mask =
5996 state.uioh <- state.uioh#button button down x y mask;
5999 let motion ~x ~y =
6000 state.uioh <- state.uioh#motion x y
6003 let pmotion ~x ~y =
6004 state.uioh <- state.uioh#pmotion x y;
6007 let uioh = object
6008 method display = ()
6010 method key key mask =
6011 begin match state.mode with
6012 | Textentry textentry -> textentrykeyboard key mask textentry
6013 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6014 | View -> viewkeyboard key mask
6015 | LinkNav linknav -> linknavkeyboard key mask linknav
6016 end;
6017 state.uioh
6019 method button button bstate x y mask =
6020 begin match state.mode with
6021 | LinkNav _
6022 | View -> viewmouse button bstate x y mask
6023 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6024 | Textentry _ -> ()
6025 end;
6026 state.uioh
6028 method motion x y =
6029 begin match state.mode with
6030 | Textentry _ -> ()
6031 | View | Birdseye _ | LinkNav _ ->
6032 match state.mstate with
6033 | Mzoom _ | Mnone -> ()
6035 | Mpan (x0, y0) ->
6036 let dx = x - x0
6037 and dy = y0 - y in
6038 state.mstate <- Mpan (x, y);
6039 if canpan ()
6040 then state.x <- state.x + dx;
6041 let y = clamp dy in
6042 gotoy_and_clear_text y
6044 | Msel (a, _) ->
6045 state.mstate <- Msel (a, (x, y));
6046 G.postRedisplay "motion select";
6048 | Mscrolly ->
6049 let y = min state.winh (max 0 y) in
6050 scrolly y
6052 | Mscrollx ->
6053 let x = min state.winw (max 0 x) in
6054 scrollx x
6056 | Mzoomrect (p0, _) ->
6057 state.mstate <- Mzoomrect (p0, (x, y));
6058 G.postRedisplay "motion zoomrect";
6059 end;
6060 state.uioh
6062 method pmotion x y =
6063 begin match state.mode with
6064 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6065 let rec loop = function
6066 | [] ->
6067 if hooverpageno != -1
6068 then (
6069 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6070 G.postRedisplay "pmotion birdseye no hoover";
6072 | l :: rest ->
6073 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6074 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6075 then (
6076 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6077 G.postRedisplay "pmotion birdseye hoover";
6079 else loop rest
6081 loop state.layout
6083 | Textentry _ -> ()
6085 | LinkNav _
6086 | View ->
6087 match state.mstate with
6088 | Mnone -> updateunder x y
6089 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6091 end;
6092 state.uioh
6094 method infochanged _ = ()
6096 method scrollph =
6097 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6098 let p, h = scrollph state.y maxy in
6099 state.scrollw, p, h
6101 method scrollpw =
6102 let winw = state.winw - state.scrollw - 1 in
6103 let fwinw = float winw in
6104 let sw =
6105 let sw = fwinw /. float state.w in
6106 let sw = fwinw *. sw in
6107 max sw (float conf.scrollh)
6109 let position, sw =
6110 let f = state.w+winw in
6111 let r = float (winw-state.x) /. float f in
6112 let p = fwinw *. r in
6113 p-.sw/.2., sw
6115 let sw =
6116 if position +. sw > fwinw
6117 then fwinw -. position
6118 else sw
6120 state.hscrollh, position, sw
6122 method modehash =
6123 let modename =
6124 match state.mode with
6125 | LinkNav _ -> "links"
6126 | Textentry _ -> "textentry"
6127 | Birdseye _ -> "birdseye"
6128 | View -> "view"
6130 findkeyhash conf modename
6131 end;;
6133 module Config =
6134 struct
6135 open Parser
6137 let fontpath = ref "";;
6139 module KeyMap =
6140 Map.Make (struct type t = (int * int) let compare = compare end);;
6142 let unent s =
6143 let l = String.length s in
6144 let b = Buffer.create l in
6145 unent b s 0 l;
6146 Buffer.contents b;
6149 let home =
6150 try Sys.getenv "HOME"
6151 with exn ->
6152 prerr_endline
6153 ("Can not determine home directory location: " ^ exntos exn);
6157 let modifier_of_string = function
6158 | "alt" -> Wsi.altmask
6159 | "shift" -> Wsi.shiftmask
6160 | "ctrl" | "control" -> Wsi.ctrlmask
6161 | "meta" -> Wsi.metamask
6162 | _ -> 0
6165 let key_of_string =
6166 let r = Str.regexp "-" in
6167 fun s ->
6168 let elems = Str.full_split r s in
6169 let f n k m =
6170 let g s =
6171 let m1 = modifier_of_string s in
6172 if m1 = 0
6173 then (Wsi.namekey s, m)
6174 else (k, m lor m1)
6175 in function
6176 | Str.Delim s when n land 1 = 0 -> g s
6177 | Str.Text s -> g s
6178 | Str.Delim _ -> (k, m)
6180 let rec loop n k m = function
6181 | [] -> (k, m)
6182 | x :: xs ->
6183 let k, m = f n k m x in
6184 loop (n+1) k m xs
6186 loop 0 0 0 elems
6189 let keys_of_string =
6190 let r = Str.regexp "[ \t]" in
6191 fun s ->
6192 let elems = Str.split r s in
6193 List.map key_of_string elems
6196 let copykeyhashes c =
6197 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6200 let config_of c attrs =
6201 let apply c k v =
6203 match k with
6204 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6205 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6206 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6207 | "preload" -> { c with preload = bool_of_string v }
6208 | "page-bias" -> { c with pagebias = int_of_string v }
6209 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6210 | "horizontal-scroll-step" ->
6211 { c with hscrollstep = max (int_of_string v) 1 }
6212 | "auto-scroll-step" ->
6213 { c with autoscrollstep = max 0 (int_of_string v) }
6214 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6215 | "crop-hack" -> { c with crophack = bool_of_string v }
6216 | "throttle" ->
6217 let mw =
6218 match String.lowercase v with
6219 | "true" -> Some infinity
6220 | "false" -> None
6221 | f -> Some (float_of_string f)
6223 { c with maxwait = mw}
6224 | "highlight-links" -> { c with hlinks = bool_of_string v }
6225 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6226 | "vertical-margin" ->
6227 { c with interpagespace = max 0 (int_of_string v) }
6228 | "zoom" ->
6229 let zoom = float_of_string v /. 100. in
6230 let zoom = max zoom 0.0 in
6231 { c with zoom = zoom }
6232 | "presentation" -> { c with presentation = bool_of_string v }
6233 | "rotation-angle" -> { c with angle = int_of_string v }
6234 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6235 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6236 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6237 | "proportional-display" -> { c with proportional = bool_of_string v }
6238 | "pixmap-cache-size" ->
6239 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6240 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6241 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6242 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6243 | "persistent-location" -> { c with jumpback = bool_of_string v }
6244 | "background-color" -> { c with bgcolor = color_of_string v }
6245 | "scrollbar-in-presentation" ->
6246 { c with scrollbarinpm = bool_of_string v }
6247 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6248 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6249 | "mupdf-store-size" ->
6250 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6251 | "checkers" -> { c with checkers = bool_of_string v }
6252 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6253 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6254 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6255 | "uri-launcher" -> { c with urilauncher = unent v }
6256 | "path-launcher" -> { c with pathlauncher = unent v }
6257 | "color-space" -> { c with colorspace = colorspace_of_string v }
6258 | "invert-colors" -> { c with invert = bool_of_string v }
6259 | "brightness" -> { c with colorscale = float_of_string v }
6260 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6261 | "ghyllscroll" ->
6262 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6263 | "columns" ->
6264 let (n, _, _) as nab = multicolumns_of_string v in
6265 if n < 0
6266 then { c with columns = Csplit (-n, [||]) }
6267 else { c with columns = Cmulti (nab, [||]) }
6268 | "birds-eye-columns" ->
6269 { c with beyecolumns = Some (max (int_of_string v) 2) }
6270 | "selection-command" -> { c with selcmd = unent v }
6271 | "synctex-command" -> { c with stcmd = unent v }
6272 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6273 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6274 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6275 | "use-pbo" -> { c with usepbo = bool_of_string v }
6276 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6277 | _ -> c
6278 with exn ->
6279 prerr_endline ("Error processing attribute (`" ^
6280 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6283 let rec fold c = function
6284 | [] -> c
6285 | (k, v) :: rest ->
6286 let c = apply c k v in
6287 fold c rest
6289 fold { c with keyhashes = copykeyhashes c } attrs;
6292 let fromstring f pos n v d =
6293 try f v
6294 with exn ->
6295 dolog "Error processing attribute (%S=%S) at %d\n%s"
6296 n v pos (exntos exn)
6301 let bookmark_of attrs =
6302 let rec fold title page rely visy = function
6303 | ("title", v) :: rest -> fold v page rely visy rest
6304 | ("page", v) :: rest -> fold title v rely visy rest
6305 | ("rely", v) :: rest -> fold title page v visy rest
6306 | ("visy", v) :: rest -> fold title page rely v rest
6307 | _ :: rest -> fold title page rely visy rest
6308 | [] -> title, page, rely, visy
6310 fold "invalid" "0" "0" "0" attrs
6313 let doc_of attrs =
6314 let rec fold path page rely pan visy = function
6315 | ("path", v) :: rest -> fold v page rely pan visy rest
6316 | ("page", v) :: rest -> fold path v rely pan visy rest
6317 | ("rely", v) :: rest -> fold path page v pan visy rest
6318 | ("pan", v) :: rest -> fold path page rely v visy rest
6319 | ("visy", v) :: rest -> fold path page rely pan v rest
6320 | _ :: rest -> fold path page rely pan visy rest
6321 | [] -> path, page, rely, pan, visy
6323 fold "" "0" "0" "0" "0" attrs
6326 let map_of attrs =
6327 let rec fold rs ls = function
6328 | ("out", v) :: rest -> fold v ls rest
6329 | ("in", v) :: rest -> fold rs v rest
6330 | _ :: rest -> fold ls rs rest
6331 | [] -> ls, rs
6333 fold "" "" attrs
6336 let setconf dst src =
6337 dst.scrollbw <- src.scrollbw;
6338 dst.scrollh <- src.scrollh;
6339 dst.icase <- src.icase;
6340 dst.preload <- src.preload;
6341 dst.pagebias <- src.pagebias;
6342 dst.verbose <- src.verbose;
6343 dst.scrollstep <- src.scrollstep;
6344 dst.maxhfit <- src.maxhfit;
6345 dst.crophack <- src.crophack;
6346 dst.autoscrollstep <- src.autoscrollstep;
6347 dst.maxwait <- src.maxwait;
6348 dst.hlinks <- src.hlinks;
6349 dst.underinfo <- src.underinfo;
6350 dst.interpagespace <- src.interpagespace;
6351 dst.zoom <- src.zoom;
6352 dst.presentation <- src.presentation;
6353 dst.angle <- src.angle;
6354 dst.cwinw <- src.cwinw;
6355 dst.cwinh <- src.cwinh;
6356 dst.savebmarks <- src.savebmarks;
6357 dst.memlimit <- src.memlimit;
6358 dst.proportional <- src.proportional;
6359 dst.texcount <- src.texcount;
6360 dst.sliceheight <- src.sliceheight;
6361 dst.thumbw <- src.thumbw;
6362 dst.jumpback <- src.jumpback;
6363 dst.bgcolor <- src.bgcolor;
6364 dst.scrollbarinpm <- src.scrollbarinpm;
6365 dst.tilew <- src.tilew;
6366 dst.tileh <- src.tileh;
6367 dst.mustoresize <- src.mustoresize;
6368 dst.checkers <- src.checkers;
6369 dst.aalevel <- src.aalevel;
6370 dst.trimmargins <- src.trimmargins;
6371 dst.trimfuzz <- src.trimfuzz;
6372 dst.urilauncher <- src.urilauncher;
6373 dst.colorspace <- src.colorspace;
6374 dst.invert <- src.invert;
6375 dst.colorscale <- src.colorscale;
6376 dst.redirectstderr <- src.redirectstderr;
6377 dst.ghyllscroll <- src.ghyllscroll;
6378 dst.columns <- src.columns;
6379 dst.beyecolumns <- src.beyecolumns;
6380 dst.selcmd <- src.selcmd;
6381 dst.updatecurs <- src.updatecurs;
6382 dst.pathlauncher <- src.pathlauncher;
6383 dst.keyhashes <- copykeyhashes src;
6384 dst.hfsize <- src.hfsize;
6385 dst.hscrollstep <- src.hscrollstep;
6386 dst.pgscale <- src.pgscale;
6387 dst.usepbo <- src.usepbo;
6388 dst.wheelbypage <- src.wheelbypage;
6389 dst.stcmd <- src.stcmd;
6392 let get s =
6393 let h = Hashtbl.create 10 in
6394 let dc = { defconf with angle = defconf.angle } in
6395 let rec toplevel v t spos _ =
6396 match t with
6397 | Vdata | Vcdata | Vend -> v
6398 | Vopen ("llppconfig", _, closed) ->
6399 if closed
6400 then v
6401 else { v with f = llppconfig }
6402 | Vopen _ ->
6403 error "unexpected subelement at top level" s spos
6404 | Vclose _ -> error "unexpected close at top level" s spos
6406 and llppconfig v t spos _ =
6407 match t with
6408 | Vdata | Vcdata -> v
6409 | Vend -> error "unexpected end of input in llppconfig" s spos
6410 | Vopen ("defaults", attrs, closed) ->
6411 let c = config_of dc attrs in
6412 setconf dc c;
6413 if closed
6414 then v
6415 else { v with f = defaults }
6417 | Vopen ("ui-font", attrs, closed) ->
6418 let rec getsize size = function
6419 | [] -> size
6420 | ("size", v) :: rest ->
6421 let size =
6422 fromstring int_of_string spos "size" v fstate.fontsize in
6423 getsize size rest
6424 | l -> getsize size l
6426 fstate.fontsize <- getsize fstate.fontsize attrs;
6427 if closed
6428 then v
6429 else { v with f = uifont (Buffer.create 10) }
6431 | Vopen ("doc", attrs, closed) ->
6432 let pathent, spage, srely, span, svisy = doc_of attrs in
6433 let path = unent pathent
6434 and pageno = fromstring int_of_string spos "page" spage 0
6435 and rely = fromstring float_of_string spos "rely" srely 0.0
6436 and pan = fromstring int_of_string spos "pan" span 0
6437 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6438 let c = config_of dc attrs in
6439 let anchor = (pageno, rely, visy) in
6440 if closed
6441 then (Hashtbl.add h path (c, [], pan, anchor); v)
6442 else { v with f = doc path pan anchor c [] }
6444 | Vopen _ ->
6445 error "unexpected subelement in llppconfig" s spos
6447 | Vclose "llppconfig" -> { v with f = toplevel }
6448 | Vclose _ -> error "unexpected close in llppconfig" s spos
6450 and defaults v t spos _ =
6451 match t with
6452 | Vdata | Vcdata -> v
6453 | Vend -> error "unexpected end of input in defaults" s spos
6454 | Vopen ("keymap", attrs, closed) ->
6455 let modename =
6456 try List.assoc "mode" attrs
6457 with Not_found -> "global" in
6458 if closed
6459 then v
6460 else
6461 let ret keymap =
6462 let h = findkeyhash dc modename in
6463 KeyMap.iter (Hashtbl.replace h) keymap;
6464 defaults
6466 { v with f = pkeymap ret KeyMap.empty }
6468 | Vopen (_, _, _) ->
6469 error "unexpected subelement in defaults" s spos
6471 | Vclose "defaults" ->
6472 { v with f = llppconfig }
6474 | Vclose _ -> error "unexpected close in defaults" s spos
6476 and uifont b v t spos epos =
6477 match t with
6478 | Vdata | Vcdata ->
6479 Buffer.add_substring b s spos (epos - spos);
6481 | Vopen (_, _, _) ->
6482 error "unexpected subelement in ui-font" s spos
6483 | Vclose "ui-font" ->
6484 if String.length !fontpath = 0
6485 then fontpath := Buffer.contents b;
6486 { v with f = llppconfig }
6487 | Vclose _ -> error "unexpected close in ui-font" s spos
6488 | Vend -> error "unexpected end of input in ui-font" s spos
6490 and doc path pan anchor c bookmarks v t spos _ =
6491 match t with
6492 | Vdata | Vcdata -> v
6493 | Vend -> error "unexpected end of input in doc" s spos
6494 | Vopen ("bookmarks", _, closed) ->
6495 if closed
6496 then v
6497 else { v with f = pbookmarks path pan anchor c bookmarks }
6499 | Vopen ("keymap", attrs, closed) ->
6500 let modename =
6501 try List.assoc "mode" attrs
6502 with Not_found -> "global"
6504 if closed
6505 then v
6506 else
6507 let ret keymap =
6508 let h = findkeyhash c modename in
6509 KeyMap.iter (Hashtbl.replace h) keymap;
6510 doc path pan anchor c bookmarks
6512 { v with f = pkeymap ret KeyMap.empty }
6514 | Vopen (_, _, _) ->
6515 error "unexpected subelement in doc" s spos
6517 | Vclose "doc" ->
6518 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6519 { v with f = llppconfig }
6521 | Vclose _ -> error "unexpected close in doc" s spos
6523 and pkeymap ret keymap v t spos _ =
6524 match t with
6525 | Vdata | Vcdata -> v
6526 | Vend -> error "unexpected end of input in keymap" s spos
6527 | Vopen ("map", attrs, closed) ->
6528 let r, l = map_of attrs in
6529 let kss = fromstring keys_of_string spos "in" r [] in
6530 let lss = fromstring keys_of_string spos "out" l [] in
6531 let keymap =
6532 match kss with
6533 | [] -> keymap
6534 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6535 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6537 if closed
6538 then { v with f = pkeymap ret keymap }
6539 else
6540 let f () = v in
6541 { v with f = skip "map" f }
6543 | Vopen _ ->
6544 error "unexpected subelement in keymap" s spos
6546 | Vclose "keymap" ->
6547 { v with f = ret keymap }
6549 | Vclose _ -> error "unexpected close in keymap" s spos
6551 and pbookmarks path pan anchor c bookmarks v t spos _ =
6552 match t with
6553 | Vdata | Vcdata -> v
6554 | Vend -> error "unexpected end of input in bookmarks" s spos
6555 | Vopen ("item", attrs, closed) ->
6556 let titleent, spage, srely, svisy = bookmark_of attrs in
6557 let page = fromstring int_of_string spos "page" spage 0
6558 and rely = fromstring float_of_string spos "rely" srely 0.0
6559 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6560 let bookmarks =
6561 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6563 if closed
6564 then { v with f = pbookmarks path pan anchor c bookmarks }
6565 else
6566 let f () = v in
6567 { v with f = skip "item" f }
6569 | Vopen _ ->
6570 error "unexpected subelement in bookmarks" s spos
6572 | Vclose "bookmarks" ->
6573 { v with f = doc path pan anchor c bookmarks }
6575 | Vclose _ -> error "unexpected close in bookmarks" s spos
6577 and skip tag f v t spos _ =
6578 match t with
6579 | Vdata | Vcdata -> v
6580 | Vend ->
6581 error ("unexpected end of input in skipped " ^ tag) s spos
6582 | Vopen (tag', _, closed) ->
6583 if closed
6584 then v
6585 else
6586 let f' () = { v with f = skip tag f } in
6587 { v with f = skip tag' f' }
6588 | Vclose ctag ->
6589 if tag = ctag
6590 then f ()
6591 else error ("unexpected close in skipped " ^ tag) s spos
6594 parse { f = toplevel; accu = () } s;
6595 h, dc;
6598 let do_load f ic =
6600 let len = in_channel_length ic in
6601 let s = String.create len in
6602 really_input ic s 0 len;
6603 f s;
6604 with
6605 | Parse_error (msg, s, pos) ->
6606 let subs = subs s pos in
6607 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6608 failwith ("parse error: " ^ s)
6610 | exn ->
6611 failwith ("config load error: " ^ exntos exn)
6614 let defconfpath =
6615 let dir =
6617 let dir = Filename.concat home ".config" in
6618 if Sys.is_directory dir then dir else home
6619 with _ -> home
6621 Filename.concat dir "llpp.conf"
6624 let confpath = ref defconfpath;;
6626 let load1 f =
6627 if Sys.file_exists !confpath
6628 then
6629 match
6630 (try Some (open_in_bin !confpath)
6631 with exn ->
6632 prerr_endline
6633 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6634 exntos exn);
6635 None
6637 with
6638 | Some ic ->
6639 let success =
6641 f (do_load get ic)
6642 with exn ->
6643 prerr_endline
6644 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6645 exntos exn);
6646 false
6648 close_in ic;
6649 success
6651 | None -> false
6652 else
6653 f (Hashtbl.create 0, defconf)
6656 let load () =
6657 let f (h, dc) =
6658 let pc, pb, px, pa =
6660 Hashtbl.find h (Filename.basename state.path)
6661 with Not_found -> dc, [], 0, emptyanchor
6663 setconf defconf dc;
6664 setconf conf pc;
6665 state.bookmarks <- pb;
6666 state.x <- px;
6667 state.scrollw <- conf.scrollbw;
6668 if conf.jumpback
6669 then state.anchor <- pa;
6670 cbput state.hists.nav pa;
6671 true
6673 load1 f
6676 let add_attrs bb always dc c =
6677 let ob s a b =
6678 if always || a != b
6679 then Printf.bprintf bb "\n %s='%b'" s a
6680 and oi s a b =
6681 if always || a != b
6682 then Printf.bprintf bb "\n %s='%d'" s a
6683 and oI s a b =
6684 if always || a != b
6685 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6686 and oz s a b =
6687 if always || a <> b
6688 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6689 and oF s a b =
6690 if always || a <> b
6691 then Printf.bprintf bb "\n %s='%f'" s a
6692 and oc s a b =
6693 if always || a <> b
6694 then
6695 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6696 and oC s a b =
6697 if always || a <> b
6698 then
6699 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6700 and oR s a b =
6701 if always || a <> b
6702 then
6703 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6704 and os s a b =
6705 if always || a <> b
6706 then
6707 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6708 and og s a b =
6709 if always || a <> b
6710 then
6711 match a with
6712 | None -> ()
6713 | Some (_N, _A, _B) ->
6714 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6715 and oW s a b =
6716 if always || a <> b
6717 then
6718 let v =
6719 match a with
6720 | None -> "false"
6721 | Some f ->
6722 if f = infinity
6723 then "true"
6724 else string_of_float f
6726 Printf.bprintf bb "\n %s='%s'" s v
6727 and oco s a b =
6728 if always || a <> b
6729 then
6730 match a with
6731 | Cmulti ((n, a, b), _) when n > 1 ->
6732 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6733 | Csplit (n, _) when n > 1 ->
6734 Printf.bprintf bb "\n %s='%d'" s ~-n
6735 | _ -> ()
6736 and obeco s a b =
6737 if always || a <> b
6738 then
6739 match a with
6740 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6741 | _ -> ()
6743 oi "width" c.cwinw dc.cwinw;
6744 oi "height" c.cwinh dc.cwinh;
6745 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6746 oi "scroll-handle-height" c.scrollh dc.scrollh;
6747 ob "case-insensitive-search" c.icase dc.icase;
6748 ob "preload" c.preload dc.preload;
6749 oi "page-bias" c.pagebias dc.pagebias;
6750 oi "scroll-step" c.scrollstep dc.scrollstep;
6751 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6752 ob "max-height-fit" c.maxhfit dc.maxhfit;
6753 ob "crop-hack" c.crophack dc.crophack;
6754 oW "throttle" c.maxwait dc.maxwait;
6755 ob "highlight-links" c.hlinks dc.hlinks;
6756 ob "under-cursor-info" c.underinfo dc.underinfo;
6757 oi "vertical-margin" c.interpagespace dc.interpagespace;
6758 oz "zoom" c.zoom dc.zoom;
6759 ob "presentation" c.presentation dc.presentation;
6760 oi "rotation-angle" c.angle dc.angle;
6761 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6762 ob "proportional-display" c.proportional dc.proportional;
6763 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6764 oi "tex-count" c.texcount dc.texcount;
6765 oi "slice-height" c.sliceheight dc.sliceheight;
6766 oi "thumbnail-width" c.thumbw dc.thumbw;
6767 ob "persistent-location" c.jumpback dc.jumpback;
6768 oc "background-color" c.bgcolor dc.bgcolor;
6769 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6770 oi "tile-width" c.tilew dc.tilew;
6771 oi "tile-height" c.tileh dc.tileh;
6772 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6773 ob "checkers" c.checkers dc.checkers;
6774 oi "aalevel" c.aalevel dc.aalevel;
6775 ob "trim-margins" c.trimmargins dc.trimmargins;
6776 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6777 os "uri-launcher" c.urilauncher dc.urilauncher;
6778 os "path-launcher" c.pathlauncher dc.pathlauncher;
6779 oC "color-space" c.colorspace dc.colorspace;
6780 ob "invert-colors" c.invert dc.invert;
6781 oF "brightness" c.colorscale dc.colorscale;
6782 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6783 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6784 oco "columns" c.columns dc.columns;
6785 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6786 os "selection-command" c.selcmd dc.selcmd;
6787 os "synctex-command" c.stcmd dc.stcmd;
6788 ob "update-cursor" c.updatecurs dc.updatecurs;
6789 oi "hint-font-size" c.hfsize dc.hfsize;
6790 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6791 oF "page-scroll-scale" c.pgscale dc.pgscale;
6792 ob "use-pbo" c.usepbo dc.usepbo;
6793 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6796 let keymapsbuf always dc c =
6797 let bb = Buffer.create 16 in
6798 let rec loop = function
6799 | [] -> ()
6800 | (modename, h) :: rest ->
6801 let dh = findkeyhash dc modename in
6802 if always || h <> dh
6803 then (
6804 if Hashtbl.length h > 0
6805 then (
6806 if Buffer.length bb > 0
6807 then Buffer.add_char bb '\n';
6808 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6809 Hashtbl.iter (fun i o ->
6810 let isdifferent = always ||
6812 let dO = Hashtbl.find dh i in
6813 dO <> o
6814 with Not_found -> true
6816 if isdifferent
6817 then
6818 let addkm (k, m) =
6819 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6820 if Wsi.withalt m then Buffer.add_string bb "alt-";
6821 if Wsi.withshift m then Buffer.add_string bb "shift-";
6822 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6823 Buffer.add_string bb (Wsi.keyname k);
6825 let addkms l =
6826 let rec loop = function
6827 | [] -> ()
6828 | km :: [] -> addkm km
6829 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6831 loop l
6833 Buffer.add_string bb "<map in='";
6834 addkm i;
6835 match o with
6836 | KMinsrt km ->
6837 Buffer.add_string bb "' out='";
6838 addkm km;
6839 Buffer.add_string bb "'/>\n"
6841 | KMinsrl kms ->
6842 Buffer.add_string bb "' out='";
6843 addkms kms;
6844 Buffer.add_string bb "'/>\n"
6846 | KMmulti (ins, kms) ->
6847 Buffer.add_char bb ' ';
6848 addkms ins;
6849 Buffer.add_string bb "' out='";
6850 addkms kms;
6851 Buffer.add_string bb "'/>\n"
6852 ) h;
6853 Buffer.add_string bb "</keymap>";
6856 loop rest
6858 loop c.keyhashes;
6862 let save () =
6863 let uifontsize = fstate.fontsize in
6864 let bb = Buffer.create 32768 in
6865 let w, h =
6866 List.fold_left
6867 (fun (w, h) ws ->
6868 match ws with
6869 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6870 | Wsi.MaxVert -> (w, conf.cwinh)
6871 | Wsi.MaxHorz -> (conf.cwinw, h)
6873 (state.winw, state.winh) state.winstate
6875 conf.cwinw <- w;
6876 conf.cwinh <- h;
6877 let f (h, dc) =
6878 let dc = if conf.bedefault then conf else dc in
6879 Buffer.add_string bb "<llppconfig>\n";
6881 if String.length !fontpath > 0
6882 then
6883 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6884 uifontsize
6885 !fontpath
6886 else (
6887 if uifontsize <> 14
6888 then
6889 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6892 Buffer.add_string bb "<defaults ";
6893 add_attrs bb true dc dc;
6894 let kb = keymapsbuf true dc dc in
6895 if Buffer.length kb > 0
6896 then (
6897 Buffer.add_string bb ">\n";
6898 Buffer.add_buffer bb kb;
6899 Buffer.add_string bb "\n</defaults>\n";
6901 else Buffer.add_string bb "/>\n";
6903 let adddoc path pan anchor c bookmarks =
6904 if bookmarks == [] && c = dc && anchor = emptyanchor
6905 then ()
6906 else (
6907 Printf.bprintf bb "<doc path='%s'"
6908 (enent path 0 (String.length path));
6910 if anchor <> emptyanchor
6911 then (
6912 let n, rely, visy = anchor in
6913 Printf.bprintf bb " page='%d'" n;
6914 if rely > 1e-6
6915 then
6916 Printf.bprintf bb " rely='%f'" rely
6918 if abs_float visy > 1e-6
6919 then
6920 Printf.bprintf bb " visy='%f'" visy
6924 if pan != 0
6925 then Printf.bprintf bb " pan='%d'" pan;
6927 add_attrs bb false dc c;
6928 let kb = keymapsbuf false dc c in
6930 begin match bookmarks with
6931 | [] ->
6932 if Buffer.length kb > 0
6933 then (
6934 Buffer.add_string bb ">\n";
6935 Buffer.add_buffer bb kb;
6936 Buffer.add_string bb "\n</doc>\n";
6938 else Buffer.add_string bb "/>\n"
6939 | _ ->
6940 Buffer.add_string bb ">\n<bookmarks>\n";
6941 List.iter (fun (title, _level, (page, rely, visy)) ->
6942 Printf.bprintf bb
6943 "<item title='%s' page='%d'"
6944 (enent title 0 (String.length title))
6945 page
6947 if rely > 1e-6
6948 then
6949 Printf.bprintf bb " rely='%f'" rely
6951 if abs_float visy > 1e-6
6952 then
6953 Printf.bprintf bb " visy='%f'" visy
6955 Buffer.add_string bb "/>\n";
6956 ) bookmarks;
6957 Buffer.add_string bb "</bookmarks>";
6958 if Buffer.length kb > 0
6959 then (
6960 Buffer.add_string bb "\n";
6961 Buffer.add_buffer bb kb;
6963 Buffer.add_string bb "\n</doc>\n";
6964 end;
6968 let pan, conf =
6969 match state.mode with
6970 | Birdseye (c, pan, _, _, _) ->
6971 let beyecolumns =
6972 match conf.columns with
6973 | Cmulti ((c, _, _), _) -> Some c
6974 | Csingle _ -> None
6975 | Csplit _ -> None
6976 and columns =
6977 match c.columns with
6978 | Cmulti (c, _) -> Cmulti (c, [||])
6979 | Csingle _ -> Csingle [||]
6980 | Csplit _ -> failwith "quit from bird's eye while split"
6982 pan, { c with beyecolumns = beyecolumns; columns = columns }
6983 | _ -> state.x, conf
6985 let basename = Filename.basename state.path in
6986 adddoc basename pan (getanchor ())
6987 (let conf =
6988 let autoscrollstep =
6989 match state.autoscroll with
6990 | Some step -> step
6991 | None -> conf.autoscrollstep
6993 match state.mode with
6994 | Birdseye (bc, _, _, _, _) ->
6995 { conf with
6996 zoom = bc.zoom;
6997 presentation = bc.presentation;
6998 interpagespace = bc.interpagespace;
6999 maxwait = bc.maxwait;
7000 autoscrollstep = autoscrollstep }
7001 | _ -> { conf with autoscrollstep = autoscrollstep }
7002 in conf)
7003 (if conf.savebmarks then state.bookmarks else []);
7005 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7006 if basename <> path
7007 then adddoc path x anchor c bookmarks
7008 ) h;
7009 Buffer.add_string bb "</llppconfig>\n";
7010 true;
7012 if load1 f && Buffer.length bb > 0
7013 then
7015 let tmp = !confpath ^ ".tmp" in
7016 let oc = open_out_bin tmp in
7017 Buffer.output_buffer oc bb;
7018 close_out oc;
7019 Unix.rename tmp !confpath;
7020 with exn ->
7021 prerr_endline
7022 ("error while saving configuration: " ^ exntos exn)
7024 end;;
7026 let adderrmsg src msg =
7027 Buffer.add_string state.errmsgs msg;
7028 state.newerrmsgs <- true;
7029 G.postRedisplay src
7032 let adderrfmt src fmt =
7033 Format.kprintf (fun s -> adderrmsg src s) fmt;
7036 let ract cmds =
7037 let cl = splitatspace cmds in
7038 let scan s fmt f =
7039 try Scanf.sscanf s fmt f
7040 with exn ->
7041 adderrfmt "remote exec"
7042 "error processing '%S': %s\n" cmds (exntos exn)
7044 match cl with
7045 | "reload" :: [] -> reload ()
7046 | "goto" :: args :: [] ->
7047 scan args "%u %f %f"
7048 (fun pageno x y ->
7049 let cmd, _ = state.geomcmds in
7050 if String.length cmd = 0
7051 then gotopagexy pageno x y
7052 else
7053 let f prevf () =
7054 gotopagexy pageno x y;
7055 prevf ()
7057 state.reprf <- f state.reprf
7059 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7060 | "rect" :: args :: [] ->
7061 scan args "%u %u %f %f %f %f"
7062 (fun pageno color x0 y0 x1 y1 ->
7063 onpagerect pageno (fun w h ->
7064 let _,w1,h1,_ = getpagedim pageno in
7065 let sw = float w1 /. w
7066 and sh = float h1 /. h in
7067 let x0s = x0 *. sw
7068 and x1s = x1 *. sw
7069 and y0s = y0 *. sh
7070 and y1s = y1 *. sh in
7071 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7072 debugrect rect;
7073 state.rects <- (pageno, color, rect) :: state.rects;
7074 G.postRedisplay "rect";
7077 | "activatewin" :: [] -> Wsi.activatewin ()
7078 | "quit" :: [] -> raise Quit
7079 | _ ->
7080 adderrfmt "remote command"
7081 "error processing remote command: %S\n" cmds;
7084 let remote =
7085 let scratch = String.create 80 in
7086 let buf = Buffer.create 80 in
7087 fun fd ->
7088 let rec tempfr () =
7089 try Some (Unix.read fd scratch 0 80)
7090 with
7091 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7092 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7093 | exn -> raise exn
7095 match tempfr () with
7096 | None -> Some fd
7097 | Some n ->
7098 if n = 0
7099 then (
7100 Unix.close fd;
7101 if Buffer.length buf > 0
7102 then (
7103 let s = Buffer.contents buf in
7104 Buffer.clear buf;
7105 ract s;
7107 None
7109 else
7110 let rec eat ppos =
7111 let nlpos =
7113 let pos = String.index_from scratch ppos '\n' in
7114 if pos >= n then -1 else pos
7115 with Not_found -> -1
7117 if nlpos >= 0
7118 then (
7119 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7120 let s = Buffer.contents buf in
7121 Buffer.clear buf;
7122 ract s;
7123 eat (nlpos+1);
7125 else (
7126 Buffer.add_substring buf scratch ppos (n-ppos);
7127 Some fd
7129 in eat 0
7132 let remoteopen path =
7133 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7134 with exn ->
7135 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7136 None
7139 let () =
7140 let trimcachepath = ref "" in
7141 let rcmdpath = ref "" in
7142 Arg.parse
7143 (Arg.align
7144 [("-p", Arg.String (fun s -> state.password <- s),
7145 "<password> Set password");
7147 ("-f", Arg.String (fun s -> Config.fontpath := s),
7148 "<path> Set path to the user interface font");
7150 ("-c", Arg.String (fun s -> Config.confpath := s),
7151 "<path> Set path to the configuration file");
7153 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7154 "<path> Set path to the trim cache file");
7156 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7157 "<named-destination> Set named destination");
7159 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7161 ("-remote", Arg.String (fun s -> rcmdpath := s),
7162 "<path> Set path to the remote commands source");
7164 ("-v", Arg.Unit (fun () ->
7165 Printf.printf
7166 "%s\nconfiguration path: %s\n"
7167 (version ())
7168 Config.defconfpath
7170 exit 0), " Print version and exit");
7173 (fun s -> state.path <- s)
7174 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7176 if String.length state.path = 0
7177 then (prerr_endline "file name missing"; exit 1);
7179 if not (Config.load ())
7180 then prerr_endline "failed to load configuration";
7182 let globalkeyhash = findkeyhash conf "global" in
7183 let wsfd, winw, winh = Wsi.init (object
7184 method expose =
7185 state.wthack <- false;
7186 if nogeomcmds state.geomcmds || platform == Posx
7187 then display ()
7188 else (
7189 GlClear.color (scalecolor2 conf.bgcolor);
7190 GlClear.clear [`color];
7192 method display = display ()
7193 method reshape w h = reshape w h
7194 method mouse b d x y m = mouse b d x y m
7195 method motion x y = state.mpos <- (x, y); motion x y
7196 method pmotion x y = state.mpos <- (x, y); pmotion x y
7197 method key k m =
7198 let mascm = m land (
7199 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7200 ) in
7201 match state.keystate with
7202 | KSnone ->
7203 let km = k, mascm in
7204 begin
7205 match
7206 let modehash = state.uioh#modehash in
7207 try Hashtbl.find modehash km
7208 with Not_found ->
7209 try Hashtbl.find globalkeyhash km
7210 with Not_found -> KMinsrt (k, m)
7211 with
7212 | KMinsrt (k, m) -> keyboard k m
7213 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7214 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7216 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7217 List.iter (fun (k, m) -> keyboard k m) insrt;
7218 state.keystate <- KSnone
7219 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7220 state.keystate <- KSinto (keys, insrt)
7221 | _ ->
7222 state.keystate <- KSnone
7224 method enter x y = state.mpos <- (x, y); pmotion x y
7225 method leave = state.mpos <- (-1, -1)
7226 method winstate wsl = state.winstate <- wsl
7227 method quit = raise Quit
7228 end) conf.cwinw conf.cwinh (platform = Posx) in
7230 state.wsfd <- wsfd;
7232 if not (
7233 List.exists GlMisc.check_extension
7234 [ "GL_ARB_texture_rectangle"
7235 ; "GL_EXT_texture_recangle"
7236 ; "GL_NV_texture_rectangle" ]
7238 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7240 let cr, sw =
7241 match Ne.pipe () with
7242 | Ne.Exn exn ->
7243 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7244 exit 1
7245 | Ne.Res rw -> rw
7246 and sr, cw =
7247 match Ne.pipe () with
7248 | Ne.Exn exn ->
7249 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7250 exit 1
7251 | Ne.Res rw -> rw
7254 cloexec cr;
7255 cloexec sw;
7256 cloexec sr;
7257 cloexec cw;
7259 setcheckers conf.checkers;
7260 redirectstderr ();
7262 init (cr, cw) (
7263 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7264 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7265 !Config.fontpath, !trimcachepath,
7266 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7268 state.sr <- sr;
7269 state.sw <- sw;
7270 state.text <- "Opening " ^ (mbtoutf8 state.path);
7271 reshape winw winh;
7272 opendoc state.path state.password;
7273 state.uioh <- uioh;
7275 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7276 let optrfd =
7277 ref (
7278 if String.length !rcmdpath > 0
7279 then remoteopen !rcmdpath
7280 else None
7284 let rec loop deadline =
7285 let r =
7286 match state.errfd with
7287 | None -> [state.sr; state.wsfd]
7288 | Some fd -> [state.sr; state.wsfd; fd]
7290 let r =
7291 match !optrfd with
7292 | None -> r
7293 | Some fd -> fd :: r
7295 if state.redisplay && not state.wthack
7296 then (
7297 state.redisplay <- false;
7298 display ();
7300 let timeout =
7301 let now = now () in
7302 if deadline > now
7303 then (
7304 if deadline = infinity
7305 then ~-.1.0
7306 else max 0.0 (deadline -. now)
7308 else 0.0
7310 let r, _, _ =
7311 try Unix.select r [] [] timeout
7312 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7314 begin match r with
7315 | [] ->
7316 state.ghyll None;
7317 let newdeadline =
7318 if state.ghyll == noghyll
7319 then
7320 match state.autoscroll with
7321 | Some step when step != 0 ->
7322 let y = state.y + step in
7323 let y =
7324 if y < 0
7325 then state.maxy
7326 else if y >= state.maxy then 0 else y
7328 gotoy y;
7329 if state.mode = View
7330 then state.text <- "";
7331 deadline +. 0.01
7332 | _ -> infinity
7333 else deadline +. 0.01
7335 loop newdeadline
7337 | l ->
7338 let rec checkfds = function
7339 | [] -> ()
7340 | fd :: rest when fd = state.sr ->
7341 let cmd = readcmd state.sr in
7342 act cmd;
7343 checkfds rest
7345 | fd :: rest when fd = state.wsfd ->
7346 Wsi.readresp fd;
7347 checkfds rest
7349 | fd :: rest when Some fd = !optrfd ->
7350 begin match remote fd with
7351 | None -> optrfd := remoteopen !rcmdpath;
7352 | opt -> optrfd := opt
7353 end;
7354 checkfds rest
7356 | fd :: rest ->
7357 let s = String.create 80 in
7358 let n = tempfailureretry (Unix.read fd s 0) 80 in
7359 if conf.redirectstderr
7360 then (
7361 Buffer.add_substring state.errmsgs s 0 n;
7362 state.newerrmsgs <- true;
7363 state.redisplay <- true;
7365 else (
7366 prerr_string (String.sub s 0 n);
7367 flush stderr;
7369 checkfds rest
7371 checkfds l;
7372 let newdeadline =
7373 let deadline1 =
7374 if deadline = infinity
7375 then now () +. 0.01
7376 else deadline
7378 match state.autoscroll with
7379 | Some step when step != 0 -> deadline1
7380 | _ -> if state.ghyll == noghyll then infinity else deadline1
7382 loop newdeadline
7383 end;
7386 loop infinity;
7387 with Quit ->
7388 Config.save ();