Try to avoid flickering on reload
[llpp.git] / main.ml
blob8002d074dffdd26371d284e2fc564dcd25df0c43
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
348 and columns =
349 | Csingle of singlecolumn
350 | Cmulti of multicolumns
351 | Csplit of splitcolumns
354 type anchor = pageno * top * dtop;;
356 type outline = string * int * anchor;;
358 type rect = float * float * float * float * float * float * float * float;;
360 type tile = opaque * pixmapsize * elapsed
361 and elapsed = float;;
362 type pagemapkey = pageno * gen;;
363 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
364 and row = int
365 and col = int;;
367 let emptyanchor = (0, 0.0, 0.0);;
369 type infochange = | Memused | Docinfo | Pdim;;
371 class type uioh = object
372 method display : unit
373 method key : int -> int -> uioh
374 method button : int -> bool -> int -> int -> int -> uioh
375 method motion : int -> int -> uioh
376 method pmotion : int -> int -> uioh
377 method infochanged : infochange -> unit
378 method scrollpw : (int * float * float)
379 method scrollph : (int * float * float)
380 method modehash : keyhash
381 end;;
383 type mode =
384 | Birdseye of (conf * leftx * pageno * pageno * anchor)
385 | Textentry of (textentry * onleave)
386 | View
387 | LinkNav of linktarget
388 and onleave = leavetextentrystatus -> unit
389 and leavetextentrystatus = | Cancel | Confirm
390 and helpitem = string * int * action
391 and action =
392 | Noaction
393 | Action of (uioh -> uioh)
394 and linktarget =
395 | Ltexact of (pageno * int)
396 | Ltgendir of int
399 let isbirdseye = function Birdseye _ -> true | _ -> false;;
400 let istextentry = function Textentry _ -> true | _ -> false;;
402 type currently =
403 | Idle
404 | Loading of (page * gen)
405 | Tiling of (
406 page * opaque * colorspace * angle * gen * col * row * width * height
408 | Outlining of outline list
411 let emptykeyhash = Hashtbl.create 0;;
412 let nouioh : uioh = object (self)
413 method display = ()
414 method key _ _ = self
415 method button _ _ _ _ _ = self
416 method motion _ _ = self
417 method pmotion _ _ = self
418 method infochanged _ = ()
419 method scrollpw = (0, nan, nan)
420 method scrollph = (0, nan, nan)
421 method modehash = emptykeyhash
422 end;;
424 type state =
425 { mutable sr : Unix.file_descr
426 ; mutable sw : Unix.file_descr
427 ; mutable wsfd : Unix.file_descr
428 ; mutable errfd : Unix.file_descr option
429 ; mutable stderr : Unix.file_descr
430 ; mutable errmsgs : Buffer.t
431 ; mutable newerrmsgs : bool
432 ; mutable w : int
433 ; mutable x : int
434 ; mutable y : int
435 ; mutable scrollw : int
436 ; mutable hscrollh : int
437 ; mutable anchor : anchor
438 ; mutable ranchors : (string * string * anchor) list
439 ; mutable maxy : int
440 ; mutable layout : page list
441 ; pagemap : (pagemapkey, opaque) Hashtbl.t
442 ; tilemap : (tilemapkey, tile) Hashtbl.t
443 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
444 ; mutable pdims : (pageno * width * height * leftx) list
445 ; mutable pagecount : int
446 ; mutable currently : currently
447 ; mutable mstate : mstate
448 ; mutable searchpattern : string
449 ; mutable rects : (pageno * recttype * rect) list
450 ; mutable rects1 : (pageno * recttype * rect) list
451 ; mutable text : string
452 ; mutable fullscreen : (width * height) option
453 ; mutable mode : mode
454 ; mutable uioh : uioh
455 ; mutable outlines : outline array
456 ; mutable bookmarks : outline list
457 ; mutable path : string
458 ; mutable password : string
459 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
460 ; mutable memused : memsize
461 ; mutable gen : gen
462 ; mutable throttle : (page list * int * float) option
463 ; mutable autoscroll : int option
464 ; mutable ghyll : (int option -> unit)
465 ; mutable help : helpitem array
466 ; mutable docinfo : (int * string) list
467 ; mutable texid : GlTex.texture_id option
468 ; hists : hists
469 ; mutable prevzoom : float
470 ; mutable progress : float
471 ; mutable redisplay : bool
472 ; mutable mpos : mpos
473 ; mutable keystate : keystate
474 ; mutable glinks : bool
475 ; mutable prevcolumns : (columns * float) option
476 ; mutable wthack : bool
478 and hists =
479 { pat : string circbuf
480 ; pag : string circbuf
481 ; nav : anchor circbuf
482 ; sel : string circbuf
486 let defconf =
487 { scrollbw = 7
488 ; scrollh = 12
489 ; icase = true
490 ; preload = true
491 ; pagebias = 0
492 ; verbose = false
493 ; debug = false
494 ; scrollstep = 24
495 ; hscrollstep = 24
496 ; maxhfit = true
497 ; crophack = false
498 ; autoscrollstep = 2
499 ; maxwait = None
500 ; hlinks = false
501 ; underinfo = false
502 ; interpagespace = 2
503 ; zoom = 1.0
504 ; presentation = false
505 ; angle = 0
506 ; winw = 900
507 ; winh = 900
508 ; savebmarks = true
509 ; proportional = true
510 ; trimmargins = false
511 ; trimfuzz = (0,0,0,0)
512 ; memlimit = 32 lsl 20
513 ; texcount = 256
514 ; sliceheight = 24
515 ; thumbw = 76
516 ; jumpback = true
517 ; bgcolor = (0.5, 0.5, 0.5)
518 ; bedefault = false
519 ; scrollbarinpm = true
520 ; tilew = 2048
521 ; tileh = 2048
522 ; mustoresize = 256 lsl 20
523 ; checkers = true
524 ; aalevel = 8
525 ; urilauncher =
526 (match platform with
527 | Plinux | Pfreebsd | Pdragonflybsd
528 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
529 | Posx -> "open \"%s\""
530 | Pcygwin -> "cygstart \"%s\""
531 | Punknown -> "echo %s")
532 ; pathlauncher = "lp \"%s\""
533 ; selcmd =
534 (match platform with
535 | Plinux | Pfreebsd | Pdragonflybsd
536 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
537 | Posx -> "pbcopy"
538 | Pcygwin -> "wsel"
539 | Punknown -> "cat")
540 ; colorspace = Rgb
541 ; invert = false
542 ; colorscale = 1.0
543 ; redirectstderr = false
544 ; ghyllscroll = None
545 ; columns = Csingle [||]
546 ; beyecolumns = None
547 ; updatecurs = false
548 ; hfsize = 12
549 ; pgscale = 1.0
550 ; usepbo = false
551 ; keyhashes =
552 let mk n = (n, Hashtbl.create 1) in
553 [ mk "global"
554 ; mk "info"
555 ; mk "help"
556 ; mk "outline"
557 ; mk "listview"
558 ; mk "birdseye"
559 ; mk "textentry"
560 ; mk "links"
561 ; mk "view"
566 let findkeyhash c name =
567 try List.assoc name c.keyhashes
568 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
571 let conf = { defconf with angle = defconf.angle };;
573 let pgscale h = truncate (float h *. conf.pgscale);;
575 type fontstate =
576 { mutable fontsize : int
577 ; mutable wwidth : float
578 ; mutable maxrows : int
582 let fstate =
583 { fontsize = 14
584 ; wwidth = nan
585 ; maxrows = -1
589 let setfontsize n =
590 fstate.fontsize <- n;
591 fstate.wwidth <- measurestr fstate.fontsize "w";
592 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
595 let geturl s =
596 let colonpos = try String.index s ':' with Not_found -> -1 in
597 let len = String.length s in
598 if colonpos >= 0 && colonpos + 3 < len
599 then (
600 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
601 then
602 let schemestartpos =
603 try String.rindex_from s colonpos ' '
604 with Not_found -> -1
606 let scheme =
607 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
609 match scheme with
610 | "http" | "ftp" | "mailto" ->
611 let epos =
612 try String.index_from s colonpos ' '
613 with Not_found -> len
615 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
616 | _ -> ""
617 else ""
619 else ""
622 let gotouri uri =
623 if String.length conf.urilauncher = 0
624 then print_endline uri
625 else (
626 let url = geturl uri in
627 if String.length url = 0
628 then print_endline uri
629 else
630 let re = Str.regexp "%s" in
631 let command = Str.global_replace re url conf.urilauncher in
632 try popen command []
633 with exn ->
634 Printf.eprintf
635 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
636 flush stderr;
640 let version () =
641 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
642 (platform_to_string platform) Sys.word_size Sys.ocaml_version
645 let makehelp () =
646 let strings = version () :: "" :: Help.keys in
647 Array.of_list (
648 List.map (fun s ->
649 let url = geturl s in
650 if String.length url > 0
651 then (s, 0, Action (fun u -> gotouri url; u))
652 else (s, 0, Noaction)
653 ) strings);
656 let noghyll _ = ();;
657 let firstgeomcmds = "", [];;
659 let state =
660 { sr = Unix.stdin
661 ; sw = Unix.stdin
662 ; wsfd = Unix.stdin
663 ; errfd = None
664 ; stderr = Unix.stderr
665 ; errmsgs = Buffer.create 0
666 ; newerrmsgs = false
667 ; x = 0
668 ; y = 0
669 ; w = 0
670 ; scrollw = 0
671 ; hscrollh = 0
672 ; anchor = emptyanchor
673 ; ranchors = []
674 ; layout = []
675 ; maxy = max_int
676 ; tilelru = Queue.create ()
677 ; pagemap = Hashtbl.create 10
678 ; tilemap = Hashtbl.create 10
679 ; pdims = []
680 ; pagecount = 0
681 ; currently = Idle
682 ; mstate = Mnone
683 ; rects = []
684 ; rects1 = []
685 ; text = ""
686 ; mode = View
687 ; fullscreen = None
688 ; searchpattern = ""
689 ; outlines = [||]
690 ; bookmarks = []
691 ; path = ""
692 ; password = ""
693 ; geomcmds = firstgeomcmds
694 ; hists =
695 { nav = cbnew 10 emptyanchor
696 ; pat = cbnew 10 ""
697 ; pag = cbnew 10 ""
698 ; sel = cbnew 10 ""
700 ; memused = 0
701 ; gen = 0
702 ; throttle = None
703 ; autoscroll = None
704 ; ghyll = noghyll
705 ; help = makehelp ()
706 ; docinfo = []
707 ; texid = None
708 ; prevzoom = 1.0
709 ; progress = -1.0
710 ; uioh = nouioh
711 ; redisplay = true
712 ; mpos = (-1, -1)
713 ; keystate = KSnone
714 ; glinks = false
715 ; prevcolumns = None
716 ; wthack = false
720 let vlog fmt =
721 if conf.verbose
722 then
723 Printf.kprintf prerr_endline fmt
724 else
725 Printf.kprintf ignore fmt
728 let launchpath () =
729 if String.length conf.pathlauncher = 0
730 then print_endline state.path
731 else (
732 let re = Str.regexp "%s" in
733 let command = Str.global_replace re state.path conf.pathlauncher in
734 try popen command []
735 with exn ->
736 Printf.eprintf
737 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
738 flush stderr;
742 module Ne = struct
743 type 'a t = | Res of 'a | Exn of exn;;
745 let pipe () =
746 try Res (Unix.pipe ())
747 with exn -> Exn exn
750 let clo fd f =
751 try Unix.close fd
752 with exn -> f (Printexc.to_string exn)
755 let dup fd =
756 try Res (Unix.dup fd)
757 with exn -> Exn exn
760 let dup2 fd1 fd2 =
761 try Res (Unix.dup2 fd1 fd2)
762 with exn -> Exn exn
764 end;;
766 let redirectstderr () =
767 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
768 if conf.redirectstderr
769 then
770 match Ne.pipe () with
771 | Ne.Exn exn ->
772 dolog "failed to create stderr redirection pipes: %s"
773 (Printexc.to_string exn)
775 | Ne.Res (r, w) ->
776 begin match Ne.dup Unix.stderr with
777 | Ne.Exn exn ->
778 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
779 Ne.clo r (clofail "pipe/r");
780 Ne.clo w (clofail "pipe/w");
782 | Ne.Res dupstderr ->
783 begin match Ne.dup2 w Unix.stderr with
784 | Ne.Exn exn ->
785 dolog "failed to dup2 to stderr: %s"
786 (Printexc.to_string exn);
787 Ne.clo dupstderr (clofail "stderr duplicate");
788 Ne.clo r (clofail "redir pipe/r");
789 Ne.clo w (clofail "redir pipe/w");
791 | Ne.Res () ->
792 state.stderr <- dupstderr;
793 state.errfd <- Some r;
794 end;
796 else (
797 state.newerrmsgs <- false;
798 begin match state.errfd with
799 | Some fd ->
800 begin match Ne.dup2 state.stderr Unix.stderr with
801 | Ne.Exn exn ->
802 dolog "failed to dup2 original stderr: %s"
803 (Printexc.to_string exn)
804 | Ne.Res () ->
805 Ne.clo fd (clofail "dup of stderr");
806 Unix.dup2 state.stderr Unix.stderr;
807 state.errfd <- None;
808 end;
809 | None -> ()
810 end;
811 prerr_string (Buffer.contents state.errmsgs);
812 flush stderr;
813 Buffer.clear state.errmsgs;
817 module G =
818 struct
819 let postRedisplay who =
820 if conf.verbose
821 then prerr_endline ("redisplay for " ^ who);
822 state.redisplay <- true;
824 end;;
826 let getopaque pageno =
827 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
828 with Not_found -> None
831 let putopaque pageno opaque =
832 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
835 let pagetranslatepoint l x y =
836 let dy = y - l.pagedispy in
837 let y = dy + l.pagey in
838 let dx = x - l.pagedispx in
839 let x = dx + l.pagex in
840 (x, y);
843 let getunder x y =
844 let rec f = function
845 | l :: rest ->
846 begin match getopaque l.pageno with
847 | Some opaque ->
848 let x0 = l.pagedispx in
849 let x1 = x0 + l.pagevw in
850 let y0 = l.pagedispy in
851 let y1 = y0 + l.pagevh in
852 if y >= y0 && y <= y1 && x >= x0 && x <= x1
853 then
854 let px, py = pagetranslatepoint l x y in
855 match whatsunder opaque px py with
856 | Unone -> f rest
857 | under -> under
858 else f rest
859 | _ ->
860 f rest
862 | [] -> Unone
864 f state.layout
867 let showtext c s =
868 state.text <- Printf.sprintf "%c%s" c s;
869 G.postRedisplay "showtext";
872 let undertext = function
873 | Unone -> "none"
874 | Ulinkuri s -> s
875 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
876 | Utext s -> "font: " ^ s
877 | Uunexpected s -> "unexpected: " ^ s
878 | Ulaunch s -> "launch: " ^ s
879 | Unamed s -> "named: " ^ s
880 | Uremote (filename, pageno) ->
881 Printf.sprintf "%s: page %d" filename (pageno+1)
884 let updateunder x y =
885 match getunder x y with
886 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
887 | Ulinkuri uri ->
888 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
889 Wsi.setcursor Wsi.CURSOR_INFO
890 | Ulinkgoto (pageno, _) ->
891 if conf.underinfo
892 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
893 Wsi.setcursor Wsi.CURSOR_INFO
894 | Utext s ->
895 if conf.underinfo then showtext 'f' ("ont: " ^ s);
896 Wsi.setcursor Wsi.CURSOR_TEXT
897 | Uunexpected s ->
898 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
899 Wsi.setcursor Wsi.CURSOR_INHERIT
900 | Ulaunch s ->
901 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
902 Wsi.setcursor Wsi.CURSOR_INHERIT
903 | Unamed s ->
904 if conf.underinfo then showtext 'n' ("amed: " ^ s);
905 Wsi.setcursor Wsi.CURSOR_INHERIT
906 | Uremote (filename, pageno) ->
907 if conf.underinfo then showtext 'r'
908 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
909 Wsi.setcursor Wsi.CURSOR_INFO
912 let showlinktype under =
913 if conf.underinfo
914 then
915 match under with
916 | Unone -> ()
917 | under ->
918 let s = undertext under in
919 showtext ' ' s
922 let addchar s c =
923 let b = Buffer.create (String.length s + 1) in
924 Buffer.add_string b s;
925 Buffer.add_char b c;
926 Buffer.contents b;
929 let colorspace_of_string s =
930 match String.lowercase s with
931 | "rgb" -> Rgb
932 | "bgr" -> Bgr
933 | "gray" -> Gray
934 | _ -> failwith "invalid colorspace"
937 let int_of_colorspace = function
938 | Rgb -> 0
939 | Bgr -> 1
940 | Gray -> 2
943 let colorspace_of_int = function
944 | 0 -> Rgb
945 | 1 -> Bgr
946 | 2 -> Gray
947 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
950 let colorspace_to_string = function
951 | Rgb -> "rgb"
952 | Bgr -> "bgr"
953 | Gray -> "gray"
956 let intentry_with_suffix text key =
957 let c =
958 if key >= 32 && key < 127
959 then Char.chr key
960 else '\000'
962 match Char.lowercase c with
963 | '0' .. '9' ->
964 let text = addchar text c in
965 TEcont text
967 | 'k' | 'm' | 'g' ->
968 let text = addchar text c in
969 TEcont text
971 | _ ->
972 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
973 TEcont text
976 let multicolumns_to_string (n, a, b) =
977 if a = 0 && b = 0
978 then Printf.sprintf "%d" n
979 else Printf.sprintf "%d,%d,%d" n a b;
982 let multicolumns_of_string s =
984 (int_of_string s, 0, 0)
985 with _ ->
986 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
987 if a > 1 || b > 1
988 then failwith "subtly broken"; (n, a, b)
992 let readcmd fd =
993 let s = "xxxx" in
994 let n = Unix.read fd s 0 4 in
995 if n != 4 then failwith "incomplete read(len)";
996 let len = 0
997 lor (Char.code s.[0] lsl 24)
998 lor (Char.code s.[1] lsl 16)
999 lor (Char.code s.[2] lsl 8)
1000 lor (Char.code s.[3] lsl 0)
1002 let s = String.create len in
1003 let n = Unix.read fd s 0 len in
1004 if n != len then failwith "incomplete read(data)";
1008 let btod b = if b then 1 else 0;;
1010 let wcmd fmt =
1011 let b = Buffer.create 16 in
1012 Buffer.add_string b "llll";
1013 Printf.kbprintf
1014 (fun b ->
1015 let s = Buffer.contents b in
1016 let n = String.length s in
1017 let len = n - 4 in
1018 (* dolog "wcmd %S" (String.sub s 4 len); *)
1019 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1020 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1021 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1022 s.[3] <- Char.chr (len land 0xff);
1023 let n' = Unix.write state.sw s 0 n in
1024 if n' != n then failwith "write failed";
1025 ) b fmt;
1028 let calcips h =
1029 let d = conf.winh - h in
1030 max conf.interpagespace ((d + 1) / 2)
1033 let rowyh (c, coverA, coverB) b n =
1034 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1035 then
1036 let _, _, vy, (_, _, h, _) = b.(n) in
1037 (vy, h)
1038 else
1039 let n' = n - coverA in
1040 let d = n' mod c in
1041 let s = n - d in
1042 let e = min state.pagecount (s + c) in
1043 let rec find m miny maxh = if m = e then miny, maxh else
1044 let _, _, y, (_, _, h, _) = b.(m) in
1045 let miny = min miny y in
1046 let maxh = max maxh h in
1047 find (m+1) miny maxh
1048 in find s max_int 0
1051 let calcheight () =
1052 match conf.columns with
1053 | Cmulti ((_, _, _) as cl, b) ->
1054 if Array.length b > 0
1055 then
1056 let y, h = rowyh cl b (Array.length b - 1) in
1057 y + h + (if conf.presentation then calcips h else 0)
1058 else 0
1059 | Csingle b ->
1060 if Array.length b > 0
1061 then
1062 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1063 y + h + (if conf.presentation then calcips h else 0)
1064 else 0
1065 | Csplit (_, b) ->
1066 if Array.length b > 0
1067 then
1068 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1069 y + h
1070 else 0
1073 let getpageyh pageno =
1074 let pageno = bound pageno 0 (state.pagecount-1) in
1075 match conf.columns with
1076 | Csingle b ->
1077 if Array.length b = 0
1078 then 0, 0
1079 else
1080 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1081 let y =
1082 if conf.presentation
1083 then y - calcips h
1084 else y
1086 y, h
1087 | Cmulti (cl, b) ->
1088 if Array.length b = 0
1089 then 0, 0
1090 else
1091 let y, h = rowyh cl b pageno in
1092 let y =
1093 if conf.presentation
1094 then y - calcips h
1095 else y
1097 y, h
1098 | Csplit (c, b) ->
1099 if Array.length b = 0
1100 then 0, 0
1101 else
1102 let n = pageno*c in
1103 let (_, _, y, (_, _, h, _)) = b.(n) in
1104 y, h
1107 let getpagedim pageno =
1108 let rec f ppdim l =
1109 match l with
1110 | (n, _, _, _) as pdim :: rest ->
1111 if n >= pageno
1112 then (if n = pageno then pdim else ppdim)
1113 else f pdim rest
1115 | [] -> ppdim
1117 f (-1, -1, -1, -1) state.pdims
1120 let getpagey pageno = fst (getpageyh pageno);;
1122 let nogeomcmds cmds =
1123 match cmds with
1124 | s, [] -> String.length s = 0
1125 | _ -> false
1128 let page_of_y y =
1129 let ((c, coverA, coverB) as cl), b =
1130 match conf.columns with
1131 | Csingle b -> (1, 0, 0), b
1132 | Cmulti (c, b) -> c, b
1133 | Csplit (_, b) -> (1, 0, 0), b
1135 let rec bsearch nmin nmax =
1136 if nmin > nmax
1137 then bound nmin 0 (state.pagecount-1)
1138 else
1139 let n = (nmax + nmin) / 2 in
1140 let vy, h = rowyh cl b n in
1141 let y0, y1 =
1142 if conf.presentation
1143 then
1144 let ips = calcips h in
1145 let y0 = vy - ips in
1146 let y1 = vy + h + ips in
1147 y0, y1
1148 else (
1149 if n = 0
1150 then 0, vy + h + conf.interpagespace
1151 else
1152 let y0 = vy - conf.interpagespace in
1153 y0, y0 + h + conf.interpagespace
1156 if y >= y0 && y < y1
1157 then (
1158 if c = 1
1159 then n
1160 else (
1161 if n > coverA
1162 then
1163 if n < state.pagecount - coverB
1164 then ((n-coverA)/c)*c + coverA
1165 else n
1166 else n
1169 else (
1170 if y > y0
1171 then bsearch (n+1) nmax
1172 else bsearch nmin (n-1)
1175 let r = bsearch 0 (state.pagecount-1) in
1179 let layoutN ((columns, coverA, coverB), b) y sh =
1180 let sh = sh - state.hscrollh in
1181 let rec fold accu n =
1182 if n = Array.length b
1183 then accu
1184 else
1185 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1186 if (vy - y) > sh &&
1187 (n = coverA - 1
1188 || n = state.pagecount - coverB
1189 || (n - coverA) mod columns = columns - 1)
1190 then accu
1191 else
1192 let accu =
1193 if vy + h > y
1194 then
1195 let pagey = max 0 (y - vy) in
1196 let pagedispy = if pagey > 0 then 0 else vy - y in
1197 let pagedispx, pagex =
1198 let pdx =
1199 if n = coverA - 1 || n = state.pagecount - coverB
1200 then state.x + (conf.winw - state.scrollw - w) / 2
1201 else dx + xoff + state.x
1203 if pdx < 0
1204 then 0, -pdx
1205 else pdx, 0
1207 let pagevw =
1208 let vw = conf.winw - state.scrollw - pagedispx in
1209 let pw = w - pagex in
1210 min vw pw
1212 let pagevh = min (h - pagey) (sh - pagedispy) in
1213 if pagevw > 0 && pagevh > 0
1214 then
1215 let e =
1216 { pageno = n
1217 ; pagedimno = pdimno
1218 ; pagew = w
1219 ; pageh = h
1220 ; pagex = pagex
1221 ; pagey = pagey
1222 ; pagevw = pagevw
1223 ; pagevh = pagevh
1224 ; pagedispx = pagedispx
1225 ; pagedispy = pagedispy
1226 ; pagecol = 0
1229 e :: accu
1230 else
1231 accu
1232 else
1233 accu
1235 fold accu (n+1)
1237 List.rev (fold [] (page_of_y y));
1240 let layoutS (columns, b) y sh =
1241 let sh = sh - state.hscrollh in
1242 let rec fold accu n =
1243 if n = Array.length b
1244 then accu
1245 else
1246 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1247 if (vy - y) > sh
1248 then accu
1249 else
1250 let accu =
1251 if vy + pageh > y
1252 then
1253 let x = xoff + state.x in
1254 let pagey = max 0 (y - vy) in
1255 let pagedispy = if pagey > 0 then 0 else vy - y in
1256 let pagedispx, pagex =
1257 if px = 0
1258 then (
1259 if x < 0
1260 then 0, -x
1261 else x, 0
1263 else (
1264 let px = px - x in
1265 if px < 0
1266 then -px, 0
1267 else 0, px
1270 let pagecolw = pagew/columns in
1271 let pagedispx =
1272 if pagecolw < conf.winw
1273 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1274 else pagedispx
1276 let pagevw =
1277 let vw = conf.winw - pagedispx - state.scrollw in
1278 let pw = pagew - pagex in
1279 min vw pw
1281 let pagevw = min pagevw pagecolw in
1282 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1283 if pagevw > 0 && pagevh > 0
1284 then
1285 let e =
1286 { pageno = n/columns
1287 ; pagedimno = pdimno
1288 ; pagew = pagew
1289 ; pageh = pageh
1290 ; pagex = pagex
1291 ; pagey = pagey
1292 ; pagevw = pagevw
1293 ; pagevh = pagevh
1294 ; pagedispx = pagedispx
1295 ; pagedispy = pagedispy
1296 ; pagecol = n mod columns
1299 e :: accu
1300 else
1301 accu
1302 else
1303 accu
1305 fold accu (n+1)
1307 List.rev (fold [] 0)
1310 let layout y sh =
1311 if nogeomcmds state.geomcmds
1312 then
1313 match conf.columns with
1314 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1315 | Cmulti c -> layoutN c y sh
1316 | Csplit s -> layoutS s y sh
1317 else []
1320 let clamp incr =
1321 let y = state.y + incr in
1322 let y = max 0 y in
1323 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1327 let itertiles l f =
1328 let tilex = l.pagex mod conf.tilew in
1329 let tiley = l.pagey mod conf.tileh in
1331 let col = l.pagex / conf.tilew in
1332 let row = l.pagey / conf.tileh in
1334 let rec rowloop row y0 dispy h =
1335 if h = 0
1336 then ()
1337 else (
1338 let dh = conf.tileh - y0 in
1339 let dh = min h dh in
1340 let rec colloop col x0 dispx w =
1341 if w = 0
1342 then ()
1343 else (
1344 let dw = conf.tilew - x0 in
1345 let dw = min w dw in
1347 f col row dispx dispy x0 y0 dw dh;
1348 colloop (col+1) 0 (dispx+dw) (w-dw)
1351 colloop col tilex l.pagedispx l.pagevw;
1352 rowloop (row+1) 0 (dispy+dh) (h-dh)
1355 if l.pagevw > 0 && l.pagevh > 0
1356 then rowloop row tiley l.pagedispy l.pagevh;
1359 let gettileopaque l col row =
1360 let key =
1361 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1363 try Some (Hashtbl.find state.tilemap key)
1364 with Not_found -> None
1367 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1368 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1369 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1372 let drawtiles l color =
1373 GlDraw.color color;
1374 let f col row x y tilex tiley w h =
1375 match gettileopaque l col row with
1376 | Some (opaque, _, t) ->
1377 let params = x, y, w, h, tilex, tiley in
1378 if conf.invert
1379 then (
1380 Gl.enable `blend;
1381 GlFunc.blend_func `zero `one_minus_src_color;
1383 drawtile params opaque;
1384 if conf.invert
1385 then Gl.disable `blend;
1386 if conf.debug
1387 then (
1388 let s = Printf.sprintf
1389 "%d[%d,%d] %f sec"
1390 l.pageno col row t
1392 let w = measurestr fstate.fontsize s in
1393 GlMisc.push_attrib [`current];
1394 GlDraw.color (0.0, 0.0, 0.0);
1395 GlDraw.rect
1396 (float (x-2), float (y-2))
1397 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1398 GlDraw.color (1.0, 1.0, 1.0);
1399 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1400 GlMisc.pop_attrib ();
1403 | _ ->
1404 let w =
1405 let lw = conf.winw - state.scrollw - x in
1406 min lw w
1407 and h =
1408 let lh = conf.winh - y in
1409 min lh h
1411 begin match state.texid with
1412 | Some id ->
1413 Gl.enable `texture_2d;
1414 GlTex.bind_texture `texture_2d id;
1415 let x0 = float x
1416 and y0 = float y
1417 and x1 = float (x+w)
1418 and y1 = float (y+h) in
1420 let tw = float w /. 16.0
1421 and th = float h /. 16.0 in
1422 let tx0 = float tilex /. 16.0
1423 and ty0 = float tiley /. 16.0 in
1424 let tx1 = tx0 +. tw
1425 and ty1 = ty0 +. th in
1426 GlDraw.begins `quads;
1427 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1428 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1429 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1430 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1431 GlDraw.ends ();
1433 Gl.disable `texture_2d;
1434 | None ->
1435 GlDraw.color (1.0, 1.0, 1.0);
1436 GlDraw.rect
1437 (float x, float y)
1438 (float (x+w), float (y+h));
1439 end;
1440 if w > 128 && h > fstate.fontsize + 10
1441 then (
1442 GlDraw.color (0.0, 0.0, 0.0);
1443 let c, r =
1444 if conf.verbose
1445 then (col*conf.tilew, row*conf.tileh)
1446 else col, row
1448 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1450 GlDraw.color color;
1452 itertiles l f
1455 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1457 let tilevisible1 l x y =
1458 let ax0 = l.pagex
1459 and ax1 = l.pagex + l.pagevw
1460 and ay0 = l.pagey
1461 and ay1 = l.pagey + l.pagevh in
1463 let bx0 = x
1464 and by0 = y in
1465 let bx1 = min (bx0 + conf.tilew) l.pagew
1466 and by1 = min (by0 + conf.tileh) l.pageh in
1468 let rx0 = max ax0 bx0
1469 and ry0 = max ay0 by0
1470 and rx1 = min ax1 bx1
1471 and ry1 = min ay1 by1 in
1473 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1474 nonemptyintersection
1477 let tilevisible layout n x y =
1478 let rec findpageinlayout m = function
1479 | l :: rest when l.pageno = n ->
1480 tilevisible1 l x y || (
1481 match conf.columns with
1482 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1483 | _ -> false
1485 | _ :: rest -> findpageinlayout 0 rest
1486 | [] -> false
1488 findpageinlayout 0 layout;
1491 let tileready l x y =
1492 tilevisible1 l x y &&
1493 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1496 let tilepage n p layout =
1497 let rec loop = function
1498 | l :: rest ->
1499 if l.pageno = n
1500 then
1501 let f col row _ _ _ _ _ _ =
1502 if state.currently = Idle
1503 then
1504 match gettileopaque l col row with
1505 | Some _ -> ()
1506 | None ->
1507 let x = col*conf.tilew
1508 and y = row*conf.tileh in
1509 let w =
1510 let w = l.pagew - x in
1511 min w conf.tilew
1513 let h =
1514 let h = l.pageh - y in
1515 min h conf.tileh
1517 let pbo =
1518 if conf.usepbo
1519 then getpbo w h conf.colorspace
1520 else "0"
1522 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1523 state.currently <-
1524 Tiling (
1525 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1526 conf.tilew, conf.tileh
1529 itertiles l f;
1530 else
1531 loop rest
1533 | [] -> ()
1535 if nogeomcmds state.geomcmds
1536 then loop layout;
1539 let preloadlayout y =
1540 let y = if y < conf.winh then 0 else y - conf.winh in
1541 let h = conf.winh*3 in
1542 layout y h;
1545 let load pages =
1546 let rec loop pages =
1547 if state.currently != Idle
1548 then ()
1549 else
1550 match pages with
1551 | l :: rest ->
1552 begin match getopaque l.pageno with
1553 | None ->
1554 wcmd "page %d %d" l.pageno l.pagedimno;
1555 state.currently <- Loading (l, state.gen);
1556 | Some opaque ->
1557 tilepage l.pageno opaque pages;
1558 loop rest
1559 end;
1560 | _ -> ()
1562 if nogeomcmds state.geomcmds
1563 then loop pages
1566 let preload pages =
1567 load pages;
1568 if conf.preload && state.currently = Idle
1569 then load (preloadlayout state.y);
1572 let layoutready layout =
1573 let rec fold all ls =
1574 all && match ls with
1575 | l :: rest ->
1576 let seen = ref false in
1577 let allvisible = ref true in
1578 let foo col row _ _ _ _ _ _ =
1579 seen := true;
1580 allvisible := !allvisible &&
1581 begin match gettileopaque l col row with
1582 | Some _ -> true
1583 | None -> false
1586 itertiles l foo;
1587 fold (!seen && !allvisible) rest
1588 | [] -> true
1590 let alltilesvisible = fold true layout in
1591 alltilesvisible;
1594 let gotoy y =
1595 let y = bound y 0 state.maxy in
1596 let y, layout, proceed =
1597 match conf.maxwait with
1598 | Some time when state.ghyll == noghyll ->
1599 begin match state.throttle with
1600 | None ->
1601 let layout = layout y conf.winh in
1602 let ready = layoutready layout in
1603 if not ready
1604 then (
1605 load layout;
1606 state.throttle <- Some (layout, y, now ());
1608 else G.postRedisplay "gotoy showall (None)";
1609 y, layout, ready
1610 | Some (_, _, started) ->
1611 let dt = now () -. started in
1612 if dt > time
1613 then (
1614 state.throttle <- None;
1615 let layout = layout y conf.winh in
1616 load layout;
1617 G.postRedisplay "maxwait";
1618 y, layout, true
1620 else -1, [], false
1623 | _ ->
1624 let layout = layout y conf.winh in
1625 if true || layoutready layout
1626 then G.postRedisplay "gotoy ready";
1627 y, layout, true
1629 if proceed
1630 then (
1631 state.y <- y;
1632 state.layout <- layout;
1633 begin match state.mode with
1634 | LinkNav (Ltexact (pageno, linkno)) ->
1635 let rec loop = function
1636 | [] ->
1637 state.mode <- LinkNav (Ltgendir 0)
1638 | l :: _ when l.pageno = pageno ->
1639 begin match getopaque pageno with
1640 | None ->
1641 state.mode <- LinkNav (Ltgendir 0)
1642 | Some opaque ->
1643 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1644 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1645 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1646 then state.mode <- LinkNav (Ltgendir 0)
1648 | _ :: rest -> loop rest
1650 loop layout
1651 | _ -> ()
1652 end;
1653 begin match state.mode with
1654 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1655 if not (pagevisible layout pageno)
1656 then (
1657 match state.layout with
1658 | [] -> ()
1659 | l :: _ ->
1660 state.mode <- Birdseye (
1661 conf, leftx, l.pageno, hooverpageno, anchor
1664 | LinkNav (Ltgendir dir as lt) ->
1665 let linknav =
1666 let rec loop = function
1667 | [] -> lt
1668 | l :: rest ->
1669 match getopaque l.pageno with
1670 | None -> loop rest
1671 | Some opaque ->
1672 let link =
1673 let ld =
1674 if dir = 0
1675 then LDfirstvisible (l.pagex, l.pagey, dir)
1676 else (
1677 if dir > 0 then LDfirst else LDlast
1680 findlink opaque ld
1682 match link with
1683 | Lnotfound -> loop rest
1684 | Lfound n ->
1685 showlinktype (getlink opaque n);
1686 Ltexact (l.pageno, n)
1688 loop state.layout
1690 state.mode <- LinkNav linknav
1691 | _ -> ()
1692 end;
1693 preload layout;
1695 state.ghyll <- noghyll;
1696 if conf.updatecurs
1697 then (
1698 let mx, my = state.mpos in
1699 updateunder mx my;
1703 let conttiling pageno opaque =
1704 tilepage pageno opaque
1705 (if conf.preload then preloadlayout state.y else state.layout)
1708 let gotoy_and_clear_text y =
1709 if not conf.verbose then state.text <- "";
1710 gotoy y;
1713 let getanchor1 l =
1714 let top =
1715 let coloff = l.pagecol * l.pageh in
1716 float (l.pagey + coloff) /. float l.pageh
1718 let dtop =
1719 if l.pagedispy = 0
1720 then
1722 else
1723 if conf.presentation
1724 then float l.pagedispy /. float (calcips l.pageh)
1725 else float l.pagedispy /. float conf.interpagespace
1727 (l.pageno, top, dtop)
1730 let getanchor () =
1731 match state.layout with
1732 | l :: _ -> getanchor1 l
1733 | [] ->
1734 let n = page_of_y state.y in
1735 let y, h = getpageyh n in
1736 let dy = y - state.y in
1737 let dtop =
1738 if conf.presentation
1739 then
1740 let ips = calcips h in
1741 float (dy + ips) /. float ips
1742 else
1743 float dy /. float conf.interpagespace
1745 (n, 0.0, dtop)
1748 let getanchory (n, top, dtop) =
1749 let y, h = getpageyh n in
1750 if conf.presentation
1751 then
1752 let ips = calcips h in
1753 y + truncate (top*.float h -. dtop*.float ips) + ips;
1754 else
1755 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1758 let gotoanchor anchor =
1759 gotoy (getanchory anchor);
1762 let addnav () =
1763 cbput state.hists.nav (getanchor ());
1766 let getnav dir =
1767 let anchor = cbgetc state.hists.nav dir in
1768 getanchory anchor;
1771 let gotoghyll y =
1772 let scroll f n a b =
1773 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1774 let snake f a b =
1775 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1776 if f < a
1777 then s (float f /. float a)
1778 else (
1779 if f > b
1780 then 1.0 -. s ((float (f-b) /. float (n-b)))
1781 else 1.0
1784 snake f a b
1785 and summa f n a b =
1786 (* courtesy:
1787 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1788 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1789 let iv1 = iv f in
1790 let ins = float a *. iv1
1791 and outs = float (n-b) *. iv1 in
1792 let ones = b - a in
1793 ins +. outs +. float ones
1795 let rec set (_N, _A, _B) y sy =
1796 let sum = summa 1.0 _N _A _B in
1797 let dy = float (y - sy) in
1798 state.ghyll <- (
1799 let rec gf n y1 o =
1800 if n >= _N
1801 then state.ghyll <- noghyll
1802 else
1803 let go n =
1804 let s = scroll n _N _A _B in
1805 let y1 = y1 +. ((s *. dy) /. sum) in
1806 gotoy_and_clear_text (truncate y1);
1807 state.ghyll <- gf (n+1) y1;
1809 match o with
1810 | None -> go n
1811 | Some y' -> set (_N/2, 1, 1) y' state.y
1813 gf 0 (float state.y)
1816 match conf.ghyllscroll with
1817 | None ->
1818 gotoy_and_clear_text y
1819 | Some nab ->
1820 if state.ghyll == noghyll
1821 then set nab y state.y
1822 else state.ghyll (Some y)
1825 let gotopage n top =
1826 let y, h = getpageyh n in
1827 let y = y + (truncate (top *. float h)) in
1828 gotoghyll y
1831 let gotopage1 n top =
1832 let y = getpagey n in
1833 let y = y + top in
1834 gotoghyll y
1837 let invalidate s f =
1838 state.layout <- [];
1839 state.pdims <- [];
1840 state.rects <- [];
1841 state.rects1 <- [];
1842 match state.geomcmds with
1843 | ps, [] when String.length ps = 0 ->
1844 f ();
1845 state.geomcmds <- s, [];
1847 | ps, [] ->
1848 state.geomcmds <- ps, [s, f];
1850 | ps, (s', _) :: rest when s' = s ->
1851 state.geomcmds <- ps, ((s, f) :: rest);
1853 | ps, cmds ->
1854 state.geomcmds <- ps, ((s, f) :: cmds);
1857 let flushpages () =
1858 Hashtbl.iter (fun _ opaque ->
1859 wcmd "freepage %s" opaque;
1860 ) state.pagemap;
1861 Hashtbl.clear state.pagemap;
1864 let opendoc path password =
1865 state.path <- path;
1866 state.password <- password;
1867 state.gen <- state.gen + 1;
1868 state.docinfo <- [];
1870 flushpages ();
1871 setaalevel conf.aalevel;
1872 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1873 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1874 invalidate "reqlayout"
1875 (fun () ->
1876 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1879 let reload () =
1880 state.anchor <- getanchor ();
1881 state.wthack <- true;
1882 opendoc state.path state.password;
1885 let scalecolor c =
1886 let c = c *. conf.colorscale in
1887 (c, c, c);
1890 let scalecolor2 (r, g, b) =
1891 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1894 let docolumns = function
1895 | Csingle _ ->
1896 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1897 let rec loop pageno pdimno pdim y ph pdims =
1898 if pageno = state.pagecount
1899 then ()
1900 else
1901 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1902 match pdims with
1903 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1904 pdimno+1, pdim, rest
1905 | _ ->
1906 pdimno, pdim, pdims
1908 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1909 let y = y +
1910 (if conf.presentation
1911 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1912 else (if pageno = 0 then 0 else conf.interpagespace)
1915 a.(pageno) <- (pdimno, x, y, pdim);
1916 loop (pageno+1) pdimno pdim (y + h) h pdims
1918 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1919 conf.columns <- Csingle a;
1921 | Cmulti ((columns, coverA, coverB), _) ->
1922 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1923 let rec loop pageno pdimno pdim x y rowh pdims =
1924 let rec fixrow m = if m = pageno then () else
1925 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1926 if h < rowh
1927 then (
1928 let y = y + (rowh - h) / 2 in
1929 a.(m) <- (pdimno, x, y, pdim);
1931 fixrow (m+1)
1933 if pageno = state.pagecount
1934 then fixrow (((pageno - 1) / columns) * columns)
1935 else
1936 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1937 match pdims with
1938 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1939 pdimno+1, pdim, rest
1940 | _ ->
1941 pdimno, pdim, pdims
1943 let x, y, rowh' =
1944 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1945 then (
1946 let x = (conf.winw - state.scrollw - w) / 2 in
1947 let ips =
1948 if conf.presentation then calcips h else conf.interpagespace in
1949 x, y + ips + rowh, h
1951 else (
1952 if (pageno - coverA) mod columns = 0
1953 then (
1954 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1955 let y =
1956 if conf.presentation
1957 then
1958 let ips = calcips h in
1959 y + (if pageno = 0 then 0 else calcips rowh + ips)
1960 else
1961 y + (if pageno = 0 then 0 else conf.interpagespace)
1963 x, y + rowh, h
1965 else x, y, max rowh h
1968 let y =
1969 if pageno > 1 && (pageno - coverA) mod columns = 0
1970 then (
1971 let y =
1972 if pageno = columns && conf.presentation
1973 then (
1974 let ips = calcips rowh in
1975 for i = 0 to pred columns
1977 let (pdimno, x, y, pdim) = a.(i) in
1978 a.(i) <- (pdimno, x, y+ips, pdim)
1979 done;
1980 y+ips;
1982 else y
1984 fixrow (pageno - columns);
1987 else y
1989 a.(pageno) <- (pdimno, x, y, pdim);
1990 let x = x + w + xoff*2 + conf.interpagespace in
1991 loop (pageno+1) pdimno pdim x y rowh' pdims
1993 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1994 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1996 | Csplit (c, _) ->
1997 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1998 let rec loop pageno pdimno pdim y pdims =
1999 if pageno = state.pagecount
2000 then ()
2001 else
2002 let pdimno, ((_, w, h, _) as pdim), pdims =
2003 match pdims with
2004 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2005 pdimno+1, pdim, rest
2006 | _ ->
2007 pdimno, pdim, pdims
2009 let cw = w / c in
2010 let rec loop1 n x y =
2011 if n = c then y else (
2012 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2013 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2016 let y = loop1 0 0 y in
2017 loop (pageno+1) pdimno pdim y pdims
2019 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2020 conf.columns <- Csplit (c, a);
2023 let represent () =
2024 docolumns conf.columns;
2025 state.maxy <- calcheight ();
2026 state.hscrollh <-
2027 if state.w <= conf.winw - state.scrollw
2028 then 0
2029 else state.scrollw
2031 match state.mode with
2032 | Birdseye (_, _, pageno, _, _) ->
2033 let y, h = getpageyh pageno in
2034 let top = (conf.winh - h) / 2 in
2035 gotoy (max 0 (y - top))
2036 | _ -> gotoanchor state.anchor
2039 let reshape w h =
2040 state.wthack <- false;
2041 GlDraw.viewport 0 0 w h;
2042 let firsttime = state.geomcmds == firstgeomcmds in
2043 if not firsttime && nogeomcmds state.geomcmds
2044 then state.anchor <- getanchor ();
2046 conf.winw <- w;
2047 let w = truncate (float w *. conf.zoom) - state.scrollw in
2048 let w = max w 2 in
2049 conf.winh <- h;
2050 setfontsize fstate.fontsize;
2051 GlMat.mode `modelview;
2052 GlMat.load_identity ();
2054 GlMat.mode `projection;
2055 GlMat.load_identity ();
2056 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2057 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2058 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2060 let relx =
2061 if conf.zoom <= 1.0
2062 then 0.0
2063 else float state.x /. float state.w
2065 invalidate "geometry"
2066 (fun () ->
2067 state.w <- w;
2068 if not firsttime
2069 then state.x <- truncate (relx *. float w);
2070 let w =
2071 match conf.columns with
2072 | Csingle _ -> w
2073 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2074 | Csplit (c, _) -> w * c
2076 wcmd "geometry %d %d" w h);
2079 let enttext () =
2080 let len = String.length state.text in
2081 let drawstring s =
2082 let hscrollh =
2083 match state.mode with
2084 | Textentry _
2085 | View ->
2086 let h, _, _ = state.uioh#scrollpw in
2088 | _ -> 0
2090 let rect x w =
2091 GlDraw.rect
2092 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2093 (x+.w, float (conf.winh - hscrollh))
2096 let w = float (conf.winw - state.scrollw - 1) in
2097 if state.progress >= 0.0 && state.progress < 1.0
2098 then (
2099 GlDraw.color (0.3, 0.3, 0.3);
2100 let w1 = w *. state.progress in
2101 rect 0.0 w1;
2102 GlDraw.color (0.0, 0.0, 0.0);
2103 rect w1 (w-.w1)
2105 else (
2106 GlDraw.color (0.0, 0.0, 0.0);
2107 rect 0.0 w;
2110 GlDraw.color (1.0, 1.0, 1.0);
2111 drawstring fstate.fontsize
2112 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2114 let s =
2115 match state.mode with
2116 | Textentry ((prefix, text, _, _, _, _), _) ->
2117 let s =
2118 if len > 0
2119 then
2120 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2121 else
2122 Printf.sprintf "%s%s_" prefix text
2126 | _ -> state.text
2128 let s =
2129 if state.newerrmsgs
2130 then (
2131 if not (istextentry state.mode)
2132 then
2133 let s1 = "(press 'e' to review error messasges)" in
2134 if String.length s > 0 then s ^ " " ^ s1 else s1
2135 else s
2137 else s
2139 if String.length s > 0
2140 then drawstring s
2143 let gctiles () =
2144 let len = Queue.length state.tilelru in
2145 let layout = lazy (
2146 match state.throttle with
2147 | None ->
2148 if conf.preload
2149 then preloadlayout state.y
2150 else state.layout
2151 | Some (layout, _, _) ->
2152 layout
2153 ) in
2154 let rec loop qpos =
2155 if state.memused <= conf.memlimit
2156 then ()
2157 else (
2158 if qpos < len
2159 then
2160 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2161 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2162 let (_, pw, ph, _) = getpagedim n in
2164 gen = state.gen
2165 && colorspace = conf.colorspace
2166 && angle = conf.angle
2167 && pagew = pw
2168 && pageh = ph
2169 && (
2170 let x = col*conf.tilew
2171 and y = row*conf.tileh in
2172 tilevisible (Lazy.force_val layout) n x y
2174 then Queue.push lruitem state.tilelru
2175 else (
2176 freepbo p;
2177 wcmd "freetile %s" p;
2178 state.memused <- state.memused - s;
2179 state.uioh#infochanged Memused;
2180 Hashtbl.remove state.tilemap k;
2182 loop (qpos+1)
2185 loop 0
2188 let flushtiles () =
2189 Queue.iter (fun (k, p, s) ->
2190 wcmd "freetile %s" p;
2191 state.memused <- state.memused - s;
2192 state.uioh#infochanged Memused;
2193 Hashtbl.remove state.tilemap k;
2194 ) state.tilelru;
2195 Queue.clear state.tilelru;
2196 load state.layout;
2199 let logcurrently = function
2200 | Idle -> dolog "Idle"
2201 | Loading (l, gen) ->
2202 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2203 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2204 dolog
2205 "Tiling %d[%d,%d] page=%s cs=%s angle"
2206 l.pageno col row pageopaque
2207 (colorspace_to_string colorspace)
2209 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2210 angle gen conf.angle state.gen
2211 tilew tileh
2212 conf.tilew conf.tileh
2214 | Outlining _ ->
2215 dolog "outlining"
2218 let act cmds =
2219 (* dolog "%S" cmds; *)
2220 let op, args =
2221 let spacepos =
2222 try String.index cmds ' '
2223 with Not_found -> -1
2225 if spacepos = -1
2226 then cmds, ""
2227 else
2228 let l = String.length cmds in
2229 let op = String.sub cmds 0 spacepos in
2230 op, begin
2231 if l - spacepos < 2 then ""
2232 else String.sub cmds (spacepos+1) (l-spacepos-1)
2235 match op with
2236 | "clear" ->
2237 state.uioh#infochanged Pdim;
2238 state.pdims <- [];
2240 | "clearrects" ->
2241 state.rects <- state.rects1;
2242 G.postRedisplay "clearrects";
2244 | "continue" ->
2245 let n =
2246 try Scanf.sscanf args "%u" (fun n -> n)
2247 with exn ->
2248 dolog "error processing 'continue' %S: %s"
2249 cmds (Printexc.to_string exn);
2250 exit 1;
2252 state.pagecount <- n;
2253 begin match state.currently with
2254 | Outlining l ->
2255 state.currently <- Idle;
2256 state.outlines <- Array.of_list (List.rev l)
2257 | _ -> ()
2258 end;
2260 let cur, cmds = state.geomcmds in
2261 if String.length cur = 0
2262 then failwith "umpossible";
2264 begin match List.rev cmds with
2265 | [] ->
2266 state.geomcmds <- "", [];
2267 represent ();
2268 | (s, f) :: rest ->
2269 f ();
2270 state.geomcmds <- s, List.rev rest;
2271 end;
2272 if conf.maxwait = None
2273 then G.postRedisplay "continue";
2275 | "title" ->
2276 Wsi.settitle args
2278 | "msg" ->
2279 showtext ' ' args
2281 | "vmsg" ->
2282 if conf.verbose
2283 then showtext ' ' args
2285 | "progress" ->
2286 let progress, text =
2288 Scanf.sscanf args "%f %n"
2289 (fun f pos ->
2290 f, String.sub args pos (String.length args - pos))
2291 with exn ->
2292 dolog "error processing 'progress' %S: %s"
2293 cmds (Printexc.to_string exn);
2294 exit 1;
2296 state.text <- text;
2297 state.progress <- progress;
2298 G.postRedisplay "progress"
2300 | "firstmatch" ->
2301 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2303 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2304 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2305 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2306 with exn ->
2307 dolog "error processing 'firstmatch' %S: %s"
2308 cmds (Printexc.to_string exn);
2309 exit 1;
2311 let y = (getpagey pageno) + truncate y0 in
2312 addnav ();
2313 gotoy y;
2314 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2316 | "match" ->
2317 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2319 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2320 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2321 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2322 with exn ->
2323 dolog "error processing 'match' %S: %s"
2324 cmds (Printexc.to_string exn);
2325 exit 1;
2327 state.rects1 <-
2328 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2330 | "page" ->
2331 let pageopaque, t =
2333 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2334 with exn ->
2335 dolog "error processing 'page' %S: %s"
2336 cmds (Printexc.to_string exn);
2337 exit 1;
2339 begin match state.currently with
2340 | Loading (l, gen) ->
2341 vlog "page %d took %f sec" l.pageno t;
2342 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2343 begin match state.throttle with
2344 | None ->
2345 let preloadedpages =
2346 if conf.preload
2347 then preloadlayout state.y
2348 else state.layout
2350 let evict () =
2351 let module IntSet =
2352 Set.Make (struct type t = int let compare = (-) end) in
2353 let set =
2354 List.fold_left (fun s l -> IntSet.add l.pageno s)
2355 IntSet.empty preloadedpages
2357 let evictedpages =
2358 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2359 if not (IntSet.mem pageno set)
2360 then (
2361 wcmd "freepage %s" opaque;
2362 key :: accu
2364 else accu
2365 ) state.pagemap []
2367 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2369 evict ();
2370 state.currently <- Idle;
2371 if gen = state.gen
2372 then (
2373 tilepage l.pageno pageopaque state.layout;
2374 load state.layout;
2375 load preloadedpages;
2376 if pagevisible state.layout l.pageno
2377 && layoutready state.layout
2378 then G.postRedisplay "page";
2381 | Some (layout, _, _) ->
2382 state.currently <- Idle;
2383 tilepage l.pageno pageopaque layout;
2384 load state.layout
2385 end;
2387 | _ ->
2388 dolog "Inconsistent loading state";
2389 logcurrently state.currently;
2390 exit 1
2393 | "tile" ->
2394 let (x, y, opaque, size, t) =
2396 Scanf.sscanf args "%u %u %s %u %f"
2397 (fun x y p size t -> (x, y, p, size, t))
2398 with exn ->
2399 dolog "error processing 'tile' %S: %s"
2400 cmds (Printexc.to_string exn);
2401 exit 1;
2403 begin match state.currently with
2404 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2405 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2407 unmappbo opaque;
2408 if tilew != conf.tilew || tileh != conf.tileh
2409 then (
2410 wcmd "freetile %s" opaque;
2411 state.currently <- Idle;
2412 load state.layout;
2414 else (
2415 puttileopaque l col row gen cs angle opaque size t;
2416 state.memused <- state.memused + size;
2417 state.uioh#infochanged Memused;
2418 gctiles ();
2419 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2420 opaque, size) state.tilelru;
2422 let layout =
2423 match state.throttle with
2424 | None -> state.layout
2425 | Some (layout, _, _) -> layout
2428 state.currently <- Idle;
2429 if gen = state.gen
2430 && conf.colorspace = cs
2431 && conf.angle = angle
2432 && tilevisible layout l.pageno x y
2433 then conttiling l.pageno pageopaque;
2435 begin match state.throttle with
2436 | None ->
2437 state.wthack <- false;
2438 preload state.layout;
2439 if gen = state.gen
2440 && conf.colorspace = cs
2441 && conf.angle = angle
2442 && tilevisible state.layout l.pageno x y
2443 then G.postRedisplay "tile nothrottle";
2445 | Some (layout, y, _) ->
2446 let ready = layoutready layout in
2447 if ready
2448 then (
2449 state.wthack <- false;
2450 state.y <- y;
2451 state.layout <- layout;
2452 state.throttle <- None;
2453 G.postRedisplay "throttle";
2455 else load layout;
2456 end;
2459 | _ ->
2460 dolog "Inconsistent tiling state";
2461 logcurrently state.currently;
2462 exit 1
2465 | "pdim" ->
2466 let pdim =
2468 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2469 with exn ->
2470 dolog "error processing 'pdim' %S: %s"
2471 cmds (Printexc.to_string exn);
2472 exit 1;
2474 state.uioh#infochanged Pdim;
2475 state.pdims <- pdim :: state.pdims
2477 | "o" ->
2478 let (l, n, t, h, pos) =
2480 Scanf.sscanf args "%u %u %d %u %n"
2481 (fun l n t h pos -> l, n, t, h, pos)
2482 with exn ->
2483 dolog "error processing 'o' %S: %s"
2484 cmds (Printexc.to_string exn);
2485 exit 1;
2487 let s = String.sub args pos (String.length args - pos) in
2488 let outline = (s, l, (n, float t /. float h, 0.0)) in
2489 begin match state.currently with
2490 | Outlining outlines ->
2491 state.currently <- Outlining (outline :: outlines)
2492 | Idle ->
2493 state.currently <- Outlining [outline]
2494 | currently ->
2495 dolog "invalid outlining state";
2496 logcurrently currently
2499 | "info" ->
2500 state.docinfo <- (1, args) :: state.docinfo
2502 | "infoend" ->
2503 state.uioh#infochanged Docinfo;
2504 state.docinfo <- List.rev state.docinfo
2506 | _ ->
2507 dolog "unknown cmd `%S'" cmds
2510 let onhist cb =
2511 let rc = cb.rc in
2512 let action = function
2513 | HCprev -> cbget cb ~-1
2514 | HCnext -> cbget cb 1
2515 | HCfirst -> cbget cb ~-(cb.rc)
2516 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2517 and cancel () = cb.rc <- rc
2518 in (action, cancel)
2521 let search pattern forward =
2522 if String.length pattern > 0
2523 then
2524 let pn, py =
2525 match state.layout with
2526 | [] -> 0, 0
2527 | l :: _ ->
2528 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2530 wcmd "search %d %d %d %d,%s\000"
2531 (btod conf.icase) pn py (btod forward) pattern;
2534 let intentry text key =
2535 let c =
2536 if key >= 32 && key < 127
2537 then Char.chr key
2538 else '\000'
2540 match c with
2541 | '0' .. '9' ->
2542 let text = addchar text c in
2543 TEcont text
2545 | _ ->
2546 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2547 TEcont text
2550 let linknentry text key =
2551 let c =
2552 if key >= 32 && key < 127
2553 then Char.chr key
2554 else '\000'
2556 match c with
2557 | 'a' .. 'z' ->
2558 let text = addchar text c in
2559 TEcont text
2561 | _ ->
2562 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2563 TEcont text
2566 let linkndone f s =
2567 if String.length s > 0
2568 then (
2569 let n =
2570 let l = String.length s in
2571 let rec loop pos n = if pos = l then n else
2572 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2573 loop (pos+1) (n*26 + m)
2574 in loop 0 0
2576 let rec loop n = function
2577 | [] -> ()
2578 | l :: rest ->
2579 match getopaque l.pageno with
2580 | None -> loop n rest
2581 | Some opaque ->
2582 let m = getlinkcount opaque in
2583 if n < m
2584 then (
2585 let under = getlink opaque n in
2586 f under
2588 else loop (n-m) rest
2590 loop n state.layout;
2594 let textentry text key =
2595 if key land 0xff00 = 0xff00
2596 then TEcont text
2597 else TEcont (text ^ Wsi.toutf8 key)
2600 let reqlayout angle proportional =
2601 match state.throttle with
2602 | None ->
2603 if nogeomcmds state.geomcmds
2604 then state.anchor <- getanchor ();
2605 conf.angle <- angle mod 360;
2606 if conf.angle != 0
2607 then (
2608 match state.mode with
2609 | LinkNav _ -> state.mode <- View
2610 | _ -> ()
2612 conf.proportional <- proportional;
2613 invalidate "reqlayout"
2614 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2615 | _ -> ()
2618 let settrim trimmargins trimfuzz =
2619 if nogeomcmds state.geomcmds
2620 then state.anchor <- getanchor ();
2621 conf.trimmargins <- trimmargins;
2622 conf.trimfuzz <- trimfuzz;
2623 let x0, y0, x1, y1 = trimfuzz in
2624 invalidate "settrim"
2625 (fun () ->
2626 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2627 flushpages ();
2630 let setzoom zoom =
2631 match state.throttle with
2632 | None ->
2633 let zoom = max 0.01 zoom in
2634 if zoom <> conf.zoom
2635 then (
2636 state.prevzoom <- conf.zoom;
2637 conf.zoom <- zoom;
2638 reshape conf.winw conf.winh;
2639 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2642 | Some (layout, y, started) ->
2643 let time =
2644 match conf.maxwait with
2645 | None -> 0.0
2646 | Some t -> t
2648 let dt = now () -. started in
2649 if dt > time
2650 then (
2651 state.y <- y;
2652 load layout;
2656 let setcolumns mode columns coverA coverB =
2657 state.prevcolumns <- Some (conf.columns, conf.zoom);
2658 if columns < 0
2659 then (
2660 if isbirdseye mode
2661 then showtext '!' "split mode doesn't work in bird's eye"
2662 else (
2663 conf.columns <- Csplit (-columns, [||]);
2664 state.x <- 0;
2665 conf.zoom <- 1.0;
2668 else (
2669 if columns < 2
2670 then (
2671 conf.columns <- Csingle [||];
2672 state.x <- 0;
2673 setzoom 1.0;
2675 else (
2676 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2677 conf.zoom <- 1.0;
2680 reshape conf.winw conf.winh;
2683 let enterbirdseye () =
2684 let zoom = float conf.thumbw /. float conf.winw in
2685 let birdseyepageno =
2686 let cy = conf.winh / 2 in
2687 let fold = function
2688 | [] -> 0
2689 | l :: rest ->
2690 let rec fold best = function
2691 | [] -> best.pageno
2692 | l :: rest ->
2693 let d = cy - (l.pagedispy + l.pagevh/2)
2694 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2695 if abs d < abs dbest
2696 then fold l rest
2697 else best.pageno
2698 in fold l rest
2700 fold state.layout
2702 state.mode <- Birdseye (
2703 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2705 conf.zoom <- zoom;
2706 conf.presentation <- false;
2707 conf.interpagespace <- 10;
2708 conf.hlinks <- false;
2709 state.x <- 0;
2710 state.mstate <- Mnone;
2711 conf.maxwait <- None;
2712 conf.columns <- (
2713 match conf.beyecolumns with
2714 | Some c ->
2715 conf.zoom <- 1.0;
2716 Cmulti ((c, 0, 0), [||])
2717 | None -> Csingle [||]
2719 Wsi.setcursor Wsi.CURSOR_INHERIT;
2720 if conf.verbose
2721 then
2722 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2723 (100.0*.zoom)
2724 else
2725 state.text <- ""
2727 reshape conf.winw conf.winh;
2730 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2731 state.mode <- View;
2732 conf.zoom <- c.zoom;
2733 conf.presentation <- c.presentation;
2734 conf.interpagespace <- c.interpagespace;
2735 conf.maxwait <- c.maxwait;
2736 conf.hlinks <- c.hlinks;
2737 conf.beyecolumns <- (
2738 match conf.columns with
2739 | Cmulti ((c, _, _), _) -> Some c
2740 | Csingle _ -> None
2741 | Csplit _ -> failwith "leaving bird's eye split mode"
2743 conf.columns <- (
2744 match c.columns with
2745 | Cmulti (c, _) -> Cmulti (c, [||])
2746 | Csingle _ -> Csingle [||]
2747 | Csplit (c, _) -> Csplit (c, [||])
2749 state.x <- leftx;
2750 if conf.verbose
2751 then
2752 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2753 (100.0*.conf.zoom)
2755 reshape conf.winw conf.winh;
2756 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2759 let togglebirdseye () =
2760 match state.mode with
2761 | Birdseye vals -> leavebirdseye vals true
2762 | View -> enterbirdseye ()
2763 | _ -> ()
2766 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2767 let pageno = max 0 (pageno - incr) in
2768 let rec loop = function
2769 | [] -> gotopage1 pageno 0
2770 | l :: _ when l.pageno = pageno ->
2771 if l.pagedispy >= 0 && l.pagey = 0
2772 then G.postRedisplay "upbirdseye"
2773 else gotopage1 pageno 0
2774 | _ :: rest -> loop rest
2776 loop state.layout;
2777 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2780 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2781 let pageno = min (state.pagecount - 1) (pageno + incr) in
2782 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2783 let rec loop = function
2784 | [] ->
2785 let y, h = getpageyh pageno in
2786 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2787 gotoy (clamp dy)
2788 | l :: _ when l.pageno = pageno ->
2789 if l.pagevh != l.pageh
2790 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2791 else G.postRedisplay "downbirdseye"
2792 | _ :: rest -> loop rest
2794 loop state.layout
2797 let optentry mode _ key =
2798 let btos b = if b then "on" else "off" in
2799 if key >= 32 && key < 127
2800 then
2801 let c = Char.chr key in
2802 match c with
2803 | 's' ->
2804 let ondone s =
2805 try conf.scrollstep <- int_of_string s with exc ->
2806 state.text <- Printf.sprintf "bad integer `%s': %s"
2807 s (Printexc.to_string exc)
2809 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2811 | 'A' ->
2812 let ondone s =
2814 conf.autoscrollstep <- int_of_string s;
2815 if state.autoscroll <> None
2816 then state.autoscroll <- Some conf.autoscrollstep
2817 with exc ->
2818 state.text <- Printf.sprintf "bad integer `%s': %s"
2819 s (Printexc.to_string exc)
2821 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2823 | 'C' ->
2824 let ondone s =
2826 let n, a, b = multicolumns_of_string s in
2827 setcolumns mode n a b;
2828 with exc ->
2829 state.text <- Printf.sprintf "bad columns `%s': %s"
2830 s (Printexc.to_string exc)
2832 TEswitch ("columns: ", "", None, textentry, ondone, true)
2834 | 'Z' ->
2835 let ondone s =
2837 let zoom = float (int_of_string s) /. 100.0 in
2838 setzoom zoom
2839 with exc ->
2840 state.text <- Printf.sprintf "bad integer `%s': %s"
2841 s (Printexc.to_string exc)
2843 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2845 | 't' ->
2846 let ondone s =
2848 conf.thumbw <- bound (int_of_string s) 2 4096;
2849 state.text <-
2850 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2851 begin match mode with
2852 | Birdseye beye ->
2853 leavebirdseye beye false;
2854 enterbirdseye ();
2855 | _ -> ();
2857 with exc ->
2858 state.text <- Printf.sprintf "bad integer `%s': %s"
2859 s (Printexc.to_string exc)
2861 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2863 | 'R' ->
2864 let ondone s =
2865 match try
2866 Some (int_of_string s)
2867 with exc ->
2868 state.text <- Printf.sprintf "bad integer `%s': %s"
2869 s (Printexc.to_string exc);
2870 None
2871 with
2872 | Some angle -> reqlayout angle conf.proportional
2873 | None -> ()
2875 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2877 | 'i' ->
2878 conf.icase <- not conf.icase;
2879 TEdone ("case insensitive search " ^ (btos conf.icase))
2881 | 'p' ->
2882 conf.preload <- not conf.preload;
2883 gotoy state.y;
2884 TEdone ("preload " ^ (btos conf.preload))
2886 | 'v' ->
2887 conf.verbose <- not conf.verbose;
2888 TEdone ("verbose " ^ (btos conf.verbose))
2890 | 'd' ->
2891 conf.debug <- not conf.debug;
2892 TEdone ("debug " ^ (btos conf.debug))
2894 | 'h' ->
2895 conf.maxhfit <- not conf.maxhfit;
2896 state.maxy <- calcheight ();
2897 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2899 | 'c' ->
2900 conf.crophack <- not conf.crophack;
2901 TEdone ("crophack " ^ btos conf.crophack)
2903 | 'a' ->
2904 let s =
2905 match conf.maxwait with
2906 | None ->
2907 conf.maxwait <- Some infinity;
2908 "always wait for page to complete"
2909 | Some _ ->
2910 conf.maxwait <- None;
2911 "show placeholder if page is not ready"
2913 TEdone s
2915 | 'f' ->
2916 conf.underinfo <- not conf.underinfo;
2917 TEdone ("underinfo " ^ btos conf.underinfo)
2919 | 'P' ->
2920 conf.savebmarks <- not conf.savebmarks;
2921 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2923 | 'S' ->
2924 let ondone s =
2926 let pageno, py =
2927 match state.layout with
2928 | [] -> 0, 0
2929 | l :: _ ->
2930 l.pageno, l.pagey
2932 conf.interpagespace <- int_of_string s;
2933 docolumns conf.columns;
2934 state.maxy <- calcheight ();
2935 let y = getpagey pageno in
2936 gotoy (y + py)
2937 with exc ->
2938 state.text <- Printf.sprintf "bad integer `%s': %s"
2939 s (Printexc.to_string exc)
2941 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2943 | 'l' ->
2944 reqlayout conf.angle (not conf.proportional);
2945 TEdone ("proportional display " ^ btos conf.proportional)
2947 | 'T' ->
2948 settrim (not conf.trimmargins) conf.trimfuzz;
2949 TEdone ("trim margins " ^ btos conf.trimmargins)
2951 | 'I' ->
2952 conf.invert <- not conf.invert;
2953 TEdone ("invert colors " ^ btos conf.invert)
2955 | 'x' ->
2956 let ondone s =
2957 cbput state.hists.sel s;
2958 conf.selcmd <- s;
2960 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2961 textentry, ondone, true)
2963 | _ ->
2964 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2965 TEstop
2966 else
2967 TEcont state.text
2970 class type lvsource = object
2971 method getitemcount : int
2972 method getitem : int -> (string * int)
2973 method hasaction : int -> bool
2974 method exit :
2975 uioh:uioh ->
2976 cancel:bool ->
2977 active:int ->
2978 first:int ->
2979 pan:int ->
2980 qsearch:string ->
2981 uioh option
2982 method getactive : int
2983 method getfirst : int
2984 method getqsearch : string
2985 method setqsearch : string -> unit
2986 method getpan : int
2987 end;;
2989 class virtual lvsourcebase = object
2990 val mutable m_active = 0
2991 val mutable m_first = 0
2992 val mutable m_qsearch = ""
2993 val mutable m_pan = 0
2994 method getactive = m_active
2995 method getfirst = m_first
2996 method getqsearch = m_qsearch
2997 method getpan = m_pan
2998 method setqsearch s = m_qsearch <- s
2999 end;;
3001 let withoutlastutf8 s =
3002 let len = String.length s in
3003 if len = 0
3004 then s
3005 else
3006 let rec find pos =
3007 if pos = 0
3008 then pos
3009 else
3010 let b = Char.code s.[pos] in
3011 if b land 0b11000000 = 0b11000000
3012 then pos
3013 else find (pos-1)
3015 let first =
3016 if Char.code s.[len-1] land 0x80 = 0
3017 then len-1
3018 else find (len-1)
3020 String.sub s 0 first;
3023 let textentrykeyboard
3024 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3025 let key =
3026 if key >= 0xffb0 && key <= 0xffb9
3027 then key - 0xffb0 + 48 else key
3029 let enttext te =
3030 state.mode <- Textentry (te, onleave);
3031 state.text <- "";
3032 enttext ();
3033 G.postRedisplay "textentrykeyboard enttext";
3035 let histaction cmd =
3036 match opthist with
3037 | None -> ()
3038 | Some (action, _) ->
3039 state.mode <- Textentry (
3040 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3042 G.postRedisplay "textentry histaction"
3044 match key with
3045 | 0xff08 -> (* backspace *)
3046 let s = withoutlastutf8 text in
3047 let len = String.length s in
3048 if cancelonempty && len = 0
3049 then (
3050 onleave Cancel;
3051 G.postRedisplay "textentrykeyboard after cancel";
3053 else (
3054 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3057 | 0xff0d | 0xff8d -> (* (kp) enter *)
3058 ondone text;
3059 onleave Confirm;
3060 G.postRedisplay "textentrykeyboard after confirm"
3062 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3063 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3064 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3065 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3067 | 0xff1b -> (* escape*)
3068 if String.length text = 0
3069 then (
3070 begin match opthist with
3071 | None -> ()
3072 | Some (_, onhistcancel) -> onhistcancel ()
3073 end;
3074 onleave Cancel;
3075 state.text <- "";
3076 G.postRedisplay "textentrykeyboard after cancel2"
3078 else (
3079 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3082 | 0xff9f | 0xffff -> () (* delete *)
3084 | _ when key != 0
3085 && key land 0xff00 != 0xff00 (* keyboard *)
3086 && key land 0xfe00 != 0xfe00 (* xkb *)
3087 && key land 0xfd00 != 0xfd00 (* 3270 *)
3089 begin match onkey text key with
3090 | TEdone text ->
3091 ondone text;
3092 onleave Confirm;
3093 G.postRedisplay "textentrykeyboard after confirm2";
3095 | TEcont text ->
3096 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3098 | TEstop ->
3099 onleave Cancel;
3100 G.postRedisplay "textentrykeyboard after cancel3"
3102 | TEswitch te ->
3103 state.mode <- Textentry (te, onleave);
3104 G.postRedisplay "textentrykeyboard switch";
3105 end;
3107 | _ ->
3108 vlog "unhandled key %s" (Wsi.keyname key)
3111 let firstof first active =
3112 if first > active || abs (first - active) > fstate.maxrows - 1
3113 then max 0 (active - (fstate.maxrows/2))
3114 else first
3117 let calcfirst first active =
3118 if active > first
3119 then
3120 let rows = active - first in
3121 if rows > fstate.maxrows then active - fstate.maxrows else first
3122 else active
3125 let scrollph y maxy =
3126 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3127 let sh = float conf.winh /. sh in
3128 let sh = max sh (float conf.scrollh) in
3130 let percent =
3131 if y = state.maxy
3132 then 1.0
3133 else float y /. float maxy
3135 let position = (float conf.winh -. sh) *. percent in
3137 let position =
3138 if position +. sh > float conf.winh
3139 then float conf.winh -. sh
3140 else position
3142 position, sh;
3145 let coe s = (s :> uioh);;
3147 class listview ~(source:lvsource) ~trusted ~modehash =
3148 object (self)
3149 val m_pan = source#getpan
3150 val m_first = source#getfirst
3151 val m_active = source#getactive
3152 val m_qsearch = source#getqsearch
3153 val m_prev_uioh = state.uioh
3155 method private elemunder y =
3156 let n = y / (fstate.fontsize+1) in
3157 if m_first + n < source#getitemcount
3158 then (
3159 if source#hasaction (m_first + n)
3160 then Some (m_first + n)
3161 else None
3163 else None
3165 method display =
3166 Gl.enable `blend;
3167 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3168 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3169 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3170 GlDraw.color (1., 1., 1.);
3171 Gl.enable `texture_2d;
3172 let fs = fstate.fontsize in
3173 let nfs = fs + 1 in
3174 let ww = fstate.wwidth in
3175 let tabw = 30.0*.ww in
3176 let itemcount = source#getitemcount in
3177 let rec loop row =
3178 if (row - m_first) > fstate.maxrows
3179 then ()
3180 else (
3181 if row >= 0 && row < itemcount
3182 then (
3183 let (s, level) = source#getitem row in
3184 let y = (row - m_first) * nfs in
3185 let x = 5.0 +. float (level + m_pan) *. ww in
3186 if row = m_active
3187 then (
3188 Gl.disable `texture_2d;
3189 GlDraw.polygon_mode `both `line;
3190 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3191 GlDraw.rect (1., float (y + 1))
3192 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3193 GlDraw.polygon_mode `both `fill;
3194 GlDraw.color (1., 1., 1.);
3195 Gl.enable `texture_2d;
3198 let drawtabularstring s =
3199 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3200 if trusted
3201 then
3202 let tabpos = try String.index s '\t' with Not_found -> -1 in
3203 if tabpos > 0
3204 then
3205 let len = String.length s - tabpos - 1 in
3206 let s1 = String.sub s 0 tabpos
3207 and s2 = String.sub s (tabpos + 1) len in
3208 let nx = drawstr x s1 in
3209 let sw = nx -. x in
3210 let x = x +. (max tabw sw) in
3211 drawstr x s2
3212 else
3213 drawstr x s
3214 else
3215 drawstr x s
3217 let _ = drawtabularstring s in
3218 loop (row+1)
3222 loop m_first;
3223 Gl.disable `blend;
3224 Gl.disable `texture_2d;
3226 method updownlevel incr =
3227 let len = source#getitemcount in
3228 let curlevel =
3229 if m_active >= 0 && m_active < len
3230 then snd (source#getitem m_active)
3231 else -1
3233 let rec flow i =
3234 if i = len then i-1 else if i = -1 then 0 else
3235 let _, l = source#getitem i in
3236 if l != curlevel then i else flow (i+incr)
3238 let active = flow m_active in
3239 let first = calcfirst m_first active in
3240 G.postRedisplay "outline updownlevel";
3241 {< m_active = active; m_first = first >}
3243 method private key1 key mask =
3244 let set1 active first qsearch =
3245 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3247 let search active pattern incr =
3248 let dosearch re =
3249 let rec loop n =
3250 if n >= 0 && n < source#getitemcount
3251 then (
3252 let s, _ = source#getitem n in
3254 (try ignore (Str.search_forward re s 0); true
3255 with Not_found -> false)
3256 then Some n
3257 else loop (n + incr)
3259 else None
3261 loop active
3264 let re = Str.regexp_case_fold pattern in
3265 dosearch re
3266 with Failure s ->
3267 state.text <- s;
3268 None
3270 let itemcount = source#getitemcount in
3271 let find start incr =
3272 let rec find i =
3273 if i = -1 || i = itemcount
3274 then -1
3275 else (
3276 if source#hasaction i
3277 then i
3278 else find (i + incr)
3281 find start
3283 let set active first =
3284 let first = bound first 0 (itemcount - fstate.maxrows) in
3285 state.text <- "";
3286 coe {< m_active = active; m_first = first >}
3288 let navigate incr =
3289 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3290 let active, first =
3291 let incr1 = if incr > 0 then 1 else -1 in
3292 if isvisible m_first m_active
3293 then
3294 let next =
3295 let next = m_active + incr in
3296 let next =
3297 if next < 0 || next >= itemcount
3298 then -1
3299 else find next incr1
3301 if next = -1 || abs (m_active - next) > fstate.maxrows
3302 then -1
3303 else next
3305 if next = -1
3306 then
3307 let first = m_first + incr in
3308 let first = bound first 0 (itemcount - 1) in
3309 let next =
3310 let next = m_active + incr in
3311 let next = bound next 0 (itemcount - 1) in
3312 find next ~-incr1
3314 let active = if next = -1 then m_active else next in
3315 active, first
3316 else
3317 let first = min next m_first in
3318 let first =
3319 if abs (next - first) > fstate.maxrows
3320 then first + incr
3321 else first
3323 next, first
3324 else
3325 let first = m_first + incr in
3326 let first = bound first 0 (itemcount - 1) in
3327 let active =
3328 let next = m_active + incr in
3329 let next = bound next 0 (itemcount - 1) in
3330 let next = find next incr1 in
3331 let active =
3332 if next = -1 || abs (m_active - first) > fstate.maxrows
3333 then (
3334 let active = if m_active = -1 then next else m_active in
3335 active
3337 else next
3339 if isvisible first active
3340 then active
3341 else -1
3343 active, first
3345 G.postRedisplay "listview navigate";
3346 set active first;
3348 match key with
3349 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3350 let incr = if key = 0x72 then -1 else 1 in
3351 let active, first =
3352 match search (m_active + incr) m_qsearch incr with
3353 | None ->
3354 state.text <- m_qsearch ^ " [not found]";
3355 m_active, m_first
3356 | Some active ->
3357 state.text <- m_qsearch;
3358 active, firstof m_first active
3360 G.postRedisplay "listview ctrl-r/s";
3361 set1 active first m_qsearch;
3363 | 0xff08 -> (* backspace *)
3364 if String.length m_qsearch = 0
3365 then coe self
3366 else (
3367 let qsearch = withoutlastutf8 m_qsearch in
3368 let len = String.length qsearch in
3369 if len = 0
3370 then (
3371 state.text <- "";
3372 G.postRedisplay "listview empty qsearch";
3373 set1 m_active m_first "";
3375 else
3376 let active, first =
3377 match search m_active qsearch ~-1 with
3378 | None ->
3379 state.text <- qsearch ^ " [not found]";
3380 m_active, m_first
3381 | Some active ->
3382 state.text <- qsearch;
3383 active, firstof m_first active
3385 G.postRedisplay "listview backspace qsearch";
3386 set1 active first qsearch
3389 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3390 let pattern = m_qsearch ^ Wsi.toutf8 key in
3391 let active, first =
3392 match search m_active pattern 1 with
3393 | None ->
3394 state.text <- pattern ^ " [not found]";
3395 m_active, m_first
3396 | Some active ->
3397 state.text <- pattern;
3398 active, firstof m_first active
3400 G.postRedisplay "listview qsearch add";
3401 set1 active first pattern;
3403 | 0xff1b -> (* escape *)
3404 state.text <- "";
3405 if String.length m_qsearch = 0
3406 then (
3407 G.postRedisplay "list view escape";
3408 begin
3409 match
3410 source#exit (coe self) true m_active m_first m_pan m_qsearch
3411 with
3412 | None -> m_prev_uioh
3413 | Some uioh -> uioh
3416 else (
3417 G.postRedisplay "list view kill qsearch";
3418 source#setqsearch "";
3419 coe {< m_qsearch = "" >}
3422 | 0xff0d | 0xff8d -> (* (kp) enter *)
3423 state.text <- "";
3424 let self = {< m_qsearch = "" >} in
3425 source#setqsearch "";
3426 let opt =
3427 G.postRedisplay "listview enter";
3428 if m_active >= 0 && m_active < source#getitemcount
3429 then (
3430 source#exit (coe self) false m_active m_first m_pan "";
3432 else (
3433 source#exit (coe self) true m_active m_first m_pan "";
3436 begin match opt with
3437 | None -> m_prev_uioh
3438 | Some uioh -> uioh
3441 | 0xff9f | 0xffff -> (* (kp) delete *)
3442 coe self
3444 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3445 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3446 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3447 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3449 | 0xff53 | 0xff98 -> (* (kp) right *)
3450 state.text <- "";
3451 G.postRedisplay "listview right";
3452 coe {< m_pan = m_pan - 1 >}
3454 | 0xff51 | 0xff96 -> (* (kp) left *)
3455 state.text <- "";
3456 G.postRedisplay "listview left";
3457 coe {< m_pan = m_pan + 1 >}
3459 | 0xff50 | 0xff95 -> (* (kp) home *)
3460 let active = find 0 1 in
3461 G.postRedisplay "listview home";
3462 set active 0;
3464 | 0xff57 | 0xff9c -> (* (kp) end *)
3465 let first = max 0 (itemcount - fstate.maxrows) in
3466 let active = find (itemcount - 1) ~-1 in
3467 G.postRedisplay "listview end";
3468 set active first;
3470 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3471 coe self
3473 | _ ->
3474 dolog "listview unknown key %#x" key; coe self
3476 method key key mask =
3477 match state.mode with
3478 | Textentry te -> textentrykeyboard key mask te; coe self
3479 | _ -> self#key1 key mask
3481 method button button down x y _ =
3482 let opt =
3483 match button with
3484 | 1 when x > conf.winw - conf.scrollbw ->
3485 G.postRedisplay "listview scroll";
3486 if down
3487 then
3488 let _, position, sh = self#scrollph in
3489 if y > truncate position && y < truncate (position +. sh)
3490 then (
3491 state.mstate <- Mscrolly;
3492 Some (coe self)
3494 else
3495 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3496 let first = truncate (s *. float source#getitemcount) in
3497 let first = min source#getitemcount first in
3498 Some (coe {< m_first = first; m_active = first >})
3499 else (
3500 state.mstate <- Mnone;
3501 Some (coe self);
3503 | 1 when not down ->
3504 begin match self#elemunder y with
3505 | Some n ->
3506 G.postRedisplay "listview click";
3507 source#exit
3508 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3509 | _ ->
3510 Some (coe self)
3512 | n when (n == 4 || n == 5) && not down ->
3513 let len = source#getitemcount in
3514 let first =
3515 if n = 5 && m_first + fstate.maxrows >= len
3516 then
3517 m_first
3518 else
3519 let first = m_first + (if n == 4 then -1 else 1) in
3520 bound first 0 (len - 1)
3522 G.postRedisplay "listview wheel";
3523 Some (coe {< m_first = first >})
3524 | n when (n = 6 || n = 7) && not down ->
3525 let inc = m_first + (if n = 7 then -1 else 1) in
3526 G.postRedisplay "listview hwheel";
3527 Some (coe {< m_pan = m_pan + inc >})
3528 | _ ->
3529 Some (coe self)
3531 match opt with
3532 | None -> m_prev_uioh
3533 | Some uioh -> uioh
3535 method motion _ y =
3536 match state.mstate with
3537 | Mscrolly ->
3538 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3539 let first = truncate (s *. float source#getitemcount) in
3540 let first = min source#getitemcount first in
3541 G.postRedisplay "listview motion";
3542 coe {< m_first = first; m_active = first >}
3543 | _ -> coe self
3545 method pmotion x y =
3546 if x < conf.winw - conf.scrollbw
3547 then
3548 let n =
3549 match self#elemunder y with
3550 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3551 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3553 let o =
3554 if n != m_active
3555 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3556 else self
3558 coe o
3559 else (
3560 Wsi.setcursor Wsi.CURSOR_INHERIT;
3561 coe self
3564 method infochanged _ = ()
3566 method scrollpw = (0, 0.0, 0.0)
3567 method scrollph =
3568 let nfs = fstate.fontsize + 1 in
3569 let y = m_first * nfs in
3570 let itemcount = source#getitemcount in
3571 let maxi = max 0 (itemcount - fstate.maxrows) in
3572 let maxy = maxi * nfs in
3573 let p, h = scrollph y maxy in
3574 conf.scrollbw, p, h
3576 method modehash = modehash
3577 end;;
3579 class outlinelistview ~source =
3580 object (self)
3581 inherit listview
3582 ~source:(source :> lvsource)
3583 ~trusted:false
3584 ~modehash:(findkeyhash conf "outline")
3585 as super
3587 method key key mask =
3588 let calcfirst first active =
3589 if active > first
3590 then
3591 let rows = active - first in
3592 let maxrows =
3593 if String.length state.text = 0
3594 then fstate.maxrows
3595 else fstate.maxrows - 2
3597 if rows > maxrows then active - maxrows else first
3598 else active
3600 let navigate incr =
3601 let active = m_active + incr in
3602 let active = bound active 0 (source#getitemcount - 1) in
3603 let first = calcfirst m_first active in
3604 G.postRedisplay "outline navigate";
3605 coe {< m_active = active; m_first = first >}
3607 let ctrl = Wsi.withctrl mask in
3608 match key with
3609 | 110 when ctrl -> (* ctrl-n *)
3610 source#narrow m_qsearch;
3611 G.postRedisplay "outline ctrl-n";
3612 coe {< m_first = 0; m_active = 0 >}
3614 | 117 when ctrl -> (* ctrl-u *)
3615 source#denarrow;
3616 G.postRedisplay "outline ctrl-u";
3617 state.text <- "";
3618 coe {< m_first = 0; m_active = 0 >}
3620 | 108 when ctrl -> (* ctrl-l *)
3621 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3622 G.postRedisplay "outline ctrl-l";
3623 coe {< m_first = first >}
3625 | 0xff9f | 0xffff -> (* (kp) delete *)
3626 source#remove m_active;
3627 G.postRedisplay "outline delete";
3628 let active = max 0 (m_active-1) in
3629 coe {< m_first = firstof m_first active;
3630 m_active = active >}
3632 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3633 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3634 | 0xff55 | 0xff9a -> (* (kp) prior *)
3635 navigate ~-(fstate.maxrows)
3636 | 0xff56 | 0xff9b -> (* (kp) next *)
3637 navigate fstate.maxrows
3639 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3640 let o =
3641 if ctrl
3642 then (
3643 G.postRedisplay "outline ctrl right";
3644 {< m_pan = m_pan + 1 >}
3646 else self#updownlevel 1
3648 coe o
3650 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3651 let o =
3652 if ctrl
3653 then (
3654 G.postRedisplay "outline ctrl left";
3655 {< m_pan = m_pan - 1 >}
3657 else self#updownlevel ~-1
3659 coe o
3661 | 0xff50 | 0xff95 -> (* (kp) home *)
3662 G.postRedisplay "outline home";
3663 coe {< m_first = 0; m_active = 0 >}
3665 | 0xff57 | 0xff9c -> (* (kp) end *)
3666 let active = source#getitemcount - 1 in
3667 let first = max 0 (active - fstate.maxrows) in
3668 G.postRedisplay "outline end";
3669 coe {< m_active = active; m_first = first >}
3671 | _ -> super#key key mask
3674 let outlinesource usebookmarks =
3675 let empty = [||] in
3676 (object
3677 inherit lvsourcebase
3678 val mutable m_items = empty
3679 val mutable m_orig_items = empty
3680 val mutable m_prev_items = empty
3681 val mutable m_narrow_pattern = ""
3682 val mutable m_hadremovals = false
3684 method getitemcount =
3685 Array.length m_items + (if m_hadremovals then 1 else 0)
3687 method getitem n =
3688 if n == Array.length m_items && m_hadremovals
3689 then
3690 ("[Confirm removal]", 0)
3691 else
3692 let s, n, _ = m_items.(n) in
3693 (s, n)
3695 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3696 ignore (uioh, first, qsearch);
3697 let confrimremoval = m_hadremovals && active = Array.length m_items in
3698 let items =
3699 if String.length m_narrow_pattern = 0
3700 then m_orig_items
3701 else m_items
3703 if not cancel
3704 then (
3705 if not confrimremoval
3706 then(
3707 let _, _, anchor = m_items.(active) in
3708 gotoghyll (getanchory anchor);
3709 m_items <- items;
3711 else (
3712 state.bookmarks <- Array.to_list m_items;
3713 m_orig_items <- m_items;
3716 else m_items <- items;
3717 m_pan <- pan;
3718 None
3720 method hasaction _ = true
3722 method greetmsg =
3723 if Array.length m_items != Array.length m_orig_items
3724 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3725 else ""
3727 method narrow pattern =
3728 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3729 match reopt with
3730 | None -> ()
3731 | Some re ->
3732 let rec loop accu n =
3733 if n = -1
3734 then (
3735 m_narrow_pattern <- pattern;
3736 m_items <- Array.of_list accu
3738 else
3739 let (s, _, _) as o = m_items.(n) in
3740 let accu =
3741 if (try ignore (Str.search_forward re s 0); true
3742 with Not_found -> false)
3743 then o :: accu
3744 else accu
3746 loop accu (n-1)
3748 loop [] (Array.length m_items - 1)
3750 method denarrow =
3751 m_orig_items <- (
3752 if usebookmarks
3753 then Array.of_list state.bookmarks
3754 else state.outlines
3756 m_items <- m_orig_items
3758 method remove m =
3759 if usebookmarks
3760 then
3761 if m >= 0 && m < Array.length m_items
3762 then (
3763 m_hadremovals <- true;
3764 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3765 let n = if n >= m then n+1 else n in
3766 m_items.(n)
3770 method reset anchor items =
3771 m_hadremovals <- false;
3772 if m_orig_items == empty || m_prev_items != items
3773 then (
3774 m_orig_items <- items;
3775 if String.length m_narrow_pattern = 0
3776 then m_items <- items;
3778 m_prev_items <- items;
3779 let rely = getanchory anchor in
3780 let active =
3781 let rec loop n best bestd =
3782 if n = Array.length m_items
3783 then best
3784 else
3785 let (_, _, anchor) = m_items.(n) in
3786 let orely = getanchory anchor in
3787 let d = abs (orely - rely) in
3788 if d < bestd
3789 then loop (n+1) n d
3790 else loop (n+1) best bestd
3792 loop 0 ~-1 max_int
3794 m_active <- active;
3795 m_first <- firstof m_first active
3796 end)
3799 let enterselector usebookmarks =
3800 let source = outlinesource usebookmarks in
3801 fun errmsg ->
3802 let outlines =
3803 if usebookmarks
3804 then Array.of_list state.bookmarks
3805 else state.outlines
3807 if Array.length outlines = 0
3808 then (
3809 showtext ' ' errmsg;
3811 else (
3812 state.text <- source#greetmsg;
3813 Wsi.setcursor Wsi.CURSOR_INHERIT;
3814 let anchor = getanchor () in
3815 source#reset anchor outlines;
3816 state.uioh <- coe (new outlinelistview ~source);
3817 G.postRedisplay "enter selector";
3821 let enteroutlinemode =
3822 let f = enterselector false in
3823 fun ()-> f "Document has no outline";
3826 let enterbookmarkmode =
3827 let f = enterselector true in
3828 fun () -> f "Document has no bookmarks (yet)";
3831 let color_of_string s =
3832 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3833 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3837 let color_to_string (r, g, b) =
3838 let r = truncate (r *. 256.0)
3839 and g = truncate (g *. 256.0)
3840 and b = truncate (b *. 256.0) in
3841 Printf.sprintf "%d/%d/%d" r g b
3844 let irect_of_string s =
3845 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3848 let irect_to_string (x0,y0,x1,y1) =
3849 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3852 let makecheckers () =
3853 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3854 following to say:
3855 converted by Issac Trotts. July 25, 2002 *)
3856 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3857 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3858 let id = GlTex.gen_texture () in
3859 GlTex.bind_texture `texture_2d id;
3860 GlPix.store (`unpack_alignment 1);
3861 GlTex.image2d image;
3862 List.iter (GlTex.parameter ~target:`texture_2d)
3863 [ `wrap_s `repeat;
3864 `wrap_t `repeat;
3865 `mag_filter `nearest;
3866 `min_filter `nearest ];
3870 let setcheckers enabled =
3871 match state.texid with
3872 | None ->
3873 if enabled then state.texid <- Some (makecheckers ())
3875 | Some texid ->
3876 if not enabled
3877 then (
3878 GlTex.delete_texture texid;
3879 state.texid <- None;
3883 let int_of_string_with_suffix s =
3884 let l = String.length s in
3885 let s1, shift =
3886 if l > 1
3887 then
3888 let suffix = Char.lowercase s.[l-1] in
3889 match suffix with
3890 | 'k' -> String.sub s 0 (l-1), 10
3891 | 'm' -> String.sub s 0 (l-1), 20
3892 | 'g' -> String.sub s 0 (l-1), 30
3893 | _ -> s, 0
3894 else s, 0
3896 let n = int_of_string s1 in
3897 let m = n lsl shift in
3898 if m < 0 || m < n
3899 then raise (Failure "value too large")
3900 else m
3903 let string_with_suffix_of_int n =
3904 if n = 0
3905 then "0"
3906 else
3907 let n, s =
3908 if n land ((1 lsl 30) - 1) = 0
3909 then n lsr 30, "G"
3910 else (
3911 if n land ((1 lsl 20) - 1) = 0
3912 then n lsr 20, "M"
3913 else (
3914 if n land ((1 lsl 10) - 1) = 0
3915 then n lsr 10, "K"
3916 else n, ""
3920 let rec loop s n =
3921 let h = n mod 1000 in
3922 let n = n / 1000 in
3923 if n = 0
3924 then string_of_int h ^ s
3925 else (
3926 let s = Printf.sprintf "_%03d%s" h s in
3927 loop s n
3930 loop "" n ^ s;
3933 let defghyllscroll = (40, 8, 32);;
3934 let ghyllscroll_of_string s =
3935 let (n, a, b) as nab =
3936 if s = "default"
3937 then defghyllscroll
3938 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3940 if n <= a || n <= b || a >= b
3941 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3942 nab;
3945 let ghyllscroll_to_string ((n, a, b) as nab) =
3946 if nab = defghyllscroll
3947 then "default"
3948 else Printf.sprintf "%d,%d,%d" n a b;
3951 let describe_location () =
3952 let f (fn, _) l =
3953 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3955 let fn, ln = List.fold_left f (-1, -1) state.layout in
3956 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3957 let percent =
3958 if maxy <= 0
3959 then 100.
3960 else (100. *. (float state.y /. float maxy))
3962 if fn = ln
3963 then
3964 Printf.sprintf "page %d of %d [%.2f%%]"
3965 (fn+1) state.pagecount percent
3966 else
3967 Printf.sprintf
3968 "pages %d-%d of %d [%.2f%%]"
3969 (fn+1) (ln+1) state.pagecount percent
3972 let setpresentationmode v =
3973 let (n, _, _) = getanchor () in
3974 let _, h = getpageyh n in
3975 let ips = if conf.presentation then calcips h else conf.interpagespace in
3976 state.anchor <- (n, 0.0, float ips);
3977 conf.presentation <- v;
3978 if conf.presentation
3979 then (
3980 if not conf.scrollbarinpm
3981 then state.scrollw <- 0;
3983 else state.scrollw <- conf.scrollbw;
3984 represent ();
3987 let enterinfomode =
3988 let btos b = if b then "\xe2\x88\x9a" else "" in
3989 let showextended = ref false in
3990 let leave mode = function
3991 | Confirm -> state.mode <- mode
3992 | Cancel -> state.mode <- mode in
3993 let src =
3994 (object
3995 val mutable m_first_time = true
3996 val mutable m_l = []
3997 val mutable m_a = [||]
3998 val mutable m_prev_uioh = nouioh
3999 val mutable m_prev_mode = View
4001 inherit lvsourcebase
4003 method reset prev_mode prev_uioh =
4004 m_a <- Array.of_list (List.rev m_l);
4005 m_l <- [];
4006 m_prev_mode <- prev_mode;
4007 m_prev_uioh <- prev_uioh;
4008 if m_first_time
4009 then (
4010 let rec loop n =
4011 if n >= Array.length m_a
4012 then ()
4013 else
4014 match m_a.(n) with
4015 | _, _, _, Action _ -> m_active <- n
4016 | _ -> loop (n+1)
4018 loop 0;
4019 m_first_time <- false;
4022 method int name get set =
4023 m_l <-
4024 (name, `int get, 1, Action (
4025 fun u ->
4026 let ondone s =
4027 try set (int_of_string s)
4028 with exn ->
4029 state.text <- Printf.sprintf "bad integer `%s': %s"
4030 s (Printexc.to_string exn)
4032 state.text <- "";
4033 let te = name ^ ": ", "", None, intentry, ondone, true in
4034 state.mode <- Textentry (te, leave m_prev_mode);
4036 )) :: m_l
4038 method int_with_suffix name get set =
4039 m_l <-
4040 (name, `intws get, 1, Action (
4041 fun u ->
4042 let ondone s =
4043 try set (int_of_string_with_suffix s)
4044 with exn ->
4045 state.text <- Printf.sprintf "bad integer `%s': %s"
4046 s (Printexc.to_string exn)
4048 state.text <- "";
4049 let te =
4050 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4052 state.mode <- Textentry (te, leave m_prev_mode);
4054 )) :: m_l
4056 method bool ?(offset=1) ?(btos=btos) name get set =
4057 m_l <-
4058 (name, `bool (btos, get), offset, Action (
4059 fun u ->
4060 let v = get () in
4061 set (not v);
4063 )) :: m_l
4065 method color name get set =
4066 m_l <-
4067 (name, `color get, 1, Action (
4068 fun u ->
4069 let invalid = (nan, nan, nan) in
4070 let ondone s =
4071 let c =
4072 try color_of_string s
4073 with exn ->
4074 state.text <- Printf.sprintf "bad color `%s': %s"
4075 s (Printexc.to_string exn);
4076 invalid
4078 if c <> invalid
4079 then set c;
4081 let te = name ^ ": ", "", None, textentry, ondone, true in
4082 state.text <- color_to_string (get ());
4083 state.mode <- Textentry (te, leave m_prev_mode);
4085 )) :: m_l
4087 method string name get set =
4088 m_l <-
4089 (name, `string get, 1, Action (
4090 fun u ->
4091 let ondone s = set s in
4092 let te = name ^ ": ", "", None, textentry, ondone, true in
4093 state.mode <- Textentry (te, leave m_prev_mode);
4095 )) :: m_l
4097 method colorspace name get set =
4098 m_l <-
4099 (name, `string get, 1, Action (
4100 fun _ ->
4101 let source =
4102 let vals = [| "rgb"; "bgr"; "gray" |] in
4103 (object
4104 inherit lvsourcebase
4106 initializer
4107 m_active <- int_of_colorspace conf.colorspace;
4108 m_first <- 0;
4110 method getitemcount = Array.length vals
4111 method getitem n = (vals.(n), 0)
4112 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4113 ignore (uioh, first, pan, qsearch);
4114 if not cancel then set active;
4115 None
4116 method hasaction _ = true
4117 end)
4119 state.text <- "";
4120 let modehash = findkeyhash conf "info" in
4121 coe (new listview ~source ~trusted:true ~modehash)
4122 )) :: m_l
4124 method caption s offset =
4125 m_l <- (s, `empty, offset, Noaction) :: m_l
4127 method caption2 s f offset =
4128 m_l <- (s, `string f, offset, Noaction) :: m_l
4130 method getitemcount = Array.length m_a
4132 method getitem n =
4133 let tostr = function
4134 | `int f -> string_of_int (f ())
4135 | `intws f -> string_with_suffix_of_int (f ())
4136 | `string f -> f ()
4137 | `color f -> color_to_string (f ())
4138 | `bool (btos, f) -> btos (f ())
4139 | `empty -> ""
4141 let name, t, offset, _ = m_a.(n) in
4142 ((let s = tostr t in
4143 if String.length s > 0
4144 then Printf.sprintf "%s\t%s" name s
4145 else name),
4146 offset)
4148 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4149 let uiohopt =
4150 if not cancel
4151 then (
4152 m_qsearch <- qsearch;
4153 let uioh =
4154 match m_a.(active) with
4155 | _, _, _, Action f -> f uioh
4156 | _ -> uioh
4158 Some uioh
4160 else None
4162 m_active <- active;
4163 m_first <- first;
4164 m_pan <- pan;
4165 uiohopt
4167 method hasaction n =
4168 match m_a.(n) with
4169 | _, _, _, Action _ -> true
4170 | _ -> false
4171 end)
4173 let rec fillsrc prevmode prevuioh =
4174 let sep () = src#caption "" 0 in
4175 let colorp name get set =
4176 src#string name
4177 (fun () -> color_to_string (get ()))
4178 (fun v ->
4180 let c = color_of_string v in
4181 set c
4182 with exn ->
4183 state.text <- Printf.sprintf "bad color `%s': %s"
4184 v (Printexc.to_string exn);
4187 let oldmode = state.mode in
4188 let birdseye = isbirdseye state.mode in
4190 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4192 src#bool "presentation mode"
4193 (fun () -> conf.presentation)
4194 (fun v -> setpresentationmode v);
4196 src#bool "ignore case in searches"
4197 (fun () -> conf.icase)
4198 (fun v -> conf.icase <- v);
4200 src#bool "preload"
4201 (fun () -> conf.preload)
4202 (fun v -> conf.preload <- v);
4204 src#bool "highlight links"
4205 (fun () -> conf.hlinks)
4206 (fun v -> conf.hlinks <- v);
4208 src#bool "under info"
4209 (fun () -> conf.underinfo)
4210 (fun v -> conf.underinfo <- v);
4212 src#bool "persistent bookmarks"
4213 (fun () -> conf.savebmarks)
4214 (fun v -> conf.savebmarks <- v);
4216 src#bool "proportional display"
4217 (fun () -> conf.proportional)
4218 (fun v -> reqlayout conf.angle v);
4220 src#bool "trim margins"
4221 (fun () -> conf.trimmargins)
4222 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4224 src#bool "persistent location"
4225 (fun () -> conf.jumpback)
4226 (fun v -> conf.jumpback <- v);
4228 sep ();
4229 src#int "inter-page space"
4230 (fun () -> conf.interpagespace)
4231 (fun n ->
4232 conf.interpagespace <- n;
4233 docolumns conf.columns;
4234 let pageno, py =
4235 match state.layout with
4236 | [] -> 0, 0
4237 | l :: _ ->
4238 l.pageno, l.pagey
4240 state.maxy <- calcheight ();
4241 let y = getpagey pageno in
4242 gotoy (y + py)
4245 src#int "page bias"
4246 (fun () -> conf.pagebias)
4247 (fun v -> conf.pagebias <- v);
4249 src#int "scroll step"
4250 (fun () -> conf.scrollstep)
4251 (fun n -> conf.scrollstep <- n);
4253 src#int "horizontal scroll step"
4254 (fun () -> conf.hscrollstep)
4255 (fun v -> conf.hscrollstep <- v);
4257 src#int "auto scroll step"
4258 (fun () ->
4259 match state.autoscroll with
4260 | Some step -> step
4261 | _ -> conf.autoscrollstep)
4262 (fun n ->
4263 if state.autoscroll <> None
4264 then state.autoscroll <- Some n;
4265 conf.autoscrollstep <- n);
4267 src#int "zoom"
4268 (fun () -> truncate (conf.zoom *. 100.))
4269 (fun v -> setzoom ((float v) /. 100.));
4271 src#int "rotation"
4272 (fun () -> conf.angle)
4273 (fun v -> reqlayout v conf.proportional);
4275 src#int "scroll bar width"
4276 (fun () -> state.scrollw)
4277 (fun v ->
4278 state.scrollw <- v;
4279 conf.scrollbw <- v;
4280 reshape conf.winw conf.winh;
4283 src#int "scroll handle height"
4284 (fun () -> conf.scrollh)
4285 (fun v -> conf.scrollh <- v;);
4287 src#int "thumbnail width"
4288 (fun () -> conf.thumbw)
4289 (fun v ->
4290 conf.thumbw <- min 4096 v;
4291 match oldmode with
4292 | Birdseye beye ->
4293 leavebirdseye beye false;
4294 enterbirdseye ()
4295 | _ -> ()
4298 let mode = state.mode in
4299 src#string "columns"
4300 (fun () ->
4301 match conf.columns with
4302 | Csingle _ -> "1"
4303 | Cmulti (multi, _) -> multicolumns_to_string multi
4304 | Csplit (count, _) -> "-" ^ string_of_int count
4306 (fun v ->
4307 let n, a, b = multicolumns_of_string v in
4308 setcolumns mode n a b);
4310 sep ();
4311 src#caption "Presentation mode" 0;
4312 src#bool "scrollbar visible"
4313 (fun () -> conf.scrollbarinpm)
4314 (fun v ->
4315 if v != conf.scrollbarinpm
4316 then (
4317 conf.scrollbarinpm <- v;
4318 if conf.presentation
4319 then (
4320 state.scrollw <- if v then conf.scrollbw else 0;
4321 reshape conf.winw conf.winh;
4326 sep ();
4327 src#caption "Pixmap cache" 0;
4328 src#int_with_suffix "size (advisory)"
4329 (fun () -> conf.memlimit)
4330 (fun v -> conf.memlimit <- v);
4332 src#caption2 "used"
4333 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4334 (string_with_suffix_of_int state.memused)
4335 (Hashtbl.length state.tilemap)) 1;
4337 sep ();
4338 src#caption "Layout" 0;
4339 src#caption2 "Dimension"
4340 (fun () ->
4341 Printf.sprintf "%dx%d (virtual %dx%d)"
4342 conf.winw conf.winh
4343 state.w state.maxy)
4345 if conf.debug
4346 then
4347 src#caption2 "Position" (fun () ->
4348 Printf.sprintf "%dx%d" state.x state.y
4350 else
4351 src#caption2 "Visible" (fun () -> describe_location ()) 1
4354 sep ();
4355 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4356 "Save these parameters as global defaults at exit"
4357 (fun () -> conf.bedefault)
4358 (fun v -> conf.bedefault <- v)
4361 sep ();
4362 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4363 src#bool ~offset:0 ~btos "Extended parameters"
4364 (fun () -> !showextended)
4365 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4366 if !showextended
4367 then (
4368 src#bool "checkers"
4369 (fun () -> conf.checkers)
4370 (fun v -> conf.checkers <- v; setcheckers v);
4371 src#bool "update cursor"
4372 (fun () -> conf.updatecurs)
4373 (fun v -> conf.updatecurs <- v);
4374 src#bool "verbose"
4375 (fun () -> conf.verbose)
4376 (fun v -> conf.verbose <- v);
4377 src#bool "invert colors"
4378 (fun () -> conf.invert)
4379 (fun v -> conf.invert <- v);
4380 src#bool "max fit"
4381 (fun () -> conf.maxhfit)
4382 (fun v -> conf.maxhfit <- v);
4383 src#bool "redirect stderr"
4384 (fun () -> conf.redirectstderr)
4385 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4386 src#string "uri launcher"
4387 (fun () -> conf.urilauncher)
4388 (fun v -> conf.urilauncher <- v);
4389 src#string "path launcher"
4390 (fun () -> conf.pathlauncher)
4391 (fun v -> conf.pathlauncher <- v);
4392 src#string "tile size"
4393 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4394 (fun v ->
4396 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4397 conf.tilew <- max 64 w;
4398 conf.tileh <- max 64 h;
4399 flushtiles ();
4400 with exn ->
4401 state.text <- Printf.sprintf "bad tile size `%s': %s"
4402 v (Printexc.to_string exn));
4403 src#int "texture count"
4404 (fun () -> conf.texcount)
4405 (fun v ->
4406 if realloctexts v
4407 then conf.texcount <- v
4408 else showtext '!' " Failed to set texture count please retry later"
4410 src#int "slice height"
4411 (fun () -> conf.sliceheight)
4412 (fun v ->
4413 conf.sliceheight <- v;
4414 wcmd "sliceh %d" conf.sliceheight;
4416 src#int "anti-aliasing level"
4417 (fun () -> conf.aalevel)
4418 (fun v ->
4419 conf.aalevel <- bound v 0 8;
4420 state.anchor <- getanchor ();
4421 opendoc state.path state.password;
4423 src#string "page scroll scaling factor"
4424 (fun () -> string_of_float conf.pgscale)
4425 (fun v ->
4427 let s = float_of_string v in
4428 conf.pgscale <- s
4429 with exn ->
4430 state.text <- Printf.sprintf
4431 "bad page scroll scaling factor `%s': %s"
4432 v (Printexc.to_string exn)
4435 src#int "ui font size"
4436 (fun () -> fstate.fontsize)
4437 (fun v -> setfontsize (bound v 5 100));
4438 src#int "hint font size"
4439 (fun () -> conf.hfsize)
4440 (fun v -> conf.hfsize <- bound v 5 100);
4441 colorp "background color"
4442 (fun () -> conf.bgcolor)
4443 (fun v -> conf.bgcolor <- v);
4444 src#bool "crop hack"
4445 (fun () -> conf.crophack)
4446 (fun v -> conf.crophack <- v);
4447 src#string "trim fuzz"
4448 (fun () -> irect_to_string conf.trimfuzz)
4449 (fun v ->
4451 conf.trimfuzz <- irect_of_string v;
4452 if conf.trimmargins
4453 then settrim true conf.trimfuzz;
4454 with exn ->
4455 state.text <- Printf.sprintf "bad irect `%s': %s"
4456 v (Printexc.to_string exn)
4458 src#string "throttle"
4459 (fun () ->
4460 match conf.maxwait with
4461 | None -> "show place holder if page is not ready"
4462 | Some time ->
4463 if time = infinity
4464 then "wait for page to fully render"
4465 else
4466 "wait " ^ string_of_float time
4467 ^ " seconds before showing placeholder"
4469 (fun v ->
4471 let f = float_of_string v in
4472 if f <= 0.0
4473 then conf.maxwait <- None
4474 else conf.maxwait <- Some f
4475 with exn ->
4476 state.text <- Printf.sprintf "bad time `%s': %s"
4477 v (Printexc.to_string exn)
4479 src#string "ghyll scroll"
4480 (fun () ->
4481 match conf.ghyllscroll with
4482 | None -> ""
4483 | Some nab -> ghyllscroll_to_string nab
4485 (fun v ->
4487 let gs =
4488 if String.length v = 0
4489 then None
4490 else Some (ghyllscroll_of_string v)
4492 conf.ghyllscroll <- gs
4493 with exn ->
4494 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4495 v (Printexc.to_string exn)
4497 src#string "selection command"
4498 (fun () -> conf.selcmd)
4499 (fun v -> conf.selcmd <- v);
4500 src#colorspace "color space"
4501 (fun () -> colorspace_to_string conf.colorspace)
4502 (fun v ->
4503 conf.colorspace <- colorspace_of_int v;
4504 wcmd "cs %d" v;
4505 load state.layout;
4507 if pbousable ()
4508 then
4509 src#bool "use PBO"
4510 (fun () -> conf.usepbo)
4511 (fun v -> conf.usepbo <- v);
4514 sep ();
4515 src#caption "Document" 0;
4516 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4517 src#caption2 "Pages"
4518 (fun () -> string_of_int state.pagecount) 1;
4519 src#caption2 "Dimensions"
4520 (fun () -> string_of_int (List.length state.pdims)) 1;
4521 if conf.trimmargins
4522 then (
4523 sep ();
4524 src#caption "Trimmed margins" 0;
4525 src#caption2 "Dimensions"
4526 (fun () -> string_of_int (List.length state.pdims)) 1;
4529 sep ();
4530 src#caption "OpenGL" 0;
4531 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4532 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4533 src#reset prevmode prevuioh;
4535 fun () ->
4536 state.text <- "";
4537 let prevmode = state.mode
4538 and prevuioh = state.uioh in
4539 fillsrc prevmode prevuioh;
4540 let source = (src :> lvsource) in
4541 let modehash = findkeyhash conf "info" in
4542 state.uioh <- coe (object (self)
4543 inherit listview ~source ~trusted:true ~modehash as super
4544 val mutable m_prevmemused = 0
4545 method infochanged = function
4546 | Memused ->
4547 if m_prevmemused != state.memused
4548 then (
4549 m_prevmemused <- state.memused;
4550 G.postRedisplay "memusedchanged";
4552 | Pdim -> G.postRedisplay "pdimchanged"
4553 | Docinfo -> fillsrc prevmode prevuioh
4555 method key key mask =
4556 if not (Wsi.withctrl mask)
4557 then
4558 match key with
4559 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4560 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4561 | _ -> super#key key mask
4562 else super#key key mask
4563 end);
4564 G.postRedisplay "info";
4567 let enterhelpmode =
4568 let source =
4569 (object
4570 inherit lvsourcebase
4571 method getitemcount = Array.length state.help
4572 method getitem n =
4573 let s, l, _ = state.help.(n) in
4574 (s, l)
4576 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4577 let optuioh =
4578 if not cancel
4579 then (
4580 m_qsearch <- qsearch;
4581 match state.help.(active) with
4582 | _, _, Action f -> Some (f uioh)
4583 | _ -> Some (uioh)
4585 else None
4587 m_active <- active;
4588 m_first <- first;
4589 m_pan <- pan;
4590 optuioh
4592 method hasaction n =
4593 match state.help.(n) with
4594 | _, _, Action _ -> true
4595 | _ -> false
4597 initializer
4598 m_active <- -1
4599 end)
4600 in fun () ->
4601 let modehash = findkeyhash conf "help" in
4602 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4603 G.postRedisplay "help";
4606 let entermsgsmode =
4607 let msgsource =
4608 let re = Str.regexp "[\r\n]" in
4609 (object
4610 inherit lvsourcebase
4611 val mutable m_items = [||]
4613 method getitemcount = 1 + Array.length m_items
4615 method getitem n =
4616 if n = 0
4617 then "[Clear]", 0
4618 else m_items.(n-1), 0
4620 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4621 ignore uioh;
4622 if not cancel
4623 then (
4624 if active = 0
4625 then Buffer.clear state.errmsgs;
4626 m_qsearch <- qsearch;
4628 m_active <- active;
4629 m_first <- first;
4630 m_pan <- pan;
4631 None
4633 method hasaction n =
4634 n = 0
4636 method reset =
4637 state.newerrmsgs <- false;
4638 let l = Str.split re (Buffer.contents state.errmsgs) in
4639 m_items <- Array.of_list l
4641 initializer
4642 m_active <- 0
4643 end)
4644 in fun () ->
4645 state.text <- "";
4646 msgsource#reset;
4647 let source = (msgsource :> lvsource) in
4648 let modehash = findkeyhash conf "listview" in
4649 state.uioh <- coe (object
4650 inherit listview ~source ~trusted:false ~modehash as super
4651 method display =
4652 if state.newerrmsgs
4653 then msgsource#reset;
4654 super#display
4655 end);
4656 G.postRedisplay "msgs";
4659 let quickbookmark ?title () =
4660 match state.layout with
4661 | [] -> ()
4662 | l :: _ ->
4663 let title =
4664 match title with
4665 | None ->
4666 let sec = Unix.gettimeofday () in
4667 let tm = Unix.localtime sec in
4668 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4669 (l.pageno+1)
4670 tm.Unix.tm_mday
4671 tm.Unix.tm_mon
4672 (tm.Unix.tm_year + 1900)
4673 tm.Unix.tm_hour
4674 tm.Unix.tm_min
4675 | Some title -> title
4677 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4680 let doreshape w h =
4681 state.fullscreen <- None;
4682 Wsi.reshape w h;
4685 let setautoscrollspeed step goingdown =
4686 let incr = max 1 ((abs step) / 2) in
4687 let incr = if goingdown then incr else -incr in
4688 let astep = step + incr in
4689 state.autoscroll <- Some astep;
4692 let gotounder = function
4693 | Ulinkgoto (pageno, top) ->
4694 if pageno >= 0
4695 then (
4696 addnav ();
4697 gotopage1 pageno top;
4700 | Ulinkuri s ->
4701 gotouri s
4703 | Uremote (filename, pageno) ->
4704 let path =
4705 if Sys.file_exists filename
4706 then filename
4707 else
4708 let dir = Filename.dirname state.path in
4709 let path = Filename.concat dir filename in
4710 if Sys.file_exists path
4711 then path
4712 else ""
4714 if String.length path > 0
4715 then (
4716 let anchor = getanchor () in
4717 let ranchor = state.path, state.password, anchor in
4718 state.anchor <- (pageno, 0.0, 0.0);
4719 state.ranchors <- ranchor :: state.ranchors;
4720 opendoc path "";
4722 else showtext '!' ("Could not find " ^ filename)
4724 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4727 let canpan () =
4728 match conf.columns with
4729 | Csplit _ -> true
4730 | _ -> conf.zoom > 1.0
4733 let viewkeyboard key mask =
4734 let enttext te =
4735 let mode = state.mode in
4736 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4737 state.text <- "";
4738 enttext ();
4739 G.postRedisplay "view:enttext"
4741 let ctrl = Wsi.withctrl mask in
4742 let existsinrow pageno (columns, coverA, coverB) p =
4743 let last = ((pageno - coverA) mod columns) + columns in
4744 let rec any = function
4745 | [] -> false
4746 | l :: rest ->
4747 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4748 then p l
4749 else (
4750 if not (p l)
4751 then (if l.pageno = last then false else any rest)
4752 else true
4755 any state.layout
4757 let key =
4758 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4760 match key with
4761 | 81 -> (* Q *)
4762 exit 0
4764 | 0xff63 -> (* insert *)
4765 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4766 then (
4767 state.mode <- LinkNav (Ltgendir 0);
4768 gotoy state.y;
4770 else showtext '!' "Keyboard link navigation does not work under rotation"
4772 | 0xff1b | 113 -> (* escape / q *)
4773 begin match state.mstate with
4774 | Mzoomrect _ ->
4775 state.mstate <- Mnone;
4776 Wsi.setcursor Wsi.CURSOR_INHERIT;
4777 G.postRedisplay "kill zoom rect";
4778 | _ ->
4779 begin match state.mode with
4780 | LinkNav _ ->
4781 state.mode <- View;
4782 G.postRedisplay "esc leave linknav"
4783 | _ ->
4784 match state.ranchors with
4785 | [] -> raise Quit
4786 | (path, password, anchor) :: rest ->
4787 state.ranchors <- rest;
4788 state.anchor <- anchor;
4789 opendoc path password
4790 end;
4791 end;
4793 | 0xff08 -> (* backspace *)
4794 gotoghyll (getnav ~-1)
4796 | 111 -> (* o *)
4797 enteroutlinemode ()
4799 | 117 -> (* u *)
4800 state.rects <- [];
4801 state.text <- "";
4802 G.postRedisplay "dehighlight";
4804 | 47 | 63 -> (* / ? *)
4805 let ondone isforw s =
4806 cbput state.hists.pat s;
4807 state.searchpattern <- s;
4808 search s isforw
4810 let s = String.create 1 in
4811 s.[0] <- Char.chr key;
4812 enttext (s, "", Some (onhist state.hists.pat),
4813 textentry, ondone (key = 47), true)
4815 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4816 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4817 setzoom (conf.zoom +. incr)
4819 | 43 | 0xffab -> (* + *)
4820 let ondone s =
4821 let n =
4822 try int_of_string s with exc ->
4823 state.text <- Printf.sprintf "bad integer `%s': %s"
4824 s (Printexc.to_string exc);
4825 max_int
4827 if n != max_int
4828 then (
4829 conf.pagebias <- n;
4830 state.text <- "page bias is now " ^ string_of_int n;
4833 enttext ("page bias: ", "", None, intentry, ondone, true)
4835 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4836 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4837 setzoom (max 0.01 (conf.zoom -. decr))
4839 | 45 | 0xffad -> (* - *)
4840 let ondone msg = state.text <- msg in
4841 enttext (
4842 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4843 optentry state.mode, ondone, true
4846 | 48 when ctrl -> (* ctrl-0 *)
4847 setzoom 1.0
4849 | 49 when ctrl -> (* ctrl-1 *)
4850 let cols =
4851 match conf.columns with
4852 | Csingle _ | Cmulti _ -> 1
4853 | Csplit (n, _) -> n
4855 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4856 if zoom < 1.0
4857 then setzoom zoom
4859 | 0xffc6 -> (* f9 *)
4860 togglebirdseye ()
4862 | 57 when ctrl -> (* ctrl-9 *)
4863 togglebirdseye ()
4865 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4866 when not ctrl -> (* 0..9 *)
4867 let ondone s =
4868 let n =
4869 try int_of_string s with exc ->
4870 state.text <- Printf.sprintf "bad integer `%s': %s"
4871 s (Printexc.to_string exc);
4874 if n >= 0
4875 then (
4876 addnav ();
4877 cbput state.hists.pag (string_of_int n);
4878 gotopage1 (n + conf.pagebias - 1) 0;
4881 let pageentry text key =
4882 match Char.unsafe_chr key with
4883 | 'g' -> TEdone text
4884 | _ -> intentry text key
4886 let text = "x" in text.[0] <- Char.chr key;
4887 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4889 | 98 -> (* b *)
4890 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4891 reshape conf.winw conf.winh;
4893 | 108 -> (* l *)
4894 conf.hlinks <- not conf.hlinks;
4895 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4896 G.postRedisplay "toggle highlightlinks";
4898 | 70 -> (* F *)
4899 state.glinks <- true;
4900 let mode = state.mode in
4901 state.mode <- Textentry (
4902 (":", "", None, linknentry, linkndone gotounder, false),
4903 (fun _ ->
4904 state.glinks <- false;
4905 state.mode <- mode)
4907 state.text <- "";
4908 G.postRedisplay "view:linkent(F)"
4910 | 121 -> (* y *)
4911 state.glinks <- true;
4912 let mode = state.mode in
4913 state.mode <- Textentry (
4914 (":", "", None, linknentry, linkndone (fun under ->
4915 match Ne.pipe () with
4916 | Ne.Exn exn ->
4917 showtext '!' (Printf.sprintf "pipe failed: %s"
4918 (Printexc.to_string exn));
4919 | Ne.Res (r, w) ->
4920 let popened =
4921 try popen conf.selcmd [r, 0; w, -1]; true
4922 with exn ->
4923 showtext '!'
4924 (Printf.sprintf "failed to execute %s: %s"
4925 conf.selcmd (Printexc.to_string exn));
4926 false
4928 let clo cap fd =
4929 Ne.clo fd (fun msg ->
4930 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4933 let s = undertext under in
4934 if popened
4935 then
4936 (try
4937 let l = String.length s in
4938 let n = Unix.write w s 0 l in
4939 if n != l
4940 then
4941 showtext '!'
4942 (Printf.sprintf
4943 "failed to write %d characters to sel pipe, wrote %d"
4946 with exn ->
4947 showtext '!'
4948 (Printf.sprintf "failed to write to sel pipe: %s"
4949 (Printexc.to_string exn)
4952 else dolog "%s" s;
4953 clo "pipe/r" r;
4954 clo "pipe/w" w;
4955 ), false
4957 fun _ ->
4958 state.glinks <- false;
4959 state.mode <- mode
4961 state.text <- "";
4962 G.postRedisplay "view:linkent"
4964 | 97 -> (* a *)
4965 begin match state.autoscroll with
4966 | Some step ->
4967 conf.autoscrollstep <- step;
4968 state.autoscroll <- None
4969 | None ->
4970 if conf.autoscrollstep = 0
4971 then state.autoscroll <- Some 1
4972 else state.autoscroll <- Some conf.autoscrollstep
4975 | 112 when ctrl -> (* ctrl-p *)
4976 launchpath ()
4978 | 80 -> (* P *)
4979 setpresentationmode (not conf.presentation);
4980 showtext ' ' ("presentation mode " ^
4981 if conf.presentation then "on" else "off");
4983 | 102 -> (* f *)
4984 begin match state.fullscreen with
4985 | None ->
4986 state.fullscreen <- Some (conf.winw, conf.winh);
4987 Wsi.fullscreen ()
4988 | Some (w, h) ->
4989 state.fullscreen <- None;
4990 doreshape w h
4993 | 112 | 78 -> (* p|N *)
4994 search state.searchpattern false
4996 | 110 | 0xffc0 -> (* n|F3 *)
4997 search state.searchpattern true
4999 | 116 -> (* t *)
5000 begin match state.layout with
5001 | [] -> ()
5002 | l :: _ ->
5003 gotoy_and_clear_text (getpagey l.pageno)
5006 | 32 -> (* space *)
5007 begin match state.layout with
5008 | [] -> ()
5009 | l :: rest ->
5010 match conf.columns with
5011 | Csingle _ ->
5012 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5013 then
5014 let y = clamp (pgscale conf.winh) in
5015 gotoghyll y
5016 else
5017 let pageno = min (l.pageno+1) (state.pagecount-1) in
5018 gotoghyll (getpagey pageno)
5019 | Cmulti ((c, _, _) as cl, _) ->
5020 if conf.presentation
5021 && (existsinrow l.pageno cl
5022 (fun l -> l.pageh > l.pagey + l.pagevh))
5023 then
5024 let y = clamp (pgscale conf.winh) in
5025 gotoghyll y
5026 else
5027 let pageno = min (l.pageno+c) (state.pagecount-1) in
5028 gotoghyll (getpagey pageno)
5029 | Csplit (n, _) ->
5030 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5031 then
5032 let pagey, pageh = getpageyh l.pageno in
5033 let pagey = pagey + pageh * l.pagecol in
5034 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5035 gotoghyll (pagey + pageh + ips)
5038 | 0xff9f | 0xffff -> (* delete *)
5039 begin match state.layout with
5040 | [] -> ()
5041 | l :: _ ->
5042 match conf.columns with
5043 | Csingle _ ->
5044 if conf.presentation && l.pagey != 0
5045 then
5046 gotoghyll (clamp (pgscale ~-(conf.winh)))
5047 else
5048 let pageno = max 0 (l.pageno-1) in
5049 gotoghyll (getpagey pageno)
5050 | Cmulti ((c, _, coverB) as cl, _) ->
5051 if conf.presentation &&
5052 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5053 then
5054 gotoghyll (clamp (pgscale ~-(conf.winh)))
5055 else
5056 let decr =
5057 if l.pageno = state.pagecount - coverB
5058 then 1
5059 else c
5061 let pageno = max 0 (l.pageno-decr) in
5062 gotoghyll (getpagey pageno)
5063 | Csplit (n, _) ->
5064 let y =
5065 if l.pagecol = 0
5066 then
5067 if l.pageno = 0
5068 then l.pagey
5069 else
5070 let pageno = max 0 (l.pageno-1) in
5071 let pagey, pageh = getpageyh pageno in
5072 pagey + (n-1)*pageh
5073 else
5074 let pagey, pageh = getpageyh l.pageno in
5075 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5077 gotoghyll y
5080 | 61 -> (* = *)
5081 showtext ' ' (describe_location ());
5083 | 119 -> (* w *)
5084 begin match state.layout with
5085 | [] -> ()
5086 | l :: _ ->
5087 doreshape (l.pagew + state.scrollw) l.pageh;
5088 G.postRedisplay "w"
5091 | 39 -> (* ' *)
5092 enterbookmarkmode ()
5094 | 104 | 0xffbe -> (* h|F1 *)
5095 enterhelpmode ()
5097 | 105 -> (* i *)
5098 enterinfomode ()
5100 | 101 when conf.redirectstderr -> (* e *)
5101 entermsgsmode ()
5103 | 109 -> (* m *)
5104 let ondone s =
5105 match state.layout with
5106 | l :: _ ->
5107 if String.length s > 0
5108 then
5109 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5110 | _ -> ()
5112 enttext ("bookmark: ", "", None, textentry, ondone, true)
5114 | 126 -> (* ~ *)
5115 quickbookmark ();
5116 showtext ' ' "Quick bookmark added";
5118 | 122 -> (* z *)
5119 begin match state.layout with
5120 | l :: _ ->
5121 let rect = getpdimrect l.pagedimno in
5122 let w, h =
5123 if conf.crophack
5124 then
5125 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5126 truncate (1.2 *. (rect.(3) -. rect.(0))))
5127 else
5128 (truncate (rect.(1) -. rect.(0)),
5129 truncate (rect.(3) -. rect.(0)))
5131 let w = truncate ((float w)*.conf.zoom)
5132 and h = truncate ((float h)*.conf.zoom) in
5133 if w != 0 && h != 0
5134 then (
5135 state.anchor <- getanchor ();
5136 doreshape (w + state.scrollw) (h + conf.interpagespace)
5138 G.postRedisplay "z";
5140 | [] -> ()
5143 | 50 when ctrl -> (* ctrl-2 *)
5144 let maxw = getmaxw () in
5145 if maxw > 0.0
5146 then setzoom (maxw /. float conf.winw)
5148 | 60 | 62 -> (* < > *)
5149 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5151 | 91 | 93 -> (* [ ] *)
5152 conf.colorscale <-
5153 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5155 G.postRedisplay "brightness";
5157 | 99 when state.mode = View -> (* c *)
5158 let (c, a, b), z =
5159 match state.prevcolumns with
5160 | None -> (1, 0, 0), 1.0
5161 | Some (columns, z) ->
5162 let cab =
5163 match columns with
5164 | Csplit (c, _) -> -c, 0, 0
5165 | Cmulti ((c, a, b), _) -> c, a, b
5166 | Csingle _ -> 1, 0, 0
5168 cab, z
5170 setcolumns View c a b;
5171 setzoom z;
5173 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5174 setzoom state.prevzoom
5176 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5177 begin match state.autoscroll with
5178 | None ->
5179 begin match state.mode with
5180 | Birdseye beye -> upbirdseye 1 beye
5181 | _ ->
5182 if ctrl
5183 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5184 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5186 | Some n ->
5187 setautoscrollspeed n false
5190 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5191 begin match state.autoscroll with
5192 | None ->
5193 begin match state.mode with
5194 | Birdseye beye -> downbirdseye 1 beye
5195 | _ ->
5196 if ctrl
5197 then gotoy_and_clear_text (clamp (conf.winh/2))
5198 else gotoy_and_clear_text (clamp conf.scrollstep)
5200 | Some n ->
5201 setautoscrollspeed n true
5204 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5205 if canpan ()
5206 then
5207 let dx =
5208 if ctrl
5209 then conf.winw / 2
5210 else conf.hscrollstep
5212 let dx = if key = 0xff51 then dx else -dx in
5213 state.x <- state.x + dx;
5214 gotoy_and_clear_text state.y
5215 else (
5216 state.text <- "";
5217 G.postRedisplay "lef/right"
5220 | 0xff55 | 0xff9a -> (* (kp) prior *)
5221 let y =
5222 if ctrl
5223 then
5224 match state.layout with
5225 | [] -> state.y
5226 | l :: _ -> state.y - l.pagey
5227 else
5228 clamp (pgscale (-conf.winh))
5230 gotoghyll y
5232 | 0xff56 | 0xff9b -> (* (kp) next *)
5233 let y =
5234 if ctrl
5235 then
5236 match List.rev state.layout with
5237 | [] -> state.y
5238 | l :: _ -> getpagey l.pageno
5239 else
5240 clamp (pgscale conf.winh)
5242 gotoghyll y
5244 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5245 gotoghyll 0
5246 | 71 | 0xff57 | 0xff9c -> (* G end *)
5247 gotoghyll (clamp state.maxy)
5249 | 0xff53 when Wsi.withalt mask -> (* alt-right *)
5250 gotoghyll (getnav 1)
5251 | 0xff51 when Wsi.withalt mask -> (* alt-left *)
5252 gotoghyll (getnav ~-1)
5254 | 114 -> (* r *)
5255 reload ()
5257 | 118 when conf.debug -> (* v *)
5258 state.rects <- [];
5259 List.iter (fun l ->
5260 match getopaque l.pageno with
5261 | None -> ()
5262 | Some opaque ->
5263 let x0, y0, x1, y1 = pagebbox opaque in
5264 let a,b = float x0, float y0 in
5265 let c,d = float x1, float y0 in
5266 let e,f = float x1, float y1 in
5267 let h,j = float x0, float y1 in
5268 let rect = (a,b,c,d,e,f,h,j) in
5269 debugrect rect;
5270 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5271 ) state.layout;
5272 G.postRedisplay "v";
5274 | _ ->
5275 vlog "huh? %s" (Wsi.keyname key)
5278 let linknavkeyboard key mask linknav =
5279 let getpage pageno =
5280 let rec loop = function
5281 | [] -> None
5282 | l :: _ when l.pageno = pageno -> Some l
5283 | _ :: rest -> loop rest
5284 in loop state.layout
5286 let doexact (pageno, n) =
5287 match getopaque pageno, getpage pageno with
5288 | Some opaque, Some l ->
5289 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5290 then
5291 let under = getlink opaque n in
5292 G.postRedisplay "link gotounder";
5293 gotounder under;
5294 state.mode <- View;
5295 else
5296 let opt, dir =
5297 match key with
5298 | 0xff50 -> (* home *)
5299 Some (findlink opaque LDfirst), -1
5301 | 0xff57 -> (* end *)
5302 Some (findlink opaque LDlast), 1
5304 | 0xff51 -> (* left *)
5305 Some (findlink opaque (LDleft n)), -1
5307 | 0xff53 -> (* right *)
5308 Some (findlink opaque (LDright n)), 1
5310 | 0xff52 -> (* up *)
5311 Some (findlink opaque (LDup n)), -1
5313 | 0xff54 -> (* down *)
5314 Some (findlink opaque (LDdown n)), 1
5316 | _ -> None, 0
5318 let pwl l dir =
5319 begin match findpwl l.pageno dir with
5320 | Pwlnotfound -> ()
5321 | Pwl pageno ->
5322 let notfound dir =
5323 state.mode <- LinkNav (Ltgendir dir);
5324 let y, h = getpageyh pageno in
5325 let y =
5326 if dir < 0
5327 then y + h - conf.winh
5328 else y
5330 gotoy y
5332 begin match getopaque pageno, getpage pageno with
5333 | Some opaque, Some _ ->
5334 let link =
5335 let ld = if dir > 0 then LDfirst else LDlast in
5336 findlink opaque ld
5338 begin match link with
5339 | Lfound m ->
5340 showlinktype (getlink opaque m);
5341 state.mode <- LinkNav (Ltexact (pageno, m));
5342 G.postRedisplay "linknav jpage";
5343 | _ -> notfound dir
5344 end;
5345 | _ -> notfound dir
5346 end;
5347 end;
5349 begin match opt with
5350 | Some Lnotfound -> pwl l dir;
5351 | Some (Lfound m) ->
5352 if m = n
5353 then pwl l dir
5354 else (
5355 let _, y0, _, y1 = getlinkrect opaque m in
5356 if y0 < l.pagey
5357 then gotopage1 l.pageno y0
5358 else (
5359 let d = fstate.fontsize + 1 in
5360 if y1 - l.pagey > l.pagevh - d
5361 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5362 else G.postRedisplay "linknav";
5364 showlinktype (getlink opaque m);
5365 state.mode <- LinkNav (Ltexact (l.pageno, m));
5368 | None -> viewkeyboard key mask
5369 end;
5370 | _ -> viewkeyboard key mask
5372 if key = 0xff63
5373 then (
5374 state.mode <- View;
5375 G.postRedisplay "leave linknav"
5377 else
5378 match linknav with
5379 | Ltgendir _ -> viewkeyboard key mask
5380 | Ltexact exact -> doexact exact
5383 let keyboard key mask =
5384 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5385 then wcmd "interrupt"
5386 else state.uioh <- state.uioh#key key mask
5389 let birdseyekeyboard key mask
5390 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5391 let incr =
5392 match conf.columns with
5393 | Csingle _ -> 1
5394 | Cmulti ((c, _, _), _) -> c
5395 | Csplit _ -> failwith "bird's eye split mode"
5397 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5398 match key with
5399 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5400 let y, h = getpageyh pageno in
5401 let top = (conf.winh - h) / 2 in
5402 gotoy (max 0 (y - top))
5403 | 0xff0d (* enter *)
5404 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5405 | 0xff1b -> leavebirdseye beye true (* escape *)
5406 | 0xff52 -> upbirdseye incr beye (* up *)
5407 | 0xff54 -> downbirdseye incr beye (* down *)
5408 | 0xff51 -> upbirdseye 1 beye (* left *)
5409 | 0xff53 -> downbirdseye 1 beye (* right *)
5411 | 0xff55 -> (* prior *)
5412 begin match state.layout with
5413 | l :: _ ->
5414 if l.pagey != 0
5415 then (
5416 state.mode <- Birdseye (
5417 oconf, leftx, l.pageno, hooverpageno, anchor
5419 gotopage1 l.pageno 0;
5421 else (
5422 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5423 match layout with
5424 | [] -> gotoy (clamp (-conf.winh))
5425 | l :: _ ->
5426 state.mode <- Birdseye (
5427 oconf, leftx, l.pageno, hooverpageno, anchor
5429 gotopage1 l.pageno 0
5432 | [] -> gotoy (clamp (-conf.winh))
5433 end;
5435 | 0xff56 -> (* next *)
5436 begin match List.rev state.layout with
5437 | l :: _ ->
5438 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5439 begin match layout with
5440 | [] ->
5441 let incr = l.pageh - l.pagevh in
5442 if incr = 0
5443 then (
5444 state.mode <-
5445 Birdseye (
5446 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5448 G.postRedisplay "birdseye pagedown";
5450 else gotoy (clamp (incr + conf.interpagespace*2));
5452 | l :: _ ->
5453 state.mode <-
5454 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5455 gotopage1 l.pageno 0;
5458 | [] -> gotoy (clamp conf.winh)
5459 end;
5461 | 0xff50 -> (* home *)
5462 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5463 gotopage1 0 0
5465 | 0xff57 -> (* end *)
5466 let pageno = state.pagecount - 1 in
5467 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5468 if not (pagevisible state.layout pageno)
5469 then
5470 let h =
5471 match List.rev state.pdims with
5472 | [] -> conf.winh
5473 | (_, _, h, _) :: _ -> h
5475 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5476 else G.postRedisplay "birdseye end";
5477 | _ -> viewkeyboard key mask
5480 let drawpage l linkindexbase =
5481 let color =
5482 match state.mode with
5483 | Textentry _ -> scalecolor 0.4
5484 | LinkNav _
5485 | View -> scalecolor 1.0
5486 | Birdseye (_, _, pageno, hooverpageno, _) ->
5487 if l.pageno = hooverpageno
5488 then scalecolor 0.9
5489 else (
5490 if l.pageno = pageno
5491 then scalecolor 1.0
5492 else scalecolor 0.8
5495 drawtiles l color;
5496 begin match getopaque l.pageno with
5497 | Some opaque ->
5498 if tileready l l.pagex l.pagey
5499 then
5500 let x = l.pagedispx - l.pagex
5501 and y = l.pagedispy - l.pagey in
5502 let hlmask =
5503 match conf.columns with
5504 | Csingle _ | Cmulti _ ->
5505 (if conf.hlinks then 1 else 0)
5506 + (if state.glinks
5507 && not (isbirdseye state.mode) then 2 else 0)
5508 | _ -> 0
5510 let s =
5511 match state.mode with
5512 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5513 | _ -> ""
5515 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5516 else 0
5518 | _ -> 0
5519 end;
5522 let scrollindicator () =
5523 let sbw, ph, sh = state.uioh#scrollph in
5524 let sbh, pw, sw = state.uioh#scrollpw in
5526 GlDraw.color (0.64, 0.64, 0.64);
5527 GlDraw.rect
5528 (float (conf.winw - sbw), 0.)
5529 (float conf.winw, float conf.winh)
5531 GlDraw.rect
5532 (0., float (conf.winh - sbh))
5533 (float (conf.winw - state.scrollw - 1), float conf.winh)
5535 GlDraw.color (0.0, 0.0, 0.0);
5537 GlDraw.rect
5538 (float (conf.winw - sbw), ph)
5539 (float conf.winw, ph +. sh)
5541 GlDraw.rect
5542 (pw, float (conf.winh - sbh))
5543 (pw +. sw, float conf.winh)
5547 let showsel () =
5548 match state.mstate with
5549 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5552 | Msel ((x0, y0), (x1, y1)) ->
5553 let rec loop = function
5554 | l :: ls ->
5555 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5556 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5557 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5558 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5559 then
5560 match getopaque l.pageno with
5561 | Some opaque ->
5562 let x0, y0 = pagetranslatepoint l x0 y0 in
5563 let x1, y1 = pagetranslatepoint l x1 y1 in
5564 seltext opaque (x0, y0, x1, y1);
5565 | _ -> ()
5566 else loop ls
5567 | [] -> ()
5569 loop state.layout
5572 let showrects rects =
5573 Gl.enable `blend;
5574 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5575 GlDraw.polygon_mode `both `fill;
5576 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5577 List.iter
5578 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5579 List.iter (fun l ->
5580 if l.pageno = pageno
5581 then (
5582 let dx = float (l.pagedispx - l.pagex) in
5583 let dy = float (l.pagedispy - l.pagey) in
5584 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5585 GlDraw.begins `quads;
5587 GlDraw.vertex2 (x0+.dx, y0+.dy);
5588 GlDraw.vertex2 (x1+.dx, y1+.dy);
5589 GlDraw.vertex2 (x2+.dx, y2+.dy);
5590 GlDraw.vertex2 (x3+.dx, y3+.dy);
5592 GlDraw.ends ();
5594 ) state.layout
5595 ) rects
5597 Gl.disable `blend;
5600 let display () =
5601 GlClear.color (scalecolor2 conf.bgcolor);
5602 GlClear.clear [`color];
5603 let rec loop linkindexbase = function
5604 | l :: rest ->
5605 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5606 loop linkindexbase rest
5607 | [] -> ()
5609 loop 0 state.layout;
5610 let rects =
5611 match state.mode with
5612 | LinkNav (Ltexact (pageno, linkno)) ->
5613 begin match getopaque pageno with
5614 | Some opaque ->
5615 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5616 (pageno, 5, (
5617 float x0, float y0,
5618 float x1, float y0,
5619 float x1, float y1,
5620 float x0, float y1)
5621 ) :: state.rects
5622 | None -> state.rects
5624 | _ -> state.rects
5626 showrects rects;
5627 showsel ();
5628 state.uioh#display;
5629 begin match state.mstate with
5630 | Mzoomrect ((x0, y0), (x1, y1)) ->
5631 Gl.enable `blend;
5632 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5633 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5634 GlDraw.rect (float x0, float y0)
5635 (float x1, float y1);
5636 Gl.disable `blend;
5637 | _ -> ()
5638 end;
5639 enttext ();
5640 scrollindicator ();
5641 if not state.wthack then Wsi.swapb ();
5644 let zoomrect x y x1 y1 =
5645 let x0 = min x x1
5646 and x1 = max x x1
5647 and y0 = min y y1 in
5648 gotoy (state.y + y0);
5649 state.anchor <- getanchor ();
5650 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5651 let margin =
5652 if state.w < conf.winw - state.scrollw
5653 then (conf.winw - state.scrollw - state.w) / 2
5654 else 0
5656 state.x <- (state.x + margin) - x0;
5657 setzoom zoom;
5658 Wsi.setcursor Wsi.CURSOR_INHERIT;
5659 state.mstate <- Mnone;
5662 let scrollx x =
5663 let winw = conf.winw - state.scrollw - 1 in
5664 let s = float x /. float winw in
5665 let destx = truncate (float (state.w + winw) *. s) in
5666 state.x <- winw - destx;
5667 gotoy_and_clear_text state.y;
5668 state.mstate <- Mscrollx;
5671 let scrolly y =
5672 let s = float y /. float conf.winh in
5673 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5674 gotoy_and_clear_text desty;
5675 state.mstate <- Mscrolly;
5678 let viewmouse button down x y mask =
5679 match button with
5680 | n when (n == 4 || n == 5) && not down ->
5681 if Wsi.withctrl mask
5682 then (
5683 match state.mstate with
5684 | Mzoom (oldn, i) ->
5685 if oldn = n
5686 then (
5687 if i = 2
5688 then
5689 let incr =
5690 match n with
5691 | 5 ->
5692 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5693 | _ ->
5694 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5696 let zoom = conf.zoom -. incr in
5697 setzoom zoom;
5698 state.mstate <- Mzoom (n, 0);
5699 else
5700 state.mstate <- Mzoom (n, i+1);
5702 else state.mstate <- Mzoom (n, 0)
5704 | _ -> state.mstate <- Mzoom (n, 0)
5706 else (
5707 match state.autoscroll with
5708 | Some step -> setautoscrollspeed step (n=4)
5709 | None ->
5710 let incr =
5711 if n = 4
5712 then -conf.scrollstep
5713 else conf.scrollstep
5715 let incr = incr * 2 in
5716 let y = clamp incr in
5717 gotoy_and_clear_text y
5720 | n when (n = 6 || n = 7) && not down && canpan () ->
5721 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5722 gotoy_and_clear_text state.y
5724 | 1 when Wsi.withctrl mask ->
5725 if down
5726 then (
5727 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5728 state.mstate <- Mpan (x, y)
5730 else
5731 state.mstate <- Mnone
5733 | 3 ->
5734 if down
5735 then (
5736 Wsi.setcursor Wsi.CURSOR_CYCLE;
5737 let p = (x, y) in
5738 state.mstate <- Mzoomrect (p, p)
5740 else (
5741 match state.mstate with
5742 | Mzoomrect ((x0, y0), _) ->
5743 if abs (x-x0) > 10 && abs (y - y0) > 10
5744 then zoomrect x0 y0 x y
5745 else (
5746 state.mstate <- Mnone;
5747 Wsi.setcursor Wsi.CURSOR_INHERIT;
5748 G.postRedisplay "kill accidental zoom rect";
5750 | _ ->
5751 Wsi.setcursor Wsi.CURSOR_INHERIT;
5752 state.mstate <- Mnone
5755 | 1 when x > conf.winw - state.scrollw ->
5756 if down
5757 then
5758 let _, position, sh = state.uioh#scrollph in
5759 if y > truncate position && y < truncate (position +. sh)
5760 then state.mstate <- Mscrolly
5761 else scrolly y
5762 else
5763 state.mstate <- Mnone
5765 | 1 when y > conf.winh - state.hscrollh ->
5766 if down
5767 then
5768 let _, position, sw = state.uioh#scrollpw in
5769 if x > truncate position && x < truncate (position +. sw)
5770 then state.mstate <- Mscrollx
5771 else scrollx x
5772 else
5773 state.mstate <- Mnone
5775 | 1 ->
5776 let dest = if down then getunder x y else Unone in
5777 begin match dest with
5778 | Ulinkgoto _
5779 | Ulinkuri _
5780 | Uremote _
5781 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5782 gotounder dest
5784 | Unone when down ->
5785 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5786 state.mstate <- Mpan (x, y);
5788 | Unone | Utext _ ->
5789 if down
5790 then (
5791 if conf.angle mod 360 = 0
5792 then (
5793 state.mstate <- Msel ((x, y), (x, y));
5794 G.postRedisplay "mouse select";
5797 else (
5798 match state.mstate with
5799 | Mnone -> ()
5801 | Mzoom _ | Mscrollx | Mscrolly ->
5802 state.mstate <- Mnone
5804 | Mzoomrect ((x0, y0), _) ->
5805 zoomrect x0 y0 x y
5807 | Mpan _ ->
5808 Wsi.setcursor Wsi.CURSOR_INHERIT;
5809 state.mstate <- Mnone
5811 | Msel ((x0, y0), (x1, y1)) ->
5812 let rec loop = function
5813 | [] -> ()
5814 | l :: rest ->
5815 let inside =
5816 let a0 = l.pagedispy in
5817 let a1 = a0 + l.pagevh in
5818 let b0 = l.pagedispx in
5819 let b1 = b0 + l.pagevw in
5820 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5821 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5823 if inside
5824 then
5825 match getopaque l.pageno with
5826 | Some opaque ->
5827 begin
5828 match Ne.pipe () with
5829 | Ne.Exn exn ->
5830 showtext '!'
5831 (Printf.sprintf
5832 "can not create sel pipe: %s"
5833 (Printexc.to_string exn));
5834 | Ne.Res (r, w) ->
5835 let doclose what fd =
5836 Ne.clo fd (fun msg ->
5837 dolog "%s close failed: %s" what msg)
5840 popen conf.selcmd [r, 0; w, -1];
5841 copysel w opaque;
5842 doclose "pipe/r" r;
5843 G.postRedisplay "copysel";
5844 with exn ->
5845 dolog "can not execute %S: %s"
5846 conf.selcmd (Printexc.to_string exn);
5847 doclose "pipe/r" r;
5848 doclose "pipe/w" w;
5850 | None -> ()
5851 else loop rest
5853 loop state.layout;
5854 Wsi.setcursor Wsi.CURSOR_INHERIT;
5855 state.mstate <- Mnone;
5859 | _ -> ()
5862 let birdseyemouse button down x y mask
5863 (conf, leftx, _, hooverpageno, anchor) =
5864 match button with
5865 | 1 when down ->
5866 let rec loop = function
5867 | [] -> ()
5868 | l :: rest ->
5869 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5870 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5871 then (
5872 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5874 else loop rest
5876 loop state.layout
5877 | 3 -> ()
5878 | _ -> viewmouse button down x y mask
5881 let mouse button down x y mask =
5882 state.uioh <- state.uioh#button button down x y mask;
5885 let motion ~x ~y =
5886 state.uioh <- state.uioh#motion x y
5889 let pmotion ~x ~y =
5890 state.uioh <- state.uioh#pmotion x y;
5893 let uioh = object
5894 method display = ()
5896 method key key mask =
5897 begin match state.mode with
5898 | Textentry textentry -> textentrykeyboard key mask textentry
5899 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5900 | View -> viewkeyboard key mask
5901 | LinkNav linknav -> linknavkeyboard key mask linknav
5902 end;
5903 state.uioh
5905 method button button bstate x y mask =
5906 begin match state.mode with
5907 | LinkNav _
5908 | View -> viewmouse button bstate x y mask
5909 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5910 | Textentry _ -> ()
5911 end;
5912 state.uioh
5914 method motion x y =
5915 begin match state.mode with
5916 | Textentry _ -> ()
5917 | View | Birdseye _ | LinkNav _ ->
5918 match state.mstate with
5919 | Mzoom _ | Mnone -> ()
5921 | Mpan (x0, y0) ->
5922 let dx = x - x0
5923 and dy = y0 - y in
5924 state.mstate <- Mpan (x, y);
5925 if canpan ()
5926 then state.x <- state.x + dx;
5927 let y = clamp dy in
5928 gotoy_and_clear_text y
5930 | Msel (a, _) ->
5931 state.mstate <- Msel (a, (x, y));
5932 G.postRedisplay "motion select";
5934 | Mscrolly ->
5935 let y = min conf.winh (max 0 y) in
5936 scrolly y
5938 | Mscrollx ->
5939 let x = min conf.winw (max 0 x) in
5940 scrollx x
5942 | Mzoomrect (p0, _) ->
5943 state.mstate <- Mzoomrect (p0, (x, y));
5944 G.postRedisplay "motion zoomrect";
5945 end;
5946 state.uioh
5948 method pmotion x y =
5949 begin match state.mode with
5950 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5951 let rec loop = function
5952 | [] ->
5953 if hooverpageno != -1
5954 then (
5955 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5956 G.postRedisplay "pmotion birdseye no hoover";
5958 | l :: rest ->
5959 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5960 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5961 then (
5962 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5963 G.postRedisplay "pmotion birdseye hoover";
5965 else loop rest
5967 loop state.layout
5969 | Textentry _ -> ()
5971 | LinkNav _
5972 | View ->
5973 match state.mstate with
5974 | Mnone -> updateunder x y
5975 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5977 end;
5978 state.uioh
5980 method infochanged _ = ()
5982 method scrollph =
5983 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5984 let p, h = scrollph state.y maxy in
5985 state.scrollw, p, h
5987 method scrollpw =
5988 let winw = conf.winw - state.scrollw - 1 in
5989 let fwinw = float winw in
5990 let sw =
5991 let sw = fwinw /. float state.w in
5992 let sw = fwinw *. sw in
5993 max sw (float conf.scrollh)
5995 let position, sw =
5996 let f = state.w+winw in
5997 let r = float (winw-state.x) /. float f in
5998 let p = fwinw *. r in
5999 p-.sw/.2., sw
6001 let sw =
6002 if position +. sw > fwinw
6003 then fwinw -. position
6004 else sw
6006 state.hscrollh, position, sw
6008 method modehash =
6009 let modename =
6010 match state.mode with
6011 | LinkNav _ -> "links"
6012 | Textentry _ -> "textentry"
6013 | Birdseye _ -> "birdseye"
6014 | View -> "view"
6016 findkeyhash conf modename
6017 end;;
6019 module Config =
6020 struct
6021 open Parser
6023 let fontpath = ref "";;
6025 module KeyMap =
6026 Map.Make (struct type t = (int * int) let compare = compare end);;
6028 let unent s =
6029 let l = String.length s in
6030 let b = Buffer.create l in
6031 unent b s 0 l;
6032 Buffer.contents b;
6035 let home =
6036 try Sys.getenv "HOME"
6037 with exn ->
6038 prerr_endline
6039 ("Can not determine home directory location: " ^
6040 Printexc.to_string exn);
6044 let modifier_of_string = function
6045 | "alt" -> Wsi.altmask
6046 | "shift" -> Wsi.shiftmask
6047 | "ctrl" | "control" -> Wsi.ctrlmask
6048 | "meta" -> Wsi.metamask
6049 | _ -> 0
6052 let key_of_string =
6053 let r = Str.regexp "-" in
6054 fun s ->
6055 let elems = Str.full_split r s in
6056 let f n k m =
6057 let g s =
6058 let m1 = modifier_of_string s in
6059 if m1 = 0
6060 then (Wsi.namekey s, m)
6061 else (k, m lor m1)
6062 in function
6063 | Str.Delim s when n land 1 = 0 -> g s
6064 | Str.Text s -> g s
6065 | Str.Delim _ -> (k, m)
6067 let rec loop n k m = function
6068 | [] -> (k, m)
6069 | x :: xs ->
6070 let k, m = f n k m x in
6071 loop (n+1) k m xs
6073 loop 0 0 0 elems
6076 let keys_of_string =
6077 let r = Str.regexp "[ \t]" in
6078 fun s ->
6079 let elems = Str.split r s in
6080 List.map key_of_string elems
6083 let copykeyhashes c =
6084 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6087 let config_of c attrs =
6088 let apply c k v =
6090 match k with
6091 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6092 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6093 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6094 | "preload" -> { c with preload = bool_of_string v }
6095 | "page-bias" -> { c with pagebias = int_of_string v }
6096 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6097 | "horizontal-scroll-step" ->
6098 { c with hscrollstep = max (int_of_string v) 1 }
6099 | "auto-scroll-step" ->
6100 { c with autoscrollstep = max 0 (int_of_string v) }
6101 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6102 | "crop-hack" -> { c with crophack = bool_of_string v }
6103 | "throttle" ->
6104 let mw =
6105 match String.lowercase v with
6106 | "true" -> Some infinity
6107 | "false" -> None
6108 | f -> Some (float_of_string f)
6110 { c with maxwait = mw}
6111 | "highlight-links" -> { c with hlinks = bool_of_string v }
6112 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6113 | "vertical-margin" ->
6114 { c with interpagespace = max 0 (int_of_string v) }
6115 | "zoom" ->
6116 let zoom = float_of_string v /. 100. in
6117 let zoom = max zoom 0.0 in
6118 { c with zoom = zoom }
6119 | "presentation" -> { c with presentation = bool_of_string v }
6120 | "rotation-angle" -> { c with angle = int_of_string v }
6121 | "width" -> { c with winw = max 20 (int_of_string v) }
6122 | "height" -> { c with winh = max 20 (int_of_string v) }
6123 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6124 | "proportional-display" -> { c with proportional = bool_of_string v }
6125 | "pixmap-cache-size" ->
6126 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6127 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6128 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6129 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6130 | "persistent-location" -> { c with jumpback = bool_of_string v }
6131 | "background-color" -> { c with bgcolor = color_of_string v }
6132 | "scrollbar-in-presentation" ->
6133 { c with scrollbarinpm = bool_of_string v }
6134 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6135 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6136 | "mupdf-store-size" ->
6137 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6138 | "checkers" -> { c with checkers = bool_of_string v }
6139 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6140 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6141 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6142 | "uri-launcher" -> { c with urilauncher = unent v }
6143 | "path-launcher" -> { c with pathlauncher = unent v }
6144 | "color-space" -> { c with colorspace = colorspace_of_string v }
6145 | "invert-colors" -> { c with invert = bool_of_string v }
6146 | "brightness" -> { c with colorscale = float_of_string v }
6147 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6148 | "ghyllscroll" ->
6149 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6150 | "columns" ->
6151 let (n, _, _) as nab = multicolumns_of_string v in
6152 if n < 0
6153 then { c with columns = Csplit (-n, [||]) }
6154 else { c with columns = Cmulti (nab, [||]) }
6155 | "birds-eye-columns" ->
6156 { c with beyecolumns = Some (max (int_of_string v) 2) }
6157 | "selection-command" -> { c with selcmd = unent v }
6158 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6159 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6160 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6161 | "use-pbo" -> { c with usepbo = bool_of_string v }
6162 | _ -> c
6163 with exn ->
6164 prerr_endline ("Error processing attribute (`" ^
6165 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6168 let rec fold c = function
6169 | [] -> c
6170 | (k, v) :: rest ->
6171 let c = apply c k v in
6172 fold c rest
6174 fold { c with keyhashes = copykeyhashes c } attrs;
6177 let fromstring f pos n v d =
6178 try f v
6179 with exn ->
6180 dolog "Error processing attribute (%S=%S) at %d\n%s"
6181 n v pos (Printexc.to_string exn)
6186 let bookmark_of attrs =
6187 let rec fold title page rely visy = function
6188 | ("title", v) :: rest -> fold v page rely visy rest
6189 | ("page", v) :: rest -> fold title v rely visy rest
6190 | ("rely", v) :: rest -> fold title page v visy rest
6191 | ("visy", v) :: rest -> fold title page rely v rest
6192 | _ :: rest -> fold title page rely visy rest
6193 | [] -> title, page, rely, visy
6195 fold "invalid" "0" "0" "0" attrs
6198 let doc_of attrs =
6199 let rec fold path page rely pan visy = function
6200 | ("path", v) :: rest -> fold v page rely pan visy rest
6201 | ("page", v) :: rest -> fold path v rely pan visy rest
6202 | ("rely", v) :: rest -> fold path page v pan visy rest
6203 | ("pan", v) :: rest -> fold path page rely v visy rest
6204 | ("visy", v) :: rest -> fold path page rely pan v rest
6205 | _ :: rest -> fold path page rely pan visy rest
6206 | [] -> path, page, rely, pan, visy
6208 fold "" "0" "0" "0" "0" attrs
6211 let map_of attrs =
6212 let rec fold rs ls = function
6213 | ("out", v) :: rest -> fold v ls rest
6214 | ("in", v) :: rest -> fold rs v rest
6215 | _ :: rest -> fold ls rs rest
6216 | [] -> ls, rs
6218 fold "" "" attrs
6221 let setconf dst src =
6222 dst.scrollbw <- src.scrollbw;
6223 dst.scrollh <- src.scrollh;
6224 dst.icase <- src.icase;
6225 dst.preload <- src.preload;
6226 dst.pagebias <- src.pagebias;
6227 dst.verbose <- src.verbose;
6228 dst.scrollstep <- src.scrollstep;
6229 dst.maxhfit <- src.maxhfit;
6230 dst.crophack <- src.crophack;
6231 dst.autoscrollstep <- src.autoscrollstep;
6232 dst.maxwait <- src.maxwait;
6233 dst.hlinks <- src.hlinks;
6234 dst.underinfo <- src.underinfo;
6235 dst.interpagespace <- src.interpagespace;
6236 dst.zoom <- src.zoom;
6237 dst.presentation <- src.presentation;
6238 dst.angle <- src.angle;
6239 dst.winw <- src.winw;
6240 dst.winh <- src.winh;
6241 dst.savebmarks <- src.savebmarks;
6242 dst.memlimit <- src.memlimit;
6243 dst.proportional <- src.proportional;
6244 dst.texcount <- src.texcount;
6245 dst.sliceheight <- src.sliceheight;
6246 dst.thumbw <- src.thumbw;
6247 dst.jumpback <- src.jumpback;
6248 dst.bgcolor <- src.bgcolor;
6249 dst.scrollbarinpm <- src.scrollbarinpm;
6250 dst.tilew <- src.tilew;
6251 dst.tileh <- src.tileh;
6252 dst.mustoresize <- src.mustoresize;
6253 dst.checkers <- src.checkers;
6254 dst.aalevel <- src.aalevel;
6255 dst.trimmargins <- src.trimmargins;
6256 dst.trimfuzz <- src.trimfuzz;
6257 dst.urilauncher <- src.urilauncher;
6258 dst.colorspace <- src.colorspace;
6259 dst.invert <- src.invert;
6260 dst.colorscale <- src.colorscale;
6261 dst.redirectstderr <- src.redirectstderr;
6262 dst.ghyllscroll <- src.ghyllscroll;
6263 dst.columns <- src.columns;
6264 dst.beyecolumns <- src.beyecolumns;
6265 dst.selcmd <- src.selcmd;
6266 dst.updatecurs <- src.updatecurs;
6267 dst.pathlauncher <- src.pathlauncher;
6268 dst.keyhashes <- copykeyhashes src;
6269 dst.hfsize <- src.hfsize;
6270 dst.hscrollstep <- src.hscrollstep;
6271 dst.pgscale <- src.pgscale;
6272 dst.usepbo <- src.usepbo;
6275 let get s =
6276 let h = Hashtbl.create 10 in
6277 let dc = { defconf with angle = defconf.angle } in
6278 let rec toplevel v t spos _ =
6279 match t with
6280 | Vdata | Vcdata | Vend -> v
6281 | Vopen ("llppconfig", _, closed) ->
6282 if closed
6283 then v
6284 else { v with f = llppconfig }
6285 | Vopen _ ->
6286 error "unexpected subelement at top level" s spos
6287 | Vclose _ -> error "unexpected close at top level" s spos
6289 and llppconfig v t spos _ =
6290 match t with
6291 | Vdata | Vcdata -> v
6292 | Vend -> error "unexpected end of input in llppconfig" s spos
6293 | Vopen ("defaults", attrs, closed) ->
6294 let c = config_of dc attrs in
6295 setconf dc c;
6296 if closed
6297 then v
6298 else { v with f = defaults }
6300 | Vopen ("ui-font", attrs, closed) ->
6301 let rec getsize size = function
6302 | [] -> size
6303 | ("size", v) :: rest ->
6304 let size =
6305 fromstring int_of_string spos "size" v fstate.fontsize in
6306 getsize size rest
6307 | l -> getsize size l
6309 fstate.fontsize <- getsize fstate.fontsize attrs;
6310 if closed
6311 then v
6312 else { v with f = uifont (Buffer.create 10) }
6314 | Vopen ("doc", attrs, closed) ->
6315 let pathent, spage, srely, span, svisy = doc_of attrs in
6316 let path = unent pathent
6317 and pageno = fromstring int_of_string spos "page" spage 0
6318 and rely = fromstring float_of_string spos "rely" srely 0.0
6319 and pan = fromstring int_of_string spos "pan" span 0
6320 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6321 let c = config_of dc attrs in
6322 let anchor = (pageno, rely, visy) in
6323 if closed
6324 then (Hashtbl.add h path (c, [], pan, anchor); v)
6325 else { v with f = doc path pan anchor c [] }
6327 | Vopen _ ->
6328 error "unexpected subelement in llppconfig" s spos
6330 | Vclose "llppconfig" -> { v with f = toplevel }
6331 | Vclose _ -> error "unexpected close in llppconfig" s spos
6333 and defaults v t spos _ =
6334 match t with
6335 | Vdata | Vcdata -> v
6336 | Vend -> error "unexpected end of input in defaults" s spos
6337 | Vopen ("keymap", attrs, closed) ->
6338 let modename =
6339 try List.assoc "mode" attrs
6340 with Not_found -> "global" in
6341 if closed
6342 then v
6343 else
6344 let ret keymap =
6345 let h = findkeyhash dc modename in
6346 KeyMap.iter (Hashtbl.replace h) keymap;
6347 defaults
6349 { v with f = pkeymap ret KeyMap.empty }
6351 | Vopen (_, _, _) ->
6352 error "unexpected subelement in defaults" s spos
6354 | Vclose "defaults" ->
6355 { v with f = llppconfig }
6357 | Vclose _ -> error "unexpected close in defaults" s spos
6359 and uifont b v t spos epos =
6360 match t with
6361 | Vdata | Vcdata ->
6362 Buffer.add_substring b s spos (epos - spos);
6364 | Vopen (_, _, _) ->
6365 error "unexpected subelement in ui-font" s spos
6366 | Vclose "ui-font" ->
6367 if String.length !fontpath = 0
6368 then fontpath := Buffer.contents b;
6369 { v with f = llppconfig }
6370 | Vclose _ -> error "unexpected close in ui-font" s spos
6371 | Vend -> error "unexpected end of input in ui-font" s spos
6373 and doc path pan anchor c bookmarks v t spos _ =
6374 match t with
6375 | Vdata | Vcdata -> v
6376 | Vend -> error "unexpected end of input in doc" s spos
6377 | Vopen ("bookmarks", _, closed) ->
6378 if closed
6379 then v
6380 else { v with f = pbookmarks path pan anchor c bookmarks }
6382 | Vopen ("keymap", attrs, closed) ->
6383 let modename =
6384 try List.assoc "mode" attrs
6385 with Not_found -> "global"
6387 if closed
6388 then v
6389 else
6390 let ret keymap =
6391 let h = findkeyhash c modename in
6392 KeyMap.iter (Hashtbl.replace h) keymap;
6393 doc path pan anchor c bookmarks
6395 { v with f = pkeymap ret KeyMap.empty }
6397 | Vopen (_, _, _) ->
6398 error "unexpected subelement in doc" s spos
6400 | Vclose "doc" ->
6401 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6402 { v with f = llppconfig }
6404 | Vclose _ -> error "unexpected close in doc" s spos
6406 and pkeymap ret keymap v t spos _ =
6407 match t with
6408 | Vdata | Vcdata -> v
6409 | Vend -> error "unexpected end of input in keymap" s spos
6410 | Vopen ("map", attrs, closed) ->
6411 let r, l = map_of attrs in
6412 let kss = fromstring keys_of_string spos "in" r [] in
6413 let lss = fromstring keys_of_string spos "out" l [] in
6414 let keymap =
6415 match kss with
6416 | [] -> keymap
6417 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6418 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6420 if closed
6421 then { v with f = pkeymap ret keymap }
6422 else
6423 let f () = v in
6424 { v with f = skip "map" f }
6426 | Vopen _ ->
6427 error "unexpected subelement in keymap" s spos
6429 | Vclose "keymap" ->
6430 { v with f = ret keymap }
6432 | Vclose _ -> error "unexpected close in keymap" s spos
6434 and pbookmarks path pan anchor c bookmarks v t spos _ =
6435 match t with
6436 | Vdata | Vcdata -> v
6437 | Vend -> error "unexpected end of input in bookmarks" s spos
6438 | Vopen ("item", attrs, closed) ->
6439 let titleent, spage, srely, svisy = bookmark_of attrs in
6440 let page = fromstring int_of_string spos "page" spage 0
6441 and rely = fromstring float_of_string spos "rely" srely 0.0
6442 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6443 let bookmarks =
6444 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6446 if closed
6447 then { v with f = pbookmarks path pan anchor c bookmarks }
6448 else
6449 let f () = v in
6450 { v with f = skip "item" f }
6452 | Vopen _ ->
6453 error "unexpected subelement in bookmarks" s spos
6455 | Vclose "bookmarks" ->
6456 { v with f = doc path pan anchor c bookmarks }
6458 | Vclose _ -> error "unexpected close in bookmarks" s spos
6460 and skip tag f v t spos _ =
6461 match t with
6462 | Vdata | Vcdata -> v
6463 | Vend ->
6464 error ("unexpected end of input in skipped " ^ tag) s spos
6465 | Vopen (tag', _, closed) ->
6466 if closed
6467 then v
6468 else
6469 let f' () = { v with f = skip tag f } in
6470 { v with f = skip tag' f' }
6471 | Vclose ctag ->
6472 if tag = ctag
6473 then f ()
6474 else error ("unexpected close in skipped " ^ tag) s spos
6477 parse { f = toplevel; accu = () } s;
6478 h, dc;
6481 let do_load f ic =
6483 let len = in_channel_length ic in
6484 let s = String.create len in
6485 really_input ic s 0 len;
6486 f s;
6487 with
6488 | Parse_error (msg, s, pos) ->
6489 let subs = subs s pos in
6490 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6491 failwith ("parse error: " ^ s)
6493 | exn ->
6494 failwith ("config load error: " ^ Printexc.to_string exn)
6497 let defconfpath =
6498 let dir =
6500 let dir = Filename.concat home ".config" in
6501 if Sys.is_directory dir then dir else home
6502 with _ -> home
6504 Filename.concat dir "llpp.conf"
6507 let confpath = ref defconfpath;;
6509 let load1 f =
6510 if Sys.file_exists !confpath
6511 then
6512 match
6513 (try Some (open_in_bin !confpath)
6514 with exn ->
6515 prerr_endline
6516 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6517 Printexc.to_string exn);
6518 None
6520 with
6521 | Some ic ->
6522 let success =
6524 f (do_load get ic)
6525 with exn ->
6526 prerr_endline
6527 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6528 Printexc.to_string exn);
6529 false
6531 close_in ic;
6532 success
6534 | None -> false
6535 else
6536 f (Hashtbl.create 0, defconf)
6539 let load () =
6540 let f (h, dc) =
6541 let pc, pb, px, pa =
6543 Hashtbl.find h (Filename.basename state.path)
6544 with Not_found -> dc, [], 0, emptyanchor
6546 setconf defconf dc;
6547 setconf conf pc;
6548 state.bookmarks <- pb;
6549 state.x <- px;
6550 state.scrollw <- conf.scrollbw;
6551 if conf.jumpback
6552 then state.anchor <- pa;
6553 cbput state.hists.nav pa;
6554 true
6556 load1 f
6559 let add_attrs bb always dc c =
6560 let ob s a b =
6561 if always || a != b
6562 then Printf.bprintf bb "\n %s='%b'" s a
6563 and oi s a b =
6564 if always || a != b
6565 then Printf.bprintf bb "\n %s='%d'" s a
6566 and oI s a b =
6567 if always || a != b
6568 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6569 and oz s a b =
6570 if always || a <> b
6571 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6572 and oF s a b =
6573 if always || a <> b
6574 then Printf.bprintf bb "\n %s='%f'" s a
6575 and oc s a b =
6576 if always || a <> b
6577 then
6578 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6579 and oC s a b =
6580 if always || a <> b
6581 then
6582 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6583 and oR s a b =
6584 if always || a <> b
6585 then
6586 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6587 and os s a b =
6588 if always || a <> b
6589 then
6590 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6591 and og s a b =
6592 if always || a <> b
6593 then
6594 match a with
6595 | None -> ()
6596 | Some (_N, _A, _B) ->
6597 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6598 and oW s a b =
6599 if always || a <> b
6600 then
6601 let v =
6602 match a with
6603 | None -> "false"
6604 | Some f ->
6605 if f = infinity
6606 then "true"
6607 else string_of_float f
6609 Printf.bprintf bb "\n %s='%s'" s v
6610 and oco s a b =
6611 if always || a <> b
6612 then
6613 match a with
6614 | Cmulti ((n, a, b), _) when n > 1 ->
6615 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6616 | Csplit (n, _) when n > 1 ->
6617 Printf.bprintf bb "\n %s='%d'" s ~-n
6618 | _ -> ()
6619 and obeco s a b =
6620 if always || a <> b
6621 then
6622 match a with
6623 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6624 | _ -> ()
6626 let w, h =
6627 if always
6628 then dc.winw, dc.winh
6629 else
6630 match state.fullscreen with
6631 | Some wh -> wh
6632 | None -> c.winw, c.winh
6634 oi "width" w dc.winw;
6635 oi "height" h dc.winh;
6636 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6637 oi "scroll-handle-height" c.scrollh dc.scrollh;
6638 ob "case-insensitive-search" c.icase dc.icase;
6639 ob "preload" c.preload dc.preload;
6640 oi "page-bias" c.pagebias dc.pagebias;
6641 oi "scroll-step" c.scrollstep dc.scrollstep;
6642 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6643 ob "max-height-fit" c.maxhfit dc.maxhfit;
6644 ob "crop-hack" c.crophack dc.crophack;
6645 oW "throttle" c.maxwait dc.maxwait;
6646 ob "highlight-links" c.hlinks dc.hlinks;
6647 ob "under-cursor-info" c.underinfo dc.underinfo;
6648 oi "vertical-margin" c.interpagespace dc.interpagespace;
6649 oz "zoom" c.zoom dc.zoom;
6650 ob "presentation" c.presentation dc.presentation;
6651 oi "rotation-angle" c.angle dc.angle;
6652 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6653 ob "proportional-display" c.proportional dc.proportional;
6654 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6655 oi "tex-count" c.texcount dc.texcount;
6656 oi "slice-height" c.sliceheight dc.sliceheight;
6657 oi "thumbnail-width" c.thumbw dc.thumbw;
6658 ob "persistent-location" c.jumpback dc.jumpback;
6659 oc "background-color" c.bgcolor dc.bgcolor;
6660 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6661 oi "tile-width" c.tilew dc.tilew;
6662 oi "tile-height" c.tileh dc.tileh;
6663 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6664 ob "checkers" c.checkers dc.checkers;
6665 oi "aalevel" c.aalevel dc.aalevel;
6666 ob "trim-margins" c.trimmargins dc.trimmargins;
6667 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6668 os "uri-launcher" c.urilauncher dc.urilauncher;
6669 os "path-launcher" c.pathlauncher dc.pathlauncher;
6670 oC "color-space" c.colorspace dc.colorspace;
6671 ob "invert-colors" c.invert dc.invert;
6672 oF "brightness" c.colorscale dc.colorscale;
6673 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6674 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6675 oco "columns" c.columns dc.columns;
6676 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6677 os "selection-command" c.selcmd dc.selcmd;
6678 ob "update-cursor" c.updatecurs dc.updatecurs;
6679 oi "hint-font-size" c.hfsize dc.hfsize;
6680 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6681 oF "page-scroll-scale" c.pgscale dc.pgscale;
6682 ob "use-pbo" c.usepbo dc.usepbo;
6685 let keymapsbuf always dc c =
6686 let bb = Buffer.create 16 in
6687 let rec loop = function
6688 | [] -> ()
6689 | (modename, h) :: rest ->
6690 let dh = findkeyhash dc modename in
6691 if always || h <> dh
6692 then (
6693 if Hashtbl.length h > 0
6694 then (
6695 if Buffer.length bb > 0
6696 then Buffer.add_char bb '\n';
6697 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6698 Hashtbl.iter (fun i o ->
6699 let isdifferent = always ||
6701 let dO = Hashtbl.find dh i in
6702 dO <> o
6703 with Not_found -> true
6705 if isdifferent
6706 then
6707 let addkm (k, m) =
6708 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6709 if Wsi.withalt m then Buffer.add_string bb "alt-";
6710 if Wsi.withshift m then Buffer.add_string bb "shift-";
6711 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6712 Buffer.add_string bb (Wsi.keyname k);
6714 let addkms l =
6715 let rec loop = function
6716 | [] -> ()
6717 | km :: [] -> addkm km
6718 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6720 loop l
6722 Buffer.add_string bb "<map in='";
6723 addkm i;
6724 match o with
6725 | KMinsrt km ->
6726 Buffer.add_string bb "' out='";
6727 addkm km;
6728 Buffer.add_string bb "'/>\n"
6730 | KMinsrl kms ->
6731 Buffer.add_string bb "' out='";
6732 addkms kms;
6733 Buffer.add_string bb "'/>\n"
6735 | KMmulti (ins, kms) ->
6736 Buffer.add_char bb ' ';
6737 addkms ins;
6738 Buffer.add_string bb "' out='";
6739 addkms kms;
6740 Buffer.add_string bb "'/>\n"
6741 ) h;
6742 Buffer.add_string bb "</keymap>";
6745 loop rest
6747 loop c.keyhashes;
6751 let save () =
6752 let uifontsize = fstate.fontsize in
6753 let bb = Buffer.create 32768 in
6754 let f (h, dc) =
6755 let dc = if conf.bedefault then conf else dc in
6756 Buffer.add_string bb "<llppconfig>\n";
6758 if String.length !fontpath > 0
6759 then
6760 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6761 uifontsize
6762 !fontpath
6763 else (
6764 if uifontsize <> 14
6765 then
6766 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6769 Buffer.add_string bb "<defaults ";
6770 add_attrs bb true dc dc;
6771 let kb = keymapsbuf true dc dc in
6772 if Buffer.length kb > 0
6773 then (
6774 Buffer.add_string bb ">\n";
6775 Buffer.add_buffer bb kb;
6776 Buffer.add_string bb "\n</defaults>\n";
6778 else Buffer.add_string bb "/>\n";
6780 let adddoc path pan anchor c bookmarks =
6781 if bookmarks == [] && c = dc && anchor = emptyanchor
6782 then ()
6783 else (
6784 Printf.bprintf bb "<doc path='%s'"
6785 (enent path 0 (String.length path));
6787 if anchor <> emptyanchor
6788 then (
6789 let n, rely, visy = anchor in
6790 Printf.bprintf bb " page='%d'" n;
6791 if rely > 1e-6
6792 then
6793 Printf.bprintf bb " rely='%f'" rely
6795 if abs_float visy > 1e-6
6796 then
6797 Printf.bprintf bb " visy='%f'" visy
6801 if pan != 0
6802 then Printf.bprintf bb " pan='%d'" pan;
6804 add_attrs bb false dc c;
6805 let kb = keymapsbuf false dc c in
6807 begin match bookmarks with
6808 | [] ->
6809 if Buffer.length kb > 0
6810 then (
6811 Buffer.add_string bb ">\n";
6812 Buffer.add_buffer bb kb;
6813 Buffer.add_string bb "\n</doc>\n";
6815 else Buffer.add_string bb "/>\n"
6816 | _ ->
6817 Buffer.add_string bb ">\n<bookmarks>\n";
6818 List.iter (fun (title, _level, (page, rely, visy)) ->
6819 Printf.bprintf bb
6820 "<item title='%s' page='%d'"
6821 (enent title 0 (String.length title))
6822 page
6824 if rely > 1e-6
6825 then
6826 Printf.bprintf bb " rely='%f'" rely
6828 if abs_float visy > 1e-6
6829 then
6830 Printf.bprintf bb " visy='%f'" visy
6832 Buffer.add_string bb "/>\n";
6833 ) bookmarks;
6834 Buffer.add_string bb "</bookmarks>";
6835 if Buffer.length kb > 0
6836 then (
6837 Buffer.add_string bb "\n";
6838 Buffer.add_buffer bb kb;
6840 Buffer.add_string bb "\n</doc>\n";
6841 end;
6845 let pan, conf =
6846 match state.mode with
6847 | Birdseye (c, pan, _, _, _) ->
6848 let beyecolumns =
6849 match conf.columns with
6850 | Cmulti ((c, _, _), _) -> Some c
6851 | Csingle _ -> None
6852 | Csplit _ -> None
6853 and columns =
6854 match c.columns with
6855 | Cmulti (c, _) -> Cmulti (c, [||])
6856 | Csingle _ -> Csingle [||]
6857 | Csplit _ -> failwith "quit from bird's eye while split"
6859 pan, { c with beyecolumns = beyecolumns; columns = columns }
6860 | _ -> state.x, conf
6862 let basename = Filename.basename state.path in
6863 adddoc basename pan (getanchor ())
6864 (let conf =
6865 let autoscrollstep =
6866 match state.autoscroll with
6867 | Some step -> step
6868 | None -> conf.autoscrollstep
6870 match state.mode with
6871 | Birdseye (bc, _, _, _, _) ->
6872 { conf with
6873 zoom = bc.zoom;
6874 presentation = bc.presentation;
6875 interpagespace = bc.interpagespace;
6876 maxwait = bc.maxwait;
6877 autoscrollstep = autoscrollstep }
6878 | _ -> { conf with autoscrollstep = autoscrollstep }
6879 in conf)
6880 (if conf.savebmarks then state.bookmarks else []);
6882 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6883 if basename <> path
6884 then adddoc path x anchor c bookmarks
6885 ) h;
6886 Buffer.add_string bb "</llppconfig>\n";
6887 true;
6889 if load1 f && Buffer.length bb > 0
6890 then
6892 let tmp = !confpath ^ ".tmp" in
6893 let oc = open_out_bin tmp in
6894 Buffer.output_buffer oc bb;
6895 close_out oc;
6896 Unix.rename tmp !confpath;
6897 with exn ->
6898 prerr_endline
6899 ("error while saving configuration: " ^ Printexc.to_string exn)
6901 end;;
6903 let () =
6904 let trimcachepath = ref "" in
6905 Arg.parse
6906 (Arg.align
6907 [("-p", Arg.String (fun s -> state.password <- s) ,
6908 "<password> Set password");
6910 ("-f", Arg.String (fun s -> Config.fontpath := s),
6911 "<path> Set path to the user interface font");
6913 ("-c", Arg.String (fun s -> Config.confpath := s),
6914 "<path> Set path to the configuration file");
6916 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6917 "<path> Set path to the trim cache file");
6919 ("-v", Arg.Unit (fun () ->
6920 Printf.printf
6921 "%s\nconfiguration path: %s\n"
6922 (version ())
6923 Config.defconfpath
6925 exit 0), " Print version and exit");
6928 (fun s -> state.path <- s)
6929 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6931 if String.length state.path = 0
6932 then (prerr_endline "file name missing"; exit 1);
6934 if not (Config.load ())
6935 then prerr_endline "failed to load configuration";
6937 let globalkeyhash = findkeyhash conf "global" in
6938 let wsfd, winw, winh = Wsi.init (object
6939 method expose =
6940 state.wthack <- false;
6941 if nogeomcmds state.geomcmds || platform == Posx
6942 then display ()
6943 else (
6944 GlClear.color (scalecolor2 conf.bgcolor);
6945 GlClear.clear [`color];
6947 method display = display ()
6948 method reshape w h = reshape w h
6949 method mouse b d x y m = mouse b d x y m
6950 method motion x y = state.mpos <- (x, y); motion x y
6951 method pmotion x y = state.mpos <- (x, y); pmotion x y
6952 method key k m =
6953 let mascm = m land (
6954 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6955 ) in
6956 match state.keystate with
6957 | KSnone ->
6958 let km = k, mascm in
6959 begin
6960 match
6961 let modehash = state.uioh#modehash in
6962 try Hashtbl.find modehash km
6963 with Not_found ->
6964 try Hashtbl.find globalkeyhash km
6965 with Not_found -> KMinsrt (k, m)
6966 with
6967 | KMinsrt (k, m) -> keyboard k m
6968 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6969 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6971 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6972 List.iter (fun (k, m) -> keyboard k m) insrt;
6973 state.keystate <- KSnone
6974 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6975 state.keystate <- KSinto (keys, insrt)
6976 | _ ->
6977 state.keystate <- KSnone
6979 method enter x y = state.mpos <- (x, y); pmotion x y
6980 method leave = state.mpos <- (-1, -1)
6981 method quit = raise Quit
6982 end) conf.winw conf.winh (platform = Posx) in
6984 state.wsfd <- wsfd;
6986 if not (
6987 List.exists GlMisc.check_extension
6988 [ "GL_ARB_texture_rectangle"
6989 ; "GL_EXT_texture_recangle"
6990 ; "GL_NV_texture_rectangle" ]
6992 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6994 let cr, sw =
6995 match Ne.pipe () with
6996 | Ne.Exn exn ->
6997 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6998 exit 1
6999 | Ne.Res rw -> rw
7000 and sr, cw =
7001 match Ne.pipe () with
7002 | Ne.Exn exn ->
7003 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7004 exit 1
7005 | Ne.Res rw -> rw
7008 cloexec cr;
7009 cloexec sw;
7010 cloexec sr;
7011 cloexec cw;
7013 setcheckers conf.checkers;
7014 redirectstderr ();
7016 init (cr, cw) (
7017 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7018 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7019 !Config.fontpath, !trimcachepath,
7020 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7022 state.sr <- sr;
7023 state.sw <- sw;
7024 state.text <- "Opening " ^ (mbtoutf8 state.path);
7025 reshape winw winh;
7026 opendoc state.path state.password;
7027 state.uioh <- uioh;
7029 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7031 let rec loop deadline =
7032 let r =
7033 match state.errfd with
7034 | None -> [state.sr; state.wsfd]
7035 | Some fd -> [state.sr; state.wsfd; fd]
7037 if state.redisplay
7038 then (
7039 state.redisplay <- false;
7040 display ();
7042 let timeout =
7043 let now = now () in
7044 if deadline > now
7045 then (
7046 if deadline = infinity
7047 then ~-.1.0
7048 else max 0.0 (deadline -. now)
7050 else 0.0
7052 let r, _, _ =
7053 try Unix.select r [] [] timeout
7054 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7056 begin match r with
7057 | [] ->
7058 state.ghyll None;
7059 let newdeadline =
7060 if state.ghyll == noghyll
7061 then
7062 match state.autoscroll with
7063 | Some step when step != 0 ->
7064 let y = state.y + step in
7065 let y =
7066 if y < 0
7067 then state.maxy
7068 else if y >= state.maxy then 0 else y
7070 gotoy y;
7071 if state.mode = View
7072 then state.text <- "";
7073 deadline +. 0.01
7074 | _ -> infinity
7075 else deadline +. 0.01
7077 loop newdeadline
7079 | l ->
7080 let rec checkfds = function
7081 | [] -> ()
7082 | fd :: rest when fd = state.sr ->
7083 let cmd = readcmd state.sr in
7084 act cmd;
7085 checkfds rest
7087 | fd :: rest when fd = state.wsfd ->
7088 Wsi.readresp fd;
7089 checkfds rest
7091 | fd :: rest ->
7092 let s = String.create 80 in
7093 let n = Unix.read fd s 0 80 in
7094 if conf.redirectstderr
7095 then (
7096 Buffer.add_substring state.errmsgs s 0 n;
7097 state.newerrmsgs <- true;
7098 state.redisplay <- true;
7100 else (
7101 prerr_string (String.sub s 0 n);
7102 flush stderr;
7104 checkfds rest
7106 checkfds l;
7107 let newdeadline =
7108 let deadline1 =
7109 if deadline = infinity
7110 then now () +. 0.01
7111 else deadline
7113 match state.autoscroll with
7114 | Some step when step != 0 -> deadline1
7115 | _ -> if state.ghyll == noghyll then infinity else deadline1
7117 loop newdeadline
7118 end;
7121 loop infinity;
7122 with Quit ->
7123 Config.save ();