Do multicolumns properly
[llpp/slowscroll.git] / main.ml
blob74cc6b05da408787a76a554ec044cabc5bb25244
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
340 and columns =
341 | Csingle of singlecolumn
342 | Cmulti of multicolumns
343 | Csplit of splitcolumns
346 type anchor = pageno * top * dtop;;
348 type outline = string * int * anchor;;
350 type rect = float * float * float * float * float * float * float * float;;
352 type tile = opaque * pixmapsize * elapsed
353 and elapsed = float;;
354 type pagemapkey = pageno * gen;;
355 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
356 and row = int
357 and col = int;;
359 let emptyanchor = (0, 0.0, 0.0);;
361 type infochange = | Memused | Docinfo | Pdim;;
363 class type uioh = object
364 method display : unit
365 method key : int -> int -> uioh
366 method button : int -> bool -> int -> int -> int -> uioh
367 method motion : int -> int -> uioh
368 method pmotion : int -> int -> uioh
369 method infochanged : infochange -> unit
370 method scrollpw : (int * float * float)
371 method scrollph : (int * float * float)
372 method modehash : keyhash
373 end;;
375 type mode =
376 | Birdseye of (conf * leftx * pageno * pageno * anchor)
377 | Textentry of (textentry * onleave)
378 | View
379 | LinkNav of linktarget
380 and onleave = leavetextentrystatus -> unit
381 and leavetextentrystatus = | Cancel | Confirm
382 and helpitem = string * int * action
383 and action =
384 | Noaction
385 | Action of (uioh -> uioh)
386 and linktarget =
387 | Ltexact of (pageno * int)
388 | Ltgendir of int
391 let isbirdseye = function Birdseye _ -> true | _ -> false;;
392 let istextentry = function Textentry _ -> true | _ -> false;;
394 type currently =
395 | Idle
396 | Loading of (page * gen)
397 | Tiling of (
398 page * opaque * colorspace * angle * gen * col * row * width * height
400 | Outlining of outline list
403 let emptykeyhash = Hashtbl.create 0;;
404 let nouioh : uioh = object (self)
405 method display = ()
406 method key _ _ = self
407 method button _ _ _ _ _ = self
408 method motion _ _ = self
409 method pmotion _ _ = self
410 method infochanged _ = ()
411 method scrollpw = (0, nan, nan)
412 method scrollph = (0, nan, nan)
413 method modehash = emptykeyhash
414 end;;
416 type state =
417 { mutable sr : Unix.file_descr
418 ; mutable sw : Unix.file_descr
419 ; mutable wsfd : Unix.file_descr
420 ; mutable errfd : Unix.file_descr option
421 ; mutable stderr : Unix.file_descr
422 ; mutable errmsgs : Buffer.t
423 ; mutable newerrmsgs : bool
424 ; mutable w : int
425 ; mutable x : int
426 ; mutable y : int
427 ; mutable scrollw : int
428 ; mutable hscrollh : int
429 ; mutable anchor : anchor
430 ; mutable ranchors : (string * string * anchor) list
431 ; mutable maxy : int
432 ; mutable layout : page list
433 ; pagemap : (pagemapkey, opaque) Hashtbl.t
434 ; tilemap : (tilemapkey, tile) Hashtbl.t
435 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
436 ; mutable pdims : (pageno * width * height * leftx) list
437 ; mutable pagecount : int
438 ; mutable currently : currently
439 ; mutable mstate : mstate
440 ; mutable searchpattern : string
441 ; mutable rects : (pageno * recttype * rect) list
442 ; mutable rects1 : (pageno * recttype * rect) list
443 ; mutable text : string
444 ; mutable fullscreen : (width * height) option
445 ; mutable mode : mode
446 ; mutable uioh : uioh
447 ; mutable outlines : outline array
448 ; mutable bookmarks : outline list
449 ; mutable path : string
450 ; mutable password : string
451 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
452 ; mutable memused : memsize
453 ; mutable gen : gen
454 ; mutable throttle : (page list * int * float) option
455 ; mutable autoscroll : int option
456 ; mutable ghyll : (int option -> unit)
457 ; mutable help : helpitem array
458 ; mutable docinfo : (int * string) list
459 ; mutable texid : GlTex.texture_id option
460 ; hists : hists
461 ; mutable prevzoom : float
462 ; mutable progress : float
463 ; mutable redisplay : bool
464 ; mutable mpos : mpos
465 ; mutable keystate : keystate
466 ; mutable glinks : bool
467 ; mutable prevcolumns : (columns * float) option
469 and hists =
470 { pat : string circbuf
471 ; pag : string circbuf
472 ; nav : anchor circbuf
473 ; sel : string circbuf
477 let defconf =
478 { scrollbw = 7
479 ; scrollh = 12
480 ; icase = true
481 ; preload = true
482 ; pagebias = 0
483 ; verbose = false
484 ; debug = false
485 ; scrollstep = 24
486 ; hscrollstep = 24
487 ; maxhfit = true
488 ; crophack = false
489 ; autoscrollstep = 2
490 ; maxwait = None
491 ; hlinks = false
492 ; underinfo = false
493 ; interpagespace = 2
494 ; zoom = 1.0
495 ; presentation = false
496 ; angle = 0
497 ; winw = 900
498 ; winh = 900
499 ; savebmarks = true
500 ; proportional = true
501 ; trimmargins = false
502 ; trimfuzz = (0,0,0,0)
503 ; memlimit = 32 lsl 20
504 ; texcount = 256
505 ; sliceheight = 24
506 ; thumbw = 76
507 ; jumpback = true
508 ; bgcolor = (0.5, 0.5, 0.5)
509 ; bedefault = false
510 ; scrollbarinpm = true
511 ; tilew = 2048
512 ; tileh = 2048
513 ; mustoresize = 256 lsl 20
514 ; checkers = true
515 ; aalevel = 8
516 ; urilauncher =
517 (match platform with
518 | Plinux | Pfreebsd | Pdragonflybsd
519 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
520 | Posx -> "open \"%s\""
521 | Pcygwin -> "cygstart \"%s\""
522 | Punknown -> "echo %s")
523 ; pathlauncher = "lp \"%s\""
524 ; selcmd =
525 (match platform with
526 | Plinux | Pfreebsd | Pdragonflybsd
527 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
528 | Posx -> "pbcopy"
529 | Pcygwin -> "wsel"
530 | Punknown -> "cat")
531 ; colorspace = Rgb
532 ; invert = false
533 ; colorscale = 1.0
534 ; redirectstderr = false
535 ; ghyllscroll = None
536 ; columns = Csingle [||]
537 ; beyecolumns = None
538 ; updatecurs = false
539 ; hfsize = 12
540 ; pgscale = 1.0
541 ; keyhashes =
542 let mk n = (n, Hashtbl.create 1) in
543 [ mk "global"
544 ; mk "info"
545 ; mk "help"
546 ; mk "outline"
547 ; mk "listview"
548 ; mk "birdseye"
549 ; mk "textentry"
550 ; mk "links"
551 ; mk "view"
556 let findkeyhash c name =
557 try List.assoc name c.keyhashes
558 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
561 let conf = { defconf with angle = defconf.angle };;
563 let pgscale h = truncate (float h *. conf.pgscale);;
565 type fontstate =
566 { mutable fontsize : int
567 ; mutable wwidth : float
568 ; mutable maxrows : int
572 let fstate =
573 { fontsize = 14
574 ; wwidth = nan
575 ; maxrows = -1
579 let setfontsize n =
580 fstate.fontsize <- n;
581 fstate.wwidth <- measurestr fstate.fontsize "w";
582 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
585 let geturl s =
586 let colonpos = try String.index s ':' with Not_found -> -1 in
587 let len = String.length s in
588 if colonpos >= 0 && colonpos + 3 < len
589 then (
590 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
591 then
592 let schemestartpos =
593 try String.rindex_from s colonpos ' '
594 with Not_found -> -1
596 let scheme =
597 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
599 match scheme with
600 | "http" | "ftp" | "mailto" ->
601 let epos =
602 try String.index_from s colonpos ' '
603 with Not_found -> len
605 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
606 | _ -> ""
607 else ""
609 else ""
612 let gotouri uri =
613 if String.length conf.urilauncher = 0
614 then print_endline uri
615 else (
616 let url = geturl uri in
617 if String.length url = 0
618 then print_endline uri
619 else
620 let re = Str.regexp "%s" in
621 let command = Str.global_replace re url conf.urilauncher in
622 try popen command []
623 with exn ->
624 Printf.eprintf
625 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
626 flush stderr;
630 let version () =
631 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
632 (platform_to_string platform) Sys.word_size Sys.ocaml_version
635 let makehelp () =
636 let strings = version () :: "" :: Help.keys in
637 Array.of_list (
638 List.map (fun s ->
639 let url = geturl s in
640 if String.length url > 0
641 then (s, 0, Action (fun u -> gotouri url; u))
642 else (s, 0, Noaction)
643 ) strings);
646 let noghyll _ = ();;
647 let firstgeomcmds = "", [];;
649 let state =
650 { sr = Unix.stdin
651 ; sw = Unix.stdin
652 ; wsfd = Unix.stdin
653 ; errfd = None
654 ; stderr = Unix.stderr
655 ; errmsgs = Buffer.create 0
656 ; newerrmsgs = false
657 ; x = 0
658 ; y = 0
659 ; w = 0
660 ; scrollw = 0
661 ; hscrollh = 0
662 ; anchor = emptyanchor
663 ; ranchors = []
664 ; layout = []
665 ; maxy = max_int
666 ; tilelru = Queue.create ()
667 ; pagemap = Hashtbl.create 10
668 ; tilemap = Hashtbl.create 10
669 ; pdims = []
670 ; pagecount = 0
671 ; currently = Idle
672 ; mstate = Mnone
673 ; rects = []
674 ; rects1 = []
675 ; text = ""
676 ; mode = View
677 ; fullscreen = None
678 ; searchpattern = ""
679 ; outlines = [||]
680 ; bookmarks = []
681 ; path = ""
682 ; password = ""
683 ; geomcmds = firstgeomcmds
684 ; hists =
685 { nav = cbnew 10 emptyanchor
686 ; pat = cbnew 10 ""
687 ; pag = cbnew 10 ""
688 ; sel = cbnew 10 ""
690 ; memused = 0
691 ; gen = 0
692 ; throttle = None
693 ; autoscroll = None
694 ; ghyll = noghyll
695 ; help = makehelp ()
696 ; docinfo = []
697 ; texid = None
698 ; prevzoom = 1.0
699 ; progress = -1.0
700 ; uioh = nouioh
701 ; redisplay = true
702 ; mpos = (-1, -1)
703 ; keystate = KSnone
704 ; glinks = false
705 ; prevcolumns = None
709 let vlog fmt =
710 if conf.verbose
711 then
712 Printf.kprintf prerr_endline fmt
713 else
714 Printf.kprintf ignore fmt
717 let launchpath () =
718 if String.length conf.pathlauncher = 0
719 then print_endline state.path
720 else (
721 let re = Str.regexp "%s" in
722 let command = Str.global_replace re state.path conf.pathlauncher in
723 try popen command []
724 with exn ->
725 Printf.eprintf
726 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
727 flush stderr;
731 module Ne = struct
732 type 'a t = | Res of 'a | Exn of exn;;
734 let pipe () =
735 try Res (Unix.pipe ())
736 with exn -> Exn exn
739 let clo fd f =
740 try Unix.close fd
741 with exn -> f (Printexc.to_string exn)
744 let dup fd =
745 try Res (Unix.dup fd)
746 with exn -> Exn exn
749 let dup2 fd1 fd2 =
750 try Res (Unix.dup2 fd1 fd2)
751 with exn -> Exn exn
753 end;;
755 let redirectstderr () =
756 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
757 if conf.redirectstderr
758 then
759 match Ne.pipe () with
760 | Ne.Exn exn ->
761 dolog "failed to create stderr redirection pipes: %s"
762 (Printexc.to_string exn)
764 | Ne.Res (r, w) ->
765 begin match Ne.dup Unix.stderr with
766 | Ne.Exn exn ->
767 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
768 Ne.clo r (clofail "pipe/r");
769 Ne.clo w (clofail "pipe/w");
771 | Ne.Res dupstderr ->
772 begin match Ne.dup2 w Unix.stderr with
773 | Ne.Exn exn ->
774 dolog "failed to dup2 to stderr: %s"
775 (Printexc.to_string exn);
776 Ne.clo dupstderr (clofail "stderr duplicate");
777 Ne.clo r (clofail "redir pipe/r");
778 Ne.clo w (clofail "redir pipe/w");
780 | Ne.Res () ->
781 state.stderr <- dupstderr;
782 state.errfd <- Some r;
783 end;
785 else (
786 state.newerrmsgs <- false;
787 begin match state.errfd with
788 | Some fd ->
789 begin match Ne.dup2 state.stderr Unix.stderr with
790 | Ne.Exn exn ->
791 dolog "failed to dup2 original stderr: %s"
792 (Printexc.to_string exn)
793 | Ne.Res () ->
794 Ne.clo fd (clofail "dup of stderr");
795 Unix.dup2 state.stderr Unix.stderr;
796 state.errfd <- None;
797 end;
798 | None -> ()
799 end;
800 prerr_string (Buffer.contents state.errmsgs);
801 flush stderr;
802 Buffer.clear state.errmsgs;
806 module G =
807 struct
808 let postRedisplay who =
809 if conf.verbose
810 then prerr_endline ("redisplay for " ^ who);
811 state.redisplay <- true;
813 end;;
815 let getopaque pageno =
816 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
817 with Not_found -> None
820 let putopaque pageno opaque =
821 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
824 let pagetranslatepoint l x y =
825 let dy = y - l.pagedispy in
826 let y = dy + l.pagey in
827 let dx = x - l.pagedispx in
828 let x = dx + l.pagex in
829 (x, y);
832 let getunder x y =
833 let rec f = function
834 | l :: rest ->
835 begin match getopaque l.pageno with
836 | Some opaque ->
837 let x0 = l.pagedispx in
838 let x1 = x0 + l.pagevw in
839 let y0 = l.pagedispy in
840 let y1 = y0 + l.pagevh in
841 if y >= y0 && y <= y1 && x >= x0 && x <= x1
842 then
843 let px, py = pagetranslatepoint l x y in
844 match whatsunder opaque px py with
845 | Unone -> f rest
846 | under -> under
847 else f rest
848 | _ ->
849 f rest
851 | [] -> Unone
853 f state.layout
856 let showtext c s =
857 state.text <- Printf.sprintf "%c%s" c s;
858 G.postRedisplay "showtext";
861 let undertext = function
862 | Unone -> "none"
863 | Ulinkuri s -> s
864 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
865 | Utext s -> "font: " ^ s
866 | Uunexpected s -> "unexpected: " ^ s
867 | Ulaunch s -> "launch: " ^ s
868 | Unamed s -> "named: " ^ s
869 | Uremote (filename, pageno) ->
870 Printf.sprintf "%s: page %d" filename (pageno+1)
873 let updateunder x y =
874 match getunder x y with
875 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
876 | Ulinkuri uri ->
877 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
878 Wsi.setcursor Wsi.CURSOR_INFO
879 | Ulinkgoto (pageno, _) ->
880 if conf.underinfo
881 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
882 Wsi.setcursor Wsi.CURSOR_INFO
883 | Utext s ->
884 if conf.underinfo then showtext 'f' ("ont: " ^ s);
885 Wsi.setcursor Wsi.CURSOR_TEXT
886 | Uunexpected s ->
887 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
888 Wsi.setcursor Wsi.CURSOR_INHERIT
889 | Ulaunch s ->
890 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
891 Wsi.setcursor Wsi.CURSOR_INHERIT
892 | Unamed s ->
893 if conf.underinfo then showtext 'n' ("amed: " ^ s);
894 Wsi.setcursor Wsi.CURSOR_INHERIT
895 | Uremote (filename, pageno) ->
896 if conf.underinfo then showtext 'r'
897 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
898 Wsi.setcursor Wsi.CURSOR_INFO
901 let showlinktype under =
902 if conf.underinfo
903 then
904 match under with
905 | Unone -> ()
906 | under ->
907 let s = undertext under in
908 showtext ' ' s
911 let addchar s c =
912 let b = Buffer.create (String.length s + 1) in
913 Buffer.add_string b s;
914 Buffer.add_char b c;
915 Buffer.contents b;
918 let colorspace_of_string s =
919 match String.lowercase s with
920 | "rgb" -> Rgb
921 | "bgr" -> Bgr
922 | "gray" -> Gray
923 | _ -> failwith "invalid colorspace"
926 let int_of_colorspace = function
927 | Rgb -> 0
928 | Bgr -> 1
929 | Gray -> 2
932 let colorspace_of_int = function
933 | 0 -> Rgb
934 | 1 -> Bgr
935 | 2 -> Gray
936 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
939 let colorspace_to_string = function
940 | Rgb -> "rgb"
941 | Bgr -> "bgr"
942 | Gray -> "gray"
945 let intentry_with_suffix text key =
946 let c =
947 if key >= 32 && key < 127
948 then Char.chr key
949 else '\000'
951 match Char.lowercase c with
952 | '0' .. '9' ->
953 let text = addchar text c in
954 TEcont text
956 | 'k' | 'm' | 'g' ->
957 let text = addchar text c in
958 TEcont text
960 | _ ->
961 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
962 TEcont text
965 let multicolumns_to_string (n, a, b) =
966 if a = 0 && b = 0
967 then Printf.sprintf "%d" n
968 else Printf.sprintf "%d,%d,%d" n a b;
971 let multicolumns_of_string s =
973 (int_of_string s, 0, 0)
974 with _ ->
975 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
976 if a > 1 || b > 1
977 then failwith "subtly broken"; (n, a, b)
981 let readcmd fd =
982 let s = "xxxx" in
983 let n = Unix.read fd s 0 4 in
984 if n != 4 then failwith "incomplete read(len)";
985 let len = 0
986 lor (Char.code s.[0] lsl 24)
987 lor (Char.code s.[1] lsl 16)
988 lor (Char.code s.[2] lsl 8)
989 lor (Char.code s.[3] lsl 0)
991 let s = String.create len in
992 let n = Unix.read fd s 0 len in
993 if n != len then failwith "incomplete read(data)";
997 let btod b = if b then 1 else 0;;
999 let wcmd fmt =
1000 let b = Buffer.create 16 in
1001 Buffer.add_string b "llll";
1002 Printf.kbprintf
1003 (fun b ->
1004 let s = Buffer.contents b in
1005 let n = String.length s in
1006 let len = n - 4 in
1007 (* dolog "wcmd %S" (String.sub s 4 len); *)
1008 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1009 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1010 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1011 s.[3] <- Char.chr (len land 0xff);
1012 let n' = Unix.write state.sw s 0 n in
1013 if n' != n then failwith "write failed";
1014 ) b fmt;
1017 let calcips h =
1018 let d = conf.winh - h in
1019 max conf.interpagespace ((d + 1) / 2)
1022 let rowyh (c, coverA, coverB) b n =
1023 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1024 then
1025 let _, _, vy, (_, _, h, _) = b.(n) in
1026 (vy, h)
1027 else
1028 let n' = n - coverA in
1029 let d = n' mod c in
1030 let s = n - d in
1031 let e = min state.pagecount (s + c) in
1032 let rec find m miny maxh = if m = e then miny, maxh else
1033 let _, _, y, (_, _, h, _) = b.(m) in
1034 let miny = min miny y in
1035 let maxh = max maxh h in
1036 find (m+1) miny maxh
1037 in find s max_int 0
1040 let calcheight () =
1041 match conf.columns with
1042 | Cmulti ((_, _, _) as cl, b) ->
1043 if Array.length b > 0
1044 then
1045 let y, h = rowyh cl b (Array.length b - 1) in
1046 y + h + (if conf.presentation then calcips h else 0)
1047 else 0
1048 | Csingle b ->
1049 if Array.length b > 0
1050 then
1051 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1052 y + h + (if conf.presentation then calcips h else 0)
1053 else 0
1054 | Csplit (_, b) ->
1055 if Array.length b > 0
1056 then
1057 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1058 y + h
1059 else 0
1062 let getpageyh pageno =
1063 let pageno = bound pageno 0 (state.pagecount-1) in
1064 match conf.columns with
1065 | Csingle b ->
1066 if Array.length b = 0
1067 then 0, 0
1068 else
1069 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1070 let y =
1071 if conf.presentation
1072 then y - calcips h
1073 else y
1075 y, h
1076 | Cmulti (cl, b) ->
1077 if Array.length b = 0
1078 then 0, 0
1079 else
1080 let y, h = rowyh cl b pageno in
1081 let y =
1082 if conf.presentation
1083 then y - calcips h
1084 else y
1086 y, h
1087 | Csplit (c, b) ->
1088 if Array.length b = 0
1089 then 0, 0
1090 else
1091 let n = pageno*c in
1092 let (_, _, y, (_, _, h, _)) = b.(n) in
1093 y, h
1096 let getpagedim pageno =
1097 let rec f ppdim l =
1098 match l with
1099 | (n, _, _, _) as pdim :: rest ->
1100 if n >= pageno
1101 then (if n = pageno then pdim else ppdim)
1102 else f pdim rest
1104 | [] -> ppdim
1106 f (-1, -1, -1, -1) state.pdims
1109 let getpagey pageno = fst (getpageyh pageno);;
1111 let nogeomcmds cmds =
1112 match cmds with
1113 | s, [] -> String.length s = 0
1114 | _ -> false
1117 let page_of_y y =
1118 let ((c, coverA, coverB) as cl), b =
1119 match conf.columns with
1120 | Csingle b -> (1, 0, 0), b
1121 | Cmulti (c, b) -> c, b
1122 | Csplit (_, b) -> (1, 0, 0), b
1124 let rec bsearch nmin nmax =
1125 if nmin > nmax
1126 then bound nmin 0 (state.pagecount-1)
1127 else
1128 let n = (nmax + nmin) / 2 in
1129 let vy, h = rowyh cl b n in
1130 let y0, y1 =
1131 if conf.presentation
1132 then
1133 let ips = calcips h in
1134 let y0 = vy - ips in
1135 let y1 = vy + h + ips in
1136 y0, y1
1137 else (
1138 if n = 0
1139 then 0, vy + h + conf.interpagespace
1140 else
1141 let y0 = vy - conf.interpagespace in
1142 y0, y0 + h + conf.interpagespace
1145 if y >= y0 && y < y1
1146 then (
1147 if c = 1
1148 then n
1149 else (
1150 if n > coverA
1151 then
1152 if n < state.pagecount - coverB
1153 then ((n-coverA)/c)*c + coverA
1154 else n
1155 else n
1158 else (
1159 if y > y0
1160 then bsearch (n+1) nmax
1161 else bsearch nmin (n-1)
1164 let r = bsearch 0 (state.pagecount-1) in
1168 let layoutN ((columns, coverA, coverB), b) y sh =
1169 let sh = sh - state.hscrollh in
1170 let rec fold accu n =
1171 if n = Array.length b
1172 then accu
1173 else
1174 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1175 if (vy - y) > sh &&
1176 (n = coverA - 1
1177 || n = state.pagecount - coverB
1178 || (n - coverA) mod columns = columns - 1)
1179 then accu
1180 else
1181 let accu =
1182 if vy + h > y
1183 then
1184 let pagey = max 0 (y - vy) in
1185 let pagedispy = if pagey > 0 then 0 else vy - y in
1186 let pagedispx, pagex =
1187 let pdx =
1188 if n = coverA - 1 || n = state.pagecount - coverB
1189 then state.x + (conf.winw - state.scrollw - w) / 2
1190 else dx + xoff + state.x
1192 if pdx < 0
1193 then 0, -pdx
1194 else pdx, 0
1196 let pagevw =
1197 let vw = conf.winw - state.scrollw - pagedispx in
1198 let pw = w - pagex in
1199 min vw pw
1201 let pagevh = min (h - pagey) (sh - pagedispy) in
1202 if pagevw > 0 && pagevh > 0
1203 then
1204 let e =
1205 { pageno = n
1206 ; pagedimno = pdimno
1207 ; pagew = w
1208 ; pageh = h
1209 ; pagex = pagex
1210 ; pagey = pagey
1211 ; pagevw = pagevw
1212 ; pagevh = pagevh
1213 ; pagedispx = pagedispx
1214 ; pagedispy = pagedispy
1215 ; pagecol = 0
1218 e :: accu
1219 else
1220 accu
1221 else
1222 accu
1224 fold accu (n+1)
1226 List.rev (fold [] (page_of_y y));
1229 let layoutS (columns, b) y sh =
1230 let sh = sh - state.hscrollh in
1231 let rec fold accu n =
1232 if n = Array.length b
1233 then accu
1234 else
1235 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1236 if (vy - y) > sh
1237 then accu
1238 else
1239 let accu =
1240 if vy + pageh > y
1241 then
1242 let x = xoff + state.x in
1243 let pagey = max 0 (y - vy) in
1244 let pagedispy = if pagey > 0 then 0 else vy - y in
1245 let pagedispx, pagex =
1246 if px = 0
1247 then (
1248 if x < 0
1249 then 0, -x
1250 else x, 0
1252 else (
1253 let px = px - x in
1254 if px < 0
1255 then -px, 0
1256 else 0, px
1259 let pagecolw = pagew/columns in
1260 let pagedispx =
1261 if pagecolw < conf.winw
1262 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1263 else pagedispx
1265 let pagevw =
1266 let vw = conf.winw - pagedispx - state.scrollw in
1267 let pw = pagew - pagex in
1268 min vw pw
1270 let pagevw = min pagevw pagecolw in
1271 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1272 if pagevw > 0 && pagevh > 0
1273 then
1274 let e =
1275 { pageno = n/columns
1276 ; pagedimno = pdimno
1277 ; pagew = pagew
1278 ; pageh = pageh
1279 ; pagex = pagex
1280 ; pagey = pagey
1281 ; pagevw = pagevw
1282 ; pagevh = pagevh
1283 ; pagedispx = pagedispx
1284 ; pagedispy = pagedispy
1285 ; pagecol = n mod columns
1288 e :: accu
1289 else
1290 accu
1291 else
1292 accu
1294 fold accu (n+1)
1296 List.rev (fold [] 0)
1299 let layout y sh =
1300 if nogeomcmds state.geomcmds
1301 then
1302 match conf.columns with
1303 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1304 | Cmulti c -> layoutN c y sh
1305 | Csplit s -> layoutS s y sh
1306 else []
1309 let clamp incr =
1310 let y = state.y + incr in
1311 let y = max 0 y in
1312 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1316 let itertiles l f =
1317 let tilex = l.pagex mod conf.tilew in
1318 let tiley = l.pagey mod conf.tileh in
1320 let col = l.pagex / conf.tilew in
1321 let row = l.pagey / conf.tileh in
1323 let rec rowloop row y0 dispy h =
1324 if h = 0
1325 then ()
1326 else (
1327 let dh = conf.tileh - y0 in
1328 let dh = min h dh in
1329 let rec colloop col x0 dispx w =
1330 if w = 0
1331 then ()
1332 else (
1333 let dw = conf.tilew - x0 in
1334 let dw = min w dw in
1336 f col row dispx dispy x0 y0 dw dh;
1337 colloop (col+1) 0 (dispx+dw) (w-dw)
1340 colloop col tilex l.pagedispx l.pagevw;
1341 rowloop (row+1) 0 (dispy+dh) (h-dh)
1344 if l.pagevw > 0 && l.pagevh > 0
1345 then rowloop row tiley l.pagedispy l.pagevh;
1348 let gettileopaque l col row =
1349 let key =
1350 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1352 try Some (Hashtbl.find state.tilemap key)
1353 with Not_found -> None
1356 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1357 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1358 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1361 let drawtiles l color =
1362 GlDraw.color color;
1363 let f col row x y tilex tiley w h =
1364 match gettileopaque l col row with
1365 | Some (opaque, _, t) ->
1366 let params = x, y, w, h, tilex, tiley in
1367 if conf.invert
1368 then (
1369 Gl.enable `blend;
1370 GlFunc.blend_func `zero `one_minus_src_color;
1372 drawtile params opaque;
1373 if conf.invert
1374 then Gl.disable `blend;
1375 if conf.debug
1376 then (
1377 let s = Printf.sprintf
1378 "%d[%d,%d] %f sec"
1379 l.pageno col row t
1381 let w = measurestr fstate.fontsize s in
1382 GlMisc.push_attrib [`current];
1383 GlDraw.color (0.0, 0.0, 0.0);
1384 GlDraw.rect
1385 (float (x-2), float (y-2))
1386 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1387 GlDraw.color (1.0, 1.0, 1.0);
1388 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1389 GlMisc.pop_attrib ();
1392 | _ ->
1393 let w =
1394 let lw = conf.winw - state.scrollw - x in
1395 min lw w
1396 and h =
1397 let lh = conf.winh - y in
1398 min lh h
1400 begin match state.texid with
1401 | Some id ->
1402 Gl.enable `texture_2d;
1403 GlTex.bind_texture `texture_2d id;
1404 let x0 = float x
1405 and y0 = float y
1406 and x1 = float (x+w)
1407 and y1 = float (y+h) in
1409 let tw = float w /. 64.0
1410 and th = float h /. 64.0 in
1411 let tx0 = float tilex /. 64.0
1412 and ty0 = float tiley /. 64.0 in
1413 let tx1 = tx0 +. tw
1414 and ty1 = ty0 +. th in
1415 GlDraw.begins `quads;
1416 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1417 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1418 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1419 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1420 GlDraw.ends ();
1422 Gl.disable `texture_2d;
1423 | None ->
1424 GlDraw.color (1.0, 1.0, 1.0);
1425 GlDraw.rect
1426 (float x, float y)
1427 (float (x+w), float (y+h));
1428 end;
1429 if w > 128 && h > fstate.fontsize + 10
1430 then (
1431 GlDraw.color (0.0, 0.0, 0.0);
1432 let c, r =
1433 if conf.verbose
1434 then (col*conf.tilew, row*conf.tileh)
1435 else col, row
1437 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1439 GlDraw.color color;
1441 itertiles l f
1444 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1446 let tilevisible1 l x y =
1447 let ax0 = l.pagex
1448 and ax1 = l.pagex + l.pagevw
1449 and ay0 = l.pagey
1450 and ay1 = l.pagey + l.pagevh in
1452 let bx0 = x
1453 and by0 = y in
1454 let bx1 = min (bx0 + conf.tilew) l.pagew
1455 and by1 = min (by0 + conf.tileh) l.pageh in
1457 let rx0 = max ax0 bx0
1458 and ry0 = max ay0 by0
1459 and rx1 = min ax1 bx1
1460 and ry1 = min ay1 by1 in
1462 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1463 nonemptyintersection
1466 let tilevisible layout n x y =
1467 let rec findpageinlayout m = function
1468 | l :: rest when l.pageno = n ->
1469 tilevisible1 l x y || (
1470 match conf.columns with
1471 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1472 | _ -> false
1474 | _ :: rest -> findpageinlayout 0 rest
1475 | [] -> false
1477 findpageinlayout 0 layout;
1480 let tileready l x y =
1481 tilevisible1 l x y &&
1482 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1485 let tilepage n p layout =
1486 let rec loop = function
1487 | l :: rest ->
1488 if l.pageno = n
1489 then
1490 let f col row _ _ _ _ _ _ =
1491 if state.currently = Idle
1492 then
1493 match gettileopaque l col row with
1494 | Some _ -> ()
1495 | None ->
1496 let x = col*conf.tilew
1497 and y = row*conf.tileh in
1498 let w =
1499 let w = l.pagew - x in
1500 min w conf.tilew
1502 let h =
1503 let h = l.pageh - y in
1504 min h conf.tileh
1506 wcmd "tile %s %d %d %d %d" p x y w h;
1507 state.currently <-
1508 Tiling (
1509 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1510 conf.tilew, conf.tileh
1513 itertiles l f;
1514 else
1515 loop rest
1517 | [] -> ()
1519 if nogeomcmds state.geomcmds
1520 then loop layout;
1523 let preloadlayout y =
1524 let y = if y < conf.winh then 0 else y - conf.winh in
1525 let h = conf.winh*3 in
1526 layout y h;
1529 let load pages =
1530 let rec loop pages =
1531 if state.currently != Idle
1532 then ()
1533 else
1534 match pages with
1535 | l :: rest ->
1536 begin match getopaque l.pageno with
1537 | None ->
1538 wcmd "page %d %d" l.pageno l.pagedimno;
1539 state.currently <- Loading (l, state.gen);
1540 | Some opaque ->
1541 tilepage l.pageno opaque pages;
1542 loop rest
1543 end;
1544 | _ -> ()
1546 if nogeomcmds state.geomcmds
1547 then loop pages
1550 let preload pages =
1551 load pages;
1552 if conf.preload && state.currently = Idle
1553 then load (preloadlayout state.y);
1556 let layoutready layout =
1557 let rec fold all ls =
1558 all && match ls with
1559 | l :: rest ->
1560 let seen = ref false in
1561 let allvisible = ref true in
1562 let foo col row _ _ _ _ _ _ =
1563 seen := true;
1564 allvisible := !allvisible &&
1565 begin match gettileopaque l col row with
1566 | Some _ -> true
1567 | None -> false
1570 itertiles l foo;
1571 fold (!seen && !allvisible) rest
1572 | [] -> true
1574 let alltilesvisible = fold true layout in
1575 alltilesvisible;
1578 let gotoy y =
1579 let y = bound y 0 state.maxy in
1580 let y, layout, proceed =
1581 match conf.maxwait with
1582 | Some time when state.ghyll == noghyll ->
1583 begin match state.throttle with
1584 | None ->
1585 let layout = layout y conf.winh in
1586 let ready = layoutready layout in
1587 if not ready
1588 then (
1589 load layout;
1590 state.throttle <- Some (layout, y, now ());
1592 else G.postRedisplay "gotoy showall (None)";
1593 y, layout, ready
1594 | Some (_, _, started) ->
1595 let dt = now () -. started in
1596 if dt > time
1597 then (
1598 state.throttle <- None;
1599 let layout = layout y conf.winh in
1600 load layout;
1601 G.postRedisplay "maxwait";
1602 y, layout, true
1604 else -1, [], false
1607 | _ ->
1608 let layout = layout y conf.winh in
1609 if true || layoutready layout
1610 then G.postRedisplay "gotoy ready";
1611 y, layout, true
1613 if proceed
1614 then (
1615 state.y <- y;
1616 state.layout <- layout;
1617 begin match state.mode with
1618 | LinkNav (Ltexact (pageno, linkno)) ->
1619 let rec loop = function
1620 | [] ->
1621 state.mode <- LinkNav (Ltgendir 0)
1622 | l :: _ when l.pageno = pageno ->
1623 begin match getopaque pageno with
1624 | None ->
1625 state.mode <- LinkNav (Ltgendir 0)
1626 | Some opaque ->
1627 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1628 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1629 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1630 then state.mode <- LinkNav (Ltgendir 0)
1632 | _ :: rest -> loop rest
1634 loop layout
1635 | _ -> ()
1636 end;
1637 begin match state.mode with
1638 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1639 if not (pagevisible layout pageno)
1640 then (
1641 match state.layout with
1642 | [] -> ()
1643 | l :: _ ->
1644 state.mode <- Birdseye (
1645 conf, leftx, l.pageno, hooverpageno, anchor
1648 | LinkNav (Ltgendir dir as lt) ->
1649 let linknav =
1650 let rec loop = function
1651 | [] -> lt
1652 | l :: rest ->
1653 match getopaque l.pageno with
1654 | None -> loop rest
1655 | Some opaque ->
1656 let link =
1657 let ld =
1658 if dir = 0
1659 then LDfirstvisible (l.pagex, l.pagey, dir)
1660 else (
1661 if dir > 0 then LDfirst else LDlast
1664 findlink opaque ld
1666 match link with
1667 | Lnotfound -> loop rest
1668 | Lfound n ->
1669 showlinktype (getlink opaque n);
1670 Ltexact (l.pageno, n)
1672 loop state.layout
1674 state.mode <- LinkNav linknav
1675 | _ -> ()
1676 end;
1677 preload layout;
1679 state.ghyll <- noghyll;
1680 if conf.updatecurs
1681 then (
1682 let mx, my = state.mpos in
1683 updateunder mx my;
1687 let conttiling pageno opaque =
1688 tilepage pageno opaque
1689 (if conf.preload then preloadlayout state.y else state.layout)
1692 let gotoy_and_clear_text y =
1693 if not conf.verbose then state.text <- "";
1694 gotoy y;
1697 let getanchor1 l =
1698 let top =
1699 let coloff = l.pagecol * l.pageh in
1700 float (l.pagey + coloff) /. float l.pageh
1702 let dtop =
1703 if l.pagedispy = 0
1704 then
1706 else
1707 if conf.presentation
1708 then float l.pagedispy /. float (calcips l.pageh)
1709 else float l.pagedispy /. float conf.interpagespace
1711 (l.pageno, top, dtop)
1714 let getanchor () =
1715 match state.layout with
1716 | l :: _ -> getanchor1 l
1717 | [] ->
1718 let n = page_of_y state.y in
1719 let y, h = getpageyh n in
1720 let dy = y - state.y in
1721 let dtop =
1722 if conf.presentation
1723 then
1724 let ips = calcips h in
1725 float (dy + ips) /. float ips
1726 else
1727 float dy /. float conf.interpagespace
1729 (n, 0.0, dtop)
1732 let getanchory (n, top, dtop) =
1733 let y, h = getpageyh n in
1734 if conf.presentation
1735 then
1736 let ips = calcips h in
1737 y + truncate (top*.float h -. dtop*.float ips) + ips;
1738 else
1739 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1742 let gotoanchor anchor =
1743 gotoy (getanchory anchor);
1746 let addnav () =
1747 cbput state.hists.nav (getanchor ());
1750 let getnav dir =
1751 let anchor = cbgetc state.hists.nav dir in
1752 getanchory anchor;
1755 let gotoghyll y =
1756 let scroll f n a b =
1757 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1758 let snake f a b =
1759 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1760 if f < a
1761 then s (float f /. float a)
1762 else (
1763 if f > b
1764 then 1.0 -. s ((float (f-b) /. float (n-b)))
1765 else 1.0
1768 snake f a b
1769 and summa f n a b =
1770 (* courtesy:
1771 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1772 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1773 let iv1 = iv f in
1774 let ins = float a *. iv1
1775 and outs = float (n-b) *. iv1 in
1776 let ones = b - a in
1777 ins +. outs +. float ones
1779 let rec set (_N, _A, _B) y sy =
1780 let sum = summa 1.0 _N _A _B in
1781 let dy = float (y - sy) in
1782 state.ghyll <- (
1783 let rec gf n y1 o =
1784 if n >= _N
1785 then state.ghyll <- noghyll
1786 else
1787 let go n =
1788 let s = scroll n _N _A _B in
1789 let y1 = y1 +. ((s *. dy) /. sum) in
1790 gotoy_and_clear_text (truncate y1);
1791 state.ghyll <- gf (n+1) y1;
1793 match o with
1794 | None -> go n
1795 | Some y' -> set (_N/2, 1, 1) y' state.y
1797 gf 0 (float state.y)
1800 match conf.ghyllscroll with
1801 | None ->
1802 gotoy_and_clear_text y
1803 | Some nab ->
1804 if state.ghyll == noghyll
1805 then set nab y state.y
1806 else state.ghyll (Some y)
1809 let gotopage n top =
1810 let y, h = getpageyh n in
1811 let y = y + (truncate (top *. float h)) in
1812 gotoghyll y
1815 let gotopage1 n top =
1816 let y = getpagey n in
1817 let y = y + top in
1818 gotoghyll y
1821 let invalidate s f =
1822 state.layout <- [];
1823 state.pdims <- [];
1824 state.rects <- [];
1825 state.rects1 <- [];
1826 match state.geomcmds with
1827 | ps, [] when String.length ps = 0 ->
1828 f ();
1829 state.geomcmds <- s, [];
1831 | ps, [] ->
1832 state.geomcmds <- ps, [s, f];
1834 | ps, (s', _) :: rest when s' = s ->
1835 state.geomcmds <- ps, ((s, f) :: rest);
1837 | ps, cmds ->
1838 state.geomcmds <- ps, ((s, f) :: cmds);
1841 let opendoc path password =
1842 state.path <- path;
1843 state.password <- password;
1844 state.gen <- state.gen + 1;
1845 state.docinfo <- [];
1847 setaalevel conf.aalevel;
1848 Wsi.settitle ("llpp " ^ Filename.basename path);
1849 wcmd "open %s\000%s\000" path password;
1850 invalidate "reqlayout"
1851 (fun () ->
1852 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1855 let scalecolor c =
1856 let c = c *. conf.colorscale in
1857 (c, c, c);
1860 let scalecolor2 (r, g, b) =
1861 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1864 let docolumns = function
1865 | Csingle _ ->
1866 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1867 let rec loop pageno pdimno pdim y ph pdims =
1868 if pageno = state.pagecount
1869 then ()
1870 else
1871 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1872 match pdims with
1873 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1874 pdimno+1, pdim, rest
1875 | _ ->
1876 pdimno, pdim, pdims
1878 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1879 let y = y +
1880 (if conf.presentation
1881 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1882 else (if pageno = 0 then 0 else conf.interpagespace)
1885 a.(pageno) <- (pdimno, x, y, pdim);
1886 loop (pageno+1) pdimno pdim (y + h) h pdims
1888 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1889 conf.columns <- Csingle a;
1891 | Cmulti ((columns, coverA, coverB), _) ->
1892 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1893 let rec loop pageno pdimno pdim x y rowh pdims =
1894 let rec fixrow m = if m = pageno then () else
1895 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1896 if h < rowh
1897 then (
1898 let y = y + (rowh - h) / 2 in
1899 a.(m) <- (pdimno, x, y, pdim);
1901 fixrow (m+1)
1903 if pageno = state.pagecount
1904 then fixrow (((pageno - 1) / columns) * columns)
1905 else
1906 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1907 match pdims with
1908 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1909 pdimno+1, pdim, rest
1910 | _ ->
1911 pdimno, pdim, pdims
1913 let x, y, rowh' =
1914 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1915 then (
1916 let x = (conf.winw - state.scrollw - w) / 2 in
1917 let ips =
1918 if conf.presentation then calcips h else conf.interpagespace in
1919 x, y + ips + rowh, h
1921 else (
1922 if (pageno - coverA) mod columns = 0
1923 then (
1924 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1925 let y =
1926 if conf.presentation
1927 then
1928 let ips = calcips h in
1929 if pageno = 0
1930 then y + ips
1931 else y + calcips rowh + ips
1932 else
1933 y + (if pageno = 0 then 0 else conf.interpagespace)
1935 x, y + rowh, h
1937 else x, y, max rowh h
1940 if pageno > 1 && (pageno - coverA) mod columns = 0
1941 then fixrow (pageno - columns);
1942 a.(pageno) <- (pdimno, x, y, pdim);
1943 let x = x + w + xoff*2 + conf.interpagespace in
1944 loop (pageno+1) pdimno pdim x y rowh' pdims
1946 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1947 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1949 | Csplit (c, _) ->
1950 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1951 let rec loop pageno pdimno pdim y pdims =
1952 if pageno = state.pagecount
1953 then ()
1954 else
1955 let pdimno, ((_, w, h, _) as pdim), pdims =
1956 match pdims with
1957 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1958 pdimno+1, pdim, rest
1959 | _ ->
1960 pdimno, pdim, pdims
1962 let cw = w / c in
1963 let rec loop1 n x y =
1964 if n = c then y else (
1965 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1966 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1969 let y = loop1 0 0 y in
1970 loop (pageno+1) pdimno pdim y pdims
1972 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1973 conf.columns <- Csplit (c, a);
1976 let represent () =
1977 docolumns conf.columns;
1978 state.maxy <- calcheight ();
1979 state.hscrollh <-
1980 if state.w <= conf.winw - state.scrollw
1981 then 0
1982 else state.scrollw
1984 match state.mode with
1985 | Birdseye (_, _, pageno, _, _) ->
1986 let y, h = getpageyh pageno in
1987 let top = (conf.winh - h) / 2 in
1988 gotoy (max 0 (y - top))
1989 | _ -> gotoanchor state.anchor
1992 let reshape w h =
1993 GlDraw.viewport 0 0 w h;
1994 let firsttime = state.geomcmds == firstgeomcmds in
1995 if not firsttime && nogeomcmds state.geomcmds
1996 then state.anchor <- getanchor ();
1998 conf.winw <- w;
1999 let w = truncate (float w *. conf.zoom) - state.scrollw in
2000 let w = max w 2 in
2001 conf.winh <- h;
2002 setfontsize fstate.fontsize;
2003 GlMat.mode `modelview;
2004 GlMat.load_identity ();
2006 GlMat.mode `projection;
2007 GlMat.load_identity ();
2008 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2009 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2010 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2012 let relx =
2013 if conf.zoom <= 1.0
2014 then 0.0
2015 else float state.x /. float state.w
2017 invalidate "geometry"
2018 (fun () ->
2019 state.w <- w;
2020 if not firsttime
2021 then state.x <- truncate (relx *. float w);
2022 let w =
2023 match conf.columns with
2024 | Csingle _ -> w
2025 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2026 | Csplit (c, _) -> w * c
2028 wcmd "geometry %d %d" w h);
2031 let enttext () =
2032 let len = String.length state.text in
2033 let drawstring s =
2034 let hscrollh =
2035 match state.mode with
2036 | Textentry _
2037 | View ->
2038 let h, _, _ = state.uioh#scrollpw in
2040 | _ -> 0
2042 let rect x w =
2043 GlDraw.rect
2044 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2045 (x+.w, float (conf.winh - hscrollh))
2048 let w = float (conf.winw - state.scrollw - 1) in
2049 if state.progress >= 0.0 && state.progress < 1.0
2050 then (
2051 GlDraw.color (0.3, 0.3, 0.3);
2052 let w1 = w *. state.progress in
2053 rect 0.0 w1;
2054 GlDraw.color (0.0, 0.0, 0.0);
2055 rect w1 (w-.w1)
2057 else (
2058 GlDraw.color (0.0, 0.0, 0.0);
2059 rect 0.0 w;
2062 GlDraw.color (1.0, 1.0, 1.0);
2063 drawstring fstate.fontsize
2064 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2066 let s =
2067 match state.mode with
2068 | Textentry ((prefix, text, _, _, _, _), _) ->
2069 let s =
2070 if len > 0
2071 then
2072 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2073 else
2074 Printf.sprintf "%s%s_" prefix text
2078 | _ -> state.text
2080 let s =
2081 if state.newerrmsgs
2082 then (
2083 if not (istextentry state.mode)
2084 then
2085 let s1 = "(press 'e' to review error messasges)" in
2086 if String.length s > 0 then s ^ " " ^ s1 else s1
2087 else s
2089 else s
2091 if String.length s > 0
2092 then drawstring s
2095 let gctiles () =
2096 let len = Queue.length state.tilelru in
2097 let layout = lazy (
2098 match state.throttle with
2099 | None ->
2100 if conf.preload
2101 then preloadlayout state.y
2102 else state.layout
2103 | Some (layout, _, _) ->
2104 layout
2105 ) in
2106 let rec loop qpos =
2107 if state.memused <= conf.memlimit
2108 then ()
2109 else (
2110 if qpos < len
2111 then
2112 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2113 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2114 let (_, pw, ph, _) = getpagedim n in
2116 gen = state.gen
2117 && colorspace = conf.colorspace
2118 && angle = conf.angle
2119 && pagew = pw
2120 && pageh = ph
2121 && (
2122 let x = col*conf.tilew
2123 and y = row*conf.tileh in
2124 tilevisible (Lazy.force_val layout) n x y
2126 then Queue.push lruitem state.tilelru
2127 else (
2128 wcmd "freetile %s" p;
2129 state.memused <- state.memused - s;
2130 state.uioh#infochanged Memused;
2131 Hashtbl.remove state.tilemap k;
2133 loop (qpos+1)
2136 loop 0
2139 let flushtiles () =
2140 Queue.iter (fun (k, p, s) ->
2141 wcmd "freetile %s" p;
2142 state.memused <- state.memused - s;
2143 state.uioh#infochanged Memused;
2144 Hashtbl.remove state.tilemap k;
2145 ) state.tilelru;
2146 Queue.clear state.tilelru;
2147 load state.layout;
2150 let logcurrently = function
2151 | Idle -> dolog "Idle"
2152 | Loading (l, gen) ->
2153 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2154 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2155 dolog
2156 "Tiling %d[%d,%d] page=%s cs=%s angle"
2157 l.pageno col row pageopaque
2158 (colorspace_to_string colorspace)
2160 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2161 angle gen conf.angle state.gen
2162 tilew tileh
2163 conf.tilew conf.tileh
2165 | Outlining _ ->
2166 dolog "outlining"
2169 let act cmds =
2170 (* dolog "%S" cmds; *)
2171 let op, args =
2172 let spacepos =
2173 try String.index cmds ' '
2174 with Not_found -> -1
2176 if spacepos = -1
2177 then cmds, ""
2178 else
2179 let l = String.length cmds in
2180 let op = String.sub cmds 0 spacepos in
2181 op, begin
2182 if l - spacepos < 2 then ""
2183 else String.sub cmds (spacepos+1) (l-spacepos-1)
2186 match op with
2187 | "clear" ->
2188 state.uioh#infochanged Pdim;
2189 state.pdims <- [];
2191 | "clearrects" ->
2192 state.rects <- state.rects1;
2193 G.postRedisplay "clearrects";
2195 | "continue" ->
2196 let n =
2197 try Scanf.sscanf args "%u" (fun n -> n)
2198 with exn ->
2199 dolog "error processing 'continue' %S: %s"
2200 cmds (Printexc.to_string exn);
2201 exit 1;
2203 state.pagecount <- n;
2204 begin match state.currently with
2205 | Outlining l ->
2206 state.currently <- Idle;
2207 state.outlines <- Array.of_list (List.rev l)
2208 | _ -> ()
2209 end;
2211 let cur, cmds = state.geomcmds in
2212 if String.length cur = 0
2213 then failwith "umpossible";
2215 begin match List.rev cmds with
2216 | [] ->
2217 state.geomcmds <- "", [];
2218 represent ();
2219 | (s, f) :: rest ->
2220 f ();
2221 state.geomcmds <- s, List.rev rest;
2222 end;
2223 if conf.maxwait = None
2224 then G.postRedisplay "continue";
2226 | "title" ->
2227 Wsi.settitle args
2229 | "msg" ->
2230 showtext ' ' args
2232 | "vmsg" ->
2233 if conf.verbose
2234 then showtext ' ' args
2236 | "progress" ->
2237 let progress, text =
2239 Scanf.sscanf args "%f %n"
2240 (fun f pos ->
2241 f, String.sub args pos (String.length args - pos))
2242 with exn ->
2243 dolog "error processing 'progress' %S: %s"
2244 cmds (Printexc.to_string exn);
2245 exit 1;
2247 state.text <- text;
2248 state.progress <- progress;
2249 G.postRedisplay "progress"
2251 | "firstmatch" ->
2252 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2254 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2255 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2256 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2257 with exn ->
2258 dolog "error processing 'firstmatch' %S: %s"
2259 cmds (Printexc.to_string exn);
2260 exit 1;
2262 let y = (getpagey pageno) + truncate y0 in
2263 addnav ();
2264 gotoy y;
2265 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2267 | "match" ->
2268 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2270 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2271 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2272 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2273 with exn ->
2274 dolog "error processing 'match' %S: %s"
2275 cmds (Printexc.to_string exn);
2276 exit 1;
2278 state.rects1 <-
2279 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2281 | "page" ->
2282 let pageopaque, t =
2284 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2285 with exn ->
2286 dolog "error processing 'page' %S: %s"
2287 cmds (Printexc.to_string exn);
2288 exit 1;
2290 begin match state.currently with
2291 | Loading (l, gen) ->
2292 vlog "page %d took %f sec" l.pageno t;
2293 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2294 begin match state.throttle with
2295 | None ->
2296 let preloadedpages =
2297 if conf.preload
2298 then preloadlayout state.y
2299 else state.layout
2301 let evict () =
2302 let module IntSet =
2303 Set.Make (struct type t = int let compare = (-) end) in
2304 let set =
2305 List.fold_left (fun s l -> IntSet.add l.pageno s)
2306 IntSet.empty preloadedpages
2308 let evictedpages =
2309 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2310 if not (IntSet.mem pageno set)
2311 then (
2312 wcmd "freepage %s" opaque;
2313 key :: accu
2315 else accu
2316 ) state.pagemap []
2318 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2320 evict ();
2321 state.currently <- Idle;
2322 if gen = state.gen
2323 then (
2324 tilepage l.pageno pageopaque state.layout;
2325 load state.layout;
2326 load preloadedpages;
2327 if pagevisible state.layout l.pageno
2328 && layoutready state.layout
2329 then G.postRedisplay "page";
2332 | Some (layout, _, _) ->
2333 state.currently <- Idle;
2334 tilepage l.pageno pageopaque layout;
2335 load state.layout
2336 end;
2338 | _ ->
2339 dolog "Inconsistent loading state";
2340 logcurrently state.currently;
2341 exit 1
2344 | "tile" ->
2345 let (x, y, opaque, size, t) =
2347 Scanf.sscanf args "%u %u %s %u %f"
2348 (fun x y p size t -> (x, y, p, size, t))
2349 with exn ->
2350 dolog "error processing 'tile' %S: %s"
2351 cmds (Printexc.to_string exn);
2352 exit 1;
2354 begin match state.currently with
2355 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2356 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2358 if tilew != conf.tilew || tileh != conf.tileh
2359 then (
2360 wcmd "freetile %s" opaque;
2361 state.currently <- Idle;
2362 load state.layout;
2364 else (
2365 puttileopaque l col row gen cs angle opaque size t;
2366 state.memused <- state.memused + size;
2367 state.uioh#infochanged Memused;
2368 gctiles ();
2369 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2370 opaque, size) state.tilelru;
2372 let layout =
2373 match state.throttle with
2374 | None -> state.layout
2375 | Some (layout, _, _) -> layout
2378 state.currently <- Idle;
2379 if gen = state.gen
2380 && conf.colorspace = cs
2381 && conf.angle = angle
2382 && tilevisible layout l.pageno x y
2383 then conttiling l.pageno pageopaque;
2385 begin match state.throttle with
2386 | None ->
2387 preload state.layout;
2388 if gen = state.gen
2389 && conf.colorspace = cs
2390 && conf.angle = angle
2391 && tilevisible state.layout l.pageno x y
2392 then G.postRedisplay "tile nothrottle";
2394 | Some (layout, y, _) ->
2395 let ready = layoutready layout in
2396 if ready
2397 then (
2398 state.y <- y;
2399 state.layout <- layout;
2400 state.throttle <- None;
2401 G.postRedisplay "throttle";
2403 else load layout;
2404 end;
2407 | _ ->
2408 dolog "Inconsistent tiling state";
2409 logcurrently state.currently;
2410 exit 1
2413 | "pdim" ->
2414 let pdim =
2416 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2417 with exn ->
2418 dolog "error processing 'pdim' %S: %s"
2419 cmds (Printexc.to_string exn);
2420 exit 1;
2422 state.uioh#infochanged Pdim;
2423 state.pdims <- pdim :: state.pdims
2425 | "o" ->
2426 let (l, n, t, h, pos) =
2428 Scanf.sscanf args "%u %u %d %u %n"
2429 (fun l n t h pos -> l, n, t, h, pos)
2430 with exn ->
2431 dolog "error processing 'o' %S: %s"
2432 cmds (Printexc.to_string exn);
2433 exit 1;
2435 let s = String.sub args pos (String.length args - pos) in
2436 let outline = (s, l, (n, float t /. float h, 0.0)) in
2437 begin match state.currently with
2438 | Outlining outlines ->
2439 state.currently <- Outlining (outline :: outlines)
2440 | Idle ->
2441 state.currently <- Outlining [outline]
2442 | currently ->
2443 dolog "invalid outlining state";
2444 logcurrently currently
2447 | "info" ->
2448 state.docinfo <- (1, args) :: state.docinfo
2450 | "infoend" ->
2451 state.uioh#infochanged Docinfo;
2452 state.docinfo <- List.rev state.docinfo
2454 | _ ->
2455 dolog "unknown cmd `%S'" cmds
2458 let onhist cb =
2459 let rc = cb.rc in
2460 let action = function
2461 | HCprev -> cbget cb ~-1
2462 | HCnext -> cbget cb 1
2463 | HCfirst -> cbget cb ~-(cb.rc)
2464 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2465 and cancel () = cb.rc <- rc
2466 in (action, cancel)
2469 let search pattern forward =
2470 if String.length pattern > 0
2471 then
2472 let pn, py =
2473 match state.layout with
2474 | [] -> 0, 0
2475 | l :: _ ->
2476 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2478 wcmd "search %d %d %d %d,%s\000"
2479 (btod conf.icase) pn py (btod forward) pattern;
2482 let intentry text key =
2483 let c =
2484 if key >= 32 && key < 127
2485 then Char.chr key
2486 else '\000'
2488 match c with
2489 | '0' .. '9' ->
2490 let text = addchar text c in
2491 TEcont text
2493 | _ ->
2494 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2495 TEcont text
2498 let linknentry text key =
2499 let c =
2500 if key >= 32 && key < 127
2501 then Char.chr key
2502 else '\000'
2504 match c with
2505 | 'a' .. 'z' ->
2506 let text = addchar text c in
2507 TEcont text
2509 | _ ->
2510 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2511 TEcont text
2514 let linkndone f s =
2515 if String.length s > 0
2516 then (
2517 let n =
2518 let l = String.length s in
2519 let rec loop pos n = if pos = l then n else
2520 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2521 loop (pos+1) (n*26 + m)
2522 in loop 0 0
2524 let rec loop n = function
2525 | [] -> ()
2526 | l :: rest ->
2527 match getopaque l.pageno with
2528 | None -> loop n rest
2529 | Some opaque ->
2530 let m = getlinkcount opaque in
2531 if n < m
2532 then (
2533 let under = getlink opaque n in
2534 f under
2536 else loop (n-m) rest
2538 loop n state.layout;
2542 let textentry text key =
2543 if key land 0xff00 = 0xff00
2544 then TEcont text
2545 else TEcont (text ^ Wsi.toutf8 key)
2548 let reqlayout angle proportional =
2549 match state.throttle with
2550 | None ->
2551 if nogeomcmds state.geomcmds
2552 then state.anchor <- getanchor ();
2553 conf.angle <- angle mod 360;
2554 if conf.angle != 0
2555 then (
2556 match state.mode with
2557 | LinkNav _ -> state.mode <- View
2558 | _ -> ()
2560 conf.proportional <- proportional;
2561 invalidate "reqlayout"
2562 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2563 | _ -> ()
2566 let settrim trimmargins trimfuzz =
2567 if nogeomcmds state.geomcmds
2568 then state.anchor <- getanchor ();
2569 conf.trimmargins <- trimmargins;
2570 conf.trimfuzz <- trimfuzz;
2571 let x0, y0, x1, y1 = trimfuzz in
2572 invalidate "settrim"
2573 (fun () ->
2574 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2575 Hashtbl.iter (fun _ opaque ->
2576 wcmd "freepage %s" opaque;
2577 ) state.pagemap;
2578 Hashtbl.clear state.pagemap;
2581 let setzoom zoom =
2582 match state.throttle with
2583 | None ->
2584 let zoom = max 0.01 zoom in
2585 if zoom <> conf.zoom
2586 then (
2587 state.prevzoom <- conf.zoom;
2588 conf.zoom <- zoom;
2589 reshape conf.winw conf.winh;
2590 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2593 | Some (layout, y, started) ->
2594 let time =
2595 match conf.maxwait with
2596 | None -> 0.0
2597 | Some t -> t
2599 let dt = now () -. started in
2600 if dt > time
2601 then (
2602 state.y <- y;
2603 load layout;
2607 let setcolumns mode columns coverA coverB =
2608 state.prevcolumns <- Some (conf.columns, conf.zoom);
2609 if columns < 0
2610 then (
2611 if isbirdseye mode
2612 then showtext '!' "split mode doesn't work in bird's eye"
2613 else (
2614 conf.columns <- Csplit (-columns, [||]);
2615 state.x <- 0;
2616 conf.zoom <- 1.0;
2619 else (
2620 if columns < 2
2621 then (
2622 conf.columns <- Csingle [||];
2623 state.x <- 0;
2624 setzoom 1.0;
2626 else (
2627 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2628 conf.zoom <- 1.0;
2631 reshape conf.winw conf.winh;
2634 let enterbirdseye () =
2635 let zoom = float conf.thumbw /. float conf.winw in
2636 let birdseyepageno =
2637 let cy = conf.winh / 2 in
2638 let fold = function
2639 | [] -> 0
2640 | l :: rest ->
2641 let rec fold best = function
2642 | [] -> best.pageno
2643 | l :: rest ->
2644 let d = cy - (l.pagedispy + l.pagevh/2)
2645 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2646 if abs d < abs dbest
2647 then fold l rest
2648 else best.pageno
2649 in fold l rest
2651 fold state.layout
2653 state.mode <- Birdseye (
2654 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2656 conf.zoom <- zoom;
2657 conf.presentation <- false;
2658 conf.interpagespace <- 10;
2659 conf.hlinks <- false;
2660 state.x <- 0;
2661 state.mstate <- Mnone;
2662 conf.maxwait <- None;
2663 conf.columns <- (
2664 match conf.beyecolumns with
2665 | Some c ->
2666 conf.zoom <- 1.0;
2667 Cmulti ((c, 0, 0), [||])
2668 | None -> Csingle [||]
2670 Wsi.setcursor Wsi.CURSOR_INHERIT;
2671 if conf.verbose
2672 then
2673 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2674 (100.0*.zoom)
2675 else
2676 state.text <- ""
2678 reshape conf.winw conf.winh;
2681 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2682 state.mode <- View;
2683 conf.zoom <- c.zoom;
2684 conf.presentation <- c.presentation;
2685 conf.interpagespace <- c.interpagespace;
2686 conf.maxwait <- c.maxwait;
2687 conf.hlinks <- c.hlinks;
2688 conf.beyecolumns <- (
2689 match conf.columns with
2690 | Cmulti ((c, _, _), _) -> Some c
2691 | Csingle _ -> None
2692 | Csplit _ -> failwith "leaving bird's eye split mode"
2694 conf.columns <- (
2695 match c.columns with
2696 | Cmulti (c, _) -> Cmulti (c, [||])
2697 | Csingle _ -> Csingle [||]
2698 | Csplit (c, _) -> Csplit (c, [||])
2700 state.x <- leftx;
2701 if conf.verbose
2702 then
2703 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2704 (100.0*.conf.zoom)
2706 reshape conf.winw conf.winh;
2707 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2710 let togglebirdseye () =
2711 match state.mode with
2712 | Birdseye vals -> leavebirdseye vals true
2713 | View -> enterbirdseye ()
2714 | _ -> ()
2717 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2718 let pageno = max 0 (pageno - incr) in
2719 let rec loop = function
2720 | [] -> gotopage1 pageno 0
2721 | l :: _ when l.pageno = pageno ->
2722 if l.pagedispy >= 0 && l.pagey = 0
2723 then G.postRedisplay "upbirdseye"
2724 else gotopage1 pageno 0
2725 | _ :: rest -> loop rest
2727 loop state.layout;
2728 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2731 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2732 let pageno = min (state.pagecount - 1) (pageno + incr) in
2733 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2734 let rec loop = function
2735 | [] ->
2736 let y, h = getpageyh pageno in
2737 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2738 gotoy (clamp dy)
2739 | l :: _ when l.pageno = pageno ->
2740 if l.pagevh != l.pageh
2741 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2742 else G.postRedisplay "downbirdseye"
2743 | _ :: rest -> loop rest
2745 loop state.layout
2748 let optentry mode _ key =
2749 let btos b = if b then "on" else "off" in
2750 if key >= 32 && key < 127
2751 then
2752 let c = Char.chr key in
2753 match c with
2754 | 's' ->
2755 let ondone s =
2756 try conf.scrollstep <- int_of_string s with exc ->
2757 state.text <- Printf.sprintf "bad integer `%s': %s"
2758 s (Printexc.to_string exc)
2760 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2762 | 'A' ->
2763 let ondone s =
2765 conf.autoscrollstep <- int_of_string s;
2766 if state.autoscroll <> None
2767 then state.autoscroll <- Some conf.autoscrollstep
2768 with exc ->
2769 state.text <- Printf.sprintf "bad integer `%s': %s"
2770 s (Printexc.to_string exc)
2772 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2774 | 'C' ->
2775 let ondone s =
2777 let n, a, b = multicolumns_of_string s in
2778 setcolumns mode n a b;
2779 with exc ->
2780 state.text <- Printf.sprintf "bad columns `%s': %s"
2781 s (Printexc.to_string exc)
2783 TEswitch ("columns: ", "", None, textentry, ondone, true)
2785 | 'Z' ->
2786 let ondone s =
2788 let zoom = float (int_of_string s) /. 100.0 in
2789 setzoom zoom
2790 with exc ->
2791 state.text <- Printf.sprintf "bad integer `%s': %s"
2792 s (Printexc.to_string exc)
2794 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2796 | 't' ->
2797 let ondone s =
2799 conf.thumbw <- bound (int_of_string s) 2 4096;
2800 state.text <-
2801 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2802 begin match mode with
2803 | Birdseye beye ->
2804 leavebirdseye beye false;
2805 enterbirdseye ();
2806 | _ -> ();
2808 with exc ->
2809 state.text <- Printf.sprintf "bad integer `%s': %s"
2810 s (Printexc.to_string exc)
2812 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2814 | 'R' ->
2815 let ondone s =
2816 match try
2817 Some (int_of_string s)
2818 with exc ->
2819 state.text <- Printf.sprintf "bad integer `%s': %s"
2820 s (Printexc.to_string exc);
2821 None
2822 with
2823 | Some angle -> reqlayout angle conf.proportional
2824 | None -> ()
2826 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2828 | 'i' ->
2829 conf.icase <- not conf.icase;
2830 TEdone ("case insensitive search " ^ (btos conf.icase))
2832 | 'p' ->
2833 conf.preload <- not conf.preload;
2834 gotoy state.y;
2835 TEdone ("preload " ^ (btos conf.preload))
2837 | 'v' ->
2838 conf.verbose <- not conf.verbose;
2839 TEdone ("verbose " ^ (btos conf.verbose))
2841 | 'd' ->
2842 conf.debug <- not conf.debug;
2843 TEdone ("debug " ^ (btos conf.debug))
2845 | 'h' ->
2846 conf.maxhfit <- not conf.maxhfit;
2847 state.maxy <- calcheight ();
2848 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2850 | 'c' ->
2851 conf.crophack <- not conf.crophack;
2852 TEdone ("crophack " ^ btos conf.crophack)
2854 | 'a' ->
2855 let s =
2856 match conf.maxwait with
2857 | None ->
2858 conf.maxwait <- Some infinity;
2859 "always wait for page to complete"
2860 | Some _ ->
2861 conf.maxwait <- None;
2862 "show placeholder if page is not ready"
2864 TEdone s
2866 | 'f' ->
2867 conf.underinfo <- not conf.underinfo;
2868 TEdone ("underinfo " ^ btos conf.underinfo)
2870 | 'P' ->
2871 conf.savebmarks <- not conf.savebmarks;
2872 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2874 | 'S' ->
2875 let ondone s =
2877 let pageno, py =
2878 match state.layout with
2879 | [] -> 0, 0
2880 | l :: _ ->
2881 l.pageno, l.pagey
2883 conf.interpagespace <- int_of_string s;
2884 docolumns conf.columns;
2885 state.maxy <- calcheight ();
2886 let y = getpagey pageno in
2887 gotoy (y + py)
2888 with exc ->
2889 state.text <- Printf.sprintf "bad integer `%s': %s"
2890 s (Printexc.to_string exc)
2892 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2894 | 'l' ->
2895 reqlayout conf.angle (not conf.proportional);
2896 TEdone ("proportional display " ^ btos conf.proportional)
2898 | 'T' ->
2899 settrim (not conf.trimmargins) conf.trimfuzz;
2900 TEdone ("trim margins " ^ btos conf.trimmargins)
2902 | 'I' ->
2903 conf.invert <- not conf.invert;
2904 TEdone ("invert colors " ^ btos conf.invert)
2906 | 'x' ->
2907 let ondone s =
2908 cbput state.hists.sel s;
2909 conf.selcmd <- s;
2911 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2912 textentry, ondone, true)
2914 | _ ->
2915 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2916 TEstop
2917 else
2918 TEcont state.text
2921 class type lvsource = object
2922 method getitemcount : int
2923 method getitem : int -> (string * int)
2924 method hasaction : int -> bool
2925 method exit :
2926 uioh:uioh ->
2927 cancel:bool ->
2928 active:int ->
2929 first:int ->
2930 pan:int ->
2931 qsearch:string ->
2932 uioh option
2933 method getactive : int
2934 method getfirst : int
2935 method getqsearch : string
2936 method setqsearch : string -> unit
2937 method getpan : int
2938 end;;
2940 class virtual lvsourcebase = object
2941 val mutable m_active = 0
2942 val mutable m_first = 0
2943 val mutable m_qsearch = ""
2944 val mutable m_pan = 0
2945 method getactive = m_active
2946 method getfirst = m_first
2947 method getqsearch = m_qsearch
2948 method getpan = m_pan
2949 method setqsearch s = m_qsearch <- s
2950 end;;
2952 let withoutlastutf8 s =
2953 let len = String.length s in
2954 if len = 0
2955 then s
2956 else
2957 let rec find pos =
2958 if pos = 0
2959 then pos
2960 else
2961 let b = Char.code s.[pos] in
2962 if b land 0b110000 = 0b11000000
2963 then find (pos-1)
2964 else pos-1
2966 let first =
2967 if Char.code s.[len-1] land 0x80 = 0
2968 then len-1
2969 else find (len-1)
2971 String.sub s 0 first;
2974 let textentrykeyboard
2975 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2976 let enttext te =
2977 state.mode <- Textentry (te, onleave);
2978 state.text <- "";
2979 enttext ();
2980 G.postRedisplay "textentrykeyboard enttext";
2982 let histaction cmd =
2983 match opthist with
2984 | None -> ()
2985 | Some (action, _) ->
2986 state.mode <- Textentry (
2987 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2989 G.postRedisplay "textentry histaction"
2991 match key with
2992 | 0xff08 -> (* backspace *)
2993 let s = withoutlastutf8 text in
2994 let len = String.length s in
2995 if cancelonempty && len = 0
2996 then (
2997 onleave Cancel;
2998 G.postRedisplay "textentrykeyboard after cancel";
3000 else (
3001 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3004 | 0xff0d ->
3005 ondone text;
3006 onleave Confirm;
3007 G.postRedisplay "textentrykeyboard after confirm"
3009 | 0xff52 -> histaction HCprev
3010 | 0xff54 -> histaction HCnext
3011 | 0xff50 -> histaction HCfirst
3012 | 0xff57 -> histaction HClast
3014 | 0xff1b -> (* escape*)
3015 if String.length text = 0
3016 then (
3017 begin match opthist with
3018 | None -> ()
3019 | Some (_, onhistcancel) -> onhistcancel ()
3020 end;
3021 onleave Cancel;
3022 state.text <- "";
3023 G.postRedisplay "textentrykeyboard after cancel2"
3025 else (
3026 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3029 | 0xff9f | 0xffff -> () (* delete *)
3031 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3032 begin match onkey text key with
3033 | TEdone text ->
3034 ondone text;
3035 onleave Confirm;
3036 G.postRedisplay "textentrykeyboard after confirm2";
3038 | TEcont text ->
3039 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3041 | TEstop ->
3042 onleave Cancel;
3043 G.postRedisplay "textentrykeyboard after cancel3"
3045 | TEswitch te ->
3046 state.mode <- Textentry (te, onleave);
3047 G.postRedisplay "textentrykeyboard switch";
3048 end;
3050 | _ ->
3051 vlog "unhandled key %s" (Wsi.keyname key)
3054 let firstof first active =
3055 if first > active || abs (first - active) > fstate.maxrows - 1
3056 then max 0 (active - (fstate.maxrows/2))
3057 else first
3060 let calcfirst first active =
3061 if active > first
3062 then
3063 let rows = active - first in
3064 if rows > fstate.maxrows then active - fstate.maxrows else first
3065 else active
3068 let scrollph y maxy =
3069 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3070 let sh = float conf.winh /. sh in
3071 let sh = max sh (float conf.scrollh) in
3073 let percent =
3074 if y = state.maxy
3075 then 1.0
3076 else float y /. float maxy
3078 let position = (float conf.winh -. sh) *. percent in
3080 let position =
3081 if position +. sh > float conf.winh
3082 then float conf.winh -. sh
3083 else position
3085 position, sh;
3088 let coe s = (s :> uioh);;
3090 class listview ~(source:lvsource) ~trusted ~modehash =
3091 object (self)
3092 val m_pan = source#getpan
3093 val m_first = source#getfirst
3094 val m_active = source#getactive
3095 val m_qsearch = source#getqsearch
3096 val m_prev_uioh = state.uioh
3098 method private elemunder y =
3099 let n = y / (fstate.fontsize+1) in
3100 if m_first + n < source#getitemcount
3101 then (
3102 if source#hasaction (m_first + n)
3103 then Some (m_first + n)
3104 else None
3106 else None
3108 method display =
3109 Gl.enable `blend;
3110 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3111 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3112 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3113 GlDraw.color (1., 1., 1.);
3114 Gl.enable `texture_2d;
3115 let fs = fstate.fontsize in
3116 let nfs = fs + 1 in
3117 let ww = fstate.wwidth in
3118 let tabw = 30.0*.ww in
3119 let itemcount = source#getitemcount in
3120 let rec loop row =
3121 if (row - m_first) * nfs > conf.winh
3122 then ()
3123 else (
3124 if row >= 0 && row < itemcount
3125 then (
3126 let (s, level) = source#getitem row in
3127 let y = (row - m_first) * nfs in
3128 let x = 5.0 +. float (level + m_pan) *. ww in
3129 if row = m_active
3130 then (
3131 Gl.disable `texture_2d;
3132 GlDraw.polygon_mode `both `line;
3133 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3134 GlDraw.rect (1., float (y + 1))
3135 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3136 GlDraw.polygon_mode `both `fill;
3137 GlDraw.color (1., 1., 1.);
3138 Gl.enable `texture_2d;
3141 let drawtabularstring s =
3142 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3143 if trusted
3144 then
3145 let tabpos = try String.index s '\t' with Not_found -> -1 in
3146 if tabpos > 0
3147 then
3148 let len = String.length s - tabpos - 1 in
3149 let s1 = String.sub s 0 tabpos
3150 and s2 = String.sub s (tabpos + 1) len in
3151 let nx = drawstr x s1 in
3152 let sw = nx -. x in
3153 let x = x +. (max tabw sw) in
3154 drawstr x s2
3155 else
3156 drawstr x s
3157 else
3158 drawstr x s
3160 let _ = drawtabularstring s in
3161 loop (row+1)
3165 loop m_first;
3166 Gl.disable `blend;
3167 Gl.disable `texture_2d;
3169 method updownlevel incr =
3170 let len = source#getitemcount in
3171 let curlevel =
3172 if m_active >= 0 && m_active < len
3173 then snd (source#getitem m_active)
3174 else -1
3176 let rec flow i =
3177 if i = len then i-1 else if i = -1 then 0 else
3178 let _, l = source#getitem i in
3179 if l != curlevel then i else flow (i+incr)
3181 let active = flow m_active in
3182 let first = calcfirst m_first active in
3183 G.postRedisplay "outline updownlevel";
3184 {< m_active = active; m_first = first >}
3186 method private key1 key mask =
3187 let set1 active first qsearch =
3188 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3190 let search active pattern incr =
3191 let dosearch re =
3192 let rec loop n =
3193 if n >= 0 && n < source#getitemcount
3194 then (
3195 let s, _ = source#getitem n in
3197 (try ignore (Str.search_forward re s 0); true
3198 with Not_found -> false)
3199 then Some n
3200 else loop (n + incr)
3202 else None
3204 loop active
3207 let re = Str.regexp_case_fold pattern in
3208 dosearch re
3209 with Failure s ->
3210 state.text <- s;
3211 None
3213 let itemcount = source#getitemcount in
3214 let find start incr =
3215 let rec find i =
3216 if i = -1 || i = itemcount
3217 then -1
3218 else (
3219 if source#hasaction i
3220 then i
3221 else find (i + incr)
3224 find start
3226 let set active first =
3227 let first = bound first 0 (itemcount - fstate.maxrows) in
3228 state.text <- "";
3229 coe {< m_active = active; m_first = first >}
3231 let navigate incr =
3232 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3233 let active, first =
3234 let incr1 = if incr > 0 then 1 else -1 in
3235 if isvisible m_first m_active
3236 then
3237 let next =
3238 let next = m_active + incr in
3239 let next =
3240 if next < 0 || next >= itemcount
3241 then -1
3242 else find next incr1
3244 if next = -1 || abs (m_active - next) > fstate.maxrows
3245 then -1
3246 else next
3248 if next = -1
3249 then
3250 let first = m_first + incr in
3251 let first = bound first 0 (itemcount - 1) in
3252 let next =
3253 let next = m_active + incr in
3254 let next = bound next 0 (itemcount - 1) in
3255 find next ~-incr1
3257 let active = if next = -1 then m_active else next in
3258 active, first
3259 else
3260 let first = min next m_first in
3261 let first =
3262 if abs (next - first) > fstate.maxrows
3263 then first + incr
3264 else first
3266 next, first
3267 else
3268 let first = m_first + incr in
3269 let first = bound first 0 (itemcount - 1) in
3270 let active =
3271 let next = m_active + incr in
3272 let next = bound next 0 (itemcount - 1) in
3273 let next = find next incr1 in
3274 let active =
3275 if next = -1 || abs (m_active - first) > fstate.maxrows
3276 then (
3277 let active = if m_active = -1 then next else m_active in
3278 active
3280 else next
3282 if isvisible first active
3283 then active
3284 else -1
3286 active, first
3288 G.postRedisplay "listview navigate";
3289 set active first;
3291 match key with
3292 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3293 let incr = if key = 0x72 then -1 else 1 in
3294 let active, first =
3295 match search (m_active + incr) m_qsearch incr with
3296 | None ->
3297 state.text <- m_qsearch ^ " [not found]";
3298 m_active, m_first
3299 | Some active ->
3300 state.text <- m_qsearch;
3301 active, firstof m_first active
3303 G.postRedisplay "listview ctrl-r/s";
3304 set1 active first m_qsearch;
3306 | 0xff08 -> (* backspace *)
3307 if String.length m_qsearch = 0
3308 then coe self
3309 else (
3310 let qsearch = withoutlastutf8 m_qsearch in
3311 let len = String.length qsearch in
3312 if len = 0
3313 then (
3314 state.text <- "";
3315 G.postRedisplay "listview empty qsearch";
3316 set1 m_active m_first "";
3318 else
3319 let active, first =
3320 match search m_active qsearch ~-1 with
3321 | None ->
3322 state.text <- qsearch ^ " [not found]";
3323 m_active, m_first
3324 | Some active ->
3325 state.text <- qsearch;
3326 active, firstof m_first active
3328 G.postRedisplay "listview backspace qsearch";
3329 set1 active first qsearch
3332 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3333 let pattern = m_qsearch ^ Wsi.toutf8 key in
3334 let active, first =
3335 match search m_active pattern 1 with
3336 | None ->
3337 state.text <- pattern ^ " [not found]";
3338 m_active, m_first
3339 | Some active ->
3340 state.text <- pattern;
3341 active, firstof m_first active
3343 G.postRedisplay "listview qsearch add";
3344 set1 active first pattern;
3346 | 0xff1b -> (* escape *)
3347 state.text <- "";
3348 if String.length m_qsearch = 0
3349 then (
3350 G.postRedisplay "list view escape";
3351 begin
3352 match
3353 source#exit (coe self) true m_active m_first m_pan m_qsearch
3354 with
3355 | None -> m_prev_uioh
3356 | Some uioh -> uioh
3359 else (
3360 G.postRedisplay "list view kill qsearch";
3361 source#setqsearch "";
3362 coe {< m_qsearch = "" >}
3365 | 0xff0d -> (* return *)
3366 state.text <- "";
3367 let self = {< m_qsearch = "" >} in
3368 source#setqsearch "";
3369 let opt =
3370 G.postRedisplay "listview enter";
3371 if m_active >= 0 && m_active < source#getitemcount
3372 then (
3373 source#exit (coe self) false m_active m_first m_pan "";
3375 else (
3376 source#exit (coe self) true m_active m_first m_pan "";
3379 begin match opt with
3380 | None -> m_prev_uioh
3381 | Some uioh -> uioh
3384 | 0xff9f | 0xffff -> (* delete *)
3385 coe self
3387 | 0xff52 -> navigate ~-1 (* up *)
3388 | 0xff54 -> navigate 1 (* down *)
3389 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3390 | 0xff56 -> navigate fstate.maxrows (* next *)
3392 | 0xff53 -> (* right *)
3393 state.text <- "";
3394 G.postRedisplay "listview right";
3395 coe {< m_pan = m_pan - 1 >}
3397 | 0xff51 -> (* left *)
3398 state.text <- "";
3399 G.postRedisplay "listview left";
3400 coe {< m_pan = m_pan + 1 >}
3402 | 0xff50 -> (* home *)
3403 let active = find 0 1 in
3404 G.postRedisplay "listview home";
3405 set active 0;
3407 | 0xff57 -> (* end *)
3408 let first = max 0 (itemcount - fstate.maxrows) in
3409 let active = find (itemcount - 1) ~-1 in
3410 G.postRedisplay "listview end";
3411 set active first;
3413 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3414 coe self
3416 | _ ->
3417 dolog "listview unknown key %#x" key; coe self
3419 method key key mask =
3420 match state.mode with
3421 | Textentry te -> textentrykeyboard key mask te; coe self
3422 | _ -> self#key1 key mask
3424 method button button down x y _ =
3425 let opt =
3426 match button with
3427 | 1 when x > conf.winw - conf.scrollbw ->
3428 G.postRedisplay "listview scroll";
3429 if down
3430 then
3431 let _, position, sh = self#scrollph in
3432 if y > truncate position && y < truncate (position +. sh)
3433 then (
3434 state.mstate <- Mscrolly;
3435 Some (coe self)
3437 else
3438 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3439 let first = truncate (s *. float source#getitemcount) in
3440 let first = min source#getitemcount first in
3441 Some (coe {< m_first = first; m_active = first >})
3442 else (
3443 state.mstate <- Mnone;
3444 Some (coe self);
3446 | 1 when not down ->
3447 begin match self#elemunder y with
3448 | Some n ->
3449 G.postRedisplay "listview click";
3450 source#exit
3451 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3452 | _ ->
3453 Some (coe self)
3455 | n when (n == 4 || n == 5) && not down ->
3456 let len = source#getitemcount in
3457 let first =
3458 if n = 5 && m_first + fstate.maxrows >= len
3459 then
3460 m_first
3461 else
3462 let first = m_first + (if n == 4 then -1 else 1) in
3463 bound first 0 (len - 1)
3465 G.postRedisplay "listview wheel";
3466 Some (coe {< m_first = first >})
3467 | n when (n = 6 || n = 7) && not down ->
3468 let inc = m_first + (if n = 7 then -1 else 1) in
3469 G.postRedisplay "listview hwheel";
3470 Some (coe {< m_pan = m_pan + inc >})
3471 | _ ->
3472 Some (coe self)
3474 match opt with
3475 | None -> m_prev_uioh
3476 | Some uioh -> uioh
3478 method motion _ y =
3479 match state.mstate with
3480 | Mscrolly ->
3481 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3482 let first = truncate (s *. float source#getitemcount) in
3483 let first = min source#getitemcount first in
3484 G.postRedisplay "listview motion";
3485 coe {< m_first = first; m_active = first >}
3486 | _ -> coe self
3488 method pmotion x y =
3489 if x < conf.winw - conf.scrollbw
3490 then
3491 let n =
3492 match self#elemunder y with
3493 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3494 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3496 let o =
3497 if n != m_active
3498 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3499 else self
3501 coe o
3502 else (
3503 Wsi.setcursor Wsi.CURSOR_INHERIT;
3504 coe self
3507 method infochanged _ = ()
3509 method scrollpw = (0, 0.0, 0.0)
3510 method scrollph =
3511 let nfs = fstate.fontsize + 1 in
3512 let y = m_first * nfs in
3513 let itemcount = source#getitemcount in
3514 let maxi = max 0 (itemcount - fstate.maxrows) in
3515 let maxy = maxi * nfs in
3516 let p, h = scrollph y maxy in
3517 conf.scrollbw, p, h
3519 method modehash = modehash
3520 end;;
3522 class outlinelistview ~source =
3523 object (self)
3524 inherit listview
3525 ~source:(source :> lvsource)
3526 ~trusted:false
3527 ~modehash:(findkeyhash conf "outline")
3528 as super
3530 method key key mask =
3531 let calcfirst first active =
3532 if active > first
3533 then
3534 let rows = active - first in
3535 let maxrows =
3536 if String.length state.text = 0
3537 then fstate.maxrows
3538 else fstate.maxrows - 2
3540 if rows > maxrows then active - maxrows else first
3541 else active
3543 let navigate incr =
3544 let active = m_active + incr in
3545 let active = bound active 0 (source#getitemcount - 1) in
3546 let first = calcfirst m_first active in
3547 G.postRedisplay "outline navigate";
3548 coe {< m_active = active; m_first = first >}
3550 let ctrl = Wsi.withctrl mask in
3551 match key with
3552 | 110 when ctrl -> (* ctrl-n *)
3553 source#narrow m_qsearch;
3554 G.postRedisplay "outline ctrl-n";
3555 coe {< m_first = 0; m_active = 0 >}
3557 | 117 when ctrl -> (* ctrl-u *)
3558 source#denarrow;
3559 G.postRedisplay "outline ctrl-u";
3560 state.text <- "";
3561 coe {< m_first = 0; m_active = 0 >}
3563 | 108 when ctrl -> (* ctrl-l *)
3564 let first = m_active - (fstate.maxrows / 2) in
3565 G.postRedisplay "outline ctrl-l";
3566 coe {< m_first = first >}
3568 | 0xff9f | 0xffff -> (* delete *)
3569 source#remove m_active;
3570 G.postRedisplay "outline delete";
3571 let active = max 0 (m_active-1) in
3572 coe {< m_first = firstof m_first active;
3573 m_active = active >}
3575 | 0xff52 -> navigate ~-1 (* up *)
3576 | 0xff54 -> navigate 1 (* down *)
3577 | 0xff55 -> (* prior *)
3578 navigate ~-(fstate.maxrows)
3579 | 0xff56 -> (* next *)
3580 navigate fstate.maxrows
3582 | 0xff53 -> (* [ctrl-]right *)
3583 let o =
3584 if ctrl
3585 then (
3586 G.postRedisplay "outline ctrl right";
3587 {< m_pan = m_pan + 1 >}
3589 else self#updownlevel 1
3591 coe o
3593 | 0xff51 -> (* [ctrl-]left *)
3594 let o =
3595 if ctrl
3596 then (
3597 G.postRedisplay "outline ctrl left";
3598 {< m_pan = m_pan - 1 >}
3600 else self#updownlevel ~-1
3602 coe o
3604 | 0xff50 -> (* home *)
3605 G.postRedisplay "outline home";
3606 coe {< m_first = 0; m_active = 0 >}
3608 | 0xff57 -> (* end *)
3609 let active = source#getitemcount - 1 in
3610 let first = max 0 (active - fstate.maxrows) in
3611 G.postRedisplay "outline end";
3612 coe {< m_active = active; m_first = first >}
3614 | _ -> super#key key mask
3617 let outlinesource usebookmarks =
3618 let empty = [||] in
3619 (object
3620 inherit lvsourcebase
3621 val mutable m_items = empty
3622 val mutable m_orig_items = empty
3623 val mutable m_prev_items = empty
3624 val mutable m_narrow_pattern = ""
3625 val mutable m_hadremovals = false
3627 method getitemcount =
3628 Array.length m_items + (if m_hadremovals then 1 else 0)
3630 method getitem n =
3631 if n == Array.length m_items && m_hadremovals
3632 then
3633 ("[Confirm removal]", 0)
3634 else
3635 let s, n, _ = m_items.(n) in
3636 (s, n)
3638 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3639 ignore (uioh, first, qsearch);
3640 let confrimremoval = m_hadremovals && active = Array.length m_items in
3641 let items =
3642 if String.length m_narrow_pattern = 0
3643 then m_orig_items
3644 else m_items
3646 if not cancel
3647 then (
3648 if not confrimremoval
3649 then(
3650 let _, _, anchor = m_items.(active) in
3651 gotoghyll (getanchory anchor);
3652 m_items <- items;
3654 else (
3655 state.bookmarks <- Array.to_list m_items;
3656 m_orig_items <- m_items;
3659 else m_items <- items;
3660 m_pan <- pan;
3661 None
3663 method hasaction _ = true
3665 method greetmsg =
3666 if Array.length m_items != Array.length m_orig_items
3667 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3668 else ""
3670 method narrow pattern =
3671 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3672 match reopt with
3673 | None -> ()
3674 | Some re ->
3675 let rec loop accu n =
3676 if n = -1
3677 then (
3678 m_narrow_pattern <- pattern;
3679 m_items <- Array.of_list accu
3681 else
3682 let (s, _, _) as o = m_items.(n) in
3683 let accu =
3684 if (try ignore (Str.search_forward re s 0); true
3685 with Not_found -> false)
3686 then o :: accu
3687 else accu
3689 loop accu (n-1)
3691 loop [] (Array.length m_items - 1)
3693 method denarrow =
3694 m_orig_items <- (
3695 if usebookmarks
3696 then Array.of_list state.bookmarks
3697 else state.outlines
3699 m_items <- m_orig_items
3701 method remove m =
3702 if usebookmarks
3703 then
3704 if m >= 0 && m < Array.length m_items
3705 then (
3706 m_hadremovals <- true;
3707 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3708 let n = if n >= m then n+1 else n in
3709 m_items.(n)
3713 method reset anchor items =
3714 m_hadremovals <- false;
3715 if m_orig_items == empty || m_prev_items != items
3716 then (
3717 m_orig_items <- items;
3718 if String.length m_narrow_pattern = 0
3719 then m_items <- items;
3721 m_prev_items <- items;
3722 let rely = getanchory anchor in
3723 let active =
3724 let rec loop n best bestd =
3725 if n = Array.length m_items
3726 then best
3727 else
3728 let (_, _, anchor) = m_items.(n) in
3729 let orely = getanchory anchor in
3730 let d = abs (orely - rely) in
3731 if d < bestd
3732 then loop (n+1) n d
3733 else loop (n+1) best bestd
3735 loop 0 ~-1 max_int
3737 m_active <- active;
3738 m_first <- firstof m_first active
3739 end)
3742 let enterselector usebookmarks =
3743 let source = outlinesource usebookmarks in
3744 fun errmsg ->
3745 let outlines =
3746 if usebookmarks
3747 then Array.of_list state.bookmarks
3748 else state.outlines
3750 if Array.length outlines = 0
3751 then (
3752 showtext ' ' errmsg;
3754 else (
3755 state.text <- source#greetmsg;
3756 Wsi.setcursor Wsi.CURSOR_INHERIT;
3757 let anchor = getanchor () in
3758 source#reset anchor outlines;
3759 state.uioh <- coe (new outlinelistview ~source);
3760 G.postRedisplay "enter selector";
3764 let enteroutlinemode =
3765 let f = enterselector false in
3766 fun ()-> f "Document has no outline";
3769 let enterbookmarkmode =
3770 let f = enterselector true in
3771 fun () -> f "Document has no bookmarks (yet)";
3774 let color_of_string s =
3775 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3776 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3780 let color_to_string (r, g, b) =
3781 let r = truncate (r *. 256.0)
3782 and g = truncate (g *. 256.0)
3783 and b = truncate (b *. 256.0) in
3784 Printf.sprintf "%d/%d/%d" r g b
3787 let irect_of_string s =
3788 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3791 let irect_to_string (x0,y0,x1,y1) =
3792 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3795 let makecheckers () =
3796 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3797 following to say:
3798 converted by Issac Trotts. July 25, 2002 *)
3799 let image_height = 64
3800 and image_width = 64 in
3802 let make_image () =
3803 let image =
3804 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3806 for i = 0 to image_width - 1 do
3807 for j = 0 to image_height - 1 do
3808 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3809 (if (i land 8 ) lxor (j land 8) = 0
3810 then [|255;255;255|] else [|200;200;200|])
3811 done
3812 done;
3813 image
3815 let image = make_image () in
3816 let id = GlTex.gen_texture () in
3817 GlTex.bind_texture `texture_2d id;
3818 GlPix.store (`unpack_alignment 1);
3819 GlTex.image2d image;
3820 List.iter (GlTex.parameter ~target:`texture_2d)
3821 [ `wrap_s `repeat;
3822 `wrap_t `repeat;
3823 `mag_filter `nearest;
3824 `min_filter `nearest ];
3828 let setcheckers enabled =
3829 match state.texid with
3830 | None ->
3831 if enabled then state.texid <- Some (makecheckers ())
3833 | Some texid ->
3834 if not enabled
3835 then (
3836 GlTex.delete_texture texid;
3837 state.texid <- None;
3841 let int_of_string_with_suffix s =
3842 let l = String.length s in
3843 let s1, shift =
3844 if l > 1
3845 then
3846 let suffix = Char.lowercase s.[l-1] in
3847 match suffix with
3848 | 'k' -> String.sub s 0 (l-1), 10
3849 | 'm' -> String.sub s 0 (l-1), 20
3850 | 'g' -> String.sub s 0 (l-1), 30
3851 | _ -> s, 0
3852 else s, 0
3854 let n = int_of_string s1 in
3855 let m = n lsl shift in
3856 if m < 0 || m < n
3857 then raise (Failure "value too large")
3858 else m
3861 let string_with_suffix_of_int n =
3862 if n = 0
3863 then "0"
3864 else
3865 let n, s =
3866 if n land ((1 lsl 20) - 1) = 0
3867 then n lsr 20, "M"
3868 else (
3869 if n land ((1 lsl 10) - 1) = 0
3870 then n lsr 10, "K"
3871 else n, ""
3874 let rec loop s n =
3875 let h = n mod 1000 in
3876 let n = n / 1000 in
3877 if n = 0
3878 then string_of_int h ^ s
3879 else (
3880 let s = Printf.sprintf "_%03d%s" h s in
3881 loop s n
3884 loop "" n ^ s;
3887 let defghyllscroll = (40, 8, 32);;
3888 let ghyllscroll_of_string s =
3889 let (n, a, b) as nab =
3890 if s = "default"
3891 then defghyllscroll
3892 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3894 if n <= a || n <= b || a >= b
3895 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3896 nab;
3899 let ghyllscroll_to_string ((n, a, b) as nab) =
3900 if nab = defghyllscroll
3901 then "default"
3902 else Printf.sprintf "%d,%d,%d" n a b;
3905 let describe_location () =
3906 let f (fn, _) l =
3907 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3909 let fn, ln = List.fold_left f (-1, -1) state.layout in
3910 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3911 let percent =
3912 if maxy <= 0
3913 then 100.
3914 else (100. *. (float state.y /. float maxy))
3916 if fn = ln
3917 then
3918 Printf.sprintf "page %d of %d [%.2f%%]"
3919 (fn+1) state.pagecount percent
3920 else
3921 Printf.sprintf
3922 "pages %d-%d of %d [%.2f%%]"
3923 (fn+1) (ln+1) state.pagecount percent
3926 let setpresentationmode v =
3927 let (n, _, _) = getanchor () in
3928 let _, h = getpageyh n in
3929 let ips = if conf.presentation then calcips h else conf.interpagespace in
3930 state.anchor <- (n, 0.0, float ips);
3931 conf.presentation <- v;
3932 if conf.presentation
3933 then (
3934 if not conf.scrollbarinpm
3935 then state.scrollw <- 0;
3937 else state.scrollw <- conf.scrollbw;
3938 represent ();
3941 let enterinfomode =
3942 let btos b = if b then "\xe2\x88\x9a" else "" in
3943 let showextended = ref false in
3944 let leave mode = function
3945 | Confirm -> state.mode <- mode
3946 | Cancel -> state.mode <- mode in
3947 let src =
3948 (object
3949 val mutable m_first_time = true
3950 val mutable m_l = []
3951 val mutable m_a = [||]
3952 val mutable m_prev_uioh = nouioh
3953 val mutable m_prev_mode = View
3955 inherit lvsourcebase
3957 method reset prev_mode prev_uioh =
3958 m_a <- Array.of_list (List.rev m_l);
3959 m_l <- [];
3960 m_prev_mode <- prev_mode;
3961 m_prev_uioh <- prev_uioh;
3962 if m_first_time
3963 then (
3964 let rec loop n =
3965 if n >= Array.length m_a
3966 then ()
3967 else
3968 match m_a.(n) with
3969 | _, _, _, Action _ -> m_active <- n
3970 | _ -> loop (n+1)
3972 loop 0;
3973 m_first_time <- false;
3976 method int name get set =
3977 m_l <-
3978 (name, `int get, 1, Action (
3979 fun u ->
3980 let ondone s =
3981 try set (int_of_string s)
3982 with exn ->
3983 state.text <- Printf.sprintf "bad integer `%s': %s"
3984 s (Printexc.to_string exn)
3986 state.text <- "";
3987 let te = name ^ ": ", "", None, intentry, ondone, true in
3988 state.mode <- Textentry (te, leave m_prev_mode);
3990 )) :: m_l
3992 method int_with_suffix name get set =
3993 m_l <-
3994 (name, `intws get, 1, Action (
3995 fun u ->
3996 let ondone s =
3997 try set (int_of_string_with_suffix s)
3998 with exn ->
3999 state.text <- Printf.sprintf "bad integer `%s': %s"
4000 s (Printexc.to_string exn)
4002 state.text <- "";
4003 let te =
4004 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4006 state.mode <- Textentry (te, leave m_prev_mode);
4008 )) :: m_l
4010 method bool ?(offset=1) ?(btos=btos) name get set =
4011 m_l <-
4012 (name, `bool (btos, get), offset, Action (
4013 fun u ->
4014 let v = get () in
4015 set (not v);
4017 )) :: m_l
4019 method color name get set =
4020 m_l <-
4021 (name, `color get, 1, Action (
4022 fun u ->
4023 let invalid = (nan, nan, nan) in
4024 let ondone s =
4025 let c =
4026 try color_of_string s
4027 with exn ->
4028 state.text <- Printf.sprintf "bad color `%s': %s"
4029 s (Printexc.to_string exn);
4030 invalid
4032 if c <> invalid
4033 then set c;
4035 let te = name ^ ": ", "", None, textentry, ondone, true in
4036 state.text <- color_to_string (get ());
4037 state.mode <- Textentry (te, leave m_prev_mode);
4039 )) :: m_l
4041 method string name get set =
4042 m_l <-
4043 (name, `string get, 1, Action (
4044 fun u ->
4045 let ondone s = set s in
4046 let te = name ^ ": ", "", None, textentry, ondone, true in
4047 state.mode <- Textentry (te, leave m_prev_mode);
4049 )) :: m_l
4051 method colorspace name get set =
4052 m_l <-
4053 (name, `string get, 1, Action (
4054 fun _ ->
4055 let source =
4056 let vals = [| "rgb"; "bgr"; "gray" |] in
4057 (object
4058 inherit lvsourcebase
4060 initializer
4061 m_active <- int_of_colorspace conf.colorspace;
4062 m_first <- 0;
4064 method getitemcount = Array.length vals
4065 method getitem n = (vals.(n), 0)
4066 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4067 ignore (uioh, first, pan, qsearch);
4068 if not cancel then set active;
4069 None
4070 method hasaction _ = true
4071 end)
4073 state.text <- "";
4074 let modehash = findkeyhash conf "info" in
4075 coe (new listview ~source ~trusted:true ~modehash)
4076 )) :: m_l
4078 method caption s offset =
4079 m_l <- (s, `empty, offset, Noaction) :: m_l
4081 method caption2 s f offset =
4082 m_l <- (s, `string f, offset, Noaction) :: m_l
4084 method getitemcount = Array.length m_a
4086 method getitem n =
4087 let tostr = function
4088 | `int f -> string_of_int (f ())
4089 | `intws f -> string_with_suffix_of_int (f ())
4090 | `string f -> f ()
4091 | `color f -> color_to_string (f ())
4092 | `bool (btos, f) -> btos (f ())
4093 | `empty -> ""
4095 let name, t, offset, _ = m_a.(n) in
4096 ((let s = tostr t in
4097 if String.length s > 0
4098 then Printf.sprintf "%s\t%s" name s
4099 else name),
4100 offset)
4102 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4103 let uiohopt =
4104 if not cancel
4105 then (
4106 m_qsearch <- qsearch;
4107 let uioh =
4108 match m_a.(active) with
4109 | _, _, _, Action f -> f uioh
4110 | _ -> uioh
4112 Some uioh
4114 else None
4116 m_active <- active;
4117 m_first <- first;
4118 m_pan <- pan;
4119 uiohopt
4121 method hasaction n =
4122 match m_a.(n) with
4123 | _, _, _, Action _ -> true
4124 | _ -> false
4125 end)
4127 let rec fillsrc prevmode prevuioh =
4128 let sep () = src#caption "" 0 in
4129 let colorp name get set =
4130 src#string name
4131 (fun () -> color_to_string (get ()))
4132 (fun v ->
4134 let c = color_of_string v in
4135 set c
4136 with exn ->
4137 state.text <- Printf.sprintf "bad color `%s': %s"
4138 v (Printexc.to_string exn);
4141 let oldmode = state.mode in
4142 let birdseye = isbirdseye state.mode in
4144 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4146 src#bool "presentation mode"
4147 (fun () -> conf.presentation)
4148 (fun v -> setpresentationmode v);
4150 src#bool "ignore case in searches"
4151 (fun () -> conf.icase)
4152 (fun v -> conf.icase <- v);
4154 src#bool "preload"
4155 (fun () -> conf.preload)
4156 (fun v -> conf.preload <- v);
4158 src#bool "highlight links"
4159 (fun () -> conf.hlinks)
4160 (fun v -> conf.hlinks <- v);
4162 src#bool "under info"
4163 (fun () -> conf.underinfo)
4164 (fun v -> conf.underinfo <- v);
4166 src#bool "persistent bookmarks"
4167 (fun () -> conf.savebmarks)
4168 (fun v -> conf.savebmarks <- v);
4170 src#bool "proportional display"
4171 (fun () -> conf.proportional)
4172 (fun v -> reqlayout conf.angle v);
4174 src#bool "trim margins"
4175 (fun () -> conf.trimmargins)
4176 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4178 src#bool "persistent location"
4179 (fun () -> conf.jumpback)
4180 (fun v -> conf.jumpback <- v);
4182 sep ();
4183 src#int "inter-page space"
4184 (fun () -> conf.interpagespace)
4185 (fun n ->
4186 conf.interpagespace <- n;
4187 docolumns conf.columns;
4188 let pageno, py =
4189 match state.layout with
4190 | [] -> 0, 0
4191 | l :: _ ->
4192 l.pageno, l.pagey
4194 state.maxy <- calcheight ();
4195 let y = getpagey pageno in
4196 gotoy (y + py)
4199 src#int "page bias"
4200 (fun () -> conf.pagebias)
4201 (fun v -> conf.pagebias <- v);
4203 src#int "scroll step"
4204 (fun () -> conf.scrollstep)
4205 (fun n -> conf.scrollstep <- n);
4207 src#int "horizontal scroll step"
4208 (fun () -> conf.hscrollstep)
4209 (fun v -> conf.hscrollstep <- v);
4211 src#int "auto scroll step"
4212 (fun () ->
4213 match state.autoscroll with
4214 | Some step -> step
4215 | _ -> conf.autoscrollstep)
4216 (fun n ->
4217 if state.autoscroll <> None
4218 then state.autoscroll <- Some n;
4219 conf.autoscrollstep <- n);
4221 src#int "zoom"
4222 (fun () -> truncate (conf.zoom *. 100.))
4223 (fun v -> setzoom ((float v) /. 100.));
4225 src#int "rotation"
4226 (fun () -> conf.angle)
4227 (fun v -> reqlayout v conf.proportional);
4229 src#int "scroll bar width"
4230 (fun () -> state.scrollw)
4231 (fun v ->
4232 state.scrollw <- v;
4233 conf.scrollbw <- v;
4234 reshape conf.winw conf.winh;
4237 src#int "scroll handle height"
4238 (fun () -> conf.scrollh)
4239 (fun v -> conf.scrollh <- v;);
4241 src#int "thumbnail width"
4242 (fun () -> conf.thumbw)
4243 (fun v ->
4244 conf.thumbw <- min 4096 v;
4245 match oldmode with
4246 | Birdseye beye ->
4247 leavebirdseye beye false;
4248 enterbirdseye ()
4249 | _ -> ()
4252 let mode = state.mode in
4253 src#string "columns"
4254 (fun () ->
4255 match conf.columns with
4256 | Csingle _ -> "1"
4257 | Cmulti (multi, _) -> multicolumns_to_string multi
4258 | Csplit (count, _) -> "-" ^ string_of_int count
4260 (fun v ->
4261 let n, a, b = multicolumns_of_string v in
4262 setcolumns mode n a b);
4264 sep ();
4265 src#caption "Presentation mode" 0;
4266 src#bool "scrollbar visible"
4267 (fun () -> conf.scrollbarinpm)
4268 (fun v ->
4269 if v != conf.scrollbarinpm
4270 then (
4271 conf.scrollbarinpm <- v;
4272 if conf.presentation
4273 then (
4274 state.scrollw <- if v then conf.scrollbw else 0;
4275 reshape conf.winw conf.winh;
4280 sep ();
4281 src#caption "Pixmap cache" 0;
4282 src#int_with_suffix "size (advisory)"
4283 (fun () -> conf.memlimit)
4284 (fun v -> conf.memlimit <- v);
4286 src#caption2 "used"
4287 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4288 (string_with_suffix_of_int state.memused)
4289 (Hashtbl.length state.tilemap)) 1;
4291 sep ();
4292 src#caption "Layout" 0;
4293 src#caption2 "Dimension"
4294 (fun () ->
4295 Printf.sprintf "%dx%d (virtual %dx%d)"
4296 conf.winw conf.winh
4297 state.w state.maxy)
4299 if conf.debug
4300 then
4301 src#caption2 "Position" (fun () ->
4302 Printf.sprintf "%dx%d" state.x state.y
4304 else
4305 src#caption2 "Visible" (fun () -> describe_location ()) 1
4308 sep ();
4309 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4310 "Save these parameters as global defaults at exit"
4311 (fun () -> conf.bedefault)
4312 (fun v -> conf.bedefault <- v)
4315 sep ();
4316 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4317 src#bool ~offset:0 ~btos "Extended parameters"
4318 (fun () -> !showextended)
4319 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4320 if !showextended
4321 then (
4322 src#bool "checkers"
4323 (fun () -> conf.checkers)
4324 (fun v -> conf.checkers <- v; setcheckers v);
4325 src#bool "update cursor"
4326 (fun () -> conf.updatecurs)
4327 (fun v -> conf.updatecurs <- v);
4328 src#bool "verbose"
4329 (fun () -> conf.verbose)
4330 (fun v -> conf.verbose <- v);
4331 src#bool "invert colors"
4332 (fun () -> conf.invert)
4333 (fun v -> conf.invert <- v);
4334 src#bool "max fit"
4335 (fun () -> conf.maxhfit)
4336 (fun v -> conf.maxhfit <- v);
4337 src#bool "redirect stderr"
4338 (fun () -> conf.redirectstderr)
4339 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4340 src#string "uri launcher"
4341 (fun () -> conf.urilauncher)
4342 (fun v -> conf.urilauncher <- v);
4343 src#string "path launcher"
4344 (fun () -> conf.pathlauncher)
4345 (fun v -> conf.pathlauncher <- v);
4346 src#string "tile size"
4347 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4348 (fun v ->
4350 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4351 conf.tilew <- max 64 w;
4352 conf.tileh <- max 64 h;
4353 flushtiles ();
4354 with exn ->
4355 state.text <- Printf.sprintf "bad tile size `%s': %s"
4356 v (Printexc.to_string exn));
4357 src#int "texture count"
4358 (fun () -> conf.texcount)
4359 (fun v ->
4360 if realloctexts v
4361 then conf.texcount <- v
4362 else showtext '!' " Failed to set texture count please retry later"
4364 src#int "slice height"
4365 (fun () -> conf.sliceheight)
4366 (fun v ->
4367 conf.sliceheight <- v;
4368 wcmd "sliceh %d" conf.sliceheight;
4370 src#int "anti-aliasing level"
4371 (fun () -> conf.aalevel)
4372 (fun v ->
4373 conf.aalevel <- bound v 0 8;
4374 state.anchor <- getanchor ();
4375 opendoc state.path state.password;
4377 src#string "page scroll scaling factor"
4378 (fun () -> string_of_float conf.pgscale)
4379 (fun v ->
4381 let s = float_of_string v in
4382 conf.pgscale <- s
4383 with exn ->
4384 state.text <- Printf.sprintf
4385 "bad page scroll scaling factor `%s': %s"
4386 v (Printexc.to_string exn)
4389 src#int "ui font size"
4390 (fun () -> fstate.fontsize)
4391 (fun v -> setfontsize (bound v 5 100));
4392 src#int "hint font size"
4393 (fun () -> conf.hfsize)
4394 (fun v -> conf.hfsize <- bound v 5 100);
4395 colorp "background color"
4396 (fun () -> conf.bgcolor)
4397 (fun v -> conf.bgcolor <- v);
4398 src#bool "crop hack"
4399 (fun () -> conf.crophack)
4400 (fun v -> conf.crophack <- v);
4401 src#string "trim fuzz"
4402 (fun () -> irect_to_string conf.trimfuzz)
4403 (fun v ->
4405 conf.trimfuzz <- irect_of_string v;
4406 if conf.trimmargins
4407 then settrim true conf.trimfuzz;
4408 with exn ->
4409 state.text <- Printf.sprintf "bad irect `%s': %s"
4410 v (Printexc.to_string exn)
4412 src#string "throttle"
4413 (fun () ->
4414 match conf.maxwait with
4415 | None -> "show place holder if page is not ready"
4416 | Some time ->
4417 if time = infinity
4418 then "wait for page to fully render"
4419 else
4420 "wait " ^ string_of_float time
4421 ^ " seconds before showing placeholder"
4423 (fun v ->
4425 let f = float_of_string v in
4426 if f <= 0.0
4427 then conf.maxwait <- None
4428 else conf.maxwait <- Some f
4429 with exn ->
4430 state.text <- Printf.sprintf "bad time `%s': %s"
4431 v (Printexc.to_string exn)
4433 src#string "ghyll scroll"
4434 (fun () ->
4435 match conf.ghyllscroll with
4436 | None -> ""
4437 | Some nab -> ghyllscroll_to_string nab
4439 (fun v ->
4441 let gs =
4442 if String.length v = 0
4443 then None
4444 else Some (ghyllscroll_of_string v)
4446 conf.ghyllscroll <- gs
4447 with exn ->
4448 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4449 v (Printexc.to_string exn)
4451 src#string "selection command"
4452 (fun () -> conf.selcmd)
4453 (fun v -> conf.selcmd <- v);
4454 src#colorspace "color space"
4455 (fun () -> colorspace_to_string conf.colorspace)
4456 (fun v ->
4457 conf.colorspace <- colorspace_of_int v;
4458 wcmd "cs %d" v;
4459 load state.layout;
4463 sep ();
4464 src#caption "Document" 0;
4465 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4466 src#caption2 "Pages"
4467 (fun () -> string_of_int state.pagecount) 1;
4468 src#caption2 "Dimensions"
4469 (fun () -> string_of_int (List.length state.pdims)) 1;
4470 if conf.trimmargins
4471 then (
4472 sep ();
4473 src#caption "Trimmed margins" 0;
4474 src#caption2 "Dimensions"
4475 (fun () -> string_of_int (List.length state.pdims)) 1;
4478 sep ();
4479 src#caption "OpenGL" 0;
4480 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4481 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4482 src#reset prevmode prevuioh;
4484 fun () ->
4485 state.text <- "";
4486 let prevmode = state.mode
4487 and prevuioh = state.uioh in
4488 fillsrc prevmode prevuioh;
4489 let source = (src :> lvsource) in
4490 let modehash = findkeyhash conf "info" in
4491 state.uioh <- coe (object (self)
4492 inherit listview ~source ~trusted:true ~modehash as super
4493 val mutable m_prevmemused = 0
4494 method infochanged = function
4495 | Memused ->
4496 if m_prevmemused != state.memused
4497 then (
4498 m_prevmemused <- state.memused;
4499 G.postRedisplay "memusedchanged";
4501 | Pdim -> G.postRedisplay "pdimchanged"
4502 | Docinfo -> fillsrc prevmode prevuioh
4504 method key key mask =
4505 if not (Wsi.withctrl mask)
4506 then
4507 match key with
4508 | 0xff51 -> coe (self#updownlevel ~-1)
4509 | 0xff53 -> coe (self#updownlevel 1)
4510 | _ -> super#key key mask
4511 else super#key key mask
4512 end);
4513 G.postRedisplay "info";
4516 let enterhelpmode =
4517 let source =
4518 (object
4519 inherit lvsourcebase
4520 method getitemcount = Array.length state.help
4521 method getitem n =
4522 let s, n, _ = state.help.(n) in
4523 (s, n)
4525 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4526 let optuioh =
4527 if not cancel
4528 then (
4529 m_qsearch <- qsearch;
4530 match state.help.(active) with
4531 | _, _, Action f -> Some (f uioh)
4532 | _ -> Some (uioh)
4534 else None
4536 m_active <- active;
4537 m_first <- first;
4538 m_pan <- pan;
4539 optuioh
4541 method hasaction n =
4542 match state.help.(n) with
4543 | _, _, Action _ -> true
4544 | _ -> false
4546 initializer
4547 m_active <- -1
4548 end)
4549 in fun () ->
4550 let modehash = findkeyhash conf "help" in
4551 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4552 G.postRedisplay "help";
4555 let entermsgsmode =
4556 let msgsource =
4557 let re = Str.regexp "[\r\n]" in
4558 (object
4559 inherit lvsourcebase
4560 val mutable m_items = [||]
4562 method getitemcount = 1 + Array.length m_items
4564 method getitem n =
4565 if n = 0
4566 then "[Clear]", 0
4567 else m_items.(n-1), 0
4569 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4570 ignore uioh;
4571 if not cancel
4572 then (
4573 if active = 0
4574 then Buffer.clear state.errmsgs;
4575 m_qsearch <- qsearch;
4577 m_active <- active;
4578 m_first <- first;
4579 m_pan <- pan;
4580 None
4582 method hasaction n =
4583 n = 0
4585 method reset =
4586 state.newerrmsgs <- false;
4587 let l = Str.split re (Buffer.contents state.errmsgs) in
4588 m_items <- Array.of_list l
4590 initializer
4591 m_active <- 0
4592 end)
4593 in fun () ->
4594 state.text <- "";
4595 msgsource#reset;
4596 let source = (msgsource :> lvsource) in
4597 let modehash = findkeyhash conf "listview" in
4598 state.uioh <- coe (object
4599 inherit listview ~source ~trusted:false ~modehash as super
4600 method display =
4601 if state.newerrmsgs
4602 then msgsource#reset;
4603 super#display
4604 end);
4605 G.postRedisplay "msgs";
4608 let quickbookmark ?title () =
4609 match state.layout with
4610 | [] -> ()
4611 | l :: _ ->
4612 let title =
4613 match title with
4614 | None ->
4615 let sec = Unix.gettimeofday () in
4616 let tm = Unix.localtime sec in
4617 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4618 (l.pageno+1)
4619 tm.Unix.tm_mday
4620 tm.Unix.tm_mon
4621 (tm.Unix.tm_year + 1900)
4622 tm.Unix.tm_hour
4623 tm.Unix.tm_min
4624 | Some title -> title
4626 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4629 let doreshape w h =
4630 state.fullscreen <- None;
4631 Wsi.reshape w h;
4634 let setautoscrollspeed step goingdown =
4635 let incr = max 1 ((abs step) / 2) in
4636 let incr = if goingdown then incr else -incr in
4637 let astep = step + incr in
4638 state.autoscroll <- Some astep;
4641 let gotounder = function
4642 | Ulinkgoto (pageno, top) ->
4643 if pageno >= 0
4644 then (
4645 addnav ();
4646 gotopage1 pageno top;
4649 | Ulinkuri s ->
4650 gotouri s
4652 | Uremote (filename, pageno) ->
4653 let path =
4654 if Sys.file_exists filename
4655 then filename
4656 else
4657 let dir = Filename.dirname state.path in
4658 let path = Filename.concat dir filename in
4659 if Sys.file_exists path
4660 then path
4661 else ""
4663 if String.length path > 0
4664 then (
4665 let anchor = getanchor () in
4666 let ranchor = state.path, state.password, anchor in
4667 state.anchor <- (pageno, 0.0, 0.0);
4668 state.ranchors <- ranchor :: state.ranchors;
4669 opendoc path "";
4671 else showtext '!' ("Could not find " ^ filename)
4673 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4676 let canpan () =
4677 match conf.columns with
4678 | Csplit _ -> true
4679 | _ -> conf.zoom > 1.0
4682 let viewkeyboard key mask =
4683 let enttext te =
4684 let mode = state.mode in
4685 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4686 state.text <- "";
4687 enttext ();
4688 G.postRedisplay "view:enttext"
4690 let ctrl = Wsi.withctrl mask in
4691 match key with
4692 | 81 -> (* Q *)
4693 exit 0
4695 | 0xff63 -> (* insert *)
4696 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4697 then (
4698 state.mode <- LinkNav (Ltgendir 0);
4699 gotoy state.y;
4701 else showtext '!' "Keyboard link navigation does not work under rotation"
4703 | 0xff1b | 113 -> (* escape / q *)
4704 begin match state.mstate with
4705 | Mzoomrect _ ->
4706 state.mstate <- Mnone;
4707 Wsi.setcursor Wsi.CURSOR_INHERIT;
4708 G.postRedisplay "kill zoom rect";
4709 | _ ->
4710 match state.ranchors with
4711 | [] -> raise Quit
4712 | (path, password, anchor) :: rest ->
4713 state.ranchors <- rest;
4714 state.anchor <- anchor;
4715 opendoc path password
4716 end;
4718 | 0xff08 -> (* backspace *)
4719 gotoghyll (getnav ~-1)
4721 | 111 -> (* o *)
4722 enteroutlinemode ()
4724 | 117 -> (* u *)
4725 state.rects <- [];
4726 state.text <- "";
4727 G.postRedisplay "dehighlight";
4729 | 47 | 63 -> (* / ? *)
4730 let ondone isforw s =
4731 cbput state.hists.pat s;
4732 state.searchpattern <- s;
4733 search s isforw
4735 let s = String.create 1 in
4736 s.[0] <- Char.chr key;
4737 enttext (s, "", Some (onhist state.hists.pat),
4738 textentry, ondone (key = 47), true)
4740 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4741 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4742 setzoom (conf.zoom +. incr)
4744 | 43 | 0xffab -> (* + *)
4745 let ondone s =
4746 let n =
4747 try int_of_string s with exc ->
4748 state.text <- Printf.sprintf "bad integer `%s': %s"
4749 s (Printexc.to_string exc);
4750 max_int
4752 if n != max_int
4753 then (
4754 conf.pagebias <- n;
4755 state.text <- "page bias is now " ^ string_of_int n;
4758 enttext ("page bias: ", "", None, intentry, ondone, true)
4760 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4761 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4762 setzoom (max 0.01 (conf.zoom -. decr))
4764 | 45 | 0xffad -> (* - *)
4765 let ondone msg = state.text <- msg in
4766 enttext (
4767 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4768 optentry state.mode, ondone, true
4771 | 48 when ctrl -> (* ctrl-0 *)
4772 setzoom 1.0
4774 | 49 when ctrl -> (* ctrl-1 *)
4775 let cols =
4776 match conf.columns with
4777 | Csingle _ | Cmulti _ -> 1
4778 | Csplit (n, _) -> n
4780 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4781 if zoom < 1.0
4782 then setzoom zoom
4784 | 0xffc6 -> (* f9 *)
4785 togglebirdseye ()
4787 | 57 when ctrl -> (* ctrl-9 *)
4788 togglebirdseye ()
4790 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4791 when not ctrl -> (* 0..9 *)
4792 let ondone s =
4793 let n =
4794 try int_of_string s with exc ->
4795 state.text <- Printf.sprintf "bad integer `%s': %s"
4796 s (Printexc.to_string exc);
4799 if n >= 0
4800 then (
4801 addnav ();
4802 cbput state.hists.pag (string_of_int n);
4803 gotopage1 (n + conf.pagebias - 1) 0;
4806 let pageentry text key =
4807 match Char.unsafe_chr key with
4808 | 'g' -> TEdone text
4809 | _ -> intentry text key
4811 let text = "x" in text.[0] <- Char.chr key;
4812 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4814 | 98 -> (* b *)
4815 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4816 reshape conf.winw conf.winh;
4818 | 108 -> (* l *)
4819 conf.hlinks <- not conf.hlinks;
4820 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4821 G.postRedisplay "toggle highlightlinks";
4823 | 70 -> (* F *)
4824 state.glinks <- true;
4825 let mode = state.mode in
4826 state.mode <- Textentry (
4827 (":", "", None, linknentry, linkndone (fun under ->
4828 addnav ();
4829 gotounder under
4830 ), false
4831 ), fun _ ->
4832 state.glinks <- false;
4833 state.mode <- mode
4835 state.text <- "";
4836 G.postRedisplay "view:linkent(F)"
4838 | 121 -> (* y *)
4839 state.glinks <- true;
4840 let mode = state.mode in
4841 state.mode <- Textentry (
4842 (":", "", None, linknentry, linkndone (fun under ->
4843 match Ne.pipe () with
4844 | Ne.Exn exn ->
4845 showtext '!' (Printf.sprintf "pipe failed: %s"
4846 (Printexc.to_string exn));
4847 | Ne.Res (r, w) ->
4848 let popened =
4849 try popen conf.selcmd [r, 0; w, -1]; true
4850 with exn ->
4851 showtext '!'
4852 (Printf.sprintf "failed to execute %s: %s"
4853 conf.selcmd (Printexc.to_string exn));
4854 false
4856 let clo cap fd =
4857 Ne.clo fd (fun msg ->
4858 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4861 let s = undertext under in
4862 if popened
4863 then
4864 (try
4865 let l = String.length s in
4866 let n = Unix.write w s 0 l in
4867 if n != l
4868 then
4869 showtext '!'
4870 (Printf.sprintf
4871 "failed to write %d characters to sel pipe, wrote %d"
4874 with exn ->
4875 showtext '!'
4876 (Printf.sprintf "failed to write to sel pipe: %s"
4877 (Printexc.to_string exn)
4880 else dolog "%s" s;
4881 clo "pipe/r" r;
4882 clo "pipe/w" w;
4883 ), false
4885 fun _ ->
4886 state.glinks <- false;
4887 state.mode <- mode
4889 state.text <- "";
4890 G.postRedisplay "view:linkent"
4892 | 97 -> (* a *)
4893 begin match state.autoscroll with
4894 | Some step ->
4895 conf.autoscrollstep <- step;
4896 state.autoscroll <- None
4897 | None ->
4898 if conf.autoscrollstep = 0
4899 then state.autoscroll <- Some 1
4900 else state.autoscroll <- Some conf.autoscrollstep
4903 | 112 when ctrl -> (* ctrl-p *)
4904 launchpath ()
4906 | 80 -> (* P *)
4907 setpresentationmode (not conf.presentation);
4908 showtext ' ' ("presentation mode " ^
4909 if conf.presentation then "on" else "off");
4911 | 102 -> (* f *)
4912 begin match state.fullscreen with
4913 | None ->
4914 state.fullscreen <- Some (conf.winw, conf.winh);
4915 Wsi.fullscreen ()
4916 | Some (w, h) ->
4917 state.fullscreen <- None;
4918 doreshape w h
4921 | 103 -> (* g *)
4922 gotoy_and_clear_text 0
4924 | 71 -> (* G *)
4925 gotopage1 (state.pagecount - 1) 0
4927 | 112 | 78 -> (* p|N *)
4928 search state.searchpattern false
4930 | 110 | 0xffc0 -> (* n|F3 *)
4931 search state.searchpattern true
4933 | 116 -> (* t *)
4934 begin match state.layout with
4935 | [] -> ()
4936 | l :: _ ->
4937 gotoy_and_clear_text (getpagey l.pageno)
4940 | 32 -> (* space *)
4941 begin match state.layout with
4942 | [] -> ()
4943 | l :: rest ->
4944 match conf.columns with
4945 | Csingle _ ->
4946 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4947 then
4948 let y = clamp (pgscale conf.winh) in
4949 gotoghyll y
4950 else
4951 let pageno = min (l.pageno+1) (state.pagecount-1) in
4952 gotoghyll (getpagey pageno)
4953 | Cmulti ((c, _, _), _) ->
4954 if conf.presentation && l.pageh > l.pagey + l.pagevh
4955 then
4956 let y = clamp (pgscale conf.winh) in
4957 gotoghyll y
4958 else
4959 let pageno = min (l.pageno+c) (state.pagecount-1) in
4960 gotoghyll (getpagey pageno)
4961 | Csplit (n, _) ->
4962 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4963 then
4964 let pagey, pageh = getpageyh l.pageno in
4965 let pagey = pagey + pageh * l.pagecol in
4966 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4967 gotoghyll (pagey + pageh + ips)
4970 | 0xff9f | 0xffff -> (* delete *)
4971 begin match state.layout with
4972 | [] -> ()
4973 | l :: _ ->
4974 match conf.columns with
4975 | Csingle _ ->
4976 if conf.presentation && l.pagey != 0
4977 then
4978 gotoghyll (clamp (pgscale ~-(conf.winh)))
4979 else
4980 let pageno = max 0 (l.pageno-1) in
4981 gotoghyll (getpagey pageno)
4982 | Cmulti ((c, _, coverB), _) ->
4983 let decr =
4984 if l.pageno = state.pagecount - coverB
4985 then 1
4986 else c
4988 let pageno = max 0 (l.pageno-decr) in
4989 gotoghyll (getpagey pageno)
4990 | Csplit (n, _) ->
4991 let y =
4992 if l.pagecol = 0
4993 then
4994 if l.pageno = 0
4995 then l.pagey
4996 else
4997 let pageno = max 0 (l.pageno-1) in
4998 let pagey, pageh = getpageyh pageno in
4999 pagey + (n-1)*pageh
5000 else
5001 let pagey, pageh = getpageyh l.pageno in
5002 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5004 gotoghyll y
5007 | 61 -> (* = *)
5008 showtext ' ' (describe_location ());
5010 | 119 -> (* w *)
5011 begin match state.layout with
5012 | [] -> ()
5013 | l :: _ ->
5014 doreshape (l.pagew + state.scrollw) l.pageh;
5015 G.postRedisplay "w"
5018 | 39 -> (* ' *)
5019 enterbookmarkmode ()
5021 | 104 | 0xffbe -> (* h|F1 *)
5022 enterhelpmode ()
5024 | 105 -> (* i *)
5025 enterinfomode ()
5027 | 101 when conf.redirectstderr -> (* e *)
5028 entermsgsmode ()
5030 | 109 -> (* m *)
5031 let ondone s =
5032 match state.layout with
5033 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5034 | _ -> ()
5036 enttext ("bookmark: ", "", None, textentry, ondone, true)
5038 | 126 -> (* ~ *)
5039 quickbookmark ();
5040 showtext ' ' "Quick bookmark added";
5042 | 122 -> (* z *)
5043 begin match state.layout with
5044 | l :: _ ->
5045 let rect = getpdimrect l.pagedimno in
5046 let w, h =
5047 if conf.crophack
5048 then
5049 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5050 truncate (1.2 *. (rect.(3) -. rect.(0))))
5051 else
5052 (truncate (rect.(1) -. rect.(0)),
5053 truncate (rect.(3) -. rect.(0)))
5055 let w = truncate ((float w)*.conf.zoom)
5056 and h = truncate ((float h)*.conf.zoom) in
5057 if w != 0 && h != 0
5058 then (
5059 state.anchor <- getanchor ();
5060 doreshape (w + state.scrollw) (h + conf.interpagespace)
5062 G.postRedisplay "z";
5064 | [] -> ()
5067 | 50 when ctrl -> (* ctrl-2 *)
5068 let maxw = getmaxw () in
5069 if maxw > 0.0
5070 then setzoom (maxw /. float conf.winw)
5072 | 60 | 62 -> (* < > *)
5073 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5075 | 91 | 93 -> (* [ ] *)
5076 conf.colorscale <-
5077 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5079 G.postRedisplay "brightness";
5081 | 99 when state.mode = View -> (* c *)
5082 let (c, a, b), z =
5083 match state.prevcolumns with
5084 | None -> (1, 0, 0), 1.0
5085 | Some (columns, z) ->
5086 let cab =
5087 match columns with
5088 | Csplit (c, _) -> -c, 0, 0
5089 | Cmulti ((c, a, b), _) -> c, a, b
5090 | Csingle _ -> 1, 0, 0
5092 cab, z
5094 setcolumns View c a b;
5095 setzoom z;
5097 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5098 setzoom state.prevzoom
5100 | 107 | 0xff52 -> (* k up *)
5101 begin match state.autoscroll with
5102 | None ->
5103 begin match state.mode with
5104 | Birdseye beye -> upbirdseye 1 beye
5105 | _ ->
5106 if ctrl
5107 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5108 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5110 | Some n ->
5111 setautoscrollspeed n false
5114 | 106 | 0xff54 -> (* j down *)
5115 begin match state.autoscroll with
5116 | None ->
5117 begin match state.mode with
5118 | Birdseye beye -> downbirdseye 1 beye
5119 | _ ->
5120 if ctrl
5121 then gotoy_and_clear_text (clamp (conf.winh/2))
5122 else gotoy_and_clear_text (clamp conf.scrollstep)
5124 | Some n ->
5125 setautoscrollspeed n true
5128 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5129 if canpan ()
5130 then
5131 let dx =
5132 if ctrl
5133 then conf.winw / 2
5134 else 10
5136 let dx = if key = 0xff51 then dx else -dx in
5137 state.x <- state.x + dx;
5138 gotoy_and_clear_text state.y
5139 else (
5140 state.text <- "";
5141 G.postRedisplay "lef/right"
5144 | 0xff55 -> (* prior *)
5145 let y =
5146 if ctrl
5147 then
5148 match state.layout with
5149 | [] -> state.y
5150 | l :: _ -> state.y - l.pagey
5151 else
5152 clamp (pgscale (-conf.winh))
5154 gotoghyll y
5156 | 0xff56 -> (* next *)
5157 let y =
5158 if ctrl
5159 then
5160 match List.rev state.layout with
5161 | [] -> state.y
5162 | l :: _ -> getpagey l.pageno
5163 else
5164 clamp (pgscale conf.winh)
5166 gotoghyll y
5168 | 0xff50 -> (* home *)
5169 gotoghyll 0
5170 | 0xff57 -> (* end *)
5171 gotoghyll (clamp state.maxy)
5172 | 0xff53 when Wsi.withalt mask -> (* right *)
5173 gotoghyll (getnav ~-1)
5174 | 0xff51 when Wsi.withalt mask -> (* left *)
5175 gotoghyll (getnav 1)
5177 | 114 -> (* r *)
5178 state.anchor <- getanchor ();
5179 opendoc state.path state.password
5181 | 118 when conf.debug -> (* v *)
5182 state.rects <- [];
5183 List.iter (fun l ->
5184 match getopaque l.pageno with
5185 | None -> ()
5186 | Some opaque ->
5187 let x0, y0, x1, y1 = pagebbox opaque in
5188 let a,b = float x0, float y0 in
5189 let c,d = float x1, float y0 in
5190 let e,f = float x1, float y1 in
5191 let h,j = float x0, float y1 in
5192 let rect = (a,b,c,d,e,f,h,j) in
5193 debugrect rect;
5194 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5195 ) state.layout;
5196 G.postRedisplay "v";
5198 | _ ->
5199 vlog "huh? %s" (Wsi.keyname key)
5202 let linknavkeyboard key mask linknav =
5203 let getpage pageno =
5204 let rec loop = function
5205 | [] -> None
5206 | l :: _ when l.pageno = pageno -> Some l
5207 | _ :: rest -> loop rest
5208 in loop state.layout
5210 let doexact (pageno, n) =
5211 match getopaque pageno, getpage pageno with
5212 | Some opaque, Some l ->
5213 if key = 0xff0d
5214 then
5215 let under = getlink opaque n in
5216 G.postRedisplay "link gotounder";
5217 gotounder under;
5218 state.mode <- View;
5219 else
5220 let opt, dir =
5221 match key with
5222 | 0xff50 -> (* home *)
5223 Some (findlink opaque LDfirst), -1
5225 | 0xff57 -> (* end *)
5226 Some (findlink opaque LDlast), 1
5228 | 0xff51 -> (* left *)
5229 Some (findlink opaque (LDleft n)), -1
5231 | 0xff53 -> (* right *)
5232 Some (findlink opaque (LDright n)), 1
5234 | 0xff52 -> (* up *)
5235 Some (findlink opaque (LDup n)), -1
5237 | 0xff54 -> (* down *)
5238 Some (findlink opaque (LDdown n)), 1
5240 | _ -> None, 0
5242 let pwl l dir =
5243 begin match findpwl l.pageno dir with
5244 | Pwlnotfound -> ()
5245 | Pwl pageno ->
5246 let notfound dir =
5247 state.mode <- LinkNav (Ltgendir dir);
5248 let y, h = getpageyh pageno in
5249 let y =
5250 if dir < 0
5251 then y + h - conf.winh
5252 else y
5254 gotoy y
5256 begin match getopaque pageno, getpage pageno with
5257 | Some opaque, Some _ ->
5258 let link =
5259 let ld = if dir > 0 then LDfirst else LDlast in
5260 findlink opaque ld
5262 begin match link with
5263 | Lfound m ->
5264 showlinktype (getlink opaque m);
5265 state.mode <- LinkNav (Ltexact (pageno, m));
5266 G.postRedisplay "linknav jpage";
5267 | _ -> notfound dir
5268 end;
5269 | _ -> notfound dir
5270 end;
5271 end;
5273 begin match opt with
5274 | Some Lnotfound -> pwl l dir;
5275 | Some (Lfound m) ->
5276 if m = n
5277 then pwl l dir
5278 else (
5279 let _, y0, _, y1 = getlinkrect opaque m in
5280 if y0 < l.pagey
5281 then gotopage1 l.pageno y0
5282 else (
5283 let d = fstate.fontsize + 1 in
5284 if y1 - l.pagey > l.pagevh - d
5285 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5286 else G.postRedisplay "linknav";
5288 showlinktype (getlink opaque m);
5289 state.mode <- LinkNav (Ltexact (l.pageno, m));
5292 | None -> viewkeyboard key mask
5293 end;
5294 | _ -> viewkeyboard key mask
5296 if key = 0xff63
5297 then (
5298 state.mode <- View;
5299 G.postRedisplay "leave linknav"
5301 else
5302 match linknav with
5303 | Ltgendir _ -> viewkeyboard key mask
5304 | Ltexact exact -> doexact exact
5307 let keyboard key mask =
5308 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5309 then wcmd "interrupt"
5310 else state.uioh <- state.uioh#key key mask
5313 let birdseyekeyboard key mask
5314 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5315 let incr =
5316 match conf.columns with
5317 | Csingle _ -> 1
5318 | Cmulti ((c, _, _), _) -> c
5319 | Csplit _ -> failwith "bird's eye split mode"
5321 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5322 match key with
5323 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5324 let y, h = getpageyh pageno in
5325 let top = (conf.winh - h) / 2 in
5326 gotoy (max 0 (y - top))
5327 | 0xff0d -> leavebirdseye beye false
5328 | 0xff1b -> leavebirdseye beye true (* escape *)
5329 | 0xff52 -> upbirdseye incr beye (* up *)
5330 | 0xff54 -> downbirdseye incr beye (* down *)
5331 | 0xff51 -> upbirdseye 1 beye (* left *)
5332 | 0xff53 -> downbirdseye 1 beye (* right *)
5334 | 0xff55 -> (* prior *)
5335 begin match state.layout with
5336 | l :: _ ->
5337 if l.pagey != 0
5338 then (
5339 state.mode <- Birdseye (
5340 oconf, leftx, l.pageno, hooverpageno, anchor
5342 gotopage1 l.pageno 0;
5344 else (
5345 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5346 match layout with
5347 | [] -> gotoy (clamp (-conf.winh))
5348 | l :: _ ->
5349 state.mode <- Birdseye (
5350 oconf, leftx, l.pageno, hooverpageno, anchor
5352 gotopage1 l.pageno 0
5355 | [] -> gotoy (clamp (-conf.winh))
5356 end;
5358 | 0xff56 -> (* next *)
5359 begin match List.rev state.layout with
5360 | l :: _ ->
5361 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5362 begin match layout with
5363 | [] ->
5364 let incr = l.pageh - l.pagevh in
5365 if incr = 0
5366 then (
5367 state.mode <-
5368 Birdseye (
5369 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5371 G.postRedisplay "birdseye pagedown";
5373 else gotoy (clamp (incr + conf.interpagespace*2));
5375 | l :: _ ->
5376 state.mode <-
5377 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5378 gotopage1 l.pageno 0;
5381 | [] -> gotoy (clamp conf.winh)
5382 end;
5384 | 0xff50 -> (* home *)
5385 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5386 gotopage1 0 0
5388 | 0xff57 -> (* end *)
5389 let pageno = state.pagecount - 1 in
5390 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5391 if not (pagevisible state.layout pageno)
5392 then
5393 let h =
5394 match List.rev state.pdims with
5395 | [] -> conf.winh
5396 | (_, _, h, _) :: _ -> h
5398 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5399 else G.postRedisplay "birdseye end";
5400 | _ -> viewkeyboard key mask
5403 let drawpage l linkindexbase =
5404 let color =
5405 match state.mode with
5406 | Textentry _ -> scalecolor 0.4
5407 | LinkNav _
5408 | View -> scalecolor 1.0
5409 | Birdseye (_, _, pageno, hooverpageno, _) ->
5410 if l.pageno = hooverpageno
5411 then scalecolor 0.9
5412 else (
5413 if l.pageno = pageno
5414 then scalecolor 1.0
5415 else scalecolor 0.8
5418 drawtiles l color;
5419 begin match getopaque l.pageno with
5420 | Some opaque ->
5421 if tileready l l.pagex l.pagey
5422 then
5423 let x = l.pagedispx - l.pagex
5424 and y = l.pagedispy - l.pagey in
5425 let hlmask =
5426 match conf.columns with
5427 | Csingle _ | Cmulti _ ->
5428 (if conf.hlinks then 1 else 0)
5429 + (if state.glinks
5430 && not (isbirdseye state.mode) then 2 else 0)
5431 | _ -> 0
5433 let s =
5434 match state.mode with
5435 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5436 | _ -> ""
5438 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5439 else 0
5441 | _ -> 0
5442 end;
5445 let scrollindicator () =
5446 let sbw, ph, sh = state.uioh#scrollph in
5447 let sbh, pw, sw = state.uioh#scrollpw in
5449 GlDraw.color (0.64, 0.64, 0.64);
5450 GlDraw.rect
5451 (float (conf.winw - sbw), 0.)
5452 (float conf.winw, float conf.winh)
5454 GlDraw.rect
5455 (0., float (conf.winh - sbh))
5456 (float (conf.winw - state.scrollw - 1), float conf.winh)
5458 GlDraw.color (0.0, 0.0, 0.0);
5460 GlDraw.rect
5461 (float (conf.winw - sbw), ph)
5462 (float conf.winw, ph +. sh)
5464 GlDraw.rect
5465 (pw, float (conf.winh - sbh))
5466 (pw +. sw, float conf.winh)
5470 let showsel () =
5471 match state.mstate with
5472 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5475 | Msel ((x0, y0), (x1, y1)) ->
5476 let rec loop = function
5477 | l :: ls ->
5478 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5479 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5480 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5481 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5482 then
5483 match getopaque l.pageno with
5484 | Some opaque ->
5485 let x0, y0 = pagetranslatepoint l x0 y0 in
5486 let x1, y1 = pagetranslatepoint l x1 y1 in
5487 seltext opaque (x0, y0, x1, y1);
5488 | _ -> ()
5489 else loop ls
5490 | [] -> ()
5492 loop state.layout
5495 let showrects rects =
5496 Gl.enable `blend;
5497 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5498 GlDraw.polygon_mode `both `fill;
5499 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5500 List.iter
5501 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5502 List.iter (fun l ->
5503 if l.pageno = pageno
5504 then (
5505 let dx = float (l.pagedispx - l.pagex) in
5506 let dy = float (l.pagedispy - l.pagey) in
5507 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5508 GlDraw.begins `quads;
5510 GlDraw.vertex2 (x0+.dx, y0+.dy);
5511 GlDraw.vertex2 (x1+.dx, y1+.dy);
5512 GlDraw.vertex2 (x2+.dx, y2+.dy);
5513 GlDraw.vertex2 (x3+.dx, y3+.dy);
5515 GlDraw.ends ();
5517 ) state.layout
5518 ) rects
5520 Gl.disable `blend;
5523 let display () =
5524 GlClear.color (scalecolor2 conf.bgcolor);
5525 GlClear.clear [`color];
5526 let rec loop linkindexbase = function
5527 | l :: rest ->
5528 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5529 loop linkindexbase rest
5530 | [] -> ()
5532 loop 0 state.layout;
5533 let rects =
5534 match state.mode with
5535 | LinkNav (Ltexact (pageno, linkno)) ->
5536 begin match getopaque pageno with
5537 | Some opaque ->
5538 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5539 (pageno, 5, (
5540 float x0, float y0,
5541 float x1, float y0,
5542 float x1, float y1,
5543 float x0, float y1)
5544 ) :: state.rects
5545 | None -> state.rects
5547 | _ -> state.rects
5549 showrects rects;
5550 showsel ();
5551 state.uioh#display;
5552 begin match state.mstate with
5553 | Mzoomrect ((x0, y0), (x1, y1)) ->
5554 Gl.enable `blend;
5555 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5556 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5557 GlDraw.rect (float x0, float y0)
5558 (float x1, float y1);
5559 Gl.disable `blend;
5560 | _ -> ()
5561 end;
5562 enttext ();
5563 scrollindicator ();
5564 Wsi.swapb ();
5567 let zoomrect x y x1 y1 =
5568 let x0 = min x x1
5569 and x1 = max x x1
5570 and y0 = min y y1 in
5571 gotoy (state.y + y0);
5572 state.anchor <- getanchor ();
5573 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5574 let margin =
5575 if state.w < conf.winw - state.scrollw
5576 then (conf.winw - state.scrollw - state.w) / 2
5577 else 0
5579 state.x <- (state.x + margin) - x0;
5580 setzoom zoom;
5581 Wsi.setcursor Wsi.CURSOR_INHERIT;
5582 state.mstate <- Mnone;
5585 let scrollx x =
5586 let winw = conf.winw - state.scrollw - 1 in
5587 let s = float x /. float winw in
5588 let destx = truncate (float (state.w + winw) *. s) in
5589 state.x <- winw - destx;
5590 gotoy_and_clear_text state.y;
5591 state.mstate <- Mscrollx;
5594 let scrolly y =
5595 let s = float y /. float conf.winh in
5596 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5597 gotoy_and_clear_text desty;
5598 state.mstate <- Mscrolly;
5601 let viewmouse button down x y mask =
5602 match button with
5603 | n when (n == 4 || n == 5) && not down ->
5604 if Wsi.withctrl mask
5605 then (
5606 match state.mstate with
5607 | Mzoom (oldn, i) ->
5608 if oldn = n
5609 then (
5610 if i = 2
5611 then
5612 let incr =
5613 match n with
5614 | 5 ->
5615 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5616 | _ ->
5617 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5619 let zoom = conf.zoom -. incr in
5620 setzoom zoom;
5621 state.mstate <- Mzoom (n, 0);
5622 else
5623 state.mstate <- Mzoom (n, i+1);
5625 else state.mstate <- Mzoom (n, 0)
5627 | _ -> state.mstate <- Mzoom (n, 0)
5629 else (
5630 match state.autoscroll with
5631 | Some step -> setautoscrollspeed step (n=4)
5632 | None ->
5633 let incr =
5634 if n = 4
5635 then -conf.scrollstep
5636 else conf.scrollstep
5638 let incr = incr * 2 in
5639 let y = clamp incr in
5640 gotoy_and_clear_text y
5643 | n when (n = 6 || n = 7) && not down && canpan () ->
5644 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5645 gotoy_and_clear_text state.y
5647 | 1 when Wsi.withctrl mask ->
5648 if down
5649 then (
5650 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5651 state.mstate <- Mpan (x, y)
5653 else
5654 state.mstate <- Mnone
5656 | 3 ->
5657 if down
5658 then (
5659 Wsi.setcursor Wsi.CURSOR_CYCLE;
5660 let p = (x, y) in
5661 state.mstate <- Mzoomrect (p, p)
5663 else (
5664 match state.mstate with
5665 | Mzoomrect ((x0, y0), _) ->
5666 if abs (x-x0) > 10 && abs (y - y0) > 10
5667 then zoomrect x0 y0 x y
5668 else (
5669 state.mstate <- Mnone;
5670 Wsi.setcursor Wsi.CURSOR_INHERIT;
5671 G.postRedisplay "kill accidental zoom rect";
5673 | _ ->
5674 Wsi.setcursor Wsi.CURSOR_INHERIT;
5675 state.mstate <- Mnone
5678 | 1 when x > conf.winw - state.scrollw ->
5679 if down
5680 then
5681 let _, position, sh = state.uioh#scrollph in
5682 if y > truncate position && y < truncate (position +. sh)
5683 then state.mstate <- Mscrolly
5684 else scrolly y
5685 else
5686 state.mstate <- Mnone
5688 | 1 when y > conf.winh - state.hscrollh ->
5689 if down
5690 then
5691 let _, position, sw = state.uioh#scrollpw in
5692 if x > truncate position && x < truncate (position +. sw)
5693 then state.mstate <- Mscrollx
5694 else scrollx x
5695 else
5696 state.mstate <- Mnone
5698 | 1 ->
5699 let dest = if down then getunder x y else Unone in
5700 begin match dest with
5701 | Ulinkgoto _
5702 | Ulinkuri _
5703 | Uremote _
5704 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5705 gotounder dest
5707 | Unone when down ->
5708 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5709 state.mstate <- Mpan (x, y);
5711 | Unone | Utext _ ->
5712 if down
5713 then (
5714 if conf.angle mod 360 = 0
5715 then (
5716 state.mstate <- Msel ((x, y), (x, y));
5717 G.postRedisplay "mouse select";
5720 else (
5721 match state.mstate with
5722 | Mnone -> ()
5724 | Mzoom _ | Mscrollx | Mscrolly ->
5725 state.mstate <- Mnone
5727 | Mzoomrect ((x0, y0), _) ->
5728 zoomrect x0 y0 x y
5730 | Mpan _ ->
5731 Wsi.setcursor Wsi.CURSOR_INHERIT;
5732 state.mstate <- Mnone
5734 | Msel ((x0, y0), (x1, y1)) ->
5735 let rec loop = function
5736 | [] -> ()
5737 | l :: rest ->
5738 let inside =
5739 let a0 = l.pagedispy in
5740 let a1 = a0 + l.pagevh in
5741 let b0 = l.pagedispx in
5742 let b1 = b0 + l.pagevw in
5743 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5744 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5746 if inside
5747 then
5748 match getopaque l.pageno with
5749 | Some opaque ->
5750 begin
5751 match Ne.pipe () with
5752 | Ne.Exn exn ->
5753 showtext '!'
5754 (Printf.sprintf
5755 "can not create sel pipe: %s"
5756 (Printexc.to_string exn));
5757 | Ne.Res (r, w) ->
5758 let doclose what fd =
5759 Ne.clo fd (fun msg ->
5760 dolog "%s close failed: %s" what msg)
5763 popen conf.selcmd [r, 0; w, -1];
5764 copysel w opaque;
5765 doclose "pipe/r" r;
5766 G.postRedisplay "copysel";
5767 with exn ->
5768 dolog "can not execute %S: %s"
5769 conf.selcmd (Printexc.to_string exn);
5770 doclose "pipe/r" r;
5771 doclose "pipe/w" w;
5773 | None -> ()
5774 else loop rest
5776 loop state.layout;
5777 Wsi.setcursor Wsi.CURSOR_INHERIT;
5778 state.mstate <- Mnone;
5782 | _ -> ()
5785 let birdseyemouse button down x y mask
5786 (conf, leftx, _, hooverpageno, anchor) =
5787 match button with
5788 | 1 when down ->
5789 let rec loop = function
5790 | [] -> ()
5791 | l :: rest ->
5792 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5793 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5794 then (
5795 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5797 else loop rest
5799 loop state.layout
5800 | 3 -> ()
5801 | _ -> viewmouse button down x y mask
5804 let mouse button down x y mask =
5805 state.uioh <- state.uioh#button button down x y mask;
5808 let motion ~x ~y =
5809 state.uioh <- state.uioh#motion x y
5812 let pmotion ~x ~y =
5813 state.uioh <- state.uioh#pmotion x y;
5816 let uioh = object
5817 method display = ()
5819 method key key mask =
5820 begin match state.mode with
5821 | Textentry textentry -> textentrykeyboard key mask textentry
5822 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5823 | View -> viewkeyboard key mask
5824 | LinkNav linknav -> linknavkeyboard key mask linknav
5825 end;
5826 state.uioh
5828 method button button bstate x y mask =
5829 begin match state.mode with
5830 | LinkNav _
5831 | View -> viewmouse button bstate x y mask
5832 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5833 | Textentry _ -> ()
5834 end;
5835 state.uioh
5837 method motion x y =
5838 begin match state.mode with
5839 | Textentry _ -> ()
5840 | View | Birdseye _ | LinkNav _ ->
5841 match state.mstate with
5842 | Mzoom _ | Mnone -> ()
5844 | Mpan (x0, y0) ->
5845 let dx = x - x0
5846 and dy = y0 - y in
5847 state.mstate <- Mpan (x, y);
5848 if canpan ()
5849 then state.x <- state.x + dx;
5850 let y = clamp dy in
5851 gotoy_and_clear_text y
5853 | Msel (a, _) ->
5854 state.mstate <- Msel (a, (x, y));
5855 G.postRedisplay "motion select";
5857 | Mscrolly ->
5858 let y = min conf.winh (max 0 y) in
5859 scrolly y
5861 | Mscrollx ->
5862 let x = min conf.winw (max 0 x) in
5863 scrollx x
5865 | Mzoomrect (p0, _) ->
5866 state.mstate <- Mzoomrect (p0, (x, y));
5867 G.postRedisplay "motion zoomrect";
5868 end;
5869 state.uioh
5871 method pmotion x y =
5872 begin match state.mode with
5873 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5874 let rec loop = function
5875 | [] ->
5876 if hooverpageno != -1
5877 then (
5878 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5879 G.postRedisplay "pmotion birdseye no hoover";
5881 | l :: rest ->
5882 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5883 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5884 then (
5885 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5886 G.postRedisplay "pmotion birdseye hoover";
5888 else loop rest
5890 loop state.layout
5892 | Textentry _ -> ()
5894 | LinkNav _
5895 | View ->
5896 match state.mstate with
5897 | Mnone -> updateunder x y
5898 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5900 end;
5901 state.uioh
5903 method infochanged _ = ()
5905 method scrollph =
5906 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5907 let p, h = scrollph state.y maxy in
5908 state.scrollw, p, h
5910 method scrollpw =
5911 let winw = conf.winw - state.scrollw - 1 in
5912 let fwinw = float winw in
5913 let sw =
5914 let sw = fwinw /. float state.w in
5915 let sw = fwinw *. sw in
5916 max sw (float conf.scrollh)
5918 let position, sw =
5919 let f = state.w+winw in
5920 let r = float (winw-state.x) /. float f in
5921 let p = fwinw *. r in
5922 p-.sw/.2., sw
5924 let sw =
5925 if position +. sw > fwinw
5926 then fwinw -. position
5927 else sw
5929 state.hscrollh, position, sw
5931 method modehash =
5932 let modename =
5933 match state.mode with
5934 | LinkNav _ -> "links"
5935 | Textentry _ -> "textentry"
5936 | Birdseye _ -> "birdseye"
5937 | View -> "view"
5939 findkeyhash conf modename
5940 end;;
5942 module Config =
5943 struct
5944 open Parser
5946 let fontpath = ref "";;
5948 module KeyMap =
5949 Map.Make (struct type t = (int * int) let compare = compare end);;
5951 let unent s =
5952 let l = String.length s in
5953 let b = Buffer.create l in
5954 unent b s 0 l;
5955 Buffer.contents b;
5958 let home =
5959 try Sys.getenv "HOME"
5960 with exn ->
5961 prerr_endline
5962 ("Can not determine home directory location: " ^
5963 Printexc.to_string exn);
5967 let modifier_of_string = function
5968 | "alt" -> Wsi.altmask
5969 | "shift" -> Wsi.shiftmask
5970 | "ctrl" | "control" -> Wsi.ctrlmask
5971 | "meta" -> Wsi.metamask
5972 | _ -> 0
5975 let key_of_string =
5976 let r = Str.regexp "-" in
5977 fun s ->
5978 let elems = Str.full_split r s in
5979 let f n k m =
5980 let g s =
5981 let m1 = modifier_of_string s in
5982 if m1 = 0
5983 then (Wsi.namekey s, m)
5984 else (k, m lor m1)
5985 in function
5986 | Str.Delim s when n land 1 = 0 -> g s
5987 | Str.Text s -> g s
5988 | Str.Delim _ -> (k, m)
5990 let rec loop n k m = function
5991 | [] -> (k, m)
5992 | x :: xs ->
5993 let k, m = f n k m x in
5994 loop (n+1) k m xs
5996 loop 0 0 0 elems
5999 let keys_of_string =
6000 let r = Str.regexp "[ \t]" in
6001 fun s ->
6002 let elems = Str.split r s in
6003 List.map key_of_string elems
6006 let copykeyhashes c =
6007 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6010 let config_of c attrs =
6011 let apply c k v =
6013 match k with
6014 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6015 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6016 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6017 | "preload" -> { c with preload = bool_of_string v }
6018 | "page-bias" -> { c with pagebias = int_of_string v }
6019 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6020 | "horizontal-scroll-step" ->
6021 { c with hscrollstep = max (int_of_string v) 1 }
6022 | "auto-scroll-step" ->
6023 { c with autoscrollstep = max 0 (int_of_string v) }
6024 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6025 | "crop-hack" -> { c with crophack = bool_of_string v }
6026 | "throttle" ->
6027 let mw =
6028 match String.lowercase v with
6029 | "true" -> Some infinity
6030 | "false" -> None
6031 | f -> Some (float_of_string f)
6033 { c with maxwait = mw}
6034 | "highlight-links" -> { c with hlinks = bool_of_string v }
6035 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6036 | "vertical-margin" ->
6037 { c with interpagespace = max 0 (int_of_string v) }
6038 | "zoom" ->
6039 let zoom = float_of_string v /. 100. in
6040 let zoom = max zoom 0.0 in
6041 { c with zoom = zoom }
6042 | "presentation" -> { c with presentation = bool_of_string v }
6043 | "rotation-angle" -> { c with angle = int_of_string v }
6044 | "width" -> { c with winw = max 20 (int_of_string v) }
6045 | "height" -> { c with winh = max 20 (int_of_string v) }
6046 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6047 | "proportional-display" -> { c with proportional = bool_of_string v }
6048 | "pixmap-cache-size" ->
6049 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6050 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6051 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6052 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6053 | "persistent-location" -> { c with jumpback = bool_of_string v }
6054 | "background-color" -> { c with bgcolor = color_of_string v }
6055 | "scrollbar-in-presentation" ->
6056 { c with scrollbarinpm = bool_of_string v }
6057 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6058 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6059 | "mupdf-store-size" ->
6060 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6061 | "checkers" -> { c with checkers = bool_of_string v }
6062 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6063 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6064 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6065 | "uri-launcher" -> { c with urilauncher = unent v }
6066 | "path-launcher" -> { c with pathlauncher = unent v }
6067 | "color-space" -> { c with colorspace = colorspace_of_string v }
6068 | "invert-colors" -> { c with invert = bool_of_string v }
6069 | "brightness" -> { c with colorscale = float_of_string v }
6070 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6071 | "ghyllscroll" ->
6072 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6073 | "columns" ->
6074 let (n, _, _) as nab = multicolumns_of_string v in
6075 if n < 0
6076 then { c with columns = Csplit (-n, [||]) }
6077 else { c with columns = Cmulti (nab, [||]) }
6078 | "birds-eye-columns" ->
6079 { c with beyecolumns = Some (max (int_of_string v) 2) }
6080 | "selection-command" -> { c with selcmd = unent v }
6081 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6082 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6083 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6084 | _ -> c
6085 with exn ->
6086 prerr_endline ("Error processing attribute (`" ^
6087 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6090 let rec fold c = function
6091 | [] -> c
6092 | (k, v) :: rest ->
6093 let c = apply c k v in
6094 fold c rest
6096 fold { c with keyhashes = copykeyhashes c } attrs;
6099 let fromstring f pos n v d =
6100 try f v
6101 with exn ->
6102 dolog "Error processing attribute (%S=%S) at %d\n%s"
6103 n v pos (Printexc.to_string exn)
6108 let bookmark_of attrs =
6109 let rec fold title page rely visy = function
6110 | ("title", v) :: rest -> fold v page rely visy rest
6111 | ("page", v) :: rest -> fold title v rely visy rest
6112 | ("rely", v) :: rest -> fold title page v visy rest
6113 | ("visy", v) :: rest -> fold title page rely v rest
6114 | _ :: rest -> fold title page rely visy rest
6115 | [] -> title, page, rely, visy
6117 fold "invalid" "0" "0" "0" attrs
6120 let doc_of attrs =
6121 let rec fold path page rely pan visy = function
6122 | ("path", v) :: rest -> fold v page rely pan visy rest
6123 | ("page", v) :: rest -> fold path v rely pan visy rest
6124 | ("rely", v) :: rest -> fold path page v pan visy rest
6125 | ("pan", v) :: rest -> fold path page rely v visy rest
6126 | ("visy", v) :: rest -> fold path page rely pan v rest
6127 | _ :: rest -> fold path page rely pan visy rest
6128 | [] -> path, page, rely, pan, visy
6130 fold "" "0" "0" "0" "0" attrs
6133 let map_of attrs =
6134 let rec fold rs ls = function
6135 | ("out", v) :: rest -> fold v ls rest
6136 | ("in", v) :: rest -> fold rs v rest
6137 | _ :: rest -> fold ls rs rest
6138 | [] -> ls, rs
6140 fold "" "" attrs
6143 let setconf dst src =
6144 dst.scrollbw <- src.scrollbw;
6145 dst.scrollh <- src.scrollh;
6146 dst.icase <- src.icase;
6147 dst.preload <- src.preload;
6148 dst.pagebias <- src.pagebias;
6149 dst.verbose <- src.verbose;
6150 dst.scrollstep <- src.scrollstep;
6151 dst.maxhfit <- src.maxhfit;
6152 dst.crophack <- src.crophack;
6153 dst.autoscrollstep <- src.autoscrollstep;
6154 dst.maxwait <- src.maxwait;
6155 dst.hlinks <- src.hlinks;
6156 dst.underinfo <- src.underinfo;
6157 dst.interpagespace <- src.interpagespace;
6158 dst.zoom <- src.zoom;
6159 dst.presentation <- src.presentation;
6160 dst.angle <- src.angle;
6161 dst.winw <- src.winw;
6162 dst.winh <- src.winh;
6163 dst.savebmarks <- src.savebmarks;
6164 dst.memlimit <- src.memlimit;
6165 dst.proportional <- src.proportional;
6166 dst.texcount <- src.texcount;
6167 dst.sliceheight <- src.sliceheight;
6168 dst.thumbw <- src.thumbw;
6169 dst.jumpback <- src.jumpback;
6170 dst.bgcolor <- src.bgcolor;
6171 dst.scrollbarinpm <- src.scrollbarinpm;
6172 dst.tilew <- src.tilew;
6173 dst.tileh <- src.tileh;
6174 dst.mustoresize <- src.mustoresize;
6175 dst.checkers <- src.checkers;
6176 dst.aalevel <- src.aalevel;
6177 dst.trimmargins <- src.trimmargins;
6178 dst.trimfuzz <- src.trimfuzz;
6179 dst.urilauncher <- src.urilauncher;
6180 dst.colorspace <- src.colorspace;
6181 dst.invert <- src.invert;
6182 dst.colorscale <- src.colorscale;
6183 dst.redirectstderr <- src.redirectstderr;
6184 dst.ghyllscroll <- src.ghyllscroll;
6185 dst.columns <- src.columns;
6186 dst.beyecolumns <- src.beyecolumns;
6187 dst.selcmd <- src.selcmd;
6188 dst.updatecurs <- src.updatecurs;
6189 dst.pathlauncher <- src.pathlauncher;
6190 dst.keyhashes <- copykeyhashes src;
6191 dst.hfsize <- src.hfsize;
6192 dst.hscrollstep <- src.hscrollstep;
6193 dst.pgscale <- src.pgscale;
6196 let get s =
6197 let h = Hashtbl.create 10 in
6198 let dc = { defconf with angle = defconf.angle } in
6199 let rec toplevel v t spos _ =
6200 match t with
6201 | Vdata | Vcdata | Vend -> v
6202 | Vopen ("llppconfig", _, closed) ->
6203 if closed
6204 then v
6205 else { v with f = llppconfig }
6206 | Vopen _ ->
6207 error "unexpected subelement at top level" s spos
6208 | Vclose _ -> error "unexpected close at top level" s spos
6210 and llppconfig v t spos _ =
6211 match t with
6212 | Vdata | Vcdata -> v
6213 | Vend -> error "unexpected end of input in llppconfig" s spos
6214 | Vopen ("defaults", attrs, closed) ->
6215 let c = config_of dc attrs in
6216 setconf dc c;
6217 if closed
6218 then v
6219 else { v with f = defaults }
6221 | Vopen ("ui-font", attrs, closed) ->
6222 let rec getsize size = function
6223 | [] -> size
6224 | ("size", v) :: rest ->
6225 let size =
6226 fromstring int_of_string spos "size" v fstate.fontsize in
6227 getsize size rest
6228 | l -> getsize size l
6230 fstate.fontsize <- getsize fstate.fontsize attrs;
6231 if closed
6232 then v
6233 else { v with f = uifont (Buffer.create 10) }
6235 | Vopen ("doc", attrs, closed) ->
6236 let pathent, spage, srely, span, svisy = doc_of attrs in
6237 let path = unent pathent
6238 and pageno = fromstring int_of_string spos "page" spage 0
6239 and rely = fromstring float_of_string spos "rely" srely 0.0
6240 and pan = fromstring int_of_string spos "pan" span 0
6241 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6242 let c = config_of dc attrs in
6243 let anchor = (pageno, rely, visy) in
6244 if closed
6245 then (Hashtbl.add h path (c, [], pan, anchor); v)
6246 else { v with f = doc path pan anchor c [] }
6248 | Vopen _ ->
6249 error "unexpected subelement in llppconfig" s spos
6251 | Vclose "llppconfig" -> { v with f = toplevel }
6252 | Vclose _ -> error "unexpected close in llppconfig" s spos
6254 and defaults v t spos _ =
6255 match t with
6256 | Vdata | Vcdata -> v
6257 | Vend -> error "unexpected end of input in defaults" s spos
6258 | Vopen ("keymap", attrs, closed) ->
6259 let modename =
6260 try List.assoc "mode" attrs
6261 with Not_found -> "global" in
6262 if closed
6263 then v
6264 else
6265 let ret keymap =
6266 let h = findkeyhash dc modename in
6267 KeyMap.iter (Hashtbl.replace h) keymap;
6268 defaults
6270 { v with f = pkeymap ret KeyMap.empty }
6272 | Vopen (_, _, _) ->
6273 error "unexpected subelement in defaults" s spos
6275 | Vclose "defaults" ->
6276 { v with f = llppconfig }
6278 | Vclose _ -> error "unexpected close in defaults" s spos
6280 and uifont b v t spos epos =
6281 match t with
6282 | Vdata | Vcdata ->
6283 Buffer.add_substring b s spos (epos - spos);
6285 | Vopen (_, _, _) ->
6286 error "unexpected subelement in ui-font" s spos
6287 | Vclose "ui-font" ->
6288 if String.length !fontpath = 0
6289 then fontpath := Buffer.contents b;
6290 { v with f = llppconfig }
6291 | Vclose _ -> error "unexpected close in ui-font" s spos
6292 | Vend -> error "unexpected end of input in ui-font" s spos
6294 and doc path pan anchor c bookmarks v t spos _ =
6295 match t with
6296 | Vdata | Vcdata -> v
6297 | Vend -> error "unexpected end of input in doc" s spos
6298 | Vopen ("bookmarks", _, closed) ->
6299 if closed
6300 then v
6301 else { v with f = pbookmarks path pan anchor c bookmarks }
6303 | Vopen ("keymap", attrs, closed) ->
6304 let modename =
6305 try List.assoc "mode" attrs
6306 with Not_found -> "global"
6308 if closed
6309 then v
6310 else
6311 let ret keymap =
6312 let h = findkeyhash c modename in
6313 KeyMap.iter (Hashtbl.replace h) keymap;
6314 doc path pan anchor c bookmarks
6316 { v with f = pkeymap ret KeyMap.empty }
6318 | Vopen (_, _, _) ->
6319 error "unexpected subelement in doc" s spos
6321 | Vclose "doc" ->
6322 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6323 { v with f = llppconfig }
6325 | Vclose _ -> error "unexpected close in doc" s spos
6327 and pkeymap ret keymap v t spos _ =
6328 match t with
6329 | Vdata | Vcdata -> v
6330 | Vend -> error "unexpected end of input in keymap" s spos
6331 | Vopen ("map", attrs, closed) ->
6332 let r, l = map_of attrs in
6333 let kss = fromstring keys_of_string spos "in" r [] in
6334 let lss = fromstring keys_of_string spos "out" l [] in
6335 let keymap =
6336 match kss with
6337 | [] -> keymap
6338 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6339 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6341 if closed
6342 then { v with f = pkeymap ret keymap }
6343 else
6344 let f () = v in
6345 { v with f = skip "map" f }
6347 | Vopen _ ->
6348 error "unexpected subelement in keymap" s spos
6350 | Vclose "keymap" ->
6351 { v with f = ret keymap }
6353 | Vclose _ -> error "unexpected close in keymap" s spos
6355 and pbookmarks path pan anchor c bookmarks v t spos _ =
6356 match t with
6357 | Vdata | Vcdata -> v
6358 | Vend -> error "unexpected end of input in bookmarks" s spos
6359 | Vopen ("item", attrs, closed) ->
6360 let titleent, spage, srely, svisy = bookmark_of attrs in
6361 let page = fromstring int_of_string spos "page" spage 0
6362 and rely = fromstring float_of_string spos "rely" srely 0.0
6363 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6364 let bookmarks =
6365 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6367 if closed
6368 then { v with f = pbookmarks path pan anchor c bookmarks }
6369 else
6370 let f () = v in
6371 { v with f = skip "item" f }
6373 | Vopen _ ->
6374 error "unexpected subelement in bookmarks" s spos
6376 | Vclose "bookmarks" ->
6377 { v with f = doc path pan anchor c bookmarks }
6379 | Vclose _ -> error "unexpected close in bookmarks" s spos
6381 and skip tag f v t spos _ =
6382 match t with
6383 | Vdata | Vcdata -> v
6384 | Vend ->
6385 error ("unexpected end of input in skipped " ^ tag) s spos
6386 | Vopen (tag', _, closed) ->
6387 if closed
6388 then v
6389 else
6390 let f' () = { v with f = skip tag f } in
6391 { v with f = skip tag' f' }
6392 | Vclose ctag ->
6393 if tag = ctag
6394 then f ()
6395 else error ("unexpected close in skipped " ^ tag) s spos
6398 parse { f = toplevel; accu = () } s;
6399 h, dc;
6402 let do_load f ic =
6404 let len = in_channel_length ic in
6405 let s = String.create len in
6406 really_input ic s 0 len;
6407 f s;
6408 with
6409 | Parse_error (msg, s, pos) ->
6410 let subs = subs s pos in
6411 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6412 failwith ("parse error: " ^ s)
6414 | exn ->
6415 failwith ("config load error: " ^ Printexc.to_string exn)
6418 let defconfpath =
6419 let dir =
6421 let dir = Filename.concat home ".config" in
6422 if Sys.is_directory dir then dir else home
6423 with _ -> home
6425 Filename.concat dir "llpp.conf"
6428 let confpath = ref defconfpath;;
6430 let load1 f =
6431 if Sys.file_exists !confpath
6432 then
6433 match
6434 (try Some (open_in_bin !confpath)
6435 with exn ->
6436 prerr_endline
6437 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6438 Printexc.to_string exn);
6439 None
6441 with
6442 | Some ic ->
6443 let success =
6445 f (do_load get ic)
6446 with exn ->
6447 prerr_endline
6448 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6449 Printexc.to_string exn);
6450 false
6452 close_in ic;
6453 success
6455 | None -> false
6456 else
6457 f (Hashtbl.create 0, defconf)
6460 let load () =
6461 let f (h, dc) =
6462 let pc, pb, px, pa =
6464 Hashtbl.find h (Filename.basename state.path)
6465 with Not_found -> dc, [], 0, emptyanchor
6467 setconf defconf dc;
6468 setconf conf pc;
6469 state.bookmarks <- pb;
6470 state.x <- px;
6471 state.scrollw <- conf.scrollbw;
6472 if conf.jumpback
6473 then state.anchor <- pa;
6474 cbput state.hists.nav pa;
6475 true
6477 load1 f
6480 let add_attrs bb always dc c =
6481 let ob s a b =
6482 if always || a != b
6483 then Printf.bprintf bb "\n %s='%b'" s a
6484 and oi s a b =
6485 if always || a != b
6486 then Printf.bprintf bb "\n %s='%d'" s a
6487 and oI s a b =
6488 if always || a != b
6489 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6490 and oz s a b =
6491 if always || a <> b
6492 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6493 and oF s a b =
6494 if always || a <> b
6495 then Printf.bprintf bb "\n %s='%f'" s a
6496 and oc s a b =
6497 if always || a <> b
6498 then
6499 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6500 and oC s a b =
6501 if always || a <> b
6502 then
6503 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6504 and oR s a b =
6505 if always || a <> b
6506 then
6507 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6508 and os s a b =
6509 if always || a <> b
6510 then
6511 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6512 and og s a b =
6513 if always || a <> b
6514 then
6515 match a with
6516 | None -> ()
6517 | Some (_N, _A, _B) ->
6518 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6519 and oW s a b =
6520 if always || a <> b
6521 then
6522 let v =
6523 match a with
6524 | None -> "false"
6525 | Some f ->
6526 if f = infinity
6527 then "true"
6528 else string_of_float f
6530 Printf.bprintf bb "\n %s='%s'" s v
6531 and oco s a b =
6532 if always || a <> b
6533 then
6534 match a with
6535 | Cmulti ((n, a, b), _) when n > 1 ->
6536 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6537 | Csplit (n, _) when n > 1 ->
6538 Printf.bprintf bb "\n %s='%d'" s ~-n
6539 | _ -> ()
6540 and obeco s a b =
6541 if always || a <> b
6542 then
6543 match a with
6544 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6545 | _ -> ()
6547 let w, h =
6548 if always
6549 then dc.winw, dc.winh
6550 else
6551 match state.fullscreen with
6552 | Some wh -> wh
6553 | None -> c.winw, c.winh
6555 oi "width" w dc.winw;
6556 oi "height" h dc.winh;
6557 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6558 oi "scroll-handle-height" c.scrollh dc.scrollh;
6559 ob "case-insensitive-search" c.icase dc.icase;
6560 ob "preload" c.preload dc.preload;
6561 oi "page-bias" c.pagebias dc.pagebias;
6562 oi "scroll-step" c.scrollstep dc.scrollstep;
6563 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6564 ob "max-height-fit" c.maxhfit dc.maxhfit;
6565 ob "crop-hack" c.crophack dc.crophack;
6566 oW "throttle" c.maxwait dc.maxwait;
6567 ob "highlight-links" c.hlinks dc.hlinks;
6568 ob "under-cursor-info" c.underinfo dc.underinfo;
6569 oi "vertical-margin" c.interpagespace dc.interpagespace;
6570 oz "zoom" c.zoom dc.zoom;
6571 ob "presentation" c.presentation dc.presentation;
6572 oi "rotation-angle" c.angle dc.angle;
6573 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6574 ob "proportional-display" c.proportional dc.proportional;
6575 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6576 oi "tex-count" c.texcount dc.texcount;
6577 oi "slice-height" c.sliceheight dc.sliceheight;
6578 oi "thumbnail-width" c.thumbw dc.thumbw;
6579 ob "persistent-location" c.jumpback dc.jumpback;
6580 oc "background-color" c.bgcolor dc.bgcolor;
6581 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6582 oi "tile-width" c.tilew dc.tilew;
6583 oi "tile-height" c.tileh dc.tileh;
6584 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6585 ob "checkers" c.checkers dc.checkers;
6586 oi "aalevel" c.aalevel dc.aalevel;
6587 ob "trim-margins" c.trimmargins dc.trimmargins;
6588 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6589 os "uri-launcher" c.urilauncher dc.urilauncher;
6590 os "path-launcher" c.pathlauncher dc.pathlauncher;
6591 oC "color-space" c.colorspace dc.colorspace;
6592 ob "invert-colors" c.invert dc.invert;
6593 oF "brightness" c.colorscale dc.colorscale;
6594 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6595 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6596 oco "columns" c.columns dc.columns;
6597 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6598 os "selection-command" c.selcmd dc.selcmd;
6599 ob "update-cursor" c.updatecurs dc.updatecurs;
6600 oi "hint-font-size" c.hfsize dc.hfsize;
6601 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6602 oF "page-scroll-scale" c.pgscale dc.pgscale;
6605 let keymapsbuf always dc c =
6606 let bb = Buffer.create 16 in
6607 let rec loop = function
6608 | [] -> ()
6609 | (modename, h) :: rest ->
6610 let dh = findkeyhash dc modename in
6611 if always || h <> dh
6612 then (
6613 if Hashtbl.length h > 0
6614 then (
6615 if Buffer.length bb > 0
6616 then Buffer.add_char bb '\n';
6617 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6618 Hashtbl.iter (fun i o ->
6619 let isdifferent = always ||
6621 let dO = Hashtbl.find dh i in
6622 dO <> o
6623 with Not_found -> true
6625 if isdifferent
6626 then
6627 let addkm (k, m) =
6628 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6629 if Wsi.withalt m then Buffer.add_string bb "alt-";
6630 if Wsi.withshift m then Buffer.add_string bb "shift-";
6631 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6632 Buffer.add_string bb (Wsi.keyname k);
6634 let addkms l =
6635 let rec loop = function
6636 | [] -> ()
6637 | km :: [] -> addkm km
6638 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6640 loop l
6642 Buffer.add_string bb "<map in='";
6643 addkm i;
6644 match o with
6645 | KMinsrt km ->
6646 Buffer.add_string bb "' out='";
6647 addkm km;
6648 Buffer.add_string bb "'/>\n"
6650 | KMinsrl kms ->
6651 Buffer.add_string bb "' out='";
6652 addkms kms;
6653 Buffer.add_string bb "'/>\n"
6655 | KMmulti (ins, kms) ->
6656 Buffer.add_char bb ' ';
6657 addkms ins;
6658 Buffer.add_string bb "' out='";
6659 addkms kms;
6660 Buffer.add_string bb "'/>\n"
6661 ) h;
6662 Buffer.add_string bb "</keymap>";
6665 loop rest
6667 loop c.keyhashes;
6671 let save () =
6672 let uifontsize = fstate.fontsize in
6673 let bb = Buffer.create 32768 in
6674 let f (h, dc) =
6675 let dc = if conf.bedefault then conf else dc in
6676 Buffer.add_string bb "<llppconfig>\n";
6678 if String.length !fontpath > 0
6679 then
6680 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6681 uifontsize
6682 !fontpath
6683 else (
6684 if uifontsize <> 14
6685 then
6686 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6689 Buffer.add_string bb "<defaults ";
6690 add_attrs bb true dc dc;
6691 let kb = keymapsbuf true dc dc in
6692 if Buffer.length kb > 0
6693 then (
6694 Buffer.add_string bb ">\n";
6695 Buffer.add_buffer bb kb;
6696 Buffer.add_string bb "\n</defaults>\n";
6698 else Buffer.add_string bb "/>\n";
6700 let adddoc path pan anchor c bookmarks =
6701 if bookmarks == [] && c = dc && anchor = emptyanchor
6702 then ()
6703 else (
6704 Printf.bprintf bb "<doc path='%s'"
6705 (enent path 0 (String.length path));
6707 if anchor <> emptyanchor
6708 then (
6709 let n, rely, visy = anchor in
6710 Printf.bprintf bb " page='%d'" n;
6711 if rely > 1e-6
6712 then
6713 Printf.bprintf bb " rely='%f'" rely
6715 if abs_float visy > 1e-6
6716 then
6717 Printf.bprintf bb " visy='%f'" visy
6721 if pan != 0
6722 then Printf.bprintf bb " pan='%d'" pan;
6724 add_attrs bb false dc c;
6725 let kb = keymapsbuf false dc c in
6727 begin match bookmarks with
6728 | [] ->
6729 if Buffer.length kb > 0
6730 then (
6731 Buffer.add_string bb ">\n";
6732 Buffer.add_buffer bb kb;
6733 Buffer.add_string bb "\n</doc>\n";
6735 else Buffer.add_string bb "/>\n"
6736 | _ ->
6737 Buffer.add_string bb ">\n<bookmarks>\n";
6738 List.iter (fun (title, _level, (page, rely, visy)) ->
6739 Printf.bprintf bb
6740 "<item title='%s' page='%d'"
6741 (enent title 0 (String.length title))
6742 page
6744 if rely > 1e-6
6745 then
6746 Printf.bprintf bb " rely='%f'" rely
6748 if abs_float visy > 1e-6
6749 then
6750 Printf.bprintf bb " visy='%f'" visy
6752 Buffer.add_string bb "/>\n";
6753 ) bookmarks;
6754 Buffer.add_string bb "</bookmarks>";
6755 if Buffer.length kb > 0
6756 then (
6757 Buffer.add_string bb "\n";
6758 Buffer.add_buffer bb kb;
6760 Buffer.add_string bb "\n</doc>\n";
6761 end;
6765 let pan, conf =
6766 match state.mode with
6767 | Birdseye (c, pan, _, _, _) ->
6768 let beyecolumns =
6769 match conf.columns with
6770 | Cmulti ((c, _, _), _) -> Some c
6771 | Csingle _ -> None
6772 | Csplit _ -> None
6773 and columns =
6774 match c.columns with
6775 | Cmulti (c, _) -> Cmulti (c, [||])
6776 | Csingle _ -> Csingle [||]
6777 | Csplit _ -> failwith "quit from bird's eye while split"
6779 pan, { c with beyecolumns = beyecolumns; columns = columns }
6780 | _ -> state.x, conf
6782 let basename = Filename.basename state.path in
6783 adddoc basename pan (getanchor ())
6784 (let conf =
6785 let autoscrollstep =
6786 match state.autoscroll with
6787 | Some step -> step
6788 | None -> conf.autoscrollstep
6790 match state.mode with
6791 | Birdseye (bc, _, _, _, _) ->
6792 { conf with
6793 zoom = bc.zoom;
6794 presentation = bc.presentation;
6795 interpagespace = bc.interpagespace;
6796 maxwait = bc.maxwait;
6797 autoscrollstep = autoscrollstep }
6798 | _ -> { conf with autoscrollstep = autoscrollstep }
6799 in conf)
6800 (if conf.savebmarks then state.bookmarks else []);
6802 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6803 if basename <> path
6804 then adddoc path x anchor c bookmarks
6805 ) h;
6806 Buffer.add_string bb "</llppconfig>\n";
6807 true;
6809 if load1 f && Buffer.length bb > 0
6810 then
6812 let tmp = !confpath ^ ".tmp" in
6813 let oc = open_out_bin tmp in
6814 Buffer.output_buffer oc bb;
6815 close_out oc;
6816 Unix.rename tmp !confpath;
6817 with exn ->
6818 prerr_endline
6819 ("error while saving configuration: " ^ Printexc.to_string exn)
6821 end;;
6823 let () =
6824 let trimcachepath = ref "" in
6825 Arg.parse
6826 (Arg.align
6827 [("-p", Arg.String (fun s -> state.password <- s) ,
6828 "<password> Set password");
6830 ("-f", Arg.String (fun s -> Config.fontpath := s),
6831 "<path> Set path to the user interface font");
6833 ("-c", Arg.String (fun s -> Config.confpath := s),
6834 "<path> Set path to the configuration file");
6836 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6837 "<path> Set path to the trim cache file");
6839 ("-v", Arg.Unit (fun () ->
6840 Printf.printf
6841 "%s\nconfiguration path: %s\n"
6842 (version ())
6843 Config.defconfpath
6845 exit 0), " Print version and exit");
6848 (fun s -> state.path <- s)
6849 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6851 if String.length state.path = 0
6852 then (prerr_endline "file name missing"; exit 1);
6854 if not (Config.load ())
6855 then prerr_endline "failed to load configuration";
6857 let globalkeyhash = findkeyhash conf "global" in
6858 let wsfd, winw, winh = Wsi.init (object
6859 method expose =
6860 if nogeomcmds state.geomcmds || platform == Posx
6861 then display ()
6862 else (
6863 GlClear.color (scalecolor2 conf.bgcolor);
6864 GlClear.clear [`color];
6866 method display = display ()
6867 method reshape w h = reshape w h
6868 method mouse b d x y m = mouse b d x y m
6869 method motion x y = state.mpos <- (x, y); motion x y
6870 method pmotion x y = state.mpos <- (x, y); pmotion x y
6871 method key k m =
6872 let mascm = m land (
6873 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6874 ) in
6875 match state.keystate with
6876 | KSnone ->
6877 let km = k, mascm in
6878 begin
6879 match
6880 let modehash = state.uioh#modehash in
6881 try Hashtbl.find modehash km
6882 with Not_found ->
6883 try Hashtbl.find globalkeyhash km
6884 with Not_found -> KMinsrt (k, m)
6885 with
6886 | KMinsrt (k, m) -> keyboard k m
6887 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6888 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6890 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6891 List.iter (fun (k, m) -> keyboard k m) insrt;
6892 state.keystate <- KSnone
6893 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6894 state.keystate <- KSinto (keys, insrt)
6895 | _ ->
6896 state.keystate <- KSnone
6898 method enter x y = state.mpos <- (x, y); pmotion x y
6899 method leave = state.mpos <- (-1, -1)
6900 method quit = raise Quit
6901 end) conf.winw conf.winh (platform = Posx) in
6903 state.wsfd <- wsfd;
6905 if not (
6906 List.exists GlMisc.check_extension
6907 [ "GL_ARB_texture_rectangle"
6908 ; "GL_EXT_texture_recangle"
6909 ; "GL_NV_texture_rectangle" ]
6911 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6913 let cr, sw =
6914 match Ne.pipe () with
6915 | Ne.Exn exn ->
6916 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6917 exit 1
6918 | Ne.Res rw -> rw
6919 and sr, cw =
6920 match Ne.pipe () with
6921 | Ne.Exn exn ->
6922 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6923 exit 1
6924 | Ne.Res rw -> rw
6927 cloexec cr;
6928 cloexec sw;
6929 cloexec sr;
6930 cloexec cw;
6932 setcheckers conf.checkers;
6933 redirectstderr ();
6935 init (cr, cw) (
6936 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6937 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6938 !Config.fontpath, !trimcachepath
6940 state.sr <- sr;
6941 state.sw <- sw;
6942 state.text <- "Opening " ^ state.path;
6943 reshape winw winh;
6944 opendoc state.path state.password;
6945 state.uioh <- uioh;
6947 let rec loop deadline =
6948 let r =
6949 match state.errfd with
6950 | None -> [state.sr; state.wsfd]
6951 | Some fd -> [state.sr; state.wsfd; fd]
6953 if state.redisplay
6954 then (
6955 state.redisplay <- false;
6956 display ();
6958 let timeout =
6959 let now = now () in
6960 if deadline > now
6961 then (
6962 if deadline = infinity
6963 then ~-.1.0
6964 else max 0.0 (deadline -. now)
6966 else 0.0
6968 let r, _, _ =
6969 try Unix.select r [] [] timeout
6970 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6972 begin match r with
6973 | [] ->
6974 state.ghyll None;
6975 let newdeadline =
6976 if state.ghyll == noghyll
6977 then
6978 match state.autoscroll with
6979 | Some step when step != 0 ->
6980 let y = state.y + step in
6981 let y =
6982 if y < 0
6983 then state.maxy
6984 else if y >= state.maxy then 0 else y
6986 gotoy y;
6987 if state.mode = View
6988 then state.text <- "";
6989 deadline +. 0.01
6990 | _ -> infinity
6991 else deadline +. 0.01
6993 loop newdeadline
6995 | l ->
6996 let rec checkfds = function
6997 | [] -> ()
6998 | fd :: rest when fd = state.sr ->
6999 let cmd = readcmd state.sr in
7000 act cmd;
7001 checkfds rest
7003 | fd :: rest when fd = state.wsfd ->
7004 Wsi.readresp fd;
7005 checkfds rest
7007 | fd :: rest ->
7008 let s = String.create 80 in
7009 let n = Unix.read fd s 0 80 in
7010 if conf.redirectstderr
7011 then (
7012 Buffer.add_substring state.errmsgs s 0 n;
7013 state.newerrmsgs <- true;
7014 state.redisplay <- true;
7016 else (
7017 prerr_string (String.sub s 0 n);
7018 flush stderr;
7020 checkfds rest
7022 checkfds l;
7023 let newdeadline =
7024 let deadline1 =
7025 if deadline = infinity
7026 then now () +. 0.01
7027 else deadline
7029 match state.autoscroll with
7030 | Some step when step != 0 -> deadline1
7031 | _ -> if state.ghyll == noghyll then infinity else deadline1
7033 loop newdeadline
7034 end;
7037 loop infinity;
7038 with Quit ->
7039 Config.save ();