Cosmetics
[llpp.git] / main.ml
blobf19a47cf66ee128999e84ac1faccf2f1b61094f7
1 open Utils;;
3 exception Quit;;
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 type params = (angle * fitmodel * trimparams
17 * texcount * sliceheight * memsize
18 * colorspace * fontpath * trimcachepath
19 * haspbo)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and trimmargins = bool
29 and interpagespace = int
30 and texcount = int
31 and sliceheight = int
32 and gen = int
33 and top = float
34 and dtop = float
35 and fontpath = string
36 and trimcachepath = string
37 and memsize = int
38 and aalevel = int
39 and irect = (int * int * int * int)
40 and trimparams = (trimmargins * irect)
41 and colorspace = | Rgb | Bgr | Gray
42 and fitmodel = | FitWidth | FitProportional | FitPage
43 and haspbo = bool
46 type x = int
47 and y = int
48 and tilex = int
49 and tiley = int
50 and tileparams = (x * y * width * height * tilex * tiley)
53 type link =
54 | Lnotfound
55 | Lfound of int
56 and linkdir =
57 | LDfirst
58 | LDlast
59 | LDfirstvisible of (int * int * int)
60 | LDleft of int
61 | LDright of int
62 | LDdown of int
63 | LDup of int
66 type pagewithlinks =
67 | Pwlnotfound
68 | Pwl of int
71 type keymap =
72 | KMinsrt of key
73 | KMinsrl of key list
74 | KMmulti of key list * key list
75 and key = int * int
76 and keyhash = (key, keymap) Hashtbl.t
77 and keystate =
78 | KSnone
79 | KSinto of (key list * key list)
82 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
83 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
85 type pipe = (Unix.file_descr * Unix.file_descr);;
87 external init : pipe -> params -> unit = "ml_init";;
88 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
89 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
90 external getpdimrect : int -> float array = "ml_getpdimrect";;
91 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
92 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
93 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
94 external measurestr : int -> string -> float = "ml_measure_string";;
95 external postprocess :
96 opaque -> int -> int -> int -> (int * string * int) -> int
97 = "ml_postprocess";;
98 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
99 external platform : unit -> platform = "ml_platform";;
100 external setaalevel : int -> unit = "ml_setaalevel";;
101 external realloctexts : int -> bool = "ml_realloctexts";;
102 external findlink : opaque -> linkdir -> link = "ml_findlink";;
103 external getlink : opaque -> int -> under = "ml_getlink";;
104 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
105 external getlinkcount : opaque -> int = "ml_getlinkcount";;
106 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
107 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
108 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
109 external freepbo : string -> unit = "ml_freepbo";;
110 external unmappbo : string -> unit = "ml_unmappbo";;
111 external pbousable : unit -> bool = "ml_pbo_usable";;
112 external unproject : opaque -> int -> int -> (int * int) option
113 = "ml_unproject";;
114 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
116 let platform_to_string = function
117 | Punknown -> "unknown"
118 | Plinux -> "Linux"
119 | Posx -> "OSX"
120 | Psun -> "Sun"
121 | Pfreebsd -> "FreeBSD"
122 | Pdragonflybsd -> "DragonflyBSD"
123 | Popenbsd -> "OpenBSD"
124 | Pnetbsd -> "NetBSD"
125 | Pcygwin -> "Cygwin"
128 let platform = platform ();;
130 let now = Unix.gettimeofday;;
132 let popen cmd fda =
133 if platform = Pcygwin
134 then (
135 let sh = "/bin/sh" in
136 let args = [|sh; "-c"; cmd|] in
137 let rec std si so se = function
138 | [] -> si, so, se
139 | (fd, 0) :: rest -> std fd so se rest
140 | (fd, -1) :: rest ->
141 Unix.set_close_on_exec fd;
142 std si so se rest
143 | (_, n) :: _ ->
144 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
146 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
147 ignore (Unix.create_process sh args si so se)
149 else popen cmd fda;
152 type mpos = int * int
153 and mstate =
154 | Msel of (mpos * mpos)
155 | Mpan of mpos
156 | Mscrolly | Mscrollx
157 | Mzoom of (int * int)
158 | Mzoomrect of (mpos * mpos)
159 | Mnone
162 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
163 and onkey = string -> int -> te
164 and ondone = string -> unit
165 and histcancel = unit -> unit
166 and onhist = ((histcmd -> string) * histcancel)
167 and histcmd = HCnext | HCprev | HCfirst | HClast
168 and cancelonempty = bool
169 and te =
170 | TEstop
171 | TEdone of string
172 | TEcont of string
173 | TEswitch of textentry
176 type 'a circbuf =
177 { store : 'a array
178 ; mutable rc : int
179 ; mutable wc : int
180 ; mutable len : int
184 let bound v minv maxv =
185 max minv (min maxv v);
188 let cbnew n v =
189 { store = Array.create n v
190 ; rc = 0
191 ; wc = 0
192 ; len = 0
196 let cbcap b = Array.length b.store;;
198 let cbput b v =
199 let cap = cbcap b in
200 b.store.(b.wc) <- v;
201 b.wc <- (b.wc + 1) mod cap;
202 b.rc <- b.wc;
203 b.len <- min (b.len + 1) cap;
206 let cbempty b = b.len = 0;;
208 let cbgetg b circular dir =
209 if cbempty b
210 then b.store.(0)
211 else
212 let rc = b.rc + dir in
213 let rc =
214 if circular
215 then (
216 if rc = -1
217 then b.len-1
218 else (
219 if rc >= b.len
220 then 0
221 else rc
224 else bound rc 0 (b.len-1)
226 b.rc <- rc;
227 b.store.(rc);
230 let cbget b = cbgetg b false;;
231 let cbgetc b = cbgetg b true;;
233 let drawstring size x y s =
234 Gl.enable `blend;
235 Gl.enable `texture_2d;
236 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
237 ignore (drawstr size x y s);
238 Gl.disable `blend;
239 Gl.disable `texture_2d;
242 let drawstring1 size x y s =
243 drawstr size x y s;
246 let drawstring2 size x y fmt =
247 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
250 type page =
251 { pageno : int
252 ; pagedimno : int
253 ; pagew : int
254 ; pageh : int
255 ; pagex : int
256 ; pagey : int
257 ; pagevw : int
258 ; pagevh : int
259 ; pagedispx : int
260 ; pagedispy : int
261 ; pagecol : int
265 let debugl l =
266 dolog "l %d dim=%d {" l.pageno l.pagedimno;
267 dolog " WxH %dx%d" l.pagew l.pageh;
268 dolog " vWxH %dx%d" l.pagevw l.pagevh;
269 dolog " pagex,y %d,%d" l.pagex l.pagey;
270 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
271 dolog " column %d" l.pagecol;
272 dolog "}";
275 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
276 dolog "rect {";
277 dolog " x0,y0=(% f, % f)" x0 y0;
278 dolog " x1,y1=(% f, % f)" x1 y1;
279 dolog " x2,y2=(% f, % f)" x2 y2;
280 dolog " x3,y3=(% f, % f)" x3 y3;
281 dolog "}";
284 type multicolumns = multicol * pagegeom
285 and singlecolumn = pagegeom
286 and splitcolumns = columncount * pagegeom
287 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
288 and multicol = columncount * covercount * covercount
289 and pdimno = int
290 and columncount = int
291 and covercount = int;;
293 type conf =
294 { mutable scrollbw : int
295 ; mutable scrollh : int
296 ; mutable icase : bool
297 ; mutable preload : bool
298 ; mutable pagebias : int
299 ; mutable verbose : bool
300 ; mutable debug : bool
301 ; mutable scrollstep : int
302 ; mutable hscrollstep : int
303 ; mutable maxhfit : bool
304 ; mutable crophack : bool
305 ; mutable autoscrollstep : int
306 ; mutable maxwait : float option
307 ; mutable hlinks : bool
308 ; mutable underinfo : bool
309 ; mutable interpagespace : interpagespace
310 ; mutable zoom : float
311 ; mutable presentation : bool
312 ; mutable angle : angle
313 ; mutable cwinw : int
314 ; mutable cwinh : int
315 ; mutable savebmarks : bool
316 ; mutable fitmodel : fitmodel
317 ; mutable trimmargins : trimmargins
318 ; mutable trimfuzz : irect
319 ; mutable memlimit : memsize
320 ; mutable texcount : texcount
321 ; mutable sliceheight : sliceheight
322 ; mutable thumbw : width
323 ; mutable jumpback : bool
324 ; mutable bgcolor : (float * float * float)
325 ; mutable bedefault : bool
326 ; mutable scrollbarinpm : bool
327 ; mutable tilew : int
328 ; mutable tileh : int
329 ; mutable mustoresize : memsize
330 ; mutable checkers : bool
331 ; mutable aalevel : int
332 ; mutable urilauncher : string
333 ; mutable pathlauncher : string
334 ; mutable colorspace : colorspace
335 ; mutable invert : bool
336 ; mutable colorscale : float
337 ; mutable redirectstderr : bool
338 ; mutable ghyllscroll : (int * int * int) option
339 ; mutable columns : columns
340 ; mutable beyecolumns : columncount option
341 ; mutable selcmd : string
342 ; mutable updatecurs : bool
343 ; mutable keyhashes : (string * keyhash) list
344 ; mutable hfsize : int
345 ; mutable pgscale : float
346 ; mutable usepbo : bool
347 ; mutable wheelbypage : bool
348 ; mutable stcmd : string
350 and columns =
351 | Csingle of singlecolumn
352 | Cmulti of multicolumns
353 | Csplit of splitcolumns
356 type anchor = pageno * top * dtop;;
358 type outline = string * int * anchor;;
360 type rect = float * float * float * float * float * float * float * float;;
362 type tile = opaque * pixmapsize * elapsed
363 and elapsed = float;;
364 type pagemapkey = pageno * gen;;
365 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
366 and row = int
367 and col = int;;
369 let emptyanchor = (0, 0.0, 0.0);;
371 type infochange = | Memused | Docinfo | Pdim;;
373 class type uioh = object
374 method display : unit
375 method key : int -> int -> uioh
376 method button : int -> bool -> int -> int -> int -> uioh
377 method motion : int -> int -> uioh
378 method pmotion : int -> int -> uioh
379 method infochanged : infochange -> unit
380 method scrollpw : (int * float * float)
381 method scrollph : (int * float * float)
382 method modehash : keyhash
383 method eformsgs : bool
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 method eformsgs = false
426 end;;
428 type state =
429 { mutable sr : Unix.file_descr
430 ; mutable sw : Unix.file_descr
431 ; mutable wsfd : Unix.file_descr
432 ; mutable errfd : Unix.file_descr option
433 ; mutable stderr : Unix.file_descr
434 ; mutable errmsgs : Buffer.t
435 ; mutable newerrmsgs : bool
436 ; mutable w : int
437 ; mutable x : int
438 ; mutable y : int
439 ; mutable scrollw : int
440 ; mutable hscrollh : int
441 ; mutable anchor : anchor
442 ; mutable ranchors : (string * string * anchor) list
443 ; mutable maxy : int
444 ; mutable layout : page list
445 ; pagemap : (pagemapkey, opaque) Hashtbl.t
446 ; tilemap : (tilemapkey, tile) Hashtbl.t
447 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
448 ; mutable pdims : (pageno * width * height * leftx) list
449 ; mutable pagecount : int
450 ; mutable currently : currently
451 ; mutable mstate : mstate
452 ; mutable searchpattern : string
453 ; mutable rects : (pageno * recttype * rect) list
454 ; mutable rects1 : (pageno * recttype * rect) list
455 ; mutable text : string
456 ; mutable winstate : Wsi.winstate list
457 ; mutable mode : mode
458 ; mutable uioh : uioh
459 ; mutable outlines : outline array
460 ; mutable bookmarks : outline list
461 ; mutable path : string
462 ; mutable password : string
463 ; mutable nameddest : string
464 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
465 ; mutable memused : memsize
466 ; mutable gen : gen
467 ; mutable throttle : (page list * int * float) option
468 ; mutable autoscroll : int option
469 ; mutable ghyll : (int option -> unit)
470 ; mutable help : helpitem array
471 ; mutable docinfo : (int * string) list
472 ; mutable texid : GlTex.texture_id option
473 ; hists : hists
474 ; mutable prevzoom : float
475 ; mutable progress : float
476 ; mutable redisplay : bool
477 ; mutable mpos : mpos
478 ; mutable keystate : keystate
479 ; mutable glinks : bool
480 ; mutable prevcolumns : (columns * float) option
481 ; mutable winw : int
482 ; mutable winh : int
483 ; mutable reprf : (unit -> unit)
485 and hists =
486 { pat : string circbuf
487 ; pag : string circbuf
488 ; nav : anchor circbuf
489 ; sel : string circbuf
493 let defconf =
494 { scrollbw = 7
495 ; scrollh = 12
496 ; icase = true
497 ; preload = true
498 ; pagebias = 0
499 ; verbose = false
500 ; debug = false
501 ; scrollstep = 24
502 ; hscrollstep = 24
503 ; maxhfit = true
504 ; crophack = false
505 ; autoscrollstep = 2
506 ; maxwait = None
507 ; hlinks = false
508 ; underinfo = false
509 ; interpagespace = 2
510 ; zoom = 1.0
511 ; presentation = false
512 ; angle = 0
513 ; cwinw = 900
514 ; cwinh = 900
515 ; savebmarks = true
516 ; fitmodel = FitProportional
517 ; trimmargins = false
518 ; trimfuzz = (0,0,0,0)
519 ; memlimit = 32 lsl 20
520 ; texcount = 256
521 ; sliceheight = 24
522 ; thumbw = 76
523 ; jumpback = true
524 ; bgcolor = (0.5, 0.5, 0.5)
525 ; bedefault = false
526 ; scrollbarinpm = true
527 ; tilew = 2048
528 ; tileh = 2048
529 ; mustoresize = 256 lsl 20
530 ; checkers = true
531 ; aalevel = 8
532 ; urilauncher =
533 (match platform with
534 | Plinux | Pfreebsd | Pdragonflybsd
535 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
536 | Posx -> "open \"%s\""
537 | Pcygwin -> "cygstart \"%s\""
538 | Punknown -> "echo %s")
539 ; pathlauncher = "lp \"%s\""
540 ; selcmd =
541 (match platform with
542 | Plinux | Pfreebsd | Pdragonflybsd
543 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
544 | Posx -> "pbcopy"
545 | Pcygwin -> "wsel"
546 | Punknown -> "cat")
547 ; colorspace = Rgb
548 ; invert = false
549 ; colorscale = 1.0
550 ; redirectstderr = false
551 ; ghyllscroll = None
552 ; columns = Csingle [||]
553 ; beyecolumns = None
554 ; updatecurs = false
555 ; hfsize = 12
556 ; pgscale = 1.0
557 ; usepbo = false
558 ; wheelbypage = false
559 ; stcmd = "echo SyncTex"
560 ; keyhashes =
561 let mk n = (n, Hashtbl.create 1) in
562 [ mk "global"
563 ; mk "info"
564 ; mk "help"
565 ; mk "outline"
566 ; mk "listview"
567 ; mk "birdseye"
568 ; mk "textentry"
569 ; mk "links"
570 ; mk "view"
575 let wtmode = ref false;;
577 let findkeyhash c name =
578 try List.assoc name c.keyhashes
579 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
582 let conf = { defconf with angle = defconf.angle };;
584 let pgscale h = truncate (float h *. conf.pgscale);;
586 type fontstate =
587 { mutable fontsize : int
588 ; mutable wwidth : float
589 ; mutable maxrows : int
593 let fstate =
594 { fontsize = 14
595 ; wwidth = nan
596 ; maxrows = -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 (exntos 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 = "", [];;
663 let noreprf () = ();;
665 let state =
666 { sr = Unix.stdin
667 ; sw = Unix.stdin
668 ; wsfd = Unix.stdin
669 ; errfd = None
670 ; stderr = Unix.stderr
671 ; errmsgs = Buffer.create 0
672 ; newerrmsgs = false
673 ; x = 0
674 ; y = 0
675 ; w = 0
676 ; scrollw = 0
677 ; hscrollh = 0
678 ; anchor = emptyanchor
679 ; ranchors = []
680 ; layout = []
681 ; maxy = max_int
682 ; tilelru = Queue.create ()
683 ; pagemap = Hashtbl.create 10
684 ; tilemap = Hashtbl.create 10
685 ; pdims = []
686 ; pagecount = 0
687 ; currently = Idle
688 ; mstate = Mnone
689 ; rects = []
690 ; rects1 = []
691 ; text = ""
692 ; mode = View
693 ; winstate = []
694 ; searchpattern = ""
695 ; outlines = [||]
696 ; bookmarks = []
697 ; path = ""
698 ; password = ""
699 ; nameddest = ""
700 ; geomcmds = firstgeomcmds
701 ; hists =
702 { nav = cbnew 10 emptyanchor
703 ; pat = cbnew 10 ""
704 ; pag = cbnew 10 ""
705 ; sel = cbnew 10 ""
707 ; memused = 0
708 ; gen = 0
709 ; throttle = None
710 ; autoscroll = None
711 ; ghyll = noghyll
712 ; help = makehelp ()
713 ; docinfo = []
714 ; texid = None
715 ; prevzoom = 1.0
716 ; progress = -1.0
717 ; uioh = nouioh
718 ; redisplay = true
719 ; mpos = (-1, -1)
720 ; keystate = KSnone
721 ; glinks = false
722 ; prevcolumns = None
723 ; winw = -1
724 ; winh = -1
725 ; reprf = noreprf
729 let setfontsize n =
730 fstate.fontsize <- n;
731 fstate.wwidth <- measurestr fstate.fontsize "w";
732 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
735 let vlog fmt =
736 if conf.verbose
737 then
738 Printf.kprintf prerr_endline fmt
739 else
740 Printf.kprintf ignore fmt
743 let launchpath () =
744 if String.length conf.pathlauncher = 0
745 then print_endline state.path
746 else (
747 let re = Str.regexp "%s" in
748 let command = Str.global_replace re state.path conf.pathlauncher in
749 try popen command []
750 with exn ->
751 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
752 flush stderr;
756 module Ne = struct
757 type 'a t = | Res of 'a | Exn of exn;;
759 let pipe () =
760 try Res (Unix.pipe ())
761 with exn -> Exn exn
764 let clo fd f =
765 try tempfailureretry Unix.close fd
766 with exn -> f (exntos exn)
769 let dup fd =
770 try Res (tempfailureretry Unix.dup fd)
771 with exn -> Exn exn
774 let dup2 fd1 fd2 =
775 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
776 with exn -> Exn exn
778 end;;
780 let redirectstderr () =
781 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
782 if conf.redirectstderr
783 then
784 match Ne.pipe () with
785 | Ne.Exn exn ->
786 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
788 | Ne.Res (r, w) ->
789 begin match Ne.dup Unix.stderr with
790 | Ne.Exn exn ->
791 dolog "failed to dup stderr: %s" (exntos exn);
792 Ne.clo r (clofail "pipe/r");
793 Ne.clo w (clofail "pipe/w");
795 | Ne.Res dupstderr ->
796 begin match Ne.dup2 w Unix.stderr with
797 | Ne.Exn exn ->
798 dolog "failed to dup2 to stderr: %s" (exntos exn);
799 Ne.clo dupstderr (clofail "stderr duplicate");
800 Ne.clo r (clofail "redir pipe/r");
801 Ne.clo w (clofail "redir pipe/w");
803 | Ne.Res () ->
804 state.stderr <- dupstderr;
805 state.errfd <- Some r;
806 end;
808 else (
809 state.newerrmsgs <- false;
810 begin match state.errfd with
811 | Some fd ->
812 begin match Ne.dup2 state.stderr Unix.stderr with
813 | Ne.Exn exn ->
814 dolog "failed to dup2 original stderr: %s" (exntos exn)
815 | Ne.Res () ->
816 Ne.clo fd (clofail "dup of stderr");
817 state.errfd <- None;
818 end;
819 | None -> ()
820 end;
821 prerr_string (Buffer.contents state.errmsgs);
822 flush stderr;
823 Buffer.clear state.errmsgs;
827 module G =
828 struct
829 let postRedisplay who =
830 if conf.verbose
831 then prerr_endline ("redisplay for " ^ who);
832 state.redisplay <- true;
834 end;;
836 let getopaque pageno =
837 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
838 with Not_found -> None
841 let putopaque pageno opaque =
842 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
845 let pagetranslatepoint l x y =
846 let dy = y - l.pagedispy in
847 let y = dy + l.pagey in
848 let dx = x - l.pagedispx in
849 let x = dx + l.pagex in
850 (x, y);
853 let onppundermouse g x y d =
854 let rec f = function
855 | l :: rest ->
856 begin match getopaque l.pageno with
857 | Some opaque ->
858 let x0 = l.pagedispx in
859 let x1 = x0 + l.pagevw in
860 let y0 = l.pagedispy in
861 let y1 = y0 + l.pagevh in
862 if y >= y0 && y <= y1 && x >= x0 && x <= x1
863 then
864 let px, py = pagetranslatepoint l x y in
865 match g opaque l px py with
866 | Some res -> res
867 | None -> f rest
868 else f rest
869 | _ ->
870 f rest
872 | [] -> d
874 f state.layout
877 let getunder x y =
878 let g opaque _ px py =
879 match whatsunder opaque px py with
880 | Unone -> None
881 | under -> Some under
883 onppundermouse g x y Unone
886 let unproject x y =
887 let g opaque l x y =
888 match unproject opaque x y with
889 | Some (x, y) -> Some (Some (l.pageno, x, y))
890 | None -> None
892 onppundermouse g x y None;
895 let showtext c s =
896 state.text <- Printf.sprintf "%c%s" c s;
897 G.postRedisplay "showtext";
900 let undertext = function
901 | Unone -> "none"
902 | Ulinkuri s -> s
903 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
904 | Utext s -> "font: " ^ s
905 | Uunexpected s -> "unexpected: " ^ s
906 | Ulaunch s -> "launch: " ^ s
907 | Unamed s -> "named: " ^ s
908 | Uremote (filename, pageno) ->
909 Printf.sprintf "%s: page %d" filename (pageno+1)
912 let updateunder x y =
913 match getunder x y with
914 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
915 | Ulinkuri uri ->
916 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
917 Wsi.setcursor Wsi.CURSOR_INFO
918 | Ulinkgoto (pageno, _) ->
919 if conf.underinfo
920 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
921 Wsi.setcursor Wsi.CURSOR_INFO
922 | Utext s ->
923 if conf.underinfo then showtext 'f' ("ont: " ^ s);
924 Wsi.setcursor Wsi.CURSOR_TEXT
925 | Uunexpected s ->
926 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
927 Wsi.setcursor Wsi.CURSOR_INHERIT
928 | Ulaunch s ->
929 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
930 Wsi.setcursor Wsi.CURSOR_INHERIT
931 | Unamed s ->
932 if conf.underinfo then showtext 'n' ("amed: " ^ s);
933 Wsi.setcursor Wsi.CURSOR_INHERIT
934 | Uremote (filename, pageno) ->
935 if conf.underinfo then showtext 'r'
936 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
937 Wsi.setcursor Wsi.CURSOR_INFO
940 let showlinktype under =
941 if conf.underinfo
942 then
943 match under with
944 | Unone -> ()
945 | under ->
946 let s = undertext under in
947 showtext ' ' s
950 let addchar s c =
951 let b = Buffer.create (String.length s + 1) in
952 Buffer.add_string b s;
953 Buffer.add_char b c;
954 Buffer.contents b;
957 let colorspace_of_string s =
958 match String.lowercase s with
959 | "rgb" -> Rgb
960 | "bgr" -> Bgr
961 | "gray" -> Gray
962 | _ -> failwith "invalid colorspace"
965 let int_of_colorspace = function
966 | Rgb -> 0
967 | Bgr -> 1
968 | Gray -> 2
971 let colorspace_of_int = function
972 | 0 -> Rgb
973 | 1 -> Bgr
974 | 2 -> Gray
975 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
978 let colorspace_to_string = function
979 | Rgb -> "rgb"
980 | Bgr -> "bgr"
981 | Gray -> "gray"
984 let fitmodel_of_string s =
985 match String.lowercase s with
986 | "width" -> FitWidth
987 | "proportional" -> FitProportional
988 | "page" -> FitPage
989 | _ -> failwith "invalid fit model"
992 let int_of_fitmodel = function
993 | FitWidth -> 0
994 | FitProportional -> 1
995 | FitPage -> 2
998 let fitmodel_of_int = function
999 | 0 -> FitWidth
1000 | 1 -> FitProportional
1001 | 2 -> FitPage
1002 | n -> failwith ("invalid fit model index " ^ string_of_int n)
1005 let fitmodel_to_string = function
1006 | FitWidth -> "width"
1007 | FitProportional -> "proportional"
1008 | FitPage -> "page"
1011 let intentry_with_suffix text key =
1012 let c =
1013 if key >= 32 && key < 127
1014 then Char.chr key
1015 else '\000'
1017 match Char.lowercase c with
1018 | '0' .. '9' ->
1019 let text = addchar text c in
1020 TEcont text
1022 | 'k' | 'm' | 'g' ->
1023 let text = addchar text c in
1024 TEcont text
1026 | _ ->
1027 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1028 TEcont text
1031 let multicolumns_to_string (n, a, b) =
1032 if a = 0 && b = 0
1033 then Printf.sprintf "%d" n
1034 else Printf.sprintf "%d,%d,%d" n a b;
1037 let multicolumns_of_string s =
1039 (int_of_string s, 0, 0)
1040 with _ ->
1041 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1042 if a > 1 || b > 1
1043 then failwith "subtly broken"; (n, a, b)
1047 let readcmd fd =
1048 let s = "xxxx" in
1049 let n = tempfailureretry (Unix.read fd s 0) 4 in
1050 if n != 4 then failwith "incomplete read(len)";
1051 let len = 0
1052 lor (Char.code s.[0] lsl 24)
1053 lor (Char.code s.[1] lsl 16)
1054 lor (Char.code s.[2] lsl 8)
1055 lor (Char.code s.[3] lsl 0)
1057 let s = String.create len in
1058 let n = tempfailureretry (Unix.read fd s 0) len in
1059 if n != len then failwith "incomplete read(data)";
1063 let btod b = if b then 1 else 0;;
1065 let wcmd fmt =
1066 let b = Buffer.create 16 in
1067 Buffer.add_string b "llll";
1068 Printf.kbprintf
1069 (fun b ->
1070 let s = Buffer.contents b in
1071 let n = String.length s in
1072 let len = n - 4 in
1073 (* dolog "wcmd %S" (String.sub s 4 len); *)
1074 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1075 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1076 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1077 s.[3] <- Char.chr (len land 0xff);
1078 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1079 if n' != n then failwith "write failed";
1080 ) b fmt;
1083 let calcips h =
1084 let d = state.winh - h in
1085 max conf.interpagespace ((d + 1) / 2)
1088 let rowyh (c, coverA, coverB) b n =
1089 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1090 then
1091 let _, _, vy, (_, _, h, _) = b.(n) in
1092 (vy, h)
1093 else
1094 let n' = n - coverA in
1095 let d = n' mod c in
1096 let s = n - d in
1097 let e = min state.pagecount (s + c) in
1098 let rec find m miny maxh = if m = e then miny, maxh else
1099 let _, _, y, (_, _, h, _) = b.(m) in
1100 let miny = min miny y in
1101 let maxh = max maxh h in
1102 find (m+1) miny maxh
1103 in find s max_int 0
1106 let calcheight () =
1107 match conf.columns with
1108 | Cmulti ((_, _, _) as cl, b) ->
1109 if Array.length b > 0
1110 then
1111 let y, h = rowyh cl b (Array.length b - 1) in
1112 y + h + (if conf.presentation then calcips h else 0)
1113 else 0
1114 | Csingle b ->
1115 if Array.length b > 0
1116 then
1117 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1118 y + h + (if conf.presentation then calcips h else 0)
1119 else 0
1120 | Csplit (_, b) ->
1121 if Array.length b > 0
1122 then
1123 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1124 y + h
1125 else 0
1128 let getpageyh pageno =
1129 let pageno = bound pageno 0 (state.pagecount-1) in
1130 match conf.columns with
1131 | Csingle b ->
1132 if Array.length b = 0
1133 then 0, 0
1134 else
1135 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1136 let y =
1137 if conf.presentation
1138 then y - calcips h
1139 else y
1141 y, h
1142 | Cmulti (cl, b) ->
1143 if Array.length b = 0
1144 then 0, 0
1145 else
1146 let y, h = rowyh cl b pageno in
1147 let y =
1148 if conf.presentation
1149 then y - calcips h
1150 else y
1152 y, h
1153 | Csplit (c, b) ->
1154 if Array.length b = 0
1155 then 0, 0
1156 else
1157 let n = pageno*c in
1158 let (_, _, y, (_, _, h, _)) = b.(n) in
1159 y, h
1162 let getpagedim pageno =
1163 let rec f ppdim l =
1164 match l with
1165 | (n, _, _, _) as pdim :: rest ->
1166 if n >= pageno
1167 then (if n = pageno then pdim else ppdim)
1168 else f pdim rest
1170 | [] -> ppdim
1172 f (-1, -1, -1, -1) state.pdims
1175 let getpagey pageno = fst (getpageyh pageno);;
1177 let nogeomcmds cmds =
1178 match cmds with
1179 | s, [] -> String.length s = 0
1180 | _ -> false
1183 let page_of_y y =
1184 let ((c, coverA, coverB) as cl), b =
1185 match conf.columns with
1186 | Csingle b -> (1, 0, 0), b
1187 | Cmulti (c, b) -> c, b
1188 | Csplit (_, b) -> (1, 0, 0), b
1190 if Array.length b = 0
1191 then -1
1192 else
1193 let rec bsearch nmin nmax =
1194 if nmin > nmax
1195 then bound nmin 0 (state.pagecount-1)
1196 else
1197 let n = (nmax + nmin) / 2 in
1198 let vy, h = rowyh cl b n in
1199 let y0, y1 =
1200 if conf.presentation
1201 then
1202 let ips = calcips h in
1203 let y0 = vy - ips in
1204 let y1 = vy + h + ips in
1205 y0, y1
1206 else (
1207 if n = 0
1208 then 0, vy + h + conf.interpagespace
1209 else
1210 let y0 = vy - conf.interpagespace in
1211 y0, y0 + h + conf.interpagespace
1214 if y >= y0 && y < y1
1215 then (
1216 if c = 1
1217 then n
1218 else (
1219 if n > coverA
1220 then
1221 if n < state.pagecount - coverB
1222 then ((n-coverA)/c)*c + coverA
1223 else n
1224 else n
1227 else (
1228 if y > y0
1229 then bsearch (n+1) nmax
1230 else bsearch nmin (n-1)
1233 let r = bsearch 0 (state.pagecount-1) in
1237 let layoutN ((columns, coverA, coverB), b) y sh =
1238 let sh = sh - state.hscrollh in
1239 let rec fold accu n =
1240 if n = Array.length b
1241 then accu
1242 else
1243 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1244 if (vy - y) > sh &&
1245 (n = coverA - 1
1246 || n = state.pagecount - coverB
1247 || (n - coverA) mod columns = columns - 1)
1248 then accu
1249 else
1250 let accu =
1251 if vy + h > y
1252 then
1253 let pagey = max 0 (y - vy) in
1254 let pagedispy = if pagey > 0 then 0 else vy - y in
1255 let pagedispx, pagex =
1256 let pdx =
1257 if n = coverA - 1 || n = state.pagecount - coverB
1258 then state.x + (state.winw - state.scrollw - w) / 2
1259 else dx + xoff + state.x
1261 if pdx < 0
1262 then 0, -pdx
1263 else pdx, 0
1265 let pagevw =
1266 let vw = state.winw - state.scrollw - pagedispx in
1267 let pw = w - pagex in
1268 min vw pw
1270 let pagevh = min (h - pagey) (sh - pagedispy) in
1271 if pagevw > 0 && pagevh > 0
1272 then
1273 let e =
1274 { pageno = n
1275 ; pagedimno = pdimno
1276 ; pagew = w
1277 ; pageh = h
1278 ; pagex = pagex
1279 ; pagey = pagey
1280 ; pagevw = pagevw
1281 ; pagevh = pagevh
1282 ; pagedispx = pagedispx
1283 ; pagedispy = pagedispy
1284 ; pagecol = 0
1287 e :: accu
1288 else
1289 accu
1290 else
1291 accu
1293 fold accu (n+1)
1295 List.rev (fold [] (page_of_y y));
1298 let layoutS (columns, b) y sh =
1299 let sh = sh - state.hscrollh in
1300 let rec fold accu n =
1301 if n = Array.length b
1302 then accu
1303 else
1304 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1305 if (vy - y) > sh
1306 then accu
1307 else
1308 let accu =
1309 if vy + pageh > y
1310 then
1311 let x = xoff + state.x in
1312 let pagey = max 0 (y - vy) in
1313 let pagedispy = if pagey > 0 then 0 else vy - y in
1314 let pagedispx, pagex =
1315 if px = 0
1316 then (
1317 if x < 0
1318 then 0, -x
1319 else x, 0
1321 else (
1322 let px = px - x in
1323 if px < 0
1324 then -px, 0
1325 else 0, px
1328 let pagecolw = pagew/columns in
1329 let pagedispx =
1330 if pagecolw < state.winw
1331 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1332 else pagedispx
1334 let pagevw =
1335 let vw = state.winw - pagedispx - state.scrollw in
1336 let pw = pagew - pagex in
1337 min vw pw
1339 let pagevw = min pagevw pagecolw in
1340 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1341 if pagevw > 0 && pagevh > 0
1342 then
1343 let e =
1344 { pageno = n/columns
1345 ; pagedimno = pdimno
1346 ; pagew = pagew
1347 ; pageh = pageh
1348 ; pagex = pagex
1349 ; pagey = pagey
1350 ; pagevw = pagevw
1351 ; pagevh = pagevh
1352 ; pagedispx = pagedispx
1353 ; pagedispy = pagedispy
1354 ; pagecol = n mod columns
1357 e :: accu
1358 else
1359 accu
1360 else
1361 accu
1363 fold accu (n+1)
1365 List.rev (fold [] 0)
1368 let layout y sh =
1369 if nogeomcmds state.geomcmds
1370 then
1371 match conf.columns with
1372 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1373 | Cmulti c -> layoutN c y sh
1374 | Csplit s -> layoutS s y sh
1375 else []
1378 let clamp incr =
1379 let y = state.y + incr in
1380 let y = max 0 y in
1381 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1385 let itertiles l f =
1386 let tilex = l.pagex mod conf.tilew in
1387 let tiley = l.pagey mod conf.tileh in
1389 let col = l.pagex / conf.tilew in
1390 let row = l.pagey / conf.tileh in
1392 let rec rowloop row y0 dispy h =
1393 if h = 0
1394 then ()
1395 else (
1396 let dh = conf.tileh - y0 in
1397 let dh = min h dh in
1398 let rec colloop col x0 dispx w =
1399 if w = 0
1400 then ()
1401 else (
1402 let dw = conf.tilew - x0 in
1403 let dw = min w dw in
1405 f col row dispx dispy x0 y0 dw dh;
1406 colloop (col+1) 0 (dispx+dw) (w-dw)
1409 colloop col tilex l.pagedispx l.pagevw;
1410 rowloop (row+1) 0 (dispy+dh) (h-dh)
1413 if l.pagevw > 0 && l.pagevh > 0
1414 then rowloop row tiley l.pagedispy l.pagevh;
1417 let gettileopaque l col row =
1418 let key =
1419 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1421 try Some (Hashtbl.find state.tilemap key)
1422 with Not_found -> None
1425 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1426 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1427 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1430 let drawtiles l color =
1431 GlDraw.color color;
1432 let f col row x y tilex tiley w h =
1433 match gettileopaque l col row with
1434 | Some (opaque, _, t) ->
1435 let params = x, y, w, h, tilex, tiley in
1436 if conf.invert
1437 then (
1438 Gl.enable `blend;
1439 GlFunc.blend_func `zero `one_minus_src_color;
1441 drawtile params opaque;
1442 if conf.invert
1443 then Gl.disable `blend;
1444 if conf.debug
1445 then (
1446 let s = Printf.sprintf
1447 "%d[%d,%d] %f sec"
1448 l.pageno col row t
1450 let w = measurestr fstate.fontsize s in
1451 GlMisc.push_attrib [`current];
1452 GlDraw.color (0.0, 0.0, 0.0);
1453 GlDraw.rect
1454 (float (x-2), float (y-2))
1455 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1456 GlDraw.color (1.0, 1.0, 1.0);
1457 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1458 GlMisc.pop_attrib ();
1461 | _ ->
1462 let w =
1463 let lw = state.winw - state.scrollw - x in
1464 min lw w
1465 and h =
1466 let lh = state.winh - y in
1467 min lh h
1469 begin match state.texid with
1470 | Some id ->
1471 Gl.enable `texture_2d;
1472 GlTex.bind_texture `texture_2d id;
1473 let x0 = float x
1474 and y0 = float y
1475 and x1 = float (x+w)
1476 and y1 = float (y+h) in
1478 let tw = float w /. 16.0
1479 and th = float h /. 16.0 in
1480 let tx0 = float tilex /. 16.0
1481 and ty0 = float tiley /. 16.0 in
1482 let tx1 = tx0 +. tw
1483 and ty1 = ty0 +. th in
1484 GlDraw.begins `quads;
1485 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1486 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1487 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1488 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1489 GlDraw.ends ();
1491 Gl.disable `texture_2d;
1492 | None ->
1493 GlDraw.color (1.0, 1.0, 1.0);
1494 GlDraw.rect
1495 (float x, float y)
1496 (float (x+w), float (y+h));
1497 end;
1498 if w > 128 && h > fstate.fontsize + 10
1499 then (
1500 GlDraw.color (0.0, 0.0, 0.0);
1501 let c, r =
1502 if conf.verbose
1503 then (col*conf.tilew, row*conf.tileh)
1504 else col, row
1506 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1508 GlDraw.color color;
1510 itertiles l f
1513 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1515 let tilevisible1 l x y =
1516 let ax0 = l.pagex
1517 and ax1 = l.pagex + l.pagevw
1518 and ay0 = l.pagey
1519 and ay1 = l.pagey + l.pagevh in
1521 let bx0 = x
1522 and by0 = y in
1523 let bx1 = min (bx0 + conf.tilew) l.pagew
1524 and by1 = min (by0 + conf.tileh) l.pageh in
1526 let rx0 = max ax0 bx0
1527 and ry0 = max ay0 by0
1528 and rx1 = min ax1 bx1
1529 and ry1 = min ay1 by1 in
1531 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1532 nonemptyintersection
1535 let tilevisible layout n x y =
1536 let rec findpageinlayout m = function
1537 | l :: rest when l.pageno = n ->
1538 tilevisible1 l x y || (
1539 match conf.columns with
1540 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1541 | _ -> false
1543 | _ :: rest -> findpageinlayout 0 rest
1544 | [] -> false
1546 findpageinlayout 0 layout;
1549 let tileready l x y =
1550 tilevisible1 l x y &&
1551 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1554 let tilepage n p layout =
1555 let rec loop = function
1556 | l :: rest ->
1557 if l.pageno = n
1558 then
1559 let f col row _ _ _ _ _ _ =
1560 if state.currently = Idle
1561 then
1562 match gettileopaque l col row with
1563 | Some _ -> ()
1564 | None ->
1565 let x = col*conf.tilew
1566 and y = row*conf.tileh in
1567 let w =
1568 let w = l.pagew - x in
1569 min w conf.tilew
1571 let h =
1572 let h = l.pageh - y in
1573 min h conf.tileh
1575 let pbo =
1576 if conf.usepbo
1577 then getpbo w h conf.colorspace
1578 else "0"
1580 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1581 state.currently <-
1582 Tiling (
1583 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1584 conf.tilew, conf.tileh
1587 itertiles l f;
1588 else
1589 loop rest
1591 | [] -> ()
1593 if nogeomcmds state.geomcmds
1594 then loop layout;
1597 let preloadlayout y =
1598 let y = if y < state.winh then 0 else y - state.winh in
1599 let h = state.winh*3 in
1600 layout y h;
1603 let load pages =
1604 let rec loop pages =
1605 if state.currently != Idle
1606 then ()
1607 else
1608 match pages with
1609 | l :: rest ->
1610 begin match getopaque l.pageno with
1611 | None ->
1612 wcmd "page %d %d" l.pageno l.pagedimno;
1613 state.currently <- Loading (l, state.gen);
1614 | Some opaque ->
1615 tilepage l.pageno opaque pages;
1616 loop rest
1617 end;
1618 | _ -> ()
1620 if nogeomcmds state.geomcmds
1621 then loop pages
1624 let preload pages =
1625 load pages;
1626 if conf.preload && state.currently = Idle
1627 then load (preloadlayout state.y);
1630 let layoutready layout =
1631 let rec fold all ls =
1632 all && match ls with
1633 | l :: rest ->
1634 let seen = ref false in
1635 let allvisible = ref true in
1636 let foo col row _ _ _ _ _ _ =
1637 seen := true;
1638 allvisible := !allvisible &&
1639 begin match gettileopaque l col row with
1640 | Some _ -> true
1641 | None -> false
1644 itertiles l foo;
1645 fold (!seen && !allvisible) rest
1646 | [] -> true
1648 let alltilesvisible = fold true layout in
1649 alltilesvisible;
1652 let gotoy y =
1653 let y = bound y 0 state.maxy in
1654 let y, layout, proceed =
1655 match conf.maxwait with
1656 | Some time when state.ghyll == noghyll ->
1657 begin match state.throttle with
1658 | None ->
1659 let layout = layout y state.winh in
1660 let ready = layoutready layout in
1661 if not ready
1662 then (
1663 load layout;
1664 state.throttle <- Some (layout, y, now ());
1666 else G.postRedisplay "gotoy showall (None)";
1667 y, layout, ready
1668 | Some (_, _, started) ->
1669 let dt = now () -. started in
1670 if dt > time
1671 then (
1672 state.throttle <- None;
1673 let layout = layout y state.winh in
1674 load layout;
1675 G.postRedisplay "maxwait";
1676 y, layout, true
1678 else -1, [], false
1681 | _ ->
1682 let layout = layout y state.winh in
1683 if not !wtmode || layoutready layout
1684 then G.postRedisplay "gotoy ready";
1685 y, layout, true
1687 if proceed
1688 then (
1689 state.y <- y;
1690 state.layout <- layout;
1691 begin match state.mode with
1692 | LinkNav (Ltexact (pageno, linkno)) ->
1693 let rec loop = function
1694 | [] ->
1695 state.mode <- LinkNav (Ltgendir 0)
1696 | l :: _ when l.pageno = pageno ->
1697 begin match getopaque pageno with
1698 | None ->
1699 state.mode <- LinkNav (Ltgendir 0)
1700 | Some opaque ->
1701 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1702 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1703 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1704 then state.mode <- LinkNav (Ltgendir 0)
1706 | _ :: rest -> loop rest
1708 loop layout
1709 | _ -> ()
1710 end;
1711 begin match state.mode with
1712 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1713 if not (pagevisible layout pageno)
1714 then (
1715 match state.layout with
1716 | [] -> ()
1717 | l :: _ ->
1718 state.mode <- Birdseye (
1719 conf, leftx, l.pageno, hooverpageno, anchor
1722 | LinkNav (Ltgendir dir as lt) ->
1723 let linknav =
1724 let rec loop = function
1725 | [] -> lt
1726 | l :: rest ->
1727 match getopaque l.pageno with
1728 | None -> loop rest
1729 | Some opaque ->
1730 let link =
1731 let ld =
1732 if dir = 0
1733 then LDfirstvisible (l.pagex, l.pagey, dir)
1734 else (
1735 if dir > 0 then LDfirst else LDlast
1738 findlink opaque ld
1740 match link with
1741 | Lnotfound -> loop rest
1742 | Lfound n ->
1743 showlinktype (getlink opaque n);
1744 Ltexact (l.pageno, n)
1746 loop state.layout
1748 state.mode <- LinkNav linknav
1749 | _ -> ()
1750 end;
1751 preload layout;
1753 state.ghyll <- noghyll;
1754 if conf.updatecurs
1755 then (
1756 let mx, my = state.mpos in
1757 updateunder mx my;
1761 let conttiling pageno opaque =
1762 tilepage pageno opaque
1763 (if conf.preload then preloadlayout state.y else state.layout)
1766 let gotoy_and_clear_text y =
1767 if not conf.verbose then state.text <- "";
1768 gotoy y;
1771 let getanchor1 l =
1772 let top =
1773 let coloff = l.pagecol * l.pageh in
1774 float (l.pagey + coloff) /. float l.pageh
1776 let dtop =
1777 if l.pagedispy = 0
1778 then
1780 else
1781 if conf.presentation
1782 then float l.pagedispy /. float (calcips l.pageh)
1783 else float l.pagedispy /. float conf.interpagespace
1785 (l.pageno, top, dtop)
1788 let getanchor () =
1789 match state.layout with
1790 | l :: _ -> getanchor1 l
1791 | [] ->
1792 let n = page_of_y state.y in
1793 if n = -1
1794 then state.anchor
1795 else
1796 let y, h = getpageyh n in
1797 let dy = y - state.y in
1798 let dtop =
1799 if conf.presentation
1800 then
1801 let ips = calcips h in
1802 float (dy + ips) /. float ips
1803 else
1804 float dy /. float conf.interpagespace
1806 (n, 0.0, dtop)
1809 let getanchory (n, top, dtop) =
1810 let y, h = getpageyh n in
1811 if conf.presentation
1812 then
1813 let ips = calcips h in
1814 y + truncate (top*.float h -. dtop*.float ips) + ips;
1815 else
1816 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1819 let gotoanchor anchor =
1820 gotoy (getanchory anchor);
1823 let addnav () =
1824 cbput state.hists.nav (getanchor ());
1827 let getnav dir =
1828 let anchor = cbgetc state.hists.nav dir in
1829 getanchory anchor;
1832 let gotoghyll y =
1833 let scroll f n a b =
1834 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1835 let snake f a b =
1836 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1837 if f < a
1838 then s (float f /. float a)
1839 else (
1840 if f > b
1841 then 1.0 -. s ((float (f-b) /. float (n-b)))
1842 else 1.0
1845 snake f a b
1846 and summa f n a b =
1847 (* courtesy:
1848 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1849 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1850 let iv1 = iv f in
1851 let ins = float a *. iv1
1852 and outs = float (n-b) *. iv1 in
1853 let ones = b - a in
1854 ins +. outs +. float ones
1856 let rec set (_N, _A, _B) y sy =
1857 let sum = summa 1.0 _N _A _B in
1858 let dy = float (y - sy) in
1859 state.ghyll <- (
1860 let rec gf n y1 o =
1861 if n >= _N
1862 then state.ghyll <- noghyll
1863 else
1864 let go n =
1865 let s = scroll n _N _A _B in
1866 let y1 = y1 +. ((s *. dy) /. sum) in
1867 gotoy_and_clear_text (truncate y1);
1868 state.ghyll <- gf (n+1) y1;
1870 match o with
1871 | None -> go n
1872 | Some y' -> set (_N/2, 1, 1) y' state.y
1874 gf 0 (float state.y)
1877 match conf.ghyllscroll with
1878 | None ->
1879 gotoy_and_clear_text y
1880 | Some nab ->
1881 if state.ghyll == noghyll
1882 then set nab y state.y
1883 else state.ghyll (Some y)
1886 let gotopage n top =
1887 let y, h = getpageyh n in
1888 let y = y + (truncate (top *. float h)) in
1889 gotoghyll y
1892 let gotopage1 n top =
1893 let y = getpagey n in
1894 let y = y + top in
1895 gotoghyll y
1898 let invalidate s f =
1899 state.layout <- [];
1900 state.pdims <- [];
1901 state.rects <- [];
1902 state.rects1 <- [];
1903 match state.geomcmds with
1904 | ps, [] when String.length ps = 0 ->
1905 f ();
1906 state.geomcmds <- s, [];
1908 | ps, [] ->
1909 state.geomcmds <- ps, [s, f];
1911 | ps, (s', _) :: rest when s' = s ->
1912 state.geomcmds <- ps, ((s, f) :: rest);
1914 | ps, cmds ->
1915 state.geomcmds <- ps, ((s, f) :: cmds);
1918 let flushpages () =
1919 Hashtbl.iter (fun _ opaque ->
1920 wcmd "freepage %s" opaque;
1921 ) state.pagemap;
1922 Hashtbl.clear state.pagemap;
1925 let flushtiles () =
1926 if not (Queue.is_empty state.tilelru)
1927 then (
1928 Queue.iter (fun (k, p, s) ->
1929 wcmd "freetile %s" p;
1930 state.memused <- state.memused - s;
1931 Hashtbl.remove state.tilemap k;
1932 ) state.tilelru;
1933 state.uioh#infochanged Memused;
1934 Queue.clear state.tilelru;
1936 load state.layout;
1939 let opendoc path password =
1940 state.path <- path;
1941 state.password <- password;
1942 state.gen <- state.gen + 1;
1943 state.docinfo <- [];
1945 flushpages ();
1946 setaalevel conf.aalevel;
1947 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1948 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
1949 invalidate "reqlayout"
1950 (fun () ->
1951 wcmd "reqlayout %d %d %f %s\000"
1952 conf.angle (int_of_fitmodel conf.fitmodel) conf.zoom state.nameddest;
1956 let reload () =
1957 state.anchor <- getanchor ();
1958 opendoc state.path state.password;
1961 let scalecolor c =
1962 let c = c *. conf.colorscale in
1963 (c, c, c);
1966 let scalecolor2 (r, g, b) =
1967 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1970 let docolumns = function
1971 | Csingle _ ->
1972 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1973 let rec loop pageno pdimno pdim y ph pdims =
1974 if pageno = state.pagecount
1975 then ()
1976 else
1977 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1978 match pdims with
1979 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1980 pdimno+1, pdim, rest
1981 | _ ->
1982 pdimno, pdim, pdims
1984 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
1985 let y = y +
1986 (if conf.presentation
1987 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1988 else (if pageno = 0 then 0 else conf.interpagespace)
1991 a.(pageno) <- (pdimno, x, y, pdim);
1992 loop (pageno+1) pdimno pdim (y + h) h pdims
1994 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1995 conf.columns <- Csingle a;
1997 | Cmulti ((columns, coverA, coverB), _) ->
1998 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1999 let rec loop pageno pdimno pdim x y rowh pdims =
2000 let rec fixrow m = if m = pageno then () else
2001 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2002 if h < rowh
2003 then (
2004 let y = y + (rowh - h) / 2 in
2005 a.(m) <- (pdimno, x, y, pdim);
2007 fixrow (m+1)
2009 if pageno = state.pagecount
2010 then fixrow (((pageno - 1) / columns) * columns)
2011 else
2012 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2013 match pdims with
2014 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2015 pdimno+1, pdim, rest
2016 | _ ->
2017 pdimno, pdim, pdims
2019 let x, y, rowh' =
2020 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2021 then (
2022 let x = (state.winw - state.scrollw - w) / 2 in
2023 let ips =
2024 if conf.presentation then calcips h else conf.interpagespace in
2025 x, y + ips + rowh, h
2027 else (
2028 if (pageno - coverA) mod columns = 0
2029 then (
2030 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2031 let y =
2032 if conf.presentation
2033 then
2034 let ips = calcips h in
2035 y + (if pageno = 0 then 0 else calcips rowh + ips)
2036 else
2037 y + (if pageno = 0 then 0 else conf.interpagespace)
2039 x, y + rowh, h
2041 else x, y, max rowh h
2044 let y =
2045 if pageno > 1 && (pageno - coverA) mod columns = 0
2046 then (
2047 let y =
2048 if pageno = columns && conf.presentation
2049 then (
2050 let ips = calcips rowh in
2051 for i = 0 to pred columns
2053 let (pdimno, x, y, pdim) = a.(i) in
2054 a.(i) <- (pdimno, x, y+ips, pdim)
2055 done;
2056 y+ips;
2058 else y
2060 fixrow (pageno - columns);
2063 else y
2065 a.(pageno) <- (pdimno, x, y, pdim);
2066 let x = x + w + xoff*2 + conf.interpagespace in
2067 loop (pageno+1) pdimno pdim x y rowh' pdims
2069 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2070 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2072 | Csplit (c, _) ->
2073 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2074 let rec loop pageno pdimno pdim y pdims =
2075 if pageno = state.pagecount
2076 then ()
2077 else
2078 let pdimno, ((_, w, h, _) as pdim), pdims =
2079 match pdims with
2080 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2081 pdimno+1, pdim, rest
2082 | _ ->
2083 pdimno, pdim, pdims
2085 let cw = w / c in
2086 let rec loop1 n x y =
2087 if n = c then y else (
2088 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2089 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2092 let y = loop1 0 0 y in
2093 loop (pageno+1) pdimno pdim y pdims
2095 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2096 conf.columns <- Csplit (c, a);
2099 let represent () =
2100 docolumns conf.columns;
2101 state.maxy <- calcheight ();
2102 state.hscrollh <-
2103 if state.x = 0 && state.w <= state.winw - state.scrollw
2104 then 0
2105 else state.scrollw
2107 if state.reprf == noreprf
2108 then (
2109 match state.mode with
2110 | Birdseye (_, _, pageno, _, _) ->
2111 let y, h = getpageyh pageno in
2112 let top = (state.winh - h) / 2 in
2113 gotoy (max 0 (y - top))
2114 | _ -> gotoanchor state.anchor
2116 else (
2117 state.reprf ();
2118 state.reprf <- noreprf;
2122 let reshape w h =
2123 GlDraw.viewport 0 0 w h;
2124 let firsttime = state.geomcmds == firstgeomcmds in
2125 if not firsttime && nogeomcmds state.geomcmds
2126 then state.anchor <- getanchor ();
2128 state.winw <- w;
2129 let w = truncate (float w *. conf.zoom) - state.scrollw in
2130 let w = max w 2 in
2131 state.winh <- h;
2132 setfontsize fstate.fontsize;
2133 GlMat.mode `modelview;
2134 GlMat.load_identity ();
2136 GlMat.mode `projection;
2137 GlMat.load_identity ();
2138 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2139 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2140 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2142 let relx =
2143 if conf.zoom <= 1.0
2144 then 0.0
2145 else float state.x /. float state.w
2147 invalidate "geometry"
2148 (fun () ->
2149 state.w <- w;
2150 if not firsttime
2151 then state.x <- truncate (relx *. float w);
2152 let w =
2153 match conf.columns with
2154 | Csingle _ -> w
2155 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2156 | Csplit (c, _) -> w * c
2158 wcmd "geometry %d %d %f" w (h - 2*conf.interpagespace) conf.zoom);
2161 let enttext () =
2162 let len = String.length state.text in
2163 let drawstring s =
2164 let hscrollh =
2165 match state.mode with
2166 | Textentry _
2167 | View ->
2168 let h, _, _ = state.uioh#scrollpw in
2170 | _ -> 0
2172 let rect x w =
2173 GlDraw.rect
2174 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2175 (x+.w, float (state.winh - hscrollh))
2178 let w = float (state.winw - state.scrollw - 1) in
2179 if state.progress >= 0.0 && state.progress < 1.0
2180 then (
2181 GlDraw.color (0.3, 0.3, 0.3);
2182 let w1 = w *. state.progress in
2183 rect 0.0 w1;
2184 GlDraw.color (0.0, 0.0, 0.0);
2185 rect w1 (w-.w1)
2187 else (
2188 GlDraw.color (0.0, 0.0, 0.0);
2189 rect 0.0 w;
2192 GlDraw.color (1.0, 1.0, 1.0);
2193 drawstring fstate.fontsize
2194 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2196 let s =
2197 match state.mode with
2198 | Textentry ((prefix, text, _, _, _, _), _) ->
2199 let s =
2200 if len > 0
2201 then
2202 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2203 else
2204 Printf.sprintf "%s%s_" prefix text
2208 | _ -> state.text
2210 let s =
2211 if state.newerrmsgs
2212 then (
2213 if not (istextentry state.mode) && state.uioh#eformsgs
2214 then
2215 let s1 = "(press 'e' to review error messasges)" in
2216 if String.length s > 0 then s ^ " " ^ s1 else s1
2217 else s
2219 else s
2221 if String.length s > 0
2222 then drawstring s
2225 let gctiles () =
2226 let len = Queue.length state.tilelru in
2227 let layout = lazy (
2228 match state.throttle with
2229 | None ->
2230 if conf.preload
2231 then preloadlayout state.y
2232 else state.layout
2233 | Some (layout, _, _) ->
2234 layout
2235 ) in
2236 let rec loop qpos =
2237 if state.memused <= conf.memlimit
2238 then ()
2239 else (
2240 if qpos < len
2241 then
2242 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2243 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2244 let (_, pw, ph, _) = getpagedim n in
2246 gen = state.gen
2247 && colorspace = conf.colorspace
2248 && angle = conf.angle
2249 && pagew = pw
2250 && pageh = ph
2251 && (
2252 let x = col*conf.tilew
2253 and y = row*conf.tileh in
2254 tilevisible (Lazy.force_val layout) n x y
2256 then Queue.push lruitem state.tilelru
2257 else (
2258 freepbo p;
2259 wcmd "freetile %s" p;
2260 state.memused <- state.memused - s;
2261 state.uioh#infochanged Memused;
2262 Hashtbl.remove state.tilemap k;
2264 loop (qpos+1)
2267 loop 0
2270 let logcurrently = function
2271 | Idle -> dolog "Idle"
2272 | Loading (l, gen) ->
2273 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2274 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2275 dolog
2276 "Tiling %d[%d,%d] page=%s cs=%s angle"
2277 l.pageno col row pageopaque
2278 (colorspace_to_string colorspace)
2280 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2281 angle gen conf.angle state.gen
2282 tilew tileh
2283 conf.tilew conf.tileh
2285 | Outlining _ ->
2286 dolog "outlining"
2289 let splitatspace =
2290 let r = Str.regexp " " in
2291 fun s -> Str.bounded_split r s 2;
2294 let onpagerect pageno f =
2295 let b =
2296 match conf.columns with
2297 | Cmulti (_, b) -> b
2298 | Csingle b -> b
2299 | Csplit (_, b) -> b
2301 if pageno >= 0 && pageno < Array.length b
2302 then
2303 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2304 let r = getpdimrect pdimno in
2305 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2308 let gotopagexy1 pageno x y =
2309 onpagerect pageno (fun w h ->
2310 let top = y /. h in
2311 let _,w1,_,leftx = getpagedim pageno in
2312 let wh = state.winh - state.hscrollh in
2313 let sw = float w1 /. w in
2314 let x = sw *. x in
2315 let x = leftx + state.x + truncate x in
2316 let sx =
2317 if x < 0 || x >= state.winw - state.scrollw
2318 then state.x - x
2319 else state.x
2321 let py, h = getpageyh pageno in
2322 let pdy = truncate (top *. float h) in
2323 let y' = py + pdy in
2324 let dy = y' - state.y in
2325 let sy =
2326 if x != state.x || not (dy > 0 && dy < wh)
2327 then (
2328 if conf.presentation
2329 then
2330 if abs (py - y') > wh
2331 then y'
2332 else py
2333 else y';
2335 else state.y
2337 if state.x != sx || state.y != sy
2338 then (
2339 let x, y =
2340 if !wtmode
2341 then (
2342 let ww = state.winw - state.scrollw in
2343 let qx = sx / ww
2344 and qy = pdy / wh in
2345 let x = qx * ww
2346 and y = py + qy * wh in
2347 let x = if -x + ww > w1 then -(w1-ww) else x
2348 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2349 let y =
2350 if conf.presentation
2351 then
2352 if abs (py - y') > wh
2353 then y'
2354 else py
2355 else y';
2357 (x, y)
2359 else (sx, sy)
2361 state.x <- x;
2362 state.hscrollh <-
2363 if x = 0 && state.w <= state.winw - state.scrollw
2364 then 0
2365 else state.scrollw
2367 gotoy_and_clear_text y;
2369 else gotoy_and_clear_text state.y;
2373 let gotopagexy pageno x y =
2374 match state.mode with
2375 | Birdseye _ -> gotopage pageno 0.0
2376 | _ -> gotopagexy1 pageno x y
2379 let act cmds =
2380 (* dolog "%S" cmds; *)
2381 let cl = splitatspace cmds in
2382 let scan s fmt f =
2383 try Scanf.sscanf s fmt f
2384 with exn ->
2385 dolog "error processing '%S': %s" cmds (exntos exn);
2386 exit 1
2388 match cl with
2389 | "clear" :: [] ->
2390 state.uioh#infochanged Pdim;
2391 state.pdims <- [];
2393 | "clearrects" :: [] ->
2394 state.rects <- state.rects1;
2395 G.postRedisplay "clearrects";
2397 | "continue" :: args :: [] ->
2398 let n = scan args "%u" (fun n -> n) in
2399 state.pagecount <- n;
2400 begin match state.currently with
2401 | Outlining l ->
2402 state.currently <- Idle;
2403 state.outlines <- Array.of_list (List.rev l)
2404 | _ -> ()
2405 end;
2407 let cur, cmds = state.geomcmds in
2408 if String.length cur = 0
2409 then failwith "umpossible";
2411 begin match List.rev cmds with
2412 | [] ->
2413 state.geomcmds <- "", [];
2414 represent ();
2415 | (s, f) :: rest ->
2416 f ();
2417 state.geomcmds <- s, List.rev rest;
2418 end;
2419 if conf.maxwait = None && not !wtmode
2420 then G.postRedisplay "continue";
2422 | "title" :: args :: [] ->
2423 Wsi.settitle args
2425 | "msg" :: args :: [] ->
2426 showtext ' ' args
2428 | "vmsg" :: args :: [] ->
2429 if conf.verbose
2430 then showtext ' ' args
2432 | "emsg" :: args :: [] ->
2433 Buffer.add_string state.errmsgs args;
2434 state.newerrmsgs <- true;
2435 G.postRedisplay "error message"
2437 | "progress" :: args :: [] ->
2438 let progress, text =
2439 scan args "%f %n"
2440 (fun f pos ->
2441 f, String.sub args pos (String.length args - pos))
2443 state.text <- text;
2444 state.progress <- progress;
2445 G.postRedisplay "progress"
2447 | "firstmatch" :: args :: [] ->
2448 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2449 scan args "%u %d %f %f %f %f %f %f %f %f"
2450 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2451 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2453 let y = (getpagey pageno) + truncate y0 in
2454 addnav ();
2455 gotoy y;
2456 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2458 | "match" :: args :: [] ->
2459 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2460 scan args "%u %d %f %f %f %f %f %f %f %f"
2461 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2462 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2464 state.rects1 <-
2465 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2467 | "page" :: args :: [] ->
2468 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2469 begin match state.currently with
2470 | Loading (l, gen) ->
2471 vlog "page %d took %f sec" l.pageno t;
2472 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2473 begin match state.throttle with
2474 | None ->
2475 let preloadedpages =
2476 if conf.preload
2477 then preloadlayout state.y
2478 else state.layout
2480 let evict () =
2481 let set =
2482 List.fold_left (fun s l -> IntSet.add l.pageno s)
2483 IntSet.empty preloadedpages
2485 let evictedpages =
2486 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2487 if not (IntSet.mem pageno set)
2488 then (
2489 wcmd "freepage %s" opaque;
2490 key :: accu
2492 else accu
2493 ) state.pagemap []
2495 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2497 evict ();
2498 state.currently <- Idle;
2499 if gen = state.gen
2500 then (
2501 tilepage l.pageno pageopaque state.layout;
2502 load state.layout;
2503 load preloadedpages;
2504 if pagevisible state.layout l.pageno
2505 && layoutready state.layout
2506 then G.postRedisplay "page";
2509 | Some (layout, _, _) ->
2510 state.currently <- Idle;
2511 tilepage l.pageno pageopaque layout;
2512 load state.layout
2513 end;
2515 | _ ->
2516 dolog "Inconsistent loading state";
2517 logcurrently state.currently;
2518 exit 1
2521 | "tile" :: args :: [] ->
2522 let (x, y, opaque, size, t) =
2523 scan args "%u %u %s %u %f"
2524 (fun x y p size t -> (x, y, p, size, t))
2526 begin match state.currently with
2527 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2528 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2530 unmappbo opaque;
2531 if tilew != conf.tilew || tileh != conf.tileh
2532 then (
2533 wcmd "freetile %s" opaque;
2534 state.currently <- Idle;
2535 load state.layout;
2537 else (
2538 puttileopaque l col row gen cs angle opaque size t;
2539 state.memused <- state.memused + size;
2540 state.uioh#infochanged Memused;
2541 gctiles ();
2542 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2543 opaque, size) state.tilelru;
2545 let layout =
2546 match state.throttle with
2547 | None -> state.layout
2548 | Some (layout, _, _) -> layout
2551 state.currently <- Idle;
2552 if gen = state.gen
2553 && conf.colorspace = cs
2554 && conf.angle = angle
2555 && tilevisible layout l.pageno x y
2556 then conttiling l.pageno pageopaque;
2558 begin match state.throttle with
2559 | None ->
2560 preload state.layout;
2561 if gen = state.gen
2562 && conf.colorspace = cs
2563 && conf.angle = angle
2564 && tilevisible state.layout l.pageno x y
2565 && (not !wtmode || layoutready state.layout)
2566 then G.postRedisplay "tile nothrottle";
2568 | Some (layout, y, _) ->
2569 let ready = layoutready layout in
2570 if ready
2571 then (
2572 state.y <- y;
2573 state.layout <- layout;
2574 state.throttle <- None;
2575 G.postRedisplay "throttle";
2577 else load layout;
2578 end;
2581 | _ ->
2582 dolog "Inconsistent tiling state";
2583 logcurrently state.currently;
2584 exit 1
2587 | "pdim" :: args :: [] ->
2588 let pdim =
2589 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2591 state.uioh#infochanged Pdim;
2592 state.pdims <- pdim :: state.pdims
2594 | "o" :: args :: [] ->
2595 let (l, n, t, h, pos) =
2596 scan args "%u %u %d %u %n"
2597 (fun l n t h pos -> l, n, t, h, pos)
2599 let s = String.sub args pos (String.length args - pos) in
2600 let outline = (s, l, (n, float t /. float h, 0.0)) in
2601 begin match state.currently with
2602 | Outlining outlines ->
2603 state.currently <- Outlining (outline :: outlines)
2604 | Idle ->
2605 state.currently <- Outlining [outline]
2606 | currently ->
2607 dolog "invalid outlining state";
2608 logcurrently currently
2611 | "a" :: args :: [] ->
2612 let (n, l, t) =
2613 scan args "%u %d %d" (fun n l t -> n, l, t)
2615 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2617 | "info" :: args :: [] ->
2618 state.docinfo <- (1, args) :: state.docinfo
2620 | "infoend" :: [] ->
2621 state.uioh#infochanged Docinfo;
2622 state.docinfo <- List.rev state.docinfo
2624 | _ ->
2625 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2628 let onhist cb =
2629 let rc = cb.rc in
2630 let action = function
2631 | HCprev -> cbget cb ~-1
2632 | HCnext -> cbget cb 1
2633 | HCfirst -> cbget cb ~-(cb.rc)
2634 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2635 and cancel () = cb.rc <- rc
2636 in (action, cancel)
2639 let search pattern forward =
2640 if String.length pattern > 0
2641 then
2642 let pn, py =
2643 match state.layout with
2644 | [] -> 0, 0
2645 | l :: _ ->
2646 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2648 wcmd "search %d %d %d %d,%s\000"
2649 (btod conf.icase) pn py (btod forward) pattern;
2652 let intentry text key =
2653 let c =
2654 if key >= 32 && key < 127
2655 then Char.chr key
2656 else '\000'
2658 match c with
2659 | '0' .. '9' ->
2660 let text = addchar text c in
2661 TEcont text
2663 | _ ->
2664 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2665 TEcont text
2668 let linknentry text key =
2669 let c =
2670 if key >= 32 && key < 127
2671 then Char.chr key
2672 else '\000'
2674 match c with
2675 | 'a' .. 'z' ->
2676 let text = addchar text c in
2677 TEcont text
2679 | _ ->
2680 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2681 TEcont text
2684 let linkndone f s =
2685 if String.length s > 0
2686 then (
2687 let n =
2688 let l = String.length s in
2689 let rec loop pos n = if pos = l then n else
2690 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2691 loop (pos+1) (n*26 + m)
2692 in loop 0 0
2694 let rec loop n = function
2695 | [] -> ()
2696 | l :: rest ->
2697 match getopaque l.pageno with
2698 | None -> loop n rest
2699 | Some opaque ->
2700 let m = getlinkcount opaque in
2701 if n < m
2702 then (
2703 let under = getlink opaque n in
2704 f under
2706 else loop (n-m) rest
2708 loop n state.layout;
2712 let textentry text key =
2713 if key land 0xff00 = 0xff00
2714 then TEcont text
2715 else TEcont (text ^ toutf8 key)
2718 let reqlayout angle fitmodel =
2719 match state.throttle with
2720 | None ->
2721 if nogeomcmds state.geomcmds
2722 then state.anchor <- getanchor ();
2723 conf.angle <- angle mod 360;
2724 if conf.angle != 0
2725 then (
2726 match state.mode with
2727 | LinkNav _ -> state.mode <- View
2728 | _ -> ()
2730 conf.fitmodel <- fitmodel;
2731 invalidate "reqlayout"
2732 (fun () ->
2733 wcmd "reqlayout %d %d %f"
2734 conf.angle (int_of_fitmodel fitmodel) conf.zoom);
2735 | _ -> ()
2738 let settrim trimmargins trimfuzz =
2739 if nogeomcmds state.geomcmds
2740 then state.anchor <- getanchor ();
2741 conf.trimmargins <- trimmargins;
2742 conf.trimfuzz <- trimfuzz;
2743 let x0, y0, x1, y1 = trimfuzz in
2744 invalidate "settrim"
2745 (fun () ->
2746 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2747 flushpages ();
2750 let setzoom zoom =
2751 match state.throttle with
2752 | None ->
2753 let zoom = max 0.0001 zoom in
2754 if zoom <> conf.zoom
2755 then (
2756 state.prevzoom <- conf.zoom;
2757 conf.zoom <- zoom;
2758 reshape state.winw state.winh;
2759 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2762 | Some (layout, y, started) ->
2763 let time =
2764 match conf.maxwait with
2765 | None -> 0.0
2766 | Some t -> t
2768 let dt = now () -. started in
2769 if dt > time
2770 then (
2771 state.y <- y;
2772 load layout;
2776 let setcolumns mode columns coverA coverB =
2777 state.prevcolumns <- Some (conf.columns, conf.zoom);
2778 if columns < 0
2779 then (
2780 if isbirdseye mode
2781 then showtext '!' "split mode doesn't work in bird's eye"
2782 else (
2783 conf.columns <- Csplit (-columns, [||]);
2784 state.x <- 0;
2785 conf.zoom <- 1.0;
2788 else (
2789 if columns < 2
2790 then (
2791 conf.columns <- Csingle [||];
2792 state.x <- 0;
2793 setzoom 1.0;
2795 else (
2796 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2797 conf.zoom <- 1.0;
2800 reshape state.winw state.winh;
2803 let enterbirdseye () =
2804 let zoom = float conf.thumbw /. float state.winw in
2805 let birdseyepageno =
2806 let cy = state.winh / 2 in
2807 let fold = function
2808 | [] -> 0
2809 | l :: rest ->
2810 let rec fold best = function
2811 | [] -> best.pageno
2812 | l :: rest ->
2813 let d = cy - (l.pagedispy + l.pagevh/2)
2814 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2815 if abs d < abs dbest
2816 then fold l rest
2817 else best.pageno
2818 in fold l rest
2820 fold state.layout
2822 state.mode <- Birdseye (
2823 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2825 conf.zoom <- zoom;
2826 conf.presentation <- false;
2827 conf.interpagespace <- 10;
2828 conf.hlinks <- false;
2829 state.x <- 0;
2830 state.mstate <- Mnone;
2831 conf.maxwait <- None;
2832 conf.columns <- (
2833 match conf.beyecolumns with
2834 | Some c ->
2835 conf.zoom <- 1.0;
2836 Cmulti ((c, 0, 0), [||])
2837 | None -> Csingle [||]
2839 Wsi.setcursor Wsi.CURSOR_INHERIT;
2840 if conf.verbose
2841 then
2842 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2843 (100.0*.zoom)
2844 else
2845 state.text <- ""
2847 reshape state.winw state.winh;
2850 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2851 state.mode <- View;
2852 conf.zoom <- c.zoom;
2853 conf.presentation <- c.presentation;
2854 conf.interpagespace <- c.interpagespace;
2855 conf.maxwait <- c.maxwait;
2856 conf.hlinks <- c.hlinks;
2857 conf.beyecolumns <- (
2858 match conf.columns with
2859 | Cmulti ((c, _, _), _) -> Some c
2860 | Csingle _ -> None
2861 | Csplit _ -> failwith "leaving bird's eye split mode"
2863 conf.columns <- (
2864 match c.columns with
2865 | Cmulti (c, _) -> Cmulti (c, [||])
2866 | Csingle _ -> Csingle [||]
2867 | Csplit (c, _) -> Csplit (c, [||])
2869 state.x <- leftx;
2870 if conf.verbose
2871 then
2872 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2873 (100.0*.conf.zoom)
2875 reshape state.winw state.winh;
2876 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2879 let togglebirdseye () =
2880 match state.mode with
2881 | Birdseye vals -> leavebirdseye vals true
2882 | View -> enterbirdseye ()
2883 | _ -> ()
2886 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2887 let pageno = max 0 (pageno - incr) in
2888 let rec loop = function
2889 | [] -> gotopage1 pageno 0
2890 | l :: _ when l.pageno = pageno ->
2891 if l.pagedispy >= 0 && l.pagey = 0
2892 then G.postRedisplay "upbirdseye"
2893 else gotopage1 pageno 0
2894 | _ :: rest -> loop rest
2896 loop state.layout;
2897 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2900 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2901 let pageno = min (state.pagecount - 1) (pageno + incr) in
2902 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2903 let rec loop = function
2904 | [] ->
2905 let y, h = getpageyh pageno in
2906 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2907 gotoy (clamp dy)
2908 | l :: _ when l.pageno = pageno ->
2909 if l.pagevh != l.pageh
2910 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2911 else G.postRedisplay "downbirdseye"
2912 | _ :: rest -> loop rest
2914 loop state.layout
2917 let optentry mode _ key =
2918 let btos b = if b then "on" else "off" in
2919 if key >= 32 && key < 127
2920 then
2921 let c = Char.chr key in
2922 match c with
2923 | 's' ->
2924 let ondone s =
2925 try conf.scrollstep <- int_of_string s with exc ->
2926 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2928 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2930 | 'A' ->
2931 let ondone s =
2933 conf.autoscrollstep <- int_of_string s;
2934 if state.autoscroll <> None
2935 then state.autoscroll <- Some conf.autoscrollstep
2936 with exc ->
2937 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2939 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2941 | 'C' ->
2942 let ondone s =
2944 let n, a, b = multicolumns_of_string s in
2945 setcolumns mode n a b;
2946 with exc ->
2947 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2949 TEswitch ("columns: ", "", None, textentry, ondone, true)
2951 | 'Z' ->
2952 let ondone s =
2954 let zoom = float (int_of_string s) /. 100.0 in
2955 setzoom zoom
2956 with exc ->
2957 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2959 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2961 | 't' ->
2962 let ondone s =
2964 conf.thumbw <- bound (int_of_string s) 2 4096;
2965 state.text <-
2966 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2967 begin match mode with
2968 | Birdseye beye ->
2969 leavebirdseye beye false;
2970 enterbirdseye ();
2971 | _ -> ();
2973 with exc ->
2974 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2976 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2978 | 'R' ->
2979 let ondone s =
2980 match try
2981 Some (int_of_string s)
2982 with exc ->
2983 state.text <- Printf.sprintf "bad integer `%s': %s"
2984 s (exntos exc);
2985 None
2986 with
2987 | Some angle -> reqlayout angle conf.fitmodel
2988 | None -> ()
2990 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2992 | 'i' ->
2993 conf.icase <- not conf.icase;
2994 TEdone ("case insensitive search " ^ (btos conf.icase))
2996 | 'p' ->
2997 conf.preload <- not conf.preload;
2998 gotoy state.y;
2999 TEdone ("preload " ^ (btos conf.preload))
3001 | 'v' ->
3002 conf.verbose <- not conf.verbose;
3003 TEdone ("verbose " ^ (btos conf.verbose))
3005 | 'd' ->
3006 conf.debug <- not conf.debug;
3007 TEdone ("debug " ^ (btos conf.debug))
3009 | 'h' ->
3010 conf.maxhfit <- not conf.maxhfit;
3011 state.maxy <- calcheight ();
3012 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3014 | 'c' ->
3015 conf.crophack <- not conf.crophack;
3016 TEdone ("crophack " ^ btos conf.crophack)
3018 | 'a' ->
3019 let s =
3020 match conf.maxwait with
3021 | None ->
3022 conf.maxwait <- Some infinity;
3023 "always wait for page to complete"
3024 | Some _ ->
3025 conf.maxwait <- None;
3026 "show placeholder if page is not ready"
3028 TEdone s
3030 | 'f' ->
3031 conf.underinfo <- not conf.underinfo;
3032 TEdone ("underinfo " ^ btos conf.underinfo)
3034 | 'P' ->
3035 conf.savebmarks <- not conf.savebmarks;
3036 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3038 | 'S' ->
3039 let ondone s =
3041 let pageno, py =
3042 match state.layout with
3043 | [] -> 0, 0
3044 | l :: _ ->
3045 l.pageno, l.pagey
3047 conf.interpagespace <- int_of_string s;
3048 docolumns conf.columns;
3049 state.maxy <- calcheight ();
3050 let y = getpagey pageno in
3051 gotoy (y + py)
3052 with exc ->
3053 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3055 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3057 | 'l' ->
3058 let fm =
3059 match conf.fitmodel with
3060 | FitProportional -> FitWidth
3061 | _ -> FitProportional
3063 reqlayout conf.angle fm;
3064 TEdone ("proportional display " ^ btos (fm == FitProportional))
3066 | 'T' ->
3067 settrim (not conf.trimmargins) conf.trimfuzz;
3068 TEdone ("trim margins " ^ btos conf.trimmargins)
3070 | 'I' ->
3071 conf.invert <- not conf.invert;
3072 TEdone ("invert colors " ^ btos conf.invert)
3074 | 'x' ->
3075 let ondone s =
3076 cbput state.hists.sel s;
3077 conf.selcmd <- s;
3079 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3080 textentry, ondone, true)
3082 | _ ->
3083 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3084 TEstop
3085 else
3086 TEcont state.text
3089 class type lvsource = object
3090 method getitemcount : int
3091 method getitem : int -> (string * int)
3092 method hasaction : int -> bool
3093 method exit :
3094 uioh:uioh ->
3095 cancel:bool ->
3096 active:int ->
3097 first:int ->
3098 pan:int ->
3099 qsearch:string ->
3100 uioh option
3101 method getactive : int
3102 method getfirst : int
3103 method getqsearch : string
3104 method setqsearch : string -> unit
3105 method getpan : int
3106 end;;
3108 class virtual lvsourcebase = object
3109 val mutable m_active = 0
3110 val mutable m_first = 0
3111 val mutable m_qsearch = ""
3112 val mutable m_pan = 0
3113 method getactive = m_active
3114 method getfirst = m_first
3115 method getqsearch = m_qsearch
3116 method getpan = m_pan
3117 method setqsearch s = m_qsearch <- s
3118 end;;
3120 let withoutlastutf8 s =
3121 let len = String.length s in
3122 if len = 0
3123 then s
3124 else
3125 let rec find pos =
3126 if pos = 0
3127 then pos
3128 else
3129 let b = Char.code s.[pos] in
3130 if b land 0b11000000 = 0b11000000
3131 then pos
3132 else find (pos-1)
3134 let first =
3135 if Char.code s.[len-1] land 0x80 = 0
3136 then len-1
3137 else find (len-1)
3139 String.sub s 0 first;
3142 let textentrykeyboard
3143 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3144 let key =
3145 if key >= 0xffb0 && key <= 0xffb9
3146 then key - 0xffb0 + 48 else key
3148 let enttext te =
3149 state.mode <- Textentry (te, onleave);
3150 state.text <- "";
3151 enttext ();
3152 G.postRedisplay "textentrykeyboard enttext";
3154 let histaction cmd =
3155 match opthist with
3156 | None -> ()
3157 | Some (action, _) ->
3158 state.mode <- Textentry (
3159 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3161 G.postRedisplay "textentry histaction"
3163 match key with
3164 | 0xff08 -> (* backspace *)
3165 let s = withoutlastutf8 text in
3166 let len = String.length s in
3167 if cancelonempty && len = 0
3168 then (
3169 onleave Cancel;
3170 G.postRedisplay "textentrykeyboard after cancel";
3172 else (
3173 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3176 | 0xff0d | 0xff8d -> (* (kp) enter *)
3177 ondone text;
3178 onleave Confirm;
3179 G.postRedisplay "textentrykeyboard after confirm"
3181 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3182 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3183 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3184 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3186 | 0xff1b -> (* escape*)
3187 if String.length text = 0
3188 then (
3189 begin match opthist with
3190 | None -> ()
3191 | Some (_, onhistcancel) -> onhistcancel ()
3192 end;
3193 onleave Cancel;
3194 state.text <- "";
3195 G.postRedisplay "textentrykeyboard after cancel2"
3197 else (
3198 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3201 | 0xff9f | 0xffff -> () (* delete *)
3203 | _ when key != 0
3204 && key land 0xff00 != 0xff00 (* keyboard *)
3205 && key land 0xfe00 != 0xfe00 (* xkb *)
3206 && key land 0xfd00 != 0xfd00 (* 3270 *)
3208 begin match onkey text key with
3209 | TEdone text ->
3210 ondone text;
3211 onleave Confirm;
3212 G.postRedisplay "textentrykeyboard after confirm2";
3214 | TEcont text ->
3215 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3217 | TEstop ->
3218 onleave Cancel;
3219 G.postRedisplay "textentrykeyboard after cancel3"
3221 | TEswitch te ->
3222 state.mode <- Textentry (te, onleave);
3223 G.postRedisplay "textentrykeyboard switch";
3224 end;
3226 | _ ->
3227 vlog "unhandled key %s" (Wsi.keyname key)
3230 let firstof first active =
3231 if first > active || abs (first - active) > fstate.maxrows - 1
3232 then max 0 (active - (fstate.maxrows/2))
3233 else first
3236 let calcfirst first active =
3237 if active > first
3238 then
3239 let rows = active - first in
3240 if rows > fstate.maxrows then active - fstate.maxrows else first
3241 else active
3244 let scrollph y maxy =
3245 let sh = (float (maxy + state.winh) /. float state.winh) in
3246 let sh = float state.winh /. sh in
3247 let sh = max sh (float conf.scrollh) in
3249 let percent =
3250 if y = state.maxy
3251 then 1.0
3252 else float y /. float maxy
3254 let position = (float state.winh -. sh) *. percent in
3256 let position =
3257 if position +. sh > float state.winh
3258 then float state.winh -. sh
3259 else position
3261 position, sh;
3264 let coe s = (s :> uioh);;
3266 class listview ~(source:lvsource) ~trusted ~modehash =
3267 object (self)
3268 val m_pan = source#getpan
3269 val m_first = source#getfirst
3270 val m_active = source#getactive
3271 val m_qsearch = source#getqsearch
3272 val m_prev_uioh = state.uioh
3274 method private elemunder y =
3275 let n = y / (fstate.fontsize+1) in
3276 if m_first + n < source#getitemcount
3277 then (
3278 if source#hasaction (m_first + n)
3279 then Some (m_first + n)
3280 else None
3282 else None
3284 method display =
3285 Gl.enable `blend;
3286 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3287 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3288 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3289 GlDraw.color (1., 1., 1.);
3290 Gl.enable `texture_2d;
3291 let fs = fstate.fontsize in
3292 let nfs = fs + 1 in
3293 let ww = fstate.wwidth in
3294 let tabw = 30.0*.ww in
3295 let itemcount = source#getitemcount in
3296 let rec loop row =
3297 if (row - m_first) > fstate.maxrows
3298 then ()
3299 else (
3300 if row >= 0 && row < itemcount
3301 then (
3302 let (s, level) = source#getitem row in
3303 let y = (row - m_first) * nfs in
3304 let x = 5.0 +. float (level + m_pan) *. ww in
3305 if row = m_active
3306 then (
3307 Gl.disable `texture_2d;
3308 GlDraw.polygon_mode `both `line;
3309 let alpha = if source#hasaction row then 0.9 else 0.3 in
3310 GlDraw.color (1., 1., 1.) ~alpha;
3311 GlDraw.rect (1., float (y + 1))
3312 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3313 GlDraw.polygon_mode `both `fill;
3314 GlDraw.color (1., 1., 1.);
3315 Gl.enable `texture_2d;
3318 let drawtabularstring s =
3319 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3320 if trusted
3321 then
3322 let tabpos = try String.index s '\t' with Not_found -> -1 in
3323 if tabpos > 0
3324 then
3325 let len = String.length s - tabpos - 1 in
3326 let s1 = String.sub s 0 tabpos
3327 and s2 = String.sub s (tabpos + 1) len in
3328 let nx = drawstr x s1 in
3329 let sw = nx -. x in
3330 let x = x +. (max tabw sw) in
3331 drawstr x s2
3332 else
3333 drawstr x s
3334 else
3335 drawstr x s
3337 let _ = drawtabularstring s in
3338 loop (row+1)
3342 loop m_first;
3343 Gl.disable `blend;
3344 Gl.disable `texture_2d;
3346 method updownlevel incr =
3347 let len = source#getitemcount in
3348 let curlevel =
3349 if m_active >= 0 && m_active < len
3350 then snd (source#getitem m_active)
3351 else -1
3353 let rec flow i =
3354 if i = len then i-1 else if i = -1 then 0 else
3355 let _, l = source#getitem i in
3356 if l != curlevel then i else flow (i+incr)
3358 let active = flow m_active in
3359 let first = calcfirst m_first active in
3360 G.postRedisplay "outline updownlevel";
3361 {< m_active = active; m_first = first >}
3363 method private key1 key mask =
3364 let set1 active first qsearch =
3365 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3367 let search active pattern incr =
3368 let active = if active = -1 then m_first else active in
3369 let dosearch re =
3370 let rec loop n =
3371 if n >= 0 && n < source#getitemcount
3372 then (
3373 let s, _ = source#getitem n in
3375 (try ignore (Str.search_forward re s 0); true
3376 with Not_found -> false)
3377 then Some n
3378 else loop (n + incr)
3380 else None
3382 loop active
3385 let re = Str.regexp_case_fold pattern in
3386 dosearch re
3387 with Failure s ->
3388 state.text <- s;
3389 None
3391 let itemcount = source#getitemcount in
3392 let find start incr =
3393 let rec find i =
3394 if i = -1 || i = itemcount
3395 then -1
3396 else (
3397 if source#hasaction i
3398 then i
3399 else find (i + incr)
3402 find start
3404 let set active first =
3405 let first = bound first 0 (itemcount - fstate.maxrows) in
3406 state.text <- "";
3407 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3409 let navigate incr =
3410 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3411 let active, first =
3412 let incr1 = if incr > 0 then 1 else -1 in
3413 if isvisible m_first m_active
3414 then
3415 let next =
3416 let next = m_active + incr in
3417 let next =
3418 if next < 0 || next >= itemcount
3419 then -1
3420 else find next incr1
3422 if next = -1 || abs (m_active - next) > fstate.maxrows
3423 then -1
3424 else next
3426 if next = -1
3427 then
3428 let first = m_first + incr in
3429 let first = bound first 0 (itemcount - 1) in
3430 let next =
3431 let next = m_active + incr in
3432 let next = bound next 0 (itemcount - 1) in
3433 find next ~-incr1
3435 let active = if next = -1 then m_active else next in
3436 active, first
3437 else
3438 let first = min next m_first in
3439 let first =
3440 if abs (next - first) > fstate.maxrows
3441 then first + incr
3442 else first
3444 next, first
3445 else
3446 let first = m_first + incr in
3447 let first = bound first 0 (itemcount - 1) in
3448 let active =
3449 let next = m_active + incr in
3450 let next = bound next 0 (itemcount - 1) in
3451 let next = find next incr1 in
3452 let active =
3453 if next = -1 || abs (m_active - first) > fstate.maxrows
3454 then (
3455 let active = if m_active = -1 then next else m_active in
3456 active
3458 else next
3460 if isvisible first active
3461 then active
3462 else -1
3464 active, first
3466 G.postRedisplay "listview navigate";
3467 set active first;
3469 match key with
3470 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3471 let incr = if key = 0x72 then -1 else 1 in
3472 let active, first =
3473 match search (m_active + incr) m_qsearch incr with
3474 | None ->
3475 state.text <- m_qsearch ^ " [not found]";
3476 m_active, m_first
3477 | Some active ->
3478 state.text <- m_qsearch;
3479 active, firstof m_first active
3481 G.postRedisplay "listview ctrl-r/s";
3482 set1 active first m_qsearch;
3484 | 0xff08 -> (* backspace *)
3485 if String.length m_qsearch = 0
3486 then coe self
3487 else (
3488 let qsearch = withoutlastutf8 m_qsearch in
3489 let len = String.length qsearch in
3490 if len = 0
3491 then (
3492 state.text <- "";
3493 G.postRedisplay "listview empty qsearch";
3494 set1 m_active m_first "";
3496 else
3497 let active, first =
3498 match search m_active qsearch ~-1 with
3499 | None ->
3500 state.text <- qsearch ^ " [not found]";
3501 m_active, m_first
3502 | Some active ->
3503 state.text <- qsearch;
3504 active, firstof m_first active
3506 G.postRedisplay "listview backspace qsearch";
3507 set1 active first qsearch
3510 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3511 let pattern = m_qsearch ^ toutf8 key in
3512 let active, first =
3513 match search m_active pattern 1 with
3514 | None ->
3515 state.text <- pattern ^ " [not found]";
3516 m_active, m_first
3517 | Some active ->
3518 state.text <- pattern;
3519 active, firstof m_first active
3521 G.postRedisplay "listview qsearch add";
3522 set1 active first pattern;
3524 | 0xff1b -> (* escape *)
3525 state.text <- "";
3526 if String.length m_qsearch = 0
3527 then (
3528 G.postRedisplay "list view escape";
3529 begin
3530 match
3531 source#exit (coe self) true m_active m_first m_pan m_qsearch
3532 with
3533 | None -> m_prev_uioh
3534 | Some uioh -> uioh
3537 else (
3538 G.postRedisplay "list view kill qsearch";
3539 source#setqsearch "";
3540 coe {< m_qsearch = "" >}
3543 | 0xff0d | 0xff8d -> (* (kp) enter *)
3544 state.text <- "";
3545 let self = {< m_qsearch = "" >} in
3546 source#setqsearch "";
3547 let opt =
3548 G.postRedisplay "listview enter";
3549 if m_active >= 0 && m_active < source#getitemcount
3550 then (
3551 source#exit (coe self) false m_active m_first m_pan "";
3553 else (
3554 source#exit (coe self) true m_active m_first m_pan "";
3557 begin match opt with
3558 | None -> m_prev_uioh
3559 | Some uioh -> uioh
3562 | 0xff9f | 0xffff -> (* (kp) delete *)
3563 coe self
3565 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3566 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3567 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3568 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3570 | 0xff53 | 0xff98 -> (* (kp) right *)
3571 state.text <- "";
3572 G.postRedisplay "listview right";
3573 coe {< m_pan = m_pan - 1 >}
3575 | 0xff51 | 0xff96 -> (* (kp) left *)
3576 state.text <- "";
3577 G.postRedisplay "listview left";
3578 coe {< m_pan = m_pan + 1 >}
3580 | 0xff50 | 0xff95 -> (* (kp) home *)
3581 let active = find 0 1 in
3582 G.postRedisplay "listview home";
3583 set active 0;
3585 | 0xff57 | 0xff9c -> (* (kp) end *)
3586 let first = max 0 (itemcount - fstate.maxrows) in
3587 let active = find (itemcount - 1) ~-1 in
3588 G.postRedisplay "listview end";
3589 set active first;
3591 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3592 coe self
3594 | _ ->
3595 dolog "listview unknown key %#x" key; coe self
3597 method key key mask =
3598 match state.mode with
3599 | Textentry te -> textentrykeyboard key mask te; coe self
3600 | _ -> self#key1 key mask
3602 method button button down x y _ =
3603 let opt =
3604 match button with
3605 | 1 when x > state.winw - conf.scrollbw ->
3606 G.postRedisplay "listview scroll";
3607 if down
3608 then
3609 let _, position, sh = self#scrollph in
3610 if y > truncate position && y < truncate (position +. sh)
3611 then (
3612 state.mstate <- Mscrolly;
3613 Some (coe self)
3615 else
3616 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3617 let first = truncate (s *. float source#getitemcount) in
3618 let first = min source#getitemcount first in
3619 Some (coe {< m_first = first; m_active = first >})
3620 else (
3621 state.mstate <- Mnone;
3622 Some (coe self);
3624 | 1 when not down ->
3625 begin match self#elemunder y with
3626 | Some n ->
3627 G.postRedisplay "listview click";
3628 source#exit
3629 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3630 | _ ->
3631 Some (coe self)
3633 | n when (n == 4 || n == 5) && not down ->
3634 let len = source#getitemcount in
3635 let first =
3636 if n = 5 && m_first + fstate.maxrows >= len
3637 then
3638 m_first
3639 else
3640 let first = m_first + (if n == 4 then -1 else 1) in
3641 bound first 0 (len - 1)
3643 G.postRedisplay "listview wheel";
3644 Some (coe {< m_first = first >})
3645 | n when (n = 6 || n = 7) && not down ->
3646 let inc = m_first + (if n = 7 then -1 else 1) in
3647 G.postRedisplay "listview hwheel";
3648 Some (coe {< m_pan = m_pan + inc >})
3649 | _ ->
3650 Some (coe self)
3652 match opt with
3653 | None -> m_prev_uioh
3654 | Some uioh -> uioh
3656 method motion _ y =
3657 match state.mstate with
3658 | Mscrolly ->
3659 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3660 let first = truncate (s *. float source#getitemcount) in
3661 let first = min source#getitemcount first in
3662 G.postRedisplay "listview motion";
3663 coe {< m_first = first; m_active = first >}
3664 | _ -> coe self
3666 method pmotion x y =
3667 if x < state.winw - conf.scrollbw
3668 then
3669 let n =
3670 match self#elemunder y with
3671 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3672 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3674 let o =
3675 if n != m_active
3676 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3677 else self
3679 coe o
3680 else (
3681 Wsi.setcursor Wsi.CURSOR_INHERIT;
3682 coe self
3685 method infochanged _ = ()
3687 method scrollpw = (0, 0.0, 0.0)
3688 method scrollph =
3689 let nfs = fstate.fontsize + 1 in
3690 let y = m_first * nfs in
3691 let itemcount = source#getitemcount in
3692 let maxi = max 0 (itemcount - fstate.maxrows) in
3693 let maxy = maxi * nfs in
3694 let p, h = scrollph y maxy in
3695 conf.scrollbw, p, h
3697 method modehash = modehash
3698 method eformsgs = false
3699 end;;
3701 class outlinelistview ~source =
3702 object (self)
3703 inherit listview
3704 ~source:(source :> lvsource)
3705 ~trusted:false
3706 ~modehash:(findkeyhash conf "outline")
3707 as super
3709 method key key mask =
3710 let calcfirst first active =
3711 if active > first
3712 then
3713 let rows = active - first in
3714 let maxrows =
3715 if String.length state.text = 0
3716 then fstate.maxrows
3717 else fstate.maxrows - 2
3719 if rows > maxrows then active - maxrows else first
3720 else active
3722 let navigate incr =
3723 let active = m_active + incr in
3724 let active = bound active 0 (source#getitemcount - 1) in
3725 let first = calcfirst m_first active in
3726 G.postRedisplay "outline navigate";
3727 coe {< m_active = active; m_first = first >}
3729 let ctrl = Wsi.withctrl mask in
3730 match key with
3731 | 110 when ctrl -> (* ctrl-n *)
3732 source#narrow m_qsearch;
3733 G.postRedisplay "outline ctrl-n";
3734 coe {< m_first = 0; m_active = 0 >}
3736 | 117 when ctrl -> (* ctrl-u *)
3737 source#denarrow;
3738 G.postRedisplay "outline ctrl-u";
3739 state.text <- "";
3740 coe {< m_first = 0; m_active = 0 >}
3742 | 108 when ctrl -> (* ctrl-l *)
3743 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3744 G.postRedisplay "outline ctrl-l";
3745 coe {< m_first = first >}
3747 | 0xff9f | 0xffff -> (* (kp) delete *)
3748 source#remove m_active;
3749 G.postRedisplay "outline delete";
3750 let active = max 0 (m_active-1) in
3751 coe {< m_first = firstof m_first active;
3752 m_active = active >}
3754 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3755 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3756 | 0xff55 | 0xff9a -> (* (kp) prior *)
3757 navigate ~-(fstate.maxrows)
3758 | 0xff56 | 0xff9b -> (* (kp) next *)
3759 navigate fstate.maxrows
3761 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3762 let o =
3763 if ctrl
3764 then (
3765 G.postRedisplay "outline ctrl right";
3766 {< m_pan = m_pan + 1 >}
3768 else self#updownlevel 1
3770 coe o
3772 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3773 let o =
3774 if ctrl
3775 then (
3776 G.postRedisplay "outline ctrl left";
3777 {< m_pan = m_pan - 1 >}
3779 else self#updownlevel ~-1
3781 coe o
3783 | 0xff50 | 0xff95 -> (* (kp) home *)
3784 G.postRedisplay "outline home";
3785 coe {< m_first = 0; m_active = 0 >}
3787 | 0xff57 | 0xff9c -> (* (kp) end *)
3788 let active = source#getitemcount - 1 in
3789 let first = max 0 (active - fstate.maxrows) in
3790 G.postRedisplay "outline end";
3791 coe {< m_active = active; m_first = first >}
3793 | _ -> super#key key mask
3796 let outlinesource usebookmarks =
3797 let empty = [||] in
3798 (object
3799 inherit lvsourcebase
3800 val mutable m_items = empty
3801 val mutable m_orig_items = empty
3802 val mutable m_prev_items = empty
3803 val mutable m_narrow_pattern = ""
3804 val mutable m_hadremovals = false
3806 method getitemcount =
3807 Array.length m_items + (if m_hadremovals then 1 else 0)
3809 method getitem n =
3810 if n == Array.length m_items && m_hadremovals
3811 then
3812 ("[Confirm removal]", 0)
3813 else
3814 let s, n, _ = m_items.(n) in
3815 (s, n)
3817 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3818 ignore (uioh, first, qsearch);
3819 let confrimremoval = m_hadremovals && active = Array.length m_items in
3820 let items =
3821 if String.length m_narrow_pattern = 0
3822 then m_orig_items
3823 else m_items
3825 if not cancel
3826 then (
3827 if not confrimremoval
3828 then(
3829 let _, _, anchor = m_items.(active) in
3830 gotoghyll (getanchory anchor);
3831 m_items <- items;
3833 else (
3834 state.bookmarks <- Array.to_list m_items;
3835 m_orig_items <- m_items;
3838 else m_items <- items;
3839 m_pan <- pan;
3840 None
3842 method hasaction _ = true
3844 method greetmsg =
3845 if Array.length m_items != Array.length m_orig_items
3846 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3847 else ""
3849 method narrow pattern =
3850 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3851 match reopt with
3852 | None -> ()
3853 | Some re ->
3854 let rec loop accu n =
3855 if n = -1
3856 then (
3857 m_narrow_pattern <- pattern;
3858 m_items <- Array.of_list accu
3860 else
3861 let (s, _, _) as o = m_items.(n) in
3862 let accu =
3863 if (try ignore (Str.search_forward re s 0); true
3864 with Not_found -> false)
3865 then o :: accu
3866 else accu
3868 loop accu (n-1)
3870 loop [] (Array.length m_items - 1)
3872 method denarrow =
3873 m_orig_items <- (
3874 if usebookmarks
3875 then Array.of_list state.bookmarks
3876 else state.outlines
3878 m_items <- m_orig_items
3880 method remove m =
3881 if usebookmarks
3882 then
3883 if m >= 0 && m < Array.length m_items
3884 then (
3885 m_hadremovals <- true;
3886 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3887 let n = if n >= m then n+1 else n in
3888 m_items.(n)
3892 method reset anchor items =
3893 m_hadremovals <- false;
3894 if m_orig_items == empty || m_prev_items != items
3895 then (
3896 m_orig_items <- items;
3897 if String.length m_narrow_pattern = 0
3898 then m_items <- items;
3900 m_prev_items <- items;
3901 let rely = getanchory anchor in
3902 let active =
3903 let rec loop n best bestd =
3904 if n = Array.length m_items
3905 then best
3906 else
3907 let (_, _, anchor) = m_items.(n) in
3908 let orely = getanchory anchor in
3909 let d = abs (orely - rely) in
3910 if d < bestd
3911 then loop (n+1) n d
3912 else loop (n+1) best bestd
3914 loop 0 ~-1 max_int
3916 m_active <- active;
3917 m_first <- firstof m_first active
3918 end)
3921 let enterselector usebookmarks =
3922 let source = outlinesource usebookmarks in
3923 fun errmsg ->
3924 let outlines =
3925 if usebookmarks
3926 then Array.of_list state.bookmarks
3927 else state.outlines
3929 if Array.length outlines = 0
3930 then (
3931 showtext ' ' errmsg;
3933 else (
3934 state.text <- source#greetmsg;
3935 Wsi.setcursor Wsi.CURSOR_INHERIT;
3936 let anchor = getanchor () in
3937 source#reset anchor outlines;
3938 state.uioh <- coe (new outlinelistview ~source);
3939 G.postRedisplay "enter selector";
3943 let enteroutlinemode =
3944 let f = enterselector false in
3945 fun ()-> f "Document has no outline";
3948 let enterbookmarkmode =
3949 let f = enterselector true in
3950 fun () -> f "Document has no bookmarks (yet)";
3953 let color_of_string s =
3954 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3955 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3959 let color_to_string (r, g, b) =
3960 let r = truncate (r *. 256.0)
3961 and g = truncate (g *. 256.0)
3962 and b = truncate (b *. 256.0) in
3963 Printf.sprintf "%d/%d/%d" r g b
3966 let irect_of_string s =
3967 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3970 let irect_to_string (x0,y0,x1,y1) =
3971 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3974 let makecheckers () =
3975 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3976 following to say:
3977 converted by Issac Trotts. July 25, 2002 *)
3978 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3979 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3980 let id = GlTex.gen_texture () in
3981 GlTex.bind_texture `texture_2d id;
3982 GlPix.store (`unpack_alignment 1);
3983 GlTex.image2d image;
3984 List.iter (GlTex.parameter ~target:`texture_2d)
3985 [ `mag_filter `nearest; `min_filter `nearest ];
3989 let setcheckers enabled =
3990 match state.texid with
3991 | None ->
3992 if enabled then state.texid <- Some (makecheckers ())
3994 | Some texid ->
3995 if not enabled
3996 then (
3997 GlTex.delete_texture texid;
3998 state.texid <- None;
4002 let int_of_string_with_suffix s =
4003 let l = String.length s in
4004 let s1, shift =
4005 if l > 1
4006 then
4007 let suffix = Char.lowercase s.[l-1] in
4008 match suffix with
4009 | 'k' -> String.sub s 0 (l-1), 10
4010 | 'm' -> String.sub s 0 (l-1), 20
4011 | 'g' -> String.sub s 0 (l-1), 30
4012 | _ -> s, 0
4013 else s, 0
4015 let n = int_of_string s1 in
4016 let m = n lsl shift in
4017 if m < 0 || m < n
4018 then raise (Failure "value too large")
4019 else m
4022 let string_with_suffix_of_int n =
4023 if n = 0
4024 then "0"
4025 else
4026 let n, s =
4027 if n land ((1 lsl 30) - 1) = 0
4028 then n lsr 30, "G"
4029 else (
4030 if n land ((1 lsl 20) - 1) = 0
4031 then n lsr 20, "M"
4032 else (
4033 if n land ((1 lsl 10) - 1) = 0
4034 then n lsr 10, "K"
4035 else n, ""
4039 let rec loop s n =
4040 let h = n mod 1000 in
4041 let n = n / 1000 in
4042 if n = 0
4043 then string_of_int h ^ s
4044 else (
4045 let s = Printf.sprintf "_%03d%s" h s in
4046 loop s n
4049 loop "" n ^ s;
4052 let defghyllscroll = (40, 8, 32);;
4053 let ghyllscroll_of_string s =
4054 let (n, a, b) as nab =
4055 if s = "default"
4056 then defghyllscroll
4057 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4059 if n <= a || n <= b || a >= b
4060 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4061 nab;
4064 let ghyllscroll_to_string ((n, a, b) as nab) =
4065 if nab = defghyllscroll
4066 then "default"
4067 else Printf.sprintf "%d,%d,%d" n a b;
4070 let describe_location () =
4071 let fn = page_of_y state.y in
4072 let ln = page_of_y (state.y + state.winh - state.hscrollh - 1) in
4073 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4074 let percent =
4075 if maxy <= 0
4076 then 100.
4077 else (100. *. (float state.y /. float maxy))
4079 if fn = ln
4080 then
4081 Printf.sprintf "page %d of %d [%.2f%%]"
4082 (fn+1) state.pagecount percent
4083 else
4084 Printf.sprintf
4085 "pages %d-%d of %d [%.2f%%]"
4086 (fn+1) (ln+1) state.pagecount percent
4089 let setpresentationmode v =
4090 let n = page_of_y state.y in
4091 state.anchor <- (n, 0.0, 1.0);
4092 conf.presentation <- v;
4093 if conf.presentation
4094 then (
4095 if not conf.scrollbarinpm
4096 then state.scrollw <- 0;
4098 else state.scrollw <- conf.scrollbw;
4099 represent ();
4102 let enterinfomode =
4103 let btos b = if b then "\xe2\x88\x9a" else "" in
4104 let showextended = ref false in
4105 let leave mode = function
4106 | Confirm -> state.mode <- mode
4107 | Cancel -> state.mode <- mode in
4108 let src =
4109 (object
4110 val mutable m_first_time = true
4111 val mutable m_l = []
4112 val mutable m_a = [||]
4113 val mutable m_prev_uioh = nouioh
4114 val mutable m_prev_mode = View
4116 inherit lvsourcebase
4118 method reset prev_mode prev_uioh =
4119 m_a <- Array.of_list (List.rev m_l);
4120 m_l <- [];
4121 m_prev_mode <- prev_mode;
4122 m_prev_uioh <- prev_uioh;
4123 if m_first_time
4124 then (
4125 let rec loop n =
4126 if n >= Array.length m_a
4127 then ()
4128 else
4129 match m_a.(n) with
4130 | _, _, _, Action _ -> m_active <- n
4131 | _ -> loop (n+1)
4133 loop 0;
4134 m_first_time <- false;
4137 method int name get set =
4138 m_l <-
4139 (name, `int get, 1, Action (
4140 fun u ->
4141 let ondone s =
4142 try set (int_of_string s)
4143 with exn ->
4144 state.text <- Printf.sprintf "bad integer `%s': %s"
4145 s (exntos exn)
4147 state.text <- "";
4148 let te = name ^ ": ", "", None, intentry, ondone, true in
4149 state.mode <- Textentry (te, leave m_prev_mode);
4151 )) :: m_l
4153 method int_with_suffix name get set =
4154 m_l <-
4155 (name, `intws get, 1, Action (
4156 fun u ->
4157 let ondone s =
4158 try set (int_of_string_with_suffix s)
4159 with exn ->
4160 state.text <- Printf.sprintf "bad integer `%s': %s"
4161 s (exntos exn)
4163 state.text <- "";
4164 let te =
4165 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4167 state.mode <- Textentry (te, leave m_prev_mode);
4169 )) :: m_l
4171 method bool ?(offset=1) ?(btos=btos) name get set =
4172 m_l <-
4173 (name, `bool (btos, get), offset, Action (
4174 fun u ->
4175 let v = get () in
4176 set (not v);
4178 )) :: m_l
4180 method color name get set =
4181 m_l <-
4182 (name, `color get, 1, Action (
4183 fun u ->
4184 let invalid = (nan, nan, nan) in
4185 let ondone s =
4186 let c =
4187 try color_of_string s
4188 with exn ->
4189 state.text <- Printf.sprintf "bad color `%s': %s"
4190 s (exntos exn);
4191 invalid
4193 if c <> invalid
4194 then set c;
4196 let te = name ^ ": ", "", None, textentry, ondone, true in
4197 state.text <- color_to_string (get ());
4198 state.mode <- Textentry (te, leave m_prev_mode);
4200 )) :: m_l
4202 method string name get set =
4203 m_l <-
4204 (name, `string get, 1, Action (
4205 fun u ->
4206 let ondone s = set s in
4207 let te = name ^ ": ", "", None, textentry, ondone, true in
4208 state.mode <- Textentry (te, leave m_prev_mode);
4210 )) :: m_l
4212 method colorspace name get set =
4213 m_l <-
4214 (name, `string get, 1, Action (
4215 fun _ ->
4216 let source =
4217 let vals = [| "rgb"; "bgr"; "gray" |] in
4218 (object
4219 inherit lvsourcebase
4221 initializer
4222 m_active <- int_of_colorspace conf.colorspace;
4223 m_first <- 0;
4225 method getitemcount = Array.length vals
4226 method getitem n = (vals.(n), 0)
4227 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4228 ignore (uioh, first, pan, qsearch);
4229 if not cancel then set active;
4230 None
4231 method hasaction _ = true
4232 end)
4234 state.text <- "";
4235 let modehash = findkeyhash conf "info" in
4236 coe (new listview ~source ~trusted:true ~modehash)
4237 )) :: m_l
4239 method fitmodel name get set =
4240 m_l <-
4241 (name, `string get, 1, Action (
4242 fun _ ->
4243 let source =
4244 let vals = [| "fit width"; "proportional"; "fit page" |] in
4245 (object
4246 inherit lvsourcebase
4248 initializer
4249 m_active <- int_of_fitmodel conf.fitmodel;
4250 m_first <- 0;
4252 method getitemcount = Array.length vals
4253 method getitem n = (vals.(n), 0)
4254 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4255 ignore (uioh, first, pan, qsearch);
4256 if not cancel then set active;
4257 None
4258 method hasaction _ = true
4259 end)
4261 state.text <- "";
4262 let modehash = findkeyhash conf "info" in
4263 coe (new listview ~source ~trusted:true ~modehash)
4264 )) :: m_l
4266 method caption s offset =
4267 m_l <- (s, `empty, offset, Noaction) :: m_l
4269 method caption2 s f offset =
4270 m_l <- (s, `string f, offset, Noaction) :: m_l
4272 method getitemcount = Array.length m_a
4274 method getitem n =
4275 let tostr = function
4276 | `int f -> string_of_int (f ())
4277 | `intws f -> string_with_suffix_of_int (f ())
4278 | `string f -> f ()
4279 | `color f -> color_to_string (f ())
4280 | `bool (btos, f) -> btos (f ())
4281 | `empty -> ""
4283 let name, t, offset, _ = m_a.(n) in
4284 ((let s = tostr t in
4285 if String.length s > 0
4286 then Printf.sprintf "%s\t%s" name s
4287 else name),
4288 offset)
4290 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4291 let uiohopt =
4292 if not cancel
4293 then (
4294 m_qsearch <- qsearch;
4295 let uioh =
4296 match m_a.(active) with
4297 | _, _, _, Action f -> f uioh
4298 | _ -> uioh
4300 Some uioh
4302 else None
4304 m_active <- active;
4305 m_first <- first;
4306 m_pan <- pan;
4307 uiohopt
4309 method hasaction n =
4310 match m_a.(n) with
4311 | _, _, _, Action _ -> true
4312 | _ -> false
4313 end)
4315 let rec fillsrc prevmode prevuioh =
4316 let sep () = src#caption "" 0 in
4317 let colorp name get set =
4318 src#string name
4319 (fun () -> color_to_string (get ()))
4320 (fun v ->
4322 let c = color_of_string v in
4323 set c
4324 with exn ->
4325 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4328 let oldmode = state.mode in
4329 let birdseye = isbirdseye state.mode in
4331 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4333 src#bool "presentation mode"
4334 (fun () -> conf.presentation)
4335 (fun v -> setpresentationmode v);
4337 src#bool "ignore case in searches"
4338 (fun () -> conf.icase)
4339 (fun v -> conf.icase <- v);
4341 src#bool "preload"
4342 (fun () -> conf.preload)
4343 (fun v -> conf.preload <- v);
4345 src#bool "highlight links"
4346 (fun () -> conf.hlinks)
4347 (fun v -> conf.hlinks <- v);
4349 src#bool "under info"
4350 (fun () -> conf.underinfo)
4351 (fun v -> conf.underinfo <- v);
4353 src#bool "persistent bookmarks"
4354 (fun () -> conf.savebmarks)
4355 (fun v -> conf.savebmarks <- v);
4357 src#fitmodel "fit model"
4358 (fun () -> fitmodel_to_string conf.fitmodel)
4359 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4361 src#bool "trim margins"
4362 (fun () -> conf.trimmargins)
4363 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4365 src#bool "persistent location"
4366 (fun () -> conf.jumpback)
4367 (fun v -> conf.jumpback <- v);
4369 sep ();
4370 src#int "inter-page space"
4371 (fun () -> conf.interpagespace)
4372 (fun n ->
4373 conf.interpagespace <- n;
4374 docolumns conf.columns;
4375 let pageno, py =
4376 match state.layout with
4377 | [] -> 0, 0
4378 | l :: _ ->
4379 l.pageno, l.pagey
4381 state.maxy <- calcheight ();
4382 let y = getpagey pageno in
4383 gotoy (y + py)
4386 src#int "page bias"
4387 (fun () -> conf.pagebias)
4388 (fun v -> conf.pagebias <- v);
4390 src#int "scroll step"
4391 (fun () -> conf.scrollstep)
4392 (fun n -> conf.scrollstep <- n);
4394 src#int "horizontal scroll step"
4395 (fun () -> conf.hscrollstep)
4396 (fun v -> conf.hscrollstep <- v);
4398 src#int "auto scroll step"
4399 (fun () ->
4400 match state.autoscroll with
4401 | Some step -> step
4402 | _ -> conf.autoscrollstep)
4403 (fun n ->
4404 if state.autoscroll <> None
4405 then state.autoscroll <- Some n;
4406 conf.autoscrollstep <- n);
4408 src#int "zoom"
4409 (fun () -> truncate (conf.zoom *. 100.))
4410 (fun v -> setzoom ((float v) /. 100.));
4412 src#int "rotation"
4413 (fun () -> conf.angle)
4414 (fun v -> reqlayout v conf.fitmodel);
4416 src#int "scroll bar width"
4417 (fun () -> state.scrollw)
4418 (fun v ->
4419 state.scrollw <- v;
4420 conf.scrollbw <- v;
4421 reshape state.winw state.winh;
4424 src#int "scroll handle height"
4425 (fun () -> conf.scrollh)
4426 (fun v -> conf.scrollh <- v;);
4428 src#int "thumbnail width"
4429 (fun () -> conf.thumbw)
4430 (fun v ->
4431 conf.thumbw <- min 4096 v;
4432 match oldmode with
4433 | Birdseye beye ->
4434 leavebirdseye beye false;
4435 enterbirdseye ()
4436 | _ -> ()
4439 let mode = state.mode in
4440 src#string "columns"
4441 (fun () ->
4442 match conf.columns with
4443 | Csingle _ -> "1"
4444 | Cmulti (multi, _) -> multicolumns_to_string multi
4445 | Csplit (count, _) -> "-" ^ string_of_int count
4447 (fun v ->
4448 let n, a, b = multicolumns_of_string v in
4449 setcolumns mode n a b);
4451 sep ();
4452 src#caption "Presentation mode" 0;
4453 src#bool "scrollbar visible"
4454 (fun () -> conf.scrollbarinpm)
4455 (fun v ->
4456 if v != conf.scrollbarinpm
4457 then (
4458 conf.scrollbarinpm <- v;
4459 if conf.presentation
4460 then (
4461 state.scrollw <- if v then conf.scrollbw else 0;
4462 reshape state.winw state.winh;
4467 sep ();
4468 src#caption "Pixmap cache" 0;
4469 src#int_with_suffix "size (advisory)"
4470 (fun () -> conf.memlimit)
4471 (fun v -> conf.memlimit <- v);
4473 src#caption2 "used"
4474 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4475 (string_with_suffix_of_int state.memused)
4476 (Hashtbl.length state.tilemap)) 1;
4478 sep ();
4479 src#caption "Layout" 0;
4480 src#caption2 "Dimension"
4481 (fun () ->
4482 Printf.sprintf "%dx%d (virtual %dx%d)"
4483 state.winw state.winh
4484 state.w state.maxy)
4486 if conf.debug
4487 then
4488 src#caption2 "Position" (fun () ->
4489 Printf.sprintf "%dx%d" state.x state.y
4491 else
4492 src#caption2 "Position" (fun () -> describe_location ()) 1
4495 sep ();
4496 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4497 "Save these parameters as global defaults at exit"
4498 (fun () -> conf.bedefault)
4499 (fun v -> conf.bedefault <- v)
4502 sep ();
4503 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4504 src#bool ~offset:0 ~btos "Extended parameters"
4505 (fun () -> !showextended)
4506 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4507 if !showextended
4508 then (
4509 src#bool "checkers"
4510 (fun () -> conf.checkers)
4511 (fun v -> conf.checkers <- v; setcheckers v);
4512 src#bool "update cursor"
4513 (fun () -> conf.updatecurs)
4514 (fun v -> conf.updatecurs <- v);
4515 src#bool "verbose"
4516 (fun () -> conf.verbose)
4517 (fun v -> conf.verbose <- v);
4518 src#bool "invert colors"
4519 (fun () -> conf.invert)
4520 (fun v -> conf.invert <- v);
4521 src#bool "max fit"
4522 (fun () -> conf.maxhfit)
4523 (fun v -> conf.maxhfit <- v);
4524 src#bool "redirect stderr"
4525 (fun () -> conf.redirectstderr)
4526 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4527 src#string "uri launcher"
4528 (fun () -> conf.urilauncher)
4529 (fun v -> conf.urilauncher <- v);
4530 src#string "path launcher"
4531 (fun () -> conf.pathlauncher)
4532 (fun v -> conf.pathlauncher <- v);
4533 src#string "tile size"
4534 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4535 (fun v ->
4537 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4538 conf.tilew <- max 64 w;
4539 conf.tileh <- max 64 h;
4540 flushtiles ();
4541 with exn ->
4542 state.text <- Printf.sprintf "bad tile size `%s': %s"
4543 v (exntos exn)
4545 src#int "texture count"
4546 (fun () -> conf.texcount)
4547 (fun v ->
4548 if realloctexts v
4549 then conf.texcount <- v
4550 else showtext '!' " Failed to set texture count please retry later"
4552 src#int "slice height"
4553 (fun () -> conf.sliceheight)
4554 (fun v ->
4555 conf.sliceheight <- v;
4556 wcmd "sliceh %d" conf.sliceheight;
4558 src#int "anti-aliasing level"
4559 (fun () -> conf.aalevel)
4560 (fun v ->
4561 conf.aalevel <- bound v 0 8;
4562 state.anchor <- getanchor ();
4563 opendoc state.path state.password;
4565 src#string "page scroll scaling factor"
4566 (fun () -> string_of_float conf.pgscale)
4567 (fun v ->
4569 let s = float_of_string v in
4570 conf.pgscale <- s
4571 with exn ->
4572 state.text <- Printf.sprintf
4573 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4576 src#int "ui font size"
4577 (fun () -> fstate.fontsize)
4578 (fun v -> setfontsize (bound v 5 100));
4579 src#int "hint font size"
4580 (fun () -> conf.hfsize)
4581 (fun v -> conf.hfsize <- bound v 5 100);
4582 colorp "background color"
4583 (fun () -> conf.bgcolor)
4584 (fun v -> conf.bgcolor <- v);
4585 src#bool "crop hack"
4586 (fun () -> conf.crophack)
4587 (fun v -> conf.crophack <- v);
4588 src#string "trim fuzz"
4589 (fun () -> irect_to_string conf.trimfuzz)
4590 (fun v ->
4592 conf.trimfuzz <- irect_of_string v;
4593 if conf.trimmargins
4594 then settrim true conf.trimfuzz;
4595 with exn ->
4596 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4598 src#string "throttle"
4599 (fun () ->
4600 match conf.maxwait with
4601 | None -> "show place holder if page is not ready"
4602 | Some time ->
4603 if time = infinity
4604 then "wait for page to fully render"
4605 else
4606 "wait " ^ string_of_float time
4607 ^ " seconds before showing placeholder"
4609 (fun v ->
4611 let f = float_of_string v in
4612 if f <= 0.0
4613 then conf.maxwait <- None
4614 else conf.maxwait <- Some f
4615 with exn ->
4616 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4618 src#string "ghyll scroll"
4619 (fun () ->
4620 match conf.ghyllscroll with
4621 | None -> ""
4622 | Some nab -> ghyllscroll_to_string nab
4624 (fun v ->
4626 let gs =
4627 if String.length v = 0
4628 then None
4629 else Some (ghyllscroll_of_string v)
4631 conf.ghyllscroll <- gs
4632 with exn ->
4633 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4635 src#string "selection command"
4636 (fun () -> conf.selcmd)
4637 (fun v -> conf.selcmd <- v);
4638 src#string "synctex command"
4639 (fun () -> conf.stcmd)
4640 (fun v -> conf.stcmd <- v);
4641 src#colorspace "color space"
4642 (fun () -> colorspace_to_string conf.colorspace)
4643 (fun v ->
4644 conf.colorspace <- colorspace_of_int v;
4645 wcmd "cs %d" v;
4646 load state.layout;
4648 if pbousable ()
4649 then
4650 src#bool "use PBO"
4651 (fun () -> conf.usepbo)
4652 (fun v -> conf.usepbo <- v);
4653 src#bool "mouse wheel scrolls pages"
4654 (fun () -> conf.wheelbypage)
4655 (fun v -> conf.wheelbypage <- v);
4658 sep ();
4659 src#caption "Document" 0;
4660 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4661 src#caption2 "Pages"
4662 (fun () -> string_of_int state.pagecount) 1;
4663 src#caption2 "Dimensions"
4664 (fun () -> string_of_int (List.length state.pdims)) 1;
4665 if conf.trimmargins
4666 then (
4667 sep ();
4668 src#caption "Trimmed margins" 0;
4669 src#caption2 "Dimensions"
4670 (fun () -> string_of_int (List.length state.pdims)) 1;
4673 sep ();
4674 src#caption "OpenGL" 0;
4675 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4676 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4677 src#reset prevmode prevuioh;
4679 fun () ->
4680 state.text <- "";
4681 let prevmode = state.mode
4682 and prevuioh = state.uioh in
4683 fillsrc prevmode prevuioh;
4684 let source = (src :> lvsource) in
4685 let modehash = findkeyhash conf "info" in
4686 state.uioh <- coe (object (self)
4687 inherit listview ~source ~trusted:true ~modehash as super
4688 val mutable m_prevmemused = 0
4689 method infochanged = function
4690 | Memused ->
4691 if m_prevmemused != state.memused
4692 then (
4693 m_prevmemused <- state.memused;
4694 G.postRedisplay "memusedchanged";
4696 | Pdim -> G.postRedisplay "pdimchanged"
4697 | Docinfo -> fillsrc prevmode prevuioh
4699 method key key mask =
4700 if not (Wsi.withctrl mask)
4701 then
4702 match key with
4703 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4704 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4705 | _ -> super#key key mask
4706 else super#key key mask
4707 end);
4708 G.postRedisplay "info";
4711 let enterhelpmode =
4712 let source =
4713 (object
4714 inherit lvsourcebase
4715 method getitemcount = Array.length state.help
4716 method getitem n =
4717 let s, l, _ = state.help.(n) in
4718 (s, l)
4720 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4721 let optuioh =
4722 if not cancel
4723 then (
4724 m_qsearch <- qsearch;
4725 match state.help.(active) with
4726 | _, _, Action f -> Some (f uioh)
4727 | _ -> Some (uioh)
4729 else None
4731 m_active <- active;
4732 m_first <- first;
4733 m_pan <- pan;
4734 optuioh
4736 method hasaction n =
4737 match state.help.(n) with
4738 | _, _, Action _ -> true
4739 | _ -> false
4741 initializer
4742 m_active <- -1
4743 end)
4744 in fun () ->
4745 let modehash = findkeyhash conf "help" in
4746 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4747 G.postRedisplay "help";
4750 let entermsgsmode =
4751 let msgsource =
4752 let re = Str.regexp "[\r\n]" in
4753 (object
4754 inherit lvsourcebase
4755 val mutable m_items = [||]
4757 method getitemcount = 1 + Array.length m_items
4759 method getitem n =
4760 if n = 0
4761 then "[Clear]", 0
4762 else m_items.(n-1), 0
4764 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4765 ignore uioh;
4766 if not cancel
4767 then (
4768 if active = 0
4769 then Buffer.clear state.errmsgs;
4770 m_qsearch <- qsearch;
4772 m_active <- active;
4773 m_first <- first;
4774 m_pan <- pan;
4775 None
4777 method hasaction n =
4778 n = 0
4780 method reset =
4781 state.newerrmsgs <- false;
4782 let l = Str.split re (Buffer.contents state.errmsgs) in
4783 m_items <- Array.of_list l
4785 initializer
4786 m_active <- 0
4787 end)
4788 in fun () ->
4789 state.text <- "";
4790 msgsource#reset;
4791 let source = (msgsource :> lvsource) in
4792 let modehash = findkeyhash conf "listview" in
4793 state.uioh <- coe (object
4794 inherit listview ~source ~trusted:false ~modehash as super
4795 method display =
4796 if state.newerrmsgs
4797 then msgsource#reset;
4798 super#display
4799 end);
4800 G.postRedisplay "msgs";
4803 let quickbookmark ?title () =
4804 match state.layout with
4805 | [] -> ()
4806 | l :: _ ->
4807 let title =
4808 match title with
4809 | None ->
4810 let sec = Unix.gettimeofday () in
4811 let tm = Unix.localtime sec in
4812 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4813 (l.pageno+1)
4814 tm.Unix.tm_mday
4815 tm.Unix.tm_mon
4816 (tm.Unix.tm_year + 1900)
4817 tm.Unix.tm_hour
4818 tm.Unix.tm_min
4819 | Some title -> title
4821 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4824 let setautoscrollspeed step goingdown =
4825 let incr = max 1 ((abs step) / 2) in
4826 let incr = if goingdown then incr else -incr in
4827 let astep = step + incr in
4828 state.autoscroll <- Some astep;
4831 let gotounder = function
4832 | Ulinkgoto (pageno, top) ->
4833 if pageno >= 0
4834 then (
4835 addnav ();
4836 gotopage1 pageno top;
4839 | Ulinkuri s ->
4840 gotouri s
4842 | Uremote (filename, pageno) ->
4843 let path =
4844 if Sys.file_exists filename
4845 then filename
4846 else
4847 let dir = Filename.dirname state.path in
4848 let path = Filename.concat dir filename in
4849 if Sys.file_exists path
4850 then path
4851 else ""
4853 if String.length path > 0
4854 then (
4855 let anchor = getanchor () in
4856 let ranchor = state.path, state.password, anchor in
4857 state.anchor <- (pageno, 0.0, 0.0);
4858 state.ranchors <- ranchor :: state.ranchors;
4859 opendoc path "";
4861 else showtext '!' ("Could not find " ^ filename)
4863 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4866 let canpan () =
4867 match conf.columns with
4868 | Csplit _ -> true
4869 | _ -> state.x != 0 || conf.zoom > 1.0
4872 let existsinrow pageno (columns, coverA, coverB) p =
4873 let last = ((pageno - coverA) mod columns) + columns in
4874 let rec any = function
4875 | [] -> false
4876 | l :: rest ->
4877 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4878 then p l
4879 else (
4880 if not (p l)
4881 then (if l.pageno = last then false else any rest)
4882 else true
4885 any state.layout
4888 let nextpage () =
4889 match state.layout with
4890 | [] ->
4891 let pageno = page_of_y state.y in
4892 gotoghyll (getpagey (pageno+1))
4893 | l :: rest ->
4894 match conf.columns with
4895 | Csingle _ ->
4896 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4897 then
4898 let y = clamp (pgscale state.winh) in
4899 gotoghyll y
4900 else
4901 let pageno = min (l.pageno+1) (state.pagecount-1) in
4902 gotoghyll (getpagey pageno)
4903 | Cmulti ((c, _, _) as cl, _) ->
4904 if conf.presentation
4905 && (existsinrow l.pageno cl
4906 (fun l -> l.pageh > l.pagey + l.pagevh))
4907 then
4908 let y = clamp (pgscale state.winh) in
4909 gotoghyll y
4910 else
4911 let pageno = min (l.pageno+c) (state.pagecount-1) in
4912 gotoghyll (getpagey pageno)
4913 | Csplit (n, _) ->
4914 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4915 then
4916 let pagey, pageh = getpageyh l.pageno in
4917 let pagey = pagey + pageh * l.pagecol in
4918 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4919 gotoghyll (pagey + pageh + ips)
4922 let prevpage () =
4923 match state.layout with
4924 | [] ->
4925 let pageno = page_of_y state.y in
4926 gotoghyll (getpagey (pageno-1))
4927 | l :: _ ->
4928 match conf.columns with
4929 | Csingle _ ->
4930 if conf.presentation && l.pagey != 0
4931 then
4932 gotoghyll (clamp (pgscale ~-(state.winh)))
4933 else
4934 let pageno = max 0 (l.pageno-1) in
4935 gotoghyll (getpagey pageno)
4936 | Cmulti ((c, _, coverB) as cl, _) ->
4937 if conf.presentation &&
4938 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4939 then
4940 gotoghyll (clamp (pgscale ~-(state.winh)))
4941 else
4942 let decr =
4943 if l.pageno = state.pagecount - coverB
4944 then 1
4945 else c
4947 let pageno = max 0 (l.pageno-decr) in
4948 gotoghyll (getpagey pageno)
4949 | Csplit (n, _) ->
4950 let y =
4951 if l.pagecol = 0
4952 then
4953 if l.pageno = 0
4954 then l.pagey
4955 else
4956 let pageno = max 0 (l.pageno-1) in
4957 let pagey, pageh = getpageyh pageno in
4958 pagey + (n-1)*pageh
4959 else
4960 let pagey, pageh = getpageyh l.pageno in
4961 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4963 gotoghyll y
4966 let viewkeyboard key mask =
4967 let enttext te =
4968 let mode = state.mode in
4969 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4970 state.text <- "";
4971 enttext ();
4972 G.postRedisplay "view:enttext"
4974 let ctrl = Wsi.withctrl mask in
4975 let key =
4976 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4978 match key with
4979 | 81 -> (* Q *)
4980 exit 0
4982 | 0xff63 -> (* insert *)
4983 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4984 then (
4985 state.mode <- LinkNav (Ltgendir 0);
4986 gotoy state.y;
4988 else showtext '!' "Keyboard link navigation does not work under rotation"
4990 | 0xff1b | 113 -> (* escape / q *)
4991 begin match state.mstate with
4992 | Mzoomrect _ ->
4993 state.mstate <- Mnone;
4994 Wsi.setcursor Wsi.CURSOR_INHERIT;
4995 G.postRedisplay "kill zoom rect";
4996 | _ ->
4997 begin match state.mode with
4998 | LinkNav _ ->
4999 state.mode <- View;
5000 G.postRedisplay "esc leave linknav"
5001 | _ ->
5002 match state.ranchors with
5003 | [] -> raise Quit
5004 | (path, password, anchor) :: rest ->
5005 state.ranchors <- rest;
5006 state.anchor <- anchor;
5007 opendoc path password
5008 end;
5009 end;
5011 | 0xff08 -> (* backspace *)
5012 gotoghyll (getnav ~-1)
5014 | 111 -> (* o *)
5015 enteroutlinemode ()
5017 | 117 -> (* u *)
5018 state.rects <- [];
5019 state.text <- "";
5020 G.postRedisplay "dehighlight";
5022 | 47 | 63 -> (* / ? *)
5023 let ondone isforw s =
5024 cbput state.hists.pat s;
5025 state.searchpattern <- s;
5026 search s isforw
5028 let s = String.create 1 in
5029 s.[0] <- Char.chr key;
5030 enttext (s, "", Some (onhist state.hists.pat),
5031 textentry, ondone (key = 47), true)
5033 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5034 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5035 setzoom (conf.zoom +. incr)
5037 | 43 | 0xffab -> (* + *)
5038 let ondone s =
5039 let n =
5040 try int_of_string s with exc ->
5041 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5042 max_int
5044 if n != max_int
5045 then (
5046 conf.pagebias <- n;
5047 state.text <- "page bias is now " ^ string_of_int n;
5050 enttext ("page bias: ", "", None, intentry, ondone, true)
5052 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5053 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5054 setzoom (max 0.01 (conf.zoom -. decr))
5056 | 45 | 0xffad -> (* - *)
5057 let ondone msg = state.text <- msg in
5058 enttext (
5059 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5060 optentry state.mode, ondone, true
5063 | 48 when ctrl -> (* ctrl-0 *)
5064 if conf.zoom = 1.0
5065 then (
5066 state.x <- 0;
5067 state.hscrollh <-
5068 if state.w <= state.winw - state.scrollw
5069 then 0
5070 else state.scrollw
5072 gotoy state.y
5074 else setzoom 1.0
5076 | (49 | 50) when ctrl -> (* ctrl-1/2 *)
5077 let cols =
5078 match conf.columns with
5079 | Csingle _ | Cmulti _ -> 1
5080 | Csplit (n, _) -> n
5082 let h = state.winh -
5083 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5085 let zoom = zoomforh state.winw h state.scrollw cols in
5086 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5087 then setzoom zoom
5089 | 51 when ctrl -> (* ctrl-3 *)
5090 let fm =
5091 match conf.fitmodel with
5092 | FitWidth -> FitProportional
5093 | FitProportional -> FitPage
5094 | FitPage -> FitWidth
5096 state.text <- "fit model: " ^ fitmodel_to_string fm;
5097 reqlayout conf.angle fm
5099 | 0xffc6 -> (* f9 *)
5100 togglebirdseye ()
5102 | 57 when ctrl -> (* ctrl-9 *)
5103 togglebirdseye ()
5105 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5106 when not ctrl -> (* 0..9 *)
5107 let ondone s =
5108 let n =
5109 try int_of_string s with exc ->
5110 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5113 if n >= 0
5114 then (
5115 addnav ();
5116 cbput state.hists.pag (string_of_int n);
5117 gotopage1 (n + conf.pagebias - 1) 0;
5120 let pageentry text key =
5121 match Char.unsafe_chr key with
5122 | 'g' -> TEdone text
5123 | _ -> intentry text key
5125 let text = "x" in text.[0] <- Char.chr key;
5126 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5128 | 98 -> (* b *)
5129 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5130 reshape state.winw state.winh;
5132 | 108 -> (* l *)
5133 conf.hlinks <- not conf.hlinks;
5134 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5135 G.postRedisplay "toggle highlightlinks";
5137 | 70 -> (* F *)
5138 state.glinks <- true;
5139 let mode = state.mode in
5140 state.mode <- Textentry (
5141 (":", "", None, linknentry, linkndone gotounder, false),
5142 (fun _ ->
5143 state.glinks <- false;
5144 state.mode <- mode)
5146 state.text <- "";
5147 G.postRedisplay "view:linkent(F)"
5149 | 121 -> (* y *)
5150 state.glinks <- true;
5151 let mode = state.mode in
5152 state.mode <- Textentry (
5153 (":", "", None, linknentry, linkndone (fun under ->
5154 match Ne.pipe () with
5155 | Ne.Exn exn ->
5156 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
5157 | Ne.Res (r, w) ->
5158 let popened =
5159 try popen conf.selcmd [r, 0; w, -1]; true
5160 with exn ->
5161 showtext '!'
5162 (Printf.sprintf "failed to execute %s: %s"
5163 conf.selcmd (exntos exn));
5164 false
5166 let clo cap fd =
5167 Ne.clo fd (fun msg ->
5168 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5171 let s = undertext under in
5172 if popened
5173 then
5174 (try
5175 let l = String.length s in
5176 let n = tempfailureretry (Unix.write w s 0) l in
5177 if n != l
5178 then
5179 showtext '!'
5180 (Printf.sprintf
5181 "failed to write %d characters to sel pipe, wrote %d"
5184 with exn ->
5185 showtext '!'
5186 (Printf.sprintf "failed to write to sel pipe: %s"
5187 (exntos exn)
5190 else dolog "%s" s;
5191 clo "pipe/r" r;
5192 clo "pipe/w" w;
5193 ), false
5195 fun _ ->
5196 state.glinks <- false;
5197 state.mode <- mode
5199 state.text <- "";
5200 G.postRedisplay "view:linkent"
5202 | 97 -> (* a *)
5203 begin match state.autoscroll with
5204 | Some step ->
5205 conf.autoscrollstep <- step;
5206 state.autoscroll <- None
5207 | None ->
5208 if conf.autoscrollstep = 0
5209 then state.autoscroll <- Some 1
5210 else state.autoscroll <- Some conf.autoscrollstep
5213 | 112 when ctrl -> (* ctrl-p *)
5214 launchpath ()
5216 | 80 -> (* P *)
5217 setpresentationmode (not conf.presentation);
5218 showtext ' ' ("presentation mode " ^
5219 if conf.presentation then "on" else "off");
5221 | 102 -> (* f *)
5222 if List.mem Wsi.Fullscreen state.winstate
5223 then Wsi.reshape conf.cwinw conf.cwinh
5224 else Wsi.fullscreen ()
5226 | 112 | 78 -> (* p|N *)
5227 search state.searchpattern false
5229 | 110 | 0xffc0 -> (* n|F3 *)
5230 search state.searchpattern true
5232 | 116 -> (* t *)
5233 begin match state.layout with
5234 | [] -> ()
5235 | l :: _ ->
5236 gotoghyll (getpagey l.pageno)
5239 | 32 -> (* space *)
5240 nextpage ()
5242 | 0xff9f | 0xffff -> (* delete *)
5243 prevpage ()
5245 | 61 -> (* = *)
5246 showtext ' ' (describe_location ());
5248 | 119 -> (* w *)
5249 begin match state.layout with
5250 | [] -> ()
5251 | l :: _ ->
5252 Wsi.reshape (l.pagew + state.scrollw) l.pageh;
5253 G.postRedisplay "w"
5256 | 39 -> (* ' *)
5257 enterbookmarkmode ()
5259 | 104 | 0xffbe -> (* h|F1 *)
5260 enterhelpmode ()
5262 | 105 -> (* i *)
5263 enterinfomode ()
5265 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5266 entermsgsmode ()
5268 | 109 -> (* m *)
5269 let ondone s =
5270 match state.layout with
5271 | l :: _ ->
5272 if String.length s > 0
5273 then
5274 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5275 | _ -> ()
5277 enttext ("bookmark: ", "", None, textentry, ondone, true)
5279 | 126 -> (* ~ *)
5280 quickbookmark ();
5281 showtext ' ' "Quick bookmark added";
5283 | 122 -> (* z *)
5284 begin match state.layout with
5285 | l :: _ ->
5286 let rect = getpdimrect l.pagedimno in
5287 let w, h =
5288 if conf.crophack
5289 then
5290 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5291 truncate (1.2 *. (rect.(3) -. rect.(0))))
5292 else
5293 (truncate (rect.(1) -. rect.(0)),
5294 truncate (rect.(3) -. rect.(0)))
5296 let w = truncate ((float w)*.conf.zoom)
5297 and h = truncate ((float h)*.conf.zoom) in
5298 if w != 0 && h != 0
5299 then (
5300 state.anchor <- getanchor ();
5301 Wsi.reshape (w + state.scrollw) (h + conf.interpagespace)
5303 G.postRedisplay "z";
5305 | [] -> ()
5308 | 60 | 62 -> (* < > *)
5309 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5311 | 91 | 93 -> (* [ ] *)
5312 conf.colorscale <-
5313 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5315 G.postRedisplay "brightness";
5317 | 99 when state.mode = View -> (* c *)
5318 let (c, a, b), z =
5319 match state.prevcolumns with
5320 | None -> (1, 0, 0), 1.0
5321 | Some (columns, z) ->
5322 let cab =
5323 match columns with
5324 | Csplit (c, _) -> -c, 0, 0
5325 | Cmulti ((c, a, b), _) -> c, a, b
5326 | Csingle _ -> 1, 0, 0
5328 cab, z
5330 setcolumns View c a b;
5331 setzoom z;
5333 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5334 setzoom state.prevzoom
5336 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5337 begin match state.autoscroll with
5338 | None ->
5339 begin match state.mode with
5340 | Birdseye beye -> upbirdseye 1 beye
5341 | _ ->
5342 if ctrl
5343 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5344 else (
5345 if not (Wsi.withshift mask) && conf.presentation
5346 then prevpage ()
5347 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5350 | Some n ->
5351 setautoscrollspeed n false
5354 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5355 begin match state.autoscroll with
5356 | None ->
5357 begin match state.mode with
5358 | Birdseye beye -> downbirdseye 1 beye
5359 | _ ->
5360 if ctrl
5361 then gotoy_and_clear_text (clamp (state.winh/2))
5362 else (
5363 if not (Wsi.withshift mask) && conf.presentation
5364 then nextpage ()
5365 else gotoy_and_clear_text (clamp conf.scrollstep)
5368 | Some n ->
5369 setautoscrollspeed n true
5372 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5373 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5374 if canpan ()
5375 then
5376 let dx =
5377 if ctrl
5378 then state.winw / 2
5379 else conf.hscrollstep
5381 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5382 state.x <- state.x + dx;
5383 gotoy_and_clear_text state.y
5384 else (
5385 state.text <- "";
5386 G.postRedisplay "lef/right"
5389 | 0xff55 | 0xff9a -> (* (kp) prior *)
5390 let y =
5391 if ctrl
5392 then
5393 match state.layout with
5394 | [] -> state.y
5395 | l :: _ -> state.y - l.pagey
5396 else
5397 clamp (pgscale (-state.winh))
5399 gotoghyll y
5401 | 0xff56 | 0xff9b -> (* (kp) next *)
5402 let y =
5403 if ctrl
5404 then
5405 match List.rev state.layout with
5406 | [] -> state.y
5407 | l :: _ -> getpagey l.pageno
5408 else
5409 clamp (pgscale state.winh)
5411 gotoghyll y
5413 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5414 gotoghyll 0
5415 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5416 gotoghyll (clamp state.maxy)
5418 | 0xff53 | 0xff98
5419 when Wsi.withalt mask -> (* alt-(kp) right *)
5420 gotoghyll (getnav 1)
5421 | 0xff51 | 0xff96
5422 when Wsi.withalt mask -> (* alt-(kp) left *)
5423 gotoghyll (getnav ~-1)
5425 | 114 -> (* r *)
5426 reload ()
5428 | 118 when conf.debug -> (* v *)
5429 state.rects <- [];
5430 List.iter (fun l ->
5431 match getopaque l.pageno with
5432 | None -> ()
5433 | Some opaque ->
5434 let x0, y0, x1, y1 = pagebbox opaque in
5435 let a,b = float x0, float y0 in
5436 let c,d = float x1, float y0 in
5437 let e,f = float x1, float y1 in
5438 let h,j = float x0, float y1 in
5439 let rect = (a,b,c,d,e,f,h,j) in
5440 debugrect rect;
5441 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5442 ) state.layout;
5443 G.postRedisplay "v";
5445 | _ ->
5446 vlog "huh? %s" (Wsi.keyname key)
5449 let linknavkeyboard key mask linknav =
5450 let getpage pageno =
5451 let rec loop = function
5452 | [] -> None
5453 | l :: _ when l.pageno = pageno -> Some l
5454 | _ :: rest -> loop rest
5455 in loop state.layout
5457 let doexact (pageno, n) =
5458 match getopaque pageno, getpage pageno with
5459 | Some opaque, Some l ->
5460 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5461 then
5462 let under = getlink opaque n in
5463 G.postRedisplay "link gotounder";
5464 gotounder under;
5465 state.mode <- View;
5466 else
5467 let opt, dir =
5468 match key with
5469 | 0xff50 -> (* home *)
5470 Some (findlink opaque LDfirst), -1
5472 | 0xff57 -> (* end *)
5473 Some (findlink opaque LDlast), 1
5475 | 0xff51 -> (* left *)
5476 Some (findlink opaque (LDleft n)), -1
5478 | 0xff53 -> (* right *)
5479 Some (findlink opaque (LDright n)), 1
5481 | 0xff52 -> (* up *)
5482 Some (findlink opaque (LDup n)), -1
5484 | 0xff54 -> (* down *)
5485 Some (findlink opaque (LDdown n)), 1
5487 | _ -> None, 0
5489 let pwl l dir =
5490 begin match findpwl l.pageno dir with
5491 | Pwlnotfound -> ()
5492 | Pwl pageno ->
5493 let notfound dir =
5494 state.mode <- LinkNav (Ltgendir dir);
5495 let y, h = getpageyh pageno in
5496 let y =
5497 if dir < 0
5498 then y + h - state.winh
5499 else y
5501 gotoy y
5503 begin match getopaque pageno, getpage pageno with
5504 | Some opaque, Some _ ->
5505 let link =
5506 let ld = if dir > 0 then LDfirst else LDlast in
5507 findlink opaque ld
5509 begin match link with
5510 | Lfound m ->
5511 showlinktype (getlink opaque m);
5512 state.mode <- LinkNav (Ltexact (pageno, m));
5513 G.postRedisplay "linknav jpage";
5514 | _ -> notfound dir
5515 end;
5516 | _ -> notfound dir
5517 end;
5518 end;
5520 begin match opt with
5521 | Some Lnotfound -> pwl l dir;
5522 | Some (Lfound m) ->
5523 if m = n
5524 then pwl l dir
5525 else (
5526 let _, y0, _, y1 = getlinkrect opaque m in
5527 if y0 < l.pagey
5528 then gotopage1 l.pageno y0
5529 else (
5530 let d = fstate.fontsize + 1 in
5531 if y1 - l.pagey > l.pagevh - d
5532 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5533 else G.postRedisplay "linknav";
5535 showlinktype (getlink opaque m);
5536 state.mode <- LinkNav (Ltexact (l.pageno, m));
5539 | None -> viewkeyboard key mask
5540 end;
5541 | _ -> viewkeyboard key mask
5543 if key = 0xff63
5544 then (
5545 state.mode <- View;
5546 G.postRedisplay "leave linknav"
5548 else
5549 match linknav with
5550 | Ltgendir _ -> viewkeyboard key mask
5551 | Ltexact exact -> doexact exact
5554 let keyboard key mask =
5555 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5556 then wcmd "interrupt"
5557 else state.uioh <- state.uioh#key key mask
5560 let birdseyekeyboard key mask
5561 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5562 let incr =
5563 match conf.columns with
5564 | Csingle _ -> 1
5565 | Cmulti ((c, _, _), _) -> c
5566 | Csplit _ -> failwith "bird's eye split mode"
5568 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5569 match key with
5570 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5571 let y, h = getpageyh pageno in
5572 let top = (state.winh - h) / 2 in
5573 gotoy (max 0 (y - top))
5574 | 0xff0d (* enter *)
5575 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5576 | 0xff1b -> leavebirdseye beye true (* escape *)
5577 | 0xff52 -> upbirdseye incr beye (* up *)
5578 | 0xff54 -> downbirdseye incr beye (* down *)
5579 | 0xff51 -> upbirdseye 1 beye (* left *)
5580 | 0xff53 -> downbirdseye 1 beye (* right *)
5582 | 0xff55 -> (* prior *)
5583 begin match state.layout with
5584 | l :: _ ->
5585 if l.pagey != 0
5586 then (
5587 state.mode <- Birdseye (
5588 oconf, leftx, l.pageno, hooverpageno, anchor
5590 gotopage1 l.pageno 0;
5592 else (
5593 let layout = layout (state.y-state.winh) (pgh state.layout) in
5594 match layout with
5595 | [] -> gotoy (clamp (-state.winh))
5596 | l :: _ ->
5597 state.mode <- Birdseye (
5598 oconf, leftx, l.pageno, hooverpageno, anchor
5600 gotopage1 l.pageno 0
5603 | [] -> gotoy (clamp (-state.winh))
5604 end;
5606 | 0xff56 -> (* next *)
5607 begin match List.rev state.layout with
5608 | l :: _ ->
5609 let layout = layout (state.y + (pgh state.layout)) state.winh in
5610 begin match layout with
5611 | [] ->
5612 let incr = l.pageh - l.pagevh in
5613 if incr = 0
5614 then (
5615 state.mode <-
5616 Birdseye (
5617 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5619 G.postRedisplay "birdseye pagedown";
5621 else gotoy (clamp (incr + conf.interpagespace*2));
5623 | l :: _ ->
5624 state.mode <-
5625 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5626 gotopage1 l.pageno 0;
5629 | [] -> gotoy (clamp state.winh)
5630 end;
5632 | 0xff50 -> (* home *)
5633 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5634 gotopage1 0 0
5636 | 0xff57 -> (* end *)
5637 let pageno = state.pagecount - 1 in
5638 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5639 if not (pagevisible state.layout pageno)
5640 then
5641 let h =
5642 match List.rev state.pdims with
5643 | [] -> state.winh
5644 | (_, _, h, _) :: _ -> h
5646 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5647 else G.postRedisplay "birdseye end";
5648 | _ -> viewkeyboard key mask
5651 let drawpage l linkindexbase =
5652 let color =
5653 match state.mode with
5654 | Textentry _ -> scalecolor 0.4
5655 | LinkNav _
5656 | View -> scalecolor 1.0
5657 | Birdseye (_, _, pageno, hooverpageno, _) ->
5658 if l.pageno = hooverpageno
5659 then scalecolor 0.9
5660 else (
5661 if l.pageno = pageno
5662 then scalecolor 1.0
5663 else scalecolor 0.8
5666 drawtiles l color;
5667 begin match getopaque l.pageno with
5668 | Some opaque ->
5669 if tileready l l.pagex l.pagey
5670 then
5671 let x = l.pagedispx - l.pagex
5672 and y = l.pagedispy - l.pagey in
5673 let hlmask =
5674 match conf.columns with
5675 | Csingle _ | Cmulti _ ->
5676 (if conf.hlinks then 1 else 0)
5677 + (if state.glinks
5678 && not (isbirdseye state.mode) then 2 else 0)
5679 | _ -> 0
5681 let s =
5682 match state.mode with
5683 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5684 | _ -> ""
5686 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5687 else 0
5689 | _ -> 0
5690 end;
5693 let scrollindicator () =
5694 let sbw, ph, sh = state.uioh#scrollph in
5695 let sbh, pw, sw = state.uioh#scrollpw in
5697 GlDraw.color (0.64, 0.64, 0.64);
5698 GlDraw.rect
5699 (float (state.winw - sbw), 0.)
5700 (float state.winw, float state.winh)
5702 GlDraw.rect
5703 (0., float (state.winh - sbh))
5704 (float (state.winw - state.scrollw - 1), float state.winh)
5706 GlDraw.color (0.0, 0.0, 0.0);
5708 GlDraw.rect
5709 (float (state.winw - sbw), ph)
5710 (float state.winw, ph +. sh)
5712 GlDraw.rect
5713 (pw, float (state.winh - sbh))
5714 (pw +. sw, float state.winh)
5718 let showsel () =
5719 match state.mstate with
5720 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5723 | Msel ((x0, y0), (x1, y1)) ->
5724 let rec loop = function
5725 | l :: ls ->
5726 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5727 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5728 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5729 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5730 then
5731 match getopaque l.pageno with
5732 | Some opaque ->
5733 let x0, y0 = pagetranslatepoint l x0 y0 in
5734 let x1, y1 = pagetranslatepoint l x1 y1 in
5735 seltext opaque (x0, y0, x1, y1);
5736 | _ -> ()
5737 else loop ls
5738 | [] -> ()
5740 loop state.layout
5743 let showrects rects =
5744 Gl.enable `blend;
5745 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5746 GlDraw.polygon_mode `both `fill;
5747 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5748 List.iter
5749 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5750 List.iter (fun l ->
5751 if l.pageno = pageno
5752 then (
5753 let dx = float (l.pagedispx - l.pagex) in
5754 let dy = float (l.pagedispy - l.pagey) in
5755 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5756 GlDraw.begins `quads;
5758 GlDraw.vertex2 (x0+.dx, y0+.dy);
5759 GlDraw.vertex2 (x1+.dx, y1+.dy);
5760 GlDraw.vertex2 (x2+.dx, y2+.dy);
5761 GlDraw.vertex2 (x3+.dx, y3+.dy);
5763 GlDraw.ends ();
5765 ) state.layout
5766 ) rects
5768 Gl.disable `blend;
5771 let display () =
5772 GlClear.color (scalecolor2 conf.bgcolor);
5773 GlClear.clear [`color];
5774 let rec loop linkindexbase = function
5775 | l :: rest ->
5776 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5777 loop linkindexbase rest
5778 | [] -> ()
5780 loop 0 state.layout;
5781 let rects =
5782 match state.mode with
5783 | LinkNav (Ltexact (pageno, linkno)) ->
5784 begin match getopaque pageno with
5785 | Some opaque ->
5786 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5787 (pageno, 5, (
5788 float x0, float y0,
5789 float x1, float y0,
5790 float x1, float y1,
5791 float x0, float y1)
5792 ) :: state.rects
5793 | None -> state.rects
5795 | _ -> state.rects
5797 showrects rects;
5798 showsel ();
5799 state.uioh#display;
5800 begin match state.mstate with
5801 | Mzoomrect ((x0, y0), (x1, y1)) ->
5802 Gl.enable `blend;
5803 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5804 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5805 GlDraw.rect (float x0, float y0)
5806 (float x1, float y1);
5807 Gl.disable `blend;
5808 | _ -> ()
5809 end;
5810 enttext ();
5811 scrollindicator ();
5812 Wsi.swapb ();
5815 let zoomrect x y x1 y1 =
5816 let x0 = min x x1
5817 and x1 = max x x1
5818 and y0 = min y y1 in
5819 gotoy (state.y + y0);
5820 state.anchor <- getanchor ();
5821 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5822 let margin =
5823 if state.w < state.winw - state.scrollw
5824 then (state.winw - state.scrollw - state.w) / 2
5825 else 0
5827 state.x <- (state.x + margin) - x0;
5828 setzoom zoom;
5829 Wsi.setcursor Wsi.CURSOR_INHERIT;
5830 state.mstate <- Mnone;
5833 let scrollx x =
5834 let winw = state.winw - state.scrollw - 1 in
5835 let s = float x /. float winw in
5836 let destx = truncate (float (state.w + winw) *. s) in
5837 state.x <- winw - destx;
5838 gotoy_and_clear_text state.y;
5839 state.mstate <- Mscrollx;
5842 let scrolly y =
5843 let s = float y /. float state.winh in
5844 let desty = truncate (float (state.maxy - state.winh) *. s) in
5845 gotoy_and_clear_text desty;
5846 state.mstate <- Mscrolly;
5849 let viewmouse button down x y mask =
5850 match button with
5851 | n when (n == 4 || n == 5) && not down ->
5852 if Wsi.withctrl mask
5853 then (
5854 match state.mstate with
5855 | Mzoom (oldn, i) ->
5856 if oldn = n
5857 then (
5858 if i = 2
5859 then
5860 let incr =
5861 match n with
5862 | 5 ->
5863 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5864 | _ ->
5865 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5867 let zoom = conf.zoom -. incr in
5868 setzoom zoom;
5869 state.mstate <- Mzoom (n, 0);
5870 else
5871 state.mstate <- Mzoom (n, i+1);
5873 else state.mstate <- Mzoom (n, 0)
5875 | _ -> state.mstate <- Mzoom (n, 0)
5877 else (
5878 match state.autoscroll with
5879 | Some step -> setautoscrollspeed step (n=4)
5880 | None ->
5881 if conf.wheelbypage || conf.presentation
5882 then (
5883 if n = 4
5884 then prevpage ()
5885 else nextpage ()
5887 else
5888 let incr =
5889 if n = 4
5890 then -conf.scrollstep
5891 else conf.scrollstep
5893 let incr = incr * 2 in
5894 let y = clamp incr in
5895 gotoy_and_clear_text y
5898 | n when (n = 6 || n = 7) && not down && canpan () ->
5899 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5900 gotoy_and_clear_text state.y
5902 | 1 when Wsi.withshift mask ->
5903 state.mstate <- Mnone;
5904 if not down
5905 then (
5906 match unproject x y with
5907 | Some (pageno, ux, uy) ->
5908 let cmd = Printf.sprintf
5909 "%s %s %d %d %d"
5910 conf.stcmd state.path pageno ux uy
5912 popen cmd []
5913 | None -> ()
5916 | 1 when Wsi.withctrl mask ->
5917 if down
5918 then (
5919 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5920 state.mstate <- Mpan (x, y)
5922 else
5923 state.mstate <- Mnone
5925 | 3 ->
5926 if down
5927 then (
5928 Wsi.setcursor Wsi.CURSOR_CYCLE;
5929 let p = (x, y) in
5930 state.mstate <- Mzoomrect (p, p)
5932 else (
5933 match state.mstate with
5934 | Mzoomrect ((x0, y0), _) ->
5935 if abs (x-x0) > 10 && abs (y - y0) > 10
5936 then zoomrect x0 y0 x y
5937 else (
5938 state.mstate <- Mnone;
5939 Wsi.setcursor Wsi.CURSOR_INHERIT;
5940 G.postRedisplay "kill accidental zoom rect";
5942 | _ ->
5943 Wsi.setcursor Wsi.CURSOR_INHERIT;
5944 state.mstate <- Mnone
5947 | 1 when x > state.winw - state.scrollw ->
5948 if down
5949 then
5950 let _, position, sh = state.uioh#scrollph in
5951 if y > truncate position && y < truncate (position +. sh)
5952 then state.mstate <- Mscrolly
5953 else scrolly y
5954 else
5955 state.mstate <- Mnone
5957 | 1 when y > state.winh - state.hscrollh ->
5958 if down
5959 then
5960 let _, position, sw = state.uioh#scrollpw in
5961 if x > truncate position && x < truncate (position +. sw)
5962 then state.mstate <- Mscrollx
5963 else scrollx x
5964 else
5965 state.mstate <- Mnone
5967 | 1 ->
5968 let dest = if down then getunder x y else Unone in
5969 begin match dest with
5970 | Ulinkgoto _
5971 | Ulinkuri _
5972 | Uremote _
5973 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5974 gotounder dest
5976 | Unone when down ->
5977 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5978 state.mstate <- Mpan (x, y);
5980 | Unone | Utext _ ->
5981 if down
5982 then (
5983 if conf.angle mod 360 = 0
5984 then (
5985 state.mstate <- Msel ((x, y), (x, y));
5986 G.postRedisplay "mouse select";
5989 else (
5990 match state.mstate with
5991 | Mnone -> ()
5993 | Mzoom _ | Mscrollx | Mscrolly ->
5994 state.mstate <- Mnone
5996 | Mzoomrect ((x0, y0), _) ->
5997 zoomrect x0 y0 x y
5999 | Mpan _ ->
6000 Wsi.setcursor Wsi.CURSOR_INHERIT;
6001 state.mstate <- Mnone
6003 | Msel ((x0, y0), (x1, y1)) ->
6004 let rec loop = function
6005 | [] -> ()
6006 | l :: rest ->
6007 let inside =
6008 let a0 = l.pagedispy in
6009 let a1 = a0 + l.pagevh in
6010 let b0 = l.pagedispx in
6011 let b1 = b0 + l.pagevw in
6012 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6013 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6015 if inside
6016 then
6017 match getopaque l.pageno with
6018 | Some opaque ->
6019 begin
6020 match Ne.pipe () with
6021 | Ne.Exn exn ->
6022 showtext '!'
6023 (Printf.sprintf
6024 "can not create sel pipe: %s"
6025 (exntos exn));
6026 | Ne.Res (r, w) ->
6027 let doclose what fd =
6028 Ne.clo fd (fun msg ->
6029 dolog "%s close failed: %s" what msg)
6032 popen conf.selcmd [r, 0; w, -1];
6033 copysel w opaque;
6034 doclose "pipe/r" r;
6035 G.postRedisplay "copysel";
6036 with exn ->
6037 dolog "can not execute %S: %s"
6038 conf.selcmd (exntos exn);
6039 doclose "pipe/r" r;
6040 doclose "pipe/w" w;
6042 | None -> ()
6043 else loop rest
6045 loop state.layout;
6046 Wsi.setcursor Wsi.CURSOR_INHERIT;
6047 state.mstate <- Mnone;
6051 | _ -> ()
6054 let birdseyemouse button down x y mask
6055 (conf, leftx, _, hooverpageno, anchor) =
6056 match button with
6057 | 1 when down ->
6058 let rec loop = function
6059 | [] -> ()
6060 | l :: rest ->
6061 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6062 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6063 then (
6064 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6066 else loop rest
6068 loop state.layout
6069 | 3 -> ()
6070 | _ -> viewmouse button down x y mask
6073 let mouse button down x y mask =
6074 state.uioh <- state.uioh#button button down x y mask;
6077 let motion ~x ~y =
6078 state.uioh <- state.uioh#motion x y
6081 let pmotion ~x ~y =
6082 state.uioh <- state.uioh#pmotion x y;
6085 let uioh = object
6086 method display = ()
6088 method key key mask =
6089 begin match state.mode with
6090 | Textentry textentry -> textentrykeyboard key mask textentry
6091 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6092 | View -> viewkeyboard key mask
6093 | LinkNav linknav -> linknavkeyboard key mask linknav
6094 end;
6095 state.uioh
6097 method button button bstate x y mask =
6098 begin match state.mode with
6099 | LinkNav _
6100 | View -> viewmouse button bstate x y mask
6101 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6102 | Textentry _ -> ()
6103 end;
6104 state.uioh
6106 method motion x y =
6107 begin match state.mode with
6108 | Textentry _ -> ()
6109 | View | Birdseye _ | LinkNav _ ->
6110 match state.mstate with
6111 | Mzoom _ | Mnone -> ()
6113 | Mpan (x0, y0) ->
6114 let dx = x - x0
6115 and dy = y0 - y in
6116 state.mstate <- Mpan (x, y);
6117 if canpan ()
6118 then state.x <- state.x + dx;
6119 let y = clamp dy in
6120 gotoy_and_clear_text y
6122 | Msel (a, _) ->
6123 state.mstate <- Msel (a, (x, y));
6124 G.postRedisplay "motion select";
6126 | Mscrolly ->
6127 let y = min state.winh (max 0 y) in
6128 scrolly y
6130 | Mscrollx ->
6131 let x = min state.winw (max 0 x) in
6132 scrollx x
6134 | Mzoomrect (p0, _) ->
6135 state.mstate <- Mzoomrect (p0, (x, y));
6136 G.postRedisplay "motion zoomrect";
6137 end;
6138 state.uioh
6140 method pmotion x y =
6141 begin match state.mode with
6142 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6143 let rec loop = function
6144 | [] ->
6145 if hooverpageno != -1
6146 then (
6147 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6148 G.postRedisplay "pmotion birdseye no hoover";
6150 | l :: rest ->
6151 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6152 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6153 then (
6154 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6155 G.postRedisplay "pmotion birdseye hoover";
6157 else loop rest
6159 loop state.layout
6161 | Textentry _ -> ()
6163 | LinkNav _
6164 | View ->
6165 match state.mstate with
6166 | Mnone -> updateunder x y
6167 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6169 end;
6170 state.uioh
6172 method infochanged _ = ()
6174 method scrollph =
6175 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6176 let p, h = scrollph state.y maxy in
6177 state.scrollw, p, h
6179 method scrollpw =
6180 let winw = state.winw - state.scrollw - 1 in
6181 let fwinw = float winw in
6182 let sw =
6183 let sw = fwinw /. float state.w in
6184 let sw = fwinw *. sw in
6185 max sw (float conf.scrollh)
6187 let position, sw =
6188 let f = state.w+winw in
6189 let r = float (winw-state.x) /. float f in
6190 let p = fwinw *. r in
6191 p-.sw/.2., sw
6193 let sw =
6194 if position +. sw > fwinw
6195 then fwinw -. position
6196 else sw
6198 state.hscrollh, position, sw
6200 method modehash =
6201 let modename =
6202 match state.mode with
6203 | LinkNav _ -> "links"
6204 | Textentry _ -> "textentry"
6205 | Birdseye _ -> "birdseye"
6206 | View -> "view"
6208 findkeyhash conf modename
6210 method eformsgs = true
6211 end;;
6213 module Config =
6214 struct
6215 open Parser
6217 let fontpath = ref "";;
6219 module KeyMap =
6220 Map.Make (struct type t = (int * int) let compare = compare end);;
6222 let unent s =
6223 let l = String.length s in
6224 let b = Buffer.create l in
6225 unent b s 0 l;
6226 Buffer.contents b;
6229 let home =
6230 try Sys.getenv "HOME"
6231 with exn ->
6232 prerr_endline
6233 ("Can not determine home directory location: " ^ exntos exn);
6237 let modifier_of_string = function
6238 | "alt" -> Wsi.altmask
6239 | "shift" -> Wsi.shiftmask
6240 | "ctrl" | "control" -> Wsi.ctrlmask
6241 | "meta" -> Wsi.metamask
6242 | _ -> 0
6245 let key_of_string =
6246 let r = Str.regexp "-" in
6247 fun s ->
6248 let elems = Str.full_split r s in
6249 let f n k m =
6250 let g s =
6251 let m1 = modifier_of_string s in
6252 if m1 = 0
6253 then (Wsi.namekey s, m)
6254 else (k, m lor m1)
6255 in function
6256 | Str.Delim s when n land 1 = 0 -> g s
6257 | Str.Text s -> g s
6258 | Str.Delim _ -> (k, m)
6260 let rec loop n k m = function
6261 | [] -> (k, m)
6262 | x :: xs ->
6263 let k, m = f n k m x in
6264 loop (n+1) k m xs
6266 loop 0 0 0 elems
6269 let keys_of_string =
6270 let r = Str.regexp "[ \t]" in
6271 fun s ->
6272 let elems = Str.split r s in
6273 List.map key_of_string elems
6276 let copykeyhashes c =
6277 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6280 let config_of c attrs =
6281 let apply c k v =
6283 match k with
6284 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6285 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6286 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6287 | "preload" -> { c with preload = bool_of_string v }
6288 | "page-bias" -> { c with pagebias = int_of_string v }
6289 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6290 | "horizontal-scroll-step" ->
6291 { c with hscrollstep = max (int_of_string v) 1 }
6292 | "auto-scroll-step" ->
6293 { c with autoscrollstep = max 0 (int_of_string v) }
6294 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6295 | "crop-hack" -> { c with crophack = bool_of_string v }
6296 | "throttle" ->
6297 let mw =
6298 match String.lowercase v with
6299 | "true" -> Some infinity
6300 | "false" -> None
6301 | f -> Some (float_of_string f)
6303 { c with maxwait = mw}
6304 | "highlight-links" -> { c with hlinks = bool_of_string v }
6305 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6306 | "vertical-margin" ->
6307 { c with interpagespace = max 0 (int_of_string v) }
6308 | "zoom" ->
6309 let zoom = float_of_string v /. 100. in
6310 let zoom = max zoom 0.0 in
6311 { c with zoom = zoom }
6312 | "presentation" -> { c with presentation = bool_of_string v }
6313 | "rotation-angle" -> { c with angle = int_of_string v }
6314 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6315 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6316 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6317 | "proportional-display" ->
6318 let fm =
6319 if bool_of_string v
6320 then FitProportional
6321 else FitWidth
6323 { c with fitmodel = fm }
6324 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6325 | "pixmap-cache-size" ->
6326 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6327 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6328 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6329 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6330 | "persistent-location" -> { c with jumpback = bool_of_string v }
6331 | "background-color" -> { c with bgcolor = color_of_string v }
6332 | "scrollbar-in-presentation" ->
6333 { c with scrollbarinpm = bool_of_string v }
6334 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6335 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6336 | "mupdf-store-size" ->
6337 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6338 | "checkers" -> { c with checkers = bool_of_string v }
6339 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6340 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6341 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6342 | "uri-launcher" -> { c with urilauncher = unent v }
6343 | "path-launcher" -> { c with pathlauncher = unent v }
6344 | "color-space" -> { c with colorspace = colorspace_of_string v }
6345 | "invert-colors" -> { c with invert = bool_of_string v }
6346 | "brightness" -> { c with colorscale = float_of_string v }
6347 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6348 | "ghyllscroll" ->
6349 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6350 | "columns" ->
6351 let (n, _, _) as nab = multicolumns_of_string v in
6352 if n < 0
6353 then { c with columns = Csplit (-n, [||]) }
6354 else { c with columns = Cmulti (nab, [||]) }
6355 | "birds-eye-columns" ->
6356 { c with beyecolumns = Some (max (int_of_string v) 2) }
6357 | "selection-command" -> { c with selcmd = unent v }
6358 | "synctex-command" -> { c with stcmd = unent v }
6359 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6360 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6361 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6362 | "use-pbo" -> { c with usepbo = bool_of_string v }
6363 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6364 | _ -> c
6365 with exn ->
6366 prerr_endline ("Error processing attribute (`" ^
6367 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6370 let rec fold c = function
6371 | [] -> c
6372 | (k, v) :: rest ->
6373 let c = apply c k v in
6374 fold c rest
6376 fold { c with keyhashes = copykeyhashes c } attrs;
6379 let fromstring f pos n v d =
6380 try f v
6381 with exn ->
6382 dolog "Error processing attribute (%S=%S) at %d\n%s"
6383 n v pos (exntos exn)
6388 let bookmark_of attrs =
6389 let rec fold title page rely visy = function
6390 | ("title", v) :: rest -> fold v page rely visy rest
6391 | ("page", v) :: rest -> fold title v rely visy rest
6392 | ("rely", v) :: rest -> fold title page v visy rest
6393 | ("visy", v) :: rest -> fold title page rely v rest
6394 | _ :: rest -> fold title page rely visy rest
6395 | [] -> title, page, rely, visy
6397 fold "invalid" "0" "0" "0" attrs
6400 let doc_of attrs =
6401 let rec fold path page rely pan visy = function
6402 | ("path", v) :: rest -> fold v page rely pan visy rest
6403 | ("page", v) :: rest -> fold path v rely pan visy rest
6404 | ("rely", v) :: rest -> fold path page v pan visy rest
6405 | ("pan", v) :: rest -> fold path page rely v visy rest
6406 | ("visy", v) :: rest -> fold path page rely pan v rest
6407 | _ :: rest -> fold path page rely pan visy rest
6408 | [] -> path, page, rely, pan, visy
6410 fold "" "0" "0" "0" "0" attrs
6413 let map_of attrs =
6414 let rec fold rs ls = function
6415 | ("out", v) :: rest -> fold v ls rest
6416 | ("in", v) :: rest -> fold rs v rest
6417 | _ :: rest -> fold ls rs rest
6418 | [] -> ls, rs
6420 fold "" "" attrs
6423 let setconf dst src =
6424 dst.scrollbw <- src.scrollbw;
6425 dst.scrollh <- src.scrollh;
6426 dst.icase <- src.icase;
6427 dst.preload <- src.preload;
6428 dst.pagebias <- src.pagebias;
6429 dst.verbose <- src.verbose;
6430 dst.scrollstep <- src.scrollstep;
6431 dst.maxhfit <- src.maxhfit;
6432 dst.crophack <- src.crophack;
6433 dst.autoscrollstep <- src.autoscrollstep;
6434 dst.maxwait <- src.maxwait;
6435 dst.hlinks <- src.hlinks;
6436 dst.underinfo <- src.underinfo;
6437 dst.interpagespace <- src.interpagespace;
6438 dst.zoom <- src.zoom;
6439 dst.presentation <- src.presentation;
6440 dst.angle <- src.angle;
6441 dst.cwinw <- src.cwinw;
6442 dst.cwinh <- src.cwinh;
6443 dst.savebmarks <- src.savebmarks;
6444 dst.memlimit <- src.memlimit;
6445 dst.fitmodel <- src.fitmodel;
6446 dst.texcount <- src.texcount;
6447 dst.sliceheight <- src.sliceheight;
6448 dst.thumbw <- src.thumbw;
6449 dst.jumpback <- src.jumpback;
6450 dst.bgcolor <- src.bgcolor;
6451 dst.scrollbarinpm <- src.scrollbarinpm;
6452 dst.tilew <- src.tilew;
6453 dst.tileh <- src.tileh;
6454 dst.mustoresize <- src.mustoresize;
6455 dst.checkers <- src.checkers;
6456 dst.aalevel <- src.aalevel;
6457 dst.trimmargins <- src.trimmargins;
6458 dst.trimfuzz <- src.trimfuzz;
6459 dst.urilauncher <- src.urilauncher;
6460 dst.colorspace <- src.colorspace;
6461 dst.invert <- src.invert;
6462 dst.colorscale <- src.colorscale;
6463 dst.redirectstderr <- src.redirectstderr;
6464 dst.ghyllscroll <- src.ghyllscroll;
6465 dst.columns <- src.columns;
6466 dst.beyecolumns <- src.beyecolumns;
6467 dst.selcmd <- src.selcmd;
6468 dst.updatecurs <- src.updatecurs;
6469 dst.pathlauncher <- src.pathlauncher;
6470 dst.keyhashes <- copykeyhashes src;
6471 dst.hfsize <- src.hfsize;
6472 dst.hscrollstep <- src.hscrollstep;
6473 dst.pgscale <- src.pgscale;
6474 dst.usepbo <- src.usepbo;
6475 dst.wheelbypage <- src.wheelbypage;
6476 dst.stcmd <- src.stcmd;
6479 let get s =
6480 let h = Hashtbl.create 10 in
6481 let dc = { defconf with angle = defconf.angle } in
6482 let rec toplevel v t spos _ =
6483 match t with
6484 | Vdata | Vcdata | Vend -> v
6485 | Vopen ("llppconfig", _, closed) ->
6486 if closed
6487 then v
6488 else { v with f = llppconfig }
6489 | Vopen _ ->
6490 error "unexpected subelement at top level" s spos
6491 | Vclose _ -> error "unexpected close at top level" s spos
6493 and llppconfig v t spos _ =
6494 match t with
6495 | Vdata | Vcdata -> v
6496 | Vend -> error "unexpected end of input in llppconfig" s spos
6497 | Vopen ("defaults", attrs, closed) ->
6498 let c = config_of dc attrs in
6499 setconf dc c;
6500 if closed
6501 then v
6502 else { v with f = defaults }
6504 | Vopen ("ui-font", attrs, closed) ->
6505 let rec getsize size = function
6506 | [] -> size
6507 | ("size", v) :: rest ->
6508 let size =
6509 fromstring int_of_string spos "size" v fstate.fontsize in
6510 getsize size rest
6511 | l -> getsize size l
6513 fstate.fontsize <- getsize fstate.fontsize attrs;
6514 if closed
6515 then v
6516 else { v with f = uifont (Buffer.create 10) }
6518 | Vopen ("doc", attrs, closed) ->
6519 let pathent, spage, srely, span, svisy = doc_of attrs in
6520 let path = unent pathent
6521 and pageno = fromstring int_of_string spos "page" spage 0
6522 and rely = fromstring float_of_string spos "rely" srely 0.0
6523 and pan = fromstring int_of_string spos "pan" span 0
6524 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6525 let c = config_of dc attrs in
6526 let anchor = (pageno, rely, visy) in
6527 if closed
6528 then (Hashtbl.add h path (c, [], pan, anchor); v)
6529 else { v with f = doc path pan anchor c [] }
6531 | Vopen _ ->
6532 error "unexpected subelement in llppconfig" s spos
6534 | Vclose "llppconfig" -> { v with f = toplevel }
6535 | Vclose _ -> error "unexpected close in llppconfig" s spos
6537 and defaults v t spos _ =
6538 match t with
6539 | Vdata | Vcdata -> v
6540 | Vend -> error "unexpected end of input in defaults" s spos
6541 | Vopen ("keymap", attrs, closed) ->
6542 let modename =
6543 try List.assoc "mode" attrs
6544 with Not_found -> "global" in
6545 if closed
6546 then v
6547 else
6548 let ret keymap =
6549 let h = findkeyhash dc modename in
6550 KeyMap.iter (Hashtbl.replace h) keymap;
6551 defaults
6553 { v with f = pkeymap ret KeyMap.empty }
6555 | Vopen (_, _, _) ->
6556 error "unexpected subelement in defaults" s spos
6558 | Vclose "defaults" ->
6559 { v with f = llppconfig }
6561 | Vclose _ -> error "unexpected close in defaults" s spos
6563 and uifont b v t spos epos =
6564 match t with
6565 | Vdata | Vcdata ->
6566 Buffer.add_substring b s spos (epos - spos);
6568 | Vopen (_, _, _) ->
6569 error "unexpected subelement in ui-font" s spos
6570 | Vclose "ui-font" ->
6571 if String.length !fontpath = 0
6572 then fontpath := Buffer.contents b;
6573 { v with f = llppconfig }
6574 | Vclose _ -> error "unexpected close in ui-font" s spos
6575 | Vend -> error "unexpected end of input in ui-font" s spos
6577 and doc path pan anchor c bookmarks v t spos _ =
6578 match t with
6579 | Vdata | Vcdata -> v
6580 | Vend -> error "unexpected end of input in doc" s spos
6581 | Vopen ("bookmarks", _, closed) ->
6582 if closed
6583 then v
6584 else { v with f = pbookmarks path pan anchor c bookmarks }
6586 | Vopen ("keymap", attrs, closed) ->
6587 let modename =
6588 try List.assoc "mode" attrs
6589 with Not_found -> "global"
6591 if closed
6592 then v
6593 else
6594 let ret keymap =
6595 let h = findkeyhash c modename in
6596 KeyMap.iter (Hashtbl.replace h) keymap;
6597 doc path pan anchor c bookmarks
6599 { v with f = pkeymap ret KeyMap.empty }
6601 | Vopen (_, _, _) ->
6602 error "unexpected subelement in doc" s spos
6604 | Vclose "doc" ->
6605 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6606 { v with f = llppconfig }
6608 | Vclose _ -> error "unexpected close in doc" s spos
6610 and pkeymap ret keymap v t spos _ =
6611 match t with
6612 | Vdata | Vcdata -> v
6613 | Vend -> error "unexpected end of input in keymap" s spos
6614 | Vopen ("map", attrs, closed) ->
6615 let r, l = map_of attrs in
6616 let kss = fromstring keys_of_string spos "in" r [] in
6617 let lss = fromstring keys_of_string spos "out" l [] in
6618 let keymap =
6619 match kss with
6620 | [] -> keymap
6621 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6622 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6624 if closed
6625 then { v with f = pkeymap ret keymap }
6626 else
6627 let f () = v in
6628 { v with f = skip "map" f }
6630 | Vopen _ ->
6631 error "unexpected subelement in keymap" s spos
6633 | Vclose "keymap" ->
6634 { v with f = ret keymap }
6636 | Vclose _ -> error "unexpected close in keymap" s spos
6638 and pbookmarks path pan anchor c bookmarks v t spos _ =
6639 match t with
6640 | Vdata | Vcdata -> v
6641 | Vend -> error "unexpected end of input in bookmarks" s spos
6642 | Vopen ("item", attrs, closed) ->
6643 let titleent, spage, srely, svisy = bookmark_of attrs in
6644 let page = fromstring int_of_string spos "page" spage 0
6645 and rely = fromstring float_of_string spos "rely" srely 0.0
6646 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6647 let bookmarks =
6648 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6650 if closed
6651 then { v with f = pbookmarks path pan anchor c bookmarks }
6652 else
6653 let f () = v in
6654 { v with f = skip "item" f }
6656 | Vopen _ ->
6657 error "unexpected subelement in bookmarks" s spos
6659 | Vclose "bookmarks" ->
6660 { v with f = doc path pan anchor c bookmarks }
6662 | Vclose _ -> error "unexpected close in bookmarks" s spos
6664 and skip tag f v t spos _ =
6665 match t with
6666 | Vdata | Vcdata -> v
6667 | Vend ->
6668 error ("unexpected end of input in skipped " ^ tag) s spos
6669 | Vopen (tag', _, closed) ->
6670 if closed
6671 then v
6672 else
6673 let f' () = { v with f = skip tag f } in
6674 { v with f = skip tag' f' }
6675 | Vclose ctag ->
6676 if tag = ctag
6677 then f ()
6678 else error ("unexpected close in skipped " ^ tag) s spos
6681 parse { f = toplevel; accu = () } s;
6682 h, dc;
6685 let do_load f ic =
6687 let len = in_channel_length ic in
6688 let s = String.create len in
6689 really_input ic s 0 len;
6690 f s;
6691 with
6692 | Parse_error (msg, s, pos) ->
6693 let subs = subs s pos in
6694 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6695 failwith ("parse error: " ^ s)
6697 | exn ->
6698 failwith ("config load error: " ^ exntos exn)
6701 let defconfpath =
6702 let dir =
6704 let dir = Filename.concat home ".config" in
6705 if Sys.is_directory dir then dir else home
6706 with _ -> home
6708 Filename.concat dir "llpp.conf"
6711 let confpath = ref defconfpath;;
6713 let load1 f =
6714 if Sys.file_exists !confpath
6715 then
6716 match
6717 (try Some (open_in_bin !confpath)
6718 with exn ->
6719 prerr_endline
6720 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6721 exntos exn);
6722 None
6724 with
6725 | Some ic ->
6726 let success =
6728 f (do_load get ic)
6729 with exn ->
6730 prerr_endline
6731 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6732 exntos exn);
6733 false
6735 close_in ic;
6736 success
6738 | None -> false
6739 else
6740 f (Hashtbl.create 0, defconf)
6743 let load () =
6744 let f (h, dc) =
6745 let pc, pb, px, pa =
6747 Hashtbl.find h (Filename.basename state.path)
6748 with Not_found -> dc, [], 0, emptyanchor
6750 setconf defconf dc;
6751 setconf conf pc;
6752 state.bookmarks <- pb;
6753 state.x <- px;
6754 state.scrollw <- conf.scrollbw;
6755 if conf.jumpback
6756 then state.anchor <- pa;
6757 cbput state.hists.nav pa;
6758 true
6760 load1 f
6763 let add_attrs bb always dc c =
6764 let ob s a b =
6765 if always || a != b
6766 then Printf.bprintf bb "\n %s='%b'" s a
6767 and oi s a b =
6768 if always || a != b
6769 then Printf.bprintf bb "\n %s='%d'" s a
6770 and oI s a b =
6771 if always || a != b
6772 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6773 and oz s a b =
6774 if always || a <> b
6775 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6776 and oF s a b =
6777 if always || a <> b
6778 then Printf.bprintf bb "\n %s='%f'" s a
6779 and oc s a b =
6780 if always || a <> b
6781 then
6782 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6783 and oC s a b =
6784 if always || a <> b
6785 then
6786 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6787 and oR s a b =
6788 if always || a <> b
6789 then
6790 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6791 and os s a b =
6792 if always || a <> b
6793 then
6794 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6795 and og s a b =
6796 if always || a <> b
6797 then
6798 match a with
6799 | None -> ()
6800 | Some (_N, _A, _B) ->
6801 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6802 and oW s a b =
6803 if always || a <> b
6804 then
6805 let v =
6806 match a with
6807 | None -> "false"
6808 | Some f ->
6809 if f = infinity
6810 then "true"
6811 else string_of_float f
6813 Printf.bprintf bb "\n %s='%s'" s v
6814 and oco s a b =
6815 if always || a <> b
6816 then
6817 match a with
6818 | Cmulti ((n, a, b), _) when n > 1 ->
6819 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6820 | Csplit (n, _) when n > 1 ->
6821 Printf.bprintf bb "\n %s='%d'" s ~-n
6822 | _ -> ()
6823 and obeco s a b =
6824 if always || a <> b
6825 then
6826 match a with
6827 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6828 | _ -> ()
6829 and oFm s a b =
6830 if always || a <> b
6831 then
6832 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6834 oi "width" c.cwinw dc.cwinw;
6835 oi "height" c.cwinh dc.cwinh;
6836 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6837 oi "scroll-handle-height" c.scrollh dc.scrollh;
6838 ob "case-insensitive-search" c.icase dc.icase;
6839 ob "preload" c.preload dc.preload;
6840 oi "page-bias" c.pagebias dc.pagebias;
6841 oi "scroll-step" c.scrollstep dc.scrollstep;
6842 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6843 ob "max-height-fit" c.maxhfit dc.maxhfit;
6844 ob "crop-hack" c.crophack dc.crophack;
6845 oW "throttle" c.maxwait dc.maxwait;
6846 ob "highlight-links" c.hlinks dc.hlinks;
6847 ob "under-cursor-info" c.underinfo dc.underinfo;
6848 oi "vertical-margin" c.interpagespace dc.interpagespace;
6849 oz "zoom" c.zoom dc.zoom;
6850 ob "presentation" c.presentation dc.presentation;
6851 oi "rotation-angle" c.angle dc.angle;
6852 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6853 oFm "fit-model" c.fitmodel dc.fitmodel;
6854 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6855 oi "tex-count" c.texcount dc.texcount;
6856 oi "slice-height" c.sliceheight dc.sliceheight;
6857 oi "thumbnail-width" c.thumbw dc.thumbw;
6858 ob "persistent-location" c.jumpback dc.jumpback;
6859 oc "background-color" c.bgcolor dc.bgcolor;
6860 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6861 oi "tile-width" c.tilew dc.tilew;
6862 oi "tile-height" c.tileh dc.tileh;
6863 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6864 ob "checkers" c.checkers dc.checkers;
6865 oi "aalevel" c.aalevel dc.aalevel;
6866 ob "trim-margins" c.trimmargins dc.trimmargins;
6867 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6868 os "uri-launcher" c.urilauncher dc.urilauncher;
6869 os "path-launcher" c.pathlauncher dc.pathlauncher;
6870 oC "color-space" c.colorspace dc.colorspace;
6871 ob "invert-colors" c.invert dc.invert;
6872 oF "brightness" c.colorscale dc.colorscale;
6873 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6874 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6875 oco "columns" c.columns dc.columns;
6876 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6877 os "selection-command" c.selcmd dc.selcmd;
6878 os "synctex-command" c.stcmd dc.stcmd;
6879 ob "update-cursor" c.updatecurs dc.updatecurs;
6880 oi "hint-font-size" c.hfsize dc.hfsize;
6881 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6882 oF "page-scroll-scale" c.pgscale dc.pgscale;
6883 ob "use-pbo" c.usepbo dc.usepbo;
6884 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6887 let keymapsbuf always dc c =
6888 let bb = Buffer.create 16 in
6889 let rec loop = function
6890 | [] -> ()
6891 | (modename, h) :: rest ->
6892 let dh = findkeyhash dc modename in
6893 if always || h <> dh
6894 then (
6895 if Hashtbl.length h > 0
6896 then (
6897 if Buffer.length bb > 0
6898 then Buffer.add_char bb '\n';
6899 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6900 Hashtbl.iter (fun i o ->
6901 let isdifferent = always ||
6903 let dO = Hashtbl.find dh i in
6904 dO <> o
6905 with Not_found -> true
6907 if isdifferent
6908 then
6909 let addkm (k, m) =
6910 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6911 if Wsi.withalt m then Buffer.add_string bb "alt-";
6912 if Wsi.withshift m then Buffer.add_string bb "shift-";
6913 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6914 Buffer.add_string bb (Wsi.keyname k);
6916 let addkms l =
6917 let rec loop = function
6918 | [] -> ()
6919 | km :: [] -> addkm km
6920 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6922 loop l
6924 Buffer.add_string bb "<map in='";
6925 addkm i;
6926 match o with
6927 | KMinsrt km ->
6928 Buffer.add_string bb "' out='";
6929 addkm km;
6930 Buffer.add_string bb "'/>\n"
6932 | KMinsrl kms ->
6933 Buffer.add_string bb "' out='";
6934 addkms kms;
6935 Buffer.add_string bb "'/>\n"
6937 | KMmulti (ins, kms) ->
6938 Buffer.add_char bb ' ';
6939 addkms ins;
6940 Buffer.add_string bb "' out='";
6941 addkms kms;
6942 Buffer.add_string bb "'/>\n"
6943 ) h;
6944 Buffer.add_string bb "</keymap>";
6947 loop rest
6949 loop c.keyhashes;
6953 let save () =
6954 let uifontsize = fstate.fontsize in
6955 let bb = Buffer.create 32768 in
6956 let w, h =
6957 List.fold_left
6958 (fun (w, h) ws ->
6959 match ws with
6960 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6961 | Wsi.MaxVert -> (w, conf.cwinh)
6962 | Wsi.MaxHorz -> (conf.cwinw, h)
6964 (state.winw, state.winh) state.winstate
6966 conf.cwinw <- w;
6967 conf.cwinh <- h;
6968 let f (h, dc) =
6969 let dc = if conf.bedefault then conf else dc in
6970 Buffer.add_string bb "<llppconfig>\n";
6972 if String.length !fontpath > 0
6973 then
6974 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6975 uifontsize
6976 !fontpath
6977 else (
6978 if uifontsize <> 14
6979 then
6980 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6983 Buffer.add_string bb "<defaults ";
6984 add_attrs bb true dc dc;
6985 let kb = keymapsbuf true dc dc in
6986 if Buffer.length kb > 0
6987 then (
6988 Buffer.add_string bb ">\n";
6989 Buffer.add_buffer bb kb;
6990 Buffer.add_string bb "\n</defaults>\n";
6992 else Buffer.add_string bb "/>\n";
6994 let adddoc path pan anchor c bookmarks =
6995 if bookmarks == [] && c = dc && anchor = emptyanchor
6996 then ()
6997 else (
6998 Printf.bprintf bb "<doc path='%s'"
6999 (enent path 0 (String.length path));
7001 if anchor <> emptyanchor
7002 then (
7003 let n, rely, visy = anchor in
7004 Printf.bprintf bb " page='%d'" n;
7005 if rely > 1e-6
7006 then
7007 Printf.bprintf bb " rely='%f'" rely
7009 if abs_float visy > 1e-6
7010 then
7011 Printf.bprintf bb " visy='%f'" visy
7015 if pan != 0
7016 then Printf.bprintf bb " pan='%d'" pan;
7018 add_attrs bb false dc c;
7019 let kb = keymapsbuf false dc c in
7021 begin match bookmarks with
7022 | [] ->
7023 if Buffer.length kb > 0
7024 then (
7025 Buffer.add_string bb ">\n";
7026 Buffer.add_buffer bb kb;
7027 Buffer.add_string bb "\n</doc>\n";
7029 else Buffer.add_string bb "/>\n"
7030 | _ ->
7031 Buffer.add_string bb ">\n<bookmarks>\n";
7032 List.iter (fun (title, _level, (page, rely, visy)) ->
7033 Printf.bprintf bb
7034 "<item title='%s' page='%d'"
7035 (enent title 0 (String.length title))
7036 page
7038 if rely > 1e-6
7039 then
7040 Printf.bprintf bb " rely='%f'" rely
7042 if abs_float visy > 1e-6
7043 then
7044 Printf.bprintf bb " visy='%f'" visy
7046 Buffer.add_string bb "/>\n";
7047 ) bookmarks;
7048 Buffer.add_string bb "</bookmarks>";
7049 if Buffer.length kb > 0
7050 then (
7051 Buffer.add_string bb "\n";
7052 Buffer.add_buffer bb kb;
7054 Buffer.add_string bb "\n</doc>\n";
7055 end;
7059 let pan, conf =
7060 match state.mode with
7061 | Birdseye (c, pan, _, _, _) ->
7062 let beyecolumns =
7063 match conf.columns with
7064 | Cmulti ((c, _, _), _) -> Some c
7065 | Csingle _ -> None
7066 | Csplit _ -> None
7067 and columns =
7068 match c.columns with
7069 | Cmulti (c, _) -> Cmulti (c, [||])
7070 | Csingle _ -> Csingle [||]
7071 | Csplit _ -> failwith "quit from bird's eye while split"
7073 pan, { c with beyecolumns = beyecolumns; columns = columns }
7074 | _ -> state.x, conf
7076 let basename = Filename.basename state.path in
7077 adddoc basename pan (getanchor ())
7078 (let conf =
7079 let autoscrollstep =
7080 match state.autoscroll with
7081 | Some step -> step
7082 | None -> conf.autoscrollstep
7084 match state.mode with
7085 | Birdseye (bc, _, _, _, _) ->
7086 { conf with
7087 zoom = bc.zoom;
7088 presentation = bc.presentation;
7089 interpagespace = bc.interpagespace;
7090 maxwait = bc.maxwait;
7091 autoscrollstep = autoscrollstep }
7092 | _ -> { conf with autoscrollstep = autoscrollstep }
7093 in conf)
7094 (if conf.savebmarks then state.bookmarks else []);
7096 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7097 if basename <> path
7098 then adddoc path x anchor c bookmarks
7099 ) h;
7100 Buffer.add_string bb "</llppconfig>\n";
7101 true;
7103 if load1 f && Buffer.length bb > 0
7104 then
7106 let tmp = !confpath ^ ".tmp" in
7107 let oc = open_out_bin tmp in
7108 Buffer.output_buffer oc bb;
7109 close_out oc;
7110 Unix.rename tmp !confpath;
7111 with exn ->
7112 prerr_endline
7113 ("error while saving configuration: " ^ exntos exn)
7115 end;;
7117 let adderrmsg src msg =
7118 Buffer.add_string state.errmsgs msg;
7119 state.newerrmsgs <- true;
7120 G.postRedisplay src
7123 let adderrfmt src fmt =
7124 Format.kprintf (fun s -> adderrmsg src s) fmt;
7127 let ract cmds =
7128 let cl = splitatspace cmds in
7129 let scan s fmt f =
7130 try Scanf.sscanf s fmt f
7131 with exn ->
7132 adderrfmt "remote exec"
7133 "error processing '%S': %s\n" cmds (exntos exn)
7135 match cl with
7136 | "reload" :: [] -> reload ()
7137 | "goto" :: args :: [] ->
7138 scan args "%u %f %f"
7139 (fun pageno x y ->
7140 let cmd, _ = state.geomcmds in
7141 if String.length cmd = 0
7142 then gotopagexy pageno x y
7143 else
7144 let f prevf () =
7145 gotopagexy pageno x y;
7146 prevf ()
7148 state.reprf <- f state.reprf
7150 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7151 | "rect" :: args :: [] ->
7152 scan args "%u %u %f %f %f %f"
7153 (fun pageno color x0 y0 x1 y1 ->
7154 onpagerect pageno (fun w h ->
7155 let _,w1,h1,_ = getpagedim pageno in
7156 let sw = float w1 /. w
7157 and sh = float h1 /. h in
7158 let x0s = x0 *. sw
7159 and x1s = x1 *. sw
7160 and y0s = y0 *. sh
7161 and y1s = y1 *. sh in
7162 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7163 debugrect rect;
7164 state.rects <- (pageno, color, rect) :: state.rects;
7165 G.postRedisplay "rect";
7168 | "activatewin" :: [] -> Wsi.activatewin ()
7169 | "quit" :: [] -> raise Quit
7170 | _ ->
7171 adderrfmt "remote command"
7172 "error processing remote command: %S\n" cmds;
7175 let remote =
7176 let scratch = String.create 80 in
7177 let buf = Buffer.create 80 in
7178 fun fd ->
7179 let rec tempfr () =
7180 try Some (Unix.read fd scratch 0 80)
7181 with
7182 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7183 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7184 | exn -> raise exn
7186 match tempfr () with
7187 | None -> Some fd
7188 | Some n ->
7189 if n = 0
7190 then (
7191 Unix.close fd;
7192 if Buffer.length buf > 0
7193 then (
7194 let s = Buffer.contents buf in
7195 Buffer.clear buf;
7196 ract s;
7198 None
7200 else
7201 let rec eat ppos =
7202 let nlpos =
7204 let pos = String.index_from scratch ppos '\n' in
7205 if pos >= n then -1 else pos
7206 with Not_found -> -1
7208 if nlpos >= 0
7209 then (
7210 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7211 let s = Buffer.contents buf in
7212 Buffer.clear buf;
7213 ract s;
7214 eat (nlpos+1);
7216 else (
7217 Buffer.add_substring buf scratch ppos (n-ppos);
7218 Some fd
7220 in eat 0
7223 let remoteopen path =
7224 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7225 with exn ->
7226 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7227 None
7230 let () =
7231 let trimcachepath = ref "" in
7232 let rcmdpath = ref "" in
7233 Arg.parse
7234 (Arg.align
7235 [("-p", Arg.String (fun s -> state.password <- s),
7236 "<password> Set password");
7238 ("-f", Arg.String (fun s -> Config.fontpath := s),
7239 "<path> Set path to the user interface font");
7241 ("-c", Arg.String (fun s -> Config.confpath := s),
7242 "<path> Set path to the configuration file");
7244 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7245 "<path> Set path to the trim cache file");
7247 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7248 "<named-destination> Set named destination");
7250 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7252 ("-remote", Arg.String (fun s -> rcmdpath := s),
7253 "<path> Set path to the remote commands source");
7255 ("-v", Arg.Unit (fun () ->
7256 Printf.printf
7257 "%s\nconfiguration path: %s\n"
7258 (version ())
7259 Config.defconfpath
7261 exit 0), " Print version and exit");
7264 (fun s -> state.path <- s)
7265 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7267 if String.length state.path = 0
7268 then (prerr_endline "file name missing"; exit 1);
7270 if not (Config.load ())
7271 then prerr_endline "failed to load configuration";
7273 let globalkeyhash = findkeyhash conf "global" in
7274 let wsfd, winw, winh = Wsi.init (object
7275 method expose =
7276 if nogeomcmds state.geomcmds || platform == Posx
7277 then display ()
7278 else (
7279 GlClear.color (scalecolor2 conf.bgcolor);
7280 GlClear.clear [`color];
7282 method display = display ()
7283 method reshape w h = reshape w h
7284 method mouse b d x y m = mouse b d x y m
7285 method motion x y = state.mpos <- (x, y); motion x y
7286 method pmotion x y = state.mpos <- (x, y); pmotion x y
7287 method key k m =
7288 let mascm = m land (
7289 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7290 ) in
7291 match state.keystate with
7292 | KSnone ->
7293 let km = k, mascm in
7294 begin
7295 match
7296 let modehash = state.uioh#modehash in
7297 try Hashtbl.find modehash km
7298 with Not_found ->
7299 try Hashtbl.find globalkeyhash km
7300 with Not_found -> KMinsrt (k, m)
7301 with
7302 | KMinsrt (k, m) -> keyboard k m
7303 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7304 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7306 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7307 List.iter (fun (k, m) -> keyboard k m) insrt;
7308 state.keystate <- KSnone
7309 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7310 state.keystate <- KSinto (keys, insrt)
7311 | _ ->
7312 state.keystate <- KSnone
7314 method enter x y = state.mpos <- (x, y); pmotion x y
7315 method leave = state.mpos <- (-1, -1)
7316 method winstate wsl = state.winstate <- wsl
7317 method quit = raise Quit
7318 end) conf.cwinw conf.cwinh (platform = Posx) in
7320 state.wsfd <- wsfd;
7322 if not (
7323 List.exists GlMisc.check_extension
7324 [ "GL_ARB_texture_rectangle"
7325 ; "GL_EXT_texture_recangle"
7326 ; "GL_NV_texture_rectangle" ]
7328 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7330 let cr, sw =
7331 match Ne.pipe () with
7332 | Ne.Exn exn ->
7333 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7334 exit 1
7335 | Ne.Res rw -> rw
7336 and sr, cw =
7337 match Ne.pipe () with
7338 | Ne.Exn exn ->
7339 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7340 exit 1
7341 | Ne.Res rw -> rw
7344 cloexec cr;
7345 cloexec sw;
7346 cloexec sr;
7347 cloexec cw;
7349 setcheckers conf.checkers;
7350 redirectstderr ();
7352 init (cr, cw) (
7353 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7354 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7355 !Config.fontpath, !trimcachepath,
7356 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7358 state.sr <- sr;
7359 state.sw <- sw;
7360 state.text <- "Opening " ^ (mbtoutf8 state.path);
7361 reshape winw winh;
7362 opendoc state.path state.password;
7363 state.uioh <- uioh;
7365 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7366 let optrfd =
7367 ref (
7368 if String.length !rcmdpath > 0
7369 then remoteopen !rcmdpath
7370 else None
7374 let rec loop deadline =
7375 let r =
7376 match state.errfd with
7377 | None -> [state.sr; state.wsfd]
7378 | Some fd -> [state.sr; state.wsfd; fd]
7380 let r =
7381 match !optrfd with
7382 | None -> r
7383 | Some fd -> fd :: r
7385 if state.redisplay
7386 then (
7387 state.redisplay <- false;
7388 display ();
7390 let timeout =
7391 let now = now () in
7392 if deadline > now
7393 then (
7394 if deadline = infinity
7395 then ~-.1.0
7396 else max 0.0 (deadline -. now)
7398 else 0.0
7400 let r, _, _ =
7401 try Unix.select r [] [] timeout
7402 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7404 begin match r with
7405 | [] ->
7406 state.ghyll None;
7407 let newdeadline =
7408 if state.ghyll == noghyll
7409 then
7410 match state.autoscroll with
7411 | Some step when step != 0 ->
7412 let y = state.y + step in
7413 let y =
7414 if y < 0
7415 then state.maxy
7416 else if y >= state.maxy then 0 else y
7418 gotoy y;
7419 if state.mode = View
7420 then state.text <- "";
7421 deadline +. 0.01
7422 | _ -> infinity
7423 else deadline +. 0.01
7425 loop newdeadline
7427 | l ->
7428 let rec checkfds = function
7429 | [] -> ()
7430 | fd :: rest when fd = state.sr ->
7431 let cmd = readcmd state.sr in
7432 act cmd;
7433 checkfds rest
7435 | fd :: rest when fd = state.wsfd ->
7436 Wsi.readresp fd;
7437 checkfds rest
7439 | fd :: rest when Some fd = !optrfd ->
7440 begin match remote fd with
7441 | None -> optrfd := remoteopen !rcmdpath;
7442 | opt -> optrfd := opt
7443 end;
7444 checkfds rest
7446 | fd :: rest ->
7447 let s = String.create 80 in
7448 let n = tempfailureretry (Unix.read fd s 0) 80 in
7449 if conf.redirectstderr
7450 then (
7451 Buffer.add_substring state.errmsgs s 0 n;
7452 state.newerrmsgs <- true;
7453 state.redisplay <- true;
7455 else (
7456 prerr_string (String.sub s 0 n);
7457 flush stderr;
7459 checkfds rest
7461 checkfds l;
7462 let newdeadline =
7463 let deadline1 =
7464 if deadline = infinity
7465 then now () +. 0.01
7466 else deadline
7468 match state.autoscroll with
7469 | Some step when step != 0 -> deadline1
7470 | _ -> if state.ghyll == noghyll then infinity else deadline1
7472 loop newdeadline
7473 end;
7476 loop infinity;
7477 with Quit ->
7478 Config.save ();