Expand information screen
[llpp.git] / main.ml
blobeaa8e03663e10453d87bb3ded217ce3b10bf6a75
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 * string) 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 %d"
2166 w (h - 2*conf.interpagespace) conf.zoom
2167 (int_of_fitmodel conf.fitmodel));
2170 let enttext () =
2171 let len = String.length state.text in
2172 let drawstring s =
2173 let hscrollh =
2174 match state.mode with
2175 | Textentry _
2176 | View ->
2177 let h, _, _ = state.uioh#scrollpw in
2179 | _ -> 0
2181 let rect x w =
2182 GlDraw.rect
2183 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2184 (x+.w, float (state.winh - hscrollh))
2187 let w = float (state.winw - state.scrollw - 1) in
2188 if state.progress >= 0.0 && state.progress < 1.0
2189 then (
2190 GlDraw.color (0.3, 0.3, 0.3);
2191 let w1 = w *. state.progress in
2192 rect 0.0 w1;
2193 GlDraw.color (0.0, 0.0, 0.0);
2194 rect w1 (w-.w1)
2196 else (
2197 GlDraw.color (0.0, 0.0, 0.0);
2198 rect 0.0 w;
2201 GlDraw.color (1.0, 1.0, 1.0);
2202 drawstring fstate.fontsize
2203 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2205 let s =
2206 match state.mode with
2207 | Textentry ((prefix, text, _, _, _, _), _) ->
2208 let s =
2209 if len > 0
2210 then
2211 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2212 else
2213 Printf.sprintf "%s%s_" prefix text
2217 | _ -> state.text
2219 let s =
2220 if state.newerrmsgs
2221 then (
2222 if not (istextentry state.mode) && state.uioh#eformsgs
2223 then
2224 let s1 = "(press 'e' to review error messasges)" in
2225 if String.length s > 0 then s ^ " " ^ s1 else s1
2226 else s
2228 else s
2230 if String.length s > 0
2231 then drawstring s
2234 let gctiles () =
2235 let len = Queue.length state.tilelru in
2236 let layout = lazy (
2237 match state.throttle with
2238 | None ->
2239 if conf.preload
2240 then preloadlayout state.y
2241 else state.layout
2242 | Some (layout, _, _) ->
2243 layout
2244 ) in
2245 let rec loop qpos =
2246 if state.memused <= conf.memlimit
2247 then ()
2248 else (
2249 if qpos < len
2250 then
2251 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2252 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2253 let (_, pw, ph, _) = getpagedim n in
2255 gen = state.gen
2256 && colorspace = conf.colorspace
2257 && angle = conf.angle
2258 && pagew = pw
2259 && pageh = ph
2260 && (
2261 let x = col*conf.tilew
2262 and y = row*conf.tileh in
2263 tilevisible (Lazy.force_val layout) n x y
2265 then Queue.push lruitem state.tilelru
2266 else (
2267 freepbo p;
2268 wcmd "freetile %s" p;
2269 state.memused <- state.memused - s;
2270 state.uioh#infochanged Memused;
2271 Hashtbl.remove state.tilemap k;
2273 loop (qpos+1)
2276 loop 0
2279 let logcurrently = function
2280 | Idle -> dolog "Idle"
2281 | Loading (l, gen) ->
2282 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2283 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2284 dolog
2285 "Tiling %d[%d,%d] page=%s cs=%s angle"
2286 l.pageno col row pageopaque
2287 (colorspace_to_string colorspace)
2289 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2290 angle gen conf.angle state.gen
2291 tilew tileh
2292 conf.tilew conf.tileh
2294 | Outlining _ ->
2295 dolog "outlining"
2298 let splitatspace =
2299 let r = Str.regexp " " in
2300 fun s -> Str.bounded_split r s 2;
2303 let onpagerect pageno f =
2304 let b =
2305 match conf.columns with
2306 | Cmulti (_, b) -> b
2307 | Csingle b -> b
2308 | Csplit (_, b) -> b
2310 if pageno >= 0 && pageno < Array.length b
2311 then
2312 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2313 let r = getpdimrect pdimno in
2314 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2317 let gotopagexy1 pageno x y =
2318 onpagerect pageno (fun w h ->
2319 let top = y /. h in
2320 let _,w1,_,leftx = getpagedim pageno in
2321 let wh = state.winh - state.hscrollh in
2322 let sw = float w1 /. w in
2323 let x = sw *. x in
2324 let x = leftx + state.x + truncate x in
2325 let sx =
2326 if x < 0 || x >= state.winw - state.scrollw
2327 then state.x - x
2328 else state.x
2330 let py, h = getpageyh pageno in
2331 let pdy = truncate (top *. float h) in
2332 let y' = py + pdy in
2333 let dy = y' - state.y in
2334 let sy =
2335 if x != state.x || not (dy > 0 && dy < wh)
2336 then (
2337 if conf.presentation
2338 then
2339 if abs (py - y') > wh
2340 then y'
2341 else py
2342 else y';
2344 else state.y
2346 if state.x != sx || state.y != sy
2347 then (
2348 let x, y =
2349 if !wtmode
2350 then (
2351 let ww = state.winw - state.scrollw in
2352 let qx = sx / ww
2353 and qy = pdy / wh in
2354 let x = qx * ww
2355 and y = py + qy * wh in
2356 let x = if -x + ww > w1 then -(w1-ww) else x
2357 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2358 let y =
2359 if conf.presentation
2360 then
2361 if abs (py - y') > wh
2362 then y'
2363 else py
2364 else y';
2366 (x, y)
2368 else (sx, sy)
2370 state.x <- x;
2371 state.hscrollh <-
2372 if x = 0 && state.w <= state.winw - state.scrollw
2373 then 0
2374 else state.scrollw
2376 gotoy_and_clear_text y;
2378 else gotoy_and_clear_text state.y;
2382 let gotopagexy pageno x y =
2383 match state.mode with
2384 | Birdseye _ -> gotopage pageno 0.0
2385 | _ -> gotopagexy1 pageno x y
2388 let act cmds =
2389 (* dolog "%S" cmds; *)
2390 let cl = splitatspace cmds in
2391 let scan s fmt f =
2392 try Scanf.sscanf s fmt f
2393 with exn ->
2394 dolog "error processing '%S': %s" cmds (exntos exn);
2395 exit 1
2397 match cl with
2398 | "clear" :: [] ->
2399 state.uioh#infochanged Pdim;
2400 state.pdims <- [];
2402 | "clearrects" :: [] ->
2403 state.rects <- state.rects1;
2404 G.postRedisplay "clearrects";
2406 | "continue" :: args :: [] ->
2407 let n = scan args "%u" (fun n -> n) in
2408 state.pagecount <- n;
2409 begin match state.currently with
2410 | Outlining l ->
2411 state.currently <- Idle;
2412 state.outlines <- Array.of_list (List.rev l)
2413 | _ -> ()
2414 end;
2416 let cur, cmds = state.geomcmds in
2417 if String.length cur = 0
2418 then failwith "umpossible";
2420 begin match List.rev cmds with
2421 | [] ->
2422 state.geomcmds <- "", [];
2423 represent ();
2424 | (s, f) :: rest ->
2425 f ();
2426 state.geomcmds <- s, List.rev rest;
2427 end;
2428 if conf.maxwait = None && not !wtmode
2429 then G.postRedisplay "continue";
2431 | "title" :: args :: [] ->
2432 Wsi.settitle args
2434 | "msg" :: args :: [] ->
2435 showtext ' ' args
2437 | "vmsg" :: args :: [] ->
2438 if conf.verbose
2439 then showtext ' ' args
2441 | "emsg" :: args :: [] ->
2442 Buffer.add_string state.errmsgs args;
2443 state.newerrmsgs <- true;
2444 G.postRedisplay "error message"
2446 | "progress" :: args :: [] ->
2447 let progress, text =
2448 scan args "%f %n"
2449 (fun f pos ->
2450 f, String.sub args pos (String.length args - pos))
2452 state.text <- text;
2453 state.progress <- progress;
2454 G.postRedisplay "progress"
2456 | "firstmatch" :: args :: [] ->
2457 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2458 scan args "%u %d %f %f %f %f %f %f %f %f"
2459 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2460 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2462 let y = (getpagey pageno) + truncate y0 in
2463 addnav ();
2464 gotoy y;
2465 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2467 | "match" :: args :: [] ->
2468 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2469 scan args "%u %d %f %f %f %f %f %f %f %f"
2470 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2471 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2473 state.rects1 <-
2474 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2476 | "page" :: args :: [] ->
2477 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2478 begin match state.currently with
2479 | Loading (l, gen) ->
2480 vlog "page %d took %f sec" l.pageno t;
2481 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2482 begin match state.throttle with
2483 | None ->
2484 let preloadedpages =
2485 if conf.preload
2486 then preloadlayout state.y
2487 else state.layout
2489 let evict () =
2490 let set =
2491 List.fold_left (fun s l -> IntSet.add l.pageno s)
2492 IntSet.empty preloadedpages
2494 let evictedpages =
2495 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2496 if not (IntSet.mem pageno set)
2497 then (
2498 wcmd "freepage %s" opaque;
2499 key :: accu
2501 else accu
2502 ) state.pagemap []
2504 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2506 evict ();
2507 state.currently <- Idle;
2508 if gen = state.gen
2509 then (
2510 tilepage l.pageno pageopaque state.layout;
2511 load state.layout;
2512 load preloadedpages;
2513 if pagevisible state.layout l.pageno
2514 && layoutready state.layout
2515 then G.postRedisplay "page";
2518 | Some (layout, _, _) ->
2519 state.currently <- Idle;
2520 tilepage l.pageno pageopaque layout;
2521 load state.layout
2522 end;
2524 | _ ->
2525 dolog "Inconsistent loading state";
2526 logcurrently state.currently;
2527 exit 1
2530 | "tile" :: args :: [] ->
2531 let (x, y, opaque, size, t) =
2532 scan args "%u %u %s %u %f"
2533 (fun x y p size t -> (x, y, p, size, t))
2535 begin match state.currently with
2536 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2537 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2539 unmappbo opaque;
2540 if tilew != conf.tilew || tileh != conf.tileh
2541 then (
2542 wcmd "freetile %s" opaque;
2543 state.currently <- Idle;
2544 load state.layout;
2546 else (
2547 puttileopaque l col row gen cs angle opaque size t;
2548 state.memused <- state.memused + size;
2549 state.uioh#infochanged Memused;
2550 gctiles ();
2551 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2552 opaque, size) state.tilelru;
2554 let layout =
2555 match state.throttle with
2556 | None -> state.layout
2557 | Some (layout, _, _) -> layout
2560 state.currently <- Idle;
2561 if gen = state.gen
2562 && conf.colorspace = cs
2563 && conf.angle = angle
2564 && tilevisible layout l.pageno x y
2565 then conttiling l.pageno pageopaque;
2567 begin match state.throttle with
2568 | None ->
2569 preload state.layout;
2570 if gen = state.gen
2571 && conf.colorspace = cs
2572 && conf.angle = angle
2573 && tilevisible state.layout l.pageno x y
2574 && (not !wtmode || layoutready state.layout)
2575 then G.postRedisplay "tile nothrottle";
2577 | Some (layout, y, _) ->
2578 let ready = layoutready layout in
2579 if ready
2580 then (
2581 state.y <- y;
2582 state.layout <- layout;
2583 state.throttle <- None;
2584 G.postRedisplay "throttle";
2586 else load layout;
2587 end;
2590 | _ ->
2591 dolog "Inconsistent tiling state";
2592 logcurrently state.currently;
2593 exit 1
2596 | "pdim" :: args :: [] ->
2597 let pdim =
2598 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2600 state.uioh#infochanged Pdim;
2601 state.pdims <- pdim :: state.pdims
2603 | "o" :: args :: [] ->
2604 let (l, n, t, h, pos) =
2605 scan args "%u %u %d %u %n"
2606 (fun l n t h pos -> l, n, t, h, pos)
2608 let s = String.sub args pos (String.length args - pos) in
2609 let outline = (s, l, (n, float t /. float h, 0.0)) in
2610 begin match state.currently with
2611 | Outlining outlines ->
2612 state.currently <- Outlining (outline :: outlines)
2613 | Idle ->
2614 state.currently <- Outlining [outline]
2615 | currently ->
2616 dolog "invalid outlining state";
2617 logcurrently currently
2620 | "a" :: args :: [] ->
2621 let (n, l, t) =
2622 scan args "%u %d %d" (fun n l t -> n, l, t)
2624 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2626 | "info" :: args :: [] ->
2627 state.docinfo <- (1, args) :: state.docinfo
2629 | "infoend" :: [] ->
2630 state.uioh#infochanged Docinfo;
2631 state.docinfo <- List.rev state.docinfo
2633 | _ ->
2634 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2637 let onhist cb =
2638 let rc = cb.rc in
2639 let action = function
2640 | HCprev -> cbget cb ~-1
2641 | HCnext -> cbget cb 1
2642 | HCfirst -> cbget cb ~-(cb.rc)
2643 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2644 and cancel () = cb.rc <- rc
2645 in (action, cancel)
2648 let search pattern forward =
2649 if String.length pattern > 0
2650 then
2651 let pn, py =
2652 match state.layout with
2653 | [] -> 0, 0
2654 | l :: _ ->
2655 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2657 wcmd "search %d %d %d %d,%s\000"
2658 (btod conf.icase) pn py (btod forward) pattern;
2661 let intentry text key =
2662 let c =
2663 if key >= 32 && key < 127
2664 then Char.chr key
2665 else '\000'
2667 match c with
2668 | '0' .. '9' ->
2669 let text = addchar text c in
2670 TEcont text
2672 | _ ->
2673 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2674 TEcont text
2677 let linknentry text key =
2678 let c =
2679 if key >= 32 && key < 127
2680 then Char.chr key
2681 else '\000'
2683 match c with
2684 | 'a' .. 'z' ->
2685 let text = addchar text c in
2686 TEcont text
2688 | _ ->
2689 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2690 TEcont text
2693 let linkndone f s =
2694 if String.length s > 0
2695 then (
2696 let n =
2697 let l = String.length s in
2698 let rec loop pos n = if pos = l then n else
2699 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2700 loop (pos+1) (n*26 + m)
2701 in loop 0 0
2703 let rec loop n = function
2704 | [] -> ()
2705 | l :: rest ->
2706 match getopaque l.pageno with
2707 | None -> loop n rest
2708 | Some opaque ->
2709 let m = getlinkcount opaque in
2710 if n < m
2711 then (
2712 let under = getlink opaque n in
2713 f under
2715 else loop (n-m) rest
2717 loop n state.layout;
2721 let textentry text key =
2722 if key land 0xff00 = 0xff00
2723 then TEcont text
2724 else TEcont (text ^ toutf8 key)
2727 let reqlayout angle fitmodel =
2728 match state.throttle with
2729 | None ->
2730 if nogeomcmds state.geomcmds
2731 then state.anchor <- getanchor ();
2732 conf.angle <- angle mod 360;
2733 if conf.angle != 0
2734 then (
2735 match state.mode with
2736 | LinkNav _ -> state.mode <- View
2737 | _ -> ()
2739 conf.fitmodel <- fitmodel;
2740 invalidate "reqlayout"
2741 (fun () ->
2742 wcmd "reqlayout %d %d %f"
2743 conf.angle (int_of_fitmodel fitmodel) conf.zoom);
2744 | _ -> ()
2747 let settrim trimmargins trimfuzz =
2748 if nogeomcmds state.geomcmds
2749 then state.anchor <- getanchor ();
2750 conf.trimmargins <- trimmargins;
2751 conf.trimfuzz <- trimfuzz;
2752 let x0, y0, x1, y1 = trimfuzz in
2753 invalidate "settrim"
2754 (fun () ->
2755 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2756 flushpages ();
2759 let setzoom zoom =
2760 match state.throttle with
2761 | None ->
2762 let zoom = max 0.0001 zoom in
2763 if zoom <> conf.zoom
2764 then (
2765 state.prevzoom <- conf.zoom;
2766 conf.zoom <- zoom;
2767 reshape state.winw state.winh;
2768 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2771 | Some (layout, y, started) ->
2772 let time =
2773 match conf.maxwait with
2774 | None -> 0.0
2775 | Some t -> t
2777 let dt = now () -. started in
2778 if dt > time
2779 then (
2780 state.y <- y;
2781 load layout;
2785 let setcolumns mode columns coverA coverB =
2786 state.prevcolumns <- Some (conf.columns, conf.zoom);
2787 if columns < 0
2788 then (
2789 if isbirdseye mode
2790 then showtext '!' "split mode doesn't work in bird's eye"
2791 else (
2792 conf.columns <- Csplit (-columns, [||]);
2793 state.x <- 0;
2794 conf.zoom <- 1.0;
2797 else (
2798 if columns < 2
2799 then (
2800 conf.columns <- Csingle [||];
2801 state.x <- 0;
2802 setzoom 1.0;
2804 else (
2805 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2806 conf.zoom <- 1.0;
2809 reshape state.winw state.winh;
2812 let enterbirdseye () =
2813 let zoom = float conf.thumbw /. float state.winw in
2814 let birdseyepageno =
2815 let cy = state.winh / 2 in
2816 let fold = function
2817 | [] -> 0
2818 | l :: rest ->
2819 let rec fold best = function
2820 | [] -> best.pageno
2821 | l :: rest ->
2822 let d = cy - (l.pagedispy + l.pagevh/2)
2823 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2824 if abs d < abs dbest
2825 then fold l rest
2826 else best.pageno
2827 in fold l rest
2829 fold state.layout
2831 state.mode <- Birdseye (
2832 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2834 conf.zoom <- zoom;
2835 conf.presentation <- false;
2836 conf.interpagespace <- 10;
2837 conf.hlinks <- false;
2838 conf.fitmodel <- FitProportional;
2839 state.x <- 0;
2840 state.mstate <- Mnone;
2841 conf.maxwait <- None;
2842 conf.columns <- (
2843 match conf.beyecolumns with
2844 | Some c ->
2845 conf.zoom <- 1.0;
2846 Cmulti ((c, 0, 0), [||])
2847 | None -> Csingle [||]
2849 Wsi.setcursor Wsi.CURSOR_INHERIT;
2850 if conf.verbose
2851 then
2852 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2853 (100.0*.zoom)
2854 else
2855 state.text <- ""
2857 reshape state.winw state.winh;
2860 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2861 state.mode <- View;
2862 conf.zoom <- c.zoom;
2863 conf.presentation <- c.presentation;
2864 conf.interpagespace <- c.interpagespace;
2865 conf.maxwait <- c.maxwait;
2866 conf.hlinks <- c.hlinks;
2867 conf.fitmodel <- c.fitmodel;
2868 conf.beyecolumns <- (
2869 match conf.columns with
2870 | Cmulti ((c, _, _), _) -> Some c
2871 | Csingle _ -> None
2872 | Csplit _ -> failwith "leaving bird's eye split mode"
2874 conf.columns <- (
2875 match c.columns with
2876 | Cmulti (c, _) -> Cmulti (c, [||])
2877 | Csingle _ -> Csingle [||]
2878 | Csplit (c, _) -> Csplit (c, [||])
2880 state.x <- leftx;
2881 if conf.verbose
2882 then
2883 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2884 (100.0*.conf.zoom)
2886 reshape state.winw state.winh;
2887 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2890 let togglebirdseye () =
2891 match state.mode with
2892 | Birdseye vals -> leavebirdseye vals true
2893 | View -> enterbirdseye ()
2894 | _ -> ()
2897 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2898 let pageno = max 0 (pageno - incr) in
2899 let rec loop = function
2900 | [] -> gotopage1 pageno 0
2901 | l :: _ when l.pageno = pageno ->
2902 if l.pagedispy >= 0 && l.pagey = 0
2903 then G.postRedisplay "upbirdseye"
2904 else gotopage1 pageno 0
2905 | _ :: rest -> loop rest
2907 loop state.layout;
2908 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2911 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2912 let pageno = min (state.pagecount - 1) (pageno + incr) in
2913 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2914 let rec loop = function
2915 | [] ->
2916 let y, h = getpageyh pageno in
2917 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2918 gotoy (clamp dy)
2919 | l :: _ when l.pageno = pageno ->
2920 if l.pagevh != l.pageh
2921 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2922 else G.postRedisplay "downbirdseye"
2923 | _ :: rest -> loop rest
2925 loop state.layout
2928 let optentry mode _ key =
2929 let btos b = if b then "on" else "off" in
2930 if key >= 32 && key < 127
2931 then
2932 let c = Char.chr key in
2933 match c with
2934 | 's' ->
2935 let ondone s =
2936 try conf.scrollstep <- int_of_string s with exc ->
2937 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2939 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2941 | 'A' ->
2942 let ondone s =
2944 conf.autoscrollstep <- int_of_string s;
2945 if state.autoscroll <> None
2946 then state.autoscroll <- Some conf.autoscrollstep
2947 with exc ->
2948 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2950 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2952 | 'C' ->
2953 let ondone s =
2955 let n, a, b = multicolumns_of_string s in
2956 setcolumns mode n a b;
2957 with exc ->
2958 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2960 TEswitch ("columns: ", "", None, textentry, ondone, true)
2962 | 'Z' ->
2963 let ondone s =
2965 let zoom = float (int_of_string s) /. 100.0 in
2966 setzoom zoom
2967 with exc ->
2968 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2970 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2972 | 't' ->
2973 let ondone s =
2975 conf.thumbw <- bound (int_of_string s) 2 4096;
2976 state.text <-
2977 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2978 begin match mode with
2979 | Birdseye beye ->
2980 leavebirdseye beye false;
2981 enterbirdseye ();
2982 | _ -> ();
2984 with exc ->
2985 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2987 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2989 | 'R' ->
2990 let ondone s =
2991 match try
2992 Some (int_of_string s)
2993 with exc ->
2994 state.text <- Printf.sprintf "bad integer `%s': %s"
2995 s (exntos exc);
2996 None
2997 with
2998 | Some angle -> reqlayout angle conf.fitmodel
2999 | None -> ()
3001 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3003 | 'i' ->
3004 conf.icase <- not conf.icase;
3005 TEdone ("case insensitive search " ^ (btos conf.icase))
3007 | 'p' ->
3008 conf.preload <- not conf.preload;
3009 gotoy state.y;
3010 TEdone ("preload " ^ (btos conf.preload))
3012 | 'v' ->
3013 conf.verbose <- not conf.verbose;
3014 TEdone ("verbose " ^ (btos conf.verbose))
3016 | 'd' ->
3017 conf.debug <- not conf.debug;
3018 TEdone ("debug " ^ (btos conf.debug))
3020 | 'h' ->
3021 conf.maxhfit <- not conf.maxhfit;
3022 state.maxy <- calcheight ();
3023 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3025 | 'c' ->
3026 conf.crophack <- not conf.crophack;
3027 TEdone ("crophack " ^ btos conf.crophack)
3029 | 'a' ->
3030 let s =
3031 match conf.maxwait with
3032 | None ->
3033 conf.maxwait <- Some infinity;
3034 "always wait for page to complete"
3035 | Some _ ->
3036 conf.maxwait <- None;
3037 "show placeholder if page is not ready"
3039 TEdone s
3041 | 'f' ->
3042 conf.underinfo <- not conf.underinfo;
3043 TEdone ("underinfo " ^ btos conf.underinfo)
3045 | 'P' ->
3046 conf.savebmarks <- not conf.savebmarks;
3047 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3049 | 'S' ->
3050 let ondone s =
3052 let pageno, py =
3053 match state.layout with
3054 | [] -> 0, 0
3055 | l :: _ ->
3056 l.pageno, l.pagey
3058 conf.interpagespace <- int_of_string s;
3059 docolumns conf.columns;
3060 state.maxy <- calcheight ();
3061 let y = getpagey pageno in
3062 gotoy (y + py)
3063 with exc ->
3064 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3066 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3068 | 'l' ->
3069 let fm =
3070 match conf.fitmodel with
3071 | FitProportional -> FitWidth
3072 | _ -> FitProportional
3074 reqlayout conf.angle fm;
3075 TEdone ("proportional display " ^ btos (fm == FitProportional))
3077 | 'T' ->
3078 settrim (not conf.trimmargins) conf.trimfuzz;
3079 TEdone ("trim margins " ^ btos conf.trimmargins)
3081 | 'I' ->
3082 conf.invert <- not conf.invert;
3083 TEdone ("invert colors " ^ btos conf.invert)
3085 | 'x' ->
3086 let ondone s =
3087 cbput state.hists.sel s;
3088 conf.selcmd <- s;
3090 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3091 textentry, ondone, true)
3093 | _ ->
3094 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3095 TEstop
3096 else
3097 TEcont state.text
3100 class type lvsource = object
3101 method getitemcount : int
3102 method getitem : int -> (string * int)
3103 method hasaction : int -> bool
3104 method exit :
3105 uioh:uioh ->
3106 cancel:bool ->
3107 active:int ->
3108 first:int ->
3109 pan:int ->
3110 qsearch:string ->
3111 uioh option
3112 method getactive : int
3113 method getfirst : int
3114 method getqsearch : string
3115 method setqsearch : string -> unit
3116 method getpan : int
3117 end;;
3119 class virtual lvsourcebase = object
3120 val mutable m_active = 0
3121 val mutable m_first = 0
3122 val mutable m_qsearch = ""
3123 val mutable m_pan = 0
3124 method getactive = m_active
3125 method getfirst = m_first
3126 method getqsearch = m_qsearch
3127 method getpan = m_pan
3128 method setqsearch s = m_qsearch <- s
3129 end;;
3131 let withoutlastutf8 s =
3132 let len = String.length s in
3133 if len = 0
3134 then s
3135 else
3136 let rec find pos =
3137 if pos = 0
3138 then pos
3139 else
3140 let b = Char.code s.[pos] in
3141 if b land 0b11000000 = 0b11000000
3142 then pos
3143 else find (pos-1)
3145 let first =
3146 if Char.code s.[len-1] land 0x80 = 0
3147 then len-1
3148 else find (len-1)
3150 String.sub s 0 first;
3153 let textentrykeyboard
3154 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3155 let key =
3156 if key >= 0xffb0 && key <= 0xffb9
3157 then key - 0xffb0 + 48 else key
3159 let enttext te =
3160 state.mode <- Textentry (te, onleave);
3161 state.text <- "";
3162 enttext ();
3163 G.postRedisplay "textentrykeyboard enttext";
3165 let histaction cmd =
3166 match opthist with
3167 | None -> ()
3168 | Some (action, _) ->
3169 state.mode <- Textentry (
3170 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3172 G.postRedisplay "textentry histaction"
3174 match key with
3175 | 0xff08 -> (* backspace *)
3176 let s = withoutlastutf8 text in
3177 let len = String.length s in
3178 if cancelonempty && len = 0
3179 then (
3180 onleave Cancel;
3181 G.postRedisplay "textentrykeyboard after cancel";
3183 else (
3184 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3187 | 0xff0d | 0xff8d -> (* (kp) enter *)
3188 ondone text;
3189 onleave Confirm;
3190 G.postRedisplay "textentrykeyboard after confirm"
3192 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3193 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3194 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3195 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3197 | 0xff1b -> (* escape*)
3198 if String.length text = 0
3199 then (
3200 begin match opthist with
3201 | None -> ()
3202 | Some (_, onhistcancel) -> onhistcancel ()
3203 end;
3204 onleave Cancel;
3205 state.text <- "";
3206 G.postRedisplay "textentrykeyboard after cancel2"
3208 else (
3209 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3212 | 0xff9f | 0xffff -> () (* delete *)
3214 | _ when key != 0
3215 && key land 0xff00 != 0xff00 (* keyboard *)
3216 && key land 0xfe00 != 0xfe00 (* xkb *)
3217 && key land 0xfd00 != 0xfd00 (* 3270 *)
3219 begin match onkey text key with
3220 | TEdone text ->
3221 ondone text;
3222 onleave Confirm;
3223 G.postRedisplay "textentrykeyboard after confirm2";
3225 | TEcont text ->
3226 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3228 | TEstop ->
3229 onleave Cancel;
3230 G.postRedisplay "textentrykeyboard after cancel3"
3232 | TEswitch te ->
3233 state.mode <- Textentry (te, onleave);
3234 G.postRedisplay "textentrykeyboard switch";
3235 end;
3237 | _ ->
3238 vlog "unhandled key %s" (Wsi.keyname key)
3241 let firstof first active =
3242 if first > active || abs (first - active) > fstate.maxrows - 1
3243 then max 0 (active - (fstate.maxrows/2))
3244 else first
3247 let calcfirst first active =
3248 if active > first
3249 then
3250 let rows = active - first in
3251 if rows > fstate.maxrows then active - fstate.maxrows else first
3252 else active
3255 let scrollph y maxy =
3256 let sh = (float (maxy + state.winh) /. float state.winh) in
3257 let sh = float state.winh /. sh in
3258 let sh = max sh (float conf.scrollh) in
3260 let percent =
3261 if y = state.maxy
3262 then 1.0
3263 else float y /. float maxy
3265 let position = (float state.winh -. sh) *. percent in
3267 let position =
3268 if position +. sh > float state.winh
3269 then float state.winh -. sh
3270 else position
3272 position, sh;
3275 let coe s = (s :> uioh);;
3277 class listview ~(source:lvsource) ~trusted ~modehash =
3278 object (self)
3279 val m_pan = source#getpan
3280 val m_first = source#getfirst
3281 val m_active = source#getactive
3282 val m_qsearch = source#getqsearch
3283 val m_prev_uioh = state.uioh
3285 method private elemunder y =
3286 let n = y / (fstate.fontsize+1) in
3287 if m_first + n < source#getitemcount
3288 then (
3289 if source#hasaction (m_first + n)
3290 then Some (m_first + n)
3291 else None
3293 else None
3295 method display =
3296 Gl.enable `blend;
3297 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3298 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3299 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3300 GlDraw.color (1., 1., 1.);
3301 Gl.enable `texture_2d;
3302 let fs = fstate.fontsize in
3303 let nfs = fs + 1 in
3304 let ww = fstate.wwidth in
3305 let tabw = 30.0*.ww in
3306 let itemcount = source#getitemcount in
3307 let rec loop row =
3308 if (row - m_first) > fstate.maxrows
3309 then ()
3310 else (
3311 if row >= 0 && row < itemcount
3312 then (
3313 let (s, level) = source#getitem row in
3314 let y = (row - m_first) * nfs in
3315 let x = 5.0 +. float (level + m_pan) *. ww in
3316 if row = m_active
3317 then (
3318 Gl.disable `texture_2d;
3319 GlDraw.polygon_mode `both `line;
3320 let alpha = if source#hasaction row then 0.9 else 0.3 in
3321 GlDraw.color (1., 1., 1.) ~alpha;
3322 GlDraw.rect (1., float (y + 1))
3323 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3324 GlDraw.polygon_mode `both `fill;
3325 GlDraw.color (1., 1., 1.);
3326 Gl.enable `texture_2d;
3329 let drawtabularstring s =
3330 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3331 if trusted
3332 then
3333 let tabpos = try String.index s '\t' with Not_found -> -1 in
3334 if tabpos > 0
3335 then
3336 let len = String.length s - tabpos - 1 in
3337 let s1 = String.sub s 0 tabpos
3338 and s2 = String.sub s (tabpos + 1) len in
3339 let nx = drawstr x s1 in
3340 let sw = nx -. x in
3341 let x = x +. (max tabw sw) in
3342 drawstr x s2
3343 else
3344 drawstr x s
3345 else
3346 drawstr x s
3348 let _ = drawtabularstring s in
3349 loop (row+1)
3353 loop m_first;
3354 Gl.disable `blend;
3355 Gl.disable `texture_2d;
3357 method updownlevel incr =
3358 let len = source#getitemcount in
3359 let curlevel =
3360 if m_active >= 0 && m_active < len
3361 then snd (source#getitem m_active)
3362 else -1
3364 let rec flow i =
3365 if i = len then i-1 else if i = -1 then 0 else
3366 let _, l = source#getitem i in
3367 if l != curlevel then i else flow (i+incr)
3369 let active = flow m_active in
3370 let first = calcfirst m_first active in
3371 G.postRedisplay "outline updownlevel";
3372 {< m_active = active; m_first = first >}
3374 method private key1 key mask =
3375 let set1 active first qsearch =
3376 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3378 let search active pattern incr =
3379 let active = if active = -1 then m_first else active in
3380 let dosearch re =
3381 let rec loop n =
3382 if n >= 0 && n < source#getitemcount
3383 then (
3384 let s, _ = source#getitem n in
3386 (try ignore (Str.search_forward re s 0); true
3387 with Not_found -> false)
3388 then Some n
3389 else loop (n + incr)
3391 else None
3393 loop active
3396 let re = Str.regexp_case_fold pattern in
3397 dosearch re
3398 with Failure s ->
3399 state.text <- s;
3400 None
3402 let itemcount = source#getitemcount in
3403 let find start incr =
3404 let rec find i =
3405 if i = -1 || i = itemcount
3406 then -1
3407 else (
3408 if source#hasaction i
3409 then i
3410 else find (i + incr)
3413 find start
3415 let set active first =
3416 let first = bound first 0 (itemcount - fstate.maxrows) in
3417 state.text <- "";
3418 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3420 let navigate incr =
3421 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3422 let active, first =
3423 let incr1 = if incr > 0 then 1 else -1 in
3424 if isvisible m_first m_active
3425 then
3426 let next =
3427 let next = m_active + incr in
3428 let next =
3429 if next < 0 || next >= itemcount
3430 then -1
3431 else find next incr1
3433 if next = -1 || abs (m_active - next) > fstate.maxrows
3434 then -1
3435 else next
3437 if next = -1
3438 then
3439 let first = m_first + incr in
3440 let first = bound first 0 (itemcount - 1) in
3441 let next =
3442 let next = m_active + incr in
3443 let next = bound next 0 (itemcount - 1) in
3444 find next ~-incr1
3446 let active = if next = -1 then m_active else next in
3447 active, first
3448 else
3449 let first = min next m_first in
3450 let first =
3451 if abs (next - first) > fstate.maxrows
3452 then first + incr
3453 else first
3455 next, first
3456 else
3457 let first = m_first + incr in
3458 let first = bound first 0 (itemcount - 1) in
3459 let active =
3460 let next = m_active + incr in
3461 let next = bound next 0 (itemcount - 1) in
3462 let next = find next incr1 in
3463 let active =
3464 if next = -1 || abs (m_active - first) > fstate.maxrows
3465 then (
3466 let active = if m_active = -1 then next else m_active in
3467 active
3469 else next
3471 if isvisible first active
3472 then active
3473 else -1
3475 active, first
3477 G.postRedisplay "listview navigate";
3478 set active first;
3480 match key with
3481 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3482 let incr = if key = 0x72 then -1 else 1 in
3483 let active, first =
3484 match search (m_active + incr) m_qsearch incr with
3485 | None ->
3486 state.text <- m_qsearch ^ " [not found]";
3487 m_active, m_first
3488 | Some active ->
3489 state.text <- m_qsearch;
3490 active, firstof m_first active
3492 G.postRedisplay "listview ctrl-r/s";
3493 set1 active first m_qsearch;
3495 | 0xff08 -> (* backspace *)
3496 if String.length m_qsearch = 0
3497 then coe self
3498 else (
3499 let qsearch = withoutlastutf8 m_qsearch in
3500 let len = String.length qsearch in
3501 if len = 0
3502 then (
3503 state.text <- "";
3504 G.postRedisplay "listview empty qsearch";
3505 set1 m_active m_first "";
3507 else
3508 let active, first =
3509 match search m_active qsearch ~-1 with
3510 | None ->
3511 state.text <- qsearch ^ " [not found]";
3512 m_active, m_first
3513 | Some active ->
3514 state.text <- qsearch;
3515 active, firstof m_first active
3517 G.postRedisplay "listview backspace qsearch";
3518 set1 active first qsearch
3521 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3522 let pattern = m_qsearch ^ toutf8 key in
3523 let active, first =
3524 match search m_active pattern 1 with
3525 | None ->
3526 state.text <- pattern ^ " [not found]";
3527 m_active, m_first
3528 | Some active ->
3529 state.text <- pattern;
3530 active, firstof m_first active
3532 G.postRedisplay "listview qsearch add";
3533 set1 active first pattern;
3535 | 0xff1b -> (* escape *)
3536 state.text <- "";
3537 if String.length m_qsearch = 0
3538 then (
3539 G.postRedisplay "list view escape";
3540 begin
3541 match
3542 source#exit (coe self) true m_active m_first m_pan m_qsearch
3543 with
3544 | None -> m_prev_uioh
3545 | Some uioh -> uioh
3548 else (
3549 G.postRedisplay "list view kill qsearch";
3550 source#setqsearch "";
3551 coe {< m_qsearch = "" >}
3554 | 0xff0d | 0xff8d -> (* (kp) enter *)
3555 state.text <- "";
3556 let self = {< m_qsearch = "" >} in
3557 source#setqsearch "";
3558 let opt =
3559 G.postRedisplay "listview enter";
3560 if m_active >= 0 && m_active < source#getitemcount
3561 then (
3562 source#exit (coe self) false m_active m_first m_pan "";
3564 else (
3565 source#exit (coe self) true m_active m_first m_pan "";
3568 begin match opt with
3569 | None -> m_prev_uioh
3570 | Some uioh -> uioh
3573 | 0xff9f | 0xffff -> (* (kp) delete *)
3574 coe self
3576 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3577 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3578 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3579 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3581 | 0xff53 | 0xff98 -> (* (kp) right *)
3582 state.text <- "";
3583 G.postRedisplay "listview right";
3584 coe {< m_pan = m_pan - 1 >}
3586 | 0xff51 | 0xff96 -> (* (kp) left *)
3587 state.text <- "";
3588 G.postRedisplay "listview left";
3589 coe {< m_pan = m_pan + 1 >}
3591 | 0xff50 | 0xff95 -> (* (kp) home *)
3592 let active = find 0 1 in
3593 G.postRedisplay "listview home";
3594 set active 0;
3596 | 0xff57 | 0xff9c -> (* (kp) end *)
3597 let first = max 0 (itemcount - fstate.maxrows) in
3598 let active = find (itemcount - 1) ~-1 in
3599 G.postRedisplay "listview end";
3600 set active first;
3602 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3603 coe self
3605 | _ ->
3606 dolog "listview unknown key %#x" key; coe self
3608 method key key mask =
3609 match state.mode with
3610 | Textentry te -> textentrykeyboard key mask te; coe self
3611 | _ -> self#key1 key mask
3613 method button button down x y _ =
3614 let opt =
3615 match button with
3616 | 1 when x > state.winw - conf.scrollbw ->
3617 G.postRedisplay "listview scroll";
3618 if down
3619 then
3620 let _, position, sh = self#scrollph in
3621 if y > truncate position && y < truncate (position +. sh)
3622 then (
3623 state.mstate <- Mscrolly;
3624 Some (coe self)
3626 else
3627 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3628 let first = truncate (s *. float source#getitemcount) in
3629 let first = min source#getitemcount first in
3630 Some (coe {< m_first = first; m_active = first >})
3631 else (
3632 state.mstate <- Mnone;
3633 Some (coe self);
3635 | 1 when not down ->
3636 begin match self#elemunder y with
3637 | Some n ->
3638 G.postRedisplay "listview click";
3639 source#exit
3640 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3641 | _ ->
3642 Some (coe self)
3644 | n when (n == 4 || n == 5) && not down ->
3645 let len = source#getitemcount in
3646 let first =
3647 if n = 5 && m_first + fstate.maxrows >= len
3648 then
3649 m_first
3650 else
3651 let first = m_first + (if n == 4 then -1 else 1) in
3652 bound first 0 (len - 1)
3654 G.postRedisplay "listview wheel";
3655 Some (coe {< m_first = first >})
3656 | n when (n = 6 || n = 7) && not down ->
3657 let inc = m_first + (if n = 7 then -1 else 1) in
3658 G.postRedisplay "listview hwheel";
3659 Some (coe {< m_pan = m_pan + inc >})
3660 | _ ->
3661 Some (coe self)
3663 match opt with
3664 | None -> m_prev_uioh
3665 | Some uioh -> uioh
3667 method motion _ y =
3668 match state.mstate with
3669 | Mscrolly ->
3670 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3671 let first = truncate (s *. float source#getitemcount) in
3672 let first = min source#getitemcount first in
3673 G.postRedisplay "listview motion";
3674 coe {< m_first = first; m_active = first >}
3675 | _ -> coe self
3677 method pmotion x y =
3678 if x < state.winw - conf.scrollbw
3679 then
3680 let n =
3681 match self#elemunder y with
3682 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3683 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3685 let o =
3686 if n != m_active
3687 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3688 else self
3690 coe o
3691 else (
3692 Wsi.setcursor Wsi.CURSOR_INHERIT;
3693 coe self
3696 method infochanged _ = ()
3698 method scrollpw = (0, 0.0, 0.0)
3699 method scrollph =
3700 let nfs = fstate.fontsize + 1 in
3701 let y = m_first * nfs in
3702 let itemcount = source#getitemcount in
3703 let maxi = max 0 (itemcount - fstate.maxrows) in
3704 let maxy = maxi * nfs in
3705 let p, h = scrollph y maxy in
3706 conf.scrollbw, p, h
3708 method modehash = modehash
3709 method eformsgs = false
3710 end;;
3712 class outlinelistview ~source =
3713 object (self)
3714 inherit listview
3715 ~source:(source :> lvsource)
3716 ~trusted:false
3717 ~modehash:(findkeyhash conf "outline")
3718 as super
3720 method key key mask =
3721 let calcfirst first active =
3722 if active > first
3723 then
3724 let rows = active - first in
3725 let maxrows =
3726 if String.length state.text = 0
3727 then fstate.maxrows
3728 else fstate.maxrows - 2
3730 if rows > maxrows then active - maxrows else first
3731 else active
3733 let navigate incr =
3734 let active = m_active + incr in
3735 let active = bound active 0 (source#getitemcount - 1) in
3736 let first = calcfirst m_first active in
3737 G.postRedisplay "outline navigate";
3738 coe {< m_active = active; m_first = first >}
3740 let ctrl = Wsi.withctrl mask in
3741 match key with
3742 | 110 when ctrl -> (* ctrl-n *)
3743 source#narrow m_qsearch;
3744 G.postRedisplay "outline ctrl-n";
3745 coe {< m_first = 0; m_active = 0 >}
3747 | 117 when ctrl -> (* ctrl-u *)
3748 source#denarrow;
3749 G.postRedisplay "outline ctrl-u";
3750 state.text <- "";
3751 coe {< m_first = 0; m_active = 0 >}
3753 | 108 when ctrl -> (* ctrl-l *)
3754 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3755 G.postRedisplay "outline ctrl-l";
3756 coe {< m_first = first >}
3758 | 0xff9f | 0xffff -> (* (kp) delete *)
3759 source#remove m_active;
3760 G.postRedisplay "outline delete";
3761 let active = max 0 (m_active-1) in
3762 coe {< m_first = firstof m_first active;
3763 m_active = active >}
3765 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3766 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3767 | 0xff55 | 0xff9a -> (* (kp) prior *)
3768 navigate ~-(fstate.maxrows)
3769 | 0xff56 | 0xff9b -> (* (kp) next *)
3770 navigate fstate.maxrows
3772 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3773 let o =
3774 if ctrl
3775 then (
3776 G.postRedisplay "outline ctrl right";
3777 {< m_pan = m_pan + 1 >}
3779 else self#updownlevel 1
3781 coe o
3783 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3784 let o =
3785 if ctrl
3786 then (
3787 G.postRedisplay "outline ctrl left";
3788 {< m_pan = m_pan - 1 >}
3790 else self#updownlevel ~-1
3792 coe o
3794 | 0xff50 | 0xff95 -> (* (kp) home *)
3795 G.postRedisplay "outline home";
3796 coe {< m_first = 0; m_active = 0 >}
3798 | 0xff57 | 0xff9c -> (* (kp) end *)
3799 let active = source#getitemcount - 1 in
3800 let first = max 0 (active - fstate.maxrows) in
3801 G.postRedisplay "outline end";
3802 coe {< m_active = active; m_first = first >}
3804 | _ -> super#key key mask
3807 let outlinesource usebookmarks =
3808 let empty = [||] in
3809 (object
3810 inherit lvsourcebase
3811 val mutable m_items = empty
3812 val mutable m_orig_items = empty
3813 val mutable m_prev_items = empty
3814 val mutable m_narrow_pattern = ""
3815 val mutable m_hadremovals = false
3817 method getitemcount =
3818 Array.length m_items + (if m_hadremovals then 1 else 0)
3820 method getitem n =
3821 if n == Array.length m_items && m_hadremovals
3822 then
3823 ("[Confirm removal]", 0)
3824 else
3825 let s, n, _ = m_items.(n) in
3826 (s, n)
3828 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3829 ignore (uioh, first, qsearch);
3830 let confrimremoval = m_hadremovals && active = Array.length m_items in
3831 let items =
3832 if String.length m_narrow_pattern = 0
3833 then m_orig_items
3834 else m_items
3836 if not cancel
3837 then (
3838 if not confrimremoval
3839 then(
3840 let _, _, anchor = m_items.(active) in
3841 gotoghyll (getanchory anchor);
3842 m_items <- items;
3844 else (
3845 state.bookmarks <- Array.to_list m_items;
3846 m_orig_items <- m_items;
3849 else m_items <- items;
3850 m_pan <- pan;
3851 None
3853 method hasaction _ = true
3855 method greetmsg =
3856 if Array.length m_items != Array.length m_orig_items
3857 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3858 else ""
3860 method narrow pattern =
3861 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3862 match reopt with
3863 | None -> ()
3864 | Some re ->
3865 let rec loop accu n =
3866 if n = -1
3867 then (
3868 m_narrow_pattern <- pattern;
3869 m_items <- Array.of_list accu
3871 else
3872 let (s, _, _) as o = m_items.(n) in
3873 let accu =
3874 if (try ignore (Str.search_forward re s 0); true
3875 with Not_found -> false)
3876 then o :: accu
3877 else accu
3879 loop accu (n-1)
3881 loop [] (Array.length m_items - 1)
3883 method denarrow =
3884 m_orig_items <- (
3885 if usebookmarks
3886 then Array.of_list state.bookmarks
3887 else state.outlines
3889 m_items <- m_orig_items
3891 method remove m =
3892 if usebookmarks
3893 then
3894 if m >= 0 && m < Array.length m_items
3895 then (
3896 m_hadremovals <- true;
3897 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3898 let n = if n >= m then n+1 else n in
3899 m_items.(n)
3903 method reset anchor items =
3904 m_hadremovals <- false;
3905 if m_orig_items == empty || m_prev_items != items
3906 then (
3907 m_orig_items <- items;
3908 if String.length m_narrow_pattern = 0
3909 then m_items <- items;
3911 m_prev_items <- items;
3912 let rely = getanchory anchor in
3913 let active =
3914 let rec loop n best bestd =
3915 if n = Array.length m_items
3916 then best
3917 else
3918 let (_, _, anchor) = m_items.(n) in
3919 let orely = getanchory anchor in
3920 let d = abs (orely - rely) in
3921 if d < bestd
3922 then loop (n+1) n d
3923 else loop (n+1) best bestd
3925 loop 0 ~-1 max_int
3927 m_active <- active;
3928 m_first <- firstof m_first active
3929 end)
3932 let enterselector usebookmarks =
3933 let source = outlinesource usebookmarks in
3934 fun errmsg ->
3935 let outlines =
3936 if usebookmarks
3937 then Array.of_list state.bookmarks
3938 else state.outlines
3940 if Array.length outlines = 0
3941 then (
3942 showtext ' ' errmsg;
3944 else (
3945 state.text <- source#greetmsg;
3946 Wsi.setcursor Wsi.CURSOR_INHERIT;
3947 let anchor = getanchor () in
3948 source#reset anchor outlines;
3949 state.uioh <- coe (new outlinelistview ~source);
3950 G.postRedisplay "enter selector";
3954 let enteroutlinemode =
3955 let f = enterselector false in
3956 fun ()-> f "Document has no outline";
3959 let enterbookmarkmode =
3960 let f = enterselector true in
3961 fun () -> f "Document has no bookmarks (yet)";
3964 let color_of_string s =
3965 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3966 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3970 let color_to_string (r, g, b) =
3971 let r = truncate (r *. 256.0)
3972 and g = truncate (g *. 256.0)
3973 and b = truncate (b *. 256.0) in
3974 Printf.sprintf "%d/%d/%d" r g b
3977 let irect_of_string s =
3978 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3981 let irect_to_string (x0,y0,x1,y1) =
3982 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3985 let makecheckers () =
3986 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3987 following to say:
3988 converted by Issac Trotts. July 25, 2002 *)
3989 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3990 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3991 let id = GlTex.gen_texture () in
3992 GlTex.bind_texture `texture_2d id;
3993 GlPix.store (`unpack_alignment 1);
3994 GlTex.image2d image;
3995 List.iter (GlTex.parameter ~target:`texture_2d)
3996 [ `mag_filter `nearest; `min_filter `nearest ];
4000 let setcheckers enabled =
4001 match state.texid with
4002 | None ->
4003 if enabled then state.texid <- Some (makecheckers ())
4005 | Some texid ->
4006 if not enabled
4007 then (
4008 GlTex.delete_texture texid;
4009 state.texid <- None;
4013 let int_of_string_with_suffix s =
4014 let l = String.length s in
4015 let s1, shift =
4016 if l > 1
4017 then
4018 let suffix = Char.lowercase s.[l-1] in
4019 match suffix with
4020 | 'k' -> String.sub s 0 (l-1), 10
4021 | 'm' -> String.sub s 0 (l-1), 20
4022 | 'g' -> String.sub s 0 (l-1), 30
4023 | _ -> s, 0
4024 else s, 0
4026 let n = int_of_string s1 in
4027 let m = n lsl shift in
4028 if m < 0 || m < n
4029 then raise (Failure "value too large")
4030 else m
4033 let string_with_suffix_of_int n =
4034 if n = 0
4035 then "0"
4036 else
4037 let n, s =
4038 if n land ((1 lsl 30) - 1) = 0
4039 then n lsr 30, "G"
4040 else (
4041 if n land ((1 lsl 20) - 1) = 0
4042 then n lsr 20, "M"
4043 else (
4044 if n land ((1 lsl 10) - 1) = 0
4045 then n lsr 10, "K"
4046 else n, ""
4050 let rec loop s n =
4051 let h = n mod 1000 in
4052 let n = n / 1000 in
4053 if n = 0
4054 then string_of_int h ^ s
4055 else (
4056 let s = Printf.sprintf "_%03d%s" h s in
4057 loop s n
4060 loop "" n ^ s;
4063 let defghyllscroll = (40, 8, 32);;
4064 let ghyllscroll_of_string s =
4065 let (n, a, b) as nab =
4066 if s = "default"
4067 then defghyllscroll
4068 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4070 if n <= a || n <= b || a >= b
4071 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4072 nab;
4075 let ghyllscroll_to_string ((n, a, b) as nab) =
4076 if nab = defghyllscroll
4077 then "default"
4078 else Printf.sprintf "%d,%d,%d" n a b;
4081 let describe_location () =
4082 let fn = page_of_y state.y in
4083 let ln = page_of_y (state.y + state.winh - state.hscrollh - 1) in
4084 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4085 let percent =
4086 if maxy <= 0
4087 then 100.
4088 else (100. *. (float state.y /. float maxy))
4090 if fn = ln
4091 then
4092 Printf.sprintf "page %d of %d [%.2f%%]"
4093 (fn+1) state.pagecount percent
4094 else
4095 Printf.sprintf
4096 "pages %d-%d of %d [%.2f%%]"
4097 (fn+1) (ln+1) state.pagecount percent
4100 let setpresentationmode v =
4101 let n = page_of_y state.y in
4102 state.anchor <- (n, 0.0, 1.0);
4103 conf.presentation <- v;
4104 if conf.presentation
4105 then (
4106 if not conf.scrollbarinpm
4107 then state.scrollw <- 0;
4109 else state.scrollw <- conf.scrollbw;
4110 represent ();
4113 let enterinfomode =
4114 let btos b = if b then "\xe2\x88\x9a" else "" in
4115 let showextended = ref false in
4116 let leave mode = function
4117 | Confirm -> state.mode <- mode
4118 | Cancel -> state.mode <- mode in
4119 let src =
4120 (object
4121 val mutable m_first_time = true
4122 val mutable m_l = []
4123 val mutable m_a = [||]
4124 val mutable m_prev_uioh = nouioh
4125 val mutable m_prev_mode = View
4127 inherit lvsourcebase
4129 method reset prev_mode prev_uioh =
4130 m_a <- Array.of_list (List.rev m_l);
4131 m_l <- [];
4132 m_prev_mode <- prev_mode;
4133 m_prev_uioh <- prev_uioh;
4134 if m_first_time
4135 then (
4136 let rec loop n =
4137 if n >= Array.length m_a
4138 then ()
4139 else
4140 match m_a.(n) with
4141 | _, _, _, Action _ -> m_active <- n
4142 | _ -> loop (n+1)
4144 loop 0;
4145 m_first_time <- false;
4148 method int name get set =
4149 m_l <-
4150 (name, `int get, 1, Action (
4151 fun u ->
4152 let ondone s =
4153 try set (int_of_string s)
4154 with exn ->
4155 state.text <- Printf.sprintf "bad integer `%s': %s"
4156 s (exntos exn)
4158 state.text <- "";
4159 let te = name ^ ": ", "", None, intentry, ondone, true in
4160 state.mode <- Textentry (te, leave m_prev_mode);
4162 )) :: m_l
4164 method int_with_suffix name get set =
4165 m_l <-
4166 (name, `intws get, 1, Action (
4167 fun u ->
4168 let ondone s =
4169 try set (int_of_string_with_suffix s)
4170 with exn ->
4171 state.text <- Printf.sprintf "bad integer `%s': %s"
4172 s (exntos exn)
4174 state.text <- "";
4175 let te =
4176 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4178 state.mode <- Textentry (te, leave m_prev_mode);
4180 )) :: m_l
4182 method bool ?(offset=1) ?(btos=btos) name get set =
4183 m_l <-
4184 (name, `bool (btos, get), offset, Action (
4185 fun u ->
4186 let v = get () in
4187 set (not v);
4189 )) :: m_l
4191 method color name get set =
4192 m_l <-
4193 (name, `color get, 1, Action (
4194 fun u ->
4195 let invalid = (nan, nan, nan) in
4196 let ondone s =
4197 let c =
4198 try color_of_string s
4199 with exn ->
4200 state.text <- Printf.sprintf "bad color `%s': %s"
4201 s (exntos exn);
4202 invalid
4204 if c <> invalid
4205 then set c;
4207 let te = name ^ ": ", "", None, textentry, ondone, true in
4208 state.text <- color_to_string (get ());
4209 state.mode <- Textentry (te, leave m_prev_mode);
4211 )) :: m_l
4213 method string name get set =
4214 m_l <-
4215 (name, `string get, 1, Action (
4216 fun u ->
4217 let ondone s = set s in
4218 let te = name ^ ": ", "", None, textentry, ondone, true in
4219 state.mode <- Textentry (te, leave m_prev_mode);
4221 )) :: m_l
4223 method colorspace name get set =
4224 m_l <-
4225 (name, `string get, 1, Action (
4226 fun _ ->
4227 let source =
4228 let vals = [| "rgb"; "bgr"; "gray" |] in
4229 (object
4230 inherit lvsourcebase
4232 initializer
4233 m_active <- int_of_colorspace conf.colorspace;
4234 m_first <- 0;
4236 method getitemcount = Array.length vals
4237 method getitem n = (vals.(n), 0)
4238 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4239 ignore (uioh, first, pan, qsearch);
4240 if not cancel then set active;
4241 None
4242 method hasaction _ = true
4243 end)
4245 state.text <- "";
4246 let modehash = findkeyhash conf "info" in
4247 coe (new listview ~source ~trusted:true ~modehash)
4248 )) :: m_l
4250 method fitmodel name get set =
4251 m_l <-
4252 (name, `string get, 1, Action (
4253 fun _ ->
4254 let source =
4255 let vals = [| "fit width"; "proportional"; "fit page" |] in
4256 (object
4257 inherit lvsourcebase
4259 initializer
4260 m_active <- int_of_fitmodel conf.fitmodel;
4261 m_first <- 0;
4263 method getitemcount = Array.length vals
4264 method getitem n = (vals.(n), 0)
4265 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4266 ignore (uioh, first, pan, qsearch);
4267 if not cancel then set active;
4268 None
4269 method hasaction _ = true
4270 end)
4272 state.text <- "";
4273 let modehash = findkeyhash conf "info" in
4274 coe (new listview ~source ~trusted:true ~modehash)
4275 )) :: m_l
4277 method caption s offset =
4278 m_l <- (s, `empty, offset, Noaction) :: m_l
4280 method caption2 s f offset =
4281 m_l <- (s, `string f, offset, Noaction) :: m_l
4283 method getitemcount = Array.length m_a
4285 method getitem n =
4286 let tostr = function
4287 | `int f -> string_of_int (f ())
4288 | `intws f -> string_with_suffix_of_int (f ())
4289 | `string f -> f ()
4290 | `color f -> color_to_string (f ())
4291 | `bool (btos, f) -> btos (f ())
4292 | `empty -> ""
4294 let name, t, offset, _ = m_a.(n) in
4295 ((let s = tostr t in
4296 if String.length s > 0
4297 then Printf.sprintf "%s\t%s" name s
4298 else name),
4299 offset)
4301 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4302 let uiohopt =
4303 if not cancel
4304 then (
4305 m_qsearch <- qsearch;
4306 let uioh =
4307 match m_a.(active) with
4308 | _, _, _, Action f -> f uioh
4309 | _ -> uioh
4311 Some uioh
4313 else None
4315 m_active <- active;
4316 m_first <- first;
4317 m_pan <- pan;
4318 uiohopt
4320 method hasaction n =
4321 match m_a.(n) with
4322 | _, _, _, Action _ -> true
4323 | _ -> false
4324 end)
4326 let rec fillsrc prevmode prevuioh =
4327 let sep () = src#caption "" 0 in
4328 let colorp name get set =
4329 src#string name
4330 (fun () -> color_to_string (get ()))
4331 (fun v ->
4333 let c = color_of_string v in
4334 set c
4335 with exn ->
4336 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4339 let oldmode = state.mode in
4340 let birdseye = isbirdseye state.mode in
4342 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4344 src#bool "presentation mode"
4345 (fun () -> conf.presentation)
4346 (fun v -> setpresentationmode v);
4348 src#bool "ignore case in searches"
4349 (fun () -> conf.icase)
4350 (fun v -> conf.icase <- v);
4352 src#bool "preload"
4353 (fun () -> conf.preload)
4354 (fun v -> conf.preload <- v);
4356 src#bool "highlight links"
4357 (fun () -> conf.hlinks)
4358 (fun v -> conf.hlinks <- v);
4360 src#bool "under info"
4361 (fun () -> conf.underinfo)
4362 (fun v -> conf.underinfo <- v);
4364 src#bool "persistent bookmarks"
4365 (fun () -> conf.savebmarks)
4366 (fun v -> conf.savebmarks <- v);
4368 src#fitmodel "fit model"
4369 (fun () -> fitmodel_to_string conf.fitmodel)
4370 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4372 src#bool "trim margins"
4373 (fun () -> conf.trimmargins)
4374 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4376 src#bool "persistent location"
4377 (fun () -> conf.jumpback)
4378 (fun v -> conf.jumpback <- v);
4380 sep ();
4381 src#int "inter-page space"
4382 (fun () -> conf.interpagespace)
4383 (fun n ->
4384 conf.interpagespace <- n;
4385 docolumns conf.columns;
4386 let pageno, py =
4387 match state.layout with
4388 | [] -> 0, 0
4389 | l :: _ ->
4390 l.pageno, l.pagey
4392 state.maxy <- calcheight ();
4393 let y = getpagey pageno in
4394 gotoy (y + py)
4397 src#int "page bias"
4398 (fun () -> conf.pagebias)
4399 (fun v -> conf.pagebias <- v);
4401 src#int "scroll step"
4402 (fun () -> conf.scrollstep)
4403 (fun n -> conf.scrollstep <- n);
4405 src#int "horizontal scroll step"
4406 (fun () -> conf.hscrollstep)
4407 (fun v -> conf.hscrollstep <- v);
4409 src#int "auto scroll step"
4410 (fun () ->
4411 match state.autoscroll with
4412 | Some step -> step
4413 | _ -> conf.autoscrollstep)
4414 (fun n ->
4415 if state.autoscroll <> None
4416 then state.autoscroll <- Some n;
4417 conf.autoscrollstep <- n);
4419 src#int "zoom"
4420 (fun () -> truncate (conf.zoom *. 100.))
4421 (fun v -> setzoom ((float v) /. 100.));
4423 src#int "rotation"
4424 (fun () -> conf.angle)
4425 (fun v -> reqlayout v conf.fitmodel);
4427 src#int "scroll bar width"
4428 (fun () -> state.scrollw)
4429 (fun v ->
4430 state.scrollw <- v;
4431 conf.scrollbw <- v;
4432 reshape state.winw state.winh;
4435 src#int "scroll handle height"
4436 (fun () -> conf.scrollh)
4437 (fun v -> conf.scrollh <- v;);
4439 src#int "thumbnail width"
4440 (fun () -> conf.thumbw)
4441 (fun v ->
4442 conf.thumbw <- min 4096 v;
4443 match oldmode with
4444 | Birdseye beye ->
4445 leavebirdseye beye false;
4446 enterbirdseye ()
4447 | _ -> ()
4450 let mode = state.mode in
4451 src#string "columns"
4452 (fun () ->
4453 match conf.columns with
4454 | Csingle _ -> "1"
4455 | Cmulti (multi, _) -> multicolumns_to_string multi
4456 | Csplit (count, _) -> "-" ^ string_of_int count
4458 (fun v ->
4459 let n, a, b = multicolumns_of_string v in
4460 setcolumns mode n a b);
4462 sep ();
4463 src#caption "Presentation mode" 0;
4464 src#bool "scrollbar visible"
4465 (fun () -> conf.scrollbarinpm)
4466 (fun v ->
4467 if v != conf.scrollbarinpm
4468 then (
4469 conf.scrollbarinpm <- v;
4470 if conf.presentation
4471 then (
4472 state.scrollw <- if v then conf.scrollbw else 0;
4473 reshape state.winw state.winh;
4478 sep ();
4479 src#caption "Pixmap cache" 0;
4480 src#int_with_suffix "size (advisory)"
4481 (fun () -> conf.memlimit)
4482 (fun v -> conf.memlimit <- v);
4484 src#caption2 "used"
4485 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4486 (string_with_suffix_of_int state.memused)
4487 (Hashtbl.length state.tilemap)) 1;
4489 sep ();
4490 src#caption "Layout" 0;
4491 src#caption2 "Dimension"
4492 (fun () ->
4493 Printf.sprintf "%dx%d (virtual %dx%d)"
4494 state.winw state.winh
4495 state.w state.maxy)
4497 if conf.debug
4498 then
4499 src#caption2 "Position" (fun () ->
4500 Printf.sprintf "%dx%d" state.x state.y
4502 else
4503 src#caption2 "Position" (fun () -> describe_location ()) 1
4506 sep ();
4507 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4508 "Save these parameters as global defaults at exit"
4509 (fun () -> conf.bedefault)
4510 (fun v -> conf.bedefault <- v)
4513 sep ();
4514 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4515 src#bool ~offset:0 ~btos "Extended parameters"
4516 (fun () -> !showextended)
4517 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4518 if !showextended
4519 then (
4520 src#bool "checkers"
4521 (fun () -> conf.checkers)
4522 (fun v -> conf.checkers <- v; setcheckers v);
4523 src#bool "update cursor"
4524 (fun () -> conf.updatecurs)
4525 (fun v -> conf.updatecurs <- v);
4526 src#bool "verbose"
4527 (fun () -> conf.verbose)
4528 (fun v -> conf.verbose <- v);
4529 src#bool "invert colors"
4530 (fun () -> conf.invert)
4531 (fun v -> conf.invert <- v);
4532 src#bool "max fit"
4533 (fun () -> conf.maxhfit)
4534 (fun v -> conf.maxhfit <- v);
4535 src#bool "redirect stderr"
4536 (fun () -> conf.redirectstderr)
4537 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4538 src#string "uri launcher"
4539 (fun () -> conf.urilauncher)
4540 (fun v -> conf.urilauncher <- v);
4541 src#string "path launcher"
4542 (fun () -> conf.pathlauncher)
4543 (fun v -> conf.pathlauncher <- v);
4544 src#string "tile size"
4545 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4546 (fun v ->
4548 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4549 conf.tilew <- max 64 w;
4550 conf.tileh <- max 64 h;
4551 flushtiles ();
4552 with exn ->
4553 state.text <- Printf.sprintf "bad tile size `%s': %s"
4554 v (exntos exn)
4556 src#int "texture count"
4557 (fun () -> conf.texcount)
4558 (fun v ->
4559 if realloctexts v
4560 then conf.texcount <- v
4561 else showtext '!' " Failed to set texture count please retry later"
4563 src#int "slice height"
4564 (fun () -> conf.sliceheight)
4565 (fun v ->
4566 conf.sliceheight <- v;
4567 wcmd "sliceh %d" conf.sliceheight;
4569 src#int "anti-aliasing level"
4570 (fun () -> conf.aalevel)
4571 (fun v ->
4572 conf.aalevel <- bound v 0 8;
4573 state.anchor <- getanchor ();
4574 opendoc state.path state.password;
4576 src#string "page scroll scaling factor"
4577 (fun () -> string_of_float conf.pgscale)
4578 (fun v ->
4580 let s = float_of_string v in
4581 conf.pgscale <- s
4582 with exn ->
4583 state.text <- Printf.sprintf
4584 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4587 src#int "ui font size"
4588 (fun () -> fstate.fontsize)
4589 (fun v -> setfontsize (bound v 5 100));
4590 src#int "hint font size"
4591 (fun () -> conf.hfsize)
4592 (fun v -> conf.hfsize <- bound v 5 100);
4593 colorp "background color"
4594 (fun () -> conf.bgcolor)
4595 (fun v -> conf.bgcolor <- v);
4596 src#bool "crop hack"
4597 (fun () -> conf.crophack)
4598 (fun v -> conf.crophack <- v);
4599 src#string "trim fuzz"
4600 (fun () -> irect_to_string conf.trimfuzz)
4601 (fun v ->
4603 conf.trimfuzz <- irect_of_string v;
4604 if conf.trimmargins
4605 then settrim true conf.trimfuzz;
4606 with exn ->
4607 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4609 src#string "throttle"
4610 (fun () ->
4611 match conf.maxwait with
4612 | None -> "show place holder if page is not ready"
4613 | Some time ->
4614 if time = infinity
4615 then "wait for page to fully render"
4616 else
4617 "wait " ^ string_of_float time
4618 ^ " seconds before showing placeholder"
4620 (fun v ->
4622 let f = float_of_string v in
4623 if f <= 0.0
4624 then conf.maxwait <- None
4625 else conf.maxwait <- Some f
4626 with exn ->
4627 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4629 src#string "ghyll scroll"
4630 (fun () ->
4631 match conf.ghyllscroll with
4632 | None -> ""
4633 | Some nab -> ghyllscroll_to_string nab
4635 (fun v ->
4637 let gs =
4638 if String.length v = 0
4639 then None
4640 else Some (ghyllscroll_of_string v)
4642 conf.ghyllscroll <- gs
4643 with exn ->
4644 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4646 src#string "selection command"
4647 (fun () -> conf.selcmd)
4648 (fun v -> conf.selcmd <- v);
4649 src#string "synctex command"
4650 (fun () -> conf.stcmd)
4651 (fun v -> conf.stcmd <- v);
4652 src#colorspace "color space"
4653 (fun () -> colorspace_to_string conf.colorspace)
4654 (fun v ->
4655 conf.colorspace <- colorspace_of_int v;
4656 wcmd "cs %d" v;
4657 load state.layout;
4659 if pbousable ()
4660 then
4661 src#bool "use PBO"
4662 (fun () -> conf.usepbo)
4663 (fun v -> conf.usepbo <- v);
4664 src#bool "mouse wheel scrolls pages"
4665 (fun () -> conf.wheelbypage)
4666 (fun v -> conf.wheelbypage <- v);
4669 sep ();
4670 src#caption "Document" 0;
4671 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4672 src#caption ("Path\t" ^ state.path) 1;
4673 if String.length state.origin > 0
4674 then src#caption ("Orign\t" ^ state.origin) 1;
4675 src#caption2 "Pages"
4676 (fun () -> string_of_int state.pagecount) 1;
4677 src#caption2 "Dimensions"
4678 (fun () -> string_of_int (List.length state.pdims)) 1;
4679 if conf.trimmargins
4680 then (
4681 sep ();
4682 src#caption "Trimmed margins" 0;
4683 src#caption2 "Dimensions"
4684 (fun () -> string_of_int (List.length state.pdims)) 1;
4687 sep ();
4688 src#caption "OpenGL" 0;
4689 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4690 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4691 src#reset prevmode prevuioh;
4693 fun () ->
4694 state.text <- "";
4695 let prevmode = state.mode
4696 and prevuioh = state.uioh in
4697 fillsrc prevmode prevuioh;
4698 let source = (src :> lvsource) in
4699 let modehash = findkeyhash conf "info" in
4700 state.uioh <- coe (object (self)
4701 inherit listview ~source ~trusted:true ~modehash as super
4702 val mutable m_prevmemused = 0
4703 method infochanged = function
4704 | Memused ->
4705 if m_prevmemused != state.memused
4706 then (
4707 m_prevmemused <- state.memused;
4708 G.postRedisplay "memusedchanged";
4710 | Pdim -> G.postRedisplay "pdimchanged"
4711 | Docinfo -> fillsrc prevmode prevuioh
4713 method key key mask =
4714 if not (Wsi.withctrl mask)
4715 then
4716 match key with
4717 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4718 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4719 | _ -> super#key key mask
4720 else super#key key mask
4721 end);
4722 G.postRedisplay "info";
4725 let enterhelpmode =
4726 let source =
4727 (object
4728 inherit lvsourcebase
4729 method getitemcount = Array.length state.help
4730 method getitem n =
4731 let s, l, _ = state.help.(n) in
4732 (s, l)
4734 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4735 let optuioh =
4736 if not cancel
4737 then (
4738 m_qsearch <- qsearch;
4739 match state.help.(active) with
4740 | _, _, Action f -> Some (f uioh)
4741 | _ -> Some (uioh)
4743 else None
4745 m_active <- active;
4746 m_first <- first;
4747 m_pan <- pan;
4748 optuioh
4750 method hasaction n =
4751 match state.help.(n) with
4752 | _, _, Action _ -> true
4753 | _ -> false
4755 initializer
4756 m_active <- -1
4757 end)
4758 in fun () ->
4759 let modehash = findkeyhash conf "help" in
4760 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4761 G.postRedisplay "help";
4764 let entermsgsmode =
4765 let msgsource =
4766 let re = Str.regexp "[\r\n]" in
4767 (object
4768 inherit lvsourcebase
4769 val mutable m_items = [||]
4771 method getitemcount = 1 + Array.length m_items
4773 method getitem n =
4774 if n = 0
4775 then "[Clear]", 0
4776 else m_items.(n-1), 0
4778 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4779 ignore uioh;
4780 if not cancel
4781 then (
4782 if active = 0
4783 then Buffer.clear state.errmsgs;
4784 m_qsearch <- qsearch;
4786 m_active <- active;
4787 m_first <- first;
4788 m_pan <- pan;
4789 None
4791 method hasaction n =
4792 n = 0
4794 method reset =
4795 state.newerrmsgs <- false;
4796 let l = Str.split re (Buffer.contents state.errmsgs) in
4797 m_items <- Array.of_list l
4799 initializer
4800 m_active <- 0
4801 end)
4802 in fun () ->
4803 state.text <- "";
4804 msgsource#reset;
4805 let source = (msgsource :> lvsource) in
4806 let modehash = findkeyhash conf "listview" in
4807 state.uioh <- coe (object
4808 inherit listview ~source ~trusted:false ~modehash as super
4809 method display =
4810 if state.newerrmsgs
4811 then msgsource#reset;
4812 super#display
4813 end);
4814 G.postRedisplay "msgs";
4817 let quickbookmark ?title () =
4818 match state.layout with
4819 | [] -> ()
4820 | l :: _ ->
4821 let title =
4822 match title with
4823 | None ->
4824 let sec = Unix.gettimeofday () in
4825 let tm = Unix.localtime sec in
4826 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4827 (l.pageno+1)
4828 tm.Unix.tm_mday
4829 tm.Unix.tm_mon
4830 (tm.Unix.tm_year + 1900)
4831 tm.Unix.tm_hour
4832 tm.Unix.tm_min
4833 | Some title -> title
4835 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4838 let setautoscrollspeed step goingdown =
4839 let incr = max 1 ((abs step) / 2) in
4840 let incr = if goingdown then incr else -incr in
4841 let astep = step + incr in
4842 state.autoscroll <- Some astep;
4845 let gotounder = function
4846 | Ulinkgoto (pageno, top) ->
4847 if pageno >= 0
4848 then (
4849 addnav ();
4850 gotopage1 pageno top;
4853 | Ulinkuri s ->
4854 gotouri s
4856 | Uremote (filename, pageno) ->
4857 let path =
4858 if Sys.file_exists filename
4859 then filename
4860 else
4861 let dir = Filename.dirname state.path in
4862 let path = Filename.concat dir filename in
4863 if Sys.file_exists path
4864 then path
4865 else ""
4867 if String.length path > 0
4868 then (
4869 let anchor = getanchor () in
4870 let ranchor = state.path, state.password, anchor, state.origin in
4871 state.origin <- "";
4872 state.anchor <- (pageno, 0.0, 0.0);
4873 state.ranchors <- ranchor :: state.ranchors;
4874 opendoc path "";
4876 else showtext '!' ("Could not find " ^ filename)
4878 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4881 let canpan () =
4882 match conf.columns with
4883 | Csplit _ -> true
4884 | _ -> state.x != 0 || conf.zoom > 1.0
4887 let existsinrow pageno (columns, coverA, coverB) p =
4888 let last = ((pageno - coverA) mod columns) + columns in
4889 let rec any = function
4890 | [] -> false
4891 | l :: rest ->
4892 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4893 then p l
4894 else (
4895 if not (p l)
4896 then (if l.pageno = last then false else any rest)
4897 else true
4900 any state.layout
4903 let nextpage () =
4904 match state.layout with
4905 | [] ->
4906 let pageno = page_of_y state.y in
4907 gotoghyll (getpagey (pageno+1))
4908 | l :: rest ->
4909 match conf.columns with
4910 | Csingle _ ->
4911 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4912 then
4913 let y = clamp (pgscale state.winh) in
4914 gotoghyll y
4915 else
4916 let pageno = min (l.pageno+1) (state.pagecount-1) in
4917 gotoghyll (getpagey pageno)
4918 | Cmulti ((c, _, _) as cl, _) ->
4919 if conf.presentation
4920 && (existsinrow l.pageno cl
4921 (fun l -> l.pageh > l.pagey + l.pagevh))
4922 then
4923 let y = clamp (pgscale state.winh) in
4924 gotoghyll y
4925 else
4926 let pageno = min (l.pageno+c) (state.pagecount-1) in
4927 gotoghyll (getpagey pageno)
4928 | Csplit (n, _) ->
4929 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4930 then
4931 let pagey, pageh = getpageyh l.pageno in
4932 let pagey = pagey + pageh * l.pagecol in
4933 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4934 gotoghyll (pagey + pageh + ips)
4937 let prevpage () =
4938 match state.layout with
4939 | [] ->
4940 let pageno = page_of_y state.y in
4941 gotoghyll (getpagey (pageno-1))
4942 | l :: _ ->
4943 match conf.columns with
4944 | Csingle _ ->
4945 if conf.presentation && l.pagey != 0
4946 then
4947 gotoghyll (clamp (pgscale ~-(state.winh)))
4948 else
4949 let pageno = max 0 (l.pageno-1) in
4950 gotoghyll (getpagey pageno)
4951 | Cmulti ((c, _, coverB) as cl, _) ->
4952 if conf.presentation &&
4953 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4954 then
4955 gotoghyll (clamp (pgscale ~-(state.winh)))
4956 else
4957 let decr =
4958 if l.pageno = state.pagecount - coverB
4959 then 1
4960 else c
4962 let pageno = max 0 (l.pageno-decr) in
4963 gotoghyll (getpagey pageno)
4964 | Csplit (n, _) ->
4965 let y =
4966 if l.pagecol = 0
4967 then
4968 if l.pageno = 0
4969 then l.pagey
4970 else
4971 let pageno = max 0 (l.pageno-1) in
4972 let pagey, pageh = getpageyh pageno in
4973 pagey + (n-1)*pageh
4974 else
4975 let pagey, pageh = getpageyh l.pageno in
4976 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4978 gotoghyll y
4981 let viewkeyboard key mask =
4982 let enttext te =
4983 let mode = state.mode in
4984 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4985 state.text <- "";
4986 enttext ();
4987 G.postRedisplay "view:enttext"
4989 let ctrl = Wsi.withctrl mask in
4990 let key =
4991 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4993 match key with
4994 | 81 -> (* Q *)
4995 exit 0
4997 | 0xff63 -> (* insert *)
4998 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4999 then (
5000 state.mode <- LinkNav (Ltgendir 0);
5001 gotoy state.y;
5003 else showtext '!' "Keyboard link navigation does not work under rotation"
5005 | 0xff1b | 113 -> (* escape / q *)
5006 begin match state.mstate with
5007 | Mzoomrect _ ->
5008 state.mstate <- Mnone;
5009 Wsi.setcursor Wsi.CURSOR_INHERIT;
5010 G.postRedisplay "kill zoom rect";
5011 | _ ->
5012 begin match state.mode with
5013 | LinkNav _ ->
5014 state.mode <- View;
5015 G.postRedisplay "esc leave linknav"
5016 | _ ->
5017 match state.ranchors with
5018 | [] -> raise Quit
5019 | (path, password, anchor, origin) :: rest ->
5020 state.ranchors <- rest;
5021 state.anchor <- anchor;
5022 state.origin <- origin;
5023 opendoc path password
5024 end;
5025 end;
5027 | 0xff08 -> (* backspace *)
5028 gotoghyll (getnav ~-1)
5030 | 111 -> (* o *)
5031 enteroutlinemode ()
5033 | 117 -> (* u *)
5034 state.rects <- [];
5035 state.text <- "";
5036 G.postRedisplay "dehighlight";
5038 | 47 | 63 -> (* / ? *)
5039 let ondone isforw s =
5040 cbput state.hists.pat s;
5041 state.searchpattern <- s;
5042 search s isforw
5044 let s = String.create 1 in
5045 s.[0] <- Char.chr key;
5046 enttext (s, "", Some (onhist state.hists.pat),
5047 textentry, ondone (key = 47), true)
5049 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5050 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5051 setzoom (conf.zoom +. incr)
5053 | 43 | 0xffab -> (* + *)
5054 let ondone s =
5055 let n =
5056 try int_of_string s with exc ->
5057 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5058 max_int
5060 if n != max_int
5061 then (
5062 conf.pagebias <- n;
5063 state.text <- "page bias is now " ^ string_of_int n;
5066 enttext ("page bias: ", "", None, intentry, ondone, true)
5068 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5069 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5070 setzoom (max 0.01 (conf.zoom -. decr))
5072 | 45 | 0xffad -> (* - *)
5073 let ondone msg = state.text <- msg in
5074 enttext (
5075 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5076 optentry state.mode, ondone, true
5079 | 48 when ctrl -> (* ctrl-0 *)
5080 if conf.zoom = 1.0
5081 then (
5082 state.x <- 0;
5083 state.hscrollh <-
5084 if state.w <= state.winw - state.scrollw
5085 then 0
5086 else state.scrollw
5088 gotoy state.y
5090 else setzoom 1.0
5092 | (49 | 50) when ctrl -> (* ctrl-1/2 *)
5093 let cols =
5094 match conf.columns with
5095 | Csingle _ | Cmulti _ -> 1
5096 | Csplit (n, _) -> n
5098 let h = state.winh -
5099 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5101 let zoom = zoomforh state.winw h state.scrollw cols in
5102 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5103 then setzoom zoom
5105 | 51 when ctrl -> (* ctrl-3 *)
5106 let fm =
5107 match conf.fitmodel with
5108 | FitWidth -> FitProportional
5109 | FitProportional -> FitPage
5110 | FitPage -> FitWidth
5112 state.text <- "fit model: " ^ fitmodel_to_string fm;
5113 reqlayout conf.angle fm
5115 | 0xffc6 -> (* f9 *)
5116 togglebirdseye ()
5118 | 57 when ctrl -> (* ctrl-9 *)
5119 togglebirdseye ()
5121 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5122 when not ctrl -> (* 0..9 *)
5123 let ondone s =
5124 let n =
5125 try int_of_string s with exc ->
5126 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5129 if n >= 0
5130 then (
5131 addnav ();
5132 cbput state.hists.pag (string_of_int n);
5133 gotopage1 (n + conf.pagebias - 1) 0;
5136 let pageentry text key =
5137 match Char.unsafe_chr key with
5138 | 'g' -> TEdone text
5139 | _ -> intentry text key
5141 let text = "x" in text.[0] <- Char.chr key;
5142 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5144 | 98 -> (* b *)
5145 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5146 reshape state.winw state.winh;
5148 | 108 -> (* l *)
5149 conf.hlinks <- not conf.hlinks;
5150 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5151 G.postRedisplay "toggle highlightlinks";
5153 | 70 -> (* F *)
5154 state.glinks <- true;
5155 let mode = state.mode in
5156 state.mode <- Textentry (
5157 (":", "", None, linknentry, linkndone gotounder, false),
5158 (fun _ ->
5159 state.glinks <- false;
5160 state.mode <- mode)
5162 state.text <- "";
5163 G.postRedisplay "view:linkent(F)"
5165 | 121 -> (* y *)
5166 state.glinks <- true;
5167 let mode = state.mode in
5168 state.mode <- Textentry (
5169 (":", "", None, linknentry, linkndone (fun under ->
5170 match Ne.pipe () with
5171 | Ne.Exn exn ->
5172 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
5173 | Ne.Res (r, w) ->
5174 let popened =
5175 try popen conf.selcmd [r, 0; w, -1]; true
5176 with exn ->
5177 showtext '!'
5178 (Printf.sprintf "failed to execute %s: %s"
5179 conf.selcmd (exntos exn));
5180 false
5182 let clo cap fd =
5183 Ne.clo fd (fun msg ->
5184 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5187 let s = undertext under in
5188 if popened
5189 then
5190 (try
5191 let l = String.length s in
5192 let n = tempfailureretry (Unix.write w s 0) l in
5193 if n != l
5194 then
5195 showtext '!'
5196 (Printf.sprintf
5197 "failed to write %d characters to sel pipe, wrote %d"
5200 with exn ->
5201 showtext '!'
5202 (Printf.sprintf "failed to write to sel pipe: %s"
5203 (exntos exn)
5206 else dolog "%s" s;
5207 clo "pipe/r" r;
5208 clo "pipe/w" w;
5209 ), false
5211 fun _ ->
5212 state.glinks <- false;
5213 state.mode <- mode
5215 state.text <- "";
5216 G.postRedisplay "view:linkent"
5218 | 97 -> (* a *)
5219 begin match state.autoscroll with
5220 | Some step ->
5221 conf.autoscrollstep <- step;
5222 state.autoscroll <- None
5223 | None ->
5224 if conf.autoscrollstep = 0
5225 then state.autoscroll <- Some 1
5226 else state.autoscroll <- Some conf.autoscrollstep
5229 | 112 when ctrl -> (* ctrl-p *)
5230 launchpath ()
5232 | 80 -> (* P *)
5233 setpresentationmode (not conf.presentation);
5234 showtext ' ' ("presentation mode " ^
5235 if conf.presentation then "on" else "off");
5237 | 102 -> (* f *)
5238 if List.mem Wsi.Fullscreen state.winstate
5239 then Wsi.reshape conf.cwinw conf.cwinh
5240 else Wsi.fullscreen ()
5242 | 112 | 78 -> (* p|N *)
5243 search state.searchpattern false
5245 | 110 | 0xffc0 -> (* n|F3 *)
5246 search state.searchpattern true
5248 | 116 -> (* t *)
5249 begin match state.layout with
5250 | [] -> ()
5251 | l :: _ ->
5252 gotoghyll (getpagey l.pageno)
5255 | 32 -> (* space *)
5256 nextpage ()
5258 | 0xff9f | 0xffff -> (* delete *)
5259 prevpage ()
5261 | 61 -> (* = *)
5262 showtext ' ' (describe_location ());
5264 | 119 -> (* w *)
5265 begin match state.layout with
5266 | [] -> ()
5267 | l :: _ ->
5268 Wsi.reshape (l.pagew + state.scrollw) l.pageh;
5269 G.postRedisplay "w"
5272 | 39 -> (* ' *)
5273 enterbookmarkmode ()
5275 | 104 | 0xffbe -> (* h|F1 *)
5276 enterhelpmode ()
5278 | 105 -> (* i *)
5279 enterinfomode ()
5281 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5282 entermsgsmode ()
5284 | 109 -> (* m *)
5285 let ondone s =
5286 match state.layout with
5287 | l :: _ ->
5288 if String.length s > 0
5289 then
5290 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5291 | _ -> ()
5293 enttext ("bookmark: ", "", None, textentry, ondone, true)
5295 | 126 -> (* ~ *)
5296 quickbookmark ();
5297 showtext ' ' "Quick bookmark added";
5299 | 122 -> (* z *)
5300 begin match state.layout with
5301 | l :: _ ->
5302 let rect = getpdimrect l.pagedimno in
5303 let w, h =
5304 if conf.crophack
5305 then
5306 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5307 truncate (1.2 *. (rect.(3) -. rect.(0))))
5308 else
5309 (truncate (rect.(1) -. rect.(0)),
5310 truncate (rect.(3) -. rect.(0)))
5312 let w = truncate ((float w)*.conf.zoom)
5313 and h = truncate ((float h)*.conf.zoom) in
5314 if w != 0 && h != 0
5315 then (
5316 state.anchor <- getanchor ();
5317 Wsi.reshape (w + state.scrollw) (h + conf.interpagespace)
5319 G.postRedisplay "z";
5321 | [] -> ()
5324 | 60 | 62 -> (* < > *)
5325 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5327 | 91 | 93 -> (* [ ] *)
5328 conf.colorscale <-
5329 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5331 G.postRedisplay "brightness";
5333 | 99 when state.mode = View -> (* c *)
5334 let (c, a, b), z =
5335 match state.prevcolumns with
5336 | None -> (1, 0, 0), 1.0
5337 | Some (columns, z) ->
5338 let cab =
5339 match columns with
5340 | Csplit (c, _) -> -c, 0, 0
5341 | Cmulti ((c, a, b), _) -> c, a, b
5342 | Csingle _ -> 1, 0, 0
5344 cab, z
5346 setcolumns View c a b;
5347 setzoom z;
5349 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5350 setzoom state.prevzoom
5352 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5353 begin match state.autoscroll with
5354 | None ->
5355 begin match state.mode with
5356 | Birdseye beye -> upbirdseye 1 beye
5357 | _ ->
5358 if ctrl
5359 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5360 else (
5361 if not (Wsi.withshift mask) && conf.presentation
5362 then prevpage ()
5363 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5366 | Some n ->
5367 setautoscrollspeed n false
5370 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5371 begin match state.autoscroll with
5372 | None ->
5373 begin match state.mode with
5374 | Birdseye beye -> downbirdseye 1 beye
5375 | _ ->
5376 if ctrl
5377 then gotoy_and_clear_text (clamp (state.winh/2))
5378 else (
5379 if not (Wsi.withshift mask) && conf.presentation
5380 then nextpage ()
5381 else gotoy_and_clear_text (clamp conf.scrollstep)
5384 | Some n ->
5385 setautoscrollspeed n true
5388 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5389 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5390 if canpan ()
5391 then
5392 let dx =
5393 if ctrl
5394 then state.winw / 2
5395 else conf.hscrollstep
5397 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5398 state.x <- state.x + dx;
5399 gotoy_and_clear_text state.y
5400 else (
5401 state.text <- "";
5402 G.postRedisplay "lef/right"
5405 | 0xff55 | 0xff9a -> (* (kp) prior *)
5406 let y =
5407 if ctrl
5408 then
5409 match state.layout with
5410 | [] -> state.y
5411 | l :: _ -> state.y - l.pagey
5412 else
5413 clamp (pgscale (-state.winh))
5415 gotoghyll y
5417 | 0xff56 | 0xff9b -> (* (kp) next *)
5418 let y =
5419 if ctrl
5420 then
5421 match List.rev state.layout with
5422 | [] -> state.y
5423 | l :: _ -> getpagey l.pageno
5424 else
5425 clamp (pgscale state.winh)
5427 gotoghyll y
5429 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5430 gotoghyll 0
5431 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5432 gotoghyll (clamp state.maxy)
5434 | 0xff53 | 0xff98
5435 when Wsi.withalt mask -> (* alt-(kp) right *)
5436 gotoghyll (getnav 1)
5437 | 0xff51 | 0xff96
5438 when Wsi.withalt mask -> (* alt-(kp) left *)
5439 gotoghyll (getnav ~-1)
5441 | 114 -> (* r *)
5442 reload ()
5444 | 118 when conf.debug -> (* v *)
5445 state.rects <- [];
5446 List.iter (fun l ->
5447 match getopaque l.pageno with
5448 | None -> ()
5449 | Some opaque ->
5450 let x0, y0, x1, y1 = pagebbox opaque in
5451 let a,b = float x0, float y0 in
5452 let c,d = float x1, float y0 in
5453 let e,f = float x1, float y1 in
5454 let h,j = float x0, float y1 in
5455 let rect = (a,b,c,d,e,f,h,j) in
5456 debugrect rect;
5457 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5458 ) state.layout;
5459 G.postRedisplay "v";
5461 | _ ->
5462 vlog "huh? %s" (Wsi.keyname key)
5465 let linknavkeyboard key mask linknav =
5466 let getpage pageno =
5467 let rec loop = function
5468 | [] -> None
5469 | l :: _ when l.pageno = pageno -> Some l
5470 | _ :: rest -> loop rest
5471 in loop state.layout
5473 let doexact (pageno, n) =
5474 match getopaque pageno, getpage pageno with
5475 | Some opaque, Some l ->
5476 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5477 then
5478 let under = getlink opaque n in
5479 G.postRedisplay "link gotounder";
5480 gotounder under;
5481 state.mode <- View;
5482 else
5483 let opt, dir =
5484 match key with
5485 | 0xff50 -> (* home *)
5486 Some (findlink opaque LDfirst), -1
5488 | 0xff57 -> (* end *)
5489 Some (findlink opaque LDlast), 1
5491 | 0xff51 -> (* left *)
5492 Some (findlink opaque (LDleft n)), -1
5494 | 0xff53 -> (* right *)
5495 Some (findlink opaque (LDright n)), 1
5497 | 0xff52 -> (* up *)
5498 Some (findlink opaque (LDup n)), -1
5500 | 0xff54 -> (* down *)
5501 Some (findlink opaque (LDdown n)), 1
5503 | _ -> None, 0
5505 let pwl l dir =
5506 begin match findpwl l.pageno dir with
5507 | Pwlnotfound -> ()
5508 | Pwl pageno ->
5509 let notfound dir =
5510 state.mode <- LinkNav (Ltgendir dir);
5511 let y, h = getpageyh pageno in
5512 let y =
5513 if dir < 0
5514 then y + h - state.winh
5515 else y
5517 gotoy y
5519 begin match getopaque pageno, getpage pageno with
5520 | Some opaque, Some _ ->
5521 let link =
5522 let ld = if dir > 0 then LDfirst else LDlast in
5523 findlink opaque ld
5525 begin match link with
5526 | Lfound m ->
5527 showlinktype (getlink opaque m);
5528 state.mode <- LinkNav (Ltexact (pageno, m));
5529 G.postRedisplay "linknav jpage";
5530 | _ -> notfound dir
5531 end;
5532 | _ -> notfound dir
5533 end;
5534 end;
5536 begin match opt with
5537 | Some Lnotfound -> pwl l dir;
5538 | Some (Lfound m) ->
5539 if m = n
5540 then pwl l dir
5541 else (
5542 let _, y0, _, y1 = getlinkrect opaque m in
5543 if y0 < l.pagey
5544 then gotopage1 l.pageno y0
5545 else (
5546 let d = fstate.fontsize + 1 in
5547 if y1 - l.pagey > l.pagevh - d
5548 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5549 else G.postRedisplay "linknav";
5551 showlinktype (getlink opaque m);
5552 state.mode <- LinkNav (Ltexact (l.pageno, m));
5555 | None -> viewkeyboard key mask
5556 end;
5557 | _ -> viewkeyboard key mask
5559 if key = 0xff63
5560 then (
5561 state.mode <- View;
5562 G.postRedisplay "leave linknav"
5564 else
5565 match linknav with
5566 | Ltgendir _ -> viewkeyboard key mask
5567 | Ltexact exact -> doexact exact
5570 let keyboard key mask =
5571 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5572 then wcmd "interrupt"
5573 else state.uioh <- state.uioh#key key mask
5576 let birdseyekeyboard key mask
5577 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5578 let incr =
5579 match conf.columns with
5580 | Csingle _ -> 1
5581 | Cmulti ((c, _, _), _) -> c
5582 | Csplit _ -> failwith "bird's eye split mode"
5584 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5585 match key with
5586 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5587 let y, h = getpageyh pageno in
5588 let top = (state.winh - h) / 2 in
5589 gotoy (max 0 (y - top))
5590 | 0xff0d (* enter *)
5591 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5592 | 0xff1b -> leavebirdseye beye true (* escape *)
5593 | 0xff52 -> upbirdseye incr beye (* up *)
5594 | 0xff54 -> downbirdseye incr beye (* down *)
5595 | 0xff51 -> upbirdseye 1 beye (* left *)
5596 | 0xff53 -> downbirdseye 1 beye (* right *)
5598 | 0xff55 -> (* prior *)
5599 begin match state.layout with
5600 | l :: _ ->
5601 if l.pagey != 0
5602 then (
5603 state.mode <- Birdseye (
5604 oconf, leftx, l.pageno, hooverpageno, anchor
5606 gotopage1 l.pageno 0;
5608 else (
5609 let layout = layout (state.y-state.winh) (pgh state.layout) in
5610 match layout with
5611 | [] -> gotoy (clamp (-state.winh))
5612 | l :: _ ->
5613 state.mode <- Birdseye (
5614 oconf, leftx, l.pageno, hooverpageno, anchor
5616 gotopage1 l.pageno 0
5619 | [] -> gotoy (clamp (-state.winh))
5620 end;
5622 | 0xff56 -> (* next *)
5623 begin match List.rev state.layout with
5624 | l :: _ ->
5625 let layout = layout (state.y + (pgh state.layout)) state.winh in
5626 begin match layout with
5627 | [] ->
5628 let incr = l.pageh - l.pagevh in
5629 if incr = 0
5630 then (
5631 state.mode <-
5632 Birdseye (
5633 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5635 G.postRedisplay "birdseye pagedown";
5637 else gotoy (clamp (incr + conf.interpagespace*2));
5639 | l :: _ ->
5640 state.mode <-
5641 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5642 gotopage1 l.pageno 0;
5645 | [] -> gotoy (clamp state.winh)
5646 end;
5648 | 0xff50 -> (* home *)
5649 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5650 gotopage1 0 0
5652 | 0xff57 -> (* end *)
5653 let pageno = state.pagecount - 1 in
5654 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5655 if not (pagevisible state.layout pageno)
5656 then
5657 let h =
5658 match List.rev state.pdims with
5659 | [] -> state.winh
5660 | (_, _, h, _) :: _ -> h
5662 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5663 else G.postRedisplay "birdseye end";
5664 | _ -> viewkeyboard key mask
5667 let drawpage l linkindexbase =
5668 let color =
5669 match state.mode with
5670 | Textentry _ -> scalecolor 0.4
5671 | LinkNav _
5672 | View -> scalecolor 1.0
5673 | Birdseye (_, _, pageno, hooverpageno, _) ->
5674 if l.pageno = hooverpageno
5675 then scalecolor 0.9
5676 else (
5677 if l.pageno = pageno
5678 then scalecolor 1.0
5679 else scalecolor 0.8
5682 drawtiles l color;
5683 begin match getopaque l.pageno with
5684 | Some opaque ->
5685 if tileready l l.pagex l.pagey
5686 then
5687 let x = l.pagedispx - l.pagex
5688 and y = l.pagedispy - l.pagey in
5689 let hlmask =
5690 match conf.columns with
5691 | Csingle _ | Cmulti _ ->
5692 (if conf.hlinks then 1 else 0)
5693 + (if state.glinks
5694 && not (isbirdseye state.mode) then 2 else 0)
5695 | _ -> 0
5697 let s =
5698 match state.mode with
5699 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5700 | _ -> ""
5702 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5703 else 0
5705 | _ -> 0
5706 end;
5709 let scrollindicator () =
5710 let sbw, ph, sh = state.uioh#scrollph in
5711 let sbh, pw, sw = state.uioh#scrollpw in
5713 GlDraw.color (0.64, 0.64, 0.64);
5714 GlDraw.rect
5715 (float (state.winw - sbw), 0.)
5716 (float state.winw, float state.winh)
5718 GlDraw.rect
5719 (0., float (state.winh - sbh))
5720 (float (state.winw - state.scrollw - 1), float state.winh)
5722 GlDraw.color (0.0, 0.0, 0.0);
5724 GlDraw.rect
5725 (float (state.winw - sbw), ph)
5726 (float state.winw, ph +. sh)
5728 GlDraw.rect
5729 (pw, float (state.winh - sbh))
5730 (pw +. sw, float state.winh)
5734 let showsel () =
5735 match state.mstate with
5736 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5739 | Msel ((x0, y0), (x1, y1)) ->
5740 let rec loop = function
5741 | l :: ls ->
5742 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5743 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5744 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5745 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5746 then
5747 match getopaque l.pageno with
5748 | Some opaque ->
5749 let x0, y0 = pagetranslatepoint l x0 y0 in
5750 let x1, y1 = pagetranslatepoint l x1 y1 in
5751 seltext opaque (x0, y0, x1, y1);
5752 | _ -> ()
5753 else loop ls
5754 | [] -> ()
5756 loop state.layout
5759 let showrects rects =
5760 Gl.enable `blend;
5761 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5762 GlDraw.polygon_mode `both `fill;
5763 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5764 List.iter
5765 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5766 List.iter (fun l ->
5767 if l.pageno = pageno
5768 then (
5769 let dx = float (l.pagedispx - l.pagex) in
5770 let dy = float (l.pagedispy - l.pagey) in
5771 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5772 GlDraw.begins `quads;
5774 GlDraw.vertex2 (x0+.dx, y0+.dy);
5775 GlDraw.vertex2 (x1+.dx, y1+.dy);
5776 GlDraw.vertex2 (x2+.dx, y2+.dy);
5777 GlDraw.vertex2 (x3+.dx, y3+.dy);
5779 GlDraw.ends ();
5781 ) state.layout
5782 ) rects
5784 Gl.disable `blend;
5787 let display () =
5788 GlClear.color (scalecolor2 conf.bgcolor);
5789 GlClear.clear [`color];
5790 let rec loop linkindexbase = function
5791 | l :: rest ->
5792 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5793 loop linkindexbase rest
5794 | [] -> ()
5796 loop 0 state.layout;
5797 let rects =
5798 match state.mode with
5799 | LinkNav (Ltexact (pageno, linkno)) ->
5800 begin match getopaque pageno with
5801 | Some opaque ->
5802 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5803 (pageno, 5, (
5804 float x0, float y0,
5805 float x1, float y0,
5806 float x1, float y1,
5807 float x0, float y1)
5808 ) :: state.rects
5809 | None -> state.rects
5811 | _ -> state.rects
5813 showrects rects;
5814 showsel ();
5815 state.uioh#display;
5816 begin match state.mstate with
5817 | Mzoomrect ((x0, y0), (x1, y1)) ->
5818 Gl.enable `blend;
5819 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5820 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5821 GlDraw.rect (float x0, float y0)
5822 (float x1, float y1);
5823 Gl.disable `blend;
5824 | _ -> ()
5825 end;
5826 enttext ();
5827 scrollindicator ();
5828 Wsi.swapb ();
5831 let zoomrect x y x1 y1 =
5832 let x0 = min x x1
5833 and x1 = max x x1
5834 and y0 = min y y1 in
5835 gotoy (state.y + y0);
5836 state.anchor <- getanchor ();
5837 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5838 let margin =
5839 if state.w < state.winw - state.scrollw
5840 then (state.winw - state.scrollw - state.w) / 2
5841 else 0
5843 state.x <- (state.x + margin) - x0;
5844 setzoom zoom;
5845 Wsi.setcursor Wsi.CURSOR_INHERIT;
5846 state.mstate <- Mnone;
5849 let scrollx x =
5850 let winw = state.winw - state.scrollw - 1 in
5851 let s = float x /. float winw in
5852 let destx = truncate (float (state.w + winw) *. s) in
5853 state.x <- winw - destx;
5854 gotoy_and_clear_text state.y;
5855 state.mstate <- Mscrollx;
5858 let scrolly y =
5859 let s = float y /. float state.winh in
5860 let desty = truncate (float (state.maxy - state.winh) *. s) in
5861 gotoy_and_clear_text desty;
5862 state.mstate <- Mscrolly;
5865 let viewmouse button down x y mask =
5866 match button with
5867 | n when (n == 4 || n == 5) && not down ->
5868 if Wsi.withctrl mask
5869 then (
5870 match state.mstate with
5871 | Mzoom (oldn, i) ->
5872 if oldn = n
5873 then (
5874 if i = 2
5875 then
5876 let incr =
5877 match n with
5878 | 5 ->
5879 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5880 | _ ->
5881 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5883 let zoom = conf.zoom -. incr in
5884 setzoom zoom;
5885 state.mstate <- Mzoom (n, 0);
5886 else
5887 state.mstate <- Mzoom (n, i+1);
5889 else state.mstate <- Mzoom (n, 0)
5891 | _ -> state.mstate <- Mzoom (n, 0)
5893 else (
5894 match state.autoscroll with
5895 | Some step -> setautoscrollspeed step (n=4)
5896 | None ->
5897 if conf.wheelbypage || conf.presentation
5898 then (
5899 if n = 4
5900 then prevpage ()
5901 else nextpage ()
5903 else
5904 let incr =
5905 if n = 4
5906 then -conf.scrollstep
5907 else conf.scrollstep
5909 let incr = incr * 2 in
5910 let y = clamp incr in
5911 gotoy_and_clear_text y
5914 | n when (n = 6 || n = 7) && not down && canpan () ->
5915 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5916 gotoy_and_clear_text state.y
5918 | 1 when Wsi.withshift mask ->
5919 state.mstate <- Mnone;
5920 if not down
5921 then (
5922 match unproject x y with
5923 | Some (pageno, ux, uy) ->
5924 let cmd = Printf.sprintf
5925 "%s %s %d %d %d"
5926 conf.stcmd state.path pageno ux uy
5928 popen cmd []
5929 | None -> ()
5932 | 1 when Wsi.withctrl mask ->
5933 if down
5934 then (
5935 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5936 state.mstate <- Mpan (x, y)
5938 else
5939 state.mstate <- Mnone
5941 | 3 ->
5942 if down
5943 then (
5944 Wsi.setcursor Wsi.CURSOR_CYCLE;
5945 let p = (x, y) in
5946 state.mstate <- Mzoomrect (p, p)
5948 else (
5949 match state.mstate with
5950 | Mzoomrect ((x0, y0), _) ->
5951 if abs (x-x0) > 10 && abs (y - y0) > 10
5952 then zoomrect x0 y0 x y
5953 else (
5954 state.mstate <- Mnone;
5955 Wsi.setcursor Wsi.CURSOR_INHERIT;
5956 G.postRedisplay "kill accidental zoom rect";
5958 | _ ->
5959 Wsi.setcursor Wsi.CURSOR_INHERIT;
5960 state.mstate <- Mnone
5963 | 1 when x > state.winw - state.scrollw ->
5964 if down
5965 then
5966 let _, position, sh = state.uioh#scrollph in
5967 if y > truncate position && y < truncate (position +. sh)
5968 then state.mstate <- Mscrolly
5969 else scrolly y
5970 else
5971 state.mstate <- Mnone
5973 | 1 when y > state.winh - state.hscrollh ->
5974 if down
5975 then
5976 let _, position, sw = state.uioh#scrollpw in
5977 if x > truncate position && x < truncate (position +. sw)
5978 then state.mstate <- Mscrollx
5979 else scrollx x
5980 else
5981 state.mstate <- Mnone
5983 | 1 ->
5984 let dest = if down then getunder x y else Unone in
5985 begin match dest with
5986 | Ulinkgoto _
5987 | Ulinkuri _
5988 | Uremote _
5989 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5990 gotounder dest
5992 | Unone when down ->
5993 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5994 state.mstate <- Mpan (x, y);
5996 | Unone | Utext _ ->
5997 if down
5998 then (
5999 if conf.angle mod 360 = 0
6000 then (
6001 state.mstate <- Msel ((x, y), (x, y));
6002 G.postRedisplay "mouse select";
6005 else (
6006 match state.mstate with
6007 | Mnone -> ()
6009 | Mzoom _ | Mscrollx | Mscrolly ->
6010 state.mstate <- Mnone
6012 | Mzoomrect ((x0, y0), _) ->
6013 zoomrect x0 y0 x y
6015 | Mpan _ ->
6016 Wsi.setcursor Wsi.CURSOR_INHERIT;
6017 state.mstate <- Mnone
6019 | Msel ((x0, y0), (x1, y1)) ->
6020 let rec loop = function
6021 | [] -> ()
6022 | l :: rest ->
6023 let inside =
6024 let a0 = l.pagedispy in
6025 let a1 = a0 + l.pagevh in
6026 let b0 = l.pagedispx in
6027 let b1 = b0 + l.pagevw in
6028 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6029 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6031 if inside
6032 then
6033 match getopaque l.pageno with
6034 | Some opaque ->
6035 begin
6036 match Ne.pipe () with
6037 | Ne.Exn exn ->
6038 showtext '!'
6039 (Printf.sprintf
6040 "can not create sel pipe: %s"
6041 (exntos exn));
6042 | Ne.Res (r, w) ->
6043 let doclose what fd =
6044 Ne.clo fd (fun msg ->
6045 dolog "%s close failed: %s" what msg)
6048 popen conf.selcmd [r, 0; w, -1];
6049 copysel w opaque;
6050 doclose "pipe/r" r;
6051 G.postRedisplay "copysel";
6052 with exn ->
6053 dolog "can not execute %S: %s"
6054 conf.selcmd (exntos exn);
6055 doclose "pipe/r" r;
6056 doclose "pipe/w" w;
6058 | None -> ()
6059 else loop rest
6061 loop state.layout;
6062 Wsi.setcursor Wsi.CURSOR_INHERIT;
6063 state.mstate <- Mnone;
6067 | _ -> ()
6070 let birdseyemouse button down x y mask
6071 (conf, leftx, _, hooverpageno, anchor) =
6072 match button with
6073 | 1 when down ->
6074 let rec loop = function
6075 | [] -> ()
6076 | l :: rest ->
6077 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6078 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6079 then (
6080 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6082 else loop rest
6084 loop state.layout
6085 | 3 -> ()
6086 | _ -> viewmouse button down x y mask
6089 let mouse button down x y mask =
6090 state.uioh <- state.uioh#button button down x y mask;
6093 let motion ~x ~y =
6094 state.uioh <- state.uioh#motion x y
6097 let pmotion ~x ~y =
6098 state.uioh <- state.uioh#pmotion x y;
6101 let uioh = object
6102 method display = ()
6104 method key key mask =
6105 begin match state.mode with
6106 | Textentry textentry -> textentrykeyboard key mask textentry
6107 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6108 | View -> viewkeyboard key mask
6109 | LinkNav linknav -> linknavkeyboard key mask linknav
6110 end;
6111 state.uioh
6113 method button button bstate x y mask =
6114 begin match state.mode with
6115 | LinkNav _
6116 | View -> viewmouse button bstate x y mask
6117 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6118 | Textentry _ -> ()
6119 end;
6120 state.uioh
6122 method motion x y =
6123 begin match state.mode with
6124 | Textentry _ -> ()
6125 | View | Birdseye _ | LinkNav _ ->
6126 match state.mstate with
6127 | Mzoom _ | Mnone -> ()
6129 | Mpan (x0, y0) ->
6130 let dx = x - x0
6131 and dy = y0 - y in
6132 state.mstate <- Mpan (x, y);
6133 if canpan ()
6134 then state.x <- state.x + dx;
6135 let y = clamp dy in
6136 gotoy_and_clear_text y
6138 | Msel (a, _) ->
6139 state.mstate <- Msel (a, (x, y));
6140 G.postRedisplay "motion select";
6142 | Mscrolly ->
6143 let y = min state.winh (max 0 y) in
6144 scrolly y
6146 | Mscrollx ->
6147 let x = min state.winw (max 0 x) in
6148 scrollx x
6150 | Mzoomrect (p0, _) ->
6151 state.mstate <- Mzoomrect (p0, (x, y));
6152 G.postRedisplay "motion zoomrect";
6153 end;
6154 state.uioh
6156 method pmotion x y =
6157 begin match state.mode with
6158 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6159 let rec loop = function
6160 | [] ->
6161 if hooverpageno != -1
6162 then (
6163 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6164 G.postRedisplay "pmotion birdseye no hoover";
6166 | l :: rest ->
6167 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6168 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6169 then (
6170 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6171 G.postRedisplay "pmotion birdseye hoover";
6173 else loop rest
6175 loop state.layout
6177 | Textentry _ -> ()
6179 | LinkNav _
6180 | View ->
6181 match state.mstate with
6182 | Mnone -> updateunder x y
6183 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6185 end;
6186 state.uioh
6188 method infochanged _ = ()
6190 method scrollph =
6191 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6192 let p, h = scrollph state.y maxy in
6193 state.scrollw, p, h
6195 method scrollpw =
6196 let winw = state.winw - state.scrollw - 1 in
6197 let fwinw = float winw in
6198 let sw =
6199 let sw = fwinw /. float state.w in
6200 let sw = fwinw *. sw in
6201 max sw (float conf.scrollh)
6203 let position, sw =
6204 let f = state.w+winw in
6205 let r = float (winw-state.x) /. float f in
6206 let p = fwinw *. r in
6207 p-.sw/.2., sw
6209 let sw =
6210 if position +. sw > fwinw
6211 then fwinw -. position
6212 else sw
6214 state.hscrollh, position, sw
6216 method modehash =
6217 let modename =
6218 match state.mode with
6219 | LinkNav _ -> "links"
6220 | Textentry _ -> "textentry"
6221 | Birdseye _ -> "birdseye"
6222 | View -> "view"
6224 findkeyhash conf modename
6226 method eformsgs = true
6227 end;;
6229 module Config =
6230 struct
6231 open Parser
6233 let fontpath = ref "";;
6235 module KeyMap =
6236 Map.Make (struct type t = (int * int) let compare = compare end);;
6238 let unent s =
6239 let l = String.length s in
6240 let b = Buffer.create l in
6241 unent b s 0 l;
6242 Buffer.contents b;
6245 let home =
6246 try Sys.getenv "HOME"
6247 with exn ->
6248 prerr_endline
6249 ("Can not determine home directory location: " ^ exntos exn);
6253 let modifier_of_string = function
6254 | "alt" -> Wsi.altmask
6255 | "shift" -> Wsi.shiftmask
6256 | "ctrl" | "control" -> Wsi.ctrlmask
6257 | "meta" -> Wsi.metamask
6258 | _ -> 0
6261 let key_of_string =
6262 let r = Str.regexp "-" in
6263 fun s ->
6264 let elems = Str.full_split r s in
6265 let f n k m =
6266 let g s =
6267 let m1 = modifier_of_string s in
6268 if m1 = 0
6269 then (Wsi.namekey s, m)
6270 else (k, m lor m1)
6271 in function
6272 | Str.Delim s when n land 1 = 0 -> g s
6273 | Str.Text s -> g s
6274 | Str.Delim _ -> (k, m)
6276 let rec loop n k m = function
6277 | [] -> (k, m)
6278 | x :: xs ->
6279 let k, m = f n k m x in
6280 loop (n+1) k m xs
6282 loop 0 0 0 elems
6285 let keys_of_string =
6286 let r = Str.regexp "[ \t]" in
6287 fun s ->
6288 let elems = Str.split r s in
6289 List.map key_of_string elems
6292 let copykeyhashes c =
6293 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6296 let config_of c attrs =
6297 let apply c k v =
6299 match k with
6300 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6301 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6302 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6303 | "preload" -> { c with preload = bool_of_string v }
6304 | "page-bias" -> { c with pagebias = int_of_string v }
6305 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6306 | "horizontal-scroll-step" ->
6307 { c with hscrollstep = max (int_of_string v) 1 }
6308 | "auto-scroll-step" ->
6309 { c with autoscrollstep = max 0 (int_of_string v) }
6310 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6311 | "crop-hack" -> { c with crophack = bool_of_string v }
6312 | "throttle" ->
6313 let mw =
6314 match String.lowercase v with
6315 | "true" -> Some infinity
6316 | "false" -> None
6317 | f -> Some (float_of_string f)
6319 { c with maxwait = mw}
6320 | "highlight-links" -> { c with hlinks = bool_of_string v }
6321 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6322 | "vertical-margin" ->
6323 { c with interpagespace = max 0 (int_of_string v) }
6324 | "zoom" ->
6325 let zoom = float_of_string v /. 100. in
6326 let zoom = max zoom 0.0 in
6327 { c with zoom = zoom }
6328 | "presentation" -> { c with presentation = bool_of_string v }
6329 | "rotation-angle" -> { c with angle = int_of_string v }
6330 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6331 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6332 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6333 | "proportional-display" ->
6334 let fm =
6335 if bool_of_string v
6336 then FitProportional
6337 else FitWidth
6339 { c with fitmodel = fm }
6340 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6341 | "pixmap-cache-size" ->
6342 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6343 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6344 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6345 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6346 | "persistent-location" -> { c with jumpback = bool_of_string v }
6347 | "background-color" -> { c with bgcolor = color_of_string v }
6348 | "scrollbar-in-presentation" ->
6349 { c with scrollbarinpm = bool_of_string v }
6350 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6351 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6352 | "mupdf-store-size" ->
6353 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6354 | "checkers" -> { c with checkers = bool_of_string v }
6355 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6356 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6357 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6358 | "uri-launcher" -> { c with urilauncher = unent v }
6359 | "path-launcher" -> { c with pathlauncher = unent v }
6360 | "color-space" -> { c with colorspace = colorspace_of_string v }
6361 | "invert-colors" -> { c with invert = bool_of_string v }
6362 | "brightness" -> { c with colorscale = float_of_string v }
6363 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6364 | "ghyllscroll" ->
6365 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6366 | "columns" ->
6367 let (n, _, _) as nab = multicolumns_of_string v in
6368 if n < 0
6369 then { c with columns = Csplit (-n, [||]) }
6370 else { c with columns = Cmulti (nab, [||]) }
6371 | "birds-eye-columns" ->
6372 { c with beyecolumns = Some (max (int_of_string v) 2) }
6373 | "selection-command" -> { c with selcmd = unent v }
6374 | "synctex-command" -> { c with stcmd = unent v }
6375 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6376 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6377 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6378 | "use-pbo" -> { c with usepbo = bool_of_string v }
6379 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6380 | _ -> c
6381 with exn ->
6382 prerr_endline ("Error processing attribute (`" ^
6383 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6386 let rec fold c = function
6387 | [] -> c
6388 | (k, v) :: rest ->
6389 let c = apply c k v in
6390 fold c rest
6392 fold { c with keyhashes = copykeyhashes c } attrs;
6395 let fromstring f pos n v d =
6396 try f v
6397 with exn ->
6398 dolog "Error processing attribute (%S=%S) at %d\n%s"
6399 n v pos (exntos exn)
6404 let bookmark_of attrs =
6405 let rec fold title page rely visy = function
6406 | ("title", v) :: rest -> fold v page rely visy rest
6407 | ("page", v) :: rest -> fold title v rely visy rest
6408 | ("rely", v) :: rest -> fold title page v visy rest
6409 | ("visy", v) :: rest -> fold title page rely v rest
6410 | _ :: rest -> fold title page rely visy rest
6411 | [] -> title, page, rely, visy
6413 fold "invalid" "0" "0" "0" attrs
6416 let doc_of attrs =
6417 let rec fold path page rely pan visy = function
6418 | ("path", v) :: rest -> fold v page rely pan visy rest
6419 | ("page", v) :: rest -> fold path v rely pan visy rest
6420 | ("rely", v) :: rest -> fold path page v pan visy rest
6421 | ("pan", v) :: rest -> fold path page rely v visy rest
6422 | ("visy", v) :: rest -> fold path page rely pan v rest
6423 | _ :: rest -> fold path page rely pan visy rest
6424 | [] -> path, page, rely, pan, visy
6426 fold "" "0" "0" "0" "0" attrs
6429 let map_of attrs =
6430 let rec fold rs ls = function
6431 | ("out", v) :: rest -> fold v ls rest
6432 | ("in", v) :: rest -> fold rs v rest
6433 | _ :: rest -> fold ls rs rest
6434 | [] -> ls, rs
6436 fold "" "" attrs
6439 let setconf dst src =
6440 dst.scrollbw <- src.scrollbw;
6441 dst.scrollh <- src.scrollh;
6442 dst.icase <- src.icase;
6443 dst.preload <- src.preload;
6444 dst.pagebias <- src.pagebias;
6445 dst.verbose <- src.verbose;
6446 dst.scrollstep <- src.scrollstep;
6447 dst.maxhfit <- src.maxhfit;
6448 dst.crophack <- src.crophack;
6449 dst.autoscrollstep <- src.autoscrollstep;
6450 dst.maxwait <- src.maxwait;
6451 dst.hlinks <- src.hlinks;
6452 dst.underinfo <- src.underinfo;
6453 dst.interpagespace <- src.interpagespace;
6454 dst.zoom <- src.zoom;
6455 dst.presentation <- src.presentation;
6456 dst.angle <- src.angle;
6457 dst.cwinw <- src.cwinw;
6458 dst.cwinh <- src.cwinh;
6459 dst.savebmarks <- src.savebmarks;
6460 dst.memlimit <- src.memlimit;
6461 dst.fitmodel <- src.fitmodel;
6462 dst.texcount <- src.texcount;
6463 dst.sliceheight <- src.sliceheight;
6464 dst.thumbw <- src.thumbw;
6465 dst.jumpback <- src.jumpback;
6466 dst.bgcolor <- src.bgcolor;
6467 dst.scrollbarinpm <- src.scrollbarinpm;
6468 dst.tilew <- src.tilew;
6469 dst.tileh <- src.tileh;
6470 dst.mustoresize <- src.mustoresize;
6471 dst.checkers <- src.checkers;
6472 dst.aalevel <- src.aalevel;
6473 dst.trimmargins <- src.trimmargins;
6474 dst.trimfuzz <- src.trimfuzz;
6475 dst.urilauncher <- src.urilauncher;
6476 dst.colorspace <- src.colorspace;
6477 dst.invert <- src.invert;
6478 dst.colorscale <- src.colorscale;
6479 dst.redirectstderr <- src.redirectstderr;
6480 dst.ghyllscroll <- src.ghyllscroll;
6481 dst.columns <- src.columns;
6482 dst.beyecolumns <- src.beyecolumns;
6483 dst.selcmd <- src.selcmd;
6484 dst.updatecurs <- src.updatecurs;
6485 dst.pathlauncher <- src.pathlauncher;
6486 dst.keyhashes <- copykeyhashes src;
6487 dst.hfsize <- src.hfsize;
6488 dst.hscrollstep <- src.hscrollstep;
6489 dst.pgscale <- src.pgscale;
6490 dst.usepbo <- src.usepbo;
6491 dst.wheelbypage <- src.wheelbypage;
6492 dst.stcmd <- src.stcmd;
6495 let get s =
6496 let h = Hashtbl.create 10 in
6497 let dc = { defconf with angle = defconf.angle } in
6498 let rec toplevel v t spos _ =
6499 match t with
6500 | Vdata | Vcdata | Vend -> v
6501 | Vopen ("llppconfig", _, closed) ->
6502 if closed
6503 then v
6504 else { v with f = llppconfig }
6505 | Vopen _ ->
6506 error "unexpected subelement at top level" s spos
6507 | Vclose _ -> error "unexpected close at top level" s spos
6509 and llppconfig v t spos _ =
6510 match t with
6511 | Vdata | Vcdata -> v
6512 | Vend -> error "unexpected end of input in llppconfig" s spos
6513 | Vopen ("defaults", attrs, closed) ->
6514 let c = config_of dc attrs in
6515 setconf dc c;
6516 if closed
6517 then v
6518 else { v with f = defaults }
6520 | Vopen ("ui-font", attrs, closed) ->
6521 let rec getsize size = function
6522 | [] -> size
6523 | ("size", v) :: rest ->
6524 let size =
6525 fromstring int_of_string spos "size" v fstate.fontsize in
6526 getsize size rest
6527 | l -> getsize size l
6529 fstate.fontsize <- getsize fstate.fontsize attrs;
6530 if closed
6531 then v
6532 else { v with f = uifont (Buffer.create 10) }
6534 | Vopen ("doc", attrs, closed) ->
6535 let pathent, spage, srely, span, svisy = doc_of attrs in
6536 let path = unent pathent
6537 and pageno = fromstring int_of_string spos "page" spage 0
6538 and rely = fromstring float_of_string spos "rely" srely 0.0
6539 and pan = fromstring int_of_string spos "pan" span 0
6540 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6541 let c = config_of dc attrs in
6542 let anchor = (pageno, rely, visy) in
6543 if closed
6544 then (Hashtbl.add h path (c, [], pan, anchor); v)
6545 else { v with f = doc path pan anchor c [] }
6547 | Vopen _ ->
6548 error "unexpected subelement in llppconfig" s spos
6550 | Vclose "llppconfig" -> { v with f = toplevel }
6551 | Vclose _ -> error "unexpected close in llppconfig" s spos
6553 and defaults v t spos _ =
6554 match t with
6555 | Vdata | Vcdata -> v
6556 | Vend -> error "unexpected end of input in defaults" s spos
6557 | Vopen ("keymap", attrs, closed) ->
6558 let modename =
6559 try List.assoc "mode" attrs
6560 with Not_found -> "global" in
6561 if closed
6562 then v
6563 else
6564 let ret keymap =
6565 let h = findkeyhash dc modename in
6566 KeyMap.iter (Hashtbl.replace h) keymap;
6567 defaults
6569 { v with f = pkeymap ret KeyMap.empty }
6571 | Vopen (_, _, _) ->
6572 error "unexpected subelement in defaults" s spos
6574 | Vclose "defaults" ->
6575 { v with f = llppconfig }
6577 | Vclose _ -> error "unexpected close in defaults" s spos
6579 and uifont b v t spos epos =
6580 match t with
6581 | Vdata | Vcdata ->
6582 Buffer.add_substring b s spos (epos - spos);
6584 | Vopen (_, _, _) ->
6585 error "unexpected subelement in ui-font" s spos
6586 | Vclose "ui-font" ->
6587 if String.length !fontpath = 0
6588 then fontpath := Buffer.contents b;
6589 { v with f = llppconfig }
6590 | Vclose _ -> error "unexpected close in ui-font" s spos
6591 | Vend -> error "unexpected end of input in ui-font" s spos
6593 and doc path pan anchor c bookmarks v t spos _ =
6594 match t with
6595 | Vdata | Vcdata -> v
6596 | Vend -> error "unexpected end of input in doc" s spos
6597 | Vopen ("bookmarks", _, closed) ->
6598 if closed
6599 then v
6600 else { v with f = pbookmarks path pan anchor c bookmarks }
6602 | Vopen ("keymap", attrs, closed) ->
6603 let modename =
6604 try List.assoc "mode" attrs
6605 with Not_found -> "global"
6607 if closed
6608 then v
6609 else
6610 let ret keymap =
6611 let h = findkeyhash c modename in
6612 KeyMap.iter (Hashtbl.replace h) keymap;
6613 doc path pan anchor c bookmarks
6615 { v with f = pkeymap ret KeyMap.empty }
6617 | Vopen (_, _, _) ->
6618 error "unexpected subelement in doc" s spos
6620 | Vclose "doc" ->
6621 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6622 { v with f = llppconfig }
6624 | Vclose _ -> error "unexpected close in doc" s spos
6626 and pkeymap ret keymap v t spos _ =
6627 match t with
6628 | Vdata | Vcdata -> v
6629 | Vend -> error "unexpected end of input in keymap" s spos
6630 | Vopen ("map", attrs, closed) ->
6631 let r, l = map_of attrs in
6632 let kss = fromstring keys_of_string spos "in" r [] in
6633 let lss = fromstring keys_of_string spos "out" l [] in
6634 let keymap =
6635 match kss with
6636 | [] -> keymap
6637 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6638 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6640 if closed
6641 then { v with f = pkeymap ret keymap }
6642 else
6643 let f () = v in
6644 { v with f = skip "map" f }
6646 | Vopen _ ->
6647 error "unexpected subelement in keymap" s spos
6649 | Vclose "keymap" ->
6650 { v with f = ret keymap }
6652 | Vclose _ -> error "unexpected close in keymap" s spos
6654 and pbookmarks path pan anchor c bookmarks v t spos _ =
6655 match t with
6656 | Vdata | Vcdata -> v
6657 | Vend -> error "unexpected end of input in bookmarks" s spos
6658 | Vopen ("item", attrs, closed) ->
6659 let titleent, spage, srely, svisy = bookmark_of attrs in
6660 let page = fromstring int_of_string spos "page" spage 0
6661 and rely = fromstring float_of_string spos "rely" srely 0.0
6662 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6663 let bookmarks =
6664 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6666 if closed
6667 then { v with f = pbookmarks path pan anchor c bookmarks }
6668 else
6669 let f () = v in
6670 { v with f = skip "item" f }
6672 | Vopen _ ->
6673 error "unexpected subelement in bookmarks" s spos
6675 | Vclose "bookmarks" ->
6676 { v with f = doc path pan anchor c bookmarks }
6678 | Vclose _ -> error "unexpected close in bookmarks" s spos
6680 and skip tag f v t spos _ =
6681 match t with
6682 | Vdata | Vcdata -> v
6683 | Vend ->
6684 error ("unexpected end of input in skipped " ^ tag) s spos
6685 | Vopen (tag', _, closed) ->
6686 if closed
6687 then v
6688 else
6689 let f' () = { v with f = skip tag f } in
6690 { v with f = skip tag' f' }
6691 | Vclose ctag ->
6692 if tag = ctag
6693 then f ()
6694 else error ("unexpected close in skipped " ^ tag) s spos
6697 parse { f = toplevel; accu = () } s;
6698 h, dc;
6701 let do_load f ic =
6703 let len = in_channel_length ic in
6704 let s = String.create len in
6705 really_input ic s 0 len;
6706 f s;
6707 with
6708 | Parse_error (msg, s, pos) ->
6709 let subs = subs s pos in
6710 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6711 failwith ("parse error: " ^ s)
6713 | exn ->
6714 failwith ("config load error: " ^ exntos exn)
6717 let defconfpath =
6718 let dir =
6720 let dir = Filename.concat home ".config" in
6721 if Sys.is_directory dir then dir else home
6722 with _ -> home
6724 Filename.concat dir "llpp.conf"
6727 let confpath = ref defconfpath;;
6729 let load1 f =
6730 if Sys.file_exists !confpath
6731 then
6732 match
6733 (try Some (open_in_bin !confpath)
6734 with exn ->
6735 prerr_endline
6736 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6737 exntos exn);
6738 None
6740 with
6741 | Some ic ->
6742 let success =
6744 f (do_load get ic)
6745 with exn ->
6746 prerr_endline
6747 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6748 exntos exn);
6749 false
6751 close_in ic;
6752 success
6754 | None -> false
6755 else
6756 f (Hashtbl.create 0, defconf)
6759 let load () =
6760 let f (h, dc) =
6761 let pc, pb, px, pa =
6763 Hashtbl.find h (Filename.basename state.origin)
6764 with Not_found -> dc, [], 0, emptyanchor
6766 setconf defconf dc;
6767 setconf conf pc;
6768 state.bookmarks <- pb;
6769 state.x <- px;
6770 state.scrollw <- conf.scrollbw;
6771 if conf.jumpback
6772 then state.anchor <- pa;
6773 cbput state.hists.nav pa;
6774 true
6776 load1 f
6779 let add_attrs bb always dc c =
6780 let ob s a b =
6781 if always || a != b
6782 then Printf.bprintf bb "\n %s='%b'" s a
6783 and oi s a b =
6784 if always || a != b
6785 then Printf.bprintf bb "\n %s='%d'" s a
6786 and oI s a b =
6787 if always || a != b
6788 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6789 and oz s a b =
6790 if always || a <> b
6791 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6792 and oF s a b =
6793 if always || a <> b
6794 then Printf.bprintf bb "\n %s='%f'" s a
6795 and oc s a b =
6796 if always || a <> b
6797 then
6798 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6799 and oC s a b =
6800 if always || a <> b
6801 then
6802 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6803 and oR s a b =
6804 if always || a <> b
6805 then
6806 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6807 and os s a b =
6808 if always || a <> b
6809 then
6810 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6811 and og s a b =
6812 if always || a <> b
6813 then
6814 match a with
6815 | None -> ()
6816 | Some (_N, _A, _B) ->
6817 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6818 and oW s a b =
6819 if always || a <> b
6820 then
6821 let v =
6822 match a with
6823 | None -> "false"
6824 | Some f ->
6825 if f = infinity
6826 then "true"
6827 else string_of_float f
6829 Printf.bprintf bb "\n %s='%s'" s v
6830 and oco s a b =
6831 if always || a <> b
6832 then
6833 match a with
6834 | Cmulti ((n, a, b), _) when n > 1 ->
6835 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6836 | Csplit (n, _) when n > 1 ->
6837 Printf.bprintf bb "\n %s='%d'" s ~-n
6838 | _ -> ()
6839 and obeco s a b =
6840 if always || a <> b
6841 then
6842 match a with
6843 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6844 | _ -> ()
6845 and oFm s a b =
6846 if always || a <> b
6847 then
6848 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6850 oi "width" c.cwinw dc.cwinw;
6851 oi "height" c.cwinh dc.cwinh;
6852 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6853 oi "scroll-handle-height" c.scrollh dc.scrollh;
6854 ob "case-insensitive-search" c.icase dc.icase;
6855 ob "preload" c.preload dc.preload;
6856 oi "page-bias" c.pagebias dc.pagebias;
6857 oi "scroll-step" c.scrollstep dc.scrollstep;
6858 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6859 ob "max-height-fit" c.maxhfit dc.maxhfit;
6860 ob "crop-hack" c.crophack dc.crophack;
6861 oW "throttle" c.maxwait dc.maxwait;
6862 ob "highlight-links" c.hlinks dc.hlinks;
6863 ob "under-cursor-info" c.underinfo dc.underinfo;
6864 oi "vertical-margin" c.interpagespace dc.interpagespace;
6865 oz "zoom" c.zoom dc.zoom;
6866 ob "presentation" c.presentation dc.presentation;
6867 oi "rotation-angle" c.angle dc.angle;
6868 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6869 oFm "fit-model" c.fitmodel dc.fitmodel;
6870 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6871 oi "tex-count" c.texcount dc.texcount;
6872 oi "slice-height" c.sliceheight dc.sliceheight;
6873 oi "thumbnail-width" c.thumbw dc.thumbw;
6874 ob "persistent-location" c.jumpback dc.jumpback;
6875 oc "background-color" c.bgcolor dc.bgcolor;
6876 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6877 oi "tile-width" c.tilew dc.tilew;
6878 oi "tile-height" c.tileh dc.tileh;
6879 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6880 ob "checkers" c.checkers dc.checkers;
6881 oi "aalevel" c.aalevel dc.aalevel;
6882 ob "trim-margins" c.trimmargins dc.trimmargins;
6883 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6884 os "uri-launcher" c.urilauncher dc.urilauncher;
6885 os "path-launcher" c.pathlauncher dc.pathlauncher;
6886 oC "color-space" c.colorspace dc.colorspace;
6887 ob "invert-colors" c.invert dc.invert;
6888 oF "brightness" c.colorscale dc.colorscale;
6889 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6890 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6891 oco "columns" c.columns dc.columns;
6892 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6893 os "selection-command" c.selcmd dc.selcmd;
6894 os "synctex-command" c.stcmd dc.stcmd;
6895 ob "update-cursor" c.updatecurs dc.updatecurs;
6896 oi "hint-font-size" c.hfsize dc.hfsize;
6897 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6898 oF "page-scroll-scale" c.pgscale dc.pgscale;
6899 ob "use-pbo" c.usepbo dc.usepbo;
6900 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6903 let keymapsbuf always dc c =
6904 let bb = Buffer.create 16 in
6905 let rec loop = function
6906 | [] -> ()
6907 | (modename, h) :: rest ->
6908 let dh = findkeyhash dc modename in
6909 if always || h <> dh
6910 then (
6911 if Hashtbl.length h > 0
6912 then (
6913 if Buffer.length bb > 0
6914 then Buffer.add_char bb '\n';
6915 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6916 Hashtbl.iter (fun i o ->
6917 let isdifferent = always ||
6919 let dO = Hashtbl.find dh i in
6920 dO <> o
6921 with Not_found -> true
6923 if isdifferent
6924 then
6925 let addkm (k, m) =
6926 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6927 if Wsi.withalt m then Buffer.add_string bb "alt-";
6928 if Wsi.withshift m then Buffer.add_string bb "shift-";
6929 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6930 Buffer.add_string bb (Wsi.keyname k);
6932 let addkms l =
6933 let rec loop = function
6934 | [] -> ()
6935 | km :: [] -> addkm km
6936 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6938 loop l
6940 Buffer.add_string bb "<map in='";
6941 addkm i;
6942 match o with
6943 | KMinsrt km ->
6944 Buffer.add_string bb "' out='";
6945 addkm km;
6946 Buffer.add_string bb "'/>\n"
6948 | KMinsrl kms ->
6949 Buffer.add_string bb "' out='";
6950 addkms kms;
6951 Buffer.add_string bb "'/>\n"
6953 | KMmulti (ins, kms) ->
6954 Buffer.add_char bb ' ';
6955 addkms ins;
6956 Buffer.add_string bb "' out='";
6957 addkms kms;
6958 Buffer.add_string bb "'/>\n"
6959 ) h;
6960 Buffer.add_string bb "</keymap>";
6963 loop rest
6965 loop c.keyhashes;
6969 let save () =
6970 let uifontsize = fstate.fontsize in
6971 let bb = Buffer.create 32768 in
6972 let w, h =
6973 List.fold_left
6974 (fun (w, h) ws ->
6975 match ws with
6976 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6977 | Wsi.MaxVert -> (w, conf.cwinh)
6978 | Wsi.MaxHorz -> (conf.cwinw, h)
6980 (state.winw, state.winh) state.winstate
6982 conf.cwinw <- w;
6983 conf.cwinh <- h;
6984 let f (h, dc) =
6985 let dc = if conf.bedefault then conf else dc in
6986 Buffer.add_string bb "<llppconfig>\n";
6988 if String.length !fontpath > 0
6989 then
6990 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6991 uifontsize
6992 !fontpath
6993 else (
6994 if uifontsize <> 14
6995 then
6996 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6999 Buffer.add_string bb "<defaults ";
7000 add_attrs bb true dc dc;
7001 let kb = keymapsbuf true dc dc in
7002 if Buffer.length kb > 0
7003 then (
7004 Buffer.add_string bb ">\n";
7005 Buffer.add_buffer bb kb;
7006 Buffer.add_string bb "\n</defaults>\n";
7008 else Buffer.add_string bb "/>\n";
7010 let adddoc path pan anchor c bookmarks =
7011 if bookmarks == [] && c = dc && anchor = emptyanchor
7012 then ()
7013 else (
7014 Printf.bprintf bb "<doc path='%s'"
7015 (enent path 0 (String.length path));
7017 if anchor <> emptyanchor
7018 then (
7019 let n, rely, visy = anchor in
7020 Printf.bprintf bb " page='%d'" n;
7021 if rely > 1e-6
7022 then
7023 Printf.bprintf bb " rely='%f'" rely
7025 if abs_float visy > 1e-6
7026 then
7027 Printf.bprintf bb " visy='%f'" visy
7031 if pan != 0
7032 then Printf.bprintf bb " pan='%d'" pan;
7034 add_attrs bb false dc c;
7035 let kb = keymapsbuf false dc c in
7037 begin match bookmarks with
7038 | [] ->
7039 if Buffer.length kb > 0
7040 then (
7041 Buffer.add_string bb ">\n";
7042 Buffer.add_buffer bb kb;
7043 Buffer.add_string bb "\n</doc>\n";
7045 else Buffer.add_string bb "/>\n"
7046 | _ ->
7047 Buffer.add_string bb ">\n<bookmarks>\n";
7048 List.iter (fun (title, _level, (page, rely, visy)) ->
7049 Printf.bprintf bb
7050 "<item title='%s' page='%d'"
7051 (enent title 0 (String.length title))
7052 page
7054 if rely > 1e-6
7055 then
7056 Printf.bprintf bb " rely='%f'" rely
7058 if abs_float visy > 1e-6
7059 then
7060 Printf.bprintf bb " visy='%f'" visy
7062 Buffer.add_string bb "/>\n";
7063 ) bookmarks;
7064 Buffer.add_string bb "</bookmarks>";
7065 if Buffer.length kb > 0
7066 then (
7067 Buffer.add_string bb "\n";
7068 Buffer.add_buffer bb kb;
7070 Buffer.add_string bb "\n</doc>\n";
7071 end;
7075 let pan, conf =
7076 match state.mode with
7077 | Birdseye (c, pan, _, _, _) ->
7078 let beyecolumns =
7079 match conf.columns with
7080 | Cmulti ((c, _, _), _) -> Some c
7081 | Csingle _ -> None
7082 | Csplit _ -> None
7083 and columns =
7084 match c.columns with
7085 | Cmulti (c, _) -> Cmulti (c, [||])
7086 | Csingle _ -> Csingle [||]
7087 | Csplit _ -> failwith "quit from bird's eye while split"
7089 pan, { c with beyecolumns = beyecolumns; columns = columns }
7090 | _ -> state.x, conf
7092 let basename = Filename.basename state.origin in
7093 adddoc basename pan (getanchor ())
7094 (let conf =
7095 let autoscrollstep =
7096 match state.autoscroll with
7097 | Some step -> step
7098 | None -> conf.autoscrollstep
7100 match state.mode with
7101 | Birdseye (bc, _, _, _, _) ->
7102 { conf with
7103 zoom = bc.zoom;
7104 presentation = bc.presentation;
7105 interpagespace = bc.interpagespace;
7106 maxwait = bc.maxwait;
7107 autoscrollstep = autoscrollstep }
7108 | _ -> { conf with autoscrollstep = autoscrollstep }
7109 in conf)
7110 (if conf.savebmarks then state.bookmarks else []);
7112 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7113 if basename <> path
7114 then adddoc path x anchor c bookmarks
7115 ) h;
7116 Buffer.add_string bb "</llppconfig>\n";
7117 true;
7119 if load1 f && Buffer.length bb > 0
7120 then
7122 let tmp = !confpath ^ ".tmp" in
7123 let oc = open_out_bin tmp in
7124 Buffer.output_buffer oc bb;
7125 close_out oc;
7126 Unix.rename tmp !confpath;
7127 with exn ->
7128 prerr_endline
7129 ("error while saving configuration: " ^ exntos exn)
7131 end;;
7133 let adderrmsg src msg =
7134 Buffer.add_string state.errmsgs msg;
7135 state.newerrmsgs <- true;
7136 G.postRedisplay src
7139 let adderrfmt src fmt =
7140 Format.kprintf (fun s -> adderrmsg src s) fmt;
7143 let ract cmds =
7144 let cl = splitatspace cmds in
7145 let scan s fmt f =
7146 try Scanf.sscanf s fmt f
7147 with exn ->
7148 adderrfmt "remote exec"
7149 "error processing '%S': %s\n" cmds (exntos exn)
7151 match cl with
7152 | "reload" :: [] -> reload ()
7153 | "goto" :: args :: [] ->
7154 scan args "%u %f %f"
7155 (fun pageno x y ->
7156 let cmd, _ = state.geomcmds in
7157 if String.length cmd = 0
7158 then gotopagexy pageno x y
7159 else
7160 let f prevf () =
7161 gotopagexy pageno x y;
7162 prevf ()
7164 state.reprf <- f state.reprf
7166 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7167 | "rect" :: args :: [] ->
7168 scan args "%u %u %f %f %f %f"
7169 (fun pageno color x0 y0 x1 y1 ->
7170 onpagerect pageno (fun w h ->
7171 let _,w1,h1,_ = getpagedim pageno in
7172 let sw = float w1 /. w
7173 and sh = float h1 /. h in
7174 let x0s = x0 *. sw
7175 and x1s = x1 *. sw
7176 and y0s = y0 *. sh
7177 and y1s = y1 *. sh in
7178 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7179 debugrect rect;
7180 state.rects <- (pageno, color, rect) :: state.rects;
7181 G.postRedisplay "rect";
7184 | "activatewin" :: [] -> Wsi.activatewin ()
7185 | "quit" :: [] -> raise Quit
7186 | _ ->
7187 adderrfmt "remote command"
7188 "error processing remote command: %S\n" cmds;
7191 let remote =
7192 let scratch = String.create 80 in
7193 let buf = Buffer.create 80 in
7194 fun fd ->
7195 let rec tempfr () =
7196 try Some (Unix.read fd scratch 0 80)
7197 with
7198 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7199 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7200 | exn -> raise exn
7202 match tempfr () with
7203 | None -> Some fd
7204 | Some n ->
7205 if n = 0
7206 then (
7207 Unix.close fd;
7208 if Buffer.length buf > 0
7209 then (
7210 let s = Buffer.contents buf in
7211 Buffer.clear buf;
7212 ract s;
7214 None
7216 else
7217 let rec eat ppos =
7218 let nlpos =
7220 let pos = String.index_from scratch ppos '\n' in
7221 if pos >= n then -1 else pos
7222 with Not_found -> -1
7224 if nlpos >= 0
7225 then (
7226 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7227 let s = Buffer.contents buf in
7228 Buffer.clear buf;
7229 ract s;
7230 eat (nlpos+1);
7232 else (
7233 Buffer.add_substring buf scratch ppos (n-ppos);
7234 Some fd
7236 in eat 0
7239 let remoteopen path =
7240 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7241 with exn ->
7242 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7243 None
7246 let () =
7247 let trimcachepath = ref "" in
7248 let rcmdpath = ref "" in
7249 Arg.parse
7250 (Arg.align
7251 [("-p", Arg.String (fun s -> state.password <- s),
7252 "<password> Set password");
7254 ("-f", Arg.String (fun s -> Config.fontpath := s),
7255 "<path> Set path to the user interface font");
7257 ("-c", Arg.String (fun s -> Config.confpath := s),
7258 "<path> Set path to the configuration file");
7260 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7261 "<path> Set path to the trim cache file");
7263 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7264 "<named-destination> Set named destination");
7266 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7268 ("-remote", Arg.String (fun s -> rcmdpath := s),
7269 "<path> Set path to the remote commands source");
7271 ("-origin", Arg.String (fun s -> state.origin <- s),
7272 "<original path> Set original path");
7274 ("-v", Arg.Unit (fun () ->
7275 Printf.printf
7276 "%s\nconfiguration path: %s\n"
7277 (version ())
7278 Config.defconfpath
7280 exit 0), " Print version and exit");
7283 (fun s -> state.path <- s)
7284 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7286 if String.length state.path = 0
7287 then (prerr_endline "file name missing"; exit 1);
7289 if not (Config.load ())
7290 then prerr_endline "failed to load configuration";
7292 let globalkeyhash = findkeyhash conf "global" in
7293 let wsfd, winw, winh = Wsi.init (object
7294 method expose =
7295 if nogeomcmds state.geomcmds || platform == Posx
7296 then display ()
7297 else (
7298 GlClear.color (scalecolor2 conf.bgcolor);
7299 GlClear.clear [`color];
7301 method display = display ()
7302 method reshape w h = reshape w h
7303 method mouse b d x y m = mouse b d x y m
7304 method motion x y = state.mpos <- (x, y); motion x y
7305 method pmotion x y = state.mpos <- (x, y); pmotion x y
7306 method key k m =
7307 let mascm = m land (
7308 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7309 ) in
7310 match state.keystate with
7311 | KSnone ->
7312 let km = k, mascm in
7313 begin
7314 match
7315 let modehash = state.uioh#modehash in
7316 try Hashtbl.find modehash km
7317 with Not_found ->
7318 try Hashtbl.find globalkeyhash km
7319 with Not_found -> KMinsrt (k, m)
7320 with
7321 | KMinsrt (k, m) -> keyboard k m
7322 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7323 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7325 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7326 List.iter (fun (k, m) -> keyboard k m) insrt;
7327 state.keystate <- KSnone
7328 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7329 state.keystate <- KSinto (keys, insrt)
7330 | _ ->
7331 state.keystate <- KSnone
7333 method enter x y = state.mpos <- (x, y); pmotion x y
7334 method leave = state.mpos <- (-1, -1)
7335 method winstate wsl = state.winstate <- wsl
7336 method quit = raise Quit
7337 end) conf.cwinw conf.cwinh (platform = Posx) in
7339 state.wsfd <- wsfd;
7341 if not (
7342 List.exists GlMisc.check_extension
7343 [ "GL_ARB_texture_rectangle"
7344 ; "GL_EXT_texture_recangle"
7345 ; "GL_NV_texture_rectangle" ]
7347 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7349 let cr, sw =
7350 match Ne.pipe () with
7351 | Ne.Exn exn ->
7352 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7353 exit 1
7354 | Ne.Res rw -> rw
7355 and sr, cw =
7356 match Ne.pipe () with
7357 | Ne.Exn exn ->
7358 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7359 exit 1
7360 | Ne.Res rw -> rw
7363 cloexec cr;
7364 cloexec sw;
7365 cloexec sr;
7366 cloexec cw;
7368 setcheckers conf.checkers;
7369 redirectstderr ();
7371 init (cr, cw) (
7372 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7373 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7374 !Config.fontpath, !trimcachepath,
7375 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7377 state.sr <- sr;
7378 state.sw <- sw;
7379 state.text <- "Opening " ^ (mbtoutf8 state.path);
7380 reshape winw winh;
7381 opendoc state.path state.password;
7382 state.uioh <- uioh;
7384 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7385 let optrfd =
7386 ref (
7387 if String.length !rcmdpath > 0
7388 then remoteopen !rcmdpath
7389 else None
7393 let rec loop deadline =
7394 let r =
7395 match state.errfd with
7396 | None -> [state.sr; state.wsfd]
7397 | Some fd -> [state.sr; state.wsfd; fd]
7399 let r =
7400 match !optrfd with
7401 | None -> r
7402 | Some fd -> fd :: r
7404 if state.redisplay
7405 then (
7406 state.redisplay <- false;
7407 display ();
7409 let timeout =
7410 let now = now () in
7411 if deadline > now
7412 then (
7413 if deadline = infinity
7414 then ~-.1.0
7415 else max 0.0 (deadline -. now)
7417 else 0.0
7419 let r, _, _ =
7420 try Unix.select r [] [] timeout
7421 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7423 begin match r with
7424 | [] ->
7425 state.ghyll None;
7426 let newdeadline =
7427 if state.ghyll == noghyll
7428 then
7429 match state.autoscroll with
7430 | Some step when step != 0 ->
7431 let y = state.y + step in
7432 let y =
7433 if y < 0
7434 then state.maxy
7435 else if y >= state.maxy then 0 else y
7437 gotoy y;
7438 if state.mode = View
7439 then state.text <- "";
7440 deadline +. 0.01
7441 | _ -> infinity
7442 else deadline +. 0.01
7444 loop newdeadline
7446 | l ->
7447 let rec checkfds = function
7448 | [] -> ()
7449 | fd :: rest when fd = state.sr ->
7450 let cmd = readcmd state.sr in
7451 act cmd;
7452 checkfds rest
7454 | fd :: rest when fd = state.wsfd ->
7455 Wsi.readresp fd;
7456 checkfds rest
7458 | fd :: rest when Some fd = !optrfd ->
7459 begin match remote fd with
7460 | None -> optrfd := remoteopen !rcmdpath;
7461 | opt -> optrfd := opt
7462 end;
7463 checkfds rest
7465 | fd :: rest ->
7466 let s = String.create 80 in
7467 let n = tempfailureretry (Unix.read fd s 0) 80 in
7468 if conf.redirectstderr
7469 then (
7470 Buffer.add_substring state.errmsgs s 0 n;
7471 state.newerrmsgs <- true;
7472 state.redisplay <- true;
7474 else (
7475 prerr_string (String.sub s 0 n);
7476 flush stderr;
7478 checkfds rest
7480 checkfds l;
7481 let newdeadline =
7482 let deadline1 =
7483 if deadline = infinity
7484 then now () +. 0.01
7485 else deadline
7487 match state.autoscroll with
7488 | Some step when step != 0 -> deadline1
7489 | _ -> if state.ghyll == noghyll then infinity else deadline1
7491 loop newdeadline
7492 end;
7495 loop infinity;
7496 with Quit ->
7497 Config.save ();