Handle compressed pdfs properly, do not cache erroneous things
[llpp.git] / main.ml
blob731c6b8dd8e5e5121ae4fd7f8dc132c00e39cb96
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)
484 ; mutable origin : string
486 and hists =
487 { pat : string circbuf
488 ; pag : string circbuf
489 ; nav : anchor circbuf
490 ; sel : string circbuf
494 let defconf =
495 { scrollbw = 7
496 ; scrollh = 12
497 ; icase = true
498 ; preload = true
499 ; pagebias = 0
500 ; verbose = false
501 ; debug = false
502 ; scrollstep = 24
503 ; hscrollstep = 24
504 ; maxhfit = true
505 ; crophack = false
506 ; autoscrollstep = 2
507 ; maxwait = None
508 ; hlinks = false
509 ; underinfo = false
510 ; interpagespace = 2
511 ; zoom = 1.0
512 ; presentation = false
513 ; angle = 0
514 ; cwinw = 900
515 ; cwinh = 900
516 ; savebmarks = true
517 ; fitmodel = FitProportional
518 ; trimmargins = false
519 ; trimfuzz = (0,0,0,0)
520 ; memlimit = 32 lsl 20
521 ; texcount = 256
522 ; sliceheight = 24
523 ; thumbw = 76
524 ; jumpback = true
525 ; bgcolor = (0.5, 0.5, 0.5)
526 ; bedefault = false
527 ; scrollbarinpm = true
528 ; tilew = 2048
529 ; tileh = 2048
530 ; mustoresize = 256 lsl 20
531 ; checkers = true
532 ; aalevel = 8
533 ; urilauncher =
534 (match platform with
535 | Plinux | Pfreebsd | Pdragonflybsd
536 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
537 | Posx -> "open \"%s\""
538 | Pcygwin -> "cygstart \"%s\""
539 | Punknown -> "echo %s")
540 ; pathlauncher = "lp \"%s\""
541 ; selcmd =
542 (match platform with
543 | Plinux | Pfreebsd | Pdragonflybsd
544 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
545 | Posx -> "pbcopy"
546 | Pcygwin -> "wsel"
547 | Punknown -> "cat")
548 ; colorspace = Rgb
549 ; invert = false
550 ; colorscale = 1.0
551 ; redirectstderr = false
552 ; ghyllscroll = None
553 ; columns = Csingle [||]
554 ; beyecolumns = None
555 ; updatecurs = false
556 ; hfsize = 12
557 ; pgscale = 1.0
558 ; usepbo = false
559 ; wheelbypage = false
560 ; stcmd = "echo SyncTex"
561 ; keyhashes =
562 let mk n = (n, Hashtbl.create 1) in
563 [ mk "global"
564 ; mk "info"
565 ; mk "help"
566 ; mk "outline"
567 ; mk "listview"
568 ; mk "birdseye"
569 ; mk "textentry"
570 ; mk "links"
571 ; mk "view"
576 let wtmode = ref false;;
578 let findkeyhash c name =
579 try List.assoc name c.keyhashes
580 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
583 let conf = { defconf with angle = defconf.angle };;
585 let pgscale h = truncate (float h *. conf.pgscale);;
587 type fontstate =
588 { mutable fontsize : int
589 ; mutable wwidth : float
590 ; mutable maxrows : int
594 let fstate =
595 { fontsize = 14
596 ; wwidth = nan
597 ; maxrows = -1
601 let geturl s =
602 let colonpos = try String.index s ':' with Not_found -> -1 in
603 let len = String.length s in
604 if colonpos >= 0 && colonpos + 3 < len
605 then (
606 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
607 then
608 let schemestartpos =
609 try String.rindex_from s colonpos ' '
610 with Not_found -> -1
612 let scheme =
613 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
615 match scheme with
616 | "http" | "ftp" | "mailto" ->
617 let epos =
618 try String.index_from s colonpos ' '
619 with Not_found -> len
621 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
622 | _ -> ""
623 else ""
625 else ""
628 let gotouri uri =
629 if String.length conf.urilauncher = 0
630 then print_endline uri
631 else (
632 let url = geturl uri in
633 if String.length url = 0
634 then print_endline uri
635 else
636 let re = Str.regexp "%s" in
637 let command = Str.global_replace re url conf.urilauncher in
638 try popen command []
639 with exn ->
640 Printf.eprintf
641 "failed to execute `%s': %s\n" command (exntos exn);
642 flush stderr;
646 let version () =
647 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
648 (platform_to_string platform) Sys.word_size Sys.ocaml_version
651 let makehelp () =
652 let strings = version () :: "" :: Help.keys in
653 Array.of_list (
654 List.map (fun s ->
655 let url = geturl s in
656 if String.length url > 0
657 then (s, 0, Action (fun u -> gotouri url; u))
658 else (s, 0, Noaction)
659 ) strings);
662 let noghyll _ = ();;
663 let firstgeomcmds = "", [];;
664 let noreprf () = ();;
666 let state =
667 { sr = Unix.stdin
668 ; sw = Unix.stdin
669 ; wsfd = Unix.stdin
670 ; errfd = None
671 ; stderr = Unix.stderr
672 ; errmsgs = Buffer.create 0
673 ; newerrmsgs = false
674 ; x = 0
675 ; y = 0
676 ; w = 0
677 ; scrollw = 0
678 ; hscrollh = 0
679 ; anchor = emptyanchor
680 ; ranchors = []
681 ; layout = []
682 ; maxy = max_int
683 ; tilelru = Queue.create ()
684 ; pagemap = Hashtbl.create 10
685 ; tilemap = Hashtbl.create 10
686 ; pdims = []
687 ; pagecount = 0
688 ; currently = Idle
689 ; mstate = Mnone
690 ; rects = []
691 ; rects1 = []
692 ; text = ""
693 ; mode = View
694 ; winstate = []
695 ; searchpattern = ""
696 ; outlines = [||]
697 ; bookmarks = []
698 ; path = ""
699 ; password = ""
700 ; nameddest = ""
701 ; geomcmds = firstgeomcmds
702 ; hists =
703 { nav = cbnew 10 emptyanchor
704 ; pat = cbnew 10 ""
705 ; pag = cbnew 10 ""
706 ; sel = cbnew 10 ""
708 ; memused = 0
709 ; gen = 0
710 ; throttle = None
711 ; autoscroll = None
712 ; ghyll = noghyll
713 ; help = makehelp ()
714 ; docinfo = []
715 ; texid = None
716 ; prevzoom = 1.0
717 ; progress = -1.0
718 ; uioh = nouioh
719 ; redisplay = true
720 ; mpos = (-1, -1)
721 ; keystate = KSnone
722 ; glinks = false
723 ; prevcolumns = None
724 ; winw = -1
725 ; winh = -1
726 ; reprf = noreprf
727 ; origin = ""
731 let setfontsize n =
732 fstate.fontsize <- n;
733 fstate.wwidth <- measurestr fstate.fontsize "w";
734 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
737 let vlog fmt =
738 if conf.verbose
739 then
740 Printf.kprintf prerr_endline fmt
741 else
742 Printf.kprintf ignore fmt
745 let launchpath () =
746 if String.length conf.pathlauncher = 0
747 then print_endline state.path
748 else (
749 let re = Str.regexp "%s" in
750 let command = Str.global_replace re state.path conf.pathlauncher in
751 try popen command []
752 with exn ->
753 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
754 flush stderr;
758 module Ne = struct
759 type 'a t = | Res of 'a | Exn of exn;;
761 let pipe () =
762 try Res (Unix.pipe ())
763 with exn -> Exn exn
766 let clo fd f =
767 try tempfailureretry Unix.close fd
768 with exn -> f (exntos exn)
771 let dup fd =
772 try Res (tempfailureretry Unix.dup fd)
773 with exn -> Exn exn
776 let dup2 fd1 fd2 =
777 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
778 with exn -> Exn exn
780 end;;
782 let redirectstderr () =
783 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
784 if conf.redirectstderr
785 then
786 match Ne.pipe () with
787 | Ne.Exn exn ->
788 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
790 | Ne.Res (r, w) ->
791 begin match Ne.dup Unix.stderr with
792 | Ne.Exn exn ->
793 dolog "failed to dup stderr: %s" (exntos exn);
794 Ne.clo r (clofail "pipe/r");
795 Ne.clo w (clofail "pipe/w");
797 | Ne.Res dupstderr ->
798 begin match Ne.dup2 w Unix.stderr with
799 | Ne.Exn exn ->
800 dolog "failed to dup2 to stderr: %s" (exntos exn);
801 Ne.clo dupstderr (clofail "stderr duplicate");
802 Ne.clo r (clofail "redir pipe/r");
803 Ne.clo w (clofail "redir pipe/w");
805 | Ne.Res () ->
806 state.stderr <- dupstderr;
807 state.errfd <- Some r;
808 end;
810 else (
811 state.newerrmsgs <- false;
812 begin match state.errfd with
813 | Some fd ->
814 begin match Ne.dup2 state.stderr Unix.stderr with
815 | Ne.Exn exn ->
816 dolog "failed to dup2 original stderr: %s" (exntos exn)
817 | Ne.Res () ->
818 Ne.clo fd (clofail "dup of stderr");
819 state.errfd <- None;
820 end;
821 | None -> ()
822 end;
823 prerr_string (Buffer.contents state.errmsgs);
824 flush stderr;
825 Buffer.clear state.errmsgs;
829 module G =
830 struct
831 let postRedisplay who =
832 if conf.verbose
833 then prerr_endline ("redisplay for " ^ who);
834 state.redisplay <- true;
836 end;;
838 let getopaque pageno =
839 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
840 with Not_found -> None
843 let putopaque pageno opaque =
844 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
847 let pagetranslatepoint l x y =
848 let dy = y - l.pagedispy in
849 let y = dy + l.pagey in
850 let dx = x - l.pagedispx in
851 let x = dx + l.pagex in
852 (x, y);
855 let onppundermouse g x y d =
856 let rec f = function
857 | l :: rest ->
858 begin match getopaque l.pageno with
859 | Some opaque ->
860 let x0 = l.pagedispx in
861 let x1 = x0 + l.pagevw in
862 let y0 = l.pagedispy in
863 let y1 = y0 + l.pagevh in
864 if y >= y0 && y <= y1 && x >= x0 && x <= x1
865 then
866 let px, py = pagetranslatepoint l x y in
867 match g opaque l px py with
868 | Some res -> res
869 | None -> f rest
870 else f rest
871 | _ ->
872 f rest
874 | [] -> d
876 f state.layout
879 let getunder x y =
880 let g opaque _ px py =
881 match whatsunder opaque px py with
882 | Unone -> None
883 | under -> Some under
885 onppundermouse g x y Unone
888 let unproject x y =
889 let g opaque l x y =
890 match unproject opaque x y with
891 | Some (x, y) -> Some (Some (l.pageno, x, y))
892 | None -> None
894 onppundermouse g x y None;
897 let showtext c s =
898 state.text <- Printf.sprintf "%c%s" c s;
899 G.postRedisplay "showtext";
902 let undertext = function
903 | Unone -> "none"
904 | Ulinkuri s -> s
905 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
906 | Utext s -> "font: " ^ s
907 | Uunexpected s -> "unexpected: " ^ s
908 | Ulaunch s -> "launch: " ^ s
909 | Unamed s -> "named: " ^ s
910 | Uremote (filename, pageno) ->
911 Printf.sprintf "%s: page %d" filename (pageno+1)
914 let updateunder x y =
915 match getunder x y with
916 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
917 | Ulinkuri uri ->
918 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
919 Wsi.setcursor Wsi.CURSOR_INFO
920 | Ulinkgoto (pageno, _) ->
921 if conf.underinfo
922 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
923 Wsi.setcursor Wsi.CURSOR_INFO
924 | Utext s ->
925 if conf.underinfo then showtext 'f' ("ont: " ^ s);
926 Wsi.setcursor Wsi.CURSOR_TEXT
927 | Uunexpected s ->
928 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
929 Wsi.setcursor Wsi.CURSOR_INHERIT
930 | Ulaunch s ->
931 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
932 Wsi.setcursor Wsi.CURSOR_INHERIT
933 | Unamed s ->
934 if conf.underinfo then showtext 'n' ("amed: " ^ s);
935 Wsi.setcursor Wsi.CURSOR_INHERIT
936 | Uremote (filename, pageno) ->
937 if conf.underinfo then showtext 'r'
938 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
939 Wsi.setcursor Wsi.CURSOR_INFO
942 let showlinktype under =
943 if conf.underinfo
944 then
945 match under with
946 | Unone -> ()
947 | under ->
948 let s = undertext under in
949 showtext ' ' s
952 let addchar s c =
953 let b = Buffer.create (String.length s + 1) in
954 Buffer.add_string b s;
955 Buffer.add_char b c;
956 Buffer.contents b;
959 let colorspace_of_string s =
960 match String.lowercase s with
961 | "rgb" -> Rgb
962 | "bgr" -> Bgr
963 | "gray" -> Gray
964 | _ -> failwith "invalid colorspace"
967 let int_of_colorspace = function
968 | Rgb -> 0
969 | Bgr -> 1
970 | Gray -> 2
973 let colorspace_of_int = function
974 | 0 -> Rgb
975 | 1 -> Bgr
976 | 2 -> Gray
977 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
980 let colorspace_to_string = function
981 | Rgb -> "rgb"
982 | Bgr -> "bgr"
983 | Gray -> "gray"
986 let fitmodel_of_string s =
987 match String.lowercase s with
988 | "width" -> FitWidth
989 | "proportional" -> FitProportional
990 | "page" -> FitPage
991 | _ -> failwith "invalid fit model"
994 let int_of_fitmodel = function
995 | FitWidth -> 0
996 | FitProportional -> 1
997 | FitPage -> 2
1000 let fitmodel_of_int = function
1001 | 0 -> FitWidth
1002 | 1 -> FitProportional
1003 | 2 -> FitPage
1004 | n -> failwith ("invalid fit model index " ^ string_of_int n)
1007 let fitmodel_to_string = function
1008 | FitWidth -> "width"
1009 | FitProportional -> "proportional"
1010 | FitPage -> "page"
1013 let intentry_with_suffix text key =
1014 let c =
1015 if key >= 32 && key < 127
1016 then Char.chr key
1017 else '\000'
1019 match Char.lowercase c with
1020 | '0' .. '9' ->
1021 let text = addchar text c in
1022 TEcont text
1024 | 'k' | 'm' | 'g' ->
1025 let text = addchar text c in
1026 TEcont text
1028 | _ ->
1029 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1030 TEcont text
1033 let multicolumns_to_string (n, a, b) =
1034 if a = 0 && b = 0
1035 then Printf.sprintf "%d" n
1036 else Printf.sprintf "%d,%d,%d" n a b;
1039 let multicolumns_of_string s =
1041 (int_of_string s, 0, 0)
1042 with _ ->
1043 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1044 if a > 1 || b > 1
1045 then failwith "subtly broken"; (n, a, b)
1049 let readcmd fd =
1050 let s = "xxxx" in
1051 let n = tempfailureretry (Unix.read fd s 0) 4 in
1052 if n != 4 then failwith "incomplete read(len)";
1053 let len = 0
1054 lor (Char.code s.[0] lsl 24)
1055 lor (Char.code s.[1] lsl 16)
1056 lor (Char.code s.[2] lsl 8)
1057 lor (Char.code s.[3] lsl 0)
1059 let s = String.create len in
1060 let n = tempfailureretry (Unix.read fd s 0) len in
1061 if n != len then failwith "incomplete read(data)";
1065 let btod b = if b then 1 else 0;;
1067 let wcmd fmt =
1068 let b = Buffer.create 16 in
1069 Buffer.add_string b "llll";
1070 Printf.kbprintf
1071 (fun b ->
1072 let s = Buffer.contents b in
1073 let n = String.length s in
1074 let len = n - 4 in
1075 (* dolog "wcmd %S" (String.sub s 4 len); *)
1076 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1077 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1078 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1079 s.[3] <- Char.chr (len land 0xff);
1080 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1081 if n' != n then failwith "write failed";
1082 ) b fmt;
1085 let calcips h =
1086 let d = state.winh - h in
1087 max conf.interpagespace ((d + 1) / 2)
1090 let rowyh (c, coverA, coverB) b n =
1091 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1092 then
1093 let _, _, vy, (_, _, h, _) = b.(n) in
1094 (vy, h)
1095 else
1096 let n' = n - coverA in
1097 let d = n' mod c in
1098 let s = n - d in
1099 let e = min state.pagecount (s + c) in
1100 let rec find m miny maxh = if m = e then miny, maxh else
1101 let _, _, y, (_, _, h, _) = b.(m) in
1102 let miny = min miny y in
1103 let maxh = max maxh h in
1104 find (m+1) miny maxh
1105 in find s max_int 0
1108 let calcheight () =
1109 match conf.columns with
1110 | Cmulti ((_, _, _) as cl, b) ->
1111 if Array.length b > 0
1112 then
1113 let y, h = rowyh cl b (Array.length b - 1) in
1114 y + h + (if conf.presentation then calcips h else 0)
1115 else 0
1116 | Csingle b ->
1117 if Array.length b > 0
1118 then
1119 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1120 y + h + (if conf.presentation then calcips h else 0)
1121 else 0
1122 | Csplit (_, b) ->
1123 if Array.length b > 0
1124 then
1125 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1126 y + h
1127 else 0
1130 let getpageyh pageno =
1131 let pageno = bound pageno 0 (state.pagecount-1) in
1132 match conf.columns with
1133 | Csingle b ->
1134 if Array.length b = 0
1135 then 0, 0
1136 else
1137 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1138 let y =
1139 if conf.presentation
1140 then y - calcips h
1141 else y
1143 y, h
1144 | Cmulti (cl, b) ->
1145 if Array.length b = 0
1146 then 0, 0
1147 else
1148 let y, h = rowyh cl b pageno in
1149 let y =
1150 if conf.presentation
1151 then y - calcips h
1152 else y
1154 y, h
1155 | Csplit (c, b) ->
1156 if Array.length b = 0
1157 then 0, 0
1158 else
1159 let n = pageno*c in
1160 let (_, _, y, (_, _, h, _)) = b.(n) in
1161 y, h
1164 let getpagedim pageno =
1165 let rec f ppdim l =
1166 match l with
1167 | (n, _, _, _) as pdim :: rest ->
1168 if n >= pageno
1169 then (if n = pageno then pdim else ppdim)
1170 else f pdim rest
1172 | [] -> ppdim
1174 f (-1, -1, -1, -1) state.pdims
1177 let getpagey pageno = fst (getpageyh pageno);;
1179 let nogeomcmds cmds =
1180 match cmds with
1181 | s, [] -> String.length s = 0
1182 | _ -> false
1185 let page_of_y y =
1186 let ((c, coverA, coverB) as cl), b =
1187 match conf.columns with
1188 | Csingle b -> (1, 0, 0), b
1189 | Cmulti (c, b) -> c, b
1190 | Csplit (_, b) -> (1, 0, 0), b
1192 if Array.length b = 0
1193 then -1
1194 else
1195 let rec bsearch nmin nmax =
1196 if nmin > nmax
1197 then bound nmin 0 (state.pagecount-1)
1198 else
1199 let n = (nmax + nmin) / 2 in
1200 let vy, h = rowyh cl b n in
1201 let y0, y1 =
1202 if conf.presentation
1203 then
1204 let ips = calcips h in
1205 let y0 = vy - ips in
1206 let y1 = vy + h + ips in
1207 y0, y1
1208 else (
1209 if n = 0
1210 then 0, vy + h + conf.interpagespace
1211 else
1212 let y0 = vy - conf.interpagespace in
1213 y0, y0 + h + conf.interpagespace
1216 if y >= y0 && y < y1
1217 then (
1218 if c = 1
1219 then n
1220 else (
1221 if n > coverA
1222 then
1223 if n < state.pagecount - coverB
1224 then ((n-coverA)/c)*c + coverA
1225 else n
1226 else n
1229 else (
1230 if y > y0
1231 then bsearch (n+1) nmax
1232 else bsearch nmin (n-1)
1235 let r = bsearch 0 (state.pagecount-1) in
1239 let layoutN ((columns, coverA, coverB), b) y sh =
1240 let sh = sh - state.hscrollh in
1241 let rec fold accu n =
1242 if n = Array.length b
1243 then accu
1244 else
1245 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1246 if (vy - y) > sh &&
1247 (n = coverA - 1
1248 || n = state.pagecount - coverB
1249 || (n - coverA) mod columns = columns - 1)
1250 then accu
1251 else
1252 let accu =
1253 if vy + h > y
1254 then
1255 let pagey = max 0 (y - vy) in
1256 let pagedispy = if pagey > 0 then 0 else vy - y in
1257 let pagedispx, pagex =
1258 let pdx =
1259 if n = coverA - 1 || n = state.pagecount - coverB
1260 then state.x + (state.winw - state.scrollw - w) / 2
1261 else dx + xoff + state.x
1263 if pdx < 0
1264 then 0, -pdx
1265 else pdx, 0
1267 let pagevw =
1268 let vw = state.winw - state.scrollw - pagedispx in
1269 let pw = w - pagex in
1270 min vw pw
1272 let pagevh = min (h - pagey) (sh - pagedispy) in
1273 if pagevw > 0 && pagevh > 0
1274 then
1275 let e =
1276 { pageno = n
1277 ; pagedimno = pdimno
1278 ; pagew = w
1279 ; pageh = h
1280 ; pagex = pagex
1281 ; pagey = pagey
1282 ; pagevw = pagevw
1283 ; pagevh = pagevh
1284 ; pagedispx = pagedispx
1285 ; pagedispy = pagedispy
1286 ; pagecol = 0
1289 e :: accu
1290 else
1291 accu
1292 else
1293 accu
1295 fold accu (n+1)
1297 List.rev (fold [] (page_of_y y));
1300 let layoutS (columns, b) y sh =
1301 let sh = sh - state.hscrollh in
1302 let rec fold accu n =
1303 if n = Array.length b
1304 then accu
1305 else
1306 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1307 if (vy - y) > sh
1308 then accu
1309 else
1310 let accu =
1311 if vy + pageh > y
1312 then
1313 let x = xoff + state.x in
1314 let pagey = max 0 (y - vy) in
1315 let pagedispy = if pagey > 0 then 0 else vy - y in
1316 let pagedispx, pagex =
1317 if px = 0
1318 then (
1319 if x < 0
1320 then 0, -x
1321 else x, 0
1323 else (
1324 let px = px - x in
1325 if px < 0
1326 then -px, 0
1327 else 0, px
1330 let pagecolw = pagew/columns in
1331 let pagedispx =
1332 if pagecolw < state.winw
1333 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1334 else pagedispx
1336 let pagevw =
1337 let vw = state.winw - pagedispx - state.scrollw in
1338 let pw = pagew - pagex in
1339 min vw pw
1341 let pagevw = min pagevw pagecolw in
1342 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1343 if pagevw > 0 && pagevh > 0
1344 then
1345 let e =
1346 { pageno = n/columns
1347 ; pagedimno = pdimno
1348 ; pagew = pagew
1349 ; pageh = pageh
1350 ; pagex = pagex
1351 ; pagey = pagey
1352 ; pagevw = pagevw
1353 ; pagevh = pagevh
1354 ; pagedispx = pagedispx
1355 ; pagedispy = pagedispy
1356 ; pagecol = n mod columns
1359 e :: accu
1360 else
1361 accu
1362 else
1363 accu
1365 fold accu (n+1)
1367 List.rev (fold [] 0)
1370 let layout y sh =
1371 if nogeomcmds state.geomcmds
1372 then
1373 match conf.columns with
1374 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1375 | Cmulti c -> layoutN c y sh
1376 | Csplit s -> layoutS s y sh
1377 else []
1380 let clamp incr =
1381 let y = state.y + incr in
1382 let y = max 0 y in
1383 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1387 let itertiles l f =
1388 let tilex = l.pagex mod conf.tilew in
1389 let tiley = l.pagey mod conf.tileh in
1391 let col = l.pagex / conf.tilew in
1392 let row = l.pagey / conf.tileh in
1394 let rec rowloop row y0 dispy h =
1395 if h = 0
1396 then ()
1397 else (
1398 let dh = conf.tileh - y0 in
1399 let dh = min h dh in
1400 let rec colloop col x0 dispx w =
1401 if w = 0
1402 then ()
1403 else (
1404 let dw = conf.tilew - x0 in
1405 let dw = min w dw in
1407 f col row dispx dispy x0 y0 dw dh;
1408 colloop (col+1) 0 (dispx+dw) (w-dw)
1411 colloop col tilex l.pagedispx l.pagevw;
1412 rowloop (row+1) 0 (dispy+dh) (h-dh)
1415 if l.pagevw > 0 && l.pagevh > 0
1416 then rowloop row tiley l.pagedispy l.pagevh;
1419 let gettileopaque l col row =
1420 let key =
1421 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1423 try Some (Hashtbl.find state.tilemap key)
1424 with Not_found -> None
1427 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1428 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1429 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1432 let drawtiles l color =
1433 GlDraw.color color;
1434 let f col row x y tilex tiley w h =
1435 match gettileopaque l col row with
1436 | Some (opaque, _, t) ->
1437 let params = x, y, w, h, tilex, tiley in
1438 if conf.invert
1439 then (
1440 Gl.enable `blend;
1441 GlFunc.blend_func `zero `one_minus_src_color;
1443 drawtile params opaque;
1444 if conf.invert
1445 then Gl.disable `blend;
1446 if conf.debug
1447 then (
1448 let s = Printf.sprintf
1449 "%d[%d,%d] %f sec"
1450 l.pageno col row t
1452 let w = measurestr fstate.fontsize s in
1453 GlMisc.push_attrib [`current];
1454 GlDraw.color (0.0, 0.0, 0.0);
1455 GlDraw.rect
1456 (float (x-2), float (y-2))
1457 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1458 GlDraw.color (1.0, 1.0, 1.0);
1459 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1460 GlMisc.pop_attrib ();
1463 | _ ->
1464 let w =
1465 let lw = state.winw - state.scrollw - x in
1466 min lw w
1467 and h =
1468 let lh = state.winh - y in
1469 min lh h
1471 begin match state.texid with
1472 | Some id ->
1473 Gl.enable `texture_2d;
1474 GlTex.bind_texture `texture_2d id;
1475 let x0 = float x
1476 and y0 = float y
1477 and x1 = float (x+w)
1478 and y1 = float (y+h) in
1480 let tw = float w /. 16.0
1481 and th = float h /. 16.0 in
1482 let tx0 = float tilex /. 16.0
1483 and ty0 = float tiley /. 16.0 in
1484 let tx1 = tx0 +. tw
1485 and ty1 = ty0 +. th in
1486 GlDraw.begins `quads;
1487 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1488 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1489 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1490 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1491 GlDraw.ends ();
1493 Gl.disable `texture_2d;
1494 | None ->
1495 GlDraw.color (1.0, 1.0, 1.0);
1496 GlDraw.rect
1497 (float x, float y)
1498 (float (x+w), float (y+h));
1499 end;
1500 if w > 128 && h > fstate.fontsize + 10
1501 then (
1502 GlDraw.color (0.0, 0.0, 0.0);
1503 let c, r =
1504 if conf.verbose
1505 then (col*conf.tilew, row*conf.tileh)
1506 else col, row
1508 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1510 GlDraw.color color;
1512 itertiles l f
1515 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1517 let tilevisible1 l x y =
1518 let ax0 = l.pagex
1519 and ax1 = l.pagex + l.pagevw
1520 and ay0 = l.pagey
1521 and ay1 = l.pagey + l.pagevh in
1523 let bx0 = x
1524 and by0 = y in
1525 let bx1 = min (bx0 + conf.tilew) l.pagew
1526 and by1 = min (by0 + conf.tileh) l.pageh in
1528 let rx0 = max ax0 bx0
1529 and ry0 = max ay0 by0
1530 and rx1 = min ax1 bx1
1531 and ry1 = min ay1 by1 in
1533 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1534 nonemptyintersection
1537 let tilevisible layout n x y =
1538 let rec findpageinlayout m = function
1539 | l :: rest when l.pageno = n ->
1540 tilevisible1 l x y || (
1541 match conf.columns with
1542 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1543 | _ -> false
1545 | _ :: rest -> findpageinlayout 0 rest
1546 | [] -> false
1548 findpageinlayout 0 layout;
1551 let tileready l x y =
1552 tilevisible1 l x y &&
1553 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1556 let tilepage n p layout =
1557 let rec loop = function
1558 | l :: rest ->
1559 if l.pageno = n
1560 then
1561 let f col row _ _ _ _ _ _ =
1562 if state.currently = Idle
1563 then
1564 match gettileopaque l col row with
1565 | Some _ -> ()
1566 | None ->
1567 let x = col*conf.tilew
1568 and y = row*conf.tileh in
1569 let w =
1570 let w = l.pagew - x in
1571 min w conf.tilew
1573 let h =
1574 let h = l.pageh - y in
1575 min h conf.tileh
1577 let pbo =
1578 if conf.usepbo
1579 then getpbo w h conf.colorspace
1580 else "0"
1582 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1583 state.currently <-
1584 Tiling (
1585 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1586 conf.tilew, conf.tileh
1589 itertiles l f;
1590 else
1591 loop rest
1593 | [] -> ()
1595 if nogeomcmds state.geomcmds
1596 then loop layout;
1599 let preloadlayout y =
1600 let y = if y < state.winh then 0 else y - state.winh in
1601 let h = state.winh*3 in
1602 layout y h;
1605 let load pages =
1606 let rec loop pages =
1607 if state.currently != Idle
1608 then ()
1609 else
1610 match pages with
1611 | l :: rest ->
1612 begin match getopaque l.pageno with
1613 | None ->
1614 wcmd "page %d %d" l.pageno l.pagedimno;
1615 state.currently <- Loading (l, state.gen);
1616 | Some opaque ->
1617 tilepage l.pageno opaque pages;
1618 loop rest
1619 end;
1620 | _ -> ()
1622 if nogeomcmds state.geomcmds
1623 then loop pages
1626 let preload pages =
1627 load pages;
1628 if conf.preload && state.currently = Idle
1629 then load (preloadlayout state.y);
1632 let layoutready layout =
1633 let rec fold all ls =
1634 all && match ls with
1635 | l :: rest ->
1636 let seen = ref false in
1637 let allvisible = ref true in
1638 let foo col row _ _ _ _ _ _ =
1639 seen := true;
1640 allvisible := !allvisible &&
1641 begin match gettileopaque l col row with
1642 | Some _ -> true
1643 | None -> false
1646 itertiles l foo;
1647 fold (!seen && !allvisible) rest
1648 | [] -> true
1650 let alltilesvisible = fold true layout in
1651 alltilesvisible;
1654 let gotoy y =
1655 let y = bound y 0 state.maxy in
1656 let y, layout, proceed =
1657 match conf.maxwait with
1658 | Some time when state.ghyll == noghyll ->
1659 begin match state.throttle with
1660 | None ->
1661 let layout = layout y state.winh in
1662 let ready = layoutready layout in
1663 if not ready
1664 then (
1665 load layout;
1666 state.throttle <- Some (layout, y, now ());
1668 else G.postRedisplay "gotoy showall (None)";
1669 y, layout, ready
1670 | Some (_, _, started) ->
1671 let dt = now () -. started in
1672 if dt > time
1673 then (
1674 state.throttle <- None;
1675 let layout = layout y state.winh in
1676 load layout;
1677 G.postRedisplay "maxwait";
1678 y, layout, true
1680 else -1, [], false
1683 | _ ->
1684 let layout = layout y state.winh in
1685 if not !wtmode || layoutready layout
1686 then G.postRedisplay "gotoy ready";
1687 y, layout, true
1689 if proceed
1690 then (
1691 state.y <- y;
1692 state.layout <- layout;
1693 begin match state.mode with
1694 | LinkNav (Ltexact (pageno, linkno)) ->
1695 let rec loop = function
1696 | [] ->
1697 state.mode <- LinkNav (Ltgendir 0)
1698 | l :: _ when l.pageno = pageno ->
1699 begin match getopaque pageno with
1700 | None ->
1701 state.mode <- LinkNav (Ltgendir 0)
1702 | Some opaque ->
1703 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1704 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1705 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1706 then state.mode <- LinkNav (Ltgendir 0)
1708 | _ :: rest -> loop rest
1710 loop layout
1711 | _ -> ()
1712 end;
1713 begin match state.mode with
1714 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1715 if not (pagevisible layout pageno)
1716 then (
1717 match state.layout with
1718 | [] -> ()
1719 | l :: _ ->
1720 state.mode <- Birdseye (
1721 conf, leftx, l.pageno, hooverpageno, anchor
1724 | LinkNav (Ltgendir dir as lt) ->
1725 let linknav =
1726 let rec loop = function
1727 | [] -> lt
1728 | l :: rest ->
1729 match getopaque l.pageno with
1730 | None -> loop rest
1731 | Some opaque ->
1732 let link =
1733 let ld =
1734 if dir = 0
1735 then LDfirstvisible (l.pagex, l.pagey, dir)
1736 else (
1737 if dir > 0 then LDfirst else LDlast
1740 findlink opaque ld
1742 match link with
1743 | Lnotfound -> loop rest
1744 | Lfound n ->
1745 showlinktype (getlink opaque n);
1746 Ltexact (l.pageno, n)
1748 loop state.layout
1750 state.mode <- LinkNav linknav
1751 | _ -> ()
1752 end;
1753 preload layout;
1755 state.ghyll <- noghyll;
1756 if conf.updatecurs
1757 then (
1758 let mx, my = state.mpos in
1759 updateunder mx my;
1763 let conttiling pageno opaque =
1764 tilepage pageno opaque
1765 (if conf.preload then preloadlayout state.y else state.layout)
1768 let gotoy_and_clear_text y =
1769 if not conf.verbose then state.text <- "";
1770 gotoy y;
1773 let getanchor1 l =
1774 let top =
1775 let coloff = l.pagecol * l.pageh in
1776 float (l.pagey + coloff) /. float l.pageh
1778 let dtop =
1779 if l.pagedispy = 0
1780 then
1782 else
1783 if conf.presentation
1784 then float l.pagedispy /. float (calcips l.pageh)
1785 else float l.pagedispy /. float conf.interpagespace
1787 (l.pageno, top, dtop)
1790 let getanchor () =
1791 match state.layout with
1792 | l :: _ -> getanchor1 l
1793 | [] ->
1794 let n = page_of_y state.y in
1795 if n = -1
1796 then state.anchor
1797 else
1798 let y, h = getpageyh n in
1799 let dy = y - state.y in
1800 let dtop =
1801 if conf.presentation
1802 then
1803 let ips = calcips h in
1804 float (dy + ips) /. float ips
1805 else
1806 float dy /. float conf.interpagespace
1808 (n, 0.0, dtop)
1811 let getanchory (n, top, dtop) =
1812 let y, h = getpageyh n in
1813 if conf.presentation
1814 then
1815 let ips = calcips h in
1816 y + truncate (top*.float h -. dtop*.float ips) + ips;
1817 else
1818 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1821 let gotoanchor anchor =
1822 gotoy (getanchory anchor);
1825 let addnav () =
1826 cbput state.hists.nav (getanchor ());
1829 let getnav dir =
1830 let anchor = cbgetc state.hists.nav dir in
1831 getanchory anchor;
1834 let gotoghyll y =
1835 let scroll f n a b =
1836 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1837 let snake f a b =
1838 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1839 if f < a
1840 then s (float f /. float a)
1841 else (
1842 if f > b
1843 then 1.0 -. s ((float (f-b) /. float (n-b)))
1844 else 1.0
1847 snake f a b
1848 and summa f n a b =
1849 (* courtesy:
1850 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1851 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1852 let iv1 = iv f in
1853 let ins = float a *. iv1
1854 and outs = float (n-b) *. iv1 in
1855 let ones = b - a in
1856 ins +. outs +. float ones
1858 let rec set (_N, _A, _B) y sy =
1859 let sum = summa 1.0 _N _A _B in
1860 let dy = float (y - sy) in
1861 state.ghyll <- (
1862 let rec gf n y1 o =
1863 if n >= _N
1864 then state.ghyll <- noghyll
1865 else
1866 let go n =
1867 let s = scroll n _N _A _B in
1868 let y1 = y1 +. ((s *. dy) /. sum) in
1869 gotoy_and_clear_text (truncate y1);
1870 state.ghyll <- gf (n+1) y1;
1872 match o with
1873 | None -> go n
1874 | Some y' -> set (_N/2, 1, 1) y' state.y
1876 gf 0 (float state.y)
1879 match conf.ghyllscroll with
1880 | None ->
1881 gotoy_and_clear_text y
1882 | Some nab ->
1883 if state.ghyll == noghyll
1884 then set nab y state.y
1885 else state.ghyll (Some y)
1888 let gotopage n top =
1889 let y, h = getpageyh n in
1890 let y = y + (truncate (top *. float h)) in
1891 gotoghyll y
1894 let gotopage1 n top =
1895 let y = getpagey n in
1896 let y = y + top in
1897 gotoghyll y
1900 let invalidate s f =
1901 state.layout <- [];
1902 state.pdims <- [];
1903 state.rects <- [];
1904 state.rects1 <- [];
1905 match state.geomcmds with
1906 | ps, [] when String.length ps = 0 ->
1907 f ();
1908 state.geomcmds <- s, [];
1910 | ps, [] ->
1911 state.geomcmds <- ps, [s, f];
1913 | ps, (s', _) :: rest when s' = s ->
1914 state.geomcmds <- ps, ((s, f) :: rest);
1916 | ps, cmds ->
1917 state.geomcmds <- ps, ((s, f) :: cmds);
1920 let flushpages () =
1921 Hashtbl.iter (fun _ opaque ->
1922 wcmd "freepage %s" opaque;
1923 ) state.pagemap;
1924 Hashtbl.clear state.pagemap;
1927 let flushtiles () =
1928 if not (Queue.is_empty state.tilelru)
1929 then (
1930 Queue.iter (fun (k, p, s) ->
1931 wcmd "freetile %s" p;
1932 state.memused <- state.memused - s;
1933 Hashtbl.remove state.tilemap k;
1934 ) state.tilelru;
1935 state.uioh#infochanged Memused;
1936 Queue.clear state.tilelru;
1938 load state.layout;
1941 let opendoc path password =
1942 state.path <- path;
1943 state.password <- password;
1944 state.gen <- state.gen + 1;
1945 state.docinfo <- [];
1947 flushpages ();
1948 setaalevel conf.aalevel;
1949 let titlepath =
1950 if String.length state.origin = 0
1951 then path
1952 else state.origin
1954 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1955 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
1956 invalidate "reqlayout"
1957 (fun () ->
1958 wcmd "reqlayout %d %d %f %s\000"
1959 conf.angle (int_of_fitmodel conf.fitmodel) conf.zoom state.nameddest;
1963 let reload () =
1964 state.anchor <- getanchor ();
1965 opendoc state.path state.password;
1968 let scalecolor c =
1969 let c = c *. conf.colorscale in
1970 (c, c, c);
1973 let scalecolor2 (r, g, b) =
1974 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1977 let docolumns = function
1978 | Csingle _ ->
1979 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1980 let rec loop pageno pdimno pdim y ph pdims =
1981 if pageno = state.pagecount
1982 then ()
1983 else
1984 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1985 match pdims with
1986 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1987 pdimno+1, pdim, rest
1988 | _ ->
1989 pdimno, pdim, pdims
1991 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
1992 let y = y +
1993 (if conf.presentation
1994 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1995 else (if pageno = 0 then 0 else conf.interpagespace)
1998 a.(pageno) <- (pdimno, x, y, pdim);
1999 loop (pageno+1) pdimno pdim (y + h) h pdims
2001 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2002 conf.columns <- Csingle a;
2004 | Cmulti ((columns, coverA, coverB), _) ->
2005 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2006 let rec loop pageno pdimno pdim x y rowh pdims =
2007 let rec fixrow m = if m = pageno then () else
2008 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2009 if h < rowh
2010 then (
2011 let y = y + (rowh - h) / 2 in
2012 a.(m) <- (pdimno, x, y, pdim);
2014 fixrow (m+1)
2016 if pageno = state.pagecount
2017 then fixrow (((pageno - 1) / columns) * columns)
2018 else
2019 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2020 match pdims with
2021 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2022 pdimno+1, pdim, rest
2023 | _ ->
2024 pdimno, pdim, pdims
2026 let x, y, rowh' =
2027 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2028 then (
2029 let x = (state.winw - state.scrollw - w) / 2 in
2030 let ips =
2031 if conf.presentation then calcips h else conf.interpagespace in
2032 x, y + ips + rowh, h
2034 else (
2035 if (pageno - coverA) mod columns = 0
2036 then (
2037 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2038 let y =
2039 if conf.presentation
2040 then
2041 let ips = calcips h in
2042 y + (if pageno = 0 then 0 else calcips rowh + ips)
2043 else
2044 y + (if pageno = 0 then 0 else conf.interpagespace)
2046 x, y + rowh, h
2048 else x, y, max rowh h
2051 let y =
2052 if pageno > 1 && (pageno - coverA) mod columns = 0
2053 then (
2054 let y =
2055 if pageno = columns && conf.presentation
2056 then (
2057 let ips = calcips rowh in
2058 for i = 0 to pred columns
2060 let (pdimno, x, y, pdim) = a.(i) in
2061 a.(i) <- (pdimno, x, y+ips, pdim)
2062 done;
2063 y+ips;
2065 else y
2067 fixrow (pageno - columns);
2070 else y
2072 a.(pageno) <- (pdimno, x, y, pdim);
2073 let x = x + w + xoff*2 + conf.interpagespace in
2074 loop (pageno+1) pdimno pdim x y rowh' pdims
2076 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2077 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2079 | Csplit (c, _) ->
2080 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2081 let rec loop pageno pdimno pdim y pdims =
2082 if pageno = state.pagecount
2083 then ()
2084 else
2085 let pdimno, ((_, w, h, _) as pdim), pdims =
2086 match pdims with
2087 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2088 pdimno+1, pdim, rest
2089 | _ ->
2090 pdimno, pdim, pdims
2092 let cw = w / c in
2093 let rec loop1 n x y =
2094 if n = c then y else (
2095 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2096 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2099 let y = loop1 0 0 y in
2100 loop (pageno+1) pdimno pdim y pdims
2102 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2103 conf.columns <- Csplit (c, a);
2106 let represent () =
2107 docolumns conf.columns;
2108 state.maxy <- calcheight ();
2109 state.hscrollh <-
2110 if state.x = 0 && state.w <= state.winw - state.scrollw
2111 then 0
2112 else state.scrollw
2114 if state.reprf == noreprf
2115 then (
2116 match state.mode with
2117 | Birdseye (_, _, pageno, _, _) ->
2118 let y, h = getpageyh pageno in
2119 let top = (state.winh - h) / 2 in
2120 gotoy (max 0 (y - top))
2121 | _ -> gotoanchor state.anchor
2123 else (
2124 state.reprf ();
2125 state.reprf <- noreprf;
2129 let reshape w h =
2130 GlDraw.viewport 0 0 w h;
2131 let firsttime = state.geomcmds == firstgeomcmds in
2132 if not firsttime && nogeomcmds state.geomcmds
2133 then state.anchor <- getanchor ();
2135 state.winw <- w;
2136 let w = truncate (float w *. conf.zoom) - state.scrollw in
2137 let w = max w 2 in
2138 state.winh <- h;
2139 setfontsize fstate.fontsize;
2140 GlMat.mode `modelview;
2141 GlMat.load_identity ();
2143 GlMat.mode `projection;
2144 GlMat.load_identity ();
2145 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2146 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2147 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2149 let relx =
2150 if conf.zoom <= 1.0
2151 then 0.0
2152 else float state.x /. float state.w
2154 invalidate "geometry"
2155 (fun () ->
2156 state.w <- w;
2157 if not firsttime
2158 then state.x <- truncate (relx *. float w);
2159 let w =
2160 match conf.columns with
2161 | Csingle _ -> w
2162 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2163 | Csplit (c, _) -> w * c
2165 wcmd "geometry %d %d %f" w (h - 2*conf.interpagespace) conf.zoom);
2168 let enttext () =
2169 let len = String.length state.text in
2170 let drawstring s =
2171 let hscrollh =
2172 match state.mode with
2173 | Textentry _
2174 | View ->
2175 let h, _, _ = state.uioh#scrollpw in
2177 | _ -> 0
2179 let rect x w =
2180 GlDraw.rect
2181 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2182 (x+.w, float (state.winh - hscrollh))
2185 let w = float (state.winw - state.scrollw - 1) in
2186 if state.progress >= 0.0 && state.progress < 1.0
2187 then (
2188 GlDraw.color (0.3, 0.3, 0.3);
2189 let w1 = w *. state.progress in
2190 rect 0.0 w1;
2191 GlDraw.color (0.0, 0.0, 0.0);
2192 rect w1 (w-.w1)
2194 else (
2195 GlDraw.color (0.0, 0.0, 0.0);
2196 rect 0.0 w;
2199 GlDraw.color (1.0, 1.0, 1.0);
2200 drawstring fstate.fontsize
2201 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2203 let s =
2204 match state.mode with
2205 | Textentry ((prefix, text, _, _, _, _), _) ->
2206 let s =
2207 if len > 0
2208 then
2209 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2210 else
2211 Printf.sprintf "%s%s_" prefix text
2215 | _ -> state.text
2217 let s =
2218 if state.newerrmsgs
2219 then (
2220 if not (istextentry state.mode) && state.uioh#eformsgs
2221 then
2222 let s1 = "(press 'e' to review error messasges)" in
2223 if String.length s > 0 then s ^ " " ^ s1 else s1
2224 else s
2226 else s
2228 if String.length s > 0
2229 then drawstring s
2232 let gctiles () =
2233 let len = Queue.length state.tilelru in
2234 let layout = lazy (
2235 match state.throttle with
2236 | None ->
2237 if conf.preload
2238 then preloadlayout state.y
2239 else state.layout
2240 | Some (layout, _, _) ->
2241 layout
2242 ) in
2243 let rec loop qpos =
2244 if state.memused <= conf.memlimit
2245 then ()
2246 else (
2247 if qpos < len
2248 then
2249 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2250 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2251 let (_, pw, ph, _) = getpagedim n in
2253 gen = state.gen
2254 && colorspace = conf.colorspace
2255 && angle = conf.angle
2256 && pagew = pw
2257 && pageh = ph
2258 && (
2259 let x = col*conf.tilew
2260 and y = row*conf.tileh in
2261 tilevisible (Lazy.force_val layout) n x y
2263 then Queue.push lruitem state.tilelru
2264 else (
2265 freepbo p;
2266 wcmd "freetile %s" p;
2267 state.memused <- state.memused - s;
2268 state.uioh#infochanged Memused;
2269 Hashtbl.remove state.tilemap k;
2271 loop (qpos+1)
2274 loop 0
2277 let logcurrently = function
2278 | Idle -> dolog "Idle"
2279 | Loading (l, gen) ->
2280 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2281 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2282 dolog
2283 "Tiling %d[%d,%d] page=%s cs=%s angle"
2284 l.pageno col row pageopaque
2285 (colorspace_to_string colorspace)
2287 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2288 angle gen conf.angle state.gen
2289 tilew tileh
2290 conf.tilew conf.tileh
2292 | Outlining _ ->
2293 dolog "outlining"
2296 let splitatspace =
2297 let r = Str.regexp " " in
2298 fun s -> Str.bounded_split r s 2;
2301 let onpagerect pageno f =
2302 let b =
2303 match conf.columns with
2304 | Cmulti (_, b) -> b
2305 | Csingle b -> b
2306 | Csplit (_, b) -> b
2308 if pageno >= 0 && pageno < Array.length b
2309 then
2310 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2311 let r = getpdimrect pdimno in
2312 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2315 let gotopagexy1 pageno x y =
2316 onpagerect pageno (fun w h ->
2317 let top = y /. h in
2318 let _,w1,_,leftx = getpagedim pageno in
2319 let wh = state.winh - state.hscrollh in
2320 let sw = float w1 /. w in
2321 let x = sw *. x in
2322 let x = leftx + state.x + truncate x in
2323 let sx =
2324 if x < 0 || x >= state.winw - state.scrollw
2325 then state.x - x
2326 else state.x
2328 let py, h = getpageyh pageno in
2329 let pdy = truncate (top *. float h) in
2330 let y' = py + pdy in
2331 let dy = y' - state.y in
2332 let sy =
2333 if x != state.x || not (dy > 0 && dy < wh)
2334 then (
2335 if conf.presentation
2336 then
2337 if abs (py - y') > wh
2338 then y'
2339 else py
2340 else y';
2342 else state.y
2344 if state.x != sx || state.y != sy
2345 then (
2346 let x, y =
2347 if !wtmode
2348 then (
2349 let ww = state.winw - state.scrollw in
2350 let qx = sx / ww
2351 and qy = pdy / wh in
2352 let x = qx * ww
2353 and y = py + qy * wh in
2354 let x = if -x + ww > w1 then -(w1-ww) else x
2355 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2356 let y =
2357 if conf.presentation
2358 then
2359 if abs (py - y') > wh
2360 then y'
2361 else py
2362 else y';
2364 (x, y)
2366 else (sx, sy)
2368 state.x <- x;
2369 state.hscrollh <-
2370 if x = 0 && state.w <= state.winw - state.scrollw
2371 then 0
2372 else state.scrollw
2374 gotoy_and_clear_text y;
2376 else gotoy_and_clear_text state.y;
2380 let gotopagexy pageno x y =
2381 match state.mode with
2382 | Birdseye _ -> gotopage pageno 0.0
2383 | _ -> gotopagexy1 pageno x y
2386 let act cmds =
2387 (* dolog "%S" cmds; *)
2388 let cl = splitatspace cmds in
2389 let scan s fmt f =
2390 try Scanf.sscanf s fmt f
2391 with exn ->
2392 dolog "error processing '%S': %s" cmds (exntos exn);
2393 exit 1
2395 match cl with
2396 | "clear" :: [] ->
2397 state.uioh#infochanged Pdim;
2398 state.pdims <- [];
2400 | "clearrects" :: [] ->
2401 state.rects <- state.rects1;
2402 G.postRedisplay "clearrects";
2404 | "continue" :: args :: [] ->
2405 let n = scan args "%u" (fun n -> n) in
2406 state.pagecount <- n;
2407 begin match state.currently with
2408 | Outlining l ->
2409 state.currently <- Idle;
2410 state.outlines <- Array.of_list (List.rev l)
2411 | _ -> ()
2412 end;
2414 let cur, cmds = state.geomcmds in
2415 if String.length cur = 0
2416 then failwith "umpossible";
2418 begin match List.rev cmds with
2419 | [] ->
2420 state.geomcmds <- "", [];
2421 represent ();
2422 | (s, f) :: rest ->
2423 f ();
2424 state.geomcmds <- s, List.rev rest;
2425 end;
2426 if conf.maxwait = None && not !wtmode
2427 then G.postRedisplay "continue";
2429 | "title" :: args :: [] ->
2430 Wsi.settitle args
2432 | "msg" :: args :: [] ->
2433 showtext ' ' args
2435 | "vmsg" :: args :: [] ->
2436 if conf.verbose
2437 then showtext ' ' args
2439 | "emsg" :: args :: [] ->
2440 Buffer.add_string state.errmsgs args;
2441 state.newerrmsgs <- true;
2442 G.postRedisplay "error message"
2444 | "progress" :: args :: [] ->
2445 let progress, text =
2446 scan args "%f %n"
2447 (fun f pos ->
2448 f, String.sub args pos (String.length args - pos))
2450 state.text <- text;
2451 state.progress <- progress;
2452 G.postRedisplay "progress"
2454 | "firstmatch" :: args :: [] ->
2455 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2456 scan args "%u %d %f %f %f %f %f %f %f %f"
2457 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2458 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2460 let y = (getpagey pageno) + truncate y0 in
2461 addnav ();
2462 gotoy y;
2463 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2465 | "match" :: args :: [] ->
2466 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2467 scan args "%u %d %f %f %f %f %f %f %f %f"
2468 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2469 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2471 state.rects1 <-
2472 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2474 | "page" :: args :: [] ->
2475 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2476 begin match state.currently with
2477 | Loading (l, gen) ->
2478 vlog "page %d took %f sec" l.pageno t;
2479 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2480 begin match state.throttle with
2481 | None ->
2482 let preloadedpages =
2483 if conf.preload
2484 then preloadlayout state.y
2485 else state.layout
2487 let evict () =
2488 let set =
2489 List.fold_left (fun s l -> IntSet.add l.pageno s)
2490 IntSet.empty preloadedpages
2492 let evictedpages =
2493 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2494 if not (IntSet.mem pageno set)
2495 then (
2496 wcmd "freepage %s" opaque;
2497 key :: accu
2499 else accu
2500 ) state.pagemap []
2502 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2504 evict ();
2505 state.currently <- Idle;
2506 if gen = state.gen
2507 then (
2508 tilepage l.pageno pageopaque state.layout;
2509 load state.layout;
2510 load preloadedpages;
2511 if pagevisible state.layout l.pageno
2512 && layoutready state.layout
2513 then G.postRedisplay "page";
2516 | Some (layout, _, _) ->
2517 state.currently <- Idle;
2518 tilepage l.pageno pageopaque layout;
2519 load state.layout
2520 end;
2522 | _ ->
2523 dolog "Inconsistent loading state";
2524 logcurrently state.currently;
2525 exit 1
2528 | "tile" :: args :: [] ->
2529 let (x, y, opaque, size, t) =
2530 scan args "%u %u %s %u %f"
2531 (fun x y p size t -> (x, y, p, size, t))
2533 begin match state.currently with
2534 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2535 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2537 unmappbo opaque;
2538 if tilew != conf.tilew || tileh != conf.tileh
2539 then (
2540 wcmd "freetile %s" opaque;
2541 state.currently <- Idle;
2542 load state.layout;
2544 else (
2545 puttileopaque l col row gen cs angle opaque size t;
2546 state.memused <- state.memused + size;
2547 state.uioh#infochanged Memused;
2548 gctiles ();
2549 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2550 opaque, size) state.tilelru;
2552 let layout =
2553 match state.throttle with
2554 | None -> state.layout
2555 | Some (layout, _, _) -> layout
2558 state.currently <- Idle;
2559 if gen = state.gen
2560 && conf.colorspace = cs
2561 && conf.angle = angle
2562 && tilevisible layout l.pageno x y
2563 then conttiling l.pageno pageopaque;
2565 begin match state.throttle with
2566 | None ->
2567 preload state.layout;
2568 if gen = state.gen
2569 && conf.colorspace = cs
2570 && conf.angle = angle
2571 && tilevisible state.layout l.pageno x y
2572 && (not !wtmode || layoutready state.layout)
2573 then G.postRedisplay "tile nothrottle";
2575 | Some (layout, y, _) ->
2576 let ready = layoutready layout in
2577 if ready
2578 then (
2579 state.y <- y;
2580 state.layout <- layout;
2581 state.throttle <- None;
2582 G.postRedisplay "throttle";
2584 else load layout;
2585 end;
2588 | _ ->
2589 dolog "Inconsistent tiling state";
2590 logcurrently state.currently;
2591 exit 1
2594 | "pdim" :: args :: [] ->
2595 let pdim =
2596 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2598 state.uioh#infochanged Pdim;
2599 state.pdims <- pdim :: state.pdims
2601 | "o" :: args :: [] ->
2602 let (l, n, t, h, pos) =
2603 scan args "%u %u %d %u %n"
2604 (fun l n t h pos -> l, n, t, h, pos)
2606 let s = String.sub args pos (String.length args - pos) in
2607 let outline = (s, l, (n, float t /. float h, 0.0)) in
2608 begin match state.currently with
2609 | Outlining outlines ->
2610 state.currently <- Outlining (outline :: outlines)
2611 | Idle ->
2612 state.currently <- Outlining [outline]
2613 | currently ->
2614 dolog "invalid outlining state";
2615 logcurrently currently
2618 | "a" :: args :: [] ->
2619 let (n, l, t) =
2620 scan args "%u %d %d" (fun n l t -> n, l, t)
2622 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2624 | "info" :: args :: [] ->
2625 state.docinfo <- (1, args) :: state.docinfo
2627 | "infoend" :: [] ->
2628 state.uioh#infochanged Docinfo;
2629 state.docinfo <- List.rev state.docinfo
2631 | _ ->
2632 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2635 let onhist cb =
2636 let rc = cb.rc in
2637 let action = function
2638 | HCprev -> cbget cb ~-1
2639 | HCnext -> cbget cb 1
2640 | HCfirst -> cbget cb ~-(cb.rc)
2641 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2642 and cancel () = cb.rc <- rc
2643 in (action, cancel)
2646 let search pattern forward =
2647 if String.length pattern > 0
2648 then
2649 let pn, py =
2650 match state.layout with
2651 | [] -> 0, 0
2652 | l :: _ ->
2653 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2655 wcmd "search %d %d %d %d,%s\000"
2656 (btod conf.icase) pn py (btod forward) pattern;
2659 let intentry text key =
2660 let c =
2661 if key >= 32 && key < 127
2662 then Char.chr key
2663 else '\000'
2665 match c with
2666 | '0' .. '9' ->
2667 let text = addchar text c in
2668 TEcont text
2670 | _ ->
2671 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2672 TEcont text
2675 let linknentry text key =
2676 let c =
2677 if key >= 32 && key < 127
2678 then Char.chr key
2679 else '\000'
2681 match c with
2682 | 'a' .. 'z' ->
2683 let text = addchar text c in
2684 TEcont text
2686 | _ ->
2687 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2688 TEcont text
2691 let linkndone f s =
2692 if String.length s > 0
2693 then (
2694 let n =
2695 let l = String.length s in
2696 let rec loop pos n = if pos = l then n else
2697 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2698 loop (pos+1) (n*26 + m)
2699 in loop 0 0
2701 let rec loop n = function
2702 | [] -> ()
2703 | l :: rest ->
2704 match getopaque l.pageno with
2705 | None -> loop n rest
2706 | Some opaque ->
2707 let m = getlinkcount opaque in
2708 if n < m
2709 then (
2710 let under = getlink opaque n in
2711 f under
2713 else loop (n-m) rest
2715 loop n state.layout;
2719 let textentry text key =
2720 if key land 0xff00 = 0xff00
2721 then TEcont text
2722 else TEcont (text ^ toutf8 key)
2725 let reqlayout angle fitmodel =
2726 match state.throttle with
2727 | None ->
2728 if nogeomcmds state.geomcmds
2729 then state.anchor <- getanchor ();
2730 conf.angle <- angle mod 360;
2731 if conf.angle != 0
2732 then (
2733 match state.mode with
2734 | LinkNav _ -> state.mode <- View
2735 | _ -> ()
2737 conf.fitmodel <- fitmodel;
2738 invalidate "reqlayout"
2739 (fun () ->
2740 wcmd "reqlayout %d %d %f"
2741 conf.angle (int_of_fitmodel fitmodel) conf.zoom);
2742 | _ -> ()
2745 let settrim trimmargins trimfuzz =
2746 if nogeomcmds state.geomcmds
2747 then state.anchor <- getanchor ();
2748 conf.trimmargins <- trimmargins;
2749 conf.trimfuzz <- trimfuzz;
2750 let x0, y0, x1, y1 = trimfuzz in
2751 invalidate "settrim"
2752 (fun () ->
2753 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2754 flushpages ();
2757 let setzoom zoom =
2758 match state.throttle with
2759 | None ->
2760 let zoom = max 0.0001 zoom in
2761 if zoom <> conf.zoom
2762 then (
2763 state.prevzoom <- conf.zoom;
2764 conf.zoom <- zoom;
2765 reshape state.winw state.winh;
2766 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2769 | Some (layout, y, started) ->
2770 let time =
2771 match conf.maxwait with
2772 | None -> 0.0
2773 | Some t -> t
2775 let dt = now () -. started in
2776 if dt > time
2777 then (
2778 state.y <- y;
2779 load layout;
2783 let setcolumns mode columns coverA coverB =
2784 state.prevcolumns <- Some (conf.columns, conf.zoom);
2785 if columns < 0
2786 then (
2787 if isbirdseye mode
2788 then showtext '!' "split mode doesn't work in bird's eye"
2789 else (
2790 conf.columns <- Csplit (-columns, [||]);
2791 state.x <- 0;
2792 conf.zoom <- 1.0;
2795 else (
2796 if columns < 2
2797 then (
2798 conf.columns <- Csingle [||];
2799 state.x <- 0;
2800 setzoom 1.0;
2802 else (
2803 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2804 conf.zoom <- 1.0;
2807 reshape state.winw state.winh;
2810 let enterbirdseye () =
2811 let zoom = float conf.thumbw /. float state.winw in
2812 let birdseyepageno =
2813 let cy = state.winh / 2 in
2814 let fold = function
2815 | [] -> 0
2816 | l :: rest ->
2817 let rec fold best = function
2818 | [] -> best.pageno
2819 | l :: rest ->
2820 let d = cy - (l.pagedispy + l.pagevh/2)
2821 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2822 if abs d < abs dbest
2823 then fold l rest
2824 else best.pageno
2825 in fold l rest
2827 fold state.layout
2829 state.mode <- Birdseye (
2830 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2832 conf.zoom <- zoom;
2833 conf.presentation <- false;
2834 conf.interpagespace <- 10;
2835 conf.hlinks <- false;
2836 state.x <- 0;
2837 state.mstate <- Mnone;
2838 conf.maxwait <- None;
2839 conf.columns <- (
2840 match conf.beyecolumns with
2841 | Some c ->
2842 conf.zoom <- 1.0;
2843 Cmulti ((c, 0, 0), [||])
2844 | None -> Csingle [||]
2846 Wsi.setcursor Wsi.CURSOR_INHERIT;
2847 if conf.verbose
2848 then
2849 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2850 (100.0*.zoom)
2851 else
2852 state.text <- ""
2854 reshape state.winw state.winh;
2857 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2858 state.mode <- View;
2859 conf.zoom <- c.zoom;
2860 conf.presentation <- c.presentation;
2861 conf.interpagespace <- c.interpagespace;
2862 conf.maxwait <- c.maxwait;
2863 conf.hlinks <- c.hlinks;
2864 conf.beyecolumns <- (
2865 match conf.columns with
2866 | Cmulti ((c, _, _), _) -> Some c
2867 | Csingle _ -> None
2868 | Csplit _ -> failwith "leaving bird's eye split mode"
2870 conf.columns <- (
2871 match c.columns with
2872 | Cmulti (c, _) -> Cmulti (c, [||])
2873 | Csingle _ -> Csingle [||]
2874 | Csplit (c, _) -> Csplit (c, [||])
2876 state.x <- leftx;
2877 if conf.verbose
2878 then
2879 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2880 (100.0*.conf.zoom)
2882 reshape state.winw state.winh;
2883 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2886 let togglebirdseye () =
2887 match state.mode with
2888 | Birdseye vals -> leavebirdseye vals true
2889 | View -> enterbirdseye ()
2890 | _ -> ()
2893 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2894 let pageno = max 0 (pageno - incr) in
2895 let rec loop = function
2896 | [] -> gotopage1 pageno 0
2897 | l :: _ when l.pageno = pageno ->
2898 if l.pagedispy >= 0 && l.pagey = 0
2899 then G.postRedisplay "upbirdseye"
2900 else gotopage1 pageno 0
2901 | _ :: rest -> loop rest
2903 loop state.layout;
2904 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2907 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2908 let pageno = min (state.pagecount - 1) (pageno + incr) in
2909 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2910 let rec loop = function
2911 | [] ->
2912 let y, h = getpageyh pageno in
2913 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2914 gotoy (clamp dy)
2915 | l :: _ when l.pageno = pageno ->
2916 if l.pagevh != l.pageh
2917 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2918 else G.postRedisplay "downbirdseye"
2919 | _ :: rest -> loop rest
2921 loop state.layout
2924 let optentry mode _ key =
2925 let btos b = if b then "on" else "off" in
2926 if key >= 32 && key < 127
2927 then
2928 let c = Char.chr key in
2929 match c with
2930 | 's' ->
2931 let ondone s =
2932 try conf.scrollstep <- int_of_string s with exc ->
2933 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2935 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2937 | 'A' ->
2938 let ondone s =
2940 conf.autoscrollstep <- int_of_string s;
2941 if state.autoscroll <> None
2942 then state.autoscroll <- Some conf.autoscrollstep
2943 with exc ->
2944 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2946 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2948 | 'C' ->
2949 let ondone s =
2951 let n, a, b = multicolumns_of_string s in
2952 setcolumns mode n a b;
2953 with exc ->
2954 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2956 TEswitch ("columns: ", "", None, textentry, ondone, true)
2958 | 'Z' ->
2959 let ondone s =
2961 let zoom = float (int_of_string s) /. 100.0 in
2962 setzoom zoom
2963 with exc ->
2964 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2966 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2968 | 't' ->
2969 let ondone s =
2971 conf.thumbw <- bound (int_of_string s) 2 4096;
2972 state.text <-
2973 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2974 begin match mode with
2975 | Birdseye beye ->
2976 leavebirdseye beye false;
2977 enterbirdseye ();
2978 | _ -> ();
2980 with exc ->
2981 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2983 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2985 | 'R' ->
2986 let ondone s =
2987 match try
2988 Some (int_of_string s)
2989 with exc ->
2990 state.text <- Printf.sprintf "bad integer `%s': %s"
2991 s (exntos exc);
2992 None
2993 with
2994 | Some angle -> reqlayout angle conf.fitmodel
2995 | None -> ()
2997 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2999 | 'i' ->
3000 conf.icase <- not conf.icase;
3001 TEdone ("case insensitive search " ^ (btos conf.icase))
3003 | 'p' ->
3004 conf.preload <- not conf.preload;
3005 gotoy state.y;
3006 TEdone ("preload " ^ (btos conf.preload))
3008 | 'v' ->
3009 conf.verbose <- not conf.verbose;
3010 TEdone ("verbose " ^ (btos conf.verbose))
3012 | 'd' ->
3013 conf.debug <- not conf.debug;
3014 TEdone ("debug " ^ (btos conf.debug))
3016 | 'h' ->
3017 conf.maxhfit <- not conf.maxhfit;
3018 state.maxy <- calcheight ();
3019 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3021 | 'c' ->
3022 conf.crophack <- not conf.crophack;
3023 TEdone ("crophack " ^ btos conf.crophack)
3025 | 'a' ->
3026 let s =
3027 match conf.maxwait with
3028 | None ->
3029 conf.maxwait <- Some infinity;
3030 "always wait for page to complete"
3031 | Some _ ->
3032 conf.maxwait <- None;
3033 "show placeholder if page is not ready"
3035 TEdone s
3037 | 'f' ->
3038 conf.underinfo <- not conf.underinfo;
3039 TEdone ("underinfo " ^ btos conf.underinfo)
3041 | 'P' ->
3042 conf.savebmarks <- not conf.savebmarks;
3043 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3045 | 'S' ->
3046 let ondone s =
3048 let pageno, py =
3049 match state.layout with
3050 | [] -> 0, 0
3051 | l :: _ ->
3052 l.pageno, l.pagey
3054 conf.interpagespace <- int_of_string s;
3055 docolumns conf.columns;
3056 state.maxy <- calcheight ();
3057 let y = getpagey pageno in
3058 gotoy (y + py)
3059 with exc ->
3060 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3062 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3064 | 'l' ->
3065 let fm =
3066 match conf.fitmodel with
3067 | FitProportional -> FitWidth
3068 | _ -> FitProportional
3070 reqlayout conf.angle fm;
3071 TEdone ("proportional display " ^ btos (fm == FitProportional))
3073 | 'T' ->
3074 settrim (not conf.trimmargins) conf.trimfuzz;
3075 TEdone ("trim margins " ^ btos conf.trimmargins)
3077 | 'I' ->
3078 conf.invert <- not conf.invert;
3079 TEdone ("invert colors " ^ btos conf.invert)
3081 | 'x' ->
3082 let ondone s =
3083 cbput state.hists.sel s;
3084 conf.selcmd <- s;
3086 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3087 textentry, ondone, true)
3089 | _ ->
3090 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3091 TEstop
3092 else
3093 TEcont state.text
3096 class type lvsource = object
3097 method getitemcount : int
3098 method getitem : int -> (string * int)
3099 method hasaction : int -> bool
3100 method exit :
3101 uioh:uioh ->
3102 cancel:bool ->
3103 active:int ->
3104 first:int ->
3105 pan:int ->
3106 qsearch:string ->
3107 uioh option
3108 method getactive : int
3109 method getfirst : int
3110 method getqsearch : string
3111 method setqsearch : string -> unit
3112 method getpan : int
3113 end;;
3115 class virtual lvsourcebase = object
3116 val mutable m_active = 0
3117 val mutable m_first = 0
3118 val mutable m_qsearch = ""
3119 val mutable m_pan = 0
3120 method getactive = m_active
3121 method getfirst = m_first
3122 method getqsearch = m_qsearch
3123 method getpan = m_pan
3124 method setqsearch s = m_qsearch <- s
3125 end;;
3127 let withoutlastutf8 s =
3128 let len = String.length s in
3129 if len = 0
3130 then s
3131 else
3132 let rec find pos =
3133 if pos = 0
3134 then pos
3135 else
3136 let b = Char.code s.[pos] in
3137 if b land 0b11000000 = 0b11000000
3138 then pos
3139 else find (pos-1)
3141 let first =
3142 if Char.code s.[len-1] land 0x80 = 0
3143 then len-1
3144 else find (len-1)
3146 String.sub s 0 first;
3149 let textentrykeyboard
3150 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3151 let key =
3152 if key >= 0xffb0 && key <= 0xffb9
3153 then key - 0xffb0 + 48 else key
3155 let enttext te =
3156 state.mode <- Textentry (te, onleave);
3157 state.text <- "";
3158 enttext ();
3159 G.postRedisplay "textentrykeyboard enttext";
3161 let histaction cmd =
3162 match opthist with
3163 | None -> ()
3164 | Some (action, _) ->
3165 state.mode <- Textentry (
3166 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3168 G.postRedisplay "textentry histaction"
3170 match key with
3171 | 0xff08 -> (* backspace *)
3172 let s = withoutlastutf8 text in
3173 let len = String.length s in
3174 if cancelonempty && len = 0
3175 then (
3176 onleave Cancel;
3177 G.postRedisplay "textentrykeyboard after cancel";
3179 else (
3180 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3183 | 0xff0d | 0xff8d -> (* (kp) enter *)
3184 ondone text;
3185 onleave Confirm;
3186 G.postRedisplay "textentrykeyboard after confirm"
3188 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3189 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3190 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3191 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3193 | 0xff1b -> (* escape*)
3194 if String.length text = 0
3195 then (
3196 begin match opthist with
3197 | None -> ()
3198 | Some (_, onhistcancel) -> onhistcancel ()
3199 end;
3200 onleave Cancel;
3201 state.text <- "";
3202 G.postRedisplay "textentrykeyboard after cancel2"
3204 else (
3205 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3208 | 0xff9f | 0xffff -> () (* delete *)
3210 | _ when key != 0
3211 && key land 0xff00 != 0xff00 (* keyboard *)
3212 && key land 0xfe00 != 0xfe00 (* xkb *)
3213 && key land 0xfd00 != 0xfd00 (* 3270 *)
3215 begin match onkey text key with
3216 | TEdone text ->
3217 ondone text;
3218 onleave Confirm;
3219 G.postRedisplay "textentrykeyboard after confirm2";
3221 | TEcont text ->
3222 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3224 | TEstop ->
3225 onleave Cancel;
3226 G.postRedisplay "textentrykeyboard after cancel3"
3228 | TEswitch te ->
3229 state.mode <- Textentry (te, onleave);
3230 G.postRedisplay "textentrykeyboard switch";
3231 end;
3233 | _ ->
3234 vlog "unhandled key %s" (Wsi.keyname key)
3237 let firstof first active =
3238 if first > active || abs (first - active) > fstate.maxrows - 1
3239 then max 0 (active - (fstate.maxrows/2))
3240 else first
3243 let calcfirst first active =
3244 if active > first
3245 then
3246 let rows = active - first in
3247 if rows > fstate.maxrows then active - fstate.maxrows else first
3248 else active
3251 let scrollph y maxy =
3252 let sh = (float (maxy + state.winh) /. float state.winh) in
3253 let sh = float state.winh /. sh in
3254 let sh = max sh (float conf.scrollh) in
3256 let percent =
3257 if y = state.maxy
3258 then 1.0
3259 else float y /. float maxy
3261 let position = (float state.winh -. sh) *. percent in
3263 let position =
3264 if position +. sh > float state.winh
3265 then float state.winh -. sh
3266 else position
3268 position, sh;
3271 let coe s = (s :> uioh);;
3273 class listview ~(source:lvsource) ~trusted ~modehash =
3274 object (self)
3275 val m_pan = source#getpan
3276 val m_first = source#getfirst
3277 val m_active = source#getactive
3278 val m_qsearch = source#getqsearch
3279 val m_prev_uioh = state.uioh
3281 method private elemunder y =
3282 let n = y / (fstate.fontsize+1) in
3283 if m_first + n < source#getitemcount
3284 then (
3285 if source#hasaction (m_first + n)
3286 then Some (m_first + n)
3287 else None
3289 else None
3291 method display =
3292 Gl.enable `blend;
3293 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3294 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3295 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3296 GlDraw.color (1., 1., 1.);
3297 Gl.enable `texture_2d;
3298 let fs = fstate.fontsize in
3299 let nfs = fs + 1 in
3300 let ww = fstate.wwidth in
3301 let tabw = 30.0*.ww in
3302 let itemcount = source#getitemcount in
3303 let rec loop row =
3304 if (row - m_first) > fstate.maxrows
3305 then ()
3306 else (
3307 if row >= 0 && row < itemcount
3308 then (
3309 let (s, level) = source#getitem row in
3310 let y = (row - m_first) * nfs in
3311 let x = 5.0 +. float (level + m_pan) *. ww in
3312 if row = m_active
3313 then (
3314 Gl.disable `texture_2d;
3315 GlDraw.polygon_mode `both `line;
3316 let alpha = if source#hasaction row then 0.9 else 0.3 in
3317 GlDraw.color (1., 1., 1.) ~alpha;
3318 GlDraw.rect (1., float (y + 1))
3319 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3320 GlDraw.polygon_mode `both `fill;
3321 GlDraw.color (1., 1., 1.);
3322 Gl.enable `texture_2d;
3325 let drawtabularstring s =
3326 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3327 if trusted
3328 then
3329 let tabpos = try String.index s '\t' with Not_found -> -1 in
3330 if tabpos > 0
3331 then
3332 let len = String.length s - tabpos - 1 in
3333 let s1 = String.sub s 0 tabpos
3334 and s2 = String.sub s (tabpos + 1) len in
3335 let nx = drawstr x s1 in
3336 let sw = nx -. x in
3337 let x = x +. (max tabw sw) in
3338 drawstr x s2
3339 else
3340 drawstr x s
3341 else
3342 drawstr x s
3344 let _ = drawtabularstring s in
3345 loop (row+1)
3349 loop m_first;
3350 Gl.disable `blend;
3351 Gl.disable `texture_2d;
3353 method updownlevel incr =
3354 let len = source#getitemcount in
3355 let curlevel =
3356 if m_active >= 0 && m_active < len
3357 then snd (source#getitem m_active)
3358 else -1
3360 let rec flow i =
3361 if i = len then i-1 else if i = -1 then 0 else
3362 let _, l = source#getitem i in
3363 if l != curlevel then i else flow (i+incr)
3365 let active = flow m_active in
3366 let first = calcfirst m_first active in
3367 G.postRedisplay "outline updownlevel";
3368 {< m_active = active; m_first = first >}
3370 method private key1 key mask =
3371 let set1 active first qsearch =
3372 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3374 let search active pattern incr =
3375 let active = if active = -1 then m_first else active in
3376 let dosearch re =
3377 let rec loop n =
3378 if n >= 0 && n < source#getitemcount
3379 then (
3380 let s, _ = source#getitem n in
3382 (try ignore (Str.search_forward re s 0); true
3383 with Not_found -> false)
3384 then Some n
3385 else loop (n + incr)
3387 else None
3389 loop active
3392 let re = Str.regexp_case_fold pattern in
3393 dosearch re
3394 with Failure s ->
3395 state.text <- s;
3396 None
3398 let itemcount = source#getitemcount in
3399 let find start incr =
3400 let rec find i =
3401 if i = -1 || i = itemcount
3402 then -1
3403 else (
3404 if source#hasaction i
3405 then i
3406 else find (i + incr)
3409 find start
3411 let set active first =
3412 let first = bound first 0 (itemcount - fstate.maxrows) in
3413 state.text <- "";
3414 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3416 let navigate incr =
3417 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3418 let active, first =
3419 let incr1 = if incr > 0 then 1 else -1 in
3420 if isvisible m_first m_active
3421 then
3422 let next =
3423 let next = m_active + incr in
3424 let next =
3425 if next < 0 || next >= itemcount
3426 then -1
3427 else find next incr1
3429 if next = -1 || abs (m_active - next) > fstate.maxrows
3430 then -1
3431 else next
3433 if next = -1
3434 then
3435 let first = m_first + incr in
3436 let first = bound first 0 (itemcount - 1) in
3437 let next =
3438 let next = m_active + incr in
3439 let next = bound next 0 (itemcount - 1) in
3440 find next ~-incr1
3442 let active = if next = -1 then m_active else next in
3443 active, first
3444 else
3445 let first = min next m_first in
3446 let first =
3447 if abs (next - first) > fstate.maxrows
3448 then first + incr
3449 else first
3451 next, first
3452 else
3453 let first = m_first + incr in
3454 let first = bound first 0 (itemcount - 1) in
3455 let active =
3456 let next = m_active + incr in
3457 let next = bound next 0 (itemcount - 1) in
3458 let next = find next incr1 in
3459 let active =
3460 if next = -1 || abs (m_active - first) > fstate.maxrows
3461 then (
3462 let active = if m_active = -1 then next else m_active in
3463 active
3465 else next
3467 if isvisible first active
3468 then active
3469 else -1
3471 active, first
3473 G.postRedisplay "listview navigate";
3474 set active first;
3476 match key with
3477 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3478 let incr = if key = 0x72 then -1 else 1 in
3479 let active, first =
3480 match search (m_active + incr) m_qsearch incr with
3481 | None ->
3482 state.text <- m_qsearch ^ " [not found]";
3483 m_active, m_first
3484 | Some active ->
3485 state.text <- m_qsearch;
3486 active, firstof m_first active
3488 G.postRedisplay "listview ctrl-r/s";
3489 set1 active first m_qsearch;
3491 | 0xff08 -> (* backspace *)
3492 if String.length m_qsearch = 0
3493 then coe self
3494 else (
3495 let qsearch = withoutlastutf8 m_qsearch in
3496 let len = String.length qsearch in
3497 if len = 0
3498 then (
3499 state.text <- "";
3500 G.postRedisplay "listview empty qsearch";
3501 set1 m_active m_first "";
3503 else
3504 let active, first =
3505 match search m_active qsearch ~-1 with
3506 | None ->
3507 state.text <- qsearch ^ " [not found]";
3508 m_active, m_first
3509 | Some active ->
3510 state.text <- qsearch;
3511 active, firstof m_first active
3513 G.postRedisplay "listview backspace qsearch";
3514 set1 active first qsearch
3517 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3518 let pattern = m_qsearch ^ toutf8 key in
3519 let active, first =
3520 match search m_active pattern 1 with
3521 | None ->
3522 state.text <- pattern ^ " [not found]";
3523 m_active, m_first
3524 | Some active ->
3525 state.text <- pattern;
3526 active, firstof m_first active
3528 G.postRedisplay "listview qsearch add";
3529 set1 active first pattern;
3531 | 0xff1b -> (* escape *)
3532 state.text <- "";
3533 if String.length m_qsearch = 0
3534 then (
3535 G.postRedisplay "list view escape";
3536 begin
3537 match
3538 source#exit (coe self) true m_active m_first m_pan m_qsearch
3539 with
3540 | None -> m_prev_uioh
3541 | Some uioh -> uioh
3544 else (
3545 G.postRedisplay "list view kill qsearch";
3546 source#setqsearch "";
3547 coe {< m_qsearch = "" >}
3550 | 0xff0d | 0xff8d -> (* (kp) enter *)
3551 state.text <- "";
3552 let self = {< m_qsearch = "" >} in
3553 source#setqsearch "";
3554 let opt =
3555 G.postRedisplay "listview enter";
3556 if m_active >= 0 && m_active < source#getitemcount
3557 then (
3558 source#exit (coe self) false m_active m_first m_pan "";
3560 else (
3561 source#exit (coe self) true m_active m_first m_pan "";
3564 begin match opt with
3565 | None -> m_prev_uioh
3566 | Some uioh -> uioh
3569 | 0xff9f | 0xffff -> (* (kp) delete *)
3570 coe self
3572 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3573 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3574 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3575 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3577 | 0xff53 | 0xff98 -> (* (kp) right *)
3578 state.text <- "";
3579 G.postRedisplay "listview right";
3580 coe {< m_pan = m_pan - 1 >}
3582 | 0xff51 | 0xff96 -> (* (kp) left *)
3583 state.text <- "";
3584 G.postRedisplay "listview left";
3585 coe {< m_pan = m_pan + 1 >}
3587 | 0xff50 | 0xff95 -> (* (kp) home *)
3588 let active = find 0 1 in
3589 G.postRedisplay "listview home";
3590 set active 0;
3592 | 0xff57 | 0xff9c -> (* (kp) end *)
3593 let first = max 0 (itemcount - fstate.maxrows) in
3594 let active = find (itemcount - 1) ~-1 in
3595 G.postRedisplay "listview end";
3596 set active first;
3598 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3599 coe self
3601 | _ ->
3602 dolog "listview unknown key %#x" key; coe self
3604 method key key mask =
3605 match state.mode with
3606 | Textentry te -> textentrykeyboard key mask te; coe self
3607 | _ -> self#key1 key mask
3609 method button button down x y _ =
3610 let opt =
3611 match button with
3612 | 1 when x > state.winw - conf.scrollbw ->
3613 G.postRedisplay "listview scroll";
3614 if down
3615 then
3616 let _, position, sh = self#scrollph in
3617 if y > truncate position && y < truncate (position +. sh)
3618 then (
3619 state.mstate <- Mscrolly;
3620 Some (coe self)
3622 else
3623 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3624 let first = truncate (s *. float source#getitemcount) in
3625 let first = min source#getitemcount first in
3626 Some (coe {< m_first = first; m_active = first >})
3627 else (
3628 state.mstate <- Mnone;
3629 Some (coe self);
3631 | 1 when not down ->
3632 begin match self#elemunder y with
3633 | Some n ->
3634 G.postRedisplay "listview click";
3635 source#exit
3636 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3637 | _ ->
3638 Some (coe self)
3640 | n when (n == 4 || n == 5) && not down ->
3641 let len = source#getitemcount in
3642 let first =
3643 if n = 5 && m_first + fstate.maxrows >= len
3644 then
3645 m_first
3646 else
3647 let first = m_first + (if n == 4 then -1 else 1) in
3648 bound first 0 (len - 1)
3650 G.postRedisplay "listview wheel";
3651 Some (coe {< m_first = first >})
3652 | n when (n = 6 || n = 7) && not down ->
3653 let inc = m_first + (if n = 7 then -1 else 1) in
3654 G.postRedisplay "listview hwheel";
3655 Some (coe {< m_pan = m_pan + inc >})
3656 | _ ->
3657 Some (coe self)
3659 match opt with
3660 | None -> m_prev_uioh
3661 | Some uioh -> uioh
3663 method motion _ y =
3664 match state.mstate with
3665 | Mscrolly ->
3666 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3667 let first = truncate (s *. float source#getitemcount) in
3668 let first = min source#getitemcount first in
3669 G.postRedisplay "listview motion";
3670 coe {< m_first = first; m_active = first >}
3671 | _ -> coe self
3673 method pmotion x y =
3674 if x < state.winw - conf.scrollbw
3675 then
3676 let n =
3677 match self#elemunder y with
3678 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3679 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3681 let o =
3682 if n != m_active
3683 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3684 else self
3686 coe o
3687 else (
3688 Wsi.setcursor Wsi.CURSOR_INHERIT;
3689 coe self
3692 method infochanged _ = ()
3694 method scrollpw = (0, 0.0, 0.0)
3695 method scrollph =
3696 let nfs = fstate.fontsize + 1 in
3697 let y = m_first * nfs in
3698 let itemcount = source#getitemcount in
3699 let maxi = max 0 (itemcount - fstate.maxrows) in
3700 let maxy = maxi * nfs in
3701 let p, h = scrollph y maxy in
3702 conf.scrollbw, p, h
3704 method modehash = modehash
3705 method eformsgs = false
3706 end;;
3708 class outlinelistview ~source =
3709 object (self)
3710 inherit listview
3711 ~source:(source :> lvsource)
3712 ~trusted:false
3713 ~modehash:(findkeyhash conf "outline")
3714 as super
3716 method key key mask =
3717 let calcfirst first active =
3718 if active > first
3719 then
3720 let rows = active - first in
3721 let maxrows =
3722 if String.length state.text = 0
3723 then fstate.maxrows
3724 else fstate.maxrows - 2
3726 if rows > maxrows then active - maxrows else first
3727 else active
3729 let navigate incr =
3730 let active = m_active + incr in
3731 let active = bound active 0 (source#getitemcount - 1) in
3732 let first = calcfirst m_first active in
3733 G.postRedisplay "outline navigate";
3734 coe {< m_active = active; m_first = first >}
3736 let ctrl = Wsi.withctrl mask in
3737 match key with
3738 | 110 when ctrl -> (* ctrl-n *)
3739 source#narrow m_qsearch;
3740 G.postRedisplay "outline ctrl-n";
3741 coe {< m_first = 0; m_active = 0 >}
3743 | 117 when ctrl -> (* ctrl-u *)
3744 source#denarrow;
3745 G.postRedisplay "outline ctrl-u";
3746 state.text <- "";
3747 coe {< m_first = 0; m_active = 0 >}
3749 | 108 when ctrl -> (* ctrl-l *)
3750 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3751 G.postRedisplay "outline ctrl-l";
3752 coe {< m_first = first >}
3754 | 0xff9f | 0xffff -> (* (kp) delete *)
3755 source#remove m_active;
3756 G.postRedisplay "outline delete";
3757 let active = max 0 (m_active-1) in
3758 coe {< m_first = firstof m_first active;
3759 m_active = active >}
3761 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3762 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3763 | 0xff55 | 0xff9a -> (* (kp) prior *)
3764 navigate ~-(fstate.maxrows)
3765 | 0xff56 | 0xff9b -> (* (kp) next *)
3766 navigate fstate.maxrows
3768 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3769 let o =
3770 if ctrl
3771 then (
3772 G.postRedisplay "outline ctrl right";
3773 {< m_pan = m_pan + 1 >}
3775 else self#updownlevel 1
3777 coe o
3779 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3780 let o =
3781 if ctrl
3782 then (
3783 G.postRedisplay "outline ctrl left";
3784 {< m_pan = m_pan - 1 >}
3786 else self#updownlevel ~-1
3788 coe o
3790 | 0xff50 | 0xff95 -> (* (kp) home *)
3791 G.postRedisplay "outline home";
3792 coe {< m_first = 0; m_active = 0 >}
3794 | 0xff57 | 0xff9c -> (* (kp) end *)
3795 let active = source#getitemcount - 1 in
3796 let first = max 0 (active - fstate.maxrows) in
3797 G.postRedisplay "outline end";
3798 coe {< m_active = active; m_first = first >}
3800 | _ -> super#key key mask
3803 let outlinesource usebookmarks =
3804 let empty = [||] in
3805 (object
3806 inherit lvsourcebase
3807 val mutable m_items = empty
3808 val mutable m_orig_items = empty
3809 val mutable m_prev_items = empty
3810 val mutable m_narrow_pattern = ""
3811 val mutable m_hadremovals = false
3813 method getitemcount =
3814 Array.length m_items + (if m_hadremovals then 1 else 0)
3816 method getitem n =
3817 if n == Array.length m_items && m_hadremovals
3818 then
3819 ("[Confirm removal]", 0)
3820 else
3821 let s, n, _ = m_items.(n) in
3822 (s, n)
3824 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3825 ignore (uioh, first, qsearch);
3826 let confrimremoval = m_hadremovals && active = Array.length m_items in
3827 let items =
3828 if String.length m_narrow_pattern = 0
3829 then m_orig_items
3830 else m_items
3832 if not cancel
3833 then (
3834 if not confrimremoval
3835 then(
3836 let _, _, anchor = m_items.(active) in
3837 gotoghyll (getanchory anchor);
3838 m_items <- items;
3840 else (
3841 state.bookmarks <- Array.to_list m_items;
3842 m_orig_items <- m_items;
3845 else m_items <- items;
3846 m_pan <- pan;
3847 None
3849 method hasaction _ = true
3851 method greetmsg =
3852 if Array.length m_items != Array.length m_orig_items
3853 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3854 else ""
3856 method narrow pattern =
3857 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3858 match reopt with
3859 | None -> ()
3860 | Some re ->
3861 let rec loop accu n =
3862 if n = -1
3863 then (
3864 m_narrow_pattern <- pattern;
3865 m_items <- Array.of_list accu
3867 else
3868 let (s, _, _) as o = m_items.(n) in
3869 let accu =
3870 if (try ignore (Str.search_forward re s 0); true
3871 with Not_found -> false)
3872 then o :: accu
3873 else accu
3875 loop accu (n-1)
3877 loop [] (Array.length m_items - 1)
3879 method denarrow =
3880 m_orig_items <- (
3881 if usebookmarks
3882 then Array.of_list state.bookmarks
3883 else state.outlines
3885 m_items <- m_orig_items
3887 method remove m =
3888 if usebookmarks
3889 then
3890 if m >= 0 && m < Array.length m_items
3891 then (
3892 m_hadremovals <- true;
3893 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3894 let n = if n >= m then n+1 else n in
3895 m_items.(n)
3899 method reset anchor items =
3900 m_hadremovals <- false;
3901 if m_orig_items == empty || m_prev_items != items
3902 then (
3903 m_orig_items <- items;
3904 if String.length m_narrow_pattern = 0
3905 then m_items <- items;
3907 m_prev_items <- items;
3908 let rely = getanchory anchor in
3909 let active =
3910 let rec loop n best bestd =
3911 if n = Array.length m_items
3912 then best
3913 else
3914 let (_, _, anchor) = m_items.(n) in
3915 let orely = getanchory anchor in
3916 let d = abs (orely - rely) in
3917 if d < bestd
3918 then loop (n+1) n d
3919 else loop (n+1) best bestd
3921 loop 0 ~-1 max_int
3923 m_active <- active;
3924 m_first <- firstof m_first active
3925 end)
3928 let enterselector usebookmarks =
3929 let source = outlinesource usebookmarks in
3930 fun errmsg ->
3931 let outlines =
3932 if usebookmarks
3933 then Array.of_list state.bookmarks
3934 else state.outlines
3936 if Array.length outlines = 0
3937 then (
3938 showtext ' ' errmsg;
3940 else (
3941 state.text <- source#greetmsg;
3942 Wsi.setcursor Wsi.CURSOR_INHERIT;
3943 let anchor = getanchor () in
3944 source#reset anchor outlines;
3945 state.uioh <- coe (new outlinelistview ~source);
3946 G.postRedisplay "enter selector";
3950 let enteroutlinemode =
3951 let f = enterselector false in
3952 fun ()-> f "Document has no outline";
3955 let enterbookmarkmode =
3956 let f = enterselector true in
3957 fun () -> f "Document has no bookmarks (yet)";
3960 let color_of_string s =
3961 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3962 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3966 let color_to_string (r, g, b) =
3967 let r = truncate (r *. 256.0)
3968 and g = truncate (g *. 256.0)
3969 and b = truncate (b *. 256.0) in
3970 Printf.sprintf "%d/%d/%d" r g b
3973 let irect_of_string s =
3974 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3977 let irect_to_string (x0,y0,x1,y1) =
3978 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3981 let makecheckers () =
3982 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3983 following to say:
3984 converted by Issac Trotts. July 25, 2002 *)
3985 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3986 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3987 let id = GlTex.gen_texture () in
3988 GlTex.bind_texture `texture_2d id;
3989 GlPix.store (`unpack_alignment 1);
3990 GlTex.image2d image;
3991 List.iter (GlTex.parameter ~target:`texture_2d)
3992 [ `mag_filter `nearest; `min_filter `nearest ];
3996 let setcheckers enabled =
3997 match state.texid with
3998 | None ->
3999 if enabled then state.texid <- Some (makecheckers ())
4001 | Some texid ->
4002 if not enabled
4003 then (
4004 GlTex.delete_texture texid;
4005 state.texid <- None;
4009 let int_of_string_with_suffix s =
4010 let l = String.length s in
4011 let s1, shift =
4012 if l > 1
4013 then
4014 let suffix = Char.lowercase s.[l-1] in
4015 match suffix with
4016 | 'k' -> String.sub s 0 (l-1), 10
4017 | 'm' -> String.sub s 0 (l-1), 20
4018 | 'g' -> String.sub s 0 (l-1), 30
4019 | _ -> s, 0
4020 else s, 0
4022 let n = int_of_string s1 in
4023 let m = n lsl shift in
4024 if m < 0 || m < n
4025 then raise (Failure "value too large")
4026 else m
4029 let string_with_suffix_of_int n =
4030 if n = 0
4031 then "0"
4032 else
4033 let n, s =
4034 if n land ((1 lsl 30) - 1) = 0
4035 then n lsr 30, "G"
4036 else (
4037 if n land ((1 lsl 20) - 1) = 0
4038 then n lsr 20, "M"
4039 else (
4040 if n land ((1 lsl 10) - 1) = 0
4041 then n lsr 10, "K"
4042 else n, ""
4046 let rec loop s n =
4047 let h = n mod 1000 in
4048 let n = n / 1000 in
4049 if n = 0
4050 then string_of_int h ^ s
4051 else (
4052 let s = Printf.sprintf "_%03d%s" h s in
4053 loop s n
4056 loop "" n ^ s;
4059 let defghyllscroll = (40, 8, 32);;
4060 let ghyllscroll_of_string s =
4061 let (n, a, b) as nab =
4062 if s = "default"
4063 then defghyllscroll
4064 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4066 if n <= a || n <= b || a >= b
4067 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4068 nab;
4071 let ghyllscroll_to_string ((n, a, b) as nab) =
4072 if nab = defghyllscroll
4073 then "default"
4074 else Printf.sprintf "%d,%d,%d" n a b;
4077 let describe_location () =
4078 let fn = page_of_y state.y in
4079 let ln = page_of_y (state.y + state.winh - state.hscrollh - 1) in
4080 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4081 let percent =
4082 if maxy <= 0
4083 then 100.
4084 else (100. *. (float state.y /. float maxy))
4086 if fn = ln
4087 then
4088 Printf.sprintf "page %d of %d [%.2f%%]"
4089 (fn+1) state.pagecount percent
4090 else
4091 Printf.sprintf
4092 "pages %d-%d of %d [%.2f%%]"
4093 (fn+1) (ln+1) state.pagecount percent
4096 let setpresentationmode v =
4097 let n = page_of_y state.y in
4098 state.anchor <- (n, 0.0, 1.0);
4099 conf.presentation <- v;
4100 if conf.presentation
4101 then (
4102 if not conf.scrollbarinpm
4103 then state.scrollw <- 0;
4105 else state.scrollw <- conf.scrollbw;
4106 represent ();
4109 let enterinfomode =
4110 let btos b = if b then "\xe2\x88\x9a" else "" in
4111 let showextended = ref false in
4112 let leave mode = function
4113 | Confirm -> state.mode <- mode
4114 | Cancel -> state.mode <- mode in
4115 let src =
4116 (object
4117 val mutable m_first_time = true
4118 val mutable m_l = []
4119 val mutable m_a = [||]
4120 val mutable m_prev_uioh = nouioh
4121 val mutable m_prev_mode = View
4123 inherit lvsourcebase
4125 method reset prev_mode prev_uioh =
4126 m_a <- Array.of_list (List.rev m_l);
4127 m_l <- [];
4128 m_prev_mode <- prev_mode;
4129 m_prev_uioh <- prev_uioh;
4130 if m_first_time
4131 then (
4132 let rec loop n =
4133 if n >= Array.length m_a
4134 then ()
4135 else
4136 match m_a.(n) with
4137 | _, _, _, Action _ -> m_active <- n
4138 | _ -> loop (n+1)
4140 loop 0;
4141 m_first_time <- false;
4144 method int name get set =
4145 m_l <-
4146 (name, `int get, 1, Action (
4147 fun u ->
4148 let ondone s =
4149 try set (int_of_string s)
4150 with exn ->
4151 state.text <- Printf.sprintf "bad integer `%s': %s"
4152 s (exntos exn)
4154 state.text <- "";
4155 let te = name ^ ": ", "", None, intentry, ondone, true in
4156 state.mode <- Textentry (te, leave m_prev_mode);
4158 )) :: m_l
4160 method int_with_suffix name get set =
4161 m_l <-
4162 (name, `intws get, 1, Action (
4163 fun u ->
4164 let ondone s =
4165 try set (int_of_string_with_suffix s)
4166 with exn ->
4167 state.text <- Printf.sprintf "bad integer `%s': %s"
4168 s (exntos exn)
4170 state.text <- "";
4171 let te =
4172 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4174 state.mode <- Textentry (te, leave m_prev_mode);
4176 )) :: m_l
4178 method bool ?(offset=1) ?(btos=btos) name get set =
4179 m_l <-
4180 (name, `bool (btos, get), offset, Action (
4181 fun u ->
4182 let v = get () in
4183 set (not v);
4185 )) :: m_l
4187 method color name get set =
4188 m_l <-
4189 (name, `color get, 1, Action (
4190 fun u ->
4191 let invalid = (nan, nan, nan) in
4192 let ondone s =
4193 let c =
4194 try color_of_string s
4195 with exn ->
4196 state.text <- Printf.sprintf "bad color `%s': %s"
4197 s (exntos exn);
4198 invalid
4200 if c <> invalid
4201 then set c;
4203 let te = name ^ ": ", "", None, textentry, ondone, true in
4204 state.text <- color_to_string (get ());
4205 state.mode <- Textentry (te, leave m_prev_mode);
4207 )) :: m_l
4209 method string name get set =
4210 m_l <-
4211 (name, `string get, 1, Action (
4212 fun u ->
4213 let ondone s = set s in
4214 let te = name ^ ": ", "", None, textentry, ondone, true in
4215 state.mode <- Textentry (te, leave m_prev_mode);
4217 )) :: m_l
4219 method colorspace name get set =
4220 m_l <-
4221 (name, `string get, 1, Action (
4222 fun _ ->
4223 let source =
4224 let vals = [| "rgb"; "bgr"; "gray" |] in
4225 (object
4226 inherit lvsourcebase
4228 initializer
4229 m_active <- int_of_colorspace conf.colorspace;
4230 m_first <- 0;
4232 method getitemcount = Array.length vals
4233 method getitem n = (vals.(n), 0)
4234 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4235 ignore (uioh, first, pan, qsearch);
4236 if not cancel then set active;
4237 None
4238 method hasaction _ = true
4239 end)
4241 state.text <- "";
4242 let modehash = findkeyhash conf "info" in
4243 coe (new listview ~source ~trusted:true ~modehash)
4244 )) :: m_l
4246 method fitmodel name get set =
4247 m_l <-
4248 (name, `string get, 1, Action (
4249 fun _ ->
4250 let source =
4251 let vals = [| "fit width"; "proportional"; "fit page" |] in
4252 (object
4253 inherit lvsourcebase
4255 initializer
4256 m_active <- int_of_fitmodel conf.fitmodel;
4257 m_first <- 0;
4259 method getitemcount = Array.length vals
4260 method getitem n = (vals.(n), 0)
4261 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4262 ignore (uioh, first, pan, qsearch);
4263 if not cancel then set active;
4264 None
4265 method hasaction _ = true
4266 end)
4268 state.text <- "";
4269 let modehash = findkeyhash conf "info" in
4270 coe (new listview ~source ~trusted:true ~modehash)
4271 )) :: m_l
4273 method caption s offset =
4274 m_l <- (s, `empty, offset, Noaction) :: m_l
4276 method caption2 s f offset =
4277 m_l <- (s, `string f, offset, Noaction) :: m_l
4279 method getitemcount = Array.length m_a
4281 method getitem n =
4282 let tostr = function
4283 | `int f -> string_of_int (f ())
4284 | `intws f -> string_with_suffix_of_int (f ())
4285 | `string f -> f ()
4286 | `color f -> color_to_string (f ())
4287 | `bool (btos, f) -> btos (f ())
4288 | `empty -> ""
4290 let name, t, offset, _ = m_a.(n) in
4291 ((let s = tostr t in
4292 if String.length s > 0
4293 then Printf.sprintf "%s\t%s" name s
4294 else name),
4295 offset)
4297 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4298 let uiohopt =
4299 if not cancel
4300 then (
4301 m_qsearch <- qsearch;
4302 let uioh =
4303 match m_a.(active) with
4304 | _, _, _, Action f -> f uioh
4305 | _ -> uioh
4307 Some uioh
4309 else None
4311 m_active <- active;
4312 m_first <- first;
4313 m_pan <- pan;
4314 uiohopt
4316 method hasaction n =
4317 match m_a.(n) with
4318 | _, _, _, Action _ -> true
4319 | _ -> false
4320 end)
4322 let rec fillsrc prevmode prevuioh =
4323 let sep () = src#caption "" 0 in
4324 let colorp name get set =
4325 src#string name
4326 (fun () -> color_to_string (get ()))
4327 (fun v ->
4329 let c = color_of_string v in
4330 set c
4331 with exn ->
4332 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4335 let oldmode = state.mode in
4336 let birdseye = isbirdseye state.mode in
4338 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4340 src#bool "presentation mode"
4341 (fun () -> conf.presentation)
4342 (fun v -> setpresentationmode v);
4344 src#bool "ignore case in searches"
4345 (fun () -> conf.icase)
4346 (fun v -> conf.icase <- v);
4348 src#bool "preload"
4349 (fun () -> conf.preload)
4350 (fun v -> conf.preload <- v);
4352 src#bool "highlight links"
4353 (fun () -> conf.hlinks)
4354 (fun v -> conf.hlinks <- v);
4356 src#bool "under info"
4357 (fun () -> conf.underinfo)
4358 (fun v -> conf.underinfo <- v);
4360 src#bool "persistent bookmarks"
4361 (fun () -> conf.savebmarks)
4362 (fun v -> conf.savebmarks <- v);
4364 src#fitmodel "fit model"
4365 (fun () -> fitmodel_to_string conf.fitmodel)
4366 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4368 src#bool "trim margins"
4369 (fun () -> conf.trimmargins)
4370 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4372 src#bool "persistent location"
4373 (fun () -> conf.jumpback)
4374 (fun v -> conf.jumpback <- v);
4376 sep ();
4377 src#int "inter-page space"
4378 (fun () -> conf.interpagespace)
4379 (fun n ->
4380 conf.interpagespace <- n;
4381 docolumns conf.columns;
4382 let pageno, py =
4383 match state.layout with
4384 | [] -> 0, 0
4385 | l :: _ ->
4386 l.pageno, l.pagey
4388 state.maxy <- calcheight ();
4389 let y = getpagey pageno in
4390 gotoy (y + py)
4393 src#int "page bias"
4394 (fun () -> conf.pagebias)
4395 (fun v -> conf.pagebias <- v);
4397 src#int "scroll step"
4398 (fun () -> conf.scrollstep)
4399 (fun n -> conf.scrollstep <- n);
4401 src#int "horizontal scroll step"
4402 (fun () -> conf.hscrollstep)
4403 (fun v -> conf.hscrollstep <- v);
4405 src#int "auto scroll step"
4406 (fun () ->
4407 match state.autoscroll with
4408 | Some step -> step
4409 | _ -> conf.autoscrollstep)
4410 (fun n ->
4411 if state.autoscroll <> None
4412 then state.autoscroll <- Some n;
4413 conf.autoscrollstep <- n);
4415 src#int "zoom"
4416 (fun () -> truncate (conf.zoom *. 100.))
4417 (fun v -> setzoom ((float v) /. 100.));
4419 src#int "rotation"
4420 (fun () -> conf.angle)
4421 (fun v -> reqlayout v conf.fitmodel);
4423 src#int "scroll bar width"
4424 (fun () -> state.scrollw)
4425 (fun v ->
4426 state.scrollw <- v;
4427 conf.scrollbw <- v;
4428 reshape state.winw state.winh;
4431 src#int "scroll handle height"
4432 (fun () -> conf.scrollh)
4433 (fun v -> conf.scrollh <- v;);
4435 src#int "thumbnail width"
4436 (fun () -> conf.thumbw)
4437 (fun v ->
4438 conf.thumbw <- min 4096 v;
4439 match oldmode with
4440 | Birdseye beye ->
4441 leavebirdseye beye false;
4442 enterbirdseye ()
4443 | _ -> ()
4446 let mode = state.mode in
4447 src#string "columns"
4448 (fun () ->
4449 match conf.columns with
4450 | Csingle _ -> "1"
4451 | Cmulti (multi, _) -> multicolumns_to_string multi
4452 | Csplit (count, _) -> "-" ^ string_of_int count
4454 (fun v ->
4455 let n, a, b = multicolumns_of_string v in
4456 setcolumns mode n a b);
4458 sep ();
4459 src#caption "Presentation mode" 0;
4460 src#bool "scrollbar visible"
4461 (fun () -> conf.scrollbarinpm)
4462 (fun v ->
4463 if v != conf.scrollbarinpm
4464 then (
4465 conf.scrollbarinpm <- v;
4466 if conf.presentation
4467 then (
4468 state.scrollw <- if v then conf.scrollbw else 0;
4469 reshape state.winw state.winh;
4474 sep ();
4475 src#caption "Pixmap cache" 0;
4476 src#int_with_suffix "size (advisory)"
4477 (fun () -> conf.memlimit)
4478 (fun v -> conf.memlimit <- v);
4480 src#caption2 "used"
4481 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4482 (string_with_suffix_of_int state.memused)
4483 (Hashtbl.length state.tilemap)) 1;
4485 sep ();
4486 src#caption "Layout" 0;
4487 src#caption2 "Dimension"
4488 (fun () ->
4489 Printf.sprintf "%dx%d (virtual %dx%d)"
4490 state.winw state.winh
4491 state.w state.maxy)
4493 if conf.debug
4494 then
4495 src#caption2 "Position" (fun () ->
4496 Printf.sprintf "%dx%d" state.x state.y
4498 else
4499 src#caption2 "Position" (fun () -> describe_location ()) 1
4502 sep ();
4503 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4504 "Save these parameters as global defaults at exit"
4505 (fun () -> conf.bedefault)
4506 (fun v -> conf.bedefault <- v)
4509 sep ();
4510 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4511 src#bool ~offset:0 ~btos "Extended parameters"
4512 (fun () -> !showextended)
4513 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4514 if !showextended
4515 then (
4516 src#bool "checkers"
4517 (fun () -> conf.checkers)
4518 (fun v -> conf.checkers <- v; setcheckers v);
4519 src#bool "update cursor"
4520 (fun () -> conf.updatecurs)
4521 (fun v -> conf.updatecurs <- v);
4522 src#bool "verbose"
4523 (fun () -> conf.verbose)
4524 (fun v -> conf.verbose <- v);
4525 src#bool "invert colors"
4526 (fun () -> conf.invert)
4527 (fun v -> conf.invert <- v);
4528 src#bool "max fit"
4529 (fun () -> conf.maxhfit)
4530 (fun v -> conf.maxhfit <- v);
4531 src#bool "redirect stderr"
4532 (fun () -> conf.redirectstderr)
4533 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4534 src#string "uri launcher"
4535 (fun () -> conf.urilauncher)
4536 (fun v -> conf.urilauncher <- v);
4537 src#string "path launcher"
4538 (fun () -> conf.pathlauncher)
4539 (fun v -> conf.pathlauncher <- v);
4540 src#string "tile size"
4541 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4542 (fun v ->
4544 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4545 conf.tilew <- max 64 w;
4546 conf.tileh <- max 64 h;
4547 flushtiles ();
4548 with exn ->
4549 state.text <- Printf.sprintf "bad tile size `%s': %s"
4550 v (exntos exn)
4552 src#int "texture count"
4553 (fun () -> conf.texcount)
4554 (fun v ->
4555 if realloctexts v
4556 then conf.texcount <- v
4557 else showtext '!' " Failed to set texture count please retry later"
4559 src#int "slice height"
4560 (fun () -> conf.sliceheight)
4561 (fun v ->
4562 conf.sliceheight <- v;
4563 wcmd "sliceh %d" conf.sliceheight;
4565 src#int "anti-aliasing level"
4566 (fun () -> conf.aalevel)
4567 (fun v ->
4568 conf.aalevel <- bound v 0 8;
4569 state.anchor <- getanchor ();
4570 opendoc state.path state.password;
4572 src#string "page scroll scaling factor"
4573 (fun () -> string_of_float conf.pgscale)
4574 (fun v ->
4576 let s = float_of_string v in
4577 conf.pgscale <- s
4578 with exn ->
4579 state.text <- Printf.sprintf
4580 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4583 src#int "ui font size"
4584 (fun () -> fstate.fontsize)
4585 (fun v -> setfontsize (bound v 5 100));
4586 src#int "hint font size"
4587 (fun () -> conf.hfsize)
4588 (fun v -> conf.hfsize <- bound v 5 100);
4589 colorp "background color"
4590 (fun () -> conf.bgcolor)
4591 (fun v -> conf.bgcolor <- v);
4592 src#bool "crop hack"
4593 (fun () -> conf.crophack)
4594 (fun v -> conf.crophack <- v);
4595 src#string "trim fuzz"
4596 (fun () -> irect_to_string conf.trimfuzz)
4597 (fun v ->
4599 conf.trimfuzz <- irect_of_string v;
4600 if conf.trimmargins
4601 then settrim true conf.trimfuzz;
4602 with exn ->
4603 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4605 src#string "throttle"
4606 (fun () ->
4607 match conf.maxwait with
4608 | None -> "show place holder if page is not ready"
4609 | Some time ->
4610 if time = infinity
4611 then "wait for page to fully render"
4612 else
4613 "wait " ^ string_of_float time
4614 ^ " seconds before showing placeholder"
4616 (fun v ->
4618 let f = float_of_string v in
4619 if f <= 0.0
4620 then conf.maxwait <- None
4621 else conf.maxwait <- Some f
4622 with exn ->
4623 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4625 src#string "ghyll scroll"
4626 (fun () ->
4627 match conf.ghyllscroll with
4628 | None -> ""
4629 | Some nab -> ghyllscroll_to_string nab
4631 (fun v ->
4633 let gs =
4634 if String.length v = 0
4635 then None
4636 else Some (ghyllscroll_of_string v)
4638 conf.ghyllscroll <- gs
4639 with exn ->
4640 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4642 src#string "selection command"
4643 (fun () -> conf.selcmd)
4644 (fun v -> conf.selcmd <- v);
4645 src#string "synctex command"
4646 (fun () -> conf.stcmd)
4647 (fun v -> conf.stcmd <- v);
4648 src#colorspace "color space"
4649 (fun () -> colorspace_to_string conf.colorspace)
4650 (fun v ->
4651 conf.colorspace <- colorspace_of_int v;
4652 wcmd "cs %d" v;
4653 load state.layout;
4655 if pbousable ()
4656 then
4657 src#bool "use PBO"
4658 (fun () -> conf.usepbo)
4659 (fun v -> conf.usepbo <- v);
4660 src#bool "mouse wheel scrolls pages"
4661 (fun () -> conf.wheelbypage)
4662 (fun v -> conf.wheelbypage <- v);
4665 sep ();
4666 src#caption "Document" 0;
4667 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4668 src#caption2 "Pages"
4669 (fun () -> string_of_int state.pagecount) 1;
4670 src#caption2 "Dimensions"
4671 (fun () -> string_of_int (List.length state.pdims)) 1;
4672 if conf.trimmargins
4673 then (
4674 sep ();
4675 src#caption "Trimmed margins" 0;
4676 src#caption2 "Dimensions"
4677 (fun () -> string_of_int (List.length state.pdims)) 1;
4680 sep ();
4681 src#caption "OpenGL" 0;
4682 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4683 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4684 src#reset prevmode prevuioh;
4686 fun () ->
4687 state.text <- "";
4688 let prevmode = state.mode
4689 and prevuioh = state.uioh in
4690 fillsrc prevmode prevuioh;
4691 let source = (src :> lvsource) in
4692 let modehash = findkeyhash conf "info" in
4693 state.uioh <- coe (object (self)
4694 inherit listview ~source ~trusted:true ~modehash as super
4695 val mutable m_prevmemused = 0
4696 method infochanged = function
4697 | Memused ->
4698 if m_prevmemused != state.memused
4699 then (
4700 m_prevmemused <- state.memused;
4701 G.postRedisplay "memusedchanged";
4703 | Pdim -> G.postRedisplay "pdimchanged"
4704 | Docinfo -> fillsrc prevmode prevuioh
4706 method key key mask =
4707 if not (Wsi.withctrl mask)
4708 then
4709 match key with
4710 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4711 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4712 | _ -> super#key key mask
4713 else super#key key mask
4714 end);
4715 G.postRedisplay "info";
4718 let enterhelpmode =
4719 let source =
4720 (object
4721 inherit lvsourcebase
4722 method getitemcount = Array.length state.help
4723 method getitem n =
4724 let s, l, _ = state.help.(n) in
4725 (s, l)
4727 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4728 let optuioh =
4729 if not cancel
4730 then (
4731 m_qsearch <- qsearch;
4732 match state.help.(active) with
4733 | _, _, Action f -> Some (f uioh)
4734 | _ -> Some (uioh)
4736 else None
4738 m_active <- active;
4739 m_first <- first;
4740 m_pan <- pan;
4741 optuioh
4743 method hasaction n =
4744 match state.help.(n) with
4745 | _, _, Action _ -> true
4746 | _ -> false
4748 initializer
4749 m_active <- -1
4750 end)
4751 in fun () ->
4752 let modehash = findkeyhash conf "help" in
4753 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4754 G.postRedisplay "help";
4757 let entermsgsmode =
4758 let msgsource =
4759 let re = Str.regexp "[\r\n]" in
4760 (object
4761 inherit lvsourcebase
4762 val mutable m_items = [||]
4764 method getitemcount = 1 + Array.length m_items
4766 method getitem n =
4767 if n = 0
4768 then "[Clear]", 0
4769 else m_items.(n-1), 0
4771 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4772 ignore uioh;
4773 if not cancel
4774 then (
4775 if active = 0
4776 then Buffer.clear state.errmsgs;
4777 m_qsearch <- qsearch;
4779 m_active <- active;
4780 m_first <- first;
4781 m_pan <- pan;
4782 None
4784 method hasaction n =
4785 n = 0
4787 method reset =
4788 state.newerrmsgs <- false;
4789 let l = Str.split re (Buffer.contents state.errmsgs) in
4790 m_items <- Array.of_list l
4792 initializer
4793 m_active <- 0
4794 end)
4795 in fun () ->
4796 state.text <- "";
4797 msgsource#reset;
4798 let source = (msgsource :> lvsource) in
4799 let modehash = findkeyhash conf "listview" in
4800 state.uioh <- coe (object
4801 inherit listview ~source ~trusted:false ~modehash as super
4802 method display =
4803 if state.newerrmsgs
4804 then msgsource#reset;
4805 super#display
4806 end);
4807 G.postRedisplay "msgs";
4810 let quickbookmark ?title () =
4811 match state.layout with
4812 | [] -> ()
4813 | l :: _ ->
4814 let title =
4815 match title with
4816 | None ->
4817 let sec = Unix.gettimeofday () in
4818 let tm = Unix.localtime sec in
4819 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4820 (l.pageno+1)
4821 tm.Unix.tm_mday
4822 tm.Unix.tm_mon
4823 (tm.Unix.tm_year + 1900)
4824 tm.Unix.tm_hour
4825 tm.Unix.tm_min
4826 | Some title -> title
4828 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4831 let setautoscrollspeed step goingdown =
4832 let incr = max 1 ((abs step) / 2) in
4833 let incr = if goingdown then incr else -incr in
4834 let astep = step + incr in
4835 state.autoscroll <- Some astep;
4838 let gotounder = function
4839 | Ulinkgoto (pageno, top) ->
4840 if pageno >= 0
4841 then (
4842 addnav ();
4843 gotopage1 pageno top;
4846 | Ulinkuri s ->
4847 gotouri s
4849 | Uremote (filename, pageno) ->
4850 let path =
4851 if Sys.file_exists filename
4852 then filename
4853 else
4854 let dir = Filename.dirname state.path in
4855 let path = Filename.concat dir filename in
4856 if Sys.file_exists path
4857 then path
4858 else ""
4860 if String.length path > 0
4861 then (
4862 let anchor = getanchor () in
4863 let ranchor = state.path, state.password, anchor in
4864 state.anchor <- (pageno, 0.0, 0.0);
4865 state.ranchors <- ranchor :: state.ranchors;
4866 opendoc path "";
4868 else showtext '!' ("Could not find " ^ filename)
4870 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4873 let canpan () =
4874 match conf.columns with
4875 | Csplit _ -> true
4876 | _ -> state.x != 0 || conf.zoom > 1.0
4879 let existsinrow pageno (columns, coverA, coverB) p =
4880 let last = ((pageno - coverA) mod columns) + columns in
4881 let rec any = function
4882 | [] -> false
4883 | l :: rest ->
4884 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4885 then p l
4886 else (
4887 if not (p l)
4888 then (if l.pageno = last then false else any rest)
4889 else true
4892 any state.layout
4895 let nextpage () =
4896 match state.layout with
4897 | [] ->
4898 let pageno = page_of_y state.y in
4899 gotoghyll (getpagey (pageno+1))
4900 | l :: rest ->
4901 match conf.columns with
4902 | Csingle _ ->
4903 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4904 then
4905 let y = clamp (pgscale state.winh) in
4906 gotoghyll y
4907 else
4908 let pageno = min (l.pageno+1) (state.pagecount-1) in
4909 gotoghyll (getpagey pageno)
4910 | Cmulti ((c, _, _) as cl, _) ->
4911 if conf.presentation
4912 && (existsinrow l.pageno cl
4913 (fun l -> l.pageh > l.pagey + l.pagevh))
4914 then
4915 let y = clamp (pgscale state.winh) in
4916 gotoghyll y
4917 else
4918 let pageno = min (l.pageno+c) (state.pagecount-1) in
4919 gotoghyll (getpagey pageno)
4920 | Csplit (n, _) ->
4921 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4922 then
4923 let pagey, pageh = getpageyh l.pageno in
4924 let pagey = pagey + pageh * l.pagecol in
4925 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4926 gotoghyll (pagey + pageh + ips)
4929 let prevpage () =
4930 match state.layout with
4931 | [] ->
4932 let pageno = page_of_y state.y in
4933 gotoghyll (getpagey (pageno-1))
4934 | l :: _ ->
4935 match conf.columns with
4936 | Csingle _ ->
4937 if conf.presentation && l.pagey != 0
4938 then
4939 gotoghyll (clamp (pgscale ~-(state.winh)))
4940 else
4941 let pageno = max 0 (l.pageno-1) in
4942 gotoghyll (getpagey pageno)
4943 | Cmulti ((c, _, coverB) as cl, _) ->
4944 if conf.presentation &&
4945 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4946 then
4947 gotoghyll (clamp (pgscale ~-(state.winh)))
4948 else
4949 let decr =
4950 if l.pageno = state.pagecount - coverB
4951 then 1
4952 else c
4954 let pageno = max 0 (l.pageno-decr) in
4955 gotoghyll (getpagey pageno)
4956 | Csplit (n, _) ->
4957 let y =
4958 if l.pagecol = 0
4959 then
4960 if l.pageno = 0
4961 then l.pagey
4962 else
4963 let pageno = max 0 (l.pageno-1) in
4964 let pagey, pageh = getpageyh pageno in
4965 pagey + (n-1)*pageh
4966 else
4967 let pagey, pageh = getpageyh l.pageno in
4968 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4970 gotoghyll y
4973 let viewkeyboard key mask =
4974 let enttext te =
4975 let mode = state.mode in
4976 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4977 state.text <- "";
4978 enttext ();
4979 G.postRedisplay "view:enttext"
4981 let ctrl = Wsi.withctrl mask in
4982 let key =
4983 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4985 match key with
4986 | 81 -> (* Q *)
4987 exit 0
4989 | 0xff63 -> (* insert *)
4990 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4991 then (
4992 state.mode <- LinkNav (Ltgendir 0);
4993 gotoy state.y;
4995 else showtext '!' "Keyboard link navigation does not work under rotation"
4997 | 0xff1b | 113 -> (* escape / q *)
4998 begin match state.mstate with
4999 | Mzoomrect _ ->
5000 state.mstate <- Mnone;
5001 Wsi.setcursor Wsi.CURSOR_INHERIT;
5002 G.postRedisplay "kill zoom rect";
5003 | _ ->
5004 begin match state.mode with
5005 | LinkNav _ ->
5006 state.mode <- View;
5007 G.postRedisplay "esc leave linknav"
5008 | _ ->
5009 match state.ranchors with
5010 | [] -> raise Quit
5011 | (path, password, anchor) :: rest ->
5012 state.ranchors <- rest;
5013 state.anchor <- anchor;
5014 opendoc path password
5015 end;
5016 end;
5018 | 0xff08 -> (* backspace *)
5019 gotoghyll (getnav ~-1)
5021 | 111 -> (* o *)
5022 enteroutlinemode ()
5024 | 117 -> (* u *)
5025 state.rects <- [];
5026 state.text <- "";
5027 G.postRedisplay "dehighlight";
5029 | 47 | 63 -> (* / ? *)
5030 let ondone isforw s =
5031 cbput state.hists.pat s;
5032 state.searchpattern <- s;
5033 search s isforw
5035 let s = String.create 1 in
5036 s.[0] <- Char.chr key;
5037 enttext (s, "", Some (onhist state.hists.pat),
5038 textentry, ondone (key = 47), true)
5040 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5041 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5042 setzoom (conf.zoom +. incr)
5044 | 43 | 0xffab -> (* + *)
5045 let ondone s =
5046 let n =
5047 try int_of_string s with exc ->
5048 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5049 max_int
5051 if n != max_int
5052 then (
5053 conf.pagebias <- n;
5054 state.text <- "page bias is now " ^ string_of_int n;
5057 enttext ("page bias: ", "", None, intentry, ondone, true)
5059 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5060 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5061 setzoom (max 0.01 (conf.zoom -. decr))
5063 | 45 | 0xffad -> (* - *)
5064 let ondone msg = state.text <- msg in
5065 enttext (
5066 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5067 optentry state.mode, ondone, true
5070 | 48 when ctrl -> (* ctrl-0 *)
5071 if conf.zoom = 1.0
5072 then (
5073 state.x <- 0;
5074 state.hscrollh <-
5075 if state.w <= state.winw - state.scrollw
5076 then 0
5077 else state.scrollw
5079 gotoy state.y
5081 else setzoom 1.0
5083 | (49 | 50) when ctrl -> (* ctrl-1/2 *)
5084 let cols =
5085 match conf.columns with
5086 | Csingle _ | Cmulti _ -> 1
5087 | Csplit (n, _) -> n
5089 let h = state.winh -
5090 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5092 let zoom = zoomforh state.winw h state.scrollw cols in
5093 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5094 then setzoom zoom
5096 | 51 when ctrl -> (* ctrl-3 *)
5097 let fm =
5098 match conf.fitmodel with
5099 | FitWidth -> FitProportional
5100 | FitProportional -> FitPage
5101 | FitPage -> FitWidth
5103 state.text <- "fit model: " ^ fitmodel_to_string fm;
5104 reqlayout conf.angle fm
5106 | 0xffc6 -> (* f9 *)
5107 togglebirdseye ()
5109 | 57 when ctrl -> (* ctrl-9 *)
5110 togglebirdseye ()
5112 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5113 when not ctrl -> (* 0..9 *)
5114 let ondone s =
5115 let n =
5116 try int_of_string s with exc ->
5117 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5120 if n >= 0
5121 then (
5122 addnav ();
5123 cbput state.hists.pag (string_of_int n);
5124 gotopage1 (n + conf.pagebias - 1) 0;
5127 let pageentry text key =
5128 match Char.unsafe_chr key with
5129 | 'g' -> TEdone text
5130 | _ -> intentry text key
5132 let text = "x" in text.[0] <- Char.chr key;
5133 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5135 | 98 -> (* b *)
5136 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5137 reshape state.winw state.winh;
5139 | 108 -> (* l *)
5140 conf.hlinks <- not conf.hlinks;
5141 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5142 G.postRedisplay "toggle highlightlinks";
5144 | 70 -> (* F *)
5145 state.glinks <- true;
5146 let mode = state.mode in
5147 state.mode <- Textentry (
5148 (":", "", None, linknentry, linkndone gotounder, false),
5149 (fun _ ->
5150 state.glinks <- false;
5151 state.mode <- mode)
5153 state.text <- "";
5154 G.postRedisplay "view:linkent(F)"
5156 | 121 -> (* y *)
5157 state.glinks <- true;
5158 let mode = state.mode in
5159 state.mode <- Textentry (
5160 (":", "", None, linknentry, linkndone (fun under ->
5161 match Ne.pipe () with
5162 | Ne.Exn exn ->
5163 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
5164 | Ne.Res (r, w) ->
5165 let popened =
5166 try popen conf.selcmd [r, 0; w, -1]; true
5167 with exn ->
5168 showtext '!'
5169 (Printf.sprintf "failed to execute %s: %s"
5170 conf.selcmd (exntos exn));
5171 false
5173 let clo cap fd =
5174 Ne.clo fd (fun msg ->
5175 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5178 let s = undertext under in
5179 if popened
5180 then
5181 (try
5182 let l = String.length s in
5183 let n = tempfailureretry (Unix.write w s 0) l in
5184 if n != l
5185 then
5186 showtext '!'
5187 (Printf.sprintf
5188 "failed to write %d characters to sel pipe, wrote %d"
5191 with exn ->
5192 showtext '!'
5193 (Printf.sprintf "failed to write to sel pipe: %s"
5194 (exntos exn)
5197 else dolog "%s" s;
5198 clo "pipe/r" r;
5199 clo "pipe/w" w;
5200 ), false
5202 fun _ ->
5203 state.glinks <- false;
5204 state.mode <- mode
5206 state.text <- "";
5207 G.postRedisplay "view:linkent"
5209 | 97 -> (* a *)
5210 begin match state.autoscroll with
5211 | Some step ->
5212 conf.autoscrollstep <- step;
5213 state.autoscroll <- None
5214 | None ->
5215 if conf.autoscrollstep = 0
5216 then state.autoscroll <- Some 1
5217 else state.autoscroll <- Some conf.autoscrollstep
5220 | 112 when ctrl -> (* ctrl-p *)
5221 launchpath ()
5223 | 80 -> (* P *)
5224 setpresentationmode (not conf.presentation);
5225 showtext ' ' ("presentation mode " ^
5226 if conf.presentation then "on" else "off");
5228 | 102 -> (* f *)
5229 if List.mem Wsi.Fullscreen state.winstate
5230 then Wsi.reshape conf.cwinw conf.cwinh
5231 else Wsi.fullscreen ()
5233 | 112 | 78 -> (* p|N *)
5234 search state.searchpattern false
5236 | 110 | 0xffc0 -> (* n|F3 *)
5237 search state.searchpattern true
5239 | 116 -> (* t *)
5240 begin match state.layout with
5241 | [] -> ()
5242 | l :: _ ->
5243 gotoghyll (getpagey l.pageno)
5246 | 32 -> (* space *)
5247 nextpage ()
5249 | 0xff9f | 0xffff -> (* delete *)
5250 prevpage ()
5252 | 61 -> (* = *)
5253 showtext ' ' (describe_location ());
5255 | 119 -> (* w *)
5256 begin match state.layout with
5257 | [] -> ()
5258 | l :: _ ->
5259 Wsi.reshape (l.pagew + state.scrollw) l.pageh;
5260 G.postRedisplay "w"
5263 | 39 -> (* ' *)
5264 enterbookmarkmode ()
5266 | 104 | 0xffbe -> (* h|F1 *)
5267 enterhelpmode ()
5269 | 105 -> (* i *)
5270 enterinfomode ()
5272 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5273 entermsgsmode ()
5275 | 109 -> (* m *)
5276 let ondone s =
5277 match state.layout with
5278 | l :: _ ->
5279 if String.length s > 0
5280 then
5281 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5282 | _ -> ()
5284 enttext ("bookmark: ", "", None, textentry, ondone, true)
5286 | 126 -> (* ~ *)
5287 quickbookmark ();
5288 showtext ' ' "Quick bookmark added";
5290 | 122 -> (* z *)
5291 begin match state.layout with
5292 | l :: _ ->
5293 let rect = getpdimrect l.pagedimno in
5294 let w, h =
5295 if conf.crophack
5296 then
5297 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5298 truncate (1.2 *. (rect.(3) -. rect.(0))))
5299 else
5300 (truncate (rect.(1) -. rect.(0)),
5301 truncate (rect.(3) -. rect.(0)))
5303 let w = truncate ((float w)*.conf.zoom)
5304 and h = truncate ((float h)*.conf.zoom) in
5305 if w != 0 && h != 0
5306 then (
5307 state.anchor <- getanchor ();
5308 Wsi.reshape (w + state.scrollw) (h + conf.interpagespace)
5310 G.postRedisplay "z";
5312 | [] -> ()
5315 | 60 | 62 -> (* < > *)
5316 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5318 | 91 | 93 -> (* [ ] *)
5319 conf.colorscale <-
5320 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5322 G.postRedisplay "brightness";
5324 | 99 when state.mode = View -> (* c *)
5325 let (c, a, b), z =
5326 match state.prevcolumns with
5327 | None -> (1, 0, 0), 1.0
5328 | Some (columns, z) ->
5329 let cab =
5330 match columns with
5331 | Csplit (c, _) -> -c, 0, 0
5332 | Cmulti ((c, a, b), _) -> c, a, b
5333 | Csingle _ -> 1, 0, 0
5335 cab, z
5337 setcolumns View c a b;
5338 setzoom z;
5340 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5341 setzoom state.prevzoom
5343 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5344 begin match state.autoscroll with
5345 | None ->
5346 begin match state.mode with
5347 | Birdseye beye -> upbirdseye 1 beye
5348 | _ ->
5349 if ctrl
5350 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5351 else (
5352 if not (Wsi.withshift mask) && conf.presentation
5353 then prevpage ()
5354 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5357 | Some n ->
5358 setautoscrollspeed n false
5361 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5362 begin match state.autoscroll with
5363 | None ->
5364 begin match state.mode with
5365 | Birdseye beye -> downbirdseye 1 beye
5366 | _ ->
5367 if ctrl
5368 then gotoy_and_clear_text (clamp (state.winh/2))
5369 else (
5370 if not (Wsi.withshift mask) && conf.presentation
5371 then nextpage ()
5372 else gotoy_and_clear_text (clamp conf.scrollstep)
5375 | Some n ->
5376 setautoscrollspeed n true
5379 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5380 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5381 if canpan ()
5382 then
5383 let dx =
5384 if ctrl
5385 then state.winw / 2
5386 else conf.hscrollstep
5388 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5389 state.x <- state.x + dx;
5390 gotoy_and_clear_text state.y
5391 else (
5392 state.text <- "";
5393 G.postRedisplay "lef/right"
5396 | 0xff55 | 0xff9a -> (* (kp) prior *)
5397 let y =
5398 if ctrl
5399 then
5400 match state.layout with
5401 | [] -> state.y
5402 | l :: _ -> state.y - l.pagey
5403 else
5404 clamp (pgscale (-state.winh))
5406 gotoghyll y
5408 | 0xff56 | 0xff9b -> (* (kp) next *)
5409 let y =
5410 if ctrl
5411 then
5412 match List.rev state.layout with
5413 | [] -> state.y
5414 | l :: _ -> getpagey l.pageno
5415 else
5416 clamp (pgscale state.winh)
5418 gotoghyll y
5420 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5421 gotoghyll 0
5422 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5423 gotoghyll (clamp state.maxy)
5425 | 0xff53 | 0xff98
5426 when Wsi.withalt mask -> (* alt-(kp) right *)
5427 gotoghyll (getnav 1)
5428 | 0xff51 | 0xff96
5429 when Wsi.withalt mask -> (* alt-(kp) left *)
5430 gotoghyll (getnav ~-1)
5432 | 114 -> (* r *)
5433 reload ()
5435 | 118 when conf.debug -> (* v *)
5436 state.rects <- [];
5437 List.iter (fun l ->
5438 match getopaque l.pageno with
5439 | None -> ()
5440 | Some opaque ->
5441 let x0, y0, x1, y1 = pagebbox opaque in
5442 let a,b = float x0, float y0 in
5443 let c,d = float x1, float y0 in
5444 let e,f = float x1, float y1 in
5445 let h,j = float x0, float y1 in
5446 let rect = (a,b,c,d,e,f,h,j) in
5447 debugrect rect;
5448 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5449 ) state.layout;
5450 G.postRedisplay "v";
5452 | _ ->
5453 vlog "huh? %s" (Wsi.keyname key)
5456 let linknavkeyboard key mask linknav =
5457 let getpage pageno =
5458 let rec loop = function
5459 | [] -> None
5460 | l :: _ when l.pageno = pageno -> Some l
5461 | _ :: rest -> loop rest
5462 in loop state.layout
5464 let doexact (pageno, n) =
5465 match getopaque pageno, getpage pageno with
5466 | Some opaque, Some l ->
5467 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5468 then
5469 let under = getlink opaque n in
5470 G.postRedisplay "link gotounder";
5471 gotounder under;
5472 state.mode <- View;
5473 else
5474 let opt, dir =
5475 match key with
5476 | 0xff50 -> (* home *)
5477 Some (findlink opaque LDfirst), -1
5479 | 0xff57 -> (* end *)
5480 Some (findlink opaque LDlast), 1
5482 | 0xff51 -> (* left *)
5483 Some (findlink opaque (LDleft n)), -1
5485 | 0xff53 -> (* right *)
5486 Some (findlink opaque (LDright n)), 1
5488 | 0xff52 -> (* up *)
5489 Some (findlink opaque (LDup n)), -1
5491 | 0xff54 -> (* down *)
5492 Some (findlink opaque (LDdown n)), 1
5494 | _ -> None, 0
5496 let pwl l dir =
5497 begin match findpwl l.pageno dir with
5498 | Pwlnotfound -> ()
5499 | Pwl pageno ->
5500 let notfound dir =
5501 state.mode <- LinkNav (Ltgendir dir);
5502 let y, h = getpageyh pageno in
5503 let y =
5504 if dir < 0
5505 then y + h - state.winh
5506 else y
5508 gotoy y
5510 begin match getopaque pageno, getpage pageno with
5511 | Some opaque, Some _ ->
5512 let link =
5513 let ld = if dir > 0 then LDfirst else LDlast in
5514 findlink opaque ld
5516 begin match link with
5517 | Lfound m ->
5518 showlinktype (getlink opaque m);
5519 state.mode <- LinkNav (Ltexact (pageno, m));
5520 G.postRedisplay "linknav jpage";
5521 | _ -> notfound dir
5522 end;
5523 | _ -> notfound dir
5524 end;
5525 end;
5527 begin match opt with
5528 | Some Lnotfound -> pwl l dir;
5529 | Some (Lfound m) ->
5530 if m = n
5531 then pwl l dir
5532 else (
5533 let _, y0, _, y1 = getlinkrect opaque m in
5534 if y0 < l.pagey
5535 then gotopage1 l.pageno y0
5536 else (
5537 let d = fstate.fontsize + 1 in
5538 if y1 - l.pagey > l.pagevh - d
5539 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5540 else G.postRedisplay "linknav";
5542 showlinktype (getlink opaque m);
5543 state.mode <- LinkNav (Ltexact (l.pageno, m));
5546 | None -> viewkeyboard key mask
5547 end;
5548 | _ -> viewkeyboard key mask
5550 if key = 0xff63
5551 then (
5552 state.mode <- View;
5553 G.postRedisplay "leave linknav"
5555 else
5556 match linknav with
5557 | Ltgendir _ -> viewkeyboard key mask
5558 | Ltexact exact -> doexact exact
5561 let keyboard key mask =
5562 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5563 then wcmd "interrupt"
5564 else state.uioh <- state.uioh#key key mask
5567 let birdseyekeyboard key mask
5568 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5569 let incr =
5570 match conf.columns with
5571 | Csingle _ -> 1
5572 | Cmulti ((c, _, _), _) -> c
5573 | Csplit _ -> failwith "bird's eye split mode"
5575 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5576 match key with
5577 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5578 let y, h = getpageyh pageno in
5579 let top = (state.winh - h) / 2 in
5580 gotoy (max 0 (y - top))
5581 | 0xff0d (* enter *)
5582 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5583 | 0xff1b -> leavebirdseye beye true (* escape *)
5584 | 0xff52 -> upbirdseye incr beye (* up *)
5585 | 0xff54 -> downbirdseye incr beye (* down *)
5586 | 0xff51 -> upbirdseye 1 beye (* left *)
5587 | 0xff53 -> downbirdseye 1 beye (* right *)
5589 | 0xff55 -> (* prior *)
5590 begin match state.layout with
5591 | l :: _ ->
5592 if l.pagey != 0
5593 then (
5594 state.mode <- Birdseye (
5595 oconf, leftx, l.pageno, hooverpageno, anchor
5597 gotopage1 l.pageno 0;
5599 else (
5600 let layout = layout (state.y-state.winh) (pgh state.layout) in
5601 match layout with
5602 | [] -> gotoy (clamp (-state.winh))
5603 | l :: _ ->
5604 state.mode <- Birdseye (
5605 oconf, leftx, l.pageno, hooverpageno, anchor
5607 gotopage1 l.pageno 0
5610 | [] -> gotoy (clamp (-state.winh))
5611 end;
5613 | 0xff56 -> (* next *)
5614 begin match List.rev state.layout with
5615 | l :: _ ->
5616 let layout = layout (state.y + (pgh state.layout)) state.winh in
5617 begin match layout with
5618 | [] ->
5619 let incr = l.pageh - l.pagevh in
5620 if incr = 0
5621 then (
5622 state.mode <-
5623 Birdseye (
5624 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5626 G.postRedisplay "birdseye pagedown";
5628 else gotoy (clamp (incr + conf.interpagespace*2));
5630 | l :: _ ->
5631 state.mode <-
5632 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5633 gotopage1 l.pageno 0;
5636 | [] -> gotoy (clamp state.winh)
5637 end;
5639 | 0xff50 -> (* home *)
5640 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5641 gotopage1 0 0
5643 | 0xff57 -> (* end *)
5644 let pageno = state.pagecount - 1 in
5645 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5646 if not (pagevisible state.layout pageno)
5647 then
5648 let h =
5649 match List.rev state.pdims with
5650 | [] -> state.winh
5651 | (_, _, h, _) :: _ -> h
5653 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5654 else G.postRedisplay "birdseye end";
5655 | _ -> viewkeyboard key mask
5658 let drawpage l linkindexbase =
5659 let color =
5660 match state.mode with
5661 | Textentry _ -> scalecolor 0.4
5662 | LinkNav _
5663 | View -> scalecolor 1.0
5664 | Birdseye (_, _, pageno, hooverpageno, _) ->
5665 if l.pageno = hooverpageno
5666 then scalecolor 0.9
5667 else (
5668 if l.pageno = pageno
5669 then scalecolor 1.0
5670 else scalecolor 0.8
5673 drawtiles l color;
5674 begin match getopaque l.pageno with
5675 | Some opaque ->
5676 if tileready l l.pagex l.pagey
5677 then
5678 let x = l.pagedispx - l.pagex
5679 and y = l.pagedispy - l.pagey in
5680 let hlmask =
5681 match conf.columns with
5682 | Csingle _ | Cmulti _ ->
5683 (if conf.hlinks then 1 else 0)
5684 + (if state.glinks
5685 && not (isbirdseye state.mode) then 2 else 0)
5686 | _ -> 0
5688 let s =
5689 match state.mode with
5690 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5691 | _ -> ""
5693 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5694 else 0
5696 | _ -> 0
5697 end;
5700 let scrollindicator () =
5701 let sbw, ph, sh = state.uioh#scrollph in
5702 let sbh, pw, sw = state.uioh#scrollpw in
5704 GlDraw.color (0.64, 0.64, 0.64);
5705 GlDraw.rect
5706 (float (state.winw - sbw), 0.)
5707 (float state.winw, float state.winh)
5709 GlDraw.rect
5710 (0., float (state.winh - sbh))
5711 (float (state.winw - state.scrollw - 1), float state.winh)
5713 GlDraw.color (0.0, 0.0, 0.0);
5715 GlDraw.rect
5716 (float (state.winw - sbw), ph)
5717 (float state.winw, ph +. sh)
5719 GlDraw.rect
5720 (pw, float (state.winh - sbh))
5721 (pw +. sw, float state.winh)
5725 let showsel () =
5726 match state.mstate with
5727 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5730 | Msel ((x0, y0), (x1, y1)) ->
5731 let rec loop = function
5732 | l :: ls ->
5733 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5734 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5735 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5736 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5737 then
5738 match getopaque l.pageno with
5739 | Some opaque ->
5740 let x0, y0 = pagetranslatepoint l x0 y0 in
5741 let x1, y1 = pagetranslatepoint l x1 y1 in
5742 seltext opaque (x0, y0, x1, y1);
5743 | _ -> ()
5744 else loop ls
5745 | [] -> ()
5747 loop state.layout
5750 let showrects rects =
5751 Gl.enable `blend;
5752 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5753 GlDraw.polygon_mode `both `fill;
5754 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5755 List.iter
5756 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5757 List.iter (fun l ->
5758 if l.pageno = pageno
5759 then (
5760 let dx = float (l.pagedispx - l.pagex) in
5761 let dy = float (l.pagedispy - l.pagey) in
5762 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5763 GlDraw.begins `quads;
5765 GlDraw.vertex2 (x0+.dx, y0+.dy);
5766 GlDraw.vertex2 (x1+.dx, y1+.dy);
5767 GlDraw.vertex2 (x2+.dx, y2+.dy);
5768 GlDraw.vertex2 (x3+.dx, y3+.dy);
5770 GlDraw.ends ();
5772 ) state.layout
5773 ) rects
5775 Gl.disable `blend;
5778 let display () =
5779 GlClear.color (scalecolor2 conf.bgcolor);
5780 GlClear.clear [`color];
5781 let rec loop linkindexbase = function
5782 | l :: rest ->
5783 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5784 loop linkindexbase rest
5785 | [] -> ()
5787 loop 0 state.layout;
5788 let rects =
5789 match state.mode with
5790 | LinkNav (Ltexact (pageno, linkno)) ->
5791 begin match getopaque pageno with
5792 | Some opaque ->
5793 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5794 (pageno, 5, (
5795 float x0, float y0,
5796 float x1, float y0,
5797 float x1, float y1,
5798 float x0, float y1)
5799 ) :: state.rects
5800 | None -> state.rects
5802 | _ -> state.rects
5804 showrects rects;
5805 showsel ();
5806 state.uioh#display;
5807 begin match state.mstate with
5808 | Mzoomrect ((x0, y0), (x1, y1)) ->
5809 Gl.enable `blend;
5810 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5811 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5812 GlDraw.rect (float x0, float y0)
5813 (float x1, float y1);
5814 Gl.disable `blend;
5815 | _ -> ()
5816 end;
5817 enttext ();
5818 scrollindicator ();
5819 Wsi.swapb ();
5822 let zoomrect x y x1 y1 =
5823 let x0 = min x x1
5824 and x1 = max x x1
5825 and y0 = min y y1 in
5826 gotoy (state.y + y0);
5827 state.anchor <- getanchor ();
5828 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5829 let margin =
5830 if state.w < state.winw - state.scrollw
5831 then (state.winw - state.scrollw - state.w) / 2
5832 else 0
5834 state.x <- (state.x + margin) - x0;
5835 setzoom zoom;
5836 Wsi.setcursor Wsi.CURSOR_INHERIT;
5837 state.mstate <- Mnone;
5840 let scrollx x =
5841 let winw = state.winw - state.scrollw - 1 in
5842 let s = float x /. float winw in
5843 let destx = truncate (float (state.w + winw) *. s) in
5844 state.x <- winw - destx;
5845 gotoy_and_clear_text state.y;
5846 state.mstate <- Mscrollx;
5849 let scrolly y =
5850 let s = float y /. float state.winh in
5851 let desty = truncate (float (state.maxy - state.winh) *. s) in
5852 gotoy_and_clear_text desty;
5853 state.mstate <- Mscrolly;
5856 let viewmouse button down x y mask =
5857 match button with
5858 | n when (n == 4 || n == 5) && not down ->
5859 if Wsi.withctrl mask
5860 then (
5861 match state.mstate with
5862 | Mzoom (oldn, i) ->
5863 if oldn = n
5864 then (
5865 if i = 2
5866 then
5867 let incr =
5868 match n with
5869 | 5 ->
5870 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5871 | _ ->
5872 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5874 let zoom = conf.zoom -. incr in
5875 setzoom zoom;
5876 state.mstate <- Mzoom (n, 0);
5877 else
5878 state.mstate <- Mzoom (n, i+1);
5880 else state.mstate <- Mzoom (n, 0)
5882 | _ -> state.mstate <- Mzoom (n, 0)
5884 else (
5885 match state.autoscroll with
5886 | Some step -> setautoscrollspeed step (n=4)
5887 | None ->
5888 if conf.wheelbypage || conf.presentation
5889 then (
5890 if n = 4
5891 then prevpage ()
5892 else nextpage ()
5894 else
5895 let incr =
5896 if n = 4
5897 then -conf.scrollstep
5898 else conf.scrollstep
5900 let incr = incr * 2 in
5901 let y = clamp incr in
5902 gotoy_and_clear_text y
5905 | n when (n = 6 || n = 7) && not down && canpan () ->
5906 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5907 gotoy_and_clear_text state.y
5909 | 1 when Wsi.withshift mask ->
5910 state.mstate <- Mnone;
5911 if not down
5912 then (
5913 match unproject x y with
5914 | Some (pageno, ux, uy) ->
5915 let cmd = Printf.sprintf
5916 "%s %s %d %d %d"
5917 conf.stcmd state.path pageno ux uy
5919 popen cmd []
5920 | None -> ()
5923 | 1 when Wsi.withctrl mask ->
5924 if down
5925 then (
5926 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5927 state.mstate <- Mpan (x, y)
5929 else
5930 state.mstate <- Mnone
5932 | 3 ->
5933 if down
5934 then (
5935 Wsi.setcursor Wsi.CURSOR_CYCLE;
5936 let p = (x, y) in
5937 state.mstate <- Mzoomrect (p, p)
5939 else (
5940 match state.mstate with
5941 | Mzoomrect ((x0, y0), _) ->
5942 if abs (x-x0) > 10 && abs (y - y0) > 10
5943 then zoomrect x0 y0 x y
5944 else (
5945 state.mstate <- Mnone;
5946 Wsi.setcursor Wsi.CURSOR_INHERIT;
5947 G.postRedisplay "kill accidental zoom rect";
5949 | _ ->
5950 Wsi.setcursor Wsi.CURSOR_INHERIT;
5951 state.mstate <- Mnone
5954 | 1 when x > state.winw - state.scrollw ->
5955 if down
5956 then
5957 let _, position, sh = state.uioh#scrollph in
5958 if y > truncate position && y < truncate (position +. sh)
5959 then state.mstate <- Mscrolly
5960 else scrolly y
5961 else
5962 state.mstate <- Mnone
5964 | 1 when y > state.winh - state.hscrollh ->
5965 if down
5966 then
5967 let _, position, sw = state.uioh#scrollpw in
5968 if x > truncate position && x < truncate (position +. sw)
5969 then state.mstate <- Mscrollx
5970 else scrollx x
5971 else
5972 state.mstate <- Mnone
5974 | 1 ->
5975 let dest = if down then getunder x y else Unone in
5976 begin match dest with
5977 | Ulinkgoto _
5978 | Ulinkuri _
5979 | Uremote _
5980 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5981 gotounder dest
5983 | Unone when down ->
5984 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5985 state.mstate <- Mpan (x, y);
5987 | Unone | Utext _ ->
5988 if down
5989 then (
5990 if conf.angle mod 360 = 0
5991 then (
5992 state.mstate <- Msel ((x, y), (x, y));
5993 G.postRedisplay "mouse select";
5996 else (
5997 match state.mstate with
5998 | Mnone -> ()
6000 | Mzoom _ | Mscrollx | Mscrolly ->
6001 state.mstate <- Mnone
6003 | Mzoomrect ((x0, y0), _) ->
6004 zoomrect x0 y0 x y
6006 | Mpan _ ->
6007 Wsi.setcursor Wsi.CURSOR_INHERIT;
6008 state.mstate <- Mnone
6010 | Msel ((x0, y0), (x1, y1)) ->
6011 let rec loop = function
6012 | [] -> ()
6013 | l :: rest ->
6014 let inside =
6015 let a0 = l.pagedispy in
6016 let a1 = a0 + l.pagevh in
6017 let b0 = l.pagedispx in
6018 let b1 = b0 + l.pagevw in
6019 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6020 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6022 if inside
6023 then
6024 match getopaque l.pageno with
6025 | Some opaque ->
6026 begin
6027 match Ne.pipe () with
6028 | Ne.Exn exn ->
6029 showtext '!'
6030 (Printf.sprintf
6031 "can not create sel pipe: %s"
6032 (exntos exn));
6033 | Ne.Res (r, w) ->
6034 let doclose what fd =
6035 Ne.clo fd (fun msg ->
6036 dolog "%s close failed: %s" what msg)
6039 popen conf.selcmd [r, 0; w, -1];
6040 copysel w opaque;
6041 doclose "pipe/r" r;
6042 G.postRedisplay "copysel";
6043 with exn ->
6044 dolog "can not execute %S: %s"
6045 conf.selcmd (exntos exn);
6046 doclose "pipe/r" r;
6047 doclose "pipe/w" w;
6049 | None -> ()
6050 else loop rest
6052 loop state.layout;
6053 Wsi.setcursor Wsi.CURSOR_INHERIT;
6054 state.mstate <- Mnone;
6058 | _ -> ()
6061 let birdseyemouse button down x y mask
6062 (conf, leftx, _, hooverpageno, anchor) =
6063 match button with
6064 | 1 when down ->
6065 let rec loop = function
6066 | [] -> ()
6067 | l :: rest ->
6068 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6069 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6070 then (
6071 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6073 else loop rest
6075 loop state.layout
6076 | 3 -> ()
6077 | _ -> viewmouse button down x y mask
6080 let mouse button down x y mask =
6081 state.uioh <- state.uioh#button button down x y mask;
6084 let motion ~x ~y =
6085 state.uioh <- state.uioh#motion x y
6088 let pmotion ~x ~y =
6089 state.uioh <- state.uioh#pmotion x y;
6092 let uioh = object
6093 method display = ()
6095 method key key mask =
6096 begin match state.mode with
6097 | Textentry textentry -> textentrykeyboard key mask textentry
6098 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6099 | View -> viewkeyboard key mask
6100 | LinkNav linknav -> linknavkeyboard key mask linknav
6101 end;
6102 state.uioh
6104 method button button bstate x y mask =
6105 begin match state.mode with
6106 | LinkNav _
6107 | View -> viewmouse button bstate x y mask
6108 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6109 | Textentry _ -> ()
6110 end;
6111 state.uioh
6113 method motion x y =
6114 begin match state.mode with
6115 | Textentry _ -> ()
6116 | View | Birdseye _ | LinkNav _ ->
6117 match state.mstate with
6118 | Mzoom _ | Mnone -> ()
6120 | Mpan (x0, y0) ->
6121 let dx = x - x0
6122 and dy = y0 - y in
6123 state.mstate <- Mpan (x, y);
6124 if canpan ()
6125 then state.x <- state.x + dx;
6126 let y = clamp dy in
6127 gotoy_and_clear_text y
6129 | Msel (a, _) ->
6130 state.mstate <- Msel (a, (x, y));
6131 G.postRedisplay "motion select";
6133 | Mscrolly ->
6134 let y = min state.winh (max 0 y) in
6135 scrolly y
6137 | Mscrollx ->
6138 let x = min state.winw (max 0 x) in
6139 scrollx x
6141 | Mzoomrect (p0, _) ->
6142 state.mstate <- Mzoomrect (p0, (x, y));
6143 G.postRedisplay "motion zoomrect";
6144 end;
6145 state.uioh
6147 method pmotion x y =
6148 begin match state.mode with
6149 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6150 let rec loop = function
6151 | [] ->
6152 if hooverpageno != -1
6153 then (
6154 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6155 G.postRedisplay "pmotion birdseye no hoover";
6157 | l :: rest ->
6158 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6159 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6160 then (
6161 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6162 G.postRedisplay "pmotion birdseye hoover";
6164 else loop rest
6166 loop state.layout
6168 | Textentry _ -> ()
6170 | LinkNav _
6171 | View ->
6172 match state.mstate with
6173 | Mnone -> updateunder x y
6174 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6176 end;
6177 state.uioh
6179 method infochanged _ = ()
6181 method scrollph =
6182 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6183 let p, h = scrollph state.y maxy in
6184 state.scrollw, p, h
6186 method scrollpw =
6187 let winw = state.winw - state.scrollw - 1 in
6188 let fwinw = float winw in
6189 let sw =
6190 let sw = fwinw /. float state.w in
6191 let sw = fwinw *. sw in
6192 max sw (float conf.scrollh)
6194 let position, sw =
6195 let f = state.w+winw in
6196 let r = float (winw-state.x) /. float f in
6197 let p = fwinw *. r in
6198 p-.sw/.2., sw
6200 let sw =
6201 if position +. sw > fwinw
6202 then fwinw -. position
6203 else sw
6205 state.hscrollh, position, sw
6207 method modehash =
6208 let modename =
6209 match state.mode with
6210 | LinkNav _ -> "links"
6211 | Textentry _ -> "textentry"
6212 | Birdseye _ -> "birdseye"
6213 | View -> "view"
6215 findkeyhash conf modename
6217 method eformsgs = true
6218 end;;
6220 module Config =
6221 struct
6222 open Parser
6224 let fontpath = ref "";;
6226 module KeyMap =
6227 Map.Make (struct type t = (int * int) let compare = compare end);;
6229 let unent s =
6230 let l = String.length s in
6231 let b = Buffer.create l in
6232 unent b s 0 l;
6233 Buffer.contents b;
6236 let home =
6237 try Sys.getenv "HOME"
6238 with exn ->
6239 prerr_endline
6240 ("Can not determine home directory location: " ^ exntos exn);
6244 let modifier_of_string = function
6245 | "alt" -> Wsi.altmask
6246 | "shift" -> Wsi.shiftmask
6247 | "ctrl" | "control" -> Wsi.ctrlmask
6248 | "meta" -> Wsi.metamask
6249 | _ -> 0
6252 let key_of_string =
6253 let r = Str.regexp "-" in
6254 fun s ->
6255 let elems = Str.full_split r s in
6256 let f n k m =
6257 let g s =
6258 let m1 = modifier_of_string s in
6259 if m1 = 0
6260 then (Wsi.namekey s, m)
6261 else (k, m lor m1)
6262 in function
6263 | Str.Delim s when n land 1 = 0 -> g s
6264 | Str.Text s -> g s
6265 | Str.Delim _ -> (k, m)
6267 let rec loop n k m = function
6268 | [] -> (k, m)
6269 | x :: xs ->
6270 let k, m = f n k m x in
6271 loop (n+1) k m xs
6273 loop 0 0 0 elems
6276 let keys_of_string =
6277 let r = Str.regexp "[ \t]" in
6278 fun s ->
6279 let elems = Str.split r s in
6280 List.map key_of_string elems
6283 let copykeyhashes c =
6284 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6287 let config_of c attrs =
6288 let apply c k v =
6290 match k with
6291 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6292 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6293 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6294 | "preload" -> { c with preload = bool_of_string v }
6295 | "page-bias" -> { c with pagebias = int_of_string v }
6296 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6297 | "horizontal-scroll-step" ->
6298 { c with hscrollstep = max (int_of_string v) 1 }
6299 | "auto-scroll-step" ->
6300 { c with autoscrollstep = max 0 (int_of_string v) }
6301 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6302 | "crop-hack" -> { c with crophack = bool_of_string v }
6303 | "throttle" ->
6304 let mw =
6305 match String.lowercase v with
6306 | "true" -> Some infinity
6307 | "false" -> None
6308 | f -> Some (float_of_string f)
6310 { c with maxwait = mw}
6311 | "highlight-links" -> { c with hlinks = bool_of_string v }
6312 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6313 | "vertical-margin" ->
6314 { c with interpagespace = max 0 (int_of_string v) }
6315 | "zoom" ->
6316 let zoom = float_of_string v /. 100. in
6317 let zoom = max zoom 0.0 in
6318 { c with zoom = zoom }
6319 | "presentation" -> { c with presentation = bool_of_string v }
6320 | "rotation-angle" -> { c with angle = int_of_string v }
6321 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6322 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6323 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6324 | "proportional-display" ->
6325 let fm =
6326 if bool_of_string v
6327 then FitProportional
6328 else FitWidth
6330 { c with fitmodel = fm }
6331 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6332 | "pixmap-cache-size" ->
6333 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6334 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6335 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6336 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6337 | "persistent-location" -> { c with jumpback = bool_of_string v }
6338 | "background-color" -> { c with bgcolor = color_of_string v }
6339 | "scrollbar-in-presentation" ->
6340 { c with scrollbarinpm = bool_of_string v }
6341 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6342 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6343 | "mupdf-store-size" ->
6344 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6345 | "checkers" -> { c with checkers = bool_of_string v }
6346 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6347 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6348 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6349 | "uri-launcher" -> { c with urilauncher = unent v }
6350 | "path-launcher" -> { c with pathlauncher = unent v }
6351 | "color-space" -> { c with colorspace = colorspace_of_string v }
6352 | "invert-colors" -> { c with invert = bool_of_string v }
6353 | "brightness" -> { c with colorscale = float_of_string v }
6354 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6355 | "ghyllscroll" ->
6356 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6357 | "columns" ->
6358 let (n, _, _) as nab = multicolumns_of_string v in
6359 if n < 0
6360 then { c with columns = Csplit (-n, [||]) }
6361 else { c with columns = Cmulti (nab, [||]) }
6362 | "birds-eye-columns" ->
6363 { c with beyecolumns = Some (max (int_of_string v) 2) }
6364 | "selection-command" -> { c with selcmd = unent v }
6365 | "synctex-command" -> { c with stcmd = unent v }
6366 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6367 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6368 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6369 | "use-pbo" -> { c with usepbo = bool_of_string v }
6370 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6371 | _ -> c
6372 with exn ->
6373 prerr_endline ("Error processing attribute (`" ^
6374 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6377 let rec fold c = function
6378 | [] -> c
6379 | (k, v) :: rest ->
6380 let c = apply c k v in
6381 fold c rest
6383 fold { c with keyhashes = copykeyhashes c } attrs;
6386 let fromstring f pos n v d =
6387 try f v
6388 with exn ->
6389 dolog "Error processing attribute (%S=%S) at %d\n%s"
6390 n v pos (exntos exn)
6395 let bookmark_of attrs =
6396 let rec fold title page rely visy = function
6397 | ("title", v) :: rest -> fold v page rely visy rest
6398 | ("page", v) :: rest -> fold title v rely visy rest
6399 | ("rely", v) :: rest -> fold title page v visy rest
6400 | ("visy", v) :: rest -> fold title page rely v rest
6401 | _ :: rest -> fold title page rely visy rest
6402 | [] -> title, page, rely, visy
6404 fold "invalid" "0" "0" "0" attrs
6407 let doc_of attrs =
6408 let rec fold path page rely pan visy = function
6409 | ("path", v) :: rest -> fold v page rely pan visy rest
6410 | ("page", v) :: rest -> fold path v rely pan visy rest
6411 | ("rely", v) :: rest -> fold path page v pan visy rest
6412 | ("pan", v) :: rest -> fold path page rely v visy rest
6413 | ("visy", v) :: rest -> fold path page rely pan v rest
6414 | _ :: rest -> fold path page rely pan visy rest
6415 | [] -> path, page, rely, pan, visy
6417 fold "" "0" "0" "0" "0" attrs
6420 let map_of attrs =
6421 let rec fold rs ls = function
6422 | ("out", v) :: rest -> fold v ls rest
6423 | ("in", v) :: rest -> fold rs v rest
6424 | _ :: rest -> fold ls rs rest
6425 | [] -> ls, rs
6427 fold "" "" attrs
6430 let setconf dst src =
6431 dst.scrollbw <- src.scrollbw;
6432 dst.scrollh <- src.scrollh;
6433 dst.icase <- src.icase;
6434 dst.preload <- src.preload;
6435 dst.pagebias <- src.pagebias;
6436 dst.verbose <- src.verbose;
6437 dst.scrollstep <- src.scrollstep;
6438 dst.maxhfit <- src.maxhfit;
6439 dst.crophack <- src.crophack;
6440 dst.autoscrollstep <- src.autoscrollstep;
6441 dst.maxwait <- src.maxwait;
6442 dst.hlinks <- src.hlinks;
6443 dst.underinfo <- src.underinfo;
6444 dst.interpagespace <- src.interpagespace;
6445 dst.zoom <- src.zoom;
6446 dst.presentation <- src.presentation;
6447 dst.angle <- src.angle;
6448 dst.cwinw <- src.cwinw;
6449 dst.cwinh <- src.cwinh;
6450 dst.savebmarks <- src.savebmarks;
6451 dst.memlimit <- src.memlimit;
6452 dst.fitmodel <- src.fitmodel;
6453 dst.texcount <- src.texcount;
6454 dst.sliceheight <- src.sliceheight;
6455 dst.thumbw <- src.thumbw;
6456 dst.jumpback <- src.jumpback;
6457 dst.bgcolor <- src.bgcolor;
6458 dst.scrollbarinpm <- src.scrollbarinpm;
6459 dst.tilew <- src.tilew;
6460 dst.tileh <- src.tileh;
6461 dst.mustoresize <- src.mustoresize;
6462 dst.checkers <- src.checkers;
6463 dst.aalevel <- src.aalevel;
6464 dst.trimmargins <- src.trimmargins;
6465 dst.trimfuzz <- src.trimfuzz;
6466 dst.urilauncher <- src.urilauncher;
6467 dst.colorspace <- src.colorspace;
6468 dst.invert <- src.invert;
6469 dst.colorscale <- src.colorscale;
6470 dst.redirectstderr <- src.redirectstderr;
6471 dst.ghyllscroll <- src.ghyllscroll;
6472 dst.columns <- src.columns;
6473 dst.beyecolumns <- src.beyecolumns;
6474 dst.selcmd <- src.selcmd;
6475 dst.updatecurs <- src.updatecurs;
6476 dst.pathlauncher <- src.pathlauncher;
6477 dst.keyhashes <- copykeyhashes src;
6478 dst.hfsize <- src.hfsize;
6479 dst.hscrollstep <- src.hscrollstep;
6480 dst.pgscale <- src.pgscale;
6481 dst.usepbo <- src.usepbo;
6482 dst.wheelbypage <- src.wheelbypage;
6483 dst.stcmd <- src.stcmd;
6486 let get s =
6487 let h = Hashtbl.create 10 in
6488 let dc = { defconf with angle = defconf.angle } in
6489 let rec toplevel v t spos _ =
6490 match t with
6491 | Vdata | Vcdata | Vend -> v
6492 | Vopen ("llppconfig", _, closed) ->
6493 if closed
6494 then v
6495 else { v with f = llppconfig }
6496 | Vopen _ ->
6497 error "unexpected subelement at top level" s spos
6498 | Vclose _ -> error "unexpected close at top level" s spos
6500 and llppconfig v t spos _ =
6501 match t with
6502 | Vdata | Vcdata -> v
6503 | Vend -> error "unexpected end of input in llppconfig" s spos
6504 | Vopen ("defaults", attrs, closed) ->
6505 let c = config_of dc attrs in
6506 setconf dc c;
6507 if closed
6508 then v
6509 else { v with f = defaults }
6511 | Vopen ("ui-font", attrs, closed) ->
6512 let rec getsize size = function
6513 | [] -> size
6514 | ("size", v) :: rest ->
6515 let size =
6516 fromstring int_of_string spos "size" v fstate.fontsize in
6517 getsize size rest
6518 | l -> getsize size l
6520 fstate.fontsize <- getsize fstate.fontsize attrs;
6521 if closed
6522 then v
6523 else { v with f = uifont (Buffer.create 10) }
6525 | Vopen ("doc", attrs, closed) ->
6526 let pathent, spage, srely, span, svisy = doc_of attrs in
6527 let path = unent pathent
6528 and pageno = fromstring int_of_string spos "page" spage 0
6529 and rely = fromstring float_of_string spos "rely" srely 0.0
6530 and pan = fromstring int_of_string spos "pan" span 0
6531 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6532 let c = config_of dc attrs in
6533 let anchor = (pageno, rely, visy) in
6534 if closed
6535 then (Hashtbl.add h path (c, [], pan, anchor); v)
6536 else { v with f = doc path pan anchor c [] }
6538 | Vopen _ ->
6539 error "unexpected subelement in llppconfig" s spos
6541 | Vclose "llppconfig" -> { v with f = toplevel }
6542 | Vclose _ -> error "unexpected close in llppconfig" s spos
6544 and defaults v t spos _ =
6545 match t with
6546 | Vdata | Vcdata -> v
6547 | Vend -> error "unexpected end of input in defaults" s spos
6548 | Vopen ("keymap", attrs, closed) ->
6549 let modename =
6550 try List.assoc "mode" attrs
6551 with Not_found -> "global" in
6552 if closed
6553 then v
6554 else
6555 let ret keymap =
6556 let h = findkeyhash dc modename in
6557 KeyMap.iter (Hashtbl.replace h) keymap;
6558 defaults
6560 { v with f = pkeymap ret KeyMap.empty }
6562 | Vopen (_, _, _) ->
6563 error "unexpected subelement in defaults" s spos
6565 | Vclose "defaults" ->
6566 { v with f = llppconfig }
6568 | Vclose _ -> error "unexpected close in defaults" s spos
6570 and uifont b v t spos epos =
6571 match t with
6572 | Vdata | Vcdata ->
6573 Buffer.add_substring b s spos (epos - spos);
6575 | Vopen (_, _, _) ->
6576 error "unexpected subelement in ui-font" s spos
6577 | Vclose "ui-font" ->
6578 if String.length !fontpath = 0
6579 then fontpath := Buffer.contents b;
6580 { v with f = llppconfig }
6581 | Vclose _ -> error "unexpected close in ui-font" s spos
6582 | Vend -> error "unexpected end of input in ui-font" s spos
6584 and doc path pan anchor c bookmarks v t spos _ =
6585 match t with
6586 | Vdata | Vcdata -> v
6587 | Vend -> error "unexpected end of input in doc" s spos
6588 | Vopen ("bookmarks", _, closed) ->
6589 if closed
6590 then v
6591 else { v with f = pbookmarks path pan anchor c bookmarks }
6593 | Vopen ("keymap", attrs, closed) ->
6594 let modename =
6595 try List.assoc "mode" attrs
6596 with Not_found -> "global"
6598 if closed
6599 then v
6600 else
6601 let ret keymap =
6602 let h = findkeyhash c modename in
6603 KeyMap.iter (Hashtbl.replace h) keymap;
6604 doc path pan anchor c bookmarks
6606 { v with f = pkeymap ret KeyMap.empty }
6608 | Vopen (_, _, _) ->
6609 error "unexpected subelement in doc" s spos
6611 | Vclose "doc" ->
6612 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6613 { v with f = llppconfig }
6615 | Vclose _ -> error "unexpected close in doc" s spos
6617 and pkeymap ret keymap v t spos _ =
6618 match t with
6619 | Vdata | Vcdata -> v
6620 | Vend -> error "unexpected end of input in keymap" s spos
6621 | Vopen ("map", attrs, closed) ->
6622 let r, l = map_of attrs in
6623 let kss = fromstring keys_of_string spos "in" r [] in
6624 let lss = fromstring keys_of_string spos "out" l [] in
6625 let keymap =
6626 match kss with
6627 | [] -> keymap
6628 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6629 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6631 if closed
6632 then { v with f = pkeymap ret keymap }
6633 else
6634 let f () = v in
6635 { v with f = skip "map" f }
6637 | Vopen _ ->
6638 error "unexpected subelement in keymap" s spos
6640 | Vclose "keymap" ->
6641 { v with f = ret keymap }
6643 | Vclose _ -> error "unexpected close in keymap" s spos
6645 and pbookmarks path pan anchor c bookmarks v t spos _ =
6646 match t with
6647 | Vdata | Vcdata -> v
6648 | Vend -> error "unexpected end of input in bookmarks" s spos
6649 | Vopen ("item", attrs, closed) ->
6650 let titleent, spage, srely, svisy = bookmark_of attrs in
6651 let page = fromstring int_of_string spos "page" spage 0
6652 and rely = fromstring float_of_string spos "rely" srely 0.0
6653 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6654 let bookmarks =
6655 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6657 if closed
6658 then { v with f = pbookmarks path pan anchor c bookmarks }
6659 else
6660 let f () = v in
6661 { v with f = skip "item" f }
6663 | Vopen _ ->
6664 error "unexpected subelement in bookmarks" s spos
6666 | Vclose "bookmarks" ->
6667 { v with f = doc path pan anchor c bookmarks }
6669 | Vclose _ -> error "unexpected close in bookmarks" s spos
6671 and skip tag f v t spos _ =
6672 match t with
6673 | Vdata | Vcdata -> v
6674 | Vend ->
6675 error ("unexpected end of input in skipped " ^ tag) s spos
6676 | Vopen (tag', _, closed) ->
6677 if closed
6678 then v
6679 else
6680 let f' () = { v with f = skip tag f } in
6681 { v with f = skip tag' f' }
6682 | Vclose ctag ->
6683 if tag = ctag
6684 then f ()
6685 else error ("unexpected close in skipped " ^ tag) s spos
6688 parse { f = toplevel; accu = () } s;
6689 h, dc;
6692 let do_load f ic =
6694 let len = in_channel_length ic in
6695 let s = String.create len in
6696 really_input ic s 0 len;
6697 f s;
6698 with
6699 | Parse_error (msg, s, pos) ->
6700 let subs = subs s pos in
6701 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6702 failwith ("parse error: " ^ s)
6704 | exn ->
6705 failwith ("config load error: " ^ exntos exn)
6708 let defconfpath =
6709 let dir =
6711 let dir = Filename.concat home ".config" in
6712 if Sys.is_directory dir then dir else home
6713 with _ -> home
6715 Filename.concat dir "llpp.conf"
6718 let confpath = ref defconfpath;;
6720 let load1 f =
6721 if Sys.file_exists !confpath
6722 then
6723 match
6724 (try Some (open_in_bin !confpath)
6725 with exn ->
6726 prerr_endline
6727 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6728 exntos exn);
6729 None
6731 with
6732 | Some ic ->
6733 let success =
6735 f (do_load get ic)
6736 with exn ->
6737 prerr_endline
6738 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6739 exntos exn);
6740 false
6742 close_in ic;
6743 success
6745 | None -> false
6746 else
6747 f (Hashtbl.create 0, defconf)
6750 let load () =
6751 let f (h, dc) =
6752 let pc, pb, px, pa =
6754 Hashtbl.find h (Filename.basename state.origin)
6755 with Not_found -> dc, [], 0, emptyanchor
6757 setconf defconf dc;
6758 setconf conf pc;
6759 state.bookmarks <- pb;
6760 state.x <- px;
6761 state.scrollw <- conf.scrollbw;
6762 if conf.jumpback
6763 then state.anchor <- pa;
6764 cbput state.hists.nav pa;
6765 true
6767 load1 f
6770 let add_attrs bb always dc c =
6771 let ob s a b =
6772 if always || a != b
6773 then Printf.bprintf bb "\n %s='%b'" s a
6774 and oi s a b =
6775 if always || a != b
6776 then Printf.bprintf bb "\n %s='%d'" s a
6777 and oI s a b =
6778 if always || a != b
6779 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6780 and oz s a b =
6781 if always || a <> b
6782 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6783 and oF s a b =
6784 if always || a <> b
6785 then Printf.bprintf bb "\n %s='%f'" s a
6786 and oc s a b =
6787 if always || a <> b
6788 then
6789 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6790 and oC s a b =
6791 if always || a <> b
6792 then
6793 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6794 and oR s a b =
6795 if always || a <> b
6796 then
6797 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6798 and os s a b =
6799 if always || a <> b
6800 then
6801 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6802 and og s a b =
6803 if always || a <> b
6804 then
6805 match a with
6806 | None -> ()
6807 | Some (_N, _A, _B) ->
6808 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6809 and oW s a b =
6810 if always || a <> b
6811 then
6812 let v =
6813 match a with
6814 | None -> "false"
6815 | Some f ->
6816 if f = infinity
6817 then "true"
6818 else string_of_float f
6820 Printf.bprintf bb "\n %s='%s'" s v
6821 and oco s a b =
6822 if always || a <> b
6823 then
6824 match a with
6825 | Cmulti ((n, a, b), _) when n > 1 ->
6826 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6827 | Csplit (n, _) when n > 1 ->
6828 Printf.bprintf bb "\n %s='%d'" s ~-n
6829 | _ -> ()
6830 and obeco s a b =
6831 if always || a <> b
6832 then
6833 match a with
6834 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6835 | _ -> ()
6836 and oFm s a b =
6837 if always || a <> b
6838 then
6839 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6841 oi "width" c.cwinw dc.cwinw;
6842 oi "height" c.cwinh dc.cwinh;
6843 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6844 oi "scroll-handle-height" c.scrollh dc.scrollh;
6845 ob "case-insensitive-search" c.icase dc.icase;
6846 ob "preload" c.preload dc.preload;
6847 oi "page-bias" c.pagebias dc.pagebias;
6848 oi "scroll-step" c.scrollstep dc.scrollstep;
6849 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6850 ob "max-height-fit" c.maxhfit dc.maxhfit;
6851 ob "crop-hack" c.crophack dc.crophack;
6852 oW "throttle" c.maxwait dc.maxwait;
6853 ob "highlight-links" c.hlinks dc.hlinks;
6854 ob "under-cursor-info" c.underinfo dc.underinfo;
6855 oi "vertical-margin" c.interpagespace dc.interpagespace;
6856 oz "zoom" c.zoom dc.zoom;
6857 ob "presentation" c.presentation dc.presentation;
6858 oi "rotation-angle" c.angle dc.angle;
6859 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6860 oFm "fit-model" c.fitmodel dc.fitmodel;
6861 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6862 oi "tex-count" c.texcount dc.texcount;
6863 oi "slice-height" c.sliceheight dc.sliceheight;
6864 oi "thumbnail-width" c.thumbw dc.thumbw;
6865 ob "persistent-location" c.jumpback dc.jumpback;
6866 oc "background-color" c.bgcolor dc.bgcolor;
6867 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6868 oi "tile-width" c.tilew dc.tilew;
6869 oi "tile-height" c.tileh dc.tileh;
6870 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6871 ob "checkers" c.checkers dc.checkers;
6872 oi "aalevel" c.aalevel dc.aalevel;
6873 ob "trim-margins" c.trimmargins dc.trimmargins;
6874 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6875 os "uri-launcher" c.urilauncher dc.urilauncher;
6876 os "path-launcher" c.pathlauncher dc.pathlauncher;
6877 oC "color-space" c.colorspace dc.colorspace;
6878 ob "invert-colors" c.invert dc.invert;
6879 oF "brightness" c.colorscale dc.colorscale;
6880 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6881 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6882 oco "columns" c.columns dc.columns;
6883 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6884 os "selection-command" c.selcmd dc.selcmd;
6885 os "synctex-command" c.stcmd dc.stcmd;
6886 ob "update-cursor" c.updatecurs dc.updatecurs;
6887 oi "hint-font-size" c.hfsize dc.hfsize;
6888 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6889 oF "page-scroll-scale" c.pgscale dc.pgscale;
6890 ob "use-pbo" c.usepbo dc.usepbo;
6891 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6894 let keymapsbuf always dc c =
6895 let bb = Buffer.create 16 in
6896 let rec loop = function
6897 | [] -> ()
6898 | (modename, h) :: rest ->
6899 let dh = findkeyhash dc modename in
6900 if always || h <> dh
6901 then (
6902 if Hashtbl.length h > 0
6903 then (
6904 if Buffer.length bb > 0
6905 then Buffer.add_char bb '\n';
6906 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6907 Hashtbl.iter (fun i o ->
6908 let isdifferent = always ||
6910 let dO = Hashtbl.find dh i in
6911 dO <> o
6912 with Not_found -> true
6914 if isdifferent
6915 then
6916 let addkm (k, m) =
6917 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6918 if Wsi.withalt m then Buffer.add_string bb "alt-";
6919 if Wsi.withshift m then Buffer.add_string bb "shift-";
6920 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6921 Buffer.add_string bb (Wsi.keyname k);
6923 let addkms l =
6924 let rec loop = function
6925 | [] -> ()
6926 | km :: [] -> addkm km
6927 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6929 loop l
6931 Buffer.add_string bb "<map in='";
6932 addkm i;
6933 match o with
6934 | KMinsrt km ->
6935 Buffer.add_string bb "' out='";
6936 addkm km;
6937 Buffer.add_string bb "'/>\n"
6939 | KMinsrl kms ->
6940 Buffer.add_string bb "' out='";
6941 addkms kms;
6942 Buffer.add_string bb "'/>\n"
6944 | KMmulti (ins, kms) ->
6945 Buffer.add_char bb ' ';
6946 addkms ins;
6947 Buffer.add_string bb "' out='";
6948 addkms kms;
6949 Buffer.add_string bb "'/>\n"
6950 ) h;
6951 Buffer.add_string bb "</keymap>";
6954 loop rest
6956 loop c.keyhashes;
6960 let save () =
6961 let uifontsize = fstate.fontsize in
6962 let bb = Buffer.create 32768 in
6963 let w, h =
6964 List.fold_left
6965 (fun (w, h) ws ->
6966 match ws with
6967 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6968 | Wsi.MaxVert -> (w, conf.cwinh)
6969 | Wsi.MaxHorz -> (conf.cwinw, h)
6971 (state.winw, state.winh) state.winstate
6973 conf.cwinw <- w;
6974 conf.cwinh <- h;
6975 let f (h, dc) =
6976 let dc = if conf.bedefault then conf else dc in
6977 Buffer.add_string bb "<llppconfig>\n";
6979 if String.length !fontpath > 0
6980 then
6981 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6982 uifontsize
6983 !fontpath
6984 else (
6985 if uifontsize <> 14
6986 then
6987 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6990 Buffer.add_string bb "<defaults ";
6991 add_attrs bb true dc dc;
6992 let kb = keymapsbuf true dc dc in
6993 if Buffer.length kb > 0
6994 then (
6995 Buffer.add_string bb ">\n";
6996 Buffer.add_buffer bb kb;
6997 Buffer.add_string bb "\n</defaults>\n";
6999 else Buffer.add_string bb "/>\n";
7001 let adddoc path pan anchor c bookmarks =
7002 if bookmarks == [] && c = dc && anchor = emptyanchor
7003 then ()
7004 else (
7005 Printf.bprintf bb "<doc path='%s'"
7006 (enent path 0 (String.length path));
7008 if anchor <> emptyanchor
7009 then (
7010 let n, rely, visy = anchor in
7011 Printf.bprintf bb " page='%d'" n;
7012 if rely > 1e-6
7013 then
7014 Printf.bprintf bb " rely='%f'" rely
7016 if abs_float visy > 1e-6
7017 then
7018 Printf.bprintf bb " visy='%f'" visy
7022 if pan != 0
7023 then Printf.bprintf bb " pan='%d'" pan;
7025 add_attrs bb false dc c;
7026 let kb = keymapsbuf false dc c in
7028 begin match bookmarks with
7029 | [] ->
7030 if Buffer.length kb > 0
7031 then (
7032 Buffer.add_string bb ">\n";
7033 Buffer.add_buffer bb kb;
7034 Buffer.add_string bb "\n</doc>\n";
7036 else Buffer.add_string bb "/>\n"
7037 | _ ->
7038 Buffer.add_string bb ">\n<bookmarks>\n";
7039 List.iter (fun (title, _level, (page, rely, visy)) ->
7040 Printf.bprintf bb
7041 "<item title='%s' page='%d'"
7042 (enent title 0 (String.length title))
7043 page
7045 if rely > 1e-6
7046 then
7047 Printf.bprintf bb " rely='%f'" rely
7049 if abs_float visy > 1e-6
7050 then
7051 Printf.bprintf bb " visy='%f'" visy
7053 Buffer.add_string bb "/>\n";
7054 ) bookmarks;
7055 Buffer.add_string bb "</bookmarks>";
7056 if Buffer.length kb > 0
7057 then (
7058 Buffer.add_string bb "\n";
7059 Buffer.add_buffer bb kb;
7061 Buffer.add_string bb "\n</doc>\n";
7062 end;
7066 let pan, conf =
7067 match state.mode with
7068 | Birdseye (c, pan, _, _, _) ->
7069 let beyecolumns =
7070 match conf.columns with
7071 | Cmulti ((c, _, _), _) -> Some c
7072 | Csingle _ -> None
7073 | Csplit _ -> None
7074 and columns =
7075 match c.columns with
7076 | Cmulti (c, _) -> Cmulti (c, [||])
7077 | Csingle _ -> Csingle [||]
7078 | Csplit _ -> failwith "quit from bird's eye while split"
7080 pan, { c with beyecolumns = beyecolumns; columns = columns }
7081 | _ -> state.x, conf
7083 let basename = Filename.basename state.origin in
7084 adddoc basename pan (getanchor ())
7085 (let conf =
7086 let autoscrollstep =
7087 match state.autoscroll with
7088 | Some step -> step
7089 | None -> conf.autoscrollstep
7091 match state.mode with
7092 | Birdseye (bc, _, _, _, _) ->
7093 { conf with
7094 zoom = bc.zoom;
7095 presentation = bc.presentation;
7096 interpagespace = bc.interpagespace;
7097 maxwait = bc.maxwait;
7098 autoscrollstep = autoscrollstep }
7099 | _ -> { conf with autoscrollstep = autoscrollstep }
7100 in conf)
7101 (if conf.savebmarks then state.bookmarks else []);
7103 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7104 if basename <> path
7105 then adddoc path x anchor c bookmarks
7106 ) h;
7107 Buffer.add_string bb "</llppconfig>\n";
7108 true;
7110 if load1 f && Buffer.length bb > 0
7111 then
7113 let tmp = !confpath ^ ".tmp" in
7114 let oc = open_out_bin tmp in
7115 Buffer.output_buffer oc bb;
7116 close_out oc;
7117 Unix.rename tmp !confpath;
7118 with exn ->
7119 prerr_endline
7120 ("error while saving configuration: " ^ exntos exn)
7122 end;;
7124 let adderrmsg src msg =
7125 Buffer.add_string state.errmsgs msg;
7126 state.newerrmsgs <- true;
7127 G.postRedisplay src
7130 let adderrfmt src fmt =
7131 Format.kprintf (fun s -> adderrmsg src s) fmt;
7134 let ract cmds =
7135 let cl = splitatspace cmds in
7136 let scan s fmt f =
7137 try Scanf.sscanf s fmt f
7138 with exn ->
7139 adderrfmt "remote exec"
7140 "error processing '%S': %s\n" cmds (exntos exn)
7142 match cl with
7143 | "reload" :: [] -> reload ()
7144 | "goto" :: args :: [] ->
7145 scan args "%u %f %f"
7146 (fun pageno x y ->
7147 let cmd, _ = state.geomcmds in
7148 if String.length cmd = 0
7149 then gotopagexy pageno x y
7150 else
7151 let f prevf () =
7152 gotopagexy pageno x y;
7153 prevf ()
7155 state.reprf <- f state.reprf
7157 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7158 | "rect" :: args :: [] ->
7159 scan args "%u %u %f %f %f %f"
7160 (fun pageno color x0 y0 x1 y1 ->
7161 onpagerect pageno (fun w h ->
7162 let _,w1,h1,_ = getpagedim pageno in
7163 let sw = float w1 /. w
7164 and sh = float h1 /. h in
7165 let x0s = x0 *. sw
7166 and x1s = x1 *. sw
7167 and y0s = y0 *. sh
7168 and y1s = y1 *. sh in
7169 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7170 debugrect rect;
7171 state.rects <- (pageno, color, rect) :: state.rects;
7172 G.postRedisplay "rect";
7175 | "activatewin" :: [] -> Wsi.activatewin ()
7176 | "quit" :: [] -> raise Quit
7177 | _ ->
7178 adderrfmt "remote command"
7179 "error processing remote command: %S\n" cmds;
7182 let remote =
7183 let scratch = String.create 80 in
7184 let buf = Buffer.create 80 in
7185 fun fd ->
7186 let rec tempfr () =
7187 try Some (Unix.read fd scratch 0 80)
7188 with
7189 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7190 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7191 | exn -> raise exn
7193 match tempfr () with
7194 | None -> Some fd
7195 | Some n ->
7196 if n = 0
7197 then (
7198 Unix.close fd;
7199 if Buffer.length buf > 0
7200 then (
7201 let s = Buffer.contents buf in
7202 Buffer.clear buf;
7203 ract s;
7205 None
7207 else
7208 let rec eat ppos =
7209 let nlpos =
7211 let pos = String.index_from scratch ppos '\n' in
7212 if pos >= n then -1 else pos
7213 with Not_found -> -1
7215 if nlpos >= 0
7216 then (
7217 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7218 let s = Buffer.contents buf in
7219 Buffer.clear buf;
7220 ract s;
7221 eat (nlpos+1);
7223 else (
7224 Buffer.add_substring buf scratch ppos (n-ppos);
7225 Some fd
7227 in eat 0
7230 let remoteopen path =
7231 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7232 with exn ->
7233 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7234 None
7237 let () =
7238 let trimcachepath = ref "" in
7239 let rcmdpath = ref "" in
7240 Arg.parse
7241 (Arg.align
7242 [("-p", Arg.String (fun s -> state.password <- s),
7243 "<password> Set password");
7245 ("-f", Arg.String (fun s -> Config.fontpath := s),
7246 "<path> Set path to the user interface font");
7248 ("-c", Arg.String (fun s -> Config.confpath := s),
7249 "<path> Set path to the configuration file");
7251 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7252 "<path> Set path to the trim cache file");
7254 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7255 "<named-destination> Set named destination");
7257 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7259 ("-remote", Arg.String (fun s -> rcmdpath := s),
7260 "<path> Set path to the remote commands source");
7262 ("-origin", Arg.String (fun s -> state.origin <- s),
7263 "<original path> Set original path");
7265 ("-v", Arg.Unit (fun () ->
7266 Printf.printf
7267 "%s\nconfiguration path: %s\n"
7268 (version ())
7269 Config.defconfpath
7271 exit 0), " Print version and exit");
7274 (fun s -> state.path <- s)
7275 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7277 if String.length state.path = 0
7278 then (prerr_endline "file name missing"; exit 1);
7280 if not (Config.load ())
7281 then prerr_endline "failed to load configuration";
7283 let globalkeyhash = findkeyhash conf "global" in
7284 let wsfd, winw, winh = Wsi.init (object
7285 method expose =
7286 if nogeomcmds state.geomcmds || platform == Posx
7287 then display ()
7288 else (
7289 GlClear.color (scalecolor2 conf.bgcolor);
7290 GlClear.clear [`color];
7292 method display = display ()
7293 method reshape w h = reshape w h
7294 method mouse b d x y m = mouse b d x y m
7295 method motion x y = state.mpos <- (x, y); motion x y
7296 method pmotion x y = state.mpos <- (x, y); pmotion x y
7297 method key k m =
7298 let mascm = m land (
7299 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7300 ) in
7301 match state.keystate with
7302 | KSnone ->
7303 let km = k, mascm in
7304 begin
7305 match
7306 let modehash = state.uioh#modehash in
7307 try Hashtbl.find modehash km
7308 with Not_found ->
7309 try Hashtbl.find globalkeyhash km
7310 with Not_found -> KMinsrt (k, m)
7311 with
7312 | KMinsrt (k, m) -> keyboard k m
7313 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7314 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7316 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7317 List.iter (fun (k, m) -> keyboard k m) insrt;
7318 state.keystate <- KSnone
7319 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7320 state.keystate <- KSinto (keys, insrt)
7321 | _ ->
7322 state.keystate <- KSnone
7324 method enter x y = state.mpos <- (x, y); pmotion x y
7325 method leave = state.mpos <- (-1, -1)
7326 method winstate wsl = state.winstate <- wsl
7327 method quit = raise Quit
7328 end) conf.cwinw conf.cwinh (platform = Posx) in
7330 state.wsfd <- wsfd;
7332 if not (
7333 List.exists GlMisc.check_extension
7334 [ "GL_ARB_texture_rectangle"
7335 ; "GL_EXT_texture_recangle"
7336 ; "GL_NV_texture_rectangle" ]
7338 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7340 let cr, sw =
7341 match Ne.pipe () with
7342 | Ne.Exn exn ->
7343 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7344 exit 1
7345 | Ne.Res rw -> rw
7346 and sr, cw =
7347 match Ne.pipe () with
7348 | Ne.Exn exn ->
7349 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7350 exit 1
7351 | Ne.Res rw -> rw
7354 cloexec cr;
7355 cloexec sw;
7356 cloexec sr;
7357 cloexec cw;
7359 setcheckers conf.checkers;
7360 redirectstderr ();
7362 init (cr, cw) (
7363 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7364 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7365 !Config.fontpath, !trimcachepath,
7366 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7368 state.sr <- sr;
7369 state.sw <- sw;
7370 state.text <- "Opening " ^ (mbtoutf8 state.path);
7371 reshape winw winh;
7372 opendoc state.path state.password;
7373 state.uioh <- uioh;
7375 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7376 let optrfd =
7377 ref (
7378 if String.length !rcmdpath > 0
7379 then remoteopen !rcmdpath
7380 else None
7384 let rec loop deadline =
7385 let r =
7386 match state.errfd with
7387 | None -> [state.sr; state.wsfd]
7388 | Some fd -> [state.sr; state.wsfd; fd]
7390 let r =
7391 match !optrfd with
7392 | None -> r
7393 | Some fd -> fd :: r
7395 if state.redisplay
7396 then (
7397 state.redisplay <- false;
7398 display ();
7400 let timeout =
7401 let now = now () in
7402 if deadline > now
7403 then (
7404 if deadline = infinity
7405 then ~-.1.0
7406 else max 0.0 (deadline -. now)
7408 else 0.0
7410 let r, _, _ =
7411 try Unix.select r [] [] timeout
7412 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7414 begin match r with
7415 | [] ->
7416 state.ghyll None;
7417 let newdeadline =
7418 if state.ghyll == noghyll
7419 then
7420 match state.autoscroll with
7421 | Some step when step != 0 ->
7422 let y = state.y + step in
7423 let y =
7424 if y < 0
7425 then state.maxy
7426 else if y >= state.maxy then 0 else y
7428 gotoy y;
7429 if state.mode = View
7430 then state.text <- "";
7431 deadline +. 0.01
7432 | _ -> infinity
7433 else deadline +. 0.01
7435 loop newdeadline
7437 | l ->
7438 let rec checkfds = function
7439 | [] -> ()
7440 | fd :: rest when fd = state.sr ->
7441 let cmd = readcmd state.sr in
7442 act cmd;
7443 checkfds rest
7445 | fd :: rest when fd = state.wsfd ->
7446 Wsi.readresp fd;
7447 checkfds rest
7449 | fd :: rest when Some fd = !optrfd ->
7450 begin match remote fd with
7451 | None -> optrfd := remoteopen !rcmdpath;
7452 | opt -> optrfd := opt
7453 end;
7454 checkfds rest
7456 | fd :: rest ->
7457 let s = String.create 80 in
7458 let n = tempfailureretry (Unix.read fd s 0) 80 in
7459 if conf.redirectstderr
7460 then (
7461 Buffer.add_substring state.errmsgs s 0 n;
7462 state.newerrmsgs <- true;
7463 state.redisplay <- true;
7465 else (
7466 prerr_string (String.sub s 0 n);
7467 flush stderr;
7469 checkfds rest
7471 checkfds l;
7472 let newdeadline =
7473 let deadline1 =
7474 if deadline = infinity
7475 then now () +. 0.01
7476 else deadline
7478 match state.autoscroll with
7479 | Some step when step != 0 -> deadline1
7480 | _ -> if state.ghyll == noghyll then infinity else deadline1
7482 loop newdeadline
7483 end;
7486 loop infinity;
7487 with Quit ->
7488 Config.save ();