Allow scrolling by page via mouse wheel
[llpp.git] / main.ml
blob7b5e075c42b68dcc11c12074b4778012380cb51f
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath * trimcachepath
20 * haspbo)
21 and pageno = int
22 and width = int
23 and height = int
24 and leftx = int
25 and opaque = string
26 and recttype = int
27 and pixmapsize = int
28 and angle = int
29 and proportional = bool
30 and trimmargins = bool
31 and interpagespace = int
32 and texcount = int
33 and sliceheight = int
34 and gen = int
35 and top = float
36 and dtop = float
37 and fontpath = string
38 and trimcachepath = string
39 and memsize = int
40 and aalevel = int
41 and irect = (int * int * int * int)
42 and trimparams = (trimmargins * irect)
43 and colorspace = | Rgb | Bgr | Gray
44 and haspbo = bool
47 type link =
48 | Lnotfound
49 | Lfound of int
50 and linkdir =
51 | LDfirst
52 | LDlast
53 | LDfirstvisible of (int * int * int)
54 | LDleft of int
55 | LDright of int
56 | LDdown of int
57 | LDup of int
60 type pagewithlinks =
61 | Pwlnotfound
62 | Pwl of int
65 type keymap =
66 | KMinsrt of key
67 | KMinsrl of key list
68 | KMmulti of key list * key list
69 and key = int * int
70 and keyhash = (key, keymap) Hashtbl.t
71 and keystate =
72 | KSnone
73 | KSinto of (key list * key list)
76 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
77 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
79 type pipe = (Unix.file_descr * Unix.file_descr);;
81 external init : pipe -> params -> unit = "ml_init";;
82 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
83 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
84 external getpdimrect : int -> float array = "ml_getpdimrect";;
85 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
86 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
87 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
88 external measurestr : int -> string -> float = "ml_measure_string";;
89 external getmaxw : unit -> float = "ml_getmaxw";;
90 external postprocess :
91 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
92 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
93 external platform : unit -> platform = "ml_platform";;
94 external setaalevel : int -> unit = "ml_setaalevel";;
95 external realloctexts : int -> bool = "ml_realloctexts";;
96 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
97 external findlink : opaque -> linkdir -> link = "ml_findlink";;
98 external getlink : opaque -> int -> under = "ml_getlink";;
99 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
100 external getlinkcount : opaque -> int = "ml_getlinkcount";;
101 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
102 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
103 external mbtoutf8 : string -> string = "ml_mbtoutf8";;
104 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
105 external freepbo : string -> unit = "ml_freepbo";;
106 external unmappbo : string -> unit = "ml_unmappbo";;
107 external pbousable : unit -> bool = "ml_pbo_usable";;
109 let platform_to_string = function
110 | Punknown -> "unknown"
111 | Plinux -> "Linux"
112 | Posx -> "OSX"
113 | Psun -> "Sun"
114 | Pfreebsd -> "FreeBSD"
115 | Pdragonflybsd -> "DragonflyBSD"
116 | Popenbsd -> "OpenBSD"
117 | Pnetbsd -> "NetBSD"
118 | Pcygwin -> "Cygwin"
121 let platform = platform ();;
123 let popen cmd fda =
124 if platform = Pcygwin
125 then (
126 let sh = "/bin/sh" in
127 let args = [|sh; "-c"; cmd|] in
128 let rec std si so se = function
129 | [] -> si, so, se
130 | (fd, 0) :: rest -> std fd so se rest
131 | (fd, -1) :: rest ->
132 Unix.set_close_on_exec fd;
133 std si so se rest
134 | (_, n) :: _ ->
135 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
137 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
138 ignore (Unix.create_process sh args si so se)
140 else popen cmd fda;
143 type x = int
144 and y = int
145 and tilex = int
146 and tiley = int
147 and tileparams = (x * y * width * height * tilex * tiley)
150 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
152 type mpos = int * int
153 and mstate =
154 | Msel of (mpos * mpos)
155 | Mpan of mpos
156 | Mscrolly | Mscrollx
157 | Mzoom of (int * int)
158 | Mzoomrect of (mpos * mpos)
159 | Mnone
162 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
163 and onkey = string -> int -> te
164 and ondone = string -> unit
165 and histcancel = unit -> unit
166 and onhist = ((histcmd -> string) * histcancel)
167 and histcmd = HCnext | HCprev | HCfirst | HClast
168 and cancelonempty = bool
169 and te =
170 | TEstop
171 | TEdone of string
172 | TEcont of string
173 | TEswitch of textentry
176 type 'a circbuf =
177 { store : 'a array
178 ; mutable rc : int
179 ; mutable wc : int
180 ; mutable len : int
184 let bound v minv maxv =
185 max minv (min maxv v);
188 let cbnew n v =
189 { store = Array.create n v
190 ; rc = 0
191 ; wc = 0
192 ; len = 0
196 let cbcap b = Array.length b.store;;
198 let cbput b v =
199 let cap = cbcap b in
200 b.store.(b.wc) <- v;
201 b.wc <- (b.wc + 1) mod cap;
202 b.rc <- b.wc;
203 b.len <- min (b.len + 1) cap;
206 let cbempty b = b.len = 0;;
208 let cbgetg b circular dir =
209 if cbempty b
210 then b.store.(0)
211 else
212 let rc = b.rc + dir in
213 let rc =
214 if circular
215 then (
216 if rc = -1
217 then b.len-1
218 else (
219 if rc >= b.len
220 then 0
221 else rc
224 else bound rc 0 (b.len-1)
226 b.rc <- rc;
227 b.store.(rc);
230 let cbget b = cbgetg b false;;
231 let cbgetc b = cbgetg b true;;
233 let drawstring size x y s =
234 Gl.enable `blend;
235 Gl.enable `texture_2d;
236 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
237 ignore (drawstr size x y s);
238 Gl.disable `blend;
239 Gl.disable `texture_2d;
242 let drawstring1 size x y s =
243 drawstr size x y s;
246 let drawstring2 size x y fmt =
247 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
250 type page =
251 { pageno : int
252 ; pagedimno : int
253 ; pagew : int
254 ; pageh : int
255 ; pagex : int
256 ; pagey : int
257 ; pagevw : int
258 ; pagevh : int
259 ; pagedispx : int
260 ; pagedispy : int
261 ; pagecol : int
265 let debugl l =
266 dolog "l %d dim=%d {" l.pageno l.pagedimno;
267 dolog " WxH %dx%d" l.pagew l.pageh;
268 dolog " vWxH %dx%d" l.pagevw l.pagevh;
269 dolog " pagex,y %d,%d" l.pagex l.pagey;
270 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
271 dolog " column %d" l.pagecol;
272 dolog "}";
275 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
276 dolog "rect {";
277 dolog " x0,y0=(% f, % f)" x0 y0;
278 dolog " x1,y1=(% f, % f)" x1 y1;
279 dolog " x2,y2=(% f, % f)" x2 y2;
280 dolog " x3,y3=(% f, % f)" x3 y3;
281 dolog "}";
284 type multicolumns = multicol * pagegeom
285 and singlecolumn = pagegeom
286 and splitcolumns = columncount * pagegeom
287 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
288 and multicol = columncount * covercount * covercount
289 and pdimno = int
290 and columncount = int
291 and covercount = int;;
293 type conf =
294 { mutable scrollbw : int
295 ; mutable scrollh : int
296 ; mutable icase : bool
297 ; mutable preload : bool
298 ; mutable pagebias : int
299 ; mutable verbose : bool
300 ; mutable debug : bool
301 ; mutable scrollstep : int
302 ; mutable hscrollstep : int
303 ; mutable maxhfit : bool
304 ; mutable crophack : bool
305 ; mutable autoscrollstep : int
306 ; mutable maxwait : float option
307 ; mutable hlinks : bool
308 ; mutable underinfo : bool
309 ; mutable interpagespace : interpagespace
310 ; mutable zoom : float
311 ; mutable presentation : bool
312 ; mutable angle : angle
313 ; mutable winw : int
314 ; mutable winh : int
315 ; mutable savebmarks : bool
316 ; mutable proportional : proportional
317 ; mutable trimmargins : trimmargins
318 ; mutable trimfuzz : irect
319 ; mutable memlimit : memsize
320 ; mutable texcount : texcount
321 ; mutable sliceheight : sliceheight
322 ; mutable thumbw : width
323 ; mutable jumpback : bool
324 ; mutable bgcolor : float * float * float
325 ; mutable bedefault : bool
326 ; mutable scrollbarinpm : bool
327 ; mutable tilew : int
328 ; mutable tileh : int
329 ; mutable mustoresize : memsize
330 ; mutable checkers : bool
331 ; mutable aalevel : int
332 ; mutable urilauncher : string
333 ; mutable pathlauncher : string
334 ; mutable colorspace : colorspace
335 ; mutable invert : bool
336 ; mutable colorscale : float
337 ; mutable redirectstderr : bool
338 ; mutable ghyllscroll : (int * int * int) option
339 ; mutable columns : columns
340 ; mutable beyecolumns : columncount option
341 ; mutable selcmd : string
342 ; mutable updatecurs : bool
343 ; mutable keyhashes : (string * keyhash) list
344 ; mutable hfsize : int
345 ; mutable pgscale : float
346 ; mutable usepbo : bool
347 ; mutable wheelbypage : bool
349 and columns =
350 | Csingle of singlecolumn
351 | Cmulti of multicolumns
352 | Csplit of splitcolumns
355 type anchor = pageno * top * dtop;;
357 type outline = string * int * anchor;;
359 type rect = float * float * float * float * float * float * float * float;;
361 type tile = opaque * pixmapsize * elapsed
362 and elapsed = float;;
363 type pagemapkey = pageno * gen;;
364 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
365 and row = int
366 and col = int;;
368 let emptyanchor = (0, 0.0, 0.0);;
370 type infochange = | Memused | Docinfo | Pdim;;
372 class type uioh = object
373 method display : unit
374 method key : int -> int -> uioh
375 method button : int -> bool -> int -> int -> int -> uioh
376 method motion : int -> int -> uioh
377 method pmotion : int -> int -> uioh
378 method infochanged : infochange -> unit
379 method scrollpw : (int * float * float)
380 method scrollph : (int * float * float)
381 method modehash : keyhash
382 end;;
384 type mode =
385 | Birdseye of (conf * leftx * pageno * pageno * anchor)
386 | Textentry of (textentry * onleave)
387 | View
388 | LinkNav of linktarget
389 and onleave = leavetextentrystatus -> unit
390 and leavetextentrystatus = | Cancel | Confirm
391 and helpitem = string * int * action
392 and action =
393 | Noaction
394 | Action of (uioh -> uioh)
395 and linktarget =
396 | Ltexact of (pageno * int)
397 | Ltgendir of int
400 let isbirdseye = function Birdseye _ -> true | _ -> false;;
401 let istextentry = function Textentry _ -> true | _ -> false;;
403 type currently =
404 | Idle
405 | Loading of (page * gen)
406 | Tiling of (
407 page * opaque * colorspace * angle * gen * col * row * width * height
409 | Outlining of outline list
412 let emptykeyhash = Hashtbl.create 0;;
413 let nouioh : uioh = object (self)
414 method display = ()
415 method key _ _ = self
416 method button _ _ _ _ _ = self
417 method motion _ _ = self
418 method pmotion _ _ = self
419 method infochanged _ = ()
420 method scrollpw = (0, nan, nan)
421 method scrollph = (0, nan, nan)
422 method modehash = emptykeyhash
423 end;;
425 type state =
426 { mutable sr : Unix.file_descr
427 ; mutable sw : Unix.file_descr
428 ; mutable wsfd : Unix.file_descr
429 ; mutable errfd : Unix.file_descr option
430 ; mutable stderr : Unix.file_descr
431 ; mutable errmsgs : Buffer.t
432 ; mutable newerrmsgs : bool
433 ; mutable w : int
434 ; mutable x : int
435 ; mutable y : int
436 ; mutable scrollw : int
437 ; mutable hscrollh : int
438 ; mutable anchor : anchor
439 ; mutable ranchors : (string * string * anchor) list
440 ; mutable maxy : int
441 ; mutable layout : page list
442 ; pagemap : (pagemapkey, opaque) Hashtbl.t
443 ; tilemap : (tilemapkey, tile) Hashtbl.t
444 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
445 ; mutable pdims : (pageno * width * height * leftx) list
446 ; mutable pagecount : int
447 ; mutable currently : currently
448 ; mutable mstate : mstate
449 ; mutable searchpattern : string
450 ; mutable rects : (pageno * recttype * rect) list
451 ; mutable rects1 : (pageno * recttype * rect) list
452 ; mutable text : string
453 ; mutable fullscreen : (width * height) option
454 ; mutable mode : mode
455 ; mutable uioh : uioh
456 ; mutable outlines : outline array
457 ; mutable bookmarks : outline list
458 ; mutable path : string
459 ; mutable password : string
460 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
461 ; mutable memused : memsize
462 ; mutable gen : gen
463 ; mutable throttle : (page list * int * float) option
464 ; mutable autoscroll : int option
465 ; mutable ghyll : (int option -> unit)
466 ; mutable help : helpitem array
467 ; mutable docinfo : (int * string) list
468 ; mutable texid : GlTex.texture_id option
469 ; hists : hists
470 ; mutable prevzoom : float
471 ; mutable progress : float
472 ; mutable redisplay : bool
473 ; mutable mpos : mpos
474 ; mutable keystate : keystate
475 ; mutable glinks : bool
476 ; mutable prevcolumns : (columns * float) option
477 ; mutable wthack : bool
479 and hists =
480 { pat : string circbuf
481 ; pag : string circbuf
482 ; nav : anchor circbuf
483 ; sel : string circbuf
487 let defconf =
488 { scrollbw = 7
489 ; scrollh = 12
490 ; icase = true
491 ; preload = true
492 ; pagebias = 0
493 ; verbose = false
494 ; debug = false
495 ; scrollstep = 24
496 ; hscrollstep = 24
497 ; maxhfit = true
498 ; crophack = false
499 ; autoscrollstep = 2
500 ; maxwait = None
501 ; hlinks = false
502 ; underinfo = false
503 ; interpagespace = 2
504 ; zoom = 1.0
505 ; presentation = false
506 ; angle = 0
507 ; winw = 900
508 ; winh = 900
509 ; savebmarks = true
510 ; proportional = true
511 ; trimmargins = false
512 ; trimfuzz = (0,0,0,0)
513 ; memlimit = 32 lsl 20
514 ; texcount = 256
515 ; sliceheight = 24
516 ; thumbw = 76
517 ; jumpback = true
518 ; bgcolor = (0.5, 0.5, 0.5)
519 ; bedefault = false
520 ; scrollbarinpm = true
521 ; tilew = 2048
522 ; tileh = 2048
523 ; mustoresize = 256 lsl 20
524 ; checkers = true
525 ; aalevel = 8
526 ; urilauncher =
527 (match platform with
528 | Plinux | Pfreebsd | Pdragonflybsd
529 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
530 | Posx -> "open \"%s\""
531 | Pcygwin -> "cygstart \"%s\""
532 | Punknown -> "echo %s")
533 ; pathlauncher = "lp \"%s\""
534 ; selcmd =
535 (match platform with
536 | Plinux | Pfreebsd | Pdragonflybsd
537 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
538 | Posx -> "pbcopy"
539 | Pcygwin -> "wsel"
540 | Punknown -> "cat")
541 ; colorspace = Rgb
542 ; invert = false
543 ; colorscale = 1.0
544 ; redirectstderr = false
545 ; ghyllscroll = None
546 ; columns = Csingle [||]
547 ; beyecolumns = None
548 ; updatecurs = false
549 ; hfsize = 12
550 ; pgscale = 1.0
551 ; usepbo = false
552 ; wheelbypage = false
553 ; keyhashes =
554 let mk n = (n, Hashtbl.create 1) in
555 [ mk "global"
556 ; mk "info"
557 ; mk "help"
558 ; mk "outline"
559 ; mk "listview"
560 ; mk "birdseye"
561 ; mk "textentry"
562 ; mk "links"
563 ; mk "view"
568 let findkeyhash c name =
569 try List.assoc name c.keyhashes
570 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
573 let conf = { defconf with angle = defconf.angle };;
575 let pgscale h = truncate (float h *. conf.pgscale);;
577 type fontstate =
578 { mutable fontsize : int
579 ; mutable wwidth : float
580 ; mutable maxrows : int
584 let fstate =
585 { fontsize = 14
586 ; wwidth = nan
587 ; maxrows = -1
591 let setfontsize n =
592 fstate.fontsize <- n;
593 fstate.wwidth <- measurestr fstate.fontsize "w";
594 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
597 let geturl s =
598 let colonpos = try String.index s ':' with Not_found -> -1 in
599 let len = String.length s in
600 if colonpos >= 0 && colonpos + 3 < len
601 then (
602 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
603 then
604 let schemestartpos =
605 try String.rindex_from s colonpos ' '
606 with Not_found -> -1
608 let scheme =
609 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
611 match scheme with
612 | "http" | "ftp" | "mailto" ->
613 let epos =
614 try String.index_from s colonpos ' '
615 with Not_found -> len
617 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
618 | _ -> ""
619 else ""
621 else ""
624 let gotouri uri =
625 if String.length conf.urilauncher = 0
626 then print_endline uri
627 else (
628 let url = geturl uri in
629 if String.length url = 0
630 then print_endline uri
631 else
632 let re = Str.regexp "%s" in
633 let command = Str.global_replace re url conf.urilauncher in
634 try popen command []
635 with exn ->
636 Printf.eprintf
637 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
638 flush stderr;
642 let version () =
643 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
644 (platform_to_string platform) Sys.word_size Sys.ocaml_version
647 let makehelp () =
648 let strings = version () :: "" :: Help.keys in
649 Array.of_list (
650 List.map (fun s ->
651 let url = geturl s in
652 if String.length url > 0
653 then (s, 0, Action (fun u -> gotouri url; u))
654 else (s, 0, Noaction)
655 ) strings);
658 let noghyll _ = ();;
659 let firstgeomcmds = "", [];;
661 let state =
662 { sr = Unix.stdin
663 ; sw = Unix.stdin
664 ; wsfd = Unix.stdin
665 ; errfd = None
666 ; stderr = Unix.stderr
667 ; errmsgs = Buffer.create 0
668 ; newerrmsgs = false
669 ; x = 0
670 ; y = 0
671 ; w = 0
672 ; scrollw = 0
673 ; hscrollh = 0
674 ; anchor = emptyanchor
675 ; ranchors = []
676 ; layout = []
677 ; maxy = max_int
678 ; tilelru = Queue.create ()
679 ; pagemap = Hashtbl.create 10
680 ; tilemap = Hashtbl.create 10
681 ; pdims = []
682 ; pagecount = 0
683 ; currently = Idle
684 ; mstate = Mnone
685 ; rects = []
686 ; rects1 = []
687 ; text = ""
688 ; mode = View
689 ; fullscreen = None
690 ; searchpattern = ""
691 ; outlines = [||]
692 ; bookmarks = []
693 ; path = ""
694 ; password = ""
695 ; geomcmds = firstgeomcmds
696 ; hists =
697 { nav = cbnew 10 emptyanchor
698 ; pat = cbnew 10 ""
699 ; pag = cbnew 10 ""
700 ; sel = cbnew 10 ""
702 ; memused = 0
703 ; gen = 0
704 ; throttle = None
705 ; autoscroll = None
706 ; ghyll = noghyll
707 ; help = makehelp ()
708 ; docinfo = []
709 ; texid = None
710 ; prevzoom = 1.0
711 ; progress = -1.0
712 ; uioh = nouioh
713 ; redisplay = true
714 ; mpos = (-1, -1)
715 ; keystate = KSnone
716 ; glinks = false
717 ; prevcolumns = None
718 ; wthack = false
722 let vlog fmt =
723 if conf.verbose
724 then
725 Printf.kprintf prerr_endline fmt
726 else
727 Printf.kprintf ignore fmt
730 let launchpath () =
731 if String.length conf.pathlauncher = 0
732 then print_endline state.path
733 else (
734 let re = Str.regexp "%s" in
735 let command = Str.global_replace re state.path conf.pathlauncher in
736 try popen command []
737 with exn ->
738 Printf.eprintf
739 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
740 flush stderr;
744 module Ne = struct
745 type 'a t = | Res of 'a | Exn of exn;;
747 let pipe () =
748 try Res (Unix.pipe ())
749 with exn -> Exn exn
752 let clo fd f =
753 try Unix.close fd
754 with exn -> f (Printexc.to_string exn)
757 let dup fd =
758 try Res (Unix.dup fd)
759 with exn -> Exn exn
762 let dup2 fd1 fd2 =
763 try Res (Unix.dup2 fd1 fd2)
764 with exn -> Exn exn
766 end;;
768 let redirectstderr () =
769 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
770 if conf.redirectstderr
771 then
772 match Ne.pipe () with
773 | Ne.Exn exn ->
774 dolog "failed to create stderr redirection pipes: %s"
775 (Printexc.to_string exn)
777 | Ne.Res (r, w) ->
778 begin match Ne.dup Unix.stderr with
779 | Ne.Exn exn ->
780 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
781 Ne.clo r (clofail "pipe/r");
782 Ne.clo w (clofail "pipe/w");
784 | Ne.Res dupstderr ->
785 begin match Ne.dup2 w Unix.stderr with
786 | Ne.Exn exn ->
787 dolog "failed to dup2 to stderr: %s"
788 (Printexc.to_string exn);
789 Ne.clo dupstderr (clofail "stderr duplicate");
790 Ne.clo r (clofail "redir pipe/r");
791 Ne.clo w (clofail "redir pipe/w");
793 | Ne.Res () ->
794 state.stderr <- dupstderr;
795 state.errfd <- Some r;
796 end;
798 else (
799 state.newerrmsgs <- false;
800 begin match state.errfd with
801 | Some fd ->
802 begin match Ne.dup2 state.stderr Unix.stderr with
803 | Ne.Exn exn ->
804 dolog "failed to dup2 original stderr: %s"
805 (Printexc.to_string exn)
806 | Ne.Res () ->
807 Ne.clo fd (clofail "dup of stderr");
808 Unix.dup2 state.stderr Unix.stderr;
809 state.errfd <- None;
810 end;
811 | None -> ()
812 end;
813 prerr_string (Buffer.contents state.errmsgs);
814 flush stderr;
815 Buffer.clear state.errmsgs;
819 module G =
820 struct
821 let postRedisplay who =
822 if conf.verbose
823 then prerr_endline ("redisplay for " ^ who);
824 state.redisplay <- true;
826 end;;
828 let getopaque pageno =
829 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
830 with Not_found -> None
833 let putopaque pageno opaque =
834 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
837 let pagetranslatepoint l x y =
838 let dy = y - l.pagedispy in
839 let y = dy + l.pagey in
840 let dx = x - l.pagedispx in
841 let x = dx + l.pagex in
842 (x, y);
845 let getunder x y =
846 let rec f = function
847 | l :: rest ->
848 begin match getopaque l.pageno with
849 | Some opaque ->
850 let x0 = l.pagedispx in
851 let x1 = x0 + l.pagevw in
852 let y0 = l.pagedispy in
853 let y1 = y0 + l.pagevh in
854 if y >= y0 && y <= y1 && x >= x0 && x <= x1
855 then
856 let px, py = pagetranslatepoint l x y in
857 match whatsunder opaque px py with
858 | Unone -> f rest
859 | under -> under
860 else f rest
861 | _ ->
862 f rest
864 | [] -> Unone
866 f state.layout
869 let showtext c s =
870 state.text <- Printf.sprintf "%c%s" c s;
871 G.postRedisplay "showtext";
874 let undertext = function
875 | Unone -> "none"
876 | Ulinkuri s -> s
877 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
878 | Utext s -> "font: " ^ s
879 | Uunexpected s -> "unexpected: " ^ s
880 | Ulaunch s -> "launch: " ^ s
881 | Unamed s -> "named: " ^ s
882 | Uremote (filename, pageno) ->
883 Printf.sprintf "%s: page %d" filename (pageno+1)
886 let updateunder x y =
887 match getunder x y with
888 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
889 | Ulinkuri uri ->
890 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
891 Wsi.setcursor Wsi.CURSOR_INFO
892 | Ulinkgoto (pageno, _) ->
893 if conf.underinfo
894 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
895 Wsi.setcursor Wsi.CURSOR_INFO
896 | Utext s ->
897 if conf.underinfo then showtext 'f' ("ont: " ^ s);
898 Wsi.setcursor Wsi.CURSOR_TEXT
899 | Uunexpected s ->
900 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
901 Wsi.setcursor Wsi.CURSOR_INHERIT
902 | Ulaunch s ->
903 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
904 Wsi.setcursor Wsi.CURSOR_INHERIT
905 | Unamed s ->
906 if conf.underinfo then showtext 'n' ("amed: " ^ s);
907 Wsi.setcursor Wsi.CURSOR_INHERIT
908 | Uremote (filename, pageno) ->
909 if conf.underinfo then showtext 'r'
910 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
911 Wsi.setcursor Wsi.CURSOR_INFO
914 let showlinktype under =
915 if conf.underinfo
916 then
917 match under with
918 | Unone -> ()
919 | under ->
920 let s = undertext under in
921 showtext ' ' s
924 let addchar s c =
925 let b = Buffer.create (String.length s + 1) in
926 Buffer.add_string b s;
927 Buffer.add_char b c;
928 Buffer.contents b;
931 let colorspace_of_string s =
932 match String.lowercase s with
933 | "rgb" -> Rgb
934 | "bgr" -> Bgr
935 | "gray" -> Gray
936 | _ -> failwith "invalid colorspace"
939 let int_of_colorspace = function
940 | Rgb -> 0
941 | Bgr -> 1
942 | Gray -> 2
945 let colorspace_of_int = function
946 | 0 -> Rgb
947 | 1 -> Bgr
948 | 2 -> Gray
949 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
952 let colorspace_to_string = function
953 | Rgb -> "rgb"
954 | Bgr -> "bgr"
955 | Gray -> "gray"
958 let intentry_with_suffix text key =
959 let c =
960 if key >= 32 && key < 127
961 then Char.chr key
962 else '\000'
964 match Char.lowercase c with
965 | '0' .. '9' ->
966 let text = addchar text c in
967 TEcont text
969 | 'k' | 'm' | 'g' ->
970 let text = addchar text c in
971 TEcont text
973 | _ ->
974 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
975 TEcont text
978 let multicolumns_to_string (n, a, b) =
979 if a = 0 && b = 0
980 then Printf.sprintf "%d" n
981 else Printf.sprintf "%d,%d,%d" n a b;
984 let multicolumns_of_string s =
986 (int_of_string s, 0, 0)
987 with _ ->
988 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
989 if a > 1 || b > 1
990 then failwith "subtly broken"; (n, a, b)
994 let readcmd fd =
995 let s = "xxxx" in
996 let n = Unix.read fd s 0 4 in
997 if n != 4 then failwith "incomplete read(len)";
998 let len = 0
999 lor (Char.code s.[0] lsl 24)
1000 lor (Char.code s.[1] lsl 16)
1001 lor (Char.code s.[2] lsl 8)
1002 lor (Char.code s.[3] lsl 0)
1004 let s = String.create len in
1005 let n = Unix.read fd s 0 len in
1006 if n != len then failwith "incomplete read(data)";
1010 let btod b = if b then 1 else 0;;
1012 let wcmd fmt =
1013 let b = Buffer.create 16 in
1014 Buffer.add_string b "llll";
1015 Printf.kbprintf
1016 (fun b ->
1017 let s = Buffer.contents b in
1018 let n = String.length s in
1019 let len = n - 4 in
1020 (* dolog "wcmd %S" (String.sub s 4 len); *)
1021 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1022 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1023 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1024 s.[3] <- Char.chr (len land 0xff);
1025 let n' = Unix.write state.sw s 0 n in
1026 if n' != n then failwith "write failed";
1027 ) b fmt;
1030 let calcips h =
1031 let d = conf.winh - h in
1032 max conf.interpagespace ((d + 1) / 2)
1035 let rowyh (c, coverA, coverB) b n =
1036 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1037 then
1038 let _, _, vy, (_, _, h, _) = b.(n) in
1039 (vy, h)
1040 else
1041 let n' = n - coverA in
1042 let d = n' mod c in
1043 let s = n - d in
1044 let e = min state.pagecount (s + c) in
1045 let rec find m miny maxh = if m = e then miny, maxh else
1046 let _, _, y, (_, _, h, _) = b.(m) in
1047 let miny = min miny y in
1048 let maxh = max maxh h in
1049 find (m+1) miny maxh
1050 in find s max_int 0
1053 let calcheight () =
1054 match conf.columns with
1055 | Cmulti ((_, _, _) as cl, b) ->
1056 if Array.length b > 0
1057 then
1058 let y, h = rowyh cl b (Array.length b - 1) in
1059 y + h + (if conf.presentation then calcips h else 0)
1060 else 0
1061 | Csingle b ->
1062 if Array.length b > 0
1063 then
1064 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1065 y + h + (if conf.presentation then calcips h else 0)
1066 else 0
1067 | Csplit (_, b) ->
1068 if Array.length b > 0
1069 then
1070 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1071 y + h
1072 else 0
1075 let getpageyh pageno =
1076 let pageno = bound pageno 0 (state.pagecount-1) in
1077 match conf.columns with
1078 | Csingle b ->
1079 if Array.length b = 0
1080 then 0, 0
1081 else
1082 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1083 let y =
1084 if conf.presentation
1085 then y - calcips h
1086 else y
1088 y, h
1089 | Cmulti (cl, b) ->
1090 if Array.length b = 0
1091 then 0, 0
1092 else
1093 let y, h = rowyh cl b pageno in
1094 let y =
1095 if conf.presentation
1096 then y - calcips h
1097 else y
1099 y, h
1100 | Csplit (c, b) ->
1101 if Array.length b = 0
1102 then 0, 0
1103 else
1104 let n = pageno*c in
1105 let (_, _, y, (_, _, h, _)) = b.(n) in
1106 y, h
1109 let getpagedim pageno =
1110 let rec f ppdim l =
1111 match l with
1112 | (n, _, _, _) as pdim :: rest ->
1113 if n >= pageno
1114 then (if n = pageno then pdim else ppdim)
1115 else f pdim rest
1117 | [] -> ppdim
1119 f (-1, -1, -1, -1) state.pdims
1122 let getpagey pageno = fst (getpageyh pageno);;
1124 let nogeomcmds cmds =
1125 match cmds with
1126 | s, [] -> String.length s = 0
1127 | _ -> false
1130 let page_of_y y =
1131 let ((c, coverA, coverB) as cl), b =
1132 match conf.columns with
1133 | Csingle b -> (1, 0, 0), b
1134 | Cmulti (c, b) -> c, b
1135 | Csplit (_, b) -> (1, 0, 0), b
1137 let rec bsearch nmin nmax =
1138 if nmin > nmax
1139 then bound nmin 0 (state.pagecount-1)
1140 else
1141 let n = (nmax + nmin) / 2 in
1142 let vy, h = rowyh cl b n in
1143 let y0, y1 =
1144 if conf.presentation
1145 then
1146 let ips = calcips h in
1147 let y0 = vy - ips in
1148 let y1 = vy + h + ips in
1149 y0, y1
1150 else (
1151 if n = 0
1152 then 0, vy + h + conf.interpagespace
1153 else
1154 let y0 = vy - conf.interpagespace in
1155 y0, y0 + h + conf.interpagespace
1158 if y >= y0 && y < y1
1159 then (
1160 if c = 1
1161 then n
1162 else (
1163 if n > coverA
1164 then
1165 if n < state.pagecount - coverB
1166 then ((n-coverA)/c)*c + coverA
1167 else n
1168 else n
1171 else (
1172 if y > y0
1173 then bsearch (n+1) nmax
1174 else bsearch nmin (n-1)
1177 let r = bsearch 0 (state.pagecount-1) in
1181 let layoutN ((columns, coverA, coverB), b) y sh =
1182 let sh = sh - state.hscrollh in
1183 let rec fold accu n =
1184 if n = Array.length b
1185 then accu
1186 else
1187 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1188 if (vy - y) > sh &&
1189 (n = coverA - 1
1190 || n = state.pagecount - coverB
1191 || (n - coverA) mod columns = columns - 1)
1192 then accu
1193 else
1194 let accu =
1195 if vy + h > y
1196 then
1197 let pagey = max 0 (y - vy) in
1198 let pagedispy = if pagey > 0 then 0 else vy - y in
1199 let pagedispx, pagex =
1200 let pdx =
1201 if n = coverA - 1 || n = state.pagecount - coverB
1202 then state.x + (conf.winw - state.scrollw - w) / 2
1203 else dx + xoff + state.x
1205 if pdx < 0
1206 then 0, -pdx
1207 else pdx, 0
1209 let pagevw =
1210 let vw = conf.winw - state.scrollw - pagedispx in
1211 let pw = w - pagex in
1212 min vw pw
1214 let pagevh = min (h - pagey) (sh - pagedispy) in
1215 if pagevw > 0 && pagevh > 0
1216 then
1217 let e =
1218 { pageno = n
1219 ; pagedimno = pdimno
1220 ; pagew = w
1221 ; pageh = h
1222 ; pagex = pagex
1223 ; pagey = pagey
1224 ; pagevw = pagevw
1225 ; pagevh = pagevh
1226 ; pagedispx = pagedispx
1227 ; pagedispy = pagedispy
1228 ; pagecol = 0
1231 e :: accu
1232 else
1233 accu
1234 else
1235 accu
1237 fold accu (n+1)
1239 List.rev (fold [] (page_of_y y));
1242 let layoutS (columns, b) y sh =
1243 let sh = sh - state.hscrollh in
1244 let rec fold accu n =
1245 if n = Array.length b
1246 then accu
1247 else
1248 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1249 if (vy - y) > sh
1250 then accu
1251 else
1252 let accu =
1253 if vy + pageh > y
1254 then
1255 let x = xoff + state.x in
1256 let pagey = max 0 (y - vy) in
1257 let pagedispy = if pagey > 0 then 0 else vy - y in
1258 let pagedispx, pagex =
1259 if px = 0
1260 then (
1261 if x < 0
1262 then 0, -x
1263 else x, 0
1265 else (
1266 let px = px - x in
1267 if px < 0
1268 then -px, 0
1269 else 0, px
1272 let pagecolw = pagew/columns in
1273 let pagedispx =
1274 if pagecolw < conf.winw
1275 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1276 else pagedispx
1278 let pagevw =
1279 let vw = conf.winw - pagedispx - state.scrollw in
1280 let pw = pagew - pagex in
1281 min vw pw
1283 let pagevw = min pagevw pagecolw in
1284 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1285 if pagevw > 0 && pagevh > 0
1286 then
1287 let e =
1288 { pageno = n/columns
1289 ; pagedimno = pdimno
1290 ; pagew = pagew
1291 ; pageh = pageh
1292 ; pagex = pagex
1293 ; pagey = pagey
1294 ; pagevw = pagevw
1295 ; pagevh = pagevh
1296 ; pagedispx = pagedispx
1297 ; pagedispy = pagedispy
1298 ; pagecol = n mod columns
1301 e :: accu
1302 else
1303 accu
1304 else
1305 accu
1307 fold accu (n+1)
1309 List.rev (fold [] 0)
1312 let layout y sh =
1313 if nogeomcmds state.geomcmds
1314 then
1315 match conf.columns with
1316 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1317 | Cmulti c -> layoutN c y sh
1318 | Csplit s -> layoutS s y sh
1319 else []
1322 let clamp incr =
1323 let y = state.y + incr in
1324 let y = max 0 y in
1325 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1329 let itertiles l f =
1330 let tilex = l.pagex mod conf.tilew in
1331 let tiley = l.pagey mod conf.tileh in
1333 let col = l.pagex / conf.tilew in
1334 let row = l.pagey / conf.tileh in
1336 let rec rowloop row y0 dispy h =
1337 if h = 0
1338 then ()
1339 else (
1340 let dh = conf.tileh - y0 in
1341 let dh = min h dh in
1342 let rec colloop col x0 dispx w =
1343 if w = 0
1344 then ()
1345 else (
1346 let dw = conf.tilew - x0 in
1347 let dw = min w dw in
1349 f col row dispx dispy x0 y0 dw dh;
1350 colloop (col+1) 0 (dispx+dw) (w-dw)
1353 colloop col tilex l.pagedispx l.pagevw;
1354 rowloop (row+1) 0 (dispy+dh) (h-dh)
1357 if l.pagevw > 0 && l.pagevh > 0
1358 then rowloop row tiley l.pagedispy l.pagevh;
1361 let gettileopaque l col row =
1362 let key =
1363 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1365 try Some (Hashtbl.find state.tilemap key)
1366 with Not_found -> None
1369 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1370 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1371 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1374 let drawtiles l color =
1375 GlDraw.color color;
1376 let f col row x y tilex tiley w h =
1377 match gettileopaque l col row with
1378 | Some (opaque, _, t) ->
1379 let params = x, y, w, h, tilex, tiley in
1380 if conf.invert
1381 then (
1382 Gl.enable `blend;
1383 GlFunc.blend_func `zero `one_minus_src_color;
1385 drawtile params opaque;
1386 if conf.invert
1387 then Gl.disable `blend;
1388 if conf.debug
1389 then (
1390 let s = Printf.sprintf
1391 "%d[%d,%d] %f sec"
1392 l.pageno col row t
1394 let w = measurestr fstate.fontsize s in
1395 GlMisc.push_attrib [`current];
1396 GlDraw.color (0.0, 0.0, 0.0);
1397 GlDraw.rect
1398 (float (x-2), float (y-2))
1399 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1400 GlDraw.color (1.0, 1.0, 1.0);
1401 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1402 GlMisc.pop_attrib ();
1405 | _ ->
1406 let w =
1407 let lw = conf.winw - state.scrollw - x in
1408 min lw w
1409 and h =
1410 let lh = conf.winh - y in
1411 min lh h
1413 begin match state.texid with
1414 | Some id ->
1415 Gl.enable `texture_2d;
1416 GlTex.bind_texture `texture_2d id;
1417 let x0 = float x
1418 and y0 = float y
1419 and x1 = float (x+w)
1420 and y1 = float (y+h) in
1422 let tw = float w /. 16.0
1423 and th = float h /. 16.0 in
1424 let tx0 = float tilex /. 16.0
1425 and ty0 = float tiley /. 16.0 in
1426 let tx1 = tx0 +. tw
1427 and ty1 = ty0 +. th in
1428 GlDraw.begins `quads;
1429 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1430 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1431 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1432 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1433 GlDraw.ends ();
1435 Gl.disable `texture_2d;
1436 | None ->
1437 GlDraw.color (1.0, 1.0, 1.0);
1438 GlDraw.rect
1439 (float x, float y)
1440 (float (x+w), float (y+h));
1441 end;
1442 if w > 128 && h > fstate.fontsize + 10
1443 then (
1444 GlDraw.color (0.0, 0.0, 0.0);
1445 let c, r =
1446 if conf.verbose
1447 then (col*conf.tilew, row*conf.tileh)
1448 else col, row
1450 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1452 GlDraw.color color;
1454 itertiles l f
1457 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1459 let tilevisible1 l x y =
1460 let ax0 = l.pagex
1461 and ax1 = l.pagex + l.pagevw
1462 and ay0 = l.pagey
1463 and ay1 = l.pagey + l.pagevh in
1465 let bx0 = x
1466 and by0 = y in
1467 let bx1 = min (bx0 + conf.tilew) l.pagew
1468 and by1 = min (by0 + conf.tileh) l.pageh in
1470 let rx0 = max ax0 bx0
1471 and ry0 = max ay0 by0
1472 and rx1 = min ax1 bx1
1473 and ry1 = min ay1 by1 in
1475 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1476 nonemptyintersection
1479 let tilevisible layout n x y =
1480 let rec findpageinlayout m = function
1481 | l :: rest when l.pageno = n ->
1482 tilevisible1 l x y || (
1483 match conf.columns with
1484 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1485 | _ -> false
1487 | _ :: rest -> findpageinlayout 0 rest
1488 | [] -> false
1490 findpageinlayout 0 layout;
1493 let tileready l x y =
1494 tilevisible1 l x y &&
1495 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1498 let tilepage n p layout =
1499 let rec loop = function
1500 | l :: rest ->
1501 if l.pageno = n
1502 then
1503 let f col row _ _ _ _ _ _ =
1504 if state.currently = Idle
1505 then
1506 match gettileopaque l col row with
1507 | Some _ -> ()
1508 | None ->
1509 let x = col*conf.tilew
1510 and y = row*conf.tileh in
1511 let w =
1512 let w = l.pagew - x in
1513 min w conf.tilew
1515 let h =
1516 let h = l.pageh - y in
1517 min h conf.tileh
1519 let pbo =
1520 if conf.usepbo
1521 then getpbo w h conf.colorspace
1522 else "0"
1524 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1525 state.currently <-
1526 Tiling (
1527 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1528 conf.tilew, conf.tileh
1531 itertiles l f;
1532 else
1533 loop rest
1535 | [] -> ()
1537 if nogeomcmds state.geomcmds
1538 then loop layout;
1541 let preloadlayout y =
1542 let y = if y < conf.winh then 0 else y - conf.winh in
1543 let h = conf.winh*3 in
1544 layout y h;
1547 let load pages =
1548 let rec loop pages =
1549 if state.currently != Idle
1550 then ()
1551 else
1552 match pages with
1553 | l :: rest ->
1554 begin match getopaque l.pageno with
1555 | None ->
1556 wcmd "page %d %d" l.pageno l.pagedimno;
1557 state.currently <- Loading (l, state.gen);
1558 | Some opaque ->
1559 tilepage l.pageno opaque pages;
1560 loop rest
1561 end;
1562 | _ -> ()
1564 if nogeomcmds state.geomcmds
1565 then loop pages
1568 let preload pages =
1569 load pages;
1570 if conf.preload && state.currently = Idle
1571 then load (preloadlayout state.y);
1574 let layoutready layout =
1575 let rec fold all ls =
1576 all && match ls with
1577 | l :: rest ->
1578 let seen = ref false in
1579 let allvisible = ref true in
1580 let foo col row _ _ _ _ _ _ =
1581 seen := true;
1582 allvisible := !allvisible &&
1583 begin match gettileopaque l col row with
1584 | Some _ -> true
1585 | None -> false
1588 itertiles l foo;
1589 fold (!seen && !allvisible) rest
1590 | [] -> true
1592 let alltilesvisible = fold true layout in
1593 alltilesvisible;
1596 let gotoy y =
1597 let y = bound y 0 state.maxy in
1598 let y, layout, proceed =
1599 match conf.maxwait with
1600 | Some time when state.ghyll == noghyll ->
1601 begin match state.throttle with
1602 | None ->
1603 let layout = layout y conf.winh in
1604 let ready = layoutready layout in
1605 if not ready
1606 then (
1607 load layout;
1608 state.throttle <- Some (layout, y, now ());
1610 else G.postRedisplay "gotoy showall (None)";
1611 y, layout, ready
1612 | Some (_, _, started) ->
1613 let dt = now () -. started in
1614 if dt > time
1615 then (
1616 state.throttle <- None;
1617 let layout = layout y conf.winh in
1618 load layout;
1619 G.postRedisplay "maxwait";
1620 y, layout, true
1622 else -1, [], false
1625 | _ ->
1626 let layout = layout y conf.winh in
1627 if true || layoutready layout
1628 then G.postRedisplay "gotoy ready";
1629 y, layout, true
1631 if proceed
1632 then (
1633 state.y <- y;
1634 state.layout <- layout;
1635 begin match state.mode with
1636 | LinkNav (Ltexact (pageno, linkno)) ->
1637 let rec loop = function
1638 | [] ->
1639 state.mode <- LinkNav (Ltgendir 0)
1640 | l :: _ when l.pageno = pageno ->
1641 begin match getopaque pageno with
1642 | None ->
1643 state.mode <- LinkNav (Ltgendir 0)
1644 | Some opaque ->
1645 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1646 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1647 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1648 then state.mode <- LinkNav (Ltgendir 0)
1650 | _ :: rest -> loop rest
1652 loop layout
1653 | _ -> ()
1654 end;
1655 begin match state.mode with
1656 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1657 if not (pagevisible layout pageno)
1658 then (
1659 match state.layout with
1660 | [] -> ()
1661 | l :: _ ->
1662 state.mode <- Birdseye (
1663 conf, leftx, l.pageno, hooverpageno, anchor
1666 | LinkNav (Ltgendir dir as lt) ->
1667 let linknav =
1668 let rec loop = function
1669 | [] -> lt
1670 | l :: rest ->
1671 match getopaque l.pageno with
1672 | None -> loop rest
1673 | Some opaque ->
1674 let link =
1675 let ld =
1676 if dir = 0
1677 then LDfirstvisible (l.pagex, l.pagey, dir)
1678 else (
1679 if dir > 0 then LDfirst else LDlast
1682 findlink opaque ld
1684 match link with
1685 | Lnotfound -> loop rest
1686 | Lfound n ->
1687 showlinktype (getlink opaque n);
1688 Ltexact (l.pageno, n)
1690 loop state.layout
1692 state.mode <- LinkNav linknav
1693 | _ -> ()
1694 end;
1695 preload layout;
1697 state.ghyll <- noghyll;
1698 if conf.updatecurs
1699 then (
1700 let mx, my = state.mpos in
1701 updateunder mx my;
1705 let conttiling pageno opaque =
1706 tilepage pageno opaque
1707 (if conf.preload then preloadlayout state.y else state.layout)
1710 let gotoy_and_clear_text y =
1711 if not conf.verbose then state.text <- "";
1712 gotoy y;
1715 let getanchor1 l =
1716 let top =
1717 let coloff = l.pagecol * l.pageh in
1718 float (l.pagey + coloff) /. float l.pageh
1720 let dtop =
1721 if l.pagedispy = 0
1722 then
1724 else
1725 if conf.presentation
1726 then float l.pagedispy /. float (calcips l.pageh)
1727 else float l.pagedispy /. float conf.interpagespace
1729 (l.pageno, top, dtop)
1732 let getanchor () =
1733 match state.layout with
1734 | l :: _ -> getanchor1 l
1735 | [] ->
1736 let n = page_of_y state.y in
1737 let y, h = getpageyh n in
1738 let dy = y - state.y in
1739 let dtop =
1740 if conf.presentation
1741 then
1742 let ips = calcips h in
1743 float (dy + ips) /. float ips
1744 else
1745 float dy /. float conf.interpagespace
1747 (n, 0.0, dtop)
1750 let getanchory (n, top, dtop) =
1751 let y, h = getpageyh n in
1752 if conf.presentation
1753 then
1754 let ips = calcips h in
1755 y + truncate (top*.float h -. dtop*.float ips) + ips;
1756 else
1757 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1760 let gotoanchor anchor =
1761 gotoy (getanchory anchor);
1764 let addnav () =
1765 cbput state.hists.nav (getanchor ());
1768 let getnav dir =
1769 let anchor = cbgetc state.hists.nav dir in
1770 getanchory anchor;
1773 let gotoghyll y =
1774 let scroll f n a b =
1775 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1776 let snake f a b =
1777 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1778 if f < a
1779 then s (float f /. float a)
1780 else (
1781 if f > b
1782 then 1.0 -. s ((float (f-b) /. float (n-b)))
1783 else 1.0
1786 snake f a b
1787 and summa f n a b =
1788 (* courtesy:
1789 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1790 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1791 let iv1 = iv f in
1792 let ins = float a *. iv1
1793 and outs = float (n-b) *. iv1 in
1794 let ones = b - a in
1795 ins +. outs +. float ones
1797 let rec set (_N, _A, _B) y sy =
1798 let sum = summa 1.0 _N _A _B in
1799 let dy = float (y - sy) in
1800 state.ghyll <- (
1801 let rec gf n y1 o =
1802 if n >= _N
1803 then state.ghyll <- noghyll
1804 else
1805 let go n =
1806 let s = scroll n _N _A _B in
1807 let y1 = y1 +. ((s *. dy) /. sum) in
1808 gotoy_and_clear_text (truncate y1);
1809 state.ghyll <- gf (n+1) y1;
1811 match o with
1812 | None -> go n
1813 | Some y' -> set (_N/2, 1, 1) y' state.y
1815 gf 0 (float state.y)
1818 match conf.ghyllscroll with
1819 | None ->
1820 gotoy_and_clear_text y
1821 | Some nab ->
1822 if state.ghyll == noghyll
1823 then set nab y state.y
1824 else state.ghyll (Some y)
1827 let gotopage n top =
1828 let y, h = getpageyh n in
1829 let y = y + (truncate (top *. float h)) in
1830 gotoghyll y
1833 let gotopage1 n top =
1834 let y = getpagey n in
1835 let y = y + top in
1836 gotoghyll y
1839 let invalidate s f =
1840 state.layout <- [];
1841 state.pdims <- [];
1842 state.rects <- [];
1843 state.rects1 <- [];
1844 match state.geomcmds with
1845 | ps, [] when String.length ps = 0 ->
1846 f ();
1847 state.geomcmds <- s, [];
1849 | ps, [] ->
1850 state.geomcmds <- ps, [s, f];
1852 | ps, (s', _) :: rest when s' = s ->
1853 state.geomcmds <- ps, ((s, f) :: rest);
1855 | ps, cmds ->
1856 state.geomcmds <- ps, ((s, f) :: cmds);
1859 let flushpages () =
1860 Hashtbl.iter (fun _ opaque ->
1861 wcmd "freepage %s" opaque;
1862 ) state.pagemap;
1863 Hashtbl.clear state.pagemap;
1866 let opendoc path password =
1867 state.path <- path;
1868 state.password <- password;
1869 state.gen <- state.gen + 1;
1870 state.docinfo <- [];
1872 flushpages ();
1873 setaalevel conf.aalevel;
1874 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1875 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1876 invalidate "reqlayout"
1877 (fun () ->
1878 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1881 let reload () =
1882 state.anchor <- getanchor ();
1883 state.wthack <- true;
1884 opendoc state.path state.password;
1887 let scalecolor c =
1888 let c = c *. conf.colorscale in
1889 (c, c, c);
1892 let scalecolor2 (r, g, b) =
1893 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1896 let docolumns = function
1897 | Csingle _ ->
1898 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1899 let rec loop pageno pdimno pdim y ph pdims =
1900 if pageno = state.pagecount
1901 then ()
1902 else
1903 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1904 match pdims with
1905 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1906 pdimno+1, pdim, rest
1907 | _ ->
1908 pdimno, pdim, pdims
1910 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1911 let y = y +
1912 (if conf.presentation
1913 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1914 else (if pageno = 0 then 0 else conf.interpagespace)
1917 a.(pageno) <- (pdimno, x, y, pdim);
1918 loop (pageno+1) pdimno pdim (y + h) h pdims
1920 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1921 conf.columns <- Csingle a;
1923 | Cmulti ((columns, coverA, coverB), _) ->
1924 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1925 let rec loop pageno pdimno pdim x y rowh pdims =
1926 let rec fixrow m = if m = pageno then () else
1927 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1928 if h < rowh
1929 then (
1930 let y = y + (rowh - h) / 2 in
1931 a.(m) <- (pdimno, x, y, pdim);
1933 fixrow (m+1)
1935 if pageno = state.pagecount
1936 then fixrow (((pageno - 1) / columns) * columns)
1937 else
1938 let pdimno, ((_, w, h, xoff) 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 x, y, rowh' =
1946 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1947 then (
1948 let x = (conf.winw - state.scrollw - w) / 2 in
1949 let ips =
1950 if conf.presentation then calcips h else conf.interpagespace in
1951 x, y + ips + rowh, h
1953 else (
1954 if (pageno - coverA) mod columns = 0
1955 then (
1956 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1957 let y =
1958 if conf.presentation
1959 then
1960 let ips = calcips h in
1961 y + (if pageno = 0 then 0 else calcips rowh + ips)
1962 else
1963 y + (if pageno = 0 then 0 else conf.interpagespace)
1965 x, y + rowh, h
1967 else x, y, max rowh h
1970 let y =
1971 if pageno > 1 && (pageno - coverA) mod columns = 0
1972 then (
1973 let y =
1974 if pageno = columns && conf.presentation
1975 then (
1976 let ips = calcips rowh in
1977 for i = 0 to pred columns
1979 let (pdimno, x, y, pdim) = a.(i) in
1980 a.(i) <- (pdimno, x, y+ips, pdim)
1981 done;
1982 y+ips;
1984 else y
1986 fixrow (pageno - columns);
1989 else y
1991 a.(pageno) <- (pdimno, x, y, pdim);
1992 let x = x + w + xoff*2 + conf.interpagespace in
1993 loop (pageno+1) pdimno pdim x y rowh' pdims
1995 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1996 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1998 | Csplit (c, _) ->
1999 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2000 let rec loop pageno pdimno pdim y pdims =
2001 if pageno = state.pagecount
2002 then ()
2003 else
2004 let pdimno, ((_, w, h, _) as pdim), pdims =
2005 match pdims with
2006 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2007 pdimno+1, pdim, rest
2008 | _ ->
2009 pdimno, pdim, pdims
2011 let cw = w / c in
2012 let rec loop1 n x y =
2013 if n = c then y else (
2014 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2015 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2018 let y = loop1 0 0 y in
2019 loop (pageno+1) pdimno pdim y pdims
2021 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2022 conf.columns <- Csplit (c, a);
2025 let represent () =
2026 docolumns conf.columns;
2027 state.maxy <- calcheight ();
2028 state.hscrollh <-
2029 if state.w <= conf.winw - state.scrollw
2030 then 0
2031 else state.scrollw
2033 match state.mode with
2034 | Birdseye (_, _, pageno, _, _) ->
2035 let y, h = getpageyh pageno in
2036 let top = (conf.winh - h) / 2 in
2037 gotoy (max 0 (y - top))
2038 | _ -> gotoanchor state.anchor
2041 let reshape w h =
2042 state.wthack <- false;
2043 GlDraw.viewport 0 0 w h;
2044 let firsttime = state.geomcmds == firstgeomcmds in
2045 if not firsttime && nogeomcmds state.geomcmds
2046 then state.anchor <- getanchor ();
2048 conf.winw <- w;
2049 let w = truncate (float w *. conf.zoom) - state.scrollw in
2050 let w = max w 2 in
2051 conf.winh <- h;
2052 setfontsize fstate.fontsize;
2053 GlMat.mode `modelview;
2054 GlMat.load_identity ();
2056 GlMat.mode `projection;
2057 GlMat.load_identity ();
2058 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2059 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2060 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2062 let relx =
2063 if conf.zoom <= 1.0
2064 then 0.0
2065 else float state.x /. float state.w
2067 invalidate "geometry"
2068 (fun () ->
2069 state.w <- w;
2070 if not firsttime
2071 then state.x <- truncate (relx *. float w);
2072 let w =
2073 match conf.columns with
2074 | Csingle _ -> w
2075 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2076 | Csplit (c, _) -> w * c
2078 wcmd "geometry %d %d" w h);
2081 let enttext () =
2082 let len = String.length state.text in
2083 let drawstring s =
2084 let hscrollh =
2085 match state.mode with
2086 | Textentry _
2087 | View ->
2088 let h, _, _ = state.uioh#scrollpw in
2090 | _ -> 0
2092 let rect x w =
2093 GlDraw.rect
2094 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2095 (x+.w, float (conf.winh - hscrollh))
2098 let w = float (conf.winw - state.scrollw - 1) in
2099 if state.progress >= 0.0 && state.progress < 1.0
2100 then (
2101 GlDraw.color (0.3, 0.3, 0.3);
2102 let w1 = w *. state.progress in
2103 rect 0.0 w1;
2104 GlDraw.color (0.0, 0.0, 0.0);
2105 rect w1 (w-.w1)
2107 else (
2108 GlDraw.color (0.0, 0.0, 0.0);
2109 rect 0.0 w;
2112 GlDraw.color (1.0, 1.0, 1.0);
2113 drawstring fstate.fontsize
2114 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2116 let s =
2117 match state.mode with
2118 | Textentry ((prefix, text, _, _, _, _), _) ->
2119 let s =
2120 if len > 0
2121 then
2122 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2123 else
2124 Printf.sprintf "%s%s_" prefix text
2128 | _ -> state.text
2130 let s =
2131 if state.newerrmsgs
2132 then (
2133 if not (istextentry state.mode)
2134 then
2135 let s1 = "(press 'e' to review error messasges)" in
2136 if String.length s > 0 then s ^ " " ^ s1 else s1
2137 else s
2139 else s
2141 if String.length s > 0
2142 then drawstring s
2145 let gctiles () =
2146 let len = Queue.length state.tilelru in
2147 let layout = lazy (
2148 match state.throttle with
2149 | None ->
2150 if conf.preload
2151 then preloadlayout state.y
2152 else state.layout
2153 | Some (layout, _, _) ->
2154 layout
2155 ) in
2156 let rec loop qpos =
2157 if state.memused <= conf.memlimit
2158 then ()
2159 else (
2160 if qpos < len
2161 then
2162 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2163 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2164 let (_, pw, ph, _) = getpagedim n in
2166 gen = state.gen
2167 && colorspace = conf.colorspace
2168 && angle = conf.angle
2169 && pagew = pw
2170 && pageh = ph
2171 && (
2172 let x = col*conf.tilew
2173 and y = row*conf.tileh in
2174 tilevisible (Lazy.force_val layout) n x y
2176 then Queue.push lruitem state.tilelru
2177 else (
2178 freepbo p;
2179 wcmd "freetile %s" p;
2180 state.memused <- state.memused - s;
2181 state.uioh#infochanged Memused;
2182 Hashtbl.remove state.tilemap k;
2184 loop (qpos+1)
2187 loop 0
2190 let flushtiles () =
2191 Queue.iter (fun (k, p, s) ->
2192 wcmd "freetile %s" p;
2193 state.memused <- state.memused - s;
2194 state.uioh#infochanged Memused;
2195 Hashtbl.remove state.tilemap k;
2196 ) state.tilelru;
2197 Queue.clear state.tilelru;
2198 load state.layout;
2201 let logcurrently = function
2202 | Idle -> dolog "Idle"
2203 | Loading (l, gen) ->
2204 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2205 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2206 dolog
2207 "Tiling %d[%d,%d] page=%s cs=%s angle"
2208 l.pageno col row pageopaque
2209 (colorspace_to_string colorspace)
2211 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2212 angle gen conf.angle state.gen
2213 tilew tileh
2214 conf.tilew conf.tileh
2216 | Outlining _ ->
2217 dolog "outlining"
2220 let act cmds =
2221 (* dolog "%S" cmds; *)
2222 let op, args =
2223 let spacepos =
2224 try String.index cmds ' '
2225 with Not_found -> -1
2227 if spacepos = -1
2228 then cmds, ""
2229 else
2230 let l = String.length cmds in
2231 let op = String.sub cmds 0 spacepos in
2232 op, begin
2233 if l - spacepos < 2 then ""
2234 else String.sub cmds (spacepos+1) (l-spacepos-1)
2237 match op with
2238 | "clear" ->
2239 state.uioh#infochanged Pdim;
2240 state.pdims <- [];
2242 | "clearrects" ->
2243 state.rects <- state.rects1;
2244 G.postRedisplay "clearrects";
2246 | "continue" ->
2247 let n =
2248 try Scanf.sscanf args "%u" (fun n -> n)
2249 with exn ->
2250 dolog "error processing 'continue' %S: %s"
2251 cmds (Printexc.to_string exn);
2252 exit 1;
2254 state.pagecount <- n;
2255 begin match state.currently with
2256 | Outlining l ->
2257 state.currently <- Idle;
2258 state.outlines <- Array.of_list (List.rev l)
2259 | _ -> ()
2260 end;
2262 let cur, cmds = state.geomcmds in
2263 if String.length cur = 0
2264 then failwith "umpossible";
2266 begin match List.rev cmds with
2267 | [] ->
2268 state.geomcmds <- "", [];
2269 represent ();
2270 | (s, f) :: rest ->
2271 f ();
2272 state.geomcmds <- s, List.rev rest;
2273 end;
2274 if conf.maxwait = None
2275 then G.postRedisplay "continue";
2277 | "title" ->
2278 Wsi.settitle args
2280 | "msg" ->
2281 showtext ' ' args
2283 | "vmsg" ->
2284 if conf.verbose
2285 then showtext ' ' args
2287 | "progress" ->
2288 let progress, text =
2290 Scanf.sscanf args "%f %n"
2291 (fun f pos ->
2292 f, String.sub args pos (String.length args - pos))
2293 with exn ->
2294 dolog "error processing 'progress' %S: %s"
2295 cmds (Printexc.to_string exn);
2296 exit 1;
2298 state.text <- text;
2299 state.progress <- progress;
2300 G.postRedisplay "progress"
2302 | "firstmatch" ->
2303 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2305 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2306 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2307 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2308 with exn ->
2309 dolog "error processing 'firstmatch' %S: %s"
2310 cmds (Printexc.to_string exn);
2311 exit 1;
2313 let y = (getpagey pageno) + truncate y0 in
2314 addnav ();
2315 gotoy y;
2316 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2318 | "match" ->
2319 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2321 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2322 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2323 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2324 with exn ->
2325 dolog "error processing 'match' %S: %s"
2326 cmds (Printexc.to_string exn);
2327 exit 1;
2329 state.rects1 <-
2330 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2332 | "page" ->
2333 let pageopaque, t =
2335 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2336 with exn ->
2337 dolog "error processing 'page' %S: %s"
2338 cmds (Printexc.to_string exn);
2339 exit 1;
2341 begin match state.currently with
2342 | Loading (l, gen) ->
2343 vlog "page %d took %f sec" l.pageno t;
2344 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2345 begin match state.throttle with
2346 | None ->
2347 let preloadedpages =
2348 if conf.preload
2349 then preloadlayout state.y
2350 else state.layout
2352 let evict () =
2353 let module IntSet =
2354 Set.Make (struct type t = int let compare = (-) end) in
2355 let set =
2356 List.fold_left (fun s l -> IntSet.add l.pageno s)
2357 IntSet.empty preloadedpages
2359 let evictedpages =
2360 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2361 if not (IntSet.mem pageno set)
2362 then (
2363 wcmd "freepage %s" opaque;
2364 key :: accu
2366 else accu
2367 ) state.pagemap []
2369 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2371 evict ();
2372 state.currently <- Idle;
2373 if gen = state.gen
2374 then (
2375 tilepage l.pageno pageopaque state.layout;
2376 load state.layout;
2377 load preloadedpages;
2378 if pagevisible state.layout l.pageno
2379 && layoutready state.layout
2380 then G.postRedisplay "page";
2383 | Some (layout, _, _) ->
2384 state.currently <- Idle;
2385 tilepage l.pageno pageopaque layout;
2386 load state.layout
2387 end;
2389 | _ ->
2390 dolog "Inconsistent loading state";
2391 logcurrently state.currently;
2392 exit 1
2395 | "tile" ->
2396 let (x, y, opaque, size, t) =
2398 Scanf.sscanf args "%u %u %s %u %f"
2399 (fun x y p size t -> (x, y, p, size, t))
2400 with exn ->
2401 dolog "error processing 'tile' %S: %s"
2402 cmds (Printexc.to_string exn);
2403 exit 1;
2405 begin match state.currently with
2406 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2407 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2409 unmappbo opaque;
2410 if tilew != conf.tilew || tileh != conf.tileh
2411 then (
2412 wcmd "freetile %s" opaque;
2413 state.currently <- Idle;
2414 load state.layout;
2416 else (
2417 puttileopaque l col row gen cs angle opaque size t;
2418 state.memused <- state.memused + size;
2419 state.uioh#infochanged Memused;
2420 gctiles ();
2421 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2422 opaque, size) state.tilelru;
2424 let layout =
2425 match state.throttle with
2426 | None -> state.layout
2427 | Some (layout, _, _) -> layout
2430 state.currently <- Idle;
2431 if gen = state.gen
2432 && conf.colorspace = cs
2433 && conf.angle = angle
2434 && tilevisible layout l.pageno x y
2435 then conttiling l.pageno pageopaque;
2437 begin match state.throttle with
2438 | None ->
2439 state.wthack <- false;
2440 preload state.layout;
2441 if gen = state.gen
2442 && conf.colorspace = cs
2443 && conf.angle = angle
2444 && tilevisible state.layout l.pageno x y
2445 then G.postRedisplay "tile nothrottle";
2447 | Some (layout, y, _) ->
2448 let ready = layoutready layout in
2449 if ready
2450 then (
2451 state.wthack <- false;
2452 state.y <- y;
2453 state.layout <- layout;
2454 state.throttle <- None;
2455 G.postRedisplay "throttle";
2457 else load layout;
2458 end;
2461 | _ ->
2462 dolog "Inconsistent tiling state";
2463 logcurrently state.currently;
2464 exit 1
2467 | "pdim" ->
2468 let pdim =
2470 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2471 with exn ->
2472 dolog "error processing 'pdim' %S: %s"
2473 cmds (Printexc.to_string exn);
2474 exit 1;
2476 state.uioh#infochanged Pdim;
2477 state.pdims <- pdim :: state.pdims
2479 | "o" ->
2480 let (l, n, t, h, pos) =
2482 Scanf.sscanf args "%u %u %d %u %n"
2483 (fun l n t h pos -> l, n, t, h, pos)
2484 with exn ->
2485 dolog "error processing 'o' %S: %s"
2486 cmds (Printexc.to_string exn);
2487 exit 1;
2489 let s = String.sub args pos (String.length args - pos) in
2490 let outline = (s, l, (n, float t /. float h, 0.0)) in
2491 begin match state.currently with
2492 | Outlining outlines ->
2493 state.currently <- Outlining (outline :: outlines)
2494 | Idle ->
2495 state.currently <- Outlining [outline]
2496 | currently ->
2497 dolog "invalid outlining state";
2498 logcurrently currently
2501 | "info" ->
2502 state.docinfo <- (1, args) :: state.docinfo
2504 | "infoend" ->
2505 state.uioh#infochanged Docinfo;
2506 state.docinfo <- List.rev state.docinfo
2508 | _ ->
2509 dolog "unknown cmd `%S'" cmds
2512 let onhist cb =
2513 let rc = cb.rc in
2514 let action = function
2515 | HCprev -> cbget cb ~-1
2516 | HCnext -> cbget cb 1
2517 | HCfirst -> cbget cb ~-(cb.rc)
2518 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2519 and cancel () = cb.rc <- rc
2520 in (action, cancel)
2523 let search pattern forward =
2524 if String.length pattern > 0
2525 then
2526 let pn, py =
2527 match state.layout with
2528 | [] -> 0, 0
2529 | l :: _ ->
2530 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2532 wcmd "search %d %d %d %d,%s\000"
2533 (btod conf.icase) pn py (btod forward) pattern;
2536 let intentry text key =
2537 let c =
2538 if key >= 32 && key < 127
2539 then Char.chr key
2540 else '\000'
2542 match c with
2543 | '0' .. '9' ->
2544 let text = addchar text c in
2545 TEcont text
2547 | _ ->
2548 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2549 TEcont text
2552 let linknentry text key =
2553 let c =
2554 if key >= 32 && key < 127
2555 then Char.chr key
2556 else '\000'
2558 match c with
2559 | 'a' .. 'z' ->
2560 let text = addchar text c in
2561 TEcont text
2563 | _ ->
2564 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2565 TEcont text
2568 let linkndone f s =
2569 if String.length s > 0
2570 then (
2571 let n =
2572 let l = String.length s in
2573 let rec loop pos n = if pos = l then n else
2574 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2575 loop (pos+1) (n*26 + m)
2576 in loop 0 0
2578 let rec loop n = function
2579 | [] -> ()
2580 | l :: rest ->
2581 match getopaque l.pageno with
2582 | None -> loop n rest
2583 | Some opaque ->
2584 let m = getlinkcount opaque in
2585 if n < m
2586 then (
2587 let under = getlink opaque n in
2588 f under
2590 else loop (n-m) rest
2592 loop n state.layout;
2596 let textentry text key =
2597 if key land 0xff00 = 0xff00
2598 then TEcont text
2599 else TEcont (text ^ Wsi.toutf8 key)
2602 let reqlayout angle proportional =
2603 match state.throttle with
2604 | None ->
2605 if nogeomcmds state.geomcmds
2606 then state.anchor <- getanchor ();
2607 conf.angle <- angle mod 360;
2608 if conf.angle != 0
2609 then (
2610 match state.mode with
2611 | LinkNav _ -> state.mode <- View
2612 | _ -> ()
2614 conf.proportional <- proportional;
2615 invalidate "reqlayout"
2616 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2617 | _ -> ()
2620 let settrim trimmargins trimfuzz =
2621 if nogeomcmds state.geomcmds
2622 then state.anchor <- getanchor ();
2623 conf.trimmargins <- trimmargins;
2624 conf.trimfuzz <- trimfuzz;
2625 let x0, y0, x1, y1 = trimfuzz in
2626 invalidate "settrim"
2627 (fun () ->
2628 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2629 flushpages ();
2632 let setzoom zoom =
2633 match state.throttle with
2634 | None ->
2635 let zoom = max 0.01 zoom in
2636 if zoom <> conf.zoom
2637 then (
2638 state.prevzoom <- conf.zoom;
2639 conf.zoom <- zoom;
2640 reshape conf.winw conf.winh;
2641 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2644 | Some (layout, y, started) ->
2645 let time =
2646 match conf.maxwait with
2647 | None -> 0.0
2648 | Some t -> t
2650 let dt = now () -. started in
2651 if dt > time
2652 then (
2653 state.y <- y;
2654 load layout;
2658 let setcolumns mode columns coverA coverB =
2659 state.prevcolumns <- Some (conf.columns, conf.zoom);
2660 if columns < 0
2661 then (
2662 if isbirdseye mode
2663 then showtext '!' "split mode doesn't work in bird's eye"
2664 else (
2665 conf.columns <- Csplit (-columns, [||]);
2666 state.x <- 0;
2667 conf.zoom <- 1.0;
2670 else (
2671 if columns < 2
2672 then (
2673 conf.columns <- Csingle [||];
2674 state.x <- 0;
2675 setzoom 1.0;
2677 else (
2678 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2679 conf.zoom <- 1.0;
2682 reshape conf.winw conf.winh;
2685 let enterbirdseye () =
2686 let zoom = float conf.thumbw /. float conf.winw in
2687 let birdseyepageno =
2688 let cy = conf.winh / 2 in
2689 let fold = function
2690 | [] -> 0
2691 | l :: rest ->
2692 let rec fold best = function
2693 | [] -> best.pageno
2694 | l :: rest ->
2695 let d = cy - (l.pagedispy + l.pagevh/2)
2696 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2697 if abs d < abs dbest
2698 then fold l rest
2699 else best.pageno
2700 in fold l rest
2702 fold state.layout
2704 state.mode <- Birdseye (
2705 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2707 conf.zoom <- zoom;
2708 conf.presentation <- false;
2709 conf.interpagespace <- 10;
2710 conf.hlinks <- false;
2711 state.x <- 0;
2712 state.mstate <- Mnone;
2713 conf.maxwait <- None;
2714 conf.columns <- (
2715 match conf.beyecolumns with
2716 | Some c ->
2717 conf.zoom <- 1.0;
2718 Cmulti ((c, 0, 0), [||])
2719 | None -> Csingle [||]
2721 Wsi.setcursor Wsi.CURSOR_INHERIT;
2722 if conf.verbose
2723 then
2724 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2725 (100.0*.zoom)
2726 else
2727 state.text <- ""
2729 reshape conf.winw conf.winh;
2732 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2733 state.mode <- View;
2734 conf.zoom <- c.zoom;
2735 conf.presentation <- c.presentation;
2736 conf.interpagespace <- c.interpagespace;
2737 conf.maxwait <- c.maxwait;
2738 conf.hlinks <- c.hlinks;
2739 conf.beyecolumns <- (
2740 match conf.columns with
2741 | Cmulti ((c, _, _), _) -> Some c
2742 | Csingle _ -> None
2743 | Csplit _ -> failwith "leaving bird's eye split mode"
2745 conf.columns <- (
2746 match c.columns with
2747 | Cmulti (c, _) -> Cmulti (c, [||])
2748 | Csingle _ -> Csingle [||]
2749 | Csplit (c, _) -> Csplit (c, [||])
2751 state.x <- leftx;
2752 if conf.verbose
2753 then
2754 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2755 (100.0*.conf.zoom)
2757 reshape conf.winw conf.winh;
2758 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2761 let togglebirdseye () =
2762 match state.mode with
2763 | Birdseye vals -> leavebirdseye vals true
2764 | View -> enterbirdseye ()
2765 | _ -> ()
2768 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2769 let pageno = max 0 (pageno - incr) in
2770 let rec loop = function
2771 | [] -> gotopage1 pageno 0
2772 | l :: _ when l.pageno = pageno ->
2773 if l.pagedispy >= 0 && l.pagey = 0
2774 then G.postRedisplay "upbirdseye"
2775 else gotopage1 pageno 0
2776 | _ :: rest -> loop rest
2778 loop state.layout;
2779 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2782 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2783 let pageno = min (state.pagecount - 1) (pageno + incr) in
2784 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2785 let rec loop = function
2786 | [] ->
2787 let y, h = getpageyh pageno in
2788 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2789 gotoy (clamp dy)
2790 | l :: _ when l.pageno = pageno ->
2791 if l.pagevh != l.pageh
2792 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2793 else G.postRedisplay "downbirdseye"
2794 | _ :: rest -> loop rest
2796 loop state.layout
2799 let optentry mode _ key =
2800 let btos b = if b then "on" else "off" in
2801 if key >= 32 && key < 127
2802 then
2803 let c = Char.chr key in
2804 match c with
2805 | 's' ->
2806 let ondone s =
2807 try conf.scrollstep <- int_of_string s with exc ->
2808 state.text <- Printf.sprintf "bad integer `%s': %s"
2809 s (Printexc.to_string exc)
2811 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2813 | 'A' ->
2814 let ondone s =
2816 conf.autoscrollstep <- int_of_string s;
2817 if state.autoscroll <> None
2818 then state.autoscroll <- Some conf.autoscrollstep
2819 with exc ->
2820 state.text <- Printf.sprintf "bad integer `%s': %s"
2821 s (Printexc.to_string exc)
2823 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2825 | 'C' ->
2826 let ondone s =
2828 let n, a, b = multicolumns_of_string s in
2829 setcolumns mode n a b;
2830 with exc ->
2831 state.text <- Printf.sprintf "bad columns `%s': %s"
2832 s (Printexc.to_string exc)
2834 TEswitch ("columns: ", "", None, textentry, ondone, true)
2836 | 'Z' ->
2837 let ondone s =
2839 let zoom = float (int_of_string s) /. 100.0 in
2840 setzoom zoom
2841 with exc ->
2842 state.text <- Printf.sprintf "bad integer `%s': %s"
2843 s (Printexc.to_string exc)
2845 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2847 | 't' ->
2848 let ondone s =
2850 conf.thumbw <- bound (int_of_string s) 2 4096;
2851 state.text <-
2852 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2853 begin match mode with
2854 | Birdseye beye ->
2855 leavebirdseye beye false;
2856 enterbirdseye ();
2857 | _ -> ();
2859 with exc ->
2860 state.text <- Printf.sprintf "bad integer `%s': %s"
2861 s (Printexc.to_string exc)
2863 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2865 | 'R' ->
2866 let ondone s =
2867 match try
2868 Some (int_of_string s)
2869 with exc ->
2870 state.text <- Printf.sprintf "bad integer `%s': %s"
2871 s (Printexc.to_string exc);
2872 None
2873 with
2874 | Some angle -> reqlayout angle conf.proportional
2875 | None -> ()
2877 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2879 | 'i' ->
2880 conf.icase <- not conf.icase;
2881 TEdone ("case insensitive search " ^ (btos conf.icase))
2883 | 'p' ->
2884 conf.preload <- not conf.preload;
2885 gotoy state.y;
2886 TEdone ("preload " ^ (btos conf.preload))
2888 | 'v' ->
2889 conf.verbose <- not conf.verbose;
2890 TEdone ("verbose " ^ (btos conf.verbose))
2892 | 'd' ->
2893 conf.debug <- not conf.debug;
2894 TEdone ("debug " ^ (btos conf.debug))
2896 | 'h' ->
2897 conf.maxhfit <- not conf.maxhfit;
2898 state.maxy <- calcheight ();
2899 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2901 | 'c' ->
2902 conf.crophack <- not conf.crophack;
2903 TEdone ("crophack " ^ btos conf.crophack)
2905 | 'a' ->
2906 let s =
2907 match conf.maxwait with
2908 | None ->
2909 conf.maxwait <- Some infinity;
2910 "always wait for page to complete"
2911 | Some _ ->
2912 conf.maxwait <- None;
2913 "show placeholder if page is not ready"
2915 TEdone s
2917 | 'f' ->
2918 conf.underinfo <- not conf.underinfo;
2919 TEdone ("underinfo " ^ btos conf.underinfo)
2921 | 'P' ->
2922 conf.savebmarks <- not conf.savebmarks;
2923 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2925 | 'S' ->
2926 let ondone s =
2928 let pageno, py =
2929 match state.layout with
2930 | [] -> 0, 0
2931 | l :: _ ->
2932 l.pageno, l.pagey
2934 conf.interpagespace <- int_of_string s;
2935 docolumns conf.columns;
2936 state.maxy <- calcheight ();
2937 let y = getpagey pageno in
2938 gotoy (y + py)
2939 with exc ->
2940 state.text <- Printf.sprintf "bad integer `%s': %s"
2941 s (Printexc.to_string exc)
2943 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2945 | 'l' ->
2946 reqlayout conf.angle (not conf.proportional);
2947 TEdone ("proportional display " ^ btos conf.proportional)
2949 | 'T' ->
2950 settrim (not conf.trimmargins) conf.trimfuzz;
2951 TEdone ("trim margins " ^ btos conf.trimmargins)
2953 | 'I' ->
2954 conf.invert <- not conf.invert;
2955 TEdone ("invert colors " ^ btos conf.invert)
2957 | 'x' ->
2958 let ondone s =
2959 cbput state.hists.sel s;
2960 conf.selcmd <- s;
2962 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2963 textentry, ondone, true)
2965 | _ ->
2966 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2967 TEstop
2968 else
2969 TEcont state.text
2972 class type lvsource = object
2973 method getitemcount : int
2974 method getitem : int -> (string * int)
2975 method hasaction : int -> bool
2976 method exit :
2977 uioh:uioh ->
2978 cancel:bool ->
2979 active:int ->
2980 first:int ->
2981 pan:int ->
2982 qsearch:string ->
2983 uioh option
2984 method getactive : int
2985 method getfirst : int
2986 method getqsearch : string
2987 method setqsearch : string -> unit
2988 method getpan : int
2989 end;;
2991 class virtual lvsourcebase = object
2992 val mutable m_active = 0
2993 val mutable m_first = 0
2994 val mutable m_qsearch = ""
2995 val mutable m_pan = 0
2996 method getactive = m_active
2997 method getfirst = m_first
2998 method getqsearch = m_qsearch
2999 method getpan = m_pan
3000 method setqsearch s = m_qsearch <- s
3001 end;;
3003 let withoutlastutf8 s =
3004 let len = String.length s in
3005 if len = 0
3006 then s
3007 else
3008 let rec find pos =
3009 if pos = 0
3010 then pos
3011 else
3012 let b = Char.code s.[pos] in
3013 if b land 0b11000000 = 0b11000000
3014 then pos
3015 else find (pos-1)
3017 let first =
3018 if Char.code s.[len-1] land 0x80 = 0
3019 then len-1
3020 else find (len-1)
3022 String.sub s 0 first;
3025 let textentrykeyboard
3026 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3027 let key =
3028 if key >= 0xffb0 && key <= 0xffb9
3029 then key - 0xffb0 + 48 else key
3031 let enttext te =
3032 state.mode <- Textentry (te, onleave);
3033 state.text <- "";
3034 enttext ();
3035 G.postRedisplay "textentrykeyboard enttext";
3037 let histaction cmd =
3038 match opthist with
3039 | None -> ()
3040 | Some (action, _) ->
3041 state.mode <- Textentry (
3042 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3044 G.postRedisplay "textentry histaction"
3046 match key with
3047 | 0xff08 -> (* backspace *)
3048 let s = withoutlastutf8 text in
3049 let len = String.length s in
3050 if cancelonempty && len = 0
3051 then (
3052 onleave Cancel;
3053 G.postRedisplay "textentrykeyboard after cancel";
3055 else (
3056 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3059 | 0xff0d | 0xff8d -> (* (kp) enter *)
3060 ondone text;
3061 onleave Confirm;
3062 G.postRedisplay "textentrykeyboard after confirm"
3064 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3065 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3066 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3067 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3069 | 0xff1b -> (* escape*)
3070 if String.length text = 0
3071 then (
3072 begin match opthist with
3073 | None -> ()
3074 | Some (_, onhistcancel) -> onhistcancel ()
3075 end;
3076 onleave Cancel;
3077 state.text <- "";
3078 G.postRedisplay "textentrykeyboard after cancel2"
3080 else (
3081 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3084 | 0xff9f | 0xffff -> () (* delete *)
3086 | _ when key != 0
3087 && key land 0xff00 != 0xff00 (* keyboard *)
3088 && key land 0xfe00 != 0xfe00 (* xkb *)
3089 && key land 0xfd00 != 0xfd00 (* 3270 *)
3091 begin match onkey text key with
3092 | TEdone text ->
3093 ondone text;
3094 onleave Confirm;
3095 G.postRedisplay "textentrykeyboard after confirm2";
3097 | TEcont text ->
3098 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3100 | TEstop ->
3101 onleave Cancel;
3102 G.postRedisplay "textentrykeyboard after cancel3"
3104 | TEswitch te ->
3105 state.mode <- Textentry (te, onleave);
3106 G.postRedisplay "textentrykeyboard switch";
3107 end;
3109 | _ ->
3110 vlog "unhandled key %s" (Wsi.keyname key)
3113 let firstof first active =
3114 if first > active || abs (first - active) > fstate.maxrows - 1
3115 then max 0 (active - (fstate.maxrows/2))
3116 else first
3119 let calcfirst first active =
3120 if active > first
3121 then
3122 let rows = active - first in
3123 if rows > fstate.maxrows then active - fstate.maxrows else first
3124 else active
3127 let scrollph y maxy =
3128 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3129 let sh = float conf.winh /. sh in
3130 let sh = max sh (float conf.scrollh) in
3132 let percent =
3133 if y = state.maxy
3134 then 1.0
3135 else float y /. float maxy
3137 let position = (float conf.winh -. sh) *. percent in
3139 let position =
3140 if position +. sh > float conf.winh
3141 then float conf.winh -. sh
3142 else position
3144 position, sh;
3147 let coe s = (s :> uioh);;
3149 class listview ~(source:lvsource) ~trusted ~modehash =
3150 object (self)
3151 val m_pan = source#getpan
3152 val m_first = source#getfirst
3153 val m_active = source#getactive
3154 val m_qsearch = source#getqsearch
3155 val m_prev_uioh = state.uioh
3157 method private elemunder y =
3158 let n = y / (fstate.fontsize+1) in
3159 if m_first + n < source#getitemcount
3160 then (
3161 if source#hasaction (m_first + n)
3162 then Some (m_first + n)
3163 else None
3165 else None
3167 method display =
3168 Gl.enable `blend;
3169 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3170 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3171 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3172 GlDraw.color (1., 1., 1.);
3173 Gl.enable `texture_2d;
3174 let fs = fstate.fontsize in
3175 let nfs = fs + 1 in
3176 let ww = fstate.wwidth in
3177 let tabw = 30.0*.ww in
3178 let itemcount = source#getitemcount in
3179 let rec loop row =
3180 if (row - m_first) > fstate.maxrows
3181 then ()
3182 else (
3183 if row >= 0 && row < itemcount
3184 then (
3185 let (s, level) = source#getitem row in
3186 let y = (row - m_first) * nfs in
3187 let x = 5.0 +. float (level + m_pan) *. ww in
3188 if row = m_active
3189 then (
3190 Gl.disable `texture_2d;
3191 GlDraw.polygon_mode `both `line;
3192 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3193 GlDraw.rect (1., float (y + 1))
3194 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3195 GlDraw.polygon_mode `both `fill;
3196 GlDraw.color (1., 1., 1.);
3197 Gl.enable `texture_2d;
3200 let drawtabularstring s =
3201 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3202 if trusted
3203 then
3204 let tabpos = try String.index s '\t' with Not_found -> -1 in
3205 if tabpos > 0
3206 then
3207 let len = String.length s - tabpos - 1 in
3208 let s1 = String.sub s 0 tabpos
3209 and s2 = String.sub s (tabpos + 1) len in
3210 let nx = drawstr x s1 in
3211 let sw = nx -. x in
3212 let x = x +. (max tabw sw) in
3213 drawstr x s2
3214 else
3215 drawstr x s
3216 else
3217 drawstr x s
3219 let _ = drawtabularstring s in
3220 loop (row+1)
3224 loop m_first;
3225 Gl.disable `blend;
3226 Gl.disable `texture_2d;
3228 method updownlevel incr =
3229 let len = source#getitemcount in
3230 let curlevel =
3231 if m_active >= 0 && m_active < len
3232 then snd (source#getitem m_active)
3233 else -1
3235 let rec flow i =
3236 if i = len then i-1 else if i = -1 then 0 else
3237 let _, l = source#getitem i in
3238 if l != curlevel then i else flow (i+incr)
3240 let active = flow m_active in
3241 let first = calcfirst m_first active in
3242 G.postRedisplay "outline updownlevel";
3243 {< m_active = active; m_first = first >}
3245 method private key1 key mask =
3246 let set1 active first qsearch =
3247 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3249 let search active pattern incr =
3250 let dosearch re =
3251 let rec loop n =
3252 if n >= 0 && n < source#getitemcount
3253 then (
3254 let s, _ = source#getitem n in
3256 (try ignore (Str.search_forward re s 0); true
3257 with Not_found -> false)
3258 then Some n
3259 else loop (n + incr)
3261 else None
3263 loop active
3266 let re = Str.regexp_case_fold pattern in
3267 dosearch re
3268 with Failure s ->
3269 state.text <- s;
3270 None
3272 let itemcount = source#getitemcount in
3273 let find start incr =
3274 let rec find i =
3275 if i = -1 || i = itemcount
3276 then -1
3277 else (
3278 if source#hasaction i
3279 then i
3280 else find (i + incr)
3283 find start
3285 let set active first =
3286 let first = bound first 0 (itemcount - fstate.maxrows) in
3287 state.text <- "";
3288 coe {< m_active = active; m_first = first >}
3290 let navigate incr =
3291 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3292 let active, first =
3293 let incr1 = if incr > 0 then 1 else -1 in
3294 if isvisible m_first m_active
3295 then
3296 let next =
3297 let next = m_active + incr in
3298 let next =
3299 if next < 0 || next >= itemcount
3300 then -1
3301 else find next incr1
3303 if next = -1 || abs (m_active - next) > fstate.maxrows
3304 then -1
3305 else next
3307 if next = -1
3308 then
3309 let first = m_first + incr in
3310 let first = bound first 0 (itemcount - 1) in
3311 let next =
3312 let next = m_active + incr in
3313 let next = bound next 0 (itemcount - 1) in
3314 find next ~-incr1
3316 let active = if next = -1 then m_active else next in
3317 active, first
3318 else
3319 let first = min next m_first in
3320 let first =
3321 if abs (next - first) > fstate.maxrows
3322 then first + incr
3323 else first
3325 next, first
3326 else
3327 let first = m_first + incr in
3328 let first = bound first 0 (itemcount - 1) in
3329 let active =
3330 let next = m_active + incr in
3331 let next = bound next 0 (itemcount - 1) in
3332 let next = find next incr1 in
3333 let active =
3334 if next = -1 || abs (m_active - first) > fstate.maxrows
3335 then (
3336 let active = if m_active = -1 then next else m_active in
3337 active
3339 else next
3341 if isvisible first active
3342 then active
3343 else -1
3345 active, first
3347 G.postRedisplay "listview navigate";
3348 set active first;
3350 match key with
3351 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3352 let incr = if key = 0x72 then -1 else 1 in
3353 let active, first =
3354 match search (m_active + incr) m_qsearch incr with
3355 | None ->
3356 state.text <- m_qsearch ^ " [not found]";
3357 m_active, m_first
3358 | Some active ->
3359 state.text <- m_qsearch;
3360 active, firstof m_first active
3362 G.postRedisplay "listview ctrl-r/s";
3363 set1 active first m_qsearch;
3365 | 0xff08 -> (* backspace *)
3366 if String.length m_qsearch = 0
3367 then coe self
3368 else (
3369 let qsearch = withoutlastutf8 m_qsearch in
3370 let len = String.length qsearch in
3371 if len = 0
3372 then (
3373 state.text <- "";
3374 G.postRedisplay "listview empty qsearch";
3375 set1 m_active m_first "";
3377 else
3378 let active, first =
3379 match search m_active qsearch ~-1 with
3380 | None ->
3381 state.text <- qsearch ^ " [not found]";
3382 m_active, m_first
3383 | Some active ->
3384 state.text <- qsearch;
3385 active, firstof m_first active
3387 G.postRedisplay "listview backspace qsearch";
3388 set1 active first qsearch
3391 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3392 let pattern = m_qsearch ^ Wsi.toutf8 key in
3393 let active, first =
3394 match search m_active pattern 1 with
3395 | None ->
3396 state.text <- pattern ^ " [not found]";
3397 m_active, m_first
3398 | Some active ->
3399 state.text <- pattern;
3400 active, firstof m_first active
3402 G.postRedisplay "listview qsearch add";
3403 set1 active first pattern;
3405 | 0xff1b -> (* escape *)
3406 state.text <- "";
3407 if String.length m_qsearch = 0
3408 then (
3409 G.postRedisplay "list view escape";
3410 begin
3411 match
3412 source#exit (coe self) true m_active m_first m_pan m_qsearch
3413 with
3414 | None -> m_prev_uioh
3415 | Some uioh -> uioh
3418 else (
3419 G.postRedisplay "list view kill qsearch";
3420 source#setqsearch "";
3421 coe {< m_qsearch = "" >}
3424 | 0xff0d | 0xff8d -> (* (kp) enter *)
3425 state.text <- "";
3426 let self = {< m_qsearch = "" >} in
3427 source#setqsearch "";
3428 let opt =
3429 G.postRedisplay "listview enter";
3430 if m_active >= 0 && m_active < source#getitemcount
3431 then (
3432 source#exit (coe self) false m_active m_first m_pan "";
3434 else (
3435 source#exit (coe self) true m_active m_first m_pan "";
3438 begin match opt with
3439 | None -> m_prev_uioh
3440 | Some uioh -> uioh
3443 | 0xff9f | 0xffff -> (* (kp) delete *)
3444 coe self
3446 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3447 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3448 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3449 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3451 | 0xff53 | 0xff98 -> (* (kp) right *)
3452 state.text <- "";
3453 G.postRedisplay "listview right";
3454 coe {< m_pan = m_pan - 1 >}
3456 | 0xff51 | 0xff96 -> (* (kp) left *)
3457 state.text <- "";
3458 G.postRedisplay "listview left";
3459 coe {< m_pan = m_pan + 1 >}
3461 | 0xff50 | 0xff95 -> (* (kp) home *)
3462 let active = find 0 1 in
3463 G.postRedisplay "listview home";
3464 set active 0;
3466 | 0xff57 | 0xff9c -> (* (kp) end *)
3467 let first = max 0 (itemcount - fstate.maxrows) in
3468 let active = find (itemcount - 1) ~-1 in
3469 G.postRedisplay "listview end";
3470 set active first;
3472 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3473 coe self
3475 | _ ->
3476 dolog "listview unknown key %#x" key; coe self
3478 method key key mask =
3479 match state.mode with
3480 | Textentry te -> textentrykeyboard key mask te; coe self
3481 | _ -> self#key1 key mask
3483 method button button down x y _ =
3484 let opt =
3485 match button with
3486 | 1 when x > conf.winw - conf.scrollbw ->
3487 G.postRedisplay "listview scroll";
3488 if down
3489 then
3490 let _, position, sh = self#scrollph in
3491 if y > truncate position && y < truncate (position +. sh)
3492 then (
3493 state.mstate <- Mscrolly;
3494 Some (coe self)
3496 else
3497 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3498 let first = truncate (s *. float source#getitemcount) in
3499 let first = min source#getitemcount first in
3500 Some (coe {< m_first = first; m_active = first >})
3501 else (
3502 state.mstate <- Mnone;
3503 Some (coe self);
3505 | 1 when not down ->
3506 begin match self#elemunder y with
3507 | Some n ->
3508 G.postRedisplay "listview click";
3509 source#exit
3510 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3511 | _ ->
3512 Some (coe self)
3514 | n when (n == 4 || n == 5) && not down ->
3515 let len = source#getitemcount in
3516 let first =
3517 if n = 5 && m_first + fstate.maxrows >= len
3518 then
3519 m_first
3520 else
3521 let first = m_first + (if n == 4 then -1 else 1) in
3522 bound first 0 (len - 1)
3524 G.postRedisplay "listview wheel";
3525 Some (coe {< m_first = first >})
3526 | n when (n = 6 || n = 7) && not down ->
3527 let inc = m_first + (if n = 7 then -1 else 1) in
3528 G.postRedisplay "listview hwheel";
3529 Some (coe {< m_pan = m_pan + inc >})
3530 | _ ->
3531 Some (coe self)
3533 match opt with
3534 | None -> m_prev_uioh
3535 | Some uioh -> uioh
3537 method motion _ y =
3538 match state.mstate with
3539 | Mscrolly ->
3540 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3541 let first = truncate (s *. float source#getitemcount) in
3542 let first = min source#getitemcount first in
3543 G.postRedisplay "listview motion";
3544 coe {< m_first = first; m_active = first >}
3545 | _ -> coe self
3547 method pmotion x y =
3548 if x < conf.winw - conf.scrollbw
3549 then
3550 let n =
3551 match self#elemunder y with
3552 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3553 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3555 let o =
3556 if n != m_active
3557 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3558 else self
3560 coe o
3561 else (
3562 Wsi.setcursor Wsi.CURSOR_INHERIT;
3563 coe self
3566 method infochanged _ = ()
3568 method scrollpw = (0, 0.0, 0.0)
3569 method scrollph =
3570 let nfs = fstate.fontsize + 1 in
3571 let y = m_first * nfs in
3572 let itemcount = source#getitemcount in
3573 let maxi = max 0 (itemcount - fstate.maxrows) in
3574 let maxy = maxi * nfs in
3575 let p, h = scrollph y maxy in
3576 conf.scrollbw, p, h
3578 method modehash = modehash
3579 end;;
3581 class outlinelistview ~source =
3582 object (self)
3583 inherit listview
3584 ~source:(source :> lvsource)
3585 ~trusted:false
3586 ~modehash:(findkeyhash conf "outline")
3587 as super
3589 method key key mask =
3590 let calcfirst first active =
3591 if active > first
3592 then
3593 let rows = active - first in
3594 let maxrows =
3595 if String.length state.text = 0
3596 then fstate.maxrows
3597 else fstate.maxrows - 2
3599 if rows > maxrows then active - maxrows else first
3600 else active
3602 let navigate incr =
3603 let active = m_active + incr in
3604 let active = bound active 0 (source#getitemcount - 1) in
3605 let first = calcfirst m_first active in
3606 G.postRedisplay "outline navigate";
3607 coe {< m_active = active; m_first = first >}
3609 let ctrl = Wsi.withctrl mask in
3610 match key with
3611 | 110 when ctrl -> (* ctrl-n *)
3612 source#narrow m_qsearch;
3613 G.postRedisplay "outline ctrl-n";
3614 coe {< m_first = 0; m_active = 0 >}
3616 | 117 when ctrl -> (* ctrl-u *)
3617 source#denarrow;
3618 G.postRedisplay "outline ctrl-u";
3619 state.text <- "";
3620 coe {< m_first = 0; m_active = 0 >}
3622 | 108 when ctrl -> (* ctrl-l *)
3623 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3624 G.postRedisplay "outline ctrl-l";
3625 coe {< m_first = first >}
3627 | 0xff9f | 0xffff -> (* (kp) delete *)
3628 source#remove m_active;
3629 G.postRedisplay "outline delete";
3630 let active = max 0 (m_active-1) in
3631 coe {< m_first = firstof m_first active;
3632 m_active = active >}
3634 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3635 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3636 | 0xff55 | 0xff9a -> (* (kp) prior *)
3637 navigate ~-(fstate.maxrows)
3638 | 0xff56 | 0xff9b -> (* (kp) next *)
3639 navigate fstate.maxrows
3641 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3642 let o =
3643 if ctrl
3644 then (
3645 G.postRedisplay "outline ctrl right";
3646 {< m_pan = m_pan + 1 >}
3648 else self#updownlevel 1
3650 coe o
3652 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3653 let o =
3654 if ctrl
3655 then (
3656 G.postRedisplay "outline ctrl left";
3657 {< m_pan = m_pan - 1 >}
3659 else self#updownlevel ~-1
3661 coe o
3663 | 0xff50 | 0xff95 -> (* (kp) home *)
3664 G.postRedisplay "outline home";
3665 coe {< m_first = 0; m_active = 0 >}
3667 | 0xff57 | 0xff9c -> (* (kp) end *)
3668 let active = source#getitemcount - 1 in
3669 let first = max 0 (active - fstate.maxrows) in
3670 G.postRedisplay "outline end";
3671 coe {< m_active = active; m_first = first >}
3673 | _ -> super#key key mask
3676 let outlinesource usebookmarks =
3677 let empty = [||] in
3678 (object
3679 inherit lvsourcebase
3680 val mutable m_items = empty
3681 val mutable m_orig_items = empty
3682 val mutable m_prev_items = empty
3683 val mutable m_narrow_pattern = ""
3684 val mutable m_hadremovals = false
3686 method getitemcount =
3687 Array.length m_items + (if m_hadremovals then 1 else 0)
3689 method getitem n =
3690 if n == Array.length m_items && m_hadremovals
3691 then
3692 ("[Confirm removal]", 0)
3693 else
3694 let s, n, _ = m_items.(n) in
3695 (s, n)
3697 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3698 ignore (uioh, first, qsearch);
3699 let confrimremoval = m_hadremovals && active = Array.length m_items in
3700 let items =
3701 if String.length m_narrow_pattern = 0
3702 then m_orig_items
3703 else m_items
3705 if not cancel
3706 then (
3707 if not confrimremoval
3708 then(
3709 let _, _, anchor = m_items.(active) in
3710 gotoghyll (getanchory anchor);
3711 m_items <- items;
3713 else (
3714 state.bookmarks <- Array.to_list m_items;
3715 m_orig_items <- m_items;
3718 else m_items <- items;
3719 m_pan <- pan;
3720 None
3722 method hasaction _ = true
3724 method greetmsg =
3725 if Array.length m_items != Array.length m_orig_items
3726 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3727 else ""
3729 method narrow pattern =
3730 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3731 match reopt with
3732 | None -> ()
3733 | Some re ->
3734 let rec loop accu n =
3735 if n = -1
3736 then (
3737 m_narrow_pattern <- pattern;
3738 m_items <- Array.of_list accu
3740 else
3741 let (s, _, _) as o = m_items.(n) in
3742 let accu =
3743 if (try ignore (Str.search_forward re s 0); true
3744 with Not_found -> false)
3745 then o :: accu
3746 else accu
3748 loop accu (n-1)
3750 loop [] (Array.length m_items - 1)
3752 method denarrow =
3753 m_orig_items <- (
3754 if usebookmarks
3755 then Array.of_list state.bookmarks
3756 else state.outlines
3758 m_items <- m_orig_items
3760 method remove m =
3761 if usebookmarks
3762 then
3763 if m >= 0 && m < Array.length m_items
3764 then (
3765 m_hadremovals <- true;
3766 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3767 let n = if n >= m then n+1 else n in
3768 m_items.(n)
3772 method reset anchor items =
3773 m_hadremovals <- false;
3774 if m_orig_items == empty || m_prev_items != items
3775 then (
3776 m_orig_items <- items;
3777 if String.length m_narrow_pattern = 0
3778 then m_items <- items;
3780 m_prev_items <- items;
3781 let rely = getanchory anchor in
3782 let active =
3783 let rec loop n best bestd =
3784 if n = Array.length m_items
3785 then best
3786 else
3787 let (_, _, anchor) = m_items.(n) in
3788 let orely = getanchory anchor in
3789 let d = abs (orely - rely) in
3790 if d < bestd
3791 then loop (n+1) n d
3792 else loop (n+1) best bestd
3794 loop 0 ~-1 max_int
3796 m_active <- active;
3797 m_first <- firstof m_first active
3798 end)
3801 let enterselector usebookmarks =
3802 let source = outlinesource usebookmarks in
3803 fun errmsg ->
3804 let outlines =
3805 if usebookmarks
3806 then Array.of_list state.bookmarks
3807 else state.outlines
3809 if Array.length outlines = 0
3810 then (
3811 showtext ' ' errmsg;
3813 else (
3814 state.text <- source#greetmsg;
3815 Wsi.setcursor Wsi.CURSOR_INHERIT;
3816 let anchor = getanchor () in
3817 source#reset anchor outlines;
3818 state.uioh <- coe (new outlinelistview ~source);
3819 G.postRedisplay "enter selector";
3823 let enteroutlinemode =
3824 let f = enterselector false in
3825 fun ()-> f "Document has no outline";
3828 let enterbookmarkmode =
3829 let f = enterselector true in
3830 fun () -> f "Document has no bookmarks (yet)";
3833 let color_of_string s =
3834 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3835 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3839 let color_to_string (r, g, b) =
3840 let r = truncate (r *. 256.0)
3841 and g = truncate (g *. 256.0)
3842 and b = truncate (b *. 256.0) in
3843 Printf.sprintf "%d/%d/%d" r g b
3846 let irect_of_string s =
3847 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3850 let irect_to_string (x0,y0,x1,y1) =
3851 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3854 let makecheckers () =
3855 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3856 following to say:
3857 converted by Issac Trotts. July 25, 2002 *)
3858 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3859 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3860 let id = GlTex.gen_texture () in
3861 GlTex.bind_texture `texture_2d id;
3862 GlPix.store (`unpack_alignment 1);
3863 GlTex.image2d image;
3864 List.iter (GlTex.parameter ~target:`texture_2d)
3865 [ `wrap_s `repeat;
3866 `wrap_t `repeat;
3867 `mag_filter `nearest;
3868 `min_filter `nearest ];
3872 let setcheckers enabled =
3873 match state.texid with
3874 | None ->
3875 if enabled then state.texid <- Some (makecheckers ())
3877 | Some texid ->
3878 if not enabled
3879 then (
3880 GlTex.delete_texture texid;
3881 state.texid <- None;
3885 let int_of_string_with_suffix s =
3886 let l = String.length s in
3887 let s1, shift =
3888 if l > 1
3889 then
3890 let suffix = Char.lowercase s.[l-1] in
3891 match suffix with
3892 | 'k' -> String.sub s 0 (l-1), 10
3893 | 'm' -> String.sub s 0 (l-1), 20
3894 | 'g' -> String.sub s 0 (l-1), 30
3895 | _ -> s, 0
3896 else s, 0
3898 let n = int_of_string s1 in
3899 let m = n lsl shift in
3900 if m < 0 || m < n
3901 then raise (Failure "value too large")
3902 else m
3905 let string_with_suffix_of_int n =
3906 if n = 0
3907 then "0"
3908 else
3909 let n, s =
3910 if n land ((1 lsl 30) - 1) = 0
3911 then n lsr 30, "G"
3912 else (
3913 if n land ((1 lsl 20) - 1) = 0
3914 then n lsr 20, "M"
3915 else (
3916 if n land ((1 lsl 10) - 1) = 0
3917 then n lsr 10, "K"
3918 else n, ""
3922 let rec loop s n =
3923 let h = n mod 1000 in
3924 let n = n / 1000 in
3925 if n = 0
3926 then string_of_int h ^ s
3927 else (
3928 let s = Printf.sprintf "_%03d%s" h s in
3929 loop s n
3932 loop "" n ^ s;
3935 let defghyllscroll = (40, 8, 32);;
3936 let ghyllscroll_of_string s =
3937 let (n, a, b) as nab =
3938 if s = "default"
3939 then defghyllscroll
3940 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3942 if n <= a || n <= b || a >= b
3943 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3944 nab;
3947 let ghyllscroll_to_string ((n, a, b) as nab) =
3948 if nab = defghyllscroll
3949 then "default"
3950 else Printf.sprintf "%d,%d,%d" n a b;
3953 let describe_location () =
3954 let f (fn, _) l =
3955 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3957 let fn, ln = List.fold_left f (-1, -1) state.layout in
3958 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3959 let percent =
3960 if maxy <= 0
3961 then 100.
3962 else (100. *. (float state.y /. float maxy))
3964 if fn = ln
3965 then
3966 Printf.sprintf "page %d of %d [%.2f%%]"
3967 (fn+1) state.pagecount percent
3968 else
3969 Printf.sprintf
3970 "pages %d-%d of %d [%.2f%%]"
3971 (fn+1) (ln+1) state.pagecount percent
3974 let setpresentationmode v =
3975 let (n, _, _) = getanchor () in
3976 let _, h = getpageyh n in
3977 let ips = if conf.presentation then calcips h else conf.interpagespace in
3978 state.anchor <- (n, 0.0, float ips);
3979 conf.presentation <- v;
3980 if conf.presentation
3981 then (
3982 if not conf.scrollbarinpm
3983 then state.scrollw <- 0;
3985 else state.scrollw <- conf.scrollbw;
3986 represent ();
3989 let enterinfomode =
3990 let btos b = if b then "\xe2\x88\x9a" else "" in
3991 let showextended = ref false in
3992 let leave mode = function
3993 | Confirm -> state.mode <- mode
3994 | Cancel -> state.mode <- mode in
3995 let src =
3996 (object
3997 val mutable m_first_time = true
3998 val mutable m_l = []
3999 val mutable m_a = [||]
4000 val mutable m_prev_uioh = nouioh
4001 val mutable m_prev_mode = View
4003 inherit lvsourcebase
4005 method reset prev_mode prev_uioh =
4006 m_a <- Array.of_list (List.rev m_l);
4007 m_l <- [];
4008 m_prev_mode <- prev_mode;
4009 m_prev_uioh <- prev_uioh;
4010 if m_first_time
4011 then (
4012 let rec loop n =
4013 if n >= Array.length m_a
4014 then ()
4015 else
4016 match m_a.(n) with
4017 | _, _, _, Action _ -> m_active <- n
4018 | _ -> loop (n+1)
4020 loop 0;
4021 m_first_time <- false;
4024 method int name get set =
4025 m_l <-
4026 (name, `int get, 1, Action (
4027 fun u ->
4028 let ondone s =
4029 try set (int_of_string s)
4030 with exn ->
4031 state.text <- Printf.sprintf "bad integer `%s': %s"
4032 s (Printexc.to_string exn)
4034 state.text <- "";
4035 let te = name ^ ": ", "", None, intentry, ondone, true in
4036 state.mode <- Textentry (te, leave m_prev_mode);
4038 )) :: m_l
4040 method int_with_suffix name get set =
4041 m_l <-
4042 (name, `intws get, 1, Action (
4043 fun u ->
4044 let ondone s =
4045 try set (int_of_string_with_suffix s)
4046 with exn ->
4047 state.text <- Printf.sprintf "bad integer `%s': %s"
4048 s (Printexc.to_string exn)
4050 state.text <- "";
4051 let te =
4052 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4054 state.mode <- Textentry (te, leave m_prev_mode);
4056 )) :: m_l
4058 method bool ?(offset=1) ?(btos=btos) name get set =
4059 m_l <-
4060 (name, `bool (btos, get), offset, Action (
4061 fun u ->
4062 let v = get () in
4063 set (not v);
4065 )) :: m_l
4067 method color name get set =
4068 m_l <-
4069 (name, `color get, 1, Action (
4070 fun u ->
4071 let invalid = (nan, nan, nan) in
4072 let ondone s =
4073 let c =
4074 try color_of_string s
4075 with exn ->
4076 state.text <- Printf.sprintf "bad color `%s': %s"
4077 s (Printexc.to_string exn);
4078 invalid
4080 if c <> invalid
4081 then set c;
4083 let te = name ^ ": ", "", None, textentry, ondone, true in
4084 state.text <- color_to_string (get ());
4085 state.mode <- Textentry (te, leave m_prev_mode);
4087 )) :: m_l
4089 method string name get set =
4090 m_l <-
4091 (name, `string get, 1, Action (
4092 fun u ->
4093 let ondone s = set s in
4094 let te = name ^ ": ", "", None, textentry, ondone, true in
4095 state.mode <- Textentry (te, leave m_prev_mode);
4097 )) :: m_l
4099 method colorspace name get set =
4100 m_l <-
4101 (name, `string get, 1, Action (
4102 fun _ ->
4103 let source =
4104 let vals = [| "rgb"; "bgr"; "gray" |] in
4105 (object
4106 inherit lvsourcebase
4108 initializer
4109 m_active <- int_of_colorspace conf.colorspace;
4110 m_first <- 0;
4112 method getitemcount = Array.length vals
4113 method getitem n = (vals.(n), 0)
4114 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4115 ignore (uioh, first, pan, qsearch);
4116 if not cancel then set active;
4117 None
4118 method hasaction _ = true
4119 end)
4121 state.text <- "";
4122 let modehash = findkeyhash conf "info" in
4123 coe (new listview ~source ~trusted:true ~modehash)
4124 )) :: m_l
4126 method caption s offset =
4127 m_l <- (s, `empty, offset, Noaction) :: m_l
4129 method caption2 s f offset =
4130 m_l <- (s, `string f, offset, Noaction) :: m_l
4132 method getitemcount = Array.length m_a
4134 method getitem n =
4135 let tostr = function
4136 | `int f -> string_of_int (f ())
4137 | `intws f -> string_with_suffix_of_int (f ())
4138 | `string f -> f ()
4139 | `color f -> color_to_string (f ())
4140 | `bool (btos, f) -> btos (f ())
4141 | `empty -> ""
4143 let name, t, offset, _ = m_a.(n) in
4144 ((let s = tostr t in
4145 if String.length s > 0
4146 then Printf.sprintf "%s\t%s" name s
4147 else name),
4148 offset)
4150 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4151 let uiohopt =
4152 if not cancel
4153 then (
4154 m_qsearch <- qsearch;
4155 let uioh =
4156 match m_a.(active) with
4157 | _, _, _, Action f -> f uioh
4158 | _ -> uioh
4160 Some uioh
4162 else None
4164 m_active <- active;
4165 m_first <- first;
4166 m_pan <- pan;
4167 uiohopt
4169 method hasaction n =
4170 match m_a.(n) with
4171 | _, _, _, Action _ -> true
4172 | _ -> false
4173 end)
4175 let rec fillsrc prevmode prevuioh =
4176 let sep () = src#caption "" 0 in
4177 let colorp name get set =
4178 src#string name
4179 (fun () -> color_to_string (get ()))
4180 (fun v ->
4182 let c = color_of_string v in
4183 set c
4184 with exn ->
4185 state.text <- Printf.sprintf "bad color `%s': %s"
4186 v (Printexc.to_string exn);
4189 let oldmode = state.mode in
4190 let birdseye = isbirdseye state.mode in
4192 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4194 src#bool "presentation mode"
4195 (fun () -> conf.presentation)
4196 (fun v -> setpresentationmode v);
4198 src#bool "ignore case in searches"
4199 (fun () -> conf.icase)
4200 (fun v -> conf.icase <- v);
4202 src#bool "preload"
4203 (fun () -> conf.preload)
4204 (fun v -> conf.preload <- v);
4206 src#bool "highlight links"
4207 (fun () -> conf.hlinks)
4208 (fun v -> conf.hlinks <- v);
4210 src#bool "under info"
4211 (fun () -> conf.underinfo)
4212 (fun v -> conf.underinfo <- v);
4214 src#bool "persistent bookmarks"
4215 (fun () -> conf.savebmarks)
4216 (fun v -> conf.savebmarks <- v);
4218 src#bool "proportional display"
4219 (fun () -> conf.proportional)
4220 (fun v -> reqlayout conf.angle v);
4222 src#bool "trim margins"
4223 (fun () -> conf.trimmargins)
4224 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4226 src#bool "persistent location"
4227 (fun () -> conf.jumpback)
4228 (fun v -> conf.jumpback <- v);
4230 sep ();
4231 src#int "inter-page space"
4232 (fun () -> conf.interpagespace)
4233 (fun n ->
4234 conf.interpagespace <- n;
4235 docolumns conf.columns;
4236 let pageno, py =
4237 match state.layout with
4238 | [] -> 0, 0
4239 | l :: _ ->
4240 l.pageno, l.pagey
4242 state.maxy <- calcheight ();
4243 let y = getpagey pageno in
4244 gotoy (y + py)
4247 src#int "page bias"
4248 (fun () -> conf.pagebias)
4249 (fun v -> conf.pagebias <- v);
4251 src#int "scroll step"
4252 (fun () -> conf.scrollstep)
4253 (fun n -> conf.scrollstep <- n);
4255 src#int "horizontal scroll step"
4256 (fun () -> conf.hscrollstep)
4257 (fun v -> conf.hscrollstep <- v);
4259 src#int "auto scroll step"
4260 (fun () ->
4261 match state.autoscroll with
4262 | Some step -> step
4263 | _ -> conf.autoscrollstep)
4264 (fun n ->
4265 if state.autoscroll <> None
4266 then state.autoscroll <- Some n;
4267 conf.autoscrollstep <- n);
4269 src#int "zoom"
4270 (fun () -> truncate (conf.zoom *. 100.))
4271 (fun v -> setzoom ((float v) /. 100.));
4273 src#int "rotation"
4274 (fun () -> conf.angle)
4275 (fun v -> reqlayout v conf.proportional);
4277 src#int "scroll bar width"
4278 (fun () -> state.scrollw)
4279 (fun v ->
4280 state.scrollw <- v;
4281 conf.scrollbw <- v;
4282 reshape conf.winw conf.winh;
4285 src#int "scroll handle height"
4286 (fun () -> conf.scrollh)
4287 (fun v -> conf.scrollh <- v;);
4289 src#int "thumbnail width"
4290 (fun () -> conf.thumbw)
4291 (fun v ->
4292 conf.thumbw <- min 4096 v;
4293 match oldmode with
4294 | Birdseye beye ->
4295 leavebirdseye beye false;
4296 enterbirdseye ()
4297 | _ -> ()
4300 let mode = state.mode in
4301 src#string "columns"
4302 (fun () ->
4303 match conf.columns with
4304 | Csingle _ -> "1"
4305 | Cmulti (multi, _) -> multicolumns_to_string multi
4306 | Csplit (count, _) -> "-" ^ string_of_int count
4308 (fun v ->
4309 let n, a, b = multicolumns_of_string v in
4310 setcolumns mode n a b);
4312 sep ();
4313 src#caption "Presentation mode" 0;
4314 src#bool "scrollbar visible"
4315 (fun () -> conf.scrollbarinpm)
4316 (fun v ->
4317 if v != conf.scrollbarinpm
4318 then (
4319 conf.scrollbarinpm <- v;
4320 if conf.presentation
4321 then (
4322 state.scrollw <- if v then conf.scrollbw else 0;
4323 reshape conf.winw conf.winh;
4328 sep ();
4329 src#caption "Pixmap cache" 0;
4330 src#int_with_suffix "size (advisory)"
4331 (fun () -> conf.memlimit)
4332 (fun v -> conf.memlimit <- v);
4334 src#caption2 "used"
4335 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4336 (string_with_suffix_of_int state.memused)
4337 (Hashtbl.length state.tilemap)) 1;
4339 sep ();
4340 src#caption "Layout" 0;
4341 src#caption2 "Dimension"
4342 (fun () ->
4343 Printf.sprintf "%dx%d (virtual %dx%d)"
4344 conf.winw conf.winh
4345 state.w state.maxy)
4347 if conf.debug
4348 then
4349 src#caption2 "Position" (fun () ->
4350 Printf.sprintf "%dx%d" state.x state.y
4352 else
4353 src#caption2 "Visible" (fun () -> describe_location ()) 1
4356 sep ();
4357 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4358 "Save these parameters as global defaults at exit"
4359 (fun () -> conf.bedefault)
4360 (fun v -> conf.bedefault <- v)
4363 sep ();
4364 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4365 src#bool ~offset:0 ~btos "Extended parameters"
4366 (fun () -> !showextended)
4367 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4368 if !showextended
4369 then (
4370 src#bool "checkers"
4371 (fun () -> conf.checkers)
4372 (fun v -> conf.checkers <- v; setcheckers v);
4373 src#bool "update cursor"
4374 (fun () -> conf.updatecurs)
4375 (fun v -> conf.updatecurs <- v);
4376 src#bool "verbose"
4377 (fun () -> conf.verbose)
4378 (fun v -> conf.verbose <- v);
4379 src#bool "invert colors"
4380 (fun () -> conf.invert)
4381 (fun v -> conf.invert <- v);
4382 src#bool "max fit"
4383 (fun () -> conf.maxhfit)
4384 (fun v -> conf.maxhfit <- v);
4385 src#bool "redirect stderr"
4386 (fun () -> conf.redirectstderr)
4387 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4388 src#string "uri launcher"
4389 (fun () -> conf.urilauncher)
4390 (fun v -> conf.urilauncher <- v);
4391 src#string "path launcher"
4392 (fun () -> conf.pathlauncher)
4393 (fun v -> conf.pathlauncher <- v);
4394 src#string "tile size"
4395 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4396 (fun v ->
4398 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4399 conf.tilew <- max 64 w;
4400 conf.tileh <- max 64 h;
4401 flushtiles ();
4402 with exn ->
4403 state.text <- Printf.sprintf "bad tile size `%s': %s"
4404 v (Printexc.to_string exn));
4405 src#int "texture count"
4406 (fun () -> conf.texcount)
4407 (fun v ->
4408 if realloctexts v
4409 then conf.texcount <- v
4410 else showtext '!' " Failed to set texture count please retry later"
4412 src#int "slice height"
4413 (fun () -> conf.sliceheight)
4414 (fun v ->
4415 conf.sliceheight <- v;
4416 wcmd "sliceh %d" conf.sliceheight;
4418 src#int "anti-aliasing level"
4419 (fun () -> conf.aalevel)
4420 (fun v ->
4421 conf.aalevel <- bound v 0 8;
4422 state.anchor <- getanchor ();
4423 opendoc state.path state.password;
4425 src#string "page scroll scaling factor"
4426 (fun () -> string_of_float conf.pgscale)
4427 (fun v ->
4429 let s = float_of_string v in
4430 conf.pgscale <- s
4431 with exn ->
4432 state.text <- Printf.sprintf
4433 "bad page scroll scaling factor `%s': %s"
4434 v (Printexc.to_string exn)
4437 src#int "ui font size"
4438 (fun () -> fstate.fontsize)
4439 (fun v -> setfontsize (bound v 5 100));
4440 src#int "hint font size"
4441 (fun () -> conf.hfsize)
4442 (fun v -> conf.hfsize <- bound v 5 100);
4443 colorp "background color"
4444 (fun () -> conf.bgcolor)
4445 (fun v -> conf.bgcolor <- v);
4446 src#bool "crop hack"
4447 (fun () -> conf.crophack)
4448 (fun v -> conf.crophack <- v);
4449 src#string "trim fuzz"
4450 (fun () -> irect_to_string conf.trimfuzz)
4451 (fun v ->
4453 conf.trimfuzz <- irect_of_string v;
4454 if conf.trimmargins
4455 then settrim true conf.trimfuzz;
4456 with exn ->
4457 state.text <- Printf.sprintf "bad irect `%s': %s"
4458 v (Printexc.to_string exn)
4460 src#string "throttle"
4461 (fun () ->
4462 match conf.maxwait with
4463 | None -> "show place holder if page is not ready"
4464 | Some time ->
4465 if time = infinity
4466 then "wait for page to fully render"
4467 else
4468 "wait " ^ string_of_float time
4469 ^ " seconds before showing placeholder"
4471 (fun v ->
4473 let f = float_of_string v in
4474 if f <= 0.0
4475 then conf.maxwait <- None
4476 else conf.maxwait <- Some f
4477 with exn ->
4478 state.text <- Printf.sprintf "bad time `%s': %s"
4479 v (Printexc.to_string exn)
4481 src#string "ghyll scroll"
4482 (fun () ->
4483 match conf.ghyllscroll with
4484 | None -> ""
4485 | Some nab -> ghyllscroll_to_string nab
4487 (fun v ->
4489 let gs =
4490 if String.length v = 0
4491 then None
4492 else Some (ghyllscroll_of_string v)
4494 conf.ghyllscroll <- gs
4495 with exn ->
4496 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4497 v (Printexc.to_string exn)
4499 src#string "selection command"
4500 (fun () -> conf.selcmd)
4501 (fun v -> conf.selcmd <- v);
4502 src#colorspace "color space"
4503 (fun () -> colorspace_to_string conf.colorspace)
4504 (fun v ->
4505 conf.colorspace <- colorspace_of_int v;
4506 wcmd "cs %d" v;
4507 load state.layout;
4509 if pbousable ()
4510 then
4511 src#bool "use PBO"
4512 (fun () -> conf.usepbo)
4513 (fun v -> conf.usepbo <- v);
4514 src#bool "mouse wheel scrolls pages"
4515 (fun () -> conf.wheelbypage)
4516 (fun v -> conf.wheelbypage <- v);
4519 sep ();
4520 src#caption "Document" 0;
4521 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4522 src#caption2 "Pages"
4523 (fun () -> string_of_int state.pagecount) 1;
4524 src#caption2 "Dimensions"
4525 (fun () -> string_of_int (List.length state.pdims)) 1;
4526 if conf.trimmargins
4527 then (
4528 sep ();
4529 src#caption "Trimmed margins" 0;
4530 src#caption2 "Dimensions"
4531 (fun () -> string_of_int (List.length state.pdims)) 1;
4534 sep ();
4535 src#caption "OpenGL" 0;
4536 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4537 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4538 src#reset prevmode prevuioh;
4540 fun () ->
4541 state.text <- "";
4542 let prevmode = state.mode
4543 and prevuioh = state.uioh in
4544 fillsrc prevmode prevuioh;
4545 let source = (src :> lvsource) in
4546 let modehash = findkeyhash conf "info" in
4547 state.uioh <- coe (object (self)
4548 inherit listview ~source ~trusted:true ~modehash as super
4549 val mutable m_prevmemused = 0
4550 method infochanged = function
4551 | Memused ->
4552 if m_prevmemused != state.memused
4553 then (
4554 m_prevmemused <- state.memused;
4555 G.postRedisplay "memusedchanged";
4557 | Pdim -> G.postRedisplay "pdimchanged"
4558 | Docinfo -> fillsrc prevmode prevuioh
4560 method key key mask =
4561 if not (Wsi.withctrl mask)
4562 then
4563 match key with
4564 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4565 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4566 | _ -> super#key key mask
4567 else super#key key mask
4568 end);
4569 G.postRedisplay "info";
4572 let enterhelpmode =
4573 let source =
4574 (object
4575 inherit lvsourcebase
4576 method getitemcount = Array.length state.help
4577 method getitem n =
4578 let s, l, _ = state.help.(n) in
4579 (s, l)
4581 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4582 let optuioh =
4583 if not cancel
4584 then (
4585 m_qsearch <- qsearch;
4586 match state.help.(active) with
4587 | _, _, Action f -> Some (f uioh)
4588 | _ -> Some (uioh)
4590 else None
4592 m_active <- active;
4593 m_first <- first;
4594 m_pan <- pan;
4595 optuioh
4597 method hasaction n =
4598 match state.help.(n) with
4599 | _, _, Action _ -> true
4600 | _ -> false
4602 initializer
4603 m_active <- -1
4604 end)
4605 in fun () ->
4606 let modehash = findkeyhash conf "help" in
4607 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4608 G.postRedisplay "help";
4611 let entermsgsmode =
4612 let msgsource =
4613 let re = Str.regexp "[\r\n]" in
4614 (object
4615 inherit lvsourcebase
4616 val mutable m_items = [||]
4618 method getitemcount = 1 + Array.length m_items
4620 method getitem n =
4621 if n = 0
4622 then "[Clear]", 0
4623 else m_items.(n-1), 0
4625 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4626 ignore uioh;
4627 if not cancel
4628 then (
4629 if active = 0
4630 then Buffer.clear state.errmsgs;
4631 m_qsearch <- qsearch;
4633 m_active <- active;
4634 m_first <- first;
4635 m_pan <- pan;
4636 None
4638 method hasaction n =
4639 n = 0
4641 method reset =
4642 state.newerrmsgs <- false;
4643 let l = Str.split re (Buffer.contents state.errmsgs) in
4644 m_items <- Array.of_list l
4646 initializer
4647 m_active <- 0
4648 end)
4649 in fun () ->
4650 state.text <- "";
4651 msgsource#reset;
4652 let source = (msgsource :> lvsource) in
4653 let modehash = findkeyhash conf "listview" in
4654 state.uioh <- coe (object
4655 inherit listview ~source ~trusted:false ~modehash as super
4656 method display =
4657 if state.newerrmsgs
4658 then msgsource#reset;
4659 super#display
4660 end);
4661 G.postRedisplay "msgs";
4664 let quickbookmark ?title () =
4665 match state.layout with
4666 | [] -> ()
4667 | l :: _ ->
4668 let title =
4669 match title with
4670 | None ->
4671 let sec = Unix.gettimeofday () in
4672 let tm = Unix.localtime sec in
4673 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4674 (l.pageno+1)
4675 tm.Unix.tm_mday
4676 tm.Unix.tm_mon
4677 (tm.Unix.tm_year + 1900)
4678 tm.Unix.tm_hour
4679 tm.Unix.tm_min
4680 | Some title -> title
4682 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4685 let doreshape w h =
4686 state.fullscreen <- None;
4687 Wsi.reshape w h;
4690 let setautoscrollspeed step goingdown =
4691 let incr = max 1 ((abs step) / 2) in
4692 let incr = if goingdown then incr else -incr in
4693 let astep = step + incr in
4694 state.autoscroll <- Some astep;
4697 let gotounder = function
4698 | Ulinkgoto (pageno, top) ->
4699 if pageno >= 0
4700 then (
4701 addnav ();
4702 gotopage1 pageno top;
4705 | Ulinkuri s ->
4706 gotouri s
4708 | Uremote (filename, pageno) ->
4709 let path =
4710 if Sys.file_exists filename
4711 then filename
4712 else
4713 let dir = Filename.dirname state.path in
4714 let path = Filename.concat dir filename in
4715 if Sys.file_exists path
4716 then path
4717 else ""
4719 if String.length path > 0
4720 then (
4721 let anchor = getanchor () in
4722 let ranchor = state.path, state.password, anchor in
4723 state.anchor <- (pageno, 0.0, 0.0);
4724 state.ranchors <- ranchor :: state.ranchors;
4725 opendoc path "";
4727 else showtext '!' ("Could not find " ^ filename)
4729 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4732 let canpan () =
4733 match conf.columns with
4734 | Csplit _ -> true
4735 | _ -> conf.zoom > 1.0
4738 let existsinrow pageno (columns, coverA, coverB) p =
4739 let last = ((pageno - coverA) mod columns) + columns in
4740 let rec any = function
4741 | [] -> false
4742 | l :: rest ->
4743 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4744 then p l
4745 else (
4746 if not (p l)
4747 then (if l.pageno = last then false else any rest)
4748 else true
4751 any state.layout
4754 let nextpage () =
4755 match state.layout with
4756 | [] -> ()
4757 | l :: rest ->
4758 match conf.columns with
4759 | Csingle _ ->
4760 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4761 then
4762 let y = clamp (pgscale conf.winh) in
4763 gotoghyll y
4764 else
4765 let pageno = min (l.pageno+1) (state.pagecount-1) in
4766 gotoghyll (getpagey pageno)
4767 | Cmulti ((c, _, _) as cl, _) ->
4768 if conf.presentation
4769 && (existsinrow l.pageno cl
4770 (fun l -> l.pageh > l.pagey + l.pagevh))
4771 then
4772 let y = clamp (pgscale conf.winh) in
4773 gotoghyll y
4774 else
4775 let pageno = min (l.pageno+c) (state.pagecount-1) in
4776 gotoghyll (getpagey pageno)
4777 | Csplit (n, _) ->
4778 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4779 then
4780 let pagey, pageh = getpageyh l.pageno in
4781 let pagey = pagey + pageh * l.pagecol in
4782 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4783 gotoghyll (pagey + pageh + ips)
4786 let prevpage () =
4787 match state.layout with
4788 | [] -> ()
4789 | l :: _ ->
4790 match conf.columns with
4791 | Csingle _ ->
4792 if conf.presentation && l.pagey != 0
4793 then
4794 gotoghyll (clamp (pgscale ~-(conf.winh)))
4795 else
4796 let pageno = max 0 (l.pageno-1) in
4797 gotoghyll (getpagey pageno)
4798 | Cmulti ((c, _, coverB) as cl, _) ->
4799 if conf.presentation &&
4800 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4801 then
4802 gotoghyll (clamp (pgscale ~-(conf.winh)))
4803 else
4804 let decr =
4805 if l.pageno = state.pagecount - coverB
4806 then 1
4807 else c
4809 let pageno = max 0 (l.pageno-decr) in
4810 gotoghyll (getpagey pageno)
4811 | Csplit (n, _) ->
4812 let y =
4813 if l.pagecol = 0
4814 then
4815 if l.pageno = 0
4816 then l.pagey
4817 else
4818 let pageno = max 0 (l.pageno-1) in
4819 let pagey, pageh = getpageyh pageno in
4820 pagey + (n-1)*pageh
4821 else
4822 let pagey, pageh = getpageyh l.pageno in
4823 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4825 gotoghyll y
4828 let viewkeyboard key mask =
4829 let enttext te =
4830 let mode = state.mode in
4831 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4832 state.text <- "";
4833 enttext ();
4834 G.postRedisplay "view:enttext"
4836 let ctrl = Wsi.withctrl mask in
4837 let key =
4838 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4840 match key with
4841 | 81 -> (* Q *)
4842 exit 0
4844 | 0xff63 -> (* insert *)
4845 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4846 then (
4847 state.mode <- LinkNav (Ltgendir 0);
4848 gotoy state.y;
4850 else showtext '!' "Keyboard link navigation does not work under rotation"
4852 | 0xff1b | 113 -> (* escape / q *)
4853 begin match state.mstate with
4854 | Mzoomrect _ ->
4855 state.mstate <- Mnone;
4856 Wsi.setcursor Wsi.CURSOR_INHERIT;
4857 G.postRedisplay "kill zoom rect";
4858 | _ ->
4859 begin match state.mode with
4860 | LinkNav _ ->
4861 state.mode <- View;
4862 G.postRedisplay "esc leave linknav"
4863 | _ ->
4864 match state.ranchors with
4865 | [] -> raise Quit
4866 | (path, password, anchor) :: rest ->
4867 state.ranchors <- rest;
4868 state.anchor <- anchor;
4869 opendoc path password
4870 end;
4871 end;
4873 | 0xff08 -> (* backspace *)
4874 gotoghyll (getnav ~-1)
4876 | 111 -> (* o *)
4877 enteroutlinemode ()
4879 | 117 -> (* u *)
4880 state.rects <- [];
4881 state.text <- "";
4882 G.postRedisplay "dehighlight";
4884 | 47 | 63 -> (* / ? *)
4885 let ondone isforw s =
4886 cbput state.hists.pat s;
4887 state.searchpattern <- s;
4888 search s isforw
4890 let s = String.create 1 in
4891 s.[0] <- Char.chr key;
4892 enttext (s, "", Some (onhist state.hists.pat),
4893 textentry, ondone (key = 47), true)
4895 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4896 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4897 setzoom (conf.zoom +. incr)
4899 | 43 | 0xffab -> (* + *)
4900 let ondone s =
4901 let n =
4902 try int_of_string s with exc ->
4903 state.text <- Printf.sprintf "bad integer `%s': %s"
4904 s (Printexc.to_string exc);
4905 max_int
4907 if n != max_int
4908 then (
4909 conf.pagebias <- n;
4910 state.text <- "page bias is now " ^ string_of_int n;
4913 enttext ("page bias: ", "", None, intentry, ondone, true)
4915 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4916 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4917 setzoom (max 0.01 (conf.zoom -. decr))
4919 | 45 | 0xffad -> (* - *)
4920 let ondone msg = state.text <- msg in
4921 enttext (
4922 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4923 optentry state.mode, ondone, true
4926 | 48 when ctrl -> (* ctrl-0 *)
4927 setzoom 1.0
4929 | 49 when ctrl -> (* ctrl-1 *)
4930 let cols =
4931 match conf.columns with
4932 | Csingle _ | Cmulti _ -> 1
4933 | Csplit (n, _) -> n
4935 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4936 if zoom < 1.0
4937 then setzoom zoom
4939 | 0xffc6 -> (* f9 *)
4940 togglebirdseye ()
4942 | 57 when ctrl -> (* ctrl-9 *)
4943 togglebirdseye ()
4945 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4946 when not ctrl -> (* 0..9 *)
4947 let ondone s =
4948 let n =
4949 try int_of_string s with exc ->
4950 state.text <- Printf.sprintf "bad integer `%s': %s"
4951 s (Printexc.to_string exc);
4954 if n >= 0
4955 then (
4956 addnav ();
4957 cbput state.hists.pag (string_of_int n);
4958 gotopage1 (n + conf.pagebias - 1) 0;
4961 let pageentry text key =
4962 match Char.unsafe_chr key with
4963 | 'g' -> TEdone text
4964 | _ -> intentry text key
4966 let text = "x" in text.[0] <- Char.chr key;
4967 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4969 | 98 -> (* b *)
4970 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4971 reshape conf.winw conf.winh;
4973 | 108 -> (* l *)
4974 conf.hlinks <- not conf.hlinks;
4975 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4976 G.postRedisplay "toggle highlightlinks";
4978 | 70 -> (* F *)
4979 state.glinks <- true;
4980 let mode = state.mode in
4981 state.mode <- Textentry (
4982 (":", "", None, linknentry, linkndone gotounder, false),
4983 (fun _ ->
4984 state.glinks <- false;
4985 state.mode <- mode)
4987 state.text <- "";
4988 G.postRedisplay "view:linkent(F)"
4990 | 121 -> (* y *)
4991 state.glinks <- true;
4992 let mode = state.mode in
4993 state.mode <- Textentry (
4994 (":", "", None, linknentry, linkndone (fun under ->
4995 match Ne.pipe () with
4996 | Ne.Exn exn ->
4997 showtext '!' (Printf.sprintf "pipe failed: %s"
4998 (Printexc.to_string exn));
4999 | Ne.Res (r, w) ->
5000 let popened =
5001 try popen conf.selcmd [r, 0; w, -1]; true
5002 with exn ->
5003 showtext '!'
5004 (Printf.sprintf "failed to execute %s: %s"
5005 conf.selcmd (Printexc.to_string exn));
5006 false
5008 let clo cap fd =
5009 Ne.clo fd (fun msg ->
5010 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5013 let s = undertext under in
5014 if popened
5015 then
5016 (try
5017 let l = String.length s in
5018 let n = Unix.write w s 0 l in
5019 if n != l
5020 then
5021 showtext '!'
5022 (Printf.sprintf
5023 "failed to write %d characters to sel pipe, wrote %d"
5026 with exn ->
5027 showtext '!'
5028 (Printf.sprintf "failed to write to sel pipe: %s"
5029 (Printexc.to_string exn)
5032 else dolog "%s" s;
5033 clo "pipe/r" r;
5034 clo "pipe/w" w;
5035 ), false
5037 fun _ ->
5038 state.glinks <- false;
5039 state.mode <- mode
5041 state.text <- "";
5042 G.postRedisplay "view:linkent"
5044 | 97 -> (* a *)
5045 begin match state.autoscroll with
5046 | Some step ->
5047 conf.autoscrollstep <- step;
5048 state.autoscroll <- None
5049 | None ->
5050 if conf.autoscrollstep = 0
5051 then state.autoscroll <- Some 1
5052 else state.autoscroll <- Some conf.autoscrollstep
5055 | 112 when ctrl -> (* ctrl-p *)
5056 launchpath ()
5058 | 80 -> (* P *)
5059 setpresentationmode (not conf.presentation);
5060 showtext ' ' ("presentation mode " ^
5061 if conf.presentation then "on" else "off");
5063 | 102 -> (* f *)
5064 begin match state.fullscreen with
5065 | None ->
5066 state.fullscreen <- Some (conf.winw, conf.winh);
5067 Wsi.fullscreen ()
5068 | Some (w, h) ->
5069 state.fullscreen <- None;
5070 doreshape w h
5073 | 112 | 78 -> (* p|N *)
5074 search state.searchpattern false
5076 | 110 | 0xffc0 -> (* n|F3 *)
5077 search state.searchpattern true
5079 | 116 -> (* t *)
5080 begin match state.layout with
5081 | [] -> ()
5082 | l :: _ ->
5083 gotoy_and_clear_text (getpagey l.pageno)
5086 | 32 -> (* space *)
5087 nextpage ()
5089 | 0xff9f | 0xffff -> (* delete *)
5090 prevpage ()
5092 | 61 -> (* = *)
5093 showtext ' ' (describe_location ());
5095 | 119 -> (* w *)
5096 begin match state.layout with
5097 | [] -> ()
5098 | l :: _ ->
5099 doreshape (l.pagew + state.scrollw) l.pageh;
5100 G.postRedisplay "w"
5103 | 39 -> (* ' *)
5104 enterbookmarkmode ()
5106 | 104 | 0xffbe -> (* h|F1 *)
5107 enterhelpmode ()
5109 | 105 -> (* i *)
5110 enterinfomode ()
5112 | 101 when conf.redirectstderr -> (* e *)
5113 entermsgsmode ()
5115 | 109 -> (* m *)
5116 let ondone s =
5117 match state.layout with
5118 | l :: _ ->
5119 if String.length s > 0
5120 then
5121 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5122 | _ -> ()
5124 enttext ("bookmark: ", "", None, textentry, ondone, true)
5126 | 126 -> (* ~ *)
5127 quickbookmark ();
5128 showtext ' ' "Quick bookmark added";
5130 | 122 -> (* z *)
5131 begin match state.layout with
5132 | l :: _ ->
5133 let rect = getpdimrect l.pagedimno in
5134 let w, h =
5135 if conf.crophack
5136 then
5137 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5138 truncate (1.2 *. (rect.(3) -. rect.(0))))
5139 else
5140 (truncate (rect.(1) -. rect.(0)),
5141 truncate (rect.(3) -. rect.(0)))
5143 let w = truncate ((float w)*.conf.zoom)
5144 and h = truncate ((float h)*.conf.zoom) in
5145 if w != 0 && h != 0
5146 then (
5147 state.anchor <- getanchor ();
5148 doreshape (w + state.scrollw) (h + conf.interpagespace)
5150 G.postRedisplay "z";
5152 | [] -> ()
5155 | 50 when ctrl -> (* ctrl-2 *)
5156 let maxw = getmaxw () in
5157 if maxw > 0.0
5158 then setzoom (maxw /. float conf.winw)
5160 | 60 | 62 -> (* < > *)
5161 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5163 | 91 | 93 -> (* [ ] *)
5164 conf.colorscale <-
5165 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5167 G.postRedisplay "brightness";
5169 | 99 when state.mode = View -> (* c *)
5170 let (c, a, b), z =
5171 match state.prevcolumns with
5172 | None -> (1, 0, 0), 1.0
5173 | Some (columns, z) ->
5174 let cab =
5175 match columns with
5176 | Csplit (c, _) -> -c, 0, 0
5177 | Cmulti ((c, a, b), _) -> c, a, b
5178 | Csingle _ -> 1, 0, 0
5180 cab, z
5182 setcolumns View c a b;
5183 setzoom z;
5185 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5186 setzoom state.prevzoom
5188 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5189 begin match state.autoscroll with
5190 | None ->
5191 begin match state.mode with
5192 | Birdseye beye -> upbirdseye 1 beye
5193 | _ ->
5194 if ctrl
5195 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5196 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5198 | Some n ->
5199 setautoscrollspeed n false
5202 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5203 begin match state.autoscroll with
5204 | None ->
5205 begin match state.mode with
5206 | Birdseye beye -> downbirdseye 1 beye
5207 | _ ->
5208 if ctrl
5209 then gotoy_and_clear_text (clamp (conf.winh/2))
5210 else gotoy_and_clear_text (clamp conf.scrollstep)
5212 | Some n ->
5213 setautoscrollspeed n true
5216 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5217 if canpan ()
5218 then
5219 let dx =
5220 if ctrl
5221 then conf.winw / 2
5222 else conf.hscrollstep
5224 let dx = if key = 0xff51 then dx else -dx in
5225 state.x <- state.x + dx;
5226 gotoy_and_clear_text state.y
5227 else (
5228 state.text <- "";
5229 G.postRedisplay "lef/right"
5232 | 0xff55 | 0xff9a -> (* (kp) prior *)
5233 let y =
5234 if ctrl
5235 then
5236 match state.layout with
5237 | [] -> state.y
5238 | l :: _ -> state.y - l.pagey
5239 else
5240 clamp (pgscale (-conf.winh))
5242 gotoghyll y
5244 | 0xff56 | 0xff9b -> (* (kp) next *)
5245 let y =
5246 if ctrl
5247 then
5248 match List.rev state.layout with
5249 | [] -> state.y
5250 | l :: _ -> getpagey l.pageno
5251 else
5252 clamp (pgscale conf.winh)
5254 gotoghyll y
5256 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5257 gotoghyll 0
5258 | 71 | 0xff57 | 0xff9c -> (* G end *)
5259 gotoghyll (clamp state.maxy)
5261 | 0xff53 when Wsi.withalt mask -> (* alt-right *)
5262 gotoghyll (getnav 1)
5263 | 0xff51 when Wsi.withalt mask -> (* alt-left *)
5264 gotoghyll (getnav ~-1)
5266 | 114 -> (* r *)
5267 reload ()
5269 | 118 when conf.debug -> (* v *)
5270 state.rects <- [];
5271 List.iter (fun l ->
5272 match getopaque l.pageno with
5273 | None -> ()
5274 | Some opaque ->
5275 let x0, y0, x1, y1 = pagebbox opaque in
5276 let a,b = float x0, float y0 in
5277 let c,d = float x1, float y0 in
5278 let e,f = float x1, float y1 in
5279 let h,j = float x0, float y1 in
5280 let rect = (a,b,c,d,e,f,h,j) in
5281 debugrect rect;
5282 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5283 ) state.layout;
5284 G.postRedisplay "v";
5286 | _ ->
5287 vlog "huh? %s" (Wsi.keyname key)
5290 let linknavkeyboard key mask linknav =
5291 let getpage pageno =
5292 let rec loop = function
5293 | [] -> None
5294 | l :: _ when l.pageno = pageno -> Some l
5295 | _ :: rest -> loop rest
5296 in loop state.layout
5298 let doexact (pageno, n) =
5299 match getopaque pageno, getpage pageno with
5300 | Some opaque, Some l ->
5301 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5302 then
5303 let under = getlink opaque n in
5304 G.postRedisplay "link gotounder";
5305 gotounder under;
5306 state.mode <- View;
5307 else
5308 let opt, dir =
5309 match key with
5310 | 0xff50 -> (* home *)
5311 Some (findlink opaque LDfirst), -1
5313 | 0xff57 -> (* end *)
5314 Some (findlink opaque LDlast), 1
5316 | 0xff51 -> (* left *)
5317 Some (findlink opaque (LDleft n)), -1
5319 | 0xff53 -> (* right *)
5320 Some (findlink opaque (LDright n)), 1
5322 | 0xff52 -> (* up *)
5323 Some (findlink opaque (LDup n)), -1
5325 | 0xff54 -> (* down *)
5326 Some (findlink opaque (LDdown n)), 1
5328 | _ -> None, 0
5330 let pwl l dir =
5331 begin match findpwl l.pageno dir with
5332 | Pwlnotfound -> ()
5333 | Pwl pageno ->
5334 let notfound dir =
5335 state.mode <- LinkNav (Ltgendir dir);
5336 let y, h = getpageyh pageno in
5337 let y =
5338 if dir < 0
5339 then y + h - conf.winh
5340 else y
5342 gotoy y
5344 begin match getopaque pageno, getpage pageno with
5345 | Some opaque, Some _ ->
5346 let link =
5347 let ld = if dir > 0 then LDfirst else LDlast in
5348 findlink opaque ld
5350 begin match link with
5351 | Lfound m ->
5352 showlinktype (getlink opaque m);
5353 state.mode <- LinkNav (Ltexact (pageno, m));
5354 G.postRedisplay "linknav jpage";
5355 | _ -> notfound dir
5356 end;
5357 | _ -> notfound dir
5358 end;
5359 end;
5361 begin match opt with
5362 | Some Lnotfound -> pwl l dir;
5363 | Some (Lfound m) ->
5364 if m = n
5365 then pwl l dir
5366 else (
5367 let _, y0, _, y1 = getlinkrect opaque m in
5368 if y0 < l.pagey
5369 then gotopage1 l.pageno y0
5370 else (
5371 let d = fstate.fontsize + 1 in
5372 if y1 - l.pagey > l.pagevh - d
5373 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5374 else G.postRedisplay "linknav";
5376 showlinktype (getlink opaque m);
5377 state.mode <- LinkNav (Ltexact (l.pageno, m));
5380 | None -> viewkeyboard key mask
5381 end;
5382 | _ -> viewkeyboard key mask
5384 if key = 0xff63
5385 then (
5386 state.mode <- View;
5387 G.postRedisplay "leave linknav"
5389 else
5390 match linknav with
5391 | Ltgendir _ -> viewkeyboard key mask
5392 | Ltexact exact -> doexact exact
5395 let keyboard key mask =
5396 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5397 then wcmd "interrupt"
5398 else state.uioh <- state.uioh#key key mask
5401 let birdseyekeyboard key mask
5402 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5403 let incr =
5404 match conf.columns with
5405 | Csingle _ -> 1
5406 | Cmulti ((c, _, _), _) -> c
5407 | Csplit _ -> failwith "bird's eye split mode"
5409 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5410 match key with
5411 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5412 let y, h = getpageyh pageno in
5413 let top = (conf.winh - h) / 2 in
5414 gotoy (max 0 (y - top))
5415 | 0xff0d (* enter *)
5416 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5417 | 0xff1b -> leavebirdseye beye true (* escape *)
5418 | 0xff52 -> upbirdseye incr beye (* up *)
5419 | 0xff54 -> downbirdseye incr beye (* down *)
5420 | 0xff51 -> upbirdseye 1 beye (* left *)
5421 | 0xff53 -> downbirdseye 1 beye (* right *)
5423 | 0xff55 -> (* prior *)
5424 begin match state.layout with
5425 | l :: _ ->
5426 if l.pagey != 0
5427 then (
5428 state.mode <- Birdseye (
5429 oconf, leftx, l.pageno, hooverpageno, anchor
5431 gotopage1 l.pageno 0;
5433 else (
5434 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5435 match layout with
5436 | [] -> gotoy (clamp (-conf.winh))
5437 | l :: _ ->
5438 state.mode <- Birdseye (
5439 oconf, leftx, l.pageno, hooverpageno, anchor
5441 gotopage1 l.pageno 0
5444 | [] -> gotoy (clamp (-conf.winh))
5445 end;
5447 | 0xff56 -> (* next *)
5448 begin match List.rev state.layout with
5449 | l :: _ ->
5450 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5451 begin match layout with
5452 | [] ->
5453 let incr = l.pageh - l.pagevh in
5454 if incr = 0
5455 then (
5456 state.mode <-
5457 Birdseye (
5458 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5460 G.postRedisplay "birdseye pagedown";
5462 else gotoy (clamp (incr + conf.interpagespace*2));
5464 | l :: _ ->
5465 state.mode <-
5466 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5467 gotopage1 l.pageno 0;
5470 | [] -> gotoy (clamp conf.winh)
5471 end;
5473 | 0xff50 -> (* home *)
5474 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5475 gotopage1 0 0
5477 | 0xff57 -> (* end *)
5478 let pageno = state.pagecount - 1 in
5479 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5480 if not (pagevisible state.layout pageno)
5481 then
5482 let h =
5483 match List.rev state.pdims with
5484 | [] -> conf.winh
5485 | (_, _, h, _) :: _ -> h
5487 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5488 else G.postRedisplay "birdseye end";
5489 | _ -> viewkeyboard key mask
5492 let drawpage l linkindexbase =
5493 let color =
5494 match state.mode with
5495 | Textentry _ -> scalecolor 0.4
5496 | LinkNav _
5497 | View -> scalecolor 1.0
5498 | Birdseye (_, _, pageno, hooverpageno, _) ->
5499 if l.pageno = hooverpageno
5500 then scalecolor 0.9
5501 else (
5502 if l.pageno = pageno
5503 then scalecolor 1.0
5504 else scalecolor 0.8
5507 drawtiles l color;
5508 begin match getopaque l.pageno with
5509 | Some opaque ->
5510 if tileready l l.pagex l.pagey
5511 then
5512 let x = l.pagedispx - l.pagex
5513 and y = l.pagedispy - l.pagey in
5514 let hlmask =
5515 match conf.columns with
5516 | Csingle _ | Cmulti _ ->
5517 (if conf.hlinks then 1 else 0)
5518 + (if state.glinks
5519 && not (isbirdseye state.mode) then 2 else 0)
5520 | _ -> 0
5522 let s =
5523 match state.mode with
5524 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5525 | _ -> ""
5527 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5528 else 0
5530 | _ -> 0
5531 end;
5534 let scrollindicator () =
5535 let sbw, ph, sh = state.uioh#scrollph in
5536 let sbh, pw, sw = state.uioh#scrollpw in
5538 GlDraw.color (0.64, 0.64, 0.64);
5539 GlDraw.rect
5540 (float (conf.winw - sbw), 0.)
5541 (float conf.winw, float conf.winh)
5543 GlDraw.rect
5544 (0., float (conf.winh - sbh))
5545 (float (conf.winw - state.scrollw - 1), float conf.winh)
5547 GlDraw.color (0.0, 0.0, 0.0);
5549 GlDraw.rect
5550 (float (conf.winw - sbw), ph)
5551 (float conf.winw, ph +. sh)
5553 GlDraw.rect
5554 (pw, float (conf.winh - sbh))
5555 (pw +. sw, float conf.winh)
5559 let showsel () =
5560 match state.mstate with
5561 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5564 | Msel ((x0, y0), (x1, y1)) ->
5565 let rec loop = function
5566 | l :: ls ->
5567 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5568 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5569 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5570 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5571 then
5572 match getopaque l.pageno with
5573 | Some opaque ->
5574 let x0, y0 = pagetranslatepoint l x0 y0 in
5575 let x1, y1 = pagetranslatepoint l x1 y1 in
5576 seltext opaque (x0, y0, x1, y1);
5577 | _ -> ()
5578 else loop ls
5579 | [] -> ()
5581 loop state.layout
5584 let showrects rects =
5585 Gl.enable `blend;
5586 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5587 GlDraw.polygon_mode `both `fill;
5588 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5589 List.iter
5590 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5591 List.iter (fun l ->
5592 if l.pageno = pageno
5593 then (
5594 let dx = float (l.pagedispx - l.pagex) in
5595 let dy = float (l.pagedispy - l.pagey) in
5596 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5597 GlDraw.begins `quads;
5599 GlDraw.vertex2 (x0+.dx, y0+.dy);
5600 GlDraw.vertex2 (x1+.dx, y1+.dy);
5601 GlDraw.vertex2 (x2+.dx, y2+.dy);
5602 GlDraw.vertex2 (x3+.dx, y3+.dy);
5604 GlDraw.ends ();
5606 ) state.layout
5607 ) rects
5609 Gl.disable `blend;
5612 let display () =
5613 GlClear.color (scalecolor2 conf.bgcolor);
5614 GlClear.clear [`color];
5615 let rec loop linkindexbase = function
5616 | l :: rest ->
5617 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5618 loop linkindexbase rest
5619 | [] -> ()
5621 loop 0 state.layout;
5622 let rects =
5623 match state.mode with
5624 | LinkNav (Ltexact (pageno, linkno)) ->
5625 begin match getopaque pageno with
5626 | Some opaque ->
5627 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5628 (pageno, 5, (
5629 float x0, float y0,
5630 float x1, float y0,
5631 float x1, float y1,
5632 float x0, float y1)
5633 ) :: state.rects
5634 | None -> state.rects
5636 | _ -> state.rects
5638 showrects rects;
5639 showsel ();
5640 state.uioh#display;
5641 begin match state.mstate with
5642 | Mzoomrect ((x0, y0), (x1, y1)) ->
5643 Gl.enable `blend;
5644 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5645 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5646 GlDraw.rect (float x0, float y0)
5647 (float x1, float y1);
5648 Gl.disable `blend;
5649 | _ -> ()
5650 end;
5651 enttext ();
5652 scrollindicator ();
5653 if not state.wthack then Wsi.swapb ();
5656 let zoomrect x y x1 y1 =
5657 let x0 = min x x1
5658 and x1 = max x x1
5659 and y0 = min y y1 in
5660 gotoy (state.y + y0);
5661 state.anchor <- getanchor ();
5662 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5663 let margin =
5664 if state.w < conf.winw - state.scrollw
5665 then (conf.winw - state.scrollw - state.w) / 2
5666 else 0
5668 state.x <- (state.x + margin) - x0;
5669 setzoom zoom;
5670 Wsi.setcursor Wsi.CURSOR_INHERIT;
5671 state.mstate <- Mnone;
5674 let scrollx x =
5675 let winw = conf.winw - state.scrollw - 1 in
5676 let s = float x /. float winw in
5677 let destx = truncate (float (state.w + winw) *. s) in
5678 state.x <- winw - destx;
5679 gotoy_and_clear_text state.y;
5680 state.mstate <- Mscrollx;
5683 let scrolly y =
5684 let s = float y /. float conf.winh in
5685 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5686 gotoy_and_clear_text desty;
5687 state.mstate <- Mscrolly;
5690 let viewmouse button down x y mask =
5691 match button with
5692 | n when (n == 4 || n == 5) && not down ->
5693 if Wsi.withctrl mask
5694 then (
5695 match state.mstate with
5696 | Mzoom (oldn, i) ->
5697 if oldn = n
5698 then (
5699 if i = 2
5700 then
5701 let incr =
5702 match n with
5703 | 5 ->
5704 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5705 | _ ->
5706 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5708 let zoom = conf.zoom -. incr in
5709 setzoom zoom;
5710 state.mstate <- Mzoom (n, 0);
5711 else
5712 state.mstate <- Mzoom (n, i+1);
5714 else state.mstate <- Mzoom (n, 0)
5716 | _ -> state.mstate <- Mzoom (n, 0)
5718 else (
5719 match state.autoscroll with
5720 | Some step -> setautoscrollspeed step (n=4)
5721 | None ->
5722 if conf.wheelbypage
5723 then (
5724 if n = 4
5725 then nextpage ()
5726 else prevpage ()
5728 else
5729 let incr =
5730 if n = 4
5731 then -conf.scrollstep
5732 else conf.scrollstep
5734 let incr = incr * 2 in
5735 let y = clamp incr in
5736 gotoy_and_clear_text y
5739 | n when (n = 6 || n = 7) && not down && canpan () ->
5740 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5741 gotoy_and_clear_text state.y
5743 | 1 when Wsi.withctrl mask ->
5744 if down
5745 then (
5746 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5747 state.mstate <- Mpan (x, y)
5749 else
5750 state.mstate <- Mnone
5752 | 3 ->
5753 if down
5754 then (
5755 Wsi.setcursor Wsi.CURSOR_CYCLE;
5756 let p = (x, y) in
5757 state.mstate <- Mzoomrect (p, p)
5759 else (
5760 match state.mstate with
5761 | Mzoomrect ((x0, y0), _) ->
5762 if abs (x-x0) > 10 && abs (y - y0) > 10
5763 then zoomrect x0 y0 x y
5764 else (
5765 state.mstate <- Mnone;
5766 Wsi.setcursor Wsi.CURSOR_INHERIT;
5767 G.postRedisplay "kill accidental zoom rect";
5769 | _ ->
5770 Wsi.setcursor Wsi.CURSOR_INHERIT;
5771 state.mstate <- Mnone
5774 | 1 when x > conf.winw - state.scrollw ->
5775 if down
5776 then
5777 let _, position, sh = state.uioh#scrollph in
5778 if y > truncate position && y < truncate (position +. sh)
5779 then state.mstate <- Mscrolly
5780 else scrolly y
5781 else
5782 state.mstate <- Mnone
5784 | 1 when y > conf.winh - state.hscrollh ->
5785 if down
5786 then
5787 let _, position, sw = state.uioh#scrollpw in
5788 if x > truncate position && x < truncate (position +. sw)
5789 then state.mstate <- Mscrollx
5790 else scrollx x
5791 else
5792 state.mstate <- Mnone
5794 | 1 ->
5795 let dest = if down then getunder x y else Unone in
5796 begin match dest with
5797 | Ulinkgoto _
5798 | Ulinkuri _
5799 | Uremote _
5800 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5801 gotounder dest
5803 | Unone when down ->
5804 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5805 state.mstate <- Mpan (x, y);
5807 | Unone | Utext _ ->
5808 if down
5809 then (
5810 if conf.angle mod 360 = 0
5811 then (
5812 state.mstate <- Msel ((x, y), (x, y));
5813 G.postRedisplay "mouse select";
5816 else (
5817 match state.mstate with
5818 | Mnone -> ()
5820 | Mzoom _ | Mscrollx | Mscrolly ->
5821 state.mstate <- Mnone
5823 | Mzoomrect ((x0, y0), _) ->
5824 zoomrect x0 y0 x y
5826 | Mpan _ ->
5827 Wsi.setcursor Wsi.CURSOR_INHERIT;
5828 state.mstate <- Mnone
5830 | Msel ((x0, y0), (x1, y1)) ->
5831 let rec loop = function
5832 | [] -> ()
5833 | l :: rest ->
5834 let inside =
5835 let a0 = l.pagedispy in
5836 let a1 = a0 + l.pagevh in
5837 let b0 = l.pagedispx in
5838 let b1 = b0 + l.pagevw in
5839 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5840 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5842 if inside
5843 then
5844 match getopaque l.pageno with
5845 | Some opaque ->
5846 begin
5847 match Ne.pipe () with
5848 | Ne.Exn exn ->
5849 showtext '!'
5850 (Printf.sprintf
5851 "can not create sel pipe: %s"
5852 (Printexc.to_string exn));
5853 | Ne.Res (r, w) ->
5854 let doclose what fd =
5855 Ne.clo fd (fun msg ->
5856 dolog "%s close failed: %s" what msg)
5859 popen conf.selcmd [r, 0; w, -1];
5860 copysel w opaque;
5861 doclose "pipe/r" r;
5862 G.postRedisplay "copysel";
5863 with exn ->
5864 dolog "can not execute %S: %s"
5865 conf.selcmd (Printexc.to_string exn);
5866 doclose "pipe/r" r;
5867 doclose "pipe/w" w;
5869 | None -> ()
5870 else loop rest
5872 loop state.layout;
5873 Wsi.setcursor Wsi.CURSOR_INHERIT;
5874 state.mstate <- Mnone;
5878 | _ -> ()
5881 let birdseyemouse button down x y mask
5882 (conf, leftx, _, hooverpageno, anchor) =
5883 match button with
5884 | 1 when down ->
5885 let rec loop = function
5886 | [] -> ()
5887 | l :: rest ->
5888 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5889 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5890 then (
5891 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5893 else loop rest
5895 loop state.layout
5896 | 3 -> ()
5897 | _ -> viewmouse button down x y mask
5900 let mouse button down x y mask =
5901 state.uioh <- state.uioh#button button down x y mask;
5904 let motion ~x ~y =
5905 state.uioh <- state.uioh#motion x y
5908 let pmotion ~x ~y =
5909 state.uioh <- state.uioh#pmotion x y;
5912 let uioh = object
5913 method display = ()
5915 method key key mask =
5916 begin match state.mode with
5917 | Textentry textentry -> textentrykeyboard key mask textentry
5918 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5919 | View -> viewkeyboard key mask
5920 | LinkNav linknav -> linknavkeyboard key mask linknav
5921 end;
5922 state.uioh
5924 method button button bstate x y mask =
5925 begin match state.mode with
5926 | LinkNav _
5927 | View -> viewmouse button bstate x y mask
5928 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5929 | Textentry _ -> ()
5930 end;
5931 state.uioh
5933 method motion x y =
5934 begin match state.mode with
5935 | Textentry _ -> ()
5936 | View | Birdseye _ | LinkNav _ ->
5937 match state.mstate with
5938 | Mzoom _ | Mnone -> ()
5940 | Mpan (x0, y0) ->
5941 let dx = x - x0
5942 and dy = y0 - y in
5943 state.mstate <- Mpan (x, y);
5944 if canpan ()
5945 then state.x <- state.x + dx;
5946 let y = clamp dy in
5947 gotoy_and_clear_text y
5949 | Msel (a, _) ->
5950 state.mstate <- Msel (a, (x, y));
5951 G.postRedisplay "motion select";
5953 | Mscrolly ->
5954 let y = min conf.winh (max 0 y) in
5955 scrolly y
5957 | Mscrollx ->
5958 let x = min conf.winw (max 0 x) in
5959 scrollx x
5961 | Mzoomrect (p0, _) ->
5962 state.mstate <- Mzoomrect (p0, (x, y));
5963 G.postRedisplay "motion zoomrect";
5964 end;
5965 state.uioh
5967 method pmotion x y =
5968 begin match state.mode with
5969 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5970 let rec loop = function
5971 | [] ->
5972 if hooverpageno != -1
5973 then (
5974 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5975 G.postRedisplay "pmotion birdseye no hoover";
5977 | l :: rest ->
5978 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5979 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5980 then (
5981 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5982 G.postRedisplay "pmotion birdseye hoover";
5984 else loop rest
5986 loop state.layout
5988 | Textentry _ -> ()
5990 | LinkNav _
5991 | View ->
5992 match state.mstate with
5993 | Mnone -> updateunder x y
5994 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5996 end;
5997 state.uioh
5999 method infochanged _ = ()
6001 method scrollph =
6002 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
6003 let p, h = scrollph state.y maxy in
6004 state.scrollw, p, h
6006 method scrollpw =
6007 let winw = conf.winw - state.scrollw - 1 in
6008 let fwinw = float winw in
6009 let sw =
6010 let sw = fwinw /. float state.w in
6011 let sw = fwinw *. sw in
6012 max sw (float conf.scrollh)
6014 let position, sw =
6015 let f = state.w+winw in
6016 let r = float (winw-state.x) /. float f in
6017 let p = fwinw *. r in
6018 p-.sw/.2., sw
6020 let sw =
6021 if position +. sw > fwinw
6022 then fwinw -. position
6023 else sw
6025 state.hscrollh, position, sw
6027 method modehash =
6028 let modename =
6029 match state.mode with
6030 | LinkNav _ -> "links"
6031 | Textentry _ -> "textentry"
6032 | Birdseye _ -> "birdseye"
6033 | View -> "view"
6035 findkeyhash conf modename
6036 end;;
6038 module Config =
6039 struct
6040 open Parser
6042 let fontpath = ref "";;
6044 module KeyMap =
6045 Map.Make (struct type t = (int * int) let compare = compare end);;
6047 let unent s =
6048 let l = String.length s in
6049 let b = Buffer.create l in
6050 unent b s 0 l;
6051 Buffer.contents b;
6054 let home =
6055 try Sys.getenv "HOME"
6056 with exn ->
6057 prerr_endline
6058 ("Can not determine home directory location: " ^
6059 Printexc.to_string exn);
6063 let modifier_of_string = function
6064 | "alt" -> Wsi.altmask
6065 | "shift" -> Wsi.shiftmask
6066 | "ctrl" | "control" -> Wsi.ctrlmask
6067 | "meta" -> Wsi.metamask
6068 | _ -> 0
6071 let key_of_string =
6072 let r = Str.regexp "-" in
6073 fun s ->
6074 let elems = Str.full_split r s in
6075 let f n k m =
6076 let g s =
6077 let m1 = modifier_of_string s in
6078 if m1 = 0
6079 then (Wsi.namekey s, m)
6080 else (k, m lor m1)
6081 in function
6082 | Str.Delim s when n land 1 = 0 -> g s
6083 | Str.Text s -> g s
6084 | Str.Delim _ -> (k, m)
6086 let rec loop n k m = function
6087 | [] -> (k, m)
6088 | x :: xs ->
6089 let k, m = f n k m x in
6090 loop (n+1) k m xs
6092 loop 0 0 0 elems
6095 let keys_of_string =
6096 let r = Str.regexp "[ \t]" in
6097 fun s ->
6098 let elems = Str.split r s in
6099 List.map key_of_string elems
6102 let copykeyhashes c =
6103 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6106 let config_of c attrs =
6107 let apply c k v =
6109 match k with
6110 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6111 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6112 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6113 | "preload" -> { c with preload = bool_of_string v }
6114 | "page-bias" -> { c with pagebias = int_of_string v }
6115 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6116 | "horizontal-scroll-step" ->
6117 { c with hscrollstep = max (int_of_string v) 1 }
6118 | "auto-scroll-step" ->
6119 { c with autoscrollstep = max 0 (int_of_string v) }
6120 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6121 | "crop-hack" -> { c with crophack = bool_of_string v }
6122 | "throttle" ->
6123 let mw =
6124 match String.lowercase v with
6125 | "true" -> Some infinity
6126 | "false" -> None
6127 | f -> Some (float_of_string f)
6129 { c with maxwait = mw}
6130 | "highlight-links" -> { c with hlinks = bool_of_string v }
6131 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6132 | "vertical-margin" ->
6133 { c with interpagespace = max 0 (int_of_string v) }
6134 | "zoom" ->
6135 let zoom = float_of_string v /. 100. in
6136 let zoom = max zoom 0.0 in
6137 { c with zoom = zoom }
6138 | "presentation" -> { c with presentation = bool_of_string v }
6139 | "rotation-angle" -> { c with angle = int_of_string v }
6140 | "width" -> { c with winw = max 20 (int_of_string v) }
6141 | "height" -> { c with winh = max 20 (int_of_string v) }
6142 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6143 | "proportional-display" -> { c with proportional = bool_of_string v }
6144 | "pixmap-cache-size" ->
6145 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6146 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6147 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6148 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6149 | "persistent-location" -> { c with jumpback = bool_of_string v }
6150 | "background-color" -> { c with bgcolor = color_of_string v }
6151 | "scrollbar-in-presentation" ->
6152 { c with scrollbarinpm = bool_of_string v }
6153 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6154 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6155 | "mupdf-store-size" ->
6156 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6157 | "checkers" -> { c with checkers = bool_of_string v }
6158 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6159 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6160 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6161 | "uri-launcher" -> { c with urilauncher = unent v }
6162 | "path-launcher" -> { c with pathlauncher = unent v }
6163 | "color-space" -> { c with colorspace = colorspace_of_string v }
6164 | "invert-colors" -> { c with invert = bool_of_string v }
6165 | "brightness" -> { c with colorscale = float_of_string v }
6166 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6167 | "ghyllscroll" ->
6168 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6169 | "columns" ->
6170 let (n, _, _) as nab = multicolumns_of_string v in
6171 if n < 0
6172 then { c with columns = Csplit (-n, [||]) }
6173 else { c with columns = Cmulti (nab, [||]) }
6174 | "birds-eye-columns" ->
6175 { c with beyecolumns = Some (max (int_of_string v) 2) }
6176 | "selection-command" -> { c with selcmd = unent v }
6177 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6178 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6179 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6180 | "use-pbo" -> { c with usepbo = bool_of_string v }
6181 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6182 | _ -> c
6183 with exn ->
6184 prerr_endline ("Error processing attribute (`" ^
6185 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6188 let rec fold c = function
6189 | [] -> c
6190 | (k, v) :: rest ->
6191 let c = apply c k v in
6192 fold c rest
6194 fold { c with keyhashes = copykeyhashes c } attrs;
6197 let fromstring f pos n v d =
6198 try f v
6199 with exn ->
6200 dolog "Error processing attribute (%S=%S) at %d\n%s"
6201 n v pos (Printexc.to_string exn)
6206 let bookmark_of attrs =
6207 let rec fold title page rely visy = function
6208 | ("title", v) :: rest -> fold v page rely visy rest
6209 | ("page", v) :: rest -> fold title v rely visy rest
6210 | ("rely", v) :: rest -> fold title page v visy rest
6211 | ("visy", v) :: rest -> fold title page rely v rest
6212 | _ :: rest -> fold title page rely visy rest
6213 | [] -> title, page, rely, visy
6215 fold "invalid" "0" "0" "0" attrs
6218 let doc_of attrs =
6219 let rec fold path page rely pan visy = function
6220 | ("path", v) :: rest -> fold v page rely pan visy rest
6221 | ("page", v) :: rest -> fold path v rely pan visy rest
6222 | ("rely", v) :: rest -> fold path page v pan visy rest
6223 | ("pan", v) :: rest -> fold path page rely v visy rest
6224 | ("visy", v) :: rest -> fold path page rely pan v rest
6225 | _ :: rest -> fold path page rely pan visy rest
6226 | [] -> path, page, rely, pan, visy
6228 fold "" "0" "0" "0" "0" attrs
6231 let map_of attrs =
6232 let rec fold rs ls = function
6233 | ("out", v) :: rest -> fold v ls rest
6234 | ("in", v) :: rest -> fold rs v rest
6235 | _ :: rest -> fold ls rs rest
6236 | [] -> ls, rs
6238 fold "" "" attrs
6241 let setconf dst src =
6242 dst.scrollbw <- src.scrollbw;
6243 dst.scrollh <- src.scrollh;
6244 dst.icase <- src.icase;
6245 dst.preload <- src.preload;
6246 dst.pagebias <- src.pagebias;
6247 dst.verbose <- src.verbose;
6248 dst.scrollstep <- src.scrollstep;
6249 dst.maxhfit <- src.maxhfit;
6250 dst.crophack <- src.crophack;
6251 dst.autoscrollstep <- src.autoscrollstep;
6252 dst.maxwait <- src.maxwait;
6253 dst.hlinks <- src.hlinks;
6254 dst.underinfo <- src.underinfo;
6255 dst.interpagespace <- src.interpagespace;
6256 dst.zoom <- src.zoom;
6257 dst.presentation <- src.presentation;
6258 dst.angle <- src.angle;
6259 dst.winw <- src.winw;
6260 dst.winh <- src.winh;
6261 dst.savebmarks <- src.savebmarks;
6262 dst.memlimit <- src.memlimit;
6263 dst.proportional <- src.proportional;
6264 dst.texcount <- src.texcount;
6265 dst.sliceheight <- src.sliceheight;
6266 dst.thumbw <- src.thumbw;
6267 dst.jumpback <- src.jumpback;
6268 dst.bgcolor <- src.bgcolor;
6269 dst.scrollbarinpm <- src.scrollbarinpm;
6270 dst.tilew <- src.tilew;
6271 dst.tileh <- src.tileh;
6272 dst.mustoresize <- src.mustoresize;
6273 dst.checkers <- src.checkers;
6274 dst.aalevel <- src.aalevel;
6275 dst.trimmargins <- src.trimmargins;
6276 dst.trimfuzz <- src.trimfuzz;
6277 dst.urilauncher <- src.urilauncher;
6278 dst.colorspace <- src.colorspace;
6279 dst.invert <- src.invert;
6280 dst.colorscale <- src.colorscale;
6281 dst.redirectstderr <- src.redirectstderr;
6282 dst.ghyllscroll <- src.ghyllscroll;
6283 dst.columns <- src.columns;
6284 dst.beyecolumns <- src.beyecolumns;
6285 dst.selcmd <- src.selcmd;
6286 dst.updatecurs <- src.updatecurs;
6287 dst.pathlauncher <- src.pathlauncher;
6288 dst.keyhashes <- copykeyhashes src;
6289 dst.hfsize <- src.hfsize;
6290 dst.hscrollstep <- src.hscrollstep;
6291 dst.pgscale <- src.pgscale;
6292 dst.usepbo <- src.usepbo;
6293 dst.wheelbypage <- src.wheelbypage;
6296 let get s =
6297 let h = Hashtbl.create 10 in
6298 let dc = { defconf with angle = defconf.angle } in
6299 let rec toplevel v t spos _ =
6300 match t with
6301 | Vdata | Vcdata | Vend -> v
6302 | Vopen ("llppconfig", _, closed) ->
6303 if closed
6304 then v
6305 else { v with f = llppconfig }
6306 | Vopen _ ->
6307 error "unexpected subelement at top level" s spos
6308 | Vclose _ -> error "unexpected close at top level" s spos
6310 and llppconfig v t spos _ =
6311 match t with
6312 | Vdata | Vcdata -> v
6313 | Vend -> error "unexpected end of input in llppconfig" s spos
6314 | Vopen ("defaults", attrs, closed) ->
6315 let c = config_of dc attrs in
6316 setconf dc c;
6317 if closed
6318 then v
6319 else { v with f = defaults }
6321 | Vopen ("ui-font", attrs, closed) ->
6322 let rec getsize size = function
6323 | [] -> size
6324 | ("size", v) :: rest ->
6325 let size =
6326 fromstring int_of_string spos "size" v fstate.fontsize in
6327 getsize size rest
6328 | l -> getsize size l
6330 fstate.fontsize <- getsize fstate.fontsize attrs;
6331 if closed
6332 then v
6333 else { v with f = uifont (Buffer.create 10) }
6335 | Vopen ("doc", attrs, closed) ->
6336 let pathent, spage, srely, span, svisy = doc_of attrs in
6337 let path = unent pathent
6338 and pageno = fromstring int_of_string spos "page" spage 0
6339 and rely = fromstring float_of_string spos "rely" srely 0.0
6340 and pan = fromstring int_of_string spos "pan" span 0
6341 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6342 let c = config_of dc attrs in
6343 let anchor = (pageno, rely, visy) in
6344 if closed
6345 then (Hashtbl.add h path (c, [], pan, anchor); v)
6346 else { v with f = doc path pan anchor c [] }
6348 | Vopen _ ->
6349 error "unexpected subelement in llppconfig" s spos
6351 | Vclose "llppconfig" -> { v with f = toplevel }
6352 | Vclose _ -> error "unexpected close in llppconfig" s spos
6354 and defaults v t spos _ =
6355 match t with
6356 | Vdata | Vcdata -> v
6357 | Vend -> error "unexpected end of input in defaults" s spos
6358 | Vopen ("keymap", attrs, closed) ->
6359 let modename =
6360 try List.assoc "mode" attrs
6361 with Not_found -> "global" in
6362 if closed
6363 then v
6364 else
6365 let ret keymap =
6366 let h = findkeyhash dc modename in
6367 KeyMap.iter (Hashtbl.replace h) keymap;
6368 defaults
6370 { v with f = pkeymap ret KeyMap.empty }
6372 | Vopen (_, _, _) ->
6373 error "unexpected subelement in defaults" s spos
6375 | Vclose "defaults" ->
6376 { v with f = llppconfig }
6378 | Vclose _ -> error "unexpected close in defaults" s spos
6380 and uifont b v t spos epos =
6381 match t with
6382 | Vdata | Vcdata ->
6383 Buffer.add_substring b s spos (epos - spos);
6385 | Vopen (_, _, _) ->
6386 error "unexpected subelement in ui-font" s spos
6387 | Vclose "ui-font" ->
6388 if String.length !fontpath = 0
6389 then fontpath := Buffer.contents b;
6390 { v with f = llppconfig }
6391 | Vclose _ -> error "unexpected close in ui-font" s spos
6392 | Vend -> error "unexpected end of input in ui-font" s spos
6394 and doc path pan anchor c bookmarks v t spos _ =
6395 match t with
6396 | Vdata | Vcdata -> v
6397 | Vend -> error "unexpected end of input in doc" s spos
6398 | Vopen ("bookmarks", _, closed) ->
6399 if closed
6400 then v
6401 else { v with f = pbookmarks path pan anchor c bookmarks }
6403 | Vopen ("keymap", attrs, closed) ->
6404 let modename =
6405 try List.assoc "mode" attrs
6406 with Not_found -> "global"
6408 if closed
6409 then v
6410 else
6411 let ret keymap =
6412 let h = findkeyhash c modename in
6413 KeyMap.iter (Hashtbl.replace h) keymap;
6414 doc path pan anchor c bookmarks
6416 { v with f = pkeymap ret KeyMap.empty }
6418 | Vopen (_, _, _) ->
6419 error "unexpected subelement in doc" s spos
6421 | Vclose "doc" ->
6422 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6423 { v with f = llppconfig }
6425 | Vclose _ -> error "unexpected close in doc" s spos
6427 and pkeymap ret keymap v t spos _ =
6428 match t with
6429 | Vdata | Vcdata -> v
6430 | Vend -> error "unexpected end of input in keymap" s spos
6431 | Vopen ("map", attrs, closed) ->
6432 let r, l = map_of attrs in
6433 let kss = fromstring keys_of_string spos "in" r [] in
6434 let lss = fromstring keys_of_string spos "out" l [] in
6435 let keymap =
6436 match kss with
6437 | [] -> keymap
6438 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6439 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6441 if closed
6442 then { v with f = pkeymap ret keymap }
6443 else
6444 let f () = v in
6445 { v with f = skip "map" f }
6447 | Vopen _ ->
6448 error "unexpected subelement in keymap" s spos
6450 | Vclose "keymap" ->
6451 { v with f = ret keymap }
6453 | Vclose _ -> error "unexpected close in keymap" s spos
6455 and pbookmarks path pan anchor c bookmarks v t spos _ =
6456 match t with
6457 | Vdata | Vcdata -> v
6458 | Vend -> error "unexpected end of input in bookmarks" s spos
6459 | Vopen ("item", attrs, closed) ->
6460 let titleent, spage, srely, svisy = bookmark_of attrs in
6461 let page = fromstring int_of_string spos "page" spage 0
6462 and rely = fromstring float_of_string spos "rely" srely 0.0
6463 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6464 let bookmarks =
6465 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6467 if closed
6468 then { v with f = pbookmarks path pan anchor c bookmarks }
6469 else
6470 let f () = v in
6471 { v with f = skip "item" f }
6473 | Vopen _ ->
6474 error "unexpected subelement in bookmarks" s spos
6476 | Vclose "bookmarks" ->
6477 { v with f = doc path pan anchor c bookmarks }
6479 | Vclose _ -> error "unexpected close in bookmarks" s spos
6481 and skip tag f v t spos _ =
6482 match t with
6483 | Vdata | Vcdata -> v
6484 | Vend ->
6485 error ("unexpected end of input in skipped " ^ tag) s spos
6486 | Vopen (tag', _, closed) ->
6487 if closed
6488 then v
6489 else
6490 let f' () = { v with f = skip tag f } in
6491 { v with f = skip tag' f' }
6492 | Vclose ctag ->
6493 if tag = ctag
6494 then f ()
6495 else error ("unexpected close in skipped " ^ tag) s spos
6498 parse { f = toplevel; accu = () } s;
6499 h, dc;
6502 let do_load f ic =
6504 let len = in_channel_length ic in
6505 let s = String.create len in
6506 really_input ic s 0 len;
6507 f s;
6508 with
6509 | Parse_error (msg, s, pos) ->
6510 let subs = subs s pos in
6511 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6512 failwith ("parse error: " ^ s)
6514 | exn ->
6515 failwith ("config load error: " ^ Printexc.to_string exn)
6518 let defconfpath =
6519 let dir =
6521 let dir = Filename.concat home ".config" in
6522 if Sys.is_directory dir then dir else home
6523 with _ -> home
6525 Filename.concat dir "llpp.conf"
6528 let confpath = ref defconfpath;;
6530 let load1 f =
6531 if Sys.file_exists !confpath
6532 then
6533 match
6534 (try Some (open_in_bin !confpath)
6535 with exn ->
6536 prerr_endline
6537 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6538 Printexc.to_string exn);
6539 None
6541 with
6542 | Some ic ->
6543 let success =
6545 f (do_load get ic)
6546 with exn ->
6547 prerr_endline
6548 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6549 Printexc.to_string exn);
6550 false
6552 close_in ic;
6553 success
6555 | None -> false
6556 else
6557 f (Hashtbl.create 0, defconf)
6560 let load () =
6561 let f (h, dc) =
6562 let pc, pb, px, pa =
6564 Hashtbl.find h (Filename.basename state.path)
6565 with Not_found -> dc, [], 0, emptyanchor
6567 setconf defconf dc;
6568 setconf conf pc;
6569 state.bookmarks <- pb;
6570 state.x <- px;
6571 state.scrollw <- conf.scrollbw;
6572 if conf.jumpback
6573 then state.anchor <- pa;
6574 cbput state.hists.nav pa;
6575 true
6577 load1 f
6580 let add_attrs bb always dc c =
6581 let ob s a b =
6582 if always || a != b
6583 then Printf.bprintf bb "\n %s='%b'" s a
6584 and oi s a b =
6585 if always || a != b
6586 then Printf.bprintf bb "\n %s='%d'" s a
6587 and oI s a b =
6588 if always || a != b
6589 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6590 and oz s a b =
6591 if always || a <> b
6592 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6593 and oF s a b =
6594 if always || a <> b
6595 then Printf.bprintf bb "\n %s='%f'" s a
6596 and oc s a b =
6597 if always || a <> b
6598 then
6599 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6600 and oC s a b =
6601 if always || a <> b
6602 then
6603 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6604 and oR s a b =
6605 if always || a <> b
6606 then
6607 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6608 and os s a b =
6609 if always || a <> b
6610 then
6611 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6612 and og s a b =
6613 if always || a <> b
6614 then
6615 match a with
6616 | None -> ()
6617 | Some (_N, _A, _B) ->
6618 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6619 and oW s a b =
6620 if always || a <> b
6621 then
6622 let v =
6623 match a with
6624 | None -> "false"
6625 | Some f ->
6626 if f = infinity
6627 then "true"
6628 else string_of_float f
6630 Printf.bprintf bb "\n %s='%s'" s v
6631 and oco s a b =
6632 if always || a <> b
6633 then
6634 match a with
6635 | Cmulti ((n, a, b), _) when n > 1 ->
6636 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6637 | Csplit (n, _) when n > 1 ->
6638 Printf.bprintf bb "\n %s='%d'" s ~-n
6639 | _ -> ()
6640 and obeco s a b =
6641 if always || a <> b
6642 then
6643 match a with
6644 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6645 | _ -> ()
6647 let w, h =
6648 if always
6649 then dc.winw, dc.winh
6650 else
6651 match state.fullscreen with
6652 | Some wh -> wh
6653 | None -> c.winw, c.winh
6655 oi "width" w dc.winw;
6656 oi "height" h dc.winh;
6657 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6658 oi "scroll-handle-height" c.scrollh dc.scrollh;
6659 ob "case-insensitive-search" c.icase dc.icase;
6660 ob "preload" c.preload dc.preload;
6661 oi "page-bias" c.pagebias dc.pagebias;
6662 oi "scroll-step" c.scrollstep dc.scrollstep;
6663 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6664 ob "max-height-fit" c.maxhfit dc.maxhfit;
6665 ob "crop-hack" c.crophack dc.crophack;
6666 oW "throttle" c.maxwait dc.maxwait;
6667 ob "highlight-links" c.hlinks dc.hlinks;
6668 ob "under-cursor-info" c.underinfo dc.underinfo;
6669 oi "vertical-margin" c.interpagespace dc.interpagespace;
6670 oz "zoom" c.zoom dc.zoom;
6671 ob "presentation" c.presentation dc.presentation;
6672 oi "rotation-angle" c.angle dc.angle;
6673 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6674 ob "proportional-display" c.proportional dc.proportional;
6675 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6676 oi "tex-count" c.texcount dc.texcount;
6677 oi "slice-height" c.sliceheight dc.sliceheight;
6678 oi "thumbnail-width" c.thumbw dc.thumbw;
6679 ob "persistent-location" c.jumpback dc.jumpback;
6680 oc "background-color" c.bgcolor dc.bgcolor;
6681 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6682 oi "tile-width" c.tilew dc.tilew;
6683 oi "tile-height" c.tileh dc.tileh;
6684 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6685 ob "checkers" c.checkers dc.checkers;
6686 oi "aalevel" c.aalevel dc.aalevel;
6687 ob "trim-margins" c.trimmargins dc.trimmargins;
6688 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6689 os "uri-launcher" c.urilauncher dc.urilauncher;
6690 os "path-launcher" c.pathlauncher dc.pathlauncher;
6691 oC "color-space" c.colorspace dc.colorspace;
6692 ob "invert-colors" c.invert dc.invert;
6693 oF "brightness" c.colorscale dc.colorscale;
6694 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6695 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6696 oco "columns" c.columns dc.columns;
6697 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6698 os "selection-command" c.selcmd dc.selcmd;
6699 ob "update-cursor" c.updatecurs dc.updatecurs;
6700 oi "hint-font-size" c.hfsize dc.hfsize;
6701 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6702 oF "page-scroll-scale" c.pgscale dc.pgscale;
6703 ob "use-pbo" c.usepbo dc.usepbo;
6704 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6707 let keymapsbuf always dc c =
6708 let bb = Buffer.create 16 in
6709 let rec loop = function
6710 | [] -> ()
6711 | (modename, h) :: rest ->
6712 let dh = findkeyhash dc modename in
6713 if always || h <> dh
6714 then (
6715 if Hashtbl.length h > 0
6716 then (
6717 if Buffer.length bb > 0
6718 then Buffer.add_char bb '\n';
6719 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6720 Hashtbl.iter (fun i o ->
6721 let isdifferent = always ||
6723 let dO = Hashtbl.find dh i in
6724 dO <> o
6725 with Not_found -> true
6727 if isdifferent
6728 then
6729 let addkm (k, m) =
6730 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6731 if Wsi.withalt m then Buffer.add_string bb "alt-";
6732 if Wsi.withshift m then Buffer.add_string bb "shift-";
6733 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6734 Buffer.add_string bb (Wsi.keyname k);
6736 let addkms l =
6737 let rec loop = function
6738 | [] -> ()
6739 | km :: [] -> addkm km
6740 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6742 loop l
6744 Buffer.add_string bb "<map in='";
6745 addkm i;
6746 match o with
6747 | KMinsrt km ->
6748 Buffer.add_string bb "' out='";
6749 addkm km;
6750 Buffer.add_string bb "'/>\n"
6752 | KMinsrl kms ->
6753 Buffer.add_string bb "' out='";
6754 addkms kms;
6755 Buffer.add_string bb "'/>\n"
6757 | KMmulti (ins, kms) ->
6758 Buffer.add_char bb ' ';
6759 addkms ins;
6760 Buffer.add_string bb "' out='";
6761 addkms kms;
6762 Buffer.add_string bb "'/>\n"
6763 ) h;
6764 Buffer.add_string bb "</keymap>";
6767 loop rest
6769 loop c.keyhashes;
6773 let save () =
6774 let uifontsize = fstate.fontsize in
6775 let bb = Buffer.create 32768 in
6776 let f (h, dc) =
6777 let dc = if conf.bedefault then conf else dc in
6778 Buffer.add_string bb "<llppconfig>\n";
6780 if String.length !fontpath > 0
6781 then
6782 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6783 uifontsize
6784 !fontpath
6785 else (
6786 if uifontsize <> 14
6787 then
6788 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6791 Buffer.add_string bb "<defaults ";
6792 add_attrs bb true dc dc;
6793 let kb = keymapsbuf true dc dc in
6794 if Buffer.length kb > 0
6795 then (
6796 Buffer.add_string bb ">\n";
6797 Buffer.add_buffer bb kb;
6798 Buffer.add_string bb "\n</defaults>\n";
6800 else Buffer.add_string bb "/>\n";
6802 let adddoc path pan anchor c bookmarks =
6803 if bookmarks == [] && c = dc && anchor = emptyanchor
6804 then ()
6805 else (
6806 Printf.bprintf bb "<doc path='%s'"
6807 (enent path 0 (String.length path));
6809 if anchor <> emptyanchor
6810 then (
6811 let n, rely, visy = anchor in
6812 Printf.bprintf bb " page='%d'" n;
6813 if rely > 1e-6
6814 then
6815 Printf.bprintf bb " rely='%f'" rely
6817 if abs_float visy > 1e-6
6818 then
6819 Printf.bprintf bb " visy='%f'" visy
6823 if pan != 0
6824 then Printf.bprintf bb " pan='%d'" pan;
6826 add_attrs bb false dc c;
6827 let kb = keymapsbuf false dc c in
6829 begin match bookmarks with
6830 | [] ->
6831 if Buffer.length kb > 0
6832 then (
6833 Buffer.add_string bb ">\n";
6834 Buffer.add_buffer bb kb;
6835 Buffer.add_string bb "\n</doc>\n";
6837 else Buffer.add_string bb "/>\n"
6838 | _ ->
6839 Buffer.add_string bb ">\n<bookmarks>\n";
6840 List.iter (fun (title, _level, (page, rely, visy)) ->
6841 Printf.bprintf bb
6842 "<item title='%s' page='%d'"
6843 (enent title 0 (String.length title))
6844 page
6846 if rely > 1e-6
6847 then
6848 Printf.bprintf bb " rely='%f'" rely
6850 if abs_float visy > 1e-6
6851 then
6852 Printf.bprintf bb " visy='%f'" visy
6854 Buffer.add_string bb "/>\n";
6855 ) bookmarks;
6856 Buffer.add_string bb "</bookmarks>";
6857 if Buffer.length kb > 0
6858 then (
6859 Buffer.add_string bb "\n";
6860 Buffer.add_buffer bb kb;
6862 Buffer.add_string bb "\n</doc>\n";
6863 end;
6867 let pan, conf =
6868 match state.mode with
6869 | Birdseye (c, pan, _, _, _) ->
6870 let beyecolumns =
6871 match conf.columns with
6872 | Cmulti ((c, _, _), _) -> Some c
6873 | Csingle _ -> None
6874 | Csplit _ -> None
6875 and columns =
6876 match c.columns with
6877 | Cmulti (c, _) -> Cmulti (c, [||])
6878 | Csingle _ -> Csingle [||]
6879 | Csplit _ -> failwith "quit from bird's eye while split"
6881 pan, { c with beyecolumns = beyecolumns; columns = columns }
6882 | _ -> state.x, conf
6884 let basename = Filename.basename state.path in
6885 adddoc basename pan (getanchor ())
6886 (let conf =
6887 let autoscrollstep =
6888 match state.autoscroll with
6889 | Some step -> step
6890 | None -> conf.autoscrollstep
6892 match state.mode with
6893 | Birdseye (bc, _, _, _, _) ->
6894 { conf with
6895 zoom = bc.zoom;
6896 presentation = bc.presentation;
6897 interpagespace = bc.interpagespace;
6898 maxwait = bc.maxwait;
6899 autoscrollstep = autoscrollstep }
6900 | _ -> { conf with autoscrollstep = autoscrollstep }
6901 in conf)
6902 (if conf.savebmarks then state.bookmarks else []);
6904 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6905 if basename <> path
6906 then adddoc path x anchor c bookmarks
6907 ) h;
6908 Buffer.add_string bb "</llppconfig>\n";
6909 true;
6911 if load1 f && Buffer.length bb > 0
6912 then
6914 let tmp = !confpath ^ ".tmp" in
6915 let oc = open_out_bin tmp in
6916 Buffer.output_buffer oc bb;
6917 close_out oc;
6918 Unix.rename tmp !confpath;
6919 with exn ->
6920 prerr_endline
6921 ("error while saving configuration: " ^ Printexc.to_string exn)
6923 end;;
6925 let () =
6926 let trimcachepath = ref "" in
6927 Arg.parse
6928 (Arg.align
6929 [("-p", Arg.String (fun s -> state.password <- s) ,
6930 "<password> Set password");
6932 ("-f", Arg.String (fun s -> Config.fontpath := s),
6933 "<path> Set path to the user interface font");
6935 ("-c", Arg.String (fun s -> Config.confpath := s),
6936 "<path> Set path to the configuration file");
6938 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6939 "<path> Set path to the trim cache file");
6941 ("-v", Arg.Unit (fun () ->
6942 Printf.printf
6943 "%s\nconfiguration path: %s\n"
6944 (version ())
6945 Config.defconfpath
6947 exit 0), " Print version and exit");
6950 (fun s -> state.path <- s)
6951 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6953 if String.length state.path = 0
6954 then (prerr_endline "file name missing"; exit 1);
6956 if not (Config.load ())
6957 then prerr_endline "failed to load configuration";
6959 let globalkeyhash = findkeyhash conf "global" in
6960 let wsfd, winw, winh = Wsi.init (object
6961 method expose =
6962 state.wthack <- false;
6963 if nogeomcmds state.geomcmds || platform == Posx
6964 then display ()
6965 else (
6966 GlClear.color (scalecolor2 conf.bgcolor);
6967 GlClear.clear [`color];
6969 method display = display ()
6970 method reshape w h = reshape w h
6971 method mouse b d x y m = mouse b d x y m
6972 method motion x y = state.mpos <- (x, y); motion x y
6973 method pmotion x y = state.mpos <- (x, y); pmotion x y
6974 method key k m =
6975 let mascm = m land (
6976 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6977 ) in
6978 match state.keystate with
6979 | KSnone ->
6980 let km = k, mascm in
6981 begin
6982 match
6983 let modehash = state.uioh#modehash in
6984 try Hashtbl.find modehash km
6985 with Not_found ->
6986 try Hashtbl.find globalkeyhash km
6987 with Not_found -> KMinsrt (k, m)
6988 with
6989 | KMinsrt (k, m) -> keyboard k m
6990 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6991 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6993 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6994 List.iter (fun (k, m) -> keyboard k m) insrt;
6995 state.keystate <- KSnone
6996 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6997 state.keystate <- KSinto (keys, insrt)
6998 | _ ->
6999 state.keystate <- KSnone
7001 method enter x y = state.mpos <- (x, y); pmotion x y
7002 method leave = state.mpos <- (-1, -1)
7003 method quit = raise Quit
7004 end) conf.winw conf.winh (platform = Posx) in
7006 state.wsfd <- wsfd;
7008 if not (
7009 List.exists GlMisc.check_extension
7010 [ "GL_ARB_texture_rectangle"
7011 ; "GL_EXT_texture_recangle"
7012 ; "GL_NV_texture_rectangle" ]
7014 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7016 let cr, sw =
7017 match Ne.pipe () with
7018 | Ne.Exn exn ->
7019 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
7020 exit 1
7021 | Ne.Res rw -> rw
7022 and sr, cw =
7023 match Ne.pipe () with
7024 | Ne.Exn exn ->
7025 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7026 exit 1
7027 | Ne.Res rw -> rw
7030 cloexec cr;
7031 cloexec sw;
7032 cloexec sr;
7033 cloexec cw;
7035 setcheckers conf.checkers;
7036 redirectstderr ();
7038 init (cr, cw) (
7039 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7040 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7041 !Config.fontpath, !trimcachepath,
7042 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7044 state.sr <- sr;
7045 state.sw <- sw;
7046 state.text <- "Opening " ^ (mbtoutf8 state.path);
7047 reshape winw winh;
7048 opendoc state.path state.password;
7049 state.uioh <- uioh;
7051 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7053 let rec loop deadline =
7054 let r =
7055 match state.errfd with
7056 | None -> [state.sr; state.wsfd]
7057 | Some fd -> [state.sr; state.wsfd; fd]
7059 if state.redisplay
7060 then (
7061 state.redisplay <- false;
7062 display ();
7064 let timeout =
7065 let now = now () in
7066 if deadline > now
7067 then (
7068 if deadline = infinity
7069 then ~-.1.0
7070 else max 0.0 (deadline -. now)
7072 else 0.0
7074 let r, _, _ =
7075 try Unix.select r [] [] timeout
7076 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7078 begin match r with
7079 | [] ->
7080 state.ghyll None;
7081 let newdeadline =
7082 if state.ghyll == noghyll
7083 then
7084 match state.autoscroll with
7085 | Some step when step != 0 ->
7086 let y = state.y + step in
7087 let y =
7088 if y < 0
7089 then state.maxy
7090 else if y >= state.maxy then 0 else y
7092 gotoy y;
7093 if state.mode = View
7094 then state.text <- "";
7095 deadline +. 0.01
7096 | _ -> infinity
7097 else deadline +. 0.01
7099 loop newdeadline
7101 | l ->
7102 let rec checkfds = function
7103 | [] -> ()
7104 | fd :: rest when fd = state.sr ->
7105 let cmd = readcmd state.sr in
7106 act cmd;
7107 checkfds rest
7109 | fd :: rest when fd = state.wsfd ->
7110 Wsi.readresp fd;
7111 checkfds rest
7113 | fd :: rest ->
7114 let s = String.create 80 in
7115 let n = Unix.read fd s 0 80 in
7116 if conf.redirectstderr
7117 then (
7118 Buffer.add_substring state.errmsgs s 0 n;
7119 state.newerrmsgs <- true;
7120 state.redisplay <- true;
7122 else (
7123 prerr_string (String.sub s 0 n);
7124 flush stderr;
7126 checkfds rest
7128 checkfds l;
7129 let newdeadline =
7130 let deadline1 =
7131 if deadline = infinity
7132 then now () +. 0.01
7133 else deadline
7135 match state.autoscroll with
7136 | Some step when step != 0 -> deadline1
7137 | _ -> if state.ghyll == noghyll then infinity else deadline1
7139 loop newdeadline
7140 end;
7143 loop infinity;
7144 with Quit ->
7145 Config.save ();