Make "a" respect presentation mode
[llpp.git] / main.ml
blobfb272b6ae49cfb09d9bc0a0f41d5e83f3a007936
1 exception Quit;;
3 let tempfailureretry = Wsi.tempfailureretry;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 let dolog fmt = Printf.kprintf prerr_endline fmt;;
17 let now = Unix.gettimeofday;;
19 type params = (angle * proportional * trimparams
20 * texcount * sliceheight * memsize
21 * colorspace * fontpath * trimcachepath
22 * haspbo)
23 and pageno = int
24 and width = int
25 and height = int
26 and leftx = int
27 and opaque = string
28 and recttype = int
29 and pixmapsize = int
30 and angle = int
31 and proportional = bool
32 and trimmargins = bool
33 and interpagespace = int
34 and texcount = int
35 and sliceheight = int
36 and gen = int
37 and top = float
38 and dtop = float
39 and fontpath = string
40 and trimcachepath = string
41 and memsize = int
42 and aalevel = int
43 and irect = (int * int * int * int)
44 and trimparams = (trimmargins * irect)
45 and colorspace = | Rgb | Bgr | Gray
46 and haspbo = bool
49 type link =
50 | Lnotfound
51 | Lfound of int
52 and linkdir =
53 | LDfirst
54 | LDlast
55 | LDfirstvisible of (int * int * int)
56 | LDleft of int
57 | LDright of int
58 | LDdown of int
59 | LDup of int
62 type pagewithlinks =
63 | Pwlnotfound
64 | Pwl of int
67 type keymap =
68 | KMinsrt of key
69 | KMinsrl of key list
70 | KMmulti of key list * key list
71 and key = int * int
72 and keyhash = (key, keymap) Hashtbl.t
73 and keystate =
74 | KSnone
75 | KSinto of (key list * key list)
78 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
79 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
81 type pipe = (Unix.file_descr * Unix.file_descr);;
83 external init : pipe -> params -> unit = "ml_init";;
84 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
85 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
86 external getpdimrect : int -> float array = "ml_getpdimrect";;
87 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
88 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
89 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
90 external measurestr : int -> string -> float = "ml_measure_string";;
91 external getmaxw : unit -> float = "ml_getmaxw";;
92 external postprocess :
93 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
94 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
95 external platform : unit -> platform = "ml_platform";;
96 external setaalevel : int -> unit = "ml_setaalevel";;
97 external realloctexts : int -> bool = "ml_realloctexts";;
98 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
99 external findlink : opaque -> linkdir -> link = "ml_findlink";;
100 external getlink : opaque -> int -> under = "ml_getlink";;
101 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
102 external getlinkcount : opaque -> int = "ml_getlinkcount";;
103 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
104 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
105 external mbtoutf8 : string -> string = "ml_mbtoutf8";;
106 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
107 external freepbo : string -> unit = "ml_freepbo";;
108 external unmappbo : string -> unit = "ml_unmappbo";;
109 external pbousable : unit -> bool = "ml_pbo_usable";;
111 let platform_to_string = function
112 | Punknown -> "unknown"
113 | Plinux -> "Linux"
114 | Posx -> "OSX"
115 | Psun -> "Sun"
116 | Pfreebsd -> "FreeBSD"
117 | Pdragonflybsd -> "DragonflyBSD"
118 | Popenbsd -> "OpenBSD"
119 | Pnetbsd -> "NetBSD"
120 | Pcygwin -> "Cygwin"
123 let platform = platform ();;
125 let popen cmd fda =
126 if platform = Pcygwin
127 then (
128 let sh = "/bin/sh" in
129 let args = [|sh; "-c"; cmd|] in
130 let rec std si so se = function
131 | [] -> si, so, se
132 | (fd, 0) :: rest -> std fd so se rest
133 | (fd, -1) :: rest ->
134 Unix.set_close_on_exec fd;
135 std si so se rest
136 | (_, n) :: _ ->
137 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
139 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
140 ignore (Unix.create_process sh args si so se)
142 else popen cmd fda;
145 type x = int
146 and y = int
147 and tilex = int
148 and tiley = int
149 and tileparams = (x * y * width * height * tilex * tiley)
152 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
154 type mpos = int * int
155 and mstate =
156 | Msel of (mpos * mpos)
157 | Mpan of mpos
158 | Mscrolly | Mscrollx
159 | Mzoom of (int * int)
160 | Mzoomrect of (mpos * mpos)
161 | Mnone
164 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
165 and onkey = string -> int -> te
166 and ondone = string -> unit
167 and histcancel = unit -> unit
168 and onhist = ((histcmd -> string) * histcancel)
169 and histcmd = HCnext | HCprev | HCfirst | HClast
170 and cancelonempty = bool
171 and te =
172 | TEstop
173 | TEdone of string
174 | TEcont of string
175 | TEswitch of textentry
178 type 'a circbuf =
179 { store : 'a array
180 ; mutable rc : int
181 ; mutable wc : int
182 ; mutable len : int
186 let bound v minv maxv =
187 max minv (min maxv v);
190 let cbnew n v =
191 { store = Array.create n v
192 ; rc = 0
193 ; wc = 0
194 ; len = 0
198 let cbcap b = Array.length b.store;;
200 let cbput b v =
201 let cap = cbcap b in
202 b.store.(b.wc) <- v;
203 b.wc <- (b.wc + 1) mod cap;
204 b.rc <- b.wc;
205 b.len <- min (b.len + 1) cap;
208 let cbempty b = b.len = 0;;
210 let cbgetg b circular dir =
211 if cbempty b
212 then b.store.(0)
213 else
214 let rc = b.rc + dir in
215 let rc =
216 if circular
217 then (
218 if rc = -1
219 then b.len-1
220 else (
221 if rc >= b.len
222 then 0
223 else rc
226 else bound rc 0 (b.len-1)
228 b.rc <- rc;
229 b.store.(rc);
232 let cbget b = cbgetg b false;;
233 let cbgetc b = cbgetg b true;;
235 let drawstring size x y s =
236 Gl.enable `blend;
237 Gl.enable `texture_2d;
238 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
239 ignore (drawstr size x y s);
240 Gl.disable `blend;
241 Gl.disable `texture_2d;
244 let drawstring1 size x y s =
245 drawstr size x y s;
248 let drawstring2 size x y fmt =
249 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
252 type page =
253 { pageno : int
254 ; pagedimno : int
255 ; pagew : int
256 ; pageh : int
257 ; pagex : int
258 ; pagey : int
259 ; pagevw : int
260 ; pagevh : int
261 ; pagedispx : int
262 ; pagedispy : int
263 ; pagecol : int
267 let debugl l =
268 dolog "l %d dim=%d {" l.pageno l.pagedimno;
269 dolog " WxH %dx%d" l.pagew l.pageh;
270 dolog " vWxH %dx%d" l.pagevw l.pagevh;
271 dolog " pagex,y %d,%d" l.pagex l.pagey;
272 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
273 dolog " column %d" l.pagecol;
274 dolog "}";
277 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
278 dolog "rect {";
279 dolog " x0,y0=(% f, % f)" x0 y0;
280 dolog " x1,y1=(% f, % f)" x1 y1;
281 dolog " x2,y2=(% f, % f)" x2 y2;
282 dolog " x3,y3=(% f, % f)" x3 y3;
283 dolog "}";
286 type multicolumns = multicol * pagegeom
287 and singlecolumn = pagegeom
288 and splitcolumns = columncount * pagegeom
289 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
290 and multicol = columncount * covercount * covercount
291 and pdimno = int
292 and columncount = int
293 and covercount = int;;
295 type conf =
296 { mutable scrollbw : int
297 ; mutable scrollh : int
298 ; mutable icase : bool
299 ; mutable preload : bool
300 ; mutable pagebias : int
301 ; mutable verbose : bool
302 ; mutable debug : bool
303 ; mutable scrollstep : int
304 ; mutable hscrollstep : int
305 ; mutable maxhfit : bool
306 ; mutable crophack : bool
307 ; mutable autoscrollstep : int
308 ; mutable maxwait : float option
309 ; mutable hlinks : bool
310 ; mutable underinfo : bool
311 ; mutable interpagespace : interpagespace
312 ; mutable zoom : float
313 ; mutable presentation : bool
314 ; mutable angle : angle
315 ; mutable winw : int
316 ; mutable winh : int
317 ; mutable savebmarks : bool
318 ; mutable proportional : proportional
319 ; mutable trimmargins : trimmargins
320 ; mutable trimfuzz : irect
321 ; mutable memlimit : memsize
322 ; mutable texcount : texcount
323 ; mutable sliceheight : sliceheight
324 ; mutable thumbw : width
325 ; mutable jumpback : bool
326 ; mutable bgcolor : float * float * float
327 ; mutable bedefault : bool
328 ; mutable scrollbarinpm : bool
329 ; mutable tilew : int
330 ; mutable tileh : int
331 ; mutable mustoresize : memsize
332 ; mutable checkers : bool
333 ; mutable aalevel : int
334 ; mutable urilauncher : string
335 ; mutable pathlauncher : string
336 ; mutable colorspace : colorspace
337 ; mutable invert : bool
338 ; mutable colorscale : float
339 ; mutable redirectstderr : bool
340 ; mutable ghyllscroll : (int * int * int) option
341 ; mutable columns : columns
342 ; mutable beyecolumns : columncount option
343 ; mutable selcmd : string
344 ; mutable updatecurs : bool
345 ; mutable keyhashes : (string * keyhash) list
346 ; mutable hfsize : int
347 ; mutable pgscale : float
348 ; mutable usepbo : bool
349 ; mutable wheelbypage : bool
351 and columns =
352 | Csingle of singlecolumn
353 | Cmulti of multicolumns
354 | Csplit of splitcolumns
357 type anchor = pageno * top * dtop;;
359 type outline = string * int * anchor;;
361 type rect = float * float * float * float * float * float * float * float;;
363 type tile = opaque * pixmapsize * elapsed
364 and elapsed = float;;
365 type pagemapkey = pageno * gen;;
366 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
367 and row = int
368 and col = int;;
370 let emptyanchor = (0, 0.0, 0.0);;
372 type infochange = | Memused | Docinfo | Pdim;;
374 class type uioh = object
375 method display : unit
376 method key : int -> int -> uioh
377 method button : int -> bool -> int -> int -> int -> uioh
378 method motion : int -> int -> uioh
379 method pmotion : int -> int -> uioh
380 method infochanged : infochange -> unit
381 method scrollpw : (int * float * float)
382 method scrollph : (int * float * float)
383 method modehash : keyhash
384 end;;
386 type mode =
387 | Birdseye of (conf * leftx * pageno * pageno * anchor)
388 | Textentry of (textentry * onleave)
389 | View
390 | LinkNav of linktarget
391 and onleave = leavetextentrystatus -> unit
392 and leavetextentrystatus = | Cancel | Confirm
393 and helpitem = string * int * action
394 and action =
395 | Noaction
396 | Action of (uioh -> uioh)
397 and linktarget =
398 | Ltexact of (pageno * int)
399 | Ltgendir of int
402 let isbirdseye = function Birdseye _ -> true | _ -> false;;
403 let istextentry = function Textentry _ -> true | _ -> false;;
405 type currently =
406 | Idle
407 | Loading of (page * gen)
408 | Tiling of (
409 page * opaque * colorspace * angle * gen * col * row * width * height
411 | Outlining of outline list
414 let emptykeyhash = Hashtbl.create 0;;
415 let nouioh : uioh = object (self)
416 method display = ()
417 method key _ _ = self
418 method button _ _ _ _ _ = self
419 method motion _ _ = self
420 method pmotion _ _ = self
421 method infochanged _ = ()
422 method scrollpw = (0, nan, nan)
423 method scrollph = (0, nan, nan)
424 method modehash = emptykeyhash
425 end;;
427 type state =
428 { mutable sr : Unix.file_descr
429 ; mutable sw : Unix.file_descr
430 ; mutable wsfd : Unix.file_descr
431 ; mutable errfd : Unix.file_descr option
432 ; mutable stderr : Unix.file_descr
433 ; mutable errmsgs : Buffer.t
434 ; mutable newerrmsgs : bool
435 ; mutable w : int
436 ; mutable x : int
437 ; mutable y : int
438 ; mutable scrollw : int
439 ; mutable hscrollh : int
440 ; mutable anchor : anchor
441 ; mutable ranchors : (string * string * anchor) list
442 ; mutable maxy : int
443 ; mutable layout : page list
444 ; pagemap : (pagemapkey, opaque) Hashtbl.t
445 ; tilemap : (tilemapkey, tile) Hashtbl.t
446 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
447 ; mutable pdims : (pageno * width * height * leftx) list
448 ; mutable pagecount : int
449 ; mutable currently : currently
450 ; mutable mstate : mstate
451 ; mutable searchpattern : string
452 ; mutable rects : (pageno * recttype * rect) list
453 ; mutable rects1 : (pageno * recttype * rect) list
454 ; mutable text : string
455 ; mutable fullscreen : (width * height) option
456 ; mutable mode : mode
457 ; mutable uioh : uioh
458 ; mutable outlines : outline array
459 ; mutable bookmarks : outline list
460 ; mutable path : string
461 ; mutable password : string
462 ; mutable nameddest : string
463 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
464 ; mutable memused : memsize
465 ; mutable gen : gen
466 ; mutable throttle : (page list * int * float) option
467 ; mutable autoscroll : int option
468 ; mutable ghyll : (int option -> unit)
469 ; mutable help : helpitem array
470 ; mutable docinfo : (int * string) list
471 ; mutable texid : GlTex.texture_id option
472 ; hists : hists
473 ; mutable prevzoom : float
474 ; mutable progress : float
475 ; mutable redisplay : bool
476 ; mutable mpos : mpos
477 ; mutable keystate : keystate
478 ; mutable glinks : bool
479 ; mutable prevcolumns : (columns * float) option
480 ; mutable wthack : bool
482 and hists =
483 { pat : string circbuf
484 ; pag : string circbuf
485 ; nav : anchor circbuf
486 ; sel : string circbuf
490 let defconf =
491 { scrollbw = 7
492 ; scrollh = 12
493 ; icase = true
494 ; preload = true
495 ; pagebias = 0
496 ; verbose = false
497 ; debug = false
498 ; scrollstep = 24
499 ; hscrollstep = 24
500 ; maxhfit = true
501 ; crophack = false
502 ; autoscrollstep = 2
503 ; maxwait = None
504 ; hlinks = false
505 ; underinfo = false
506 ; interpagespace = 2
507 ; zoom = 1.0
508 ; presentation = false
509 ; angle = 0
510 ; winw = 900
511 ; winh = 900
512 ; savebmarks = true
513 ; proportional = true
514 ; trimmargins = false
515 ; trimfuzz = (0,0,0,0)
516 ; memlimit = 32 lsl 20
517 ; texcount = 256
518 ; sliceheight = 24
519 ; thumbw = 76
520 ; jumpback = true
521 ; bgcolor = (0.5, 0.5, 0.5)
522 ; bedefault = false
523 ; scrollbarinpm = true
524 ; tilew = 2048
525 ; tileh = 2048
526 ; mustoresize = 256 lsl 20
527 ; checkers = true
528 ; aalevel = 8
529 ; urilauncher =
530 (match platform with
531 | Plinux | Pfreebsd | Pdragonflybsd
532 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
533 | Posx -> "open \"%s\""
534 | Pcygwin -> "cygstart \"%s\""
535 | Punknown -> "echo %s")
536 ; pathlauncher = "lp \"%s\""
537 ; selcmd =
538 (match platform with
539 | Plinux | Pfreebsd | Pdragonflybsd
540 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
541 | Posx -> "pbcopy"
542 | Pcygwin -> "wsel"
543 | Punknown -> "cat")
544 ; colorspace = Rgb
545 ; invert = false
546 ; colorscale = 1.0
547 ; redirectstderr = false
548 ; ghyllscroll = None
549 ; columns = Csingle [||]
550 ; beyecolumns = None
551 ; updatecurs = false
552 ; hfsize = 12
553 ; pgscale = 1.0
554 ; usepbo = false
555 ; wheelbypage = false
556 ; keyhashes =
557 let mk n = (n, Hashtbl.create 1) in
558 [ mk "global"
559 ; mk "info"
560 ; mk "help"
561 ; mk "outline"
562 ; mk "listview"
563 ; mk "birdseye"
564 ; mk "textentry"
565 ; mk "links"
566 ; mk "view"
571 let findkeyhash c name =
572 try List.assoc name c.keyhashes
573 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
576 let conf = { defconf with angle = defconf.angle };;
578 let pgscale h = truncate (float h *. conf.pgscale);;
580 type fontstate =
581 { mutable fontsize : int
582 ; mutable wwidth : float
583 ; mutable maxrows : int
587 let fstate =
588 { fontsize = 14
589 ; wwidth = nan
590 ; maxrows = -1
594 let setfontsize n =
595 fstate.fontsize <- n;
596 fstate.wwidth <- measurestr fstate.fontsize "w";
597 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
600 let geturl s =
601 let colonpos = try String.index s ':' with Not_found -> -1 in
602 let len = String.length s in
603 if colonpos >= 0 && colonpos + 3 < len
604 then (
605 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
606 then
607 let schemestartpos =
608 try String.rindex_from s colonpos ' '
609 with Not_found -> -1
611 let scheme =
612 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
614 match scheme with
615 | "http" | "ftp" | "mailto" ->
616 let epos =
617 try String.index_from s colonpos ' '
618 with Not_found -> len
620 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
621 | _ -> ""
622 else ""
624 else ""
627 let gotouri uri =
628 if String.length conf.urilauncher = 0
629 then print_endline uri
630 else (
631 let url = geturl uri in
632 if String.length url = 0
633 then print_endline uri
634 else
635 let re = Str.regexp "%s" in
636 let command = Str.global_replace re url conf.urilauncher in
637 try popen command []
638 with exn ->
639 Printf.eprintf
640 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
641 flush stderr;
645 let version () =
646 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
647 (platform_to_string platform) Sys.word_size Sys.ocaml_version
650 let makehelp () =
651 let strings = version () :: "" :: Help.keys in
652 Array.of_list (
653 List.map (fun s ->
654 let url = geturl s in
655 if String.length url > 0
656 then (s, 0, Action (fun u -> gotouri url; u))
657 else (s, 0, Noaction)
658 ) strings);
661 let noghyll _ = ();;
662 let firstgeomcmds = "", [];;
664 let state =
665 { sr = Unix.stdin
666 ; sw = Unix.stdin
667 ; wsfd = Unix.stdin
668 ; errfd = None
669 ; stderr = Unix.stderr
670 ; errmsgs = Buffer.create 0
671 ; newerrmsgs = false
672 ; x = 0
673 ; y = 0
674 ; w = 0
675 ; scrollw = 0
676 ; hscrollh = 0
677 ; anchor = emptyanchor
678 ; ranchors = []
679 ; layout = []
680 ; maxy = max_int
681 ; tilelru = Queue.create ()
682 ; pagemap = Hashtbl.create 10
683 ; tilemap = Hashtbl.create 10
684 ; pdims = []
685 ; pagecount = 0
686 ; currently = Idle
687 ; mstate = Mnone
688 ; rects = []
689 ; rects1 = []
690 ; text = ""
691 ; mode = View
692 ; fullscreen = None
693 ; searchpattern = ""
694 ; outlines = [||]
695 ; bookmarks = []
696 ; path = ""
697 ; password = ""
698 ; nameddest = ""
699 ; geomcmds = firstgeomcmds
700 ; hists =
701 { nav = cbnew 10 emptyanchor
702 ; pat = cbnew 10 ""
703 ; pag = cbnew 10 ""
704 ; sel = cbnew 10 ""
706 ; memused = 0
707 ; gen = 0
708 ; throttle = None
709 ; autoscroll = None
710 ; ghyll = noghyll
711 ; help = makehelp ()
712 ; docinfo = []
713 ; texid = None
714 ; prevzoom = 1.0
715 ; progress = -1.0
716 ; uioh = nouioh
717 ; redisplay = true
718 ; mpos = (-1, -1)
719 ; keystate = KSnone
720 ; glinks = false
721 ; prevcolumns = None
722 ; wthack = false
726 let vlog fmt =
727 if conf.verbose
728 then
729 Printf.kprintf prerr_endline fmt
730 else
731 Printf.kprintf ignore fmt
734 let launchpath () =
735 if String.length conf.pathlauncher = 0
736 then print_endline state.path
737 else (
738 let re = Str.regexp "%s" in
739 let command = Str.global_replace re state.path conf.pathlauncher in
740 try popen command []
741 with exn ->
742 Printf.eprintf
743 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
744 flush stderr;
748 module Ne = struct
749 type 'a t = | Res of 'a | Exn of exn;;
751 let pipe () =
752 try Res (Unix.pipe ())
753 with exn -> Exn exn
756 let clo fd f =
757 try tempfailureretry Unix.close fd
758 with exn -> f (Printexc.to_string exn)
761 let dup fd =
762 try Res (tempfailureretry Unix.dup fd)
763 with exn -> Exn exn
766 let dup2 fd1 fd2 =
767 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
768 with exn -> Exn exn
770 end;;
772 let redirectstderr () =
773 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
774 if conf.redirectstderr
775 then
776 match Ne.pipe () with
777 | Ne.Exn exn ->
778 dolog "failed to create stderr redirection pipes: %s"
779 (Printexc.to_string exn)
781 | Ne.Res (r, w) ->
782 begin match Ne.dup Unix.stderr with
783 | Ne.Exn exn ->
784 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
785 Ne.clo r (clofail "pipe/r");
786 Ne.clo w (clofail "pipe/w");
788 | Ne.Res dupstderr ->
789 begin match Ne.dup2 w Unix.stderr with
790 | Ne.Exn exn ->
791 dolog "failed to dup2 to stderr: %s"
792 (Printexc.to_string exn);
793 Ne.clo dupstderr (clofail "stderr duplicate");
794 Ne.clo r (clofail "redir pipe/r");
795 Ne.clo w (clofail "redir pipe/w");
797 | Ne.Res () ->
798 state.stderr <- dupstderr;
799 state.errfd <- Some r;
800 end;
802 else (
803 state.newerrmsgs <- false;
804 begin match state.errfd with
805 | Some fd ->
806 begin match Ne.dup2 state.stderr Unix.stderr with
807 | Ne.Exn exn ->
808 dolog "failed to dup2 original stderr: %s"
809 (Printexc.to_string exn)
810 | Ne.Res () ->
811 Ne.clo fd (clofail "dup of stderr");
812 state.errfd <- None;
813 end;
814 | None -> ()
815 end;
816 prerr_string (Buffer.contents state.errmsgs);
817 flush stderr;
818 Buffer.clear state.errmsgs;
822 module G =
823 struct
824 let postRedisplay who =
825 if conf.verbose
826 then prerr_endline ("redisplay for " ^ who);
827 state.redisplay <- true;
829 end;;
831 let getopaque pageno =
832 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
833 with Not_found -> None
836 let putopaque pageno opaque =
837 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
840 let pagetranslatepoint l x y =
841 let dy = y - l.pagedispy in
842 let y = dy + l.pagey in
843 let dx = x - l.pagedispx in
844 let x = dx + l.pagex in
845 (x, y);
848 let getunder x y =
849 let rec f = function
850 | l :: rest ->
851 begin match getopaque l.pageno with
852 | Some opaque ->
853 let x0 = l.pagedispx in
854 let x1 = x0 + l.pagevw in
855 let y0 = l.pagedispy in
856 let y1 = y0 + l.pagevh in
857 if y >= y0 && y <= y1 && x >= x0 && x <= x1
858 then
859 let px, py = pagetranslatepoint l x y in
860 match whatsunder opaque px py with
861 | Unone -> f rest
862 | under -> under
863 else f rest
864 | _ ->
865 f rest
867 | [] -> Unone
869 f state.layout
872 let showtext c s =
873 state.text <- Printf.sprintf "%c%s" c s;
874 G.postRedisplay "showtext";
877 let undertext = function
878 | Unone -> "none"
879 | Ulinkuri s -> s
880 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
881 | Utext s -> "font: " ^ s
882 | Uunexpected s -> "unexpected: " ^ s
883 | Ulaunch s -> "launch: " ^ s
884 | Unamed s -> "named: " ^ s
885 | Uremote (filename, pageno) ->
886 Printf.sprintf "%s: page %d" filename (pageno+1)
889 let updateunder x y =
890 match getunder x y with
891 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
892 | Ulinkuri uri ->
893 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
894 Wsi.setcursor Wsi.CURSOR_INFO
895 | Ulinkgoto (pageno, _) ->
896 if conf.underinfo
897 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
898 Wsi.setcursor Wsi.CURSOR_INFO
899 | Utext s ->
900 if conf.underinfo then showtext 'f' ("ont: " ^ s);
901 Wsi.setcursor Wsi.CURSOR_TEXT
902 | Uunexpected s ->
903 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
904 Wsi.setcursor Wsi.CURSOR_INHERIT
905 | Ulaunch s ->
906 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
907 Wsi.setcursor Wsi.CURSOR_INHERIT
908 | Unamed s ->
909 if conf.underinfo then showtext 'n' ("amed: " ^ s);
910 Wsi.setcursor Wsi.CURSOR_INHERIT
911 | Uremote (filename, pageno) ->
912 if conf.underinfo then showtext 'r'
913 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
914 Wsi.setcursor Wsi.CURSOR_INFO
917 let showlinktype under =
918 if conf.underinfo
919 then
920 match under with
921 | Unone -> ()
922 | under ->
923 let s = undertext under in
924 showtext ' ' s
927 let addchar s c =
928 let b = Buffer.create (String.length s + 1) in
929 Buffer.add_string b s;
930 Buffer.add_char b c;
931 Buffer.contents b;
934 let colorspace_of_string s =
935 match String.lowercase s with
936 | "rgb" -> Rgb
937 | "bgr" -> Bgr
938 | "gray" -> Gray
939 | _ -> failwith "invalid colorspace"
942 let int_of_colorspace = function
943 | Rgb -> 0
944 | Bgr -> 1
945 | Gray -> 2
948 let colorspace_of_int = function
949 | 0 -> Rgb
950 | 1 -> Bgr
951 | 2 -> Gray
952 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
955 let colorspace_to_string = function
956 | Rgb -> "rgb"
957 | Bgr -> "bgr"
958 | Gray -> "gray"
961 let intentry_with_suffix text key =
962 let c =
963 if key >= 32 && key < 127
964 then Char.chr key
965 else '\000'
967 match Char.lowercase c with
968 | '0' .. '9' ->
969 let text = addchar text c in
970 TEcont text
972 | 'k' | 'm' | 'g' ->
973 let text = addchar text c in
974 TEcont text
976 | _ ->
977 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
978 TEcont text
981 let multicolumns_to_string (n, a, b) =
982 if a = 0 && b = 0
983 then Printf.sprintf "%d" n
984 else Printf.sprintf "%d,%d,%d" n a b;
987 let multicolumns_of_string s =
989 (int_of_string s, 0, 0)
990 with _ ->
991 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
992 if a > 1 || b > 1
993 then failwith "subtly broken"; (n, a, b)
997 let readcmd fd =
998 let s = "xxxx" in
999 let n = tempfailureretry (Unix.read fd s 0) 4 in
1000 if n != 4 then failwith "incomplete read(len)";
1001 let len = 0
1002 lor (Char.code s.[0] lsl 24)
1003 lor (Char.code s.[1] lsl 16)
1004 lor (Char.code s.[2] lsl 8)
1005 lor (Char.code s.[3] lsl 0)
1007 let s = String.create len in
1008 let n = tempfailureretry (Unix.read fd s 0) len in
1009 if n != len then failwith "incomplete read(data)";
1013 let btod b = if b then 1 else 0;;
1015 let wcmd fmt =
1016 let b = Buffer.create 16 in
1017 Buffer.add_string b "llll";
1018 Printf.kbprintf
1019 (fun b ->
1020 let s = Buffer.contents b in
1021 let n = String.length s in
1022 let len = n - 4 in
1023 (* dolog "wcmd %S" (String.sub s 4 len); *)
1024 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1025 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1026 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1027 s.[3] <- Char.chr (len land 0xff);
1028 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1029 if n' != n then failwith "write failed";
1030 ) b fmt;
1033 let calcips h =
1034 let d = conf.winh - h in
1035 max conf.interpagespace ((d + 1) / 2)
1038 let rowyh (c, coverA, coverB) b n =
1039 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1040 then
1041 let _, _, vy, (_, _, h, _) = b.(n) in
1042 (vy, h)
1043 else
1044 let n' = n - coverA in
1045 let d = n' mod c in
1046 let s = n - d in
1047 let e = min state.pagecount (s + c) in
1048 let rec find m miny maxh = if m = e then miny, maxh else
1049 let _, _, y, (_, _, h, _) = b.(m) in
1050 let miny = min miny y in
1051 let maxh = max maxh h in
1052 find (m+1) miny maxh
1053 in find s max_int 0
1056 let calcheight () =
1057 match conf.columns with
1058 | Cmulti ((_, _, _) as cl, b) ->
1059 if Array.length b > 0
1060 then
1061 let y, h = rowyh cl b (Array.length b - 1) in
1062 y + h + (if conf.presentation then calcips h else 0)
1063 else 0
1064 | Csingle b ->
1065 if Array.length b > 0
1066 then
1067 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1068 y + h + (if conf.presentation then calcips h else 0)
1069 else 0
1070 | Csplit (_, b) ->
1071 if Array.length b > 0
1072 then
1073 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1074 y + h
1075 else 0
1078 let getpageyh pageno =
1079 let pageno = bound pageno 0 (state.pagecount-1) in
1080 match conf.columns with
1081 | Csingle b ->
1082 if Array.length b = 0
1083 then 0, 0
1084 else
1085 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1086 let y =
1087 if conf.presentation
1088 then y - calcips h
1089 else y
1091 y, h
1092 | Cmulti (cl, b) ->
1093 if Array.length b = 0
1094 then 0, 0
1095 else
1096 let y, h = rowyh cl b pageno in
1097 let y =
1098 if conf.presentation
1099 then y - calcips h
1100 else y
1102 y, h
1103 | Csplit (c, b) ->
1104 if Array.length b = 0
1105 then 0, 0
1106 else
1107 let n = pageno*c in
1108 let (_, _, y, (_, _, h, _)) = b.(n) in
1109 y, h
1112 let getpagedim pageno =
1113 let rec f ppdim l =
1114 match l with
1115 | (n, _, _, _) as pdim :: rest ->
1116 if n >= pageno
1117 then (if n = pageno then pdim else ppdim)
1118 else f pdim rest
1120 | [] -> ppdim
1122 f (-1, -1, -1, -1) state.pdims
1125 let getpagey pageno = fst (getpageyh pageno);;
1127 let nogeomcmds cmds =
1128 match cmds with
1129 | s, [] -> String.length s = 0
1130 | _ -> false
1133 let page_of_y y =
1134 let ((c, coverA, coverB) as cl), b =
1135 match conf.columns with
1136 | Csingle b -> (1, 0, 0), b
1137 | Cmulti (c, b) -> c, b
1138 | Csplit (_, b) -> (1, 0, 0), b
1140 let rec bsearch nmin nmax =
1141 if nmin > nmax
1142 then bound nmin 0 (state.pagecount-1)
1143 else
1144 let n = (nmax + nmin) / 2 in
1145 let vy, h = rowyh cl b n in
1146 let y0, y1 =
1147 if conf.presentation
1148 then
1149 let ips = calcips h in
1150 let y0 = vy - ips in
1151 let y1 = vy + h + ips in
1152 y0, y1
1153 else (
1154 if n = 0
1155 then 0, vy + h + conf.interpagespace
1156 else
1157 let y0 = vy - conf.interpagespace in
1158 y0, y0 + h + conf.interpagespace
1161 if y >= y0 && y < y1
1162 then (
1163 if c = 1
1164 then n
1165 else (
1166 if n > coverA
1167 then
1168 if n < state.pagecount - coverB
1169 then ((n-coverA)/c)*c + coverA
1170 else n
1171 else n
1174 else (
1175 if y > y0
1176 then bsearch (n+1) nmax
1177 else bsearch nmin (n-1)
1180 let r = bsearch 0 (state.pagecount-1) in
1184 let layoutN ((columns, coverA, coverB), b) y sh =
1185 let sh = sh - state.hscrollh in
1186 let rec fold accu n =
1187 if n = Array.length b
1188 then accu
1189 else
1190 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1191 if (vy - y) > sh &&
1192 (n = coverA - 1
1193 || n = state.pagecount - coverB
1194 || (n - coverA) mod columns = columns - 1)
1195 then accu
1196 else
1197 let accu =
1198 if vy + h > y
1199 then
1200 let pagey = max 0 (y - vy) in
1201 let pagedispy = if pagey > 0 then 0 else vy - y in
1202 let pagedispx, pagex =
1203 let pdx =
1204 if n = coverA - 1 || n = state.pagecount - coverB
1205 then state.x + (conf.winw - state.scrollw - w) / 2
1206 else dx + xoff + state.x
1208 if pdx < 0
1209 then 0, -pdx
1210 else pdx, 0
1212 let pagevw =
1213 let vw = conf.winw - state.scrollw - pagedispx in
1214 let pw = w - pagex in
1215 min vw pw
1217 let pagevh = min (h - pagey) (sh - pagedispy) in
1218 if pagevw > 0 && pagevh > 0
1219 then
1220 let e =
1221 { pageno = n
1222 ; pagedimno = pdimno
1223 ; pagew = w
1224 ; pageh = h
1225 ; pagex = pagex
1226 ; pagey = pagey
1227 ; pagevw = pagevw
1228 ; pagevh = pagevh
1229 ; pagedispx = pagedispx
1230 ; pagedispy = pagedispy
1231 ; pagecol = 0
1234 e :: accu
1235 else
1236 accu
1237 else
1238 accu
1240 fold accu (n+1)
1242 List.rev (fold [] (page_of_y y));
1245 let layoutS (columns, b) y sh =
1246 let sh = sh - state.hscrollh in
1247 let rec fold accu n =
1248 if n = Array.length b
1249 then accu
1250 else
1251 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1252 if (vy - y) > sh
1253 then accu
1254 else
1255 let accu =
1256 if vy + pageh > y
1257 then
1258 let x = xoff + state.x in
1259 let pagey = max 0 (y - vy) in
1260 let pagedispy = if pagey > 0 then 0 else vy - y in
1261 let pagedispx, pagex =
1262 if px = 0
1263 then (
1264 if x < 0
1265 then 0, -x
1266 else x, 0
1268 else (
1269 let px = px - x in
1270 if px < 0
1271 then -px, 0
1272 else 0, px
1275 let pagecolw = pagew/columns in
1276 let pagedispx =
1277 if pagecolw < conf.winw
1278 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1279 else pagedispx
1281 let pagevw =
1282 let vw = conf.winw - pagedispx - state.scrollw in
1283 let pw = pagew - pagex in
1284 min vw pw
1286 let pagevw = min pagevw pagecolw in
1287 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1288 if pagevw > 0 && pagevh > 0
1289 then
1290 let e =
1291 { pageno = n/columns
1292 ; pagedimno = pdimno
1293 ; pagew = pagew
1294 ; pageh = pageh
1295 ; pagex = pagex
1296 ; pagey = pagey
1297 ; pagevw = pagevw
1298 ; pagevh = pagevh
1299 ; pagedispx = pagedispx
1300 ; pagedispy = pagedispy
1301 ; pagecol = n mod columns
1304 e :: accu
1305 else
1306 accu
1307 else
1308 accu
1310 fold accu (n+1)
1312 List.rev (fold [] 0)
1315 let layout y sh =
1316 if nogeomcmds state.geomcmds
1317 then
1318 match conf.columns with
1319 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1320 | Cmulti c -> layoutN c y sh
1321 | Csplit s -> layoutS s y sh
1322 else []
1325 let clamp incr =
1326 let y = state.y + incr in
1327 let y = max 0 y in
1328 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1332 let itertiles l f =
1333 let tilex = l.pagex mod conf.tilew in
1334 let tiley = l.pagey mod conf.tileh in
1336 let col = l.pagex / conf.tilew in
1337 let row = l.pagey / conf.tileh in
1339 let rec rowloop row y0 dispy h =
1340 if h = 0
1341 then ()
1342 else (
1343 let dh = conf.tileh - y0 in
1344 let dh = min h dh in
1345 let rec colloop col x0 dispx w =
1346 if w = 0
1347 then ()
1348 else (
1349 let dw = conf.tilew - x0 in
1350 let dw = min w dw in
1352 f col row dispx dispy x0 y0 dw dh;
1353 colloop (col+1) 0 (dispx+dw) (w-dw)
1356 colloop col tilex l.pagedispx l.pagevw;
1357 rowloop (row+1) 0 (dispy+dh) (h-dh)
1360 if l.pagevw > 0 && l.pagevh > 0
1361 then rowloop row tiley l.pagedispy l.pagevh;
1364 let gettileopaque l col row =
1365 let key =
1366 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1368 try Some (Hashtbl.find state.tilemap key)
1369 with Not_found -> None
1372 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1373 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1374 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1377 let drawtiles l color =
1378 GlDraw.color color;
1379 let f col row x y tilex tiley w h =
1380 match gettileopaque l col row with
1381 | Some (opaque, _, t) ->
1382 let params = x, y, w, h, tilex, tiley in
1383 if conf.invert
1384 then (
1385 Gl.enable `blend;
1386 GlFunc.blend_func `zero `one_minus_src_color;
1388 drawtile params opaque;
1389 if conf.invert
1390 then Gl.disable `blend;
1391 if conf.debug
1392 then (
1393 let s = Printf.sprintf
1394 "%d[%d,%d] %f sec"
1395 l.pageno col row t
1397 let w = measurestr fstate.fontsize s in
1398 GlMisc.push_attrib [`current];
1399 GlDraw.color (0.0, 0.0, 0.0);
1400 GlDraw.rect
1401 (float (x-2), float (y-2))
1402 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1403 GlDraw.color (1.0, 1.0, 1.0);
1404 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1405 GlMisc.pop_attrib ();
1408 | _ ->
1409 let w =
1410 let lw = conf.winw - state.scrollw - x in
1411 min lw w
1412 and h =
1413 let lh = conf.winh - y in
1414 min lh h
1416 begin match state.texid with
1417 | Some id ->
1418 Gl.enable `texture_2d;
1419 GlTex.bind_texture `texture_2d id;
1420 let x0 = float x
1421 and y0 = float y
1422 and x1 = float (x+w)
1423 and y1 = float (y+h) in
1425 let tw = float w /. 16.0
1426 and th = float h /. 16.0 in
1427 let tx0 = float tilex /. 16.0
1428 and ty0 = float tiley /. 16.0 in
1429 let tx1 = tx0 +. tw
1430 and ty1 = ty0 +. th in
1431 GlDraw.begins `quads;
1432 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1433 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1434 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1435 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1436 GlDraw.ends ();
1438 Gl.disable `texture_2d;
1439 | None ->
1440 GlDraw.color (1.0, 1.0, 1.0);
1441 GlDraw.rect
1442 (float x, float y)
1443 (float (x+w), float (y+h));
1444 end;
1445 if w > 128 && h > fstate.fontsize + 10
1446 then (
1447 GlDraw.color (0.0, 0.0, 0.0);
1448 let c, r =
1449 if conf.verbose
1450 then (col*conf.tilew, row*conf.tileh)
1451 else col, row
1453 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1455 GlDraw.color color;
1457 itertiles l f
1460 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1462 let tilevisible1 l x y =
1463 let ax0 = l.pagex
1464 and ax1 = l.pagex + l.pagevw
1465 and ay0 = l.pagey
1466 and ay1 = l.pagey + l.pagevh in
1468 let bx0 = x
1469 and by0 = y in
1470 let bx1 = min (bx0 + conf.tilew) l.pagew
1471 and by1 = min (by0 + conf.tileh) l.pageh in
1473 let rx0 = max ax0 bx0
1474 and ry0 = max ay0 by0
1475 and rx1 = min ax1 bx1
1476 and ry1 = min ay1 by1 in
1478 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1479 nonemptyintersection
1482 let tilevisible layout n x y =
1483 let rec findpageinlayout m = function
1484 | l :: rest when l.pageno = n ->
1485 tilevisible1 l x y || (
1486 match conf.columns with
1487 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1488 | _ -> false
1490 | _ :: rest -> findpageinlayout 0 rest
1491 | [] -> false
1493 findpageinlayout 0 layout;
1496 let tileready l x y =
1497 tilevisible1 l x y &&
1498 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1501 let tilepage n p layout =
1502 let rec loop = function
1503 | l :: rest ->
1504 if l.pageno = n
1505 then
1506 let f col row _ _ _ _ _ _ =
1507 if state.currently = Idle
1508 then
1509 match gettileopaque l col row with
1510 | Some _ -> ()
1511 | None ->
1512 let x = col*conf.tilew
1513 and y = row*conf.tileh in
1514 let w =
1515 let w = l.pagew - x in
1516 min w conf.tilew
1518 let h =
1519 let h = l.pageh - y in
1520 min h conf.tileh
1522 let pbo =
1523 if conf.usepbo
1524 then getpbo w h conf.colorspace
1525 else "0"
1527 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1528 state.currently <-
1529 Tiling (
1530 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1531 conf.tilew, conf.tileh
1534 itertiles l f;
1535 else
1536 loop rest
1538 | [] -> ()
1540 if nogeomcmds state.geomcmds
1541 then loop layout;
1544 let preloadlayout y =
1545 let y = if y < conf.winh then 0 else y - conf.winh in
1546 let h = conf.winh*3 in
1547 layout y h;
1550 let load pages =
1551 let rec loop pages =
1552 if state.currently != Idle
1553 then ()
1554 else
1555 match pages with
1556 | l :: rest ->
1557 begin match getopaque l.pageno with
1558 | None ->
1559 wcmd "page %d %d" l.pageno l.pagedimno;
1560 state.currently <- Loading (l, state.gen);
1561 | Some opaque ->
1562 tilepage l.pageno opaque pages;
1563 loop rest
1564 end;
1565 | _ -> ()
1567 if nogeomcmds state.geomcmds
1568 then loop pages
1571 let preload pages =
1572 load pages;
1573 if conf.preload && state.currently = Idle
1574 then load (preloadlayout state.y);
1577 let layoutready layout =
1578 let rec fold all ls =
1579 all && match ls with
1580 | l :: rest ->
1581 let seen = ref false in
1582 let allvisible = ref true in
1583 let foo col row _ _ _ _ _ _ =
1584 seen := true;
1585 allvisible := !allvisible &&
1586 begin match gettileopaque l col row with
1587 | Some _ -> true
1588 | None -> false
1591 itertiles l foo;
1592 fold (!seen && !allvisible) rest
1593 | [] -> true
1595 let alltilesvisible = fold true layout in
1596 alltilesvisible;
1599 let gotoy y =
1600 let y = bound y 0 state.maxy in
1601 let y, layout, proceed =
1602 match conf.maxwait with
1603 | Some time when state.ghyll == noghyll ->
1604 begin match state.throttle with
1605 | None ->
1606 let layout = layout y conf.winh in
1607 let ready = layoutready layout in
1608 if not ready
1609 then (
1610 load layout;
1611 state.throttle <- Some (layout, y, now ());
1613 else G.postRedisplay "gotoy showall (None)";
1614 y, layout, ready
1615 | Some (_, _, started) ->
1616 let dt = now () -. started in
1617 if dt > time
1618 then (
1619 state.throttle <- None;
1620 let layout = layout y conf.winh in
1621 load layout;
1622 G.postRedisplay "maxwait";
1623 y, layout, true
1625 else -1, [], false
1628 | _ ->
1629 let layout = layout y conf.winh in
1630 if true || layoutready layout
1631 then G.postRedisplay "gotoy ready";
1632 y, layout, true
1634 if proceed
1635 then (
1636 state.y <- y;
1637 state.layout <- layout;
1638 begin match state.mode with
1639 | LinkNav (Ltexact (pageno, linkno)) ->
1640 let rec loop = function
1641 | [] ->
1642 state.mode <- LinkNav (Ltgendir 0)
1643 | l :: _ when l.pageno = pageno ->
1644 begin match getopaque pageno with
1645 | None ->
1646 state.mode <- LinkNav (Ltgendir 0)
1647 | Some opaque ->
1648 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1649 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1650 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1651 then state.mode <- LinkNav (Ltgendir 0)
1653 | _ :: rest -> loop rest
1655 loop layout
1656 | _ -> ()
1657 end;
1658 begin match state.mode with
1659 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1660 if not (pagevisible layout pageno)
1661 then (
1662 match state.layout with
1663 | [] -> ()
1664 | l :: _ ->
1665 state.mode <- Birdseye (
1666 conf, leftx, l.pageno, hooverpageno, anchor
1669 | LinkNav (Ltgendir dir as lt) ->
1670 let linknav =
1671 let rec loop = function
1672 | [] -> lt
1673 | l :: rest ->
1674 match getopaque l.pageno with
1675 | None -> loop rest
1676 | Some opaque ->
1677 let link =
1678 let ld =
1679 if dir = 0
1680 then LDfirstvisible (l.pagex, l.pagey, dir)
1681 else (
1682 if dir > 0 then LDfirst else LDlast
1685 findlink opaque ld
1687 match link with
1688 | Lnotfound -> loop rest
1689 | Lfound n ->
1690 showlinktype (getlink opaque n);
1691 Ltexact (l.pageno, n)
1693 loop state.layout
1695 state.mode <- LinkNav linknav
1696 | _ -> ()
1697 end;
1698 preload layout;
1700 state.ghyll <- noghyll;
1701 if conf.updatecurs
1702 then (
1703 let mx, my = state.mpos in
1704 updateunder mx my;
1708 let conttiling pageno opaque =
1709 tilepage pageno opaque
1710 (if conf.preload then preloadlayout state.y else state.layout)
1713 let gotoy_and_clear_text y =
1714 if not conf.verbose then state.text <- "";
1715 gotoy y;
1718 let getanchor1 l =
1719 let top =
1720 let coloff = l.pagecol * l.pageh in
1721 float (l.pagey + coloff) /. float l.pageh
1723 let dtop =
1724 if l.pagedispy = 0
1725 then
1727 else
1728 if conf.presentation
1729 then float l.pagedispy /. float (calcips l.pageh)
1730 else float l.pagedispy /. float conf.interpagespace
1732 (l.pageno, top, dtop)
1735 let getanchor () =
1736 match state.layout with
1737 | l :: _ -> getanchor1 l
1738 | [] ->
1739 let n = page_of_y state.y in
1740 let y, h = getpageyh n in
1741 let dy = y - state.y in
1742 let dtop =
1743 if conf.presentation
1744 then
1745 let ips = calcips h in
1746 float (dy + ips) /. float ips
1747 else
1748 float dy /. float conf.interpagespace
1750 (n, 0.0, dtop)
1753 let getanchory (n, top, dtop) =
1754 let y, h = getpageyh n in
1755 if conf.presentation
1756 then
1757 let ips = calcips h in
1758 y + truncate (top*.float h -. dtop*.float ips) + ips;
1759 else
1760 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1763 let gotoanchor anchor =
1764 gotoy (getanchory anchor);
1767 let addnav () =
1768 cbput state.hists.nav (getanchor ());
1771 let getnav dir =
1772 let anchor = cbgetc state.hists.nav dir in
1773 getanchory anchor;
1776 let gotoghyll y =
1777 let scroll f n a b =
1778 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1779 let snake f a b =
1780 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1781 if f < a
1782 then s (float f /. float a)
1783 else (
1784 if f > b
1785 then 1.0 -. s ((float (f-b) /. float (n-b)))
1786 else 1.0
1789 snake f a b
1790 and summa f n a b =
1791 (* courtesy:
1792 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1793 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1794 let iv1 = iv f in
1795 let ins = float a *. iv1
1796 and outs = float (n-b) *. iv1 in
1797 let ones = b - a in
1798 ins +. outs +. float ones
1800 let rec set (_N, _A, _B) y sy =
1801 let sum = summa 1.0 _N _A _B in
1802 let dy = float (y - sy) in
1803 state.ghyll <- (
1804 let rec gf n y1 o =
1805 if n >= _N
1806 then state.ghyll <- noghyll
1807 else
1808 let go n =
1809 let s = scroll n _N _A _B in
1810 let y1 = y1 +. ((s *. dy) /. sum) in
1811 gotoy_and_clear_text (truncate y1);
1812 state.ghyll <- gf (n+1) y1;
1814 match o with
1815 | None -> go n
1816 | Some y' -> set (_N/2, 1, 1) y' state.y
1818 gf 0 (float state.y)
1821 match conf.ghyllscroll with
1822 | None ->
1823 gotoy_and_clear_text y
1824 | Some nab ->
1825 if state.ghyll == noghyll
1826 then set nab y state.y
1827 else state.ghyll (Some y)
1830 let gotopage n top =
1831 let y, h = getpageyh n in
1832 let y = y + (truncate (top *. float h)) in
1833 gotoghyll y
1836 let gotopage1 n top =
1837 let y = getpagey n in
1838 let y = y + top in
1839 gotoghyll y
1842 let invalidate s f =
1843 state.layout <- [];
1844 state.pdims <- [];
1845 state.rects <- [];
1846 state.rects1 <- [];
1847 match state.geomcmds with
1848 | ps, [] when String.length ps = 0 ->
1849 f ();
1850 state.geomcmds <- s, [];
1852 | ps, [] ->
1853 state.geomcmds <- ps, [s, f];
1855 | ps, (s', _) :: rest when s' = s ->
1856 state.geomcmds <- ps, ((s, f) :: rest);
1858 | ps, cmds ->
1859 state.geomcmds <- ps, ((s, f) :: cmds);
1862 let flushpages () =
1863 Hashtbl.iter (fun _ opaque ->
1864 wcmd "freepage %s" opaque;
1865 ) state.pagemap;
1866 Hashtbl.clear state.pagemap;
1869 let opendoc path password =
1870 state.path <- path;
1871 state.password <- password;
1872 state.gen <- state.gen + 1;
1873 state.docinfo <- [];
1875 flushpages ();
1876 setaalevel conf.aalevel;
1877 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1878 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1879 invalidate "reqlayout"
1880 (fun () ->
1881 wcmd "reqlayout %d %d %s\000"
1882 conf.angle (btod conf.proportional) state.nameddest;
1886 let reload () =
1887 state.anchor <- getanchor ();
1888 state.wthack <- true;
1889 opendoc state.path state.password;
1892 let scalecolor c =
1893 let c = c *. conf.colorscale in
1894 (c, c, c);
1897 let scalecolor2 (r, g, b) =
1898 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1901 let docolumns = function
1902 | Csingle _ ->
1903 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1904 let rec loop pageno pdimno pdim y ph pdims =
1905 if pageno = state.pagecount
1906 then ()
1907 else
1908 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1909 match pdims with
1910 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1911 pdimno+1, pdim, rest
1912 | _ ->
1913 pdimno, pdim, pdims
1915 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1916 let y = y +
1917 (if conf.presentation
1918 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1919 else (if pageno = 0 then 0 else conf.interpagespace)
1922 a.(pageno) <- (pdimno, x, y, pdim);
1923 loop (pageno+1) pdimno pdim (y + h) h pdims
1925 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1926 conf.columns <- Csingle a;
1928 | Cmulti ((columns, coverA, coverB), _) ->
1929 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1930 let rec loop pageno pdimno pdim x y rowh pdims =
1931 let rec fixrow m = if m = pageno then () else
1932 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1933 if h < rowh
1934 then (
1935 let y = y + (rowh - h) / 2 in
1936 a.(m) <- (pdimno, x, y, pdim);
1938 fixrow (m+1)
1940 if pageno = state.pagecount
1941 then fixrow (((pageno - 1) / columns) * columns)
1942 else
1943 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1944 match pdims with
1945 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1946 pdimno+1, pdim, rest
1947 | _ ->
1948 pdimno, pdim, pdims
1950 let x, y, rowh' =
1951 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1952 then (
1953 let x = (conf.winw - state.scrollw - w) / 2 in
1954 let ips =
1955 if conf.presentation then calcips h else conf.interpagespace in
1956 x, y + ips + rowh, h
1958 else (
1959 if (pageno - coverA) mod columns = 0
1960 then (
1961 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1962 let y =
1963 if conf.presentation
1964 then
1965 let ips = calcips h in
1966 y + (if pageno = 0 then 0 else calcips rowh + ips)
1967 else
1968 y + (if pageno = 0 then 0 else conf.interpagespace)
1970 x, y + rowh, h
1972 else x, y, max rowh h
1975 let y =
1976 if pageno > 1 && (pageno - coverA) mod columns = 0
1977 then (
1978 let y =
1979 if pageno = columns && conf.presentation
1980 then (
1981 let ips = calcips rowh in
1982 for i = 0 to pred columns
1984 let (pdimno, x, y, pdim) = a.(i) in
1985 a.(i) <- (pdimno, x, y+ips, pdim)
1986 done;
1987 y+ips;
1989 else y
1991 fixrow (pageno - columns);
1994 else y
1996 a.(pageno) <- (pdimno, x, y, pdim);
1997 let x = x + w + xoff*2 + conf.interpagespace in
1998 loop (pageno+1) pdimno pdim x y rowh' pdims
2000 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2001 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2003 | Csplit (c, _) ->
2004 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2005 let rec loop pageno pdimno pdim y pdims =
2006 if pageno = state.pagecount
2007 then ()
2008 else
2009 let pdimno, ((_, w, h, _) as pdim), pdims =
2010 match pdims with
2011 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2012 pdimno+1, pdim, rest
2013 | _ ->
2014 pdimno, pdim, pdims
2016 let cw = w / c in
2017 let rec loop1 n x y =
2018 if n = c then y else (
2019 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2020 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2023 let y = loop1 0 0 y in
2024 loop (pageno+1) pdimno pdim y pdims
2026 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2027 conf.columns <- Csplit (c, a);
2030 let represent () =
2031 docolumns conf.columns;
2032 state.maxy <- calcheight ();
2033 state.hscrollh <-
2034 if state.w <= conf.winw - state.scrollw
2035 then 0
2036 else state.scrollw
2038 match state.mode with
2039 | Birdseye (_, _, pageno, _, _) ->
2040 let y, h = getpageyh pageno in
2041 let top = (conf.winh - h) / 2 in
2042 gotoy (max 0 (y - top))
2043 | _ -> gotoanchor state.anchor
2046 let reshape w h =
2047 state.wthack <- false;
2048 GlDraw.viewport 0 0 w h;
2049 let firsttime = state.geomcmds == firstgeomcmds in
2050 if not firsttime && nogeomcmds state.geomcmds
2051 then state.anchor <- getanchor ();
2053 conf.winw <- w;
2054 let w = truncate (float w *. conf.zoom) - state.scrollw in
2055 let w = max w 2 in
2056 conf.winh <- h;
2057 setfontsize fstate.fontsize;
2058 GlMat.mode `modelview;
2059 GlMat.load_identity ();
2061 GlMat.mode `projection;
2062 GlMat.load_identity ();
2063 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2064 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2065 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2067 let relx =
2068 if conf.zoom <= 1.0
2069 then 0.0
2070 else float state.x /. float state.w
2072 invalidate "geometry"
2073 (fun () ->
2074 state.w <- w;
2075 if not firsttime
2076 then state.x <- truncate (relx *. float w);
2077 let w =
2078 match conf.columns with
2079 | Csingle _ -> w
2080 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2081 | Csplit (c, _) -> w * c
2083 wcmd "geometry %d %d" w h);
2086 let enttext () =
2087 let len = String.length state.text in
2088 let drawstring s =
2089 let hscrollh =
2090 match state.mode with
2091 | Textentry _
2092 | View ->
2093 let h, _, _ = state.uioh#scrollpw in
2095 | _ -> 0
2097 let rect x w =
2098 GlDraw.rect
2099 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2100 (x+.w, float (conf.winh - hscrollh))
2103 let w = float (conf.winw - state.scrollw - 1) in
2104 if state.progress >= 0.0 && state.progress < 1.0
2105 then (
2106 GlDraw.color (0.3, 0.3, 0.3);
2107 let w1 = w *. state.progress in
2108 rect 0.0 w1;
2109 GlDraw.color (0.0, 0.0, 0.0);
2110 rect w1 (w-.w1)
2112 else (
2113 GlDraw.color (0.0, 0.0, 0.0);
2114 rect 0.0 w;
2117 GlDraw.color (1.0, 1.0, 1.0);
2118 drawstring fstate.fontsize
2119 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2121 let s =
2122 match state.mode with
2123 | Textentry ((prefix, text, _, _, _, _), _) ->
2124 let s =
2125 if len > 0
2126 then
2127 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2128 else
2129 Printf.sprintf "%s%s_" prefix text
2133 | _ -> state.text
2135 let s =
2136 if state.newerrmsgs
2137 then (
2138 if not (istextentry state.mode)
2139 then
2140 let s1 = "(press 'e' to review error messasges)" in
2141 if String.length s > 0 then s ^ " " ^ s1 else s1
2142 else s
2144 else s
2146 if String.length s > 0
2147 then drawstring s
2150 let gctiles () =
2151 let len = Queue.length state.tilelru in
2152 let layout = lazy (
2153 match state.throttle with
2154 | None ->
2155 if conf.preload
2156 then preloadlayout state.y
2157 else state.layout
2158 | Some (layout, _, _) ->
2159 layout
2160 ) in
2161 let rec loop qpos =
2162 if state.memused <= conf.memlimit
2163 then ()
2164 else (
2165 if qpos < len
2166 then
2167 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2168 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2169 let (_, pw, ph, _) = getpagedim n in
2171 gen = state.gen
2172 && colorspace = conf.colorspace
2173 && angle = conf.angle
2174 && pagew = pw
2175 && pageh = ph
2176 && (
2177 let x = col*conf.tilew
2178 and y = row*conf.tileh in
2179 tilevisible (Lazy.force_val layout) n x y
2181 then Queue.push lruitem state.tilelru
2182 else (
2183 freepbo p;
2184 wcmd "freetile %s" p;
2185 state.memused <- state.memused - s;
2186 state.uioh#infochanged Memused;
2187 Hashtbl.remove state.tilemap k;
2189 loop (qpos+1)
2192 loop 0
2195 let flushtiles () =
2196 Queue.iter (fun (k, p, s) ->
2197 wcmd "freetile %s" p;
2198 state.memused <- state.memused - s;
2199 state.uioh#infochanged Memused;
2200 Hashtbl.remove state.tilemap k;
2201 ) state.tilelru;
2202 Queue.clear state.tilelru;
2203 load state.layout;
2206 let logcurrently = function
2207 | Idle -> dolog "Idle"
2208 | Loading (l, gen) ->
2209 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2210 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2211 dolog
2212 "Tiling %d[%d,%d] page=%s cs=%s angle"
2213 l.pageno col row pageopaque
2214 (colorspace_to_string colorspace)
2216 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2217 angle gen conf.angle state.gen
2218 tilew tileh
2219 conf.tilew conf.tileh
2221 | Outlining _ ->
2222 dolog "outlining"
2225 let act cmds =
2226 (* dolog "%S" cmds; *)
2227 let op, args =
2228 let spacepos =
2229 try String.index cmds ' '
2230 with Not_found -> -1
2232 if spacepos = -1
2233 then cmds, ""
2234 else
2235 let l = String.length cmds in
2236 let op = String.sub cmds 0 spacepos in
2237 op, begin
2238 if l - spacepos < 2 then ""
2239 else String.sub cmds (spacepos+1) (l-spacepos-1)
2242 match op with
2243 | "clear" ->
2244 state.uioh#infochanged Pdim;
2245 state.pdims <- [];
2247 | "clearrects" ->
2248 state.rects <- state.rects1;
2249 G.postRedisplay "clearrects";
2251 | "continue" ->
2252 let n =
2253 try Scanf.sscanf args "%u" (fun n -> n)
2254 with exn ->
2255 dolog "error processing 'continue' %S: %s"
2256 cmds (Printexc.to_string exn);
2257 exit 1;
2259 state.pagecount <- n;
2260 begin match state.currently with
2261 | Outlining l ->
2262 state.currently <- Idle;
2263 state.outlines <- Array.of_list (List.rev l)
2264 | _ -> ()
2265 end;
2267 let cur, cmds = state.geomcmds in
2268 if String.length cur = 0
2269 then failwith "umpossible";
2271 begin match List.rev cmds with
2272 | [] ->
2273 state.geomcmds <- "", [];
2274 represent ();
2275 | (s, f) :: rest ->
2276 f ();
2277 state.geomcmds <- s, List.rev rest;
2278 end;
2279 if conf.maxwait = None
2280 then G.postRedisplay "continue";
2282 | "title" ->
2283 Wsi.settitle args
2285 | "msg" ->
2286 showtext ' ' args
2288 | "vmsg" ->
2289 if conf.verbose
2290 then showtext ' ' args
2292 | "emsg" ->
2293 Buffer.add_string state.errmsgs args;
2294 state.newerrmsgs <- true;
2295 G.postRedisplay "error message"
2297 | "progress" ->
2298 let progress, text =
2300 Scanf.sscanf args "%f %n"
2301 (fun f pos ->
2302 f, String.sub args pos (String.length args - pos))
2303 with exn ->
2304 dolog "error processing 'progress' %S: %s"
2305 cmds (Printexc.to_string exn);
2306 exit 1;
2308 state.text <- text;
2309 state.progress <- progress;
2310 G.postRedisplay "progress"
2312 | "firstmatch" ->
2313 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2315 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2316 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2317 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2318 with exn ->
2319 dolog "error processing 'firstmatch' %S: %s"
2320 cmds (Printexc.to_string exn);
2321 exit 1;
2323 let y = (getpagey pageno) + truncate y0 in
2324 addnav ();
2325 gotoy y;
2326 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2328 | "match" ->
2329 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2331 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2332 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2333 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2334 with exn ->
2335 dolog "error processing 'match' %S: %s"
2336 cmds (Printexc.to_string exn);
2337 exit 1;
2339 state.rects1 <-
2340 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2342 | "page" ->
2343 let pageopaque, t =
2345 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2346 with exn ->
2347 dolog "error processing 'page' %S: %s"
2348 cmds (Printexc.to_string exn);
2349 exit 1;
2351 begin match state.currently with
2352 | Loading (l, gen) ->
2353 vlog "page %d took %f sec" l.pageno t;
2354 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2355 begin match state.throttle with
2356 | None ->
2357 let preloadedpages =
2358 if conf.preload
2359 then preloadlayout state.y
2360 else state.layout
2362 let evict () =
2363 let module IntSet =
2364 Set.Make (struct type t = int let compare = (-) end) in
2365 let set =
2366 List.fold_left (fun s l -> IntSet.add l.pageno s)
2367 IntSet.empty preloadedpages
2369 let evictedpages =
2370 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2371 if not (IntSet.mem pageno set)
2372 then (
2373 wcmd "freepage %s" opaque;
2374 key :: accu
2376 else accu
2377 ) state.pagemap []
2379 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2381 evict ();
2382 state.currently <- Idle;
2383 if gen = state.gen
2384 then (
2385 tilepage l.pageno pageopaque state.layout;
2386 load state.layout;
2387 load preloadedpages;
2388 if pagevisible state.layout l.pageno
2389 && layoutready state.layout
2390 then G.postRedisplay "page";
2393 | Some (layout, _, _) ->
2394 state.currently <- Idle;
2395 tilepage l.pageno pageopaque layout;
2396 load state.layout
2397 end;
2399 | _ ->
2400 dolog "Inconsistent loading state";
2401 logcurrently state.currently;
2402 exit 1
2405 | "tile" ->
2406 let (x, y, opaque, size, t) =
2408 Scanf.sscanf args "%u %u %s %u %f"
2409 (fun x y p size t -> (x, y, p, size, t))
2410 with exn ->
2411 dolog "error processing 'tile' %S: %s"
2412 cmds (Printexc.to_string exn);
2413 exit 1;
2415 begin match state.currently with
2416 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2417 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2419 unmappbo opaque;
2420 if tilew != conf.tilew || tileh != conf.tileh
2421 then (
2422 wcmd "freetile %s" opaque;
2423 state.currently <- Idle;
2424 load state.layout;
2426 else (
2427 puttileopaque l col row gen cs angle opaque size t;
2428 state.memused <- state.memused + size;
2429 state.uioh#infochanged Memused;
2430 gctiles ();
2431 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2432 opaque, size) state.tilelru;
2434 let layout =
2435 match state.throttle with
2436 | None -> state.layout
2437 | Some (layout, _, _) -> layout
2440 state.currently <- Idle;
2441 if gen = state.gen
2442 && conf.colorspace = cs
2443 && conf.angle = angle
2444 && tilevisible layout l.pageno x y
2445 then conttiling l.pageno pageopaque;
2447 begin match state.throttle with
2448 | None ->
2449 state.wthack <- false;
2450 preload state.layout;
2451 if gen = state.gen
2452 && conf.colorspace = cs
2453 && conf.angle = angle
2454 && tilevisible state.layout l.pageno x y
2455 then G.postRedisplay "tile nothrottle";
2457 | Some (layout, y, _) ->
2458 let ready = layoutready layout in
2459 if ready
2460 then (
2461 state.wthack <- false;
2462 state.y <- y;
2463 state.layout <- layout;
2464 state.throttle <- None;
2465 G.postRedisplay "throttle";
2467 else load layout;
2468 end;
2471 | _ ->
2472 dolog "Inconsistent tiling state";
2473 logcurrently state.currently;
2474 exit 1
2477 | "pdim" ->
2478 let pdim =
2480 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2481 with exn ->
2482 dolog "error processing 'pdim' %S: %s"
2483 cmds (Printexc.to_string exn);
2484 exit 1;
2486 state.uioh#infochanged Pdim;
2487 state.pdims <- pdim :: state.pdims
2489 | "o" ->
2490 let (l, n, t, h, pos) =
2492 Scanf.sscanf args "%u %u %d %u %n"
2493 (fun l n t h pos -> l, n, t, h, pos)
2494 with exn ->
2495 dolog "error processing 'o' %S: %s"
2496 cmds (Printexc.to_string exn);
2497 exit 1;
2499 let s = String.sub args pos (String.length args - pos) in
2500 let outline = (s, l, (n, float t /. float h, 0.0)) in
2501 begin match state.currently with
2502 | Outlining outlines ->
2503 state.currently <- Outlining (outline :: outlines)
2504 | Idle ->
2505 state.currently <- Outlining [outline]
2506 | currently ->
2507 dolog "invalid outlining state";
2508 logcurrently currently
2511 | "a" ->
2512 let (n, t, h) =
2514 Scanf.sscanf args "%u %u %d"
2515 (fun n t h -> n, t, h)
2516 with exn ->
2517 dolog "error processing 'a' %S: %s"
2518 cmds (Printexc.to_string exn);
2519 exit 1;
2521 let top, dtop =
2522 if conf.presentation
2523 then (0.0, 1.0)
2524 else float t, 0.0
2526 state.anchor <- (n, top /. float h, dtop)
2528 | "info" ->
2529 state.docinfo <- (1, args) :: state.docinfo
2531 | "infoend" ->
2532 state.uioh#infochanged Docinfo;
2533 state.docinfo <- List.rev state.docinfo
2535 | _ ->
2536 dolog "unknown cmd `%S'" cmds
2539 let onhist cb =
2540 let rc = cb.rc in
2541 let action = function
2542 | HCprev -> cbget cb ~-1
2543 | HCnext -> cbget cb 1
2544 | HCfirst -> cbget cb ~-(cb.rc)
2545 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2546 and cancel () = cb.rc <- rc
2547 in (action, cancel)
2550 let search pattern forward =
2551 if String.length pattern > 0
2552 then
2553 let pn, py =
2554 match state.layout with
2555 | [] -> 0, 0
2556 | l :: _ ->
2557 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2559 wcmd "search %d %d %d %d,%s\000"
2560 (btod conf.icase) pn py (btod forward) pattern;
2563 let intentry text key =
2564 let c =
2565 if key >= 32 && key < 127
2566 then Char.chr key
2567 else '\000'
2569 match c with
2570 | '0' .. '9' ->
2571 let text = addchar text c in
2572 TEcont text
2574 | _ ->
2575 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2576 TEcont text
2579 let linknentry text key =
2580 let c =
2581 if key >= 32 && key < 127
2582 then Char.chr key
2583 else '\000'
2585 match c with
2586 | 'a' .. 'z' ->
2587 let text = addchar text c in
2588 TEcont text
2590 | _ ->
2591 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2592 TEcont text
2595 let linkndone f s =
2596 if String.length s > 0
2597 then (
2598 let n =
2599 let l = String.length s in
2600 let rec loop pos n = if pos = l then n else
2601 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2602 loop (pos+1) (n*26 + m)
2603 in loop 0 0
2605 let rec loop n = function
2606 | [] -> ()
2607 | l :: rest ->
2608 match getopaque l.pageno with
2609 | None -> loop n rest
2610 | Some opaque ->
2611 let m = getlinkcount opaque in
2612 if n < m
2613 then (
2614 let under = getlink opaque n in
2615 f under
2617 else loop (n-m) rest
2619 loop n state.layout;
2623 let textentry text key =
2624 if key land 0xff00 = 0xff00
2625 then TEcont text
2626 else TEcont (text ^ Wsi.toutf8 key)
2629 let reqlayout angle proportional =
2630 match state.throttle with
2631 | None ->
2632 if nogeomcmds state.geomcmds
2633 then state.anchor <- getanchor ();
2634 conf.angle <- angle mod 360;
2635 if conf.angle != 0
2636 then (
2637 match state.mode with
2638 | LinkNav _ -> state.mode <- View
2639 | _ -> ()
2641 conf.proportional <- proportional;
2642 invalidate "reqlayout"
2643 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2644 | _ -> ()
2647 let settrim trimmargins trimfuzz =
2648 if nogeomcmds state.geomcmds
2649 then state.anchor <- getanchor ();
2650 conf.trimmargins <- trimmargins;
2651 conf.trimfuzz <- trimfuzz;
2652 let x0, y0, x1, y1 = trimfuzz in
2653 invalidate "settrim"
2654 (fun () ->
2655 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2656 flushpages ();
2659 let setzoom zoom =
2660 match state.throttle with
2661 | None ->
2662 let zoom = max 0.01 zoom in
2663 if zoom <> conf.zoom
2664 then (
2665 state.prevzoom <- conf.zoom;
2666 conf.zoom <- zoom;
2667 reshape conf.winw conf.winh;
2668 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2671 | Some (layout, y, started) ->
2672 let time =
2673 match conf.maxwait with
2674 | None -> 0.0
2675 | Some t -> t
2677 let dt = now () -. started in
2678 if dt > time
2679 then (
2680 state.y <- y;
2681 load layout;
2685 let setcolumns mode columns coverA coverB =
2686 state.prevcolumns <- Some (conf.columns, conf.zoom);
2687 if columns < 0
2688 then (
2689 if isbirdseye mode
2690 then showtext '!' "split mode doesn't work in bird's eye"
2691 else (
2692 conf.columns <- Csplit (-columns, [||]);
2693 state.x <- 0;
2694 conf.zoom <- 1.0;
2697 else (
2698 if columns < 2
2699 then (
2700 conf.columns <- Csingle [||];
2701 state.x <- 0;
2702 setzoom 1.0;
2704 else (
2705 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2706 conf.zoom <- 1.0;
2709 reshape conf.winw conf.winh;
2712 let enterbirdseye () =
2713 let zoom = float conf.thumbw /. float conf.winw in
2714 let birdseyepageno =
2715 let cy = conf.winh / 2 in
2716 let fold = function
2717 | [] -> 0
2718 | l :: rest ->
2719 let rec fold best = function
2720 | [] -> best.pageno
2721 | l :: rest ->
2722 let d = cy - (l.pagedispy + l.pagevh/2)
2723 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2724 if abs d < abs dbest
2725 then fold l rest
2726 else best.pageno
2727 in fold l rest
2729 fold state.layout
2731 state.mode <- Birdseye (
2732 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2734 conf.zoom <- zoom;
2735 conf.presentation <- false;
2736 conf.interpagespace <- 10;
2737 conf.hlinks <- false;
2738 state.x <- 0;
2739 state.mstate <- Mnone;
2740 conf.maxwait <- None;
2741 conf.columns <- (
2742 match conf.beyecolumns with
2743 | Some c ->
2744 conf.zoom <- 1.0;
2745 Cmulti ((c, 0, 0), [||])
2746 | None -> Csingle [||]
2748 Wsi.setcursor Wsi.CURSOR_INHERIT;
2749 if conf.verbose
2750 then
2751 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2752 (100.0*.zoom)
2753 else
2754 state.text <- ""
2756 reshape conf.winw conf.winh;
2759 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2760 state.mode <- View;
2761 conf.zoom <- c.zoom;
2762 conf.presentation <- c.presentation;
2763 conf.interpagespace <- c.interpagespace;
2764 conf.maxwait <- c.maxwait;
2765 conf.hlinks <- c.hlinks;
2766 conf.beyecolumns <- (
2767 match conf.columns with
2768 | Cmulti ((c, _, _), _) -> Some c
2769 | Csingle _ -> None
2770 | Csplit _ -> failwith "leaving bird's eye split mode"
2772 conf.columns <- (
2773 match c.columns with
2774 | Cmulti (c, _) -> Cmulti (c, [||])
2775 | Csingle _ -> Csingle [||]
2776 | Csplit (c, _) -> Csplit (c, [||])
2778 state.x <- leftx;
2779 if conf.verbose
2780 then
2781 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2782 (100.0*.conf.zoom)
2784 reshape conf.winw conf.winh;
2785 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2788 let togglebirdseye () =
2789 match state.mode with
2790 | Birdseye vals -> leavebirdseye vals true
2791 | View -> enterbirdseye ()
2792 | _ -> ()
2795 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2796 let pageno = max 0 (pageno - incr) in
2797 let rec loop = function
2798 | [] -> gotopage1 pageno 0
2799 | l :: _ when l.pageno = pageno ->
2800 if l.pagedispy >= 0 && l.pagey = 0
2801 then G.postRedisplay "upbirdseye"
2802 else gotopage1 pageno 0
2803 | _ :: rest -> loop rest
2805 loop state.layout;
2806 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2809 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2810 let pageno = min (state.pagecount - 1) (pageno + incr) in
2811 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2812 let rec loop = function
2813 | [] ->
2814 let y, h = getpageyh pageno in
2815 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2816 gotoy (clamp dy)
2817 | l :: _ when l.pageno = pageno ->
2818 if l.pagevh != l.pageh
2819 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2820 else G.postRedisplay "downbirdseye"
2821 | _ :: rest -> loop rest
2823 loop state.layout
2826 let optentry mode _ key =
2827 let btos b = if b then "on" else "off" in
2828 if key >= 32 && key < 127
2829 then
2830 let c = Char.chr key in
2831 match c with
2832 | 's' ->
2833 let ondone s =
2834 try conf.scrollstep <- int_of_string s with exc ->
2835 state.text <- Printf.sprintf "bad integer `%s': %s"
2836 s (Printexc.to_string exc)
2838 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2840 | 'A' ->
2841 let ondone s =
2843 conf.autoscrollstep <- int_of_string s;
2844 if state.autoscroll <> None
2845 then state.autoscroll <- Some conf.autoscrollstep
2846 with exc ->
2847 state.text <- Printf.sprintf "bad integer `%s': %s"
2848 s (Printexc.to_string exc)
2850 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2852 | 'C' ->
2853 let ondone s =
2855 let n, a, b = multicolumns_of_string s in
2856 setcolumns mode n a b;
2857 with exc ->
2858 state.text <- Printf.sprintf "bad columns `%s': %s"
2859 s (Printexc.to_string exc)
2861 TEswitch ("columns: ", "", None, textentry, ondone, true)
2863 | 'Z' ->
2864 let ondone s =
2866 let zoom = float (int_of_string s) /. 100.0 in
2867 setzoom zoom
2868 with exc ->
2869 state.text <- Printf.sprintf "bad integer `%s': %s"
2870 s (Printexc.to_string exc)
2872 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2874 | 't' ->
2875 let ondone s =
2877 conf.thumbw <- bound (int_of_string s) 2 4096;
2878 state.text <-
2879 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2880 begin match mode with
2881 | Birdseye beye ->
2882 leavebirdseye beye false;
2883 enterbirdseye ();
2884 | _ -> ();
2886 with exc ->
2887 state.text <- Printf.sprintf "bad integer `%s': %s"
2888 s (Printexc.to_string exc)
2890 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2892 | 'R' ->
2893 let ondone s =
2894 match try
2895 Some (int_of_string s)
2896 with exc ->
2897 state.text <- Printf.sprintf "bad integer `%s': %s"
2898 s (Printexc.to_string exc);
2899 None
2900 with
2901 | Some angle -> reqlayout angle conf.proportional
2902 | None -> ()
2904 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2906 | 'i' ->
2907 conf.icase <- not conf.icase;
2908 TEdone ("case insensitive search " ^ (btos conf.icase))
2910 | 'p' ->
2911 conf.preload <- not conf.preload;
2912 gotoy state.y;
2913 TEdone ("preload " ^ (btos conf.preload))
2915 | 'v' ->
2916 conf.verbose <- not conf.verbose;
2917 TEdone ("verbose " ^ (btos conf.verbose))
2919 | 'd' ->
2920 conf.debug <- not conf.debug;
2921 TEdone ("debug " ^ (btos conf.debug))
2923 | 'h' ->
2924 conf.maxhfit <- not conf.maxhfit;
2925 state.maxy <- calcheight ();
2926 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2928 | 'c' ->
2929 conf.crophack <- not conf.crophack;
2930 TEdone ("crophack " ^ btos conf.crophack)
2932 | 'a' ->
2933 let s =
2934 match conf.maxwait with
2935 | None ->
2936 conf.maxwait <- Some infinity;
2937 "always wait for page to complete"
2938 | Some _ ->
2939 conf.maxwait <- None;
2940 "show placeholder if page is not ready"
2942 TEdone s
2944 | 'f' ->
2945 conf.underinfo <- not conf.underinfo;
2946 TEdone ("underinfo " ^ btos conf.underinfo)
2948 | 'P' ->
2949 conf.savebmarks <- not conf.savebmarks;
2950 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2952 | 'S' ->
2953 let ondone s =
2955 let pageno, py =
2956 match state.layout with
2957 | [] -> 0, 0
2958 | l :: _ ->
2959 l.pageno, l.pagey
2961 conf.interpagespace <- int_of_string s;
2962 docolumns conf.columns;
2963 state.maxy <- calcheight ();
2964 let y = getpagey pageno in
2965 gotoy (y + py)
2966 with exc ->
2967 state.text <- Printf.sprintf "bad integer `%s': %s"
2968 s (Printexc.to_string exc)
2970 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2972 | 'l' ->
2973 reqlayout conf.angle (not conf.proportional);
2974 TEdone ("proportional display " ^ btos conf.proportional)
2976 | 'T' ->
2977 settrim (not conf.trimmargins) conf.trimfuzz;
2978 TEdone ("trim margins " ^ btos conf.trimmargins)
2980 | 'I' ->
2981 conf.invert <- not conf.invert;
2982 TEdone ("invert colors " ^ btos conf.invert)
2984 | 'x' ->
2985 let ondone s =
2986 cbput state.hists.sel s;
2987 conf.selcmd <- s;
2989 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2990 textentry, ondone, true)
2992 | _ ->
2993 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2994 TEstop
2995 else
2996 TEcont state.text
2999 class type lvsource = object
3000 method getitemcount : int
3001 method getitem : int -> (string * int)
3002 method hasaction : int -> bool
3003 method exit :
3004 uioh:uioh ->
3005 cancel:bool ->
3006 active:int ->
3007 first:int ->
3008 pan:int ->
3009 qsearch:string ->
3010 uioh option
3011 method getactive : int
3012 method getfirst : int
3013 method getqsearch : string
3014 method setqsearch : string -> unit
3015 method getpan : int
3016 end;;
3018 class virtual lvsourcebase = object
3019 val mutable m_active = 0
3020 val mutable m_first = 0
3021 val mutable m_qsearch = ""
3022 val mutable m_pan = 0
3023 method getactive = m_active
3024 method getfirst = m_first
3025 method getqsearch = m_qsearch
3026 method getpan = m_pan
3027 method setqsearch s = m_qsearch <- s
3028 end;;
3030 let withoutlastutf8 s =
3031 let len = String.length s in
3032 if len = 0
3033 then s
3034 else
3035 let rec find pos =
3036 if pos = 0
3037 then pos
3038 else
3039 let b = Char.code s.[pos] in
3040 if b land 0b11000000 = 0b11000000
3041 then pos
3042 else find (pos-1)
3044 let first =
3045 if Char.code s.[len-1] land 0x80 = 0
3046 then len-1
3047 else find (len-1)
3049 String.sub s 0 first;
3052 let textentrykeyboard
3053 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3054 let key =
3055 if key >= 0xffb0 && key <= 0xffb9
3056 then key - 0xffb0 + 48 else key
3058 let enttext te =
3059 state.mode <- Textentry (te, onleave);
3060 state.text <- "";
3061 enttext ();
3062 G.postRedisplay "textentrykeyboard enttext";
3064 let histaction cmd =
3065 match opthist with
3066 | None -> ()
3067 | Some (action, _) ->
3068 state.mode <- Textentry (
3069 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3071 G.postRedisplay "textentry histaction"
3073 match key with
3074 | 0xff08 -> (* backspace *)
3075 let s = withoutlastutf8 text in
3076 let len = String.length s in
3077 if cancelonempty && len = 0
3078 then (
3079 onleave Cancel;
3080 G.postRedisplay "textentrykeyboard after cancel";
3082 else (
3083 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3086 | 0xff0d | 0xff8d -> (* (kp) enter *)
3087 ondone text;
3088 onleave Confirm;
3089 G.postRedisplay "textentrykeyboard after confirm"
3091 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3092 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3093 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3094 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3096 | 0xff1b -> (* escape*)
3097 if String.length text = 0
3098 then (
3099 begin match opthist with
3100 | None -> ()
3101 | Some (_, onhistcancel) -> onhistcancel ()
3102 end;
3103 onleave Cancel;
3104 state.text <- "";
3105 G.postRedisplay "textentrykeyboard after cancel2"
3107 else (
3108 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3111 | 0xff9f | 0xffff -> () (* delete *)
3113 | _ when key != 0
3114 && key land 0xff00 != 0xff00 (* keyboard *)
3115 && key land 0xfe00 != 0xfe00 (* xkb *)
3116 && key land 0xfd00 != 0xfd00 (* 3270 *)
3118 begin match onkey text key with
3119 | TEdone text ->
3120 ondone text;
3121 onleave Confirm;
3122 G.postRedisplay "textentrykeyboard after confirm2";
3124 | TEcont text ->
3125 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3127 | TEstop ->
3128 onleave Cancel;
3129 G.postRedisplay "textentrykeyboard after cancel3"
3131 | TEswitch te ->
3132 state.mode <- Textentry (te, onleave);
3133 G.postRedisplay "textentrykeyboard switch";
3134 end;
3136 | _ ->
3137 vlog "unhandled key %s" (Wsi.keyname key)
3140 let firstof first active =
3141 if first > active || abs (first - active) > fstate.maxrows - 1
3142 then max 0 (active - (fstate.maxrows/2))
3143 else first
3146 let calcfirst first active =
3147 if active > first
3148 then
3149 let rows = active - first in
3150 if rows > fstate.maxrows then active - fstate.maxrows else first
3151 else active
3154 let scrollph y maxy =
3155 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3156 let sh = float conf.winh /. sh in
3157 let sh = max sh (float conf.scrollh) in
3159 let percent =
3160 if y = state.maxy
3161 then 1.0
3162 else float y /. float maxy
3164 let position = (float conf.winh -. sh) *. percent in
3166 let position =
3167 if position +. sh > float conf.winh
3168 then float conf.winh -. sh
3169 else position
3171 position, sh;
3174 let coe s = (s :> uioh);;
3176 class listview ~(source:lvsource) ~trusted ~modehash =
3177 object (self)
3178 val m_pan = source#getpan
3179 val m_first = source#getfirst
3180 val m_active = source#getactive
3181 val m_qsearch = source#getqsearch
3182 val m_prev_uioh = state.uioh
3184 method private elemunder y =
3185 let n = y / (fstate.fontsize+1) in
3186 if m_first + n < source#getitemcount
3187 then (
3188 if source#hasaction (m_first + n)
3189 then Some (m_first + n)
3190 else None
3192 else None
3194 method display =
3195 Gl.enable `blend;
3196 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3197 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3198 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3199 GlDraw.color (1., 1., 1.);
3200 Gl.enable `texture_2d;
3201 let fs = fstate.fontsize in
3202 let nfs = fs + 1 in
3203 let ww = fstate.wwidth in
3204 let tabw = 30.0*.ww in
3205 let itemcount = source#getitemcount in
3206 let rec loop row =
3207 if (row - m_first) > fstate.maxrows
3208 then ()
3209 else (
3210 if row >= 0 && row < itemcount
3211 then (
3212 let (s, level) = source#getitem row in
3213 let y = (row - m_first) * nfs in
3214 let x = 5.0 +. float (level + m_pan) *. ww in
3215 if row = m_active
3216 then (
3217 Gl.disable `texture_2d;
3218 GlDraw.polygon_mode `both `line;
3219 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3220 GlDraw.rect (1., float (y + 1))
3221 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3222 GlDraw.polygon_mode `both `fill;
3223 GlDraw.color (1., 1., 1.);
3224 Gl.enable `texture_2d;
3227 let drawtabularstring s =
3228 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3229 if trusted
3230 then
3231 let tabpos = try String.index s '\t' with Not_found -> -1 in
3232 if tabpos > 0
3233 then
3234 let len = String.length s - tabpos - 1 in
3235 let s1 = String.sub s 0 tabpos
3236 and s2 = String.sub s (tabpos + 1) len in
3237 let nx = drawstr x s1 in
3238 let sw = nx -. x in
3239 let x = x +. (max tabw sw) in
3240 drawstr x s2
3241 else
3242 drawstr x s
3243 else
3244 drawstr x s
3246 let _ = drawtabularstring s in
3247 loop (row+1)
3251 loop m_first;
3252 Gl.disable `blend;
3253 Gl.disable `texture_2d;
3255 method updownlevel incr =
3256 let len = source#getitemcount in
3257 let curlevel =
3258 if m_active >= 0 && m_active < len
3259 then snd (source#getitem m_active)
3260 else -1
3262 let rec flow i =
3263 if i = len then i-1 else if i = -1 then 0 else
3264 let _, l = source#getitem i in
3265 if l != curlevel then i else flow (i+incr)
3267 let active = flow m_active in
3268 let first = calcfirst m_first active in
3269 G.postRedisplay "outline updownlevel";
3270 {< m_active = active; m_first = first >}
3272 method private key1 key mask =
3273 let set1 active first qsearch =
3274 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3276 let search active pattern incr =
3277 let dosearch re =
3278 let rec loop n =
3279 if n >= 0 && n < source#getitemcount
3280 then (
3281 let s, _ = source#getitem n in
3283 (try ignore (Str.search_forward re s 0); true
3284 with Not_found -> false)
3285 then Some n
3286 else loop (n + incr)
3288 else None
3290 loop active
3293 let re = Str.regexp_case_fold pattern in
3294 dosearch re
3295 with Failure s ->
3296 state.text <- s;
3297 None
3299 let itemcount = source#getitemcount in
3300 let find start incr =
3301 let rec find i =
3302 if i = -1 || i = itemcount
3303 then -1
3304 else (
3305 if source#hasaction i
3306 then i
3307 else find (i + incr)
3310 find start
3312 let set active first =
3313 let first = bound first 0 (itemcount - fstate.maxrows) in
3314 state.text <- "";
3315 coe {< m_active = active; m_first = first >}
3317 let navigate incr =
3318 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3319 let active, first =
3320 let incr1 = if incr > 0 then 1 else -1 in
3321 if isvisible m_first m_active
3322 then
3323 let next =
3324 let next = m_active + incr in
3325 let next =
3326 if next < 0 || next >= itemcount
3327 then -1
3328 else find next incr1
3330 if next = -1 || abs (m_active - next) > fstate.maxrows
3331 then -1
3332 else next
3334 if next = -1
3335 then
3336 let first = m_first + incr in
3337 let first = bound first 0 (itemcount - 1) in
3338 let next =
3339 let next = m_active + incr in
3340 let next = bound next 0 (itemcount - 1) in
3341 find next ~-incr1
3343 let active = if next = -1 then m_active else next in
3344 active, first
3345 else
3346 let first = min next m_first in
3347 let first =
3348 if abs (next - first) > fstate.maxrows
3349 then first + incr
3350 else first
3352 next, first
3353 else
3354 let first = m_first + incr in
3355 let first = bound first 0 (itemcount - 1) in
3356 let active =
3357 let next = m_active + incr in
3358 let next = bound next 0 (itemcount - 1) in
3359 let next = find next incr1 in
3360 let active =
3361 if next = -1 || abs (m_active - first) > fstate.maxrows
3362 then (
3363 let active = if m_active = -1 then next else m_active in
3364 active
3366 else next
3368 if isvisible first active
3369 then active
3370 else -1
3372 active, first
3374 G.postRedisplay "listview navigate";
3375 set active first;
3377 match key with
3378 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3379 let incr = if key = 0x72 then -1 else 1 in
3380 let active, first =
3381 match search (m_active + incr) m_qsearch incr with
3382 | None ->
3383 state.text <- m_qsearch ^ " [not found]";
3384 m_active, m_first
3385 | Some active ->
3386 state.text <- m_qsearch;
3387 active, firstof m_first active
3389 G.postRedisplay "listview ctrl-r/s";
3390 set1 active first m_qsearch;
3392 | 0xff08 -> (* backspace *)
3393 if String.length m_qsearch = 0
3394 then coe self
3395 else (
3396 let qsearch = withoutlastutf8 m_qsearch in
3397 let len = String.length qsearch in
3398 if len = 0
3399 then (
3400 state.text <- "";
3401 G.postRedisplay "listview empty qsearch";
3402 set1 m_active m_first "";
3404 else
3405 let active, first =
3406 match search m_active qsearch ~-1 with
3407 | None ->
3408 state.text <- qsearch ^ " [not found]";
3409 m_active, m_first
3410 | Some active ->
3411 state.text <- qsearch;
3412 active, firstof m_first active
3414 G.postRedisplay "listview backspace qsearch";
3415 set1 active first qsearch
3418 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3419 let pattern = m_qsearch ^ Wsi.toutf8 key in
3420 let active, first =
3421 match search m_active pattern 1 with
3422 | None ->
3423 state.text <- pattern ^ " [not found]";
3424 m_active, m_first
3425 | Some active ->
3426 state.text <- pattern;
3427 active, firstof m_first active
3429 G.postRedisplay "listview qsearch add";
3430 set1 active first pattern;
3432 | 0xff1b -> (* escape *)
3433 state.text <- "";
3434 if String.length m_qsearch = 0
3435 then (
3436 G.postRedisplay "list view escape";
3437 begin
3438 match
3439 source#exit (coe self) true m_active m_first m_pan m_qsearch
3440 with
3441 | None -> m_prev_uioh
3442 | Some uioh -> uioh
3445 else (
3446 G.postRedisplay "list view kill qsearch";
3447 source#setqsearch "";
3448 coe {< m_qsearch = "" >}
3451 | 0xff0d | 0xff8d -> (* (kp) enter *)
3452 state.text <- "";
3453 let self = {< m_qsearch = "" >} in
3454 source#setqsearch "";
3455 let opt =
3456 G.postRedisplay "listview enter";
3457 if m_active >= 0 && m_active < source#getitemcount
3458 then (
3459 source#exit (coe self) false m_active m_first m_pan "";
3461 else (
3462 source#exit (coe self) true m_active m_first m_pan "";
3465 begin match opt with
3466 | None -> m_prev_uioh
3467 | Some uioh -> uioh
3470 | 0xff9f | 0xffff -> (* (kp) delete *)
3471 coe self
3473 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3474 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3475 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3476 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3478 | 0xff53 | 0xff98 -> (* (kp) right *)
3479 state.text <- "";
3480 G.postRedisplay "listview right";
3481 coe {< m_pan = m_pan - 1 >}
3483 | 0xff51 | 0xff96 -> (* (kp) left *)
3484 state.text <- "";
3485 G.postRedisplay "listview left";
3486 coe {< m_pan = m_pan + 1 >}
3488 | 0xff50 | 0xff95 -> (* (kp) home *)
3489 let active = find 0 1 in
3490 G.postRedisplay "listview home";
3491 set active 0;
3493 | 0xff57 | 0xff9c -> (* (kp) end *)
3494 let first = max 0 (itemcount - fstate.maxrows) in
3495 let active = find (itemcount - 1) ~-1 in
3496 G.postRedisplay "listview end";
3497 set active first;
3499 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3500 coe self
3502 | _ ->
3503 dolog "listview unknown key %#x" key; coe self
3505 method key key mask =
3506 match state.mode with
3507 | Textentry te -> textentrykeyboard key mask te; coe self
3508 | _ -> self#key1 key mask
3510 method button button down x y _ =
3511 let opt =
3512 match button with
3513 | 1 when x > conf.winw - conf.scrollbw ->
3514 G.postRedisplay "listview scroll";
3515 if down
3516 then
3517 let _, position, sh = self#scrollph in
3518 if y > truncate position && y < truncate (position +. sh)
3519 then (
3520 state.mstate <- Mscrolly;
3521 Some (coe self)
3523 else
3524 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3525 let first = truncate (s *. float source#getitemcount) in
3526 let first = min source#getitemcount first in
3527 Some (coe {< m_first = first; m_active = first >})
3528 else (
3529 state.mstate <- Mnone;
3530 Some (coe self);
3532 | 1 when not down ->
3533 begin match self#elemunder y with
3534 | Some n ->
3535 G.postRedisplay "listview click";
3536 source#exit
3537 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3538 | _ ->
3539 Some (coe self)
3541 | n when (n == 4 || n == 5) && not down ->
3542 let len = source#getitemcount in
3543 let first =
3544 if n = 5 && m_first + fstate.maxrows >= len
3545 then
3546 m_first
3547 else
3548 let first = m_first + (if n == 4 then -1 else 1) in
3549 bound first 0 (len - 1)
3551 G.postRedisplay "listview wheel";
3552 Some (coe {< m_first = first >})
3553 | n when (n = 6 || n = 7) && not down ->
3554 let inc = m_first + (if n = 7 then -1 else 1) in
3555 G.postRedisplay "listview hwheel";
3556 Some (coe {< m_pan = m_pan + inc >})
3557 | _ ->
3558 Some (coe self)
3560 match opt with
3561 | None -> m_prev_uioh
3562 | Some uioh -> uioh
3564 method motion _ y =
3565 match state.mstate with
3566 | Mscrolly ->
3567 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3568 let first = truncate (s *. float source#getitemcount) in
3569 let first = min source#getitemcount first in
3570 G.postRedisplay "listview motion";
3571 coe {< m_first = first; m_active = first >}
3572 | _ -> coe self
3574 method pmotion x y =
3575 if x < conf.winw - conf.scrollbw
3576 then
3577 let n =
3578 match self#elemunder y with
3579 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3580 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3582 let o =
3583 if n != m_active
3584 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3585 else self
3587 coe o
3588 else (
3589 Wsi.setcursor Wsi.CURSOR_INHERIT;
3590 coe self
3593 method infochanged _ = ()
3595 method scrollpw = (0, 0.0, 0.0)
3596 method scrollph =
3597 let nfs = fstate.fontsize + 1 in
3598 let y = m_first * nfs in
3599 let itemcount = source#getitemcount in
3600 let maxi = max 0 (itemcount - fstate.maxrows) in
3601 let maxy = maxi * nfs in
3602 let p, h = scrollph y maxy in
3603 conf.scrollbw, p, h
3605 method modehash = modehash
3606 end;;
3608 class outlinelistview ~source =
3609 object (self)
3610 inherit listview
3611 ~source:(source :> lvsource)
3612 ~trusted:false
3613 ~modehash:(findkeyhash conf "outline")
3614 as super
3616 method key key mask =
3617 let calcfirst first active =
3618 if active > first
3619 then
3620 let rows = active - first in
3621 let maxrows =
3622 if String.length state.text = 0
3623 then fstate.maxrows
3624 else fstate.maxrows - 2
3626 if rows > maxrows then active - maxrows else first
3627 else active
3629 let navigate incr =
3630 let active = m_active + incr in
3631 let active = bound active 0 (source#getitemcount - 1) in
3632 let first = calcfirst m_first active in
3633 G.postRedisplay "outline navigate";
3634 coe {< m_active = active; m_first = first >}
3636 let ctrl = Wsi.withctrl mask in
3637 match key with
3638 | 110 when ctrl -> (* ctrl-n *)
3639 source#narrow m_qsearch;
3640 G.postRedisplay "outline ctrl-n";
3641 coe {< m_first = 0; m_active = 0 >}
3643 | 117 when ctrl -> (* ctrl-u *)
3644 source#denarrow;
3645 G.postRedisplay "outline ctrl-u";
3646 state.text <- "";
3647 coe {< m_first = 0; m_active = 0 >}
3649 | 108 when ctrl -> (* ctrl-l *)
3650 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3651 G.postRedisplay "outline ctrl-l";
3652 coe {< m_first = first >}
3654 | 0xff9f | 0xffff -> (* (kp) delete *)
3655 source#remove m_active;
3656 G.postRedisplay "outline delete";
3657 let active = max 0 (m_active-1) in
3658 coe {< m_first = firstof m_first active;
3659 m_active = active >}
3661 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3662 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3663 | 0xff55 | 0xff9a -> (* (kp) prior *)
3664 navigate ~-(fstate.maxrows)
3665 | 0xff56 | 0xff9b -> (* (kp) next *)
3666 navigate fstate.maxrows
3668 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3669 let o =
3670 if ctrl
3671 then (
3672 G.postRedisplay "outline ctrl right";
3673 {< m_pan = m_pan + 1 >}
3675 else self#updownlevel 1
3677 coe o
3679 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3680 let o =
3681 if ctrl
3682 then (
3683 G.postRedisplay "outline ctrl left";
3684 {< m_pan = m_pan - 1 >}
3686 else self#updownlevel ~-1
3688 coe o
3690 | 0xff50 | 0xff95 -> (* (kp) home *)
3691 G.postRedisplay "outline home";
3692 coe {< m_first = 0; m_active = 0 >}
3694 | 0xff57 | 0xff9c -> (* (kp) end *)
3695 let active = source#getitemcount - 1 in
3696 let first = max 0 (active - fstate.maxrows) in
3697 G.postRedisplay "outline end";
3698 coe {< m_active = active; m_first = first >}
3700 | _ -> super#key key mask
3703 let outlinesource usebookmarks =
3704 let empty = [||] in
3705 (object
3706 inherit lvsourcebase
3707 val mutable m_items = empty
3708 val mutable m_orig_items = empty
3709 val mutable m_prev_items = empty
3710 val mutable m_narrow_pattern = ""
3711 val mutable m_hadremovals = false
3713 method getitemcount =
3714 Array.length m_items + (if m_hadremovals then 1 else 0)
3716 method getitem n =
3717 if n == Array.length m_items && m_hadremovals
3718 then
3719 ("[Confirm removal]", 0)
3720 else
3721 let s, n, _ = m_items.(n) in
3722 (s, n)
3724 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3725 ignore (uioh, first, qsearch);
3726 let confrimremoval = m_hadremovals && active = Array.length m_items in
3727 let items =
3728 if String.length m_narrow_pattern = 0
3729 then m_orig_items
3730 else m_items
3732 if not cancel
3733 then (
3734 if not confrimremoval
3735 then(
3736 let _, _, anchor = m_items.(active) in
3737 gotoghyll (getanchory anchor);
3738 m_items <- items;
3740 else (
3741 state.bookmarks <- Array.to_list m_items;
3742 m_orig_items <- m_items;
3745 else m_items <- items;
3746 m_pan <- pan;
3747 None
3749 method hasaction _ = true
3751 method greetmsg =
3752 if Array.length m_items != Array.length m_orig_items
3753 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3754 else ""
3756 method narrow pattern =
3757 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3758 match reopt with
3759 | None -> ()
3760 | Some re ->
3761 let rec loop accu n =
3762 if n = -1
3763 then (
3764 m_narrow_pattern <- pattern;
3765 m_items <- Array.of_list accu
3767 else
3768 let (s, _, _) as o = m_items.(n) in
3769 let accu =
3770 if (try ignore (Str.search_forward re s 0); true
3771 with Not_found -> false)
3772 then o :: accu
3773 else accu
3775 loop accu (n-1)
3777 loop [] (Array.length m_items - 1)
3779 method denarrow =
3780 m_orig_items <- (
3781 if usebookmarks
3782 then Array.of_list state.bookmarks
3783 else state.outlines
3785 m_items <- m_orig_items
3787 method remove m =
3788 if usebookmarks
3789 then
3790 if m >= 0 && m < Array.length m_items
3791 then (
3792 m_hadremovals <- true;
3793 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3794 let n = if n >= m then n+1 else n in
3795 m_items.(n)
3799 method reset anchor items =
3800 m_hadremovals <- false;
3801 if m_orig_items == empty || m_prev_items != items
3802 then (
3803 m_orig_items <- items;
3804 if String.length m_narrow_pattern = 0
3805 then m_items <- items;
3807 m_prev_items <- items;
3808 let rely = getanchory anchor in
3809 let active =
3810 let rec loop n best bestd =
3811 if n = Array.length m_items
3812 then best
3813 else
3814 let (_, _, anchor) = m_items.(n) in
3815 let orely = getanchory anchor in
3816 let d = abs (orely - rely) in
3817 if d < bestd
3818 then loop (n+1) n d
3819 else loop (n+1) best bestd
3821 loop 0 ~-1 max_int
3823 m_active <- active;
3824 m_first <- firstof m_first active
3825 end)
3828 let enterselector usebookmarks =
3829 let source = outlinesource usebookmarks in
3830 fun errmsg ->
3831 let outlines =
3832 if usebookmarks
3833 then Array.of_list state.bookmarks
3834 else state.outlines
3836 if Array.length outlines = 0
3837 then (
3838 showtext ' ' errmsg;
3840 else (
3841 state.text <- source#greetmsg;
3842 Wsi.setcursor Wsi.CURSOR_INHERIT;
3843 let anchor = getanchor () in
3844 source#reset anchor outlines;
3845 state.uioh <- coe (new outlinelistview ~source);
3846 G.postRedisplay "enter selector";
3850 let enteroutlinemode =
3851 let f = enterselector false in
3852 fun ()-> f "Document has no outline";
3855 let enterbookmarkmode =
3856 let f = enterselector true in
3857 fun () -> f "Document has no bookmarks (yet)";
3860 let color_of_string s =
3861 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3862 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3866 let color_to_string (r, g, b) =
3867 let r = truncate (r *. 256.0)
3868 and g = truncate (g *. 256.0)
3869 and b = truncate (b *. 256.0) in
3870 Printf.sprintf "%d/%d/%d" r g b
3873 let irect_of_string s =
3874 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3877 let irect_to_string (x0,y0,x1,y1) =
3878 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3881 let makecheckers () =
3882 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3883 following to say:
3884 converted by Issac Trotts. July 25, 2002 *)
3885 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3886 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3887 let id = GlTex.gen_texture () in
3888 GlTex.bind_texture `texture_2d id;
3889 GlPix.store (`unpack_alignment 1);
3890 GlTex.image2d image;
3891 List.iter (GlTex.parameter ~target:`texture_2d)
3892 [ `wrap_s `repeat;
3893 `wrap_t `repeat;
3894 `mag_filter `nearest;
3895 `min_filter `nearest ];
3899 let setcheckers enabled =
3900 match state.texid with
3901 | None ->
3902 if enabled then state.texid <- Some (makecheckers ())
3904 | Some texid ->
3905 if not enabled
3906 then (
3907 GlTex.delete_texture texid;
3908 state.texid <- None;
3912 let int_of_string_with_suffix s =
3913 let l = String.length s in
3914 let s1, shift =
3915 if l > 1
3916 then
3917 let suffix = Char.lowercase s.[l-1] in
3918 match suffix with
3919 | 'k' -> String.sub s 0 (l-1), 10
3920 | 'm' -> String.sub s 0 (l-1), 20
3921 | 'g' -> String.sub s 0 (l-1), 30
3922 | _ -> s, 0
3923 else s, 0
3925 let n = int_of_string s1 in
3926 let m = n lsl shift in
3927 if m < 0 || m < n
3928 then raise (Failure "value too large")
3929 else m
3932 let string_with_suffix_of_int n =
3933 if n = 0
3934 then "0"
3935 else
3936 let n, s =
3937 if n land ((1 lsl 30) - 1) = 0
3938 then n lsr 30, "G"
3939 else (
3940 if n land ((1 lsl 20) - 1) = 0
3941 then n lsr 20, "M"
3942 else (
3943 if n land ((1 lsl 10) - 1) = 0
3944 then n lsr 10, "K"
3945 else n, ""
3949 let rec loop s n =
3950 let h = n mod 1000 in
3951 let n = n / 1000 in
3952 if n = 0
3953 then string_of_int h ^ s
3954 else (
3955 let s = Printf.sprintf "_%03d%s" h s in
3956 loop s n
3959 loop "" n ^ s;
3962 let defghyllscroll = (40, 8, 32);;
3963 let ghyllscroll_of_string s =
3964 let (n, a, b) as nab =
3965 if s = "default"
3966 then defghyllscroll
3967 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3969 if n <= a || n <= b || a >= b
3970 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3971 nab;
3974 let ghyllscroll_to_string ((n, a, b) as nab) =
3975 if nab = defghyllscroll
3976 then "default"
3977 else Printf.sprintf "%d,%d,%d" n a b;
3980 let describe_location () =
3981 let f (fn, _) l =
3982 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3984 let fn, ln = List.fold_left f (-1, -1) state.layout in
3985 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3986 let percent =
3987 if maxy <= 0
3988 then 100.
3989 else (100. *. (float state.y /. float maxy))
3991 if fn = ln
3992 then
3993 Printf.sprintf "page %d of %d [%.2f%%]"
3994 (fn+1) state.pagecount percent
3995 else
3996 Printf.sprintf
3997 "pages %d-%d of %d [%.2f%%]"
3998 (fn+1) (ln+1) state.pagecount percent
4001 let setpresentationmode v =
4002 let (n, _, _) = getanchor () in
4003 let _, h = getpageyh n in
4004 let ips = if conf.presentation then calcips h else conf.interpagespace in
4005 state.anchor <- (n, 0.0, float ips);
4006 conf.presentation <- v;
4007 if conf.presentation
4008 then (
4009 if not conf.scrollbarinpm
4010 then state.scrollw <- 0;
4012 else state.scrollw <- conf.scrollbw;
4013 represent ();
4016 let enterinfomode =
4017 let btos b = if b then "\xe2\x88\x9a" else "" in
4018 let showextended = ref false in
4019 let leave mode = function
4020 | Confirm -> state.mode <- mode
4021 | Cancel -> state.mode <- mode in
4022 let src =
4023 (object
4024 val mutable m_first_time = true
4025 val mutable m_l = []
4026 val mutable m_a = [||]
4027 val mutable m_prev_uioh = nouioh
4028 val mutable m_prev_mode = View
4030 inherit lvsourcebase
4032 method reset prev_mode prev_uioh =
4033 m_a <- Array.of_list (List.rev m_l);
4034 m_l <- [];
4035 m_prev_mode <- prev_mode;
4036 m_prev_uioh <- prev_uioh;
4037 if m_first_time
4038 then (
4039 let rec loop n =
4040 if n >= Array.length m_a
4041 then ()
4042 else
4043 match m_a.(n) with
4044 | _, _, _, Action _ -> m_active <- n
4045 | _ -> loop (n+1)
4047 loop 0;
4048 m_first_time <- false;
4051 method int name get set =
4052 m_l <-
4053 (name, `int get, 1, Action (
4054 fun u ->
4055 let ondone s =
4056 try set (int_of_string s)
4057 with exn ->
4058 state.text <- Printf.sprintf "bad integer `%s': %s"
4059 s (Printexc.to_string exn)
4061 state.text <- "";
4062 let te = name ^ ": ", "", None, intentry, ondone, true in
4063 state.mode <- Textentry (te, leave m_prev_mode);
4065 )) :: m_l
4067 method int_with_suffix name get set =
4068 m_l <-
4069 (name, `intws get, 1, Action (
4070 fun u ->
4071 let ondone s =
4072 try set (int_of_string_with_suffix s)
4073 with exn ->
4074 state.text <- Printf.sprintf "bad integer `%s': %s"
4075 s (Printexc.to_string exn)
4077 state.text <- "";
4078 let te =
4079 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4081 state.mode <- Textentry (te, leave m_prev_mode);
4083 )) :: m_l
4085 method bool ?(offset=1) ?(btos=btos) name get set =
4086 m_l <-
4087 (name, `bool (btos, get), offset, Action (
4088 fun u ->
4089 let v = get () in
4090 set (not v);
4092 )) :: m_l
4094 method color name get set =
4095 m_l <-
4096 (name, `color get, 1, Action (
4097 fun u ->
4098 let invalid = (nan, nan, nan) in
4099 let ondone s =
4100 let c =
4101 try color_of_string s
4102 with exn ->
4103 state.text <- Printf.sprintf "bad color `%s': %s"
4104 s (Printexc.to_string exn);
4105 invalid
4107 if c <> invalid
4108 then set c;
4110 let te = name ^ ": ", "", None, textentry, ondone, true in
4111 state.text <- color_to_string (get ());
4112 state.mode <- Textentry (te, leave m_prev_mode);
4114 )) :: m_l
4116 method string name get set =
4117 m_l <-
4118 (name, `string get, 1, Action (
4119 fun u ->
4120 let ondone s = set s in
4121 let te = name ^ ": ", "", None, textentry, ondone, true in
4122 state.mode <- Textentry (te, leave m_prev_mode);
4124 )) :: m_l
4126 method colorspace name get set =
4127 m_l <-
4128 (name, `string get, 1, Action (
4129 fun _ ->
4130 let source =
4131 let vals = [| "rgb"; "bgr"; "gray" |] in
4132 (object
4133 inherit lvsourcebase
4135 initializer
4136 m_active <- int_of_colorspace conf.colorspace;
4137 m_first <- 0;
4139 method getitemcount = Array.length vals
4140 method getitem n = (vals.(n), 0)
4141 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4142 ignore (uioh, first, pan, qsearch);
4143 if not cancel then set active;
4144 None
4145 method hasaction _ = true
4146 end)
4148 state.text <- "";
4149 let modehash = findkeyhash conf "info" in
4150 coe (new listview ~source ~trusted:true ~modehash)
4151 )) :: m_l
4153 method caption s offset =
4154 m_l <- (s, `empty, offset, Noaction) :: m_l
4156 method caption2 s f offset =
4157 m_l <- (s, `string f, offset, Noaction) :: m_l
4159 method getitemcount = Array.length m_a
4161 method getitem n =
4162 let tostr = function
4163 | `int f -> string_of_int (f ())
4164 | `intws f -> string_with_suffix_of_int (f ())
4165 | `string f -> f ()
4166 | `color f -> color_to_string (f ())
4167 | `bool (btos, f) -> btos (f ())
4168 | `empty -> ""
4170 let name, t, offset, _ = m_a.(n) in
4171 ((let s = tostr t in
4172 if String.length s > 0
4173 then Printf.sprintf "%s\t%s" name s
4174 else name),
4175 offset)
4177 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4178 let uiohopt =
4179 if not cancel
4180 then (
4181 m_qsearch <- qsearch;
4182 let uioh =
4183 match m_a.(active) with
4184 | _, _, _, Action f -> f uioh
4185 | _ -> uioh
4187 Some uioh
4189 else None
4191 m_active <- active;
4192 m_first <- first;
4193 m_pan <- pan;
4194 uiohopt
4196 method hasaction n =
4197 match m_a.(n) with
4198 | _, _, _, Action _ -> true
4199 | _ -> false
4200 end)
4202 let rec fillsrc prevmode prevuioh =
4203 let sep () = src#caption "" 0 in
4204 let colorp name get set =
4205 src#string name
4206 (fun () -> color_to_string (get ()))
4207 (fun v ->
4209 let c = color_of_string v in
4210 set c
4211 with exn ->
4212 state.text <- Printf.sprintf "bad color `%s': %s"
4213 v (Printexc.to_string exn);
4216 let oldmode = state.mode in
4217 let birdseye = isbirdseye state.mode in
4219 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4221 src#bool "presentation mode"
4222 (fun () -> conf.presentation)
4223 (fun v -> setpresentationmode v);
4225 src#bool "ignore case in searches"
4226 (fun () -> conf.icase)
4227 (fun v -> conf.icase <- v);
4229 src#bool "preload"
4230 (fun () -> conf.preload)
4231 (fun v -> conf.preload <- v);
4233 src#bool "highlight links"
4234 (fun () -> conf.hlinks)
4235 (fun v -> conf.hlinks <- v);
4237 src#bool "under info"
4238 (fun () -> conf.underinfo)
4239 (fun v -> conf.underinfo <- v);
4241 src#bool "persistent bookmarks"
4242 (fun () -> conf.savebmarks)
4243 (fun v -> conf.savebmarks <- v);
4245 src#bool "proportional display"
4246 (fun () -> conf.proportional)
4247 (fun v -> reqlayout conf.angle v);
4249 src#bool "trim margins"
4250 (fun () -> conf.trimmargins)
4251 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4253 src#bool "persistent location"
4254 (fun () -> conf.jumpback)
4255 (fun v -> conf.jumpback <- v);
4257 sep ();
4258 src#int "inter-page space"
4259 (fun () -> conf.interpagespace)
4260 (fun n ->
4261 conf.interpagespace <- n;
4262 docolumns conf.columns;
4263 let pageno, py =
4264 match state.layout with
4265 | [] -> 0, 0
4266 | l :: _ ->
4267 l.pageno, l.pagey
4269 state.maxy <- calcheight ();
4270 let y = getpagey pageno in
4271 gotoy (y + py)
4274 src#int "page bias"
4275 (fun () -> conf.pagebias)
4276 (fun v -> conf.pagebias <- v);
4278 src#int "scroll step"
4279 (fun () -> conf.scrollstep)
4280 (fun n -> conf.scrollstep <- n);
4282 src#int "horizontal scroll step"
4283 (fun () -> conf.hscrollstep)
4284 (fun v -> conf.hscrollstep <- v);
4286 src#int "auto scroll step"
4287 (fun () ->
4288 match state.autoscroll with
4289 | Some step -> step
4290 | _ -> conf.autoscrollstep)
4291 (fun n ->
4292 if state.autoscroll <> None
4293 then state.autoscroll <- Some n;
4294 conf.autoscrollstep <- n);
4296 src#int "zoom"
4297 (fun () -> truncate (conf.zoom *. 100.))
4298 (fun v -> setzoom ((float v) /. 100.));
4300 src#int "rotation"
4301 (fun () -> conf.angle)
4302 (fun v -> reqlayout v conf.proportional);
4304 src#int "scroll bar width"
4305 (fun () -> state.scrollw)
4306 (fun v ->
4307 state.scrollw <- v;
4308 conf.scrollbw <- v;
4309 reshape conf.winw conf.winh;
4312 src#int "scroll handle height"
4313 (fun () -> conf.scrollh)
4314 (fun v -> conf.scrollh <- v;);
4316 src#int "thumbnail width"
4317 (fun () -> conf.thumbw)
4318 (fun v ->
4319 conf.thumbw <- min 4096 v;
4320 match oldmode with
4321 | Birdseye beye ->
4322 leavebirdseye beye false;
4323 enterbirdseye ()
4324 | _ -> ()
4327 let mode = state.mode in
4328 src#string "columns"
4329 (fun () ->
4330 match conf.columns with
4331 | Csingle _ -> "1"
4332 | Cmulti (multi, _) -> multicolumns_to_string multi
4333 | Csplit (count, _) -> "-" ^ string_of_int count
4335 (fun v ->
4336 let n, a, b = multicolumns_of_string v in
4337 setcolumns mode n a b);
4339 sep ();
4340 src#caption "Presentation mode" 0;
4341 src#bool "scrollbar visible"
4342 (fun () -> conf.scrollbarinpm)
4343 (fun v ->
4344 if v != conf.scrollbarinpm
4345 then (
4346 conf.scrollbarinpm <- v;
4347 if conf.presentation
4348 then (
4349 state.scrollw <- if v then conf.scrollbw else 0;
4350 reshape conf.winw conf.winh;
4355 sep ();
4356 src#caption "Pixmap cache" 0;
4357 src#int_with_suffix "size (advisory)"
4358 (fun () -> conf.memlimit)
4359 (fun v -> conf.memlimit <- v);
4361 src#caption2 "used"
4362 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4363 (string_with_suffix_of_int state.memused)
4364 (Hashtbl.length state.tilemap)) 1;
4366 sep ();
4367 src#caption "Layout" 0;
4368 src#caption2 "Dimension"
4369 (fun () ->
4370 Printf.sprintf "%dx%d (virtual %dx%d)"
4371 conf.winw conf.winh
4372 state.w state.maxy)
4374 if conf.debug
4375 then
4376 src#caption2 "Position" (fun () ->
4377 Printf.sprintf "%dx%d" state.x state.y
4379 else
4380 src#caption2 "Visible" (fun () -> describe_location ()) 1
4383 sep ();
4384 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4385 "Save these parameters as global defaults at exit"
4386 (fun () -> conf.bedefault)
4387 (fun v -> conf.bedefault <- v)
4390 sep ();
4391 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4392 src#bool ~offset:0 ~btos "Extended parameters"
4393 (fun () -> !showextended)
4394 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4395 if !showextended
4396 then (
4397 src#bool "checkers"
4398 (fun () -> conf.checkers)
4399 (fun v -> conf.checkers <- v; setcheckers v);
4400 src#bool "update cursor"
4401 (fun () -> conf.updatecurs)
4402 (fun v -> conf.updatecurs <- v);
4403 src#bool "verbose"
4404 (fun () -> conf.verbose)
4405 (fun v -> conf.verbose <- v);
4406 src#bool "invert colors"
4407 (fun () -> conf.invert)
4408 (fun v -> conf.invert <- v);
4409 src#bool "max fit"
4410 (fun () -> conf.maxhfit)
4411 (fun v -> conf.maxhfit <- v);
4412 src#bool "redirect stderr"
4413 (fun () -> conf.redirectstderr)
4414 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4415 src#string "uri launcher"
4416 (fun () -> conf.urilauncher)
4417 (fun v -> conf.urilauncher <- v);
4418 src#string "path launcher"
4419 (fun () -> conf.pathlauncher)
4420 (fun v -> conf.pathlauncher <- v);
4421 src#string "tile size"
4422 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4423 (fun v ->
4425 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4426 conf.tilew <- max 64 w;
4427 conf.tileh <- max 64 h;
4428 flushtiles ();
4429 with exn ->
4430 state.text <- Printf.sprintf "bad tile size `%s': %s"
4431 v (Printexc.to_string exn));
4432 src#int "texture count"
4433 (fun () -> conf.texcount)
4434 (fun v ->
4435 if realloctexts v
4436 then conf.texcount <- v
4437 else showtext '!' " Failed to set texture count please retry later"
4439 src#int "slice height"
4440 (fun () -> conf.sliceheight)
4441 (fun v ->
4442 conf.sliceheight <- v;
4443 wcmd "sliceh %d" conf.sliceheight;
4445 src#int "anti-aliasing level"
4446 (fun () -> conf.aalevel)
4447 (fun v ->
4448 conf.aalevel <- bound v 0 8;
4449 state.anchor <- getanchor ();
4450 opendoc state.path state.password;
4452 src#string "page scroll scaling factor"
4453 (fun () -> string_of_float conf.pgscale)
4454 (fun v ->
4456 let s = float_of_string v in
4457 conf.pgscale <- s
4458 with exn ->
4459 state.text <- Printf.sprintf
4460 "bad page scroll scaling factor `%s': %s"
4461 v (Printexc.to_string exn)
4464 src#int "ui font size"
4465 (fun () -> fstate.fontsize)
4466 (fun v -> setfontsize (bound v 5 100));
4467 src#int "hint font size"
4468 (fun () -> conf.hfsize)
4469 (fun v -> conf.hfsize <- bound v 5 100);
4470 colorp "background color"
4471 (fun () -> conf.bgcolor)
4472 (fun v -> conf.bgcolor <- v);
4473 src#bool "crop hack"
4474 (fun () -> conf.crophack)
4475 (fun v -> conf.crophack <- v);
4476 src#string "trim fuzz"
4477 (fun () -> irect_to_string conf.trimfuzz)
4478 (fun v ->
4480 conf.trimfuzz <- irect_of_string v;
4481 if conf.trimmargins
4482 then settrim true conf.trimfuzz;
4483 with exn ->
4484 state.text <- Printf.sprintf "bad irect `%s': %s"
4485 v (Printexc.to_string exn)
4487 src#string "throttle"
4488 (fun () ->
4489 match conf.maxwait with
4490 | None -> "show place holder if page is not ready"
4491 | Some time ->
4492 if time = infinity
4493 then "wait for page to fully render"
4494 else
4495 "wait " ^ string_of_float time
4496 ^ " seconds before showing placeholder"
4498 (fun v ->
4500 let f = float_of_string v in
4501 if f <= 0.0
4502 then conf.maxwait <- None
4503 else conf.maxwait <- Some f
4504 with exn ->
4505 state.text <- Printf.sprintf "bad time `%s': %s"
4506 v (Printexc.to_string exn)
4508 src#string "ghyll scroll"
4509 (fun () ->
4510 match conf.ghyllscroll with
4511 | None -> ""
4512 | Some nab -> ghyllscroll_to_string nab
4514 (fun v ->
4516 let gs =
4517 if String.length v = 0
4518 then None
4519 else Some (ghyllscroll_of_string v)
4521 conf.ghyllscroll <- gs
4522 with exn ->
4523 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4524 v (Printexc.to_string exn)
4526 src#string "selection command"
4527 (fun () -> conf.selcmd)
4528 (fun v -> conf.selcmd <- v);
4529 src#colorspace "color space"
4530 (fun () -> colorspace_to_string conf.colorspace)
4531 (fun v ->
4532 conf.colorspace <- colorspace_of_int v;
4533 wcmd "cs %d" v;
4534 load state.layout;
4536 if pbousable ()
4537 then
4538 src#bool "use PBO"
4539 (fun () -> conf.usepbo)
4540 (fun v -> conf.usepbo <- v);
4541 src#bool "mouse wheel scrolls pages"
4542 (fun () -> conf.wheelbypage)
4543 (fun v -> conf.wheelbypage <- v);
4546 sep ();
4547 src#caption "Document" 0;
4548 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4549 src#caption2 "Pages"
4550 (fun () -> string_of_int state.pagecount) 1;
4551 src#caption2 "Dimensions"
4552 (fun () -> string_of_int (List.length state.pdims)) 1;
4553 if conf.trimmargins
4554 then (
4555 sep ();
4556 src#caption "Trimmed margins" 0;
4557 src#caption2 "Dimensions"
4558 (fun () -> string_of_int (List.length state.pdims)) 1;
4561 sep ();
4562 src#caption "OpenGL" 0;
4563 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4564 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4565 src#reset prevmode prevuioh;
4567 fun () ->
4568 state.text <- "";
4569 let prevmode = state.mode
4570 and prevuioh = state.uioh in
4571 fillsrc prevmode prevuioh;
4572 let source = (src :> lvsource) in
4573 let modehash = findkeyhash conf "info" in
4574 state.uioh <- coe (object (self)
4575 inherit listview ~source ~trusted:true ~modehash as super
4576 val mutable m_prevmemused = 0
4577 method infochanged = function
4578 | Memused ->
4579 if m_prevmemused != state.memused
4580 then (
4581 m_prevmemused <- state.memused;
4582 G.postRedisplay "memusedchanged";
4584 | Pdim -> G.postRedisplay "pdimchanged"
4585 | Docinfo -> fillsrc prevmode prevuioh
4587 method key key mask =
4588 if not (Wsi.withctrl mask)
4589 then
4590 match key with
4591 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4592 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4593 | _ -> super#key key mask
4594 else super#key key mask
4595 end);
4596 G.postRedisplay "info";
4599 let enterhelpmode =
4600 let source =
4601 (object
4602 inherit lvsourcebase
4603 method getitemcount = Array.length state.help
4604 method getitem n =
4605 let s, l, _ = state.help.(n) in
4606 (s, l)
4608 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4609 let optuioh =
4610 if not cancel
4611 then (
4612 m_qsearch <- qsearch;
4613 match state.help.(active) with
4614 | _, _, Action f -> Some (f uioh)
4615 | _ -> Some (uioh)
4617 else None
4619 m_active <- active;
4620 m_first <- first;
4621 m_pan <- pan;
4622 optuioh
4624 method hasaction n =
4625 match state.help.(n) with
4626 | _, _, Action _ -> true
4627 | _ -> false
4629 initializer
4630 m_active <- -1
4631 end)
4632 in fun () ->
4633 let modehash = findkeyhash conf "help" in
4634 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4635 G.postRedisplay "help";
4638 let entermsgsmode =
4639 let msgsource =
4640 let re = Str.regexp "[\r\n]" in
4641 (object
4642 inherit lvsourcebase
4643 val mutable m_items = [||]
4645 method getitemcount = 1 + Array.length m_items
4647 method getitem n =
4648 if n = 0
4649 then "[Clear]", 0
4650 else m_items.(n-1), 0
4652 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4653 ignore uioh;
4654 if not cancel
4655 then (
4656 if active = 0
4657 then Buffer.clear state.errmsgs;
4658 m_qsearch <- qsearch;
4660 m_active <- active;
4661 m_first <- first;
4662 m_pan <- pan;
4663 None
4665 method hasaction n =
4666 n = 0
4668 method reset =
4669 state.newerrmsgs <- false;
4670 let l = Str.split re (Buffer.contents state.errmsgs) in
4671 m_items <- Array.of_list l
4673 initializer
4674 m_active <- 0
4675 end)
4676 in fun () ->
4677 state.text <- "";
4678 msgsource#reset;
4679 let source = (msgsource :> lvsource) in
4680 let modehash = findkeyhash conf "listview" in
4681 state.uioh <- coe (object
4682 inherit listview ~source ~trusted:false ~modehash as super
4683 method display =
4684 if state.newerrmsgs
4685 then msgsource#reset;
4686 super#display
4687 end);
4688 G.postRedisplay "msgs";
4691 let quickbookmark ?title () =
4692 match state.layout with
4693 | [] -> ()
4694 | l :: _ ->
4695 let title =
4696 match title with
4697 | None ->
4698 let sec = Unix.gettimeofday () in
4699 let tm = Unix.localtime sec in
4700 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4701 (l.pageno+1)
4702 tm.Unix.tm_mday
4703 tm.Unix.tm_mon
4704 (tm.Unix.tm_year + 1900)
4705 tm.Unix.tm_hour
4706 tm.Unix.tm_min
4707 | Some title -> title
4709 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4712 let doreshape w h =
4713 state.fullscreen <- None;
4714 Wsi.reshape w h;
4717 let setautoscrollspeed step goingdown =
4718 let incr = max 1 ((abs step) / 2) in
4719 let incr = if goingdown then incr else -incr in
4720 let astep = step + incr in
4721 state.autoscroll <- Some astep;
4724 let gotounder = function
4725 | Ulinkgoto (pageno, top) ->
4726 if pageno >= 0
4727 then (
4728 addnav ();
4729 gotopage1 pageno top;
4732 | Ulinkuri s ->
4733 gotouri s
4735 | Uremote (filename, pageno) ->
4736 let path =
4737 if Sys.file_exists filename
4738 then filename
4739 else
4740 let dir = Filename.dirname state.path in
4741 let path = Filename.concat dir filename in
4742 if Sys.file_exists path
4743 then path
4744 else ""
4746 if String.length path > 0
4747 then (
4748 let anchor = getanchor () in
4749 let ranchor = state.path, state.password, anchor in
4750 state.anchor <- (pageno, 0.0, 0.0);
4751 state.ranchors <- ranchor :: state.ranchors;
4752 opendoc path "";
4754 else showtext '!' ("Could not find " ^ filename)
4756 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4759 let canpan () =
4760 match conf.columns with
4761 | Csplit _ -> true
4762 | _ -> conf.zoom > 1.0
4765 let existsinrow pageno (columns, coverA, coverB) p =
4766 let last = ((pageno - coverA) mod columns) + columns in
4767 let rec any = function
4768 | [] -> false
4769 | l :: rest ->
4770 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4771 then p l
4772 else (
4773 if not (p l)
4774 then (if l.pageno = last then false else any rest)
4775 else true
4778 any state.layout
4781 let nextpage () =
4782 match state.layout with
4783 | [] -> ()
4784 | l :: rest ->
4785 match conf.columns with
4786 | Csingle _ ->
4787 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4788 then
4789 let y = clamp (pgscale conf.winh) in
4790 gotoghyll y
4791 else
4792 let pageno = min (l.pageno+1) (state.pagecount-1) in
4793 gotoghyll (getpagey pageno)
4794 | Cmulti ((c, _, _) as cl, _) ->
4795 if conf.presentation
4796 && (existsinrow l.pageno cl
4797 (fun l -> l.pageh > l.pagey + l.pagevh))
4798 then
4799 let y = clamp (pgscale conf.winh) in
4800 gotoghyll y
4801 else
4802 let pageno = min (l.pageno+c) (state.pagecount-1) in
4803 gotoghyll (getpagey pageno)
4804 | Csplit (n, _) ->
4805 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4806 then
4807 let pagey, pageh = getpageyh l.pageno in
4808 let pagey = pagey + pageh * l.pagecol in
4809 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4810 gotoghyll (pagey + pageh + ips)
4813 let prevpage () =
4814 match state.layout with
4815 | [] -> ()
4816 | l :: _ ->
4817 match conf.columns with
4818 | Csingle _ ->
4819 if conf.presentation && l.pagey != 0
4820 then
4821 gotoghyll (clamp (pgscale ~-(conf.winh)))
4822 else
4823 let pageno = max 0 (l.pageno-1) in
4824 gotoghyll (getpagey pageno)
4825 | Cmulti ((c, _, coverB) as cl, _) ->
4826 if conf.presentation &&
4827 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4828 then
4829 gotoghyll (clamp (pgscale ~-(conf.winh)))
4830 else
4831 let decr =
4832 if l.pageno = state.pagecount - coverB
4833 then 1
4834 else c
4836 let pageno = max 0 (l.pageno-decr) in
4837 gotoghyll (getpagey pageno)
4838 | Csplit (n, _) ->
4839 let y =
4840 if l.pagecol = 0
4841 then
4842 if l.pageno = 0
4843 then l.pagey
4844 else
4845 let pageno = max 0 (l.pageno-1) in
4846 let pagey, pageh = getpageyh pageno in
4847 pagey + (n-1)*pageh
4848 else
4849 let pagey, pageh = getpageyh l.pageno in
4850 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4852 gotoghyll y
4855 let viewkeyboard key mask =
4856 let enttext te =
4857 let mode = state.mode in
4858 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4859 state.text <- "";
4860 enttext ();
4861 G.postRedisplay "view:enttext"
4863 let ctrl = Wsi.withctrl mask in
4864 let key =
4865 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4867 match key with
4868 | 81 -> (* Q *)
4869 exit 0
4871 | 0xff63 -> (* insert *)
4872 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4873 then (
4874 state.mode <- LinkNav (Ltgendir 0);
4875 gotoy state.y;
4877 else showtext '!' "Keyboard link navigation does not work under rotation"
4879 | 0xff1b | 113 -> (* escape / q *)
4880 begin match state.mstate with
4881 | Mzoomrect _ ->
4882 state.mstate <- Mnone;
4883 Wsi.setcursor Wsi.CURSOR_INHERIT;
4884 G.postRedisplay "kill zoom rect";
4885 | _ ->
4886 begin match state.mode with
4887 | LinkNav _ ->
4888 state.mode <- View;
4889 G.postRedisplay "esc leave linknav"
4890 | _ ->
4891 match state.ranchors with
4892 | [] -> raise Quit
4893 | (path, password, anchor) :: rest ->
4894 state.ranchors <- rest;
4895 state.anchor <- anchor;
4896 opendoc path password
4897 end;
4898 end;
4900 | 0xff08 -> (* backspace *)
4901 gotoghyll (getnav ~-1)
4903 | 111 -> (* o *)
4904 enteroutlinemode ()
4906 | 117 -> (* u *)
4907 state.rects <- [];
4908 state.text <- "";
4909 G.postRedisplay "dehighlight";
4911 | 47 | 63 -> (* / ? *)
4912 let ondone isforw s =
4913 cbput state.hists.pat s;
4914 state.searchpattern <- s;
4915 search s isforw
4917 let s = String.create 1 in
4918 s.[0] <- Char.chr key;
4919 enttext (s, "", Some (onhist state.hists.pat),
4920 textentry, ondone (key = 47), true)
4922 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4923 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4924 setzoom (conf.zoom +. incr)
4926 | 43 | 0xffab -> (* + *)
4927 let ondone s =
4928 let n =
4929 try int_of_string s with exc ->
4930 state.text <- Printf.sprintf "bad integer `%s': %s"
4931 s (Printexc.to_string exc);
4932 max_int
4934 if n != max_int
4935 then (
4936 conf.pagebias <- n;
4937 state.text <- "page bias is now " ^ string_of_int n;
4940 enttext ("page bias: ", "", None, intentry, ondone, true)
4942 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4943 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4944 setzoom (max 0.01 (conf.zoom -. decr))
4946 | 45 | 0xffad -> (* - *)
4947 let ondone msg = state.text <- msg in
4948 enttext (
4949 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4950 optentry state.mode, ondone, true
4953 | 48 when ctrl -> (* ctrl-0 *)
4954 setzoom 1.0
4956 | 49 when ctrl -> (* ctrl-1 *)
4957 let cols =
4958 match conf.columns with
4959 | Csingle _ | Cmulti _ -> 1
4960 | Csplit (n, _) -> n
4962 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4963 if zoom < 1.0
4964 then setzoom zoom
4966 | 0xffc6 -> (* f9 *)
4967 togglebirdseye ()
4969 | 57 when ctrl -> (* ctrl-9 *)
4970 togglebirdseye ()
4972 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4973 when not ctrl -> (* 0..9 *)
4974 let ondone s =
4975 let n =
4976 try int_of_string s with exc ->
4977 state.text <- Printf.sprintf "bad integer `%s': %s"
4978 s (Printexc.to_string exc);
4981 if n >= 0
4982 then (
4983 addnav ();
4984 cbput state.hists.pag (string_of_int n);
4985 gotopage1 (n + conf.pagebias - 1) 0;
4988 let pageentry text key =
4989 match Char.unsafe_chr key with
4990 | 'g' -> TEdone text
4991 | _ -> intentry text key
4993 let text = "x" in text.[0] <- Char.chr key;
4994 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4996 | 98 -> (* b *)
4997 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4998 reshape conf.winw conf.winh;
5000 | 108 -> (* l *)
5001 conf.hlinks <- not conf.hlinks;
5002 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5003 G.postRedisplay "toggle highlightlinks";
5005 | 70 -> (* F *)
5006 state.glinks <- true;
5007 let mode = state.mode in
5008 state.mode <- Textentry (
5009 (":", "", None, linknentry, linkndone gotounder, false),
5010 (fun _ ->
5011 state.glinks <- false;
5012 state.mode <- mode)
5014 state.text <- "";
5015 G.postRedisplay "view:linkent(F)"
5017 | 121 -> (* y *)
5018 state.glinks <- true;
5019 let mode = state.mode in
5020 state.mode <- Textentry (
5021 (":", "", None, linknentry, linkndone (fun under ->
5022 match Ne.pipe () with
5023 | Ne.Exn exn ->
5024 showtext '!' (Printf.sprintf "pipe failed: %s"
5025 (Printexc.to_string exn));
5026 | Ne.Res (r, w) ->
5027 let popened =
5028 try popen conf.selcmd [r, 0; w, -1]; true
5029 with exn ->
5030 showtext '!'
5031 (Printf.sprintf "failed to execute %s: %s"
5032 conf.selcmd (Printexc.to_string exn));
5033 false
5035 let clo cap fd =
5036 Ne.clo fd (fun msg ->
5037 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5040 let s = undertext under in
5041 if popened
5042 then
5043 (try
5044 let l = String.length s in
5045 let n = tempfailureretry (Unix.write w s 0) l in
5046 if n != l
5047 then
5048 showtext '!'
5049 (Printf.sprintf
5050 "failed to write %d characters to sel pipe, wrote %d"
5053 with exn ->
5054 showtext '!'
5055 (Printf.sprintf "failed to write to sel pipe: %s"
5056 (Printexc.to_string exn)
5059 else dolog "%s" s;
5060 clo "pipe/r" r;
5061 clo "pipe/w" w;
5062 ), false
5064 fun _ ->
5065 state.glinks <- false;
5066 state.mode <- mode
5068 state.text <- "";
5069 G.postRedisplay "view:linkent"
5071 | 97 -> (* a *)
5072 begin match state.autoscroll with
5073 | Some step ->
5074 conf.autoscrollstep <- step;
5075 state.autoscroll <- None
5076 | None ->
5077 if conf.autoscrollstep = 0
5078 then state.autoscroll <- Some 1
5079 else state.autoscroll <- Some conf.autoscrollstep
5082 | 112 when ctrl -> (* ctrl-p *)
5083 launchpath ()
5085 | 80 -> (* P *)
5086 setpresentationmode (not conf.presentation);
5087 showtext ' ' ("presentation mode " ^
5088 if conf.presentation then "on" else "off");
5090 | 102 -> (* f *)
5091 begin match state.fullscreen with
5092 | None ->
5093 state.fullscreen <- Some (conf.winw, conf.winh);
5094 Wsi.fullscreen ()
5095 | Some (w, h) ->
5096 state.fullscreen <- None;
5097 doreshape w h
5100 | 112 | 78 -> (* p|N *)
5101 search state.searchpattern false
5103 | 110 | 0xffc0 -> (* n|F3 *)
5104 search state.searchpattern true
5106 | 116 -> (* t *)
5107 begin match state.layout with
5108 | [] -> ()
5109 | l :: _ ->
5110 gotoy_and_clear_text (getpagey l.pageno)
5113 | 32 -> (* space *)
5114 nextpage ()
5116 | 0xff9f | 0xffff -> (* delete *)
5117 prevpage ()
5119 | 61 -> (* = *)
5120 showtext ' ' (describe_location ());
5122 | 119 -> (* w *)
5123 begin match state.layout with
5124 | [] -> ()
5125 | l :: _ ->
5126 doreshape (l.pagew + state.scrollw) l.pageh;
5127 G.postRedisplay "w"
5130 | 39 -> (* ' *)
5131 enterbookmarkmode ()
5133 | 104 | 0xffbe -> (* h|F1 *)
5134 enterhelpmode ()
5136 | 105 -> (* i *)
5137 enterinfomode ()
5139 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5140 entermsgsmode ()
5142 | 109 -> (* m *)
5143 let ondone s =
5144 match state.layout with
5145 | l :: _ ->
5146 if String.length s > 0
5147 then
5148 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5149 | _ -> ()
5151 enttext ("bookmark: ", "", None, textentry, ondone, true)
5153 | 126 -> (* ~ *)
5154 quickbookmark ();
5155 showtext ' ' "Quick bookmark added";
5157 | 122 -> (* z *)
5158 begin match state.layout with
5159 | l :: _ ->
5160 let rect = getpdimrect l.pagedimno in
5161 let w, h =
5162 if conf.crophack
5163 then
5164 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5165 truncate (1.2 *. (rect.(3) -. rect.(0))))
5166 else
5167 (truncate (rect.(1) -. rect.(0)),
5168 truncate (rect.(3) -. rect.(0)))
5170 let w = truncate ((float w)*.conf.zoom)
5171 and h = truncate ((float h)*.conf.zoom) in
5172 if w != 0 && h != 0
5173 then (
5174 state.anchor <- getanchor ();
5175 doreshape (w + state.scrollw) (h + conf.interpagespace)
5177 G.postRedisplay "z";
5179 | [] -> ()
5182 | 50 when ctrl -> (* ctrl-2 *)
5183 let maxw = getmaxw () in
5184 if maxw > 0.0
5185 then setzoom (maxw /. float conf.winw)
5187 | 60 | 62 -> (* < > *)
5188 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5190 | 91 | 93 -> (* [ ] *)
5191 conf.colorscale <-
5192 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5194 G.postRedisplay "brightness";
5196 | 99 when state.mode = View -> (* c *)
5197 let (c, a, b), z =
5198 match state.prevcolumns with
5199 | None -> (1, 0, 0), 1.0
5200 | Some (columns, z) ->
5201 let cab =
5202 match columns with
5203 | Csplit (c, _) -> -c, 0, 0
5204 | Cmulti ((c, a, b), _) -> c, a, b
5205 | Csingle _ -> 1, 0, 0
5207 cab, z
5209 setcolumns View c a b;
5210 setzoom z;
5212 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5213 setzoom state.prevzoom
5215 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5216 begin match state.autoscroll with
5217 | None ->
5218 begin match state.mode with
5219 | Birdseye beye -> upbirdseye 1 beye
5220 | _ ->
5221 if ctrl
5222 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5223 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5225 | Some n ->
5226 setautoscrollspeed n false
5229 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5230 begin match state.autoscroll with
5231 | None ->
5232 begin match state.mode with
5233 | Birdseye beye -> downbirdseye 1 beye
5234 | _ ->
5235 if ctrl
5236 then gotoy_and_clear_text (clamp (conf.winh/2))
5237 else gotoy_and_clear_text (clamp conf.scrollstep)
5239 | Some n ->
5240 setautoscrollspeed n true
5243 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5244 if canpan ()
5245 then
5246 let dx =
5247 if ctrl
5248 then conf.winw / 2
5249 else conf.hscrollstep
5251 let dx = if key = 0xff51 then dx else -dx in
5252 state.x <- state.x + dx;
5253 gotoy_and_clear_text state.y
5254 else (
5255 state.text <- "";
5256 G.postRedisplay "lef/right"
5259 | 0xff55 | 0xff9a -> (* (kp) prior *)
5260 let y =
5261 if ctrl
5262 then
5263 match state.layout with
5264 | [] -> state.y
5265 | l :: _ -> state.y - l.pagey
5266 else
5267 clamp (pgscale (-conf.winh))
5269 gotoghyll y
5271 | 0xff56 | 0xff9b -> (* (kp) next *)
5272 let y =
5273 if ctrl
5274 then
5275 match List.rev state.layout with
5276 | [] -> state.y
5277 | l :: _ -> getpagey l.pageno
5278 else
5279 clamp (pgscale conf.winh)
5281 gotoghyll y
5283 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5284 gotoghyll 0
5285 | 71 | 0xff57 | 0xff9c -> (* G end *)
5286 gotoghyll (clamp state.maxy)
5288 | 0xff53 when Wsi.withalt mask -> (* alt-right *)
5289 gotoghyll (getnav 1)
5290 | 0xff51 when Wsi.withalt mask -> (* alt-left *)
5291 gotoghyll (getnav ~-1)
5293 | 114 -> (* r *)
5294 reload ()
5296 | 118 when conf.debug -> (* v *)
5297 state.rects <- [];
5298 List.iter (fun l ->
5299 match getopaque l.pageno with
5300 | None -> ()
5301 | Some opaque ->
5302 let x0, y0, x1, y1 = pagebbox opaque in
5303 let a,b = float x0, float y0 in
5304 let c,d = float x1, float y0 in
5305 let e,f = float x1, float y1 in
5306 let h,j = float x0, float y1 in
5307 let rect = (a,b,c,d,e,f,h,j) in
5308 debugrect rect;
5309 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5310 ) state.layout;
5311 G.postRedisplay "v";
5313 | _ ->
5314 vlog "huh? %s" (Wsi.keyname key)
5317 let linknavkeyboard key mask linknav =
5318 let getpage pageno =
5319 let rec loop = function
5320 | [] -> None
5321 | l :: _ when l.pageno = pageno -> Some l
5322 | _ :: rest -> loop rest
5323 in loop state.layout
5325 let doexact (pageno, n) =
5326 match getopaque pageno, getpage pageno with
5327 | Some opaque, Some l ->
5328 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5329 then
5330 let under = getlink opaque n in
5331 G.postRedisplay "link gotounder";
5332 gotounder under;
5333 state.mode <- View;
5334 else
5335 let opt, dir =
5336 match key with
5337 | 0xff50 -> (* home *)
5338 Some (findlink opaque LDfirst), -1
5340 | 0xff57 -> (* end *)
5341 Some (findlink opaque LDlast), 1
5343 | 0xff51 -> (* left *)
5344 Some (findlink opaque (LDleft n)), -1
5346 | 0xff53 -> (* right *)
5347 Some (findlink opaque (LDright n)), 1
5349 | 0xff52 -> (* up *)
5350 Some (findlink opaque (LDup n)), -1
5352 | 0xff54 -> (* down *)
5353 Some (findlink opaque (LDdown n)), 1
5355 | _ -> None, 0
5357 let pwl l dir =
5358 begin match findpwl l.pageno dir with
5359 | Pwlnotfound -> ()
5360 | Pwl pageno ->
5361 let notfound dir =
5362 state.mode <- LinkNav (Ltgendir dir);
5363 let y, h = getpageyh pageno in
5364 let y =
5365 if dir < 0
5366 then y + h - conf.winh
5367 else y
5369 gotoy y
5371 begin match getopaque pageno, getpage pageno with
5372 | Some opaque, Some _ ->
5373 let link =
5374 let ld = if dir > 0 then LDfirst else LDlast in
5375 findlink opaque ld
5377 begin match link with
5378 | Lfound m ->
5379 showlinktype (getlink opaque m);
5380 state.mode <- LinkNav (Ltexact (pageno, m));
5381 G.postRedisplay "linknav jpage";
5382 | _ -> notfound dir
5383 end;
5384 | _ -> notfound dir
5385 end;
5386 end;
5388 begin match opt with
5389 | Some Lnotfound -> pwl l dir;
5390 | Some (Lfound m) ->
5391 if m = n
5392 then pwl l dir
5393 else (
5394 let _, y0, _, y1 = getlinkrect opaque m in
5395 if y0 < l.pagey
5396 then gotopage1 l.pageno y0
5397 else (
5398 let d = fstate.fontsize + 1 in
5399 if y1 - l.pagey > l.pagevh - d
5400 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5401 else G.postRedisplay "linknav";
5403 showlinktype (getlink opaque m);
5404 state.mode <- LinkNav (Ltexact (l.pageno, m));
5407 | None -> viewkeyboard key mask
5408 end;
5409 | _ -> viewkeyboard key mask
5411 if key = 0xff63
5412 then (
5413 state.mode <- View;
5414 G.postRedisplay "leave linknav"
5416 else
5417 match linknav with
5418 | Ltgendir _ -> viewkeyboard key mask
5419 | Ltexact exact -> doexact exact
5422 let keyboard key mask =
5423 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5424 then wcmd "interrupt"
5425 else state.uioh <- state.uioh#key key mask
5428 let birdseyekeyboard key mask
5429 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5430 let incr =
5431 match conf.columns with
5432 | Csingle _ -> 1
5433 | Cmulti ((c, _, _), _) -> c
5434 | Csplit _ -> failwith "bird's eye split mode"
5436 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5437 match key with
5438 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5439 let y, h = getpageyh pageno in
5440 let top = (conf.winh - h) / 2 in
5441 gotoy (max 0 (y - top))
5442 | 0xff0d (* enter *)
5443 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5444 | 0xff1b -> leavebirdseye beye true (* escape *)
5445 | 0xff52 -> upbirdseye incr beye (* up *)
5446 | 0xff54 -> downbirdseye incr beye (* down *)
5447 | 0xff51 -> upbirdseye 1 beye (* left *)
5448 | 0xff53 -> downbirdseye 1 beye (* right *)
5450 | 0xff55 -> (* prior *)
5451 begin match state.layout with
5452 | l :: _ ->
5453 if l.pagey != 0
5454 then (
5455 state.mode <- Birdseye (
5456 oconf, leftx, l.pageno, hooverpageno, anchor
5458 gotopage1 l.pageno 0;
5460 else (
5461 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5462 match layout with
5463 | [] -> gotoy (clamp (-conf.winh))
5464 | l :: _ ->
5465 state.mode <- Birdseye (
5466 oconf, leftx, l.pageno, hooverpageno, anchor
5468 gotopage1 l.pageno 0
5471 | [] -> gotoy (clamp (-conf.winh))
5472 end;
5474 | 0xff56 -> (* next *)
5475 begin match List.rev state.layout with
5476 | l :: _ ->
5477 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5478 begin match layout with
5479 | [] ->
5480 let incr = l.pageh - l.pagevh in
5481 if incr = 0
5482 then (
5483 state.mode <-
5484 Birdseye (
5485 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5487 G.postRedisplay "birdseye pagedown";
5489 else gotoy (clamp (incr + conf.interpagespace*2));
5491 | l :: _ ->
5492 state.mode <-
5493 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5494 gotopage1 l.pageno 0;
5497 | [] -> gotoy (clamp conf.winh)
5498 end;
5500 | 0xff50 -> (* home *)
5501 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5502 gotopage1 0 0
5504 | 0xff57 -> (* end *)
5505 let pageno = state.pagecount - 1 in
5506 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5507 if not (pagevisible state.layout pageno)
5508 then
5509 let h =
5510 match List.rev state.pdims with
5511 | [] -> conf.winh
5512 | (_, _, h, _) :: _ -> h
5514 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5515 else G.postRedisplay "birdseye end";
5516 | _ -> viewkeyboard key mask
5519 let drawpage l linkindexbase =
5520 let color =
5521 match state.mode with
5522 | Textentry _ -> scalecolor 0.4
5523 | LinkNav _
5524 | View -> scalecolor 1.0
5525 | Birdseye (_, _, pageno, hooverpageno, _) ->
5526 if l.pageno = hooverpageno
5527 then scalecolor 0.9
5528 else (
5529 if l.pageno = pageno
5530 then scalecolor 1.0
5531 else scalecolor 0.8
5534 drawtiles l color;
5535 begin match getopaque l.pageno with
5536 | Some opaque ->
5537 if tileready l l.pagex l.pagey
5538 then
5539 let x = l.pagedispx - l.pagex
5540 and y = l.pagedispy - l.pagey in
5541 let hlmask =
5542 match conf.columns with
5543 | Csingle _ | Cmulti _ ->
5544 (if conf.hlinks then 1 else 0)
5545 + (if state.glinks
5546 && not (isbirdseye state.mode) then 2 else 0)
5547 | _ -> 0
5549 let s =
5550 match state.mode with
5551 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5552 | _ -> ""
5554 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5555 else 0
5557 | _ -> 0
5558 end;
5561 let scrollindicator () =
5562 let sbw, ph, sh = state.uioh#scrollph in
5563 let sbh, pw, sw = state.uioh#scrollpw in
5565 GlDraw.color (0.64, 0.64, 0.64);
5566 GlDraw.rect
5567 (float (conf.winw - sbw), 0.)
5568 (float conf.winw, float conf.winh)
5570 GlDraw.rect
5571 (0., float (conf.winh - sbh))
5572 (float (conf.winw - state.scrollw - 1), float conf.winh)
5574 GlDraw.color (0.0, 0.0, 0.0);
5576 GlDraw.rect
5577 (float (conf.winw - sbw), ph)
5578 (float conf.winw, ph +. sh)
5580 GlDraw.rect
5581 (pw, float (conf.winh - sbh))
5582 (pw +. sw, float conf.winh)
5586 let showsel () =
5587 match state.mstate with
5588 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5591 | Msel ((x0, y0), (x1, y1)) ->
5592 let rec loop = function
5593 | l :: ls ->
5594 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5595 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5596 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5597 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5598 then
5599 match getopaque l.pageno with
5600 | Some opaque ->
5601 let x0, y0 = pagetranslatepoint l x0 y0 in
5602 let x1, y1 = pagetranslatepoint l x1 y1 in
5603 seltext opaque (x0, y0, x1, y1);
5604 | _ -> ()
5605 else loop ls
5606 | [] -> ()
5608 loop state.layout
5611 let showrects rects =
5612 Gl.enable `blend;
5613 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5614 GlDraw.polygon_mode `both `fill;
5615 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5616 List.iter
5617 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5618 List.iter (fun l ->
5619 if l.pageno = pageno
5620 then (
5621 let dx = float (l.pagedispx - l.pagex) in
5622 let dy = float (l.pagedispy - l.pagey) in
5623 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5624 GlDraw.begins `quads;
5626 GlDraw.vertex2 (x0+.dx, y0+.dy);
5627 GlDraw.vertex2 (x1+.dx, y1+.dy);
5628 GlDraw.vertex2 (x2+.dx, y2+.dy);
5629 GlDraw.vertex2 (x3+.dx, y3+.dy);
5631 GlDraw.ends ();
5633 ) state.layout
5634 ) rects
5636 Gl.disable `blend;
5639 let display () =
5640 GlClear.color (scalecolor2 conf.bgcolor);
5641 GlClear.clear [`color];
5642 let rec loop linkindexbase = function
5643 | l :: rest ->
5644 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5645 loop linkindexbase rest
5646 | [] -> ()
5648 loop 0 state.layout;
5649 let rects =
5650 match state.mode with
5651 | LinkNav (Ltexact (pageno, linkno)) ->
5652 begin match getopaque pageno with
5653 | Some opaque ->
5654 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5655 (pageno, 5, (
5656 float x0, float y0,
5657 float x1, float y0,
5658 float x1, float y1,
5659 float x0, float y1)
5660 ) :: state.rects
5661 | None -> state.rects
5663 | _ -> state.rects
5665 showrects rects;
5666 showsel ();
5667 state.uioh#display;
5668 begin match state.mstate with
5669 | Mzoomrect ((x0, y0), (x1, y1)) ->
5670 Gl.enable `blend;
5671 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5672 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5673 GlDraw.rect (float x0, float y0)
5674 (float x1, float y1);
5675 Gl.disable `blend;
5676 | _ -> ()
5677 end;
5678 enttext ();
5679 scrollindicator ();
5680 if not state.wthack then Wsi.swapb ();
5683 let zoomrect x y x1 y1 =
5684 let x0 = min x x1
5685 and x1 = max x x1
5686 and y0 = min y y1 in
5687 gotoy (state.y + y0);
5688 state.anchor <- getanchor ();
5689 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5690 let margin =
5691 if state.w < conf.winw - state.scrollw
5692 then (conf.winw - state.scrollw - state.w) / 2
5693 else 0
5695 state.x <- (state.x + margin) - x0;
5696 setzoom zoom;
5697 Wsi.setcursor Wsi.CURSOR_INHERIT;
5698 state.mstate <- Mnone;
5701 let scrollx x =
5702 let winw = conf.winw - state.scrollw - 1 in
5703 let s = float x /. float winw in
5704 let destx = truncate (float (state.w + winw) *. s) in
5705 state.x <- winw - destx;
5706 gotoy_and_clear_text state.y;
5707 state.mstate <- Mscrollx;
5710 let scrolly y =
5711 let s = float y /. float conf.winh in
5712 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5713 gotoy_and_clear_text desty;
5714 state.mstate <- Mscrolly;
5717 let viewmouse button down x y mask =
5718 match button with
5719 | n when (n == 4 || n == 5) && not down ->
5720 if Wsi.withctrl mask
5721 then (
5722 match state.mstate with
5723 | Mzoom (oldn, i) ->
5724 if oldn = n
5725 then (
5726 if i = 2
5727 then
5728 let incr =
5729 match n with
5730 | 5 ->
5731 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5732 | _ ->
5733 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5735 let zoom = conf.zoom -. incr in
5736 setzoom zoom;
5737 state.mstate <- Mzoom (n, 0);
5738 else
5739 state.mstate <- Mzoom (n, i+1);
5741 else state.mstate <- Mzoom (n, 0)
5743 | _ -> state.mstate <- Mzoom (n, 0)
5745 else (
5746 match state.autoscroll with
5747 | Some step -> setautoscrollspeed step (n=4)
5748 | None ->
5749 if conf.wheelbypage
5750 then (
5751 if n = 4
5752 then nextpage ()
5753 else prevpage ()
5755 else
5756 let incr =
5757 if n = 4
5758 then -conf.scrollstep
5759 else conf.scrollstep
5761 let incr = incr * 2 in
5762 let y = clamp incr in
5763 gotoy_and_clear_text y
5766 | n when (n = 6 || n = 7) && not down && canpan () ->
5767 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5768 gotoy_and_clear_text state.y
5770 | 1 when Wsi.withctrl mask ->
5771 if down
5772 then (
5773 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5774 state.mstate <- Mpan (x, y)
5776 else
5777 state.mstate <- Mnone
5779 | 3 ->
5780 if down
5781 then (
5782 Wsi.setcursor Wsi.CURSOR_CYCLE;
5783 let p = (x, y) in
5784 state.mstate <- Mzoomrect (p, p)
5786 else (
5787 match state.mstate with
5788 | Mzoomrect ((x0, y0), _) ->
5789 if abs (x-x0) > 10 && abs (y - y0) > 10
5790 then zoomrect x0 y0 x y
5791 else (
5792 state.mstate <- Mnone;
5793 Wsi.setcursor Wsi.CURSOR_INHERIT;
5794 G.postRedisplay "kill accidental zoom rect";
5796 | _ ->
5797 Wsi.setcursor Wsi.CURSOR_INHERIT;
5798 state.mstate <- Mnone
5801 | 1 when x > conf.winw - state.scrollw ->
5802 if down
5803 then
5804 let _, position, sh = state.uioh#scrollph in
5805 if y > truncate position && y < truncate (position +. sh)
5806 then state.mstate <- Mscrolly
5807 else scrolly y
5808 else
5809 state.mstate <- Mnone
5811 | 1 when y > conf.winh - state.hscrollh ->
5812 if down
5813 then
5814 let _, position, sw = state.uioh#scrollpw in
5815 if x > truncate position && x < truncate (position +. sw)
5816 then state.mstate <- Mscrollx
5817 else scrollx x
5818 else
5819 state.mstate <- Mnone
5821 | 1 ->
5822 let dest = if down then getunder x y else Unone in
5823 begin match dest with
5824 | Ulinkgoto _
5825 | Ulinkuri _
5826 | Uremote _
5827 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5828 gotounder dest
5830 | Unone when down ->
5831 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5832 state.mstate <- Mpan (x, y);
5834 | Unone | Utext _ ->
5835 if down
5836 then (
5837 if conf.angle mod 360 = 0
5838 then (
5839 state.mstate <- Msel ((x, y), (x, y));
5840 G.postRedisplay "mouse select";
5843 else (
5844 match state.mstate with
5845 | Mnone -> ()
5847 | Mzoom _ | Mscrollx | Mscrolly ->
5848 state.mstate <- Mnone
5850 | Mzoomrect ((x0, y0), _) ->
5851 zoomrect x0 y0 x y
5853 | Mpan _ ->
5854 Wsi.setcursor Wsi.CURSOR_INHERIT;
5855 state.mstate <- Mnone
5857 | Msel ((x0, y0), (x1, y1)) ->
5858 let rec loop = function
5859 | [] -> ()
5860 | l :: rest ->
5861 let inside =
5862 let a0 = l.pagedispy in
5863 let a1 = a0 + l.pagevh in
5864 let b0 = l.pagedispx in
5865 let b1 = b0 + l.pagevw in
5866 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5867 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5869 if inside
5870 then
5871 match getopaque l.pageno with
5872 | Some opaque ->
5873 begin
5874 match Ne.pipe () with
5875 | Ne.Exn exn ->
5876 showtext '!'
5877 (Printf.sprintf
5878 "can not create sel pipe: %s"
5879 (Printexc.to_string exn));
5880 | Ne.Res (r, w) ->
5881 let doclose what fd =
5882 Ne.clo fd (fun msg ->
5883 dolog "%s close failed: %s" what msg)
5886 popen conf.selcmd [r, 0; w, -1];
5887 copysel w opaque;
5888 doclose "pipe/r" r;
5889 G.postRedisplay "copysel";
5890 with exn ->
5891 dolog "can not execute %S: %s"
5892 conf.selcmd (Printexc.to_string exn);
5893 doclose "pipe/r" r;
5894 doclose "pipe/w" w;
5896 | None -> ()
5897 else loop rest
5899 loop state.layout;
5900 Wsi.setcursor Wsi.CURSOR_INHERIT;
5901 state.mstate <- Mnone;
5905 | _ -> ()
5908 let birdseyemouse button down x y mask
5909 (conf, leftx, _, hooverpageno, anchor) =
5910 match button with
5911 | 1 when down ->
5912 let rec loop = function
5913 | [] -> ()
5914 | l :: rest ->
5915 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5916 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5917 then (
5918 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5920 else loop rest
5922 loop state.layout
5923 | 3 -> ()
5924 | _ -> viewmouse button down x y mask
5927 let mouse button down x y mask =
5928 state.uioh <- state.uioh#button button down x y mask;
5931 let motion ~x ~y =
5932 state.uioh <- state.uioh#motion x y
5935 let pmotion ~x ~y =
5936 state.uioh <- state.uioh#pmotion x y;
5939 let uioh = object
5940 method display = ()
5942 method key key mask =
5943 begin match state.mode with
5944 | Textentry textentry -> textentrykeyboard key mask textentry
5945 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5946 | View -> viewkeyboard key mask
5947 | LinkNav linknav -> linknavkeyboard key mask linknav
5948 end;
5949 state.uioh
5951 method button button bstate x y mask =
5952 begin match state.mode with
5953 | LinkNav _
5954 | View -> viewmouse button bstate x y mask
5955 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5956 | Textentry _ -> ()
5957 end;
5958 state.uioh
5960 method motion x y =
5961 begin match state.mode with
5962 | Textentry _ -> ()
5963 | View | Birdseye _ | LinkNav _ ->
5964 match state.mstate with
5965 | Mzoom _ | Mnone -> ()
5967 | Mpan (x0, y0) ->
5968 let dx = x - x0
5969 and dy = y0 - y in
5970 state.mstate <- Mpan (x, y);
5971 if canpan ()
5972 then state.x <- state.x + dx;
5973 let y = clamp dy in
5974 gotoy_and_clear_text y
5976 | Msel (a, _) ->
5977 state.mstate <- Msel (a, (x, y));
5978 G.postRedisplay "motion select";
5980 | Mscrolly ->
5981 let y = min conf.winh (max 0 y) in
5982 scrolly y
5984 | Mscrollx ->
5985 let x = min conf.winw (max 0 x) in
5986 scrollx x
5988 | Mzoomrect (p0, _) ->
5989 state.mstate <- Mzoomrect (p0, (x, y));
5990 G.postRedisplay "motion zoomrect";
5991 end;
5992 state.uioh
5994 method pmotion x y =
5995 begin match state.mode with
5996 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5997 let rec loop = function
5998 | [] ->
5999 if hooverpageno != -1
6000 then (
6001 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6002 G.postRedisplay "pmotion birdseye no hoover";
6004 | l :: rest ->
6005 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6006 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6007 then (
6008 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6009 G.postRedisplay "pmotion birdseye hoover";
6011 else loop rest
6013 loop state.layout
6015 | Textentry _ -> ()
6017 | LinkNav _
6018 | View ->
6019 match state.mstate with
6020 | Mnone -> updateunder x y
6021 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6023 end;
6024 state.uioh
6026 method infochanged _ = ()
6028 method scrollph =
6029 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
6030 let p, h = scrollph state.y maxy in
6031 state.scrollw, p, h
6033 method scrollpw =
6034 let winw = conf.winw - state.scrollw - 1 in
6035 let fwinw = float winw in
6036 let sw =
6037 let sw = fwinw /. float state.w in
6038 let sw = fwinw *. sw in
6039 max sw (float conf.scrollh)
6041 let position, sw =
6042 let f = state.w+winw in
6043 let r = float (winw-state.x) /. float f in
6044 let p = fwinw *. r in
6045 p-.sw/.2., sw
6047 let sw =
6048 if position +. sw > fwinw
6049 then fwinw -. position
6050 else sw
6052 state.hscrollh, position, sw
6054 method modehash =
6055 let modename =
6056 match state.mode with
6057 | LinkNav _ -> "links"
6058 | Textentry _ -> "textentry"
6059 | Birdseye _ -> "birdseye"
6060 | View -> "view"
6062 findkeyhash conf modename
6063 end;;
6065 module Config =
6066 struct
6067 open Parser
6069 let fontpath = ref "";;
6071 module KeyMap =
6072 Map.Make (struct type t = (int * int) let compare = compare end);;
6074 let unent s =
6075 let l = String.length s in
6076 let b = Buffer.create l in
6077 unent b s 0 l;
6078 Buffer.contents b;
6081 let home =
6082 try Sys.getenv "HOME"
6083 with exn ->
6084 prerr_endline
6085 ("Can not determine home directory location: " ^
6086 Printexc.to_string exn);
6090 let modifier_of_string = function
6091 | "alt" -> Wsi.altmask
6092 | "shift" -> Wsi.shiftmask
6093 | "ctrl" | "control" -> Wsi.ctrlmask
6094 | "meta" -> Wsi.metamask
6095 | _ -> 0
6098 let key_of_string =
6099 let r = Str.regexp "-" in
6100 fun s ->
6101 let elems = Str.full_split r s in
6102 let f n k m =
6103 let g s =
6104 let m1 = modifier_of_string s in
6105 if m1 = 0
6106 then (Wsi.namekey s, m)
6107 else (k, m lor m1)
6108 in function
6109 | Str.Delim s when n land 1 = 0 -> g s
6110 | Str.Text s -> g s
6111 | Str.Delim _ -> (k, m)
6113 let rec loop n k m = function
6114 | [] -> (k, m)
6115 | x :: xs ->
6116 let k, m = f n k m x in
6117 loop (n+1) k m xs
6119 loop 0 0 0 elems
6122 let keys_of_string =
6123 let r = Str.regexp "[ \t]" in
6124 fun s ->
6125 let elems = Str.split r s in
6126 List.map key_of_string elems
6129 let copykeyhashes c =
6130 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6133 let config_of c attrs =
6134 let apply c k v =
6136 match k with
6137 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6138 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6139 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6140 | "preload" -> { c with preload = bool_of_string v }
6141 | "page-bias" -> { c with pagebias = int_of_string v }
6142 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6143 | "horizontal-scroll-step" ->
6144 { c with hscrollstep = max (int_of_string v) 1 }
6145 | "auto-scroll-step" ->
6146 { c with autoscrollstep = max 0 (int_of_string v) }
6147 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6148 | "crop-hack" -> { c with crophack = bool_of_string v }
6149 | "throttle" ->
6150 let mw =
6151 match String.lowercase v with
6152 | "true" -> Some infinity
6153 | "false" -> None
6154 | f -> Some (float_of_string f)
6156 { c with maxwait = mw}
6157 | "highlight-links" -> { c with hlinks = bool_of_string v }
6158 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6159 | "vertical-margin" ->
6160 { c with interpagespace = max 0 (int_of_string v) }
6161 | "zoom" ->
6162 let zoom = float_of_string v /. 100. in
6163 let zoom = max zoom 0.0 in
6164 { c with zoom = zoom }
6165 | "presentation" -> { c with presentation = bool_of_string v }
6166 | "rotation-angle" -> { c with angle = int_of_string v }
6167 | "width" -> { c with winw = max 20 (int_of_string v) }
6168 | "height" -> { c with winh = max 20 (int_of_string v) }
6169 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6170 | "proportional-display" -> { c with proportional = bool_of_string v }
6171 | "pixmap-cache-size" ->
6172 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6173 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6174 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6175 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6176 | "persistent-location" -> { c with jumpback = bool_of_string v }
6177 | "background-color" -> { c with bgcolor = color_of_string v }
6178 | "scrollbar-in-presentation" ->
6179 { c with scrollbarinpm = bool_of_string v }
6180 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6181 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6182 | "mupdf-store-size" ->
6183 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6184 | "checkers" -> { c with checkers = bool_of_string v }
6185 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6186 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6187 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6188 | "uri-launcher" -> { c with urilauncher = unent v }
6189 | "path-launcher" -> { c with pathlauncher = unent v }
6190 | "color-space" -> { c with colorspace = colorspace_of_string v }
6191 | "invert-colors" -> { c with invert = bool_of_string v }
6192 | "brightness" -> { c with colorscale = float_of_string v }
6193 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6194 | "ghyllscroll" ->
6195 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6196 | "columns" ->
6197 let (n, _, _) as nab = multicolumns_of_string v in
6198 if n < 0
6199 then { c with columns = Csplit (-n, [||]) }
6200 else { c with columns = Cmulti (nab, [||]) }
6201 | "birds-eye-columns" ->
6202 { c with beyecolumns = Some (max (int_of_string v) 2) }
6203 | "selection-command" -> { c with selcmd = unent v }
6204 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6205 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6206 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6207 | "use-pbo" -> { c with usepbo = bool_of_string v }
6208 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6209 | _ -> c
6210 with exn ->
6211 prerr_endline ("Error processing attribute (`" ^
6212 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6215 let rec fold c = function
6216 | [] -> c
6217 | (k, v) :: rest ->
6218 let c = apply c k v in
6219 fold c rest
6221 fold { c with keyhashes = copykeyhashes c } attrs;
6224 let fromstring f pos n v d =
6225 try f v
6226 with exn ->
6227 dolog "Error processing attribute (%S=%S) at %d\n%s"
6228 n v pos (Printexc.to_string exn)
6233 let bookmark_of attrs =
6234 let rec fold title page rely visy = function
6235 | ("title", v) :: rest -> fold v page rely visy rest
6236 | ("page", v) :: rest -> fold title v rely visy rest
6237 | ("rely", v) :: rest -> fold title page v visy rest
6238 | ("visy", v) :: rest -> fold title page rely v rest
6239 | _ :: rest -> fold title page rely visy rest
6240 | [] -> title, page, rely, visy
6242 fold "invalid" "0" "0" "0" attrs
6245 let doc_of attrs =
6246 let rec fold path page rely pan visy = function
6247 | ("path", v) :: rest -> fold v page rely pan visy rest
6248 | ("page", v) :: rest -> fold path v rely pan visy rest
6249 | ("rely", v) :: rest -> fold path page v pan visy rest
6250 | ("pan", v) :: rest -> fold path page rely v visy rest
6251 | ("visy", v) :: rest -> fold path page rely pan v rest
6252 | _ :: rest -> fold path page rely pan visy rest
6253 | [] -> path, page, rely, pan, visy
6255 fold "" "0" "0" "0" "0" attrs
6258 let map_of attrs =
6259 let rec fold rs ls = function
6260 | ("out", v) :: rest -> fold v ls rest
6261 | ("in", v) :: rest -> fold rs v rest
6262 | _ :: rest -> fold ls rs rest
6263 | [] -> ls, rs
6265 fold "" "" attrs
6268 let setconf dst src =
6269 dst.scrollbw <- src.scrollbw;
6270 dst.scrollh <- src.scrollh;
6271 dst.icase <- src.icase;
6272 dst.preload <- src.preload;
6273 dst.pagebias <- src.pagebias;
6274 dst.verbose <- src.verbose;
6275 dst.scrollstep <- src.scrollstep;
6276 dst.maxhfit <- src.maxhfit;
6277 dst.crophack <- src.crophack;
6278 dst.autoscrollstep <- src.autoscrollstep;
6279 dst.maxwait <- src.maxwait;
6280 dst.hlinks <- src.hlinks;
6281 dst.underinfo <- src.underinfo;
6282 dst.interpagespace <- src.interpagespace;
6283 dst.zoom <- src.zoom;
6284 dst.presentation <- src.presentation;
6285 dst.angle <- src.angle;
6286 dst.winw <- src.winw;
6287 dst.winh <- src.winh;
6288 dst.savebmarks <- src.savebmarks;
6289 dst.memlimit <- src.memlimit;
6290 dst.proportional <- src.proportional;
6291 dst.texcount <- src.texcount;
6292 dst.sliceheight <- src.sliceheight;
6293 dst.thumbw <- src.thumbw;
6294 dst.jumpback <- src.jumpback;
6295 dst.bgcolor <- src.bgcolor;
6296 dst.scrollbarinpm <- src.scrollbarinpm;
6297 dst.tilew <- src.tilew;
6298 dst.tileh <- src.tileh;
6299 dst.mustoresize <- src.mustoresize;
6300 dst.checkers <- src.checkers;
6301 dst.aalevel <- src.aalevel;
6302 dst.trimmargins <- src.trimmargins;
6303 dst.trimfuzz <- src.trimfuzz;
6304 dst.urilauncher <- src.urilauncher;
6305 dst.colorspace <- src.colorspace;
6306 dst.invert <- src.invert;
6307 dst.colorscale <- src.colorscale;
6308 dst.redirectstderr <- src.redirectstderr;
6309 dst.ghyllscroll <- src.ghyllscroll;
6310 dst.columns <- src.columns;
6311 dst.beyecolumns <- src.beyecolumns;
6312 dst.selcmd <- src.selcmd;
6313 dst.updatecurs <- src.updatecurs;
6314 dst.pathlauncher <- src.pathlauncher;
6315 dst.keyhashes <- copykeyhashes src;
6316 dst.hfsize <- src.hfsize;
6317 dst.hscrollstep <- src.hscrollstep;
6318 dst.pgscale <- src.pgscale;
6319 dst.usepbo <- src.usepbo;
6320 dst.wheelbypage <- src.wheelbypage;
6323 let get s =
6324 let h = Hashtbl.create 10 in
6325 let dc = { defconf with angle = defconf.angle } in
6326 let rec toplevel v t spos _ =
6327 match t with
6328 | Vdata | Vcdata | Vend -> v
6329 | Vopen ("llppconfig", _, closed) ->
6330 if closed
6331 then v
6332 else { v with f = llppconfig }
6333 | Vopen _ ->
6334 error "unexpected subelement at top level" s spos
6335 | Vclose _ -> error "unexpected close at top level" s spos
6337 and llppconfig v t spos _ =
6338 match t with
6339 | Vdata | Vcdata -> v
6340 | Vend -> error "unexpected end of input in llppconfig" s spos
6341 | Vopen ("defaults", attrs, closed) ->
6342 let c = config_of dc attrs in
6343 setconf dc c;
6344 if closed
6345 then v
6346 else { v with f = defaults }
6348 | Vopen ("ui-font", attrs, closed) ->
6349 let rec getsize size = function
6350 | [] -> size
6351 | ("size", v) :: rest ->
6352 let size =
6353 fromstring int_of_string spos "size" v fstate.fontsize in
6354 getsize size rest
6355 | l -> getsize size l
6357 fstate.fontsize <- getsize fstate.fontsize attrs;
6358 if closed
6359 then v
6360 else { v with f = uifont (Buffer.create 10) }
6362 | Vopen ("doc", attrs, closed) ->
6363 let pathent, spage, srely, span, svisy = doc_of attrs in
6364 let path = unent pathent
6365 and pageno = fromstring int_of_string spos "page" spage 0
6366 and rely = fromstring float_of_string spos "rely" srely 0.0
6367 and pan = fromstring int_of_string spos "pan" span 0
6368 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6369 let c = config_of dc attrs in
6370 let anchor = (pageno, rely, visy) in
6371 if closed
6372 then (Hashtbl.add h path (c, [], pan, anchor); v)
6373 else { v with f = doc path pan anchor c [] }
6375 | Vopen _ ->
6376 error "unexpected subelement in llppconfig" s spos
6378 | Vclose "llppconfig" -> { v with f = toplevel }
6379 | Vclose _ -> error "unexpected close in llppconfig" s spos
6381 and defaults v t spos _ =
6382 match t with
6383 | Vdata | Vcdata -> v
6384 | Vend -> error "unexpected end of input in defaults" s spos
6385 | Vopen ("keymap", attrs, closed) ->
6386 let modename =
6387 try List.assoc "mode" attrs
6388 with Not_found -> "global" in
6389 if closed
6390 then v
6391 else
6392 let ret keymap =
6393 let h = findkeyhash dc modename in
6394 KeyMap.iter (Hashtbl.replace h) keymap;
6395 defaults
6397 { v with f = pkeymap ret KeyMap.empty }
6399 | Vopen (_, _, _) ->
6400 error "unexpected subelement in defaults" s spos
6402 | Vclose "defaults" ->
6403 { v with f = llppconfig }
6405 | Vclose _ -> error "unexpected close in defaults" s spos
6407 and uifont b v t spos epos =
6408 match t with
6409 | Vdata | Vcdata ->
6410 Buffer.add_substring b s spos (epos - spos);
6412 | Vopen (_, _, _) ->
6413 error "unexpected subelement in ui-font" s spos
6414 | Vclose "ui-font" ->
6415 if String.length !fontpath = 0
6416 then fontpath := Buffer.contents b;
6417 { v with f = llppconfig }
6418 | Vclose _ -> error "unexpected close in ui-font" s spos
6419 | Vend -> error "unexpected end of input in ui-font" s spos
6421 and doc path pan anchor c bookmarks v t spos _ =
6422 match t with
6423 | Vdata | Vcdata -> v
6424 | Vend -> error "unexpected end of input in doc" s spos
6425 | Vopen ("bookmarks", _, closed) ->
6426 if closed
6427 then v
6428 else { v with f = pbookmarks path pan anchor c bookmarks }
6430 | Vopen ("keymap", attrs, closed) ->
6431 let modename =
6432 try List.assoc "mode" attrs
6433 with Not_found -> "global"
6435 if closed
6436 then v
6437 else
6438 let ret keymap =
6439 let h = findkeyhash c modename in
6440 KeyMap.iter (Hashtbl.replace h) keymap;
6441 doc path pan anchor c bookmarks
6443 { v with f = pkeymap ret KeyMap.empty }
6445 | Vopen (_, _, _) ->
6446 error "unexpected subelement in doc" s spos
6448 | Vclose "doc" ->
6449 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6450 { v with f = llppconfig }
6452 | Vclose _ -> error "unexpected close in doc" s spos
6454 and pkeymap ret keymap v t spos _ =
6455 match t with
6456 | Vdata | Vcdata -> v
6457 | Vend -> error "unexpected end of input in keymap" s spos
6458 | Vopen ("map", attrs, closed) ->
6459 let r, l = map_of attrs in
6460 let kss = fromstring keys_of_string spos "in" r [] in
6461 let lss = fromstring keys_of_string spos "out" l [] in
6462 let keymap =
6463 match kss with
6464 | [] -> keymap
6465 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6466 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6468 if closed
6469 then { v with f = pkeymap ret keymap }
6470 else
6471 let f () = v in
6472 { v with f = skip "map" f }
6474 | Vopen _ ->
6475 error "unexpected subelement in keymap" s spos
6477 | Vclose "keymap" ->
6478 { v with f = ret keymap }
6480 | Vclose _ -> error "unexpected close in keymap" s spos
6482 and pbookmarks path pan anchor c bookmarks v t spos _ =
6483 match t with
6484 | Vdata | Vcdata -> v
6485 | Vend -> error "unexpected end of input in bookmarks" s spos
6486 | Vopen ("item", attrs, closed) ->
6487 let titleent, spage, srely, svisy = bookmark_of attrs in
6488 let page = fromstring int_of_string spos "page" spage 0
6489 and rely = fromstring float_of_string spos "rely" srely 0.0
6490 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6491 let bookmarks =
6492 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6494 if closed
6495 then { v with f = pbookmarks path pan anchor c bookmarks }
6496 else
6497 let f () = v in
6498 { v with f = skip "item" f }
6500 | Vopen _ ->
6501 error "unexpected subelement in bookmarks" s spos
6503 | Vclose "bookmarks" ->
6504 { v with f = doc path pan anchor c bookmarks }
6506 | Vclose _ -> error "unexpected close in bookmarks" s spos
6508 and skip tag f v t spos _ =
6509 match t with
6510 | Vdata | Vcdata -> v
6511 | Vend ->
6512 error ("unexpected end of input in skipped " ^ tag) s spos
6513 | Vopen (tag', _, closed) ->
6514 if closed
6515 then v
6516 else
6517 let f' () = { v with f = skip tag f } in
6518 { v with f = skip tag' f' }
6519 | Vclose ctag ->
6520 if tag = ctag
6521 then f ()
6522 else error ("unexpected close in skipped " ^ tag) s spos
6525 parse { f = toplevel; accu = () } s;
6526 h, dc;
6529 let do_load f ic =
6531 let len = in_channel_length ic in
6532 let s = String.create len in
6533 really_input ic s 0 len;
6534 f s;
6535 with
6536 | Parse_error (msg, s, pos) ->
6537 let subs = subs s pos in
6538 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6539 failwith ("parse error: " ^ s)
6541 | exn ->
6542 failwith ("config load error: " ^ Printexc.to_string exn)
6545 let defconfpath =
6546 let dir =
6548 let dir = Filename.concat home ".config" in
6549 if Sys.is_directory dir then dir else home
6550 with _ -> home
6552 Filename.concat dir "llpp.conf"
6555 let confpath = ref defconfpath;;
6557 let load1 f =
6558 if Sys.file_exists !confpath
6559 then
6560 match
6561 (try Some (open_in_bin !confpath)
6562 with exn ->
6563 prerr_endline
6564 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6565 Printexc.to_string exn);
6566 None
6568 with
6569 | Some ic ->
6570 let success =
6572 f (do_load get ic)
6573 with exn ->
6574 prerr_endline
6575 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6576 Printexc.to_string exn);
6577 false
6579 close_in ic;
6580 success
6582 | None -> false
6583 else
6584 f (Hashtbl.create 0, defconf)
6587 let load () =
6588 let f (h, dc) =
6589 let pc, pb, px, pa =
6591 Hashtbl.find h (Filename.basename state.path)
6592 with Not_found -> dc, [], 0, emptyanchor
6594 setconf defconf dc;
6595 setconf conf pc;
6596 state.bookmarks <- pb;
6597 state.x <- px;
6598 state.scrollw <- conf.scrollbw;
6599 if conf.jumpback
6600 then state.anchor <- pa;
6601 cbput state.hists.nav pa;
6602 true
6604 load1 f
6607 let add_attrs bb always dc c =
6608 let ob s a b =
6609 if always || a != b
6610 then Printf.bprintf bb "\n %s='%b'" s a
6611 and oi s a b =
6612 if always || a != b
6613 then Printf.bprintf bb "\n %s='%d'" s a
6614 and oI s a b =
6615 if always || a != b
6616 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6617 and oz s a b =
6618 if always || a <> b
6619 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6620 and oF s a b =
6621 if always || a <> b
6622 then Printf.bprintf bb "\n %s='%f'" s a
6623 and oc s a b =
6624 if always || a <> b
6625 then
6626 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6627 and oC s a b =
6628 if always || a <> b
6629 then
6630 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6631 and oR s a b =
6632 if always || a <> b
6633 then
6634 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6635 and os s a b =
6636 if always || a <> b
6637 then
6638 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6639 and og s a b =
6640 if always || a <> b
6641 then
6642 match a with
6643 | None -> ()
6644 | Some (_N, _A, _B) ->
6645 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6646 and oW s a b =
6647 if always || a <> b
6648 then
6649 let v =
6650 match a with
6651 | None -> "false"
6652 | Some f ->
6653 if f = infinity
6654 then "true"
6655 else string_of_float f
6657 Printf.bprintf bb "\n %s='%s'" s v
6658 and oco s a b =
6659 if always || a <> b
6660 then
6661 match a with
6662 | Cmulti ((n, a, b), _) when n > 1 ->
6663 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6664 | Csplit (n, _) when n > 1 ->
6665 Printf.bprintf bb "\n %s='%d'" s ~-n
6666 | _ -> ()
6667 and obeco s a b =
6668 if always || a <> b
6669 then
6670 match a with
6671 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6672 | _ -> ()
6674 let w, h =
6675 if always
6676 then dc.winw, dc.winh
6677 else
6678 match state.fullscreen with
6679 | Some wh -> wh
6680 | None -> c.winw, c.winh
6682 oi "width" w dc.winw;
6683 oi "height" h dc.winh;
6684 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6685 oi "scroll-handle-height" c.scrollh dc.scrollh;
6686 ob "case-insensitive-search" c.icase dc.icase;
6687 ob "preload" c.preload dc.preload;
6688 oi "page-bias" c.pagebias dc.pagebias;
6689 oi "scroll-step" c.scrollstep dc.scrollstep;
6690 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6691 ob "max-height-fit" c.maxhfit dc.maxhfit;
6692 ob "crop-hack" c.crophack dc.crophack;
6693 oW "throttle" c.maxwait dc.maxwait;
6694 ob "highlight-links" c.hlinks dc.hlinks;
6695 ob "under-cursor-info" c.underinfo dc.underinfo;
6696 oi "vertical-margin" c.interpagespace dc.interpagespace;
6697 oz "zoom" c.zoom dc.zoom;
6698 ob "presentation" c.presentation dc.presentation;
6699 oi "rotation-angle" c.angle dc.angle;
6700 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6701 ob "proportional-display" c.proportional dc.proportional;
6702 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6703 oi "tex-count" c.texcount dc.texcount;
6704 oi "slice-height" c.sliceheight dc.sliceheight;
6705 oi "thumbnail-width" c.thumbw dc.thumbw;
6706 ob "persistent-location" c.jumpback dc.jumpback;
6707 oc "background-color" c.bgcolor dc.bgcolor;
6708 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6709 oi "tile-width" c.tilew dc.tilew;
6710 oi "tile-height" c.tileh dc.tileh;
6711 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6712 ob "checkers" c.checkers dc.checkers;
6713 oi "aalevel" c.aalevel dc.aalevel;
6714 ob "trim-margins" c.trimmargins dc.trimmargins;
6715 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6716 os "uri-launcher" c.urilauncher dc.urilauncher;
6717 os "path-launcher" c.pathlauncher dc.pathlauncher;
6718 oC "color-space" c.colorspace dc.colorspace;
6719 ob "invert-colors" c.invert dc.invert;
6720 oF "brightness" c.colorscale dc.colorscale;
6721 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6722 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6723 oco "columns" c.columns dc.columns;
6724 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6725 os "selection-command" c.selcmd dc.selcmd;
6726 ob "update-cursor" c.updatecurs dc.updatecurs;
6727 oi "hint-font-size" c.hfsize dc.hfsize;
6728 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6729 oF "page-scroll-scale" c.pgscale dc.pgscale;
6730 ob "use-pbo" c.usepbo dc.usepbo;
6731 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6734 let keymapsbuf always dc c =
6735 let bb = Buffer.create 16 in
6736 let rec loop = function
6737 | [] -> ()
6738 | (modename, h) :: rest ->
6739 let dh = findkeyhash dc modename in
6740 if always || h <> dh
6741 then (
6742 if Hashtbl.length h > 0
6743 then (
6744 if Buffer.length bb > 0
6745 then Buffer.add_char bb '\n';
6746 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6747 Hashtbl.iter (fun i o ->
6748 let isdifferent = always ||
6750 let dO = Hashtbl.find dh i in
6751 dO <> o
6752 with Not_found -> true
6754 if isdifferent
6755 then
6756 let addkm (k, m) =
6757 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6758 if Wsi.withalt m then Buffer.add_string bb "alt-";
6759 if Wsi.withshift m then Buffer.add_string bb "shift-";
6760 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6761 Buffer.add_string bb (Wsi.keyname k);
6763 let addkms l =
6764 let rec loop = function
6765 | [] -> ()
6766 | km :: [] -> addkm km
6767 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6769 loop l
6771 Buffer.add_string bb "<map in='";
6772 addkm i;
6773 match o with
6774 | KMinsrt km ->
6775 Buffer.add_string bb "' out='";
6776 addkm km;
6777 Buffer.add_string bb "'/>\n"
6779 | KMinsrl kms ->
6780 Buffer.add_string bb "' out='";
6781 addkms kms;
6782 Buffer.add_string bb "'/>\n"
6784 | KMmulti (ins, kms) ->
6785 Buffer.add_char bb ' ';
6786 addkms ins;
6787 Buffer.add_string bb "' out='";
6788 addkms kms;
6789 Buffer.add_string bb "'/>\n"
6790 ) h;
6791 Buffer.add_string bb "</keymap>";
6794 loop rest
6796 loop c.keyhashes;
6800 let save () =
6801 let uifontsize = fstate.fontsize in
6802 let bb = Buffer.create 32768 in
6803 let f (h, dc) =
6804 let dc = if conf.bedefault then conf else dc in
6805 Buffer.add_string bb "<llppconfig>\n";
6807 if String.length !fontpath > 0
6808 then
6809 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6810 uifontsize
6811 !fontpath
6812 else (
6813 if uifontsize <> 14
6814 then
6815 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6818 Buffer.add_string bb "<defaults ";
6819 add_attrs bb true dc dc;
6820 let kb = keymapsbuf true dc dc in
6821 if Buffer.length kb > 0
6822 then (
6823 Buffer.add_string bb ">\n";
6824 Buffer.add_buffer bb kb;
6825 Buffer.add_string bb "\n</defaults>\n";
6827 else Buffer.add_string bb "/>\n";
6829 let adddoc path pan anchor c bookmarks =
6830 if bookmarks == [] && c = dc && anchor = emptyanchor
6831 then ()
6832 else (
6833 Printf.bprintf bb "<doc path='%s'"
6834 (enent path 0 (String.length path));
6836 if anchor <> emptyanchor
6837 then (
6838 let n, rely, visy = anchor in
6839 Printf.bprintf bb " page='%d'" n;
6840 if rely > 1e-6
6841 then
6842 Printf.bprintf bb " rely='%f'" rely
6844 if abs_float visy > 1e-6
6845 then
6846 Printf.bprintf bb " visy='%f'" visy
6850 if pan != 0
6851 then Printf.bprintf bb " pan='%d'" pan;
6853 add_attrs bb false dc c;
6854 let kb = keymapsbuf false dc c in
6856 begin match bookmarks with
6857 | [] ->
6858 if Buffer.length kb > 0
6859 then (
6860 Buffer.add_string bb ">\n";
6861 Buffer.add_buffer bb kb;
6862 Buffer.add_string bb "\n</doc>\n";
6864 else Buffer.add_string bb "/>\n"
6865 | _ ->
6866 Buffer.add_string bb ">\n<bookmarks>\n";
6867 List.iter (fun (title, _level, (page, rely, visy)) ->
6868 Printf.bprintf bb
6869 "<item title='%s' page='%d'"
6870 (enent title 0 (String.length title))
6871 page
6873 if rely > 1e-6
6874 then
6875 Printf.bprintf bb " rely='%f'" rely
6877 if abs_float visy > 1e-6
6878 then
6879 Printf.bprintf bb " visy='%f'" visy
6881 Buffer.add_string bb "/>\n";
6882 ) bookmarks;
6883 Buffer.add_string bb "</bookmarks>";
6884 if Buffer.length kb > 0
6885 then (
6886 Buffer.add_string bb "\n";
6887 Buffer.add_buffer bb kb;
6889 Buffer.add_string bb "\n</doc>\n";
6890 end;
6894 let pan, conf =
6895 match state.mode with
6896 | Birdseye (c, pan, _, _, _) ->
6897 let beyecolumns =
6898 match conf.columns with
6899 | Cmulti ((c, _, _), _) -> Some c
6900 | Csingle _ -> None
6901 | Csplit _ -> None
6902 and columns =
6903 match c.columns with
6904 | Cmulti (c, _) -> Cmulti (c, [||])
6905 | Csingle _ -> Csingle [||]
6906 | Csplit _ -> failwith "quit from bird's eye while split"
6908 pan, { c with beyecolumns = beyecolumns; columns = columns }
6909 | _ -> state.x, conf
6911 let basename = Filename.basename state.path in
6912 adddoc basename pan (getanchor ())
6913 (let conf =
6914 let autoscrollstep =
6915 match state.autoscroll with
6916 | Some step -> step
6917 | None -> conf.autoscrollstep
6919 match state.mode with
6920 | Birdseye (bc, _, _, _, _) ->
6921 { conf with
6922 zoom = bc.zoom;
6923 presentation = bc.presentation;
6924 interpagespace = bc.interpagespace;
6925 maxwait = bc.maxwait;
6926 autoscrollstep = autoscrollstep }
6927 | _ -> { conf with autoscrollstep = autoscrollstep }
6928 in conf)
6929 (if conf.savebmarks then state.bookmarks else []);
6931 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6932 if basename <> path
6933 then adddoc path x anchor c bookmarks
6934 ) h;
6935 Buffer.add_string bb "</llppconfig>\n";
6936 true;
6938 if load1 f && Buffer.length bb > 0
6939 then
6941 let tmp = !confpath ^ ".tmp" in
6942 let oc = open_out_bin tmp in
6943 Buffer.output_buffer oc bb;
6944 close_out oc;
6945 Unix.rename tmp !confpath;
6946 with exn ->
6947 prerr_endline
6948 ("error while saving configuration: " ^ Printexc.to_string exn)
6950 end;;
6952 let () =
6953 let trimcachepath = ref "" in
6954 Arg.parse
6955 (Arg.align
6956 [("-p", Arg.String (fun s -> state.password <- s) ,
6957 "<password> Set password");
6959 ("-f", Arg.String (fun s -> Config.fontpath := s),
6960 "<path> Set path to the user interface font");
6962 ("-c", Arg.String (fun s -> Config.confpath := s),
6963 "<path> Set path to the configuration file");
6965 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6966 "<path> Set path to the trim cache file");
6968 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6969 "<named destination> Set named destination");
6971 ("-v", Arg.Unit (fun () ->
6972 Printf.printf
6973 "%s\nconfiguration path: %s\n"
6974 (version ())
6975 Config.defconfpath
6977 exit 0), " Print version and exit");
6980 (fun s -> state.path <- s)
6981 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6983 if String.length state.path = 0
6984 then (prerr_endline "file name missing"; exit 1);
6986 if not (Config.load ())
6987 then prerr_endline "failed to load configuration";
6989 let globalkeyhash = findkeyhash conf "global" in
6990 let wsfd, winw, winh = Wsi.init (object
6991 method expose =
6992 state.wthack <- false;
6993 if nogeomcmds state.geomcmds || platform == Posx
6994 then display ()
6995 else (
6996 GlClear.color (scalecolor2 conf.bgcolor);
6997 GlClear.clear [`color];
6999 method display = display ()
7000 method reshape w h = reshape w h
7001 method mouse b d x y m = mouse b d x y m
7002 method motion x y = state.mpos <- (x, y); motion x y
7003 method pmotion x y = state.mpos <- (x, y); pmotion x y
7004 method key k m =
7005 let mascm = m land (
7006 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7007 ) in
7008 match state.keystate with
7009 | KSnone ->
7010 let km = k, mascm in
7011 begin
7012 match
7013 let modehash = state.uioh#modehash in
7014 try Hashtbl.find modehash km
7015 with Not_found ->
7016 try Hashtbl.find globalkeyhash km
7017 with Not_found -> KMinsrt (k, m)
7018 with
7019 | KMinsrt (k, m) -> keyboard k m
7020 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7021 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7023 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7024 List.iter (fun (k, m) -> keyboard k m) insrt;
7025 state.keystate <- KSnone
7026 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7027 state.keystate <- KSinto (keys, insrt)
7028 | _ ->
7029 state.keystate <- KSnone
7031 method enter x y = state.mpos <- (x, y); pmotion x y
7032 method leave = state.mpos <- (-1, -1)
7033 method quit = raise Quit
7034 end) conf.winw conf.winh (platform = Posx) in
7036 state.wsfd <- wsfd;
7038 if not (
7039 List.exists GlMisc.check_extension
7040 [ "GL_ARB_texture_rectangle"
7041 ; "GL_EXT_texture_recangle"
7042 ; "GL_NV_texture_rectangle" ]
7044 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7046 let cr, sw =
7047 match Ne.pipe () with
7048 | Ne.Exn exn ->
7049 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
7050 exit 1
7051 | Ne.Res rw -> rw
7052 and sr, cw =
7053 match Ne.pipe () with
7054 | Ne.Exn exn ->
7055 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7056 exit 1
7057 | Ne.Res rw -> rw
7060 cloexec cr;
7061 cloexec sw;
7062 cloexec sr;
7063 cloexec cw;
7065 setcheckers conf.checkers;
7066 redirectstderr ();
7068 init (cr, cw) (
7069 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7070 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7071 !Config.fontpath, !trimcachepath,
7072 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7074 state.sr <- sr;
7075 state.sw <- sw;
7076 state.text <- "Opening " ^ (mbtoutf8 state.path);
7077 reshape winw winh;
7078 opendoc state.path state.password;
7079 state.uioh <- uioh;
7081 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7083 let rec loop deadline =
7084 let r =
7085 match state.errfd with
7086 | None -> [state.sr; state.wsfd]
7087 | Some fd -> [state.sr; state.wsfd; fd]
7089 if state.redisplay
7090 then (
7091 state.redisplay <- false;
7092 display ();
7094 let timeout =
7095 let now = now () in
7096 if deadline > now
7097 then (
7098 if deadline = infinity
7099 then ~-.1.0
7100 else max 0.0 (deadline -. now)
7102 else 0.0
7104 let r, _, _ =
7105 try tempfailureretry (Unix.select r [] []) timeout
7106 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7108 begin match r with
7109 | [] ->
7110 state.ghyll None;
7111 let newdeadline =
7112 if state.ghyll == noghyll
7113 then
7114 match state.autoscroll with
7115 | Some step when step != 0 ->
7116 let y = state.y + step in
7117 let y =
7118 if y < 0
7119 then state.maxy
7120 else if y >= state.maxy then 0 else y
7122 gotoy y;
7123 if state.mode = View
7124 then state.text <- "";
7125 deadline +. 0.01
7126 | _ -> infinity
7127 else deadline +. 0.01
7129 loop newdeadline
7131 | l ->
7132 let rec checkfds = function
7133 | [] -> ()
7134 | fd :: rest when fd = state.sr ->
7135 let cmd = readcmd state.sr in
7136 act cmd;
7137 checkfds rest
7139 | fd :: rest when fd = state.wsfd ->
7140 Wsi.readresp fd;
7141 checkfds rest
7143 | fd :: rest ->
7144 let s = String.create 80 in
7145 let n = tempfailureretry (Unix.read fd s 0) 80 in
7146 if conf.redirectstderr
7147 then (
7148 Buffer.add_substring state.errmsgs s 0 n;
7149 state.newerrmsgs <- true;
7150 state.redisplay <- true;
7152 else (
7153 prerr_string (String.sub s 0 n);
7154 flush stderr;
7156 checkfds rest
7158 checkfds l;
7159 let newdeadline =
7160 let deadline1 =
7161 if deadline = infinity
7162 then now () +. 0.01
7163 else deadline
7165 match state.autoscroll with
7166 | Some step when step != 0 -> deadline1
7167 | _ -> if state.ghyll == noghyll then infinity else deadline1
7169 loop newdeadline
7170 end;
7173 loop infinity;
7174 with Quit ->
7175 Config.save ();