1337c440f6c713511898bf962788d3aa1f08dfdf
[llpp.git] / main.ml
blob1337c440f6c713511898bf962788d3aa1f08dfdf
1 exception Quit;;
3 let tempfailureretry = Wsi.tempfailureretry;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 let dolog fmt = Printf.kprintf prerr_endline fmt;;
17 let now = Unix.gettimeofday;;
19 type params = (angle * proportional * trimparams
20 * texcount * sliceheight * memsize
21 * colorspace * fontpath * trimcachepath
22 * haspbo)
23 and pageno = int
24 and width = int
25 and height = int
26 and leftx = int
27 and opaque = string
28 and recttype = int
29 and pixmapsize = int
30 and angle = int
31 and proportional = bool
32 and trimmargins = bool
33 and interpagespace = int
34 and texcount = int
35 and sliceheight = int
36 and gen = int
37 and top = float
38 and dtop = float
39 and fontpath = string
40 and trimcachepath = string
41 and memsize = int
42 and aalevel = int
43 and irect = (int * int * int * int)
44 and trimparams = (trimmargins * irect)
45 and colorspace = | Rgb | Bgr | Gray
46 and haspbo = bool
49 type link =
50 | Lnotfound
51 | Lfound of int
52 and linkdir =
53 | LDfirst
54 | LDlast
55 | LDfirstvisible of (int * int * int)
56 | LDleft of int
57 | LDright of int
58 | LDdown of int
59 | LDup of int
62 type pagewithlinks =
63 | Pwlnotfound
64 | Pwl of int
67 type keymap =
68 | KMinsrt of key
69 | KMinsrl of key list
70 | KMmulti of key list * key list
71 and key = int * int
72 and keyhash = (key, keymap) Hashtbl.t
73 and keystate =
74 | KSnone
75 | KSinto of (key list * key list)
78 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
79 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
81 type pipe = (Unix.file_descr * Unix.file_descr);;
83 external init : pipe -> params -> unit = "ml_init";;
84 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
85 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
86 external getpdimrect : int -> float array = "ml_getpdimrect";;
87 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
88 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
89 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
90 external measurestr : int -> string -> float = "ml_measure_string";;
91 external getmaxw : unit -> float = "ml_getmaxw";;
92 external postprocess :
93 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
94 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
95 external platform : unit -> platform = "ml_platform";;
96 external setaalevel : int -> unit = "ml_setaalevel";;
97 external realloctexts : int -> bool = "ml_realloctexts";;
98 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
99 external findlink : opaque -> linkdir -> link = "ml_findlink";;
100 external getlink : opaque -> int -> under = "ml_getlink";;
101 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
102 external getlinkcount : opaque -> int = "ml_getlinkcount";;
103 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
104 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
105 external mbtoutf8 : string -> string = "ml_mbtoutf8";;
106 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
107 external freepbo : string -> unit = "ml_freepbo";;
108 external unmappbo : string -> unit = "ml_unmappbo";;
109 external pbousable : unit -> bool = "ml_pbo_usable";;
111 let platform_to_string = function
112 | Punknown -> "unknown"
113 | Plinux -> "Linux"
114 | Posx -> "OSX"
115 | Psun -> "Sun"
116 | Pfreebsd -> "FreeBSD"
117 | Pdragonflybsd -> "DragonflyBSD"
118 | Popenbsd -> "OpenBSD"
119 | Pnetbsd -> "NetBSD"
120 | Pcygwin -> "Cygwin"
123 let platform = platform ();;
125 let popen cmd fda =
126 if platform = Pcygwin
127 then (
128 let sh = "/bin/sh" in
129 let args = [|sh; "-c"; cmd|] in
130 let rec std si so se = function
131 | [] -> si, so, se
132 | (fd, 0) :: rest -> std fd so se rest
133 | (fd, -1) :: rest ->
134 Unix.set_close_on_exec fd;
135 std si so se rest
136 | (_, n) :: _ ->
137 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
139 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
140 ignore (Unix.create_process sh args si so se)
142 else popen cmd fda;
145 type x = int
146 and y = int
147 and tilex = int
148 and tiley = int
149 and tileparams = (x * y * width * height * tilex * tiley)
152 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
154 type mpos = int * int
155 and mstate =
156 | Msel of (mpos * mpos)
157 | Mpan of mpos
158 | Mscrolly | Mscrollx
159 | Mzoom of (int * int)
160 | Mzoomrect of (mpos * mpos)
161 | Mnone
164 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
165 and onkey = string -> int -> te
166 and ondone = string -> unit
167 and histcancel = unit -> unit
168 and onhist = ((histcmd -> string) * histcancel)
169 and histcmd = HCnext | HCprev | HCfirst | HClast
170 and cancelonempty = bool
171 and te =
172 | TEstop
173 | TEdone of string
174 | TEcont of string
175 | TEswitch of textentry
178 type 'a circbuf =
179 { store : 'a array
180 ; mutable rc : int
181 ; mutable wc : int
182 ; mutable len : int
186 let bound v minv maxv =
187 max minv (min maxv v);
190 let cbnew n v =
191 { store = Array.create n v
192 ; rc = 0
193 ; wc = 0
194 ; len = 0
198 let cbcap b = Array.length b.store;;
200 let cbput b v =
201 let cap = cbcap b in
202 b.store.(b.wc) <- v;
203 b.wc <- (b.wc + 1) mod cap;
204 b.rc <- b.wc;
205 b.len <- min (b.len + 1) cap;
208 let cbempty b = b.len = 0;;
210 let cbgetg b circular dir =
211 if cbempty b
212 then b.store.(0)
213 else
214 let rc = b.rc + dir in
215 let rc =
216 if circular
217 then (
218 if rc = -1
219 then b.len-1
220 else (
221 if rc >= b.len
222 then 0
223 else rc
226 else bound rc 0 (b.len-1)
228 b.rc <- rc;
229 b.store.(rc);
232 let cbget b = cbgetg b false;;
233 let cbgetc b = cbgetg b true;;
235 let drawstring size x y s =
236 Gl.enable `blend;
237 Gl.enable `texture_2d;
238 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
239 ignore (drawstr size x y s);
240 Gl.disable `blend;
241 Gl.disable `texture_2d;
244 let drawstring1 size x y s =
245 drawstr size x y s;
248 let drawstring2 size x y fmt =
249 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
252 type page =
253 { pageno : int
254 ; pagedimno : int
255 ; pagew : int
256 ; pageh : int
257 ; pagex : int
258 ; pagey : int
259 ; pagevw : int
260 ; pagevh : int
261 ; pagedispx : int
262 ; pagedispy : int
263 ; pagecol : int
267 let debugl l =
268 dolog "l %d dim=%d {" l.pageno l.pagedimno;
269 dolog " WxH %dx%d" l.pagew l.pageh;
270 dolog " vWxH %dx%d" l.pagevw l.pagevh;
271 dolog " pagex,y %d,%d" l.pagex l.pagey;
272 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
273 dolog " column %d" l.pagecol;
274 dolog "}";
277 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
278 dolog "rect {";
279 dolog " x0,y0=(% f, % f)" x0 y0;
280 dolog " x1,y1=(% f, % f)" x1 y1;
281 dolog " x2,y2=(% f, % f)" x2 y2;
282 dolog " x3,y3=(% f, % f)" x3 y3;
283 dolog "}";
286 type multicolumns = multicol * pagegeom
287 and singlecolumn = pagegeom
288 and splitcolumns = columncount * pagegeom
289 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
290 and multicol = columncount * covercount * covercount
291 and pdimno = int
292 and columncount = int
293 and covercount = int;;
295 type conf =
296 { mutable scrollbw : int
297 ; mutable scrollh : int
298 ; mutable icase : bool
299 ; mutable preload : bool
300 ; mutable pagebias : int
301 ; mutable verbose : bool
302 ; mutable debug : bool
303 ; mutable scrollstep : int
304 ; mutable hscrollstep : int
305 ; mutable maxhfit : bool
306 ; mutable crophack : bool
307 ; mutable autoscrollstep : int
308 ; mutable maxwait : float option
309 ; mutable hlinks : bool
310 ; mutable underinfo : bool
311 ; mutable interpagespace : interpagespace
312 ; mutable zoom : float
313 ; mutable presentation : bool
314 ; mutable angle : angle
315 ; mutable winw : int
316 ; mutable winh : int
317 ; mutable savebmarks : bool
318 ; mutable proportional : proportional
319 ; mutable trimmargins : trimmargins
320 ; mutable trimfuzz : irect
321 ; mutable memlimit : memsize
322 ; mutable texcount : texcount
323 ; mutable sliceheight : sliceheight
324 ; mutable thumbw : width
325 ; mutable jumpback : bool
326 ; mutable bgcolor : float * float * float
327 ; mutable bedefault : bool
328 ; mutable scrollbarinpm : bool
329 ; mutable tilew : int
330 ; mutable tileh : int
331 ; mutable mustoresize : memsize
332 ; mutable checkers : bool
333 ; mutable aalevel : int
334 ; mutable urilauncher : string
335 ; mutable pathlauncher : string
336 ; mutable colorspace : colorspace
337 ; mutable invert : bool
338 ; mutable colorscale : float
339 ; mutable redirectstderr : bool
340 ; mutable ghyllscroll : (int * int * int) option
341 ; mutable columns : columns
342 ; mutable beyecolumns : columncount option
343 ; mutable selcmd : string
344 ; mutable updatecurs : bool
345 ; mutable keyhashes : (string * keyhash) list
346 ; mutable hfsize : int
347 ; mutable pgscale : float
348 ; mutable usepbo : bool
349 ; mutable wheelbypage : bool
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 fullscreen : (width * height) option
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
482 and hists =
483 { pat : string circbuf
484 ; pag : string circbuf
485 ; nav : anchor circbuf
486 ; sel : string circbuf
490 let defconf =
491 { scrollbw = 7
492 ; scrollh = 12
493 ; icase = true
494 ; preload = true
495 ; pagebias = 0
496 ; verbose = false
497 ; debug = false
498 ; scrollstep = 24
499 ; hscrollstep = 24
500 ; maxhfit = true
501 ; crophack = false
502 ; autoscrollstep = 2
503 ; maxwait = None
504 ; hlinks = false
505 ; underinfo = false
506 ; interpagespace = 2
507 ; zoom = 1.0
508 ; presentation = false
509 ; angle = 0
510 ; winw = 900
511 ; winh = 900
512 ; savebmarks = true
513 ; proportional = true
514 ; trimmargins = false
515 ; trimfuzz = (0,0,0,0)
516 ; memlimit = 32 lsl 20
517 ; texcount = 256
518 ; sliceheight = 24
519 ; thumbw = 76
520 ; jumpback = true
521 ; bgcolor = (0.5, 0.5, 0.5)
522 ; bedefault = false
523 ; scrollbarinpm = true
524 ; tilew = 2048
525 ; tileh = 2048
526 ; mustoresize = 256 lsl 20
527 ; checkers = true
528 ; aalevel = 8
529 ; urilauncher =
530 (match platform with
531 | Plinux | Pfreebsd | Pdragonflybsd
532 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
533 | Posx -> "open \"%s\""
534 | Pcygwin -> "cygstart \"%s\""
535 | Punknown -> "echo %s")
536 ; pathlauncher = "lp \"%s\""
537 ; selcmd =
538 (match platform with
539 | Plinux | Pfreebsd | Pdragonflybsd
540 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
541 | Posx -> "pbcopy"
542 | Pcygwin -> "wsel"
543 | Punknown -> "cat")
544 ; colorspace = Rgb
545 ; invert = false
546 ; colorscale = 1.0
547 ; redirectstderr = false
548 ; ghyllscroll = None
549 ; columns = Csingle [||]
550 ; beyecolumns = None
551 ; updatecurs = false
552 ; hfsize = 12
553 ; pgscale = 1.0
554 ; usepbo = false
555 ; wheelbypage = false
556 ; keyhashes =
557 let mk n = (n, Hashtbl.create 1) in
558 [ mk "global"
559 ; mk "info"
560 ; mk "help"
561 ; mk "outline"
562 ; mk "listview"
563 ; mk "birdseye"
564 ; mk "textentry"
565 ; mk "links"
566 ; mk "view"
571 let findkeyhash c name =
572 try List.assoc name c.keyhashes
573 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
576 let conf = { defconf with angle = defconf.angle };;
578 let pgscale h = truncate (float h *. conf.pgscale);;
580 type fontstate =
581 { mutable fontsize : int
582 ; mutable wwidth : float
583 ; mutable maxrows : int
587 let fstate =
588 { fontsize = 14
589 ; wwidth = nan
590 ; maxrows = -1
594 let setfontsize n =
595 fstate.fontsize <- n;
596 fstate.wwidth <- measurestr fstate.fontsize "w";
597 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 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 (Printexc.to_string 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 = "", [];;
664 let state =
665 { sr = Unix.stdin
666 ; sw = Unix.stdin
667 ; wsfd = Unix.stdin
668 ; errfd = None
669 ; stderr = Unix.stderr
670 ; errmsgs = Buffer.create 0
671 ; newerrmsgs = false
672 ; x = 0
673 ; y = 0
674 ; w = 0
675 ; scrollw = 0
676 ; hscrollh = 0
677 ; anchor = emptyanchor
678 ; ranchors = []
679 ; layout = []
680 ; maxy = max_int
681 ; tilelru = Queue.create ()
682 ; pagemap = Hashtbl.create 10
683 ; tilemap = Hashtbl.create 10
684 ; pdims = []
685 ; pagecount = 0
686 ; currently = Idle
687 ; mstate = Mnone
688 ; rects = []
689 ; rects1 = []
690 ; text = ""
691 ; mode = View
692 ; fullscreen = None
693 ; searchpattern = ""
694 ; outlines = [||]
695 ; bookmarks = []
696 ; path = ""
697 ; password = ""
698 ; nameddest = ""
699 ; geomcmds = firstgeomcmds
700 ; hists =
701 { nav = cbnew 10 emptyanchor
702 ; pat = cbnew 10 ""
703 ; pag = cbnew 10 ""
704 ; sel = cbnew 10 ""
706 ; memused = 0
707 ; gen = 0
708 ; throttle = None
709 ; autoscroll = None
710 ; ghyll = noghyll
711 ; help = makehelp ()
712 ; docinfo = []
713 ; texid = None
714 ; prevzoom = 1.0
715 ; progress = -1.0
716 ; uioh = nouioh
717 ; redisplay = true
718 ; mpos = (-1, -1)
719 ; keystate = KSnone
720 ; glinks = false
721 ; prevcolumns = None
722 ; wthack = false
726 let vlog fmt =
727 if conf.verbose
728 then
729 Printf.kprintf prerr_endline fmt
730 else
731 Printf.kprintf ignore fmt
734 let launchpath () =
735 if String.length conf.pathlauncher = 0
736 then print_endline state.path
737 else (
738 let re = Str.regexp "%s" in
739 let command = Str.global_replace re state.path conf.pathlauncher in
740 try popen command []
741 with exn ->
742 Printf.eprintf
743 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
744 flush stderr;
748 module Ne = struct
749 type 'a t = | Res of 'a | Exn of exn;;
751 let pipe () =
752 try Res (Unix.pipe ())
753 with exn -> Exn exn
756 let clo fd f =
757 try tempfailureretry Unix.close fd
758 with exn -> f (Printexc.to_string exn)
761 let dup fd =
762 try Res (tempfailureretry Unix.dup fd)
763 with exn -> Exn exn
766 let dup2 fd1 fd2 =
767 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
768 with exn -> Exn exn
770 end;;
772 let redirectstderr () =
773 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
774 if conf.redirectstderr
775 then
776 match Ne.pipe () with
777 | Ne.Exn exn ->
778 dolog "failed to create stderr redirection pipes: %s"
779 (Printexc.to_string exn)
781 | Ne.Res (r, w) ->
782 begin match Ne.dup Unix.stderr with
783 | Ne.Exn exn ->
784 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
785 Ne.clo r (clofail "pipe/r");
786 Ne.clo w (clofail "pipe/w");
788 | Ne.Res dupstderr ->
789 begin match Ne.dup2 w Unix.stderr with
790 | Ne.Exn exn ->
791 dolog "failed to dup2 to stderr: %s"
792 (Printexc.to_string exn);
793 Ne.clo dupstderr (clofail "stderr duplicate");
794 Ne.clo r (clofail "redir pipe/r");
795 Ne.clo w (clofail "redir pipe/w");
797 | Ne.Res () ->
798 state.stderr <- dupstderr;
799 state.errfd <- Some r;
800 end;
802 else (
803 state.newerrmsgs <- false;
804 begin match state.errfd with
805 | Some fd ->
806 begin match Ne.dup2 state.stderr Unix.stderr with
807 | Ne.Exn exn ->
808 dolog "failed to dup2 original stderr: %s"
809 (Printexc.to_string exn)
810 | Ne.Res () ->
811 Ne.clo fd (clofail "dup of stderr");
812 state.errfd <- None;
813 end;
814 | None -> ()
815 end;
816 prerr_string (Buffer.contents state.errmsgs);
817 flush stderr;
818 Buffer.clear state.errmsgs;
822 module G =
823 struct
824 let postRedisplay who =
825 if conf.verbose
826 then prerr_endline ("redisplay for " ^ who);
827 state.redisplay <- true;
829 end;;
831 let getopaque pageno =
832 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
833 with Not_found -> None
836 let putopaque pageno opaque =
837 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
840 let pagetranslatepoint l x y =
841 let dy = y - l.pagedispy in
842 let y = dy + l.pagey in
843 let dx = x - l.pagedispx in
844 let x = dx + l.pagex in
845 (x, y);
848 let getunder x y =
849 let rec f = function
850 | l :: rest ->
851 begin match getopaque l.pageno with
852 | Some opaque ->
853 let x0 = l.pagedispx in
854 let x1 = x0 + l.pagevw in
855 let y0 = l.pagedispy in
856 let y1 = y0 + l.pagevh in
857 if y >= y0 && y <= y1 && x >= x0 && x <= x1
858 then
859 let px, py = pagetranslatepoint l x y in
860 match whatsunder opaque px py with
861 | Unone -> f rest
862 | under -> under
863 else f rest
864 | _ ->
865 f rest
867 | [] -> Unone
869 f state.layout
872 let showtext c s =
873 state.text <- Printf.sprintf "%c%s" c s;
874 G.postRedisplay "showtext";
877 let undertext = function
878 | Unone -> "none"
879 | Ulinkuri s -> s
880 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
881 | Utext s -> "font: " ^ s
882 | Uunexpected s -> "unexpected: " ^ s
883 | Ulaunch s -> "launch: " ^ s
884 | Unamed s -> "named: " ^ s
885 | Uremote (filename, pageno) ->
886 Printf.sprintf "%s: page %d" filename (pageno+1)
889 let updateunder x y =
890 match getunder x y with
891 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
892 | Ulinkuri uri ->
893 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
894 Wsi.setcursor Wsi.CURSOR_INFO
895 | Ulinkgoto (pageno, _) ->
896 if conf.underinfo
897 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
898 Wsi.setcursor Wsi.CURSOR_INFO
899 | Utext s ->
900 if conf.underinfo then showtext 'f' ("ont: " ^ s);
901 Wsi.setcursor Wsi.CURSOR_TEXT
902 | Uunexpected s ->
903 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
904 Wsi.setcursor Wsi.CURSOR_INHERIT
905 | Ulaunch s ->
906 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
907 Wsi.setcursor Wsi.CURSOR_INHERIT
908 | Unamed s ->
909 if conf.underinfo then showtext 'n' ("amed: " ^ s);
910 Wsi.setcursor Wsi.CURSOR_INHERIT
911 | Uremote (filename, pageno) ->
912 if conf.underinfo then showtext 'r'
913 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
914 Wsi.setcursor Wsi.CURSOR_INFO
917 let showlinktype under =
918 if conf.underinfo
919 then
920 match under with
921 | Unone -> ()
922 | under ->
923 let s = undertext under in
924 showtext ' ' s
927 let addchar s c =
928 let b = Buffer.create (String.length s + 1) in
929 Buffer.add_string b s;
930 Buffer.add_char b c;
931 Buffer.contents b;
934 let colorspace_of_string s =
935 match String.lowercase s with
936 | "rgb" -> Rgb
937 | "bgr" -> Bgr
938 | "gray" -> Gray
939 | _ -> failwith "invalid colorspace"
942 let int_of_colorspace = function
943 | Rgb -> 0
944 | Bgr -> 1
945 | Gray -> 2
948 let colorspace_of_int = function
949 | 0 -> Rgb
950 | 1 -> Bgr
951 | 2 -> Gray
952 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
955 let colorspace_to_string = function
956 | Rgb -> "rgb"
957 | Bgr -> "bgr"
958 | Gray -> "gray"
961 let intentry_with_suffix text key =
962 let c =
963 if key >= 32 && key < 127
964 then Char.chr key
965 else '\000'
967 match Char.lowercase c with
968 | '0' .. '9' ->
969 let text = addchar text c in
970 TEcont text
972 | 'k' | 'm' | 'g' ->
973 let text = addchar text c in
974 TEcont text
976 | _ ->
977 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
978 TEcont text
981 let multicolumns_to_string (n, a, b) =
982 if a = 0 && b = 0
983 then Printf.sprintf "%d" n
984 else Printf.sprintf "%d,%d,%d" n a b;
987 let multicolumns_of_string s =
989 (int_of_string s, 0, 0)
990 with _ ->
991 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
992 if a > 1 || b > 1
993 then failwith "subtly broken"; (n, a, b)
997 let readcmd fd =
998 let s = "xxxx" in
999 let n = tempfailureretry (Unix.read fd s 0) 4 in
1000 if n != 4 then failwith "incomplete read(len)";
1001 let len = 0
1002 lor (Char.code s.[0] lsl 24)
1003 lor (Char.code s.[1] lsl 16)
1004 lor (Char.code s.[2] lsl 8)
1005 lor (Char.code s.[3] lsl 0)
1007 let s = String.create len in
1008 let n = tempfailureretry (Unix.read fd s 0) len in
1009 if n != len then failwith "incomplete read(data)";
1013 let btod b = if b then 1 else 0;;
1015 let wcmd fmt =
1016 let b = Buffer.create 16 in
1017 Buffer.add_string b "llll";
1018 Printf.kbprintf
1019 (fun b ->
1020 let s = Buffer.contents b in
1021 let n = String.length s in
1022 let len = n - 4 in
1023 (* dolog "wcmd %S" (String.sub s 4 len); *)
1024 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1025 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1026 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1027 s.[3] <- Char.chr (len land 0xff);
1028 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1029 if n' != n then failwith "write failed";
1030 ) b fmt;
1033 let calcips h =
1034 let d = conf.winh - h in
1035 max conf.interpagespace ((d + 1) / 2)
1038 let rowyh (c, coverA, coverB) b n =
1039 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1040 then
1041 let _, _, vy, (_, _, h, _) = b.(n) in
1042 (vy, h)
1043 else
1044 let n' = n - coverA in
1045 let d = n' mod c in
1046 let s = n - d in
1047 let e = min state.pagecount (s + c) in
1048 let rec find m miny maxh = if m = e then miny, maxh else
1049 let _, _, y, (_, _, h, _) = b.(m) in
1050 let miny = min miny y in
1051 let maxh = max maxh h in
1052 find (m+1) miny maxh
1053 in find s max_int 0
1056 let calcheight () =
1057 match conf.columns with
1058 | Cmulti ((_, _, _) as cl, b) ->
1059 if Array.length b > 0
1060 then
1061 let y, h = rowyh cl b (Array.length b - 1) in
1062 y + h + (if conf.presentation then calcips h else 0)
1063 else 0
1064 | Csingle b ->
1065 if Array.length b > 0
1066 then
1067 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1068 y + h + (if conf.presentation then calcips h else 0)
1069 else 0
1070 | Csplit (_, b) ->
1071 if Array.length b > 0
1072 then
1073 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1074 y + h
1075 else 0
1078 let getpageyh pageno =
1079 let pageno = bound pageno 0 (state.pagecount-1) in
1080 match conf.columns with
1081 | Csingle b ->
1082 if Array.length b = 0
1083 then 0, 0
1084 else
1085 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1086 let y =
1087 if conf.presentation
1088 then y - calcips h
1089 else y
1091 y, h
1092 | Cmulti (cl, b) ->
1093 if Array.length b = 0
1094 then 0, 0
1095 else
1096 let y, h = rowyh cl b pageno in
1097 let y =
1098 if conf.presentation
1099 then y - calcips h
1100 else y
1102 y, h
1103 | Csplit (c, b) ->
1104 if Array.length b = 0
1105 then 0, 0
1106 else
1107 let n = pageno*c in
1108 let (_, _, y, (_, _, h, _)) = b.(n) in
1109 y, h
1112 let getpagedim pageno =
1113 let rec f ppdim l =
1114 match l with
1115 | (n, _, _, _) as pdim :: rest ->
1116 if n >= pageno
1117 then (if n = pageno then pdim else ppdim)
1118 else f pdim rest
1120 | [] -> ppdim
1122 f (-1, -1, -1, -1) state.pdims
1125 let getpagey pageno = fst (getpageyh pageno);;
1127 let nogeomcmds cmds =
1128 match cmds with
1129 | s, [] -> String.length s = 0
1130 | _ -> false
1133 let page_of_y y =
1134 let ((c, coverA, coverB) as cl), b =
1135 match conf.columns with
1136 | Csingle b -> (1, 0, 0), b
1137 | Cmulti (c, b) -> c, b
1138 | Csplit (_, b) -> (1, 0, 0), b
1140 let rec bsearch nmin nmax =
1141 if nmin > nmax
1142 then bound nmin 0 (state.pagecount-1)
1143 else
1144 let n = (nmax + nmin) / 2 in
1145 let vy, h = rowyh cl b n in
1146 let y0, y1 =
1147 if conf.presentation
1148 then
1149 let ips = calcips h in
1150 let y0 = vy - ips in
1151 let y1 = vy + h + ips in
1152 y0, y1
1153 else (
1154 if n = 0
1155 then 0, vy + h + conf.interpagespace
1156 else
1157 let y0 = vy - conf.interpagespace in
1158 y0, y0 + h + conf.interpagespace
1161 if y >= y0 && y < y1
1162 then (
1163 if c = 1
1164 then n
1165 else (
1166 if n > coverA
1167 then
1168 if n < state.pagecount - coverB
1169 then ((n-coverA)/c)*c + coverA
1170 else n
1171 else n
1174 else (
1175 if y > y0
1176 then bsearch (n+1) nmax
1177 else bsearch nmin (n-1)
1180 let r = bsearch 0 (state.pagecount-1) in
1184 let layoutN ((columns, coverA, coverB), b) y sh =
1185 let sh = sh - state.hscrollh in
1186 let rec fold accu n =
1187 if n = Array.length b
1188 then accu
1189 else
1190 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1191 if (vy - y) > sh &&
1192 (n = coverA - 1
1193 || n = state.pagecount - coverB
1194 || (n - coverA) mod columns = columns - 1)
1195 then accu
1196 else
1197 let accu =
1198 if vy + h > y
1199 then
1200 let pagey = max 0 (y - vy) in
1201 let pagedispy = if pagey > 0 then 0 else vy - y in
1202 let pagedispx, pagex =
1203 let pdx =
1204 if n = coverA - 1 || n = state.pagecount - coverB
1205 then state.x + (conf.winw - state.scrollw - w) / 2
1206 else dx + xoff + state.x
1208 if pdx < 0
1209 then 0, -pdx
1210 else pdx, 0
1212 let pagevw =
1213 let vw = conf.winw - state.scrollw - pagedispx in
1214 let pw = w - pagex in
1215 min vw pw
1217 let pagevh = min (h - pagey) (sh - pagedispy) in
1218 if pagevw > 0 && pagevh > 0
1219 then
1220 let e =
1221 { pageno = n
1222 ; pagedimno = pdimno
1223 ; pagew = w
1224 ; pageh = h
1225 ; pagex = pagex
1226 ; pagey = pagey
1227 ; pagevw = pagevw
1228 ; pagevh = pagevh
1229 ; pagedispx = pagedispx
1230 ; pagedispy = pagedispy
1231 ; pagecol = 0
1234 e :: accu
1235 else
1236 accu
1237 else
1238 accu
1240 fold accu (n+1)
1242 List.rev (fold [] (page_of_y y));
1245 let layoutS (columns, b) y sh =
1246 let sh = sh - state.hscrollh in
1247 let rec fold accu n =
1248 if n = Array.length b
1249 then accu
1250 else
1251 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1252 if (vy - y) > sh
1253 then accu
1254 else
1255 let accu =
1256 if vy + pageh > y
1257 then
1258 let x = xoff + state.x in
1259 let pagey = max 0 (y - vy) in
1260 let pagedispy = if pagey > 0 then 0 else vy - y in
1261 let pagedispx, pagex =
1262 if px = 0
1263 then (
1264 if x < 0
1265 then 0, -x
1266 else x, 0
1268 else (
1269 let px = px - x in
1270 if px < 0
1271 then -px, 0
1272 else 0, px
1275 let pagecolw = pagew/columns in
1276 let pagedispx =
1277 if pagecolw < conf.winw
1278 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1279 else pagedispx
1281 let pagevw =
1282 let vw = conf.winw - pagedispx - state.scrollw in
1283 let pw = pagew - pagex in
1284 min vw pw
1286 let pagevw = min pagevw pagecolw in
1287 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1288 if pagevw > 0 && pagevh > 0
1289 then
1290 let e =
1291 { pageno = n/columns
1292 ; pagedimno = pdimno
1293 ; pagew = pagew
1294 ; pageh = pageh
1295 ; pagex = pagex
1296 ; pagey = pagey
1297 ; pagevw = pagevw
1298 ; pagevh = pagevh
1299 ; pagedispx = pagedispx
1300 ; pagedispy = pagedispy
1301 ; pagecol = n mod columns
1304 e :: accu
1305 else
1306 accu
1307 else
1308 accu
1310 fold accu (n+1)
1312 List.rev (fold [] 0)
1315 let layout y sh =
1316 if nogeomcmds state.geomcmds
1317 then
1318 match conf.columns with
1319 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1320 | Cmulti c -> layoutN c y sh
1321 | Csplit s -> layoutS s y sh
1322 else []
1325 let clamp incr =
1326 let y = state.y + incr in
1327 let y = max 0 y in
1328 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1332 let itertiles l f =
1333 let tilex = l.pagex mod conf.tilew in
1334 let tiley = l.pagey mod conf.tileh in
1336 let col = l.pagex / conf.tilew in
1337 let row = l.pagey / conf.tileh in
1339 let rec rowloop row y0 dispy h =
1340 if h = 0
1341 then ()
1342 else (
1343 let dh = conf.tileh - y0 in
1344 let dh = min h dh in
1345 let rec colloop col x0 dispx w =
1346 if w = 0
1347 then ()
1348 else (
1349 let dw = conf.tilew - x0 in
1350 let dw = min w dw in
1352 f col row dispx dispy x0 y0 dw dh;
1353 colloop (col+1) 0 (dispx+dw) (w-dw)
1356 colloop col tilex l.pagedispx l.pagevw;
1357 rowloop (row+1) 0 (dispy+dh) (h-dh)
1360 if l.pagevw > 0 && l.pagevh > 0
1361 then rowloop row tiley l.pagedispy l.pagevh;
1364 let gettileopaque l col row =
1365 let key =
1366 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1368 try Some (Hashtbl.find state.tilemap key)
1369 with Not_found -> None
1372 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1373 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1374 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1377 let drawtiles l color =
1378 GlDraw.color color;
1379 let f col row x y tilex tiley w h =
1380 match gettileopaque l col row with
1381 | Some (opaque, _, t) ->
1382 let params = x, y, w, h, tilex, tiley in
1383 if conf.invert
1384 then (
1385 Gl.enable `blend;
1386 GlFunc.blend_func `zero `one_minus_src_color;
1388 drawtile params opaque;
1389 if conf.invert
1390 then Gl.disable `blend;
1391 if conf.debug
1392 then (
1393 let s = Printf.sprintf
1394 "%d[%d,%d] %f sec"
1395 l.pageno col row t
1397 let w = measurestr fstate.fontsize s in
1398 GlMisc.push_attrib [`current];
1399 GlDraw.color (0.0, 0.0, 0.0);
1400 GlDraw.rect
1401 (float (x-2), float (y-2))
1402 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1403 GlDraw.color (1.0, 1.0, 1.0);
1404 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1405 GlMisc.pop_attrib ();
1408 | _ ->
1409 let w =
1410 let lw = conf.winw - state.scrollw - x in
1411 min lw w
1412 and h =
1413 let lh = conf.winh - y in
1414 min lh h
1416 begin match state.texid with
1417 | Some id ->
1418 Gl.enable `texture_2d;
1419 GlTex.bind_texture `texture_2d id;
1420 let x0 = float x
1421 and y0 = float y
1422 and x1 = float (x+w)
1423 and y1 = float (y+h) in
1425 let tw = float w /. 16.0
1426 and th = float h /. 16.0 in
1427 let tx0 = float tilex /. 16.0
1428 and ty0 = float tiley /. 16.0 in
1429 let tx1 = tx0 +. tw
1430 and ty1 = ty0 +. th in
1431 GlDraw.begins `quads;
1432 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1433 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1434 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1435 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1436 GlDraw.ends ();
1438 Gl.disable `texture_2d;
1439 | None ->
1440 GlDraw.color (1.0, 1.0, 1.0);
1441 GlDraw.rect
1442 (float x, float y)
1443 (float (x+w), float (y+h));
1444 end;
1445 if w > 128 && h > fstate.fontsize + 10
1446 then (
1447 GlDraw.color (0.0, 0.0, 0.0);
1448 let c, r =
1449 if conf.verbose
1450 then (col*conf.tilew, row*conf.tileh)
1451 else col, row
1453 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1455 GlDraw.color color;
1457 itertiles l f
1460 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1462 let tilevisible1 l x y =
1463 let ax0 = l.pagex
1464 and ax1 = l.pagex + l.pagevw
1465 and ay0 = l.pagey
1466 and ay1 = l.pagey + l.pagevh in
1468 let bx0 = x
1469 and by0 = y in
1470 let bx1 = min (bx0 + conf.tilew) l.pagew
1471 and by1 = min (by0 + conf.tileh) l.pageh in
1473 let rx0 = max ax0 bx0
1474 and ry0 = max ay0 by0
1475 and rx1 = min ax1 bx1
1476 and ry1 = min ay1 by1 in
1478 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1479 nonemptyintersection
1482 let tilevisible layout n x y =
1483 let rec findpageinlayout m = function
1484 | l :: rest when l.pageno = n ->
1485 tilevisible1 l x y || (
1486 match conf.columns with
1487 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1488 | _ -> false
1490 | _ :: rest -> findpageinlayout 0 rest
1491 | [] -> false
1493 findpageinlayout 0 layout;
1496 let tileready l x y =
1497 tilevisible1 l x y &&
1498 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1501 let tilepage n p layout =
1502 let rec loop = function
1503 | l :: rest ->
1504 if l.pageno = n
1505 then
1506 let f col row _ _ _ _ _ _ =
1507 if state.currently = Idle
1508 then
1509 match gettileopaque l col row with
1510 | Some _ -> ()
1511 | None ->
1512 let x = col*conf.tilew
1513 and y = row*conf.tileh in
1514 let w =
1515 let w = l.pagew - x in
1516 min w conf.tilew
1518 let h =
1519 let h = l.pageh - y in
1520 min h conf.tileh
1522 let pbo =
1523 if conf.usepbo
1524 then getpbo w h conf.colorspace
1525 else "0"
1527 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1528 state.currently <-
1529 Tiling (
1530 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1531 conf.tilew, conf.tileh
1534 itertiles l f;
1535 else
1536 loop rest
1538 | [] -> ()
1540 if nogeomcmds state.geomcmds
1541 then loop layout;
1544 let preloadlayout y =
1545 let y = if y < conf.winh then 0 else y - conf.winh in
1546 let h = conf.winh*3 in
1547 layout y h;
1550 let load pages =
1551 let rec loop pages =
1552 if state.currently != Idle
1553 then ()
1554 else
1555 match pages with
1556 | l :: rest ->
1557 begin match getopaque l.pageno with
1558 | None ->
1559 wcmd "page %d %d" l.pageno l.pagedimno;
1560 state.currently <- Loading (l, state.gen);
1561 | Some opaque ->
1562 tilepage l.pageno opaque pages;
1563 loop rest
1564 end;
1565 | _ -> ()
1567 if nogeomcmds state.geomcmds
1568 then loop pages
1571 let preload pages =
1572 load pages;
1573 if conf.preload && state.currently = Idle
1574 then load (preloadlayout state.y);
1577 let layoutready layout =
1578 let rec fold all ls =
1579 all && match ls with
1580 | l :: rest ->
1581 let seen = ref false in
1582 let allvisible = ref true in
1583 let foo col row _ _ _ _ _ _ =
1584 seen := true;
1585 allvisible := !allvisible &&
1586 begin match gettileopaque l col row with
1587 | Some _ -> true
1588 | None -> false
1591 itertiles l foo;
1592 fold (!seen && !allvisible) rest
1593 | [] -> true
1595 let alltilesvisible = fold true layout in
1596 alltilesvisible;
1599 let gotoy y =
1600 state.wthack <- false;
1601 let y = bound y 0 state.maxy in
1602 let y, layout, proceed =
1603 match conf.maxwait with
1604 | Some time when state.ghyll == noghyll ->
1605 begin match state.throttle with
1606 | None ->
1607 let layout = layout y conf.winh in
1608 let ready = layoutready layout in
1609 if not ready
1610 then (
1611 load layout;
1612 state.throttle <- Some (layout, y, now ());
1614 else G.postRedisplay "gotoy showall (None)";
1615 y, layout, ready
1616 | Some (_, _, started) ->
1617 let dt = now () -. started in
1618 if dt > time
1619 then (
1620 state.throttle <- None;
1621 let layout = layout y conf.winh in
1622 load layout;
1623 G.postRedisplay "maxwait";
1624 y, layout, true
1626 else -1, [], false
1629 | _ ->
1630 let layout = layout y conf.winh in
1631 if true || layoutready layout
1632 then G.postRedisplay "gotoy ready";
1633 y, layout, true
1635 if proceed
1636 then (
1637 state.y <- y;
1638 state.layout <- layout;
1639 begin match state.mode with
1640 | LinkNav (Ltexact (pageno, linkno)) ->
1641 let rec loop = function
1642 | [] ->
1643 state.mode <- LinkNav (Ltgendir 0)
1644 | l :: _ when l.pageno = pageno ->
1645 begin match getopaque pageno with
1646 | None ->
1647 state.mode <- LinkNav (Ltgendir 0)
1648 | Some opaque ->
1649 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1650 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1651 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1652 then state.mode <- LinkNav (Ltgendir 0)
1654 | _ :: rest -> loop rest
1656 loop layout
1657 | _ -> ()
1658 end;
1659 begin match state.mode with
1660 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1661 if not (pagevisible layout pageno)
1662 then (
1663 match state.layout with
1664 | [] -> ()
1665 | l :: _ ->
1666 state.mode <- Birdseye (
1667 conf, leftx, l.pageno, hooverpageno, anchor
1670 | LinkNav (Ltgendir dir as lt) ->
1671 let linknav =
1672 let rec loop = function
1673 | [] -> lt
1674 | l :: rest ->
1675 match getopaque l.pageno with
1676 | None -> loop rest
1677 | Some opaque ->
1678 let link =
1679 let ld =
1680 if dir = 0
1681 then LDfirstvisible (l.pagex, l.pagey, dir)
1682 else (
1683 if dir > 0 then LDfirst else LDlast
1686 findlink opaque ld
1688 match link with
1689 | Lnotfound -> loop rest
1690 | Lfound n ->
1691 showlinktype (getlink opaque n);
1692 Ltexact (l.pageno, n)
1694 loop state.layout
1696 state.mode <- LinkNav linknav
1697 | _ -> ()
1698 end;
1699 preload layout;
1701 state.ghyll <- noghyll;
1702 if conf.updatecurs
1703 then (
1704 let mx, my = state.mpos in
1705 updateunder mx my;
1709 let conttiling pageno opaque =
1710 tilepage pageno opaque
1711 (if conf.preload then preloadlayout state.y else state.layout)
1714 let gotoy_and_clear_text y =
1715 if not conf.verbose then state.text <- "";
1716 gotoy y;
1719 let getanchor1 l =
1720 let top =
1721 let coloff = l.pagecol * l.pageh in
1722 float (l.pagey + coloff) /. float l.pageh
1724 let dtop =
1725 if l.pagedispy = 0
1726 then
1728 else
1729 if conf.presentation
1730 then float l.pagedispy /. float (calcips l.pageh)
1731 else float l.pagedispy /. float conf.interpagespace
1733 (l.pageno, top, dtop)
1736 let getanchor () =
1737 match state.layout with
1738 | l :: _ -> getanchor1 l
1739 | [] ->
1740 let n = page_of_y state.y in
1741 let y, h = getpageyh n in
1742 let dy = y - state.y in
1743 let dtop =
1744 if conf.presentation
1745 then
1746 let ips = calcips h in
1747 float (dy + ips) /. float ips
1748 else
1749 float dy /. float conf.interpagespace
1751 (n, 0.0, dtop)
1754 let getanchory (n, top, dtop) =
1755 let y, h = getpageyh n in
1756 if conf.presentation
1757 then
1758 let ips = calcips h in
1759 y + truncate (top*.float h -. dtop*.float ips) + ips;
1760 else
1761 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1764 let gotoanchor anchor =
1765 gotoy (getanchory anchor);
1768 let addnav () =
1769 cbput state.hists.nav (getanchor ());
1772 let getnav dir =
1773 let anchor = cbgetc state.hists.nav dir in
1774 getanchory anchor;
1777 let gotoghyll y =
1778 let scroll f n a b =
1779 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1780 let snake f a b =
1781 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1782 if f < a
1783 then s (float f /. float a)
1784 else (
1785 if f > b
1786 then 1.0 -. s ((float (f-b) /. float (n-b)))
1787 else 1.0
1790 snake f a b
1791 and summa f n a b =
1792 (* courtesy:
1793 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1794 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1795 let iv1 = iv f in
1796 let ins = float a *. iv1
1797 and outs = float (n-b) *. iv1 in
1798 let ones = b - a in
1799 ins +. outs +. float ones
1801 let rec set (_N, _A, _B) y sy =
1802 let sum = summa 1.0 _N _A _B in
1803 let dy = float (y - sy) in
1804 state.ghyll <- (
1805 let rec gf n y1 o =
1806 if n >= _N
1807 then state.ghyll <- noghyll
1808 else
1809 let go n =
1810 let s = scroll n _N _A _B in
1811 let y1 = y1 +. ((s *. dy) /. sum) in
1812 gotoy_and_clear_text (truncate y1);
1813 state.ghyll <- gf (n+1) y1;
1815 match o with
1816 | None -> go n
1817 | Some y' -> set (_N/2, 1, 1) y' state.y
1819 gf 0 (float state.y)
1822 match conf.ghyllscroll with
1823 | None ->
1824 gotoy_and_clear_text y
1825 | Some nab ->
1826 if state.ghyll == noghyll
1827 then set nab y state.y
1828 else state.ghyll (Some y)
1831 let gotopage n top =
1832 let y, h = getpageyh n in
1833 let y = y + (truncate (top *. float h)) in
1834 gotoghyll y
1837 let gotopage1 n top =
1838 let y = getpagey n in
1839 let y = y + top in
1840 gotoghyll y
1843 let invalidate s f =
1844 state.layout <- [];
1845 state.pdims <- [];
1846 state.rects <- [];
1847 state.rects1 <- [];
1848 match state.geomcmds with
1849 | ps, [] when String.length ps = 0 ->
1850 f ();
1851 state.geomcmds <- s, [];
1853 | ps, [] ->
1854 state.geomcmds <- ps, [s, f];
1856 | ps, (s', _) :: rest when s' = s ->
1857 state.geomcmds <- ps, ((s, f) :: rest);
1859 | ps, cmds ->
1860 state.geomcmds <- ps, ((s, f) :: cmds);
1863 let flushpages () =
1864 Hashtbl.iter (fun _ opaque ->
1865 wcmd "freepage %s" opaque;
1866 ) state.pagemap;
1867 Hashtbl.clear state.pagemap;
1870 let opendoc path password =
1871 state.path <- path;
1872 state.password <- password;
1873 state.gen <- state.gen + 1;
1874 state.docinfo <- [];
1876 flushpages ();
1877 setaalevel conf.aalevel;
1878 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1879 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1880 invalidate "reqlayout"
1881 (fun () ->
1882 wcmd "reqlayout %d %d %s\000"
1883 conf.angle (btod conf.proportional) state.nameddest;
1887 let reload () =
1888 state.anchor <- getanchor ();
1889 state.wthack <- true;
1890 opendoc state.path state.password;
1893 let scalecolor c =
1894 let c = c *. conf.colorscale in
1895 (c, c, c);
1898 let scalecolor2 (r, g, b) =
1899 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1902 let docolumns = function
1903 | Csingle _ ->
1904 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1905 let rec loop pageno pdimno pdim y ph pdims =
1906 if pageno = state.pagecount
1907 then ()
1908 else
1909 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1910 match pdims with
1911 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1912 pdimno+1, pdim, rest
1913 | _ ->
1914 pdimno, pdim, pdims
1916 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1917 let y = y +
1918 (if conf.presentation
1919 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1920 else (if pageno = 0 then 0 else conf.interpagespace)
1923 a.(pageno) <- (pdimno, x, y, pdim);
1924 loop (pageno+1) pdimno pdim (y + h) h pdims
1926 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1927 conf.columns <- Csingle a;
1929 | Cmulti ((columns, coverA, coverB), _) ->
1930 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1931 let rec loop pageno pdimno pdim x y rowh pdims =
1932 let rec fixrow m = if m = pageno then () else
1933 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1934 if h < rowh
1935 then (
1936 let y = y + (rowh - h) / 2 in
1937 a.(m) <- (pdimno, x, y, pdim);
1939 fixrow (m+1)
1941 if pageno = state.pagecount
1942 then fixrow (((pageno - 1) / columns) * columns)
1943 else
1944 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1945 match pdims with
1946 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1947 pdimno+1, pdim, rest
1948 | _ ->
1949 pdimno, pdim, pdims
1951 let x, y, rowh' =
1952 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1953 then (
1954 let x = (conf.winw - state.scrollw - w) / 2 in
1955 let ips =
1956 if conf.presentation then calcips h else conf.interpagespace in
1957 x, y + ips + rowh, h
1959 else (
1960 if (pageno - coverA) mod columns = 0
1961 then (
1962 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1963 let y =
1964 if conf.presentation
1965 then
1966 let ips = calcips h in
1967 y + (if pageno = 0 then 0 else calcips rowh + ips)
1968 else
1969 y + (if pageno = 0 then 0 else conf.interpagespace)
1971 x, y + rowh, h
1973 else x, y, max rowh h
1976 let y =
1977 if pageno > 1 && (pageno - coverA) mod columns = 0
1978 then (
1979 let y =
1980 if pageno = columns && conf.presentation
1981 then (
1982 let ips = calcips rowh in
1983 for i = 0 to pred columns
1985 let (pdimno, x, y, pdim) = a.(i) in
1986 a.(i) <- (pdimno, x, y+ips, pdim)
1987 done;
1988 y+ips;
1990 else y
1992 fixrow (pageno - columns);
1995 else y
1997 a.(pageno) <- (pdimno, x, y, pdim);
1998 let x = x + w + xoff*2 + conf.interpagespace in
1999 loop (pageno+1) pdimno pdim x y rowh' pdims
2001 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2002 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2004 | Csplit (c, _) ->
2005 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2006 let rec loop pageno pdimno pdim y pdims =
2007 if pageno = state.pagecount
2008 then ()
2009 else
2010 let pdimno, ((_, w, h, _) as pdim), pdims =
2011 match pdims with
2012 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2013 pdimno+1, pdim, rest
2014 | _ ->
2015 pdimno, pdim, pdims
2017 let cw = w / c in
2018 let rec loop1 n x y =
2019 if n = c then y else (
2020 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2021 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2024 let y = loop1 0 0 y in
2025 loop (pageno+1) pdimno pdim y pdims
2027 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2028 conf.columns <- Csplit (c, a);
2031 let represent () =
2032 docolumns conf.columns;
2033 state.maxy <- calcheight ();
2034 state.hscrollh <-
2035 if state.w <= conf.winw - state.scrollw
2036 then 0
2037 else state.scrollw
2039 let wthack = state.wthack in
2040 begin match state.mode with
2041 | Birdseye (_, _, pageno, _, _) ->
2042 let y, h = getpageyh pageno in
2043 let top = (conf.winh - h) / 2 in
2044 gotoy (max 0 (y - top))
2045 | _ -> gotoanchor state.anchor
2046 end;
2047 state.wthack <- wthack;
2050 let reshape w h =
2051 state.wthack <- false;
2052 GlDraw.viewport 0 0 w h;
2053 let firsttime = state.geomcmds == firstgeomcmds in
2054 if not firsttime && nogeomcmds state.geomcmds
2055 then state.anchor <- getanchor ();
2057 conf.winw <- w;
2058 let w = truncate (float w *. conf.zoom) - state.scrollw in
2059 let w = max w 2 in
2060 conf.winh <- h;
2061 setfontsize fstate.fontsize;
2062 GlMat.mode `modelview;
2063 GlMat.load_identity ();
2065 GlMat.mode `projection;
2066 GlMat.load_identity ();
2067 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2068 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2069 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2071 let relx =
2072 if conf.zoom <= 1.0
2073 then 0.0
2074 else float state.x /. float state.w
2076 invalidate "geometry"
2077 (fun () ->
2078 state.w <- w;
2079 if not firsttime
2080 then state.x <- truncate (relx *. float w);
2081 let w =
2082 match conf.columns with
2083 | Csingle _ -> w
2084 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2085 | Csplit (c, _) -> w * c
2087 wcmd "geometry %d %d" w h);
2090 let enttext () =
2091 let len = String.length state.text in
2092 let drawstring s =
2093 let hscrollh =
2094 match state.mode with
2095 | Textentry _
2096 | View ->
2097 let h, _, _ = state.uioh#scrollpw in
2099 | _ -> 0
2101 let rect x w =
2102 GlDraw.rect
2103 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2104 (x+.w, float (conf.winh - hscrollh))
2107 let w = float (conf.winw - state.scrollw - 1) in
2108 if state.progress >= 0.0 && state.progress < 1.0
2109 then (
2110 GlDraw.color (0.3, 0.3, 0.3);
2111 let w1 = w *. state.progress in
2112 rect 0.0 w1;
2113 GlDraw.color (0.0, 0.0, 0.0);
2114 rect w1 (w-.w1)
2116 else (
2117 GlDraw.color (0.0, 0.0, 0.0);
2118 rect 0.0 w;
2121 GlDraw.color (1.0, 1.0, 1.0);
2122 drawstring fstate.fontsize
2123 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2125 let s =
2126 match state.mode with
2127 | Textentry ((prefix, text, _, _, _, _), _) ->
2128 let s =
2129 if len > 0
2130 then
2131 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2132 else
2133 Printf.sprintf "%s%s_" prefix text
2137 | _ -> state.text
2139 let s =
2140 if state.newerrmsgs
2141 then (
2142 if not (istextentry state.mode)
2143 then
2144 let s1 = "(press 'e' to review error messasges)" in
2145 if String.length s > 0 then s ^ " " ^ s1 else s1
2146 else s
2148 else s
2150 if String.length s > 0
2151 then drawstring s
2154 let gctiles () =
2155 let len = Queue.length state.tilelru in
2156 let layout = lazy (
2157 match state.throttle with
2158 | None ->
2159 if conf.preload
2160 then preloadlayout state.y
2161 else state.layout
2162 | Some (layout, _, _) ->
2163 layout
2164 ) in
2165 let rec loop qpos =
2166 if state.memused <= conf.memlimit
2167 then ()
2168 else (
2169 if qpos < len
2170 then
2171 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2172 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2173 let (_, pw, ph, _) = getpagedim n in
2175 gen = state.gen
2176 && colorspace = conf.colorspace
2177 && angle = conf.angle
2178 && pagew = pw
2179 && pageh = ph
2180 && (
2181 let x = col*conf.tilew
2182 and y = row*conf.tileh in
2183 tilevisible (Lazy.force_val layout) n x y
2185 then Queue.push lruitem state.tilelru
2186 else (
2187 freepbo p;
2188 wcmd "freetile %s" p;
2189 state.memused <- state.memused - s;
2190 state.uioh#infochanged Memused;
2191 Hashtbl.remove state.tilemap k;
2193 loop (qpos+1)
2196 loop 0
2199 let flushtiles () =
2200 Queue.iter (fun (k, p, s) ->
2201 wcmd "freetile %s" p;
2202 state.memused <- state.memused - s;
2203 state.uioh#infochanged Memused;
2204 Hashtbl.remove state.tilemap k;
2205 ) state.tilelru;
2206 Queue.clear state.tilelru;
2207 load state.layout;
2210 let logcurrently = function
2211 | Idle -> dolog "Idle"
2212 | Loading (l, gen) ->
2213 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2214 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2215 dolog
2216 "Tiling %d[%d,%d] page=%s cs=%s angle"
2217 l.pageno col row pageopaque
2218 (colorspace_to_string colorspace)
2220 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2221 angle gen conf.angle state.gen
2222 tilew tileh
2223 conf.tilew conf.tileh
2225 | Outlining _ ->
2226 dolog "outlining"
2229 let act cmds =
2230 (* dolog "%S" cmds; *)
2231 let op, args =
2232 let spacepos =
2233 try String.index cmds ' '
2234 with Not_found -> -1
2236 if spacepos = -1
2237 then cmds, ""
2238 else
2239 let l = String.length cmds in
2240 let op = String.sub cmds 0 spacepos in
2241 op, begin
2242 if l - spacepos < 2 then ""
2243 else String.sub cmds (spacepos+1) (l-spacepos-1)
2246 match op with
2247 | "clear" ->
2248 state.uioh#infochanged Pdim;
2249 state.pdims <- [];
2251 | "clearrects" ->
2252 state.rects <- state.rects1;
2253 G.postRedisplay "clearrects";
2255 | "continue" ->
2256 let n =
2257 try Scanf.sscanf args "%u" (fun n -> n)
2258 with exn ->
2259 dolog "error processing 'continue' %S: %s"
2260 cmds (Printexc.to_string exn);
2261 exit 1;
2263 state.pagecount <- n;
2264 begin match state.currently with
2265 | Outlining l ->
2266 state.currently <- Idle;
2267 state.outlines <- Array.of_list (List.rev l)
2268 | _ -> ()
2269 end;
2271 let cur, cmds = state.geomcmds in
2272 if String.length cur = 0
2273 then failwith "umpossible";
2275 begin match List.rev cmds with
2276 | [] ->
2277 state.geomcmds <- "", [];
2278 represent ();
2279 | (s, f) :: rest ->
2280 f ();
2281 state.geomcmds <- s, List.rev rest;
2282 end;
2283 if conf.maxwait = None
2284 then G.postRedisplay "continue";
2286 | "title" ->
2287 Wsi.settitle args
2289 | "msg" ->
2290 showtext ' ' args
2292 | "vmsg" ->
2293 if conf.verbose
2294 then showtext ' ' args
2296 | "emsg" ->
2297 Buffer.add_string state.errmsgs args;
2298 state.newerrmsgs <- true;
2299 G.postRedisplay "error message"
2301 | "progress" ->
2302 let progress, text =
2304 Scanf.sscanf args "%f %n"
2305 (fun f pos ->
2306 f, String.sub args pos (String.length args - pos))
2307 with exn ->
2308 dolog "error processing 'progress' %S: %s"
2309 cmds (Printexc.to_string exn);
2310 exit 1;
2312 state.text <- text;
2313 state.progress <- progress;
2314 G.postRedisplay "progress"
2316 | "firstmatch" ->
2317 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2319 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2320 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2321 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2322 with exn ->
2323 dolog "error processing 'firstmatch' %S: %s"
2324 cmds (Printexc.to_string exn);
2325 exit 1;
2327 let y = (getpagey pageno) + truncate y0 in
2328 addnav ();
2329 gotoy y;
2330 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2332 | "match" ->
2333 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2335 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2336 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2337 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2338 with exn ->
2339 dolog "error processing 'match' %S: %s"
2340 cmds (Printexc.to_string exn);
2341 exit 1;
2343 state.rects1 <-
2344 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2346 | "page" ->
2347 let pageopaque, t =
2349 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2350 with exn ->
2351 dolog "error processing 'page' %S: %s"
2352 cmds (Printexc.to_string exn);
2353 exit 1;
2355 begin match state.currently with
2356 | Loading (l, gen) ->
2357 vlog "page %d took %f sec" l.pageno t;
2358 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2359 begin match state.throttle with
2360 | None ->
2361 let preloadedpages =
2362 if conf.preload
2363 then preloadlayout state.y
2364 else state.layout
2366 let evict () =
2367 let module IntSet =
2368 Set.Make (struct type t = int let compare = (-) end) in
2369 let set =
2370 List.fold_left (fun s l -> IntSet.add l.pageno s)
2371 IntSet.empty preloadedpages
2373 let evictedpages =
2374 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2375 if not (IntSet.mem pageno set)
2376 then (
2377 wcmd "freepage %s" opaque;
2378 key :: accu
2380 else accu
2381 ) state.pagemap []
2383 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2385 evict ();
2386 state.currently <- Idle;
2387 if gen = state.gen
2388 then (
2389 tilepage l.pageno pageopaque state.layout;
2390 load state.layout;
2391 load preloadedpages;
2392 if pagevisible state.layout l.pageno
2393 && layoutready state.layout
2394 then G.postRedisplay "page";
2397 | Some (layout, _, _) ->
2398 state.currently <- Idle;
2399 tilepage l.pageno pageopaque layout;
2400 load state.layout
2401 end;
2403 | _ ->
2404 dolog "Inconsistent loading state";
2405 logcurrently state.currently;
2406 exit 1
2409 | "tile" ->
2410 let (x, y, opaque, size, t) =
2412 Scanf.sscanf args "%u %u %s %u %f"
2413 (fun x y p size t -> (x, y, p, size, t))
2414 with exn ->
2415 dolog "error processing 'tile' %S: %s"
2416 cmds (Printexc.to_string exn);
2417 exit 1;
2419 begin match state.currently with
2420 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2421 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2423 unmappbo opaque;
2424 if tilew != conf.tilew || tileh != conf.tileh
2425 then (
2426 wcmd "freetile %s" opaque;
2427 state.currently <- Idle;
2428 load state.layout;
2430 else (
2431 puttileopaque l col row gen cs angle opaque size t;
2432 state.memused <- state.memused + size;
2433 state.uioh#infochanged Memused;
2434 gctiles ();
2435 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2436 opaque, size) state.tilelru;
2438 let layout =
2439 match state.throttle with
2440 | None -> state.layout
2441 | Some (layout, _, _) -> layout
2444 state.currently <- Idle;
2445 if gen = state.gen
2446 && conf.colorspace = cs
2447 && conf.angle = angle
2448 && tilevisible layout l.pageno x y
2449 then conttiling l.pageno pageopaque;
2451 begin match state.throttle with
2452 | None ->
2453 if state.wthack
2454 then state.wthack <- not (layoutready state.layout);
2455 preload state.layout;
2456 if gen = state.gen
2457 && conf.colorspace = cs
2458 && conf.angle = angle
2459 && tilevisible state.layout l.pageno x y
2460 then G.postRedisplay "tile nothrottle";
2462 | Some (layout, y, _) ->
2463 let ready = layoutready layout in
2464 if ready
2465 then (
2466 state.wthack <- false;
2467 state.y <- y;
2468 state.layout <- layout;
2469 state.throttle <- None;
2470 G.postRedisplay "throttle";
2472 else load layout;
2473 end;
2476 | _ ->
2477 dolog "Inconsistent tiling state";
2478 logcurrently state.currently;
2479 exit 1
2482 | "pdim" ->
2483 let pdim =
2485 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2486 with exn ->
2487 dolog "error processing 'pdim' %S: %s"
2488 cmds (Printexc.to_string exn);
2489 exit 1;
2491 state.uioh#infochanged Pdim;
2492 state.pdims <- pdim :: state.pdims
2494 | "o" ->
2495 let (l, n, t, h, pos) =
2497 Scanf.sscanf args "%u %u %d %u %n"
2498 (fun l n t h pos -> l, n, t, h, pos)
2499 with exn ->
2500 dolog "error processing 'o' %S: %s"
2501 cmds (Printexc.to_string exn);
2502 exit 1;
2504 let s = String.sub args pos (String.length args - pos) in
2505 let outline = (s, l, (n, float t /. float h, 0.0)) in
2506 begin match state.currently with
2507 | Outlining outlines ->
2508 state.currently <- Outlining (outline :: outlines)
2509 | Idle ->
2510 state.currently <- Outlining [outline]
2511 | currently ->
2512 dolog "invalid outlining state";
2513 logcurrently currently
2516 | "a" ->
2517 let (n, t, h) =
2519 Scanf.sscanf args "%u %u %d"
2520 (fun n t h -> n, t, h)
2521 with exn ->
2522 dolog "error processing 'a' %S: %s"
2523 cmds (Printexc.to_string exn);
2524 exit 1;
2526 let top, dtop =
2527 if conf.presentation
2528 then (0.0, 1.0)
2529 else float t, 0.0
2531 state.anchor <- (n, top /. float h, dtop)
2533 | "info" ->
2534 state.docinfo <- (1, args) :: state.docinfo
2536 | "infoend" ->
2537 state.uioh#infochanged Docinfo;
2538 state.docinfo <- List.rev state.docinfo
2540 | _ ->
2541 dolog "unknown cmd `%S'" cmds
2544 let onhist cb =
2545 let rc = cb.rc in
2546 let action = function
2547 | HCprev -> cbget cb ~-1
2548 | HCnext -> cbget cb 1
2549 | HCfirst -> cbget cb ~-(cb.rc)
2550 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2551 and cancel () = cb.rc <- rc
2552 in (action, cancel)
2555 let search pattern forward =
2556 if String.length pattern > 0
2557 then
2558 let pn, py =
2559 match state.layout with
2560 | [] -> 0, 0
2561 | l :: _ ->
2562 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2564 wcmd "search %d %d %d %d,%s\000"
2565 (btod conf.icase) pn py (btod forward) pattern;
2568 let intentry text key =
2569 let c =
2570 if key >= 32 && key < 127
2571 then Char.chr key
2572 else '\000'
2574 match c with
2575 | '0' .. '9' ->
2576 let text = addchar text c in
2577 TEcont text
2579 | _ ->
2580 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2581 TEcont text
2584 let linknentry text key =
2585 let c =
2586 if key >= 32 && key < 127
2587 then Char.chr key
2588 else '\000'
2590 match c with
2591 | 'a' .. 'z' ->
2592 let text = addchar text c in
2593 TEcont text
2595 | _ ->
2596 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2597 TEcont text
2600 let linkndone f s =
2601 if String.length s > 0
2602 then (
2603 let n =
2604 let l = String.length s in
2605 let rec loop pos n = if pos = l then n else
2606 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2607 loop (pos+1) (n*26 + m)
2608 in loop 0 0
2610 let rec loop n = function
2611 | [] -> ()
2612 | l :: rest ->
2613 match getopaque l.pageno with
2614 | None -> loop n rest
2615 | Some opaque ->
2616 let m = getlinkcount opaque in
2617 if n < m
2618 then (
2619 let under = getlink opaque n in
2620 f under
2622 else loop (n-m) rest
2624 loop n state.layout;
2628 let textentry text key =
2629 if key land 0xff00 = 0xff00
2630 then TEcont text
2631 else TEcont (text ^ Wsi.toutf8 key)
2634 let reqlayout angle proportional =
2635 match state.throttle with
2636 | None ->
2637 if nogeomcmds state.geomcmds
2638 then state.anchor <- getanchor ();
2639 conf.angle <- angle mod 360;
2640 if conf.angle != 0
2641 then (
2642 match state.mode with
2643 | LinkNav _ -> state.mode <- View
2644 | _ -> ()
2646 conf.proportional <- proportional;
2647 invalidate "reqlayout"
2648 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2649 | _ -> ()
2652 let settrim trimmargins trimfuzz =
2653 if nogeomcmds state.geomcmds
2654 then state.anchor <- getanchor ();
2655 conf.trimmargins <- trimmargins;
2656 conf.trimfuzz <- trimfuzz;
2657 let x0, y0, x1, y1 = trimfuzz in
2658 invalidate "settrim"
2659 (fun () ->
2660 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2661 flushpages ();
2664 let setzoom zoom =
2665 match state.throttle with
2666 | None ->
2667 let zoom = max 0.01 zoom in
2668 if zoom <> conf.zoom
2669 then (
2670 state.prevzoom <- conf.zoom;
2671 conf.zoom <- zoom;
2672 reshape conf.winw conf.winh;
2673 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2676 | Some (layout, y, started) ->
2677 let time =
2678 match conf.maxwait with
2679 | None -> 0.0
2680 | Some t -> t
2682 let dt = now () -. started in
2683 if dt > time
2684 then (
2685 state.y <- y;
2686 load layout;
2690 let setcolumns mode columns coverA coverB =
2691 state.prevcolumns <- Some (conf.columns, conf.zoom);
2692 if columns < 0
2693 then (
2694 if isbirdseye mode
2695 then showtext '!' "split mode doesn't work in bird's eye"
2696 else (
2697 conf.columns <- Csplit (-columns, [||]);
2698 state.x <- 0;
2699 conf.zoom <- 1.0;
2702 else (
2703 if columns < 2
2704 then (
2705 conf.columns <- Csingle [||];
2706 state.x <- 0;
2707 setzoom 1.0;
2709 else (
2710 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2711 conf.zoom <- 1.0;
2714 reshape conf.winw conf.winh;
2717 let enterbirdseye () =
2718 let zoom = float conf.thumbw /. float conf.winw in
2719 let birdseyepageno =
2720 let cy = conf.winh / 2 in
2721 let fold = function
2722 | [] -> 0
2723 | l :: rest ->
2724 let rec fold best = function
2725 | [] -> best.pageno
2726 | l :: rest ->
2727 let d = cy - (l.pagedispy + l.pagevh/2)
2728 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2729 if abs d < abs dbest
2730 then fold l rest
2731 else best.pageno
2732 in fold l rest
2734 fold state.layout
2736 state.mode <- Birdseye (
2737 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2739 conf.zoom <- zoom;
2740 conf.presentation <- false;
2741 conf.interpagespace <- 10;
2742 conf.hlinks <- false;
2743 state.x <- 0;
2744 state.mstate <- Mnone;
2745 conf.maxwait <- None;
2746 conf.columns <- (
2747 match conf.beyecolumns with
2748 | Some c ->
2749 conf.zoom <- 1.0;
2750 Cmulti ((c, 0, 0), [||])
2751 | None -> Csingle [||]
2753 Wsi.setcursor Wsi.CURSOR_INHERIT;
2754 if conf.verbose
2755 then
2756 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2757 (100.0*.zoom)
2758 else
2759 state.text <- ""
2761 reshape conf.winw conf.winh;
2764 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2765 state.mode <- View;
2766 conf.zoom <- c.zoom;
2767 conf.presentation <- c.presentation;
2768 conf.interpagespace <- c.interpagespace;
2769 conf.maxwait <- c.maxwait;
2770 conf.hlinks <- c.hlinks;
2771 conf.beyecolumns <- (
2772 match conf.columns with
2773 | Cmulti ((c, _, _), _) -> Some c
2774 | Csingle _ -> None
2775 | Csplit _ -> failwith "leaving bird's eye split mode"
2777 conf.columns <- (
2778 match c.columns with
2779 | Cmulti (c, _) -> Cmulti (c, [||])
2780 | Csingle _ -> Csingle [||]
2781 | Csplit (c, _) -> Csplit (c, [||])
2783 state.x <- leftx;
2784 if conf.verbose
2785 then
2786 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2787 (100.0*.conf.zoom)
2789 reshape conf.winw conf.winh;
2790 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2793 let togglebirdseye () =
2794 match state.mode with
2795 | Birdseye vals -> leavebirdseye vals true
2796 | View -> enterbirdseye ()
2797 | _ -> ()
2800 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2801 let pageno = max 0 (pageno - incr) in
2802 let rec loop = function
2803 | [] -> gotopage1 pageno 0
2804 | l :: _ when l.pageno = pageno ->
2805 if l.pagedispy >= 0 && l.pagey = 0
2806 then G.postRedisplay "upbirdseye"
2807 else gotopage1 pageno 0
2808 | _ :: rest -> loop rest
2810 loop state.layout;
2811 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2814 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2815 let pageno = min (state.pagecount - 1) (pageno + incr) in
2816 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2817 let rec loop = function
2818 | [] ->
2819 let y, h = getpageyh pageno in
2820 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2821 gotoy (clamp dy)
2822 | l :: _ when l.pageno = pageno ->
2823 if l.pagevh != l.pageh
2824 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2825 else G.postRedisplay "downbirdseye"
2826 | _ :: rest -> loop rest
2828 loop state.layout
2831 let optentry mode _ key =
2832 let btos b = if b then "on" else "off" in
2833 if key >= 32 && key < 127
2834 then
2835 let c = Char.chr key in
2836 match c with
2837 | 's' ->
2838 let ondone s =
2839 try conf.scrollstep <- int_of_string s with exc ->
2840 state.text <- Printf.sprintf "bad integer `%s': %s"
2841 s (Printexc.to_string exc)
2843 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2845 | 'A' ->
2846 let ondone s =
2848 conf.autoscrollstep <- int_of_string s;
2849 if state.autoscroll <> None
2850 then state.autoscroll <- Some conf.autoscrollstep
2851 with exc ->
2852 state.text <- Printf.sprintf "bad integer `%s': %s"
2853 s (Printexc.to_string exc)
2855 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2857 | 'C' ->
2858 let ondone s =
2860 let n, a, b = multicolumns_of_string s in
2861 setcolumns mode n a b;
2862 with exc ->
2863 state.text <- Printf.sprintf "bad columns `%s': %s"
2864 s (Printexc.to_string exc)
2866 TEswitch ("columns: ", "", None, textentry, ondone, true)
2868 | 'Z' ->
2869 let ondone s =
2871 let zoom = float (int_of_string s) /. 100.0 in
2872 setzoom zoom
2873 with exc ->
2874 state.text <- Printf.sprintf "bad integer `%s': %s"
2875 s (Printexc.to_string exc)
2877 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2879 | 't' ->
2880 let ondone s =
2882 conf.thumbw <- bound (int_of_string s) 2 4096;
2883 state.text <-
2884 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2885 begin match mode with
2886 | Birdseye beye ->
2887 leavebirdseye beye false;
2888 enterbirdseye ();
2889 | _ -> ();
2891 with exc ->
2892 state.text <- Printf.sprintf "bad integer `%s': %s"
2893 s (Printexc.to_string exc)
2895 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2897 | 'R' ->
2898 let ondone s =
2899 match try
2900 Some (int_of_string s)
2901 with exc ->
2902 state.text <- Printf.sprintf "bad integer `%s': %s"
2903 s (Printexc.to_string exc);
2904 None
2905 with
2906 | Some angle -> reqlayout angle conf.proportional
2907 | None -> ()
2909 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2911 | 'i' ->
2912 conf.icase <- not conf.icase;
2913 TEdone ("case insensitive search " ^ (btos conf.icase))
2915 | 'p' ->
2916 conf.preload <- not conf.preload;
2917 gotoy state.y;
2918 TEdone ("preload " ^ (btos conf.preload))
2920 | 'v' ->
2921 conf.verbose <- not conf.verbose;
2922 TEdone ("verbose " ^ (btos conf.verbose))
2924 | 'd' ->
2925 conf.debug <- not conf.debug;
2926 TEdone ("debug " ^ (btos conf.debug))
2928 | 'h' ->
2929 conf.maxhfit <- not conf.maxhfit;
2930 state.maxy <- calcheight ();
2931 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2933 | 'c' ->
2934 conf.crophack <- not conf.crophack;
2935 TEdone ("crophack " ^ btos conf.crophack)
2937 | 'a' ->
2938 let s =
2939 match conf.maxwait with
2940 | None ->
2941 conf.maxwait <- Some infinity;
2942 "always wait for page to complete"
2943 | Some _ ->
2944 conf.maxwait <- None;
2945 "show placeholder if page is not ready"
2947 TEdone s
2949 | 'f' ->
2950 conf.underinfo <- not conf.underinfo;
2951 TEdone ("underinfo " ^ btos conf.underinfo)
2953 | 'P' ->
2954 conf.savebmarks <- not conf.savebmarks;
2955 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2957 | 'S' ->
2958 let ondone s =
2960 let pageno, py =
2961 match state.layout with
2962 | [] -> 0, 0
2963 | l :: _ ->
2964 l.pageno, l.pagey
2966 conf.interpagespace <- int_of_string s;
2967 docolumns conf.columns;
2968 state.maxy <- calcheight ();
2969 let y = getpagey pageno in
2970 gotoy (y + py)
2971 with exc ->
2972 state.text <- Printf.sprintf "bad integer `%s': %s"
2973 s (Printexc.to_string exc)
2975 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2977 | 'l' ->
2978 reqlayout conf.angle (not conf.proportional);
2979 TEdone ("proportional display " ^ btos conf.proportional)
2981 | 'T' ->
2982 settrim (not conf.trimmargins) conf.trimfuzz;
2983 TEdone ("trim margins " ^ btos conf.trimmargins)
2985 | 'I' ->
2986 conf.invert <- not conf.invert;
2987 TEdone ("invert colors " ^ btos conf.invert)
2989 | 'x' ->
2990 let ondone s =
2991 cbput state.hists.sel s;
2992 conf.selcmd <- s;
2994 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2995 textentry, ondone, true)
2997 | _ ->
2998 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2999 TEstop
3000 else
3001 TEcont state.text
3004 class type lvsource = object
3005 method getitemcount : int
3006 method getitem : int -> (string * int)
3007 method hasaction : int -> bool
3008 method exit :
3009 uioh:uioh ->
3010 cancel:bool ->
3011 active:int ->
3012 first:int ->
3013 pan:int ->
3014 qsearch:string ->
3015 uioh option
3016 method getactive : int
3017 method getfirst : int
3018 method getqsearch : string
3019 method setqsearch : string -> unit
3020 method getpan : int
3021 end;;
3023 class virtual lvsourcebase = object
3024 val mutable m_active = 0
3025 val mutable m_first = 0
3026 val mutable m_qsearch = ""
3027 val mutable m_pan = 0
3028 method getactive = m_active
3029 method getfirst = m_first
3030 method getqsearch = m_qsearch
3031 method getpan = m_pan
3032 method setqsearch s = m_qsearch <- s
3033 end;;
3035 let withoutlastutf8 s =
3036 let len = String.length s in
3037 if len = 0
3038 then s
3039 else
3040 let rec find pos =
3041 if pos = 0
3042 then pos
3043 else
3044 let b = Char.code s.[pos] in
3045 if b land 0b11000000 = 0b11000000
3046 then pos
3047 else find (pos-1)
3049 let first =
3050 if Char.code s.[len-1] land 0x80 = 0
3051 then len-1
3052 else find (len-1)
3054 String.sub s 0 first;
3057 let textentrykeyboard
3058 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3059 let key =
3060 if key >= 0xffb0 && key <= 0xffb9
3061 then key - 0xffb0 + 48 else key
3063 let enttext te =
3064 state.mode <- Textentry (te, onleave);
3065 state.text <- "";
3066 enttext ();
3067 G.postRedisplay "textentrykeyboard enttext";
3069 let histaction cmd =
3070 match opthist with
3071 | None -> ()
3072 | Some (action, _) ->
3073 state.mode <- Textentry (
3074 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3076 G.postRedisplay "textentry histaction"
3078 match key with
3079 | 0xff08 -> (* backspace *)
3080 let s = withoutlastutf8 text in
3081 let len = String.length s in
3082 if cancelonempty && len = 0
3083 then (
3084 onleave Cancel;
3085 G.postRedisplay "textentrykeyboard after cancel";
3087 else (
3088 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3091 | 0xff0d | 0xff8d -> (* (kp) enter *)
3092 ondone text;
3093 onleave Confirm;
3094 G.postRedisplay "textentrykeyboard after confirm"
3096 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3097 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3098 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3099 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3101 | 0xff1b -> (* escape*)
3102 if String.length text = 0
3103 then (
3104 begin match opthist with
3105 | None -> ()
3106 | Some (_, onhistcancel) -> onhistcancel ()
3107 end;
3108 onleave Cancel;
3109 state.text <- "";
3110 G.postRedisplay "textentrykeyboard after cancel2"
3112 else (
3113 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3116 | 0xff9f | 0xffff -> () (* delete *)
3118 | _ when key != 0
3119 && key land 0xff00 != 0xff00 (* keyboard *)
3120 && key land 0xfe00 != 0xfe00 (* xkb *)
3121 && key land 0xfd00 != 0xfd00 (* 3270 *)
3123 begin match onkey text key with
3124 | TEdone text ->
3125 ondone text;
3126 onleave Confirm;
3127 G.postRedisplay "textentrykeyboard after confirm2";
3129 | TEcont text ->
3130 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3132 | TEstop ->
3133 onleave Cancel;
3134 G.postRedisplay "textentrykeyboard after cancel3"
3136 | TEswitch te ->
3137 state.mode <- Textentry (te, onleave);
3138 G.postRedisplay "textentrykeyboard switch";
3139 end;
3141 | _ ->
3142 vlog "unhandled key %s" (Wsi.keyname key)
3145 let firstof first active =
3146 if first > active || abs (first - active) > fstate.maxrows - 1
3147 then max 0 (active - (fstate.maxrows/2))
3148 else first
3151 let calcfirst first active =
3152 if active > first
3153 then
3154 let rows = active - first in
3155 if rows > fstate.maxrows then active - fstate.maxrows else first
3156 else active
3159 let scrollph y maxy =
3160 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3161 let sh = float conf.winh /. sh in
3162 let sh = max sh (float conf.scrollh) in
3164 let percent =
3165 if y = state.maxy
3166 then 1.0
3167 else float y /. float maxy
3169 let position = (float conf.winh -. sh) *. percent in
3171 let position =
3172 if position +. sh > float conf.winh
3173 then float conf.winh -. sh
3174 else position
3176 position, sh;
3179 let coe s = (s :> uioh);;
3181 class listview ~(source:lvsource) ~trusted ~modehash =
3182 object (self)
3183 val m_pan = source#getpan
3184 val m_first = source#getfirst
3185 val m_active = source#getactive
3186 val m_qsearch = source#getqsearch
3187 val m_prev_uioh = state.uioh
3189 method private elemunder y =
3190 let n = y / (fstate.fontsize+1) in
3191 if m_first + n < source#getitemcount
3192 then (
3193 if source#hasaction (m_first + n)
3194 then Some (m_first + n)
3195 else None
3197 else None
3199 method display =
3200 Gl.enable `blend;
3201 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3202 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3203 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3204 GlDraw.color (1., 1., 1.);
3205 Gl.enable `texture_2d;
3206 let fs = fstate.fontsize in
3207 let nfs = fs + 1 in
3208 let ww = fstate.wwidth in
3209 let tabw = 30.0*.ww in
3210 let itemcount = source#getitemcount in
3211 let rec loop row =
3212 if (row - m_first) > fstate.maxrows
3213 then ()
3214 else (
3215 if row >= 0 && row < itemcount
3216 then (
3217 let (s, level) = source#getitem row in
3218 let y = (row - m_first) * nfs in
3219 let x = 5.0 +. float (level + m_pan) *. ww in
3220 if row = m_active
3221 then (
3222 Gl.disable `texture_2d;
3223 GlDraw.polygon_mode `both `line;
3224 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3225 GlDraw.rect (1., float (y + 1))
3226 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3227 GlDraw.polygon_mode `both `fill;
3228 GlDraw.color (1., 1., 1.);
3229 Gl.enable `texture_2d;
3232 let drawtabularstring s =
3233 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3234 if trusted
3235 then
3236 let tabpos = try String.index s '\t' with Not_found -> -1 in
3237 if tabpos > 0
3238 then
3239 let len = String.length s - tabpos - 1 in
3240 let s1 = String.sub s 0 tabpos
3241 and s2 = String.sub s (tabpos + 1) len in
3242 let nx = drawstr x s1 in
3243 let sw = nx -. x in
3244 let x = x +. (max tabw sw) in
3245 drawstr x s2
3246 else
3247 drawstr x s
3248 else
3249 drawstr x s
3251 let _ = drawtabularstring s in
3252 loop (row+1)
3256 loop m_first;
3257 Gl.disable `blend;
3258 Gl.disable `texture_2d;
3260 method updownlevel incr =
3261 let len = source#getitemcount in
3262 let curlevel =
3263 if m_active >= 0 && m_active < len
3264 then snd (source#getitem m_active)
3265 else -1
3267 let rec flow i =
3268 if i = len then i-1 else if i = -1 then 0 else
3269 let _, l = source#getitem i in
3270 if l != curlevel then i else flow (i+incr)
3272 let active = flow m_active in
3273 let first = calcfirst m_first active in
3274 G.postRedisplay "outline updownlevel";
3275 {< m_active = active; m_first = first >}
3277 method private key1 key mask =
3278 let set1 active first qsearch =
3279 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3281 let search active pattern incr =
3282 let dosearch re =
3283 let rec loop n =
3284 if n >= 0 && n < source#getitemcount
3285 then (
3286 let s, _ = source#getitem n in
3288 (try ignore (Str.search_forward re s 0); true
3289 with Not_found -> false)
3290 then Some n
3291 else loop (n + incr)
3293 else None
3295 loop active
3298 let re = Str.regexp_case_fold pattern in
3299 dosearch re
3300 with Failure s ->
3301 state.text <- s;
3302 None
3304 let itemcount = source#getitemcount in
3305 let find start incr =
3306 let rec find i =
3307 if i = -1 || i = itemcount
3308 then -1
3309 else (
3310 if source#hasaction i
3311 then i
3312 else find (i + incr)
3315 find start
3317 let set active first =
3318 let first = bound first 0 (itemcount - fstate.maxrows) in
3319 state.text <- "";
3320 coe {< m_active = active; m_first = first >}
3322 let navigate incr =
3323 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3324 let active, first =
3325 let incr1 = if incr > 0 then 1 else -1 in
3326 if isvisible m_first m_active
3327 then
3328 let next =
3329 let next = m_active + incr in
3330 let next =
3331 if next < 0 || next >= itemcount
3332 then -1
3333 else find next incr1
3335 if next = -1 || abs (m_active - next) > fstate.maxrows
3336 then -1
3337 else next
3339 if next = -1
3340 then
3341 let first = m_first + incr in
3342 let first = bound first 0 (itemcount - 1) in
3343 let next =
3344 let next = m_active + incr in
3345 let next = bound next 0 (itemcount - 1) in
3346 find next ~-incr1
3348 let active = if next = -1 then m_active else next in
3349 active, first
3350 else
3351 let first = min next m_first in
3352 let first =
3353 if abs (next - first) > fstate.maxrows
3354 then first + incr
3355 else first
3357 next, first
3358 else
3359 let first = m_first + incr in
3360 let first = bound first 0 (itemcount - 1) in
3361 let active =
3362 let next = m_active + incr in
3363 let next = bound next 0 (itemcount - 1) in
3364 let next = find next incr1 in
3365 let active =
3366 if next = -1 || abs (m_active - first) > fstate.maxrows
3367 then (
3368 let active = if m_active = -1 then next else m_active in
3369 active
3371 else next
3373 if isvisible first active
3374 then active
3375 else -1
3377 active, first
3379 G.postRedisplay "listview navigate";
3380 set active first;
3382 match key with
3383 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3384 let incr = if key = 0x72 then -1 else 1 in
3385 let active, first =
3386 match search (m_active + incr) m_qsearch incr with
3387 | None ->
3388 state.text <- m_qsearch ^ " [not found]";
3389 m_active, m_first
3390 | Some active ->
3391 state.text <- m_qsearch;
3392 active, firstof m_first active
3394 G.postRedisplay "listview ctrl-r/s";
3395 set1 active first m_qsearch;
3397 | 0xff08 -> (* backspace *)
3398 if String.length m_qsearch = 0
3399 then coe self
3400 else (
3401 let qsearch = withoutlastutf8 m_qsearch in
3402 let len = String.length qsearch in
3403 if len = 0
3404 then (
3405 state.text <- "";
3406 G.postRedisplay "listview empty qsearch";
3407 set1 m_active m_first "";
3409 else
3410 let active, first =
3411 match search m_active qsearch ~-1 with
3412 | None ->
3413 state.text <- qsearch ^ " [not found]";
3414 m_active, m_first
3415 | Some active ->
3416 state.text <- qsearch;
3417 active, firstof m_first active
3419 G.postRedisplay "listview backspace qsearch";
3420 set1 active first qsearch
3423 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3424 let pattern = m_qsearch ^ Wsi.toutf8 key in
3425 let active, first =
3426 match search m_active pattern 1 with
3427 | None ->
3428 state.text <- pattern ^ " [not found]";
3429 m_active, m_first
3430 | Some active ->
3431 state.text <- pattern;
3432 active, firstof m_first active
3434 G.postRedisplay "listview qsearch add";
3435 set1 active first pattern;
3437 | 0xff1b -> (* escape *)
3438 state.text <- "";
3439 if String.length m_qsearch = 0
3440 then (
3441 G.postRedisplay "list view escape";
3442 begin
3443 match
3444 source#exit (coe self) true m_active m_first m_pan m_qsearch
3445 with
3446 | None -> m_prev_uioh
3447 | Some uioh -> uioh
3450 else (
3451 G.postRedisplay "list view kill qsearch";
3452 source#setqsearch "";
3453 coe {< m_qsearch = "" >}
3456 | 0xff0d | 0xff8d -> (* (kp) enter *)
3457 state.text <- "";
3458 let self = {< m_qsearch = "" >} in
3459 source#setqsearch "";
3460 let opt =
3461 G.postRedisplay "listview enter";
3462 if m_active >= 0 && m_active < source#getitemcount
3463 then (
3464 source#exit (coe self) false m_active m_first m_pan "";
3466 else (
3467 source#exit (coe self) true m_active m_first m_pan "";
3470 begin match opt with
3471 | None -> m_prev_uioh
3472 | Some uioh -> uioh
3475 | 0xff9f | 0xffff -> (* (kp) delete *)
3476 coe self
3478 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3479 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3480 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3481 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3483 | 0xff53 | 0xff98 -> (* (kp) right *)
3484 state.text <- "";
3485 G.postRedisplay "listview right";
3486 coe {< m_pan = m_pan - 1 >}
3488 | 0xff51 | 0xff96 -> (* (kp) left *)
3489 state.text <- "";
3490 G.postRedisplay "listview left";
3491 coe {< m_pan = m_pan + 1 >}
3493 | 0xff50 | 0xff95 -> (* (kp) home *)
3494 let active = find 0 1 in
3495 G.postRedisplay "listview home";
3496 set active 0;
3498 | 0xff57 | 0xff9c -> (* (kp) end *)
3499 let first = max 0 (itemcount - fstate.maxrows) in
3500 let active = find (itemcount - 1) ~-1 in
3501 G.postRedisplay "listview end";
3502 set active first;
3504 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3505 coe self
3507 | _ ->
3508 dolog "listview unknown key %#x" key; coe self
3510 method key key mask =
3511 match state.mode with
3512 | Textentry te -> textentrykeyboard key mask te; coe self
3513 | _ -> self#key1 key mask
3515 method button button down x y _ =
3516 let opt =
3517 match button with
3518 | 1 when x > conf.winw - conf.scrollbw ->
3519 G.postRedisplay "listview scroll";
3520 if down
3521 then
3522 let _, position, sh = self#scrollph in
3523 if y > truncate position && y < truncate (position +. sh)
3524 then (
3525 state.mstate <- Mscrolly;
3526 Some (coe self)
3528 else
3529 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3530 let first = truncate (s *. float source#getitemcount) in
3531 let first = min source#getitemcount first in
3532 Some (coe {< m_first = first; m_active = first >})
3533 else (
3534 state.mstate <- Mnone;
3535 Some (coe self);
3537 | 1 when not down ->
3538 begin match self#elemunder y with
3539 | Some n ->
3540 G.postRedisplay "listview click";
3541 source#exit
3542 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3543 | _ ->
3544 Some (coe self)
3546 | n when (n == 4 || n == 5) && not down ->
3547 let len = source#getitemcount in
3548 let first =
3549 if n = 5 && m_first + fstate.maxrows >= len
3550 then
3551 m_first
3552 else
3553 let first = m_first + (if n == 4 then -1 else 1) in
3554 bound first 0 (len - 1)
3556 G.postRedisplay "listview wheel";
3557 Some (coe {< m_first = first >})
3558 | n when (n = 6 || n = 7) && not down ->
3559 let inc = m_first + (if n = 7 then -1 else 1) in
3560 G.postRedisplay "listview hwheel";
3561 Some (coe {< m_pan = m_pan + inc >})
3562 | _ ->
3563 Some (coe self)
3565 match opt with
3566 | None -> m_prev_uioh
3567 | Some uioh -> uioh
3569 method motion _ y =
3570 match state.mstate with
3571 | Mscrolly ->
3572 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3573 let first = truncate (s *. float source#getitemcount) in
3574 let first = min source#getitemcount first in
3575 G.postRedisplay "listview motion";
3576 coe {< m_first = first; m_active = first >}
3577 | _ -> coe self
3579 method pmotion x y =
3580 if x < conf.winw - conf.scrollbw
3581 then
3582 let n =
3583 match self#elemunder y with
3584 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3585 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3587 let o =
3588 if n != m_active
3589 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3590 else self
3592 coe o
3593 else (
3594 Wsi.setcursor Wsi.CURSOR_INHERIT;
3595 coe self
3598 method infochanged _ = ()
3600 method scrollpw = (0, 0.0, 0.0)
3601 method scrollph =
3602 let nfs = fstate.fontsize + 1 in
3603 let y = m_first * nfs in
3604 let itemcount = source#getitemcount in
3605 let maxi = max 0 (itemcount - fstate.maxrows) in
3606 let maxy = maxi * nfs in
3607 let p, h = scrollph y maxy in
3608 conf.scrollbw, p, h
3610 method modehash = modehash
3611 end;;
3613 class outlinelistview ~source =
3614 object (self)
3615 inherit listview
3616 ~source:(source :> lvsource)
3617 ~trusted:false
3618 ~modehash:(findkeyhash conf "outline")
3619 as super
3621 method key key mask =
3622 let calcfirst first active =
3623 if active > first
3624 then
3625 let rows = active - first in
3626 let maxrows =
3627 if String.length state.text = 0
3628 then fstate.maxrows
3629 else fstate.maxrows - 2
3631 if rows > maxrows then active - maxrows else first
3632 else active
3634 let navigate incr =
3635 let active = m_active + incr in
3636 let active = bound active 0 (source#getitemcount - 1) in
3637 let first = calcfirst m_first active in
3638 G.postRedisplay "outline navigate";
3639 coe {< m_active = active; m_first = first >}
3641 let ctrl = Wsi.withctrl mask in
3642 match key with
3643 | 110 when ctrl -> (* ctrl-n *)
3644 source#narrow m_qsearch;
3645 G.postRedisplay "outline ctrl-n";
3646 coe {< m_first = 0; m_active = 0 >}
3648 | 117 when ctrl -> (* ctrl-u *)
3649 source#denarrow;
3650 G.postRedisplay "outline ctrl-u";
3651 state.text <- "";
3652 coe {< m_first = 0; m_active = 0 >}
3654 | 108 when ctrl -> (* ctrl-l *)
3655 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3656 G.postRedisplay "outline ctrl-l";
3657 coe {< m_first = first >}
3659 | 0xff9f | 0xffff -> (* (kp) delete *)
3660 source#remove m_active;
3661 G.postRedisplay "outline delete";
3662 let active = max 0 (m_active-1) in
3663 coe {< m_first = firstof m_first active;
3664 m_active = active >}
3666 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3667 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3668 | 0xff55 | 0xff9a -> (* (kp) prior *)
3669 navigate ~-(fstate.maxrows)
3670 | 0xff56 | 0xff9b -> (* (kp) next *)
3671 navigate fstate.maxrows
3673 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3674 let o =
3675 if ctrl
3676 then (
3677 G.postRedisplay "outline ctrl right";
3678 {< m_pan = m_pan + 1 >}
3680 else self#updownlevel 1
3682 coe o
3684 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3685 let o =
3686 if ctrl
3687 then (
3688 G.postRedisplay "outline ctrl left";
3689 {< m_pan = m_pan - 1 >}
3691 else self#updownlevel ~-1
3693 coe o
3695 | 0xff50 | 0xff95 -> (* (kp) home *)
3696 G.postRedisplay "outline home";
3697 coe {< m_first = 0; m_active = 0 >}
3699 | 0xff57 | 0xff9c -> (* (kp) end *)
3700 let active = source#getitemcount - 1 in
3701 let first = max 0 (active - fstate.maxrows) in
3702 G.postRedisplay "outline end";
3703 coe {< m_active = active; m_first = first >}
3705 | _ -> super#key key mask
3708 let outlinesource usebookmarks =
3709 let empty = [||] in
3710 (object
3711 inherit lvsourcebase
3712 val mutable m_items = empty
3713 val mutable m_orig_items = empty
3714 val mutable m_prev_items = empty
3715 val mutable m_narrow_pattern = ""
3716 val mutable m_hadremovals = false
3718 method getitemcount =
3719 Array.length m_items + (if m_hadremovals then 1 else 0)
3721 method getitem n =
3722 if n == Array.length m_items && m_hadremovals
3723 then
3724 ("[Confirm removal]", 0)
3725 else
3726 let s, n, _ = m_items.(n) in
3727 (s, n)
3729 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3730 ignore (uioh, first, qsearch);
3731 let confrimremoval = m_hadremovals && active = Array.length m_items in
3732 let items =
3733 if String.length m_narrow_pattern = 0
3734 then m_orig_items
3735 else m_items
3737 if not cancel
3738 then (
3739 if not confrimremoval
3740 then(
3741 let _, _, anchor = m_items.(active) in
3742 gotoghyll (getanchory anchor);
3743 m_items <- items;
3745 else (
3746 state.bookmarks <- Array.to_list m_items;
3747 m_orig_items <- m_items;
3750 else m_items <- items;
3751 m_pan <- pan;
3752 None
3754 method hasaction _ = true
3756 method greetmsg =
3757 if Array.length m_items != Array.length m_orig_items
3758 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3759 else ""
3761 method narrow pattern =
3762 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3763 match reopt with
3764 | None -> ()
3765 | Some re ->
3766 let rec loop accu n =
3767 if n = -1
3768 then (
3769 m_narrow_pattern <- pattern;
3770 m_items <- Array.of_list accu
3772 else
3773 let (s, _, _) as o = m_items.(n) in
3774 let accu =
3775 if (try ignore (Str.search_forward re s 0); true
3776 with Not_found -> false)
3777 then o :: accu
3778 else accu
3780 loop accu (n-1)
3782 loop [] (Array.length m_items - 1)
3784 method denarrow =
3785 m_orig_items <- (
3786 if usebookmarks
3787 then Array.of_list state.bookmarks
3788 else state.outlines
3790 m_items <- m_orig_items
3792 method remove m =
3793 if usebookmarks
3794 then
3795 if m >= 0 && m < Array.length m_items
3796 then (
3797 m_hadremovals <- true;
3798 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3799 let n = if n >= m then n+1 else n in
3800 m_items.(n)
3804 method reset anchor items =
3805 m_hadremovals <- false;
3806 if m_orig_items == empty || m_prev_items != items
3807 then (
3808 m_orig_items <- items;
3809 if String.length m_narrow_pattern = 0
3810 then m_items <- items;
3812 m_prev_items <- items;
3813 let rely = getanchory anchor in
3814 let active =
3815 let rec loop n best bestd =
3816 if n = Array.length m_items
3817 then best
3818 else
3819 let (_, _, anchor) = m_items.(n) in
3820 let orely = getanchory anchor in
3821 let d = abs (orely - rely) in
3822 if d < bestd
3823 then loop (n+1) n d
3824 else loop (n+1) best bestd
3826 loop 0 ~-1 max_int
3828 m_active <- active;
3829 m_first <- firstof m_first active
3830 end)
3833 let enterselector usebookmarks =
3834 let source = outlinesource usebookmarks in
3835 fun errmsg ->
3836 let outlines =
3837 if usebookmarks
3838 then Array.of_list state.bookmarks
3839 else state.outlines
3841 if Array.length outlines = 0
3842 then (
3843 showtext ' ' errmsg;
3845 else (
3846 state.text <- source#greetmsg;
3847 Wsi.setcursor Wsi.CURSOR_INHERIT;
3848 let anchor = getanchor () in
3849 source#reset anchor outlines;
3850 state.uioh <- coe (new outlinelistview ~source);
3851 G.postRedisplay "enter selector";
3855 let enteroutlinemode =
3856 let f = enterselector false in
3857 fun ()-> f "Document has no outline";
3860 let enterbookmarkmode =
3861 let f = enterselector true in
3862 fun () -> f "Document has no bookmarks (yet)";
3865 let color_of_string s =
3866 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3867 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3871 let color_to_string (r, g, b) =
3872 let r = truncate (r *. 256.0)
3873 and g = truncate (g *. 256.0)
3874 and b = truncate (b *. 256.0) in
3875 Printf.sprintf "%d/%d/%d" r g b
3878 let irect_of_string s =
3879 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3882 let irect_to_string (x0,y0,x1,y1) =
3883 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3886 let makecheckers () =
3887 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3888 following to say:
3889 converted by Issac Trotts. July 25, 2002 *)
3890 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3891 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3892 let id = GlTex.gen_texture () in
3893 GlTex.bind_texture `texture_2d id;
3894 GlPix.store (`unpack_alignment 1);
3895 GlTex.image2d image;
3896 List.iter (GlTex.parameter ~target:`texture_2d)
3897 [ `wrap_s `repeat;
3898 `wrap_t `repeat;
3899 `mag_filter `nearest;
3900 `min_filter `nearest ];
3904 let setcheckers enabled =
3905 match state.texid with
3906 | None ->
3907 if enabled then state.texid <- Some (makecheckers ())
3909 | Some texid ->
3910 if not enabled
3911 then (
3912 GlTex.delete_texture texid;
3913 state.texid <- None;
3917 let int_of_string_with_suffix s =
3918 let l = String.length s in
3919 let s1, shift =
3920 if l > 1
3921 then
3922 let suffix = Char.lowercase s.[l-1] in
3923 match suffix with
3924 | 'k' -> String.sub s 0 (l-1), 10
3925 | 'm' -> String.sub s 0 (l-1), 20
3926 | 'g' -> String.sub s 0 (l-1), 30
3927 | _ -> s, 0
3928 else s, 0
3930 let n = int_of_string s1 in
3931 let m = n lsl shift in
3932 if m < 0 || m < n
3933 then raise (Failure "value too large")
3934 else m
3937 let string_with_suffix_of_int n =
3938 if n = 0
3939 then "0"
3940 else
3941 let n, s =
3942 if n land ((1 lsl 30) - 1) = 0
3943 then n lsr 30, "G"
3944 else (
3945 if n land ((1 lsl 20) - 1) = 0
3946 then n lsr 20, "M"
3947 else (
3948 if n land ((1 lsl 10) - 1) = 0
3949 then n lsr 10, "K"
3950 else n, ""
3954 let rec loop s n =
3955 let h = n mod 1000 in
3956 let n = n / 1000 in
3957 if n = 0
3958 then string_of_int h ^ s
3959 else (
3960 let s = Printf.sprintf "_%03d%s" h s in
3961 loop s n
3964 loop "" n ^ s;
3967 let defghyllscroll = (40, 8, 32);;
3968 let ghyllscroll_of_string s =
3969 let (n, a, b) as nab =
3970 if s = "default"
3971 then defghyllscroll
3972 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3974 if n <= a || n <= b || a >= b
3975 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3976 nab;
3979 let ghyllscroll_to_string ((n, a, b) as nab) =
3980 if nab = defghyllscroll
3981 then "default"
3982 else Printf.sprintf "%d,%d,%d" n a b;
3985 let describe_location () =
3986 let f (fn, _) l =
3987 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3989 let fn, ln = List.fold_left f (-1, -1) state.layout in
3990 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3991 let percent =
3992 if maxy <= 0
3993 then 100.
3994 else (100. *. (float state.y /. float maxy))
3996 if fn = ln
3997 then
3998 Printf.sprintf "page %d of %d [%.2f%%]"
3999 (fn+1) state.pagecount percent
4000 else
4001 Printf.sprintf
4002 "pages %d-%d of %d [%.2f%%]"
4003 (fn+1) (ln+1) state.pagecount percent
4006 let setpresentationmode v =
4007 let (n, _, _) = getanchor () in
4008 let _, h = getpageyh n in
4009 let ips = if conf.presentation then calcips h else conf.interpagespace in
4010 state.anchor <- (n, 0.0, float ips);
4011 conf.presentation <- v;
4012 if conf.presentation
4013 then (
4014 if not conf.scrollbarinpm
4015 then state.scrollw <- 0;
4017 else state.scrollw <- conf.scrollbw;
4018 represent ();
4021 let enterinfomode =
4022 let btos b = if b then "\xe2\x88\x9a" else "" in
4023 let showextended = ref false in
4024 let leave mode = function
4025 | Confirm -> state.mode <- mode
4026 | Cancel -> state.mode <- mode in
4027 let src =
4028 (object
4029 val mutable m_first_time = true
4030 val mutable m_l = []
4031 val mutable m_a = [||]
4032 val mutable m_prev_uioh = nouioh
4033 val mutable m_prev_mode = View
4035 inherit lvsourcebase
4037 method reset prev_mode prev_uioh =
4038 m_a <- Array.of_list (List.rev m_l);
4039 m_l <- [];
4040 m_prev_mode <- prev_mode;
4041 m_prev_uioh <- prev_uioh;
4042 if m_first_time
4043 then (
4044 let rec loop n =
4045 if n >= Array.length m_a
4046 then ()
4047 else
4048 match m_a.(n) with
4049 | _, _, _, Action _ -> m_active <- n
4050 | _ -> loop (n+1)
4052 loop 0;
4053 m_first_time <- false;
4056 method int name get set =
4057 m_l <-
4058 (name, `int get, 1, Action (
4059 fun u ->
4060 let ondone s =
4061 try set (int_of_string s)
4062 with exn ->
4063 state.text <- Printf.sprintf "bad integer `%s': %s"
4064 s (Printexc.to_string exn)
4066 state.text <- "";
4067 let te = name ^ ": ", "", None, intentry, ondone, true in
4068 state.mode <- Textentry (te, leave m_prev_mode);
4070 )) :: m_l
4072 method int_with_suffix name get set =
4073 m_l <-
4074 (name, `intws get, 1, Action (
4075 fun u ->
4076 let ondone s =
4077 try set (int_of_string_with_suffix s)
4078 with exn ->
4079 state.text <- Printf.sprintf "bad integer `%s': %s"
4080 s (Printexc.to_string exn)
4082 state.text <- "";
4083 let te =
4084 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4086 state.mode <- Textentry (te, leave m_prev_mode);
4088 )) :: m_l
4090 method bool ?(offset=1) ?(btos=btos) name get set =
4091 m_l <-
4092 (name, `bool (btos, get), offset, Action (
4093 fun u ->
4094 let v = get () in
4095 set (not v);
4097 )) :: m_l
4099 method color name get set =
4100 m_l <-
4101 (name, `color get, 1, Action (
4102 fun u ->
4103 let invalid = (nan, nan, nan) in
4104 let ondone s =
4105 let c =
4106 try color_of_string s
4107 with exn ->
4108 state.text <- Printf.sprintf "bad color `%s': %s"
4109 s (Printexc.to_string exn);
4110 invalid
4112 if c <> invalid
4113 then set c;
4115 let te = name ^ ": ", "", None, textentry, ondone, true in
4116 state.text <- color_to_string (get ());
4117 state.mode <- Textentry (te, leave m_prev_mode);
4119 )) :: m_l
4121 method string name get set =
4122 m_l <-
4123 (name, `string get, 1, Action (
4124 fun u ->
4125 let ondone s = set s in
4126 let te = name ^ ": ", "", None, textentry, ondone, true in
4127 state.mode <- Textentry (te, leave m_prev_mode);
4129 )) :: m_l
4131 method colorspace name get set =
4132 m_l <-
4133 (name, `string get, 1, Action (
4134 fun _ ->
4135 let source =
4136 let vals = [| "rgb"; "bgr"; "gray" |] in
4137 (object
4138 inherit lvsourcebase
4140 initializer
4141 m_active <- int_of_colorspace conf.colorspace;
4142 m_first <- 0;
4144 method getitemcount = Array.length vals
4145 method getitem n = (vals.(n), 0)
4146 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4147 ignore (uioh, first, pan, qsearch);
4148 if not cancel then set active;
4149 None
4150 method hasaction _ = true
4151 end)
4153 state.text <- "";
4154 let modehash = findkeyhash conf "info" in
4155 coe (new listview ~source ~trusted:true ~modehash)
4156 )) :: m_l
4158 method caption s offset =
4159 m_l <- (s, `empty, offset, Noaction) :: m_l
4161 method caption2 s f offset =
4162 m_l <- (s, `string f, offset, Noaction) :: m_l
4164 method getitemcount = Array.length m_a
4166 method getitem n =
4167 let tostr = function
4168 | `int f -> string_of_int (f ())
4169 | `intws f -> string_with_suffix_of_int (f ())
4170 | `string f -> f ()
4171 | `color f -> color_to_string (f ())
4172 | `bool (btos, f) -> btos (f ())
4173 | `empty -> ""
4175 let name, t, offset, _ = m_a.(n) in
4176 ((let s = tostr t in
4177 if String.length s > 0
4178 then Printf.sprintf "%s\t%s" name s
4179 else name),
4180 offset)
4182 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4183 let uiohopt =
4184 if not cancel
4185 then (
4186 m_qsearch <- qsearch;
4187 let uioh =
4188 match m_a.(active) with
4189 | _, _, _, Action f -> f uioh
4190 | _ -> uioh
4192 Some uioh
4194 else None
4196 m_active <- active;
4197 m_first <- first;
4198 m_pan <- pan;
4199 uiohopt
4201 method hasaction n =
4202 match m_a.(n) with
4203 | _, _, _, Action _ -> true
4204 | _ -> false
4205 end)
4207 let rec fillsrc prevmode prevuioh =
4208 let sep () = src#caption "" 0 in
4209 let colorp name get set =
4210 src#string name
4211 (fun () -> color_to_string (get ()))
4212 (fun v ->
4214 let c = color_of_string v in
4215 set c
4216 with exn ->
4217 state.text <- Printf.sprintf "bad color `%s': %s"
4218 v (Printexc.to_string exn);
4221 let oldmode = state.mode in
4222 let birdseye = isbirdseye state.mode in
4224 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4226 src#bool "presentation mode"
4227 (fun () -> conf.presentation)
4228 (fun v -> setpresentationmode v);
4230 src#bool "ignore case in searches"
4231 (fun () -> conf.icase)
4232 (fun v -> conf.icase <- v);
4234 src#bool "preload"
4235 (fun () -> conf.preload)
4236 (fun v -> conf.preload <- v);
4238 src#bool "highlight links"
4239 (fun () -> conf.hlinks)
4240 (fun v -> conf.hlinks <- v);
4242 src#bool "under info"
4243 (fun () -> conf.underinfo)
4244 (fun v -> conf.underinfo <- v);
4246 src#bool "persistent bookmarks"
4247 (fun () -> conf.savebmarks)
4248 (fun v -> conf.savebmarks <- v);
4250 src#bool "proportional display"
4251 (fun () -> conf.proportional)
4252 (fun v -> reqlayout conf.angle v);
4254 src#bool "trim margins"
4255 (fun () -> conf.trimmargins)
4256 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4258 src#bool "persistent location"
4259 (fun () -> conf.jumpback)
4260 (fun v -> conf.jumpback <- v);
4262 sep ();
4263 src#int "inter-page space"
4264 (fun () -> conf.interpagespace)
4265 (fun n ->
4266 conf.interpagespace <- n;
4267 docolumns conf.columns;
4268 let pageno, py =
4269 match state.layout with
4270 | [] -> 0, 0
4271 | l :: _ ->
4272 l.pageno, l.pagey
4274 state.maxy <- calcheight ();
4275 let y = getpagey pageno in
4276 gotoy (y + py)
4279 src#int "page bias"
4280 (fun () -> conf.pagebias)
4281 (fun v -> conf.pagebias <- v);
4283 src#int "scroll step"
4284 (fun () -> conf.scrollstep)
4285 (fun n -> conf.scrollstep <- n);
4287 src#int "horizontal scroll step"
4288 (fun () -> conf.hscrollstep)
4289 (fun v -> conf.hscrollstep <- v);
4291 src#int "auto scroll step"
4292 (fun () ->
4293 match state.autoscroll with
4294 | Some step -> step
4295 | _ -> conf.autoscrollstep)
4296 (fun n ->
4297 if state.autoscroll <> None
4298 then state.autoscroll <- Some n;
4299 conf.autoscrollstep <- n);
4301 src#int "zoom"
4302 (fun () -> truncate (conf.zoom *. 100.))
4303 (fun v -> setzoom ((float v) /. 100.));
4305 src#int "rotation"
4306 (fun () -> conf.angle)
4307 (fun v -> reqlayout v conf.proportional);
4309 src#int "scroll bar width"
4310 (fun () -> state.scrollw)
4311 (fun v ->
4312 state.scrollw <- v;
4313 conf.scrollbw <- v;
4314 reshape conf.winw conf.winh;
4317 src#int "scroll handle height"
4318 (fun () -> conf.scrollh)
4319 (fun v -> conf.scrollh <- v;);
4321 src#int "thumbnail width"
4322 (fun () -> conf.thumbw)
4323 (fun v ->
4324 conf.thumbw <- min 4096 v;
4325 match oldmode with
4326 | Birdseye beye ->
4327 leavebirdseye beye false;
4328 enterbirdseye ()
4329 | _ -> ()
4332 let mode = state.mode in
4333 src#string "columns"
4334 (fun () ->
4335 match conf.columns with
4336 | Csingle _ -> "1"
4337 | Cmulti (multi, _) -> multicolumns_to_string multi
4338 | Csplit (count, _) -> "-" ^ string_of_int count
4340 (fun v ->
4341 let n, a, b = multicolumns_of_string v in
4342 setcolumns mode n a b);
4344 sep ();
4345 src#caption "Presentation mode" 0;
4346 src#bool "scrollbar visible"
4347 (fun () -> conf.scrollbarinpm)
4348 (fun v ->
4349 if v != conf.scrollbarinpm
4350 then (
4351 conf.scrollbarinpm <- v;
4352 if conf.presentation
4353 then (
4354 state.scrollw <- if v then conf.scrollbw else 0;
4355 reshape conf.winw conf.winh;
4360 sep ();
4361 src#caption "Pixmap cache" 0;
4362 src#int_with_suffix "size (advisory)"
4363 (fun () -> conf.memlimit)
4364 (fun v -> conf.memlimit <- v);
4366 src#caption2 "used"
4367 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4368 (string_with_suffix_of_int state.memused)
4369 (Hashtbl.length state.tilemap)) 1;
4371 sep ();
4372 src#caption "Layout" 0;
4373 src#caption2 "Dimension"
4374 (fun () ->
4375 Printf.sprintf "%dx%d (virtual %dx%d)"
4376 conf.winw conf.winh
4377 state.w state.maxy)
4379 if conf.debug
4380 then
4381 src#caption2 "Position" (fun () ->
4382 Printf.sprintf "%dx%d" state.x state.y
4384 else
4385 src#caption2 "Visible" (fun () -> describe_location ()) 1
4388 sep ();
4389 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4390 "Save these parameters as global defaults at exit"
4391 (fun () -> conf.bedefault)
4392 (fun v -> conf.bedefault <- v)
4395 sep ();
4396 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4397 src#bool ~offset:0 ~btos "Extended parameters"
4398 (fun () -> !showextended)
4399 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4400 if !showextended
4401 then (
4402 src#bool "checkers"
4403 (fun () -> conf.checkers)
4404 (fun v -> conf.checkers <- v; setcheckers v);
4405 src#bool "update cursor"
4406 (fun () -> conf.updatecurs)
4407 (fun v -> conf.updatecurs <- v);
4408 src#bool "verbose"
4409 (fun () -> conf.verbose)
4410 (fun v -> conf.verbose <- v);
4411 src#bool "invert colors"
4412 (fun () -> conf.invert)
4413 (fun v -> conf.invert <- v);
4414 src#bool "max fit"
4415 (fun () -> conf.maxhfit)
4416 (fun v -> conf.maxhfit <- v);
4417 src#bool "redirect stderr"
4418 (fun () -> conf.redirectstderr)
4419 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4420 src#string "uri launcher"
4421 (fun () -> conf.urilauncher)
4422 (fun v -> conf.urilauncher <- v);
4423 src#string "path launcher"
4424 (fun () -> conf.pathlauncher)
4425 (fun v -> conf.pathlauncher <- v);
4426 src#string "tile size"
4427 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4428 (fun v ->
4430 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4431 conf.tilew <- max 64 w;
4432 conf.tileh <- max 64 h;
4433 flushtiles ();
4434 with exn ->
4435 state.text <- Printf.sprintf "bad tile size `%s': %s"
4436 v (Printexc.to_string exn));
4437 src#int "texture count"
4438 (fun () -> conf.texcount)
4439 (fun v ->
4440 if realloctexts v
4441 then conf.texcount <- v
4442 else showtext '!' " Failed to set texture count please retry later"
4444 src#int "slice height"
4445 (fun () -> conf.sliceheight)
4446 (fun v ->
4447 conf.sliceheight <- v;
4448 wcmd "sliceh %d" conf.sliceheight;
4450 src#int "anti-aliasing level"
4451 (fun () -> conf.aalevel)
4452 (fun v ->
4453 conf.aalevel <- bound v 0 8;
4454 state.anchor <- getanchor ();
4455 opendoc state.path state.password;
4457 src#string "page scroll scaling factor"
4458 (fun () -> string_of_float conf.pgscale)
4459 (fun v ->
4461 let s = float_of_string v in
4462 conf.pgscale <- s
4463 with exn ->
4464 state.text <- Printf.sprintf
4465 "bad page scroll scaling factor `%s': %s"
4466 v (Printexc.to_string exn)
4469 src#int "ui font size"
4470 (fun () -> fstate.fontsize)
4471 (fun v -> setfontsize (bound v 5 100));
4472 src#int "hint font size"
4473 (fun () -> conf.hfsize)
4474 (fun v -> conf.hfsize <- bound v 5 100);
4475 colorp "background color"
4476 (fun () -> conf.bgcolor)
4477 (fun v -> conf.bgcolor <- v);
4478 src#bool "crop hack"
4479 (fun () -> conf.crophack)
4480 (fun v -> conf.crophack <- v);
4481 src#string "trim fuzz"
4482 (fun () -> irect_to_string conf.trimfuzz)
4483 (fun v ->
4485 conf.trimfuzz <- irect_of_string v;
4486 if conf.trimmargins
4487 then settrim true conf.trimfuzz;
4488 with exn ->
4489 state.text <- Printf.sprintf "bad irect `%s': %s"
4490 v (Printexc.to_string exn)
4492 src#string "throttle"
4493 (fun () ->
4494 match conf.maxwait with
4495 | None -> "show place holder if page is not ready"
4496 | Some time ->
4497 if time = infinity
4498 then "wait for page to fully render"
4499 else
4500 "wait " ^ string_of_float time
4501 ^ " seconds before showing placeholder"
4503 (fun v ->
4505 let f = float_of_string v in
4506 if f <= 0.0
4507 then conf.maxwait <- None
4508 else conf.maxwait <- Some f
4509 with exn ->
4510 state.text <- Printf.sprintf "bad time `%s': %s"
4511 v (Printexc.to_string exn)
4513 src#string "ghyll scroll"
4514 (fun () ->
4515 match conf.ghyllscroll with
4516 | None -> ""
4517 | Some nab -> ghyllscroll_to_string nab
4519 (fun v ->
4521 let gs =
4522 if String.length v = 0
4523 then None
4524 else Some (ghyllscroll_of_string v)
4526 conf.ghyllscroll <- gs
4527 with exn ->
4528 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4529 v (Printexc.to_string exn)
4531 src#string "selection command"
4532 (fun () -> conf.selcmd)
4533 (fun v -> conf.selcmd <- v);
4534 src#colorspace "color space"
4535 (fun () -> colorspace_to_string conf.colorspace)
4536 (fun v ->
4537 conf.colorspace <- colorspace_of_int v;
4538 wcmd "cs %d" v;
4539 load state.layout;
4541 if pbousable ()
4542 then
4543 src#bool "use PBO"
4544 (fun () -> conf.usepbo)
4545 (fun v -> conf.usepbo <- v);
4546 src#bool "mouse wheel scrolls pages"
4547 (fun () -> conf.wheelbypage)
4548 (fun v -> conf.wheelbypage <- v);
4551 sep ();
4552 src#caption "Document" 0;
4553 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4554 src#caption2 "Pages"
4555 (fun () -> string_of_int state.pagecount) 1;
4556 src#caption2 "Dimensions"
4557 (fun () -> string_of_int (List.length state.pdims)) 1;
4558 if conf.trimmargins
4559 then (
4560 sep ();
4561 src#caption "Trimmed margins" 0;
4562 src#caption2 "Dimensions"
4563 (fun () -> string_of_int (List.length state.pdims)) 1;
4566 sep ();
4567 src#caption "OpenGL" 0;
4568 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4569 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4570 src#reset prevmode prevuioh;
4572 fun () ->
4573 state.text <- "";
4574 let prevmode = state.mode
4575 and prevuioh = state.uioh in
4576 fillsrc prevmode prevuioh;
4577 let source = (src :> lvsource) in
4578 let modehash = findkeyhash conf "info" in
4579 state.uioh <- coe (object (self)
4580 inherit listview ~source ~trusted:true ~modehash as super
4581 val mutable m_prevmemused = 0
4582 method infochanged = function
4583 | Memused ->
4584 if m_prevmemused != state.memused
4585 then (
4586 m_prevmemused <- state.memused;
4587 G.postRedisplay "memusedchanged";
4589 | Pdim -> G.postRedisplay "pdimchanged"
4590 | Docinfo -> fillsrc prevmode prevuioh
4592 method key key mask =
4593 if not (Wsi.withctrl mask)
4594 then
4595 match key with
4596 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4597 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4598 | _ -> super#key key mask
4599 else super#key key mask
4600 end);
4601 G.postRedisplay "info";
4604 let enterhelpmode =
4605 let source =
4606 (object
4607 inherit lvsourcebase
4608 method getitemcount = Array.length state.help
4609 method getitem n =
4610 let s, l, _ = state.help.(n) in
4611 (s, l)
4613 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4614 let optuioh =
4615 if not cancel
4616 then (
4617 m_qsearch <- qsearch;
4618 match state.help.(active) with
4619 | _, _, Action f -> Some (f uioh)
4620 | _ -> Some (uioh)
4622 else None
4624 m_active <- active;
4625 m_first <- first;
4626 m_pan <- pan;
4627 optuioh
4629 method hasaction n =
4630 match state.help.(n) with
4631 | _, _, Action _ -> true
4632 | _ -> false
4634 initializer
4635 m_active <- -1
4636 end)
4637 in fun () ->
4638 let modehash = findkeyhash conf "help" in
4639 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4640 G.postRedisplay "help";
4643 let entermsgsmode =
4644 let msgsource =
4645 let re = Str.regexp "[\r\n]" in
4646 (object
4647 inherit lvsourcebase
4648 val mutable m_items = [||]
4650 method getitemcount = 1 + Array.length m_items
4652 method getitem n =
4653 if n = 0
4654 then "[Clear]", 0
4655 else m_items.(n-1), 0
4657 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4658 ignore uioh;
4659 if not cancel
4660 then (
4661 if active = 0
4662 then Buffer.clear state.errmsgs;
4663 m_qsearch <- qsearch;
4665 m_active <- active;
4666 m_first <- first;
4667 m_pan <- pan;
4668 None
4670 method hasaction n =
4671 n = 0
4673 method reset =
4674 state.newerrmsgs <- false;
4675 let l = Str.split re (Buffer.contents state.errmsgs) in
4676 m_items <- Array.of_list l
4678 initializer
4679 m_active <- 0
4680 end)
4681 in fun () ->
4682 state.text <- "";
4683 msgsource#reset;
4684 let source = (msgsource :> lvsource) in
4685 let modehash = findkeyhash conf "listview" in
4686 state.uioh <- coe (object
4687 inherit listview ~source ~trusted:false ~modehash as super
4688 method display =
4689 if state.newerrmsgs
4690 then msgsource#reset;
4691 super#display
4692 end);
4693 G.postRedisplay "msgs";
4696 let quickbookmark ?title () =
4697 match state.layout with
4698 | [] -> ()
4699 | l :: _ ->
4700 let title =
4701 match title with
4702 | None ->
4703 let sec = Unix.gettimeofday () in
4704 let tm = Unix.localtime sec in
4705 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4706 (l.pageno+1)
4707 tm.Unix.tm_mday
4708 tm.Unix.tm_mon
4709 (tm.Unix.tm_year + 1900)
4710 tm.Unix.tm_hour
4711 tm.Unix.tm_min
4712 | Some title -> title
4714 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4717 let doreshape w h =
4718 state.fullscreen <- None;
4719 Wsi.reshape w h;
4722 let setautoscrollspeed step goingdown =
4723 let incr = max 1 ((abs step) / 2) in
4724 let incr = if goingdown then incr else -incr in
4725 let astep = step + incr in
4726 state.autoscroll <- Some astep;
4729 let gotounder = function
4730 | Ulinkgoto (pageno, top) ->
4731 if pageno >= 0
4732 then (
4733 addnav ();
4734 gotopage1 pageno top;
4737 | Ulinkuri s ->
4738 gotouri s
4740 | Uremote (filename, pageno) ->
4741 let path =
4742 if Sys.file_exists filename
4743 then filename
4744 else
4745 let dir = Filename.dirname state.path in
4746 let path = Filename.concat dir filename in
4747 if Sys.file_exists path
4748 then path
4749 else ""
4751 if String.length path > 0
4752 then (
4753 let anchor = getanchor () in
4754 let ranchor = state.path, state.password, anchor in
4755 state.anchor <- (pageno, 0.0, 0.0);
4756 state.ranchors <- ranchor :: state.ranchors;
4757 opendoc path "";
4759 else showtext '!' ("Could not find " ^ filename)
4761 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4764 let canpan () =
4765 match conf.columns with
4766 | Csplit _ -> true
4767 | _ -> conf.zoom > 1.0
4770 let existsinrow pageno (columns, coverA, coverB) p =
4771 let last = ((pageno - coverA) mod columns) + columns in
4772 let rec any = function
4773 | [] -> false
4774 | l :: rest ->
4775 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4776 then p l
4777 else (
4778 if not (p l)
4779 then (if l.pageno = last then false else any rest)
4780 else true
4783 any state.layout
4786 let nextpage () =
4787 match state.layout with
4788 | [] -> ()
4789 | l :: rest ->
4790 match conf.columns with
4791 | Csingle _ ->
4792 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4793 then
4794 let y = clamp (pgscale conf.winh) in
4795 gotoghyll y
4796 else
4797 let pageno = min (l.pageno+1) (state.pagecount-1) in
4798 gotoghyll (getpagey pageno)
4799 | Cmulti ((c, _, _) as cl, _) ->
4800 if conf.presentation
4801 && (existsinrow l.pageno cl
4802 (fun l -> l.pageh > l.pagey + l.pagevh))
4803 then
4804 let y = clamp (pgscale conf.winh) in
4805 gotoghyll y
4806 else
4807 let pageno = min (l.pageno+c) (state.pagecount-1) in
4808 gotoghyll (getpagey pageno)
4809 | Csplit (n, _) ->
4810 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4811 then
4812 let pagey, pageh = getpageyh l.pageno in
4813 let pagey = pagey + pageh * l.pagecol in
4814 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4815 gotoghyll (pagey + pageh + ips)
4818 let prevpage () =
4819 match state.layout with
4820 | [] -> ()
4821 | l :: _ ->
4822 match conf.columns with
4823 | Csingle _ ->
4824 if conf.presentation && l.pagey != 0
4825 then
4826 gotoghyll (clamp (pgscale ~-(conf.winh)))
4827 else
4828 let pageno = max 0 (l.pageno-1) in
4829 gotoghyll (getpagey pageno)
4830 | Cmulti ((c, _, coverB) as cl, _) ->
4831 if conf.presentation &&
4832 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4833 then
4834 gotoghyll (clamp (pgscale ~-(conf.winh)))
4835 else
4836 let decr =
4837 if l.pageno = state.pagecount - coverB
4838 then 1
4839 else c
4841 let pageno = max 0 (l.pageno-decr) in
4842 gotoghyll (getpagey pageno)
4843 | Csplit (n, _) ->
4844 let y =
4845 if l.pagecol = 0
4846 then
4847 if l.pageno = 0
4848 then l.pagey
4849 else
4850 let pageno = max 0 (l.pageno-1) in
4851 let pagey, pageh = getpageyh pageno in
4852 pagey + (n-1)*pageh
4853 else
4854 let pagey, pageh = getpageyh l.pageno in
4855 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4857 gotoghyll y
4860 let viewkeyboard key mask =
4861 let enttext te =
4862 let mode = state.mode in
4863 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4864 state.text <- "";
4865 enttext ();
4866 G.postRedisplay "view:enttext"
4868 let ctrl = Wsi.withctrl mask in
4869 let key =
4870 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4872 match key with
4873 | 81 -> (* Q *)
4874 exit 0
4876 | 0xff63 -> (* insert *)
4877 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4878 then (
4879 state.mode <- LinkNav (Ltgendir 0);
4880 gotoy state.y;
4882 else showtext '!' "Keyboard link navigation does not work under rotation"
4884 | 0xff1b | 113 -> (* escape / q *)
4885 begin match state.mstate with
4886 | Mzoomrect _ ->
4887 state.mstate <- Mnone;
4888 Wsi.setcursor Wsi.CURSOR_INHERIT;
4889 G.postRedisplay "kill zoom rect";
4890 | _ ->
4891 begin match state.mode with
4892 | LinkNav _ ->
4893 state.mode <- View;
4894 G.postRedisplay "esc leave linknav"
4895 | _ ->
4896 match state.ranchors with
4897 | [] -> raise Quit
4898 | (path, password, anchor) :: rest ->
4899 state.ranchors <- rest;
4900 state.anchor <- anchor;
4901 opendoc path password
4902 end;
4903 end;
4905 | 0xff08 -> (* backspace *)
4906 gotoghyll (getnav ~-1)
4908 | 111 -> (* o *)
4909 enteroutlinemode ()
4911 | 117 -> (* u *)
4912 state.rects <- [];
4913 state.text <- "";
4914 G.postRedisplay "dehighlight";
4916 | 47 | 63 -> (* / ? *)
4917 let ondone isforw s =
4918 cbput state.hists.pat s;
4919 state.searchpattern <- s;
4920 search s isforw
4922 let s = String.create 1 in
4923 s.[0] <- Char.chr key;
4924 enttext (s, "", Some (onhist state.hists.pat),
4925 textentry, ondone (key = 47), true)
4927 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4928 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4929 setzoom (conf.zoom +. incr)
4931 | 43 | 0xffab -> (* + *)
4932 let ondone s =
4933 let n =
4934 try int_of_string s with exc ->
4935 state.text <- Printf.sprintf "bad integer `%s': %s"
4936 s (Printexc.to_string exc);
4937 max_int
4939 if n != max_int
4940 then (
4941 conf.pagebias <- n;
4942 state.text <- "page bias is now " ^ string_of_int n;
4945 enttext ("page bias: ", "", None, intentry, ondone, true)
4947 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4948 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4949 setzoom (max 0.01 (conf.zoom -. decr))
4951 | 45 | 0xffad -> (* - *)
4952 let ondone msg = state.text <- msg in
4953 enttext (
4954 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4955 optentry state.mode, ondone, true
4958 | 48 when ctrl -> (* ctrl-0 *)
4959 setzoom 1.0
4961 | 49 when ctrl -> (* ctrl-1 *)
4962 let cols =
4963 match conf.columns with
4964 | Csingle _ | Cmulti _ -> 1
4965 | Csplit (n, _) -> n
4967 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4968 if zoom < 1.0
4969 then setzoom zoom
4971 | 0xffc6 -> (* f9 *)
4972 togglebirdseye ()
4974 | 57 when ctrl -> (* ctrl-9 *)
4975 togglebirdseye ()
4977 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4978 when not ctrl -> (* 0..9 *)
4979 let ondone s =
4980 let n =
4981 try int_of_string s with exc ->
4982 state.text <- Printf.sprintf "bad integer `%s': %s"
4983 s (Printexc.to_string exc);
4986 if n >= 0
4987 then (
4988 addnav ();
4989 cbput state.hists.pag (string_of_int n);
4990 gotopage1 (n + conf.pagebias - 1) 0;
4993 let pageentry text key =
4994 match Char.unsafe_chr key with
4995 | 'g' -> TEdone text
4996 | _ -> intentry text key
4998 let text = "x" in text.[0] <- Char.chr key;
4999 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5001 | 98 -> (* b *)
5002 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5003 reshape conf.winw conf.winh;
5005 | 108 -> (* l *)
5006 conf.hlinks <- not conf.hlinks;
5007 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5008 G.postRedisplay "toggle highlightlinks";
5010 | 70 -> (* F *)
5011 state.glinks <- true;
5012 let mode = state.mode in
5013 state.mode <- Textentry (
5014 (":", "", None, linknentry, linkndone gotounder, false),
5015 (fun _ ->
5016 state.glinks <- false;
5017 state.mode <- mode)
5019 state.text <- "";
5020 G.postRedisplay "view:linkent(F)"
5022 | 121 -> (* y *)
5023 state.glinks <- true;
5024 let mode = state.mode in
5025 state.mode <- Textentry (
5026 (":", "", None, linknentry, linkndone (fun under ->
5027 match Ne.pipe () with
5028 | Ne.Exn exn ->
5029 showtext '!' (Printf.sprintf "pipe failed: %s"
5030 (Printexc.to_string exn));
5031 | Ne.Res (r, w) ->
5032 let popened =
5033 try popen conf.selcmd [r, 0; w, -1]; true
5034 with exn ->
5035 showtext '!'
5036 (Printf.sprintf "failed to execute %s: %s"
5037 conf.selcmd (Printexc.to_string exn));
5038 false
5040 let clo cap fd =
5041 Ne.clo fd (fun msg ->
5042 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5045 let s = undertext under in
5046 if popened
5047 then
5048 (try
5049 let l = String.length s in
5050 let n = tempfailureretry (Unix.write w s 0) l in
5051 if n != l
5052 then
5053 showtext '!'
5054 (Printf.sprintf
5055 "failed to write %d characters to sel pipe, wrote %d"
5058 with exn ->
5059 showtext '!'
5060 (Printf.sprintf "failed to write to sel pipe: %s"
5061 (Printexc.to_string exn)
5064 else dolog "%s" s;
5065 clo "pipe/r" r;
5066 clo "pipe/w" w;
5067 ), false
5069 fun _ ->
5070 state.glinks <- false;
5071 state.mode <- mode
5073 state.text <- "";
5074 G.postRedisplay "view:linkent"
5076 | 97 -> (* a *)
5077 begin match state.autoscroll with
5078 | Some step ->
5079 conf.autoscrollstep <- step;
5080 state.autoscroll <- None
5081 | None ->
5082 if conf.autoscrollstep = 0
5083 then state.autoscroll <- Some 1
5084 else state.autoscroll <- Some conf.autoscrollstep
5087 | 112 when ctrl -> (* ctrl-p *)
5088 launchpath ()
5090 | 80 -> (* P *)
5091 setpresentationmode (not conf.presentation);
5092 showtext ' ' ("presentation mode " ^
5093 if conf.presentation then "on" else "off");
5095 | 102 -> (* f *)
5096 begin match state.fullscreen with
5097 | None ->
5098 state.fullscreen <- Some (conf.winw, conf.winh);
5099 Wsi.fullscreen ()
5100 | Some (w, h) ->
5101 state.fullscreen <- None;
5102 doreshape w h
5105 | 112 | 78 -> (* p|N *)
5106 search state.searchpattern false
5108 | 110 | 0xffc0 -> (* n|F3 *)
5109 search state.searchpattern true
5111 | 116 -> (* t *)
5112 begin match state.layout with
5113 | [] -> ()
5114 | l :: _ ->
5115 gotoy_and_clear_text (getpagey l.pageno)
5118 | 32 -> (* space *)
5119 nextpage ()
5121 | 0xff9f | 0xffff -> (* delete *)
5122 prevpage ()
5124 | 61 -> (* = *)
5125 showtext ' ' (describe_location ());
5127 | 119 -> (* w *)
5128 begin match state.layout with
5129 | [] -> ()
5130 | l :: _ ->
5131 doreshape (l.pagew + state.scrollw) l.pageh;
5132 G.postRedisplay "w"
5135 | 39 -> (* ' *)
5136 enterbookmarkmode ()
5138 | 104 | 0xffbe -> (* h|F1 *)
5139 enterhelpmode ()
5141 | 105 -> (* i *)
5142 enterinfomode ()
5144 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5145 entermsgsmode ()
5147 | 109 -> (* m *)
5148 let ondone s =
5149 match state.layout with
5150 | l :: _ ->
5151 if String.length s > 0
5152 then
5153 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5154 | _ -> ()
5156 enttext ("bookmark: ", "", None, textentry, ondone, true)
5158 | 126 -> (* ~ *)
5159 quickbookmark ();
5160 showtext ' ' "Quick bookmark added";
5162 | 122 -> (* z *)
5163 begin match state.layout with
5164 | l :: _ ->
5165 let rect = getpdimrect l.pagedimno in
5166 let w, h =
5167 if conf.crophack
5168 then
5169 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5170 truncate (1.2 *. (rect.(3) -. rect.(0))))
5171 else
5172 (truncate (rect.(1) -. rect.(0)),
5173 truncate (rect.(3) -. rect.(0)))
5175 let w = truncate ((float w)*.conf.zoom)
5176 and h = truncate ((float h)*.conf.zoom) in
5177 if w != 0 && h != 0
5178 then (
5179 state.anchor <- getanchor ();
5180 doreshape (w + state.scrollw) (h + conf.interpagespace)
5182 G.postRedisplay "z";
5184 | [] -> ()
5187 | 50 when ctrl -> (* ctrl-2 *)
5188 let maxw = getmaxw () in
5189 if maxw > 0.0
5190 then setzoom (maxw /. float conf.winw)
5192 | 60 | 62 -> (* < > *)
5193 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5195 | 91 | 93 -> (* [ ] *)
5196 conf.colorscale <-
5197 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5199 G.postRedisplay "brightness";
5201 | 99 when state.mode = View -> (* c *)
5202 let (c, a, b), z =
5203 match state.prevcolumns with
5204 | None -> (1, 0, 0), 1.0
5205 | Some (columns, z) ->
5206 let cab =
5207 match columns with
5208 | Csplit (c, _) -> -c, 0, 0
5209 | Cmulti ((c, a, b), _) -> c, a, b
5210 | Csingle _ -> 1, 0, 0
5212 cab, z
5214 setcolumns View c a b;
5215 setzoom z;
5217 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5218 setzoom state.prevzoom
5220 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5221 begin match state.autoscroll with
5222 | None ->
5223 begin match state.mode with
5224 | Birdseye beye -> upbirdseye 1 beye
5225 | _ ->
5226 if ctrl
5227 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5228 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5230 | Some n ->
5231 setautoscrollspeed n false
5234 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5235 begin match state.autoscroll with
5236 | None ->
5237 begin match state.mode with
5238 | Birdseye beye -> downbirdseye 1 beye
5239 | _ ->
5240 if ctrl
5241 then gotoy_and_clear_text (clamp (conf.winh/2))
5242 else gotoy_and_clear_text (clamp conf.scrollstep)
5244 | Some n ->
5245 setautoscrollspeed n true
5248 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5249 if canpan ()
5250 then
5251 let dx =
5252 if ctrl
5253 then conf.winw / 2
5254 else conf.hscrollstep
5256 let dx = if key = 0xff51 then dx else -dx in
5257 state.x <- state.x + dx;
5258 gotoy_and_clear_text state.y
5259 else (
5260 state.text <- "";
5261 G.postRedisplay "lef/right"
5264 | 0xff55 | 0xff9a -> (* (kp) prior *)
5265 let y =
5266 if ctrl
5267 then
5268 match state.layout with
5269 | [] -> state.y
5270 | l :: _ -> state.y - l.pagey
5271 else
5272 clamp (pgscale (-conf.winh))
5274 gotoghyll y
5276 | 0xff56 | 0xff9b -> (* (kp) next *)
5277 let y =
5278 if ctrl
5279 then
5280 match List.rev state.layout with
5281 | [] -> state.y
5282 | l :: _ -> getpagey l.pageno
5283 else
5284 clamp (pgscale conf.winh)
5286 gotoghyll y
5288 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5289 gotoghyll 0
5290 | 71 | 0xff57 | 0xff9c -> (* G end *)
5291 gotoghyll (clamp state.maxy)
5293 | 0xff53 when Wsi.withalt mask -> (* alt-right *)
5294 gotoghyll (getnav 1)
5295 | 0xff51 when Wsi.withalt mask -> (* alt-left *)
5296 gotoghyll (getnav ~-1)
5298 | 114 -> (* r *)
5299 reload ()
5301 | 118 when conf.debug -> (* v *)
5302 state.rects <- [];
5303 List.iter (fun l ->
5304 match getopaque l.pageno with
5305 | None -> ()
5306 | Some opaque ->
5307 let x0, y0, x1, y1 = pagebbox opaque in
5308 let a,b = float x0, float y0 in
5309 let c,d = float x1, float y0 in
5310 let e,f = float x1, float y1 in
5311 let h,j = float x0, float y1 in
5312 let rect = (a,b,c,d,e,f,h,j) in
5313 debugrect rect;
5314 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5315 ) state.layout;
5316 G.postRedisplay "v";
5318 | _ ->
5319 vlog "huh? %s" (Wsi.keyname key)
5322 let linknavkeyboard key mask linknav =
5323 let getpage pageno =
5324 let rec loop = function
5325 | [] -> None
5326 | l :: _ when l.pageno = pageno -> Some l
5327 | _ :: rest -> loop rest
5328 in loop state.layout
5330 let doexact (pageno, n) =
5331 match getopaque pageno, getpage pageno with
5332 | Some opaque, Some l ->
5333 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5334 then
5335 let under = getlink opaque n in
5336 G.postRedisplay "link gotounder";
5337 gotounder under;
5338 state.mode <- View;
5339 else
5340 let opt, dir =
5341 match key with
5342 | 0xff50 -> (* home *)
5343 Some (findlink opaque LDfirst), -1
5345 | 0xff57 -> (* end *)
5346 Some (findlink opaque LDlast), 1
5348 | 0xff51 -> (* left *)
5349 Some (findlink opaque (LDleft n)), -1
5351 | 0xff53 -> (* right *)
5352 Some (findlink opaque (LDright n)), 1
5354 | 0xff52 -> (* up *)
5355 Some (findlink opaque (LDup n)), -1
5357 | 0xff54 -> (* down *)
5358 Some (findlink opaque (LDdown n)), 1
5360 | _ -> None, 0
5362 let pwl l dir =
5363 begin match findpwl l.pageno dir with
5364 | Pwlnotfound -> ()
5365 | Pwl pageno ->
5366 let notfound dir =
5367 state.mode <- LinkNav (Ltgendir dir);
5368 let y, h = getpageyh pageno in
5369 let y =
5370 if dir < 0
5371 then y + h - conf.winh
5372 else y
5374 gotoy y
5376 begin match getopaque pageno, getpage pageno with
5377 | Some opaque, Some _ ->
5378 let link =
5379 let ld = if dir > 0 then LDfirst else LDlast in
5380 findlink opaque ld
5382 begin match link with
5383 | Lfound m ->
5384 showlinktype (getlink opaque m);
5385 state.mode <- LinkNav (Ltexact (pageno, m));
5386 G.postRedisplay "linknav jpage";
5387 | _ -> notfound dir
5388 end;
5389 | _ -> notfound dir
5390 end;
5391 end;
5393 begin match opt with
5394 | Some Lnotfound -> pwl l dir;
5395 | Some (Lfound m) ->
5396 if m = n
5397 then pwl l dir
5398 else (
5399 let _, y0, _, y1 = getlinkrect opaque m in
5400 if y0 < l.pagey
5401 then gotopage1 l.pageno y0
5402 else (
5403 let d = fstate.fontsize + 1 in
5404 if y1 - l.pagey > l.pagevh - d
5405 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5406 else G.postRedisplay "linknav";
5408 showlinktype (getlink opaque m);
5409 state.mode <- LinkNav (Ltexact (l.pageno, m));
5412 | None -> viewkeyboard key mask
5413 end;
5414 | _ -> viewkeyboard key mask
5416 if key = 0xff63
5417 then (
5418 state.mode <- View;
5419 G.postRedisplay "leave linknav"
5421 else
5422 match linknav with
5423 | Ltgendir _ -> viewkeyboard key mask
5424 | Ltexact exact -> doexact exact
5427 let keyboard key mask =
5428 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5429 then wcmd "interrupt"
5430 else state.uioh <- state.uioh#key key mask
5433 let birdseyekeyboard key mask
5434 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5435 let incr =
5436 match conf.columns with
5437 | Csingle _ -> 1
5438 | Cmulti ((c, _, _), _) -> c
5439 | Csplit _ -> failwith "bird's eye split mode"
5441 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5442 match key with
5443 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5444 let y, h = getpageyh pageno in
5445 let top = (conf.winh - h) / 2 in
5446 gotoy (max 0 (y - top))
5447 | 0xff0d (* enter *)
5448 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5449 | 0xff1b -> leavebirdseye beye true (* escape *)
5450 | 0xff52 -> upbirdseye incr beye (* up *)
5451 | 0xff54 -> downbirdseye incr beye (* down *)
5452 | 0xff51 -> upbirdseye 1 beye (* left *)
5453 | 0xff53 -> downbirdseye 1 beye (* right *)
5455 | 0xff55 -> (* prior *)
5456 begin match state.layout with
5457 | l :: _ ->
5458 if l.pagey != 0
5459 then (
5460 state.mode <- Birdseye (
5461 oconf, leftx, l.pageno, hooverpageno, anchor
5463 gotopage1 l.pageno 0;
5465 else (
5466 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5467 match layout with
5468 | [] -> gotoy (clamp (-conf.winh))
5469 | l :: _ ->
5470 state.mode <- Birdseye (
5471 oconf, leftx, l.pageno, hooverpageno, anchor
5473 gotopage1 l.pageno 0
5476 | [] -> gotoy (clamp (-conf.winh))
5477 end;
5479 | 0xff56 -> (* next *)
5480 begin match List.rev state.layout with
5481 | l :: _ ->
5482 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5483 begin match layout with
5484 | [] ->
5485 let incr = l.pageh - l.pagevh in
5486 if incr = 0
5487 then (
5488 state.mode <-
5489 Birdseye (
5490 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5492 G.postRedisplay "birdseye pagedown";
5494 else gotoy (clamp (incr + conf.interpagespace*2));
5496 | l :: _ ->
5497 state.mode <-
5498 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5499 gotopage1 l.pageno 0;
5502 | [] -> gotoy (clamp conf.winh)
5503 end;
5505 | 0xff50 -> (* home *)
5506 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5507 gotopage1 0 0
5509 | 0xff57 -> (* end *)
5510 let pageno = state.pagecount - 1 in
5511 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5512 if not (pagevisible state.layout pageno)
5513 then
5514 let h =
5515 match List.rev state.pdims with
5516 | [] -> conf.winh
5517 | (_, _, h, _) :: _ -> h
5519 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5520 else G.postRedisplay "birdseye end";
5521 | _ -> viewkeyboard key mask
5524 let drawpage l linkindexbase =
5525 let color =
5526 match state.mode with
5527 | Textentry _ -> scalecolor 0.4
5528 | LinkNav _
5529 | View -> scalecolor 1.0
5530 | Birdseye (_, _, pageno, hooverpageno, _) ->
5531 if l.pageno = hooverpageno
5532 then scalecolor 0.9
5533 else (
5534 if l.pageno = pageno
5535 then scalecolor 1.0
5536 else scalecolor 0.8
5539 drawtiles l color;
5540 begin match getopaque l.pageno with
5541 | Some opaque ->
5542 if tileready l l.pagex l.pagey
5543 then
5544 let x = l.pagedispx - l.pagex
5545 and y = l.pagedispy - l.pagey in
5546 let hlmask =
5547 match conf.columns with
5548 | Csingle _ | Cmulti _ ->
5549 (if conf.hlinks then 1 else 0)
5550 + (if state.glinks
5551 && not (isbirdseye state.mode) then 2 else 0)
5552 | _ -> 0
5554 let s =
5555 match state.mode with
5556 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5557 | _ -> ""
5559 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5560 else 0
5562 | _ -> 0
5563 end;
5566 let scrollindicator () =
5567 let sbw, ph, sh = state.uioh#scrollph in
5568 let sbh, pw, sw = state.uioh#scrollpw in
5570 GlDraw.color (0.64, 0.64, 0.64);
5571 GlDraw.rect
5572 (float (conf.winw - sbw), 0.)
5573 (float conf.winw, float conf.winh)
5575 GlDraw.rect
5576 (0., float (conf.winh - sbh))
5577 (float (conf.winw - state.scrollw - 1), float conf.winh)
5579 GlDraw.color (0.0, 0.0, 0.0);
5581 GlDraw.rect
5582 (float (conf.winw - sbw), ph)
5583 (float conf.winw, ph +. sh)
5585 GlDraw.rect
5586 (pw, float (conf.winh - sbh))
5587 (pw +. sw, float conf.winh)
5591 let showsel () =
5592 match state.mstate with
5593 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5596 | Msel ((x0, y0), (x1, y1)) ->
5597 let rec loop = function
5598 | l :: ls ->
5599 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5600 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5601 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5602 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5603 then
5604 match getopaque l.pageno with
5605 | Some opaque ->
5606 let x0, y0 = pagetranslatepoint l x0 y0 in
5607 let x1, y1 = pagetranslatepoint l x1 y1 in
5608 seltext opaque (x0, y0, x1, y1);
5609 | _ -> ()
5610 else loop ls
5611 | [] -> ()
5613 loop state.layout
5616 let showrects rects =
5617 Gl.enable `blend;
5618 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5619 GlDraw.polygon_mode `both `fill;
5620 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5621 List.iter
5622 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5623 List.iter (fun l ->
5624 if l.pageno = pageno
5625 then (
5626 let dx = float (l.pagedispx - l.pagex) in
5627 let dy = float (l.pagedispy - l.pagey) in
5628 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5629 GlDraw.begins `quads;
5631 GlDraw.vertex2 (x0+.dx, y0+.dy);
5632 GlDraw.vertex2 (x1+.dx, y1+.dy);
5633 GlDraw.vertex2 (x2+.dx, y2+.dy);
5634 GlDraw.vertex2 (x3+.dx, y3+.dy);
5636 GlDraw.ends ();
5638 ) state.layout
5639 ) rects
5641 Gl.disable `blend;
5644 let display () =
5645 GlClear.color (scalecolor2 conf.bgcolor);
5646 GlClear.clear [`color];
5647 let rec loop linkindexbase = function
5648 | l :: rest ->
5649 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5650 loop linkindexbase rest
5651 | [] -> ()
5653 loop 0 state.layout;
5654 let rects =
5655 match state.mode with
5656 | LinkNav (Ltexact (pageno, linkno)) ->
5657 begin match getopaque pageno with
5658 | Some opaque ->
5659 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5660 (pageno, 5, (
5661 float x0, float y0,
5662 float x1, float y0,
5663 float x1, float y1,
5664 float x0, float y1)
5665 ) :: state.rects
5666 | None -> state.rects
5668 | _ -> state.rects
5670 showrects rects;
5671 showsel ();
5672 state.uioh#display;
5673 begin match state.mstate with
5674 | Mzoomrect ((x0, y0), (x1, y1)) ->
5675 Gl.enable `blend;
5676 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5677 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5678 GlDraw.rect (float x0, float y0)
5679 (float x1, float y1);
5680 Gl.disable `blend;
5681 | _ -> ()
5682 end;
5683 enttext ();
5684 scrollindicator ();
5685 if not state.wthack then Wsi.swapb ();
5688 let zoomrect x y x1 y1 =
5689 let x0 = min x x1
5690 and x1 = max x x1
5691 and y0 = min y y1 in
5692 gotoy (state.y + y0);
5693 state.anchor <- getanchor ();
5694 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5695 let margin =
5696 if state.w < conf.winw - state.scrollw
5697 then (conf.winw - state.scrollw - state.w) / 2
5698 else 0
5700 state.x <- (state.x + margin) - x0;
5701 setzoom zoom;
5702 Wsi.setcursor Wsi.CURSOR_INHERIT;
5703 state.mstate <- Mnone;
5706 let scrollx x =
5707 let winw = conf.winw - state.scrollw - 1 in
5708 let s = float x /. float winw in
5709 let destx = truncate (float (state.w + winw) *. s) in
5710 state.x <- winw - destx;
5711 gotoy_and_clear_text state.y;
5712 state.mstate <- Mscrollx;
5715 let scrolly y =
5716 let s = float y /. float conf.winh in
5717 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5718 gotoy_and_clear_text desty;
5719 state.mstate <- Mscrolly;
5722 let viewmouse button down x y mask =
5723 match button with
5724 | n when (n == 4 || n == 5) && not down ->
5725 if Wsi.withctrl mask
5726 then (
5727 match state.mstate with
5728 | Mzoom (oldn, i) ->
5729 if oldn = n
5730 then (
5731 if i = 2
5732 then
5733 let incr =
5734 match n with
5735 | 5 ->
5736 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5737 | _ ->
5738 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5740 let zoom = conf.zoom -. incr in
5741 setzoom zoom;
5742 state.mstate <- Mzoom (n, 0);
5743 else
5744 state.mstate <- Mzoom (n, i+1);
5746 else state.mstate <- Mzoom (n, 0)
5748 | _ -> state.mstate <- Mzoom (n, 0)
5750 else (
5751 match state.autoscroll with
5752 | Some step -> setautoscrollspeed step (n=4)
5753 | None ->
5754 if conf.wheelbypage
5755 then (
5756 if n = 4
5757 then nextpage ()
5758 else prevpage ()
5760 else
5761 let incr =
5762 if n = 4
5763 then -conf.scrollstep
5764 else conf.scrollstep
5766 let incr = incr * 2 in
5767 let y = clamp incr in
5768 gotoy_and_clear_text y
5771 | n when (n = 6 || n = 7) && not down && canpan () ->
5772 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5773 gotoy_and_clear_text state.y
5775 | 1 when Wsi.withctrl mask ->
5776 if down
5777 then (
5778 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5779 state.mstate <- Mpan (x, y)
5781 else
5782 state.mstate <- Mnone
5784 | 3 ->
5785 if down
5786 then (
5787 Wsi.setcursor Wsi.CURSOR_CYCLE;
5788 let p = (x, y) in
5789 state.mstate <- Mzoomrect (p, p)
5791 else (
5792 match state.mstate with
5793 | Mzoomrect ((x0, y0), _) ->
5794 if abs (x-x0) > 10 && abs (y - y0) > 10
5795 then zoomrect x0 y0 x y
5796 else (
5797 state.mstate <- Mnone;
5798 Wsi.setcursor Wsi.CURSOR_INHERIT;
5799 G.postRedisplay "kill accidental zoom rect";
5801 | _ ->
5802 Wsi.setcursor Wsi.CURSOR_INHERIT;
5803 state.mstate <- Mnone
5806 | 1 when x > conf.winw - state.scrollw ->
5807 if down
5808 then
5809 let _, position, sh = state.uioh#scrollph in
5810 if y > truncate position && y < truncate (position +. sh)
5811 then state.mstate <- Mscrolly
5812 else scrolly y
5813 else
5814 state.mstate <- Mnone
5816 | 1 when y > conf.winh - state.hscrollh ->
5817 if down
5818 then
5819 let _, position, sw = state.uioh#scrollpw in
5820 if x > truncate position && x < truncate (position +. sw)
5821 then state.mstate <- Mscrollx
5822 else scrollx x
5823 else
5824 state.mstate <- Mnone
5826 | 1 ->
5827 let dest = if down then getunder x y else Unone in
5828 begin match dest with
5829 | Ulinkgoto _
5830 | Ulinkuri _
5831 | Uremote _
5832 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5833 gotounder dest
5835 | Unone when down ->
5836 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5837 state.mstate <- Mpan (x, y);
5839 | Unone | Utext _ ->
5840 if down
5841 then (
5842 if conf.angle mod 360 = 0
5843 then (
5844 state.mstate <- Msel ((x, y), (x, y));
5845 G.postRedisplay "mouse select";
5848 else (
5849 match state.mstate with
5850 | Mnone -> ()
5852 | Mzoom _ | Mscrollx | Mscrolly ->
5853 state.mstate <- Mnone
5855 | Mzoomrect ((x0, y0), _) ->
5856 zoomrect x0 y0 x y
5858 | Mpan _ ->
5859 Wsi.setcursor Wsi.CURSOR_INHERIT;
5860 state.mstate <- Mnone
5862 | Msel ((x0, y0), (x1, y1)) ->
5863 let rec loop = function
5864 | [] -> ()
5865 | l :: rest ->
5866 let inside =
5867 let a0 = l.pagedispy in
5868 let a1 = a0 + l.pagevh in
5869 let b0 = l.pagedispx in
5870 let b1 = b0 + l.pagevw in
5871 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5872 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5874 if inside
5875 then
5876 match getopaque l.pageno with
5877 | Some opaque ->
5878 begin
5879 match Ne.pipe () with
5880 | Ne.Exn exn ->
5881 showtext '!'
5882 (Printf.sprintf
5883 "can not create sel pipe: %s"
5884 (Printexc.to_string exn));
5885 | Ne.Res (r, w) ->
5886 let doclose what fd =
5887 Ne.clo fd (fun msg ->
5888 dolog "%s close failed: %s" what msg)
5891 popen conf.selcmd [r, 0; w, -1];
5892 copysel w opaque;
5893 doclose "pipe/r" r;
5894 G.postRedisplay "copysel";
5895 with exn ->
5896 dolog "can not execute %S: %s"
5897 conf.selcmd (Printexc.to_string exn);
5898 doclose "pipe/r" r;
5899 doclose "pipe/w" w;
5901 | None -> ()
5902 else loop rest
5904 loop state.layout;
5905 Wsi.setcursor Wsi.CURSOR_INHERIT;
5906 state.mstate <- Mnone;
5910 | _ -> ()
5913 let birdseyemouse button down x y mask
5914 (conf, leftx, _, hooverpageno, anchor) =
5915 match button with
5916 | 1 when down ->
5917 let rec loop = function
5918 | [] -> ()
5919 | l :: rest ->
5920 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5921 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5922 then (
5923 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5925 else loop rest
5927 loop state.layout
5928 | 3 -> ()
5929 | _ -> viewmouse button down x y mask
5932 let mouse button down x y mask =
5933 state.uioh <- state.uioh#button button down x y mask;
5936 let motion ~x ~y =
5937 state.uioh <- state.uioh#motion x y
5940 let pmotion ~x ~y =
5941 state.uioh <- state.uioh#pmotion x y;
5944 let uioh = object
5945 method display = ()
5947 method key key mask =
5948 begin match state.mode with
5949 | Textentry textentry -> textentrykeyboard key mask textentry
5950 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5951 | View -> viewkeyboard key mask
5952 | LinkNav linknav -> linknavkeyboard key mask linknav
5953 end;
5954 state.uioh
5956 method button button bstate x y mask =
5957 begin match state.mode with
5958 | LinkNav _
5959 | View -> viewmouse button bstate x y mask
5960 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5961 | Textentry _ -> ()
5962 end;
5963 state.uioh
5965 method motion x y =
5966 begin match state.mode with
5967 | Textentry _ -> ()
5968 | View | Birdseye _ | LinkNav _ ->
5969 match state.mstate with
5970 | Mzoom _ | Mnone -> ()
5972 | Mpan (x0, y0) ->
5973 let dx = x - x0
5974 and dy = y0 - y in
5975 state.mstate <- Mpan (x, y);
5976 if canpan ()
5977 then state.x <- state.x + dx;
5978 let y = clamp dy in
5979 gotoy_and_clear_text y
5981 | Msel (a, _) ->
5982 state.mstate <- Msel (a, (x, y));
5983 G.postRedisplay "motion select";
5985 | Mscrolly ->
5986 let y = min conf.winh (max 0 y) in
5987 scrolly y
5989 | Mscrollx ->
5990 let x = min conf.winw (max 0 x) in
5991 scrollx x
5993 | Mzoomrect (p0, _) ->
5994 state.mstate <- Mzoomrect (p0, (x, y));
5995 G.postRedisplay "motion zoomrect";
5996 end;
5997 state.uioh
5999 method pmotion x y =
6000 begin match state.mode with
6001 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6002 let rec loop = function
6003 | [] ->
6004 if hooverpageno != -1
6005 then (
6006 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6007 G.postRedisplay "pmotion birdseye no hoover";
6009 | l :: rest ->
6010 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6011 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6012 then (
6013 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6014 G.postRedisplay "pmotion birdseye hoover";
6016 else loop rest
6018 loop state.layout
6020 | Textentry _ -> ()
6022 | LinkNav _
6023 | View ->
6024 match state.mstate with
6025 | Mnone -> updateunder x y
6026 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6028 end;
6029 state.uioh
6031 method infochanged _ = ()
6033 method scrollph =
6034 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
6035 let p, h = scrollph state.y maxy in
6036 state.scrollw, p, h
6038 method scrollpw =
6039 let winw = conf.winw - state.scrollw - 1 in
6040 let fwinw = float winw in
6041 let sw =
6042 let sw = fwinw /. float state.w in
6043 let sw = fwinw *. sw in
6044 max sw (float conf.scrollh)
6046 let position, sw =
6047 let f = state.w+winw in
6048 let r = float (winw-state.x) /. float f in
6049 let p = fwinw *. r in
6050 p-.sw/.2., sw
6052 let sw =
6053 if position +. sw > fwinw
6054 then fwinw -. position
6055 else sw
6057 state.hscrollh, position, sw
6059 method modehash =
6060 let modename =
6061 match state.mode with
6062 | LinkNav _ -> "links"
6063 | Textentry _ -> "textentry"
6064 | Birdseye _ -> "birdseye"
6065 | View -> "view"
6067 findkeyhash conf modename
6068 end;;
6070 module Config =
6071 struct
6072 open Parser
6074 let fontpath = ref "";;
6076 module KeyMap =
6077 Map.Make (struct type t = (int * int) let compare = compare end);;
6079 let unent s =
6080 let l = String.length s in
6081 let b = Buffer.create l in
6082 unent b s 0 l;
6083 Buffer.contents b;
6086 let home =
6087 try Sys.getenv "HOME"
6088 with exn ->
6089 prerr_endline
6090 ("Can not determine home directory location: " ^
6091 Printexc.to_string exn);
6095 let modifier_of_string = function
6096 | "alt" -> Wsi.altmask
6097 | "shift" -> Wsi.shiftmask
6098 | "ctrl" | "control" -> Wsi.ctrlmask
6099 | "meta" -> Wsi.metamask
6100 | _ -> 0
6103 let key_of_string =
6104 let r = Str.regexp "-" in
6105 fun s ->
6106 let elems = Str.full_split r s in
6107 let f n k m =
6108 let g s =
6109 let m1 = modifier_of_string s in
6110 if m1 = 0
6111 then (Wsi.namekey s, m)
6112 else (k, m lor m1)
6113 in function
6114 | Str.Delim s when n land 1 = 0 -> g s
6115 | Str.Text s -> g s
6116 | Str.Delim _ -> (k, m)
6118 let rec loop n k m = function
6119 | [] -> (k, m)
6120 | x :: xs ->
6121 let k, m = f n k m x in
6122 loop (n+1) k m xs
6124 loop 0 0 0 elems
6127 let keys_of_string =
6128 let r = Str.regexp "[ \t]" in
6129 fun s ->
6130 let elems = Str.split r s in
6131 List.map key_of_string elems
6134 let copykeyhashes c =
6135 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6138 let config_of c attrs =
6139 let apply c k v =
6141 match k with
6142 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6143 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6144 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6145 | "preload" -> { c with preload = bool_of_string v }
6146 | "page-bias" -> { c with pagebias = int_of_string v }
6147 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6148 | "horizontal-scroll-step" ->
6149 { c with hscrollstep = max (int_of_string v) 1 }
6150 | "auto-scroll-step" ->
6151 { c with autoscrollstep = max 0 (int_of_string v) }
6152 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6153 | "crop-hack" -> { c with crophack = bool_of_string v }
6154 | "throttle" ->
6155 let mw =
6156 match String.lowercase v with
6157 | "true" -> Some infinity
6158 | "false" -> None
6159 | f -> Some (float_of_string f)
6161 { c with maxwait = mw}
6162 | "highlight-links" -> { c with hlinks = bool_of_string v }
6163 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6164 | "vertical-margin" ->
6165 { c with interpagespace = max 0 (int_of_string v) }
6166 | "zoom" ->
6167 let zoom = float_of_string v /. 100. in
6168 let zoom = max zoom 0.0 in
6169 { c with zoom = zoom }
6170 | "presentation" -> { c with presentation = bool_of_string v }
6171 | "rotation-angle" -> { c with angle = int_of_string v }
6172 | "width" -> { c with winw = max 20 (int_of_string v) }
6173 | "height" -> { c with winh = max 20 (int_of_string v) }
6174 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6175 | "proportional-display" -> { c with proportional = bool_of_string v }
6176 | "pixmap-cache-size" ->
6177 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6178 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6179 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6180 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6181 | "persistent-location" -> { c with jumpback = bool_of_string v }
6182 | "background-color" -> { c with bgcolor = color_of_string v }
6183 | "scrollbar-in-presentation" ->
6184 { c with scrollbarinpm = bool_of_string v }
6185 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6186 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6187 | "mupdf-store-size" ->
6188 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6189 | "checkers" -> { c with checkers = bool_of_string v }
6190 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6191 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6192 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6193 | "uri-launcher" -> { c with urilauncher = unent v }
6194 | "path-launcher" -> { c with pathlauncher = unent v }
6195 | "color-space" -> { c with colorspace = colorspace_of_string v }
6196 | "invert-colors" -> { c with invert = bool_of_string v }
6197 | "brightness" -> { c with colorscale = float_of_string v }
6198 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6199 | "ghyllscroll" ->
6200 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6201 | "columns" ->
6202 let (n, _, _) as nab = multicolumns_of_string v in
6203 if n < 0
6204 then { c with columns = Csplit (-n, [||]) }
6205 else { c with columns = Cmulti (nab, [||]) }
6206 | "birds-eye-columns" ->
6207 { c with beyecolumns = Some (max (int_of_string v) 2) }
6208 | "selection-command" -> { c with selcmd = unent v }
6209 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6210 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6211 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6212 | "use-pbo" -> { c with usepbo = bool_of_string v }
6213 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6214 | _ -> c
6215 with exn ->
6216 prerr_endline ("Error processing attribute (`" ^
6217 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6220 let rec fold c = function
6221 | [] -> c
6222 | (k, v) :: rest ->
6223 let c = apply c k v in
6224 fold c rest
6226 fold { c with keyhashes = copykeyhashes c } attrs;
6229 let fromstring f pos n v d =
6230 try f v
6231 with exn ->
6232 dolog "Error processing attribute (%S=%S) at %d\n%s"
6233 n v pos (Printexc.to_string exn)
6238 let bookmark_of attrs =
6239 let rec fold title page rely visy = function
6240 | ("title", v) :: rest -> fold v page rely visy rest
6241 | ("page", v) :: rest -> fold title v rely visy rest
6242 | ("rely", v) :: rest -> fold title page v visy rest
6243 | ("visy", v) :: rest -> fold title page rely v rest
6244 | _ :: rest -> fold title page rely visy rest
6245 | [] -> title, page, rely, visy
6247 fold "invalid" "0" "0" "0" attrs
6250 let doc_of attrs =
6251 let rec fold path page rely pan visy = function
6252 | ("path", v) :: rest -> fold v page rely pan visy rest
6253 | ("page", v) :: rest -> fold path v rely pan visy rest
6254 | ("rely", v) :: rest -> fold path page v pan visy rest
6255 | ("pan", v) :: rest -> fold path page rely v visy rest
6256 | ("visy", v) :: rest -> fold path page rely pan v rest
6257 | _ :: rest -> fold path page rely pan visy rest
6258 | [] -> path, page, rely, pan, visy
6260 fold "" "0" "0" "0" "0" attrs
6263 let map_of attrs =
6264 let rec fold rs ls = function
6265 | ("out", v) :: rest -> fold v ls rest
6266 | ("in", v) :: rest -> fold rs v rest
6267 | _ :: rest -> fold ls rs rest
6268 | [] -> ls, rs
6270 fold "" "" attrs
6273 let setconf dst src =
6274 dst.scrollbw <- src.scrollbw;
6275 dst.scrollh <- src.scrollh;
6276 dst.icase <- src.icase;
6277 dst.preload <- src.preload;
6278 dst.pagebias <- src.pagebias;
6279 dst.verbose <- src.verbose;
6280 dst.scrollstep <- src.scrollstep;
6281 dst.maxhfit <- src.maxhfit;
6282 dst.crophack <- src.crophack;
6283 dst.autoscrollstep <- src.autoscrollstep;
6284 dst.maxwait <- src.maxwait;
6285 dst.hlinks <- src.hlinks;
6286 dst.underinfo <- src.underinfo;
6287 dst.interpagespace <- src.interpagespace;
6288 dst.zoom <- src.zoom;
6289 dst.presentation <- src.presentation;
6290 dst.angle <- src.angle;
6291 dst.winw <- src.winw;
6292 dst.winh <- src.winh;
6293 dst.savebmarks <- src.savebmarks;
6294 dst.memlimit <- src.memlimit;
6295 dst.proportional <- src.proportional;
6296 dst.texcount <- src.texcount;
6297 dst.sliceheight <- src.sliceheight;
6298 dst.thumbw <- src.thumbw;
6299 dst.jumpback <- src.jumpback;
6300 dst.bgcolor <- src.bgcolor;
6301 dst.scrollbarinpm <- src.scrollbarinpm;
6302 dst.tilew <- src.tilew;
6303 dst.tileh <- src.tileh;
6304 dst.mustoresize <- src.mustoresize;
6305 dst.checkers <- src.checkers;
6306 dst.aalevel <- src.aalevel;
6307 dst.trimmargins <- src.trimmargins;
6308 dst.trimfuzz <- src.trimfuzz;
6309 dst.urilauncher <- src.urilauncher;
6310 dst.colorspace <- src.colorspace;
6311 dst.invert <- src.invert;
6312 dst.colorscale <- src.colorscale;
6313 dst.redirectstderr <- src.redirectstderr;
6314 dst.ghyllscroll <- src.ghyllscroll;
6315 dst.columns <- src.columns;
6316 dst.beyecolumns <- src.beyecolumns;
6317 dst.selcmd <- src.selcmd;
6318 dst.updatecurs <- src.updatecurs;
6319 dst.pathlauncher <- src.pathlauncher;
6320 dst.keyhashes <- copykeyhashes src;
6321 dst.hfsize <- src.hfsize;
6322 dst.hscrollstep <- src.hscrollstep;
6323 dst.pgscale <- src.pgscale;
6324 dst.usepbo <- src.usepbo;
6325 dst.wheelbypage <- src.wheelbypage;
6328 let get s =
6329 let h = Hashtbl.create 10 in
6330 let dc = { defconf with angle = defconf.angle } in
6331 let rec toplevel v t spos _ =
6332 match t with
6333 | Vdata | Vcdata | Vend -> v
6334 | Vopen ("llppconfig", _, closed) ->
6335 if closed
6336 then v
6337 else { v with f = llppconfig }
6338 | Vopen _ ->
6339 error "unexpected subelement at top level" s spos
6340 | Vclose _ -> error "unexpected close at top level" s spos
6342 and llppconfig v t spos _ =
6343 match t with
6344 | Vdata | Vcdata -> v
6345 | Vend -> error "unexpected end of input in llppconfig" s spos
6346 | Vopen ("defaults", attrs, closed) ->
6347 let c = config_of dc attrs in
6348 setconf dc c;
6349 if closed
6350 then v
6351 else { v with f = defaults }
6353 | Vopen ("ui-font", attrs, closed) ->
6354 let rec getsize size = function
6355 | [] -> size
6356 | ("size", v) :: rest ->
6357 let size =
6358 fromstring int_of_string spos "size" v fstate.fontsize in
6359 getsize size rest
6360 | l -> getsize size l
6362 fstate.fontsize <- getsize fstate.fontsize attrs;
6363 if closed
6364 then v
6365 else { v with f = uifont (Buffer.create 10) }
6367 | Vopen ("doc", attrs, closed) ->
6368 let pathent, spage, srely, span, svisy = doc_of attrs in
6369 let path = unent pathent
6370 and pageno = fromstring int_of_string spos "page" spage 0
6371 and rely = fromstring float_of_string spos "rely" srely 0.0
6372 and pan = fromstring int_of_string spos "pan" span 0
6373 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6374 let c = config_of dc attrs in
6375 let anchor = (pageno, rely, visy) in
6376 if closed
6377 then (Hashtbl.add h path (c, [], pan, anchor); v)
6378 else { v with f = doc path pan anchor c [] }
6380 | Vopen _ ->
6381 error "unexpected subelement in llppconfig" s spos
6383 | Vclose "llppconfig" -> { v with f = toplevel }
6384 | Vclose _ -> error "unexpected close in llppconfig" s spos
6386 and defaults v t spos _ =
6387 match t with
6388 | Vdata | Vcdata -> v
6389 | Vend -> error "unexpected end of input in defaults" s spos
6390 | Vopen ("keymap", attrs, closed) ->
6391 let modename =
6392 try List.assoc "mode" attrs
6393 with Not_found -> "global" in
6394 if closed
6395 then v
6396 else
6397 let ret keymap =
6398 let h = findkeyhash dc modename in
6399 KeyMap.iter (Hashtbl.replace h) keymap;
6400 defaults
6402 { v with f = pkeymap ret KeyMap.empty }
6404 | Vopen (_, _, _) ->
6405 error "unexpected subelement in defaults" s spos
6407 | Vclose "defaults" ->
6408 { v with f = llppconfig }
6410 | Vclose _ -> error "unexpected close in defaults" s spos
6412 and uifont b v t spos epos =
6413 match t with
6414 | Vdata | Vcdata ->
6415 Buffer.add_substring b s spos (epos - spos);
6417 | Vopen (_, _, _) ->
6418 error "unexpected subelement in ui-font" s spos
6419 | Vclose "ui-font" ->
6420 if String.length !fontpath = 0
6421 then fontpath := Buffer.contents b;
6422 { v with f = llppconfig }
6423 | Vclose _ -> error "unexpected close in ui-font" s spos
6424 | Vend -> error "unexpected end of input in ui-font" s spos
6426 and doc path pan anchor c bookmarks v t spos _ =
6427 match t with
6428 | Vdata | Vcdata -> v
6429 | Vend -> error "unexpected end of input in doc" s spos
6430 | Vopen ("bookmarks", _, closed) ->
6431 if closed
6432 then v
6433 else { v with f = pbookmarks path pan anchor c bookmarks }
6435 | Vopen ("keymap", attrs, closed) ->
6436 let modename =
6437 try List.assoc "mode" attrs
6438 with Not_found -> "global"
6440 if closed
6441 then v
6442 else
6443 let ret keymap =
6444 let h = findkeyhash c modename in
6445 KeyMap.iter (Hashtbl.replace h) keymap;
6446 doc path pan anchor c bookmarks
6448 { v with f = pkeymap ret KeyMap.empty }
6450 | Vopen (_, _, _) ->
6451 error "unexpected subelement in doc" s spos
6453 | Vclose "doc" ->
6454 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6455 { v with f = llppconfig }
6457 | Vclose _ -> error "unexpected close in doc" s spos
6459 and pkeymap ret keymap v t spos _ =
6460 match t with
6461 | Vdata | Vcdata -> v
6462 | Vend -> error "unexpected end of input in keymap" s spos
6463 | Vopen ("map", attrs, closed) ->
6464 let r, l = map_of attrs in
6465 let kss = fromstring keys_of_string spos "in" r [] in
6466 let lss = fromstring keys_of_string spos "out" l [] in
6467 let keymap =
6468 match kss with
6469 | [] -> keymap
6470 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6471 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6473 if closed
6474 then { v with f = pkeymap ret keymap }
6475 else
6476 let f () = v in
6477 { v with f = skip "map" f }
6479 | Vopen _ ->
6480 error "unexpected subelement in keymap" s spos
6482 | Vclose "keymap" ->
6483 { v with f = ret keymap }
6485 | Vclose _ -> error "unexpected close in keymap" s spos
6487 and pbookmarks path pan anchor c bookmarks v t spos _ =
6488 match t with
6489 | Vdata | Vcdata -> v
6490 | Vend -> error "unexpected end of input in bookmarks" s spos
6491 | Vopen ("item", attrs, closed) ->
6492 let titleent, spage, srely, svisy = bookmark_of attrs in
6493 let page = fromstring int_of_string spos "page" spage 0
6494 and rely = fromstring float_of_string spos "rely" srely 0.0
6495 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6496 let bookmarks =
6497 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6499 if closed
6500 then { v with f = pbookmarks path pan anchor c bookmarks }
6501 else
6502 let f () = v in
6503 { v with f = skip "item" f }
6505 | Vopen _ ->
6506 error "unexpected subelement in bookmarks" s spos
6508 | Vclose "bookmarks" ->
6509 { v with f = doc path pan anchor c bookmarks }
6511 | Vclose _ -> error "unexpected close in bookmarks" s spos
6513 and skip tag f v t spos _ =
6514 match t with
6515 | Vdata | Vcdata -> v
6516 | Vend ->
6517 error ("unexpected end of input in skipped " ^ tag) s spos
6518 | Vopen (tag', _, closed) ->
6519 if closed
6520 then v
6521 else
6522 let f' () = { v with f = skip tag f } in
6523 { v with f = skip tag' f' }
6524 | Vclose ctag ->
6525 if tag = ctag
6526 then f ()
6527 else error ("unexpected close in skipped " ^ tag) s spos
6530 parse { f = toplevel; accu = () } s;
6531 h, dc;
6534 let do_load f ic =
6536 let len = in_channel_length ic in
6537 let s = String.create len in
6538 really_input ic s 0 len;
6539 f s;
6540 with
6541 | Parse_error (msg, s, pos) ->
6542 let subs = subs s pos in
6543 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6544 failwith ("parse error: " ^ s)
6546 | exn ->
6547 failwith ("config load error: " ^ Printexc.to_string exn)
6550 let defconfpath =
6551 let dir =
6553 let dir = Filename.concat home ".config" in
6554 if Sys.is_directory dir then dir else home
6555 with _ -> home
6557 Filename.concat dir "llpp.conf"
6560 let confpath = ref defconfpath;;
6562 let load1 f =
6563 if Sys.file_exists !confpath
6564 then
6565 match
6566 (try Some (open_in_bin !confpath)
6567 with exn ->
6568 prerr_endline
6569 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6570 Printexc.to_string exn);
6571 None
6573 with
6574 | Some ic ->
6575 let success =
6577 f (do_load get ic)
6578 with exn ->
6579 prerr_endline
6580 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6581 Printexc.to_string exn);
6582 false
6584 close_in ic;
6585 success
6587 | None -> false
6588 else
6589 f (Hashtbl.create 0, defconf)
6592 let load () =
6593 let f (h, dc) =
6594 let pc, pb, px, pa =
6596 Hashtbl.find h (Filename.basename state.path)
6597 with Not_found -> dc, [], 0, emptyanchor
6599 setconf defconf dc;
6600 setconf conf pc;
6601 state.bookmarks <- pb;
6602 state.x <- px;
6603 state.scrollw <- conf.scrollbw;
6604 if conf.jumpback
6605 then state.anchor <- pa;
6606 cbput state.hists.nav pa;
6607 true
6609 load1 f
6612 let add_attrs bb always dc c =
6613 let ob s a b =
6614 if always || a != b
6615 then Printf.bprintf bb "\n %s='%b'" s a
6616 and oi s a b =
6617 if always || a != b
6618 then Printf.bprintf bb "\n %s='%d'" s a
6619 and oI s a b =
6620 if always || a != b
6621 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6622 and oz s a b =
6623 if always || a <> b
6624 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6625 and oF s a b =
6626 if always || a <> b
6627 then Printf.bprintf bb "\n %s='%f'" s a
6628 and oc s a b =
6629 if always || a <> b
6630 then
6631 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6632 and oC s a b =
6633 if always || a <> b
6634 then
6635 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6636 and oR s a b =
6637 if always || a <> b
6638 then
6639 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6640 and os s a b =
6641 if always || a <> b
6642 then
6643 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6644 and og s a b =
6645 if always || a <> b
6646 then
6647 match a with
6648 | None -> ()
6649 | Some (_N, _A, _B) ->
6650 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6651 and oW s a b =
6652 if always || a <> b
6653 then
6654 let v =
6655 match a with
6656 | None -> "false"
6657 | Some f ->
6658 if f = infinity
6659 then "true"
6660 else string_of_float f
6662 Printf.bprintf bb "\n %s='%s'" s v
6663 and oco s a b =
6664 if always || a <> b
6665 then
6666 match a with
6667 | Cmulti ((n, a, b), _) when n > 1 ->
6668 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6669 | Csplit (n, _) when n > 1 ->
6670 Printf.bprintf bb "\n %s='%d'" s ~-n
6671 | _ -> ()
6672 and obeco s a b =
6673 if always || a <> b
6674 then
6675 match a with
6676 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6677 | _ -> ()
6679 let w, h =
6680 if always
6681 then dc.winw, dc.winh
6682 else
6683 match state.fullscreen with
6684 | Some wh -> wh
6685 | None -> c.winw, c.winh
6687 oi "width" w dc.winw;
6688 oi "height" h dc.winh;
6689 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6690 oi "scroll-handle-height" c.scrollh dc.scrollh;
6691 ob "case-insensitive-search" c.icase dc.icase;
6692 ob "preload" c.preload dc.preload;
6693 oi "page-bias" c.pagebias dc.pagebias;
6694 oi "scroll-step" c.scrollstep dc.scrollstep;
6695 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6696 ob "max-height-fit" c.maxhfit dc.maxhfit;
6697 ob "crop-hack" c.crophack dc.crophack;
6698 oW "throttle" c.maxwait dc.maxwait;
6699 ob "highlight-links" c.hlinks dc.hlinks;
6700 ob "under-cursor-info" c.underinfo dc.underinfo;
6701 oi "vertical-margin" c.interpagespace dc.interpagespace;
6702 oz "zoom" c.zoom dc.zoom;
6703 ob "presentation" c.presentation dc.presentation;
6704 oi "rotation-angle" c.angle dc.angle;
6705 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6706 ob "proportional-display" c.proportional dc.proportional;
6707 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6708 oi "tex-count" c.texcount dc.texcount;
6709 oi "slice-height" c.sliceheight dc.sliceheight;
6710 oi "thumbnail-width" c.thumbw dc.thumbw;
6711 ob "persistent-location" c.jumpback dc.jumpback;
6712 oc "background-color" c.bgcolor dc.bgcolor;
6713 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6714 oi "tile-width" c.tilew dc.tilew;
6715 oi "tile-height" c.tileh dc.tileh;
6716 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6717 ob "checkers" c.checkers dc.checkers;
6718 oi "aalevel" c.aalevel dc.aalevel;
6719 ob "trim-margins" c.trimmargins dc.trimmargins;
6720 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6721 os "uri-launcher" c.urilauncher dc.urilauncher;
6722 os "path-launcher" c.pathlauncher dc.pathlauncher;
6723 oC "color-space" c.colorspace dc.colorspace;
6724 ob "invert-colors" c.invert dc.invert;
6725 oF "brightness" c.colorscale dc.colorscale;
6726 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6727 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6728 oco "columns" c.columns dc.columns;
6729 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6730 os "selection-command" c.selcmd dc.selcmd;
6731 ob "update-cursor" c.updatecurs dc.updatecurs;
6732 oi "hint-font-size" c.hfsize dc.hfsize;
6733 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6734 oF "page-scroll-scale" c.pgscale dc.pgscale;
6735 ob "use-pbo" c.usepbo dc.usepbo;
6736 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6739 let keymapsbuf always dc c =
6740 let bb = Buffer.create 16 in
6741 let rec loop = function
6742 | [] -> ()
6743 | (modename, h) :: rest ->
6744 let dh = findkeyhash dc modename in
6745 if always || h <> dh
6746 then (
6747 if Hashtbl.length h > 0
6748 then (
6749 if Buffer.length bb > 0
6750 then Buffer.add_char bb '\n';
6751 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6752 Hashtbl.iter (fun i o ->
6753 let isdifferent = always ||
6755 let dO = Hashtbl.find dh i in
6756 dO <> o
6757 with Not_found -> true
6759 if isdifferent
6760 then
6761 let addkm (k, m) =
6762 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6763 if Wsi.withalt m then Buffer.add_string bb "alt-";
6764 if Wsi.withshift m then Buffer.add_string bb "shift-";
6765 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6766 Buffer.add_string bb (Wsi.keyname k);
6768 let addkms l =
6769 let rec loop = function
6770 | [] -> ()
6771 | km :: [] -> addkm km
6772 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6774 loop l
6776 Buffer.add_string bb "<map in='";
6777 addkm i;
6778 match o with
6779 | KMinsrt km ->
6780 Buffer.add_string bb "' out='";
6781 addkm km;
6782 Buffer.add_string bb "'/>\n"
6784 | KMinsrl kms ->
6785 Buffer.add_string bb "' out='";
6786 addkms kms;
6787 Buffer.add_string bb "'/>\n"
6789 | KMmulti (ins, kms) ->
6790 Buffer.add_char bb ' ';
6791 addkms ins;
6792 Buffer.add_string bb "' out='";
6793 addkms kms;
6794 Buffer.add_string bb "'/>\n"
6795 ) h;
6796 Buffer.add_string bb "</keymap>";
6799 loop rest
6801 loop c.keyhashes;
6805 let save () =
6806 let uifontsize = fstate.fontsize in
6807 let bb = Buffer.create 32768 in
6808 let f (h, dc) =
6809 let dc = if conf.bedefault then conf else dc in
6810 Buffer.add_string bb "<llppconfig>\n";
6812 if String.length !fontpath > 0
6813 then
6814 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6815 uifontsize
6816 !fontpath
6817 else (
6818 if uifontsize <> 14
6819 then
6820 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6823 Buffer.add_string bb "<defaults ";
6824 add_attrs bb true dc dc;
6825 let kb = keymapsbuf true dc dc in
6826 if Buffer.length kb > 0
6827 then (
6828 Buffer.add_string bb ">\n";
6829 Buffer.add_buffer bb kb;
6830 Buffer.add_string bb "\n</defaults>\n";
6832 else Buffer.add_string bb "/>\n";
6834 let adddoc path pan anchor c bookmarks =
6835 if bookmarks == [] && c = dc && anchor = emptyanchor
6836 then ()
6837 else (
6838 Printf.bprintf bb "<doc path='%s'"
6839 (enent path 0 (String.length path));
6841 if anchor <> emptyanchor
6842 then (
6843 let n, rely, visy = anchor in
6844 Printf.bprintf bb " page='%d'" n;
6845 if rely > 1e-6
6846 then
6847 Printf.bprintf bb " rely='%f'" rely
6849 if abs_float visy > 1e-6
6850 then
6851 Printf.bprintf bb " visy='%f'" visy
6855 if pan != 0
6856 then Printf.bprintf bb " pan='%d'" pan;
6858 add_attrs bb false dc c;
6859 let kb = keymapsbuf false dc c in
6861 begin match bookmarks with
6862 | [] ->
6863 if Buffer.length kb > 0
6864 then (
6865 Buffer.add_string bb ">\n";
6866 Buffer.add_buffer bb kb;
6867 Buffer.add_string bb "\n</doc>\n";
6869 else Buffer.add_string bb "/>\n"
6870 | _ ->
6871 Buffer.add_string bb ">\n<bookmarks>\n";
6872 List.iter (fun (title, _level, (page, rely, visy)) ->
6873 Printf.bprintf bb
6874 "<item title='%s' page='%d'"
6875 (enent title 0 (String.length title))
6876 page
6878 if rely > 1e-6
6879 then
6880 Printf.bprintf bb " rely='%f'" rely
6882 if abs_float visy > 1e-6
6883 then
6884 Printf.bprintf bb " visy='%f'" visy
6886 Buffer.add_string bb "/>\n";
6887 ) bookmarks;
6888 Buffer.add_string bb "</bookmarks>";
6889 if Buffer.length kb > 0
6890 then (
6891 Buffer.add_string bb "\n";
6892 Buffer.add_buffer bb kb;
6894 Buffer.add_string bb "\n</doc>\n";
6895 end;
6899 let pan, conf =
6900 match state.mode with
6901 | Birdseye (c, pan, _, _, _) ->
6902 let beyecolumns =
6903 match conf.columns with
6904 | Cmulti ((c, _, _), _) -> Some c
6905 | Csingle _ -> None
6906 | Csplit _ -> None
6907 and columns =
6908 match c.columns with
6909 | Cmulti (c, _) -> Cmulti (c, [||])
6910 | Csingle _ -> Csingle [||]
6911 | Csplit _ -> failwith "quit from bird's eye while split"
6913 pan, { c with beyecolumns = beyecolumns; columns = columns }
6914 | _ -> state.x, conf
6916 let basename = Filename.basename state.path in
6917 adddoc basename pan (getanchor ())
6918 (let conf =
6919 let autoscrollstep =
6920 match state.autoscroll with
6921 | Some step -> step
6922 | None -> conf.autoscrollstep
6924 match state.mode with
6925 | Birdseye (bc, _, _, _, _) ->
6926 { conf with
6927 zoom = bc.zoom;
6928 presentation = bc.presentation;
6929 interpagespace = bc.interpagespace;
6930 maxwait = bc.maxwait;
6931 autoscrollstep = autoscrollstep }
6932 | _ -> { conf with autoscrollstep = autoscrollstep }
6933 in conf)
6934 (if conf.savebmarks then state.bookmarks else []);
6936 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6937 if basename <> path
6938 then adddoc path x anchor c bookmarks
6939 ) h;
6940 Buffer.add_string bb "</llppconfig>\n";
6941 true;
6943 if load1 f && Buffer.length bb > 0
6944 then
6946 let tmp = !confpath ^ ".tmp" in
6947 let oc = open_out_bin tmp in
6948 Buffer.output_buffer oc bb;
6949 close_out oc;
6950 Unix.rename tmp !confpath;
6951 with exn ->
6952 prerr_endline
6953 ("error while saving configuration: " ^ Printexc.to_string exn)
6955 end;;
6957 let () =
6958 let trimcachepath = ref "" in
6959 Arg.parse
6960 (Arg.align
6961 [("-p", Arg.String (fun s -> state.password <- s) ,
6962 "<password> Set password");
6964 ("-f", Arg.String (fun s -> Config.fontpath := s),
6965 "<path> Set path to the user interface font");
6967 ("-c", Arg.String (fun s -> Config.confpath := s),
6968 "<path> Set path to the configuration file");
6970 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6971 "<path> Set path to the trim cache file");
6973 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6974 "<named destination> Set named destination");
6976 ("-v", Arg.Unit (fun () ->
6977 Printf.printf
6978 "%s\nconfiguration path: %s\n"
6979 (version ())
6980 Config.defconfpath
6982 exit 0), " Print version and exit");
6985 (fun s -> state.path <- s)
6986 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6988 if String.length state.path = 0
6989 then (prerr_endline "file name missing"; exit 1);
6991 if not (Config.load ())
6992 then prerr_endline "failed to load configuration";
6994 let globalkeyhash = findkeyhash conf "global" in
6995 let wsfd, winw, winh = Wsi.init (object
6996 method expose =
6997 state.wthack <- false;
6998 if nogeomcmds state.geomcmds || platform == Posx
6999 then display ()
7000 else (
7001 GlClear.color (scalecolor2 conf.bgcolor);
7002 GlClear.clear [`color];
7004 method display = display ()
7005 method reshape w h = reshape w h
7006 method mouse b d x y m = mouse b d x y m
7007 method motion x y = state.mpos <- (x, y); motion x y
7008 method pmotion x y = state.mpos <- (x, y); pmotion x y
7009 method key k m =
7010 let mascm = m land (
7011 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7012 ) in
7013 match state.keystate with
7014 | KSnone ->
7015 let km = k, mascm in
7016 begin
7017 match
7018 let modehash = state.uioh#modehash in
7019 try Hashtbl.find modehash km
7020 with Not_found ->
7021 try Hashtbl.find globalkeyhash km
7022 with Not_found -> KMinsrt (k, m)
7023 with
7024 | KMinsrt (k, m) -> keyboard k m
7025 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7026 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7028 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7029 List.iter (fun (k, m) -> keyboard k m) insrt;
7030 state.keystate <- KSnone
7031 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7032 state.keystate <- KSinto (keys, insrt)
7033 | _ ->
7034 state.keystate <- KSnone
7036 method enter x y = state.mpos <- (x, y); pmotion x y
7037 method leave = state.mpos <- (-1, -1)
7038 method quit = raise Quit
7039 end) conf.winw conf.winh (platform = Posx) in
7041 state.wsfd <- wsfd;
7043 if not (
7044 List.exists GlMisc.check_extension
7045 [ "GL_ARB_texture_rectangle"
7046 ; "GL_EXT_texture_recangle"
7047 ; "GL_NV_texture_rectangle" ]
7049 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7051 let cr, sw =
7052 match Ne.pipe () with
7053 | Ne.Exn exn ->
7054 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
7055 exit 1
7056 | Ne.Res rw -> rw
7057 and sr, cw =
7058 match Ne.pipe () with
7059 | Ne.Exn exn ->
7060 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7061 exit 1
7062 | Ne.Res rw -> rw
7065 cloexec cr;
7066 cloexec sw;
7067 cloexec sr;
7068 cloexec cw;
7070 setcheckers conf.checkers;
7071 redirectstderr ();
7073 init (cr, cw) (
7074 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7075 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7076 !Config.fontpath, !trimcachepath,
7077 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7079 state.sr <- sr;
7080 state.sw <- sw;
7081 state.text <- "Opening " ^ (mbtoutf8 state.path);
7082 reshape winw winh;
7083 opendoc state.path state.password;
7084 state.uioh <- uioh;
7086 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7088 let rec loop deadline =
7089 let r =
7090 match state.errfd with
7091 | None -> [state.sr; state.wsfd]
7092 | Some fd -> [state.sr; state.wsfd; fd]
7094 if state.redisplay
7095 then (
7096 state.redisplay <- false;
7097 display ();
7099 let timeout =
7100 let now = now () in
7101 if deadline > now
7102 then (
7103 if deadline = infinity
7104 then ~-.1.0
7105 else max 0.0 (deadline -. now)
7107 else 0.0
7109 let r, _, _ =
7110 try tempfailureretry (Unix.select r [] []) timeout
7111 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7113 begin match r with
7114 | [] ->
7115 state.ghyll None;
7116 let newdeadline =
7117 if state.ghyll == noghyll
7118 then
7119 match state.autoscroll with
7120 | Some step when step != 0 ->
7121 let y = state.y + step in
7122 let y =
7123 if y < 0
7124 then state.maxy
7125 else if y >= state.maxy then 0 else y
7127 gotoy y;
7128 if state.mode = View
7129 then state.text <- "";
7130 deadline +. 0.01
7131 | _ -> infinity
7132 else deadline +. 0.01
7134 loop newdeadline
7136 | l ->
7137 let rec checkfds = function
7138 | [] -> ()
7139 | fd :: rest when fd = state.sr ->
7140 let cmd = readcmd state.sr in
7141 act cmd;
7142 checkfds rest
7144 | fd :: rest when fd = state.wsfd ->
7145 Wsi.readresp fd;
7146 checkfds rest
7148 | fd :: rest ->
7149 let s = String.create 80 in
7150 let n = tempfailureretry (Unix.read fd s 0) 80 in
7151 if conf.redirectstderr
7152 then (
7153 Buffer.add_substring state.errmsgs s 0 n;
7154 state.newerrmsgs <- true;
7155 state.redisplay <- true;
7157 else (
7158 prerr_string (String.sub s 0 n);
7159 flush stderr;
7161 checkfds rest
7163 checkfds l;
7164 let newdeadline =
7165 let deadline1 =
7166 if deadline = infinity
7167 then now () +. 0.01
7168 else deadline
7170 match state.autoscroll with
7171 | Some step when step != 0 -> deadline1
7172 | _ -> if state.ghyll == noghyll then infinity else deadline1
7174 loop newdeadline
7175 end;
7178 loop infinity;
7179 with Quit ->
7180 Config.save ();