Add -dest option to jump to named destination on startup/reload
[llpp.git] / main.ml
blob7a468c3485aded424529bbaf5cc2daaf25e945a1
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath * trimcachepath
20 * haspbo)
21 and pageno = int
22 and width = int
23 and height = int
24 and leftx = int
25 and opaque = string
26 and recttype = int
27 and pixmapsize = int
28 and angle = int
29 and proportional = bool
30 and trimmargins = bool
31 and interpagespace = int
32 and texcount = int
33 and sliceheight = int
34 and gen = int
35 and top = float
36 and dtop = float
37 and fontpath = string
38 and trimcachepath = string
39 and memsize = int
40 and aalevel = int
41 and irect = (int * int * int * int)
42 and trimparams = (trimmargins * irect)
43 and colorspace = | Rgb | Bgr | Gray
44 and haspbo = bool
47 type link =
48 | Lnotfound
49 | Lfound of int
50 and linkdir =
51 | LDfirst
52 | LDlast
53 | LDfirstvisible of (int * int * int)
54 | LDleft of int
55 | LDright of int
56 | LDdown of int
57 | LDup of int
60 type pagewithlinks =
61 | Pwlnotfound
62 | Pwl of int
65 type keymap =
66 | KMinsrt of key
67 | KMinsrl of key list
68 | KMmulti of key list * key list
69 and key = int * int
70 and keyhash = (key, keymap) Hashtbl.t
71 and keystate =
72 | KSnone
73 | KSinto of (key list * key list)
76 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
77 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
79 type pipe = (Unix.file_descr * Unix.file_descr);;
81 external init : pipe -> params -> unit = "ml_init";;
82 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
83 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
84 external getpdimrect : int -> float array = "ml_getpdimrect";;
85 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
86 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
87 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
88 external measurestr : int -> string -> float = "ml_measure_string";;
89 external getmaxw : unit -> float = "ml_getmaxw";;
90 external postprocess :
91 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
92 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
93 external platform : unit -> platform = "ml_platform";;
94 external setaalevel : int -> unit = "ml_setaalevel";;
95 external realloctexts : int -> bool = "ml_realloctexts";;
96 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
97 external findlink : opaque -> linkdir -> link = "ml_findlink";;
98 external getlink : opaque -> int -> under = "ml_getlink";;
99 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
100 external getlinkcount : opaque -> int = "ml_getlinkcount";;
101 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
102 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
103 external mbtoutf8 : string -> string = "ml_mbtoutf8";;
104 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
105 external freepbo : string -> unit = "ml_freepbo";;
106 external unmappbo : string -> unit = "ml_unmappbo";;
107 external pbousable : unit -> bool = "ml_pbo_usable";;
109 let platform_to_string = function
110 | Punknown -> "unknown"
111 | Plinux -> "Linux"
112 | Posx -> "OSX"
113 | Psun -> "Sun"
114 | Pfreebsd -> "FreeBSD"
115 | Pdragonflybsd -> "DragonflyBSD"
116 | Popenbsd -> "OpenBSD"
117 | Pnetbsd -> "NetBSD"
118 | Pcygwin -> "Cygwin"
121 let platform = platform ();;
123 let popen cmd fda =
124 if platform = Pcygwin
125 then (
126 let sh = "/bin/sh" in
127 let args = [|sh; "-c"; cmd|] in
128 let rec std si so se = function
129 | [] -> si, so, se
130 | (fd, 0) :: rest -> std fd so se rest
131 | (fd, -1) :: rest ->
132 Unix.set_close_on_exec fd;
133 std si so se rest
134 | (_, n) :: _ ->
135 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
137 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
138 ignore (Unix.create_process sh args si so se)
140 else popen cmd fda;
143 type x = int
144 and y = int
145 and tilex = int
146 and tiley = int
147 and tileparams = (x * y * width * height * tilex * tiley)
150 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
152 type mpos = int * int
153 and mstate =
154 | Msel of (mpos * mpos)
155 | Mpan of mpos
156 | Mscrolly | Mscrollx
157 | Mzoom of (int * int)
158 | Mzoomrect of (mpos * mpos)
159 | Mnone
162 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
163 and onkey = string -> int -> te
164 and ondone = string -> unit
165 and histcancel = unit -> unit
166 and onhist = ((histcmd -> string) * histcancel)
167 and histcmd = HCnext | HCprev | HCfirst | HClast
168 and cancelonempty = bool
169 and te =
170 | TEstop
171 | TEdone of string
172 | TEcont of string
173 | TEswitch of textentry
176 type 'a circbuf =
177 { store : 'a array
178 ; mutable rc : int
179 ; mutable wc : int
180 ; mutable len : int
184 let bound v minv maxv =
185 max minv (min maxv v);
188 let cbnew n v =
189 { store = Array.create n v
190 ; rc = 0
191 ; wc = 0
192 ; len = 0
196 let cbcap b = Array.length b.store;;
198 let cbput b v =
199 let cap = cbcap b in
200 b.store.(b.wc) <- v;
201 b.wc <- (b.wc + 1) mod cap;
202 b.rc <- b.wc;
203 b.len <- min (b.len + 1) cap;
206 let cbempty b = b.len = 0;;
208 let cbgetg b circular dir =
209 if cbempty b
210 then b.store.(0)
211 else
212 let rc = b.rc + dir in
213 let rc =
214 if circular
215 then (
216 if rc = -1
217 then b.len-1
218 else (
219 if rc >= b.len
220 then 0
221 else rc
224 else bound rc 0 (b.len-1)
226 b.rc <- rc;
227 b.store.(rc);
230 let cbget b = cbgetg b false;;
231 let cbgetc b = cbgetg b true;;
233 let drawstring size x y s =
234 Gl.enable `blend;
235 Gl.enable `texture_2d;
236 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
237 ignore (drawstr size x y s);
238 Gl.disable `blend;
239 Gl.disable `texture_2d;
242 let drawstring1 size x y s =
243 drawstr size x y s;
246 let drawstring2 size x y fmt =
247 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
250 type page =
251 { pageno : int
252 ; pagedimno : int
253 ; pagew : int
254 ; pageh : int
255 ; pagex : int
256 ; pagey : int
257 ; pagevw : int
258 ; pagevh : int
259 ; pagedispx : int
260 ; pagedispy : int
261 ; pagecol : int
265 let debugl l =
266 dolog "l %d dim=%d {" l.pageno l.pagedimno;
267 dolog " WxH %dx%d" l.pagew l.pageh;
268 dolog " vWxH %dx%d" l.pagevw l.pagevh;
269 dolog " pagex,y %d,%d" l.pagex l.pagey;
270 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
271 dolog " column %d" l.pagecol;
272 dolog "}";
275 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
276 dolog "rect {";
277 dolog " x0,y0=(% f, % f)" x0 y0;
278 dolog " x1,y1=(% f, % f)" x1 y1;
279 dolog " x2,y2=(% f, % f)" x2 y2;
280 dolog " x3,y3=(% f, % f)" x3 y3;
281 dolog "}";
284 type multicolumns = multicol * pagegeom
285 and singlecolumn = pagegeom
286 and splitcolumns = columncount * pagegeom
287 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
288 and multicol = columncount * covercount * covercount
289 and pdimno = int
290 and columncount = int
291 and covercount = int;;
293 type conf =
294 { mutable scrollbw : int
295 ; mutable scrollh : int
296 ; mutable icase : bool
297 ; mutable preload : bool
298 ; mutable pagebias : int
299 ; mutable verbose : bool
300 ; mutable debug : bool
301 ; mutable scrollstep : int
302 ; mutable hscrollstep : int
303 ; mutable maxhfit : bool
304 ; mutable crophack : bool
305 ; mutable autoscrollstep : int
306 ; mutable maxwait : float option
307 ; mutable hlinks : bool
308 ; mutable underinfo : bool
309 ; mutable interpagespace : interpagespace
310 ; mutable zoom : float
311 ; mutable presentation : bool
312 ; mutable angle : angle
313 ; mutable winw : int
314 ; mutable winh : int
315 ; mutable savebmarks : bool
316 ; mutable proportional : proportional
317 ; mutable trimmargins : trimmargins
318 ; mutable trimfuzz : irect
319 ; mutable memlimit : memsize
320 ; mutable texcount : texcount
321 ; mutable sliceheight : sliceheight
322 ; mutable thumbw : width
323 ; mutable jumpback : bool
324 ; mutable bgcolor : float * float * float
325 ; mutable bedefault : bool
326 ; mutable scrollbarinpm : bool
327 ; mutable tilew : int
328 ; mutable tileh : int
329 ; mutable mustoresize : memsize
330 ; mutable checkers : bool
331 ; mutable aalevel : int
332 ; mutable urilauncher : string
333 ; mutable pathlauncher : string
334 ; mutable colorspace : colorspace
335 ; mutable invert : bool
336 ; mutable colorscale : float
337 ; mutable redirectstderr : bool
338 ; mutable ghyllscroll : (int * int * int) option
339 ; mutable columns : columns
340 ; mutable beyecolumns : columncount option
341 ; mutable selcmd : string
342 ; mutable updatecurs : bool
343 ; mutable keyhashes : (string * keyhash) list
344 ; mutable hfsize : int
345 ; mutable pgscale : float
346 ; mutable usepbo : bool
347 ; mutable wheelbypage : bool
349 and columns =
350 | Csingle of singlecolumn
351 | Cmulti of multicolumns
352 | Csplit of splitcolumns
355 type anchor = pageno * top * dtop;;
357 type outline = string * int * anchor;;
359 type rect = float * float * float * float * float * float * float * float;;
361 type tile = opaque * pixmapsize * elapsed
362 and elapsed = float;;
363 type pagemapkey = pageno * gen;;
364 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
365 and row = int
366 and col = int;;
368 let emptyanchor = (0, 0.0, 0.0);;
370 type infochange = | Memused | Docinfo | Pdim;;
372 class type uioh = object
373 method display : unit
374 method key : int -> int -> uioh
375 method button : int -> bool -> int -> int -> int -> uioh
376 method motion : int -> int -> uioh
377 method pmotion : int -> int -> uioh
378 method infochanged : infochange -> unit
379 method scrollpw : (int * float * float)
380 method scrollph : (int * float * float)
381 method modehash : keyhash
382 end;;
384 type mode =
385 | Birdseye of (conf * leftx * pageno * pageno * anchor)
386 | Textentry of (textentry * onleave)
387 | View
388 | LinkNav of linktarget
389 and onleave = leavetextentrystatus -> unit
390 and leavetextentrystatus = | Cancel | Confirm
391 and helpitem = string * int * action
392 and action =
393 | Noaction
394 | Action of (uioh -> uioh)
395 and linktarget =
396 | Ltexact of (pageno * int)
397 | Ltgendir of int
400 let isbirdseye = function Birdseye _ -> true | _ -> false;;
401 let istextentry = function Textentry _ -> true | _ -> false;;
403 type currently =
404 | Idle
405 | Loading of (page * gen)
406 | Tiling of (
407 page * opaque * colorspace * angle * gen * col * row * width * height
409 | Outlining of outline list
412 let emptykeyhash = Hashtbl.create 0;;
413 let nouioh : uioh = object (self)
414 method display = ()
415 method key _ _ = self
416 method button _ _ _ _ _ = self
417 method motion _ _ = self
418 method pmotion _ _ = self
419 method infochanged _ = ()
420 method scrollpw = (0, nan, nan)
421 method scrollph = (0, nan, nan)
422 method modehash = emptykeyhash
423 end;;
425 type state =
426 { mutable sr : Unix.file_descr
427 ; mutable sw : Unix.file_descr
428 ; mutable wsfd : Unix.file_descr
429 ; mutable errfd : Unix.file_descr option
430 ; mutable stderr : Unix.file_descr
431 ; mutable errmsgs : Buffer.t
432 ; mutable newerrmsgs : bool
433 ; mutable w : int
434 ; mutable x : int
435 ; mutable y : int
436 ; mutable scrollw : int
437 ; mutable hscrollh : int
438 ; mutable anchor : anchor
439 ; mutable ranchors : (string * string * anchor) list
440 ; mutable maxy : int
441 ; mutable layout : page list
442 ; pagemap : (pagemapkey, opaque) Hashtbl.t
443 ; tilemap : (tilemapkey, tile) Hashtbl.t
444 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
445 ; mutable pdims : (pageno * width * height * leftx) list
446 ; mutable pagecount : int
447 ; mutable currently : currently
448 ; mutable mstate : mstate
449 ; mutable searchpattern : string
450 ; mutable rects : (pageno * recttype * rect) list
451 ; mutable rects1 : (pageno * recttype * rect) list
452 ; mutable text : string
453 ; mutable fullscreen : (width * height) option
454 ; mutable mode : mode
455 ; mutable uioh : uioh
456 ; mutable outlines : outline array
457 ; mutable bookmarks : outline list
458 ; mutable path : string
459 ; mutable password : string
460 ; mutable nameddest : string
461 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
462 ; mutable memused : memsize
463 ; mutable gen : gen
464 ; mutable throttle : (page list * int * float) option
465 ; mutable autoscroll : int option
466 ; mutable ghyll : (int option -> unit)
467 ; mutable help : helpitem array
468 ; mutable docinfo : (int * string) list
469 ; mutable texid : GlTex.texture_id option
470 ; hists : hists
471 ; mutable prevzoom : float
472 ; mutable progress : float
473 ; mutable redisplay : bool
474 ; mutable mpos : mpos
475 ; mutable keystate : keystate
476 ; mutable glinks : bool
477 ; mutable prevcolumns : (columns * float) option
478 ; mutable wthack : bool
480 and hists =
481 { pat : string circbuf
482 ; pag : string circbuf
483 ; nav : anchor circbuf
484 ; sel : string circbuf
488 let defconf =
489 { scrollbw = 7
490 ; scrollh = 12
491 ; icase = true
492 ; preload = true
493 ; pagebias = 0
494 ; verbose = false
495 ; debug = false
496 ; scrollstep = 24
497 ; hscrollstep = 24
498 ; maxhfit = true
499 ; crophack = false
500 ; autoscrollstep = 2
501 ; maxwait = None
502 ; hlinks = false
503 ; underinfo = false
504 ; interpagespace = 2
505 ; zoom = 1.0
506 ; presentation = false
507 ; angle = 0
508 ; winw = 900
509 ; winh = 900
510 ; savebmarks = true
511 ; proportional = true
512 ; trimmargins = false
513 ; trimfuzz = (0,0,0,0)
514 ; memlimit = 32 lsl 20
515 ; texcount = 256
516 ; sliceheight = 24
517 ; thumbw = 76
518 ; jumpback = true
519 ; bgcolor = (0.5, 0.5, 0.5)
520 ; bedefault = false
521 ; scrollbarinpm = true
522 ; tilew = 2048
523 ; tileh = 2048
524 ; mustoresize = 256 lsl 20
525 ; checkers = true
526 ; aalevel = 8
527 ; urilauncher =
528 (match platform with
529 | Plinux | Pfreebsd | Pdragonflybsd
530 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
531 | Posx -> "open \"%s\""
532 | Pcygwin -> "cygstart \"%s\""
533 | Punknown -> "echo %s")
534 ; pathlauncher = "lp \"%s\""
535 ; selcmd =
536 (match platform with
537 | Plinux | Pfreebsd | Pdragonflybsd
538 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
539 | Posx -> "pbcopy"
540 | Pcygwin -> "wsel"
541 | Punknown -> "cat")
542 ; colorspace = Rgb
543 ; invert = false
544 ; colorscale = 1.0
545 ; redirectstderr = false
546 ; ghyllscroll = None
547 ; columns = Csingle [||]
548 ; beyecolumns = None
549 ; updatecurs = false
550 ; hfsize = 12
551 ; pgscale = 1.0
552 ; usepbo = false
553 ; wheelbypage = false
554 ; keyhashes =
555 let mk n = (n, Hashtbl.create 1) in
556 [ mk "global"
557 ; mk "info"
558 ; mk "help"
559 ; mk "outline"
560 ; mk "listview"
561 ; mk "birdseye"
562 ; mk "textentry"
563 ; mk "links"
564 ; mk "view"
569 let findkeyhash c name =
570 try List.assoc name c.keyhashes
571 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
574 let conf = { defconf with angle = defconf.angle };;
576 let pgscale h = truncate (float h *. conf.pgscale);;
578 type fontstate =
579 { mutable fontsize : int
580 ; mutable wwidth : float
581 ; mutable maxrows : int
585 let fstate =
586 { fontsize = 14
587 ; wwidth = nan
588 ; maxrows = -1
592 let setfontsize n =
593 fstate.fontsize <- n;
594 fstate.wwidth <- measurestr fstate.fontsize "w";
595 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
598 let geturl s =
599 let colonpos = try String.index s ':' with Not_found -> -1 in
600 let len = String.length s in
601 if colonpos >= 0 && colonpos + 3 < len
602 then (
603 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
604 then
605 let schemestartpos =
606 try String.rindex_from s colonpos ' '
607 with Not_found -> -1
609 let scheme =
610 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
612 match scheme with
613 | "http" | "ftp" | "mailto" ->
614 let epos =
615 try String.index_from s colonpos ' '
616 with Not_found -> len
618 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
619 | _ -> ""
620 else ""
622 else ""
625 let gotouri uri =
626 if String.length conf.urilauncher = 0
627 then print_endline uri
628 else (
629 let url = geturl uri in
630 if String.length url = 0
631 then print_endline uri
632 else
633 let re = Str.regexp "%s" in
634 let command = Str.global_replace re url conf.urilauncher in
635 try popen command []
636 with exn ->
637 Printf.eprintf
638 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
639 flush stderr;
643 let version () =
644 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
645 (platform_to_string platform) Sys.word_size Sys.ocaml_version
648 let makehelp () =
649 let strings = version () :: "" :: Help.keys in
650 Array.of_list (
651 List.map (fun s ->
652 let url = geturl s in
653 if String.length url > 0
654 then (s, 0, Action (fun u -> gotouri url; u))
655 else (s, 0, Noaction)
656 ) strings);
659 let noghyll _ = ();;
660 let firstgeomcmds = "", [];;
662 let state =
663 { sr = Unix.stdin
664 ; sw = Unix.stdin
665 ; wsfd = Unix.stdin
666 ; errfd = None
667 ; stderr = Unix.stderr
668 ; errmsgs = Buffer.create 0
669 ; newerrmsgs = false
670 ; x = 0
671 ; y = 0
672 ; w = 0
673 ; scrollw = 0
674 ; hscrollh = 0
675 ; anchor = emptyanchor
676 ; ranchors = []
677 ; layout = []
678 ; maxy = max_int
679 ; tilelru = Queue.create ()
680 ; pagemap = Hashtbl.create 10
681 ; tilemap = Hashtbl.create 10
682 ; pdims = []
683 ; pagecount = 0
684 ; currently = Idle
685 ; mstate = Mnone
686 ; rects = []
687 ; rects1 = []
688 ; text = ""
689 ; mode = View
690 ; fullscreen = None
691 ; searchpattern = ""
692 ; outlines = [||]
693 ; bookmarks = []
694 ; path = ""
695 ; password = ""
696 ; nameddest = ""
697 ; geomcmds = firstgeomcmds
698 ; hists =
699 { nav = cbnew 10 emptyanchor
700 ; pat = cbnew 10 ""
701 ; pag = cbnew 10 ""
702 ; sel = cbnew 10 ""
704 ; memused = 0
705 ; gen = 0
706 ; throttle = None
707 ; autoscroll = None
708 ; ghyll = noghyll
709 ; help = makehelp ()
710 ; docinfo = []
711 ; texid = None
712 ; prevzoom = 1.0
713 ; progress = -1.0
714 ; uioh = nouioh
715 ; redisplay = true
716 ; mpos = (-1, -1)
717 ; keystate = KSnone
718 ; glinks = false
719 ; prevcolumns = None
720 ; wthack = false
724 let vlog fmt =
725 if conf.verbose
726 then
727 Printf.kprintf prerr_endline fmt
728 else
729 Printf.kprintf ignore fmt
732 let launchpath () =
733 if String.length conf.pathlauncher = 0
734 then print_endline state.path
735 else (
736 let re = Str.regexp "%s" in
737 let command = Str.global_replace re state.path conf.pathlauncher in
738 try popen command []
739 with exn ->
740 Printf.eprintf
741 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
742 flush stderr;
746 module Ne = struct
747 type 'a t = | Res of 'a | Exn of exn;;
749 let pipe () =
750 try Res (Unix.pipe ())
751 with exn -> Exn exn
754 let clo fd f =
755 try Unix.close fd
756 with exn -> f (Printexc.to_string exn)
759 let dup fd =
760 try Res (Unix.dup fd)
761 with exn -> Exn exn
764 let dup2 fd1 fd2 =
765 try Res (Unix.dup2 fd1 fd2)
766 with exn -> Exn exn
768 end;;
770 let redirectstderr () =
771 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
772 if conf.redirectstderr
773 then
774 match Ne.pipe () with
775 | Ne.Exn exn ->
776 dolog "failed to create stderr redirection pipes: %s"
777 (Printexc.to_string exn)
779 | Ne.Res (r, w) ->
780 begin match Ne.dup Unix.stderr with
781 | Ne.Exn exn ->
782 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
783 Ne.clo r (clofail "pipe/r");
784 Ne.clo w (clofail "pipe/w");
786 | Ne.Res dupstderr ->
787 begin match Ne.dup2 w Unix.stderr with
788 | Ne.Exn exn ->
789 dolog "failed to dup2 to stderr: %s"
790 (Printexc.to_string exn);
791 Ne.clo dupstderr (clofail "stderr duplicate");
792 Ne.clo r (clofail "redir pipe/r");
793 Ne.clo w (clofail "redir pipe/w");
795 | Ne.Res () ->
796 state.stderr <- dupstderr;
797 state.errfd <- Some r;
798 end;
800 else (
801 state.newerrmsgs <- false;
802 begin match state.errfd with
803 | Some fd ->
804 begin match Ne.dup2 state.stderr Unix.stderr with
805 | Ne.Exn exn ->
806 dolog "failed to dup2 original stderr: %s"
807 (Printexc.to_string exn)
808 | Ne.Res () ->
809 Ne.clo fd (clofail "dup of stderr");
810 Unix.dup2 state.stderr Unix.stderr;
811 state.errfd <- None;
812 end;
813 | None -> ()
814 end;
815 prerr_string (Buffer.contents state.errmsgs);
816 flush stderr;
817 Buffer.clear state.errmsgs;
821 module G =
822 struct
823 let postRedisplay who =
824 if conf.verbose
825 then prerr_endline ("redisplay for " ^ who);
826 state.redisplay <- true;
828 end;;
830 let getopaque pageno =
831 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
832 with Not_found -> None
835 let putopaque pageno opaque =
836 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
839 let pagetranslatepoint l x y =
840 let dy = y - l.pagedispy in
841 let y = dy + l.pagey in
842 let dx = x - l.pagedispx in
843 let x = dx + l.pagex in
844 (x, y);
847 let getunder x y =
848 let rec f = function
849 | l :: rest ->
850 begin match getopaque l.pageno with
851 | Some opaque ->
852 let x0 = l.pagedispx in
853 let x1 = x0 + l.pagevw in
854 let y0 = l.pagedispy in
855 let y1 = y0 + l.pagevh in
856 if y >= y0 && y <= y1 && x >= x0 && x <= x1
857 then
858 let px, py = pagetranslatepoint l x y in
859 match whatsunder opaque px py with
860 | Unone -> f rest
861 | under -> under
862 else f rest
863 | _ ->
864 f rest
866 | [] -> Unone
868 f state.layout
871 let showtext c s =
872 state.text <- Printf.sprintf "%c%s" c s;
873 G.postRedisplay "showtext";
876 let undertext = function
877 | Unone -> "none"
878 | Ulinkuri s -> s
879 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
880 | Utext s -> "font: " ^ s
881 | Uunexpected s -> "unexpected: " ^ s
882 | Ulaunch s -> "launch: " ^ s
883 | Unamed s -> "named: " ^ s
884 | Uremote (filename, pageno) ->
885 Printf.sprintf "%s: page %d" filename (pageno+1)
888 let updateunder x y =
889 match getunder x y with
890 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
891 | Ulinkuri uri ->
892 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
893 Wsi.setcursor Wsi.CURSOR_INFO
894 | Ulinkgoto (pageno, _) ->
895 if conf.underinfo
896 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
897 Wsi.setcursor Wsi.CURSOR_INFO
898 | Utext s ->
899 if conf.underinfo then showtext 'f' ("ont: " ^ s);
900 Wsi.setcursor Wsi.CURSOR_TEXT
901 | Uunexpected s ->
902 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
903 Wsi.setcursor Wsi.CURSOR_INHERIT
904 | Ulaunch s ->
905 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
906 Wsi.setcursor Wsi.CURSOR_INHERIT
907 | Unamed s ->
908 if conf.underinfo then showtext 'n' ("amed: " ^ s);
909 Wsi.setcursor Wsi.CURSOR_INHERIT
910 | Uremote (filename, pageno) ->
911 if conf.underinfo then showtext 'r'
912 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
913 Wsi.setcursor Wsi.CURSOR_INFO
916 let showlinktype under =
917 if conf.underinfo
918 then
919 match under with
920 | Unone -> ()
921 | under ->
922 let s = undertext under in
923 showtext ' ' s
926 let addchar s c =
927 let b = Buffer.create (String.length s + 1) in
928 Buffer.add_string b s;
929 Buffer.add_char b c;
930 Buffer.contents b;
933 let colorspace_of_string s =
934 match String.lowercase s with
935 | "rgb" -> Rgb
936 | "bgr" -> Bgr
937 | "gray" -> Gray
938 | _ -> failwith "invalid colorspace"
941 let int_of_colorspace = function
942 | Rgb -> 0
943 | Bgr -> 1
944 | Gray -> 2
947 let colorspace_of_int = function
948 | 0 -> Rgb
949 | 1 -> Bgr
950 | 2 -> Gray
951 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
954 let colorspace_to_string = function
955 | Rgb -> "rgb"
956 | Bgr -> "bgr"
957 | Gray -> "gray"
960 let intentry_with_suffix text key =
961 let c =
962 if key >= 32 && key < 127
963 then Char.chr key
964 else '\000'
966 match Char.lowercase c with
967 | '0' .. '9' ->
968 let text = addchar text c in
969 TEcont text
971 | 'k' | 'm' | 'g' ->
972 let text = addchar text c in
973 TEcont text
975 | _ ->
976 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
977 TEcont text
980 let multicolumns_to_string (n, a, b) =
981 if a = 0 && b = 0
982 then Printf.sprintf "%d" n
983 else Printf.sprintf "%d,%d,%d" n a b;
986 let multicolumns_of_string s =
988 (int_of_string s, 0, 0)
989 with _ ->
990 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
991 if a > 1 || b > 1
992 then failwith "subtly broken"; (n, a, b)
996 let readcmd fd =
997 let s = "xxxx" in
998 let n = Unix.read fd s 0 4 in
999 if n != 4 then failwith "incomplete read(len)";
1000 let len = 0
1001 lor (Char.code s.[0] lsl 24)
1002 lor (Char.code s.[1] lsl 16)
1003 lor (Char.code s.[2] lsl 8)
1004 lor (Char.code s.[3] lsl 0)
1006 let s = String.create len in
1007 let n = Unix.read fd s 0 len in
1008 if n != len then failwith "incomplete read(data)";
1012 let btod b = if b then 1 else 0;;
1014 let wcmd fmt =
1015 let b = Buffer.create 16 in
1016 Buffer.add_string b "llll";
1017 Printf.kbprintf
1018 (fun b ->
1019 let s = Buffer.contents b in
1020 let n = String.length s in
1021 let len = n - 4 in
1022 (* dolog "wcmd %S" (String.sub s 4 len); *)
1023 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1024 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1025 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1026 s.[3] <- Char.chr (len land 0xff);
1027 let n' = Unix.write state.sw s 0 n in
1028 if n' != n then failwith "write failed";
1029 ) b fmt;
1032 let calcips h =
1033 let d = conf.winh - h in
1034 max conf.interpagespace ((d + 1) / 2)
1037 let rowyh (c, coverA, coverB) b n =
1038 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1039 then
1040 let _, _, vy, (_, _, h, _) = b.(n) in
1041 (vy, h)
1042 else
1043 let n' = n - coverA in
1044 let d = n' mod c in
1045 let s = n - d in
1046 let e = min state.pagecount (s + c) in
1047 let rec find m miny maxh = if m = e then miny, maxh else
1048 let _, _, y, (_, _, h, _) = b.(m) in
1049 let miny = min miny y in
1050 let maxh = max maxh h in
1051 find (m+1) miny maxh
1052 in find s max_int 0
1055 let calcheight () =
1056 match conf.columns with
1057 | Cmulti ((_, _, _) as cl, b) ->
1058 if Array.length b > 0
1059 then
1060 let y, h = rowyh cl b (Array.length b - 1) in
1061 y + h + (if conf.presentation then calcips h else 0)
1062 else 0
1063 | Csingle b ->
1064 if Array.length b > 0
1065 then
1066 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1067 y + h + (if conf.presentation then calcips h else 0)
1068 else 0
1069 | Csplit (_, b) ->
1070 if Array.length b > 0
1071 then
1072 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1073 y + h
1074 else 0
1077 let getpageyh pageno =
1078 let pageno = bound pageno 0 (state.pagecount-1) in
1079 match conf.columns with
1080 | Csingle b ->
1081 if Array.length b = 0
1082 then 0, 0
1083 else
1084 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1085 let y =
1086 if conf.presentation
1087 then y - calcips h
1088 else y
1090 y, h
1091 | Cmulti (cl, b) ->
1092 if Array.length b = 0
1093 then 0, 0
1094 else
1095 let y, h = rowyh cl b pageno in
1096 let y =
1097 if conf.presentation
1098 then y - calcips h
1099 else y
1101 y, h
1102 | Csplit (c, b) ->
1103 if Array.length b = 0
1104 then 0, 0
1105 else
1106 let n = pageno*c in
1107 let (_, _, y, (_, _, h, _)) = b.(n) in
1108 y, h
1111 let getpagedim pageno =
1112 let rec f ppdim l =
1113 match l with
1114 | (n, _, _, _) as pdim :: rest ->
1115 if n >= pageno
1116 then (if n = pageno then pdim else ppdim)
1117 else f pdim rest
1119 | [] -> ppdim
1121 f (-1, -1, -1, -1) state.pdims
1124 let getpagey pageno = fst (getpageyh pageno);;
1126 let nogeomcmds cmds =
1127 match cmds with
1128 | s, [] -> String.length s = 0
1129 | _ -> false
1132 let page_of_y y =
1133 let ((c, coverA, coverB) as cl), b =
1134 match conf.columns with
1135 | Csingle b -> (1, 0, 0), b
1136 | Cmulti (c, b) -> c, b
1137 | Csplit (_, b) -> (1, 0, 0), b
1139 let rec bsearch nmin nmax =
1140 if nmin > nmax
1141 then bound nmin 0 (state.pagecount-1)
1142 else
1143 let n = (nmax + nmin) / 2 in
1144 let vy, h = rowyh cl b n in
1145 let y0, y1 =
1146 if conf.presentation
1147 then
1148 let ips = calcips h in
1149 let y0 = vy - ips in
1150 let y1 = vy + h + ips in
1151 y0, y1
1152 else (
1153 if n = 0
1154 then 0, vy + h + conf.interpagespace
1155 else
1156 let y0 = vy - conf.interpagespace in
1157 y0, y0 + h + conf.interpagespace
1160 if y >= y0 && y < y1
1161 then (
1162 if c = 1
1163 then n
1164 else (
1165 if n > coverA
1166 then
1167 if n < state.pagecount - coverB
1168 then ((n-coverA)/c)*c + coverA
1169 else n
1170 else n
1173 else (
1174 if y > y0
1175 then bsearch (n+1) nmax
1176 else bsearch nmin (n-1)
1179 let r = bsearch 0 (state.pagecount-1) in
1183 let layoutN ((columns, coverA, coverB), b) y sh =
1184 let sh = sh - state.hscrollh in
1185 let rec fold accu n =
1186 if n = Array.length b
1187 then accu
1188 else
1189 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1190 if (vy - y) > sh &&
1191 (n = coverA - 1
1192 || n = state.pagecount - coverB
1193 || (n - coverA) mod columns = columns - 1)
1194 then accu
1195 else
1196 let accu =
1197 if vy + h > y
1198 then
1199 let pagey = max 0 (y - vy) in
1200 let pagedispy = if pagey > 0 then 0 else vy - y in
1201 let pagedispx, pagex =
1202 let pdx =
1203 if n = coverA - 1 || n = state.pagecount - coverB
1204 then state.x + (conf.winw - state.scrollw - w) / 2
1205 else dx + xoff + state.x
1207 if pdx < 0
1208 then 0, -pdx
1209 else pdx, 0
1211 let pagevw =
1212 let vw = conf.winw - state.scrollw - pagedispx in
1213 let pw = w - pagex in
1214 min vw pw
1216 let pagevh = min (h - pagey) (sh - pagedispy) in
1217 if pagevw > 0 && pagevh > 0
1218 then
1219 let e =
1220 { pageno = n
1221 ; pagedimno = pdimno
1222 ; pagew = w
1223 ; pageh = h
1224 ; pagex = pagex
1225 ; pagey = pagey
1226 ; pagevw = pagevw
1227 ; pagevh = pagevh
1228 ; pagedispx = pagedispx
1229 ; pagedispy = pagedispy
1230 ; pagecol = 0
1233 e :: accu
1234 else
1235 accu
1236 else
1237 accu
1239 fold accu (n+1)
1241 List.rev (fold [] (page_of_y y));
1244 let layoutS (columns, b) y sh =
1245 let sh = sh - state.hscrollh in
1246 let rec fold accu n =
1247 if n = Array.length b
1248 then accu
1249 else
1250 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1251 if (vy - y) > sh
1252 then accu
1253 else
1254 let accu =
1255 if vy + pageh > y
1256 then
1257 let x = xoff + state.x in
1258 let pagey = max 0 (y - vy) in
1259 let pagedispy = if pagey > 0 then 0 else vy - y in
1260 let pagedispx, pagex =
1261 if px = 0
1262 then (
1263 if x < 0
1264 then 0, -x
1265 else x, 0
1267 else (
1268 let px = px - x in
1269 if px < 0
1270 then -px, 0
1271 else 0, px
1274 let pagecolw = pagew/columns in
1275 let pagedispx =
1276 if pagecolw < conf.winw
1277 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1278 else pagedispx
1280 let pagevw =
1281 let vw = conf.winw - pagedispx - state.scrollw in
1282 let pw = pagew - pagex in
1283 min vw pw
1285 let pagevw = min pagevw pagecolw in
1286 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1287 if pagevw > 0 && pagevh > 0
1288 then
1289 let e =
1290 { pageno = n/columns
1291 ; pagedimno = pdimno
1292 ; pagew = pagew
1293 ; pageh = pageh
1294 ; pagex = pagex
1295 ; pagey = pagey
1296 ; pagevw = pagevw
1297 ; pagevh = pagevh
1298 ; pagedispx = pagedispx
1299 ; pagedispy = pagedispy
1300 ; pagecol = n mod columns
1303 e :: accu
1304 else
1305 accu
1306 else
1307 accu
1309 fold accu (n+1)
1311 List.rev (fold [] 0)
1314 let layout y sh =
1315 if nogeomcmds state.geomcmds
1316 then
1317 match conf.columns with
1318 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1319 | Cmulti c -> layoutN c y sh
1320 | Csplit s -> layoutS s y sh
1321 else []
1324 let clamp incr =
1325 let y = state.y + incr in
1326 let y = max 0 y in
1327 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1331 let itertiles l f =
1332 let tilex = l.pagex mod conf.tilew in
1333 let tiley = l.pagey mod conf.tileh in
1335 let col = l.pagex / conf.tilew in
1336 let row = l.pagey / conf.tileh in
1338 let rec rowloop row y0 dispy h =
1339 if h = 0
1340 then ()
1341 else (
1342 let dh = conf.tileh - y0 in
1343 let dh = min h dh in
1344 let rec colloop col x0 dispx w =
1345 if w = 0
1346 then ()
1347 else (
1348 let dw = conf.tilew - x0 in
1349 let dw = min w dw in
1351 f col row dispx dispy x0 y0 dw dh;
1352 colloop (col+1) 0 (dispx+dw) (w-dw)
1355 colloop col tilex l.pagedispx l.pagevw;
1356 rowloop (row+1) 0 (dispy+dh) (h-dh)
1359 if l.pagevw > 0 && l.pagevh > 0
1360 then rowloop row tiley l.pagedispy l.pagevh;
1363 let gettileopaque l col row =
1364 let key =
1365 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1367 try Some (Hashtbl.find state.tilemap key)
1368 with Not_found -> None
1371 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1372 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1373 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1376 let drawtiles l color =
1377 GlDraw.color color;
1378 let f col row x y tilex tiley w h =
1379 match gettileopaque l col row with
1380 | Some (opaque, _, t) ->
1381 let params = x, y, w, h, tilex, tiley in
1382 if conf.invert
1383 then (
1384 Gl.enable `blend;
1385 GlFunc.blend_func `zero `one_minus_src_color;
1387 drawtile params opaque;
1388 if conf.invert
1389 then Gl.disable `blend;
1390 if conf.debug
1391 then (
1392 let s = Printf.sprintf
1393 "%d[%d,%d] %f sec"
1394 l.pageno col row t
1396 let w = measurestr fstate.fontsize s in
1397 GlMisc.push_attrib [`current];
1398 GlDraw.color (0.0, 0.0, 0.0);
1399 GlDraw.rect
1400 (float (x-2), float (y-2))
1401 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1402 GlDraw.color (1.0, 1.0, 1.0);
1403 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1404 GlMisc.pop_attrib ();
1407 | _ ->
1408 let w =
1409 let lw = conf.winw - state.scrollw - x in
1410 min lw w
1411 and h =
1412 let lh = conf.winh - y in
1413 min lh h
1415 begin match state.texid with
1416 | Some id ->
1417 Gl.enable `texture_2d;
1418 GlTex.bind_texture `texture_2d id;
1419 let x0 = float x
1420 and y0 = float y
1421 and x1 = float (x+w)
1422 and y1 = float (y+h) in
1424 let tw = float w /. 16.0
1425 and th = float h /. 16.0 in
1426 let tx0 = float tilex /. 16.0
1427 and ty0 = float tiley /. 16.0 in
1428 let tx1 = tx0 +. tw
1429 and ty1 = ty0 +. th in
1430 GlDraw.begins `quads;
1431 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1432 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1433 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1434 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1435 GlDraw.ends ();
1437 Gl.disable `texture_2d;
1438 | None ->
1439 GlDraw.color (1.0, 1.0, 1.0);
1440 GlDraw.rect
1441 (float x, float y)
1442 (float (x+w), float (y+h));
1443 end;
1444 if w > 128 && h > fstate.fontsize + 10
1445 then (
1446 GlDraw.color (0.0, 0.0, 0.0);
1447 let c, r =
1448 if conf.verbose
1449 then (col*conf.tilew, row*conf.tileh)
1450 else col, row
1452 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1454 GlDraw.color color;
1456 itertiles l f
1459 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1461 let tilevisible1 l x y =
1462 let ax0 = l.pagex
1463 and ax1 = l.pagex + l.pagevw
1464 and ay0 = l.pagey
1465 and ay1 = l.pagey + l.pagevh in
1467 let bx0 = x
1468 and by0 = y in
1469 let bx1 = min (bx0 + conf.tilew) l.pagew
1470 and by1 = min (by0 + conf.tileh) l.pageh in
1472 let rx0 = max ax0 bx0
1473 and ry0 = max ay0 by0
1474 and rx1 = min ax1 bx1
1475 and ry1 = min ay1 by1 in
1477 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1478 nonemptyintersection
1481 let tilevisible layout n x y =
1482 let rec findpageinlayout m = function
1483 | l :: rest when l.pageno = n ->
1484 tilevisible1 l x y || (
1485 match conf.columns with
1486 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1487 | _ -> false
1489 | _ :: rest -> findpageinlayout 0 rest
1490 | [] -> false
1492 findpageinlayout 0 layout;
1495 let tileready l x y =
1496 tilevisible1 l x y &&
1497 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1500 let tilepage n p layout =
1501 let rec loop = function
1502 | l :: rest ->
1503 if l.pageno = n
1504 then
1505 let f col row _ _ _ _ _ _ =
1506 if state.currently = Idle
1507 then
1508 match gettileopaque l col row with
1509 | Some _ -> ()
1510 | None ->
1511 let x = col*conf.tilew
1512 and y = row*conf.tileh in
1513 let w =
1514 let w = l.pagew - x in
1515 min w conf.tilew
1517 let h =
1518 let h = l.pageh - y in
1519 min h conf.tileh
1521 let pbo =
1522 if conf.usepbo
1523 then getpbo w h conf.colorspace
1524 else "0"
1526 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1527 state.currently <-
1528 Tiling (
1529 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1530 conf.tilew, conf.tileh
1533 itertiles l f;
1534 else
1535 loop rest
1537 | [] -> ()
1539 if nogeomcmds state.geomcmds
1540 then loop layout;
1543 let preloadlayout y =
1544 let y = if y < conf.winh then 0 else y - conf.winh in
1545 let h = conf.winh*3 in
1546 layout y h;
1549 let load pages =
1550 let rec loop pages =
1551 if state.currently != Idle
1552 then ()
1553 else
1554 match pages with
1555 | l :: rest ->
1556 begin match getopaque l.pageno with
1557 | None ->
1558 wcmd "page %d %d" l.pageno l.pagedimno;
1559 state.currently <- Loading (l, state.gen);
1560 | Some opaque ->
1561 tilepage l.pageno opaque pages;
1562 loop rest
1563 end;
1564 | _ -> ()
1566 if nogeomcmds state.geomcmds
1567 then loop pages
1570 let preload pages =
1571 load pages;
1572 if conf.preload && state.currently = Idle
1573 then load (preloadlayout state.y);
1576 let layoutready layout =
1577 let rec fold all ls =
1578 all && match ls with
1579 | l :: rest ->
1580 let seen = ref false in
1581 let allvisible = ref true in
1582 let foo col row _ _ _ _ _ _ =
1583 seen := true;
1584 allvisible := !allvisible &&
1585 begin match gettileopaque l col row with
1586 | Some _ -> true
1587 | None -> false
1590 itertiles l foo;
1591 fold (!seen && !allvisible) rest
1592 | [] -> true
1594 let alltilesvisible = fold true layout in
1595 alltilesvisible;
1598 let gotoy y =
1599 let y = bound y 0 state.maxy in
1600 let y, layout, proceed =
1601 match conf.maxwait with
1602 | Some time when state.ghyll == noghyll ->
1603 begin match state.throttle with
1604 | None ->
1605 let layout = layout y conf.winh in
1606 let ready = layoutready layout in
1607 if not ready
1608 then (
1609 load layout;
1610 state.throttle <- Some (layout, y, now ());
1612 else G.postRedisplay "gotoy showall (None)";
1613 y, layout, ready
1614 | Some (_, _, started) ->
1615 let dt = now () -. started in
1616 if dt > time
1617 then (
1618 state.throttle <- None;
1619 let layout = layout y conf.winh in
1620 load layout;
1621 G.postRedisplay "maxwait";
1622 y, layout, true
1624 else -1, [], false
1627 | _ ->
1628 let layout = layout y conf.winh in
1629 if true || layoutready layout
1630 then G.postRedisplay "gotoy ready";
1631 y, layout, true
1633 if proceed
1634 then (
1635 state.y <- y;
1636 state.layout <- layout;
1637 begin match state.mode with
1638 | LinkNav (Ltexact (pageno, linkno)) ->
1639 let rec loop = function
1640 | [] ->
1641 state.mode <- LinkNav (Ltgendir 0)
1642 | l :: _ when l.pageno = pageno ->
1643 begin match getopaque pageno with
1644 | None ->
1645 state.mode <- LinkNav (Ltgendir 0)
1646 | Some opaque ->
1647 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1648 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1649 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1650 then state.mode <- LinkNav (Ltgendir 0)
1652 | _ :: rest -> loop rest
1654 loop layout
1655 | _ -> ()
1656 end;
1657 begin match state.mode with
1658 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1659 if not (pagevisible layout pageno)
1660 then (
1661 match state.layout with
1662 | [] -> ()
1663 | l :: _ ->
1664 state.mode <- Birdseye (
1665 conf, leftx, l.pageno, hooverpageno, anchor
1668 | LinkNav (Ltgendir dir as lt) ->
1669 let linknav =
1670 let rec loop = function
1671 | [] -> lt
1672 | l :: rest ->
1673 match getopaque l.pageno with
1674 | None -> loop rest
1675 | Some opaque ->
1676 let link =
1677 let ld =
1678 if dir = 0
1679 then LDfirstvisible (l.pagex, l.pagey, dir)
1680 else (
1681 if dir > 0 then LDfirst else LDlast
1684 findlink opaque ld
1686 match link with
1687 | Lnotfound -> loop rest
1688 | Lfound n ->
1689 showlinktype (getlink opaque n);
1690 Ltexact (l.pageno, n)
1692 loop state.layout
1694 state.mode <- LinkNav linknav
1695 | _ -> ()
1696 end;
1697 preload layout;
1699 state.ghyll <- noghyll;
1700 if conf.updatecurs
1701 then (
1702 let mx, my = state.mpos in
1703 updateunder mx my;
1707 let conttiling pageno opaque =
1708 tilepage pageno opaque
1709 (if conf.preload then preloadlayout state.y else state.layout)
1712 let gotoy_and_clear_text y =
1713 if not conf.verbose then state.text <- "";
1714 gotoy y;
1717 let getanchor1 l =
1718 let top =
1719 let coloff = l.pagecol * l.pageh in
1720 float (l.pagey + coloff) /. float l.pageh
1722 let dtop =
1723 if l.pagedispy = 0
1724 then
1726 else
1727 if conf.presentation
1728 then float l.pagedispy /. float (calcips l.pageh)
1729 else float l.pagedispy /. float conf.interpagespace
1731 (l.pageno, top, dtop)
1734 let getanchor () =
1735 match state.layout with
1736 | l :: _ -> getanchor1 l
1737 | [] ->
1738 let n = page_of_y state.y in
1739 let y, h = getpageyh n in
1740 let dy = y - state.y in
1741 let dtop =
1742 if conf.presentation
1743 then
1744 let ips = calcips h in
1745 float (dy + ips) /. float ips
1746 else
1747 float dy /. float conf.interpagespace
1749 (n, 0.0, dtop)
1752 let getanchory (n, top, dtop) =
1753 let y, h = getpageyh n in
1754 if conf.presentation
1755 then
1756 let ips = calcips h in
1757 y + truncate (top*.float h -. dtop*.float ips) + ips;
1758 else
1759 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1762 let gotoanchor anchor =
1763 gotoy (getanchory anchor);
1766 let addnav () =
1767 cbput state.hists.nav (getanchor ());
1770 let getnav dir =
1771 let anchor = cbgetc state.hists.nav dir in
1772 getanchory anchor;
1775 let gotoghyll y =
1776 let scroll f n a b =
1777 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1778 let snake f a b =
1779 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1780 if f < a
1781 then s (float f /. float a)
1782 else (
1783 if f > b
1784 then 1.0 -. s ((float (f-b) /. float (n-b)))
1785 else 1.0
1788 snake f a b
1789 and summa f n a b =
1790 (* courtesy:
1791 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1792 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1793 let iv1 = iv f in
1794 let ins = float a *. iv1
1795 and outs = float (n-b) *. iv1 in
1796 let ones = b - a in
1797 ins +. outs +. float ones
1799 let rec set (_N, _A, _B) y sy =
1800 let sum = summa 1.0 _N _A _B in
1801 let dy = float (y - sy) in
1802 state.ghyll <- (
1803 let rec gf n y1 o =
1804 if n >= _N
1805 then state.ghyll <- noghyll
1806 else
1807 let go n =
1808 let s = scroll n _N _A _B in
1809 let y1 = y1 +. ((s *. dy) /. sum) in
1810 gotoy_and_clear_text (truncate y1);
1811 state.ghyll <- gf (n+1) y1;
1813 match o with
1814 | None -> go n
1815 | Some y' -> set (_N/2, 1, 1) y' state.y
1817 gf 0 (float state.y)
1820 match conf.ghyllscroll with
1821 | None ->
1822 gotoy_and_clear_text y
1823 | Some nab ->
1824 if state.ghyll == noghyll
1825 then set nab y state.y
1826 else state.ghyll (Some y)
1829 let gotopage n top =
1830 let y, h = getpageyh n in
1831 let y = y + (truncate (top *. float h)) in
1832 gotoghyll y
1835 let gotopage1 n top =
1836 let y = getpagey n in
1837 let y = y + top in
1838 gotoghyll y
1841 let invalidate s f =
1842 state.layout <- [];
1843 state.pdims <- [];
1844 state.rects <- [];
1845 state.rects1 <- [];
1846 match state.geomcmds with
1847 | ps, [] when String.length ps = 0 ->
1848 f ();
1849 state.geomcmds <- s, [];
1851 | ps, [] ->
1852 state.geomcmds <- ps, [s, f];
1854 | ps, (s', _) :: rest when s' = s ->
1855 state.geomcmds <- ps, ((s, f) :: rest);
1857 | ps, cmds ->
1858 state.geomcmds <- ps, ((s, f) :: cmds);
1861 let flushpages () =
1862 Hashtbl.iter (fun _ opaque ->
1863 wcmd "freepage %s" opaque;
1864 ) state.pagemap;
1865 Hashtbl.clear state.pagemap;
1868 let opendoc path password nameddest =
1869 state.path <- path;
1870 state.password <- password;
1871 state.gen <- state.gen + 1;
1872 state.docinfo <- [];
1873 state.nameddest <- nameddest;
1875 flushpages ();
1876 setaalevel conf.aalevel;
1877 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1878 wcmd "open %d %s\000%s\000%s\000" (btod state.wthack) path password nameddest;
1879 invalidate "reqlayout"
1880 (fun () ->
1881 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1884 let reload () =
1885 state.anchor <- getanchor ();
1886 state.wthack <- true;
1887 opendoc state.path state.password state.nameddest;
1890 let scalecolor c =
1891 let c = c *. conf.colorscale in
1892 (c, c, c);
1895 let scalecolor2 (r, g, b) =
1896 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1899 let docolumns = function
1900 | Csingle _ ->
1901 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1902 let rec loop pageno pdimno pdim y ph pdims =
1903 if pageno = state.pagecount
1904 then ()
1905 else
1906 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1907 match pdims with
1908 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1909 pdimno+1, pdim, rest
1910 | _ ->
1911 pdimno, pdim, pdims
1913 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1914 let y = y +
1915 (if conf.presentation
1916 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1917 else (if pageno = 0 then 0 else conf.interpagespace)
1920 a.(pageno) <- (pdimno, x, y, pdim);
1921 loop (pageno+1) pdimno pdim (y + h) h pdims
1923 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1924 conf.columns <- Csingle a;
1926 | Cmulti ((columns, coverA, coverB), _) ->
1927 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1928 let rec loop pageno pdimno pdim x y rowh pdims =
1929 let rec fixrow m = if m = pageno then () else
1930 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1931 if h < rowh
1932 then (
1933 let y = y + (rowh - h) / 2 in
1934 a.(m) <- (pdimno, x, y, pdim);
1936 fixrow (m+1)
1938 if pageno = state.pagecount
1939 then fixrow (((pageno - 1) / columns) * columns)
1940 else
1941 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1942 match pdims with
1943 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1944 pdimno+1, pdim, rest
1945 | _ ->
1946 pdimno, pdim, pdims
1948 let x, y, rowh' =
1949 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1950 then (
1951 let x = (conf.winw - state.scrollw - w) / 2 in
1952 let ips =
1953 if conf.presentation then calcips h else conf.interpagespace in
1954 x, y + ips + rowh, h
1956 else (
1957 if (pageno - coverA) mod columns = 0
1958 then (
1959 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1960 let y =
1961 if conf.presentation
1962 then
1963 let ips = calcips h in
1964 y + (if pageno = 0 then 0 else calcips rowh + ips)
1965 else
1966 y + (if pageno = 0 then 0 else conf.interpagespace)
1968 x, y + rowh, h
1970 else x, y, max rowh h
1973 let y =
1974 if pageno > 1 && (pageno - coverA) mod columns = 0
1975 then (
1976 let y =
1977 if pageno = columns && conf.presentation
1978 then (
1979 let ips = calcips rowh in
1980 for i = 0 to pred columns
1982 let (pdimno, x, y, pdim) = a.(i) in
1983 a.(i) <- (pdimno, x, y+ips, pdim)
1984 done;
1985 y+ips;
1987 else y
1989 fixrow (pageno - columns);
1992 else y
1994 a.(pageno) <- (pdimno, x, y, pdim);
1995 let x = x + w + xoff*2 + conf.interpagespace in
1996 loop (pageno+1) pdimno pdim x y rowh' pdims
1998 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1999 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2001 | Csplit (c, _) ->
2002 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2003 let rec loop pageno pdimno pdim y pdims =
2004 if pageno = state.pagecount
2005 then ()
2006 else
2007 let pdimno, ((_, w, h, _) as pdim), pdims =
2008 match pdims with
2009 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2010 pdimno+1, pdim, rest
2011 | _ ->
2012 pdimno, pdim, pdims
2014 let cw = w / c in
2015 let rec loop1 n x y =
2016 if n = c then y else (
2017 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2018 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2021 let y = loop1 0 0 y in
2022 loop (pageno+1) pdimno pdim y pdims
2024 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2025 conf.columns <- Csplit (c, a);
2028 let represent () =
2029 docolumns conf.columns;
2030 state.maxy <- calcheight ();
2031 state.hscrollh <-
2032 if state.w <= conf.winw - state.scrollw
2033 then 0
2034 else state.scrollw
2036 match state.mode with
2037 | Birdseye (_, _, pageno, _, _) ->
2038 let y, h = getpageyh pageno in
2039 let top = (conf.winh - h) / 2 in
2040 gotoy (max 0 (y - top))
2041 | _ -> gotoanchor state.anchor
2044 let reshape w h =
2045 state.wthack <- false;
2046 GlDraw.viewport 0 0 w h;
2047 let firsttime = state.geomcmds == firstgeomcmds in
2048 if not firsttime && nogeomcmds state.geomcmds
2049 then state.anchor <- getanchor ();
2051 conf.winw <- w;
2052 let w = truncate (float w *. conf.zoom) - state.scrollw in
2053 let w = max w 2 in
2054 conf.winh <- h;
2055 setfontsize fstate.fontsize;
2056 GlMat.mode `modelview;
2057 GlMat.load_identity ();
2059 GlMat.mode `projection;
2060 GlMat.load_identity ();
2061 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2062 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2063 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2065 let relx =
2066 if conf.zoom <= 1.0
2067 then 0.0
2068 else float state.x /. float state.w
2070 invalidate "geometry"
2071 (fun () ->
2072 state.w <- w;
2073 if not firsttime
2074 then state.x <- truncate (relx *. float w);
2075 let w =
2076 match conf.columns with
2077 | Csingle _ -> w
2078 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2079 | Csplit (c, _) -> w * c
2081 wcmd "geometry %d %d" w h);
2084 let enttext () =
2085 let len = String.length state.text in
2086 let drawstring s =
2087 let hscrollh =
2088 match state.mode with
2089 | Textentry _
2090 | View ->
2091 let h, _, _ = state.uioh#scrollpw in
2093 | _ -> 0
2095 let rect x w =
2096 GlDraw.rect
2097 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2098 (x+.w, float (conf.winh - hscrollh))
2101 let w = float (conf.winw - state.scrollw - 1) in
2102 if state.progress >= 0.0 && state.progress < 1.0
2103 then (
2104 GlDraw.color (0.3, 0.3, 0.3);
2105 let w1 = w *. state.progress in
2106 rect 0.0 w1;
2107 GlDraw.color (0.0, 0.0, 0.0);
2108 rect w1 (w-.w1)
2110 else (
2111 GlDraw.color (0.0, 0.0, 0.0);
2112 rect 0.0 w;
2115 GlDraw.color (1.0, 1.0, 1.0);
2116 drawstring fstate.fontsize
2117 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2119 let s =
2120 match state.mode with
2121 | Textentry ((prefix, text, _, _, _, _), _) ->
2122 let s =
2123 if len > 0
2124 then
2125 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2126 else
2127 Printf.sprintf "%s%s_" prefix text
2131 | _ -> state.text
2133 let s =
2134 if state.newerrmsgs
2135 then (
2136 if not (istextentry state.mode)
2137 then
2138 let s1 = "(press 'e' to review error messasges)" in
2139 if String.length s > 0 then s ^ " " ^ s1 else s1
2140 else s
2142 else s
2144 if String.length s > 0
2145 then drawstring s
2148 let gctiles () =
2149 let len = Queue.length state.tilelru in
2150 let layout = lazy (
2151 match state.throttle with
2152 | None ->
2153 if conf.preload
2154 then preloadlayout state.y
2155 else state.layout
2156 | Some (layout, _, _) ->
2157 layout
2158 ) in
2159 let rec loop qpos =
2160 if state.memused <= conf.memlimit
2161 then ()
2162 else (
2163 if qpos < len
2164 then
2165 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2166 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2167 let (_, pw, ph, _) = getpagedim n in
2169 gen = state.gen
2170 && colorspace = conf.colorspace
2171 && angle = conf.angle
2172 && pagew = pw
2173 && pageh = ph
2174 && (
2175 let x = col*conf.tilew
2176 and y = row*conf.tileh in
2177 tilevisible (Lazy.force_val layout) n x y
2179 then Queue.push lruitem state.tilelru
2180 else (
2181 freepbo p;
2182 wcmd "freetile %s" p;
2183 state.memused <- state.memused - s;
2184 state.uioh#infochanged Memused;
2185 Hashtbl.remove state.tilemap k;
2187 loop (qpos+1)
2190 loop 0
2193 let flushtiles () =
2194 Queue.iter (fun (k, p, s) ->
2195 wcmd "freetile %s" p;
2196 state.memused <- state.memused - s;
2197 state.uioh#infochanged Memused;
2198 Hashtbl.remove state.tilemap k;
2199 ) state.tilelru;
2200 Queue.clear state.tilelru;
2201 load state.layout;
2204 let logcurrently = function
2205 | Idle -> dolog "Idle"
2206 | Loading (l, gen) ->
2207 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2208 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2209 dolog
2210 "Tiling %d[%d,%d] page=%s cs=%s angle"
2211 l.pageno col row pageopaque
2212 (colorspace_to_string colorspace)
2214 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2215 angle gen conf.angle state.gen
2216 tilew tileh
2217 conf.tilew conf.tileh
2219 | Outlining _ ->
2220 dolog "outlining"
2223 let act cmds =
2224 (* dolog "%S" cmds; *)
2225 let op, args =
2226 let spacepos =
2227 try String.index cmds ' '
2228 with Not_found -> -1
2230 if spacepos = -1
2231 then cmds, ""
2232 else
2233 let l = String.length cmds in
2234 let op = String.sub cmds 0 spacepos in
2235 op, begin
2236 if l - spacepos < 2 then ""
2237 else String.sub cmds (spacepos+1) (l-spacepos-1)
2240 match op with
2241 | "clear" ->
2242 state.uioh#infochanged Pdim;
2243 state.pdims <- [];
2245 | "clearrects" ->
2246 state.rects <- state.rects1;
2247 G.postRedisplay "clearrects";
2249 | "continue" ->
2250 let n =
2251 try Scanf.sscanf args "%u" (fun n -> n)
2252 with exn ->
2253 dolog "error processing 'continue' %S: %s"
2254 cmds (Printexc.to_string exn);
2255 exit 1;
2257 state.pagecount <- n;
2258 begin match state.currently with
2259 | Outlining l ->
2260 state.currently <- Idle;
2261 state.outlines <- Array.of_list (List.rev l)
2262 | _ -> ()
2263 end;
2265 let cur, cmds = state.geomcmds in
2266 if String.length cur = 0
2267 then failwith "umpossible";
2269 begin match List.rev cmds with
2270 | [] ->
2271 state.geomcmds <- "", [];
2272 represent ();
2273 | (s, f) :: rest ->
2274 f ();
2275 state.geomcmds <- s, List.rev rest;
2276 end;
2277 if conf.maxwait = None
2278 then G.postRedisplay "continue";
2280 | "title" ->
2281 Wsi.settitle args
2283 | "msg" ->
2284 showtext ' ' args
2286 | "vmsg" ->
2287 if conf.verbose
2288 then showtext ' ' args
2290 | "emsg" ->
2291 Buffer.add_string state.errmsgs args;
2292 state.newerrmsgs <- true;
2293 G.postRedisplay "error message"
2295 | "progress" ->
2296 let progress, text =
2298 Scanf.sscanf args "%f %n"
2299 (fun f pos ->
2300 f, String.sub args pos (String.length args - pos))
2301 with exn ->
2302 dolog "error processing 'progress' %S: %s"
2303 cmds (Printexc.to_string exn);
2304 exit 1;
2306 state.text <- text;
2307 state.progress <- progress;
2308 G.postRedisplay "progress"
2310 | "firstmatch" ->
2311 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2313 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2314 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2315 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2316 with exn ->
2317 dolog "error processing 'firstmatch' %S: %s"
2318 cmds (Printexc.to_string exn);
2319 exit 1;
2321 let y = (getpagey pageno) + truncate y0 in
2322 addnav ();
2323 gotoy y;
2324 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2326 | "match" ->
2327 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2329 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2330 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2331 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2332 with exn ->
2333 dolog "error processing 'match' %S: %s"
2334 cmds (Printexc.to_string exn);
2335 exit 1;
2337 state.rects1 <-
2338 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2340 | "page" ->
2341 let pageopaque, t =
2343 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2344 with exn ->
2345 dolog "error processing 'page' %S: %s"
2346 cmds (Printexc.to_string exn);
2347 exit 1;
2349 begin match state.currently with
2350 | Loading (l, gen) ->
2351 vlog "page %d took %f sec" l.pageno t;
2352 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2353 begin match state.throttle with
2354 | None ->
2355 let preloadedpages =
2356 if conf.preload
2357 then preloadlayout state.y
2358 else state.layout
2360 let evict () =
2361 let module IntSet =
2362 Set.Make (struct type t = int let compare = (-) end) in
2363 let set =
2364 List.fold_left (fun s l -> IntSet.add l.pageno s)
2365 IntSet.empty preloadedpages
2367 let evictedpages =
2368 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2369 if not (IntSet.mem pageno set)
2370 then (
2371 wcmd "freepage %s" opaque;
2372 key :: accu
2374 else accu
2375 ) state.pagemap []
2377 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2379 evict ();
2380 state.currently <- Idle;
2381 if gen = state.gen
2382 then (
2383 tilepage l.pageno pageopaque state.layout;
2384 load state.layout;
2385 load preloadedpages;
2386 if pagevisible state.layout l.pageno
2387 && layoutready state.layout
2388 then G.postRedisplay "page";
2391 | Some (layout, _, _) ->
2392 state.currently <- Idle;
2393 tilepage l.pageno pageopaque layout;
2394 load state.layout
2395 end;
2397 | _ ->
2398 dolog "Inconsistent loading state";
2399 logcurrently state.currently;
2400 exit 1
2403 | "tile" ->
2404 let (x, y, opaque, size, t) =
2406 Scanf.sscanf args "%u %u %s %u %f"
2407 (fun x y p size t -> (x, y, p, size, t))
2408 with exn ->
2409 dolog "error processing 'tile' %S: %s"
2410 cmds (Printexc.to_string exn);
2411 exit 1;
2413 begin match state.currently with
2414 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2415 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2417 unmappbo opaque;
2418 if tilew != conf.tilew || tileh != conf.tileh
2419 then (
2420 wcmd "freetile %s" opaque;
2421 state.currently <- Idle;
2422 load state.layout;
2424 else (
2425 puttileopaque l col row gen cs angle opaque size t;
2426 state.memused <- state.memused + size;
2427 state.uioh#infochanged Memused;
2428 gctiles ();
2429 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2430 opaque, size) state.tilelru;
2432 let layout =
2433 match state.throttle with
2434 | None -> state.layout
2435 | Some (layout, _, _) -> layout
2438 state.currently <- Idle;
2439 if gen = state.gen
2440 && conf.colorspace = cs
2441 && conf.angle = angle
2442 && tilevisible layout l.pageno x y
2443 then conttiling l.pageno pageopaque;
2445 begin match state.throttle with
2446 | None ->
2447 state.wthack <- false;
2448 preload state.layout;
2449 if gen = state.gen
2450 && conf.colorspace = cs
2451 && conf.angle = angle
2452 && tilevisible state.layout l.pageno x y
2453 then G.postRedisplay "tile nothrottle";
2455 | Some (layout, y, _) ->
2456 let ready = layoutready layout in
2457 if ready
2458 then (
2459 state.wthack <- false;
2460 state.y <- y;
2461 state.layout <- layout;
2462 state.throttle <- None;
2463 G.postRedisplay "throttle";
2465 else load layout;
2466 end;
2469 | _ ->
2470 dolog "Inconsistent tiling state";
2471 logcurrently state.currently;
2472 exit 1
2475 | "pdim" ->
2476 let pdim =
2478 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2479 with exn ->
2480 dolog "error processing 'pdim' %S: %s"
2481 cmds (Printexc.to_string exn);
2482 exit 1;
2484 state.uioh#infochanged Pdim;
2485 state.pdims <- pdim :: state.pdims
2487 | "o" ->
2488 let (l, n, t, h, pos) =
2490 Scanf.sscanf args "%u %u %d %u %n"
2491 (fun l n t h pos -> l, n, t, h, pos)
2492 with exn ->
2493 dolog "error processing 'o' %S: %s"
2494 cmds (Printexc.to_string exn);
2495 exit 1;
2497 let s = String.sub args pos (String.length args - pos) in
2498 let outline = (s, l, (n, float t /. float h, 0.0)) in
2499 begin match state.currently with
2500 | Outlining outlines ->
2501 state.currently <- Outlining (outline :: outlines)
2502 | Idle ->
2503 state.currently <- Outlining [outline]
2504 | currently ->
2505 dolog "invalid outlining state";
2506 logcurrently currently
2509 | "a" ->
2510 let (n, t, h) =
2512 Scanf.sscanf args "%u %u %d"
2513 (fun n t h -> n, t, h)
2514 with exn ->
2515 dolog "error processing 'a' %S: %s"
2516 cmds (Printexc.to_string exn);
2517 exit 1;
2519 state.anchor <- (n, float t /. float h, 0.0)
2521 | "info" ->
2522 state.docinfo <- (1, args) :: state.docinfo
2524 | "infoend" ->
2525 state.uioh#infochanged Docinfo;
2526 state.docinfo <- List.rev state.docinfo
2528 | _ ->
2529 dolog "unknown cmd `%S'" cmds
2532 let onhist cb =
2533 let rc = cb.rc in
2534 let action = function
2535 | HCprev -> cbget cb ~-1
2536 | HCnext -> cbget cb 1
2537 | HCfirst -> cbget cb ~-(cb.rc)
2538 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2539 and cancel () = cb.rc <- rc
2540 in (action, cancel)
2543 let search pattern forward =
2544 if String.length pattern > 0
2545 then
2546 let pn, py =
2547 match state.layout with
2548 | [] -> 0, 0
2549 | l :: _ ->
2550 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2552 wcmd "search %d %d %d %d,%s\000"
2553 (btod conf.icase) pn py (btod forward) pattern;
2556 let intentry text key =
2557 let c =
2558 if key >= 32 && key < 127
2559 then Char.chr key
2560 else '\000'
2562 match c with
2563 | '0' .. '9' ->
2564 let text = addchar text c in
2565 TEcont text
2567 | _ ->
2568 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2569 TEcont text
2572 let linknentry text key =
2573 let c =
2574 if key >= 32 && key < 127
2575 then Char.chr key
2576 else '\000'
2578 match c with
2579 | 'a' .. 'z' ->
2580 let text = addchar text c in
2581 TEcont text
2583 | _ ->
2584 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2585 TEcont text
2588 let linkndone f s =
2589 if String.length s > 0
2590 then (
2591 let n =
2592 let l = String.length s in
2593 let rec loop pos n = if pos = l then n else
2594 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2595 loop (pos+1) (n*26 + m)
2596 in loop 0 0
2598 let rec loop n = function
2599 | [] -> ()
2600 | l :: rest ->
2601 match getopaque l.pageno with
2602 | None -> loop n rest
2603 | Some opaque ->
2604 let m = getlinkcount opaque in
2605 if n < m
2606 then (
2607 let under = getlink opaque n in
2608 f under
2610 else loop (n-m) rest
2612 loop n state.layout;
2616 let textentry text key =
2617 if key land 0xff00 = 0xff00
2618 then TEcont text
2619 else TEcont (text ^ Wsi.toutf8 key)
2622 let reqlayout angle proportional =
2623 match state.throttle with
2624 | None ->
2625 if nogeomcmds state.geomcmds
2626 then state.anchor <- getanchor ();
2627 conf.angle <- angle mod 360;
2628 if conf.angle != 0
2629 then (
2630 match state.mode with
2631 | LinkNav _ -> state.mode <- View
2632 | _ -> ()
2634 conf.proportional <- proportional;
2635 invalidate "reqlayout"
2636 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2637 | _ -> ()
2640 let settrim trimmargins trimfuzz =
2641 if nogeomcmds state.geomcmds
2642 then state.anchor <- getanchor ();
2643 conf.trimmargins <- trimmargins;
2644 conf.trimfuzz <- trimfuzz;
2645 let x0, y0, x1, y1 = trimfuzz in
2646 invalidate "settrim"
2647 (fun () ->
2648 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2649 flushpages ();
2652 let setzoom zoom =
2653 match state.throttle with
2654 | None ->
2655 let zoom = max 0.01 zoom in
2656 if zoom <> conf.zoom
2657 then (
2658 state.prevzoom <- conf.zoom;
2659 conf.zoom <- zoom;
2660 reshape conf.winw conf.winh;
2661 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2664 | Some (layout, y, started) ->
2665 let time =
2666 match conf.maxwait with
2667 | None -> 0.0
2668 | Some t -> t
2670 let dt = now () -. started in
2671 if dt > time
2672 then (
2673 state.y <- y;
2674 load layout;
2678 let setcolumns mode columns coverA coverB =
2679 state.prevcolumns <- Some (conf.columns, conf.zoom);
2680 if columns < 0
2681 then (
2682 if isbirdseye mode
2683 then showtext '!' "split mode doesn't work in bird's eye"
2684 else (
2685 conf.columns <- Csplit (-columns, [||]);
2686 state.x <- 0;
2687 conf.zoom <- 1.0;
2690 else (
2691 if columns < 2
2692 then (
2693 conf.columns <- Csingle [||];
2694 state.x <- 0;
2695 setzoom 1.0;
2697 else (
2698 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2699 conf.zoom <- 1.0;
2702 reshape conf.winw conf.winh;
2705 let enterbirdseye () =
2706 let zoom = float conf.thumbw /. float conf.winw in
2707 let birdseyepageno =
2708 let cy = conf.winh / 2 in
2709 let fold = function
2710 | [] -> 0
2711 | l :: rest ->
2712 let rec fold best = function
2713 | [] -> best.pageno
2714 | l :: rest ->
2715 let d = cy - (l.pagedispy + l.pagevh/2)
2716 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2717 if abs d < abs dbest
2718 then fold l rest
2719 else best.pageno
2720 in fold l rest
2722 fold state.layout
2724 state.mode <- Birdseye (
2725 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2727 conf.zoom <- zoom;
2728 conf.presentation <- false;
2729 conf.interpagespace <- 10;
2730 conf.hlinks <- false;
2731 state.x <- 0;
2732 state.mstate <- Mnone;
2733 conf.maxwait <- None;
2734 conf.columns <- (
2735 match conf.beyecolumns with
2736 | Some c ->
2737 conf.zoom <- 1.0;
2738 Cmulti ((c, 0, 0), [||])
2739 | None -> Csingle [||]
2741 Wsi.setcursor Wsi.CURSOR_INHERIT;
2742 if conf.verbose
2743 then
2744 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2745 (100.0*.zoom)
2746 else
2747 state.text <- ""
2749 reshape conf.winw conf.winh;
2752 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2753 state.mode <- View;
2754 conf.zoom <- c.zoom;
2755 conf.presentation <- c.presentation;
2756 conf.interpagespace <- c.interpagespace;
2757 conf.maxwait <- c.maxwait;
2758 conf.hlinks <- c.hlinks;
2759 conf.beyecolumns <- (
2760 match conf.columns with
2761 | Cmulti ((c, _, _), _) -> Some c
2762 | Csingle _ -> None
2763 | Csplit _ -> failwith "leaving bird's eye split mode"
2765 conf.columns <- (
2766 match c.columns with
2767 | Cmulti (c, _) -> Cmulti (c, [||])
2768 | Csingle _ -> Csingle [||]
2769 | Csplit (c, _) -> Csplit (c, [||])
2771 state.x <- leftx;
2772 if conf.verbose
2773 then
2774 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2775 (100.0*.conf.zoom)
2777 reshape conf.winw conf.winh;
2778 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2781 let togglebirdseye () =
2782 match state.mode with
2783 | Birdseye vals -> leavebirdseye vals true
2784 | View -> enterbirdseye ()
2785 | _ -> ()
2788 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2789 let pageno = max 0 (pageno - incr) in
2790 let rec loop = function
2791 | [] -> gotopage1 pageno 0
2792 | l :: _ when l.pageno = pageno ->
2793 if l.pagedispy >= 0 && l.pagey = 0
2794 then G.postRedisplay "upbirdseye"
2795 else gotopage1 pageno 0
2796 | _ :: rest -> loop rest
2798 loop state.layout;
2799 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2802 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2803 let pageno = min (state.pagecount - 1) (pageno + incr) in
2804 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2805 let rec loop = function
2806 | [] ->
2807 let y, h = getpageyh pageno in
2808 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2809 gotoy (clamp dy)
2810 | l :: _ when l.pageno = pageno ->
2811 if l.pagevh != l.pageh
2812 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2813 else G.postRedisplay "downbirdseye"
2814 | _ :: rest -> loop rest
2816 loop state.layout
2819 let optentry mode _ key =
2820 let btos b = if b then "on" else "off" in
2821 if key >= 32 && key < 127
2822 then
2823 let c = Char.chr key in
2824 match c with
2825 | 's' ->
2826 let ondone s =
2827 try conf.scrollstep <- int_of_string s with exc ->
2828 state.text <- Printf.sprintf "bad integer `%s': %s"
2829 s (Printexc.to_string exc)
2831 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2833 | 'A' ->
2834 let ondone s =
2836 conf.autoscrollstep <- int_of_string s;
2837 if state.autoscroll <> None
2838 then state.autoscroll <- Some conf.autoscrollstep
2839 with exc ->
2840 state.text <- Printf.sprintf "bad integer `%s': %s"
2841 s (Printexc.to_string exc)
2843 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2845 | 'C' ->
2846 let ondone s =
2848 let n, a, b = multicolumns_of_string s in
2849 setcolumns mode n a b;
2850 with exc ->
2851 state.text <- Printf.sprintf "bad columns `%s': %s"
2852 s (Printexc.to_string exc)
2854 TEswitch ("columns: ", "", None, textentry, ondone, true)
2856 | 'Z' ->
2857 let ondone s =
2859 let zoom = float (int_of_string s) /. 100.0 in
2860 setzoom zoom
2861 with exc ->
2862 state.text <- Printf.sprintf "bad integer `%s': %s"
2863 s (Printexc.to_string exc)
2865 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2867 | 't' ->
2868 let ondone s =
2870 conf.thumbw <- bound (int_of_string s) 2 4096;
2871 state.text <-
2872 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2873 begin match mode with
2874 | Birdseye beye ->
2875 leavebirdseye beye false;
2876 enterbirdseye ();
2877 | _ -> ();
2879 with exc ->
2880 state.text <- Printf.sprintf "bad integer `%s': %s"
2881 s (Printexc.to_string exc)
2883 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2885 | 'R' ->
2886 let ondone s =
2887 match try
2888 Some (int_of_string s)
2889 with exc ->
2890 state.text <- Printf.sprintf "bad integer `%s': %s"
2891 s (Printexc.to_string exc);
2892 None
2893 with
2894 | Some angle -> reqlayout angle conf.proportional
2895 | None -> ()
2897 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2899 | 'i' ->
2900 conf.icase <- not conf.icase;
2901 TEdone ("case insensitive search " ^ (btos conf.icase))
2903 | 'p' ->
2904 conf.preload <- not conf.preload;
2905 gotoy state.y;
2906 TEdone ("preload " ^ (btos conf.preload))
2908 | 'v' ->
2909 conf.verbose <- not conf.verbose;
2910 TEdone ("verbose " ^ (btos conf.verbose))
2912 | 'd' ->
2913 conf.debug <- not conf.debug;
2914 TEdone ("debug " ^ (btos conf.debug))
2916 | 'h' ->
2917 conf.maxhfit <- not conf.maxhfit;
2918 state.maxy <- calcheight ();
2919 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2921 | 'c' ->
2922 conf.crophack <- not conf.crophack;
2923 TEdone ("crophack " ^ btos conf.crophack)
2925 | 'a' ->
2926 let s =
2927 match conf.maxwait with
2928 | None ->
2929 conf.maxwait <- Some infinity;
2930 "always wait for page to complete"
2931 | Some _ ->
2932 conf.maxwait <- None;
2933 "show placeholder if page is not ready"
2935 TEdone s
2937 | 'f' ->
2938 conf.underinfo <- not conf.underinfo;
2939 TEdone ("underinfo " ^ btos conf.underinfo)
2941 | 'P' ->
2942 conf.savebmarks <- not conf.savebmarks;
2943 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2945 | 'S' ->
2946 let ondone s =
2948 let pageno, py =
2949 match state.layout with
2950 | [] -> 0, 0
2951 | l :: _ ->
2952 l.pageno, l.pagey
2954 conf.interpagespace <- int_of_string s;
2955 docolumns conf.columns;
2956 state.maxy <- calcheight ();
2957 let y = getpagey pageno in
2958 gotoy (y + py)
2959 with exc ->
2960 state.text <- Printf.sprintf "bad integer `%s': %s"
2961 s (Printexc.to_string exc)
2963 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2965 | 'l' ->
2966 reqlayout conf.angle (not conf.proportional);
2967 TEdone ("proportional display " ^ btos conf.proportional)
2969 | 'T' ->
2970 settrim (not conf.trimmargins) conf.trimfuzz;
2971 TEdone ("trim margins " ^ btos conf.trimmargins)
2973 | 'I' ->
2974 conf.invert <- not conf.invert;
2975 TEdone ("invert colors " ^ btos conf.invert)
2977 | 'x' ->
2978 let ondone s =
2979 cbput state.hists.sel s;
2980 conf.selcmd <- s;
2982 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2983 textentry, ondone, true)
2985 | _ ->
2986 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2987 TEstop
2988 else
2989 TEcont state.text
2992 class type lvsource = object
2993 method getitemcount : int
2994 method getitem : int -> (string * int)
2995 method hasaction : int -> bool
2996 method exit :
2997 uioh:uioh ->
2998 cancel:bool ->
2999 active:int ->
3000 first:int ->
3001 pan:int ->
3002 qsearch:string ->
3003 uioh option
3004 method getactive : int
3005 method getfirst : int
3006 method getqsearch : string
3007 method setqsearch : string -> unit
3008 method getpan : int
3009 end;;
3011 class virtual lvsourcebase = object
3012 val mutable m_active = 0
3013 val mutable m_first = 0
3014 val mutable m_qsearch = ""
3015 val mutable m_pan = 0
3016 method getactive = m_active
3017 method getfirst = m_first
3018 method getqsearch = m_qsearch
3019 method getpan = m_pan
3020 method setqsearch s = m_qsearch <- s
3021 end;;
3023 let withoutlastutf8 s =
3024 let len = String.length s in
3025 if len = 0
3026 then s
3027 else
3028 let rec find pos =
3029 if pos = 0
3030 then pos
3031 else
3032 let b = Char.code s.[pos] in
3033 if b land 0b11000000 = 0b11000000
3034 then pos
3035 else find (pos-1)
3037 let first =
3038 if Char.code s.[len-1] land 0x80 = 0
3039 then len-1
3040 else find (len-1)
3042 String.sub s 0 first;
3045 let textentrykeyboard
3046 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3047 let key =
3048 if key >= 0xffb0 && key <= 0xffb9
3049 then key - 0xffb0 + 48 else key
3051 let enttext te =
3052 state.mode <- Textentry (te, onleave);
3053 state.text <- "";
3054 enttext ();
3055 G.postRedisplay "textentrykeyboard enttext";
3057 let histaction cmd =
3058 match opthist with
3059 | None -> ()
3060 | Some (action, _) ->
3061 state.mode <- Textentry (
3062 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3064 G.postRedisplay "textentry histaction"
3066 match key with
3067 | 0xff08 -> (* backspace *)
3068 let s = withoutlastutf8 text in
3069 let len = String.length s in
3070 if cancelonempty && len = 0
3071 then (
3072 onleave Cancel;
3073 G.postRedisplay "textentrykeyboard after cancel";
3075 else (
3076 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3079 | 0xff0d | 0xff8d -> (* (kp) enter *)
3080 ondone text;
3081 onleave Confirm;
3082 G.postRedisplay "textentrykeyboard after confirm"
3084 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3085 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3086 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3087 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3089 | 0xff1b -> (* escape*)
3090 if String.length text = 0
3091 then (
3092 begin match opthist with
3093 | None -> ()
3094 | Some (_, onhistcancel) -> onhistcancel ()
3095 end;
3096 onleave Cancel;
3097 state.text <- "";
3098 G.postRedisplay "textentrykeyboard after cancel2"
3100 else (
3101 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3104 | 0xff9f | 0xffff -> () (* delete *)
3106 | _ when key != 0
3107 && key land 0xff00 != 0xff00 (* keyboard *)
3108 && key land 0xfe00 != 0xfe00 (* xkb *)
3109 && key land 0xfd00 != 0xfd00 (* 3270 *)
3111 begin match onkey text key with
3112 | TEdone text ->
3113 ondone text;
3114 onleave Confirm;
3115 G.postRedisplay "textentrykeyboard after confirm2";
3117 | TEcont text ->
3118 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3120 | TEstop ->
3121 onleave Cancel;
3122 G.postRedisplay "textentrykeyboard after cancel3"
3124 | TEswitch te ->
3125 state.mode <- Textentry (te, onleave);
3126 G.postRedisplay "textentrykeyboard switch";
3127 end;
3129 | _ ->
3130 vlog "unhandled key %s" (Wsi.keyname key)
3133 let firstof first active =
3134 if first > active || abs (first - active) > fstate.maxrows - 1
3135 then max 0 (active - (fstate.maxrows/2))
3136 else first
3139 let calcfirst first active =
3140 if active > first
3141 then
3142 let rows = active - first in
3143 if rows > fstate.maxrows then active - fstate.maxrows else first
3144 else active
3147 let scrollph y maxy =
3148 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3149 let sh = float conf.winh /. sh in
3150 let sh = max sh (float conf.scrollh) in
3152 let percent =
3153 if y = state.maxy
3154 then 1.0
3155 else float y /. float maxy
3157 let position = (float conf.winh -. sh) *. percent in
3159 let position =
3160 if position +. sh > float conf.winh
3161 then float conf.winh -. sh
3162 else position
3164 position, sh;
3167 let coe s = (s :> uioh);;
3169 class listview ~(source:lvsource) ~trusted ~modehash =
3170 object (self)
3171 val m_pan = source#getpan
3172 val m_first = source#getfirst
3173 val m_active = source#getactive
3174 val m_qsearch = source#getqsearch
3175 val m_prev_uioh = state.uioh
3177 method private elemunder y =
3178 let n = y / (fstate.fontsize+1) in
3179 if m_first + n < source#getitemcount
3180 then (
3181 if source#hasaction (m_first + n)
3182 then Some (m_first + n)
3183 else None
3185 else None
3187 method display =
3188 Gl.enable `blend;
3189 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3190 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3191 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3192 GlDraw.color (1., 1., 1.);
3193 Gl.enable `texture_2d;
3194 let fs = fstate.fontsize in
3195 let nfs = fs + 1 in
3196 let ww = fstate.wwidth in
3197 let tabw = 30.0*.ww in
3198 let itemcount = source#getitemcount in
3199 let rec loop row =
3200 if (row - m_first) > fstate.maxrows
3201 then ()
3202 else (
3203 if row >= 0 && row < itemcount
3204 then (
3205 let (s, level) = source#getitem row in
3206 let y = (row - m_first) * nfs in
3207 let x = 5.0 +. float (level + m_pan) *. ww in
3208 if row = m_active
3209 then (
3210 Gl.disable `texture_2d;
3211 GlDraw.polygon_mode `both `line;
3212 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3213 GlDraw.rect (1., float (y + 1))
3214 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3215 GlDraw.polygon_mode `both `fill;
3216 GlDraw.color (1., 1., 1.);
3217 Gl.enable `texture_2d;
3220 let drawtabularstring s =
3221 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3222 if trusted
3223 then
3224 let tabpos = try String.index s '\t' with Not_found -> -1 in
3225 if tabpos > 0
3226 then
3227 let len = String.length s - tabpos - 1 in
3228 let s1 = String.sub s 0 tabpos
3229 and s2 = String.sub s (tabpos + 1) len in
3230 let nx = drawstr x s1 in
3231 let sw = nx -. x in
3232 let x = x +. (max tabw sw) in
3233 drawstr x s2
3234 else
3235 drawstr x s
3236 else
3237 drawstr x s
3239 let _ = drawtabularstring s in
3240 loop (row+1)
3244 loop m_first;
3245 Gl.disable `blend;
3246 Gl.disable `texture_2d;
3248 method updownlevel incr =
3249 let len = source#getitemcount in
3250 let curlevel =
3251 if m_active >= 0 && m_active < len
3252 then snd (source#getitem m_active)
3253 else -1
3255 let rec flow i =
3256 if i = len then i-1 else if i = -1 then 0 else
3257 let _, l = source#getitem i in
3258 if l != curlevel then i else flow (i+incr)
3260 let active = flow m_active in
3261 let first = calcfirst m_first active in
3262 G.postRedisplay "outline updownlevel";
3263 {< m_active = active; m_first = first >}
3265 method private key1 key mask =
3266 let set1 active first qsearch =
3267 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3269 let search active pattern incr =
3270 let dosearch re =
3271 let rec loop n =
3272 if n >= 0 && n < source#getitemcount
3273 then (
3274 let s, _ = source#getitem n in
3276 (try ignore (Str.search_forward re s 0); true
3277 with Not_found -> false)
3278 then Some n
3279 else loop (n + incr)
3281 else None
3283 loop active
3286 let re = Str.regexp_case_fold pattern in
3287 dosearch re
3288 with Failure s ->
3289 state.text <- s;
3290 None
3292 let itemcount = source#getitemcount in
3293 let find start incr =
3294 let rec find i =
3295 if i = -1 || i = itemcount
3296 then -1
3297 else (
3298 if source#hasaction i
3299 then i
3300 else find (i + incr)
3303 find start
3305 let set active first =
3306 let first = bound first 0 (itemcount - fstate.maxrows) in
3307 state.text <- "";
3308 coe {< m_active = active; m_first = first >}
3310 let navigate incr =
3311 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3312 let active, first =
3313 let incr1 = if incr > 0 then 1 else -1 in
3314 if isvisible m_first m_active
3315 then
3316 let next =
3317 let next = m_active + incr in
3318 let next =
3319 if next < 0 || next >= itemcount
3320 then -1
3321 else find next incr1
3323 if next = -1 || abs (m_active - next) > fstate.maxrows
3324 then -1
3325 else next
3327 if next = -1
3328 then
3329 let first = m_first + incr in
3330 let first = bound first 0 (itemcount - 1) in
3331 let next =
3332 let next = m_active + incr in
3333 let next = bound next 0 (itemcount - 1) in
3334 find next ~-incr1
3336 let active = if next = -1 then m_active else next in
3337 active, first
3338 else
3339 let first = min next m_first in
3340 let first =
3341 if abs (next - first) > fstate.maxrows
3342 then first + incr
3343 else first
3345 next, first
3346 else
3347 let first = m_first + incr in
3348 let first = bound first 0 (itemcount - 1) in
3349 let active =
3350 let next = m_active + incr in
3351 let next = bound next 0 (itemcount - 1) in
3352 let next = find next incr1 in
3353 let active =
3354 if next = -1 || abs (m_active - first) > fstate.maxrows
3355 then (
3356 let active = if m_active = -1 then next else m_active in
3357 active
3359 else next
3361 if isvisible first active
3362 then active
3363 else -1
3365 active, first
3367 G.postRedisplay "listview navigate";
3368 set active first;
3370 match key with
3371 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3372 let incr = if key = 0x72 then -1 else 1 in
3373 let active, first =
3374 match search (m_active + incr) m_qsearch incr with
3375 | None ->
3376 state.text <- m_qsearch ^ " [not found]";
3377 m_active, m_first
3378 | Some active ->
3379 state.text <- m_qsearch;
3380 active, firstof m_first active
3382 G.postRedisplay "listview ctrl-r/s";
3383 set1 active first m_qsearch;
3385 | 0xff08 -> (* backspace *)
3386 if String.length m_qsearch = 0
3387 then coe self
3388 else (
3389 let qsearch = withoutlastutf8 m_qsearch in
3390 let len = String.length qsearch in
3391 if len = 0
3392 then (
3393 state.text <- "";
3394 G.postRedisplay "listview empty qsearch";
3395 set1 m_active m_first "";
3397 else
3398 let active, first =
3399 match search m_active qsearch ~-1 with
3400 | None ->
3401 state.text <- qsearch ^ " [not found]";
3402 m_active, m_first
3403 | Some active ->
3404 state.text <- qsearch;
3405 active, firstof m_first active
3407 G.postRedisplay "listview backspace qsearch";
3408 set1 active first qsearch
3411 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3412 let pattern = m_qsearch ^ Wsi.toutf8 key in
3413 let active, first =
3414 match search m_active pattern 1 with
3415 | None ->
3416 state.text <- pattern ^ " [not found]";
3417 m_active, m_first
3418 | Some active ->
3419 state.text <- pattern;
3420 active, firstof m_first active
3422 G.postRedisplay "listview qsearch add";
3423 set1 active first pattern;
3425 | 0xff1b -> (* escape *)
3426 state.text <- "";
3427 if String.length m_qsearch = 0
3428 then (
3429 G.postRedisplay "list view escape";
3430 begin
3431 match
3432 source#exit (coe self) true m_active m_first m_pan m_qsearch
3433 with
3434 | None -> m_prev_uioh
3435 | Some uioh -> uioh
3438 else (
3439 G.postRedisplay "list view kill qsearch";
3440 source#setqsearch "";
3441 coe {< m_qsearch = "" >}
3444 | 0xff0d | 0xff8d -> (* (kp) enter *)
3445 state.text <- "";
3446 let self = {< m_qsearch = "" >} in
3447 source#setqsearch "";
3448 let opt =
3449 G.postRedisplay "listview enter";
3450 if m_active >= 0 && m_active < source#getitemcount
3451 then (
3452 source#exit (coe self) false m_active m_first m_pan "";
3454 else (
3455 source#exit (coe self) true m_active m_first m_pan "";
3458 begin match opt with
3459 | None -> m_prev_uioh
3460 | Some uioh -> uioh
3463 | 0xff9f | 0xffff -> (* (kp) delete *)
3464 coe self
3466 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3467 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3468 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3469 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3471 | 0xff53 | 0xff98 -> (* (kp) right *)
3472 state.text <- "";
3473 G.postRedisplay "listview right";
3474 coe {< m_pan = m_pan - 1 >}
3476 | 0xff51 | 0xff96 -> (* (kp) left *)
3477 state.text <- "";
3478 G.postRedisplay "listview left";
3479 coe {< m_pan = m_pan + 1 >}
3481 | 0xff50 | 0xff95 -> (* (kp) home *)
3482 let active = find 0 1 in
3483 G.postRedisplay "listview home";
3484 set active 0;
3486 | 0xff57 | 0xff9c -> (* (kp) end *)
3487 let first = max 0 (itemcount - fstate.maxrows) in
3488 let active = find (itemcount - 1) ~-1 in
3489 G.postRedisplay "listview end";
3490 set active first;
3492 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3493 coe self
3495 | _ ->
3496 dolog "listview unknown key %#x" key; coe self
3498 method key key mask =
3499 match state.mode with
3500 | Textentry te -> textentrykeyboard key mask te; coe self
3501 | _ -> self#key1 key mask
3503 method button button down x y _ =
3504 let opt =
3505 match button with
3506 | 1 when x > conf.winw - conf.scrollbw ->
3507 G.postRedisplay "listview scroll";
3508 if down
3509 then
3510 let _, position, sh = self#scrollph in
3511 if y > truncate position && y < truncate (position +. sh)
3512 then (
3513 state.mstate <- Mscrolly;
3514 Some (coe self)
3516 else
3517 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3518 let first = truncate (s *. float source#getitemcount) in
3519 let first = min source#getitemcount first in
3520 Some (coe {< m_first = first; m_active = first >})
3521 else (
3522 state.mstate <- Mnone;
3523 Some (coe self);
3525 | 1 when not down ->
3526 begin match self#elemunder y with
3527 | Some n ->
3528 G.postRedisplay "listview click";
3529 source#exit
3530 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3531 | _ ->
3532 Some (coe self)
3534 | n when (n == 4 || n == 5) && not down ->
3535 let len = source#getitemcount in
3536 let first =
3537 if n = 5 && m_first + fstate.maxrows >= len
3538 then
3539 m_first
3540 else
3541 let first = m_first + (if n == 4 then -1 else 1) in
3542 bound first 0 (len - 1)
3544 G.postRedisplay "listview wheel";
3545 Some (coe {< m_first = first >})
3546 | n when (n = 6 || n = 7) && not down ->
3547 let inc = m_first + (if n = 7 then -1 else 1) in
3548 G.postRedisplay "listview hwheel";
3549 Some (coe {< m_pan = m_pan + inc >})
3550 | _ ->
3551 Some (coe self)
3553 match opt with
3554 | None -> m_prev_uioh
3555 | Some uioh -> uioh
3557 method motion _ y =
3558 match state.mstate with
3559 | Mscrolly ->
3560 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3561 let first = truncate (s *. float source#getitemcount) in
3562 let first = min source#getitemcount first in
3563 G.postRedisplay "listview motion";
3564 coe {< m_first = first; m_active = first >}
3565 | _ -> coe self
3567 method pmotion x y =
3568 if x < conf.winw - conf.scrollbw
3569 then
3570 let n =
3571 match self#elemunder y with
3572 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3573 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3575 let o =
3576 if n != m_active
3577 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3578 else self
3580 coe o
3581 else (
3582 Wsi.setcursor Wsi.CURSOR_INHERIT;
3583 coe self
3586 method infochanged _ = ()
3588 method scrollpw = (0, 0.0, 0.0)
3589 method scrollph =
3590 let nfs = fstate.fontsize + 1 in
3591 let y = m_first * nfs in
3592 let itemcount = source#getitemcount in
3593 let maxi = max 0 (itemcount - fstate.maxrows) in
3594 let maxy = maxi * nfs in
3595 let p, h = scrollph y maxy in
3596 conf.scrollbw, p, h
3598 method modehash = modehash
3599 end;;
3601 class outlinelistview ~source =
3602 object (self)
3603 inherit listview
3604 ~source:(source :> lvsource)
3605 ~trusted:false
3606 ~modehash:(findkeyhash conf "outline")
3607 as super
3609 method key key mask =
3610 let calcfirst first active =
3611 if active > first
3612 then
3613 let rows = active - first in
3614 let maxrows =
3615 if String.length state.text = 0
3616 then fstate.maxrows
3617 else fstate.maxrows - 2
3619 if rows > maxrows then active - maxrows else first
3620 else active
3622 let navigate incr =
3623 let active = m_active + incr in
3624 let active = bound active 0 (source#getitemcount - 1) in
3625 let first = calcfirst m_first active in
3626 G.postRedisplay "outline navigate";
3627 coe {< m_active = active; m_first = first >}
3629 let ctrl = Wsi.withctrl mask in
3630 match key with
3631 | 110 when ctrl -> (* ctrl-n *)
3632 source#narrow m_qsearch;
3633 G.postRedisplay "outline ctrl-n";
3634 coe {< m_first = 0; m_active = 0 >}
3636 | 117 when ctrl -> (* ctrl-u *)
3637 source#denarrow;
3638 G.postRedisplay "outline ctrl-u";
3639 state.text <- "";
3640 coe {< m_first = 0; m_active = 0 >}
3642 | 108 when ctrl -> (* ctrl-l *)
3643 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3644 G.postRedisplay "outline ctrl-l";
3645 coe {< m_first = first >}
3647 | 0xff9f | 0xffff -> (* (kp) delete *)
3648 source#remove m_active;
3649 G.postRedisplay "outline delete";
3650 let active = max 0 (m_active-1) in
3651 coe {< m_first = firstof m_first active;
3652 m_active = active >}
3654 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3655 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3656 | 0xff55 | 0xff9a -> (* (kp) prior *)
3657 navigate ~-(fstate.maxrows)
3658 | 0xff56 | 0xff9b -> (* (kp) next *)
3659 navigate fstate.maxrows
3661 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3662 let o =
3663 if ctrl
3664 then (
3665 G.postRedisplay "outline ctrl right";
3666 {< m_pan = m_pan + 1 >}
3668 else self#updownlevel 1
3670 coe o
3672 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3673 let o =
3674 if ctrl
3675 then (
3676 G.postRedisplay "outline ctrl left";
3677 {< m_pan = m_pan - 1 >}
3679 else self#updownlevel ~-1
3681 coe o
3683 | 0xff50 | 0xff95 -> (* (kp) home *)
3684 G.postRedisplay "outline home";
3685 coe {< m_first = 0; m_active = 0 >}
3687 | 0xff57 | 0xff9c -> (* (kp) end *)
3688 let active = source#getitemcount - 1 in
3689 let first = max 0 (active - fstate.maxrows) in
3690 G.postRedisplay "outline end";
3691 coe {< m_active = active; m_first = first >}
3693 | _ -> super#key key mask
3696 let outlinesource usebookmarks =
3697 let empty = [||] in
3698 (object
3699 inherit lvsourcebase
3700 val mutable m_items = empty
3701 val mutable m_orig_items = empty
3702 val mutable m_prev_items = empty
3703 val mutable m_narrow_pattern = ""
3704 val mutable m_hadremovals = false
3706 method getitemcount =
3707 Array.length m_items + (if m_hadremovals then 1 else 0)
3709 method getitem n =
3710 if n == Array.length m_items && m_hadremovals
3711 then
3712 ("[Confirm removal]", 0)
3713 else
3714 let s, n, _ = m_items.(n) in
3715 (s, n)
3717 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3718 ignore (uioh, first, qsearch);
3719 let confrimremoval = m_hadremovals && active = Array.length m_items in
3720 let items =
3721 if String.length m_narrow_pattern = 0
3722 then m_orig_items
3723 else m_items
3725 if not cancel
3726 then (
3727 if not confrimremoval
3728 then(
3729 let _, _, anchor = m_items.(active) in
3730 gotoghyll (getanchory anchor);
3731 m_items <- items;
3733 else (
3734 state.bookmarks <- Array.to_list m_items;
3735 m_orig_items <- m_items;
3738 else m_items <- items;
3739 m_pan <- pan;
3740 None
3742 method hasaction _ = true
3744 method greetmsg =
3745 if Array.length m_items != Array.length m_orig_items
3746 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3747 else ""
3749 method narrow pattern =
3750 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3751 match reopt with
3752 | None -> ()
3753 | Some re ->
3754 let rec loop accu n =
3755 if n = -1
3756 then (
3757 m_narrow_pattern <- pattern;
3758 m_items <- Array.of_list accu
3760 else
3761 let (s, _, _) as o = m_items.(n) in
3762 let accu =
3763 if (try ignore (Str.search_forward re s 0); true
3764 with Not_found -> false)
3765 then o :: accu
3766 else accu
3768 loop accu (n-1)
3770 loop [] (Array.length m_items - 1)
3772 method denarrow =
3773 m_orig_items <- (
3774 if usebookmarks
3775 then Array.of_list state.bookmarks
3776 else state.outlines
3778 m_items <- m_orig_items
3780 method remove m =
3781 if usebookmarks
3782 then
3783 if m >= 0 && m < Array.length m_items
3784 then (
3785 m_hadremovals <- true;
3786 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3787 let n = if n >= m then n+1 else n in
3788 m_items.(n)
3792 method reset anchor items =
3793 m_hadremovals <- false;
3794 if m_orig_items == empty || m_prev_items != items
3795 then (
3796 m_orig_items <- items;
3797 if String.length m_narrow_pattern = 0
3798 then m_items <- items;
3800 m_prev_items <- items;
3801 let rely = getanchory anchor in
3802 let active =
3803 let rec loop n best bestd =
3804 if n = Array.length m_items
3805 then best
3806 else
3807 let (_, _, anchor) = m_items.(n) in
3808 let orely = getanchory anchor in
3809 let d = abs (orely - rely) in
3810 if d < bestd
3811 then loop (n+1) n d
3812 else loop (n+1) best bestd
3814 loop 0 ~-1 max_int
3816 m_active <- active;
3817 m_first <- firstof m_first active
3818 end)
3821 let enterselector usebookmarks =
3822 let source = outlinesource usebookmarks in
3823 fun errmsg ->
3824 let outlines =
3825 if usebookmarks
3826 then Array.of_list state.bookmarks
3827 else state.outlines
3829 if Array.length outlines = 0
3830 then (
3831 showtext ' ' errmsg;
3833 else (
3834 state.text <- source#greetmsg;
3835 Wsi.setcursor Wsi.CURSOR_INHERIT;
3836 let anchor = getanchor () in
3837 source#reset anchor outlines;
3838 state.uioh <- coe (new outlinelistview ~source);
3839 G.postRedisplay "enter selector";
3843 let enteroutlinemode =
3844 let f = enterselector false in
3845 fun ()-> f "Document has no outline";
3848 let enterbookmarkmode =
3849 let f = enterselector true in
3850 fun () -> f "Document has no bookmarks (yet)";
3853 let color_of_string s =
3854 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3855 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3859 let color_to_string (r, g, b) =
3860 let r = truncate (r *. 256.0)
3861 and g = truncate (g *. 256.0)
3862 and b = truncate (b *. 256.0) in
3863 Printf.sprintf "%d/%d/%d" r g b
3866 let irect_of_string s =
3867 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3870 let irect_to_string (x0,y0,x1,y1) =
3871 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3874 let makecheckers () =
3875 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3876 following to say:
3877 converted by Issac Trotts. July 25, 2002 *)
3878 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3879 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3880 let id = GlTex.gen_texture () in
3881 GlTex.bind_texture `texture_2d id;
3882 GlPix.store (`unpack_alignment 1);
3883 GlTex.image2d image;
3884 List.iter (GlTex.parameter ~target:`texture_2d)
3885 [ `wrap_s `repeat;
3886 `wrap_t `repeat;
3887 `mag_filter `nearest;
3888 `min_filter `nearest ];
3892 let setcheckers enabled =
3893 match state.texid with
3894 | None ->
3895 if enabled then state.texid <- Some (makecheckers ())
3897 | Some texid ->
3898 if not enabled
3899 then (
3900 GlTex.delete_texture texid;
3901 state.texid <- None;
3905 let int_of_string_with_suffix s =
3906 let l = String.length s in
3907 let s1, shift =
3908 if l > 1
3909 then
3910 let suffix = Char.lowercase s.[l-1] in
3911 match suffix with
3912 | 'k' -> String.sub s 0 (l-1), 10
3913 | 'm' -> String.sub s 0 (l-1), 20
3914 | 'g' -> String.sub s 0 (l-1), 30
3915 | _ -> s, 0
3916 else s, 0
3918 let n = int_of_string s1 in
3919 let m = n lsl shift in
3920 if m < 0 || m < n
3921 then raise (Failure "value too large")
3922 else m
3925 let string_with_suffix_of_int n =
3926 if n = 0
3927 then "0"
3928 else
3929 let n, s =
3930 if n land ((1 lsl 30) - 1) = 0
3931 then n lsr 30, "G"
3932 else (
3933 if n land ((1 lsl 20) - 1) = 0
3934 then n lsr 20, "M"
3935 else (
3936 if n land ((1 lsl 10) - 1) = 0
3937 then n lsr 10, "K"
3938 else n, ""
3942 let rec loop s n =
3943 let h = n mod 1000 in
3944 let n = n / 1000 in
3945 if n = 0
3946 then string_of_int h ^ s
3947 else (
3948 let s = Printf.sprintf "_%03d%s" h s in
3949 loop s n
3952 loop "" n ^ s;
3955 let defghyllscroll = (40, 8, 32);;
3956 let ghyllscroll_of_string s =
3957 let (n, a, b) as nab =
3958 if s = "default"
3959 then defghyllscroll
3960 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3962 if n <= a || n <= b || a >= b
3963 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3964 nab;
3967 let ghyllscroll_to_string ((n, a, b) as nab) =
3968 if nab = defghyllscroll
3969 then "default"
3970 else Printf.sprintf "%d,%d,%d" n a b;
3973 let describe_location () =
3974 let f (fn, _) l =
3975 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3977 let fn, ln = List.fold_left f (-1, -1) state.layout in
3978 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3979 let percent =
3980 if maxy <= 0
3981 then 100.
3982 else (100. *. (float state.y /. float maxy))
3984 if fn = ln
3985 then
3986 Printf.sprintf "page %d of %d [%.2f%%]"
3987 (fn+1) state.pagecount percent
3988 else
3989 Printf.sprintf
3990 "pages %d-%d of %d [%.2f%%]"
3991 (fn+1) (ln+1) state.pagecount percent
3994 let setpresentationmode v =
3995 let (n, _, _) = getanchor () in
3996 let _, h = getpageyh n in
3997 let ips = if conf.presentation then calcips h else conf.interpagespace in
3998 state.anchor <- (n, 0.0, float ips);
3999 conf.presentation <- v;
4000 if conf.presentation
4001 then (
4002 if not conf.scrollbarinpm
4003 then state.scrollw <- 0;
4005 else state.scrollw <- conf.scrollbw;
4006 represent ();
4009 let enterinfomode =
4010 let btos b = if b then "\xe2\x88\x9a" else "" in
4011 let showextended = ref false in
4012 let leave mode = function
4013 | Confirm -> state.mode <- mode
4014 | Cancel -> state.mode <- mode in
4015 let src =
4016 (object
4017 val mutable m_first_time = true
4018 val mutable m_l = []
4019 val mutable m_a = [||]
4020 val mutable m_prev_uioh = nouioh
4021 val mutable m_prev_mode = View
4023 inherit lvsourcebase
4025 method reset prev_mode prev_uioh =
4026 m_a <- Array.of_list (List.rev m_l);
4027 m_l <- [];
4028 m_prev_mode <- prev_mode;
4029 m_prev_uioh <- prev_uioh;
4030 if m_first_time
4031 then (
4032 let rec loop n =
4033 if n >= Array.length m_a
4034 then ()
4035 else
4036 match m_a.(n) with
4037 | _, _, _, Action _ -> m_active <- n
4038 | _ -> loop (n+1)
4040 loop 0;
4041 m_first_time <- false;
4044 method int name get set =
4045 m_l <-
4046 (name, `int get, 1, Action (
4047 fun u ->
4048 let ondone s =
4049 try set (int_of_string s)
4050 with exn ->
4051 state.text <- Printf.sprintf "bad integer `%s': %s"
4052 s (Printexc.to_string exn)
4054 state.text <- "";
4055 let te = name ^ ": ", "", None, intentry, ondone, true in
4056 state.mode <- Textentry (te, leave m_prev_mode);
4058 )) :: m_l
4060 method int_with_suffix name get set =
4061 m_l <-
4062 (name, `intws get, 1, Action (
4063 fun u ->
4064 let ondone s =
4065 try set (int_of_string_with_suffix s)
4066 with exn ->
4067 state.text <- Printf.sprintf "bad integer `%s': %s"
4068 s (Printexc.to_string exn)
4070 state.text <- "";
4071 let te =
4072 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4074 state.mode <- Textentry (te, leave m_prev_mode);
4076 )) :: m_l
4078 method bool ?(offset=1) ?(btos=btos) name get set =
4079 m_l <-
4080 (name, `bool (btos, get), offset, Action (
4081 fun u ->
4082 let v = get () in
4083 set (not v);
4085 )) :: m_l
4087 method color name get set =
4088 m_l <-
4089 (name, `color get, 1, Action (
4090 fun u ->
4091 let invalid = (nan, nan, nan) in
4092 let ondone s =
4093 let c =
4094 try color_of_string s
4095 with exn ->
4096 state.text <- Printf.sprintf "bad color `%s': %s"
4097 s (Printexc.to_string exn);
4098 invalid
4100 if c <> invalid
4101 then set c;
4103 let te = name ^ ": ", "", None, textentry, ondone, true in
4104 state.text <- color_to_string (get ());
4105 state.mode <- Textentry (te, leave m_prev_mode);
4107 )) :: m_l
4109 method string name get set =
4110 m_l <-
4111 (name, `string get, 1, Action (
4112 fun u ->
4113 let ondone s = set s in
4114 let te = name ^ ": ", "", None, textentry, ondone, true in
4115 state.mode <- Textentry (te, leave m_prev_mode);
4117 )) :: m_l
4119 method colorspace name get set =
4120 m_l <-
4121 (name, `string get, 1, Action (
4122 fun _ ->
4123 let source =
4124 let vals = [| "rgb"; "bgr"; "gray" |] in
4125 (object
4126 inherit lvsourcebase
4128 initializer
4129 m_active <- int_of_colorspace conf.colorspace;
4130 m_first <- 0;
4132 method getitemcount = Array.length vals
4133 method getitem n = (vals.(n), 0)
4134 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4135 ignore (uioh, first, pan, qsearch);
4136 if not cancel then set active;
4137 None
4138 method hasaction _ = true
4139 end)
4141 state.text <- "";
4142 let modehash = findkeyhash conf "info" in
4143 coe (new listview ~source ~trusted:true ~modehash)
4144 )) :: m_l
4146 method caption s offset =
4147 m_l <- (s, `empty, offset, Noaction) :: m_l
4149 method caption2 s f offset =
4150 m_l <- (s, `string f, offset, Noaction) :: m_l
4152 method getitemcount = Array.length m_a
4154 method getitem n =
4155 let tostr = function
4156 | `int f -> string_of_int (f ())
4157 | `intws f -> string_with_suffix_of_int (f ())
4158 | `string f -> f ()
4159 | `color f -> color_to_string (f ())
4160 | `bool (btos, f) -> btos (f ())
4161 | `empty -> ""
4163 let name, t, offset, _ = m_a.(n) in
4164 ((let s = tostr t in
4165 if String.length s > 0
4166 then Printf.sprintf "%s\t%s" name s
4167 else name),
4168 offset)
4170 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4171 let uiohopt =
4172 if not cancel
4173 then (
4174 m_qsearch <- qsearch;
4175 let uioh =
4176 match m_a.(active) with
4177 | _, _, _, Action f -> f uioh
4178 | _ -> uioh
4180 Some uioh
4182 else None
4184 m_active <- active;
4185 m_first <- first;
4186 m_pan <- pan;
4187 uiohopt
4189 method hasaction n =
4190 match m_a.(n) with
4191 | _, _, _, Action _ -> true
4192 | _ -> false
4193 end)
4195 let rec fillsrc prevmode prevuioh =
4196 let sep () = src#caption "" 0 in
4197 let colorp name get set =
4198 src#string name
4199 (fun () -> color_to_string (get ()))
4200 (fun v ->
4202 let c = color_of_string v in
4203 set c
4204 with exn ->
4205 state.text <- Printf.sprintf "bad color `%s': %s"
4206 v (Printexc.to_string exn);
4209 let oldmode = state.mode in
4210 let birdseye = isbirdseye state.mode in
4212 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4214 src#bool "presentation mode"
4215 (fun () -> conf.presentation)
4216 (fun v -> setpresentationmode v);
4218 src#bool "ignore case in searches"
4219 (fun () -> conf.icase)
4220 (fun v -> conf.icase <- v);
4222 src#bool "preload"
4223 (fun () -> conf.preload)
4224 (fun v -> conf.preload <- v);
4226 src#bool "highlight links"
4227 (fun () -> conf.hlinks)
4228 (fun v -> conf.hlinks <- v);
4230 src#bool "under info"
4231 (fun () -> conf.underinfo)
4232 (fun v -> conf.underinfo <- v);
4234 src#bool "persistent bookmarks"
4235 (fun () -> conf.savebmarks)
4236 (fun v -> conf.savebmarks <- v);
4238 src#bool "proportional display"
4239 (fun () -> conf.proportional)
4240 (fun v -> reqlayout conf.angle v);
4242 src#bool "trim margins"
4243 (fun () -> conf.trimmargins)
4244 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4246 src#bool "persistent location"
4247 (fun () -> conf.jumpback)
4248 (fun v -> conf.jumpback <- v);
4250 sep ();
4251 src#int "inter-page space"
4252 (fun () -> conf.interpagespace)
4253 (fun n ->
4254 conf.interpagespace <- n;
4255 docolumns conf.columns;
4256 let pageno, py =
4257 match state.layout with
4258 | [] -> 0, 0
4259 | l :: _ ->
4260 l.pageno, l.pagey
4262 state.maxy <- calcheight ();
4263 let y = getpagey pageno in
4264 gotoy (y + py)
4267 src#int "page bias"
4268 (fun () -> conf.pagebias)
4269 (fun v -> conf.pagebias <- v);
4271 src#int "scroll step"
4272 (fun () -> conf.scrollstep)
4273 (fun n -> conf.scrollstep <- n);
4275 src#int "horizontal scroll step"
4276 (fun () -> conf.hscrollstep)
4277 (fun v -> conf.hscrollstep <- v);
4279 src#int "auto scroll step"
4280 (fun () ->
4281 match state.autoscroll with
4282 | Some step -> step
4283 | _ -> conf.autoscrollstep)
4284 (fun n ->
4285 if state.autoscroll <> None
4286 then state.autoscroll <- Some n;
4287 conf.autoscrollstep <- n);
4289 src#int "zoom"
4290 (fun () -> truncate (conf.zoom *. 100.))
4291 (fun v -> setzoom ((float v) /. 100.));
4293 src#int "rotation"
4294 (fun () -> conf.angle)
4295 (fun v -> reqlayout v conf.proportional);
4297 src#int "scroll bar width"
4298 (fun () -> state.scrollw)
4299 (fun v ->
4300 state.scrollw <- v;
4301 conf.scrollbw <- v;
4302 reshape conf.winw conf.winh;
4305 src#int "scroll handle height"
4306 (fun () -> conf.scrollh)
4307 (fun v -> conf.scrollh <- v;);
4309 src#int "thumbnail width"
4310 (fun () -> conf.thumbw)
4311 (fun v ->
4312 conf.thumbw <- min 4096 v;
4313 match oldmode with
4314 | Birdseye beye ->
4315 leavebirdseye beye false;
4316 enterbirdseye ()
4317 | _ -> ()
4320 let mode = state.mode in
4321 src#string "columns"
4322 (fun () ->
4323 match conf.columns with
4324 | Csingle _ -> "1"
4325 | Cmulti (multi, _) -> multicolumns_to_string multi
4326 | Csplit (count, _) -> "-" ^ string_of_int count
4328 (fun v ->
4329 let n, a, b = multicolumns_of_string v in
4330 setcolumns mode n a b);
4332 sep ();
4333 src#caption "Presentation mode" 0;
4334 src#bool "scrollbar visible"
4335 (fun () -> conf.scrollbarinpm)
4336 (fun v ->
4337 if v != conf.scrollbarinpm
4338 then (
4339 conf.scrollbarinpm <- v;
4340 if conf.presentation
4341 then (
4342 state.scrollw <- if v then conf.scrollbw else 0;
4343 reshape conf.winw conf.winh;
4348 sep ();
4349 src#caption "Pixmap cache" 0;
4350 src#int_with_suffix "size (advisory)"
4351 (fun () -> conf.memlimit)
4352 (fun v -> conf.memlimit <- v);
4354 src#caption2 "used"
4355 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4356 (string_with_suffix_of_int state.memused)
4357 (Hashtbl.length state.tilemap)) 1;
4359 sep ();
4360 src#caption "Layout" 0;
4361 src#caption2 "Dimension"
4362 (fun () ->
4363 Printf.sprintf "%dx%d (virtual %dx%d)"
4364 conf.winw conf.winh
4365 state.w state.maxy)
4367 if conf.debug
4368 then
4369 src#caption2 "Position" (fun () ->
4370 Printf.sprintf "%dx%d" state.x state.y
4372 else
4373 src#caption2 "Visible" (fun () -> describe_location ()) 1
4376 sep ();
4377 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4378 "Save these parameters as global defaults at exit"
4379 (fun () -> conf.bedefault)
4380 (fun v -> conf.bedefault <- v)
4383 sep ();
4384 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4385 src#bool ~offset:0 ~btos "Extended parameters"
4386 (fun () -> !showextended)
4387 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4388 if !showextended
4389 then (
4390 src#bool "checkers"
4391 (fun () -> conf.checkers)
4392 (fun v -> conf.checkers <- v; setcheckers v);
4393 src#bool "update cursor"
4394 (fun () -> conf.updatecurs)
4395 (fun v -> conf.updatecurs <- v);
4396 src#bool "verbose"
4397 (fun () -> conf.verbose)
4398 (fun v -> conf.verbose <- v);
4399 src#bool "invert colors"
4400 (fun () -> conf.invert)
4401 (fun v -> conf.invert <- v);
4402 src#bool "max fit"
4403 (fun () -> conf.maxhfit)
4404 (fun v -> conf.maxhfit <- v);
4405 src#bool "redirect stderr"
4406 (fun () -> conf.redirectstderr)
4407 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4408 src#string "uri launcher"
4409 (fun () -> conf.urilauncher)
4410 (fun v -> conf.urilauncher <- v);
4411 src#string "path launcher"
4412 (fun () -> conf.pathlauncher)
4413 (fun v -> conf.pathlauncher <- v);
4414 src#string "tile size"
4415 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4416 (fun v ->
4418 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4419 conf.tilew <- max 64 w;
4420 conf.tileh <- max 64 h;
4421 flushtiles ();
4422 with exn ->
4423 state.text <- Printf.sprintf "bad tile size `%s': %s"
4424 v (Printexc.to_string exn));
4425 src#int "texture count"
4426 (fun () -> conf.texcount)
4427 (fun v ->
4428 if realloctexts v
4429 then conf.texcount <- v
4430 else showtext '!' " Failed to set texture count please retry later"
4432 src#int "slice height"
4433 (fun () -> conf.sliceheight)
4434 (fun v ->
4435 conf.sliceheight <- v;
4436 wcmd "sliceh %d" conf.sliceheight;
4438 src#int "anti-aliasing level"
4439 (fun () -> conf.aalevel)
4440 (fun v ->
4441 conf.aalevel <- bound v 0 8;
4442 state.anchor <- getanchor ();
4443 opendoc state.path state.password state.nameddest;
4445 src#string "page scroll scaling factor"
4446 (fun () -> string_of_float conf.pgscale)
4447 (fun v ->
4449 let s = float_of_string v in
4450 conf.pgscale <- s
4451 with exn ->
4452 state.text <- Printf.sprintf
4453 "bad page scroll scaling factor `%s': %s"
4454 v (Printexc.to_string exn)
4457 src#int "ui font size"
4458 (fun () -> fstate.fontsize)
4459 (fun v -> setfontsize (bound v 5 100));
4460 src#int "hint font size"
4461 (fun () -> conf.hfsize)
4462 (fun v -> conf.hfsize <- bound v 5 100);
4463 colorp "background color"
4464 (fun () -> conf.bgcolor)
4465 (fun v -> conf.bgcolor <- v);
4466 src#bool "crop hack"
4467 (fun () -> conf.crophack)
4468 (fun v -> conf.crophack <- v);
4469 src#string "trim fuzz"
4470 (fun () -> irect_to_string conf.trimfuzz)
4471 (fun v ->
4473 conf.trimfuzz <- irect_of_string v;
4474 if conf.trimmargins
4475 then settrim true conf.trimfuzz;
4476 with exn ->
4477 state.text <- Printf.sprintf "bad irect `%s': %s"
4478 v (Printexc.to_string exn)
4480 src#string "throttle"
4481 (fun () ->
4482 match conf.maxwait with
4483 | None -> "show place holder if page is not ready"
4484 | Some time ->
4485 if time = infinity
4486 then "wait for page to fully render"
4487 else
4488 "wait " ^ string_of_float time
4489 ^ " seconds before showing placeholder"
4491 (fun v ->
4493 let f = float_of_string v in
4494 if f <= 0.0
4495 then conf.maxwait <- None
4496 else conf.maxwait <- Some f
4497 with exn ->
4498 state.text <- Printf.sprintf "bad time `%s': %s"
4499 v (Printexc.to_string exn)
4501 src#string "ghyll scroll"
4502 (fun () ->
4503 match conf.ghyllscroll with
4504 | None -> ""
4505 | Some nab -> ghyllscroll_to_string nab
4507 (fun v ->
4509 let gs =
4510 if String.length v = 0
4511 then None
4512 else Some (ghyllscroll_of_string v)
4514 conf.ghyllscroll <- gs
4515 with exn ->
4516 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4517 v (Printexc.to_string exn)
4519 src#string "selection command"
4520 (fun () -> conf.selcmd)
4521 (fun v -> conf.selcmd <- v);
4522 src#colorspace "color space"
4523 (fun () -> colorspace_to_string conf.colorspace)
4524 (fun v ->
4525 conf.colorspace <- colorspace_of_int v;
4526 wcmd "cs %d" v;
4527 load state.layout;
4529 if pbousable ()
4530 then
4531 src#bool "use PBO"
4532 (fun () -> conf.usepbo)
4533 (fun v -> conf.usepbo <- v);
4534 src#bool "mouse wheel scrolls pages"
4535 (fun () -> conf.wheelbypage)
4536 (fun v -> conf.wheelbypage <- v);
4539 sep ();
4540 src#caption "Document" 0;
4541 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4542 src#caption2 "Pages"
4543 (fun () -> string_of_int state.pagecount) 1;
4544 src#caption2 "Dimensions"
4545 (fun () -> string_of_int (List.length state.pdims)) 1;
4546 if conf.trimmargins
4547 then (
4548 sep ();
4549 src#caption "Trimmed margins" 0;
4550 src#caption2 "Dimensions"
4551 (fun () -> string_of_int (List.length state.pdims)) 1;
4554 sep ();
4555 src#caption "OpenGL" 0;
4556 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4557 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4558 src#reset prevmode prevuioh;
4560 fun () ->
4561 state.text <- "";
4562 let prevmode = state.mode
4563 and prevuioh = state.uioh in
4564 fillsrc prevmode prevuioh;
4565 let source = (src :> lvsource) in
4566 let modehash = findkeyhash conf "info" in
4567 state.uioh <- coe (object (self)
4568 inherit listview ~source ~trusted:true ~modehash as super
4569 val mutable m_prevmemused = 0
4570 method infochanged = function
4571 | Memused ->
4572 if m_prevmemused != state.memused
4573 then (
4574 m_prevmemused <- state.memused;
4575 G.postRedisplay "memusedchanged";
4577 | Pdim -> G.postRedisplay "pdimchanged"
4578 | Docinfo -> fillsrc prevmode prevuioh
4580 method key key mask =
4581 if not (Wsi.withctrl mask)
4582 then
4583 match key with
4584 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4585 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4586 | _ -> super#key key mask
4587 else super#key key mask
4588 end);
4589 G.postRedisplay "info";
4592 let enterhelpmode =
4593 let source =
4594 (object
4595 inherit lvsourcebase
4596 method getitemcount = Array.length state.help
4597 method getitem n =
4598 let s, l, _ = state.help.(n) in
4599 (s, l)
4601 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4602 let optuioh =
4603 if not cancel
4604 then (
4605 m_qsearch <- qsearch;
4606 match state.help.(active) with
4607 | _, _, Action f -> Some (f uioh)
4608 | _ -> Some (uioh)
4610 else None
4612 m_active <- active;
4613 m_first <- first;
4614 m_pan <- pan;
4615 optuioh
4617 method hasaction n =
4618 match state.help.(n) with
4619 | _, _, Action _ -> true
4620 | _ -> false
4622 initializer
4623 m_active <- -1
4624 end)
4625 in fun () ->
4626 let modehash = findkeyhash conf "help" in
4627 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4628 G.postRedisplay "help";
4631 let entermsgsmode =
4632 let msgsource =
4633 let re = Str.regexp "[\r\n]" in
4634 (object
4635 inherit lvsourcebase
4636 val mutable m_items = [||]
4638 method getitemcount = 1 + Array.length m_items
4640 method getitem n =
4641 if n = 0
4642 then "[Clear]", 0
4643 else m_items.(n-1), 0
4645 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4646 ignore uioh;
4647 if not cancel
4648 then (
4649 if active = 0
4650 then Buffer.clear state.errmsgs;
4651 m_qsearch <- qsearch;
4653 m_active <- active;
4654 m_first <- first;
4655 m_pan <- pan;
4656 None
4658 method hasaction n =
4659 n = 0
4661 method reset =
4662 state.newerrmsgs <- false;
4663 let l = Str.split re (Buffer.contents state.errmsgs) in
4664 m_items <- Array.of_list l
4666 initializer
4667 m_active <- 0
4668 end)
4669 in fun () ->
4670 state.text <- "";
4671 msgsource#reset;
4672 let source = (msgsource :> lvsource) in
4673 let modehash = findkeyhash conf "listview" in
4674 state.uioh <- coe (object
4675 inherit listview ~source ~trusted:false ~modehash as super
4676 method display =
4677 if state.newerrmsgs
4678 then msgsource#reset;
4679 super#display
4680 end);
4681 G.postRedisplay "msgs";
4684 let quickbookmark ?title () =
4685 match state.layout with
4686 | [] -> ()
4687 | l :: _ ->
4688 let title =
4689 match title with
4690 | None ->
4691 let sec = Unix.gettimeofday () in
4692 let tm = Unix.localtime sec in
4693 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4694 (l.pageno+1)
4695 tm.Unix.tm_mday
4696 tm.Unix.tm_mon
4697 (tm.Unix.tm_year + 1900)
4698 tm.Unix.tm_hour
4699 tm.Unix.tm_min
4700 | Some title -> title
4702 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4705 let doreshape w h =
4706 state.fullscreen <- None;
4707 Wsi.reshape w h;
4710 let setautoscrollspeed step goingdown =
4711 let incr = max 1 ((abs step) / 2) in
4712 let incr = if goingdown then incr else -incr in
4713 let astep = step + incr in
4714 state.autoscroll <- Some astep;
4717 let gotounder = function
4718 | Ulinkgoto (pageno, top) ->
4719 if pageno >= 0
4720 then (
4721 addnav ();
4722 gotopage1 pageno top;
4725 | Ulinkuri s ->
4726 gotouri s
4728 | Uremote (filename, pageno) ->
4729 let path =
4730 if Sys.file_exists filename
4731 then filename
4732 else
4733 let dir = Filename.dirname state.path in
4734 let path = Filename.concat dir filename in
4735 if Sys.file_exists path
4736 then path
4737 else ""
4739 if String.length path > 0
4740 then (
4741 let anchor = getanchor () in
4742 let ranchor = state.path, state.password, anchor in
4743 state.anchor <- (pageno, 0.0, 0.0);
4744 state.ranchors <- ranchor :: state.ranchors;
4745 opendoc path "" "";
4747 else showtext '!' ("Could not find " ^ filename)
4749 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4752 let canpan () =
4753 match conf.columns with
4754 | Csplit _ -> true
4755 | _ -> conf.zoom > 1.0
4758 let existsinrow pageno (columns, coverA, coverB) p =
4759 let last = ((pageno - coverA) mod columns) + columns in
4760 let rec any = function
4761 | [] -> false
4762 | l :: rest ->
4763 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4764 then p l
4765 else (
4766 if not (p l)
4767 then (if l.pageno = last then false else any rest)
4768 else true
4771 any state.layout
4774 let nextpage () =
4775 match state.layout with
4776 | [] -> ()
4777 | l :: rest ->
4778 match conf.columns with
4779 | Csingle _ ->
4780 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4781 then
4782 let y = clamp (pgscale conf.winh) in
4783 gotoghyll y
4784 else
4785 let pageno = min (l.pageno+1) (state.pagecount-1) in
4786 gotoghyll (getpagey pageno)
4787 | Cmulti ((c, _, _) as cl, _) ->
4788 if conf.presentation
4789 && (existsinrow l.pageno cl
4790 (fun l -> l.pageh > l.pagey + l.pagevh))
4791 then
4792 let y = clamp (pgscale conf.winh) in
4793 gotoghyll y
4794 else
4795 let pageno = min (l.pageno+c) (state.pagecount-1) in
4796 gotoghyll (getpagey pageno)
4797 | Csplit (n, _) ->
4798 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4799 then
4800 let pagey, pageh = getpageyh l.pageno in
4801 let pagey = pagey + pageh * l.pagecol in
4802 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4803 gotoghyll (pagey + pageh + ips)
4806 let prevpage () =
4807 match state.layout with
4808 | [] -> ()
4809 | l :: _ ->
4810 match conf.columns with
4811 | Csingle _ ->
4812 if conf.presentation && l.pagey != 0
4813 then
4814 gotoghyll (clamp (pgscale ~-(conf.winh)))
4815 else
4816 let pageno = max 0 (l.pageno-1) in
4817 gotoghyll (getpagey pageno)
4818 | Cmulti ((c, _, coverB) as cl, _) ->
4819 if conf.presentation &&
4820 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4821 then
4822 gotoghyll (clamp (pgscale ~-(conf.winh)))
4823 else
4824 let decr =
4825 if l.pageno = state.pagecount - coverB
4826 then 1
4827 else c
4829 let pageno = max 0 (l.pageno-decr) in
4830 gotoghyll (getpagey pageno)
4831 | Csplit (n, _) ->
4832 let y =
4833 if l.pagecol = 0
4834 then
4835 if l.pageno = 0
4836 then l.pagey
4837 else
4838 let pageno = max 0 (l.pageno-1) in
4839 let pagey, pageh = getpageyh pageno in
4840 pagey + (n-1)*pageh
4841 else
4842 let pagey, pageh = getpageyh l.pageno in
4843 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4845 gotoghyll y
4848 let viewkeyboard key mask =
4849 let enttext te =
4850 let mode = state.mode in
4851 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4852 state.text <- "";
4853 enttext ();
4854 G.postRedisplay "view:enttext"
4856 let ctrl = Wsi.withctrl mask in
4857 let key =
4858 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4860 match key with
4861 | 81 -> (* Q *)
4862 exit 0
4864 | 0xff63 -> (* insert *)
4865 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4866 then (
4867 state.mode <- LinkNav (Ltgendir 0);
4868 gotoy state.y;
4870 else showtext '!' "Keyboard link navigation does not work under rotation"
4872 | 0xff1b | 113 -> (* escape / q *)
4873 begin match state.mstate with
4874 | Mzoomrect _ ->
4875 state.mstate <- Mnone;
4876 Wsi.setcursor Wsi.CURSOR_INHERIT;
4877 G.postRedisplay "kill zoom rect";
4878 | _ ->
4879 begin match state.mode with
4880 | LinkNav _ ->
4881 state.mode <- View;
4882 G.postRedisplay "esc leave linknav"
4883 | _ ->
4884 match state.ranchors with
4885 | [] -> raise Quit
4886 | (path, password, anchor) :: rest ->
4887 state.ranchors <- rest;
4888 state.anchor <- anchor;
4889 opendoc path password ""
4890 end;
4891 end;
4893 | 0xff08 -> (* backspace *)
4894 gotoghyll (getnav ~-1)
4896 | 111 -> (* o *)
4897 enteroutlinemode ()
4899 | 117 -> (* u *)
4900 state.rects <- [];
4901 state.text <- "";
4902 G.postRedisplay "dehighlight";
4904 | 47 | 63 -> (* / ? *)
4905 let ondone isforw s =
4906 cbput state.hists.pat s;
4907 state.searchpattern <- s;
4908 search s isforw
4910 let s = String.create 1 in
4911 s.[0] <- Char.chr key;
4912 enttext (s, "", Some (onhist state.hists.pat),
4913 textentry, ondone (key = 47), true)
4915 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4916 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4917 setzoom (conf.zoom +. incr)
4919 | 43 | 0xffab -> (* + *)
4920 let ondone s =
4921 let n =
4922 try int_of_string s with exc ->
4923 state.text <- Printf.sprintf "bad integer `%s': %s"
4924 s (Printexc.to_string exc);
4925 max_int
4927 if n != max_int
4928 then (
4929 conf.pagebias <- n;
4930 state.text <- "page bias is now " ^ string_of_int n;
4933 enttext ("page bias: ", "", None, intentry, ondone, true)
4935 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4936 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4937 setzoom (max 0.01 (conf.zoom -. decr))
4939 | 45 | 0xffad -> (* - *)
4940 let ondone msg = state.text <- msg in
4941 enttext (
4942 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4943 optentry state.mode, ondone, true
4946 | 48 when ctrl -> (* ctrl-0 *)
4947 setzoom 1.0
4949 | 49 when ctrl -> (* ctrl-1 *)
4950 let cols =
4951 match conf.columns with
4952 | Csingle _ | Cmulti _ -> 1
4953 | Csplit (n, _) -> n
4955 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4956 if zoom < 1.0
4957 then setzoom zoom
4959 | 0xffc6 -> (* f9 *)
4960 togglebirdseye ()
4962 | 57 when ctrl -> (* ctrl-9 *)
4963 togglebirdseye ()
4965 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4966 when not ctrl -> (* 0..9 *)
4967 let ondone s =
4968 let n =
4969 try int_of_string s with exc ->
4970 state.text <- Printf.sprintf "bad integer `%s': %s"
4971 s (Printexc.to_string exc);
4974 if n >= 0
4975 then (
4976 addnav ();
4977 cbput state.hists.pag (string_of_int n);
4978 gotopage1 (n + conf.pagebias - 1) 0;
4981 let pageentry text key =
4982 match Char.unsafe_chr key with
4983 | 'g' -> TEdone text
4984 | _ -> intentry text key
4986 let text = "x" in text.[0] <- Char.chr key;
4987 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4989 | 98 -> (* b *)
4990 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4991 reshape conf.winw conf.winh;
4993 | 108 -> (* l *)
4994 conf.hlinks <- not conf.hlinks;
4995 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4996 G.postRedisplay "toggle highlightlinks";
4998 | 70 -> (* F *)
4999 state.glinks <- true;
5000 let mode = state.mode in
5001 state.mode <- Textentry (
5002 (":", "", None, linknentry, linkndone gotounder, false),
5003 (fun _ ->
5004 state.glinks <- false;
5005 state.mode <- mode)
5007 state.text <- "";
5008 G.postRedisplay "view:linkent(F)"
5010 | 121 -> (* y *)
5011 state.glinks <- true;
5012 let mode = state.mode in
5013 state.mode <- Textentry (
5014 (":", "", None, linknentry, linkndone (fun under ->
5015 match Ne.pipe () with
5016 | Ne.Exn exn ->
5017 showtext '!' (Printf.sprintf "pipe failed: %s"
5018 (Printexc.to_string exn));
5019 | Ne.Res (r, w) ->
5020 let popened =
5021 try popen conf.selcmd [r, 0; w, -1]; true
5022 with exn ->
5023 showtext '!'
5024 (Printf.sprintf "failed to execute %s: %s"
5025 conf.selcmd (Printexc.to_string exn));
5026 false
5028 let clo cap fd =
5029 Ne.clo fd (fun msg ->
5030 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5033 let s = undertext under in
5034 if popened
5035 then
5036 (try
5037 let l = String.length s in
5038 let n = Unix.write w s 0 l in
5039 if n != l
5040 then
5041 showtext '!'
5042 (Printf.sprintf
5043 "failed to write %d characters to sel pipe, wrote %d"
5046 with exn ->
5047 showtext '!'
5048 (Printf.sprintf "failed to write to sel pipe: %s"
5049 (Printexc.to_string exn)
5052 else dolog "%s" s;
5053 clo "pipe/r" r;
5054 clo "pipe/w" w;
5055 ), false
5057 fun _ ->
5058 state.glinks <- false;
5059 state.mode <- mode
5061 state.text <- "";
5062 G.postRedisplay "view:linkent"
5064 | 97 -> (* a *)
5065 begin match state.autoscroll with
5066 | Some step ->
5067 conf.autoscrollstep <- step;
5068 state.autoscroll <- None
5069 | None ->
5070 if conf.autoscrollstep = 0
5071 then state.autoscroll <- Some 1
5072 else state.autoscroll <- Some conf.autoscrollstep
5075 | 112 when ctrl -> (* ctrl-p *)
5076 launchpath ()
5078 | 80 -> (* P *)
5079 setpresentationmode (not conf.presentation);
5080 showtext ' ' ("presentation mode " ^
5081 if conf.presentation then "on" else "off");
5083 | 102 -> (* f *)
5084 begin match state.fullscreen with
5085 | None ->
5086 state.fullscreen <- Some (conf.winw, conf.winh);
5087 Wsi.fullscreen ()
5088 | Some (w, h) ->
5089 state.fullscreen <- None;
5090 doreshape w h
5093 | 112 | 78 -> (* p|N *)
5094 search state.searchpattern false
5096 | 110 | 0xffc0 -> (* n|F3 *)
5097 search state.searchpattern true
5099 | 116 -> (* t *)
5100 begin match state.layout with
5101 | [] -> ()
5102 | l :: _ ->
5103 gotoy_and_clear_text (getpagey l.pageno)
5106 | 32 -> (* space *)
5107 nextpage ()
5109 | 0xff9f | 0xffff -> (* delete *)
5110 prevpage ()
5112 | 61 -> (* = *)
5113 showtext ' ' (describe_location ());
5115 | 119 -> (* w *)
5116 begin match state.layout with
5117 | [] -> ()
5118 | l :: _ ->
5119 doreshape (l.pagew + state.scrollw) l.pageh;
5120 G.postRedisplay "w"
5123 | 39 -> (* ' *)
5124 enterbookmarkmode ()
5126 | 104 | 0xffbe -> (* h|F1 *)
5127 enterhelpmode ()
5129 | 105 -> (* i *)
5130 enterinfomode ()
5132 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5133 entermsgsmode ()
5135 | 109 -> (* m *)
5136 let ondone s =
5137 match state.layout with
5138 | l :: _ ->
5139 if String.length s > 0
5140 then
5141 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5142 | _ -> ()
5144 enttext ("bookmark: ", "", None, textentry, ondone, true)
5146 | 126 -> (* ~ *)
5147 quickbookmark ();
5148 showtext ' ' "Quick bookmark added";
5150 | 122 -> (* z *)
5151 begin match state.layout with
5152 | l :: _ ->
5153 let rect = getpdimrect l.pagedimno in
5154 let w, h =
5155 if conf.crophack
5156 then
5157 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5158 truncate (1.2 *. (rect.(3) -. rect.(0))))
5159 else
5160 (truncate (rect.(1) -. rect.(0)),
5161 truncate (rect.(3) -. rect.(0)))
5163 let w = truncate ((float w)*.conf.zoom)
5164 and h = truncate ((float h)*.conf.zoom) in
5165 if w != 0 && h != 0
5166 then (
5167 state.anchor <- getanchor ();
5168 doreshape (w + state.scrollw) (h + conf.interpagespace)
5170 G.postRedisplay "z";
5172 | [] -> ()
5175 | 50 when ctrl -> (* ctrl-2 *)
5176 let maxw = getmaxw () in
5177 if maxw > 0.0
5178 then setzoom (maxw /. float conf.winw)
5180 | 60 | 62 -> (* < > *)
5181 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5183 | 91 | 93 -> (* [ ] *)
5184 conf.colorscale <-
5185 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5187 G.postRedisplay "brightness";
5189 | 99 when state.mode = View -> (* c *)
5190 let (c, a, b), z =
5191 match state.prevcolumns with
5192 | None -> (1, 0, 0), 1.0
5193 | Some (columns, z) ->
5194 let cab =
5195 match columns with
5196 | Csplit (c, _) -> -c, 0, 0
5197 | Cmulti ((c, a, b), _) -> c, a, b
5198 | Csingle _ -> 1, 0, 0
5200 cab, z
5202 setcolumns View c a b;
5203 setzoom z;
5205 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5206 setzoom state.prevzoom
5208 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5209 begin match state.autoscroll with
5210 | None ->
5211 begin match state.mode with
5212 | Birdseye beye -> upbirdseye 1 beye
5213 | _ ->
5214 if ctrl
5215 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5216 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5218 | Some n ->
5219 setautoscrollspeed n false
5222 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5223 begin match state.autoscroll with
5224 | None ->
5225 begin match state.mode with
5226 | Birdseye beye -> downbirdseye 1 beye
5227 | _ ->
5228 if ctrl
5229 then gotoy_and_clear_text (clamp (conf.winh/2))
5230 else gotoy_and_clear_text (clamp conf.scrollstep)
5232 | Some n ->
5233 setautoscrollspeed n true
5236 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5237 if canpan ()
5238 then
5239 let dx =
5240 if ctrl
5241 then conf.winw / 2
5242 else conf.hscrollstep
5244 let dx = if key = 0xff51 then dx else -dx in
5245 state.x <- state.x + dx;
5246 gotoy_and_clear_text state.y
5247 else (
5248 state.text <- "";
5249 G.postRedisplay "lef/right"
5252 | 0xff55 | 0xff9a -> (* (kp) prior *)
5253 let y =
5254 if ctrl
5255 then
5256 match state.layout with
5257 | [] -> state.y
5258 | l :: _ -> state.y - l.pagey
5259 else
5260 clamp (pgscale (-conf.winh))
5262 gotoghyll y
5264 | 0xff56 | 0xff9b -> (* (kp) next *)
5265 let y =
5266 if ctrl
5267 then
5268 match List.rev state.layout with
5269 | [] -> state.y
5270 | l :: _ -> getpagey l.pageno
5271 else
5272 clamp (pgscale conf.winh)
5274 gotoghyll y
5276 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5277 gotoghyll 0
5278 | 71 | 0xff57 | 0xff9c -> (* G end *)
5279 gotoghyll (clamp state.maxy)
5281 | 0xff53 when Wsi.withalt mask -> (* alt-right *)
5282 gotoghyll (getnav 1)
5283 | 0xff51 when Wsi.withalt mask -> (* alt-left *)
5284 gotoghyll (getnav ~-1)
5286 | 114 -> (* r *)
5287 reload ()
5289 | 118 when conf.debug -> (* v *)
5290 state.rects <- [];
5291 List.iter (fun l ->
5292 match getopaque l.pageno with
5293 | None -> ()
5294 | Some opaque ->
5295 let x0, y0, x1, y1 = pagebbox opaque in
5296 let a,b = float x0, float y0 in
5297 let c,d = float x1, float y0 in
5298 let e,f = float x1, float y1 in
5299 let h,j = float x0, float y1 in
5300 let rect = (a,b,c,d,e,f,h,j) in
5301 debugrect rect;
5302 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5303 ) state.layout;
5304 G.postRedisplay "v";
5306 | _ ->
5307 vlog "huh? %s" (Wsi.keyname key)
5310 let linknavkeyboard key mask linknav =
5311 let getpage pageno =
5312 let rec loop = function
5313 | [] -> None
5314 | l :: _ when l.pageno = pageno -> Some l
5315 | _ :: rest -> loop rest
5316 in loop state.layout
5318 let doexact (pageno, n) =
5319 match getopaque pageno, getpage pageno with
5320 | Some opaque, Some l ->
5321 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5322 then
5323 let under = getlink opaque n in
5324 G.postRedisplay "link gotounder";
5325 gotounder under;
5326 state.mode <- View;
5327 else
5328 let opt, dir =
5329 match key with
5330 | 0xff50 -> (* home *)
5331 Some (findlink opaque LDfirst), -1
5333 | 0xff57 -> (* end *)
5334 Some (findlink opaque LDlast), 1
5336 | 0xff51 -> (* left *)
5337 Some (findlink opaque (LDleft n)), -1
5339 | 0xff53 -> (* right *)
5340 Some (findlink opaque (LDright n)), 1
5342 | 0xff52 -> (* up *)
5343 Some (findlink opaque (LDup n)), -1
5345 | 0xff54 -> (* down *)
5346 Some (findlink opaque (LDdown n)), 1
5348 | _ -> None, 0
5350 let pwl l dir =
5351 begin match findpwl l.pageno dir with
5352 | Pwlnotfound -> ()
5353 | Pwl pageno ->
5354 let notfound dir =
5355 state.mode <- LinkNav (Ltgendir dir);
5356 let y, h = getpageyh pageno in
5357 let y =
5358 if dir < 0
5359 then y + h - conf.winh
5360 else y
5362 gotoy y
5364 begin match getopaque pageno, getpage pageno with
5365 | Some opaque, Some _ ->
5366 let link =
5367 let ld = if dir > 0 then LDfirst else LDlast in
5368 findlink opaque ld
5370 begin match link with
5371 | Lfound m ->
5372 showlinktype (getlink opaque m);
5373 state.mode <- LinkNav (Ltexact (pageno, m));
5374 G.postRedisplay "linknav jpage";
5375 | _ -> notfound dir
5376 end;
5377 | _ -> notfound dir
5378 end;
5379 end;
5381 begin match opt with
5382 | Some Lnotfound -> pwl l dir;
5383 | Some (Lfound m) ->
5384 if m = n
5385 then pwl l dir
5386 else (
5387 let _, y0, _, y1 = getlinkrect opaque m in
5388 if y0 < l.pagey
5389 then gotopage1 l.pageno y0
5390 else (
5391 let d = fstate.fontsize + 1 in
5392 if y1 - l.pagey > l.pagevh - d
5393 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5394 else G.postRedisplay "linknav";
5396 showlinktype (getlink opaque m);
5397 state.mode <- LinkNav (Ltexact (l.pageno, m));
5400 | None -> viewkeyboard key mask
5401 end;
5402 | _ -> viewkeyboard key mask
5404 if key = 0xff63
5405 then (
5406 state.mode <- View;
5407 G.postRedisplay "leave linknav"
5409 else
5410 match linknav with
5411 | Ltgendir _ -> viewkeyboard key mask
5412 | Ltexact exact -> doexact exact
5415 let keyboard key mask =
5416 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5417 then wcmd "interrupt"
5418 else state.uioh <- state.uioh#key key mask
5421 let birdseyekeyboard key mask
5422 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5423 let incr =
5424 match conf.columns with
5425 | Csingle _ -> 1
5426 | Cmulti ((c, _, _), _) -> c
5427 | Csplit _ -> failwith "bird's eye split mode"
5429 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5430 match key with
5431 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5432 let y, h = getpageyh pageno in
5433 let top = (conf.winh - h) / 2 in
5434 gotoy (max 0 (y - top))
5435 | 0xff0d (* enter *)
5436 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5437 | 0xff1b -> leavebirdseye beye true (* escape *)
5438 | 0xff52 -> upbirdseye incr beye (* up *)
5439 | 0xff54 -> downbirdseye incr beye (* down *)
5440 | 0xff51 -> upbirdseye 1 beye (* left *)
5441 | 0xff53 -> downbirdseye 1 beye (* right *)
5443 | 0xff55 -> (* prior *)
5444 begin match state.layout with
5445 | l :: _ ->
5446 if l.pagey != 0
5447 then (
5448 state.mode <- Birdseye (
5449 oconf, leftx, l.pageno, hooverpageno, anchor
5451 gotopage1 l.pageno 0;
5453 else (
5454 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5455 match layout with
5456 | [] -> gotoy (clamp (-conf.winh))
5457 | l :: _ ->
5458 state.mode <- Birdseye (
5459 oconf, leftx, l.pageno, hooverpageno, anchor
5461 gotopage1 l.pageno 0
5464 | [] -> gotoy (clamp (-conf.winh))
5465 end;
5467 | 0xff56 -> (* next *)
5468 begin match List.rev state.layout with
5469 | l :: _ ->
5470 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5471 begin match layout with
5472 | [] ->
5473 let incr = l.pageh - l.pagevh in
5474 if incr = 0
5475 then (
5476 state.mode <-
5477 Birdseye (
5478 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5480 G.postRedisplay "birdseye pagedown";
5482 else gotoy (clamp (incr + conf.interpagespace*2));
5484 | l :: _ ->
5485 state.mode <-
5486 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5487 gotopage1 l.pageno 0;
5490 | [] -> gotoy (clamp conf.winh)
5491 end;
5493 | 0xff50 -> (* home *)
5494 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5495 gotopage1 0 0
5497 | 0xff57 -> (* end *)
5498 let pageno = state.pagecount - 1 in
5499 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5500 if not (pagevisible state.layout pageno)
5501 then
5502 let h =
5503 match List.rev state.pdims with
5504 | [] -> conf.winh
5505 | (_, _, h, _) :: _ -> h
5507 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5508 else G.postRedisplay "birdseye end";
5509 | _ -> viewkeyboard key mask
5512 let drawpage l linkindexbase =
5513 let color =
5514 match state.mode with
5515 | Textentry _ -> scalecolor 0.4
5516 | LinkNav _
5517 | View -> scalecolor 1.0
5518 | Birdseye (_, _, pageno, hooverpageno, _) ->
5519 if l.pageno = hooverpageno
5520 then scalecolor 0.9
5521 else (
5522 if l.pageno = pageno
5523 then scalecolor 1.0
5524 else scalecolor 0.8
5527 drawtiles l color;
5528 begin match getopaque l.pageno with
5529 | Some opaque ->
5530 if tileready l l.pagex l.pagey
5531 then
5532 let x = l.pagedispx - l.pagex
5533 and y = l.pagedispy - l.pagey in
5534 let hlmask =
5535 match conf.columns with
5536 | Csingle _ | Cmulti _ ->
5537 (if conf.hlinks then 1 else 0)
5538 + (if state.glinks
5539 && not (isbirdseye state.mode) then 2 else 0)
5540 | _ -> 0
5542 let s =
5543 match state.mode with
5544 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5545 | _ -> ""
5547 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5548 else 0
5550 | _ -> 0
5551 end;
5554 let scrollindicator () =
5555 let sbw, ph, sh = state.uioh#scrollph in
5556 let sbh, pw, sw = state.uioh#scrollpw in
5558 GlDraw.color (0.64, 0.64, 0.64);
5559 GlDraw.rect
5560 (float (conf.winw - sbw), 0.)
5561 (float conf.winw, float conf.winh)
5563 GlDraw.rect
5564 (0., float (conf.winh - sbh))
5565 (float (conf.winw - state.scrollw - 1), float conf.winh)
5567 GlDraw.color (0.0, 0.0, 0.0);
5569 GlDraw.rect
5570 (float (conf.winw - sbw), ph)
5571 (float conf.winw, ph +. sh)
5573 GlDraw.rect
5574 (pw, float (conf.winh - sbh))
5575 (pw +. sw, float conf.winh)
5579 let showsel () =
5580 match state.mstate with
5581 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5584 | Msel ((x0, y0), (x1, y1)) ->
5585 let rec loop = function
5586 | l :: ls ->
5587 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5588 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5589 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5590 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5591 then
5592 match getopaque l.pageno with
5593 | Some opaque ->
5594 let x0, y0 = pagetranslatepoint l x0 y0 in
5595 let x1, y1 = pagetranslatepoint l x1 y1 in
5596 seltext opaque (x0, y0, x1, y1);
5597 | _ -> ()
5598 else loop ls
5599 | [] -> ()
5601 loop state.layout
5604 let showrects rects =
5605 Gl.enable `blend;
5606 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5607 GlDraw.polygon_mode `both `fill;
5608 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5609 List.iter
5610 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5611 List.iter (fun l ->
5612 if l.pageno = pageno
5613 then (
5614 let dx = float (l.pagedispx - l.pagex) in
5615 let dy = float (l.pagedispy - l.pagey) in
5616 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5617 GlDraw.begins `quads;
5619 GlDraw.vertex2 (x0+.dx, y0+.dy);
5620 GlDraw.vertex2 (x1+.dx, y1+.dy);
5621 GlDraw.vertex2 (x2+.dx, y2+.dy);
5622 GlDraw.vertex2 (x3+.dx, y3+.dy);
5624 GlDraw.ends ();
5626 ) state.layout
5627 ) rects
5629 Gl.disable `blend;
5632 let display () =
5633 GlClear.color (scalecolor2 conf.bgcolor);
5634 GlClear.clear [`color];
5635 let rec loop linkindexbase = function
5636 | l :: rest ->
5637 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5638 loop linkindexbase rest
5639 | [] -> ()
5641 loop 0 state.layout;
5642 let rects =
5643 match state.mode with
5644 | LinkNav (Ltexact (pageno, linkno)) ->
5645 begin match getopaque pageno with
5646 | Some opaque ->
5647 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5648 (pageno, 5, (
5649 float x0, float y0,
5650 float x1, float y0,
5651 float x1, float y1,
5652 float x0, float y1)
5653 ) :: state.rects
5654 | None -> state.rects
5656 | _ -> state.rects
5658 showrects rects;
5659 showsel ();
5660 state.uioh#display;
5661 begin match state.mstate with
5662 | Mzoomrect ((x0, y0), (x1, y1)) ->
5663 Gl.enable `blend;
5664 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5665 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5666 GlDraw.rect (float x0, float y0)
5667 (float x1, float y1);
5668 Gl.disable `blend;
5669 | _ -> ()
5670 end;
5671 enttext ();
5672 scrollindicator ();
5673 if not state.wthack then Wsi.swapb ();
5676 let zoomrect x y x1 y1 =
5677 let x0 = min x x1
5678 and x1 = max x x1
5679 and y0 = min y y1 in
5680 gotoy (state.y + y0);
5681 state.anchor <- getanchor ();
5682 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5683 let margin =
5684 if state.w < conf.winw - state.scrollw
5685 then (conf.winw - state.scrollw - state.w) / 2
5686 else 0
5688 state.x <- (state.x + margin) - x0;
5689 setzoom zoom;
5690 Wsi.setcursor Wsi.CURSOR_INHERIT;
5691 state.mstate <- Mnone;
5694 let scrollx x =
5695 let winw = conf.winw - state.scrollw - 1 in
5696 let s = float x /. float winw in
5697 let destx = truncate (float (state.w + winw) *. s) in
5698 state.x <- winw - destx;
5699 gotoy_and_clear_text state.y;
5700 state.mstate <- Mscrollx;
5703 let scrolly y =
5704 let s = float y /. float conf.winh in
5705 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5706 gotoy_and_clear_text desty;
5707 state.mstate <- Mscrolly;
5710 let viewmouse button down x y mask =
5711 match button with
5712 | n when (n == 4 || n == 5) && not down ->
5713 if Wsi.withctrl mask
5714 then (
5715 match state.mstate with
5716 | Mzoom (oldn, i) ->
5717 if oldn = n
5718 then (
5719 if i = 2
5720 then
5721 let incr =
5722 match n with
5723 | 5 ->
5724 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5725 | _ ->
5726 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5728 let zoom = conf.zoom -. incr in
5729 setzoom zoom;
5730 state.mstate <- Mzoom (n, 0);
5731 else
5732 state.mstate <- Mzoom (n, i+1);
5734 else state.mstate <- Mzoom (n, 0)
5736 | _ -> state.mstate <- Mzoom (n, 0)
5738 else (
5739 match state.autoscroll with
5740 | Some step -> setautoscrollspeed step (n=4)
5741 | None ->
5742 if conf.wheelbypage
5743 then (
5744 if n = 4
5745 then nextpage ()
5746 else prevpage ()
5748 else
5749 let incr =
5750 if n = 4
5751 then -conf.scrollstep
5752 else conf.scrollstep
5754 let incr = incr * 2 in
5755 let y = clamp incr in
5756 gotoy_and_clear_text y
5759 | n when (n = 6 || n = 7) && not down && canpan () ->
5760 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5761 gotoy_and_clear_text state.y
5763 | 1 when Wsi.withctrl mask ->
5764 if down
5765 then (
5766 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5767 state.mstate <- Mpan (x, y)
5769 else
5770 state.mstate <- Mnone
5772 | 3 ->
5773 if down
5774 then (
5775 Wsi.setcursor Wsi.CURSOR_CYCLE;
5776 let p = (x, y) in
5777 state.mstate <- Mzoomrect (p, p)
5779 else (
5780 match state.mstate with
5781 | Mzoomrect ((x0, y0), _) ->
5782 if abs (x-x0) > 10 && abs (y - y0) > 10
5783 then zoomrect x0 y0 x y
5784 else (
5785 state.mstate <- Mnone;
5786 Wsi.setcursor Wsi.CURSOR_INHERIT;
5787 G.postRedisplay "kill accidental zoom rect";
5789 | _ ->
5790 Wsi.setcursor Wsi.CURSOR_INHERIT;
5791 state.mstate <- Mnone
5794 | 1 when x > conf.winw - state.scrollw ->
5795 if down
5796 then
5797 let _, position, sh = state.uioh#scrollph in
5798 if y > truncate position && y < truncate (position +. sh)
5799 then state.mstate <- Mscrolly
5800 else scrolly y
5801 else
5802 state.mstate <- Mnone
5804 | 1 when y > conf.winh - state.hscrollh ->
5805 if down
5806 then
5807 let _, position, sw = state.uioh#scrollpw in
5808 if x > truncate position && x < truncate (position +. sw)
5809 then state.mstate <- Mscrollx
5810 else scrollx x
5811 else
5812 state.mstate <- Mnone
5814 | 1 ->
5815 let dest = if down then getunder x y else Unone in
5816 begin match dest with
5817 | Ulinkgoto _
5818 | Ulinkuri _
5819 | Uremote _
5820 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5821 gotounder dest
5823 | Unone when down ->
5824 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5825 state.mstate <- Mpan (x, y);
5827 | Unone | Utext _ ->
5828 if down
5829 then (
5830 if conf.angle mod 360 = 0
5831 then (
5832 state.mstate <- Msel ((x, y), (x, y));
5833 G.postRedisplay "mouse select";
5836 else (
5837 match state.mstate with
5838 | Mnone -> ()
5840 | Mzoom _ | Mscrollx | Mscrolly ->
5841 state.mstate <- Mnone
5843 | Mzoomrect ((x0, y0), _) ->
5844 zoomrect x0 y0 x y
5846 | Mpan _ ->
5847 Wsi.setcursor Wsi.CURSOR_INHERIT;
5848 state.mstate <- Mnone
5850 | Msel ((x0, y0), (x1, y1)) ->
5851 let rec loop = function
5852 | [] -> ()
5853 | l :: rest ->
5854 let inside =
5855 let a0 = l.pagedispy in
5856 let a1 = a0 + l.pagevh in
5857 let b0 = l.pagedispx in
5858 let b1 = b0 + l.pagevw in
5859 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5860 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5862 if inside
5863 then
5864 match getopaque l.pageno with
5865 | Some opaque ->
5866 begin
5867 match Ne.pipe () with
5868 | Ne.Exn exn ->
5869 showtext '!'
5870 (Printf.sprintf
5871 "can not create sel pipe: %s"
5872 (Printexc.to_string exn));
5873 | Ne.Res (r, w) ->
5874 let doclose what fd =
5875 Ne.clo fd (fun msg ->
5876 dolog "%s close failed: %s" what msg)
5879 popen conf.selcmd [r, 0; w, -1];
5880 copysel w opaque;
5881 doclose "pipe/r" r;
5882 G.postRedisplay "copysel";
5883 with exn ->
5884 dolog "can not execute %S: %s"
5885 conf.selcmd (Printexc.to_string exn);
5886 doclose "pipe/r" r;
5887 doclose "pipe/w" w;
5889 | None -> ()
5890 else loop rest
5892 loop state.layout;
5893 Wsi.setcursor Wsi.CURSOR_INHERIT;
5894 state.mstate <- Mnone;
5898 | _ -> ()
5901 let birdseyemouse button down x y mask
5902 (conf, leftx, _, hooverpageno, anchor) =
5903 match button with
5904 | 1 when down ->
5905 let rec loop = function
5906 | [] -> ()
5907 | l :: rest ->
5908 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5909 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5910 then (
5911 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5913 else loop rest
5915 loop state.layout
5916 | 3 -> ()
5917 | _ -> viewmouse button down x y mask
5920 let mouse button down x y mask =
5921 state.uioh <- state.uioh#button button down x y mask;
5924 let motion ~x ~y =
5925 state.uioh <- state.uioh#motion x y
5928 let pmotion ~x ~y =
5929 state.uioh <- state.uioh#pmotion x y;
5932 let uioh = object
5933 method display = ()
5935 method key key mask =
5936 begin match state.mode with
5937 | Textentry textentry -> textentrykeyboard key mask textentry
5938 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5939 | View -> viewkeyboard key mask
5940 | LinkNav linknav -> linknavkeyboard key mask linknav
5941 end;
5942 state.uioh
5944 method button button bstate x y mask =
5945 begin match state.mode with
5946 | LinkNav _
5947 | View -> viewmouse button bstate x y mask
5948 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5949 | Textentry _ -> ()
5950 end;
5951 state.uioh
5953 method motion x y =
5954 begin match state.mode with
5955 | Textentry _ -> ()
5956 | View | Birdseye _ | LinkNav _ ->
5957 match state.mstate with
5958 | Mzoom _ | Mnone -> ()
5960 | Mpan (x0, y0) ->
5961 let dx = x - x0
5962 and dy = y0 - y in
5963 state.mstate <- Mpan (x, y);
5964 if canpan ()
5965 then state.x <- state.x + dx;
5966 let y = clamp dy in
5967 gotoy_and_clear_text y
5969 | Msel (a, _) ->
5970 state.mstate <- Msel (a, (x, y));
5971 G.postRedisplay "motion select";
5973 | Mscrolly ->
5974 let y = min conf.winh (max 0 y) in
5975 scrolly y
5977 | Mscrollx ->
5978 let x = min conf.winw (max 0 x) in
5979 scrollx x
5981 | Mzoomrect (p0, _) ->
5982 state.mstate <- Mzoomrect (p0, (x, y));
5983 G.postRedisplay "motion zoomrect";
5984 end;
5985 state.uioh
5987 method pmotion x y =
5988 begin match state.mode with
5989 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5990 let rec loop = function
5991 | [] ->
5992 if hooverpageno != -1
5993 then (
5994 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5995 G.postRedisplay "pmotion birdseye no hoover";
5997 | l :: rest ->
5998 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5999 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6000 then (
6001 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6002 G.postRedisplay "pmotion birdseye hoover";
6004 else loop rest
6006 loop state.layout
6008 | Textentry _ -> ()
6010 | LinkNav _
6011 | View ->
6012 match state.mstate with
6013 | Mnone -> updateunder x y
6014 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6016 end;
6017 state.uioh
6019 method infochanged _ = ()
6021 method scrollph =
6022 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
6023 let p, h = scrollph state.y maxy in
6024 state.scrollw, p, h
6026 method scrollpw =
6027 let winw = conf.winw - state.scrollw - 1 in
6028 let fwinw = float winw in
6029 let sw =
6030 let sw = fwinw /. float state.w in
6031 let sw = fwinw *. sw in
6032 max sw (float conf.scrollh)
6034 let position, sw =
6035 let f = state.w+winw in
6036 let r = float (winw-state.x) /. float f in
6037 let p = fwinw *. r in
6038 p-.sw/.2., sw
6040 let sw =
6041 if position +. sw > fwinw
6042 then fwinw -. position
6043 else sw
6045 state.hscrollh, position, sw
6047 method modehash =
6048 let modename =
6049 match state.mode with
6050 | LinkNav _ -> "links"
6051 | Textentry _ -> "textentry"
6052 | Birdseye _ -> "birdseye"
6053 | View -> "view"
6055 findkeyhash conf modename
6056 end;;
6058 module Config =
6059 struct
6060 open Parser
6062 let fontpath = ref "";;
6064 module KeyMap =
6065 Map.Make (struct type t = (int * int) let compare = compare end);;
6067 let unent s =
6068 let l = String.length s in
6069 let b = Buffer.create l in
6070 unent b s 0 l;
6071 Buffer.contents b;
6074 let home =
6075 try Sys.getenv "HOME"
6076 with exn ->
6077 prerr_endline
6078 ("Can not determine home directory location: " ^
6079 Printexc.to_string exn);
6083 let modifier_of_string = function
6084 | "alt" -> Wsi.altmask
6085 | "shift" -> Wsi.shiftmask
6086 | "ctrl" | "control" -> Wsi.ctrlmask
6087 | "meta" -> Wsi.metamask
6088 | _ -> 0
6091 let key_of_string =
6092 let r = Str.regexp "-" in
6093 fun s ->
6094 let elems = Str.full_split r s in
6095 let f n k m =
6096 let g s =
6097 let m1 = modifier_of_string s in
6098 if m1 = 0
6099 then (Wsi.namekey s, m)
6100 else (k, m lor m1)
6101 in function
6102 | Str.Delim s when n land 1 = 0 -> g s
6103 | Str.Text s -> g s
6104 | Str.Delim _ -> (k, m)
6106 let rec loop n k m = function
6107 | [] -> (k, m)
6108 | x :: xs ->
6109 let k, m = f n k m x in
6110 loop (n+1) k m xs
6112 loop 0 0 0 elems
6115 let keys_of_string =
6116 let r = Str.regexp "[ \t]" in
6117 fun s ->
6118 let elems = Str.split r s in
6119 List.map key_of_string elems
6122 let copykeyhashes c =
6123 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6126 let config_of c attrs =
6127 let apply c k v =
6129 match k with
6130 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6131 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6132 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6133 | "preload" -> { c with preload = bool_of_string v }
6134 | "page-bias" -> { c with pagebias = int_of_string v }
6135 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6136 | "horizontal-scroll-step" ->
6137 { c with hscrollstep = max (int_of_string v) 1 }
6138 | "auto-scroll-step" ->
6139 { c with autoscrollstep = max 0 (int_of_string v) }
6140 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6141 | "crop-hack" -> { c with crophack = bool_of_string v }
6142 | "throttle" ->
6143 let mw =
6144 match String.lowercase v with
6145 | "true" -> Some infinity
6146 | "false" -> None
6147 | f -> Some (float_of_string f)
6149 { c with maxwait = mw}
6150 | "highlight-links" -> { c with hlinks = bool_of_string v }
6151 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6152 | "vertical-margin" ->
6153 { c with interpagespace = max 0 (int_of_string v) }
6154 | "zoom" ->
6155 let zoom = float_of_string v /. 100. in
6156 let zoom = max zoom 0.0 in
6157 { c with zoom = zoom }
6158 | "presentation" -> { c with presentation = bool_of_string v }
6159 | "rotation-angle" -> { c with angle = int_of_string v }
6160 | "width" -> { c with winw = max 20 (int_of_string v) }
6161 | "height" -> { c with winh = max 20 (int_of_string v) }
6162 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6163 | "proportional-display" -> { c with proportional = bool_of_string v }
6164 | "pixmap-cache-size" ->
6165 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6166 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6167 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6168 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6169 | "persistent-location" -> { c with jumpback = bool_of_string v }
6170 | "background-color" -> { c with bgcolor = color_of_string v }
6171 | "scrollbar-in-presentation" ->
6172 { c with scrollbarinpm = bool_of_string v }
6173 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6174 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6175 | "mupdf-store-size" ->
6176 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6177 | "checkers" -> { c with checkers = bool_of_string v }
6178 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6179 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6180 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6181 | "uri-launcher" -> { c with urilauncher = unent v }
6182 | "path-launcher" -> { c with pathlauncher = unent v }
6183 | "color-space" -> { c with colorspace = colorspace_of_string v }
6184 | "invert-colors" -> { c with invert = bool_of_string v }
6185 | "brightness" -> { c with colorscale = float_of_string v }
6186 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6187 | "ghyllscroll" ->
6188 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6189 | "columns" ->
6190 let (n, _, _) as nab = multicolumns_of_string v in
6191 if n < 0
6192 then { c with columns = Csplit (-n, [||]) }
6193 else { c with columns = Cmulti (nab, [||]) }
6194 | "birds-eye-columns" ->
6195 { c with beyecolumns = Some (max (int_of_string v) 2) }
6196 | "selection-command" -> { c with selcmd = unent v }
6197 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6198 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6199 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6200 | "use-pbo" -> { c with usepbo = bool_of_string v }
6201 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6202 | _ -> c
6203 with exn ->
6204 prerr_endline ("Error processing attribute (`" ^
6205 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6208 let rec fold c = function
6209 | [] -> c
6210 | (k, v) :: rest ->
6211 let c = apply c k v in
6212 fold c rest
6214 fold { c with keyhashes = copykeyhashes c } attrs;
6217 let fromstring f pos n v d =
6218 try f v
6219 with exn ->
6220 dolog "Error processing attribute (%S=%S) at %d\n%s"
6221 n v pos (Printexc.to_string exn)
6226 let bookmark_of attrs =
6227 let rec fold title page rely visy = function
6228 | ("title", v) :: rest -> fold v page rely visy rest
6229 | ("page", v) :: rest -> fold title v rely visy rest
6230 | ("rely", v) :: rest -> fold title page v visy rest
6231 | ("visy", v) :: rest -> fold title page rely v rest
6232 | _ :: rest -> fold title page rely visy rest
6233 | [] -> title, page, rely, visy
6235 fold "invalid" "0" "0" "0" attrs
6238 let doc_of attrs =
6239 let rec fold path page rely pan visy = function
6240 | ("path", v) :: rest -> fold v page rely pan visy rest
6241 | ("page", v) :: rest -> fold path v rely pan visy rest
6242 | ("rely", v) :: rest -> fold path page v pan visy rest
6243 | ("pan", v) :: rest -> fold path page rely v visy rest
6244 | ("visy", v) :: rest -> fold path page rely pan v rest
6245 | _ :: rest -> fold path page rely pan visy rest
6246 | [] -> path, page, rely, pan, visy
6248 fold "" "0" "0" "0" "0" attrs
6251 let map_of attrs =
6252 let rec fold rs ls = function
6253 | ("out", v) :: rest -> fold v ls rest
6254 | ("in", v) :: rest -> fold rs v rest
6255 | _ :: rest -> fold ls rs rest
6256 | [] -> ls, rs
6258 fold "" "" attrs
6261 let setconf dst src =
6262 dst.scrollbw <- src.scrollbw;
6263 dst.scrollh <- src.scrollh;
6264 dst.icase <- src.icase;
6265 dst.preload <- src.preload;
6266 dst.pagebias <- src.pagebias;
6267 dst.verbose <- src.verbose;
6268 dst.scrollstep <- src.scrollstep;
6269 dst.maxhfit <- src.maxhfit;
6270 dst.crophack <- src.crophack;
6271 dst.autoscrollstep <- src.autoscrollstep;
6272 dst.maxwait <- src.maxwait;
6273 dst.hlinks <- src.hlinks;
6274 dst.underinfo <- src.underinfo;
6275 dst.interpagespace <- src.interpagespace;
6276 dst.zoom <- src.zoom;
6277 dst.presentation <- src.presentation;
6278 dst.angle <- src.angle;
6279 dst.winw <- src.winw;
6280 dst.winh <- src.winh;
6281 dst.savebmarks <- src.savebmarks;
6282 dst.memlimit <- src.memlimit;
6283 dst.proportional <- src.proportional;
6284 dst.texcount <- src.texcount;
6285 dst.sliceheight <- src.sliceheight;
6286 dst.thumbw <- src.thumbw;
6287 dst.jumpback <- src.jumpback;
6288 dst.bgcolor <- src.bgcolor;
6289 dst.scrollbarinpm <- src.scrollbarinpm;
6290 dst.tilew <- src.tilew;
6291 dst.tileh <- src.tileh;
6292 dst.mustoresize <- src.mustoresize;
6293 dst.checkers <- src.checkers;
6294 dst.aalevel <- src.aalevel;
6295 dst.trimmargins <- src.trimmargins;
6296 dst.trimfuzz <- src.trimfuzz;
6297 dst.urilauncher <- src.urilauncher;
6298 dst.colorspace <- src.colorspace;
6299 dst.invert <- src.invert;
6300 dst.colorscale <- src.colorscale;
6301 dst.redirectstderr <- src.redirectstderr;
6302 dst.ghyllscroll <- src.ghyllscroll;
6303 dst.columns <- src.columns;
6304 dst.beyecolumns <- src.beyecolumns;
6305 dst.selcmd <- src.selcmd;
6306 dst.updatecurs <- src.updatecurs;
6307 dst.pathlauncher <- src.pathlauncher;
6308 dst.keyhashes <- copykeyhashes src;
6309 dst.hfsize <- src.hfsize;
6310 dst.hscrollstep <- src.hscrollstep;
6311 dst.pgscale <- src.pgscale;
6312 dst.usepbo <- src.usepbo;
6313 dst.wheelbypage <- src.wheelbypage;
6316 let get s =
6317 let h = Hashtbl.create 10 in
6318 let dc = { defconf with angle = defconf.angle } in
6319 let rec toplevel v t spos _ =
6320 match t with
6321 | Vdata | Vcdata | Vend -> v
6322 | Vopen ("llppconfig", _, closed) ->
6323 if closed
6324 then v
6325 else { v with f = llppconfig }
6326 | Vopen _ ->
6327 error "unexpected subelement at top level" s spos
6328 | Vclose _ -> error "unexpected close at top level" s spos
6330 and llppconfig v t spos _ =
6331 match t with
6332 | Vdata | Vcdata -> v
6333 | Vend -> error "unexpected end of input in llppconfig" s spos
6334 | Vopen ("defaults", attrs, closed) ->
6335 let c = config_of dc attrs in
6336 setconf dc c;
6337 if closed
6338 then v
6339 else { v with f = defaults }
6341 | Vopen ("ui-font", attrs, closed) ->
6342 let rec getsize size = function
6343 | [] -> size
6344 | ("size", v) :: rest ->
6345 let size =
6346 fromstring int_of_string spos "size" v fstate.fontsize in
6347 getsize size rest
6348 | l -> getsize size l
6350 fstate.fontsize <- getsize fstate.fontsize attrs;
6351 if closed
6352 then v
6353 else { v with f = uifont (Buffer.create 10) }
6355 | Vopen ("doc", attrs, closed) ->
6356 let pathent, spage, srely, span, svisy = doc_of attrs in
6357 let path = unent pathent
6358 and pageno = fromstring int_of_string spos "page" spage 0
6359 and rely = fromstring float_of_string spos "rely" srely 0.0
6360 and pan = fromstring int_of_string spos "pan" span 0
6361 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6362 let c = config_of dc attrs in
6363 let anchor = (pageno, rely, visy) in
6364 if closed
6365 then (Hashtbl.add h path (c, [], pan, anchor); v)
6366 else { v with f = doc path pan anchor c [] }
6368 | Vopen _ ->
6369 error "unexpected subelement in llppconfig" s spos
6371 | Vclose "llppconfig" -> { v with f = toplevel }
6372 | Vclose _ -> error "unexpected close in llppconfig" s spos
6374 and defaults v t spos _ =
6375 match t with
6376 | Vdata | Vcdata -> v
6377 | Vend -> error "unexpected end of input in defaults" s spos
6378 | Vopen ("keymap", attrs, closed) ->
6379 let modename =
6380 try List.assoc "mode" attrs
6381 with Not_found -> "global" in
6382 if closed
6383 then v
6384 else
6385 let ret keymap =
6386 let h = findkeyhash dc modename in
6387 KeyMap.iter (Hashtbl.replace h) keymap;
6388 defaults
6390 { v with f = pkeymap ret KeyMap.empty }
6392 | Vopen (_, _, _) ->
6393 error "unexpected subelement in defaults" s spos
6395 | Vclose "defaults" ->
6396 { v with f = llppconfig }
6398 | Vclose _ -> error "unexpected close in defaults" s spos
6400 and uifont b v t spos epos =
6401 match t with
6402 | Vdata | Vcdata ->
6403 Buffer.add_substring b s spos (epos - spos);
6405 | Vopen (_, _, _) ->
6406 error "unexpected subelement in ui-font" s spos
6407 | Vclose "ui-font" ->
6408 if String.length !fontpath = 0
6409 then fontpath := Buffer.contents b;
6410 { v with f = llppconfig }
6411 | Vclose _ -> error "unexpected close in ui-font" s spos
6412 | Vend -> error "unexpected end of input in ui-font" s spos
6414 and doc path pan anchor c bookmarks v t spos _ =
6415 match t with
6416 | Vdata | Vcdata -> v
6417 | Vend -> error "unexpected end of input in doc" s spos
6418 | Vopen ("bookmarks", _, closed) ->
6419 if closed
6420 then v
6421 else { v with f = pbookmarks path pan anchor c bookmarks }
6423 | Vopen ("keymap", attrs, closed) ->
6424 let modename =
6425 try List.assoc "mode" attrs
6426 with Not_found -> "global"
6428 if closed
6429 then v
6430 else
6431 let ret keymap =
6432 let h = findkeyhash c modename in
6433 KeyMap.iter (Hashtbl.replace h) keymap;
6434 doc path pan anchor c bookmarks
6436 { v with f = pkeymap ret KeyMap.empty }
6438 | Vopen (_, _, _) ->
6439 error "unexpected subelement in doc" s spos
6441 | Vclose "doc" ->
6442 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6443 { v with f = llppconfig }
6445 | Vclose _ -> error "unexpected close in doc" s spos
6447 and pkeymap ret keymap v t spos _ =
6448 match t with
6449 | Vdata | Vcdata -> v
6450 | Vend -> error "unexpected end of input in keymap" s spos
6451 | Vopen ("map", attrs, closed) ->
6452 let r, l = map_of attrs in
6453 let kss = fromstring keys_of_string spos "in" r [] in
6454 let lss = fromstring keys_of_string spos "out" l [] in
6455 let keymap =
6456 match kss with
6457 | [] -> keymap
6458 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6459 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6461 if closed
6462 then { v with f = pkeymap ret keymap }
6463 else
6464 let f () = v in
6465 { v with f = skip "map" f }
6467 | Vopen _ ->
6468 error "unexpected subelement in keymap" s spos
6470 | Vclose "keymap" ->
6471 { v with f = ret keymap }
6473 | Vclose _ -> error "unexpected close in keymap" s spos
6475 and pbookmarks path pan anchor c bookmarks v t spos _ =
6476 match t with
6477 | Vdata | Vcdata -> v
6478 | Vend -> error "unexpected end of input in bookmarks" s spos
6479 | Vopen ("item", attrs, closed) ->
6480 let titleent, spage, srely, svisy = bookmark_of attrs in
6481 let page = fromstring int_of_string spos "page" spage 0
6482 and rely = fromstring float_of_string spos "rely" srely 0.0
6483 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6484 let bookmarks =
6485 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6487 if closed
6488 then { v with f = pbookmarks path pan anchor c bookmarks }
6489 else
6490 let f () = v in
6491 { v with f = skip "item" f }
6493 | Vopen _ ->
6494 error "unexpected subelement in bookmarks" s spos
6496 | Vclose "bookmarks" ->
6497 { v with f = doc path pan anchor c bookmarks }
6499 | Vclose _ -> error "unexpected close in bookmarks" s spos
6501 and skip tag f v t spos _ =
6502 match t with
6503 | Vdata | Vcdata -> v
6504 | Vend ->
6505 error ("unexpected end of input in skipped " ^ tag) s spos
6506 | Vopen (tag', _, closed) ->
6507 if closed
6508 then v
6509 else
6510 let f' () = { v with f = skip tag f } in
6511 { v with f = skip tag' f' }
6512 | Vclose ctag ->
6513 if tag = ctag
6514 then f ()
6515 else error ("unexpected close in skipped " ^ tag) s spos
6518 parse { f = toplevel; accu = () } s;
6519 h, dc;
6522 let do_load f ic =
6524 let len = in_channel_length ic in
6525 let s = String.create len in
6526 really_input ic s 0 len;
6527 f s;
6528 with
6529 | Parse_error (msg, s, pos) ->
6530 let subs = subs s pos in
6531 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6532 failwith ("parse error: " ^ s)
6534 | exn ->
6535 failwith ("config load error: " ^ Printexc.to_string exn)
6538 let defconfpath =
6539 let dir =
6541 let dir = Filename.concat home ".config" in
6542 if Sys.is_directory dir then dir else home
6543 with _ -> home
6545 Filename.concat dir "llpp.conf"
6548 let confpath = ref defconfpath;;
6550 let load1 f =
6551 if Sys.file_exists !confpath
6552 then
6553 match
6554 (try Some (open_in_bin !confpath)
6555 with exn ->
6556 prerr_endline
6557 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6558 Printexc.to_string exn);
6559 None
6561 with
6562 | Some ic ->
6563 let success =
6565 f (do_load get ic)
6566 with exn ->
6567 prerr_endline
6568 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6569 Printexc.to_string exn);
6570 false
6572 close_in ic;
6573 success
6575 | None -> false
6576 else
6577 f (Hashtbl.create 0, defconf)
6580 let load () =
6581 let f (h, dc) =
6582 let pc, pb, px, pa =
6584 Hashtbl.find h (Filename.basename state.path)
6585 with Not_found -> dc, [], 0, emptyanchor
6587 setconf defconf dc;
6588 setconf conf pc;
6589 state.bookmarks <- pb;
6590 state.x <- px;
6591 state.scrollw <- conf.scrollbw;
6592 if conf.jumpback
6593 then state.anchor <- pa;
6594 cbput state.hists.nav pa;
6595 true
6597 load1 f
6600 let add_attrs bb always dc c =
6601 let ob s a b =
6602 if always || a != b
6603 then Printf.bprintf bb "\n %s='%b'" s a
6604 and oi s a b =
6605 if always || a != b
6606 then Printf.bprintf bb "\n %s='%d'" s a
6607 and oI s a b =
6608 if always || a != b
6609 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6610 and oz s a b =
6611 if always || a <> b
6612 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6613 and oF s a b =
6614 if always || a <> b
6615 then Printf.bprintf bb "\n %s='%f'" s a
6616 and oc s a b =
6617 if always || a <> b
6618 then
6619 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6620 and oC s a b =
6621 if always || a <> b
6622 then
6623 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6624 and oR s a b =
6625 if always || a <> b
6626 then
6627 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6628 and os s a b =
6629 if always || a <> b
6630 then
6631 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6632 and og s a b =
6633 if always || a <> b
6634 then
6635 match a with
6636 | None -> ()
6637 | Some (_N, _A, _B) ->
6638 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6639 and oW s a b =
6640 if always || a <> b
6641 then
6642 let v =
6643 match a with
6644 | None -> "false"
6645 | Some f ->
6646 if f = infinity
6647 then "true"
6648 else string_of_float f
6650 Printf.bprintf bb "\n %s='%s'" s v
6651 and oco s a b =
6652 if always || a <> b
6653 then
6654 match a with
6655 | Cmulti ((n, a, b), _) when n > 1 ->
6656 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6657 | Csplit (n, _) when n > 1 ->
6658 Printf.bprintf bb "\n %s='%d'" s ~-n
6659 | _ -> ()
6660 and obeco s a b =
6661 if always || a <> b
6662 then
6663 match a with
6664 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6665 | _ -> ()
6667 let w, h =
6668 if always
6669 then dc.winw, dc.winh
6670 else
6671 match state.fullscreen with
6672 | Some wh -> wh
6673 | None -> c.winw, c.winh
6675 oi "width" w dc.winw;
6676 oi "height" h dc.winh;
6677 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6678 oi "scroll-handle-height" c.scrollh dc.scrollh;
6679 ob "case-insensitive-search" c.icase dc.icase;
6680 ob "preload" c.preload dc.preload;
6681 oi "page-bias" c.pagebias dc.pagebias;
6682 oi "scroll-step" c.scrollstep dc.scrollstep;
6683 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6684 ob "max-height-fit" c.maxhfit dc.maxhfit;
6685 ob "crop-hack" c.crophack dc.crophack;
6686 oW "throttle" c.maxwait dc.maxwait;
6687 ob "highlight-links" c.hlinks dc.hlinks;
6688 ob "under-cursor-info" c.underinfo dc.underinfo;
6689 oi "vertical-margin" c.interpagespace dc.interpagespace;
6690 oz "zoom" c.zoom dc.zoom;
6691 ob "presentation" c.presentation dc.presentation;
6692 oi "rotation-angle" c.angle dc.angle;
6693 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6694 ob "proportional-display" c.proportional dc.proportional;
6695 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6696 oi "tex-count" c.texcount dc.texcount;
6697 oi "slice-height" c.sliceheight dc.sliceheight;
6698 oi "thumbnail-width" c.thumbw dc.thumbw;
6699 ob "persistent-location" c.jumpback dc.jumpback;
6700 oc "background-color" c.bgcolor dc.bgcolor;
6701 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6702 oi "tile-width" c.tilew dc.tilew;
6703 oi "tile-height" c.tileh dc.tileh;
6704 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6705 ob "checkers" c.checkers dc.checkers;
6706 oi "aalevel" c.aalevel dc.aalevel;
6707 ob "trim-margins" c.trimmargins dc.trimmargins;
6708 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6709 os "uri-launcher" c.urilauncher dc.urilauncher;
6710 os "path-launcher" c.pathlauncher dc.pathlauncher;
6711 oC "color-space" c.colorspace dc.colorspace;
6712 ob "invert-colors" c.invert dc.invert;
6713 oF "brightness" c.colorscale dc.colorscale;
6714 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6715 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6716 oco "columns" c.columns dc.columns;
6717 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6718 os "selection-command" c.selcmd dc.selcmd;
6719 ob "update-cursor" c.updatecurs dc.updatecurs;
6720 oi "hint-font-size" c.hfsize dc.hfsize;
6721 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6722 oF "page-scroll-scale" c.pgscale dc.pgscale;
6723 ob "use-pbo" c.usepbo dc.usepbo;
6724 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6727 let keymapsbuf always dc c =
6728 let bb = Buffer.create 16 in
6729 let rec loop = function
6730 | [] -> ()
6731 | (modename, h) :: rest ->
6732 let dh = findkeyhash dc modename in
6733 if always || h <> dh
6734 then (
6735 if Hashtbl.length h > 0
6736 then (
6737 if Buffer.length bb > 0
6738 then Buffer.add_char bb '\n';
6739 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6740 Hashtbl.iter (fun i o ->
6741 let isdifferent = always ||
6743 let dO = Hashtbl.find dh i in
6744 dO <> o
6745 with Not_found -> true
6747 if isdifferent
6748 then
6749 let addkm (k, m) =
6750 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6751 if Wsi.withalt m then Buffer.add_string bb "alt-";
6752 if Wsi.withshift m then Buffer.add_string bb "shift-";
6753 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6754 Buffer.add_string bb (Wsi.keyname k);
6756 let addkms l =
6757 let rec loop = function
6758 | [] -> ()
6759 | km :: [] -> addkm km
6760 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6762 loop l
6764 Buffer.add_string bb "<map in='";
6765 addkm i;
6766 match o with
6767 | KMinsrt km ->
6768 Buffer.add_string bb "' out='";
6769 addkm km;
6770 Buffer.add_string bb "'/>\n"
6772 | KMinsrl kms ->
6773 Buffer.add_string bb "' out='";
6774 addkms kms;
6775 Buffer.add_string bb "'/>\n"
6777 | KMmulti (ins, kms) ->
6778 Buffer.add_char bb ' ';
6779 addkms ins;
6780 Buffer.add_string bb "' out='";
6781 addkms kms;
6782 Buffer.add_string bb "'/>\n"
6783 ) h;
6784 Buffer.add_string bb "</keymap>";
6787 loop rest
6789 loop c.keyhashes;
6793 let save () =
6794 let uifontsize = fstate.fontsize in
6795 let bb = Buffer.create 32768 in
6796 let f (h, dc) =
6797 let dc = if conf.bedefault then conf else dc in
6798 Buffer.add_string bb "<llppconfig>\n";
6800 if String.length !fontpath > 0
6801 then
6802 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6803 uifontsize
6804 !fontpath
6805 else (
6806 if uifontsize <> 14
6807 then
6808 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6811 Buffer.add_string bb "<defaults ";
6812 add_attrs bb true dc dc;
6813 let kb = keymapsbuf true dc dc in
6814 if Buffer.length kb > 0
6815 then (
6816 Buffer.add_string bb ">\n";
6817 Buffer.add_buffer bb kb;
6818 Buffer.add_string bb "\n</defaults>\n";
6820 else Buffer.add_string bb "/>\n";
6822 let adddoc path pan anchor c bookmarks =
6823 if bookmarks == [] && c = dc && anchor = emptyanchor
6824 then ()
6825 else (
6826 Printf.bprintf bb "<doc path='%s'"
6827 (enent path 0 (String.length path));
6829 if anchor <> emptyanchor
6830 then (
6831 let n, rely, visy = anchor in
6832 Printf.bprintf bb " page='%d'" n;
6833 if rely > 1e-6
6834 then
6835 Printf.bprintf bb " rely='%f'" rely
6837 if abs_float visy > 1e-6
6838 then
6839 Printf.bprintf bb " visy='%f'" visy
6843 if pan != 0
6844 then Printf.bprintf bb " pan='%d'" pan;
6846 add_attrs bb false dc c;
6847 let kb = keymapsbuf false dc c in
6849 begin match bookmarks with
6850 | [] ->
6851 if Buffer.length kb > 0
6852 then (
6853 Buffer.add_string bb ">\n";
6854 Buffer.add_buffer bb kb;
6855 Buffer.add_string bb "\n</doc>\n";
6857 else Buffer.add_string bb "/>\n"
6858 | _ ->
6859 Buffer.add_string bb ">\n<bookmarks>\n";
6860 List.iter (fun (title, _level, (page, rely, visy)) ->
6861 Printf.bprintf bb
6862 "<item title='%s' page='%d'"
6863 (enent title 0 (String.length title))
6864 page
6866 if rely > 1e-6
6867 then
6868 Printf.bprintf bb " rely='%f'" rely
6870 if abs_float visy > 1e-6
6871 then
6872 Printf.bprintf bb " visy='%f'" visy
6874 Buffer.add_string bb "/>\n";
6875 ) bookmarks;
6876 Buffer.add_string bb "</bookmarks>";
6877 if Buffer.length kb > 0
6878 then (
6879 Buffer.add_string bb "\n";
6880 Buffer.add_buffer bb kb;
6882 Buffer.add_string bb "\n</doc>\n";
6883 end;
6887 let pan, conf =
6888 match state.mode with
6889 | Birdseye (c, pan, _, _, _) ->
6890 let beyecolumns =
6891 match conf.columns with
6892 | Cmulti ((c, _, _), _) -> Some c
6893 | Csingle _ -> None
6894 | Csplit _ -> None
6895 and columns =
6896 match c.columns with
6897 | Cmulti (c, _) -> Cmulti (c, [||])
6898 | Csingle _ -> Csingle [||]
6899 | Csplit _ -> failwith "quit from bird's eye while split"
6901 pan, { c with beyecolumns = beyecolumns; columns = columns }
6902 | _ -> state.x, conf
6904 let basename = Filename.basename state.path in
6905 adddoc basename pan (getanchor ())
6906 (let conf =
6907 let autoscrollstep =
6908 match state.autoscroll with
6909 | Some step -> step
6910 | None -> conf.autoscrollstep
6912 match state.mode with
6913 | Birdseye (bc, _, _, _, _) ->
6914 { conf with
6915 zoom = bc.zoom;
6916 presentation = bc.presentation;
6917 interpagespace = bc.interpagespace;
6918 maxwait = bc.maxwait;
6919 autoscrollstep = autoscrollstep }
6920 | _ -> { conf with autoscrollstep = autoscrollstep }
6921 in conf)
6922 (if conf.savebmarks then state.bookmarks else []);
6924 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6925 if basename <> path
6926 then adddoc path x anchor c bookmarks
6927 ) h;
6928 Buffer.add_string bb "</llppconfig>\n";
6929 true;
6931 if load1 f && Buffer.length bb > 0
6932 then
6934 let tmp = !confpath ^ ".tmp" in
6935 let oc = open_out_bin tmp in
6936 Buffer.output_buffer oc bb;
6937 close_out oc;
6938 Unix.rename tmp !confpath;
6939 with exn ->
6940 prerr_endline
6941 ("error while saving configuration: " ^ Printexc.to_string exn)
6943 end;;
6945 let () =
6946 let trimcachepath = ref "" in
6947 Arg.parse
6948 (Arg.align
6949 [("-p", Arg.String (fun s -> state.password <- s) ,
6950 "<password> Set password");
6952 ("-f", Arg.String (fun s -> Config.fontpath := s),
6953 "<path> Set path to the user interface font");
6955 ("-c", Arg.String (fun s -> Config.confpath := s),
6956 "<path> Set path to the configuration file");
6958 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6959 "<path> Set path to the trim cache file");
6961 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6962 "<named destination> Set named destination");
6964 ("-v", Arg.Unit (fun () ->
6965 Printf.printf
6966 "%s\nconfiguration path: %s\n"
6967 (version ())
6968 Config.defconfpath
6970 exit 0), " Print version and exit");
6973 (fun s -> state.path <- s)
6974 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6976 if String.length state.path = 0
6977 then (prerr_endline "file name missing"; exit 1);
6979 if not (Config.load ())
6980 then prerr_endline "failed to load configuration";
6982 let globalkeyhash = findkeyhash conf "global" in
6983 let wsfd, winw, winh = Wsi.init (object
6984 method expose =
6985 state.wthack <- false;
6986 if nogeomcmds state.geomcmds || platform == Posx
6987 then display ()
6988 else (
6989 GlClear.color (scalecolor2 conf.bgcolor);
6990 GlClear.clear [`color];
6992 method display = display ()
6993 method reshape w h = reshape w h
6994 method mouse b d x y m = mouse b d x y m
6995 method motion x y = state.mpos <- (x, y); motion x y
6996 method pmotion x y = state.mpos <- (x, y); pmotion x y
6997 method key k m =
6998 let mascm = m land (
6999 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7000 ) in
7001 match state.keystate with
7002 | KSnone ->
7003 let km = k, mascm in
7004 begin
7005 match
7006 let modehash = state.uioh#modehash in
7007 try Hashtbl.find modehash km
7008 with Not_found ->
7009 try Hashtbl.find globalkeyhash km
7010 with Not_found -> KMinsrt (k, m)
7011 with
7012 | KMinsrt (k, m) -> keyboard k m
7013 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7014 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7016 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7017 List.iter (fun (k, m) -> keyboard k m) insrt;
7018 state.keystate <- KSnone
7019 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7020 state.keystate <- KSinto (keys, insrt)
7021 | _ ->
7022 state.keystate <- KSnone
7024 method enter x y = state.mpos <- (x, y); pmotion x y
7025 method leave = state.mpos <- (-1, -1)
7026 method quit = raise Quit
7027 end) conf.winw conf.winh (platform = Posx) in
7029 state.wsfd <- wsfd;
7031 if not (
7032 List.exists GlMisc.check_extension
7033 [ "GL_ARB_texture_rectangle"
7034 ; "GL_EXT_texture_recangle"
7035 ; "GL_NV_texture_rectangle" ]
7037 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7039 let cr, sw =
7040 match Ne.pipe () with
7041 | Ne.Exn exn ->
7042 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
7043 exit 1
7044 | Ne.Res rw -> rw
7045 and sr, cw =
7046 match Ne.pipe () with
7047 | Ne.Exn exn ->
7048 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7049 exit 1
7050 | Ne.Res rw -> rw
7053 cloexec cr;
7054 cloexec sw;
7055 cloexec sr;
7056 cloexec cw;
7058 setcheckers conf.checkers;
7059 redirectstderr ();
7061 init (cr, cw) (
7062 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7063 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7064 !Config.fontpath, !trimcachepath,
7065 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7067 state.sr <- sr;
7068 state.sw <- sw;
7069 state.text <- "Opening " ^ (mbtoutf8 state.path);
7070 reshape winw winh;
7071 opendoc state.path state.password state.nameddest;
7072 state.uioh <- uioh;
7074 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7076 let rec loop deadline =
7077 let r =
7078 match state.errfd with
7079 | None -> [state.sr; state.wsfd]
7080 | Some fd -> [state.sr; state.wsfd; fd]
7082 if state.redisplay
7083 then (
7084 state.redisplay <- false;
7085 display ();
7087 let timeout =
7088 let now = now () in
7089 if deadline > now
7090 then (
7091 if deadline = infinity
7092 then ~-.1.0
7093 else max 0.0 (deadline -. now)
7095 else 0.0
7097 let r, _, _ =
7098 try Unix.select r [] [] timeout
7099 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7101 begin match r with
7102 | [] ->
7103 state.ghyll None;
7104 let newdeadline =
7105 if state.ghyll == noghyll
7106 then
7107 match state.autoscroll with
7108 | Some step when step != 0 ->
7109 let y = state.y + step in
7110 let y =
7111 if y < 0
7112 then state.maxy
7113 else if y >= state.maxy then 0 else y
7115 gotoy y;
7116 if state.mode = View
7117 then state.text <- "";
7118 deadline +. 0.01
7119 | _ -> infinity
7120 else deadline +. 0.01
7122 loop newdeadline
7124 | l ->
7125 let rec checkfds = function
7126 | [] -> ()
7127 | fd :: rest when fd = state.sr ->
7128 let cmd = readcmd state.sr in
7129 act cmd;
7130 checkfds rest
7132 | fd :: rest when fd = state.wsfd ->
7133 Wsi.readresp fd;
7134 checkfds rest
7136 | fd :: rest ->
7137 let s = String.create 80 in
7138 let n = Unix.read fd s 0 80 in
7139 if conf.redirectstderr
7140 then (
7141 Buffer.add_substring state.errmsgs s 0 n;
7142 state.newerrmsgs <- true;
7143 state.redisplay <- true;
7145 else (
7146 prerr_string (String.sub s 0 n);
7147 flush stderr;
7149 checkfds rest
7151 checkfds l;
7152 let newdeadline =
7153 let deadline1 =
7154 if deadline = infinity
7155 then now () +. 0.01
7156 else deadline
7158 match state.autoscroll with
7159 | Some step when step != 0 -> deadline1
7160 | _ -> if state.ghyll == noghyll then infinity else deadline1
7162 loop newdeadline
7163 end;
7166 loop infinity;
7167 with Quit ->
7168 Config.save ();