Magic
[llpp.git] / main.ml
blob4ed8a0a26f572e7549d74fe147674898afaf763e
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 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and dtop = float
36 and fontpath = string
37 and trimcachepath = string
38 and memsize = int
39 and aalevel = int
40 and irect = (int * int * int * int)
41 and trimparams = (trimmargins * irect)
42 and colorspace = | Rgb | Bgr | Gray
45 type link =
46 | Lnotfound
47 | Lfound of int
48 and linkdir =
49 | LDfirst
50 | LDlast
51 | LDfirstvisible of (int * int * int)
52 | LDleft of int
53 | LDright of int
54 | LDdown of int
55 | LDup of int
58 type pagewithlinks =
59 | Pwlnotfound
60 | Pwl of int
63 type keymap =
64 | KMinsrt of key
65 | KMinsrl of key list
66 | KMmulti of key list * key list
67 and key = int * int
68 and keyhash = (key, keymap) Hashtbl.t
69 and keystate =
70 | KSnone
71 | KSinto of (key list * key list)
74 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
75 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
77 type pipe = (Unix.file_descr * Unix.file_descr);;
79 external init : pipe -> params -> unit = "ml_init";;
80 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
81 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
82 external getpdimrect : int -> float array = "ml_getpdimrect";;
83 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
84 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
85 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
86 external measurestr : int -> string -> float = "ml_measure_string";;
87 external getmaxw : unit -> float = "ml_getmaxw";;
88 external postprocess :
89 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
90 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
91 external platform : unit -> platform = "ml_platform";;
92 external setaalevel : int -> unit = "ml_setaalevel";;
93 external realloctexts : int -> bool = "ml_realloctexts";;
94 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
95 external findlink : opaque -> linkdir -> link = "ml_findlink";;
96 external getlink : opaque -> int -> under = "ml_getlink";;
97 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
98 external getlinkcount : opaque -> int = "ml_getlinkcount";;
99 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
100 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
102 let platform_to_string = function
103 | Punknown -> "unknown"
104 | Plinux -> "Linux"
105 | Posx -> "OSX"
106 | Psun -> "Sun"
107 | Pfreebsd -> "FreeBSD"
108 | Pdragonflybsd -> "DragonflyBSD"
109 | Popenbsd -> "OpenBSD"
110 | Pnetbsd -> "NetBSD"
111 | Pcygwin -> "Cygwin"
114 let platform = platform ();;
116 let popen cmd fda =
117 if platform = Pcygwin
118 then (
119 let sh = "/bin/sh" in
120 let args = [|sh; "-c"; cmd|] in
121 let rec std si so se = function
122 | [] -> si, so, se
123 | (fd, 0) :: rest -> std fd so se rest
124 | (fd, -1) :: rest ->
125 Unix.set_close_on_exec fd;
126 std si so se rest
127 | (_, n) :: _ ->
128 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
130 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
131 ignore (Unix.create_process sh args si so se)
133 else popen cmd fda;
136 type x = int
137 and y = int
138 and tilex = int
139 and tiley = int
140 and tileparams = (x * y * width * height * tilex * tiley)
143 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
145 type mpos = int * int
146 and mstate =
147 | Msel of (mpos * mpos)
148 | Mpan of mpos
149 | Mscrolly | Mscrollx
150 | Mzoom of (int * int)
151 | Mzoomrect of (mpos * mpos)
152 | Mnone
155 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
156 and onkey = string -> int -> te
157 and ondone = string -> unit
158 and histcancel = unit -> unit
159 and onhist = ((histcmd -> string) * histcancel)
160 and histcmd = HCnext | HCprev | HCfirst | HClast
161 and cancelonempty = bool
162 and te =
163 | TEstop
164 | TEdone of string
165 | TEcont of string
166 | TEswitch of textentry
169 type 'a circbuf =
170 { store : 'a array
171 ; mutable rc : int
172 ; mutable wc : int
173 ; mutable len : int
177 let bound v minv maxv =
178 max minv (min maxv v);
181 let cbnew n v =
182 { store = Array.create n v
183 ; rc = 0
184 ; wc = 0
185 ; len = 0
189 let drawstring size x y s =
190 Gl.enable `blend;
191 Gl.enable `texture_2d;
192 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
193 ignore (drawstr size x y s);
194 Gl.disable `blend;
195 Gl.disable `texture_2d;
198 let drawstring1 size x y s =
199 drawstr size x y s;
202 let drawstring2 size x y fmt =
203 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
206 let cbcap b = Array.length b.store;;
208 let cbput b v =
209 let cap = cbcap b in
210 b.store.(b.wc) <- v;
211 b.wc <- (b.wc + 1) mod cap;
212 b.rc <- b.wc;
213 b.len <- min (b.len + 1) cap;
216 let cbempty b = b.len = 0;;
218 let cbgetg b circular dir =
219 if cbempty b
220 then b.store.(0)
221 else
222 let rc = b.rc + dir in
223 let rc =
224 if circular
225 then (
226 if rc = -1
227 then b.len-1
228 else (
229 if rc = b.len
230 then 0
231 else rc
234 else max 0 (min rc (b.len-1))
236 b.rc <- rc;
237 b.store.(rc);
240 let cbget b = cbgetg b false;;
241 let cbgetc b = cbgetg b true;;
243 type page =
244 { pageno : int
245 ; pagedimno : int
246 ; pagew : int
247 ; pageh : int
248 ; pagex : int
249 ; pagey : int
250 ; pagevw : int
251 ; pagevh : int
252 ; pagedispx : int
253 ; pagedispy : int
254 ; pagecol : int
258 let debugl l =
259 dolog "l %d dim=%d {" l.pageno l.pagedimno;
260 dolog " WxH %dx%d" l.pagew l.pageh;
261 dolog " vWxH %dx%d" l.pagevw l.pagevh;
262 dolog " pagex,y %d,%d" l.pagex l.pagey;
263 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
264 dolog " column %d" l.pagecol;
265 dolog "}";
268 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
269 dolog "rect {";
270 dolog " x0,y0=(% f, % f)" x0 y0;
271 dolog " x1,y1=(% f, % f)" x1 y1;
272 dolog " x2,y2=(% f, % f)" x2 y2;
273 dolog " x3,y3=(% f, % f)" x3 y3;
274 dolog "}";
277 type multicolumns = multicol * pagegeom
278 and singlecolumn = pagegeom
279 and splitcolumns = columncount * pagegeom
280 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
281 and multicol = columncount * covercount * covercount
282 and pdimno = int
283 and columncount = int
284 and covercount = int;;
286 type conf =
287 { mutable scrollbw : int
288 ; mutable scrollh : int
289 ; mutable icase : bool
290 ; mutable preload : bool
291 ; mutable pagebias : int
292 ; mutable verbose : bool
293 ; mutable debug : bool
294 ; mutable scrollstep : int
295 ; mutable hscrollstep : int
296 ; mutable maxhfit : bool
297 ; mutable crophack : bool
298 ; mutable autoscrollstep : int
299 ; mutable maxwait : float option
300 ; mutable hlinks : bool
301 ; mutable underinfo : bool
302 ; mutable interpagespace : interpagespace
303 ; mutable zoom : float
304 ; mutable presentation : bool
305 ; mutable angle : angle
306 ; mutable winw : int
307 ; mutable winh : int
308 ; mutable savebmarks : bool
309 ; mutable proportional : proportional
310 ; mutable trimmargins : trimmargins
311 ; mutable trimfuzz : irect
312 ; mutable memlimit : memsize
313 ; mutable texcount : texcount
314 ; mutable sliceheight : sliceheight
315 ; mutable thumbw : width
316 ; mutable jumpback : bool
317 ; mutable bgcolor : float * float * float
318 ; mutable bedefault : bool
319 ; mutable scrollbarinpm : bool
320 ; mutable tilew : int
321 ; mutable tileh : int
322 ; mutable mustoresize : memsize
323 ; mutable checkers : bool
324 ; mutable aalevel : int
325 ; mutable urilauncher : string
326 ; mutable pathlauncher : string
327 ; mutable colorspace : colorspace
328 ; mutable invert : bool
329 ; mutable colorscale : float
330 ; mutable redirectstderr : bool
331 ; mutable ghyllscroll : (int * int * int) option
332 ; mutable columns : columns
333 ; mutable beyecolumns : columncount option
334 ; mutable selcmd : string
335 ; mutable updatecurs : bool
336 ; mutable keyhashes : (string * keyhash) list
337 ; mutable hfsize : int
338 ; mutable pgscale : float
339 ; mutable multicenter : bool
341 and columns =
342 | Csingle of singlecolumn
343 | Cmulti of multicolumns
344 | Csplit of splitcolumns
347 type anchor = pageno * top * dtop;;
349 type outline = string * int * anchor;;
351 type rect = float * float * float * float * float * float * float * float;;
353 type tile = opaque * pixmapsize * elapsed
354 and elapsed = float;;
355 type pagemapkey = pageno * gen;;
356 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
357 and row = int
358 and col = int;;
360 let emptyanchor = (0, 0.0, 0.0);;
362 type infochange = | Memused | Docinfo | Pdim;;
364 class type uioh = object
365 method display : unit
366 method key : int -> int -> uioh
367 method button : int -> bool -> int -> int -> int -> uioh
368 method motion : int -> int -> uioh
369 method pmotion : int -> int -> uioh
370 method infochanged : infochange -> unit
371 method scrollpw : (int * float * float)
372 method scrollph : (int * float * float)
373 method modehash : keyhash
374 end;;
376 type mode =
377 | Birdseye of (conf * leftx * pageno * pageno * anchor)
378 | Textentry of (textentry * onleave)
379 | View
380 | LinkNav of linktarget
381 and onleave = leavetextentrystatus -> unit
382 and leavetextentrystatus = | Cancel | Confirm
383 and helpitem = string * int * action
384 and action =
385 | Noaction
386 | Action of (uioh -> uioh)
387 and linktarget =
388 | Ltexact of (pageno * int)
389 | Ltgendir of int
392 let isbirdseye = function Birdseye _ -> true | _ -> false;;
393 let istextentry = function Textentry _ -> true | _ -> false;;
395 type currently =
396 | Idle
397 | Loading of (page * gen)
398 | Tiling of (
399 page * opaque * colorspace * angle * gen * col * row * width * height
401 | Outlining of outline list
404 let emptykeyhash = Hashtbl.create 0;;
405 let nouioh : uioh = object (self)
406 method display = ()
407 method key _ _ = self
408 method button _ _ _ _ _ = self
409 method motion _ _ = self
410 method pmotion _ _ = self
411 method infochanged _ = ()
412 method scrollpw = (0, nan, nan)
413 method scrollph = (0, nan, nan)
414 method modehash = emptykeyhash
415 end;;
417 type state =
418 { mutable sr : Unix.file_descr
419 ; mutable sw : Unix.file_descr
420 ; mutable wsfd : Unix.file_descr
421 ; mutable errfd : Unix.file_descr option
422 ; mutable stderr : Unix.file_descr
423 ; mutable errmsgs : Buffer.t
424 ; mutable newerrmsgs : bool
425 ; mutable w : int
426 ; mutable x : int
427 ; mutable y : int
428 ; mutable scrollw : int
429 ; mutable hscrollh : int
430 ; mutable anchor : anchor
431 ; mutable ranchors : (string * string * anchor) list
432 ; mutable maxy : int
433 ; mutable layout : page list
434 ; pagemap : (pagemapkey, opaque) Hashtbl.t
435 ; tilemap : (tilemapkey, tile) Hashtbl.t
436 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
437 ; mutable pdims : (pageno * width * height * leftx) list
438 ; mutable pagecount : int
439 ; mutable currently : currently
440 ; mutable mstate : mstate
441 ; mutable searchpattern : string
442 ; mutable rects : (pageno * recttype * rect) list
443 ; mutable rects1 : (pageno * recttype * rect) list
444 ; mutable text : string
445 ; mutable fullscreen : (width * height) option
446 ; mutable mode : mode
447 ; mutable uioh : uioh
448 ; mutable outlines : outline array
449 ; mutable bookmarks : outline list
450 ; mutable path : string
451 ; mutable password : string
452 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
453 ; mutable memused : memsize
454 ; mutable gen : gen
455 ; mutable throttle : (page list * int * float) option
456 ; mutable autoscroll : int option
457 ; mutable ghyll : (int option -> unit)
458 ; mutable help : helpitem array
459 ; mutable docinfo : (int * string) list
460 ; mutable texid : GlTex.texture_id option
461 ; hists : hists
462 ; mutable prevzoom : float
463 ; mutable progress : float
464 ; mutable redisplay : bool
465 ; mutable mpos : mpos
466 ; mutable keystate : keystate
467 ; mutable glinks : bool
468 ; mutable prevcolumns : (columns * float) option
470 and hists =
471 { pat : string circbuf
472 ; pag : string circbuf
473 ; nav : anchor circbuf
474 ; sel : string circbuf
478 let defconf =
479 { scrollbw = 7
480 ; scrollh = 12
481 ; icase = true
482 ; preload = true
483 ; pagebias = 0
484 ; verbose = false
485 ; debug = false
486 ; scrollstep = 24
487 ; hscrollstep = 24
488 ; maxhfit = true
489 ; crophack = false
490 ; autoscrollstep = 2
491 ; maxwait = None
492 ; hlinks = false
493 ; underinfo = false
494 ; interpagespace = 2
495 ; zoom = 1.0
496 ; presentation = false
497 ; angle = 0
498 ; winw = 900
499 ; winh = 900
500 ; savebmarks = true
501 ; proportional = true
502 ; trimmargins = false
503 ; trimfuzz = (0,0,0,0)
504 ; memlimit = 32 lsl 20
505 ; texcount = 256
506 ; sliceheight = 24
507 ; thumbw = 76
508 ; jumpback = true
509 ; bgcolor = (0.5, 0.5, 0.5)
510 ; bedefault = false
511 ; scrollbarinpm = true
512 ; tilew = 2048
513 ; tileh = 2048
514 ; mustoresize = 256 lsl 20
515 ; checkers = true
516 ; aalevel = 8
517 ; urilauncher =
518 (match platform with
519 | Plinux | Pfreebsd | Pdragonflybsd
520 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
521 | Posx -> "open \"%s\""
522 | Pcygwin -> "cygstart \"%s\""
523 | Punknown -> "echo %s")
524 ; pathlauncher = "lp \"%s\""
525 ; selcmd =
526 (match platform with
527 | Plinux | Pfreebsd | Pdragonflybsd
528 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
529 | Posx -> "pbcopy"
530 | Pcygwin -> "wsel"
531 | Punknown -> "cat")
532 ; colorspace = Rgb
533 ; invert = false
534 ; colorscale = 1.0
535 ; redirectstderr = false
536 ; ghyllscroll = None
537 ; columns = Csingle [||]
538 ; beyecolumns = None
539 ; updatecurs = false
540 ; hfsize = 12
541 ; pgscale = 1.0
542 ; multicenter = false
543 ; keyhashes =
544 let mk n = (n, Hashtbl.create 1) in
545 [ mk "global"
546 ; mk "info"
547 ; mk "help"
548 ; mk "outline"
549 ; mk "listview"
550 ; mk "birdseye"
551 ; mk "textentry"
552 ; mk "links"
553 ; mk "view"
558 let findkeyhash c name =
559 try List.assoc name c.keyhashes
560 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
563 let conf = { defconf with angle = defconf.angle };;
565 let pgscale h = truncate (float h *. conf.pgscale);;
567 type fontstate =
568 { mutable fontsize : int
569 ; mutable wwidth : float
570 ; mutable maxrows : int
574 let fstate =
575 { fontsize = 14
576 ; wwidth = nan
577 ; maxrows = -1
581 let setfontsize n =
582 fstate.fontsize <- n;
583 fstate.wwidth <- measurestr fstate.fontsize "w";
584 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
587 let geturl s =
588 let colonpos = try String.index s ':' with Not_found -> -1 in
589 let len = String.length s in
590 if colonpos >= 0 && colonpos + 3 < len
591 then (
592 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
593 then
594 let schemestartpos =
595 try String.rindex_from s colonpos ' '
596 with Not_found -> -1
598 let scheme =
599 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
601 match scheme with
602 | "http" | "ftp" | "mailto" ->
603 let epos =
604 try String.index_from s colonpos ' '
605 with Not_found -> len
607 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
608 | _ -> ""
609 else ""
611 else ""
614 let gotouri uri =
615 if String.length conf.urilauncher = 0
616 then print_endline uri
617 else (
618 let url = geturl uri in
619 if String.length url = 0
620 then print_endline uri
621 else
622 let re = Str.regexp "%s" in
623 let command = Str.global_replace re url conf.urilauncher in
624 try popen command []
625 with exn ->
626 Printf.eprintf
627 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
628 flush stderr;
632 let version () =
633 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
634 (platform_to_string platform) Sys.word_size Sys.ocaml_version
637 let makehelp () =
638 let strings = version () :: "" :: Help.keys in
639 Array.of_list (
640 List.map (fun s ->
641 let url = geturl s in
642 if String.length url > 0
643 then (s, 0, Action (fun u -> gotouri url; u))
644 else (s, 0, Noaction)
645 ) strings);
648 let noghyll _ = ();;
649 let firstgeomcmds = "", [];;
651 let state =
652 { sr = Unix.stdin
653 ; sw = Unix.stdin
654 ; wsfd = Unix.stdin
655 ; errfd = None
656 ; stderr = Unix.stderr
657 ; errmsgs = Buffer.create 0
658 ; newerrmsgs = false
659 ; x = 0
660 ; y = 0
661 ; w = 0
662 ; scrollw = 0
663 ; hscrollh = 0
664 ; anchor = emptyanchor
665 ; ranchors = []
666 ; layout = []
667 ; maxy = max_int
668 ; tilelru = Queue.create ()
669 ; pagemap = Hashtbl.create 10
670 ; tilemap = Hashtbl.create 10
671 ; pdims = []
672 ; pagecount = 0
673 ; currently = Idle
674 ; mstate = Mnone
675 ; rects = []
676 ; rects1 = []
677 ; text = ""
678 ; mode = View
679 ; fullscreen = None
680 ; searchpattern = ""
681 ; outlines = [||]
682 ; bookmarks = []
683 ; path = ""
684 ; password = ""
685 ; geomcmds = firstgeomcmds
686 ; hists =
687 { nav = cbnew 10 emptyanchor
688 ; pat = cbnew 10 ""
689 ; pag = cbnew 10 ""
690 ; sel = cbnew 10 ""
692 ; memused = 0
693 ; gen = 0
694 ; throttle = None
695 ; autoscroll = None
696 ; ghyll = noghyll
697 ; help = makehelp ()
698 ; docinfo = []
699 ; texid = None
700 ; prevzoom = 1.0
701 ; progress = -1.0
702 ; uioh = nouioh
703 ; redisplay = true
704 ; mpos = (-1, -1)
705 ; keystate = KSnone
706 ; glinks = false
707 ; prevcolumns = None
711 let vlog fmt =
712 if conf.verbose
713 then
714 Printf.kprintf prerr_endline fmt
715 else
716 Printf.kprintf ignore fmt
719 let launchpath () =
720 if String.length conf.pathlauncher = 0
721 then print_endline state.path
722 else (
723 let re = Str.regexp "%s" in
724 let command = Str.global_replace re state.path conf.pathlauncher in
725 try popen command []
726 with exn ->
727 Printf.eprintf
728 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
729 flush stderr;
733 module Ne = struct
734 type 'a t = | Res of 'a | Exn of exn;;
736 let pipe () =
737 try Res (Unix.pipe ())
738 with exn -> Exn exn
741 let clo fd f =
742 try Unix.close fd
743 with exn -> f (Printexc.to_string exn)
746 let dup fd =
747 try Res (Unix.dup fd)
748 with exn -> Exn exn
751 let dup2 fd1 fd2 =
752 try Res (Unix.dup2 fd1 fd2)
753 with exn -> Exn exn
755 end;;
757 let redirectstderr () =
758 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
759 if conf.redirectstderr
760 then
761 match Ne.pipe () with
762 | Ne.Exn exn ->
763 dolog "failed to create stderr redirection pipes: %s"
764 (Printexc.to_string exn)
766 | Ne.Res (r, w) ->
767 begin match Ne.dup Unix.stderr with
768 | Ne.Exn exn ->
769 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
770 Ne.clo r (clofail "pipe/r");
771 Ne.clo w (clofail "pipe/w");
773 | Ne.Res dupstderr ->
774 begin match Ne.dup2 w Unix.stderr with
775 | Ne.Exn exn ->
776 dolog "failed to dup2 to stderr: %s"
777 (Printexc.to_string exn);
778 Ne.clo dupstderr (clofail "stderr duplicate");
779 Ne.clo r (clofail "redir pipe/r");
780 Ne.clo w (clofail "redir pipe/w");
782 | Ne.Res () ->
783 state.stderr <- dupstderr;
784 state.errfd <- Some r;
785 end;
787 else (
788 state.newerrmsgs <- false;
789 begin match state.errfd with
790 | Some fd ->
791 begin match Ne.dup2 state.stderr Unix.stderr with
792 | Ne.Exn exn ->
793 dolog "failed to dup2 original stderr: %s"
794 (Printexc.to_string exn)
795 | Ne.Res () ->
796 Ne.clo fd (clofail "dup of stderr");
797 Unix.dup2 state.stderr Unix.stderr;
798 state.errfd <- None;
799 end;
800 | None -> ()
801 end;
802 prerr_string (Buffer.contents state.errmsgs);
803 flush stderr;
804 Buffer.clear state.errmsgs;
808 module G =
809 struct
810 let postRedisplay who =
811 if conf.verbose
812 then prerr_endline ("redisplay for " ^ who);
813 state.redisplay <- true;
815 end;;
817 let getopaque pageno =
818 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
819 with Not_found -> None
822 let putopaque pageno opaque =
823 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
826 let pagetranslatepoint l x y =
827 let dy = y - l.pagedispy in
828 let y = dy + l.pagey in
829 let dx = x - l.pagedispx in
830 let x = dx + l.pagex in
831 (x, y);
834 let getunder x y =
835 let rec f = function
836 | l :: rest ->
837 begin match getopaque l.pageno with
838 | Some opaque ->
839 let x0 = l.pagedispx in
840 let x1 = x0 + l.pagevw in
841 let y0 = l.pagedispy in
842 let y1 = y0 + l.pagevh in
843 if y >= y0 && y <= y1 && x >= x0 && x <= x1
844 then
845 let px, py = pagetranslatepoint l x y in
846 match whatsunder opaque px py with
847 | Unone -> f rest
848 | under -> under
849 else f rest
850 | _ ->
851 f rest
853 | [] -> Unone
855 f state.layout
858 let showtext c s =
859 state.text <- Printf.sprintf "%c%s" c s;
860 G.postRedisplay "showtext";
863 let undertext = function
864 | Unone -> "none"
865 | Ulinkuri s -> s
866 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
867 | Utext s -> "font: " ^ s
868 | Uunexpected s -> "unexpected: " ^ s
869 | Ulaunch s -> "launch: " ^ s
870 | Unamed s -> "named: " ^ s
871 | Uremote (filename, pageno) ->
872 Printf.sprintf "%s: page %d" filename (pageno+1)
875 let updateunder x y =
876 match getunder x y with
877 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
878 | Ulinkuri uri ->
879 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
880 Wsi.setcursor Wsi.CURSOR_INFO
881 | Ulinkgoto (pageno, _) ->
882 if conf.underinfo
883 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
884 Wsi.setcursor Wsi.CURSOR_INFO
885 | Utext s ->
886 if conf.underinfo then showtext 'f' ("ont: " ^ s);
887 Wsi.setcursor Wsi.CURSOR_TEXT
888 | Uunexpected s ->
889 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
890 Wsi.setcursor Wsi.CURSOR_INHERIT
891 | Ulaunch s ->
892 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
893 Wsi.setcursor Wsi.CURSOR_INHERIT
894 | Unamed s ->
895 if conf.underinfo then showtext 'n' ("amed: " ^ s);
896 Wsi.setcursor Wsi.CURSOR_INHERIT
897 | Uremote (filename, pageno) ->
898 if conf.underinfo then showtext 'r'
899 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
900 Wsi.setcursor Wsi.CURSOR_INFO
903 let showlinktype under =
904 if conf.underinfo
905 then
906 match under with
907 | Unone -> ()
908 | under ->
909 let s = undertext under in
910 showtext ' ' s
913 let addchar s c =
914 let b = Buffer.create (String.length s + 1) in
915 Buffer.add_string b s;
916 Buffer.add_char b c;
917 Buffer.contents b;
920 let colorspace_of_string s =
921 match String.lowercase s with
922 | "rgb" -> Rgb
923 | "bgr" -> Bgr
924 | "gray" -> Gray
925 | _ -> failwith "invalid colorspace"
928 let int_of_colorspace = function
929 | Rgb -> 0
930 | Bgr -> 1
931 | Gray -> 2
934 let colorspace_of_int = function
935 | 0 -> Rgb
936 | 1 -> Bgr
937 | 2 -> Gray
938 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
941 let colorspace_to_string = function
942 | Rgb -> "rgb"
943 | Bgr -> "bgr"
944 | Gray -> "gray"
947 let intentry_with_suffix text key =
948 let c =
949 if key >= 32 && key < 127
950 then Char.chr key
951 else '\000'
953 match Char.lowercase c with
954 | '0' .. '9' ->
955 let text = addchar text c in
956 TEcont text
958 | 'k' | 'm' | 'g' ->
959 let text = addchar text c in
960 TEcont text
962 | _ ->
963 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
964 TEcont text
967 let multicolumns_to_string (n, a, b) =
968 if a = 0 && b = 0
969 then Printf.sprintf "%d" n
970 else Printf.sprintf "%d,%d,%d" n a b;
973 let multicolumns_of_string s =
975 (int_of_string s, 0, 0)
976 with _ ->
977 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
978 if a > 1 || b > 1
979 then failwith "subtly broken"; (n, a, b)
983 let readcmd fd =
984 let s = "xxxx" in
985 let n = Unix.read fd s 0 4 in
986 if n != 4 then failwith "incomplete read(len)";
987 let len = 0
988 lor (Char.code s.[0] lsl 24)
989 lor (Char.code s.[1] lsl 16)
990 lor (Char.code s.[2] lsl 8)
991 lor (Char.code s.[3] lsl 0)
993 let s = String.create len in
994 let n = Unix.read fd s 0 len in
995 if n != len then failwith "incomplete read(data)";
999 let btod b = if b then 1 else 0;;
1001 let wcmd fmt =
1002 let b = Buffer.create 16 in
1003 Buffer.add_string b "llll";
1004 Printf.kbprintf
1005 (fun b ->
1006 let s = Buffer.contents b in
1007 let n = String.length s in
1008 let len = n - 4 in
1009 (* dolog "wcmd %S" (String.sub s 4 len); *)
1010 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1011 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1012 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1013 s.[3] <- Char.chr (len land 0xff);
1014 let n' = Unix.write state.sw s 0 n in
1015 if n' != n then failwith "write failed";
1016 ) b fmt;
1019 let calcips h =
1020 let d = conf.winh - h in
1021 max conf.interpagespace ((d + 1) / 2)
1024 let rowyh (c, coverA, coverB) b n =
1025 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1026 then
1027 let _, _, vy, (_, _, h, _) = b.(n) in
1028 (vy, h)
1029 else
1030 let n' = n - coverA in
1031 let d = n' mod c in
1032 let s = n - d in
1033 let e = min state.pagecount (s + c) in
1034 let rec find m miny maxh = if m = e then miny, maxh else
1035 let _, _, y, (_, _, h, _) = b.(m) in
1036 let miny = min miny y in
1037 let maxh = max maxh h in
1038 find (m+1) miny maxh
1039 in find s max_int 0
1042 let calcheight () =
1043 match conf.columns with
1044 | Cmulti ((_, _, _) as cl, b) ->
1045 if Array.length b > 0
1046 then
1047 let y, h = rowyh cl b (Array.length b - 1) in
1048 y + h + (if conf.presentation then calcips h else 0)
1049 else 0
1050 | Csingle b ->
1051 if Array.length b > 0
1052 then
1053 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1054 y + h + (if conf.presentation then calcips h else 0)
1055 else 0
1056 | Csplit (_, b) ->
1057 if Array.length b > 0
1058 then
1059 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1060 y + h
1061 else 0
1064 let getpageyh pageno =
1065 let pageno = bound pageno 0 (state.pagecount-1) in
1066 match conf.columns with
1067 | Csingle b ->
1068 if Array.length b = 0
1069 then 0, 0
1070 else
1071 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1072 let y =
1073 if conf.presentation
1074 then y - calcips h
1075 else y
1077 y, h
1078 | Cmulti (cl, b) ->
1079 if Array.length b = 0
1080 then 0, 0
1081 else
1082 let y, h = rowyh cl b pageno in
1083 let y =
1084 if conf.presentation
1085 then y - calcips h
1086 else y
1088 y, h
1089 | Csplit (c, b) ->
1090 if Array.length b = 0
1091 then 0, 0
1092 else
1093 let n = pageno*c in
1094 let (_, _, y, (_, _, h, _)) = b.(n) in
1095 y, h
1098 let getpagedim pageno =
1099 let rec f ppdim l =
1100 match l with
1101 | (n, _, _, _) as pdim :: rest ->
1102 if n >= pageno
1103 then (if n = pageno then pdim else ppdim)
1104 else f pdim rest
1106 | [] -> ppdim
1108 f (-1, -1, -1, -1) state.pdims
1111 let getpagey pageno = fst (getpageyh pageno);;
1113 let nogeomcmds cmds =
1114 match cmds with
1115 | s, [] -> String.length s = 0
1116 | _ -> false
1119 let page_of_y y =
1120 let ((c, coverA, coverB) as cl), b =
1121 match conf.columns with
1122 | Csingle b -> (1, 0, 0), b
1123 | Cmulti (c, b) -> c, b
1124 | Csplit (_, b) -> (1, 0, 0), b
1126 let rec bsearch nmin nmax =
1127 if nmin > nmax
1128 then bound nmin 0 (state.pagecount-1)
1129 else
1130 let n = (nmax + nmin) / 2 in
1131 let vy, h = rowyh cl b n in
1132 let y0, y1 =
1133 if conf.presentation
1134 then
1135 let ips = calcips h in
1136 let y0 = vy - ips in
1137 let y1 = vy + h + ips in
1138 y0, y1
1139 else (
1140 if n = 0
1141 then 0, vy + h + conf.interpagespace
1142 else
1143 let y0 = vy - conf.interpagespace in
1144 y0, y0 + h + conf.interpagespace
1147 if y >= y0 && y < y1
1148 then (
1149 if c = 1
1150 then n
1151 else (
1152 if n > coverA
1153 then
1154 if n < state.pagecount - coverB
1155 then ((n-coverA)/c)*c + coverA
1156 else n
1157 else n
1160 else (
1161 if y > y0
1162 then bsearch (n+1) nmax
1163 else bsearch nmin (n-1)
1166 let r = bsearch 0 (state.pagecount-1) in
1170 let layoutN ((columns, coverA, coverB), b) y sh =
1171 let sh = sh - state.hscrollh in
1172 let rec fold accu n =
1173 if n = Array.length b
1174 then accu
1175 else
1176 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1177 if (vy - y) > sh &&
1178 (n = coverA - 1
1179 || n = state.pagecount - coverB
1180 || (n - coverA) mod columns = columns - 1)
1181 then accu
1182 else
1183 let accu =
1184 if vy + h > y
1185 then
1186 let pagey = max 0 (y - vy) in
1187 let pagedispy = if pagey > 0 then 0 else vy - y in
1188 let pagedispx, pagex =
1189 let pdx =
1190 if n = coverA - 1 || n = state.pagecount - coverB
1191 then state.x + (conf.winw - state.scrollw - w) / 2
1192 else dx + xoff + state.x
1194 if pdx < 0
1195 then 0, -pdx
1196 else pdx, 0
1198 let pagevw =
1199 let vw = conf.winw - state.scrollw - pagedispx in
1200 let pw = w - pagex in
1201 min vw pw
1203 let pagevh = min (h - pagey) (sh - pagedispy) in
1204 if pagevw > 0 && pagevh > 0
1205 then
1206 let e =
1207 { pageno = n
1208 ; pagedimno = pdimno
1209 ; pagew = w
1210 ; pageh = h
1211 ; pagex = pagex
1212 ; pagey = pagey
1213 ; pagevw = pagevw
1214 ; pagevh = pagevh
1215 ; pagedispx = pagedispx
1216 ; pagedispy = pagedispy
1217 ; pagecol = 0
1220 e :: accu
1221 else
1222 accu
1223 else
1224 accu
1226 fold accu (n+1)
1228 List.rev (fold [] (page_of_y y));
1231 let layoutS (columns, b) y sh =
1232 let sh = sh - state.hscrollh in
1233 let rec fold accu n =
1234 if n = Array.length b
1235 then accu
1236 else
1237 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1238 if (vy - y) > sh
1239 then accu
1240 else
1241 let accu =
1242 if vy + pageh > y
1243 then
1244 let x = xoff + state.x in
1245 let pagey = max 0 (y - vy) in
1246 let pagedispy = if pagey > 0 then 0 else vy - y in
1247 let pagedispx, pagex =
1248 if px = 0
1249 then (
1250 if x < 0
1251 then 0, -x
1252 else x, 0
1254 else (
1255 let px = px - x in
1256 if px < 0
1257 then -px, 0
1258 else 0, px
1261 let pagecolw = pagew/columns in
1262 let pagedispx =
1263 if pagecolw < conf.winw
1264 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1265 else pagedispx
1267 let pagevw =
1268 let vw = conf.winw - pagedispx - state.scrollw in
1269 let pw = pagew - pagex in
1270 min vw pw
1272 let pagevw = min pagevw pagecolw in
1273 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1274 if pagevw > 0 && pagevh > 0
1275 then
1276 let e =
1277 { pageno = n/columns
1278 ; pagedimno = pdimno
1279 ; pagew = pagew
1280 ; pageh = pageh
1281 ; pagex = pagex
1282 ; pagey = pagey
1283 ; pagevw = pagevw
1284 ; pagevh = pagevh
1285 ; pagedispx = pagedispx
1286 ; pagedispy = pagedispy
1287 ; pagecol = n mod columns
1290 e :: accu
1291 else
1292 accu
1293 else
1294 accu
1296 fold accu (n+1)
1298 List.rev (fold [] 0)
1301 let layout y sh =
1302 if nogeomcmds state.geomcmds
1303 then
1304 match conf.columns with
1305 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1306 | Cmulti c -> layoutN c y sh
1307 | Csplit s -> layoutS s y sh
1308 else []
1311 let clamp incr =
1312 let y = state.y + incr in
1313 let y = max 0 y in
1314 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1318 let itertiles l f =
1319 let tilex = l.pagex mod conf.tilew in
1320 let tiley = l.pagey mod conf.tileh in
1322 let col = l.pagex / conf.tilew in
1323 let row = l.pagey / conf.tileh in
1325 let rec rowloop row y0 dispy h =
1326 if h = 0
1327 then ()
1328 else (
1329 let dh = conf.tileh - y0 in
1330 let dh = min h dh in
1331 let rec colloop col x0 dispx w =
1332 if w = 0
1333 then ()
1334 else (
1335 let dw = conf.tilew - x0 in
1336 let dw = min w dw in
1338 f col row dispx dispy x0 y0 dw dh;
1339 colloop (col+1) 0 (dispx+dw) (w-dw)
1342 colloop col tilex l.pagedispx l.pagevw;
1343 rowloop (row+1) 0 (dispy+dh) (h-dh)
1346 if l.pagevw > 0 && l.pagevh > 0
1347 then rowloop row tiley l.pagedispy l.pagevh;
1350 let gettileopaque l col row =
1351 let key =
1352 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1354 try Some (Hashtbl.find state.tilemap key)
1355 with Not_found -> None
1358 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1359 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1360 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1363 let drawtiles l color =
1364 GlDraw.color color;
1365 let f col row x y tilex tiley w h =
1366 match gettileopaque l col row with
1367 | Some (opaque, _, t) ->
1368 let params = x, y, w, h, tilex, tiley in
1369 if conf.invert
1370 then (
1371 Gl.enable `blend;
1372 GlFunc.blend_func `zero `one_minus_src_color;
1374 drawtile params opaque;
1375 if conf.invert
1376 then Gl.disable `blend;
1377 if conf.debug
1378 then (
1379 let s = Printf.sprintf
1380 "%d[%d,%d] %f sec"
1381 l.pageno col row t
1383 let w = measurestr fstate.fontsize s in
1384 GlMisc.push_attrib [`current];
1385 GlDraw.color (0.0, 0.0, 0.0);
1386 GlDraw.rect
1387 (float (x-2), float (y-2))
1388 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1389 GlDraw.color (1.0, 1.0, 1.0);
1390 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1391 GlMisc.pop_attrib ();
1394 | _ ->
1395 let w =
1396 let lw = conf.winw - state.scrollw - x in
1397 min lw w
1398 and h =
1399 let lh = conf.winh - y in
1400 min lh h
1402 begin match state.texid with
1403 | Some id ->
1404 Gl.enable `texture_2d;
1405 GlTex.bind_texture `texture_2d id;
1406 let x0 = float x
1407 and y0 = float y
1408 and x1 = float (x+w)
1409 and y1 = float (y+h) in
1411 let tw = float w /. 64.0
1412 and th = float h /. 64.0 in
1413 let tx0 = float tilex /. 64.0
1414 and ty0 = float tiley /. 64.0 in
1415 let tx1 = tx0 +. tw
1416 and ty1 = ty0 +. th in
1417 GlDraw.begins `quads;
1418 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1419 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1420 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1421 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1422 GlDraw.ends ();
1424 Gl.disable `texture_2d;
1425 | None ->
1426 GlDraw.color (1.0, 1.0, 1.0);
1427 GlDraw.rect
1428 (float x, float y)
1429 (float (x+w), float (y+h));
1430 end;
1431 if w > 128 && h > fstate.fontsize + 10
1432 then (
1433 GlDraw.color (0.0, 0.0, 0.0);
1434 let c, r =
1435 if conf.verbose
1436 then (col*conf.tilew, row*conf.tileh)
1437 else col, row
1439 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1441 GlDraw.color color;
1443 itertiles l f
1446 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1448 let tilevisible1 l x y =
1449 let ax0 = l.pagex
1450 and ax1 = l.pagex + l.pagevw
1451 and ay0 = l.pagey
1452 and ay1 = l.pagey + l.pagevh in
1454 let bx0 = x
1455 and by0 = y in
1456 let bx1 = min (bx0 + conf.tilew) l.pagew
1457 and by1 = min (by0 + conf.tileh) l.pageh in
1459 let rx0 = max ax0 bx0
1460 and ry0 = max ay0 by0
1461 and rx1 = min ax1 bx1
1462 and ry1 = min ay1 by1 in
1464 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1465 nonemptyintersection
1468 let tilevisible layout n x y =
1469 let rec findpageinlayout m = function
1470 | l :: rest when l.pageno = n ->
1471 tilevisible1 l x y || (
1472 match conf.columns with
1473 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1474 | _ -> false
1476 | _ :: rest -> findpageinlayout 0 rest
1477 | [] -> false
1479 findpageinlayout 0 layout;
1482 let tileready l x y =
1483 tilevisible1 l x y &&
1484 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1487 let tilepage n p layout =
1488 let rec loop = function
1489 | l :: rest ->
1490 if l.pageno = n
1491 then
1492 let f col row _ _ _ _ _ _ =
1493 if state.currently = Idle
1494 then
1495 match gettileopaque l col row with
1496 | Some _ -> ()
1497 | None ->
1498 let x = col*conf.tilew
1499 and y = row*conf.tileh in
1500 let w =
1501 let w = l.pagew - x in
1502 min w conf.tilew
1504 let h =
1505 let h = l.pageh - y in
1506 min h conf.tileh
1508 wcmd "tile %s %d %d %d %d" p x y w h;
1509 state.currently <-
1510 Tiling (
1511 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1512 conf.tilew, conf.tileh
1515 itertiles l f;
1516 else
1517 loop rest
1519 | [] -> ()
1521 if nogeomcmds state.geomcmds
1522 then loop layout;
1525 let preloadlayout y =
1526 let y = if y < conf.winh then 0 else y - conf.winh in
1527 let h = conf.winh*3 in
1528 layout y h;
1531 let load pages =
1532 let rec loop pages =
1533 if state.currently != Idle
1534 then ()
1535 else
1536 match pages with
1537 | l :: rest ->
1538 begin match getopaque l.pageno with
1539 | None ->
1540 wcmd "page %d %d" l.pageno l.pagedimno;
1541 state.currently <- Loading (l, state.gen);
1542 | Some opaque ->
1543 tilepage l.pageno opaque pages;
1544 loop rest
1545 end;
1546 | _ -> ()
1548 if nogeomcmds state.geomcmds
1549 then loop pages
1552 let preload pages =
1553 load pages;
1554 if conf.preload && state.currently = Idle
1555 then load (preloadlayout state.y);
1558 let layoutready layout =
1559 let rec fold all ls =
1560 all && match ls with
1561 | l :: rest ->
1562 let seen = ref false in
1563 let allvisible = ref true in
1564 let foo col row _ _ _ _ _ _ =
1565 seen := true;
1566 allvisible := !allvisible &&
1567 begin match gettileopaque l col row with
1568 | Some _ -> true
1569 | None -> false
1572 itertiles l foo;
1573 fold (!seen && !allvisible) rest
1574 | [] -> true
1576 let alltilesvisible = fold true layout in
1577 alltilesvisible;
1580 let gotoy y =
1581 let y = bound y 0 state.maxy in
1582 let y, layout, proceed =
1583 match conf.maxwait with
1584 | Some time when state.ghyll == noghyll ->
1585 begin match state.throttle with
1586 | None ->
1587 let layout = layout y conf.winh in
1588 let ready = layoutready layout in
1589 if not ready
1590 then (
1591 load layout;
1592 state.throttle <- Some (layout, y, now ());
1594 else G.postRedisplay "gotoy showall (None)";
1595 y, layout, ready
1596 | Some (_, _, started) ->
1597 let dt = now () -. started in
1598 if dt > time
1599 then (
1600 state.throttle <- None;
1601 let layout = layout y conf.winh in
1602 load layout;
1603 G.postRedisplay "maxwait";
1604 y, layout, true
1606 else -1, [], false
1609 | _ ->
1610 let layout = layout y conf.winh in
1611 if true || layoutready layout
1612 then G.postRedisplay "gotoy ready";
1613 y, layout, true
1615 if proceed
1616 then (
1617 state.y <- y;
1618 state.layout <- layout;
1619 begin match state.mode with
1620 | LinkNav (Ltexact (pageno, linkno)) ->
1621 let rec loop = function
1622 | [] ->
1623 state.mode <- LinkNav (Ltgendir 0)
1624 | l :: _ when l.pageno = pageno ->
1625 begin match getopaque pageno with
1626 | None ->
1627 state.mode <- LinkNav (Ltgendir 0)
1628 | Some opaque ->
1629 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1630 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1631 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1632 then state.mode <- LinkNav (Ltgendir 0)
1634 | _ :: rest -> loop rest
1636 loop layout
1637 | _ -> ()
1638 end;
1639 begin match state.mode with
1640 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1641 if not (pagevisible layout pageno)
1642 then (
1643 match state.layout with
1644 | [] -> ()
1645 | l :: _ ->
1646 state.mode <- Birdseye (
1647 conf, leftx, l.pageno, hooverpageno, anchor
1650 | LinkNav (Ltgendir dir as lt) ->
1651 let linknav =
1652 let rec loop = function
1653 | [] -> lt
1654 | l :: rest ->
1655 match getopaque l.pageno with
1656 | None -> loop rest
1657 | Some opaque ->
1658 let link =
1659 let ld =
1660 if dir = 0
1661 then LDfirstvisible (l.pagex, l.pagey, dir)
1662 else (
1663 if dir > 0 then LDfirst else LDlast
1666 findlink opaque ld
1668 match link with
1669 | Lnotfound -> loop rest
1670 | Lfound n ->
1671 showlinktype (getlink opaque n);
1672 Ltexact (l.pageno, n)
1674 loop state.layout
1676 state.mode <- LinkNav linknav
1677 | _ -> ()
1678 end;
1679 preload layout;
1681 state.ghyll <- noghyll;
1682 if conf.updatecurs
1683 then (
1684 let mx, my = state.mpos in
1685 updateunder mx my;
1689 let conttiling pageno opaque =
1690 tilepage pageno opaque
1691 (if conf.preload then preloadlayout state.y else state.layout)
1694 let gotoy_and_clear_text y =
1695 if not conf.verbose then state.text <- "";
1696 gotoy y;
1699 let getanchor1 l =
1700 let top =
1701 let coloff = l.pagecol * l.pageh in
1702 float (l.pagey + coloff) /. float l.pageh
1704 let dtop =
1705 if l.pagedispy = 0
1706 then
1708 else
1709 if conf.presentation
1710 then float l.pagedispy /. float (calcips l.pageh)
1711 else float l.pagedispy /. float conf.interpagespace
1713 (l.pageno, top, dtop)
1716 let getanchor () =
1717 match state.layout with
1718 | l :: _ -> getanchor1 l
1719 | [] ->
1720 let n = page_of_y state.y in
1721 let y, h = getpageyh n in
1722 let dy = y - state.y in
1723 let dtop =
1724 if conf.presentation
1725 then
1726 let ips = calcips h in
1727 float (dy + ips) /. float ips
1728 else
1729 float dy /. float conf.interpagespace
1731 (n, 0.0, dtop)
1734 let getanchory (n, top, dtop) =
1735 let y, h = getpageyh n in
1736 if conf.presentation
1737 then
1738 let ips = calcips h in
1739 y + truncate (top*.float h -. dtop*.float ips) + ips;
1740 else
1741 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1744 let gotoanchor anchor =
1745 gotoy (getanchory anchor);
1748 let addnav () =
1749 cbput state.hists.nav (getanchor ());
1752 let getnav dir =
1753 let anchor = cbgetc state.hists.nav dir in
1754 getanchory anchor;
1757 let gotoghyll y =
1758 let scroll f n a b =
1759 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1760 let snake f a b =
1761 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1762 if f < a
1763 then s (float f /. float a)
1764 else (
1765 if f > b
1766 then 1.0 -. s ((float (f-b) /. float (n-b)))
1767 else 1.0
1770 snake f a b
1771 and summa f n a b =
1772 (* courtesy:
1773 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1774 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1775 let iv1 = iv f in
1776 let ins = float a *. iv1
1777 and outs = float (n-b) *. iv1 in
1778 let ones = b - a in
1779 ins +. outs +. float ones
1781 let rec set (_N, _A, _B) y sy =
1782 let sum = summa 1.0 _N _A _B in
1783 let dy = float (y - sy) in
1784 state.ghyll <- (
1785 let rec gf n y1 o =
1786 if n >= _N
1787 then state.ghyll <- noghyll
1788 else
1789 let go n =
1790 let s = scroll n _N _A _B in
1791 let y1 = y1 +. ((s *. dy) /. sum) in
1792 gotoy_and_clear_text (truncate y1);
1793 state.ghyll <- gf (n+1) y1;
1795 match o with
1796 | None -> go n
1797 | Some y' -> set (_N/2, 1, 1) y' state.y
1799 gf 0 (float state.y)
1802 match conf.ghyllscroll with
1803 | None ->
1804 gotoy_and_clear_text y
1805 | Some nab ->
1806 if state.ghyll == noghyll
1807 then set nab y state.y
1808 else state.ghyll (Some y)
1811 let gotopage n top =
1812 let y, h = getpageyh n in
1813 let y = y + (truncate (top *. float h)) in
1814 gotoghyll y
1817 let gotopage1 n top =
1818 let y = getpagey n in
1819 let y = y + top in
1820 gotoghyll y
1823 let invalidate s f =
1824 state.layout <- [];
1825 state.pdims <- [];
1826 state.rects <- [];
1827 state.rects1 <- [];
1828 match state.geomcmds with
1829 | ps, [] when String.length ps = 0 ->
1830 f ();
1831 state.geomcmds <- s, [];
1833 | ps, [] ->
1834 state.geomcmds <- ps, [s, f];
1836 | ps, (s', _) :: rest when s' = s ->
1837 state.geomcmds <- ps, ((s, f) :: rest);
1839 | ps, cmds ->
1840 state.geomcmds <- ps, ((s, f) :: cmds);
1843 let opendoc path password =
1844 state.path <- path;
1845 state.password <- password;
1846 state.gen <- state.gen + 1;
1847 state.docinfo <- [];
1849 setaalevel conf.aalevel;
1850 Wsi.settitle ("llpp " ^ Filename.basename path);
1851 wcmd "open %s\000%s\000" path password;
1852 invalidate "reqlayout"
1853 (fun () ->
1854 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1857 let scalecolor c =
1858 let c = c *. conf.colorscale in
1859 (c, c, c);
1862 let scalecolor2 (r, g, b) =
1863 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1866 let docolumns = function
1867 | Csingle _ ->
1868 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1869 let rec loop pageno pdimno pdim y ph pdims =
1870 if pageno = state.pagecount
1871 then ()
1872 else
1873 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1874 match pdims with
1875 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1876 pdimno+1, pdim, rest
1877 | _ ->
1878 pdimno, pdim, pdims
1880 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1881 let y = y +
1882 (if conf.presentation
1883 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1884 else (if pageno = 0 then 0 else conf.interpagespace)
1887 a.(pageno) <- (pdimno, x, y, pdim);
1888 loop (pageno+1) pdimno pdim (y + h) h pdims
1890 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1891 conf.columns <- Csingle a;
1893 | Cmulti ((columns, coverA, coverB), _) ->
1894 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1895 let rec loop pageno pdimno pdim x y rowh pdims =
1896 let rec fixrow m = if m = pageno then () else
1897 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1898 if h < rowh
1899 then (
1900 let y = y + (rowh - h) / 2 in
1901 a.(m) <- (pdimno, x, y, pdim);
1903 fixrow (m+1)
1905 if pageno = state.pagecount
1906 then fixrow (((pageno - 1) / columns) * columns)
1907 else
1908 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1909 match pdims with
1910 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1911 pdimno+1, pdim, rest
1912 | _ ->
1913 pdimno, pdim, pdims
1915 let x, y, rowh' =
1916 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1917 then (
1918 let x = (conf.winw - state.scrollw - w) / 2 in
1919 let ips =
1920 if conf.presentation then calcips h else conf.interpagespace in
1921 x, y + ips + rowh, h
1923 else (
1924 if (pageno - coverA) mod columns = 0
1925 then (
1926 let x =
1927 if conf.multicenter
1928 then (conf.winw - state.scrollw - state.w) / 2
1929 else 0
1931 let y =
1932 if conf.presentation
1933 then
1934 let ips = calcips h in
1935 if pageno = 0
1936 then y + ips
1937 else y + calcips rowh + ips
1938 else
1939 y + (if pageno = 0 then 0 else conf.interpagespace)
1941 x, y + rowh, h
1943 else x, y, max rowh h
1946 if pageno > 1 && (pageno - coverA) mod columns = 0
1947 then fixrow (pageno - columns);
1948 a.(pageno) <- (pdimno, x, y, pdim);
1949 let x = x + w + xoff*2 + conf.interpagespace in
1950 loop (pageno+1) pdimno pdim x y rowh' pdims
1952 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1953 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1955 | Csplit (c, _) ->
1956 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1957 let rec loop pageno pdimno pdim y pdims =
1958 if pageno = state.pagecount
1959 then ()
1960 else
1961 let pdimno, ((_, w, h, _) as pdim), pdims =
1962 match pdims with
1963 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1964 pdimno+1, pdim, rest
1965 | _ ->
1966 pdimno, pdim, pdims
1968 let cw = w / c in
1969 let rec loop1 n x y =
1970 if n = c then y else (
1971 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1972 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1975 let y = loop1 0 0 y in
1976 loop (pageno+1) pdimno pdim y pdims
1978 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1979 conf.columns <- Csplit (c, a);
1982 let represent () =
1983 docolumns conf.columns;
1984 state.maxy <- calcheight ();
1985 state.hscrollh <-
1986 if state.w <= conf.winw - state.scrollw
1987 then 0
1988 else state.scrollw
1990 match state.mode with
1991 | Birdseye (_, _, pageno, _, _) ->
1992 let y, h = getpageyh pageno in
1993 let top = (conf.winh - h) / 2 in
1994 gotoy (max 0 (y - top))
1995 | _ -> gotoanchor state.anchor
1998 let reshape w h =
1999 GlDraw.viewport 0 0 w h;
2000 let firsttime = state.geomcmds == firstgeomcmds in
2001 if not firsttime && nogeomcmds state.geomcmds
2002 then state.anchor <- getanchor ();
2004 conf.winw <- w;
2005 let w = truncate (float w *. conf.zoom) - state.scrollw in
2006 let w = max w 2 in
2007 conf.winh <- h;
2008 setfontsize fstate.fontsize;
2009 GlMat.mode `modelview;
2010 GlMat.load_identity ();
2012 GlMat.mode `projection;
2013 GlMat.load_identity ();
2014 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2015 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2016 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2018 let relx =
2019 if conf.zoom <= 1.0
2020 then 0.0
2021 else float state.x /. float state.w
2023 invalidate "geometry"
2024 (fun () ->
2025 state.w <- w;
2026 if not firsttime
2027 then state.x <- truncate (relx *. float w);
2028 let w =
2029 match conf.columns with
2030 | Csingle _ -> w
2031 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2032 | Csplit (c, _) -> w * c
2034 wcmd "geometry %d %d" w h);
2037 let enttext () =
2038 let len = String.length state.text in
2039 let drawstring s =
2040 let hscrollh =
2041 match state.mode with
2042 | Textentry _
2043 | View ->
2044 let h, _, _ = state.uioh#scrollpw in
2046 | _ -> 0
2048 let rect x w =
2049 GlDraw.rect
2050 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2051 (x+.w, float (conf.winh - hscrollh))
2054 let w = float (conf.winw - state.scrollw - 1) in
2055 if state.progress >= 0.0 && state.progress < 1.0
2056 then (
2057 GlDraw.color (0.3, 0.3, 0.3);
2058 let w1 = w *. state.progress in
2059 rect 0.0 w1;
2060 GlDraw.color (0.0, 0.0, 0.0);
2061 rect w1 (w-.w1)
2063 else (
2064 GlDraw.color (0.0, 0.0, 0.0);
2065 rect 0.0 w;
2068 GlDraw.color (1.0, 1.0, 1.0);
2069 drawstring fstate.fontsize
2070 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2072 let s =
2073 match state.mode with
2074 | Textentry ((prefix, text, _, _, _, _), _) ->
2075 let s =
2076 if len > 0
2077 then
2078 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2079 else
2080 Printf.sprintf "%s%s_" prefix text
2084 | _ -> state.text
2086 let s =
2087 if state.newerrmsgs
2088 then (
2089 if not (istextentry state.mode)
2090 then
2091 let s1 = "(press 'e' to review error messasges)" in
2092 if String.length s > 0 then s ^ " " ^ s1 else s1
2093 else s
2095 else s
2097 if String.length s > 0
2098 then drawstring s
2101 let gctiles () =
2102 let len = Queue.length state.tilelru in
2103 let layout = lazy (
2104 match state.throttle with
2105 | None ->
2106 if conf.preload
2107 then preloadlayout state.y
2108 else state.layout
2109 | Some (layout, _, _) ->
2110 layout
2111 ) in
2112 let rec loop qpos =
2113 if state.memused <= conf.memlimit
2114 then ()
2115 else (
2116 if qpos < len
2117 then
2118 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2119 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2120 let (_, pw, ph, _) = getpagedim n in
2122 gen = state.gen
2123 && colorspace = conf.colorspace
2124 && angle = conf.angle
2125 && pagew = pw
2126 && pageh = ph
2127 && (
2128 let x = col*conf.tilew
2129 and y = row*conf.tileh in
2130 tilevisible (Lazy.force_val layout) n x y
2132 then Queue.push lruitem state.tilelru
2133 else (
2134 wcmd "freetile %s" p;
2135 state.memused <- state.memused - s;
2136 state.uioh#infochanged Memused;
2137 Hashtbl.remove state.tilemap k;
2139 loop (qpos+1)
2142 loop 0
2145 let flushtiles () =
2146 Queue.iter (fun (k, p, s) ->
2147 wcmd "freetile %s" p;
2148 state.memused <- state.memused - s;
2149 state.uioh#infochanged Memused;
2150 Hashtbl.remove state.tilemap k;
2151 ) state.tilelru;
2152 Queue.clear state.tilelru;
2153 load state.layout;
2156 let logcurrently = function
2157 | Idle -> dolog "Idle"
2158 | Loading (l, gen) ->
2159 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2160 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2161 dolog
2162 "Tiling %d[%d,%d] page=%s cs=%s angle"
2163 l.pageno col row pageopaque
2164 (colorspace_to_string colorspace)
2166 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2167 angle gen conf.angle state.gen
2168 tilew tileh
2169 conf.tilew conf.tileh
2171 | Outlining _ ->
2172 dolog "outlining"
2175 let act cmds =
2176 (* dolog "%S" cmds; *)
2177 let op, args =
2178 let spacepos =
2179 try String.index cmds ' '
2180 with Not_found -> -1
2182 if spacepos = -1
2183 then cmds, ""
2184 else
2185 let l = String.length cmds in
2186 let op = String.sub cmds 0 spacepos in
2187 op, begin
2188 if l - spacepos < 2 then ""
2189 else String.sub cmds (spacepos+1) (l-spacepos-1)
2192 match op with
2193 | "clear" ->
2194 state.uioh#infochanged Pdim;
2195 state.pdims <- [];
2197 | "clearrects" ->
2198 state.rects <- state.rects1;
2199 G.postRedisplay "clearrects";
2201 | "continue" ->
2202 let n =
2203 try Scanf.sscanf args "%u" (fun n -> n)
2204 with exn ->
2205 dolog "error processing 'continue' %S: %s"
2206 cmds (Printexc.to_string exn);
2207 exit 1;
2209 state.pagecount <- n;
2210 begin match state.currently with
2211 | Outlining l ->
2212 state.currently <- Idle;
2213 state.outlines <- Array.of_list (List.rev l)
2214 | _ -> ()
2215 end;
2217 let cur, cmds = state.geomcmds in
2218 if String.length cur = 0
2219 then failwith "umpossible";
2221 begin match List.rev cmds with
2222 | [] ->
2223 state.geomcmds <- "", [];
2224 represent ();
2225 | (s, f) :: rest ->
2226 f ();
2227 state.geomcmds <- s, List.rev rest;
2228 end;
2229 if conf.maxwait = None
2230 then G.postRedisplay "continue";
2232 | "title" ->
2233 Wsi.settitle args
2235 | "msg" ->
2236 showtext ' ' args
2238 | "vmsg" ->
2239 if conf.verbose
2240 then showtext ' ' args
2242 | "progress" ->
2243 let progress, text =
2245 Scanf.sscanf args "%f %n"
2246 (fun f pos ->
2247 f, String.sub args pos (String.length args - pos))
2248 with exn ->
2249 dolog "error processing 'progress' %S: %s"
2250 cmds (Printexc.to_string exn);
2251 exit 1;
2253 state.text <- text;
2254 state.progress <- progress;
2255 G.postRedisplay "progress"
2257 | "firstmatch" ->
2258 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2260 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2261 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2262 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2263 with exn ->
2264 dolog "error processing 'firstmatch' %S: %s"
2265 cmds (Printexc.to_string exn);
2266 exit 1;
2268 let y = (getpagey pageno) + truncate y0 in
2269 addnav ();
2270 gotoy y;
2271 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2273 | "match" ->
2274 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2276 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2277 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2278 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2279 with exn ->
2280 dolog "error processing 'match' %S: %s"
2281 cmds (Printexc.to_string exn);
2282 exit 1;
2284 state.rects1 <-
2285 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2287 | "page" ->
2288 let pageopaque, t =
2290 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2291 with exn ->
2292 dolog "error processing 'page' %S: %s"
2293 cmds (Printexc.to_string exn);
2294 exit 1;
2296 begin match state.currently with
2297 | Loading (l, gen) ->
2298 vlog "page %d took %f sec" l.pageno t;
2299 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2300 begin match state.throttle with
2301 | None ->
2302 let preloadedpages =
2303 if conf.preload
2304 then preloadlayout state.y
2305 else state.layout
2307 let evict () =
2308 let module IntSet =
2309 Set.Make (struct type t = int let compare = (-) end) in
2310 let set =
2311 List.fold_left (fun s l -> IntSet.add l.pageno s)
2312 IntSet.empty preloadedpages
2314 let evictedpages =
2315 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2316 if not (IntSet.mem pageno set)
2317 then (
2318 wcmd "freepage %s" opaque;
2319 key :: accu
2321 else accu
2322 ) state.pagemap []
2324 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2326 evict ();
2327 state.currently <- Idle;
2328 if gen = state.gen
2329 then (
2330 tilepage l.pageno pageopaque state.layout;
2331 load state.layout;
2332 load preloadedpages;
2333 if pagevisible state.layout l.pageno
2334 && layoutready state.layout
2335 then G.postRedisplay "page";
2338 | Some (layout, _, _) ->
2339 state.currently <- Idle;
2340 tilepage l.pageno pageopaque layout;
2341 load state.layout
2342 end;
2344 | _ ->
2345 dolog "Inconsistent loading state";
2346 logcurrently state.currently;
2347 exit 1
2350 | "tile" ->
2351 let (x, y, opaque, size, t) =
2353 Scanf.sscanf args "%u %u %s %u %f"
2354 (fun x y p size t -> (x, y, p, size, t))
2355 with exn ->
2356 dolog "error processing 'tile' %S: %s"
2357 cmds (Printexc.to_string exn);
2358 exit 1;
2360 begin match state.currently with
2361 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2362 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2364 if tilew != conf.tilew || tileh != conf.tileh
2365 then (
2366 wcmd "freetile %s" opaque;
2367 state.currently <- Idle;
2368 load state.layout;
2370 else (
2371 puttileopaque l col row gen cs angle opaque size t;
2372 state.memused <- state.memused + size;
2373 state.uioh#infochanged Memused;
2374 gctiles ();
2375 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2376 opaque, size) state.tilelru;
2378 let layout =
2379 match state.throttle with
2380 | None -> state.layout
2381 | Some (layout, _, _) -> layout
2384 state.currently <- Idle;
2385 if gen = state.gen
2386 && conf.colorspace = cs
2387 && conf.angle = angle
2388 && tilevisible layout l.pageno x y
2389 then conttiling l.pageno pageopaque;
2391 begin match state.throttle with
2392 | None ->
2393 preload state.layout;
2394 if gen = state.gen
2395 && conf.colorspace = cs
2396 && conf.angle = angle
2397 && tilevisible state.layout l.pageno x y
2398 then G.postRedisplay "tile nothrottle";
2400 | Some (layout, y, _) ->
2401 let ready = layoutready layout in
2402 if ready
2403 then (
2404 state.y <- y;
2405 state.layout <- layout;
2406 state.throttle <- None;
2407 G.postRedisplay "throttle";
2409 else load layout;
2410 end;
2413 | _ ->
2414 dolog "Inconsistent tiling state";
2415 logcurrently state.currently;
2416 exit 1
2419 | "pdim" ->
2420 let pdim =
2422 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2423 with exn ->
2424 dolog "error processing 'pdim' %S: %s"
2425 cmds (Printexc.to_string exn);
2426 exit 1;
2428 state.uioh#infochanged Pdim;
2429 state.pdims <- pdim :: state.pdims
2431 | "o" ->
2432 let (l, n, t, h, pos) =
2434 Scanf.sscanf args "%u %u %d %u %n"
2435 (fun l n t h pos -> l, n, t, h, pos)
2436 with exn ->
2437 dolog "error processing 'o' %S: %s"
2438 cmds (Printexc.to_string exn);
2439 exit 1;
2441 let s = String.sub args pos (String.length args - pos) in
2442 let outline = (s, l, (n, float t /. float h, 0.0)) in
2443 begin match state.currently with
2444 | Outlining outlines ->
2445 state.currently <- Outlining (outline :: outlines)
2446 | Idle ->
2447 state.currently <- Outlining [outline]
2448 | currently ->
2449 dolog "invalid outlining state";
2450 logcurrently currently
2453 | "info" ->
2454 state.docinfo <- (1, args) :: state.docinfo
2456 | "infoend" ->
2457 state.uioh#infochanged Docinfo;
2458 state.docinfo <- List.rev state.docinfo
2460 | _ ->
2461 dolog "unknown cmd `%S'" cmds
2464 let onhist cb =
2465 let rc = cb.rc in
2466 let action = function
2467 | HCprev -> cbget cb ~-1
2468 | HCnext -> cbget cb 1
2469 | HCfirst -> cbget cb ~-(cb.rc)
2470 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2471 and cancel () = cb.rc <- rc
2472 in (action, cancel)
2475 let search pattern forward =
2476 if String.length pattern > 0
2477 then
2478 let pn, py =
2479 match state.layout with
2480 | [] -> 0, 0
2481 | l :: _ ->
2482 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2484 wcmd "search %d %d %d %d,%s\000"
2485 (btod conf.icase) pn py (btod forward) pattern;
2488 let intentry text key =
2489 let c =
2490 if key >= 32 && key < 127
2491 then Char.chr key
2492 else '\000'
2494 match c with
2495 | '0' .. '9' ->
2496 let text = addchar text c in
2497 TEcont text
2499 | _ ->
2500 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2501 TEcont text
2504 let linknentry text key =
2505 let c =
2506 if key >= 32 && key < 127
2507 then Char.chr key
2508 else '\000'
2510 match c with
2511 | 'a' .. 'z' ->
2512 let text = addchar text c in
2513 TEcont text
2515 | _ ->
2516 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2517 TEcont text
2520 let linkndone f s =
2521 if String.length s > 0
2522 then (
2523 let n =
2524 let l = String.length s in
2525 let rec loop pos n = if pos = l then n else
2526 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2527 loop (pos+1) (n*26 + m)
2528 in loop 0 0
2530 let rec loop n = function
2531 | [] -> ()
2532 | l :: rest ->
2533 match getopaque l.pageno with
2534 | None -> loop n rest
2535 | Some opaque ->
2536 let m = getlinkcount opaque in
2537 if n < m
2538 then (
2539 let under = getlink opaque n in
2540 f under
2542 else loop (n-m) rest
2544 loop n state.layout;
2548 let textentry text key =
2549 if key land 0xff00 = 0xff00
2550 then TEcont text
2551 else TEcont (text ^ Wsi.toutf8 key)
2554 let reqlayout angle proportional =
2555 match state.throttle with
2556 | None ->
2557 if nogeomcmds state.geomcmds
2558 then state.anchor <- getanchor ();
2559 conf.angle <- angle mod 360;
2560 if conf.angle != 0
2561 then (
2562 match state.mode with
2563 | LinkNav _ -> state.mode <- View
2564 | _ -> ()
2566 conf.proportional <- proportional;
2567 invalidate "reqlayout"
2568 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2569 | _ -> ()
2572 let settrim trimmargins trimfuzz =
2573 if nogeomcmds state.geomcmds
2574 then state.anchor <- getanchor ();
2575 conf.trimmargins <- trimmargins;
2576 conf.trimfuzz <- trimfuzz;
2577 let x0, y0, x1, y1 = trimfuzz in
2578 invalidate "settrim"
2579 (fun () ->
2580 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2581 Hashtbl.iter (fun _ opaque ->
2582 wcmd "freepage %s" opaque;
2583 ) state.pagemap;
2584 Hashtbl.clear state.pagemap;
2587 let setzoom zoom =
2588 match state.throttle with
2589 | None ->
2590 let zoom = max 0.01 zoom in
2591 if zoom <> conf.zoom
2592 then (
2593 state.prevzoom <- conf.zoom;
2594 conf.zoom <- zoom;
2595 reshape conf.winw conf.winh;
2596 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2599 | Some (layout, y, started) ->
2600 let time =
2601 match conf.maxwait with
2602 | None -> 0.0
2603 | Some t -> t
2605 let dt = now () -. started in
2606 if dt > time
2607 then (
2608 state.y <- y;
2609 load layout;
2613 let setcolumns mode columns coverA coverB =
2614 state.prevcolumns <- Some (conf.columns, conf.zoom);
2615 if columns < 0
2616 then (
2617 if isbirdseye mode
2618 then showtext '!' "split mode doesn't work in bird's eye"
2619 else (
2620 conf.columns <- Csplit (-columns, [||]);
2621 state.x <- 0;
2622 conf.zoom <- 1.0;
2625 else (
2626 if columns < 2
2627 then (
2628 conf.columns <- Csingle [||];
2629 state.x <- 0;
2630 setzoom 1.0;
2632 else (
2633 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2634 conf.zoom <- 1.0;
2637 reshape conf.winw conf.winh;
2640 let enterbirdseye () =
2641 let zoom = float conf.thumbw /. float conf.winw in
2642 let birdseyepageno =
2643 let cy = conf.winh / 2 in
2644 let fold = function
2645 | [] -> 0
2646 | l :: rest ->
2647 let rec fold best = function
2648 | [] -> best.pageno
2649 | l :: rest ->
2650 let d = cy - (l.pagedispy + l.pagevh/2)
2651 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2652 if abs d < abs dbest
2653 then fold l rest
2654 else best.pageno
2655 in fold l rest
2657 fold state.layout
2659 state.mode <- Birdseye (
2660 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2662 conf.zoom <- zoom;
2663 conf.presentation <- false;
2664 conf.interpagespace <- 10;
2665 conf.hlinks <- false;
2666 state.x <- 0;
2667 state.mstate <- Mnone;
2668 conf.maxwait <- None;
2669 conf.columns <- (
2670 match conf.beyecolumns with
2671 | Some c ->
2672 conf.zoom <- 1.0;
2673 Cmulti ((c, 0, 0), [||])
2674 | None -> Csingle [||]
2676 Wsi.setcursor Wsi.CURSOR_INHERIT;
2677 if conf.verbose
2678 then
2679 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2680 (100.0*.zoom)
2681 else
2682 state.text <- ""
2684 reshape conf.winw conf.winh;
2687 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2688 state.mode <- View;
2689 conf.zoom <- c.zoom;
2690 conf.presentation <- c.presentation;
2691 conf.interpagespace <- c.interpagespace;
2692 conf.maxwait <- c.maxwait;
2693 conf.hlinks <- c.hlinks;
2694 conf.beyecolumns <- (
2695 match conf.columns with
2696 | Cmulti ((c, _, _), _) -> Some c
2697 | Csingle _ -> None
2698 | Csplit _ -> failwith "leaving bird's eye split mode"
2700 conf.columns <- (
2701 match c.columns with
2702 | Cmulti (c, _) -> Cmulti (c, [||])
2703 | Csingle _ -> Csingle [||]
2704 | Csplit (c, _) -> Csplit (c, [||])
2706 state.x <- leftx;
2707 if conf.verbose
2708 then
2709 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2710 (100.0*.conf.zoom)
2712 reshape conf.winw conf.winh;
2713 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2716 let togglebirdseye () =
2717 match state.mode with
2718 | Birdseye vals -> leavebirdseye vals true
2719 | View -> enterbirdseye ()
2720 | _ -> ()
2723 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2724 let pageno = max 0 (pageno - incr) in
2725 let rec loop = function
2726 | [] -> gotopage1 pageno 0
2727 | l :: _ when l.pageno = pageno ->
2728 if l.pagedispy >= 0 && l.pagey = 0
2729 then G.postRedisplay "upbirdseye"
2730 else gotopage1 pageno 0
2731 | _ :: rest -> loop rest
2733 loop state.layout;
2734 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2737 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2738 let pageno = min (state.pagecount - 1) (pageno + incr) in
2739 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2740 let rec loop = function
2741 | [] ->
2742 let y, h = getpageyh pageno in
2743 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2744 gotoy (clamp dy)
2745 | l :: _ when l.pageno = pageno ->
2746 if l.pagevh != l.pageh
2747 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2748 else G.postRedisplay "downbirdseye"
2749 | _ :: rest -> loop rest
2751 loop state.layout
2754 let optentry mode _ key =
2755 let btos b = if b then "on" else "off" in
2756 if key >= 32 && key < 127
2757 then
2758 let c = Char.chr key in
2759 match c with
2760 | 's' ->
2761 let ondone s =
2762 try conf.scrollstep <- int_of_string s with exc ->
2763 state.text <- Printf.sprintf "bad integer `%s': %s"
2764 s (Printexc.to_string exc)
2766 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2768 | 'A' ->
2769 let ondone s =
2771 conf.autoscrollstep <- int_of_string s;
2772 if state.autoscroll <> None
2773 then state.autoscroll <- Some conf.autoscrollstep
2774 with exc ->
2775 state.text <- Printf.sprintf "bad integer `%s': %s"
2776 s (Printexc.to_string exc)
2778 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2780 | 'C' ->
2781 let ondone s =
2783 let n, a, b = multicolumns_of_string s in
2784 setcolumns mode n a b;
2785 with exc ->
2786 state.text <- Printf.sprintf "bad columns `%s': %s"
2787 s (Printexc.to_string exc)
2789 TEswitch ("columns: ", "", None, textentry, ondone, true)
2791 | 'Z' ->
2792 let ondone s =
2794 let zoom = float (int_of_string s) /. 100.0 in
2795 setzoom zoom
2796 with exc ->
2797 state.text <- Printf.sprintf "bad integer `%s': %s"
2798 s (Printexc.to_string exc)
2800 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2802 | 't' ->
2803 let ondone s =
2805 conf.thumbw <- bound (int_of_string s) 2 4096;
2806 state.text <-
2807 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2808 begin match mode with
2809 | Birdseye beye ->
2810 leavebirdseye beye false;
2811 enterbirdseye ();
2812 | _ -> ();
2814 with exc ->
2815 state.text <- Printf.sprintf "bad integer `%s': %s"
2816 s (Printexc.to_string exc)
2818 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2820 | 'R' ->
2821 let ondone s =
2822 match try
2823 Some (int_of_string s)
2824 with exc ->
2825 state.text <- Printf.sprintf "bad integer `%s': %s"
2826 s (Printexc.to_string exc);
2827 None
2828 with
2829 | Some angle -> reqlayout angle conf.proportional
2830 | None -> ()
2832 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2834 | 'i' ->
2835 conf.icase <- not conf.icase;
2836 TEdone ("case insensitive search " ^ (btos conf.icase))
2838 | 'p' ->
2839 conf.preload <- not conf.preload;
2840 gotoy state.y;
2841 TEdone ("preload " ^ (btos conf.preload))
2843 | 'v' ->
2844 conf.verbose <- not conf.verbose;
2845 TEdone ("verbose " ^ (btos conf.verbose))
2847 | 'd' ->
2848 conf.debug <- not conf.debug;
2849 TEdone ("debug " ^ (btos conf.debug))
2851 | 'h' ->
2852 conf.maxhfit <- not conf.maxhfit;
2853 state.maxy <- calcheight ();
2854 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2856 | 'c' ->
2857 conf.crophack <- not conf.crophack;
2858 TEdone ("crophack " ^ btos conf.crophack)
2860 | 'a' ->
2861 let s =
2862 match conf.maxwait with
2863 | None ->
2864 conf.maxwait <- Some infinity;
2865 "always wait for page to complete"
2866 | Some _ ->
2867 conf.maxwait <- None;
2868 "show placeholder if page is not ready"
2870 TEdone s
2872 | 'f' ->
2873 conf.underinfo <- not conf.underinfo;
2874 TEdone ("underinfo " ^ btos conf.underinfo)
2876 | 'P' ->
2877 conf.savebmarks <- not conf.savebmarks;
2878 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2880 | 'S' ->
2881 let ondone s =
2883 let pageno, py =
2884 match state.layout with
2885 | [] -> 0, 0
2886 | l :: _ ->
2887 l.pageno, l.pagey
2889 conf.interpagespace <- int_of_string s;
2890 docolumns conf.columns;
2891 state.maxy <- calcheight ();
2892 let y = getpagey pageno in
2893 gotoy (y + py)
2894 with exc ->
2895 state.text <- Printf.sprintf "bad integer `%s': %s"
2896 s (Printexc.to_string exc)
2898 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2900 | 'l' ->
2901 reqlayout conf.angle (not conf.proportional);
2902 TEdone ("proportional display " ^ btos conf.proportional)
2904 | 'T' ->
2905 settrim (not conf.trimmargins) conf.trimfuzz;
2906 TEdone ("trim margins " ^ btos conf.trimmargins)
2908 | 'I' ->
2909 conf.invert <- not conf.invert;
2910 TEdone ("invert colors " ^ btos conf.invert)
2912 | 'x' ->
2913 let ondone s =
2914 cbput state.hists.sel s;
2915 conf.selcmd <- s;
2917 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2918 textentry, ondone, true)
2920 | _ ->
2921 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2922 TEstop
2923 else
2924 TEcont state.text
2927 class type lvsource = object
2928 method getitemcount : int
2929 method getitem : int -> (string * int)
2930 method hasaction : int -> bool
2931 method exit :
2932 uioh:uioh ->
2933 cancel:bool ->
2934 active:int ->
2935 first:int ->
2936 pan:int ->
2937 qsearch:string ->
2938 uioh option
2939 method getactive : int
2940 method getfirst : int
2941 method getqsearch : string
2942 method setqsearch : string -> unit
2943 method getpan : int
2944 end;;
2946 class virtual lvsourcebase = object
2947 val mutable m_active = 0
2948 val mutable m_first = 0
2949 val mutable m_qsearch = ""
2950 val mutable m_pan = 0
2951 method getactive = m_active
2952 method getfirst = m_first
2953 method getqsearch = m_qsearch
2954 method getpan = m_pan
2955 method setqsearch s = m_qsearch <- s
2956 end;;
2958 let withoutlastutf8 s =
2959 let len = String.length s in
2960 if len = 0
2961 then s
2962 else
2963 let rec find pos =
2964 if pos = 0
2965 then pos
2966 else
2967 let b = Char.code s.[pos] in
2968 if b land 0b110000 = 0b11000000
2969 then find (pos-1)
2970 else pos-1
2972 let first =
2973 if Char.code s.[len-1] land 0x80 = 0
2974 then len-1
2975 else find (len-1)
2977 String.sub s 0 first;
2980 let textentrykeyboard
2981 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2982 let enttext te =
2983 state.mode <- Textentry (te, onleave);
2984 state.text <- "";
2985 enttext ();
2986 G.postRedisplay "textentrykeyboard enttext";
2988 let histaction cmd =
2989 match opthist with
2990 | None -> ()
2991 | Some (action, _) ->
2992 state.mode <- Textentry (
2993 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2995 G.postRedisplay "textentry histaction"
2997 match key with
2998 | 0xff08 -> (* backspace *)
2999 let s = withoutlastutf8 text in
3000 let len = String.length s in
3001 if cancelonempty && len = 0
3002 then (
3003 onleave Cancel;
3004 G.postRedisplay "textentrykeyboard after cancel";
3006 else (
3007 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3010 | 0xff0d ->
3011 ondone text;
3012 onleave Confirm;
3013 G.postRedisplay "textentrykeyboard after confirm"
3015 | 0xff52 -> histaction HCprev
3016 | 0xff54 -> histaction HCnext
3017 | 0xff50 -> histaction HCfirst
3018 | 0xff57 -> histaction HClast
3020 | 0xff1b -> (* escape*)
3021 if String.length text = 0
3022 then (
3023 begin match opthist with
3024 | None -> ()
3025 | Some (_, onhistcancel) -> onhistcancel ()
3026 end;
3027 onleave Cancel;
3028 state.text <- "";
3029 G.postRedisplay "textentrykeyboard after cancel2"
3031 else (
3032 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3035 | 0xff9f | 0xffff -> () (* delete *)
3037 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3038 begin match onkey text key with
3039 | TEdone text ->
3040 ondone text;
3041 onleave Confirm;
3042 G.postRedisplay "textentrykeyboard after confirm2";
3044 | TEcont text ->
3045 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3047 | TEstop ->
3048 onleave Cancel;
3049 G.postRedisplay "textentrykeyboard after cancel3"
3051 | TEswitch te ->
3052 state.mode <- Textentry (te, onleave);
3053 G.postRedisplay "textentrykeyboard switch";
3054 end;
3056 | _ ->
3057 vlog "unhandled key %s" (Wsi.keyname key)
3060 let firstof first active =
3061 if first > active || abs (first - active) > fstate.maxrows - 1
3062 then max 0 (active - (fstate.maxrows/2))
3063 else first
3066 let calcfirst first active =
3067 if active > first
3068 then
3069 let rows = active - first in
3070 if rows > fstate.maxrows then active - fstate.maxrows else first
3071 else active
3074 let scrollph y maxy =
3075 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3076 let sh = float conf.winh /. sh in
3077 let sh = max sh (float conf.scrollh) in
3079 let percent =
3080 if y = state.maxy
3081 then 1.0
3082 else float y /. float maxy
3084 let position = (float conf.winh -. sh) *. percent in
3086 let position =
3087 if position +. sh > float conf.winh
3088 then float conf.winh -. sh
3089 else position
3091 position, sh;
3094 let coe s = (s :> uioh);;
3096 class listview ~(source:lvsource) ~trusted ~modehash =
3097 object (self)
3098 val m_pan = source#getpan
3099 val m_first = source#getfirst
3100 val m_active = source#getactive
3101 val m_qsearch = source#getqsearch
3102 val m_prev_uioh = state.uioh
3104 method private elemunder y =
3105 let n = y / (fstate.fontsize+1) in
3106 if m_first + n < source#getitemcount
3107 then (
3108 if source#hasaction (m_first + n)
3109 then Some (m_first + n)
3110 else None
3112 else None
3114 method display =
3115 Gl.enable `blend;
3116 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3117 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3118 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3119 GlDraw.color (1., 1., 1.);
3120 Gl.enable `texture_2d;
3121 let fs = fstate.fontsize in
3122 let nfs = fs + 1 in
3123 let ww = fstate.wwidth in
3124 let tabw = 30.0*.ww in
3125 let itemcount = source#getitemcount in
3126 let rec loop row =
3127 if (row - m_first) * nfs > conf.winh
3128 then ()
3129 else (
3130 if row >= 0 && row < itemcount
3131 then (
3132 let (s, level) = source#getitem row in
3133 let y = (row - m_first) * nfs in
3134 let x = 5.0 +. float (level + m_pan) *. ww in
3135 if row = m_active
3136 then (
3137 Gl.disable `texture_2d;
3138 GlDraw.polygon_mode `both `line;
3139 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3140 GlDraw.rect (1., float (y + 1))
3141 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3142 GlDraw.polygon_mode `both `fill;
3143 GlDraw.color (1., 1., 1.);
3144 Gl.enable `texture_2d;
3147 let drawtabularstring s =
3148 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3149 if trusted
3150 then
3151 let tabpos = try String.index s '\t' with Not_found -> -1 in
3152 if tabpos > 0
3153 then
3154 let len = String.length s - tabpos - 1 in
3155 let s1 = String.sub s 0 tabpos
3156 and s2 = String.sub s (tabpos + 1) len in
3157 let nx = drawstr x s1 in
3158 let sw = nx -. x in
3159 let x = x +. (max tabw sw) in
3160 drawstr x s2
3161 else
3162 drawstr x s
3163 else
3164 drawstr x s
3166 let _ = drawtabularstring s in
3167 loop (row+1)
3171 loop m_first;
3172 Gl.disable `blend;
3173 Gl.disable `texture_2d;
3175 method updownlevel incr =
3176 let len = source#getitemcount in
3177 let curlevel =
3178 if m_active >= 0 && m_active < len
3179 then snd (source#getitem m_active)
3180 else -1
3182 let rec flow i =
3183 if i = len then i-1 else if i = -1 then 0 else
3184 let _, l = source#getitem i in
3185 if l != curlevel then i else flow (i+incr)
3187 let active = flow m_active in
3188 let first = calcfirst m_first active in
3189 G.postRedisplay "outline updownlevel";
3190 {< m_active = active; m_first = first >}
3192 method private key1 key mask =
3193 let set1 active first qsearch =
3194 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3196 let search active pattern incr =
3197 let dosearch re =
3198 let rec loop n =
3199 if n >= 0 && n < source#getitemcount
3200 then (
3201 let s, _ = source#getitem n in
3203 (try ignore (Str.search_forward re s 0); true
3204 with Not_found -> false)
3205 then Some n
3206 else loop (n + incr)
3208 else None
3210 loop active
3213 let re = Str.regexp_case_fold pattern in
3214 dosearch re
3215 with Failure s ->
3216 state.text <- s;
3217 None
3219 let itemcount = source#getitemcount in
3220 let find start incr =
3221 let rec find i =
3222 if i = -1 || i = itemcount
3223 then -1
3224 else (
3225 if source#hasaction i
3226 then i
3227 else find (i + incr)
3230 find start
3232 let set active first =
3233 let first = bound first 0 (itemcount - fstate.maxrows) in
3234 state.text <- "";
3235 coe {< m_active = active; m_first = first >}
3237 let navigate incr =
3238 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3239 let active, first =
3240 let incr1 = if incr > 0 then 1 else -1 in
3241 if isvisible m_first m_active
3242 then
3243 let next =
3244 let next = m_active + incr in
3245 let next =
3246 if next < 0 || next >= itemcount
3247 then -1
3248 else find next incr1
3250 if next = -1 || abs (m_active - next) > fstate.maxrows
3251 then -1
3252 else next
3254 if next = -1
3255 then
3256 let first = m_first + incr in
3257 let first = bound first 0 (itemcount - 1) in
3258 let next =
3259 let next = m_active + incr in
3260 let next = bound next 0 (itemcount - 1) in
3261 find next ~-incr1
3263 let active = if next = -1 then m_active else next in
3264 active, first
3265 else
3266 let first = min next m_first in
3267 let first =
3268 if abs (next - first) > fstate.maxrows
3269 then first + incr
3270 else first
3272 next, first
3273 else
3274 let first = m_first + incr in
3275 let first = bound first 0 (itemcount - 1) in
3276 let active =
3277 let next = m_active + incr in
3278 let next = bound next 0 (itemcount - 1) in
3279 let next = find next incr1 in
3280 let active =
3281 if next = -1 || abs (m_active - first) > fstate.maxrows
3282 then (
3283 let active = if m_active = -1 then next else m_active in
3284 active
3286 else next
3288 if isvisible first active
3289 then active
3290 else -1
3292 active, first
3294 G.postRedisplay "listview navigate";
3295 set active first;
3297 match key with
3298 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3299 let incr = if key = 0x72 then -1 else 1 in
3300 let active, first =
3301 match search (m_active + incr) m_qsearch incr with
3302 | None ->
3303 state.text <- m_qsearch ^ " [not found]";
3304 m_active, m_first
3305 | Some active ->
3306 state.text <- m_qsearch;
3307 active, firstof m_first active
3309 G.postRedisplay "listview ctrl-r/s";
3310 set1 active first m_qsearch;
3312 | 0xff08 -> (* backspace *)
3313 if String.length m_qsearch = 0
3314 then coe self
3315 else (
3316 let qsearch = withoutlastutf8 m_qsearch in
3317 let len = String.length qsearch in
3318 if len = 0
3319 then (
3320 state.text <- "";
3321 G.postRedisplay "listview empty qsearch";
3322 set1 m_active m_first "";
3324 else
3325 let active, first =
3326 match search m_active qsearch ~-1 with
3327 | None ->
3328 state.text <- qsearch ^ " [not found]";
3329 m_active, m_first
3330 | Some active ->
3331 state.text <- qsearch;
3332 active, firstof m_first active
3334 G.postRedisplay "listview backspace qsearch";
3335 set1 active first qsearch
3338 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3339 let pattern = m_qsearch ^ Wsi.toutf8 key in
3340 let active, first =
3341 match search m_active pattern 1 with
3342 | None ->
3343 state.text <- pattern ^ " [not found]";
3344 m_active, m_first
3345 | Some active ->
3346 state.text <- pattern;
3347 active, firstof m_first active
3349 G.postRedisplay "listview qsearch add";
3350 set1 active first pattern;
3352 | 0xff1b -> (* escape *)
3353 state.text <- "";
3354 if String.length m_qsearch = 0
3355 then (
3356 G.postRedisplay "list view escape";
3357 begin
3358 match
3359 source#exit (coe self) true m_active m_first m_pan m_qsearch
3360 with
3361 | None -> m_prev_uioh
3362 | Some uioh -> uioh
3365 else (
3366 G.postRedisplay "list view kill qsearch";
3367 source#setqsearch "";
3368 coe {< m_qsearch = "" >}
3371 | 0xff0d -> (* return *)
3372 state.text <- "";
3373 let self = {< m_qsearch = "" >} in
3374 source#setqsearch "";
3375 let opt =
3376 G.postRedisplay "listview enter";
3377 if m_active >= 0 && m_active < source#getitemcount
3378 then (
3379 source#exit (coe self) false m_active m_first m_pan "";
3381 else (
3382 source#exit (coe self) true m_active m_first m_pan "";
3385 begin match opt with
3386 | None -> m_prev_uioh
3387 | Some uioh -> uioh
3390 | 0xff9f | 0xffff -> (* delete *)
3391 coe self
3393 | 0xff52 -> navigate ~-1 (* up *)
3394 | 0xff54 -> navigate 1 (* down *)
3395 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3396 | 0xff56 -> navigate fstate.maxrows (* next *)
3398 | 0xff53 -> (* right *)
3399 state.text <- "";
3400 G.postRedisplay "listview right";
3401 coe {< m_pan = m_pan - 1 >}
3403 | 0xff51 -> (* left *)
3404 state.text <- "";
3405 G.postRedisplay "listview left";
3406 coe {< m_pan = m_pan + 1 >}
3408 | 0xff50 -> (* home *)
3409 let active = find 0 1 in
3410 G.postRedisplay "listview home";
3411 set active 0;
3413 | 0xff57 -> (* end *)
3414 let first = max 0 (itemcount - fstate.maxrows) in
3415 let active = find (itemcount - 1) ~-1 in
3416 G.postRedisplay "listview end";
3417 set active first;
3419 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3420 coe self
3422 | _ ->
3423 dolog "listview unknown key %#x" key; coe self
3425 method key key mask =
3426 match state.mode with
3427 | Textentry te -> textentrykeyboard key mask te; coe self
3428 | _ -> self#key1 key mask
3430 method button button down x y _ =
3431 let opt =
3432 match button with
3433 | 1 when x > conf.winw - conf.scrollbw ->
3434 G.postRedisplay "listview scroll";
3435 if down
3436 then
3437 let _, position, sh = self#scrollph in
3438 if y > truncate position && y < truncate (position +. sh)
3439 then (
3440 state.mstate <- Mscrolly;
3441 Some (coe self)
3443 else
3444 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3445 let first = truncate (s *. float source#getitemcount) in
3446 let first = min source#getitemcount first in
3447 Some (coe {< m_first = first; m_active = first >})
3448 else (
3449 state.mstate <- Mnone;
3450 Some (coe self);
3452 | 1 when not down ->
3453 begin match self#elemunder y with
3454 | Some n ->
3455 G.postRedisplay "listview click";
3456 source#exit
3457 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3458 | _ ->
3459 Some (coe self)
3461 | n when (n == 4 || n == 5) && not down ->
3462 let len = source#getitemcount in
3463 let first =
3464 if n = 5 && m_first + fstate.maxrows >= len
3465 then
3466 m_first
3467 else
3468 let first = m_first + (if n == 4 then -1 else 1) in
3469 bound first 0 (len - 1)
3471 G.postRedisplay "listview wheel";
3472 Some (coe {< m_first = first >})
3473 | n when (n = 6 || n = 7) && not down ->
3474 let inc = m_first + (if n = 7 then -1 else 1) in
3475 G.postRedisplay "listview hwheel";
3476 Some (coe {< m_pan = m_pan + inc >})
3477 | _ ->
3478 Some (coe self)
3480 match opt with
3481 | None -> m_prev_uioh
3482 | Some uioh -> uioh
3484 method motion _ y =
3485 match state.mstate with
3486 | Mscrolly ->
3487 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3488 let first = truncate (s *. float source#getitemcount) in
3489 let first = min source#getitemcount first in
3490 G.postRedisplay "listview motion";
3491 coe {< m_first = first; m_active = first >}
3492 | _ -> coe self
3494 method pmotion x y =
3495 if x < conf.winw - conf.scrollbw
3496 then
3497 let n =
3498 match self#elemunder y with
3499 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3500 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3502 let o =
3503 if n != m_active
3504 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3505 else self
3507 coe o
3508 else (
3509 Wsi.setcursor Wsi.CURSOR_INHERIT;
3510 coe self
3513 method infochanged _ = ()
3515 method scrollpw = (0, 0.0, 0.0)
3516 method scrollph =
3517 let nfs = fstate.fontsize + 1 in
3518 let y = m_first * nfs in
3519 let itemcount = source#getitemcount in
3520 let maxi = max 0 (itemcount - fstate.maxrows) in
3521 let maxy = maxi * nfs in
3522 let p, h = scrollph y maxy in
3523 conf.scrollbw, p, h
3525 method modehash = modehash
3526 end;;
3528 class outlinelistview ~source =
3529 object (self)
3530 inherit listview
3531 ~source:(source :> lvsource)
3532 ~trusted:false
3533 ~modehash:(findkeyhash conf "outline")
3534 as super
3536 method key key mask =
3537 let calcfirst first active =
3538 if active > first
3539 then
3540 let rows = active - first in
3541 let maxrows =
3542 if String.length state.text = 0
3543 then fstate.maxrows
3544 else fstate.maxrows - 2
3546 if rows > maxrows then active - maxrows else first
3547 else active
3549 let navigate incr =
3550 let active = m_active + incr in
3551 let active = bound active 0 (source#getitemcount - 1) in
3552 let first = calcfirst m_first active in
3553 G.postRedisplay "outline navigate";
3554 coe {< m_active = active; m_first = first >}
3556 let ctrl = Wsi.withctrl mask in
3557 match key with
3558 | 110 when ctrl -> (* ctrl-n *)
3559 source#narrow m_qsearch;
3560 G.postRedisplay "outline ctrl-n";
3561 coe {< m_first = 0; m_active = 0 >}
3563 | 117 when ctrl -> (* ctrl-u *)
3564 source#denarrow;
3565 G.postRedisplay "outline ctrl-u";
3566 state.text <- "";
3567 coe {< m_first = 0; m_active = 0 >}
3569 | 108 when ctrl -> (* ctrl-l *)
3570 let first = m_active - (fstate.maxrows / 2) in
3571 G.postRedisplay "outline ctrl-l";
3572 coe {< m_first = first >}
3574 | 0xff9f | 0xffff -> (* delete *)
3575 source#remove m_active;
3576 G.postRedisplay "outline delete";
3577 let active = max 0 (m_active-1) in
3578 coe {< m_first = firstof m_first active;
3579 m_active = active >}
3581 | 0xff52 -> navigate ~-1 (* up *)
3582 | 0xff54 -> navigate 1 (* down *)
3583 | 0xff55 -> (* prior *)
3584 navigate ~-(fstate.maxrows)
3585 | 0xff56 -> (* next *)
3586 navigate fstate.maxrows
3588 | 0xff53 -> (* [ctrl-]right *)
3589 let o =
3590 if ctrl
3591 then (
3592 G.postRedisplay "outline ctrl right";
3593 {< m_pan = m_pan + 1 >}
3595 else self#updownlevel 1
3597 coe o
3599 | 0xff51 -> (* [ctrl-]left *)
3600 let o =
3601 if ctrl
3602 then (
3603 G.postRedisplay "outline ctrl left";
3604 {< m_pan = m_pan - 1 >}
3606 else self#updownlevel ~-1
3608 coe o
3610 | 0xff50 -> (* home *)
3611 G.postRedisplay "outline home";
3612 coe {< m_first = 0; m_active = 0 >}
3614 | 0xff57 -> (* end *)
3615 let active = source#getitemcount - 1 in
3616 let first = max 0 (active - fstate.maxrows) in
3617 G.postRedisplay "outline end";
3618 coe {< m_active = active; m_first = first >}
3620 | _ -> super#key key mask
3623 let outlinesource usebookmarks =
3624 let empty = [||] in
3625 (object
3626 inherit lvsourcebase
3627 val mutable m_items = empty
3628 val mutable m_orig_items = empty
3629 val mutable m_prev_items = empty
3630 val mutable m_narrow_pattern = ""
3631 val mutable m_hadremovals = false
3633 method getitemcount =
3634 Array.length m_items + (if m_hadremovals then 1 else 0)
3636 method getitem n =
3637 if n == Array.length m_items && m_hadremovals
3638 then
3639 ("[Confirm removal]", 0)
3640 else
3641 let s, n, _ = m_items.(n) in
3642 (s, n)
3644 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3645 ignore (uioh, first, qsearch);
3646 let confrimremoval = m_hadremovals && active = Array.length m_items in
3647 let items =
3648 if String.length m_narrow_pattern = 0
3649 then m_orig_items
3650 else m_items
3652 if not cancel
3653 then (
3654 if not confrimremoval
3655 then(
3656 let _, _, anchor = m_items.(active) in
3657 gotoanchor anchor;
3658 m_items <- items;
3660 else (
3661 state.bookmarks <- Array.to_list m_items;
3662 m_orig_items <- m_items;
3665 else m_items <- items;
3666 m_pan <- pan;
3667 None
3669 method hasaction _ = true
3671 method greetmsg =
3672 if Array.length m_items != Array.length m_orig_items
3673 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3674 else ""
3676 method narrow pattern =
3677 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3678 match reopt with
3679 | None -> ()
3680 | Some re ->
3681 let rec loop accu n =
3682 if n = -1
3683 then (
3684 m_narrow_pattern <- pattern;
3685 m_items <- Array.of_list accu
3687 else
3688 let (s, _, _) as o = m_items.(n) in
3689 let accu =
3690 if (try ignore (Str.search_forward re s 0); true
3691 with Not_found -> false)
3692 then o :: accu
3693 else accu
3695 loop accu (n-1)
3697 loop [] (Array.length m_items - 1)
3699 method denarrow =
3700 m_orig_items <- (
3701 if usebookmarks
3702 then Array.of_list state.bookmarks
3703 else state.outlines
3705 m_items <- m_orig_items
3707 method remove m =
3708 if usebookmarks
3709 then
3710 if m >= 0 && m < Array.length m_items
3711 then (
3712 m_hadremovals <- true;
3713 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3714 let n = if n >= m then n+1 else n in
3715 m_items.(n)
3719 method reset anchor items =
3720 m_hadremovals <- false;
3721 if m_orig_items == empty || m_prev_items != items
3722 then (
3723 m_orig_items <- items;
3724 if String.length m_narrow_pattern = 0
3725 then m_items <- items;
3727 m_prev_items <- items;
3728 let rely = getanchory anchor in
3729 let active =
3730 let rec loop n best bestd =
3731 if n = Array.length m_items
3732 then best
3733 else
3734 let (_, _, anchor) = m_items.(n) in
3735 let orely = getanchory anchor in
3736 let d = abs (orely - rely) in
3737 if d < bestd
3738 then loop (n+1) n d
3739 else loop (n+1) best bestd
3741 loop 0 ~-1 max_int
3743 m_active <- active;
3744 m_first <- firstof m_first active
3745 end)
3748 let enterselector usebookmarks =
3749 let source = outlinesource usebookmarks in
3750 fun errmsg ->
3751 let outlines =
3752 if usebookmarks
3753 then Array.of_list state.bookmarks
3754 else state.outlines
3756 if Array.length outlines = 0
3757 then (
3758 showtext ' ' errmsg;
3760 else (
3761 state.text <- source#greetmsg;
3762 Wsi.setcursor Wsi.CURSOR_INHERIT;
3763 let anchor = getanchor () in
3764 source#reset anchor outlines;
3765 state.uioh <- coe (new outlinelistview ~source);
3766 G.postRedisplay "enter selector";
3770 let enteroutlinemode =
3771 let f = enterselector false in
3772 fun ()-> f "Document has no outline";
3775 let enterbookmarkmode =
3776 let f = enterselector true in
3777 fun () -> f "Document has no bookmarks (yet)";
3780 let color_of_string s =
3781 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3782 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3786 let color_to_string (r, g, b) =
3787 let r = truncate (r *. 256.0)
3788 and g = truncate (g *. 256.0)
3789 and b = truncate (b *. 256.0) in
3790 Printf.sprintf "%d/%d/%d" r g b
3793 let irect_of_string s =
3794 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3797 let irect_to_string (x0,y0,x1,y1) =
3798 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3801 let makecheckers () =
3802 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3803 following to say:
3804 converted by Issac Trotts. July 25, 2002 *)
3805 let image_height = 64
3806 and image_width = 64 in
3808 let make_image () =
3809 let image =
3810 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3812 for i = 0 to image_width - 1 do
3813 for j = 0 to image_height - 1 do
3814 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3815 (if (i land 8 ) lxor (j land 8) = 0
3816 then [|255;255;255|] else [|200;200;200|])
3817 done
3818 done;
3819 image
3821 let image = make_image () in
3822 let id = GlTex.gen_texture () in
3823 GlTex.bind_texture `texture_2d id;
3824 GlPix.store (`unpack_alignment 1);
3825 GlTex.image2d image;
3826 List.iter (GlTex.parameter ~target:`texture_2d)
3827 [ `wrap_s `repeat;
3828 `wrap_t `repeat;
3829 `mag_filter `nearest;
3830 `min_filter `nearest ];
3834 let setcheckers enabled =
3835 match state.texid with
3836 | None ->
3837 if enabled then state.texid <- Some (makecheckers ())
3839 | Some texid ->
3840 if not enabled
3841 then (
3842 GlTex.delete_texture texid;
3843 state.texid <- None;
3847 let int_of_string_with_suffix s =
3848 let l = String.length s in
3849 let s1, shift =
3850 if l > 1
3851 then
3852 let suffix = Char.lowercase s.[l-1] in
3853 match suffix with
3854 | 'k' -> String.sub s 0 (l-1), 10
3855 | 'm' -> String.sub s 0 (l-1), 20
3856 | 'g' -> String.sub s 0 (l-1), 30
3857 | _ -> s, 0
3858 else s, 0
3860 let n = int_of_string s1 in
3861 let m = n lsl shift in
3862 if m < 0 || m < n
3863 then raise (Failure "value too large")
3864 else m
3867 let string_with_suffix_of_int n =
3868 if n = 0
3869 then "0"
3870 else
3871 let n, s =
3872 if n land ((1 lsl 20) - 1) = 0
3873 then n lsr 20, "M"
3874 else (
3875 if n land ((1 lsl 10) - 1) = 0
3876 then n lsr 10, "K"
3877 else n, ""
3880 let rec loop s n =
3881 let h = n mod 1000 in
3882 let n = n / 1000 in
3883 if n = 0
3884 then string_of_int h ^ s
3885 else (
3886 let s = Printf.sprintf "_%03d%s" h s in
3887 loop s n
3890 loop "" n ^ s;
3893 let defghyllscroll = (40, 8, 32);;
3894 let ghyllscroll_of_string s =
3895 let (n, a, b) as nab =
3896 if s = "default"
3897 then defghyllscroll
3898 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3900 if n <= a || n <= b || a >= b
3901 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3902 nab;
3905 let ghyllscroll_to_string ((n, a, b) as nab) =
3906 if nab = defghyllscroll
3907 then "default"
3908 else Printf.sprintf "%d,%d,%d" n a b;
3911 let describe_location () =
3912 let f (fn, _) l =
3913 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3915 let fn, ln = List.fold_left f (-1, -1) state.layout in
3916 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3917 let percent =
3918 if maxy <= 0
3919 then 100.
3920 else (100. *. (float state.y /. float maxy))
3922 if fn = ln
3923 then
3924 Printf.sprintf "page %d of %d [%.2f%%]"
3925 (fn+1) state.pagecount percent
3926 else
3927 Printf.sprintf
3928 "pages %d-%d of %d [%.2f%%]"
3929 (fn+1) (ln+1) state.pagecount percent
3932 let setpresentationmode v =
3933 let (n, _, _) = getanchor () in
3934 let _, h = getpageyh n in
3935 let ips = if conf.presentation then calcips h else conf.interpagespace in
3936 state.anchor <- (n, 0.0, float ips);
3937 conf.presentation <- v;
3938 if conf.presentation
3939 then (
3940 if not conf.scrollbarinpm
3941 then state.scrollw <- 0;
3943 else state.scrollw <- conf.scrollbw;
3944 represent ();
3947 let enterinfomode =
3948 let btos b = if b then "\xe2\x88\x9a" else "" in
3949 let showextended = ref false in
3950 let leave mode = function
3951 | Confirm -> state.mode <- mode
3952 | Cancel -> state.mode <- mode in
3953 let src =
3954 (object
3955 val mutable m_first_time = true
3956 val mutable m_l = []
3957 val mutable m_a = [||]
3958 val mutable m_prev_uioh = nouioh
3959 val mutable m_prev_mode = View
3961 inherit lvsourcebase
3963 method reset prev_mode prev_uioh =
3964 m_a <- Array.of_list (List.rev m_l);
3965 m_l <- [];
3966 m_prev_mode <- prev_mode;
3967 m_prev_uioh <- prev_uioh;
3968 if m_first_time
3969 then (
3970 let rec loop n =
3971 if n >= Array.length m_a
3972 then ()
3973 else
3974 match m_a.(n) with
3975 | _, _, _, Action _ -> m_active <- n
3976 | _ -> loop (n+1)
3978 loop 0;
3979 m_first_time <- false;
3982 method int name get set =
3983 m_l <-
3984 (name, `int get, 1, Action (
3985 fun u ->
3986 let ondone s =
3987 try set (int_of_string s)
3988 with exn ->
3989 state.text <- Printf.sprintf "bad integer `%s': %s"
3990 s (Printexc.to_string exn)
3992 state.text <- "";
3993 let te = name ^ ": ", "", None, intentry, ondone, true in
3994 state.mode <- Textentry (te, leave m_prev_mode);
3996 )) :: m_l
3998 method int_with_suffix name get set =
3999 m_l <-
4000 (name, `intws get, 1, Action (
4001 fun u ->
4002 let ondone s =
4003 try set (int_of_string_with_suffix s)
4004 with exn ->
4005 state.text <- Printf.sprintf "bad integer `%s': %s"
4006 s (Printexc.to_string exn)
4008 state.text <- "";
4009 let te =
4010 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4012 state.mode <- Textentry (te, leave m_prev_mode);
4014 )) :: m_l
4016 method bool ?(offset=1) ?(btos=btos) name get set =
4017 m_l <-
4018 (name, `bool (btos, get), offset, Action (
4019 fun u ->
4020 let v = get () in
4021 set (not v);
4023 )) :: m_l
4025 method color name get set =
4026 m_l <-
4027 (name, `color get, 1, Action (
4028 fun u ->
4029 let invalid = (nan, nan, nan) in
4030 let ondone s =
4031 let c =
4032 try color_of_string s
4033 with exn ->
4034 state.text <- Printf.sprintf "bad color `%s': %s"
4035 s (Printexc.to_string exn);
4036 invalid
4038 if c <> invalid
4039 then set c;
4041 let te = name ^ ": ", "", None, textentry, ondone, true in
4042 state.text <- color_to_string (get ());
4043 state.mode <- Textentry (te, leave m_prev_mode);
4045 )) :: m_l
4047 method string name get set =
4048 m_l <-
4049 (name, `string get, 1, Action (
4050 fun u ->
4051 let ondone s = set s in
4052 let te = name ^ ": ", "", None, textentry, ondone, true in
4053 state.mode <- Textentry (te, leave m_prev_mode);
4055 )) :: m_l
4057 method colorspace name get set =
4058 m_l <-
4059 (name, `string get, 1, Action (
4060 fun _ ->
4061 let source =
4062 let vals = [| "rgb"; "bgr"; "gray" |] in
4063 (object
4064 inherit lvsourcebase
4066 initializer
4067 m_active <- int_of_colorspace conf.colorspace;
4068 m_first <- 0;
4070 method getitemcount = Array.length vals
4071 method getitem n = (vals.(n), 0)
4072 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4073 ignore (uioh, first, pan, qsearch);
4074 if not cancel then set active;
4075 None
4076 method hasaction _ = true
4077 end)
4079 state.text <- "";
4080 let modehash = findkeyhash conf "info" in
4081 coe (new listview ~source ~trusted:true ~modehash)
4082 )) :: m_l
4084 method caption s offset =
4085 m_l <- (s, `empty, offset, Noaction) :: m_l
4087 method caption2 s f offset =
4088 m_l <- (s, `string f, offset, Noaction) :: m_l
4090 method getitemcount = Array.length m_a
4092 method getitem n =
4093 let tostr = function
4094 | `int f -> string_of_int (f ())
4095 | `intws f -> string_with_suffix_of_int (f ())
4096 | `string f -> f ()
4097 | `color f -> color_to_string (f ())
4098 | `bool (btos, f) -> btos (f ())
4099 | `empty -> ""
4101 let name, t, offset, _ = m_a.(n) in
4102 ((let s = tostr t in
4103 if String.length s > 0
4104 then Printf.sprintf "%s\t%s" name s
4105 else name),
4106 offset)
4108 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4109 let uiohopt =
4110 if not cancel
4111 then (
4112 m_qsearch <- qsearch;
4113 let uioh =
4114 match m_a.(active) with
4115 | _, _, _, Action f -> f uioh
4116 | _ -> uioh
4118 Some uioh
4120 else None
4122 m_active <- active;
4123 m_first <- first;
4124 m_pan <- pan;
4125 uiohopt
4127 method hasaction n =
4128 match m_a.(n) with
4129 | _, _, _, Action _ -> true
4130 | _ -> false
4131 end)
4133 let rec fillsrc prevmode prevuioh =
4134 let sep () = src#caption "" 0 in
4135 let colorp name get set =
4136 src#string name
4137 (fun () -> color_to_string (get ()))
4138 (fun v ->
4140 let c = color_of_string v in
4141 set c
4142 with exn ->
4143 state.text <- Printf.sprintf "bad color `%s': %s"
4144 v (Printexc.to_string exn);
4147 let oldmode = state.mode in
4148 let birdseye = isbirdseye state.mode in
4150 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4152 src#bool "presentation mode"
4153 (fun () -> conf.presentation)
4154 (fun v -> setpresentationmode v);
4156 src#bool "ignore case in searches"
4157 (fun () -> conf.icase)
4158 (fun v -> conf.icase <- v);
4160 src#bool "preload"
4161 (fun () -> conf.preload)
4162 (fun v -> conf.preload <- v);
4164 src#bool "highlight links"
4165 (fun () -> conf.hlinks)
4166 (fun v -> conf.hlinks <- v);
4168 src#bool "under info"
4169 (fun () -> conf.underinfo)
4170 (fun v -> conf.underinfo <- v);
4172 src#bool "persistent bookmarks"
4173 (fun () -> conf.savebmarks)
4174 (fun v -> conf.savebmarks <- v);
4176 src#bool "proportional display"
4177 (fun () -> conf.proportional)
4178 (fun v -> reqlayout conf.angle v);
4180 src#bool "trim margins"
4181 (fun () -> conf.trimmargins)
4182 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4184 src#bool "persistent location"
4185 (fun () -> conf.jumpback)
4186 (fun v -> conf.jumpback <- v);
4188 sep ();
4189 src#int "inter-page space"
4190 (fun () -> conf.interpagespace)
4191 (fun n ->
4192 conf.interpagespace <- n;
4193 docolumns conf.columns;
4194 let pageno, py =
4195 match state.layout with
4196 | [] -> 0, 0
4197 | l :: _ ->
4198 l.pageno, l.pagey
4200 state.maxy <- calcheight ();
4201 let y = getpagey pageno in
4202 gotoy (y + py)
4205 src#int "page bias"
4206 (fun () -> conf.pagebias)
4207 (fun v -> conf.pagebias <- v);
4209 src#int "scroll step"
4210 (fun () -> conf.scrollstep)
4211 (fun n -> conf.scrollstep <- n);
4213 src#int "horizontal scroll step"
4214 (fun () -> conf.hscrollstep)
4215 (fun v -> conf.hscrollstep <- v);
4217 src#int "auto scroll step"
4218 (fun () ->
4219 match state.autoscroll with
4220 | Some step -> step
4221 | _ -> conf.autoscrollstep)
4222 (fun n ->
4223 if state.autoscroll <> None
4224 then state.autoscroll <- Some n;
4225 conf.autoscrollstep <- n);
4227 src#int "zoom"
4228 (fun () -> truncate (conf.zoom *. 100.))
4229 (fun v -> setzoom ((float v) /. 100.));
4231 src#int "rotation"
4232 (fun () -> conf.angle)
4233 (fun v -> reqlayout v conf.proportional);
4235 src#int "scroll bar width"
4236 (fun () -> state.scrollw)
4237 (fun v ->
4238 state.scrollw <- v;
4239 conf.scrollbw <- v;
4240 reshape conf.winw conf.winh;
4243 src#int "scroll handle height"
4244 (fun () -> conf.scrollh)
4245 (fun v -> conf.scrollh <- v;);
4247 src#int "thumbnail width"
4248 (fun () -> conf.thumbw)
4249 (fun v ->
4250 conf.thumbw <- min 4096 v;
4251 match oldmode with
4252 | Birdseye beye ->
4253 leavebirdseye beye false;
4254 enterbirdseye ()
4255 | _ -> ()
4258 let mode = state.mode in
4259 src#string "columns"
4260 (fun () ->
4261 match conf.columns with
4262 | Csingle _ -> "1"
4263 | Cmulti (multi, _) -> multicolumns_to_string multi
4264 | Csplit (count, _) -> "-" ^ string_of_int count
4266 (fun v ->
4267 let n, a, b = multicolumns_of_string v in
4268 setcolumns mode n a b);
4270 sep ();
4271 src#caption "Presentation mode" 0;
4272 src#bool "scrollbar visible"
4273 (fun () -> conf.scrollbarinpm)
4274 (fun v ->
4275 if v != conf.scrollbarinpm
4276 then (
4277 conf.scrollbarinpm <- v;
4278 if conf.presentation
4279 then (
4280 state.scrollw <- if v then conf.scrollbw else 0;
4281 reshape conf.winw conf.winh;
4286 sep ();
4287 src#caption "Pixmap cache" 0;
4288 src#int_with_suffix "size (advisory)"
4289 (fun () -> conf.memlimit)
4290 (fun v -> conf.memlimit <- v);
4292 src#caption2 "used"
4293 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4294 (string_with_suffix_of_int state.memused)
4295 (Hashtbl.length state.tilemap)) 1;
4297 sep ();
4298 src#caption "Layout" 0;
4299 src#caption2 "Dimension"
4300 (fun () ->
4301 Printf.sprintf "%dx%d (virtual %dx%d)"
4302 conf.winw conf.winh
4303 state.w state.maxy)
4305 if conf.debug
4306 then
4307 src#caption2 "Position" (fun () ->
4308 Printf.sprintf "%dx%d" state.x state.y
4310 else
4311 src#caption2 "Visible" (fun () -> describe_location ()) 1
4314 sep ();
4315 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4316 "Save these parameters as global defaults at exit"
4317 (fun () -> conf.bedefault)
4318 (fun v -> conf.bedefault <- v)
4321 sep ();
4322 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4323 src#bool ~offset:0 ~btos "Extended parameters"
4324 (fun () -> !showextended)
4325 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4326 if !showextended
4327 then (
4328 src#bool "checkers"
4329 (fun () -> conf.checkers)
4330 (fun v -> conf.checkers <- v; setcheckers v);
4331 src#bool "update cursor"
4332 (fun () -> conf.updatecurs)
4333 (fun v -> conf.updatecurs <- v);
4334 src#bool "verbose"
4335 (fun () -> conf.verbose)
4336 (fun v -> conf.verbose <- v);
4337 src#bool "invert colors"
4338 (fun () -> conf.invert)
4339 (fun v -> conf.invert <- v);
4340 src#bool "max fit"
4341 (fun () -> conf.maxhfit)
4342 (fun v -> conf.maxhfit <- v);
4343 src#bool "redirect stderr"
4344 (fun () -> conf.redirectstderr)
4345 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4346 src#string "uri launcher"
4347 (fun () -> conf.urilauncher)
4348 (fun v -> conf.urilauncher <- v);
4349 src#string "path launcher"
4350 (fun () -> conf.pathlauncher)
4351 (fun v -> conf.pathlauncher <- v);
4352 src#string "tile size"
4353 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4354 (fun v ->
4356 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4357 conf.tilew <- max 64 w;
4358 conf.tileh <- max 64 h;
4359 flushtiles ();
4360 with exn ->
4361 state.text <- Printf.sprintf "bad tile size `%s': %s"
4362 v (Printexc.to_string exn));
4363 src#int "texture count"
4364 (fun () -> conf.texcount)
4365 (fun v ->
4366 if realloctexts v
4367 then conf.texcount <- v
4368 else showtext '!' " Failed to set texture count please retry later"
4370 src#int "slice height"
4371 (fun () -> conf.sliceheight)
4372 (fun v ->
4373 conf.sliceheight <- v;
4374 wcmd "sliceh %d" conf.sliceheight;
4376 src#int "anti-aliasing level"
4377 (fun () -> conf.aalevel)
4378 (fun v ->
4379 conf.aalevel <- bound v 0 8;
4380 state.anchor <- getanchor ();
4381 opendoc state.path state.password;
4383 src#string "page scroll scaling factor"
4384 (fun () -> string_of_float conf.pgscale)
4385 (fun v ->
4387 let s = float_of_string v in
4388 conf.pgscale <- s
4389 with exn ->
4390 state.text <- Printf.sprintf
4391 "bad page scroll scaling factor `%s': %s"
4392 v (Printexc.to_string exn)
4395 src#int "ui font size"
4396 (fun () -> fstate.fontsize)
4397 (fun v -> setfontsize (bound v 5 100));
4398 src#int "hint font size"
4399 (fun () -> conf.hfsize)
4400 (fun v -> conf.hfsize <- bound v 5 100);
4401 colorp "background color"
4402 (fun () -> conf.bgcolor)
4403 (fun v -> conf.bgcolor <- v);
4404 src#bool "crop hack"
4405 (fun () -> conf.crophack)
4406 (fun v -> conf.crophack <- v);
4407 src#bool "multi column centering"
4408 (fun () -> conf.multicenter)
4409 (fun v -> conf.multicenter <- v; represent ());
4410 src#string "trim fuzz"
4411 (fun () -> irect_to_string conf.trimfuzz)
4412 (fun v ->
4414 conf.trimfuzz <- irect_of_string v;
4415 if conf.trimmargins
4416 then settrim true conf.trimfuzz;
4417 with exn ->
4418 state.text <- Printf.sprintf "bad irect `%s': %s"
4419 v (Printexc.to_string exn)
4421 src#string "throttle"
4422 (fun () ->
4423 match conf.maxwait with
4424 | None -> "show place holder if page is not ready"
4425 | Some time ->
4426 if time = infinity
4427 then "wait for page to fully render"
4428 else
4429 "wait " ^ string_of_float time
4430 ^ " seconds before showing placeholder"
4432 (fun v ->
4434 let f = float_of_string v in
4435 if f <= 0.0
4436 then conf.maxwait <- None
4437 else conf.maxwait <- Some f
4438 with exn ->
4439 state.text <- Printf.sprintf "bad time `%s': %s"
4440 v (Printexc.to_string exn)
4442 src#string "ghyll scroll"
4443 (fun () ->
4444 match conf.ghyllscroll with
4445 | None -> ""
4446 | Some nab -> ghyllscroll_to_string nab
4448 (fun v ->
4450 let gs =
4451 if String.length v = 0
4452 then None
4453 else Some (ghyllscroll_of_string v)
4455 conf.ghyllscroll <- gs
4456 with exn ->
4457 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4458 v (Printexc.to_string exn)
4460 src#string "selection command"
4461 (fun () -> conf.selcmd)
4462 (fun v -> conf.selcmd <- v);
4463 src#colorspace "color space"
4464 (fun () -> colorspace_to_string conf.colorspace)
4465 (fun v ->
4466 conf.colorspace <- colorspace_of_int v;
4467 wcmd "cs %d" v;
4468 load state.layout;
4472 sep ();
4473 src#caption "Document" 0;
4474 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4475 src#caption2 "Pages"
4476 (fun () -> string_of_int state.pagecount) 1;
4477 src#caption2 "Dimensions"
4478 (fun () -> string_of_int (List.length state.pdims)) 1;
4479 if conf.trimmargins
4480 then (
4481 sep ();
4482 src#caption "Trimmed margins" 0;
4483 src#caption2 "Dimensions"
4484 (fun () -> string_of_int (List.length state.pdims)) 1;
4487 sep ();
4488 src#caption "OpenGL" 0;
4489 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4490 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4491 src#reset prevmode prevuioh;
4493 fun () ->
4494 state.text <- "";
4495 let prevmode = state.mode
4496 and prevuioh = state.uioh in
4497 fillsrc prevmode prevuioh;
4498 let source = (src :> lvsource) in
4499 let modehash = findkeyhash conf "info" in
4500 state.uioh <- coe (object (self)
4501 inherit listview ~source ~trusted:true ~modehash as super
4502 val mutable m_prevmemused = 0
4503 method infochanged = function
4504 | Memused ->
4505 if m_prevmemused != state.memused
4506 then (
4507 m_prevmemused <- state.memused;
4508 G.postRedisplay "memusedchanged";
4510 | Pdim -> G.postRedisplay "pdimchanged"
4511 | Docinfo -> fillsrc prevmode prevuioh
4513 method key key mask =
4514 if not (Wsi.withctrl mask)
4515 then
4516 match key with
4517 | 0xff51 -> coe (self#updownlevel ~-1)
4518 | 0xff53 -> coe (self#updownlevel 1)
4519 | _ -> super#key key mask
4520 else super#key key mask
4521 end);
4522 G.postRedisplay "info";
4525 let enterhelpmode =
4526 let source =
4527 (object
4528 inherit lvsourcebase
4529 method getitemcount = Array.length state.help
4530 method getitem n =
4531 let s, n, _ = state.help.(n) in
4532 (s, n)
4534 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4535 let optuioh =
4536 if not cancel
4537 then (
4538 m_qsearch <- qsearch;
4539 match state.help.(active) with
4540 | _, _, Action f -> Some (f uioh)
4541 | _ -> Some (uioh)
4543 else None
4545 m_active <- active;
4546 m_first <- first;
4547 m_pan <- pan;
4548 optuioh
4550 method hasaction n =
4551 match state.help.(n) with
4552 | _, _, Action _ -> true
4553 | _ -> false
4555 initializer
4556 m_active <- -1
4557 end)
4558 in fun () ->
4559 let modehash = findkeyhash conf "help" in
4560 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4561 G.postRedisplay "help";
4564 let entermsgsmode =
4565 let msgsource =
4566 let re = Str.regexp "[\r\n]" in
4567 (object
4568 inherit lvsourcebase
4569 val mutable m_items = [||]
4571 method getitemcount = 1 + Array.length m_items
4573 method getitem n =
4574 if n = 0
4575 then "[Clear]", 0
4576 else m_items.(n-1), 0
4578 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4579 ignore uioh;
4580 if not cancel
4581 then (
4582 if active = 0
4583 then Buffer.clear state.errmsgs;
4584 m_qsearch <- qsearch;
4586 m_active <- active;
4587 m_first <- first;
4588 m_pan <- pan;
4589 None
4591 method hasaction n =
4592 n = 0
4594 method reset =
4595 state.newerrmsgs <- false;
4596 let l = Str.split re (Buffer.contents state.errmsgs) in
4597 m_items <- Array.of_list l
4599 initializer
4600 m_active <- 0
4601 end)
4602 in fun () ->
4603 state.text <- "";
4604 msgsource#reset;
4605 let source = (msgsource :> lvsource) in
4606 let modehash = findkeyhash conf "listview" in
4607 state.uioh <- coe (object
4608 inherit listview ~source ~trusted:false ~modehash as super
4609 method display =
4610 if state.newerrmsgs
4611 then msgsource#reset;
4612 super#display
4613 end);
4614 G.postRedisplay "msgs";
4617 let quickbookmark ?title () =
4618 match state.layout with
4619 | [] -> ()
4620 | l :: _ ->
4621 let title =
4622 match title with
4623 | None ->
4624 let sec = Unix.gettimeofday () in
4625 let tm = Unix.localtime sec in
4626 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4627 (l.pageno+1)
4628 tm.Unix.tm_mday
4629 tm.Unix.tm_mon
4630 (tm.Unix.tm_year + 1900)
4631 tm.Unix.tm_hour
4632 tm.Unix.tm_min
4633 | Some title -> title
4635 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4638 let doreshape w h =
4639 state.fullscreen <- None;
4640 Wsi.reshape w h;
4643 let setautoscrollspeed step goingdown =
4644 let incr = max 1 ((abs step) / 2) in
4645 let incr = if goingdown then incr else -incr in
4646 let astep = step + incr in
4647 state.autoscroll <- Some astep;
4650 let gotounder = function
4651 | Ulinkgoto (pageno, top) ->
4652 if pageno >= 0
4653 then (
4654 addnav ();
4655 gotopage1 pageno top;
4658 | Ulinkuri s ->
4659 gotouri s
4661 | Uremote (filename, pageno) ->
4662 let path =
4663 if Sys.file_exists filename
4664 then filename
4665 else
4666 let dir = Filename.dirname state.path in
4667 let path = Filename.concat dir filename in
4668 if Sys.file_exists path
4669 then path
4670 else ""
4672 if String.length path > 0
4673 then (
4674 let anchor = getanchor () in
4675 let ranchor = state.path, state.password, anchor in
4676 state.anchor <- (pageno, 0.0, 0.0);
4677 state.ranchors <- ranchor :: state.ranchors;
4678 opendoc path "";
4680 else showtext '!' ("Could not find " ^ filename)
4682 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4685 let canpan () =
4686 match conf.columns with
4687 | Csplit _ -> true
4688 | _ -> conf.zoom > 1.0
4691 let viewkeyboard key mask =
4692 let enttext te =
4693 let mode = state.mode in
4694 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4695 state.text <- "";
4696 enttext ();
4697 G.postRedisplay "view:enttext"
4699 let ctrl = Wsi.withctrl mask in
4700 match key with
4701 | 81 -> (* Q *)
4702 exit 0
4704 | 0xff63 -> (* insert *)
4705 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4706 then (
4707 state.mode <- LinkNav (Ltgendir 0);
4708 gotoy state.y;
4710 else showtext '!' "Keyboard link navigation does not work under rotation"
4712 | 0xff1b | 113 -> (* escape / q *)
4713 begin match state.mstate with
4714 | Mzoomrect _ ->
4715 state.mstate <- Mnone;
4716 Wsi.setcursor Wsi.CURSOR_INHERIT;
4717 G.postRedisplay "kill zoom rect";
4718 | _ ->
4719 match state.ranchors with
4720 | [] -> raise Quit
4721 | (path, password, anchor) :: rest ->
4722 state.ranchors <- rest;
4723 state.anchor <- anchor;
4724 opendoc path password
4725 end;
4727 | 0xff08 -> (* backspace *)
4728 let y = getnav ~-1 in
4729 gotoy_and_clear_text y
4731 | 111 -> (* o *)
4732 enteroutlinemode ()
4734 | 117 -> (* u *)
4735 state.rects <- [];
4736 state.text <- "";
4737 G.postRedisplay "dehighlight";
4739 | 47 | 63 -> (* / ? *)
4740 let ondone isforw s =
4741 cbput state.hists.pat s;
4742 state.searchpattern <- s;
4743 search s isforw
4745 let s = String.create 1 in
4746 s.[0] <- Char.chr key;
4747 enttext (s, "", Some (onhist state.hists.pat),
4748 textentry, ondone (key = 47), true)
4750 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4751 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4752 setzoom (conf.zoom +. incr)
4754 | 43 | 0xffab -> (* + *)
4755 let ondone s =
4756 let n =
4757 try int_of_string s with exc ->
4758 state.text <- Printf.sprintf "bad integer `%s': %s"
4759 s (Printexc.to_string exc);
4760 max_int
4762 if n != max_int
4763 then (
4764 conf.pagebias <- n;
4765 state.text <- "page bias is now " ^ string_of_int n;
4768 enttext ("page bias: ", "", None, intentry, ondone, true)
4770 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4771 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4772 setzoom (max 0.01 (conf.zoom -. decr))
4774 | 45 | 0xffad -> (* - *)
4775 let ondone msg = state.text <- msg in
4776 enttext (
4777 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4778 optentry state.mode, ondone, true
4781 | 48 when ctrl -> (* ctrl-0 *)
4782 setzoom 1.0
4784 | 49 when ctrl -> (* ctrl-1 *)
4785 let cols =
4786 match conf.columns with
4787 | Csingle _ | Cmulti _ -> 1
4788 | Csplit (n, _) -> n
4790 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4791 if zoom < 1.0
4792 then setzoom zoom
4794 | 0xffc6 -> (* f9 *)
4795 togglebirdseye ()
4797 | 57 when ctrl -> (* ctrl-9 *)
4798 togglebirdseye ()
4800 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4801 when not ctrl -> (* 0..9 *)
4802 let ondone s =
4803 let n =
4804 try int_of_string s with exc ->
4805 state.text <- Printf.sprintf "bad integer `%s': %s"
4806 s (Printexc.to_string exc);
4809 if n >= 0
4810 then (
4811 addnav ();
4812 cbput state.hists.pag (string_of_int n);
4813 gotopage1 (n + conf.pagebias - 1) 0;
4816 let pageentry text key =
4817 match Char.unsafe_chr key with
4818 | 'g' -> TEdone text
4819 | _ -> intentry text key
4821 let text = "x" in text.[0] <- Char.chr key;
4822 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4824 | 98 -> (* b *)
4825 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4826 reshape conf.winw conf.winh;
4828 | 108 -> (* l *)
4829 conf.hlinks <- not conf.hlinks;
4830 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4831 G.postRedisplay "toggle highlightlinks";
4833 | 70 -> (* F *)
4834 state.glinks <- true;
4835 let mode = state.mode in
4836 state.mode <- Textentry (
4837 (":", "", None, linknentry, linkndone (fun under ->
4838 addnav ();
4839 gotounder under
4840 ), false
4841 ), fun _ ->
4842 state.glinks <- false;
4843 state.mode <- mode
4845 state.text <- "";
4846 G.postRedisplay "view:linkent(F)"
4848 | 121 -> (* y *)
4849 state.glinks <- true;
4850 let mode = state.mode in
4851 state.mode <- Textentry (
4852 (":", "", None, linknentry, linkndone (fun under ->
4853 match Ne.pipe () with
4854 | Ne.Exn exn ->
4855 showtext '!' (Printf.sprintf "pipe failed: %s"
4856 (Printexc.to_string exn));
4857 | Ne.Res (r, w) ->
4858 let popened =
4859 try popen conf.selcmd [r, 0; w, -1]; true
4860 with exn ->
4861 showtext '!'
4862 (Printf.sprintf "failed to execute %s: %s"
4863 conf.selcmd (Printexc.to_string exn));
4864 false
4866 let clo cap fd =
4867 Ne.clo fd (fun msg ->
4868 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4871 let s = undertext under in
4872 if popened
4873 then
4874 (try
4875 let l = String.length s in
4876 let n = Unix.write w s 0 l in
4877 if n != l
4878 then
4879 showtext '!'
4880 (Printf.sprintf
4881 "failed to write %d characters to sel pipe, wrote %d"
4884 with exn ->
4885 showtext '!'
4886 (Printf.sprintf "failed to write to sel pipe: %s"
4887 (Printexc.to_string exn)
4890 else dolog "%s" s;
4891 clo "pipe/r" r;
4892 clo "pipe/w" w;
4893 ), false
4895 fun _ ->
4896 state.glinks <- false;
4897 state.mode <- mode
4899 state.text <- "";
4900 G.postRedisplay "view:linkent"
4902 | 97 -> (* a *)
4903 begin match state.autoscroll with
4904 | Some step ->
4905 conf.autoscrollstep <- step;
4906 state.autoscroll <- None
4907 | None ->
4908 if conf.autoscrollstep = 0
4909 then state.autoscroll <- Some 1
4910 else state.autoscroll <- Some conf.autoscrollstep
4913 | 112 when ctrl -> (* ctrl-p *)
4914 launchpath ()
4916 | 80 -> (* P *)
4917 setpresentationmode (not conf.presentation);
4918 showtext ' ' ("presentation mode " ^
4919 if conf.presentation then "on" else "off");
4921 | 102 -> (* f *)
4922 begin match state.fullscreen with
4923 | None ->
4924 state.fullscreen <- Some (conf.winw, conf.winh);
4925 Wsi.fullscreen ()
4926 | Some (w, h) ->
4927 state.fullscreen <- None;
4928 doreshape w h
4931 | 103 -> (* g *)
4932 gotoy_and_clear_text 0
4934 | 71 -> (* G *)
4935 gotopage1 (state.pagecount - 1) 0
4937 | 112 | 78 -> (* p|N *)
4938 search state.searchpattern false
4940 | 110 | 0xffc0 -> (* n|F3 *)
4941 search state.searchpattern true
4943 | 116 -> (* t *)
4944 begin match state.layout with
4945 | [] -> ()
4946 | l :: _ ->
4947 gotoy_and_clear_text (getpagey l.pageno)
4950 | 32 -> (* space *)
4951 begin match state.layout with
4952 | [] -> ()
4953 | l :: rest ->
4954 match conf.columns with
4955 | Csingle _ ->
4956 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4957 then
4958 let y = clamp (pgscale conf.winh) in
4959 gotoy_and_clear_text y
4960 else
4961 let pageno = min (l.pageno+1) (state.pagecount-1) in
4962 gotoy_and_clear_text (getpagey pageno)
4963 | Cmulti ((c, _, _), _) ->
4964 if conf.presentation && l.pageh > l.pagey + l.pagevh
4965 then
4966 let y = clamp (pgscale conf.winh) in
4967 gotoy_and_clear_text y
4968 else
4969 let pageno = min (l.pageno+c) (state.pagecount-1) in
4970 gotoy_and_clear_text (getpagey pageno)
4971 | Csplit (n, _) ->
4972 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4973 then
4974 let pagey, pageh = getpageyh l.pageno in
4975 let pagey = pagey + pageh * l.pagecol in
4976 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4977 gotoy_and_clear_text (pagey + pageh + ips)
4980 | 0xff9f | 0xffff -> (* delete *)
4981 begin match state.layout with
4982 | [] -> ()
4983 | l :: _ ->
4984 match conf.columns with
4985 | Csingle _ ->
4986 if conf.presentation && l.pagey != 0
4987 then
4988 gotoy_and_clear_text (clamp (pgscale ~-(conf.winh)))
4989 else
4990 let pageno = max 0 (l.pageno-1) in
4991 gotoy_and_clear_text (getpagey pageno)
4992 | Cmulti ((c, _, coverB), _) ->
4993 let decr =
4994 if l.pageno = state.pagecount - coverB
4995 then 1
4996 else c
4998 let pageno = max 0 (l.pageno-decr) in
4999 gotoy_and_clear_text (getpagey pageno)
5000 | Csplit (n, _) ->
5001 let y =
5002 if l.pagecol = 0
5003 then
5004 if l.pageno = 0
5005 then l.pagey
5006 else
5007 let pageno = max 0 (l.pageno-1) in
5008 let pagey, pageh = getpageyh pageno in
5009 pagey + (n-1)*pageh
5010 else
5011 let pagey, pageh = getpageyh l.pageno in
5012 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5014 gotoy_and_clear_text y
5017 | 61 -> (* = *)
5018 showtext ' ' (describe_location ());
5020 | 119 -> (* w *)
5021 begin match state.layout with
5022 | [] -> ()
5023 | l :: _ ->
5024 doreshape (l.pagew + state.scrollw) l.pageh;
5025 G.postRedisplay "w"
5028 | 39 -> (* ' *)
5029 enterbookmarkmode ()
5031 | 104 | 0xffbe -> (* h|F1 *)
5032 enterhelpmode ()
5034 | 105 -> (* i *)
5035 enterinfomode ()
5037 | 101 when conf.redirectstderr -> (* e *)
5038 entermsgsmode ()
5040 | 109 -> (* m *)
5041 let ondone s =
5042 match state.layout with
5043 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5044 | _ -> ()
5046 enttext ("bookmark: ", "", None, textentry, ondone, true)
5048 | 126 -> (* ~ *)
5049 quickbookmark ();
5050 showtext ' ' "Quick bookmark added";
5052 | 122 -> (* z *)
5053 begin match state.layout with
5054 | l :: _ ->
5055 let rect = getpdimrect l.pagedimno in
5056 let w, h =
5057 if conf.crophack
5058 then
5059 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5060 truncate (1.2 *. (rect.(3) -. rect.(0))))
5061 else
5062 (truncate (rect.(1) -. rect.(0)),
5063 truncate (rect.(3) -. rect.(0)))
5065 let w = truncate ((float w)*.conf.zoom)
5066 and h = truncate ((float h)*.conf.zoom) in
5067 if w != 0 && h != 0
5068 then (
5069 state.anchor <- getanchor ();
5070 doreshape (w + state.scrollw) (h + conf.interpagespace)
5072 G.postRedisplay "z";
5074 | [] -> ()
5077 | 50 when ctrl -> (* ctrl-2 *)
5078 let maxw = getmaxw () in
5079 if maxw > 0.0
5080 then setzoom (maxw /. float conf.winw)
5082 | 60 | 62 -> (* < > *)
5083 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5085 | 91 | 93 -> (* [ ] *)
5086 conf.colorscale <-
5087 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5089 G.postRedisplay "brightness";
5091 | 99 when state.mode = View -> (* c *)
5092 let (c, a, b), z =
5093 match state.prevcolumns with
5094 | None -> (1, 0, 0), 1.0
5095 | Some (columns, z) ->
5096 let cab =
5097 match columns with
5098 | Csplit (c, _) -> -c, 0, 0
5099 | Cmulti ((c, a, b), _) -> c, a, b
5100 | Csingle _ -> 1, 0, 0
5102 cab, z
5104 setcolumns View c a b;
5105 setzoom z;
5107 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5108 setzoom state.prevzoom
5110 | 107 | 0xff52 -> (* k up *)
5111 begin match state.autoscroll with
5112 | None ->
5113 begin match state.mode with
5114 | Birdseye beye -> upbirdseye 1 beye
5115 | _ ->
5116 if ctrl
5117 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5118 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5120 | Some n ->
5121 setautoscrollspeed n false
5124 | 106 | 0xff54 -> (* j down *)
5125 begin match state.autoscroll with
5126 | None ->
5127 begin match state.mode with
5128 | Birdseye beye -> downbirdseye 1 beye
5129 | _ ->
5130 if ctrl
5131 then gotoy_and_clear_text (clamp (conf.winh/2))
5132 else gotoy_and_clear_text (clamp conf.scrollstep)
5134 | Some n ->
5135 setautoscrollspeed n true
5138 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5139 if canpan ()
5140 then
5141 let dx =
5142 if ctrl
5143 then conf.winw / 2
5144 else 10
5146 let dx = if key = 0xff51 then dx else -dx in
5147 state.x <- state.x + dx;
5148 gotoy_and_clear_text state.y
5149 else (
5150 state.text <- "";
5151 G.postRedisplay "lef/right"
5154 | 0xff55 -> (* prior *)
5155 let y =
5156 if ctrl
5157 then
5158 match state.layout with
5159 | [] -> state.y
5160 | l :: _ -> state.y - l.pagey
5161 else
5162 clamp (pgscale (-conf.winh))
5164 gotoghyll y
5166 | 0xff56 -> (* next *)
5167 let y =
5168 if ctrl
5169 then
5170 match List.rev state.layout with
5171 | [] -> state.y
5172 | l :: _ -> getpagey l.pageno
5173 else
5174 clamp (pgscale conf.winh)
5176 gotoghyll y
5178 | 0xff50 -> (* home *)
5179 gotoghyll 0
5180 | 0xff57 -> (* end *)
5181 gotoghyll (clamp state.maxy)
5182 | 0xff53 when Wsi.withalt mask -> (* right *)
5183 gotoghyll (getnav ~-1)
5184 | 0xff51 when Wsi.withalt mask -> (* left *)
5185 gotoghyll (getnav 1)
5187 | 114 -> (* r *)
5188 state.anchor <- getanchor ();
5189 opendoc state.path state.password
5191 | 118 when conf.debug -> (* v *)
5192 state.rects <- [];
5193 List.iter (fun l ->
5194 match getopaque l.pageno with
5195 | None -> ()
5196 | Some opaque ->
5197 let x0, y0, x1, y1 = pagebbox opaque in
5198 let a,b = float x0, float y0 in
5199 let c,d = float x1, float y0 in
5200 let e,f = float x1, float y1 in
5201 let h,j = float x0, float y1 in
5202 let rect = (a,b,c,d,e,f,h,j) in
5203 debugrect rect;
5204 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5205 ) state.layout;
5206 G.postRedisplay "v";
5208 | _ ->
5209 vlog "huh? %s" (Wsi.keyname key)
5212 let linknavkeyboard key mask linknav =
5213 let getpage pageno =
5214 let rec loop = function
5215 | [] -> None
5216 | l :: _ when l.pageno = pageno -> Some l
5217 | _ :: rest -> loop rest
5218 in loop state.layout
5220 let doexact (pageno, n) =
5221 match getopaque pageno, getpage pageno with
5222 | Some opaque, Some l ->
5223 if key = 0xff0d
5224 then
5225 let under = getlink opaque n in
5226 G.postRedisplay "link gotounder";
5227 gotounder under;
5228 state.mode <- View;
5229 else
5230 let opt, dir =
5231 match key with
5232 | 0xff50 -> (* home *)
5233 Some (findlink opaque LDfirst), -1
5235 | 0xff57 -> (* end *)
5236 Some (findlink opaque LDlast), 1
5238 | 0xff51 -> (* left *)
5239 Some (findlink opaque (LDleft n)), -1
5241 | 0xff53 -> (* right *)
5242 Some (findlink opaque (LDright n)), 1
5244 | 0xff52 -> (* up *)
5245 Some (findlink opaque (LDup n)), -1
5247 | 0xff54 -> (* down *)
5248 Some (findlink opaque (LDdown n)), 1
5250 | _ -> None, 0
5252 let pwl l dir =
5253 begin match findpwl l.pageno dir with
5254 | Pwlnotfound -> ()
5255 | Pwl pageno ->
5256 let notfound dir =
5257 state.mode <- LinkNav (Ltgendir dir);
5258 let y, h = getpageyh pageno in
5259 let y =
5260 if dir < 0
5261 then y + h - conf.winh
5262 else y
5264 gotoy y
5266 begin match getopaque pageno, getpage pageno with
5267 | Some opaque, Some _ ->
5268 let link =
5269 let ld = if dir > 0 then LDfirst else LDlast in
5270 findlink opaque ld
5272 begin match link with
5273 | Lfound m ->
5274 showlinktype (getlink opaque m);
5275 state.mode <- LinkNav (Ltexact (pageno, m));
5276 G.postRedisplay "linknav jpage";
5277 | _ -> notfound dir
5278 end;
5279 | _ -> notfound dir
5280 end;
5281 end;
5283 begin match opt with
5284 | Some Lnotfound -> pwl l dir;
5285 | Some (Lfound m) ->
5286 if m = n
5287 then pwl l dir
5288 else (
5289 let _, y0, _, y1 = getlinkrect opaque m in
5290 if y0 < l.pagey
5291 then gotopage1 l.pageno y0
5292 else (
5293 let d = fstate.fontsize + 1 in
5294 if y1 - l.pagey > l.pagevh - d
5295 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5296 else G.postRedisplay "linknav";
5298 showlinktype (getlink opaque m);
5299 state.mode <- LinkNav (Ltexact (l.pageno, m));
5302 | None -> viewkeyboard key mask
5303 end;
5304 | _ -> viewkeyboard key mask
5306 if key = 0xff63
5307 then (
5308 state.mode <- View;
5309 G.postRedisplay "leave linknav"
5311 else
5312 match linknav with
5313 | Ltgendir _ -> viewkeyboard key mask
5314 | Ltexact exact -> doexact exact
5317 let keyboard key mask =
5318 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5319 then wcmd "interrupt"
5320 else state.uioh <- state.uioh#key key mask
5323 let birdseyekeyboard key mask
5324 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5325 let incr =
5326 match conf.columns with
5327 | Csingle _ -> 1
5328 | Cmulti ((c, _, _), _) -> c
5329 | Csplit _ -> failwith "bird's eye split mode"
5331 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5332 match key with
5333 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5334 let y, h = getpageyh pageno in
5335 let top = (conf.winh - h) / 2 in
5336 gotoy (max 0 (y - top))
5337 | 0xff0d -> leavebirdseye beye false
5338 | 0xff1b -> leavebirdseye beye true (* escape *)
5339 | 0xff52 -> upbirdseye incr beye (* up *)
5340 | 0xff54 -> downbirdseye incr beye (* down *)
5341 | 0xff51 -> upbirdseye 1 beye (* left *)
5342 | 0xff53 -> downbirdseye 1 beye (* right *)
5344 | 0xff55 -> (* prior *)
5345 begin match state.layout with
5346 | l :: _ ->
5347 if l.pagey != 0
5348 then (
5349 state.mode <- Birdseye (
5350 oconf, leftx, l.pageno, hooverpageno, anchor
5352 gotopage1 l.pageno 0;
5354 else (
5355 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5356 match layout with
5357 | [] -> gotoy (clamp (-conf.winh))
5358 | l :: _ ->
5359 state.mode <- Birdseye (
5360 oconf, leftx, l.pageno, hooverpageno, anchor
5362 gotopage1 l.pageno 0
5365 | [] -> gotoy (clamp (-conf.winh))
5366 end;
5368 | 0xff56 -> (* next *)
5369 begin match List.rev state.layout with
5370 | l :: _ ->
5371 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5372 begin match layout with
5373 | [] ->
5374 let incr = l.pageh - l.pagevh in
5375 if incr = 0
5376 then (
5377 state.mode <-
5378 Birdseye (
5379 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5381 G.postRedisplay "birdseye pagedown";
5383 else gotoy (clamp (incr + conf.interpagespace*2));
5385 | l :: _ ->
5386 state.mode <-
5387 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5388 gotopage1 l.pageno 0;
5391 | [] -> gotoy (clamp conf.winh)
5392 end;
5394 | 0xff50 -> (* home *)
5395 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5396 gotopage1 0 0
5398 | 0xff57 -> (* end *)
5399 let pageno = state.pagecount - 1 in
5400 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5401 if not (pagevisible state.layout pageno)
5402 then
5403 let h =
5404 match List.rev state.pdims with
5405 | [] -> conf.winh
5406 | (_, _, h, _) :: _ -> h
5408 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5409 else G.postRedisplay "birdseye end";
5410 | _ -> viewkeyboard key mask
5413 let drawpage l linkindexbase =
5414 let color =
5415 match state.mode with
5416 | Textentry _ -> scalecolor 0.4
5417 | LinkNav _
5418 | View -> scalecolor 1.0
5419 | Birdseye (_, _, pageno, hooverpageno, _) ->
5420 if l.pageno = hooverpageno
5421 then scalecolor 0.9
5422 else (
5423 if l.pageno = pageno
5424 then scalecolor 1.0
5425 else scalecolor 0.8
5428 drawtiles l color;
5429 begin match getopaque l.pageno with
5430 | Some opaque ->
5431 if tileready l l.pagex l.pagey
5432 then
5433 let x = l.pagedispx - l.pagex
5434 and y = l.pagedispy - l.pagey in
5435 let hlmask =
5436 match conf.columns with
5437 | Csingle _ | Cmulti _ ->
5438 (if conf.hlinks then 1 else 0)
5439 + (if state.glinks
5440 && not (isbirdseye state.mode) then 2 else 0)
5441 | _ -> 0
5443 let s =
5444 match state.mode with
5445 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5446 | _ -> ""
5448 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5449 else 0
5451 | _ -> 0
5452 end;
5455 let scrollindicator () =
5456 let sbw, ph, sh = state.uioh#scrollph in
5457 let sbh, pw, sw = state.uioh#scrollpw in
5459 GlDraw.color (0.64, 0.64, 0.64);
5460 GlDraw.rect
5461 (float (conf.winw - sbw), 0.)
5462 (float conf.winw, float conf.winh)
5464 GlDraw.rect
5465 (0., float (conf.winh - sbh))
5466 (float (conf.winw - state.scrollw - 1), float conf.winh)
5468 GlDraw.color (0.0, 0.0, 0.0);
5470 GlDraw.rect
5471 (float (conf.winw - sbw), ph)
5472 (float conf.winw, ph +. sh)
5474 GlDraw.rect
5475 (pw, float (conf.winh - sbh))
5476 (pw +. sw, float conf.winh)
5480 let showsel () =
5481 match state.mstate with
5482 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5485 | Msel ((x0, y0), (x1, y1)) ->
5486 let rec loop = function
5487 | l :: ls ->
5488 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5489 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5490 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5491 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5492 then
5493 match getopaque l.pageno with
5494 | Some opaque ->
5495 let x0, y0 = pagetranslatepoint l x0 y0 in
5496 let x1, y1 = pagetranslatepoint l x1 y1 in
5497 seltext opaque (x0, y0, x1, y1);
5498 | _ -> ()
5499 else loop ls
5500 | [] -> ()
5502 loop state.layout
5505 let showrects rects =
5506 Gl.enable `blend;
5507 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5508 GlDraw.polygon_mode `both `fill;
5509 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5510 List.iter
5511 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5512 List.iter (fun l ->
5513 if l.pageno = pageno
5514 then (
5515 let dx = float (l.pagedispx - l.pagex) in
5516 let dy = float (l.pagedispy - l.pagey) in
5517 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5518 GlDraw.begins `quads;
5520 GlDraw.vertex2 (x0+.dx, y0+.dy);
5521 GlDraw.vertex2 (x1+.dx, y1+.dy);
5522 GlDraw.vertex2 (x2+.dx, y2+.dy);
5523 GlDraw.vertex2 (x3+.dx, y3+.dy);
5525 GlDraw.ends ();
5527 ) state.layout
5528 ) rects
5530 Gl.disable `blend;
5533 let display () =
5534 GlClear.color (scalecolor2 conf.bgcolor);
5535 GlClear.clear [`color];
5536 let rec loop linkindexbase = function
5537 | l :: rest ->
5538 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5539 loop linkindexbase rest
5540 | [] -> ()
5542 loop 0 state.layout;
5543 let rects =
5544 match state.mode with
5545 | LinkNav (Ltexact (pageno, linkno)) ->
5546 begin match getopaque pageno with
5547 | Some opaque ->
5548 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5549 (pageno, 5, (
5550 float x0, float y0,
5551 float x1, float y0,
5552 float x1, float y1,
5553 float x0, float y1)
5554 ) :: state.rects
5555 | None -> state.rects
5557 | _ -> state.rects
5559 showrects rects;
5560 showsel ();
5561 state.uioh#display;
5562 begin match state.mstate with
5563 | Mzoomrect ((x0, y0), (x1, y1)) ->
5564 Gl.enable `blend;
5565 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5566 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5567 GlDraw.rect (float x0, float y0)
5568 (float x1, float y1);
5569 Gl.disable `blend;
5570 | _ -> ()
5571 end;
5572 enttext ();
5573 scrollindicator ();
5574 Wsi.swapb ();
5577 let zoomrect x y x1 y1 =
5578 let x0 = min x x1
5579 and x1 = max x x1
5580 and y0 = min y y1 in
5581 gotoy (state.y + y0);
5582 state.anchor <- getanchor ();
5583 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5584 let margin =
5585 if state.w < conf.winw - state.scrollw
5586 then (conf.winw - state.scrollw - state.w) / 2
5587 else 0
5589 state.x <- (state.x + margin) - x0;
5590 setzoom zoom;
5591 Wsi.setcursor Wsi.CURSOR_INHERIT;
5592 state.mstate <- Mnone;
5595 let scrollx x =
5596 let winw = conf.winw - state.scrollw - 1 in
5597 let s = float x /. float winw in
5598 let destx = truncate (float (state.w + winw) *. s) in
5599 state.x <- winw - destx;
5600 gotoy_and_clear_text state.y;
5601 state.mstate <- Mscrollx;
5604 let scrolly y =
5605 let s = float y /. float conf.winh in
5606 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5607 gotoy_and_clear_text desty;
5608 state.mstate <- Mscrolly;
5611 let viewmouse button down x y mask =
5612 match button with
5613 | n when (n == 4 || n == 5) && not down ->
5614 if Wsi.withctrl mask
5615 then (
5616 match state.mstate with
5617 | Mzoom (oldn, i) ->
5618 if oldn = n
5619 then (
5620 if i = 2
5621 then
5622 let incr =
5623 match n with
5624 | 5 ->
5625 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5626 | _ ->
5627 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5629 let zoom = conf.zoom -. incr in
5630 setzoom zoom;
5631 state.mstate <- Mzoom (n, 0);
5632 else
5633 state.mstate <- Mzoom (n, i+1);
5635 else state.mstate <- Mzoom (n, 0)
5637 | _ -> state.mstate <- Mzoom (n, 0)
5639 else (
5640 match state.autoscroll with
5641 | Some step -> setautoscrollspeed step (n=4)
5642 | None ->
5643 let incr =
5644 if n = 4
5645 then -conf.scrollstep
5646 else conf.scrollstep
5648 let incr = incr * 2 in
5649 let y = clamp incr in
5650 gotoy_and_clear_text y
5653 | n when (n = 6 || n = 7) && not down && canpan () ->
5654 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5655 gotoy_and_clear_text state.y
5657 | 1 when Wsi.withctrl mask ->
5658 if down
5659 then (
5660 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5661 state.mstate <- Mpan (x, y)
5663 else
5664 state.mstate <- Mnone
5666 | 3 ->
5667 if down
5668 then (
5669 Wsi.setcursor Wsi.CURSOR_CYCLE;
5670 let p = (x, y) in
5671 state.mstate <- Mzoomrect (p, p)
5673 else (
5674 match state.mstate with
5675 | Mzoomrect ((x0, y0), _) ->
5676 if abs (x-x0) > 10 && abs (y - y0) > 10
5677 then zoomrect x0 y0 x y
5678 else (
5679 state.mstate <- Mnone;
5680 Wsi.setcursor Wsi.CURSOR_INHERIT;
5681 G.postRedisplay "kill accidental zoom rect";
5683 | _ ->
5684 Wsi.setcursor Wsi.CURSOR_INHERIT;
5685 state.mstate <- Mnone
5688 | 1 when x > conf.winw - state.scrollw ->
5689 if down
5690 then
5691 let _, position, sh = state.uioh#scrollph in
5692 if y > truncate position && y < truncate (position +. sh)
5693 then state.mstate <- Mscrolly
5694 else scrolly y
5695 else
5696 state.mstate <- Mnone
5698 | 1 when y > conf.winh - state.hscrollh ->
5699 if down
5700 then
5701 let _, position, sw = state.uioh#scrollpw in
5702 if x > truncate position && x < truncate (position +. sw)
5703 then state.mstate <- Mscrollx
5704 else scrollx x
5705 else
5706 state.mstate <- Mnone
5708 | 1 ->
5709 let dest = if down then getunder x y else Unone in
5710 begin match dest with
5711 | Ulinkgoto _
5712 | Ulinkuri _
5713 | Uremote _
5714 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5715 gotounder dest
5717 | Unone when down ->
5718 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5719 state.mstate <- Mpan (x, y);
5721 | Unone | Utext _ ->
5722 if down
5723 then (
5724 if conf.angle mod 360 = 0
5725 then (
5726 state.mstate <- Msel ((x, y), (x, y));
5727 G.postRedisplay "mouse select";
5730 else (
5731 match state.mstate with
5732 | Mnone -> ()
5734 | Mzoom _ | Mscrollx | Mscrolly ->
5735 state.mstate <- Mnone
5737 | Mzoomrect ((x0, y0), _) ->
5738 zoomrect x0 y0 x y
5740 | Mpan _ ->
5741 Wsi.setcursor Wsi.CURSOR_INHERIT;
5742 state.mstate <- Mnone
5744 | Msel ((_, y0), (_, y1)) ->
5745 let rec loop = function
5746 | [] -> ()
5747 | l :: rest ->
5748 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5749 || ((y1 >= l.pagedispy
5750 && y1 <= (l.pagedispy + l.pagevh)))
5751 then
5752 match getopaque l.pageno with
5753 | Some opaque ->
5754 begin
5755 match Ne.pipe () with
5756 | Ne.Exn exn ->
5757 showtext '!'
5758 (Printf.sprintf
5759 "can not create sel pipe: %s"
5760 (Printexc.to_string exn));
5761 | Ne.Res (r, w) ->
5762 let doclose what fd =
5763 Ne.clo fd (fun msg ->
5764 dolog "%s close failed: %s" what msg)
5767 popen conf.selcmd [r, 0; w, -1];
5768 copysel w opaque;
5769 doclose "pipe/r" r;
5770 G.postRedisplay "copysel";
5771 with exn ->
5772 dolog "can not execute %S: %s"
5773 conf.selcmd (Printexc.to_string exn);
5774 doclose "pipe/r" r;
5775 doclose "pipe/w" w;
5777 | None -> ()
5778 else loop rest
5780 loop state.layout;
5781 Wsi.setcursor Wsi.CURSOR_INHERIT;
5782 state.mstate <- Mnone;
5786 | _ -> ()
5789 let birdseyemouse button down x y mask
5790 (conf, leftx, _, hooverpageno, anchor) =
5791 match button with
5792 | 1 when down ->
5793 let rec loop = function
5794 | [] -> ()
5795 | l :: rest ->
5796 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5797 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5798 then (
5799 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5801 else loop rest
5803 loop state.layout
5804 | 3 -> ()
5805 | _ -> viewmouse button down x y mask
5808 let mouse button down x y mask =
5809 state.uioh <- state.uioh#button button down x y mask;
5812 let motion ~x ~y =
5813 state.uioh <- state.uioh#motion x y
5816 let pmotion ~x ~y =
5817 state.uioh <- state.uioh#pmotion x y;
5820 let uioh = object
5821 method display = ()
5823 method key key mask =
5824 begin match state.mode with
5825 | Textentry textentry -> textentrykeyboard key mask textentry
5826 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5827 | View -> viewkeyboard key mask
5828 | LinkNav linknav -> linknavkeyboard key mask linknav
5829 end;
5830 state.uioh
5832 method button button bstate x y mask =
5833 begin match state.mode with
5834 | LinkNav _
5835 | View -> viewmouse button bstate x y mask
5836 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5837 | Textentry _ -> ()
5838 end;
5839 state.uioh
5841 method motion x y =
5842 begin match state.mode with
5843 | Textentry _ -> ()
5844 | View | Birdseye _ | LinkNav _ ->
5845 match state.mstate with
5846 | Mzoom _ | Mnone -> ()
5848 | Mpan (x0, y0) ->
5849 let dx = x - x0
5850 and dy = y0 - y in
5851 state.mstate <- Mpan (x, y);
5852 if canpan ()
5853 then state.x <- state.x + dx;
5854 let y = clamp dy in
5855 gotoy_and_clear_text y
5857 | Msel (a, _) ->
5858 state.mstate <- Msel (a, (x, y));
5859 G.postRedisplay "motion select";
5861 | Mscrolly ->
5862 let y = min conf.winh (max 0 y) in
5863 scrolly y
5865 | Mscrollx ->
5866 let x = min conf.winw (max 0 x) in
5867 scrollx x
5869 | Mzoomrect (p0, _) ->
5870 state.mstate <- Mzoomrect (p0, (x, y));
5871 G.postRedisplay "motion zoomrect";
5872 end;
5873 state.uioh
5875 method pmotion x y =
5876 begin match state.mode with
5877 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5878 let rec loop = function
5879 | [] ->
5880 if hooverpageno != -1
5881 then (
5882 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5883 G.postRedisplay "pmotion birdseye no hoover";
5885 | l :: rest ->
5886 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5887 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5888 then (
5889 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5890 G.postRedisplay "pmotion birdseye hoover";
5892 else loop rest
5894 loop state.layout
5896 | Textentry _ -> ()
5898 | LinkNav _
5899 | View ->
5900 match state.mstate with
5901 | Mnone -> updateunder x y
5902 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5904 end;
5905 state.uioh
5907 method infochanged _ = ()
5909 method scrollph =
5910 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5911 let p, h = scrollph state.y maxy in
5912 state.scrollw, p, h
5914 method scrollpw =
5915 let winw = conf.winw - state.scrollw - 1 in
5916 let fwinw = float winw in
5917 let sw =
5918 let sw = fwinw /. float state.w in
5919 let sw = fwinw *. sw in
5920 max sw (float conf.scrollh)
5922 let position, sw =
5923 let f = state.w+winw in
5924 let r = float (winw-state.x) /. float f in
5925 let p = fwinw *. r in
5926 p-.sw/.2., sw
5928 let sw =
5929 if position +. sw > fwinw
5930 then fwinw -. position
5931 else sw
5933 state.hscrollh, position, sw
5935 method modehash =
5936 let modename =
5937 match state.mode with
5938 | LinkNav _ -> "links"
5939 | Textentry _ -> "textentry"
5940 | Birdseye _ -> "birdseye"
5941 | View -> "view"
5943 findkeyhash conf modename
5944 end;;
5946 module Config =
5947 struct
5948 open Parser
5950 let fontpath = ref "";;
5952 module KeyMap =
5953 Map.Make (struct type t = (int * int) let compare = compare end);;
5955 let unent s =
5956 let l = String.length s in
5957 let b = Buffer.create l in
5958 unent b s 0 l;
5959 Buffer.contents b;
5962 let home =
5963 try Sys.getenv "HOME"
5964 with exn ->
5965 prerr_endline
5966 ("Can not determine home directory location: " ^
5967 Printexc.to_string exn);
5971 let modifier_of_string = function
5972 | "alt" -> Wsi.altmask
5973 | "shift" -> Wsi.shiftmask
5974 | "ctrl" | "control" -> Wsi.ctrlmask
5975 | "meta" -> Wsi.metamask
5976 | _ -> 0
5979 let key_of_string =
5980 let r = Str.regexp "-" in
5981 fun s ->
5982 let elems = Str.full_split r s in
5983 let f n k m =
5984 let g s =
5985 let m1 = modifier_of_string s in
5986 if m1 = 0
5987 then (Wsi.namekey s, m)
5988 else (k, m lor m1)
5989 in function
5990 | Str.Delim s when n land 1 = 0 -> g s
5991 | Str.Text s -> g s
5992 | Str.Delim _ -> (k, m)
5994 let rec loop n k m = function
5995 | [] -> (k, m)
5996 | x :: xs ->
5997 let k, m = f n k m x in
5998 loop (n+1) k m xs
6000 loop 0 0 0 elems
6003 let keys_of_string =
6004 let r = Str.regexp "[ \t]" in
6005 fun s ->
6006 let elems = Str.split r s in
6007 List.map key_of_string elems
6010 let copykeyhashes c =
6011 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6014 let config_of c attrs =
6015 let apply c k v =
6017 match k with
6018 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6019 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6020 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6021 | "preload" -> { c with preload = bool_of_string v }
6022 | "page-bias" -> { c with pagebias = int_of_string v }
6023 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6024 | "horizontal-scroll-step" ->
6025 { c with hscrollstep = max (int_of_string v) 1 }
6026 | "auto-scroll-step" ->
6027 { c with autoscrollstep = max 0 (int_of_string v) }
6028 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6029 | "crop-hack" -> { c with crophack = bool_of_string v }
6030 | "throttle" ->
6031 let mw =
6032 match String.lowercase v with
6033 | "true" -> Some infinity
6034 | "false" -> None
6035 | f -> Some (float_of_string f)
6037 { c with maxwait = mw}
6038 | "highlight-links" -> { c with hlinks = bool_of_string v }
6039 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6040 | "vertical-margin" ->
6041 { c with interpagespace = max 0 (int_of_string v) }
6042 | "zoom" ->
6043 let zoom = float_of_string v /. 100. in
6044 let zoom = max zoom 0.0 in
6045 { c with zoom = zoom }
6046 | "presentation" -> { c with presentation = bool_of_string v }
6047 | "rotation-angle" -> { c with angle = int_of_string v }
6048 | "width" -> { c with winw = max 20 (int_of_string v) }
6049 | "height" -> { c with winh = max 20 (int_of_string v) }
6050 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6051 | "proportional-display" -> { c with proportional = bool_of_string v }
6052 | "pixmap-cache-size" ->
6053 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6054 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6055 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6056 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6057 | "persistent-location" -> { c with jumpback = bool_of_string v }
6058 | "background-color" -> { c with bgcolor = color_of_string v }
6059 | "scrollbar-in-presentation" ->
6060 { c with scrollbarinpm = bool_of_string v }
6061 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6062 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6063 | "mupdf-store-size" ->
6064 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6065 | "checkers" -> { c with checkers = bool_of_string v }
6066 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6067 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6068 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6069 | "uri-launcher" -> { c with urilauncher = unent v }
6070 | "path-launcher" -> { c with pathlauncher = unent v }
6071 | "color-space" -> { c with colorspace = colorspace_of_string v }
6072 | "invert-colors" -> { c with invert = bool_of_string v }
6073 | "brightness" -> { c with colorscale = float_of_string v }
6074 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6075 | "ghyllscroll" ->
6076 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6077 | "columns" ->
6078 let (n, _, _) as nab = multicolumns_of_string v in
6079 if n < 0
6080 then { c with columns = Csplit (-n, [||]) }
6081 else { c with columns = Cmulti (nab, [||]) }
6082 | "birds-eye-columns" ->
6083 { c with beyecolumns = Some (max (int_of_string v) 2) }
6084 | "selection-command" -> { c with selcmd = unent v }
6085 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6086 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6087 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6088 | "multi-column-centering" -> { c with multicenter = bool_of_string v }
6089 | _ -> c
6090 with exn ->
6091 prerr_endline ("Error processing attribute (`" ^
6092 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6095 let rec fold c = function
6096 | [] -> c
6097 | (k, v) :: rest ->
6098 let c = apply c k v in
6099 fold c rest
6101 fold { c with keyhashes = copykeyhashes c } attrs;
6104 let fromstring f pos n v d =
6105 try f v
6106 with exn ->
6107 dolog "Error processing attribute (%S=%S) at %d\n%s"
6108 n v pos (Printexc.to_string exn)
6113 let bookmark_of attrs =
6114 let rec fold title page rely visy = function
6115 | ("title", v) :: rest -> fold v page rely visy rest
6116 | ("page", v) :: rest -> fold title v rely visy rest
6117 | ("rely", v) :: rest -> fold title page v visy rest
6118 | ("visy", v) :: rest -> fold title page rely v rest
6119 | _ :: rest -> fold title page rely visy rest
6120 | [] -> title, page, rely, visy
6122 fold "invalid" "0" "0" "0" attrs
6125 let doc_of attrs =
6126 let rec fold path page rely pan visy = function
6127 | ("path", v) :: rest -> fold v page rely pan visy rest
6128 | ("page", v) :: rest -> fold path v rely pan visy rest
6129 | ("rely", v) :: rest -> fold path page v pan visy rest
6130 | ("pan", v) :: rest -> fold path page rely v visy rest
6131 | ("visy", v) :: rest -> fold path page rely pan v rest
6132 | _ :: rest -> fold path page rely pan visy rest
6133 | [] -> path, page, rely, pan, visy
6135 fold "" "0" "0" "0" "0" attrs
6138 let map_of attrs =
6139 let rec fold rs ls = function
6140 | ("out", v) :: rest -> fold v ls rest
6141 | ("in", v) :: rest -> fold rs v rest
6142 | _ :: rest -> fold ls rs rest
6143 | [] -> ls, rs
6145 fold "" "" attrs
6148 let setconf dst src =
6149 dst.scrollbw <- src.scrollbw;
6150 dst.scrollh <- src.scrollh;
6151 dst.icase <- src.icase;
6152 dst.preload <- src.preload;
6153 dst.pagebias <- src.pagebias;
6154 dst.verbose <- src.verbose;
6155 dst.scrollstep <- src.scrollstep;
6156 dst.maxhfit <- src.maxhfit;
6157 dst.crophack <- src.crophack;
6158 dst.autoscrollstep <- src.autoscrollstep;
6159 dst.maxwait <- src.maxwait;
6160 dst.hlinks <- src.hlinks;
6161 dst.underinfo <- src.underinfo;
6162 dst.interpagespace <- src.interpagespace;
6163 dst.zoom <- src.zoom;
6164 dst.presentation <- src.presentation;
6165 dst.angle <- src.angle;
6166 dst.winw <- src.winw;
6167 dst.winh <- src.winh;
6168 dst.savebmarks <- src.savebmarks;
6169 dst.memlimit <- src.memlimit;
6170 dst.proportional <- src.proportional;
6171 dst.texcount <- src.texcount;
6172 dst.sliceheight <- src.sliceheight;
6173 dst.thumbw <- src.thumbw;
6174 dst.jumpback <- src.jumpback;
6175 dst.bgcolor <- src.bgcolor;
6176 dst.scrollbarinpm <- src.scrollbarinpm;
6177 dst.tilew <- src.tilew;
6178 dst.tileh <- src.tileh;
6179 dst.mustoresize <- src.mustoresize;
6180 dst.checkers <- src.checkers;
6181 dst.aalevel <- src.aalevel;
6182 dst.trimmargins <- src.trimmargins;
6183 dst.trimfuzz <- src.trimfuzz;
6184 dst.urilauncher <- src.urilauncher;
6185 dst.colorspace <- src.colorspace;
6186 dst.invert <- src.invert;
6187 dst.colorscale <- src.colorscale;
6188 dst.redirectstderr <- src.redirectstderr;
6189 dst.ghyllscroll <- src.ghyllscroll;
6190 dst.columns <- src.columns;
6191 dst.beyecolumns <- src.beyecolumns;
6192 dst.selcmd <- src.selcmd;
6193 dst.updatecurs <- src.updatecurs;
6194 dst.pathlauncher <- src.pathlauncher;
6195 dst.keyhashes <- copykeyhashes src;
6196 dst.hfsize <- src.hfsize;
6197 dst.hscrollstep <- src.hscrollstep;
6198 dst.pgscale <- src.pgscale;
6199 dst.multicenter <- src.multicenter;
6202 let get s =
6203 let h = Hashtbl.create 10 in
6204 let dc = { defconf with angle = defconf.angle } in
6205 let rec toplevel v t spos _ =
6206 match t with
6207 | Vdata | Vcdata | Vend -> v
6208 | Vopen ("llppconfig", _, closed) ->
6209 if closed
6210 then v
6211 else { v with f = llppconfig }
6212 | Vopen _ ->
6213 error "unexpected subelement at top level" s spos
6214 | Vclose _ -> error "unexpected close at top level" s spos
6216 and llppconfig v t spos _ =
6217 match t with
6218 | Vdata | Vcdata -> v
6219 | Vend -> error "unexpected end of input in llppconfig" s spos
6220 | Vopen ("defaults", attrs, closed) ->
6221 let c = config_of dc attrs in
6222 setconf dc c;
6223 if closed
6224 then v
6225 else { v with f = defaults }
6227 | Vopen ("ui-font", attrs, closed) ->
6228 let rec getsize size = function
6229 | [] -> size
6230 | ("size", v) :: rest ->
6231 let size =
6232 fromstring int_of_string spos "size" v fstate.fontsize in
6233 getsize size rest
6234 | l -> getsize size l
6236 fstate.fontsize <- getsize fstate.fontsize attrs;
6237 if closed
6238 then v
6239 else { v with f = uifont (Buffer.create 10) }
6241 | Vopen ("doc", attrs, closed) ->
6242 let pathent, spage, srely, span, svisy = doc_of attrs in
6243 let path = unent pathent
6244 and pageno = fromstring int_of_string spos "page" spage 0
6245 and rely = fromstring float_of_string spos "rely" srely 0.0
6246 and pan = fromstring int_of_string spos "pan" span 0
6247 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6248 let c = config_of dc attrs in
6249 let anchor = (pageno, rely, visy) in
6250 if closed
6251 then (Hashtbl.add h path (c, [], pan, anchor); v)
6252 else { v with f = doc path pan anchor c [] }
6254 | Vopen _ ->
6255 error "unexpected subelement in llppconfig" s spos
6257 | Vclose "llppconfig" -> { v with f = toplevel }
6258 | Vclose _ -> error "unexpected close in llppconfig" s spos
6260 and defaults v t spos _ =
6261 match t with
6262 | Vdata | Vcdata -> v
6263 | Vend -> error "unexpected end of input in defaults" s spos
6264 | Vopen ("keymap", attrs, closed) ->
6265 let modename =
6266 try List.assoc "mode" attrs
6267 with Not_found -> "global" in
6268 if closed
6269 then v
6270 else
6271 let ret keymap =
6272 let h = findkeyhash dc modename in
6273 KeyMap.iter (Hashtbl.replace h) keymap;
6274 defaults
6276 { v with f = pkeymap ret KeyMap.empty }
6278 | Vopen (_, _, _) ->
6279 error "unexpected subelement in defaults" s spos
6281 | Vclose "defaults" ->
6282 { v with f = llppconfig }
6284 | Vclose _ -> error "unexpected close in defaults" s spos
6286 and uifont b v t spos epos =
6287 match t with
6288 | Vdata | Vcdata ->
6289 Buffer.add_substring b s spos (epos - spos);
6291 | Vopen (_, _, _) ->
6292 error "unexpected subelement in ui-font" s spos
6293 | Vclose "ui-font" ->
6294 if String.length !fontpath = 0
6295 then fontpath := Buffer.contents b;
6296 { v with f = llppconfig }
6297 | Vclose _ -> error "unexpected close in ui-font" s spos
6298 | Vend -> error "unexpected end of input in ui-font" s spos
6300 and doc path pan anchor c bookmarks v t spos _ =
6301 match t with
6302 | Vdata | Vcdata -> v
6303 | Vend -> error "unexpected end of input in doc" s spos
6304 | Vopen ("bookmarks", _, closed) ->
6305 if closed
6306 then v
6307 else { v with f = pbookmarks path pan anchor c bookmarks }
6309 | Vopen ("keymap", attrs, closed) ->
6310 let modename =
6311 try List.assoc "mode" attrs
6312 with Not_found -> "global"
6314 if closed
6315 then v
6316 else
6317 let ret keymap =
6318 let h = findkeyhash c modename in
6319 KeyMap.iter (Hashtbl.replace h) keymap;
6320 doc path pan anchor c bookmarks
6322 { v with f = pkeymap ret KeyMap.empty }
6324 | Vopen (_, _, _) ->
6325 error "unexpected subelement in doc" s spos
6327 | Vclose "doc" ->
6328 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6329 { v with f = llppconfig }
6331 | Vclose _ -> error "unexpected close in doc" s spos
6333 and pkeymap ret keymap v t spos _ =
6334 match t with
6335 | Vdata | Vcdata -> v
6336 | Vend -> error "unexpected end of input in keymap" s spos
6337 | Vopen ("map", attrs, closed) ->
6338 let r, l = map_of attrs in
6339 let kss = fromstring keys_of_string spos "in" r [] in
6340 let lss = fromstring keys_of_string spos "out" l [] in
6341 let keymap =
6342 match kss with
6343 | [] -> keymap
6344 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6345 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6347 if closed
6348 then { v with f = pkeymap ret keymap }
6349 else
6350 let f () = v in
6351 { v with f = skip "map" f }
6353 | Vopen _ ->
6354 error "unexpected subelement in keymap" s spos
6356 | Vclose "keymap" ->
6357 { v with f = ret keymap }
6359 | Vclose _ -> error "unexpected close in keymap" s spos
6361 and pbookmarks path pan anchor c bookmarks v t spos _ =
6362 match t with
6363 | Vdata | Vcdata -> v
6364 | Vend -> error "unexpected end of input in bookmarks" s spos
6365 | Vopen ("item", attrs, closed) ->
6366 let titleent, spage, srely, svisy = bookmark_of attrs in
6367 let page = fromstring int_of_string spos "page" spage 0
6368 and rely = fromstring float_of_string spos "rely" srely 0.0
6369 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6370 let bookmarks =
6371 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6373 if closed
6374 then { v with f = pbookmarks path pan anchor c bookmarks }
6375 else
6376 let f () = v in
6377 { v with f = skip "item" f }
6379 | Vopen _ ->
6380 error "unexpected subelement in bookmarks" s spos
6382 | Vclose "bookmarks" ->
6383 { v with f = doc path pan anchor c bookmarks }
6385 | Vclose _ -> error "unexpected close in bookmarks" s spos
6387 and skip tag f v t spos _ =
6388 match t with
6389 | Vdata | Vcdata -> v
6390 | Vend ->
6391 error ("unexpected end of input in skipped " ^ tag) s spos
6392 | Vopen (tag', _, closed) ->
6393 if closed
6394 then v
6395 else
6396 let f' () = { v with f = skip tag f } in
6397 { v with f = skip tag' f' }
6398 | Vclose ctag ->
6399 if tag = ctag
6400 then f ()
6401 else error ("unexpected close in skipped " ^ tag) s spos
6404 parse { f = toplevel; accu = () } s;
6405 h, dc;
6408 let do_load f ic =
6410 let len = in_channel_length ic in
6411 let s = String.create len in
6412 really_input ic s 0 len;
6413 f s;
6414 with
6415 | Parse_error (msg, s, pos) ->
6416 let subs = subs s pos in
6417 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6418 failwith ("parse error: " ^ s)
6420 | exn ->
6421 failwith ("config load error: " ^ Printexc.to_string exn)
6424 let defconfpath =
6425 let dir =
6427 let dir = Filename.concat home ".config" in
6428 if Sys.is_directory dir then dir else home
6429 with _ -> home
6431 Filename.concat dir "llpp.conf"
6434 let confpath = ref defconfpath;;
6436 let load1 f =
6437 if Sys.file_exists !confpath
6438 then
6439 match
6440 (try Some (open_in_bin !confpath)
6441 with exn ->
6442 prerr_endline
6443 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6444 Printexc.to_string exn);
6445 None
6447 with
6448 | Some ic ->
6449 let success =
6451 f (do_load get ic)
6452 with exn ->
6453 prerr_endline
6454 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6455 Printexc.to_string exn);
6456 false
6458 close_in ic;
6459 success
6461 | None -> false
6462 else
6463 f (Hashtbl.create 0, defconf)
6466 let load () =
6467 let f (h, dc) =
6468 let pc, pb, px, pa =
6470 Hashtbl.find h (Filename.basename state.path)
6471 with Not_found -> dc, [], 0, emptyanchor
6473 setconf defconf dc;
6474 setconf conf pc;
6475 state.bookmarks <- pb;
6476 state.x <- px;
6477 state.scrollw <- conf.scrollbw;
6478 if conf.jumpback
6479 then state.anchor <- pa;
6480 cbput state.hists.nav pa;
6481 true
6483 load1 f
6486 let add_attrs bb always dc c =
6487 let ob s a b =
6488 if always || a != b
6489 then Printf.bprintf bb "\n %s='%b'" s a
6490 and oi s a b =
6491 if always || a != b
6492 then Printf.bprintf bb "\n %s='%d'" s a
6493 and oI s a b =
6494 if always || a != b
6495 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6496 and oz s a b =
6497 if always || a <> b
6498 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6499 and oF s a b =
6500 if always || a <> b
6501 then Printf.bprintf bb "\n %s='%f'" s a
6502 and oc s a b =
6503 if always || a <> b
6504 then
6505 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6506 and oC s a b =
6507 if always || a <> b
6508 then
6509 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6510 and oR s a b =
6511 if always || a <> b
6512 then
6513 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6514 and os s a b =
6515 if always || a <> b
6516 then
6517 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6518 and og s a b =
6519 if always || a <> b
6520 then
6521 match a with
6522 | None -> ()
6523 | Some (_N, _A, _B) ->
6524 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6525 and oW s a b =
6526 if always || a <> b
6527 then
6528 let v =
6529 match a with
6530 | None -> "false"
6531 | Some f ->
6532 if f = infinity
6533 then "true"
6534 else string_of_float f
6536 Printf.bprintf bb "\n %s='%s'" s v
6537 and oco s a b =
6538 if always || a <> b
6539 then
6540 match a with
6541 | Cmulti ((n, a, b), _) when n > 1 ->
6542 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6543 | Csplit (n, _) when n > 1 ->
6544 Printf.bprintf bb "\n %s='%d'" s ~-n
6545 | _ -> ()
6546 and obeco s a b =
6547 if always || a <> b
6548 then
6549 match a with
6550 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6551 | _ -> ()
6553 let w, h =
6554 if always
6555 then dc.winw, dc.winh
6556 else
6557 match state.fullscreen with
6558 | Some wh -> wh
6559 | None -> c.winw, c.winh
6561 oi "width" w dc.winw;
6562 oi "height" h dc.winh;
6563 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6564 oi "scroll-handle-height" c.scrollh dc.scrollh;
6565 ob "case-insensitive-search" c.icase dc.icase;
6566 ob "preload" c.preload dc.preload;
6567 oi "page-bias" c.pagebias dc.pagebias;
6568 oi "scroll-step" c.scrollstep dc.scrollstep;
6569 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6570 ob "max-height-fit" c.maxhfit dc.maxhfit;
6571 ob "crop-hack" c.crophack dc.crophack;
6572 oW "throttle" c.maxwait dc.maxwait;
6573 ob "highlight-links" c.hlinks dc.hlinks;
6574 ob "under-cursor-info" c.underinfo dc.underinfo;
6575 oi "vertical-margin" c.interpagespace dc.interpagespace;
6576 oz "zoom" c.zoom dc.zoom;
6577 ob "presentation" c.presentation dc.presentation;
6578 oi "rotation-angle" c.angle dc.angle;
6579 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6580 ob "proportional-display" c.proportional dc.proportional;
6581 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6582 oi "tex-count" c.texcount dc.texcount;
6583 oi "slice-height" c.sliceheight dc.sliceheight;
6584 oi "thumbnail-width" c.thumbw dc.thumbw;
6585 ob "persistent-location" c.jumpback dc.jumpback;
6586 oc "background-color" c.bgcolor dc.bgcolor;
6587 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6588 oi "tile-width" c.tilew dc.tilew;
6589 oi "tile-height" c.tileh dc.tileh;
6590 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6591 ob "checkers" c.checkers dc.checkers;
6592 oi "aalevel" c.aalevel dc.aalevel;
6593 ob "trim-margins" c.trimmargins dc.trimmargins;
6594 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6595 os "uri-launcher" c.urilauncher dc.urilauncher;
6596 os "path-launcher" c.pathlauncher dc.pathlauncher;
6597 oC "color-space" c.colorspace dc.colorspace;
6598 ob "invert-colors" c.invert dc.invert;
6599 oF "brightness" c.colorscale dc.colorscale;
6600 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6601 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6602 oco "columns" c.columns dc.columns;
6603 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6604 os "selection-command" c.selcmd dc.selcmd;
6605 ob "update-cursor" c.updatecurs dc.updatecurs;
6606 oi "hint-font-size" c.hfsize dc.hfsize;
6607 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6608 oF "page-scroll-scale" c.pgscale dc.pgscale;
6609 ob "multi-column-centering" c.multicenter dc.multicenter;
6612 let keymapsbuf always dc c =
6613 let bb = Buffer.create 16 in
6614 let rec loop = function
6615 | [] -> ()
6616 | (modename, h) :: rest ->
6617 let dh = findkeyhash dc modename in
6618 if always || h <> dh
6619 then (
6620 if Hashtbl.length h > 0
6621 then (
6622 if Buffer.length bb > 0
6623 then Buffer.add_char bb '\n';
6624 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6625 Hashtbl.iter (fun i o ->
6626 let isdifferent = always ||
6628 let dO = Hashtbl.find dh i in
6629 dO <> o
6630 with Not_found -> true
6632 if isdifferent
6633 then
6634 let addkm (k, m) =
6635 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6636 if Wsi.withalt m then Buffer.add_string bb "alt-";
6637 if Wsi.withshift m then Buffer.add_string bb "shift-";
6638 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6639 Buffer.add_string bb (Wsi.keyname k);
6641 let addkms l =
6642 let rec loop = function
6643 | [] -> ()
6644 | km :: [] -> addkm km
6645 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6647 loop l
6649 Buffer.add_string bb "<map in='";
6650 addkm i;
6651 match o with
6652 | KMinsrt km ->
6653 Buffer.add_string bb "' out='";
6654 addkm km;
6655 Buffer.add_string bb "'/>\n"
6657 | KMinsrl kms ->
6658 Buffer.add_string bb "' out='";
6659 addkms kms;
6660 Buffer.add_string bb "'/>\n"
6662 | KMmulti (ins, kms) ->
6663 Buffer.add_char bb ' ';
6664 addkms ins;
6665 Buffer.add_string bb "' out='";
6666 addkms kms;
6667 Buffer.add_string bb "'/>\n"
6668 ) h;
6669 Buffer.add_string bb "</keymap>";
6672 loop rest
6674 loop c.keyhashes;
6678 let save () =
6679 let uifontsize = fstate.fontsize in
6680 let bb = Buffer.create 32768 in
6681 let f (h, dc) =
6682 let dc = if conf.bedefault then conf else dc in
6683 Buffer.add_string bb "<llppconfig>\n";
6685 if String.length !fontpath > 0
6686 then
6687 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6688 uifontsize
6689 !fontpath
6690 else (
6691 if uifontsize <> 14
6692 then
6693 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6696 Buffer.add_string bb "<defaults ";
6697 add_attrs bb true dc dc;
6698 let kb = keymapsbuf true dc dc in
6699 if Buffer.length kb > 0
6700 then (
6701 Buffer.add_string bb ">\n";
6702 Buffer.add_buffer bb kb;
6703 Buffer.add_string bb "\n</defaults>\n";
6705 else Buffer.add_string bb "/>\n";
6707 let adddoc path pan anchor c bookmarks =
6708 if bookmarks == [] && c = dc && anchor = emptyanchor
6709 then ()
6710 else (
6711 Printf.bprintf bb "<doc path='%s'"
6712 (enent path 0 (String.length path));
6714 if anchor <> emptyanchor
6715 then (
6716 let n, rely, visy = anchor in
6717 Printf.bprintf bb " page='%d'" n;
6718 if rely > 1e-6
6719 then
6720 Printf.bprintf bb " rely='%f'" rely
6722 if abs_float visy > 1e-6
6723 then
6724 Printf.bprintf bb " visy='%f'" visy
6728 if pan != 0
6729 then Printf.bprintf bb " pan='%d'" pan;
6731 add_attrs bb false dc c;
6732 let kb = keymapsbuf false dc c in
6734 begin match bookmarks with
6735 | [] ->
6736 if Buffer.length kb > 0
6737 then (
6738 Buffer.add_string bb ">\n";
6739 Buffer.add_buffer bb kb;
6740 Buffer.add_string bb "\n</doc>\n";
6742 else Buffer.add_string bb "/>\n"
6743 | _ ->
6744 Buffer.add_string bb ">\n<bookmarks>\n";
6745 List.iter (fun (title, _level, (page, rely, visy)) ->
6746 Printf.bprintf bb
6747 "<item title='%s' page='%d'"
6748 (enent title 0 (String.length title))
6749 page
6751 if rely > 1e-6
6752 then
6753 Printf.bprintf bb " rely='%f'" rely
6755 if abs_float visy > 1e-6
6756 then
6757 Printf.bprintf bb " visy='%f'" visy
6759 Buffer.add_string bb "/>\n";
6760 ) bookmarks;
6761 Buffer.add_string bb "</bookmarks>";
6762 if Buffer.length kb > 0
6763 then (
6764 Buffer.add_string bb "\n";
6765 Buffer.add_buffer bb kb;
6767 Buffer.add_string bb "\n</doc>\n";
6768 end;
6772 let pan, conf =
6773 match state.mode with
6774 | Birdseye (c, pan, _, _, _) ->
6775 let beyecolumns =
6776 match conf.columns with
6777 | Cmulti ((c, _, _), _) -> Some c
6778 | Csingle _ -> None
6779 | Csplit _ -> None
6780 and columns =
6781 match c.columns with
6782 | Cmulti (c, _) -> Cmulti (c, [||])
6783 | Csingle _ -> Csingle [||]
6784 | Csplit _ -> failwith "quit from bird's eye while split"
6786 pan, { c with beyecolumns = beyecolumns; columns = columns }
6787 | _ -> state.x, conf
6789 let basename = Filename.basename state.path in
6790 adddoc basename pan (getanchor ())
6791 (let conf =
6792 let autoscrollstep =
6793 match state.autoscroll with
6794 | Some step -> step
6795 | None -> conf.autoscrollstep
6797 match state.mode with
6798 | Birdseye (bc, _, _, _, _) ->
6799 { conf with
6800 zoom = bc.zoom;
6801 presentation = bc.presentation;
6802 interpagespace = bc.interpagespace;
6803 maxwait = bc.maxwait;
6804 autoscrollstep = autoscrollstep }
6805 | _ -> { conf with autoscrollstep = autoscrollstep }
6806 in conf)
6807 (if conf.savebmarks then state.bookmarks else []);
6809 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6810 if basename <> path
6811 then adddoc path x anchor c bookmarks
6812 ) h;
6813 Buffer.add_string bb "</llppconfig>\n";
6814 true;
6816 if load1 f && Buffer.length bb > 0
6817 then
6819 let tmp = !confpath ^ ".tmp" in
6820 let oc = open_out_bin tmp in
6821 Buffer.output_buffer oc bb;
6822 close_out oc;
6823 Unix.rename tmp !confpath;
6824 with exn ->
6825 prerr_endline
6826 ("error while saving configuration: " ^ Printexc.to_string exn)
6828 end;;
6830 let () =
6831 let trimcachepath = ref "" in
6832 Arg.parse
6833 (Arg.align
6834 [("-p", Arg.String (fun s -> state.password <- s) ,
6835 "<password> Set password");
6837 ("-f", Arg.String (fun s -> Config.fontpath := s),
6838 "<path> Set path to the user interface font");
6840 ("-c", Arg.String (fun s -> Config.confpath := s),
6841 "<path> Set path to the configuration file");
6843 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6844 "<path> Set path to the trim cache file");
6846 ("-v", Arg.Unit (fun () ->
6847 Printf.printf
6848 "%s\nconfiguration path: %s\n"
6849 (version ())
6850 Config.defconfpath
6852 exit 0), " Print version and exit");
6855 (fun s -> state.path <- s)
6856 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6858 if String.length state.path = 0
6859 then (prerr_endline "file name missing"; exit 1);
6861 if not (Config.load ())
6862 then prerr_endline "failed to load configuration";
6864 let globalkeyhash = findkeyhash conf "global" in
6865 let wsfd, winw, winh = Wsi.init (object
6866 method expose =
6867 if nogeomcmds state.geomcmds || platform == Posx
6868 then display ()
6869 else (
6870 GlClear.color (scalecolor2 conf.bgcolor);
6871 GlClear.clear [`color];
6873 method display = display ()
6874 method reshape w h = reshape w h
6875 method mouse b d x y m = mouse b d x y m
6876 method motion x y = state.mpos <- (x, y); motion x y
6877 method pmotion x y = state.mpos <- (x, y); pmotion x y
6878 method key k m =
6879 let mascm = m land (
6880 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6881 ) in
6882 match state.keystate with
6883 | KSnone ->
6884 let km = k, mascm in
6885 begin
6886 match
6887 let modehash = state.uioh#modehash in
6888 try Hashtbl.find modehash km
6889 with Not_found ->
6890 try Hashtbl.find globalkeyhash km
6891 with Not_found -> KMinsrt (k, m)
6892 with
6893 | KMinsrt (k, m) -> keyboard k m
6894 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6895 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6897 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6898 List.iter (fun (k, m) -> keyboard k m) insrt;
6899 state.keystate <- KSnone
6900 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6901 state.keystate <- KSinto (keys, insrt)
6902 | _ ->
6903 state.keystate <- KSnone
6905 method enter x y = state.mpos <- (x, y); pmotion x y
6906 method leave = state.mpos <- (-1, -1)
6907 method quit = raise Quit
6908 end) conf.winw conf.winh (platform = Posx) in
6910 state.wsfd <- wsfd;
6912 if not (
6913 List.exists GlMisc.check_extension
6914 [ "GL_ARB_texture_rectangle"
6915 ; "GL_EXT_texture_recangle"
6916 ; "GL_NV_texture_rectangle" ]
6918 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6920 let cr, sw =
6921 match Ne.pipe () with
6922 | Ne.Exn exn ->
6923 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6924 exit 1
6925 | Ne.Res rw -> rw
6926 and sr, cw =
6927 match Ne.pipe () with
6928 | Ne.Exn exn ->
6929 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6930 exit 1
6931 | Ne.Res rw -> rw
6934 cloexec cr;
6935 cloexec sw;
6936 cloexec sr;
6937 cloexec cw;
6939 setcheckers conf.checkers;
6940 redirectstderr ();
6942 init (cr, cw) (
6943 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6944 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6945 !Config.fontpath, !trimcachepath
6947 state.sr <- sr;
6948 state.sw <- sw;
6949 state.text <- "Opening " ^ state.path;
6950 reshape winw winh;
6951 opendoc state.path state.password;
6952 state.uioh <- uioh;
6954 let rec loop deadline =
6955 let r =
6956 match state.errfd with
6957 | None -> [state.sr; state.wsfd]
6958 | Some fd -> [state.sr; state.wsfd; fd]
6960 if state.redisplay
6961 then (
6962 state.redisplay <- false;
6963 display ();
6965 let timeout =
6966 let now = now () in
6967 if deadline > now
6968 then (
6969 if deadline = infinity
6970 then ~-.1.0
6971 else max 0.0 (deadline -. now)
6973 else 0.0
6975 let r, _, _ =
6976 try Unix.select r [] [] timeout
6977 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6979 begin match r with
6980 | [] ->
6981 state.ghyll None;
6982 let newdeadline =
6983 if state.ghyll == noghyll
6984 then
6985 match state.autoscroll with
6986 | Some step when step != 0 ->
6987 let y = state.y + step in
6988 let y =
6989 if y < 0
6990 then state.maxy
6991 else if y >= state.maxy then 0 else y
6993 gotoy y;
6994 if state.mode = View
6995 then state.text <- "";
6996 deadline +. 0.01
6997 | _ -> infinity
6998 else deadline +. 0.01
7000 loop newdeadline
7002 | l ->
7003 let rec checkfds = function
7004 | [] -> ()
7005 | fd :: rest when fd = state.sr ->
7006 let cmd = readcmd state.sr in
7007 act cmd;
7008 checkfds rest
7010 | fd :: rest when fd = state.wsfd ->
7011 Wsi.readresp fd;
7012 checkfds rest
7014 | fd :: rest ->
7015 let s = String.create 80 in
7016 let n = Unix.read fd s 0 80 in
7017 if conf.redirectstderr
7018 then (
7019 Buffer.add_substring state.errmsgs s 0 n;
7020 state.newerrmsgs <- true;
7021 state.redisplay <- true;
7023 else (
7024 prerr_string (String.sub s 0 n);
7025 flush stderr;
7027 checkfds rest
7029 checkfds l;
7030 let newdeadline =
7031 let deadline1 =
7032 if deadline = infinity
7033 then now () +. 0.01
7034 else deadline
7036 match state.autoscroll with
7037 | Some step when step != 0 -> deadline1
7038 | _ -> if state.ghyll == noghyll then infinity else deadline1
7040 loop newdeadline
7041 end;
7044 loop infinity;
7045 with Quit ->
7046 Config.save ();