Memoize layout when gcing
[llpp.git] / main.ml
blobe3adfa7c0764b90c68103b69aa2f7cfba191bd0d
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath * trimcachepath)
20 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 proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = 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
44 type link =
45 | Lnotfound
46 | Lfound of int
47 and linkdir =
48 | LDfirst
49 | LDlast
50 | LDfirstvisible of (int * int * int)
51 | LDleft of int
52 | LDright of int
53 | LDdown of int
54 | LDup of int
57 type pagewithlinks =
58 | Pwlnotfound
59 | Pwl of int
62 type keymap =
63 | KMinsrt of key
64 | KMinsrl of key list
65 | KMmulti of key list * key list
66 and key = int * int
67 and keyhash = (key, keymap) Hashtbl.t
68 and keystate =
69 | KSnone
70 | KSinto of (key list * key list)
73 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
74 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
76 type pipe = (Unix.file_descr * Unix.file_descr);;
78 external init : pipe -> params -> unit = "ml_init";;
79 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
80 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
81 external getpdimrect : int -> float array = "ml_getpdimrect";;
82 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
83 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
84 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
85 external measurestr : int -> string -> float = "ml_measure_string";;
86 external getmaxw : unit -> float = "ml_getmaxw";;
87 external postprocess :
88 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
89 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
90 external platform : unit -> platform = "ml_platform";;
91 external setaalevel : int -> unit = "ml_setaalevel";;
92 external realloctexts : int -> bool = "ml_realloctexts";;
93 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
94 external findlink : opaque -> linkdir -> link = "ml_findlink";;
95 external getlink : opaque -> int -> under = "ml_getlink";;
96 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
97 external getlinkcount : opaque -> int = "ml_getlinkcount";;
98 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
99 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
101 let platform_to_string = function
102 | Punknown -> "unknown"
103 | Plinux -> "Linux"
104 | Posx -> "OSX"
105 | Psun -> "Sun"
106 | Pfreebsd -> "FreeBSD"
107 | Pdragonflybsd -> "DragonflyBSD"
108 | Popenbsd -> "OpenBSD"
109 | Pnetbsd -> "NetBSD"
110 | Pcygwin -> "Cygwin"
113 let platform = platform ();;
115 let popen cmd fda =
116 if platform = Pcygwin
117 then (
118 let sh = "/bin/sh" in
119 let args = [|sh; "-c"; cmd|] in
120 let rec std si so se = function
121 | [] -> si, so, se
122 | (fd, 0) :: rest -> std fd so se rest
123 | (fd, -1) :: rest ->
124 Unix.set_close_on_exec fd;
125 std si so se rest
126 | (_, n) :: _ ->
127 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
129 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
130 ignore (Unix.create_process sh args si so se)
132 else popen cmd fda;
135 type x = int
136 and y = int
137 and tilex = int
138 and tiley = int
139 and tileparams = (x * y * width * height * tilex * tiley)
142 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
144 type mpos = int * int
145 and mstate =
146 | Msel of (mpos * mpos)
147 | Mpan of mpos
148 | Mscrolly | Mscrollx
149 | Mzoom of (int * int)
150 | Mzoomrect of (mpos * mpos)
151 | Mnone
154 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
155 and onkey = string -> int -> te
156 and ondone = string -> unit
157 and histcancel = unit -> unit
158 and onhist = ((histcmd -> string) * histcancel)
159 and histcmd = HCnext | HCprev | HCfirst | HClast
160 and cancelonempty = bool
161 and te =
162 | TEstop
163 | TEdone of string
164 | TEcont of string
165 | TEswitch of textentry
168 type 'a circbuf =
169 { store : 'a array
170 ; mutable rc : int
171 ; mutable wc : int
172 ; mutable len : int
176 let bound v minv maxv =
177 max minv (min maxv v);
180 let cbnew n v =
181 { store = Array.create n v
182 ; rc = 0
183 ; wc = 0
184 ; len = 0
188 let drawstring size x y s =
189 Gl.enable `blend;
190 Gl.enable `texture_2d;
191 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
192 ignore (drawstr size x y s);
193 Gl.disable `blend;
194 Gl.disable `texture_2d;
197 let drawstring1 size x y s =
198 drawstr size x y s;
201 let drawstring2 size x y fmt =
202 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
205 let cbcap b = Array.length b.store;;
207 let cbput b v =
208 let cap = cbcap b in
209 b.store.(b.wc) <- v;
210 b.wc <- (b.wc + 1) mod cap;
211 b.rc <- b.wc;
212 b.len <- min (b.len + 1) cap;
215 let cbempty b = b.len = 0;;
217 let cbgetg b circular dir =
218 if cbempty b
219 then b.store.(0)
220 else
221 let rc = b.rc + dir in
222 let rc =
223 if circular
224 then (
225 if rc = -1
226 then b.len-1
227 else (
228 if rc = b.len
229 then 0
230 else rc
233 else max 0 (min rc (b.len-1))
235 b.rc <- rc;
236 b.store.(rc);
239 let cbget b = cbgetg b false;;
240 let cbgetc b = cbgetg b true;;
242 type page =
243 { pageno : int
244 ; pagedimno : int
245 ; pagew : int
246 ; pageh : int
247 ; pagex : int
248 ; pagey : int
249 ; pagevw : int
250 ; pagevh : int
251 ; pagedispx : int
252 ; pagedispy : int
253 ; pagecol : int
257 let debugl l =
258 dolog "l %d dim=%d {" l.pageno l.pagedimno;
259 dolog " WxH %dx%d" l.pagew l.pageh;
260 dolog " vWxH %dx%d" l.pagevw l.pagevh;
261 dolog " pagex,y %d,%d" l.pagex l.pagey;
262 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
263 dolog " column %d" l.pagecol;
264 dolog "}";
267 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
268 dolog "rect {";
269 dolog " x0,y0=(% f, % f)" x0 y0;
270 dolog " x1,y1=(% f, % f)" x1 y1;
271 dolog " x2,y2=(% f, % f)" x2 y2;
272 dolog " x3,y3=(% f, % f)" x3 y3;
273 dolog "}";
276 type multicolumns = multicol * pagegeom
277 and singlecolumn = pagegeom
278 and splitcolumns = columncount * pagegeom
279 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
280 and multicol = columncount * covercount * covercount
281 and pdimno = int
282 and columncount = int
283 and covercount = int;;
285 type conf =
286 { mutable scrollbw : int
287 ; mutable scrollh : int
288 ; mutable icase : bool
289 ; mutable preload : bool
290 ; mutable pagebias : int
291 ; mutable verbose : bool
292 ; mutable debug : bool
293 ; mutable scrollstep : int
294 ; mutable hscrollstep : int
295 ; mutable maxhfit : bool
296 ; mutable crophack : bool
297 ; mutable autoscrollstep : int
298 ; mutable maxwait : float option
299 ; mutable hlinks : bool
300 ; mutable underinfo : bool
301 ; mutable interpagespace : interpagespace
302 ; mutable zoom : float
303 ; mutable presentation : bool
304 ; mutable angle : angle
305 ; mutable winw : int
306 ; mutable winh : int
307 ; mutable savebmarks : bool
308 ; mutable proportional : proportional
309 ; mutable trimmargins : trimmargins
310 ; mutable trimfuzz : irect
311 ; mutable memlimit : memsize
312 ; mutable texcount : texcount
313 ; mutable sliceheight : sliceheight
314 ; mutable thumbw : width
315 ; mutable jumpback : bool
316 ; mutable bgcolor : float * float * float
317 ; mutable bedefault : bool
318 ; mutable scrollbarinpm : bool
319 ; mutable tilew : int
320 ; mutable tileh : int
321 ; mutable mustoresize : memsize
322 ; mutable checkers : bool
323 ; mutable aalevel : int
324 ; mutable urilauncher : string
325 ; mutable pathlauncher : string
326 ; mutable colorspace : colorspace
327 ; mutable invert : bool
328 ; mutable colorscale : float
329 ; mutable redirectstderr : bool
330 ; mutable ghyllscroll : (int * int * int) option
331 ; mutable columns : columns
332 ; mutable beyecolumns : columncount option
333 ; mutable selcmd : string
334 ; mutable updatecurs : bool
335 ; mutable keyhashes : (string * keyhash) list
336 ; mutable hfsize : int
337 ; mutable pgscale : float
339 and columns =
340 | Csingle of singlecolumn
341 | Cmulti of multicolumns
342 | Csplit of splitcolumns
345 type anchor = pageno * top;;
347 type outline = string * int * anchor;;
349 type rect = float * float * float * float * float * float * float * float;;
351 type tile = opaque * pixmapsize * elapsed
352 and elapsed = float;;
353 type pagemapkey = pageno * gen;;
354 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
355 and row = int
356 and col = int;;
358 let emptyanchor = (0, 0.0);;
360 type infochange = | Memused | Docinfo | Pdim;;
362 class type uioh = object
363 method display : unit
364 method key : int -> int -> uioh
365 method button : int -> bool -> int -> int -> int -> uioh
366 method motion : int -> int -> uioh
367 method pmotion : int -> int -> uioh
368 method infochanged : infochange -> unit
369 method scrollpw : (int * float * float)
370 method scrollph : (int * float * float)
371 method modehash : keyhash
372 end;;
374 type mode =
375 | Birdseye of (conf * leftx * pageno * pageno * anchor)
376 | Textentry of (textentry * onleave)
377 | View
378 | LinkNav of linktarget
379 and onleave = leavetextentrystatus -> unit
380 and leavetextentrystatus = | Cancel | Confirm
381 and helpitem = string * int * action
382 and action =
383 | Noaction
384 | Action of (uioh -> uioh)
385 and linktarget =
386 | Ltexact of (pageno * int)
387 | Ltgendir of int
390 let isbirdseye = function Birdseye _ -> true | _ -> false;;
391 let istextentry = function Textentry _ -> true | _ -> false;;
393 type currently =
394 | Idle
395 | Loading of (page * gen)
396 | Tiling of (
397 page * opaque * colorspace * angle * gen * col * row * width * height
399 | Outlining of outline list
402 let emptykeyhash = Hashtbl.create 0;;
403 let nouioh : uioh = object (self)
404 method display = ()
405 method key _ _ = self
406 method button _ _ _ _ _ = self
407 method motion _ _ = self
408 method pmotion _ _ = self
409 method infochanged _ = ()
410 method scrollpw = (0, nan, nan)
411 method scrollph = (0, nan, nan)
412 method modehash = emptykeyhash
413 end;;
415 type state =
416 { mutable sr : Unix.file_descr
417 ; mutable sw : Unix.file_descr
418 ; mutable wsfd : Unix.file_descr
419 ; mutable errfd : Unix.file_descr option
420 ; mutable stderr : Unix.file_descr
421 ; mutable errmsgs : Buffer.t
422 ; mutable newerrmsgs : bool
423 ; mutable w : int
424 ; mutable x : int
425 ; mutable y : int
426 ; mutable scrollw : int
427 ; mutable hscrollh : int
428 ; mutable anchor : anchor
429 ; mutable ranchors : (string * string * anchor) list
430 ; mutable maxy : int
431 ; mutable layout : page list
432 ; pagemap : (pagemapkey, opaque) Hashtbl.t
433 ; tilemap : (tilemapkey, tile) Hashtbl.t
434 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
435 ; mutable pdims : (pageno * width * height * leftx) list
436 ; mutable pagecount : int
437 ; mutable currently : currently
438 ; mutable mstate : mstate
439 ; mutable searchpattern : string
440 ; mutable rects : (pageno * recttype * rect) list
441 ; mutable rects1 : (pageno * recttype * rect) list
442 ; mutable text : string
443 ; mutable fullscreen : (width * height) option
444 ; mutable mode : mode
445 ; mutable uioh : uioh
446 ; mutable outlines : outline array
447 ; mutable bookmarks : outline list
448 ; mutable path : string
449 ; mutable password : string
450 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
451 ; mutable memused : memsize
452 ; mutable gen : gen
453 ; mutable throttle : (page list * int * float) option
454 ; mutable autoscroll : int option
455 ; mutable ghyll : (int option -> unit)
456 ; mutable help : helpitem array
457 ; mutable docinfo : (int * string) list
458 ; mutable texid : GlTex.texture_id option
459 ; hists : hists
460 ; mutable prevzoom : float
461 ; mutable progress : float
462 ; mutable redisplay : bool
463 ; mutable mpos : mpos
464 ; mutable keystate : keystate
465 ; mutable glinks : bool
466 ; mutable prevcolumns : (columns * float) option
468 and hists =
469 { pat : string circbuf
470 ; pag : string circbuf
471 ; nav : anchor circbuf
472 ; sel : string circbuf
476 let defconf =
477 { scrollbw = 7
478 ; scrollh = 12
479 ; icase = true
480 ; preload = true
481 ; pagebias = 0
482 ; verbose = false
483 ; debug = false
484 ; scrollstep = 24
485 ; hscrollstep = 24
486 ; maxhfit = true
487 ; crophack = false
488 ; autoscrollstep = 2
489 ; maxwait = None
490 ; hlinks = false
491 ; underinfo = false
492 ; interpagespace = 2
493 ; zoom = 1.0
494 ; presentation = false
495 ; angle = 0
496 ; winw = 900
497 ; winh = 900
498 ; savebmarks = true
499 ; proportional = true
500 ; trimmargins = false
501 ; trimfuzz = (0,0,0,0)
502 ; memlimit = 32 lsl 20
503 ; texcount = 256
504 ; sliceheight = 24
505 ; thumbw = 76
506 ; jumpback = true
507 ; bgcolor = (0.5, 0.5, 0.5)
508 ; bedefault = false
509 ; scrollbarinpm = true
510 ; tilew = 2048
511 ; tileh = 2048
512 ; mustoresize = 256 lsl 20
513 ; checkers = true
514 ; aalevel = 8
515 ; urilauncher =
516 (match platform with
517 | Plinux | Pfreebsd | Pdragonflybsd
518 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
519 | Posx -> "open \"%s\""
520 | Pcygwin -> "cygstart \"%s\""
521 | Punknown -> "echo %s")
522 ; pathlauncher = "lp \"%s\""
523 ; selcmd =
524 (match platform with
525 | Plinux | Pfreebsd | Pdragonflybsd
526 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
527 | Posx -> "pbcopy"
528 | Pcygwin -> "wsel"
529 | Punknown -> "cat")
530 ; colorspace = Rgb
531 ; invert = false
532 ; colorscale = 1.0
533 ; redirectstderr = false
534 ; ghyllscroll = None
535 ; columns = Csingle [||]
536 ; beyecolumns = None
537 ; updatecurs = false
538 ; hfsize = 12
539 ; pgscale = 1.0
540 ; keyhashes =
541 let mk n = (n, Hashtbl.create 1) in
542 [ mk "global"
543 ; mk "info"
544 ; mk "help"
545 ; mk "outline"
546 ; mk "listview"
547 ; mk "birdseye"
548 ; mk "textentry"
549 ; mk "links"
550 ; mk "view"
555 let findkeyhash c name =
556 try List.assoc name c.keyhashes
557 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
560 let conf = { defconf with angle = defconf.angle };;
562 let pgscale h = truncate (float h *. conf.pgscale);;
564 type fontstate =
565 { mutable fontsize : int
566 ; mutable wwidth : float
567 ; mutable maxrows : int
571 let fstate =
572 { fontsize = 14
573 ; wwidth = nan
574 ; maxrows = -1
578 let setfontsize n =
579 fstate.fontsize <- n;
580 fstate.wwidth <- measurestr fstate.fontsize "w";
581 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
584 let geturl s =
585 let colonpos = try String.index s ':' with Not_found -> -1 in
586 let len = String.length s in
587 if colonpos >= 0 && colonpos + 3 < len
588 then (
589 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
590 then
591 let schemestartpos =
592 try String.rindex_from s colonpos ' '
593 with Not_found -> -1
595 let scheme =
596 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
598 match scheme with
599 | "http" | "ftp" | "mailto" ->
600 let epos =
601 try String.index_from s colonpos ' '
602 with Not_found -> len
604 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
605 | _ -> ""
606 else ""
608 else ""
611 let gotouri uri =
612 if String.length conf.urilauncher = 0
613 then print_endline uri
614 else (
615 let url = geturl uri in
616 if String.length url = 0
617 then print_endline uri
618 else
619 let re = Str.regexp "%s" in
620 let command = Str.global_replace re url conf.urilauncher in
621 try popen command []
622 with exn ->
623 Printf.eprintf
624 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
625 flush stderr;
629 let version () =
630 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
631 (platform_to_string platform) Sys.word_size Sys.ocaml_version
634 let makehelp () =
635 let strings = version () :: "" :: Help.keys in
636 Array.of_list (
637 List.map (fun s ->
638 let url = geturl s in
639 if String.length url > 0
640 then (s, 0, Action (fun u -> gotouri url; u))
641 else (s, 0, Noaction)
642 ) strings);
645 let noghyll _ = ();;
646 let firstgeomcmds = "", [];;
648 let state =
649 { sr = Unix.stdin
650 ; sw = Unix.stdin
651 ; wsfd = Unix.stdin
652 ; errfd = None
653 ; stderr = Unix.stderr
654 ; errmsgs = Buffer.create 0
655 ; newerrmsgs = false
656 ; x = 0
657 ; y = 0
658 ; w = 0
659 ; scrollw = 0
660 ; hscrollh = 0
661 ; anchor = emptyanchor
662 ; ranchors = []
663 ; layout = []
664 ; maxy = max_int
665 ; tilelru = Queue.create ()
666 ; pagemap = Hashtbl.create 10
667 ; tilemap = Hashtbl.create 10
668 ; pdims = []
669 ; pagecount = 0
670 ; currently = Idle
671 ; mstate = Mnone
672 ; rects = []
673 ; rects1 = []
674 ; text = ""
675 ; mode = View
676 ; fullscreen = None
677 ; searchpattern = ""
678 ; outlines = [||]
679 ; bookmarks = []
680 ; path = ""
681 ; password = ""
682 ; geomcmds = firstgeomcmds
683 ; hists =
684 { nav = cbnew 10 (0, 0.0)
685 ; pat = cbnew 10 ""
686 ; pag = cbnew 10 ""
687 ; sel = cbnew 10 ""
689 ; memused = 0
690 ; gen = 0
691 ; throttle = None
692 ; autoscroll = None
693 ; ghyll = noghyll
694 ; help = makehelp ()
695 ; docinfo = []
696 ; texid = None
697 ; prevzoom = 1.0
698 ; progress = -1.0
699 ; uioh = nouioh
700 ; redisplay = true
701 ; mpos = (-1, -1)
702 ; keystate = KSnone
703 ; glinks = false
704 ; prevcolumns = None
708 let vlog fmt =
709 if conf.verbose
710 then
711 Printf.kprintf prerr_endline fmt
712 else
713 Printf.kprintf ignore fmt
716 let launchpath () =
717 if String.length conf.pathlauncher = 0
718 then print_endline state.path
719 else (
720 let re = Str.regexp "%s" in
721 let command = Str.global_replace re state.path conf.pathlauncher in
722 try popen command []
723 with exn ->
724 Printf.eprintf
725 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
726 flush stderr;
730 module Ne = struct
731 type 'a t = | Res of 'a | Exn of exn;;
733 let pipe () =
734 try Res (Unix.pipe ())
735 with exn -> Exn exn
738 let clo fd f =
739 try Unix.close fd
740 with exn -> f (Printexc.to_string exn)
743 let dup fd =
744 try Res (Unix.dup fd)
745 with exn -> Exn exn
748 let dup2 fd1 fd2 =
749 try Res (Unix.dup2 fd1 fd2)
750 with exn -> Exn exn
752 end;;
754 let redirectstderr () =
755 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
756 if conf.redirectstderr
757 then
758 match Ne.pipe () with
759 | Ne.Exn exn ->
760 dolog "failed to create stderr redirection pipes: %s"
761 (Printexc.to_string exn)
763 | Ne.Res (r, w) ->
764 begin match Ne.dup Unix.stderr with
765 | Ne.Exn exn ->
766 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
767 Ne.clo r (clofail "pipe/r");
768 Ne.clo w (clofail "pipe/w");
770 | Ne.Res dupstderr ->
771 begin match Ne.dup2 w Unix.stderr with
772 | Ne.Exn exn ->
773 dolog "failed to dup2 to stderr: %s"
774 (Printexc.to_string exn);
775 Ne.clo dupstderr (clofail "stderr duplicate");
776 Ne.clo r (clofail "redir pipe/r");
777 Ne.clo w (clofail "redir pipe/w");
779 | Ne.Res () ->
780 state.stderr <- dupstderr;
781 state.errfd <- Some r;
782 end;
784 else (
785 state.newerrmsgs <- false;
786 begin match state.errfd with
787 | Some fd ->
788 begin match Ne.dup2 state.stderr Unix.stderr with
789 | Ne.Exn exn ->
790 dolog "failed to dup2 original stderr: %s"
791 (Printexc.to_string exn)
792 | Ne.Res () ->
793 Ne.clo fd (clofail "dup of stderr");
794 Unix.dup2 state.stderr Unix.stderr;
795 state.errfd <- None;
796 end;
797 | None -> ()
798 end;
799 prerr_string (Buffer.contents state.errmsgs);
800 flush stderr;
801 Buffer.clear state.errmsgs;
805 module G =
806 struct
807 let postRedisplay who =
808 if conf.verbose
809 then prerr_endline ("redisplay for " ^ who);
810 state.redisplay <- true;
812 end;;
814 let getopaque pageno =
815 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
816 with Not_found -> None
819 let putopaque pageno opaque =
820 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
823 let pagetranslatepoint l x y =
824 let dy = y - l.pagedispy in
825 let y = dy + l.pagey in
826 let dx = x - l.pagedispx in
827 let x = dx + l.pagex in
828 (x, y);
831 let getunder x y =
832 let rec f = function
833 | l :: rest ->
834 begin match getopaque l.pageno with
835 | Some opaque ->
836 let x0 = l.pagedispx in
837 let x1 = x0 + l.pagevw in
838 let y0 = l.pagedispy in
839 let y1 = y0 + l.pagevh in
840 if y >= y0 && y <= y1 && x >= x0 && x <= x1
841 then
842 let px, py = pagetranslatepoint l x y in
843 match whatsunder opaque px py with
844 | Unone -> f rest
845 | under -> under
846 else f rest
847 | _ ->
848 f rest
850 | [] -> Unone
852 f state.layout
855 let showtext c s =
856 state.text <- Printf.sprintf "%c%s" c s;
857 G.postRedisplay "showtext";
860 let undertext = function
861 | Unone -> "none"
862 | Ulinkuri s -> s
863 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
864 | Utext s -> "font: " ^ s
865 | Uunexpected s -> "unexpected: " ^ s
866 | Ulaunch s -> "launch: " ^ s
867 | Unamed s -> "named: " ^ s
868 | Uremote (filename, pageno) ->
869 Printf.sprintf "%s: page %d" filename (pageno+1)
872 let updateunder x y =
873 match getunder x y with
874 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
875 | Ulinkuri uri ->
876 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
877 Wsi.setcursor Wsi.CURSOR_INFO
878 | Ulinkgoto (pageno, _) ->
879 if conf.underinfo
880 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
881 Wsi.setcursor Wsi.CURSOR_INFO
882 | Utext s ->
883 if conf.underinfo then showtext 'f' ("ont: " ^ s);
884 Wsi.setcursor Wsi.CURSOR_TEXT
885 | Uunexpected s ->
886 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
887 Wsi.setcursor Wsi.CURSOR_INHERIT
888 | Ulaunch s ->
889 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
890 Wsi.setcursor Wsi.CURSOR_INHERIT
891 | Unamed s ->
892 if conf.underinfo then showtext 'n' ("amed: " ^ s);
893 Wsi.setcursor Wsi.CURSOR_INHERIT
894 | Uremote (filename, pageno) ->
895 if conf.underinfo then showtext 'r'
896 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
897 Wsi.setcursor Wsi.CURSOR_INFO
900 let showlinktype under =
901 if conf.underinfo
902 then
903 match under with
904 | Unone -> ()
905 | under ->
906 let s = undertext under in
907 showtext ' ' s
910 let addchar s c =
911 let b = Buffer.create (String.length s + 1) in
912 Buffer.add_string b s;
913 Buffer.add_char b c;
914 Buffer.contents b;
917 let colorspace_of_string s =
918 match String.lowercase s with
919 | "rgb" -> Rgb
920 | "bgr" -> Bgr
921 | "gray" -> Gray
922 | _ -> failwith "invalid colorspace"
925 let int_of_colorspace = function
926 | Rgb -> 0
927 | Bgr -> 1
928 | Gray -> 2
931 let colorspace_of_int = function
932 | 0 -> Rgb
933 | 1 -> Bgr
934 | 2 -> Gray
935 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
938 let colorspace_to_string = function
939 | Rgb -> "rgb"
940 | Bgr -> "bgr"
941 | Gray -> "gray"
944 let intentry_with_suffix text key =
945 let c =
946 if key >= 32 && key < 127
947 then Char.chr key
948 else '\000'
950 match Char.lowercase c with
951 | '0' .. '9' ->
952 let text = addchar text c in
953 TEcont text
955 | 'k' | 'm' | 'g' ->
956 let text = addchar text c in
957 TEcont text
959 | _ ->
960 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
961 TEcont text
964 let multicolumns_to_string (n, a, b) =
965 if a = 0 && b = 0
966 then Printf.sprintf "%d" n
967 else Printf.sprintf "%d,%d,%d" n a b;
970 let multicolumns_of_string s =
972 (int_of_string s, 0, 0)
973 with _ ->
974 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
977 let readcmd fd =
978 let s = "xxxx" in
979 let n = Unix.read fd s 0 4 in
980 if n != 4 then failwith "incomplete read(len)";
981 let len = 0
982 lor (Char.code s.[0] lsl 24)
983 lor (Char.code s.[1] lsl 16)
984 lor (Char.code s.[2] lsl 8)
985 lor (Char.code s.[3] lsl 0)
987 let s = String.create len in
988 let n = Unix.read fd s 0 len in
989 if n != len then failwith "incomplete read(data)";
993 let btod b = if b then 1 else 0;;
995 let wcmd fmt =
996 let b = Buffer.create 16 in
997 Buffer.add_string b "llll";
998 Printf.kbprintf
999 (fun b ->
1000 let s = Buffer.contents b in
1001 let n = String.length s in
1002 let len = n - 4 in
1003 (* dolog "wcmd %S" (String.sub s 4 len); *)
1004 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1005 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1006 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1007 s.[3] <- Char.chr (len land 0xff);
1008 let n' = Unix.write state.sw s 0 n in
1009 if n' != n then failwith "write failed";
1010 ) b fmt;
1013 let calcips h =
1014 if conf.presentation
1015 then
1016 let d = conf.winh - h in
1017 max conf.interpagespace ((d + 1) / 2)
1018 else
1019 conf.interpagespace
1022 let calcheight () =
1023 match conf.columns with
1024 | Cmulti ((c, _, _), b) ->
1025 let rec loop y h n =
1026 if n < 0
1027 then loop y h (n+1)
1028 else (
1029 if n = Array.length b
1030 then y + h
1031 else
1032 let (_, _, y', (_, _, h', _)) = b.(n) in
1033 let y = min y y'
1034 and h = max h h' in
1035 loop y h (n+1)
1038 loop max_int 0 (((Array.length b - 1) / c) * c)
1039 | Csingle b ->
1040 if Array.length b > 0
1041 then
1042 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1043 y + h + (if conf.presentation then calcips h else 0)
1044 else 0
1045 | Csplit (_, b) ->
1046 if Array.length b > 0
1047 then
1048 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1049 y + h
1050 else 0
1053 let getpageyh pageno =
1054 let pageno = bound pageno 0 state.pagecount in
1055 match conf.columns with
1056 | Csingle b ->
1057 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1058 let y =
1059 if conf.presentation
1060 then y - calcips h
1061 else y
1063 y, h
1064 | Cmulti (_, b) ->
1065 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1066 y, h
1067 | Csplit (c, b) ->
1068 let n = pageno*c in
1069 let (_, _, y, (_, _, h, _)) = b.(n) in
1070 y, h
1073 let getpagedim pageno =
1074 let rec f ppdim l =
1075 match l with
1076 | (n, _, _, _) as pdim :: rest ->
1077 if n >= pageno
1078 then (if n = pageno then pdim else ppdim)
1079 else f pdim rest
1081 | [] -> ppdim
1083 f (-1, -1, -1, -1) state.pdims
1086 let getpagey pageno = fst (getpageyh pageno);;
1088 let nogeomcmds cmds =
1089 match cmds with
1090 | s, [] -> String.length s = 0
1091 | _ -> false
1094 let layout1 b y sh =
1095 let sh = sh - state.hscrollh in
1096 let rec fold accu n =
1097 if n = Array.length b
1098 then accu
1099 else
1100 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1101 if (vy - y) > sh
1102 then accu
1103 else
1104 let accu =
1105 if vy + h > y
1106 then
1107 let pagey = max 0 (y - vy) in
1108 let pagedispy = if pagey > 0 then 0 else vy - y in
1109 let pagedispx, pagex =
1110 let pdx = dx + xoff + state.x in
1111 if pdx < 0
1112 then 0, -pdx
1113 else pdx, 0
1115 let pagevw =
1116 let vw = conf.winw - state.scrollw - pagedispx in
1117 let pw = w - pagex in
1118 min vw pw
1120 let pagevh = min (h - pagey) (sh - pagedispy) in
1121 if pagevw > 0 && pagevh > 0
1122 then
1123 let e =
1124 { pageno = n
1125 ; pagedimno = pdimno
1126 ; pagew = w
1127 ; pageh = h
1128 ; pagex = pagex
1129 ; pagey = pagey
1130 ; pagevw = pagevw
1131 ; pagevh = pagevh
1132 ; pagedispx = pagedispx
1133 ; pagedispy = pagedispy
1134 ; pagecol = 0
1137 e :: accu
1138 else
1139 accu
1140 else
1141 accu
1143 fold accu (n+1)
1145 List.rev (fold [] 0)
1148 let layoutN ((columns, coverA, coverB), b) y sh =
1149 let sh = sh - state.hscrollh in
1150 let rec fold accu n =
1151 if n = Array.length b
1152 then accu
1153 else
1154 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1155 if (vy - y) > sh &&
1156 (n = coverA - 1
1157 || n = state.pagecount - coverB
1158 || (n - coverA) mod columns = columns - 1)
1159 then accu
1160 else
1161 let accu =
1162 if vy + h > y
1163 then
1164 let pagey = max 0 (y - vy) in
1165 let pagedispy = if pagey > 0 then 0 else vy - y in
1166 let pagedispx, pagex =
1167 let pdx =
1168 if n = coverA - 1 || n = state.pagecount - coverB
1169 then state.x + (conf.winw - state.scrollw - w) / 2
1170 else dx + xoff + state.x
1172 if pdx < 0
1173 then 0, -pdx
1174 else pdx, 0
1176 let pagevw =
1177 let vw = conf.winw - state.scrollw - pagedispx in
1178 let pw = w - pagex in
1179 min vw pw
1181 let pagevh = min (h - pagey) (sh - pagedispy) in
1182 if pagevw > 0 && pagevh > 0
1183 then
1184 let e =
1185 { pageno = n
1186 ; pagedimno = pdimno
1187 ; pagew = w
1188 ; pageh = h
1189 ; pagex = pagex
1190 ; pagey = pagey
1191 ; pagevw = pagevw
1192 ; pagevh = pagevh
1193 ; pagedispx = pagedispx
1194 ; pagedispy = pagedispy
1195 ; pagecol = 0
1198 e :: accu
1199 else
1200 accu
1201 else
1202 accu
1204 fold accu (n+1)
1206 List.rev (fold [] 0)
1209 let layoutS (columns, b) y sh =
1210 let sh = sh - state.hscrollh in
1211 let rec fold accu n =
1212 if n = Array.length b
1213 then accu
1214 else
1215 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1216 if (vy - y) > sh
1217 then accu
1218 else
1219 let accu =
1220 if vy + pageh > y
1221 then
1222 let x = xoff + state.x in
1223 let pagey = max 0 (y - vy) in
1224 let pagedispy = if pagey > 0 then 0 else vy - y in
1225 let pagedispx, pagex =
1226 if px = 0
1227 then (
1228 if x < 0
1229 then 0, -x
1230 else x, 0
1232 else (
1233 let px = px - x in
1234 if px < 0
1235 then -px, 0
1236 else 0, px
1239 let pagecolw = pagew/columns in
1240 let pagedispx =
1241 if pagecolw < conf.winw
1242 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1243 else pagedispx
1245 let pagevw =
1246 let vw = conf.winw - pagedispx - state.scrollw in
1247 let pw = pagew - pagex in
1248 min vw pw
1250 let pagevw = min pagevw pagecolw in
1251 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1252 if pagevw > 0 && pagevh > 0
1253 then
1254 let e =
1255 { pageno = n/columns
1256 ; pagedimno = pdimno
1257 ; pagew = pagew
1258 ; pageh = pageh
1259 ; pagex = pagex
1260 ; pagey = pagey
1261 ; pagevw = pagevw
1262 ; pagevh = pagevh
1263 ; pagedispx = pagedispx
1264 ; pagedispy = pagedispy
1265 ; pagecol = n mod columns
1268 e :: accu
1269 else
1270 accu
1271 else
1272 accu
1274 fold accu (n+1)
1276 List.rev (fold [] 0)
1279 let layout y sh =
1280 if nogeomcmds state.geomcmds
1281 then
1282 match conf.columns with
1283 | Csingle b -> layout1 b y sh
1284 | Cmulti c -> layoutN c y sh
1285 | Csplit s -> layoutS s y sh
1286 else []
1289 let clamp incr =
1290 let y = state.y + incr in
1291 let y = max 0 y in
1292 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1296 let itertiles l f =
1297 let tilex = l.pagex mod conf.tilew in
1298 let tiley = l.pagey mod conf.tileh in
1300 let col = l.pagex / conf.tilew in
1301 let row = l.pagey / conf.tileh in
1303 let rec rowloop row y0 dispy h =
1304 if h = 0
1305 then ()
1306 else (
1307 let dh = conf.tileh - y0 in
1308 let dh = min h dh in
1309 let rec colloop col x0 dispx w =
1310 if w = 0
1311 then ()
1312 else (
1313 let dw = conf.tilew - x0 in
1314 let dw = min w dw in
1316 f col row dispx dispy x0 y0 dw dh;
1317 colloop (col+1) 0 (dispx+dw) (w-dw)
1320 colloop col tilex l.pagedispx l.pagevw;
1321 rowloop (row+1) 0 (dispy+dh) (h-dh)
1324 if l.pagevw > 0 && l.pagevh > 0
1325 then rowloop row tiley l.pagedispy l.pagevh;
1328 let gettileopaque l col row =
1329 let key =
1330 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1332 try Some (Hashtbl.find state.tilemap key)
1333 with Not_found -> None
1336 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1337 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1338 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1341 let drawtiles l color =
1342 GlDraw.color color;
1343 let f col row x y tilex tiley w h =
1344 match gettileopaque l col row with
1345 | Some (opaque, _, t) ->
1346 let params = x, y, w, h, tilex, tiley in
1347 if conf.invert
1348 then (
1349 Gl.enable `blend;
1350 GlFunc.blend_func `zero `one_minus_src_color;
1352 drawtile params opaque;
1353 if conf.invert
1354 then Gl.disable `blend;
1355 if conf.debug
1356 then (
1357 let s = Printf.sprintf
1358 "%d[%d,%d] %f sec"
1359 l.pageno col row t
1361 let w = measurestr fstate.fontsize s in
1362 GlMisc.push_attrib [`current];
1363 GlDraw.color (0.0, 0.0, 0.0);
1364 GlDraw.rect
1365 (float (x-2), float (y-2))
1366 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1367 GlDraw.color (1.0, 1.0, 1.0);
1368 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1369 GlMisc.pop_attrib ();
1372 | _ ->
1373 let w =
1374 let lw = conf.winw - state.scrollw - x in
1375 min lw w
1376 and h =
1377 let lh = conf.winh - y in
1378 min lh h
1380 begin match state.texid with
1381 | Some id ->
1382 Gl.enable `texture_2d;
1383 GlTex.bind_texture `texture_2d id;
1384 let x0 = float x
1385 and y0 = float y
1386 and x1 = float (x+w)
1387 and y1 = float (y+h) in
1389 let tw = float w /. 64.0
1390 and th = float h /. 64.0 in
1391 let tx0 = float tilex /. 64.0
1392 and ty0 = float tiley /. 64.0 in
1393 let tx1 = tx0 +. tw
1394 and ty1 = ty0 +. th in
1395 GlDraw.begins `quads;
1396 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1397 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1398 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1399 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1400 GlDraw.ends ();
1402 Gl.disable `texture_2d;
1403 | None ->
1404 GlDraw.color (1.0, 1.0, 1.0);
1405 GlDraw.rect
1406 (float x, float y)
1407 (float (x+w), float (y+h));
1408 end;
1409 if w > 128 && h > fstate.fontsize + 10
1410 then (
1411 GlDraw.color (0.0, 0.0, 0.0);
1412 let c, r =
1413 if conf.verbose
1414 then (col*conf.tilew, row*conf.tileh)
1415 else col, row
1417 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1419 GlDraw.color color;
1421 itertiles l f
1424 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1426 let tilevisible1 l x y =
1427 let ax0 = l.pagex
1428 and ax1 = l.pagex + l.pagevw
1429 and ay0 = l.pagey
1430 and ay1 = l.pagey + l.pagevh in
1432 let bx0 = x
1433 and by0 = y in
1434 let bx1 = min (bx0 + conf.tilew) l.pagew
1435 and by1 = min (by0 + conf.tileh) l.pageh in
1437 let rx0 = max ax0 bx0
1438 and ry0 = max ay0 by0
1439 and rx1 = min ax1 bx1
1440 and ry1 = min ay1 by1 in
1442 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1443 nonemptyintersection
1446 let tilevisible layout n x y =
1447 let rec findpageinlayout m = function
1448 | l :: rest when l.pageno = n ->
1449 tilevisible1 l x y || (
1450 match conf.columns with
1451 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1452 | _ -> false
1454 | _ :: rest -> findpageinlayout 0 rest
1455 | [] -> false
1457 findpageinlayout 0 layout;
1460 let tileready l x y =
1461 tilevisible1 l x y &&
1462 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1465 let tilepage n p layout =
1466 let rec loop = function
1467 | l :: rest ->
1468 if l.pageno = n
1469 then
1470 let f col row _ _ _ _ _ _ =
1471 if state.currently = Idle
1472 then
1473 match gettileopaque l col row with
1474 | Some _ -> ()
1475 | None ->
1476 let x = col*conf.tilew
1477 and y = row*conf.tileh in
1478 let w =
1479 let w = l.pagew - x in
1480 min w conf.tilew
1482 let h =
1483 let h = l.pageh - y in
1484 min h conf.tileh
1486 wcmd "tile %s %d %d %d %d" p x y w h;
1487 state.currently <-
1488 Tiling (
1489 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1490 conf.tilew, conf.tileh
1493 itertiles l f;
1494 else
1495 loop rest
1497 | [] -> ()
1499 if nogeomcmds state.geomcmds
1500 then loop layout;
1503 let preloadlayout visiblepages =
1504 let presentation = conf.presentation in
1505 let interpagespace = conf.interpagespace in
1506 conf.presentation <- false;
1507 conf.interpagespace <- 0;
1508 let y =
1509 match visiblepages with
1510 | [] -> if state.y >= state.maxy then state.maxy else 0
1511 | l :: _ -> getpagey l.pageno + (l.pagey - min 0 l.pagedispy)
1513 let y = if y < conf.winh then 0 else y - conf.winh in
1514 let h = conf.winh*3 in
1515 let pages = layout y h in
1516 conf.presentation <- presentation;
1517 conf.interpagespace <- interpagespace;
1518 pages;
1521 let load pages =
1522 let rec loop pages =
1523 if state.currently != Idle
1524 then ()
1525 else
1526 match pages with
1527 | l :: rest ->
1528 begin match getopaque l.pageno with
1529 | None ->
1530 wcmd "page %d %d" l.pageno l.pagedimno;
1531 state.currently <- Loading (l, state.gen);
1532 | Some opaque ->
1533 tilepage l.pageno opaque pages;
1534 loop rest
1535 end;
1536 | _ -> ()
1538 if nogeomcmds state.geomcmds
1539 then loop pages
1542 let preload pages =
1543 load pages;
1544 if conf.preload && state.currently = Idle
1545 then load (preloadlayout pages);
1548 let layoutready layout =
1549 let rec fold all ls =
1550 all && match ls with
1551 | l :: rest ->
1552 let seen = ref false in
1553 let allvisible = ref true in
1554 let foo col row _ _ _ _ _ _ =
1555 seen := true;
1556 allvisible := !allvisible &&
1557 begin match gettileopaque l col row with
1558 | Some _ -> true
1559 | None -> false
1562 itertiles l foo;
1563 fold (!seen && !allvisible) rest
1564 | [] -> true
1566 let alltilesvisible = fold true layout in
1567 alltilesvisible;
1570 let gotoy y =
1571 let y = bound y 0 state.maxy in
1572 let y, layout, proceed =
1573 match conf.maxwait with
1574 | Some time when state.ghyll == noghyll ->
1575 begin match state.throttle with
1576 | None ->
1577 let layout = layout y conf.winh in
1578 let ready = layoutready layout in
1579 if not ready
1580 then (
1581 load layout;
1582 state.throttle <- Some (layout, y, now ());
1584 else G.postRedisplay "gotoy showall (None)";
1585 y, layout, ready
1586 | Some (_, _, started) ->
1587 let dt = now () -. started in
1588 if dt > time
1589 then (
1590 state.throttle <- None;
1591 let layout = layout y conf.winh in
1592 load layout;
1593 G.postRedisplay "maxwait";
1594 y, layout, true
1596 else -1, [], false
1599 | _ ->
1600 let layout = layout y conf.winh in
1601 if true || layoutready layout
1602 then G.postRedisplay "gotoy ready";
1603 y, layout, true
1605 if proceed
1606 then (
1607 state.y <- y;
1608 state.layout <- layout;
1609 begin match state.mode with
1610 | LinkNav (Ltexact (pageno, linkno)) ->
1611 let rec loop = function
1612 | [] ->
1613 state.mode <- LinkNav (Ltgendir 0)
1614 | l :: _ when l.pageno = pageno ->
1615 begin match getopaque pageno with
1616 | None ->
1617 state.mode <- LinkNav (Ltgendir 0)
1618 | Some opaque ->
1619 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1620 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1621 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1622 then state.mode <- LinkNav (Ltgendir 0)
1624 | _ :: rest -> loop rest
1626 loop layout
1627 | _ -> ()
1628 end;
1629 begin match state.mode with
1630 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1631 if not (pagevisible layout pageno)
1632 then (
1633 match state.layout with
1634 | [] -> ()
1635 | l :: _ ->
1636 state.mode <- Birdseye (
1637 conf, leftx, l.pageno, hooverpageno, anchor
1640 | LinkNav (Ltgendir dir as lt) ->
1641 let linknav =
1642 let rec loop = function
1643 | [] -> lt
1644 | l :: rest ->
1645 match getopaque l.pageno with
1646 | None -> loop rest
1647 | Some opaque ->
1648 let link =
1649 let ld =
1650 if dir = 0
1651 then LDfirstvisible (l.pagex, l.pagey, dir)
1652 else (
1653 if dir > 0 then LDfirst else LDlast
1656 findlink opaque ld
1658 match link with
1659 | Lnotfound -> loop rest
1660 | Lfound n ->
1661 showlinktype (getlink opaque n);
1662 Ltexact (l.pageno, n)
1664 loop state.layout
1666 state.mode <- LinkNav linknav
1667 | _ -> ()
1668 end;
1669 preload layout;
1671 state.ghyll <- noghyll;
1672 if conf.updatecurs
1673 then (
1674 let mx, my = state.mpos in
1675 updateunder mx my;
1679 let conttiling pageno opaque =
1680 tilepage pageno opaque
1681 (if conf.preload then preloadlayout state.layout else state.layout)
1684 let gotoy_and_clear_text y =
1685 if not conf.verbose then state.text <- "";
1686 gotoy y;
1689 let getanchor () =
1690 match state.layout with
1691 | [] -> emptyanchor
1692 | l :: _ ->
1693 let coloff = l.pagecol * l.pageh in
1694 (l.pageno,
1695 (float (l.pagey - l.pagedispy) +. float coloff) /. float l.pageh)
1698 let getanchory (n, top) =
1699 let y, h = getpageyh n in
1700 y + (truncate (top *. float h));
1703 let gotoanchor anchor =
1704 gotoy (getanchory anchor);
1707 let addnav () =
1708 cbput state.hists.nav (getanchor ());
1711 let getnav dir =
1712 let anchor = cbgetc state.hists.nav dir in
1713 getanchory anchor;
1716 let gotoghyll y =
1717 let scroll f n a b =
1718 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1719 let snake f a b =
1720 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1721 if f < a
1722 then s (float f /. float a)
1723 else (
1724 if f > b
1725 then 1.0 -. s ((float (f-b) /. float (n-b)))
1726 else 1.0
1729 snake f a b
1730 and summa f n a b =
1731 (* courtesy:
1732 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1733 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1734 let iv1 = iv f in
1735 let ins = float a *. iv1
1736 and outs = float (n-b) *. iv1 in
1737 let ones = b - a in
1738 ins +. outs +. float ones
1740 let rec set (_N, _A, _B) y sy =
1741 let sum = summa 1.0 _N _A _B in
1742 let dy = float (y - sy) in
1743 state.ghyll <- (
1744 let rec gf n y1 o =
1745 if n >= _N
1746 then state.ghyll <- noghyll
1747 else
1748 let go n =
1749 let s = scroll n _N _A _B in
1750 let y1 = y1 +. ((s *. dy) /. sum) in
1751 gotoy_and_clear_text (truncate y1);
1752 state.ghyll <- gf (n+1) y1;
1754 match o with
1755 | None -> go n
1756 | Some y' -> set (_N/2, 0, 0) y' state.y
1758 gf 0 (float state.y)
1761 match conf.ghyllscroll with
1762 | None ->
1763 gotoy_and_clear_text y
1764 | Some nab ->
1765 if state.ghyll == noghyll
1766 then set nab y state.y
1767 else state.ghyll (Some y)
1770 let gotopage n top =
1771 let y, h = getpageyh n in
1772 let y = y + (truncate (top *. float h)) in
1773 gotoghyll y
1776 let gotopage1 n top =
1777 let y = getpagey n in
1778 let y = y + top in
1779 gotoghyll y
1782 let invalidate s f =
1783 state.layout <- [];
1784 state.pdims <- [];
1785 state.rects <- [];
1786 state.rects1 <- [];
1787 match state.geomcmds with
1788 | ps, [] when String.length ps = 0 ->
1789 f ();
1790 state.geomcmds <- s, [];
1792 | ps, [] ->
1793 state.geomcmds <- ps, [s, f];
1795 | ps, (s', _) :: rest when s' = s ->
1796 state.geomcmds <- ps, ((s, f) :: rest);
1798 | ps, cmds ->
1799 state.geomcmds <- ps, ((s, f) :: cmds);
1802 let opendoc path password =
1803 state.path <- path;
1804 state.password <- password;
1805 state.gen <- state.gen + 1;
1806 state.docinfo <- [];
1808 setaalevel conf.aalevel;
1809 Wsi.settitle ("llpp " ^ Filename.basename path);
1810 wcmd "open %s\000%s\000" path password;
1811 invalidate "reqlayout"
1812 (fun () ->
1813 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1816 let scalecolor c =
1817 let c = c *. conf.colorscale in
1818 (c, c, c);
1821 let scalecolor2 (r, g, b) =
1822 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1825 let docolumns = function
1826 | Csingle _ ->
1827 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1828 let rec loop pageno pdimno pdim y ph pdims =
1829 if pageno = state.pagecount
1830 then ()
1831 else
1832 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1833 match pdims with
1834 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1835 pdimno+1, pdim, rest
1836 | _ ->
1837 pdimno, pdim, pdims
1839 let x = (conf.winw - state.scrollw - w) / 2 - xoff in
1840 let y = y +
1841 (if conf.presentation
1842 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1843 else (if pageno = 0 then 0 else calcips h)
1846 a.(pageno) <- (pdimno, x, y, pdim);
1847 loop (pageno+1) pdimno pdim (y + h) h pdims
1849 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1850 conf.columns <- Csingle a;
1852 | Cmulti ((columns, coverA, coverB), _) ->
1853 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1854 let rec loop pageno pdimno pdim x y rowh pdims =
1855 let rec fixrow m = if m = pageno then () else
1856 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1857 if h < rowh
1858 then (
1859 let y = y + (rowh - h) / 2 in
1860 a.(m) <- (pdimno, x, y, pdim);
1862 fixrow (m+1)
1864 if pageno = state.pagecount
1865 then fixrow (((pageno - 1) / columns) * columns)
1866 else
1867 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1868 match pdims with
1869 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1870 pdimno+1, pdim, rest
1871 | _ ->
1872 pdimno, pdim, pdims
1874 let x, y, rowh' =
1875 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1876 then (
1877 (conf.winw - state.scrollw - w) / 2,
1878 y + rowh + conf.interpagespace, h
1880 else (
1881 if (pageno - coverA) mod columns = 0
1882 then 0, y + rowh + conf.interpagespace, h
1883 else x, y, max rowh h
1886 if pageno > 1 && (pageno - coverA) mod columns = 0
1887 then fixrow (pageno - columns);
1888 a.(pageno) <- (pdimno, x, y, pdim);
1889 let x = x + w + xoff*2 + conf.interpagespace in
1890 loop (pageno+1) pdimno pdim x y rowh' pdims
1892 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1893 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1895 | Csplit (c, _) ->
1896 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1897 let rec loop pageno pdimno pdim y pdims =
1898 if pageno = state.pagecount
1899 then ()
1900 else
1901 let pdimno, ((_, w, h, _) as pdim), pdims =
1902 match pdims with
1903 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1904 pdimno+1, pdim, rest
1905 | _ ->
1906 pdimno, pdim, pdims
1908 let cw = w / c in
1909 let rec loop1 n x y =
1910 if n = c then y else (
1911 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1912 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1915 let y = loop1 0 0 y in
1916 loop (pageno+1) pdimno pdim y pdims
1918 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1919 conf.columns <- Csplit (c, a);
1922 let represent () =
1923 docolumns conf.columns;
1924 state.maxy <- calcheight ();
1925 state.hscrollh <-
1926 if state.w <= conf.winw - state.scrollw
1927 then 0
1928 else state.scrollw
1930 match state.mode with
1931 | Birdseye (_, _, pageno, _, _) ->
1932 let y, h = getpageyh pageno in
1933 let top = (conf.winh - h) / 2 in
1934 gotoy (max 0 (y - top))
1935 | _ -> gotoanchor state.anchor
1938 let reshape w h =
1939 GlDraw.viewport 0 0 w h;
1940 let firsttime = state.geomcmds == firstgeomcmds in
1941 if not firsttime && nogeomcmds state.geomcmds
1942 then state.anchor <- getanchor ();
1944 conf.winw <- w;
1945 let w = truncate (float w *. conf.zoom) - state.scrollw in
1946 let w = max w 2 in
1947 conf.winh <- h;
1948 setfontsize fstate.fontsize;
1949 GlMat.mode `modelview;
1950 GlMat.load_identity ();
1952 GlMat.mode `projection;
1953 GlMat.load_identity ();
1954 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1955 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1956 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1958 let relx =
1959 if conf.zoom <= 1.0
1960 then 0.0
1961 else float state.x /. float state.w
1963 invalidate "geometry"
1964 (fun () ->
1965 state.w <- w;
1966 if not firsttime
1967 then state.x <- truncate (relx *. float w);
1968 let w =
1969 match conf.columns with
1970 | Csingle _ -> w
1971 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1972 | Csplit (c, _) -> w * c
1974 wcmd "geometry %d %d" w h);
1977 let enttext () =
1978 let len = String.length state.text in
1979 let drawstring s =
1980 let hscrollh =
1981 match state.mode with
1982 | Textentry _
1983 | View ->
1984 let h, _, _ = state.uioh#scrollpw in
1986 | _ -> 0
1988 let rect x w =
1989 GlDraw.rect
1990 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1991 (x+.w, float (conf.winh - hscrollh))
1994 let w = float (conf.winw - state.scrollw - 1) in
1995 if state.progress >= 0.0 && state.progress < 1.0
1996 then (
1997 GlDraw.color (0.3, 0.3, 0.3);
1998 let w1 = w *. state.progress in
1999 rect 0.0 w1;
2000 GlDraw.color (0.0, 0.0, 0.0);
2001 rect w1 (w-.w1)
2003 else (
2004 GlDraw.color (0.0, 0.0, 0.0);
2005 rect 0.0 w;
2008 GlDraw.color (1.0, 1.0, 1.0);
2009 drawstring fstate.fontsize
2010 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2012 let s =
2013 match state.mode with
2014 | Textentry ((prefix, text, _, _, _, _), _) ->
2015 let s =
2016 if len > 0
2017 then
2018 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2019 else
2020 Printf.sprintf "%s%s_" prefix text
2024 | _ -> state.text
2026 let s =
2027 if state.newerrmsgs
2028 then (
2029 if not (istextentry state.mode)
2030 then
2031 let s1 = "(press 'e' to review error messasges)" in
2032 if String.length s > 0 then s ^ " " ^ s1 else s1
2033 else s
2035 else s
2037 if String.length s > 0
2038 then drawstring s
2041 let gctiles () =
2042 let len = Queue.length state.tilelru in
2043 let layout = lazy (
2044 match state.throttle with
2045 | None ->
2046 if conf.preload
2047 then preloadlayout state.layout
2048 else state.layout
2049 | Some (layout, _, _) ->
2050 layout
2051 ) in
2052 let rec loop qpos =
2053 if state.memused <= conf.memlimit
2054 then ()
2055 else (
2056 if qpos < len
2057 then
2058 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2059 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2060 let (_, pw, ph, _) = getpagedim n in
2062 gen = state.gen
2063 && colorspace = conf.colorspace
2064 && angle = conf.angle
2065 && pagew = pw
2066 && pageh = ph
2067 && (
2068 let x = col*conf.tilew
2069 and y = row*conf.tileh in
2070 tilevisible (Lazy.force_val layout) n x y
2072 then Queue.push lruitem state.tilelru
2073 else (
2074 wcmd "freetile %s" p;
2075 state.memused <- state.memused - s;
2076 state.uioh#infochanged Memused;
2077 Hashtbl.remove state.tilemap k;
2079 loop (qpos+1)
2082 loop 0
2085 let flushtiles () =
2086 Queue.iter (fun (k, p, s) ->
2087 wcmd "freetile %s" p;
2088 state.memused <- state.memused - s;
2089 state.uioh#infochanged Memused;
2090 Hashtbl.remove state.tilemap k;
2091 ) state.tilelru;
2092 Queue.clear state.tilelru;
2093 load state.layout;
2096 let logcurrently = function
2097 | Idle -> dolog "Idle"
2098 | Loading (l, gen) ->
2099 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2100 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2101 dolog
2102 "Tiling %d[%d,%d] page=%s cs=%s angle"
2103 l.pageno col row pageopaque
2104 (colorspace_to_string colorspace)
2106 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2107 angle gen conf.angle state.gen
2108 tilew tileh
2109 conf.tilew conf.tileh
2111 | Outlining _ ->
2112 dolog "outlining"
2115 let act cmds =
2116 (* dolog "%S" cmds; *)
2117 let op, args =
2118 let spacepos =
2119 try String.index cmds ' '
2120 with Not_found -> -1
2122 if spacepos = -1
2123 then cmds, ""
2124 else
2125 let l = String.length cmds in
2126 let op = String.sub cmds 0 spacepos in
2127 op, begin
2128 if l - spacepos < 2 then ""
2129 else String.sub cmds (spacepos+1) (l-spacepos-1)
2132 match op with
2133 | "clear" ->
2134 state.uioh#infochanged Pdim;
2135 state.pdims <- [];
2137 | "clearrects" ->
2138 state.rects <- state.rects1;
2139 G.postRedisplay "clearrects";
2141 | "continue" ->
2142 let n =
2143 try Scanf.sscanf args "%u" (fun n -> n)
2144 with exn ->
2145 dolog "error processing 'continue' %S: %s"
2146 cmds (Printexc.to_string exn);
2147 exit 1;
2149 state.pagecount <- n;
2150 begin match state.currently with
2151 | Outlining l ->
2152 state.currently <- Idle;
2153 state.outlines <- Array.of_list (List.rev l)
2154 | _ -> ()
2155 end;
2157 let cur, cmds = state.geomcmds in
2158 if String.length cur = 0
2159 then failwith "umpossible";
2161 begin match List.rev cmds with
2162 | [] ->
2163 state.geomcmds <- "", [];
2164 represent ();
2165 | (s, f) :: rest ->
2166 f ();
2167 state.geomcmds <- s, List.rev rest;
2168 end;
2169 if conf.maxwait = None
2170 then G.postRedisplay "continue";
2172 | "title" ->
2173 Wsi.settitle args
2175 | "msg" ->
2176 showtext ' ' args
2178 | "vmsg" ->
2179 if conf.verbose
2180 then showtext ' ' args
2182 | "progress" ->
2183 let progress, text =
2185 Scanf.sscanf args "%f %n"
2186 (fun f pos ->
2187 f, String.sub args pos (String.length args - pos))
2188 with exn ->
2189 dolog "error processing 'progress' %S: %s"
2190 cmds (Printexc.to_string exn);
2191 exit 1;
2193 state.text <- text;
2194 state.progress <- progress;
2195 G.postRedisplay "progress"
2197 | "firstmatch" ->
2198 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2200 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2201 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2202 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2203 with exn ->
2204 dolog "error processing 'firstmatch' %S: %s"
2205 cmds (Printexc.to_string exn);
2206 exit 1;
2208 let y = (getpagey pageno) + truncate y0 in
2209 addnav ();
2210 gotoy y;
2211 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2213 | "match" ->
2214 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2216 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2217 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2218 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2219 with exn ->
2220 dolog "error processing 'match' %S: %s"
2221 cmds (Printexc.to_string exn);
2222 exit 1;
2224 state.rects1 <-
2225 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2227 | "page" ->
2228 let pageopaque, t =
2230 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2231 with exn ->
2232 dolog "error processing 'page' %S: %s"
2233 cmds (Printexc.to_string exn);
2234 exit 1;
2236 begin match state.currently with
2237 | Loading (l, gen) ->
2238 vlog "page %d took %f sec" l.pageno t;
2239 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2240 begin match state.throttle with
2241 | None ->
2242 let preloadedpages =
2243 if conf.preload
2244 then preloadlayout state.layout
2245 else state.layout
2247 let evict () =
2248 let module IntSet =
2249 Set.Make (struct type t = int let compare = (-) end) in
2250 let set =
2251 List.fold_left (fun s l -> IntSet.add l.pageno s)
2252 IntSet.empty preloadedpages
2254 let evictedpages =
2255 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2256 if not (IntSet.mem pageno set)
2257 then (
2258 wcmd "freepage %s" opaque;
2259 key :: accu
2261 else accu
2262 ) state.pagemap []
2264 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2266 evict ();
2267 state.currently <- Idle;
2268 if gen = state.gen
2269 then (
2270 tilepage l.pageno pageopaque state.layout;
2271 load state.layout;
2272 load preloadedpages;
2273 if pagevisible state.layout l.pageno
2274 && layoutready state.layout
2275 then G.postRedisplay "page";
2278 | Some (layout, _, _) ->
2279 state.currently <- Idle;
2280 tilepage l.pageno pageopaque layout;
2281 load state.layout
2282 end;
2284 | _ ->
2285 dolog "Inconsistent loading state";
2286 logcurrently state.currently;
2287 exit 1
2290 | "tile" ->
2291 let (x, y, opaque, size, t) =
2293 Scanf.sscanf args "%u %u %s %u %f"
2294 (fun x y p size t -> (x, y, p, size, t))
2295 with exn ->
2296 dolog "error processing 'tile' %S: %s"
2297 cmds (Printexc.to_string exn);
2298 exit 1;
2300 begin match state.currently with
2301 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2302 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2304 if tilew != conf.tilew || tileh != conf.tileh
2305 then (
2306 wcmd "freetile %s" opaque;
2307 state.currently <- Idle;
2308 load state.layout;
2310 else (
2311 puttileopaque l col row gen cs angle opaque size t;
2312 state.memused <- state.memused + size;
2313 state.uioh#infochanged Memused;
2314 gctiles ();
2315 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2316 opaque, size) state.tilelru;
2318 let layout =
2319 match state.throttle with
2320 | None -> state.layout
2321 | Some (layout, _, _) -> layout
2324 state.currently <- Idle;
2325 if gen = state.gen
2326 && conf.colorspace = cs
2327 && conf.angle = angle
2328 && tilevisible layout l.pageno x y
2329 then conttiling l.pageno pageopaque;
2331 begin match state.throttle with
2332 | None ->
2333 preload state.layout;
2334 if gen = state.gen
2335 && conf.colorspace = cs
2336 && conf.angle = angle
2337 && tilevisible state.layout l.pageno x y
2338 then G.postRedisplay "tile nothrottle";
2340 | Some (layout, y, _) ->
2341 let ready = layoutready layout in
2342 if ready
2343 then (
2344 state.y <- y;
2345 state.layout <- layout;
2346 state.throttle <- None;
2347 G.postRedisplay "throttle";
2349 else load layout;
2350 end;
2353 | _ ->
2354 dolog "Inconsistent tiling state";
2355 logcurrently state.currently;
2356 exit 1
2359 | "pdim" ->
2360 let pdim =
2362 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2363 with exn ->
2364 dolog "error processing 'pdim' %S: %s"
2365 cmds (Printexc.to_string exn);
2366 exit 1;
2368 state.uioh#infochanged Pdim;
2369 state.pdims <- pdim :: state.pdims
2371 | "o" ->
2372 let (l, n, t, h, pos) =
2374 Scanf.sscanf args "%u %u %d %u %n"
2375 (fun l n t h pos -> l, n, t, h, pos)
2376 with exn ->
2377 dolog "error processing 'o' %S: %s"
2378 cmds (Printexc.to_string exn);
2379 exit 1;
2381 let s = String.sub args pos (String.length args - pos) in
2382 let outline = (s, l, (n, float t /. float h)) in
2383 begin match state.currently with
2384 | Outlining outlines ->
2385 state.currently <- Outlining (outline :: outlines)
2386 | Idle ->
2387 state.currently <- Outlining [outline]
2388 | currently ->
2389 dolog "invalid outlining state";
2390 logcurrently currently
2393 | "info" ->
2394 state.docinfo <- (1, args) :: state.docinfo
2396 | "infoend" ->
2397 state.uioh#infochanged Docinfo;
2398 state.docinfo <- List.rev state.docinfo
2400 | _ ->
2401 dolog "unknown cmd `%S'" cmds
2404 let onhist cb =
2405 let rc = cb.rc in
2406 let action = function
2407 | HCprev -> cbget cb ~-1
2408 | HCnext -> cbget cb 1
2409 | HCfirst -> cbget cb ~-(cb.rc)
2410 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2411 and cancel () = cb.rc <- rc
2412 in (action, cancel)
2415 let search pattern forward =
2416 if String.length pattern > 0
2417 then
2418 let pn, py =
2419 match state.layout with
2420 | [] -> 0, 0
2421 | l :: _ ->
2422 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2424 wcmd "search %d %d %d %d,%s\000"
2425 (btod conf.icase) pn py (btod forward) pattern;
2428 let intentry text key =
2429 let c =
2430 if key >= 32 && key < 127
2431 then Char.chr key
2432 else '\000'
2434 match c with
2435 | '0' .. '9' ->
2436 let text = addchar text c in
2437 TEcont text
2439 | _ ->
2440 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2441 TEcont text
2444 let linknentry text key =
2445 let c =
2446 if key >= 32 && key < 127
2447 then Char.chr key
2448 else '\000'
2450 match c with
2451 | 'a' .. 'z' ->
2452 let text = addchar text c in
2453 TEcont text
2455 | _ ->
2456 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2457 TEcont text
2460 let linkndone f s =
2461 if String.length s > 0
2462 then (
2463 let n =
2464 let l = String.length s in
2465 let rec loop pos n = if pos = l then n else
2466 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2467 loop (pos+1) (n*26 + m)
2468 in loop 0 0
2470 let rec loop n = function
2471 | [] -> ()
2472 | l :: rest ->
2473 match getopaque l.pageno with
2474 | None -> loop n rest
2475 | Some opaque ->
2476 let m = getlinkcount opaque in
2477 if n < m
2478 then (
2479 let under = getlink opaque n in
2480 f under
2482 else loop (n-m) rest
2484 loop n state.layout;
2488 let textentry text key =
2489 if key land 0xff00 = 0xff00
2490 then TEcont text
2491 else TEcont (text ^ Wsi.toutf8 key)
2494 let reqlayout angle proportional =
2495 match state.throttle with
2496 | None ->
2497 if nogeomcmds state.geomcmds
2498 then state.anchor <- getanchor ();
2499 conf.angle <- angle mod 360;
2500 if conf.angle != 0
2501 then (
2502 match state.mode with
2503 | LinkNav _ -> state.mode <- View
2504 | _ -> ()
2506 conf.proportional <- proportional;
2507 invalidate "reqlayout"
2508 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2509 | _ -> ()
2512 let settrim trimmargins trimfuzz =
2513 if nogeomcmds state.geomcmds
2514 then state.anchor <- getanchor ();
2515 conf.trimmargins <- trimmargins;
2516 conf.trimfuzz <- trimfuzz;
2517 let x0, y0, x1, y1 = trimfuzz in
2518 invalidate "settrim"
2519 (fun () ->
2520 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2521 Hashtbl.iter (fun _ opaque ->
2522 wcmd "freepage %s" opaque;
2523 ) state.pagemap;
2524 Hashtbl.clear state.pagemap;
2527 let setzoom zoom =
2528 match state.throttle with
2529 | None ->
2530 let zoom = max 0.01 zoom in
2531 if zoom <> conf.zoom
2532 then (
2533 state.prevzoom <- conf.zoom;
2534 conf.zoom <- zoom;
2535 reshape conf.winw conf.winh;
2536 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2539 | Some (layout, y, started) ->
2540 let time =
2541 match conf.maxwait with
2542 | None -> 0.0
2543 | Some t -> t
2545 let dt = now () -. started in
2546 if dt > time
2547 then (
2548 state.y <- y;
2549 load layout;
2553 let setcolumns mode columns coverA coverB =
2554 state.prevcolumns <- Some (conf.columns, conf.zoom);
2555 if columns < 0
2556 then (
2557 if isbirdseye mode
2558 then showtext '!' "split mode doesn't work in bird's eye"
2559 else (
2560 conf.columns <- Csplit (-columns, [||]);
2561 state.x <- 0;
2562 conf.zoom <- 1.0;
2565 else (
2566 if columns < 2
2567 then (
2568 conf.columns <- Csingle [||];
2569 state.x <- 0;
2570 setzoom 1.0;
2572 else (
2573 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2574 conf.zoom <- 1.0;
2577 reshape conf.winw conf.winh;
2580 let enterbirdseye () =
2581 let zoom = float conf.thumbw /. float conf.winw in
2582 let birdseyepageno =
2583 let cy = conf.winh / 2 in
2584 let fold = function
2585 | [] -> 0
2586 | l :: rest ->
2587 let rec fold best = function
2588 | [] -> best.pageno
2589 | l :: rest ->
2590 let d = cy - (l.pagedispy + l.pagevh/2)
2591 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2592 if abs d < abs dbest
2593 then fold l rest
2594 else best.pageno
2595 in fold l rest
2597 fold state.layout
2599 state.mode <- Birdseye (
2600 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2602 conf.zoom <- zoom;
2603 conf.presentation <- false;
2604 conf.interpagespace <- 10;
2605 conf.hlinks <- false;
2606 state.x <- 0;
2607 state.mstate <- Mnone;
2608 conf.maxwait <- None;
2609 conf.columns <- (
2610 match conf.beyecolumns with
2611 | Some c ->
2612 conf.zoom <- 1.0;
2613 Cmulti ((c, 0, 0), [||])
2614 | None -> Csingle [||]
2616 Wsi.setcursor Wsi.CURSOR_INHERIT;
2617 if conf.verbose
2618 then
2619 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2620 (100.0*.zoom)
2621 else
2622 state.text <- ""
2624 reshape conf.winw conf.winh;
2627 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2628 state.mode <- View;
2629 conf.zoom <- c.zoom;
2630 conf.presentation <- c.presentation;
2631 conf.interpagespace <- c.interpagespace;
2632 conf.maxwait <- c.maxwait;
2633 conf.hlinks <- c.hlinks;
2634 conf.beyecolumns <- (
2635 match conf.columns with
2636 | Cmulti ((c, _, _), _) -> Some c
2637 | Csingle _ -> None
2638 | Csplit _ -> failwith "leaving bird's eye split mode"
2640 conf.columns <- (
2641 match c.columns with
2642 | Cmulti (c, _) -> Cmulti (c, [||])
2643 | Csingle _ -> Csingle [||]
2644 | Csplit (c, _) -> Csplit (c, [||])
2646 state.x <- leftx;
2647 if conf.verbose
2648 then
2649 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2650 (100.0*.conf.zoom)
2652 reshape conf.winw conf.winh;
2653 state.anchor <- if goback then anchor else (pageno, 0.0);
2656 let togglebirdseye () =
2657 match state.mode with
2658 | Birdseye vals -> leavebirdseye vals true
2659 | View -> enterbirdseye ()
2660 | _ -> ()
2663 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2664 let pageno = max 0 (pageno - incr) in
2665 let rec loop = function
2666 | [] -> gotopage1 pageno 0
2667 | l :: _ when l.pageno = pageno ->
2668 if l.pagedispy >= 0 && l.pagey = 0
2669 then G.postRedisplay "upbirdseye"
2670 else gotopage1 pageno 0
2671 | _ :: rest -> loop rest
2673 loop state.layout;
2674 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2677 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2678 let pageno = min (state.pagecount - 1) (pageno + incr) in
2679 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2680 let rec loop = function
2681 | [] ->
2682 let y, h = getpageyh pageno in
2683 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2684 gotoy (clamp dy)
2685 | l :: _ when l.pageno = pageno ->
2686 if l.pagevh != l.pageh
2687 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2688 else G.postRedisplay "downbirdseye"
2689 | _ :: rest -> loop rest
2691 loop state.layout
2694 let optentry mode _ key =
2695 let btos b = if b then "on" else "off" in
2696 if key >= 32 && key < 127
2697 then
2698 let c = Char.chr key in
2699 match c with
2700 | 's' ->
2701 let ondone s =
2702 try conf.scrollstep <- int_of_string s with exc ->
2703 state.text <- Printf.sprintf "bad integer `%s': %s"
2704 s (Printexc.to_string exc)
2706 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2708 | 'A' ->
2709 let ondone s =
2711 conf.autoscrollstep <- int_of_string s;
2712 if state.autoscroll <> None
2713 then state.autoscroll <- Some conf.autoscrollstep
2714 with exc ->
2715 state.text <- Printf.sprintf "bad integer `%s': %s"
2716 s (Printexc.to_string exc)
2718 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2720 | 'C' ->
2721 let ondone s =
2723 let n, a, b = multicolumns_of_string s in
2724 setcolumns mode n a b;
2725 with exc ->
2726 state.text <- Printf.sprintf "bad columns `%s': %s"
2727 s (Printexc.to_string exc)
2729 TEswitch ("columns: ", "", None, textentry, ondone, true)
2731 | 'Z' ->
2732 let ondone s =
2734 let zoom = float (int_of_string s) /. 100.0 in
2735 setzoom zoom
2736 with exc ->
2737 state.text <- Printf.sprintf "bad integer `%s': %s"
2738 s (Printexc.to_string exc)
2740 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2742 | 't' ->
2743 let ondone s =
2745 conf.thumbw <- bound (int_of_string s) 2 4096;
2746 state.text <-
2747 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2748 begin match mode with
2749 | Birdseye beye ->
2750 leavebirdseye beye false;
2751 enterbirdseye ();
2752 | _ -> ();
2754 with exc ->
2755 state.text <- Printf.sprintf "bad integer `%s': %s"
2756 s (Printexc.to_string exc)
2758 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2760 | 'R' ->
2761 let ondone s =
2762 match try
2763 Some (int_of_string s)
2764 with exc ->
2765 state.text <- Printf.sprintf "bad integer `%s': %s"
2766 s (Printexc.to_string exc);
2767 None
2768 with
2769 | Some angle -> reqlayout angle conf.proportional
2770 | None -> ()
2772 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2774 | 'i' ->
2775 conf.icase <- not conf.icase;
2776 TEdone ("case insensitive search " ^ (btos conf.icase))
2778 | 'p' ->
2779 conf.preload <- not conf.preload;
2780 gotoy state.y;
2781 TEdone ("preload " ^ (btos conf.preload))
2783 | 'v' ->
2784 conf.verbose <- not conf.verbose;
2785 TEdone ("verbose " ^ (btos conf.verbose))
2787 | 'd' ->
2788 conf.debug <- not conf.debug;
2789 TEdone ("debug " ^ (btos conf.debug))
2791 | 'h' ->
2792 conf.maxhfit <- not conf.maxhfit;
2793 state.maxy <- calcheight ();
2794 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2796 | 'c' ->
2797 conf.crophack <- not conf.crophack;
2798 TEdone ("crophack " ^ btos conf.crophack)
2800 | 'a' ->
2801 let s =
2802 match conf.maxwait with
2803 | None ->
2804 conf.maxwait <- Some infinity;
2805 "always wait for page to complete"
2806 | Some _ ->
2807 conf.maxwait <- None;
2808 "show placeholder if page is not ready"
2810 TEdone s
2812 | 'f' ->
2813 conf.underinfo <- not conf.underinfo;
2814 TEdone ("underinfo " ^ btos conf.underinfo)
2816 | 'P' ->
2817 conf.savebmarks <- not conf.savebmarks;
2818 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2820 | 'S' ->
2821 let ondone s =
2823 let pageno, py =
2824 match state.layout with
2825 | [] -> 0, 0
2826 | l :: _ ->
2827 l.pageno, l.pagey
2829 conf.interpagespace <- int_of_string s;
2830 docolumns conf.columns;
2831 state.maxy <- calcheight ();
2832 let y = getpagey pageno in
2833 gotoy (y + py)
2834 with exc ->
2835 state.text <- Printf.sprintf "bad integer `%s': %s"
2836 s (Printexc.to_string exc)
2838 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2840 | 'l' ->
2841 reqlayout conf.angle (not conf.proportional);
2842 TEdone ("proportional display " ^ btos conf.proportional)
2844 | 'T' ->
2845 settrim (not conf.trimmargins) conf.trimfuzz;
2846 TEdone ("trim margins " ^ btos conf.trimmargins)
2848 | 'I' ->
2849 conf.invert <- not conf.invert;
2850 TEdone ("invert colors " ^ btos conf.invert)
2852 | 'x' ->
2853 let ondone s =
2854 cbput state.hists.sel s;
2855 conf.selcmd <- s;
2857 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2858 textentry, ondone, true)
2860 | _ ->
2861 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2862 TEstop
2863 else
2864 TEcont state.text
2867 class type lvsource = object
2868 method getitemcount : int
2869 method getitem : int -> (string * int)
2870 method hasaction : int -> bool
2871 method exit :
2872 uioh:uioh ->
2873 cancel:bool ->
2874 active:int ->
2875 first:int ->
2876 pan:int ->
2877 qsearch:string ->
2878 uioh option
2879 method getactive : int
2880 method getfirst : int
2881 method getqsearch : string
2882 method setqsearch : string -> unit
2883 method getpan : int
2884 end;;
2886 class virtual lvsourcebase = object
2887 val mutable m_active = 0
2888 val mutable m_first = 0
2889 val mutable m_qsearch = ""
2890 val mutable m_pan = 0
2891 method getactive = m_active
2892 method getfirst = m_first
2893 method getqsearch = m_qsearch
2894 method getpan = m_pan
2895 method setqsearch s = m_qsearch <- s
2896 end;;
2898 let withoutlastutf8 s =
2899 let len = String.length s in
2900 if len = 0
2901 then s
2902 else
2903 let rec find pos =
2904 if pos = 0
2905 then pos
2906 else
2907 let b = Char.code s.[pos] in
2908 if b land 0b110000 = 0b11000000
2909 then find (pos-1)
2910 else pos-1
2912 let first =
2913 if Char.code s.[len-1] land 0x80 = 0
2914 then len-1
2915 else find (len-1)
2917 String.sub s 0 first;
2920 let textentrykeyboard
2921 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2922 let enttext te =
2923 state.mode <- Textentry (te, onleave);
2924 state.text <- "";
2925 enttext ();
2926 G.postRedisplay "textentrykeyboard enttext";
2928 let histaction cmd =
2929 match opthist with
2930 | None -> ()
2931 | Some (action, _) ->
2932 state.mode <- Textentry (
2933 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2935 G.postRedisplay "textentry histaction"
2937 match key with
2938 | 0xff08 -> (* backspace *)
2939 let s = withoutlastutf8 text in
2940 let len = String.length s in
2941 if cancelonempty && len = 0
2942 then (
2943 onleave Cancel;
2944 G.postRedisplay "textentrykeyboard after cancel";
2946 else (
2947 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2950 | 0xff0d ->
2951 ondone text;
2952 onleave Confirm;
2953 G.postRedisplay "textentrykeyboard after confirm"
2955 | 0xff52 -> histaction HCprev
2956 | 0xff54 -> histaction HCnext
2957 | 0xff50 -> histaction HCfirst
2958 | 0xff57 -> histaction HClast
2960 | 0xff1b -> (* escape*)
2961 if String.length text = 0
2962 then (
2963 begin match opthist with
2964 | None -> ()
2965 | Some (_, onhistcancel) -> onhistcancel ()
2966 end;
2967 onleave Cancel;
2968 state.text <- "";
2969 G.postRedisplay "textentrykeyboard after cancel2"
2971 else (
2972 enttext (c, "", opthist, onkey, ondone, cancelonempty)
2975 | 0xff9f | 0xffff -> () (* delete *)
2977 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2978 begin match onkey text key with
2979 | TEdone text ->
2980 ondone text;
2981 onleave Confirm;
2982 G.postRedisplay "textentrykeyboard after confirm2";
2984 | TEcont text ->
2985 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2987 | TEstop ->
2988 onleave Cancel;
2989 G.postRedisplay "textentrykeyboard after cancel3"
2991 | TEswitch te ->
2992 state.mode <- Textentry (te, onleave);
2993 G.postRedisplay "textentrykeyboard switch";
2994 end;
2996 | _ ->
2997 vlog "unhandled key %s" (Wsi.keyname key)
3000 let firstof first active =
3001 if first > active || abs (first - active) > fstate.maxrows - 1
3002 then max 0 (active - (fstate.maxrows/2))
3003 else first
3006 let calcfirst first active =
3007 if active > first
3008 then
3009 let rows = active - first in
3010 if rows > fstate.maxrows then active - fstate.maxrows else first
3011 else active
3014 let scrollph y maxy =
3015 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3016 let sh = float conf.winh /. sh in
3017 let sh = max sh (float conf.scrollh) in
3019 let percent =
3020 if y = state.maxy
3021 then 1.0
3022 else float y /. float maxy
3024 let position = (float conf.winh -. sh) *. percent in
3026 let position =
3027 if position +. sh > float conf.winh
3028 then float conf.winh -. sh
3029 else position
3031 position, sh;
3034 let coe s = (s :> uioh);;
3036 class listview ~(source:lvsource) ~trusted ~modehash =
3037 object (self)
3038 val m_pan = source#getpan
3039 val m_first = source#getfirst
3040 val m_active = source#getactive
3041 val m_qsearch = source#getqsearch
3042 val m_prev_uioh = state.uioh
3044 method private elemunder y =
3045 let n = y / (fstate.fontsize+1) in
3046 if m_first + n < source#getitemcount
3047 then (
3048 if source#hasaction (m_first + n)
3049 then Some (m_first + n)
3050 else None
3052 else None
3054 method display =
3055 Gl.enable `blend;
3056 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3057 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3058 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3059 GlDraw.color (1., 1., 1.);
3060 Gl.enable `texture_2d;
3061 let fs = fstate.fontsize in
3062 let nfs = fs + 1 in
3063 let ww = fstate.wwidth in
3064 let tabw = 30.0*.ww in
3065 let itemcount = source#getitemcount in
3066 let rec loop row =
3067 if (row - m_first) * nfs > conf.winh
3068 then ()
3069 else (
3070 if row >= 0 && row < itemcount
3071 then (
3072 let (s, level) = source#getitem row in
3073 let y = (row - m_first) * nfs in
3074 let x = 5.0 +. float (level + m_pan) *. ww in
3075 if row = m_active
3076 then (
3077 Gl.disable `texture_2d;
3078 GlDraw.polygon_mode `both `line;
3079 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3080 GlDraw.rect (1., float (y + 1))
3081 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3082 GlDraw.polygon_mode `both `fill;
3083 GlDraw.color (1., 1., 1.);
3084 Gl.enable `texture_2d;
3087 let drawtabularstring s =
3088 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3089 if trusted
3090 then
3091 let tabpos = try String.index s '\t' with Not_found -> -1 in
3092 if tabpos > 0
3093 then
3094 let len = String.length s - tabpos - 1 in
3095 let s1 = String.sub s 0 tabpos
3096 and s2 = String.sub s (tabpos + 1) len in
3097 let nx = drawstr x s1 in
3098 let sw = nx -. x in
3099 let x = x +. (max tabw sw) in
3100 drawstr x s2
3101 else
3102 drawstr x s
3103 else
3104 drawstr x s
3106 let _ = drawtabularstring s in
3107 loop (row+1)
3111 loop m_first;
3112 Gl.disable `blend;
3113 Gl.disable `texture_2d;
3115 method updownlevel incr =
3116 let len = source#getitemcount in
3117 let curlevel =
3118 if m_active >= 0 && m_active < len
3119 then snd (source#getitem m_active)
3120 else -1
3122 let rec flow i =
3123 if i = len then i-1 else if i = -1 then 0 else
3124 let _, l = source#getitem i in
3125 if l != curlevel then i else flow (i+incr)
3127 let active = flow m_active in
3128 let first = calcfirst m_first active in
3129 G.postRedisplay "outline updownlevel";
3130 {< m_active = active; m_first = first >}
3132 method private key1 key mask =
3133 let set1 active first qsearch =
3134 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3136 let search active pattern incr =
3137 let dosearch re =
3138 let rec loop n =
3139 if n >= 0 && n < source#getitemcount
3140 then (
3141 let s, _ = source#getitem n in
3143 (try ignore (Str.search_forward re s 0); true
3144 with Not_found -> false)
3145 then Some n
3146 else loop (n + incr)
3148 else None
3150 loop active
3153 let re = Str.regexp_case_fold pattern in
3154 dosearch re
3155 with Failure s ->
3156 state.text <- s;
3157 None
3159 let itemcount = source#getitemcount in
3160 let find start incr =
3161 let rec find i =
3162 if i = -1 || i = itemcount
3163 then -1
3164 else (
3165 if source#hasaction i
3166 then i
3167 else find (i + incr)
3170 find start
3172 let set active first =
3173 let first = bound first 0 (itemcount - fstate.maxrows) in
3174 state.text <- "";
3175 coe {< m_active = active; m_first = first >}
3177 let navigate incr =
3178 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3179 let active, first =
3180 let incr1 = if incr > 0 then 1 else -1 in
3181 if isvisible m_first m_active
3182 then
3183 let next =
3184 let next = m_active + incr in
3185 let next =
3186 if next < 0 || next >= itemcount
3187 then -1
3188 else find next incr1
3190 if next = -1 || abs (m_active - next) > fstate.maxrows
3191 then -1
3192 else next
3194 if next = -1
3195 then
3196 let first = m_first + incr in
3197 let first = bound first 0 (itemcount - 1) in
3198 let next =
3199 let next = m_active + incr in
3200 let next = bound next 0 (itemcount - 1) in
3201 find next ~-incr1
3203 let active = if next = -1 then m_active else next in
3204 active, first
3205 else
3206 let first = min next m_first in
3207 let first =
3208 if abs (next - first) > fstate.maxrows
3209 then first + incr
3210 else first
3212 next, first
3213 else
3214 let first = m_first + incr in
3215 let first = bound first 0 (itemcount - 1) in
3216 let active =
3217 let next = m_active + incr in
3218 let next = bound next 0 (itemcount - 1) in
3219 let next = find next incr1 in
3220 let active =
3221 if next = -1 || abs (m_active - first) > fstate.maxrows
3222 then (
3223 let active = if m_active = -1 then next else m_active in
3224 active
3226 else next
3228 if isvisible first active
3229 then active
3230 else -1
3232 active, first
3234 G.postRedisplay "listview navigate";
3235 set active first;
3237 match key with
3238 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3239 let incr = if key = 0x72 then -1 else 1 in
3240 let active, first =
3241 match search (m_active + incr) m_qsearch incr with
3242 | None ->
3243 state.text <- m_qsearch ^ " [not found]";
3244 m_active, m_first
3245 | Some active ->
3246 state.text <- m_qsearch;
3247 active, firstof m_first active
3249 G.postRedisplay "listview ctrl-r/s";
3250 set1 active first m_qsearch;
3252 | 0xff08 -> (* backspace *)
3253 if String.length m_qsearch = 0
3254 then coe self
3255 else (
3256 let qsearch = withoutlastutf8 m_qsearch in
3257 let len = String.length qsearch in
3258 if len = 0
3259 then (
3260 state.text <- "";
3261 G.postRedisplay "listview empty qsearch";
3262 set1 m_active m_first "";
3264 else
3265 let active, first =
3266 match search m_active qsearch ~-1 with
3267 | None ->
3268 state.text <- qsearch ^ " [not found]";
3269 m_active, m_first
3270 | Some active ->
3271 state.text <- qsearch;
3272 active, firstof m_first active
3274 G.postRedisplay "listview backspace qsearch";
3275 set1 active first qsearch
3278 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3279 let pattern = m_qsearch ^ Wsi.toutf8 key in
3280 let active, first =
3281 match search m_active pattern 1 with
3282 | None ->
3283 state.text <- pattern ^ " [not found]";
3284 m_active, m_first
3285 | Some active ->
3286 state.text <- pattern;
3287 active, firstof m_first active
3289 G.postRedisplay "listview qsearch add";
3290 set1 active first pattern;
3292 | 0xff1b -> (* escape *)
3293 state.text <- "";
3294 if String.length m_qsearch = 0
3295 then (
3296 G.postRedisplay "list view escape";
3297 begin
3298 match
3299 source#exit (coe self) true m_active m_first m_pan m_qsearch
3300 with
3301 | None -> m_prev_uioh
3302 | Some uioh -> uioh
3305 else (
3306 G.postRedisplay "list view kill qsearch";
3307 source#setqsearch "";
3308 coe {< m_qsearch = "" >}
3311 | 0xff0d -> (* return *)
3312 state.text <- "";
3313 let self = {< m_qsearch = "" >} in
3314 source#setqsearch "";
3315 let opt =
3316 G.postRedisplay "listview enter";
3317 if m_active >= 0 && m_active < source#getitemcount
3318 then (
3319 source#exit (coe self) false m_active m_first m_pan "";
3321 else (
3322 source#exit (coe self) true m_active m_first m_pan "";
3325 begin match opt with
3326 | None -> m_prev_uioh
3327 | Some uioh -> uioh
3330 | 0xff9f | 0xffff -> (* delete *)
3331 coe self
3333 | 0xff52 -> navigate ~-1 (* up *)
3334 | 0xff54 -> navigate 1 (* down *)
3335 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3336 | 0xff56 -> navigate fstate.maxrows (* next *)
3338 | 0xff53 -> (* right *)
3339 state.text <- "";
3340 G.postRedisplay "listview right";
3341 coe {< m_pan = m_pan - 1 >}
3343 | 0xff51 -> (* left *)
3344 state.text <- "";
3345 G.postRedisplay "listview left";
3346 coe {< m_pan = m_pan + 1 >}
3348 | 0xff50 -> (* home *)
3349 let active = find 0 1 in
3350 G.postRedisplay "listview home";
3351 set active 0;
3353 | 0xff57 -> (* end *)
3354 let first = max 0 (itemcount - fstate.maxrows) in
3355 let active = find (itemcount - 1) ~-1 in
3356 G.postRedisplay "listview end";
3357 set active first;
3359 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3360 coe self
3362 | _ ->
3363 dolog "listview unknown key %#x" key; coe self
3365 method key key mask =
3366 match state.mode with
3367 | Textentry te -> textentrykeyboard key mask te; coe self
3368 | _ -> self#key1 key mask
3370 method button button down x y _ =
3371 let opt =
3372 match button with
3373 | 1 when x > conf.winw - conf.scrollbw ->
3374 G.postRedisplay "listview scroll";
3375 if down
3376 then
3377 let _, position, sh = self#scrollph in
3378 if y > truncate position && y < truncate (position +. sh)
3379 then (
3380 state.mstate <- Mscrolly;
3381 Some (coe self)
3383 else
3384 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3385 let first = truncate (s *. float source#getitemcount) in
3386 let first = min source#getitemcount first in
3387 Some (coe {< m_first = first; m_active = first >})
3388 else (
3389 state.mstate <- Mnone;
3390 Some (coe self);
3392 | 1 when not down ->
3393 begin match self#elemunder y with
3394 | Some n ->
3395 G.postRedisplay "listview click";
3396 source#exit
3397 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3398 | _ ->
3399 Some (coe self)
3401 | n when (n == 4 || n == 5) && not down ->
3402 let len = source#getitemcount in
3403 let first =
3404 if n = 5 && m_first + fstate.maxrows >= len
3405 then
3406 m_first
3407 else
3408 let first = m_first + (if n == 4 then -1 else 1) in
3409 bound first 0 (len - 1)
3411 G.postRedisplay "listview wheel";
3412 Some (coe {< m_first = first >})
3413 | n when (n = 6 || n = 7) && not down ->
3414 let inc = m_first + (if n = 7 then -1 else 1) in
3415 G.postRedisplay "listview hwheel";
3416 Some (coe {< m_pan = m_pan + inc >})
3417 | _ ->
3418 Some (coe self)
3420 match opt with
3421 | None -> m_prev_uioh
3422 | Some uioh -> uioh
3424 method motion _ y =
3425 match state.mstate with
3426 | Mscrolly ->
3427 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3428 let first = truncate (s *. float source#getitemcount) in
3429 let first = min source#getitemcount first in
3430 G.postRedisplay "listview motion";
3431 coe {< m_first = first; m_active = first >}
3432 | _ -> coe self
3434 method pmotion x y =
3435 if x < conf.winw - conf.scrollbw
3436 then
3437 let n =
3438 match self#elemunder y with
3439 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3440 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3442 let o =
3443 if n != m_active
3444 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3445 else self
3447 coe o
3448 else (
3449 Wsi.setcursor Wsi.CURSOR_INHERIT;
3450 coe self
3453 method infochanged _ = ()
3455 method scrollpw = (0, 0.0, 0.0)
3456 method scrollph =
3457 let nfs = fstate.fontsize + 1 in
3458 let y = m_first * nfs in
3459 let itemcount = source#getitemcount in
3460 let maxi = max 0 (itemcount - fstate.maxrows) in
3461 let maxy = maxi * nfs in
3462 let p, h = scrollph y maxy in
3463 conf.scrollbw, p, h
3465 method modehash = modehash
3466 end;;
3468 class outlinelistview ~source =
3469 object (self)
3470 inherit listview
3471 ~source:(source :> lvsource)
3472 ~trusted:false
3473 ~modehash:(findkeyhash conf "outline")
3474 as super
3476 method key key mask =
3477 let calcfirst first active =
3478 if active > first
3479 then
3480 let rows = active - first in
3481 let maxrows =
3482 if String.length state.text = 0
3483 then fstate.maxrows
3484 else fstate.maxrows - 2
3486 if rows > maxrows then active - maxrows else first
3487 else active
3489 let navigate incr =
3490 let active = m_active + incr in
3491 let active = bound active 0 (source#getitemcount - 1) in
3492 let first = calcfirst m_first active in
3493 G.postRedisplay "outline navigate";
3494 coe {< m_active = active; m_first = first >}
3496 let ctrl = Wsi.withctrl mask in
3497 match key with
3498 | 110 when ctrl -> (* ctrl-n *)
3499 source#narrow m_qsearch;
3500 G.postRedisplay "outline ctrl-n";
3501 coe {< m_first = 0; m_active = 0 >}
3503 | 117 when ctrl -> (* ctrl-u *)
3504 source#denarrow;
3505 G.postRedisplay "outline ctrl-u";
3506 state.text <- "";
3507 coe {< m_first = 0; m_active = 0 >}
3509 | 108 when ctrl -> (* ctrl-l *)
3510 let first = m_active - (fstate.maxrows / 2) in
3511 G.postRedisplay "outline ctrl-l";
3512 coe {< m_first = first >}
3514 | 0xff9f | 0xffff -> (* delete *)
3515 source#remove m_active;
3516 G.postRedisplay "outline delete";
3517 let active = max 0 (m_active-1) in
3518 coe {< m_first = firstof m_first active;
3519 m_active = active >}
3521 | 0xff52 -> navigate ~-1 (* up *)
3522 | 0xff54 -> navigate 1 (* down *)
3523 | 0xff55 -> (* prior *)
3524 navigate ~-(fstate.maxrows)
3525 | 0xff56 -> (* next *)
3526 navigate fstate.maxrows
3528 | 0xff53 -> (* [ctrl-]right *)
3529 let o =
3530 if ctrl
3531 then (
3532 G.postRedisplay "outline ctrl right";
3533 {< m_pan = m_pan + 1 >}
3535 else self#updownlevel 1
3537 coe o
3539 | 0xff51 -> (* [ctrl-]left *)
3540 let o =
3541 if ctrl
3542 then (
3543 G.postRedisplay "outline ctrl left";
3544 {< m_pan = m_pan - 1 >}
3546 else self#updownlevel ~-1
3548 coe o
3550 | 0xff50 -> (* home *)
3551 G.postRedisplay "outline home";
3552 coe {< m_first = 0; m_active = 0 >}
3554 | 0xff57 -> (* end *)
3555 let active = source#getitemcount - 1 in
3556 let first = max 0 (active - fstate.maxrows) in
3557 G.postRedisplay "outline end";
3558 coe {< m_active = active; m_first = first >}
3560 | _ -> super#key key mask
3563 let outlinesource usebookmarks =
3564 let empty = [||] in
3565 (object
3566 inherit lvsourcebase
3567 val mutable m_items = empty
3568 val mutable m_orig_items = empty
3569 val mutable m_prev_items = empty
3570 val mutable m_narrow_pattern = ""
3571 val mutable m_hadremovals = false
3573 method getitemcount =
3574 Array.length m_items + (if m_hadremovals then 1 else 0)
3576 method getitem n =
3577 if n == Array.length m_items && m_hadremovals
3578 then
3579 ("[Confirm removal]", 0)
3580 else
3581 let s, n, _ = m_items.(n) in
3582 (s, n)
3584 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3585 ignore (uioh, first, qsearch);
3586 let confrimremoval = m_hadremovals && active = Array.length m_items in
3587 let items =
3588 if String.length m_narrow_pattern = 0
3589 then m_orig_items
3590 else m_items
3592 if not cancel
3593 then (
3594 if not confrimremoval
3595 then(
3596 let _, _, anchor = m_items.(active) in
3597 gotoanchor anchor;
3598 m_items <- items;
3600 else (
3601 state.bookmarks <- Array.to_list m_items;
3602 m_orig_items <- m_items;
3605 else m_items <- items;
3606 m_pan <- pan;
3607 None
3609 method hasaction _ = true
3611 method greetmsg =
3612 if Array.length m_items != Array.length m_orig_items
3613 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3614 else ""
3616 method narrow pattern =
3617 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3618 match reopt with
3619 | None -> ()
3620 | Some re ->
3621 let rec loop accu n =
3622 if n = -1
3623 then (
3624 m_narrow_pattern <- pattern;
3625 m_items <- Array.of_list accu
3627 else
3628 let (s, _, _) as o = m_items.(n) in
3629 let accu =
3630 if (try ignore (Str.search_forward re s 0); true
3631 with Not_found -> false)
3632 then o :: accu
3633 else accu
3635 loop accu (n-1)
3637 loop [] (Array.length m_items - 1)
3639 method denarrow =
3640 m_orig_items <- (
3641 if usebookmarks
3642 then Array.of_list state.bookmarks
3643 else state.outlines
3645 m_items <- m_orig_items
3647 method remove m =
3648 if usebookmarks
3649 then
3650 if m >= 0 && m < Array.length m_items
3651 then (
3652 m_hadremovals <- true;
3653 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3654 let n = if n >= m then n+1 else n in
3655 m_items.(n)
3659 method reset anchor items =
3660 m_hadremovals <- false;
3661 if m_orig_items == empty || m_prev_items != items
3662 then (
3663 m_orig_items <- items;
3664 if String.length m_narrow_pattern = 0
3665 then m_items <- items;
3667 m_prev_items <- items;
3668 let rely = getanchory anchor in
3669 let active =
3670 let rec loop n best bestd =
3671 if n = Array.length m_items
3672 then best
3673 else
3674 let (_, _, anchor) = m_items.(n) in
3675 let orely = getanchory anchor in
3676 let d = abs (orely - rely) in
3677 if d < bestd
3678 then loop (n+1) n d
3679 else loop (n+1) best bestd
3681 loop 0 ~-1 max_int
3683 m_active <- active;
3684 m_first <- firstof m_first active
3685 end)
3688 let enterselector usebookmarks =
3689 let source = outlinesource usebookmarks in
3690 fun errmsg ->
3691 let outlines =
3692 if usebookmarks
3693 then Array.of_list state.bookmarks
3694 else state.outlines
3696 if Array.length outlines = 0
3697 then (
3698 showtext ' ' errmsg;
3700 else (
3701 state.text <- source#greetmsg;
3702 Wsi.setcursor Wsi.CURSOR_INHERIT;
3703 let anchor = getanchor () in
3704 source#reset anchor outlines;
3705 state.uioh <- coe (new outlinelistview ~source);
3706 G.postRedisplay "enter selector";
3710 let enteroutlinemode =
3711 let f = enterselector false in
3712 fun ()-> f "Document has no outline";
3715 let enterbookmarkmode =
3716 let f = enterselector true in
3717 fun () -> f "Document has no bookmarks (yet)";
3720 let color_of_string s =
3721 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3722 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3726 let color_to_string (r, g, b) =
3727 let r = truncate (r *. 256.0)
3728 and g = truncate (g *. 256.0)
3729 and b = truncate (b *. 256.0) in
3730 Printf.sprintf "%d/%d/%d" r g b
3733 let irect_of_string s =
3734 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3737 let irect_to_string (x0,y0,x1,y1) =
3738 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3741 let makecheckers () =
3742 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3743 following to say:
3744 converted by Issac Trotts. July 25, 2002 *)
3745 let image_height = 64
3746 and image_width = 64 in
3748 let make_image () =
3749 let image =
3750 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3752 for i = 0 to image_width - 1 do
3753 for j = 0 to image_height - 1 do
3754 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3755 (if (i land 8 ) lxor (j land 8) = 0
3756 then [|255;255;255|] else [|200;200;200|])
3757 done
3758 done;
3759 image
3761 let image = make_image () in
3762 let id = GlTex.gen_texture () in
3763 GlTex.bind_texture `texture_2d id;
3764 GlPix.store (`unpack_alignment 1);
3765 GlTex.image2d image;
3766 List.iter (GlTex.parameter ~target:`texture_2d)
3767 [ `wrap_s `repeat;
3768 `wrap_t `repeat;
3769 `mag_filter `nearest;
3770 `min_filter `nearest ];
3774 let setcheckers enabled =
3775 match state.texid with
3776 | None ->
3777 if enabled then state.texid <- Some (makecheckers ())
3779 | Some texid ->
3780 if not enabled
3781 then (
3782 GlTex.delete_texture texid;
3783 state.texid <- None;
3787 let int_of_string_with_suffix s =
3788 let l = String.length s in
3789 let s1, shift =
3790 if l > 1
3791 then
3792 let suffix = Char.lowercase s.[l-1] in
3793 match suffix with
3794 | 'k' -> String.sub s 0 (l-1), 10
3795 | 'm' -> String.sub s 0 (l-1), 20
3796 | 'g' -> String.sub s 0 (l-1), 30
3797 | _ -> s, 0
3798 else s, 0
3800 let n = int_of_string s1 in
3801 let m = n lsl shift in
3802 if m < 0 || m < n
3803 then raise (Failure "value too large")
3804 else m
3807 let string_with_suffix_of_int n =
3808 if n = 0
3809 then "0"
3810 else
3811 let n, s =
3812 if n land ((1 lsl 20) - 1) = 0
3813 then n lsr 20, "M"
3814 else (
3815 if n land ((1 lsl 10) - 1) = 0
3816 then n lsr 10, "K"
3817 else n, ""
3820 let rec loop s n =
3821 let h = n mod 1000 in
3822 let n = n / 1000 in
3823 if n = 0
3824 then string_of_int h ^ s
3825 else (
3826 let s = Printf.sprintf "_%03d%s" h s in
3827 loop s n
3830 loop "" n ^ s;
3833 let defghyllscroll = (40, 8, 32);;
3834 let ghyllscroll_of_string s =
3835 let (n, a, b) as nab =
3836 if s = "default"
3837 then defghyllscroll
3838 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3840 if n <= a || n <= b || a >= b
3841 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3842 nab;
3845 let ghyllscroll_to_string ((n, a, b) as nab) =
3846 if nab = defghyllscroll
3847 then "default"
3848 else Printf.sprintf "%d,%d,%d" n a b;
3851 let describe_location () =
3852 let f (fn, _) l =
3853 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3855 let fn, ln = List.fold_left f (-1, -1) state.layout in
3856 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3857 let percent =
3858 if maxy <= 0
3859 then 100.
3860 else (100. *. (float state.y /. float maxy))
3862 if fn = ln
3863 then
3864 Printf.sprintf "page %d of %d [%.2f%%]"
3865 (fn+1) state.pagecount percent
3866 else
3867 Printf.sprintf
3868 "pages %d-%d of %d [%.2f%%]"
3869 (fn+1) (ln+1) state.pagecount percent
3872 let enterinfomode =
3873 let btos b = if b then "\xe2\x88\x9a" else "" in
3874 let showextended = ref false in
3875 let leave mode = function
3876 | Confirm -> state.mode <- mode
3877 | Cancel -> state.mode <- mode in
3878 let src =
3879 (object
3880 val mutable m_first_time = true
3881 val mutable m_l = []
3882 val mutable m_a = [||]
3883 val mutable m_prev_uioh = nouioh
3884 val mutable m_prev_mode = View
3886 inherit lvsourcebase
3888 method reset prev_mode prev_uioh =
3889 m_a <- Array.of_list (List.rev m_l);
3890 m_l <- [];
3891 m_prev_mode <- prev_mode;
3892 m_prev_uioh <- prev_uioh;
3893 if m_first_time
3894 then (
3895 let rec loop n =
3896 if n >= Array.length m_a
3897 then ()
3898 else
3899 match m_a.(n) with
3900 | _, _, _, Action _ -> m_active <- n
3901 | _ -> loop (n+1)
3903 loop 0;
3904 m_first_time <- false;
3907 method int name get set =
3908 m_l <-
3909 (name, `int get, 1, Action (
3910 fun u ->
3911 let ondone s =
3912 try set (int_of_string s)
3913 with exn ->
3914 state.text <- Printf.sprintf "bad integer `%s': %s"
3915 s (Printexc.to_string exn)
3917 state.text <- "";
3918 let te = name ^ ": ", "", None, intentry, ondone, true in
3919 state.mode <- Textentry (te, leave m_prev_mode);
3921 )) :: m_l
3923 method int_with_suffix name get set =
3924 m_l <-
3925 (name, `intws get, 1, Action (
3926 fun u ->
3927 let ondone s =
3928 try set (int_of_string_with_suffix s)
3929 with exn ->
3930 state.text <- Printf.sprintf "bad integer `%s': %s"
3931 s (Printexc.to_string exn)
3933 state.text <- "";
3934 let te =
3935 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3937 state.mode <- Textentry (te, leave m_prev_mode);
3939 )) :: m_l
3941 method bool ?(offset=1) ?(btos=btos) name get set =
3942 m_l <-
3943 (name, `bool (btos, get), offset, Action (
3944 fun u ->
3945 let v = get () in
3946 set (not v);
3948 )) :: m_l
3950 method color name get set =
3951 m_l <-
3952 (name, `color get, 1, Action (
3953 fun u ->
3954 let invalid = (nan, nan, nan) in
3955 let ondone s =
3956 let c =
3957 try color_of_string s
3958 with exn ->
3959 state.text <- Printf.sprintf "bad color `%s': %s"
3960 s (Printexc.to_string exn);
3961 invalid
3963 if c <> invalid
3964 then set c;
3966 let te = name ^ ": ", "", None, textentry, ondone, true in
3967 state.text <- color_to_string (get ());
3968 state.mode <- Textentry (te, leave m_prev_mode);
3970 )) :: m_l
3972 method string name get set =
3973 m_l <-
3974 (name, `string get, 1, Action (
3975 fun u ->
3976 let ondone s = set s in
3977 let te = name ^ ": ", "", None, textentry, ondone, true in
3978 state.mode <- Textentry (te, leave m_prev_mode);
3980 )) :: m_l
3982 method colorspace name get set =
3983 m_l <-
3984 (name, `string get, 1, Action (
3985 fun _ ->
3986 let source =
3987 let vals = [| "rgb"; "bgr"; "gray" |] in
3988 (object
3989 inherit lvsourcebase
3991 initializer
3992 m_active <- int_of_colorspace conf.colorspace;
3993 m_first <- 0;
3995 method getitemcount = Array.length vals
3996 method getitem n = (vals.(n), 0)
3997 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3998 ignore (uioh, first, pan, qsearch);
3999 if not cancel then set active;
4000 None
4001 method hasaction _ = true
4002 end)
4004 state.text <- "";
4005 let modehash = findkeyhash conf "info" in
4006 coe (new listview ~source ~trusted:true ~modehash)
4007 )) :: m_l
4009 method caption s offset =
4010 m_l <- (s, `empty, offset, Noaction) :: m_l
4012 method caption2 s f offset =
4013 m_l <- (s, `string f, offset, Noaction) :: m_l
4015 method getitemcount = Array.length m_a
4017 method getitem n =
4018 let tostr = function
4019 | `int f -> string_of_int (f ())
4020 | `intws f -> string_with_suffix_of_int (f ())
4021 | `string f -> f ()
4022 | `color f -> color_to_string (f ())
4023 | `bool (btos, f) -> btos (f ())
4024 | `empty -> ""
4026 let name, t, offset, _ = m_a.(n) in
4027 ((let s = tostr t in
4028 if String.length s > 0
4029 then Printf.sprintf "%s\t%s" name s
4030 else name),
4031 offset)
4033 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4034 let uiohopt =
4035 if not cancel
4036 then (
4037 m_qsearch <- qsearch;
4038 let uioh =
4039 match m_a.(active) with
4040 | _, _, _, Action f -> f uioh
4041 | _ -> uioh
4043 Some uioh
4045 else None
4047 m_active <- active;
4048 m_first <- first;
4049 m_pan <- pan;
4050 uiohopt
4052 method hasaction n =
4053 match m_a.(n) with
4054 | _, _, _, Action _ -> true
4055 | _ -> false
4056 end)
4058 let rec fillsrc prevmode prevuioh =
4059 let sep () = src#caption "" 0 in
4060 let colorp name get set =
4061 src#string name
4062 (fun () -> color_to_string (get ()))
4063 (fun v ->
4065 let c = color_of_string v in
4066 set c
4067 with exn ->
4068 state.text <- Printf.sprintf "bad color `%s': %s"
4069 v (Printexc.to_string exn);
4072 let oldmode = state.mode in
4073 let birdseye = isbirdseye state.mode in
4075 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4077 src#bool "presentation mode"
4078 (fun () -> conf.presentation)
4079 (fun v ->
4080 conf.presentation <- v;
4081 state.anchor <- getanchor ();
4082 represent ());
4084 src#bool "ignore case in searches"
4085 (fun () -> conf.icase)
4086 (fun v -> conf.icase <- v);
4088 src#bool "preload"
4089 (fun () -> conf.preload)
4090 (fun v -> conf.preload <- v);
4092 src#bool "highlight links"
4093 (fun () -> conf.hlinks)
4094 (fun v -> conf.hlinks <- v);
4096 src#bool "under info"
4097 (fun () -> conf.underinfo)
4098 (fun v -> conf.underinfo <- v);
4100 src#bool "persistent bookmarks"
4101 (fun () -> conf.savebmarks)
4102 (fun v -> conf.savebmarks <- v);
4104 src#bool "proportional display"
4105 (fun () -> conf.proportional)
4106 (fun v -> reqlayout conf.angle v);
4108 src#bool "trim margins"
4109 (fun () -> conf.trimmargins)
4110 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4112 src#bool "persistent location"
4113 (fun () -> conf.jumpback)
4114 (fun v -> conf.jumpback <- v);
4116 sep ();
4117 src#int "inter-page space"
4118 (fun () -> conf.interpagespace)
4119 (fun n ->
4120 conf.interpagespace <- n;
4121 docolumns conf.columns;
4122 let pageno, py =
4123 match state.layout with
4124 | [] -> 0, 0
4125 | l :: _ ->
4126 l.pageno, l.pagey
4128 state.maxy <- calcheight ();
4129 let y = getpagey pageno in
4130 gotoy (y + py)
4133 src#int "page bias"
4134 (fun () -> conf.pagebias)
4135 (fun v -> conf.pagebias <- v);
4137 src#int "scroll step"
4138 (fun () -> conf.scrollstep)
4139 (fun n -> conf.scrollstep <- n);
4141 src#int "horizontal scroll step"
4142 (fun () -> conf.hscrollstep)
4143 (fun v -> conf.hscrollstep <- v);
4145 src#int "auto scroll step"
4146 (fun () ->
4147 match state.autoscroll with
4148 | Some step -> step
4149 | _ -> conf.autoscrollstep)
4150 (fun n ->
4151 if state.autoscroll <> None
4152 then state.autoscroll <- Some n;
4153 conf.autoscrollstep <- n);
4155 src#int "zoom"
4156 (fun () -> truncate (conf.zoom *. 100.))
4157 (fun v -> setzoom ((float v) /. 100.));
4159 src#int "rotation"
4160 (fun () -> conf.angle)
4161 (fun v -> reqlayout v conf.proportional);
4163 src#int "scroll bar width"
4164 (fun () -> state.scrollw)
4165 (fun v ->
4166 state.scrollw <- v;
4167 conf.scrollbw <- v;
4168 reshape conf.winw conf.winh;
4171 src#int "scroll handle height"
4172 (fun () -> conf.scrollh)
4173 (fun v -> conf.scrollh <- v;);
4175 src#int "thumbnail width"
4176 (fun () -> conf.thumbw)
4177 (fun v ->
4178 conf.thumbw <- min 4096 v;
4179 match oldmode with
4180 | Birdseye beye ->
4181 leavebirdseye beye false;
4182 enterbirdseye ()
4183 | _ -> ()
4186 let mode = state.mode in
4187 src#string "columns"
4188 (fun () ->
4189 match conf.columns with
4190 | Csingle _ -> "1"
4191 | Cmulti (multi, _) -> multicolumns_to_string multi
4192 | Csplit (count, _) -> "-" ^ string_of_int count
4194 (fun v ->
4195 let n, a, b = multicolumns_of_string v in
4196 setcolumns mode n a b);
4198 sep ();
4199 src#caption "Presentation mode" 0;
4200 src#bool "scrollbar visible"
4201 (fun () -> conf.scrollbarinpm)
4202 (fun v ->
4203 if v != conf.scrollbarinpm
4204 then (
4205 conf.scrollbarinpm <- v;
4206 if conf.presentation
4207 then (
4208 state.scrollw <- if v then conf.scrollbw else 0;
4209 reshape conf.winw conf.winh;
4214 sep ();
4215 src#caption "Pixmap cache" 0;
4216 src#int_with_suffix "size (advisory)"
4217 (fun () -> conf.memlimit)
4218 (fun v -> conf.memlimit <- v);
4220 src#caption2 "used"
4221 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4222 (string_with_suffix_of_int state.memused)
4223 (Hashtbl.length state.tilemap)) 1;
4225 sep ();
4226 src#caption "Layout" 0;
4227 src#caption2 "Dimension"
4228 (fun () ->
4229 Printf.sprintf "%dx%d (virtual %dx%d) [pos %d]"
4230 conf.winw conf.winh
4231 state.w state.maxy state.y)
4233 if conf.debug
4234 then
4235 src#caption2 "Position" (fun () ->
4236 Printf.sprintf "%dx%d" state.x state.y
4238 else
4239 src#caption2 "Visible" (fun () -> describe_location ()) 1
4242 sep ();
4243 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4244 "Save these parameters as global defaults at exit"
4245 (fun () -> conf.bedefault)
4246 (fun v -> conf.bedefault <- v)
4249 sep ();
4250 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4251 src#bool ~offset:0 ~btos "Extended parameters"
4252 (fun () -> !showextended)
4253 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4254 if !showextended
4255 then (
4256 src#bool "checkers"
4257 (fun () -> conf.checkers)
4258 (fun v -> conf.checkers <- v; setcheckers v);
4259 src#bool "update cursor"
4260 (fun () -> conf.updatecurs)
4261 (fun v -> conf.updatecurs <- v);
4262 src#bool "verbose"
4263 (fun () -> conf.verbose)
4264 (fun v -> conf.verbose <- v);
4265 src#bool "invert colors"
4266 (fun () -> conf.invert)
4267 (fun v -> conf.invert <- v);
4268 src#bool "max fit"
4269 (fun () -> conf.maxhfit)
4270 (fun v -> conf.maxhfit <- v);
4271 src#bool "redirect stderr"
4272 (fun () -> conf.redirectstderr)
4273 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4274 src#string "uri launcher"
4275 (fun () -> conf.urilauncher)
4276 (fun v -> conf.urilauncher <- v);
4277 src#string "path launcher"
4278 (fun () -> conf.pathlauncher)
4279 (fun v -> conf.pathlauncher <- v);
4280 src#string "tile size"
4281 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4282 (fun v ->
4284 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4285 conf.tilew <- max 64 w;
4286 conf.tileh <- max 64 h;
4287 flushtiles ();
4288 with exn ->
4289 state.text <- Printf.sprintf "bad tile size `%s': %s"
4290 v (Printexc.to_string exn));
4291 src#int "texture count"
4292 (fun () -> conf.texcount)
4293 (fun v ->
4294 if realloctexts v
4295 then conf.texcount <- v
4296 else showtext '!' " Failed to set texture count please retry later"
4298 src#int "slice height"
4299 (fun () -> conf.sliceheight)
4300 (fun v ->
4301 conf.sliceheight <- v;
4302 wcmd "sliceh %d" conf.sliceheight;
4304 src#int "anti-aliasing level"
4305 (fun () -> conf.aalevel)
4306 (fun v ->
4307 conf.aalevel <- bound v 0 8;
4308 state.anchor <- getanchor ();
4309 opendoc state.path state.password;
4311 src#string "page scroll scaling factor"
4312 (fun () -> string_of_float conf.pgscale)
4313 (fun v ->
4315 let s = float_of_string v in
4316 conf.pgscale <- s
4317 with exn ->
4318 state.text <- Printf.sprintf
4319 "bad page scroll scaling factor `%s': %s"
4320 v (Printexc.to_string exn)
4323 src#int "ui font size"
4324 (fun () -> fstate.fontsize)
4325 (fun v -> setfontsize (bound v 5 100));
4326 src#int "hint font size"
4327 (fun () -> conf.hfsize)
4328 (fun v -> conf.hfsize <- bound v 5 100);
4329 colorp "background color"
4330 (fun () -> conf.bgcolor)
4331 (fun v -> conf.bgcolor <- v);
4332 src#bool "crop hack"
4333 (fun () -> conf.crophack)
4334 (fun v -> conf.crophack <- v);
4335 src#string "trim fuzz"
4336 (fun () -> irect_to_string conf.trimfuzz)
4337 (fun v ->
4339 conf.trimfuzz <- irect_of_string v;
4340 if conf.trimmargins
4341 then settrim true conf.trimfuzz;
4342 with exn ->
4343 state.text <- Printf.sprintf "bad irect `%s': %s"
4344 v (Printexc.to_string exn)
4346 src#string "throttle"
4347 (fun () ->
4348 match conf.maxwait with
4349 | None -> "show place holder if page is not ready"
4350 | Some time ->
4351 if time = infinity
4352 then "wait for page to fully render"
4353 else
4354 "wait " ^ string_of_float time
4355 ^ " seconds before showing placeholder"
4357 (fun v ->
4359 let f = float_of_string v in
4360 if f <= 0.0
4361 then conf.maxwait <- None
4362 else conf.maxwait <- Some f
4363 with exn ->
4364 state.text <- Printf.sprintf "bad time `%s': %s"
4365 v (Printexc.to_string exn)
4367 src#string "ghyll scroll"
4368 (fun () ->
4369 match conf.ghyllscroll with
4370 | None -> ""
4371 | Some nab -> ghyllscroll_to_string nab
4373 (fun v ->
4375 let gs =
4376 if String.length v = 0
4377 then None
4378 else Some (ghyllscroll_of_string v)
4380 conf.ghyllscroll <- gs
4381 with exn ->
4382 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4383 v (Printexc.to_string exn)
4385 src#string "selection command"
4386 (fun () -> conf.selcmd)
4387 (fun v -> conf.selcmd <- v);
4388 src#colorspace "color space"
4389 (fun () -> colorspace_to_string conf.colorspace)
4390 (fun v ->
4391 conf.colorspace <- colorspace_of_int v;
4392 wcmd "cs %d" v;
4393 load state.layout;
4397 sep ();
4398 src#caption "Document" 0;
4399 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4400 src#caption2 "Pages"
4401 (fun () -> string_of_int state.pagecount) 1;
4402 src#caption2 "Dimensions"
4403 (fun () -> string_of_int (List.length state.pdims)) 1;
4404 if conf.trimmargins
4405 then (
4406 sep ();
4407 src#caption "Trimmed margins" 0;
4408 src#caption2 "Dimensions"
4409 (fun () -> string_of_int (List.length state.pdims)) 1;
4412 sep ();
4413 src#caption "OpenGL" 0;
4414 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4415 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4416 src#reset prevmode prevuioh;
4418 fun () ->
4419 state.text <- "";
4420 let prevmode = state.mode
4421 and prevuioh = state.uioh in
4422 fillsrc prevmode prevuioh;
4423 let source = (src :> lvsource) in
4424 let modehash = findkeyhash conf "info" in
4425 state.uioh <- coe (object (self)
4426 inherit listview ~source ~trusted:true ~modehash as super
4427 val mutable m_prevmemused = 0
4428 method infochanged = function
4429 | Memused ->
4430 if m_prevmemused != state.memused
4431 then (
4432 m_prevmemused <- state.memused;
4433 G.postRedisplay "memusedchanged";
4435 | Pdim -> G.postRedisplay "pdimchanged"
4436 | Docinfo -> fillsrc prevmode prevuioh
4438 method key key mask =
4439 if not (Wsi.withctrl mask)
4440 then
4441 match key with
4442 | 0xff51 -> coe (self#updownlevel ~-1)
4443 | 0xff53 -> coe (self#updownlevel 1)
4444 | _ -> super#key key mask
4445 else super#key key mask
4446 end);
4447 G.postRedisplay "info";
4450 let enterhelpmode =
4451 let source =
4452 (object
4453 inherit lvsourcebase
4454 method getitemcount = Array.length state.help
4455 method getitem n =
4456 let s, n, _ = state.help.(n) in
4457 (s, n)
4459 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4460 let optuioh =
4461 if not cancel
4462 then (
4463 m_qsearch <- qsearch;
4464 match state.help.(active) with
4465 | _, _, Action f -> Some (f uioh)
4466 | _ -> Some (uioh)
4468 else None
4470 m_active <- active;
4471 m_first <- first;
4472 m_pan <- pan;
4473 optuioh
4475 method hasaction n =
4476 match state.help.(n) with
4477 | _, _, Action _ -> true
4478 | _ -> false
4480 initializer
4481 m_active <- -1
4482 end)
4483 in fun () ->
4484 let modehash = findkeyhash conf "help" in
4485 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4486 G.postRedisplay "help";
4489 let entermsgsmode =
4490 let msgsource =
4491 let re = Str.regexp "[\r\n]" in
4492 (object
4493 inherit lvsourcebase
4494 val mutable m_items = [||]
4496 method getitemcount = 1 + Array.length m_items
4498 method getitem n =
4499 if n = 0
4500 then "[Clear]", 0
4501 else m_items.(n-1), 0
4503 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4504 ignore uioh;
4505 if not cancel
4506 then (
4507 if active = 0
4508 then Buffer.clear state.errmsgs;
4509 m_qsearch <- qsearch;
4511 m_active <- active;
4512 m_first <- first;
4513 m_pan <- pan;
4514 None
4516 method hasaction n =
4517 n = 0
4519 method reset =
4520 state.newerrmsgs <- false;
4521 let l = Str.split re (Buffer.contents state.errmsgs) in
4522 m_items <- Array.of_list l
4524 initializer
4525 m_active <- 0
4526 end)
4527 in fun () ->
4528 state.text <- "";
4529 msgsource#reset;
4530 let source = (msgsource :> lvsource) in
4531 let modehash = findkeyhash conf "listview" in
4532 state.uioh <- coe (object
4533 inherit listview ~source ~trusted:false ~modehash as super
4534 method display =
4535 if state.newerrmsgs
4536 then msgsource#reset;
4537 super#display
4538 end);
4539 G.postRedisplay "msgs";
4542 let quickbookmark ?title () =
4543 match state.layout with
4544 | [] -> ()
4545 | l :: _ ->
4546 let title =
4547 match title with
4548 | None ->
4549 let sec = Unix.gettimeofday () in
4550 let tm = Unix.localtime sec in
4551 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4552 (l.pageno+1)
4553 tm.Unix.tm_mday
4554 tm.Unix.tm_mon
4555 (tm.Unix.tm_year + 1900)
4556 tm.Unix.tm_hour
4557 tm.Unix.tm_min
4558 | Some title -> title
4560 state.bookmarks <-
4561 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4562 :: state.bookmarks
4565 let doreshape w h =
4566 state.fullscreen <- None;
4567 Wsi.reshape w h;
4570 let setautoscrollspeed step goingdown =
4571 let incr = max 1 ((abs step) / 2) in
4572 let incr = if goingdown then incr else -incr in
4573 let astep = step + incr in
4574 state.autoscroll <- Some astep;
4577 let gotounder = function
4578 | Ulinkgoto (pageno, top) ->
4579 if pageno >= 0
4580 then (
4581 addnav ();
4582 gotopage1 pageno top;
4585 | Ulinkuri s ->
4586 gotouri s
4588 | Uremote (filename, pageno) ->
4589 let path =
4590 if Sys.file_exists filename
4591 then filename
4592 else
4593 let dir = Filename.dirname state.path in
4594 let path = Filename.concat dir filename in
4595 if Sys.file_exists path
4596 then path
4597 else ""
4599 if String.length path > 0
4600 then (
4601 let anchor = getanchor () in
4602 let ranchor = state.path, state.password, anchor in
4603 state.anchor <- (pageno, 0.0);
4604 state.ranchors <- ranchor :: state.ranchors;
4605 opendoc path "";
4607 else showtext '!' ("Could not find " ^ filename)
4609 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4612 let canpan () =
4613 match conf.columns with
4614 | Csplit _ -> true
4615 | _ -> conf.zoom > 1.0
4618 let viewkeyboard key mask =
4619 let enttext te =
4620 let mode = state.mode in
4621 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4622 state.text <- "";
4623 enttext ();
4624 G.postRedisplay "view:enttext"
4626 let ctrl = Wsi.withctrl mask in
4627 match key with
4628 | 81 -> (* Q *)
4629 exit 0
4631 | 0xff63 -> (* insert *)
4632 if conf.angle mod 360 = 0
4633 then (
4634 state.mode <- LinkNav (Ltgendir 0);
4635 gotoy state.y;
4637 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4639 | 0xff1b | 113 -> (* escape / q *)
4640 begin match state.mstate with
4641 | Mzoomrect _ ->
4642 state.mstate <- Mnone;
4643 Wsi.setcursor Wsi.CURSOR_INHERIT;
4644 G.postRedisplay "kill zoom rect";
4645 | _ ->
4646 match state.ranchors with
4647 | [] -> raise Quit
4648 | (path, password, anchor) :: rest ->
4649 state.ranchors <- rest;
4650 state.anchor <- anchor;
4651 opendoc path password
4652 end;
4654 | 0xff08 -> (* backspace *)
4655 let y = getnav ~-1 in
4656 gotoy_and_clear_text y
4658 | 111 -> (* o *)
4659 enteroutlinemode ()
4661 | 117 -> (* u *)
4662 state.rects <- [];
4663 state.text <- "";
4664 G.postRedisplay "dehighlight";
4666 | 47 | 63 -> (* / ? *)
4667 let ondone isforw s =
4668 cbput state.hists.pat s;
4669 state.searchpattern <- s;
4670 search s isforw
4672 let s = String.create 1 in
4673 s.[0] <- Char.chr key;
4674 enttext (s, "", Some (onhist state.hists.pat),
4675 textentry, ondone (key = 47), true)
4677 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-=*)
4678 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4679 setzoom (conf.zoom +. incr)
4681 | 43 | 0xffab -> (* + *)
4682 let ondone s =
4683 let n =
4684 try int_of_string s with exc ->
4685 state.text <- Printf.sprintf "bad integer `%s': %s"
4686 s (Printexc.to_string exc);
4687 max_int
4689 if n != max_int
4690 then (
4691 conf.pagebias <- n;
4692 state.text <- "page bias is now " ^ string_of_int n;
4695 enttext ("page bias: ", "", None, intentry, ondone, true)
4697 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4698 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4699 setzoom (max 0.01 (conf.zoom -. decr))
4701 | 45 | 0xffad -> (* - *)
4702 let ondone msg = state.text <- msg in
4703 enttext (
4704 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4705 optentry state.mode, ondone, true
4708 | 48 when ctrl -> (* ctrl-0 *)
4709 setzoom 1.0
4711 | 49 when ctrl -> (* ctrl-1 *)
4712 let cols =
4713 match conf.columns with
4714 | Csingle _ | Cmulti _ -> 1
4715 | Csplit (n, _) -> n
4717 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4718 if zoom < 1.0
4719 then setzoom zoom
4721 | 0xffc6 -> (* f9 *)
4722 togglebirdseye ()
4724 | 57 when ctrl -> (* ctrl-9 *)
4725 togglebirdseye ()
4727 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4728 when not ctrl -> (* 0..9 *)
4729 let ondone s =
4730 let n =
4731 try int_of_string s with exc ->
4732 state.text <- Printf.sprintf "bad integer `%s': %s"
4733 s (Printexc.to_string exc);
4736 if n >= 0
4737 then (
4738 addnav ();
4739 cbput state.hists.pag (string_of_int n);
4740 gotopage1 (n + conf.pagebias - 1) 0;
4743 let pageentry text key =
4744 match Char.unsafe_chr key with
4745 | 'g' -> TEdone text
4746 | _ -> intentry text key
4748 let text = "x" in text.[0] <- Char.chr key;
4749 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4751 | 98 -> (* b *)
4752 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4753 reshape conf.winw conf.winh;
4755 | 108 -> (* l *)
4756 conf.hlinks <- not conf.hlinks;
4757 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4758 G.postRedisplay "toggle highlightlinks";
4760 | 70 -> (* F *)
4761 state.glinks <- true;
4762 let mode = state.mode in
4763 state.mode <- Textentry (
4764 (":", "", None, linknentry, linkndone (fun under ->
4765 addnav ();
4766 gotounder under
4767 ), false
4768 ), fun _ ->
4769 state.glinks <- false;
4770 state.mode <- mode
4772 state.text <- "";
4773 G.postRedisplay "view:linkent(F)"
4775 | 121 -> (* y *)
4776 state.glinks <- true;
4777 let mode = state.mode in
4778 state.mode <- Textentry (
4779 (":", "", None, linknentry, linkndone (fun under ->
4780 match Ne.pipe () with
4781 | Ne.Exn exn ->
4782 showtext '!' (Printf.sprintf "pipe failed: %s"
4783 (Printexc.to_string exn));
4784 | Ne.Res (r, w) ->
4785 let popened =
4786 try popen conf.selcmd [r, 0; w, -1]; true
4787 with exn ->
4788 showtext '!'
4789 (Printf.sprintf "failed to execute %s: %s"
4790 conf.selcmd (Printexc.to_string exn));
4791 false
4793 let clo cap fd =
4794 Ne.clo fd (fun msg ->
4795 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4798 let s = undertext under in
4799 if popened
4800 then
4801 (try
4802 let l = String.length s in
4803 let n = Unix.write w s 0 l in
4804 if n != l
4805 then
4806 showtext '!'
4807 (Printf.sprintf
4808 "failed to write %d characters to sel pipe, wrote %d"
4811 with exn ->
4812 showtext '!'
4813 (Printf.sprintf "failed to write to sel pipe: %s"
4814 (Printexc.to_string exn)
4817 else dolog "%s" s;
4818 clo "pipe/r" r;
4819 clo "pipe/w" w;
4820 ), false
4822 fun _ ->
4823 state.glinks <- false;
4824 state.mode <- mode
4826 state.text <- "";
4827 G.postRedisplay "view:linkent"
4829 | 97 -> (* a *)
4830 begin match state.autoscroll with
4831 | Some step ->
4832 conf.autoscrollstep <- step;
4833 state.autoscroll <- None
4834 | None ->
4835 if conf.autoscrollstep = 0
4836 then state.autoscroll <- Some 1
4837 else state.autoscroll <- Some conf.autoscrollstep
4840 | 112 when ctrl -> (* ctrl-p *)
4841 launchpath ()
4843 | 80 -> (* P *)
4844 conf.presentation <- not conf.presentation;
4845 if conf.presentation
4846 then (
4847 if not conf.scrollbarinpm
4848 then state.scrollw <- 0;
4850 else
4851 state.scrollw <- conf.scrollbw;
4853 showtext ' ' ("presentation mode " ^
4854 if conf.presentation then "on" else "off");
4855 state.anchor <- getanchor ();
4856 represent ()
4858 | 102 -> (* f *)
4859 begin match state.fullscreen with
4860 | None ->
4861 state.fullscreen <- Some (conf.winw, conf.winh);
4862 Wsi.fullscreen ()
4863 | Some (w, h) ->
4864 state.fullscreen <- None;
4865 doreshape w h
4868 | 103 -> (* g *)
4869 gotoy_and_clear_text 0
4871 | 71 -> (* G *)
4872 gotopage1 (state.pagecount - 1) 0
4874 | 112 | 78 -> (* p|N *)
4875 search state.searchpattern false
4877 | 110 | 0xffc0 -> (* n|F3 *)
4878 search state.searchpattern true
4880 | 116 -> (* t *)
4881 begin match state.layout with
4882 | [] -> ()
4883 | l :: _ ->
4884 gotoy_and_clear_text (getpagey l.pageno)
4887 | 32 -> (* space *)
4888 begin match state.layout with
4889 | [] -> ()
4890 | l :: rest ->
4891 match conf.columns with
4892 | Csingle _ | Cmulti _ ->
4893 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4894 then
4895 let y = clamp (pgscale conf.winh) in
4896 gotoy_and_clear_text y
4897 else
4898 let pageno = min (l.pageno+1) (state.pagecount-1) in
4899 gotoy_and_clear_text (getpagey pageno)
4900 | Csplit (n, _) ->
4901 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4902 then
4903 let pagey, pageh = getpageyh l.pageno in
4904 let pagey = pagey + pageh * l.pagecol in
4905 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4906 gotoy_and_clear_text (pagey + pageh + ips)
4909 | 0xff9f | 0xffff -> (* delete *)
4910 begin match state.layout with
4911 | [] -> ()
4912 | l :: _ ->
4913 match conf.columns with
4914 | Csingle _ | Cmulti _ ->
4915 if conf.presentation && (l.pagey != 0 || l.pagedispy < 0)
4916 then
4917 gotoy_and_clear_text (clamp (pgscale ~-(conf.winh)))
4918 else
4919 let pageno = max 0 (l.pageno-1) in
4920 gotoy_and_clear_text (getpagey pageno)
4921 | Csplit (n, _) ->
4922 let y =
4923 if l.pagecol = 0
4924 then
4925 if l.pageno = 0
4926 then l.pagey
4927 else
4928 let pageno = max 0 (l.pageno-1) in
4929 let pagey, pageh = getpageyh pageno in
4930 pagey + (n-1)*pageh
4931 else
4932 let pagey, pageh = getpageyh l.pageno in
4933 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4935 gotoy_and_clear_text y
4938 | 61 -> (* = *)
4939 showtext ' ' (describe_location ());
4941 | 119 -> (* w *)
4942 begin match state.layout with
4943 | [] -> ()
4944 | l :: _ ->
4945 doreshape (l.pagew + state.scrollw) l.pageh;
4946 G.postRedisplay "w"
4949 | 39 -> (* ' *)
4950 enterbookmarkmode ()
4952 | 104 | 0xffbe -> (* h|F1 *)
4953 enterhelpmode ()
4955 | 105 -> (* i *)
4956 enterinfomode ()
4958 | 101 when conf.redirectstderr -> (* e *)
4959 entermsgsmode ()
4961 | 109 -> (* m *)
4962 let ondone s =
4963 match state.layout with
4964 | l :: _ ->
4965 state.bookmarks <-
4966 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4967 :: state.bookmarks
4968 | _ -> ()
4970 enttext ("bookmark: ", "", None, textentry, ondone, true)
4972 | 126 -> (* ~ *)
4973 quickbookmark ();
4974 showtext ' ' "Quick bookmark added";
4976 | 122 -> (* z *)
4977 begin match state.layout with
4978 | l :: _ ->
4979 let rect = getpdimrect l.pagedimno in
4980 let w, h =
4981 if conf.crophack
4982 then
4983 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4984 truncate (1.2 *. (rect.(3) -. rect.(0))))
4985 else
4986 (truncate (rect.(1) -. rect.(0)),
4987 truncate (rect.(3) -. rect.(0)))
4989 let w = truncate ((float w)*.conf.zoom)
4990 and h = truncate ((float h)*.conf.zoom) in
4991 if w != 0 && h != 0
4992 then (
4993 state.anchor <- getanchor ();
4994 doreshape (w + state.scrollw) (h + conf.interpagespace)
4996 G.postRedisplay "z";
4998 | [] -> ()
5001 | 50 when ctrl -> (* ctrl-2 *)
5002 let maxw = getmaxw () in
5003 if maxw > 0.0
5004 then setzoom (maxw /. float conf.winw)
5006 | 60 | 62 -> (* < > *)
5007 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5009 | 91 | 93 -> (* [ ] *)
5010 conf.colorscale <-
5011 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5013 G.postRedisplay "brightness";
5015 | 99 when state.mode = View -> (* c *)
5016 let (c, a, b), z =
5017 match state.prevcolumns with
5018 | None -> (1, 0, 0), 1.0
5019 | Some (columns, z) ->
5020 let cab =
5021 match columns with
5022 | Csplit (c, _) -> -c, 0, 0
5023 | Cmulti ((c, a, b), _) -> c, a, b
5024 | Csingle _ -> 1, 0, 0
5026 cab, z
5028 setcolumns View c a b;
5029 setzoom z;
5031 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5032 setzoom state.prevzoom
5034 | 107 | 0xff52 -> (* k up *)
5035 begin match state.autoscroll with
5036 | None ->
5037 begin match state.mode with
5038 | Birdseye beye -> upbirdseye 1 beye
5039 | _ ->
5040 if ctrl
5041 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5042 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5044 | Some n ->
5045 setautoscrollspeed n false
5048 | 106 | 0xff54 -> (* j down *)
5049 begin match state.autoscroll with
5050 | None ->
5051 begin match state.mode with
5052 | Birdseye beye -> downbirdseye 1 beye
5053 | _ ->
5054 if ctrl
5055 then gotoy_and_clear_text (clamp (conf.winh/2))
5056 else gotoy_and_clear_text (clamp conf.scrollstep)
5058 | Some n ->
5059 setautoscrollspeed n true
5062 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5063 if canpan ()
5064 then
5065 let dx =
5066 if ctrl
5067 then conf.winw / 2
5068 else 10
5070 let dx = if key = 0xff51 then dx else -dx in
5071 state.x <- state.x + dx;
5072 gotoy_and_clear_text state.y
5073 else (
5074 state.text <- "";
5075 G.postRedisplay "lef/right"
5078 | 0xff55 -> (* prior *)
5079 let y =
5080 if ctrl
5081 then
5082 match state.layout with
5083 | [] -> state.y
5084 | l :: _ -> state.y - l.pagey
5085 else
5086 clamp (pgscale (-conf.winh))
5088 gotoghyll y
5090 | 0xff56 -> (* next *)
5091 let y =
5092 if ctrl
5093 then
5094 match List.rev state.layout with
5095 | [] -> state.y
5096 | l :: _ -> getpagey l.pageno
5097 else
5098 clamp (pgscale conf.winh)
5100 gotoghyll y
5102 | 0xff50 -> gotoghyll 0
5103 | 0xff57 -> gotoghyll (clamp state.maxy)
5104 | 0xff53 when Wsi.withalt mask ->
5105 gotoghyll (getnav ~-1)
5106 | 0xff51 when Wsi.withalt mask ->
5107 gotoghyll (getnav 1)
5109 | 114 -> (* r *)
5110 state.anchor <- getanchor ();
5111 opendoc state.path state.password
5113 | 118 when conf.debug -> (* v *)
5114 state.rects <- [];
5115 List.iter (fun l ->
5116 match getopaque l.pageno with
5117 | None -> ()
5118 | Some opaque ->
5119 let x0, y0, x1, y1 = pagebbox opaque in
5120 let a,b = float x0, float y0 in
5121 let c,d = float x1, float y0 in
5122 let e,f = float x1, float y1 in
5123 let h,j = float x0, float y1 in
5124 let rect = (a,b,c,d,e,f,h,j) in
5125 debugrect rect;
5126 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5127 ) state.layout;
5128 G.postRedisplay "v";
5130 | _ ->
5131 vlog "huh? %s" (Wsi.keyname key)
5134 let linknavkeyboard key mask linknav =
5135 let getpage pageno =
5136 let rec loop = function
5137 | [] -> None
5138 | l :: _ when l.pageno = pageno -> Some l
5139 | _ :: rest -> loop rest
5140 in loop state.layout
5142 let doexact (pageno, n) =
5143 match getopaque pageno, getpage pageno with
5144 | Some opaque, Some l ->
5145 if key = 0xff0d
5146 then
5147 let under = getlink opaque n in
5148 G.postRedisplay "link gotounder";
5149 gotounder under;
5150 state.mode <- View;
5151 else
5152 let opt, dir =
5153 match key with
5154 | 0xff50 -> (* home *)
5155 Some (findlink opaque LDfirst), -1
5157 | 0xff57 -> (* end *)
5158 Some (findlink opaque LDlast), 1
5160 | 0xff51 -> (* left *)
5161 Some (findlink opaque (LDleft n)), -1
5163 | 0xff53 -> (* right *)
5164 Some (findlink opaque (LDright n)), 1
5166 | 0xff52 -> (* up *)
5167 Some (findlink opaque (LDup n)), -1
5169 | 0xff54 -> (* down *)
5170 Some (findlink opaque (LDdown n)), 1
5172 | _ -> None, 0
5174 let pwl l dir =
5175 begin match findpwl l.pageno dir with
5176 | Pwlnotfound -> ()
5177 | Pwl pageno ->
5178 let notfound dir =
5179 state.mode <- LinkNav (Ltgendir dir);
5180 let y, h = getpageyh pageno in
5181 let y =
5182 if dir < 0
5183 then y + h - conf.winh
5184 else y
5186 gotoy y
5188 begin match getopaque pageno, getpage pageno with
5189 | Some opaque, Some _ ->
5190 let link =
5191 let ld = if dir > 0 then LDfirst else LDlast in
5192 findlink opaque ld
5194 begin match link with
5195 | Lfound m ->
5196 showlinktype (getlink opaque m);
5197 state.mode <- LinkNav (Ltexact (pageno, m));
5198 G.postRedisplay "linknav jpage";
5199 | _ -> notfound dir
5200 end;
5201 | _ -> notfound dir
5202 end;
5203 end;
5205 begin match opt with
5206 | Some Lnotfound -> pwl l dir;
5207 | Some (Lfound m) ->
5208 if m = n
5209 then pwl l dir
5210 else (
5211 let _, y0, _, y1 = getlinkrect opaque m in
5212 if y0 < l.pagey
5213 then gotopage1 l.pageno y0
5214 else (
5215 let d = fstate.fontsize + 1 in
5216 if y1 - l.pagey > l.pagevh - d
5217 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5218 else G.postRedisplay "linknav";
5220 showlinktype (getlink opaque m);
5221 state.mode <- LinkNav (Ltexact (l.pageno, m));
5224 | None -> viewkeyboard key mask
5225 end;
5226 | _ -> viewkeyboard key mask
5228 if key = 0xff63
5229 then (
5230 state.mode <- View;
5231 G.postRedisplay "leave linknav"
5233 else
5234 match linknav with
5235 | Ltgendir _ -> viewkeyboard key mask
5236 | Ltexact exact -> doexact exact
5239 let keyboard key mask =
5240 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5241 then wcmd "interrupt"
5242 else state.uioh <- state.uioh#key key mask
5245 let birdseyekeyboard key mask
5246 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5247 let incr =
5248 match conf.columns with
5249 | Csingle _ -> 1
5250 | Cmulti ((c, _, _), _) -> c
5251 | Csplit _ -> failwith "bird's eye split mode"
5253 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5254 match key with
5255 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5256 let y, h = getpageyh pageno in
5257 let top = (conf.winh - h) / 2 in
5258 gotoy (max 0 (y - top))
5259 | 0xff0d -> leavebirdseye beye false
5260 | 0xff1b -> leavebirdseye beye true (* escape *)
5261 | 0xff52 -> upbirdseye incr beye (* up *)
5262 | 0xff54 -> downbirdseye incr beye (* down *)
5263 | 0xff51 -> upbirdseye 1 beye (* left *)
5264 | 0xff53 -> downbirdseye 1 beye (* right *)
5266 | 0xff55 -> (* prior *)
5267 begin match state.layout with
5268 | l :: _ ->
5269 if l.pagey != 0
5270 then (
5271 state.mode <- Birdseye (
5272 oconf, leftx, l.pageno, hooverpageno, anchor
5274 gotopage1 l.pageno 0;
5276 else (
5277 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5278 match layout with
5279 | [] -> gotoy (clamp (-conf.winh))
5280 | l :: _ ->
5281 state.mode <- Birdseye (
5282 oconf, leftx, l.pageno, hooverpageno, anchor
5284 gotopage1 l.pageno 0
5287 | [] -> gotoy (clamp (-conf.winh))
5288 end;
5290 | 0xff56 -> (* next *)
5291 begin match List.rev state.layout with
5292 | l :: _ ->
5293 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5294 begin match layout with
5295 | [] ->
5296 let incr = l.pageh - l.pagevh in
5297 if incr = 0
5298 then (
5299 state.mode <-
5300 Birdseye (
5301 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5303 G.postRedisplay "birdseye pagedown";
5305 else gotoy (clamp (incr + conf.interpagespace*2));
5307 | l :: _ ->
5308 state.mode <-
5309 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5310 gotopage1 l.pageno 0;
5313 | [] -> gotoy (clamp conf.winh)
5314 end;
5316 | 0xff50 -> (* home *)
5317 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5318 gotopage1 0 0
5320 | 0xff57 -> (* end *)
5321 let pageno = state.pagecount - 1 in
5322 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5323 if not (pagevisible state.layout pageno)
5324 then
5325 let h =
5326 match List.rev state.pdims with
5327 | [] -> conf.winh
5328 | (_, _, h, _) :: _ -> h
5330 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5331 else G.postRedisplay "birdseye end";
5332 | _ -> viewkeyboard key mask
5335 let drawpage l linkindexbase =
5336 let color =
5337 match state.mode with
5338 | Textentry _ -> scalecolor 0.4
5339 | LinkNav _
5340 | View -> scalecolor 1.0
5341 | Birdseye (_, _, pageno, hooverpageno, _) ->
5342 if l.pageno = hooverpageno
5343 then scalecolor 0.9
5344 else (
5345 if l.pageno = pageno
5346 then scalecolor 1.0
5347 else scalecolor 0.8
5350 drawtiles l color;
5351 begin match getopaque l.pageno with
5352 | Some opaque ->
5353 if tileready l l.pagex l.pagey
5354 then
5355 let x = l.pagedispx - l.pagex
5356 and y = l.pagedispy - l.pagey in
5357 let hlmask =
5358 match conf.columns with
5359 | Csingle _ | Cmulti _ ->
5360 (if conf.hlinks then 1 else 0)
5361 + (if state.glinks
5362 && not (isbirdseye state.mode) then 2 else 0)
5363 | _ -> 0
5365 let s =
5366 match state.mode with
5367 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5368 | _ -> ""
5370 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5371 else 0
5373 | _ -> 0
5374 end;
5377 let scrollindicator () =
5378 let sbw, ph, sh = state.uioh#scrollph in
5379 let sbh, pw, sw = state.uioh#scrollpw in
5381 GlDraw.color (0.64, 0.64, 0.64);
5382 GlDraw.rect
5383 (float (conf.winw - sbw), 0.)
5384 (float conf.winw, float conf.winh)
5386 GlDraw.rect
5387 (0., float (conf.winh - sbh))
5388 (float (conf.winw - state.scrollw - 1), float conf.winh)
5390 GlDraw.color (0.0, 0.0, 0.0);
5392 GlDraw.rect
5393 (float (conf.winw - sbw), ph)
5394 (float conf.winw, ph +. sh)
5396 GlDraw.rect
5397 (pw, float (conf.winh - sbh))
5398 (pw +. sw, float conf.winh)
5402 let showsel () =
5403 match state.mstate with
5404 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5407 | Msel ((x0, y0), (x1, y1)) ->
5408 let rec loop = function
5409 | l :: ls ->
5410 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5411 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5412 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5413 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5414 then
5415 match getopaque l.pageno with
5416 | Some opaque ->
5417 let x0, y0 = pagetranslatepoint l x0 y0 in
5418 let x1, y1 = pagetranslatepoint l x1 y1 in
5419 seltext opaque (x0, y0, x1, y1);
5420 | _ -> ()
5421 else loop ls
5422 | [] -> ()
5424 loop state.layout
5427 let showrects rects =
5428 Gl.enable `blend;
5429 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5430 GlDraw.polygon_mode `both `fill;
5431 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5432 List.iter
5433 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5434 List.iter (fun l ->
5435 if l.pageno = pageno
5436 then (
5437 let dx = float (l.pagedispx - l.pagex) in
5438 let dy = float (l.pagedispy - l.pagey) in
5439 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5440 GlDraw.begins `quads;
5442 GlDraw.vertex2 (x0+.dx, y0+.dy);
5443 GlDraw.vertex2 (x1+.dx, y1+.dy);
5444 GlDraw.vertex2 (x2+.dx, y2+.dy);
5445 GlDraw.vertex2 (x3+.dx, y3+.dy);
5447 GlDraw.ends ();
5449 ) state.layout
5450 ) rects
5452 Gl.disable `blend;
5455 let display () =
5456 GlClear.color (scalecolor2 conf.bgcolor);
5457 GlClear.clear [`color];
5458 let rec loop linkindexbase = function
5459 | l :: rest ->
5460 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5461 loop linkindexbase rest
5462 | [] -> ()
5464 loop 0 state.layout;
5465 let rects =
5466 match state.mode with
5467 | LinkNav (Ltexact (pageno, linkno)) ->
5468 begin match getopaque pageno with
5469 | Some opaque ->
5470 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5471 (pageno, 5, (
5472 float x0, float y0,
5473 float x1, float y0,
5474 float x1, float y1,
5475 float x0, float y1)
5476 ) :: state.rects
5477 | None -> state.rects
5479 | _ -> state.rects
5481 showrects rects;
5482 showsel ();
5483 state.uioh#display;
5484 begin match state.mstate with
5485 | Mzoomrect ((x0, y0), (x1, y1)) ->
5486 Gl.enable `blend;
5487 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5488 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5489 GlDraw.rect (float x0, float y0)
5490 (float x1, float y1);
5491 Gl.disable `blend;
5492 | _ -> ()
5493 end;
5494 enttext ();
5495 scrollindicator ();
5496 Wsi.swapb ();
5499 let zoomrect x y x1 y1 =
5500 let x0 = min x x1
5501 and x1 = max x x1
5502 and y0 = min y y1 in
5503 gotoy (state.y + y0);
5504 state.anchor <- getanchor ();
5505 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5506 let margin =
5507 if state.w < conf.winw - state.scrollw
5508 then (conf.winw - state.scrollw - state.w) / 2
5509 else 0
5511 state.x <- (state.x + margin) - x0;
5512 setzoom zoom;
5513 Wsi.setcursor Wsi.CURSOR_INHERIT;
5514 state.mstate <- Mnone;
5517 let scrollx x =
5518 let winw = conf.winw - state.scrollw - 1 in
5519 let s = float x /. float winw in
5520 let destx = truncate (float (state.w + winw) *. s) in
5521 state.x <- winw - destx;
5522 gotoy_and_clear_text state.y;
5523 state.mstate <- Mscrollx;
5526 let scrolly y =
5527 let s = float y /. float conf.winh in
5528 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5529 gotoy_and_clear_text desty;
5530 state.mstate <- Mscrolly;
5533 let viewmouse button down x y mask =
5534 match button with
5535 | n when (n == 4 || n == 5) && not down ->
5536 if Wsi.withctrl mask
5537 then (
5538 match state.mstate with
5539 | Mzoom (oldn, i) ->
5540 if oldn = n
5541 then (
5542 if i = 2
5543 then
5544 let incr =
5545 match n with
5546 | 5 ->
5547 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5548 | _ ->
5549 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5551 let zoom = conf.zoom -. incr in
5552 setzoom zoom;
5553 state.mstate <- Mzoom (n, 0);
5554 else
5555 state.mstate <- Mzoom (n, i+1);
5557 else state.mstate <- Mzoom (n, 0)
5559 | _ -> state.mstate <- Mzoom (n, 0)
5561 else (
5562 match state.autoscroll with
5563 | Some step -> setautoscrollspeed step (n=4)
5564 | None ->
5565 let incr =
5566 if n = 4
5567 then -conf.scrollstep
5568 else conf.scrollstep
5570 let incr = incr * 2 in
5571 let y = clamp incr in
5572 gotoy_and_clear_text y
5575 | n when (n = 6 || n = 7) && not down && canpan () ->
5576 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5577 gotoy_and_clear_text state.y
5579 | 1 when Wsi.withctrl mask ->
5580 if down
5581 then (
5582 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5583 state.mstate <- Mpan (x, y)
5585 else
5586 state.mstate <- Mnone
5588 | 3 ->
5589 if down
5590 then (
5591 Wsi.setcursor Wsi.CURSOR_CYCLE;
5592 let p = (x, y) in
5593 state.mstate <- Mzoomrect (p, p)
5595 else (
5596 match state.mstate with
5597 | Mzoomrect ((x0, y0), _) ->
5598 if abs (x-x0) > 10 && abs (y - y0) > 10
5599 then zoomrect x0 y0 x y
5600 else (
5601 state.mstate <- Mnone;
5602 Wsi.setcursor Wsi.CURSOR_INHERIT;
5603 G.postRedisplay "kill accidental zoom rect";
5605 | _ ->
5606 Wsi.setcursor Wsi.CURSOR_INHERIT;
5607 state.mstate <- Mnone
5610 | 1 when x > conf.winw - state.scrollw ->
5611 if down
5612 then
5613 let _, position, sh = state.uioh#scrollph in
5614 if y > truncate position && y < truncate (position +. sh)
5615 then state.mstate <- Mscrolly
5616 else scrolly y
5617 else
5618 state.mstate <- Mnone
5620 | 1 when y > conf.winh - state.hscrollh ->
5621 if down
5622 then
5623 let _, position, sw = state.uioh#scrollpw in
5624 if x > truncate position && x < truncate (position +. sw)
5625 then state.mstate <- Mscrollx
5626 else scrollx x
5627 else
5628 state.mstate <- Mnone
5630 | 1 ->
5631 let dest = if down then getunder x y else Unone in
5632 begin match dest with
5633 | Ulinkgoto _
5634 | Ulinkuri _
5635 | Uremote _
5636 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5637 gotounder dest
5639 | Unone when down ->
5640 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5641 state.mstate <- Mpan (x, y);
5643 | Unone | Utext _ ->
5644 if down
5645 then (
5646 if conf.angle mod 360 = 0
5647 then (
5648 state.mstate <- Msel ((x, y), (x, y));
5649 G.postRedisplay "mouse select";
5652 else (
5653 match state.mstate with
5654 | Mnone -> ()
5656 | Mzoom _ | Mscrollx | Mscrolly ->
5657 state.mstate <- Mnone
5659 | Mzoomrect ((x0, y0), _) ->
5660 zoomrect x0 y0 x y
5662 | Mpan _ ->
5663 Wsi.setcursor Wsi.CURSOR_INHERIT;
5664 state.mstate <- Mnone
5666 | Msel ((_, y0), (_, y1)) ->
5667 let rec loop = function
5668 | [] -> ()
5669 | l :: rest ->
5670 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5671 || ((y1 >= l.pagedispy
5672 && y1 <= (l.pagedispy + l.pagevh)))
5673 then
5674 match getopaque l.pageno with
5675 | Some opaque ->
5676 begin
5677 match Ne.pipe () with
5678 | Ne.Exn exn ->
5679 showtext '!'
5680 (Printf.sprintf
5681 "can not create sel pipe: %s"
5682 (Printexc.to_string exn));
5683 | Ne.Res (r, w) ->
5684 let doclose what fd =
5685 Ne.clo fd (fun msg ->
5686 dolog "%s close failed: %s" what msg)
5689 popen conf.selcmd [r, 0; w, -1];
5690 copysel w opaque;
5691 doclose "pipe/r" r;
5692 G.postRedisplay "copysel";
5693 with exn ->
5694 dolog "can not execute %S: %s"
5695 conf.selcmd (Printexc.to_string exn);
5696 doclose "pipe/r" r;
5697 doclose "pipe/w" w;
5699 | None -> ()
5700 else loop rest
5702 loop state.layout;
5703 Wsi.setcursor Wsi.CURSOR_INHERIT;
5704 state.mstate <- Mnone;
5708 | _ -> ()
5711 let birdseyemouse button down x y mask
5712 (conf, leftx, _, hooverpageno, anchor) =
5713 match button with
5714 | 1 when down ->
5715 let rec loop = function
5716 | [] -> ()
5717 | l :: rest ->
5718 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5719 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5720 then (
5721 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5723 else loop rest
5725 loop state.layout
5726 | 3 -> ()
5727 | _ -> viewmouse button down x y mask
5730 let mouse button down x y mask =
5731 state.uioh <- state.uioh#button button down x y mask;
5734 let motion ~x ~y =
5735 state.uioh <- state.uioh#motion x y
5738 let pmotion ~x ~y =
5739 state.uioh <- state.uioh#pmotion x y;
5742 let uioh = object
5743 method display = ()
5745 method key key mask =
5746 begin match state.mode with
5747 | Textentry textentry -> textentrykeyboard key mask textentry
5748 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5749 | View -> viewkeyboard key mask
5750 | LinkNav linknav -> linknavkeyboard key mask linknav
5751 end;
5752 state.uioh
5754 method button button bstate x y mask =
5755 begin match state.mode with
5756 | LinkNav _
5757 | View -> viewmouse button bstate x y mask
5758 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5759 | Textentry _ -> ()
5760 end;
5761 state.uioh
5763 method motion x y =
5764 begin match state.mode with
5765 | Textentry _ -> ()
5766 | View | Birdseye _ | LinkNav _ ->
5767 match state.mstate with
5768 | Mzoom _ | Mnone -> ()
5770 | Mpan (x0, y0) ->
5771 let dx = x - x0
5772 and dy = y0 - y in
5773 state.mstate <- Mpan (x, y);
5774 if canpan ()
5775 then state.x <- state.x + dx;
5776 let y = clamp dy in
5777 gotoy_and_clear_text y
5779 | Msel (a, _) ->
5780 state.mstate <- Msel (a, (x, y));
5781 G.postRedisplay "motion select";
5783 | Mscrolly ->
5784 let y = min conf.winh (max 0 y) in
5785 scrolly y
5787 | Mscrollx ->
5788 let x = min conf.winw (max 0 x) in
5789 scrollx x
5791 | Mzoomrect (p0, _) ->
5792 state.mstate <- Mzoomrect (p0, (x, y));
5793 G.postRedisplay "motion zoomrect";
5794 end;
5795 state.uioh
5797 method pmotion x y =
5798 begin match state.mode with
5799 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5800 let rec loop = function
5801 | [] ->
5802 if hooverpageno != -1
5803 then (
5804 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5805 G.postRedisplay "pmotion birdseye no hoover";
5807 | l :: rest ->
5808 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5809 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5810 then (
5811 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5812 G.postRedisplay "pmotion birdseye hoover";
5814 else loop rest
5816 loop state.layout
5818 | Textentry _ -> ()
5820 | LinkNav _
5821 | View ->
5822 match state.mstate with
5823 | Mnone -> updateunder x y
5824 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5826 end;
5827 state.uioh
5829 method infochanged _ = ()
5831 method scrollph =
5832 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5833 let p, h = scrollph state.y maxy in
5834 state.scrollw, p, h
5836 method scrollpw =
5837 let winw = conf.winw - state.scrollw - 1 in
5838 let fwinw = float winw in
5839 let sw =
5840 let sw = fwinw /. float state.w in
5841 let sw = fwinw *. sw in
5842 max sw (float conf.scrollh)
5844 let position, sw =
5845 let f = state.w+winw in
5846 let r = float (winw-state.x) /. float f in
5847 let p = fwinw *. r in
5848 p-.sw/.2., sw
5850 let sw =
5851 if position +. sw > fwinw
5852 then fwinw -. position
5853 else sw
5855 state.hscrollh, position, sw
5857 method modehash =
5858 let modename =
5859 match state.mode with
5860 | LinkNav _ -> "links"
5861 | Textentry _ -> "textentry"
5862 | Birdseye _ -> "birdseye"
5863 | View -> "view"
5865 findkeyhash conf modename
5866 end;;
5868 module Config =
5869 struct
5870 open Parser
5872 let fontpath = ref "";;
5874 module KeyMap =
5875 Map.Make (struct type t = (int * int) let compare = compare end);;
5877 let unent s =
5878 let l = String.length s in
5879 let b = Buffer.create l in
5880 unent b s 0 l;
5881 Buffer.contents b;
5884 let home =
5885 try Sys.getenv "HOME"
5886 with exn ->
5887 prerr_endline
5888 ("Can not determine home directory location: " ^
5889 Printexc.to_string exn);
5893 let modifier_of_string = function
5894 | "alt" -> Wsi.altmask
5895 | "shift" -> Wsi.shiftmask
5896 | "ctrl" | "control" -> Wsi.ctrlmask
5897 | "meta" -> Wsi.metamask
5898 | _ -> 0
5901 let key_of_string =
5902 let r = Str.regexp "-" in
5903 fun s ->
5904 let elems = Str.full_split r s in
5905 let f n k m =
5906 let g s =
5907 let m1 = modifier_of_string s in
5908 if m1 = 0
5909 then (Wsi.namekey s, m)
5910 else (k, m lor m1)
5911 in function
5912 | Str.Delim s when n land 1 = 0 -> g s
5913 | Str.Text s -> g s
5914 | Str.Delim _ -> (k, m)
5916 let rec loop n k m = function
5917 | [] -> (k, m)
5918 | x :: xs ->
5919 let k, m = f n k m x in
5920 loop (n+1) k m xs
5922 loop 0 0 0 elems
5925 let keys_of_string =
5926 let r = Str.regexp "[ \t]" in
5927 fun s ->
5928 let elems = Str.split r s in
5929 List.map key_of_string elems
5932 let copykeyhashes c =
5933 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5936 let config_of c attrs =
5937 let apply c k v =
5939 match k with
5940 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5941 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5942 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5943 | "preload" -> { c with preload = bool_of_string v }
5944 | "page-bias" -> { c with pagebias = int_of_string v }
5945 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5946 | "horizontal-scroll-step" ->
5947 { c with hscrollstep = max (int_of_string v) 1 }
5948 | "auto-scroll-step" ->
5949 { c with autoscrollstep = max 0 (int_of_string v) }
5950 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5951 | "crop-hack" -> { c with crophack = bool_of_string v }
5952 | "throttle" ->
5953 let mw =
5954 match String.lowercase v with
5955 | "true" -> Some infinity
5956 | "false" -> None
5957 | f -> Some (float_of_string f)
5959 { c with maxwait = mw}
5960 | "highlight-links" -> { c with hlinks = bool_of_string v }
5961 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5962 | "vertical-margin" ->
5963 { c with interpagespace = max 0 (int_of_string v) }
5964 | "zoom" ->
5965 let zoom = float_of_string v /. 100. in
5966 let zoom = max zoom 0.0 in
5967 { c with zoom = zoom }
5968 | "presentation" -> { c with presentation = bool_of_string v }
5969 | "rotation-angle" -> { c with angle = int_of_string v }
5970 | "width" -> { c with winw = max 20 (int_of_string v) }
5971 | "height" -> { c with winh = max 20 (int_of_string v) }
5972 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5973 | "proportional-display" -> { c with proportional = bool_of_string v }
5974 | "pixmap-cache-size" ->
5975 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5976 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5977 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5978 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5979 | "persistent-location" -> { c with jumpback = bool_of_string v }
5980 | "background-color" -> { c with bgcolor = color_of_string v }
5981 | "scrollbar-in-presentation" ->
5982 { c with scrollbarinpm = bool_of_string v }
5983 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5984 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5985 | "mupdf-store-size" ->
5986 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5987 | "checkers" -> { c with checkers = bool_of_string v }
5988 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5989 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5990 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5991 | "uri-launcher" -> { c with urilauncher = unent v }
5992 | "path-launcher" -> { c with pathlauncher = unent v }
5993 | "color-space" -> { c with colorspace = colorspace_of_string v }
5994 | "invert-colors" -> { c with invert = bool_of_string v }
5995 | "brightness" -> { c with colorscale = float_of_string v }
5996 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5997 | "ghyllscroll" ->
5998 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5999 | "columns" ->
6000 let (n, _, _) as nab = multicolumns_of_string v in
6001 if n < 0
6002 then { c with columns = Csplit (-n, [||]) }
6003 else { c with columns = Cmulti (nab, [||]) }
6004 | "birds-eye-columns" ->
6005 { c with beyecolumns = Some (max (int_of_string v) 2) }
6006 | "selection-command" -> { c with selcmd = unent v }
6007 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6008 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6009 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6010 | _ -> c
6011 with exn ->
6012 prerr_endline ("Error processing attribute (`" ^
6013 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6016 let rec fold c = function
6017 | [] -> c
6018 | (k, v) :: rest ->
6019 let c = apply c k v in
6020 fold c rest
6022 fold { c with keyhashes = copykeyhashes c } attrs;
6025 let fromstring f pos n v d =
6026 try f v
6027 with exn ->
6028 dolog "Error processing attribute (%S=%S) at %d\n%s"
6029 n v pos (Printexc.to_string exn)
6034 let bookmark_of attrs =
6035 let rec fold title page rely = function
6036 | ("title", v) :: rest -> fold v page rely rest
6037 | ("page", v) :: rest -> fold title v rely rest
6038 | ("rely", v) :: rest -> fold title page v rest
6039 | _ :: rest -> fold title page rely rest
6040 | [] -> title, page, rely
6042 fold "invalid" "0" "0" attrs
6045 let doc_of attrs =
6046 let rec fold path page rely pan = function
6047 | ("path", v) :: rest -> fold v page rely pan rest
6048 | ("page", v) :: rest -> fold path v rely pan rest
6049 | ("rely", v) :: rest -> fold path page v pan rest
6050 | ("pan", v) :: rest -> fold path page rely v rest
6051 | _ :: rest -> fold path page rely pan rest
6052 | [] -> path, page, rely, pan
6054 fold "" "0" "0" "0" attrs
6057 let map_of attrs =
6058 let rec fold rs ls = function
6059 | ("out", v) :: rest -> fold v ls rest
6060 | ("in", v) :: rest -> fold rs v rest
6061 | _ :: rest -> fold ls rs rest
6062 | [] -> ls, rs
6064 fold "" "" attrs
6067 let setconf dst src =
6068 dst.scrollbw <- src.scrollbw;
6069 dst.scrollh <- src.scrollh;
6070 dst.icase <- src.icase;
6071 dst.preload <- src.preload;
6072 dst.pagebias <- src.pagebias;
6073 dst.verbose <- src.verbose;
6074 dst.scrollstep <- src.scrollstep;
6075 dst.maxhfit <- src.maxhfit;
6076 dst.crophack <- src.crophack;
6077 dst.autoscrollstep <- src.autoscrollstep;
6078 dst.maxwait <- src.maxwait;
6079 dst.hlinks <- src.hlinks;
6080 dst.underinfo <- src.underinfo;
6081 dst.interpagespace <- src.interpagespace;
6082 dst.zoom <- src.zoom;
6083 dst.presentation <- src.presentation;
6084 dst.angle <- src.angle;
6085 dst.winw <- src.winw;
6086 dst.winh <- src.winh;
6087 dst.savebmarks <- src.savebmarks;
6088 dst.memlimit <- src.memlimit;
6089 dst.proportional <- src.proportional;
6090 dst.texcount <- src.texcount;
6091 dst.sliceheight <- src.sliceheight;
6092 dst.thumbw <- src.thumbw;
6093 dst.jumpback <- src.jumpback;
6094 dst.bgcolor <- src.bgcolor;
6095 dst.scrollbarinpm <- src.scrollbarinpm;
6096 dst.tilew <- src.tilew;
6097 dst.tileh <- src.tileh;
6098 dst.mustoresize <- src.mustoresize;
6099 dst.checkers <- src.checkers;
6100 dst.aalevel <- src.aalevel;
6101 dst.trimmargins <- src.trimmargins;
6102 dst.trimfuzz <- src.trimfuzz;
6103 dst.urilauncher <- src.urilauncher;
6104 dst.colorspace <- src.colorspace;
6105 dst.invert <- src.invert;
6106 dst.colorscale <- src.colorscale;
6107 dst.redirectstderr <- src.redirectstderr;
6108 dst.ghyllscroll <- src.ghyllscroll;
6109 dst.columns <- src.columns;
6110 dst.beyecolumns <- src.beyecolumns;
6111 dst.selcmd <- src.selcmd;
6112 dst.updatecurs <- src.updatecurs;
6113 dst.pathlauncher <- src.pathlauncher;
6114 dst.keyhashes <- copykeyhashes src;
6115 dst.hfsize <- src.hfsize;
6116 dst.hscrollstep <- src.hscrollstep;
6117 dst.pgscale <- src.pgscale;
6120 let get s =
6121 let h = Hashtbl.create 10 in
6122 let dc = { defconf with angle = defconf.angle } in
6123 let rec toplevel v t spos _ =
6124 match t with
6125 | Vdata | Vcdata | Vend -> v
6126 | Vopen ("llppconfig", _, closed) ->
6127 if closed
6128 then v
6129 else { v with f = llppconfig }
6130 | Vopen _ ->
6131 error "unexpected subelement at top level" s spos
6132 | Vclose _ -> error "unexpected close at top level" s spos
6134 and llppconfig v t spos _ =
6135 match t with
6136 | Vdata | Vcdata -> v
6137 | Vend -> error "unexpected end of input in llppconfig" s spos
6138 | Vopen ("defaults", attrs, closed) ->
6139 let c = config_of dc attrs in
6140 setconf dc c;
6141 if closed
6142 then v
6143 else { v with f = defaults }
6145 | Vopen ("ui-font", attrs, closed) ->
6146 let rec getsize size = function
6147 | [] -> size
6148 | ("size", v) :: rest ->
6149 let size =
6150 fromstring int_of_string spos "size" v fstate.fontsize in
6151 getsize size rest
6152 | l -> getsize size l
6154 fstate.fontsize <- getsize fstate.fontsize attrs;
6155 if closed
6156 then v
6157 else { v with f = uifont (Buffer.create 10) }
6159 | Vopen ("doc", attrs, closed) ->
6160 let pathent, spage, srely, span = doc_of attrs in
6161 let path = unent pathent
6162 and pageno = fromstring int_of_string spos "page" spage 0
6163 and rely = fromstring float_of_string spos "rely" srely 0.0
6164 and pan = fromstring int_of_string spos "pan" span 0 in
6165 let c = config_of dc attrs in
6166 let anchor = (pageno, rely) in
6167 if closed
6168 then (Hashtbl.add h path (c, [], pan, anchor); v)
6169 else { v with f = doc path pan anchor c [] }
6171 | Vopen _ ->
6172 error "unexpected subelement in llppconfig" s spos
6174 | Vclose "llppconfig" -> { v with f = toplevel }
6175 | Vclose _ -> error "unexpected close in llppconfig" s spos
6177 and defaults v t spos _ =
6178 match t with
6179 | Vdata | Vcdata -> v
6180 | Vend -> error "unexpected end of input in defaults" s spos
6181 | Vopen ("keymap", attrs, closed) ->
6182 let modename =
6183 try List.assoc "mode" attrs
6184 with Not_found -> "global" in
6185 if closed
6186 then v
6187 else
6188 let ret keymap =
6189 let h = findkeyhash dc modename in
6190 KeyMap.iter (Hashtbl.replace h) keymap;
6191 defaults
6193 { v with f = pkeymap ret KeyMap.empty }
6195 | Vopen (_, _, _) ->
6196 error "unexpected subelement in defaults" s spos
6198 | Vclose "defaults" ->
6199 { v with f = llppconfig }
6201 | Vclose _ -> error "unexpected close in defaults" s spos
6203 and uifont b v t spos epos =
6204 match t with
6205 | Vdata | Vcdata ->
6206 Buffer.add_substring b s spos (epos - spos);
6208 | Vopen (_, _, _) ->
6209 error "unexpected subelement in ui-font" s spos
6210 | Vclose "ui-font" ->
6211 if String.length !fontpath = 0
6212 then fontpath := Buffer.contents b;
6213 { v with f = llppconfig }
6214 | Vclose _ -> error "unexpected close in ui-font" s spos
6215 | Vend -> error "unexpected end of input in ui-font" s spos
6217 and doc path pan anchor c bookmarks v t spos _ =
6218 match t with
6219 | Vdata | Vcdata -> v
6220 | Vend -> error "unexpected end of input in doc" s spos
6221 | Vopen ("bookmarks", _, closed) ->
6222 if closed
6223 then v
6224 else { v with f = pbookmarks path pan anchor c bookmarks }
6226 | Vopen ("keymap", attrs, closed) ->
6227 let modename =
6228 try List.assoc "mode" attrs
6229 with Not_found -> "global"
6231 if closed
6232 then v
6233 else
6234 let ret keymap =
6235 let h = findkeyhash c modename in
6236 KeyMap.iter (Hashtbl.replace h) keymap;
6237 doc path pan anchor c bookmarks
6239 { v with f = pkeymap ret KeyMap.empty }
6241 | Vopen (_, _, _) ->
6242 error "unexpected subelement in doc" s spos
6244 | Vclose "doc" ->
6245 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6246 { v with f = llppconfig }
6248 | Vclose _ -> error "unexpected close in doc" s spos
6250 and pkeymap ret keymap v t spos _ =
6251 match t with
6252 | Vdata | Vcdata -> v
6253 | Vend -> error "unexpected end of input in keymap" s spos
6254 | Vopen ("map", attrs, closed) ->
6255 let r, l = map_of attrs in
6256 let kss = fromstring keys_of_string spos "in" r [] in
6257 let lss = fromstring keys_of_string spos "out" l [] in
6258 let keymap =
6259 match kss with
6260 | [] -> keymap
6261 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6262 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6264 if closed
6265 then { v with f = pkeymap ret keymap }
6266 else
6267 let f () = v in
6268 { v with f = skip "map" f }
6270 | Vopen _ ->
6271 error "unexpected subelement in keymap" s spos
6273 | Vclose "keymap" ->
6274 { v with f = ret keymap }
6276 | Vclose _ -> error "unexpected close in keymap" s spos
6278 and pbookmarks path pan anchor c bookmarks v t spos _ =
6279 match t with
6280 | Vdata | Vcdata -> v
6281 | Vend -> error "unexpected end of input in bookmarks" s spos
6282 | Vopen ("item", attrs, closed) ->
6283 let titleent, spage, srely = bookmark_of attrs in
6284 let page = fromstring int_of_string spos "page" spage 0
6285 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6286 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6287 if closed
6288 then { v with f = pbookmarks path pan anchor c bookmarks }
6289 else
6290 let f () = v in
6291 { v with f = skip "item" f }
6293 | Vopen _ ->
6294 error "unexpected subelement in bookmarks" s spos
6296 | Vclose "bookmarks" ->
6297 { v with f = doc path pan anchor c bookmarks }
6299 | Vclose _ -> error "unexpected close in bookmarks" s spos
6301 and skip tag f v t spos _ =
6302 match t with
6303 | Vdata | Vcdata -> v
6304 | Vend ->
6305 error ("unexpected end of input in skipped " ^ tag) s spos
6306 | Vopen (tag', _, closed) ->
6307 if closed
6308 then v
6309 else
6310 let f' () = { v with f = skip tag f } in
6311 { v with f = skip tag' f' }
6312 | Vclose ctag ->
6313 if tag = ctag
6314 then f ()
6315 else error ("unexpected close in skipped " ^ tag) s spos
6318 parse { f = toplevel; accu = () } s;
6319 h, dc;
6322 let do_load f ic =
6324 let len = in_channel_length ic in
6325 let s = String.create len in
6326 really_input ic s 0 len;
6327 f s;
6328 with
6329 | Parse_error (msg, s, pos) ->
6330 let subs = subs s pos in
6331 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6332 failwith ("parse error: " ^ s)
6334 | exn ->
6335 failwith ("config load error: " ^ Printexc.to_string exn)
6338 let defconfpath =
6339 let dir =
6341 let dir = Filename.concat home ".config" in
6342 if Sys.is_directory dir then dir else home
6343 with _ -> home
6345 Filename.concat dir "llpp.conf"
6348 let confpath = ref defconfpath;;
6350 let load1 f =
6351 if Sys.file_exists !confpath
6352 then
6353 match
6354 (try Some (open_in_bin !confpath)
6355 with exn ->
6356 prerr_endline
6357 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6358 Printexc.to_string exn);
6359 None
6361 with
6362 | Some ic ->
6363 begin try
6364 f (do_load get ic)
6365 with exn ->
6366 prerr_endline
6367 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6368 Printexc.to_string exn);
6369 end;
6370 close_in ic;
6372 | None -> ()
6373 else
6374 f (Hashtbl.create 0, defconf)
6377 let load () =
6378 let f (h, dc) =
6379 let pc, pb, px, pa =
6381 Hashtbl.find h (Filename.basename state.path)
6382 with Not_found -> dc, [], 0, (0, 0.0)
6384 setconf defconf dc;
6385 setconf conf pc;
6386 state.bookmarks <- pb;
6387 state.x <- px;
6388 state.scrollw <- conf.scrollbw;
6389 if conf.jumpback
6390 then state.anchor <- pa;
6391 cbput state.hists.nav pa;
6393 load1 f
6396 let add_attrs bb always dc c =
6397 let ob s a b =
6398 if always || a != b
6399 then Printf.bprintf bb "\n %s='%b'" s a
6400 and oi s a b =
6401 if always || a != b
6402 then Printf.bprintf bb "\n %s='%d'" s a
6403 and oI s a b =
6404 if always || a != b
6405 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6406 and oz s a b =
6407 if always || a <> b
6408 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6409 and oF s a b =
6410 if always || a <> b
6411 then Printf.bprintf bb "\n %s='%f'" s a
6412 and oc s a b =
6413 if always || a <> b
6414 then
6415 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6416 and oC s a b =
6417 if always || a <> b
6418 then
6419 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6420 and oR s a b =
6421 if always || a <> b
6422 then
6423 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6424 and os s a b =
6425 if always || a <> b
6426 then
6427 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6428 and og s a b =
6429 if always || a <> b
6430 then
6431 match a with
6432 | None -> ()
6433 | Some (_N, _A, _B) ->
6434 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6435 and oW s a b =
6436 if always || a <> b
6437 then
6438 let v =
6439 match a with
6440 | None -> "false"
6441 | Some f ->
6442 if f = infinity
6443 then "true"
6444 else string_of_float f
6446 Printf.bprintf bb "\n %s='%s'" s v
6447 and oco s a b =
6448 if always || a <> b
6449 then
6450 match a with
6451 | Cmulti ((n, a, b), _) when n > 1 ->
6452 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6453 | Csplit (n, _) when n > 1 ->
6454 Printf.bprintf bb "\n %s='%d'" s ~-n
6455 | _ -> ()
6456 and obeco s a b =
6457 if always || a <> b
6458 then
6459 match a with
6460 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6461 | _ -> ()
6463 let w, h =
6464 if always
6465 then dc.winw, dc.winh
6466 else
6467 match state.fullscreen with
6468 | Some wh -> wh
6469 | None -> c.winw, c.winh
6471 let zoom, presentation, interpagespace, maxwait =
6472 if always
6473 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6474 else
6475 match state.mode with
6476 | Birdseye (bc, _, _, _, _) ->
6477 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6478 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6480 oi "width" w dc.winw;
6481 oi "height" h dc.winh;
6482 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6483 oi "scroll-handle-height" c.scrollh dc.scrollh;
6484 ob "case-insensitive-search" c.icase dc.icase;
6485 ob "preload" c.preload dc.preload;
6486 oi "page-bias" c.pagebias dc.pagebias;
6487 oi "scroll-step" c.scrollstep dc.scrollstep;
6488 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6489 ob "max-height-fit" c.maxhfit dc.maxhfit;
6490 ob "crop-hack" c.crophack dc.crophack;
6491 oW "throttle" maxwait dc.maxwait;
6492 ob "highlight-links" c.hlinks dc.hlinks;
6493 ob "under-cursor-info" c.underinfo dc.underinfo;
6494 oi "vertical-margin" interpagespace dc.interpagespace;
6495 oz "zoom" zoom dc.zoom;
6496 ob "presentation" presentation dc.presentation;
6497 oi "rotation-angle" c.angle dc.angle;
6498 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6499 ob "proportional-display" c.proportional dc.proportional;
6500 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6501 oi "tex-count" c.texcount dc.texcount;
6502 oi "slice-height" c.sliceheight dc.sliceheight;
6503 oi "thumbnail-width" c.thumbw dc.thumbw;
6504 ob "persistent-location" c.jumpback dc.jumpback;
6505 oc "background-color" c.bgcolor dc.bgcolor;
6506 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6507 oi "tile-width" c.tilew dc.tilew;
6508 oi "tile-height" c.tileh dc.tileh;
6509 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6510 ob "checkers" c.checkers dc.checkers;
6511 oi "aalevel" c.aalevel dc.aalevel;
6512 ob "trim-margins" c.trimmargins dc.trimmargins;
6513 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6514 os "uri-launcher" c.urilauncher dc.urilauncher;
6515 os "path-launcher" c.pathlauncher dc.pathlauncher;
6516 oC "color-space" c.colorspace dc.colorspace;
6517 ob "invert-colors" c.invert dc.invert;
6518 oF "brightness" c.colorscale dc.colorscale;
6519 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6520 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6521 oco "columns" c.columns dc.columns;
6522 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6523 os "selection-command" c.selcmd dc.selcmd;
6524 ob "update-cursor" c.updatecurs dc.updatecurs;
6525 oi "hint-font-size" c.hfsize dc.hfsize;
6526 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6527 oF "page-scroll-scale" c.pgscale dc.pgscale;
6530 let keymapsbuf always dc c =
6531 let bb = Buffer.create 16 in
6532 let rec loop = function
6533 | [] -> ()
6534 | (modename, h) :: rest ->
6535 let dh = findkeyhash dc modename in
6536 if always || h <> dh
6537 then (
6538 if Hashtbl.length h > 0
6539 then (
6540 if Buffer.length bb > 0
6541 then Buffer.add_char bb '\n';
6542 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6543 Hashtbl.iter (fun i o ->
6544 let isdifferent = always ||
6546 let dO = Hashtbl.find dh i in
6547 dO <> o
6548 with Not_found -> true
6550 if isdifferent
6551 then
6552 let addkm (k, m) =
6553 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6554 if Wsi.withalt m then Buffer.add_string bb "alt-";
6555 if Wsi.withshift m then Buffer.add_string bb "shift-";
6556 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6557 Buffer.add_string bb (Wsi.keyname k);
6559 let addkms l =
6560 let rec loop = function
6561 | [] -> ()
6562 | km :: [] -> addkm km
6563 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6565 loop l
6567 Buffer.add_string bb "<map in='";
6568 addkm i;
6569 match o with
6570 | KMinsrt km ->
6571 Buffer.add_string bb "' out='";
6572 addkm km;
6573 Buffer.add_string bb "'/>\n"
6575 | KMinsrl kms ->
6576 Buffer.add_string bb "' out='";
6577 addkms kms;
6578 Buffer.add_string bb "'/>\n"
6580 | KMmulti (ins, kms) ->
6581 Buffer.add_char bb ' ';
6582 addkms ins;
6583 Buffer.add_string bb "' out='";
6584 addkms kms;
6585 Buffer.add_string bb "'/>\n"
6586 ) h;
6587 Buffer.add_string bb "</keymap>";
6590 loop rest
6592 loop c.keyhashes;
6596 let save () =
6597 let uifontsize = fstate.fontsize in
6598 let bb = Buffer.create 32768 in
6599 let f (h, dc) =
6600 let dc = if conf.bedefault then conf else dc in
6601 Buffer.add_string bb "<llppconfig>\n";
6603 if String.length !fontpath > 0
6604 then
6605 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6606 uifontsize
6607 !fontpath
6608 else (
6609 if uifontsize <> 14
6610 then
6611 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6614 Buffer.add_string bb "<defaults ";
6615 add_attrs bb true dc dc;
6616 let kb = keymapsbuf true dc dc in
6617 if Buffer.length kb > 0
6618 then (
6619 Buffer.add_string bb ">\n";
6620 Buffer.add_buffer bb kb;
6621 Buffer.add_string bb "\n</defaults>\n";
6623 else Buffer.add_string bb "/>\n";
6625 let adddoc path pan anchor c bookmarks =
6626 if bookmarks == [] && c = dc && anchor = emptyanchor
6627 then ()
6628 else (
6629 Printf.bprintf bb "<doc path='%s'"
6630 (enent path 0 (String.length path));
6632 if anchor <> emptyanchor
6633 then (
6634 let n, y = anchor in
6635 Printf.bprintf bb " page='%d'" n;
6636 if y > 1e-6
6637 then
6638 Printf.bprintf bb " rely='%f'" y
6642 if pan != 0
6643 then Printf.bprintf bb " pan='%d'" pan;
6645 add_attrs bb false dc c;
6646 let kb = keymapsbuf false dc c in
6648 begin match bookmarks with
6649 | [] ->
6650 if Buffer.length kb > 0
6651 then (
6652 Buffer.add_string bb ">\n";
6653 Buffer.add_buffer bb kb;
6654 Buffer.add_string bb "\n</doc>\n";
6656 else Buffer.add_string bb "/>\n"
6657 | _ ->
6658 Buffer.add_string bb ">\n<bookmarks>\n";
6659 List.iter (fun (title, _level, (page, rely)) ->
6660 Printf.bprintf bb
6661 "<item title='%s' page='%d'"
6662 (enent title 0 (String.length title))
6663 page
6665 if rely > 1e-6
6666 then
6667 Printf.bprintf bb " rely='%f'" rely
6669 Buffer.add_string bb "/>\n";
6670 ) bookmarks;
6671 Buffer.add_string bb "</bookmarks>";
6672 if Buffer.length kb > 0
6673 then (
6674 Buffer.add_string bb "\n";
6675 Buffer.add_buffer bb kb;
6677 Buffer.add_string bb "\n</doc>\n";
6678 end;
6682 let pan, conf =
6683 match state.mode with
6684 | Birdseye (c, pan, _, _, _) ->
6685 let beyecolumns =
6686 match conf.columns with
6687 | Cmulti ((c, _, _), _) -> Some c
6688 | Csingle _ -> None
6689 | Csplit _ -> None
6690 and columns =
6691 match c.columns with
6692 | Cmulti (c, _) -> Cmulti (c, [||])
6693 | Csingle _ -> Csingle [||]
6694 | Csplit _ -> failwith "quit from bird's eye while split"
6696 pan, { c with beyecolumns = beyecolumns; columns = columns }
6697 | _ -> state.x, conf
6699 let basename = Filename.basename state.path in
6700 adddoc basename pan (getanchor ())
6701 { conf with
6702 autoscrollstep =
6703 match state.autoscroll with
6704 | Some step -> step
6705 | None -> conf.autoscrollstep }
6706 (if conf.savebmarks then state.bookmarks else []);
6708 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6709 if basename <> path
6710 then adddoc path x y c bookmarks
6711 ) h;
6712 Buffer.add_string bb "</llppconfig>";
6714 load1 f;
6715 if Buffer.length bb > 0
6716 then
6718 let tmp = !confpath ^ ".tmp" in
6719 let oc = open_out_bin tmp in
6720 Buffer.output_buffer oc bb;
6721 close_out oc;
6722 Unix.rename tmp !confpath;
6723 with exn ->
6724 prerr_endline
6725 ("error while saving configuration: " ^ Printexc.to_string exn)
6727 end;;
6729 let () =
6730 let trimcachepath = ref "" in
6731 Arg.parse
6732 (Arg.align
6733 [("-p", Arg.String (fun s -> state.password <- s) ,
6734 "<password> Set password");
6736 ("-f", Arg.String (fun s -> Config.fontpath := s),
6737 "<path> Set path to the user interface font");
6739 ("-c", Arg.String (fun s -> Config.confpath := s),
6740 "<path> Set path to the configuration file");
6742 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6743 "<path> Set path to the trim cache file");
6745 ("-v", Arg.Unit (fun () ->
6746 Printf.printf
6747 "%s\nconfiguration path: %s\n"
6748 (version ())
6749 Config.defconfpath
6751 exit 0), " Print version and exit");
6754 (fun s -> state.path <- s)
6755 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6757 if String.length state.path = 0
6758 then (prerr_endline "file name missing"; exit 1);
6760 Config.load ();
6762 let globalkeyhash = findkeyhash conf "global" in
6763 let wsfd, winw, winh = Wsi.init (object
6764 method expose =
6765 if nogeomcmds state.geomcmds || platform == Posx
6766 then display ()
6767 else (
6768 GlClear.color (scalecolor2 conf.bgcolor);
6769 GlClear.clear [`color];
6771 method display = display ()
6772 method reshape w h = reshape w h
6773 method mouse b d x y m = mouse b d x y m
6774 method motion x y = state.mpos <- (x, y); motion x y
6775 method pmotion x y = state.mpos <- (x, y); pmotion x y
6776 method key k m =
6777 let mascm = m land (
6778 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6779 ) in
6780 match state.keystate with
6781 | KSnone ->
6782 let km = k, mascm in
6783 begin
6784 match
6785 let modehash = state.uioh#modehash in
6786 try Hashtbl.find modehash km
6787 with Not_found ->
6788 try Hashtbl.find globalkeyhash km
6789 with Not_found -> KMinsrt (k, m)
6790 with
6791 | KMinsrt (k, m) -> keyboard k m
6792 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6793 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6795 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6796 List.iter (fun (k, m) -> keyboard k m) insrt;
6797 state.keystate <- KSnone
6798 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6799 state.keystate <- KSinto (keys, insrt)
6800 | _ ->
6801 state.keystate <- KSnone
6803 method enter x y = state.mpos <- (x, y); pmotion x y
6804 method leave = state.mpos <- (-1, -1)
6805 method quit = raise Quit
6806 end) conf.winw conf.winh (platform = Posx) in
6808 state.wsfd <- wsfd;
6810 if not (
6811 List.exists GlMisc.check_extension
6812 [ "GL_ARB_texture_rectangle"
6813 ; "GL_EXT_texture_recangle"
6814 ; "GL_NV_texture_rectangle" ]
6816 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6818 let cr, sw =
6819 match Ne.pipe () with
6820 | Ne.Exn exn ->
6821 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6822 exit 1
6823 | Ne.Res rw -> rw
6824 and sr, cw =
6825 match Ne.pipe () with
6826 | Ne.Exn exn ->
6827 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6828 exit 1
6829 | Ne.Res rw -> rw
6832 cloexec cr;
6833 cloexec sw;
6834 cloexec sr;
6835 cloexec cw;
6837 setcheckers conf.checkers;
6838 redirectstderr ();
6840 init (cr, cw) (
6841 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6842 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6843 !Config.fontpath, !trimcachepath
6845 state.sr <- sr;
6846 state.sw <- sw;
6847 state.text <- "Opening " ^ state.path;
6848 reshape winw winh;
6849 opendoc state.path state.password;
6850 state.uioh <- uioh;
6852 let rec loop deadline =
6853 let r =
6854 match state.errfd with
6855 | None -> [state.sr; state.wsfd]
6856 | Some fd -> [state.sr; state.wsfd; fd]
6858 if state.redisplay
6859 then (
6860 state.redisplay <- false;
6861 display ();
6863 let timeout =
6864 let now = now () in
6865 if deadline > now
6866 then (
6867 if deadline = infinity
6868 then ~-.1.0
6869 else max 0.0 (deadline -. now)
6871 else 0.0
6873 let r, _, _ =
6874 try Unix.select r [] [] timeout
6875 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6877 begin match r with
6878 | [] ->
6879 state.ghyll None;
6880 let newdeadline =
6881 if state.ghyll == noghyll
6882 then
6883 match state.autoscroll with
6884 | Some step when step != 0 ->
6885 let y = state.y + step in
6886 let y =
6887 if y < 0
6888 then state.maxy
6889 else if y >= state.maxy then 0 else y
6891 gotoy y;
6892 if state.mode = View
6893 then state.text <- "";
6894 deadline +. 0.01
6895 | _ -> infinity
6896 else deadline +. 0.01
6898 loop newdeadline
6900 | l ->
6901 let rec checkfds = function
6902 | [] -> ()
6903 | fd :: rest when fd = state.sr ->
6904 let cmd = readcmd state.sr in
6905 act cmd;
6906 checkfds rest
6908 | fd :: rest when fd = state.wsfd ->
6909 Wsi.readresp fd;
6910 checkfds rest
6912 | fd :: rest ->
6913 let s = String.create 80 in
6914 let n = Unix.read fd s 0 80 in
6915 if conf.redirectstderr
6916 then (
6917 Buffer.add_substring state.errmsgs s 0 n;
6918 state.newerrmsgs <- true;
6919 state.redisplay <- true;
6921 else (
6922 prerr_string (String.sub s 0 n);
6923 flush stderr;
6925 checkfds rest
6927 checkfds l;
6928 let newdeadline =
6929 let deadline1 =
6930 if deadline = infinity
6931 then now () +. 0.01
6932 else deadline
6934 match state.autoscroll with
6935 | Some step when step != 0 -> deadline1
6936 | _ -> if state.ghyll == noghyll then infinity else deadline1
6938 loop newdeadline
6939 end;
6942 loop infinity;
6943 with Quit ->
6944 Config.save ();