56387856227210682af3474797a3ce1f35b8cb9c
[llpp.git] / main.ml
blob56387856227210682af3474797a3ce1f35b8cb9c
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 /. 64.0
1410 and th = float h /. 64.0 in
1411 let tx0 = float tilex /. 64.0
1412 and ty0 = float tiley /. 64.0 in
1413 let tx1 = tx0 +. tw
1414 and ty1 = ty0 +. th in
1415 GlDraw.begins `quads;
1416 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1417 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1418 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1419 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1420 GlDraw.ends ();
1422 Gl.disable `texture_2d;
1423 | None ->
1424 GlDraw.color (1.0, 1.0, 1.0);
1425 GlDraw.rect
1426 (float x, float y)
1427 (float (x+w), float (y+h));
1428 end;
1429 if w > 128 && h > fstate.fontsize + 10
1430 then (
1431 GlDraw.color (0.0, 0.0, 0.0);
1432 let c, r =
1433 if conf.verbose
1434 then (col*conf.tilew, row*conf.tileh)
1435 else col, row
1437 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1439 GlDraw.color color;
1441 itertiles l f
1444 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1446 let tilevisible1 l x y =
1447 let ax0 = l.pagex
1448 and ax1 = l.pagex + l.pagevw
1449 and ay0 = l.pagey
1450 and ay1 = l.pagey + l.pagevh in
1452 let bx0 = x
1453 and by0 = y in
1454 let bx1 = min (bx0 + conf.tilew) l.pagew
1455 and by1 = min (by0 + conf.tileh) l.pageh in
1457 let rx0 = max ax0 bx0
1458 and ry0 = max ay0 by0
1459 and rx1 = min ax1 bx1
1460 and ry1 = min ay1 by1 in
1462 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1463 nonemptyintersection
1466 let tilevisible layout n x y =
1467 let rec findpageinlayout m = function
1468 | l :: rest when l.pageno = n ->
1469 tilevisible1 l x y || (
1470 match conf.columns with
1471 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1472 | _ -> false
1474 | _ :: rest -> findpageinlayout 0 rest
1475 | [] -> false
1477 findpageinlayout 0 layout;
1480 let tileready l x y =
1481 tilevisible1 l x y &&
1482 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1485 let tilepage n p layout =
1486 let rec loop = function
1487 | l :: rest ->
1488 if l.pageno = n
1489 then
1490 let f col row _ _ _ _ _ _ =
1491 if state.currently = Idle
1492 then
1493 match gettileopaque l col row with
1494 | Some _ -> ()
1495 | None ->
1496 let x = col*conf.tilew
1497 and y = row*conf.tileh in
1498 let w =
1499 let w = l.pagew - x in
1500 min w conf.tilew
1502 let h =
1503 let h = l.pageh - y in
1504 min h conf.tileh
1506 wcmd "tile %s %d %d %d %d" p x y w h;
1507 state.currently <-
1508 Tiling (
1509 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1510 conf.tilew, conf.tileh
1513 itertiles l f;
1514 else
1515 loop rest
1517 | [] -> ()
1519 if nogeomcmds state.geomcmds
1520 then loop layout;
1523 let preloadlayout y =
1524 let y = if y < conf.winh then 0 else y - conf.winh in
1525 let h = conf.winh*3 in
1526 layout y h;
1529 let load pages =
1530 let rec loop pages =
1531 if state.currently != Idle
1532 then ()
1533 else
1534 match pages with
1535 | l :: rest ->
1536 begin match getopaque l.pageno with
1537 | None ->
1538 wcmd "page %d %d" l.pageno l.pagedimno;
1539 state.currently <- Loading (l, state.gen);
1540 | Some opaque ->
1541 tilepage l.pageno opaque pages;
1542 loop rest
1543 end;
1544 | _ -> ()
1546 if nogeomcmds state.geomcmds
1547 then loop pages
1550 let preload pages =
1551 load pages;
1552 if conf.preload && state.currently = Idle
1553 then load (preloadlayout state.y);
1556 let layoutready layout =
1557 let rec fold all ls =
1558 all && match ls with
1559 | l :: rest ->
1560 let seen = ref false in
1561 let allvisible = ref true in
1562 let foo col row _ _ _ _ _ _ =
1563 seen := true;
1564 allvisible := !allvisible &&
1565 begin match gettileopaque l col row with
1566 | Some _ -> true
1567 | None -> false
1570 itertiles l foo;
1571 fold (!seen && !allvisible) rest
1572 | [] -> true
1574 let alltilesvisible = fold true layout in
1575 alltilesvisible;
1578 let gotoy y =
1579 let y = bound y 0 state.maxy in
1580 let y, layout, proceed =
1581 match conf.maxwait with
1582 | Some time when state.ghyll == noghyll ->
1583 begin match state.throttle with
1584 | None ->
1585 let layout = layout y conf.winh in
1586 let ready = layoutready layout in
1587 if not ready
1588 then (
1589 load layout;
1590 state.throttle <- Some (layout, y, now ());
1592 else G.postRedisplay "gotoy showall (None)";
1593 y, layout, ready
1594 | Some (_, _, started) ->
1595 let dt = now () -. started in
1596 if dt > time
1597 then (
1598 state.throttle <- None;
1599 let layout = layout y conf.winh in
1600 load layout;
1601 G.postRedisplay "maxwait";
1602 y, layout, true
1604 else -1, [], false
1607 | _ ->
1608 let layout = layout y conf.winh in
1609 if true || layoutready layout
1610 then G.postRedisplay "gotoy ready";
1611 y, layout, true
1613 if proceed
1614 then (
1615 state.y <- y;
1616 state.layout <- layout;
1617 begin match state.mode with
1618 | LinkNav (Ltexact (pageno, linkno)) ->
1619 let rec loop = function
1620 | [] ->
1621 state.mode <- LinkNav (Ltgendir 0)
1622 | l :: _ when l.pageno = pageno ->
1623 begin match getopaque pageno with
1624 | None ->
1625 state.mode <- LinkNav (Ltgendir 0)
1626 | Some opaque ->
1627 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1628 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1629 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1630 then state.mode <- LinkNav (Ltgendir 0)
1632 | _ :: rest -> loop rest
1634 loop layout
1635 | _ -> ()
1636 end;
1637 begin match state.mode with
1638 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1639 if not (pagevisible layout pageno)
1640 then (
1641 match state.layout with
1642 | [] -> ()
1643 | l :: _ ->
1644 state.mode <- Birdseye (
1645 conf, leftx, l.pageno, hooverpageno, anchor
1648 | LinkNav (Ltgendir dir as lt) ->
1649 let linknav =
1650 let rec loop = function
1651 | [] -> lt
1652 | l :: rest ->
1653 match getopaque l.pageno with
1654 | None -> loop rest
1655 | Some opaque ->
1656 let link =
1657 let ld =
1658 if dir = 0
1659 then LDfirstvisible (l.pagex, l.pagey, dir)
1660 else (
1661 if dir > 0 then LDfirst else LDlast
1664 findlink opaque ld
1666 match link with
1667 | Lnotfound -> loop rest
1668 | Lfound n ->
1669 showlinktype (getlink opaque n);
1670 Ltexact (l.pageno, n)
1672 loop state.layout
1674 state.mode <- LinkNav linknav
1675 | _ -> ()
1676 end;
1677 preload layout;
1679 state.ghyll <- noghyll;
1680 if conf.updatecurs
1681 then (
1682 let mx, my = state.mpos in
1683 updateunder mx my;
1687 let conttiling pageno opaque =
1688 tilepage pageno opaque
1689 (if conf.preload then preloadlayout state.y else state.layout)
1692 let gotoy_and_clear_text y =
1693 if not conf.verbose then state.text <- "";
1694 gotoy y;
1697 let getanchor1 l =
1698 let top =
1699 let coloff = l.pagecol * l.pageh in
1700 float (l.pagey + coloff) /. float l.pageh
1702 let dtop =
1703 if l.pagedispy = 0
1704 then
1706 else
1707 if conf.presentation
1708 then float l.pagedispy /. float (calcips l.pageh)
1709 else float l.pagedispy /. float conf.interpagespace
1711 (l.pageno, top, dtop)
1714 let getanchor () =
1715 match state.layout with
1716 | l :: _ -> getanchor1 l
1717 | [] ->
1718 let n = page_of_y state.y in
1719 let y, h = getpageyh n in
1720 let dy = y - state.y in
1721 let dtop =
1722 if conf.presentation
1723 then
1724 let ips = calcips h in
1725 float (dy + ips) /. float ips
1726 else
1727 float dy /. float conf.interpagespace
1729 (n, 0.0, dtop)
1732 let getanchory (n, top, dtop) =
1733 let y, h = getpageyh n in
1734 if conf.presentation
1735 then
1736 let ips = calcips h in
1737 y + truncate (top*.float h -. dtop*.float ips) + ips;
1738 else
1739 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1742 let gotoanchor anchor =
1743 gotoy (getanchory anchor);
1746 let addnav () =
1747 cbput state.hists.nav (getanchor ());
1750 let getnav dir =
1751 let anchor = cbgetc state.hists.nav dir in
1752 getanchory anchor;
1755 let gotoghyll y =
1756 let scroll f n a b =
1757 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1758 let snake f a b =
1759 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1760 if f < a
1761 then s (float f /. float a)
1762 else (
1763 if f > b
1764 then 1.0 -. s ((float (f-b) /. float (n-b)))
1765 else 1.0
1768 snake f a b
1769 and summa f n a b =
1770 (* courtesy:
1771 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1772 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1773 let iv1 = iv f in
1774 let ins = float a *. iv1
1775 and outs = float (n-b) *. iv1 in
1776 let ones = b - a in
1777 ins +. outs +. float ones
1779 let rec set (_N, _A, _B) y sy =
1780 let sum = summa 1.0 _N _A _B in
1781 let dy = float (y - sy) in
1782 state.ghyll <- (
1783 let rec gf n y1 o =
1784 if n >= _N
1785 then state.ghyll <- noghyll
1786 else
1787 let go n =
1788 let s = scroll n _N _A _B in
1789 let y1 = y1 +. ((s *. dy) /. sum) in
1790 gotoy_and_clear_text (truncate y1);
1791 state.ghyll <- gf (n+1) y1;
1793 match o with
1794 | None -> go n
1795 | Some y' -> set (_N/2, 1, 1) y' state.y
1797 gf 0 (float state.y)
1800 match conf.ghyllscroll with
1801 | None ->
1802 gotoy_and_clear_text y
1803 | Some nab ->
1804 if state.ghyll == noghyll
1805 then set nab y state.y
1806 else state.ghyll (Some y)
1809 let gotopage n top =
1810 let y, h = getpageyh n in
1811 let y = y + (truncate (top *. float h)) in
1812 gotoghyll y
1815 let gotopage1 n top =
1816 let y = getpagey n in
1817 let y = y + top in
1818 gotoghyll y
1821 let invalidate s f =
1822 state.layout <- [];
1823 state.pdims <- [];
1824 state.rects <- [];
1825 state.rects1 <- [];
1826 match state.geomcmds with
1827 | ps, [] when String.length ps = 0 ->
1828 f ();
1829 state.geomcmds <- s, [];
1831 | ps, [] ->
1832 state.geomcmds <- ps, [s, f];
1834 | ps, (s', _) :: rest when s' = s ->
1835 state.geomcmds <- ps, ((s, f) :: rest);
1837 | ps, cmds ->
1838 state.geomcmds <- ps, ((s, f) :: cmds);
1841 let opendoc path password =
1842 state.path <- path;
1843 state.password <- password;
1844 state.gen <- state.gen + 1;
1845 state.docinfo <- [];
1847 setaalevel conf.aalevel;
1848 Wsi.settitle ("llpp " ^ Filename.basename path);
1849 wcmd "open %s\000%s\000" path password;
1850 invalidate "reqlayout"
1851 (fun () ->
1852 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1855 let scalecolor c =
1856 let c = c *. conf.colorscale in
1857 (c, c, c);
1860 let scalecolor2 (r, g, b) =
1861 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1864 let docolumns = function
1865 | Csingle _ ->
1866 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1867 let rec loop pageno pdimno pdim y ph pdims =
1868 if pageno = state.pagecount
1869 then ()
1870 else
1871 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1872 match pdims with
1873 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1874 pdimno+1, pdim, rest
1875 | _ ->
1876 pdimno, pdim, pdims
1878 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1879 let y = y +
1880 (if conf.presentation
1881 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1882 else (if pageno = 0 then 0 else conf.interpagespace)
1885 a.(pageno) <- (pdimno, x, y, pdim);
1886 loop (pageno+1) pdimno pdim (y + h) h pdims
1888 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1889 conf.columns <- Csingle a;
1891 | Cmulti ((columns, coverA, coverB), _) ->
1892 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1893 let rec loop pageno pdimno pdim x y rowh pdims =
1894 let rec fixrow m = if m = pageno then () else
1895 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1896 if h < rowh
1897 then (
1898 let y = y + (rowh - h) / 2 in
1899 a.(m) <- (pdimno, x, y, pdim);
1901 fixrow (m+1)
1903 if pageno = state.pagecount
1904 then fixrow (((pageno - 1) / columns) * columns)
1905 else
1906 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1907 match pdims with
1908 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1909 pdimno+1, pdim, rest
1910 | _ ->
1911 pdimno, pdim, pdims
1913 let x, y, rowh' =
1914 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1915 then (
1916 let x = (conf.winw - state.scrollw - w) / 2 in
1917 let ips =
1918 if conf.presentation then calcips h else conf.interpagespace in
1919 x, y + ips + rowh, h
1921 else (
1922 if (pageno - coverA) mod columns = 0
1923 then (
1924 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1925 let y =
1926 if conf.presentation
1927 then
1928 let ips = calcips h in
1929 y + (if pageno = 0 then 0 else calcips rowh + ips)
1930 else
1931 y + (if pageno = 0 then 0 else conf.interpagespace)
1933 x, y + rowh, h
1935 else x, y, max rowh h
1938 let y =
1939 if pageno > 1 && (pageno - coverA) mod columns = 0
1940 then (
1941 let y =
1942 if pageno = columns && conf.presentation
1943 then (
1944 let ips = calcips rowh in
1945 for i = 0 to pred columns
1947 let (pdimno, x, y, pdim) = a.(i) in
1948 a.(i) <- (pdimno, x, y+ips, pdim)
1949 done;
1950 y+ips;
1952 else y
1954 fixrow (pageno - columns);
1957 else y
1959 a.(pageno) <- (pdimno, x, y, pdim);
1960 let x = x + w + xoff*2 + conf.interpagespace in
1961 loop (pageno+1) pdimno pdim x y rowh' pdims
1963 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1964 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1966 | Csplit (c, _) ->
1967 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1968 let rec loop pageno pdimno pdim y pdims =
1969 if pageno = state.pagecount
1970 then ()
1971 else
1972 let pdimno, ((_, w, h, _) as pdim), pdims =
1973 match pdims with
1974 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1975 pdimno+1, pdim, rest
1976 | _ ->
1977 pdimno, pdim, pdims
1979 let cw = w / c in
1980 let rec loop1 n x y =
1981 if n = c then y else (
1982 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1983 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1986 let y = loop1 0 0 y in
1987 loop (pageno+1) pdimno pdim y pdims
1989 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1990 conf.columns <- Csplit (c, a);
1993 let represent () =
1994 docolumns conf.columns;
1995 state.maxy <- calcheight ();
1996 state.hscrollh <-
1997 if state.w <= conf.winw - state.scrollw
1998 then 0
1999 else state.scrollw
2001 match state.mode with
2002 | Birdseye (_, _, pageno, _, _) ->
2003 let y, h = getpageyh pageno in
2004 let top = (conf.winh - h) / 2 in
2005 gotoy (max 0 (y - top))
2006 | _ -> gotoanchor state.anchor
2009 let reshape w h =
2010 GlDraw.viewport 0 0 w h;
2011 let firsttime = state.geomcmds == firstgeomcmds in
2012 if not firsttime && nogeomcmds state.geomcmds
2013 then state.anchor <- getanchor ();
2015 conf.winw <- w;
2016 let w = truncate (float w *. conf.zoom) - state.scrollw in
2017 let w = max w 2 in
2018 conf.winh <- h;
2019 setfontsize fstate.fontsize;
2020 GlMat.mode `modelview;
2021 GlMat.load_identity ();
2023 GlMat.mode `projection;
2024 GlMat.load_identity ();
2025 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2026 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2027 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2029 let relx =
2030 if conf.zoom <= 1.0
2031 then 0.0
2032 else float state.x /. float state.w
2034 invalidate "geometry"
2035 (fun () ->
2036 state.w <- w;
2037 if not firsttime
2038 then state.x <- truncate (relx *. float w);
2039 let w =
2040 match conf.columns with
2041 | Csingle _ -> w
2042 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2043 | Csplit (c, _) -> w * c
2045 wcmd "geometry %d %d" w h);
2048 let enttext () =
2049 let len = String.length state.text in
2050 let drawstring s =
2051 let hscrollh =
2052 match state.mode with
2053 | Textentry _
2054 | View ->
2055 let h, _, _ = state.uioh#scrollpw in
2057 | _ -> 0
2059 let rect x w =
2060 GlDraw.rect
2061 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2062 (x+.w, float (conf.winh - hscrollh))
2065 let w = float (conf.winw - state.scrollw - 1) in
2066 if state.progress >= 0.0 && state.progress < 1.0
2067 then (
2068 GlDraw.color (0.3, 0.3, 0.3);
2069 let w1 = w *. state.progress in
2070 rect 0.0 w1;
2071 GlDraw.color (0.0, 0.0, 0.0);
2072 rect w1 (w-.w1)
2074 else (
2075 GlDraw.color (0.0, 0.0, 0.0);
2076 rect 0.0 w;
2079 GlDraw.color (1.0, 1.0, 1.0);
2080 drawstring fstate.fontsize
2081 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2083 let s =
2084 match state.mode with
2085 | Textentry ((prefix, text, _, _, _, _), _) ->
2086 let s =
2087 if len > 0
2088 then
2089 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2090 else
2091 Printf.sprintf "%s%s_" prefix text
2095 | _ -> state.text
2097 let s =
2098 if state.newerrmsgs
2099 then (
2100 if not (istextentry state.mode)
2101 then
2102 let s1 = "(press 'e' to review error messasges)" in
2103 if String.length s > 0 then s ^ " " ^ s1 else s1
2104 else s
2106 else s
2108 if String.length s > 0
2109 then drawstring s
2112 let gctiles () =
2113 let len = Queue.length state.tilelru in
2114 let layout = lazy (
2115 match state.throttle with
2116 | None ->
2117 if conf.preload
2118 then preloadlayout state.y
2119 else state.layout
2120 | Some (layout, _, _) ->
2121 layout
2122 ) in
2123 let rec loop qpos =
2124 if state.memused <= conf.memlimit
2125 then ()
2126 else (
2127 if qpos < len
2128 then
2129 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2130 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2131 let (_, pw, ph, _) = getpagedim n in
2133 gen = state.gen
2134 && colorspace = conf.colorspace
2135 && angle = conf.angle
2136 && pagew = pw
2137 && pageh = ph
2138 && (
2139 let x = col*conf.tilew
2140 and y = row*conf.tileh in
2141 tilevisible (Lazy.force_val layout) n x y
2143 then Queue.push lruitem state.tilelru
2144 else (
2145 wcmd "freetile %s" p;
2146 state.memused <- state.memused - s;
2147 state.uioh#infochanged Memused;
2148 Hashtbl.remove state.tilemap k;
2150 loop (qpos+1)
2153 loop 0
2156 let flushtiles () =
2157 Queue.iter (fun (k, p, s) ->
2158 wcmd "freetile %s" p;
2159 state.memused <- state.memused - s;
2160 state.uioh#infochanged Memused;
2161 Hashtbl.remove state.tilemap k;
2162 ) state.tilelru;
2163 Queue.clear state.tilelru;
2164 load state.layout;
2167 let logcurrently = function
2168 | Idle -> dolog "Idle"
2169 | Loading (l, gen) ->
2170 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2171 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2172 dolog
2173 "Tiling %d[%d,%d] page=%s cs=%s angle"
2174 l.pageno col row pageopaque
2175 (colorspace_to_string colorspace)
2177 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2178 angle gen conf.angle state.gen
2179 tilew tileh
2180 conf.tilew conf.tileh
2182 | Outlining _ ->
2183 dolog "outlining"
2186 let act cmds =
2187 (* dolog "%S" cmds; *)
2188 let op, args =
2189 let spacepos =
2190 try String.index cmds ' '
2191 with Not_found -> -1
2193 if spacepos = -1
2194 then cmds, ""
2195 else
2196 let l = String.length cmds in
2197 let op = String.sub cmds 0 spacepos in
2198 op, begin
2199 if l - spacepos < 2 then ""
2200 else String.sub cmds (spacepos+1) (l-spacepos-1)
2203 match op with
2204 | "clear" ->
2205 state.uioh#infochanged Pdim;
2206 state.pdims <- [];
2208 | "clearrects" ->
2209 state.rects <- state.rects1;
2210 G.postRedisplay "clearrects";
2212 | "continue" ->
2213 let n =
2214 try Scanf.sscanf args "%u" (fun n -> n)
2215 with exn ->
2216 dolog "error processing 'continue' %S: %s"
2217 cmds (Printexc.to_string exn);
2218 exit 1;
2220 state.pagecount <- n;
2221 begin match state.currently with
2222 | Outlining l ->
2223 state.currently <- Idle;
2224 state.outlines <- Array.of_list (List.rev l)
2225 | _ -> ()
2226 end;
2228 let cur, cmds = state.geomcmds in
2229 if String.length cur = 0
2230 then failwith "umpossible";
2232 begin match List.rev cmds with
2233 | [] ->
2234 state.geomcmds <- "", [];
2235 represent ();
2236 | (s, f) :: rest ->
2237 f ();
2238 state.geomcmds <- s, List.rev rest;
2239 end;
2240 if conf.maxwait = None
2241 then G.postRedisplay "continue";
2243 | "title" ->
2244 Wsi.settitle args
2246 | "msg" ->
2247 showtext ' ' args
2249 | "vmsg" ->
2250 if conf.verbose
2251 then showtext ' ' args
2253 | "progress" ->
2254 let progress, text =
2256 Scanf.sscanf args "%f %n"
2257 (fun f pos ->
2258 f, String.sub args pos (String.length args - pos))
2259 with exn ->
2260 dolog "error processing 'progress' %S: %s"
2261 cmds (Printexc.to_string exn);
2262 exit 1;
2264 state.text <- text;
2265 state.progress <- progress;
2266 G.postRedisplay "progress"
2268 | "firstmatch" ->
2269 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2271 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2272 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2273 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2274 with exn ->
2275 dolog "error processing 'firstmatch' %S: %s"
2276 cmds (Printexc.to_string exn);
2277 exit 1;
2279 let y = (getpagey pageno) + truncate y0 in
2280 addnav ();
2281 gotoy y;
2282 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2284 | "match" ->
2285 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2287 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2288 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2289 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2290 with exn ->
2291 dolog "error processing 'match' %S: %s"
2292 cmds (Printexc.to_string exn);
2293 exit 1;
2295 state.rects1 <-
2296 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2298 | "page" ->
2299 let pageopaque, t =
2301 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2302 with exn ->
2303 dolog "error processing 'page' %S: %s"
2304 cmds (Printexc.to_string exn);
2305 exit 1;
2307 begin match state.currently with
2308 | Loading (l, gen) ->
2309 vlog "page %d took %f sec" l.pageno t;
2310 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2311 begin match state.throttle with
2312 | None ->
2313 let preloadedpages =
2314 if conf.preload
2315 then preloadlayout state.y
2316 else state.layout
2318 let evict () =
2319 let module IntSet =
2320 Set.Make (struct type t = int let compare = (-) end) in
2321 let set =
2322 List.fold_left (fun s l -> IntSet.add l.pageno s)
2323 IntSet.empty preloadedpages
2325 let evictedpages =
2326 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2327 if not (IntSet.mem pageno set)
2328 then (
2329 wcmd "freepage %s" opaque;
2330 key :: accu
2332 else accu
2333 ) state.pagemap []
2335 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2337 evict ();
2338 state.currently <- Idle;
2339 if gen = state.gen
2340 then (
2341 tilepage l.pageno pageopaque state.layout;
2342 load state.layout;
2343 load preloadedpages;
2344 if pagevisible state.layout l.pageno
2345 && layoutready state.layout
2346 then G.postRedisplay "page";
2349 | Some (layout, _, _) ->
2350 state.currently <- Idle;
2351 tilepage l.pageno pageopaque layout;
2352 load state.layout
2353 end;
2355 | _ ->
2356 dolog "Inconsistent loading state";
2357 logcurrently state.currently;
2358 exit 1
2361 | "tile" ->
2362 let (x, y, opaque, size, t) =
2364 Scanf.sscanf args "%u %u %s %u %f"
2365 (fun x y p size t -> (x, y, p, size, t))
2366 with exn ->
2367 dolog "error processing 'tile' %S: %s"
2368 cmds (Printexc.to_string exn);
2369 exit 1;
2371 begin match state.currently with
2372 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2373 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2375 if tilew != conf.tilew || tileh != conf.tileh
2376 then (
2377 wcmd "freetile %s" opaque;
2378 state.currently <- Idle;
2379 load state.layout;
2381 else (
2382 puttileopaque l col row gen cs angle opaque size t;
2383 state.memused <- state.memused + size;
2384 state.uioh#infochanged Memused;
2385 gctiles ();
2386 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2387 opaque, size) state.tilelru;
2389 let layout =
2390 match state.throttle with
2391 | None -> state.layout
2392 | Some (layout, _, _) -> layout
2395 state.currently <- Idle;
2396 if gen = state.gen
2397 && conf.colorspace = cs
2398 && conf.angle = angle
2399 && tilevisible layout l.pageno x y
2400 then conttiling l.pageno pageopaque;
2402 begin match state.throttle with
2403 | None ->
2404 preload state.layout;
2405 if gen = state.gen
2406 && conf.colorspace = cs
2407 && conf.angle = angle
2408 && tilevisible state.layout l.pageno x y
2409 then G.postRedisplay "tile nothrottle";
2411 | Some (layout, y, _) ->
2412 let ready = layoutready layout in
2413 if ready
2414 then (
2415 state.y <- y;
2416 state.layout <- layout;
2417 state.throttle <- None;
2418 G.postRedisplay "throttle";
2420 else load layout;
2421 end;
2424 | _ ->
2425 dolog "Inconsistent tiling state";
2426 logcurrently state.currently;
2427 exit 1
2430 | "pdim" ->
2431 let pdim =
2433 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2434 with exn ->
2435 dolog "error processing 'pdim' %S: %s"
2436 cmds (Printexc.to_string exn);
2437 exit 1;
2439 state.uioh#infochanged Pdim;
2440 state.pdims <- pdim :: state.pdims
2442 | "o" ->
2443 let (l, n, t, h, pos) =
2445 Scanf.sscanf args "%u %u %d %u %n"
2446 (fun l n t h pos -> l, n, t, h, pos)
2447 with exn ->
2448 dolog "error processing 'o' %S: %s"
2449 cmds (Printexc.to_string exn);
2450 exit 1;
2452 let s = String.sub args pos (String.length args - pos) in
2453 let outline = (s, l, (n, float t /. float h, 0.0)) in
2454 begin match state.currently with
2455 | Outlining outlines ->
2456 state.currently <- Outlining (outline :: outlines)
2457 | Idle ->
2458 state.currently <- Outlining [outline]
2459 | currently ->
2460 dolog "invalid outlining state";
2461 logcurrently currently
2464 | "info" ->
2465 state.docinfo <- (1, args) :: state.docinfo
2467 | "infoend" ->
2468 state.uioh#infochanged Docinfo;
2469 state.docinfo <- List.rev state.docinfo
2471 | _ ->
2472 dolog "unknown cmd `%S'" cmds
2475 let onhist cb =
2476 let rc = cb.rc in
2477 let action = function
2478 | HCprev -> cbget cb ~-1
2479 | HCnext -> cbget cb 1
2480 | HCfirst -> cbget cb ~-(cb.rc)
2481 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2482 and cancel () = cb.rc <- rc
2483 in (action, cancel)
2486 let search pattern forward =
2487 if String.length pattern > 0
2488 then
2489 let pn, py =
2490 match state.layout with
2491 | [] -> 0, 0
2492 | l :: _ ->
2493 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2495 wcmd "search %d %d %d %d,%s\000"
2496 (btod conf.icase) pn py (btod forward) pattern;
2499 let intentry text key =
2500 let c =
2501 if key >= 32 && key < 127
2502 then Char.chr key
2503 else '\000'
2505 match c with
2506 | '0' .. '9' ->
2507 let text = addchar text c in
2508 TEcont text
2510 | _ ->
2511 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2512 TEcont text
2515 let linknentry text key =
2516 let c =
2517 if key >= 32 && key < 127
2518 then Char.chr key
2519 else '\000'
2521 match c with
2522 | 'a' .. 'z' ->
2523 let text = addchar text c in
2524 TEcont text
2526 | _ ->
2527 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2528 TEcont text
2531 let linkndone f s =
2532 if String.length s > 0
2533 then (
2534 let n =
2535 let l = String.length s in
2536 let rec loop pos n = if pos = l then n else
2537 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2538 loop (pos+1) (n*26 + m)
2539 in loop 0 0
2541 let rec loop n = function
2542 | [] -> ()
2543 | l :: rest ->
2544 match getopaque l.pageno with
2545 | None -> loop n rest
2546 | Some opaque ->
2547 let m = getlinkcount opaque in
2548 if n < m
2549 then (
2550 let under = getlink opaque n in
2551 f under
2553 else loop (n-m) rest
2555 loop n state.layout;
2559 let textentry text key =
2560 if key land 0xff00 = 0xff00
2561 then TEcont text
2562 else TEcont (text ^ Wsi.toutf8 key)
2565 let reqlayout angle proportional =
2566 match state.throttle with
2567 | None ->
2568 if nogeomcmds state.geomcmds
2569 then state.anchor <- getanchor ();
2570 conf.angle <- angle mod 360;
2571 if conf.angle != 0
2572 then (
2573 match state.mode with
2574 | LinkNav _ -> state.mode <- View
2575 | _ -> ()
2577 conf.proportional <- proportional;
2578 invalidate "reqlayout"
2579 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2580 | _ -> ()
2583 let settrim trimmargins trimfuzz =
2584 if nogeomcmds state.geomcmds
2585 then state.anchor <- getanchor ();
2586 conf.trimmargins <- trimmargins;
2587 conf.trimfuzz <- trimfuzz;
2588 let x0, y0, x1, y1 = trimfuzz in
2589 invalidate "settrim"
2590 (fun () ->
2591 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2592 Hashtbl.iter (fun _ opaque ->
2593 wcmd "freepage %s" opaque;
2594 ) state.pagemap;
2595 Hashtbl.clear state.pagemap;
2598 let setzoom zoom =
2599 match state.throttle with
2600 | None ->
2601 let zoom = max 0.01 zoom in
2602 if zoom <> conf.zoom
2603 then (
2604 state.prevzoom <- conf.zoom;
2605 conf.zoom <- zoom;
2606 reshape conf.winw conf.winh;
2607 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2610 | Some (layout, y, started) ->
2611 let time =
2612 match conf.maxwait with
2613 | None -> 0.0
2614 | Some t -> t
2616 let dt = now () -. started in
2617 if dt > time
2618 then (
2619 state.y <- y;
2620 load layout;
2624 let setcolumns mode columns coverA coverB =
2625 state.prevcolumns <- Some (conf.columns, conf.zoom);
2626 if columns < 0
2627 then (
2628 if isbirdseye mode
2629 then showtext '!' "split mode doesn't work in bird's eye"
2630 else (
2631 conf.columns <- Csplit (-columns, [||]);
2632 state.x <- 0;
2633 conf.zoom <- 1.0;
2636 else (
2637 if columns < 2
2638 then (
2639 conf.columns <- Csingle [||];
2640 state.x <- 0;
2641 setzoom 1.0;
2643 else (
2644 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2645 conf.zoom <- 1.0;
2648 reshape conf.winw conf.winh;
2651 let enterbirdseye () =
2652 let zoom = float conf.thumbw /. float conf.winw in
2653 let birdseyepageno =
2654 let cy = conf.winh / 2 in
2655 let fold = function
2656 | [] -> 0
2657 | l :: rest ->
2658 let rec fold best = function
2659 | [] -> best.pageno
2660 | l :: rest ->
2661 let d = cy - (l.pagedispy + l.pagevh/2)
2662 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2663 if abs d < abs dbest
2664 then fold l rest
2665 else best.pageno
2666 in fold l rest
2668 fold state.layout
2670 state.mode <- Birdseye (
2671 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2673 conf.zoom <- zoom;
2674 conf.presentation <- false;
2675 conf.interpagespace <- 10;
2676 conf.hlinks <- false;
2677 state.x <- 0;
2678 state.mstate <- Mnone;
2679 conf.maxwait <- None;
2680 conf.columns <- (
2681 match conf.beyecolumns with
2682 | Some c ->
2683 conf.zoom <- 1.0;
2684 Cmulti ((c, 0, 0), [||])
2685 | None -> Csingle [||]
2687 Wsi.setcursor Wsi.CURSOR_INHERIT;
2688 if conf.verbose
2689 then
2690 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2691 (100.0*.zoom)
2692 else
2693 state.text <- ""
2695 reshape conf.winw conf.winh;
2698 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2699 state.mode <- View;
2700 conf.zoom <- c.zoom;
2701 conf.presentation <- c.presentation;
2702 conf.interpagespace <- c.interpagespace;
2703 conf.maxwait <- c.maxwait;
2704 conf.hlinks <- c.hlinks;
2705 conf.beyecolumns <- (
2706 match conf.columns with
2707 | Cmulti ((c, _, _), _) -> Some c
2708 | Csingle _ -> None
2709 | Csplit _ -> failwith "leaving bird's eye split mode"
2711 conf.columns <- (
2712 match c.columns with
2713 | Cmulti (c, _) -> Cmulti (c, [||])
2714 | Csingle _ -> Csingle [||]
2715 | Csplit (c, _) -> Csplit (c, [||])
2717 state.x <- leftx;
2718 if conf.verbose
2719 then
2720 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2721 (100.0*.conf.zoom)
2723 reshape conf.winw conf.winh;
2724 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2727 let togglebirdseye () =
2728 match state.mode with
2729 | Birdseye vals -> leavebirdseye vals true
2730 | View -> enterbirdseye ()
2731 | _ -> ()
2734 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2735 let pageno = max 0 (pageno - incr) in
2736 let rec loop = function
2737 | [] -> gotopage1 pageno 0
2738 | l :: _ when l.pageno = pageno ->
2739 if l.pagedispy >= 0 && l.pagey = 0
2740 then G.postRedisplay "upbirdseye"
2741 else gotopage1 pageno 0
2742 | _ :: rest -> loop rest
2744 loop state.layout;
2745 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2748 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2749 let pageno = min (state.pagecount - 1) (pageno + incr) in
2750 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2751 let rec loop = function
2752 | [] ->
2753 let y, h = getpageyh pageno in
2754 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2755 gotoy (clamp dy)
2756 | l :: _ when l.pageno = pageno ->
2757 if l.pagevh != l.pageh
2758 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2759 else G.postRedisplay "downbirdseye"
2760 | _ :: rest -> loop rest
2762 loop state.layout
2765 let optentry mode _ key =
2766 let btos b = if b then "on" else "off" in
2767 if key >= 32 && key < 127
2768 then
2769 let c = Char.chr key in
2770 match c with
2771 | 's' ->
2772 let ondone s =
2773 try conf.scrollstep <- int_of_string s with exc ->
2774 state.text <- Printf.sprintf "bad integer `%s': %s"
2775 s (Printexc.to_string exc)
2777 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2779 | 'A' ->
2780 let ondone s =
2782 conf.autoscrollstep <- int_of_string s;
2783 if state.autoscroll <> None
2784 then state.autoscroll <- Some conf.autoscrollstep
2785 with exc ->
2786 state.text <- Printf.sprintf "bad integer `%s': %s"
2787 s (Printexc.to_string exc)
2789 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2791 | 'C' ->
2792 let ondone s =
2794 let n, a, b = multicolumns_of_string s in
2795 setcolumns mode n a b;
2796 with exc ->
2797 state.text <- Printf.sprintf "bad columns `%s': %s"
2798 s (Printexc.to_string exc)
2800 TEswitch ("columns: ", "", None, textentry, ondone, true)
2802 | 'Z' ->
2803 let ondone s =
2805 let zoom = float (int_of_string s) /. 100.0 in
2806 setzoom zoom
2807 with exc ->
2808 state.text <- Printf.sprintf "bad integer `%s': %s"
2809 s (Printexc.to_string exc)
2811 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2813 | 't' ->
2814 let ondone s =
2816 conf.thumbw <- bound (int_of_string s) 2 4096;
2817 state.text <-
2818 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2819 begin match mode with
2820 | Birdseye beye ->
2821 leavebirdseye beye false;
2822 enterbirdseye ();
2823 | _ -> ();
2825 with exc ->
2826 state.text <- Printf.sprintf "bad integer `%s': %s"
2827 s (Printexc.to_string exc)
2829 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2831 | 'R' ->
2832 let ondone s =
2833 match try
2834 Some (int_of_string s)
2835 with exc ->
2836 state.text <- Printf.sprintf "bad integer `%s': %s"
2837 s (Printexc.to_string exc);
2838 None
2839 with
2840 | Some angle -> reqlayout angle conf.proportional
2841 | None -> ()
2843 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2845 | 'i' ->
2846 conf.icase <- not conf.icase;
2847 TEdone ("case insensitive search " ^ (btos conf.icase))
2849 | 'p' ->
2850 conf.preload <- not conf.preload;
2851 gotoy state.y;
2852 TEdone ("preload " ^ (btos conf.preload))
2854 | 'v' ->
2855 conf.verbose <- not conf.verbose;
2856 TEdone ("verbose " ^ (btos conf.verbose))
2858 | 'd' ->
2859 conf.debug <- not conf.debug;
2860 TEdone ("debug " ^ (btos conf.debug))
2862 | 'h' ->
2863 conf.maxhfit <- not conf.maxhfit;
2864 state.maxy <- calcheight ();
2865 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2867 | 'c' ->
2868 conf.crophack <- not conf.crophack;
2869 TEdone ("crophack " ^ btos conf.crophack)
2871 | 'a' ->
2872 let s =
2873 match conf.maxwait with
2874 | None ->
2875 conf.maxwait <- Some infinity;
2876 "always wait for page to complete"
2877 | Some _ ->
2878 conf.maxwait <- None;
2879 "show placeholder if page is not ready"
2881 TEdone s
2883 | 'f' ->
2884 conf.underinfo <- not conf.underinfo;
2885 TEdone ("underinfo " ^ btos conf.underinfo)
2887 | 'P' ->
2888 conf.savebmarks <- not conf.savebmarks;
2889 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2891 | 'S' ->
2892 let ondone s =
2894 let pageno, py =
2895 match state.layout with
2896 | [] -> 0, 0
2897 | l :: _ ->
2898 l.pageno, l.pagey
2900 conf.interpagespace <- int_of_string s;
2901 docolumns conf.columns;
2902 state.maxy <- calcheight ();
2903 let y = getpagey pageno in
2904 gotoy (y + py)
2905 with exc ->
2906 state.text <- Printf.sprintf "bad integer `%s': %s"
2907 s (Printexc.to_string exc)
2909 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2911 | 'l' ->
2912 reqlayout conf.angle (not conf.proportional);
2913 TEdone ("proportional display " ^ btos conf.proportional)
2915 | 'T' ->
2916 settrim (not conf.trimmargins) conf.trimfuzz;
2917 TEdone ("trim margins " ^ btos conf.trimmargins)
2919 | 'I' ->
2920 conf.invert <- not conf.invert;
2921 TEdone ("invert colors " ^ btos conf.invert)
2923 | 'x' ->
2924 let ondone s =
2925 cbput state.hists.sel s;
2926 conf.selcmd <- s;
2928 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2929 textentry, ondone, true)
2931 | _ ->
2932 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2933 TEstop
2934 else
2935 TEcont state.text
2938 class type lvsource = object
2939 method getitemcount : int
2940 method getitem : int -> (string * int)
2941 method hasaction : int -> bool
2942 method exit :
2943 uioh:uioh ->
2944 cancel:bool ->
2945 active:int ->
2946 first:int ->
2947 pan:int ->
2948 qsearch:string ->
2949 uioh option
2950 method getactive : int
2951 method getfirst : int
2952 method getqsearch : string
2953 method setqsearch : string -> unit
2954 method getpan : int
2955 end;;
2957 class virtual lvsourcebase = object
2958 val mutable m_active = 0
2959 val mutable m_first = 0
2960 val mutable m_qsearch = ""
2961 val mutable m_pan = 0
2962 method getactive = m_active
2963 method getfirst = m_first
2964 method getqsearch = m_qsearch
2965 method getpan = m_pan
2966 method setqsearch s = m_qsearch <- s
2967 end;;
2969 let withoutlastutf8 s =
2970 let len = String.length s in
2971 if len = 0
2972 then s
2973 else
2974 let rec find pos =
2975 if pos = 0
2976 then pos
2977 else
2978 let b = Char.code s.[pos] in
2979 if b land 0b110000 = 0b11000000
2980 then find (pos-1)
2981 else pos-1
2983 let first =
2984 if Char.code s.[len-1] land 0x80 = 0
2985 then len-1
2986 else find (len-1)
2988 String.sub s 0 first;
2991 let textentrykeyboard
2992 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2993 let enttext te =
2994 state.mode <- Textentry (te, onleave);
2995 state.text <- "";
2996 enttext ();
2997 G.postRedisplay "textentrykeyboard enttext";
2999 let histaction cmd =
3000 match opthist with
3001 | None -> ()
3002 | Some (action, _) ->
3003 state.mode <- Textentry (
3004 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3006 G.postRedisplay "textentry histaction"
3008 match key with
3009 | 0xff08 -> (* backspace *)
3010 let s = withoutlastutf8 text in
3011 let len = String.length s in
3012 if cancelonempty && len = 0
3013 then (
3014 onleave Cancel;
3015 G.postRedisplay "textentrykeyboard after cancel";
3017 else (
3018 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3021 | 0xff0d ->
3022 ondone text;
3023 onleave Confirm;
3024 G.postRedisplay "textentrykeyboard after confirm"
3026 | 0xff52 -> histaction HCprev
3027 | 0xff54 -> histaction HCnext
3028 | 0xff50 -> histaction HCfirst
3029 | 0xff57 -> histaction HClast
3031 | 0xff1b -> (* escape*)
3032 if String.length text = 0
3033 then (
3034 begin match opthist with
3035 | None -> ()
3036 | Some (_, onhistcancel) -> onhistcancel ()
3037 end;
3038 onleave Cancel;
3039 state.text <- "";
3040 G.postRedisplay "textentrykeyboard after cancel2"
3042 else (
3043 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3046 | 0xff9f | 0xffff -> () (* delete *)
3048 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3049 begin match onkey text key with
3050 | TEdone text ->
3051 ondone text;
3052 onleave Confirm;
3053 G.postRedisplay "textentrykeyboard after confirm2";
3055 | TEcont text ->
3056 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3058 | TEstop ->
3059 onleave Cancel;
3060 G.postRedisplay "textentrykeyboard after cancel3"
3062 | TEswitch te ->
3063 state.mode <- Textentry (te, onleave);
3064 G.postRedisplay "textentrykeyboard switch";
3065 end;
3067 | _ ->
3068 vlog "unhandled key %s" (Wsi.keyname key)
3071 let firstof first active =
3072 if first > active || abs (first - active) > fstate.maxrows - 1
3073 then max 0 (active - (fstate.maxrows/2))
3074 else first
3077 let calcfirst first active =
3078 if active > first
3079 then
3080 let rows = active - first in
3081 if rows > fstate.maxrows then active - fstate.maxrows else first
3082 else active
3085 let scrollph y maxy =
3086 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3087 let sh = float conf.winh /. sh in
3088 let sh = max sh (float conf.scrollh) in
3090 let percent =
3091 if y = state.maxy
3092 then 1.0
3093 else float y /. float maxy
3095 let position = (float conf.winh -. sh) *. percent in
3097 let position =
3098 if position +. sh > float conf.winh
3099 then float conf.winh -. sh
3100 else position
3102 position, sh;
3105 let coe s = (s :> uioh);;
3107 class listview ~(source:lvsource) ~trusted ~modehash =
3108 object (self)
3109 val m_pan = source#getpan
3110 val m_first = source#getfirst
3111 val m_active = source#getactive
3112 val m_qsearch = source#getqsearch
3113 val m_prev_uioh = state.uioh
3115 method private elemunder y =
3116 let n = y / (fstate.fontsize+1) in
3117 if m_first + n < source#getitemcount
3118 then (
3119 if source#hasaction (m_first + n)
3120 then Some (m_first + n)
3121 else None
3123 else None
3125 method display =
3126 Gl.enable `blend;
3127 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3128 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3129 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3130 GlDraw.color (1., 1., 1.);
3131 Gl.enable `texture_2d;
3132 let fs = fstate.fontsize in
3133 let nfs = fs + 1 in
3134 let ww = fstate.wwidth in
3135 let tabw = 30.0*.ww in
3136 let itemcount = source#getitemcount in
3137 let rec loop row =
3138 if (row - m_first) > fstate.maxrows
3139 then ()
3140 else (
3141 if row >= 0 && row < itemcount
3142 then (
3143 let (s, level) = source#getitem row in
3144 let y = (row - m_first) * nfs in
3145 let x = 5.0 +. float (level + m_pan) *. ww in
3146 if row = m_active
3147 then (
3148 Gl.disable `texture_2d;
3149 GlDraw.polygon_mode `both `line;
3150 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3151 GlDraw.rect (1., float (y + 1))
3152 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3153 GlDraw.polygon_mode `both `fill;
3154 GlDraw.color (1., 1., 1.);
3155 Gl.enable `texture_2d;
3158 let drawtabularstring s =
3159 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3160 if trusted
3161 then
3162 let tabpos = try String.index s '\t' with Not_found -> -1 in
3163 if tabpos > 0
3164 then
3165 let len = String.length s - tabpos - 1 in
3166 let s1 = String.sub s 0 tabpos
3167 and s2 = String.sub s (tabpos + 1) len in
3168 let nx = drawstr x s1 in
3169 let sw = nx -. x in
3170 let x = x +. (max tabw sw) in
3171 drawstr x s2
3172 else
3173 drawstr x s
3174 else
3175 drawstr x s
3177 let _ = drawtabularstring s in
3178 loop (row+1)
3182 loop m_first;
3183 Gl.disable `blend;
3184 Gl.disable `texture_2d;
3186 method updownlevel incr =
3187 let len = source#getitemcount in
3188 let curlevel =
3189 if m_active >= 0 && m_active < len
3190 then snd (source#getitem m_active)
3191 else -1
3193 let rec flow i =
3194 if i = len then i-1 else if i = -1 then 0 else
3195 let _, l = source#getitem i in
3196 if l != curlevel then i else flow (i+incr)
3198 let active = flow m_active in
3199 let first = calcfirst m_first active in
3200 G.postRedisplay "outline updownlevel";
3201 {< m_active = active; m_first = first >}
3203 method private key1 key mask =
3204 let set1 active first qsearch =
3205 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3207 let search active pattern incr =
3208 let dosearch re =
3209 let rec loop n =
3210 if n >= 0 && n < source#getitemcount
3211 then (
3212 let s, _ = source#getitem n in
3214 (try ignore (Str.search_forward re s 0); true
3215 with Not_found -> false)
3216 then Some n
3217 else loop (n + incr)
3219 else None
3221 loop active
3224 let re = Str.regexp_case_fold pattern in
3225 dosearch re
3226 with Failure s ->
3227 state.text <- s;
3228 None
3230 let itemcount = source#getitemcount in
3231 let find start incr =
3232 let rec find i =
3233 if i = -1 || i = itemcount
3234 then -1
3235 else (
3236 if source#hasaction i
3237 then i
3238 else find (i + incr)
3241 find start
3243 let set active first =
3244 let first = bound first 0 (itemcount - fstate.maxrows) in
3245 state.text <- "";
3246 coe {< m_active = active; m_first = first >}
3248 let navigate incr =
3249 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3250 let active, first =
3251 let incr1 = if incr > 0 then 1 else -1 in
3252 if isvisible m_first m_active
3253 then
3254 let next =
3255 let next = m_active + incr in
3256 let next =
3257 if next < 0 || next >= itemcount
3258 then -1
3259 else find next incr1
3261 if next = -1 || abs (m_active - next) > fstate.maxrows
3262 then -1
3263 else next
3265 if next = -1
3266 then
3267 let first = m_first + incr in
3268 let first = bound first 0 (itemcount - 1) in
3269 let next =
3270 let next = m_active + incr in
3271 let next = bound next 0 (itemcount - 1) in
3272 find next ~-incr1
3274 let active = if next = -1 then m_active else next in
3275 active, first
3276 else
3277 let first = min next m_first in
3278 let first =
3279 if abs (next - first) > fstate.maxrows
3280 then first + incr
3281 else first
3283 next, first
3284 else
3285 let first = m_first + incr in
3286 let first = bound first 0 (itemcount - 1) in
3287 let active =
3288 let next = m_active + incr in
3289 let next = bound next 0 (itemcount - 1) in
3290 let next = find next incr1 in
3291 let active =
3292 if next = -1 || abs (m_active - first) > fstate.maxrows
3293 then (
3294 let active = if m_active = -1 then next else m_active in
3295 active
3297 else next
3299 if isvisible first active
3300 then active
3301 else -1
3303 active, first
3305 G.postRedisplay "listview navigate";
3306 set active first;
3308 match key with
3309 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3310 let incr = if key = 0x72 then -1 else 1 in
3311 let active, first =
3312 match search (m_active + incr) m_qsearch incr with
3313 | None ->
3314 state.text <- m_qsearch ^ " [not found]";
3315 m_active, m_first
3316 | Some active ->
3317 state.text <- m_qsearch;
3318 active, firstof m_first active
3320 G.postRedisplay "listview ctrl-r/s";
3321 set1 active first m_qsearch;
3323 | 0xff08 -> (* backspace *)
3324 if String.length m_qsearch = 0
3325 then coe self
3326 else (
3327 let qsearch = withoutlastutf8 m_qsearch in
3328 let len = String.length qsearch in
3329 if len = 0
3330 then (
3331 state.text <- "";
3332 G.postRedisplay "listview empty qsearch";
3333 set1 m_active m_first "";
3335 else
3336 let active, first =
3337 match search m_active qsearch ~-1 with
3338 | None ->
3339 state.text <- qsearch ^ " [not found]";
3340 m_active, m_first
3341 | Some active ->
3342 state.text <- qsearch;
3343 active, firstof m_first active
3345 G.postRedisplay "listview backspace qsearch";
3346 set1 active first qsearch
3349 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3350 let pattern = m_qsearch ^ Wsi.toutf8 key in
3351 let active, first =
3352 match search m_active pattern 1 with
3353 | None ->
3354 state.text <- pattern ^ " [not found]";
3355 m_active, m_first
3356 | Some active ->
3357 state.text <- pattern;
3358 active, firstof m_first active
3360 G.postRedisplay "listview qsearch add";
3361 set1 active first pattern;
3363 | 0xff1b -> (* escape *)
3364 state.text <- "";
3365 if String.length m_qsearch = 0
3366 then (
3367 G.postRedisplay "list view escape";
3368 begin
3369 match
3370 source#exit (coe self) true m_active m_first m_pan m_qsearch
3371 with
3372 | None -> m_prev_uioh
3373 | Some uioh -> uioh
3376 else (
3377 G.postRedisplay "list view kill qsearch";
3378 source#setqsearch "";
3379 coe {< m_qsearch = "" >}
3382 | 0xff0d -> (* return *)
3383 state.text <- "";
3384 let self = {< m_qsearch = "" >} in
3385 source#setqsearch "";
3386 let opt =
3387 G.postRedisplay "listview enter";
3388 if m_active >= 0 && m_active < source#getitemcount
3389 then (
3390 source#exit (coe self) false m_active m_first m_pan "";
3392 else (
3393 source#exit (coe self) true m_active m_first m_pan "";
3396 begin match opt with
3397 | None -> m_prev_uioh
3398 | Some uioh -> uioh
3401 | 0xff9f | 0xffff -> (* delete *)
3402 coe self
3404 | 0xff52 -> navigate ~-1 (* up *)
3405 | 0xff54 -> navigate 1 (* down *)
3406 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3407 | 0xff56 -> navigate fstate.maxrows (* next *)
3409 | 0xff53 -> (* right *)
3410 state.text <- "";
3411 G.postRedisplay "listview right";
3412 coe {< m_pan = m_pan - 1 >}
3414 | 0xff51 -> (* left *)
3415 state.text <- "";
3416 G.postRedisplay "listview left";
3417 coe {< m_pan = m_pan + 1 >}
3419 | 0xff50 -> (* home *)
3420 let active = find 0 1 in
3421 G.postRedisplay "listview home";
3422 set active 0;
3424 | 0xff57 -> (* end *)
3425 let first = max 0 (itemcount - fstate.maxrows) in
3426 let active = find (itemcount - 1) ~-1 in
3427 G.postRedisplay "listview end";
3428 set active first;
3430 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3431 coe self
3433 | _ ->
3434 dolog "listview unknown key %#x" key; coe self
3436 method key key mask =
3437 match state.mode with
3438 | Textentry te -> textentrykeyboard key mask te; coe self
3439 | _ -> self#key1 key mask
3441 method button button down x y _ =
3442 let opt =
3443 match button with
3444 | 1 when x > conf.winw - conf.scrollbw ->
3445 G.postRedisplay "listview scroll";
3446 if down
3447 then
3448 let _, position, sh = self#scrollph in
3449 if y > truncate position && y < truncate (position +. sh)
3450 then (
3451 state.mstate <- Mscrolly;
3452 Some (coe self)
3454 else
3455 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3456 let first = truncate (s *. float source#getitemcount) in
3457 let first = min source#getitemcount first in
3458 Some (coe {< m_first = first; m_active = first >})
3459 else (
3460 state.mstate <- Mnone;
3461 Some (coe self);
3463 | 1 when not down ->
3464 begin match self#elemunder y with
3465 | Some n ->
3466 G.postRedisplay "listview click";
3467 source#exit
3468 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3469 | _ ->
3470 Some (coe self)
3472 | n when (n == 4 || n == 5) && not down ->
3473 let len = source#getitemcount in
3474 let first =
3475 if n = 5 && m_first + fstate.maxrows >= len
3476 then
3477 m_first
3478 else
3479 let first = m_first + (if n == 4 then -1 else 1) in
3480 bound first 0 (len - 1)
3482 G.postRedisplay "listview wheel";
3483 Some (coe {< m_first = first >})
3484 | n when (n = 6 || n = 7) && not down ->
3485 let inc = m_first + (if n = 7 then -1 else 1) in
3486 G.postRedisplay "listview hwheel";
3487 Some (coe {< m_pan = m_pan + inc >})
3488 | _ ->
3489 Some (coe self)
3491 match opt with
3492 | None -> m_prev_uioh
3493 | Some uioh -> uioh
3495 method motion _ y =
3496 match state.mstate with
3497 | Mscrolly ->
3498 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3499 let first = truncate (s *. float source#getitemcount) in
3500 let first = min source#getitemcount first in
3501 G.postRedisplay "listview motion";
3502 coe {< m_first = first; m_active = first >}
3503 | _ -> coe self
3505 method pmotion x y =
3506 if x < conf.winw - conf.scrollbw
3507 then
3508 let n =
3509 match self#elemunder y with
3510 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3511 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3513 let o =
3514 if n != m_active
3515 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3516 else self
3518 coe o
3519 else (
3520 Wsi.setcursor Wsi.CURSOR_INHERIT;
3521 coe self
3524 method infochanged _ = ()
3526 method scrollpw = (0, 0.0, 0.0)
3527 method scrollph =
3528 let nfs = fstate.fontsize + 1 in
3529 let y = m_first * nfs in
3530 let itemcount = source#getitemcount in
3531 let maxi = max 0 (itemcount - fstate.maxrows) in
3532 let maxy = maxi * nfs in
3533 let p, h = scrollph y maxy in
3534 conf.scrollbw, p, h
3536 method modehash = modehash
3537 end;;
3539 class outlinelistview ~source =
3540 object (self)
3541 inherit listview
3542 ~source:(source :> lvsource)
3543 ~trusted:false
3544 ~modehash:(findkeyhash conf "outline")
3545 as super
3547 method key key mask =
3548 let calcfirst first active =
3549 if active > first
3550 then
3551 let rows = active - first in
3552 let maxrows =
3553 if String.length state.text = 0
3554 then fstate.maxrows
3555 else fstate.maxrows - 2
3557 if rows > maxrows then active - maxrows else first
3558 else active
3560 let navigate incr =
3561 let active = m_active + incr in
3562 let active = bound active 0 (source#getitemcount - 1) in
3563 let first = calcfirst m_first active in
3564 G.postRedisplay "outline navigate";
3565 coe {< m_active = active; m_first = first >}
3567 let ctrl = Wsi.withctrl mask in
3568 match key with
3569 | 110 when ctrl -> (* ctrl-n *)
3570 source#narrow m_qsearch;
3571 G.postRedisplay "outline ctrl-n";
3572 coe {< m_first = 0; m_active = 0 >}
3574 | 117 when ctrl -> (* ctrl-u *)
3575 source#denarrow;
3576 G.postRedisplay "outline ctrl-u";
3577 state.text <- "";
3578 coe {< m_first = 0; m_active = 0 >}
3580 | 108 when ctrl -> (* ctrl-l *)
3581 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3582 G.postRedisplay "outline ctrl-l";
3583 coe {< m_first = first >}
3585 | 0xff9f | 0xffff -> (* delete *)
3586 source#remove m_active;
3587 G.postRedisplay "outline delete";
3588 let active = max 0 (m_active-1) in
3589 coe {< m_first = firstof m_first active;
3590 m_active = active >}
3592 | 0xff52 -> navigate ~-1 (* up *)
3593 | 0xff54 -> navigate 1 (* down *)
3594 | 0xff55 -> (* prior *)
3595 navigate ~-(fstate.maxrows)
3596 | 0xff56 -> (* next *)
3597 navigate fstate.maxrows
3599 | 0xff53 -> (* [ctrl-]right *)
3600 let o =
3601 if ctrl
3602 then (
3603 G.postRedisplay "outline ctrl right";
3604 {< m_pan = m_pan + 1 >}
3606 else self#updownlevel 1
3608 coe o
3610 | 0xff51 -> (* [ctrl-]left *)
3611 let o =
3612 if ctrl
3613 then (
3614 G.postRedisplay "outline ctrl left";
3615 {< m_pan = m_pan - 1 >}
3617 else self#updownlevel ~-1
3619 coe o
3621 | 0xff50 -> (* home *)
3622 G.postRedisplay "outline home";
3623 coe {< m_first = 0; m_active = 0 >}
3625 | 0xff57 -> (* end *)
3626 let active = source#getitemcount - 1 in
3627 let first = max 0 (active - fstate.maxrows) in
3628 G.postRedisplay "outline end";
3629 coe {< m_active = active; m_first = first >}
3631 | _ -> super#key key mask
3634 let outlinesource usebookmarks =
3635 let empty = [||] in
3636 (object
3637 inherit lvsourcebase
3638 val mutable m_items = empty
3639 val mutable m_orig_items = empty
3640 val mutable m_prev_items = empty
3641 val mutable m_narrow_pattern = ""
3642 val mutable m_hadremovals = false
3644 method getitemcount =
3645 Array.length m_items + (if m_hadremovals then 1 else 0)
3647 method getitem n =
3648 if n == Array.length m_items && m_hadremovals
3649 then
3650 ("[Confirm removal]", 0)
3651 else
3652 let s, n, _ = m_items.(n) in
3653 (s, n)
3655 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3656 ignore (uioh, first, qsearch);
3657 let confrimremoval = m_hadremovals && active = Array.length m_items in
3658 let items =
3659 if String.length m_narrow_pattern = 0
3660 then m_orig_items
3661 else m_items
3663 if not cancel
3664 then (
3665 if not confrimremoval
3666 then(
3667 let _, _, anchor = m_items.(active) in
3668 gotoghyll (getanchory anchor);
3669 m_items <- items;
3671 else (
3672 state.bookmarks <- Array.to_list m_items;
3673 m_orig_items <- m_items;
3676 else m_items <- items;
3677 m_pan <- pan;
3678 None
3680 method hasaction _ = true
3682 method greetmsg =
3683 if Array.length m_items != Array.length m_orig_items
3684 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3685 else ""
3687 method narrow pattern =
3688 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3689 match reopt with
3690 | None -> ()
3691 | Some re ->
3692 let rec loop accu n =
3693 if n = -1
3694 then (
3695 m_narrow_pattern <- pattern;
3696 m_items <- Array.of_list accu
3698 else
3699 let (s, _, _) as o = m_items.(n) in
3700 let accu =
3701 if (try ignore (Str.search_forward re s 0); true
3702 with Not_found -> false)
3703 then o :: accu
3704 else accu
3706 loop accu (n-1)
3708 loop [] (Array.length m_items - 1)
3710 method denarrow =
3711 m_orig_items <- (
3712 if usebookmarks
3713 then Array.of_list state.bookmarks
3714 else state.outlines
3716 m_items <- m_orig_items
3718 method remove m =
3719 if usebookmarks
3720 then
3721 if m >= 0 && m < Array.length m_items
3722 then (
3723 m_hadremovals <- true;
3724 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3725 let n = if n >= m then n+1 else n in
3726 m_items.(n)
3730 method reset anchor items =
3731 m_hadremovals <- false;
3732 if m_orig_items == empty || m_prev_items != items
3733 then (
3734 m_orig_items <- items;
3735 if String.length m_narrow_pattern = 0
3736 then m_items <- items;
3738 m_prev_items <- items;
3739 let rely = getanchory anchor in
3740 let active =
3741 let rec loop n best bestd =
3742 if n = Array.length m_items
3743 then best
3744 else
3745 let (_, _, anchor) = m_items.(n) in
3746 let orely = getanchory anchor in
3747 let d = abs (orely - rely) in
3748 if d < bestd
3749 then loop (n+1) n d
3750 else loop (n+1) best bestd
3752 loop 0 ~-1 max_int
3754 m_active <- active;
3755 m_first <- firstof m_first active
3756 end)
3759 let enterselector usebookmarks =
3760 let source = outlinesource usebookmarks in
3761 fun errmsg ->
3762 let outlines =
3763 if usebookmarks
3764 then Array.of_list state.bookmarks
3765 else state.outlines
3767 if Array.length outlines = 0
3768 then (
3769 showtext ' ' errmsg;
3771 else (
3772 state.text <- source#greetmsg;
3773 Wsi.setcursor Wsi.CURSOR_INHERIT;
3774 let anchor = getanchor () in
3775 source#reset anchor outlines;
3776 state.uioh <- coe (new outlinelistview ~source);
3777 G.postRedisplay "enter selector";
3781 let enteroutlinemode =
3782 let f = enterselector false in
3783 fun ()-> f "Document has no outline";
3786 let enterbookmarkmode =
3787 let f = enterselector true in
3788 fun () -> f "Document has no bookmarks (yet)";
3791 let color_of_string s =
3792 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3793 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3797 let color_to_string (r, g, b) =
3798 let r = truncate (r *. 256.0)
3799 and g = truncate (g *. 256.0)
3800 and b = truncate (b *. 256.0) in
3801 Printf.sprintf "%d/%d/%d" r g b
3804 let irect_of_string s =
3805 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3808 let irect_to_string (x0,y0,x1,y1) =
3809 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3812 let makecheckers () =
3813 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3814 following to say:
3815 converted by Issac Trotts. July 25, 2002 *)
3816 let image_height = 64
3817 and image_width = 64 in
3819 let make_image () =
3820 let image =
3821 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3823 for i = 0 to image_width - 1 do
3824 for j = 0 to image_height - 1 do
3825 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3826 (if (i land 8 ) lxor (j land 8) = 0
3827 then [|255;255;255|] else [|200;200;200|])
3828 done
3829 done;
3830 image
3832 let image = make_image () in
3833 let id = GlTex.gen_texture () in
3834 GlTex.bind_texture `texture_2d id;
3835 GlPix.store (`unpack_alignment 1);
3836 GlTex.image2d image;
3837 List.iter (GlTex.parameter ~target:`texture_2d)
3838 [ `wrap_s `repeat;
3839 `wrap_t `repeat;
3840 `mag_filter `nearest;
3841 `min_filter `nearest ];
3845 let setcheckers enabled =
3846 match state.texid with
3847 | None ->
3848 if enabled then state.texid <- Some (makecheckers ())
3850 | Some texid ->
3851 if not enabled
3852 then (
3853 GlTex.delete_texture texid;
3854 state.texid <- None;
3858 let int_of_string_with_suffix s =
3859 let l = String.length s in
3860 let s1, shift =
3861 if l > 1
3862 then
3863 let suffix = Char.lowercase s.[l-1] in
3864 match suffix with
3865 | 'k' -> String.sub s 0 (l-1), 10
3866 | 'm' -> String.sub s 0 (l-1), 20
3867 | 'g' -> String.sub s 0 (l-1), 30
3868 | _ -> s, 0
3869 else s, 0
3871 let n = int_of_string s1 in
3872 let m = n lsl shift in
3873 if m < 0 || m < n
3874 then raise (Failure "value too large")
3875 else m
3878 let string_with_suffix_of_int n =
3879 if n = 0
3880 then "0"
3881 else
3882 let n, s =
3883 if n land ((1 lsl 20) - 1) = 0
3884 then n lsr 20, "M"
3885 else (
3886 if n land ((1 lsl 10) - 1) = 0
3887 then n lsr 10, "K"
3888 else n, ""
3891 let rec loop s n =
3892 let h = n mod 1000 in
3893 let n = n / 1000 in
3894 if n = 0
3895 then string_of_int h ^ s
3896 else (
3897 let s = Printf.sprintf "_%03d%s" h s in
3898 loop s n
3901 loop "" n ^ s;
3904 let defghyllscroll = (40, 8, 32);;
3905 let ghyllscroll_of_string s =
3906 let (n, a, b) as nab =
3907 if s = "default"
3908 then defghyllscroll
3909 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3911 if n <= a || n <= b || a >= b
3912 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3913 nab;
3916 let ghyllscroll_to_string ((n, a, b) as nab) =
3917 if nab = defghyllscroll
3918 then "default"
3919 else Printf.sprintf "%d,%d,%d" n a b;
3922 let describe_location () =
3923 let f (fn, _) l =
3924 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3926 let fn, ln = List.fold_left f (-1, -1) state.layout in
3927 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3928 let percent =
3929 if maxy <= 0
3930 then 100.
3931 else (100. *. (float state.y /. float maxy))
3933 if fn = ln
3934 then
3935 Printf.sprintf "page %d of %d [%.2f%%]"
3936 (fn+1) state.pagecount percent
3937 else
3938 Printf.sprintf
3939 "pages %d-%d of %d [%.2f%%]"
3940 (fn+1) (ln+1) state.pagecount percent
3943 let setpresentationmode v =
3944 let (n, _, _) = getanchor () in
3945 let _, h = getpageyh n in
3946 let ips = if conf.presentation then calcips h else conf.interpagespace in
3947 state.anchor <- (n, 0.0, float ips);
3948 conf.presentation <- v;
3949 if conf.presentation
3950 then (
3951 if not conf.scrollbarinpm
3952 then state.scrollw <- 0;
3954 else state.scrollw <- conf.scrollbw;
3955 represent ();
3958 let enterinfomode =
3959 let btos b = if b then "\xe2\x88\x9a" else "" in
3960 let showextended = ref false in
3961 let leave mode = function
3962 | Confirm -> state.mode <- mode
3963 | Cancel -> state.mode <- mode in
3964 let src =
3965 (object
3966 val mutable m_first_time = true
3967 val mutable m_l = []
3968 val mutable m_a = [||]
3969 val mutable m_prev_uioh = nouioh
3970 val mutable m_prev_mode = View
3972 inherit lvsourcebase
3974 method reset prev_mode prev_uioh =
3975 m_a <- Array.of_list (List.rev m_l);
3976 m_l <- [];
3977 m_prev_mode <- prev_mode;
3978 m_prev_uioh <- prev_uioh;
3979 if m_first_time
3980 then (
3981 let rec loop n =
3982 if n >= Array.length m_a
3983 then ()
3984 else
3985 match m_a.(n) with
3986 | _, _, _, Action _ -> m_active <- n
3987 | _ -> loop (n+1)
3989 loop 0;
3990 m_first_time <- false;
3993 method int name get set =
3994 m_l <-
3995 (name, `int get, 1, Action (
3996 fun u ->
3997 let ondone s =
3998 try set (int_of_string s)
3999 with exn ->
4000 state.text <- Printf.sprintf "bad integer `%s': %s"
4001 s (Printexc.to_string exn)
4003 state.text <- "";
4004 let te = name ^ ": ", "", None, intentry, ondone, true in
4005 state.mode <- Textentry (te, leave m_prev_mode);
4007 )) :: m_l
4009 method int_with_suffix name get set =
4010 m_l <-
4011 (name, `intws get, 1, Action (
4012 fun u ->
4013 let ondone s =
4014 try set (int_of_string_with_suffix s)
4015 with exn ->
4016 state.text <- Printf.sprintf "bad integer `%s': %s"
4017 s (Printexc.to_string exn)
4019 state.text <- "";
4020 let te =
4021 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4023 state.mode <- Textentry (te, leave m_prev_mode);
4025 )) :: m_l
4027 method bool ?(offset=1) ?(btos=btos) name get set =
4028 m_l <-
4029 (name, `bool (btos, get), offset, Action (
4030 fun u ->
4031 let v = get () in
4032 set (not v);
4034 )) :: m_l
4036 method color name get set =
4037 m_l <-
4038 (name, `color get, 1, Action (
4039 fun u ->
4040 let invalid = (nan, nan, nan) in
4041 let ondone s =
4042 let c =
4043 try color_of_string s
4044 with exn ->
4045 state.text <- Printf.sprintf "bad color `%s': %s"
4046 s (Printexc.to_string exn);
4047 invalid
4049 if c <> invalid
4050 then set c;
4052 let te = name ^ ": ", "", None, textentry, ondone, true in
4053 state.text <- color_to_string (get ());
4054 state.mode <- Textentry (te, leave m_prev_mode);
4056 )) :: m_l
4058 method string name get set =
4059 m_l <-
4060 (name, `string get, 1, Action (
4061 fun u ->
4062 let ondone s = set s in
4063 let te = name ^ ": ", "", None, textentry, ondone, true in
4064 state.mode <- Textentry (te, leave m_prev_mode);
4066 )) :: m_l
4068 method colorspace name get set =
4069 m_l <-
4070 (name, `string get, 1, Action (
4071 fun _ ->
4072 let source =
4073 let vals = [| "rgb"; "bgr"; "gray" |] in
4074 (object
4075 inherit lvsourcebase
4077 initializer
4078 m_active <- int_of_colorspace conf.colorspace;
4079 m_first <- 0;
4081 method getitemcount = Array.length vals
4082 method getitem n = (vals.(n), 0)
4083 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4084 ignore (uioh, first, pan, qsearch);
4085 if not cancel then set active;
4086 None
4087 method hasaction _ = true
4088 end)
4090 state.text <- "";
4091 let modehash = findkeyhash conf "info" in
4092 coe (new listview ~source ~trusted:true ~modehash)
4093 )) :: m_l
4095 method caption s offset =
4096 m_l <- (s, `empty, offset, Noaction) :: m_l
4098 method caption2 s f offset =
4099 m_l <- (s, `string f, offset, Noaction) :: m_l
4101 method getitemcount = Array.length m_a
4103 method getitem n =
4104 let tostr = function
4105 | `int f -> string_of_int (f ())
4106 | `intws f -> string_with_suffix_of_int (f ())
4107 | `string f -> f ()
4108 | `color f -> color_to_string (f ())
4109 | `bool (btos, f) -> btos (f ())
4110 | `empty -> ""
4112 let name, t, offset, _ = m_a.(n) in
4113 ((let s = tostr t in
4114 if String.length s > 0
4115 then Printf.sprintf "%s\t%s" name s
4116 else name),
4117 offset)
4119 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4120 let uiohopt =
4121 if not cancel
4122 then (
4123 m_qsearch <- qsearch;
4124 let uioh =
4125 match m_a.(active) with
4126 | _, _, _, Action f -> f uioh
4127 | _ -> uioh
4129 Some uioh
4131 else None
4133 m_active <- active;
4134 m_first <- first;
4135 m_pan <- pan;
4136 uiohopt
4138 method hasaction n =
4139 match m_a.(n) with
4140 | _, _, _, Action _ -> true
4141 | _ -> false
4142 end)
4144 let rec fillsrc prevmode prevuioh =
4145 let sep () = src#caption "" 0 in
4146 let colorp name get set =
4147 src#string name
4148 (fun () -> color_to_string (get ()))
4149 (fun v ->
4151 let c = color_of_string v in
4152 set c
4153 with exn ->
4154 state.text <- Printf.sprintf "bad color `%s': %s"
4155 v (Printexc.to_string exn);
4158 let oldmode = state.mode in
4159 let birdseye = isbirdseye state.mode in
4161 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4163 src#bool "presentation mode"
4164 (fun () -> conf.presentation)
4165 (fun v -> setpresentationmode v);
4167 src#bool "ignore case in searches"
4168 (fun () -> conf.icase)
4169 (fun v -> conf.icase <- v);
4171 src#bool "preload"
4172 (fun () -> conf.preload)
4173 (fun v -> conf.preload <- v);
4175 src#bool "highlight links"
4176 (fun () -> conf.hlinks)
4177 (fun v -> conf.hlinks <- v);
4179 src#bool "under info"
4180 (fun () -> conf.underinfo)
4181 (fun v -> conf.underinfo <- v);
4183 src#bool "persistent bookmarks"
4184 (fun () -> conf.savebmarks)
4185 (fun v -> conf.savebmarks <- v);
4187 src#bool "proportional display"
4188 (fun () -> conf.proportional)
4189 (fun v -> reqlayout conf.angle v);
4191 src#bool "trim margins"
4192 (fun () -> conf.trimmargins)
4193 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4195 src#bool "persistent location"
4196 (fun () -> conf.jumpback)
4197 (fun v -> conf.jumpback <- v);
4199 sep ();
4200 src#int "inter-page space"
4201 (fun () -> conf.interpagespace)
4202 (fun n ->
4203 conf.interpagespace <- n;
4204 docolumns conf.columns;
4205 let pageno, py =
4206 match state.layout with
4207 | [] -> 0, 0
4208 | l :: _ ->
4209 l.pageno, l.pagey
4211 state.maxy <- calcheight ();
4212 let y = getpagey pageno in
4213 gotoy (y + py)
4216 src#int "page bias"
4217 (fun () -> conf.pagebias)
4218 (fun v -> conf.pagebias <- v);
4220 src#int "scroll step"
4221 (fun () -> conf.scrollstep)
4222 (fun n -> conf.scrollstep <- n);
4224 src#int "horizontal scroll step"
4225 (fun () -> conf.hscrollstep)
4226 (fun v -> conf.hscrollstep <- v);
4228 src#int "auto scroll step"
4229 (fun () ->
4230 match state.autoscroll with
4231 | Some step -> step
4232 | _ -> conf.autoscrollstep)
4233 (fun n ->
4234 if state.autoscroll <> None
4235 then state.autoscroll <- Some n;
4236 conf.autoscrollstep <- n);
4238 src#int "zoom"
4239 (fun () -> truncate (conf.zoom *. 100.))
4240 (fun v -> setzoom ((float v) /. 100.));
4242 src#int "rotation"
4243 (fun () -> conf.angle)
4244 (fun v -> reqlayout v conf.proportional);
4246 src#int "scroll bar width"
4247 (fun () -> state.scrollw)
4248 (fun v ->
4249 state.scrollw <- v;
4250 conf.scrollbw <- v;
4251 reshape conf.winw conf.winh;
4254 src#int "scroll handle height"
4255 (fun () -> conf.scrollh)
4256 (fun v -> conf.scrollh <- v;);
4258 src#int "thumbnail width"
4259 (fun () -> conf.thumbw)
4260 (fun v ->
4261 conf.thumbw <- min 4096 v;
4262 match oldmode with
4263 | Birdseye beye ->
4264 leavebirdseye beye false;
4265 enterbirdseye ()
4266 | _ -> ()
4269 let mode = state.mode in
4270 src#string "columns"
4271 (fun () ->
4272 match conf.columns with
4273 | Csingle _ -> "1"
4274 | Cmulti (multi, _) -> multicolumns_to_string multi
4275 | Csplit (count, _) -> "-" ^ string_of_int count
4277 (fun v ->
4278 let n, a, b = multicolumns_of_string v in
4279 setcolumns mode n a b);
4281 sep ();
4282 src#caption "Presentation mode" 0;
4283 src#bool "scrollbar visible"
4284 (fun () -> conf.scrollbarinpm)
4285 (fun v ->
4286 if v != conf.scrollbarinpm
4287 then (
4288 conf.scrollbarinpm <- v;
4289 if conf.presentation
4290 then (
4291 state.scrollw <- if v then conf.scrollbw else 0;
4292 reshape conf.winw conf.winh;
4297 sep ();
4298 src#caption "Pixmap cache" 0;
4299 src#int_with_suffix "size (advisory)"
4300 (fun () -> conf.memlimit)
4301 (fun v -> conf.memlimit <- v);
4303 src#caption2 "used"
4304 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4305 (string_with_suffix_of_int state.memused)
4306 (Hashtbl.length state.tilemap)) 1;
4308 sep ();
4309 src#caption "Layout" 0;
4310 src#caption2 "Dimension"
4311 (fun () ->
4312 Printf.sprintf "%dx%d (virtual %dx%d)"
4313 conf.winw conf.winh
4314 state.w state.maxy)
4316 if conf.debug
4317 then
4318 src#caption2 "Position" (fun () ->
4319 Printf.sprintf "%dx%d" state.x state.y
4321 else
4322 src#caption2 "Visible" (fun () -> describe_location ()) 1
4325 sep ();
4326 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4327 "Save these parameters as global defaults at exit"
4328 (fun () -> conf.bedefault)
4329 (fun v -> conf.bedefault <- v)
4332 sep ();
4333 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4334 src#bool ~offset:0 ~btos "Extended parameters"
4335 (fun () -> !showextended)
4336 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4337 if !showextended
4338 then (
4339 src#bool "checkers"
4340 (fun () -> conf.checkers)
4341 (fun v -> conf.checkers <- v; setcheckers v);
4342 src#bool "update cursor"
4343 (fun () -> conf.updatecurs)
4344 (fun v -> conf.updatecurs <- v);
4345 src#bool "verbose"
4346 (fun () -> conf.verbose)
4347 (fun v -> conf.verbose <- v);
4348 src#bool "invert colors"
4349 (fun () -> conf.invert)
4350 (fun v -> conf.invert <- v);
4351 src#bool "max fit"
4352 (fun () -> conf.maxhfit)
4353 (fun v -> conf.maxhfit <- v);
4354 src#bool "redirect stderr"
4355 (fun () -> conf.redirectstderr)
4356 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4357 src#string "uri launcher"
4358 (fun () -> conf.urilauncher)
4359 (fun v -> conf.urilauncher <- v);
4360 src#string "path launcher"
4361 (fun () -> conf.pathlauncher)
4362 (fun v -> conf.pathlauncher <- v);
4363 src#string "tile size"
4364 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4365 (fun v ->
4367 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4368 conf.tilew <- max 64 w;
4369 conf.tileh <- max 64 h;
4370 flushtiles ();
4371 with exn ->
4372 state.text <- Printf.sprintf "bad tile size `%s': %s"
4373 v (Printexc.to_string exn));
4374 src#int "texture count"
4375 (fun () -> conf.texcount)
4376 (fun v ->
4377 if realloctexts v
4378 then conf.texcount <- v
4379 else showtext '!' " Failed to set texture count please retry later"
4381 src#int "slice height"
4382 (fun () -> conf.sliceheight)
4383 (fun v ->
4384 conf.sliceheight <- v;
4385 wcmd "sliceh %d" conf.sliceheight;
4387 src#int "anti-aliasing level"
4388 (fun () -> conf.aalevel)
4389 (fun v ->
4390 conf.aalevel <- bound v 0 8;
4391 state.anchor <- getanchor ();
4392 opendoc state.path state.password;
4394 src#string "page scroll scaling factor"
4395 (fun () -> string_of_float conf.pgscale)
4396 (fun v ->
4398 let s = float_of_string v in
4399 conf.pgscale <- s
4400 with exn ->
4401 state.text <- Printf.sprintf
4402 "bad page scroll scaling factor `%s': %s"
4403 v (Printexc.to_string exn)
4406 src#int "ui font size"
4407 (fun () -> fstate.fontsize)
4408 (fun v -> setfontsize (bound v 5 100));
4409 src#int "hint font size"
4410 (fun () -> conf.hfsize)
4411 (fun v -> conf.hfsize <- bound v 5 100);
4412 colorp "background color"
4413 (fun () -> conf.bgcolor)
4414 (fun v -> conf.bgcolor <- v);
4415 src#bool "crop hack"
4416 (fun () -> conf.crophack)
4417 (fun v -> conf.crophack <- v);
4418 src#string "trim fuzz"
4419 (fun () -> irect_to_string conf.trimfuzz)
4420 (fun v ->
4422 conf.trimfuzz <- irect_of_string v;
4423 if conf.trimmargins
4424 then settrim true conf.trimfuzz;
4425 with exn ->
4426 state.text <- Printf.sprintf "bad irect `%s': %s"
4427 v (Printexc.to_string exn)
4429 src#string "throttle"
4430 (fun () ->
4431 match conf.maxwait with
4432 | None -> "show place holder if page is not ready"
4433 | Some time ->
4434 if time = infinity
4435 then "wait for page to fully render"
4436 else
4437 "wait " ^ string_of_float time
4438 ^ " seconds before showing placeholder"
4440 (fun v ->
4442 let f = float_of_string v in
4443 if f <= 0.0
4444 then conf.maxwait <- None
4445 else conf.maxwait <- Some f
4446 with exn ->
4447 state.text <- Printf.sprintf "bad time `%s': %s"
4448 v (Printexc.to_string exn)
4450 src#string "ghyll scroll"
4451 (fun () ->
4452 match conf.ghyllscroll with
4453 | None -> ""
4454 | Some nab -> ghyllscroll_to_string nab
4456 (fun v ->
4458 let gs =
4459 if String.length v = 0
4460 then None
4461 else Some (ghyllscroll_of_string v)
4463 conf.ghyllscroll <- gs
4464 with exn ->
4465 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4466 v (Printexc.to_string exn)
4468 src#string "selection command"
4469 (fun () -> conf.selcmd)
4470 (fun v -> conf.selcmd <- v);
4471 src#colorspace "color space"
4472 (fun () -> colorspace_to_string conf.colorspace)
4473 (fun v ->
4474 conf.colorspace <- colorspace_of_int v;
4475 wcmd "cs %d" v;
4476 load state.layout;
4480 sep ();
4481 src#caption "Document" 0;
4482 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4483 src#caption2 "Pages"
4484 (fun () -> string_of_int state.pagecount) 1;
4485 src#caption2 "Dimensions"
4486 (fun () -> string_of_int (List.length state.pdims)) 1;
4487 if conf.trimmargins
4488 then (
4489 sep ();
4490 src#caption "Trimmed margins" 0;
4491 src#caption2 "Dimensions"
4492 (fun () -> string_of_int (List.length state.pdims)) 1;
4495 sep ();
4496 src#caption "OpenGL" 0;
4497 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4498 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4499 src#reset prevmode prevuioh;
4501 fun () ->
4502 state.text <- "";
4503 let prevmode = state.mode
4504 and prevuioh = state.uioh in
4505 fillsrc prevmode prevuioh;
4506 let source = (src :> lvsource) in
4507 let modehash = findkeyhash conf "info" in
4508 state.uioh <- coe (object (self)
4509 inherit listview ~source ~trusted:true ~modehash as super
4510 val mutable m_prevmemused = 0
4511 method infochanged = function
4512 | Memused ->
4513 if m_prevmemused != state.memused
4514 then (
4515 m_prevmemused <- state.memused;
4516 G.postRedisplay "memusedchanged";
4518 | Pdim -> G.postRedisplay "pdimchanged"
4519 | Docinfo -> fillsrc prevmode prevuioh
4521 method key key mask =
4522 if not (Wsi.withctrl mask)
4523 then
4524 match key with
4525 | 0xff51 -> coe (self#updownlevel ~-1)
4526 | 0xff53 -> coe (self#updownlevel 1)
4527 | _ -> super#key key mask
4528 else super#key key mask
4529 end);
4530 G.postRedisplay "info";
4533 let enterhelpmode =
4534 let source =
4535 (object
4536 inherit lvsourcebase
4537 method getitemcount = Array.length state.help
4538 method getitem n =
4539 let s, l, _ = state.help.(n) in
4540 (s, l)
4542 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4543 let optuioh =
4544 if not cancel
4545 then (
4546 m_qsearch <- qsearch;
4547 match state.help.(active) with
4548 | _, _, Action f -> Some (f uioh)
4549 | _ -> Some (uioh)
4551 else None
4553 m_active <- active;
4554 m_first <- first;
4555 m_pan <- pan;
4556 optuioh
4558 method hasaction n =
4559 match state.help.(n) with
4560 | _, _, Action _ -> true
4561 | _ -> false
4563 initializer
4564 m_active <- -1
4565 end)
4566 in fun () ->
4567 let modehash = findkeyhash conf "help" in
4568 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4569 G.postRedisplay "help";
4572 let entermsgsmode =
4573 let msgsource =
4574 let re = Str.regexp "[\r\n]" in
4575 (object
4576 inherit lvsourcebase
4577 val mutable m_items = [||]
4579 method getitemcount = 1 + Array.length m_items
4581 method getitem n =
4582 if n = 0
4583 then "[Clear]", 0
4584 else m_items.(n-1), 0
4586 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4587 ignore uioh;
4588 if not cancel
4589 then (
4590 if active = 0
4591 then Buffer.clear state.errmsgs;
4592 m_qsearch <- qsearch;
4594 m_active <- active;
4595 m_first <- first;
4596 m_pan <- pan;
4597 None
4599 method hasaction n =
4600 n = 0
4602 method reset =
4603 state.newerrmsgs <- false;
4604 let l = Str.split re (Buffer.contents state.errmsgs) in
4605 m_items <- Array.of_list l
4607 initializer
4608 m_active <- 0
4609 end)
4610 in fun () ->
4611 state.text <- "";
4612 msgsource#reset;
4613 let source = (msgsource :> lvsource) in
4614 let modehash = findkeyhash conf "listview" in
4615 state.uioh <- coe (object
4616 inherit listview ~source ~trusted:false ~modehash as super
4617 method display =
4618 if state.newerrmsgs
4619 then msgsource#reset;
4620 super#display
4621 end);
4622 G.postRedisplay "msgs";
4625 let quickbookmark ?title () =
4626 match state.layout with
4627 | [] -> ()
4628 | l :: _ ->
4629 let title =
4630 match title with
4631 | None ->
4632 let sec = Unix.gettimeofday () in
4633 let tm = Unix.localtime sec in
4634 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4635 (l.pageno+1)
4636 tm.Unix.tm_mday
4637 tm.Unix.tm_mon
4638 (tm.Unix.tm_year + 1900)
4639 tm.Unix.tm_hour
4640 tm.Unix.tm_min
4641 | Some title -> title
4643 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4646 let doreshape w h =
4647 state.fullscreen <- None;
4648 Wsi.reshape w h;
4651 let setautoscrollspeed step goingdown =
4652 let incr = max 1 ((abs step) / 2) in
4653 let incr = if goingdown then incr else -incr in
4654 let astep = step + incr in
4655 state.autoscroll <- Some astep;
4658 let gotounder = function
4659 | Ulinkgoto (pageno, top) ->
4660 if pageno >= 0
4661 then (
4662 addnav ();
4663 gotopage1 pageno top;
4666 | Ulinkuri s ->
4667 gotouri s
4669 | Uremote (filename, pageno) ->
4670 let path =
4671 if Sys.file_exists filename
4672 then filename
4673 else
4674 let dir = Filename.dirname state.path in
4675 let path = Filename.concat dir filename in
4676 if Sys.file_exists path
4677 then path
4678 else ""
4680 if String.length path > 0
4681 then (
4682 let anchor = getanchor () in
4683 let ranchor = state.path, state.password, anchor in
4684 state.anchor <- (pageno, 0.0, 0.0);
4685 state.ranchors <- ranchor :: state.ranchors;
4686 opendoc path "";
4688 else showtext '!' ("Could not find " ^ filename)
4690 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4693 let canpan () =
4694 match conf.columns with
4695 | Csplit _ -> true
4696 | _ -> conf.zoom > 1.0
4699 let viewkeyboard key mask =
4700 let enttext te =
4701 let mode = state.mode in
4702 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4703 state.text <- "";
4704 enttext ();
4705 G.postRedisplay "view:enttext"
4707 let ctrl = Wsi.withctrl mask in
4708 let existsinrow pageno (columns, coverA, coverB) p =
4709 let last = ((pageno - coverA) mod columns) + columns in
4710 let rec any = function
4711 | [] -> false
4712 | l :: rest ->
4713 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4714 then p l
4715 else (
4716 if not (p l)
4717 then (if l.pageno = last then false else any rest)
4718 else true
4721 any state.layout
4723 match key with
4724 | 81 -> (* Q *)
4725 exit 0
4727 | 0xff63 -> (* insert *)
4728 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4729 then (
4730 state.mode <- LinkNav (Ltgendir 0);
4731 gotoy state.y;
4733 else showtext '!' "Keyboard link navigation does not work under rotation"
4735 | 0xff1b | 113 -> (* escape / q *)
4736 begin match state.mstate with
4737 | Mzoomrect _ ->
4738 state.mstate <- Mnone;
4739 Wsi.setcursor Wsi.CURSOR_INHERIT;
4740 G.postRedisplay "kill zoom rect";
4741 | _ ->
4742 match state.ranchors with
4743 | [] -> raise Quit
4744 | (path, password, anchor) :: rest ->
4745 state.ranchors <- rest;
4746 state.anchor <- anchor;
4747 opendoc path password
4748 end;
4750 | 0xff08 -> (* backspace *)
4751 gotoghyll (getnav ~-1)
4753 | 111 -> (* o *)
4754 enteroutlinemode ()
4756 | 117 -> (* u *)
4757 state.rects <- [];
4758 state.text <- "";
4759 G.postRedisplay "dehighlight";
4761 | 47 | 63 -> (* / ? *)
4762 let ondone isforw s =
4763 cbput state.hists.pat s;
4764 state.searchpattern <- s;
4765 search s isforw
4767 let s = String.create 1 in
4768 s.[0] <- Char.chr key;
4769 enttext (s, "", Some (onhist state.hists.pat),
4770 textentry, ondone (key = 47), true)
4772 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4773 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4774 setzoom (conf.zoom +. incr)
4776 | 43 | 0xffab -> (* + *)
4777 let ondone s =
4778 let n =
4779 try int_of_string s with exc ->
4780 state.text <- Printf.sprintf "bad integer `%s': %s"
4781 s (Printexc.to_string exc);
4782 max_int
4784 if n != max_int
4785 then (
4786 conf.pagebias <- n;
4787 state.text <- "page bias is now " ^ string_of_int n;
4790 enttext ("page bias: ", "", None, intentry, ondone, true)
4792 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4793 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4794 setzoom (max 0.01 (conf.zoom -. decr))
4796 | 45 | 0xffad -> (* - *)
4797 let ondone msg = state.text <- msg in
4798 enttext (
4799 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4800 optentry state.mode, ondone, true
4803 | 48 when ctrl -> (* ctrl-0 *)
4804 setzoom 1.0
4806 | 49 when ctrl -> (* ctrl-1 *)
4807 let cols =
4808 match conf.columns with
4809 | Csingle _ | Cmulti _ -> 1
4810 | Csplit (n, _) -> n
4812 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4813 if zoom < 1.0
4814 then setzoom zoom
4816 | 0xffc6 -> (* f9 *)
4817 togglebirdseye ()
4819 | 57 when ctrl -> (* ctrl-9 *)
4820 togglebirdseye ()
4822 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4823 when not ctrl -> (* 0..9 *)
4824 let ondone s =
4825 let n =
4826 try int_of_string s with exc ->
4827 state.text <- Printf.sprintf "bad integer `%s': %s"
4828 s (Printexc.to_string exc);
4831 if n >= 0
4832 then (
4833 addnav ();
4834 cbput state.hists.pag (string_of_int n);
4835 gotopage1 (n + conf.pagebias - 1) 0;
4838 let pageentry text key =
4839 match Char.unsafe_chr key with
4840 | 'g' -> TEdone text
4841 | _ -> intentry text key
4843 let text = "x" in text.[0] <- Char.chr key;
4844 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4846 | 98 -> (* b *)
4847 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4848 reshape conf.winw conf.winh;
4850 | 108 -> (* l *)
4851 conf.hlinks <- not conf.hlinks;
4852 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4853 G.postRedisplay "toggle highlightlinks";
4855 | 70 -> (* F *)
4856 state.glinks <- true;
4857 let mode = state.mode in
4858 state.mode <- Textentry (
4860 ":", "", None, linknentry, linkndone (fun under -> gotounder under),
4861 false
4862 ), fun _ ->
4863 state.glinks <- false;
4864 state.mode <- mode
4866 state.text <- "";
4867 G.postRedisplay "view:linkent(F)"
4869 | 121 -> (* y *)
4870 state.glinks <- true;
4871 let mode = state.mode in
4872 state.mode <- Textentry (
4873 (":", "", None, linknentry, linkndone (fun under ->
4874 match Ne.pipe () with
4875 | Ne.Exn exn ->
4876 showtext '!' (Printf.sprintf "pipe failed: %s"
4877 (Printexc.to_string exn));
4878 | Ne.Res (r, w) ->
4879 let popened =
4880 try popen conf.selcmd [r, 0; w, -1]; true
4881 with exn ->
4882 showtext '!'
4883 (Printf.sprintf "failed to execute %s: %s"
4884 conf.selcmd (Printexc.to_string exn));
4885 false
4887 let clo cap fd =
4888 Ne.clo fd (fun msg ->
4889 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4892 let s = undertext under in
4893 if popened
4894 then
4895 (try
4896 let l = String.length s in
4897 let n = Unix.write w s 0 l in
4898 if n != l
4899 then
4900 showtext '!'
4901 (Printf.sprintf
4902 "failed to write %d characters to sel pipe, wrote %d"
4905 with exn ->
4906 showtext '!'
4907 (Printf.sprintf "failed to write to sel pipe: %s"
4908 (Printexc.to_string exn)
4911 else dolog "%s" s;
4912 clo "pipe/r" r;
4913 clo "pipe/w" w;
4914 ), false
4916 fun _ ->
4917 state.glinks <- false;
4918 state.mode <- mode
4920 state.text <- "";
4921 G.postRedisplay "view:linkent"
4923 | 97 -> (* a *)
4924 begin match state.autoscroll with
4925 | Some step ->
4926 conf.autoscrollstep <- step;
4927 state.autoscroll <- None
4928 | None ->
4929 if conf.autoscrollstep = 0
4930 then state.autoscroll <- Some 1
4931 else state.autoscroll <- Some conf.autoscrollstep
4934 | 112 when ctrl -> (* ctrl-p *)
4935 launchpath ()
4937 | 80 -> (* P *)
4938 setpresentationmode (not conf.presentation);
4939 showtext ' ' ("presentation mode " ^
4940 if conf.presentation then "on" else "off");
4942 | 102 -> (* f *)
4943 begin match state.fullscreen with
4944 | None ->
4945 state.fullscreen <- Some (conf.winw, conf.winh);
4946 Wsi.fullscreen ()
4947 | Some (w, h) ->
4948 state.fullscreen <- None;
4949 doreshape w h
4952 | 112 | 78 -> (* p|N *)
4953 search state.searchpattern false
4955 | 110 | 0xffc0 -> (* n|F3 *)
4956 search state.searchpattern true
4958 | 116 -> (* t *)
4959 begin match state.layout with
4960 | [] -> ()
4961 | l :: _ ->
4962 gotoy_and_clear_text (getpagey l.pageno)
4965 | 32 -> (* space *)
4966 begin match state.layout with
4967 | [] -> ()
4968 | l :: rest ->
4969 match conf.columns with
4970 | Csingle _ ->
4971 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4972 then
4973 let y = clamp (pgscale conf.winh) in
4974 gotoghyll y
4975 else
4976 let pageno = min (l.pageno+1) (state.pagecount-1) in
4977 gotoghyll (getpagey pageno)
4978 | Cmulti ((c, _, _) as cl, _) ->
4979 if conf.presentation
4980 && (existsinrow l.pageno cl
4981 (fun l -> l.pageh > l.pagey + l.pagevh))
4982 then
4983 let y = clamp (pgscale conf.winh) in
4984 gotoghyll y
4985 else
4986 let pageno = min (l.pageno+c) (state.pagecount-1) in
4987 gotoghyll (getpagey pageno)
4988 | Csplit (n, _) ->
4989 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4990 then
4991 let pagey, pageh = getpageyh l.pageno in
4992 let pagey = pagey + pageh * l.pagecol in
4993 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4994 gotoghyll (pagey + pageh + ips)
4997 | 0xff9f | 0xffff -> (* delete *)
4998 begin match state.layout with
4999 | [] -> ()
5000 | l :: _ ->
5001 match conf.columns with
5002 | Csingle _ ->
5003 if conf.presentation && l.pagey != 0
5004 then
5005 gotoghyll (clamp (pgscale ~-(conf.winh)))
5006 else
5007 let pageno = max 0 (l.pageno-1) in
5008 gotoghyll (getpagey pageno)
5009 | Cmulti ((c, _, coverB) as cl, _) ->
5010 if conf.presentation &&
5011 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5012 then
5013 gotoghyll (clamp (pgscale ~-(conf.winh)))
5014 else
5015 let decr =
5016 if l.pageno = state.pagecount - coverB
5017 then 1
5018 else c
5020 let pageno = max 0 (l.pageno-decr) in
5021 gotoghyll (getpagey pageno)
5022 | Csplit (n, _) ->
5023 let y =
5024 if l.pagecol = 0
5025 then
5026 if l.pageno = 0
5027 then l.pagey
5028 else
5029 let pageno = max 0 (l.pageno-1) in
5030 let pagey, pageh = getpageyh pageno in
5031 pagey + (n-1)*pageh
5032 else
5033 let pagey, pageh = getpageyh l.pageno in
5034 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5036 gotoghyll y
5039 | 61 -> (* = *)
5040 showtext ' ' (describe_location ());
5042 | 119 -> (* w *)
5043 begin match state.layout with
5044 | [] -> ()
5045 | l :: _ ->
5046 doreshape (l.pagew + state.scrollw) l.pageh;
5047 G.postRedisplay "w"
5050 | 39 -> (* ' *)
5051 enterbookmarkmode ()
5053 | 104 | 0xffbe -> (* h|F1 *)
5054 enterhelpmode ()
5056 | 105 -> (* i *)
5057 enterinfomode ()
5059 | 101 when conf.redirectstderr -> (* e *)
5060 entermsgsmode ()
5062 | 109 -> (* m *)
5063 let ondone s =
5064 match state.layout with
5065 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5066 | _ -> ()
5068 enttext ("bookmark: ", "", None, textentry, ondone, true)
5070 | 126 -> (* ~ *)
5071 quickbookmark ();
5072 showtext ' ' "Quick bookmark added";
5074 | 122 -> (* z *)
5075 begin match state.layout with
5076 | l :: _ ->
5077 let rect = getpdimrect l.pagedimno in
5078 let w, h =
5079 if conf.crophack
5080 then
5081 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5082 truncate (1.2 *. (rect.(3) -. rect.(0))))
5083 else
5084 (truncate (rect.(1) -. rect.(0)),
5085 truncate (rect.(3) -. rect.(0)))
5087 let w = truncate ((float w)*.conf.zoom)
5088 and h = truncate ((float h)*.conf.zoom) in
5089 if w != 0 && h != 0
5090 then (
5091 state.anchor <- getanchor ();
5092 doreshape (w + state.scrollw) (h + conf.interpagespace)
5094 G.postRedisplay "z";
5096 | [] -> ()
5099 | 50 when ctrl -> (* ctrl-2 *)
5100 let maxw = getmaxw () in
5101 if maxw > 0.0
5102 then setzoom (maxw /. float conf.winw)
5104 | 60 | 62 -> (* < > *)
5105 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5107 | 91 | 93 -> (* [ ] *)
5108 conf.colorscale <-
5109 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5111 G.postRedisplay "brightness";
5113 | 99 when state.mode = View -> (* c *)
5114 let (c, a, b), z =
5115 match state.prevcolumns with
5116 | None -> (1, 0, 0), 1.0
5117 | Some (columns, z) ->
5118 let cab =
5119 match columns with
5120 | Csplit (c, _) -> -c, 0, 0
5121 | Cmulti ((c, a, b), _) -> c, a, b
5122 | Csingle _ -> 1, 0, 0
5124 cab, z
5126 setcolumns View c a b;
5127 setzoom z;
5129 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5130 setzoom state.prevzoom
5132 | 107 | 0xff52 -> (* k up *)
5133 begin match state.autoscroll with
5134 | None ->
5135 begin match state.mode with
5136 | Birdseye beye -> upbirdseye 1 beye
5137 | _ ->
5138 if ctrl
5139 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5140 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5142 | Some n ->
5143 setautoscrollspeed n false
5146 | 106 | 0xff54 -> (* j down *)
5147 begin match state.autoscroll with
5148 | None ->
5149 begin match state.mode with
5150 | Birdseye beye -> downbirdseye 1 beye
5151 | _ ->
5152 if ctrl
5153 then gotoy_and_clear_text (clamp (conf.winh/2))
5154 else gotoy_and_clear_text (clamp conf.scrollstep)
5156 | Some n ->
5157 setautoscrollspeed n true
5160 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5161 if canpan ()
5162 then
5163 let dx =
5164 if ctrl
5165 then conf.winw / 2
5166 else conf.hscrollstep
5168 let dx = if key = 0xff51 then dx else -dx in
5169 state.x <- state.x + dx;
5170 gotoy_and_clear_text state.y
5171 else (
5172 state.text <- "";
5173 G.postRedisplay "lef/right"
5176 | 0xff55 -> (* prior *)
5177 let y =
5178 if ctrl
5179 then
5180 match state.layout with
5181 | [] -> state.y
5182 | l :: _ -> state.y - l.pagey
5183 else
5184 clamp (pgscale (-conf.winh))
5186 gotoghyll y
5188 | 0xff56 -> (* next *)
5189 let y =
5190 if ctrl
5191 then
5192 match List.rev state.layout with
5193 | [] -> state.y
5194 | l :: _ -> getpagey l.pageno
5195 else
5196 clamp (pgscale conf.winh)
5198 gotoghyll y
5200 | 103 | 0xff50 -> (* g home *)
5201 gotoghyll 0
5202 | 71 | 0xff57 -> (* G end *)
5203 gotoghyll (clamp state.maxy)
5205 | 0xff53 when Wsi.withalt mask -> (* alt-right *)
5206 gotoghyll (getnav 1)
5207 | 0xff51 when Wsi.withalt mask -> (* alt-left *)
5208 gotoghyll (getnav ~-1)
5210 | 114 -> (* r *)
5211 state.anchor <- getanchor ();
5212 opendoc state.path state.password
5214 | 118 when conf.debug -> (* v *)
5215 state.rects <- [];
5216 List.iter (fun l ->
5217 match getopaque l.pageno with
5218 | None -> ()
5219 | Some opaque ->
5220 let x0, y0, x1, y1 = pagebbox opaque in
5221 let a,b = float x0, float y0 in
5222 let c,d = float x1, float y0 in
5223 let e,f = float x1, float y1 in
5224 let h,j = float x0, float y1 in
5225 let rect = (a,b,c,d,e,f,h,j) in
5226 debugrect rect;
5227 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5228 ) state.layout;
5229 G.postRedisplay "v";
5231 | _ ->
5232 vlog "huh? %s" (Wsi.keyname key)
5235 let linknavkeyboard key mask linknav =
5236 let getpage pageno =
5237 let rec loop = function
5238 | [] -> None
5239 | l :: _ when l.pageno = pageno -> Some l
5240 | _ :: rest -> loop rest
5241 in loop state.layout
5243 let doexact (pageno, n) =
5244 match getopaque pageno, getpage pageno with
5245 | Some opaque, Some l ->
5246 if key = 0xff0d
5247 then
5248 let under = getlink opaque n in
5249 G.postRedisplay "link gotounder";
5250 gotounder under;
5251 state.mode <- View;
5252 else
5253 let opt, dir =
5254 match key with
5255 | 0xff50 -> (* home *)
5256 Some (findlink opaque LDfirst), -1
5258 | 0xff57 -> (* end *)
5259 Some (findlink opaque LDlast), 1
5261 | 0xff51 -> (* left *)
5262 Some (findlink opaque (LDleft n)), -1
5264 | 0xff53 -> (* right *)
5265 Some (findlink opaque (LDright n)), 1
5267 | 0xff52 -> (* up *)
5268 Some (findlink opaque (LDup n)), -1
5270 | 0xff54 -> (* down *)
5271 Some (findlink opaque (LDdown n)), 1
5273 | _ -> None, 0
5275 let pwl l dir =
5276 begin match findpwl l.pageno dir with
5277 | Pwlnotfound -> ()
5278 | Pwl pageno ->
5279 let notfound dir =
5280 state.mode <- LinkNav (Ltgendir dir);
5281 let y, h = getpageyh pageno in
5282 let y =
5283 if dir < 0
5284 then y + h - conf.winh
5285 else y
5287 gotoy y
5289 begin match getopaque pageno, getpage pageno with
5290 | Some opaque, Some _ ->
5291 let link =
5292 let ld = if dir > 0 then LDfirst else LDlast in
5293 findlink opaque ld
5295 begin match link with
5296 | Lfound m ->
5297 showlinktype (getlink opaque m);
5298 state.mode <- LinkNav (Ltexact (pageno, m));
5299 G.postRedisplay "linknav jpage";
5300 | _ -> notfound dir
5301 end;
5302 | _ -> notfound dir
5303 end;
5304 end;
5306 begin match opt with
5307 | Some Lnotfound -> pwl l dir;
5308 | Some (Lfound m) ->
5309 if m = n
5310 then pwl l dir
5311 else (
5312 let _, y0, _, y1 = getlinkrect opaque m in
5313 if y0 < l.pagey
5314 then gotopage1 l.pageno y0
5315 else (
5316 let d = fstate.fontsize + 1 in
5317 if y1 - l.pagey > l.pagevh - d
5318 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5319 else G.postRedisplay "linknav";
5321 showlinktype (getlink opaque m);
5322 state.mode <- LinkNav (Ltexact (l.pageno, m));
5325 | None -> viewkeyboard key mask
5326 end;
5327 | _ -> viewkeyboard key mask
5329 if key = 0xff63
5330 then (
5331 state.mode <- View;
5332 G.postRedisplay "leave linknav"
5334 else
5335 match linknav with
5336 | Ltgendir _ -> viewkeyboard key mask
5337 | Ltexact exact -> doexact exact
5340 let keyboard key mask =
5341 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5342 then wcmd "interrupt"
5343 else state.uioh <- state.uioh#key key mask
5346 let birdseyekeyboard key mask
5347 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5348 let incr =
5349 match conf.columns with
5350 | Csingle _ -> 1
5351 | Cmulti ((c, _, _), _) -> c
5352 | Csplit _ -> failwith "bird's eye split mode"
5354 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5355 match key with
5356 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5357 let y, h = getpageyh pageno in
5358 let top = (conf.winh - h) / 2 in
5359 gotoy (max 0 (y - top))
5360 | 0xff0d -> leavebirdseye beye false
5361 | 0xff1b -> leavebirdseye beye true (* escape *)
5362 | 0xff52 -> upbirdseye incr beye (* up *)
5363 | 0xff54 -> downbirdseye incr beye (* down *)
5364 | 0xff51 -> upbirdseye 1 beye (* left *)
5365 | 0xff53 -> downbirdseye 1 beye (* right *)
5367 | 0xff55 -> (* prior *)
5368 begin match state.layout with
5369 | l :: _ ->
5370 if l.pagey != 0
5371 then (
5372 state.mode <- Birdseye (
5373 oconf, leftx, l.pageno, hooverpageno, anchor
5375 gotopage1 l.pageno 0;
5377 else (
5378 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5379 match layout with
5380 | [] -> gotoy (clamp (-conf.winh))
5381 | l :: _ ->
5382 state.mode <- Birdseye (
5383 oconf, leftx, l.pageno, hooverpageno, anchor
5385 gotopage1 l.pageno 0
5388 | [] -> gotoy (clamp (-conf.winh))
5389 end;
5391 | 0xff56 -> (* next *)
5392 begin match List.rev state.layout with
5393 | l :: _ ->
5394 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5395 begin match layout with
5396 | [] ->
5397 let incr = l.pageh - l.pagevh in
5398 if incr = 0
5399 then (
5400 state.mode <-
5401 Birdseye (
5402 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5404 G.postRedisplay "birdseye pagedown";
5406 else gotoy (clamp (incr + conf.interpagespace*2));
5408 | l :: _ ->
5409 state.mode <-
5410 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5411 gotopage1 l.pageno 0;
5414 | [] -> gotoy (clamp conf.winh)
5415 end;
5417 | 0xff50 -> (* home *)
5418 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5419 gotopage1 0 0
5421 | 0xff57 -> (* end *)
5422 let pageno = state.pagecount - 1 in
5423 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5424 if not (pagevisible state.layout pageno)
5425 then
5426 let h =
5427 match List.rev state.pdims with
5428 | [] -> conf.winh
5429 | (_, _, h, _) :: _ -> h
5431 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5432 else G.postRedisplay "birdseye end";
5433 | _ -> viewkeyboard key mask
5436 let drawpage l linkindexbase =
5437 let color =
5438 match state.mode with
5439 | Textentry _ -> scalecolor 0.4
5440 | LinkNav _
5441 | View -> scalecolor 1.0
5442 | Birdseye (_, _, pageno, hooverpageno, _) ->
5443 if l.pageno = hooverpageno
5444 then scalecolor 0.9
5445 else (
5446 if l.pageno = pageno
5447 then scalecolor 1.0
5448 else scalecolor 0.8
5451 drawtiles l color;
5452 begin match getopaque l.pageno with
5453 | Some opaque ->
5454 if tileready l l.pagex l.pagey
5455 then
5456 let x = l.pagedispx - l.pagex
5457 and y = l.pagedispy - l.pagey in
5458 let hlmask =
5459 match conf.columns with
5460 | Csingle _ | Cmulti _ ->
5461 (if conf.hlinks then 1 else 0)
5462 + (if state.glinks
5463 && not (isbirdseye state.mode) then 2 else 0)
5464 | _ -> 0
5466 let s =
5467 match state.mode with
5468 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5469 | _ -> ""
5471 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5472 else 0
5474 | _ -> 0
5475 end;
5478 let scrollindicator () =
5479 let sbw, ph, sh = state.uioh#scrollph in
5480 let sbh, pw, sw = state.uioh#scrollpw in
5482 GlDraw.color (0.64, 0.64, 0.64);
5483 GlDraw.rect
5484 (float (conf.winw - sbw), 0.)
5485 (float conf.winw, float conf.winh)
5487 GlDraw.rect
5488 (0., float (conf.winh - sbh))
5489 (float (conf.winw - state.scrollw - 1), float conf.winh)
5491 GlDraw.color (0.0, 0.0, 0.0);
5493 GlDraw.rect
5494 (float (conf.winw - sbw), ph)
5495 (float conf.winw, ph +. sh)
5497 GlDraw.rect
5498 (pw, float (conf.winh - sbh))
5499 (pw +. sw, float conf.winh)
5503 let showsel () =
5504 match state.mstate with
5505 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5508 | Msel ((x0, y0), (x1, y1)) ->
5509 let rec loop = function
5510 | l :: ls ->
5511 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5512 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5513 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5514 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5515 then
5516 match getopaque l.pageno with
5517 | Some opaque ->
5518 let x0, y0 = pagetranslatepoint l x0 y0 in
5519 let x1, y1 = pagetranslatepoint l x1 y1 in
5520 seltext opaque (x0, y0, x1, y1);
5521 | _ -> ()
5522 else loop ls
5523 | [] -> ()
5525 loop state.layout
5528 let showrects rects =
5529 Gl.enable `blend;
5530 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5531 GlDraw.polygon_mode `both `fill;
5532 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5533 List.iter
5534 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5535 List.iter (fun l ->
5536 if l.pageno = pageno
5537 then (
5538 let dx = float (l.pagedispx - l.pagex) in
5539 let dy = float (l.pagedispy - l.pagey) in
5540 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5541 GlDraw.begins `quads;
5543 GlDraw.vertex2 (x0+.dx, y0+.dy);
5544 GlDraw.vertex2 (x1+.dx, y1+.dy);
5545 GlDraw.vertex2 (x2+.dx, y2+.dy);
5546 GlDraw.vertex2 (x3+.dx, y3+.dy);
5548 GlDraw.ends ();
5550 ) state.layout
5551 ) rects
5553 Gl.disable `blend;
5556 let display () =
5557 GlClear.color (scalecolor2 conf.bgcolor);
5558 GlClear.clear [`color];
5559 let rec loop linkindexbase = function
5560 | l :: rest ->
5561 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5562 loop linkindexbase rest
5563 | [] -> ()
5565 loop 0 state.layout;
5566 let rects =
5567 match state.mode with
5568 | LinkNav (Ltexact (pageno, linkno)) ->
5569 begin match getopaque pageno with
5570 | Some opaque ->
5571 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5572 (pageno, 5, (
5573 float x0, float y0,
5574 float x1, float y0,
5575 float x1, float y1,
5576 float x0, float y1)
5577 ) :: state.rects
5578 | None -> state.rects
5580 | _ -> state.rects
5582 showrects rects;
5583 showsel ();
5584 state.uioh#display;
5585 begin match state.mstate with
5586 | Mzoomrect ((x0, y0), (x1, y1)) ->
5587 Gl.enable `blend;
5588 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5589 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5590 GlDraw.rect (float x0, float y0)
5591 (float x1, float y1);
5592 Gl.disable `blend;
5593 | _ -> ()
5594 end;
5595 enttext ();
5596 scrollindicator ();
5597 Wsi.swapb ();
5600 let zoomrect x y x1 y1 =
5601 let x0 = min x x1
5602 and x1 = max x x1
5603 and y0 = min y y1 in
5604 gotoy (state.y + y0);
5605 state.anchor <- getanchor ();
5606 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5607 let margin =
5608 if state.w < conf.winw - state.scrollw
5609 then (conf.winw - state.scrollw - state.w) / 2
5610 else 0
5612 state.x <- (state.x + margin) - x0;
5613 setzoom zoom;
5614 Wsi.setcursor Wsi.CURSOR_INHERIT;
5615 state.mstate <- Mnone;
5618 let scrollx x =
5619 let winw = conf.winw - state.scrollw - 1 in
5620 let s = float x /. float winw in
5621 let destx = truncate (float (state.w + winw) *. s) in
5622 state.x <- winw - destx;
5623 gotoy_and_clear_text state.y;
5624 state.mstate <- Mscrollx;
5627 let scrolly y =
5628 let s = float y /. float conf.winh in
5629 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5630 gotoy_and_clear_text desty;
5631 state.mstate <- Mscrolly;
5634 let viewmouse button down x y mask =
5635 match button with
5636 | n when (n == 4 || n == 5) && not down ->
5637 if Wsi.withctrl mask
5638 then (
5639 match state.mstate with
5640 | Mzoom (oldn, i) ->
5641 if oldn = n
5642 then (
5643 if i = 2
5644 then
5645 let incr =
5646 match n with
5647 | 5 ->
5648 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5649 | _ ->
5650 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5652 let zoom = conf.zoom -. incr in
5653 setzoom zoom;
5654 state.mstate <- Mzoom (n, 0);
5655 else
5656 state.mstate <- Mzoom (n, i+1);
5658 else state.mstate <- Mzoom (n, 0)
5660 | _ -> state.mstate <- Mzoom (n, 0)
5662 else (
5663 match state.autoscroll with
5664 | Some step -> setautoscrollspeed step (n=4)
5665 | None ->
5666 let incr =
5667 if n = 4
5668 then -conf.scrollstep
5669 else conf.scrollstep
5671 let incr = incr * 2 in
5672 let y = clamp incr in
5673 gotoy_and_clear_text y
5676 | n when (n = 6 || n = 7) && not down && canpan () ->
5677 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5678 gotoy_and_clear_text state.y
5680 | 1 when Wsi.withctrl mask ->
5681 if down
5682 then (
5683 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5684 state.mstate <- Mpan (x, y)
5686 else
5687 state.mstate <- Mnone
5689 | 3 ->
5690 if down
5691 then (
5692 Wsi.setcursor Wsi.CURSOR_CYCLE;
5693 let p = (x, y) in
5694 state.mstate <- Mzoomrect (p, p)
5696 else (
5697 match state.mstate with
5698 | Mzoomrect ((x0, y0), _) ->
5699 if abs (x-x0) > 10 && abs (y - y0) > 10
5700 then zoomrect x0 y0 x y
5701 else (
5702 state.mstate <- Mnone;
5703 Wsi.setcursor Wsi.CURSOR_INHERIT;
5704 G.postRedisplay "kill accidental zoom rect";
5706 | _ ->
5707 Wsi.setcursor Wsi.CURSOR_INHERIT;
5708 state.mstate <- Mnone
5711 | 1 when x > conf.winw - state.scrollw ->
5712 if down
5713 then
5714 let _, position, sh = state.uioh#scrollph in
5715 if y > truncate position && y < truncate (position +. sh)
5716 then state.mstate <- Mscrolly
5717 else scrolly y
5718 else
5719 state.mstate <- Mnone
5721 | 1 when y > conf.winh - state.hscrollh ->
5722 if down
5723 then
5724 let _, position, sw = state.uioh#scrollpw in
5725 if x > truncate position && x < truncate (position +. sw)
5726 then state.mstate <- Mscrollx
5727 else scrollx x
5728 else
5729 state.mstate <- Mnone
5731 | 1 ->
5732 let dest = if down then getunder x y else Unone in
5733 begin match dest with
5734 | Ulinkgoto _
5735 | Ulinkuri _
5736 | Uremote _
5737 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5738 gotounder dest
5740 | Unone when down ->
5741 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5742 state.mstate <- Mpan (x, y);
5744 | Unone | Utext _ ->
5745 if down
5746 then (
5747 if conf.angle mod 360 = 0
5748 then (
5749 state.mstate <- Msel ((x, y), (x, y));
5750 G.postRedisplay "mouse select";
5753 else (
5754 match state.mstate with
5755 | Mnone -> ()
5757 | Mzoom _ | Mscrollx | Mscrolly ->
5758 state.mstate <- Mnone
5760 | Mzoomrect ((x0, y0), _) ->
5761 zoomrect x0 y0 x y
5763 | Mpan _ ->
5764 Wsi.setcursor Wsi.CURSOR_INHERIT;
5765 state.mstate <- Mnone
5767 | Msel ((x0, y0), (x1, y1)) ->
5768 let rec loop = function
5769 | [] -> ()
5770 | l :: rest ->
5771 let inside =
5772 let a0 = l.pagedispy in
5773 let a1 = a0 + l.pagevh in
5774 let b0 = l.pagedispx in
5775 let b1 = b0 + l.pagevw in
5776 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5777 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5779 if inside
5780 then
5781 match getopaque l.pageno with
5782 | Some opaque ->
5783 begin
5784 match Ne.pipe () with
5785 | Ne.Exn exn ->
5786 showtext '!'
5787 (Printf.sprintf
5788 "can not create sel pipe: %s"
5789 (Printexc.to_string exn));
5790 | Ne.Res (r, w) ->
5791 let doclose what fd =
5792 Ne.clo fd (fun msg ->
5793 dolog "%s close failed: %s" what msg)
5796 popen conf.selcmd [r, 0; w, -1];
5797 copysel w opaque;
5798 doclose "pipe/r" r;
5799 G.postRedisplay "copysel";
5800 with exn ->
5801 dolog "can not execute %S: %s"
5802 conf.selcmd (Printexc.to_string exn);
5803 doclose "pipe/r" r;
5804 doclose "pipe/w" w;
5806 | None -> ()
5807 else loop rest
5809 loop state.layout;
5810 Wsi.setcursor Wsi.CURSOR_INHERIT;
5811 state.mstate <- Mnone;
5815 | _ -> ()
5818 let birdseyemouse button down x y mask
5819 (conf, leftx, _, hooverpageno, anchor) =
5820 match button with
5821 | 1 when down ->
5822 let rec loop = function
5823 | [] -> ()
5824 | l :: rest ->
5825 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5826 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5827 then (
5828 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5830 else loop rest
5832 loop state.layout
5833 | 3 -> ()
5834 | _ -> viewmouse button down x y mask
5837 let mouse button down x y mask =
5838 state.uioh <- state.uioh#button button down x y mask;
5841 let motion ~x ~y =
5842 state.uioh <- state.uioh#motion x y
5845 let pmotion ~x ~y =
5846 state.uioh <- state.uioh#pmotion x y;
5849 let uioh = object
5850 method display = ()
5852 method key key mask =
5853 begin match state.mode with
5854 | Textentry textentry -> textentrykeyboard key mask textentry
5855 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5856 | View -> viewkeyboard key mask
5857 | LinkNav linknav -> linknavkeyboard key mask linknav
5858 end;
5859 state.uioh
5861 method button button bstate x y mask =
5862 begin match state.mode with
5863 | LinkNav _
5864 | View -> viewmouse button bstate x y mask
5865 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5866 | Textentry _ -> ()
5867 end;
5868 state.uioh
5870 method motion x y =
5871 begin match state.mode with
5872 | Textentry _ -> ()
5873 | View | Birdseye _ | LinkNav _ ->
5874 match state.mstate with
5875 | Mzoom _ | Mnone -> ()
5877 | Mpan (x0, y0) ->
5878 let dx = x - x0
5879 and dy = y0 - y in
5880 state.mstate <- Mpan (x, y);
5881 if canpan ()
5882 then state.x <- state.x + dx;
5883 let y = clamp dy in
5884 gotoy_and_clear_text y
5886 | Msel (a, _) ->
5887 state.mstate <- Msel (a, (x, y));
5888 G.postRedisplay "motion select";
5890 | Mscrolly ->
5891 let y = min conf.winh (max 0 y) in
5892 scrolly y
5894 | Mscrollx ->
5895 let x = min conf.winw (max 0 x) in
5896 scrollx x
5898 | Mzoomrect (p0, _) ->
5899 state.mstate <- Mzoomrect (p0, (x, y));
5900 G.postRedisplay "motion zoomrect";
5901 end;
5902 state.uioh
5904 method pmotion x y =
5905 begin match state.mode with
5906 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5907 let rec loop = function
5908 | [] ->
5909 if hooverpageno != -1
5910 then (
5911 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5912 G.postRedisplay "pmotion birdseye no hoover";
5914 | l :: rest ->
5915 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5916 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5917 then (
5918 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5919 G.postRedisplay "pmotion birdseye hoover";
5921 else loop rest
5923 loop state.layout
5925 | Textentry _ -> ()
5927 | LinkNav _
5928 | View ->
5929 match state.mstate with
5930 | Mnone -> updateunder x y
5931 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5933 end;
5934 state.uioh
5936 method infochanged _ = ()
5938 method scrollph =
5939 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5940 let p, h = scrollph state.y maxy in
5941 state.scrollw, p, h
5943 method scrollpw =
5944 let winw = conf.winw - state.scrollw - 1 in
5945 let fwinw = float winw in
5946 let sw =
5947 let sw = fwinw /. float state.w in
5948 let sw = fwinw *. sw in
5949 max sw (float conf.scrollh)
5951 let position, sw =
5952 let f = state.w+winw in
5953 let r = float (winw-state.x) /. float f in
5954 let p = fwinw *. r in
5955 p-.sw/.2., sw
5957 let sw =
5958 if position +. sw > fwinw
5959 then fwinw -. position
5960 else sw
5962 state.hscrollh, position, sw
5964 method modehash =
5965 let modename =
5966 match state.mode with
5967 | LinkNav _ -> "links"
5968 | Textentry _ -> "textentry"
5969 | Birdseye _ -> "birdseye"
5970 | View -> "view"
5972 findkeyhash conf modename
5973 end;;
5975 module Config =
5976 struct
5977 open Parser
5979 let fontpath = ref "";;
5981 module KeyMap =
5982 Map.Make (struct type t = (int * int) let compare = compare end);;
5984 let unent s =
5985 let l = String.length s in
5986 let b = Buffer.create l in
5987 unent b s 0 l;
5988 Buffer.contents b;
5991 let home =
5992 try Sys.getenv "HOME"
5993 with exn ->
5994 prerr_endline
5995 ("Can not determine home directory location: " ^
5996 Printexc.to_string exn);
6000 let modifier_of_string = function
6001 | "alt" -> Wsi.altmask
6002 | "shift" -> Wsi.shiftmask
6003 | "ctrl" | "control" -> Wsi.ctrlmask
6004 | "meta" -> Wsi.metamask
6005 | _ -> 0
6008 let key_of_string =
6009 let r = Str.regexp "-" in
6010 fun s ->
6011 let elems = Str.full_split r s in
6012 let f n k m =
6013 let g s =
6014 let m1 = modifier_of_string s in
6015 if m1 = 0
6016 then (Wsi.namekey s, m)
6017 else (k, m lor m1)
6018 in function
6019 | Str.Delim s when n land 1 = 0 -> g s
6020 | Str.Text s -> g s
6021 | Str.Delim _ -> (k, m)
6023 let rec loop n k m = function
6024 | [] -> (k, m)
6025 | x :: xs ->
6026 let k, m = f n k m x in
6027 loop (n+1) k m xs
6029 loop 0 0 0 elems
6032 let keys_of_string =
6033 let r = Str.regexp "[ \t]" in
6034 fun s ->
6035 let elems = Str.split r s in
6036 List.map key_of_string elems
6039 let copykeyhashes c =
6040 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6043 let config_of c attrs =
6044 let apply c k v =
6046 match k with
6047 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6048 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6049 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6050 | "preload" -> { c with preload = bool_of_string v }
6051 | "page-bias" -> { c with pagebias = int_of_string v }
6052 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6053 | "horizontal-scroll-step" ->
6054 { c with hscrollstep = max (int_of_string v) 1 }
6055 | "auto-scroll-step" ->
6056 { c with autoscrollstep = max 0 (int_of_string v) }
6057 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6058 | "crop-hack" -> { c with crophack = bool_of_string v }
6059 | "throttle" ->
6060 let mw =
6061 match String.lowercase v with
6062 | "true" -> Some infinity
6063 | "false" -> None
6064 | f -> Some (float_of_string f)
6066 { c with maxwait = mw}
6067 | "highlight-links" -> { c with hlinks = bool_of_string v }
6068 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6069 | "vertical-margin" ->
6070 { c with interpagespace = max 0 (int_of_string v) }
6071 | "zoom" ->
6072 let zoom = float_of_string v /. 100. in
6073 let zoom = max zoom 0.0 in
6074 { c with zoom = zoom }
6075 | "presentation" -> { c with presentation = bool_of_string v }
6076 | "rotation-angle" -> { c with angle = int_of_string v }
6077 | "width" -> { c with winw = max 20 (int_of_string v) }
6078 | "height" -> { c with winh = max 20 (int_of_string v) }
6079 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6080 | "proportional-display" -> { c with proportional = bool_of_string v }
6081 | "pixmap-cache-size" ->
6082 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6083 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6084 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6085 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6086 | "persistent-location" -> { c with jumpback = bool_of_string v }
6087 | "background-color" -> { c with bgcolor = color_of_string v }
6088 | "scrollbar-in-presentation" ->
6089 { c with scrollbarinpm = bool_of_string v }
6090 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6091 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6092 | "mupdf-store-size" ->
6093 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6094 | "checkers" -> { c with checkers = bool_of_string v }
6095 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6096 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6097 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6098 | "uri-launcher" -> { c with urilauncher = unent v }
6099 | "path-launcher" -> { c with pathlauncher = unent v }
6100 | "color-space" -> { c with colorspace = colorspace_of_string v }
6101 | "invert-colors" -> { c with invert = bool_of_string v }
6102 | "brightness" -> { c with colorscale = float_of_string v }
6103 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6104 | "ghyllscroll" ->
6105 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6106 | "columns" ->
6107 let (n, _, _) as nab = multicolumns_of_string v in
6108 if n < 0
6109 then { c with columns = Csplit (-n, [||]) }
6110 else { c with columns = Cmulti (nab, [||]) }
6111 | "birds-eye-columns" ->
6112 { c with beyecolumns = Some (max (int_of_string v) 2) }
6113 | "selection-command" -> { c with selcmd = unent v }
6114 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6115 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6116 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6117 | _ -> c
6118 with exn ->
6119 prerr_endline ("Error processing attribute (`" ^
6120 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6123 let rec fold c = function
6124 | [] -> c
6125 | (k, v) :: rest ->
6126 let c = apply c k v in
6127 fold c rest
6129 fold { c with keyhashes = copykeyhashes c } attrs;
6132 let fromstring f pos n v d =
6133 try f v
6134 with exn ->
6135 dolog "Error processing attribute (%S=%S) at %d\n%s"
6136 n v pos (Printexc.to_string exn)
6141 let bookmark_of attrs =
6142 let rec fold title page rely visy = function
6143 | ("title", v) :: rest -> fold v page rely visy rest
6144 | ("page", v) :: rest -> fold title v rely visy rest
6145 | ("rely", v) :: rest -> fold title page v visy rest
6146 | ("visy", v) :: rest -> fold title page rely v rest
6147 | _ :: rest -> fold title page rely visy rest
6148 | [] -> title, page, rely, visy
6150 fold "invalid" "0" "0" "0" attrs
6153 let doc_of attrs =
6154 let rec fold path page rely pan visy = function
6155 | ("path", v) :: rest -> fold v page rely pan visy rest
6156 | ("page", v) :: rest -> fold path v rely pan visy rest
6157 | ("rely", v) :: rest -> fold path page v pan visy rest
6158 | ("pan", v) :: rest -> fold path page rely v visy rest
6159 | ("visy", v) :: rest -> fold path page rely pan v rest
6160 | _ :: rest -> fold path page rely pan visy rest
6161 | [] -> path, page, rely, pan, visy
6163 fold "" "0" "0" "0" "0" attrs
6166 let map_of attrs =
6167 let rec fold rs ls = function
6168 | ("out", v) :: rest -> fold v ls rest
6169 | ("in", v) :: rest -> fold rs v rest
6170 | _ :: rest -> fold ls rs rest
6171 | [] -> ls, rs
6173 fold "" "" attrs
6176 let setconf dst src =
6177 dst.scrollbw <- src.scrollbw;
6178 dst.scrollh <- src.scrollh;
6179 dst.icase <- src.icase;
6180 dst.preload <- src.preload;
6181 dst.pagebias <- src.pagebias;
6182 dst.verbose <- src.verbose;
6183 dst.scrollstep <- src.scrollstep;
6184 dst.maxhfit <- src.maxhfit;
6185 dst.crophack <- src.crophack;
6186 dst.autoscrollstep <- src.autoscrollstep;
6187 dst.maxwait <- src.maxwait;
6188 dst.hlinks <- src.hlinks;
6189 dst.underinfo <- src.underinfo;
6190 dst.interpagespace <- src.interpagespace;
6191 dst.zoom <- src.zoom;
6192 dst.presentation <- src.presentation;
6193 dst.angle <- src.angle;
6194 dst.winw <- src.winw;
6195 dst.winh <- src.winh;
6196 dst.savebmarks <- src.savebmarks;
6197 dst.memlimit <- src.memlimit;
6198 dst.proportional <- src.proportional;
6199 dst.texcount <- src.texcount;
6200 dst.sliceheight <- src.sliceheight;
6201 dst.thumbw <- src.thumbw;
6202 dst.jumpback <- src.jumpback;
6203 dst.bgcolor <- src.bgcolor;
6204 dst.scrollbarinpm <- src.scrollbarinpm;
6205 dst.tilew <- src.tilew;
6206 dst.tileh <- src.tileh;
6207 dst.mustoresize <- src.mustoresize;
6208 dst.checkers <- src.checkers;
6209 dst.aalevel <- src.aalevel;
6210 dst.trimmargins <- src.trimmargins;
6211 dst.trimfuzz <- src.trimfuzz;
6212 dst.urilauncher <- src.urilauncher;
6213 dst.colorspace <- src.colorspace;
6214 dst.invert <- src.invert;
6215 dst.colorscale <- src.colorscale;
6216 dst.redirectstderr <- src.redirectstderr;
6217 dst.ghyllscroll <- src.ghyllscroll;
6218 dst.columns <- src.columns;
6219 dst.beyecolumns <- src.beyecolumns;
6220 dst.selcmd <- src.selcmd;
6221 dst.updatecurs <- src.updatecurs;
6222 dst.pathlauncher <- src.pathlauncher;
6223 dst.keyhashes <- copykeyhashes src;
6224 dst.hfsize <- src.hfsize;
6225 dst.hscrollstep <- src.hscrollstep;
6226 dst.pgscale <- src.pgscale;
6229 let get s =
6230 let h = Hashtbl.create 10 in
6231 let dc = { defconf with angle = defconf.angle } in
6232 let rec toplevel v t spos _ =
6233 match t with
6234 | Vdata | Vcdata | Vend -> v
6235 | Vopen ("llppconfig", _, closed) ->
6236 if closed
6237 then v
6238 else { v with f = llppconfig }
6239 | Vopen _ ->
6240 error "unexpected subelement at top level" s spos
6241 | Vclose _ -> error "unexpected close at top level" s spos
6243 and llppconfig v t spos _ =
6244 match t with
6245 | Vdata | Vcdata -> v
6246 | Vend -> error "unexpected end of input in llppconfig" s spos
6247 | Vopen ("defaults", attrs, closed) ->
6248 let c = config_of dc attrs in
6249 setconf dc c;
6250 if closed
6251 then v
6252 else { v with f = defaults }
6254 | Vopen ("ui-font", attrs, closed) ->
6255 let rec getsize size = function
6256 | [] -> size
6257 | ("size", v) :: rest ->
6258 let size =
6259 fromstring int_of_string spos "size" v fstate.fontsize in
6260 getsize size rest
6261 | l -> getsize size l
6263 fstate.fontsize <- getsize fstate.fontsize attrs;
6264 if closed
6265 then v
6266 else { v with f = uifont (Buffer.create 10) }
6268 | Vopen ("doc", attrs, closed) ->
6269 let pathent, spage, srely, span, svisy = doc_of attrs in
6270 let path = unent pathent
6271 and pageno = fromstring int_of_string spos "page" spage 0
6272 and rely = fromstring float_of_string spos "rely" srely 0.0
6273 and pan = fromstring int_of_string spos "pan" span 0
6274 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6275 let c = config_of dc attrs in
6276 let anchor = (pageno, rely, visy) in
6277 if closed
6278 then (Hashtbl.add h path (c, [], pan, anchor); v)
6279 else { v with f = doc path pan anchor c [] }
6281 | Vopen _ ->
6282 error "unexpected subelement in llppconfig" s spos
6284 | Vclose "llppconfig" -> { v with f = toplevel }
6285 | Vclose _ -> error "unexpected close in llppconfig" s spos
6287 and defaults v t spos _ =
6288 match t with
6289 | Vdata | Vcdata -> v
6290 | Vend -> error "unexpected end of input in defaults" s spos
6291 | Vopen ("keymap", attrs, closed) ->
6292 let modename =
6293 try List.assoc "mode" attrs
6294 with Not_found -> "global" in
6295 if closed
6296 then v
6297 else
6298 let ret keymap =
6299 let h = findkeyhash dc modename in
6300 KeyMap.iter (Hashtbl.replace h) keymap;
6301 defaults
6303 { v with f = pkeymap ret KeyMap.empty }
6305 | Vopen (_, _, _) ->
6306 error "unexpected subelement in defaults" s spos
6308 | Vclose "defaults" ->
6309 { v with f = llppconfig }
6311 | Vclose _ -> error "unexpected close in defaults" s spos
6313 and uifont b v t spos epos =
6314 match t with
6315 | Vdata | Vcdata ->
6316 Buffer.add_substring b s spos (epos - spos);
6318 | Vopen (_, _, _) ->
6319 error "unexpected subelement in ui-font" s spos
6320 | Vclose "ui-font" ->
6321 if String.length !fontpath = 0
6322 then fontpath := Buffer.contents b;
6323 { v with f = llppconfig }
6324 | Vclose _ -> error "unexpected close in ui-font" s spos
6325 | Vend -> error "unexpected end of input in ui-font" s spos
6327 and doc path pan anchor c bookmarks v t spos _ =
6328 match t with
6329 | Vdata | Vcdata -> v
6330 | Vend -> error "unexpected end of input in doc" s spos
6331 | Vopen ("bookmarks", _, closed) ->
6332 if closed
6333 then v
6334 else { v with f = pbookmarks path pan anchor c bookmarks }
6336 | Vopen ("keymap", attrs, closed) ->
6337 let modename =
6338 try List.assoc "mode" attrs
6339 with Not_found -> "global"
6341 if closed
6342 then v
6343 else
6344 let ret keymap =
6345 let h = findkeyhash c modename in
6346 KeyMap.iter (Hashtbl.replace h) keymap;
6347 doc path pan anchor c bookmarks
6349 { v with f = pkeymap ret KeyMap.empty }
6351 | Vopen (_, _, _) ->
6352 error "unexpected subelement in doc" s spos
6354 | Vclose "doc" ->
6355 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6356 { v with f = llppconfig }
6358 | Vclose _ -> error "unexpected close in doc" s spos
6360 and pkeymap ret keymap v t spos _ =
6361 match t with
6362 | Vdata | Vcdata -> v
6363 | Vend -> error "unexpected end of input in keymap" s spos
6364 | Vopen ("map", attrs, closed) ->
6365 let r, l = map_of attrs in
6366 let kss = fromstring keys_of_string spos "in" r [] in
6367 let lss = fromstring keys_of_string spos "out" l [] in
6368 let keymap =
6369 match kss with
6370 | [] -> keymap
6371 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6372 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6374 if closed
6375 then { v with f = pkeymap ret keymap }
6376 else
6377 let f () = v in
6378 { v with f = skip "map" f }
6380 | Vopen _ ->
6381 error "unexpected subelement in keymap" s spos
6383 | Vclose "keymap" ->
6384 { v with f = ret keymap }
6386 | Vclose _ -> error "unexpected close in keymap" s spos
6388 and pbookmarks path pan anchor c bookmarks v t spos _ =
6389 match t with
6390 | Vdata | Vcdata -> v
6391 | Vend -> error "unexpected end of input in bookmarks" s spos
6392 | Vopen ("item", attrs, closed) ->
6393 let titleent, spage, srely, svisy = bookmark_of attrs in
6394 let page = fromstring int_of_string spos "page" spage 0
6395 and rely = fromstring float_of_string spos "rely" srely 0.0
6396 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6397 let bookmarks =
6398 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6400 if closed
6401 then { v with f = pbookmarks path pan anchor c bookmarks }
6402 else
6403 let f () = v in
6404 { v with f = skip "item" f }
6406 | Vopen _ ->
6407 error "unexpected subelement in bookmarks" s spos
6409 | Vclose "bookmarks" ->
6410 { v with f = doc path pan anchor c bookmarks }
6412 | Vclose _ -> error "unexpected close in bookmarks" s spos
6414 and skip tag f v t spos _ =
6415 match t with
6416 | Vdata | Vcdata -> v
6417 | Vend ->
6418 error ("unexpected end of input in skipped " ^ tag) s spos
6419 | Vopen (tag', _, closed) ->
6420 if closed
6421 then v
6422 else
6423 let f' () = { v with f = skip tag f } in
6424 { v with f = skip tag' f' }
6425 | Vclose ctag ->
6426 if tag = ctag
6427 then f ()
6428 else error ("unexpected close in skipped " ^ tag) s spos
6431 parse { f = toplevel; accu = () } s;
6432 h, dc;
6435 let do_load f ic =
6437 let len = in_channel_length ic in
6438 let s = String.create len in
6439 really_input ic s 0 len;
6440 f s;
6441 with
6442 | Parse_error (msg, s, pos) ->
6443 let subs = subs s pos in
6444 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6445 failwith ("parse error: " ^ s)
6447 | exn ->
6448 failwith ("config load error: " ^ Printexc.to_string exn)
6451 let defconfpath =
6452 let dir =
6454 let dir = Filename.concat home ".config" in
6455 if Sys.is_directory dir then dir else home
6456 with _ -> home
6458 Filename.concat dir "llpp.conf"
6461 let confpath = ref defconfpath;;
6463 let load1 f =
6464 if Sys.file_exists !confpath
6465 then
6466 match
6467 (try Some (open_in_bin !confpath)
6468 with exn ->
6469 prerr_endline
6470 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6471 Printexc.to_string exn);
6472 None
6474 with
6475 | Some ic ->
6476 let success =
6478 f (do_load get ic)
6479 with exn ->
6480 prerr_endline
6481 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6482 Printexc.to_string exn);
6483 false
6485 close_in ic;
6486 success
6488 | None -> false
6489 else
6490 f (Hashtbl.create 0, defconf)
6493 let load () =
6494 let f (h, dc) =
6495 let pc, pb, px, pa =
6497 Hashtbl.find h (Filename.basename state.path)
6498 with Not_found -> dc, [], 0, emptyanchor
6500 setconf defconf dc;
6501 setconf conf pc;
6502 state.bookmarks <- pb;
6503 state.x <- px;
6504 state.scrollw <- conf.scrollbw;
6505 if conf.jumpback
6506 then state.anchor <- pa;
6507 cbput state.hists.nav pa;
6508 true
6510 load1 f
6513 let add_attrs bb always dc c =
6514 let ob s a b =
6515 if always || a != b
6516 then Printf.bprintf bb "\n %s='%b'" s a
6517 and oi s a b =
6518 if always || a != b
6519 then Printf.bprintf bb "\n %s='%d'" s a
6520 and oI s a b =
6521 if always || a != b
6522 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6523 and oz s a b =
6524 if always || a <> b
6525 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6526 and oF s a b =
6527 if always || a <> b
6528 then Printf.bprintf bb "\n %s='%f'" s a
6529 and oc s a b =
6530 if always || a <> b
6531 then
6532 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6533 and oC s a b =
6534 if always || a <> b
6535 then
6536 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6537 and oR s a b =
6538 if always || a <> b
6539 then
6540 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6541 and os s a b =
6542 if always || a <> b
6543 then
6544 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6545 and og s a b =
6546 if always || a <> b
6547 then
6548 match a with
6549 | None -> ()
6550 | Some (_N, _A, _B) ->
6551 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6552 and oW s a b =
6553 if always || a <> b
6554 then
6555 let v =
6556 match a with
6557 | None -> "false"
6558 | Some f ->
6559 if f = infinity
6560 then "true"
6561 else string_of_float f
6563 Printf.bprintf bb "\n %s='%s'" s v
6564 and oco s a b =
6565 if always || a <> b
6566 then
6567 match a with
6568 | Cmulti ((n, a, b), _) when n > 1 ->
6569 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6570 | Csplit (n, _) when n > 1 ->
6571 Printf.bprintf bb "\n %s='%d'" s ~-n
6572 | _ -> ()
6573 and obeco s a b =
6574 if always || a <> b
6575 then
6576 match a with
6577 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6578 | _ -> ()
6580 let w, h =
6581 if always
6582 then dc.winw, dc.winh
6583 else
6584 match state.fullscreen with
6585 | Some wh -> wh
6586 | None -> c.winw, c.winh
6588 oi "width" w dc.winw;
6589 oi "height" h dc.winh;
6590 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6591 oi "scroll-handle-height" c.scrollh dc.scrollh;
6592 ob "case-insensitive-search" c.icase dc.icase;
6593 ob "preload" c.preload dc.preload;
6594 oi "page-bias" c.pagebias dc.pagebias;
6595 oi "scroll-step" c.scrollstep dc.scrollstep;
6596 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6597 ob "max-height-fit" c.maxhfit dc.maxhfit;
6598 ob "crop-hack" c.crophack dc.crophack;
6599 oW "throttle" c.maxwait dc.maxwait;
6600 ob "highlight-links" c.hlinks dc.hlinks;
6601 ob "under-cursor-info" c.underinfo dc.underinfo;
6602 oi "vertical-margin" c.interpagespace dc.interpagespace;
6603 oz "zoom" c.zoom dc.zoom;
6604 ob "presentation" c.presentation dc.presentation;
6605 oi "rotation-angle" c.angle dc.angle;
6606 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6607 ob "proportional-display" c.proportional dc.proportional;
6608 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6609 oi "tex-count" c.texcount dc.texcount;
6610 oi "slice-height" c.sliceheight dc.sliceheight;
6611 oi "thumbnail-width" c.thumbw dc.thumbw;
6612 ob "persistent-location" c.jumpback dc.jumpback;
6613 oc "background-color" c.bgcolor dc.bgcolor;
6614 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6615 oi "tile-width" c.tilew dc.tilew;
6616 oi "tile-height" c.tileh dc.tileh;
6617 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6618 ob "checkers" c.checkers dc.checkers;
6619 oi "aalevel" c.aalevel dc.aalevel;
6620 ob "trim-margins" c.trimmargins dc.trimmargins;
6621 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6622 os "uri-launcher" c.urilauncher dc.urilauncher;
6623 os "path-launcher" c.pathlauncher dc.pathlauncher;
6624 oC "color-space" c.colorspace dc.colorspace;
6625 ob "invert-colors" c.invert dc.invert;
6626 oF "brightness" c.colorscale dc.colorscale;
6627 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6628 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6629 oco "columns" c.columns dc.columns;
6630 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6631 os "selection-command" c.selcmd dc.selcmd;
6632 ob "update-cursor" c.updatecurs dc.updatecurs;
6633 oi "hint-font-size" c.hfsize dc.hfsize;
6634 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6635 oF "page-scroll-scale" c.pgscale dc.pgscale;
6638 let keymapsbuf always dc c =
6639 let bb = Buffer.create 16 in
6640 let rec loop = function
6641 | [] -> ()
6642 | (modename, h) :: rest ->
6643 let dh = findkeyhash dc modename in
6644 if always || h <> dh
6645 then (
6646 if Hashtbl.length h > 0
6647 then (
6648 if Buffer.length bb > 0
6649 then Buffer.add_char bb '\n';
6650 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6651 Hashtbl.iter (fun i o ->
6652 let isdifferent = always ||
6654 let dO = Hashtbl.find dh i in
6655 dO <> o
6656 with Not_found -> true
6658 if isdifferent
6659 then
6660 let addkm (k, m) =
6661 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6662 if Wsi.withalt m then Buffer.add_string bb "alt-";
6663 if Wsi.withshift m then Buffer.add_string bb "shift-";
6664 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6665 Buffer.add_string bb (Wsi.keyname k);
6667 let addkms l =
6668 let rec loop = function
6669 | [] -> ()
6670 | km :: [] -> addkm km
6671 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6673 loop l
6675 Buffer.add_string bb "<map in='";
6676 addkm i;
6677 match o with
6678 | KMinsrt km ->
6679 Buffer.add_string bb "' out='";
6680 addkm km;
6681 Buffer.add_string bb "'/>\n"
6683 | KMinsrl kms ->
6684 Buffer.add_string bb "' out='";
6685 addkms kms;
6686 Buffer.add_string bb "'/>\n"
6688 | KMmulti (ins, kms) ->
6689 Buffer.add_char bb ' ';
6690 addkms ins;
6691 Buffer.add_string bb "' out='";
6692 addkms kms;
6693 Buffer.add_string bb "'/>\n"
6694 ) h;
6695 Buffer.add_string bb "</keymap>";
6698 loop rest
6700 loop c.keyhashes;
6704 let save () =
6705 let uifontsize = fstate.fontsize in
6706 let bb = Buffer.create 32768 in
6707 let f (h, dc) =
6708 let dc = if conf.bedefault then conf else dc in
6709 Buffer.add_string bb "<llppconfig>\n";
6711 if String.length !fontpath > 0
6712 then
6713 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6714 uifontsize
6715 !fontpath
6716 else (
6717 if uifontsize <> 14
6718 then
6719 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6722 Buffer.add_string bb "<defaults ";
6723 add_attrs bb true dc dc;
6724 let kb = keymapsbuf true dc dc in
6725 if Buffer.length kb > 0
6726 then (
6727 Buffer.add_string bb ">\n";
6728 Buffer.add_buffer bb kb;
6729 Buffer.add_string bb "\n</defaults>\n";
6731 else Buffer.add_string bb "/>\n";
6733 let adddoc path pan anchor c bookmarks =
6734 if bookmarks == [] && c = dc && anchor = emptyanchor
6735 then ()
6736 else (
6737 Printf.bprintf bb "<doc path='%s'"
6738 (enent path 0 (String.length path));
6740 if anchor <> emptyanchor
6741 then (
6742 let n, rely, visy = anchor in
6743 Printf.bprintf bb " page='%d'" n;
6744 if rely > 1e-6
6745 then
6746 Printf.bprintf bb " rely='%f'" rely
6748 if abs_float visy > 1e-6
6749 then
6750 Printf.bprintf bb " visy='%f'" visy
6754 if pan != 0
6755 then Printf.bprintf bb " pan='%d'" pan;
6757 add_attrs bb false dc c;
6758 let kb = keymapsbuf false dc c in
6760 begin match bookmarks with
6761 | [] ->
6762 if Buffer.length kb > 0
6763 then (
6764 Buffer.add_string bb ">\n";
6765 Buffer.add_buffer bb kb;
6766 Buffer.add_string bb "\n</doc>\n";
6768 else Buffer.add_string bb "/>\n"
6769 | _ ->
6770 Buffer.add_string bb ">\n<bookmarks>\n";
6771 List.iter (fun (title, _level, (page, rely, visy)) ->
6772 Printf.bprintf bb
6773 "<item title='%s' page='%d'"
6774 (enent title 0 (String.length title))
6775 page
6777 if rely > 1e-6
6778 then
6779 Printf.bprintf bb " rely='%f'" rely
6781 if abs_float visy > 1e-6
6782 then
6783 Printf.bprintf bb " visy='%f'" visy
6785 Buffer.add_string bb "/>\n";
6786 ) bookmarks;
6787 Buffer.add_string bb "</bookmarks>";
6788 if Buffer.length kb > 0
6789 then (
6790 Buffer.add_string bb "\n";
6791 Buffer.add_buffer bb kb;
6793 Buffer.add_string bb "\n</doc>\n";
6794 end;
6798 let pan, conf =
6799 match state.mode with
6800 | Birdseye (c, pan, _, _, _) ->
6801 let beyecolumns =
6802 match conf.columns with
6803 | Cmulti ((c, _, _), _) -> Some c
6804 | Csingle _ -> None
6805 | Csplit _ -> None
6806 and columns =
6807 match c.columns with
6808 | Cmulti (c, _) -> Cmulti (c, [||])
6809 | Csingle _ -> Csingle [||]
6810 | Csplit _ -> failwith "quit from bird's eye while split"
6812 pan, { c with beyecolumns = beyecolumns; columns = columns }
6813 | _ -> state.x, conf
6815 let basename = Filename.basename state.path in
6816 adddoc basename pan (getanchor ())
6817 (let conf =
6818 let autoscrollstep =
6819 match state.autoscroll with
6820 | Some step -> step
6821 | None -> conf.autoscrollstep
6823 match state.mode with
6824 | Birdseye (bc, _, _, _, _) ->
6825 { conf with
6826 zoom = bc.zoom;
6827 presentation = bc.presentation;
6828 interpagespace = bc.interpagespace;
6829 maxwait = bc.maxwait;
6830 autoscrollstep = autoscrollstep }
6831 | _ -> { conf with autoscrollstep = autoscrollstep }
6832 in conf)
6833 (if conf.savebmarks then state.bookmarks else []);
6835 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6836 if basename <> path
6837 then adddoc path x anchor c bookmarks
6838 ) h;
6839 Buffer.add_string bb "</llppconfig>\n";
6840 true;
6842 if load1 f && Buffer.length bb > 0
6843 then
6845 let tmp = !confpath ^ ".tmp" in
6846 let oc = open_out_bin tmp in
6847 Buffer.output_buffer oc bb;
6848 close_out oc;
6849 Unix.rename tmp !confpath;
6850 with exn ->
6851 prerr_endline
6852 ("error while saving configuration: " ^ Printexc.to_string exn)
6854 end;;
6856 let () =
6857 let trimcachepath = ref "" in
6858 Arg.parse
6859 (Arg.align
6860 [("-p", Arg.String (fun s -> state.password <- s) ,
6861 "<password> Set password");
6863 ("-f", Arg.String (fun s -> Config.fontpath := s),
6864 "<path> Set path to the user interface font");
6866 ("-c", Arg.String (fun s -> Config.confpath := s),
6867 "<path> Set path to the configuration file");
6869 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6870 "<path> Set path to the trim cache file");
6872 ("-v", Arg.Unit (fun () ->
6873 Printf.printf
6874 "%s\nconfiguration path: %s\n"
6875 (version ())
6876 Config.defconfpath
6878 exit 0), " Print version and exit");
6881 (fun s -> state.path <- s)
6882 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6884 if String.length state.path = 0
6885 then (prerr_endline "file name missing"; exit 1);
6887 if not (Config.load ())
6888 then prerr_endline "failed to load configuration";
6890 let globalkeyhash = findkeyhash conf "global" in
6891 let wsfd, winw, winh = Wsi.init (object
6892 method expose =
6893 if nogeomcmds state.geomcmds || platform == Posx
6894 then display ()
6895 else (
6896 GlClear.color (scalecolor2 conf.bgcolor);
6897 GlClear.clear [`color];
6899 method display = display ()
6900 method reshape w h = reshape w h
6901 method mouse b d x y m = mouse b d x y m
6902 method motion x y = state.mpos <- (x, y); motion x y
6903 method pmotion x y = state.mpos <- (x, y); pmotion x y
6904 method key k m =
6905 let mascm = m land (
6906 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6907 ) in
6908 match state.keystate with
6909 | KSnone ->
6910 let km = k, mascm in
6911 begin
6912 match
6913 let modehash = state.uioh#modehash in
6914 try Hashtbl.find modehash km
6915 with Not_found ->
6916 try Hashtbl.find globalkeyhash km
6917 with Not_found -> KMinsrt (k, m)
6918 with
6919 | KMinsrt (k, m) -> keyboard k m
6920 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6921 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6923 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6924 List.iter (fun (k, m) -> keyboard k m) insrt;
6925 state.keystate <- KSnone
6926 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6927 state.keystate <- KSinto (keys, insrt)
6928 | _ ->
6929 state.keystate <- KSnone
6931 method enter x y = state.mpos <- (x, y); pmotion x y
6932 method leave = state.mpos <- (-1, -1)
6933 method quit = raise Quit
6934 end) conf.winw conf.winh (platform = Posx) in
6936 state.wsfd <- wsfd;
6938 if not (
6939 List.exists GlMisc.check_extension
6940 [ "GL_ARB_texture_rectangle"
6941 ; "GL_EXT_texture_recangle"
6942 ; "GL_NV_texture_rectangle" ]
6944 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6946 let cr, sw =
6947 match Ne.pipe () with
6948 | Ne.Exn exn ->
6949 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6950 exit 1
6951 | Ne.Res rw -> rw
6952 and sr, cw =
6953 match Ne.pipe () with
6954 | Ne.Exn exn ->
6955 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6956 exit 1
6957 | Ne.Res rw -> rw
6960 cloexec cr;
6961 cloexec sw;
6962 cloexec sr;
6963 cloexec cw;
6965 setcheckers conf.checkers;
6966 redirectstderr ();
6968 init (cr, cw) (
6969 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6970 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6971 !Config.fontpath, !trimcachepath
6973 state.sr <- sr;
6974 state.sw <- sw;
6975 state.text <- "Opening " ^ state.path;
6976 reshape winw winh;
6977 opendoc state.path state.password;
6978 state.uioh <- uioh;
6980 let rec loop deadline =
6981 let r =
6982 match state.errfd with
6983 | None -> [state.sr; state.wsfd]
6984 | Some fd -> [state.sr; state.wsfd; fd]
6986 if state.redisplay
6987 then (
6988 state.redisplay <- false;
6989 display ();
6991 let timeout =
6992 let now = now () in
6993 if deadline > now
6994 then (
6995 if deadline = infinity
6996 then ~-.1.0
6997 else max 0.0 (deadline -. now)
6999 else 0.0
7001 let r, _, _ =
7002 try Unix.select r [] [] timeout
7003 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7005 begin match r with
7006 | [] ->
7007 state.ghyll None;
7008 let newdeadline =
7009 if state.ghyll == noghyll
7010 then
7011 match state.autoscroll with
7012 | Some step when step != 0 ->
7013 let y = state.y + step in
7014 let y =
7015 if y < 0
7016 then state.maxy
7017 else if y >= state.maxy then 0 else y
7019 gotoy y;
7020 if state.mode = View
7021 then state.text <- "";
7022 deadline +. 0.01
7023 | _ -> infinity
7024 else deadline +. 0.01
7026 loop newdeadline
7028 | l ->
7029 let rec checkfds = function
7030 | [] -> ()
7031 | fd :: rest when fd = state.sr ->
7032 let cmd = readcmd state.sr in
7033 act cmd;
7034 checkfds rest
7036 | fd :: rest when fd = state.wsfd ->
7037 Wsi.readresp fd;
7038 checkfds rest
7040 | fd :: rest ->
7041 let s = String.create 80 in
7042 let n = Unix.read fd s 0 80 in
7043 if conf.redirectstderr
7044 then (
7045 Buffer.add_substring state.errmsgs s 0 n;
7046 state.newerrmsgs <- true;
7047 state.redisplay <- true;
7049 else (
7050 prerr_string (String.sub s 0 n);
7051 flush stderr;
7053 checkfds rest
7055 checkfds l;
7056 let newdeadline =
7057 let deadline1 =
7058 if deadline = infinity
7059 then now () +. 0.01
7060 else deadline
7062 match state.autoscroll with
7063 | Some step when step != 0 -> deadline1
7064 | _ -> if state.ghyll == noghyll then infinity else deadline1
7066 loop newdeadline
7067 end;
7070 loop infinity;
7071 with Quit ->
7072 Config.save ();