Cosmetics
[llpp.git] / main.ml
blobda35c54f1a7174148162cabb62d89824b7ce0d4e
1 exception Quit;;
3 let tempfailureretry = Wsi.tempfailureretry;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 let dolog fmt = Printf.kprintf prerr_endline fmt;;
17 let now = Unix.gettimeofday;;
19 type params = (angle * proportional * trimparams
20 * texcount * sliceheight * memsize
21 * colorspace * fontpath * trimcachepath
22 * haspbo)
23 and pageno = int
24 and width = int
25 and height = int
26 and leftx = int
27 and opaque = string
28 and recttype = int
29 and pixmapsize = int
30 and angle = int
31 and proportional = bool
32 and trimmargins = bool
33 and interpagespace = int
34 and texcount = int
35 and sliceheight = int
36 and gen = int
37 and top = float
38 and dtop = float
39 and fontpath = string
40 and trimcachepath = string
41 and memsize = int
42 and aalevel = int
43 and irect = (int * int * int * int)
44 and trimparams = (trimmargins * irect)
45 and colorspace = | Rgb | Bgr | Gray
46 and haspbo = bool
49 type link =
50 | Lnotfound
51 | Lfound of int
52 and linkdir =
53 | LDfirst
54 | LDlast
55 | LDfirstvisible of (int * int * int)
56 | LDleft of int
57 | LDright of int
58 | LDdown of int
59 | LDup of int
62 type pagewithlinks =
63 | Pwlnotfound
64 | Pwl of int
67 type keymap =
68 | KMinsrt of key
69 | KMinsrl of key list
70 | KMmulti of key list * key list
71 and key = int * int
72 and keyhash = (key, keymap) Hashtbl.t
73 and keystate =
74 | KSnone
75 | KSinto of (key list * key list)
78 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
79 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
81 type pipe = (Unix.file_descr * Unix.file_descr);;
83 external init : pipe -> params -> unit = "ml_init";;
84 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
85 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
86 external getpdimrect : int -> float array = "ml_getpdimrect";;
87 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
88 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
89 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
90 external measurestr : int -> string -> float = "ml_measure_string";;
91 external getmaxw : unit -> float = "ml_getmaxw";;
92 external postprocess :
93 opaque -> int -> int -> int -> (int * string * int) -> int
94 = "ml_postprocess";;
95 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
96 external platform : unit -> platform = "ml_platform";;
97 external setaalevel : int -> unit = "ml_setaalevel";;
98 external realloctexts : int -> bool = "ml_realloctexts";;
99 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
100 external findlink : opaque -> linkdir -> link = "ml_findlink";;
101 external getlink : opaque -> int -> under = "ml_getlink";;
102 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
103 external getlinkcount : opaque -> int = "ml_getlinkcount";;
104 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
105 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
106 external mbtoutf8 : string -> string = "ml_mbtoutf8";;
107 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
108 external freepbo : string -> unit = "ml_freepbo";;
109 external unmappbo : string -> unit = "ml_unmappbo";;
110 external pbousable : unit -> bool = "ml_pbo_usable";;
112 let platform_to_string = function
113 | Punknown -> "unknown"
114 | Plinux -> "Linux"
115 | Posx -> "OSX"
116 | Psun -> "Sun"
117 | Pfreebsd -> "FreeBSD"
118 | Pdragonflybsd -> "DragonflyBSD"
119 | Popenbsd -> "OpenBSD"
120 | Pnetbsd -> "NetBSD"
121 | Pcygwin -> "Cygwin"
124 let platform = platform ();;
126 let popen cmd fda =
127 if platform = Pcygwin
128 then (
129 let sh = "/bin/sh" in
130 let args = [|sh; "-c"; cmd|] in
131 let rec std si so se = function
132 | [] -> si, so, se
133 | (fd, 0) :: rest -> std fd so se rest
134 | (fd, -1) :: rest ->
135 Unix.set_close_on_exec fd;
136 std si so se rest
137 | (_, n) :: _ ->
138 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
140 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
141 ignore (Unix.create_process sh args si so se)
143 else popen cmd fda;
146 type x = int
147 and y = int
148 and tilex = int
149 and tiley = int
150 and tileparams = (x * y * width * height * tilex * tiley)
153 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
155 type mpos = int * int
156 and mstate =
157 | Msel of (mpos * mpos)
158 | Mpan of mpos
159 | Mscrolly | Mscrollx
160 | Mzoom of (int * int)
161 | Mzoomrect of (mpos * mpos)
162 | Mnone
165 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
166 and onkey = string -> int -> te
167 and ondone = string -> unit
168 and histcancel = unit -> unit
169 and onhist = ((histcmd -> string) * histcancel)
170 and histcmd = HCnext | HCprev | HCfirst | HClast
171 and cancelonempty = bool
172 and te =
173 | TEstop
174 | TEdone of string
175 | TEcont of string
176 | TEswitch of textentry
179 type 'a circbuf =
180 { store : 'a array
181 ; mutable rc : int
182 ; mutable wc : int
183 ; mutable len : int
187 let bound v minv maxv =
188 max minv (min maxv v);
191 let cbnew n v =
192 { store = Array.create n v
193 ; rc = 0
194 ; wc = 0
195 ; len = 0
199 let cbcap b = Array.length b.store;;
201 let cbput b v =
202 let cap = cbcap b in
203 b.store.(b.wc) <- v;
204 b.wc <- (b.wc + 1) mod cap;
205 b.rc <- b.wc;
206 b.len <- min (b.len + 1) cap;
209 let cbempty b = b.len = 0;;
211 let cbgetg b circular dir =
212 if cbempty b
213 then b.store.(0)
214 else
215 let rc = b.rc + dir in
216 let rc =
217 if circular
218 then (
219 if rc = -1
220 then b.len-1
221 else (
222 if rc >= b.len
223 then 0
224 else rc
227 else bound rc 0 (b.len-1)
229 b.rc <- rc;
230 b.store.(rc);
233 let cbget b = cbgetg b false;;
234 let cbgetc b = cbgetg b true;;
236 let drawstring size x y s =
237 Gl.enable `blend;
238 Gl.enable `texture_2d;
239 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
240 ignore (drawstr size x y s);
241 Gl.disable `blend;
242 Gl.disable `texture_2d;
245 let drawstring1 size x y s =
246 drawstr size x y s;
249 let drawstring2 size x y fmt =
250 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
253 type page =
254 { pageno : int
255 ; pagedimno : int
256 ; pagew : int
257 ; pageh : int
258 ; pagex : int
259 ; pagey : int
260 ; pagevw : int
261 ; pagevh : int
262 ; pagedispx : int
263 ; pagedispy : int
264 ; pagecol : int
268 let debugl l =
269 dolog "l %d dim=%d {" l.pageno l.pagedimno;
270 dolog " WxH %dx%d" l.pagew l.pageh;
271 dolog " vWxH %dx%d" l.pagevw l.pagevh;
272 dolog " pagex,y %d,%d" l.pagex l.pagey;
273 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
274 dolog " column %d" l.pagecol;
275 dolog "}";
278 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
279 dolog "rect {";
280 dolog " x0,y0=(% f, % f)" x0 y0;
281 dolog " x1,y1=(% f, % f)" x1 y1;
282 dolog " x2,y2=(% f, % f)" x2 y2;
283 dolog " x3,y3=(% f, % f)" x3 y3;
284 dolog "}";
287 type multicolumns = multicol * pagegeom
288 and singlecolumn = pagegeom
289 and splitcolumns = columncount * pagegeom
290 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
291 and multicol = columncount * covercount * covercount
292 and pdimno = int
293 and columncount = int
294 and covercount = int;;
296 type conf =
297 { mutable scrollbw : int
298 ; mutable scrollh : int
299 ; mutable icase : bool
300 ; mutable preload : bool
301 ; mutable pagebias : int
302 ; mutable verbose : bool
303 ; mutable debug : bool
304 ; mutable scrollstep : int
305 ; mutable hscrollstep : int
306 ; mutable maxhfit : bool
307 ; mutable crophack : bool
308 ; mutable autoscrollstep : int
309 ; mutable maxwait : float option
310 ; mutable hlinks : bool
311 ; mutable underinfo : bool
312 ; mutable interpagespace : interpagespace
313 ; mutable zoom : float
314 ; mutable presentation : bool
315 ; mutable angle : angle
316 ; mutable winw : int
317 ; mutable winh : int
318 ; mutable savebmarks : bool
319 ; mutable proportional : proportional
320 ; mutable trimmargins : trimmargins
321 ; mutable trimfuzz : irect
322 ; mutable memlimit : memsize
323 ; mutable texcount : texcount
324 ; mutable sliceheight : sliceheight
325 ; mutable thumbw : width
326 ; mutable jumpback : bool
327 ; mutable bgcolor : float * float * float
328 ; mutable bedefault : bool
329 ; mutable scrollbarinpm : bool
330 ; mutable tilew : int
331 ; mutable tileh : int
332 ; mutable mustoresize : memsize
333 ; mutable checkers : bool
334 ; mutable aalevel : int
335 ; mutable urilauncher : string
336 ; mutable pathlauncher : string
337 ; mutable colorspace : colorspace
338 ; mutable invert : bool
339 ; mutable colorscale : float
340 ; mutable redirectstderr : bool
341 ; mutable ghyllscroll : (int * int * int) option
342 ; mutable columns : columns
343 ; mutable beyecolumns : columncount option
344 ; mutable selcmd : string
345 ; mutable updatecurs : bool
346 ; mutable keyhashes : (string * keyhash) list
347 ; mutable hfsize : int
348 ; mutable pgscale : float
349 ; mutable usepbo : bool
350 ; mutable wheelbypage : bool
352 and columns =
353 | Csingle of singlecolumn
354 | Cmulti of multicolumns
355 | Csplit of splitcolumns
358 type anchor = pageno * top * dtop;;
360 type outline = string * int * anchor;;
362 type rect = float * float * float * float * float * float * float * float;;
364 type tile = opaque * pixmapsize * elapsed
365 and elapsed = float;;
366 type pagemapkey = pageno * gen;;
367 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
368 and row = int
369 and col = int;;
371 let emptyanchor = (0, 0.0, 0.0);;
373 type infochange = | Memused | Docinfo | Pdim;;
375 class type uioh = object
376 method display : unit
377 method key : int -> int -> uioh
378 method button : int -> bool -> int -> int -> int -> uioh
379 method motion : int -> int -> uioh
380 method pmotion : int -> int -> uioh
381 method infochanged : infochange -> unit
382 method scrollpw : (int * float * float)
383 method scrollph : (int * float * float)
384 method modehash : keyhash
385 end;;
387 type mode =
388 | Birdseye of (conf * leftx * pageno * pageno * anchor)
389 | Textentry of (textentry * onleave)
390 | View
391 | LinkNav of linktarget
392 and onleave = leavetextentrystatus -> unit
393 and leavetextentrystatus = | Cancel | Confirm
394 and helpitem = string * int * action
395 and action =
396 | Noaction
397 | Action of (uioh -> uioh)
398 and linktarget =
399 | Ltexact of (pageno * int)
400 | Ltgendir of int
403 let isbirdseye = function Birdseye _ -> true | _ -> false;;
404 let istextentry = function Textentry _ -> true | _ -> false;;
406 type currently =
407 | Idle
408 | Loading of (page * gen)
409 | Tiling of (
410 page * opaque * colorspace * angle * gen * col * row * width * height
412 | Outlining of outline list
415 let emptykeyhash = Hashtbl.create 0;;
416 let nouioh : uioh = object (self)
417 method display = ()
418 method key _ _ = self
419 method button _ _ _ _ _ = self
420 method motion _ _ = self
421 method pmotion _ _ = self
422 method infochanged _ = ()
423 method scrollpw = (0, nan, nan)
424 method scrollph = (0, nan, nan)
425 method modehash = emptykeyhash
426 end;;
428 type state =
429 { mutable sr : Unix.file_descr
430 ; mutable sw : Unix.file_descr
431 ; mutable wsfd : Unix.file_descr
432 ; mutable errfd : Unix.file_descr option
433 ; mutable stderr : Unix.file_descr
434 ; mutable errmsgs : Buffer.t
435 ; mutable newerrmsgs : bool
436 ; mutable w : int
437 ; mutable x : int
438 ; mutable y : int
439 ; mutable scrollw : int
440 ; mutable hscrollh : int
441 ; mutable anchor : anchor
442 ; mutable ranchors : (string * string * anchor) list
443 ; mutable maxy : int
444 ; mutable layout : page list
445 ; pagemap : (pagemapkey, opaque) Hashtbl.t
446 ; tilemap : (tilemapkey, tile) Hashtbl.t
447 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
448 ; mutable pdims : (pageno * width * height * leftx) list
449 ; mutable pagecount : int
450 ; mutable currently : currently
451 ; mutable mstate : mstate
452 ; mutable searchpattern : string
453 ; mutable rects : (pageno * recttype * rect) list
454 ; mutable rects1 : (pageno * recttype * rect) list
455 ; mutable text : string
456 ; mutable fullscreen : (width * height) option
457 ; mutable mode : mode
458 ; mutable uioh : uioh
459 ; mutable outlines : outline array
460 ; mutable bookmarks : outline list
461 ; mutable path : string
462 ; mutable password : string
463 ; mutable nameddest : string
464 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
465 ; mutable memused : memsize
466 ; mutable gen : gen
467 ; mutable throttle : (page list * int * float) option
468 ; mutable autoscroll : int option
469 ; mutable ghyll : (int option -> unit)
470 ; mutable help : helpitem array
471 ; mutable docinfo : (int * string) list
472 ; mutable texid : GlTex.texture_id option
473 ; hists : hists
474 ; mutable prevzoom : float
475 ; mutable progress : float
476 ; mutable redisplay : bool
477 ; mutable mpos : mpos
478 ; mutable keystate : keystate
479 ; mutable glinks : bool
480 ; mutable prevcolumns : (columns * float) option
481 ; mutable wthack : bool
483 and hists =
484 { pat : string circbuf
485 ; pag : string circbuf
486 ; nav : anchor circbuf
487 ; sel : string circbuf
491 let defconf =
492 { scrollbw = 7
493 ; scrollh = 12
494 ; icase = true
495 ; preload = true
496 ; pagebias = 0
497 ; verbose = false
498 ; debug = false
499 ; scrollstep = 24
500 ; hscrollstep = 24
501 ; maxhfit = true
502 ; crophack = false
503 ; autoscrollstep = 2
504 ; maxwait = None
505 ; hlinks = false
506 ; underinfo = false
507 ; interpagespace = 2
508 ; zoom = 1.0
509 ; presentation = false
510 ; angle = 0
511 ; winw = 900
512 ; winh = 900
513 ; savebmarks = true
514 ; proportional = true
515 ; trimmargins = false
516 ; trimfuzz = (0,0,0,0)
517 ; memlimit = 32 lsl 20
518 ; texcount = 256
519 ; sliceheight = 24
520 ; thumbw = 76
521 ; jumpback = true
522 ; bgcolor = (0.5, 0.5, 0.5)
523 ; bedefault = false
524 ; scrollbarinpm = true
525 ; tilew = 2048
526 ; tileh = 2048
527 ; mustoresize = 256 lsl 20
528 ; checkers = true
529 ; aalevel = 8
530 ; urilauncher =
531 (match platform with
532 | Plinux | Pfreebsd | Pdragonflybsd
533 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
534 | Posx -> "open \"%s\""
535 | Pcygwin -> "cygstart \"%s\""
536 | Punknown -> "echo %s")
537 ; pathlauncher = "lp \"%s\""
538 ; selcmd =
539 (match platform with
540 | Plinux | Pfreebsd | Pdragonflybsd
541 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
542 | Posx -> "pbcopy"
543 | Pcygwin -> "wsel"
544 | Punknown -> "cat")
545 ; colorspace = Rgb
546 ; invert = false
547 ; colorscale = 1.0
548 ; redirectstderr = false
549 ; ghyllscroll = None
550 ; columns = Csingle [||]
551 ; beyecolumns = None
552 ; updatecurs = false
553 ; hfsize = 12
554 ; pgscale = 1.0
555 ; usepbo = false
556 ; wheelbypage = false
557 ; keyhashes =
558 let mk n = (n, Hashtbl.create 1) in
559 [ mk "global"
560 ; mk "info"
561 ; mk "help"
562 ; mk "outline"
563 ; mk "listview"
564 ; mk "birdseye"
565 ; mk "textentry"
566 ; mk "links"
567 ; mk "view"
572 let wtmode = ref false;;
574 let findkeyhash c name =
575 try List.assoc name c.keyhashes
576 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
579 let conf = { defconf with angle = defconf.angle };;
581 let pgscale h = truncate (float h *. conf.pgscale);;
583 type fontstate =
584 { mutable fontsize : int
585 ; mutable wwidth : float
586 ; mutable maxrows : int
590 let fstate =
591 { fontsize = 14
592 ; wwidth = nan
593 ; maxrows = -1
597 let setfontsize n =
598 fstate.fontsize <- n;
599 fstate.wwidth <- measurestr fstate.fontsize "w";
600 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
603 let geturl s =
604 let colonpos = try String.index s ':' with Not_found -> -1 in
605 let len = String.length s in
606 if colonpos >= 0 && colonpos + 3 < len
607 then (
608 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
609 then
610 let schemestartpos =
611 try String.rindex_from s colonpos ' '
612 with Not_found -> -1
614 let scheme =
615 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
617 match scheme with
618 | "http" | "ftp" | "mailto" ->
619 let epos =
620 try String.index_from s colonpos ' '
621 with Not_found -> len
623 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
624 | _ -> ""
625 else ""
627 else ""
630 let gotouri uri =
631 if String.length conf.urilauncher = 0
632 then print_endline uri
633 else (
634 let url = geturl uri in
635 if String.length url = 0
636 then print_endline uri
637 else
638 let re = Str.regexp "%s" in
639 let command = Str.global_replace re url conf.urilauncher in
640 try popen command []
641 with exn ->
642 Printf.eprintf
643 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
644 flush stderr;
648 let version () =
649 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
650 (platform_to_string platform) Sys.word_size Sys.ocaml_version
653 let makehelp () =
654 let strings = version () :: "" :: Help.keys in
655 Array.of_list (
656 List.map (fun s ->
657 let url = geturl s in
658 if String.length url > 0
659 then (s, 0, Action (fun u -> gotouri url; u))
660 else (s, 0, Noaction)
661 ) strings);
664 let noghyll _ = ();;
665 let firstgeomcmds = "", [];;
667 let state =
668 { sr = Unix.stdin
669 ; sw = Unix.stdin
670 ; wsfd = Unix.stdin
671 ; errfd = None
672 ; stderr = Unix.stderr
673 ; errmsgs = Buffer.create 0
674 ; newerrmsgs = false
675 ; x = 0
676 ; y = 0
677 ; w = 0
678 ; scrollw = 0
679 ; hscrollh = 0
680 ; anchor = emptyanchor
681 ; ranchors = []
682 ; layout = []
683 ; maxy = max_int
684 ; tilelru = Queue.create ()
685 ; pagemap = Hashtbl.create 10
686 ; tilemap = Hashtbl.create 10
687 ; pdims = []
688 ; pagecount = 0
689 ; currently = Idle
690 ; mstate = Mnone
691 ; rects = []
692 ; rects1 = []
693 ; text = ""
694 ; mode = View
695 ; fullscreen = None
696 ; searchpattern = ""
697 ; outlines = [||]
698 ; bookmarks = []
699 ; path = ""
700 ; password = ""
701 ; nameddest = ""
702 ; geomcmds = firstgeomcmds
703 ; hists =
704 { nav = cbnew 10 emptyanchor
705 ; pat = cbnew 10 ""
706 ; pag = cbnew 10 ""
707 ; sel = cbnew 10 ""
709 ; memused = 0
710 ; gen = 0
711 ; throttle = None
712 ; autoscroll = None
713 ; ghyll = noghyll
714 ; help = makehelp ()
715 ; docinfo = []
716 ; texid = None
717 ; prevzoom = 1.0
718 ; progress = -1.0
719 ; uioh = nouioh
720 ; redisplay = true
721 ; mpos = (-1, -1)
722 ; keystate = KSnone
723 ; glinks = false
724 ; prevcolumns = None
725 ; wthack = false
729 let vlog fmt =
730 if conf.verbose
731 then
732 Printf.kprintf prerr_endline fmt
733 else
734 Printf.kprintf ignore fmt
737 let launchpath () =
738 if String.length conf.pathlauncher = 0
739 then print_endline state.path
740 else (
741 let re = Str.regexp "%s" in
742 let command = Str.global_replace re state.path conf.pathlauncher in
743 try popen command []
744 with exn ->
745 Printf.eprintf
746 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
747 flush stderr;
751 module Ne = struct
752 type 'a t = | Res of 'a | Exn of exn;;
754 let pipe () =
755 try Res (Unix.pipe ())
756 with exn -> Exn exn
759 let clo fd f =
760 try tempfailureretry Unix.close fd
761 with exn -> f (Printexc.to_string exn)
764 let dup fd =
765 try Res (tempfailureretry Unix.dup fd)
766 with exn -> Exn exn
769 let dup2 fd1 fd2 =
770 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
771 with exn -> Exn exn
773 end;;
775 let redirectstderr () =
776 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
777 if conf.redirectstderr
778 then
779 match Ne.pipe () with
780 | Ne.Exn exn ->
781 dolog "failed to create stderr redirection pipes: %s"
782 (Printexc.to_string exn)
784 | Ne.Res (r, w) ->
785 begin match Ne.dup Unix.stderr with
786 | Ne.Exn exn ->
787 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
788 Ne.clo r (clofail "pipe/r");
789 Ne.clo w (clofail "pipe/w");
791 | Ne.Res dupstderr ->
792 begin match Ne.dup2 w Unix.stderr with
793 | Ne.Exn exn ->
794 dolog "failed to dup2 to stderr: %s"
795 (Printexc.to_string exn);
796 Ne.clo dupstderr (clofail "stderr duplicate");
797 Ne.clo r (clofail "redir pipe/r");
798 Ne.clo w (clofail "redir pipe/w");
800 | Ne.Res () ->
801 state.stderr <- dupstderr;
802 state.errfd <- Some r;
803 end;
805 else (
806 state.newerrmsgs <- false;
807 begin match state.errfd with
808 | Some fd ->
809 begin match Ne.dup2 state.stderr Unix.stderr with
810 | Ne.Exn exn ->
811 dolog "failed to dup2 original stderr: %s"
812 (Printexc.to_string exn)
813 | Ne.Res () ->
814 Ne.clo fd (clofail "dup of stderr");
815 state.errfd <- None;
816 end;
817 | None -> ()
818 end;
819 prerr_string (Buffer.contents state.errmsgs);
820 flush stderr;
821 Buffer.clear state.errmsgs;
825 module G =
826 struct
827 let postRedisplay who =
828 if conf.verbose
829 then prerr_endline ("redisplay for " ^ who);
830 state.redisplay <- true;
832 end;;
834 let getopaque pageno =
835 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
836 with Not_found -> None
839 let putopaque pageno opaque =
840 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
843 let pagetranslatepoint l x y =
844 let dy = y - l.pagedispy in
845 let y = dy + l.pagey in
846 let dx = x - l.pagedispx in
847 let x = dx + l.pagex in
848 (x, y);
851 let getunder x y =
852 let rec f = function
853 | l :: rest ->
854 begin match getopaque l.pageno with
855 | Some opaque ->
856 let x0 = l.pagedispx in
857 let x1 = x0 + l.pagevw in
858 let y0 = l.pagedispy in
859 let y1 = y0 + l.pagevh in
860 if y >= y0 && y <= y1 && x >= x0 && x <= x1
861 then
862 let px, py = pagetranslatepoint l x y in
863 match whatsunder opaque px py with
864 | Unone -> f rest
865 | under -> under
866 else f rest
867 | _ ->
868 f rest
870 | [] -> Unone
872 f state.layout
875 let showtext c s =
876 state.text <- Printf.sprintf "%c%s" c s;
877 G.postRedisplay "showtext";
880 let undertext = function
881 | Unone -> "none"
882 | Ulinkuri s -> s
883 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
884 | Utext s -> "font: " ^ s
885 | Uunexpected s -> "unexpected: " ^ s
886 | Ulaunch s -> "launch: " ^ s
887 | Unamed s -> "named: " ^ s
888 | Uremote (filename, pageno) ->
889 Printf.sprintf "%s: page %d" filename (pageno+1)
892 let updateunder x y =
893 match getunder x y with
894 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
895 | Ulinkuri uri ->
896 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
897 Wsi.setcursor Wsi.CURSOR_INFO
898 | Ulinkgoto (pageno, _) ->
899 if conf.underinfo
900 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
901 Wsi.setcursor Wsi.CURSOR_INFO
902 | Utext s ->
903 if conf.underinfo then showtext 'f' ("ont: " ^ s);
904 Wsi.setcursor Wsi.CURSOR_TEXT
905 | Uunexpected s ->
906 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
907 Wsi.setcursor Wsi.CURSOR_INHERIT
908 | Ulaunch s ->
909 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
910 Wsi.setcursor Wsi.CURSOR_INHERIT
911 | Unamed s ->
912 if conf.underinfo then showtext 'n' ("amed: " ^ s);
913 Wsi.setcursor Wsi.CURSOR_INHERIT
914 | Uremote (filename, pageno) ->
915 if conf.underinfo then showtext 'r'
916 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
917 Wsi.setcursor Wsi.CURSOR_INFO
920 let showlinktype under =
921 if conf.underinfo
922 then
923 match under with
924 | Unone -> ()
925 | under ->
926 let s = undertext under in
927 showtext ' ' s
930 let addchar s c =
931 let b = Buffer.create (String.length s + 1) in
932 Buffer.add_string b s;
933 Buffer.add_char b c;
934 Buffer.contents b;
937 let colorspace_of_string s =
938 match String.lowercase s with
939 | "rgb" -> Rgb
940 | "bgr" -> Bgr
941 | "gray" -> Gray
942 | _ -> failwith "invalid colorspace"
945 let int_of_colorspace = function
946 | Rgb -> 0
947 | Bgr -> 1
948 | Gray -> 2
951 let colorspace_of_int = function
952 | 0 -> Rgb
953 | 1 -> Bgr
954 | 2 -> Gray
955 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
958 let colorspace_to_string = function
959 | Rgb -> "rgb"
960 | Bgr -> "bgr"
961 | Gray -> "gray"
964 let intentry_with_suffix text key =
965 let c =
966 if key >= 32 && key < 127
967 then Char.chr key
968 else '\000'
970 match Char.lowercase c with
971 | '0' .. '9' ->
972 let text = addchar text c in
973 TEcont text
975 | 'k' | 'm' | 'g' ->
976 let text = addchar text c in
977 TEcont text
979 | _ ->
980 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
981 TEcont text
984 let multicolumns_to_string (n, a, b) =
985 if a = 0 && b = 0
986 then Printf.sprintf "%d" n
987 else Printf.sprintf "%d,%d,%d" n a b;
990 let multicolumns_of_string s =
992 (int_of_string s, 0, 0)
993 with _ ->
994 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
995 if a > 1 || b > 1
996 then failwith "subtly broken"; (n, a, b)
1000 let readcmd fd =
1001 let s = "xxxx" in
1002 let n = tempfailureretry (Unix.read fd s 0) 4 in
1003 if n != 4 then failwith "incomplete read(len)";
1004 let len = 0
1005 lor (Char.code s.[0] lsl 24)
1006 lor (Char.code s.[1] lsl 16)
1007 lor (Char.code s.[2] lsl 8)
1008 lor (Char.code s.[3] lsl 0)
1010 let s = String.create len in
1011 let n = tempfailureretry (Unix.read fd s 0) len in
1012 if n != len then failwith "incomplete read(data)";
1016 let btod b = if b then 1 else 0;;
1018 let wcmd fmt =
1019 let b = Buffer.create 16 in
1020 Buffer.add_string b "llll";
1021 Printf.kbprintf
1022 (fun b ->
1023 let s = Buffer.contents b in
1024 let n = String.length s in
1025 let len = n - 4 in
1026 (* dolog "wcmd %S" (String.sub s 4 len); *)
1027 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1028 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1029 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1030 s.[3] <- Char.chr (len land 0xff);
1031 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1032 if n' != n then failwith "write failed";
1033 ) b fmt;
1036 let calcips h =
1037 let d = conf.winh - h in
1038 max conf.interpagespace ((d + 1) / 2)
1041 let rowyh (c, coverA, coverB) b n =
1042 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1043 then
1044 let _, _, vy, (_, _, h, _) = b.(n) in
1045 (vy, h)
1046 else
1047 let n' = n - coverA in
1048 let d = n' mod c in
1049 let s = n - d in
1050 let e = min state.pagecount (s + c) in
1051 let rec find m miny maxh = if m = e then miny, maxh else
1052 let _, _, y, (_, _, h, _) = b.(m) in
1053 let miny = min miny y in
1054 let maxh = max maxh h in
1055 find (m+1) miny maxh
1056 in find s max_int 0
1059 let calcheight () =
1060 match conf.columns with
1061 | Cmulti ((_, _, _) as cl, b) ->
1062 if Array.length b > 0
1063 then
1064 let y, h = rowyh cl b (Array.length b - 1) in
1065 y + h + (if conf.presentation then calcips h else 0)
1066 else 0
1067 | Csingle b ->
1068 if Array.length b > 0
1069 then
1070 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1071 y + h + (if conf.presentation then calcips h else 0)
1072 else 0
1073 | Csplit (_, b) ->
1074 if Array.length b > 0
1075 then
1076 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1077 y + h
1078 else 0
1081 let getpageyh pageno =
1082 let pageno = bound pageno 0 (state.pagecount-1) in
1083 match conf.columns with
1084 | Csingle b ->
1085 if Array.length b = 0
1086 then 0, 0
1087 else
1088 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1089 let y =
1090 if conf.presentation
1091 then y - calcips h
1092 else y
1094 y, h
1095 | Cmulti (cl, b) ->
1096 if Array.length b = 0
1097 then 0, 0
1098 else
1099 let y, h = rowyh cl b pageno in
1100 let y =
1101 if conf.presentation
1102 then y - calcips h
1103 else y
1105 y, h
1106 | Csplit (c, b) ->
1107 if Array.length b = 0
1108 then 0, 0
1109 else
1110 let n = pageno*c in
1111 let (_, _, y, (_, _, h, _)) = b.(n) in
1112 y, h
1115 let getpagedim pageno =
1116 let rec f ppdim l =
1117 match l with
1118 | (n, _, _, _) as pdim :: rest ->
1119 if n >= pageno
1120 then (if n = pageno then pdim else ppdim)
1121 else f pdim rest
1123 | [] -> ppdim
1125 f (-1, -1, -1, -1) state.pdims
1128 let getpagey pageno = fst (getpageyh pageno);;
1130 let nogeomcmds cmds =
1131 match cmds with
1132 | s, [] -> String.length s = 0
1133 | _ -> false
1136 let page_of_y y =
1137 let ((c, coverA, coverB) as cl), b =
1138 match conf.columns with
1139 | Csingle b -> (1, 0, 0), b
1140 | Cmulti (c, b) -> c, b
1141 | Csplit (_, b) -> (1, 0, 0), b
1143 let rec bsearch nmin nmax =
1144 if nmin > nmax
1145 then bound nmin 0 (state.pagecount-1)
1146 else
1147 let n = (nmax + nmin) / 2 in
1148 let vy, h = rowyh cl b n in
1149 let y0, y1 =
1150 if conf.presentation
1151 then
1152 let ips = calcips h in
1153 let y0 = vy - ips in
1154 let y1 = vy + h + ips in
1155 y0, y1
1156 else (
1157 if n = 0
1158 then 0, vy + h + conf.interpagespace
1159 else
1160 let y0 = vy - conf.interpagespace in
1161 y0, y0 + h + conf.interpagespace
1164 if y >= y0 && y < y1
1165 then (
1166 if c = 1
1167 then n
1168 else (
1169 if n > coverA
1170 then
1171 if n < state.pagecount - coverB
1172 then ((n-coverA)/c)*c + coverA
1173 else n
1174 else n
1177 else (
1178 if y > y0
1179 then bsearch (n+1) nmax
1180 else bsearch nmin (n-1)
1183 let r = bsearch 0 (state.pagecount-1) in
1187 let layoutN ((columns, coverA, coverB), b) y sh =
1188 let sh = sh - state.hscrollh in
1189 let rec fold accu n =
1190 if n = Array.length b
1191 then accu
1192 else
1193 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1194 if (vy - y) > sh &&
1195 (n = coverA - 1
1196 || n = state.pagecount - coverB
1197 || (n - coverA) mod columns = columns - 1)
1198 then accu
1199 else
1200 let accu =
1201 if vy + h > y
1202 then
1203 let pagey = max 0 (y - vy) in
1204 let pagedispy = if pagey > 0 then 0 else vy - y in
1205 let pagedispx, pagex =
1206 let pdx =
1207 if n = coverA - 1 || n = state.pagecount - coverB
1208 then state.x + (conf.winw - state.scrollw - w) / 2
1209 else dx + xoff + state.x
1211 if pdx < 0
1212 then 0, -pdx
1213 else pdx, 0
1215 let pagevw =
1216 let vw = conf.winw - state.scrollw - pagedispx in
1217 let pw = w - pagex in
1218 min vw pw
1220 let pagevh = min (h - pagey) (sh - pagedispy) in
1221 if pagevw > 0 && pagevh > 0
1222 then
1223 let e =
1224 { pageno = n
1225 ; pagedimno = pdimno
1226 ; pagew = w
1227 ; pageh = h
1228 ; pagex = pagex
1229 ; pagey = pagey
1230 ; pagevw = pagevw
1231 ; pagevh = pagevh
1232 ; pagedispx = pagedispx
1233 ; pagedispy = pagedispy
1234 ; pagecol = 0
1237 e :: accu
1238 else
1239 accu
1240 else
1241 accu
1243 fold accu (n+1)
1245 List.rev (fold [] (page_of_y y));
1248 let layoutS (columns, b) y sh =
1249 let sh = sh - state.hscrollh in
1250 let rec fold accu n =
1251 if n = Array.length b
1252 then accu
1253 else
1254 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1255 if (vy - y) > sh
1256 then accu
1257 else
1258 let accu =
1259 if vy + pageh > y
1260 then
1261 let x = xoff + state.x in
1262 let pagey = max 0 (y - vy) in
1263 let pagedispy = if pagey > 0 then 0 else vy - y in
1264 let pagedispx, pagex =
1265 if px = 0
1266 then (
1267 if x < 0
1268 then 0, -x
1269 else x, 0
1271 else (
1272 let px = px - x in
1273 if px < 0
1274 then -px, 0
1275 else 0, px
1278 let pagecolw = pagew/columns in
1279 let pagedispx =
1280 if pagecolw < conf.winw
1281 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1282 else pagedispx
1284 let pagevw =
1285 let vw = conf.winw - pagedispx - state.scrollw in
1286 let pw = pagew - pagex in
1287 min vw pw
1289 let pagevw = min pagevw pagecolw in
1290 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1291 if pagevw > 0 && pagevh > 0
1292 then
1293 let e =
1294 { pageno = n/columns
1295 ; pagedimno = pdimno
1296 ; pagew = pagew
1297 ; pageh = pageh
1298 ; pagex = pagex
1299 ; pagey = pagey
1300 ; pagevw = pagevw
1301 ; pagevh = pagevh
1302 ; pagedispx = pagedispx
1303 ; pagedispy = pagedispy
1304 ; pagecol = n mod columns
1307 e :: accu
1308 else
1309 accu
1310 else
1311 accu
1313 fold accu (n+1)
1315 List.rev (fold [] 0)
1318 let layout y sh =
1319 if nogeomcmds state.geomcmds
1320 then
1321 match conf.columns with
1322 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1323 | Cmulti c -> layoutN c y sh
1324 | Csplit s -> layoutS s y sh
1325 else []
1328 let clamp incr =
1329 let y = state.y + incr in
1330 let y = max 0 y in
1331 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1335 let itertiles l f =
1336 let tilex = l.pagex mod conf.tilew in
1337 let tiley = l.pagey mod conf.tileh in
1339 let col = l.pagex / conf.tilew in
1340 let row = l.pagey / conf.tileh in
1342 let rec rowloop row y0 dispy h =
1343 if h = 0
1344 then ()
1345 else (
1346 let dh = conf.tileh - y0 in
1347 let dh = min h dh in
1348 let rec colloop col x0 dispx w =
1349 if w = 0
1350 then ()
1351 else (
1352 let dw = conf.tilew - x0 in
1353 let dw = min w dw in
1355 f col row dispx dispy x0 y0 dw dh;
1356 colloop (col+1) 0 (dispx+dw) (w-dw)
1359 colloop col tilex l.pagedispx l.pagevw;
1360 rowloop (row+1) 0 (dispy+dh) (h-dh)
1363 if l.pagevw > 0 && l.pagevh > 0
1364 then rowloop row tiley l.pagedispy l.pagevh;
1367 let gettileopaque l col row =
1368 let key =
1369 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1371 try Some (Hashtbl.find state.tilemap key)
1372 with Not_found -> None
1375 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1376 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1377 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1380 let drawtiles l color =
1381 GlDraw.color color;
1382 let f col row x y tilex tiley w h =
1383 match gettileopaque l col row with
1384 | Some (opaque, _, t) ->
1385 let params = x, y, w, h, tilex, tiley in
1386 if conf.invert
1387 then (
1388 Gl.enable `blend;
1389 GlFunc.blend_func `zero `one_minus_src_color;
1391 drawtile params opaque;
1392 if conf.invert
1393 then Gl.disable `blend;
1394 if conf.debug
1395 then (
1396 let s = Printf.sprintf
1397 "%d[%d,%d] %f sec"
1398 l.pageno col row t
1400 let w = measurestr fstate.fontsize s in
1401 GlMisc.push_attrib [`current];
1402 GlDraw.color (0.0, 0.0, 0.0);
1403 GlDraw.rect
1404 (float (x-2), float (y-2))
1405 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1406 GlDraw.color (1.0, 1.0, 1.0);
1407 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1408 GlMisc.pop_attrib ();
1411 | _ ->
1412 let w =
1413 let lw = conf.winw - state.scrollw - x in
1414 min lw w
1415 and h =
1416 let lh = conf.winh - y in
1417 min lh h
1419 begin match state.texid with
1420 | Some id ->
1421 Gl.enable `texture_2d;
1422 GlTex.bind_texture `texture_2d id;
1423 let x0 = float x
1424 and y0 = float y
1425 and x1 = float (x+w)
1426 and y1 = float (y+h) in
1428 let tw = float w /. 16.0
1429 and th = float h /. 16.0 in
1430 let tx0 = float tilex /. 16.0
1431 and ty0 = float tiley /. 16.0 in
1432 let tx1 = tx0 +. tw
1433 and ty1 = ty0 +. th in
1434 GlDraw.begins `quads;
1435 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1436 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1437 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1438 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1439 GlDraw.ends ();
1441 Gl.disable `texture_2d;
1442 | None ->
1443 GlDraw.color (1.0, 1.0, 1.0);
1444 GlDraw.rect
1445 (float x, float y)
1446 (float (x+w), float (y+h));
1447 end;
1448 if w > 128 && h > fstate.fontsize + 10
1449 then (
1450 GlDraw.color (0.0, 0.0, 0.0);
1451 let c, r =
1452 if conf.verbose
1453 then (col*conf.tilew, row*conf.tileh)
1454 else col, row
1456 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1458 GlDraw.color color;
1460 itertiles l f
1463 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1465 let tilevisible1 l x y =
1466 let ax0 = l.pagex
1467 and ax1 = l.pagex + l.pagevw
1468 and ay0 = l.pagey
1469 and ay1 = l.pagey + l.pagevh in
1471 let bx0 = x
1472 and by0 = y in
1473 let bx1 = min (bx0 + conf.tilew) l.pagew
1474 and by1 = min (by0 + conf.tileh) l.pageh in
1476 let rx0 = max ax0 bx0
1477 and ry0 = max ay0 by0
1478 and rx1 = min ax1 bx1
1479 and ry1 = min ay1 by1 in
1481 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1482 nonemptyintersection
1485 let tilevisible layout n x y =
1486 let rec findpageinlayout m = function
1487 | l :: rest when l.pageno = n ->
1488 tilevisible1 l x y || (
1489 match conf.columns with
1490 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1491 | _ -> false
1493 | _ :: rest -> findpageinlayout 0 rest
1494 | [] -> false
1496 findpageinlayout 0 layout;
1499 let tileready l x y =
1500 tilevisible1 l x y &&
1501 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1504 let tilepage n p layout =
1505 let rec loop = function
1506 | l :: rest ->
1507 if l.pageno = n
1508 then
1509 let f col row _ _ _ _ _ _ =
1510 if state.currently = Idle
1511 then
1512 match gettileopaque l col row with
1513 | Some _ -> ()
1514 | None ->
1515 let x = col*conf.tilew
1516 and y = row*conf.tileh in
1517 let w =
1518 let w = l.pagew - x in
1519 min w conf.tilew
1521 let h =
1522 let h = l.pageh - y in
1523 min h conf.tileh
1525 let pbo =
1526 if conf.usepbo
1527 then getpbo w h conf.colorspace
1528 else "0"
1530 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1531 state.currently <-
1532 Tiling (
1533 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1534 conf.tilew, conf.tileh
1537 itertiles l f;
1538 else
1539 loop rest
1541 | [] -> ()
1543 if nogeomcmds state.geomcmds
1544 then loop layout;
1547 let preloadlayout y =
1548 let y = if y < conf.winh then 0 else y - conf.winh in
1549 let h = conf.winh*3 in
1550 layout y h;
1553 let load pages =
1554 let rec loop pages =
1555 if state.currently != Idle
1556 then ()
1557 else
1558 match pages with
1559 | l :: rest ->
1560 begin match getopaque l.pageno with
1561 | None ->
1562 wcmd "page %d %d" l.pageno l.pagedimno;
1563 state.currently <- Loading (l, state.gen);
1564 | Some opaque ->
1565 tilepage l.pageno opaque pages;
1566 loop rest
1567 end;
1568 | _ -> ()
1570 if nogeomcmds state.geomcmds
1571 then loop pages
1574 let preload pages =
1575 load pages;
1576 if conf.preload && state.currently = Idle
1577 then load (preloadlayout state.y);
1580 let layoutready layout =
1581 let rec fold all ls =
1582 all && match ls with
1583 | l :: rest ->
1584 let seen = ref false in
1585 let allvisible = ref true in
1586 let foo col row _ _ _ _ _ _ =
1587 seen := true;
1588 allvisible := !allvisible &&
1589 begin match gettileopaque l col row with
1590 | Some _ -> true
1591 | None -> false
1594 itertiles l foo;
1595 fold (!seen && !allvisible) rest
1596 | [] -> true
1598 let alltilesvisible = fold true layout in
1599 alltilesvisible;
1602 let gotoy y =
1603 state.wthack <- false;
1604 let y = bound y 0 state.maxy in
1605 let y, layout, proceed =
1606 match conf.maxwait with
1607 | Some time when state.ghyll == noghyll ->
1608 begin match state.throttle with
1609 | None ->
1610 let layout = layout y conf.winh in
1611 let ready = layoutready layout in
1612 if not ready
1613 then (
1614 load layout;
1615 state.throttle <- Some (layout, y, now ());
1617 else G.postRedisplay "gotoy showall (None)";
1618 y, layout, ready
1619 | Some (_, _, started) ->
1620 let dt = now () -. started in
1621 if dt > time
1622 then (
1623 state.throttle <- None;
1624 let layout = layout y conf.winh in
1625 load layout;
1626 G.postRedisplay "maxwait";
1627 y, layout, true
1629 else -1, [], false
1632 | _ ->
1633 let layout = layout y conf.winh in
1634 G.postRedisplay "gotoy ready";
1635 y, layout, true
1637 if proceed
1638 then (
1639 state.y <- y;
1640 state.layout <- layout;
1641 begin match state.mode with
1642 | LinkNav (Ltexact (pageno, linkno)) ->
1643 let rec loop = function
1644 | [] ->
1645 state.mode <- LinkNav (Ltgendir 0)
1646 | l :: _ when l.pageno = pageno ->
1647 begin match getopaque pageno with
1648 | None ->
1649 state.mode <- LinkNav (Ltgendir 0)
1650 | Some opaque ->
1651 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1652 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1653 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1654 then state.mode <- LinkNav (Ltgendir 0)
1656 | _ :: rest -> loop rest
1658 loop layout
1659 | _ -> ()
1660 end;
1661 begin match state.mode with
1662 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1663 if not (pagevisible layout pageno)
1664 then (
1665 match state.layout with
1666 | [] -> ()
1667 | l :: _ ->
1668 state.mode <- Birdseye (
1669 conf, leftx, l.pageno, hooverpageno, anchor
1672 | LinkNav (Ltgendir dir as lt) ->
1673 let linknav =
1674 let rec loop = function
1675 | [] -> lt
1676 | l :: rest ->
1677 match getopaque l.pageno with
1678 | None -> loop rest
1679 | Some opaque ->
1680 let link =
1681 let ld =
1682 if dir = 0
1683 then LDfirstvisible (l.pagex, l.pagey, dir)
1684 else (
1685 if dir > 0 then LDfirst else LDlast
1688 findlink opaque ld
1690 match link with
1691 | Lnotfound -> loop rest
1692 | Lfound n ->
1693 showlinktype (getlink opaque n);
1694 Ltexact (l.pageno, n)
1696 loop state.layout
1698 state.mode <- LinkNav linknav
1699 | _ -> ()
1700 end;
1701 preload layout;
1703 state.ghyll <- noghyll;
1704 if conf.updatecurs
1705 then (
1706 let mx, my = state.mpos in
1707 updateunder mx my;
1711 let conttiling pageno opaque =
1712 tilepage pageno opaque
1713 (if conf.preload then preloadlayout state.y else state.layout)
1716 let gotoy_and_clear_text y =
1717 if not conf.verbose then state.text <- "";
1718 gotoy y;
1721 let getanchor1 l =
1722 let top =
1723 let coloff = l.pagecol * l.pageh in
1724 float (l.pagey + coloff) /. float l.pageh
1726 let dtop =
1727 if l.pagedispy = 0
1728 then
1730 else
1731 if conf.presentation
1732 then float l.pagedispy /. float (calcips l.pageh)
1733 else float l.pagedispy /. float conf.interpagespace
1735 (l.pageno, top, dtop)
1738 let getanchor () =
1739 match state.layout with
1740 | l :: _ -> getanchor1 l
1741 | [] ->
1742 let n = page_of_y state.y in
1743 let y, h = getpageyh n in
1744 let dy = y - state.y in
1745 let dtop =
1746 if conf.presentation
1747 then
1748 let ips = calcips h in
1749 float (dy + ips) /. float ips
1750 else
1751 float dy /. float conf.interpagespace
1753 (n, 0.0, dtop)
1756 let getanchory (n, top, dtop) =
1757 let y, h = getpageyh n in
1758 if conf.presentation
1759 then
1760 let ips = calcips h in
1761 y + truncate (top*.float h -. dtop*.float ips) + ips;
1762 else
1763 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1766 let gotoanchor anchor =
1767 gotoy (getanchory anchor);
1770 let addnav () =
1771 cbput state.hists.nav (getanchor ());
1774 let getnav dir =
1775 let anchor = cbgetc state.hists.nav dir in
1776 getanchory anchor;
1779 let gotoghyll y =
1780 let scroll f n a b =
1781 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1782 let snake f a b =
1783 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1784 if f < a
1785 then s (float f /. float a)
1786 else (
1787 if f > b
1788 then 1.0 -. s ((float (f-b) /. float (n-b)))
1789 else 1.0
1792 snake f a b
1793 and summa f n a b =
1794 (* courtesy:
1795 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1796 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1797 let iv1 = iv f in
1798 let ins = float a *. iv1
1799 and outs = float (n-b) *. iv1 in
1800 let ones = b - a in
1801 ins +. outs +. float ones
1803 let rec set (_N, _A, _B) y sy =
1804 let sum = summa 1.0 _N _A _B in
1805 let dy = float (y - sy) in
1806 state.ghyll <- (
1807 let rec gf n y1 o =
1808 if n >= _N
1809 then state.ghyll <- noghyll
1810 else
1811 let go n =
1812 let s = scroll n _N _A _B in
1813 let y1 = y1 +. ((s *. dy) /. sum) in
1814 gotoy_and_clear_text (truncate y1);
1815 state.ghyll <- gf (n+1) y1;
1817 match o with
1818 | None -> go n
1819 | Some y' -> set (_N/2, 1, 1) y' state.y
1821 gf 0 (float state.y)
1824 match conf.ghyllscroll with
1825 | None ->
1826 gotoy_and_clear_text y
1827 | Some nab ->
1828 if state.ghyll == noghyll
1829 then set nab y state.y
1830 else state.ghyll (Some y)
1833 let gotopage n top =
1834 let y, h = getpageyh n in
1835 let y = y + (truncate (top *. float h)) in
1836 gotoghyll y
1839 let gotopage1 n top =
1840 let y = getpagey n in
1841 let y = y + top in
1842 gotoghyll y
1845 let invalidate s f =
1846 state.layout <- [];
1847 state.pdims <- [];
1848 state.rects <- [];
1849 state.rects1 <- [];
1850 match state.geomcmds with
1851 | ps, [] when String.length ps = 0 ->
1852 f ();
1853 state.geomcmds <- s, [];
1855 | ps, [] ->
1856 state.geomcmds <- ps, [s, f];
1858 | ps, (s', _) :: rest when s' = s ->
1859 state.geomcmds <- ps, ((s, f) :: rest);
1861 | ps, cmds ->
1862 state.geomcmds <- ps, ((s, f) :: cmds);
1865 let flushpages () =
1866 Hashtbl.iter (fun _ opaque ->
1867 wcmd "freepage %s" opaque;
1868 ) state.pagemap;
1869 Hashtbl.clear state.pagemap;
1872 let opendoc path password =
1873 state.path <- path;
1874 state.password <- password;
1875 state.gen <- state.gen + 1;
1876 state.docinfo <- [];
1878 flushpages ();
1879 setaalevel conf.aalevel;
1880 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1881 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1882 invalidate "reqlayout"
1883 (fun () ->
1884 wcmd "reqlayout %d %d %s\000"
1885 conf.angle (btod conf.proportional) state.nameddest;
1889 let reload () =
1890 state.anchor <- getanchor ();
1891 state.wthack <- !wtmode;
1892 opendoc state.path state.password;
1895 let scalecolor c =
1896 let c = c *. conf.colorscale in
1897 (c, c, c);
1900 let scalecolor2 (r, g, b) =
1901 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1904 let docolumns = function
1905 | Csingle _ ->
1906 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1907 let rec loop pageno pdimno pdim y ph pdims =
1908 if pageno = state.pagecount
1909 then ()
1910 else
1911 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1912 match pdims with
1913 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1914 pdimno+1, pdim, rest
1915 | _ ->
1916 pdimno, pdim, pdims
1918 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1919 let y = y +
1920 (if conf.presentation
1921 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1922 else (if pageno = 0 then 0 else conf.interpagespace)
1925 a.(pageno) <- (pdimno, x, y, pdim);
1926 loop (pageno+1) pdimno pdim (y + h) h pdims
1928 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1929 conf.columns <- Csingle a;
1931 | Cmulti ((columns, coverA, coverB), _) ->
1932 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1933 let rec loop pageno pdimno pdim x y rowh pdims =
1934 let rec fixrow m = if m = pageno then () else
1935 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1936 if h < rowh
1937 then (
1938 let y = y + (rowh - h) / 2 in
1939 a.(m) <- (pdimno, x, y, pdim);
1941 fixrow (m+1)
1943 if pageno = state.pagecount
1944 then fixrow (((pageno - 1) / columns) * columns)
1945 else
1946 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1947 match pdims with
1948 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1949 pdimno+1, pdim, rest
1950 | _ ->
1951 pdimno, pdim, pdims
1953 let x, y, rowh' =
1954 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1955 then (
1956 let x = (conf.winw - state.scrollw - w) / 2 in
1957 let ips =
1958 if conf.presentation then calcips h else conf.interpagespace in
1959 x, y + ips + rowh, h
1961 else (
1962 if (pageno - coverA) mod columns = 0
1963 then (
1964 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1965 let y =
1966 if conf.presentation
1967 then
1968 let ips = calcips h in
1969 y + (if pageno = 0 then 0 else calcips rowh + ips)
1970 else
1971 y + (if pageno = 0 then 0 else conf.interpagespace)
1973 x, y + rowh, h
1975 else x, y, max rowh h
1978 let y =
1979 if pageno > 1 && (pageno - coverA) mod columns = 0
1980 then (
1981 let y =
1982 if pageno = columns && conf.presentation
1983 then (
1984 let ips = calcips rowh in
1985 for i = 0 to pred columns
1987 let (pdimno, x, y, pdim) = a.(i) in
1988 a.(i) <- (pdimno, x, y+ips, pdim)
1989 done;
1990 y+ips;
1992 else y
1994 fixrow (pageno - columns);
1997 else y
1999 a.(pageno) <- (pdimno, x, y, pdim);
2000 let x = x + w + xoff*2 + conf.interpagespace in
2001 loop (pageno+1) pdimno pdim x y rowh' pdims
2003 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2004 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2006 | Csplit (c, _) ->
2007 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2008 let rec loop pageno pdimno pdim y pdims =
2009 if pageno = state.pagecount
2010 then ()
2011 else
2012 let pdimno, ((_, w, h, _) as pdim), pdims =
2013 match pdims with
2014 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2015 pdimno+1, pdim, rest
2016 | _ ->
2017 pdimno, pdim, pdims
2019 let cw = w / c in
2020 let rec loop1 n x y =
2021 if n = c then y else (
2022 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2023 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2026 let y = loop1 0 0 y in
2027 loop (pageno+1) pdimno pdim y pdims
2029 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2030 conf.columns <- Csplit (c, a);
2033 let represent () =
2034 docolumns conf.columns;
2035 state.maxy <- calcheight ();
2036 state.hscrollh <-
2037 if state.w <= conf.winw - state.scrollw
2038 then 0
2039 else state.scrollw
2041 let wthack = state.wthack in
2042 begin match state.mode with
2043 | Birdseye (_, _, pageno, _, _) ->
2044 let y, h = getpageyh pageno in
2045 let top = (conf.winh - h) / 2 in
2046 gotoy (max 0 (y - top))
2047 | _ -> gotoanchor state.anchor
2048 end;
2049 state.wthack <- wthack;
2052 let reshape w h =
2053 state.wthack <- false;
2054 GlDraw.viewport 0 0 w h;
2055 let firsttime = state.geomcmds == firstgeomcmds in
2056 if not firsttime && nogeomcmds state.geomcmds
2057 then state.anchor <- getanchor ();
2059 conf.winw <- w;
2060 let w = truncate (float w *. conf.zoom) - state.scrollw in
2061 let w = max w 2 in
2062 conf.winh <- h;
2063 setfontsize fstate.fontsize;
2064 GlMat.mode `modelview;
2065 GlMat.load_identity ();
2067 GlMat.mode `projection;
2068 GlMat.load_identity ();
2069 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2070 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2071 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2073 let relx =
2074 if conf.zoom <= 1.0
2075 then 0.0
2076 else float state.x /. float state.w
2078 invalidate "geometry"
2079 (fun () ->
2080 state.w <- w;
2081 if not firsttime
2082 then state.x <- truncate (relx *. float w);
2083 let w =
2084 match conf.columns with
2085 | Csingle _ -> w
2086 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2087 | Csplit (c, _) -> w * c
2089 wcmd "geometry %d %d" w h);
2092 let enttext () =
2093 let len = String.length state.text in
2094 let drawstring s =
2095 let hscrollh =
2096 match state.mode with
2097 | Textentry _
2098 | View ->
2099 let h, _, _ = state.uioh#scrollpw in
2101 | _ -> 0
2103 let rect x w =
2104 GlDraw.rect
2105 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2106 (x+.w, float (conf.winh - hscrollh))
2109 let w = float (conf.winw - state.scrollw - 1) in
2110 if state.progress >= 0.0 && state.progress < 1.0
2111 then (
2112 GlDraw.color (0.3, 0.3, 0.3);
2113 let w1 = w *. state.progress in
2114 rect 0.0 w1;
2115 GlDraw.color (0.0, 0.0, 0.0);
2116 rect w1 (w-.w1)
2118 else (
2119 GlDraw.color (0.0, 0.0, 0.0);
2120 rect 0.0 w;
2123 GlDraw.color (1.0, 1.0, 1.0);
2124 drawstring fstate.fontsize
2125 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2127 let s =
2128 match state.mode with
2129 | Textentry ((prefix, text, _, _, _, _), _) ->
2130 let s =
2131 if len > 0
2132 then
2133 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2134 else
2135 Printf.sprintf "%s%s_" prefix text
2139 | _ -> state.text
2141 let s =
2142 if state.newerrmsgs
2143 then (
2144 if not (istextentry state.mode)
2145 then
2146 let s1 = "(press 'e' to review error messasges)" in
2147 if String.length s > 0 then s ^ " " ^ s1 else s1
2148 else s
2150 else s
2152 if String.length s > 0
2153 then drawstring s
2156 let gctiles () =
2157 let len = Queue.length state.tilelru in
2158 let layout = lazy (
2159 match state.throttle with
2160 | None ->
2161 if conf.preload
2162 then preloadlayout state.y
2163 else state.layout
2164 | Some (layout, _, _) ->
2165 layout
2166 ) in
2167 let rec loop qpos =
2168 if state.memused <= conf.memlimit
2169 then ()
2170 else (
2171 if qpos < len
2172 then
2173 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2174 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2175 let (_, pw, ph, _) = getpagedim n in
2177 gen = state.gen
2178 && colorspace = conf.colorspace
2179 && angle = conf.angle
2180 && pagew = pw
2181 && pageh = ph
2182 && (
2183 let x = col*conf.tilew
2184 and y = row*conf.tileh in
2185 tilevisible (Lazy.force_val layout) n x y
2187 then Queue.push lruitem state.tilelru
2188 else (
2189 freepbo p;
2190 wcmd "freetile %s" p;
2191 state.memused <- state.memused - s;
2192 state.uioh#infochanged Memused;
2193 Hashtbl.remove state.tilemap k;
2195 loop (qpos+1)
2198 loop 0
2201 let flushtiles () =
2202 Queue.iter (fun (k, p, s) ->
2203 wcmd "freetile %s" p;
2204 state.memused <- state.memused - s;
2205 state.uioh#infochanged Memused;
2206 Hashtbl.remove state.tilemap k;
2207 ) state.tilelru;
2208 Queue.clear state.tilelru;
2209 load state.layout;
2212 let logcurrently = function
2213 | Idle -> dolog "Idle"
2214 | Loading (l, gen) ->
2215 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2216 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2217 dolog
2218 "Tiling %d[%d,%d] page=%s cs=%s angle"
2219 l.pageno col row pageopaque
2220 (colorspace_to_string colorspace)
2222 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2223 angle gen conf.angle state.gen
2224 tilew tileh
2225 conf.tilew conf.tileh
2227 | Outlining _ ->
2228 dolog "outlining"
2231 let act cmds =
2232 (* dolog "%S" cmds; *)
2233 let op, args =
2234 let spacepos =
2235 try String.index cmds ' '
2236 with Not_found -> -1
2238 if spacepos = -1
2239 then cmds, ""
2240 else
2241 let l = String.length cmds in
2242 let op = String.sub cmds 0 spacepos in
2243 op, begin
2244 if l - spacepos < 2 then ""
2245 else String.sub cmds (spacepos+1) (l-spacepos-1)
2248 match op with
2249 | "clear" ->
2250 state.uioh#infochanged Pdim;
2251 state.pdims <- [];
2253 | "clearrects" ->
2254 state.rects <- state.rects1;
2255 G.postRedisplay "clearrects";
2257 | "continue" ->
2258 let n =
2259 try Scanf.sscanf args "%u" (fun n -> n)
2260 with exn ->
2261 dolog "error processing 'continue' %S: %s"
2262 cmds (Printexc.to_string exn);
2263 exit 1;
2265 state.pagecount <- n;
2266 begin match state.currently with
2267 | Outlining l ->
2268 state.currently <- Idle;
2269 state.outlines <- Array.of_list (List.rev l)
2270 | _ -> ()
2271 end;
2273 let cur, cmds = state.geomcmds in
2274 if String.length cur = 0
2275 then failwith "umpossible";
2277 begin match List.rev cmds with
2278 | [] ->
2279 state.geomcmds <- "", [];
2280 represent ();
2281 | (s, f) :: rest ->
2282 f ();
2283 state.geomcmds <- s, List.rev rest;
2284 end;
2285 if conf.maxwait = None
2286 then G.postRedisplay "continue";
2288 | "title" ->
2289 Wsi.settitle args
2291 | "msg" ->
2292 showtext ' ' args
2294 | "vmsg" ->
2295 if conf.verbose
2296 then showtext ' ' args
2298 | "emsg" ->
2299 Buffer.add_string state.errmsgs args;
2300 state.newerrmsgs <- true;
2301 G.postRedisplay "error message"
2303 | "progress" ->
2304 let progress, text =
2306 Scanf.sscanf args "%f %n"
2307 (fun f pos ->
2308 f, String.sub args pos (String.length args - pos))
2309 with exn ->
2310 dolog "error processing 'progress' %S: %s"
2311 cmds (Printexc.to_string exn);
2312 exit 1;
2314 state.text <- text;
2315 state.progress <- progress;
2316 G.postRedisplay "progress"
2318 | "firstmatch" ->
2319 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2321 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2322 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2323 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2324 with exn ->
2325 dolog "error processing 'firstmatch' %S: %s"
2326 cmds (Printexc.to_string exn);
2327 exit 1;
2329 let y = (getpagey pageno) + truncate y0 in
2330 addnav ();
2331 gotoy y;
2332 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2334 | "match" ->
2335 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2337 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2338 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2339 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2340 with exn ->
2341 dolog "error processing 'match' %S: %s"
2342 cmds (Printexc.to_string exn);
2343 exit 1;
2345 state.rects1 <-
2346 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2348 | "page" ->
2349 let pageopaque, t =
2351 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2352 with exn ->
2353 dolog "error processing 'page' %S: %s"
2354 cmds (Printexc.to_string exn);
2355 exit 1;
2357 begin match state.currently with
2358 | Loading (l, gen) ->
2359 vlog "page %d took %f sec" l.pageno t;
2360 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2361 begin match state.throttle with
2362 | None ->
2363 let preloadedpages =
2364 if conf.preload
2365 then preloadlayout state.y
2366 else state.layout
2368 let evict () =
2369 let module IntSet =
2370 Set.Make (struct type t = int let compare = (-) end) in
2371 let set =
2372 List.fold_left (fun s l -> IntSet.add l.pageno s)
2373 IntSet.empty preloadedpages
2375 let evictedpages =
2376 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2377 if not (IntSet.mem pageno set)
2378 then (
2379 wcmd "freepage %s" opaque;
2380 key :: accu
2382 else accu
2383 ) state.pagemap []
2385 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2387 evict ();
2388 state.currently <- Idle;
2389 if gen = state.gen
2390 then (
2391 tilepage l.pageno pageopaque state.layout;
2392 load state.layout;
2393 load preloadedpages;
2394 if pagevisible state.layout l.pageno
2395 && layoutready state.layout
2396 then G.postRedisplay "page";
2399 | Some (layout, _, _) ->
2400 state.currently <- Idle;
2401 tilepage l.pageno pageopaque layout;
2402 load state.layout
2403 end;
2405 | _ ->
2406 dolog "Inconsistent loading state";
2407 logcurrently state.currently;
2408 exit 1
2411 | "tile" ->
2412 let (x, y, opaque, size, t) =
2414 Scanf.sscanf args "%u %u %s %u %f"
2415 (fun x y p size t -> (x, y, p, size, t))
2416 with exn ->
2417 dolog "error processing 'tile' %S: %s"
2418 cmds (Printexc.to_string exn);
2419 exit 1;
2421 begin match state.currently with
2422 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2423 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2425 unmappbo opaque;
2426 if tilew != conf.tilew || tileh != conf.tileh
2427 then (
2428 wcmd "freetile %s" opaque;
2429 state.currently <- Idle;
2430 load state.layout;
2432 else (
2433 puttileopaque l col row gen cs angle opaque size t;
2434 state.memused <- state.memused + size;
2435 state.uioh#infochanged Memused;
2436 gctiles ();
2437 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2438 opaque, size) state.tilelru;
2440 let layout =
2441 match state.throttle with
2442 | None -> state.layout
2443 | Some (layout, _, _) -> layout
2446 state.currently <- Idle;
2447 if gen = state.gen
2448 && conf.colorspace = cs
2449 && conf.angle = angle
2450 && tilevisible layout l.pageno x y
2451 then conttiling l.pageno pageopaque;
2453 begin match state.throttle with
2454 | None ->
2455 if state.wthack
2456 then state.wthack <- not (layoutready state.layout);
2457 preload state.layout;
2458 if gen = state.gen
2459 && conf.colorspace = cs
2460 && conf.angle = angle
2461 && tilevisible state.layout l.pageno x y
2462 then G.postRedisplay "tile nothrottle";
2464 | Some (layout, y, _) ->
2465 let ready = layoutready layout in
2466 if ready
2467 then (
2468 state.wthack <- false;
2469 state.y <- y;
2470 state.layout <- layout;
2471 state.throttle <- None;
2472 G.postRedisplay "throttle";
2474 else load layout;
2475 end;
2478 | _ ->
2479 dolog "Inconsistent tiling state";
2480 logcurrently state.currently;
2481 exit 1
2484 | "pdim" ->
2485 let pdim =
2487 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2488 with exn ->
2489 dolog "error processing 'pdim' %S: %s"
2490 cmds (Printexc.to_string exn);
2491 exit 1;
2493 state.uioh#infochanged Pdim;
2494 state.pdims <- pdim :: state.pdims
2496 | "o" ->
2497 let (l, n, t, h, pos) =
2499 Scanf.sscanf args "%u %u %d %u %n"
2500 (fun l n t h pos -> l, n, t, h, pos)
2501 with exn ->
2502 dolog "error processing 'o' %S: %s"
2503 cmds (Printexc.to_string exn);
2504 exit 1;
2506 let s = String.sub args pos (String.length args - pos) in
2507 let outline = (s, l, (n, float t /. float h, 0.0)) in
2508 begin match state.currently with
2509 | Outlining outlines ->
2510 state.currently <- Outlining (outline :: outlines)
2511 | Idle ->
2512 state.currently <- Outlining [outline]
2513 | currently ->
2514 dolog "invalid outlining state";
2515 logcurrently currently
2518 | "a" ->
2519 let (n, t, h) =
2521 Scanf.sscanf args "%u %u %d"
2522 (fun n t h -> n, t, h)
2523 with exn ->
2524 dolog "error processing 'a' %S: %s"
2525 cmds (Printexc.to_string exn);
2526 exit 1;
2528 let top, dtop =
2529 if conf.presentation
2530 then (0.0, 1.0)
2531 else float t /. float h, 0.0
2533 state.anchor <- (n, top, dtop)
2535 | "info" ->
2536 state.docinfo <- (1, args) :: state.docinfo
2538 | "infoend" ->
2539 state.uioh#infochanged Docinfo;
2540 state.docinfo <- List.rev state.docinfo
2542 | _ ->
2543 dolog "unknown cmd `%S'" cmds
2546 let onhist cb =
2547 let rc = cb.rc in
2548 let action = function
2549 | HCprev -> cbget cb ~-1
2550 | HCnext -> cbget cb 1
2551 | HCfirst -> cbget cb ~-(cb.rc)
2552 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2553 and cancel () = cb.rc <- rc
2554 in (action, cancel)
2557 let search pattern forward =
2558 if String.length pattern > 0
2559 then
2560 let pn, py =
2561 match state.layout with
2562 | [] -> 0, 0
2563 | l :: _ ->
2564 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2566 wcmd "search %d %d %d %d,%s\000"
2567 (btod conf.icase) pn py (btod forward) pattern;
2570 let intentry text key =
2571 let c =
2572 if key >= 32 && key < 127
2573 then Char.chr key
2574 else '\000'
2576 match c with
2577 | '0' .. '9' ->
2578 let text = addchar text c in
2579 TEcont text
2581 | _ ->
2582 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2583 TEcont text
2586 let linknentry text key =
2587 let c =
2588 if key >= 32 && key < 127
2589 then Char.chr key
2590 else '\000'
2592 match c with
2593 | 'a' .. 'z' ->
2594 let text = addchar text c in
2595 TEcont text
2597 | _ ->
2598 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2599 TEcont text
2602 let linkndone f s =
2603 if String.length s > 0
2604 then (
2605 let n =
2606 let l = String.length s in
2607 let rec loop pos n = if pos = l then n else
2608 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2609 loop (pos+1) (n*26 + m)
2610 in loop 0 0
2612 let rec loop n = function
2613 | [] -> ()
2614 | l :: rest ->
2615 match getopaque l.pageno with
2616 | None -> loop n rest
2617 | Some opaque ->
2618 let m = getlinkcount opaque in
2619 if n < m
2620 then (
2621 let under = getlink opaque n in
2622 f under
2624 else loop (n-m) rest
2626 loop n state.layout;
2630 let textentry text key =
2631 if key land 0xff00 = 0xff00
2632 then TEcont text
2633 else TEcont (text ^ Wsi.toutf8 key)
2636 let reqlayout angle proportional =
2637 match state.throttle with
2638 | None ->
2639 if nogeomcmds state.geomcmds
2640 then state.anchor <- getanchor ();
2641 conf.angle <- angle mod 360;
2642 if conf.angle != 0
2643 then (
2644 match state.mode with
2645 | LinkNav _ -> state.mode <- View
2646 | _ -> ()
2648 conf.proportional <- proportional;
2649 invalidate "reqlayout"
2650 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2651 | _ -> ()
2654 let settrim trimmargins trimfuzz =
2655 if nogeomcmds state.geomcmds
2656 then state.anchor <- getanchor ();
2657 conf.trimmargins <- trimmargins;
2658 conf.trimfuzz <- trimfuzz;
2659 let x0, y0, x1, y1 = trimfuzz in
2660 invalidate "settrim"
2661 (fun () ->
2662 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2663 flushpages ();
2666 let setzoom zoom =
2667 match state.throttle with
2668 | None ->
2669 let zoom = max 0.01 zoom in
2670 if zoom <> conf.zoom
2671 then (
2672 state.prevzoom <- conf.zoom;
2673 conf.zoom <- zoom;
2674 reshape conf.winw conf.winh;
2675 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2678 | Some (layout, y, started) ->
2679 let time =
2680 match conf.maxwait with
2681 | None -> 0.0
2682 | Some t -> t
2684 let dt = now () -. started in
2685 if dt > time
2686 then (
2687 state.y <- y;
2688 load layout;
2692 let setcolumns mode columns coverA coverB =
2693 state.prevcolumns <- Some (conf.columns, conf.zoom);
2694 if columns < 0
2695 then (
2696 if isbirdseye mode
2697 then showtext '!' "split mode doesn't work in bird's eye"
2698 else (
2699 conf.columns <- Csplit (-columns, [||]);
2700 state.x <- 0;
2701 conf.zoom <- 1.0;
2704 else (
2705 if columns < 2
2706 then (
2707 conf.columns <- Csingle [||];
2708 state.x <- 0;
2709 setzoom 1.0;
2711 else (
2712 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2713 conf.zoom <- 1.0;
2716 reshape conf.winw conf.winh;
2719 let enterbirdseye () =
2720 let zoom = float conf.thumbw /. float conf.winw in
2721 let birdseyepageno =
2722 let cy = conf.winh / 2 in
2723 let fold = function
2724 | [] -> 0
2725 | l :: rest ->
2726 let rec fold best = function
2727 | [] -> best.pageno
2728 | l :: rest ->
2729 let d = cy - (l.pagedispy + l.pagevh/2)
2730 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2731 if abs d < abs dbest
2732 then fold l rest
2733 else best.pageno
2734 in fold l rest
2736 fold state.layout
2738 state.mode <- Birdseye (
2739 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2741 conf.zoom <- zoom;
2742 conf.presentation <- false;
2743 conf.interpagespace <- 10;
2744 conf.hlinks <- false;
2745 state.x <- 0;
2746 state.mstate <- Mnone;
2747 conf.maxwait <- None;
2748 conf.columns <- (
2749 match conf.beyecolumns with
2750 | Some c ->
2751 conf.zoom <- 1.0;
2752 Cmulti ((c, 0, 0), [||])
2753 | None -> Csingle [||]
2755 Wsi.setcursor Wsi.CURSOR_INHERIT;
2756 if conf.verbose
2757 then
2758 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2759 (100.0*.zoom)
2760 else
2761 state.text <- ""
2763 reshape conf.winw conf.winh;
2766 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2767 state.mode <- View;
2768 conf.zoom <- c.zoom;
2769 conf.presentation <- c.presentation;
2770 conf.interpagespace <- c.interpagespace;
2771 conf.maxwait <- c.maxwait;
2772 conf.hlinks <- c.hlinks;
2773 conf.beyecolumns <- (
2774 match conf.columns with
2775 | Cmulti ((c, _, _), _) -> Some c
2776 | Csingle _ -> None
2777 | Csplit _ -> failwith "leaving bird's eye split mode"
2779 conf.columns <- (
2780 match c.columns with
2781 | Cmulti (c, _) -> Cmulti (c, [||])
2782 | Csingle _ -> Csingle [||]
2783 | Csplit (c, _) -> Csplit (c, [||])
2785 state.x <- leftx;
2786 if conf.verbose
2787 then
2788 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2789 (100.0*.conf.zoom)
2791 reshape conf.winw conf.winh;
2792 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2795 let togglebirdseye () =
2796 match state.mode with
2797 | Birdseye vals -> leavebirdseye vals true
2798 | View -> enterbirdseye ()
2799 | _ -> ()
2802 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2803 let pageno = max 0 (pageno - incr) in
2804 let rec loop = function
2805 | [] -> gotopage1 pageno 0
2806 | l :: _ when l.pageno = pageno ->
2807 if l.pagedispy >= 0 && l.pagey = 0
2808 then G.postRedisplay "upbirdseye"
2809 else gotopage1 pageno 0
2810 | _ :: rest -> loop rest
2812 loop state.layout;
2813 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2816 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2817 let pageno = min (state.pagecount - 1) (pageno + incr) in
2818 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2819 let rec loop = function
2820 | [] ->
2821 let y, h = getpageyh pageno in
2822 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2823 gotoy (clamp dy)
2824 | l :: _ when l.pageno = pageno ->
2825 if l.pagevh != l.pageh
2826 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2827 else G.postRedisplay "downbirdseye"
2828 | _ :: rest -> loop rest
2830 loop state.layout
2833 let optentry mode _ key =
2834 let btos b = if b then "on" else "off" in
2835 if key >= 32 && key < 127
2836 then
2837 let c = Char.chr key in
2838 match c with
2839 | 's' ->
2840 let ondone s =
2841 try conf.scrollstep <- int_of_string s with exc ->
2842 state.text <- Printf.sprintf "bad integer `%s': %s"
2843 s (Printexc.to_string exc)
2845 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2847 | 'A' ->
2848 let ondone s =
2850 conf.autoscrollstep <- int_of_string s;
2851 if state.autoscroll <> None
2852 then state.autoscroll <- Some conf.autoscrollstep
2853 with exc ->
2854 state.text <- Printf.sprintf "bad integer `%s': %s"
2855 s (Printexc.to_string exc)
2857 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2859 | 'C' ->
2860 let ondone s =
2862 let n, a, b = multicolumns_of_string s in
2863 setcolumns mode n a b;
2864 with exc ->
2865 state.text <- Printf.sprintf "bad columns `%s': %s"
2866 s (Printexc.to_string exc)
2868 TEswitch ("columns: ", "", None, textentry, ondone, true)
2870 | 'Z' ->
2871 let ondone s =
2873 let zoom = float (int_of_string s) /. 100.0 in
2874 setzoom zoom
2875 with exc ->
2876 state.text <- Printf.sprintf "bad integer `%s': %s"
2877 s (Printexc.to_string exc)
2879 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2881 | 't' ->
2882 let ondone s =
2884 conf.thumbw <- bound (int_of_string s) 2 4096;
2885 state.text <-
2886 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2887 begin match mode with
2888 | Birdseye beye ->
2889 leavebirdseye beye false;
2890 enterbirdseye ();
2891 | _ -> ();
2893 with exc ->
2894 state.text <- Printf.sprintf "bad integer `%s': %s"
2895 s (Printexc.to_string exc)
2897 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2899 | 'R' ->
2900 let ondone s =
2901 match try
2902 Some (int_of_string s)
2903 with exc ->
2904 state.text <- Printf.sprintf "bad integer `%s': %s"
2905 s (Printexc.to_string exc);
2906 None
2907 with
2908 | Some angle -> reqlayout angle conf.proportional
2909 | None -> ()
2911 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2913 | 'i' ->
2914 conf.icase <- not conf.icase;
2915 TEdone ("case insensitive search " ^ (btos conf.icase))
2917 | 'p' ->
2918 conf.preload <- not conf.preload;
2919 gotoy state.y;
2920 TEdone ("preload " ^ (btos conf.preload))
2922 | 'v' ->
2923 conf.verbose <- not conf.verbose;
2924 TEdone ("verbose " ^ (btos conf.verbose))
2926 | 'd' ->
2927 conf.debug <- not conf.debug;
2928 TEdone ("debug " ^ (btos conf.debug))
2930 | 'h' ->
2931 conf.maxhfit <- not conf.maxhfit;
2932 state.maxy <- calcheight ();
2933 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2935 | 'c' ->
2936 conf.crophack <- not conf.crophack;
2937 TEdone ("crophack " ^ btos conf.crophack)
2939 | 'a' ->
2940 let s =
2941 match conf.maxwait with
2942 | None ->
2943 conf.maxwait <- Some infinity;
2944 "always wait for page to complete"
2945 | Some _ ->
2946 conf.maxwait <- None;
2947 "show placeholder if page is not ready"
2949 TEdone s
2951 | 'f' ->
2952 conf.underinfo <- not conf.underinfo;
2953 TEdone ("underinfo " ^ btos conf.underinfo)
2955 | 'P' ->
2956 conf.savebmarks <- not conf.savebmarks;
2957 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2959 | 'S' ->
2960 let ondone s =
2962 let pageno, py =
2963 match state.layout with
2964 | [] -> 0, 0
2965 | l :: _ ->
2966 l.pageno, l.pagey
2968 conf.interpagespace <- int_of_string s;
2969 docolumns conf.columns;
2970 state.maxy <- calcheight ();
2971 let y = getpagey pageno in
2972 gotoy (y + py)
2973 with exc ->
2974 state.text <- Printf.sprintf "bad integer `%s': %s"
2975 s (Printexc.to_string exc)
2977 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2979 | 'l' ->
2980 reqlayout conf.angle (not conf.proportional);
2981 TEdone ("proportional display " ^ btos conf.proportional)
2983 | 'T' ->
2984 settrim (not conf.trimmargins) conf.trimfuzz;
2985 TEdone ("trim margins " ^ btos conf.trimmargins)
2987 | 'I' ->
2988 conf.invert <- not conf.invert;
2989 TEdone ("invert colors " ^ btos conf.invert)
2991 | 'x' ->
2992 let ondone s =
2993 cbput state.hists.sel s;
2994 conf.selcmd <- s;
2996 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2997 textentry, ondone, true)
2999 | _ ->
3000 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3001 TEstop
3002 else
3003 TEcont state.text
3006 class type lvsource = object
3007 method getitemcount : int
3008 method getitem : int -> (string * int)
3009 method hasaction : int -> bool
3010 method exit :
3011 uioh:uioh ->
3012 cancel:bool ->
3013 active:int ->
3014 first:int ->
3015 pan:int ->
3016 qsearch:string ->
3017 uioh option
3018 method getactive : int
3019 method getfirst : int
3020 method getqsearch : string
3021 method setqsearch : string -> unit
3022 method getpan : int
3023 end;;
3025 class virtual lvsourcebase = object
3026 val mutable m_active = 0
3027 val mutable m_first = 0
3028 val mutable m_qsearch = ""
3029 val mutable m_pan = 0
3030 method getactive = m_active
3031 method getfirst = m_first
3032 method getqsearch = m_qsearch
3033 method getpan = m_pan
3034 method setqsearch s = m_qsearch <- s
3035 end;;
3037 let withoutlastutf8 s =
3038 let len = String.length s in
3039 if len = 0
3040 then s
3041 else
3042 let rec find pos =
3043 if pos = 0
3044 then pos
3045 else
3046 let b = Char.code s.[pos] in
3047 if b land 0b11000000 = 0b11000000
3048 then pos
3049 else find (pos-1)
3051 let first =
3052 if Char.code s.[len-1] land 0x80 = 0
3053 then len-1
3054 else find (len-1)
3056 String.sub s 0 first;
3059 let textentrykeyboard
3060 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3061 let key =
3062 if key >= 0xffb0 && key <= 0xffb9
3063 then key - 0xffb0 + 48 else key
3065 let enttext te =
3066 state.mode <- Textentry (te, onleave);
3067 state.text <- "";
3068 enttext ();
3069 G.postRedisplay "textentrykeyboard enttext";
3071 let histaction cmd =
3072 match opthist with
3073 | None -> ()
3074 | Some (action, _) ->
3075 state.mode <- Textentry (
3076 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3078 G.postRedisplay "textentry histaction"
3080 match key with
3081 | 0xff08 -> (* backspace *)
3082 let s = withoutlastutf8 text in
3083 let len = String.length s in
3084 if cancelonempty && len = 0
3085 then (
3086 onleave Cancel;
3087 G.postRedisplay "textentrykeyboard after cancel";
3089 else (
3090 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3093 | 0xff0d | 0xff8d -> (* (kp) enter *)
3094 ondone text;
3095 onleave Confirm;
3096 G.postRedisplay "textentrykeyboard after confirm"
3098 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3099 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3100 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3101 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3103 | 0xff1b -> (* escape*)
3104 if String.length text = 0
3105 then (
3106 begin match opthist with
3107 | None -> ()
3108 | Some (_, onhistcancel) -> onhistcancel ()
3109 end;
3110 onleave Cancel;
3111 state.text <- "";
3112 G.postRedisplay "textentrykeyboard after cancel2"
3114 else (
3115 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3118 | 0xff9f | 0xffff -> () (* delete *)
3120 | _ when key != 0
3121 && key land 0xff00 != 0xff00 (* keyboard *)
3122 && key land 0xfe00 != 0xfe00 (* xkb *)
3123 && key land 0xfd00 != 0xfd00 (* 3270 *)
3125 begin match onkey text key with
3126 | TEdone text ->
3127 ondone text;
3128 onleave Confirm;
3129 G.postRedisplay "textentrykeyboard after confirm2";
3131 | TEcont text ->
3132 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3134 | TEstop ->
3135 onleave Cancel;
3136 G.postRedisplay "textentrykeyboard after cancel3"
3138 | TEswitch te ->
3139 state.mode <- Textentry (te, onleave);
3140 G.postRedisplay "textentrykeyboard switch";
3141 end;
3143 | _ ->
3144 vlog "unhandled key %s" (Wsi.keyname key)
3147 let firstof first active =
3148 if first > active || abs (first - active) > fstate.maxrows - 1
3149 then max 0 (active - (fstate.maxrows/2))
3150 else first
3153 let calcfirst first active =
3154 if active > first
3155 then
3156 let rows = active - first in
3157 if rows > fstate.maxrows then active - fstate.maxrows else first
3158 else active
3161 let scrollph y maxy =
3162 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3163 let sh = float conf.winh /. sh in
3164 let sh = max sh (float conf.scrollh) in
3166 let percent =
3167 if y = state.maxy
3168 then 1.0
3169 else float y /. float maxy
3171 let position = (float conf.winh -. sh) *. percent in
3173 let position =
3174 if position +. sh > float conf.winh
3175 then float conf.winh -. sh
3176 else position
3178 position, sh;
3181 let coe s = (s :> uioh);;
3183 class listview ~(source:lvsource) ~trusted ~modehash =
3184 object (self)
3185 val m_pan = source#getpan
3186 val m_first = source#getfirst
3187 val m_active = source#getactive
3188 val m_qsearch = source#getqsearch
3189 val m_prev_uioh = state.uioh
3191 method private elemunder y =
3192 let n = y / (fstate.fontsize+1) in
3193 if m_first + n < source#getitemcount
3194 then (
3195 if source#hasaction (m_first + n)
3196 then Some (m_first + n)
3197 else None
3199 else None
3201 method display =
3202 Gl.enable `blend;
3203 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3204 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3205 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3206 GlDraw.color (1., 1., 1.);
3207 Gl.enable `texture_2d;
3208 let fs = fstate.fontsize in
3209 let nfs = fs + 1 in
3210 let ww = fstate.wwidth in
3211 let tabw = 30.0*.ww in
3212 let itemcount = source#getitemcount in
3213 let rec loop row =
3214 if (row - m_first) > fstate.maxrows
3215 then ()
3216 else (
3217 if row >= 0 && row < itemcount
3218 then (
3219 let (s, level) = source#getitem row in
3220 let y = (row - m_first) * nfs in
3221 let x = 5.0 +. float (level + m_pan) *. ww in
3222 if row = m_active
3223 then (
3224 Gl.disable `texture_2d;
3225 GlDraw.polygon_mode `both `line;
3226 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3227 GlDraw.rect (1., float (y + 1))
3228 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3229 GlDraw.polygon_mode `both `fill;
3230 GlDraw.color (1., 1., 1.);
3231 Gl.enable `texture_2d;
3234 let drawtabularstring s =
3235 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3236 if trusted
3237 then
3238 let tabpos = try String.index s '\t' with Not_found -> -1 in
3239 if tabpos > 0
3240 then
3241 let len = String.length s - tabpos - 1 in
3242 let s1 = String.sub s 0 tabpos
3243 and s2 = String.sub s (tabpos + 1) len in
3244 let nx = drawstr x s1 in
3245 let sw = nx -. x in
3246 let x = x +. (max tabw sw) in
3247 drawstr x s2
3248 else
3249 drawstr x s
3250 else
3251 drawstr x s
3253 let _ = drawtabularstring s in
3254 loop (row+1)
3258 loop m_first;
3259 Gl.disable `blend;
3260 Gl.disable `texture_2d;
3262 method updownlevel incr =
3263 let len = source#getitemcount in
3264 let curlevel =
3265 if m_active >= 0 && m_active < len
3266 then snd (source#getitem m_active)
3267 else -1
3269 let rec flow i =
3270 if i = len then i-1 else if i = -1 then 0 else
3271 let _, l = source#getitem i in
3272 if l != curlevel then i else flow (i+incr)
3274 let active = flow m_active in
3275 let first = calcfirst m_first active in
3276 G.postRedisplay "outline updownlevel";
3277 {< m_active = active; m_first = first >}
3279 method private key1 key mask =
3280 let set1 active first qsearch =
3281 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3283 let search active pattern incr =
3284 let dosearch re =
3285 let rec loop n =
3286 if n >= 0 && n < source#getitemcount
3287 then (
3288 let s, _ = source#getitem n in
3290 (try ignore (Str.search_forward re s 0); true
3291 with Not_found -> false)
3292 then Some n
3293 else loop (n + incr)
3295 else None
3297 loop active
3300 let re = Str.regexp_case_fold pattern in
3301 dosearch re
3302 with Failure s ->
3303 state.text <- s;
3304 None
3306 let itemcount = source#getitemcount in
3307 let find start incr =
3308 let rec find i =
3309 if i = -1 || i = itemcount
3310 then -1
3311 else (
3312 if source#hasaction i
3313 then i
3314 else find (i + incr)
3317 find start
3319 let set active first =
3320 let first = bound first 0 (itemcount - fstate.maxrows) in
3321 state.text <- "";
3322 coe {< m_active = active; m_first = first >}
3324 let navigate incr =
3325 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3326 let active, first =
3327 let incr1 = if incr > 0 then 1 else -1 in
3328 if isvisible m_first m_active
3329 then
3330 let next =
3331 let next = m_active + incr in
3332 let next =
3333 if next < 0 || next >= itemcount
3334 then -1
3335 else find next incr1
3337 if next = -1 || abs (m_active - next) > fstate.maxrows
3338 then -1
3339 else next
3341 if next = -1
3342 then
3343 let first = m_first + incr in
3344 let first = bound first 0 (itemcount - 1) in
3345 let next =
3346 let next = m_active + incr in
3347 let next = bound next 0 (itemcount - 1) in
3348 find next ~-incr1
3350 let active = if next = -1 then m_active else next in
3351 active, first
3352 else
3353 let first = min next m_first in
3354 let first =
3355 if abs (next - first) > fstate.maxrows
3356 then first + incr
3357 else first
3359 next, first
3360 else
3361 let first = m_first + incr in
3362 let first = bound first 0 (itemcount - 1) in
3363 let active =
3364 let next = m_active + incr in
3365 let next = bound next 0 (itemcount - 1) in
3366 let next = find next incr1 in
3367 let active =
3368 if next = -1 || abs (m_active - first) > fstate.maxrows
3369 then (
3370 let active = if m_active = -1 then next else m_active in
3371 active
3373 else next
3375 if isvisible first active
3376 then active
3377 else -1
3379 active, first
3381 G.postRedisplay "listview navigate";
3382 set active first;
3384 match key with
3385 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3386 let incr = if key = 0x72 then -1 else 1 in
3387 let active, first =
3388 match search (m_active + incr) m_qsearch incr with
3389 | None ->
3390 state.text <- m_qsearch ^ " [not found]";
3391 m_active, m_first
3392 | Some active ->
3393 state.text <- m_qsearch;
3394 active, firstof m_first active
3396 G.postRedisplay "listview ctrl-r/s";
3397 set1 active first m_qsearch;
3399 | 0xff08 -> (* backspace *)
3400 if String.length m_qsearch = 0
3401 then coe self
3402 else (
3403 let qsearch = withoutlastutf8 m_qsearch in
3404 let len = String.length qsearch in
3405 if len = 0
3406 then (
3407 state.text <- "";
3408 G.postRedisplay "listview empty qsearch";
3409 set1 m_active m_first "";
3411 else
3412 let active, first =
3413 match search m_active qsearch ~-1 with
3414 | None ->
3415 state.text <- qsearch ^ " [not found]";
3416 m_active, m_first
3417 | Some active ->
3418 state.text <- qsearch;
3419 active, firstof m_first active
3421 G.postRedisplay "listview backspace qsearch";
3422 set1 active first qsearch
3425 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3426 let pattern = m_qsearch ^ Wsi.toutf8 key in
3427 let active, first =
3428 match search m_active pattern 1 with
3429 | None ->
3430 state.text <- pattern ^ " [not found]";
3431 m_active, m_first
3432 | Some active ->
3433 state.text <- pattern;
3434 active, firstof m_first active
3436 G.postRedisplay "listview qsearch add";
3437 set1 active first pattern;
3439 | 0xff1b -> (* escape *)
3440 state.text <- "";
3441 if String.length m_qsearch = 0
3442 then (
3443 G.postRedisplay "list view escape";
3444 begin
3445 match
3446 source#exit (coe self) true m_active m_first m_pan m_qsearch
3447 with
3448 | None -> m_prev_uioh
3449 | Some uioh -> uioh
3452 else (
3453 G.postRedisplay "list view kill qsearch";
3454 source#setqsearch "";
3455 coe {< m_qsearch = "" >}
3458 | 0xff0d | 0xff8d -> (* (kp) enter *)
3459 state.text <- "";
3460 let self = {< m_qsearch = "" >} in
3461 source#setqsearch "";
3462 let opt =
3463 G.postRedisplay "listview enter";
3464 if m_active >= 0 && m_active < source#getitemcount
3465 then (
3466 source#exit (coe self) false m_active m_first m_pan "";
3468 else (
3469 source#exit (coe self) true m_active m_first m_pan "";
3472 begin match opt with
3473 | None -> m_prev_uioh
3474 | Some uioh -> uioh
3477 | 0xff9f | 0xffff -> (* (kp) delete *)
3478 coe self
3480 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3481 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3482 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3483 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3485 | 0xff53 | 0xff98 -> (* (kp) right *)
3486 state.text <- "";
3487 G.postRedisplay "listview right";
3488 coe {< m_pan = m_pan - 1 >}
3490 | 0xff51 | 0xff96 -> (* (kp) left *)
3491 state.text <- "";
3492 G.postRedisplay "listview left";
3493 coe {< m_pan = m_pan + 1 >}
3495 | 0xff50 | 0xff95 -> (* (kp) home *)
3496 let active = find 0 1 in
3497 G.postRedisplay "listview home";
3498 set active 0;
3500 | 0xff57 | 0xff9c -> (* (kp) end *)
3501 let first = max 0 (itemcount - fstate.maxrows) in
3502 let active = find (itemcount - 1) ~-1 in
3503 G.postRedisplay "listview end";
3504 set active first;
3506 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3507 coe self
3509 | _ ->
3510 dolog "listview unknown key %#x" key; coe self
3512 method key key mask =
3513 match state.mode with
3514 | Textentry te -> textentrykeyboard key mask te; coe self
3515 | _ -> self#key1 key mask
3517 method button button down x y _ =
3518 let opt =
3519 match button with
3520 | 1 when x > conf.winw - conf.scrollbw ->
3521 G.postRedisplay "listview scroll";
3522 if down
3523 then
3524 let _, position, sh = self#scrollph in
3525 if y > truncate position && y < truncate (position +. sh)
3526 then (
3527 state.mstate <- Mscrolly;
3528 Some (coe self)
3530 else
3531 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3532 let first = truncate (s *. float source#getitemcount) in
3533 let first = min source#getitemcount first in
3534 Some (coe {< m_first = first; m_active = first >})
3535 else (
3536 state.mstate <- Mnone;
3537 Some (coe self);
3539 | 1 when not down ->
3540 begin match self#elemunder y with
3541 | Some n ->
3542 G.postRedisplay "listview click";
3543 source#exit
3544 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3545 | _ ->
3546 Some (coe self)
3548 | n when (n == 4 || n == 5) && not down ->
3549 let len = source#getitemcount in
3550 let first =
3551 if n = 5 && m_first + fstate.maxrows >= len
3552 then
3553 m_first
3554 else
3555 let first = m_first + (if n == 4 then -1 else 1) in
3556 bound first 0 (len - 1)
3558 G.postRedisplay "listview wheel";
3559 Some (coe {< m_first = first >})
3560 | n when (n = 6 || n = 7) && not down ->
3561 let inc = m_first + (if n = 7 then -1 else 1) in
3562 G.postRedisplay "listview hwheel";
3563 Some (coe {< m_pan = m_pan + inc >})
3564 | _ ->
3565 Some (coe self)
3567 match opt with
3568 | None -> m_prev_uioh
3569 | Some uioh -> uioh
3571 method motion _ y =
3572 match state.mstate with
3573 | Mscrolly ->
3574 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3575 let first = truncate (s *. float source#getitemcount) in
3576 let first = min source#getitemcount first in
3577 G.postRedisplay "listview motion";
3578 coe {< m_first = first; m_active = first >}
3579 | _ -> coe self
3581 method pmotion x y =
3582 if x < conf.winw - conf.scrollbw
3583 then
3584 let n =
3585 match self#elemunder y with
3586 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3587 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3589 let o =
3590 if n != m_active
3591 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3592 else self
3594 coe o
3595 else (
3596 Wsi.setcursor Wsi.CURSOR_INHERIT;
3597 coe self
3600 method infochanged _ = ()
3602 method scrollpw = (0, 0.0, 0.0)
3603 method scrollph =
3604 let nfs = fstate.fontsize + 1 in
3605 let y = m_first * nfs in
3606 let itemcount = source#getitemcount in
3607 let maxi = max 0 (itemcount - fstate.maxrows) in
3608 let maxy = maxi * nfs in
3609 let p, h = scrollph y maxy in
3610 conf.scrollbw, p, h
3612 method modehash = modehash
3613 end;;
3615 class outlinelistview ~source =
3616 object (self)
3617 inherit listview
3618 ~source:(source :> lvsource)
3619 ~trusted:false
3620 ~modehash:(findkeyhash conf "outline")
3621 as super
3623 method key key mask =
3624 let calcfirst first active =
3625 if active > first
3626 then
3627 let rows = active - first in
3628 let maxrows =
3629 if String.length state.text = 0
3630 then fstate.maxrows
3631 else fstate.maxrows - 2
3633 if rows > maxrows then active - maxrows else first
3634 else active
3636 let navigate incr =
3637 let active = m_active + incr in
3638 let active = bound active 0 (source#getitemcount - 1) in
3639 let first = calcfirst m_first active in
3640 G.postRedisplay "outline navigate";
3641 coe {< m_active = active; m_first = first >}
3643 let ctrl = Wsi.withctrl mask in
3644 match key with
3645 | 110 when ctrl -> (* ctrl-n *)
3646 source#narrow m_qsearch;
3647 G.postRedisplay "outline ctrl-n";
3648 coe {< m_first = 0; m_active = 0 >}
3650 | 117 when ctrl -> (* ctrl-u *)
3651 source#denarrow;
3652 G.postRedisplay "outline ctrl-u";
3653 state.text <- "";
3654 coe {< m_first = 0; m_active = 0 >}
3656 | 108 when ctrl -> (* ctrl-l *)
3657 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3658 G.postRedisplay "outline ctrl-l";
3659 coe {< m_first = first >}
3661 | 0xff9f | 0xffff -> (* (kp) delete *)
3662 source#remove m_active;
3663 G.postRedisplay "outline delete";
3664 let active = max 0 (m_active-1) in
3665 coe {< m_first = firstof m_first active;
3666 m_active = active >}
3668 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3669 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3670 | 0xff55 | 0xff9a -> (* (kp) prior *)
3671 navigate ~-(fstate.maxrows)
3672 | 0xff56 | 0xff9b -> (* (kp) next *)
3673 navigate fstate.maxrows
3675 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3676 let o =
3677 if ctrl
3678 then (
3679 G.postRedisplay "outline ctrl right";
3680 {< m_pan = m_pan + 1 >}
3682 else self#updownlevel 1
3684 coe o
3686 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3687 let o =
3688 if ctrl
3689 then (
3690 G.postRedisplay "outline ctrl left";
3691 {< m_pan = m_pan - 1 >}
3693 else self#updownlevel ~-1
3695 coe o
3697 | 0xff50 | 0xff95 -> (* (kp) home *)
3698 G.postRedisplay "outline home";
3699 coe {< m_first = 0; m_active = 0 >}
3701 | 0xff57 | 0xff9c -> (* (kp) end *)
3702 let active = source#getitemcount - 1 in
3703 let first = max 0 (active - fstate.maxrows) in
3704 G.postRedisplay "outline end";
3705 coe {< m_active = active; m_first = first >}
3707 | _ -> super#key key mask
3710 let outlinesource usebookmarks =
3711 let empty = [||] in
3712 (object
3713 inherit lvsourcebase
3714 val mutable m_items = empty
3715 val mutable m_orig_items = empty
3716 val mutable m_prev_items = empty
3717 val mutable m_narrow_pattern = ""
3718 val mutable m_hadremovals = false
3720 method getitemcount =
3721 Array.length m_items + (if m_hadremovals then 1 else 0)
3723 method getitem n =
3724 if n == Array.length m_items && m_hadremovals
3725 then
3726 ("[Confirm removal]", 0)
3727 else
3728 let s, n, _ = m_items.(n) in
3729 (s, n)
3731 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3732 ignore (uioh, first, qsearch);
3733 let confrimremoval = m_hadremovals && active = Array.length m_items in
3734 let items =
3735 if String.length m_narrow_pattern = 0
3736 then m_orig_items
3737 else m_items
3739 if not cancel
3740 then (
3741 if not confrimremoval
3742 then(
3743 let _, _, anchor = m_items.(active) in
3744 gotoghyll (getanchory anchor);
3745 m_items <- items;
3747 else (
3748 state.bookmarks <- Array.to_list m_items;
3749 m_orig_items <- m_items;
3752 else m_items <- items;
3753 m_pan <- pan;
3754 None
3756 method hasaction _ = true
3758 method greetmsg =
3759 if Array.length m_items != Array.length m_orig_items
3760 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3761 else ""
3763 method narrow pattern =
3764 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3765 match reopt with
3766 | None -> ()
3767 | Some re ->
3768 let rec loop accu n =
3769 if n = -1
3770 then (
3771 m_narrow_pattern <- pattern;
3772 m_items <- Array.of_list accu
3774 else
3775 let (s, _, _) as o = m_items.(n) in
3776 let accu =
3777 if (try ignore (Str.search_forward re s 0); true
3778 with Not_found -> false)
3779 then o :: accu
3780 else accu
3782 loop accu (n-1)
3784 loop [] (Array.length m_items - 1)
3786 method denarrow =
3787 m_orig_items <- (
3788 if usebookmarks
3789 then Array.of_list state.bookmarks
3790 else state.outlines
3792 m_items <- m_orig_items
3794 method remove m =
3795 if usebookmarks
3796 then
3797 if m >= 0 && m < Array.length m_items
3798 then (
3799 m_hadremovals <- true;
3800 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3801 let n = if n >= m then n+1 else n in
3802 m_items.(n)
3806 method reset anchor items =
3807 m_hadremovals <- false;
3808 if m_orig_items == empty || m_prev_items != items
3809 then (
3810 m_orig_items <- items;
3811 if String.length m_narrow_pattern = 0
3812 then m_items <- items;
3814 m_prev_items <- items;
3815 let rely = getanchory anchor in
3816 let active =
3817 let rec loop n best bestd =
3818 if n = Array.length m_items
3819 then best
3820 else
3821 let (_, _, anchor) = m_items.(n) in
3822 let orely = getanchory anchor in
3823 let d = abs (orely - rely) in
3824 if d < bestd
3825 then loop (n+1) n d
3826 else loop (n+1) best bestd
3828 loop 0 ~-1 max_int
3830 m_active <- active;
3831 m_first <- firstof m_first active
3832 end)
3835 let enterselector usebookmarks =
3836 let source = outlinesource usebookmarks in
3837 fun errmsg ->
3838 let outlines =
3839 if usebookmarks
3840 then Array.of_list state.bookmarks
3841 else state.outlines
3843 if Array.length outlines = 0
3844 then (
3845 showtext ' ' errmsg;
3847 else (
3848 state.text <- source#greetmsg;
3849 Wsi.setcursor Wsi.CURSOR_INHERIT;
3850 let anchor = getanchor () in
3851 source#reset anchor outlines;
3852 state.uioh <- coe (new outlinelistview ~source);
3853 G.postRedisplay "enter selector";
3857 let enteroutlinemode =
3858 let f = enterselector false in
3859 fun ()-> f "Document has no outline";
3862 let enterbookmarkmode =
3863 let f = enterselector true in
3864 fun () -> f "Document has no bookmarks (yet)";
3867 let color_of_string s =
3868 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3869 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3873 let color_to_string (r, g, b) =
3874 let r = truncate (r *. 256.0)
3875 and g = truncate (g *. 256.0)
3876 and b = truncate (b *. 256.0) in
3877 Printf.sprintf "%d/%d/%d" r g b
3880 let irect_of_string s =
3881 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3884 let irect_to_string (x0,y0,x1,y1) =
3885 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3888 let makecheckers () =
3889 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3890 following to say:
3891 converted by Issac Trotts. July 25, 2002 *)
3892 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3893 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3894 let id = GlTex.gen_texture () in
3895 GlTex.bind_texture `texture_2d id;
3896 GlPix.store (`unpack_alignment 1);
3897 GlTex.image2d image;
3898 List.iter (GlTex.parameter ~target:`texture_2d)
3899 [ `mag_filter `nearest; `min_filter `nearest ];
3903 let setcheckers enabled =
3904 match state.texid with
3905 | None ->
3906 if enabled then state.texid <- Some (makecheckers ())
3908 | Some texid ->
3909 if not enabled
3910 then (
3911 GlTex.delete_texture texid;
3912 state.texid <- None;
3916 let int_of_string_with_suffix s =
3917 let l = String.length s in
3918 let s1, shift =
3919 if l > 1
3920 then
3921 let suffix = Char.lowercase s.[l-1] in
3922 match suffix with
3923 | 'k' -> String.sub s 0 (l-1), 10
3924 | 'm' -> String.sub s 0 (l-1), 20
3925 | 'g' -> String.sub s 0 (l-1), 30
3926 | _ -> s, 0
3927 else s, 0
3929 let n = int_of_string s1 in
3930 let m = n lsl shift in
3931 if m < 0 || m < n
3932 then raise (Failure "value too large")
3933 else m
3936 let string_with_suffix_of_int n =
3937 if n = 0
3938 then "0"
3939 else
3940 let n, s =
3941 if n land ((1 lsl 30) - 1) = 0
3942 then n lsr 30, "G"
3943 else (
3944 if n land ((1 lsl 20) - 1) = 0
3945 then n lsr 20, "M"
3946 else (
3947 if n land ((1 lsl 10) - 1) = 0
3948 then n lsr 10, "K"
3949 else n, ""
3953 let rec loop s n =
3954 let h = n mod 1000 in
3955 let n = n / 1000 in
3956 if n = 0
3957 then string_of_int h ^ s
3958 else (
3959 let s = Printf.sprintf "_%03d%s" h s in
3960 loop s n
3963 loop "" n ^ s;
3966 let defghyllscroll = (40, 8, 32);;
3967 let ghyllscroll_of_string s =
3968 let (n, a, b) as nab =
3969 if s = "default"
3970 then defghyllscroll
3971 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3973 if n <= a || n <= b || a >= b
3974 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3975 nab;
3978 let ghyllscroll_to_string ((n, a, b) as nab) =
3979 if nab = defghyllscroll
3980 then "default"
3981 else Printf.sprintf "%d,%d,%d" n a b;
3984 let describe_location () =
3985 let f (fn, _) l =
3986 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3988 let fn, ln = List.fold_left f (-1, -1) state.layout in
3989 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3990 let percent =
3991 if maxy <= 0
3992 then 100.
3993 else (100. *. (float state.y /. float maxy))
3995 if fn = ln
3996 then
3997 Printf.sprintf "page %d of %d [%.2f%%]"
3998 (fn+1) state.pagecount percent
3999 else
4000 Printf.sprintf
4001 "pages %d-%d of %d [%.2f%%]"
4002 (fn+1) (ln+1) state.pagecount percent
4005 let setpresentationmode v =
4006 let (n, _, _) = getanchor () in
4007 let _, h = getpageyh n in
4008 let ips = if conf.presentation then calcips h else conf.interpagespace in
4009 state.anchor <- (n, 0.0, float ips);
4010 conf.presentation <- v;
4011 if conf.presentation
4012 then (
4013 if not conf.scrollbarinpm
4014 then state.scrollw <- 0;
4016 else state.scrollw <- conf.scrollbw;
4017 represent ();
4020 let enterinfomode =
4021 let btos b = if b then "\xe2\x88\x9a" else "" in
4022 let showextended = ref false in
4023 let leave mode = function
4024 | Confirm -> state.mode <- mode
4025 | Cancel -> state.mode <- mode in
4026 let src =
4027 (object
4028 val mutable m_first_time = true
4029 val mutable m_l = []
4030 val mutable m_a = [||]
4031 val mutable m_prev_uioh = nouioh
4032 val mutable m_prev_mode = View
4034 inherit lvsourcebase
4036 method reset prev_mode prev_uioh =
4037 m_a <- Array.of_list (List.rev m_l);
4038 m_l <- [];
4039 m_prev_mode <- prev_mode;
4040 m_prev_uioh <- prev_uioh;
4041 if m_first_time
4042 then (
4043 let rec loop n =
4044 if n >= Array.length m_a
4045 then ()
4046 else
4047 match m_a.(n) with
4048 | _, _, _, Action _ -> m_active <- n
4049 | _ -> loop (n+1)
4051 loop 0;
4052 m_first_time <- false;
4055 method int name get set =
4056 m_l <-
4057 (name, `int get, 1, Action (
4058 fun u ->
4059 let ondone s =
4060 try set (int_of_string s)
4061 with exn ->
4062 state.text <- Printf.sprintf "bad integer `%s': %s"
4063 s (Printexc.to_string exn)
4065 state.text <- "";
4066 let te = name ^ ": ", "", None, intentry, ondone, true in
4067 state.mode <- Textentry (te, leave m_prev_mode);
4069 )) :: m_l
4071 method int_with_suffix name get set =
4072 m_l <-
4073 (name, `intws get, 1, Action (
4074 fun u ->
4075 let ondone s =
4076 try set (int_of_string_with_suffix s)
4077 with exn ->
4078 state.text <- Printf.sprintf "bad integer `%s': %s"
4079 s (Printexc.to_string exn)
4081 state.text <- "";
4082 let te =
4083 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4085 state.mode <- Textentry (te, leave m_prev_mode);
4087 )) :: m_l
4089 method bool ?(offset=1) ?(btos=btos) name get set =
4090 m_l <-
4091 (name, `bool (btos, get), offset, Action (
4092 fun u ->
4093 let v = get () in
4094 set (not v);
4096 )) :: m_l
4098 method color name get set =
4099 m_l <-
4100 (name, `color get, 1, Action (
4101 fun u ->
4102 let invalid = (nan, nan, nan) in
4103 let ondone s =
4104 let c =
4105 try color_of_string s
4106 with exn ->
4107 state.text <- Printf.sprintf "bad color `%s': %s"
4108 s (Printexc.to_string exn);
4109 invalid
4111 if c <> invalid
4112 then set c;
4114 let te = name ^ ": ", "", None, textentry, ondone, true in
4115 state.text <- color_to_string (get ());
4116 state.mode <- Textentry (te, leave m_prev_mode);
4118 )) :: m_l
4120 method string name get set =
4121 m_l <-
4122 (name, `string get, 1, Action (
4123 fun u ->
4124 let ondone s = set s in
4125 let te = name ^ ": ", "", None, textentry, ondone, true in
4126 state.mode <- Textentry (te, leave m_prev_mode);
4128 )) :: m_l
4130 method colorspace name get set =
4131 m_l <-
4132 (name, `string get, 1, Action (
4133 fun _ ->
4134 let source =
4135 let vals = [| "rgb"; "bgr"; "gray" |] in
4136 (object
4137 inherit lvsourcebase
4139 initializer
4140 m_active <- int_of_colorspace conf.colorspace;
4141 m_first <- 0;
4143 method getitemcount = Array.length vals
4144 method getitem n = (vals.(n), 0)
4145 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4146 ignore (uioh, first, pan, qsearch);
4147 if not cancel then set active;
4148 None
4149 method hasaction _ = true
4150 end)
4152 state.text <- "";
4153 let modehash = findkeyhash conf "info" in
4154 coe (new listview ~source ~trusted:true ~modehash)
4155 )) :: m_l
4157 method caption s offset =
4158 m_l <- (s, `empty, offset, Noaction) :: m_l
4160 method caption2 s f offset =
4161 m_l <- (s, `string f, offset, Noaction) :: m_l
4163 method getitemcount = Array.length m_a
4165 method getitem n =
4166 let tostr = function
4167 | `int f -> string_of_int (f ())
4168 | `intws f -> string_with_suffix_of_int (f ())
4169 | `string f -> f ()
4170 | `color f -> color_to_string (f ())
4171 | `bool (btos, f) -> btos (f ())
4172 | `empty -> ""
4174 let name, t, offset, _ = m_a.(n) in
4175 ((let s = tostr t in
4176 if String.length s > 0
4177 then Printf.sprintf "%s\t%s" name s
4178 else name),
4179 offset)
4181 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4182 let uiohopt =
4183 if not cancel
4184 then (
4185 m_qsearch <- qsearch;
4186 let uioh =
4187 match m_a.(active) with
4188 | _, _, _, Action f -> f uioh
4189 | _ -> uioh
4191 Some uioh
4193 else None
4195 m_active <- active;
4196 m_first <- first;
4197 m_pan <- pan;
4198 uiohopt
4200 method hasaction n =
4201 match m_a.(n) with
4202 | _, _, _, Action _ -> true
4203 | _ -> false
4204 end)
4206 let rec fillsrc prevmode prevuioh =
4207 let sep () = src#caption "" 0 in
4208 let colorp name get set =
4209 src#string name
4210 (fun () -> color_to_string (get ()))
4211 (fun v ->
4213 let c = color_of_string v in
4214 set c
4215 with exn ->
4216 state.text <- Printf.sprintf "bad color `%s': %s"
4217 v (Printexc.to_string exn);
4220 let oldmode = state.mode in
4221 let birdseye = isbirdseye state.mode in
4223 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4225 src#bool "presentation mode"
4226 (fun () -> conf.presentation)
4227 (fun v -> setpresentationmode v);
4229 src#bool "ignore case in searches"
4230 (fun () -> conf.icase)
4231 (fun v -> conf.icase <- v);
4233 src#bool "preload"
4234 (fun () -> conf.preload)
4235 (fun v -> conf.preload <- v);
4237 src#bool "highlight links"
4238 (fun () -> conf.hlinks)
4239 (fun v -> conf.hlinks <- v);
4241 src#bool "under info"
4242 (fun () -> conf.underinfo)
4243 (fun v -> conf.underinfo <- v);
4245 src#bool "persistent bookmarks"
4246 (fun () -> conf.savebmarks)
4247 (fun v -> conf.savebmarks <- v);
4249 src#bool "proportional display"
4250 (fun () -> conf.proportional)
4251 (fun v -> reqlayout conf.angle v);
4253 src#bool "trim margins"
4254 (fun () -> conf.trimmargins)
4255 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4257 src#bool "persistent location"
4258 (fun () -> conf.jumpback)
4259 (fun v -> conf.jumpback <- v);
4261 sep ();
4262 src#int "inter-page space"
4263 (fun () -> conf.interpagespace)
4264 (fun n ->
4265 conf.interpagespace <- n;
4266 docolumns conf.columns;
4267 let pageno, py =
4268 match state.layout with
4269 | [] -> 0, 0
4270 | l :: _ ->
4271 l.pageno, l.pagey
4273 state.maxy <- calcheight ();
4274 let y = getpagey pageno in
4275 gotoy (y + py)
4278 src#int "page bias"
4279 (fun () -> conf.pagebias)
4280 (fun v -> conf.pagebias <- v);
4282 src#int "scroll step"
4283 (fun () -> conf.scrollstep)
4284 (fun n -> conf.scrollstep <- n);
4286 src#int "horizontal scroll step"
4287 (fun () -> conf.hscrollstep)
4288 (fun v -> conf.hscrollstep <- v);
4290 src#int "auto scroll step"
4291 (fun () ->
4292 match state.autoscroll with
4293 | Some step -> step
4294 | _ -> conf.autoscrollstep)
4295 (fun n ->
4296 if state.autoscroll <> None
4297 then state.autoscroll <- Some n;
4298 conf.autoscrollstep <- n);
4300 src#int "zoom"
4301 (fun () -> truncate (conf.zoom *. 100.))
4302 (fun v -> setzoom ((float v) /. 100.));
4304 src#int "rotation"
4305 (fun () -> conf.angle)
4306 (fun v -> reqlayout v conf.proportional);
4308 src#int "scroll bar width"
4309 (fun () -> state.scrollw)
4310 (fun v ->
4311 state.scrollw <- v;
4312 conf.scrollbw <- v;
4313 reshape conf.winw conf.winh;
4316 src#int "scroll handle height"
4317 (fun () -> conf.scrollh)
4318 (fun v -> conf.scrollh <- v;);
4320 src#int "thumbnail width"
4321 (fun () -> conf.thumbw)
4322 (fun v ->
4323 conf.thumbw <- min 4096 v;
4324 match oldmode with
4325 | Birdseye beye ->
4326 leavebirdseye beye false;
4327 enterbirdseye ()
4328 | _ -> ()
4331 let mode = state.mode in
4332 src#string "columns"
4333 (fun () ->
4334 match conf.columns with
4335 | Csingle _ -> "1"
4336 | Cmulti (multi, _) -> multicolumns_to_string multi
4337 | Csplit (count, _) -> "-" ^ string_of_int count
4339 (fun v ->
4340 let n, a, b = multicolumns_of_string v in
4341 setcolumns mode n a b);
4343 sep ();
4344 src#caption "Presentation mode" 0;
4345 src#bool "scrollbar visible"
4346 (fun () -> conf.scrollbarinpm)
4347 (fun v ->
4348 if v != conf.scrollbarinpm
4349 then (
4350 conf.scrollbarinpm <- v;
4351 if conf.presentation
4352 then (
4353 state.scrollw <- if v then conf.scrollbw else 0;
4354 reshape conf.winw conf.winh;
4359 sep ();
4360 src#caption "Pixmap cache" 0;
4361 src#int_with_suffix "size (advisory)"
4362 (fun () -> conf.memlimit)
4363 (fun v -> conf.memlimit <- v);
4365 src#caption2 "used"
4366 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4367 (string_with_suffix_of_int state.memused)
4368 (Hashtbl.length state.tilemap)) 1;
4370 sep ();
4371 src#caption "Layout" 0;
4372 src#caption2 "Dimension"
4373 (fun () ->
4374 Printf.sprintf "%dx%d (virtual %dx%d)"
4375 conf.winw conf.winh
4376 state.w state.maxy)
4378 if conf.debug
4379 then
4380 src#caption2 "Position" (fun () ->
4381 Printf.sprintf "%dx%d" state.x state.y
4383 else
4384 src#caption2 "Visible" (fun () -> describe_location ()) 1
4387 sep ();
4388 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4389 "Save these parameters as global defaults at exit"
4390 (fun () -> conf.bedefault)
4391 (fun v -> conf.bedefault <- v)
4394 sep ();
4395 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4396 src#bool ~offset:0 ~btos "Extended parameters"
4397 (fun () -> !showextended)
4398 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4399 if !showextended
4400 then (
4401 src#bool "checkers"
4402 (fun () -> conf.checkers)
4403 (fun v -> conf.checkers <- v; setcheckers v);
4404 src#bool "update cursor"
4405 (fun () -> conf.updatecurs)
4406 (fun v -> conf.updatecurs <- v);
4407 src#bool "verbose"
4408 (fun () -> conf.verbose)
4409 (fun v -> conf.verbose <- v);
4410 src#bool "invert colors"
4411 (fun () -> conf.invert)
4412 (fun v -> conf.invert <- v);
4413 src#bool "max fit"
4414 (fun () -> conf.maxhfit)
4415 (fun v -> conf.maxhfit <- v);
4416 src#bool "redirect stderr"
4417 (fun () -> conf.redirectstderr)
4418 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4419 src#string "uri launcher"
4420 (fun () -> conf.urilauncher)
4421 (fun v -> conf.urilauncher <- v);
4422 src#string "path launcher"
4423 (fun () -> conf.pathlauncher)
4424 (fun v -> conf.pathlauncher <- v);
4425 src#string "tile size"
4426 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4427 (fun v ->
4429 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4430 conf.tilew <- max 64 w;
4431 conf.tileh <- max 64 h;
4432 flushtiles ();
4433 with exn ->
4434 state.text <- Printf.sprintf "bad tile size `%s': %s"
4435 v (Printexc.to_string exn));
4436 src#int "texture count"
4437 (fun () -> conf.texcount)
4438 (fun v ->
4439 if realloctexts v
4440 then conf.texcount <- v
4441 else showtext '!' " Failed to set texture count please retry later"
4443 src#int "slice height"
4444 (fun () -> conf.sliceheight)
4445 (fun v ->
4446 conf.sliceheight <- v;
4447 wcmd "sliceh %d" conf.sliceheight;
4449 src#int "anti-aliasing level"
4450 (fun () -> conf.aalevel)
4451 (fun v ->
4452 conf.aalevel <- bound v 0 8;
4453 state.anchor <- getanchor ();
4454 opendoc state.path state.password;
4456 src#string "page scroll scaling factor"
4457 (fun () -> string_of_float conf.pgscale)
4458 (fun v ->
4460 let s = float_of_string v in
4461 conf.pgscale <- s
4462 with exn ->
4463 state.text <- Printf.sprintf
4464 "bad page scroll scaling factor `%s': %s"
4465 v (Printexc.to_string exn)
4468 src#int "ui font size"
4469 (fun () -> fstate.fontsize)
4470 (fun v -> setfontsize (bound v 5 100));
4471 src#int "hint font size"
4472 (fun () -> conf.hfsize)
4473 (fun v -> conf.hfsize <- bound v 5 100);
4474 colorp "background color"
4475 (fun () -> conf.bgcolor)
4476 (fun v -> conf.bgcolor <- v);
4477 src#bool "crop hack"
4478 (fun () -> conf.crophack)
4479 (fun v -> conf.crophack <- v);
4480 src#string "trim fuzz"
4481 (fun () -> irect_to_string conf.trimfuzz)
4482 (fun v ->
4484 conf.trimfuzz <- irect_of_string v;
4485 if conf.trimmargins
4486 then settrim true conf.trimfuzz;
4487 with exn ->
4488 state.text <- Printf.sprintf "bad irect `%s': %s"
4489 v (Printexc.to_string exn)
4491 src#string "throttle"
4492 (fun () ->
4493 match conf.maxwait with
4494 | None -> "show place holder if page is not ready"
4495 | Some time ->
4496 if time = infinity
4497 then "wait for page to fully render"
4498 else
4499 "wait " ^ string_of_float time
4500 ^ " seconds before showing placeholder"
4502 (fun v ->
4504 let f = float_of_string v in
4505 if f <= 0.0
4506 then conf.maxwait <- None
4507 else conf.maxwait <- Some f
4508 with exn ->
4509 state.text <- Printf.sprintf "bad time `%s': %s"
4510 v (Printexc.to_string exn)
4512 src#string "ghyll scroll"
4513 (fun () ->
4514 match conf.ghyllscroll with
4515 | None -> ""
4516 | Some nab -> ghyllscroll_to_string nab
4518 (fun v ->
4520 let gs =
4521 if String.length v = 0
4522 then None
4523 else Some (ghyllscroll_of_string v)
4525 conf.ghyllscroll <- gs
4526 with exn ->
4527 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4528 v (Printexc.to_string exn)
4530 src#string "selection command"
4531 (fun () -> conf.selcmd)
4532 (fun v -> conf.selcmd <- v);
4533 src#colorspace "color space"
4534 (fun () -> colorspace_to_string conf.colorspace)
4535 (fun v ->
4536 conf.colorspace <- colorspace_of_int v;
4537 wcmd "cs %d" v;
4538 load state.layout;
4540 if pbousable ()
4541 then
4542 src#bool "use PBO"
4543 (fun () -> conf.usepbo)
4544 (fun v -> conf.usepbo <- v);
4545 src#bool "mouse wheel scrolls pages"
4546 (fun () -> conf.wheelbypage)
4547 (fun v -> conf.wheelbypage <- v);
4550 sep ();
4551 src#caption "Document" 0;
4552 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4553 src#caption2 "Pages"
4554 (fun () -> string_of_int state.pagecount) 1;
4555 src#caption2 "Dimensions"
4556 (fun () -> string_of_int (List.length state.pdims)) 1;
4557 if conf.trimmargins
4558 then (
4559 sep ();
4560 src#caption "Trimmed margins" 0;
4561 src#caption2 "Dimensions"
4562 (fun () -> string_of_int (List.length state.pdims)) 1;
4565 sep ();
4566 src#caption "OpenGL" 0;
4567 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4568 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4569 src#reset prevmode prevuioh;
4571 fun () ->
4572 state.text <- "";
4573 let prevmode = state.mode
4574 and prevuioh = state.uioh in
4575 fillsrc prevmode prevuioh;
4576 let source = (src :> lvsource) in
4577 let modehash = findkeyhash conf "info" in
4578 state.uioh <- coe (object (self)
4579 inherit listview ~source ~trusted:true ~modehash as super
4580 val mutable m_prevmemused = 0
4581 method infochanged = function
4582 | Memused ->
4583 if m_prevmemused != state.memused
4584 then (
4585 m_prevmemused <- state.memused;
4586 G.postRedisplay "memusedchanged";
4588 | Pdim -> G.postRedisplay "pdimchanged"
4589 | Docinfo -> fillsrc prevmode prevuioh
4591 method key key mask =
4592 if not (Wsi.withctrl mask)
4593 then
4594 match key with
4595 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4596 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4597 | _ -> super#key key mask
4598 else super#key key mask
4599 end);
4600 G.postRedisplay "info";
4603 let enterhelpmode =
4604 let source =
4605 (object
4606 inherit lvsourcebase
4607 method getitemcount = Array.length state.help
4608 method getitem n =
4609 let s, l, _ = state.help.(n) in
4610 (s, l)
4612 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4613 let optuioh =
4614 if not cancel
4615 then (
4616 m_qsearch <- qsearch;
4617 match state.help.(active) with
4618 | _, _, Action f -> Some (f uioh)
4619 | _ -> Some (uioh)
4621 else None
4623 m_active <- active;
4624 m_first <- first;
4625 m_pan <- pan;
4626 optuioh
4628 method hasaction n =
4629 match state.help.(n) with
4630 | _, _, Action _ -> true
4631 | _ -> false
4633 initializer
4634 m_active <- -1
4635 end)
4636 in fun () ->
4637 let modehash = findkeyhash conf "help" in
4638 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4639 G.postRedisplay "help";
4642 let entermsgsmode =
4643 let msgsource =
4644 let re = Str.regexp "[\r\n]" in
4645 (object
4646 inherit lvsourcebase
4647 val mutable m_items = [||]
4649 method getitemcount = 1 + Array.length m_items
4651 method getitem n =
4652 if n = 0
4653 then "[Clear]", 0
4654 else m_items.(n-1), 0
4656 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4657 ignore uioh;
4658 if not cancel
4659 then (
4660 if active = 0
4661 then Buffer.clear state.errmsgs;
4662 m_qsearch <- qsearch;
4664 m_active <- active;
4665 m_first <- first;
4666 m_pan <- pan;
4667 None
4669 method hasaction n =
4670 n = 0
4672 method reset =
4673 state.newerrmsgs <- false;
4674 let l = Str.split re (Buffer.contents state.errmsgs) in
4675 m_items <- Array.of_list l
4677 initializer
4678 m_active <- 0
4679 end)
4680 in fun () ->
4681 state.text <- "";
4682 msgsource#reset;
4683 let source = (msgsource :> lvsource) in
4684 let modehash = findkeyhash conf "listview" in
4685 state.uioh <- coe (object
4686 inherit listview ~source ~trusted:false ~modehash as super
4687 method display =
4688 if state.newerrmsgs
4689 then msgsource#reset;
4690 super#display
4691 end);
4692 G.postRedisplay "msgs";
4695 let quickbookmark ?title () =
4696 match state.layout with
4697 | [] -> ()
4698 | l :: _ ->
4699 let title =
4700 match title with
4701 | None ->
4702 let sec = Unix.gettimeofday () in
4703 let tm = Unix.localtime sec in
4704 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4705 (l.pageno+1)
4706 tm.Unix.tm_mday
4707 tm.Unix.tm_mon
4708 (tm.Unix.tm_year + 1900)
4709 tm.Unix.tm_hour
4710 tm.Unix.tm_min
4711 | Some title -> title
4713 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4716 let doreshape w h =
4717 state.fullscreen <- None;
4718 Wsi.reshape w h;
4721 let setautoscrollspeed step goingdown =
4722 let incr = max 1 ((abs step) / 2) in
4723 let incr = if goingdown then incr else -incr in
4724 let astep = step + incr in
4725 state.autoscroll <- Some astep;
4728 let gotounder = function
4729 | Ulinkgoto (pageno, top) ->
4730 if pageno >= 0
4731 then (
4732 addnav ();
4733 gotopage1 pageno top;
4736 | Ulinkuri s ->
4737 gotouri s
4739 | Uremote (filename, pageno) ->
4740 let path =
4741 if Sys.file_exists filename
4742 then filename
4743 else
4744 let dir = Filename.dirname state.path in
4745 let path = Filename.concat dir filename in
4746 if Sys.file_exists path
4747 then path
4748 else ""
4750 if String.length path > 0
4751 then (
4752 let anchor = getanchor () in
4753 let ranchor = state.path, state.password, anchor in
4754 state.anchor <- (pageno, 0.0, 0.0);
4755 state.ranchors <- ranchor :: state.ranchors;
4756 opendoc path "";
4758 else showtext '!' ("Could not find " ^ filename)
4760 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4763 let canpan () =
4764 match conf.columns with
4765 | Csplit _ -> true
4766 | _ -> conf.zoom > 1.0
4769 let existsinrow pageno (columns, coverA, coverB) p =
4770 let last = ((pageno - coverA) mod columns) + columns in
4771 let rec any = function
4772 | [] -> false
4773 | l :: rest ->
4774 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4775 then p l
4776 else (
4777 if not (p l)
4778 then (if l.pageno = last then false else any rest)
4779 else true
4782 any state.layout
4785 let nextpage () =
4786 match state.layout with
4787 | [] -> ()
4788 | l :: rest ->
4789 match conf.columns with
4790 | Csingle _ ->
4791 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4792 then
4793 let y = clamp (pgscale conf.winh) in
4794 gotoghyll y
4795 else
4796 let pageno = min (l.pageno+1) (state.pagecount-1) in
4797 gotoghyll (getpagey pageno)
4798 | Cmulti ((c, _, _) as cl, _) ->
4799 if conf.presentation
4800 && (existsinrow l.pageno cl
4801 (fun l -> l.pageh > l.pagey + l.pagevh))
4802 then
4803 let y = clamp (pgscale conf.winh) in
4804 gotoghyll y
4805 else
4806 let pageno = min (l.pageno+c) (state.pagecount-1) in
4807 gotoghyll (getpagey pageno)
4808 | Csplit (n, _) ->
4809 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4810 then
4811 let pagey, pageh = getpageyh l.pageno in
4812 let pagey = pagey + pageh * l.pagecol in
4813 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4814 gotoghyll (pagey + pageh + ips)
4817 let prevpage () =
4818 match state.layout with
4819 | [] -> ()
4820 | l :: _ ->
4821 match conf.columns with
4822 | Csingle _ ->
4823 if conf.presentation && l.pagey != 0
4824 then
4825 gotoghyll (clamp (pgscale ~-(conf.winh)))
4826 else
4827 let pageno = max 0 (l.pageno-1) in
4828 gotoghyll (getpagey pageno)
4829 | Cmulti ((c, _, coverB) as cl, _) ->
4830 if conf.presentation &&
4831 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4832 then
4833 gotoghyll (clamp (pgscale ~-(conf.winh)))
4834 else
4835 let decr =
4836 if l.pageno = state.pagecount - coverB
4837 then 1
4838 else c
4840 let pageno = max 0 (l.pageno-decr) in
4841 gotoghyll (getpagey pageno)
4842 | Csplit (n, _) ->
4843 let y =
4844 if l.pagecol = 0
4845 then
4846 if l.pageno = 0
4847 then l.pagey
4848 else
4849 let pageno = max 0 (l.pageno-1) in
4850 let pagey, pageh = getpageyh pageno in
4851 pagey + (n-1)*pageh
4852 else
4853 let pagey, pageh = getpageyh l.pageno in
4854 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4856 gotoghyll y
4859 let viewkeyboard key mask =
4860 let enttext te =
4861 let mode = state.mode in
4862 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4863 state.text <- "";
4864 enttext ();
4865 G.postRedisplay "view:enttext"
4867 let ctrl = Wsi.withctrl mask in
4868 let key =
4869 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4871 match key with
4872 | 81 -> (* Q *)
4873 exit 0
4875 | 0xff63 -> (* insert *)
4876 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4877 then (
4878 state.mode <- LinkNav (Ltgendir 0);
4879 gotoy state.y;
4881 else showtext '!' "Keyboard link navigation does not work under rotation"
4883 | 0xff1b | 113 -> (* escape / q *)
4884 begin match state.mstate with
4885 | Mzoomrect _ ->
4886 state.mstate <- Mnone;
4887 Wsi.setcursor Wsi.CURSOR_INHERIT;
4888 G.postRedisplay "kill zoom rect";
4889 | _ ->
4890 begin match state.mode with
4891 | LinkNav _ ->
4892 state.mode <- View;
4893 G.postRedisplay "esc leave linknav"
4894 | _ ->
4895 match state.ranchors with
4896 | [] -> raise Quit
4897 | (path, password, anchor) :: rest ->
4898 state.ranchors <- rest;
4899 state.anchor <- anchor;
4900 opendoc path password
4901 end;
4902 end;
4904 | 0xff08 -> (* backspace *)
4905 gotoghyll (getnav ~-1)
4907 | 111 -> (* o *)
4908 enteroutlinemode ()
4910 | 117 -> (* u *)
4911 state.rects <- [];
4912 state.text <- "";
4913 G.postRedisplay "dehighlight";
4915 | 47 | 63 -> (* / ? *)
4916 let ondone isforw s =
4917 cbput state.hists.pat s;
4918 state.searchpattern <- s;
4919 search s isforw
4921 let s = String.create 1 in
4922 s.[0] <- Char.chr key;
4923 enttext (s, "", Some (onhist state.hists.pat),
4924 textentry, ondone (key = 47), true)
4926 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4927 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4928 setzoom (conf.zoom +. incr)
4930 | 43 | 0xffab -> (* + *)
4931 let ondone s =
4932 let n =
4933 try int_of_string s with exc ->
4934 state.text <- Printf.sprintf "bad integer `%s': %s"
4935 s (Printexc.to_string exc);
4936 max_int
4938 if n != max_int
4939 then (
4940 conf.pagebias <- n;
4941 state.text <- "page bias is now " ^ string_of_int n;
4944 enttext ("page bias: ", "", None, intentry, ondone, true)
4946 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4947 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4948 setzoom (max 0.01 (conf.zoom -. decr))
4950 | 45 | 0xffad -> (* - *)
4951 let ondone msg = state.text <- msg in
4952 enttext (
4953 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4954 optentry state.mode, ondone, true
4957 | 48 when ctrl -> (* ctrl-0 *)
4958 setzoom 1.0
4960 | 49 when ctrl -> (* ctrl-1 *)
4961 let cols =
4962 match conf.columns with
4963 | Csingle _ | Cmulti _ -> 1
4964 | Csplit (n, _) -> n
4966 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4967 if zoom < 1.0
4968 then setzoom zoom
4970 | 0xffc6 -> (* f9 *)
4971 togglebirdseye ()
4973 | 57 when ctrl -> (* ctrl-9 *)
4974 togglebirdseye ()
4976 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4977 when not ctrl -> (* 0..9 *)
4978 let ondone s =
4979 let n =
4980 try int_of_string s with exc ->
4981 state.text <- Printf.sprintf "bad integer `%s': %s"
4982 s (Printexc.to_string exc);
4985 if n >= 0
4986 then (
4987 addnav ();
4988 cbput state.hists.pag (string_of_int n);
4989 gotopage1 (n + conf.pagebias - 1) 0;
4992 let pageentry text key =
4993 match Char.unsafe_chr key with
4994 | 'g' -> TEdone text
4995 | _ -> intentry text key
4997 let text = "x" in text.[0] <- Char.chr key;
4998 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5000 | 98 -> (* b *)
5001 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5002 reshape conf.winw conf.winh;
5004 | 108 -> (* l *)
5005 conf.hlinks <- not conf.hlinks;
5006 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5007 G.postRedisplay "toggle highlightlinks";
5009 | 70 -> (* F *)
5010 state.glinks <- true;
5011 let mode = state.mode in
5012 state.mode <- Textentry (
5013 (":", "", None, linknentry, linkndone gotounder, false),
5014 (fun _ ->
5015 state.glinks <- false;
5016 state.mode <- mode)
5018 state.text <- "";
5019 G.postRedisplay "view:linkent(F)"
5021 | 121 -> (* y *)
5022 state.glinks <- true;
5023 let mode = state.mode in
5024 state.mode <- Textentry (
5025 (":", "", None, linknentry, linkndone (fun under ->
5026 match Ne.pipe () with
5027 | Ne.Exn exn ->
5028 showtext '!' (Printf.sprintf "pipe failed: %s"
5029 (Printexc.to_string exn));
5030 | Ne.Res (r, w) ->
5031 let popened =
5032 try popen conf.selcmd [r, 0; w, -1]; true
5033 with exn ->
5034 showtext '!'
5035 (Printf.sprintf "failed to execute %s: %s"
5036 conf.selcmd (Printexc.to_string exn));
5037 false
5039 let clo cap fd =
5040 Ne.clo fd (fun msg ->
5041 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5044 let s = undertext under in
5045 if popened
5046 then
5047 (try
5048 let l = String.length s in
5049 let n = tempfailureretry (Unix.write w s 0) l in
5050 if n != l
5051 then
5052 showtext '!'
5053 (Printf.sprintf
5054 "failed to write %d characters to sel pipe, wrote %d"
5057 with exn ->
5058 showtext '!'
5059 (Printf.sprintf "failed to write to sel pipe: %s"
5060 (Printexc.to_string exn)
5063 else dolog "%s" s;
5064 clo "pipe/r" r;
5065 clo "pipe/w" w;
5066 ), false
5068 fun _ ->
5069 state.glinks <- false;
5070 state.mode <- mode
5072 state.text <- "";
5073 G.postRedisplay "view:linkent"
5075 | 97 -> (* a *)
5076 begin match state.autoscroll with
5077 | Some step ->
5078 conf.autoscrollstep <- step;
5079 state.autoscroll <- None
5080 | None ->
5081 if conf.autoscrollstep = 0
5082 then state.autoscroll <- Some 1
5083 else state.autoscroll <- Some conf.autoscrollstep
5086 | 112 when ctrl -> (* ctrl-p *)
5087 launchpath ()
5089 | 80 -> (* P *)
5090 setpresentationmode (not conf.presentation);
5091 showtext ' ' ("presentation mode " ^
5092 if conf.presentation then "on" else "off");
5094 | 102 -> (* f *)
5095 begin match state.fullscreen with
5096 | None ->
5097 state.fullscreen <- Some (conf.winw, conf.winh);
5098 Wsi.fullscreen ()
5099 | Some (w, h) ->
5100 state.fullscreen <- None;
5101 doreshape w h
5104 | 112 | 78 -> (* p|N *)
5105 search state.searchpattern false
5107 | 110 | 0xffc0 -> (* n|F3 *)
5108 search state.searchpattern true
5110 | 116 -> (* t *)
5111 begin match state.layout with
5112 | [] -> ()
5113 | l :: _ ->
5114 gotoy_and_clear_text (getpagey l.pageno)
5117 | 32 -> (* space *)
5118 nextpage ()
5120 | 0xff9f | 0xffff -> (* delete *)
5121 prevpage ()
5123 | 61 -> (* = *)
5124 showtext ' ' (describe_location ());
5126 | 119 -> (* w *)
5127 begin match state.layout with
5128 | [] -> ()
5129 | l :: _ ->
5130 doreshape (l.pagew + state.scrollw) l.pageh;
5131 G.postRedisplay "w"
5134 | 39 -> (* ' *)
5135 enterbookmarkmode ()
5137 | 104 | 0xffbe -> (* h|F1 *)
5138 enterhelpmode ()
5140 | 105 -> (* i *)
5141 enterinfomode ()
5143 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5144 entermsgsmode ()
5146 | 109 -> (* m *)
5147 let ondone s =
5148 match state.layout with
5149 | l :: _ ->
5150 if String.length s > 0
5151 then
5152 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5153 | _ -> ()
5155 enttext ("bookmark: ", "", None, textentry, ondone, true)
5157 | 126 -> (* ~ *)
5158 quickbookmark ();
5159 showtext ' ' "Quick bookmark added";
5161 | 122 -> (* z *)
5162 begin match state.layout with
5163 | l :: _ ->
5164 let rect = getpdimrect l.pagedimno in
5165 let w, h =
5166 if conf.crophack
5167 then
5168 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5169 truncate (1.2 *. (rect.(3) -. rect.(0))))
5170 else
5171 (truncate (rect.(1) -. rect.(0)),
5172 truncate (rect.(3) -. rect.(0)))
5174 let w = truncate ((float w)*.conf.zoom)
5175 and h = truncate ((float h)*.conf.zoom) in
5176 if w != 0 && h != 0
5177 then (
5178 state.anchor <- getanchor ();
5179 doreshape (w + state.scrollw) (h + conf.interpagespace)
5181 G.postRedisplay "z";
5183 | [] -> ()
5186 | 50 when ctrl -> (* ctrl-2 *)
5187 let maxw = getmaxw () in
5188 if maxw > 0.0
5189 then setzoom (maxw /. float conf.winw)
5191 | 60 | 62 -> (* < > *)
5192 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5194 | 91 | 93 -> (* [ ] *)
5195 conf.colorscale <-
5196 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5198 G.postRedisplay "brightness";
5200 | 99 when state.mode = View -> (* c *)
5201 let (c, a, b), z =
5202 match state.prevcolumns with
5203 | None -> (1, 0, 0), 1.0
5204 | Some (columns, z) ->
5205 let cab =
5206 match columns with
5207 | Csplit (c, _) -> -c, 0, 0
5208 | Cmulti ((c, a, b), _) -> c, a, b
5209 | Csingle _ -> 1, 0, 0
5211 cab, z
5213 setcolumns View c a b;
5214 setzoom z;
5216 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5217 setzoom state.prevzoom
5219 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5220 begin match state.autoscroll with
5221 | None ->
5222 begin match state.mode with
5223 | Birdseye beye -> upbirdseye 1 beye
5224 | _ ->
5225 if ctrl
5226 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5227 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5229 | Some n ->
5230 setautoscrollspeed n false
5233 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5234 begin match state.autoscroll with
5235 | None ->
5236 begin match state.mode with
5237 | Birdseye beye -> downbirdseye 1 beye
5238 | _ ->
5239 if ctrl
5240 then gotoy_and_clear_text (clamp (conf.winh/2))
5241 else gotoy_and_clear_text (clamp conf.scrollstep)
5243 | Some n ->
5244 setautoscrollspeed n true
5247 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5248 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5249 if canpan ()
5250 then
5251 let dx =
5252 if ctrl
5253 then conf.winw / 2
5254 else conf.hscrollstep
5256 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5257 state.x <- state.x + dx;
5258 gotoy_and_clear_text state.y
5259 else (
5260 state.text <- "";
5261 G.postRedisplay "lef/right"
5264 | 0xff55 | 0xff9a -> (* (kp) prior *)
5265 let y =
5266 if ctrl
5267 then
5268 match state.layout with
5269 | [] -> state.y
5270 | l :: _ -> state.y - l.pagey
5271 else
5272 clamp (pgscale (-conf.winh))
5274 gotoghyll y
5276 | 0xff56 | 0xff9b -> (* (kp) next *)
5277 let y =
5278 if ctrl
5279 then
5280 match List.rev state.layout with
5281 | [] -> state.y
5282 | l :: _ -> getpagey l.pageno
5283 else
5284 clamp (pgscale conf.winh)
5286 gotoghyll y
5288 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5289 gotoghyll 0
5290 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5291 gotoghyll (clamp state.maxy)
5293 | 0xff53 | 0xff98
5294 when Wsi.withalt mask -> (* alt-(kp) right *)
5295 gotoghyll (getnav 1)
5296 | 0xff51 | 0xff96
5297 when Wsi.withalt mask -> (* alt-(kp) left *)
5298 gotoghyll (getnav ~-1)
5300 | 114 -> (* r *)
5301 reload ()
5303 | 118 when conf.debug -> (* v *)
5304 state.rects <- [];
5305 List.iter (fun l ->
5306 match getopaque l.pageno with
5307 | None -> ()
5308 | Some opaque ->
5309 let x0, y0, x1, y1 = pagebbox opaque in
5310 let a,b = float x0, float y0 in
5311 let c,d = float x1, float y0 in
5312 let e,f = float x1, float y1 in
5313 let h,j = float x0, float y1 in
5314 let rect = (a,b,c,d,e,f,h,j) in
5315 debugrect rect;
5316 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5317 ) state.layout;
5318 G.postRedisplay "v";
5320 | _ ->
5321 vlog "huh? %s" (Wsi.keyname key)
5324 let linknavkeyboard key mask linknav =
5325 let getpage pageno =
5326 let rec loop = function
5327 | [] -> None
5328 | l :: _ when l.pageno = pageno -> Some l
5329 | _ :: rest -> loop rest
5330 in loop state.layout
5332 let doexact (pageno, n) =
5333 match getopaque pageno, getpage pageno with
5334 | Some opaque, Some l ->
5335 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5336 then
5337 let under = getlink opaque n in
5338 G.postRedisplay "link gotounder";
5339 gotounder under;
5340 state.mode <- View;
5341 else
5342 let opt, dir =
5343 match key with
5344 | 0xff50 -> (* home *)
5345 Some (findlink opaque LDfirst), -1
5347 | 0xff57 -> (* end *)
5348 Some (findlink opaque LDlast), 1
5350 | 0xff51 -> (* left *)
5351 Some (findlink opaque (LDleft n)), -1
5353 | 0xff53 -> (* right *)
5354 Some (findlink opaque (LDright n)), 1
5356 | 0xff52 -> (* up *)
5357 Some (findlink opaque (LDup n)), -1
5359 | 0xff54 -> (* down *)
5360 Some (findlink opaque (LDdown n)), 1
5362 | _ -> None, 0
5364 let pwl l dir =
5365 begin match findpwl l.pageno dir with
5366 | Pwlnotfound -> ()
5367 | Pwl pageno ->
5368 let notfound dir =
5369 state.mode <- LinkNav (Ltgendir dir);
5370 let y, h = getpageyh pageno in
5371 let y =
5372 if dir < 0
5373 then y + h - conf.winh
5374 else y
5376 gotoy y
5378 begin match getopaque pageno, getpage pageno with
5379 | Some opaque, Some _ ->
5380 let link =
5381 let ld = if dir > 0 then LDfirst else LDlast in
5382 findlink opaque ld
5384 begin match link with
5385 | Lfound m ->
5386 showlinktype (getlink opaque m);
5387 state.mode <- LinkNav (Ltexact (pageno, m));
5388 G.postRedisplay "linknav jpage";
5389 | _ -> notfound dir
5390 end;
5391 | _ -> notfound dir
5392 end;
5393 end;
5395 begin match opt with
5396 | Some Lnotfound -> pwl l dir;
5397 | Some (Lfound m) ->
5398 if m = n
5399 then pwl l dir
5400 else (
5401 let _, y0, _, y1 = getlinkrect opaque m in
5402 if y0 < l.pagey
5403 then gotopage1 l.pageno y0
5404 else (
5405 let d = fstate.fontsize + 1 in
5406 if y1 - l.pagey > l.pagevh - d
5407 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5408 else G.postRedisplay "linknav";
5410 showlinktype (getlink opaque m);
5411 state.mode <- LinkNav (Ltexact (l.pageno, m));
5414 | None -> viewkeyboard key mask
5415 end;
5416 | _ -> viewkeyboard key mask
5418 if key = 0xff63
5419 then (
5420 state.mode <- View;
5421 G.postRedisplay "leave linknav"
5423 else
5424 match linknav with
5425 | Ltgendir _ -> viewkeyboard key mask
5426 | Ltexact exact -> doexact exact
5429 let keyboard key mask =
5430 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5431 then wcmd "interrupt"
5432 else state.uioh <- state.uioh#key key mask
5435 let birdseyekeyboard key mask
5436 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5437 let incr =
5438 match conf.columns with
5439 | Csingle _ -> 1
5440 | Cmulti ((c, _, _), _) -> c
5441 | Csplit _ -> failwith "bird's eye split mode"
5443 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5444 match key with
5445 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5446 let y, h = getpageyh pageno in
5447 let top = (conf.winh - h) / 2 in
5448 gotoy (max 0 (y - top))
5449 | 0xff0d (* enter *)
5450 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5451 | 0xff1b -> leavebirdseye beye true (* escape *)
5452 | 0xff52 -> upbirdseye incr beye (* up *)
5453 | 0xff54 -> downbirdseye incr beye (* down *)
5454 | 0xff51 -> upbirdseye 1 beye (* left *)
5455 | 0xff53 -> downbirdseye 1 beye (* right *)
5457 | 0xff55 -> (* prior *)
5458 begin match state.layout with
5459 | l :: _ ->
5460 if l.pagey != 0
5461 then (
5462 state.mode <- Birdseye (
5463 oconf, leftx, l.pageno, hooverpageno, anchor
5465 gotopage1 l.pageno 0;
5467 else (
5468 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5469 match layout with
5470 | [] -> gotoy (clamp (-conf.winh))
5471 | l :: _ ->
5472 state.mode <- Birdseye (
5473 oconf, leftx, l.pageno, hooverpageno, anchor
5475 gotopage1 l.pageno 0
5478 | [] -> gotoy (clamp (-conf.winh))
5479 end;
5481 | 0xff56 -> (* next *)
5482 begin match List.rev state.layout with
5483 | l :: _ ->
5484 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5485 begin match layout with
5486 | [] ->
5487 let incr = l.pageh - l.pagevh in
5488 if incr = 0
5489 then (
5490 state.mode <-
5491 Birdseye (
5492 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5494 G.postRedisplay "birdseye pagedown";
5496 else gotoy (clamp (incr + conf.interpagespace*2));
5498 | l :: _ ->
5499 state.mode <-
5500 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5501 gotopage1 l.pageno 0;
5504 | [] -> gotoy (clamp conf.winh)
5505 end;
5507 | 0xff50 -> (* home *)
5508 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5509 gotopage1 0 0
5511 | 0xff57 -> (* end *)
5512 let pageno = state.pagecount - 1 in
5513 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5514 if not (pagevisible state.layout pageno)
5515 then
5516 let h =
5517 match List.rev state.pdims with
5518 | [] -> conf.winh
5519 | (_, _, h, _) :: _ -> h
5521 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5522 else G.postRedisplay "birdseye end";
5523 | _ -> viewkeyboard key mask
5526 let drawpage l linkindexbase =
5527 let color =
5528 match state.mode with
5529 | Textentry _ -> scalecolor 0.4
5530 | LinkNav _
5531 | View -> scalecolor 1.0
5532 | Birdseye (_, _, pageno, hooverpageno, _) ->
5533 if l.pageno = hooverpageno
5534 then scalecolor 0.9
5535 else (
5536 if l.pageno = pageno
5537 then scalecolor 1.0
5538 else scalecolor 0.8
5541 drawtiles l color;
5542 begin match getopaque l.pageno with
5543 | Some opaque ->
5544 if tileready l l.pagex l.pagey
5545 then
5546 let x = l.pagedispx - l.pagex
5547 and y = l.pagedispy - l.pagey in
5548 let hlmask =
5549 match conf.columns with
5550 | Csingle _ | Cmulti _ ->
5551 (if conf.hlinks then 1 else 0)
5552 + (if state.glinks
5553 && not (isbirdseye state.mode) then 2 else 0)
5554 | _ -> 0
5556 let s =
5557 match state.mode with
5558 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5559 | _ -> ""
5561 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5562 else 0
5564 | _ -> 0
5565 end;
5568 let scrollindicator () =
5569 let sbw, ph, sh = state.uioh#scrollph in
5570 let sbh, pw, sw = state.uioh#scrollpw in
5572 GlDraw.color (0.64, 0.64, 0.64);
5573 GlDraw.rect
5574 (float (conf.winw - sbw), 0.)
5575 (float conf.winw, float conf.winh)
5577 GlDraw.rect
5578 (0., float (conf.winh - sbh))
5579 (float (conf.winw - state.scrollw - 1), float conf.winh)
5581 GlDraw.color (0.0, 0.0, 0.0);
5583 GlDraw.rect
5584 (float (conf.winw - sbw), ph)
5585 (float conf.winw, ph +. sh)
5587 GlDraw.rect
5588 (pw, float (conf.winh - sbh))
5589 (pw +. sw, float conf.winh)
5593 let showsel () =
5594 match state.mstate with
5595 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5598 | Msel ((x0, y0), (x1, y1)) ->
5599 let rec loop = function
5600 | l :: ls ->
5601 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5602 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5603 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5604 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5605 then
5606 match getopaque l.pageno with
5607 | Some opaque ->
5608 let x0, y0 = pagetranslatepoint l x0 y0 in
5609 let x1, y1 = pagetranslatepoint l x1 y1 in
5610 seltext opaque (x0, y0, x1, y1);
5611 | _ -> ()
5612 else loop ls
5613 | [] -> ()
5615 loop state.layout
5618 let showrects rects =
5619 Gl.enable `blend;
5620 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5621 GlDraw.polygon_mode `both `fill;
5622 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5623 List.iter
5624 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5625 List.iter (fun l ->
5626 if l.pageno = pageno
5627 then (
5628 let dx = float (l.pagedispx - l.pagex) in
5629 let dy = float (l.pagedispy - l.pagey) in
5630 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5631 GlDraw.begins `quads;
5633 GlDraw.vertex2 (x0+.dx, y0+.dy);
5634 GlDraw.vertex2 (x1+.dx, y1+.dy);
5635 GlDraw.vertex2 (x2+.dx, y2+.dy);
5636 GlDraw.vertex2 (x3+.dx, y3+.dy);
5638 GlDraw.ends ();
5640 ) state.layout
5641 ) rects
5643 Gl.disable `blend;
5646 let display () =
5647 GlClear.color (scalecolor2 conf.bgcolor);
5648 GlClear.clear [`color];
5649 let rec loop linkindexbase = function
5650 | l :: rest ->
5651 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5652 loop linkindexbase rest
5653 | [] -> ()
5655 loop 0 state.layout;
5656 let rects =
5657 match state.mode with
5658 | LinkNav (Ltexact (pageno, linkno)) ->
5659 begin match getopaque pageno with
5660 | Some opaque ->
5661 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5662 (pageno, 5, (
5663 float x0, float y0,
5664 float x1, float y0,
5665 float x1, float y1,
5666 float x0, float y1)
5667 ) :: state.rects
5668 | None -> state.rects
5670 | _ -> state.rects
5672 showrects rects;
5673 showsel ();
5674 state.uioh#display;
5675 begin match state.mstate with
5676 | Mzoomrect ((x0, y0), (x1, y1)) ->
5677 Gl.enable `blend;
5678 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5679 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5680 GlDraw.rect (float x0, float y0)
5681 (float x1, float y1);
5682 Gl.disable `blend;
5683 | _ -> ()
5684 end;
5685 enttext ();
5686 scrollindicator ();
5687 Wsi.swapb ();
5690 let zoomrect x y x1 y1 =
5691 let x0 = min x x1
5692 and x1 = max x x1
5693 and y0 = min y y1 in
5694 gotoy (state.y + y0);
5695 state.anchor <- getanchor ();
5696 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5697 let margin =
5698 if state.w < conf.winw - state.scrollw
5699 then (conf.winw - state.scrollw - state.w) / 2
5700 else 0
5702 state.x <- (state.x + margin) - x0;
5703 setzoom zoom;
5704 Wsi.setcursor Wsi.CURSOR_INHERIT;
5705 state.mstate <- Mnone;
5708 let scrollx x =
5709 let winw = conf.winw - state.scrollw - 1 in
5710 let s = float x /. float winw in
5711 let destx = truncate (float (state.w + winw) *. s) in
5712 state.x <- winw - destx;
5713 gotoy_and_clear_text state.y;
5714 state.mstate <- Mscrollx;
5717 let scrolly y =
5718 let s = float y /. float conf.winh in
5719 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5720 gotoy_and_clear_text desty;
5721 state.mstate <- Mscrolly;
5724 let viewmouse button down x y mask =
5725 match button with
5726 | n when (n == 4 || n == 5) && not down ->
5727 if Wsi.withctrl mask
5728 then (
5729 match state.mstate with
5730 | Mzoom (oldn, i) ->
5731 if oldn = n
5732 then (
5733 if i = 2
5734 then
5735 let incr =
5736 match n with
5737 | 5 ->
5738 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5739 | _ ->
5740 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5742 let zoom = conf.zoom -. incr in
5743 setzoom zoom;
5744 state.mstate <- Mzoom (n, 0);
5745 else
5746 state.mstate <- Mzoom (n, i+1);
5748 else state.mstate <- Mzoom (n, 0)
5750 | _ -> state.mstate <- Mzoom (n, 0)
5752 else (
5753 match state.autoscroll with
5754 | Some step -> setautoscrollspeed step (n=4)
5755 | None ->
5756 if conf.wheelbypage
5757 then (
5758 if n = 4
5759 then prevpage ()
5760 else nextpage ()
5762 else
5763 let incr =
5764 if n = 4
5765 then -conf.scrollstep
5766 else conf.scrollstep
5768 let incr = incr * 2 in
5769 let y = clamp incr in
5770 gotoy_and_clear_text y
5773 | n when (n = 6 || n = 7) && not down && canpan () ->
5774 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5775 gotoy_and_clear_text state.y
5777 | 1 when Wsi.withctrl mask ->
5778 if down
5779 then (
5780 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5781 state.mstate <- Mpan (x, y)
5783 else
5784 state.mstate <- Mnone
5786 | 3 ->
5787 if down
5788 then (
5789 Wsi.setcursor Wsi.CURSOR_CYCLE;
5790 let p = (x, y) in
5791 state.mstate <- Mzoomrect (p, p)
5793 else (
5794 match state.mstate with
5795 | Mzoomrect ((x0, y0), _) ->
5796 if abs (x-x0) > 10 && abs (y - y0) > 10
5797 then zoomrect x0 y0 x y
5798 else (
5799 state.mstate <- Mnone;
5800 Wsi.setcursor Wsi.CURSOR_INHERIT;
5801 G.postRedisplay "kill accidental zoom rect";
5803 | _ ->
5804 Wsi.setcursor Wsi.CURSOR_INHERIT;
5805 state.mstate <- Mnone
5808 | 1 when x > conf.winw - state.scrollw ->
5809 if down
5810 then
5811 let _, position, sh = state.uioh#scrollph in
5812 if y > truncate position && y < truncate (position +. sh)
5813 then state.mstate <- Mscrolly
5814 else scrolly y
5815 else
5816 state.mstate <- Mnone
5818 | 1 when y > conf.winh - state.hscrollh ->
5819 if down
5820 then
5821 let _, position, sw = state.uioh#scrollpw in
5822 if x > truncate position && x < truncate (position +. sw)
5823 then state.mstate <- Mscrollx
5824 else scrollx x
5825 else
5826 state.mstate <- Mnone
5828 | 1 ->
5829 let dest = if down then getunder x y else Unone in
5830 begin match dest with
5831 | Ulinkgoto _
5832 | Ulinkuri _
5833 | Uremote _
5834 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5835 gotounder dest
5837 | Unone when down ->
5838 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5839 state.mstate <- Mpan (x, y);
5841 | Unone | Utext _ ->
5842 if down
5843 then (
5844 if conf.angle mod 360 = 0
5845 then (
5846 state.mstate <- Msel ((x, y), (x, y));
5847 G.postRedisplay "mouse select";
5850 else (
5851 match state.mstate with
5852 | Mnone -> ()
5854 | Mzoom _ | Mscrollx | Mscrolly ->
5855 state.mstate <- Mnone
5857 | Mzoomrect ((x0, y0), _) ->
5858 zoomrect x0 y0 x y
5860 | Mpan _ ->
5861 Wsi.setcursor Wsi.CURSOR_INHERIT;
5862 state.mstate <- Mnone
5864 | Msel ((x0, y0), (x1, y1)) ->
5865 let rec loop = function
5866 | [] -> ()
5867 | l :: rest ->
5868 let inside =
5869 let a0 = l.pagedispy in
5870 let a1 = a0 + l.pagevh in
5871 let b0 = l.pagedispx in
5872 let b1 = b0 + l.pagevw in
5873 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5874 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5876 if inside
5877 then
5878 match getopaque l.pageno with
5879 | Some opaque ->
5880 begin
5881 match Ne.pipe () with
5882 | Ne.Exn exn ->
5883 showtext '!'
5884 (Printf.sprintf
5885 "can not create sel pipe: %s"
5886 (Printexc.to_string exn));
5887 | Ne.Res (r, w) ->
5888 let doclose what fd =
5889 Ne.clo fd (fun msg ->
5890 dolog "%s close failed: %s" what msg)
5893 popen conf.selcmd [r, 0; w, -1];
5894 copysel w opaque;
5895 doclose "pipe/r" r;
5896 G.postRedisplay "copysel";
5897 with exn ->
5898 dolog "can not execute %S: %s"
5899 conf.selcmd (Printexc.to_string exn);
5900 doclose "pipe/r" r;
5901 doclose "pipe/w" w;
5903 | None -> ()
5904 else loop rest
5906 loop state.layout;
5907 Wsi.setcursor Wsi.CURSOR_INHERIT;
5908 state.mstate <- Mnone;
5912 | _ -> ()
5915 let birdseyemouse button down x y mask
5916 (conf, leftx, _, hooverpageno, anchor) =
5917 match button with
5918 | 1 when down ->
5919 let rec loop = function
5920 | [] -> ()
5921 | l :: rest ->
5922 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5923 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5924 then (
5925 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5927 else loop rest
5929 loop state.layout
5930 | 3 -> ()
5931 | _ -> viewmouse button down x y mask
5934 let mouse button down x y mask =
5935 state.uioh <- state.uioh#button button down x y mask;
5938 let motion ~x ~y =
5939 state.uioh <- state.uioh#motion x y
5942 let pmotion ~x ~y =
5943 state.uioh <- state.uioh#pmotion x y;
5946 let uioh = object
5947 method display = ()
5949 method key key mask =
5950 begin match state.mode with
5951 | Textentry textentry -> textentrykeyboard key mask textentry
5952 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5953 | View -> viewkeyboard key mask
5954 | LinkNav linknav -> linknavkeyboard key mask linknav
5955 end;
5956 state.uioh
5958 method button button bstate x y mask =
5959 begin match state.mode with
5960 | LinkNav _
5961 | View -> viewmouse button bstate x y mask
5962 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5963 | Textentry _ -> ()
5964 end;
5965 state.uioh
5967 method motion x y =
5968 begin match state.mode with
5969 | Textentry _ -> ()
5970 | View | Birdseye _ | LinkNav _ ->
5971 match state.mstate with
5972 | Mzoom _ | Mnone -> ()
5974 | Mpan (x0, y0) ->
5975 let dx = x - x0
5976 and dy = y0 - y in
5977 state.mstate <- Mpan (x, y);
5978 if canpan ()
5979 then state.x <- state.x + dx;
5980 let y = clamp dy in
5981 gotoy_and_clear_text y
5983 | Msel (a, _) ->
5984 state.mstate <- Msel (a, (x, y));
5985 G.postRedisplay "motion select";
5987 | Mscrolly ->
5988 let y = min conf.winh (max 0 y) in
5989 scrolly y
5991 | Mscrollx ->
5992 let x = min conf.winw (max 0 x) in
5993 scrollx x
5995 | Mzoomrect (p0, _) ->
5996 state.mstate <- Mzoomrect (p0, (x, y));
5997 G.postRedisplay "motion zoomrect";
5998 end;
5999 state.uioh
6001 method pmotion x y =
6002 begin match state.mode with
6003 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6004 let rec loop = function
6005 | [] ->
6006 if hooverpageno != -1
6007 then (
6008 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6009 G.postRedisplay "pmotion birdseye no hoover";
6011 | l :: rest ->
6012 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6013 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6014 then (
6015 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6016 G.postRedisplay "pmotion birdseye hoover";
6018 else loop rest
6020 loop state.layout
6022 | Textentry _ -> ()
6024 | LinkNav _
6025 | View ->
6026 match state.mstate with
6027 | Mnone -> updateunder x y
6028 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6030 end;
6031 state.uioh
6033 method infochanged _ = ()
6035 method scrollph =
6036 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
6037 let p, h = scrollph state.y maxy in
6038 state.scrollw, p, h
6040 method scrollpw =
6041 let winw = conf.winw - state.scrollw - 1 in
6042 let fwinw = float winw in
6043 let sw =
6044 let sw = fwinw /. float state.w in
6045 let sw = fwinw *. sw in
6046 max sw (float conf.scrollh)
6048 let position, sw =
6049 let f = state.w+winw in
6050 let r = float (winw-state.x) /. float f in
6051 let p = fwinw *. r in
6052 p-.sw/.2., sw
6054 let sw =
6055 if position +. sw > fwinw
6056 then fwinw -. position
6057 else sw
6059 state.hscrollh, position, sw
6061 method modehash =
6062 let modename =
6063 match state.mode with
6064 | LinkNav _ -> "links"
6065 | Textentry _ -> "textentry"
6066 | Birdseye _ -> "birdseye"
6067 | View -> "view"
6069 findkeyhash conf modename
6070 end;;
6072 module Config =
6073 struct
6074 open Parser
6076 let fontpath = ref "";;
6078 module KeyMap =
6079 Map.Make (struct type t = (int * int) let compare = compare end);;
6081 let unent s =
6082 let l = String.length s in
6083 let b = Buffer.create l in
6084 unent b s 0 l;
6085 Buffer.contents b;
6088 let home =
6089 try Sys.getenv "HOME"
6090 with exn ->
6091 prerr_endline
6092 ("Can not determine home directory location: " ^
6093 Printexc.to_string exn);
6097 let modifier_of_string = function
6098 | "alt" -> Wsi.altmask
6099 | "shift" -> Wsi.shiftmask
6100 | "ctrl" | "control" -> Wsi.ctrlmask
6101 | "meta" -> Wsi.metamask
6102 | _ -> 0
6105 let key_of_string =
6106 let r = Str.regexp "-" in
6107 fun s ->
6108 let elems = Str.full_split r s in
6109 let f n k m =
6110 let g s =
6111 let m1 = modifier_of_string s in
6112 if m1 = 0
6113 then (Wsi.namekey s, m)
6114 else (k, m lor m1)
6115 in function
6116 | Str.Delim s when n land 1 = 0 -> g s
6117 | Str.Text s -> g s
6118 | Str.Delim _ -> (k, m)
6120 let rec loop n k m = function
6121 | [] -> (k, m)
6122 | x :: xs ->
6123 let k, m = f n k m x in
6124 loop (n+1) k m xs
6126 loop 0 0 0 elems
6129 let keys_of_string =
6130 let r = Str.regexp "[ \t]" in
6131 fun s ->
6132 let elems = Str.split r s in
6133 List.map key_of_string elems
6136 let copykeyhashes c =
6137 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6140 let config_of c attrs =
6141 let apply c k v =
6143 match k with
6144 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6145 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6146 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6147 | "preload" -> { c with preload = bool_of_string v }
6148 | "page-bias" -> { c with pagebias = int_of_string v }
6149 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6150 | "horizontal-scroll-step" ->
6151 { c with hscrollstep = max (int_of_string v) 1 }
6152 | "auto-scroll-step" ->
6153 { c with autoscrollstep = max 0 (int_of_string v) }
6154 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6155 | "crop-hack" -> { c with crophack = bool_of_string v }
6156 | "throttle" ->
6157 let mw =
6158 match String.lowercase v with
6159 | "true" -> Some infinity
6160 | "false" -> None
6161 | f -> Some (float_of_string f)
6163 { c with maxwait = mw}
6164 | "highlight-links" -> { c with hlinks = bool_of_string v }
6165 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6166 | "vertical-margin" ->
6167 { c with interpagespace = max 0 (int_of_string v) }
6168 | "zoom" ->
6169 let zoom = float_of_string v /. 100. in
6170 let zoom = max zoom 0.0 in
6171 { c with zoom = zoom }
6172 | "presentation" -> { c with presentation = bool_of_string v }
6173 | "rotation-angle" -> { c with angle = int_of_string v }
6174 | "width" -> { c with winw = max 20 (int_of_string v) }
6175 | "height" -> { c with winh = max 20 (int_of_string v) }
6176 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6177 | "proportional-display" -> { c with proportional = bool_of_string v }
6178 | "pixmap-cache-size" ->
6179 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6180 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6181 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6182 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6183 | "persistent-location" -> { c with jumpback = bool_of_string v }
6184 | "background-color" -> { c with bgcolor = color_of_string v }
6185 | "scrollbar-in-presentation" ->
6186 { c with scrollbarinpm = bool_of_string v }
6187 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6188 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6189 | "mupdf-store-size" ->
6190 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6191 | "checkers" -> { c with checkers = bool_of_string v }
6192 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6193 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6194 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6195 | "uri-launcher" -> { c with urilauncher = unent v }
6196 | "path-launcher" -> { c with pathlauncher = unent v }
6197 | "color-space" -> { c with colorspace = colorspace_of_string v }
6198 | "invert-colors" -> { c with invert = bool_of_string v }
6199 | "brightness" -> { c with colorscale = float_of_string v }
6200 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6201 | "ghyllscroll" ->
6202 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6203 | "columns" ->
6204 let (n, _, _) as nab = multicolumns_of_string v in
6205 if n < 0
6206 then { c with columns = Csplit (-n, [||]) }
6207 else { c with columns = Cmulti (nab, [||]) }
6208 | "birds-eye-columns" ->
6209 { c with beyecolumns = Some (max (int_of_string v) 2) }
6210 | "selection-command" -> { c with selcmd = unent v }
6211 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6212 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6213 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6214 | "use-pbo" -> { c with usepbo = bool_of_string v }
6215 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6216 | _ -> c
6217 with exn ->
6218 prerr_endline ("Error processing attribute (`" ^
6219 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6222 let rec fold c = function
6223 | [] -> c
6224 | (k, v) :: rest ->
6225 let c = apply c k v in
6226 fold c rest
6228 fold { c with keyhashes = copykeyhashes c } attrs;
6231 let fromstring f pos n v d =
6232 try f v
6233 with exn ->
6234 dolog "Error processing attribute (%S=%S) at %d\n%s"
6235 n v pos (Printexc.to_string exn)
6240 let bookmark_of attrs =
6241 let rec fold title page rely visy = function
6242 | ("title", v) :: rest -> fold v page rely visy rest
6243 | ("page", v) :: rest -> fold title v rely visy rest
6244 | ("rely", v) :: rest -> fold title page v visy rest
6245 | ("visy", v) :: rest -> fold title page rely v rest
6246 | _ :: rest -> fold title page rely visy rest
6247 | [] -> title, page, rely, visy
6249 fold "invalid" "0" "0" "0" attrs
6252 let doc_of attrs =
6253 let rec fold path page rely pan visy = function
6254 | ("path", v) :: rest -> fold v page rely pan visy rest
6255 | ("page", v) :: rest -> fold path v rely pan visy rest
6256 | ("rely", v) :: rest -> fold path page v pan visy rest
6257 | ("pan", v) :: rest -> fold path page rely v visy rest
6258 | ("visy", v) :: rest -> fold path page rely pan v rest
6259 | _ :: rest -> fold path page rely pan visy rest
6260 | [] -> path, page, rely, pan, visy
6262 fold "" "0" "0" "0" "0" attrs
6265 let map_of attrs =
6266 let rec fold rs ls = function
6267 | ("out", v) :: rest -> fold v ls rest
6268 | ("in", v) :: rest -> fold rs v rest
6269 | _ :: rest -> fold ls rs rest
6270 | [] -> ls, rs
6272 fold "" "" attrs
6275 let setconf dst src =
6276 dst.scrollbw <- src.scrollbw;
6277 dst.scrollh <- src.scrollh;
6278 dst.icase <- src.icase;
6279 dst.preload <- src.preload;
6280 dst.pagebias <- src.pagebias;
6281 dst.verbose <- src.verbose;
6282 dst.scrollstep <- src.scrollstep;
6283 dst.maxhfit <- src.maxhfit;
6284 dst.crophack <- src.crophack;
6285 dst.autoscrollstep <- src.autoscrollstep;
6286 dst.maxwait <- src.maxwait;
6287 dst.hlinks <- src.hlinks;
6288 dst.underinfo <- src.underinfo;
6289 dst.interpagespace <- src.interpagespace;
6290 dst.zoom <- src.zoom;
6291 dst.presentation <- src.presentation;
6292 dst.angle <- src.angle;
6293 dst.winw <- src.winw;
6294 dst.winh <- src.winh;
6295 dst.savebmarks <- src.savebmarks;
6296 dst.memlimit <- src.memlimit;
6297 dst.proportional <- src.proportional;
6298 dst.texcount <- src.texcount;
6299 dst.sliceheight <- src.sliceheight;
6300 dst.thumbw <- src.thumbw;
6301 dst.jumpback <- src.jumpback;
6302 dst.bgcolor <- src.bgcolor;
6303 dst.scrollbarinpm <- src.scrollbarinpm;
6304 dst.tilew <- src.tilew;
6305 dst.tileh <- src.tileh;
6306 dst.mustoresize <- src.mustoresize;
6307 dst.checkers <- src.checkers;
6308 dst.aalevel <- src.aalevel;
6309 dst.trimmargins <- src.trimmargins;
6310 dst.trimfuzz <- src.trimfuzz;
6311 dst.urilauncher <- src.urilauncher;
6312 dst.colorspace <- src.colorspace;
6313 dst.invert <- src.invert;
6314 dst.colorscale <- src.colorscale;
6315 dst.redirectstderr <- src.redirectstderr;
6316 dst.ghyllscroll <- src.ghyllscroll;
6317 dst.columns <- src.columns;
6318 dst.beyecolumns <- src.beyecolumns;
6319 dst.selcmd <- src.selcmd;
6320 dst.updatecurs <- src.updatecurs;
6321 dst.pathlauncher <- src.pathlauncher;
6322 dst.keyhashes <- copykeyhashes src;
6323 dst.hfsize <- src.hfsize;
6324 dst.hscrollstep <- src.hscrollstep;
6325 dst.pgscale <- src.pgscale;
6326 dst.usepbo <- src.usepbo;
6327 dst.wheelbypage <- src.wheelbypage;
6330 let get s =
6331 let h = Hashtbl.create 10 in
6332 let dc = { defconf with angle = defconf.angle } in
6333 let rec toplevel v t spos _ =
6334 match t with
6335 | Vdata | Vcdata | Vend -> v
6336 | Vopen ("llppconfig", _, closed) ->
6337 if closed
6338 then v
6339 else { v with f = llppconfig }
6340 | Vopen _ ->
6341 error "unexpected subelement at top level" s spos
6342 | Vclose _ -> error "unexpected close at top level" s spos
6344 and llppconfig v t spos _ =
6345 match t with
6346 | Vdata | Vcdata -> v
6347 | Vend -> error "unexpected end of input in llppconfig" s spos
6348 | Vopen ("defaults", attrs, closed) ->
6349 let c = config_of dc attrs in
6350 setconf dc c;
6351 if closed
6352 then v
6353 else { v with f = defaults }
6355 | Vopen ("ui-font", attrs, closed) ->
6356 let rec getsize size = function
6357 | [] -> size
6358 | ("size", v) :: rest ->
6359 let size =
6360 fromstring int_of_string spos "size" v fstate.fontsize in
6361 getsize size rest
6362 | l -> getsize size l
6364 fstate.fontsize <- getsize fstate.fontsize attrs;
6365 if closed
6366 then v
6367 else { v with f = uifont (Buffer.create 10) }
6369 | Vopen ("doc", attrs, closed) ->
6370 let pathent, spage, srely, span, svisy = doc_of attrs in
6371 let path = unent pathent
6372 and pageno = fromstring int_of_string spos "page" spage 0
6373 and rely = fromstring float_of_string spos "rely" srely 0.0
6374 and pan = fromstring int_of_string spos "pan" span 0
6375 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6376 let c = config_of dc attrs in
6377 let anchor = (pageno, rely, visy) in
6378 if closed
6379 then (Hashtbl.add h path (c, [], pan, anchor); v)
6380 else { v with f = doc path pan anchor c [] }
6382 | Vopen _ ->
6383 error "unexpected subelement in llppconfig" s spos
6385 | Vclose "llppconfig" -> { v with f = toplevel }
6386 | Vclose _ -> error "unexpected close in llppconfig" s spos
6388 and defaults v t spos _ =
6389 match t with
6390 | Vdata | Vcdata -> v
6391 | Vend -> error "unexpected end of input in defaults" s spos
6392 | Vopen ("keymap", attrs, closed) ->
6393 let modename =
6394 try List.assoc "mode" attrs
6395 with Not_found -> "global" in
6396 if closed
6397 then v
6398 else
6399 let ret keymap =
6400 let h = findkeyhash dc modename in
6401 KeyMap.iter (Hashtbl.replace h) keymap;
6402 defaults
6404 { v with f = pkeymap ret KeyMap.empty }
6406 | Vopen (_, _, _) ->
6407 error "unexpected subelement in defaults" s spos
6409 | Vclose "defaults" ->
6410 { v with f = llppconfig }
6412 | Vclose _ -> error "unexpected close in defaults" s spos
6414 and uifont b v t spos epos =
6415 match t with
6416 | Vdata | Vcdata ->
6417 Buffer.add_substring b s spos (epos - spos);
6419 | Vopen (_, _, _) ->
6420 error "unexpected subelement in ui-font" s spos
6421 | Vclose "ui-font" ->
6422 if String.length !fontpath = 0
6423 then fontpath := Buffer.contents b;
6424 { v with f = llppconfig }
6425 | Vclose _ -> error "unexpected close in ui-font" s spos
6426 | Vend -> error "unexpected end of input in ui-font" s spos
6428 and doc path pan anchor c bookmarks v t spos _ =
6429 match t with
6430 | Vdata | Vcdata -> v
6431 | Vend -> error "unexpected end of input in doc" s spos
6432 | Vopen ("bookmarks", _, closed) ->
6433 if closed
6434 then v
6435 else { v with f = pbookmarks path pan anchor c bookmarks }
6437 | Vopen ("keymap", attrs, closed) ->
6438 let modename =
6439 try List.assoc "mode" attrs
6440 with Not_found -> "global"
6442 if closed
6443 then v
6444 else
6445 let ret keymap =
6446 let h = findkeyhash c modename in
6447 KeyMap.iter (Hashtbl.replace h) keymap;
6448 doc path pan anchor c bookmarks
6450 { v with f = pkeymap ret KeyMap.empty }
6452 | Vopen (_, _, _) ->
6453 error "unexpected subelement in doc" s spos
6455 | Vclose "doc" ->
6456 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6457 { v with f = llppconfig }
6459 | Vclose _ -> error "unexpected close in doc" s spos
6461 and pkeymap ret keymap v t spos _ =
6462 match t with
6463 | Vdata | Vcdata -> v
6464 | Vend -> error "unexpected end of input in keymap" s spos
6465 | Vopen ("map", attrs, closed) ->
6466 let r, l = map_of attrs in
6467 let kss = fromstring keys_of_string spos "in" r [] in
6468 let lss = fromstring keys_of_string spos "out" l [] in
6469 let keymap =
6470 match kss with
6471 | [] -> keymap
6472 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6473 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6475 if closed
6476 then { v with f = pkeymap ret keymap }
6477 else
6478 let f () = v in
6479 { v with f = skip "map" f }
6481 | Vopen _ ->
6482 error "unexpected subelement in keymap" s spos
6484 | Vclose "keymap" ->
6485 { v with f = ret keymap }
6487 | Vclose _ -> error "unexpected close in keymap" s spos
6489 and pbookmarks path pan anchor c bookmarks v t spos _ =
6490 match t with
6491 | Vdata | Vcdata -> v
6492 | Vend -> error "unexpected end of input in bookmarks" s spos
6493 | Vopen ("item", attrs, closed) ->
6494 let titleent, spage, srely, svisy = bookmark_of attrs in
6495 let page = fromstring int_of_string spos "page" spage 0
6496 and rely = fromstring float_of_string spos "rely" srely 0.0
6497 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6498 let bookmarks =
6499 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6501 if closed
6502 then { v with f = pbookmarks path pan anchor c bookmarks }
6503 else
6504 let f () = v in
6505 { v with f = skip "item" f }
6507 | Vopen _ ->
6508 error "unexpected subelement in bookmarks" s spos
6510 | Vclose "bookmarks" ->
6511 { v with f = doc path pan anchor c bookmarks }
6513 | Vclose _ -> error "unexpected close in bookmarks" s spos
6515 and skip tag f v t spos _ =
6516 match t with
6517 | Vdata | Vcdata -> v
6518 | Vend ->
6519 error ("unexpected end of input in skipped " ^ tag) s spos
6520 | Vopen (tag', _, closed) ->
6521 if closed
6522 then v
6523 else
6524 let f' () = { v with f = skip tag f } in
6525 { v with f = skip tag' f' }
6526 | Vclose ctag ->
6527 if tag = ctag
6528 then f ()
6529 else error ("unexpected close in skipped " ^ tag) s spos
6532 parse { f = toplevel; accu = () } s;
6533 h, dc;
6536 let do_load f ic =
6538 let len = in_channel_length ic in
6539 let s = String.create len in
6540 really_input ic s 0 len;
6541 f s;
6542 with
6543 | Parse_error (msg, s, pos) ->
6544 let subs = subs s pos in
6545 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6546 failwith ("parse error: " ^ s)
6548 | exn ->
6549 failwith ("config load error: " ^ Printexc.to_string exn)
6552 let defconfpath =
6553 let dir =
6555 let dir = Filename.concat home ".config" in
6556 if Sys.is_directory dir then dir else home
6557 with _ -> home
6559 Filename.concat dir "llpp.conf"
6562 let confpath = ref defconfpath;;
6564 let load1 f =
6565 if Sys.file_exists !confpath
6566 then
6567 match
6568 (try Some (open_in_bin !confpath)
6569 with exn ->
6570 prerr_endline
6571 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6572 Printexc.to_string exn);
6573 None
6575 with
6576 | Some ic ->
6577 let success =
6579 f (do_load get ic)
6580 with exn ->
6581 prerr_endline
6582 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6583 Printexc.to_string exn);
6584 false
6586 close_in ic;
6587 success
6589 | None -> false
6590 else
6591 f (Hashtbl.create 0, defconf)
6594 let load () =
6595 let f (h, dc) =
6596 let pc, pb, px, pa =
6598 Hashtbl.find h (Filename.basename state.path)
6599 with Not_found -> dc, [], 0, emptyanchor
6601 setconf defconf dc;
6602 setconf conf pc;
6603 state.bookmarks <- pb;
6604 state.x <- px;
6605 state.scrollw <- conf.scrollbw;
6606 if conf.jumpback
6607 then state.anchor <- pa;
6608 cbput state.hists.nav pa;
6609 true
6611 load1 f
6614 let add_attrs bb always dc c =
6615 let ob s a b =
6616 if always || a != b
6617 then Printf.bprintf bb "\n %s='%b'" s a
6618 and oi s a b =
6619 if always || a != b
6620 then Printf.bprintf bb "\n %s='%d'" s a
6621 and oI s a b =
6622 if always || a != b
6623 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6624 and oz s a b =
6625 if always || a <> b
6626 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6627 and oF s a b =
6628 if always || a <> b
6629 then Printf.bprintf bb "\n %s='%f'" s a
6630 and oc s a b =
6631 if always || a <> b
6632 then
6633 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6634 and oC s a b =
6635 if always || a <> b
6636 then
6637 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6638 and oR s a b =
6639 if always || a <> b
6640 then
6641 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6642 and os s a b =
6643 if always || a <> b
6644 then
6645 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6646 and og s a b =
6647 if always || a <> b
6648 then
6649 match a with
6650 | None -> ()
6651 | Some (_N, _A, _B) ->
6652 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6653 and oW s a b =
6654 if always || a <> b
6655 then
6656 let v =
6657 match a with
6658 | None -> "false"
6659 | Some f ->
6660 if f = infinity
6661 then "true"
6662 else string_of_float f
6664 Printf.bprintf bb "\n %s='%s'" s v
6665 and oco s a b =
6666 if always || a <> b
6667 then
6668 match a with
6669 | Cmulti ((n, a, b), _) when n > 1 ->
6670 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6671 | Csplit (n, _) when n > 1 ->
6672 Printf.bprintf bb "\n %s='%d'" s ~-n
6673 | _ -> ()
6674 and obeco s a b =
6675 if always || a <> b
6676 then
6677 match a with
6678 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6679 | _ -> ()
6681 let w, h =
6682 if always
6683 then dc.winw, dc.winh
6684 else
6685 match state.fullscreen with
6686 | Some wh -> wh
6687 | None -> c.winw, c.winh
6689 oi "width" w dc.winw;
6690 oi "height" h dc.winh;
6691 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6692 oi "scroll-handle-height" c.scrollh dc.scrollh;
6693 ob "case-insensitive-search" c.icase dc.icase;
6694 ob "preload" c.preload dc.preload;
6695 oi "page-bias" c.pagebias dc.pagebias;
6696 oi "scroll-step" c.scrollstep dc.scrollstep;
6697 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6698 ob "max-height-fit" c.maxhfit dc.maxhfit;
6699 ob "crop-hack" c.crophack dc.crophack;
6700 oW "throttle" c.maxwait dc.maxwait;
6701 ob "highlight-links" c.hlinks dc.hlinks;
6702 ob "under-cursor-info" c.underinfo dc.underinfo;
6703 oi "vertical-margin" c.interpagespace dc.interpagespace;
6704 oz "zoom" c.zoom dc.zoom;
6705 ob "presentation" c.presentation dc.presentation;
6706 oi "rotation-angle" c.angle dc.angle;
6707 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6708 ob "proportional-display" c.proportional dc.proportional;
6709 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6710 oi "tex-count" c.texcount dc.texcount;
6711 oi "slice-height" c.sliceheight dc.sliceheight;
6712 oi "thumbnail-width" c.thumbw dc.thumbw;
6713 ob "persistent-location" c.jumpback dc.jumpback;
6714 oc "background-color" c.bgcolor dc.bgcolor;
6715 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6716 oi "tile-width" c.tilew dc.tilew;
6717 oi "tile-height" c.tileh dc.tileh;
6718 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6719 ob "checkers" c.checkers dc.checkers;
6720 oi "aalevel" c.aalevel dc.aalevel;
6721 ob "trim-margins" c.trimmargins dc.trimmargins;
6722 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6723 os "uri-launcher" c.urilauncher dc.urilauncher;
6724 os "path-launcher" c.pathlauncher dc.pathlauncher;
6725 oC "color-space" c.colorspace dc.colorspace;
6726 ob "invert-colors" c.invert dc.invert;
6727 oF "brightness" c.colorscale dc.colorscale;
6728 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6729 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6730 oco "columns" c.columns dc.columns;
6731 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6732 os "selection-command" c.selcmd dc.selcmd;
6733 ob "update-cursor" c.updatecurs dc.updatecurs;
6734 oi "hint-font-size" c.hfsize dc.hfsize;
6735 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6736 oF "page-scroll-scale" c.pgscale dc.pgscale;
6737 ob "use-pbo" c.usepbo dc.usepbo;
6738 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6741 let keymapsbuf always dc c =
6742 let bb = Buffer.create 16 in
6743 let rec loop = function
6744 | [] -> ()
6745 | (modename, h) :: rest ->
6746 let dh = findkeyhash dc modename in
6747 if always || h <> dh
6748 then (
6749 if Hashtbl.length h > 0
6750 then (
6751 if Buffer.length bb > 0
6752 then Buffer.add_char bb '\n';
6753 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6754 Hashtbl.iter (fun i o ->
6755 let isdifferent = always ||
6757 let dO = Hashtbl.find dh i in
6758 dO <> o
6759 with Not_found -> true
6761 if isdifferent
6762 then
6763 let addkm (k, m) =
6764 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6765 if Wsi.withalt m then Buffer.add_string bb "alt-";
6766 if Wsi.withshift m then Buffer.add_string bb "shift-";
6767 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6768 Buffer.add_string bb (Wsi.keyname k);
6770 let addkms l =
6771 let rec loop = function
6772 | [] -> ()
6773 | km :: [] -> addkm km
6774 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6776 loop l
6778 Buffer.add_string bb "<map in='";
6779 addkm i;
6780 match o with
6781 | KMinsrt km ->
6782 Buffer.add_string bb "' out='";
6783 addkm km;
6784 Buffer.add_string bb "'/>\n"
6786 | KMinsrl kms ->
6787 Buffer.add_string bb "' out='";
6788 addkms kms;
6789 Buffer.add_string bb "'/>\n"
6791 | KMmulti (ins, kms) ->
6792 Buffer.add_char bb ' ';
6793 addkms ins;
6794 Buffer.add_string bb "' out='";
6795 addkms kms;
6796 Buffer.add_string bb "'/>\n"
6797 ) h;
6798 Buffer.add_string bb "</keymap>";
6801 loop rest
6803 loop c.keyhashes;
6807 let save () =
6808 let uifontsize = fstate.fontsize in
6809 let bb = Buffer.create 32768 in
6810 let f (h, dc) =
6811 let dc = if conf.bedefault then conf else dc in
6812 Buffer.add_string bb "<llppconfig>\n";
6814 if String.length !fontpath > 0
6815 then
6816 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6817 uifontsize
6818 !fontpath
6819 else (
6820 if uifontsize <> 14
6821 then
6822 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6825 Buffer.add_string bb "<defaults ";
6826 add_attrs bb true dc dc;
6827 let kb = keymapsbuf true dc dc in
6828 if Buffer.length kb > 0
6829 then (
6830 Buffer.add_string bb ">\n";
6831 Buffer.add_buffer bb kb;
6832 Buffer.add_string bb "\n</defaults>\n";
6834 else Buffer.add_string bb "/>\n";
6836 let adddoc path pan anchor c bookmarks =
6837 if bookmarks == [] && c = dc && anchor = emptyanchor
6838 then ()
6839 else (
6840 Printf.bprintf bb "<doc path='%s'"
6841 (enent path 0 (String.length path));
6843 if anchor <> emptyanchor
6844 then (
6845 let n, rely, visy = anchor in
6846 Printf.bprintf bb " page='%d'" n;
6847 if rely > 1e-6
6848 then
6849 Printf.bprintf bb " rely='%f'" rely
6851 if abs_float visy > 1e-6
6852 then
6853 Printf.bprintf bb " visy='%f'" visy
6857 if pan != 0
6858 then Printf.bprintf bb " pan='%d'" pan;
6860 add_attrs bb false dc c;
6861 let kb = keymapsbuf false dc c in
6863 begin match bookmarks with
6864 | [] ->
6865 if Buffer.length kb > 0
6866 then (
6867 Buffer.add_string bb ">\n";
6868 Buffer.add_buffer bb kb;
6869 Buffer.add_string bb "\n</doc>\n";
6871 else Buffer.add_string bb "/>\n"
6872 | _ ->
6873 Buffer.add_string bb ">\n<bookmarks>\n";
6874 List.iter (fun (title, _level, (page, rely, visy)) ->
6875 Printf.bprintf bb
6876 "<item title='%s' page='%d'"
6877 (enent title 0 (String.length title))
6878 page
6880 if rely > 1e-6
6881 then
6882 Printf.bprintf bb " rely='%f'" rely
6884 if abs_float visy > 1e-6
6885 then
6886 Printf.bprintf bb " visy='%f'" visy
6888 Buffer.add_string bb "/>\n";
6889 ) bookmarks;
6890 Buffer.add_string bb "</bookmarks>";
6891 if Buffer.length kb > 0
6892 then (
6893 Buffer.add_string bb "\n";
6894 Buffer.add_buffer bb kb;
6896 Buffer.add_string bb "\n</doc>\n";
6897 end;
6901 let pan, conf =
6902 match state.mode with
6903 | Birdseye (c, pan, _, _, _) ->
6904 let beyecolumns =
6905 match conf.columns with
6906 | Cmulti ((c, _, _), _) -> Some c
6907 | Csingle _ -> None
6908 | Csplit _ -> None
6909 and columns =
6910 match c.columns with
6911 | Cmulti (c, _) -> Cmulti (c, [||])
6912 | Csingle _ -> Csingle [||]
6913 | Csplit _ -> failwith "quit from bird's eye while split"
6915 pan, { c with beyecolumns = beyecolumns; columns = columns }
6916 | _ -> state.x, conf
6918 let basename = Filename.basename state.path in
6919 adddoc basename pan (getanchor ())
6920 (let conf =
6921 let autoscrollstep =
6922 match state.autoscroll with
6923 | Some step -> step
6924 | None -> conf.autoscrollstep
6926 match state.mode with
6927 | Birdseye (bc, _, _, _, _) ->
6928 { conf with
6929 zoom = bc.zoom;
6930 presentation = bc.presentation;
6931 interpagespace = bc.interpagespace;
6932 maxwait = bc.maxwait;
6933 autoscrollstep = autoscrollstep }
6934 | _ -> { conf with autoscrollstep = autoscrollstep }
6935 in conf)
6936 (if conf.savebmarks then state.bookmarks else []);
6938 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6939 if basename <> path
6940 then adddoc path x anchor c bookmarks
6941 ) h;
6942 Buffer.add_string bb "</llppconfig>\n";
6943 true;
6945 if load1 f && Buffer.length bb > 0
6946 then
6948 let tmp = !confpath ^ ".tmp" in
6949 let oc = open_out_bin tmp in
6950 Buffer.output_buffer oc bb;
6951 close_out oc;
6952 Unix.rename tmp !confpath;
6953 with exn ->
6954 prerr_endline
6955 ("error while saving configuration: " ^ Printexc.to_string exn)
6957 end;;
6959 let () =
6960 let trimcachepath = ref "" in
6961 Arg.parse
6962 (Arg.align
6963 [("-p", Arg.String (fun s -> state.password <- s) ,
6964 "<password> Set password");
6966 ("-f", Arg.String (fun s -> Config.fontpath := s),
6967 "<path> Set path to the user interface font");
6969 ("-c", Arg.String (fun s -> Config.confpath := s),
6970 "<path> Set path to the configuration file");
6972 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6973 "<path> Set path to the trim cache file");
6975 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6976 "<named destination> Set named destination");
6978 ("-wtmode", Arg.Set wtmode, "wt mode");
6980 ("-v", Arg.Unit (fun () ->
6981 Printf.printf
6982 "%s\nconfiguration path: %s\n"
6983 (version ())
6984 Config.defconfpath
6986 exit 0), " Print version and exit");
6989 (fun s -> state.path <- s)
6990 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6992 if String.length state.path = 0
6993 then (prerr_endline "file name missing"; exit 1);
6995 if not (Config.load ())
6996 then prerr_endline "failed to load configuration";
6998 let globalkeyhash = findkeyhash conf "global" in
6999 let wsfd, winw, winh = Wsi.init (object
7000 method expose =
7001 state.wthack <- false;
7002 if nogeomcmds state.geomcmds || platform == Posx
7003 then display ()
7004 else (
7005 GlClear.color (scalecolor2 conf.bgcolor);
7006 GlClear.clear [`color];
7008 method display = display ()
7009 method reshape w h = reshape w h
7010 method mouse b d x y m = mouse b d x y m
7011 method motion x y = state.mpos <- (x, y); motion x y
7012 method pmotion x y = state.mpos <- (x, y); pmotion x y
7013 method key k m =
7014 let mascm = m land (
7015 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7016 ) in
7017 match state.keystate with
7018 | KSnone ->
7019 let km = k, mascm in
7020 begin
7021 match
7022 let modehash = state.uioh#modehash in
7023 try Hashtbl.find modehash km
7024 with Not_found ->
7025 try Hashtbl.find globalkeyhash km
7026 with Not_found -> KMinsrt (k, m)
7027 with
7028 | KMinsrt (k, m) -> keyboard k m
7029 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7030 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7032 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7033 List.iter (fun (k, m) -> keyboard k m) insrt;
7034 state.keystate <- KSnone
7035 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7036 state.keystate <- KSinto (keys, insrt)
7037 | _ ->
7038 state.keystate <- KSnone
7040 method enter x y = state.mpos <- (x, y); pmotion x y
7041 method leave = state.mpos <- (-1, -1)
7042 method quit = raise Quit
7043 end) conf.winw conf.winh (platform = Posx) in
7045 state.wsfd <- wsfd;
7047 if not (
7048 List.exists GlMisc.check_extension
7049 [ "GL_ARB_texture_rectangle"
7050 ; "GL_EXT_texture_recangle"
7051 ; "GL_NV_texture_rectangle" ]
7053 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7055 let cr, sw =
7056 match Ne.pipe () with
7057 | Ne.Exn exn ->
7058 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
7059 exit 1
7060 | Ne.Res rw -> rw
7061 and sr, cw =
7062 match Ne.pipe () with
7063 | Ne.Exn exn ->
7064 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7065 exit 1
7066 | Ne.Res rw -> rw
7069 cloexec cr;
7070 cloexec sw;
7071 cloexec sr;
7072 cloexec cw;
7074 setcheckers conf.checkers;
7075 redirectstderr ();
7077 init (cr, cw) (
7078 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7079 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7080 !Config.fontpath, !trimcachepath,
7081 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7083 state.sr <- sr;
7084 state.sw <- sw;
7085 state.text <- "Opening " ^ (mbtoutf8 state.path);
7086 reshape winw winh;
7087 opendoc state.path state.password;
7088 state.uioh <- uioh;
7090 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7092 let rec loop deadline =
7093 let r =
7094 match state.errfd with
7095 | None -> [state.sr; state.wsfd]
7096 | Some fd -> [state.sr; state.wsfd; fd]
7098 if state.redisplay && not state.wthack
7099 then (
7100 state.redisplay <- false;
7101 display ();
7103 let timeout =
7104 let now = now () in
7105 if deadline > now
7106 then (
7107 if deadline = infinity
7108 then ~-.1.0
7109 else max 0.0 (deadline -. now)
7111 else 0.0
7113 let r, _, _ =
7114 try tempfailureretry (Unix.select r [] []) timeout
7115 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7117 begin match r with
7118 | [] ->
7119 state.ghyll None;
7120 let newdeadline =
7121 if state.ghyll == noghyll
7122 then
7123 match state.autoscroll with
7124 | Some step when step != 0 ->
7125 let y = state.y + step in
7126 let y =
7127 if y < 0
7128 then state.maxy
7129 else if y >= state.maxy then 0 else y
7131 gotoy y;
7132 if state.mode = View
7133 then state.text <- "";
7134 deadline +. 0.01
7135 | _ -> infinity
7136 else deadline +. 0.01
7138 loop newdeadline
7140 | l ->
7141 let rec checkfds = function
7142 | [] -> ()
7143 | fd :: rest when fd = state.sr ->
7144 let cmd = readcmd state.sr in
7145 act cmd;
7146 checkfds rest
7148 | fd :: rest when fd = state.wsfd ->
7149 Wsi.readresp fd;
7150 checkfds rest
7152 | fd :: rest ->
7153 let s = String.create 80 in
7154 let n = tempfailureretry (Unix.read fd s 0) 80 in
7155 if conf.redirectstderr
7156 then (
7157 Buffer.add_substring state.errmsgs s 0 n;
7158 state.newerrmsgs <- true;
7159 state.redisplay <- true;
7161 else (
7162 prerr_string (String.sub s 0 n);
7163 flush stderr;
7165 checkfds rest
7167 checkfds l;
7168 let newdeadline =
7169 let deadline1 =
7170 if deadline = infinity
7171 then now () +. 0.01
7172 else deadline
7174 match state.autoscroll with
7175 | Some step when step != 0 -> deadline1
7176 | _ -> if state.ghyll == noghyll then infinity else deadline1
7178 loop newdeadline
7179 end;
7182 loop infinity;
7183 with Quit ->
7184 Config.save ();