ISO_Level3_Shift support
[llpp.git] / main.ml
blob7c21aad37e3adb2e903f8b0f00258828133d5d23
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 reload () =
1864 state.anchor <- getanchor ();
1865 opendoc state.path state.password;
1868 let scalecolor c =
1869 let c = c *. conf.colorscale in
1870 (c, c, c);
1873 let scalecolor2 (r, g, b) =
1874 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1877 let docolumns = function
1878 | Csingle _ ->
1879 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1880 let rec loop pageno pdimno pdim y ph pdims =
1881 if pageno = state.pagecount
1882 then ()
1883 else
1884 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1885 match pdims with
1886 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1887 pdimno+1, pdim, rest
1888 | _ ->
1889 pdimno, pdim, pdims
1891 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1892 let y = y +
1893 (if conf.presentation
1894 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1895 else (if pageno = 0 then 0 else conf.interpagespace)
1898 a.(pageno) <- (pdimno, x, y, pdim);
1899 loop (pageno+1) pdimno pdim (y + h) h pdims
1901 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1902 conf.columns <- Csingle a;
1904 | Cmulti ((columns, coverA, coverB), _) ->
1905 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1906 let rec loop pageno pdimno pdim x y rowh pdims =
1907 let rec fixrow m = if m = pageno then () else
1908 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1909 if h < rowh
1910 then (
1911 let y = y + (rowh - h) / 2 in
1912 a.(m) <- (pdimno, x, y, pdim);
1914 fixrow (m+1)
1916 if pageno = state.pagecount
1917 then fixrow (((pageno - 1) / columns) * columns)
1918 else
1919 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1920 match pdims with
1921 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1922 pdimno+1, pdim, rest
1923 | _ ->
1924 pdimno, pdim, pdims
1926 let x, y, rowh' =
1927 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1928 then (
1929 let x = (conf.winw - state.scrollw - w) / 2 in
1930 let ips =
1931 if conf.presentation then calcips h else conf.interpagespace in
1932 x, y + ips + rowh, h
1934 else (
1935 if (pageno - coverA) mod columns = 0
1936 then (
1937 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1938 let y =
1939 if conf.presentation
1940 then
1941 let ips = calcips h in
1942 y + (if pageno = 0 then 0 else calcips rowh + ips)
1943 else
1944 y + (if pageno = 0 then 0 else conf.interpagespace)
1946 x, y + rowh, h
1948 else x, y, max rowh h
1951 let y =
1952 if pageno > 1 && (pageno - coverA) mod columns = 0
1953 then (
1954 let y =
1955 if pageno = columns && conf.presentation
1956 then (
1957 let ips = calcips rowh in
1958 for i = 0 to pred columns
1960 let (pdimno, x, y, pdim) = a.(i) in
1961 a.(i) <- (pdimno, x, y+ips, pdim)
1962 done;
1963 y+ips;
1965 else y
1967 fixrow (pageno - columns);
1970 else y
1972 a.(pageno) <- (pdimno, x, y, pdim);
1973 let x = x + w + xoff*2 + conf.interpagespace in
1974 loop (pageno+1) pdimno pdim x y rowh' pdims
1976 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1977 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1979 | Csplit (c, _) ->
1980 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1981 let rec loop pageno pdimno pdim y pdims =
1982 if pageno = state.pagecount
1983 then ()
1984 else
1985 let pdimno, ((_, w, h, _) as pdim), pdims =
1986 match pdims with
1987 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1988 pdimno+1, pdim, rest
1989 | _ ->
1990 pdimno, pdim, pdims
1992 let cw = w / c in
1993 let rec loop1 n x y =
1994 if n = c then y else (
1995 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1996 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1999 let y = loop1 0 0 y in
2000 loop (pageno+1) pdimno pdim y pdims
2002 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2003 conf.columns <- Csplit (c, a);
2006 let represent () =
2007 docolumns conf.columns;
2008 state.maxy <- calcheight ();
2009 state.hscrollh <-
2010 if state.w <= conf.winw - state.scrollw
2011 then 0
2012 else state.scrollw
2014 match state.mode with
2015 | Birdseye (_, _, pageno, _, _) ->
2016 let y, h = getpageyh pageno in
2017 let top = (conf.winh - h) / 2 in
2018 gotoy (max 0 (y - top))
2019 | _ -> gotoanchor state.anchor
2022 let reshape w h =
2023 GlDraw.viewport 0 0 w h;
2024 let firsttime = state.geomcmds == firstgeomcmds in
2025 if not firsttime && nogeomcmds state.geomcmds
2026 then state.anchor <- getanchor ();
2028 conf.winw <- w;
2029 let w = truncate (float w *. conf.zoom) - state.scrollw in
2030 let w = max w 2 in
2031 conf.winh <- h;
2032 setfontsize fstate.fontsize;
2033 GlMat.mode `modelview;
2034 GlMat.load_identity ();
2036 GlMat.mode `projection;
2037 GlMat.load_identity ();
2038 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2039 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2040 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2042 let relx =
2043 if conf.zoom <= 1.0
2044 then 0.0
2045 else float state.x /. float state.w
2047 invalidate "geometry"
2048 (fun () ->
2049 state.w <- w;
2050 if not firsttime
2051 then state.x <- truncate (relx *. float w);
2052 let w =
2053 match conf.columns with
2054 | Csingle _ -> w
2055 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2056 | Csplit (c, _) -> w * c
2058 wcmd "geometry %d %d" w h);
2061 let enttext () =
2062 let len = String.length state.text in
2063 let drawstring s =
2064 let hscrollh =
2065 match state.mode with
2066 | Textentry _
2067 | View ->
2068 let h, _, _ = state.uioh#scrollpw in
2070 | _ -> 0
2072 let rect x w =
2073 GlDraw.rect
2074 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2075 (x+.w, float (conf.winh - hscrollh))
2078 let w = float (conf.winw - state.scrollw - 1) in
2079 if state.progress >= 0.0 && state.progress < 1.0
2080 then (
2081 GlDraw.color (0.3, 0.3, 0.3);
2082 let w1 = w *. state.progress in
2083 rect 0.0 w1;
2084 GlDraw.color (0.0, 0.0, 0.0);
2085 rect w1 (w-.w1)
2087 else (
2088 GlDraw.color (0.0, 0.0, 0.0);
2089 rect 0.0 w;
2092 GlDraw.color (1.0, 1.0, 1.0);
2093 drawstring fstate.fontsize
2094 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2096 let s =
2097 match state.mode with
2098 | Textentry ((prefix, text, _, _, _, _), _) ->
2099 let s =
2100 if len > 0
2101 then
2102 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2103 else
2104 Printf.sprintf "%s%s_" prefix text
2108 | _ -> state.text
2110 let s =
2111 if state.newerrmsgs
2112 then (
2113 if not (istextentry state.mode)
2114 then
2115 let s1 = "(press 'e' to review error messasges)" in
2116 if String.length s > 0 then s ^ " " ^ s1 else s1
2117 else s
2119 else s
2121 if String.length s > 0
2122 then drawstring s
2125 let gctiles () =
2126 let len = Queue.length state.tilelru in
2127 let layout = lazy (
2128 match state.throttle with
2129 | None ->
2130 if conf.preload
2131 then preloadlayout state.y
2132 else state.layout
2133 | Some (layout, _, _) ->
2134 layout
2135 ) in
2136 let rec loop qpos =
2137 if state.memused <= conf.memlimit
2138 then ()
2139 else (
2140 if qpos < len
2141 then
2142 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2143 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2144 let (_, pw, ph, _) = getpagedim n in
2146 gen = state.gen
2147 && colorspace = conf.colorspace
2148 && angle = conf.angle
2149 && pagew = pw
2150 && pageh = ph
2151 && (
2152 let x = col*conf.tilew
2153 and y = row*conf.tileh in
2154 tilevisible (Lazy.force_val layout) n x y
2156 then Queue.push lruitem state.tilelru
2157 else (
2158 wcmd "freetile %s" p;
2159 state.memused <- state.memused - s;
2160 state.uioh#infochanged Memused;
2161 Hashtbl.remove state.tilemap k;
2163 loop (qpos+1)
2166 loop 0
2169 let flushtiles () =
2170 Queue.iter (fun (k, p, s) ->
2171 wcmd "freetile %s" p;
2172 state.memused <- state.memused - s;
2173 state.uioh#infochanged Memused;
2174 Hashtbl.remove state.tilemap k;
2175 ) state.tilelru;
2176 Queue.clear state.tilelru;
2177 load state.layout;
2180 let logcurrently = function
2181 | Idle -> dolog "Idle"
2182 | Loading (l, gen) ->
2183 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2184 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2185 dolog
2186 "Tiling %d[%d,%d] page=%s cs=%s angle"
2187 l.pageno col row pageopaque
2188 (colorspace_to_string colorspace)
2190 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2191 angle gen conf.angle state.gen
2192 tilew tileh
2193 conf.tilew conf.tileh
2195 | Outlining _ ->
2196 dolog "outlining"
2199 let act cmds =
2200 (* dolog "%S" cmds; *)
2201 let op, args =
2202 let spacepos =
2203 try String.index cmds ' '
2204 with Not_found -> -1
2206 if spacepos = -1
2207 then cmds, ""
2208 else
2209 let l = String.length cmds in
2210 let op = String.sub cmds 0 spacepos in
2211 op, begin
2212 if l - spacepos < 2 then ""
2213 else String.sub cmds (spacepos+1) (l-spacepos-1)
2216 match op with
2217 | "clear" ->
2218 state.uioh#infochanged Pdim;
2219 state.pdims <- [];
2221 | "clearrects" ->
2222 state.rects <- state.rects1;
2223 G.postRedisplay "clearrects";
2225 | "continue" ->
2226 let n =
2227 try Scanf.sscanf args "%u" (fun n -> n)
2228 with exn ->
2229 dolog "error processing 'continue' %S: %s"
2230 cmds (Printexc.to_string exn);
2231 exit 1;
2233 state.pagecount <- n;
2234 begin match state.currently with
2235 | Outlining l ->
2236 state.currently <- Idle;
2237 state.outlines <- Array.of_list (List.rev l)
2238 | _ -> ()
2239 end;
2241 let cur, cmds = state.geomcmds in
2242 if String.length cur = 0
2243 then failwith "umpossible";
2245 begin match List.rev cmds with
2246 | [] ->
2247 state.geomcmds <- "", [];
2248 represent ();
2249 | (s, f) :: rest ->
2250 f ();
2251 state.geomcmds <- s, List.rev rest;
2252 end;
2253 if conf.maxwait = None
2254 then G.postRedisplay "continue";
2256 | "title" ->
2257 Wsi.settitle args
2259 | "msg" ->
2260 showtext ' ' args
2262 | "vmsg" ->
2263 if conf.verbose
2264 then showtext ' ' args
2266 | "progress" ->
2267 let progress, text =
2269 Scanf.sscanf args "%f %n"
2270 (fun f pos ->
2271 f, String.sub args pos (String.length args - pos))
2272 with exn ->
2273 dolog "error processing 'progress' %S: %s"
2274 cmds (Printexc.to_string exn);
2275 exit 1;
2277 state.text <- text;
2278 state.progress <- progress;
2279 G.postRedisplay "progress"
2281 | "firstmatch" ->
2282 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2284 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2285 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2286 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2287 with exn ->
2288 dolog "error processing 'firstmatch' %S: %s"
2289 cmds (Printexc.to_string exn);
2290 exit 1;
2292 let y = (getpagey pageno) + truncate y0 in
2293 addnav ();
2294 gotoy y;
2295 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2297 | "match" ->
2298 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2300 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2301 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2302 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2303 with exn ->
2304 dolog "error processing 'match' %S: %s"
2305 cmds (Printexc.to_string exn);
2306 exit 1;
2308 state.rects1 <-
2309 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2311 | "page" ->
2312 let pageopaque, t =
2314 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2315 with exn ->
2316 dolog "error processing 'page' %S: %s"
2317 cmds (Printexc.to_string exn);
2318 exit 1;
2320 begin match state.currently with
2321 | Loading (l, gen) ->
2322 vlog "page %d took %f sec" l.pageno t;
2323 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2324 begin match state.throttle with
2325 | None ->
2326 let preloadedpages =
2327 if conf.preload
2328 then preloadlayout state.y
2329 else state.layout
2331 let evict () =
2332 let module IntSet =
2333 Set.Make (struct type t = int let compare = (-) end) in
2334 let set =
2335 List.fold_left (fun s l -> IntSet.add l.pageno s)
2336 IntSet.empty preloadedpages
2338 let evictedpages =
2339 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2340 if not (IntSet.mem pageno set)
2341 then (
2342 wcmd "freepage %s" opaque;
2343 key :: accu
2345 else accu
2346 ) state.pagemap []
2348 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2350 evict ();
2351 state.currently <- Idle;
2352 if gen = state.gen
2353 then (
2354 tilepage l.pageno pageopaque state.layout;
2355 load state.layout;
2356 load preloadedpages;
2357 if pagevisible state.layout l.pageno
2358 && layoutready state.layout
2359 then G.postRedisplay "page";
2362 | Some (layout, _, _) ->
2363 state.currently <- Idle;
2364 tilepage l.pageno pageopaque layout;
2365 load state.layout
2366 end;
2368 | _ ->
2369 dolog "Inconsistent loading state";
2370 logcurrently state.currently;
2371 exit 1
2374 | "tile" ->
2375 let (x, y, opaque, size, t) =
2377 Scanf.sscanf args "%u %u %s %u %f"
2378 (fun x y p size t -> (x, y, p, size, t))
2379 with exn ->
2380 dolog "error processing 'tile' %S: %s"
2381 cmds (Printexc.to_string exn);
2382 exit 1;
2384 begin match state.currently with
2385 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2386 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2388 if tilew != conf.tilew || tileh != conf.tileh
2389 then (
2390 wcmd "freetile %s" opaque;
2391 state.currently <- Idle;
2392 load state.layout;
2394 else (
2395 puttileopaque l col row gen cs angle opaque size t;
2396 state.memused <- state.memused + size;
2397 state.uioh#infochanged Memused;
2398 gctiles ();
2399 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2400 opaque, size) state.tilelru;
2402 let layout =
2403 match state.throttle with
2404 | None -> state.layout
2405 | Some (layout, _, _) -> layout
2408 state.currently <- Idle;
2409 if gen = state.gen
2410 && conf.colorspace = cs
2411 && conf.angle = angle
2412 && tilevisible layout l.pageno x y
2413 then conttiling l.pageno pageopaque;
2415 begin match state.throttle with
2416 | None ->
2417 preload state.layout;
2418 if gen = state.gen
2419 && conf.colorspace = cs
2420 && conf.angle = angle
2421 && tilevisible state.layout l.pageno x y
2422 then G.postRedisplay "tile nothrottle";
2424 | Some (layout, y, _) ->
2425 let ready = layoutready layout in
2426 if ready
2427 then (
2428 state.y <- y;
2429 state.layout <- layout;
2430 state.throttle <- None;
2431 G.postRedisplay "throttle";
2433 else load layout;
2434 end;
2437 | _ ->
2438 dolog "Inconsistent tiling state";
2439 logcurrently state.currently;
2440 exit 1
2443 | "pdim" ->
2444 let pdim =
2446 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2447 with exn ->
2448 dolog "error processing 'pdim' %S: %s"
2449 cmds (Printexc.to_string exn);
2450 exit 1;
2452 state.uioh#infochanged Pdim;
2453 state.pdims <- pdim :: state.pdims
2455 | "o" ->
2456 let (l, n, t, h, pos) =
2458 Scanf.sscanf args "%u %u %d %u %n"
2459 (fun l n t h pos -> l, n, t, h, pos)
2460 with exn ->
2461 dolog "error processing 'o' %S: %s"
2462 cmds (Printexc.to_string exn);
2463 exit 1;
2465 let s = String.sub args pos (String.length args - pos) in
2466 let outline = (s, l, (n, float t /. float h, 0.0)) in
2467 begin match state.currently with
2468 | Outlining outlines ->
2469 state.currently <- Outlining (outline :: outlines)
2470 | Idle ->
2471 state.currently <- Outlining [outline]
2472 | currently ->
2473 dolog "invalid outlining state";
2474 logcurrently currently
2477 | "info" ->
2478 state.docinfo <- (1, args) :: state.docinfo
2480 | "infoend" ->
2481 state.uioh#infochanged Docinfo;
2482 state.docinfo <- List.rev state.docinfo
2484 | _ ->
2485 dolog "unknown cmd `%S'" cmds
2488 let onhist cb =
2489 let rc = cb.rc in
2490 let action = function
2491 | HCprev -> cbget cb ~-1
2492 | HCnext -> cbget cb 1
2493 | HCfirst -> cbget cb ~-(cb.rc)
2494 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2495 and cancel () = cb.rc <- rc
2496 in (action, cancel)
2499 let search pattern forward =
2500 if String.length pattern > 0
2501 then
2502 let pn, py =
2503 match state.layout with
2504 | [] -> 0, 0
2505 | l :: _ ->
2506 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2508 wcmd "search %d %d %d %d,%s\000"
2509 (btod conf.icase) pn py (btod forward) pattern;
2512 let intentry text key =
2513 let c =
2514 if key >= 32 && key < 127
2515 then Char.chr key
2516 else '\000'
2518 match c with
2519 | '0' .. '9' ->
2520 let text = addchar text c in
2521 TEcont text
2523 | _ ->
2524 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2525 TEcont text
2528 let linknentry text key =
2529 let c =
2530 if key >= 32 && key < 127
2531 then Char.chr key
2532 else '\000'
2534 match c with
2535 | 'a' .. 'z' ->
2536 let text = addchar text c in
2537 TEcont text
2539 | _ ->
2540 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2541 TEcont text
2544 let linkndone f s =
2545 if String.length s > 0
2546 then (
2547 let n =
2548 let l = String.length s in
2549 let rec loop pos n = if pos = l then n else
2550 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2551 loop (pos+1) (n*26 + m)
2552 in loop 0 0
2554 let rec loop n = function
2555 | [] -> ()
2556 | l :: rest ->
2557 match getopaque l.pageno with
2558 | None -> loop n rest
2559 | Some opaque ->
2560 let m = getlinkcount opaque in
2561 if n < m
2562 then (
2563 let under = getlink opaque n in
2564 f under
2566 else loop (n-m) rest
2568 loop n state.layout;
2572 let textentry text key =
2573 if key land 0xff00 = 0xff00
2574 then TEcont text
2575 else TEcont (text ^ Wsi.toutf8 key)
2578 let reqlayout angle proportional =
2579 match state.throttle with
2580 | None ->
2581 if nogeomcmds state.geomcmds
2582 then state.anchor <- getanchor ();
2583 conf.angle <- angle mod 360;
2584 if conf.angle != 0
2585 then (
2586 match state.mode with
2587 | LinkNav _ -> state.mode <- View
2588 | _ -> ()
2590 conf.proportional <- proportional;
2591 invalidate "reqlayout"
2592 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2593 | _ -> ()
2596 let settrim trimmargins trimfuzz =
2597 if nogeomcmds state.geomcmds
2598 then state.anchor <- getanchor ();
2599 conf.trimmargins <- trimmargins;
2600 conf.trimfuzz <- trimfuzz;
2601 let x0, y0, x1, y1 = trimfuzz in
2602 invalidate "settrim"
2603 (fun () ->
2604 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2605 flushpages ();
2608 let setzoom zoom =
2609 match state.throttle with
2610 | None ->
2611 let zoom = max 0.01 zoom in
2612 if zoom <> conf.zoom
2613 then (
2614 state.prevzoom <- conf.zoom;
2615 conf.zoom <- zoom;
2616 reshape conf.winw conf.winh;
2617 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2620 | Some (layout, y, started) ->
2621 let time =
2622 match conf.maxwait with
2623 | None -> 0.0
2624 | Some t -> t
2626 let dt = now () -. started in
2627 if dt > time
2628 then (
2629 state.y <- y;
2630 load layout;
2634 let setcolumns mode columns coverA coverB =
2635 state.prevcolumns <- Some (conf.columns, conf.zoom);
2636 if columns < 0
2637 then (
2638 if isbirdseye mode
2639 then showtext '!' "split mode doesn't work in bird's eye"
2640 else (
2641 conf.columns <- Csplit (-columns, [||]);
2642 state.x <- 0;
2643 conf.zoom <- 1.0;
2646 else (
2647 if columns < 2
2648 then (
2649 conf.columns <- Csingle [||];
2650 state.x <- 0;
2651 setzoom 1.0;
2653 else (
2654 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2655 conf.zoom <- 1.0;
2658 reshape conf.winw conf.winh;
2661 let enterbirdseye () =
2662 let zoom = float conf.thumbw /. float conf.winw in
2663 let birdseyepageno =
2664 let cy = conf.winh / 2 in
2665 let fold = function
2666 | [] -> 0
2667 | l :: rest ->
2668 let rec fold best = function
2669 | [] -> best.pageno
2670 | l :: rest ->
2671 let d = cy - (l.pagedispy + l.pagevh/2)
2672 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2673 if abs d < abs dbest
2674 then fold l rest
2675 else best.pageno
2676 in fold l rest
2678 fold state.layout
2680 state.mode <- Birdseye (
2681 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2683 conf.zoom <- zoom;
2684 conf.presentation <- false;
2685 conf.interpagespace <- 10;
2686 conf.hlinks <- false;
2687 state.x <- 0;
2688 state.mstate <- Mnone;
2689 conf.maxwait <- None;
2690 conf.columns <- (
2691 match conf.beyecolumns with
2692 | Some c ->
2693 conf.zoom <- 1.0;
2694 Cmulti ((c, 0, 0), [||])
2695 | None -> Csingle [||]
2697 Wsi.setcursor Wsi.CURSOR_INHERIT;
2698 if conf.verbose
2699 then
2700 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2701 (100.0*.zoom)
2702 else
2703 state.text <- ""
2705 reshape conf.winw conf.winh;
2708 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2709 state.mode <- View;
2710 conf.zoom <- c.zoom;
2711 conf.presentation <- c.presentation;
2712 conf.interpagespace <- c.interpagespace;
2713 conf.maxwait <- c.maxwait;
2714 conf.hlinks <- c.hlinks;
2715 conf.beyecolumns <- (
2716 match conf.columns with
2717 | Cmulti ((c, _, _), _) -> Some c
2718 | Csingle _ -> None
2719 | Csplit _ -> failwith "leaving bird's eye split mode"
2721 conf.columns <- (
2722 match c.columns with
2723 | Cmulti (c, _) -> Cmulti (c, [||])
2724 | Csingle _ -> Csingle [||]
2725 | Csplit (c, _) -> Csplit (c, [||])
2727 state.x <- leftx;
2728 if conf.verbose
2729 then
2730 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2731 (100.0*.conf.zoom)
2733 reshape conf.winw conf.winh;
2734 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2737 let togglebirdseye () =
2738 match state.mode with
2739 | Birdseye vals -> leavebirdseye vals true
2740 | View -> enterbirdseye ()
2741 | _ -> ()
2744 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2745 let pageno = max 0 (pageno - incr) in
2746 let rec loop = function
2747 | [] -> gotopage1 pageno 0
2748 | l :: _ when l.pageno = pageno ->
2749 if l.pagedispy >= 0 && l.pagey = 0
2750 then G.postRedisplay "upbirdseye"
2751 else gotopage1 pageno 0
2752 | _ :: rest -> loop rest
2754 loop state.layout;
2755 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2758 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2759 let pageno = min (state.pagecount - 1) (pageno + incr) in
2760 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2761 let rec loop = function
2762 | [] ->
2763 let y, h = getpageyh pageno in
2764 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2765 gotoy (clamp dy)
2766 | l :: _ when l.pageno = pageno ->
2767 if l.pagevh != l.pageh
2768 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2769 else G.postRedisplay "downbirdseye"
2770 | _ :: rest -> loop rest
2772 loop state.layout
2775 let optentry mode _ key =
2776 let btos b = if b then "on" else "off" in
2777 if key >= 32 && key < 127
2778 then
2779 let c = Char.chr key in
2780 match c with
2781 | 's' ->
2782 let ondone s =
2783 try conf.scrollstep <- int_of_string s with exc ->
2784 state.text <- Printf.sprintf "bad integer `%s': %s"
2785 s (Printexc.to_string exc)
2787 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2789 | 'A' ->
2790 let ondone s =
2792 conf.autoscrollstep <- int_of_string s;
2793 if state.autoscroll <> None
2794 then state.autoscroll <- Some conf.autoscrollstep
2795 with exc ->
2796 state.text <- Printf.sprintf "bad integer `%s': %s"
2797 s (Printexc.to_string exc)
2799 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2801 | 'C' ->
2802 let ondone s =
2804 let n, a, b = multicolumns_of_string s in
2805 setcolumns mode n a b;
2806 with exc ->
2807 state.text <- Printf.sprintf "bad columns `%s': %s"
2808 s (Printexc.to_string exc)
2810 TEswitch ("columns: ", "", None, textentry, ondone, true)
2812 | 'Z' ->
2813 let ondone s =
2815 let zoom = float (int_of_string s) /. 100.0 in
2816 setzoom zoom
2817 with exc ->
2818 state.text <- Printf.sprintf "bad integer `%s': %s"
2819 s (Printexc.to_string exc)
2821 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2823 | 't' ->
2824 let ondone s =
2826 conf.thumbw <- bound (int_of_string s) 2 4096;
2827 state.text <-
2828 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2829 begin match mode with
2830 | Birdseye beye ->
2831 leavebirdseye beye false;
2832 enterbirdseye ();
2833 | _ -> ();
2835 with exc ->
2836 state.text <- Printf.sprintf "bad integer `%s': %s"
2837 s (Printexc.to_string exc)
2839 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2841 | 'R' ->
2842 let ondone s =
2843 match try
2844 Some (int_of_string s)
2845 with exc ->
2846 state.text <- Printf.sprintf "bad integer `%s': %s"
2847 s (Printexc.to_string exc);
2848 None
2849 with
2850 | Some angle -> reqlayout angle conf.proportional
2851 | None -> ()
2853 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2855 | 'i' ->
2856 conf.icase <- not conf.icase;
2857 TEdone ("case insensitive search " ^ (btos conf.icase))
2859 | 'p' ->
2860 conf.preload <- not conf.preload;
2861 gotoy state.y;
2862 TEdone ("preload " ^ (btos conf.preload))
2864 | 'v' ->
2865 conf.verbose <- not conf.verbose;
2866 TEdone ("verbose " ^ (btos conf.verbose))
2868 | 'd' ->
2869 conf.debug <- not conf.debug;
2870 TEdone ("debug " ^ (btos conf.debug))
2872 | 'h' ->
2873 conf.maxhfit <- not conf.maxhfit;
2874 state.maxy <- calcheight ();
2875 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2877 | 'c' ->
2878 conf.crophack <- not conf.crophack;
2879 TEdone ("crophack " ^ btos conf.crophack)
2881 | 'a' ->
2882 let s =
2883 match conf.maxwait with
2884 | None ->
2885 conf.maxwait <- Some infinity;
2886 "always wait for page to complete"
2887 | Some _ ->
2888 conf.maxwait <- None;
2889 "show placeholder if page is not ready"
2891 TEdone s
2893 | 'f' ->
2894 conf.underinfo <- not conf.underinfo;
2895 TEdone ("underinfo " ^ btos conf.underinfo)
2897 | 'P' ->
2898 conf.savebmarks <- not conf.savebmarks;
2899 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2901 | 'S' ->
2902 let ondone s =
2904 let pageno, py =
2905 match state.layout with
2906 | [] -> 0, 0
2907 | l :: _ ->
2908 l.pageno, l.pagey
2910 conf.interpagespace <- int_of_string s;
2911 docolumns conf.columns;
2912 state.maxy <- calcheight ();
2913 let y = getpagey pageno in
2914 gotoy (y + py)
2915 with exc ->
2916 state.text <- Printf.sprintf "bad integer `%s': %s"
2917 s (Printexc.to_string exc)
2919 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2921 | 'l' ->
2922 reqlayout conf.angle (not conf.proportional);
2923 TEdone ("proportional display " ^ btos conf.proportional)
2925 | 'T' ->
2926 settrim (not conf.trimmargins) conf.trimfuzz;
2927 TEdone ("trim margins " ^ btos conf.trimmargins)
2929 | 'I' ->
2930 conf.invert <- not conf.invert;
2931 TEdone ("invert colors " ^ btos conf.invert)
2933 | 'x' ->
2934 let ondone s =
2935 cbput state.hists.sel s;
2936 conf.selcmd <- s;
2938 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2939 textentry, ondone, true)
2941 | _ ->
2942 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2943 TEstop
2944 else
2945 TEcont state.text
2948 class type lvsource = object
2949 method getitemcount : int
2950 method getitem : int -> (string * int)
2951 method hasaction : int -> bool
2952 method exit :
2953 uioh:uioh ->
2954 cancel:bool ->
2955 active:int ->
2956 first:int ->
2957 pan:int ->
2958 qsearch:string ->
2959 uioh option
2960 method getactive : int
2961 method getfirst : int
2962 method getqsearch : string
2963 method setqsearch : string -> unit
2964 method getpan : int
2965 end;;
2967 class virtual lvsourcebase = object
2968 val mutable m_active = 0
2969 val mutable m_first = 0
2970 val mutable m_qsearch = ""
2971 val mutable m_pan = 0
2972 method getactive = m_active
2973 method getfirst = m_first
2974 method getqsearch = m_qsearch
2975 method getpan = m_pan
2976 method setqsearch s = m_qsearch <- s
2977 end;;
2979 let withoutlastutf8 s =
2980 let len = String.length s in
2981 if len = 0
2982 then s
2983 else
2984 let rec find pos =
2985 if pos = 0
2986 then pos
2987 else
2988 let b = Char.code s.[pos] in
2989 if b land 0b110000 = 0b11000000
2990 then find (pos-1)
2991 else pos-1
2993 let first =
2994 if Char.code s.[len-1] land 0x80 = 0
2995 then len-1
2996 else find (len-1)
2998 String.sub s 0 first;
3001 let textentrykeyboard
3002 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3003 let enttext te =
3004 state.mode <- Textentry (te, onleave);
3005 state.text <- "";
3006 enttext ();
3007 G.postRedisplay "textentrykeyboard enttext";
3009 let histaction cmd =
3010 match opthist with
3011 | None -> ()
3012 | Some (action, _) ->
3013 state.mode <- Textentry (
3014 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3016 G.postRedisplay "textentry histaction"
3018 match key with
3019 | 0xff08 -> (* backspace *)
3020 let s = withoutlastutf8 text in
3021 let len = String.length s in
3022 if cancelonempty && len = 0
3023 then (
3024 onleave Cancel;
3025 G.postRedisplay "textentrykeyboard after cancel";
3027 else (
3028 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3031 | 0xff0d ->
3032 ondone text;
3033 onleave Confirm;
3034 G.postRedisplay "textentrykeyboard after confirm"
3036 | 0xff52 -> histaction HCprev
3037 | 0xff54 -> histaction HCnext
3038 | 0xff50 -> histaction HCfirst
3039 | 0xff57 -> histaction HClast
3041 | 0xff1b -> (* escape*)
3042 if String.length text = 0
3043 then (
3044 begin match opthist with
3045 | None -> ()
3046 | Some (_, onhistcancel) -> onhistcancel ()
3047 end;
3048 onleave Cancel;
3049 state.text <- "";
3050 G.postRedisplay "textentrykeyboard after cancel2"
3052 else (
3053 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3056 | 0xff9f | 0xffff -> () (* delete *)
3058 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3059 begin match onkey text key with
3060 | TEdone text ->
3061 ondone text;
3062 onleave Confirm;
3063 G.postRedisplay "textentrykeyboard after confirm2";
3065 | TEcont text ->
3066 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3068 | TEstop ->
3069 onleave Cancel;
3070 G.postRedisplay "textentrykeyboard after cancel3"
3072 | TEswitch te ->
3073 state.mode <- Textentry (te, onleave);
3074 G.postRedisplay "textentrykeyboard switch";
3075 end;
3077 | _ ->
3078 vlog "unhandled key %s" (Wsi.keyname key)
3081 let firstof first active =
3082 if first > active || abs (first - active) > fstate.maxrows - 1
3083 then max 0 (active - (fstate.maxrows/2))
3084 else first
3087 let calcfirst first active =
3088 if active > first
3089 then
3090 let rows = active - first in
3091 if rows > fstate.maxrows then active - fstate.maxrows else first
3092 else active
3095 let scrollph y maxy =
3096 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3097 let sh = float conf.winh /. sh in
3098 let sh = max sh (float conf.scrollh) in
3100 let percent =
3101 if y = state.maxy
3102 then 1.0
3103 else float y /. float maxy
3105 let position = (float conf.winh -. sh) *. percent in
3107 let position =
3108 if position +. sh > float conf.winh
3109 then float conf.winh -. sh
3110 else position
3112 position, sh;
3115 let coe s = (s :> uioh);;
3117 class listview ~(source:lvsource) ~trusted ~modehash =
3118 object (self)
3119 val m_pan = source#getpan
3120 val m_first = source#getfirst
3121 val m_active = source#getactive
3122 val m_qsearch = source#getqsearch
3123 val m_prev_uioh = state.uioh
3125 method private elemunder y =
3126 let n = y / (fstate.fontsize+1) in
3127 if m_first + n < source#getitemcount
3128 then (
3129 if source#hasaction (m_first + n)
3130 then Some (m_first + n)
3131 else None
3133 else None
3135 method display =
3136 Gl.enable `blend;
3137 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3138 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3139 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3140 GlDraw.color (1., 1., 1.);
3141 Gl.enable `texture_2d;
3142 let fs = fstate.fontsize in
3143 let nfs = fs + 1 in
3144 let ww = fstate.wwidth in
3145 let tabw = 30.0*.ww in
3146 let itemcount = source#getitemcount in
3147 let rec loop row =
3148 if (row - m_first) > fstate.maxrows
3149 then ()
3150 else (
3151 if row >= 0 && row < itemcount
3152 then (
3153 let (s, level) = source#getitem row in
3154 let y = (row - m_first) * nfs in
3155 let x = 5.0 +. float (level + m_pan) *. ww in
3156 if row = m_active
3157 then (
3158 Gl.disable `texture_2d;
3159 GlDraw.polygon_mode `both `line;
3160 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3161 GlDraw.rect (1., float (y + 1))
3162 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3163 GlDraw.polygon_mode `both `fill;
3164 GlDraw.color (1., 1., 1.);
3165 Gl.enable `texture_2d;
3168 let drawtabularstring s =
3169 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3170 if trusted
3171 then
3172 let tabpos = try String.index s '\t' with Not_found -> -1 in
3173 if tabpos > 0
3174 then
3175 let len = String.length s - tabpos - 1 in
3176 let s1 = String.sub s 0 tabpos
3177 and s2 = String.sub s (tabpos + 1) len in
3178 let nx = drawstr x s1 in
3179 let sw = nx -. x in
3180 let x = x +. (max tabw sw) in
3181 drawstr x s2
3182 else
3183 drawstr x s
3184 else
3185 drawstr x s
3187 let _ = drawtabularstring s in
3188 loop (row+1)
3192 loop m_first;
3193 Gl.disable `blend;
3194 Gl.disable `texture_2d;
3196 method updownlevel incr =
3197 let len = source#getitemcount in
3198 let curlevel =
3199 if m_active >= 0 && m_active < len
3200 then snd (source#getitem m_active)
3201 else -1
3203 let rec flow i =
3204 if i = len then i-1 else if i = -1 then 0 else
3205 let _, l = source#getitem i in
3206 if l != curlevel then i else flow (i+incr)
3208 let active = flow m_active in
3209 let first = calcfirst m_first active in
3210 G.postRedisplay "outline updownlevel";
3211 {< m_active = active; m_first = first >}
3213 method private key1 key mask =
3214 let set1 active first qsearch =
3215 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3217 let search active pattern incr =
3218 let dosearch re =
3219 let rec loop n =
3220 if n >= 0 && n < source#getitemcount
3221 then (
3222 let s, _ = source#getitem n in
3224 (try ignore (Str.search_forward re s 0); true
3225 with Not_found -> false)
3226 then Some n
3227 else loop (n + incr)
3229 else None
3231 loop active
3234 let re = Str.regexp_case_fold pattern in
3235 dosearch re
3236 with Failure s ->
3237 state.text <- s;
3238 None
3240 let itemcount = source#getitemcount in
3241 let find start incr =
3242 let rec find i =
3243 if i = -1 || i = itemcount
3244 then -1
3245 else (
3246 if source#hasaction i
3247 then i
3248 else find (i + incr)
3251 find start
3253 let set active first =
3254 let first = bound first 0 (itemcount - fstate.maxrows) in
3255 state.text <- "";
3256 coe {< m_active = active; m_first = first >}
3258 let navigate incr =
3259 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3260 let active, first =
3261 let incr1 = if incr > 0 then 1 else -1 in
3262 if isvisible m_first m_active
3263 then
3264 let next =
3265 let next = m_active + incr in
3266 let next =
3267 if next < 0 || next >= itemcount
3268 then -1
3269 else find next incr1
3271 if next = -1 || abs (m_active - next) > fstate.maxrows
3272 then -1
3273 else next
3275 if next = -1
3276 then
3277 let first = m_first + incr in
3278 let first = bound first 0 (itemcount - 1) in
3279 let next =
3280 let next = m_active + incr in
3281 let next = bound next 0 (itemcount - 1) in
3282 find next ~-incr1
3284 let active = if next = -1 then m_active else next in
3285 active, first
3286 else
3287 let first = min next m_first in
3288 let first =
3289 if abs (next - first) > fstate.maxrows
3290 then first + incr
3291 else first
3293 next, first
3294 else
3295 let first = m_first + incr in
3296 let first = bound first 0 (itemcount - 1) in
3297 let active =
3298 let next = m_active + incr in
3299 let next = bound next 0 (itemcount - 1) in
3300 let next = find next incr1 in
3301 let active =
3302 if next = -1 || abs (m_active - first) > fstate.maxrows
3303 then (
3304 let active = if m_active = -1 then next else m_active in
3305 active
3307 else next
3309 if isvisible first active
3310 then active
3311 else -1
3313 active, first
3315 G.postRedisplay "listview navigate";
3316 set active first;
3318 match key with
3319 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3320 let incr = if key = 0x72 then -1 else 1 in
3321 let active, first =
3322 match search (m_active + incr) m_qsearch incr with
3323 | None ->
3324 state.text <- m_qsearch ^ " [not found]";
3325 m_active, m_first
3326 | Some active ->
3327 state.text <- m_qsearch;
3328 active, firstof m_first active
3330 G.postRedisplay "listview ctrl-r/s";
3331 set1 active first m_qsearch;
3333 | 0xff08 -> (* backspace *)
3334 if String.length m_qsearch = 0
3335 then coe self
3336 else (
3337 let qsearch = withoutlastutf8 m_qsearch in
3338 let len = String.length qsearch in
3339 if len = 0
3340 then (
3341 state.text <- "";
3342 G.postRedisplay "listview empty qsearch";
3343 set1 m_active m_first "";
3345 else
3346 let active, first =
3347 match search m_active qsearch ~-1 with
3348 | None ->
3349 state.text <- qsearch ^ " [not found]";
3350 m_active, m_first
3351 | Some active ->
3352 state.text <- qsearch;
3353 active, firstof m_first active
3355 G.postRedisplay "listview backspace qsearch";
3356 set1 active first qsearch
3359 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3360 let pattern = m_qsearch ^ Wsi.toutf8 key in
3361 let active, first =
3362 match search m_active pattern 1 with
3363 | None ->
3364 state.text <- pattern ^ " [not found]";
3365 m_active, m_first
3366 | Some active ->
3367 state.text <- pattern;
3368 active, firstof m_first active
3370 G.postRedisplay "listview qsearch add";
3371 set1 active first pattern;
3373 | 0xff1b -> (* escape *)
3374 state.text <- "";
3375 if String.length m_qsearch = 0
3376 then (
3377 G.postRedisplay "list view escape";
3378 begin
3379 match
3380 source#exit (coe self) true m_active m_first m_pan m_qsearch
3381 with
3382 | None -> m_prev_uioh
3383 | Some uioh -> uioh
3386 else (
3387 G.postRedisplay "list view kill qsearch";
3388 source#setqsearch "";
3389 coe {< m_qsearch = "" >}
3392 | 0xff0d -> (* return *)
3393 state.text <- "";
3394 let self = {< m_qsearch = "" >} in
3395 source#setqsearch "";
3396 let opt =
3397 G.postRedisplay "listview enter";
3398 if m_active >= 0 && m_active < source#getitemcount
3399 then (
3400 source#exit (coe self) false m_active m_first m_pan "";
3402 else (
3403 source#exit (coe self) true m_active m_first m_pan "";
3406 begin match opt with
3407 | None -> m_prev_uioh
3408 | Some uioh -> uioh
3411 | 0xff9f | 0xffff -> (* delete *)
3412 coe self
3414 | 0xff52 -> navigate ~-1 (* up *)
3415 | 0xff54 -> navigate 1 (* down *)
3416 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3417 | 0xff56 -> navigate fstate.maxrows (* next *)
3419 | 0xff53 -> (* right *)
3420 state.text <- "";
3421 G.postRedisplay "listview right";
3422 coe {< m_pan = m_pan - 1 >}
3424 | 0xff51 -> (* left *)
3425 state.text <- "";
3426 G.postRedisplay "listview left";
3427 coe {< m_pan = m_pan + 1 >}
3429 | 0xff50 -> (* home *)
3430 let active = find 0 1 in
3431 G.postRedisplay "listview home";
3432 set active 0;
3434 | 0xff57 -> (* end *)
3435 let first = max 0 (itemcount - fstate.maxrows) in
3436 let active = find (itemcount - 1) ~-1 in
3437 G.postRedisplay "listview end";
3438 set active first;
3440 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3441 coe self
3443 | _ ->
3444 dolog "listview unknown key %#x" key; coe self
3446 method key key mask =
3447 match state.mode with
3448 | Textentry te -> textentrykeyboard key mask te; coe self
3449 | _ -> self#key1 key mask
3451 method button button down x y _ =
3452 let opt =
3453 match button with
3454 | 1 when x > conf.winw - conf.scrollbw ->
3455 G.postRedisplay "listview scroll";
3456 if down
3457 then
3458 let _, position, sh = self#scrollph in
3459 if y > truncate position && y < truncate (position +. sh)
3460 then (
3461 state.mstate <- Mscrolly;
3462 Some (coe self)
3464 else
3465 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3466 let first = truncate (s *. float source#getitemcount) in
3467 let first = min source#getitemcount first in
3468 Some (coe {< m_first = first; m_active = first >})
3469 else (
3470 state.mstate <- Mnone;
3471 Some (coe self);
3473 | 1 when not down ->
3474 begin match self#elemunder y with
3475 | Some n ->
3476 G.postRedisplay "listview click";
3477 source#exit
3478 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3479 | _ ->
3480 Some (coe self)
3482 | n when (n == 4 || n == 5) && not down ->
3483 let len = source#getitemcount in
3484 let first =
3485 if n = 5 && m_first + fstate.maxrows >= len
3486 then
3487 m_first
3488 else
3489 let first = m_first + (if n == 4 then -1 else 1) in
3490 bound first 0 (len - 1)
3492 G.postRedisplay "listview wheel";
3493 Some (coe {< m_first = first >})
3494 | n when (n = 6 || n = 7) && not down ->
3495 let inc = m_first + (if n = 7 then -1 else 1) in
3496 G.postRedisplay "listview hwheel";
3497 Some (coe {< m_pan = m_pan + inc >})
3498 | _ ->
3499 Some (coe self)
3501 match opt with
3502 | None -> m_prev_uioh
3503 | Some uioh -> uioh
3505 method motion _ y =
3506 match state.mstate with
3507 | Mscrolly ->
3508 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3509 let first = truncate (s *. float source#getitemcount) in
3510 let first = min source#getitemcount first in
3511 G.postRedisplay "listview motion";
3512 coe {< m_first = first; m_active = first >}
3513 | _ -> coe self
3515 method pmotion x y =
3516 if x < conf.winw - conf.scrollbw
3517 then
3518 let n =
3519 match self#elemunder y with
3520 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3521 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3523 let o =
3524 if n != m_active
3525 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3526 else self
3528 coe o
3529 else (
3530 Wsi.setcursor Wsi.CURSOR_INHERIT;
3531 coe self
3534 method infochanged _ = ()
3536 method scrollpw = (0, 0.0, 0.0)
3537 method scrollph =
3538 let nfs = fstate.fontsize + 1 in
3539 let y = m_first * nfs in
3540 let itemcount = source#getitemcount in
3541 let maxi = max 0 (itemcount - fstate.maxrows) in
3542 let maxy = maxi * nfs in
3543 let p, h = scrollph y maxy in
3544 conf.scrollbw, p, h
3546 method modehash = modehash
3547 end;;
3549 class outlinelistview ~source =
3550 object (self)
3551 inherit listview
3552 ~source:(source :> lvsource)
3553 ~trusted:false
3554 ~modehash:(findkeyhash conf "outline")
3555 as super
3557 method key key mask =
3558 let calcfirst first active =
3559 if active > first
3560 then
3561 let rows = active - first in
3562 let maxrows =
3563 if String.length state.text = 0
3564 then fstate.maxrows
3565 else fstate.maxrows - 2
3567 if rows > maxrows then active - maxrows else first
3568 else active
3570 let navigate incr =
3571 let active = m_active + incr in
3572 let active = bound active 0 (source#getitemcount - 1) in
3573 let first = calcfirst m_first active in
3574 G.postRedisplay "outline navigate";
3575 coe {< m_active = active; m_first = first >}
3577 let ctrl = Wsi.withctrl mask in
3578 match key with
3579 | 110 when ctrl -> (* ctrl-n *)
3580 source#narrow m_qsearch;
3581 G.postRedisplay "outline ctrl-n";
3582 coe {< m_first = 0; m_active = 0 >}
3584 | 117 when ctrl -> (* ctrl-u *)
3585 source#denarrow;
3586 G.postRedisplay "outline ctrl-u";
3587 state.text <- "";
3588 coe {< m_first = 0; m_active = 0 >}
3590 | 108 when ctrl -> (* ctrl-l *)
3591 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3592 G.postRedisplay "outline ctrl-l";
3593 coe {< m_first = first >}
3595 | 0xff9f | 0xffff -> (* delete *)
3596 source#remove m_active;
3597 G.postRedisplay "outline delete";
3598 let active = max 0 (m_active-1) in
3599 coe {< m_first = firstof m_first active;
3600 m_active = active >}
3602 | 0xff52 -> navigate ~-1 (* up *)
3603 | 0xff54 -> navigate 1 (* down *)
3604 | 0xff55 -> (* prior *)
3605 navigate ~-(fstate.maxrows)
3606 | 0xff56 -> (* next *)
3607 navigate fstate.maxrows
3609 | 0xff53 -> (* [ctrl-]right *)
3610 let o =
3611 if ctrl
3612 then (
3613 G.postRedisplay "outline ctrl right";
3614 {< m_pan = m_pan + 1 >}
3616 else self#updownlevel 1
3618 coe o
3620 | 0xff51 -> (* [ctrl-]left *)
3621 let o =
3622 if ctrl
3623 then (
3624 G.postRedisplay "outline ctrl left";
3625 {< m_pan = m_pan - 1 >}
3627 else self#updownlevel ~-1
3629 coe o
3631 | 0xff50 -> (* home *)
3632 G.postRedisplay "outline home";
3633 coe {< m_first = 0; m_active = 0 >}
3635 | 0xff57 -> (* end *)
3636 let active = source#getitemcount - 1 in
3637 let first = max 0 (active - fstate.maxrows) in
3638 G.postRedisplay "outline end";
3639 coe {< m_active = active; m_first = first >}
3641 | _ -> super#key key mask
3644 let outlinesource usebookmarks =
3645 let empty = [||] in
3646 (object
3647 inherit lvsourcebase
3648 val mutable m_items = empty
3649 val mutable m_orig_items = empty
3650 val mutable m_prev_items = empty
3651 val mutable m_narrow_pattern = ""
3652 val mutable m_hadremovals = false
3654 method getitemcount =
3655 Array.length m_items + (if m_hadremovals then 1 else 0)
3657 method getitem n =
3658 if n == Array.length m_items && m_hadremovals
3659 then
3660 ("[Confirm removal]", 0)
3661 else
3662 let s, n, _ = m_items.(n) in
3663 (s, n)
3665 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3666 ignore (uioh, first, qsearch);
3667 let confrimremoval = m_hadremovals && active = Array.length m_items in
3668 let items =
3669 if String.length m_narrow_pattern = 0
3670 then m_orig_items
3671 else m_items
3673 if not cancel
3674 then (
3675 if not confrimremoval
3676 then(
3677 let _, _, anchor = m_items.(active) in
3678 gotoghyll (getanchory anchor);
3679 m_items <- items;
3681 else (
3682 state.bookmarks <- Array.to_list m_items;
3683 m_orig_items <- m_items;
3686 else m_items <- items;
3687 m_pan <- pan;
3688 None
3690 method hasaction _ = true
3692 method greetmsg =
3693 if Array.length m_items != Array.length m_orig_items
3694 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3695 else ""
3697 method narrow pattern =
3698 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3699 match reopt with
3700 | None -> ()
3701 | Some re ->
3702 let rec loop accu n =
3703 if n = -1
3704 then (
3705 m_narrow_pattern <- pattern;
3706 m_items <- Array.of_list accu
3708 else
3709 let (s, _, _) as o = m_items.(n) in
3710 let accu =
3711 if (try ignore (Str.search_forward re s 0); true
3712 with Not_found -> false)
3713 then o :: accu
3714 else accu
3716 loop accu (n-1)
3718 loop [] (Array.length m_items - 1)
3720 method denarrow =
3721 m_orig_items <- (
3722 if usebookmarks
3723 then Array.of_list state.bookmarks
3724 else state.outlines
3726 m_items <- m_orig_items
3728 method remove m =
3729 if usebookmarks
3730 then
3731 if m >= 0 && m < Array.length m_items
3732 then (
3733 m_hadremovals <- true;
3734 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3735 let n = if n >= m then n+1 else n in
3736 m_items.(n)
3740 method reset anchor items =
3741 m_hadremovals <- false;
3742 if m_orig_items == empty || m_prev_items != items
3743 then (
3744 m_orig_items <- items;
3745 if String.length m_narrow_pattern = 0
3746 then m_items <- items;
3748 m_prev_items <- items;
3749 let rely = getanchory anchor in
3750 let active =
3751 let rec loop n best bestd =
3752 if n = Array.length m_items
3753 then best
3754 else
3755 let (_, _, anchor) = m_items.(n) in
3756 let orely = getanchory anchor in
3757 let d = abs (orely - rely) in
3758 if d < bestd
3759 then loop (n+1) n d
3760 else loop (n+1) best bestd
3762 loop 0 ~-1 max_int
3764 m_active <- active;
3765 m_first <- firstof m_first active
3766 end)
3769 let enterselector usebookmarks =
3770 let source = outlinesource usebookmarks in
3771 fun errmsg ->
3772 let outlines =
3773 if usebookmarks
3774 then Array.of_list state.bookmarks
3775 else state.outlines
3777 if Array.length outlines = 0
3778 then (
3779 showtext ' ' errmsg;
3781 else (
3782 state.text <- source#greetmsg;
3783 Wsi.setcursor Wsi.CURSOR_INHERIT;
3784 let anchor = getanchor () in
3785 source#reset anchor outlines;
3786 state.uioh <- coe (new outlinelistview ~source);
3787 G.postRedisplay "enter selector";
3791 let enteroutlinemode =
3792 let f = enterselector false in
3793 fun ()-> f "Document has no outline";
3796 let enterbookmarkmode =
3797 let f = enterselector true in
3798 fun () -> f "Document has no bookmarks (yet)";
3801 let color_of_string s =
3802 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3803 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3807 let color_to_string (r, g, b) =
3808 let r = truncate (r *. 256.0)
3809 and g = truncate (g *. 256.0)
3810 and b = truncate (b *. 256.0) in
3811 Printf.sprintf "%d/%d/%d" r g b
3814 let irect_of_string s =
3815 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3818 let irect_to_string (x0,y0,x1,y1) =
3819 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3822 let makecheckers () =
3823 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3824 following to say:
3825 converted by Issac Trotts. July 25, 2002 *)
3826 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3827 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3828 let id = GlTex.gen_texture () in
3829 GlTex.bind_texture `texture_2d id;
3830 GlPix.store (`unpack_alignment 1);
3831 GlTex.image2d image;
3832 List.iter (GlTex.parameter ~target:`texture_2d)
3833 [ `wrap_s `repeat;
3834 `wrap_t `repeat;
3835 `mag_filter `nearest;
3836 `min_filter `nearest ];
3840 let setcheckers enabled =
3841 match state.texid with
3842 | None ->
3843 if enabled then state.texid <- Some (makecheckers ())
3845 | Some texid ->
3846 if not enabled
3847 then (
3848 GlTex.delete_texture texid;
3849 state.texid <- None;
3853 let int_of_string_with_suffix s =
3854 let l = String.length s in
3855 let s1, shift =
3856 if l > 1
3857 then
3858 let suffix = Char.lowercase s.[l-1] in
3859 match suffix with
3860 | 'k' -> String.sub s 0 (l-1), 10
3861 | 'm' -> String.sub s 0 (l-1), 20
3862 | 'g' -> String.sub s 0 (l-1), 30
3863 | _ -> s, 0
3864 else s, 0
3866 let n = int_of_string s1 in
3867 let m = n lsl shift in
3868 if m < 0 || m < n
3869 then raise (Failure "value too large")
3870 else m
3873 let string_with_suffix_of_int n =
3874 if n = 0
3875 then "0"
3876 else
3877 let n, s =
3878 if n land ((1 lsl 20) - 1) = 0
3879 then n lsr 20, "M"
3880 else (
3881 if n land ((1 lsl 10) - 1) = 0
3882 then n lsr 10, "K"
3883 else n, ""
3886 let rec loop s n =
3887 let h = n mod 1000 in
3888 let n = n / 1000 in
3889 if n = 0
3890 then string_of_int h ^ s
3891 else (
3892 let s = Printf.sprintf "_%03d%s" h s in
3893 loop s n
3896 loop "" n ^ s;
3899 let defghyllscroll = (40, 8, 32);;
3900 let ghyllscroll_of_string s =
3901 let (n, a, b) as nab =
3902 if s = "default"
3903 then defghyllscroll
3904 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3906 if n <= a || n <= b || a >= b
3907 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3908 nab;
3911 let ghyllscroll_to_string ((n, a, b) as nab) =
3912 if nab = defghyllscroll
3913 then "default"
3914 else Printf.sprintf "%d,%d,%d" n a b;
3917 let describe_location () =
3918 let f (fn, _) l =
3919 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3921 let fn, ln = List.fold_left f (-1, -1) state.layout in
3922 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3923 let percent =
3924 if maxy <= 0
3925 then 100.
3926 else (100. *. (float state.y /. float maxy))
3928 if fn = ln
3929 then
3930 Printf.sprintf "page %d of %d [%.2f%%]"
3931 (fn+1) state.pagecount percent
3932 else
3933 Printf.sprintf
3934 "pages %d-%d of %d [%.2f%%]"
3935 (fn+1) (ln+1) state.pagecount percent
3938 let setpresentationmode v =
3939 let (n, _, _) = getanchor () in
3940 let _, h = getpageyh n in
3941 let ips = if conf.presentation then calcips h else conf.interpagespace in
3942 state.anchor <- (n, 0.0, float ips);
3943 conf.presentation <- v;
3944 if conf.presentation
3945 then (
3946 if not conf.scrollbarinpm
3947 then state.scrollw <- 0;
3949 else state.scrollw <- conf.scrollbw;
3950 represent ();
3953 let enterinfomode =
3954 let btos b = if b then "\xe2\x88\x9a" else "" in
3955 let showextended = ref false in
3956 let leave mode = function
3957 | Confirm -> state.mode <- mode
3958 | Cancel -> state.mode <- mode in
3959 let src =
3960 (object
3961 val mutable m_first_time = true
3962 val mutable m_l = []
3963 val mutable m_a = [||]
3964 val mutable m_prev_uioh = nouioh
3965 val mutable m_prev_mode = View
3967 inherit lvsourcebase
3969 method reset prev_mode prev_uioh =
3970 m_a <- Array.of_list (List.rev m_l);
3971 m_l <- [];
3972 m_prev_mode <- prev_mode;
3973 m_prev_uioh <- prev_uioh;
3974 if m_first_time
3975 then (
3976 let rec loop n =
3977 if n >= Array.length m_a
3978 then ()
3979 else
3980 match m_a.(n) with
3981 | _, _, _, Action _ -> m_active <- n
3982 | _ -> loop (n+1)
3984 loop 0;
3985 m_first_time <- false;
3988 method int name get set =
3989 m_l <-
3990 (name, `int get, 1, Action (
3991 fun u ->
3992 let ondone s =
3993 try set (int_of_string s)
3994 with exn ->
3995 state.text <- Printf.sprintf "bad integer `%s': %s"
3996 s (Printexc.to_string exn)
3998 state.text <- "";
3999 let te = name ^ ": ", "", None, intentry, ondone, true in
4000 state.mode <- Textentry (te, leave m_prev_mode);
4002 )) :: m_l
4004 method int_with_suffix name get set =
4005 m_l <-
4006 (name, `intws get, 1, Action (
4007 fun u ->
4008 let ondone s =
4009 try set (int_of_string_with_suffix s)
4010 with exn ->
4011 state.text <- Printf.sprintf "bad integer `%s': %s"
4012 s (Printexc.to_string exn)
4014 state.text <- "";
4015 let te =
4016 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4018 state.mode <- Textentry (te, leave m_prev_mode);
4020 )) :: m_l
4022 method bool ?(offset=1) ?(btos=btos) name get set =
4023 m_l <-
4024 (name, `bool (btos, get), offset, Action (
4025 fun u ->
4026 let v = get () in
4027 set (not v);
4029 )) :: m_l
4031 method color name get set =
4032 m_l <-
4033 (name, `color get, 1, Action (
4034 fun u ->
4035 let invalid = (nan, nan, nan) in
4036 let ondone s =
4037 let c =
4038 try color_of_string s
4039 with exn ->
4040 state.text <- Printf.sprintf "bad color `%s': %s"
4041 s (Printexc.to_string exn);
4042 invalid
4044 if c <> invalid
4045 then set c;
4047 let te = name ^ ": ", "", None, textentry, ondone, true in
4048 state.text <- color_to_string (get ());
4049 state.mode <- Textentry (te, leave m_prev_mode);
4051 )) :: m_l
4053 method string name get set =
4054 m_l <-
4055 (name, `string get, 1, Action (
4056 fun u ->
4057 let ondone s = set s in
4058 let te = name ^ ": ", "", None, textentry, ondone, true in
4059 state.mode <- Textentry (te, leave m_prev_mode);
4061 )) :: m_l
4063 method colorspace name get set =
4064 m_l <-
4065 (name, `string get, 1, Action (
4066 fun _ ->
4067 let source =
4068 let vals = [| "rgb"; "bgr"; "gray" |] in
4069 (object
4070 inherit lvsourcebase
4072 initializer
4073 m_active <- int_of_colorspace conf.colorspace;
4074 m_first <- 0;
4076 method getitemcount = Array.length vals
4077 method getitem n = (vals.(n), 0)
4078 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4079 ignore (uioh, first, pan, qsearch);
4080 if not cancel then set active;
4081 None
4082 method hasaction _ = true
4083 end)
4085 state.text <- "";
4086 let modehash = findkeyhash conf "info" in
4087 coe (new listview ~source ~trusted:true ~modehash)
4088 )) :: m_l
4090 method caption s offset =
4091 m_l <- (s, `empty, offset, Noaction) :: m_l
4093 method caption2 s f offset =
4094 m_l <- (s, `string f, offset, Noaction) :: m_l
4096 method getitemcount = Array.length m_a
4098 method getitem n =
4099 let tostr = function
4100 | `int f -> string_of_int (f ())
4101 | `intws f -> string_with_suffix_of_int (f ())
4102 | `string f -> f ()
4103 | `color f -> color_to_string (f ())
4104 | `bool (btos, f) -> btos (f ())
4105 | `empty -> ""
4107 let name, t, offset, _ = m_a.(n) in
4108 ((let s = tostr t in
4109 if String.length s > 0
4110 then Printf.sprintf "%s\t%s" name s
4111 else name),
4112 offset)
4114 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4115 let uiohopt =
4116 if not cancel
4117 then (
4118 m_qsearch <- qsearch;
4119 let uioh =
4120 match m_a.(active) with
4121 | _, _, _, Action f -> f uioh
4122 | _ -> uioh
4124 Some uioh
4126 else None
4128 m_active <- active;
4129 m_first <- first;
4130 m_pan <- pan;
4131 uiohopt
4133 method hasaction n =
4134 match m_a.(n) with
4135 | _, _, _, Action _ -> true
4136 | _ -> false
4137 end)
4139 let rec fillsrc prevmode prevuioh =
4140 let sep () = src#caption "" 0 in
4141 let colorp name get set =
4142 src#string name
4143 (fun () -> color_to_string (get ()))
4144 (fun v ->
4146 let c = color_of_string v in
4147 set c
4148 with exn ->
4149 state.text <- Printf.sprintf "bad color `%s': %s"
4150 v (Printexc.to_string exn);
4153 let oldmode = state.mode in
4154 let birdseye = isbirdseye state.mode in
4156 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4158 src#bool "presentation mode"
4159 (fun () -> conf.presentation)
4160 (fun v -> setpresentationmode v);
4162 src#bool "ignore case in searches"
4163 (fun () -> conf.icase)
4164 (fun v -> conf.icase <- v);
4166 src#bool "preload"
4167 (fun () -> conf.preload)
4168 (fun v -> conf.preload <- v);
4170 src#bool "highlight links"
4171 (fun () -> conf.hlinks)
4172 (fun v -> conf.hlinks <- v);
4174 src#bool "under info"
4175 (fun () -> conf.underinfo)
4176 (fun v -> conf.underinfo <- v);
4178 src#bool "persistent bookmarks"
4179 (fun () -> conf.savebmarks)
4180 (fun v -> conf.savebmarks <- v);
4182 src#bool "proportional display"
4183 (fun () -> conf.proportional)
4184 (fun v -> reqlayout conf.angle v);
4186 src#bool "trim margins"
4187 (fun () -> conf.trimmargins)
4188 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4190 src#bool "persistent location"
4191 (fun () -> conf.jumpback)
4192 (fun v -> conf.jumpback <- v);
4194 sep ();
4195 src#int "inter-page space"
4196 (fun () -> conf.interpagespace)
4197 (fun n ->
4198 conf.interpagespace <- n;
4199 docolumns conf.columns;
4200 let pageno, py =
4201 match state.layout with
4202 | [] -> 0, 0
4203 | l :: _ ->
4204 l.pageno, l.pagey
4206 state.maxy <- calcheight ();
4207 let y = getpagey pageno in
4208 gotoy (y + py)
4211 src#int "page bias"
4212 (fun () -> conf.pagebias)
4213 (fun v -> conf.pagebias <- v);
4215 src#int "scroll step"
4216 (fun () -> conf.scrollstep)
4217 (fun n -> conf.scrollstep <- n);
4219 src#int "horizontal scroll step"
4220 (fun () -> conf.hscrollstep)
4221 (fun v -> conf.hscrollstep <- v);
4223 src#int "auto scroll step"
4224 (fun () ->
4225 match state.autoscroll with
4226 | Some step -> step
4227 | _ -> conf.autoscrollstep)
4228 (fun n ->
4229 if state.autoscroll <> None
4230 then state.autoscroll <- Some n;
4231 conf.autoscrollstep <- n);
4233 src#int "zoom"
4234 (fun () -> truncate (conf.zoom *. 100.))
4235 (fun v -> setzoom ((float v) /. 100.));
4237 src#int "rotation"
4238 (fun () -> conf.angle)
4239 (fun v -> reqlayout v conf.proportional);
4241 src#int "scroll bar width"
4242 (fun () -> state.scrollw)
4243 (fun v ->
4244 state.scrollw <- v;
4245 conf.scrollbw <- v;
4246 reshape conf.winw conf.winh;
4249 src#int "scroll handle height"
4250 (fun () -> conf.scrollh)
4251 (fun v -> conf.scrollh <- v;);
4253 src#int "thumbnail width"
4254 (fun () -> conf.thumbw)
4255 (fun v ->
4256 conf.thumbw <- min 4096 v;
4257 match oldmode with
4258 | Birdseye beye ->
4259 leavebirdseye beye false;
4260 enterbirdseye ()
4261 | _ -> ()
4264 let mode = state.mode in
4265 src#string "columns"
4266 (fun () ->
4267 match conf.columns with
4268 | Csingle _ -> "1"
4269 | Cmulti (multi, _) -> multicolumns_to_string multi
4270 | Csplit (count, _) -> "-" ^ string_of_int count
4272 (fun v ->
4273 let n, a, b = multicolumns_of_string v in
4274 setcolumns mode n a b);
4276 sep ();
4277 src#caption "Presentation mode" 0;
4278 src#bool "scrollbar visible"
4279 (fun () -> conf.scrollbarinpm)
4280 (fun v ->
4281 if v != conf.scrollbarinpm
4282 then (
4283 conf.scrollbarinpm <- v;
4284 if conf.presentation
4285 then (
4286 state.scrollw <- if v then conf.scrollbw else 0;
4287 reshape conf.winw conf.winh;
4292 sep ();
4293 src#caption "Pixmap cache" 0;
4294 src#int_with_suffix "size (advisory)"
4295 (fun () -> conf.memlimit)
4296 (fun v -> conf.memlimit <- v);
4298 src#caption2 "used"
4299 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4300 (string_with_suffix_of_int state.memused)
4301 (Hashtbl.length state.tilemap)) 1;
4303 sep ();
4304 src#caption "Layout" 0;
4305 src#caption2 "Dimension"
4306 (fun () ->
4307 Printf.sprintf "%dx%d (virtual %dx%d)"
4308 conf.winw conf.winh
4309 state.w state.maxy)
4311 if conf.debug
4312 then
4313 src#caption2 "Position" (fun () ->
4314 Printf.sprintf "%dx%d" state.x state.y
4316 else
4317 src#caption2 "Visible" (fun () -> describe_location ()) 1
4320 sep ();
4321 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4322 "Save these parameters as global defaults at exit"
4323 (fun () -> conf.bedefault)
4324 (fun v -> conf.bedefault <- v)
4327 sep ();
4328 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4329 src#bool ~offset:0 ~btos "Extended parameters"
4330 (fun () -> !showextended)
4331 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4332 if !showextended
4333 then (
4334 src#bool "checkers"
4335 (fun () -> conf.checkers)
4336 (fun v -> conf.checkers <- v; setcheckers v);
4337 src#bool "update cursor"
4338 (fun () -> conf.updatecurs)
4339 (fun v -> conf.updatecurs <- v);
4340 src#bool "verbose"
4341 (fun () -> conf.verbose)
4342 (fun v -> conf.verbose <- v);
4343 src#bool "invert colors"
4344 (fun () -> conf.invert)
4345 (fun v -> conf.invert <- v);
4346 src#bool "max fit"
4347 (fun () -> conf.maxhfit)
4348 (fun v -> conf.maxhfit <- v);
4349 src#bool "redirect stderr"
4350 (fun () -> conf.redirectstderr)
4351 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4352 src#string "uri launcher"
4353 (fun () -> conf.urilauncher)
4354 (fun v -> conf.urilauncher <- v);
4355 src#string "path launcher"
4356 (fun () -> conf.pathlauncher)
4357 (fun v -> conf.pathlauncher <- v);
4358 src#string "tile size"
4359 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4360 (fun v ->
4362 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4363 conf.tilew <- max 64 w;
4364 conf.tileh <- max 64 h;
4365 flushtiles ();
4366 with exn ->
4367 state.text <- Printf.sprintf "bad tile size `%s': %s"
4368 v (Printexc.to_string exn));
4369 src#int "texture count"
4370 (fun () -> conf.texcount)
4371 (fun v ->
4372 if realloctexts v
4373 then conf.texcount <- v
4374 else showtext '!' " Failed to set texture count please retry later"
4376 src#int "slice height"
4377 (fun () -> conf.sliceheight)
4378 (fun v ->
4379 conf.sliceheight <- v;
4380 wcmd "sliceh %d" conf.sliceheight;
4382 src#int "anti-aliasing level"
4383 (fun () -> conf.aalevel)
4384 (fun v ->
4385 conf.aalevel <- bound v 0 8;
4386 state.anchor <- getanchor ();
4387 opendoc state.path state.password;
4389 src#string "page scroll scaling factor"
4390 (fun () -> string_of_float conf.pgscale)
4391 (fun v ->
4393 let s = float_of_string v in
4394 conf.pgscale <- s
4395 with exn ->
4396 state.text <- Printf.sprintf
4397 "bad page scroll scaling factor `%s': %s"
4398 v (Printexc.to_string exn)
4401 src#int "ui font size"
4402 (fun () -> fstate.fontsize)
4403 (fun v -> setfontsize (bound v 5 100));
4404 src#int "hint font size"
4405 (fun () -> conf.hfsize)
4406 (fun v -> conf.hfsize <- bound v 5 100);
4407 colorp "background color"
4408 (fun () -> conf.bgcolor)
4409 (fun v -> conf.bgcolor <- v);
4410 src#bool "crop hack"
4411 (fun () -> conf.crophack)
4412 (fun v -> conf.crophack <- v);
4413 src#string "trim fuzz"
4414 (fun () -> irect_to_string conf.trimfuzz)
4415 (fun v ->
4417 conf.trimfuzz <- irect_of_string v;
4418 if conf.trimmargins
4419 then settrim true conf.trimfuzz;
4420 with exn ->
4421 state.text <- Printf.sprintf "bad irect `%s': %s"
4422 v (Printexc.to_string exn)
4424 src#string "throttle"
4425 (fun () ->
4426 match conf.maxwait with
4427 | None -> "show place holder if page is not ready"
4428 | Some time ->
4429 if time = infinity
4430 then "wait for page to fully render"
4431 else
4432 "wait " ^ string_of_float time
4433 ^ " seconds before showing placeholder"
4435 (fun v ->
4437 let f = float_of_string v in
4438 if f <= 0.0
4439 then conf.maxwait <- None
4440 else conf.maxwait <- Some f
4441 with exn ->
4442 state.text <- Printf.sprintf "bad time `%s': %s"
4443 v (Printexc.to_string exn)
4445 src#string "ghyll scroll"
4446 (fun () ->
4447 match conf.ghyllscroll with
4448 | None -> ""
4449 | Some nab -> ghyllscroll_to_string nab
4451 (fun v ->
4453 let gs =
4454 if String.length v = 0
4455 then None
4456 else Some (ghyllscroll_of_string v)
4458 conf.ghyllscroll <- gs
4459 with exn ->
4460 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4461 v (Printexc.to_string exn)
4463 src#string "selection command"
4464 (fun () -> conf.selcmd)
4465 (fun v -> conf.selcmd <- v);
4466 src#colorspace "color space"
4467 (fun () -> colorspace_to_string conf.colorspace)
4468 (fun v ->
4469 conf.colorspace <- colorspace_of_int v;
4470 wcmd "cs %d" v;
4471 load state.layout;
4475 sep ();
4476 src#caption "Document" 0;
4477 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4478 src#caption2 "Pages"
4479 (fun () -> string_of_int state.pagecount) 1;
4480 src#caption2 "Dimensions"
4481 (fun () -> string_of_int (List.length state.pdims)) 1;
4482 if conf.trimmargins
4483 then (
4484 sep ();
4485 src#caption "Trimmed margins" 0;
4486 src#caption2 "Dimensions"
4487 (fun () -> string_of_int (List.length state.pdims)) 1;
4490 sep ();
4491 src#caption "OpenGL" 0;
4492 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4493 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4494 src#reset prevmode prevuioh;
4496 fun () ->
4497 state.text <- "";
4498 let prevmode = state.mode
4499 and prevuioh = state.uioh in
4500 fillsrc prevmode prevuioh;
4501 let source = (src :> lvsource) in
4502 let modehash = findkeyhash conf "info" in
4503 state.uioh <- coe (object (self)
4504 inherit listview ~source ~trusted:true ~modehash as super
4505 val mutable m_prevmemused = 0
4506 method infochanged = function
4507 | Memused ->
4508 if m_prevmemused != state.memused
4509 then (
4510 m_prevmemused <- state.memused;
4511 G.postRedisplay "memusedchanged";
4513 | Pdim -> G.postRedisplay "pdimchanged"
4514 | Docinfo -> fillsrc prevmode prevuioh
4516 method key key mask =
4517 if not (Wsi.withctrl mask)
4518 then
4519 match key with
4520 | 0xff51 -> coe (self#updownlevel ~-1)
4521 | 0xff53 -> coe (self#updownlevel 1)
4522 | _ -> super#key key mask
4523 else super#key key mask
4524 end);
4525 G.postRedisplay "info";
4528 let enterhelpmode =
4529 let source =
4530 (object
4531 inherit lvsourcebase
4532 method getitemcount = Array.length state.help
4533 method getitem n =
4534 let s, l, _ = state.help.(n) in
4535 (s, l)
4537 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4538 let optuioh =
4539 if not cancel
4540 then (
4541 m_qsearch <- qsearch;
4542 match state.help.(active) with
4543 | _, _, Action f -> Some (f uioh)
4544 | _ -> Some (uioh)
4546 else None
4548 m_active <- active;
4549 m_first <- first;
4550 m_pan <- pan;
4551 optuioh
4553 method hasaction n =
4554 match state.help.(n) with
4555 | _, _, Action _ -> true
4556 | _ -> false
4558 initializer
4559 m_active <- -1
4560 end)
4561 in fun () ->
4562 let modehash = findkeyhash conf "help" in
4563 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4564 G.postRedisplay "help";
4567 let entermsgsmode =
4568 let msgsource =
4569 let re = Str.regexp "[\r\n]" in
4570 (object
4571 inherit lvsourcebase
4572 val mutable m_items = [||]
4574 method getitemcount = 1 + Array.length m_items
4576 method getitem n =
4577 if n = 0
4578 then "[Clear]", 0
4579 else m_items.(n-1), 0
4581 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4582 ignore uioh;
4583 if not cancel
4584 then (
4585 if active = 0
4586 then Buffer.clear state.errmsgs;
4587 m_qsearch <- qsearch;
4589 m_active <- active;
4590 m_first <- first;
4591 m_pan <- pan;
4592 None
4594 method hasaction n =
4595 n = 0
4597 method reset =
4598 state.newerrmsgs <- false;
4599 let l = Str.split re (Buffer.contents state.errmsgs) in
4600 m_items <- Array.of_list l
4602 initializer
4603 m_active <- 0
4604 end)
4605 in fun () ->
4606 state.text <- "";
4607 msgsource#reset;
4608 let source = (msgsource :> lvsource) in
4609 let modehash = findkeyhash conf "listview" in
4610 state.uioh <- coe (object
4611 inherit listview ~source ~trusted:false ~modehash as super
4612 method display =
4613 if state.newerrmsgs
4614 then msgsource#reset;
4615 super#display
4616 end);
4617 G.postRedisplay "msgs";
4620 let quickbookmark ?title () =
4621 match state.layout with
4622 | [] -> ()
4623 | l :: _ ->
4624 let title =
4625 match title with
4626 | None ->
4627 let sec = Unix.gettimeofday () in
4628 let tm = Unix.localtime sec in
4629 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4630 (l.pageno+1)
4631 tm.Unix.tm_mday
4632 tm.Unix.tm_mon
4633 (tm.Unix.tm_year + 1900)
4634 tm.Unix.tm_hour
4635 tm.Unix.tm_min
4636 | Some title -> title
4638 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4641 let doreshape w h =
4642 state.fullscreen <- None;
4643 Wsi.reshape w h;
4646 let setautoscrollspeed step goingdown =
4647 let incr = max 1 ((abs step) / 2) in
4648 let incr = if goingdown then incr else -incr in
4649 let astep = step + incr in
4650 state.autoscroll <- Some astep;
4653 let gotounder = function
4654 | Ulinkgoto (pageno, top) ->
4655 if pageno >= 0
4656 then (
4657 addnav ();
4658 gotopage1 pageno top;
4661 | Ulinkuri s ->
4662 gotouri s
4664 | Uremote (filename, pageno) ->
4665 let path =
4666 if Sys.file_exists filename
4667 then filename
4668 else
4669 let dir = Filename.dirname state.path in
4670 let path = Filename.concat dir filename in
4671 if Sys.file_exists path
4672 then path
4673 else ""
4675 if String.length path > 0
4676 then (
4677 let anchor = getanchor () in
4678 let ranchor = state.path, state.password, anchor in
4679 state.anchor <- (pageno, 0.0, 0.0);
4680 state.ranchors <- ranchor :: state.ranchors;
4681 opendoc path "";
4683 else showtext '!' ("Could not find " ^ filename)
4685 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4688 let canpan () =
4689 match conf.columns with
4690 | Csplit _ -> true
4691 | _ -> conf.zoom > 1.0
4694 let viewkeyboard key mask =
4695 let enttext te =
4696 let mode = state.mode in
4697 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4698 state.text <- "";
4699 enttext ();
4700 G.postRedisplay "view:enttext"
4702 let ctrl = Wsi.withctrl mask in
4703 let existsinrow pageno (columns, coverA, coverB) p =
4704 let last = ((pageno - coverA) mod columns) + columns in
4705 let rec any = function
4706 | [] -> false
4707 | l :: rest ->
4708 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4709 then p l
4710 else (
4711 if not (p l)
4712 then (if l.pageno = last then false else any rest)
4713 else true
4716 any state.layout
4718 match key with
4719 | 81 -> (* Q *)
4720 exit 0
4722 | 0xff63 -> (* insert *)
4723 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4724 then (
4725 state.mode <- LinkNav (Ltgendir 0);
4726 gotoy state.y;
4728 else showtext '!' "Keyboard link navigation does not work under rotation"
4730 | 0xff1b | 113 -> (* escape / q *)
4731 begin match state.mstate with
4732 | Mzoomrect _ ->
4733 state.mstate <- Mnone;
4734 Wsi.setcursor Wsi.CURSOR_INHERIT;
4735 G.postRedisplay "kill zoom rect";
4736 | _ ->
4737 match state.ranchors with
4738 | [] -> raise Quit
4739 | (path, password, anchor) :: rest ->
4740 state.ranchors <- rest;
4741 state.anchor <- anchor;
4742 opendoc path password
4743 end;
4745 | 0xff08 -> (* backspace *)
4746 gotoghyll (getnav ~-1)
4748 | 111 -> (* o *)
4749 enteroutlinemode ()
4751 | 117 -> (* u *)
4752 state.rects <- [];
4753 state.text <- "";
4754 G.postRedisplay "dehighlight";
4756 | 47 | 63 -> (* / ? *)
4757 let ondone isforw s =
4758 cbput state.hists.pat s;
4759 state.searchpattern <- s;
4760 search s isforw
4762 let s = String.create 1 in
4763 s.[0] <- Char.chr key;
4764 enttext (s, "", Some (onhist state.hists.pat),
4765 textentry, ondone (key = 47), true)
4767 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4768 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4769 setzoom (conf.zoom +. incr)
4771 | 43 | 0xffab -> (* + *)
4772 let ondone s =
4773 let n =
4774 try int_of_string s with exc ->
4775 state.text <- Printf.sprintf "bad integer `%s': %s"
4776 s (Printexc.to_string exc);
4777 max_int
4779 if n != max_int
4780 then (
4781 conf.pagebias <- n;
4782 state.text <- "page bias is now " ^ string_of_int n;
4785 enttext ("page bias: ", "", None, intentry, ondone, true)
4787 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4788 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4789 setzoom (max 0.01 (conf.zoom -. decr))
4791 | 45 | 0xffad -> (* - *)
4792 let ondone msg = state.text <- msg in
4793 enttext (
4794 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4795 optentry state.mode, ondone, true
4798 | 48 when ctrl -> (* ctrl-0 *)
4799 setzoom 1.0
4801 | 49 when ctrl -> (* ctrl-1 *)
4802 let cols =
4803 match conf.columns with
4804 | Csingle _ | Cmulti _ -> 1
4805 | Csplit (n, _) -> n
4807 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4808 if zoom < 1.0
4809 then setzoom zoom
4811 | 0xffc6 -> (* f9 *)
4812 togglebirdseye ()
4814 | 57 when ctrl -> (* ctrl-9 *)
4815 togglebirdseye ()
4817 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4818 when not ctrl -> (* 0..9 *)
4819 let ondone s =
4820 let n =
4821 try int_of_string s with exc ->
4822 state.text <- Printf.sprintf "bad integer `%s': %s"
4823 s (Printexc.to_string exc);
4826 if n >= 0
4827 then (
4828 addnav ();
4829 cbput state.hists.pag (string_of_int n);
4830 gotopage1 (n + conf.pagebias - 1) 0;
4833 let pageentry text key =
4834 match Char.unsafe_chr key with
4835 | 'g' -> TEdone text
4836 | _ -> intentry text key
4838 let text = "x" in text.[0] <- Char.chr key;
4839 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4841 | 98 -> (* b *)
4842 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4843 reshape conf.winw conf.winh;
4845 | 108 -> (* l *)
4846 conf.hlinks <- not conf.hlinks;
4847 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4848 G.postRedisplay "toggle highlightlinks";
4850 | 70 -> (* F *)
4851 state.glinks <- true;
4852 let mode = state.mode in
4853 state.mode <- Textentry (
4854 (":", "", None, linknentry, linkndone gotounder, false),
4855 (fun _ ->
4856 state.glinks <- false;
4857 state.mode <- mode)
4859 state.text <- "";
4860 G.postRedisplay "view:linkent(F)"
4862 | 121 -> (* y *)
4863 state.glinks <- true;
4864 let mode = state.mode in
4865 state.mode <- Textentry (
4866 (":", "", None, linknentry, linkndone (fun under ->
4867 match Ne.pipe () with
4868 | Ne.Exn exn ->
4869 showtext '!' (Printf.sprintf "pipe failed: %s"
4870 (Printexc.to_string exn));
4871 | Ne.Res (r, w) ->
4872 let popened =
4873 try popen conf.selcmd [r, 0; w, -1]; true
4874 with exn ->
4875 showtext '!'
4876 (Printf.sprintf "failed to execute %s: %s"
4877 conf.selcmd (Printexc.to_string exn));
4878 false
4880 let clo cap fd =
4881 Ne.clo fd (fun msg ->
4882 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4885 let s = undertext under in
4886 if popened
4887 then
4888 (try
4889 let l = String.length s in
4890 let n = Unix.write w s 0 l in
4891 if n != l
4892 then
4893 showtext '!'
4894 (Printf.sprintf
4895 "failed to write %d characters to sel pipe, wrote %d"
4898 with exn ->
4899 showtext '!'
4900 (Printf.sprintf "failed to write to sel pipe: %s"
4901 (Printexc.to_string exn)
4904 else dolog "%s" s;
4905 clo "pipe/r" r;
4906 clo "pipe/w" w;
4907 ), false
4909 fun _ ->
4910 state.glinks <- false;
4911 state.mode <- mode
4913 state.text <- "";
4914 G.postRedisplay "view:linkent"
4916 | 97 -> (* a *)
4917 begin match state.autoscroll with
4918 | Some step ->
4919 conf.autoscrollstep <- step;
4920 state.autoscroll <- None
4921 | None ->
4922 if conf.autoscrollstep = 0
4923 then state.autoscroll <- Some 1
4924 else state.autoscroll <- Some conf.autoscrollstep
4927 | 112 when ctrl -> (* ctrl-p *)
4928 launchpath ()
4930 | 80 -> (* P *)
4931 setpresentationmode (not conf.presentation);
4932 showtext ' ' ("presentation mode " ^
4933 if conf.presentation then "on" else "off");
4935 | 102 -> (* f *)
4936 begin match state.fullscreen with
4937 | None ->
4938 state.fullscreen <- Some (conf.winw, conf.winh);
4939 Wsi.fullscreen ()
4940 | Some (w, h) ->
4941 state.fullscreen <- None;
4942 doreshape w h
4945 | 112 | 78 -> (* p|N *)
4946 search state.searchpattern false
4948 | 110 | 0xffc0 -> (* n|F3 *)
4949 search state.searchpattern true
4951 | 116 -> (* t *)
4952 begin match state.layout with
4953 | [] -> ()
4954 | l :: _ ->
4955 gotoy_and_clear_text (getpagey l.pageno)
4958 | 32 -> (* space *)
4959 begin match state.layout with
4960 | [] -> ()
4961 | l :: rest ->
4962 match conf.columns with
4963 | Csingle _ ->
4964 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4965 then
4966 let y = clamp (pgscale conf.winh) in
4967 gotoghyll y
4968 else
4969 let pageno = min (l.pageno+1) (state.pagecount-1) in
4970 gotoghyll (getpagey pageno)
4971 | Cmulti ((c, _, _) as cl, _) ->
4972 if conf.presentation
4973 && (existsinrow l.pageno cl
4974 (fun l -> l.pageh > l.pagey + l.pagevh))
4975 then
4976 let y = clamp (pgscale conf.winh) in
4977 gotoghyll y
4978 else
4979 let pageno = min (l.pageno+c) (state.pagecount-1) in
4980 gotoghyll (getpagey pageno)
4981 | Csplit (n, _) ->
4982 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4983 then
4984 let pagey, pageh = getpageyh l.pageno in
4985 let pagey = pagey + pageh * l.pagecol in
4986 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4987 gotoghyll (pagey + pageh + ips)
4990 | 0xff9f | 0xffff -> (* delete *)
4991 begin match state.layout with
4992 | [] -> ()
4993 | l :: _ ->
4994 match conf.columns with
4995 | Csingle _ ->
4996 if conf.presentation && l.pagey != 0
4997 then
4998 gotoghyll (clamp (pgscale ~-(conf.winh)))
4999 else
5000 let pageno = max 0 (l.pageno-1) in
5001 gotoghyll (getpagey pageno)
5002 | Cmulti ((c, _, coverB) as cl, _) ->
5003 if conf.presentation &&
5004 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5005 then
5006 gotoghyll (clamp (pgscale ~-(conf.winh)))
5007 else
5008 let decr =
5009 if l.pageno = state.pagecount - coverB
5010 then 1
5011 else c
5013 let pageno = max 0 (l.pageno-decr) in
5014 gotoghyll (getpagey pageno)
5015 | Csplit (n, _) ->
5016 let y =
5017 if l.pagecol = 0
5018 then
5019 if l.pageno = 0
5020 then l.pagey
5021 else
5022 let pageno = max 0 (l.pageno-1) in
5023 let pagey, pageh = getpageyh pageno in
5024 pagey + (n-1)*pageh
5025 else
5026 let pagey, pageh = getpageyh l.pageno in
5027 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5029 gotoghyll y
5032 | 61 -> (* = *)
5033 showtext ' ' (describe_location ());
5035 | 119 -> (* w *)
5036 begin match state.layout with
5037 | [] -> ()
5038 | l :: _ ->
5039 doreshape (l.pagew + state.scrollw) l.pageh;
5040 G.postRedisplay "w"
5043 | 39 -> (* ' *)
5044 enterbookmarkmode ()
5046 | 104 | 0xffbe -> (* h|F1 *)
5047 enterhelpmode ()
5049 | 105 -> (* i *)
5050 enterinfomode ()
5052 | 101 when conf.redirectstderr -> (* e *)
5053 entermsgsmode ()
5055 | 109 -> (* m *)
5056 let ondone s =
5057 match state.layout with
5058 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5059 | _ -> ()
5061 enttext ("bookmark: ", "", None, textentry, ondone, true)
5063 | 126 -> (* ~ *)
5064 quickbookmark ();
5065 showtext ' ' "Quick bookmark added";
5067 | 122 -> (* z *)
5068 begin match state.layout with
5069 | l :: _ ->
5070 let rect = getpdimrect l.pagedimno in
5071 let w, h =
5072 if conf.crophack
5073 then
5074 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5075 truncate (1.2 *. (rect.(3) -. rect.(0))))
5076 else
5077 (truncate (rect.(1) -. rect.(0)),
5078 truncate (rect.(3) -. rect.(0)))
5080 let w = truncate ((float w)*.conf.zoom)
5081 and h = truncate ((float h)*.conf.zoom) in
5082 if w != 0 && h != 0
5083 then (
5084 state.anchor <- getanchor ();
5085 doreshape (w + state.scrollw) (h + conf.interpagespace)
5087 G.postRedisplay "z";
5089 | [] -> ()
5092 | 50 when ctrl -> (* ctrl-2 *)
5093 let maxw = getmaxw () in
5094 if maxw > 0.0
5095 then setzoom (maxw /. float conf.winw)
5097 | 60 | 62 -> (* < > *)
5098 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5100 | 91 | 93 -> (* [ ] *)
5101 conf.colorscale <-
5102 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5104 G.postRedisplay "brightness";
5106 | 99 when state.mode = View -> (* c *)
5107 let (c, a, b), z =
5108 match state.prevcolumns with
5109 | None -> (1, 0, 0), 1.0
5110 | Some (columns, z) ->
5111 let cab =
5112 match columns with
5113 | Csplit (c, _) -> -c, 0, 0
5114 | Cmulti ((c, a, b), _) -> c, a, b
5115 | Csingle _ -> 1, 0, 0
5117 cab, z
5119 setcolumns View c a b;
5120 setzoom z;
5122 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5123 setzoom state.prevzoom
5125 | 107 | 0xff52 -> (* k up *)
5126 begin match state.autoscroll with
5127 | None ->
5128 begin match state.mode with
5129 | Birdseye beye -> upbirdseye 1 beye
5130 | _ ->
5131 if ctrl
5132 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5133 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5135 | Some n ->
5136 setautoscrollspeed n false
5139 | 106 | 0xff54 -> (* j down *)
5140 begin match state.autoscroll with
5141 | None ->
5142 begin match state.mode with
5143 | Birdseye beye -> downbirdseye 1 beye
5144 | _ ->
5145 if ctrl
5146 then gotoy_and_clear_text (clamp (conf.winh/2))
5147 else gotoy_and_clear_text (clamp conf.scrollstep)
5149 | Some n ->
5150 setautoscrollspeed n true
5153 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5154 if canpan ()
5155 then
5156 let dx =
5157 if ctrl
5158 then conf.winw / 2
5159 else conf.hscrollstep
5161 let dx = if key = 0xff51 then dx else -dx in
5162 state.x <- state.x + dx;
5163 gotoy_and_clear_text state.y
5164 else (
5165 state.text <- "";
5166 G.postRedisplay "lef/right"
5169 | 0xff55 -> (* prior *)
5170 let y =
5171 if ctrl
5172 then
5173 match state.layout with
5174 | [] -> state.y
5175 | l :: _ -> state.y - l.pagey
5176 else
5177 clamp (pgscale (-conf.winh))
5179 gotoghyll y
5181 | 0xff56 -> (* next *)
5182 let y =
5183 if ctrl
5184 then
5185 match List.rev state.layout with
5186 | [] -> state.y
5187 | l :: _ -> getpagey l.pageno
5188 else
5189 clamp (pgscale conf.winh)
5191 gotoghyll y
5193 | 103 | 0xff50 -> (* g home *)
5194 gotoghyll 0
5195 | 71 | 0xff57 -> (* G end *)
5196 gotoghyll (clamp state.maxy)
5198 | 0xff53 when Wsi.withalt mask -> (* alt-right *)
5199 gotoghyll (getnav 1)
5200 | 0xff51 when Wsi.withalt mask -> (* alt-left *)
5201 gotoghyll (getnav ~-1)
5203 | 114 -> (* r *)
5204 reload ()
5206 | 118 when conf.debug -> (* v *)
5207 state.rects <- [];
5208 List.iter (fun l ->
5209 match getopaque l.pageno with
5210 | None -> ()
5211 | Some opaque ->
5212 let x0, y0, x1, y1 = pagebbox opaque in
5213 let a,b = float x0, float y0 in
5214 let c,d = float x1, float y0 in
5215 let e,f = float x1, float y1 in
5216 let h,j = float x0, float y1 in
5217 let rect = (a,b,c,d,e,f,h,j) in
5218 debugrect rect;
5219 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5220 ) state.layout;
5221 G.postRedisplay "v";
5223 | _ ->
5224 vlog "huh? %s" (Wsi.keyname key)
5227 let linknavkeyboard key mask linknav =
5228 let getpage pageno =
5229 let rec loop = function
5230 | [] -> None
5231 | l :: _ when l.pageno = pageno -> Some l
5232 | _ :: rest -> loop rest
5233 in loop state.layout
5235 let doexact (pageno, n) =
5236 match getopaque pageno, getpage pageno with
5237 | Some opaque, Some l ->
5238 if key = 0xff0d
5239 then
5240 let under = getlink opaque n in
5241 G.postRedisplay "link gotounder";
5242 gotounder under;
5243 state.mode <- View;
5244 else
5245 let opt, dir =
5246 match key with
5247 | 0xff50 -> (* home *)
5248 Some (findlink opaque LDfirst), -1
5250 | 0xff57 -> (* end *)
5251 Some (findlink opaque LDlast), 1
5253 | 0xff51 -> (* left *)
5254 Some (findlink opaque (LDleft n)), -1
5256 | 0xff53 -> (* right *)
5257 Some (findlink opaque (LDright n)), 1
5259 | 0xff52 -> (* up *)
5260 Some (findlink opaque (LDup n)), -1
5262 | 0xff54 -> (* down *)
5263 Some (findlink opaque (LDdown n)), 1
5265 | _ -> None, 0
5267 let pwl l dir =
5268 begin match findpwl l.pageno dir with
5269 | Pwlnotfound -> ()
5270 | Pwl pageno ->
5271 let notfound dir =
5272 state.mode <- LinkNav (Ltgendir dir);
5273 let y, h = getpageyh pageno in
5274 let y =
5275 if dir < 0
5276 then y + h - conf.winh
5277 else y
5279 gotoy y
5281 begin match getopaque pageno, getpage pageno with
5282 | Some opaque, Some _ ->
5283 let link =
5284 let ld = if dir > 0 then LDfirst else LDlast in
5285 findlink opaque ld
5287 begin match link with
5288 | Lfound m ->
5289 showlinktype (getlink opaque m);
5290 state.mode <- LinkNav (Ltexact (pageno, m));
5291 G.postRedisplay "linknav jpage";
5292 | _ -> notfound dir
5293 end;
5294 | _ -> notfound dir
5295 end;
5296 end;
5298 begin match opt with
5299 | Some Lnotfound -> pwl l dir;
5300 | Some (Lfound m) ->
5301 if m = n
5302 then pwl l dir
5303 else (
5304 let _, y0, _, y1 = getlinkrect opaque m in
5305 if y0 < l.pagey
5306 then gotopage1 l.pageno y0
5307 else (
5308 let d = fstate.fontsize + 1 in
5309 if y1 - l.pagey > l.pagevh - d
5310 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5311 else G.postRedisplay "linknav";
5313 showlinktype (getlink opaque m);
5314 state.mode <- LinkNav (Ltexact (l.pageno, m));
5317 | None -> viewkeyboard key mask
5318 end;
5319 | _ -> viewkeyboard key mask
5321 if key = 0xff63
5322 then (
5323 state.mode <- View;
5324 G.postRedisplay "leave linknav"
5326 else
5327 match linknav with
5328 | Ltgendir _ -> viewkeyboard key mask
5329 | Ltexact exact -> doexact exact
5332 let keyboard key mask =
5333 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5334 then wcmd "interrupt"
5335 else state.uioh <- state.uioh#key key mask
5338 let birdseyekeyboard key mask
5339 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5340 let incr =
5341 match conf.columns with
5342 | Csingle _ -> 1
5343 | Cmulti ((c, _, _), _) -> c
5344 | Csplit _ -> failwith "bird's eye split mode"
5346 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5347 match key with
5348 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5349 let y, h = getpageyh pageno in
5350 let top = (conf.winh - h) / 2 in
5351 gotoy (max 0 (y - top))
5352 | 0xff0d -> leavebirdseye beye false
5353 | 0xff1b -> leavebirdseye beye true (* escape *)
5354 | 0xff52 -> upbirdseye incr beye (* up *)
5355 | 0xff54 -> downbirdseye incr beye (* down *)
5356 | 0xff51 -> upbirdseye 1 beye (* left *)
5357 | 0xff53 -> downbirdseye 1 beye (* right *)
5359 | 0xff55 -> (* prior *)
5360 begin match state.layout with
5361 | l :: _ ->
5362 if l.pagey != 0
5363 then (
5364 state.mode <- Birdseye (
5365 oconf, leftx, l.pageno, hooverpageno, anchor
5367 gotopage1 l.pageno 0;
5369 else (
5370 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5371 match layout with
5372 | [] -> gotoy (clamp (-conf.winh))
5373 | l :: _ ->
5374 state.mode <- Birdseye (
5375 oconf, leftx, l.pageno, hooverpageno, anchor
5377 gotopage1 l.pageno 0
5380 | [] -> gotoy (clamp (-conf.winh))
5381 end;
5383 | 0xff56 -> (* next *)
5384 begin match List.rev state.layout with
5385 | l :: _ ->
5386 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5387 begin match layout with
5388 | [] ->
5389 let incr = l.pageh - l.pagevh in
5390 if incr = 0
5391 then (
5392 state.mode <-
5393 Birdseye (
5394 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5396 G.postRedisplay "birdseye pagedown";
5398 else gotoy (clamp (incr + conf.interpagespace*2));
5400 | l :: _ ->
5401 state.mode <-
5402 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5403 gotopage1 l.pageno 0;
5406 | [] -> gotoy (clamp conf.winh)
5407 end;
5409 | 0xff50 -> (* home *)
5410 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5411 gotopage1 0 0
5413 | 0xff57 -> (* end *)
5414 let pageno = state.pagecount - 1 in
5415 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5416 if not (pagevisible state.layout pageno)
5417 then
5418 let h =
5419 match List.rev state.pdims with
5420 | [] -> conf.winh
5421 | (_, _, h, _) :: _ -> h
5423 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5424 else G.postRedisplay "birdseye end";
5425 | _ -> viewkeyboard key mask
5428 let drawpage l linkindexbase =
5429 let color =
5430 match state.mode with
5431 | Textentry _ -> scalecolor 0.4
5432 | LinkNav _
5433 | View -> scalecolor 1.0
5434 | Birdseye (_, _, pageno, hooverpageno, _) ->
5435 if l.pageno = hooverpageno
5436 then scalecolor 0.9
5437 else (
5438 if l.pageno = pageno
5439 then scalecolor 1.0
5440 else scalecolor 0.8
5443 drawtiles l color;
5444 begin match getopaque l.pageno with
5445 | Some opaque ->
5446 if tileready l l.pagex l.pagey
5447 then
5448 let x = l.pagedispx - l.pagex
5449 and y = l.pagedispy - l.pagey in
5450 let hlmask =
5451 match conf.columns with
5452 | Csingle _ | Cmulti _ ->
5453 (if conf.hlinks then 1 else 0)
5454 + (if state.glinks
5455 && not (isbirdseye state.mode) then 2 else 0)
5456 | _ -> 0
5458 let s =
5459 match state.mode with
5460 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5461 | _ -> ""
5463 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5464 else 0
5466 | _ -> 0
5467 end;
5470 let scrollindicator () =
5471 let sbw, ph, sh = state.uioh#scrollph in
5472 let sbh, pw, sw = state.uioh#scrollpw in
5474 GlDraw.color (0.64, 0.64, 0.64);
5475 GlDraw.rect
5476 (float (conf.winw - sbw), 0.)
5477 (float conf.winw, float conf.winh)
5479 GlDraw.rect
5480 (0., float (conf.winh - sbh))
5481 (float (conf.winw - state.scrollw - 1), float conf.winh)
5483 GlDraw.color (0.0, 0.0, 0.0);
5485 GlDraw.rect
5486 (float (conf.winw - sbw), ph)
5487 (float conf.winw, ph +. sh)
5489 GlDraw.rect
5490 (pw, float (conf.winh - sbh))
5491 (pw +. sw, float conf.winh)
5495 let showsel () =
5496 match state.mstate with
5497 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5500 | Msel ((x0, y0), (x1, y1)) ->
5501 let rec loop = function
5502 | l :: ls ->
5503 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5504 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5505 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5506 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5507 then
5508 match getopaque l.pageno with
5509 | Some opaque ->
5510 let x0, y0 = pagetranslatepoint l x0 y0 in
5511 let x1, y1 = pagetranslatepoint l x1 y1 in
5512 seltext opaque (x0, y0, x1, y1);
5513 | _ -> ()
5514 else loop ls
5515 | [] -> ()
5517 loop state.layout
5520 let showrects rects =
5521 Gl.enable `blend;
5522 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5523 GlDraw.polygon_mode `both `fill;
5524 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5525 List.iter
5526 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5527 List.iter (fun l ->
5528 if l.pageno = pageno
5529 then (
5530 let dx = float (l.pagedispx - l.pagex) in
5531 let dy = float (l.pagedispy - l.pagey) in
5532 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5533 GlDraw.begins `quads;
5535 GlDraw.vertex2 (x0+.dx, y0+.dy);
5536 GlDraw.vertex2 (x1+.dx, y1+.dy);
5537 GlDraw.vertex2 (x2+.dx, y2+.dy);
5538 GlDraw.vertex2 (x3+.dx, y3+.dy);
5540 GlDraw.ends ();
5542 ) state.layout
5543 ) rects
5545 Gl.disable `blend;
5548 let display () =
5549 GlClear.color (scalecolor2 conf.bgcolor);
5550 GlClear.clear [`color];
5551 let rec loop linkindexbase = function
5552 | l :: rest ->
5553 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5554 loop linkindexbase rest
5555 | [] -> ()
5557 loop 0 state.layout;
5558 let rects =
5559 match state.mode with
5560 | LinkNav (Ltexact (pageno, linkno)) ->
5561 begin match getopaque pageno with
5562 | Some opaque ->
5563 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5564 (pageno, 5, (
5565 float x0, float y0,
5566 float x1, float y0,
5567 float x1, float y1,
5568 float x0, float y1)
5569 ) :: state.rects
5570 | None -> state.rects
5572 | _ -> state.rects
5574 showrects rects;
5575 showsel ();
5576 state.uioh#display;
5577 begin match state.mstate with
5578 | Mzoomrect ((x0, y0), (x1, y1)) ->
5579 Gl.enable `blend;
5580 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5581 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5582 GlDraw.rect (float x0, float y0)
5583 (float x1, float y1);
5584 Gl.disable `blend;
5585 | _ -> ()
5586 end;
5587 enttext ();
5588 scrollindicator ();
5589 Wsi.swapb ();
5592 let zoomrect x y x1 y1 =
5593 let x0 = min x x1
5594 and x1 = max x x1
5595 and y0 = min y y1 in
5596 gotoy (state.y + y0);
5597 state.anchor <- getanchor ();
5598 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5599 let margin =
5600 if state.w < conf.winw - state.scrollw
5601 then (conf.winw - state.scrollw - state.w) / 2
5602 else 0
5604 state.x <- (state.x + margin) - x0;
5605 setzoom zoom;
5606 Wsi.setcursor Wsi.CURSOR_INHERIT;
5607 state.mstate <- Mnone;
5610 let scrollx x =
5611 let winw = conf.winw - state.scrollw - 1 in
5612 let s = float x /. float winw in
5613 let destx = truncate (float (state.w + winw) *. s) in
5614 state.x <- winw - destx;
5615 gotoy_and_clear_text state.y;
5616 state.mstate <- Mscrollx;
5619 let scrolly y =
5620 let s = float y /. float conf.winh in
5621 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5622 gotoy_and_clear_text desty;
5623 state.mstate <- Mscrolly;
5626 let viewmouse button down x y mask =
5627 match button with
5628 | n when (n == 4 || n == 5) && not down ->
5629 if Wsi.withctrl mask
5630 then (
5631 match state.mstate with
5632 | Mzoom (oldn, i) ->
5633 if oldn = n
5634 then (
5635 if i = 2
5636 then
5637 let incr =
5638 match n with
5639 | 5 ->
5640 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5641 | _ ->
5642 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5644 let zoom = conf.zoom -. incr in
5645 setzoom zoom;
5646 state.mstate <- Mzoom (n, 0);
5647 else
5648 state.mstate <- Mzoom (n, i+1);
5650 else state.mstate <- Mzoom (n, 0)
5652 | _ -> state.mstate <- Mzoom (n, 0)
5654 else (
5655 match state.autoscroll with
5656 | Some step -> setautoscrollspeed step (n=4)
5657 | None ->
5658 let incr =
5659 if n = 4
5660 then -conf.scrollstep
5661 else conf.scrollstep
5663 let incr = incr * 2 in
5664 let y = clamp incr in
5665 gotoy_and_clear_text y
5668 | n when (n = 6 || n = 7) && not down && canpan () ->
5669 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5670 gotoy_and_clear_text state.y
5672 | 1 when Wsi.withctrl mask ->
5673 if down
5674 then (
5675 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5676 state.mstate <- Mpan (x, y)
5678 else
5679 state.mstate <- Mnone
5681 | 3 ->
5682 if down
5683 then (
5684 Wsi.setcursor Wsi.CURSOR_CYCLE;
5685 let p = (x, y) in
5686 state.mstate <- Mzoomrect (p, p)
5688 else (
5689 match state.mstate with
5690 | Mzoomrect ((x0, y0), _) ->
5691 if abs (x-x0) > 10 && abs (y - y0) > 10
5692 then zoomrect x0 y0 x y
5693 else (
5694 state.mstate <- Mnone;
5695 Wsi.setcursor Wsi.CURSOR_INHERIT;
5696 G.postRedisplay "kill accidental zoom rect";
5698 | _ ->
5699 Wsi.setcursor Wsi.CURSOR_INHERIT;
5700 state.mstate <- Mnone
5703 | 1 when x > conf.winw - state.scrollw ->
5704 if down
5705 then
5706 let _, position, sh = state.uioh#scrollph in
5707 if y > truncate position && y < truncate (position +. sh)
5708 then state.mstate <- Mscrolly
5709 else scrolly y
5710 else
5711 state.mstate <- Mnone
5713 | 1 when y > conf.winh - state.hscrollh ->
5714 if down
5715 then
5716 let _, position, sw = state.uioh#scrollpw in
5717 if x > truncate position && x < truncate (position +. sw)
5718 then state.mstate <- Mscrollx
5719 else scrollx x
5720 else
5721 state.mstate <- Mnone
5723 | 1 ->
5724 let dest = if down then getunder x y else Unone in
5725 begin match dest with
5726 | Ulinkgoto _
5727 | Ulinkuri _
5728 | Uremote _
5729 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5730 gotounder dest
5732 | Unone when down ->
5733 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5734 state.mstate <- Mpan (x, y);
5736 | Unone | Utext _ ->
5737 if down
5738 then (
5739 if conf.angle mod 360 = 0
5740 then (
5741 state.mstate <- Msel ((x, y), (x, y));
5742 G.postRedisplay "mouse select";
5745 else (
5746 match state.mstate with
5747 | Mnone -> ()
5749 | Mzoom _ | Mscrollx | Mscrolly ->
5750 state.mstate <- Mnone
5752 | Mzoomrect ((x0, y0), _) ->
5753 zoomrect x0 y0 x y
5755 | Mpan _ ->
5756 Wsi.setcursor Wsi.CURSOR_INHERIT;
5757 state.mstate <- Mnone
5759 | Msel ((x0, y0), (x1, y1)) ->
5760 let rec loop = function
5761 | [] -> ()
5762 | l :: rest ->
5763 let inside =
5764 let a0 = l.pagedispy in
5765 let a1 = a0 + l.pagevh in
5766 let b0 = l.pagedispx in
5767 let b1 = b0 + l.pagevw in
5768 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5769 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5771 if inside
5772 then
5773 match getopaque l.pageno with
5774 | Some opaque ->
5775 begin
5776 match Ne.pipe () with
5777 | Ne.Exn exn ->
5778 showtext '!'
5779 (Printf.sprintf
5780 "can not create sel pipe: %s"
5781 (Printexc.to_string exn));
5782 | Ne.Res (r, w) ->
5783 let doclose what fd =
5784 Ne.clo fd (fun msg ->
5785 dolog "%s close failed: %s" what msg)
5788 popen conf.selcmd [r, 0; w, -1];
5789 copysel w opaque;
5790 doclose "pipe/r" r;
5791 G.postRedisplay "copysel";
5792 with exn ->
5793 dolog "can not execute %S: %s"
5794 conf.selcmd (Printexc.to_string exn);
5795 doclose "pipe/r" r;
5796 doclose "pipe/w" w;
5798 | None -> ()
5799 else loop rest
5801 loop state.layout;
5802 Wsi.setcursor Wsi.CURSOR_INHERIT;
5803 state.mstate <- Mnone;
5807 | _ -> ()
5810 let birdseyemouse button down x y mask
5811 (conf, leftx, _, hooverpageno, anchor) =
5812 match button with
5813 | 1 when down ->
5814 let rec loop = function
5815 | [] -> ()
5816 | l :: rest ->
5817 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5818 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5819 then (
5820 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5822 else loop rest
5824 loop state.layout
5825 | 3 -> ()
5826 | _ -> viewmouse button down x y mask
5829 let mouse button down x y mask =
5830 state.uioh <- state.uioh#button button down x y mask;
5833 let motion ~x ~y =
5834 state.uioh <- state.uioh#motion x y
5837 let pmotion ~x ~y =
5838 state.uioh <- state.uioh#pmotion x y;
5841 let uioh = object
5842 method display = ()
5844 method key key mask =
5845 begin match state.mode with
5846 | Textentry textentry -> textentrykeyboard key mask textentry
5847 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5848 | View -> viewkeyboard key mask
5849 | LinkNav linknav -> linknavkeyboard key mask linknav
5850 end;
5851 state.uioh
5853 method button button bstate x y mask =
5854 begin match state.mode with
5855 | LinkNav _
5856 | View -> viewmouse button bstate x y mask
5857 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5858 | Textentry _ -> ()
5859 end;
5860 state.uioh
5862 method motion x y =
5863 begin match state.mode with
5864 | Textentry _ -> ()
5865 | View | Birdseye _ | LinkNav _ ->
5866 match state.mstate with
5867 | Mzoom _ | Mnone -> ()
5869 | Mpan (x0, y0) ->
5870 let dx = x - x0
5871 and dy = y0 - y in
5872 state.mstate <- Mpan (x, y);
5873 if canpan ()
5874 then state.x <- state.x + dx;
5875 let y = clamp dy in
5876 gotoy_and_clear_text y
5878 | Msel (a, _) ->
5879 state.mstate <- Msel (a, (x, y));
5880 G.postRedisplay "motion select";
5882 | Mscrolly ->
5883 let y = min conf.winh (max 0 y) in
5884 scrolly y
5886 | Mscrollx ->
5887 let x = min conf.winw (max 0 x) in
5888 scrollx x
5890 | Mzoomrect (p0, _) ->
5891 state.mstate <- Mzoomrect (p0, (x, y));
5892 G.postRedisplay "motion zoomrect";
5893 end;
5894 state.uioh
5896 method pmotion x y =
5897 begin match state.mode with
5898 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5899 let rec loop = function
5900 | [] ->
5901 if hooverpageno != -1
5902 then (
5903 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5904 G.postRedisplay "pmotion birdseye no hoover";
5906 | l :: rest ->
5907 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5908 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5909 then (
5910 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5911 G.postRedisplay "pmotion birdseye hoover";
5913 else loop rest
5915 loop state.layout
5917 | Textentry _ -> ()
5919 | LinkNav _
5920 | View ->
5921 match state.mstate with
5922 | Mnone -> updateunder x y
5923 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5925 end;
5926 state.uioh
5928 method infochanged _ = ()
5930 method scrollph =
5931 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5932 let p, h = scrollph state.y maxy in
5933 state.scrollw, p, h
5935 method scrollpw =
5936 let winw = conf.winw - state.scrollw - 1 in
5937 let fwinw = float winw in
5938 let sw =
5939 let sw = fwinw /. float state.w in
5940 let sw = fwinw *. sw in
5941 max sw (float conf.scrollh)
5943 let position, sw =
5944 let f = state.w+winw in
5945 let r = float (winw-state.x) /. float f in
5946 let p = fwinw *. r in
5947 p-.sw/.2., sw
5949 let sw =
5950 if position +. sw > fwinw
5951 then fwinw -. position
5952 else sw
5954 state.hscrollh, position, sw
5956 method modehash =
5957 let modename =
5958 match state.mode with
5959 | LinkNav _ -> "links"
5960 | Textentry _ -> "textentry"
5961 | Birdseye _ -> "birdseye"
5962 | View -> "view"
5964 findkeyhash conf modename
5965 end;;
5967 module Config =
5968 struct
5969 open Parser
5971 let fontpath = ref "";;
5973 module KeyMap =
5974 Map.Make (struct type t = (int * int) let compare = compare end);;
5976 let unent s =
5977 let l = String.length s in
5978 let b = Buffer.create l in
5979 unent b s 0 l;
5980 Buffer.contents b;
5983 let home =
5984 try Sys.getenv "HOME"
5985 with exn ->
5986 prerr_endline
5987 ("Can not determine home directory location: " ^
5988 Printexc.to_string exn);
5992 let modifier_of_string = function
5993 | "alt" -> Wsi.altmask
5994 | "shift" -> Wsi.shiftmask
5995 | "ctrl" | "control" -> Wsi.ctrlmask
5996 | "meta" -> Wsi.metamask
5997 | _ -> 0
6000 let key_of_string =
6001 let r = Str.regexp "-" in
6002 fun s ->
6003 let elems = Str.full_split r s in
6004 let f n k m =
6005 let g s =
6006 let m1 = modifier_of_string s in
6007 if m1 = 0
6008 then (Wsi.namekey s, m)
6009 else (k, m lor m1)
6010 in function
6011 | Str.Delim s when n land 1 = 0 -> g s
6012 | Str.Text s -> g s
6013 | Str.Delim _ -> (k, m)
6015 let rec loop n k m = function
6016 | [] -> (k, m)
6017 | x :: xs ->
6018 let k, m = f n k m x in
6019 loop (n+1) k m xs
6021 loop 0 0 0 elems
6024 let keys_of_string =
6025 let r = Str.regexp "[ \t]" in
6026 fun s ->
6027 let elems = Str.split r s in
6028 List.map key_of_string elems
6031 let copykeyhashes c =
6032 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6035 let config_of c attrs =
6036 let apply c k v =
6038 match k with
6039 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6040 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6041 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6042 | "preload" -> { c with preload = bool_of_string v }
6043 | "page-bias" -> { c with pagebias = int_of_string v }
6044 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6045 | "horizontal-scroll-step" ->
6046 { c with hscrollstep = max (int_of_string v) 1 }
6047 | "auto-scroll-step" ->
6048 { c with autoscrollstep = max 0 (int_of_string v) }
6049 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6050 | "crop-hack" -> { c with crophack = bool_of_string v }
6051 | "throttle" ->
6052 let mw =
6053 match String.lowercase v with
6054 | "true" -> Some infinity
6055 | "false" -> None
6056 | f -> Some (float_of_string f)
6058 { c with maxwait = mw}
6059 | "highlight-links" -> { c with hlinks = bool_of_string v }
6060 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6061 | "vertical-margin" ->
6062 { c with interpagespace = max 0 (int_of_string v) }
6063 | "zoom" ->
6064 let zoom = float_of_string v /. 100. in
6065 let zoom = max zoom 0.0 in
6066 { c with zoom = zoom }
6067 | "presentation" -> { c with presentation = bool_of_string v }
6068 | "rotation-angle" -> { c with angle = int_of_string v }
6069 | "width" -> { c with winw = max 20 (int_of_string v) }
6070 | "height" -> { c with winh = max 20 (int_of_string v) }
6071 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6072 | "proportional-display" -> { c with proportional = bool_of_string v }
6073 | "pixmap-cache-size" ->
6074 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6075 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6076 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6077 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6078 | "persistent-location" -> { c with jumpback = bool_of_string v }
6079 | "background-color" -> { c with bgcolor = color_of_string v }
6080 | "scrollbar-in-presentation" ->
6081 { c with scrollbarinpm = bool_of_string v }
6082 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6083 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6084 | "mupdf-store-size" ->
6085 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6086 | "checkers" -> { c with checkers = bool_of_string v }
6087 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6088 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6089 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6090 | "uri-launcher" -> { c with urilauncher = unent v }
6091 | "path-launcher" -> { c with pathlauncher = unent v }
6092 | "color-space" -> { c with colorspace = colorspace_of_string v }
6093 | "invert-colors" -> { c with invert = bool_of_string v }
6094 | "brightness" -> { c with colorscale = float_of_string v }
6095 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6096 | "ghyllscroll" ->
6097 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6098 | "columns" ->
6099 let (n, _, _) as nab = multicolumns_of_string v in
6100 if n < 0
6101 then { c with columns = Csplit (-n, [||]) }
6102 else { c with columns = Cmulti (nab, [||]) }
6103 | "birds-eye-columns" ->
6104 { c with beyecolumns = Some (max (int_of_string v) 2) }
6105 | "selection-command" -> { c with selcmd = unent v }
6106 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6107 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6108 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6109 | _ -> c
6110 with exn ->
6111 prerr_endline ("Error processing attribute (`" ^
6112 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6115 let rec fold c = function
6116 | [] -> c
6117 | (k, v) :: rest ->
6118 let c = apply c k v in
6119 fold c rest
6121 fold { c with keyhashes = copykeyhashes c } attrs;
6124 let fromstring f pos n v d =
6125 try f v
6126 with exn ->
6127 dolog "Error processing attribute (%S=%S) at %d\n%s"
6128 n v pos (Printexc.to_string exn)
6133 let bookmark_of attrs =
6134 let rec fold title page rely visy = function
6135 | ("title", v) :: rest -> fold v page rely visy rest
6136 | ("page", v) :: rest -> fold title v rely visy rest
6137 | ("rely", v) :: rest -> fold title page v visy rest
6138 | ("visy", v) :: rest -> fold title page rely v rest
6139 | _ :: rest -> fold title page rely visy rest
6140 | [] -> title, page, rely, visy
6142 fold "invalid" "0" "0" "0" attrs
6145 let doc_of attrs =
6146 let rec fold path page rely pan visy = function
6147 | ("path", v) :: rest -> fold v page rely pan visy rest
6148 | ("page", v) :: rest -> fold path v rely pan visy rest
6149 | ("rely", v) :: rest -> fold path page v pan visy rest
6150 | ("pan", v) :: rest -> fold path page rely v visy rest
6151 | ("visy", v) :: rest -> fold path page rely pan v rest
6152 | _ :: rest -> fold path page rely pan visy rest
6153 | [] -> path, page, rely, pan, visy
6155 fold "" "0" "0" "0" "0" attrs
6158 let map_of attrs =
6159 let rec fold rs ls = function
6160 | ("out", v) :: rest -> fold v ls rest
6161 | ("in", v) :: rest -> fold rs v rest
6162 | _ :: rest -> fold ls rs rest
6163 | [] -> ls, rs
6165 fold "" "" attrs
6168 let setconf dst src =
6169 dst.scrollbw <- src.scrollbw;
6170 dst.scrollh <- src.scrollh;
6171 dst.icase <- src.icase;
6172 dst.preload <- src.preload;
6173 dst.pagebias <- src.pagebias;
6174 dst.verbose <- src.verbose;
6175 dst.scrollstep <- src.scrollstep;
6176 dst.maxhfit <- src.maxhfit;
6177 dst.crophack <- src.crophack;
6178 dst.autoscrollstep <- src.autoscrollstep;
6179 dst.maxwait <- src.maxwait;
6180 dst.hlinks <- src.hlinks;
6181 dst.underinfo <- src.underinfo;
6182 dst.interpagespace <- src.interpagespace;
6183 dst.zoom <- src.zoom;
6184 dst.presentation <- src.presentation;
6185 dst.angle <- src.angle;
6186 dst.winw <- src.winw;
6187 dst.winh <- src.winh;
6188 dst.savebmarks <- src.savebmarks;
6189 dst.memlimit <- src.memlimit;
6190 dst.proportional <- src.proportional;
6191 dst.texcount <- src.texcount;
6192 dst.sliceheight <- src.sliceheight;
6193 dst.thumbw <- src.thumbw;
6194 dst.jumpback <- src.jumpback;
6195 dst.bgcolor <- src.bgcolor;
6196 dst.scrollbarinpm <- src.scrollbarinpm;
6197 dst.tilew <- src.tilew;
6198 dst.tileh <- src.tileh;
6199 dst.mustoresize <- src.mustoresize;
6200 dst.checkers <- src.checkers;
6201 dst.aalevel <- src.aalevel;
6202 dst.trimmargins <- src.trimmargins;
6203 dst.trimfuzz <- src.trimfuzz;
6204 dst.urilauncher <- src.urilauncher;
6205 dst.colorspace <- src.colorspace;
6206 dst.invert <- src.invert;
6207 dst.colorscale <- src.colorscale;
6208 dst.redirectstderr <- src.redirectstderr;
6209 dst.ghyllscroll <- src.ghyllscroll;
6210 dst.columns <- src.columns;
6211 dst.beyecolumns <- src.beyecolumns;
6212 dst.selcmd <- src.selcmd;
6213 dst.updatecurs <- src.updatecurs;
6214 dst.pathlauncher <- src.pathlauncher;
6215 dst.keyhashes <- copykeyhashes src;
6216 dst.hfsize <- src.hfsize;
6217 dst.hscrollstep <- src.hscrollstep;
6218 dst.pgscale <- src.pgscale;
6221 let get s =
6222 let h = Hashtbl.create 10 in
6223 let dc = { defconf with angle = defconf.angle } in
6224 let rec toplevel v t spos _ =
6225 match t with
6226 | Vdata | Vcdata | Vend -> v
6227 | Vopen ("llppconfig", _, closed) ->
6228 if closed
6229 then v
6230 else { v with f = llppconfig }
6231 | Vopen _ ->
6232 error "unexpected subelement at top level" s spos
6233 | Vclose _ -> error "unexpected close at top level" s spos
6235 and llppconfig v t spos _ =
6236 match t with
6237 | Vdata | Vcdata -> v
6238 | Vend -> error "unexpected end of input in llppconfig" s spos
6239 | Vopen ("defaults", attrs, closed) ->
6240 let c = config_of dc attrs in
6241 setconf dc c;
6242 if closed
6243 then v
6244 else { v with f = defaults }
6246 | Vopen ("ui-font", attrs, closed) ->
6247 let rec getsize size = function
6248 | [] -> size
6249 | ("size", v) :: rest ->
6250 let size =
6251 fromstring int_of_string spos "size" v fstate.fontsize in
6252 getsize size rest
6253 | l -> getsize size l
6255 fstate.fontsize <- getsize fstate.fontsize attrs;
6256 if closed
6257 then v
6258 else { v with f = uifont (Buffer.create 10) }
6260 | Vopen ("doc", attrs, closed) ->
6261 let pathent, spage, srely, span, svisy = doc_of attrs in
6262 let path = unent pathent
6263 and pageno = fromstring int_of_string spos "page" spage 0
6264 and rely = fromstring float_of_string spos "rely" srely 0.0
6265 and pan = fromstring int_of_string spos "pan" span 0
6266 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6267 let c = config_of dc attrs in
6268 let anchor = (pageno, rely, visy) in
6269 if closed
6270 then (Hashtbl.add h path (c, [], pan, anchor); v)
6271 else { v with f = doc path pan anchor c [] }
6273 | Vopen _ ->
6274 error "unexpected subelement in llppconfig" s spos
6276 | Vclose "llppconfig" -> { v with f = toplevel }
6277 | Vclose _ -> error "unexpected close in llppconfig" s spos
6279 and defaults v t spos _ =
6280 match t with
6281 | Vdata | Vcdata -> v
6282 | Vend -> error "unexpected end of input in defaults" s spos
6283 | Vopen ("keymap", attrs, closed) ->
6284 let modename =
6285 try List.assoc "mode" attrs
6286 with Not_found -> "global" in
6287 if closed
6288 then v
6289 else
6290 let ret keymap =
6291 let h = findkeyhash dc modename in
6292 KeyMap.iter (Hashtbl.replace h) keymap;
6293 defaults
6295 { v with f = pkeymap ret KeyMap.empty }
6297 | Vopen (_, _, _) ->
6298 error "unexpected subelement in defaults" s spos
6300 | Vclose "defaults" ->
6301 { v with f = llppconfig }
6303 | Vclose _ -> error "unexpected close in defaults" s spos
6305 and uifont b v t spos epos =
6306 match t with
6307 | Vdata | Vcdata ->
6308 Buffer.add_substring b s spos (epos - spos);
6310 | Vopen (_, _, _) ->
6311 error "unexpected subelement in ui-font" s spos
6312 | Vclose "ui-font" ->
6313 if String.length !fontpath = 0
6314 then fontpath := Buffer.contents b;
6315 { v with f = llppconfig }
6316 | Vclose _ -> error "unexpected close in ui-font" s spos
6317 | Vend -> error "unexpected end of input in ui-font" s spos
6319 and doc path pan anchor c bookmarks v t spos _ =
6320 match t with
6321 | Vdata | Vcdata -> v
6322 | Vend -> error "unexpected end of input in doc" s spos
6323 | Vopen ("bookmarks", _, closed) ->
6324 if closed
6325 then v
6326 else { v with f = pbookmarks path pan anchor c bookmarks }
6328 | Vopen ("keymap", attrs, closed) ->
6329 let modename =
6330 try List.assoc "mode" attrs
6331 with Not_found -> "global"
6333 if closed
6334 then v
6335 else
6336 let ret keymap =
6337 let h = findkeyhash c modename in
6338 KeyMap.iter (Hashtbl.replace h) keymap;
6339 doc path pan anchor c bookmarks
6341 { v with f = pkeymap ret KeyMap.empty }
6343 | Vopen (_, _, _) ->
6344 error "unexpected subelement in doc" s spos
6346 | Vclose "doc" ->
6347 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6348 { v with f = llppconfig }
6350 | Vclose _ -> error "unexpected close in doc" s spos
6352 and pkeymap ret keymap v t spos _ =
6353 match t with
6354 | Vdata | Vcdata -> v
6355 | Vend -> error "unexpected end of input in keymap" s spos
6356 | Vopen ("map", attrs, closed) ->
6357 let r, l = map_of attrs in
6358 let kss = fromstring keys_of_string spos "in" r [] in
6359 let lss = fromstring keys_of_string spos "out" l [] in
6360 let keymap =
6361 match kss with
6362 | [] -> keymap
6363 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6364 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6366 if closed
6367 then { v with f = pkeymap ret keymap }
6368 else
6369 let f () = v in
6370 { v with f = skip "map" f }
6372 | Vopen _ ->
6373 error "unexpected subelement in keymap" s spos
6375 | Vclose "keymap" ->
6376 { v with f = ret keymap }
6378 | Vclose _ -> error "unexpected close in keymap" s spos
6380 and pbookmarks path pan anchor c bookmarks v t spos _ =
6381 match t with
6382 | Vdata | Vcdata -> v
6383 | Vend -> error "unexpected end of input in bookmarks" s spos
6384 | Vopen ("item", attrs, closed) ->
6385 let titleent, spage, srely, svisy = bookmark_of attrs in
6386 let page = fromstring int_of_string spos "page" spage 0
6387 and rely = fromstring float_of_string spos "rely" srely 0.0
6388 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6389 let bookmarks =
6390 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6392 if closed
6393 then { v with f = pbookmarks path pan anchor c bookmarks }
6394 else
6395 let f () = v in
6396 { v with f = skip "item" f }
6398 | Vopen _ ->
6399 error "unexpected subelement in bookmarks" s spos
6401 | Vclose "bookmarks" ->
6402 { v with f = doc path pan anchor c bookmarks }
6404 | Vclose _ -> error "unexpected close in bookmarks" s spos
6406 and skip tag f v t spos _ =
6407 match t with
6408 | Vdata | Vcdata -> v
6409 | Vend ->
6410 error ("unexpected end of input in skipped " ^ tag) s spos
6411 | Vopen (tag', _, closed) ->
6412 if closed
6413 then v
6414 else
6415 let f' () = { v with f = skip tag f } in
6416 { v with f = skip tag' f' }
6417 | Vclose ctag ->
6418 if tag = ctag
6419 then f ()
6420 else error ("unexpected close in skipped " ^ tag) s spos
6423 parse { f = toplevel; accu = () } s;
6424 h, dc;
6427 let do_load f ic =
6429 let len = in_channel_length ic in
6430 let s = String.create len in
6431 really_input ic s 0 len;
6432 f s;
6433 with
6434 | Parse_error (msg, s, pos) ->
6435 let subs = subs s pos in
6436 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6437 failwith ("parse error: " ^ s)
6439 | exn ->
6440 failwith ("config load error: " ^ Printexc.to_string exn)
6443 let defconfpath =
6444 let dir =
6446 let dir = Filename.concat home ".config" in
6447 if Sys.is_directory dir then dir else home
6448 with _ -> home
6450 Filename.concat dir "llpp.conf"
6453 let confpath = ref defconfpath;;
6455 let load1 f =
6456 if Sys.file_exists !confpath
6457 then
6458 match
6459 (try Some (open_in_bin !confpath)
6460 with exn ->
6461 prerr_endline
6462 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6463 Printexc.to_string exn);
6464 None
6466 with
6467 | Some ic ->
6468 let success =
6470 f (do_load get ic)
6471 with exn ->
6472 prerr_endline
6473 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6474 Printexc.to_string exn);
6475 false
6477 close_in ic;
6478 success
6480 | None -> false
6481 else
6482 f (Hashtbl.create 0, defconf)
6485 let load () =
6486 let f (h, dc) =
6487 let pc, pb, px, pa =
6489 Hashtbl.find h (Filename.basename state.path)
6490 with Not_found -> dc, [], 0, emptyanchor
6492 setconf defconf dc;
6493 setconf conf pc;
6494 state.bookmarks <- pb;
6495 state.x <- px;
6496 state.scrollw <- conf.scrollbw;
6497 if conf.jumpback
6498 then state.anchor <- pa;
6499 cbput state.hists.nav pa;
6500 true
6502 load1 f
6505 let add_attrs bb always dc c =
6506 let ob s a b =
6507 if always || a != b
6508 then Printf.bprintf bb "\n %s='%b'" s a
6509 and oi s a b =
6510 if always || a != b
6511 then Printf.bprintf bb "\n %s='%d'" s a
6512 and oI s a b =
6513 if always || a != b
6514 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6515 and oz s a b =
6516 if always || a <> b
6517 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6518 and oF s a b =
6519 if always || a <> b
6520 then Printf.bprintf bb "\n %s='%f'" s a
6521 and oc s a b =
6522 if always || a <> b
6523 then
6524 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6525 and oC s a b =
6526 if always || a <> b
6527 then
6528 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6529 and oR s a b =
6530 if always || a <> b
6531 then
6532 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6533 and os s a b =
6534 if always || a <> b
6535 then
6536 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6537 and og s a b =
6538 if always || a <> b
6539 then
6540 match a with
6541 | None -> ()
6542 | Some (_N, _A, _B) ->
6543 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6544 and oW s a b =
6545 if always || a <> b
6546 then
6547 let v =
6548 match a with
6549 | None -> "false"
6550 | Some f ->
6551 if f = infinity
6552 then "true"
6553 else string_of_float f
6555 Printf.bprintf bb "\n %s='%s'" s v
6556 and oco s a b =
6557 if always || a <> b
6558 then
6559 match a with
6560 | Cmulti ((n, a, b), _) when n > 1 ->
6561 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6562 | Csplit (n, _) when n > 1 ->
6563 Printf.bprintf bb "\n %s='%d'" s ~-n
6564 | _ -> ()
6565 and obeco s a b =
6566 if always || a <> b
6567 then
6568 match a with
6569 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6570 | _ -> ()
6572 let w, h =
6573 if always
6574 then dc.winw, dc.winh
6575 else
6576 match state.fullscreen with
6577 | Some wh -> wh
6578 | None -> c.winw, c.winh
6580 oi "width" w dc.winw;
6581 oi "height" h dc.winh;
6582 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6583 oi "scroll-handle-height" c.scrollh dc.scrollh;
6584 ob "case-insensitive-search" c.icase dc.icase;
6585 ob "preload" c.preload dc.preload;
6586 oi "page-bias" c.pagebias dc.pagebias;
6587 oi "scroll-step" c.scrollstep dc.scrollstep;
6588 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6589 ob "max-height-fit" c.maxhfit dc.maxhfit;
6590 ob "crop-hack" c.crophack dc.crophack;
6591 oW "throttle" c.maxwait dc.maxwait;
6592 ob "highlight-links" c.hlinks dc.hlinks;
6593 ob "under-cursor-info" c.underinfo dc.underinfo;
6594 oi "vertical-margin" c.interpagespace dc.interpagespace;
6595 oz "zoom" c.zoom dc.zoom;
6596 ob "presentation" c.presentation dc.presentation;
6597 oi "rotation-angle" c.angle dc.angle;
6598 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6599 ob "proportional-display" c.proportional dc.proportional;
6600 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6601 oi "tex-count" c.texcount dc.texcount;
6602 oi "slice-height" c.sliceheight dc.sliceheight;
6603 oi "thumbnail-width" c.thumbw dc.thumbw;
6604 ob "persistent-location" c.jumpback dc.jumpback;
6605 oc "background-color" c.bgcolor dc.bgcolor;
6606 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6607 oi "tile-width" c.tilew dc.tilew;
6608 oi "tile-height" c.tileh dc.tileh;
6609 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6610 ob "checkers" c.checkers dc.checkers;
6611 oi "aalevel" c.aalevel dc.aalevel;
6612 ob "trim-margins" c.trimmargins dc.trimmargins;
6613 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6614 os "uri-launcher" c.urilauncher dc.urilauncher;
6615 os "path-launcher" c.pathlauncher dc.pathlauncher;
6616 oC "color-space" c.colorspace dc.colorspace;
6617 ob "invert-colors" c.invert dc.invert;
6618 oF "brightness" c.colorscale dc.colorscale;
6619 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6620 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6621 oco "columns" c.columns dc.columns;
6622 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6623 os "selection-command" c.selcmd dc.selcmd;
6624 ob "update-cursor" c.updatecurs dc.updatecurs;
6625 oi "hint-font-size" c.hfsize dc.hfsize;
6626 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6627 oF "page-scroll-scale" c.pgscale dc.pgscale;
6630 let keymapsbuf always dc c =
6631 let bb = Buffer.create 16 in
6632 let rec loop = function
6633 | [] -> ()
6634 | (modename, h) :: rest ->
6635 let dh = findkeyhash dc modename in
6636 if always || h <> dh
6637 then (
6638 if Hashtbl.length h > 0
6639 then (
6640 if Buffer.length bb > 0
6641 then Buffer.add_char bb '\n';
6642 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6643 Hashtbl.iter (fun i o ->
6644 let isdifferent = always ||
6646 let dO = Hashtbl.find dh i in
6647 dO <> o
6648 with Not_found -> true
6650 if isdifferent
6651 then
6652 let addkm (k, m) =
6653 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6654 if Wsi.withalt m then Buffer.add_string bb "alt-";
6655 if Wsi.withshift m then Buffer.add_string bb "shift-";
6656 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6657 Buffer.add_string bb (Wsi.keyname k);
6659 let addkms l =
6660 let rec loop = function
6661 | [] -> ()
6662 | km :: [] -> addkm km
6663 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6665 loop l
6667 Buffer.add_string bb "<map in='";
6668 addkm i;
6669 match o with
6670 | KMinsrt km ->
6671 Buffer.add_string bb "' out='";
6672 addkm km;
6673 Buffer.add_string bb "'/>\n"
6675 | KMinsrl kms ->
6676 Buffer.add_string bb "' out='";
6677 addkms kms;
6678 Buffer.add_string bb "'/>\n"
6680 | KMmulti (ins, kms) ->
6681 Buffer.add_char bb ' ';
6682 addkms ins;
6683 Buffer.add_string bb "' out='";
6684 addkms kms;
6685 Buffer.add_string bb "'/>\n"
6686 ) h;
6687 Buffer.add_string bb "</keymap>";
6690 loop rest
6692 loop c.keyhashes;
6696 let save () =
6697 let uifontsize = fstate.fontsize in
6698 let bb = Buffer.create 32768 in
6699 let f (h, dc) =
6700 let dc = if conf.bedefault then conf else dc in
6701 Buffer.add_string bb "<llppconfig>\n";
6703 if String.length !fontpath > 0
6704 then
6705 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6706 uifontsize
6707 !fontpath
6708 else (
6709 if uifontsize <> 14
6710 then
6711 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6714 Buffer.add_string bb "<defaults ";
6715 add_attrs bb true dc dc;
6716 let kb = keymapsbuf true dc dc in
6717 if Buffer.length kb > 0
6718 then (
6719 Buffer.add_string bb ">\n";
6720 Buffer.add_buffer bb kb;
6721 Buffer.add_string bb "\n</defaults>\n";
6723 else Buffer.add_string bb "/>\n";
6725 let adddoc path pan anchor c bookmarks =
6726 if bookmarks == [] && c = dc && anchor = emptyanchor
6727 then ()
6728 else (
6729 Printf.bprintf bb "<doc path='%s'"
6730 (enent path 0 (String.length path));
6732 if anchor <> emptyanchor
6733 then (
6734 let n, rely, visy = anchor in
6735 Printf.bprintf bb " page='%d'" n;
6736 if rely > 1e-6
6737 then
6738 Printf.bprintf bb " rely='%f'" rely
6740 if abs_float visy > 1e-6
6741 then
6742 Printf.bprintf bb " visy='%f'" visy
6746 if pan != 0
6747 then Printf.bprintf bb " pan='%d'" pan;
6749 add_attrs bb false dc c;
6750 let kb = keymapsbuf false dc c in
6752 begin match bookmarks with
6753 | [] ->
6754 if Buffer.length kb > 0
6755 then (
6756 Buffer.add_string bb ">\n";
6757 Buffer.add_buffer bb kb;
6758 Buffer.add_string bb "\n</doc>\n";
6760 else Buffer.add_string bb "/>\n"
6761 | _ ->
6762 Buffer.add_string bb ">\n<bookmarks>\n";
6763 List.iter (fun (title, _level, (page, rely, visy)) ->
6764 Printf.bprintf bb
6765 "<item title='%s' page='%d'"
6766 (enent title 0 (String.length title))
6767 page
6769 if rely > 1e-6
6770 then
6771 Printf.bprintf bb " rely='%f'" rely
6773 if abs_float visy > 1e-6
6774 then
6775 Printf.bprintf bb " visy='%f'" visy
6777 Buffer.add_string bb "/>\n";
6778 ) bookmarks;
6779 Buffer.add_string bb "</bookmarks>";
6780 if Buffer.length kb > 0
6781 then (
6782 Buffer.add_string bb "\n";
6783 Buffer.add_buffer bb kb;
6785 Buffer.add_string bb "\n</doc>\n";
6786 end;
6790 let pan, conf =
6791 match state.mode with
6792 | Birdseye (c, pan, _, _, _) ->
6793 let beyecolumns =
6794 match conf.columns with
6795 | Cmulti ((c, _, _), _) -> Some c
6796 | Csingle _ -> None
6797 | Csplit _ -> None
6798 and columns =
6799 match c.columns with
6800 | Cmulti (c, _) -> Cmulti (c, [||])
6801 | Csingle _ -> Csingle [||]
6802 | Csplit _ -> failwith "quit from bird's eye while split"
6804 pan, { c with beyecolumns = beyecolumns; columns = columns }
6805 | _ -> state.x, conf
6807 let basename = Filename.basename state.path in
6808 adddoc basename pan (getanchor ())
6809 (let conf =
6810 let autoscrollstep =
6811 match state.autoscroll with
6812 | Some step -> step
6813 | None -> conf.autoscrollstep
6815 match state.mode with
6816 | Birdseye (bc, _, _, _, _) ->
6817 { conf with
6818 zoom = bc.zoom;
6819 presentation = bc.presentation;
6820 interpagespace = bc.interpagespace;
6821 maxwait = bc.maxwait;
6822 autoscrollstep = autoscrollstep }
6823 | _ -> { conf with autoscrollstep = autoscrollstep }
6824 in conf)
6825 (if conf.savebmarks then state.bookmarks else []);
6827 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6828 if basename <> path
6829 then adddoc path x anchor c bookmarks
6830 ) h;
6831 Buffer.add_string bb "</llppconfig>\n";
6832 true;
6834 if load1 f && Buffer.length bb > 0
6835 then
6837 let tmp = !confpath ^ ".tmp" in
6838 let oc = open_out_bin tmp in
6839 Buffer.output_buffer oc bb;
6840 close_out oc;
6841 Unix.rename tmp !confpath;
6842 with exn ->
6843 prerr_endline
6844 ("error while saving configuration: " ^ Printexc.to_string exn)
6846 end;;
6848 let () =
6849 let trimcachepath = ref "" in
6850 Arg.parse
6851 (Arg.align
6852 [("-p", Arg.String (fun s -> state.password <- s) ,
6853 "<password> Set password");
6855 ("-f", Arg.String (fun s -> Config.fontpath := s),
6856 "<path> Set path to the user interface font");
6858 ("-c", Arg.String (fun s -> Config.confpath := s),
6859 "<path> Set path to the configuration file");
6861 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6862 "<path> Set path to the trim cache file");
6864 ("-v", Arg.Unit (fun () ->
6865 Printf.printf
6866 "%s\nconfiguration path: %s\n"
6867 (version ())
6868 Config.defconfpath
6870 exit 0), " Print version and exit");
6873 (fun s -> state.path <- s)
6874 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6876 if String.length state.path = 0
6877 then (prerr_endline "file name missing"; exit 1);
6879 if not (Config.load ())
6880 then prerr_endline "failed to load configuration";
6882 let globalkeyhash = findkeyhash conf "global" in
6883 let wsfd, winw, winh = Wsi.init (object
6884 method expose =
6885 if nogeomcmds state.geomcmds || platform == Posx
6886 then display ()
6887 else (
6888 GlClear.color (scalecolor2 conf.bgcolor);
6889 GlClear.clear [`color];
6891 method display = display ()
6892 method reshape w h = reshape w h
6893 method mouse b d x y m = mouse b d x y m
6894 method motion x y = state.mpos <- (x, y); motion x y
6895 method pmotion x y = state.mpos <- (x, y); pmotion x y
6896 method key k m =
6897 let mascm = m land (
6898 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6899 ) in
6900 match state.keystate with
6901 | KSnone ->
6902 let km = k, mascm in
6903 begin
6904 match
6905 let modehash = state.uioh#modehash in
6906 try Hashtbl.find modehash km
6907 with Not_found ->
6908 try Hashtbl.find globalkeyhash km
6909 with Not_found -> KMinsrt (k, m)
6910 with
6911 | KMinsrt (k, m) -> keyboard k m
6912 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6913 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6915 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6916 List.iter (fun (k, m) -> keyboard k m) insrt;
6917 state.keystate <- KSnone
6918 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6919 state.keystate <- KSinto (keys, insrt)
6920 | _ ->
6921 state.keystate <- KSnone
6923 method enter x y = state.mpos <- (x, y); pmotion x y
6924 method leave = state.mpos <- (-1, -1)
6925 method quit = raise Quit
6926 end) conf.winw conf.winh (platform = Posx) in
6928 state.wsfd <- wsfd;
6930 if not (
6931 List.exists GlMisc.check_extension
6932 [ "GL_ARB_texture_rectangle"
6933 ; "GL_EXT_texture_recangle"
6934 ; "GL_NV_texture_rectangle" ]
6936 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6938 let cr, sw =
6939 match Ne.pipe () with
6940 | Ne.Exn exn ->
6941 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6942 exit 1
6943 | Ne.Res rw -> rw
6944 and sr, cw =
6945 match Ne.pipe () with
6946 | Ne.Exn exn ->
6947 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6948 exit 1
6949 | Ne.Res rw -> rw
6952 cloexec cr;
6953 cloexec sw;
6954 cloexec sr;
6955 cloexec cw;
6957 setcheckers conf.checkers;
6958 redirectstderr ();
6960 init (cr, cw) (
6961 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6962 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6963 !Config.fontpath, !trimcachepath
6965 state.sr <- sr;
6966 state.sw <- sw;
6967 state.text <- "Opening " ^ state.path;
6968 reshape winw winh;
6969 opendoc state.path state.password;
6970 state.uioh <- uioh;
6972 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6974 let rec loop deadline =
6975 let r =
6976 match state.errfd with
6977 | None -> [state.sr; state.wsfd]
6978 | Some fd -> [state.sr; state.wsfd; fd]
6980 if state.redisplay
6981 then (
6982 state.redisplay <- false;
6983 display ();
6985 let timeout =
6986 let now = now () in
6987 if deadline > now
6988 then (
6989 if deadline = infinity
6990 then ~-.1.0
6991 else max 0.0 (deadline -. now)
6993 else 0.0
6995 let r, _, _ =
6996 try Unix.select r [] [] timeout
6997 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6999 begin match r with
7000 | [] ->
7001 state.ghyll None;
7002 let newdeadline =
7003 if state.ghyll == noghyll
7004 then
7005 match state.autoscroll with
7006 | Some step when step != 0 ->
7007 let y = state.y + step in
7008 let y =
7009 if y < 0
7010 then state.maxy
7011 else if y >= state.maxy then 0 else y
7013 gotoy y;
7014 if state.mode = View
7015 then state.text <- "";
7016 deadline +. 0.01
7017 | _ -> infinity
7018 else deadline +. 0.01
7020 loop newdeadline
7022 | l ->
7023 let rec checkfds = function
7024 | [] -> ()
7025 | fd :: rest when fd = state.sr ->
7026 let cmd = readcmd state.sr in
7027 act cmd;
7028 checkfds rest
7030 | fd :: rest when fd = state.wsfd ->
7031 Wsi.readresp fd;
7032 checkfds rest
7034 | fd :: rest ->
7035 let s = String.create 80 in
7036 let n = Unix.read fd s 0 80 in
7037 if conf.redirectstderr
7038 then (
7039 Buffer.add_substring state.errmsgs s 0 n;
7040 state.newerrmsgs <- true;
7041 state.redisplay <- true;
7043 else (
7044 prerr_string (String.sub s 0 n);
7045 flush stderr;
7047 checkfds rest
7049 checkfds l;
7050 let newdeadline =
7051 let deadline1 =
7052 if deadline = infinity
7053 then now () +. 0.01
7054 else deadline
7056 match state.autoscroll with
7057 | Some step when step != 0 -> deadline1
7058 | _ -> if state.ghyll == noghyll then infinity else deadline1
7060 loop newdeadline
7061 end;
7064 loop infinity;
7065 with Quit ->
7066 Config.save ();