Do not close pipe twice
[llpp.git] / main.ml
blobeaa86f5dc4e16d77b3379fe1a2907d6aa1aff62b
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)
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 fontpath = string
36 and memsize = int
37 and aalevel = int
38 and irect = (int * int * int * int)
39 and trimparams = (trimmargins * irect)
40 and colorspace = | Rgb | Bgr | Gray
43 type link =
44 | Lnotfound
45 | Lfound of int
46 and linkdir =
47 | LDfirst
48 | LDlast
49 | LDfirstvisible of (int * int * int)
50 | LDleft of int
51 | LDright of int
52 | LDdown of int
53 | LDup of int
56 type pagewithlinks =
57 | Pwlnotfound
58 | Pwl of int
61 type keymap =
62 | KMinsrt of key
63 | KMinsrl of key list
64 | KMmulti of key list * key list
65 and key = int * int
66 and keyhash = (key, keymap) Hashtbl.t
67 and keystate =
68 | KSnone
69 | KSinto of (key list * key list)
72 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
73 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
75 type pipe = (Unix.file_descr * Unix.file_descr);;
77 external init : pipe -> params -> unit = "ml_init";;
78 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
79 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
80 external getpdimrect : int -> float array = "ml_getpdimrect";;
81 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
82 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
83 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
84 external measurestr : int -> string -> float = "ml_measure_string";;
85 external getmaxw : unit -> float = "ml_getmaxw";;
86 external postprocess : opaque -> int -> int -> int -> (int * string) -> int =
87 "ml_postprocess";;
88 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
89 external platform : unit -> platform = "ml_platform";;
90 external setaalevel : int -> unit = "ml_setaalevel";;
91 external realloctexts : int -> bool = "ml_realloctexts";;
92 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
93 external findlink : opaque -> linkdir -> link = "ml_findlink";;
94 external getlink : opaque -> int -> under = "ml_getlink";;
95 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
96 external getlinkcount : opaque -> int = "ml_getlinkcount";;
97 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
98 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
100 let platform_to_string = function
101 | Punknown -> "unknown"
102 | Plinux -> "Linux"
103 | Posx -> "OSX"
104 | Psun -> "Sun"
105 | Pfreebsd -> "FreeBSD"
106 | Pdragonflybsd -> "DragonflyBSD"
107 | Popenbsd -> "OpenBSD"
108 | Pnetbsd -> "NetBSD"
109 | Pcygwin -> "Cygwin"
112 let platform = platform ();;
114 let popen cmd fda =
115 if platform = Pcygwin
116 then (
117 let sh = "/bin/sh" in
118 let args = [|sh; "-c"; cmd|] in
119 let rec std si so se = function
120 | [] -> si, so, se
121 | (fd, 0) :: rest -> std fd so se rest
122 | (fd, -1) :: rest ->
123 Unix.set_close_on_exec fd;
124 std si so se rest
125 | (_, n) :: _ ->
126 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
128 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
129 ignore (Unix.create_process sh args si so se)
131 else popen cmd fda;
134 type x = int
135 and y = int
136 and tilex = int
137 and tiley = int
138 and tileparams = (x * y * width * height * tilex * tiley)
141 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
143 type mpos = int * int
144 and mstate =
145 | Msel of (mpos * mpos)
146 | Mpan of mpos
147 | Mscrolly | Mscrollx
148 | Mzoom of (int * int)
149 | Mzoomrect of (mpos * mpos)
150 | Mnone
153 type textentry = string * string * onhist option * onkey * ondone
154 and onkey = string -> int -> te
155 and ondone = string -> unit
156 and histcancel = unit -> unit
157 and onhist = ((histcmd -> string) * histcancel)
158 and histcmd = HCnext | HCprev | HCfirst | HClast
159 and te =
160 | TEstop
161 | TEdone of string
162 | TEcont of string
163 | TEswitch of textentry
166 type 'a circbuf =
167 { store : 'a array
168 ; mutable rc : int
169 ; mutable wc : int
170 ; mutable len : int
174 let bound v minv maxv =
175 max minv (min maxv v);
178 let cbnew n v =
179 { store = Array.create n v
180 ; rc = 0
181 ; wc = 0
182 ; len = 0
186 let drawstring size x y s =
187 Gl.enable `blend;
188 Gl.enable `texture_2d;
189 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
190 ignore (drawstr size x y s);
191 Gl.disable `blend;
192 Gl.disable `texture_2d;
195 let drawstring1 size x y s =
196 drawstr size x y s;
199 let drawstring2 size x y fmt =
200 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
203 let cbcap b = Array.length b.store;;
205 let cbput b v =
206 let cap = cbcap b in
207 b.store.(b.wc) <- v;
208 b.wc <- (b.wc + 1) mod cap;
209 b.rc <- b.wc;
210 b.len <- min (b.len + 1) cap;
213 let cbempty b = b.len = 0;;
215 let cbgetg b circular dir =
216 if cbempty b
217 then b.store.(0)
218 else
219 let rc = b.rc + dir in
220 let rc =
221 if circular
222 then (
223 if rc = -1
224 then b.len-1
225 else (
226 if rc = b.len
227 then 0
228 else rc
231 else max 0 (min rc (b.len-1))
233 b.rc <- rc;
234 b.store.(rc);
237 let cbget b = cbgetg b false;;
238 let cbgetc b = cbgetg b true;;
240 type page =
241 { pageno : int
242 ; pagedimno : int
243 ; pagew : int
244 ; pageh : int
245 ; pagex : int
246 ; pagey : int
247 ; pagevw : int
248 ; pagevh : int
249 ; pagedispx : int
250 ; pagedispy : int
251 ; pagecol : int
255 let debugl l =
256 dolog "l %d dim=%d {" l.pageno l.pagedimno;
257 dolog " WxH %dx%d" l.pagew l.pageh;
258 dolog " vWxH %dx%d" l.pagevw l.pagevh;
259 dolog " pagex,y %d,%d" l.pagex l.pagey;
260 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
261 dolog " column %d" l.pagecol;
262 dolog "}";
265 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
266 dolog "rect {";
267 dolog " x0,y0=(% f, % f)" x0 y0;
268 dolog " x1,y1=(% f, % f)" x1 y1;
269 dolog " x2,y2=(% f, % f)" x2 y2;
270 dolog " x3,y3=(% f, % f)" x3 y3;
271 dolog "}";
274 type multicolumns = multicol * pagegeom
275 and splitcolumns = columncount * pagegeom
276 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
277 and multicol = columncount * covercount * covercount
278 and pdimno = int
279 and columncount = int
280 and covercount = int;;
282 type conf =
283 { mutable scrollbw : int
284 ; mutable scrollh : int
285 ; mutable icase : bool
286 ; mutable preload : bool
287 ; mutable pagebias : int
288 ; mutable verbose : bool
289 ; mutable debug : bool
290 ; mutable scrollstep : int
291 ; mutable maxhfit : bool
292 ; mutable crophack : bool
293 ; mutable autoscrollstep : int
294 ; mutable maxwait : float option
295 ; mutable hlinks : bool
296 ; mutable underinfo : bool
297 ; mutable interpagespace : interpagespace
298 ; mutable zoom : float
299 ; mutable presentation : bool
300 ; mutable angle : angle
301 ; mutable winw : int
302 ; mutable winh : int
303 ; mutable savebmarks : bool
304 ; mutable proportional : proportional
305 ; mutable trimmargins : trimmargins
306 ; mutable trimfuzz : irect
307 ; mutable memlimit : memsize
308 ; mutable texcount : texcount
309 ; mutable sliceheight : sliceheight
310 ; mutable thumbw : width
311 ; mutable jumpback : bool
312 ; mutable bgcolor : float * float * float
313 ; mutable bedefault : bool
314 ; mutable scrollbarinpm : bool
315 ; mutable tilew : int
316 ; mutable tileh : int
317 ; mutable mustoresize : memsize
318 ; mutable checkers : bool
319 ; mutable aalevel : int
320 ; mutable urilauncher : string
321 ; mutable pathlauncher : string
322 ; mutable colorspace : colorspace
323 ; mutable invert : bool
324 ; mutable colorscale : float
325 ; mutable redirectstderr : bool
326 ; mutable ghyllscroll : (int * int * int) option
327 ; mutable columns : columns
328 ; mutable beyecolumns : columncount option
329 ; mutable selcmd : string
330 ; mutable updatecurs : bool
331 ; mutable keyhashes : (string * keyhash) list
333 and columns =
334 | Csingle
335 | Cmulti of multicolumns
336 | Csplit of splitcolumns
339 type anchor = pageno * top;;
341 type outline = string * int * anchor;;
343 type rect = float * float * float * float * float * float * float * float;;
345 type tile = opaque * pixmapsize * elapsed
346 and elapsed = float;;
347 type pagemapkey = pageno * gen;;
348 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
349 and row = int
350 and col = int;;
352 let emptyanchor = (0, 0.0);;
354 type infochange = | Memused | Docinfo | Pdim;;
356 class type uioh = object
357 method display : unit
358 method key : int -> int -> uioh
359 method button : int -> bool -> int -> int -> int -> uioh
360 method motion : int -> int -> uioh
361 method pmotion : int -> int -> uioh
362 method infochanged : infochange -> unit
363 method scrollpw : (int * float * float)
364 method scrollph : (int * float * float)
365 method modehash : keyhash
366 end;;
368 type mode =
369 | Birdseye of (conf * leftx * pageno * pageno * anchor)
370 | Textentry of (textentry * onleave)
371 | View
372 | LinkNav of linktarget
373 and onleave = leavetextentrystatus -> unit
374 and leavetextentrystatus = | Cancel | Confirm
375 and helpitem = string * int * action
376 and action =
377 | Noaction
378 | Action of (uioh -> uioh)
379 and linktarget =
380 | Ltexact of (pageno * int)
381 | Ltgendir of int
384 let isbirdseye = function Birdseye _ -> true | _ -> false;;
385 let istextentry = function Textentry _ -> true | _ -> false;;
387 type currently =
388 | Idle
389 | Loading of (page * gen)
390 | Tiling of (
391 page * opaque * colorspace * angle * gen * col * row * width * height
393 | Outlining of outline list
396 let emptykeyhash = Hashtbl.create 0;;
397 let nouioh : uioh = object (self)
398 method display = ()
399 method key _ _ = self
400 method button _ _ _ _ _ = self
401 method motion _ _ = self
402 method pmotion _ _ = self
403 method infochanged _ = ()
404 method scrollpw = (0, nan, nan)
405 method scrollph = (0, nan, nan)
406 method modehash = emptykeyhash
407 end;;
409 type state =
410 { mutable sr : Unix.file_descr
411 ; mutable sw : Unix.file_descr
412 ; mutable wsfd : Unix.file_descr
413 ; mutable errfd : Unix.file_descr option
414 ; mutable stderr : Unix.file_descr
415 ; mutable errmsgs : Buffer.t
416 ; mutable newerrmsgs : bool
417 ; mutable w : int
418 ; mutable x : int
419 ; mutable y : int
420 ; mutable scrollw : int
421 ; mutable hscrollh : int
422 ; mutable anchor : anchor
423 ; mutable ranchors : (string * string * anchor) list
424 ; mutable maxy : int
425 ; mutable layout : page list
426 ; pagemap : (pagemapkey, opaque) Hashtbl.t
427 ; tilemap : (tilemapkey, tile) Hashtbl.t
428 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
429 ; mutable pdims : (pageno * width * height * leftx) list
430 ; mutable pagecount : int
431 ; mutable currently : currently
432 ; mutable mstate : mstate
433 ; mutable searchpattern : string
434 ; mutable rects : (pageno * recttype * rect) list
435 ; mutable rects1 : (pageno * recttype * rect) list
436 ; mutable text : string
437 ; mutable fullscreen : (width * height) option
438 ; mutable mode : mode
439 ; mutable uioh : uioh
440 ; mutable outlines : outline array
441 ; mutable bookmarks : outline list
442 ; mutable path : string
443 ; mutable password : string
444 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
445 ; mutable memused : memsize
446 ; mutable gen : gen
447 ; mutable throttle : (page list * int * float) option
448 ; mutable autoscroll : int option
449 ; mutable ghyll : (int option -> unit)
450 ; mutable help : helpitem array
451 ; mutable docinfo : (int * string) list
452 ; mutable texid : GlTex.texture_id option
453 ; hists : hists
454 ; mutable prevzoom : float
455 ; mutable progress : float
456 ; mutable redisplay : bool
457 ; mutable mpos : mpos
458 ; mutable keystate : keystate
459 ; mutable glinks : bool
461 and hists =
462 { pat : string circbuf
463 ; pag : string circbuf
464 ; nav : anchor circbuf
465 ; sel : string circbuf
469 let defconf =
470 { scrollbw = 7
471 ; scrollh = 12
472 ; icase = true
473 ; preload = true
474 ; pagebias = 0
475 ; verbose = false
476 ; debug = false
477 ; scrollstep = 24
478 ; maxhfit = true
479 ; crophack = false
480 ; autoscrollstep = 2
481 ; maxwait = None
482 ; hlinks = false
483 ; underinfo = false
484 ; interpagespace = 2
485 ; zoom = 1.0
486 ; presentation = false
487 ; angle = 0
488 ; winw = 900
489 ; winh = 900
490 ; savebmarks = true
491 ; proportional = true
492 ; trimmargins = false
493 ; trimfuzz = (0,0,0,0)
494 ; memlimit = 32 lsl 20
495 ; texcount = 256
496 ; sliceheight = 24
497 ; thumbw = 76
498 ; jumpback = true
499 ; bgcolor = (0.5, 0.5, 0.5)
500 ; bedefault = false
501 ; scrollbarinpm = true
502 ; tilew = 2048
503 ; tileh = 2048
504 ; mustoresize = 256 lsl 20
505 ; checkers = true
506 ; aalevel = 8
507 ; urilauncher =
508 (match platform with
509 | Plinux | Pfreebsd | Pdragonflybsd
510 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
511 | Posx -> "open \"%s\""
512 | Pcygwin -> "cygstart \"%s\""
513 | Punknown -> "echo %s")
514 ; pathlauncher = "lp \"%s\""
515 ; selcmd =
516 (match platform with
517 | Plinux | Pfreebsd | Pdragonflybsd
518 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
519 | Posx -> "pbcopy"
520 | Pcygwin -> "wsel"
521 | Punknown -> "cat")
522 ; colorspace = Rgb
523 ; invert = false
524 ; colorscale = 1.0
525 ; redirectstderr = false
526 ; ghyllscroll = None
527 ; columns = Csingle
528 ; beyecolumns = None
529 ; updatecurs = false
530 ; keyhashes =
531 let mk n = (n, Hashtbl.create 1) in
532 [ mk "global"
533 ; mk "info"
534 ; mk "help"
535 ; mk "outline"
536 ; mk "listview"
537 ; mk "birdseye"
538 ; mk "textentry"
539 ; mk "links"
544 let findkeyhash c name =
545 try List.assoc name c.keyhashes
546 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
549 let conf = { defconf with angle = defconf.angle };;
551 type fontstate =
552 { mutable fontsize : int
553 ; mutable wwidth : float
554 ; mutable maxrows : int
558 let fstate =
559 { fontsize = 14
560 ; wwidth = nan
561 ; maxrows = -1
565 let setfontsize n =
566 fstate.fontsize <- n;
567 fstate.wwidth <- measurestr fstate.fontsize "w";
568 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
571 let geturl s =
572 let colonpos = try String.index s ':' with Not_found -> -1 in
573 let len = String.length s in
574 if colonpos >= 0 && colonpos + 3 < len
575 then (
576 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
577 then
578 let schemestartpos =
579 try String.rindex_from s colonpos ' '
580 with Not_found -> -1
582 let scheme =
583 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
585 match scheme with
586 | "http" | "ftp" | "mailto" ->
587 let epos =
588 try String.index_from s colonpos ' '
589 with Not_found -> len
591 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
592 | _ -> ""
593 else ""
595 else ""
598 let gotouri uri =
599 if String.length conf.urilauncher = 0
600 then print_endline uri
601 else (
602 let url = geturl uri in
603 if String.length url = 0
604 then print_endline uri
605 else
606 let re = Str.regexp "%s" in
607 let command = Str.global_replace re url conf.urilauncher in
608 try popen command []
609 with exn ->
610 Printf.eprintf
611 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
612 flush stderr;
616 let version () =
617 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
618 (platform_to_string platform) Sys.word_size Sys.ocaml_version
621 let makehelp () =
622 let strings = version () :: "" :: Help.keys in
623 Array.of_list (
624 List.map (fun s ->
625 let url = geturl s in
626 if String.length url > 0
627 then (s, 0, Action (fun u -> gotouri url; u))
628 else (s, 0, Noaction)
629 ) strings);
632 let noghyll _ = ();;
633 let firstgeomcmds = "", [];;
635 let state =
636 { sr = Unix.stdin
637 ; sw = Unix.stdin
638 ; wsfd = Unix.stdin
639 ; errfd = None
640 ; stderr = Unix.stderr
641 ; errmsgs = Buffer.create 0
642 ; newerrmsgs = false
643 ; x = 0
644 ; y = 0
645 ; w = 0
646 ; scrollw = 0
647 ; hscrollh = 0
648 ; anchor = emptyanchor
649 ; ranchors = []
650 ; layout = []
651 ; maxy = max_int
652 ; tilelru = Queue.create ()
653 ; pagemap = Hashtbl.create 10
654 ; tilemap = Hashtbl.create 10
655 ; pdims = []
656 ; pagecount = 0
657 ; currently = Idle
658 ; mstate = Mnone
659 ; rects = []
660 ; rects1 = []
661 ; text = ""
662 ; mode = View
663 ; fullscreen = None
664 ; searchpattern = ""
665 ; outlines = [||]
666 ; bookmarks = []
667 ; path = ""
668 ; password = ""
669 ; geomcmds = firstgeomcmds
670 ; hists =
671 { nav = cbnew 10 (0, 0.0)
672 ; pat = cbnew 10 ""
673 ; pag = cbnew 10 ""
674 ; sel = cbnew 10 ""
676 ; memused = 0
677 ; gen = 0
678 ; throttle = None
679 ; autoscroll = None
680 ; ghyll = noghyll
681 ; help = makehelp ()
682 ; docinfo = []
683 ; texid = None
684 ; prevzoom = 1.0
685 ; progress = -1.0
686 ; uioh = nouioh
687 ; redisplay = true
688 ; mpos = (-1, -1)
689 ; keystate = KSnone
690 ; glinks = false
694 let vlog fmt =
695 if conf.verbose
696 then
697 Printf.kprintf prerr_endline fmt
698 else
699 Printf.kprintf ignore fmt
702 let launchpath () =
703 if String.length conf.pathlauncher = 0
704 then print_endline state.path
705 else (
706 let re = Str.regexp "%s" in
707 let command = Str.global_replace re state.path conf.pathlauncher in
708 try popen command []
709 with exn ->
710 Printf.eprintf
711 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
712 flush stderr;
716 module Ne = struct
717 type 'a t = | Res of 'a | Exn of exn;;
719 let pipe () =
720 try Res (Unix.pipe ())
721 with exn -> Exn exn
724 let clo fd f =
725 try Unix.close fd
726 with exn -> f (Printexc.to_string exn)
729 let dup fd =
730 try Res (Unix.dup fd)
731 with exn -> Exn exn
734 let dup2 fd1 fd2 =
735 try Res (Unix.dup2 fd1 fd2)
736 with exn -> Exn exn
738 end;;
740 let redirectstderr () =
741 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
742 if conf.redirectstderr
743 then
744 match Ne.pipe () with
745 | Ne.Exn exn ->
746 dolog "failed to create stderr redirection pipes: %s"
747 (Printexc.to_string exn)
749 | Ne.Res (r, w) ->
750 begin match Ne.dup Unix.stderr with
751 | Ne.Exn exn ->
752 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
753 Ne.clo r (clofail "pipe/r");
754 Ne.clo w (clofail "pipe/w");
756 | Ne.Res dupstderr ->
757 begin match Ne.dup2 w Unix.stderr with
758 | Ne.Exn exn ->
759 dolog "failed to dup2 to stderr: %s"
760 (Printexc.to_string exn);
761 Ne.clo dupstderr (clofail "stderr duplicate");
762 Ne.clo r (clofail "redir pipe/r");
763 Ne.clo w (clofail "redir pipe/w");
765 | Ne.Res () ->
766 state.stderr <- dupstderr;
767 state.errfd <- Some r;
768 end;
770 else (
771 state.newerrmsgs <- false;
772 begin match state.errfd with
773 | Some fd ->
774 begin match Ne.dup2 state.stderr Unix.stderr with
775 | Ne.Exn exn ->
776 dolog "failed to dup2 original stderr: %s"
777 (Printexc.to_string exn)
778 | Ne.Res () ->
779 Ne.clo fd (clofail "dup of stderr");
780 Unix.dup2 state.stderr Unix.stderr;
781 state.errfd <- None;
782 end;
783 | None -> ()
784 end;
785 prerr_string (Buffer.contents state.errmsgs);
786 flush stderr;
787 Buffer.clear state.errmsgs;
791 module G =
792 struct
793 let postRedisplay who =
794 if conf.verbose
795 then prerr_endline ("redisplay for " ^ who);
796 state.redisplay <- true;
798 end;;
800 let getopaque pageno =
801 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
802 with Not_found -> None
805 let putopaque pageno opaque =
806 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
809 let pagetranslatepoint l x y =
810 let dy = y - l.pagedispy in
811 let y = dy + l.pagey in
812 let dx = x - l.pagedispx in
813 let x = dx + l.pagex in
814 (x, y);
817 let getunder x y =
818 let rec f = function
819 | l :: rest ->
820 begin match getopaque l.pageno with
821 | Some opaque ->
822 let x0 = l.pagedispx in
823 let x1 = x0 + l.pagevw in
824 let y0 = l.pagedispy in
825 let y1 = y0 + l.pagevh in
826 if y >= y0 && y <= y1 && x >= x0 && x <= x1
827 then
828 let px, py = pagetranslatepoint l x y in
829 match whatsunder opaque px py with
830 | Unone -> f rest
831 | under -> under
832 else f rest
833 | _ ->
834 f rest
836 | [] -> Unone
838 f state.layout
841 let showtext c s =
842 state.text <- Printf.sprintf "%c%s" c s;
843 G.postRedisplay "showtext";
846 let undertext = function
847 | Unone -> "none"
848 | Ulinkuri s -> s
849 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
850 | Utext s -> "font: " ^ s
851 | Uunexpected s -> "unexpected: " ^ s
852 | Ulaunch s -> "launch: " ^ s
853 | Unamed s -> "named: " ^ s
854 | Uremote (filename, pageno) ->
855 Printf.sprintf "%s: page %d" filename (pageno+1)
858 let updateunder x y =
859 match getunder x y with
860 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
861 | Ulinkuri uri ->
862 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
863 Wsi.setcursor Wsi.CURSOR_INFO
864 | Ulinkgoto (pageno, _) ->
865 if conf.underinfo
866 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
867 Wsi.setcursor Wsi.CURSOR_INFO
868 | Utext s ->
869 if conf.underinfo then showtext 'f' ("ont: " ^ s);
870 Wsi.setcursor Wsi.CURSOR_TEXT
871 | Uunexpected s ->
872 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
873 Wsi.setcursor Wsi.CURSOR_INHERIT
874 | Ulaunch s ->
875 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
876 Wsi.setcursor Wsi.CURSOR_INHERIT
877 | Unamed s ->
878 if conf.underinfo then showtext 'n' ("amed: " ^ s);
879 Wsi.setcursor Wsi.CURSOR_INHERIT
880 | Uremote (filename, pageno) ->
881 if conf.underinfo then showtext 'r'
882 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
883 Wsi.setcursor Wsi.CURSOR_INFO
886 let showlinktype under =
887 if conf.underinfo
888 then
889 match under with
890 | Unone -> ()
891 | under ->
892 let s = undertext under in
893 showtext ' ' s
896 let addchar s c =
897 let b = Buffer.create (String.length s + 1) in
898 Buffer.add_string b s;
899 Buffer.add_char b c;
900 Buffer.contents b;
903 let colorspace_of_string s =
904 match String.lowercase s with
905 | "rgb" -> Rgb
906 | "bgr" -> Bgr
907 | "gray" -> Gray
908 | _ -> failwith "invalid colorspace"
911 let int_of_colorspace = function
912 | Rgb -> 0
913 | Bgr -> 1
914 | Gray -> 2
917 let colorspace_of_int = function
918 | 0 -> Rgb
919 | 1 -> Bgr
920 | 2 -> Gray
921 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
924 let colorspace_to_string = function
925 | Rgb -> "rgb"
926 | Bgr -> "bgr"
927 | Gray -> "gray"
930 let intentry_with_suffix text key =
931 let c =
932 if key >= 32 && key < 127
933 then Char.chr key
934 else '\000'
936 match Char.lowercase c with
937 | '0' .. '9' ->
938 let text = addchar text c in
939 TEcont text
941 | 'k' | 'm' | 'g' ->
942 let text = addchar text c in
943 TEcont text
945 | _ ->
946 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
947 TEcont text
950 let multicolumns_to_string (n, a, b) =
951 if a = 0 && b = 0
952 then Printf.sprintf "%d" n
953 else Printf.sprintf "%d,%d,%d" n a b;
956 let multicolumns_of_string s =
958 (int_of_string s, 0, 0)
959 with _ ->
960 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
963 let readcmd fd =
964 let s = "xxxx" in
965 let n = Unix.read fd s 0 4 in
966 if n != 4 then failwith "incomplete read(len)";
967 let len = 0
968 lor (Char.code s.[0] lsl 24)
969 lor (Char.code s.[1] lsl 16)
970 lor (Char.code s.[2] lsl 8)
971 lor (Char.code s.[3] lsl 0)
973 let s = String.create len in
974 let n = Unix.read fd s 0 len in
975 if n != len then failwith "incomplete read(data)";
979 let btod b = if b then 1 else 0;;
981 let wcmd fmt =
982 let b = Buffer.create 16 in
983 Buffer.add_string b "llll";
984 Printf.kbprintf
985 (fun b ->
986 let s = Buffer.contents b in
987 let n = String.length s in
988 let len = n - 4 in
989 (* dolog "wcmd %S" (String.sub s 4 len); *)
990 s.[0] <- Char.chr ((len lsr 24) land 0xff);
991 s.[1] <- Char.chr ((len lsr 16) land 0xff);
992 s.[2] <- Char.chr ((len lsr 8) land 0xff);
993 s.[3] <- Char.chr (len land 0xff);
994 let n' = Unix.write state.sw s 0 n in
995 if n' != n then failwith "write failed";
996 ) b fmt;
999 let calcips h =
1000 if conf.presentation
1001 then
1002 let d = conf.winh - h in
1003 max 0 ((d + 1) / 2)
1004 else
1005 conf.interpagespace
1008 let calcheight () =
1009 let rec f pn ph pi fh l =
1010 match l with
1011 | (n, _, h, _) :: rest ->
1012 let ips = calcips h in
1013 let fh =
1014 if conf.presentation
1015 then fh+ips
1016 else (
1017 if isbirdseye state.mode && pn = 0
1018 then fh + ips
1019 else fh
1022 let fh = fh + ((n - pn) * (ph + pi)) in
1023 f n h ips fh rest;
1025 | [] ->
1026 let inc =
1027 if conf.presentation || (isbirdseye state.mode && pn = 0)
1028 then 0
1029 else -pi
1031 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
1032 max 0 fh
1034 let fh = f 0 0 0 0 state.pdims in
1038 let calcheight () =
1039 match conf.columns with
1040 | Csingle -> calcheight ()
1041 | Cmulti ((c, _, _), b) ->
1042 let rec loop y h n =
1043 if n < 0
1044 then loop y h (n+1)
1045 else (
1046 if n = Array.length b
1047 then y + h
1048 else
1049 let (_, _, y', (_, _, h', _)) = b.(n) in
1050 let y = min y y'
1051 and h = max h h' in
1052 loop y h (n+1)
1055 loop max_int 0 (((Array.length b - 1) / c) * c)
1056 | Csplit (_, b) ->
1057 if Array.length b > 0
1058 then
1059 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1060 y + h
1061 else 0
1064 let getpageyh pageno =
1065 let rec f pn ph pi y l =
1066 match l with
1067 | (n, _, h, _) :: rest ->
1068 let ips = calcips h in
1069 if n >= pageno
1070 then
1071 let h = if n = pageno then h else ph in
1072 if conf.presentation && n = pageno
1073 then
1074 y + (pageno - pn) * (ph + pi) + pi, h
1075 else
1076 y + (pageno - pn) * (ph + pi), h
1077 else
1078 let y = y + (if conf.presentation then pi else 0) in
1079 let y = y + (n - pn) * (ph + pi) in
1080 f n h ips y rest
1082 | [] ->
1083 y + (pageno - pn) * (ph + pi), ph
1085 f 0 0 0 0 state.pdims
1088 let getpageyh pageno =
1089 match conf.columns with
1090 | Csingle -> getpageyh pageno
1091 | Cmulti (_, b) ->
1092 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1093 y, h
1094 | Csplit (c, b) ->
1095 let n = pageno*c in
1096 let (_, _, y, (_, _, h, _)) = b.(n) in
1097 y, h
1100 let getpagedim pageno =
1101 let rec f ppdim l =
1102 match l with
1103 | (n, _, _, _) as pdim :: rest ->
1104 if n >= pageno
1105 then (if n = pageno then pdim else ppdim)
1106 else f pdim rest
1108 | [] -> ppdim
1110 f (-1, -1, -1, -1) state.pdims
1113 let getpagey pageno = fst (getpageyh pageno);;
1115 let nogeomcmds cmds =
1116 match cmds with
1117 | s, [] -> String.length s = 0
1118 | _ -> false
1121 let layout1 y sh =
1122 let sh = sh - state.hscrollh in
1123 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1124 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1125 match pdims with
1126 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1127 let ips = calcips h in
1128 let yinc =
1129 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1130 then ips
1131 else 0
1133 (w, h, ips, xoff), rest, pdimno + 1, yinc
1134 | _ ->
1135 prev, pdims, pdimno, 0
1137 let dy = dy + yinc in
1138 let py = py + yinc in
1139 if pageno = state.pagecount || dy >= sh
1140 then
1141 accu
1142 else
1143 let vy = y + dy in
1144 if py + h <= vy - yinc
1145 then
1146 let py = py + h + ips in
1147 let dy = max 0 (py - y) in
1148 f ~pageno:(pageno+1)
1149 ~pdimno
1150 ~prev:curr
1153 ~pdims:rest
1154 ~accu
1155 else
1156 let pagey = vy - py in
1157 let pagevh = h - pagey in
1158 let pagevh = min (sh - dy) pagevh in
1159 let off = if yinc > 0 then py - vy else 0 in
1160 let py = py + h + ips in
1161 let pagex, dx =
1162 let xoff = xoff +
1163 if state.w < conf.winw - state.scrollw
1164 then (conf.winw - state.scrollw - state.w) / 2
1165 else 0
1167 let dispx = xoff + state.x in
1168 if dispx < 0
1169 then (-dispx, 0)
1170 else (0, dispx)
1172 let pagevw =
1173 let lw = w - pagex in
1174 min lw (conf.winw - state.scrollw)
1176 let e =
1177 { pageno = pageno
1178 ; pagedimno = pdimno
1179 ; pagew = w
1180 ; pageh = h
1181 ; pagex = pagex
1182 ; pagey = pagey + off
1183 ; pagevw = pagevw
1184 ; pagevh = pagevh - off
1185 ; pagedispx = dx
1186 ; pagedispy = dy + off
1187 ; pagecol = 0
1190 let accu = e :: accu in
1191 f ~pageno:(pageno+1)
1192 ~pdimno
1193 ~prev:curr
1195 ~dy:(dy+pagevh+ips)
1196 ~pdims:rest
1197 ~accu
1199 let accu =
1201 ~pageno:0
1202 ~pdimno:~-1
1203 ~prev:(0,0,0,0)
1204 ~py:0
1205 ~dy:0
1206 ~pdims:state.pdims
1207 ~accu:[]
1209 List.rev accu
1212 let layoutN ((columns, coverA, coverB), b) y sh =
1213 let sh = sh - state.hscrollh in
1214 let rec fold accu n =
1215 if n = Array.length b
1216 then accu
1217 else
1218 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1219 if (vy - y) > sh &&
1220 (n = coverA - 1
1221 || n = state.pagecount - coverB
1222 || (n - coverA) mod columns = columns - 1)
1223 then accu
1224 else
1225 let accu =
1226 if vy + h > y
1227 then
1228 let pagey = max 0 (y - vy) in
1229 let pagedispy = if pagey > 0 then 0 else vy - y in
1230 let pagedispx, pagex =
1231 let pdx =
1232 if n = coverA - 1 || n = state.pagecount - coverB
1233 then state.x + (conf.winw - state.scrollw - w) / 2
1234 else dx + xoff + state.x
1236 if pdx < 0
1237 then 0, -pdx
1238 else pdx, 0
1240 let pagevw =
1241 let vw = conf.winw - state.scrollw - pagedispx in
1242 let pw = w - pagex in
1243 min vw pw
1245 let pagevh = min (h - pagey) (sh - pagedispy) in
1246 if pagevw > 0 && pagevh > 0
1247 then
1248 let e =
1249 { pageno = n
1250 ; pagedimno = pdimno
1251 ; pagew = w
1252 ; pageh = h
1253 ; pagex = pagex
1254 ; pagey = pagey
1255 ; pagevw = pagevw
1256 ; pagevh = pagevh
1257 ; pagedispx = pagedispx
1258 ; pagedispy = pagedispy
1259 ; pagecol = 0
1262 e :: accu
1263 else
1264 accu
1265 else
1266 accu
1268 fold accu (n+1)
1270 List.rev (fold [] 0)
1273 let layoutS (columns, b) y sh =
1274 let sh = sh - state.hscrollh in
1275 let rec fold accu n =
1276 if n = Array.length b
1277 then accu
1278 else
1279 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1280 if (vy - y) > sh
1281 then accu
1282 else
1283 let accu =
1284 if vy + pageh > y
1285 then
1286 let x = xoff + state.x in
1287 let pagey = max 0 (y - vy) in
1288 let pagedispy = if pagey > 0 then 0 else vy - y in
1289 let pagedispx, pagex =
1290 if px = 0
1291 then (
1292 if x < 0
1293 then 0, -x
1294 else x, 0
1296 else (
1297 let px = px - x in
1298 if px < 0
1299 then -px, 0
1300 else 0, px
1303 let pagevw =
1304 let vw = conf.winw - pagedispx - state.scrollw in
1305 let pw = pagew - pagex in
1306 min vw pw
1308 let pagevw = min pagevw (pagew/columns) in
1309 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1310 if pagevw > 0 && pagevh > 0
1311 then
1312 let e =
1313 { pageno = n/columns
1314 ; pagedimno = pdimno
1315 ; pagew = pagew
1316 ; pageh = pageh
1317 ; pagex = pagex
1318 ; pagey = pagey
1319 ; pagevw = pagevw
1320 ; pagevh = pagevh
1321 ; pagedispx = pagedispx
1322 ; pagedispy = pagedispy
1323 ; pagecol = n mod columns
1326 e :: accu
1327 else
1328 accu
1329 else
1330 accu
1332 fold accu (n+1)
1334 List.rev (fold [] 0)
1337 let layout y sh =
1338 if nogeomcmds state.geomcmds
1339 then
1340 match conf.columns with
1341 | Csingle -> layout1 y sh
1342 | Cmulti c -> layoutN c y sh
1343 | Csplit s -> layoutS s y sh
1344 else []
1347 let clamp incr =
1348 let y = state.y + incr in
1349 let y = max 0 y in
1350 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1354 let itertiles l f =
1355 let tilex = l.pagex mod conf.tilew in
1356 let tiley = l.pagey mod conf.tileh in
1358 let col = l.pagex / conf.tilew in
1359 let row = l.pagey / conf.tileh in
1361 let rec rowloop row y0 dispy h =
1362 if h = 0
1363 then ()
1364 else (
1365 let dh = conf.tileh - y0 in
1366 let dh = min h dh in
1367 let rec colloop col x0 dispx w =
1368 if w = 0
1369 then ()
1370 else (
1371 let dw = conf.tilew - x0 in
1372 let dw = min w dw in
1374 f col row dispx dispy x0 y0 dw dh;
1375 colloop (col+1) 0 (dispx+dw) (w-dw)
1378 colloop col tilex l.pagedispx l.pagevw;
1379 rowloop (row+1) 0 (dispy+dh) (h-dh)
1382 if l.pagevw > 0 && l.pagevh > 0
1383 then rowloop row tiley l.pagedispy l.pagevh;
1386 let gettileopaque l col row =
1387 let key =
1388 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1390 try Some (Hashtbl.find state.tilemap key)
1391 with Not_found -> None
1394 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1395 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1396 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1399 let drawtiles l color =
1400 GlDraw.color color;
1401 let f col row x y tilex tiley w h =
1402 match gettileopaque l col row with
1403 | Some (opaque, _, t) ->
1404 let params = x, y, w, h, tilex, tiley in
1405 if conf.invert
1406 then (
1407 Gl.enable `blend;
1408 GlFunc.blend_func `zero `one_minus_src_color;
1410 drawtile params opaque;
1411 if conf.invert
1412 then Gl.disable `blend;
1413 if conf.debug
1414 then (
1415 let s = Printf.sprintf
1416 "%d[%d,%d] %f sec"
1417 l.pageno col row t
1419 let w = measurestr fstate.fontsize s in
1420 GlMisc.push_attrib [`current];
1421 GlDraw.color (0.0, 0.0, 0.0);
1422 GlDraw.rect
1423 (float (x-2), float (y-2))
1424 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1425 GlDraw.color (1.0, 1.0, 1.0);
1426 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1427 GlMisc.pop_attrib ();
1430 | _ ->
1431 let w =
1432 let lw = conf.winw - state.scrollw - x in
1433 min lw w
1434 and h =
1435 let lh = conf.winh - y in
1436 min lh h
1438 Gl.enable `texture_2d;
1439 begin match state.texid with
1440 | Some id ->
1441 GlTex.bind_texture `texture_2d id;
1442 let x0 = float x
1443 and y0 = float y
1444 and x1 = float (x+w)
1445 and y1 = float (y+h) in
1447 let tw = float w /. 64.0
1448 and th = float h /. 64.0 in
1449 let tx0 = float tilex /. 64.0
1450 and ty0 = float tiley /. 64.0 in
1451 let tx1 = tx0 +. tw
1452 and ty1 = ty0 +. th in
1453 GlDraw.begins `quads;
1454 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1455 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1456 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1457 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1458 GlDraw.ends ();
1460 Gl.disable `texture_2d;
1461 | None ->
1462 GlDraw.color (1.0, 1.0, 1.0);
1463 GlDraw.rect
1464 (float x, float y)
1465 (float (x+w), float (y+h));
1466 end;
1467 if w > 128 && h > fstate.fontsize + 10
1468 then (
1469 GlDraw.color (0.0, 0.0, 0.0);
1470 let c, r =
1471 if conf.verbose
1472 then (col*conf.tilew, row*conf.tileh)
1473 else col, row
1475 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1477 GlDraw.color color;
1479 itertiles l f
1482 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1484 let tilevisible1 l x y =
1485 let ax0 = l.pagex
1486 and ax1 = l.pagex + l.pagevw
1487 and ay0 = l.pagey
1488 and ay1 = l.pagey + l.pagevh in
1490 let bx0 = x
1491 and by0 = y in
1492 let bx1 = min (bx0 + conf.tilew) l.pagew
1493 and by1 = min (by0 + conf.tileh) l.pageh in
1495 let rx0 = max ax0 bx0
1496 and ry0 = max ay0 by0
1497 and rx1 = min ax1 bx1
1498 and ry1 = min ay1 by1 in
1500 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1501 nonemptyintersection
1504 let tilevisible layout n x y =
1505 let rec findpageinlayout m = function
1506 | l :: rest when l.pageno = n ->
1507 tilevisible1 l x y || (
1508 match conf.columns with
1509 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1510 | _ -> false
1512 | _ :: rest -> findpageinlayout 0 rest
1513 | [] -> false
1515 findpageinlayout 0 layout;
1518 let tileready l x y =
1519 tilevisible1 l x y &&
1520 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1523 let tilepage n p layout =
1524 let rec loop = function
1525 | l :: rest ->
1526 if l.pageno = n
1527 then
1528 let f col row _ _ _ _ _ _ =
1529 if state.currently = Idle
1530 then
1531 match gettileopaque l col row with
1532 | Some _ -> ()
1533 | None ->
1534 let x = col*conf.tilew
1535 and y = row*conf.tileh in
1536 let w =
1537 let w = l.pagew - x in
1538 min w conf.tilew
1540 let h =
1541 let h = l.pageh - y in
1542 min h conf.tileh
1544 wcmd "tile %s %d %d %d %d" p x y w h;
1545 state.currently <-
1546 Tiling (
1547 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1548 conf.tilew, conf.tileh
1551 itertiles l f;
1552 else
1553 loop rest
1555 | [] -> ()
1557 if nogeomcmds state.geomcmds
1558 then loop layout;
1561 let preloadlayout visiblepages =
1562 let presentation = conf.presentation in
1563 let interpagespace = conf.interpagespace in
1564 let maxy = state.maxy in
1565 conf.presentation <- false;
1566 conf.interpagespace <- 0;
1567 state.maxy <- calcheight ();
1568 let y =
1569 match visiblepages with
1570 | [] -> if state.y >= maxy then maxy else 0
1571 | l :: _ -> getpagey l.pageno + l.pagey
1573 let y = if y < conf.winh then 0 else y - conf.winh in
1574 let h = state.y - y + conf.winh*3 in
1575 let pages = layout y h in
1576 conf.presentation <- presentation;
1577 conf.interpagespace <- interpagespace;
1578 state.maxy <- maxy;
1579 pages;
1582 let load pages =
1583 let rec loop pages =
1584 if state.currently != Idle
1585 then ()
1586 else
1587 match pages with
1588 | l :: rest ->
1589 begin match getopaque l.pageno with
1590 | None ->
1591 wcmd "page %d %d" l.pageno l.pagedimno;
1592 state.currently <- Loading (l, state.gen);
1593 | Some opaque ->
1594 tilepage l.pageno opaque pages;
1595 loop rest
1596 end;
1597 | _ -> ()
1599 if nogeomcmds state.geomcmds
1600 then loop pages
1603 let preload pages =
1604 load pages;
1605 if conf.preload && state.currently = Idle
1606 then load (preloadlayout pages);
1609 let layoutready layout =
1610 let rec fold all ls =
1611 all && match ls with
1612 | l :: rest ->
1613 let seen = ref false in
1614 let allvisible = ref true in
1615 let foo col row _ _ _ _ _ _ =
1616 seen := true;
1617 allvisible := !allvisible &&
1618 begin match gettileopaque l col row with
1619 | Some _ -> true
1620 | None -> false
1623 itertiles l foo;
1624 fold (!seen && !allvisible) rest
1625 | [] -> true
1627 let alltilesvisible = fold true layout in
1628 alltilesvisible;
1631 let gotoy y =
1632 let y = bound y 0 state.maxy in
1633 let y, layout, proceed =
1634 match conf.maxwait with
1635 | Some time when state.ghyll == noghyll ->
1636 begin match state.throttle with
1637 | None ->
1638 let layout = layout y conf.winh in
1639 let ready = layoutready layout in
1640 if not ready
1641 then (
1642 load layout;
1643 state.throttle <- Some (layout, y, now ());
1645 else G.postRedisplay "gotoy showall (None)";
1646 y, layout, ready
1647 | Some (_, _, started) ->
1648 let dt = now () -. started in
1649 if dt > time
1650 then (
1651 state.throttle <- None;
1652 let layout = layout y conf.winh in
1653 load layout;
1654 G.postRedisplay "maxwait";
1655 y, layout, true
1657 else -1, [], false
1660 | _ ->
1661 let layout = layout y conf.winh in
1662 if true || layoutready layout
1663 then G.postRedisplay "gotoy ready";
1664 y, layout, true
1666 if proceed
1667 then (
1668 state.y <- y;
1669 state.layout <- layout;
1670 begin match state.mode with
1671 | LinkNav (Ltexact (pageno, linkno)) ->
1672 let rec loop = function
1673 | [] ->
1674 state.mode <- LinkNav (Ltgendir 0)
1675 | l :: _ when l.pageno = pageno ->
1676 begin match getopaque pageno with
1677 | None ->
1678 state.mode <- LinkNav (Ltgendir 0)
1679 | Some opaque ->
1680 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1681 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1682 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1683 then state.mode <- LinkNav (Ltgendir 0)
1685 | _ :: rest -> loop rest
1687 loop layout
1688 | _ -> ()
1689 end;
1690 begin match state.mode with
1691 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1692 if not (pagevisible layout pageno)
1693 then (
1694 match state.layout with
1695 | [] -> ()
1696 | l :: _ ->
1697 state.mode <- Birdseye (
1698 conf, leftx, l.pageno, hooverpageno, anchor
1701 | LinkNav (Ltgendir dir as lt) ->
1702 let linknav =
1703 let rec loop = function
1704 | [] -> lt
1705 | l :: rest ->
1706 match getopaque l.pageno with
1707 | None -> loop rest
1708 | Some opaque ->
1709 let link =
1710 let ld =
1711 if dir = 0
1712 then LDfirstvisible (l.pagex, l.pagey, dir)
1713 else (
1714 if dir > 0 then LDfirst else LDlast
1717 findlink opaque ld
1719 match link with
1720 | Lnotfound -> loop rest
1721 | Lfound n ->
1722 showlinktype (getlink opaque n);
1723 Ltexact (l.pageno, n)
1725 loop state.layout
1727 state.mode <- LinkNav linknav
1728 | _ -> ()
1729 end;
1730 preload layout;
1732 state.ghyll <- noghyll;
1733 if conf.updatecurs
1734 then (
1735 let mx, my = state.mpos in
1736 updateunder mx my;
1740 let conttiling pageno opaque =
1741 tilepage pageno opaque
1742 (if conf.preload then preloadlayout state.layout else state.layout)
1745 let gotoy_and_clear_text y =
1746 if not conf.verbose then state.text <- "";
1747 gotoy y;
1750 let getanchor () =
1751 match state.layout with
1752 | [] -> emptyanchor
1753 | l :: _ ->
1754 let coloff = l.pagecol * l.pageh in
1755 (l.pageno, (float l.pagey +. float coloff) /. float l.pageh)
1758 let getanchory (n, top) =
1759 let y, h = getpageyh n in
1760 y + (truncate (top *. float h));
1763 let gotoanchor anchor =
1764 gotoy (getanchory anchor);
1767 let addnav () =
1768 cbput state.hists.nav (getanchor ());
1771 let getnav dir =
1772 let anchor = cbgetc state.hists.nav dir in
1773 getanchory anchor;
1776 let gotoghyll y =
1777 let rec scroll f n a b =
1778 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1779 let snake f a b =
1780 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1781 if f < a
1782 then s (float f /. float a)
1783 else (
1784 if f > b
1785 then 1.0 -. s ((float (f-b) /. float (n-b)))
1786 else 1.0
1789 snake f a b
1790 and summa f n a b =
1791 (* courtesy:
1792 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1793 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1794 let iv1 = iv f in
1795 let ins = float a *. iv1
1796 and outs = float (n-b) *. iv1 in
1797 let ones = b - a in
1798 ins +. outs +. float ones
1800 let rec set (_N, _A, _B) y sy =
1801 let sum = summa 1.0 _N _A _B in
1802 let dy = float (y - sy) in
1803 state.ghyll <- (
1804 let rec gf n y1 o =
1805 if n >= _N
1806 then state.ghyll <- noghyll
1807 else
1808 let go n =
1809 let s = scroll n _N _A _B in
1810 let y1 = y1 +. ((s *. dy) /. sum) in
1811 gotoy_and_clear_text (truncate y1);
1812 state.ghyll <- gf (n+1) y1;
1814 match o with
1815 | None -> go n
1816 | Some y' -> set (_N/2, 0, 0) y' state.y
1818 gf 0 (float state.y)
1821 match conf.ghyllscroll with
1822 | None ->
1823 gotoy_and_clear_text y
1824 | Some nab ->
1825 if state.ghyll == noghyll
1826 then set nab y state.y
1827 else state.ghyll (Some y)
1830 let gotopage n top =
1831 let y, h = getpageyh n in
1832 let y = y + (truncate (top *. float h)) in
1833 gotoghyll y
1836 let gotopage1 n top =
1837 let y = getpagey n in
1838 let y = y + top in
1839 gotoghyll y
1842 let invalidate s f =
1843 state.layout <- [];
1844 state.pdims <- [];
1845 state.rects <- [];
1846 state.rects1 <- [];
1847 match state.geomcmds with
1848 | ps, [] when String.length ps = 0 ->
1849 f ();
1850 state.geomcmds <- s, [];
1852 | ps, [] ->
1853 state.geomcmds <- ps, [s, f];
1855 | ps, (s', _) :: rest when s' = s ->
1856 state.geomcmds <- ps, ((s, f) :: rest);
1858 | ps, cmds ->
1859 state.geomcmds <- ps, ((s, f) :: cmds);
1862 let opendoc path password =
1863 state.path <- path;
1864 state.password <- password;
1865 state.gen <- state.gen + 1;
1866 state.docinfo <- [];
1868 setaalevel conf.aalevel;
1869 Wsi.settitle ("llpp " ^ Filename.basename path);
1870 wcmd "open %s\000%s\000" path password;
1871 invalidate "reqlayout"
1872 (fun () ->
1873 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1876 let scalecolor c =
1877 let c = c *. conf.colorscale in
1878 (c, c, c);
1881 let scalecolor2 (r, g, b) =
1882 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1885 let represent () =
1886 let docolumns = function
1887 | Csingle -> ()
1889 | Cmulti ((columns, coverA, coverB), _) ->
1890 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1891 let rec loop pageno pdimno pdim x y rowh pdims =
1892 let rec fixrow m = if m = pageno then () else
1893 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1894 if h < rowh
1895 then (
1896 let y = y + (rowh - h) / 2 in
1897 a.(m) <- (pdimno, x, y, pdim);
1899 fixrow (m+1)
1901 if pageno = state.pagecount
1902 then fixrow (((pageno - 1) / columns) * columns)
1903 else
1904 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1905 match pdims with
1906 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1907 pdimno+1, pdim, rest
1908 | _ ->
1909 pdimno, pdim, pdims
1911 let x, y, rowh' =
1912 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1913 then (
1914 (conf.winw - state.scrollw - w) / 2,
1915 y + rowh + conf.interpagespace, h
1917 else (
1918 if (pageno - coverA) mod columns = 0
1919 then 0, y + rowh + conf.interpagespace, h
1920 else x, y, max rowh h
1923 if pageno > 1 && (pageno - coverA) mod columns = 0
1924 then fixrow (pageno - columns);
1925 a.(pageno) <- (pdimno, x, y, pdim);
1926 let x = x + w + xoff*2 + conf.interpagespace in
1927 loop (pageno+1) pdimno pdim x y rowh' pdims
1929 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1930 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1932 | Csplit (c, _) ->
1933 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1934 let rec loop pageno pdimno pdim y pdims =
1935 if pageno = state.pagecount
1936 then ()
1937 else
1938 let pdimno, ((_, w, h, _) as pdim), pdims =
1939 match pdims with
1940 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1941 pdimno+1, pdim, rest
1942 | _ ->
1943 pdimno, pdim, pdims
1945 let cw = w / c in
1946 let rec loop1 n x y =
1947 if n = c then y else (
1948 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1949 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1952 let y = loop1 0 0 y in
1953 loop (pageno+1) pdimno pdim y pdims
1955 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1956 conf.columns <- Csplit (c, a);
1958 docolumns conf.columns;
1959 state.maxy <- calcheight ();
1960 state.hscrollh <-
1961 if state.w <= conf.winw - state.scrollw
1962 then 0
1963 else state.scrollw
1965 match state.mode with
1966 | Birdseye (_, _, pageno, _, _) ->
1967 let y, h = getpageyh pageno in
1968 let top = (conf.winh - h) / 2 in
1969 gotoy (max 0 (y - top))
1970 | _ -> gotoanchor state.anchor
1973 let reshape w h =
1974 GlDraw.viewport 0 0 w h;
1975 let firsttime = state.geomcmds == firstgeomcmds in
1976 if not firsttime && nogeomcmds state.geomcmds
1977 then state.anchor <- getanchor ();
1979 conf.winw <- w;
1980 let w = truncate (float w *. conf.zoom) - state.scrollw in
1981 let w = max w 2 in
1982 conf.winh <- h;
1983 setfontsize fstate.fontsize;
1984 GlMat.mode `modelview;
1985 GlMat.load_identity ();
1987 GlMat.mode `projection;
1988 GlMat.load_identity ();
1989 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1990 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1991 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1993 let relx =
1994 if conf.zoom <= 1.0
1995 then 0.0
1996 else float state.x /. float state.w
1998 invalidate "geometry"
1999 (fun () ->
2000 state.w <- w;
2001 if not firsttime
2002 then state.x <- truncate (relx *. float w);
2003 let w =
2004 match conf.columns with
2005 | Csingle -> w
2006 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2007 | Csplit (c, _) -> w * c
2009 wcmd "geometry %d %d" w h);
2012 let enttext () =
2013 let len = String.length state.text in
2014 let drawstring s =
2015 let hscrollh =
2016 match state.mode with
2017 | Textentry _
2018 | View ->
2019 let h, _, _ = state.uioh#scrollpw in
2021 | _ -> 0
2023 let rect x w =
2024 GlDraw.rect
2025 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2026 (x+.w, float (conf.winh - hscrollh))
2029 let w = float (conf.winw - state.scrollw - 1) in
2030 if state.progress >= 0.0 && state.progress < 1.0
2031 then (
2032 GlDraw.color (0.3, 0.3, 0.3);
2033 let w1 = w *. state.progress in
2034 rect 0.0 w1;
2035 GlDraw.color (0.0, 0.0, 0.0);
2036 rect w1 (w-.w1)
2038 else (
2039 GlDraw.color (0.0, 0.0, 0.0);
2040 rect 0.0 w;
2043 GlDraw.color (1.0, 1.0, 1.0);
2044 drawstring fstate.fontsize
2045 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2047 let s =
2048 match state.mode with
2049 | Textentry ((prefix, text, _, _, _), _) ->
2050 let s =
2051 if len > 0
2052 then
2053 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2054 else
2055 Printf.sprintf "%s%s_" prefix text
2059 | _ -> state.text
2061 let s =
2062 if state.newerrmsgs
2063 then (
2064 if not (istextentry state.mode)
2065 then
2066 let s1 = "(press 'e' to review error messasges)" in
2067 if String.length s > 0 then s ^ " " ^ s1 else s1
2068 else s
2070 else s
2072 if String.length s > 0
2073 then drawstring s
2076 let gctiles () =
2077 let len = Queue.length state.tilelru in
2078 let rec loop qpos =
2079 if state.memused <= conf.memlimit
2080 then ()
2081 else (
2082 if qpos < len
2083 then
2084 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2085 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2086 let (_, pw, ph, _) = getpagedim n in
2088 gen = state.gen
2089 && colorspace = conf.colorspace
2090 && angle = conf.angle
2091 && pagew = pw
2092 && pageh = ph
2093 && (
2094 let layout =
2095 match state.throttle with
2096 | None ->
2097 if conf.preload
2098 then preloadlayout state.layout
2099 else state.layout
2100 | Some (layout, _, _) ->
2101 layout
2103 let x = col*conf.tilew
2104 and y = row*conf.tileh in
2105 tilevisible layout n x y
2107 then Queue.push lruitem state.tilelru
2108 else (
2109 wcmd "freetile %s" p;
2110 state.memused <- state.memused - s;
2111 state.uioh#infochanged Memused;
2112 Hashtbl.remove state.tilemap k;
2114 loop (qpos+1)
2117 loop 0
2120 let flushtiles () =
2121 Queue.iter (fun (k, p, s) ->
2122 wcmd "freetile %s" p;
2123 state.memused <- state.memused - s;
2124 state.uioh#infochanged Memused;
2125 Hashtbl.remove state.tilemap k;
2126 ) state.tilelru;
2127 Queue.clear state.tilelru;
2128 load state.layout;
2131 let logcurrently = function
2132 | Idle -> dolog "Idle"
2133 | Loading (l, gen) ->
2134 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2135 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2136 dolog
2137 "Tiling %d[%d,%d] page=%s cs=%s angle"
2138 l.pageno col row pageopaque
2139 (colorspace_to_string colorspace)
2141 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2142 angle gen conf.angle state.gen
2143 tilew tileh
2144 conf.tilew conf.tileh
2146 | Outlining _ ->
2147 dolog "outlining"
2150 let act cmds =
2151 (* dolog "%S" cmds; *)
2152 let op, args =
2153 let spacepos =
2154 try String.index cmds ' '
2155 with Not_found -> -1
2157 if spacepos = -1
2158 then cmds, ""
2159 else
2160 let l = String.length cmds in
2161 let op = String.sub cmds 0 spacepos in
2162 op, begin
2163 if l - spacepos < 2 then ""
2164 else String.sub cmds (spacepos+1) (l-spacepos-1)
2167 match op with
2168 | "clear" ->
2169 state.uioh#infochanged Pdim;
2170 state.pdims <- [];
2172 | "clearrects" ->
2173 state.rects <- state.rects1;
2174 G.postRedisplay "clearrects";
2176 | "continue" ->
2177 let n =
2178 try Scanf.sscanf args "%u" (fun n -> n)
2179 with exn ->
2180 dolog "error processing 'continue' %S: %s"
2181 cmds (Printexc.to_string exn);
2182 exit 1;
2184 state.pagecount <- n;
2185 begin match state.currently with
2186 | Outlining l ->
2187 state.currently <- Idle;
2188 state.outlines <- Array.of_list (List.rev l)
2189 | _ -> ()
2190 end;
2192 let cur, cmds = state.geomcmds in
2193 if String.length cur = 0
2194 then failwith "umpossible";
2196 begin match List.rev cmds with
2197 | [] ->
2198 state.geomcmds <- "", [];
2199 represent ();
2200 | (s, f) :: rest ->
2201 f ();
2202 state.geomcmds <- s, List.rev rest;
2203 end;
2204 if conf.maxwait = None
2205 then G.postRedisplay "continue";
2207 | "title" ->
2208 Wsi.settitle args
2210 | "msg" ->
2211 showtext ' ' args
2213 | "vmsg" ->
2214 if conf.verbose
2215 then showtext ' ' args
2217 | "progress" ->
2218 let progress, text =
2220 Scanf.sscanf args "%f %n"
2221 (fun f pos ->
2222 f, String.sub args pos (String.length args - pos))
2223 with exn ->
2224 dolog "error processing 'progress' %S: %s"
2225 cmds (Printexc.to_string exn);
2226 exit 1;
2228 state.text <- text;
2229 state.progress <- progress;
2230 G.postRedisplay "progress"
2232 | "firstmatch" ->
2233 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2235 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2236 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2237 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2238 with exn ->
2239 dolog "error processing 'firstmatch' %S: %s"
2240 cmds (Printexc.to_string exn);
2241 exit 1;
2243 let y = (getpagey pageno) + truncate y0 in
2244 addnav ();
2245 gotoy y;
2246 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2248 | "match" ->
2249 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2251 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2252 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2253 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2254 with exn ->
2255 dolog "error processing 'match' %S: %s"
2256 cmds (Printexc.to_string exn);
2257 exit 1;
2259 state.rects1 <-
2260 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2262 | "page" ->
2263 let pageopaque, t =
2265 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2266 with exn ->
2267 dolog "error processing 'page' %S: %s"
2268 cmds (Printexc.to_string exn);
2269 exit 1;
2271 begin match state.currently with
2272 | Loading (l, gen) ->
2273 vlog "page %d took %f sec" l.pageno t;
2274 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2275 begin match state.throttle with
2276 | None ->
2277 let preloadedpages =
2278 if conf.preload
2279 then preloadlayout state.layout
2280 else state.layout
2282 let evict () =
2283 let module IntSet =
2284 Set.Make (struct type t = int let compare = (-) end) in
2285 let set =
2286 List.fold_left (fun s l -> IntSet.add l.pageno s)
2287 IntSet.empty preloadedpages
2289 let evictedpages =
2290 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2291 if not (IntSet.mem pageno set)
2292 then (
2293 wcmd "freepage %s" opaque;
2294 key :: accu
2296 else accu
2297 ) state.pagemap []
2299 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2301 evict ();
2302 state.currently <- Idle;
2303 if gen = state.gen
2304 then (
2305 tilepage l.pageno pageopaque state.layout;
2306 load state.layout;
2307 load preloadedpages;
2308 if pagevisible state.layout l.pageno
2309 && layoutready state.layout
2310 then G.postRedisplay "page";
2313 | Some (layout, _, _) ->
2314 state.currently <- Idle;
2315 tilepage l.pageno pageopaque layout;
2316 load state.layout
2317 end;
2319 | _ ->
2320 dolog "Inconsistent loading state";
2321 logcurrently state.currently;
2322 exit 1
2325 | "tile" ->
2326 let (x, y, opaque, size, t) =
2328 Scanf.sscanf args "%u %u %s %u %f"
2329 (fun x y p size t -> (x, y, p, size, t))
2330 with exn ->
2331 dolog "error processing 'tile' %S: %s"
2332 cmds (Printexc.to_string exn);
2333 exit 1;
2335 begin match state.currently with
2336 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2337 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2339 if tilew != conf.tilew || tileh != conf.tileh
2340 then (
2341 wcmd "freetile %s" opaque;
2342 state.currently <- Idle;
2343 load state.layout;
2345 else (
2346 puttileopaque l col row gen cs angle opaque size t;
2347 state.memused <- state.memused + size;
2348 state.uioh#infochanged Memused;
2349 gctiles ();
2350 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2351 opaque, size) state.tilelru;
2353 let layout =
2354 match state.throttle with
2355 | None -> state.layout
2356 | Some (layout, _, _) -> layout
2359 state.currently <- Idle;
2360 if gen = state.gen
2361 && conf.colorspace = cs
2362 && conf.angle = angle
2363 && tilevisible layout l.pageno x y
2364 then conttiling l.pageno pageopaque;
2366 begin match state.throttle with
2367 | None ->
2368 preload state.layout;
2369 if gen = state.gen
2370 && conf.colorspace = cs
2371 && conf.angle = angle
2372 && tilevisible state.layout l.pageno x y
2373 then G.postRedisplay "tile nothrottle";
2375 | Some (layout, y, _) ->
2376 let ready = layoutready layout in
2377 if ready
2378 then (
2379 state.y <- y;
2380 state.layout <- layout;
2381 state.throttle <- None;
2382 G.postRedisplay "throttle";
2384 else load layout;
2385 end;
2388 | _ ->
2389 dolog "Inconsistent tiling state";
2390 logcurrently state.currently;
2391 exit 1
2394 | "pdim" ->
2395 let pdim =
2397 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2398 with exn ->
2399 dolog "error processing 'pdim' %S: %s"
2400 cmds (Printexc.to_string exn);
2401 exit 1;
2403 state.uioh#infochanged Pdim;
2404 state.pdims <- pdim :: state.pdims
2406 | "o" ->
2407 let (l, n, t, h, pos) =
2409 Scanf.sscanf args "%u %u %d %u %n"
2410 (fun l n t h pos -> l, n, t, h, pos)
2411 with exn ->
2412 dolog "error processing 'o' %S: %s"
2413 cmds (Printexc.to_string exn);
2414 exit 1;
2416 let s = String.sub args pos (String.length args - pos) in
2417 let outline = (s, l, (n, float t /. float h)) in
2418 begin match state.currently with
2419 | Outlining outlines ->
2420 state.currently <- Outlining (outline :: outlines)
2421 | Idle ->
2422 state.currently <- Outlining [outline]
2423 | currently ->
2424 dolog "invalid outlining state";
2425 logcurrently currently
2428 | "info" ->
2429 state.docinfo <- (1, args) :: state.docinfo
2431 | "infoend" ->
2432 state.uioh#infochanged Docinfo;
2433 state.docinfo <- List.rev state.docinfo
2435 | _ ->
2436 dolog "unknown cmd `%S'" cmds
2439 let onhist cb =
2440 let rc = cb.rc in
2441 let action = function
2442 | HCprev -> cbget cb ~-1
2443 | HCnext -> cbget cb 1
2444 | HCfirst -> cbget cb ~-(cb.rc)
2445 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2446 and cancel () = cb.rc <- rc
2447 in (action, cancel)
2450 let search pattern forward =
2451 if String.length pattern > 0
2452 then
2453 let pn, py =
2454 match state.layout with
2455 | [] -> 0, 0
2456 | l :: _ ->
2457 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2459 wcmd "search %d %d %d %d,%s\000"
2460 (btod conf.icase) pn py (btod forward) pattern;
2463 let intentry text key =
2464 let c =
2465 if key >= 32 && key < 127
2466 then Char.chr key
2467 else '\000'
2469 match c with
2470 | '0' .. '9' ->
2471 let text = addchar text c in
2472 TEcont text
2474 | _ ->
2475 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2476 TEcont text
2479 let linknentry text key =
2480 let c =
2481 if key >= 32 && key < 127
2482 then Char.chr key
2483 else '\000'
2485 match c with
2486 | 'a' .. 'z' ->
2487 let text = addchar text c in
2488 TEcont text
2490 | _ ->
2491 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2492 TEcont text
2495 let linkndone f s =
2496 if String.length s > 0
2497 then (
2498 let n =
2499 let l = String.length s in
2500 let rec loop pos n = if pos = l then n else
2501 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2502 loop (pos+1) (n*26 + m)
2503 in loop 0 0
2505 let rec loop n = function
2506 | [] -> ()
2507 | l :: rest ->
2508 match getopaque l.pageno with
2509 | None -> loop n rest
2510 | Some opaque ->
2511 let m = getlinkcount opaque in
2512 if n < m
2513 then (
2514 let under = getlink opaque n in
2515 f under
2517 else loop (n-m) rest
2519 loop n state.layout;
2523 let textentry text key =
2524 if key land 0xff00 = 0xff00
2525 then TEcont text
2526 else TEcont (text ^ Wsi.toutf8 key)
2529 let reqlayout angle proportional =
2530 match state.throttle with
2531 | None ->
2532 if nogeomcmds state.geomcmds
2533 then state.anchor <- getanchor ();
2534 conf.angle <- angle mod 360;
2535 if conf.angle != 0
2536 then (
2537 match state.mode with
2538 | LinkNav _ -> state.mode <- View
2539 | _ -> ()
2541 conf.proportional <- proportional;
2542 invalidate "reqlayout"
2543 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2544 | _ -> ()
2547 let settrim trimmargins trimfuzz =
2548 if nogeomcmds state.geomcmds
2549 then state.anchor <- getanchor ();
2550 conf.trimmargins <- trimmargins;
2551 conf.trimfuzz <- trimfuzz;
2552 let x0, y0, x1, y1 = trimfuzz in
2553 invalidate "settrim"
2554 (fun () ->
2555 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2556 Hashtbl.iter (fun _ opaque ->
2557 wcmd "freepage %s" opaque;
2558 ) state.pagemap;
2559 Hashtbl.clear state.pagemap;
2562 let setzoom zoom =
2563 match state.throttle with
2564 | None ->
2565 let zoom = max 0.01 zoom in
2566 if zoom <> conf.zoom
2567 then (
2568 state.prevzoom <- conf.zoom;
2569 conf.zoom <- zoom;
2570 reshape conf.winw conf.winh;
2571 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2574 | Some (layout, y, started) ->
2575 let time =
2576 match conf.maxwait with
2577 | None -> 0.0
2578 | Some t -> t
2580 let dt = now () -. started in
2581 if dt > time
2582 then (
2583 state.y <- y;
2584 load layout;
2588 let setcolumns mode columns coverA coverB =
2589 if columns < 0
2590 then (
2591 if isbirdseye mode
2592 then showtext '!' "split mode doesn't work in bird's eye"
2593 else (
2594 conf.columns <- Csplit (-columns, [||]);
2595 state.x <- 0;
2596 conf.zoom <- 1.0;
2599 else (
2600 if columns < 2
2601 then (
2602 conf.columns <- Csingle;
2603 state.x <- 0;
2604 setzoom 1.0;
2606 else (
2607 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2608 conf.zoom <- 1.0;
2611 reshape conf.winw conf.winh;
2614 let enterbirdseye () =
2615 let zoom = float conf.thumbw /. float conf.winw in
2616 let birdseyepageno =
2617 let cy = conf.winh / 2 in
2618 let fold = function
2619 | [] -> 0
2620 | l :: rest ->
2621 let rec fold best = function
2622 | [] -> best.pageno
2623 | l :: rest ->
2624 let d = cy - (l.pagedispy + l.pagevh/2)
2625 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2626 if abs d < abs dbest
2627 then fold l rest
2628 else best.pageno
2629 in fold l rest
2631 fold state.layout
2633 state.mode <- Birdseye (
2634 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2636 conf.zoom <- zoom;
2637 conf.presentation <- false;
2638 conf.interpagespace <- 10;
2639 conf.hlinks <- false;
2640 state.x <- 0;
2641 state.mstate <- Mnone;
2642 conf.maxwait <- None;
2643 conf.columns <- (
2644 match conf.beyecolumns with
2645 | Some c ->
2646 conf.zoom <- 1.0;
2647 Cmulti ((c, 0, 0), [||])
2648 | None -> Csingle
2650 Wsi.setcursor Wsi.CURSOR_INHERIT;
2651 if conf.verbose
2652 then
2653 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2654 (100.0*.zoom)
2655 else
2656 state.text <- ""
2658 reshape conf.winw conf.winh;
2661 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2662 state.mode <- View;
2663 conf.zoom <- c.zoom;
2664 conf.presentation <- c.presentation;
2665 conf.interpagespace <- c.interpagespace;
2666 conf.maxwait <- c.maxwait;
2667 conf.hlinks <- c.hlinks;
2668 conf.beyecolumns <- (
2669 match conf.columns with
2670 | Cmulti ((c, _, _), _) -> Some c
2671 | Csingle -> None
2672 | Csplit _ -> assert false
2674 conf.columns <- (
2675 match c.columns with
2676 | Cmulti (c, _) -> Cmulti (c, [||])
2677 | Csingle -> Csingle
2678 | Csplit _ -> failwith "leaving bird's eye split mode"
2680 state.x <- leftx;
2681 if conf.verbose
2682 then
2683 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2684 (100.0*.conf.zoom)
2686 reshape conf.winw conf.winh;
2687 state.anchor <- if goback then anchor else (pageno, 0.0);
2690 let togglebirdseye () =
2691 match state.mode with
2692 | Birdseye vals -> leavebirdseye vals true
2693 | View -> enterbirdseye ()
2694 | _ -> ()
2697 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2698 let pageno = max 0 (pageno - incr) in
2699 let rec loop = function
2700 | [] -> gotopage1 pageno 0
2701 | l :: _ when l.pageno = pageno ->
2702 if l.pagedispy >= 0 && l.pagey = 0
2703 then G.postRedisplay "upbirdseye"
2704 else gotopage1 pageno 0
2705 | _ :: rest -> loop rest
2707 loop state.layout;
2708 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2711 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2712 let pageno = min (state.pagecount - 1) (pageno + incr) in
2713 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2714 let rec loop = function
2715 | [] ->
2716 let y, h = getpageyh pageno in
2717 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2718 gotoy (clamp dy)
2719 | l :: _ when l.pageno = pageno ->
2720 if l.pagevh != l.pageh
2721 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2722 else G.postRedisplay "downbirdseye"
2723 | _ :: rest -> loop rest
2725 loop state.layout
2728 let optentry mode _ key =
2729 let btos b = if b then "on" else "off" in
2730 if key >= 32 && key < 127
2731 then
2732 let c = Char.chr key in
2733 match c with
2734 | 's' ->
2735 let ondone s =
2736 try conf.scrollstep <- int_of_string s with exc ->
2737 state.text <- Printf.sprintf "bad integer `%s': %s"
2738 s (Printexc.to_string exc)
2740 TEswitch ("scroll step: ", "", None, intentry, ondone)
2742 | 'A' ->
2743 let ondone s =
2745 conf.autoscrollstep <- int_of_string s;
2746 if state.autoscroll <> None
2747 then state.autoscroll <- Some conf.autoscrollstep
2748 with exc ->
2749 state.text <- Printf.sprintf "bad integer `%s': %s"
2750 s (Printexc.to_string exc)
2752 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2754 | 'C' ->
2755 let mode = state.mode in
2756 let ondone s =
2758 let n, a, b = multicolumns_of_string s in
2759 setcolumns mode n a b;
2760 with exc ->
2761 state.text <- Printf.sprintf "bad columns `%s': %s"
2762 s (Printexc.to_string exc)
2764 TEswitch ("columns: ", "", None, textentry, ondone)
2766 | 'Z' ->
2767 let ondone s =
2769 let zoom = float (int_of_string s) /. 100.0 in
2770 setzoom zoom
2771 with exc ->
2772 state.text <- Printf.sprintf "bad integer `%s': %s"
2773 s (Printexc.to_string exc)
2775 TEswitch ("zoom: ", "", None, intentry, ondone)
2777 | 't' ->
2778 let ondone s =
2780 conf.thumbw <- bound (int_of_string s) 2 4096;
2781 state.text <-
2782 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2783 begin match mode with
2784 | Birdseye beye ->
2785 leavebirdseye beye false;
2786 enterbirdseye ();
2787 | _ -> ();
2789 with exc ->
2790 state.text <- Printf.sprintf "bad integer `%s': %s"
2791 s (Printexc.to_string exc)
2793 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2795 | 'R' ->
2796 let ondone s =
2797 match try
2798 Some (int_of_string s)
2799 with exc ->
2800 state.text <- Printf.sprintf "bad integer `%s': %s"
2801 s (Printexc.to_string exc);
2802 None
2803 with
2804 | Some angle -> reqlayout angle conf.proportional
2805 | None -> ()
2807 TEswitch ("rotation: ", "", None, intentry, ondone)
2809 | 'i' ->
2810 conf.icase <- not conf.icase;
2811 TEdone ("case insensitive search " ^ (btos conf.icase))
2813 | 'p' ->
2814 conf.preload <- not conf.preload;
2815 gotoy state.y;
2816 TEdone ("preload " ^ (btos conf.preload))
2818 | 'v' ->
2819 conf.verbose <- not conf.verbose;
2820 TEdone ("verbose " ^ (btos conf.verbose))
2822 | 'd' ->
2823 conf.debug <- not conf.debug;
2824 TEdone ("debug " ^ (btos conf.debug))
2826 | 'h' ->
2827 conf.maxhfit <- not conf.maxhfit;
2828 state.maxy <- calcheight ();
2829 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2831 | 'c' ->
2832 conf.crophack <- not conf.crophack;
2833 TEdone ("crophack " ^ btos conf.crophack)
2835 | 'a' ->
2836 let s =
2837 match conf.maxwait with
2838 | None ->
2839 conf.maxwait <- Some infinity;
2840 "always wait for page to complete"
2841 | Some _ ->
2842 conf.maxwait <- None;
2843 "show placeholder if page is not ready"
2845 TEdone s
2847 | 'f' ->
2848 conf.underinfo <- not conf.underinfo;
2849 TEdone ("underinfo " ^ btos conf.underinfo)
2851 | 'P' ->
2852 conf.savebmarks <- not conf.savebmarks;
2853 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2855 | 'S' ->
2856 let ondone s =
2858 let pageno, py =
2859 match state.layout with
2860 | [] -> 0, 0
2861 | l :: _ ->
2862 l.pageno, l.pagey
2864 conf.interpagespace <- int_of_string s;
2865 state.maxy <- calcheight ();
2866 let y = getpagey pageno in
2867 gotoy (y + py)
2868 with exc ->
2869 state.text <- Printf.sprintf "bad integer `%s': %s"
2870 s (Printexc.to_string exc)
2872 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2874 | 'l' ->
2875 reqlayout conf.angle (not conf.proportional);
2876 TEdone ("proportional display " ^ btos conf.proportional)
2878 | 'T' ->
2879 settrim (not conf.trimmargins) conf.trimfuzz;
2880 TEdone ("trim margins " ^ btos conf.trimmargins)
2882 | 'I' ->
2883 conf.invert <- not conf.invert;
2884 TEdone ("invert colors " ^ btos conf.invert)
2886 | 'x' ->
2887 let ondone s =
2888 cbput state.hists.sel s;
2889 conf.selcmd <- s;
2891 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2892 textentry, ondone)
2894 | _ ->
2895 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2896 TEstop
2897 else
2898 TEcont state.text
2901 class type lvsource = object
2902 method getitemcount : int
2903 method getitem : int -> (string * int)
2904 method hasaction : int -> bool
2905 method exit :
2906 uioh:uioh ->
2907 cancel:bool ->
2908 active:int ->
2909 first:int ->
2910 pan:int ->
2911 qsearch:string ->
2912 uioh option
2913 method getactive : int
2914 method getfirst : int
2915 method getqsearch : string
2916 method setqsearch : string -> unit
2917 method getpan : int
2918 end;;
2920 class virtual lvsourcebase = object
2921 val mutable m_active = 0
2922 val mutable m_first = 0
2923 val mutable m_qsearch = ""
2924 val mutable m_pan = 0
2925 method getactive = m_active
2926 method getfirst = m_first
2927 method getqsearch = m_qsearch
2928 method getpan = m_pan
2929 method setqsearch s = m_qsearch <- s
2930 end;;
2932 let withoutlastutf8 s =
2933 let len = String.length s in
2934 if len = 0
2935 then s
2936 else
2937 let rec find pos =
2938 if pos = 0
2939 then pos
2940 else
2941 let b = Char.code s.[pos] in
2942 if b land 0b110000 = 0b11000000
2943 then find (pos-1)
2944 else pos-1
2946 let first =
2947 if Char.code s.[len-1] land 0x80 = 0
2948 then len-1
2949 else find (len-1)
2951 String.sub s 0 first;
2954 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2955 let enttext te =
2956 state.mode <- Textentry (te, onleave);
2957 state.text <- "";
2958 enttext ();
2959 G.postRedisplay "textentrykeyboard enttext";
2961 let histaction cmd =
2962 match opthist with
2963 | None -> ()
2964 | Some (action, _) ->
2965 state.mode <- Textentry (
2966 (c, action cmd, opthist, onkey, ondone), onleave
2968 G.postRedisplay "textentry histaction"
2970 match key with
2971 | 0xff08 -> (* backspace *)
2972 let s = withoutlastutf8 text in
2973 let len = String.length s in
2974 if len = 0
2975 then (
2976 onleave Cancel;
2977 G.postRedisplay "textentrykeyboard after cancel";
2979 else (
2980 enttext (c, s, opthist, onkey, ondone)
2983 | 0xff0d ->
2984 ondone text;
2985 onleave Confirm;
2986 G.postRedisplay "textentrykeyboard after confirm"
2988 | 0xff52 -> histaction HCprev
2989 | 0xff54 -> histaction HCnext
2990 | 0xff50 -> histaction HCfirst
2991 | 0xff57 -> histaction HClast
2993 | 0xff1b -> (* escape*)
2994 if String.length text = 0
2995 then (
2996 begin match opthist with
2997 | None -> ()
2998 | Some (_, onhistcancel) -> onhistcancel ()
2999 end;
3000 onleave Cancel;
3001 state.text <- "";
3002 G.postRedisplay "textentrykeyboard after cancel2"
3004 else (
3005 enttext (c, "", opthist, onkey, ondone)
3008 | 0xff9f | 0xffff -> () (* delete *)
3010 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3011 begin match onkey text key with
3012 | TEdone text ->
3013 ondone text;
3014 onleave Confirm;
3015 G.postRedisplay "textentrykeyboard after confirm2";
3017 | TEcont text ->
3018 enttext (c, text, opthist, onkey, ondone);
3020 | TEstop ->
3021 onleave Cancel;
3022 G.postRedisplay "textentrykeyboard after cancel3"
3024 | TEswitch te ->
3025 state.mode <- Textentry (te, onleave);
3026 G.postRedisplay "textentrykeyboard switch";
3027 end;
3029 | _ ->
3030 vlog "unhandled key %s" (Wsi.keyname key)
3033 let firstof first active =
3034 if first > active || abs (first - active) > fstate.maxrows - 1
3035 then max 0 (active - (fstate.maxrows/2))
3036 else first
3039 let calcfirst first active =
3040 if active > first
3041 then
3042 let rows = active - first in
3043 if rows > fstate.maxrows then active - fstate.maxrows else first
3044 else active
3047 let scrollph y maxy =
3048 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3049 let sh = float conf.winh /. sh in
3050 let sh = max sh (float conf.scrollh) in
3052 let percent =
3053 if y = state.maxy
3054 then 1.0
3055 else float y /. float maxy
3057 let position = (float conf.winh -. sh) *. percent in
3059 let position =
3060 if position +. sh > float conf.winh
3061 then float conf.winh -. sh
3062 else position
3064 position, sh;
3067 let coe s = (s :> uioh);;
3069 class listview ~(source:lvsource) ~trusted ~modehash =
3070 object (self)
3071 val m_pan = source#getpan
3072 val m_first = source#getfirst
3073 val m_active = source#getactive
3074 val m_qsearch = source#getqsearch
3075 val m_prev_uioh = state.uioh
3077 method private elemunder y =
3078 let n = y / (fstate.fontsize+1) in
3079 if m_first + n < source#getitemcount
3080 then (
3081 if source#hasaction (m_first + n)
3082 then Some (m_first + n)
3083 else None
3085 else None
3087 method display =
3088 Gl.enable `blend;
3089 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3090 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3091 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3092 GlDraw.color (1., 1., 1.);
3093 Gl.enable `texture_2d;
3094 let fs = fstate.fontsize in
3095 let nfs = fs + 1 in
3096 let ww = fstate.wwidth in
3097 let tabw = 30.0*.ww in
3098 let itemcount = source#getitemcount in
3099 let rec loop row =
3100 if (row - m_first) * nfs > conf.winh
3101 then ()
3102 else (
3103 if row >= 0 && row < itemcount
3104 then (
3105 let (s, level) = source#getitem row in
3106 let y = (row - m_first) * nfs in
3107 let x = 5.0 +. float (level + m_pan) *. ww in
3108 if row = m_active
3109 then (
3110 Gl.disable `texture_2d;
3111 GlDraw.polygon_mode `both `line;
3112 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3113 GlDraw.rect (1., float (y + 1))
3114 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3115 GlDraw.polygon_mode `both `fill;
3116 GlDraw.color (1., 1., 1.);
3117 Gl.enable `texture_2d;
3120 let drawtabularstring s =
3121 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3122 if trusted
3123 then
3124 let tabpos = try String.index s '\t' with Not_found -> -1 in
3125 if tabpos > 0
3126 then
3127 let len = String.length s - tabpos - 1 in
3128 let s1 = String.sub s 0 tabpos
3129 and s2 = String.sub s (tabpos + 1) len in
3130 let nx = drawstr x s1 in
3131 let sw = nx -. x in
3132 let x = x +. (max tabw sw) in
3133 drawstr x s2
3134 else
3135 drawstr x s
3136 else
3137 drawstr x s
3139 let _ = drawtabularstring s in
3140 loop (row+1)
3144 loop m_first;
3145 Gl.disable `blend;
3146 Gl.disable `texture_2d;
3148 method updownlevel incr =
3149 let len = source#getitemcount in
3150 let curlevel =
3151 if m_active >= 0 && m_active < len
3152 then snd (source#getitem m_active)
3153 else -1
3155 let rec flow i =
3156 if i = len then i-1 else if i = -1 then 0 else
3157 let _, l = source#getitem i in
3158 if l != curlevel then i else flow (i+incr)
3160 let active = flow m_active in
3161 let first = calcfirst m_first active in
3162 G.postRedisplay "outline updownlevel";
3163 {< m_active = active; m_first = first >}
3165 method private key1 key mask =
3166 let set1 active first qsearch =
3167 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3169 let search active pattern incr =
3170 let dosearch re =
3171 let rec loop n =
3172 if n >= 0 && n < source#getitemcount
3173 then (
3174 let s, _ = source#getitem n in
3176 (try ignore (Str.search_forward re s 0); true
3177 with Not_found -> false)
3178 then Some n
3179 else loop (n + incr)
3181 else None
3183 loop active
3186 let re = Str.regexp_case_fold pattern in
3187 dosearch re
3188 with Failure s ->
3189 state.text <- s;
3190 None
3192 let itemcount = source#getitemcount in
3193 let find start incr =
3194 let rec find i =
3195 if i = -1 || i = itemcount
3196 then -1
3197 else (
3198 if source#hasaction i
3199 then i
3200 else find (i + incr)
3203 find start
3205 let set active first =
3206 let first = bound first 0 (itemcount - fstate.maxrows) in
3207 state.text <- "";
3208 coe {< m_active = active; m_first = first >}
3210 let navigate incr =
3211 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3212 let active, first =
3213 let incr1 = if incr > 0 then 1 else -1 in
3214 if isvisible m_first m_active
3215 then
3216 let next =
3217 let next = m_active + incr in
3218 let next =
3219 if next < 0 || next >= itemcount
3220 then -1
3221 else find next incr1
3223 if next = -1 || abs (m_active - next) > fstate.maxrows
3224 then -1
3225 else next
3227 if next = -1
3228 then
3229 let first = m_first + incr in
3230 let first = bound first 0 (itemcount - 1) in
3231 let next =
3232 let next = m_active + incr in
3233 let next = bound next 0 (itemcount - 1) in
3234 find next ~-incr1
3236 let active = if next = -1 then m_active else next in
3237 active, first
3238 else
3239 let first = min next m_first in
3240 let first =
3241 if abs (next - first) > fstate.maxrows
3242 then first + incr
3243 else first
3245 next, first
3246 else
3247 let first = m_first + incr in
3248 let first = bound first 0 (itemcount - 1) in
3249 let active =
3250 let next = m_active + incr in
3251 let next = bound next 0 (itemcount - 1) in
3252 let next = find next incr1 in
3253 let active =
3254 if next = -1 || abs (m_active - first) > fstate.maxrows
3255 then (
3256 let active = if m_active = -1 then next else m_active in
3257 active
3259 else next
3261 if isvisible first active
3262 then active
3263 else -1
3265 active, first
3267 G.postRedisplay "listview navigate";
3268 set active first;
3270 match key with
3271 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3272 let incr = if key = 0x72 then -1 else 1 in
3273 let active, first =
3274 match search (m_active + incr) m_qsearch incr with
3275 | None ->
3276 state.text <- m_qsearch ^ " [not found]";
3277 m_active, m_first
3278 | Some active ->
3279 state.text <- m_qsearch;
3280 active, firstof m_first active
3282 G.postRedisplay "listview ctrl-r/s";
3283 set1 active first m_qsearch;
3285 | 0xff08 -> (* backspace *)
3286 if String.length m_qsearch = 0
3287 then coe self
3288 else (
3289 let qsearch = withoutlastutf8 m_qsearch in
3290 let len = String.length qsearch in
3291 if len = 0
3292 then (
3293 state.text <- "";
3294 G.postRedisplay "listview empty qsearch";
3295 set1 m_active m_first "";
3297 else
3298 let active, first =
3299 match search m_active qsearch ~-1 with
3300 | None ->
3301 state.text <- qsearch ^ " [not found]";
3302 m_active, m_first
3303 | Some active ->
3304 state.text <- qsearch;
3305 active, firstof m_first active
3307 G.postRedisplay "listview backspace qsearch";
3308 set1 active first qsearch
3311 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3312 let pattern = m_qsearch ^ Wsi.toutf8 key in
3313 let active, first =
3314 match search m_active pattern 1 with
3315 | None ->
3316 state.text <- pattern ^ " [not found]";
3317 m_active, m_first
3318 | Some active ->
3319 state.text <- pattern;
3320 active, firstof m_first active
3322 G.postRedisplay "listview qsearch add";
3323 set1 active first pattern;
3325 | 0xff1b -> (* escape *)
3326 state.text <- "";
3327 if String.length m_qsearch = 0
3328 then (
3329 G.postRedisplay "list view escape";
3330 begin
3331 match
3332 source#exit (coe self) true m_active m_first m_pan m_qsearch
3333 with
3334 | None -> m_prev_uioh
3335 | Some uioh -> uioh
3338 else (
3339 G.postRedisplay "list view kill qsearch";
3340 source#setqsearch "";
3341 coe {< m_qsearch = "" >}
3344 | 0xff0d -> (* return *)
3345 state.text <- "";
3346 let self = {< m_qsearch = "" >} in
3347 source#setqsearch "";
3348 let opt =
3349 G.postRedisplay "listview enter";
3350 if m_active >= 0 && m_active < source#getitemcount
3351 then (
3352 source#exit (coe self) false m_active m_first m_pan "";
3354 else (
3355 source#exit (coe self) true m_active m_first m_pan "";
3358 begin match opt with
3359 | None -> m_prev_uioh
3360 | Some uioh -> uioh
3363 | 0xff9f | 0xffff -> (* delete *)
3364 coe self
3366 | 0xff52 -> navigate ~-1 (* up *)
3367 | 0xff54 -> navigate 1 (* down *)
3368 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3369 | 0xff56 -> navigate fstate.maxrows (* next *)
3371 | 0xff53 -> (* right *)
3372 state.text <- "";
3373 G.postRedisplay "listview right";
3374 coe {< m_pan = m_pan - 1 >}
3376 | 0xff51 -> (* left *)
3377 state.text <- "";
3378 G.postRedisplay "listview left";
3379 coe {< m_pan = m_pan + 1 >}
3381 | 0xff50 -> (* home *)
3382 let active = find 0 1 in
3383 G.postRedisplay "listview home";
3384 set active 0;
3386 | 0xff57 -> (* end *)
3387 let first = max 0 (itemcount - fstate.maxrows) in
3388 let active = find (itemcount - 1) ~-1 in
3389 G.postRedisplay "listview end";
3390 set active first;
3392 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3393 coe self
3395 | _ ->
3396 dolog "listview unknown key %#x" key; coe self
3398 method key key mask =
3399 match state.mode with
3400 | Textentry te -> textentrykeyboard key mask te; coe self
3401 | _ -> self#key1 key mask
3403 method button button down x y _ =
3404 let opt =
3405 match button with
3406 | 1 when x > conf.winw - conf.scrollbw ->
3407 G.postRedisplay "listview scroll";
3408 if down
3409 then
3410 let _, position, sh = self#scrollph in
3411 if y > truncate position && y < truncate (position +. sh)
3412 then (
3413 state.mstate <- Mscrolly;
3414 Some (coe self)
3416 else
3417 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3418 let first = truncate (s *. float source#getitemcount) in
3419 let first = min source#getitemcount first in
3420 Some (coe {< m_first = first; m_active = first >})
3421 else (
3422 state.mstate <- Mnone;
3423 Some (coe self);
3425 | 1 when not down ->
3426 begin match self#elemunder y with
3427 | Some n ->
3428 G.postRedisplay "listview click";
3429 source#exit
3430 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3431 | _ ->
3432 Some (coe self)
3434 | n when (n == 4 || n == 5) && not down ->
3435 let len = source#getitemcount in
3436 let first =
3437 if n = 5 && m_first + fstate.maxrows >= len
3438 then
3439 m_first
3440 else
3441 let first = m_first + (if n == 4 then -1 else 1) in
3442 bound first 0 (len - 1)
3444 G.postRedisplay "listview wheel";
3445 Some (coe {< m_first = first >})
3446 | _ ->
3447 Some (coe self)
3449 match opt with
3450 | None -> m_prev_uioh
3451 | Some uioh -> uioh
3453 method motion _ y =
3454 match state.mstate with
3455 | Mscrolly ->
3456 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3457 let first = truncate (s *. float source#getitemcount) in
3458 let first = min source#getitemcount first in
3459 G.postRedisplay "listview motion";
3460 coe {< m_first = first; m_active = first >}
3461 | _ -> coe self
3463 method pmotion x y =
3464 if x < conf.winw - conf.scrollbw
3465 then
3466 let n =
3467 match self#elemunder y with
3468 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3469 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3471 let o =
3472 if n != m_active
3473 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3474 else self
3476 coe o
3477 else (
3478 Wsi.setcursor Wsi.CURSOR_INHERIT;
3479 coe self
3482 method infochanged _ = ()
3484 method scrollpw = (0, 0.0, 0.0)
3485 method scrollph =
3486 let nfs = fstate.fontsize + 1 in
3487 let y = m_first * nfs in
3488 let itemcount = source#getitemcount in
3489 let maxi = max 0 (itemcount - fstate.maxrows) in
3490 let maxy = maxi * nfs in
3491 let p, h = scrollph y maxy in
3492 conf.scrollbw, p, h
3494 method modehash = modehash
3495 end;;
3497 class outlinelistview ~source =
3498 object (self)
3499 inherit listview
3500 ~source:(source :> lvsource)
3501 ~trusted:false
3502 ~modehash:(findkeyhash conf "outline")
3503 as super
3505 method key key mask =
3506 let calcfirst first active =
3507 if active > first
3508 then
3509 let rows = active - first in
3510 if rows > fstate.maxrows then active - fstate.maxrows else first
3511 else active
3513 let navigate incr =
3514 let active = m_active + incr in
3515 let active = bound active 0 (source#getitemcount - 1) in
3516 let first = calcfirst m_first active in
3517 G.postRedisplay "outline navigate";
3518 coe {< m_active = active; m_first = first >}
3520 let ctrl = Wsi.withctrl mask in
3521 match key with
3522 | 110 when ctrl -> (* ctrl-n *)
3523 source#narrow m_qsearch;
3524 G.postRedisplay "outline ctrl-n";
3525 coe {< m_first = 0; m_active = 0 >}
3527 | 117 when ctrl -> (* ctrl-u *)
3528 source#denarrow;
3529 G.postRedisplay "outline ctrl-u";
3530 state.text <- "";
3531 coe {< m_first = 0; m_active = 0 >}
3533 | 108 when ctrl -> (* ctrl-l *)
3534 let first = m_active - (fstate.maxrows / 2) in
3535 G.postRedisplay "outline ctrl-l";
3536 coe {< m_first = first >}
3538 | 0xff9f | 0xffff -> (* delete *)
3539 source#remove m_active;
3540 G.postRedisplay "outline delete";
3541 let active = max 0 (m_active-1) in
3542 coe {< m_first = firstof m_first active;
3543 m_active = active >}
3545 | 0xff52 -> navigate ~-1 (* up *)
3546 | 0xff54 -> navigate 1 (* down *)
3547 | 0xff55 -> (* prior *)
3548 navigate ~-(fstate.maxrows)
3549 | 0xff56 -> (* next *)
3550 navigate fstate.maxrows
3552 | 0xff53 -> (* [ctrl-]right *)
3553 let o =
3554 if ctrl
3555 then (
3556 G.postRedisplay "outline ctrl right";
3557 {< m_pan = m_pan + 1 >}
3559 else self#updownlevel 1
3561 coe o
3563 | 0xff51 -> (* [ctrl-]left *)
3564 let o =
3565 if ctrl
3566 then (
3567 G.postRedisplay "outline ctrl left";
3568 {< m_pan = m_pan - 1 >}
3570 else self#updownlevel ~-1
3572 coe o
3574 | 0xff50 -> (* home *)
3575 G.postRedisplay "outline home";
3576 coe {< m_first = 0; m_active = 0 >}
3578 | 0xff57 -> (* end *)
3579 let active = source#getitemcount - 1 in
3580 let first = max 0 (active - fstate.maxrows) in
3581 G.postRedisplay "outline end";
3582 coe {< m_active = active; m_first = first >}
3584 | _ -> super#key key mask
3587 let outlinesource usebookmarks =
3588 let empty = [||] in
3589 (object
3590 inherit lvsourcebase
3591 val mutable m_items = empty
3592 val mutable m_orig_items = empty
3593 val mutable m_prev_items = empty
3594 val mutable m_narrow_pattern = ""
3595 val mutable m_hadremovals = false
3597 method getitemcount =
3598 Array.length m_items + (if m_hadremovals then 1 else 0)
3600 method getitem n =
3601 if n == Array.length m_items && m_hadremovals
3602 then
3603 ("[Confirm removal]", 0)
3604 else
3605 let s, n, _ = m_items.(n) in
3606 (s, n)
3608 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3609 ignore (uioh, first, qsearch);
3610 let confrimremoval = m_hadremovals && active = Array.length m_items in
3611 let items =
3612 if String.length m_narrow_pattern = 0
3613 then m_orig_items
3614 else m_items
3616 if not cancel
3617 then (
3618 if not confrimremoval
3619 then(
3620 let _, _, anchor = m_items.(active) in
3621 gotoanchor anchor;
3622 m_items <- items;
3624 else (
3625 state.bookmarks <- Array.to_list m_items;
3626 m_orig_items <- m_items;
3629 else m_items <- items;
3630 m_pan <- pan;
3631 None
3633 method hasaction _ = true
3635 method greetmsg =
3636 if Array.length m_items != Array.length m_orig_items
3637 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3638 else ""
3640 method narrow pattern =
3641 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3642 match reopt with
3643 | None -> ()
3644 | Some re ->
3645 let rec loop accu n =
3646 if n = -1
3647 then (
3648 m_narrow_pattern <- pattern;
3649 m_items <- Array.of_list accu
3651 else
3652 let (s, _, _) as o = m_items.(n) in
3653 let accu =
3654 if (try ignore (Str.search_forward re s 0); true
3655 with Not_found -> false)
3656 then o :: accu
3657 else accu
3659 loop accu (n-1)
3661 loop [] (Array.length m_items - 1)
3663 method denarrow =
3664 m_orig_items <- (
3665 if usebookmarks
3666 then Array.of_list state.bookmarks
3667 else state.outlines
3669 m_items <- m_orig_items
3671 method remove m =
3672 if usebookmarks
3673 then
3674 if m >= 0 && m < Array.length m_items
3675 then (
3676 m_hadremovals <- true;
3677 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3678 let n = if n >= m then n+1 else n in
3679 m_items.(n)
3683 method reset anchor items =
3684 m_hadremovals <- false;
3685 if m_orig_items == empty || m_prev_items != items
3686 then (
3687 m_orig_items <- items;
3688 if String.length m_narrow_pattern = 0
3689 then m_items <- items;
3691 m_prev_items <- items;
3692 let rely = getanchory anchor in
3693 let active =
3694 let rec loop n best bestd =
3695 if n = Array.length m_items
3696 then best
3697 else
3698 let (_, _, anchor) = m_items.(n) in
3699 let orely = getanchory anchor in
3700 let d = abs (orely - rely) in
3701 if d < bestd
3702 then loop (n+1) n d
3703 else loop (n+1) best bestd
3705 loop 0 ~-1 max_int
3707 m_active <- active;
3708 m_first <- firstof m_first active
3709 end)
3712 let enterselector usebookmarks =
3713 let source = outlinesource usebookmarks in
3714 fun errmsg ->
3715 let outlines =
3716 if usebookmarks
3717 then Array.of_list state.bookmarks
3718 else state.outlines
3720 if Array.length outlines = 0
3721 then (
3722 showtext ' ' errmsg;
3724 else (
3725 state.text <- source#greetmsg;
3726 Wsi.setcursor Wsi.CURSOR_INHERIT;
3727 let anchor = getanchor () in
3728 source#reset anchor outlines;
3729 state.uioh <- coe (new outlinelistview ~source);
3730 G.postRedisplay "enter selector";
3734 let enteroutlinemode =
3735 let f = enterselector false in
3736 fun ()-> f "Document has no outline";
3739 let enterbookmarkmode =
3740 let f = enterselector true in
3741 fun () -> f "Document has no bookmarks (yet)";
3744 let color_of_string s =
3745 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3746 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3750 let color_to_string (r, g, b) =
3751 let r = truncate (r *. 256.0)
3752 and g = truncate (g *. 256.0)
3753 and b = truncate (b *. 256.0) in
3754 Printf.sprintf "%d/%d/%d" r g b
3757 let irect_of_string s =
3758 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3761 let irect_to_string (x0,y0,x1,y1) =
3762 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3765 let makecheckers () =
3766 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3767 following to say:
3768 converted by Issac Trotts. July 25, 2002 *)
3769 let image_height = 64
3770 and image_width = 64 in
3772 let make_image () =
3773 let image =
3774 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3776 for i = 0 to image_width - 1 do
3777 for j = 0 to image_height - 1 do
3778 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3779 (if (i land 8 ) lxor (j land 8) = 0
3780 then [|255;255;255|] else [|200;200;200|])
3781 done
3782 done;
3783 image
3785 let image = make_image () in
3786 let id = GlTex.gen_texture () in
3787 GlTex.bind_texture `texture_2d id;
3788 GlPix.store (`unpack_alignment 1);
3789 GlTex.image2d image;
3790 List.iter (GlTex.parameter ~target:`texture_2d)
3791 [ `wrap_s `repeat;
3792 `wrap_t `repeat;
3793 `mag_filter `nearest;
3794 `min_filter `nearest ];
3798 let setcheckers enabled =
3799 match state.texid with
3800 | None ->
3801 if enabled then state.texid <- Some (makecheckers ())
3803 | Some texid ->
3804 if not enabled
3805 then (
3806 GlTex.delete_texture texid;
3807 state.texid <- None;
3811 let int_of_string_with_suffix s =
3812 let l = String.length s in
3813 let s1, shift =
3814 if l > 1
3815 then
3816 let suffix = Char.lowercase s.[l-1] in
3817 match suffix with
3818 | 'k' -> String.sub s 0 (l-1), 10
3819 | 'm' -> String.sub s 0 (l-1), 20
3820 | 'g' -> String.sub s 0 (l-1), 30
3821 | _ -> s, 0
3822 else s, 0
3824 let n = int_of_string s1 in
3825 let m = n lsl shift in
3826 if m < 0 || m < n
3827 then raise (Failure "value too large")
3828 else m
3831 let string_with_suffix_of_int n =
3832 if n = 0
3833 then "0"
3834 else
3835 let n, s =
3836 if n = 0
3837 then 0, ""
3838 else (
3839 if n land ((1 lsl 20) - 1) = 0
3840 then n lsr 20, "M"
3841 else (
3842 if n land ((1 lsl 10) - 1) = 0
3843 then n lsr 10, "K"
3844 else n, ""
3848 let rec loop s n =
3849 let h = n mod 1000 in
3850 let n = n / 1000 in
3851 if n = 0
3852 then string_of_int h ^ s
3853 else (
3854 let s = Printf.sprintf "_%03d%s" h s in
3855 loop s n
3858 loop "" n ^ s;
3861 let defghyllscroll = (40, 8, 32);;
3862 let ghyllscroll_of_string s =
3863 let (n, a, b) as nab =
3864 if s = "default"
3865 then defghyllscroll
3866 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3868 if n <= a || n <= b || a >= b
3869 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3870 nab;
3873 let ghyllscroll_to_string ((n, a, b) as nab) =
3874 if nab = defghyllscroll
3875 then "default"
3876 else Printf.sprintf "%d,%d,%d" n a b;
3879 let describe_location () =
3880 let f (fn, _) l =
3881 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3883 let fn, ln = List.fold_left f (-1, -1) state.layout in
3884 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3885 let percent =
3886 if maxy <= 0
3887 then 100.
3888 else (100. *. (float state.y /. float maxy))
3890 if fn = ln
3891 then
3892 Printf.sprintf "page %d of %d [%.2f%%]"
3893 (fn+1) state.pagecount percent
3894 else
3895 Printf.sprintf
3896 "pages %d-%d of %d [%.2f%%]"
3897 (fn+1) (ln+1) state.pagecount percent
3900 let enterinfomode =
3901 let btos b = if b then "\xe2\x88\x9a" else "" in
3902 let showextended = ref false in
3903 let leave mode = function
3904 | Confirm -> state.mode <- mode
3905 | Cancel -> state.mode <- mode in
3906 let src =
3907 (object
3908 val mutable m_first_time = true
3909 val mutable m_l = []
3910 val mutable m_a = [||]
3911 val mutable m_prev_uioh = nouioh
3912 val mutable m_prev_mode = View
3914 inherit lvsourcebase
3916 method reset prev_mode prev_uioh =
3917 m_a <- Array.of_list (List.rev m_l);
3918 m_l <- [];
3919 m_prev_mode <- prev_mode;
3920 m_prev_uioh <- prev_uioh;
3921 if m_first_time
3922 then (
3923 let rec loop n =
3924 if n >= Array.length m_a
3925 then ()
3926 else
3927 match m_a.(n) with
3928 | _, _, _, Action _ -> m_active <- n
3929 | _ -> loop (n+1)
3931 loop 0;
3932 m_first_time <- false;
3935 method int name get set =
3936 m_l <-
3937 (name, `int get, 1, Action (
3938 fun u ->
3939 let ondone s =
3940 try set (int_of_string s)
3941 with exn ->
3942 state.text <- Printf.sprintf "bad integer `%s': %s"
3943 s (Printexc.to_string exn)
3945 state.text <- "";
3946 let te = name ^ ": ", "", None, intentry, ondone in
3947 state.mode <- Textentry (te, leave m_prev_mode);
3949 )) :: m_l
3951 method int_with_suffix name get set =
3952 m_l <-
3953 (name, `intws get, 1, Action (
3954 fun u ->
3955 let ondone s =
3956 try set (int_of_string_with_suffix s)
3957 with exn ->
3958 state.text <- Printf.sprintf "bad integer `%s': %s"
3959 s (Printexc.to_string exn)
3961 state.text <- "";
3962 let te =
3963 name ^ ": ", "", None, intentry_with_suffix, ondone
3965 state.mode <- Textentry (te, leave m_prev_mode);
3967 )) :: m_l
3969 method bool ?(offset=1) ?(btos=btos) name get set =
3970 m_l <-
3971 (name, `bool (btos, get), offset, Action (
3972 fun u ->
3973 let v = get () in
3974 set (not v);
3976 )) :: m_l
3978 method color name get set =
3979 m_l <-
3980 (name, `color get, 1, Action (
3981 fun u ->
3982 let invalid = (nan, nan, nan) in
3983 let ondone s =
3984 let c =
3985 try color_of_string s
3986 with exn ->
3987 state.text <- Printf.sprintf "bad color `%s': %s"
3988 s (Printexc.to_string exn);
3989 invalid
3991 if c <> invalid
3992 then set c;
3994 let te = name ^ ": ", "", None, textentry, ondone in
3995 state.text <- color_to_string (get ());
3996 state.mode <- Textentry (te, leave m_prev_mode);
3998 )) :: m_l
4000 method string name get set =
4001 m_l <-
4002 (name, `string get, 1, Action (
4003 fun u ->
4004 let ondone s = set s in
4005 let te = name ^ ": ", "", None, textentry, ondone in
4006 state.mode <- Textentry (te, leave m_prev_mode);
4008 )) :: m_l
4010 method colorspace name get set =
4011 m_l <-
4012 (name, `string get, 1, Action (
4013 fun _ ->
4014 let source =
4015 let vals = [| "rgb"; "bgr"; "gray" |] in
4016 (object
4017 inherit lvsourcebase
4019 initializer
4020 m_active <- int_of_colorspace conf.colorspace;
4021 m_first <- 0;
4023 method getitemcount = Array.length vals
4024 method getitem n = (vals.(n), 0)
4025 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4026 ignore (uioh, first, pan, qsearch);
4027 if not cancel then set active;
4028 None
4029 method hasaction _ = true
4030 end)
4032 state.text <- "";
4033 let modehash = findkeyhash conf "info" in
4034 coe (new listview ~source ~trusted:true ~modehash)
4035 )) :: m_l
4037 method caption s offset =
4038 m_l <- (s, `empty, offset, Noaction) :: m_l
4040 method caption2 s f offset =
4041 m_l <- (s, `string f, offset, Noaction) :: m_l
4043 method getitemcount = Array.length m_a
4045 method getitem n =
4046 let tostr = function
4047 | `int f -> string_of_int (f ())
4048 | `intws f -> string_with_suffix_of_int (f ())
4049 | `string f -> f ()
4050 | `color f -> color_to_string (f ())
4051 | `bool (btos, f) -> btos (f ())
4052 | `empty -> ""
4054 let name, t, offset, _ = m_a.(n) in
4055 ((let s = tostr t in
4056 if String.length s > 0
4057 then Printf.sprintf "%s\t%s" name s
4058 else name),
4059 offset)
4061 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4062 let uiohopt =
4063 if not cancel
4064 then (
4065 m_qsearch <- qsearch;
4066 let uioh =
4067 match m_a.(active) with
4068 | _, _, _, Action f -> f uioh
4069 | _ -> uioh
4071 Some uioh
4073 else None
4075 m_active <- active;
4076 m_first <- first;
4077 m_pan <- pan;
4078 uiohopt
4080 method hasaction n =
4081 match m_a.(n) with
4082 | _, _, _, Action _ -> true
4083 | _ -> false
4084 end)
4086 let rec fillsrc prevmode prevuioh =
4087 let sep () = src#caption "" 0 in
4088 let colorp name get set =
4089 src#string name
4090 (fun () -> color_to_string (get ()))
4091 (fun v ->
4093 let c = color_of_string v in
4094 set c
4095 with exn ->
4096 state.text <- Printf.sprintf "bad color `%s': %s"
4097 v (Printexc.to_string exn);
4100 let oldmode = state.mode in
4101 let birdseye = isbirdseye state.mode in
4103 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4105 src#bool "presentation mode"
4106 (fun () -> conf.presentation)
4107 (fun v ->
4108 conf.presentation <- v;
4109 state.anchor <- getanchor ();
4110 represent ());
4112 src#bool "ignore case in searches"
4113 (fun () -> conf.icase)
4114 (fun v -> conf.icase <- v);
4116 src#bool "preload"
4117 (fun () -> conf.preload)
4118 (fun v -> conf.preload <- v);
4120 src#bool "highlight links"
4121 (fun () -> conf.hlinks)
4122 (fun v -> conf.hlinks <- v);
4124 src#bool "under info"
4125 (fun () -> conf.underinfo)
4126 (fun v -> conf.underinfo <- v);
4128 src#bool "persistent bookmarks"
4129 (fun () -> conf.savebmarks)
4130 (fun v -> conf.savebmarks <- v);
4132 src#bool "proportional display"
4133 (fun () -> conf.proportional)
4134 (fun v -> reqlayout conf.angle v);
4136 src#bool "trim margins"
4137 (fun () -> conf.trimmargins)
4138 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4140 src#bool "persistent location"
4141 (fun () -> conf.jumpback)
4142 (fun v -> conf.jumpback <- v);
4144 sep ();
4145 src#int "inter-page space"
4146 (fun () -> conf.interpagespace)
4147 (fun n ->
4148 conf.interpagespace <- n;
4149 let pageno, py =
4150 match state.layout with
4151 | [] -> 0, 0
4152 | l :: _ ->
4153 l.pageno, l.pagey
4155 state.maxy <- calcheight ();
4156 let y = getpagey pageno in
4157 gotoy (y + py)
4160 src#int "page bias"
4161 (fun () -> conf.pagebias)
4162 (fun v -> conf.pagebias <- v);
4164 src#int "scroll step"
4165 (fun () -> conf.scrollstep)
4166 (fun n -> conf.scrollstep <- n);
4168 src#int "auto scroll step"
4169 (fun () ->
4170 match state.autoscroll with
4171 | Some step -> step
4172 | _ -> conf.autoscrollstep)
4173 (fun n ->
4174 if state.autoscroll <> None
4175 then state.autoscroll <- Some n;
4176 conf.autoscrollstep <- n);
4178 src#int "zoom"
4179 (fun () -> truncate (conf.zoom *. 100.))
4180 (fun v -> setzoom ((float v) /. 100.));
4182 src#int "rotation"
4183 (fun () -> conf.angle)
4184 (fun v -> reqlayout v conf.proportional);
4186 src#int "scroll bar width"
4187 (fun () -> state.scrollw)
4188 (fun v ->
4189 state.scrollw <- v;
4190 conf.scrollbw <- v;
4191 reshape conf.winw conf.winh;
4194 src#int "scroll handle height"
4195 (fun () -> conf.scrollh)
4196 (fun v -> conf.scrollh <- v;);
4198 src#int "thumbnail width"
4199 (fun () -> conf.thumbw)
4200 (fun v ->
4201 conf.thumbw <- min 4096 v;
4202 match oldmode with
4203 | Birdseye beye ->
4204 leavebirdseye beye false;
4205 enterbirdseye ()
4206 | _ -> ()
4209 let mode = state.mode in
4210 src#string "columns"
4211 (fun () ->
4212 match conf.columns with
4213 | Csingle -> "1"
4214 | Cmulti (multi, _) -> multicolumns_to_string multi
4215 | Csplit (count, _) -> "-" ^ string_of_int count
4217 (fun v ->
4218 let n, a, b = multicolumns_of_string v in
4219 setcolumns mode n a b);
4221 sep ();
4222 src#caption "Presentation mode" 0;
4223 src#bool "scrollbar visible"
4224 (fun () -> conf.scrollbarinpm)
4225 (fun v ->
4226 if v != conf.scrollbarinpm
4227 then (
4228 conf.scrollbarinpm <- v;
4229 if conf.presentation
4230 then (
4231 state.scrollw <- if v then conf.scrollbw else 0;
4232 reshape conf.winw conf.winh;
4237 sep ();
4238 src#caption "Pixmap cache" 0;
4239 src#int_with_suffix "size (advisory)"
4240 (fun () -> conf.memlimit)
4241 (fun v -> conf.memlimit <- v);
4243 src#caption2 "used"
4244 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4245 (string_with_suffix_of_int state.memused)
4246 (Hashtbl.length state.tilemap)) 1;
4248 sep ();
4249 src#caption "Layout" 0;
4250 src#caption2 "Dimension"
4251 (fun () ->
4252 Printf.sprintf "%dx%d (virtual %dx%d)"
4253 conf.winw conf.winh
4254 state.w state.maxy)
4256 if conf.debug
4257 then
4258 src#caption2 "Position" (fun () ->
4259 Printf.sprintf "%dx%d" state.x state.y
4261 else
4262 src#caption2 "Visible" (fun () -> describe_location ()) 1
4265 sep ();
4266 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4267 "Save these parameters as global defaults at exit"
4268 (fun () -> conf.bedefault)
4269 (fun v -> conf.bedefault <- v)
4272 sep ();
4273 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4274 src#bool ~offset:0 ~btos "Extended parameters"
4275 (fun () -> !showextended)
4276 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4277 if !showextended
4278 then (
4279 src#bool "checkers"
4280 (fun () -> conf.checkers)
4281 (fun v -> conf.checkers <- v; setcheckers v);
4282 src#bool "update cursor"
4283 (fun () -> conf.updatecurs)
4284 (fun v -> conf.updatecurs <- v);
4285 src#bool "verbose"
4286 (fun () -> conf.verbose)
4287 (fun v -> conf.verbose <- v);
4288 src#bool "invert colors"
4289 (fun () -> conf.invert)
4290 (fun v -> conf.invert <- v);
4291 src#bool "max fit"
4292 (fun () -> conf.maxhfit)
4293 (fun v -> conf.maxhfit <- v);
4294 src#bool "redirect stderr"
4295 (fun () -> conf.redirectstderr)
4296 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4297 src#string "uri launcher"
4298 (fun () -> conf.urilauncher)
4299 (fun v -> conf.urilauncher <- v);
4300 src#string "path launcher"
4301 (fun () -> conf.pathlauncher)
4302 (fun v -> conf.pathlauncher <- v);
4303 src#string "tile size"
4304 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4305 (fun v ->
4307 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4308 conf.tilew <- max 64 w;
4309 conf.tileh <- max 64 h;
4310 flushtiles ();
4311 with exn ->
4312 state.text <- Printf.sprintf "bad tile size `%s': %s"
4313 v (Printexc.to_string exn));
4314 src#int "texture count"
4315 (fun () -> conf.texcount)
4316 (fun v ->
4317 if realloctexts v
4318 then conf.texcount <- v
4319 else showtext '!' " Failed to set texture count please retry later"
4321 src#int "slice height"
4322 (fun () -> conf.sliceheight)
4323 (fun v ->
4324 conf.sliceheight <- v;
4325 wcmd "sliceh %d" conf.sliceheight;
4327 src#int "anti-aliasing level"
4328 (fun () -> conf.aalevel)
4329 (fun v ->
4330 conf.aalevel <- bound v 0 8;
4331 state.anchor <- getanchor ();
4332 opendoc state.path state.password;
4334 src#int "ui font size"
4335 (fun () -> fstate.fontsize)
4336 (fun v -> setfontsize (bound v 5 100));
4337 colorp "background color"
4338 (fun () -> conf.bgcolor)
4339 (fun v -> conf.bgcolor <- v);
4340 src#bool "crop hack"
4341 (fun () -> conf.crophack)
4342 (fun v -> conf.crophack <- v);
4343 src#string "trim fuzz"
4344 (fun () -> irect_to_string conf.trimfuzz)
4345 (fun v ->
4347 conf.trimfuzz <- irect_of_string v;
4348 if conf.trimmargins
4349 then settrim true conf.trimfuzz;
4350 with exn ->
4351 state.text <- Printf.sprintf "bad irect `%s': %s"
4352 v (Printexc.to_string exn)
4354 src#string "throttle"
4355 (fun () ->
4356 match conf.maxwait with
4357 | None -> "show place holder if page is not ready"
4358 | Some time ->
4359 if time = infinity
4360 then "wait for page to fully render"
4361 else
4362 "wait " ^ string_of_float time
4363 ^ " seconds before showing placeholder"
4365 (fun v ->
4367 let f = float_of_string v in
4368 if f <= 0.0
4369 then conf.maxwait <- None
4370 else conf.maxwait <- Some f
4371 with exn ->
4372 state.text <- Printf.sprintf "bad time `%s': %s"
4373 v (Printexc.to_string exn)
4375 src#string "ghyll scroll"
4376 (fun () ->
4377 match conf.ghyllscroll with
4378 | None -> ""
4379 | Some nab -> ghyllscroll_to_string nab
4381 (fun v ->
4383 let gs =
4384 if String.length v = 0
4385 then None
4386 else Some (ghyllscroll_of_string v)
4388 conf.ghyllscroll <- gs
4389 with exn ->
4390 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4391 v (Printexc.to_string exn)
4393 src#string "selection command"
4394 (fun () -> conf.selcmd)
4395 (fun v -> conf.selcmd <- v);
4396 src#colorspace "color space"
4397 (fun () -> colorspace_to_string conf.colorspace)
4398 (fun v ->
4399 conf.colorspace <- colorspace_of_int v;
4400 wcmd "cs %d" v;
4401 load state.layout;
4405 sep ();
4406 src#caption "Document" 0;
4407 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4408 src#caption2 "Pages"
4409 (fun () -> string_of_int state.pagecount) 1;
4410 src#caption2 "Dimensions"
4411 (fun () -> string_of_int (List.length state.pdims)) 1;
4412 if conf.trimmargins
4413 then (
4414 sep ();
4415 src#caption "Trimmed margins" 0;
4416 src#caption2 "Dimensions"
4417 (fun () -> string_of_int (List.length state.pdims)) 1;
4420 src#reset prevmode prevuioh;
4422 fun () ->
4423 state.text <- "";
4424 let prevmode = state.mode
4425 and prevuioh = state.uioh in
4426 fillsrc prevmode prevuioh;
4427 let source = (src :> lvsource) in
4428 let modehash = findkeyhash conf "info" in
4429 state.uioh <- coe (object (self)
4430 inherit listview ~source ~trusted:true ~modehash as super
4431 val mutable m_prevmemused = 0
4432 method infochanged = function
4433 | Memused ->
4434 if m_prevmemused != state.memused
4435 then (
4436 m_prevmemused <- state.memused;
4437 G.postRedisplay "memusedchanged";
4439 | Pdim -> G.postRedisplay "pdimchanged"
4440 | Docinfo -> fillsrc prevmode prevuioh
4442 method key key mask =
4443 if not (Wsi.withctrl mask)
4444 then
4445 match key with
4446 | 0xff51 -> coe (self#updownlevel ~-1)
4447 | 0xff53 -> coe (self#updownlevel 1)
4448 | _ -> super#key key mask
4449 else super#key key mask
4450 end);
4451 G.postRedisplay "info";
4454 let enterhelpmode =
4455 let source =
4456 (object
4457 inherit lvsourcebase
4458 method getitemcount = Array.length state.help
4459 method getitem n =
4460 let s, n, _ = state.help.(n) in
4461 (s, n)
4463 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4464 let optuioh =
4465 if not cancel
4466 then (
4467 m_qsearch <- qsearch;
4468 match state.help.(active) with
4469 | _, _, Action f -> Some (f uioh)
4470 | _ -> Some (uioh)
4472 else None
4474 m_active <- active;
4475 m_first <- first;
4476 m_pan <- pan;
4477 optuioh
4479 method hasaction n =
4480 match state.help.(n) with
4481 | _, _, Action _ -> true
4482 | _ -> false
4484 initializer
4485 m_active <- -1
4486 end)
4487 in fun () ->
4488 let modehash = findkeyhash conf "help" in
4489 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4490 G.postRedisplay "help";
4493 let entermsgsmode =
4494 let msgsource =
4495 let re = Str.regexp "[\r\n]" in
4496 (object
4497 inherit lvsourcebase
4498 val mutable m_items = [||]
4500 method getitemcount = 1 + Array.length m_items
4502 method getitem n =
4503 if n = 0
4504 then "[Clear]", 0
4505 else m_items.(n-1), 0
4507 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4508 ignore uioh;
4509 if not cancel
4510 then (
4511 if active = 0
4512 then Buffer.clear state.errmsgs;
4513 m_qsearch <- qsearch;
4515 m_active <- active;
4516 m_first <- first;
4517 m_pan <- pan;
4518 None
4520 method hasaction n =
4521 n = 0
4523 method reset =
4524 state.newerrmsgs <- false;
4525 let l = Str.split re (Buffer.contents state.errmsgs) in
4526 m_items <- Array.of_list l
4528 initializer
4529 m_active <- 0
4530 end)
4531 in fun () ->
4532 state.text <- "";
4533 msgsource#reset;
4534 let source = (msgsource :> lvsource) in
4535 let modehash = findkeyhash conf "listview" in
4536 state.uioh <- coe (object
4537 inherit listview ~source ~trusted:false ~modehash as super
4538 method display =
4539 if state.newerrmsgs
4540 then msgsource#reset;
4541 super#display
4542 end);
4543 G.postRedisplay "msgs";
4546 let quickbookmark ?title () =
4547 match state.layout with
4548 | [] -> ()
4549 | l :: _ ->
4550 let title =
4551 match title with
4552 | None ->
4553 let sec = Unix.gettimeofday () in
4554 let tm = Unix.localtime sec in
4555 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4556 (l.pageno+1)
4557 tm.Unix.tm_mday
4558 tm.Unix.tm_mon
4559 (tm.Unix.tm_year + 1900)
4560 tm.Unix.tm_hour
4561 tm.Unix.tm_min
4562 | Some title -> title
4564 state.bookmarks <-
4565 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4566 :: state.bookmarks
4569 let doreshape w h =
4570 state.fullscreen <- None;
4571 Wsi.reshape w h;
4574 let setautoscrollspeed step goingdown =
4575 let incr = max 1 ((abs step) / 2) in
4576 let incr = if goingdown then incr else -incr in
4577 let astep = step + incr in
4578 state.autoscroll <- Some astep;
4581 let gotounder = function
4582 | Ulinkgoto (pageno, top) ->
4583 if pageno >= 0
4584 then (
4585 addnav ();
4586 gotopage1 pageno top;
4589 | Ulinkuri s ->
4590 gotouri s
4592 | Uremote (filename, pageno) ->
4593 let path =
4594 if Sys.file_exists filename
4595 then filename
4596 else
4597 let dir = Filename.dirname state.path in
4598 let path = Filename.concat dir filename in
4599 if Sys.file_exists path
4600 then path
4601 else ""
4603 if String.length path > 0
4604 then (
4605 let anchor = getanchor () in
4606 let ranchor = state.path, state.password, anchor in
4607 state.anchor <- (pageno, 0.0);
4608 state.ranchors <- ranchor :: state.ranchors;
4609 opendoc path "";
4611 else showtext '!' ("Could not find " ^ filename)
4613 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4616 let canpan () =
4617 match conf.columns with
4618 | Csplit _ -> true
4619 | _ -> conf.zoom > 1.0
4622 let viewkeyboard key mask =
4623 let enttext te =
4624 let mode = state.mode in
4625 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4626 state.text <- "";
4627 enttext ();
4628 G.postRedisplay "view:enttext"
4630 let ctrl = Wsi.withctrl mask in
4631 match key with
4632 | 81 -> (* Q *)
4633 exit 0
4635 | 0xff63 -> (* insert *)
4636 if conf.angle mod 360 = 0
4637 then (
4638 state.mode <- LinkNav (Ltgendir 0);
4639 gotoy state.y;
4641 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4643 | 0xff1b | 113 -> (* escape / q *)
4644 begin match state.mstate with
4645 | Mzoomrect _ ->
4646 state.mstate <- Mnone;
4647 Wsi.setcursor Wsi.CURSOR_INHERIT;
4648 G.postRedisplay "kill zoom rect";
4649 | _ ->
4650 match state.ranchors with
4651 | [] -> raise Quit
4652 | (path, password, anchor) :: rest ->
4653 state.ranchors <- rest;
4654 state.anchor <- anchor;
4655 opendoc path password
4656 end;
4658 | 0xff08 -> (* backspace *)
4659 let y = getnav ~-1 in
4660 gotoy_and_clear_text y
4662 | 111 -> (* o *)
4663 enteroutlinemode ()
4665 | 117 -> (* u *)
4666 state.rects <- [];
4667 state.text <- "";
4668 G.postRedisplay "dehighlight";
4670 | 47 | 63 -> (* / ? *)
4671 let ondone isforw s =
4672 cbput state.hists.pat s;
4673 state.searchpattern <- s;
4674 search s isforw
4676 let s = String.create 1 in
4677 s.[0] <- Char.chr key;
4678 enttext (s, "", Some (onhist state.hists.pat),
4679 textentry, ondone (key = 47))
4681 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4682 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4683 setzoom (conf.zoom +. incr)
4685 | 43 | 0xffab -> (* + *)
4686 let ondone s =
4687 let n =
4688 try int_of_string s with exc ->
4689 state.text <- Printf.sprintf "bad integer `%s': %s"
4690 s (Printexc.to_string exc);
4691 max_int
4693 if n != max_int
4694 then (
4695 conf.pagebias <- n;
4696 state.text <- "page bias is now " ^ string_of_int n;
4699 enttext ("page bias: ", "", None, intentry, ondone)
4701 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4702 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4703 setzoom (max 0.01 (conf.zoom -. decr))
4705 | 45 | 0xffad -> (* - *)
4706 let ondone msg = state.text <- msg in
4707 enttext (
4708 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4709 optentry state.mode, ondone
4712 | 48 when ctrl -> (* ctrl-0 *)
4713 setzoom 1.0
4715 | 49 when ctrl -> (* 1 *)
4716 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4717 if zoom < 1.0
4718 then setzoom zoom
4720 | 0xffc6 -> (* f9 *)
4721 togglebirdseye ()
4723 | 57 when ctrl -> (* ctrl-9 *)
4724 togglebirdseye ()
4726 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4727 when not ctrl -> (* 0..9 *)
4728 let ondone s =
4729 let n =
4730 try int_of_string s with exc ->
4731 state.text <- Printf.sprintf "bad integer `%s': %s"
4732 s (Printexc.to_string exc);
4735 if n >= 0
4736 then (
4737 addnav ();
4738 cbput state.hists.pag (string_of_int n);
4739 gotopage1 (n + conf.pagebias - 1) 0;
4742 let pageentry text key =
4743 match Char.unsafe_chr key with
4744 | 'g' -> TEdone text
4745 | _ -> intentry text key
4747 let text = "x" in text.[0] <- Char.chr key;
4748 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4750 | 98 -> (* b *)
4751 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4752 reshape conf.winw conf.winh;
4754 | 108 -> (* l *)
4755 conf.hlinks <- not conf.hlinks;
4756 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4757 G.postRedisplay "toggle highlightlinks";
4759 | 70 -> (* F *)
4760 state.glinks <- true;
4761 let mode = state.mode in
4762 state.mode <- Textentry (
4763 (":", "", None, linknentry, linkndone (fun under ->
4764 addnav ();
4765 gotounder under
4767 ), fun _ ->
4768 state.glinks <- false;
4769 state.mode <- mode
4771 state.text <- "";
4772 G.postRedisplay "view:linkent(F)"
4774 | 121 -> (* y *)
4775 state.glinks <- true;
4776 let mode = state.mode in
4777 state.mode <- Textentry (
4778 (":", "", None, linknentry, linkndone (fun under ->
4779 match Ne.pipe () with
4780 | Ne.Exn exn ->
4781 showtext '!' (Printf.sprintf "pipe failed: %s"
4782 (Printexc.to_string exn));
4783 | Ne.Res (r, w) ->
4784 begin try
4785 popen conf.selcmd [r, 0; w, -1]
4786 with exn ->
4787 showtext '!'
4788 (Printf.sprintf "failed to execute %s: %s"
4789 conf.selcmd (Printexc.to_string exn))
4790 end;
4791 let clo cap fd =
4792 Ne.clo fd (fun msg ->
4793 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4796 let s = undertext under in
4797 (try
4798 let l = String.length s in
4799 let n = Unix.write w s 0 l in
4800 if n != l
4801 then
4802 showtext '!'
4803 (Printf.sprintf
4804 "failed to write %d characters to sel pipe, wrote %d"
4807 with exn ->
4808 showtext '!'
4809 (Printf.sprintf "failed to write to sel pipe: %s"
4810 (Printexc.to_string exn)
4813 clo "pipe/r" r;
4814 clo "pipe/w" w;
4817 fun _ ->
4818 state.glinks <- false;
4819 state.mode <- mode
4821 state.text <- "";
4822 G.postRedisplay "view:linkent"
4824 | 97 -> (* a *)
4825 begin match state.autoscroll with
4826 | Some step ->
4827 conf.autoscrollstep <- step;
4828 state.autoscroll <- None
4829 | None ->
4830 if conf.autoscrollstep = 0
4831 then state.autoscroll <- Some 1
4832 else state.autoscroll <- Some conf.autoscrollstep
4835 | 112 when ctrl -> (* ctrl-p *)
4836 launchpath ()
4838 | 80 -> (* P *)
4839 conf.presentation <- not conf.presentation;
4840 if conf.presentation
4841 then (
4842 if not conf.scrollbarinpm
4843 then state.scrollw <- 0;
4845 else
4846 state.scrollw <- conf.scrollbw;
4848 showtext ' ' ("presentation mode " ^
4849 if conf.presentation then "on" else "off");
4850 state.anchor <- getanchor ();
4851 represent ()
4853 | 102 -> (* f *)
4854 begin match state.fullscreen with
4855 | None ->
4856 state.fullscreen <- Some (conf.winw, conf.winh);
4857 Wsi.fullscreen ()
4858 | Some (w, h) ->
4859 state.fullscreen <- None;
4860 doreshape w h
4863 | 103 -> (* g *)
4864 gotoy_and_clear_text 0
4866 | 71 -> (* G *)
4867 gotopage1 (state.pagecount - 1) 0
4869 | 112 | 78 -> (* p|N *)
4870 search state.searchpattern false
4872 | 110 | 0xffc0 -> (* n|F3 *)
4873 search state.searchpattern true
4875 | 116 -> (* t *)
4876 begin match state.layout with
4877 | [] -> ()
4878 | l :: _ ->
4879 gotoy_and_clear_text (getpagey l.pageno)
4882 | 32 -> (* ' ' *)
4883 begin match List.rev state.layout with
4884 | [] -> ()
4885 | l :: _ ->
4886 let pageno = min (l.pageno+1) (state.pagecount-1) in
4887 gotoy_and_clear_text (getpagey pageno)
4890 | 0xff9f | 0xffff -> (* delete *)
4891 begin match state.layout with
4892 | [] -> ()
4893 | l :: _ ->
4894 let pageno = max 0 (l.pageno-1) in
4895 gotoy_and_clear_text (getpagey pageno)
4898 | 61 -> (* = *)
4899 showtext ' ' (describe_location ());
4901 | 119 -> (* w *)
4902 begin match state.layout with
4903 | [] -> ()
4904 | l :: _ ->
4905 doreshape (l.pagew + state.scrollw) l.pageh;
4906 G.postRedisplay "w"
4909 | 39 -> (* ' *)
4910 enterbookmarkmode ()
4912 | 104 | 0xffbe -> (* h|F1 *)
4913 enterhelpmode ()
4915 | 105 -> (* i *)
4916 enterinfomode ()
4918 | 101 when conf.redirectstderr -> (* e *)
4919 entermsgsmode ()
4921 | 109 -> (* m *)
4922 let ondone s =
4923 match state.layout with
4924 | l :: _ ->
4925 state.bookmarks <-
4926 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4927 :: state.bookmarks
4928 | _ -> ()
4930 enttext ("bookmark: ", "", None, textentry, ondone)
4932 | 126 -> (* ~ *)
4933 quickbookmark ();
4934 showtext ' ' "Quick bookmark added";
4936 | 122 -> (* z *)
4937 begin match state.layout with
4938 | l :: _ ->
4939 let rect = getpdimrect l.pagedimno in
4940 let w, h =
4941 if conf.crophack
4942 then
4943 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4944 truncate (1.2 *. (rect.(3) -. rect.(0))))
4945 else
4946 (truncate (rect.(1) -. rect.(0)),
4947 truncate (rect.(3) -. rect.(0)))
4949 let w = truncate ((float w)*.conf.zoom)
4950 and h = truncate ((float h)*.conf.zoom) in
4951 if w != 0 && h != 0
4952 then (
4953 state.anchor <- getanchor ();
4954 doreshape (w + state.scrollw) (h + conf.interpagespace)
4956 G.postRedisplay "z";
4958 | [] -> ()
4961 | 50 when ctrl -> (* ctrl-2 *)
4962 let maxw = getmaxw () in
4963 if maxw > 0.0
4964 then setzoom (maxw /. float conf.winw)
4966 | 60 | 62 -> (* < > *)
4967 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4969 | 91 | 93 -> (* [ ] *)
4970 conf.colorscale <-
4971 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4973 G.postRedisplay "brightness";
4975 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4976 setzoom state.prevzoom
4978 | 107 | 0xff52 -> (* k up *)
4979 begin match state.autoscroll with
4980 | None ->
4981 begin match state.mode with
4982 | Birdseye beye -> upbirdseye 1 beye
4983 | _ ->
4984 if ctrl
4985 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4986 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4988 | Some n ->
4989 setautoscrollspeed n false
4992 | 106 | 0xff54 -> (* j down *)
4993 begin match state.autoscroll with
4994 | None ->
4995 begin match state.mode with
4996 | Birdseye beye -> downbirdseye 1 beye
4997 | _ ->
4998 if ctrl
4999 then gotoy_and_clear_text (clamp (conf.winh/2))
5000 else gotoy_and_clear_text (clamp conf.scrollstep)
5002 | Some n ->
5003 setautoscrollspeed n true
5006 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5007 if canpan ()
5008 then
5009 let dx =
5010 if ctrl
5011 then conf.winw / 2
5012 else 10
5014 let dx = if key = 0xff51 then dx else -dx in
5015 state.x <- state.x + dx;
5016 gotoy_and_clear_text state.y
5017 else (
5018 state.text <- "";
5019 G.postRedisplay "lef/right"
5022 | 0xff55 -> (* prior *)
5023 let y =
5024 if ctrl
5025 then
5026 match state.layout with
5027 | [] -> state.y
5028 | l :: _ -> state.y - l.pagey
5029 else
5030 clamp (-conf.winh)
5032 gotoghyll y
5034 | 0xff56 -> (* next *)
5035 let y =
5036 if ctrl
5037 then
5038 match List.rev state.layout with
5039 | [] -> state.y
5040 | l :: _ -> getpagey l.pageno
5041 else
5042 clamp conf.winh
5044 gotoghyll y
5046 | 0xff50 -> gotoghyll 0
5047 | 0xff57 -> gotoghyll (clamp state.maxy)
5048 | 0xff53 when Wsi.withalt mask ->
5049 gotoghyll (getnav ~-1)
5050 | 0xff51 when Wsi.withalt mask ->
5051 gotoghyll (getnav 1)
5053 | 114 -> (* r *)
5054 state.anchor <- getanchor ();
5055 opendoc state.path state.password
5057 | 118 when conf.debug -> (* v *)
5058 state.rects <- [];
5059 List.iter (fun l ->
5060 match getopaque l.pageno with
5061 | None -> ()
5062 | Some opaque ->
5063 let x0, y0, x1, y1 = pagebbox opaque in
5064 let a,b = float x0, float y0 in
5065 let c,d = float x1, float y0 in
5066 let e,f = float x1, float y1 in
5067 let h,j = float x0, float y1 in
5068 let rect = (a,b,c,d,e,f,h,j) in
5069 debugrect rect;
5070 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5071 ) state.layout;
5072 G.postRedisplay "v";
5074 | _ ->
5075 vlog "huh? %s" (Wsi.keyname key)
5078 let linknavkeyboard key mask linknav =
5079 let getpage pageno =
5080 let rec loop = function
5081 | [] -> None
5082 | l :: _ when l.pageno = pageno -> Some l
5083 | _ :: rest -> loop rest
5084 in loop state.layout
5086 let doexact (pageno, n) =
5087 match getopaque pageno, getpage pageno with
5088 | Some opaque, Some l ->
5089 if key = 0xff0d
5090 then
5091 let under = getlink opaque n in
5092 G.postRedisplay "link gotounder";
5093 gotounder under;
5094 state.mode <- View;
5095 else
5096 let opt, dir =
5097 match key with
5098 | 0xff50 -> (* home *)
5099 Some (findlink opaque LDfirst), -1
5101 | 0xff57 -> (* end *)
5102 Some (findlink opaque LDlast), 1
5104 | 0xff51 -> (* left *)
5105 Some (findlink opaque (LDleft n)), -1
5107 | 0xff53 -> (* right *)
5108 Some (findlink opaque (LDright n)), 1
5110 | 0xff52 -> (* up *)
5111 Some (findlink opaque (LDup n)), -1
5113 | 0xff54 -> (* down *)
5114 Some (findlink opaque (LDdown n)), 1
5116 | _ -> None, 0
5118 let pwl l dir =
5119 begin match findpwl l.pageno dir with
5120 | Pwlnotfound -> ()
5121 | Pwl pageno ->
5122 let notfound dir =
5123 state.mode <- LinkNav (Ltgendir dir);
5124 let y, h = getpageyh pageno in
5125 let y =
5126 if dir < 0
5127 then y + h - conf.winh
5128 else y
5130 gotoy y
5132 begin match getopaque pageno, getpage pageno with
5133 | Some opaque, Some _ ->
5134 let link =
5135 let ld = if dir > 0 then LDfirst else LDlast in
5136 findlink opaque ld
5138 begin match link with
5139 | Lfound m ->
5140 showlinktype (getlink opaque m);
5141 state.mode <- LinkNav (Ltexact (pageno, m));
5142 G.postRedisplay "linknav jpage";
5143 | _ -> notfound dir
5144 end;
5145 | _ -> notfound dir
5146 end;
5147 end;
5149 begin match opt with
5150 | Some Lnotfound -> pwl l dir;
5151 | Some (Lfound m) ->
5152 if m = n
5153 then pwl l dir
5154 else (
5155 let _, y0, _, y1 = getlinkrect opaque m in
5156 if y0 < l.pagey
5157 then gotopage1 l.pageno y0
5158 else (
5159 let d = fstate.fontsize + 1 in
5160 if y1 - l.pagey > l.pagevh - d
5161 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5162 else G.postRedisplay "linknav";
5164 showlinktype (getlink opaque m);
5165 state.mode <- LinkNav (Ltexact (l.pageno, m));
5168 | None -> viewkeyboard key mask
5169 end;
5170 | _ -> viewkeyboard key mask
5172 if key = 0xff63
5173 then (
5174 state.mode <- View;
5175 G.postRedisplay "leave linknav"
5177 else
5178 match linknav with
5179 | Ltgendir _ -> viewkeyboard key mask
5180 | Ltexact exact -> doexact exact
5183 let keyboard key mask =
5184 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5185 then wcmd "interrupt"
5186 else state.uioh <- state.uioh#key key mask
5189 let birdseyekeyboard key mask
5190 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5191 let incr =
5192 match conf.columns with
5193 | Csingle -> 1
5194 | Cmulti ((c, _, _), _) -> c
5195 | Csplit _ -> failwith "bird's eye split mode"
5197 match key with
5198 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5199 let y, h = getpageyh pageno in
5200 let top = (conf.winh - h) / 2 in
5201 gotoy (max 0 (y - top))
5202 | 0xff0d -> leavebirdseye beye false
5203 | 0xff1b -> leavebirdseye beye true (* escape *)
5204 | 0xff52 -> upbirdseye incr beye (* prior *)
5205 | 0xff54 -> downbirdseye incr beye (* next *)
5206 | 0xff51 -> upbirdseye 1 beye (* up *)
5207 | 0xff53 -> downbirdseye 1 beye (* down *)
5209 | 0xff55 ->
5210 begin match state.layout with
5211 | l :: _ ->
5212 if l.pagey != 0
5213 then (
5214 state.mode <- Birdseye (
5215 oconf, leftx, l.pageno, hooverpageno, anchor
5217 gotopage1 l.pageno 0;
5219 else (
5220 let layout = layout (state.y-conf.winh) conf.winh in
5221 match layout with
5222 | [] -> gotoy (clamp (-conf.winh))
5223 | l :: _ ->
5224 state.mode <- Birdseye (
5225 oconf, leftx, l.pageno, hooverpageno, anchor
5227 gotopage1 l.pageno 0
5230 | [] -> gotoy (clamp (-conf.winh))
5231 end;
5233 | 0xff56 ->
5234 begin match List.rev state.layout with
5235 | l :: _ ->
5236 let layout = layout (state.y + conf.winh) conf.winh in
5237 begin match layout with
5238 | [] ->
5239 let incr = l.pageh - l.pagevh in
5240 if incr = 0
5241 then (
5242 state.mode <-
5243 Birdseye (
5244 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5246 G.postRedisplay "birdseye pagedown";
5248 else gotoy (clamp (incr + conf.interpagespace*2));
5250 | l :: _ ->
5251 state.mode <-
5252 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5253 gotopage1 l.pageno 0;
5256 | [] -> gotoy (clamp conf.winh)
5257 end;
5259 | 0xff50 ->
5260 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5261 gotopage1 0 0
5263 | 0xff57 ->
5264 let pageno = state.pagecount - 1 in
5265 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5266 if not (pagevisible state.layout pageno)
5267 then
5268 let h =
5269 match List.rev state.pdims with
5270 | [] -> conf.winh
5271 | (_, _, h, _) :: _ -> h
5273 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5274 else G.postRedisplay "birdseye end";
5275 | _ -> viewkeyboard key mask
5278 let drawpage l linkindexbase =
5279 let color =
5280 match state.mode with
5281 | Textentry _ -> scalecolor 0.4
5282 | LinkNav _
5283 | View -> scalecolor 1.0
5284 | Birdseye (_, _, pageno, hooverpageno, _) ->
5285 if l.pageno = hooverpageno
5286 then scalecolor 0.9
5287 else (
5288 if l.pageno = pageno
5289 then scalecolor 1.0
5290 else scalecolor 0.8
5293 drawtiles l color;
5294 begin match getopaque l.pageno with
5295 | Some opaque ->
5296 if tileready l l.pagex l.pagey
5297 then
5298 let x = l.pagedispx - l.pagex
5299 and y = l.pagedispy - l.pagey in
5300 let hlmask = (if conf.hlinks then 1 else 0)
5301 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5303 let s =
5304 match state.mode with
5305 | Textentry ((_, s, _, _, _), _) when state.glinks -> s
5306 | _ -> ""
5308 postprocess opaque hlmask x y (linkindexbase, s);
5309 else 0
5311 | _ -> 0
5312 end;
5315 let scrollindicator () =
5316 let sbw, ph, sh = state.uioh#scrollph in
5317 let sbh, pw, sw = state.uioh#scrollpw in
5319 GlDraw.color (0.64, 0.64, 0.64);
5320 GlDraw.rect
5321 (float (conf.winw - sbw), 0.)
5322 (float conf.winw, float conf.winh)
5324 GlDraw.rect
5325 (0., float (conf.winh - sbh))
5326 (float (conf.winw - state.scrollw - 1), float conf.winh)
5328 GlDraw.color (0.0, 0.0, 0.0);
5330 GlDraw.rect
5331 (float (conf.winw - sbw), ph)
5332 (float conf.winw, ph +. sh)
5334 GlDraw.rect
5335 (pw, float (conf.winh - sbh))
5336 (pw +. sw, float conf.winh)
5340 let showsel () =
5341 match state.mstate with
5342 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5345 | Msel ((x0, y0), (x1, y1)) ->
5346 let rec loop = function
5347 | l :: ls ->
5348 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5349 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5350 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5351 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5352 then
5353 match getopaque l.pageno with
5354 | Some opaque ->
5355 let x0, y0 = pagetranslatepoint l x0 y0 in
5356 let x1, y1 = pagetranslatepoint l x1 y1 in
5357 seltext opaque (x0, y0, x1, y1);
5358 | _ -> ()
5359 else loop ls
5360 | [] -> ()
5362 loop state.layout
5365 let showrects rects =
5366 Gl.enable `blend;
5367 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5368 GlDraw.polygon_mode `both `fill;
5369 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5370 List.iter
5371 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5372 List.iter (fun l ->
5373 if l.pageno = pageno
5374 then (
5375 let dx = float (l.pagedispx - l.pagex) in
5376 let dy = float (l.pagedispy - l.pagey) in
5377 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5378 GlDraw.begins `quads;
5380 GlDraw.vertex2 (x0+.dx, y0+.dy);
5381 GlDraw.vertex2 (x1+.dx, y1+.dy);
5382 GlDraw.vertex2 (x2+.dx, y2+.dy);
5383 GlDraw.vertex2 (x3+.dx, y3+.dy);
5385 GlDraw.ends ();
5387 ) state.layout
5388 ) rects
5390 Gl.disable `blend;
5393 let display () =
5394 GlClear.color (scalecolor2 conf.bgcolor);
5395 GlClear.clear [`color];
5396 let rec loop linkindexbase = function
5397 | l :: rest ->
5398 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5399 loop linkindexbase rest
5400 | [] -> ()
5402 loop 0 state.layout;
5403 let rects =
5404 match state.mode with
5405 | LinkNav (Ltexact (pageno, linkno)) ->
5406 begin match getopaque pageno with
5407 | Some opaque ->
5408 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5409 (pageno, 5, (
5410 float x0, float y0,
5411 float x1, float y0,
5412 float x1, float y1,
5413 float x0, float y1)
5414 ) :: state.rects
5415 | None -> state.rects
5417 | _ -> state.rects
5419 showrects rects;
5420 showsel ();
5421 state.uioh#display;
5422 begin match state.mstate with
5423 | Mzoomrect ((x0, y0), (x1, y1)) ->
5424 Gl.enable `blend;
5425 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5426 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5427 GlDraw.rect (float x0, float y0)
5428 (float x1, float y1);
5429 Gl.disable `blend;
5430 | _ -> ()
5431 end;
5432 enttext ();
5433 scrollindicator ();
5434 Wsi.swapb ();
5437 let zoomrect x y x1 y1 =
5438 let x0 = min x x1
5439 and x1 = max x x1
5440 and y0 = min y y1 in
5441 gotoy (state.y + y0);
5442 state.anchor <- getanchor ();
5443 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5444 let margin =
5445 if state.w < conf.winw - state.scrollw
5446 then (conf.winw - state.scrollw - state.w) / 2
5447 else 0
5449 state.x <- (state.x + margin) - x0;
5450 setzoom zoom;
5451 Wsi.setcursor Wsi.CURSOR_INHERIT;
5452 state.mstate <- Mnone;
5455 let scrollx x =
5456 let winw = conf.winw - state.scrollw - 1 in
5457 let s = float x /. float winw in
5458 let destx = truncate (float (state.w + winw) *. s) in
5459 state.x <- winw - destx;
5460 gotoy_and_clear_text state.y;
5461 state.mstate <- Mscrollx;
5464 let scrolly y =
5465 let s = float y /. float conf.winh in
5466 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5467 gotoy_and_clear_text desty;
5468 state.mstate <- Mscrolly;
5471 let viewmouse button down x y mask =
5472 match button with
5473 | n when (n == 4 || n == 5) && not down ->
5474 if Wsi.withctrl mask
5475 then (
5476 match state.mstate with
5477 | Mzoom (oldn, i) ->
5478 if oldn = n
5479 then (
5480 if i = 2
5481 then
5482 let incr =
5483 match n with
5484 | 5 ->
5485 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5486 | _ ->
5487 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5489 let zoom = conf.zoom -. incr in
5490 setzoom zoom;
5491 state.mstate <- Mzoom (n, 0);
5492 else
5493 state.mstate <- Mzoom (n, i+1);
5495 else state.mstate <- Mzoom (n, 0)
5497 | _ -> state.mstate <- Mzoom (n, 0)
5499 else (
5500 match state.autoscroll with
5501 | Some step -> setautoscrollspeed step (n=4)
5502 | None ->
5503 let incr =
5504 if n = 4
5505 then -conf.scrollstep
5506 else conf.scrollstep
5508 let incr = incr * 2 in
5509 let y = clamp incr in
5510 gotoy_and_clear_text y
5513 | 1 when Wsi.withctrl mask ->
5514 if down
5515 then (
5516 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5517 state.mstate <- Mpan (x, y)
5519 else
5520 state.mstate <- Mnone
5522 | 3 ->
5523 if down
5524 then (
5525 Wsi.setcursor Wsi.CURSOR_CYCLE;
5526 let p = (x, y) in
5527 state.mstate <- Mzoomrect (p, p)
5529 else (
5530 match state.mstate with
5531 | Mzoomrect ((x0, y0), _) ->
5532 if abs (x-x0) > 10 && abs (y - y0) > 10
5533 then zoomrect x0 y0 x y
5534 else (
5535 state.mstate <- Mnone;
5536 Wsi.setcursor Wsi.CURSOR_INHERIT;
5537 G.postRedisplay "kill accidental zoom rect";
5539 | _ ->
5540 Wsi.setcursor Wsi.CURSOR_INHERIT;
5541 state.mstate <- Mnone
5544 | 1 when x > conf.winw - state.scrollw ->
5545 if down
5546 then
5547 let _, position, sh = state.uioh#scrollph in
5548 if y > truncate position && y < truncate (position +. sh)
5549 then state.mstate <- Mscrolly
5550 else scrolly y
5551 else
5552 state.mstate <- Mnone
5554 | 1 when y > conf.winh - state.hscrollh ->
5555 if down
5556 then
5557 let _, position, sw = state.uioh#scrollpw in
5558 if x > truncate position && x < truncate (position +. sw)
5559 then state.mstate <- Mscrollx
5560 else scrollx x
5561 else
5562 state.mstate <- Mnone
5564 | 1 ->
5565 let dest = if down then getunder x y else Unone in
5566 begin match dest with
5567 | Ulinkgoto _
5568 | Ulinkuri _
5569 | Uremote _
5570 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5571 gotounder dest
5573 | Unone when down ->
5574 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5575 state.mstate <- Mpan (x, y);
5577 | Unone | Utext _ ->
5578 if down
5579 then (
5580 if conf.angle mod 360 = 0
5581 then (
5582 state.mstate <- Msel ((x, y), (x, y));
5583 G.postRedisplay "mouse select";
5586 else (
5587 match state.mstate with
5588 | Mnone -> ()
5590 | Mzoom _ | Mscrollx | Mscrolly ->
5591 state.mstate <- Mnone
5593 | Mzoomrect ((x0, y0), _) ->
5594 zoomrect x0 y0 x y
5596 | Mpan _ ->
5597 Wsi.setcursor Wsi.CURSOR_INHERIT;
5598 state.mstate <- Mnone
5600 | Msel ((_, y0), (_, y1)) ->
5601 let rec loop = function
5602 | [] -> ()
5603 | l :: rest ->
5604 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5605 || ((y1 >= l.pagedispy
5606 && y1 <= (l.pagedispy + l.pagevh)))
5607 then
5608 match getopaque l.pageno with
5609 | Some opaque ->
5610 begin
5611 match Ne.pipe () with
5612 | Ne.Exn exn ->
5613 showtext '!'
5614 (Printf.sprintf
5615 "can not create sel pipe: %s"
5616 (Printexc.to_string exn));
5617 | Ne.Res (r, w) ->
5618 let doclose what fd =
5619 Ne.clo fd (fun msg ->
5620 dolog "%s close failed: %s" what msg)
5623 popen conf.selcmd [r, 0; w, -1];
5624 copysel w opaque;
5625 doclose "pipe/r" r;
5626 G.postRedisplay "copysel";
5627 with exn ->
5628 dolog "can not exectute %S: %s"
5629 conf.selcmd (Printexc.to_string exn);
5630 doclose "pipe/r" r;
5631 doclose "pipe/w" w;
5633 | None -> ()
5634 else loop rest
5636 loop state.layout;
5637 Wsi.setcursor Wsi.CURSOR_INHERIT;
5638 state.mstate <- Mnone;
5642 | _ -> ()
5645 let birdseyemouse button down x y mask
5646 (conf, leftx, _, hooverpageno, anchor) =
5647 match button with
5648 | 1 when down ->
5649 let rec loop = function
5650 | [] -> ()
5651 | l :: rest ->
5652 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5653 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5654 then (
5655 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5657 else loop rest
5659 loop state.layout
5660 | 3 -> ()
5661 | _ -> viewmouse button down x y mask
5664 let mouse button down x y mask =
5665 state.uioh <- state.uioh#button button down x y mask;
5668 let motion ~x ~y =
5669 state.uioh <- state.uioh#motion x y
5672 let pmotion ~x ~y =
5673 state.uioh <- state.uioh#pmotion x y;
5676 let uioh = object
5677 method display = ()
5679 method key key mask =
5680 begin match state.mode with
5681 | Textentry textentry -> textentrykeyboard key mask textentry
5682 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5683 | View -> viewkeyboard key mask
5684 | LinkNav linknav -> linknavkeyboard key mask linknav
5685 end;
5686 state.uioh
5688 method button button bstate x y mask =
5689 begin match state.mode with
5690 | LinkNav _
5691 | View -> viewmouse button bstate x y mask
5692 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5693 | Textentry _ -> ()
5694 end;
5695 state.uioh
5697 method motion x y =
5698 begin match state.mode with
5699 | Textentry _ -> ()
5700 | View | Birdseye _ | LinkNav _ ->
5701 match state.mstate with
5702 | Mzoom _ | Mnone -> ()
5704 | Mpan (x0, y0) ->
5705 let dx = x - x0
5706 and dy = y0 - y in
5707 state.mstate <- Mpan (x, y);
5708 if canpan ()
5709 then state.x <- state.x + dx;
5710 let y = clamp dy in
5711 gotoy_and_clear_text y
5713 | Msel (a, _) ->
5714 state.mstate <- Msel (a, (x, y));
5715 G.postRedisplay "motion select";
5717 | Mscrolly ->
5718 let y = min conf.winh (max 0 y) in
5719 scrolly y
5721 | Mscrollx ->
5722 let x = min conf.winw (max 0 x) in
5723 scrollx x
5725 | Mzoomrect (p0, _) ->
5726 state.mstate <- Mzoomrect (p0, (x, y));
5727 G.postRedisplay "motion zoomrect";
5728 end;
5729 state.uioh
5731 method pmotion x y =
5732 begin match state.mode with
5733 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5734 let rec loop = function
5735 | [] ->
5736 if hooverpageno != -1
5737 then (
5738 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5739 G.postRedisplay "pmotion birdseye no hoover";
5741 | l :: rest ->
5742 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5743 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5744 then (
5745 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5746 G.postRedisplay "pmotion birdseye hoover";
5748 else loop rest
5750 loop state.layout
5752 | Textentry _ -> ()
5754 | LinkNav _
5755 | View ->
5756 match state.mstate with
5757 | Mnone -> updateunder x y
5758 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5760 end;
5761 state.uioh
5763 method infochanged _ = ()
5765 method scrollph =
5766 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5767 let p, h = scrollph state.y maxy in
5768 state.scrollw, p, h
5770 method scrollpw =
5771 let winw = conf.winw - state.scrollw - 1 in
5772 let fwinw = float winw in
5773 let sw =
5774 let sw = fwinw /. float state.w in
5775 let sw = fwinw *. sw in
5776 max sw (float conf.scrollh)
5778 let position, sw =
5779 let f = state.w+winw in
5780 let r = float (winw-state.x) /. float f in
5781 let p = fwinw *. r in
5782 p-.sw/.2., sw
5784 let sw =
5785 if position +. sw > fwinw
5786 then fwinw -. position
5787 else sw
5789 state.hscrollh, position, sw
5791 method modehash =
5792 let modename =
5793 match state.mode with
5794 | LinkNav _ -> "links"
5795 | Textentry _ -> "textentry"
5796 | Birdseye _ -> "birdseye"
5797 | View -> "global"
5799 findkeyhash conf modename
5800 end;;
5802 module Config =
5803 struct
5804 open Parser
5806 let fontpath = ref "";;
5808 module KeyMap =
5809 Map.Make (struct type t = (int * int) let compare = compare end);;
5811 let unent s =
5812 let l = String.length s in
5813 let b = Buffer.create l in
5814 unent b s 0 l;
5815 Buffer.contents b;
5818 let home =
5819 try Sys.getenv "HOME"
5820 with exn ->
5821 prerr_endline
5822 ("Can not determine home directory location: " ^
5823 Printexc.to_string exn);
5827 let modifier_of_string = function
5828 | "alt" -> Wsi.altmask
5829 | "shift" -> Wsi.shiftmask
5830 | "ctrl" | "control" -> Wsi.ctrlmask
5831 | "meta" -> Wsi.metamask
5832 | _ -> 0
5835 let key_of_string =
5836 let r = Str.regexp "-" in
5837 fun s ->
5838 let elems = Str.full_split r s in
5839 let f n k m =
5840 let g s =
5841 let m1 = modifier_of_string s in
5842 if m1 = 0
5843 then (Wsi.namekey s, m)
5844 else (k, m lor m1)
5845 in function
5846 | Str.Delim s when n land 1 = 0 -> g s
5847 | Str.Text s -> g s
5848 | Str.Delim _ -> (k, m)
5850 let rec loop n k m = function
5851 | [] -> (k, m)
5852 | x :: xs ->
5853 let k, m = f n k m x in
5854 loop (n+1) k m xs
5856 loop 0 0 0 elems
5859 let keys_of_string =
5860 let r = Str.regexp "[ \t]" in
5861 fun s ->
5862 let elems = Str.split r s in
5863 List.map key_of_string elems
5866 let copykeyhashes c =
5867 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5870 let config_of c attrs =
5871 let apply c k v =
5873 match k with
5874 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5875 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5876 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5877 | "preload" -> { c with preload = bool_of_string v }
5878 | "page-bias" -> { c with pagebias = int_of_string v }
5879 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5880 | "auto-scroll-step" ->
5881 { c with autoscrollstep = max 0 (int_of_string v) }
5882 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5883 | "crop-hack" -> { c with crophack = bool_of_string v }
5884 | "throttle" ->
5885 let mw =
5886 match String.lowercase v with
5887 | "true" -> Some infinity
5888 | "false" -> None
5889 | f -> Some (float_of_string f)
5891 { c with maxwait = mw}
5892 | "highlight-links" -> { c with hlinks = bool_of_string v }
5893 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5894 | "vertical-margin" ->
5895 { c with interpagespace = max 0 (int_of_string v) }
5896 | "zoom" ->
5897 let zoom = float_of_string v /. 100. in
5898 let zoom = max zoom 0.0 in
5899 { c with zoom = zoom }
5900 | "presentation" -> { c with presentation = bool_of_string v }
5901 | "rotation-angle" -> { c with angle = int_of_string v }
5902 | "width" -> { c with winw = max 20 (int_of_string v) }
5903 | "height" -> { c with winh = max 20 (int_of_string v) }
5904 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5905 | "proportional-display" -> { c with proportional = bool_of_string v }
5906 | "pixmap-cache-size" ->
5907 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5908 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5909 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5910 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5911 | "persistent-location" -> { c with jumpback = bool_of_string v }
5912 | "background-color" -> { c with bgcolor = color_of_string v }
5913 | "scrollbar-in-presentation" ->
5914 { c with scrollbarinpm = bool_of_string v }
5915 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5916 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5917 | "mupdf-store-size" ->
5918 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5919 | "checkers" -> { c with checkers = bool_of_string v }
5920 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5921 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5922 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5923 | "uri-launcher" -> { c with urilauncher = unent v }
5924 | "path-launcher" -> { c with pathlauncher = unent v }
5925 | "color-space" -> { c with colorspace = colorspace_of_string v }
5926 | "invert-colors" -> { c with invert = bool_of_string v }
5927 | "brightness" -> { c with colorscale = float_of_string v }
5928 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5929 | "ghyllscroll" ->
5930 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5931 | "columns" ->
5932 let (n, _, _) as nab = multicolumns_of_string v in
5933 if n < 0
5934 then { c with columns = Csplit (-n, [||]) }
5935 else { c with columns = Cmulti (nab, [||]) }
5936 | "birds-eye-columns" ->
5937 { c with beyecolumns = Some (max (int_of_string v) 2) }
5938 | "selection-command" -> { c with selcmd = unent v }
5939 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5940 | _ -> c
5941 with exn ->
5942 prerr_endline ("Error processing attribute (`" ^
5943 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5946 let rec fold c = function
5947 | [] -> c
5948 | (k, v) :: rest ->
5949 let c = apply c k v in
5950 fold c rest
5952 fold { c with keyhashes = copykeyhashes c } attrs;
5955 let fromstring f pos n v d =
5956 try f v
5957 with exn ->
5958 dolog "Error processing attribute (%S=%S) at %d\n%s"
5959 n v pos (Printexc.to_string exn)
5964 let bookmark_of attrs =
5965 let rec fold title page rely = function
5966 | ("title", v) :: rest -> fold v page rely rest
5967 | ("page", v) :: rest -> fold title v rely rest
5968 | ("rely", v) :: rest -> fold title page v rest
5969 | _ :: rest -> fold title page rely rest
5970 | [] -> title, page, rely
5972 fold "invalid" "0" "0" attrs
5975 let doc_of attrs =
5976 let rec fold path page rely pan = function
5977 | ("path", v) :: rest -> fold v page rely pan rest
5978 | ("page", v) :: rest -> fold path v rely pan rest
5979 | ("rely", v) :: rest -> fold path page v pan rest
5980 | ("pan", v) :: rest -> fold path page rely v rest
5981 | _ :: rest -> fold path page rely pan rest
5982 | [] -> path, page, rely, pan
5984 fold "" "0" "0" "0" attrs
5987 let map_of attrs =
5988 let rec fold rs ls = function
5989 | ("out", v) :: rest -> fold v ls rest
5990 | ("in", v) :: rest -> fold rs v rest
5991 | _ :: rest -> fold ls rs rest
5992 | [] -> ls, rs
5994 fold "" "" attrs
5997 let setconf dst src =
5998 dst.scrollbw <- src.scrollbw;
5999 dst.scrollh <- src.scrollh;
6000 dst.icase <- src.icase;
6001 dst.preload <- src.preload;
6002 dst.pagebias <- src.pagebias;
6003 dst.verbose <- src.verbose;
6004 dst.scrollstep <- src.scrollstep;
6005 dst.maxhfit <- src.maxhfit;
6006 dst.crophack <- src.crophack;
6007 dst.autoscrollstep <- src.autoscrollstep;
6008 dst.maxwait <- src.maxwait;
6009 dst.hlinks <- src.hlinks;
6010 dst.underinfo <- src.underinfo;
6011 dst.interpagespace <- src.interpagespace;
6012 dst.zoom <- src.zoom;
6013 dst.presentation <- src.presentation;
6014 dst.angle <- src.angle;
6015 dst.winw <- src.winw;
6016 dst.winh <- src.winh;
6017 dst.savebmarks <- src.savebmarks;
6018 dst.memlimit <- src.memlimit;
6019 dst.proportional <- src.proportional;
6020 dst.texcount <- src.texcount;
6021 dst.sliceheight <- src.sliceheight;
6022 dst.thumbw <- src.thumbw;
6023 dst.jumpback <- src.jumpback;
6024 dst.bgcolor <- src.bgcolor;
6025 dst.scrollbarinpm <- src.scrollbarinpm;
6026 dst.tilew <- src.tilew;
6027 dst.tileh <- src.tileh;
6028 dst.mustoresize <- src.mustoresize;
6029 dst.checkers <- src.checkers;
6030 dst.aalevel <- src.aalevel;
6031 dst.trimmargins <- src.trimmargins;
6032 dst.trimfuzz <- src.trimfuzz;
6033 dst.urilauncher <- src.urilauncher;
6034 dst.colorspace <- src.colorspace;
6035 dst.invert <- src.invert;
6036 dst.colorscale <- src.colorscale;
6037 dst.redirectstderr <- src.redirectstderr;
6038 dst.ghyllscroll <- src.ghyllscroll;
6039 dst.columns <- src.columns;
6040 dst.beyecolumns <- src.beyecolumns;
6041 dst.selcmd <- src.selcmd;
6042 dst.updatecurs <- src.updatecurs;
6043 dst.pathlauncher <- src.pathlauncher;
6044 dst.keyhashes <- copykeyhashes src;
6047 let get s =
6048 let h = Hashtbl.create 10 in
6049 let dc = { defconf with angle = defconf.angle } in
6050 let rec toplevel v t spos _ =
6051 match t with
6052 | Vdata | Vcdata | Vend -> v
6053 | Vopen ("llppconfig", _, closed) ->
6054 if closed
6055 then v
6056 else { v with f = llppconfig }
6057 | Vopen _ ->
6058 error "unexpected subelement at top level" s spos
6059 | Vclose _ -> error "unexpected close at top level" s spos
6061 and llppconfig v t spos _ =
6062 match t with
6063 | Vdata | Vcdata -> v
6064 | Vend -> error "unexpected end of input in llppconfig" s spos
6065 | Vopen ("defaults", attrs, closed) ->
6066 let c = config_of dc attrs in
6067 setconf dc c;
6068 if closed
6069 then v
6070 else { v with f = defaults }
6072 | Vopen ("ui-font", attrs, closed) ->
6073 let rec getsize size = function
6074 | [] -> size
6075 | ("size", v) :: rest ->
6076 let size =
6077 fromstring int_of_string spos "size" v fstate.fontsize in
6078 getsize size rest
6079 | l -> getsize size l
6081 fstate.fontsize <- getsize fstate.fontsize attrs;
6082 if closed
6083 then v
6084 else { v with f = uifont (Buffer.create 10) }
6086 | Vopen ("doc", attrs, closed) ->
6087 let pathent, spage, srely, span = doc_of attrs in
6088 let path = unent pathent
6089 and pageno = fromstring int_of_string spos "page" spage 0
6090 and rely = fromstring float_of_string spos "rely" srely 0.0
6091 and pan = fromstring int_of_string spos "pan" span 0 in
6092 let c = config_of dc attrs in
6093 let anchor = (pageno, rely) in
6094 if closed
6095 then (Hashtbl.add h path (c, [], pan, anchor); v)
6096 else { v with f = doc path pan anchor c [] }
6098 | Vopen _ ->
6099 error "unexpected subelement in llppconfig" s spos
6101 | Vclose "llppconfig" -> { v with f = toplevel }
6102 | Vclose _ -> error "unexpected close in llppconfig" s spos
6104 and defaults v t spos _ =
6105 match t with
6106 | Vdata | Vcdata -> v
6107 | Vend -> error "unexpected end of input in defaults" s spos
6108 | Vopen ("keymap", attrs, closed) ->
6109 let modename =
6110 try List.assoc "mode" attrs
6111 with Not_found -> "global" in
6112 if closed
6113 then v
6114 else
6115 let ret keymap =
6116 let h = findkeyhash dc modename in
6117 KeyMap.iter (Hashtbl.replace h) keymap;
6118 defaults
6120 { v with f = pkeymap ret KeyMap.empty }
6122 | Vopen (_, _, _) ->
6123 error "unexpected subelement in defaults" s spos
6125 | Vclose "defaults" ->
6126 { v with f = llppconfig }
6128 | Vclose _ -> error "unexpected close in defaults" s spos
6130 and uifont b v t spos epos =
6131 match t with
6132 | Vdata | Vcdata ->
6133 Buffer.add_substring b s spos (epos - spos);
6135 | Vopen (_, _, _) ->
6136 error "unexpected subelement in ui-font" s spos
6137 | Vclose "ui-font" ->
6138 if String.length !fontpath = 0
6139 then fontpath := Buffer.contents b;
6140 { v with f = llppconfig }
6141 | Vclose _ -> error "unexpected close in ui-font" s spos
6142 | Vend -> error "unexpected end of input in ui-font" s spos
6144 and doc path pan anchor c bookmarks v t spos _ =
6145 match t with
6146 | Vdata | Vcdata -> v
6147 | Vend -> error "unexpected end of input in doc" s spos
6148 | Vopen ("bookmarks", _, closed) ->
6149 if closed
6150 then v
6151 else { v with f = pbookmarks path pan anchor c bookmarks }
6153 | Vopen ("keymap", attrs, closed) ->
6154 let modename =
6155 try List.assoc "mode" attrs
6156 with Not_found -> "global"
6158 if closed
6159 then v
6160 else
6161 let ret keymap =
6162 let h = findkeyhash c modename in
6163 KeyMap.iter (Hashtbl.replace h) keymap;
6164 doc path pan anchor c bookmarks
6166 { v with f = pkeymap ret KeyMap.empty }
6168 | Vopen (_, _, _) ->
6169 error "unexpected subelement in doc" s spos
6171 | Vclose "doc" ->
6172 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6173 { v with f = llppconfig }
6175 | Vclose _ -> error "unexpected close in doc" s spos
6177 and pkeymap ret keymap v t spos _ =
6178 match t with
6179 | Vdata | Vcdata -> v
6180 | Vend -> error "unexpected end of input in keymap" s spos
6181 | Vopen ("map", attrs, closed) ->
6182 let r, l = map_of attrs in
6183 let kss = fromstring keys_of_string spos "in" r [] in
6184 let lss = fromstring keys_of_string spos "out" l [] in
6185 let keymap =
6186 match kss with
6187 | [] -> keymap
6188 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6189 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6191 if closed
6192 then { v with f = pkeymap ret keymap }
6193 else
6194 let f () = v in
6195 { v with f = skip "map" f }
6197 | Vopen _ ->
6198 error "unexpected subelement in keymap" s spos
6200 | Vclose "keymap" ->
6201 { v with f = ret keymap }
6203 | Vclose _ -> error "unexpected close in keymap" s spos
6205 and pbookmarks path pan anchor c bookmarks v t spos _ =
6206 match t with
6207 | Vdata | Vcdata -> v
6208 | Vend -> error "unexpected end of input in bookmarks" s spos
6209 | Vopen ("item", attrs, closed) ->
6210 let titleent, spage, srely = bookmark_of attrs in
6211 let page = fromstring int_of_string spos "page" spage 0
6212 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6213 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6214 if closed
6215 then { v with f = pbookmarks path pan anchor c bookmarks }
6216 else
6217 let f () = v in
6218 { v with f = skip "item" f }
6220 | Vopen _ ->
6221 error "unexpected subelement in bookmarks" s spos
6223 | Vclose "bookmarks" ->
6224 { v with f = doc path pan anchor c bookmarks }
6226 | Vclose _ -> error "unexpected close in bookmarks" s spos
6228 and skip tag f v t spos _ =
6229 match t with
6230 | Vdata | Vcdata -> v
6231 | Vend ->
6232 error ("unexpected end of input in skipped " ^ tag) s spos
6233 | Vopen (tag', _, closed) ->
6234 if closed
6235 then v
6236 else
6237 let f' () = { v with f = skip tag f } in
6238 { v with f = skip tag' f' }
6239 | Vclose ctag ->
6240 if tag = ctag
6241 then f ()
6242 else error ("unexpected close in skipped " ^ tag) s spos
6245 parse { f = toplevel; accu = () } s;
6246 h, dc;
6249 let do_load f ic =
6251 let len = in_channel_length ic in
6252 let s = String.create len in
6253 really_input ic s 0 len;
6254 f s;
6255 with
6256 | Parse_error (msg, s, pos) ->
6257 let subs = subs s pos in
6258 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6259 failwith ("parse error: " ^ s)
6261 | exn ->
6262 failwith ("config load error: " ^ Printexc.to_string exn)
6265 let defconfpath =
6266 let dir =
6268 let dir = Filename.concat home ".config" in
6269 if Sys.is_directory dir then dir else home
6270 with _ -> home
6272 Filename.concat dir "llpp.conf"
6275 let confpath = ref defconfpath;;
6277 let load1 f =
6278 if Sys.file_exists !confpath
6279 then
6280 match
6281 (try Some (open_in_bin !confpath)
6282 with exn ->
6283 prerr_endline
6284 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6285 Printexc.to_string exn);
6286 None
6288 with
6289 | Some ic ->
6290 begin try
6291 f (do_load get ic)
6292 with exn ->
6293 prerr_endline
6294 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6295 Printexc.to_string exn);
6296 end;
6297 close_in ic;
6299 | None -> ()
6300 else
6301 f (Hashtbl.create 0, defconf)
6304 let load () =
6305 let f (h, dc) =
6306 let pc, pb, px, pa =
6308 Hashtbl.find h (Filename.basename state.path)
6309 with Not_found -> dc, [], 0, (0, 0.0)
6311 setconf defconf dc;
6312 setconf conf pc;
6313 state.bookmarks <- pb;
6314 state.x <- px;
6315 state.scrollw <- conf.scrollbw;
6316 if conf.jumpback
6317 then state.anchor <- pa;
6318 cbput state.hists.nav pa;
6320 load1 f
6323 let add_attrs bb always dc c =
6324 let ob s a b =
6325 if always || a != b
6326 then Printf.bprintf bb "\n %s='%b'" s a
6327 and oi s a b =
6328 if always || a != b
6329 then Printf.bprintf bb "\n %s='%d'" s a
6330 and oI s a b =
6331 if always || a != b
6332 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6333 and oz s a b =
6334 if always || a <> b
6335 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6336 and oF s a b =
6337 if always || a <> b
6338 then Printf.bprintf bb "\n %s='%f'" s a
6339 and oc s a b =
6340 if always || a <> b
6341 then
6342 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6343 and oC s a b =
6344 if always || a <> b
6345 then
6346 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6347 and oR s a b =
6348 if always || a <> b
6349 then
6350 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6351 and os s a b =
6352 if always || a <> b
6353 then
6354 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6355 and og s a b =
6356 if always || a <> b
6357 then
6358 match a with
6359 | None -> ()
6360 | Some (_N, _A, _B) ->
6361 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6362 and oW s a b =
6363 if always || a <> b
6364 then
6365 let v =
6366 match a with
6367 | None -> "false"
6368 | Some f ->
6369 if f = infinity
6370 then "true"
6371 else string_of_float f
6373 Printf.bprintf bb "\n %s='%s'" s v
6374 and oco s a b =
6375 if always || a <> b
6376 then
6377 match a with
6378 | Cmulti ((n, a, b), _) when n > 1 ->
6379 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6380 | Csplit (n, _) when n > 1 ->
6381 Printf.bprintf bb "\n %s='%d'" s ~-n
6382 | _ -> ()
6383 and obeco s a b =
6384 if always || a <> b
6385 then
6386 match a with
6387 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6388 | _ -> ()
6390 let w, h =
6391 if always
6392 then dc.winw, dc.winh
6393 else
6394 match state.fullscreen with
6395 | Some wh -> wh
6396 | None -> c.winw, c.winh
6398 let zoom, presentation, interpagespace, maxwait =
6399 if always
6400 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6401 else
6402 match state.mode with
6403 | Birdseye (bc, _, _, _, _) ->
6404 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6405 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6407 oi "width" w dc.winw;
6408 oi "height" h dc.winh;
6409 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6410 oi "scroll-handle-height" c.scrollh dc.scrollh;
6411 ob "case-insensitive-search" c.icase dc.icase;
6412 ob "preload" c.preload dc.preload;
6413 oi "page-bias" c.pagebias dc.pagebias;
6414 oi "scroll-step" c.scrollstep dc.scrollstep;
6415 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6416 ob "max-height-fit" c.maxhfit dc.maxhfit;
6417 ob "crop-hack" c.crophack dc.crophack;
6418 oW "throttle" maxwait dc.maxwait;
6419 ob "highlight-links" c.hlinks dc.hlinks;
6420 ob "under-cursor-info" c.underinfo dc.underinfo;
6421 oi "vertical-margin" interpagespace dc.interpagespace;
6422 oz "zoom" zoom dc.zoom;
6423 ob "presentation" presentation dc.presentation;
6424 oi "rotation-angle" c.angle dc.angle;
6425 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6426 ob "proportional-display" c.proportional dc.proportional;
6427 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6428 oi "tex-count" c.texcount dc.texcount;
6429 oi "slice-height" c.sliceheight dc.sliceheight;
6430 oi "thumbnail-width" c.thumbw dc.thumbw;
6431 ob "persistent-location" c.jumpback dc.jumpback;
6432 oc "background-color" c.bgcolor dc.bgcolor;
6433 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6434 oi "tile-width" c.tilew dc.tilew;
6435 oi "tile-height" c.tileh dc.tileh;
6436 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6437 ob "checkers" c.checkers dc.checkers;
6438 oi "aalevel" c.aalevel dc.aalevel;
6439 ob "trim-margins" c.trimmargins dc.trimmargins;
6440 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6441 os "uri-launcher" c.urilauncher dc.urilauncher;
6442 os "path-launcher" c.pathlauncher dc.pathlauncher;
6443 oC "color-space" c.colorspace dc.colorspace;
6444 ob "invert-colors" c.invert dc.invert;
6445 oF "brightness" c.colorscale dc.colorscale;
6446 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6447 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6448 oco "columns" c.columns dc.columns;
6449 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6450 os "selection-command" c.selcmd dc.selcmd;
6451 ob "update-cursor" c.updatecurs dc.updatecurs;
6454 let keymapsbuf always dc c =
6455 let bb = Buffer.create 16 in
6456 let rec loop = function
6457 | [] -> ()
6458 | (modename, h) :: rest ->
6459 let dh = findkeyhash dc modename in
6460 if always || h <> dh
6461 then (
6462 if Hashtbl.length h > 0
6463 then (
6464 if Buffer.length bb > 0
6465 then Buffer.add_char bb '\n';
6466 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6467 Hashtbl.iter (fun i o ->
6468 let isdifferent = always ||
6470 let dO = Hashtbl.find dh i in
6471 dO <> o
6472 with Not_found -> true
6474 if isdifferent
6475 then
6476 let addkm (k, m) =
6477 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6478 if Wsi.withalt m then Buffer.add_string bb "alt-";
6479 if Wsi.withshift m then Buffer.add_string bb "shift-";
6480 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6481 Buffer.add_string bb (Wsi.keyname k);
6483 let addkms l =
6484 let rec loop = function
6485 | [] -> ()
6486 | km :: [] -> addkm km
6487 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6489 loop l
6491 Buffer.add_string bb "<map in='";
6492 addkm i;
6493 match o with
6494 | KMinsrt km ->
6495 Buffer.add_string bb "' out='";
6496 addkm km;
6497 Buffer.add_string bb "'/>\n"
6499 | KMinsrl kms ->
6500 Buffer.add_string bb "' out='";
6501 addkms kms;
6502 Buffer.add_string bb "'/>\n"
6504 | KMmulti (ins, kms) ->
6505 Buffer.add_char bb ' ';
6506 addkms ins;
6507 Buffer.add_string bb "' out='";
6508 addkms kms;
6509 Buffer.add_string bb "'/>\n"
6510 ) h;
6511 Buffer.add_string bb "</keymap>";
6514 loop rest
6516 loop c.keyhashes;
6520 let save () =
6521 let uifontsize = fstate.fontsize in
6522 let bb = Buffer.create 32768 in
6523 let f (h, dc) =
6524 let dc = if conf.bedefault then conf else dc in
6525 Buffer.add_string bb "<llppconfig>\n";
6527 if String.length !fontpath > 0
6528 then
6529 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6530 uifontsize
6531 !fontpath
6532 else (
6533 if uifontsize <> 14
6534 then
6535 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6538 Buffer.add_string bb "<defaults ";
6539 add_attrs bb true dc dc;
6540 let kb = keymapsbuf true dc dc in
6541 if Buffer.length kb > 0
6542 then (
6543 Buffer.add_string bb ">\n";
6544 Buffer.add_buffer bb kb;
6545 Buffer.add_string bb "\n</defaults>\n";
6547 else Buffer.add_string bb "/>\n";
6549 let adddoc path pan anchor c bookmarks =
6550 if bookmarks == [] && c = dc && anchor = emptyanchor
6551 then ()
6552 else (
6553 Printf.bprintf bb "<doc path='%s'"
6554 (enent path 0 (String.length path));
6556 if anchor <> emptyanchor
6557 then (
6558 let n, y = anchor in
6559 Printf.bprintf bb " page='%d'" n;
6560 if y > 1e-6
6561 then
6562 Printf.bprintf bb " rely='%f'" y
6566 if pan != 0
6567 then Printf.bprintf bb " pan='%d'" pan;
6569 add_attrs bb false dc c;
6570 let kb = keymapsbuf false dc c in
6572 begin match bookmarks with
6573 | [] ->
6574 if Buffer.length kb > 0
6575 then (
6576 Buffer.add_string bb ">\n";
6577 Buffer.add_buffer bb kb;
6578 Buffer.add_string bb "</doc>\n";
6580 else Buffer.add_string bb "/>\n"
6581 | _ ->
6582 Buffer.add_string bb ">\n<bookmarks>\n";
6583 List.iter (fun (title, _level, (page, rely)) ->
6584 Printf.bprintf bb
6585 "<item title='%s' page='%d'"
6586 (enent title 0 (String.length title))
6587 page
6589 if rely > 1e-6
6590 then
6591 Printf.bprintf bb " rely='%f'" rely
6593 Buffer.add_string bb "/>\n";
6594 ) bookmarks;
6595 Buffer.add_string bb "</bookmarks>";
6596 if Buffer.length kb > 0
6597 then (
6598 Buffer.add_string bb "\n";
6599 Buffer.add_buffer bb kb;
6601 Buffer.add_string bb "\n</doc>\n";
6602 end;
6606 let pan, conf =
6607 match state.mode with
6608 | Birdseye (c, pan, _, _, _) ->
6609 let beyecolumns =
6610 match conf.columns with
6611 | Cmulti ((c, _, _), _) -> Some c
6612 | Csingle -> None
6613 | Csplit _ -> None
6614 and columns =
6615 match c.columns with
6616 | Cmulti (c, _) -> Cmulti (c, [||])
6617 | Csingle -> Csingle
6618 | Csplit _ -> failwith "quit from bird's eye while split"
6620 pan, { c with beyecolumns = beyecolumns; columns = columns }
6621 | _ -> state.x, conf
6623 let basename = Filename.basename state.path in
6624 adddoc basename pan (getanchor ())
6625 { conf with
6626 autoscrollstep =
6627 match state.autoscroll with
6628 | Some step -> step
6629 | None -> conf.autoscrollstep }
6630 (if conf.savebmarks then state.bookmarks else []);
6632 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6633 if basename <> path
6634 then adddoc path x y c bookmarks
6635 ) h;
6636 Buffer.add_string bb "</llppconfig>";
6638 load1 f;
6639 if Buffer.length bb > 0
6640 then
6642 let tmp = !confpath ^ ".tmp" in
6643 let oc = open_out_bin tmp in
6644 Buffer.output_buffer oc bb;
6645 close_out oc;
6646 Unix.rename tmp !confpath;
6647 with exn ->
6648 prerr_endline
6649 ("error while saving configuration: " ^ Printexc.to_string exn)
6651 end;;
6653 let () =
6654 Arg.parse
6655 (Arg.align
6656 [("-p", Arg.String (fun s -> state.password <- s) ,
6657 "<password> Set password");
6659 ("-f", Arg.String (fun s -> Config.fontpath := s),
6660 "<path> Set path to the user interface font");
6662 ("-c", Arg.String (fun s -> Config.confpath := s),
6663 "<path> Set path to the configuration file");
6665 ("-v", Arg.Unit (fun () ->
6666 Printf.printf
6667 "%s\nconfiguration path: %s\n"
6668 (version ())
6669 Config.defconfpath
6671 exit 0), " Print version and exit");
6674 (fun s -> state.path <- s)
6675 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6677 if String.length state.path = 0
6678 then (prerr_endline "file name missing"; exit 1);
6680 Config.load ();
6682 let globalkeyhash = findkeyhash conf "global" in
6683 let wsfd, winw, winh = Wsi.init (object
6684 method expose =
6685 if nogeomcmds state.geomcmds || platform == Posx
6686 then display ()
6687 else (
6688 GlFunc.draw_buffer `front;
6689 GlClear.color (scalecolor2 conf.bgcolor);
6690 GlClear.clear [`color];
6691 GlFunc.draw_buffer `back;
6693 method display = display ()
6694 method reshape w h = reshape w h
6695 method mouse b d x y m = mouse b d x y m
6696 method motion x y = state.mpos <- (x, y); motion x y
6697 method pmotion x y = state.mpos <- (x, y); pmotion x y
6698 method key k m =
6699 let mascm = m land (
6700 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6701 ) in
6702 match state.keystate with
6703 | KSnone ->
6704 let km = k, mascm in
6705 begin
6706 match
6707 try Hashtbl.find globalkeyhash km
6708 with Not_found ->
6709 let modehash = state.uioh#modehash in
6710 try Hashtbl.find modehash km
6711 with Not_found -> KMinsrt (k, m)
6712 with
6713 | KMinsrt (k, m) -> keyboard k m
6714 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6715 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6717 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6718 List.iter (fun (k, m) -> keyboard k m) insrt;
6719 state.keystate <- KSnone
6720 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6721 state.keystate <- KSinto (keys, insrt)
6722 | _ ->
6723 state.keystate <- KSnone
6725 method enter x y = state.mpos <- (x, y); pmotion x y
6726 method leave = state.mpos <- (-1, -1)
6727 method quit = raise Quit
6728 end) conf.winw conf.winh (platform = Posx) in
6730 state.wsfd <- wsfd;
6732 if not (
6733 List.exists GlMisc.check_extension
6734 [ "GL_ARB_texture_rectangle"
6735 ; "GL_EXT_texture_recangle"
6736 ; "GL_NV_texture_rectangle" ]
6738 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6740 let cr, sw =
6741 match Ne.pipe () with
6742 | Ne.Exn exn ->
6743 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6744 exit 1
6745 | Ne.Res rw -> rw
6746 and sr, cw =
6747 match Ne.pipe () with
6748 | Ne.Exn exn ->
6749 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6750 exit 1
6751 | Ne.Res rw -> rw
6754 cloexec cr;
6755 cloexec sw;
6756 cloexec sr;
6757 cloexec cw;
6759 setcheckers conf.checkers;
6760 redirectstderr ();
6762 init (cr, cw) (
6763 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6764 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6765 !Config.fontpath
6767 state.sr <- sr;
6768 state.sw <- sw;
6769 state.text <- "Opening " ^ state.path;
6770 reshape winw winh;
6771 opendoc state.path state.password;
6772 state.uioh <- uioh;
6774 let rec loop deadline =
6775 let r =
6776 match state.errfd with
6777 | None -> [state.sr; state.wsfd]
6778 | Some fd -> [state.sr; state.wsfd; fd]
6780 if state.redisplay
6781 then (
6782 state.redisplay <- false;
6783 display ();
6785 let timeout =
6786 let now = now () in
6787 if deadline > now
6788 then (
6789 if deadline = infinity
6790 then ~-.1.0
6791 else max 0.0 (deadline -. now)
6793 else 0.0
6795 let r, _, _ =
6796 try Unix.select r [] [] timeout
6797 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6799 begin match r with
6800 | [] ->
6801 state.ghyll None;
6802 let newdeadline =
6803 if state.ghyll == noghyll
6804 then
6805 match state.autoscroll with
6806 | Some step when step != 0 ->
6807 let y = state.y + step in
6808 let y =
6809 if y < 0
6810 then state.maxy
6811 else if y >= state.maxy then 0 else y
6813 gotoy y;
6814 if state.mode = View
6815 then state.text <- "";
6816 deadline +. 0.01
6817 | _ -> infinity
6818 else deadline +. 0.01
6820 loop newdeadline
6822 | l ->
6823 let rec checkfds = function
6824 | [] -> ()
6825 | fd :: rest when fd = state.sr ->
6826 let cmd = readcmd state.sr in
6827 act cmd;
6828 checkfds rest
6830 | fd :: rest when fd = state.wsfd ->
6831 Wsi.readresp fd;
6832 checkfds rest
6834 | fd :: rest ->
6835 let s = String.create 80 in
6836 let n = Unix.read fd s 0 80 in
6837 if conf.redirectstderr
6838 then (
6839 Buffer.add_substring state.errmsgs s 0 n;
6840 state.newerrmsgs <- true;
6841 state.redisplay <- true;
6843 else (
6844 prerr_string (String.sub s 0 n);
6845 flush stderr;
6847 checkfds rest
6849 checkfds l;
6850 let newdeadline =
6851 let deadline1 =
6852 if deadline = infinity
6853 then now () +. 0.01
6854 else deadline
6856 match state.autoscroll with
6857 | Some step when step != 0 -> deadline1
6858 | _ -> if state.ghyll == noghyll then infinity else deadline1
6860 loop newdeadline
6861 end;
6864 loop infinity;
6865 with Quit ->
6866 Config.save ();
6867 exit 0;