5f9e16d339cf2454128012530d9152d0740036c8
[llpp.git] / main.ml
blob5f9e16d339cf2454128012530d9152d0740036c8
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 cbcap b = Array.length b.store;;
191 let cbput b v =
192 let cap = cbcap b in
193 b.store.(b.wc) <- v;
194 b.wc <- (b.wc + 1) mod cap;
195 b.rc <- b.wc;
196 b.len <- min (b.len + 1) cap;
199 let cbempty b = b.len = 0;;
201 let cbgetg b circular dir =
202 if cbempty b
203 then b.store.(0)
204 else
205 let rc = b.rc + dir in
206 let rc =
207 if circular
208 then (
209 if rc = -1
210 then b.len-1
211 else (
212 if rc >= b.len
213 then 0
214 else rc
217 else bound rc 0 (b.len-1)
219 b.rc <- rc;
220 b.store.(rc);
223 let cbget b = cbgetg b false;;
224 let cbgetc b = cbgetg b true;;
226 let drawstring size x y s =
227 Gl.enable `blend;
228 Gl.enable `texture_2d;
229 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
230 ignore (drawstr size x y s);
231 Gl.disable `blend;
232 Gl.disable `texture_2d;
235 let drawstring1 size x y s =
236 drawstr size x y s;
239 let drawstring2 size x y fmt =
240 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
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 /. 16.0
1410 and th = float h /. 16.0 in
1411 let tx0 = float tilex /. 16.0
1412 and ty0 = float tiley /. 16.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 flushpages () =
1842 Hashtbl.iter (fun _ opaque ->
1843 wcmd "freepage %s" opaque;
1844 ) state.pagemap;
1845 Hashtbl.clear state.pagemap;
1848 let opendoc path password =
1849 state.path <- path;
1850 state.password <- password;
1851 state.gen <- state.gen + 1;
1852 state.docinfo <- [];
1854 flushpages ();
1855 setaalevel conf.aalevel;
1856 Wsi.settitle ("llpp " ^ Filename.basename path);
1857 wcmd "open %s\000%s\000" path password;
1858 invalidate "reqlayout"
1859 (fun () ->
1860 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1863 let scalecolor c =
1864 let c = c *. conf.colorscale in
1865 (c, c, c);
1868 let scalecolor2 (r, g, b) =
1869 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1872 let docolumns = function
1873 | Csingle _ ->
1874 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1875 let rec loop pageno pdimno pdim y ph pdims =
1876 if pageno = state.pagecount
1877 then ()
1878 else
1879 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1880 match pdims with
1881 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1882 pdimno+1, pdim, rest
1883 | _ ->
1884 pdimno, pdim, pdims
1886 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1887 let y = y +
1888 (if conf.presentation
1889 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1890 else (if pageno = 0 then 0 else conf.interpagespace)
1893 a.(pageno) <- (pdimno, x, y, pdim);
1894 loop (pageno+1) pdimno pdim (y + h) h pdims
1896 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1897 conf.columns <- Csingle a;
1899 | Cmulti ((columns, coverA, coverB), _) ->
1900 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1901 let rec loop pageno pdimno pdim x y rowh pdims =
1902 let rec fixrow m = if m = pageno then () else
1903 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1904 if h < rowh
1905 then (
1906 let y = y + (rowh - h) / 2 in
1907 a.(m) <- (pdimno, x, y, pdim);
1909 fixrow (m+1)
1911 if pageno = state.pagecount
1912 then fixrow (((pageno - 1) / columns) * columns)
1913 else
1914 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1915 match pdims with
1916 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1917 pdimno+1, pdim, rest
1918 | _ ->
1919 pdimno, pdim, pdims
1921 let x, y, rowh' =
1922 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1923 then (
1924 let x = (conf.winw - state.scrollw - w) / 2 in
1925 let ips =
1926 if conf.presentation then calcips h else conf.interpagespace in
1927 x, y + ips + rowh, h
1929 else (
1930 if (pageno - coverA) mod columns = 0
1931 then (
1932 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1933 let y =
1934 if conf.presentation
1935 then
1936 let ips = calcips h in
1937 y + (if pageno = 0 then 0 else calcips rowh + ips)
1938 else
1939 y + (if pageno = 0 then 0 else conf.interpagespace)
1941 x, y + rowh, h
1943 else x, y, max rowh h
1946 let y =
1947 if pageno > 1 && (pageno - coverA) mod columns = 0
1948 then (
1949 let y =
1950 if pageno = columns && conf.presentation
1951 then (
1952 let ips = calcips rowh in
1953 for i = 0 to pred columns
1955 let (pdimno, x, y, pdim) = a.(i) in
1956 a.(i) <- (pdimno, x, y+ips, pdim)
1957 done;
1958 y+ips;
1960 else y
1962 fixrow (pageno - columns);
1965 else y
1967 a.(pageno) <- (pdimno, x, y, pdim);
1968 let x = x + w + xoff*2 + conf.interpagespace in
1969 loop (pageno+1) pdimno pdim x y rowh' pdims
1971 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1972 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1974 | Csplit (c, _) ->
1975 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1976 let rec loop pageno pdimno pdim y pdims =
1977 if pageno = state.pagecount
1978 then ()
1979 else
1980 let pdimno, ((_, w, h, _) as pdim), pdims =
1981 match pdims with
1982 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1983 pdimno+1, pdim, rest
1984 | _ ->
1985 pdimno, pdim, pdims
1987 let cw = w / c in
1988 let rec loop1 n x y =
1989 if n = c then y else (
1990 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1991 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1994 let y = loop1 0 0 y in
1995 loop (pageno+1) pdimno pdim y pdims
1997 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1998 conf.columns <- Csplit (c, a);
2001 let represent () =
2002 docolumns conf.columns;
2003 state.maxy <- calcheight ();
2004 state.hscrollh <-
2005 if state.w <= conf.winw - state.scrollw
2006 then 0
2007 else state.scrollw
2009 match state.mode with
2010 | Birdseye (_, _, pageno, _, _) ->
2011 let y, h = getpageyh pageno in
2012 let top = (conf.winh - h) / 2 in
2013 gotoy (max 0 (y - top))
2014 | _ -> gotoanchor state.anchor
2017 let reshape w h =
2018 GlDraw.viewport 0 0 w h;
2019 let firsttime = state.geomcmds == firstgeomcmds in
2020 if not firsttime && nogeomcmds state.geomcmds
2021 then state.anchor <- getanchor ();
2023 conf.winw <- w;
2024 let w = truncate (float w *. conf.zoom) - state.scrollw in
2025 let w = max w 2 in
2026 conf.winh <- h;
2027 setfontsize fstate.fontsize;
2028 GlMat.mode `modelview;
2029 GlMat.load_identity ();
2031 GlMat.mode `projection;
2032 GlMat.load_identity ();
2033 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2034 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2035 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2037 let relx =
2038 if conf.zoom <= 1.0
2039 then 0.0
2040 else float state.x /. float state.w
2042 invalidate "geometry"
2043 (fun () ->
2044 state.w <- w;
2045 if not firsttime
2046 then state.x <- truncate (relx *. float w);
2047 let w =
2048 match conf.columns with
2049 | Csingle _ -> w
2050 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2051 | Csplit (c, _) -> w * c
2053 wcmd "geometry %d %d" w h);
2056 let enttext () =
2057 let len = String.length state.text in
2058 let drawstring s =
2059 let hscrollh =
2060 match state.mode with
2061 | Textentry _
2062 | View ->
2063 let h, _, _ = state.uioh#scrollpw in
2065 | _ -> 0
2067 let rect x w =
2068 GlDraw.rect
2069 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2070 (x+.w, float (conf.winh - hscrollh))
2073 let w = float (conf.winw - state.scrollw - 1) in
2074 if state.progress >= 0.0 && state.progress < 1.0
2075 then (
2076 GlDraw.color (0.3, 0.3, 0.3);
2077 let w1 = w *. state.progress in
2078 rect 0.0 w1;
2079 GlDraw.color (0.0, 0.0, 0.0);
2080 rect w1 (w-.w1)
2082 else (
2083 GlDraw.color (0.0, 0.0, 0.0);
2084 rect 0.0 w;
2087 GlDraw.color (1.0, 1.0, 1.0);
2088 drawstring fstate.fontsize
2089 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2091 let s =
2092 match state.mode with
2093 | Textentry ((prefix, text, _, _, _, _), _) ->
2094 let s =
2095 if len > 0
2096 then
2097 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2098 else
2099 Printf.sprintf "%s%s_" prefix text
2103 | _ -> state.text
2105 let s =
2106 if state.newerrmsgs
2107 then (
2108 if not (istextentry state.mode)
2109 then
2110 let s1 = "(press 'e' to review error messasges)" in
2111 if String.length s > 0 then s ^ " " ^ s1 else s1
2112 else s
2114 else s
2116 if String.length s > 0
2117 then drawstring s
2120 let gctiles () =
2121 let len = Queue.length state.tilelru in
2122 let layout = lazy (
2123 match state.throttle with
2124 | None ->
2125 if conf.preload
2126 then preloadlayout state.y
2127 else state.layout
2128 | Some (layout, _, _) ->
2129 layout
2130 ) in
2131 let rec loop qpos =
2132 if state.memused <= conf.memlimit
2133 then ()
2134 else (
2135 if qpos < len
2136 then
2137 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2138 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2139 let (_, pw, ph, _) = getpagedim n in
2141 gen = state.gen
2142 && colorspace = conf.colorspace
2143 && angle = conf.angle
2144 && pagew = pw
2145 && pageh = ph
2146 && (
2147 let x = col*conf.tilew
2148 and y = row*conf.tileh in
2149 tilevisible (Lazy.force_val layout) n x y
2151 then Queue.push lruitem state.tilelru
2152 else (
2153 wcmd "freetile %s" p;
2154 state.memused <- state.memused - s;
2155 state.uioh#infochanged Memused;
2156 Hashtbl.remove state.tilemap k;
2158 loop (qpos+1)
2161 loop 0
2164 let flushtiles () =
2165 Queue.iter (fun (k, p, s) ->
2166 wcmd "freetile %s" p;
2167 state.memused <- state.memused - s;
2168 state.uioh#infochanged Memused;
2169 Hashtbl.remove state.tilemap k;
2170 ) state.tilelru;
2171 Queue.clear state.tilelru;
2172 load state.layout;
2175 let logcurrently = function
2176 | Idle -> dolog "Idle"
2177 | Loading (l, gen) ->
2178 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2179 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2180 dolog
2181 "Tiling %d[%d,%d] page=%s cs=%s angle"
2182 l.pageno col row pageopaque
2183 (colorspace_to_string colorspace)
2185 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2186 angle gen conf.angle state.gen
2187 tilew tileh
2188 conf.tilew conf.tileh
2190 | Outlining _ ->
2191 dolog "outlining"
2194 let act cmds =
2195 (* dolog "%S" cmds; *)
2196 let op, args =
2197 let spacepos =
2198 try String.index cmds ' '
2199 with Not_found -> -1
2201 if spacepos = -1
2202 then cmds, ""
2203 else
2204 let l = String.length cmds in
2205 let op = String.sub cmds 0 spacepos in
2206 op, begin
2207 if l - spacepos < 2 then ""
2208 else String.sub cmds (spacepos+1) (l-spacepos-1)
2211 match op with
2212 | "clear" ->
2213 state.uioh#infochanged Pdim;
2214 state.pdims <- [];
2216 | "clearrects" ->
2217 state.rects <- state.rects1;
2218 G.postRedisplay "clearrects";
2220 | "continue" ->
2221 let n =
2222 try Scanf.sscanf args "%u" (fun n -> n)
2223 with exn ->
2224 dolog "error processing 'continue' %S: %s"
2225 cmds (Printexc.to_string exn);
2226 exit 1;
2228 state.pagecount <- n;
2229 begin match state.currently with
2230 | Outlining l ->
2231 state.currently <- Idle;
2232 state.outlines <- Array.of_list (List.rev l)
2233 | _ -> ()
2234 end;
2236 let cur, cmds = state.geomcmds in
2237 if String.length cur = 0
2238 then failwith "umpossible";
2240 begin match List.rev cmds with
2241 | [] ->
2242 state.geomcmds <- "", [];
2243 represent ();
2244 | (s, f) :: rest ->
2245 f ();
2246 state.geomcmds <- s, List.rev rest;
2247 end;
2248 if conf.maxwait = None
2249 then G.postRedisplay "continue";
2251 | "title" ->
2252 Wsi.settitle args
2254 | "msg" ->
2255 showtext ' ' args
2257 | "vmsg" ->
2258 if conf.verbose
2259 then showtext ' ' args
2261 | "progress" ->
2262 let progress, text =
2264 Scanf.sscanf args "%f %n"
2265 (fun f pos ->
2266 f, String.sub args pos (String.length args - pos))
2267 with exn ->
2268 dolog "error processing 'progress' %S: %s"
2269 cmds (Printexc.to_string exn);
2270 exit 1;
2272 state.text <- text;
2273 state.progress <- progress;
2274 G.postRedisplay "progress"
2276 | "firstmatch" ->
2277 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2279 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2280 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2281 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2282 with exn ->
2283 dolog "error processing 'firstmatch' %S: %s"
2284 cmds (Printexc.to_string exn);
2285 exit 1;
2287 let y = (getpagey pageno) + truncate y0 in
2288 addnav ();
2289 gotoy y;
2290 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2292 | "match" ->
2293 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2295 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2296 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2297 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2298 with exn ->
2299 dolog "error processing 'match' %S: %s"
2300 cmds (Printexc.to_string exn);
2301 exit 1;
2303 state.rects1 <-
2304 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2306 | "page" ->
2307 let pageopaque, t =
2309 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2310 with exn ->
2311 dolog "error processing 'page' %S: %s"
2312 cmds (Printexc.to_string exn);
2313 exit 1;
2315 begin match state.currently with
2316 | Loading (l, gen) ->
2317 vlog "page %d took %f sec" l.pageno t;
2318 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2319 begin match state.throttle with
2320 | None ->
2321 let preloadedpages =
2322 if conf.preload
2323 then preloadlayout state.y
2324 else state.layout
2326 let evict () =
2327 let module IntSet =
2328 Set.Make (struct type t = int let compare = (-) end) in
2329 let set =
2330 List.fold_left (fun s l -> IntSet.add l.pageno s)
2331 IntSet.empty preloadedpages
2333 let evictedpages =
2334 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2335 if not (IntSet.mem pageno set)
2336 then (
2337 wcmd "freepage %s" opaque;
2338 key :: accu
2340 else accu
2341 ) state.pagemap []
2343 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2345 evict ();
2346 state.currently <- Idle;
2347 if gen = state.gen
2348 then (
2349 tilepage l.pageno pageopaque state.layout;
2350 load state.layout;
2351 load preloadedpages;
2352 if pagevisible state.layout l.pageno
2353 && layoutready state.layout
2354 then G.postRedisplay "page";
2357 | Some (layout, _, _) ->
2358 state.currently <- Idle;
2359 tilepage l.pageno pageopaque layout;
2360 load state.layout
2361 end;
2363 | _ ->
2364 dolog "Inconsistent loading state";
2365 logcurrently state.currently;
2366 exit 1
2369 | "tile" ->
2370 let (x, y, opaque, size, t) =
2372 Scanf.sscanf args "%u %u %s %u %f"
2373 (fun x y p size t -> (x, y, p, size, t))
2374 with exn ->
2375 dolog "error processing 'tile' %S: %s"
2376 cmds (Printexc.to_string exn);
2377 exit 1;
2379 begin match state.currently with
2380 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2381 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2383 if tilew != conf.tilew || tileh != conf.tileh
2384 then (
2385 wcmd "freetile %s" opaque;
2386 state.currently <- Idle;
2387 load state.layout;
2389 else (
2390 puttileopaque l col row gen cs angle opaque size t;
2391 state.memused <- state.memused + size;
2392 state.uioh#infochanged Memused;
2393 gctiles ();
2394 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2395 opaque, size) state.tilelru;
2397 let layout =
2398 match state.throttle with
2399 | None -> state.layout
2400 | Some (layout, _, _) -> layout
2403 state.currently <- Idle;
2404 if gen = state.gen
2405 && conf.colorspace = cs
2406 && conf.angle = angle
2407 && tilevisible layout l.pageno x y
2408 then conttiling l.pageno pageopaque;
2410 begin match state.throttle with
2411 | None ->
2412 preload state.layout;
2413 if gen = state.gen
2414 && conf.colorspace = cs
2415 && conf.angle = angle
2416 && tilevisible state.layout l.pageno x y
2417 then G.postRedisplay "tile nothrottle";
2419 | Some (layout, y, _) ->
2420 let ready = layoutready layout in
2421 if ready
2422 then (
2423 state.y <- y;
2424 state.layout <- layout;
2425 state.throttle <- None;
2426 G.postRedisplay "throttle";
2428 else load layout;
2429 end;
2432 | _ ->
2433 dolog "Inconsistent tiling state";
2434 logcurrently state.currently;
2435 exit 1
2438 | "pdim" ->
2439 let pdim =
2441 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2442 with exn ->
2443 dolog "error processing 'pdim' %S: %s"
2444 cmds (Printexc.to_string exn);
2445 exit 1;
2447 state.uioh#infochanged Pdim;
2448 state.pdims <- pdim :: state.pdims
2450 | "o" ->
2451 let (l, n, t, h, pos) =
2453 Scanf.sscanf args "%u %u %d %u %n"
2454 (fun l n t h pos -> l, n, t, h, pos)
2455 with exn ->
2456 dolog "error processing 'o' %S: %s"
2457 cmds (Printexc.to_string exn);
2458 exit 1;
2460 let s = String.sub args pos (String.length args - pos) in
2461 let outline = (s, l, (n, float t /. float h, 0.0)) in
2462 begin match state.currently with
2463 | Outlining outlines ->
2464 state.currently <- Outlining (outline :: outlines)
2465 | Idle ->
2466 state.currently <- Outlining [outline]
2467 | currently ->
2468 dolog "invalid outlining state";
2469 logcurrently currently
2472 | "info" ->
2473 state.docinfo <- (1, args) :: state.docinfo
2475 | "infoend" ->
2476 state.uioh#infochanged Docinfo;
2477 state.docinfo <- List.rev state.docinfo
2479 | _ ->
2480 dolog "unknown cmd `%S'" cmds
2483 let onhist cb =
2484 let rc = cb.rc in
2485 let action = function
2486 | HCprev -> cbget cb ~-1
2487 | HCnext -> cbget cb 1
2488 | HCfirst -> cbget cb ~-(cb.rc)
2489 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2490 and cancel () = cb.rc <- rc
2491 in (action, cancel)
2494 let search pattern forward =
2495 if String.length pattern > 0
2496 then
2497 let pn, py =
2498 match state.layout with
2499 | [] -> 0, 0
2500 | l :: _ ->
2501 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2503 wcmd "search %d %d %d %d,%s\000"
2504 (btod conf.icase) pn py (btod forward) pattern;
2507 let intentry text key =
2508 let c =
2509 if key >= 32 && key < 127
2510 then Char.chr key
2511 else '\000'
2513 match c with
2514 | '0' .. '9' ->
2515 let text = addchar text c in
2516 TEcont text
2518 | _ ->
2519 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2520 TEcont text
2523 let linknentry text key =
2524 let c =
2525 if key >= 32 && key < 127
2526 then Char.chr key
2527 else '\000'
2529 match c with
2530 | 'a' .. 'z' ->
2531 let text = addchar text c in
2532 TEcont text
2534 | _ ->
2535 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2536 TEcont text
2539 let linkndone f s =
2540 if String.length s > 0
2541 then (
2542 let n =
2543 let l = String.length s in
2544 let rec loop pos n = if pos = l then n else
2545 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2546 loop (pos+1) (n*26 + m)
2547 in loop 0 0
2549 let rec loop n = function
2550 | [] -> ()
2551 | l :: rest ->
2552 match getopaque l.pageno with
2553 | None -> loop n rest
2554 | Some opaque ->
2555 let m = getlinkcount opaque in
2556 if n < m
2557 then (
2558 let under = getlink opaque n in
2559 f under
2561 else loop (n-m) rest
2563 loop n state.layout;
2567 let textentry text key =
2568 if key land 0xff00 = 0xff00
2569 then TEcont text
2570 else TEcont (text ^ Wsi.toutf8 key)
2573 let reqlayout angle proportional =
2574 match state.throttle with
2575 | None ->
2576 if nogeomcmds state.geomcmds
2577 then state.anchor <- getanchor ();
2578 conf.angle <- angle mod 360;
2579 if conf.angle != 0
2580 then (
2581 match state.mode with
2582 | LinkNav _ -> state.mode <- View
2583 | _ -> ()
2585 conf.proportional <- proportional;
2586 invalidate "reqlayout"
2587 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2588 | _ -> ()
2591 let settrim trimmargins trimfuzz =
2592 if nogeomcmds state.geomcmds
2593 then state.anchor <- getanchor ();
2594 conf.trimmargins <- trimmargins;
2595 conf.trimfuzz <- trimfuzz;
2596 let x0, y0, x1, y1 = trimfuzz in
2597 invalidate "settrim"
2598 (fun () ->
2599 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2600 flushpages ();
2603 let setzoom zoom =
2604 match state.throttle with
2605 | None ->
2606 let zoom = max 0.01 zoom in
2607 if zoom <> conf.zoom
2608 then (
2609 state.prevzoom <- conf.zoom;
2610 conf.zoom <- zoom;
2611 reshape conf.winw conf.winh;
2612 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2615 | Some (layout, y, started) ->
2616 let time =
2617 match conf.maxwait with
2618 | None -> 0.0
2619 | Some t -> t
2621 let dt = now () -. started in
2622 if dt > time
2623 then (
2624 state.y <- y;
2625 load layout;
2629 let setcolumns mode columns coverA coverB =
2630 state.prevcolumns <- Some (conf.columns, conf.zoom);
2631 if columns < 0
2632 then (
2633 if isbirdseye mode
2634 then showtext '!' "split mode doesn't work in bird's eye"
2635 else (
2636 conf.columns <- Csplit (-columns, [||]);
2637 state.x <- 0;
2638 conf.zoom <- 1.0;
2641 else (
2642 if columns < 2
2643 then (
2644 conf.columns <- Csingle [||];
2645 state.x <- 0;
2646 setzoom 1.0;
2648 else (
2649 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2650 conf.zoom <- 1.0;
2653 reshape conf.winw conf.winh;
2656 let enterbirdseye () =
2657 let zoom = float conf.thumbw /. float conf.winw in
2658 let birdseyepageno =
2659 let cy = conf.winh / 2 in
2660 let fold = function
2661 | [] -> 0
2662 | l :: rest ->
2663 let rec fold best = function
2664 | [] -> best.pageno
2665 | l :: rest ->
2666 let d = cy - (l.pagedispy + l.pagevh/2)
2667 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2668 if abs d < abs dbest
2669 then fold l rest
2670 else best.pageno
2671 in fold l rest
2673 fold state.layout
2675 state.mode <- Birdseye (
2676 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2678 conf.zoom <- zoom;
2679 conf.presentation <- false;
2680 conf.interpagespace <- 10;
2681 conf.hlinks <- false;
2682 state.x <- 0;
2683 state.mstate <- Mnone;
2684 conf.maxwait <- None;
2685 conf.columns <- (
2686 match conf.beyecolumns with
2687 | Some c ->
2688 conf.zoom <- 1.0;
2689 Cmulti ((c, 0, 0), [||])
2690 | None -> Csingle [||]
2692 Wsi.setcursor Wsi.CURSOR_INHERIT;
2693 if conf.verbose
2694 then
2695 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2696 (100.0*.zoom)
2697 else
2698 state.text <- ""
2700 reshape conf.winw conf.winh;
2703 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2704 state.mode <- View;
2705 conf.zoom <- c.zoom;
2706 conf.presentation <- c.presentation;
2707 conf.interpagespace <- c.interpagespace;
2708 conf.maxwait <- c.maxwait;
2709 conf.hlinks <- c.hlinks;
2710 conf.beyecolumns <- (
2711 match conf.columns with
2712 | Cmulti ((c, _, _), _) -> Some c
2713 | Csingle _ -> None
2714 | Csplit _ -> failwith "leaving bird's eye split mode"
2716 conf.columns <- (
2717 match c.columns with
2718 | Cmulti (c, _) -> Cmulti (c, [||])
2719 | Csingle _ -> Csingle [||]
2720 | Csplit (c, _) -> Csplit (c, [||])
2722 state.x <- leftx;
2723 if conf.verbose
2724 then
2725 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2726 (100.0*.conf.zoom)
2728 reshape conf.winw conf.winh;
2729 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2732 let togglebirdseye () =
2733 match state.mode with
2734 | Birdseye vals -> leavebirdseye vals true
2735 | View -> enterbirdseye ()
2736 | _ -> ()
2739 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2740 let pageno = max 0 (pageno - incr) in
2741 let rec loop = function
2742 | [] -> gotopage1 pageno 0
2743 | l :: _ when l.pageno = pageno ->
2744 if l.pagedispy >= 0 && l.pagey = 0
2745 then G.postRedisplay "upbirdseye"
2746 else gotopage1 pageno 0
2747 | _ :: rest -> loop rest
2749 loop state.layout;
2750 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2753 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2754 let pageno = min (state.pagecount - 1) (pageno + incr) in
2755 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2756 let rec loop = function
2757 | [] ->
2758 let y, h = getpageyh pageno in
2759 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2760 gotoy (clamp dy)
2761 | l :: _ when l.pageno = pageno ->
2762 if l.pagevh != l.pageh
2763 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2764 else G.postRedisplay "downbirdseye"
2765 | _ :: rest -> loop rest
2767 loop state.layout
2770 let optentry mode _ key =
2771 let btos b = if b then "on" else "off" in
2772 if key >= 32 && key < 127
2773 then
2774 let c = Char.chr key in
2775 match c with
2776 | 's' ->
2777 let ondone s =
2778 try conf.scrollstep <- int_of_string s with exc ->
2779 state.text <- Printf.sprintf "bad integer `%s': %s"
2780 s (Printexc.to_string exc)
2782 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2784 | 'A' ->
2785 let ondone s =
2787 conf.autoscrollstep <- int_of_string s;
2788 if state.autoscroll <> None
2789 then state.autoscroll <- Some conf.autoscrollstep
2790 with exc ->
2791 state.text <- Printf.sprintf "bad integer `%s': %s"
2792 s (Printexc.to_string exc)
2794 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2796 | 'C' ->
2797 let ondone s =
2799 let n, a, b = multicolumns_of_string s in
2800 setcolumns mode n a b;
2801 with exc ->
2802 state.text <- Printf.sprintf "bad columns `%s': %s"
2803 s (Printexc.to_string exc)
2805 TEswitch ("columns: ", "", None, textentry, ondone, true)
2807 | 'Z' ->
2808 let ondone s =
2810 let zoom = float (int_of_string s) /. 100.0 in
2811 setzoom zoom
2812 with exc ->
2813 state.text <- Printf.sprintf "bad integer `%s': %s"
2814 s (Printexc.to_string exc)
2816 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2818 | 't' ->
2819 let ondone s =
2821 conf.thumbw <- bound (int_of_string s) 2 4096;
2822 state.text <-
2823 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2824 begin match mode with
2825 | Birdseye beye ->
2826 leavebirdseye beye false;
2827 enterbirdseye ();
2828 | _ -> ();
2830 with exc ->
2831 state.text <- Printf.sprintf "bad integer `%s': %s"
2832 s (Printexc.to_string exc)
2834 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2836 | 'R' ->
2837 let ondone s =
2838 match try
2839 Some (int_of_string s)
2840 with exc ->
2841 state.text <- Printf.sprintf "bad integer `%s': %s"
2842 s (Printexc.to_string exc);
2843 None
2844 with
2845 | Some angle -> reqlayout angle conf.proportional
2846 | None -> ()
2848 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2850 | 'i' ->
2851 conf.icase <- not conf.icase;
2852 TEdone ("case insensitive search " ^ (btos conf.icase))
2854 | 'p' ->
2855 conf.preload <- not conf.preload;
2856 gotoy state.y;
2857 TEdone ("preload " ^ (btos conf.preload))
2859 | 'v' ->
2860 conf.verbose <- not conf.verbose;
2861 TEdone ("verbose " ^ (btos conf.verbose))
2863 | 'd' ->
2864 conf.debug <- not conf.debug;
2865 TEdone ("debug " ^ (btos conf.debug))
2867 | 'h' ->
2868 conf.maxhfit <- not conf.maxhfit;
2869 state.maxy <- calcheight ();
2870 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2872 | 'c' ->
2873 conf.crophack <- not conf.crophack;
2874 TEdone ("crophack " ^ btos conf.crophack)
2876 | 'a' ->
2877 let s =
2878 match conf.maxwait with
2879 | None ->
2880 conf.maxwait <- Some infinity;
2881 "always wait for page to complete"
2882 | Some _ ->
2883 conf.maxwait <- None;
2884 "show placeholder if page is not ready"
2886 TEdone s
2888 | 'f' ->
2889 conf.underinfo <- not conf.underinfo;
2890 TEdone ("underinfo " ^ btos conf.underinfo)
2892 | 'P' ->
2893 conf.savebmarks <- not conf.savebmarks;
2894 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2896 | 'S' ->
2897 let ondone s =
2899 let pageno, py =
2900 match state.layout with
2901 | [] -> 0, 0
2902 | l :: _ ->
2903 l.pageno, l.pagey
2905 conf.interpagespace <- int_of_string s;
2906 docolumns conf.columns;
2907 state.maxy <- calcheight ();
2908 let y = getpagey pageno in
2909 gotoy (y + py)
2910 with exc ->
2911 state.text <- Printf.sprintf "bad integer `%s': %s"
2912 s (Printexc.to_string exc)
2914 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2916 | 'l' ->
2917 reqlayout conf.angle (not conf.proportional);
2918 TEdone ("proportional display " ^ btos conf.proportional)
2920 | 'T' ->
2921 settrim (not conf.trimmargins) conf.trimfuzz;
2922 TEdone ("trim margins " ^ btos conf.trimmargins)
2924 | 'I' ->
2925 conf.invert <- not conf.invert;
2926 TEdone ("invert colors " ^ btos conf.invert)
2928 | 'x' ->
2929 let ondone s =
2930 cbput state.hists.sel s;
2931 conf.selcmd <- s;
2933 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2934 textentry, ondone, true)
2936 | _ ->
2937 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2938 TEstop
2939 else
2940 TEcont state.text
2943 class type lvsource = object
2944 method getitemcount : int
2945 method getitem : int -> (string * int)
2946 method hasaction : int -> bool
2947 method exit :
2948 uioh:uioh ->
2949 cancel:bool ->
2950 active:int ->
2951 first:int ->
2952 pan:int ->
2953 qsearch:string ->
2954 uioh option
2955 method getactive : int
2956 method getfirst : int
2957 method getqsearch : string
2958 method setqsearch : string -> unit
2959 method getpan : int
2960 end;;
2962 class virtual lvsourcebase = object
2963 val mutable m_active = 0
2964 val mutable m_first = 0
2965 val mutable m_qsearch = ""
2966 val mutable m_pan = 0
2967 method getactive = m_active
2968 method getfirst = m_first
2969 method getqsearch = m_qsearch
2970 method getpan = m_pan
2971 method setqsearch s = m_qsearch <- s
2972 end;;
2974 let withoutlastutf8 s =
2975 let len = String.length s in
2976 if len = 0
2977 then s
2978 else
2979 let rec find pos =
2980 if pos = 0
2981 then pos
2982 else
2983 let b = Char.code s.[pos] in
2984 if b land 0b110000 = 0b11000000
2985 then find (pos-1)
2986 else pos-1
2988 let first =
2989 if Char.code s.[len-1] land 0x80 = 0
2990 then len-1
2991 else find (len-1)
2993 String.sub s 0 first;
2996 let textentrykeyboard
2997 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2998 let enttext te =
2999 state.mode <- Textentry (te, onleave);
3000 state.text <- "";
3001 enttext ();
3002 G.postRedisplay "textentrykeyboard enttext";
3004 let histaction cmd =
3005 match opthist with
3006 | None -> ()
3007 | Some (action, _) ->
3008 state.mode <- Textentry (
3009 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3011 G.postRedisplay "textentry histaction"
3013 match key with
3014 | 0xff08 -> (* backspace *)
3015 let s = withoutlastutf8 text in
3016 let len = String.length s in
3017 if cancelonempty && len = 0
3018 then (
3019 onleave Cancel;
3020 G.postRedisplay "textentrykeyboard after cancel";
3022 else (
3023 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3026 | 0xff0d ->
3027 ondone text;
3028 onleave Confirm;
3029 G.postRedisplay "textentrykeyboard after confirm"
3031 | 0xff52 -> histaction HCprev
3032 | 0xff54 -> histaction HCnext
3033 | 0xff50 -> histaction HCfirst
3034 | 0xff57 -> histaction HClast
3036 | 0xff1b -> (* escape*)
3037 if String.length text = 0
3038 then (
3039 begin match opthist with
3040 | None -> ()
3041 | Some (_, onhistcancel) -> onhistcancel ()
3042 end;
3043 onleave Cancel;
3044 state.text <- "";
3045 G.postRedisplay "textentrykeyboard after cancel2"
3047 else (
3048 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3051 | 0xff9f | 0xffff -> () (* delete *)
3053 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3054 begin match onkey text key with
3055 | TEdone text ->
3056 ondone text;
3057 onleave Confirm;
3058 G.postRedisplay "textentrykeyboard after confirm2";
3060 | TEcont text ->
3061 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3063 | TEstop ->
3064 onleave Cancel;
3065 G.postRedisplay "textentrykeyboard after cancel3"
3067 | TEswitch te ->
3068 state.mode <- Textentry (te, onleave);
3069 G.postRedisplay "textentrykeyboard switch";
3070 end;
3072 | _ ->
3073 vlog "unhandled key %s" (Wsi.keyname key)
3076 let firstof first active =
3077 if first > active || abs (first - active) > fstate.maxrows - 1
3078 then max 0 (active - (fstate.maxrows/2))
3079 else first
3082 let calcfirst first active =
3083 if active > first
3084 then
3085 let rows = active - first in
3086 if rows > fstate.maxrows then active - fstate.maxrows else first
3087 else active
3090 let scrollph y maxy =
3091 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3092 let sh = float conf.winh /. sh in
3093 let sh = max sh (float conf.scrollh) in
3095 let percent =
3096 if y = state.maxy
3097 then 1.0
3098 else float y /. float maxy
3100 let position = (float conf.winh -. sh) *. percent in
3102 let position =
3103 if position +. sh > float conf.winh
3104 then float conf.winh -. sh
3105 else position
3107 position, sh;
3110 let coe s = (s :> uioh);;
3112 class listview ~(source:lvsource) ~trusted ~modehash =
3113 object (self)
3114 val m_pan = source#getpan
3115 val m_first = source#getfirst
3116 val m_active = source#getactive
3117 val m_qsearch = source#getqsearch
3118 val m_prev_uioh = state.uioh
3120 method private elemunder y =
3121 let n = y / (fstate.fontsize+1) in
3122 if m_first + n < source#getitemcount
3123 then (
3124 if source#hasaction (m_first + n)
3125 then Some (m_first + n)
3126 else None
3128 else None
3130 method display =
3131 Gl.enable `blend;
3132 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3133 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3134 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3135 GlDraw.color (1., 1., 1.);
3136 Gl.enable `texture_2d;
3137 let fs = fstate.fontsize in
3138 let nfs = fs + 1 in
3139 let ww = fstate.wwidth in
3140 let tabw = 30.0*.ww in
3141 let itemcount = source#getitemcount in
3142 let rec loop row =
3143 if (row - m_first) > fstate.maxrows
3144 then ()
3145 else (
3146 if row >= 0 && row < itemcount
3147 then (
3148 let (s, level) = source#getitem row in
3149 let y = (row - m_first) * nfs in
3150 let x = 5.0 +. float (level + m_pan) *. ww in
3151 if row = m_active
3152 then (
3153 Gl.disable `texture_2d;
3154 GlDraw.polygon_mode `both `line;
3155 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3156 GlDraw.rect (1., float (y + 1))
3157 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3158 GlDraw.polygon_mode `both `fill;
3159 GlDraw.color (1., 1., 1.);
3160 Gl.enable `texture_2d;
3163 let drawtabularstring s =
3164 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3165 if trusted
3166 then
3167 let tabpos = try String.index s '\t' with Not_found -> -1 in
3168 if tabpos > 0
3169 then
3170 let len = String.length s - tabpos - 1 in
3171 let s1 = String.sub s 0 tabpos
3172 and s2 = String.sub s (tabpos + 1) len in
3173 let nx = drawstr x s1 in
3174 let sw = nx -. x in
3175 let x = x +. (max tabw sw) in
3176 drawstr x s2
3177 else
3178 drawstr x s
3179 else
3180 drawstr x s
3182 let _ = drawtabularstring s in
3183 loop (row+1)
3187 loop m_first;
3188 Gl.disable `blend;
3189 Gl.disable `texture_2d;
3191 method updownlevel incr =
3192 let len = source#getitemcount in
3193 let curlevel =
3194 if m_active >= 0 && m_active < len
3195 then snd (source#getitem m_active)
3196 else -1
3198 let rec flow i =
3199 if i = len then i-1 else if i = -1 then 0 else
3200 let _, l = source#getitem i in
3201 if l != curlevel then i else flow (i+incr)
3203 let active = flow m_active in
3204 let first = calcfirst m_first active in
3205 G.postRedisplay "outline updownlevel";
3206 {< m_active = active; m_first = first >}
3208 method private key1 key mask =
3209 let set1 active first qsearch =
3210 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3212 let search active pattern incr =
3213 let dosearch re =
3214 let rec loop n =
3215 if n >= 0 && n < source#getitemcount
3216 then (
3217 let s, _ = source#getitem n in
3219 (try ignore (Str.search_forward re s 0); true
3220 with Not_found -> false)
3221 then Some n
3222 else loop (n + incr)
3224 else None
3226 loop active
3229 let re = Str.regexp_case_fold pattern in
3230 dosearch re
3231 with Failure s ->
3232 state.text <- s;
3233 None
3235 let itemcount = source#getitemcount in
3236 let find start incr =
3237 let rec find i =
3238 if i = -1 || i = itemcount
3239 then -1
3240 else (
3241 if source#hasaction i
3242 then i
3243 else find (i + incr)
3246 find start
3248 let set active first =
3249 let first = bound first 0 (itemcount - fstate.maxrows) in
3250 state.text <- "";
3251 coe {< m_active = active; m_first = first >}
3253 let navigate incr =
3254 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3255 let active, first =
3256 let incr1 = if incr > 0 then 1 else -1 in
3257 if isvisible m_first m_active
3258 then
3259 let next =
3260 let next = m_active + incr in
3261 let next =
3262 if next < 0 || next >= itemcount
3263 then -1
3264 else find next incr1
3266 if next = -1 || abs (m_active - next) > fstate.maxrows
3267 then -1
3268 else next
3270 if next = -1
3271 then
3272 let first = m_first + incr in
3273 let first = bound first 0 (itemcount - 1) in
3274 let next =
3275 let next = m_active + incr in
3276 let next = bound next 0 (itemcount - 1) in
3277 find next ~-incr1
3279 let active = if next = -1 then m_active else next in
3280 active, first
3281 else
3282 let first = min next m_first in
3283 let first =
3284 if abs (next - first) > fstate.maxrows
3285 then first + incr
3286 else first
3288 next, first
3289 else
3290 let first = m_first + incr in
3291 let first = bound first 0 (itemcount - 1) in
3292 let active =
3293 let next = m_active + incr in
3294 let next = bound next 0 (itemcount - 1) in
3295 let next = find next incr1 in
3296 let active =
3297 if next = -1 || abs (m_active - first) > fstate.maxrows
3298 then (
3299 let active = if m_active = -1 then next else m_active in
3300 active
3302 else next
3304 if isvisible first active
3305 then active
3306 else -1
3308 active, first
3310 G.postRedisplay "listview navigate";
3311 set active first;
3313 match key with
3314 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3315 let incr = if key = 0x72 then -1 else 1 in
3316 let active, first =
3317 match search (m_active + incr) m_qsearch incr with
3318 | None ->
3319 state.text <- m_qsearch ^ " [not found]";
3320 m_active, m_first
3321 | Some active ->
3322 state.text <- m_qsearch;
3323 active, firstof m_first active
3325 G.postRedisplay "listview ctrl-r/s";
3326 set1 active first m_qsearch;
3328 | 0xff08 -> (* backspace *)
3329 if String.length m_qsearch = 0
3330 then coe self
3331 else (
3332 let qsearch = withoutlastutf8 m_qsearch in
3333 let len = String.length qsearch in
3334 if len = 0
3335 then (
3336 state.text <- "";
3337 G.postRedisplay "listview empty qsearch";
3338 set1 m_active m_first "";
3340 else
3341 let active, first =
3342 match search m_active qsearch ~-1 with
3343 | None ->
3344 state.text <- qsearch ^ " [not found]";
3345 m_active, m_first
3346 | Some active ->
3347 state.text <- qsearch;
3348 active, firstof m_first active
3350 G.postRedisplay "listview backspace qsearch";
3351 set1 active first qsearch
3354 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3355 let pattern = m_qsearch ^ Wsi.toutf8 key in
3356 let active, first =
3357 match search m_active pattern 1 with
3358 | None ->
3359 state.text <- pattern ^ " [not found]";
3360 m_active, m_first
3361 | Some active ->
3362 state.text <- pattern;
3363 active, firstof m_first active
3365 G.postRedisplay "listview qsearch add";
3366 set1 active first pattern;
3368 | 0xff1b -> (* escape *)
3369 state.text <- "";
3370 if String.length m_qsearch = 0
3371 then (
3372 G.postRedisplay "list view escape";
3373 begin
3374 match
3375 source#exit (coe self) true m_active m_first m_pan m_qsearch
3376 with
3377 | None -> m_prev_uioh
3378 | Some uioh -> uioh
3381 else (
3382 G.postRedisplay "list view kill qsearch";
3383 source#setqsearch "";
3384 coe {< m_qsearch = "" >}
3387 | 0xff0d -> (* return *)
3388 state.text <- "";
3389 let self = {< m_qsearch = "" >} in
3390 source#setqsearch "";
3391 let opt =
3392 G.postRedisplay "listview enter";
3393 if m_active >= 0 && m_active < source#getitemcount
3394 then (
3395 source#exit (coe self) false m_active m_first m_pan "";
3397 else (
3398 source#exit (coe self) true m_active m_first m_pan "";
3401 begin match opt with
3402 | None -> m_prev_uioh
3403 | Some uioh -> uioh
3406 | 0xff9f | 0xffff -> (* delete *)
3407 coe self
3409 | 0xff52 -> navigate ~-1 (* up *)
3410 | 0xff54 -> navigate 1 (* down *)
3411 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3412 | 0xff56 -> navigate fstate.maxrows (* next *)
3414 | 0xff53 -> (* right *)
3415 state.text <- "";
3416 G.postRedisplay "listview right";
3417 coe {< m_pan = m_pan - 1 >}
3419 | 0xff51 -> (* left *)
3420 state.text <- "";
3421 G.postRedisplay "listview left";
3422 coe {< m_pan = m_pan + 1 >}
3424 | 0xff50 -> (* home *)
3425 let active = find 0 1 in
3426 G.postRedisplay "listview home";
3427 set active 0;
3429 | 0xff57 -> (* end *)
3430 let first = max 0 (itemcount - fstate.maxrows) in
3431 let active = find (itemcount - 1) ~-1 in
3432 G.postRedisplay "listview end";
3433 set active first;
3435 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3436 coe self
3438 | _ ->
3439 dolog "listview unknown key %#x" key; coe self
3441 method key key mask =
3442 match state.mode with
3443 | Textentry te -> textentrykeyboard key mask te; coe self
3444 | _ -> self#key1 key mask
3446 method button button down x y _ =
3447 let opt =
3448 match button with
3449 | 1 when x > conf.winw - conf.scrollbw ->
3450 G.postRedisplay "listview scroll";
3451 if down
3452 then
3453 let _, position, sh = self#scrollph in
3454 if y > truncate position && y < truncate (position +. sh)
3455 then (
3456 state.mstate <- Mscrolly;
3457 Some (coe self)
3459 else
3460 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3461 let first = truncate (s *. float source#getitemcount) in
3462 let first = min source#getitemcount first in
3463 Some (coe {< m_first = first; m_active = first >})
3464 else (
3465 state.mstate <- Mnone;
3466 Some (coe self);
3468 | 1 when not down ->
3469 begin match self#elemunder y with
3470 | Some n ->
3471 G.postRedisplay "listview click";
3472 source#exit
3473 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3474 | _ ->
3475 Some (coe self)
3477 | n when (n == 4 || n == 5) && not down ->
3478 let len = source#getitemcount in
3479 let first =
3480 if n = 5 && m_first + fstate.maxrows >= len
3481 then
3482 m_first
3483 else
3484 let first = m_first + (if n == 4 then -1 else 1) in
3485 bound first 0 (len - 1)
3487 G.postRedisplay "listview wheel";
3488 Some (coe {< m_first = first >})
3489 | n when (n = 6 || n = 7) && not down ->
3490 let inc = m_first + (if n = 7 then -1 else 1) in
3491 G.postRedisplay "listview hwheel";
3492 Some (coe {< m_pan = m_pan + inc >})
3493 | _ ->
3494 Some (coe self)
3496 match opt with
3497 | None -> m_prev_uioh
3498 | Some uioh -> uioh
3500 method motion _ y =
3501 match state.mstate with
3502 | Mscrolly ->
3503 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3504 let first = truncate (s *. float source#getitemcount) in
3505 let first = min source#getitemcount first in
3506 G.postRedisplay "listview motion";
3507 coe {< m_first = first; m_active = first >}
3508 | _ -> coe self
3510 method pmotion x y =
3511 if x < conf.winw - conf.scrollbw
3512 then
3513 let n =
3514 match self#elemunder y with
3515 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3516 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3518 let o =
3519 if n != m_active
3520 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3521 else self
3523 coe o
3524 else (
3525 Wsi.setcursor Wsi.CURSOR_INHERIT;
3526 coe self
3529 method infochanged _ = ()
3531 method scrollpw = (0, 0.0, 0.0)
3532 method scrollph =
3533 let nfs = fstate.fontsize + 1 in
3534 let y = m_first * nfs in
3535 let itemcount = source#getitemcount in
3536 let maxi = max 0 (itemcount - fstate.maxrows) in
3537 let maxy = maxi * nfs in
3538 let p, h = scrollph y maxy in
3539 conf.scrollbw, p, h
3541 method modehash = modehash
3542 end;;
3544 class outlinelistview ~source =
3545 object (self)
3546 inherit listview
3547 ~source:(source :> lvsource)
3548 ~trusted:false
3549 ~modehash:(findkeyhash conf "outline")
3550 as super
3552 method key key mask =
3553 let calcfirst first active =
3554 if active > first
3555 then
3556 let rows = active - first in
3557 let maxrows =
3558 if String.length state.text = 0
3559 then fstate.maxrows
3560 else fstate.maxrows - 2
3562 if rows > maxrows then active - maxrows else first
3563 else active
3565 let navigate incr =
3566 let active = m_active + incr in
3567 let active = bound active 0 (source#getitemcount - 1) in
3568 let first = calcfirst m_first active in
3569 G.postRedisplay "outline navigate";
3570 coe {< m_active = active; m_first = first >}
3572 let ctrl = Wsi.withctrl mask in
3573 match key with
3574 | 110 when ctrl -> (* ctrl-n *)
3575 source#narrow m_qsearch;
3576 G.postRedisplay "outline ctrl-n";
3577 coe {< m_first = 0; m_active = 0 >}
3579 | 117 when ctrl -> (* ctrl-u *)
3580 source#denarrow;
3581 G.postRedisplay "outline ctrl-u";
3582 state.text <- "";
3583 coe {< m_first = 0; m_active = 0 >}
3585 | 108 when ctrl -> (* ctrl-l *)
3586 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3587 G.postRedisplay "outline ctrl-l";
3588 coe {< m_first = first >}
3590 | 0xff9f | 0xffff -> (* delete *)
3591 source#remove m_active;
3592 G.postRedisplay "outline delete";
3593 let active = max 0 (m_active-1) in
3594 coe {< m_first = firstof m_first active;
3595 m_active = active >}
3597 | 0xff52 -> navigate ~-1 (* up *)
3598 | 0xff54 -> navigate 1 (* down *)
3599 | 0xff55 -> (* prior *)
3600 navigate ~-(fstate.maxrows)
3601 | 0xff56 -> (* next *)
3602 navigate fstate.maxrows
3604 | 0xff53 -> (* [ctrl-]right *)
3605 let o =
3606 if ctrl
3607 then (
3608 G.postRedisplay "outline ctrl right";
3609 {< m_pan = m_pan + 1 >}
3611 else self#updownlevel 1
3613 coe o
3615 | 0xff51 -> (* [ctrl-]left *)
3616 let o =
3617 if ctrl
3618 then (
3619 G.postRedisplay "outline ctrl left";
3620 {< m_pan = m_pan - 1 >}
3622 else self#updownlevel ~-1
3624 coe o
3626 | 0xff50 -> (* home *)
3627 G.postRedisplay "outline home";
3628 coe {< m_first = 0; m_active = 0 >}
3630 | 0xff57 -> (* end *)
3631 let active = source#getitemcount - 1 in
3632 let first = max 0 (active - fstate.maxrows) in
3633 G.postRedisplay "outline end";
3634 coe {< m_active = active; m_first = first >}
3636 | _ -> super#key key mask
3639 let outlinesource usebookmarks =
3640 let empty = [||] in
3641 (object
3642 inherit lvsourcebase
3643 val mutable m_items = empty
3644 val mutable m_orig_items = empty
3645 val mutable m_prev_items = empty
3646 val mutable m_narrow_pattern = ""
3647 val mutable m_hadremovals = false
3649 method getitemcount =
3650 Array.length m_items + (if m_hadremovals then 1 else 0)
3652 method getitem n =
3653 if n == Array.length m_items && m_hadremovals
3654 then
3655 ("[Confirm removal]", 0)
3656 else
3657 let s, n, _ = m_items.(n) in
3658 (s, n)
3660 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3661 ignore (uioh, first, qsearch);
3662 let confrimremoval = m_hadremovals && active = Array.length m_items in
3663 let items =
3664 if String.length m_narrow_pattern = 0
3665 then m_orig_items
3666 else m_items
3668 if not cancel
3669 then (
3670 if not confrimremoval
3671 then(
3672 let _, _, anchor = m_items.(active) in
3673 gotoghyll (getanchory anchor);
3674 m_items <- items;
3676 else (
3677 state.bookmarks <- Array.to_list m_items;
3678 m_orig_items <- m_items;
3681 else m_items <- items;
3682 m_pan <- pan;
3683 None
3685 method hasaction _ = true
3687 method greetmsg =
3688 if Array.length m_items != Array.length m_orig_items
3689 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3690 else ""
3692 method narrow pattern =
3693 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3694 match reopt with
3695 | None -> ()
3696 | Some re ->
3697 let rec loop accu n =
3698 if n = -1
3699 then (
3700 m_narrow_pattern <- pattern;
3701 m_items <- Array.of_list accu
3703 else
3704 let (s, _, _) as o = m_items.(n) in
3705 let accu =
3706 if (try ignore (Str.search_forward re s 0); true
3707 with Not_found -> false)
3708 then o :: accu
3709 else accu
3711 loop accu (n-1)
3713 loop [] (Array.length m_items - 1)
3715 method denarrow =
3716 m_orig_items <- (
3717 if usebookmarks
3718 then Array.of_list state.bookmarks
3719 else state.outlines
3721 m_items <- m_orig_items
3723 method remove m =
3724 if usebookmarks
3725 then
3726 if m >= 0 && m < Array.length m_items
3727 then (
3728 m_hadremovals <- true;
3729 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3730 let n = if n >= m then n+1 else n in
3731 m_items.(n)
3735 method reset anchor items =
3736 m_hadremovals <- false;
3737 if m_orig_items == empty || m_prev_items != items
3738 then (
3739 m_orig_items <- items;
3740 if String.length m_narrow_pattern = 0
3741 then m_items <- items;
3743 m_prev_items <- items;
3744 let rely = getanchory anchor in
3745 let active =
3746 let rec loop n best bestd =
3747 if n = Array.length m_items
3748 then best
3749 else
3750 let (_, _, anchor) = m_items.(n) in
3751 let orely = getanchory anchor in
3752 let d = abs (orely - rely) in
3753 if d < bestd
3754 then loop (n+1) n d
3755 else loop (n+1) best bestd
3757 loop 0 ~-1 max_int
3759 m_active <- active;
3760 m_first <- firstof m_first active
3761 end)
3764 let enterselector usebookmarks =
3765 let source = outlinesource usebookmarks in
3766 fun errmsg ->
3767 let outlines =
3768 if usebookmarks
3769 then Array.of_list state.bookmarks
3770 else state.outlines
3772 if Array.length outlines = 0
3773 then (
3774 showtext ' ' errmsg;
3776 else (
3777 state.text <- source#greetmsg;
3778 Wsi.setcursor Wsi.CURSOR_INHERIT;
3779 let anchor = getanchor () in
3780 source#reset anchor outlines;
3781 state.uioh <- coe (new outlinelistview ~source);
3782 G.postRedisplay "enter selector";
3786 let enteroutlinemode =
3787 let f = enterselector false in
3788 fun ()-> f "Document has no outline";
3791 let enterbookmarkmode =
3792 let f = enterselector true in
3793 fun () -> f "Document has no bookmarks (yet)";
3796 let color_of_string s =
3797 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3798 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3802 let color_to_string (r, g, b) =
3803 let r = truncate (r *. 256.0)
3804 and g = truncate (g *. 256.0)
3805 and b = truncate (b *. 256.0) in
3806 Printf.sprintf "%d/%d/%d" r g b
3809 let irect_of_string s =
3810 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3813 let irect_to_string (x0,y0,x1,y1) =
3814 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3817 let makecheckers () =
3818 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3819 following to say:
3820 converted by Issac Trotts. July 25, 2002 *)
3821 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3822 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3823 let id = GlTex.gen_texture () in
3824 GlTex.bind_texture `texture_2d id;
3825 GlPix.store (`unpack_alignment 1);
3826 GlTex.image2d image;
3827 List.iter (GlTex.parameter ~target:`texture_2d)
3828 [ `wrap_s `repeat;
3829 `wrap_t `repeat;
3830 `mag_filter `nearest;
3831 `min_filter `nearest ];
3835 let setcheckers enabled =
3836 match state.texid with
3837 | None ->
3838 if enabled then state.texid <- Some (makecheckers ())
3840 | Some texid ->
3841 if not enabled
3842 then (
3843 GlTex.delete_texture texid;
3844 state.texid <- None;
3848 let int_of_string_with_suffix s =
3849 let l = String.length s in
3850 let s1, shift =
3851 if l > 1
3852 then
3853 let suffix = Char.lowercase s.[l-1] in
3854 match suffix with
3855 | 'k' -> String.sub s 0 (l-1), 10
3856 | 'm' -> String.sub s 0 (l-1), 20
3857 | 'g' -> String.sub s 0 (l-1), 30
3858 | _ -> s, 0
3859 else s, 0
3861 let n = int_of_string s1 in
3862 let m = n lsl shift in
3863 if m < 0 || m < n
3864 then raise (Failure "value too large")
3865 else m
3868 let string_with_suffix_of_int n =
3869 if n = 0
3870 then "0"
3871 else
3872 let n, s =
3873 if n land ((1 lsl 20) - 1) = 0
3874 then n lsr 20, "M"
3875 else (
3876 if n land ((1 lsl 10) - 1) = 0
3877 then n lsr 10, "K"
3878 else n, ""
3881 let rec loop s n =
3882 let h = n mod 1000 in
3883 let n = n / 1000 in
3884 if n = 0
3885 then string_of_int h ^ s
3886 else (
3887 let s = Printf.sprintf "_%03d%s" h s in
3888 loop s n
3891 loop "" n ^ s;
3894 let defghyllscroll = (40, 8, 32);;
3895 let ghyllscroll_of_string s =
3896 let (n, a, b) as nab =
3897 if s = "default"
3898 then defghyllscroll
3899 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3901 if n <= a || n <= b || a >= b
3902 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3903 nab;
3906 let ghyllscroll_to_string ((n, a, b) as nab) =
3907 if nab = defghyllscroll
3908 then "default"
3909 else Printf.sprintf "%d,%d,%d" n a b;
3912 let describe_location () =
3913 let f (fn, _) l =
3914 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3916 let fn, ln = List.fold_left f (-1, -1) state.layout in
3917 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3918 let percent =
3919 if maxy <= 0
3920 then 100.
3921 else (100. *. (float state.y /. float maxy))
3923 if fn = ln
3924 then
3925 Printf.sprintf "page %d of %d [%.2f%%]"
3926 (fn+1) state.pagecount percent
3927 else
3928 Printf.sprintf
3929 "pages %d-%d of %d [%.2f%%]"
3930 (fn+1) (ln+1) state.pagecount percent
3933 let setpresentationmode v =
3934 let (n, _, _) = getanchor () in
3935 let _, h = getpageyh n in
3936 let ips = if conf.presentation then calcips h else conf.interpagespace in
3937 state.anchor <- (n, 0.0, float ips);
3938 conf.presentation <- v;
3939 if conf.presentation
3940 then (
3941 if not conf.scrollbarinpm
3942 then state.scrollw <- 0;
3944 else state.scrollw <- conf.scrollbw;
3945 represent ();
3948 let enterinfomode =
3949 let btos b = if b then "\xe2\x88\x9a" else "" in
3950 let showextended = ref false in
3951 let leave mode = function
3952 | Confirm -> state.mode <- mode
3953 | Cancel -> state.mode <- mode in
3954 let src =
3955 (object
3956 val mutable m_first_time = true
3957 val mutable m_l = []
3958 val mutable m_a = [||]
3959 val mutable m_prev_uioh = nouioh
3960 val mutable m_prev_mode = View
3962 inherit lvsourcebase
3964 method reset prev_mode prev_uioh =
3965 m_a <- Array.of_list (List.rev m_l);
3966 m_l <- [];
3967 m_prev_mode <- prev_mode;
3968 m_prev_uioh <- prev_uioh;
3969 if m_first_time
3970 then (
3971 let rec loop n =
3972 if n >= Array.length m_a
3973 then ()
3974 else
3975 match m_a.(n) with
3976 | _, _, _, Action _ -> m_active <- n
3977 | _ -> loop (n+1)
3979 loop 0;
3980 m_first_time <- false;
3983 method int name get set =
3984 m_l <-
3985 (name, `int get, 1, Action (
3986 fun u ->
3987 let ondone s =
3988 try set (int_of_string s)
3989 with exn ->
3990 state.text <- Printf.sprintf "bad integer `%s': %s"
3991 s (Printexc.to_string exn)
3993 state.text <- "";
3994 let te = name ^ ": ", "", None, intentry, ondone, true in
3995 state.mode <- Textentry (te, leave m_prev_mode);
3997 )) :: m_l
3999 method int_with_suffix name get set =
4000 m_l <-
4001 (name, `intws get, 1, Action (
4002 fun u ->
4003 let ondone s =
4004 try set (int_of_string_with_suffix s)
4005 with exn ->
4006 state.text <- Printf.sprintf "bad integer `%s': %s"
4007 s (Printexc.to_string exn)
4009 state.text <- "";
4010 let te =
4011 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4013 state.mode <- Textentry (te, leave m_prev_mode);
4015 )) :: m_l
4017 method bool ?(offset=1) ?(btos=btos) name get set =
4018 m_l <-
4019 (name, `bool (btos, get), offset, Action (
4020 fun u ->
4021 let v = get () in
4022 set (not v);
4024 )) :: m_l
4026 method color name get set =
4027 m_l <-
4028 (name, `color get, 1, Action (
4029 fun u ->
4030 let invalid = (nan, nan, nan) in
4031 let ondone s =
4032 let c =
4033 try color_of_string s
4034 with exn ->
4035 state.text <- Printf.sprintf "bad color `%s': %s"
4036 s (Printexc.to_string exn);
4037 invalid
4039 if c <> invalid
4040 then set c;
4042 let te = name ^ ": ", "", None, textentry, ondone, true in
4043 state.text <- color_to_string (get ());
4044 state.mode <- Textentry (te, leave m_prev_mode);
4046 )) :: m_l
4048 method string name get set =
4049 m_l <-
4050 (name, `string get, 1, Action (
4051 fun u ->
4052 let ondone s = set s in
4053 let te = name ^ ": ", "", None, textentry, ondone, true in
4054 state.mode <- Textentry (te, leave m_prev_mode);
4056 )) :: m_l
4058 method colorspace name get set =
4059 m_l <-
4060 (name, `string get, 1, Action (
4061 fun _ ->
4062 let source =
4063 let vals = [| "rgb"; "bgr"; "gray" |] in
4064 (object
4065 inherit lvsourcebase
4067 initializer
4068 m_active <- int_of_colorspace conf.colorspace;
4069 m_first <- 0;
4071 method getitemcount = Array.length vals
4072 method getitem n = (vals.(n), 0)
4073 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4074 ignore (uioh, first, pan, qsearch);
4075 if not cancel then set active;
4076 None
4077 method hasaction _ = true
4078 end)
4080 state.text <- "";
4081 let modehash = findkeyhash conf "info" in
4082 coe (new listview ~source ~trusted:true ~modehash)
4083 )) :: m_l
4085 method caption s offset =
4086 m_l <- (s, `empty, offset, Noaction) :: m_l
4088 method caption2 s f offset =
4089 m_l <- (s, `string f, offset, Noaction) :: m_l
4091 method getitemcount = Array.length m_a
4093 method getitem n =
4094 let tostr = function
4095 | `int f -> string_of_int (f ())
4096 | `intws f -> string_with_suffix_of_int (f ())
4097 | `string f -> f ()
4098 | `color f -> color_to_string (f ())
4099 | `bool (btos, f) -> btos (f ())
4100 | `empty -> ""
4102 let name, t, offset, _ = m_a.(n) in
4103 ((let s = tostr t in
4104 if String.length s > 0
4105 then Printf.sprintf "%s\t%s" name s
4106 else name),
4107 offset)
4109 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4110 let uiohopt =
4111 if not cancel
4112 then (
4113 m_qsearch <- qsearch;
4114 let uioh =
4115 match m_a.(active) with
4116 | _, _, _, Action f -> f uioh
4117 | _ -> uioh
4119 Some uioh
4121 else None
4123 m_active <- active;
4124 m_first <- first;
4125 m_pan <- pan;
4126 uiohopt
4128 method hasaction n =
4129 match m_a.(n) with
4130 | _, _, _, Action _ -> true
4131 | _ -> false
4132 end)
4134 let rec fillsrc prevmode prevuioh =
4135 let sep () = src#caption "" 0 in
4136 let colorp name get set =
4137 src#string name
4138 (fun () -> color_to_string (get ()))
4139 (fun v ->
4141 let c = color_of_string v in
4142 set c
4143 with exn ->
4144 state.text <- Printf.sprintf "bad color `%s': %s"
4145 v (Printexc.to_string exn);
4148 let oldmode = state.mode in
4149 let birdseye = isbirdseye state.mode in
4151 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4153 src#bool "presentation mode"
4154 (fun () -> conf.presentation)
4155 (fun v -> setpresentationmode v);
4157 src#bool "ignore case in searches"
4158 (fun () -> conf.icase)
4159 (fun v -> conf.icase <- v);
4161 src#bool "preload"
4162 (fun () -> conf.preload)
4163 (fun v -> conf.preload <- v);
4165 src#bool "highlight links"
4166 (fun () -> conf.hlinks)
4167 (fun v -> conf.hlinks <- v);
4169 src#bool "under info"
4170 (fun () -> conf.underinfo)
4171 (fun v -> conf.underinfo <- v);
4173 src#bool "persistent bookmarks"
4174 (fun () -> conf.savebmarks)
4175 (fun v -> conf.savebmarks <- v);
4177 src#bool "proportional display"
4178 (fun () -> conf.proportional)
4179 (fun v -> reqlayout conf.angle v);
4181 src#bool "trim margins"
4182 (fun () -> conf.trimmargins)
4183 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4185 src#bool "persistent location"
4186 (fun () -> conf.jumpback)
4187 (fun v -> conf.jumpback <- v);
4189 sep ();
4190 src#int "inter-page space"
4191 (fun () -> conf.interpagespace)
4192 (fun n ->
4193 conf.interpagespace <- n;
4194 docolumns conf.columns;
4195 let pageno, py =
4196 match state.layout with
4197 | [] -> 0, 0
4198 | l :: _ ->
4199 l.pageno, l.pagey
4201 state.maxy <- calcheight ();
4202 let y = getpagey pageno in
4203 gotoy (y + py)
4206 src#int "page bias"
4207 (fun () -> conf.pagebias)
4208 (fun v -> conf.pagebias <- v);
4210 src#int "scroll step"
4211 (fun () -> conf.scrollstep)
4212 (fun n -> conf.scrollstep <- n);
4214 src#int "horizontal scroll step"
4215 (fun () -> conf.hscrollstep)
4216 (fun v -> conf.hscrollstep <- v);
4218 src#int "auto scroll step"
4219 (fun () ->
4220 match state.autoscroll with
4221 | Some step -> step
4222 | _ -> conf.autoscrollstep)
4223 (fun n ->
4224 if state.autoscroll <> None
4225 then state.autoscroll <- Some n;
4226 conf.autoscrollstep <- n);
4228 src#int "zoom"
4229 (fun () -> truncate (conf.zoom *. 100.))
4230 (fun v -> setzoom ((float v) /. 100.));
4232 src#int "rotation"
4233 (fun () -> conf.angle)
4234 (fun v -> reqlayout v conf.proportional);
4236 src#int "scroll bar width"
4237 (fun () -> state.scrollw)
4238 (fun v ->
4239 state.scrollw <- v;
4240 conf.scrollbw <- v;
4241 reshape conf.winw conf.winh;
4244 src#int "scroll handle height"
4245 (fun () -> conf.scrollh)
4246 (fun v -> conf.scrollh <- v;);
4248 src#int "thumbnail width"
4249 (fun () -> conf.thumbw)
4250 (fun v ->
4251 conf.thumbw <- min 4096 v;
4252 match oldmode with
4253 | Birdseye beye ->
4254 leavebirdseye beye false;
4255 enterbirdseye ()
4256 | _ -> ()
4259 let mode = state.mode in
4260 src#string "columns"
4261 (fun () ->
4262 match conf.columns with
4263 | Csingle _ -> "1"
4264 | Cmulti (multi, _) -> multicolumns_to_string multi
4265 | Csplit (count, _) -> "-" ^ string_of_int count
4267 (fun v ->
4268 let n, a, b = multicolumns_of_string v in
4269 setcolumns mode n a b);
4271 sep ();
4272 src#caption "Presentation mode" 0;
4273 src#bool "scrollbar visible"
4274 (fun () -> conf.scrollbarinpm)
4275 (fun v ->
4276 if v != conf.scrollbarinpm
4277 then (
4278 conf.scrollbarinpm <- v;
4279 if conf.presentation
4280 then (
4281 state.scrollw <- if v then conf.scrollbw else 0;
4282 reshape conf.winw conf.winh;
4287 sep ();
4288 src#caption "Pixmap cache" 0;
4289 src#int_with_suffix "size (advisory)"
4290 (fun () -> conf.memlimit)
4291 (fun v -> conf.memlimit <- v);
4293 src#caption2 "used"
4294 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4295 (string_with_suffix_of_int state.memused)
4296 (Hashtbl.length state.tilemap)) 1;
4298 sep ();
4299 src#caption "Layout" 0;
4300 src#caption2 "Dimension"
4301 (fun () ->
4302 Printf.sprintf "%dx%d (virtual %dx%d)"
4303 conf.winw conf.winh
4304 state.w state.maxy)
4306 if conf.debug
4307 then
4308 src#caption2 "Position" (fun () ->
4309 Printf.sprintf "%dx%d" state.x state.y
4311 else
4312 src#caption2 "Visible" (fun () -> describe_location ()) 1
4315 sep ();
4316 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4317 "Save these parameters as global defaults at exit"
4318 (fun () -> conf.bedefault)
4319 (fun v -> conf.bedefault <- v)
4322 sep ();
4323 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4324 src#bool ~offset:0 ~btos "Extended parameters"
4325 (fun () -> !showextended)
4326 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4327 if !showextended
4328 then (
4329 src#bool "checkers"
4330 (fun () -> conf.checkers)
4331 (fun v -> conf.checkers <- v; setcheckers v);
4332 src#bool "update cursor"
4333 (fun () -> conf.updatecurs)
4334 (fun v -> conf.updatecurs <- v);
4335 src#bool "verbose"
4336 (fun () -> conf.verbose)
4337 (fun v -> conf.verbose <- v);
4338 src#bool "invert colors"
4339 (fun () -> conf.invert)
4340 (fun v -> conf.invert <- v);
4341 src#bool "max fit"
4342 (fun () -> conf.maxhfit)
4343 (fun v -> conf.maxhfit <- v);
4344 src#bool "redirect stderr"
4345 (fun () -> conf.redirectstderr)
4346 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4347 src#string "uri launcher"
4348 (fun () -> conf.urilauncher)
4349 (fun v -> conf.urilauncher <- v);
4350 src#string "path launcher"
4351 (fun () -> conf.pathlauncher)
4352 (fun v -> conf.pathlauncher <- v);
4353 src#string "tile size"
4354 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4355 (fun v ->
4357 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4358 conf.tilew <- max 64 w;
4359 conf.tileh <- max 64 h;
4360 flushtiles ();
4361 with exn ->
4362 state.text <- Printf.sprintf "bad tile size `%s': %s"
4363 v (Printexc.to_string exn));
4364 src#int "texture count"
4365 (fun () -> conf.texcount)
4366 (fun v ->
4367 if realloctexts v
4368 then conf.texcount <- v
4369 else showtext '!' " Failed to set texture count please retry later"
4371 src#int "slice height"
4372 (fun () -> conf.sliceheight)
4373 (fun v ->
4374 conf.sliceheight <- v;
4375 wcmd "sliceh %d" conf.sliceheight;
4377 src#int "anti-aliasing level"
4378 (fun () -> conf.aalevel)
4379 (fun v ->
4380 conf.aalevel <- bound v 0 8;
4381 state.anchor <- getanchor ();
4382 opendoc state.path state.password;
4384 src#string "page scroll scaling factor"
4385 (fun () -> string_of_float conf.pgscale)
4386 (fun v ->
4388 let s = float_of_string v in
4389 conf.pgscale <- s
4390 with exn ->
4391 state.text <- Printf.sprintf
4392 "bad page scroll scaling factor `%s': %s"
4393 v (Printexc.to_string exn)
4396 src#int "ui font size"
4397 (fun () -> fstate.fontsize)
4398 (fun v -> setfontsize (bound v 5 100));
4399 src#int "hint font size"
4400 (fun () -> conf.hfsize)
4401 (fun v -> conf.hfsize <- bound v 5 100);
4402 colorp "background color"
4403 (fun () -> conf.bgcolor)
4404 (fun v -> conf.bgcolor <- v);
4405 src#bool "crop hack"
4406 (fun () -> conf.crophack)
4407 (fun v -> conf.crophack <- v);
4408 src#string "trim fuzz"
4409 (fun () -> irect_to_string conf.trimfuzz)
4410 (fun v ->
4412 conf.trimfuzz <- irect_of_string v;
4413 if conf.trimmargins
4414 then settrim true conf.trimfuzz;
4415 with exn ->
4416 state.text <- Printf.sprintf "bad irect `%s': %s"
4417 v (Printexc.to_string exn)
4419 src#string "throttle"
4420 (fun () ->
4421 match conf.maxwait with
4422 | None -> "show place holder if page is not ready"
4423 | Some time ->
4424 if time = infinity
4425 then "wait for page to fully render"
4426 else
4427 "wait " ^ string_of_float time
4428 ^ " seconds before showing placeholder"
4430 (fun v ->
4432 let f = float_of_string v in
4433 if f <= 0.0
4434 then conf.maxwait <- None
4435 else conf.maxwait <- Some f
4436 with exn ->
4437 state.text <- Printf.sprintf "bad time `%s': %s"
4438 v (Printexc.to_string exn)
4440 src#string "ghyll scroll"
4441 (fun () ->
4442 match conf.ghyllscroll with
4443 | None -> ""
4444 | Some nab -> ghyllscroll_to_string nab
4446 (fun v ->
4448 let gs =
4449 if String.length v = 0
4450 then None
4451 else Some (ghyllscroll_of_string v)
4453 conf.ghyllscroll <- gs
4454 with exn ->
4455 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4456 v (Printexc.to_string exn)
4458 src#string "selection command"
4459 (fun () -> conf.selcmd)
4460 (fun v -> conf.selcmd <- v);
4461 src#colorspace "color space"
4462 (fun () -> colorspace_to_string conf.colorspace)
4463 (fun v ->
4464 conf.colorspace <- colorspace_of_int v;
4465 wcmd "cs %d" v;
4466 load state.layout;
4470 sep ();
4471 src#caption "Document" 0;
4472 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4473 src#caption2 "Pages"
4474 (fun () -> string_of_int state.pagecount) 1;
4475 src#caption2 "Dimensions"
4476 (fun () -> string_of_int (List.length state.pdims)) 1;
4477 if conf.trimmargins
4478 then (
4479 sep ();
4480 src#caption "Trimmed margins" 0;
4481 src#caption2 "Dimensions"
4482 (fun () -> string_of_int (List.length state.pdims)) 1;
4485 sep ();
4486 src#caption "OpenGL" 0;
4487 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4488 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4489 src#reset prevmode prevuioh;
4491 fun () ->
4492 state.text <- "";
4493 let prevmode = state.mode
4494 and prevuioh = state.uioh in
4495 fillsrc prevmode prevuioh;
4496 let source = (src :> lvsource) in
4497 let modehash = findkeyhash conf "info" in
4498 state.uioh <- coe (object (self)
4499 inherit listview ~source ~trusted:true ~modehash as super
4500 val mutable m_prevmemused = 0
4501 method infochanged = function
4502 | Memused ->
4503 if m_prevmemused != state.memused
4504 then (
4505 m_prevmemused <- state.memused;
4506 G.postRedisplay "memusedchanged";
4508 | Pdim -> G.postRedisplay "pdimchanged"
4509 | Docinfo -> fillsrc prevmode prevuioh
4511 method key key mask =
4512 if not (Wsi.withctrl mask)
4513 then
4514 match key with
4515 | 0xff51 -> coe (self#updownlevel ~-1)
4516 | 0xff53 -> coe (self#updownlevel 1)
4517 | _ -> super#key key mask
4518 else super#key key mask
4519 end);
4520 G.postRedisplay "info";
4523 let enterhelpmode =
4524 let source =
4525 (object
4526 inherit lvsourcebase
4527 method getitemcount = Array.length state.help
4528 method getitem n =
4529 let s, l, _ = state.help.(n) in
4530 (s, l)
4532 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4533 let optuioh =
4534 if not cancel
4535 then (
4536 m_qsearch <- qsearch;
4537 match state.help.(active) with
4538 | _, _, Action f -> Some (f uioh)
4539 | _ -> Some (uioh)
4541 else None
4543 m_active <- active;
4544 m_first <- first;
4545 m_pan <- pan;
4546 optuioh
4548 method hasaction n =
4549 match state.help.(n) with
4550 | _, _, Action _ -> true
4551 | _ -> false
4553 initializer
4554 m_active <- -1
4555 end)
4556 in fun () ->
4557 let modehash = findkeyhash conf "help" in
4558 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4559 G.postRedisplay "help";
4562 let entermsgsmode =
4563 let msgsource =
4564 let re = Str.regexp "[\r\n]" in
4565 (object
4566 inherit lvsourcebase
4567 val mutable m_items = [||]
4569 method getitemcount = 1 + Array.length m_items
4571 method getitem n =
4572 if n = 0
4573 then "[Clear]", 0
4574 else m_items.(n-1), 0
4576 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4577 ignore uioh;
4578 if not cancel
4579 then (
4580 if active = 0
4581 then Buffer.clear state.errmsgs;
4582 m_qsearch <- qsearch;
4584 m_active <- active;
4585 m_first <- first;
4586 m_pan <- pan;
4587 None
4589 method hasaction n =
4590 n = 0
4592 method reset =
4593 state.newerrmsgs <- false;
4594 let l = Str.split re (Buffer.contents state.errmsgs) in
4595 m_items <- Array.of_list l
4597 initializer
4598 m_active <- 0
4599 end)
4600 in fun () ->
4601 state.text <- "";
4602 msgsource#reset;
4603 let source = (msgsource :> lvsource) in
4604 let modehash = findkeyhash conf "listview" in
4605 state.uioh <- coe (object
4606 inherit listview ~source ~trusted:false ~modehash as super
4607 method display =
4608 if state.newerrmsgs
4609 then msgsource#reset;
4610 super#display
4611 end);
4612 G.postRedisplay "msgs";
4615 let quickbookmark ?title () =
4616 match state.layout with
4617 | [] -> ()
4618 | l :: _ ->
4619 let title =
4620 match title with
4621 | None ->
4622 let sec = Unix.gettimeofday () in
4623 let tm = Unix.localtime sec in
4624 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4625 (l.pageno+1)
4626 tm.Unix.tm_mday
4627 tm.Unix.tm_mon
4628 (tm.Unix.tm_year + 1900)
4629 tm.Unix.tm_hour
4630 tm.Unix.tm_min
4631 | Some title -> title
4633 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4636 let doreshape w h =
4637 state.fullscreen <- None;
4638 Wsi.reshape w h;
4641 let setautoscrollspeed step goingdown =
4642 let incr = max 1 ((abs step) / 2) in
4643 let incr = if goingdown then incr else -incr in
4644 let astep = step + incr in
4645 state.autoscroll <- Some astep;
4648 let gotounder = function
4649 | Ulinkgoto (pageno, top) ->
4650 if pageno >= 0
4651 then (
4652 addnav ();
4653 gotopage1 pageno top;
4656 | Ulinkuri s ->
4657 gotouri s
4659 | Uremote (filename, pageno) ->
4660 let path =
4661 if Sys.file_exists filename
4662 then filename
4663 else
4664 let dir = Filename.dirname state.path in
4665 let path = Filename.concat dir filename in
4666 if Sys.file_exists path
4667 then path
4668 else ""
4670 if String.length path > 0
4671 then (
4672 let anchor = getanchor () in
4673 let ranchor = state.path, state.password, anchor in
4674 state.anchor <- (pageno, 0.0, 0.0);
4675 state.ranchors <- ranchor :: state.ranchors;
4676 opendoc path "";
4678 else showtext '!' ("Could not find " ^ filename)
4680 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4683 let canpan () =
4684 match conf.columns with
4685 | Csplit _ -> true
4686 | _ -> conf.zoom > 1.0
4689 let viewkeyboard key mask =
4690 let enttext te =
4691 let mode = state.mode in
4692 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4693 state.text <- "";
4694 enttext ();
4695 G.postRedisplay "view:enttext"
4697 let ctrl = Wsi.withctrl mask in
4698 let existsinrow pageno (columns, coverA, coverB) p =
4699 let last = ((pageno - coverA) mod columns) + columns in
4700 let rec any = function
4701 | [] -> false
4702 | l :: rest ->
4703 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4704 then p l
4705 else (
4706 if not (p l)
4707 then (if l.pageno = last then false else any rest)
4708 else true
4711 any state.layout
4713 match key with
4714 | 81 -> (* Q *)
4715 exit 0
4717 | 0xff63 -> (* insert *)
4718 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4719 then (
4720 state.mode <- LinkNav (Ltgendir 0);
4721 gotoy state.y;
4723 else showtext '!' "Keyboard link navigation does not work under rotation"
4725 | 0xff1b | 113 -> (* escape / q *)
4726 begin match state.mstate with
4727 | Mzoomrect _ ->
4728 state.mstate <- Mnone;
4729 Wsi.setcursor Wsi.CURSOR_INHERIT;
4730 G.postRedisplay "kill zoom rect";
4731 | _ ->
4732 match state.ranchors with
4733 | [] -> raise Quit
4734 | (path, password, anchor) :: rest ->
4735 state.ranchors <- rest;
4736 state.anchor <- anchor;
4737 opendoc path password
4738 end;
4740 | 0xff08 -> (* backspace *)
4741 gotoghyll (getnav ~-1)
4743 | 111 -> (* o *)
4744 enteroutlinemode ()
4746 | 117 -> (* u *)
4747 state.rects <- [];
4748 state.text <- "";
4749 G.postRedisplay "dehighlight";
4751 | 47 | 63 -> (* / ? *)
4752 let ondone isforw s =
4753 cbput state.hists.pat s;
4754 state.searchpattern <- s;
4755 search s isforw
4757 let s = String.create 1 in
4758 s.[0] <- Char.chr key;
4759 enttext (s, "", Some (onhist state.hists.pat),
4760 textentry, ondone (key = 47), true)
4762 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4763 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4764 setzoom (conf.zoom +. incr)
4766 | 43 | 0xffab -> (* + *)
4767 let ondone s =
4768 let n =
4769 try int_of_string s with exc ->
4770 state.text <- Printf.sprintf "bad integer `%s': %s"
4771 s (Printexc.to_string exc);
4772 max_int
4774 if n != max_int
4775 then (
4776 conf.pagebias <- n;
4777 state.text <- "page bias is now " ^ string_of_int n;
4780 enttext ("page bias: ", "", None, intentry, ondone, true)
4782 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4783 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4784 setzoom (max 0.01 (conf.zoom -. decr))
4786 | 45 | 0xffad -> (* - *)
4787 let ondone msg = state.text <- msg in
4788 enttext (
4789 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4790 optentry state.mode, ondone, true
4793 | 48 when ctrl -> (* ctrl-0 *)
4794 setzoom 1.0
4796 | 49 when ctrl -> (* ctrl-1 *)
4797 let cols =
4798 match conf.columns with
4799 | Csingle _ | Cmulti _ -> 1
4800 | Csplit (n, _) -> n
4802 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4803 if zoom < 1.0
4804 then setzoom zoom
4806 | 0xffc6 -> (* f9 *)
4807 togglebirdseye ()
4809 | 57 when ctrl -> (* ctrl-9 *)
4810 togglebirdseye ()
4812 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4813 when not ctrl -> (* 0..9 *)
4814 let ondone s =
4815 let n =
4816 try int_of_string s with exc ->
4817 state.text <- Printf.sprintf "bad integer `%s': %s"
4818 s (Printexc.to_string exc);
4821 if n >= 0
4822 then (
4823 addnav ();
4824 cbput state.hists.pag (string_of_int n);
4825 gotopage1 (n + conf.pagebias - 1) 0;
4828 let pageentry text key =
4829 match Char.unsafe_chr key with
4830 | 'g' -> TEdone text
4831 | _ -> intentry text key
4833 let text = "x" in text.[0] <- Char.chr key;
4834 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4836 | 98 -> (* b *)
4837 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4838 reshape conf.winw conf.winh;
4840 | 108 -> (* l *)
4841 conf.hlinks <- not conf.hlinks;
4842 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4843 G.postRedisplay "toggle highlightlinks";
4845 | 70 -> (* F *)
4846 state.glinks <- true;
4847 let mode = state.mode in
4848 state.mode <- Textentry (
4849 (":", "", None, linknentry, linkndone gotounder, false),
4850 (fun _ ->
4851 state.glinks <- false;
4852 state.mode <- mode)
4854 state.text <- "";
4855 G.postRedisplay "view:linkent(F)"
4857 | 121 -> (* y *)
4858 state.glinks <- true;
4859 let mode = state.mode in
4860 state.mode <- Textentry (
4861 (":", "", None, linknentry, linkndone (fun under ->
4862 match Ne.pipe () with
4863 | Ne.Exn exn ->
4864 showtext '!' (Printf.sprintf "pipe failed: %s"
4865 (Printexc.to_string exn));
4866 | Ne.Res (r, w) ->
4867 let popened =
4868 try popen conf.selcmd [r, 0; w, -1]; true
4869 with exn ->
4870 showtext '!'
4871 (Printf.sprintf "failed to execute %s: %s"
4872 conf.selcmd (Printexc.to_string exn));
4873 false
4875 let clo cap fd =
4876 Ne.clo fd (fun msg ->
4877 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4880 let s = undertext under in
4881 if popened
4882 then
4883 (try
4884 let l = String.length s in
4885 let n = Unix.write w s 0 l in
4886 if n != l
4887 then
4888 showtext '!'
4889 (Printf.sprintf
4890 "failed to write %d characters to sel pipe, wrote %d"
4893 with exn ->
4894 showtext '!'
4895 (Printf.sprintf "failed to write to sel pipe: %s"
4896 (Printexc.to_string exn)
4899 else dolog "%s" s;
4900 clo "pipe/r" r;
4901 clo "pipe/w" w;
4902 ), false
4904 fun _ ->
4905 state.glinks <- false;
4906 state.mode <- mode
4908 state.text <- "";
4909 G.postRedisplay "view:linkent"
4911 | 97 -> (* a *)
4912 begin match state.autoscroll with
4913 | Some step ->
4914 conf.autoscrollstep <- step;
4915 state.autoscroll <- None
4916 | None ->
4917 if conf.autoscrollstep = 0
4918 then state.autoscroll <- Some 1
4919 else state.autoscroll <- Some conf.autoscrollstep
4922 | 112 when ctrl -> (* ctrl-p *)
4923 launchpath ()
4925 | 80 -> (* P *)
4926 setpresentationmode (not conf.presentation);
4927 showtext ' ' ("presentation mode " ^
4928 if conf.presentation then "on" else "off");
4930 | 102 -> (* f *)
4931 begin match state.fullscreen with
4932 | None ->
4933 state.fullscreen <- Some (conf.winw, conf.winh);
4934 Wsi.fullscreen ()
4935 | Some (w, h) ->
4936 state.fullscreen <- None;
4937 doreshape w h
4940 | 112 | 78 -> (* p|N *)
4941 search state.searchpattern false
4943 | 110 | 0xffc0 -> (* n|F3 *)
4944 search state.searchpattern true
4946 | 116 -> (* t *)
4947 begin match state.layout with
4948 | [] -> ()
4949 | l :: _ ->
4950 gotoy_and_clear_text (getpagey l.pageno)
4953 | 32 -> (* space *)
4954 begin match state.layout with
4955 | [] -> ()
4956 | l :: rest ->
4957 match conf.columns with
4958 | Csingle _ ->
4959 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4960 then
4961 let y = clamp (pgscale conf.winh) in
4962 gotoghyll y
4963 else
4964 let pageno = min (l.pageno+1) (state.pagecount-1) in
4965 gotoghyll (getpagey pageno)
4966 | Cmulti ((c, _, _) as cl, _) ->
4967 if conf.presentation
4968 && (existsinrow l.pageno cl
4969 (fun l -> l.pageh > l.pagey + l.pagevh))
4970 then
4971 let y = clamp (pgscale conf.winh) in
4972 gotoghyll y
4973 else
4974 let pageno = min (l.pageno+c) (state.pagecount-1) in
4975 gotoghyll (getpagey pageno)
4976 | Csplit (n, _) ->
4977 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4978 then
4979 let pagey, pageh = getpageyh l.pageno in
4980 let pagey = pagey + pageh * l.pagecol in
4981 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4982 gotoghyll (pagey + pageh + ips)
4985 | 0xff9f | 0xffff -> (* delete *)
4986 begin match state.layout with
4987 | [] -> ()
4988 | l :: _ ->
4989 match conf.columns with
4990 | Csingle _ ->
4991 if conf.presentation && l.pagey != 0
4992 then
4993 gotoghyll (clamp (pgscale ~-(conf.winh)))
4994 else
4995 let pageno = max 0 (l.pageno-1) in
4996 gotoghyll (getpagey pageno)
4997 | Cmulti ((c, _, coverB) as cl, _) ->
4998 if conf.presentation &&
4999 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5000 then
5001 gotoghyll (clamp (pgscale ~-(conf.winh)))
5002 else
5003 let decr =
5004 if l.pageno = state.pagecount - coverB
5005 then 1
5006 else c
5008 let pageno = max 0 (l.pageno-decr) in
5009 gotoghyll (getpagey pageno)
5010 | Csplit (n, _) ->
5011 let y =
5012 if l.pagecol = 0
5013 then
5014 if l.pageno = 0
5015 then l.pagey
5016 else
5017 let pageno = max 0 (l.pageno-1) in
5018 let pagey, pageh = getpageyh pageno in
5019 pagey + (n-1)*pageh
5020 else
5021 let pagey, pageh = getpageyh l.pageno in
5022 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5024 gotoghyll y
5027 | 61 -> (* = *)
5028 showtext ' ' (describe_location ());
5030 | 119 -> (* w *)
5031 begin match state.layout with
5032 | [] -> ()
5033 | l :: _ ->
5034 doreshape (l.pagew + state.scrollw) l.pageh;
5035 G.postRedisplay "w"
5038 | 39 -> (* ' *)
5039 enterbookmarkmode ()
5041 | 104 | 0xffbe -> (* h|F1 *)
5042 enterhelpmode ()
5044 | 105 -> (* i *)
5045 enterinfomode ()
5047 | 101 when conf.redirectstderr -> (* e *)
5048 entermsgsmode ()
5050 | 109 -> (* m *)
5051 let ondone s =
5052 match state.layout with
5053 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5054 | _ -> ()
5056 enttext ("bookmark: ", "", None, textentry, ondone, true)
5058 | 126 -> (* ~ *)
5059 quickbookmark ();
5060 showtext ' ' "Quick bookmark added";
5062 | 122 -> (* z *)
5063 begin match state.layout with
5064 | l :: _ ->
5065 let rect = getpdimrect l.pagedimno in
5066 let w, h =
5067 if conf.crophack
5068 then
5069 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5070 truncate (1.2 *. (rect.(3) -. rect.(0))))
5071 else
5072 (truncate (rect.(1) -. rect.(0)),
5073 truncate (rect.(3) -. rect.(0)))
5075 let w = truncate ((float w)*.conf.zoom)
5076 and h = truncate ((float h)*.conf.zoom) in
5077 if w != 0 && h != 0
5078 then (
5079 state.anchor <- getanchor ();
5080 doreshape (w + state.scrollw) (h + conf.interpagespace)
5082 G.postRedisplay "z";
5084 | [] -> ()
5087 | 50 when ctrl -> (* ctrl-2 *)
5088 let maxw = getmaxw () in
5089 if maxw > 0.0
5090 then setzoom (maxw /. float conf.winw)
5092 | 60 | 62 -> (* < > *)
5093 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5095 | 91 | 93 -> (* [ ] *)
5096 conf.colorscale <-
5097 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5099 G.postRedisplay "brightness";
5101 | 99 when state.mode = View -> (* c *)
5102 let (c, a, b), z =
5103 match state.prevcolumns with
5104 | None -> (1, 0, 0), 1.0
5105 | Some (columns, z) ->
5106 let cab =
5107 match columns with
5108 | Csplit (c, _) -> -c, 0, 0
5109 | Cmulti ((c, a, b), _) -> c, a, b
5110 | Csingle _ -> 1, 0, 0
5112 cab, z
5114 setcolumns View c a b;
5115 setzoom z;
5117 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5118 setzoom state.prevzoom
5120 | 107 | 0xff52 -> (* k up *)
5121 begin match state.autoscroll with
5122 | None ->
5123 begin match state.mode with
5124 | Birdseye beye -> upbirdseye 1 beye
5125 | _ ->
5126 if ctrl
5127 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5128 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5130 | Some n ->
5131 setautoscrollspeed n false
5134 | 106 | 0xff54 -> (* j down *)
5135 begin match state.autoscroll with
5136 | None ->
5137 begin match state.mode with
5138 | Birdseye beye -> downbirdseye 1 beye
5139 | _ ->
5140 if ctrl
5141 then gotoy_and_clear_text (clamp (conf.winh/2))
5142 else gotoy_and_clear_text (clamp conf.scrollstep)
5144 | Some n ->
5145 setautoscrollspeed n true
5148 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5149 if canpan ()
5150 then
5151 let dx =
5152 if ctrl
5153 then conf.winw / 2
5154 else conf.hscrollstep
5156 let dx = if key = 0xff51 then dx else -dx in
5157 state.x <- state.x + dx;
5158 gotoy_and_clear_text state.y
5159 else (
5160 state.text <- "";
5161 G.postRedisplay "lef/right"
5164 | 0xff55 -> (* prior *)
5165 let y =
5166 if ctrl
5167 then
5168 match state.layout with
5169 | [] -> state.y
5170 | l :: _ -> state.y - l.pagey
5171 else
5172 clamp (pgscale (-conf.winh))
5174 gotoghyll y
5176 | 0xff56 -> (* next *)
5177 let y =
5178 if ctrl
5179 then
5180 match List.rev state.layout with
5181 | [] -> state.y
5182 | l :: _ -> getpagey l.pageno
5183 else
5184 clamp (pgscale conf.winh)
5186 gotoghyll y
5188 | 103 | 0xff50 -> (* g home *)
5189 gotoghyll 0
5190 | 71 | 0xff57 -> (* G end *)
5191 gotoghyll (clamp state.maxy)
5193 | 0xff53 when Wsi.withalt mask -> (* alt-right *)
5194 gotoghyll (getnav 1)
5195 | 0xff51 when Wsi.withalt mask -> (* alt-left *)
5196 gotoghyll (getnav ~-1)
5198 | 114 -> (* r *)
5199 state.anchor <- getanchor ();
5200 opendoc state.path state.password
5202 | 118 when conf.debug -> (* v *)
5203 state.rects <- [];
5204 List.iter (fun l ->
5205 match getopaque l.pageno with
5206 | None -> ()
5207 | Some opaque ->
5208 let x0, y0, x1, y1 = pagebbox opaque in
5209 let a,b = float x0, float y0 in
5210 let c,d = float x1, float y0 in
5211 let e,f = float x1, float y1 in
5212 let h,j = float x0, float y1 in
5213 let rect = (a,b,c,d,e,f,h,j) in
5214 debugrect rect;
5215 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5216 ) state.layout;
5217 G.postRedisplay "v";
5219 | _ ->
5220 vlog "huh? %s" (Wsi.keyname key)
5223 let linknavkeyboard key mask linknav =
5224 let getpage pageno =
5225 let rec loop = function
5226 | [] -> None
5227 | l :: _ when l.pageno = pageno -> Some l
5228 | _ :: rest -> loop rest
5229 in loop state.layout
5231 let doexact (pageno, n) =
5232 match getopaque pageno, getpage pageno with
5233 | Some opaque, Some l ->
5234 if key = 0xff0d
5235 then
5236 let under = getlink opaque n in
5237 G.postRedisplay "link gotounder";
5238 gotounder under;
5239 state.mode <- View;
5240 else
5241 let opt, dir =
5242 match key with
5243 | 0xff50 -> (* home *)
5244 Some (findlink opaque LDfirst), -1
5246 | 0xff57 -> (* end *)
5247 Some (findlink opaque LDlast), 1
5249 | 0xff51 -> (* left *)
5250 Some (findlink opaque (LDleft n)), -1
5252 | 0xff53 -> (* right *)
5253 Some (findlink opaque (LDright n)), 1
5255 | 0xff52 -> (* up *)
5256 Some (findlink opaque (LDup n)), -1
5258 | 0xff54 -> (* down *)
5259 Some (findlink opaque (LDdown n)), 1
5261 | _ -> None, 0
5263 let pwl l dir =
5264 begin match findpwl l.pageno dir with
5265 | Pwlnotfound -> ()
5266 | Pwl pageno ->
5267 let notfound dir =
5268 state.mode <- LinkNav (Ltgendir dir);
5269 let y, h = getpageyh pageno in
5270 let y =
5271 if dir < 0
5272 then y + h - conf.winh
5273 else y
5275 gotoy y
5277 begin match getopaque pageno, getpage pageno with
5278 | Some opaque, Some _ ->
5279 let link =
5280 let ld = if dir > 0 then LDfirst else LDlast in
5281 findlink opaque ld
5283 begin match link with
5284 | Lfound m ->
5285 showlinktype (getlink opaque m);
5286 state.mode <- LinkNav (Ltexact (pageno, m));
5287 G.postRedisplay "linknav jpage";
5288 | _ -> notfound dir
5289 end;
5290 | _ -> notfound dir
5291 end;
5292 end;
5294 begin match opt with
5295 | Some Lnotfound -> pwl l dir;
5296 | Some (Lfound m) ->
5297 if m = n
5298 then pwl l dir
5299 else (
5300 let _, y0, _, y1 = getlinkrect opaque m in
5301 if y0 < l.pagey
5302 then gotopage1 l.pageno y0
5303 else (
5304 let d = fstate.fontsize + 1 in
5305 if y1 - l.pagey > l.pagevh - d
5306 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5307 else G.postRedisplay "linknav";
5309 showlinktype (getlink opaque m);
5310 state.mode <- LinkNav (Ltexact (l.pageno, m));
5313 | None -> viewkeyboard key mask
5314 end;
5315 | _ -> viewkeyboard key mask
5317 if key = 0xff63
5318 then (
5319 state.mode <- View;
5320 G.postRedisplay "leave linknav"
5322 else
5323 match linknav with
5324 | Ltgendir _ -> viewkeyboard key mask
5325 | Ltexact exact -> doexact exact
5328 let keyboard key mask =
5329 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5330 then wcmd "interrupt"
5331 else state.uioh <- state.uioh#key key mask
5334 let birdseyekeyboard key mask
5335 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5336 let incr =
5337 match conf.columns with
5338 | Csingle _ -> 1
5339 | Cmulti ((c, _, _), _) -> c
5340 | Csplit _ -> failwith "bird's eye split mode"
5342 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5343 match key with
5344 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5345 let y, h = getpageyh pageno in
5346 let top = (conf.winh - h) / 2 in
5347 gotoy (max 0 (y - top))
5348 | 0xff0d -> leavebirdseye beye false
5349 | 0xff1b -> leavebirdseye beye true (* escape *)
5350 | 0xff52 -> upbirdseye incr beye (* up *)
5351 | 0xff54 -> downbirdseye incr beye (* down *)
5352 | 0xff51 -> upbirdseye 1 beye (* left *)
5353 | 0xff53 -> downbirdseye 1 beye (* right *)
5355 | 0xff55 -> (* prior *)
5356 begin match state.layout with
5357 | l :: _ ->
5358 if l.pagey != 0
5359 then (
5360 state.mode <- Birdseye (
5361 oconf, leftx, l.pageno, hooverpageno, anchor
5363 gotopage1 l.pageno 0;
5365 else (
5366 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5367 match layout with
5368 | [] -> gotoy (clamp (-conf.winh))
5369 | l :: _ ->
5370 state.mode <- Birdseye (
5371 oconf, leftx, l.pageno, hooverpageno, anchor
5373 gotopage1 l.pageno 0
5376 | [] -> gotoy (clamp (-conf.winh))
5377 end;
5379 | 0xff56 -> (* next *)
5380 begin match List.rev state.layout with
5381 | l :: _ ->
5382 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5383 begin match layout with
5384 | [] ->
5385 let incr = l.pageh - l.pagevh in
5386 if incr = 0
5387 then (
5388 state.mode <-
5389 Birdseye (
5390 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5392 G.postRedisplay "birdseye pagedown";
5394 else gotoy (clamp (incr + conf.interpagespace*2));
5396 | l :: _ ->
5397 state.mode <-
5398 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5399 gotopage1 l.pageno 0;
5402 | [] -> gotoy (clamp conf.winh)
5403 end;
5405 | 0xff50 -> (* home *)
5406 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5407 gotopage1 0 0
5409 | 0xff57 -> (* end *)
5410 let pageno = state.pagecount - 1 in
5411 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5412 if not (pagevisible state.layout pageno)
5413 then
5414 let h =
5415 match List.rev state.pdims with
5416 | [] -> conf.winh
5417 | (_, _, h, _) :: _ -> h
5419 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5420 else G.postRedisplay "birdseye end";
5421 | _ -> viewkeyboard key mask
5424 let drawpage l linkindexbase =
5425 let color =
5426 match state.mode with
5427 | Textentry _ -> scalecolor 0.4
5428 | LinkNav _
5429 | View -> scalecolor 1.0
5430 | Birdseye (_, _, pageno, hooverpageno, _) ->
5431 if l.pageno = hooverpageno
5432 then scalecolor 0.9
5433 else (
5434 if l.pageno = pageno
5435 then scalecolor 1.0
5436 else scalecolor 0.8
5439 drawtiles l color;
5440 begin match getopaque l.pageno with
5441 | Some opaque ->
5442 if tileready l l.pagex l.pagey
5443 then
5444 let x = l.pagedispx - l.pagex
5445 and y = l.pagedispy - l.pagey in
5446 let hlmask =
5447 match conf.columns with
5448 | Csingle _ | Cmulti _ ->
5449 (if conf.hlinks then 1 else 0)
5450 + (if state.glinks
5451 && not (isbirdseye state.mode) then 2 else 0)
5452 | _ -> 0
5454 let s =
5455 match state.mode with
5456 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5457 | _ -> ""
5459 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5460 else 0
5462 | _ -> 0
5463 end;
5466 let scrollindicator () =
5467 let sbw, ph, sh = state.uioh#scrollph in
5468 let sbh, pw, sw = state.uioh#scrollpw in
5470 GlDraw.color (0.64, 0.64, 0.64);
5471 GlDraw.rect
5472 (float (conf.winw - sbw), 0.)
5473 (float conf.winw, float conf.winh)
5475 GlDraw.rect
5476 (0., float (conf.winh - sbh))
5477 (float (conf.winw - state.scrollw - 1), float conf.winh)
5479 GlDraw.color (0.0, 0.0, 0.0);
5481 GlDraw.rect
5482 (float (conf.winw - sbw), ph)
5483 (float conf.winw, ph +. sh)
5485 GlDraw.rect
5486 (pw, float (conf.winh - sbh))
5487 (pw +. sw, float conf.winh)
5491 let showsel () =
5492 match state.mstate with
5493 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5496 | Msel ((x0, y0), (x1, y1)) ->
5497 let rec loop = function
5498 | l :: ls ->
5499 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5500 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5501 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5502 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5503 then
5504 match getopaque l.pageno with
5505 | Some opaque ->
5506 let x0, y0 = pagetranslatepoint l x0 y0 in
5507 let x1, y1 = pagetranslatepoint l x1 y1 in
5508 seltext opaque (x0, y0, x1, y1);
5509 | _ -> ()
5510 else loop ls
5511 | [] -> ()
5513 loop state.layout
5516 let showrects rects =
5517 Gl.enable `blend;
5518 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5519 GlDraw.polygon_mode `both `fill;
5520 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5521 List.iter
5522 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5523 List.iter (fun l ->
5524 if l.pageno = pageno
5525 then (
5526 let dx = float (l.pagedispx - l.pagex) in
5527 let dy = float (l.pagedispy - l.pagey) in
5528 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5529 GlDraw.begins `quads;
5531 GlDraw.vertex2 (x0+.dx, y0+.dy);
5532 GlDraw.vertex2 (x1+.dx, y1+.dy);
5533 GlDraw.vertex2 (x2+.dx, y2+.dy);
5534 GlDraw.vertex2 (x3+.dx, y3+.dy);
5536 GlDraw.ends ();
5538 ) state.layout
5539 ) rects
5541 Gl.disable `blend;
5544 let display () =
5545 GlClear.color (scalecolor2 conf.bgcolor);
5546 GlClear.clear [`color];
5547 let rec loop linkindexbase = function
5548 | l :: rest ->
5549 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5550 loop linkindexbase rest
5551 | [] -> ()
5553 loop 0 state.layout;
5554 let rects =
5555 match state.mode with
5556 | LinkNav (Ltexact (pageno, linkno)) ->
5557 begin match getopaque pageno with
5558 | Some opaque ->
5559 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5560 (pageno, 5, (
5561 float x0, float y0,
5562 float x1, float y0,
5563 float x1, float y1,
5564 float x0, float y1)
5565 ) :: state.rects
5566 | None -> state.rects
5568 | _ -> state.rects
5570 showrects rects;
5571 showsel ();
5572 state.uioh#display;
5573 begin match state.mstate with
5574 | Mzoomrect ((x0, y0), (x1, y1)) ->
5575 Gl.enable `blend;
5576 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5577 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5578 GlDraw.rect (float x0, float y0)
5579 (float x1, float y1);
5580 Gl.disable `blend;
5581 | _ -> ()
5582 end;
5583 enttext ();
5584 scrollindicator ();
5585 Wsi.swapb ();
5588 let zoomrect x y x1 y1 =
5589 let x0 = min x x1
5590 and x1 = max x x1
5591 and y0 = min y y1 in
5592 gotoy (state.y + y0);
5593 state.anchor <- getanchor ();
5594 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5595 let margin =
5596 if state.w < conf.winw - state.scrollw
5597 then (conf.winw - state.scrollw - state.w) / 2
5598 else 0
5600 state.x <- (state.x + margin) - x0;
5601 setzoom zoom;
5602 Wsi.setcursor Wsi.CURSOR_INHERIT;
5603 state.mstate <- Mnone;
5606 let scrollx x =
5607 let winw = conf.winw - state.scrollw - 1 in
5608 let s = float x /. float winw in
5609 let destx = truncate (float (state.w + winw) *. s) in
5610 state.x <- winw - destx;
5611 gotoy_and_clear_text state.y;
5612 state.mstate <- Mscrollx;
5615 let scrolly y =
5616 let s = float y /. float conf.winh in
5617 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5618 gotoy_and_clear_text desty;
5619 state.mstate <- Mscrolly;
5622 let viewmouse button down x y mask =
5623 match button with
5624 | n when (n == 4 || n == 5) && not down ->
5625 if Wsi.withctrl mask
5626 then (
5627 match state.mstate with
5628 | Mzoom (oldn, i) ->
5629 if oldn = n
5630 then (
5631 if i = 2
5632 then
5633 let incr =
5634 match n with
5635 | 5 ->
5636 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5637 | _ ->
5638 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5640 let zoom = conf.zoom -. incr in
5641 setzoom zoom;
5642 state.mstate <- Mzoom (n, 0);
5643 else
5644 state.mstate <- Mzoom (n, i+1);
5646 else state.mstate <- Mzoom (n, 0)
5648 | _ -> state.mstate <- Mzoom (n, 0)
5650 else (
5651 match state.autoscroll with
5652 | Some step -> setautoscrollspeed step (n=4)
5653 | None ->
5654 let incr =
5655 if n = 4
5656 then -conf.scrollstep
5657 else conf.scrollstep
5659 let incr = incr * 2 in
5660 let y = clamp incr in
5661 gotoy_and_clear_text y
5664 | n when (n = 6 || n = 7) && not down && canpan () ->
5665 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5666 gotoy_and_clear_text state.y
5668 | 1 when Wsi.withctrl mask ->
5669 if down
5670 then (
5671 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5672 state.mstate <- Mpan (x, y)
5674 else
5675 state.mstate <- Mnone
5677 | 3 ->
5678 if down
5679 then (
5680 Wsi.setcursor Wsi.CURSOR_CYCLE;
5681 let p = (x, y) in
5682 state.mstate <- Mzoomrect (p, p)
5684 else (
5685 match state.mstate with
5686 | Mzoomrect ((x0, y0), _) ->
5687 if abs (x-x0) > 10 && abs (y - y0) > 10
5688 then zoomrect x0 y0 x y
5689 else (
5690 state.mstate <- Mnone;
5691 Wsi.setcursor Wsi.CURSOR_INHERIT;
5692 G.postRedisplay "kill accidental zoom rect";
5694 | _ ->
5695 Wsi.setcursor Wsi.CURSOR_INHERIT;
5696 state.mstate <- Mnone
5699 | 1 when x > conf.winw - state.scrollw ->
5700 if down
5701 then
5702 let _, position, sh = state.uioh#scrollph in
5703 if y > truncate position && y < truncate (position +. sh)
5704 then state.mstate <- Mscrolly
5705 else scrolly y
5706 else
5707 state.mstate <- Mnone
5709 | 1 when y > conf.winh - state.hscrollh ->
5710 if down
5711 then
5712 let _, position, sw = state.uioh#scrollpw in
5713 if x > truncate position && x < truncate (position +. sw)
5714 then state.mstate <- Mscrollx
5715 else scrollx x
5716 else
5717 state.mstate <- Mnone
5719 | 1 ->
5720 let dest = if down then getunder x y else Unone in
5721 begin match dest with
5722 | Ulinkgoto _
5723 | Ulinkuri _
5724 | Uremote _
5725 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5726 gotounder dest
5728 | Unone when down ->
5729 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5730 state.mstate <- Mpan (x, y);
5732 | Unone | Utext _ ->
5733 if down
5734 then (
5735 if conf.angle mod 360 = 0
5736 then (
5737 state.mstate <- Msel ((x, y), (x, y));
5738 G.postRedisplay "mouse select";
5741 else (
5742 match state.mstate with
5743 | Mnone -> ()
5745 | Mzoom _ | Mscrollx | Mscrolly ->
5746 state.mstate <- Mnone
5748 | Mzoomrect ((x0, y0), _) ->
5749 zoomrect x0 y0 x y
5751 | Mpan _ ->
5752 Wsi.setcursor Wsi.CURSOR_INHERIT;
5753 state.mstate <- Mnone
5755 | Msel ((x0, y0), (x1, y1)) ->
5756 let rec loop = function
5757 | [] -> ()
5758 | l :: rest ->
5759 let inside =
5760 let a0 = l.pagedispy in
5761 let a1 = a0 + l.pagevh in
5762 let b0 = l.pagedispx in
5763 let b1 = b0 + l.pagevw in
5764 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5765 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5767 if inside
5768 then
5769 match getopaque l.pageno with
5770 | Some opaque ->
5771 begin
5772 match Ne.pipe () with
5773 | Ne.Exn exn ->
5774 showtext '!'
5775 (Printf.sprintf
5776 "can not create sel pipe: %s"
5777 (Printexc.to_string exn));
5778 | Ne.Res (r, w) ->
5779 let doclose what fd =
5780 Ne.clo fd (fun msg ->
5781 dolog "%s close failed: %s" what msg)
5784 popen conf.selcmd [r, 0; w, -1];
5785 copysel w opaque;
5786 doclose "pipe/r" r;
5787 G.postRedisplay "copysel";
5788 with exn ->
5789 dolog "can not execute %S: %s"
5790 conf.selcmd (Printexc.to_string exn);
5791 doclose "pipe/r" r;
5792 doclose "pipe/w" w;
5794 | None -> ()
5795 else loop rest
5797 loop state.layout;
5798 Wsi.setcursor Wsi.CURSOR_INHERIT;
5799 state.mstate <- Mnone;
5803 | _ -> ()
5806 let birdseyemouse button down x y mask
5807 (conf, leftx, _, hooverpageno, anchor) =
5808 match button with
5809 | 1 when down ->
5810 let rec loop = function
5811 | [] -> ()
5812 | l :: rest ->
5813 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5814 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5815 then (
5816 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5818 else loop rest
5820 loop state.layout
5821 | 3 -> ()
5822 | _ -> viewmouse button down x y mask
5825 let mouse button down x y mask =
5826 state.uioh <- state.uioh#button button down x y mask;
5829 let motion ~x ~y =
5830 state.uioh <- state.uioh#motion x y
5833 let pmotion ~x ~y =
5834 state.uioh <- state.uioh#pmotion x y;
5837 let uioh = object
5838 method display = ()
5840 method key key mask =
5841 begin match state.mode with
5842 | Textentry textentry -> textentrykeyboard key mask textentry
5843 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5844 | View -> viewkeyboard key mask
5845 | LinkNav linknav -> linknavkeyboard key mask linknav
5846 end;
5847 state.uioh
5849 method button button bstate x y mask =
5850 begin match state.mode with
5851 | LinkNav _
5852 | View -> viewmouse button bstate x y mask
5853 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5854 | Textentry _ -> ()
5855 end;
5856 state.uioh
5858 method motion x y =
5859 begin match state.mode with
5860 | Textentry _ -> ()
5861 | View | Birdseye _ | LinkNav _ ->
5862 match state.mstate with
5863 | Mzoom _ | Mnone -> ()
5865 | Mpan (x0, y0) ->
5866 let dx = x - x0
5867 and dy = y0 - y in
5868 state.mstate <- Mpan (x, y);
5869 if canpan ()
5870 then state.x <- state.x + dx;
5871 let y = clamp dy in
5872 gotoy_and_clear_text y
5874 | Msel (a, _) ->
5875 state.mstate <- Msel (a, (x, y));
5876 G.postRedisplay "motion select";
5878 | Mscrolly ->
5879 let y = min conf.winh (max 0 y) in
5880 scrolly y
5882 | Mscrollx ->
5883 let x = min conf.winw (max 0 x) in
5884 scrollx x
5886 | Mzoomrect (p0, _) ->
5887 state.mstate <- Mzoomrect (p0, (x, y));
5888 G.postRedisplay "motion zoomrect";
5889 end;
5890 state.uioh
5892 method pmotion x y =
5893 begin match state.mode with
5894 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5895 let rec loop = function
5896 | [] ->
5897 if hooverpageno != -1
5898 then (
5899 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5900 G.postRedisplay "pmotion birdseye no hoover";
5902 | l :: rest ->
5903 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5904 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5905 then (
5906 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5907 G.postRedisplay "pmotion birdseye hoover";
5909 else loop rest
5911 loop state.layout
5913 | Textentry _ -> ()
5915 | LinkNav _
5916 | View ->
5917 match state.mstate with
5918 | Mnone -> updateunder x y
5919 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5921 end;
5922 state.uioh
5924 method infochanged _ = ()
5926 method scrollph =
5927 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5928 let p, h = scrollph state.y maxy in
5929 state.scrollw, p, h
5931 method scrollpw =
5932 let winw = conf.winw - state.scrollw - 1 in
5933 let fwinw = float winw in
5934 let sw =
5935 let sw = fwinw /. float state.w in
5936 let sw = fwinw *. sw in
5937 max sw (float conf.scrollh)
5939 let position, sw =
5940 let f = state.w+winw in
5941 let r = float (winw-state.x) /. float f in
5942 let p = fwinw *. r in
5943 p-.sw/.2., sw
5945 let sw =
5946 if position +. sw > fwinw
5947 then fwinw -. position
5948 else sw
5950 state.hscrollh, position, sw
5952 method modehash =
5953 let modename =
5954 match state.mode with
5955 | LinkNav _ -> "links"
5956 | Textentry _ -> "textentry"
5957 | Birdseye _ -> "birdseye"
5958 | View -> "view"
5960 findkeyhash conf modename
5961 end;;
5963 module Config =
5964 struct
5965 open Parser
5967 let fontpath = ref "";;
5969 module KeyMap =
5970 Map.Make (struct type t = (int * int) let compare = compare end);;
5972 let unent s =
5973 let l = String.length s in
5974 let b = Buffer.create l in
5975 unent b s 0 l;
5976 Buffer.contents b;
5979 let home =
5980 try Sys.getenv "HOME"
5981 with exn ->
5982 prerr_endline
5983 ("Can not determine home directory location: " ^
5984 Printexc.to_string exn);
5988 let modifier_of_string = function
5989 | "alt" -> Wsi.altmask
5990 | "shift" -> Wsi.shiftmask
5991 | "ctrl" | "control" -> Wsi.ctrlmask
5992 | "meta" -> Wsi.metamask
5993 | _ -> 0
5996 let key_of_string =
5997 let r = Str.regexp "-" in
5998 fun s ->
5999 let elems = Str.full_split r s in
6000 let f n k m =
6001 let g s =
6002 let m1 = modifier_of_string s in
6003 if m1 = 0
6004 then (Wsi.namekey s, m)
6005 else (k, m lor m1)
6006 in function
6007 | Str.Delim s when n land 1 = 0 -> g s
6008 | Str.Text s -> g s
6009 | Str.Delim _ -> (k, m)
6011 let rec loop n k m = function
6012 | [] -> (k, m)
6013 | x :: xs ->
6014 let k, m = f n k m x in
6015 loop (n+1) k m xs
6017 loop 0 0 0 elems
6020 let keys_of_string =
6021 let r = Str.regexp "[ \t]" in
6022 fun s ->
6023 let elems = Str.split r s in
6024 List.map key_of_string elems
6027 let copykeyhashes c =
6028 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6031 let config_of c attrs =
6032 let apply c k v =
6034 match k with
6035 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6036 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6037 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6038 | "preload" -> { c with preload = bool_of_string v }
6039 | "page-bias" -> { c with pagebias = int_of_string v }
6040 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6041 | "horizontal-scroll-step" ->
6042 { c with hscrollstep = max (int_of_string v) 1 }
6043 | "auto-scroll-step" ->
6044 { c with autoscrollstep = max 0 (int_of_string v) }
6045 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6046 | "crop-hack" -> { c with crophack = bool_of_string v }
6047 | "throttle" ->
6048 let mw =
6049 match String.lowercase v with
6050 | "true" -> Some infinity
6051 | "false" -> None
6052 | f -> Some (float_of_string f)
6054 { c with maxwait = mw}
6055 | "highlight-links" -> { c with hlinks = bool_of_string v }
6056 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6057 | "vertical-margin" ->
6058 { c with interpagespace = max 0 (int_of_string v) }
6059 | "zoom" ->
6060 let zoom = float_of_string v /. 100. in
6061 let zoom = max zoom 0.0 in
6062 { c with zoom = zoom }
6063 | "presentation" -> { c with presentation = bool_of_string v }
6064 | "rotation-angle" -> { c with angle = int_of_string v }
6065 | "width" -> { c with winw = max 20 (int_of_string v) }
6066 | "height" -> { c with winh = max 20 (int_of_string v) }
6067 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6068 | "proportional-display" -> { c with proportional = bool_of_string v }
6069 | "pixmap-cache-size" ->
6070 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6071 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6072 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6073 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6074 | "persistent-location" -> { c with jumpback = bool_of_string v }
6075 | "background-color" -> { c with bgcolor = color_of_string v }
6076 | "scrollbar-in-presentation" ->
6077 { c with scrollbarinpm = bool_of_string v }
6078 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6079 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6080 | "mupdf-store-size" ->
6081 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6082 | "checkers" -> { c with checkers = bool_of_string v }
6083 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6084 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6085 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6086 | "uri-launcher" -> { c with urilauncher = unent v }
6087 | "path-launcher" -> { c with pathlauncher = unent v }
6088 | "color-space" -> { c with colorspace = colorspace_of_string v }
6089 | "invert-colors" -> { c with invert = bool_of_string v }
6090 | "brightness" -> { c with colorscale = float_of_string v }
6091 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6092 | "ghyllscroll" ->
6093 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6094 | "columns" ->
6095 let (n, _, _) as nab = multicolumns_of_string v in
6096 if n < 0
6097 then { c with columns = Csplit (-n, [||]) }
6098 else { c with columns = Cmulti (nab, [||]) }
6099 | "birds-eye-columns" ->
6100 { c with beyecolumns = Some (max (int_of_string v) 2) }
6101 | "selection-command" -> { c with selcmd = unent v }
6102 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6103 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6104 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6105 | _ -> c
6106 with exn ->
6107 prerr_endline ("Error processing attribute (`" ^
6108 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6111 let rec fold c = function
6112 | [] -> c
6113 | (k, v) :: rest ->
6114 let c = apply c k v in
6115 fold c rest
6117 fold { c with keyhashes = copykeyhashes c } attrs;
6120 let fromstring f pos n v d =
6121 try f v
6122 with exn ->
6123 dolog "Error processing attribute (%S=%S) at %d\n%s"
6124 n v pos (Printexc.to_string exn)
6129 let bookmark_of attrs =
6130 let rec fold title page rely visy = function
6131 | ("title", v) :: rest -> fold v page rely visy rest
6132 | ("page", v) :: rest -> fold title v rely visy rest
6133 | ("rely", v) :: rest -> fold title page v visy rest
6134 | ("visy", v) :: rest -> fold title page rely v rest
6135 | _ :: rest -> fold title page rely visy rest
6136 | [] -> title, page, rely, visy
6138 fold "invalid" "0" "0" "0" attrs
6141 let doc_of attrs =
6142 let rec fold path page rely pan visy = function
6143 | ("path", v) :: rest -> fold v page rely pan visy rest
6144 | ("page", v) :: rest -> fold path v rely pan visy rest
6145 | ("rely", v) :: rest -> fold path page v pan visy rest
6146 | ("pan", v) :: rest -> fold path page rely v visy rest
6147 | ("visy", v) :: rest -> fold path page rely pan v rest
6148 | _ :: rest -> fold path page rely pan visy rest
6149 | [] -> path, page, rely, pan, visy
6151 fold "" "0" "0" "0" "0" attrs
6154 let map_of attrs =
6155 let rec fold rs ls = function
6156 | ("out", v) :: rest -> fold v ls rest
6157 | ("in", v) :: rest -> fold rs v rest
6158 | _ :: rest -> fold ls rs rest
6159 | [] -> ls, rs
6161 fold "" "" attrs
6164 let setconf dst src =
6165 dst.scrollbw <- src.scrollbw;
6166 dst.scrollh <- src.scrollh;
6167 dst.icase <- src.icase;
6168 dst.preload <- src.preload;
6169 dst.pagebias <- src.pagebias;
6170 dst.verbose <- src.verbose;
6171 dst.scrollstep <- src.scrollstep;
6172 dst.maxhfit <- src.maxhfit;
6173 dst.crophack <- src.crophack;
6174 dst.autoscrollstep <- src.autoscrollstep;
6175 dst.maxwait <- src.maxwait;
6176 dst.hlinks <- src.hlinks;
6177 dst.underinfo <- src.underinfo;
6178 dst.interpagespace <- src.interpagespace;
6179 dst.zoom <- src.zoom;
6180 dst.presentation <- src.presentation;
6181 dst.angle <- src.angle;
6182 dst.winw <- src.winw;
6183 dst.winh <- src.winh;
6184 dst.savebmarks <- src.savebmarks;
6185 dst.memlimit <- src.memlimit;
6186 dst.proportional <- src.proportional;
6187 dst.texcount <- src.texcount;
6188 dst.sliceheight <- src.sliceheight;
6189 dst.thumbw <- src.thumbw;
6190 dst.jumpback <- src.jumpback;
6191 dst.bgcolor <- src.bgcolor;
6192 dst.scrollbarinpm <- src.scrollbarinpm;
6193 dst.tilew <- src.tilew;
6194 dst.tileh <- src.tileh;
6195 dst.mustoresize <- src.mustoresize;
6196 dst.checkers <- src.checkers;
6197 dst.aalevel <- src.aalevel;
6198 dst.trimmargins <- src.trimmargins;
6199 dst.trimfuzz <- src.trimfuzz;
6200 dst.urilauncher <- src.urilauncher;
6201 dst.colorspace <- src.colorspace;
6202 dst.invert <- src.invert;
6203 dst.colorscale <- src.colorscale;
6204 dst.redirectstderr <- src.redirectstderr;
6205 dst.ghyllscroll <- src.ghyllscroll;
6206 dst.columns <- src.columns;
6207 dst.beyecolumns <- src.beyecolumns;
6208 dst.selcmd <- src.selcmd;
6209 dst.updatecurs <- src.updatecurs;
6210 dst.pathlauncher <- src.pathlauncher;
6211 dst.keyhashes <- copykeyhashes src;
6212 dst.hfsize <- src.hfsize;
6213 dst.hscrollstep <- src.hscrollstep;
6214 dst.pgscale <- src.pgscale;
6217 let get s =
6218 let h = Hashtbl.create 10 in
6219 let dc = { defconf with angle = defconf.angle } in
6220 let rec toplevel v t spos _ =
6221 match t with
6222 | Vdata | Vcdata | Vend -> v
6223 | Vopen ("llppconfig", _, closed) ->
6224 if closed
6225 then v
6226 else { v with f = llppconfig }
6227 | Vopen _ ->
6228 error "unexpected subelement at top level" s spos
6229 | Vclose _ -> error "unexpected close at top level" s spos
6231 and llppconfig v t spos _ =
6232 match t with
6233 | Vdata | Vcdata -> v
6234 | Vend -> error "unexpected end of input in llppconfig" s spos
6235 | Vopen ("defaults", attrs, closed) ->
6236 let c = config_of dc attrs in
6237 setconf dc c;
6238 if closed
6239 then v
6240 else { v with f = defaults }
6242 | Vopen ("ui-font", attrs, closed) ->
6243 let rec getsize size = function
6244 | [] -> size
6245 | ("size", v) :: rest ->
6246 let size =
6247 fromstring int_of_string spos "size" v fstate.fontsize in
6248 getsize size rest
6249 | l -> getsize size l
6251 fstate.fontsize <- getsize fstate.fontsize attrs;
6252 if closed
6253 then v
6254 else { v with f = uifont (Buffer.create 10) }
6256 | Vopen ("doc", attrs, closed) ->
6257 let pathent, spage, srely, span, svisy = doc_of attrs in
6258 let path = unent pathent
6259 and pageno = fromstring int_of_string spos "page" spage 0
6260 and rely = fromstring float_of_string spos "rely" srely 0.0
6261 and pan = fromstring int_of_string spos "pan" span 0
6262 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6263 let c = config_of dc attrs in
6264 let anchor = (pageno, rely, visy) in
6265 if closed
6266 then (Hashtbl.add h path (c, [], pan, anchor); v)
6267 else { v with f = doc path pan anchor c [] }
6269 | Vopen _ ->
6270 error "unexpected subelement in llppconfig" s spos
6272 | Vclose "llppconfig" -> { v with f = toplevel }
6273 | Vclose _ -> error "unexpected close in llppconfig" s spos
6275 and defaults v t spos _ =
6276 match t with
6277 | Vdata | Vcdata -> v
6278 | Vend -> error "unexpected end of input in defaults" s spos
6279 | Vopen ("keymap", attrs, closed) ->
6280 let modename =
6281 try List.assoc "mode" attrs
6282 with Not_found -> "global" in
6283 if closed
6284 then v
6285 else
6286 let ret keymap =
6287 let h = findkeyhash dc modename in
6288 KeyMap.iter (Hashtbl.replace h) keymap;
6289 defaults
6291 { v with f = pkeymap ret KeyMap.empty }
6293 | Vopen (_, _, _) ->
6294 error "unexpected subelement in defaults" s spos
6296 | Vclose "defaults" ->
6297 { v with f = llppconfig }
6299 | Vclose _ -> error "unexpected close in defaults" s spos
6301 and uifont b v t spos epos =
6302 match t with
6303 | Vdata | Vcdata ->
6304 Buffer.add_substring b s spos (epos - spos);
6306 | Vopen (_, _, _) ->
6307 error "unexpected subelement in ui-font" s spos
6308 | Vclose "ui-font" ->
6309 if String.length !fontpath = 0
6310 then fontpath := Buffer.contents b;
6311 { v with f = llppconfig }
6312 | Vclose _ -> error "unexpected close in ui-font" s spos
6313 | Vend -> error "unexpected end of input in ui-font" s spos
6315 and doc path pan anchor c bookmarks v t spos _ =
6316 match t with
6317 | Vdata | Vcdata -> v
6318 | Vend -> error "unexpected end of input in doc" s spos
6319 | Vopen ("bookmarks", _, closed) ->
6320 if closed
6321 then v
6322 else { v with f = pbookmarks path pan anchor c bookmarks }
6324 | Vopen ("keymap", attrs, closed) ->
6325 let modename =
6326 try List.assoc "mode" attrs
6327 with Not_found -> "global"
6329 if closed
6330 then v
6331 else
6332 let ret keymap =
6333 let h = findkeyhash c modename in
6334 KeyMap.iter (Hashtbl.replace h) keymap;
6335 doc path pan anchor c bookmarks
6337 { v with f = pkeymap ret KeyMap.empty }
6339 | Vopen (_, _, _) ->
6340 error "unexpected subelement in doc" s spos
6342 | Vclose "doc" ->
6343 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6344 { v with f = llppconfig }
6346 | Vclose _ -> error "unexpected close in doc" s spos
6348 and pkeymap ret keymap v t spos _ =
6349 match t with
6350 | Vdata | Vcdata -> v
6351 | Vend -> error "unexpected end of input in keymap" s spos
6352 | Vopen ("map", attrs, closed) ->
6353 let r, l = map_of attrs in
6354 let kss = fromstring keys_of_string spos "in" r [] in
6355 let lss = fromstring keys_of_string spos "out" l [] in
6356 let keymap =
6357 match kss with
6358 | [] -> keymap
6359 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6360 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6362 if closed
6363 then { v with f = pkeymap ret keymap }
6364 else
6365 let f () = v in
6366 { v with f = skip "map" f }
6368 | Vopen _ ->
6369 error "unexpected subelement in keymap" s spos
6371 | Vclose "keymap" ->
6372 { v with f = ret keymap }
6374 | Vclose _ -> error "unexpected close in keymap" s spos
6376 and pbookmarks path pan anchor c bookmarks v t spos _ =
6377 match t with
6378 | Vdata | Vcdata -> v
6379 | Vend -> error "unexpected end of input in bookmarks" s spos
6380 | Vopen ("item", attrs, closed) ->
6381 let titleent, spage, srely, svisy = bookmark_of attrs in
6382 let page = fromstring int_of_string spos "page" spage 0
6383 and rely = fromstring float_of_string spos "rely" srely 0.0
6384 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6385 let bookmarks =
6386 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6388 if closed
6389 then { v with f = pbookmarks path pan anchor c bookmarks }
6390 else
6391 let f () = v in
6392 { v with f = skip "item" f }
6394 | Vopen _ ->
6395 error "unexpected subelement in bookmarks" s spos
6397 | Vclose "bookmarks" ->
6398 { v with f = doc path pan anchor c bookmarks }
6400 | Vclose _ -> error "unexpected close in bookmarks" s spos
6402 and skip tag f v t spos _ =
6403 match t with
6404 | Vdata | Vcdata -> v
6405 | Vend ->
6406 error ("unexpected end of input in skipped " ^ tag) s spos
6407 | Vopen (tag', _, closed) ->
6408 if closed
6409 then v
6410 else
6411 let f' () = { v with f = skip tag f } in
6412 { v with f = skip tag' f' }
6413 | Vclose ctag ->
6414 if tag = ctag
6415 then f ()
6416 else error ("unexpected close in skipped " ^ tag) s spos
6419 parse { f = toplevel; accu = () } s;
6420 h, dc;
6423 let do_load f ic =
6425 let len = in_channel_length ic in
6426 let s = String.create len in
6427 really_input ic s 0 len;
6428 f s;
6429 with
6430 | Parse_error (msg, s, pos) ->
6431 let subs = subs s pos in
6432 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6433 failwith ("parse error: " ^ s)
6435 | exn ->
6436 failwith ("config load error: " ^ Printexc.to_string exn)
6439 let defconfpath =
6440 let dir =
6442 let dir = Filename.concat home ".config" in
6443 if Sys.is_directory dir then dir else home
6444 with _ -> home
6446 Filename.concat dir "llpp.conf"
6449 let confpath = ref defconfpath;;
6451 let load1 f =
6452 if Sys.file_exists !confpath
6453 then
6454 match
6455 (try Some (open_in_bin !confpath)
6456 with exn ->
6457 prerr_endline
6458 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6459 Printexc.to_string exn);
6460 None
6462 with
6463 | Some ic ->
6464 let success =
6466 f (do_load get ic)
6467 with exn ->
6468 prerr_endline
6469 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6470 Printexc.to_string exn);
6471 false
6473 close_in ic;
6474 success
6476 | None -> false
6477 else
6478 f (Hashtbl.create 0, defconf)
6481 let load () =
6482 let f (h, dc) =
6483 let pc, pb, px, pa =
6485 Hashtbl.find h (Filename.basename state.path)
6486 with Not_found -> dc, [], 0, emptyanchor
6488 setconf defconf dc;
6489 setconf conf pc;
6490 state.bookmarks <- pb;
6491 state.x <- px;
6492 state.scrollw <- conf.scrollbw;
6493 if conf.jumpback
6494 then state.anchor <- pa;
6495 cbput state.hists.nav pa;
6496 true
6498 load1 f
6501 let add_attrs bb always dc c =
6502 let ob s a b =
6503 if always || a != b
6504 then Printf.bprintf bb "\n %s='%b'" s a
6505 and oi s a b =
6506 if always || a != b
6507 then Printf.bprintf bb "\n %s='%d'" s a
6508 and oI s a b =
6509 if always || a != b
6510 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6511 and oz s a b =
6512 if always || a <> b
6513 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6514 and oF s a b =
6515 if always || a <> b
6516 then Printf.bprintf bb "\n %s='%f'" s a
6517 and oc s a b =
6518 if always || a <> b
6519 then
6520 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6521 and oC s a b =
6522 if always || a <> b
6523 then
6524 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6525 and oR s a b =
6526 if always || a <> b
6527 then
6528 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6529 and os s a b =
6530 if always || a <> b
6531 then
6532 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6533 and og s a b =
6534 if always || a <> b
6535 then
6536 match a with
6537 | None -> ()
6538 | Some (_N, _A, _B) ->
6539 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6540 and oW s a b =
6541 if always || a <> b
6542 then
6543 let v =
6544 match a with
6545 | None -> "false"
6546 | Some f ->
6547 if f = infinity
6548 then "true"
6549 else string_of_float f
6551 Printf.bprintf bb "\n %s='%s'" s v
6552 and oco s a b =
6553 if always || a <> b
6554 then
6555 match a with
6556 | Cmulti ((n, a, b), _) when n > 1 ->
6557 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6558 | Csplit (n, _) when n > 1 ->
6559 Printf.bprintf bb "\n %s='%d'" s ~-n
6560 | _ -> ()
6561 and obeco s a b =
6562 if always || a <> b
6563 then
6564 match a with
6565 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6566 | _ -> ()
6568 let w, h =
6569 if always
6570 then dc.winw, dc.winh
6571 else
6572 match state.fullscreen with
6573 | Some wh -> wh
6574 | None -> c.winw, c.winh
6576 oi "width" w dc.winw;
6577 oi "height" h dc.winh;
6578 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6579 oi "scroll-handle-height" c.scrollh dc.scrollh;
6580 ob "case-insensitive-search" c.icase dc.icase;
6581 ob "preload" c.preload dc.preload;
6582 oi "page-bias" c.pagebias dc.pagebias;
6583 oi "scroll-step" c.scrollstep dc.scrollstep;
6584 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6585 ob "max-height-fit" c.maxhfit dc.maxhfit;
6586 ob "crop-hack" c.crophack dc.crophack;
6587 oW "throttle" c.maxwait dc.maxwait;
6588 ob "highlight-links" c.hlinks dc.hlinks;
6589 ob "under-cursor-info" c.underinfo dc.underinfo;
6590 oi "vertical-margin" c.interpagespace dc.interpagespace;
6591 oz "zoom" c.zoom dc.zoom;
6592 ob "presentation" c.presentation dc.presentation;
6593 oi "rotation-angle" c.angle dc.angle;
6594 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6595 ob "proportional-display" c.proportional dc.proportional;
6596 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6597 oi "tex-count" c.texcount dc.texcount;
6598 oi "slice-height" c.sliceheight dc.sliceheight;
6599 oi "thumbnail-width" c.thumbw dc.thumbw;
6600 ob "persistent-location" c.jumpback dc.jumpback;
6601 oc "background-color" c.bgcolor dc.bgcolor;
6602 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6603 oi "tile-width" c.tilew dc.tilew;
6604 oi "tile-height" c.tileh dc.tileh;
6605 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6606 ob "checkers" c.checkers dc.checkers;
6607 oi "aalevel" c.aalevel dc.aalevel;
6608 ob "trim-margins" c.trimmargins dc.trimmargins;
6609 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6610 os "uri-launcher" c.urilauncher dc.urilauncher;
6611 os "path-launcher" c.pathlauncher dc.pathlauncher;
6612 oC "color-space" c.colorspace dc.colorspace;
6613 ob "invert-colors" c.invert dc.invert;
6614 oF "brightness" c.colorscale dc.colorscale;
6615 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6616 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6617 oco "columns" c.columns dc.columns;
6618 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6619 os "selection-command" c.selcmd dc.selcmd;
6620 ob "update-cursor" c.updatecurs dc.updatecurs;
6621 oi "hint-font-size" c.hfsize dc.hfsize;
6622 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6623 oF "page-scroll-scale" c.pgscale dc.pgscale;
6626 let keymapsbuf always dc c =
6627 let bb = Buffer.create 16 in
6628 let rec loop = function
6629 | [] -> ()
6630 | (modename, h) :: rest ->
6631 let dh = findkeyhash dc modename in
6632 if always || h <> dh
6633 then (
6634 if Hashtbl.length h > 0
6635 then (
6636 if Buffer.length bb > 0
6637 then Buffer.add_char bb '\n';
6638 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6639 Hashtbl.iter (fun i o ->
6640 let isdifferent = always ||
6642 let dO = Hashtbl.find dh i in
6643 dO <> o
6644 with Not_found -> true
6646 if isdifferent
6647 then
6648 let addkm (k, m) =
6649 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6650 if Wsi.withalt m then Buffer.add_string bb "alt-";
6651 if Wsi.withshift m then Buffer.add_string bb "shift-";
6652 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6653 Buffer.add_string bb (Wsi.keyname k);
6655 let addkms l =
6656 let rec loop = function
6657 | [] -> ()
6658 | km :: [] -> addkm km
6659 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6661 loop l
6663 Buffer.add_string bb "<map in='";
6664 addkm i;
6665 match o with
6666 | KMinsrt km ->
6667 Buffer.add_string bb "' out='";
6668 addkm km;
6669 Buffer.add_string bb "'/>\n"
6671 | KMinsrl kms ->
6672 Buffer.add_string bb "' out='";
6673 addkms kms;
6674 Buffer.add_string bb "'/>\n"
6676 | KMmulti (ins, kms) ->
6677 Buffer.add_char bb ' ';
6678 addkms ins;
6679 Buffer.add_string bb "' out='";
6680 addkms kms;
6681 Buffer.add_string bb "'/>\n"
6682 ) h;
6683 Buffer.add_string bb "</keymap>";
6686 loop rest
6688 loop c.keyhashes;
6692 let save () =
6693 let uifontsize = fstate.fontsize in
6694 let bb = Buffer.create 32768 in
6695 let f (h, dc) =
6696 let dc = if conf.bedefault then conf else dc in
6697 Buffer.add_string bb "<llppconfig>\n";
6699 if String.length !fontpath > 0
6700 then
6701 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6702 uifontsize
6703 !fontpath
6704 else (
6705 if uifontsize <> 14
6706 then
6707 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6710 Buffer.add_string bb "<defaults ";
6711 add_attrs bb true dc dc;
6712 let kb = keymapsbuf true dc dc in
6713 if Buffer.length kb > 0
6714 then (
6715 Buffer.add_string bb ">\n";
6716 Buffer.add_buffer bb kb;
6717 Buffer.add_string bb "\n</defaults>\n";
6719 else Buffer.add_string bb "/>\n";
6721 let adddoc path pan anchor c bookmarks =
6722 if bookmarks == [] && c = dc && anchor = emptyanchor
6723 then ()
6724 else (
6725 Printf.bprintf bb "<doc path='%s'"
6726 (enent path 0 (String.length path));
6728 if anchor <> emptyanchor
6729 then (
6730 let n, rely, visy = anchor in
6731 Printf.bprintf bb " page='%d'" n;
6732 if rely > 1e-6
6733 then
6734 Printf.bprintf bb " rely='%f'" rely
6736 if abs_float visy > 1e-6
6737 then
6738 Printf.bprintf bb " visy='%f'" visy
6742 if pan != 0
6743 then Printf.bprintf bb " pan='%d'" pan;
6745 add_attrs bb false dc c;
6746 let kb = keymapsbuf false dc c in
6748 begin match bookmarks with
6749 | [] ->
6750 if Buffer.length kb > 0
6751 then (
6752 Buffer.add_string bb ">\n";
6753 Buffer.add_buffer bb kb;
6754 Buffer.add_string bb "\n</doc>\n";
6756 else Buffer.add_string bb "/>\n"
6757 | _ ->
6758 Buffer.add_string bb ">\n<bookmarks>\n";
6759 List.iter (fun (title, _level, (page, rely, visy)) ->
6760 Printf.bprintf bb
6761 "<item title='%s' page='%d'"
6762 (enent title 0 (String.length title))
6763 page
6765 if rely > 1e-6
6766 then
6767 Printf.bprintf bb " rely='%f'" rely
6769 if abs_float visy > 1e-6
6770 then
6771 Printf.bprintf bb " visy='%f'" visy
6773 Buffer.add_string bb "/>\n";
6774 ) bookmarks;
6775 Buffer.add_string bb "</bookmarks>";
6776 if Buffer.length kb > 0
6777 then (
6778 Buffer.add_string bb "\n";
6779 Buffer.add_buffer bb kb;
6781 Buffer.add_string bb "\n</doc>\n";
6782 end;
6786 let pan, conf =
6787 match state.mode with
6788 | Birdseye (c, pan, _, _, _) ->
6789 let beyecolumns =
6790 match conf.columns with
6791 | Cmulti ((c, _, _), _) -> Some c
6792 | Csingle _ -> None
6793 | Csplit _ -> None
6794 and columns =
6795 match c.columns with
6796 | Cmulti (c, _) -> Cmulti (c, [||])
6797 | Csingle _ -> Csingle [||]
6798 | Csplit _ -> failwith "quit from bird's eye while split"
6800 pan, { c with beyecolumns = beyecolumns; columns = columns }
6801 | _ -> state.x, conf
6803 let basename = Filename.basename state.path in
6804 adddoc basename pan (getanchor ())
6805 (let conf =
6806 let autoscrollstep =
6807 match state.autoscroll with
6808 | Some step -> step
6809 | None -> conf.autoscrollstep
6811 match state.mode with
6812 | Birdseye (bc, _, _, _, _) ->
6813 { conf with
6814 zoom = bc.zoom;
6815 presentation = bc.presentation;
6816 interpagespace = bc.interpagespace;
6817 maxwait = bc.maxwait;
6818 autoscrollstep = autoscrollstep }
6819 | _ -> { conf with autoscrollstep = autoscrollstep }
6820 in conf)
6821 (if conf.savebmarks then state.bookmarks else []);
6823 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6824 if basename <> path
6825 then adddoc path x anchor c bookmarks
6826 ) h;
6827 Buffer.add_string bb "</llppconfig>\n";
6828 true;
6830 if load1 f && Buffer.length bb > 0
6831 then
6833 let tmp = !confpath ^ ".tmp" in
6834 let oc = open_out_bin tmp in
6835 Buffer.output_buffer oc bb;
6836 close_out oc;
6837 Unix.rename tmp !confpath;
6838 with exn ->
6839 prerr_endline
6840 ("error while saving configuration: " ^ Printexc.to_string exn)
6842 end;;
6844 let () =
6845 let trimcachepath = ref "" in
6846 Arg.parse
6847 (Arg.align
6848 [("-p", Arg.String (fun s -> state.password <- s) ,
6849 "<password> Set password");
6851 ("-f", Arg.String (fun s -> Config.fontpath := s),
6852 "<path> Set path to the user interface font");
6854 ("-c", Arg.String (fun s -> Config.confpath := s),
6855 "<path> Set path to the configuration file");
6857 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6858 "<path> Set path to the trim cache file");
6860 ("-v", Arg.Unit (fun () ->
6861 Printf.printf
6862 "%s\nconfiguration path: %s\n"
6863 (version ())
6864 Config.defconfpath
6866 exit 0), " Print version and exit");
6869 (fun s -> state.path <- s)
6870 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6872 if String.length state.path = 0
6873 then (prerr_endline "file name missing"; exit 1);
6875 if not (Config.load ())
6876 then prerr_endline "failed to load configuration";
6878 let globalkeyhash = findkeyhash conf "global" in
6879 let wsfd, winw, winh = Wsi.init (object
6880 method expose =
6881 if nogeomcmds state.geomcmds || platform == Posx
6882 then display ()
6883 else (
6884 GlClear.color (scalecolor2 conf.bgcolor);
6885 GlClear.clear [`color];
6887 method display = display ()
6888 method reshape w h = reshape w h
6889 method mouse b d x y m = mouse b d x y m
6890 method motion x y = state.mpos <- (x, y); motion x y
6891 method pmotion x y = state.mpos <- (x, y); pmotion x y
6892 method key k m =
6893 let mascm = m land (
6894 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6895 ) in
6896 match state.keystate with
6897 | KSnone ->
6898 let km = k, mascm in
6899 begin
6900 match
6901 let modehash = state.uioh#modehash in
6902 try Hashtbl.find modehash km
6903 with Not_found ->
6904 try Hashtbl.find globalkeyhash km
6905 with Not_found -> KMinsrt (k, m)
6906 with
6907 | KMinsrt (k, m) -> keyboard k m
6908 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6909 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6911 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6912 List.iter (fun (k, m) -> keyboard k m) insrt;
6913 state.keystate <- KSnone
6914 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6915 state.keystate <- KSinto (keys, insrt)
6916 | _ ->
6917 state.keystate <- KSnone
6919 method enter x y = state.mpos <- (x, y); pmotion x y
6920 method leave = state.mpos <- (-1, -1)
6921 method quit = raise Quit
6922 end) conf.winw conf.winh (platform = Posx) in
6924 state.wsfd <- wsfd;
6926 if not (
6927 List.exists GlMisc.check_extension
6928 [ "GL_ARB_texture_rectangle"
6929 ; "GL_EXT_texture_recangle"
6930 ; "GL_NV_texture_rectangle" ]
6932 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6934 let cr, sw =
6935 match Ne.pipe () with
6936 | Ne.Exn exn ->
6937 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6938 exit 1
6939 | Ne.Res rw -> rw
6940 and sr, cw =
6941 match Ne.pipe () with
6942 | Ne.Exn exn ->
6943 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6944 exit 1
6945 | Ne.Res rw -> rw
6948 cloexec cr;
6949 cloexec sw;
6950 cloexec sr;
6951 cloexec cw;
6953 setcheckers conf.checkers;
6954 redirectstderr ();
6956 init (cr, cw) (
6957 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6958 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6959 !Config.fontpath, !trimcachepath
6961 state.sr <- sr;
6962 state.sw <- sw;
6963 state.text <- "Opening " ^ state.path;
6964 reshape winw winh;
6965 opendoc state.path state.password;
6966 state.uioh <- uioh;
6968 Sys.set_signal Sys.sighup
6969 (Sys.Signal_handle (fun _ -> opendoc state.path state.password));
6971 let rec loop deadline =
6972 let r =
6973 match state.errfd with
6974 | None -> [state.sr; state.wsfd]
6975 | Some fd -> [state.sr; state.wsfd; fd]
6977 if state.redisplay
6978 then (
6979 state.redisplay <- false;
6980 display ();
6982 let timeout =
6983 let now = now () in
6984 if deadline > now
6985 then (
6986 if deadline = infinity
6987 then ~-.1.0
6988 else max 0.0 (deadline -. now)
6990 else 0.0
6992 let r, _, _ =
6993 try Unix.select r [] [] timeout
6994 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6996 begin match r with
6997 | [] ->
6998 state.ghyll None;
6999 let newdeadline =
7000 if state.ghyll == noghyll
7001 then
7002 match state.autoscroll with
7003 | Some step when step != 0 ->
7004 let y = state.y + step in
7005 let y =
7006 if y < 0
7007 then state.maxy
7008 else if y >= state.maxy then 0 else y
7010 gotoy y;
7011 if state.mode = View
7012 then state.text <- "";
7013 deadline +. 0.01
7014 | _ -> infinity
7015 else deadline +. 0.01
7017 loop newdeadline
7019 | l ->
7020 let rec checkfds = function
7021 | [] -> ()
7022 | fd :: rest when fd = state.sr ->
7023 let cmd = readcmd state.sr in
7024 act cmd;
7025 checkfds rest
7027 | fd :: rest when fd = state.wsfd ->
7028 Wsi.readresp fd;
7029 checkfds rest
7031 | fd :: rest ->
7032 let s = String.create 80 in
7033 let n = Unix.read fd s 0 80 in
7034 if conf.redirectstderr
7035 then (
7036 Buffer.add_substring state.errmsgs s 0 n;
7037 state.newerrmsgs <- true;
7038 state.redisplay <- true;
7040 else (
7041 prerr_string (String.sub s 0 n);
7042 flush stderr;
7044 checkfds rest
7046 checkfds l;
7047 let newdeadline =
7048 let deadline1 =
7049 if deadline = infinity
7050 then now () +. 0.01
7051 else deadline
7053 match state.autoscroll with
7054 | Some step when step != 0 -> deadline1
7055 | _ -> if state.ghyll == noghyll then infinity else deadline1
7057 loop newdeadline
7058 end;
7061 loop infinity;
7062 with Quit ->
7063 Config.save ();