Robustness
[llpp.git] / main.ml
blob6502debfddfb026477201cc68987872ae026704f
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath * trimcachepath
20 * haspbo)
21 and pageno = int
22 and width = int
23 and height = int
24 and leftx = int
25 and opaque = string
26 and recttype = int
27 and pixmapsize = int
28 and angle = int
29 and proportional = bool
30 and trimmargins = bool
31 and interpagespace = int
32 and texcount = int
33 and sliceheight = int
34 and gen = int
35 and top = float
36 and dtop = float
37 and fontpath = string
38 and trimcachepath = string
39 and memsize = int
40 and aalevel = int
41 and irect = (int * int * int * int)
42 and trimparams = (trimmargins * irect)
43 and colorspace = | Rgb | Bgr | Gray
44 and haspbo = bool
47 type link =
48 | Lnotfound
49 | Lfound of int
50 and linkdir =
51 | LDfirst
52 | LDlast
53 | LDfirstvisible of (int * int * int)
54 | LDleft of int
55 | LDright of int
56 | LDdown of int
57 | LDup of int
60 type pagewithlinks =
61 | Pwlnotfound
62 | Pwl of int
65 type keymap =
66 | KMinsrt of key
67 | KMinsrl of key list
68 | KMmulti of key list * key list
69 and key = int * int
70 and keyhash = (key, keymap) Hashtbl.t
71 and keystate =
72 | KSnone
73 | KSinto of (key list * key list)
76 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
77 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
79 type pipe = (Unix.file_descr * Unix.file_descr);;
81 external init : pipe -> params -> unit = "ml_init";;
82 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
83 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
84 external getpdimrect : int -> float array = "ml_getpdimrect";;
85 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
86 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
87 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
88 external measurestr : int -> string -> float = "ml_measure_string";;
89 external getmaxw : unit -> float = "ml_getmaxw";;
90 external postprocess :
91 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
92 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
93 external platform : unit -> platform = "ml_platform";;
94 external setaalevel : int -> unit = "ml_setaalevel";;
95 external realloctexts : int -> bool = "ml_realloctexts";;
96 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
97 external findlink : opaque -> linkdir -> link = "ml_findlink";;
98 external getlink : opaque -> int -> under = "ml_getlink";;
99 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
100 external getlinkcount : opaque -> int = "ml_getlinkcount";;
101 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
102 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
103 external mbtoutf8 : string -> string = "ml_mbtoutf8";;
104 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
105 external freepbo : string -> unit = "ml_freepbo";;
106 external unmappbo : string -> unit = "ml_unmappbo";;
107 external pbousable : unit -> bool = "ml_pbo_usable";;
109 let platform_to_string = function
110 | Punknown -> "unknown"
111 | Plinux -> "Linux"
112 | Posx -> "OSX"
113 | Psun -> "Sun"
114 | Pfreebsd -> "FreeBSD"
115 | Pdragonflybsd -> "DragonflyBSD"
116 | Popenbsd -> "OpenBSD"
117 | Pnetbsd -> "NetBSD"
118 | Pcygwin -> "Cygwin"
121 let platform = platform ();;
123 let popen cmd fda =
124 if platform = Pcygwin
125 then (
126 let sh = "/bin/sh" in
127 let args = [|sh; "-c"; cmd|] in
128 let rec std si so se = function
129 | [] -> si, so, se
130 | (fd, 0) :: rest -> std fd so se rest
131 | (fd, -1) :: rest ->
132 Unix.set_close_on_exec fd;
133 std si so se rest
134 | (_, n) :: _ ->
135 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
137 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
138 ignore (Unix.create_process sh args si so se)
140 else popen cmd fda;
143 type x = int
144 and y = int
145 and tilex = int
146 and tiley = int
147 and tileparams = (x * y * width * height * tilex * tiley)
150 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
152 type mpos = int * int
153 and mstate =
154 | Msel of (mpos * mpos)
155 | Mpan of mpos
156 | Mscrolly | Mscrollx
157 | Mzoom of (int * int)
158 | Mzoomrect of (mpos * mpos)
159 | Mnone
162 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
163 and onkey = string -> int -> te
164 and ondone = string -> unit
165 and histcancel = unit -> unit
166 and onhist = ((histcmd -> string) * histcancel)
167 and histcmd = HCnext | HCprev | HCfirst | HClast
168 and cancelonempty = bool
169 and te =
170 | TEstop
171 | TEdone of string
172 | TEcont of string
173 | TEswitch of textentry
176 type 'a circbuf =
177 { store : 'a array
178 ; mutable rc : int
179 ; mutable wc : int
180 ; mutable len : int
184 let bound v minv maxv =
185 max minv (min maxv v);
188 let cbnew n v =
189 { store = Array.create n v
190 ; rc = 0
191 ; wc = 0
192 ; len = 0
196 let cbcap b = Array.length b.store;;
198 let cbput b v =
199 let cap = cbcap b in
200 b.store.(b.wc) <- v;
201 b.wc <- (b.wc + 1) mod cap;
202 b.rc <- b.wc;
203 b.len <- min (b.len + 1) cap;
206 let cbempty b = b.len = 0;;
208 let cbgetg b circular dir =
209 if cbempty b
210 then b.store.(0)
211 else
212 let rc = b.rc + dir in
213 let rc =
214 if circular
215 then (
216 if rc = -1
217 then b.len-1
218 else (
219 if rc >= b.len
220 then 0
221 else rc
224 else bound rc 0 (b.len-1)
226 b.rc <- rc;
227 b.store.(rc);
230 let cbget b = cbgetg b false;;
231 let cbgetc b = cbgetg b true;;
233 let drawstring size x y s =
234 Gl.enable `blend;
235 Gl.enable `texture_2d;
236 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
237 ignore (drawstr size x y s);
238 Gl.disable `blend;
239 Gl.disable `texture_2d;
242 let drawstring1 size x y s =
243 drawstr size x y s;
246 let drawstring2 size x y fmt =
247 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
250 type page =
251 { pageno : int
252 ; pagedimno : int
253 ; pagew : int
254 ; pageh : int
255 ; pagex : int
256 ; pagey : int
257 ; pagevw : int
258 ; pagevh : int
259 ; pagedispx : int
260 ; pagedispy : int
261 ; pagecol : int
265 let debugl l =
266 dolog "l %d dim=%d {" l.pageno l.pagedimno;
267 dolog " WxH %dx%d" l.pagew l.pageh;
268 dolog " vWxH %dx%d" l.pagevw l.pagevh;
269 dolog " pagex,y %d,%d" l.pagex l.pagey;
270 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
271 dolog " column %d" l.pagecol;
272 dolog "}";
275 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
276 dolog "rect {";
277 dolog " x0,y0=(% f, % f)" x0 y0;
278 dolog " x1,y1=(% f, % f)" x1 y1;
279 dolog " x2,y2=(% f, % f)" x2 y2;
280 dolog " x3,y3=(% f, % f)" x3 y3;
281 dolog "}";
284 type multicolumns = multicol * pagegeom
285 and singlecolumn = pagegeom
286 and splitcolumns = columncount * pagegeom
287 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
288 and multicol = columncount * covercount * covercount
289 and pdimno = int
290 and columncount = int
291 and covercount = int;;
293 type conf =
294 { mutable scrollbw : int
295 ; mutable scrollh : int
296 ; mutable icase : bool
297 ; mutable preload : bool
298 ; mutable pagebias : int
299 ; mutable verbose : bool
300 ; mutable debug : bool
301 ; mutable scrollstep : int
302 ; mutable hscrollstep : int
303 ; mutable maxhfit : bool
304 ; mutable crophack : bool
305 ; mutable autoscrollstep : int
306 ; mutable maxwait : float option
307 ; mutable hlinks : bool
308 ; mutable underinfo : bool
309 ; mutable interpagespace : interpagespace
310 ; mutable zoom : float
311 ; mutable presentation : bool
312 ; mutable angle : angle
313 ; mutable winw : int
314 ; mutable winh : int
315 ; mutable savebmarks : bool
316 ; mutable proportional : proportional
317 ; mutable trimmargins : trimmargins
318 ; mutable trimfuzz : irect
319 ; mutable memlimit : memsize
320 ; mutable texcount : texcount
321 ; mutable sliceheight : sliceheight
322 ; mutable thumbw : width
323 ; mutable jumpback : bool
324 ; mutable bgcolor : float * float * float
325 ; mutable bedefault : bool
326 ; mutable scrollbarinpm : bool
327 ; mutable tilew : int
328 ; mutable tileh : int
329 ; mutable mustoresize : memsize
330 ; mutable checkers : bool
331 ; mutable aalevel : int
332 ; mutable urilauncher : string
333 ; mutable pathlauncher : string
334 ; mutable colorspace : colorspace
335 ; mutable invert : bool
336 ; mutable colorscale : float
337 ; mutable redirectstderr : bool
338 ; mutable ghyllscroll : (int * int * int) option
339 ; mutable columns : columns
340 ; mutable beyecolumns : columncount option
341 ; mutable selcmd : string
342 ; mutable updatecurs : bool
343 ; mutable keyhashes : (string * keyhash) list
344 ; mutable hfsize : int
345 ; mutable pgscale : float
346 ; mutable usepbo : bool
348 and columns =
349 | Csingle of singlecolumn
350 | Cmulti of multicolumns
351 | Csplit of splitcolumns
354 type anchor = pageno * top * dtop;;
356 type outline = string * int * anchor;;
358 type rect = float * float * float * float * float * float * float * float;;
360 type tile = opaque * pixmapsize * elapsed
361 and elapsed = float;;
362 type pagemapkey = pageno * gen;;
363 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
364 and row = int
365 and col = int;;
367 let emptyanchor = (0, 0.0, 0.0);;
369 type infochange = | Memused | Docinfo | Pdim;;
371 class type uioh = object
372 method display : unit
373 method key : int -> int -> uioh
374 method button : int -> bool -> int -> int -> int -> uioh
375 method motion : int -> int -> uioh
376 method pmotion : int -> int -> uioh
377 method infochanged : infochange -> unit
378 method scrollpw : (int * float * float)
379 method scrollph : (int * float * float)
380 method modehash : keyhash
381 end;;
383 type mode =
384 | Birdseye of (conf * leftx * pageno * pageno * anchor)
385 | Textentry of (textentry * onleave)
386 | View
387 | LinkNav of linktarget
388 and onleave = leavetextentrystatus -> unit
389 and leavetextentrystatus = | Cancel | Confirm
390 and helpitem = string * int * action
391 and action =
392 | Noaction
393 | Action of (uioh -> uioh)
394 and linktarget =
395 | Ltexact of (pageno * int)
396 | Ltgendir of int
399 let isbirdseye = function Birdseye _ -> true | _ -> false;;
400 let istextentry = function Textentry _ -> true | _ -> false;;
402 type currently =
403 | Idle
404 | Loading of (page * gen)
405 | Tiling of (
406 page * opaque * colorspace * angle * gen * col * row * width * height
408 | Outlining of outline list
411 let emptykeyhash = Hashtbl.create 0;;
412 let nouioh : uioh = object (self)
413 method display = ()
414 method key _ _ = self
415 method button _ _ _ _ _ = self
416 method motion _ _ = self
417 method pmotion _ _ = self
418 method infochanged _ = ()
419 method scrollpw = (0, nan, nan)
420 method scrollph = (0, nan, nan)
421 method modehash = emptykeyhash
422 end;;
424 type state =
425 { mutable sr : Unix.file_descr
426 ; mutable sw : Unix.file_descr
427 ; mutable wsfd : Unix.file_descr
428 ; mutable errfd : Unix.file_descr option
429 ; mutable stderr : Unix.file_descr
430 ; mutable errmsgs : Buffer.t
431 ; mutable newerrmsgs : bool
432 ; mutable w : int
433 ; mutable x : int
434 ; mutable y : int
435 ; mutable scrollw : int
436 ; mutable hscrollh : int
437 ; mutable anchor : anchor
438 ; mutable ranchors : (string * string * anchor) list
439 ; mutable maxy : int
440 ; mutable layout : page list
441 ; pagemap : (pagemapkey, opaque) Hashtbl.t
442 ; tilemap : (tilemapkey, tile) Hashtbl.t
443 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
444 ; mutable pdims : (pageno * width * height * leftx) list
445 ; mutable pagecount : int
446 ; mutable currently : currently
447 ; mutable mstate : mstate
448 ; mutable searchpattern : string
449 ; mutable rects : (pageno * recttype * rect) list
450 ; mutable rects1 : (pageno * recttype * rect) list
451 ; mutable text : string
452 ; mutable fullscreen : (width * height) option
453 ; mutable mode : mode
454 ; mutable uioh : uioh
455 ; mutable outlines : outline array
456 ; mutable bookmarks : outline list
457 ; mutable path : string
458 ; mutable password : string
459 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
460 ; mutable memused : memsize
461 ; mutable gen : gen
462 ; mutable throttle : (page list * int * float) option
463 ; mutable autoscroll : int option
464 ; mutable ghyll : (int option -> unit)
465 ; mutable help : helpitem array
466 ; mutable docinfo : (int * string) list
467 ; mutable texid : GlTex.texture_id option
468 ; hists : hists
469 ; mutable prevzoom : float
470 ; mutable progress : float
471 ; mutable redisplay : bool
472 ; mutable mpos : mpos
473 ; mutable keystate : keystate
474 ; mutable glinks : bool
475 ; mutable prevcolumns : (columns * float) option
477 and hists =
478 { pat : string circbuf
479 ; pag : string circbuf
480 ; nav : anchor circbuf
481 ; sel : string circbuf
485 let defconf =
486 { scrollbw = 7
487 ; scrollh = 12
488 ; icase = true
489 ; preload = true
490 ; pagebias = 0
491 ; verbose = false
492 ; debug = false
493 ; scrollstep = 24
494 ; hscrollstep = 24
495 ; maxhfit = true
496 ; crophack = false
497 ; autoscrollstep = 2
498 ; maxwait = None
499 ; hlinks = false
500 ; underinfo = false
501 ; interpagespace = 2
502 ; zoom = 1.0
503 ; presentation = false
504 ; angle = 0
505 ; winw = 900
506 ; winh = 900
507 ; savebmarks = true
508 ; proportional = true
509 ; trimmargins = false
510 ; trimfuzz = (0,0,0,0)
511 ; memlimit = 32 lsl 20
512 ; texcount = 256
513 ; sliceheight = 24
514 ; thumbw = 76
515 ; jumpback = true
516 ; bgcolor = (0.5, 0.5, 0.5)
517 ; bedefault = false
518 ; scrollbarinpm = true
519 ; tilew = 2048
520 ; tileh = 2048
521 ; mustoresize = 256 lsl 20
522 ; checkers = true
523 ; aalevel = 8
524 ; urilauncher =
525 (match platform with
526 | Plinux | Pfreebsd | Pdragonflybsd
527 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
528 | Posx -> "open \"%s\""
529 | Pcygwin -> "cygstart \"%s\""
530 | Punknown -> "echo %s")
531 ; pathlauncher = "lp \"%s\""
532 ; selcmd =
533 (match platform with
534 | Plinux | Pfreebsd | Pdragonflybsd
535 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
536 | Posx -> "pbcopy"
537 | Pcygwin -> "wsel"
538 | Punknown -> "cat")
539 ; colorspace = Rgb
540 ; invert = false
541 ; colorscale = 1.0
542 ; redirectstderr = false
543 ; ghyllscroll = None
544 ; columns = Csingle [||]
545 ; beyecolumns = None
546 ; updatecurs = false
547 ; hfsize = 12
548 ; pgscale = 1.0
549 ; usepbo = false
550 ; keyhashes =
551 let mk n = (n, Hashtbl.create 1) in
552 [ mk "global"
553 ; mk "info"
554 ; mk "help"
555 ; mk "outline"
556 ; mk "listview"
557 ; mk "birdseye"
558 ; mk "textentry"
559 ; mk "links"
560 ; mk "view"
565 let findkeyhash c name =
566 try List.assoc name c.keyhashes
567 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
570 let conf = { defconf with angle = defconf.angle };;
572 let pgscale h = truncate (float h *. conf.pgscale);;
574 type fontstate =
575 { mutable fontsize : int
576 ; mutable wwidth : float
577 ; mutable maxrows : int
581 let fstate =
582 { fontsize = 14
583 ; wwidth = nan
584 ; maxrows = -1
588 let setfontsize n =
589 fstate.fontsize <- n;
590 fstate.wwidth <- measurestr fstate.fontsize "w";
591 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
594 let geturl s =
595 let colonpos = try String.index s ':' with Not_found -> -1 in
596 let len = String.length s in
597 if colonpos >= 0 && colonpos + 3 < len
598 then (
599 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
600 then
601 let schemestartpos =
602 try String.rindex_from s colonpos ' '
603 with Not_found -> -1
605 let scheme =
606 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
608 match scheme with
609 | "http" | "ftp" | "mailto" ->
610 let epos =
611 try String.index_from s colonpos ' '
612 with Not_found -> len
614 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
615 | _ -> ""
616 else ""
618 else ""
621 let gotouri uri =
622 if String.length conf.urilauncher = 0
623 then print_endline uri
624 else (
625 let url = geturl uri in
626 if String.length url = 0
627 then print_endline uri
628 else
629 let re = Str.regexp "%s" in
630 let command = Str.global_replace re url conf.urilauncher in
631 try popen command []
632 with exn ->
633 Printf.eprintf
634 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
635 flush stderr;
639 let version () =
640 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
641 (platform_to_string platform) Sys.word_size Sys.ocaml_version
644 let makehelp () =
645 let strings = version () :: "" :: Help.keys in
646 Array.of_list (
647 List.map (fun s ->
648 let url = geturl s in
649 if String.length url > 0
650 then (s, 0, Action (fun u -> gotouri url; u))
651 else (s, 0, Noaction)
652 ) strings);
655 let noghyll _ = ();;
656 let firstgeomcmds = "", [];;
658 let state =
659 { sr = Unix.stdin
660 ; sw = Unix.stdin
661 ; wsfd = Unix.stdin
662 ; errfd = None
663 ; stderr = Unix.stderr
664 ; errmsgs = Buffer.create 0
665 ; newerrmsgs = false
666 ; x = 0
667 ; y = 0
668 ; w = 0
669 ; scrollw = 0
670 ; hscrollh = 0
671 ; anchor = emptyanchor
672 ; ranchors = []
673 ; layout = []
674 ; maxy = max_int
675 ; tilelru = Queue.create ()
676 ; pagemap = Hashtbl.create 10
677 ; tilemap = Hashtbl.create 10
678 ; pdims = []
679 ; pagecount = 0
680 ; currently = Idle
681 ; mstate = Mnone
682 ; rects = []
683 ; rects1 = []
684 ; text = ""
685 ; mode = View
686 ; fullscreen = None
687 ; searchpattern = ""
688 ; outlines = [||]
689 ; bookmarks = []
690 ; path = ""
691 ; password = ""
692 ; geomcmds = firstgeomcmds
693 ; hists =
694 { nav = cbnew 10 emptyanchor
695 ; pat = cbnew 10 ""
696 ; pag = cbnew 10 ""
697 ; sel = cbnew 10 ""
699 ; memused = 0
700 ; gen = 0
701 ; throttle = None
702 ; autoscroll = None
703 ; ghyll = noghyll
704 ; help = makehelp ()
705 ; docinfo = []
706 ; texid = None
707 ; prevzoom = 1.0
708 ; progress = -1.0
709 ; uioh = nouioh
710 ; redisplay = true
711 ; mpos = (-1, -1)
712 ; keystate = KSnone
713 ; glinks = false
714 ; prevcolumns = None
718 let vlog fmt =
719 if conf.verbose
720 then
721 Printf.kprintf prerr_endline fmt
722 else
723 Printf.kprintf ignore fmt
726 let launchpath () =
727 if String.length conf.pathlauncher = 0
728 then print_endline state.path
729 else (
730 let re = Str.regexp "%s" in
731 let command = Str.global_replace re state.path conf.pathlauncher in
732 try popen command []
733 with exn ->
734 Printf.eprintf
735 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
736 flush stderr;
740 module Ne = struct
741 type 'a t = | Res of 'a | Exn of exn;;
743 let pipe () =
744 try Res (Unix.pipe ())
745 with exn -> Exn exn
748 let clo fd f =
749 try Unix.close fd
750 with exn -> f (Printexc.to_string exn)
753 let dup fd =
754 try Res (Unix.dup fd)
755 with exn -> Exn exn
758 let dup2 fd1 fd2 =
759 try Res (Unix.dup2 fd1 fd2)
760 with exn -> Exn exn
762 end;;
764 let redirectstderr () =
765 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
766 if conf.redirectstderr
767 then
768 match Ne.pipe () with
769 | Ne.Exn exn ->
770 dolog "failed to create stderr redirection pipes: %s"
771 (Printexc.to_string exn)
773 | Ne.Res (r, w) ->
774 begin match Ne.dup Unix.stderr with
775 | Ne.Exn exn ->
776 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
777 Ne.clo r (clofail "pipe/r");
778 Ne.clo w (clofail "pipe/w");
780 | Ne.Res dupstderr ->
781 begin match Ne.dup2 w Unix.stderr with
782 | Ne.Exn exn ->
783 dolog "failed to dup2 to stderr: %s"
784 (Printexc.to_string exn);
785 Ne.clo dupstderr (clofail "stderr duplicate");
786 Ne.clo r (clofail "redir pipe/r");
787 Ne.clo w (clofail "redir pipe/w");
789 | Ne.Res () ->
790 state.stderr <- dupstderr;
791 state.errfd <- Some r;
792 end;
794 else (
795 state.newerrmsgs <- false;
796 begin match state.errfd with
797 | Some fd ->
798 begin match Ne.dup2 state.stderr Unix.stderr with
799 | Ne.Exn exn ->
800 dolog "failed to dup2 original stderr: %s"
801 (Printexc.to_string exn)
802 | Ne.Res () ->
803 Ne.clo fd (clofail "dup of stderr");
804 Unix.dup2 state.stderr Unix.stderr;
805 state.errfd <- None;
806 end;
807 | None -> ()
808 end;
809 prerr_string (Buffer.contents state.errmsgs);
810 flush stderr;
811 Buffer.clear state.errmsgs;
815 module G =
816 struct
817 let postRedisplay who =
818 if conf.verbose
819 then prerr_endline ("redisplay for " ^ who);
820 state.redisplay <- true;
822 end;;
824 let getopaque pageno =
825 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
826 with Not_found -> None
829 let putopaque pageno opaque =
830 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
833 let pagetranslatepoint l x y =
834 let dy = y - l.pagedispy in
835 let y = dy + l.pagey in
836 let dx = x - l.pagedispx in
837 let x = dx + l.pagex in
838 (x, y);
841 let getunder x y =
842 let rec f = function
843 | l :: rest ->
844 begin match getopaque l.pageno with
845 | Some opaque ->
846 let x0 = l.pagedispx in
847 let x1 = x0 + l.pagevw in
848 let y0 = l.pagedispy in
849 let y1 = y0 + l.pagevh in
850 if y >= y0 && y <= y1 && x >= x0 && x <= x1
851 then
852 let px, py = pagetranslatepoint l x y in
853 match whatsunder opaque px py with
854 | Unone -> f rest
855 | under -> under
856 else f rest
857 | _ ->
858 f rest
860 | [] -> Unone
862 f state.layout
865 let showtext c s =
866 state.text <- Printf.sprintf "%c%s" c s;
867 G.postRedisplay "showtext";
870 let undertext = function
871 | Unone -> "none"
872 | Ulinkuri s -> s
873 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
874 | Utext s -> "font: " ^ s
875 | Uunexpected s -> "unexpected: " ^ s
876 | Ulaunch s -> "launch: " ^ s
877 | Unamed s -> "named: " ^ s
878 | Uremote (filename, pageno) ->
879 Printf.sprintf "%s: page %d" filename (pageno+1)
882 let updateunder x y =
883 match getunder x y with
884 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
885 | Ulinkuri uri ->
886 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
887 Wsi.setcursor Wsi.CURSOR_INFO
888 | Ulinkgoto (pageno, _) ->
889 if conf.underinfo
890 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
891 Wsi.setcursor Wsi.CURSOR_INFO
892 | Utext s ->
893 if conf.underinfo then showtext 'f' ("ont: " ^ s);
894 Wsi.setcursor Wsi.CURSOR_TEXT
895 | Uunexpected s ->
896 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
897 Wsi.setcursor Wsi.CURSOR_INHERIT
898 | Ulaunch s ->
899 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
900 Wsi.setcursor Wsi.CURSOR_INHERIT
901 | Unamed s ->
902 if conf.underinfo then showtext 'n' ("amed: " ^ s);
903 Wsi.setcursor Wsi.CURSOR_INHERIT
904 | Uremote (filename, pageno) ->
905 if conf.underinfo then showtext 'r'
906 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
907 Wsi.setcursor Wsi.CURSOR_INFO
910 let showlinktype under =
911 if conf.underinfo
912 then
913 match under with
914 | Unone -> ()
915 | under ->
916 let s = undertext under in
917 showtext ' ' s
920 let addchar s c =
921 let b = Buffer.create (String.length s + 1) in
922 Buffer.add_string b s;
923 Buffer.add_char b c;
924 Buffer.contents b;
927 let colorspace_of_string s =
928 match String.lowercase s with
929 | "rgb" -> Rgb
930 | "bgr" -> Bgr
931 | "gray" -> Gray
932 | _ -> failwith "invalid colorspace"
935 let int_of_colorspace = function
936 | Rgb -> 0
937 | Bgr -> 1
938 | Gray -> 2
941 let colorspace_of_int = function
942 | 0 -> Rgb
943 | 1 -> Bgr
944 | 2 -> Gray
945 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
948 let colorspace_to_string = function
949 | Rgb -> "rgb"
950 | Bgr -> "bgr"
951 | Gray -> "gray"
954 let intentry_with_suffix text key =
955 let c =
956 if key >= 32 && key < 127
957 then Char.chr key
958 else '\000'
960 match Char.lowercase c with
961 | '0' .. '9' ->
962 let text = addchar text c in
963 TEcont text
965 | 'k' | 'm' | 'g' ->
966 let text = addchar text c in
967 TEcont text
969 | _ ->
970 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
971 TEcont text
974 let multicolumns_to_string (n, a, b) =
975 if a = 0 && b = 0
976 then Printf.sprintf "%d" n
977 else Printf.sprintf "%d,%d,%d" n a b;
980 let multicolumns_of_string s =
982 (int_of_string s, 0, 0)
983 with _ ->
984 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
985 if a > 1 || b > 1
986 then failwith "subtly broken"; (n, a, b)
990 let readcmd fd =
991 let s = "xxxx" in
992 let n = Unix.read fd s 0 4 in
993 if n != 4 then failwith "incomplete read(len)";
994 let len = 0
995 lor (Char.code s.[0] lsl 24)
996 lor (Char.code s.[1] lsl 16)
997 lor (Char.code s.[2] lsl 8)
998 lor (Char.code s.[3] lsl 0)
1000 let s = String.create len in
1001 let n = Unix.read fd s 0 len in
1002 if n != len then failwith "incomplete read(data)";
1006 let btod b = if b then 1 else 0;;
1008 let wcmd fmt =
1009 let b = Buffer.create 16 in
1010 Buffer.add_string b "llll";
1011 Printf.kbprintf
1012 (fun b ->
1013 let s = Buffer.contents b in
1014 let n = String.length s in
1015 let len = n - 4 in
1016 (* dolog "wcmd %S" (String.sub s 4 len); *)
1017 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1018 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1019 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1020 s.[3] <- Char.chr (len land 0xff);
1021 let n' = Unix.write state.sw s 0 n in
1022 if n' != n then failwith "write failed";
1023 ) b fmt;
1026 let calcips h =
1027 let d = conf.winh - h in
1028 max conf.interpagespace ((d + 1) / 2)
1031 let rowyh (c, coverA, coverB) b n =
1032 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1033 then
1034 let _, _, vy, (_, _, h, _) = b.(n) in
1035 (vy, h)
1036 else
1037 let n' = n - coverA in
1038 let d = n' mod c in
1039 let s = n - d in
1040 let e = min state.pagecount (s + c) in
1041 let rec find m miny maxh = if m = e then miny, maxh else
1042 let _, _, y, (_, _, h, _) = b.(m) in
1043 let miny = min miny y in
1044 let maxh = max maxh h in
1045 find (m+1) miny maxh
1046 in find s max_int 0
1049 let calcheight () =
1050 match conf.columns with
1051 | Cmulti ((_, _, _) as cl, b) ->
1052 if Array.length b > 0
1053 then
1054 let y, h = rowyh cl b (Array.length b - 1) in
1055 y + h + (if conf.presentation then calcips h else 0)
1056 else 0
1057 | Csingle b ->
1058 if Array.length b > 0
1059 then
1060 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1061 y + h + (if conf.presentation then calcips h else 0)
1062 else 0
1063 | Csplit (_, b) ->
1064 if Array.length b > 0
1065 then
1066 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1067 y + h
1068 else 0
1071 let getpageyh pageno =
1072 let pageno = bound pageno 0 (state.pagecount-1) in
1073 match conf.columns with
1074 | Csingle b ->
1075 if Array.length b = 0
1076 then 0, 0
1077 else
1078 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1079 let y =
1080 if conf.presentation
1081 then y - calcips h
1082 else y
1084 y, h
1085 | Cmulti (cl, b) ->
1086 if Array.length b = 0
1087 then 0, 0
1088 else
1089 let y, h = rowyh cl b pageno in
1090 let y =
1091 if conf.presentation
1092 then y - calcips h
1093 else y
1095 y, h
1096 | Csplit (c, b) ->
1097 if Array.length b = 0
1098 then 0, 0
1099 else
1100 let n = pageno*c in
1101 let (_, _, y, (_, _, h, _)) = b.(n) in
1102 y, h
1105 let getpagedim pageno =
1106 let rec f ppdim l =
1107 match l with
1108 | (n, _, _, _) as pdim :: rest ->
1109 if n >= pageno
1110 then (if n = pageno then pdim else ppdim)
1111 else f pdim rest
1113 | [] -> ppdim
1115 f (-1, -1, -1, -1) state.pdims
1118 let getpagey pageno = fst (getpageyh pageno);;
1120 let nogeomcmds cmds =
1121 match cmds with
1122 | s, [] -> String.length s = 0
1123 | _ -> false
1126 let page_of_y y =
1127 let ((c, coverA, coverB) as cl), b =
1128 match conf.columns with
1129 | Csingle b -> (1, 0, 0), b
1130 | Cmulti (c, b) -> c, b
1131 | Csplit (_, b) -> (1, 0, 0), b
1133 let rec bsearch nmin nmax =
1134 if nmin > nmax
1135 then bound nmin 0 (state.pagecount-1)
1136 else
1137 let n = (nmax + nmin) / 2 in
1138 let vy, h = rowyh cl b n in
1139 let y0, y1 =
1140 if conf.presentation
1141 then
1142 let ips = calcips h in
1143 let y0 = vy - ips in
1144 let y1 = vy + h + ips in
1145 y0, y1
1146 else (
1147 if n = 0
1148 then 0, vy + h + conf.interpagespace
1149 else
1150 let y0 = vy - conf.interpagespace in
1151 y0, y0 + h + conf.interpagespace
1154 if y >= y0 && y < y1
1155 then (
1156 if c = 1
1157 then n
1158 else (
1159 if n > coverA
1160 then
1161 if n < state.pagecount - coverB
1162 then ((n-coverA)/c)*c + coverA
1163 else n
1164 else n
1167 else (
1168 if y > y0
1169 then bsearch (n+1) nmax
1170 else bsearch nmin (n-1)
1173 let r = bsearch 0 (state.pagecount-1) in
1177 let layoutN ((columns, coverA, coverB), b) y sh =
1178 let sh = sh - state.hscrollh in
1179 let rec fold accu n =
1180 if n = Array.length b
1181 then accu
1182 else
1183 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1184 if (vy - y) > sh &&
1185 (n = coverA - 1
1186 || n = state.pagecount - coverB
1187 || (n - coverA) mod columns = columns - 1)
1188 then accu
1189 else
1190 let accu =
1191 if vy + h > y
1192 then
1193 let pagey = max 0 (y - vy) in
1194 let pagedispy = if pagey > 0 then 0 else vy - y in
1195 let pagedispx, pagex =
1196 let pdx =
1197 if n = coverA - 1 || n = state.pagecount - coverB
1198 then state.x + (conf.winw - state.scrollw - w) / 2
1199 else dx + xoff + state.x
1201 if pdx < 0
1202 then 0, -pdx
1203 else pdx, 0
1205 let pagevw =
1206 let vw = conf.winw - state.scrollw - pagedispx in
1207 let pw = w - pagex in
1208 min vw pw
1210 let pagevh = min (h - pagey) (sh - pagedispy) in
1211 if pagevw > 0 && pagevh > 0
1212 then
1213 let e =
1214 { pageno = n
1215 ; pagedimno = pdimno
1216 ; pagew = w
1217 ; pageh = h
1218 ; pagex = pagex
1219 ; pagey = pagey
1220 ; pagevw = pagevw
1221 ; pagevh = pagevh
1222 ; pagedispx = pagedispx
1223 ; pagedispy = pagedispy
1224 ; pagecol = 0
1227 e :: accu
1228 else
1229 accu
1230 else
1231 accu
1233 fold accu (n+1)
1235 List.rev (fold [] (page_of_y y));
1238 let layoutS (columns, b) y sh =
1239 let sh = sh - state.hscrollh in
1240 let rec fold accu n =
1241 if n = Array.length b
1242 then accu
1243 else
1244 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1245 if (vy - y) > sh
1246 then accu
1247 else
1248 let accu =
1249 if vy + pageh > y
1250 then
1251 let x = xoff + state.x in
1252 let pagey = max 0 (y - vy) in
1253 let pagedispy = if pagey > 0 then 0 else vy - y in
1254 let pagedispx, pagex =
1255 if px = 0
1256 then (
1257 if x < 0
1258 then 0, -x
1259 else x, 0
1261 else (
1262 let px = px - x in
1263 if px < 0
1264 then -px, 0
1265 else 0, px
1268 let pagecolw = pagew/columns in
1269 let pagedispx =
1270 if pagecolw < conf.winw
1271 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1272 else pagedispx
1274 let pagevw =
1275 let vw = conf.winw - pagedispx - state.scrollw in
1276 let pw = pagew - pagex in
1277 min vw pw
1279 let pagevw = min pagevw pagecolw in
1280 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1281 if pagevw > 0 && pagevh > 0
1282 then
1283 let e =
1284 { pageno = n/columns
1285 ; pagedimno = pdimno
1286 ; pagew = pagew
1287 ; pageh = pageh
1288 ; pagex = pagex
1289 ; pagey = pagey
1290 ; pagevw = pagevw
1291 ; pagevh = pagevh
1292 ; pagedispx = pagedispx
1293 ; pagedispy = pagedispy
1294 ; pagecol = n mod columns
1297 e :: accu
1298 else
1299 accu
1300 else
1301 accu
1303 fold accu (n+1)
1305 List.rev (fold [] 0)
1308 let layout y sh =
1309 if nogeomcmds state.geomcmds
1310 then
1311 match conf.columns with
1312 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1313 | Cmulti c -> layoutN c y sh
1314 | Csplit s -> layoutS s y sh
1315 else []
1318 let clamp incr =
1319 let y = state.y + incr in
1320 let y = max 0 y in
1321 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1325 let itertiles l f =
1326 let tilex = l.pagex mod conf.tilew in
1327 let tiley = l.pagey mod conf.tileh in
1329 let col = l.pagex / conf.tilew in
1330 let row = l.pagey / conf.tileh in
1332 let rec rowloop row y0 dispy h =
1333 if h = 0
1334 then ()
1335 else (
1336 let dh = conf.tileh - y0 in
1337 let dh = min h dh in
1338 let rec colloop col x0 dispx w =
1339 if w = 0
1340 then ()
1341 else (
1342 let dw = conf.tilew - x0 in
1343 let dw = min w dw in
1345 f col row dispx dispy x0 y0 dw dh;
1346 colloop (col+1) 0 (dispx+dw) (w-dw)
1349 colloop col tilex l.pagedispx l.pagevw;
1350 rowloop (row+1) 0 (dispy+dh) (h-dh)
1353 if l.pagevw > 0 && l.pagevh > 0
1354 then rowloop row tiley l.pagedispy l.pagevh;
1357 let gettileopaque l col row =
1358 let key =
1359 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1361 try Some (Hashtbl.find state.tilemap key)
1362 with Not_found -> None
1365 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1366 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1367 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1370 let drawtiles l color =
1371 GlDraw.color color;
1372 let f col row x y tilex tiley w h =
1373 match gettileopaque l col row with
1374 | Some (opaque, _, t) ->
1375 let params = x, y, w, h, tilex, tiley in
1376 if conf.invert
1377 then (
1378 Gl.enable `blend;
1379 GlFunc.blend_func `zero `one_minus_src_color;
1381 drawtile params opaque;
1382 if conf.invert
1383 then Gl.disable `blend;
1384 if conf.debug
1385 then (
1386 let s = Printf.sprintf
1387 "%d[%d,%d] %f sec"
1388 l.pageno col row t
1390 let w = measurestr fstate.fontsize s in
1391 GlMisc.push_attrib [`current];
1392 GlDraw.color (0.0, 0.0, 0.0);
1393 GlDraw.rect
1394 (float (x-2), float (y-2))
1395 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1396 GlDraw.color (1.0, 1.0, 1.0);
1397 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1398 GlMisc.pop_attrib ();
1401 | _ ->
1402 let w =
1403 let lw = conf.winw - state.scrollw - x in
1404 min lw w
1405 and h =
1406 let lh = conf.winh - y in
1407 min lh h
1409 begin match state.texid with
1410 | Some id ->
1411 Gl.enable `texture_2d;
1412 GlTex.bind_texture `texture_2d id;
1413 let x0 = float x
1414 and y0 = float y
1415 and x1 = float (x+w)
1416 and y1 = float (y+h) in
1418 let tw = float w /. 16.0
1419 and th = float h /. 16.0 in
1420 let tx0 = float tilex /. 16.0
1421 and ty0 = float tiley /. 16.0 in
1422 let tx1 = tx0 +. tw
1423 and ty1 = ty0 +. th in
1424 GlDraw.begins `quads;
1425 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1426 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1427 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1428 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1429 GlDraw.ends ();
1431 Gl.disable `texture_2d;
1432 | None ->
1433 GlDraw.color (1.0, 1.0, 1.0);
1434 GlDraw.rect
1435 (float x, float y)
1436 (float (x+w), float (y+h));
1437 end;
1438 if w > 128 && h > fstate.fontsize + 10
1439 then (
1440 GlDraw.color (0.0, 0.0, 0.0);
1441 let c, r =
1442 if conf.verbose
1443 then (col*conf.tilew, row*conf.tileh)
1444 else col, row
1446 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1448 GlDraw.color color;
1450 itertiles l f
1453 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1455 let tilevisible1 l x y =
1456 let ax0 = l.pagex
1457 and ax1 = l.pagex + l.pagevw
1458 and ay0 = l.pagey
1459 and ay1 = l.pagey + l.pagevh in
1461 let bx0 = x
1462 and by0 = y in
1463 let bx1 = min (bx0 + conf.tilew) l.pagew
1464 and by1 = min (by0 + conf.tileh) l.pageh in
1466 let rx0 = max ax0 bx0
1467 and ry0 = max ay0 by0
1468 and rx1 = min ax1 bx1
1469 and ry1 = min ay1 by1 in
1471 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1472 nonemptyintersection
1475 let tilevisible layout n x y =
1476 let rec findpageinlayout m = function
1477 | l :: rest when l.pageno = n ->
1478 tilevisible1 l x y || (
1479 match conf.columns with
1480 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1481 | _ -> false
1483 | _ :: rest -> findpageinlayout 0 rest
1484 | [] -> false
1486 findpageinlayout 0 layout;
1489 let tileready l x y =
1490 tilevisible1 l x y &&
1491 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1494 let tilepage n p layout =
1495 let rec loop = function
1496 | l :: rest ->
1497 if l.pageno = n
1498 then
1499 let f col row _ _ _ _ _ _ =
1500 if state.currently = Idle
1501 then
1502 match gettileopaque l col row with
1503 | Some _ -> ()
1504 | None ->
1505 let x = col*conf.tilew
1506 and y = row*conf.tileh in
1507 let w =
1508 let w = l.pagew - x in
1509 min w conf.tilew
1511 let h =
1512 let h = l.pageh - y in
1513 min h conf.tileh
1515 let pbo =
1516 if conf.usepbo
1517 then getpbo w h conf.colorspace
1518 else "0"
1520 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1521 state.currently <-
1522 Tiling (
1523 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1524 conf.tilew, conf.tileh
1527 itertiles l f;
1528 else
1529 loop rest
1531 | [] -> ()
1533 if nogeomcmds state.geomcmds
1534 then loop layout;
1537 let preloadlayout y =
1538 let y = if y < conf.winh then 0 else y - conf.winh in
1539 let h = conf.winh*3 in
1540 layout y h;
1543 let load pages =
1544 let rec loop pages =
1545 if state.currently != Idle
1546 then ()
1547 else
1548 match pages with
1549 | l :: rest ->
1550 begin match getopaque l.pageno with
1551 | None ->
1552 wcmd "page %d %d" l.pageno l.pagedimno;
1553 state.currently <- Loading (l, state.gen);
1554 | Some opaque ->
1555 tilepage l.pageno opaque pages;
1556 loop rest
1557 end;
1558 | _ -> ()
1560 if nogeomcmds state.geomcmds
1561 then loop pages
1564 let preload pages =
1565 load pages;
1566 if conf.preload && state.currently = Idle
1567 then load (preloadlayout state.y);
1570 let layoutready layout =
1571 let rec fold all ls =
1572 all && match ls with
1573 | l :: rest ->
1574 let seen = ref false in
1575 let allvisible = ref true in
1576 let foo col row _ _ _ _ _ _ =
1577 seen := true;
1578 allvisible := !allvisible &&
1579 begin match gettileopaque l col row with
1580 | Some _ -> true
1581 | None -> false
1584 itertiles l foo;
1585 fold (!seen && !allvisible) rest
1586 | [] -> true
1588 let alltilesvisible = fold true layout in
1589 alltilesvisible;
1592 let gotoy y =
1593 let y = bound y 0 state.maxy in
1594 let y, layout, proceed =
1595 match conf.maxwait with
1596 | Some time when state.ghyll == noghyll ->
1597 begin match state.throttle with
1598 | None ->
1599 let layout = layout y conf.winh in
1600 let ready = layoutready layout in
1601 if not ready
1602 then (
1603 load layout;
1604 state.throttle <- Some (layout, y, now ());
1606 else G.postRedisplay "gotoy showall (None)";
1607 y, layout, ready
1608 | Some (_, _, started) ->
1609 let dt = now () -. started in
1610 if dt > time
1611 then (
1612 state.throttle <- None;
1613 let layout = layout y conf.winh in
1614 load layout;
1615 G.postRedisplay "maxwait";
1616 y, layout, true
1618 else -1, [], false
1621 | _ ->
1622 let layout = layout y conf.winh in
1623 if true || layoutready layout
1624 then G.postRedisplay "gotoy ready";
1625 y, layout, true
1627 if proceed
1628 then (
1629 state.y <- y;
1630 state.layout <- layout;
1631 begin match state.mode with
1632 | LinkNav (Ltexact (pageno, linkno)) ->
1633 let rec loop = function
1634 | [] ->
1635 state.mode <- LinkNav (Ltgendir 0)
1636 | l :: _ when l.pageno = pageno ->
1637 begin match getopaque pageno with
1638 | None ->
1639 state.mode <- LinkNav (Ltgendir 0)
1640 | Some opaque ->
1641 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1642 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1643 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1644 then state.mode <- LinkNav (Ltgendir 0)
1646 | _ :: rest -> loop rest
1648 loop layout
1649 | _ -> ()
1650 end;
1651 begin match state.mode with
1652 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1653 if not (pagevisible layout pageno)
1654 then (
1655 match state.layout with
1656 | [] -> ()
1657 | l :: _ ->
1658 state.mode <- Birdseye (
1659 conf, leftx, l.pageno, hooverpageno, anchor
1662 | LinkNav (Ltgendir dir as lt) ->
1663 let linknav =
1664 let rec loop = function
1665 | [] -> lt
1666 | l :: rest ->
1667 match getopaque l.pageno with
1668 | None -> loop rest
1669 | Some opaque ->
1670 let link =
1671 let ld =
1672 if dir = 0
1673 then LDfirstvisible (l.pagex, l.pagey, dir)
1674 else (
1675 if dir > 0 then LDfirst else LDlast
1678 findlink opaque ld
1680 match link with
1681 | Lnotfound -> loop rest
1682 | Lfound n ->
1683 showlinktype (getlink opaque n);
1684 Ltexact (l.pageno, n)
1686 loop state.layout
1688 state.mode <- LinkNav linknav
1689 | _ -> ()
1690 end;
1691 preload layout;
1693 state.ghyll <- noghyll;
1694 if conf.updatecurs
1695 then (
1696 let mx, my = state.mpos in
1697 updateunder mx my;
1701 let conttiling pageno opaque =
1702 tilepage pageno opaque
1703 (if conf.preload then preloadlayout state.y else state.layout)
1706 let gotoy_and_clear_text y =
1707 if not conf.verbose then state.text <- "";
1708 gotoy y;
1711 let getanchor1 l =
1712 let top =
1713 let coloff = l.pagecol * l.pageh in
1714 float (l.pagey + coloff) /. float l.pageh
1716 let dtop =
1717 if l.pagedispy = 0
1718 then
1720 else
1721 if conf.presentation
1722 then float l.pagedispy /. float (calcips l.pageh)
1723 else float l.pagedispy /. float conf.interpagespace
1725 (l.pageno, top, dtop)
1728 let getanchor () =
1729 match state.layout with
1730 | l :: _ -> getanchor1 l
1731 | [] ->
1732 let n = page_of_y state.y in
1733 let y, h = getpageyh n in
1734 let dy = y - state.y in
1735 let dtop =
1736 if conf.presentation
1737 then
1738 let ips = calcips h in
1739 float (dy + ips) /. float ips
1740 else
1741 float dy /. float conf.interpagespace
1743 (n, 0.0, dtop)
1746 let getanchory (n, top, dtop) =
1747 let y, h = getpageyh n in
1748 if conf.presentation
1749 then
1750 let ips = calcips h in
1751 y + truncate (top*.float h -. dtop*.float ips) + ips;
1752 else
1753 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1756 let gotoanchor anchor =
1757 gotoy (getanchory anchor);
1760 let addnav () =
1761 cbput state.hists.nav (getanchor ());
1764 let getnav dir =
1765 let anchor = cbgetc state.hists.nav dir in
1766 getanchory anchor;
1769 let gotoghyll y =
1770 let scroll f n a b =
1771 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1772 let snake f a b =
1773 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1774 if f < a
1775 then s (float f /. float a)
1776 else (
1777 if f > b
1778 then 1.0 -. s ((float (f-b) /. float (n-b)))
1779 else 1.0
1782 snake f a b
1783 and summa f n a b =
1784 (* courtesy:
1785 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1786 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1787 let iv1 = iv f in
1788 let ins = float a *. iv1
1789 and outs = float (n-b) *. iv1 in
1790 let ones = b - a in
1791 ins +. outs +. float ones
1793 let rec set (_N, _A, _B) y sy =
1794 let sum = summa 1.0 _N _A _B in
1795 let dy = float (y - sy) in
1796 state.ghyll <- (
1797 let rec gf n y1 o =
1798 if n >= _N
1799 then state.ghyll <- noghyll
1800 else
1801 let go n =
1802 let s = scroll n _N _A _B in
1803 let y1 = y1 +. ((s *. dy) /. sum) in
1804 gotoy_and_clear_text (truncate y1);
1805 state.ghyll <- gf (n+1) y1;
1807 match o with
1808 | None -> go n
1809 | Some y' -> set (_N/2, 1, 1) y' state.y
1811 gf 0 (float state.y)
1814 match conf.ghyllscroll with
1815 | None ->
1816 gotoy_and_clear_text y
1817 | Some nab ->
1818 if state.ghyll == noghyll
1819 then set nab y state.y
1820 else state.ghyll (Some y)
1823 let gotopage n top =
1824 let y, h = getpageyh n in
1825 let y = y + (truncate (top *. float h)) in
1826 gotoghyll y
1829 let gotopage1 n top =
1830 let y = getpagey n in
1831 let y = y + top in
1832 gotoghyll y
1835 let invalidate s f =
1836 state.layout <- [];
1837 state.pdims <- [];
1838 state.rects <- [];
1839 state.rects1 <- [];
1840 match state.geomcmds with
1841 | ps, [] when String.length ps = 0 ->
1842 f ();
1843 state.geomcmds <- s, [];
1845 | ps, [] ->
1846 state.geomcmds <- ps, [s, f];
1848 | ps, (s', _) :: rest when s' = s ->
1849 state.geomcmds <- ps, ((s, f) :: rest);
1851 | ps, cmds ->
1852 state.geomcmds <- ps, ((s, f) :: cmds);
1855 let flushpages () =
1856 Hashtbl.iter (fun _ opaque ->
1857 wcmd "freepage %s" opaque;
1858 ) state.pagemap;
1859 Hashtbl.clear state.pagemap;
1862 let opendoc path password =
1863 state.path <- path;
1864 state.password <- password;
1865 state.gen <- state.gen + 1;
1866 state.docinfo <- [];
1868 flushpages ();
1869 setaalevel conf.aalevel;
1870 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1871 wcmd "open %s\000%s\000" path password;
1872 invalidate "reqlayout"
1873 (fun () ->
1874 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1877 let reload () =
1878 state.anchor <- getanchor ();
1879 opendoc state.path state.password;
1882 let scalecolor c =
1883 let c = c *. conf.colorscale in
1884 (c, c, c);
1887 let scalecolor2 (r, g, b) =
1888 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1891 let docolumns = function
1892 | Csingle _ ->
1893 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1894 let rec loop pageno pdimno pdim y ph pdims =
1895 if pageno = state.pagecount
1896 then ()
1897 else
1898 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1899 match pdims with
1900 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1901 pdimno+1, pdim, rest
1902 | _ ->
1903 pdimno, pdim, pdims
1905 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1906 let y = y +
1907 (if conf.presentation
1908 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1909 else (if pageno = 0 then 0 else conf.interpagespace)
1912 a.(pageno) <- (pdimno, x, y, pdim);
1913 loop (pageno+1) pdimno pdim (y + h) h pdims
1915 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1916 conf.columns <- Csingle a;
1918 | Cmulti ((columns, coverA, coverB), _) ->
1919 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1920 let rec loop pageno pdimno pdim x y rowh pdims =
1921 let rec fixrow m = if m = pageno then () else
1922 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1923 if h < rowh
1924 then (
1925 let y = y + (rowh - h) / 2 in
1926 a.(m) <- (pdimno, x, y, pdim);
1928 fixrow (m+1)
1930 if pageno = state.pagecount
1931 then fixrow (((pageno - 1) / columns) * columns)
1932 else
1933 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1934 match pdims with
1935 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1936 pdimno+1, pdim, rest
1937 | _ ->
1938 pdimno, pdim, pdims
1940 let x, y, rowh' =
1941 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1942 then (
1943 let x = (conf.winw - state.scrollw - w) / 2 in
1944 let ips =
1945 if conf.presentation then calcips h else conf.interpagespace in
1946 x, y + ips + rowh, h
1948 else (
1949 if (pageno - coverA) mod columns = 0
1950 then (
1951 let x = max 0 (conf.winw - state.scrollw - state.w) / 2 in
1952 let y =
1953 if conf.presentation
1954 then
1955 let ips = calcips h in
1956 y + (if pageno = 0 then 0 else calcips rowh + ips)
1957 else
1958 y + (if pageno = 0 then 0 else conf.interpagespace)
1960 x, y + rowh, h
1962 else x, y, max rowh h
1965 let y =
1966 if pageno > 1 && (pageno - coverA) mod columns = 0
1967 then (
1968 let y =
1969 if pageno = columns && conf.presentation
1970 then (
1971 let ips = calcips rowh in
1972 for i = 0 to pred columns
1974 let (pdimno, x, y, pdim) = a.(i) in
1975 a.(i) <- (pdimno, x, y+ips, pdim)
1976 done;
1977 y+ips;
1979 else y
1981 fixrow (pageno - columns);
1984 else y
1986 a.(pageno) <- (pdimno, x, y, pdim);
1987 let x = x + w + xoff*2 + conf.interpagespace in
1988 loop (pageno+1) pdimno pdim x y rowh' pdims
1990 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1991 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1993 | Csplit (c, _) ->
1994 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1995 let rec loop pageno pdimno pdim y pdims =
1996 if pageno = state.pagecount
1997 then ()
1998 else
1999 let pdimno, ((_, w, h, _) as pdim), pdims =
2000 match pdims with
2001 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2002 pdimno+1, pdim, rest
2003 | _ ->
2004 pdimno, pdim, pdims
2006 let cw = w / c in
2007 let rec loop1 n x y =
2008 if n = c then y else (
2009 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2010 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2013 let y = loop1 0 0 y in
2014 loop (pageno+1) pdimno pdim y pdims
2016 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2017 conf.columns <- Csplit (c, a);
2020 let represent () =
2021 docolumns conf.columns;
2022 state.maxy <- calcheight ();
2023 state.hscrollh <-
2024 if state.w <= conf.winw - state.scrollw
2025 then 0
2026 else state.scrollw
2028 match state.mode with
2029 | Birdseye (_, _, pageno, _, _) ->
2030 let y, h = getpageyh pageno in
2031 let top = (conf.winh - h) / 2 in
2032 gotoy (max 0 (y - top))
2033 | _ -> gotoanchor state.anchor
2036 let reshape w h =
2037 GlDraw.viewport 0 0 w h;
2038 let firsttime = state.geomcmds == firstgeomcmds in
2039 if not firsttime && nogeomcmds state.geomcmds
2040 then state.anchor <- getanchor ();
2042 conf.winw <- w;
2043 let w = truncate (float w *. conf.zoom) - state.scrollw in
2044 let w = max w 2 in
2045 conf.winh <- h;
2046 setfontsize fstate.fontsize;
2047 GlMat.mode `modelview;
2048 GlMat.load_identity ();
2050 GlMat.mode `projection;
2051 GlMat.load_identity ();
2052 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2053 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2054 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2056 let relx =
2057 if conf.zoom <= 1.0
2058 then 0.0
2059 else float state.x /. float state.w
2061 invalidate "geometry"
2062 (fun () ->
2063 state.w <- w;
2064 if not firsttime
2065 then state.x <- truncate (relx *. float w);
2066 let w =
2067 match conf.columns with
2068 | Csingle _ -> w
2069 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2070 | Csplit (c, _) -> w * c
2072 wcmd "geometry %d %d" w h);
2075 let enttext () =
2076 let len = String.length state.text in
2077 let drawstring s =
2078 let hscrollh =
2079 match state.mode with
2080 | Textentry _
2081 | View ->
2082 let h, _, _ = state.uioh#scrollpw in
2084 | _ -> 0
2086 let rect x w =
2087 GlDraw.rect
2088 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2089 (x+.w, float (conf.winh - hscrollh))
2092 let w = float (conf.winw - state.scrollw - 1) in
2093 if state.progress >= 0.0 && state.progress < 1.0
2094 then (
2095 GlDraw.color (0.3, 0.3, 0.3);
2096 let w1 = w *. state.progress in
2097 rect 0.0 w1;
2098 GlDraw.color (0.0, 0.0, 0.0);
2099 rect w1 (w-.w1)
2101 else (
2102 GlDraw.color (0.0, 0.0, 0.0);
2103 rect 0.0 w;
2106 GlDraw.color (1.0, 1.0, 1.0);
2107 drawstring fstate.fontsize
2108 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2110 let s =
2111 match state.mode with
2112 | Textentry ((prefix, text, _, _, _, _), _) ->
2113 let s =
2114 if len > 0
2115 then
2116 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2117 else
2118 Printf.sprintf "%s%s_" prefix text
2122 | _ -> state.text
2124 let s =
2125 if state.newerrmsgs
2126 then (
2127 if not (istextentry state.mode)
2128 then
2129 let s1 = "(press 'e' to review error messasges)" in
2130 if String.length s > 0 then s ^ " " ^ s1 else s1
2131 else s
2133 else s
2135 if String.length s > 0
2136 then drawstring s
2139 let gctiles () =
2140 let len = Queue.length state.tilelru in
2141 let layout = lazy (
2142 match state.throttle with
2143 | None ->
2144 if conf.preload
2145 then preloadlayout state.y
2146 else state.layout
2147 | Some (layout, _, _) ->
2148 layout
2149 ) in
2150 let rec loop qpos =
2151 if state.memused <= conf.memlimit
2152 then ()
2153 else (
2154 if qpos < len
2155 then
2156 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2157 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2158 let (_, pw, ph, _) = getpagedim n in
2160 gen = state.gen
2161 && colorspace = conf.colorspace
2162 && angle = conf.angle
2163 && pagew = pw
2164 && pageh = ph
2165 && (
2166 let x = col*conf.tilew
2167 and y = row*conf.tileh in
2168 tilevisible (Lazy.force_val layout) n x y
2170 then Queue.push lruitem state.tilelru
2171 else (
2172 freepbo p;
2173 wcmd "freetile %s" p;
2174 state.memused <- state.memused - s;
2175 state.uioh#infochanged Memused;
2176 Hashtbl.remove state.tilemap k;
2178 loop (qpos+1)
2181 loop 0
2184 let flushtiles () =
2185 Queue.iter (fun (k, p, s) ->
2186 wcmd "freetile %s" p;
2187 state.memused <- state.memused - s;
2188 state.uioh#infochanged Memused;
2189 Hashtbl.remove state.tilemap k;
2190 ) state.tilelru;
2191 Queue.clear state.tilelru;
2192 load state.layout;
2195 let logcurrently = function
2196 | Idle -> dolog "Idle"
2197 | Loading (l, gen) ->
2198 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2199 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2200 dolog
2201 "Tiling %d[%d,%d] page=%s cs=%s angle"
2202 l.pageno col row pageopaque
2203 (colorspace_to_string colorspace)
2205 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2206 angle gen conf.angle state.gen
2207 tilew tileh
2208 conf.tilew conf.tileh
2210 | Outlining _ ->
2211 dolog "outlining"
2214 let act cmds =
2215 (* dolog "%S" cmds; *)
2216 let op, args =
2217 let spacepos =
2218 try String.index cmds ' '
2219 with Not_found -> -1
2221 if spacepos = -1
2222 then cmds, ""
2223 else
2224 let l = String.length cmds in
2225 let op = String.sub cmds 0 spacepos in
2226 op, begin
2227 if l - spacepos < 2 then ""
2228 else String.sub cmds (spacepos+1) (l-spacepos-1)
2231 match op with
2232 | "clear" ->
2233 state.uioh#infochanged Pdim;
2234 state.pdims <- [];
2236 | "clearrects" ->
2237 state.rects <- state.rects1;
2238 G.postRedisplay "clearrects";
2240 | "continue" ->
2241 let n =
2242 try Scanf.sscanf args "%u" (fun n -> n)
2243 with exn ->
2244 dolog "error processing 'continue' %S: %s"
2245 cmds (Printexc.to_string exn);
2246 exit 1;
2248 state.pagecount <- n;
2249 begin match state.currently with
2250 | Outlining l ->
2251 state.currently <- Idle;
2252 state.outlines <- Array.of_list (List.rev l)
2253 | _ -> ()
2254 end;
2256 let cur, cmds = state.geomcmds in
2257 if String.length cur = 0
2258 then failwith "umpossible";
2260 begin match List.rev cmds with
2261 | [] ->
2262 state.geomcmds <- "", [];
2263 represent ();
2264 | (s, f) :: rest ->
2265 f ();
2266 state.geomcmds <- s, List.rev rest;
2267 end;
2268 if conf.maxwait = None
2269 then G.postRedisplay "continue";
2271 | "title" ->
2272 Wsi.settitle args
2274 | "msg" ->
2275 showtext ' ' args
2277 | "vmsg" ->
2278 if conf.verbose
2279 then showtext ' ' args
2281 | "progress" ->
2282 let progress, text =
2284 Scanf.sscanf args "%f %n"
2285 (fun f pos ->
2286 f, String.sub args pos (String.length args - pos))
2287 with exn ->
2288 dolog "error processing 'progress' %S: %s"
2289 cmds (Printexc.to_string exn);
2290 exit 1;
2292 state.text <- text;
2293 state.progress <- progress;
2294 G.postRedisplay "progress"
2296 | "firstmatch" ->
2297 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2299 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2300 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2301 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2302 with exn ->
2303 dolog "error processing 'firstmatch' %S: %s"
2304 cmds (Printexc.to_string exn);
2305 exit 1;
2307 let y = (getpagey pageno) + truncate y0 in
2308 addnav ();
2309 gotoy y;
2310 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2312 | "match" ->
2313 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2315 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2316 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2317 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2318 with exn ->
2319 dolog "error processing 'match' %S: %s"
2320 cmds (Printexc.to_string exn);
2321 exit 1;
2323 state.rects1 <-
2324 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2326 | "page" ->
2327 let pageopaque, t =
2329 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2330 with exn ->
2331 dolog "error processing 'page' %S: %s"
2332 cmds (Printexc.to_string exn);
2333 exit 1;
2335 begin match state.currently with
2336 | Loading (l, gen) ->
2337 vlog "page %d took %f sec" l.pageno t;
2338 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2339 begin match state.throttle with
2340 | None ->
2341 let preloadedpages =
2342 if conf.preload
2343 then preloadlayout state.y
2344 else state.layout
2346 let evict () =
2347 let module IntSet =
2348 Set.Make (struct type t = int let compare = (-) end) in
2349 let set =
2350 List.fold_left (fun s l -> IntSet.add l.pageno s)
2351 IntSet.empty preloadedpages
2353 let evictedpages =
2354 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2355 if not (IntSet.mem pageno set)
2356 then (
2357 wcmd "freepage %s" opaque;
2358 key :: accu
2360 else accu
2361 ) state.pagemap []
2363 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2365 evict ();
2366 state.currently <- Idle;
2367 if gen = state.gen
2368 then (
2369 tilepage l.pageno pageopaque state.layout;
2370 load state.layout;
2371 load preloadedpages;
2372 if pagevisible state.layout l.pageno
2373 && layoutready state.layout
2374 then G.postRedisplay "page";
2377 | Some (layout, _, _) ->
2378 state.currently <- Idle;
2379 tilepage l.pageno pageopaque layout;
2380 load state.layout
2381 end;
2383 | _ ->
2384 dolog "Inconsistent loading state";
2385 logcurrently state.currently;
2386 exit 1
2389 | "tile" ->
2390 let (x, y, opaque, size, t) =
2392 Scanf.sscanf args "%u %u %s %u %f"
2393 (fun x y p size t -> (x, y, p, size, t))
2394 with exn ->
2395 dolog "error processing 'tile' %S: %s"
2396 cmds (Printexc.to_string exn);
2397 exit 1;
2399 begin match state.currently with
2400 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2401 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2403 unmappbo opaque;
2404 if tilew != conf.tilew || tileh != conf.tileh
2405 then (
2406 wcmd "freetile %s" opaque;
2407 state.currently <- Idle;
2408 load state.layout;
2410 else (
2411 puttileopaque l col row gen cs angle opaque size t;
2412 state.memused <- state.memused + size;
2413 state.uioh#infochanged Memused;
2414 gctiles ();
2415 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2416 opaque, size) state.tilelru;
2418 let layout =
2419 match state.throttle with
2420 | None -> state.layout
2421 | Some (layout, _, _) -> layout
2424 state.currently <- Idle;
2425 if gen = state.gen
2426 && conf.colorspace = cs
2427 && conf.angle = angle
2428 && tilevisible layout l.pageno x y
2429 then conttiling l.pageno pageopaque;
2431 begin match state.throttle with
2432 | None ->
2433 preload state.layout;
2434 if gen = state.gen
2435 && conf.colorspace = cs
2436 && conf.angle = angle
2437 && tilevisible state.layout l.pageno x y
2438 then G.postRedisplay "tile nothrottle";
2440 | Some (layout, y, _) ->
2441 let ready = layoutready layout in
2442 if ready
2443 then (
2444 state.y <- y;
2445 state.layout <- layout;
2446 state.throttle <- None;
2447 G.postRedisplay "throttle";
2449 else load layout;
2450 end;
2453 | _ ->
2454 dolog "Inconsistent tiling state";
2455 logcurrently state.currently;
2456 exit 1
2459 | "pdim" ->
2460 let pdim =
2462 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2463 with exn ->
2464 dolog "error processing 'pdim' %S: %s"
2465 cmds (Printexc.to_string exn);
2466 exit 1;
2468 state.uioh#infochanged Pdim;
2469 state.pdims <- pdim :: state.pdims
2471 | "o" ->
2472 let (l, n, t, h, pos) =
2474 Scanf.sscanf args "%u %u %d %u %n"
2475 (fun l n t h pos -> l, n, t, h, pos)
2476 with exn ->
2477 dolog "error processing 'o' %S: %s"
2478 cmds (Printexc.to_string exn);
2479 exit 1;
2481 let s = String.sub args pos (String.length args - pos) in
2482 let outline = (s, l, (n, float t /. float h, 0.0)) in
2483 begin match state.currently with
2484 | Outlining outlines ->
2485 state.currently <- Outlining (outline :: outlines)
2486 | Idle ->
2487 state.currently <- Outlining [outline]
2488 | currently ->
2489 dolog "invalid outlining state";
2490 logcurrently currently
2493 | "info" ->
2494 state.docinfo <- (1, args) :: state.docinfo
2496 | "infoend" ->
2497 state.uioh#infochanged Docinfo;
2498 state.docinfo <- List.rev state.docinfo
2500 | _ ->
2501 dolog "unknown cmd `%S'" cmds
2504 let onhist cb =
2505 let rc = cb.rc in
2506 let action = function
2507 | HCprev -> cbget cb ~-1
2508 | HCnext -> cbget cb 1
2509 | HCfirst -> cbget cb ~-(cb.rc)
2510 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2511 and cancel () = cb.rc <- rc
2512 in (action, cancel)
2515 let search pattern forward =
2516 if String.length pattern > 0
2517 then
2518 let pn, py =
2519 match state.layout with
2520 | [] -> 0, 0
2521 | l :: _ ->
2522 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2524 wcmd "search %d %d %d %d,%s\000"
2525 (btod conf.icase) pn py (btod forward) pattern;
2528 let intentry text key =
2529 let c =
2530 if key >= 32 && key < 127
2531 then Char.chr key
2532 else '\000'
2534 match c with
2535 | '0' .. '9' ->
2536 let text = addchar text c in
2537 TEcont text
2539 | _ ->
2540 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2541 TEcont text
2544 let linknentry text key =
2545 let c =
2546 if key >= 32 && key < 127
2547 then Char.chr key
2548 else '\000'
2550 match c with
2551 | 'a' .. 'z' ->
2552 let text = addchar text c in
2553 TEcont text
2555 | _ ->
2556 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2557 TEcont text
2560 let linkndone f s =
2561 if String.length s > 0
2562 then (
2563 let n =
2564 let l = String.length s in
2565 let rec loop pos n = if pos = l then n else
2566 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2567 loop (pos+1) (n*26 + m)
2568 in loop 0 0
2570 let rec loop n = function
2571 | [] -> ()
2572 | l :: rest ->
2573 match getopaque l.pageno with
2574 | None -> loop n rest
2575 | Some opaque ->
2576 let m = getlinkcount opaque in
2577 if n < m
2578 then (
2579 let under = getlink opaque n in
2580 f under
2582 else loop (n-m) rest
2584 loop n state.layout;
2588 let textentry text key =
2589 if key land 0xff00 = 0xff00
2590 then TEcont text
2591 else TEcont (text ^ Wsi.toutf8 key)
2594 let reqlayout angle proportional =
2595 match state.throttle with
2596 | None ->
2597 if nogeomcmds state.geomcmds
2598 then state.anchor <- getanchor ();
2599 conf.angle <- angle mod 360;
2600 if conf.angle != 0
2601 then (
2602 match state.mode with
2603 | LinkNav _ -> state.mode <- View
2604 | _ -> ()
2606 conf.proportional <- proportional;
2607 invalidate "reqlayout"
2608 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2609 | _ -> ()
2612 let settrim trimmargins trimfuzz =
2613 if nogeomcmds state.geomcmds
2614 then state.anchor <- getanchor ();
2615 conf.trimmargins <- trimmargins;
2616 conf.trimfuzz <- trimfuzz;
2617 let x0, y0, x1, y1 = trimfuzz in
2618 invalidate "settrim"
2619 (fun () ->
2620 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2621 flushpages ();
2624 let setzoom zoom =
2625 match state.throttle with
2626 | None ->
2627 let zoom = max 0.01 zoom in
2628 if zoom <> conf.zoom
2629 then (
2630 state.prevzoom <- conf.zoom;
2631 conf.zoom <- zoom;
2632 reshape conf.winw conf.winh;
2633 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2636 | Some (layout, y, started) ->
2637 let time =
2638 match conf.maxwait with
2639 | None -> 0.0
2640 | Some t -> t
2642 let dt = now () -. started in
2643 if dt > time
2644 then (
2645 state.y <- y;
2646 load layout;
2650 let setcolumns mode columns coverA coverB =
2651 state.prevcolumns <- Some (conf.columns, conf.zoom);
2652 if columns < 0
2653 then (
2654 if isbirdseye mode
2655 then showtext '!' "split mode doesn't work in bird's eye"
2656 else (
2657 conf.columns <- Csplit (-columns, [||]);
2658 state.x <- 0;
2659 conf.zoom <- 1.0;
2662 else (
2663 if columns < 2
2664 then (
2665 conf.columns <- Csingle [||];
2666 state.x <- 0;
2667 setzoom 1.0;
2669 else (
2670 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2671 conf.zoom <- 1.0;
2674 reshape conf.winw conf.winh;
2677 let enterbirdseye () =
2678 let zoom = float conf.thumbw /. float conf.winw in
2679 let birdseyepageno =
2680 let cy = conf.winh / 2 in
2681 let fold = function
2682 | [] -> 0
2683 | l :: rest ->
2684 let rec fold best = function
2685 | [] -> best.pageno
2686 | l :: rest ->
2687 let d = cy - (l.pagedispy + l.pagevh/2)
2688 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2689 if abs d < abs dbest
2690 then fold l rest
2691 else best.pageno
2692 in fold l rest
2694 fold state.layout
2696 state.mode <- Birdseye (
2697 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2699 conf.zoom <- zoom;
2700 conf.presentation <- false;
2701 conf.interpagespace <- 10;
2702 conf.hlinks <- false;
2703 state.x <- 0;
2704 state.mstate <- Mnone;
2705 conf.maxwait <- None;
2706 conf.columns <- (
2707 match conf.beyecolumns with
2708 | Some c ->
2709 conf.zoom <- 1.0;
2710 Cmulti ((c, 0, 0), [||])
2711 | None -> Csingle [||]
2713 Wsi.setcursor Wsi.CURSOR_INHERIT;
2714 if conf.verbose
2715 then
2716 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2717 (100.0*.zoom)
2718 else
2719 state.text <- ""
2721 reshape conf.winw conf.winh;
2724 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2725 state.mode <- View;
2726 conf.zoom <- c.zoom;
2727 conf.presentation <- c.presentation;
2728 conf.interpagespace <- c.interpagespace;
2729 conf.maxwait <- c.maxwait;
2730 conf.hlinks <- c.hlinks;
2731 conf.beyecolumns <- (
2732 match conf.columns with
2733 | Cmulti ((c, _, _), _) -> Some c
2734 | Csingle _ -> None
2735 | Csplit _ -> failwith "leaving bird's eye split mode"
2737 conf.columns <- (
2738 match c.columns with
2739 | Cmulti (c, _) -> Cmulti (c, [||])
2740 | Csingle _ -> Csingle [||]
2741 | Csplit (c, _) -> Csplit (c, [||])
2743 state.x <- leftx;
2744 if conf.verbose
2745 then
2746 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2747 (100.0*.conf.zoom)
2749 reshape conf.winw conf.winh;
2750 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2753 let togglebirdseye () =
2754 match state.mode with
2755 | Birdseye vals -> leavebirdseye vals true
2756 | View -> enterbirdseye ()
2757 | _ -> ()
2760 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2761 let pageno = max 0 (pageno - incr) in
2762 let rec loop = function
2763 | [] -> gotopage1 pageno 0
2764 | l :: _ when l.pageno = pageno ->
2765 if l.pagedispy >= 0 && l.pagey = 0
2766 then G.postRedisplay "upbirdseye"
2767 else gotopage1 pageno 0
2768 | _ :: rest -> loop rest
2770 loop state.layout;
2771 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2774 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2775 let pageno = min (state.pagecount - 1) (pageno + incr) in
2776 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2777 let rec loop = function
2778 | [] ->
2779 let y, h = getpageyh pageno in
2780 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2781 gotoy (clamp dy)
2782 | l :: _ when l.pageno = pageno ->
2783 if l.pagevh != l.pageh
2784 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2785 else G.postRedisplay "downbirdseye"
2786 | _ :: rest -> loop rest
2788 loop state.layout
2791 let optentry mode _ key =
2792 let btos b = if b then "on" else "off" in
2793 if key >= 32 && key < 127
2794 then
2795 let c = Char.chr key in
2796 match c with
2797 | 's' ->
2798 let ondone s =
2799 try conf.scrollstep <- int_of_string s with exc ->
2800 state.text <- Printf.sprintf "bad integer `%s': %s"
2801 s (Printexc.to_string exc)
2803 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2805 | 'A' ->
2806 let ondone s =
2808 conf.autoscrollstep <- int_of_string s;
2809 if state.autoscroll <> None
2810 then state.autoscroll <- Some conf.autoscrollstep
2811 with exc ->
2812 state.text <- Printf.sprintf "bad integer `%s': %s"
2813 s (Printexc.to_string exc)
2815 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2817 | 'C' ->
2818 let ondone s =
2820 let n, a, b = multicolumns_of_string s in
2821 setcolumns mode n a b;
2822 with exc ->
2823 state.text <- Printf.sprintf "bad columns `%s': %s"
2824 s (Printexc.to_string exc)
2826 TEswitch ("columns: ", "", None, textentry, ondone, true)
2828 | 'Z' ->
2829 let ondone s =
2831 let zoom = float (int_of_string s) /. 100.0 in
2832 setzoom zoom
2833 with exc ->
2834 state.text <- Printf.sprintf "bad integer `%s': %s"
2835 s (Printexc.to_string exc)
2837 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2839 | 't' ->
2840 let ondone s =
2842 conf.thumbw <- bound (int_of_string s) 2 4096;
2843 state.text <-
2844 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2845 begin match mode with
2846 | Birdseye beye ->
2847 leavebirdseye beye false;
2848 enterbirdseye ();
2849 | _ -> ();
2851 with exc ->
2852 state.text <- Printf.sprintf "bad integer `%s': %s"
2853 s (Printexc.to_string exc)
2855 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2857 | 'R' ->
2858 let ondone s =
2859 match try
2860 Some (int_of_string s)
2861 with exc ->
2862 state.text <- Printf.sprintf "bad integer `%s': %s"
2863 s (Printexc.to_string exc);
2864 None
2865 with
2866 | Some angle -> reqlayout angle conf.proportional
2867 | None -> ()
2869 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2871 | 'i' ->
2872 conf.icase <- not conf.icase;
2873 TEdone ("case insensitive search " ^ (btos conf.icase))
2875 | 'p' ->
2876 conf.preload <- not conf.preload;
2877 gotoy state.y;
2878 TEdone ("preload " ^ (btos conf.preload))
2880 | 'v' ->
2881 conf.verbose <- not conf.verbose;
2882 TEdone ("verbose " ^ (btos conf.verbose))
2884 | 'd' ->
2885 conf.debug <- not conf.debug;
2886 TEdone ("debug " ^ (btos conf.debug))
2888 | 'h' ->
2889 conf.maxhfit <- not conf.maxhfit;
2890 state.maxy <- calcheight ();
2891 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2893 | 'c' ->
2894 conf.crophack <- not conf.crophack;
2895 TEdone ("crophack " ^ btos conf.crophack)
2897 | 'a' ->
2898 let s =
2899 match conf.maxwait with
2900 | None ->
2901 conf.maxwait <- Some infinity;
2902 "always wait for page to complete"
2903 | Some _ ->
2904 conf.maxwait <- None;
2905 "show placeholder if page is not ready"
2907 TEdone s
2909 | 'f' ->
2910 conf.underinfo <- not conf.underinfo;
2911 TEdone ("underinfo " ^ btos conf.underinfo)
2913 | 'P' ->
2914 conf.savebmarks <- not conf.savebmarks;
2915 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2917 | 'S' ->
2918 let ondone s =
2920 let pageno, py =
2921 match state.layout with
2922 | [] -> 0, 0
2923 | l :: _ ->
2924 l.pageno, l.pagey
2926 conf.interpagespace <- int_of_string s;
2927 docolumns conf.columns;
2928 state.maxy <- calcheight ();
2929 let y = getpagey pageno in
2930 gotoy (y + py)
2931 with exc ->
2932 state.text <- Printf.sprintf "bad integer `%s': %s"
2933 s (Printexc.to_string exc)
2935 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2937 | 'l' ->
2938 reqlayout conf.angle (not conf.proportional);
2939 TEdone ("proportional display " ^ btos conf.proportional)
2941 | 'T' ->
2942 settrim (not conf.trimmargins) conf.trimfuzz;
2943 TEdone ("trim margins " ^ btos conf.trimmargins)
2945 | 'I' ->
2946 conf.invert <- not conf.invert;
2947 TEdone ("invert colors " ^ btos conf.invert)
2949 | 'x' ->
2950 let ondone s =
2951 cbput state.hists.sel s;
2952 conf.selcmd <- s;
2954 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2955 textentry, ondone, true)
2957 | _ ->
2958 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2959 TEstop
2960 else
2961 TEcont state.text
2964 class type lvsource = object
2965 method getitemcount : int
2966 method getitem : int -> (string * int)
2967 method hasaction : int -> bool
2968 method exit :
2969 uioh:uioh ->
2970 cancel:bool ->
2971 active:int ->
2972 first:int ->
2973 pan:int ->
2974 qsearch:string ->
2975 uioh option
2976 method getactive : int
2977 method getfirst : int
2978 method getqsearch : string
2979 method setqsearch : string -> unit
2980 method getpan : int
2981 end;;
2983 class virtual lvsourcebase = object
2984 val mutable m_active = 0
2985 val mutable m_first = 0
2986 val mutable m_qsearch = ""
2987 val mutable m_pan = 0
2988 method getactive = m_active
2989 method getfirst = m_first
2990 method getqsearch = m_qsearch
2991 method getpan = m_pan
2992 method setqsearch s = m_qsearch <- s
2993 end;;
2995 let withoutlastutf8 s =
2996 let len = String.length s in
2997 if len = 0
2998 then s
2999 else
3000 let rec find pos =
3001 if pos = 0
3002 then pos
3003 else
3004 let b = Char.code s.[pos] in
3005 if b land 0b11000000 = 0b11000000
3006 then pos
3007 else find (pos-1)
3009 let first =
3010 if Char.code s.[len-1] land 0x80 = 0
3011 then len-1
3012 else find (len-1)
3014 String.sub s 0 first;
3017 let textentrykeyboard
3018 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3019 let key =
3020 if key >= 0xffb0 && key <= 0xffb9
3021 then key - 0xffb0 + 48 else key
3023 let enttext te =
3024 state.mode <- Textentry (te, onleave);
3025 state.text <- "";
3026 enttext ();
3027 G.postRedisplay "textentrykeyboard enttext";
3029 let histaction cmd =
3030 match opthist with
3031 | None -> ()
3032 | Some (action, _) ->
3033 state.mode <- Textentry (
3034 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3036 G.postRedisplay "textentry histaction"
3038 match key with
3039 | 0xff08 -> (* backspace *)
3040 let s = withoutlastutf8 text in
3041 let len = String.length s in
3042 if cancelonempty && len = 0
3043 then (
3044 onleave Cancel;
3045 G.postRedisplay "textentrykeyboard after cancel";
3047 else (
3048 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3051 | 0xff0d | 0xff8d -> (* (kp) enter *)
3052 ondone text;
3053 onleave Confirm;
3054 G.postRedisplay "textentrykeyboard after confirm"
3056 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3057 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3058 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3059 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3061 | 0xff1b -> (* escape*)
3062 if String.length text = 0
3063 then (
3064 begin match opthist with
3065 | None -> ()
3066 | Some (_, onhistcancel) -> onhistcancel ()
3067 end;
3068 onleave Cancel;
3069 state.text <- "";
3070 G.postRedisplay "textentrykeyboard after cancel2"
3072 else (
3073 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3076 | 0xff9f | 0xffff -> () (* delete *)
3078 | _ when key != 0
3079 && key land 0xff00 != 0xff00 (* keyboard *)
3080 && key land 0xfe00 != 0xfe00 (* xkb *)
3081 && key land 0xfd00 != 0xfd00 (* 3270 *)
3083 begin match onkey text key with
3084 | TEdone text ->
3085 ondone text;
3086 onleave Confirm;
3087 G.postRedisplay "textentrykeyboard after confirm2";
3089 | TEcont text ->
3090 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3092 | TEstop ->
3093 onleave Cancel;
3094 G.postRedisplay "textentrykeyboard after cancel3"
3096 | TEswitch te ->
3097 state.mode <- Textentry (te, onleave);
3098 G.postRedisplay "textentrykeyboard switch";
3099 end;
3101 | _ ->
3102 vlog "unhandled key %s" (Wsi.keyname key)
3105 let firstof first active =
3106 if first > active || abs (first - active) > fstate.maxrows - 1
3107 then max 0 (active - (fstate.maxrows/2))
3108 else first
3111 let calcfirst first active =
3112 if active > first
3113 then
3114 let rows = active - first in
3115 if rows > fstate.maxrows then active - fstate.maxrows else first
3116 else active
3119 let scrollph y maxy =
3120 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3121 let sh = float conf.winh /. sh in
3122 let sh = max sh (float conf.scrollh) in
3124 let percent =
3125 if y = state.maxy
3126 then 1.0
3127 else float y /. float maxy
3129 let position = (float conf.winh -. sh) *. percent in
3131 let position =
3132 if position +. sh > float conf.winh
3133 then float conf.winh -. sh
3134 else position
3136 position, sh;
3139 let coe s = (s :> uioh);;
3141 class listview ~(source:lvsource) ~trusted ~modehash =
3142 object (self)
3143 val m_pan = source#getpan
3144 val m_first = source#getfirst
3145 val m_active = source#getactive
3146 val m_qsearch = source#getqsearch
3147 val m_prev_uioh = state.uioh
3149 method private elemunder y =
3150 let n = y / (fstate.fontsize+1) in
3151 if m_first + n < source#getitemcount
3152 then (
3153 if source#hasaction (m_first + n)
3154 then Some (m_first + n)
3155 else None
3157 else None
3159 method display =
3160 Gl.enable `blend;
3161 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3162 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3163 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3164 GlDraw.color (1., 1., 1.);
3165 Gl.enable `texture_2d;
3166 let fs = fstate.fontsize in
3167 let nfs = fs + 1 in
3168 let ww = fstate.wwidth in
3169 let tabw = 30.0*.ww in
3170 let itemcount = source#getitemcount in
3171 let rec loop row =
3172 if (row - m_first) > fstate.maxrows
3173 then ()
3174 else (
3175 if row >= 0 && row < itemcount
3176 then (
3177 let (s, level) = source#getitem row in
3178 let y = (row - m_first) * nfs in
3179 let x = 5.0 +. float (level + m_pan) *. ww in
3180 if row = m_active
3181 then (
3182 Gl.disable `texture_2d;
3183 GlDraw.polygon_mode `both `line;
3184 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3185 GlDraw.rect (1., float (y + 1))
3186 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3187 GlDraw.polygon_mode `both `fill;
3188 GlDraw.color (1., 1., 1.);
3189 Gl.enable `texture_2d;
3192 let drawtabularstring s =
3193 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3194 if trusted
3195 then
3196 let tabpos = try String.index s '\t' with Not_found -> -1 in
3197 if tabpos > 0
3198 then
3199 let len = String.length s - tabpos - 1 in
3200 let s1 = String.sub s 0 tabpos
3201 and s2 = String.sub s (tabpos + 1) len in
3202 let nx = drawstr x s1 in
3203 let sw = nx -. x in
3204 let x = x +. (max tabw sw) in
3205 drawstr x s2
3206 else
3207 drawstr x s
3208 else
3209 drawstr x s
3211 let _ = drawtabularstring s in
3212 loop (row+1)
3216 loop m_first;
3217 Gl.disable `blend;
3218 Gl.disable `texture_2d;
3220 method updownlevel incr =
3221 let len = source#getitemcount in
3222 let curlevel =
3223 if m_active >= 0 && m_active < len
3224 then snd (source#getitem m_active)
3225 else -1
3227 let rec flow i =
3228 if i = len then i-1 else if i = -1 then 0 else
3229 let _, l = source#getitem i in
3230 if l != curlevel then i else flow (i+incr)
3232 let active = flow m_active in
3233 let first = calcfirst m_first active in
3234 G.postRedisplay "outline updownlevel";
3235 {< m_active = active; m_first = first >}
3237 method private key1 key mask =
3238 let set1 active first qsearch =
3239 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3241 let search active pattern incr =
3242 let dosearch re =
3243 let rec loop n =
3244 if n >= 0 && n < source#getitemcount
3245 then (
3246 let s, _ = source#getitem n in
3248 (try ignore (Str.search_forward re s 0); true
3249 with Not_found -> false)
3250 then Some n
3251 else loop (n + incr)
3253 else None
3255 loop active
3258 let re = Str.regexp_case_fold pattern in
3259 dosearch re
3260 with Failure s ->
3261 state.text <- s;
3262 None
3264 let itemcount = source#getitemcount in
3265 let find start incr =
3266 let rec find i =
3267 if i = -1 || i = itemcount
3268 then -1
3269 else (
3270 if source#hasaction i
3271 then i
3272 else find (i + incr)
3275 find start
3277 let set active first =
3278 let first = bound first 0 (itemcount - fstate.maxrows) in
3279 state.text <- "";
3280 coe {< m_active = active; m_first = first >}
3282 let navigate incr =
3283 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3284 let active, first =
3285 let incr1 = if incr > 0 then 1 else -1 in
3286 if isvisible m_first m_active
3287 then
3288 let next =
3289 let next = m_active + incr in
3290 let next =
3291 if next < 0 || next >= itemcount
3292 then -1
3293 else find next incr1
3295 if next = -1 || abs (m_active - next) > fstate.maxrows
3296 then -1
3297 else next
3299 if next = -1
3300 then
3301 let first = m_first + incr in
3302 let first = bound first 0 (itemcount - 1) in
3303 let next =
3304 let next = m_active + incr in
3305 let next = bound next 0 (itemcount - 1) in
3306 find next ~-incr1
3308 let active = if next = -1 then m_active else next in
3309 active, first
3310 else
3311 let first = min next m_first in
3312 let first =
3313 if abs (next - first) > fstate.maxrows
3314 then first + incr
3315 else first
3317 next, first
3318 else
3319 let first = m_first + incr in
3320 let first = bound first 0 (itemcount - 1) in
3321 let active =
3322 let next = m_active + incr in
3323 let next = bound next 0 (itemcount - 1) in
3324 let next = find next incr1 in
3325 let active =
3326 if next = -1 || abs (m_active - first) > fstate.maxrows
3327 then (
3328 let active = if m_active = -1 then next else m_active in
3329 active
3331 else next
3333 if isvisible first active
3334 then active
3335 else -1
3337 active, first
3339 G.postRedisplay "listview navigate";
3340 set active first;
3342 match key with
3343 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3344 let incr = if key = 0x72 then -1 else 1 in
3345 let active, first =
3346 match search (m_active + incr) m_qsearch incr with
3347 | None ->
3348 state.text <- m_qsearch ^ " [not found]";
3349 m_active, m_first
3350 | Some active ->
3351 state.text <- m_qsearch;
3352 active, firstof m_first active
3354 G.postRedisplay "listview ctrl-r/s";
3355 set1 active first m_qsearch;
3357 | 0xff08 -> (* backspace *)
3358 if String.length m_qsearch = 0
3359 then coe self
3360 else (
3361 let qsearch = withoutlastutf8 m_qsearch in
3362 let len = String.length qsearch in
3363 if len = 0
3364 then (
3365 state.text <- "";
3366 G.postRedisplay "listview empty qsearch";
3367 set1 m_active m_first "";
3369 else
3370 let active, first =
3371 match search m_active qsearch ~-1 with
3372 | None ->
3373 state.text <- qsearch ^ " [not found]";
3374 m_active, m_first
3375 | Some active ->
3376 state.text <- qsearch;
3377 active, firstof m_first active
3379 G.postRedisplay "listview backspace qsearch";
3380 set1 active first qsearch
3383 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3384 let pattern = m_qsearch ^ Wsi.toutf8 key in
3385 let active, first =
3386 match search m_active pattern 1 with
3387 | None ->
3388 state.text <- pattern ^ " [not found]";
3389 m_active, m_first
3390 | Some active ->
3391 state.text <- pattern;
3392 active, firstof m_first active
3394 G.postRedisplay "listview qsearch add";
3395 set1 active first pattern;
3397 | 0xff1b -> (* escape *)
3398 state.text <- "";
3399 if String.length m_qsearch = 0
3400 then (
3401 G.postRedisplay "list view escape";
3402 begin
3403 match
3404 source#exit (coe self) true m_active m_first m_pan m_qsearch
3405 with
3406 | None -> m_prev_uioh
3407 | Some uioh -> uioh
3410 else (
3411 G.postRedisplay "list view kill qsearch";
3412 source#setqsearch "";
3413 coe {< m_qsearch = "" >}
3416 | 0xff0d | 0xff8d -> (* (kp) enter *)
3417 state.text <- "";
3418 let self = {< m_qsearch = "" >} in
3419 source#setqsearch "";
3420 let opt =
3421 G.postRedisplay "listview enter";
3422 if m_active >= 0 && m_active < source#getitemcount
3423 then (
3424 source#exit (coe self) false m_active m_first m_pan "";
3426 else (
3427 source#exit (coe self) true m_active m_first m_pan "";
3430 begin match opt with
3431 | None -> m_prev_uioh
3432 | Some uioh -> uioh
3435 | 0xff9f | 0xffff -> (* (kp) delete *)
3436 coe self
3438 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3439 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3440 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3441 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3443 | 0xff53 | 0xff98 -> (* (kp) right *)
3444 state.text <- "";
3445 G.postRedisplay "listview right";
3446 coe {< m_pan = m_pan - 1 >}
3448 | 0xff51 | 0xff96 -> (* (kp) left *)
3449 state.text <- "";
3450 G.postRedisplay "listview left";
3451 coe {< m_pan = m_pan + 1 >}
3453 | 0xff50 | 0xff95 -> (* (kp) home *)
3454 let active = find 0 1 in
3455 G.postRedisplay "listview home";
3456 set active 0;
3458 | 0xff57 | 0xff9c -> (* (kp) end *)
3459 let first = max 0 (itemcount - fstate.maxrows) in
3460 let active = find (itemcount - 1) ~-1 in
3461 G.postRedisplay "listview end";
3462 set active first;
3464 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3465 coe self
3467 | _ ->
3468 dolog "listview unknown key %#x" key; coe self
3470 method key key mask =
3471 match state.mode with
3472 | Textentry te -> textentrykeyboard key mask te; coe self
3473 | _ -> self#key1 key mask
3475 method button button down x y _ =
3476 let opt =
3477 match button with
3478 | 1 when x > conf.winw - conf.scrollbw ->
3479 G.postRedisplay "listview scroll";
3480 if down
3481 then
3482 let _, position, sh = self#scrollph in
3483 if y > truncate position && y < truncate (position +. sh)
3484 then (
3485 state.mstate <- Mscrolly;
3486 Some (coe self)
3488 else
3489 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3490 let first = truncate (s *. float source#getitemcount) in
3491 let first = min source#getitemcount first in
3492 Some (coe {< m_first = first; m_active = first >})
3493 else (
3494 state.mstate <- Mnone;
3495 Some (coe self);
3497 | 1 when not down ->
3498 begin match self#elemunder y with
3499 | Some n ->
3500 G.postRedisplay "listview click";
3501 source#exit
3502 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3503 | _ ->
3504 Some (coe self)
3506 | n when (n == 4 || n == 5) && not down ->
3507 let len = source#getitemcount in
3508 let first =
3509 if n = 5 && m_first + fstate.maxrows >= len
3510 then
3511 m_first
3512 else
3513 let first = m_first + (if n == 4 then -1 else 1) in
3514 bound first 0 (len - 1)
3516 G.postRedisplay "listview wheel";
3517 Some (coe {< m_first = first >})
3518 | n when (n = 6 || n = 7) && not down ->
3519 let inc = m_first + (if n = 7 then -1 else 1) in
3520 G.postRedisplay "listview hwheel";
3521 Some (coe {< m_pan = m_pan + inc >})
3522 | _ ->
3523 Some (coe self)
3525 match opt with
3526 | None -> m_prev_uioh
3527 | Some uioh -> uioh
3529 method motion _ y =
3530 match state.mstate with
3531 | Mscrolly ->
3532 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3533 let first = truncate (s *. float source#getitemcount) in
3534 let first = min source#getitemcount first in
3535 G.postRedisplay "listview motion";
3536 coe {< m_first = first; m_active = first >}
3537 | _ -> coe self
3539 method pmotion x y =
3540 if x < conf.winw - conf.scrollbw
3541 then
3542 let n =
3543 match self#elemunder y with
3544 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3545 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3547 let o =
3548 if n != m_active
3549 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3550 else self
3552 coe o
3553 else (
3554 Wsi.setcursor Wsi.CURSOR_INHERIT;
3555 coe self
3558 method infochanged _ = ()
3560 method scrollpw = (0, 0.0, 0.0)
3561 method scrollph =
3562 let nfs = fstate.fontsize + 1 in
3563 let y = m_first * nfs in
3564 let itemcount = source#getitemcount in
3565 let maxi = max 0 (itemcount - fstate.maxrows) in
3566 let maxy = maxi * nfs in
3567 let p, h = scrollph y maxy in
3568 conf.scrollbw, p, h
3570 method modehash = modehash
3571 end;;
3573 class outlinelistview ~source =
3574 object (self)
3575 inherit listview
3576 ~source:(source :> lvsource)
3577 ~trusted:false
3578 ~modehash:(findkeyhash conf "outline")
3579 as super
3581 method key key mask =
3582 let calcfirst first active =
3583 if active > first
3584 then
3585 let rows = active - first in
3586 let maxrows =
3587 if String.length state.text = 0
3588 then fstate.maxrows
3589 else fstate.maxrows - 2
3591 if rows > maxrows then active - maxrows else first
3592 else active
3594 let navigate incr =
3595 let active = m_active + incr in
3596 let active = bound active 0 (source#getitemcount - 1) in
3597 let first = calcfirst m_first active in
3598 G.postRedisplay "outline navigate";
3599 coe {< m_active = active; m_first = first >}
3601 let ctrl = Wsi.withctrl mask in
3602 match key with
3603 | 110 when ctrl -> (* ctrl-n *)
3604 source#narrow m_qsearch;
3605 G.postRedisplay "outline ctrl-n";
3606 coe {< m_first = 0; m_active = 0 >}
3608 | 117 when ctrl -> (* ctrl-u *)
3609 source#denarrow;
3610 G.postRedisplay "outline ctrl-u";
3611 state.text <- "";
3612 coe {< m_first = 0; m_active = 0 >}
3614 | 108 when ctrl -> (* ctrl-l *)
3615 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3616 G.postRedisplay "outline ctrl-l";
3617 coe {< m_first = first >}
3619 | 0xff9f | 0xffff -> (* (kp) delete *)
3620 source#remove m_active;
3621 G.postRedisplay "outline delete";
3622 let active = max 0 (m_active-1) in
3623 coe {< m_first = firstof m_first active;
3624 m_active = active >}
3626 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3627 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3628 | 0xff55 | 0xff9a -> (* (kp) prior *)
3629 navigate ~-(fstate.maxrows)
3630 | 0xff56 | 0xff9b -> (* (kp) next *)
3631 navigate fstate.maxrows
3633 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3634 let o =
3635 if ctrl
3636 then (
3637 G.postRedisplay "outline ctrl right";
3638 {< m_pan = m_pan + 1 >}
3640 else self#updownlevel 1
3642 coe o
3644 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3645 let o =
3646 if ctrl
3647 then (
3648 G.postRedisplay "outline ctrl left";
3649 {< m_pan = m_pan - 1 >}
3651 else self#updownlevel ~-1
3653 coe o
3655 | 0xff50 | 0xff95 -> (* (kp) home *)
3656 G.postRedisplay "outline home";
3657 coe {< m_first = 0; m_active = 0 >}
3659 | 0xff57 | 0xff9c -> (* (kp) end *)
3660 let active = source#getitemcount - 1 in
3661 let first = max 0 (active - fstate.maxrows) in
3662 G.postRedisplay "outline end";
3663 coe {< m_active = active; m_first = first >}
3665 | _ -> super#key key mask
3668 let outlinesource usebookmarks =
3669 let empty = [||] in
3670 (object
3671 inherit lvsourcebase
3672 val mutable m_items = empty
3673 val mutable m_orig_items = empty
3674 val mutable m_prev_items = empty
3675 val mutable m_narrow_pattern = ""
3676 val mutable m_hadremovals = false
3678 method getitemcount =
3679 Array.length m_items + (if m_hadremovals then 1 else 0)
3681 method getitem n =
3682 if n == Array.length m_items && m_hadremovals
3683 then
3684 ("[Confirm removal]", 0)
3685 else
3686 let s, n, _ = m_items.(n) in
3687 (s, n)
3689 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3690 ignore (uioh, first, qsearch);
3691 let confrimremoval = m_hadremovals && active = Array.length m_items in
3692 let items =
3693 if String.length m_narrow_pattern = 0
3694 then m_orig_items
3695 else m_items
3697 if not cancel
3698 then (
3699 if not confrimremoval
3700 then(
3701 let _, _, anchor = m_items.(active) in
3702 gotoghyll (getanchory anchor);
3703 m_items <- items;
3705 else (
3706 state.bookmarks <- Array.to_list m_items;
3707 m_orig_items <- m_items;
3710 else m_items <- items;
3711 m_pan <- pan;
3712 None
3714 method hasaction _ = true
3716 method greetmsg =
3717 if Array.length m_items != Array.length m_orig_items
3718 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3719 else ""
3721 method narrow pattern =
3722 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3723 match reopt with
3724 | None -> ()
3725 | Some re ->
3726 let rec loop accu n =
3727 if n = -1
3728 then (
3729 m_narrow_pattern <- pattern;
3730 m_items <- Array.of_list accu
3732 else
3733 let (s, _, _) as o = m_items.(n) in
3734 let accu =
3735 if (try ignore (Str.search_forward re s 0); true
3736 with Not_found -> false)
3737 then o :: accu
3738 else accu
3740 loop accu (n-1)
3742 loop [] (Array.length m_items - 1)
3744 method denarrow =
3745 m_orig_items <- (
3746 if usebookmarks
3747 then Array.of_list state.bookmarks
3748 else state.outlines
3750 m_items <- m_orig_items
3752 method remove m =
3753 if usebookmarks
3754 then
3755 if m >= 0 && m < Array.length m_items
3756 then (
3757 m_hadremovals <- true;
3758 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3759 let n = if n >= m then n+1 else n in
3760 m_items.(n)
3764 method reset anchor items =
3765 m_hadremovals <- false;
3766 if m_orig_items == empty || m_prev_items != items
3767 then (
3768 m_orig_items <- items;
3769 if String.length m_narrow_pattern = 0
3770 then m_items <- items;
3772 m_prev_items <- items;
3773 let rely = getanchory anchor in
3774 let active =
3775 let rec loop n best bestd =
3776 if n = Array.length m_items
3777 then best
3778 else
3779 let (_, _, anchor) = m_items.(n) in
3780 let orely = getanchory anchor in
3781 let d = abs (orely - rely) in
3782 if d < bestd
3783 then loop (n+1) n d
3784 else loop (n+1) best bestd
3786 loop 0 ~-1 max_int
3788 m_active <- active;
3789 m_first <- firstof m_first active
3790 end)
3793 let enterselector usebookmarks =
3794 let source = outlinesource usebookmarks in
3795 fun errmsg ->
3796 let outlines =
3797 if usebookmarks
3798 then Array.of_list state.bookmarks
3799 else state.outlines
3801 if Array.length outlines = 0
3802 then (
3803 showtext ' ' errmsg;
3805 else (
3806 state.text <- source#greetmsg;
3807 Wsi.setcursor Wsi.CURSOR_INHERIT;
3808 let anchor = getanchor () in
3809 source#reset anchor outlines;
3810 state.uioh <- coe (new outlinelistview ~source);
3811 G.postRedisplay "enter selector";
3815 let enteroutlinemode =
3816 let f = enterselector false in
3817 fun ()-> f "Document has no outline";
3820 let enterbookmarkmode =
3821 let f = enterselector true in
3822 fun () -> f "Document has no bookmarks (yet)";
3825 let color_of_string s =
3826 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3827 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3831 let color_to_string (r, g, b) =
3832 let r = truncate (r *. 256.0)
3833 and g = truncate (g *. 256.0)
3834 and b = truncate (b *. 256.0) in
3835 Printf.sprintf "%d/%d/%d" r g b
3838 let irect_of_string s =
3839 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3842 let irect_to_string (x0,y0,x1,y1) =
3843 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3846 let makecheckers () =
3847 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3848 following to say:
3849 converted by Issac Trotts. July 25, 2002 *)
3850 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3851 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3852 let id = GlTex.gen_texture () in
3853 GlTex.bind_texture `texture_2d id;
3854 GlPix.store (`unpack_alignment 1);
3855 GlTex.image2d image;
3856 List.iter (GlTex.parameter ~target:`texture_2d)
3857 [ `wrap_s `repeat;
3858 `wrap_t `repeat;
3859 `mag_filter `nearest;
3860 `min_filter `nearest ];
3864 let setcheckers enabled =
3865 match state.texid with
3866 | None ->
3867 if enabled then state.texid <- Some (makecheckers ())
3869 | Some texid ->
3870 if not enabled
3871 then (
3872 GlTex.delete_texture texid;
3873 state.texid <- None;
3877 let int_of_string_with_suffix s =
3878 let l = String.length s in
3879 let s1, shift =
3880 if l > 1
3881 then
3882 let suffix = Char.lowercase s.[l-1] in
3883 match suffix with
3884 | 'k' -> String.sub s 0 (l-1), 10
3885 | 'm' -> String.sub s 0 (l-1), 20
3886 | 'g' -> String.sub s 0 (l-1), 30
3887 | _ -> s, 0
3888 else s, 0
3890 let n = int_of_string s1 in
3891 let m = n lsl shift in
3892 if m < 0 || m < n
3893 then raise (Failure "value too large")
3894 else m
3897 let string_with_suffix_of_int n =
3898 if n = 0
3899 then "0"
3900 else
3901 let n, s =
3902 if n land ((1 lsl 30) - 1) = 0
3903 then n lsr 30, "G"
3904 else (
3905 if n land ((1 lsl 20) - 1) = 0
3906 then n lsr 20, "M"
3907 else (
3908 if n land ((1 lsl 10) - 1) = 0
3909 then n lsr 10, "K"
3910 else n, ""
3914 let rec loop s n =
3915 let h = n mod 1000 in
3916 let n = n / 1000 in
3917 if n = 0
3918 then string_of_int h ^ s
3919 else (
3920 let s = Printf.sprintf "_%03d%s" h s in
3921 loop s n
3924 loop "" n ^ s;
3927 let defghyllscroll = (40, 8, 32);;
3928 let ghyllscroll_of_string s =
3929 let (n, a, b) as nab =
3930 if s = "default"
3931 then defghyllscroll
3932 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3934 if n <= a || n <= b || a >= b
3935 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3936 nab;
3939 let ghyllscroll_to_string ((n, a, b) as nab) =
3940 if nab = defghyllscroll
3941 then "default"
3942 else Printf.sprintf "%d,%d,%d" n a b;
3945 let describe_location () =
3946 let f (fn, _) l =
3947 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3949 let fn, ln = List.fold_left f (-1, -1) state.layout in
3950 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3951 let percent =
3952 if maxy <= 0
3953 then 100.
3954 else (100. *. (float state.y /. float maxy))
3956 if fn = ln
3957 then
3958 Printf.sprintf "page %d of %d [%.2f%%]"
3959 (fn+1) state.pagecount percent
3960 else
3961 Printf.sprintf
3962 "pages %d-%d of %d [%.2f%%]"
3963 (fn+1) (ln+1) state.pagecount percent
3966 let setpresentationmode v =
3967 let (n, _, _) = getanchor () in
3968 let _, h = getpageyh n in
3969 let ips = if conf.presentation then calcips h else conf.interpagespace in
3970 state.anchor <- (n, 0.0, float ips);
3971 conf.presentation <- v;
3972 if conf.presentation
3973 then (
3974 if not conf.scrollbarinpm
3975 then state.scrollw <- 0;
3977 else state.scrollw <- conf.scrollbw;
3978 represent ();
3981 let enterinfomode =
3982 let btos b = if b then "\xe2\x88\x9a" else "" in
3983 let showextended = ref false in
3984 let leave mode = function
3985 | Confirm -> state.mode <- mode
3986 | Cancel -> state.mode <- mode in
3987 let src =
3988 (object
3989 val mutable m_first_time = true
3990 val mutable m_l = []
3991 val mutable m_a = [||]
3992 val mutable m_prev_uioh = nouioh
3993 val mutable m_prev_mode = View
3995 inherit lvsourcebase
3997 method reset prev_mode prev_uioh =
3998 m_a <- Array.of_list (List.rev m_l);
3999 m_l <- [];
4000 m_prev_mode <- prev_mode;
4001 m_prev_uioh <- prev_uioh;
4002 if m_first_time
4003 then (
4004 let rec loop n =
4005 if n >= Array.length m_a
4006 then ()
4007 else
4008 match m_a.(n) with
4009 | _, _, _, Action _ -> m_active <- n
4010 | _ -> loop (n+1)
4012 loop 0;
4013 m_first_time <- false;
4016 method int name get set =
4017 m_l <-
4018 (name, `int get, 1, Action (
4019 fun u ->
4020 let ondone s =
4021 try set (int_of_string s)
4022 with exn ->
4023 state.text <- Printf.sprintf "bad integer `%s': %s"
4024 s (Printexc.to_string exn)
4026 state.text <- "";
4027 let te = name ^ ": ", "", None, intentry, ondone, true in
4028 state.mode <- Textentry (te, leave m_prev_mode);
4030 )) :: m_l
4032 method int_with_suffix name get set =
4033 m_l <-
4034 (name, `intws get, 1, Action (
4035 fun u ->
4036 let ondone s =
4037 try set (int_of_string_with_suffix s)
4038 with exn ->
4039 state.text <- Printf.sprintf "bad integer `%s': %s"
4040 s (Printexc.to_string exn)
4042 state.text <- "";
4043 let te =
4044 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4046 state.mode <- Textentry (te, leave m_prev_mode);
4048 )) :: m_l
4050 method bool ?(offset=1) ?(btos=btos) name get set =
4051 m_l <-
4052 (name, `bool (btos, get), offset, Action (
4053 fun u ->
4054 let v = get () in
4055 set (not v);
4057 )) :: m_l
4059 method color name get set =
4060 m_l <-
4061 (name, `color get, 1, Action (
4062 fun u ->
4063 let invalid = (nan, nan, nan) in
4064 let ondone s =
4065 let c =
4066 try color_of_string s
4067 with exn ->
4068 state.text <- Printf.sprintf "bad color `%s': %s"
4069 s (Printexc.to_string exn);
4070 invalid
4072 if c <> invalid
4073 then set c;
4075 let te = name ^ ": ", "", None, textentry, ondone, true in
4076 state.text <- color_to_string (get ());
4077 state.mode <- Textentry (te, leave m_prev_mode);
4079 )) :: m_l
4081 method string name get set =
4082 m_l <-
4083 (name, `string get, 1, Action (
4084 fun u ->
4085 let ondone s = set s in
4086 let te = name ^ ": ", "", None, textentry, ondone, true in
4087 state.mode <- Textentry (te, leave m_prev_mode);
4089 )) :: m_l
4091 method colorspace name get set =
4092 m_l <-
4093 (name, `string get, 1, Action (
4094 fun _ ->
4095 let source =
4096 let vals = [| "rgb"; "bgr"; "gray" |] in
4097 (object
4098 inherit lvsourcebase
4100 initializer
4101 m_active <- int_of_colorspace conf.colorspace;
4102 m_first <- 0;
4104 method getitemcount = Array.length vals
4105 method getitem n = (vals.(n), 0)
4106 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4107 ignore (uioh, first, pan, qsearch);
4108 if not cancel then set active;
4109 None
4110 method hasaction _ = true
4111 end)
4113 state.text <- "";
4114 let modehash = findkeyhash conf "info" in
4115 coe (new listview ~source ~trusted:true ~modehash)
4116 )) :: m_l
4118 method caption s offset =
4119 m_l <- (s, `empty, offset, Noaction) :: m_l
4121 method caption2 s f offset =
4122 m_l <- (s, `string f, offset, Noaction) :: m_l
4124 method getitemcount = Array.length m_a
4126 method getitem n =
4127 let tostr = function
4128 | `int f -> string_of_int (f ())
4129 | `intws f -> string_with_suffix_of_int (f ())
4130 | `string f -> f ()
4131 | `color f -> color_to_string (f ())
4132 | `bool (btos, f) -> btos (f ())
4133 | `empty -> ""
4135 let name, t, offset, _ = m_a.(n) in
4136 ((let s = tostr t in
4137 if String.length s > 0
4138 then Printf.sprintf "%s\t%s" name s
4139 else name),
4140 offset)
4142 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4143 let uiohopt =
4144 if not cancel
4145 then (
4146 m_qsearch <- qsearch;
4147 let uioh =
4148 match m_a.(active) with
4149 | _, _, _, Action f -> f uioh
4150 | _ -> uioh
4152 Some uioh
4154 else None
4156 m_active <- active;
4157 m_first <- first;
4158 m_pan <- pan;
4159 uiohopt
4161 method hasaction n =
4162 match m_a.(n) with
4163 | _, _, _, Action _ -> true
4164 | _ -> false
4165 end)
4167 let rec fillsrc prevmode prevuioh =
4168 let sep () = src#caption "" 0 in
4169 let colorp name get set =
4170 src#string name
4171 (fun () -> color_to_string (get ()))
4172 (fun v ->
4174 let c = color_of_string v in
4175 set c
4176 with exn ->
4177 state.text <- Printf.sprintf "bad color `%s': %s"
4178 v (Printexc.to_string exn);
4181 let oldmode = state.mode in
4182 let birdseye = isbirdseye state.mode in
4184 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4186 src#bool "presentation mode"
4187 (fun () -> conf.presentation)
4188 (fun v -> setpresentationmode v);
4190 src#bool "ignore case in searches"
4191 (fun () -> conf.icase)
4192 (fun v -> conf.icase <- v);
4194 src#bool "preload"
4195 (fun () -> conf.preload)
4196 (fun v -> conf.preload <- v);
4198 src#bool "highlight links"
4199 (fun () -> conf.hlinks)
4200 (fun v -> conf.hlinks <- v);
4202 src#bool "under info"
4203 (fun () -> conf.underinfo)
4204 (fun v -> conf.underinfo <- v);
4206 src#bool "persistent bookmarks"
4207 (fun () -> conf.savebmarks)
4208 (fun v -> conf.savebmarks <- v);
4210 src#bool "proportional display"
4211 (fun () -> conf.proportional)
4212 (fun v -> reqlayout conf.angle v);
4214 src#bool "trim margins"
4215 (fun () -> conf.trimmargins)
4216 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4218 src#bool "persistent location"
4219 (fun () -> conf.jumpback)
4220 (fun v -> conf.jumpback <- v);
4222 sep ();
4223 src#int "inter-page space"
4224 (fun () -> conf.interpagespace)
4225 (fun n ->
4226 conf.interpagespace <- n;
4227 docolumns conf.columns;
4228 let pageno, py =
4229 match state.layout with
4230 | [] -> 0, 0
4231 | l :: _ ->
4232 l.pageno, l.pagey
4234 state.maxy <- calcheight ();
4235 let y = getpagey pageno in
4236 gotoy (y + py)
4239 src#int "page bias"
4240 (fun () -> conf.pagebias)
4241 (fun v -> conf.pagebias <- v);
4243 src#int "scroll step"
4244 (fun () -> conf.scrollstep)
4245 (fun n -> conf.scrollstep <- n);
4247 src#int "horizontal scroll step"
4248 (fun () -> conf.hscrollstep)
4249 (fun v -> conf.hscrollstep <- v);
4251 src#int "auto scroll step"
4252 (fun () ->
4253 match state.autoscroll with
4254 | Some step -> step
4255 | _ -> conf.autoscrollstep)
4256 (fun n ->
4257 if state.autoscroll <> None
4258 then state.autoscroll <- Some n;
4259 conf.autoscrollstep <- n);
4261 src#int "zoom"
4262 (fun () -> truncate (conf.zoom *. 100.))
4263 (fun v -> setzoom ((float v) /. 100.));
4265 src#int "rotation"
4266 (fun () -> conf.angle)
4267 (fun v -> reqlayout v conf.proportional);
4269 src#int "scroll bar width"
4270 (fun () -> state.scrollw)
4271 (fun v ->
4272 state.scrollw <- v;
4273 conf.scrollbw <- v;
4274 reshape conf.winw conf.winh;
4277 src#int "scroll handle height"
4278 (fun () -> conf.scrollh)
4279 (fun v -> conf.scrollh <- v;);
4281 src#int "thumbnail width"
4282 (fun () -> conf.thumbw)
4283 (fun v ->
4284 conf.thumbw <- min 4096 v;
4285 match oldmode with
4286 | Birdseye beye ->
4287 leavebirdseye beye false;
4288 enterbirdseye ()
4289 | _ -> ()
4292 let mode = state.mode in
4293 src#string "columns"
4294 (fun () ->
4295 match conf.columns with
4296 | Csingle _ -> "1"
4297 | Cmulti (multi, _) -> multicolumns_to_string multi
4298 | Csplit (count, _) -> "-" ^ string_of_int count
4300 (fun v ->
4301 let n, a, b = multicolumns_of_string v in
4302 setcolumns mode n a b);
4304 sep ();
4305 src#caption "Presentation mode" 0;
4306 src#bool "scrollbar visible"
4307 (fun () -> conf.scrollbarinpm)
4308 (fun v ->
4309 if v != conf.scrollbarinpm
4310 then (
4311 conf.scrollbarinpm <- v;
4312 if conf.presentation
4313 then (
4314 state.scrollw <- if v then conf.scrollbw else 0;
4315 reshape conf.winw conf.winh;
4320 sep ();
4321 src#caption "Pixmap cache" 0;
4322 src#int_with_suffix "size (advisory)"
4323 (fun () -> conf.memlimit)
4324 (fun v -> conf.memlimit <- v);
4326 src#caption2 "used"
4327 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4328 (string_with_suffix_of_int state.memused)
4329 (Hashtbl.length state.tilemap)) 1;
4331 sep ();
4332 src#caption "Layout" 0;
4333 src#caption2 "Dimension"
4334 (fun () ->
4335 Printf.sprintf "%dx%d (virtual %dx%d)"
4336 conf.winw conf.winh
4337 state.w state.maxy)
4339 if conf.debug
4340 then
4341 src#caption2 "Position" (fun () ->
4342 Printf.sprintf "%dx%d" state.x state.y
4344 else
4345 src#caption2 "Visible" (fun () -> describe_location ()) 1
4348 sep ();
4349 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4350 "Save these parameters as global defaults at exit"
4351 (fun () -> conf.bedefault)
4352 (fun v -> conf.bedefault <- v)
4355 sep ();
4356 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4357 src#bool ~offset:0 ~btos "Extended parameters"
4358 (fun () -> !showextended)
4359 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4360 if !showextended
4361 then (
4362 src#bool "checkers"
4363 (fun () -> conf.checkers)
4364 (fun v -> conf.checkers <- v; setcheckers v);
4365 src#bool "update cursor"
4366 (fun () -> conf.updatecurs)
4367 (fun v -> conf.updatecurs <- v);
4368 src#bool "verbose"
4369 (fun () -> conf.verbose)
4370 (fun v -> conf.verbose <- v);
4371 src#bool "invert colors"
4372 (fun () -> conf.invert)
4373 (fun v -> conf.invert <- v);
4374 src#bool "max fit"
4375 (fun () -> conf.maxhfit)
4376 (fun v -> conf.maxhfit <- v);
4377 src#bool "redirect stderr"
4378 (fun () -> conf.redirectstderr)
4379 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4380 src#string "uri launcher"
4381 (fun () -> conf.urilauncher)
4382 (fun v -> conf.urilauncher <- v);
4383 src#string "path launcher"
4384 (fun () -> conf.pathlauncher)
4385 (fun v -> conf.pathlauncher <- v);
4386 src#string "tile size"
4387 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4388 (fun v ->
4390 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4391 conf.tilew <- max 64 w;
4392 conf.tileh <- max 64 h;
4393 flushtiles ();
4394 with exn ->
4395 state.text <- Printf.sprintf "bad tile size `%s': %s"
4396 v (Printexc.to_string exn));
4397 src#int "texture count"
4398 (fun () -> conf.texcount)
4399 (fun v ->
4400 if realloctexts v
4401 then conf.texcount <- v
4402 else showtext '!' " Failed to set texture count please retry later"
4404 src#int "slice height"
4405 (fun () -> conf.sliceheight)
4406 (fun v ->
4407 conf.sliceheight <- v;
4408 wcmd "sliceh %d" conf.sliceheight;
4410 src#int "anti-aliasing level"
4411 (fun () -> conf.aalevel)
4412 (fun v ->
4413 conf.aalevel <- bound v 0 8;
4414 state.anchor <- getanchor ();
4415 opendoc state.path state.password;
4417 src#string "page scroll scaling factor"
4418 (fun () -> string_of_float conf.pgscale)
4419 (fun v ->
4421 let s = float_of_string v in
4422 conf.pgscale <- s
4423 with exn ->
4424 state.text <- Printf.sprintf
4425 "bad page scroll scaling factor `%s': %s"
4426 v (Printexc.to_string exn)
4429 src#int "ui font size"
4430 (fun () -> fstate.fontsize)
4431 (fun v -> setfontsize (bound v 5 100));
4432 src#int "hint font size"
4433 (fun () -> conf.hfsize)
4434 (fun v -> conf.hfsize <- bound v 5 100);
4435 colorp "background color"
4436 (fun () -> conf.bgcolor)
4437 (fun v -> conf.bgcolor <- v);
4438 src#bool "crop hack"
4439 (fun () -> conf.crophack)
4440 (fun v -> conf.crophack <- v);
4441 src#string "trim fuzz"
4442 (fun () -> irect_to_string conf.trimfuzz)
4443 (fun v ->
4445 conf.trimfuzz <- irect_of_string v;
4446 if conf.trimmargins
4447 then settrim true conf.trimfuzz;
4448 with exn ->
4449 state.text <- Printf.sprintf "bad irect `%s': %s"
4450 v (Printexc.to_string exn)
4452 src#string "throttle"
4453 (fun () ->
4454 match conf.maxwait with
4455 | None -> "show place holder if page is not ready"
4456 | Some time ->
4457 if time = infinity
4458 then "wait for page to fully render"
4459 else
4460 "wait " ^ string_of_float time
4461 ^ " seconds before showing placeholder"
4463 (fun v ->
4465 let f = float_of_string v in
4466 if f <= 0.0
4467 then conf.maxwait <- None
4468 else conf.maxwait <- Some f
4469 with exn ->
4470 state.text <- Printf.sprintf "bad time `%s': %s"
4471 v (Printexc.to_string exn)
4473 src#string "ghyll scroll"
4474 (fun () ->
4475 match conf.ghyllscroll with
4476 | None -> ""
4477 | Some nab -> ghyllscroll_to_string nab
4479 (fun v ->
4481 let gs =
4482 if String.length v = 0
4483 then None
4484 else Some (ghyllscroll_of_string v)
4486 conf.ghyllscroll <- gs
4487 with exn ->
4488 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4489 v (Printexc.to_string exn)
4491 src#string "selection command"
4492 (fun () -> conf.selcmd)
4493 (fun v -> conf.selcmd <- v);
4494 src#colorspace "color space"
4495 (fun () -> colorspace_to_string conf.colorspace)
4496 (fun v ->
4497 conf.colorspace <- colorspace_of_int v;
4498 wcmd "cs %d" v;
4499 load state.layout;
4501 if pbousable ()
4502 then
4503 src#bool "use PBO"
4504 (fun () -> conf.usepbo)
4505 (fun v -> conf.usepbo <- v);
4508 sep ();
4509 src#caption "Document" 0;
4510 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4511 src#caption2 "Pages"
4512 (fun () -> string_of_int state.pagecount) 1;
4513 src#caption2 "Dimensions"
4514 (fun () -> string_of_int (List.length state.pdims)) 1;
4515 if conf.trimmargins
4516 then (
4517 sep ();
4518 src#caption "Trimmed margins" 0;
4519 src#caption2 "Dimensions"
4520 (fun () -> string_of_int (List.length state.pdims)) 1;
4523 sep ();
4524 src#caption "OpenGL" 0;
4525 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4526 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4527 src#reset prevmode prevuioh;
4529 fun () ->
4530 state.text <- "";
4531 let prevmode = state.mode
4532 and prevuioh = state.uioh in
4533 fillsrc prevmode prevuioh;
4534 let source = (src :> lvsource) in
4535 let modehash = findkeyhash conf "info" in
4536 state.uioh <- coe (object (self)
4537 inherit listview ~source ~trusted:true ~modehash as super
4538 val mutable m_prevmemused = 0
4539 method infochanged = function
4540 | Memused ->
4541 if m_prevmemused != state.memused
4542 then (
4543 m_prevmemused <- state.memused;
4544 G.postRedisplay "memusedchanged";
4546 | Pdim -> G.postRedisplay "pdimchanged"
4547 | Docinfo -> fillsrc prevmode prevuioh
4549 method key key mask =
4550 if not (Wsi.withctrl mask)
4551 then
4552 match key with
4553 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4554 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4555 | _ -> super#key key mask
4556 else super#key key mask
4557 end);
4558 G.postRedisplay "info";
4561 let enterhelpmode =
4562 let source =
4563 (object
4564 inherit lvsourcebase
4565 method getitemcount = Array.length state.help
4566 method getitem n =
4567 let s, l, _ = state.help.(n) in
4568 (s, l)
4570 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4571 let optuioh =
4572 if not cancel
4573 then (
4574 m_qsearch <- qsearch;
4575 match state.help.(active) with
4576 | _, _, Action f -> Some (f uioh)
4577 | _ -> Some (uioh)
4579 else None
4581 m_active <- active;
4582 m_first <- first;
4583 m_pan <- pan;
4584 optuioh
4586 method hasaction n =
4587 match state.help.(n) with
4588 | _, _, Action _ -> true
4589 | _ -> false
4591 initializer
4592 m_active <- -1
4593 end)
4594 in fun () ->
4595 let modehash = findkeyhash conf "help" in
4596 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4597 G.postRedisplay "help";
4600 let entermsgsmode =
4601 let msgsource =
4602 let re = Str.regexp "[\r\n]" in
4603 (object
4604 inherit lvsourcebase
4605 val mutable m_items = [||]
4607 method getitemcount = 1 + Array.length m_items
4609 method getitem n =
4610 if n = 0
4611 then "[Clear]", 0
4612 else m_items.(n-1), 0
4614 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4615 ignore uioh;
4616 if not cancel
4617 then (
4618 if active = 0
4619 then Buffer.clear state.errmsgs;
4620 m_qsearch <- qsearch;
4622 m_active <- active;
4623 m_first <- first;
4624 m_pan <- pan;
4625 None
4627 method hasaction n =
4628 n = 0
4630 method reset =
4631 state.newerrmsgs <- false;
4632 let l = Str.split re (Buffer.contents state.errmsgs) in
4633 m_items <- Array.of_list l
4635 initializer
4636 m_active <- 0
4637 end)
4638 in fun () ->
4639 state.text <- "";
4640 msgsource#reset;
4641 let source = (msgsource :> lvsource) in
4642 let modehash = findkeyhash conf "listview" in
4643 state.uioh <- coe (object
4644 inherit listview ~source ~trusted:false ~modehash as super
4645 method display =
4646 if state.newerrmsgs
4647 then msgsource#reset;
4648 super#display
4649 end);
4650 G.postRedisplay "msgs";
4653 let quickbookmark ?title () =
4654 match state.layout with
4655 | [] -> ()
4656 | l :: _ ->
4657 let title =
4658 match title with
4659 | None ->
4660 let sec = Unix.gettimeofday () in
4661 let tm = Unix.localtime sec in
4662 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4663 (l.pageno+1)
4664 tm.Unix.tm_mday
4665 tm.Unix.tm_mon
4666 (tm.Unix.tm_year + 1900)
4667 tm.Unix.tm_hour
4668 tm.Unix.tm_min
4669 | Some title -> title
4671 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4674 let doreshape w h =
4675 state.fullscreen <- None;
4676 Wsi.reshape w h;
4679 let setautoscrollspeed step goingdown =
4680 let incr = max 1 ((abs step) / 2) in
4681 let incr = if goingdown then incr else -incr in
4682 let astep = step + incr in
4683 state.autoscroll <- Some astep;
4686 let gotounder = function
4687 | Ulinkgoto (pageno, top) ->
4688 if pageno >= 0
4689 then (
4690 addnav ();
4691 gotopage1 pageno top;
4694 | Ulinkuri s ->
4695 gotouri s
4697 | Uremote (filename, pageno) ->
4698 let path =
4699 if Sys.file_exists filename
4700 then filename
4701 else
4702 let dir = Filename.dirname state.path in
4703 let path = Filename.concat dir filename in
4704 if Sys.file_exists path
4705 then path
4706 else ""
4708 if String.length path > 0
4709 then (
4710 let anchor = getanchor () in
4711 let ranchor = state.path, state.password, anchor in
4712 state.anchor <- (pageno, 0.0, 0.0);
4713 state.ranchors <- ranchor :: state.ranchors;
4714 opendoc path "";
4716 else showtext '!' ("Could not find " ^ filename)
4718 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4721 let canpan () =
4722 match conf.columns with
4723 | Csplit _ -> true
4724 | _ -> conf.zoom > 1.0
4727 let viewkeyboard key mask =
4728 let enttext te =
4729 let mode = state.mode in
4730 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4731 state.text <- "";
4732 enttext ();
4733 G.postRedisplay "view:enttext"
4735 let ctrl = Wsi.withctrl mask in
4736 let existsinrow pageno (columns, coverA, coverB) p =
4737 let last = ((pageno - coverA) mod columns) + columns in
4738 let rec any = function
4739 | [] -> false
4740 | l :: rest ->
4741 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4742 then p l
4743 else (
4744 if not (p l)
4745 then (if l.pageno = last then false else any rest)
4746 else true
4749 any state.layout
4751 let key =
4752 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4754 match key with
4755 | 81 -> (* Q *)
4756 exit 0
4758 | 0xff63 -> (* insert *)
4759 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4760 then (
4761 state.mode <- LinkNav (Ltgendir 0);
4762 gotoy state.y;
4764 else showtext '!' "Keyboard link navigation does not work under rotation"
4766 | 0xff1b | 113 -> (* escape / q *)
4767 begin match state.mstate with
4768 | Mzoomrect _ ->
4769 state.mstate <- Mnone;
4770 Wsi.setcursor Wsi.CURSOR_INHERIT;
4771 G.postRedisplay "kill zoom rect";
4772 | _ ->
4773 begin match state.mode with
4774 | LinkNav _ ->
4775 state.mode <- View;
4776 G.postRedisplay "esc leave linknav"
4777 | _ ->
4778 match state.ranchors with
4779 | [] -> raise Quit
4780 | (path, password, anchor) :: rest ->
4781 state.ranchors <- rest;
4782 state.anchor <- anchor;
4783 opendoc path password
4784 end;
4785 end;
4787 | 0xff08 -> (* backspace *)
4788 gotoghyll (getnav ~-1)
4790 | 111 -> (* o *)
4791 enteroutlinemode ()
4793 | 117 -> (* u *)
4794 state.rects <- [];
4795 state.text <- "";
4796 G.postRedisplay "dehighlight";
4798 | 47 | 63 -> (* / ? *)
4799 let ondone isforw s =
4800 cbput state.hists.pat s;
4801 state.searchpattern <- s;
4802 search s isforw
4804 let s = String.create 1 in
4805 s.[0] <- Char.chr key;
4806 enttext (s, "", Some (onhist state.hists.pat),
4807 textentry, ondone (key = 47), true)
4809 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4810 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4811 setzoom (conf.zoom +. incr)
4813 | 43 | 0xffab -> (* + *)
4814 let ondone s =
4815 let n =
4816 try int_of_string s with exc ->
4817 state.text <- Printf.sprintf "bad integer `%s': %s"
4818 s (Printexc.to_string exc);
4819 max_int
4821 if n != max_int
4822 then (
4823 conf.pagebias <- n;
4824 state.text <- "page bias is now " ^ string_of_int n;
4827 enttext ("page bias: ", "", None, intentry, ondone, true)
4829 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4830 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4831 setzoom (max 0.01 (conf.zoom -. decr))
4833 | 45 | 0xffad -> (* - *)
4834 let ondone msg = state.text <- msg in
4835 enttext (
4836 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4837 optentry state.mode, ondone, true
4840 | 48 when ctrl -> (* ctrl-0 *)
4841 setzoom 1.0
4843 | 49 when ctrl -> (* ctrl-1 *)
4844 let cols =
4845 match conf.columns with
4846 | Csingle _ | Cmulti _ -> 1
4847 | Csplit (n, _) -> n
4849 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4850 if zoom < 1.0
4851 then setzoom zoom
4853 | 0xffc6 -> (* f9 *)
4854 togglebirdseye ()
4856 | 57 when ctrl -> (* ctrl-9 *)
4857 togglebirdseye ()
4859 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4860 when not ctrl -> (* 0..9 *)
4861 let ondone s =
4862 let n =
4863 try int_of_string s with exc ->
4864 state.text <- Printf.sprintf "bad integer `%s': %s"
4865 s (Printexc.to_string exc);
4868 if n >= 0
4869 then (
4870 addnav ();
4871 cbput state.hists.pag (string_of_int n);
4872 gotopage1 (n + conf.pagebias - 1) 0;
4875 let pageentry text key =
4876 match Char.unsafe_chr key with
4877 | 'g' -> TEdone text
4878 | _ -> intentry text key
4880 let text = "x" in text.[0] <- Char.chr key;
4881 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4883 | 98 -> (* b *)
4884 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4885 reshape conf.winw conf.winh;
4887 | 108 -> (* l *)
4888 conf.hlinks <- not conf.hlinks;
4889 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4890 G.postRedisplay "toggle highlightlinks";
4892 | 70 -> (* F *)
4893 state.glinks <- true;
4894 let mode = state.mode in
4895 state.mode <- Textentry (
4896 (":", "", None, linknentry, linkndone gotounder, false),
4897 (fun _ ->
4898 state.glinks <- false;
4899 state.mode <- mode)
4901 state.text <- "";
4902 G.postRedisplay "view:linkent(F)"
4904 | 121 -> (* y *)
4905 state.glinks <- true;
4906 let mode = state.mode in
4907 state.mode <- Textentry (
4908 (":", "", None, linknentry, linkndone (fun under ->
4909 match Ne.pipe () with
4910 | Ne.Exn exn ->
4911 showtext '!' (Printf.sprintf "pipe failed: %s"
4912 (Printexc.to_string exn));
4913 | Ne.Res (r, w) ->
4914 let popened =
4915 try popen conf.selcmd [r, 0; w, -1]; true
4916 with exn ->
4917 showtext '!'
4918 (Printf.sprintf "failed to execute %s: %s"
4919 conf.selcmd (Printexc.to_string exn));
4920 false
4922 let clo cap fd =
4923 Ne.clo fd (fun msg ->
4924 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4927 let s = undertext under in
4928 if popened
4929 then
4930 (try
4931 let l = String.length s in
4932 let n = Unix.write w s 0 l in
4933 if n != l
4934 then
4935 showtext '!'
4936 (Printf.sprintf
4937 "failed to write %d characters to sel pipe, wrote %d"
4940 with exn ->
4941 showtext '!'
4942 (Printf.sprintf "failed to write to sel pipe: %s"
4943 (Printexc.to_string exn)
4946 else dolog "%s" s;
4947 clo "pipe/r" r;
4948 clo "pipe/w" w;
4949 ), false
4951 fun _ ->
4952 state.glinks <- false;
4953 state.mode <- mode
4955 state.text <- "";
4956 G.postRedisplay "view:linkent"
4958 | 97 -> (* a *)
4959 begin match state.autoscroll with
4960 | Some step ->
4961 conf.autoscrollstep <- step;
4962 state.autoscroll <- None
4963 | None ->
4964 if conf.autoscrollstep = 0
4965 then state.autoscroll <- Some 1
4966 else state.autoscroll <- Some conf.autoscrollstep
4969 | 112 when ctrl -> (* ctrl-p *)
4970 launchpath ()
4972 | 80 -> (* P *)
4973 setpresentationmode (not conf.presentation);
4974 showtext ' ' ("presentation mode " ^
4975 if conf.presentation then "on" else "off");
4977 | 102 -> (* f *)
4978 begin match state.fullscreen with
4979 | None ->
4980 state.fullscreen <- Some (conf.winw, conf.winh);
4981 Wsi.fullscreen ()
4982 | Some (w, h) ->
4983 state.fullscreen <- None;
4984 doreshape w h
4987 | 112 | 78 -> (* p|N *)
4988 search state.searchpattern false
4990 | 110 | 0xffc0 -> (* n|F3 *)
4991 search state.searchpattern true
4993 | 116 -> (* t *)
4994 begin match state.layout with
4995 | [] -> ()
4996 | l :: _ ->
4997 gotoy_and_clear_text (getpagey l.pageno)
5000 | 32 -> (* space *)
5001 begin match state.layout with
5002 | [] -> ()
5003 | l :: rest ->
5004 match conf.columns with
5005 | Csingle _ ->
5006 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5007 then
5008 let y = clamp (pgscale conf.winh) in
5009 gotoghyll y
5010 else
5011 let pageno = min (l.pageno+1) (state.pagecount-1) in
5012 gotoghyll (getpagey pageno)
5013 | Cmulti ((c, _, _) as cl, _) ->
5014 if conf.presentation
5015 && (existsinrow l.pageno cl
5016 (fun l -> l.pageh > l.pagey + l.pagevh))
5017 then
5018 let y = clamp (pgscale conf.winh) in
5019 gotoghyll y
5020 else
5021 let pageno = min (l.pageno+c) (state.pagecount-1) in
5022 gotoghyll (getpagey pageno)
5023 | Csplit (n, _) ->
5024 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5025 then
5026 let pagey, pageh = getpageyh l.pageno in
5027 let pagey = pagey + pageh * l.pagecol in
5028 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5029 gotoghyll (pagey + pageh + ips)
5032 | 0xff9f | 0xffff -> (* delete *)
5033 begin match state.layout with
5034 | [] -> ()
5035 | l :: _ ->
5036 match conf.columns with
5037 | Csingle _ ->
5038 if conf.presentation && l.pagey != 0
5039 then
5040 gotoghyll (clamp (pgscale ~-(conf.winh)))
5041 else
5042 let pageno = max 0 (l.pageno-1) in
5043 gotoghyll (getpagey pageno)
5044 | Cmulti ((c, _, coverB) as cl, _) ->
5045 if conf.presentation &&
5046 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5047 then
5048 gotoghyll (clamp (pgscale ~-(conf.winh)))
5049 else
5050 let decr =
5051 if l.pageno = state.pagecount - coverB
5052 then 1
5053 else c
5055 let pageno = max 0 (l.pageno-decr) in
5056 gotoghyll (getpagey pageno)
5057 | Csplit (n, _) ->
5058 let y =
5059 if l.pagecol = 0
5060 then
5061 if l.pageno = 0
5062 then l.pagey
5063 else
5064 let pageno = max 0 (l.pageno-1) in
5065 let pagey, pageh = getpageyh pageno in
5066 pagey + (n-1)*pageh
5067 else
5068 let pagey, pageh = getpageyh l.pageno in
5069 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5071 gotoghyll y
5074 | 61 -> (* = *)
5075 showtext ' ' (describe_location ());
5077 | 119 -> (* w *)
5078 begin match state.layout with
5079 | [] -> ()
5080 | l :: _ ->
5081 doreshape (l.pagew + state.scrollw) l.pageh;
5082 G.postRedisplay "w"
5085 | 39 -> (* ' *)
5086 enterbookmarkmode ()
5088 | 104 | 0xffbe -> (* h|F1 *)
5089 enterhelpmode ()
5091 | 105 -> (* i *)
5092 enterinfomode ()
5094 | 101 when conf.redirectstderr -> (* e *)
5095 entermsgsmode ()
5097 | 109 -> (* m *)
5098 let ondone s =
5099 match state.layout with
5100 | l :: _ ->
5101 if String.length s > 0
5102 then
5103 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5104 | _ -> ()
5106 enttext ("bookmark: ", "", None, textentry, ondone, true)
5108 | 126 -> (* ~ *)
5109 quickbookmark ();
5110 showtext ' ' "Quick bookmark added";
5112 | 122 -> (* z *)
5113 begin match state.layout with
5114 | l :: _ ->
5115 let rect = getpdimrect l.pagedimno in
5116 let w, h =
5117 if conf.crophack
5118 then
5119 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5120 truncate (1.2 *. (rect.(3) -. rect.(0))))
5121 else
5122 (truncate (rect.(1) -. rect.(0)),
5123 truncate (rect.(3) -. rect.(0)))
5125 let w = truncate ((float w)*.conf.zoom)
5126 and h = truncate ((float h)*.conf.zoom) in
5127 if w != 0 && h != 0
5128 then (
5129 state.anchor <- getanchor ();
5130 doreshape (w + state.scrollw) (h + conf.interpagespace)
5132 G.postRedisplay "z";
5134 | [] -> ()
5137 | 50 when ctrl -> (* ctrl-2 *)
5138 let maxw = getmaxw () in
5139 if maxw > 0.0
5140 then setzoom (maxw /. float conf.winw)
5142 | 60 | 62 -> (* < > *)
5143 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5145 | 91 | 93 -> (* [ ] *)
5146 conf.colorscale <-
5147 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5149 G.postRedisplay "brightness";
5151 | 99 when state.mode = View -> (* c *)
5152 let (c, a, b), z =
5153 match state.prevcolumns with
5154 | None -> (1, 0, 0), 1.0
5155 | Some (columns, z) ->
5156 let cab =
5157 match columns with
5158 | Csplit (c, _) -> -c, 0, 0
5159 | Cmulti ((c, a, b), _) -> c, a, b
5160 | Csingle _ -> 1, 0, 0
5162 cab, z
5164 setcolumns View c a b;
5165 setzoom z;
5167 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5168 setzoom state.prevzoom
5170 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5171 begin match state.autoscroll with
5172 | None ->
5173 begin match state.mode with
5174 | Birdseye beye -> upbirdseye 1 beye
5175 | _ ->
5176 if ctrl
5177 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5178 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5180 | Some n ->
5181 setautoscrollspeed n false
5184 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5185 begin match state.autoscroll with
5186 | None ->
5187 begin match state.mode with
5188 | Birdseye beye -> downbirdseye 1 beye
5189 | _ ->
5190 if ctrl
5191 then gotoy_and_clear_text (clamp (conf.winh/2))
5192 else gotoy_and_clear_text (clamp conf.scrollstep)
5194 | Some n ->
5195 setautoscrollspeed n true
5198 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5199 if canpan ()
5200 then
5201 let dx =
5202 if ctrl
5203 then conf.winw / 2
5204 else conf.hscrollstep
5206 let dx = if key = 0xff51 then dx else -dx in
5207 state.x <- state.x + dx;
5208 gotoy_and_clear_text state.y
5209 else (
5210 state.text <- "";
5211 G.postRedisplay "lef/right"
5214 | 0xff55 | 0xff9a -> (* (kp) prior *)
5215 let y =
5216 if ctrl
5217 then
5218 match state.layout with
5219 | [] -> state.y
5220 | l :: _ -> state.y - l.pagey
5221 else
5222 clamp (pgscale (-conf.winh))
5224 gotoghyll y
5226 | 0xff56 | 0xff9b -> (* (kp) next *)
5227 let y =
5228 if ctrl
5229 then
5230 match List.rev state.layout with
5231 | [] -> state.y
5232 | l :: _ -> getpagey l.pageno
5233 else
5234 clamp (pgscale conf.winh)
5236 gotoghyll y
5238 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5239 gotoghyll 0
5240 | 71 | 0xff57 | 0xff9c -> (* G end *)
5241 gotoghyll (clamp state.maxy)
5243 | 0xff53 when Wsi.withalt mask -> (* alt-right *)
5244 gotoghyll (getnav 1)
5245 | 0xff51 when Wsi.withalt mask -> (* alt-left *)
5246 gotoghyll (getnav ~-1)
5248 | 114 -> (* r *)
5249 reload ()
5251 | 118 when conf.debug -> (* v *)
5252 state.rects <- [];
5253 List.iter (fun l ->
5254 match getopaque l.pageno with
5255 | None -> ()
5256 | Some opaque ->
5257 let x0, y0, x1, y1 = pagebbox opaque in
5258 let a,b = float x0, float y0 in
5259 let c,d = float x1, float y0 in
5260 let e,f = float x1, float y1 in
5261 let h,j = float x0, float y1 in
5262 let rect = (a,b,c,d,e,f,h,j) in
5263 debugrect rect;
5264 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5265 ) state.layout;
5266 G.postRedisplay "v";
5268 | _ ->
5269 vlog "huh? %s" (Wsi.keyname key)
5272 let linknavkeyboard key mask linknav =
5273 let getpage pageno =
5274 let rec loop = function
5275 | [] -> None
5276 | l :: _ when l.pageno = pageno -> Some l
5277 | _ :: rest -> loop rest
5278 in loop state.layout
5280 let doexact (pageno, n) =
5281 match getopaque pageno, getpage pageno with
5282 | Some opaque, Some l ->
5283 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5284 then
5285 let under = getlink opaque n in
5286 G.postRedisplay "link gotounder";
5287 gotounder under;
5288 state.mode <- View;
5289 else
5290 let opt, dir =
5291 match key with
5292 | 0xff50 -> (* home *)
5293 Some (findlink opaque LDfirst), -1
5295 | 0xff57 -> (* end *)
5296 Some (findlink opaque LDlast), 1
5298 | 0xff51 -> (* left *)
5299 Some (findlink opaque (LDleft n)), -1
5301 | 0xff53 -> (* right *)
5302 Some (findlink opaque (LDright n)), 1
5304 | 0xff52 -> (* up *)
5305 Some (findlink opaque (LDup n)), -1
5307 | 0xff54 -> (* down *)
5308 Some (findlink opaque (LDdown n)), 1
5310 | _ -> None, 0
5312 let pwl l dir =
5313 begin match findpwl l.pageno dir with
5314 | Pwlnotfound -> ()
5315 | Pwl pageno ->
5316 let notfound dir =
5317 state.mode <- LinkNav (Ltgendir dir);
5318 let y, h = getpageyh pageno in
5319 let y =
5320 if dir < 0
5321 then y + h - conf.winh
5322 else y
5324 gotoy y
5326 begin match getopaque pageno, getpage pageno with
5327 | Some opaque, Some _ ->
5328 let link =
5329 let ld = if dir > 0 then LDfirst else LDlast in
5330 findlink opaque ld
5332 begin match link with
5333 | Lfound m ->
5334 showlinktype (getlink opaque m);
5335 state.mode <- LinkNav (Ltexact (pageno, m));
5336 G.postRedisplay "linknav jpage";
5337 | _ -> notfound dir
5338 end;
5339 | _ -> notfound dir
5340 end;
5341 end;
5343 begin match opt with
5344 | Some Lnotfound -> pwl l dir;
5345 | Some (Lfound m) ->
5346 if m = n
5347 then pwl l dir
5348 else (
5349 let _, y0, _, y1 = getlinkrect opaque m in
5350 if y0 < l.pagey
5351 then gotopage1 l.pageno y0
5352 else (
5353 let d = fstate.fontsize + 1 in
5354 if y1 - l.pagey > l.pagevh - d
5355 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5356 else G.postRedisplay "linknav";
5358 showlinktype (getlink opaque m);
5359 state.mode <- LinkNav (Ltexact (l.pageno, m));
5362 | None -> viewkeyboard key mask
5363 end;
5364 | _ -> viewkeyboard key mask
5366 if key = 0xff63
5367 then (
5368 state.mode <- View;
5369 G.postRedisplay "leave linknav"
5371 else
5372 match linknav with
5373 | Ltgendir _ -> viewkeyboard key mask
5374 | Ltexact exact -> doexact exact
5377 let keyboard key mask =
5378 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5379 then wcmd "interrupt"
5380 else state.uioh <- state.uioh#key key mask
5383 let birdseyekeyboard key mask
5384 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5385 let incr =
5386 match conf.columns with
5387 | Csingle _ -> 1
5388 | Cmulti ((c, _, _), _) -> c
5389 | Csplit _ -> failwith "bird's eye split mode"
5391 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5392 match key with
5393 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5394 let y, h = getpageyh pageno in
5395 let top = (conf.winh - h) / 2 in
5396 gotoy (max 0 (y - top))
5397 | 0xff0d (* enter *)
5398 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5399 | 0xff1b -> leavebirdseye beye true (* escape *)
5400 | 0xff52 -> upbirdseye incr beye (* up *)
5401 | 0xff54 -> downbirdseye incr beye (* down *)
5402 | 0xff51 -> upbirdseye 1 beye (* left *)
5403 | 0xff53 -> downbirdseye 1 beye (* right *)
5405 | 0xff55 -> (* prior *)
5406 begin match state.layout with
5407 | l :: _ ->
5408 if l.pagey != 0
5409 then (
5410 state.mode <- Birdseye (
5411 oconf, leftx, l.pageno, hooverpageno, anchor
5413 gotopage1 l.pageno 0;
5415 else (
5416 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5417 match layout with
5418 | [] -> gotoy (clamp (-conf.winh))
5419 | l :: _ ->
5420 state.mode <- Birdseye (
5421 oconf, leftx, l.pageno, hooverpageno, anchor
5423 gotopage1 l.pageno 0
5426 | [] -> gotoy (clamp (-conf.winh))
5427 end;
5429 | 0xff56 -> (* next *)
5430 begin match List.rev state.layout with
5431 | l :: _ ->
5432 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5433 begin match layout with
5434 | [] ->
5435 let incr = l.pageh - l.pagevh in
5436 if incr = 0
5437 then (
5438 state.mode <-
5439 Birdseye (
5440 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5442 G.postRedisplay "birdseye pagedown";
5444 else gotoy (clamp (incr + conf.interpagespace*2));
5446 | l :: _ ->
5447 state.mode <-
5448 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5449 gotopage1 l.pageno 0;
5452 | [] -> gotoy (clamp conf.winh)
5453 end;
5455 | 0xff50 -> (* home *)
5456 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5457 gotopage1 0 0
5459 | 0xff57 -> (* end *)
5460 let pageno = state.pagecount - 1 in
5461 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5462 if not (pagevisible state.layout pageno)
5463 then
5464 let h =
5465 match List.rev state.pdims with
5466 | [] -> conf.winh
5467 | (_, _, h, _) :: _ -> h
5469 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5470 else G.postRedisplay "birdseye end";
5471 | _ -> viewkeyboard key mask
5474 let drawpage l linkindexbase =
5475 let color =
5476 match state.mode with
5477 | Textentry _ -> scalecolor 0.4
5478 | LinkNav _
5479 | View -> scalecolor 1.0
5480 | Birdseye (_, _, pageno, hooverpageno, _) ->
5481 if l.pageno = hooverpageno
5482 then scalecolor 0.9
5483 else (
5484 if l.pageno = pageno
5485 then scalecolor 1.0
5486 else scalecolor 0.8
5489 drawtiles l color;
5490 begin match getopaque l.pageno with
5491 | Some opaque ->
5492 if tileready l l.pagex l.pagey
5493 then
5494 let x = l.pagedispx - l.pagex
5495 and y = l.pagedispy - l.pagey in
5496 let hlmask =
5497 match conf.columns with
5498 | Csingle _ | Cmulti _ ->
5499 (if conf.hlinks then 1 else 0)
5500 + (if state.glinks
5501 && not (isbirdseye state.mode) then 2 else 0)
5502 | _ -> 0
5504 let s =
5505 match state.mode with
5506 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5507 | _ -> ""
5509 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5510 else 0
5512 | _ -> 0
5513 end;
5516 let scrollindicator () =
5517 let sbw, ph, sh = state.uioh#scrollph in
5518 let sbh, pw, sw = state.uioh#scrollpw in
5520 GlDraw.color (0.64, 0.64, 0.64);
5521 GlDraw.rect
5522 (float (conf.winw - sbw), 0.)
5523 (float conf.winw, float conf.winh)
5525 GlDraw.rect
5526 (0., float (conf.winh - sbh))
5527 (float (conf.winw - state.scrollw - 1), float conf.winh)
5529 GlDraw.color (0.0, 0.0, 0.0);
5531 GlDraw.rect
5532 (float (conf.winw - sbw), ph)
5533 (float conf.winw, ph +. sh)
5535 GlDraw.rect
5536 (pw, float (conf.winh - sbh))
5537 (pw +. sw, float conf.winh)
5541 let showsel () =
5542 match state.mstate with
5543 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5546 | Msel ((x0, y0), (x1, y1)) ->
5547 let rec loop = function
5548 | l :: ls ->
5549 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5550 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5551 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5552 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5553 then
5554 match getopaque l.pageno with
5555 | Some opaque ->
5556 let x0, y0 = pagetranslatepoint l x0 y0 in
5557 let x1, y1 = pagetranslatepoint l x1 y1 in
5558 seltext opaque (x0, y0, x1, y1);
5559 | _ -> ()
5560 else loop ls
5561 | [] -> ()
5563 loop state.layout
5566 let showrects rects =
5567 Gl.enable `blend;
5568 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5569 GlDraw.polygon_mode `both `fill;
5570 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5571 List.iter
5572 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5573 List.iter (fun l ->
5574 if l.pageno = pageno
5575 then (
5576 let dx = float (l.pagedispx - l.pagex) in
5577 let dy = float (l.pagedispy - l.pagey) in
5578 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5579 GlDraw.begins `quads;
5581 GlDraw.vertex2 (x0+.dx, y0+.dy);
5582 GlDraw.vertex2 (x1+.dx, y1+.dy);
5583 GlDraw.vertex2 (x2+.dx, y2+.dy);
5584 GlDraw.vertex2 (x3+.dx, y3+.dy);
5586 GlDraw.ends ();
5588 ) state.layout
5589 ) rects
5591 Gl.disable `blend;
5594 let display () =
5595 GlClear.color (scalecolor2 conf.bgcolor);
5596 GlClear.clear [`color];
5597 let rec loop linkindexbase = function
5598 | l :: rest ->
5599 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5600 loop linkindexbase rest
5601 | [] -> ()
5603 loop 0 state.layout;
5604 let rects =
5605 match state.mode with
5606 | LinkNav (Ltexact (pageno, linkno)) ->
5607 begin match getopaque pageno with
5608 | Some opaque ->
5609 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5610 (pageno, 5, (
5611 float x0, float y0,
5612 float x1, float y0,
5613 float x1, float y1,
5614 float x0, float y1)
5615 ) :: state.rects
5616 | None -> state.rects
5618 | _ -> state.rects
5620 showrects rects;
5621 showsel ();
5622 state.uioh#display;
5623 begin match state.mstate with
5624 | Mzoomrect ((x0, y0), (x1, y1)) ->
5625 Gl.enable `blend;
5626 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5627 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5628 GlDraw.rect (float x0, float y0)
5629 (float x1, float y1);
5630 Gl.disable `blend;
5631 | _ -> ()
5632 end;
5633 enttext ();
5634 scrollindicator ();
5635 Wsi.swapb ();
5638 let zoomrect x y x1 y1 =
5639 let x0 = min x x1
5640 and x1 = max x x1
5641 and y0 = min y y1 in
5642 gotoy (state.y + y0);
5643 state.anchor <- getanchor ();
5644 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5645 let margin =
5646 if state.w < conf.winw - state.scrollw
5647 then (conf.winw - state.scrollw - state.w) / 2
5648 else 0
5650 state.x <- (state.x + margin) - x0;
5651 setzoom zoom;
5652 Wsi.setcursor Wsi.CURSOR_INHERIT;
5653 state.mstate <- Mnone;
5656 let scrollx x =
5657 let winw = conf.winw - state.scrollw - 1 in
5658 let s = float x /. float winw in
5659 let destx = truncate (float (state.w + winw) *. s) in
5660 state.x <- winw - destx;
5661 gotoy_and_clear_text state.y;
5662 state.mstate <- Mscrollx;
5665 let scrolly y =
5666 let s = float y /. float conf.winh in
5667 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5668 gotoy_and_clear_text desty;
5669 state.mstate <- Mscrolly;
5672 let viewmouse button down x y mask =
5673 match button with
5674 | n when (n == 4 || n == 5) && not down ->
5675 if Wsi.withctrl mask
5676 then (
5677 match state.mstate with
5678 | Mzoom (oldn, i) ->
5679 if oldn = n
5680 then (
5681 if i = 2
5682 then
5683 let incr =
5684 match n with
5685 | 5 ->
5686 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5687 | _ ->
5688 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5690 let zoom = conf.zoom -. incr in
5691 setzoom zoom;
5692 state.mstate <- Mzoom (n, 0);
5693 else
5694 state.mstate <- Mzoom (n, i+1);
5696 else state.mstate <- Mzoom (n, 0)
5698 | _ -> state.mstate <- Mzoom (n, 0)
5700 else (
5701 match state.autoscroll with
5702 | Some step -> setautoscrollspeed step (n=4)
5703 | None ->
5704 let incr =
5705 if n = 4
5706 then -conf.scrollstep
5707 else conf.scrollstep
5709 let incr = incr * 2 in
5710 let y = clamp incr in
5711 gotoy_and_clear_text y
5714 | n when (n = 6 || n = 7) && not down && canpan () ->
5715 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5716 gotoy_and_clear_text state.y
5718 | 1 when Wsi.withctrl mask ->
5719 if down
5720 then (
5721 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5722 state.mstate <- Mpan (x, y)
5724 else
5725 state.mstate <- Mnone
5727 | 3 ->
5728 if down
5729 then (
5730 Wsi.setcursor Wsi.CURSOR_CYCLE;
5731 let p = (x, y) in
5732 state.mstate <- Mzoomrect (p, p)
5734 else (
5735 match state.mstate with
5736 | Mzoomrect ((x0, y0), _) ->
5737 if abs (x-x0) > 10 && abs (y - y0) > 10
5738 then zoomrect x0 y0 x y
5739 else (
5740 state.mstate <- Mnone;
5741 Wsi.setcursor Wsi.CURSOR_INHERIT;
5742 G.postRedisplay "kill accidental zoom rect";
5744 | _ ->
5745 Wsi.setcursor Wsi.CURSOR_INHERIT;
5746 state.mstate <- Mnone
5749 | 1 when x > conf.winw - state.scrollw ->
5750 if down
5751 then
5752 let _, position, sh = state.uioh#scrollph in
5753 if y > truncate position && y < truncate (position +. sh)
5754 then state.mstate <- Mscrolly
5755 else scrolly y
5756 else
5757 state.mstate <- Mnone
5759 | 1 when y > conf.winh - state.hscrollh ->
5760 if down
5761 then
5762 let _, position, sw = state.uioh#scrollpw in
5763 if x > truncate position && x < truncate (position +. sw)
5764 then state.mstate <- Mscrollx
5765 else scrollx x
5766 else
5767 state.mstate <- Mnone
5769 | 1 ->
5770 let dest = if down then getunder x y else Unone in
5771 begin match dest with
5772 | Ulinkgoto _
5773 | Ulinkuri _
5774 | Uremote _
5775 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5776 gotounder dest
5778 | Unone when down ->
5779 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5780 state.mstate <- Mpan (x, y);
5782 | Unone | Utext _ ->
5783 if down
5784 then (
5785 if conf.angle mod 360 = 0
5786 then (
5787 state.mstate <- Msel ((x, y), (x, y));
5788 G.postRedisplay "mouse select";
5791 else (
5792 match state.mstate with
5793 | Mnone -> ()
5795 | Mzoom _ | Mscrollx | Mscrolly ->
5796 state.mstate <- Mnone
5798 | Mzoomrect ((x0, y0), _) ->
5799 zoomrect x0 y0 x y
5801 | Mpan _ ->
5802 Wsi.setcursor Wsi.CURSOR_INHERIT;
5803 state.mstate <- Mnone
5805 | Msel ((x0, y0), (x1, y1)) ->
5806 let rec loop = function
5807 | [] -> ()
5808 | l :: rest ->
5809 let inside =
5810 let a0 = l.pagedispy in
5811 let a1 = a0 + l.pagevh in
5812 let b0 = l.pagedispx in
5813 let b1 = b0 + l.pagevw in
5814 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5815 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5817 if inside
5818 then
5819 match getopaque l.pageno with
5820 | Some opaque ->
5821 begin
5822 match Ne.pipe () with
5823 | Ne.Exn exn ->
5824 showtext '!'
5825 (Printf.sprintf
5826 "can not create sel pipe: %s"
5827 (Printexc.to_string exn));
5828 | Ne.Res (r, w) ->
5829 let doclose what fd =
5830 Ne.clo fd (fun msg ->
5831 dolog "%s close failed: %s" what msg)
5834 popen conf.selcmd [r, 0; w, -1];
5835 copysel w opaque;
5836 doclose "pipe/r" r;
5837 G.postRedisplay "copysel";
5838 with exn ->
5839 dolog "can not execute %S: %s"
5840 conf.selcmd (Printexc.to_string exn);
5841 doclose "pipe/r" r;
5842 doclose "pipe/w" w;
5844 | None -> ()
5845 else loop rest
5847 loop state.layout;
5848 Wsi.setcursor Wsi.CURSOR_INHERIT;
5849 state.mstate <- Mnone;
5853 | _ -> ()
5856 let birdseyemouse button down x y mask
5857 (conf, leftx, _, hooverpageno, anchor) =
5858 match button with
5859 | 1 when down ->
5860 let rec loop = function
5861 | [] -> ()
5862 | l :: rest ->
5863 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5864 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5865 then (
5866 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5868 else loop rest
5870 loop state.layout
5871 | 3 -> ()
5872 | _ -> viewmouse button down x y mask
5875 let mouse button down x y mask =
5876 state.uioh <- state.uioh#button button down x y mask;
5879 let motion ~x ~y =
5880 state.uioh <- state.uioh#motion x y
5883 let pmotion ~x ~y =
5884 state.uioh <- state.uioh#pmotion x y;
5887 let uioh = object
5888 method display = ()
5890 method key key mask =
5891 begin match state.mode with
5892 | Textentry textentry -> textentrykeyboard key mask textentry
5893 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5894 | View -> viewkeyboard key mask
5895 | LinkNav linknav -> linknavkeyboard key mask linknav
5896 end;
5897 state.uioh
5899 method button button bstate x y mask =
5900 begin match state.mode with
5901 | LinkNav _
5902 | View -> viewmouse button bstate x y mask
5903 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5904 | Textentry _ -> ()
5905 end;
5906 state.uioh
5908 method motion x y =
5909 begin match state.mode with
5910 | Textentry _ -> ()
5911 | View | Birdseye _ | LinkNav _ ->
5912 match state.mstate with
5913 | Mzoom _ | Mnone -> ()
5915 | Mpan (x0, y0) ->
5916 let dx = x - x0
5917 and dy = y0 - y in
5918 state.mstate <- Mpan (x, y);
5919 if canpan ()
5920 then state.x <- state.x + dx;
5921 let y = clamp dy in
5922 gotoy_and_clear_text y
5924 | Msel (a, _) ->
5925 state.mstate <- Msel (a, (x, y));
5926 G.postRedisplay "motion select";
5928 | Mscrolly ->
5929 let y = min conf.winh (max 0 y) in
5930 scrolly y
5932 | Mscrollx ->
5933 let x = min conf.winw (max 0 x) in
5934 scrollx x
5936 | Mzoomrect (p0, _) ->
5937 state.mstate <- Mzoomrect (p0, (x, y));
5938 G.postRedisplay "motion zoomrect";
5939 end;
5940 state.uioh
5942 method pmotion x y =
5943 begin match state.mode with
5944 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5945 let rec loop = function
5946 | [] ->
5947 if hooverpageno != -1
5948 then (
5949 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5950 G.postRedisplay "pmotion birdseye no hoover";
5952 | l :: rest ->
5953 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5954 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5955 then (
5956 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5957 G.postRedisplay "pmotion birdseye hoover";
5959 else loop rest
5961 loop state.layout
5963 | Textentry _ -> ()
5965 | LinkNav _
5966 | View ->
5967 match state.mstate with
5968 | Mnone -> updateunder x y
5969 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5971 end;
5972 state.uioh
5974 method infochanged _ = ()
5976 method scrollph =
5977 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5978 let p, h = scrollph state.y maxy in
5979 state.scrollw, p, h
5981 method scrollpw =
5982 let winw = conf.winw - state.scrollw - 1 in
5983 let fwinw = float winw in
5984 let sw =
5985 let sw = fwinw /. float state.w in
5986 let sw = fwinw *. sw in
5987 max sw (float conf.scrollh)
5989 let position, sw =
5990 let f = state.w+winw in
5991 let r = float (winw-state.x) /. float f in
5992 let p = fwinw *. r in
5993 p-.sw/.2., sw
5995 let sw =
5996 if position +. sw > fwinw
5997 then fwinw -. position
5998 else sw
6000 state.hscrollh, position, sw
6002 method modehash =
6003 let modename =
6004 match state.mode with
6005 | LinkNav _ -> "links"
6006 | Textentry _ -> "textentry"
6007 | Birdseye _ -> "birdseye"
6008 | View -> "view"
6010 findkeyhash conf modename
6011 end;;
6013 module Config =
6014 struct
6015 open Parser
6017 let fontpath = ref "";;
6019 module KeyMap =
6020 Map.Make (struct type t = (int * int) let compare = compare end);;
6022 let unent s =
6023 let l = String.length s in
6024 let b = Buffer.create l in
6025 unent b s 0 l;
6026 Buffer.contents b;
6029 let home =
6030 try Sys.getenv "HOME"
6031 with exn ->
6032 prerr_endline
6033 ("Can not determine home directory location: " ^
6034 Printexc.to_string exn);
6038 let modifier_of_string = function
6039 | "alt" -> Wsi.altmask
6040 | "shift" -> Wsi.shiftmask
6041 | "ctrl" | "control" -> Wsi.ctrlmask
6042 | "meta" -> Wsi.metamask
6043 | _ -> 0
6046 let key_of_string =
6047 let r = Str.regexp "-" in
6048 fun s ->
6049 let elems = Str.full_split r s in
6050 let f n k m =
6051 let g s =
6052 let m1 = modifier_of_string s in
6053 if m1 = 0
6054 then (Wsi.namekey s, m)
6055 else (k, m lor m1)
6056 in function
6057 | Str.Delim s when n land 1 = 0 -> g s
6058 | Str.Text s -> g s
6059 | Str.Delim _ -> (k, m)
6061 let rec loop n k m = function
6062 | [] -> (k, m)
6063 | x :: xs ->
6064 let k, m = f n k m x in
6065 loop (n+1) k m xs
6067 loop 0 0 0 elems
6070 let keys_of_string =
6071 let r = Str.regexp "[ \t]" in
6072 fun s ->
6073 let elems = Str.split r s in
6074 List.map key_of_string elems
6077 let copykeyhashes c =
6078 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6081 let config_of c attrs =
6082 let apply c k v =
6084 match k with
6085 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6086 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6087 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6088 | "preload" -> { c with preload = bool_of_string v }
6089 | "page-bias" -> { c with pagebias = int_of_string v }
6090 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6091 | "horizontal-scroll-step" ->
6092 { c with hscrollstep = max (int_of_string v) 1 }
6093 | "auto-scroll-step" ->
6094 { c with autoscrollstep = max 0 (int_of_string v) }
6095 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6096 | "crop-hack" -> { c with crophack = bool_of_string v }
6097 | "throttle" ->
6098 let mw =
6099 match String.lowercase v with
6100 | "true" -> Some infinity
6101 | "false" -> None
6102 | f -> Some (float_of_string f)
6104 { c with maxwait = mw}
6105 | "highlight-links" -> { c with hlinks = bool_of_string v }
6106 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6107 | "vertical-margin" ->
6108 { c with interpagespace = max 0 (int_of_string v) }
6109 | "zoom" ->
6110 let zoom = float_of_string v /. 100. in
6111 let zoom = max zoom 0.0 in
6112 { c with zoom = zoom }
6113 | "presentation" -> { c with presentation = bool_of_string v }
6114 | "rotation-angle" -> { c with angle = int_of_string v }
6115 | "width" -> { c with winw = max 20 (int_of_string v) }
6116 | "height" -> { c with winh = max 20 (int_of_string v) }
6117 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6118 | "proportional-display" -> { c with proportional = bool_of_string v }
6119 | "pixmap-cache-size" ->
6120 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6121 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6122 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6123 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6124 | "persistent-location" -> { c with jumpback = bool_of_string v }
6125 | "background-color" -> { c with bgcolor = color_of_string v }
6126 | "scrollbar-in-presentation" ->
6127 { c with scrollbarinpm = bool_of_string v }
6128 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6129 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6130 | "mupdf-store-size" ->
6131 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6132 | "checkers" -> { c with checkers = bool_of_string v }
6133 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6134 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6135 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6136 | "uri-launcher" -> { c with urilauncher = unent v }
6137 | "path-launcher" -> { c with pathlauncher = unent v }
6138 | "color-space" -> { c with colorspace = colorspace_of_string v }
6139 | "invert-colors" -> { c with invert = bool_of_string v }
6140 | "brightness" -> { c with colorscale = float_of_string v }
6141 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6142 | "ghyllscroll" ->
6143 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6144 | "columns" ->
6145 let (n, _, _) as nab = multicolumns_of_string v in
6146 if n < 0
6147 then { c with columns = Csplit (-n, [||]) }
6148 else { c with columns = Cmulti (nab, [||]) }
6149 | "birds-eye-columns" ->
6150 { c with beyecolumns = Some (max (int_of_string v) 2) }
6151 | "selection-command" -> { c with selcmd = unent v }
6152 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6153 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6154 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6155 | "use-pbo" -> { c with usepbo = bool_of_string v }
6156 | _ -> c
6157 with exn ->
6158 prerr_endline ("Error processing attribute (`" ^
6159 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6162 let rec fold c = function
6163 | [] -> c
6164 | (k, v) :: rest ->
6165 let c = apply c k v in
6166 fold c rest
6168 fold { c with keyhashes = copykeyhashes c } attrs;
6171 let fromstring f pos n v d =
6172 try f v
6173 with exn ->
6174 dolog "Error processing attribute (%S=%S) at %d\n%s"
6175 n v pos (Printexc.to_string exn)
6180 let bookmark_of attrs =
6181 let rec fold title page rely visy = function
6182 | ("title", v) :: rest -> fold v page rely visy rest
6183 | ("page", v) :: rest -> fold title v rely visy rest
6184 | ("rely", v) :: rest -> fold title page v visy rest
6185 | ("visy", v) :: rest -> fold title page rely v rest
6186 | _ :: rest -> fold title page rely visy rest
6187 | [] -> title, page, rely, visy
6189 fold "invalid" "0" "0" "0" attrs
6192 let doc_of attrs =
6193 let rec fold path page rely pan visy = function
6194 | ("path", v) :: rest -> fold v page rely pan visy rest
6195 | ("page", v) :: rest -> fold path v rely pan visy rest
6196 | ("rely", v) :: rest -> fold path page v pan visy rest
6197 | ("pan", v) :: rest -> fold path page rely v visy rest
6198 | ("visy", v) :: rest -> fold path page rely pan v rest
6199 | _ :: rest -> fold path page rely pan visy rest
6200 | [] -> path, page, rely, pan, visy
6202 fold "" "0" "0" "0" "0" attrs
6205 let map_of attrs =
6206 let rec fold rs ls = function
6207 | ("out", v) :: rest -> fold v ls rest
6208 | ("in", v) :: rest -> fold rs v rest
6209 | _ :: rest -> fold ls rs rest
6210 | [] -> ls, rs
6212 fold "" "" attrs
6215 let setconf dst src =
6216 dst.scrollbw <- src.scrollbw;
6217 dst.scrollh <- src.scrollh;
6218 dst.icase <- src.icase;
6219 dst.preload <- src.preload;
6220 dst.pagebias <- src.pagebias;
6221 dst.verbose <- src.verbose;
6222 dst.scrollstep <- src.scrollstep;
6223 dst.maxhfit <- src.maxhfit;
6224 dst.crophack <- src.crophack;
6225 dst.autoscrollstep <- src.autoscrollstep;
6226 dst.maxwait <- src.maxwait;
6227 dst.hlinks <- src.hlinks;
6228 dst.underinfo <- src.underinfo;
6229 dst.interpagespace <- src.interpagespace;
6230 dst.zoom <- src.zoom;
6231 dst.presentation <- src.presentation;
6232 dst.angle <- src.angle;
6233 dst.winw <- src.winw;
6234 dst.winh <- src.winh;
6235 dst.savebmarks <- src.savebmarks;
6236 dst.memlimit <- src.memlimit;
6237 dst.proportional <- src.proportional;
6238 dst.texcount <- src.texcount;
6239 dst.sliceheight <- src.sliceheight;
6240 dst.thumbw <- src.thumbw;
6241 dst.jumpback <- src.jumpback;
6242 dst.bgcolor <- src.bgcolor;
6243 dst.scrollbarinpm <- src.scrollbarinpm;
6244 dst.tilew <- src.tilew;
6245 dst.tileh <- src.tileh;
6246 dst.mustoresize <- src.mustoresize;
6247 dst.checkers <- src.checkers;
6248 dst.aalevel <- src.aalevel;
6249 dst.trimmargins <- src.trimmargins;
6250 dst.trimfuzz <- src.trimfuzz;
6251 dst.urilauncher <- src.urilauncher;
6252 dst.colorspace <- src.colorspace;
6253 dst.invert <- src.invert;
6254 dst.colorscale <- src.colorscale;
6255 dst.redirectstderr <- src.redirectstderr;
6256 dst.ghyllscroll <- src.ghyllscroll;
6257 dst.columns <- src.columns;
6258 dst.beyecolumns <- src.beyecolumns;
6259 dst.selcmd <- src.selcmd;
6260 dst.updatecurs <- src.updatecurs;
6261 dst.pathlauncher <- src.pathlauncher;
6262 dst.keyhashes <- copykeyhashes src;
6263 dst.hfsize <- src.hfsize;
6264 dst.hscrollstep <- src.hscrollstep;
6265 dst.pgscale <- src.pgscale;
6266 dst.usepbo <- src.usepbo;
6269 let get s =
6270 let h = Hashtbl.create 10 in
6271 let dc = { defconf with angle = defconf.angle } in
6272 let rec toplevel v t spos _ =
6273 match t with
6274 | Vdata | Vcdata | Vend -> v
6275 | Vopen ("llppconfig", _, closed) ->
6276 if closed
6277 then v
6278 else { v with f = llppconfig }
6279 | Vopen _ ->
6280 error "unexpected subelement at top level" s spos
6281 | Vclose _ -> error "unexpected close at top level" s spos
6283 and llppconfig v t spos _ =
6284 match t with
6285 | Vdata | Vcdata -> v
6286 | Vend -> error "unexpected end of input in llppconfig" s spos
6287 | Vopen ("defaults", attrs, closed) ->
6288 let c = config_of dc attrs in
6289 setconf dc c;
6290 if closed
6291 then v
6292 else { v with f = defaults }
6294 | Vopen ("ui-font", attrs, closed) ->
6295 let rec getsize size = function
6296 | [] -> size
6297 | ("size", v) :: rest ->
6298 let size =
6299 fromstring int_of_string spos "size" v fstate.fontsize in
6300 getsize size rest
6301 | l -> getsize size l
6303 fstate.fontsize <- getsize fstate.fontsize attrs;
6304 if closed
6305 then v
6306 else { v with f = uifont (Buffer.create 10) }
6308 | Vopen ("doc", attrs, closed) ->
6309 let pathent, spage, srely, span, svisy = doc_of attrs in
6310 let path = unent pathent
6311 and pageno = fromstring int_of_string spos "page" spage 0
6312 and rely = fromstring float_of_string spos "rely" srely 0.0
6313 and pan = fromstring int_of_string spos "pan" span 0
6314 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6315 let c = config_of dc attrs in
6316 let anchor = (pageno, rely, visy) in
6317 if closed
6318 then (Hashtbl.add h path (c, [], pan, anchor); v)
6319 else { v with f = doc path pan anchor c [] }
6321 | Vopen _ ->
6322 error "unexpected subelement in llppconfig" s spos
6324 | Vclose "llppconfig" -> { v with f = toplevel }
6325 | Vclose _ -> error "unexpected close in llppconfig" s spos
6327 and defaults v t spos _ =
6328 match t with
6329 | Vdata | Vcdata -> v
6330 | Vend -> error "unexpected end of input in defaults" s spos
6331 | Vopen ("keymap", attrs, closed) ->
6332 let modename =
6333 try List.assoc "mode" attrs
6334 with Not_found -> "global" in
6335 if closed
6336 then v
6337 else
6338 let ret keymap =
6339 let h = findkeyhash dc modename in
6340 KeyMap.iter (Hashtbl.replace h) keymap;
6341 defaults
6343 { v with f = pkeymap ret KeyMap.empty }
6345 | Vopen (_, _, _) ->
6346 error "unexpected subelement in defaults" s spos
6348 | Vclose "defaults" ->
6349 { v with f = llppconfig }
6351 | Vclose _ -> error "unexpected close in defaults" s spos
6353 and uifont b v t spos epos =
6354 match t with
6355 | Vdata | Vcdata ->
6356 Buffer.add_substring b s spos (epos - spos);
6358 | Vopen (_, _, _) ->
6359 error "unexpected subelement in ui-font" s spos
6360 | Vclose "ui-font" ->
6361 if String.length !fontpath = 0
6362 then fontpath := Buffer.contents b;
6363 { v with f = llppconfig }
6364 | Vclose _ -> error "unexpected close in ui-font" s spos
6365 | Vend -> error "unexpected end of input in ui-font" s spos
6367 and doc path pan anchor c bookmarks v t spos _ =
6368 match t with
6369 | Vdata | Vcdata -> v
6370 | Vend -> error "unexpected end of input in doc" s spos
6371 | Vopen ("bookmarks", _, closed) ->
6372 if closed
6373 then v
6374 else { v with f = pbookmarks path pan anchor c bookmarks }
6376 | Vopen ("keymap", attrs, closed) ->
6377 let modename =
6378 try List.assoc "mode" attrs
6379 with Not_found -> "global"
6381 if closed
6382 then v
6383 else
6384 let ret keymap =
6385 let h = findkeyhash c modename in
6386 KeyMap.iter (Hashtbl.replace h) keymap;
6387 doc path pan anchor c bookmarks
6389 { v with f = pkeymap ret KeyMap.empty }
6391 | Vopen (_, _, _) ->
6392 error "unexpected subelement in doc" s spos
6394 | Vclose "doc" ->
6395 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6396 { v with f = llppconfig }
6398 | Vclose _ -> error "unexpected close in doc" s spos
6400 and pkeymap ret keymap v t spos _ =
6401 match t with
6402 | Vdata | Vcdata -> v
6403 | Vend -> error "unexpected end of input in keymap" s spos
6404 | Vopen ("map", attrs, closed) ->
6405 let r, l = map_of attrs in
6406 let kss = fromstring keys_of_string spos "in" r [] in
6407 let lss = fromstring keys_of_string spos "out" l [] in
6408 let keymap =
6409 match kss with
6410 | [] -> keymap
6411 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6412 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6414 if closed
6415 then { v with f = pkeymap ret keymap }
6416 else
6417 let f () = v in
6418 { v with f = skip "map" f }
6420 | Vopen _ ->
6421 error "unexpected subelement in keymap" s spos
6423 | Vclose "keymap" ->
6424 { v with f = ret keymap }
6426 | Vclose _ -> error "unexpected close in keymap" s spos
6428 and pbookmarks path pan anchor c bookmarks v t spos _ =
6429 match t with
6430 | Vdata | Vcdata -> v
6431 | Vend -> error "unexpected end of input in bookmarks" s spos
6432 | Vopen ("item", attrs, closed) ->
6433 let titleent, spage, srely, svisy = bookmark_of attrs in
6434 let page = fromstring int_of_string spos "page" spage 0
6435 and rely = fromstring float_of_string spos "rely" srely 0.0
6436 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6437 let bookmarks =
6438 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6440 if closed
6441 then { v with f = pbookmarks path pan anchor c bookmarks }
6442 else
6443 let f () = v in
6444 { v with f = skip "item" f }
6446 | Vopen _ ->
6447 error "unexpected subelement in bookmarks" s spos
6449 | Vclose "bookmarks" ->
6450 { v with f = doc path pan anchor c bookmarks }
6452 | Vclose _ -> error "unexpected close in bookmarks" s spos
6454 and skip tag f v t spos _ =
6455 match t with
6456 | Vdata | Vcdata -> v
6457 | Vend ->
6458 error ("unexpected end of input in skipped " ^ tag) s spos
6459 | Vopen (tag', _, closed) ->
6460 if closed
6461 then v
6462 else
6463 let f' () = { v with f = skip tag f } in
6464 { v with f = skip tag' f' }
6465 | Vclose ctag ->
6466 if tag = ctag
6467 then f ()
6468 else error ("unexpected close in skipped " ^ tag) s spos
6471 parse { f = toplevel; accu = () } s;
6472 h, dc;
6475 let do_load f ic =
6477 let len = in_channel_length ic in
6478 let s = String.create len in
6479 really_input ic s 0 len;
6480 f s;
6481 with
6482 | Parse_error (msg, s, pos) ->
6483 let subs = subs s pos in
6484 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6485 failwith ("parse error: " ^ s)
6487 | exn ->
6488 failwith ("config load error: " ^ Printexc.to_string exn)
6491 let defconfpath =
6492 let dir =
6494 let dir = Filename.concat home ".config" in
6495 if Sys.is_directory dir then dir else home
6496 with _ -> home
6498 Filename.concat dir "llpp.conf"
6501 let confpath = ref defconfpath;;
6503 let load1 f =
6504 if Sys.file_exists !confpath
6505 then
6506 match
6507 (try Some (open_in_bin !confpath)
6508 with exn ->
6509 prerr_endline
6510 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6511 Printexc.to_string exn);
6512 None
6514 with
6515 | Some ic ->
6516 let success =
6518 f (do_load get ic)
6519 with exn ->
6520 prerr_endline
6521 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6522 Printexc.to_string exn);
6523 false
6525 close_in ic;
6526 success
6528 | None -> false
6529 else
6530 f (Hashtbl.create 0, defconf)
6533 let load () =
6534 let f (h, dc) =
6535 let pc, pb, px, pa =
6537 Hashtbl.find h (Filename.basename state.path)
6538 with Not_found -> dc, [], 0, emptyanchor
6540 setconf defconf dc;
6541 setconf conf pc;
6542 state.bookmarks <- pb;
6543 state.x <- px;
6544 state.scrollw <- conf.scrollbw;
6545 if conf.jumpback
6546 then state.anchor <- pa;
6547 cbput state.hists.nav pa;
6548 true
6550 load1 f
6553 let add_attrs bb always dc c =
6554 let ob s a b =
6555 if always || a != b
6556 then Printf.bprintf bb "\n %s='%b'" s a
6557 and oi s a b =
6558 if always || a != b
6559 then Printf.bprintf bb "\n %s='%d'" s a
6560 and oI s a b =
6561 if always || a != b
6562 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6563 and oz s a b =
6564 if always || a <> b
6565 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6566 and oF s a b =
6567 if always || a <> b
6568 then Printf.bprintf bb "\n %s='%f'" s a
6569 and oc s a b =
6570 if always || a <> b
6571 then
6572 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6573 and oC s a b =
6574 if always || a <> b
6575 then
6576 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6577 and oR s a b =
6578 if always || a <> b
6579 then
6580 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6581 and os s a b =
6582 if always || a <> b
6583 then
6584 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6585 and og s a b =
6586 if always || a <> b
6587 then
6588 match a with
6589 | None -> ()
6590 | Some (_N, _A, _B) ->
6591 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6592 and oW s a b =
6593 if always || a <> b
6594 then
6595 let v =
6596 match a with
6597 | None -> "false"
6598 | Some f ->
6599 if f = infinity
6600 then "true"
6601 else string_of_float f
6603 Printf.bprintf bb "\n %s='%s'" s v
6604 and oco s a b =
6605 if always || a <> b
6606 then
6607 match a with
6608 | Cmulti ((n, a, b), _) when n > 1 ->
6609 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6610 | Csplit (n, _) when n > 1 ->
6611 Printf.bprintf bb "\n %s='%d'" s ~-n
6612 | _ -> ()
6613 and obeco s a b =
6614 if always || a <> b
6615 then
6616 match a with
6617 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6618 | _ -> ()
6620 let w, h =
6621 if always
6622 then dc.winw, dc.winh
6623 else
6624 match state.fullscreen with
6625 | Some wh -> wh
6626 | None -> c.winw, c.winh
6628 oi "width" w dc.winw;
6629 oi "height" h dc.winh;
6630 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6631 oi "scroll-handle-height" c.scrollh dc.scrollh;
6632 ob "case-insensitive-search" c.icase dc.icase;
6633 ob "preload" c.preload dc.preload;
6634 oi "page-bias" c.pagebias dc.pagebias;
6635 oi "scroll-step" c.scrollstep dc.scrollstep;
6636 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6637 ob "max-height-fit" c.maxhfit dc.maxhfit;
6638 ob "crop-hack" c.crophack dc.crophack;
6639 oW "throttle" c.maxwait dc.maxwait;
6640 ob "highlight-links" c.hlinks dc.hlinks;
6641 ob "under-cursor-info" c.underinfo dc.underinfo;
6642 oi "vertical-margin" c.interpagespace dc.interpagespace;
6643 oz "zoom" c.zoom dc.zoom;
6644 ob "presentation" c.presentation dc.presentation;
6645 oi "rotation-angle" c.angle dc.angle;
6646 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6647 ob "proportional-display" c.proportional dc.proportional;
6648 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6649 oi "tex-count" c.texcount dc.texcount;
6650 oi "slice-height" c.sliceheight dc.sliceheight;
6651 oi "thumbnail-width" c.thumbw dc.thumbw;
6652 ob "persistent-location" c.jumpback dc.jumpback;
6653 oc "background-color" c.bgcolor dc.bgcolor;
6654 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6655 oi "tile-width" c.tilew dc.tilew;
6656 oi "tile-height" c.tileh dc.tileh;
6657 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6658 ob "checkers" c.checkers dc.checkers;
6659 oi "aalevel" c.aalevel dc.aalevel;
6660 ob "trim-margins" c.trimmargins dc.trimmargins;
6661 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6662 os "uri-launcher" c.urilauncher dc.urilauncher;
6663 os "path-launcher" c.pathlauncher dc.pathlauncher;
6664 oC "color-space" c.colorspace dc.colorspace;
6665 ob "invert-colors" c.invert dc.invert;
6666 oF "brightness" c.colorscale dc.colorscale;
6667 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6668 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6669 oco "columns" c.columns dc.columns;
6670 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6671 os "selection-command" c.selcmd dc.selcmd;
6672 ob "update-cursor" c.updatecurs dc.updatecurs;
6673 oi "hint-font-size" c.hfsize dc.hfsize;
6674 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6675 oF "page-scroll-scale" c.pgscale dc.pgscale;
6676 ob "use-pbo" c.usepbo dc.usepbo;
6679 let keymapsbuf always dc c =
6680 let bb = Buffer.create 16 in
6681 let rec loop = function
6682 | [] -> ()
6683 | (modename, h) :: rest ->
6684 let dh = findkeyhash dc modename in
6685 if always || h <> dh
6686 then (
6687 if Hashtbl.length h > 0
6688 then (
6689 if Buffer.length bb > 0
6690 then Buffer.add_char bb '\n';
6691 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6692 Hashtbl.iter (fun i o ->
6693 let isdifferent = always ||
6695 let dO = Hashtbl.find dh i in
6696 dO <> o
6697 with Not_found -> true
6699 if isdifferent
6700 then
6701 let addkm (k, m) =
6702 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6703 if Wsi.withalt m then Buffer.add_string bb "alt-";
6704 if Wsi.withshift m then Buffer.add_string bb "shift-";
6705 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6706 Buffer.add_string bb (Wsi.keyname k);
6708 let addkms l =
6709 let rec loop = function
6710 | [] -> ()
6711 | km :: [] -> addkm km
6712 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6714 loop l
6716 Buffer.add_string bb "<map in='";
6717 addkm i;
6718 match o with
6719 | KMinsrt km ->
6720 Buffer.add_string bb "' out='";
6721 addkm km;
6722 Buffer.add_string bb "'/>\n"
6724 | KMinsrl kms ->
6725 Buffer.add_string bb "' out='";
6726 addkms kms;
6727 Buffer.add_string bb "'/>\n"
6729 | KMmulti (ins, kms) ->
6730 Buffer.add_char bb ' ';
6731 addkms ins;
6732 Buffer.add_string bb "' out='";
6733 addkms kms;
6734 Buffer.add_string bb "'/>\n"
6735 ) h;
6736 Buffer.add_string bb "</keymap>";
6739 loop rest
6741 loop c.keyhashes;
6745 let save () =
6746 let uifontsize = fstate.fontsize in
6747 let bb = Buffer.create 32768 in
6748 let f (h, dc) =
6749 let dc = if conf.bedefault then conf else dc in
6750 Buffer.add_string bb "<llppconfig>\n";
6752 if String.length !fontpath > 0
6753 then
6754 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6755 uifontsize
6756 !fontpath
6757 else (
6758 if uifontsize <> 14
6759 then
6760 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6763 Buffer.add_string bb "<defaults ";
6764 add_attrs bb true dc dc;
6765 let kb = keymapsbuf true dc dc in
6766 if Buffer.length kb > 0
6767 then (
6768 Buffer.add_string bb ">\n";
6769 Buffer.add_buffer bb kb;
6770 Buffer.add_string bb "\n</defaults>\n";
6772 else Buffer.add_string bb "/>\n";
6774 let adddoc path pan anchor c bookmarks =
6775 if bookmarks == [] && c = dc && anchor = emptyanchor
6776 then ()
6777 else (
6778 Printf.bprintf bb "<doc path='%s'"
6779 (enent path 0 (String.length path));
6781 if anchor <> emptyanchor
6782 then (
6783 let n, rely, visy = anchor in
6784 Printf.bprintf bb " page='%d'" n;
6785 if rely > 1e-6
6786 then
6787 Printf.bprintf bb " rely='%f'" rely
6789 if abs_float visy > 1e-6
6790 then
6791 Printf.bprintf bb " visy='%f'" visy
6795 if pan != 0
6796 then Printf.bprintf bb " pan='%d'" pan;
6798 add_attrs bb false dc c;
6799 let kb = keymapsbuf false dc c in
6801 begin match bookmarks with
6802 | [] ->
6803 if Buffer.length kb > 0
6804 then (
6805 Buffer.add_string bb ">\n";
6806 Buffer.add_buffer bb kb;
6807 Buffer.add_string bb "\n</doc>\n";
6809 else Buffer.add_string bb "/>\n"
6810 | _ ->
6811 Buffer.add_string bb ">\n<bookmarks>\n";
6812 List.iter (fun (title, _level, (page, rely, visy)) ->
6813 Printf.bprintf bb
6814 "<item title='%s' page='%d'"
6815 (enent title 0 (String.length title))
6816 page
6818 if rely > 1e-6
6819 then
6820 Printf.bprintf bb " rely='%f'" rely
6822 if abs_float visy > 1e-6
6823 then
6824 Printf.bprintf bb " visy='%f'" visy
6826 Buffer.add_string bb "/>\n";
6827 ) bookmarks;
6828 Buffer.add_string bb "</bookmarks>";
6829 if Buffer.length kb > 0
6830 then (
6831 Buffer.add_string bb "\n";
6832 Buffer.add_buffer bb kb;
6834 Buffer.add_string bb "\n</doc>\n";
6835 end;
6839 let pan, conf =
6840 match state.mode with
6841 | Birdseye (c, pan, _, _, _) ->
6842 let beyecolumns =
6843 match conf.columns with
6844 | Cmulti ((c, _, _), _) -> Some c
6845 | Csingle _ -> None
6846 | Csplit _ -> None
6847 and columns =
6848 match c.columns with
6849 | Cmulti (c, _) -> Cmulti (c, [||])
6850 | Csingle _ -> Csingle [||]
6851 | Csplit _ -> failwith "quit from bird's eye while split"
6853 pan, { c with beyecolumns = beyecolumns; columns = columns }
6854 | _ -> state.x, conf
6856 let basename = Filename.basename state.path in
6857 adddoc basename pan (getanchor ())
6858 (let conf =
6859 let autoscrollstep =
6860 match state.autoscroll with
6861 | Some step -> step
6862 | None -> conf.autoscrollstep
6864 match state.mode with
6865 | Birdseye (bc, _, _, _, _) ->
6866 { conf with
6867 zoom = bc.zoom;
6868 presentation = bc.presentation;
6869 interpagespace = bc.interpagespace;
6870 maxwait = bc.maxwait;
6871 autoscrollstep = autoscrollstep }
6872 | _ -> { conf with autoscrollstep = autoscrollstep }
6873 in conf)
6874 (if conf.savebmarks then state.bookmarks else []);
6876 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6877 if basename <> path
6878 then adddoc path x anchor c bookmarks
6879 ) h;
6880 Buffer.add_string bb "</llppconfig>\n";
6881 true;
6883 if load1 f && Buffer.length bb > 0
6884 then
6886 let tmp = !confpath ^ ".tmp" in
6887 let oc = open_out_bin tmp in
6888 Buffer.output_buffer oc bb;
6889 close_out oc;
6890 Unix.rename tmp !confpath;
6891 with exn ->
6892 prerr_endline
6893 ("error while saving configuration: " ^ Printexc.to_string exn)
6895 end;;
6897 let () =
6898 let trimcachepath = ref "" in
6899 Arg.parse
6900 (Arg.align
6901 [("-p", Arg.String (fun s -> state.password <- s) ,
6902 "<password> Set password");
6904 ("-f", Arg.String (fun s -> Config.fontpath := s),
6905 "<path> Set path to the user interface font");
6907 ("-c", Arg.String (fun s -> Config.confpath := s),
6908 "<path> Set path to the configuration file");
6910 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6911 "<path> Set path to the trim cache file");
6913 ("-v", Arg.Unit (fun () ->
6914 Printf.printf
6915 "%s\nconfiguration path: %s\n"
6916 (version ())
6917 Config.defconfpath
6919 exit 0), " Print version and exit");
6922 (fun s -> state.path <- s)
6923 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6925 if String.length state.path = 0
6926 then (prerr_endline "file name missing"; exit 1);
6928 if not (Config.load ())
6929 then prerr_endline "failed to load configuration";
6931 let globalkeyhash = findkeyhash conf "global" in
6932 let wsfd, winw, winh = Wsi.init (object
6933 method expose =
6934 if nogeomcmds state.geomcmds || platform == Posx
6935 then display ()
6936 else (
6937 GlClear.color (scalecolor2 conf.bgcolor);
6938 GlClear.clear [`color];
6940 method display = display ()
6941 method reshape w h = reshape w h
6942 method mouse b d x y m = mouse b d x y m
6943 method motion x y = state.mpos <- (x, y); motion x y
6944 method pmotion x y = state.mpos <- (x, y); pmotion x y
6945 method key k m =
6946 let mascm = m land (
6947 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6948 ) in
6949 match state.keystate with
6950 | KSnone ->
6951 let km = k, mascm in
6952 begin
6953 match
6954 let modehash = state.uioh#modehash in
6955 try Hashtbl.find modehash km
6956 with Not_found ->
6957 try Hashtbl.find globalkeyhash km
6958 with Not_found -> KMinsrt (k, m)
6959 with
6960 | KMinsrt (k, m) -> keyboard k m
6961 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6962 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6964 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6965 List.iter (fun (k, m) -> keyboard k m) insrt;
6966 state.keystate <- KSnone
6967 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6968 state.keystate <- KSinto (keys, insrt)
6969 | _ ->
6970 state.keystate <- KSnone
6972 method enter x y = state.mpos <- (x, y); pmotion x y
6973 method leave = state.mpos <- (-1, -1)
6974 method quit = raise Quit
6975 end) conf.winw conf.winh (platform = Posx) in
6977 state.wsfd <- wsfd;
6979 if not (
6980 List.exists GlMisc.check_extension
6981 [ "GL_ARB_texture_rectangle"
6982 ; "GL_EXT_texture_recangle"
6983 ; "GL_NV_texture_rectangle" ]
6985 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6987 let cr, sw =
6988 match Ne.pipe () with
6989 | Ne.Exn exn ->
6990 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6991 exit 1
6992 | Ne.Res rw -> rw
6993 and sr, cw =
6994 match Ne.pipe () with
6995 | Ne.Exn exn ->
6996 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6997 exit 1
6998 | Ne.Res rw -> rw
7001 cloexec cr;
7002 cloexec sw;
7003 cloexec sr;
7004 cloexec cw;
7006 setcheckers conf.checkers;
7007 redirectstderr ();
7009 init (cr, cw) (
7010 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7011 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7012 !Config.fontpath, !trimcachepath,
7013 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7015 state.sr <- sr;
7016 state.sw <- sw;
7017 state.text <- "Opening " ^ (mbtoutf8 state.path);
7018 reshape winw winh;
7019 opendoc state.path state.password;
7020 state.uioh <- uioh;
7022 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7024 let rec loop deadline =
7025 let r =
7026 match state.errfd with
7027 | None -> [state.sr; state.wsfd]
7028 | Some fd -> [state.sr; state.wsfd; fd]
7030 if state.redisplay
7031 then (
7032 state.redisplay <- false;
7033 display ();
7035 let timeout =
7036 let now = now () in
7037 if deadline > now
7038 then (
7039 if deadline = infinity
7040 then ~-.1.0
7041 else max 0.0 (deadline -. now)
7043 else 0.0
7045 let r, _, _ =
7046 try Unix.select r [] [] timeout
7047 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7049 begin match r with
7050 | [] ->
7051 state.ghyll None;
7052 let newdeadline =
7053 if state.ghyll == noghyll
7054 then
7055 match state.autoscroll with
7056 | Some step when step != 0 ->
7057 let y = state.y + step in
7058 let y =
7059 if y < 0
7060 then state.maxy
7061 else if y >= state.maxy then 0 else y
7063 gotoy y;
7064 if state.mode = View
7065 then state.text <- "";
7066 deadline +. 0.01
7067 | _ -> infinity
7068 else deadline +. 0.01
7070 loop newdeadline
7072 | l ->
7073 let rec checkfds = function
7074 | [] -> ()
7075 | fd :: rest when fd = state.sr ->
7076 let cmd = readcmd state.sr in
7077 act cmd;
7078 checkfds rest
7080 | fd :: rest when fd = state.wsfd ->
7081 Wsi.readresp fd;
7082 checkfds rest
7084 | fd :: rest ->
7085 let s = String.create 80 in
7086 let n = Unix.read fd s 0 80 in
7087 if conf.redirectstderr
7088 then (
7089 Buffer.add_substring state.errmsgs s 0 n;
7090 state.newerrmsgs <- true;
7091 state.redisplay <- true;
7093 else (
7094 prerr_string (String.sub s 0 n);
7095 flush stderr;
7097 checkfds rest
7099 checkfds l;
7100 let newdeadline =
7101 let deadline1 =
7102 if deadline = infinity
7103 then now () +. 0.01
7104 else deadline
7106 match state.autoscroll with
7107 | Some step when step != 0 -> deadline1
7108 | _ -> if state.ghyll == noghyll then infinity else deadline1
7110 loop newdeadline
7111 end;
7114 loop infinity;
7115 with Quit ->
7116 Config.save ();