1a71a65785d16fcf19837552300185f8f809bae9
[llpp.git] / main.ml
blob1a71a65785d16fcf19837552300185f8f809bae9
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 let y = bound y 0 state.maxy in
1601 let y, layout, proceed =
1602 match conf.maxwait with
1603 | Some time when state.ghyll == noghyll ->
1604 begin match state.throttle with
1605 | None ->
1606 let layout = layout y conf.winh in
1607 let ready = layoutready layout in
1608 if not ready
1609 then (
1610 load layout;
1611 state.throttle <- Some (layout, y, now ());
1613 else G.postRedisplay "gotoy showall (None)";
1614 y, layout, ready
1615 | Some (_, _, started) ->
1616 let dt = now () -. started in
1617 if dt > time
1618 then (
1619 state.throttle <- None;
1620 let layout = layout y conf.winh in
1621 load layout;
1622 G.postRedisplay "maxwait";
1623 y, layout, true
1625 else -1, [], false
1628 | _ ->
1629 let layout = layout y conf.winh in
1630 if true || layoutready layout
1631 then G.postRedisplay "gotoy ready";
1632 y, layout, true
1634 if proceed
1635 then (
1636 state.y <- y;
1637 state.layout <- layout;
1638 begin match state.mode with
1639 | LinkNav (Ltexact (pageno, linkno)) ->
1640 let rec loop = function
1641 | [] ->
1642 state.mode <- LinkNav (Ltgendir 0)
1643 | l :: _ when l.pageno = pageno ->
1644 begin match getopaque pageno with
1645 | None ->
1646 state.mode <- LinkNav (Ltgendir 0)
1647 | Some opaque ->
1648 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1649 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1650 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1651 then state.mode <- LinkNav (Ltgendir 0)
1653 | _ :: rest -> loop rest
1655 loop layout
1656 | _ -> ()
1657 end;
1658 begin match state.mode with
1659 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1660 if not (pagevisible layout pageno)
1661 then (
1662 match state.layout with
1663 | [] -> ()
1664 | l :: _ ->
1665 state.mode <- Birdseye (
1666 conf, leftx, l.pageno, hooverpageno, anchor
1669 | LinkNav (Ltgendir dir as lt) ->
1670 let linknav =
1671 let rec loop = function
1672 | [] -> lt
1673 | l :: rest ->
1674 match getopaque l.pageno with
1675 | None -> loop rest
1676 | Some opaque ->
1677 let link =
1678 let ld =
1679 if dir = 0
1680 then LDfirstvisible (l.pagex, l.pagey, dir)
1681 else (
1682 if dir > 0 then LDfirst else LDlast
1685 findlink opaque ld
1687 match link with
1688 | Lnotfound -> loop rest
1689 | Lfound n ->
1690 showlinktype (getlink opaque n);
1691 Ltexact (l.pageno, n)
1693 loop state.layout
1695 state.mode <- LinkNav linknav
1696 | _ -> ()
1697 end;
1698 preload layout;
1700 state.ghyll <- noghyll;
1701 if conf.updatecurs
1702 then (
1703 let mx, my = state.mpos in
1704 updateunder mx my;
1708 let conttiling pageno opaque =
1709 tilepage pageno opaque
1710 (if conf.preload then preloadlayout state.y else state.layout)
1713 let gotoy_and_clear_text y =
1714 if not conf.verbose then state.text <- "";
1715 gotoy y;
1718 let getanchor1 l =
1719 let top =
1720 let coloff = l.pagecol * l.pageh in
1721 float (l.pagey + coloff) /. float l.pageh
1723 let dtop =
1724 if l.pagedispy = 0
1725 then
1727 else
1728 if conf.presentation
1729 then float l.pagedispy /. float (calcips l.pageh)
1730 else float l.pagedispy /. float conf.interpagespace
1732 (l.pageno, top, dtop)
1735 let getanchor () =
1736 match state.layout with
1737 | l :: _ -> getanchor1 l
1738 | [] ->
1739 let n = page_of_y state.y in
1740 let y, h = getpageyh n in
1741 let dy = y - state.y in
1742 let dtop =
1743 if conf.presentation
1744 then
1745 let ips = calcips h in
1746 float (dy + ips) /. float ips
1747 else
1748 float dy /. float conf.interpagespace
1750 (n, 0.0, dtop)
1753 let getanchory (n, top, dtop) =
1754 let y, h = getpageyh n in
1755 if conf.presentation
1756 then
1757 let ips = calcips h in
1758 y + truncate (top*.float h -. dtop*.float ips) + ips;
1759 else
1760 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1763 let gotoanchor anchor =
1764 gotoy (getanchory anchor);
1767 let addnav () =
1768 cbput state.hists.nav (getanchor ());
1771 let getnav dir =
1772 let anchor = cbgetc state.hists.nav dir in
1773 getanchory anchor;
1776 let gotoghyll y =
1777 let scroll f n a b =
1778 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1779 let snake f a b =
1780 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1781 if f < a
1782 then s (float f /. float a)
1783 else (
1784 if f > b
1785 then 1.0 -. s ((float (f-b) /. float (n-b)))
1786 else 1.0
1789 snake f a b
1790 and summa f n a b =
1791 (* courtesy:
1792 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1793 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1794 let iv1 = iv f in
1795 let ins = float a *. iv1
1796 and outs = float (n-b) *. iv1 in
1797 let ones = b - a in
1798 ins +. outs +. float ones
1800 let rec set (_N, _A, _B) y sy =
1801 let sum = summa 1.0 _N _A _B in
1802 let dy = float (y - sy) in
1803 state.ghyll <- (
1804 let rec gf n y1 o =
1805 if n >= _N
1806 then state.ghyll <- noghyll
1807 else
1808 let go n =
1809 let s = scroll n _N _A _B in
1810 let y1 = y1 +. ((s *. dy) /. sum) in
1811 gotoy_and_clear_text (truncate y1);
1812 state.ghyll <- gf (n+1) y1;
1814 match o with
1815 | None -> go n
1816 | Some y' -> set (_N/2, 1, 1) y' state.y
1818 gf 0 (float state.y)
1821 match conf.ghyllscroll with
1822 | None ->
1823 gotoy_and_clear_text y
1824 | Some nab ->
1825 if state.ghyll == noghyll
1826 then set nab y state.y
1827 else state.ghyll (Some y)
1830 let gotopage n top =
1831 let y, h = getpageyh n in
1832 let y = y + (truncate (top *. float h)) in
1833 gotoghyll y
1836 let gotopage1 n top =
1837 let y = getpagey n in
1838 let y = y + top in
1839 gotoghyll y
1842 let invalidate s f =
1843 state.layout <- [];
1844 state.pdims <- [];
1845 state.rects <- [];
1846 state.rects1 <- [];
1847 match state.geomcmds with
1848 | ps, [] when String.length ps = 0 ->
1849 f ();
1850 state.geomcmds <- s, [];
1852 | ps, [] ->
1853 state.geomcmds <- ps, [s, f];
1855 | ps, (s', _) :: rest when s' = s ->
1856 state.geomcmds <- ps, ((s, f) :: rest);
1858 | ps, cmds ->
1859 state.geomcmds <- ps, ((s, f) :: cmds);
1862 let flushpages () =
1863 Hashtbl.iter (fun _ opaque ->
1864 wcmd "freepage %s" opaque;
1865 ) state.pagemap;
1866 Hashtbl.clear state.pagemap;
1869 let opendoc path password =
1870 state.path <- path;
1871 state.password <- password;
1872 state.gen <- state.gen + 1;
1873 state.docinfo <- [];
1875 flushpages ();
1876 setaalevel conf.aalevel;
1877 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1878 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1879 invalidate "reqlayout"
1880 (fun () ->
1881 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional);
1882 if String.length state.nameddest > 0
1883 then wcmd "anchor %s\000" state.nameddest);
1886 let reload () =
1887 state.anchor <- getanchor ();
1888 state.wthack <- true;
1889 opendoc state.path state.password;
1892 let scalecolor c =
1893 let c = c *. conf.colorscale in
1894 (c, c, c);
1897 let scalecolor2 (r, g, b) =
1898 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1901 let docolumns = function
1902 | Csingle _ ->
1903 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1904 let rec loop pageno pdimno pdim y ph pdims =
1905 if pageno = state.pagecount
1906 then ()
1907 else
1908 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1909 match pdims with
1910 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1911 pdimno+1, pdim, rest
1912 | _ ->
1913 pdimno, pdim, pdims
1915 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1916 let y = y +
1917 (if conf.presentation
1918 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1919 else (if pageno = 0 then 0 else conf.interpagespace)
1922 a.(pageno) <- (pdimno, x, y, pdim);
1923 loop (pageno+1) pdimno pdim (y + h) h pdims
1925 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1926 conf.columns <- Csingle a;
1928 | Cmulti ((columns, coverA, coverB), _) ->
1929 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1930 let rec loop pageno pdimno pdim x y rowh pdims =
1931 let rec fixrow m = if m = pageno then () else
1932 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1933 if h < rowh
1934 then (
1935 let y = y + (rowh - h) / 2 in
1936 a.(m) <- (pdimno, x, y, pdim);
1938 fixrow (m+1)
1940 if pageno = state.pagecount
1941 then fixrow (((pageno - 1) / columns) * columns)
1942 else
1943 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1944 match pdims with
1945 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1946 pdimno+1, pdim, rest
1947 | _ ->
1948 pdimno, pdim, pdims
1950 let x, y, rowh' =
1951 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1952 then (
1953 let x = (conf.winw - state.scrollw - w) / 2 in
1954 let ips =
1955 if conf.presentation then calcips h else conf.interpagespace in
1956 x, y + ips + rowh, h
1958 else (
1959 if (pageno - coverA) mod columns = 0
1960 then (
1961 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1962 let y =
1963 if conf.presentation
1964 then
1965 let ips = calcips h in
1966 y + (if pageno = 0 then 0 else calcips rowh + ips)
1967 else
1968 y + (if pageno = 0 then 0 else conf.interpagespace)
1970 x, y + rowh, h
1972 else x, y, max rowh h
1975 let y =
1976 if pageno > 1 && (pageno - coverA) mod columns = 0
1977 then (
1978 let y =
1979 if pageno = columns && conf.presentation
1980 then (
1981 let ips = calcips rowh in
1982 for i = 0 to pred columns
1984 let (pdimno, x, y, pdim) = a.(i) in
1985 a.(i) <- (pdimno, x, y+ips, pdim)
1986 done;
1987 y+ips;
1989 else y
1991 fixrow (pageno - columns);
1994 else y
1996 a.(pageno) <- (pdimno, x, y, pdim);
1997 let x = x + w + xoff*2 + conf.interpagespace in
1998 loop (pageno+1) pdimno pdim x y rowh' pdims
2000 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2001 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2003 | Csplit (c, _) ->
2004 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2005 let rec loop pageno pdimno pdim y pdims =
2006 if pageno = state.pagecount
2007 then ()
2008 else
2009 let pdimno, ((_, w, h, _) as pdim), pdims =
2010 match pdims with
2011 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2012 pdimno+1, pdim, rest
2013 | _ ->
2014 pdimno, pdim, pdims
2016 let cw = w / c in
2017 let rec loop1 n x y =
2018 if n = c then y else (
2019 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2020 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2023 let y = loop1 0 0 y in
2024 loop (pageno+1) pdimno pdim y pdims
2026 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2027 conf.columns <- Csplit (c, a);
2030 let represent () =
2031 docolumns conf.columns;
2032 state.maxy <- calcheight ();
2033 state.hscrollh <-
2034 if state.w <= conf.winw - state.scrollw
2035 then 0
2036 else state.scrollw
2038 match state.mode with
2039 | Birdseye (_, _, pageno, _, _) ->
2040 let y, h = getpageyh pageno in
2041 let top = (conf.winh - h) / 2 in
2042 gotoy (max 0 (y - top))
2043 | _ -> gotoanchor state.anchor
2046 let reshape w h =
2047 state.wthack <- false;
2048 GlDraw.viewport 0 0 w h;
2049 let firsttime = state.geomcmds == firstgeomcmds in
2050 if not firsttime && nogeomcmds state.geomcmds
2051 then state.anchor <- getanchor ();
2053 conf.winw <- w;
2054 let w = truncate (float w *. conf.zoom) - state.scrollw in
2055 let w = max w 2 in
2056 conf.winh <- h;
2057 setfontsize fstate.fontsize;
2058 GlMat.mode `modelview;
2059 GlMat.load_identity ();
2061 GlMat.mode `projection;
2062 GlMat.load_identity ();
2063 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2064 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2065 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2067 let relx =
2068 if conf.zoom <= 1.0
2069 then 0.0
2070 else float state.x /. float state.w
2072 invalidate "geometry"
2073 (fun () ->
2074 state.w <- w;
2075 if not firsttime
2076 then state.x <- truncate (relx *. float w);
2077 let w =
2078 match conf.columns with
2079 | Csingle _ -> w
2080 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2081 | Csplit (c, _) -> w * c
2083 wcmd "geometry %d %d" w h);
2086 let enttext () =
2087 let len = String.length state.text in
2088 let drawstring s =
2089 let hscrollh =
2090 match state.mode with
2091 | Textentry _
2092 | View ->
2093 let h, _, _ = state.uioh#scrollpw in
2095 | _ -> 0
2097 let rect x w =
2098 GlDraw.rect
2099 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2100 (x+.w, float (conf.winh - hscrollh))
2103 let w = float (conf.winw - state.scrollw - 1) in
2104 if state.progress >= 0.0 && state.progress < 1.0
2105 then (
2106 GlDraw.color (0.3, 0.3, 0.3);
2107 let w1 = w *. state.progress in
2108 rect 0.0 w1;
2109 GlDraw.color (0.0, 0.0, 0.0);
2110 rect w1 (w-.w1)
2112 else (
2113 GlDraw.color (0.0, 0.0, 0.0);
2114 rect 0.0 w;
2117 GlDraw.color (1.0, 1.0, 1.0);
2118 drawstring fstate.fontsize
2119 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2121 let s =
2122 match state.mode with
2123 | Textentry ((prefix, text, _, _, _, _), _) ->
2124 let s =
2125 if len > 0
2126 then
2127 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2128 else
2129 Printf.sprintf "%s%s_" prefix text
2133 | _ -> state.text
2135 let s =
2136 if state.newerrmsgs
2137 then (
2138 if not (istextentry state.mode)
2139 then
2140 let s1 = "(press 'e' to review error messasges)" in
2141 if String.length s > 0 then s ^ " " ^ s1 else s1
2142 else s
2144 else s
2146 if String.length s > 0
2147 then drawstring s
2150 let gctiles () =
2151 let len = Queue.length state.tilelru in
2152 let layout = lazy (
2153 match state.throttle with
2154 | None ->
2155 if conf.preload
2156 then preloadlayout state.y
2157 else state.layout
2158 | Some (layout, _, _) ->
2159 layout
2160 ) in
2161 let rec loop qpos =
2162 if state.memused <= conf.memlimit
2163 then ()
2164 else (
2165 if qpos < len
2166 then
2167 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2168 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2169 let (_, pw, ph, _) = getpagedim n in
2171 gen = state.gen
2172 && colorspace = conf.colorspace
2173 && angle = conf.angle
2174 && pagew = pw
2175 && pageh = ph
2176 && (
2177 let x = col*conf.tilew
2178 and y = row*conf.tileh in
2179 tilevisible (Lazy.force_val layout) n x y
2181 then Queue.push lruitem state.tilelru
2182 else (
2183 freepbo p;
2184 wcmd "freetile %s" p;
2185 state.memused <- state.memused - s;
2186 state.uioh#infochanged Memused;
2187 Hashtbl.remove state.tilemap k;
2189 loop (qpos+1)
2192 loop 0
2195 let flushtiles () =
2196 Queue.iter (fun (k, p, s) ->
2197 wcmd "freetile %s" p;
2198 state.memused <- state.memused - s;
2199 state.uioh#infochanged Memused;
2200 Hashtbl.remove state.tilemap k;
2201 ) state.tilelru;
2202 Queue.clear state.tilelru;
2203 load state.layout;
2206 let logcurrently = function
2207 | Idle -> dolog "Idle"
2208 | Loading (l, gen) ->
2209 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2210 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2211 dolog
2212 "Tiling %d[%d,%d] page=%s cs=%s angle"
2213 l.pageno col row pageopaque
2214 (colorspace_to_string colorspace)
2216 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2217 angle gen conf.angle state.gen
2218 tilew tileh
2219 conf.tilew conf.tileh
2221 | Outlining _ ->
2222 dolog "outlining"
2225 let act cmds =
2226 (* dolog "%S" cmds; *)
2227 let op, args =
2228 let spacepos =
2229 try String.index cmds ' '
2230 with Not_found -> -1
2232 if spacepos = -1
2233 then cmds, ""
2234 else
2235 let l = String.length cmds in
2236 let op = String.sub cmds 0 spacepos in
2237 op, begin
2238 if l - spacepos < 2 then ""
2239 else String.sub cmds (spacepos+1) (l-spacepos-1)
2242 match op with
2243 | "clear" ->
2244 state.uioh#infochanged Pdim;
2245 state.pdims <- [];
2247 | "clearrects" ->
2248 state.rects <- state.rects1;
2249 G.postRedisplay "clearrects";
2251 | "continue" ->
2252 let n =
2253 try Scanf.sscanf args "%u" (fun n -> n)
2254 with exn ->
2255 dolog "error processing 'continue' %S: %s"
2256 cmds (Printexc.to_string exn);
2257 exit 1;
2259 state.pagecount <- n;
2260 begin match state.currently with
2261 | Outlining l ->
2262 state.currently <- Idle;
2263 state.outlines <- Array.of_list (List.rev l)
2264 | _ -> ()
2265 end;
2267 let cur, cmds = state.geomcmds in
2268 if String.length cur = 0
2269 then failwith "umpossible";
2271 begin match List.rev cmds with
2272 | [] ->
2273 state.geomcmds <- "", [];
2274 represent ();
2275 | (s, f) :: rest ->
2276 f ();
2277 state.geomcmds <- s, List.rev rest;
2278 end;
2279 if conf.maxwait = None
2280 then G.postRedisplay "continue";
2282 | "title" ->
2283 Wsi.settitle args
2285 | "msg" ->
2286 showtext ' ' args
2288 | "vmsg" ->
2289 if conf.verbose
2290 then showtext ' ' args
2292 | "emsg" ->
2293 Buffer.add_string state.errmsgs args;
2294 state.newerrmsgs <- true;
2295 G.postRedisplay "error message"
2297 | "progress" ->
2298 let progress, text =
2300 Scanf.sscanf args "%f %n"
2301 (fun f pos ->
2302 f, String.sub args pos (String.length args - pos))
2303 with exn ->
2304 dolog "error processing 'progress' %S: %s"
2305 cmds (Printexc.to_string exn);
2306 exit 1;
2308 state.text <- text;
2309 state.progress <- progress;
2310 G.postRedisplay "progress"
2312 | "firstmatch" ->
2313 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2315 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2316 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2317 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2318 with exn ->
2319 dolog "error processing 'firstmatch' %S: %s"
2320 cmds (Printexc.to_string exn);
2321 exit 1;
2323 let y = (getpagey pageno) + truncate y0 in
2324 addnav ();
2325 gotoy y;
2326 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2328 | "match" ->
2329 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2331 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2332 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2333 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2334 with exn ->
2335 dolog "error processing 'match' %S: %s"
2336 cmds (Printexc.to_string exn);
2337 exit 1;
2339 state.rects1 <-
2340 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2342 | "page" ->
2343 let pageopaque, t =
2345 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2346 with exn ->
2347 dolog "error processing 'page' %S: %s"
2348 cmds (Printexc.to_string exn);
2349 exit 1;
2351 begin match state.currently with
2352 | Loading (l, gen) ->
2353 vlog "page %d took %f sec" l.pageno t;
2354 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2355 begin match state.throttle with
2356 | None ->
2357 let preloadedpages =
2358 if conf.preload
2359 then preloadlayout state.y
2360 else state.layout
2362 let evict () =
2363 let module IntSet =
2364 Set.Make (struct type t = int let compare = (-) end) in
2365 let set =
2366 List.fold_left (fun s l -> IntSet.add l.pageno s)
2367 IntSet.empty preloadedpages
2369 let evictedpages =
2370 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2371 if not (IntSet.mem pageno set)
2372 then (
2373 wcmd "freepage %s" opaque;
2374 key :: accu
2376 else accu
2377 ) state.pagemap []
2379 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2381 evict ();
2382 state.currently <- Idle;
2383 if gen = state.gen
2384 then (
2385 tilepage l.pageno pageopaque state.layout;
2386 load state.layout;
2387 load preloadedpages;
2388 if pagevisible state.layout l.pageno
2389 && layoutready state.layout
2390 then G.postRedisplay "page";
2393 | Some (layout, _, _) ->
2394 state.currently <- Idle;
2395 tilepage l.pageno pageopaque layout;
2396 load state.layout
2397 end;
2399 | _ ->
2400 dolog "Inconsistent loading state";
2401 logcurrently state.currently;
2402 exit 1
2405 | "tile" ->
2406 let (x, y, opaque, size, t) =
2408 Scanf.sscanf args "%u %u %s %u %f"
2409 (fun x y p size t -> (x, y, p, size, t))
2410 with exn ->
2411 dolog "error processing 'tile' %S: %s"
2412 cmds (Printexc.to_string exn);
2413 exit 1;
2415 begin match state.currently with
2416 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2417 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2419 unmappbo opaque;
2420 if tilew != conf.tilew || tileh != conf.tileh
2421 then (
2422 wcmd "freetile %s" opaque;
2423 state.currently <- Idle;
2424 load state.layout;
2426 else (
2427 puttileopaque l col row gen cs angle opaque size t;
2428 state.memused <- state.memused + size;
2429 state.uioh#infochanged Memused;
2430 gctiles ();
2431 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2432 opaque, size) state.tilelru;
2434 let layout =
2435 match state.throttle with
2436 | None -> state.layout
2437 | Some (layout, _, _) -> layout
2440 state.currently <- Idle;
2441 if gen = state.gen
2442 && conf.colorspace = cs
2443 && conf.angle = angle
2444 && tilevisible layout l.pageno x y
2445 then conttiling l.pageno pageopaque;
2447 begin match state.throttle with
2448 | None ->
2449 state.wthack <- false;
2450 preload state.layout;
2451 if gen = state.gen
2452 && conf.colorspace = cs
2453 && conf.angle = angle
2454 && tilevisible state.layout l.pageno x y
2455 then G.postRedisplay "tile nothrottle";
2457 | Some (layout, y, _) ->
2458 let ready = layoutready layout in
2459 if ready
2460 then (
2461 state.wthack <- false;
2462 state.y <- y;
2463 state.layout <- layout;
2464 state.throttle <- None;
2465 G.postRedisplay "throttle";
2467 else load layout;
2468 end;
2471 | _ ->
2472 dolog "Inconsistent tiling state";
2473 logcurrently state.currently;
2474 exit 1
2477 | "pdim" ->
2478 let pdim =
2480 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2481 with exn ->
2482 dolog "error processing 'pdim' %S: %s"
2483 cmds (Printexc.to_string exn);
2484 exit 1;
2486 state.uioh#infochanged Pdim;
2487 state.pdims <- pdim :: state.pdims
2489 | "o" ->
2490 let (l, n, t, h, pos) =
2492 Scanf.sscanf args "%u %u %d %u %n"
2493 (fun l n t h pos -> l, n, t, h, pos)
2494 with exn ->
2495 dolog "error processing 'o' %S: %s"
2496 cmds (Printexc.to_string exn);
2497 exit 1;
2499 let s = String.sub args pos (String.length args - pos) in
2500 let outline = (s, l, (n, float t /. float h, 0.0)) in
2501 begin match state.currently with
2502 | Outlining outlines ->
2503 state.currently <- Outlining (outline :: outlines)
2504 | Idle ->
2505 state.currently <- Outlining [outline]
2506 | currently ->
2507 dolog "invalid outlining state";
2508 logcurrently currently
2511 | "a" ->
2512 let (n, t, h) =
2514 Scanf.sscanf args "%u %u %d"
2515 (fun n t h -> n, t, h)
2516 with exn ->
2517 dolog "error processing 'a' %S: %s"
2518 cmds (Printexc.to_string exn);
2519 exit 1;
2521 gotoanchor (n, float t /. float h, 0.0)
2523 | "info" ->
2524 state.docinfo <- (1, args) :: state.docinfo
2526 | "infoend" ->
2527 state.uioh#infochanged Docinfo;
2528 state.docinfo <- List.rev state.docinfo
2530 | _ ->
2531 dolog "unknown cmd `%S'" cmds
2534 let onhist cb =
2535 let rc = cb.rc in
2536 let action = function
2537 | HCprev -> cbget cb ~-1
2538 | HCnext -> cbget cb 1
2539 | HCfirst -> cbget cb ~-(cb.rc)
2540 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2541 and cancel () = cb.rc <- rc
2542 in (action, cancel)
2545 let search pattern forward =
2546 if String.length pattern > 0
2547 then
2548 let pn, py =
2549 match state.layout with
2550 | [] -> 0, 0
2551 | l :: _ ->
2552 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2554 wcmd "search %d %d %d %d,%s\000"
2555 (btod conf.icase) pn py (btod forward) pattern;
2558 let intentry text key =
2559 let c =
2560 if key >= 32 && key < 127
2561 then Char.chr key
2562 else '\000'
2564 match c with
2565 | '0' .. '9' ->
2566 let text = addchar text c in
2567 TEcont text
2569 | _ ->
2570 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2571 TEcont text
2574 let linknentry text key =
2575 let c =
2576 if key >= 32 && key < 127
2577 then Char.chr key
2578 else '\000'
2580 match c with
2581 | 'a' .. 'z' ->
2582 let text = addchar text c in
2583 TEcont text
2585 | _ ->
2586 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2587 TEcont text
2590 let linkndone f s =
2591 if String.length s > 0
2592 then (
2593 let n =
2594 let l = String.length s in
2595 let rec loop pos n = if pos = l then n else
2596 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2597 loop (pos+1) (n*26 + m)
2598 in loop 0 0
2600 let rec loop n = function
2601 | [] -> ()
2602 | l :: rest ->
2603 match getopaque l.pageno with
2604 | None -> loop n rest
2605 | Some opaque ->
2606 let m = getlinkcount opaque in
2607 if n < m
2608 then (
2609 let under = getlink opaque n in
2610 f under
2612 else loop (n-m) rest
2614 loop n state.layout;
2618 let textentry text key =
2619 if key land 0xff00 = 0xff00
2620 then TEcont text
2621 else TEcont (text ^ Wsi.toutf8 key)
2624 let reqlayout angle proportional =
2625 match state.throttle with
2626 | None ->
2627 if nogeomcmds state.geomcmds
2628 then state.anchor <- getanchor ();
2629 conf.angle <- angle mod 360;
2630 if conf.angle != 0
2631 then (
2632 match state.mode with
2633 | LinkNav _ -> state.mode <- View
2634 | _ -> ()
2636 conf.proportional <- proportional;
2637 invalidate "reqlayout"
2638 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2639 | _ -> ()
2642 let settrim trimmargins trimfuzz =
2643 if nogeomcmds state.geomcmds
2644 then state.anchor <- getanchor ();
2645 conf.trimmargins <- trimmargins;
2646 conf.trimfuzz <- trimfuzz;
2647 let x0, y0, x1, y1 = trimfuzz in
2648 invalidate "settrim"
2649 (fun () ->
2650 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2651 flushpages ();
2654 let setzoom zoom =
2655 match state.throttle with
2656 | None ->
2657 let zoom = max 0.01 zoom in
2658 if zoom <> conf.zoom
2659 then (
2660 state.prevzoom <- conf.zoom;
2661 conf.zoom <- zoom;
2662 reshape conf.winw conf.winh;
2663 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2666 | Some (layout, y, started) ->
2667 let time =
2668 match conf.maxwait with
2669 | None -> 0.0
2670 | Some t -> t
2672 let dt = now () -. started in
2673 if dt > time
2674 then (
2675 state.y <- y;
2676 load layout;
2680 let setcolumns mode columns coverA coverB =
2681 state.prevcolumns <- Some (conf.columns, conf.zoom);
2682 if columns < 0
2683 then (
2684 if isbirdseye mode
2685 then showtext '!' "split mode doesn't work in bird's eye"
2686 else (
2687 conf.columns <- Csplit (-columns, [||]);
2688 state.x <- 0;
2689 conf.zoom <- 1.0;
2692 else (
2693 if columns < 2
2694 then (
2695 conf.columns <- Csingle [||];
2696 state.x <- 0;
2697 setzoom 1.0;
2699 else (
2700 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2701 conf.zoom <- 1.0;
2704 reshape conf.winw conf.winh;
2707 let enterbirdseye () =
2708 let zoom = float conf.thumbw /. float conf.winw in
2709 let birdseyepageno =
2710 let cy = conf.winh / 2 in
2711 let fold = function
2712 | [] -> 0
2713 | l :: rest ->
2714 let rec fold best = function
2715 | [] -> best.pageno
2716 | l :: rest ->
2717 let d = cy - (l.pagedispy + l.pagevh/2)
2718 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2719 if abs d < abs dbest
2720 then fold l rest
2721 else best.pageno
2722 in fold l rest
2724 fold state.layout
2726 state.mode <- Birdseye (
2727 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2729 conf.zoom <- zoom;
2730 conf.presentation <- false;
2731 conf.interpagespace <- 10;
2732 conf.hlinks <- false;
2733 state.x <- 0;
2734 state.mstate <- Mnone;
2735 conf.maxwait <- None;
2736 conf.columns <- (
2737 match conf.beyecolumns with
2738 | Some c ->
2739 conf.zoom <- 1.0;
2740 Cmulti ((c, 0, 0), [||])
2741 | None -> Csingle [||]
2743 Wsi.setcursor Wsi.CURSOR_INHERIT;
2744 if conf.verbose
2745 then
2746 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2747 (100.0*.zoom)
2748 else
2749 state.text <- ""
2751 reshape conf.winw conf.winh;
2754 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2755 state.mode <- View;
2756 conf.zoom <- c.zoom;
2757 conf.presentation <- c.presentation;
2758 conf.interpagespace <- c.interpagespace;
2759 conf.maxwait <- c.maxwait;
2760 conf.hlinks <- c.hlinks;
2761 conf.beyecolumns <- (
2762 match conf.columns with
2763 | Cmulti ((c, _, _), _) -> Some c
2764 | Csingle _ -> None
2765 | Csplit _ -> failwith "leaving bird's eye split mode"
2767 conf.columns <- (
2768 match c.columns with
2769 | Cmulti (c, _) -> Cmulti (c, [||])
2770 | Csingle _ -> Csingle [||]
2771 | Csplit (c, _) -> Csplit (c, [||])
2773 state.x <- leftx;
2774 if conf.verbose
2775 then
2776 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2777 (100.0*.conf.zoom)
2779 reshape conf.winw conf.winh;
2780 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2783 let togglebirdseye () =
2784 match state.mode with
2785 | Birdseye vals -> leavebirdseye vals true
2786 | View -> enterbirdseye ()
2787 | _ -> ()
2790 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2791 let pageno = max 0 (pageno - incr) in
2792 let rec loop = function
2793 | [] -> gotopage1 pageno 0
2794 | l :: _ when l.pageno = pageno ->
2795 if l.pagedispy >= 0 && l.pagey = 0
2796 then G.postRedisplay "upbirdseye"
2797 else gotopage1 pageno 0
2798 | _ :: rest -> loop rest
2800 loop state.layout;
2801 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2804 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2805 let pageno = min (state.pagecount - 1) (pageno + incr) in
2806 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2807 let rec loop = function
2808 | [] ->
2809 let y, h = getpageyh pageno in
2810 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2811 gotoy (clamp dy)
2812 | l :: _ when l.pageno = pageno ->
2813 if l.pagevh != l.pageh
2814 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2815 else G.postRedisplay "downbirdseye"
2816 | _ :: rest -> loop rest
2818 loop state.layout
2821 let optentry mode _ key =
2822 let btos b = if b then "on" else "off" in
2823 if key >= 32 && key < 127
2824 then
2825 let c = Char.chr key in
2826 match c with
2827 | 's' ->
2828 let ondone s =
2829 try conf.scrollstep <- int_of_string s with exc ->
2830 state.text <- Printf.sprintf "bad integer `%s': %s"
2831 s (Printexc.to_string exc)
2833 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2835 | 'A' ->
2836 let ondone s =
2838 conf.autoscrollstep <- int_of_string s;
2839 if state.autoscroll <> None
2840 then state.autoscroll <- Some conf.autoscrollstep
2841 with exc ->
2842 state.text <- Printf.sprintf "bad integer `%s': %s"
2843 s (Printexc.to_string exc)
2845 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2847 | 'C' ->
2848 let ondone s =
2850 let n, a, b = multicolumns_of_string s in
2851 setcolumns mode n a b;
2852 with exc ->
2853 state.text <- Printf.sprintf "bad columns `%s': %s"
2854 s (Printexc.to_string exc)
2856 TEswitch ("columns: ", "", None, textentry, ondone, true)
2858 | 'Z' ->
2859 let ondone s =
2861 let zoom = float (int_of_string s) /. 100.0 in
2862 setzoom zoom
2863 with exc ->
2864 state.text <- Printf.sprintf "bad integer `%s': %s"
2865 s (Printexc.to_string exc)
2867 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2869 | 't' ->
2870 let ondone s =
2872 conf.thumbw <- bound (int_of_string s) 2 4096;
2873 state.text <-
2874 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2875 begin match mode with
2876 | Birdseye beye ->
2877 leavebirdseye beye false;
2878 enterbirdseye ();
2879 | _ -> ();
2881 with exc ->
2882 state.text <- Printf.sprintf "bad integer `%s': %s"
2883 s (Printexc.to_string exc)
2885 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2887 | 'R' ->
2888 let ondone s =
2889 match try
2890 Some (int_of_string s)
2891 with exc ->
2892 state.text <- Printf.sprintf "bad integer `%s': %s"
2893 s (Printexc.to_string exc);
2894 None
2895 with
2896 | Some angle -> reqlayout angle conf.proportional
2897 | None -> ()
2899 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2901 | 'i' ->
2902 conf.icase <- not conf.icase;
2903 TEdone ("case insensitive search " ^ (btos conf.icase))
2905 | 'p' ->
2906 conf.preload <- not conf.preload;
2907 gotoy state.y;
2908 TEdone ("preload " ^ (btos conf.preload))
2910 | 'v' ->
2911 conf.verbose <- not conf.verbose;
2912 TEdone ("verbose " ^ (btos conf.verbose))
2914 | 'd' ->
2915 conf.debug <- not conf.debug;
2916 TEdone ("debug " ^ (btos conf.debug))
2918 | 'h' ->
2919 conf.maxhfit <- not conf.maxhfit;
2920 state.maxy <- calcheight ();
2921 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2923 | 'c' ->
2924 conf.crophack <- not conf.crophack;
2925 TEdone ("crophack " ^ btos conf.crophack)
2927 | 'a' ->
2928 let s =
2929 match conf.maxwait with
2930 | None ->
2931 conf.maxwait <- Some infinity;
2932 "always wait for page to complete"
2933 | Some _ ->
2934 conf.maxwait <- None;
2935 "show placeholder if page is not ready"
2937 TEdone s
2939 | 'f' ->
2940 conf.underinfo <- not conf.underinfo;
2941 TEdone ("underinfo " ^ btos conf.underinfo)
2943 | 'P' ->
2944 conf.savebmarks <- not conf.savebmarks;
2945 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2947 | 'S' ->
2948 let ondone s =
2950 let pageno, py =
2951 match state.layout with
2952 | [] -> 0, 0
2953 | l :: _ ->
2954 l.pageno, l.pagey
2956 conf.interpagespace <- int_of_string s;
2957 docolumns conf.columns;
2958 state.maxy <- calcheight ();
2959 let y = getpagey pageno in
2960 gotoy (y + py)
2961 with exc ->
2962 state.text <- Printf.sprintf "bad integer `%s': %s"
2963 s (Printexc.to_string exc)
2965 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2967 | 'l' ->
2968 reqlayout conf.angle (not conf.proportional);
2969 TEdone ("proportional display " ^ btos conf.proportional)
2971 | 'T' ->
2972 settrim (not conf.trimmargins) conf.trimfuzz;
2973 TEdone ("trim margins " ^ btos conf.trimmargins)
2975 | 'I' ->
2976 conf.invert <- not conf.invert;
2977 TEdone ("invert colors " ^ btos conf.invert)
2979 | 'x' ->
2980 let ondone s =
2981 cbput state.hists.sel s;
2982 conf.selcmd <- s;
2984 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2985 textentry, ondone, true)
2987 | _ ->
2988 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2989 TEstop
2990 else
2991 TEcont state.text
2994 class type lvsource = object
2995 method getitemcount : int
2996 method getitem : int -> (string * int)
2997 method hasaction : int -> bool
2998 method exit :
2999 uioh:uioh ->
3000 cancel:bool ->
3001 active:int ->
3002 first:int ->
3003 pan:int ->
3004 qsearch:string ->
3005 uioh option
3006 method getactive : int
3007 method getfirst : int
3008 method getqsearch : string
3009 method setqsearch : string -> unit
3010 method getpan : int
3011 end;;
3013 class virtual lvsourcebase = object
3014 val mutable m_active = 0
3015 val mutable m_first = 0
3016 val mutable m_qsearch = ""
3017 val mutable m_pan = 0
3018 method getactive = m_active
3019 method getfirst = m_first
3020 method getqsearch = m_qsearch
3021 method getpan = m_pan
3022 method setqsearch s = m_qsearch <- s
3023 end;;
3025 let withoutlastutf8 s =
3026 let len = String.length s in
3027 if len = 0
3028 then s
3029 else
3030 let rec find pos =
3031 if pos = 0
3032 then pos
3033 else
3034 let b = Char.code s.[pos] in
3035 if b land 0b11000000 = 0b11000000
3036 then pos
3037 else find (pos-1)
3039 let first =
3040 if Char.code s.[len-1] land 0x80 = 0
3041 then len-1
3042 else find (len-1)
3044 String.sub s 0 first;
3047 let textentrykeyboard
3048 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3049 let key =
3050 if key >= 0xffb0 && key <= 0xffb9
3051 then key - 0xffb0 + 48 else key
3053 let enttext te =
3054 state.mode <- Textentry (te, onleave);
3055 state.text <- "";
3056 enttext ();
3057 G.postRedisplay "textentrykeyboard enttext";
3059 let histaction cmd =
3060 match opthist with
3061 | None -> ()
3062 | Some (action, _) ->
3063 state.mode <- Textentry (
3064 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3066 G.postRedisplay "textentry histaction"
3068 match key with
3069 | 0xff08 -> (* backspace *)
3070 let s = withoutlastutf8 text in
3071 let len = String.length s in
3072 if cancelonempty && len = 0
3073 then (
3074 onleave Cancel;
3075 G.postRedisplay "textentrykeyboard after cancel";
3077 else (
3078 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3081 | 0xff0d | 0xff8d -> (* (kp) enter *)
3082 ondone text;
3083 onleave Confirm;
3084 G.postRedisplay "textentrykeyboard after confirm"
3086 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3087 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3088 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3089 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3091 | 0xff1b -> (* escape*)
3092 if String.length text = 0
3093 then (
3094 begin match opthist with
3095 | None -> ()
3096 | Some (_, onhistcancel) -> onhistcancel ()
3097 end;
3098 onleave Cancel;
3099 state.text <- "";
3100 G.postRedisplay "textentrykeyboard after cancel2"
3102 else (
3103 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3106 | 0xff9f | 0xffff -> () (* delete *)
3108 | _ when key != 0
3109 && key land 0xff00 != 0xff00 (* keyboard *)
3110 && key land 0xfe00 != 0xfe00 (* xkb *)
3111 && key land 0xfd00 != 0xfd00 (* 3270 *)
3113 begin match onkey text key with
3114 | TEdone text ->
3115 ondone text;
3116 onleave Confirm;
3117 G.postRedisplay "textentrykeyboard after confirm2";
3119 | TEcont text ->
3120 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3122 | TEstop ->
3123 onleave Cancel;
3124 G.postRedisplay "textentrykeyboard after cancel3"
3126 | TEswitch te ->
3127 state.mode <- Textentry (te, onleave);
3128 G.postRedisplay "textentrykeyboard switch";
3129 end;
3131 | _ ->
3132 vlog "unhandled key %s" (Wsi.keyname key)
3135 let firstof first active =
3136 if first > active || abs (first - active) > fstate.maxrows - 1
3137 then max 0 (active - (fstate.maxrows/2))
3138 else first
3141 let calcfirst first active =
3142 if active > first
3143 then
3144 let rows = active - first in
3145 if rows > fstate.maxrows then active - fstate.maxrows else first
3146 else active
3149 let scrollph y maxy =
3150 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3151 let sh = float conf.winh /. sh in
3152 let sh = max sh (float conf.scrollh) in
3154 let percent =
3155 if y = state.maxy
3156 then 1.0
3157 else float y /. float maxy
3159 let position = (float conf.winh -. sh) *. percent in
3161 let position =
3162 if position +. sh > float conf.winh
3163 then float conf.winh -. sh
3164 else position
3166 position, sh;
3169 let coe s = (s :> uioh);;
3171 class listview ~(source:lvsource) ~trusted ~modehash =
3172 object (self)
3173 val m_pan = source#getpan
3174 val m_first = source#getfirst
3175 val m_active = source#getactive
3176 val m_qsearch = source#getqsearch
3177 val m_prev_uioh = state.uioh
3179 method private elemunder y =
3180 let n = y / (fstate.fontsize+1) in
3181 if m_first + n < source#getitemcount
3182 then (
3183 if source#hasaction (m_first + n)
3184 then Some (m_first + n)
3185 else None
3187 else None
3189 method display =
3190 Gl.enable `blend;
3191 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3192 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3193 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3194 GlDraw.color (1., 1., 1.);
3195 Gl.enable `texture_2d;
3196 let fs = fstate.fontsize in
3197 let nfs = fs + 1 in
3198 let ww = fstate.wwidth in
3199 let tabw = 30.0*.ww in
3200 let itemcount = source#getitemcount in
3201 let rec loop row =
3202 if (row - m_first) > fstate.maxrows
3203 then ()
3204 else (
3205 if row >= 0 && row < itemcount
3206 then (
3207 let (s, level) = source#getitem row in
3208 let y = (row - m_first) * nfs in
3209 let x = 5.0 +. float (level + m_pan) *. ww in
3210 if row = m_active
3211 then (
3212 Gl.disable `texture_2d;
3213 GlDraw.polygon_mode `both `line;
3214 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3215 GlDraw.rect (1., float (y + 1))
3216 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3217 GlDraw.polygon_mode `both `fill;
3218 GlDraw.color (1., 1., 1.);
3219 Gl.enable `texture_2d;
3222 let drawtabularstring s =
3223 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3224 if trusted
3225 then
3226 let tabpos = try String.index s '\t' with Not_found -> -1 in
3227 if tabpos > 0
3228 then
3229 let len = String.length s - tabpos - 1 in
3230 let s1 = String.sub s 0 tabpos
3231 and s2 = String.sub s (tabpos + 1) len in
3232 let nx = drawstr x s1 in
3233 let sw = nx -. x in
3234 let x = x +. (max tabw sw) in
3235 drawstr x s2
3236 else
3237 drawstr x s
3238 else
3239 drawstr x s
3241 let _ = drawtabularstring s in
3242 loop (row+1)
3246 loop m_first;
3247 Gl.disable `blend;
3248 Gl.disable `texture_2d;
3250 method updownlevel incr =
3251 let len = source#getitemcount in
3252 let curlevel =
3253 if m_active >= 0 && m_active < len
3254 then snd (source#getitem m_active)
3255 else -1
3257 let rec flow i =
3258 if i = len then i-1 else if i = -1 then 0 else
3259 let _, l = source#getitem i in
3260 if l != curlevel then i else flow (i+incr)
3262 let active = flow m_active in
3263 let first = calcfirst m_first active in
3264 G.postRedisplay "outline updownlevel";
3265 {< m_active = active; m_first = first >}
3267 method private key1 key mask =
3268 let set1 active first qsearch =
3269 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3271 let search active pattern incr =
3272 let dosearch re =
3273 let rec loop n =
3274 if n >= 0 && n < source#getitemcount
3275 then (
3276 let s, _ = source#getitem n in
3278 (try ignore (Str.search_forward re s 0); true
3279 with Not_found -> false)
3280 then Some n
3281 else loop (n + incr)
3283 else None
3285 loop active
3288 let re = Str.regexp_case_fold pattern in
3289 dosearch re
3290 with Failure s ->
3291 state.text <- s;
3292 None
3294 let itemcount = source#getitemcount in
3295 let find start incr =
3296 let rec find i =
3297 if i = -1 || i = itemcount
3298 then -1
3299 else (
3300 if source#hasaction i
3301 then i
3302 else find (i + incr)
3305 find start
3307 let set active first =
3308 let first = bound first 0 (itemcount - fstate.maxrows) in
3309 state.text <- "";
3310 coe {< m_active = active; m_first = first >}
3312 let navigate incr =
3313 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3314 let active, first =
3315 let incr1 = if incr > 0 then 1 else -1 in
3316 if isvisible m_first m_active
3317 then
3318 let next =
3319 let next = m_active + incr in
3320 let next =
3321 if next < 0 || next >= itemcount
3322 then -1
3323 else find next incr1
3325 if next = -1 || abs (m_active - next) > fstate.maxrows
3326 then -1
3327 else next
3329 if next = -1
3330 then
3331 let first = m_first + incr in
3332 let first = bound first 0 (itemcount - 1) in
3333 let next =
3334 let next = m_active + incr in
3335 let next = bound next 0 (itemcount - 1) in
3336 find next ~-incr1
3338 let active = if next = -1 then m_active else next in
3339 active, first
3340 else
3341 let first = min next m_first in
3342 let first =
3343 if abs (next - first) > fstate.maxrows
3344 then first + incr
3345 else first
3347 next, first
3348 else
3349 let first = m_first + incr in
3350 let first = bound first 0 (itemcount - 1) in
3351 let active =
3352 let next = m_active + incr in
3353 let next = bound next 0 (itemcount - 1) in
3354 let next = find next incr1 in
3355 let active =
3356 if next = -1 || abs (m_active - first) > fstate.maxrows
3357 then (
3358 let active = if m_active = -1 then next else m_active in
3359 active
3361 else next
3363 if isvisible first active
3364 then active
3365 else -1
3367 active, first
3369 G.postRedisplay "listview navigate";
3370 set active first;
3372 match key with
3373 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3374 let incr = if key = 0x72 then -1 else 1 in
3375 let active, first =
3376 match search (m_active + incr) m_qsearch incr with
3377 | None ->
3378 state.text <- m_qsearch ^ " [not found]";
3379 m_active, m_first
3380 | Some active ->
3381 state.text <- m_qsearch;
3382 active, firstof m_first active
3384 G.postRedisplay "listview ctrl-r/s";
3385 set1 active first m_qsearch;
3387 | 0xff08 -> (* backspace *)
3388 if String.length m_qsearch = 0
3389 then coe self
3390 else (
3391 let qsearch = withoutlastutf8 m_qsearch in
3392 let len = String.length qsearch in
3393 if len = 0
3394 then (
3395 state.text <- "";
3396 G.postRedisplay "listview empty qsearch";
3397 set1 m_active m_first "";
3399 else
3400 let active, first =
3401 match search m_active qsearch ~-1 with
3402 | None ->
3403 state.text <- qsearch ^ " [not found]";
3404 m_active, m_first
3405 | Some active ->
3406 state.text <- qsearch;
3407 active, firstof m_first active
3409 G.postRedisplay "listview backspace qsearch";
3410 set1 active first qsearch
3413 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3414 let pattern = m_qsearch ^ Wsi.toutf8 key in
3415 let active, first =
3416 match search m_active pattern 1 with
3417 | None ->
3418 state.text <- pattern ^ " [not found]";
3419 m_active, m_first
3420 | Some active ->
3421 state.text <- pattern;
3422 active, firstof m_first active
3424 G.postRedisplay "listview qsearch add";
3425 set1 active first pattern;
3427 | 0xff1b -> (* escape *)
3428 state.text <- "";
3429 if String.length m_qsearch = 0
3430 then (
3431 G.postRedisplay "list view escape";
3432 begin
3433 match
3434 source#exit (coe self) true m_active m_first m_pan m_qsearch
3435 with
3436 | None -> m_prev_uioh
3437 | Some uioh -> uioh
3440 else (
3441 G.postRedisplay "list view kill qsearch";
3442 source#setqsearch "";
3443 coe {< m_qsearch = "" >}
3446 | 0xff0d | 0xff8d -> (* (kp) enter *)
3447 state.text <- "";
3448 let self = {< m_qsearch = "" >} in
3449 source#setqsearch "";
3450 let opt =
3451 G.postRedisplay "listview enter";
3452 if m_active >= 0 && m_active < source#getitemcount
3453 then (
3454 source#exit (coe self) false m_active m_first m_pan "";
3456 else (
3457 source#exit (coe self) true m_active m_first m_pan "";
3460 begin match opt with
3461 | None -> m_prev_uioh
3462 | Some uioh -> uioh
3465 | 0xff9f | 0xffff -> (* (kp) delete *)
3466 coe self
3468 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3469 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3470 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3471 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3473 | 0xff53 | 0xff98 -> (* (kp) right *)
3474 state.text <- "";
3475 G.postRedisplay "listview right";
3476 coe {< m_pan = m_pan - 1 >}
3478 | 0xff51 | 0xff96 -> (* (kp) left *)
3479 state.text <- "";
3480 G.postRedisplay "listview left";
3481 coe {< m_pan = m_pan + 1 >}
3483 | 0xff50 | 0xff95 -> (* (kp) home *)
3484 let active = find 0 1 in
3485 G.postRedisplay "listview home";
3486 set active 0;
3488 | 0xff57 | 0xff9c -> (* (kp) end *)
3489 let first = max 0 (itemcount - fstate.maxrows) in
3490 let active = find (itemcount - 1) ~-1 in
3491 G.postRedisplay "listview end";
3492 set active first;
3494 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3495 coe self
3497 | _ ->
3498 dolog "listview unknown key %#x" key; coe self
3500 method key key mask =
3501 match state.mode with
3502 | Textentry te -> textentrykeyboard key mask te; coe self
3503 | _ -> self#key1 key mask
3505 method button button down x y _ =
3506 let opt =
3507 match button with
3508 | 1 when x > conf.winw - conf.scrollbw ->
3509 G.postRedisplay "listview scroll";
3510 if down
3511 then
3512 let _, position, sh = self#scrollph in
3513 if y > truncate position && y < truncate (position +. sh)
3514 then (
3515 state.mstate <- Mscrolly;
3516 Some (coe self)
3518 else
3519 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3520 let first = truncate (s *. float source#getitemcount) in
3521 let first = min source#getitemcount first in
3522 Some (coe {< m_first = first; m_active = first >})
3523 else (
3524 state.mstate <- Mnone;
3525 Some (coe self);
3527 | 1 when not down ->
3528 begin match self#elemunder y with
3529 | Some n ->
3530 G.postRedisplay "listview click";
3531 source#exit
3532 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3533 | _ ->
3534 Some (coe self)
3536 | n when (n == 4 || n == 5) && not down ->
3537 let len = source#getitemcount in
3538 let first =
3539 if n = 5 && m_first + fstate.maxrows >= len
3540 then
3541 m_first
3542 else
3543 let first = m_first + (if n == 4 then -1 else 1) in
3544 bound first 0 (len - 1)
3546 G.postRedisplay "listview wheel";
3547 Some (coe {< m_first = first >})
3548 | n when (n = 6 || n = 7) && not down ->
3549 let inc = m_first + (if n = 7 then -1 else 1) in
3550 G.postRedisplay "listview hwheel";
3551 Some (coe {< m_pan = m_pan + inc >})
3552 | _ ->
3553 Some (coe self)
3555 match opt with
3556 | None -> m_prev_uioh
3557 | Some uioh -> uioh
3559 method motion _ y =
3560 match state.mstate with
3561 | Mscrolly ->
3562 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3563 let first = truncate (s *. float source#getitemcount) in
3564 let first = min source#getitemcount first in
3565 G.postRedisplay "listview motion";
3566 coe {< m_first = first; m_active = first >}
3567 | _ -> coe self
3569 method pmotion x y =
3570 if x < conf.winw - conf.scrollbw
3571 then
3572 let n =
3573 match self#elemunder y with
3574 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3575 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3577 let o =
3578 if n != m_active
3579 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3580 else self
3582 coe o
3583 else (
3584 Wsi.setcursor Wsi.CURSOR_INHERIT;
3585 coe self
3588 method infochanged _ = ()
3590 method scrollpw = (0, 0.0, 0.0)
3591 method scrollph =
3592 let nfs = fstate.fontsize + 1 in
3593 let y = m_first * nfs in
3594 let itemcount = source#getitemcount in
3595 let maxi = max 0 (itemcount - fstate.maxrows) in
3596 let maxy = maxi * nfs in
3597 let p, h = scrollph y maxy in
3598 conf.scrollbw, p, h
3600 method modehash = modehash
3601 end;;
3603 class outlinelistview ~source =
3604 object (self)
3605 inherit listview
3606 ~source:(source :> lvsource)
3607 ~trusted:false
3608 ~modehash:(findkeyhash conf "outline")
3609 as super
3611 method key key mask =
3612 let calcfirst first active =
3613 if active > first
3614 then
3615 let rows = active - first in
3616 let maxrows =
3617 if String.length state.text = 0
3618 then fstate.maxrows
3619 else fstate.maxrows - 2
3621 if rows > maxrows then active - maxrows else first
3622 else active
3624 let navigate incr =
3625 let active = m_active + incr in
3626 let active = bound active 0 (source#getitemcount - 1) in
3627 let first = calcfirst m_first active in
3628 G.postRedisplay "outline navigate";
3629 coe {< m_active = active; m_first = first >}
3631 let ctrl = Wsi.withctrl mask in
3632 match key with
3633 | 110 when ctrl -> (* ctrl-n *)
3634 source#narrow m_qsearch;
3635 G.postRedisplay "outline ctrl-n";
3636 coe {< m_first = 0; m_active = 0 >}
3638 | 117 when ctrl -> (* ctrl-u *)
3639 source#denarrow;
3640 G.postRedisplay "outline ctrl-u";
3641 state.text <- "";
3642 coe {< m_first = 0; m_active = 0 >}
3644 | 108 when ctrl -> (* ctrl-l *)
3645 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3646 G.postRedisplay "outline ctrl-l";
3647 coe {< m_first = first >}
3649 | 0xff9f | 0xffff -> (* (kp) delete *)
3650 source#remove m_active;
3651 G.postRedisplay "outline delete";
3652 let active = max 0 (m_active-1) in
3653 coe {< m_first = firstof m_first active;
3654 m_active = active >}
3656 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3657 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3658 | 0xff55 | 0xff9a -> (* (kp) prior *)
3659 navigate ~-(fstate.maxrows)
3660 | 0xff56 | 0xff9b -> (* (kp) next *)
3661 navigate fstate.maxrows
3663 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3664 let o =
3665 if ctrl
3666 then (
3667 G.postRedisplay "outline ctrl right";
3668 {< m_pan = m_pan + 1 >}
3670 else self#updownlevel 1
3672 coe o
3674 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3675 let o =
3676 if ctrl
3677 then (
3678 G.postRedisplay "outline ctrl left";
3679 {< m_pan = m_pan - 1 >}
3681 else self#updownlevel ~-1
3683 coe o
3685 | 0xff50 | 0xff95 -> (* (kp) home *)
3686 G.postRedisplay "outline home";
3687 coe {< m_first = 0; m_active = 0 >}
3689 | 0xff57 | 0xff9c -> (* (kp) end *)
3690 let active = source#getitemcount - 1 in
3691 let first = max 0 (active - fstate.maxrows) in
3692 G.postRedisplay "outline end";
3693 coe {< m_active = active; m_first = first >}
3695 | _ -> super#key key mask
3698 let outlinesource usebookmarks =
3699 let empty = [||] in
3700 (object
3701 inherit lvsourcebase
3702 val mutable m_items = empty
3703 val mutable m_orig_items = empty
3704 val mutable m_prev_items = empty
3705 val mutable m_narrow_pattern = ""
3706 val mutable m_hadremovals = false
3708 method getitemcount =
3709 Array.length m_items + (if m_hadremovals then 1 else 0)
3711 method getitem n =
3712 if n == Array.length m_items && m_hadremovals
3713 then
3714 ("[Confirm removal]", 0)
3715 else
3716 let s, n, _ = m_items.(n) in
3717 (s, n)
3719 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3720 ignore (uioh, first, qsearch);
3721 let confrimremoval = m_hadremovals && active = Array.length m_items in
3722 let items =
3723 if String.length m_narrow_pattern = 0
3724 then m_orig_items
3725 else m_items
3727 if not cancel
3728 then (
3729 if not confrimremoval
3730 then(
3731 let _, _, anchor = m_items.(active) in
3732 gotoghyll (getanchory anchor);
3733 m_items <- items;
3735 else (
3736 state.bookmarks <- Array.to_list m_items;
3737 m_orig_items <- m_items;
3740 else m_items <- items;
3741 m_pan <- pan;
3742 None
3744 method hasaction _ = true
3746 method greetmsg =
3747 if Array.length m_items != Array.length m_orig_items
3748 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3749 else ""
3751 method narrow pattern =
3752 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3753 match reopt with
3754 | None -> ()
3755 | Some re ->
3756 let rec loop accu n =
3757 if n = -1
3758 then (
3759 m_narrow_pattern <- pattern;
3760 m_items <- Array.of_list accu
3762 else
3763 let (s, _, _) as o = m_items.(n) in
3764 let accu =
3765 if (try ignore (Str.search_forward re s 0); true
3766 with Not_found -> false)
3767 then o :: accu
3768 else accu
3770 loop accu (n-1)
3772 loop [] (Array.length m_items - 1)
3774 method denarrow =
3775 m_orig_items <- (
3776 if usebookmarks
3777 then Array.of_list state.bookmarks
3778 else state.outlines
3780 m_items <- m_orig_items
3782 method remove m =
3783 if usebookmarks
3784 then
3785 if m >= 0 && m < Array.length m_items
3786 then (
3787 m_hadremovals <- true;
3788 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3789 let n = if n >= m then n+1 else n in
3790 m_items.(n)
3794 method reset anchor items =
3795 m_hadremovals <- false;
3796 if m_orig_items == empty || m_prev_items != items
3797 then (
3798 m_orig_items <- items;
3799 if String.length m_narrow_pattern = 0
3800 then m_items <- items;
3802 m_prev_items <- items;
3803 let rely = getanchory anchor in
3804 let active =
3805 let rec loop n best bestd =
3806 if n = Array.length m_items
3807 then best
3808 else
3809 let (_, _, anchor) = m_items.(n) in
3810 let orely = getanchory anchor in
3811 let d = abs (orely - rely) in
3812 if d < bestd
3813 then loop (n+1) n d
3814 else loop (n+1) best bestd
3816 loop 0 ~-1 max_int
3818 m_active <- active;
3819 m_first <- firstof m_first active
3820 end)
3823 let enterselector usebookmarks =
3824 let source = outlinesource usebookmarks in
3825 fun errmsg ->
3826 let outlines =
3827 if usebookmarks
3828 then Array.of_list state.bookmarks
3829 else state.outlines
3831 if Array.length outlines = 0
3832 then (
3833 showtext ' ' errmsg;
3835 else (
3836 state.text <- source#greetmsg;
3837 Wsi.setcursor Wsi.CURSOR_INHERIT;
3838 let anchor = getanchor () in
3839 source#reset anchor outlines;
3840 state.uioh <- coe (new outlinelistview ~source);
3841 G.postRedisplay "enter selector";
3845 let enteroutlinemode =
3846 let f = enterselector false in
3847 fun ()-> f "Document has no outline";
3850 let enterbookmarkmode =
3851 let f = enterselector true in
3852 fun () -> f "Document has no bookmarks (yet)";
3855 let color_of_string s =
3856 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3857 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3861 let color_to_string (r, g, b) =
3862 let r = truncate (r *. 256.0)
3863 and g = truncate (g *. 256.0)
3864 and b = truncate (b *. 256.0) in
3865 Printf.sprintf "%d/%d/%d" r g b
3868 let irect_of_string s =
3869 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3872 let irect_to_string (x0,y0,x1,y1) =
3873 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3876 let makecheckers () =
3877 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3878 following to say:
3879 converted by Issac Trotts. July 25, 2002 *)
3880 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3881 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3882 let id = GlTex.gen_texture () in
3883 GlTex.bind_texture `texture_2d id;
3884 GlPix.store (`unpack_alignment 1);
3885 GlTex.image2d image;
3886 List.iter (GlTex.parameter ~target:`texture_2d)
3887 [ `wrap_s `repeat;
3888 `wrap_t `repeat;
3889 `mag_filter `nearest;
3890 `min_filter `nearest ];
3894 let setcheckers enabled =
3895 match state.texid with
3896 | None ->
3897 if enabled then state.texid <- Some (makecheckers ())
3899 | Some texid ->
3900 if not enabled
3901 then (
3902 GlTex.delete_texture texid;
3903 state.texid <- None;
3907 let int_of_string_with_suffix s =
3908 let l = String.length s in
3909 let s1, shift =
3910 if l > 1
3911 then
3912 let suffix = Char.lowercase s.[l-1] in
3913 match suffix with
3914 | 'k' -> String.sub s 0 (l-1), 10
3915 | 'm' -> String.sub s 0 (l-1), 20
3916 | 'g' -> String.sub s 0 (l-1), 30
3917 | _ -> s, 0
3918 else s, 0
3920 let n = int_of_string s1 in
3921 let m = n lsl shift in
3922 if m < 0 || m < n
3923 then raise (Failure "value too large")
3924 else m
3927 let string_with_suffix_of_int n =
3928 if n = 0
3929 then "0"
3930 else
3931 let n, s =
3932 if n land ((1 lsl 30) - 1) = 0
3933 then n lsr 30, "G"
3934 else (
3935 if n land ((1 lsl 20) - 1) = 0
3936 then n lsr 20, "M"
3937 else (
3938 if n land ((1 lsl 10) - 1) = 0
3939 then n lsr 10, "K"
3940 else n, ""
3944 let rec loop s n =
3945 let h = n mod 1000 in
3946 let n = n / 1000 in
3947 if n = 0
3948 then string_of_int h ^ s
3949 else (
3950 let s = Printf.sprintf "_%03d%s" h s in
3951 loop s n
3954 loop "" n ^ s;
3957 let defghyllscroll = (40, 8, 32);;
3958 let ghyllscroll_of_string s =
3959 let (n, a, b) as nab =
3960 if s = "default"
3961 then defghyllscroll
3962 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3964 if n <= a || n <= b || a >= b
3965 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3966 nab;
3969 let ghyllscroll_to_string ((n, a, b) as nab) =
3970 if nab = defghyllscroll
3971 then "default"
3972 else Printf.sprintf "%d,%d,%d" n a b;
3975 let describe_location () =
3976 let f (fn, _) l =
3977 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3979 let fn, ln = List.fold_left f (-1, -1) state.layout in
3980 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3981 let percent =
3982 if maxy <= 0
3983 then 100.
3984 else (100. *. (float state.y /. float maxy))
3986 if fn = ln
3987 then
3988 Printf.sprintf "page %d of %d [%.2f%%]"
3989 (fn+1) state.pagecount percent
3990 else
3991 Printf.sprintf
3992 "pages %d-%d of %d [%.2f%%]"
3993 (fn+1) (ln+1) state.pagecount percent
3996 let setpresentationmode v =
3997 let (n, _, _) = getanchor () in
3998 let _, h = getpageyh n in
3999 let ips = if conf.presentation then calcips h else conf.interpagespace in
4000 state.anchor <- (n, 0.0, float ips);
4001 conf.presentation <- v;
4002 if conf.presentation
4003 then (
4004 if not conf.scrollbarinpm
4005 then state.scrollw <- 0;
4007 else state.scrollw <- conf.scrollbw;
4008 represent ();
4011 let enterinfomode =
4012 let btos b = if b then "\xe2\x88\x9a" else "" in
4013 let showextended = ref false in
4014 let leave mode = function
4015 | Confirm -> state.mode <- mode
4016 | Cancel -> state.mode <- mode in
4017 let src =
4018 (object
4019 val mutable m_first_time = true
4020 val mutable m_l = []
4021 val mutable m_a = [||]
4022 val mutable m_prev_uioh = nouioh
4023 val mutable m_prev_mode = View
4025 inherit lvsourcebase
4027 method reset prev_mode prev_uioh =
4028 m_a <- Array.of_list (List.rev m_l);
4029 m_l <- [];
4030 m_prev_mode <- prev_mode;
4031 m_prev_uioh <- prev_uioh;
4032 if m_first_time
4033 then (
4034 let rec loop n =
4035 if n >= Array.length m_a
4036 then ()
4037 else
4038 match m_a.(n) with
4039 | _, _, _, Action _ -> m_active <- n
4040 | _ -> loop (n+1)
4042 loop 0;
4043 m_first_time <- false;
4046 method int name get set =
4047 m_l <-
4048 (name, `int get, 1, Action (
4049 fun u ->
4050 let ondone s =
4051 try set (int_of_string s)
4052 with exn ->
4053 state.text <- Printf.sprintf "bad integer `%s': %s"
4054 s (Printexc.to_string exn)
4056 state.text <- "";
4057 let te = name ^ ": ", "", None, intentry, ondone, true in
4058 state.mode <- Textentry (te, leave m_prev_mode);
4060 )) :: m_l
4062 method int_with_suffix name get set =
4063 m_l <-
4064 (name, `intws get, 1, Action (
4065 fun u ->
4066 let ondone s =
4067 try set (int_of_string_with_suffix s)
4068 with exn ->
4069 state.text <- Printf.sprintf "bad integer `%s': %s"
4070 s (Printexc.to_string exn)
4072 state.text <- "";
4073 let te =
4074 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4076 state.mode <- Textentry (te, leave m_prev_mode);
4078 )) :: m_l
4080 method bool ?(offset=1) ?(btos=btos) name get set =
4081 m_l <-
4082 (name, `bool (btos, get), offset, Action (
4083 fun u ->
4084 let v = get () in
4085 set (not v);
4087 )) :: m_l
4089 method color name get set =
4090 m_l <-
4091 (name, `color get, 1, Action (
4092 fun u ->
4093 let invalid = (nan, nan, nan) in
4094 let ondone s =
4095 let c =
4096 try color_of_string s
4097 with exn ->
4098 state.text <- Printf.sprintf "bad color `%s': %s"
4099 s (Printexc.to_string exn);
4100 invalid
4102 if c <> invalid
4103 then set c;
4105 let te = name ^ ": ", "", None, textentry, ondone, true in
4106 state.text <- color_to_string (get ());
4107 state.mode <- Textentry (te, leave m_prev_mode);
4109 )) :: m_l
4111 method string name get set =
4112 m_l <-
4113 (name, `string get, 1, Action (
4114 fun u ->
4115 let ondone s = set s in
4116 let te = name ^ ": ", "", None, textentry, ondone, true in
4117 state.mode <- Textentry (te, leave m_prev_mode);
4119 )) :: m_l
4121 method colorspace name get set =
4122 m_l <-
4123 (name, `string get, 1, Action (
4124 fun _ ->
4125 let source =
4126 let vals = [| "rgb"; "bgr"; "gray" |] in
4127 (object
4128 inherit lvsourcebase
4130 initializer
4131 m_active <- int_of_colorspace conf.colorspace;
4132 m_first <- 0;
4134 method getitemcount = Array.length vals
4135 method getitem n = (vals.(n), 0)
4136 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4137 ignore (uioh, first, pan, qsearch);
4138 if not cancel then set active;
4139 None
4140 method hasaction _ = true
4141 end)
4143 state.text <- "";
4144 let modehash = findkeyhash conf "info" in
4145 coe (new listview ~source ~trusted:true ~modehash)
4146 )) :: m_l
4148 method caption s offset =
4149 m_l <- (s, `empty, offset, Noaction) :: m_l
4151 method caption2 s f offset =
4152 m_l <- (s, `string f, offset, Noaction) :: m_l
4154 method getitemcount = Array.length m_a
4156 method getitem n =
4157 let tostr = function
4158 | `int f -> string_of_int (f ())
4159 | `intws f -> string_with_suffix_of_int (f ())
4160 | `string f -> f ()
4161 | `color f -> color_to_string (f ())
4162 | `bool (btos, f) -> btos (f ())
4163 | `empty -> ""
4165 let name, t, offset, _ = m_a.(n) in
4166 ((let s = tostr t in
4167 if String.length s > 0
4168 then Printf.sprintf "%s\t%s" name s
4169 else name),
4170 offset)
4172 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4173 let uiohopt =
4174 if not cancel
4175 then (
4176 m_qsearch <- qsearch;
4177 let uioh =
4178 match m_a.(active) with
4179 | _, _, _, Action f -> f uioh
4180 | _ -> uioh
4182 Some uioh
4184 else None
4186 m_active <- active;
4187 m_first <- first;
4188 m_pan <- pan;
4189 uiohopt
4191 method hasaction n =
4192 match m_a.(n) with
4193 | _, _, _, Action _ -> true
4194 | _ -> false
4195 end)
4197 let rec fillsrc prevmode prevuioh =
4198 let sep () = src#caption "" 0 in
4199 let colorp name get set =
4200 src#string name
4201 (fun () -> color_to_string (get ()))
4202 (fun v ->
4204 let c = color_of_string v in
4205 set c
4206 with exn ->
4207 state.text <- Printf.sprintf "bad color `%s': %s"
4208 v (Printexc.to_string exn);
4211 let oldmode = state.mode in
4212 let birdseye = isbirdseye state.mode in
4214 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4216 src#bool "presentation mode"
4217 (fun () -> conf.presentation)
4218 (fun v -> setpresentationmode v);
4220 src#bool "ignore case in searches"
4221 (fun () -> conf.icase)
4222 (fun v -> conf.icase <- v);
4224 src#bool "preload"
4225 (fun () -> conf.preload)
4226 (fun v -> conf.preload <- v);
4228 src#bool "highlight links"
4229 (fun () -> conf.hlinks)
4230 (fun v -> conf.hlinks <- v);
4232 src#bool "under info"
4233 (fun () -> conf.underinfo)
4234 (fun v -> conf.underinfo <- v);
4236 src#bool "persistent bookmarks"
4237 (fun () -> conf.savebmarks)
4238 (fun v -> conf.savebmarks <- v);
4240 src#bool "proportional display"
4241 (fun () -> conf.proportional)
4242 (fun v -> reqlayout conf.angle v);
4244 src#bool "trim margins"
4245 (fun () -> conf.trimmargins)
4246 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4248 src#bool "persistent location"
4249 (fun () -> conf.jumpback)
4250 (fun v -> conf.jumpback <- v);
4252 sep ();
4253 src#int "inter-page space"
4254 (fun () -> conf.interpagespace)
4255 (fun n ->
4256 conf.interpagespace <- n;
4257 docolumns conf.columns;
4258 let pageno, py =
4259 match state.layout with
4260 | [] -> 0, 0
4261 | l :: _ ->
4262 l.pageno, l.pagey
4264 state.maxy <- calcheight ();
4265 let y = getpagey pageno in
4266 gotoy (y + py)
4269 src#int "page bias"
4270 (fun () -> conf.pagebias)
4271 (fun v -> conf.pagebias <- v);
4273 src#int "scroll step"
4274 (fun () -> conf.scrollstep)
4275 (fun n -> conf.scrollstep <- n);
4277 src#int "horizontal scroll step"
4278 (fun () -> conf.hscrollstep)
4279 (fun v -> conf.hscrollstep <- v);
4281 src#int "auto scroll step"
4282 (fun () ->
4283 match state.autoscroll with
4284 | Some step -> step
4285 | _ -> conf.autoscrollstep)
4286 (fun n ->
4287 if state.autoscroll <> None
4288 then state.autoscroll <- Some n;
4289 conf.autoscrollstep <- n);
4291 src#int "zoom"
4292 (fun () -> truncate (conf.zoom *. 100.))
4293 (fun v -> setzoom ((float v) /. 100.));
4295 src#int "rotation"
4296 (fun () -> conf.angle)
4297 (fun v -> reqlayout v conf.proportional);
4299 src#int "scroll bar width"
4300 (fun () -> state.scrollw)
4301 (fun v ->
4302 state.scrollw <- v;
4303 conf.scrollbw <- v;
4304 reshape conf.winw conf.winh;
4307 src#int "scroll handle height"
4308 (fun () -> conf.scrollh)
4309 (fun v -> conf.scrollh <- v;);
4311 src#int "thumbnail width"
4312 (fun () -> conf.thumbw)
4313 (fun v ->
4314 conf.thumbw <- min 4096 v;
4315 match oldmode with
4316 | Birdseye beye ->
4317 leavebirdseye beye false;
4318 enterbirdseye ()
4319 | _ -> ()
4322 let mode = state.mode in
4323 src#string "columns"
4324 (fun () ->
4325 match conf.columns with
4326 | Csingle _ -> "1"
4327 | Cmulti (multi, _) -> multicolumns_to_string multi
4328 | Csplit (count, _) -> "-" ^ string_of_int count
4330 (fun v ->
4331 let n, a, b = multicolumns_of_string v in
4332 setcolumns mode n a b);
4334 sep ();
4335 src#caption "Presentation mode" 0;
4336 src#bool "scrollbar visible"
4337 (fun () -> conf.scrollbarinpm)
4338 (fun v ->
4339 if v != conf.scrollbarinpm
4340 then (
4341 conf.scrollbarinpm <- v;
4342 if conf.presentation
4343 then (
4344 state.scrollw <- if v then conf.scrollbw else 0;
4345 reshape conf.winw conf.winh;
4350 sep ();
4351 src#caption "Pixmap cache" 0;
4352 src#int_with_suffix "size (advisory)"
4353 (fun () -> conf.memlimit)
4354 (fun v -> conf.memlimit <- v);
4356 src#caption2 "used"
4357 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4358 (string_with_suffix_of_int state.memused)
4359 (Hashtbl.length state.tilemap)) 1;
4361 sep ();
4362 src#caption "Layout" 0;
4363 src#caption2 "Dimension"
4364 (fun () ->
4365 Printf.sprintf "%dx%d (virtual %dx%d)"
4366 conf.winw conf.winh
4367 state.w state.maxy)
4369 if conf.debug
4370 then
4371 src#caption2 "Position" (fun () ->
4372 Printf.sprintf "%dx%d" state.x state.y
4374 else
4375 src#caption2 "Visible" (fun () -> describe_location ()) 1
4378 sep ();
4379 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4380 "Save these parameters as global defaults at exit"
4381 (fun () -> conf.bedefault)
4382 (fun v -> conf.bedefault <- v)
4385 sep ();
4386 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4387 src#bool ~offset:0 ~btos "Extended parameters"
4388 (fun () -> !showextended)
4389 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4390 if !showextended
4391 then (
4392 src#bool "checkers"
4393 (fun () -> conf.checkers)
4394 (fun v -> conf.checkers <- v; setcheckers v);
4395 src#bool "update cursor"
4396 (fun () -> conf.updatecurs)
4397 (fun v -> conf.updatecurs <- v);
4398 src#bool "verbose"
4399 (fun () -> conf.verbose)
4400 (fun v -> conf.verbose <- v);
4401 src#bool "invert colors"
4402 (fun () -> conf.invert)
4403 (fun v -> conf.invert <- v);
4404 src#bool "max fit"
4405 (fun () -> conf.maxhfit)
4406 (fun v -> conf.maxhfit <- v);
4407 src#bool "redirect stderr"
4408 (fun () -> conf.redirectstderr)
4409 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4410 src#string "uri launcher"
4411 (fun () -> conf.urilauncher)
4412 (fun v -> conf.urilauncher <- v);
4413 src#string "path launcher"
4414 (fun () -> conf.pathlauncher)
4415 (fun v -> conf.pathlauncher <- v);
4416 src#string "tile size"
4417 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4418 (fun v ->
4420 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4421 conf.tilew <- max 64 w;
4422 conf.tileh <- max 64 h;
4423 flushtiles ();
4424 with exn ->
4425 state.text <- Printf.sprintf "bad tile size `%s': %s"
4426 v (Printexc.to_string exn));
4427 src#int "texture count"
4428 (fun () -> conf.texcount)
4429 (fun v ->
4430 if realloctexts v
4431 then conf.texcount <- v
4432 else showtext '!' " Failed to set texture count please retry later"
4434 src#int "slice height"
4435 (fun () -> conf.sliceheight)
4436 (fun v ->
4437 conf.sliceheight <- v;
4438 wcmd "sliceh %d" conf.sliceheight;
4440 src#int "anti-aliasing level"
4441 (fun () -> conf.aalevel)
4442 (fun v ->
4443 conf.aalevel <- bound v 0 8;
4444 state.anchor <- getanchor ();
4445 opendoc state.path state.password;
4447 src#string "page scroll scaling factor"
4448 (fun () -> string_of_float conf.pgscale)
4449 (fun v ->
4451 let s = float_of_string v in
4452 conf.pgscale <- s
4453 with exn ->
4454 state.text <- Printf.sprintf
4455 "bad page scroll scaling factor `%s': %s"
4456 v (Printexc.to_string exn)
4459 src#int "ui font size"
4460 (fun () -> fstate.fontsize)
4461 (fun v -> setfontsize (bound v 5 100));
4462 src#int "hint font size"
4463 (fun () -> conf.hfsize)
4464 (fun v -> conf.hfsize <- bound v 5 100);
4465 colorp "background color"
4466 (fun () -> conf.bgcolor)
4467 (fun v -> conf.bgcolor <- v);
4468 src#bool "crop hack"
4469 (fun () -> conf.crophack)
4470 (fun v -> conf.crophack <- v);
4471 src#string "trim fuzz"
4472 (fun () -> irect_to_string conf.trimfuzz)
4473 (fun v ->
4475 conf.trimfuzz <- irect_of_string v;
4476 if conf.trimmargins
4477 then settrim true conf.trimfuzz;
4478 with exn ->
4479 state.text <- Printf.sprintf "bad irect `%s': %s"
4480 v (Printexc.to_string exn)
4482 src#string "throttle"
4483 (fun () ->
4484 match conf.maxwait with
4485 | None -> "show place holder if page is not ready"
4486 | Some time ->
4487 if time = infinity
4488 then "wait for page to fully render"
4489 else
4490 "wait " ^ string_of_float time
4491 ^ " seconds before showing placeholder"
4493 (fun v ->
4495 let f = float_of_string v in
4496 if f <= 0.0
4497 then conf.maxwait <- None
4498 else conf.maxwait <- Some f
4499 with exn ->
4500 state.text <- Printf.sprintf "bad time `%s': %s"
4501 v (Printexc.to_string exn)
4503 src#string "ghyll scroll"
4504 (fun () ->
4505 match conf.ghyllscroll with
4506 | None -> ""
4507 | Some nab -> ghyllscroll_to_string nab
4509 (fun v ->
4511 let gs =
4512 if String.length v = 0
4513 then None
4514 else Some (ghyllscroll_of_string v)
4516 conf.ghyllscroll <- gs
4517 with exn ->
4518 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4519 v (Printexc.to_string exn)
4521 src#string "selection command"
4522 (fun () -> conf.selcmd)
4523 (fun v -> conf.selcmd <- v);
4524 src#colorspace "color space"
4525 (fun () -> colorspace_to_string conf.colorspace)
4526 (fun v ->
4527 conf.colorspace <- colorspace_of_int v;
4528 wcmd "cs %d" v;
4529 load state.layout;
4531 if pbousable ()
4532 then
4533 src#bool "use PBO"
4534 (fun () -> conf.usepbo)
4535 (fun v -> conf.usepbo <- v);
4536 src#bool "mouse wheel scrolls pages"
4537 (fun () -> conf.wheelbypage)
4538 (fun v -> conf.wheelbypage <- v);
4541 sep ();
4542 src#caption "Document" 0;
4543 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4544 src#caption2 "Pages"
4545 (fun () -> string_of_int state.pagecount) 1;
4546 src#caption2 "Dimensions"
4547 (fun () -> string_of_int (List.length state.pdims)) 1;
4548 if conf.trimmargins
4549 then (
4550 sep ();
4551 src#caption "Trimmed margins" 0;
4552 src#caption2 "Dimensions"
4553 (fun () -> string_of_int (List.length state.pdims)) 1;
4556 sep ();
4557 src#caption "OpenGL" 0;
4558 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4559 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4560 src#reset prevmode prevuioh;
4562 fun () ->
4563 state.text <- "";
4564 let prevmode = state.mode
4565 and prevuioh = state.uioh in
4566 fillsrc prevmode prevuioh;
4567 let source = (src :> lvsource) in
4568 let modehash = findkeyhash conf "info" in
4569 state.uioh <- coe (object (self)
4570 inherit listview ~source ~trusted:true ~modehash as super
4571 val mutable m_prevmemused = 0
4572 method infochanged = function
4573 | Memused ->
4574 if m_prevmemused != state.memused
4575 then (
4576 m_prevmemused <- state.memused;
4577 G.postRedisplay "memusedchanged";
4579 | Pdim -> G.postRedisplay "pdimchanged"
4580 | Docinfo -> fillsrc prevmode prevuioh
4582 method key key mask =
4583 if not (Wsi.withctrl mask)
4584 then
4585 match key with
4586 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4587 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4588 | _ -> super#key key mask
4589 else super#key key mask
4590 end);
4591 G.postRedisplay "info";
4594 let enterhelpmode =
4595 let source =
4596 (object
4597 inherit lvsourcebase
4598 method getitemcount = Array.length state.help
4599 method getitem n =
4600 let s, l, _ = state.help.(n) in
4601 (s, l)
4603 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4604 let optuioh =
4605 if not cancel
4606 then (
4607 m_qsearch <- qsearch;
4608 match state.help.(active) with
4609 | _, _, Action f -> Some (f uioh)
4610 | _ -> Some (uioh)
4612 else None
4614 m_active <- active;
4615 m_first <- first;
4616 m_pan <- pan;
4617 optuioh
4619 method hasaction n =
4620 match state.help.(n) with
4621 | _, _, Action _ -> true
4622 | _ -> false
4624 initializer
4625 m_active <- -1
4626 end)
4627 in fun () ->
4628 let modehash = findkeyhash conf "help" in
4629 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4630 G.postRedisplay "help";
4633 let entermsgsmode =
4634 let msgsource =
4635 let re = Str.regexp "[\r\n]" in
4636 (object
4637 inherit lvsourcebase
4638 val mutable m_items = [||]
4640 method getitemcount = 1 + Array.length m_items
4642 method getitem n =
4643 if n = 0
4644 then "[Clear]", 0
4645 else m_items.(n-1), 0
4647 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4648 ignore uioh;
4649 if not cancel
4650 then (
4651 if active = 0
4652 then Buffer.clear state.errmsgs;
4653 m_qsearch <- qsearch;
4655 m_active <- active;
4656 m_first <- first;
4657 m_pan <- pan;
4658 None
4660 method hasaction n =
4661 n = 0
4663 method reset =
4664 state.newerrmsgs <- false;
4665 let l = Str.split re (Buffer.contents state.errmsgs) in
4666 m_items <- Array.of_list l
4668 initializer
4669 m_active <- 0
4670 end)
4671 in fun () ->
4672 state.text <- "";
4673 msgsource#reset;
4674 let source = (msgsource :> lvsource) in
4675 let modehash = findkeyhash conf "listview" in
4676 state.uioh <- coe (object
4677 inherit listview ~source ~trusted:false ~modehash as super
4678 method display =
4679 if state.newerrmsgs
4680 then msgsource#reset;
4681 super#display
4682 end);
4683 G.postRedisplay "msgs";
4686 let quickbookmark ?title () =
4687 match state.layout with
4688 | [] -> ()
4689 | l :: _ ->
4690 let title =
4691 match title with
4692 | None ->
4693 let sec = Unix.gettimeofday () in
4694 let tm = Unix.localtime sec in
4695 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4696 (l.pageno+1)
4697 tm.Unix.tm_mday
4698 tm.Unix.tm_mon
4699 (tm.Unix.tm_year + 1900)
4700 tm.Unix.tm_hour
4701 tm.Unix.tm_min
4702 | Some title -> title
4704 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4707 let doreshape w h =
4708 state.fullscreen <- None;
4709 Wsi.reshape w h;
4712 let setautoscrollspeed step goingdown =
4713 let incr = max 1 ((abs step) / 2) in
4714 let incr = if goingdown then incr else -incr in
4715 let astep = step + incr in
4716 state.autoscroll <- Some astep;
4719 let gotounder = function
4720 | Ulinkgoto (pageno, top) ->
4721 if pageno >= 0
4722 then (
4723 addnav ();
4724 gotopage1 pageno top;
4727 | Ulinkuri s ->
4728 gotouri s
4730 | Uremote (filename, pageno) ->
4731 let path =
4732 if Sys.file_exists filename
4733 then filename
4734 else
4735 let dir = Filename.dirname state.path in
4736 let path = Filename.concat dir filename in
4737 if Sys.file_exists path
4738 then path
4739 else ""
4741 if String.length path > 0
4742 then (
4743 let anchor = getanchor () in
4744 let ranchor = state.path, state.password, anchor in
4745 state.anchor <- (pageno, 0.0, 0.0);
4746 state.ranchors <- ranchor :: state.ranchors;
4747 opendoc path "";
4749 else showtext '!' ("Could not find " ^ filename)
4751 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4754 let canpan () =
4755 match conf.columns with
4756 | Csplit _ -> true
4757 | _ -> conf.zoom > 1.0
4760 let existsinrow pageno (columns, coverA, coverB) p =
4761 let last = ((pageno - coverA) mod columns) + columns in
4762 let rec any = function
4763 | [] -> false
4764 | l :: rest ->
4765 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4766 then p l
4767 else (
4768 if not (p l)
4769 then (if l.pageno = last then false else any rest)
4770 else true
4773 any state.layout
4776 let nextpage () =
4777 match state.layout with
4778 | [] -> ()
4779 | l :: rest ->
4780 match conf.columns with
4781 | Csingle _ ->
4782 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4783 then
4784 let y = clamp (pgscale conf.winh) in
4785 gotoghyll y
4786 else
4787 let pageno = min (l.pageno+1) (state.pagecount-1) in
4788 gotoghyll (getpagey pageno)
4789 | Cmulti ((c, _, _) as cl, _) ->
4790 if conf.presentation
4791 && (existsinrow l.pageno cl
4792 (fun l -> 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+c) (state.pagecount-1) in
4798 gotoghyll (getpagey pageno)
4799 | Csplit (n, _) ->
4800 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4801 then
4802 let pagey, pageh = getpageyh l.pageno in
4803 let pagey = pagey + pageh * l.pagecol in
4804 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4805 gotoghyll (pagey + pageh + ips)
4808 let prevpage () =
4809 match state.layout with
4810 | [] -> ()
4811 | l :: _ ->
4812 match conf.columns with
4813 | Csingle _ ->
4814 if conf.presentation && l.pagey != 0
4815 then
4816 gotoghyll (clamp (pgscale ~-(conf.winh)))
4817 else
4818 let pageno = max 0 (l.pageno-1) in
4819 gotoghyll (getpagey pageno)
4820 | Cmulti ((c, _, coverB) as cl, _) ->
4821 if conf.presentation &&
4822 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4823 then
4824 gotoghyll (clamp (pgscale ~-(conf.winh)))
4825 else
4826 let decr =
4827 if l.pageno = state.pagecount - coverB
4828 then 1
4829 else c
4831 let pageno = max 0 (l.pageno-decr) in
4832 gotoghyll (getpagey pageno)
4833 | Csplit (n, _) ->
4834 let y =
4835 if l.pagecol = 0
4836 then
4837 if l.pageno = 0
4838 then l.pagey
4839 else
4840 let pageno = max 0 (l.pageno-1) in
4841 let pagey, pageh = getpageyh pageno in
4842 pagey + (n-1)*pageh
4843 else
4844 let pagey, pageh = getpageyh l.pageno in
4845 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4847 gotoghyll y
4850 let viewkeyboard key mask =
4851 let enttext te =
4852 let mode = state.mode in
4853 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4854 state.text <- "";
4855 enttext ();
4856 G.postRedisplay "view:enttext"
4858 let ctrl = Wsi.withctrl mask in
4859 let key =
4860 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4862 match key with
4863 | 81 -> (* Q *)
4864 exit 0
4866 | 0xff63 -> (* insert *)
4867 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4868 then (
4869 state.mode <- LinkNav (Ltgendir 0);
4870 gotoy state.y;
4872 else showtext '!' "Keyboard link navigation does not work under rotation"
4874 | 0xff1b | 113 -> (* escape / q *)
4875 begin match state.mstate with
4876 | Mzoomrect _ ->
4877 state.mstate <- Mnone;
4878 Wsi.setcursor Wsi.CURSOR_INHERIT;
4879 G.postRedisplay "kill zoom rect";
4880 | _ ->
4881 begin match state.mode with
4882 | LinkNav _ ->
4883 state.mode <- View;
4884 G.postRedisplay "esc leave linknav"
4885 | _ ->
4886 match state.ranchors with
4887 | [] -> raise Quit
4888 | (path, password, anchor) :: rest ->
4889 state.ranchors <- rest;
4890 state.anchor <- anchor;
4891 opendoc path password
4892 end;
4893 end;
4895 | 0xff08 -> (* backspace *)
4896 gotoghyll (getnav ~-1)
4898 | 111 -> (* o *)
4899 enteroutlinemode ()
4901 | 117 -> (* u *)
4902 state.rects <- [];
4903 state.text <- "";
4904 G.postRedisplay "dehighlight";
4906 | 47 | 63 -> (* / ? *)
4907 let ondone isforw s =
4908 cbput state.hists.pat s;
4909 state.searchpattern <- s;
4910 search s isforw
4912 let s = String.create 1 in
4913 s.[0] <- Char.chr key;
4914 enttext (s, "", Some (onhist state.hists.pat),
4915 textentry, ondone (key = 47), true)
4917 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4918 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4919 setzoom (conf.zoom +. incr)
4921 | 43 | 0xffab -> (* + *)
4922 let ondone s =
4923 let n =
4924 try int_of_string s with exc ->
4925 state.text <- Printf.sprintf "bad integer `%s': %s"
4926 s (Printexc.to_string exc);
4927 max_int
4929 if n != max_int
4930 then (
4931 conf.pagebias <- n;
4932 state.text <- "page bias is now " ^ string_of_int n;
4935 enttext ("page bias: ", "", None, intentry, ondone, true)
4937 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4938 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4939 setzoom (max 0.01 (conf.zoom -. decr))
4941 | 45 | 0xffad -> (* - *)
4942 let ondone msg = state.text <- msg in
4943 enttext (
4944 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4945 optentry state.mode, ondone, true
4948 | 48 when ctrl -> (* ctrl-0 *)
4949 setzoom 1.0
4951 | 49 when ctrl -> (* ctrl-1 *)
4952 let cols =
4953 match conf.columns with
4954 | Csingle _ | Cmulti _ -> 1
4955 | Csplit (n, _) -> n
4957 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4958 if zoom < 1.0
4959 then setzoom zoom
4961 | 0xffc6 -> (* f9 *)
4962 togglebirdseye ()
4964 | 57 when ctrl -> (* ctrl-9 *)
4965 togglebirdseye ()
4967 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4968 when not ctrl -> (* 0..9 *)
4969 let ondone s =
4970 let n =
4971 try int_of_string s with exc ->
4972 state.text <- Printf.sprintf "bad integer `%s': %s"
4973 s (Printexc.to_string exc);
4976 if n >= 0
4977 then (
4978 addnav ();
4979 cbput state.hists.pag (string_of_int n);
4980 gotopage1 (n + conf.pagebias - 1) 0;
4983 let pageentry text key =
4984 match Char.unsafe_chr key with
4985 | 'g' -> TEdone text
4986 | _ -> intentry text key
4988 let text = "x" in text.[0] <- Char.chr key;
4989 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4991 | 98 -> (* b *)
4992 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4993 reshape conf.winw conf.winh;
4995 | 108 -> (* l *)
4996 conf.hlinks <- not conf.hlinks;
4997 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4998 G.postRedisplay "toggle highlightlinks";
5000 | 70 -> (* F *)
5001 state.glinks <- true;
5002 let mode = state.mode in
5003 state.mode <- Textentry (
5004 (":", "", None, linknentry, linkndone gotounder, false),
5005 (fun _ ->
5006 state.glinks <- false;
5007 state.mode <- mode)
5009 state.text <- "";
5010 G.postRedisplay "view:linkent(F)"
5012 | 121 -> (* y *)
5013 state.glinks <- true;
5014 let mode = state.mode in
5015 state.mode <- Textentry (
5016 (":", "", None, linknentry, linkndone (fun under ->
5017 match Ne.pipe () with
5018 | Ne.Exn exn ->
5019 showtext '!' (Printf.sprintf "pipe failed: %s"
5020 (Printexc.to_string exn));
5021 | Ne.Res (r, w) ->
5022 let popened =
5023 try popen conf.selcmd [r, 0; w, -1]; true
5024 with exn ->
5025 showtext '!'
5026 (Printf.sprintf "failed to execute %s: %s"
5027 conf.selcmd (Printexc.to_string exn));
5028 false
5030 let clo cap fd =
5031 Ne.clo fd (fun msg ->
5032 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5035 let s = undertext under in
5036 if popened
5037 then
5038 (try
5039 let l = String.length s in
5040 let n = tempfailureretry (Unix.write w s 0) l in
5041 if n != l
5042 then
5043 showtext '!'
5044 (Printf.sprintf
5045 "failed to write %d characters to sel pipe, wrote %d"
5048 with exn ->
5049 showtext '!'
5050 (Printf.sprintf "failed to write to sel pipe: %s"
5051 (Printexc.to_string exn)
5054 else dolog "%s" s;
5055 clo "pipe/r" r;
5056 clo "pipe/w" w;
5057 ), false
5059 fun _ ->
5060 state.glinks <- false;
5061 state.mode <- mode
5063 state.text <- "";
5064 G.postRedisplay "view:linkent"
5066 | 97 -> (* a *)
5067 begin match state.autoscroll with
5068 | Some step ->
5069 conf.autoscrollstep <- step;
5070 state.autoscroll <- None
5071 | None ->
5072 if conf.autoscrollstep = 0
5073 then state.autoscroll <- Some 1
5074 else state.autoscroll <- Some conf.autoscrollstep
5077 | 112 when ctrl -> (* ctrl-p *)
5078 launchpath ()
5080 | 80 -> (* P *)
5081 setpresentationmode (not conf.presentation);
5082 showtext ' ' ("presentation mode " ^
5083 if conf.presentation then "on" else "off");
5085 | 102 -> (* f *)
5086 begin match state.fullscreen with
5087 | None ->
5088 state.fullscreen <- Some (conf.winw, conf.winh);
5089 Wsi.fullscreen ()
5090 | Some (w, h) ->
5091 state.fullscreen <- None;
5092 doreshape w h
5095 | 112 | 78 -> (* p|N *)
5096 search state.searchpattern false
5098 | 110 | 0xffc0 -> (* n|F3 *)
5099 search state.searchpattern true
5101 | 116 -> (* t *)
5102 begin match state.layout with
5103 | [] -> ()
5104 | l :: _ ->
5105 gotoy_and_clear_text (getpagey l.pageno)
5108 | 32 -> (* space *)
5109 nextpage ()
5111 | 0xff9f | 0xffff -> (* delete *)
5112 prevpage ()
5114 | 61 -> (* = *)
5115 showtext ' ' (describe_location ());
5117 | 119 -> (* w *)
5118 begin match state.layout with
5119 | [] -> ()
5120 | l :: _ ->
5121 doreshape (l.pagew + state.scrollw) l.pageh;
5122 G.postRedisplay "w"
5125 | 39 -> (* ' *)
5126 enterbookmarkmode ()
5128 | 104 | 0xffbe -> (* h|F1 *)
5129 enterhelpmode ()
5131 | 105 -> (* i *)
5132 enterinfomode ()
5134 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5135 entermsgsmode ()
5137 | 109 -> (* m *)
5138 let ondone s =
5139 match state.layout with
5140 | l :: _ ->
5141 if String.length s > 0
5142 then
5143 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5144 | _ -> ()
5146 enttext ("bookmark: ", "", None, textentry, ondone, true)
5148 | 126 -> (* ~ *)
5149 quickbookmark ();
5150 showtext ' ' "Quick bookmark added";
5152 | 122 -> (* z *)
5153 begin match state.layout with
5154 | l :: _ ->
5155 let rect = getpdimrect l.pagedimno in
5156 let w, h =
5157 if conf.crophack
5158 then
5159 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5160 truncate (1.2 *. (rect.(3) -. rect.(0))))
5161 else
5162 (truncate (rect.(1) -. rect.(0)),
5163 truncate (rect.(3) -. rect.(0)))
5165 let w = truncate ((float w)*.conf.zoom)
5166 and h = truncate ((float h)*.conf.zoom) in
5167 if w != 0 && h != 0
5168 then (
5169 state.anchor <- getanchor ();
5170 doreshape (w + state.scrollw) (h + conf.interpagespace)
5172 G.postRedisplay "z";
5174 | [] -> ()
5177 | 50 when ctrl -> (* ctrl-2 *)
5178 let maxw = getmaxw () in
5179 if maxw > 0.0
5180 then setzoom (maxw /. float conf.winw)
5182 | 60 | 62 -> (* < > *)
5183 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5185 | 91 | 93 -> (* [ ] *)
5186 conf.colorscale <-
5187 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5189 G.postRedisplay "brightness";
5191 | 99 when state.mode = View -> (* c *)
5192 let (c, a, b), z =
5193 match state.prevcolumns with
5194 | None -> (1, 0, 0), 1.0
5195 | Some (columns, z) ->
5196 let cab =
5197 match columns with
5198 | Csplit (c, _) -> -c, 0, 0
5199 | Cmulti ((c, a, b), _) -> c, a, b
5200 | Csingle _ -> 1, 0, 0
5202 cab, z
5204 setcolumns View c a b;
5205 setzoom z;
5207 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5208 setzoom state.prevzoom
5210 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5211 begin match state.autoscroll with
5212 | None ->
5213 begin match state.mode with
5214 | Birdseye beye -> upbirdseye 1 beye
5215 | _ ->
5216 if ctrl
5217 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5218 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5220 | Some n ->
5221 setautoscrollspeed n false
5224 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5225 begin match state.autoscroll with
5226 | None ->
5227 begin match state.mode with
5228 | Birdseye beye -> downbirdseye 1 beye
5229 | _ ->
5230 if ctrl
5231 then gotoy_and_clear_text (clamp (conf.winh/2))
5232 else gotoy_and_clear_text (clamp conf.scrollstep)
5234 | Some n ->
5235 setautoscrollspeed n true
5238 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5239 if canpan ()
5240 then
5241 let dx =
5242 if ctrl
5243 then conf.winw / 2
5244 else conf.hscrollstep
5246 let dx = if key = 0xff51 then dx else -dx in
5247 state.x <- state.x + dx;
5248 gotoy_and_clear_text state.y
5249 else (
5250 state.text <- "";
5251 G.postRedisplay "lef/right"
5254 | 0xff55 | 0xff9a -> (* (kp) prior *)
5255 let y =
5256 if ctrl
5257 then
5258 match state.layout with
5259 | [] -> state.y
5260 | l :: _ -> state.y - l.pagey
5261 else
5262 clamp (pgscale (-conf.winh))
5264 gotoghyll y
5266 | 0xff56 | 0xff9b -> (* (kp) next *)
5267 let y =
5268 if ctrl
5269 then
5270 match List.rev state.layout with
5271 | [] -> state.y
5272 | l :: _ -> getpagey l.pageno
5273 else
5274 clamp (pgscale conf.winh)
5276 gotoghyll y
5278 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5279 gotoghyll 0
5280 | 71 | 0xff57 | 0xff9c -> (* G end *)
5281 gotoghyll (clamp state.maxy)
5283 | 0xff53 when Wsi.withalt mask -> (* alt-right *)
5284 gotoghyll (getnav 1)
5285 | 0xff51 when Wsi.withalt mask -> (* alt-left *)
5286 gotoghyll (getnav ~-1)
5288 | 114 -> (* r *)
5289 reload ()
5291 | 118 when conf.debug -> (* v *)
5292 state.rects <- [];
5293 List.iter (fun l ->
5294 match getopaque l.pageno with
5295 | None -> ()
5296 | Some opaque ->
5297 let x0, y0, x1, y1 = pagebbox opaque in
5298 let a,b = float x0, float y0 in
5299 let c,d = float x1, float y0 in
5300 let e,f = float x1, float y1 in
5301 let h,j = float x0, float y1 in
5302 let rect = (a,b,c,d,e,f,h,j) in
5303 debugrect rect;
5304 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5305 ) state.layout;
5306 G.postRedisplay "v";
5308 | _ ->
5309 vlog "huh? %s" (Wsi.keyname key)
5312 let linknavkeyboard key mask linknav =
5313 let getpage pageno =
5314 let rec loop = function
5315 | [] -> None
5316 | l :: _ when l.pageno = pageno -> Some l
5317 | _ :: rest -> loop rest
5318 in loop state.layout
5320 let doexact (pageno, n) =
5321 match getopaque pageno, getpage pageno with
5322 | Some opaque, Some l ->
5323 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5324 then
5325 let under = getlink opaque n in
5326 G.postRedisplay "link gotounder";
5327 gotounder under;
5328 state.mode <- View;
5329 else
5330 let opt, dir =
5331 match key with
5332 | 0xff50 -> (* home *)
5333 Some (findlink opaque LDfirst), -1
5335 | 0xff57 -> (* end *)
5336 Some (findlink opaque LDlast), 1
5338 | 0xff51 -> (* left *)
5339 Some (findlink opaque (LDleft n)), -1
5341 | 0xff53 -> (* right *)
5342 Some (findlink opaque (LDright n)), 1
5344 | 0xff52 -> (* up *)
5345 Some (findlink opaque (LDup n)), -1
5347 | 0xff54 -> (* down *)
5348 Some (findlink opaque (LDdown n)), 1
5350 | _ -> None, 0
5352 let pwl l dir =
5353 begin match findpwl l.pageno dir with
5354 | Pwlnotfound -> ()
5355 | Pwl pageno ->
5356 let notfound dir =
5357 state.mode <- LinkNav (Ltgendir dir);
5358 let y, h = getpageyh pageno in
5359 let y =
5360 if dir < 0
5361 then y + h - conf.winh
5362 else y
5364 gotoy y
5366 begin match getopaque pageno, getpage pageno with
5367 | Some opaque, Some _ ->
5368 let link =
5369 let ld = if dir > 0 then LDfirst else LDlast in
5370 findlink opaque ld
5372 begin match link with
5373 | Lfound m ->
5374 showlinktype (getlink opaque m);
5375 state.mode <- LinkNav (Ltexact (pageno, m));
5376 G.postRedisplay "linknav jpage";
5377 | _ -> notfound dir
5378 end;
5379 | _ -> notfound dir
5380 end;
5381 end;
5383 begin match opt with
5384 | Some Lnotfound -> pwl l dir;
5385 | Some (Lfound m) ->
5386 if m = n
5387 then pwl l dir
5388 else (
5389 let _, y0, _, y1 = getlinkrect opaque m in
5390 if y0 < l.pagey
5391 then gotopage1 l.pageno y0
5392 else (
5393 let d = fstate.fontsize + 1 in
5394 if y1 - l.pagey > l.pagevh - d
5395 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5396 else G.postRedisplay "linknav";
5398 showlinktype (getlink opaque m);
5399 state.mode <- LinkNav (Ltexact (l.pageno, m));
5402 | None -> viewkeyboard key mask
5403 end;
5404 | _ -> viewkeyboard key mask
5406 if key = 0xff63
5407 then (
5408 state.mode <- View;
5409 G.postRedisplay "leave linknav"
5411 else
5412 match linknav with
5413 | Ltgendir _ -> viewkeyboard key mask
5414 | Ltexact exact -> doexact exact
5417 let keyboard key mask =
5418 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5419 then wcmd "interrupt"
5420 else state.uioh <- state.uioh#key key mask
5423 let birdseyekeyboard key mask
5424 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5425 let incr =
5426 match conf.columns with
5427 | Csingle _ -> 1
5428 | Cmulti ((c, _, _), _) -> c
5429 | Csplit _ -> failwith "bird's eye split mode"
5431 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5432 match key with
5433 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5434 let y, h = getpageyh pageno in
5435 let top = (conf.winh - h) / 2 in
5436 gotoy (max 0 (y - top))
5437 | 0xff0d (* enter *)
5438 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5439 | 0xff1b -> leavebirdseye beye true (* escape *)
5440 | 0xff52 -> upbirdseye incr beye (* up *)
5441 | 0xff54 -> downbirdseye incr beye (* down *)
5442 | 0xff51 -> upbirdseye 1 beye (* left *)
5443 | 0xff53 -> downbirdseye 1 beye (* right *)
5445 | 0xff55 -> (* prior *)
5446 begin match state.layout with
5447 | l :: _ ->
5448 if l.pagey != 0
5449 then (
5450 state.mode <- Birdseye (
5451 oconf, leftx, l.pageno, hooverpageno, anchor
5453 gotopage1 l.pageno 0;
5455 else (
5456 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5457 match layout with
5458 | [] -> gotoy (clamp (-conf.winh))
5459 | l :: _ ->
5460 state.mode <- Birdseye (
5461 oconf, leftx, l.pageno, hooverpageno, anchor
5463 gotopage1 l.pageno 0
5466 | [] -> gotoy (clamp (-conf.winh))
5467 end;
5469 | 0xff56 -> (* next *)
5470 begin match List.rev state.layout with
5471 | l :: _ ->
5472 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5473 begin match layout with
5474 | [] ->
5475 let incr = l.pageh - l.pagevh in
5476 if incr = 0
5477 then (
5478 state.mode <-
5479 Birdseye (
5480 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5482 G.postRedisplay "birdseye pagedown";
5484 else gotoy (clamp (incr + conf.interpagespace*2));
5486 | l :: _ ->
5487 state.mode <-
5488 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5489 gotopage1 l.pageno 0;
5492 | [] -> gotoy (clamp conf.winh)
5493 end;
5495 | 0xff50 -> (* home *)
5496 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5497 gotopage1 0 0
5499 | 0xff57 -> (* end *)
5500 let pageno = state.pagecount - 1 in
5501 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5502 if not (pagevisible state.layout pageno)
5503 then
5504 let h =
5505 match List.rev state.pdims with
5506 | [] -> conf.winh
5507 | (_, _, h, _) :: _ -> h
5509 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5510 else G.postRedisplay "birdseye end";
5511 | _ -> viewkeyboard key mask
5514 let drawpage l linkindexbase =
5515 let color =
5516 match state.mode with
5517 | Textentry _ -> scalecolor 0.4
5518 | LinkNav _
5519 | View -> scalecolor 1.0
5520 | Birdseye (_, _, pageno, hooverpageno, _) ->
5521 if l.pageno = hooverpageno
5522 then scalecolor 0.9
5523 else (
5524 if l.pageno = pageno
5525 then scalecolor 1.0
5526 else scalecolor 0.8
5529 drawtiles l color;
5530 begin match getopaque l.pageno with
5531 | Some opaque ->
5532 if tileready l l.pagex l.pagey
5533 then
5534 let x = l.pagedispx - l.pagex
5535 and y = l.pagedispy - l.pagey in
5536 let hlmask =
5537 match conf.columns with
5538 | Csingle _ | Cmulti _ ->
5539 (if conf.hlinks then 1 else 0)
5540 + (if state.glinks
5541 && not (isbirdseye state.mode) then 2 else 0)
5542 | _ -> 0
5544 let s =
5545 match state.mode with
5546 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5547 | _ -> ""
5549 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5550 else 0
5552 | _ -> 0
5553 end;
5556 let scrollindicator () =
5557 let sbw, ph, sh = state.uioh#scrollph in
5558 let sbh, pw, sw = state.uioh#scrollpw in
5560 GlDraw.color (0.64, 0.64, 0.64);
5561 GlDraw.rect
5562 (float (conf.winw - sbw), 0.)
5563 (float conf.winw, float conf.winh)
5565 GlDraw.rect
5566 (0., float (conf.winh - sbh))
5567 (float (conf.winw - state.scrollw - 1), float conf.winh)
5569 GlDraw.color (0.0, 0.0, 0.0);
5571 GlDraw.rect
5572 (float (conf.winw - sbw), ph)
5573 (float conf.winw, ph +. sh)
5575 GlDraw.rect
5576 (pw, float (conf.winh - sbh))
5577 (pw +. sw, float conf.winh)
5581 let showsel () =
5582 match state.mstate with
5583 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5586 | Msel ((x0, y0), (x1, y1)) ->
5587 let rec loop = function
5588 | l :: ls ->
5589 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5590 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5591 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5592 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5593 then
5594 match getopaque l.pageno with
5595 | Some opaque ->
5596 let x0, y0 = pagetranslatepoint l x0 y0 in
5597 let x1, y1 = pagetranslatepoint l x1 y1 in
5598 seltext opaque (x0, y0, x1, y1);
5599 | _ -> ()
5600 else loop ls
5601 | [] -> ()
5603 loop state.layout
5606 let showrects rects =
5607 Gl.enable `blend;
5608 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5609 GlDraw.polygon_mode `both `fill;
5610 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5611 List.iter
5612 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5613 List.iter (fun l ->
5614 if l.pageno = pageno
5615 then (
5616 let dx = float (l.pagedispx - l.pagex) in
5617 let dy = float (l.pagedispy - l.pagey) in
5618 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5619 GlDraw.begins `quads;
5621 GlDraw.vertex2 (x0+.dx, y0+.dy);
5622 GlDraw.vertex2 (x1+.dx, y1+.dy);
5623 GlDraw.vertex2 (x2+.dx, y2+.dy);
5624 GlDraw.vertex2 (x3+.dx, y3+.dy);
5626 GlDraw.ends ();
5628 ) state.layout
5629 ) rects
5631 Gl.disable `blend;
5634 let display () =
5635 GlClear.color (scalecolor2 conf.bgcolor);
5636 GlClear.clear [`color];
5637 let rec loop linkindexbase = function
5638 | l :: rest ->
5639 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5640 loop linkindexbase rest
5641 | [] -> ()
5643 loop 0 state.layout;
5644 let rects =
5645 match state.mode with
5646 | LinkNav (Ltexact (pageno, linkno)) ->
5647 begin match getopaque pageno with
5648 | Some opaque ->
5649 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5650 (pageno, 5, (
5651 float x0, float y0,
5652 float x1, float y0,
5653 float x1, float y1,
5654 float x0, float y1)
5655 ) :: state.rects
5656 | None -> state.rects
5658 | _ -> state.rects
5660 showrects rects;
5661 showsel ();
5662 state.uioh#display;
5663 begin match state.mstate with
5664 | Mzoomrect ((x0, y0), (x1, y1)) ->
5665 Gl.enable `blend;
5666 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5667 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5668 GlDraw.rect (float x0, float y0)
5669 (float x1, float y1);
5670 Gl.disable `blend;
5671 | _ -> ()
5672 end;
5673 enttext ();
5674 scrollindicator ();
5675 if not state.wthack then Wsi.swapb ();
5678 let zoomrect x y x1 y1 =
5679 let x0 = min x x1
5680 and x1 = max x x1
5681 and y0 = min y y1 in
5682 gotoy (state.y + y0);
5683 state.anchor <- getanchor ();
5684 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5685 let margin =
5686 if state.w < conf.winw - state.scrollw
5687 then (conf.winw - state.scrollw - state.w) / 2
5688 else 0
5690 state.x <- (state.x + margin) - x0;
5691 setzoom zoom;
5692 Wsi.setcursor Wsi.CURSOR_INHERIT;
5693 state.mstate <- Mnone;
5696 let scrollx x =
5697 let winw = conf.winw - state.scrollw - 1 in
5698 let s = float x /. float winw in
5699 let destx = truncate (float (state.w + winw) *. s) in
5700 state.x <- winw - destx;
5701 gotoy_and_clear_text state.y;
5702 state.mstate <- Mscrollx;
5705 let scrolly y =
5706 let s = float y /. float conf.winh in
5707 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5708 gotoy_and_clear_text desty;
5709 state.mstate <- Mscrolly;
5712 let viewmouse button down x y mask =
5713 match button with
5714 | n when (n == 4 || n == 5) && not down ->
5715 if Wsi.withctrl mask
5716 then (
5717 match state.mstate with
5718 | Mzoom (oldn, i) ->
5719 if oldn = n
5720 then (
5721 if i = 2
5722 then
5723 let incr =
5724 match n with
5725 | 5 ->
5726 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5727 | _ ->
5728 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5730 let zoom = conf.zoom -. incr in
5731 setzoom zoom;
5732 state.mstate <- Mzoom (n, 0);
5733 else
5734 state.mstate <- Mzoom (n, i+1);
5736 else state.mstate <- Mzoom (n, 0)
5738 | _ -> state.mstate <- Mzoom (n, 0)
5740 else (
5741 match state.autoscroll with
5742 | Some step -> setautoscrollspeed step (n=4)
5743 | None ->
5744 if conf.wheelbypage
5745 then (
5746 if n = 4
5747 then nextpage ()
5748 else prevpage ()
5750 else
5751 let incr =
5752 if n = 4
5753 then -conf.scrollstep
5754 else conf.scrollstep
5756 let incr = incr * 2 in
5757 let y = clamp incr in
5758 gotoy_and_clear_text y
5761 | n when (n = 6 || n = 7) && not down && canpan () ->
5762 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5763 gotoy_and_clear_text state.y
5765 | 1 when Wsi.withctrl mask ->
5766 if down
5767 then (
5768 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5769 state.mstate <- Mpan (x, y)
5771 else
5772 state.mstate <- Mnone
5774 | 3 ->
5775 if down
5776 then (
5777 Wsi.setcursor Wsi.CURSOR_CYCLE;
5778 let p = (x, y) in
5779 state.mstate <- Mzoomrect (p, p)
5781 else (
5782 match state.mstate with
5783 | Mzoomrect ((x0, y0), _) ->
5784 if abs (x-x0) > 10 && abs (y - y0) > 10
5785 then zoomrect x0 y0 x y
5786 else (
5787 state.mstate <- Mnone;
5788 Wsi.setcursor Wsi.CURSOR_INHERIT;
5789 G.postRedisplay "kill accidental zoom rect";
5791 | _ ->
5792 Wsi.setcursor Wsi.CURSOR_INHERIT;
5793 state.mstate <- Mnone
5796 | 1 when x > conf.winw - state.scrollw ->
5797 if down
5798 then
5799 let _, position, sh = state.uioh#scrollph in
5800 if y > truncate position && y < truncate (position +. sh)
5801 then state.mstate <- Mscrolly
5802 else scrolly y
5803 else
5804 state.mstate <- Mnone
5806 | 1 when y > conf.winh - state.hscrollh ->
5807 if down
5808 then
5809 let _, position, sw = state.uioh#scrollpw in
5810 if x > truncate position && x < truncate (position +. sw)
5811 then state.mstate <- Mscrollx
5812 else scrollx x
5813 else
5814 state.mstate <- Mnone
5816 | 1 ->
5817 let dest = if down then getunder x y else Unone in
5818 begin match dest with
5819 | Ulinkgoto _
5820 | Ulinkuri _
5821 | Uremote _
5822 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5823 gotounder dest
5825 | Unone when down ->
5826 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5827 state.mstate <- Mpan (x, y);
5829 | Unone | Utext _ ->
5830 if down
5831 then (
5832 if conf.angle mod 360 = 0
5833 then (
5834 state.mstate <- Msel ((x, y), (x, y));
5835 G.postRedisplay "mouse select";
5838 else (
5839 match state.mstate with
5840 | Mnone -> ()
5842 | Mzoom _ | Mscrollx | Mscrolly ->
5843 state.mstate <- Mnone
5845 | Mzoomrect ((x0, y0), _) ->
5846 zoomrect x0 y0 x y
5848 | Mpan _ ->
5849 Wsi.setcursor Wsi.CURSOR_INHERIT;
5850 state.mstate <- Mnone
5852 | Msel ((x0, y0), (x1, y1)) ->
5853 let rec loop = function
5854 | [] -> ()
5855 | l :: rest ->
5856 let inside =
5857 let a0 = l.pagedispy in
5858 let a1 = a0 + l.pagevh in
5859 let b0 = l.pagedispx in
5860 let b1 = b0 + l.pagevw in
5861 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5862 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5864 if inside
5865 then
5866 match getopaque l.pageno with
5867 | Some opaque ->
5868 begin
5869 match Ne.pipe () with
5870 | Ne.Exn exn ->
5871 showtext '!'
5872 (Printf.sprintf
5873 "can not create sel pipe: %s"
5874 (Printexc.to_string exn));
5875 | Ne.Res (r, w) ->
5876 let doclose what fd =
5877 Ne.clo fd (fun msg ->
5878 dolog "%s close failed: %s" what msg)
5881 popen conf.selcmd [r, 0; w, -1];
5882 copysel w opaque;
5883 doclose "pipe/r" r;
5884 G.postRedisplay "copysel";
5885 with exn ->
5886 dolog "can not execute %S: %s"
5887 conf.selcmd (Printexc.to_string exn);
5888 doclose "pipe/r" r;
5889 doclose "pipe/w" w;
5891 | None -> ()
5892 else loop rest
5894 loop state.layout;
5895 Wsi.setcursor Wsi.CURSOR_INHERIT;
5896 state.mstate <- Mnone;
5900 | _ -> ()
5903 let birdseyemouse button down x y mask
5904 (conf, leftx, _, hooverpageno, anchor) =
5905 match button with
5906 | 1 when down ->
5907 let rec loop = function
5908 | [] -> ()
5909 | l :: rest ->
5910 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5911 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5912 then (
5913 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5915 else loop rest
5917 loop state.layout
5918 | 3 -> ()
5919 | _ -> viewmouse button down x y mask
5922 let mouse button down x y mask =
5923 state.uioh <- state.uioh#button button down x y mask;
5926 let motion ~x ~y =
5927 state.uioh <- state.uioh#motion x y
5930 let pmotion ~x ~y =
5931 state.uioh <- state.uioh#pmotion x y;
5934 let uioh = object
5935 method display = ()
5937 method key key mask =
5938 begin match state.mode with
5939 | Textentry textentry -> textentrykeyboard key mask textentry
5940 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5941 | View -> viewkeyboard key mask
5942 | LinkNav linknav -> linknavkeyboard key mask linknav
5943 end;
5944 state.uioh
5946 method button button bstate x y mask =
5947 begin match state.mode with
5948 | LinkNav _
5949 | View -> viewmouse button bstate x y mask
5950 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5951 | Textentry _ -> ()
5952 end;
5953 state.uioh
5955 method motion x y =
5956 begin match state.mode with
5957 | Textentry _ -> ()
5958 | View | Birdseye _ | LinkNav _ ->
5959 match state.mstate with
5960 | Mzoom _ | Mnone -> ()
5962 | Mpan (x0, y0) ->
5963 let dx = x - x0
5964 and dy = y0 - y in
5965 state.mstate <- Mpan (x, y);
5966 if canpan ()
5967 then state.x <- state.x + dx;
5968 let y = clamp dy in
5969 gotoy_and_clear_text y
5971 | Msel (a, _) ->
5972 state.mstate <- Msel (a, (x, y));
5973 G.postRedisplay "motion select";
5975 | Mscrolly ->
5976 let y = min conf.winh (max 0 y) in
5977 scrolly y
5979 | Mscrollx ->
5980 let x = min conf.winw (max 0 x) in
5981 scrollx x
5983 | Mzoomrect (p0, _) ->
5984 state.mstate <- Mzoomrect (p0, (x, y));
5985 G.postRedisplay "motion zoomrect";
5986 end;
5987 state.uioh
5989 method pmotion x y =
5990 begin match state.mode with
5991 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5992 let rec loop = function
5993 | [] ->
5994 if hooverpageno != -1
5995 then (
5996 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5997 G.postRedisplay "pmotion birdseye no hoover";
5999 | l :: rest ->
6000 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6001 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6002 then (
6003 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6004 G.postRedisplay "pmotion birdseye hoover";
6006 else loop rest
6008 loop state.layout
6010 | Textentry _ -> ()
6012 | LinkNav _
6013 | View ->
6014 match state.mstate with
6015 | Mnone -> updateunder x y
6016 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6018 end;
6019 state.uioh
6021 method infochanged _ = ()
6023 method scrollph =
6024 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
6025 let p, h = scrollph state.y maxy in
6026 state.scrollw, p, h
6028 method scrollpw =
6029 let winw = conf.winw - state.scrollw - 1 in
6030 let fwinw = float winw in
6031 let sw =
6032 let sw = fwinw /. float state.w in
6033 let sw = fwinw *. sw in
6034 max sw (float conf.scrollh)
6036 let position, sw =
6037 let f = state.w+winw in
6038 let r = float (winw-state.x) /. float f in
6039 let p = fwinw *. r in
6040 p-.sw/.2., sw
6042 let sw =
6043 if position +. sw > fwinw
6044 then fwinw -. position
6045 else sw
6047 state.hscrollh, position, sw
6049 method modehash =
6050 let modename =
6051 match state.mode with
6052 | LinkNav _ -> "links"
6053 | Textentry _ -> "textentry"
6054 | Birdseye _ -> "birdseye"
6055 | View -> "view"
6057 findkeyhash conf modename
6058 end;;
6060 module Config =
6061 struct
6062 open Parser
6064 let fontpath = ref "";;
6066 module KeyMap =
6067 Map.Make (struct type t = (int * int) let compare = compare end);;
6069 let unent s =
6070 let l = String.length s in
6071 let b = Buffer.create l in
6072 unent b s 0 l;
6073 Buffer.contents b;
6076 let home =
6077 try Sys.getenv "HOME"
6078 with exn ->
6079 prerr_endline
6080 ("Can not determine home directory location: " ^
6081 Printexc.to_string exn);
6085 let modifier_of_string = function
6086 | "alt" -> Wsi.altmask
6087 | "shift" -> Wsi.shiftmask
6088 | "ctrl" | "control" -> Wsi.ctrlmask
6089 | "meta" -> Wsi.metamask
6090 | _ -> 0
6093 let key_of_string =
6094 let r = Str.regexp "-" in
6095 fun s ->
6096 let elems = Str.full_split r s in
6097 let f n k m =
6098 let g s =
6099 let m1 = modifier_of_string s in
6100 if m1 = 0
6101 then (Wsi.namekey s, m)
6102 else (k, m lor m1)
6103 in function
6104 | Str.Delim s when n land 1 = 0 -> g s
6105 | Str.Text s -> g s
6106 | Str.Delim _ -> (k, m)
6108 let rec loop n k m = function
6109 | [] -> (k, m)
6110 | x :: xs ->
6111 let k, m = f n k m x in
6112 loop (n+1) k m xs
6114 loop 0 0 0 elems
6117 let keys_of_string =
6118 let r = Str.regexp "[ \t]" in
6119 fun s ->
6120 let elems = Str.split r s in
6121 List.map key_of_string elems
6124 let copykeyhashes c =
6125 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6128 let config_of c attrs =
6129 let apply c k v =
6131 match k with
6132 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6133 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6134 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6135 | "preload" -> { c with preload = bool_of_string v }
6136 | "page-bias" -> { c with pagebias = int_of_string v }
6137 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6138 | "horizontal-scroll-step" ->
6139 { c with hscrollstep = max (int_of_string v) 1 }
6140 | "auto-scroll-step" ->
6141 { c with autoscrollstep = max 0 (int_of_string v) }
6142 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6143 | "crop-hack" -> { c with crophack = bool_of_string v }
6144 | "throttle" ->
6145 let mw =
6146 match String.lowercase v with
6147 | "true" -> Some infinity
6148 | "false" -> None
6149 | f -> Some (float_of_string f)
6151 { c with maxwait = mw}
6152 | "highlight-links" -> { c with hlinks = bool_of_string v }
6153 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6154 | "vertical-margin" ->
6155 { c with interpagespace = max 0 (int_of_string v) }
6156 | "zoom" ->
6157 let zoom = float_of_string v /. 100. in
6158 let zoom = max zoom 0.0 in
6159 { c with zoom = zoom }
6160 | "presentation" -> { c with presentation = bool_of_string v }
6161 | "rotation-angle" -> { c with angle = int_of_string v }
6162 | "width" -> { c with winw = max 20 (int_of_string v) }
6163 | "height" -> { c with winh = max 20 (int_of_string v) }
6164 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6165 | "proportional-display" -> { c with proportional = bool_of_string v }
6166 | "pixmap-cache-size" ->
6167 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6168 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6169 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6170 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6171 | "persistent-location" -> { c with jumpback = bool_of_string v }
6172 | "background-color" -> { c with bgcolor = color_of_string v }
6173 | "scrollbar-in-presentation" ->
6174 { c with scrollbarinpm = bool_of_string v }
6175 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6176 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6177 | "mupdf-store-size" ->
6178 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6179 | "checkers" -> { c with checkers = bool_of_string v }
6180 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6181 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6182 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6183 | "uri-launcher" -> { c with urilauncher = unent v }
6184 | "path-launcher" -> { c with pathlauncher = unent v }
6185 | "color-space" -> { c with colorspace = colorspace_of_string v }
6186 | "invert-colors" -> { c with invert = bool_of_string v }
6187 | "brightness" -> { c with colorscale = float_of_string v }
6188 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6189 | "ghyllscroll" ->
6190 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6191 | "columns" ->
6192 let (n, _, _) as nab = multicolumns_of_string v in
6193 if n < 0
6194 then { c with columns = Csplit (-n, [||]) }
6195 else { c with columns = Cmulti (nab, [||]) }
6196 | "birds-eye-columns" ->
6197 { c with beyecolumns = Some (max (int_of_string v) 2) }
6198 | "selection-command" -> { c with selcmd = unent v }
6199 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6200 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6201 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6202 | "use-pbo" -> { c with usepbo = bool_of_string v }
6203 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6204 | _ -> c
6205 with exn ->
6206 prerr_endline ("Error processing attribute (`" ^
6207 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6210 let rec fold c = function
6211 | [] -> c
6212 | (k, v) :: rest ->
6213 let c = apply c k v in
6214 fold c rest
6216 fold { c with keyhashes = copykeyhashes c } attrs;
6219 let fromstring f pos n v d =
6220 try f v
6221 with exn ->
6222 dolog "Error processing attribute (%S=%S) at %d\n%s"
6223 n v pos (Printexc.to_string exn)
6228 let bookmark_of attrs =
6229 let rec fold title page rely visy = function
6230 | ("title", v) :: rest -> fold v page rely visy rest
6231 | ("page", v) :: rest -> fold title v rely visy rest
6232 | ("rely", v) :: rest -> fold title page v visy rest
6233 | ("visy", v) :: rest -> fold title page rely v rest
6234 | _ :: rest -> fold title page rely visy rest
6235 | [] -> title, page, rely, visy
6237 fold "invalid" "0" "0" "0" attrs
6240 let doc_of attrs =
6241 let rec fold path page rely pan visy = function
6242 | ("path", v) :: rest -> fold v page rely pan visy rest
6243 | ("page", v) :: rest -> fold path v rely pan visy rest
6244 | ("rely", v) :: rest -> fold path page v pan visy rest
6245 | ("pan", v) :: rest -> fold path page rely v visy rest
6246 | ("visy", v) :: rest -> fold path page rely pan v rest
6247 | _ :: rest -> fold path page rely pan visy rest
6248 | [] -> path, page, rely, pan, visy
6250 fold "" "0" "0" "0" "0" attrs
6253 let map_of attrs =
6254 let rec fold rs ls = function
6255 | ("out", v) :: rest -> fold v ls rest
6256 | ("in", v) :: rest -> fold rs v rest
6257 | _ :: rest -> fold ls rs rest
6258 | [] -> ls, rs
6260 fold "" "" attrs
6263 let setconf dst src =
6264 dst.scrollbw <- src.scrollbw;
6265 dst.scrollh <- src.scrollh;
6266 dst.icase <- src.icase;
6267 dst.preload <- src.preload;
6268 dst.pagebias <- src.pagebias;
6269 dst.verbose <- src.verbose;
6270 dst.scrollstep <- src.scrollstep;
6271 dst.maxhfit <- src.maxhfit;
6272 dst.crophack <- src.crophack;
6273 dst.autoscrollstep <- src.autoscrollstep;
6274 dst.maxwait <- src.maxwait;
6275 dst.hlinks <- src.hlinks;
6276 dst.underinfo <- src.underinfo;
6277 dst.interpagespace <- src.interpagespace;
6278 dst.zoom <- src.zoom;
6279 dst.presentation <- src.presentation;
6280 dst.angle <- src.angle;
6281 dst.winw <- src.winw;
6282 dst.winh <- src.winh;
6283 dst.savebmarks <- src.savebmarks;
6284 dst.memlimit <- src.memlimit;
6285 dst.proportional <- src.proportional;
6286 dst.texcount <- src.texcount;
6287 dst.sliceheight <- src.sliceheight;
6288 dst.thumbw <- src.thumbw;
6289 dst.jumpback <- src.jumpback;
6290 dst.bgcolor <- src.bgcolor;
6291 dst.scrollbarinpm <- src.scrollbarinpm;
6292 dst.tilew <- src.tilew;
6293 dst.tileh <- src.tileh;
6294 dst.mustoresize <- src.mustoresize;
6295 dst.checkers <- src.checkers;
6296 dst.aalevel <- src.aalevel;
6297 dst.trimmargins <- src.trimmargins;
6298 dst.trimfuzz <- src.trimfuzz;
6299 dst.urilauncher <- src.urilauncher;
6300 dst.colorspace <- src.colorspace;
6301 dst.invert <- src.invert;
6302 dst.colorscale <- src.colorscale;
6303 dst.redirectstderr <- src.redirectstderr;
6304 dst.ghyllscroll <- src.ghyllscroll;
6305 dst.columns <- src.columns;
6306 dst.beyecolumns <- src.beyecolumns;
6307 dst.selcmd <- src.selcmd;
6308 dst.updatecurs <- src.updatecurs;
6309 dst.pathlauncher <- src.pathlauncher;
6310 dst.keyhashes <- copykeyhashes src;
6311 dst.hfsize <- src.hfsize;
6312 dst.hscrollstep <- src.hscrollstep;
6313 dst.pgscale <- src.pgscale;
6314 dst.usepbo <- src.usepbo;
6315 dst.wheelbypage <- src.wheelbypage;
6318 let get s =
6319 let h = Hashtbl.create 10 in
6320 let dc = { defconf with angle = defconf.angle } in
6321 let rec toplevel v t spos _ =
6322 match t with
6323 | Vdata | Vcdata | Vend -> v
6324 | Vopen ("llppconfig", _, closed) ->
6325 if closed
6326 then v
6327 else { v with f = llppconfig }
6328 | Vopen _ ->
6329 error "unexpected subelement at top level" s spos
6330 | Vclose _ -> error "unexpected close at top level" s spos
6332 and llppconfig v t spos _ =
6333 match t with
6334 | Vdata | Vcdata -> v
6335 | Vend -> error "unexpected end of input in llppconfig" s spos
6336 | Vopen ("defaults", attrs, closed) ->
6337 let c = config_of dc attrs in
6338 setconf dc c;
6339 if closed
6340 then v
6341 else { v with f = defaults }
6343 | Vopen ("ui-font", attrs, closed) ->
6344 let rec getsize size = function
6345 | [] -> size
6346 | ("size", v) :: rest ->
6347 let size =
6348 fromstring int_of_string spos "size" v fstate.fontsize in
6349 getsize size rest
6350 | l -> getsize size l
6352 fstate.fontsize <- getsize fstate.fontsize attrs;
6353 if closed
6354 then v
6355 else { v with f = uifont (Buffer.create 10) }
6357 | Vopen ("doc", attrs, closed) ->
6358 let pathent, spage, srely, span, svisy = doc_of attrs in
6359 let path = unent pathent
6360 and pageno = fromstring int_of_string spos "page" spage 0
6361 and rely = fromstring float_of_string spos "rely" srely 0.0
6362 and pan = fromstring int_of_string spos "pan" span 0
6363 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6364 let c = config_of dc attrs in
6365 let anchor = (pageno, rely, visy) in
6366 if closed
6367 then (Hashtbl.add h path (c, [], pan, anchor); v)
6368 else { v with f = doc path pan anchor c [] }
6370 | Vopen _ ->
6371 error "unexpected subelement in llppconfig" s spos
6373 | Vclose "llppconfig" -> { v with f = toplevel }
6374 | Vclose _ -> error "unexpected close in llppconfig" s spos
6376 and defaults v t spos _ =
6377 match t with
6378 | Vdata | Vcdata -> v
6379 | Vend -> error "unexpected end of input in defaults" s spos
6380 | Vopen ("keymap", attrs, closed) ->
6381 let modename =
6382 try List.assoc "mode" attrs
6383 with Not_found -> "global" in
6384 if closed
6385 then v
6386 else
6387 let ret keymap =
6388 let h = findkeyhash dc modename in
6389 KeyMap.iter (Hashtbl.replace h) keymap;
6390 defaults
6392 { v with f = pkeymap ret KeyMap.empty }
6394 | Vopen (_, _, _) ->
6395 error "unexpected subelement in defaults" s spos
6397 | Vclose "defaults" ->
6398 { v with f = llppconfig }
6400 | Vclose _ -> error "unexpected close in defaults" s spos
6402 and uifont b v t spos epos =
6403 match t with
6404 | Vdata | Vcdata ->
6405 Buffer.add_substring b s spos (epos - spos);
6407 | Vopen (_, _, _) ->
6408 error "unexpected subelement in ui-font" s spos
6409 | Vclose "ui-font" ->
6410 if String.length !fontpath = 0
6411 then fontpath := Buffer.contents b;
6412 { v with f = llppconfig }
6413 | Vclose _ -> error "unexpected close in ui-font" s spos
6414 | Vend -> error "unexpected end of input in ui-font" s spos
6416 and doc path pan anchor c bookmarks v t spos _ =
6417 match t with
6418 | Vdata | Vcdata -> v
6419 | Vend -> error "unexpected end of input in doc" s spos
6420 | Vopen ("bookmarks", _, closed) ->
6421 if closed
6422 then v
6423 else { v with f = pbookmarks path pan anchor c bookmarks }
6425 | Vopen ("keymap", attrs, closed) ->
6426 let modename =
6427 try List.assoc "mode" attrs
6428 with Not_found -> "global"
6430 if closed
6431 then v
6432 else
6433 let ret keymap =
6434 let h = findkeyhash c modename in
6435 KeyMap.iter (Hashtbl.replace h) keymap;
6436 doc path pan anchor c bookmarks
6438 { v with f = pkeymap ret KeyMap.empty }
6440 | Vopen (_, _, _) ->
6441 error "unexpected subelement in doc" s spos
6443 | Vclose "doc" ->
6444 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6445 { v with f = llppconfig }
6447 | Vclose _ -> error "unexpected close in doc" s spos
6449 and pkeymap ret keymap v t spos _ =
6450 match t with
6451 | Vdata | Vcdata -> v
6452 | Vend -> error "unexpected end of input in keymap" s spos
6453 | Vopen ("map", attrs, closed) ->
6454 let r, l = map_of attrs in
6455 let kss = fromstring keys_of_string spos "in" r [] in
6456 let lss = fromstring keys_of_string spos "out" l [] in
6457 let keymap =
6458 match kss with
6459 | [] -> keymap
6460 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6461 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6463 if closed
6464 then { v with f = pkeymap ret keymap }
6465 else
6466 let f () = v in
6467 { v with f = skip "map" f }
6469 | Vopen _ ->
6470 error "unexpected subelement in keymap" s spos
6472 | Vclose "keymap" ->
6473 { v with f = ret keymap }
6475 | Vclose _ -> error "unexpected close in keymap" s spos
6477 and pbookmarks path pan anchor c bookmarks v t spos _ =
6478 match t with
6479 | Vdata | Vcdata -> v
6480 | Vend -> error "unexpected end of input in bookmarks" s spos
6481 | Vopen ("item", attrs, closed) ->
6482 let titleent, spage, srely, svisy = bookmark_of attrs in
6483 let page = fromstring int_of_string spos "page" spage 0
6484 and rely = fromstring float_of_string spos "rely" srely 0.0
6485 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6486 let bookmarks =
6487 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6489 if closed
6490 then { v with f = pbookmarks path pan anchor c bookmarks }
6491 else
6492 let f () = v in
6493 { v with f = skip "item" f }
6495 | Vopen _ ->
6496 error "unexpected subelement in bookmarks" s spos
6498 | Vclose "bookmarks" ->
6499 { v with f = doc path pan anchor c bookmarks }
6501 | Vclose _ -> error "unexpected close in bookmarks" s spos
6503 and skip tag f v t spos _ =
6504 match t with
6505 | Vdata | Vcdata -> v
6506 | Vend ->
6507 error ("unexpected end of input in skipped " ^ tag) s spos
6508 | Vopen (tag', _, closed) ->
6509 if closed
6510 then v
6511 else
6512 let f' () = { v with f = skip tag f } in
6513 { v with f = skip tag' f' }
6514 | Vclose ctag ->
6515 if tag = ctag
6516 then f ()
6517 else error ("unexpected close in skipped " ^ tag) s spos
6520 parse { f = toplevel; accu = () } s;
6521 h, dc;
6524 let do_load f ic =
6526 let len = in_channel_length ic in
6527 let s = String.create len in
6528 really_input ic s 0 len;
6529 f s;
6530 with
6531 | Parse_error (msg, s, pos) ->
6532 let subs = subs s pos in
6533 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6534 failwith ("parse error: " ^ s)
6536 | exn ->
6537 failwith ("config load error: " ^ Printexc.to_string exn)
6540 let defconfpath =
6541 let dir =
6543 let dir = Filename.concat home ".config" in
6544 if Sys.is_directory dir then dir else home
6545 with _ -> home
6547 Filename.concat dir "llpp.conf"
6550 let confpath = ref defconfpath;;
6552 let load1 f =
6553 if Sys.file_exists !confpath
6554 then
6555 match
6556 (try Some (open_in_bin !confpath)
6557 with exn ->
6558 prerr_endline
6559 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6560 Printexc.to_string exn);
6561 None
6563 with
6564 | Some ic ->
6565 let success =
6567 f (do_load get ic)
6568 with exn ->
6569 prerr_endline
6570 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6571 Printexc.to_string exn);
6572 false
6574 close_in ic;
6575 success
6577 | None -> false
6578 else
6579 f (Hashtbl.create 0, defconf)
6582 let load () =
6583 let f (h, dc) =
6584 let pc, pb, px, pa =
6586 Hashtbl.find h (Filename.basename state.path)
6587 with Not_found -> dc, [], 0, emptyanchor
6589 setconf defconf dc;
6590 setconf conf pc;
6591 state.bookmarks <- pb;
6592 state.x <- px;
6593 state.scrollw <- conf.scrollbw;
6594 if conf.jumpback
6595 then state.anchor <- pa;
6596 cbput state.hists.nav pa;
6597 true
6599 load1 f
6602 let add_attrs bb always dc c =
6603 let ob s a b =
6604 if always || a != b
6605 then Printf.bprintf bb "\n %s='%b'" s a
6606 and oi s a b =
6607 if always || a != b
6608 then Printf.bprintf bb "\n %s='%d'" s a
6609 and oI s a b =
6610 if always || a != b
6611 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6612 and oz s a b =
6613 if always || a <> b
6614 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6615 and oF s a b =
6616 if always || a <> b
6617 then Printf.bprintf bb "\n %s='%f'" s a
6618 and oc s a b =
6619 if always || a <> b
6620 then
6621 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6622 and oC s a b =
6623 if always || a <> b
6624 then
6625 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6626 and oR s a b =
6627 if always || a <> b
6628 then
6629 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6630 and os s a b =
6631 if always || a <> b
6632 then
6633 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6634 and og s a b =
6635 if always || a <> b
6636 then
6637 match a with
6638 | None -> ()
6639 | Some (_N, _A, _B) ->
6640 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6641 and oW s a b =
6642 if always || a <> b
6643 then
6644 let v =
6645 match a with
6646 | None -> "false"
6647 | Some f ->
6648 if f = infinity
6649 then "true"
6650 else string_of_float f
6652 Printf.bprintf bb "\n %s='%s'" s v
6653 and oco s a b =
6654 if always || a <> b
6655 then
6656 match a with
6657 | Cmulti ((n, a, b), _) when n > 1 ->
6658 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6659 | Csplit (n, _) when n > 1 ->
6660 Printf.bprintf bb "\n %s='%d'" s ~-n
6661 | _ -> ()
6662 and obeco s a b =
6663 if always || a <> b
6664 then
6665 match a with
6666 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6667 | _ -> ()
6669 let w, h =
6670 if always
6671 then dc.winw, dc.winh
6672 else
6673 match state.fullscreen with
6674 | Some wh -> wh
6675 | None -> c.winw, c.winh
6677 oi "width" w dc.winw;
6678 oi "height" h dc.winh;
6679 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6680 oi "scroll-handle-height" c.scrollh dc.scrollh;
6681 ob "case-insensitive-search" c.icase dc.icase;
6682 ob "preload" c.preload dc.preload;
6683 oi "page-bias" c.pagebias dc.pagebias;
6684 oi "scroll-step" c.scrollstep dc.scrollstep;
6685 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6686 ob "max-height-fit" c.maxhfit dc.maxhfit;
6687 ob "crop-hack" c.crophack dc.crophack;
6688 oW "throttle" c.maxwait dc.maxwait;
6689 ob "highlight-links" c.hlinks dc.hlinks;
6690 ob "under-cursor-info" c.underinfo dc.underinfo;
6691 oi "vertical-margin" c.interpagespace dc.interpagespace;
6692 oz "zoom" c.zoom dc.zoom;
6693 ob "presentation" c.presentation dc.presentation;
6694 oi "rotation-angle" c.angle dc.angle;
6695 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6696 ob "proportional-display" c.proportional dc.proportional;
6697 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6698 oi "tex-count" c.texcount dc.texcount;
6699 oi "slice-height" c.sliceheight dc.sliceheight;
6700 oi "thumbnail-width" c.thumbw dc.thumbw;
6701 ob "persistent-location" c.jumpback dc.jumpback;
6702 oc "background-color" c.bgcolor dc.bgcolor;
6703 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6704 oi "tile-width" c.tilew dc.tilew;
6705 oi "tile-height" c.tileh dc.tileh;
6706 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6707 ob "checkers" c.checkers dc.checkers;
6708 oi "aalevel" c.aalevel dc.aalevel;
6709 ob "trim-margins" c.trimmargins dc.trimmargins;
6710 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6711 os "uri-launcher" c.urilauncher dc.urilauncher;
6712 os "path-launcher" c.pathlauncher dc.pathlauncher;
6713 oC "color-space" c.colorspace dc.colorspace;
6714 ob "invert-colors" c.invert dc.invert;
6715 oF "brightness" c.colorscale dc.colorscale;
6716 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6717 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6718 oco "columns" c.columns dc.columns;
6719 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6720 os "selection-command" c.selcmd dc.selcmd;
6721 ob "update-cursor" c.updatecurs dc.updatecurs;
6722 oi "hint-font-size" c.hfsize dc.hfsize;
6723 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6724 oF "page-scroll-scale" c.pgscale dc.pgscale;
6725 ob "use-pbo" c.usepbo dc.usepbo;
6726 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6729 let keymapsbuf always dc c =
6730 let bb = Buffer.create 16 in
6731 let rec loop = function
6732 | [] -> ()
6733 | (modename, h) :: rest ->
6734 let dh = findkeyhash dc modename in
6735 if always || h <> dh
6736 then (
6737 if Hashtbl.length h > 0
6738 then (
6739 if Buffer.length bb > 0
6740 then Buffer.add_char bb '\n';
6741 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6742 Hashtbl.iter (fun i o ->
6743 let isdifferent = always ||
6745 let dO = Hashtbl.find dh i in
6746 dO <> o
6747 with Not_found -> true
6749 if isdifferent
6750 then
6751 let addkm (k, m) =
6752 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6753 if Wsi.withalt m then Buffer.add_string bb "alt-";
6754 if Wsi.withshift m then Buffer.add_string bb "shift-";
6755 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6756 Buffer.add_string bb (Wsi.keyname k);
6758 let addkms l =
6759 let rec loop = function
6760 | [] -> ()
6761 | km :: [] -> addkm km
6762 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6764 loop l
6766 Buffer.add_string bb "<map in='";
6767 addkm i;
6768 match o with
6769 | KMinsrt km ->
6770 Buffer.add_string bb "' out='";
6771 addkm km;
6772 Buffer.add_string bb "'/>\n"
6774 | KMinsrl kms ->
6775 Buffer.add_string bb "' out='";
6776 addkms kms;
6777 Buffer.add_string bb "'/>\n"
6779 | KMmulti (ins, kms) ->
6780 Buffer.add_char bb ' ';
6781 addkms ins;
6782 Buffer.add_string bb "' out='";
6783 addkms kms;
6784 Buffer.add_string bb "'/>\n"
6785 ) h;
6786 Buffer.add_string bb "</keymap>";
6789 loop rest
6791 loop c.keyhashes;
6795 let save () =
6796 let uifontsize = fstate.fontsize in
6797 let bb = Buffer.create 32768 in
6798 let f (h, dc) =
6799 let dc = if conf.bedefault then conf else dc in
6800 Buffer.add_string bb "<llppconfig>\n";
6802 if String.length !fontpath > 0
6803 then
6804 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6805 uifontsize
6806 !fontpath
6807 else (
6808 if uifontsize <> 14
6809 then
6810 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6813 Buffer.add_string bb "<defaults ";
6814 add_attrs bb true dc dc;
6815 let kb = keymapsbuf true dc dc in
6816 if Buffer.length kb > 0
6817 then (
6818 Buffer.add_string bb ">\n";
6819 Buffer.add_buffer bb kb;
6820 Buffer.add_string bb "\n</defaults>\n";
6822 else Buffer.add_string bb "/>\n";
6824 let adddoc path pan anchor c bookmarks =
6825 if bookmarks == [] && c = dc && anchor = emptyanchor
6826 then ()
6827 else (
6828 Printf.bprintf bb "<doc path='%s'"
6829 (enent path 0 (String.length path));
6831 if anchor <> emptyanchor
6832 then (
6833 let n, rely, visy = anchor in
6834 Printf.bprintf bb " page='%d'" n;
6835 if rely > 1e-6
6836 then
6837 Printf.bprintf bb " rely='%f'" rely
6839 if abs_float visy > 1e-6
6840 then
6841 Printf.bprintf bb " visy='%f'" visy
6845 if pan != 0
6846 then Printf.bprintf bb " pan='%d'" pan;
6848 add_attrs bb false dc c;
6849 let kb = keymapsbuf false dc c in
6851 begin match bookmarks with
6852 | [] ->
6853 if Buffer.length kb > 0
6854 then (
6855 Buffer.add_string bb ">\n";
6856 Buffer.add_buffer bb kb;
6857 Buffer.add_string bb "\n</doc>\n";
6859 else Buffer.add_string bb "/>\n"
6860 | _ ->
6861 Buffer.add_string bb ">\n<bookmarks>\n";
6862 List.iter (fun (title, _level, (page, rely, visy)) ->
6863 Printf.bprintf bb
6864 "<item title='%s' page='%d'"
6865 (enent title 0 (String.length title))
6866 page
6868 if rely > 1e-6
6869 then
6870 Printf.bprintf bb " rely='%f'" rely
6872 if abs_float visy > 1e-6
6873 then
6874 Printf.bprintf bb " visy='%f'" visy
6876 Buffer.add_string bb "/>\n";
6877 ) bookmarks;
6878 Buffer.add_string bb "</bookmarks>";
6879 if Buffer.length kb > 0
6880 then (
6881 Buffer.add_string bb "\n";
6882 Buffer.add_buffer bb kb;
6884 Buffer.add_string bb "\n</doc>\n";
6885 end;
6889 let pan, conf =
6890 match state.mode with
6891 | Birdseye (c, pan, _, _, _) ->
6892 let beyecolumns =
6893 match conf.columns with
6894 | Cmulti ((c, _, _), _) -> Some c
6895 | Csingle _ -> None
6896 | Csplit _ -> None
6897 and columns =
6898 match c.columns with
6899 | Cmulti (c, _) -> Cmulti (c, [||])
6900 | Csingle _ -> Csingle [||]
6901 | Csplit _ -> failwith "quit from bird's eye while split"
6903 pan, { c with beyecolumns = beyecolumns; columns = columns }
6904 | _ -> state.x, conf
6906 let basename = Filename.basename state.path in
6907 adddoc basename pan (getanchor ())
6908 (let conf =
6909 let autoscrollstep =
6910 match state.autoscroll with
6911 | Some step -> step
6912 | None -> conf.autoscrollstep
6914 match state.mode with
6915 | Birdseye (bc, _, _, _, _) ->
6916 { conf with
6917 zoom = bc.zoom;
6918 presentation = bc.presentation;
6919 interpagespace = bc.interpagespace;
6920 maxwait = bc.maxwait;
6921 autoscrollstep = autoscrollstep }
6922 | _ -> { conf with autoscrollstep = autoscrollstep }
6923 in conf)
6924 (if conf.savebmarks then state.bookmarks else []);
6926 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6927 if basename <> path
6928 then adddoc path x anchor c bookmarks
6929 ) h;
6930 Buffer.add_string bb "</llppconfig>\n";
6931 true;
6933 if load1 f && Buffer.length bb > 0
6934 then
6936 let tmp = !confpath ^ ".tmp" in
6937 let oc = open_out_bin tmp in
6938 Buffer.output_buffer oc bb;
6939 close_out oc;
6940 Unix.rename tmp !confpath;
6941 with exn ->
6942 prerr_endline
6943 ("error while saving configuration: " ^ Printexc.to_string exn)
6945 end;;
6947 let () =
6948 let trimcachepath = ref "" in
6949 Arg.parse
6950 (Arg.align
6951 [("-p", Arg.String (fun s -> state.password <- s) ,
6952 "<password> Set password");
6954 ("-f", Arg.String (fun s -> Config.fontpath := s),
6955 "<path> Set path to the user interface font");
6957 ("-c", Arg.String (fun s -> Config.confpath := s),
6958 "<path> Set path to the configuration file");
6960 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6961 "<path> Set path to the trim cache file");
6963 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6964 "<named destination> Set named destination");
6966 ("-v", Arg.Unit (fun () ->
6967 Printf.printf
6968 "%s\nconfiguration path: %s\n"
6969 (version ())
6970 Config.defconfpath
6972 exit 0), " Print version and exit");
6975 (fun s -> state.path <- s)
6976 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6978 if String.length state.path = 0
6979 then (prerr_endline "file name missing"; exit 1);
6981 if not (Config.load ())
6982 then prerr_endline "failed to load configuration";
6984 let globalkeyhash = findkeyhash conf "global" in
6985 let wsfd, winw, winh = Wsi.init (object
6986 method expose =
6987 state.wthack <- false;
6988 if nogeomcmds state.geomcmds || platform == Posx
6989 then display ()
6990 else (
6991 GlClear.color (scalecolor2 conf.bgcolor);
6992 GlClear.clear [`color];
6994 method display = display ()
6995 method reshape w h = reshape w h
6996 method mouse b d x y m = mouse b d x y m
6997 method motion x y = state.mpos <- (x, y); motion x y
6998 method pmotion x y = state.mpos <- (x, y); pmotion x y
6999 method key k m =
7000 let mascm = m land (
7001 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7002 ) in
7003 match state.keystate with
7004 | KSnone ->
7005 let km = k, mascm in
7006 begin
7007 match
7008 let modehash = state.uioh#modehash in
7009 try Hashtbl.find modehash km
7010 with Not_found ->
7011 try Hashtbl.find globalkeyhash km
7012 with Not_found -> KMinsrt (k, m)
7013 with
7014 | KMinsrt (k, m) -> keyboard k m
7015 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7016 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7018 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7019 List.iter (fun (k, m) -> keyboard k m) insrt;
7020 state.keystate <- KSnone
7021 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7022 state.keystate <- KSinto (keys, insrt)
7023 | _ ->
7024 state.keystate <- KSnone
7026 method enter x y = state.mpos <- (x, y); pmotion x y
7027 method leave = state.mpos <- (-1, -1)
7028 method quit = raise Quit
7029 end) conf.winw conf.winh (platform = Posx) in
7031 state.wsfd <- wsfd;
7033 if not (
7034 List.exists GlMisc.check_extension
7035 [ "GL_ARB_texture_rectangle"
7036 ; "GL_EXT_texture_recangle"
7037 ; "GL_NV_texture_rectangle" ]
7039 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7041 let cr, sw =
7042 match Ne.pipe () with
7043 | Ne.Exn exn ->
7044 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
7045 exit 1
7046 | Ne.Res rw -> rw
7047 and sr, cw =
7048 match Ne.pipe () with
7049 | Ne.Exn exn ->
7050 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7051 exit 1
7052 | Ne.Res rw -> rw
7055 cloexec cr;
7056 cloexec sw;
7057 cloexec sr;
7058 cloexec cw;
7060 setcheckers conf.checkers;
7061 redirectstderr ();
7063 init (cr, cw) (
7064 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7065 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7066 !Config.fontpath, !trimcachepath,
7067 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7069 state.sr <- sr;
7070 state.sw <- sw;
7071 state.text <- "Opening " ^ (mbtoutf8 state.path);
7072 reshape winw winh;
7073 opendoc state.path state.password;
7074 state.uioh <- uioh;
7076 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7078 let rec loop deadline =
7079 let r =
7080 match state.errfd with
7081 | None -> [state.sr; state.wsfd]
7082 | Some fd -> [state.sr; state.wsfd; fd]
7084 if state.redisplay
7085 then (
7086 state.redisplay <- false;
7087 display ();
7089 let timeout =
7090 let now = now () in
7091 if deadline > now
7092 then (
7093 if deadline = infinity
7094 then ~-.1.0
7095 else max 0.0 (deadline -. now)
7097 else 0.0
7099 let r, _, _ =
7100 try tempfailureretry (Unix.select r [] []) timeout
7101 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7103 begin match r with
7104 | [] ->
7105 state.ghyll None;
7106 let newdeadline =
7107 if state.ghyll == noghyll
7108 then
7109 match state.autoscroll with
7110 | Some step when step != 0 ->
7111 let y = state.y + step in
7112 let y =
7113 if y < 0
7114 then state.maxy
7115 else if y >= state.maxy then 0 else y
7117 gotoy y;
7118 if state.mode = View
7119 then state.text <- "";
7120 deadline +. 0.01
7121 | _ -> infinity
7122 else deadline +. 0.01
7124 loop newdeadline
7126 | l ->
7127 let rec checkfds = function
7128 | [] -> ()
7129 | fd :: rest when fd = state.sr ->
7130 let cmd = readcmd state.sr in
7131 act cmd;
7132 checkfds rest
7134 | fd :: rest when fd = state.wsfd ->
7135 Wsi.readresp fd;
7136 checkfds rest
7138 | fd :: rest ->
7139 let s = String.create 80 in
7140 let n = tempfailureretry (Unix.read fd s 0) 80 in
7141 if conf.redirectstderr
7142 then (
7143 Buffer.add_substring state.errmsgs s 0 n;
7144 state.newerrmsgs <- true;
7145 state.redisplay <- true;
7147 else (
7148 prerr_string (String.sub s 0 n);
7149 flush stderr;
7151 checkfds rest
7153 checkfds l;
7154 let newdeadline =
7155 let deadline1 =
7156 if deadline = infinity
7157 then now () +. 0.01
7158 else deadline
7160 match state.autoscroll with
7161 | Some step when step != 0 -> deadline1
7162 | _ -> if state.ghyll == noghyll then infinity else deadline1
7164 loop newdeadline
7165 end;
7168 loop infinity;
7169 with Quit ->
7170 Config.save ();