Cap allocation size properly
[llpp.git] / main.ml
blob16a43b5bac2b97a2adebf6397a391045352df6da
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 | Uremotedest of (string * string)
15 and facename = string;;
17 type mark =
18 | Mark_page
19 | Mark_block
20 | Mark_line
21 | Mark_word
24 type params = (angle * fitmodel * trimparams
25 * texcount * sliceheight * memsize
26 * colorspace * fontpath * trimcachepath
27 * haspbo)
28 and pageno = int
29 and width = int
30 and height = int
31 and leftx = int
32 and opaque = string
33 and recttype = int
34 and pixmapsize = int
35 and angle = int
36 and trimmargins = bool
37 and interpagespace = int
38 and texcount = int
39 and sliceheight = int
40 and gen = int
41 and top = float
42 and dtop = float
43 and fontpath = string
44 and trimcachepath = string
45 and memsize = int
46 and aalevel = int
47 and irect = (int * int * int * int)
48 and trimparams = (trimmargins * irect)
49 and colorspace = | Rgb | Bgr | Gray
50 and fitmodel = | FitWidth | FitProportional | FitPage
51 and haspbo = bool
54 type x = int
55 and y = int
56 and tilex = int
57 and tiley = int
58 and tileparams = (x * y * width * height * tilex * tiley)
61 type link =
62 | Lnotfound
63 | Lfound of int
64 and linkdir =
65 | LDfirst
66 | LDlast
67 | LDfirstvisible of (int * int * int)
68 | LDleft of int
69 | LDright of int
70 | LDdown of int
71 | LDup of int
74 type pagewithlinks =
75 | Pwlnotfound
76 | Pwl of int
79 type keymap =
80 | KMinsrt of key
81 | KMinsrl of key list
82 | KMmulti of key list * key list
83 and key = int * int
84 and keyhash = (key, keymap) Hashtbl.t
85 and keystate =
86 | KSnone
87 | KSinto of (key list * key list)
90 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
91 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
93 type pipe = (Unix.file_descr * Unix.file_descr);;
95 external init : pipe -> params -> unit = "ml_init";;
96 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
97 external copysel : Unix.file_descr -> opaque -> bool -> unit = "ml_copysel";;
98 external getpdimrect : int -> float array = "ml_getpdimrect";;
99 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
100 external markunder : string -> int -> int -> mark -> bool = "ml_markunder";;
101 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
102 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
103 external measurestr : int -> string -> float = "ml_measure_string";;
104 external postprocess :
105 opaque -> int -> int -> int -> (int * string * int) -> int
106 = "ml_postprocess";;
107 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
108 external platform : unit -> platform = "ml_platform";;
109 external setaalevel : int -> unit = "ml_setaalevel";;
110 external realloctexts : int -> bool = "ml_realloctexts";;
111 external findlink : opaque -> linkdir -> link = "ml_findlink";;
112 external getlink : opaque -> int -> under = "ml_getlink";;
113 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
114 external getlinkcount : opaque -> int = "ml_getlinkcount";;
115 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
116 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
117 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
118 external freepbo : string -> unit = "ml_freepbo";;
119 external unmappbo : string -> unit = "ml_unmappbo";;
120 external pbousable : unit -> bool = "ml_pbo_usable";;
121 external unproject : opaque -> int -> int -> (int * int) option
122 = "ml_unproject";;
123 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
124 external rectofblock : opaque -> int -> int -> float array option
125 = "ml_rectofblock";;
127 let platform_to_string = function
128 | Punknown -> "unknown"
129 | Plinux -> "Linux"
130 | Posx -> "OSX"
131 | Psun -> "Sun"
132 | Pfreebsd -> "FreeBSD"
133 | Pdragonflybsd -> "DragonflyBSD"
134 | Popenbsd -> "OpenBSD"
135 | Pnetbsd -> "NetBSD"
136 | Pcygwin -> "Cygwin"
139 let platform = platform ();;
141 let now = Unix.gettimeofday;;
143 let selfexec = ref "";;
145 let popen cmd fda =
146 if platform = Pcygwin
147 then (
148 let sh = "/bin/sh" in
149 let args = [|sh; "-c"; cmd|] in
150 let rec std si so se = function
151 | [] -> si, so, se
152 | (fd, 0) :: rest -> std fd so se rest
153 | (fd, -1) :: rest ->
154 Unix.set_close_on_exec fd;
155 std si so se rest
156 | (_, n) :: _ ->
157 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
159 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
160 ignore (Unix.create_process sh args si so se)
162 else popen cmd fda;
165 type mpos = int * int
166 and mstate =
167 | Msel of (mpos * mpos)
168 | Mpan of mpos
169 | Mscrolly | Mscrollx
170 | Mzoom of (int * int)
171 | Mzoomrect of (mpos * mpos)
172 | Mnone
175 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
176 and onkey = string -> int -> te
177 and ondone = string -> unit
178 and histcancel = unit -> unit
179 and onhist = ((histcmd -> string) * histcancel)
180 and histcmd = HCnext | HCprev | HCfirst | HClast
181 and cancelonempty = bool
182 and te =
183 | TEstop
184 | TEdone of string
185 | TEcont of string
186 | TEswitch of textentry
189 type 'a circbuf =
190 { store : 'a array
191 ; mutable rc : int
192 ; mutable wc : int
193 ; mutable len : int
197 let bound v minv maxv =
198 max minv (min maxv v);
201 let cbnew n v =
202 { store = Array.create n v
203 ; rc = 0
204 ; wc = 0
205 ; len = 0
209 let cbcap b = Array.length b.store;;
211 let cbput b v =
212 let cap = cbcap b in
213 b.store.(b.wc) <- v;
214 b.wc <- (b.wc + 1) mod cap;
215 b.rc <- b.wc;
216 b.len <- min (b.len + 1) cap;
219 let cbempty b = b.len = 0;;
221 let cbgetg b circular dir =
222 if cbempty b
223 then b.store.(0)
224 else
225 let rc = b.rc + dir in
226 let rc =
227 if circular
228 then (
229 if rc = -1
230 then b.len-1
231 else (
232 if rc >= b.len
233 then 0
234 else rc
237 else bound rc 0 (b.len-1)
239 b.rc <- rc;
240 b.store.(rc);
243 let cbget b = cbgetg b false;;
244 let cbgetc b = cbgetg b true;;
246 let drawstring size x y s =
247 Gl.enable `blend;
248 Gl.enable `texture_2d;
249 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
250 ignore (drawstr size x y s);
251 Gl.disable `blend;
252 Gl.disable `texture_2d;
255 let drawstring1 size x y s =
256 drawstr size x y s;
259 let drawstring2 size x y fmt =
260 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
263 type page =
264 { pageno : int
265 ; pagedimno : int
266 ; pagew : int
267 ; pageh : int
268 ; pagex : int
269 ; pagey : int
270 ; pagevw : int
271 ; pagevh : int
272 ; pagedispx : int
273 ; pagedispy : int
274 ; pagecol : int
278 let debugl l =
279 dolog "l %d dim=%d {" l.pageno l.pagedimno;
280 dolog " WxH %dx%d" l.pagew l.pageh;
281 dolog " vWxH %dx%d" l.pagevw l.pagevh;
282 dolog " pagex,y %d,%d" l.pagex l.pagey;
283 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
284 dolog " column %d" l.pagecol;
285 dolog "}";
288 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
289 dolog "rect {";
290 dolog " x0,y0=(% f, % f)" x0 y0;
291 dolog " x1,y1=(% f, % f)" x1 y1;
292 dolog " x2,y2=(% f, % f)" x2 y2;
293 dolog " x3,y3=(% f, % f)" x3 y3;
294 dolog "}";
297 type multicolumns = multicol * pagegeom
298 and singlecolumn = pagegeom
299 and splitcolumns = columncount * pagegeom
300 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
301 and multicol = columncount * covercount * covercount
302 and pdimno = int
303 and columncount = int
304 and covercount = int;;
306 type scrollb = int;;
307 let scrollbvv = 1;;
308 let scrollbhv = 2;;
310 type conf =
311 { mutable scrollbw : int
312 ; mutable scrollh : int
313 ; mutable scrollb : scrollb
314 ; mutable icase : bool
315 ; mutable preload : bool
316 ; mutable pagebias : int
317 ; mutable verbose : bool
318 ; mutable debug : bool
319 ; mutable scrollstep : int
320 ; mutable hscrollstep : int
321 ; mutable maxhfit : bool
322 ; mutable crophack : bool
323 ; mutable autoscrollstep : int
324 ; mutable maxwait : float option
325 ; mutable hlinks : bool
326 ; mutable underinfo : bool
327 ; mutable interpagespace : interpagespace
328 ; mutable zoom : float
329 ; mutable presentation : bool
330 ; mutable angle : angle
331 ; mutable cwinw : int
332 ; mutable cwinh : int
333 ; mutable savebmarks : bool
334 ; mutable fitmodel : fitmodel
335 ; mutable trimmargins : trimmargins
336 ; mutable trimfuzz : irect
337 ; mutable memlimit : memsize
338 ; mutable texcount : texcount
339 ; mutable sliceheight : sliceheight
340 ; mutable thumbw : width
341 ; mutable jumpback : bool
342 ; mutable bgcolor : (float * float * float)
343 ; mutable bedefault : bool
344 ; mutable tilew : int
345 ; mutable tileh : int
346 ; mutable mustoresize : memsize
347 ; mutable checkers : bool
348 ; mutable aalevel : int
349 ; mutable urilauncher : string
350 ; mutable pathlauncher : string
351 ; mutable colorspace : colorspace
352 ; mutable invert : bool
353 ; mutable colorscale : float
354 ; mutable redirectstderr : bool
355 ; mutable ghyllscroll : (int * int * int) option
356 ; mutable columns : columns
357 ; mutable beyecolumns : columncount option
358 ; mutable selcmd : string
359 ; mutable paxcmd : string
360 ; mutable updatecurs : bool
361 ; mutable keyhashes : (string * keyhash) list
362 ; mutable hfsize : int
363 ; mutable pgscale : float
364 ; mutable usepbo : bool
365 ; mutable wheelbypage : bool
366 ; mutable stcmd : string
367 ; mutable riani : bool
368 ; mutable pax : (float * int * int) ref option
369 ; mutable paxmark : mark
371 and columns =
372 | Csingle of singlecolumn
373 | Cmulti of multicolumns
374 | Csplit of splitcolumns
377 type anchor = pageno * top * dtop;;
379 type outline = string * int * anchor;;
381 type rect = float * float * float * float * float * float * float * float;;
383 type tile = opaque * pixmapsize * elapsed
384 and elapsed = float;;
385 type pagemapkey = pageno * gen;;
386 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
387 and row = int
388 and col = int;;
390 let emptyanchor = (0, 0.0, 0.0);;
392 type infochange = | Memused | Docinfo | Pdim;;
394 class type uioh = object
395 method display : unit
396 method key : int -> int -> uioh
397 method button : int -> bool -> int -> int -> int -> uioh
398 method motion : int -> int -> uioh
399 method pmotion : int -> int -> uioh
400 method infochanged : infochange -> unit
401 method scrollpw : (int * float * float)
402 method scrollph : (int * float * float)
403 method modehash : keyhash
404 method eformsgs : bool
405 end;;
407 type mode =
408 | Birdseye of (conf * leftx * pageno * pageno * anchor)
409 | Textentry of (textentry * onleave)
410 | View
411 | LinkNav of linktarget
412 and onleave = leavetextentrystatus -> unit
413 and leavetextentrystatus = | Cancel | Confirm
414 and helpitem = string * int * action
415 and action =
416 | Noaction
417 | Action of (uioh -> uioh)
418 and linktarget =
419 | Ltexact of (pageno * int)
420 | Ltgendir of int
423 let isbirdseye = function Birdseye _ -> true | _ -> false;;
424 let istextentry = function Textentry _ -> true | _ -> false;;
426 type currently =
427 | Idle
428 | Loading of (page * gen)
429 | Tiling of (
430 page * opaque * colorspace * angle * gen * col * row * width * height
432 | Outlining of outline list
435 let emptykeyhash = Hashtbl.create 0;;
436 let nouioh : uioh = object (self)
437 method display = ()
438 method key _ _ = self
439 method button _ _ _ _ _ = self
440 method motion _ _ = self
441 method pmotion _ _ = self
442 method infochanged _ = ()
443 method scrollpw = (0, nan, nan)
444 method scrollph = (0, nan, nan)
445 method modehash = emptykeyhash
446 method eformsgs = false
447 end;;
449 type state =
450 { mutable sr : Unix.file_descr
451 ; mutable sw : Unix.file_descr
452 ; mutable wsfd : Unix.file_descr
453 ; mutable errfd : Unix.file_descr option
454 ; mutable stderr : Unix.file_descr
455 ; mutable errmsgs : Buffer.t
456 ; mutable newerrmsgs : bool
457 ; mutable w : int
458 ; mutable x : int
459 ; mutable y : int
460 ; mutable anchor : anchor
461 ; mutable ranchors : (string * string * anchor * string) list
462 ; mutable maxy : int
463 ; mutable layout : page list
464 ; pagemap : (pagemapkey, opaque) Hashtbl.t
465 ; tilemap : (tilemapkey, tile) Hashtbl.t
466 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
467 ; mutable pdims : (pageno * width * height * leftx) list
468 ; mutable pagecount : int
469 ; mutable currently : currently
470 ; mutable mstate : mstate
471 ; mutable searchpattern : string
472 ; mutable rects : (pageno * recttype * rect) list
473 ; mutable rects1 : (pageno * recttype * rect) list
474 ; mutable text : string
475 ; mutable winstate : Wsi.winstate list
476 ; mutable mode : mode
477 ; mutable uioh : uioh
478 ; mutable outlines : outline array
479 ; mutable bookmarks : outline list
480 ; mutable path : string
481 ; mutable password : string
482 ; mutable nameddest : string
483 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
484 ; mutable memused : memsize
485 ; mutable gen : gen
486 ; mutable throttle : (page list * int * float) option
487 ; mutable autoscroll : int option
488 ; mutable ghyll : (int option -> unit)
489 ; mutable help : helpitem array
490 ; mutable docinfo : (int * string) list
491 ; mutable texid : GlTex.texture_id option
492 ; hists : hists
493 ; mutable prevzoom : (float * int)
494 ; mutable progress : float
495 ; mutable redisplay : bool
496 ; mutable mpos : mpos
497 ; mutable keystate : keystate
498 ; mutable glinks : bool
499 ; mutable prevcolumns : (columns * float) option
500 ; mutable winw : int
501 ; mutable winh : int
502 ; mutable reprf : (unit -> unit)
503 ; mutable origin : string
504 ; mutable roam : (unit -> unit)
505 ; mutable bzoom : bool
507 and hists =
508 { pat : string circbuf
509 ; pag : string circbuf
510 ; nav : anchor circbuf
511 ; sel : string circbuf
515 let defconf =
516 { scrollbw = 7
517 ; scrollh = 12
518 ; scrollb = scrollbhv lor scrollbvv
519 ; icase = true
520 ; preload = true
521 ; pagebias = 0
522 ; verbose = false
523 ; debug = false
524 ; scrollstep = 24
525 ; hscrollstep = 24
526 ; maxhfit = true
527 ; crophack = false
528 ; autoscrollstep = 2
529 ; maxwait = None
530 ; hlinks = false
531 ; underinfo = false
532 ; interpagespace = 2
533 ; zoom = 1.0
534 ; presentation = false
535 ; angle = 0
536 ; cwinw = 900
537 ; cwinh = 900
538 ; savebmarks = true
539 ; fitmodel = FitProportional
540 ; trimmargins = false
541 ; trimfuzz = (0,0,0,0)
542 ; memlimit = 32 lsl 20
543 ; texcount = 256
544 ; sliceheight = 24
545 ; thumbw = 76
546 ; jumpback = true
547 ; bgcolor = (0.5, 0.5, 0.5)
548 ; bedefault = false
549 ; tilew = 2048
550 ; tileh = 2048
551 ; mustoresize = 256 lsl 20
552 ; checkers = true
553 ; aalevel = 8
554 ; urilauncher =
555 (match platform with
556 | Plinux | Pfreebsd | Pdragonflybsd
557 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
558 | Posx -> "open \"%s\""
559 | Pcygwin -> "cygstart \"%s\""
560 | Punknown -> "echo %s")
561 ; pathlauncher = "lp \"%s\""
562 ; selcmd =
563 (match platform with
564 | Plinux | Pfreebsd | Pdragonflybsd
565 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
566 | Posx -> "pbcopy"
567 | Pcygwin -> "wsel"
568 | Punknown -> "cat")
569 ; paxcmd = "cat"
570 ; colorspace = Rgb
571 ; invert = false
572 ; colorscale = 1.0
573 ; redirectstderr = false
574 ; ghyllscroll = None
575 ; columns = Csingle [||]
576 ; beyecolumns = None
577 ; updatecurs = false
578 ; hfsize = 12
579 ; pgscale = 1.0
580 ; usepbo = false
581 ; wheelbypage = false
582 ; stcmd = "echo SyncTex"
583 ; riani = false
584 ; pax = None
585 ; paxmark = Mark_word
586 ; keyhashes =
587 let mk n = (n, Hashtbl.create 1) in
588 [ mk "global"
589 ; mk "info"
590 ; mk "help"
591 ; mk "outline"
592 ; mk "listview"
593 ; mk "birdseye"
594 ; mk "textentry"
595 ; mk "links"
596 ; mk "view"
601 let wtmode = ref false;;
602 let cxack = ref false;;
604 let findkeyhash c name =
605 try List.assoc name c.keyhashes
606 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
609 let conf = { defconf with angle = defconf.angle };;
611 let pgscale h = truncate (float h *. conf.pgscale);;
613 type fontstate =
614 { mutable fontsize : int
615 ; mutable wwidth : float
616 ; mutable maxrows : int
620 let fstate =
621 { fontsize = 14
622 ; wwidth = nan
623 ; maxrows = -1
627 let geturl s =
628 let colonpos = try String.index s ':' with Not_found -> -1 in
629 let len = String.length s in
630 if colonpos >= 0 && colonpos + 3 < len
631 then (
632 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
633 then
634 let schemestartpos =
635 try String.rindex_from s colonpos ' '
636 with Not_found -> -1
638 let scheme =
639 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
641 match scheme with
642 | "http" | "ftp" | "mailto" ->
643 let epos =
644 try String.index_from s colonpos ' '
645 with Not_found -> len
647 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
648 | _ -> ""
649 else ""
651 else ""
654 let gotouri uri =
655 if emptystr conf.urilauncher
656 then print_endline uri
657 else (
658 let url = geturl uri in
659 if emptystr url
660 then Printf.eprintf "obtained empty url from uri %S" uri
661 else
662 let re = Str.regexp "%s" in
663 let command = Str.global_replace re url conf.urilauncher in
664 try popen command []
665 with exn ->
666 Printf.eprintf
667 "failed to execute `%s': %s\n" command (exntos exn);
668 flush stderr;
672 let version () =
673 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
674 (platform_to_string platform) Sys.word_size Sys.ocaml_version
677 let makehelp () =
678 let strings = version () :: "" :: Help.keys in
679 Array.of_list (
680 List.map (fun s ->
681 let url = geturl s in
682 if nonemptystr url
683 then (s, 0, Action (fun u -> gotouri url; u))
684 else (s, 0, Noaction)
685 ) strings);
688 let noghyll _ = ();;
689 let firstgeomcmds = "", [];;
690 let noreprf () = ();;
692 let state =
693 { sr = Unix.stdin
694 ; sw = Unix.stdin
695 ; wsfd = Unix.stdin
696 ; errfd = None
697 ; stderr = Unix.stderr
698 ; errmsgs = Buffer.create 0
699 ; newerrmsgs = false
700 ; x = 0
701 ; y = 0
702 ; w = 0
703 ; anchor = emptyanchor
704 ; ranchors = []
705 ; layout = []
706 ; maxy = max_int
707 ; tilelru = Queue.create ()
708 ; pagemap = Hashtbl.create 10
709 ; tilemap = Hashtbl.create 10
710 ; pdims = []
711 ; pagecount = 0
712 ; currently = Idle
713 ; mstate = Mnone
714 ; rects = []
715 ; rects1 = []
716 ; text = ""
717 ; mode = View
718 ; winstate = []
719 ; searchpattern = ""
720 ; outlines = [||]
721 ; bookmarks = []
722 ; path = ""
723 ; password = ""
724 ; nameddest = ""
725 ; geomcmds = firstgeomcmds
726 ; hists =
727 { nav = cbnew 10 emptyanchor
728 ; pat = cbnew 10 ""
729 ; pag = cbnew 10 ""
730 ; sel = cbnew 10 ""
732 ; memused = 0
733 ; gen = 0
734 ; throttle = None
735 ; autoscroll = None
736 ; ghyll = noghyll
737 ; help = makehelp ()
738 ; docinfo = []
739 ; texid = None
740 ; prevzoom = (1.0, 0)
741 ; progress = -1.0
742 ; uioh = nouioh
743 ; redisplay = true
744 ; mpos = (-1, -1)
745 ; keystate = KSnone
746 ; glinks = false
747 ; prevcolumns = None
748 ; winw = -1
749 ; winh = -1
750 ; reprf = noreprf
751 ; origin = ""
752 ; roam = (fun () -> ())
753 ; bzoom = false
757 let hscrollh () =
758 if (conf.scrollb land scrollbhv = 0)
759 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
760 then 0
761 else conf.scrollbw
764 let vscrollw () =
765 if (conf.scrollb land scrollbvv = 0)
766 then 0
767 else conf.scrollbw
770 let wadjsb w = w - vscrollw ();;
772 let setfontsize n =
773 fstate.fontsize <- n;
774 fstate.wwidth <- measurestr fstate.fontsize "w";
775 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
778 let vlog fmt =
779 if conf.verbose
780 then
781 Printf.kprintf prerr_endline fmt
782 else
783 Printf.kprintf ignore fmt
786 let launchpath () =
787 if emptystr conf.pathlauncher
788 then print_endline state.path
789 else (
790 let re = Str.regexp "%s" in
791 let command = Str.global_replace re state.path conf.pathlauncher in
792 try popen command []
793 with exn ->
794 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
795 flush stderr;
799 module Ne = struct
800 type 'a t = | Res of 'a | Exn of exn;;
802 let pipe () =
803 try Res (Unix.pipe ())
804 with exn -> Exn exn
807 let clo fd f =
808 try tempfailureretry Unix.close fd
809 with exn -> f (exntos exn)
812 let dup fd =
813 try Res (tempfailureretry Unix.dup fd)
814 with exn -> Exn exn
817 let dup2 fd1 fd2 =
818 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
819 with exn -> Exn exn
821 end;;
823 let redirectstderr () =
824 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
825 if conf.redirectstderr
826 then
827 match Ne.pipe () with
828 | Ne.Exn exn ->
829 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
831 | Ne.Res (r, w) ->
832 begin match Ne.dup Unix.stderr with
833 | Ne.Exn exn ->
834 dolog "failed to dup stderr: %s" (exntos exn);
835 Ne.clo r (clofail "pipe/r");
836 Ne.clo w (clofail "pipe/w");
838 | Ne.Res dupstderr ->
839 begin match Ne.dup2 w Unix.stderr with
840 | Ne.Exn exn ->
841 dolog "failed to dup2 to stderr: %s" (exntos exn);
842 Ne.clo dupstderr (clofail "stderr duplicate");
843 Ne.clo r (clofail "redir pipe/r");
844 Ne.clo w (clofail "redir pipe/w");
846 | Ne.Res () ->
847 state.stderr <- dupstderr;
848 state.errfd <- Some r;
849 end;
851 else (
852 state.newerrmsgs <- false;
853 begin match state.errfd with
854 | Some fd ->
855 begin match Ne.dup2 state.stderr Unix.stderr with
856 | Ne.Exn exn ->
857 dolog "failed to dup2 original stderr: %s" (exntos exn)
858 | Ne.Res () ->
859 Ne.clo fd (clofail "dup of stderr");
860 state.errfd <- None;
861 end;
862 | None -> ()
863 end;
864 prerr_string (Buffer.contents state.errmsgs);
865 flush stderr;
866 Buffer.clear state.errmsgs;
870 module G =
871 struct
872 let postRedisplay who =
873 if conf.verbose
874 then prerr_endline ("redisplay for " ^ who);
875 state.redisplay <- true;
877 end;;
879 let getopaque pageno =
880 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
881 with Not_found -> None
884 let putopaque pageno opaque =
885 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
888 let pagetranslatepoint l x y =
889 let dy = y - l.pagedispy in
890 let y = dy + l.pagey in
891 let dx = x - l.pagedispx in
892 let x = dx + l.pagex in
893 (x, y);
896 let onppundermouse g x y d =
897 let rec f = function
898 | l :: rest ->
899 begin match getopaque l.pageno with
900 | Some opaque ->
901 let x0 = l.pagedispx in
902 let x1 = x0 + l.pagevw in
903 let y0 = l.pagedispy in
904 let y1 = y0 + l.pagevh in
905 if y >= y0 && y <= y1 && x >= x0 && x <= x1
906 then
907 let px, py = pagetranslatepoint l x y in
908 match g opaque l px py with
909 | Some res -> res
910 | None -> f rest
911 else f rest
912 | _ ->
913 f rest
915 | [] -> d
917 f state.layout
920 let getunder x y =
921 let g opaque l px py =
922 if state.bzoom
923 then (
924 match rectofblock opaque px py with
925 | Some a ->
926 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
927 state.rects <- [l.pageno, l.pageno mod 3, rect];
928 G.postRedisplay "getunder";
929 | None -> ()
931 match whatsunder opaque px py with
932 | Unone -> None
933 | under -> Some under
935 onppundermouse g x y Unone
938 let unproject x y =
939 let g opaque l x y =
940 match unproject opaque x y with
941 | Some (x, y) -> Some (Some (l.pageno, x, y))
942 | None -> None
944 onppundermouse g x y None;
947 let showtext c s =
948 state.text <- Printf.sprintf "%c%s" c s;
949 G.postRedisplay "showtext";
952 let paxunder x y =
953 let g opaque l px py =
954 if markunder opaque px py conf.paxmark
955 then (
956 Some (fun () ->
957 match getopaque l.pageno with
958 | None -> ()
959 | Some opaque ->
960 match Ne.pipe () with
961 | Ne.Exn exn ->
962 showtext '!'
963 (Printf.sprintf
964 "can not create mark pipe: %s"
965 (exntos exn));
966 | Ne.Res (r, w) ->
967 let doclose what fd =
968 Ne.clo fd (fun msg ->
969 dolog "%s close failed: %s" what msg)
972 popen conf.paxcmd [r, 0; w, -1];
973 copysel w opaque false;
974 doclose "pipe/r" r;
975 G.postRedisplay "paxunder";
976 with exn ->
977 dolog "can not execute %S: %s"
978 conf.paxcmd (exntos exn);
979 doclose "pipe/r" r;
980 doclose "pipe/w" w;
983 else None
985 G.postRedisplay "paxunder";
986 state.roam <-
987 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
990 let selstring s =
991 match Ne.pipe () with
992 | Ne.Exn exn ->
993 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
994 | Ne.Res (r, w) ->
995 let popened =
996 try popen conf.selcmd [r, 0; w, -1]; true
997 with exn ->
998 showtext '!'
999 (Printf.sprintf "failed to execute %s: %s"
1000 conf.selcmd (exntos exn));
1001 false
1003 let clo cap fd =
1004 Ne.clo fd (fun msg ->
1005 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1008 if popened
1009 then
1010 (try
1011 let l = String.length s in
1012 let n = tempfailureretry (Unix.write w s 0) l in
1013 if n != l
1014 then
1015 showtext '!'
1016 (Printf.sprintf
1017 "failed to write %d characters to sel pipe, wrote %d"
1020 with exn ->
1021 showtext '!'
1022 (Printf.sprintf "failed to write to sel pipe: %s"
1023 (exntos exn)
1026 else dolog "%s" s;
1027 clo "pipe/r" r;
1028 clo "pipe/w" w;
1031 let undertext = function
1032 | Unone -> "none"
1033 | Ulinkuri s -> s
1034 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1035 | Utext s -> "font: " ^ s
1036 | Uunexpected s -> "unexpected: " ^ s
1037 | Ulaunch s -> "launch: " ^ s
1038 | Unamed s -> "named: " ^ s
1039 | Uremote (filename, pageno) ->
1040 Printf.sprintf "%s: page %d" filename (pageno+1)
1041 | Uremotedest (filename, destname) ->
1042 Printf.sprintf "%s: destination %S" filename destname
1045 let updateunder x y =
1046 match getunder x y with
1047 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1048 | Ulinkuri uri ->
1049 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1050 Wsi.setcursor Wsi.CURSOR_INFO
1051 | Ulinkgoto (pageno, _) ->
1052 if conf.underinfo
1053 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1054 Wsi.setcursor Wsi.CURSOR_INFO
1055 | Utext s ->
1056 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1057 Wsi.setcursor Wsi.CURSOR_TEXT
1058 | Uunexpected s ->
1059 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1060 Wsi.setcursor Wsi.CURSOR_INHERIT
1061 | Ulaunch s ->
1062 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1063 Wsi.setcursor Wsi.CURSOR_INHERIT
1064 | Unamed s ->
1065 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1066 Wsi.setcursor Wsi.CURSOR_INHERIT
1067 | Uremote (filename, pageno) ->
1068 if conf.underinfo then showtext 'r'
1069 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1070 Wsi.setcursor Wsi.CURSOR_INFO
1071 | Uremotedest (filename, destname) ->
1072 if conf.underinfo then showtext 'r'
1073 (Printf.sprintf "emote destination: %s (%S)" filename destname);
1074 Wsi.setcursor Wsi.CURSOR_INFO
1077 let showlinktype under =
1078 if conf.underinfo
1079 then
1080 match under with
1081 | Unone -> ()
1082 | under ->
1083 let s = undertext under in
1084 showtext ' ' s
1087 let addchar s c =
1088 let b = Buffer.create (String.length s + 1) in
1089 Buffer.add_string b s;
1090 Buffer.add_char b c;
1091 Buffer.contents b;
1094 module type TextEnumType =
1096 type t
1097 val name : string
1098 val names : string array
1099 end;;
1101 module TextEnumMake (Ten : TextEnumType) =
1102 struct
1103 let names = Ten.names;;
1104 let to_int (t : Ten.t) = Obj.magic t;;
1105 let to_string t = names.(to_int t);;
1106 let of_int n : Ten.t = Obj.magic n;;
1107 let of_string s =
1108 let rec find i =
1109 if i = Array.length names
1110 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1111 else (
1112 if Ten.names.(i) = s
1113 then of_int i
1114 else find (i+1)
1116 in find 0;;
1117 end;;
1119 module CSTE = TextEnumMake (struct
1120 type t = colorspace;;
1121 let name = "colorspace";;
1122 let names = [|"rgb"; "bgr"; "gray"|];;
1123 end);;
1125 module MTE = TextEnumMake (struct
1126 type t = mark;;
1127 let name = "mark";;
1128 let names = [|"page"; "block"; "line"; "word"|];;
1129 end);;
1131 module FMTE = TextEnumMake (struct
1132 type t= fitmodel;;
1133 let name = "fitmodel";;
1134 let names = [|"width"; "proportional"; "page"|];;
1135 end);;
1137 let intentry_with_suffix text key =
1138 let c =
1139 if key >= 32 && key < 127
1140 then Char.chr key
1141 else '\000'
1143 match Char.lowercase c with
1144 | '0' .. '9' ->
1145 let text = addchar text c in
1146 TEcont text
1148 | 'k' | 'm' | 'g' ->
1149 let text = addchar text c in
1150 TEcont text
1152 | _ ->
1153 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1154 TEcont text
1157 let multicolumns_to_string (n, a, b) =
1158 if a = 0 && b = 0
1159 then Printf.sprintf "%d" n
1160 else Printf.sprintf "%d,%d,%d" n a b;
1163 let multicolumns_of_string s =
1165 (int_of_string s, 0, 0)
1166 with _ ->
1167 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1168 if a > 1 || b > 1
1169 then failwith "subtly broken"; (n, a, b)
1173 let readcmd fd =
1174 let s = "xxxx" in
1175 let n = tempfailureretry (Unix.read fd s 0) 4 in
1176 if n != 4 then error "incomplete read(len) = %d" n;
1177 let len = 0
1178 lor (Char.code s.[0] lsl 24)
1179 lor (Char.code s.[1] lsl 16)
1180 lor (Char.code s.[2] lsl 8)
1181 lor (Char.code s.[3] lsl 0)
1183 let s = String.create len in
1184 let n = tempfailureretry (Unix.read fd s 0) len in
1185 if n != len then error "incomplete read(data) %d vs %d" n len;
1189 let btod b = if b then 1 else 0;;
1191 let wcmd fmt =
1192 let b = Buffer.create 16 in
1193 Buffer.add_string b "llll";
1194 Printf.kbprintf
1195 (fun b ->
1196 let s = Buffer.contents b in
1197 let n = String.length s in
1198 let len = n - 4 in
1199 (* dolog "wcmd %S" (String.sub s 4 len); *)
1200 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1201 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1202 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1203 s.[3] <- Char.chr (len land 0xff);
1204 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1205 if n' != n then error "write failed %d vs %d" n' n;
1206 ) b fmt;
1209 let calcips h =
1210 let d = state.winh - h in
1211 max conf.interpagespace ((d + 1) / 2)
1214 let rowyh (c, coverA, coverB) b n =
1215 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1216 then
1217 let _, _, vy, (_, _, h, _) = b.(n) in
1218 (vy, h)
1219 else
1220 let n' = n - coverA in
1221 let d = n' mod c in
1222 let s = n - d in
1223 let e = min state.pagecount (s + c) in
1224 let rec find m miny maxh = if m = e then miny, maxh else
1225 let _, _, y, (_, _, h, _) = b.(m) in
1226 let miny = min miny y in
1227 let maxh = max maxh h in
1228 find (m+1) miny maxh
1229 in find s max_int 0
1232 let calcheight () =
1233 match conf.columns with
1234 | Cmulti ((_, _, _) as cl, b) ->
1235 if Array.length b > 0
1236 then
1237 let y, h = rowyh cl b (Array.length b - 1) in
1238 y + h + (if conf.presentation then calcips h else 0)
1239 else 0
1240 | Csingle b ->
1241 if Array.length b > 0
1242 then
1243 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1244 y + h + (if conf.presentation then calcips h else 0)
1245 else 0
1246 | Csplit (_, b) ->
1247 if Array.length b > 0
1248 then
1249 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1250 y + h
1251 else 0
1254 let getpageywh pageno =
1255 let pageno = bound pageno 0 (state.pagecount-1) in
1256 match conf.columns with
1257 | Csingle b ->
1258 if Array.length b = 0
1259 then 0, 0, 0
1260 else
1261 let (_, _, y, (_, w, h, _)) = b.(pageno) in
1262 let y =
1263 if conf.presentation
1264 then y - calcips h
1265 else y
1267 y, w, h
1268 | Cmulti (cl, b) ->
1269 if Array.length b = 0
1270 then 0, 0, 0
1271 else
1272 let y, h = rowyh cl b pageno in
1273 let (_, _, _, (_, w, _, _)) = b.(pageno) in
1274 let y =
1275 if conf.presentation
1276 then y - calcips h
1277 else y
1279 y, w, h
1280 | Csplit (c, b) ->
1281 if Array.length b = 0
1282 then 0, 0, 0
1283 else
1284 let n = pageno*c in
1285 let (_, _, y, (_, w, h, _)) = b.(n) in
1286 y, w / c, h
1289 let getpageyh pageno =
1290 let y,_,h = getpageywh pageno in
1291 y, h;
1294 let getpagedim pageno =
1295 let rec f ppdim l =
1296 match l with
1297 | (n, _, _, _) as pdim :: rest ->
1298 if n >= pageno
1299 then (if n = pageno then pdim else ppdim)
1300 else f pdim rest
1302 | [] -> ppdim
1304 f (-1, -1, -1, -1) state.pdims
1307 let getpagey pageno = fst (getpageyh pageno);;
1309 let nogeomcmds cmds =
1310 match cmds with
1311 | s, [] -> emptystr s
1312 | _ -> false
1315 let page_of_y y =
1316 let ((c, coverA, coverB) as cl), b =
1317 match conf.columns with
1318 | Csingle b -> (1, 0, 0), b
1319 | Cmulti (c, b) -> c, b
1320 | Csplit (_, b) -> (1, 0, 0), b
1322 if Array.length b = 0
1323 then -1
1324 else
1325 let rec bsearch nmin nmax =
1326 if nmin > nmax
1327 then bound nmin 0 (state.pagecount-1)
1328 else
1329 let n = (nmax + nmin) / 2 in
1330 let vy, h = rowyh cl b n in
1331 let y0, y1 =
1332 if conf.presentation
1333 then
1334 let ips = calcips h in
1335 let y0 = vy - ips in
1336 let y1 = vy + h + ips in
1337 y0, y1
1338 else (
1339 if n = 0
1340 then 0, vy + h + conf.interpagespace
1341 else
1342 let y0 = vy - conf.interpagespace in
1343 y0, y0 + h + conf.interpagespace
1346 if y >= y0 && y < y1
1347 then (
1348 if c = 1
1349 then n
1350 else (
1351 if n > coverA
1352 then
1353 if n < state.pagecount - coverB
1354 then ((n-coverA)/c)*c + coverA
1355 else n
1356 else n
1359 else (
1360 if y > y0
1361 then bsearch (n+1) nmax
1362 else bsearch nmin (n-1)
1365 let r = bsearch 0 (state.pagecount-1) in
1369 let layoutN ((columns, coverA, coverB), b) y sh =
1370 let sh = sh - (hscrollh ()) in
1371 let rec fold accu n =
1372 if n = Array.length b
1373 then accu
1374 else
1375 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1376 if (vy - y) > sh &&
1377 (n = coverA - 1
1378 || n = state.pagecount - coverB
1379 || (n - coverA) mod columns = columns - 1)
1380 then accu
1381 else
1382 let accu =
1383 if vy + h > y
1384 then
1385 let pagey = max 0 (y - vy) in
1386 let pagedispy = if pagey > 0 then 0 else vy - y in
1387 let pagedispx, pagex =
1388 let pdx =
1389 if n = coverA - 1 || n = state.pagecount - coverB
1390 then state.x + (wadjsb state.winw - w) / 2
1391 else dx + xoff + state.x
1393 if pdx < 0
1394 then 0, -pdx
1395 else pdx, 0
1397 let pagevw =
1398 let vw = wadjsb state.winw - pagedispx in
1399 let pw = w - pagex in
1400 min vw pw
1402 let pagevh = min (h - pagey) (sh - pagedispy) in
1403 if pagevw > 0 && pagevh > 0
1404 then
1405 let e =
1406 { pageno = n
1407 ; pagedimno = pdimno
1408 ; pagew = w
1409 ; pageh = h
1410 ; pagex = pagex
1411 ; pagey = pagey
1412 ; pagevw = pagevw
1413 ; pagevh = pagevh
1414 ; pagedispx = pagedispx
1415 ; pagedispy = pagedispy
1416 ; pagecol = 0
1419 e :: accu
1420 else
1421 accu
1422 else
1423 accu
1425 fold accu (n+1)
1427 if Array.length b = 0
1428 then []
1429 else List.rev (fold [] (page_of_y y))
1432 let layoutS (columns, b) y sh =
1433 let sh = sh - hscrollh () in
1434 let rec fold accu n =
1435 if n = Array.length b
1436 then accu
1437 else
1438 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1439 if (vy - y) > sh
1440 then accu
1441 else
1442 let accu =
1443 if vy + pageh > y
1444 then
1445 let x = xoff + state.x in
1446 let pagey = max 0 (y - vy) in
1447 let pagedispy = if pagey > 0 then 0 else vy - y in
1448 let pagedispx, pagex =
1449 if px = 0
1450 then (
1451 if x < 0
1452 then 0, -x
1453 else x, 0
1455 else (
1456 let px = px - x in
1457 if px < 0
1458 then -px, 0
1459 else 0, px
1462 let pagecolw = pagew/columns in
1463 let pagedispx =
1464 if pagecolw < state.winw
1465 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1466 else pagedispx
1468 let pagevw =
1469 let vw = wadjsb state.winw - pagedispx in
1470 let pw = pagew - pagex in
1471 min vw pw
1473 let pagevw = min pagevw pagecolw in
1474 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1475 if pagevw > 0 && pagevh > 0
1476 then
1477 let e =
1478 { pageno = n/columns
1479 ; pagedimno = pdimno
1480 ; pagew = pagew
1481 ; pageh = pageh
1482 ; pagex = pagex
1483 ; pagey = pagey
1484 ; pagevw = pagevw
1485 ; pagevh = pagevh
1486 ; pagedispx = pagedispx
1487 ; pagedispy = pagedispy
1488 ; pagecol = n mod columns
1491 e :: accu
1492 else
1493 accu
1494 else
1495 accu
1497 fold accu (n+1)
1499 List.rev (fold [] 0)
1502 let layout y sh =
1503 if nogeomcmds state.geomcmds
1504 then
1505 match conf.columns with
1506 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1507 | Cmulti c -> layoutN c y sh
1508 | Csplit s -> layoutS s y sh
1509 else []
1512 let clamp incr =
1513 let y = state.y + incr in
1514 let y = max 0 y in
1515 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1519 let itertiles l f =
1520 let tilex = l.pagex mod conf.tilew in
1521 let tiley = l.pagey mod conf.tileh in
1523 let col = l.pagex / conf.tilew in
1524 let row = l.pagey / conf.tileh in
1526 let rec rowloop row y0 dispy h =
1527 if h = 0
1528 then ()
1529 else (
1530 let dh = conf.tileh - y0 in
1531 let dh = min h dh in
1532 let rec colloop col x0 dispx w =
1533 if w = 0
1534 then ()
1535 else (
1536 let dw = conf.tilew - x0 in
1537 let dw = min w dw in
1539 f col row dispx dispy x0 y0 dw dh;
1540 colloop (col+1) 0 (dispx+dw) (w-dw)
1543 colloop col tilex l.pagedispx l.pagevw;
1544 rowloop (row+1) 0 (dispy+dh) (h-dh)
1547 if l.pagevw > 0 && l.pagevh > 0
1548 then rowloop row tiley l.pagedispy l.pagevh;
1551 let gettileopaque l col row =
1552 let key =
1553 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1555 try Some (Hashtbl.find state.tilemap key)
1556 with Not_found -> None
1559 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1560 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1561 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1564 let drawtiles l color =
1565 GlDraw.color color;
1566 let f col row x y tilex tiley w h =
1567 match gettileopaque l col row with
1568 | Some (opaque, _, t) ->
1569 let params = x, y, w, h, tilex, tiley in
1570 if conf.invert
1571 then (
1572 Gl.enable `blend;
1573 GlFunc.blend_func `zero `one_minus_src_color;
1575 drawtile params opaque;
1576 if conf.invert
1577 then Gl.disable `blend;
1578 if conf.debug
1579 then (
1580 let s = Printf.sprintf
1581 "%d[%d,%d] %f sec"
1582 l.pageno col row t
1584 let w = measurestr fstate.fontsize s in
1585 GlMisc.push_attrib [`current];
1586 GlDraw.color (0.0, 0.0, 0.0);
1587 GlDraw.rect
1588 (float (x-2), float (y-2))
1589 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1590 GlDraw.color (1.0, 1.0, 1.0);
1591 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1592 GlMisc.pop_attrib ();
1595 | _ ->
1596 let w =
1597 let lw = wadjsb state.winw - x in
1598 min lw w
1599 and h =
1600 let lh = state.winh - y in
1601 min lh h
1603 begin match state.texid with
1604 | Some id ->
1605 Gl.enable `texture_2d;
1606 GlTex.bind_texture `texture_2d id;
1607 let x0 = float x
1608 and y0 = float y
1609 and x1 = float (x+w)
1610 and y1 = float (y+h) in
1612 let tw = float w /. 16.0
1613 and th = float h /. 16.0 in
1614 let tx0 = float tilex /. 16.0
1615 and ty0 = float tiley /. 16.0 in
1616 let tx1 = tx0 +. tw
1617 and ty1 = ty0 +. th in
1618 GlDraw.begins `quads;
1619 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1620 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1621 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1622 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1623 GlDraw.ends ();
1625 Gl.disable `texture_2d;
1626 | None ->
1627 GlDraw.color (1.0, 1.0, 1.0);
1628 GlDraw.rect
1629 (float x, float y)
1630 (float (x+w), float (y+h));
1631 end;
1632 if w > 128 && h > fstate.fontsize + 10
1633 then (
1634 GlDraw.color (0.0, 0.0, 0.0);
1635 let c, r =
1636 if conf.verbose
1637 then (col*conf.tilew, row*conf.tileh)
1638 else col, row
1640 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1642 GlDraw.color color;
1644 itertiles l f
1647 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1649 let tilevisible1 l x y =
1650 let ax0 = l.pagex
1651 and ax1 = l.pagex + l.pagevw
1652 and ay0 = l.pagey
1653 and ay1 = l.pagey + l.pagevh in
1655 let bx0 = x
1656 and by0 = y in
1657 let bx1 = min (bx0 + conf.tilew) l.pagew
1658 and by1 = min (by0 + conf.tileh) l.pageh in
1660 let rx0 = max ax0 bx0
1661 and ry0 = max ay0 by0
1662 and rx1 = min ax1 bx1
1663 and ry1 = min ay1 by1 in
1665 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1666 nonemptyintersection
1669 let tilevisible layout n x y =
1670 let rec findpageinlayout m = function
1671 | l :: rest when l.pageno = n ->
1672 tilevisible1 l x y || (
1673 match conf.columns with
1674 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1675 | _ -> false
1677 | _ :: rest -> findpageinlayout 0 rest
1678 | [] -> false
1680 findpageinlayout 0 layout;
1683 let tileready l x y =
1684 tilevisible1 l x y &&
1685 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1688 let tilepage n p layout =
1689 let rec loop = function
1690 | l :: rest ->
1691 if l.pageno = n
1692 then
1693 let f col row _ _ _ _ _ _ =
1694 if state.currently = Idle
1695 then
1696 match gettileopaque l col row with
1697 | Some _ -> ()
1698 | None ->
1699 let x = col*conf.tilew
1700 and y = row*conf.tileh in
1701 let w =
1702 let w = l.pagew - x in
1703 min w conf.tilew
1705 let h =
1706 let h = l.pageh - y in
1707 min h conf.tileh
1709 let pbo =
1710 if conf.usepbo
1711 then getpbo w h conf.colorspace
1712 else "0"
1714 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1715 state.currently <-
1716 Tiling (
1717 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1718 conf.tilew, conf.tileh
1721 itertiles l f;
1722 else
1723 loop rest
1725 | [] -> ()
1727 if nogeomcmds state.geomcmds
1728 then loop layout;
1731 let preloadlayout y =
1732 let y = if y < state.winh then 0 else y - state.winh in
1733 let h = state.winh*3 in
1734 layout y h;
1737 let load pages =
1738 let rec loop pages =
1739 if state.currently != Idle
1740 then ()
1741 else
1742 match pages with
1743 | l :: rest ->
1744 begin match getopaque l.pageno with
1745 | None ->
1746 wcmd "page %d %d" l.pageno l.pagedimno;
1747 state.currently <- Loading (l, state.gen);
1748 | Some opaque ->
1749 tilepage l.pageno opaque pages;
1750 loop rest
1751 end;
1752 | _ -> ()
1754 if nogeomcmds state.geomcmds
1755 then loop pages
1758 let preload pages =
1759 load pages;
1760 if conf.preload && state.currently = Idle
1761 then load (preloadlayout state.y);
1764 let layoutready layout =
1765 let rec fold all ls =
1766 all && match ls with
1767 | l :: rest ->
1768 let seen = ref false in
1769 let allvisible = ref true in
1770 let foo col row _ _ _ _ _ _ =
1771 seen := true;
1772 allvisible := !allvisible &&
1773 begin match gettileopaque l col row with
1774 | Some _ -> true
1775 | None -> false
1778 itertiles l foo;
1779 fold (!seen && !allvisible) rest
1780 | [] -> true
1782 let alltilesvisible = fold true layout in
1783 alltilesvisible;
1786 let gotoy y =
1787 let y = bound y 0 state.maxy in
1788 let y, layout, proceed =
1789 match conf.maxwait with
1790 | Some time when state.ghyll == noghyll ->
1791 begin match state.throttle with
1792 | None ->
1793 let layout = layout y state.winh in
1794 let ready = layoutready layout in
1795 if not ready
1796 then (
1797 load layout;
1798 state.throttle <- Some (layout, y, now ());
1800 else G.postRedisplay "gotoy showall (None)";
1801 y, layout, ready
1802 | Some (_, _, started) ->
1803 let dt = now () -. started in
1804 if dt > time
1805 then (
1806 state.throttle <- None;
1807 let layout = layout y state.winh in
1808 load layout;
1809 G.postRedisplay "maxwait";
1810 y, layout, true
1812 else -1, [], false
1815 | _ ->
1816 let layout = layout y state.winh in
1817 if not !wtmode || layoutready layout
1818 then G.postRedisplay "gotoy ready";
1819 y, layout, true
1821 if proceed
1822 then (
1823 state.y <- y;
1824 state.layout <- layout;
1825 begin match state.mode with
1826 | LinkNav (Ltexact (pageno, linkno)) ->
1827 let rec loop = function
1828 | [] ->
1829 state.mode <- LinkNav (Ltgendir 0)
1830 | l :: _ when l.pageno = pageno ->
1831 begin match getopaque pageno with
1832 | None ->
1833 state.mode <- LinkNav (Ltgendir 0)
1834 | Some opaque ->
1835 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1836 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1837 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1838 then state.mode <- LinkNav (Ltgendir 0)
1840 | _ :: rest -> loop rest
1842 loop layout
1843 | _ -> ()
1844 end;
1845 begin match state.mode with
1846 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1847 if not (pagevisible layout pageno)
1848 then (
1849 match state.layout with
1850 | [] -> ()
1851 | l :: _ ->
1852 state.mode <- Birdseye (
1853 conf, leftx, l.pageno, hooverpageno, anchor
1856 | LinkNav (Ltgendir dir as lt) ->
1857 let linknav =
1858 let rec loop = function
1859 | [] -> lt
1860 | l :: rest ->
1861 match getopaque l.pageno with
1862 | None -> loop rest
1863 | Some opaque ->
1864 let link =
1865 let ld =
1866 if dir = 0
1867 then LDfirstvisible (l.pagex, l.pagey, dir)
1868 else (
1869 if dir > 0 then LDfirst else LDlast
1872 findlink opaque ld
1874 match link with
1875 | Lnotfound -> loop rest
1876 | Lfound n ->
1877 showlinktype (getlink opaque n);
1878 Ltexact (l.pageno, n)
1880 loop state.layout
1882 state.mode <- LinkNav linknav
1883 | _ -> ()
1884 end;
1885 preload layout;
1887 state.ghyll <- noghyll;
1888 if conf.updatecurs
1889 then (
1890 let mx, my = state.mpos in
1891 updateunder mx my;
1895 let conttiling pageno opaque =
1896 tilepage pageno opaque
1897 (if conf.preload then preloadlayout state.y else state.layout)
1900 let gotoy_and_clear_text y =
1901 if not conf.verbose then state.text <- "";
1902 gotoy y;
1905 let getanchor1 l =
1906 let top =
1907 let coloff = l.pagecol * l.pageh in
1908 float (l.pagey + coloff) /. float l.pageh
1910 let dtop =
1911 if l.pagedispy = 0
1912 then
1914 else (
1915 if conf.presentation
1916 then float l.pagedispy /. float (calcips l.pageh)
1917 else float l.pagedispy /. float conf.interpagespace
1920 (l.pageno, top, dtop)
1923 let getanchor () =
1924 match state.layout with
1925 | l :: _ -> getanchor1 l
1926 | [] ->
1927 let n = page_of_y state.y in
1928 if n = -1
1929 then state.anchor
1930 else
1931 let y, h = getpageyh n in
1932 let dy = y - state.y in
1933 let dtop =
1934 if conf.presentation
1935 then
1936 let ips = calcips h in
1937 float (dy + ips) /. float ips
1938 else
1939 float dy /. float conf.interpagespace
1941 (n, 0.0, dtop)
1944 let getanchory (n, top, dtop) =
1945 let y, h = getpageyh n in
1946 if conf.presentation
1947 then
1948 let ips = calcips h in
1949 y + truncate (top*.float h -. dtop*.float ips) + ips;
1950 else
1951 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1954 let gotoanchor anchor =
1955 gotoy (getanchory anchor);
1958 let addnav () =
1959 cbput state.hists.nav (getanchor ());
1962 let getnav dir =
1963 let anchor = cbgetc state.hists.nav dir in
1964 getanchory anchor;
1967 let gotoghyll y =
1968 let scroll f n a b =
1969 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1970 let snake f a b =
1971 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1972 if f < a
1973 then s (float f /. float a)
1974 else (
1975 if f > b
1976 then 1.0 -. s ((float (f-b) /. float (n-b)))
1977 else 1.0
1980 snake f a b
1981 and summa n a b =
1982 let ins = float a *. 0.5
1983 and outs = float (n-b) *. 0.5 in
1984 let ones = b - a in
1985 ins +. outs +. float ones
1987 let rec set (_N, _A, _B) y sy =
1988 let sum = summa _N _A _B in
1989 let dy = float (y - sy) in
1990 state.ghyll <- (
1991 let rec gf n y1 o =
1992 if n >= _N
1993 then state.ghyll <- noghyll
1994 else
1995 let go n =
1996 let s = scroll n _N _A _B in
1997 let y1 = y1 +. ((s *. dy) /. sum) in
1998 gotoy_and_clear_text (truncate y1);
1999 state.ghyll <- gf (n+1) y1;
2001 match o with
2002 | None -> go n
2003 | Some y' -> set (_N/2, 1, 1) y' state.y
2005 gf 0 (float state.y)
2008 match conf.ghyllscroll with
2009 | None ->
2010 gotoy_and_clear_text y
2011 | Some nab ->
2012 if state.ghyll == noghyll
2013 then set nab y state.y
2014 else state.ghyll (Some y)
2017 let gotopage n top =
2018 let y, h = getpageyh n in
2019 let y = y + (truncate (top *. float h)) in
2020 gotoghyll y
2023 let gotopage1 n top =
2024 let y = getpagey n in
2025 let y = y + top in
2026 gotoghyll y
2029 let invalidate s f =
2030 state.layout <- [];
2031 state.pdims <- [];
2032 state.rects <- [];
2033 state.rects1 <- [];
2034 match state.geomcmds with
2035 | ps, [] when emptystr ps ->
2036 f ();
2037 state.geomcmds <- s, [];
2039 | ps, [] ->
2040 state.geomcmds <- ps, [s, f];
2042 | ps, (s', _) :: rest when s' = s ->
2043 state.geomcmds <- ps, ((s, f) :: rest);
2045 | ps, cmds ->
2046 state.geomcmds <- ps, ((s, f) :: cmds);
2049 let flushpages () =
2050 Hashtbl.iter (fun _ opaque ->
2051 wcmd "freepage %s" opaque;
2052 ) state.pagemap;
2053 Hashtbl.clear state.pagemap;
2056 let flushtiles () =
2057 if not (Queue.is_empty state.tilelru)
2058 then (
2059 Queue.iter (fun (k, p, s) ->
2060 wcmd "freetile %s" p;
2061 state.memused <- state.memused - s;
2062 Hashtbl.remove state.tilemap k;
2063 ) state.tilelru;
2064 state.uioh#infochanged Memused;
2065 Queue.clear state.tilelru;
2067 load state.layout;
2070 let stateh h =
2071 let h = truncate (float h*.conf.zoom) in
2072 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2073 h - d
2076 let opendoc path password =
2077 state.path <- path;
2078 state.password <- password;
2079 state.gen <- state.gen + 1;
2080 state.docinfo <- [];
2082 flushpages ();
2083 setaalevel conf.aalevel;
2084 let titlepath =
2085 if emptystr state.origin
2086 then path
2087 else state.origin
2089 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2090 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2091 invalidate "reqlayout"
2092 (fun () ->
2093 wcmd "reqlayout %d %d %d %s\000"
2094 conf.angle (FMTE.to_int conf.fitmodel)
2095 (stateh state.winh) state.nameddest
2099 let reload () =
2100 state.anchor <- getanchor ();
2101 opendoc state.path state.password;
2104 let scalecolor c =
2105 let c = c *. conf.colorscale in
2106 (c, c, c);
2109 let scalecolor2 (r, g, b) =
2110 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2113 let docolumns = function
2114 | Csingle _ ->
2115 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2116 let rec loop pageno pdimno pdim y ph pdims =
2117 if pageno = state.pagecount
2118 then ()
2119 else
2120 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2121 match pdims with
2122 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2123 pdimno+1, pdim, rest
2124 | _ ->
2125 pdimno, pdim, pdims
2127 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2128 let y = y +
2129 (if conf.presentation
2130 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2131 else (if pageno = 0 then 0 else conf.interpagespace)
2134 a.(pageno) <- (pdimno, x, y, pdim);
2135 loop (pageno+1) pdimno pdim (y + h) h pdims
2137 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2138 conf.columns <- Csingle a;
2140 | Cmulti ((columns, coverA, coverB), _) ->
2141 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2142 let rec loop pageno pdimno pdim x y rowh pdims =
2143 let rec fixrow m = if m = pageno then () else
2144 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2145 if h < rowh
2146 then (
2147 let y = y + (rowh - h) / 2 in
2148 a.(m) <- (pdimno, x, y, pdim);
2150 fixrow (m+1)
2152 if pageno = state.pagecount
2153 then fixrow (((pageno - 1) / columns) * columns)
2154 else
2155 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2156 match pdims with
2157 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2158 pdimno+1, pdim, rest
2159 | _ ->
2160 pdimno, pdim, pdims
2162 let x, y, rowh' =
2163 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2164 then (
2165 let x = (wadjsb state.winw - w) / 2 in
2166 let ips =
2167 if conf.presentation then calcips h else conf.interpagespace in
2168 x, y + ips + rowh, h
2170 else (
2171 if (pageno - coverA) mod columns = 0
2172 then (
2173 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2174 let y =
2175 if conf.presentation
2176 then
2177 let ips = calcips h in
2178 y + (if pageno = 0 then 0 else calcips rowh + ips)
2179 else
2180 y + (if pageno = 0 then 0 else conf.interpagespace)
2182 x, y + rowh, h
2184 else x, y, max rowh h
2187 let y =
2188 if pageno > 1 && (pageno - coverA) mod columns = 0
2189 then (
2190 let y =
2191 if pageno = columns && conf.presentation
2192 then (
2193 let ips = calcips rowh in
2194 for i = 0 to pred columns
2196 let (pdimno, x, y, pdim) = a.(i) in
2197 a.(i) <- (pdimno, x, y+ips, pdim)
2198 done;
2199 y+ips;
2201 else y
2203 fixrow (pageno - columns);
2206 else y
2208 a.(pageno) <- (pdimno, x, y, pdim);
2209 let x = x + w + xoff*2 + conf.interpagespace in
2210 loop (pageno+1) pdimno pdim x y rowh' pdims
2212 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2213 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2215 | Csplit (c, _) ->
2216 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2217 let rec loop pageno pdimno pdim y pdims =
2218 if pageno = state.pagecount
2219 then ()
2220 else
2221 let pdimno, ((_, w, h, _) as pdim), pdims =
2222 match pdims with
2223 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2224 pdimno+1, pdim, rest
2225 | _ ->
2226 pdimno, pdim, pdims
2228 let cw = w / c in
2229 let rec loop1 n x y =
2230 if n = c then y else (
2231 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2232 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2235 let y = loop1 0 0 y in
2236 loop (pageno+1) pdimno pdim y pdims
2238 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2239 conf.columns <- Csplit (c, a);
2242 let represent () =
2243 docolumns conf.columns;
2244 state.maxy <- calcheight ();
2245 if state.reprf == noreprf
2246 then (
2247 match state.mode with
2248 | Birdseye (_, _, pageno, _, _) ->
2249 let y, h = getpageyh pageno in
2250 let top = (state.winh - h) / 2 in
2251 gotoy (max 0 (y - top))
2252 | _ -> gotoanchor state.anchor
2254 else (
2255 state.reprf ();
2256 state.reprf <- noreprf;
2260 let reshape w h =
2261 GlDraw.viewport 0 0 w h;
2262 let firsttime = state.geomcmds == firstgeomcmds in
2263 if not firsttime && nogeomcmds state.geomcmds
2264 then state.anchor <- getanchor ();
2266 state.winw <- w;
2267 let w = wadjsb (truncate (float w *. conf.zoom)) in
2268 let w = max w 2 in
2269 state.winh <- h;
2270 setfontsize fstate.fontsize;
2271 GlMat.mode `modelview;
2272 GlMat.load_identity ();
2274 GlMat.mode `projection;
2275 GlMat.load_identity ();
2276 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2277 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2278 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2280 let relx =
2281 if conf.zoom <= 1.0
2282 then 0.0
2283 else float state.x /. float state.w
2285 invalidate "geometry"
2286 (fun () ->
2287 state.w <- w;
2288 if not firsttime
2289 then state.x <- truncate (relx *. float w);
2290 let w =
2291 match conf.columns with
2292 | Csingle _ -> w
2293 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2294 | Csplit (c, _) -> w * c
2296 wcmd "geometry %d %d %d"
2297 w (stateh h) (FMTE.to_int conf.fitmodel)
2301 let enttext () =
2302 let len = String.length state.text in
2303 let drawstring s =
2304 let hscrollh =
2305 match state.mode with
2306 | Textentry _ | View | LinkNav _ ->
2307 let h, _, _ = state.uioh#scrollpw in
2309 | _ -> 0
2311 let rect x w =
2312 GlDraw.rect
2313 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2314 (x+.w, float (state.winh - hscrollh))
2317 let w = float (wadjsb state.winw - 1) in
2318 if state.progress >= 0.0 && state.progress < 1.0
2319 then (
2320 GlDraw.color (0.3, 0.3, 0.3);
2321 let w1 = w *. state.progress in
2322 rect 0.0 w1;
2323 GlDraw.color (0.0, 0.0, 0.0);
2324 rect w1 (w-.w1)
2326 else (
2327 GlDraw.color (0.0, 0.0, 0.0);
2328 rect 0.0 w;
2331 GlDraw.color (1.0, 1.0, 1.0);
2332 drawstring fstate.fontsize
2333 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2335 let s =
2336 match state.mode with
2337 | Textentry ((prefix, text, _, _, _, _), _) ->
2338 let s =
2339 if len > 0
2340 then
2341 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2342 else
2343 Printf.sprintf "%s%s_" prefix text
2347 | _ -> state.text
2349 let s =
2350 if state.newerrmsgs
2351 then (
2352 if not (istextentry state.mode) && state.uioh#eformsgs
2353 then
2354 let s1 = "(press 'e' to review error messasges)" in
2355 if nonemptystr s then s ^ " " ^ s1 else s1
2356 else s
2358 else s
2360 if nonemptystr s
2361 then drawstring s
2364 let gctiles () =
2365 let len = Queue.length state.tilelru in
2366 let layout = lazy (
2367 match state.throttle with
2368 | None ->
2369 if conf.preload
2370 then preloadlayout state.y
2371 else state.layout
2372 | Some (layout, _, _) ->
2373 layout
2374 ) in
2375 let rec loop qpos =
2376 if state.memused <= conf.memlimit
2377 then ()
2378 else (
2379 if qpos < len
2380 then
2381 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2382 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2383 let (_, pw, ph, _) = getpagedim n in
2385 gen = state.gen
2386 && colorspace = conf.colorspace
2387 && angle = conf.angle
2388 && pagew = pw
2389 && pageh = ph
2390 && (
2391 let x = col*conf.tilew
2392 and y = row*conf.tileh in
2393 tilevisible (Lazy.force_val layout) n x y
2395 then Queue.push lruitem state.tilelru
2396 else (
2397 freepbo p;
2398 wcmd "freetile %s" p;
2399 state.memused <- state.memused - s;
2400 state.uioh#infochanged Memused;
2401 Hashtbl.remove state.tilemap k;
2403 loop (qpos+1)
2406 loop 0
2409 let logcurrently = function
2410 | Idle -> dolog "Idle"
2411 | Loading (l, gen) ->
2412 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2413 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2414 dolog
2415 "Tiling %d[%d,%d] page=%s cs=%s angle"
2416 l.pageno col row pageopaque
2417 (CSTE.to_string colorspace)
2419 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2420 angle gen conf.angle state.gen
2421 tilew tileh
2422 conf.tilew conf.tileh
2424 | Outlining _ ->
2425 dolog "outlining"
2428 let splitatspace =
2429 let r = Str.regexp " " in
2430 fun s -> Str.bounded_split r s 2;
2433 let onpagerect pageno f =
2434 let b =
2435 match conf.columns with
2436 | Cmulti (_, b) -> b
2437 | Csingle b -> b
2438 | Csplit (_, b) -> b
2440 if pageno >= 0 && pageno < Array.length b
2441 then
2442 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2443 f w h
2446 let gotopagexy1 pageno x y =
2447 let _,w1,h1,leftx = getpagedim pageno in
2448 let top = y /. (float h1) in
2449 let left = x /. (float w1) in
2450 let py, w, h = getpageywh pageno in
2451 let wh = state.winh - hscrollh () in
2452 let x = left *. (float w) in
2453 let x = leftx + state.x + truncate x in
2454 let sx =
2455 if x < 0 || x >= wadjsb state.winw
2456 then state.x - x
2457 else state.x
2459 let pdy = truncate (top *. float h) in
2460 let y' = py + pdy in
2461 let dy = y' - state.y in
2462 let sy =
2463 if x != state.x || not (dy > 0 && dy < wh)
2464 then (
2465 if conf.presentation
2466 then
2467 if abs (py - y') > wh
2468 then y'
2469 else py
2470 else y';
2472 else state.y
2474 if state.x != sx || state.y != sy
2475 then (
2476 let x, y =
2477 if !wtmode
2478 then (
2479 let ww = wadjsb state.winw in
2480 let qx = sx / ww
2481 and qy = pdy / wh in
2482 let x = qx * ww
2483 and y = py + qy * wh in
2484 let x = if -x + ww > w1 then -(w1-ww) else x
2485 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2486 let y =
2487 if conf.presentation
2488 then
2489 if abs (py - y') > wh
2490 then y'
2491 else py
2492 else y';
2494 (x, y)
2496 else (sx, sy)
2498 state.x <- x;
2499 gotoy_and_clear_text y;
2501 else gotoy_and_clear_text state.y;
2504 let gotopagexy pageno x y =
2505 match state.mode with
2506 | Birdseye _ -> gotopage pageno 0.0
2507 | _ -> gotopagexy1 pageno x y
2510 let act cmds =
2511 (* dolog "%S" cmds; *)
2512 let cl = splitatspace cmds in
2513 let scan s fmt f =
2514 try Scanf.sscanf s fmt f
2515 with exn ->
2516 dolog "error processing '%S': %s" cmds (exntos exn);
2517 exit 1
2519 match cl with
2520 | "clear" :: [] ->
2521 state.uioh#infochanged Pdim;
2522 state.pdims <- [];
2524 | "clearrects" :: [] ->
2525 state.rects <- state.rects1;
2526 G.postRedisplay "clearrects";
2528 | "continue" :: args :: [] ->
2529 let n = scan args "%u" (fun n -> n) in
2530 state.pagecount <- n;
2531 begin match state.currently with
2532 | Outlining l ->
2533 state.currently <- Idle;
2534 state.outlines <- Array.of_list (List.rev l)
2535 | _ -> ()
2536 end;
2538 let cur, cmds = state.geomcmds in
2539 if emptystr cur
2540 then failwith "umpossible";
2542 begin match List.rev cmds with
2543 | [] ->
2544 state.geomcmds <- "", [];
2545 represent ();
2546 | (s, f) :: rest ->
2547 f ();
2548 state.geomcmds <- s, List.rev rest;
2549 end;
2550 if conf.maxwait = None && not !wtmode
2551 then G.postRedisplay "continue";
2553 | "title" :: args :: [] ->
2554 Wsi.settitle args
2556 | "msg" :: args :: [] ->
2557 showtext ' ' args
2559 | "vmsg" :: args :: [] ->
2560 if conf.verbose
2561 then showtext ' ' args
2563 | "emsg" :: args :: [] ->
2564 Buffer.add_string state.errmsgs args;
2565 state.newerrmsgs <- true;
2566 G.postRedisplay "error message"
2568 | "progress" :: args :: [] ->
2569 let progress, text =
2570 scan args "%f %n"
2571 (fun f pos ->
2572 f, String.sub args pos (String.length args - pos))
2574 state.text <- text;
2575 state.progress <- progress;
2576 G.postRedisplay "progress"
2578 | "firstmatch" :: args :: [] ->
2579 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2580 scan args "%u %d %f %f %f %f %f %f %f %f"
2581 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2582 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2584 let y = (getpagey pageno) + truncate y0 in
2585 addnav ();
2586 gotoy y;
2587 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2589 | "match" :: args :: [] ->
2590 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2591 scan args "%u %d %f %f %f %f %f %f %f %f"
2592 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2593 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2595 state.rects1 <-
2596 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2598 | "page" :: args :: [] ->
2599 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2600 begin match state.currently with
2601 | Loading (l, gen) ->
2602 vlog "page %d took %f sec" l.pageno t;
2603 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2604 begin match state.throttle with
2605 | None ->
2606 let preloadedpages =
2607 if conf.preload
2608 then preloadlayout state.y
2609 else state.layout
2611 let evict () =
2612 let set =
2613 List.fold_left (fun s l -> IntSet.add l.pageno s)
2614 IntSet.empty preloadedpages
2616 let evictedpages =
2617 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2618 if not (IntSet.mem pageno set)
2619 then (
2620 wcmd "freepage %s" opaque;
2621 key :: accu
2623 else accu
2624 ) state.pagemap []
2626 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2628 evict ();
2629 state.currently <- Idle;
2630 if gen = state.gen
2631 then (
2632 tilepage l.pageno pageopaque state.layout;
2633 load state.layout;
2634 load preloadedpages;
2635 if pagevisible state.layout l.pageno
2636 && layoutready state.layout
2637 then G.postRedisplay "page";
2640 | Some (layout, _, _) ->
2641 state.currently <- Idle;
2642 tilepage l.pageno pageopaque layout;
2643 load state.layout
2644 end;
2646 | _ ->
2647 dolog "Inconsistent loading state";
2648 logcurrently state.currently;
2649 exit 1
2652 | "tile" :: args :: [] ->
2653 let (x, y, opaque, size, t) =
2654 scan args "%u %u %s %u %f"
2655 (fun x y p size t -> (x, y, p, size, t))
2657 begin match state.currently with
2658 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2659 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2661 unmappbo opaque;
2662 if tilew != conf.tilew || tileh != conf.tileh
2663 then (
2664 wcmd "freetile %s" opaque;
2665 state.currently <- Idle;
2666 load state.layout;
2668 else (
2669 puttileopaque l col row gen cs angle opaque size t;
2670 state.memused <- state.memused + size;
2671 state.uioh#infochanged Memused;
2672 gctiles ();
2673 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2674 opaque, size) state.tilelru;
2676 let layout =
2677 match state.throttle with
2678 | None -> state.layout
2679 | Some (layout, _, _) -> layout
2682 state.currently <- Idle;
2683 if gen = state.gen
2684 && conf.colorspace = cs
2685 && conf.angle = angle
2686 && tilevisible layout l.pageno x y
2687 then conttiling l.pageno pageopaque;
2689 begin match state.throttle with
2690 | None ->
2691 preload state.layout;
2692 if gen = state.gen
2693 && conf.colorspace = cs
2694 && conf.angle = angle
2695 && tilevisible state.layout l.pageno x y
2696 && (not !wtmode || layoutready state.layout)
2697 then G.postRedisplay "tile nothrottle";
2699 | Some (layout, y, _) ->
2700 let ready = layoutready layout in
2701 if ready
2702 then (
2703 state.y <- y;
2704 state.layout <- layout;
2705 state.throttle <- None;
2706 G.postRedisplay "throttle";
2708 else load layout;
2709 end;
2712 | _ ->
2713 dolog "Inconsistent tiling state";
2714 logcurrently state.currently;
2715 exit 1
2718 | "pdim" :: args :: [] ->
2719 let (n, w, h, _) as pdim =
2720 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2722 let pdim =
2723 match conf.fitmodel, conf.columns with
2724 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2725 | _ -> pdim
2727 state.uioh#infochanged Pdim;
2728 state.pdims <- pdim :: state.pdims
2730 | "o" :: args :: [] ->
2731 let (l, n, t, h, pos) =
2732 scan args "%u %u %d %u %n"
2733 (fun l n t h pos -> l, n, t, h, pos)
2735 let s = String.sub args pos (String.length args - pos) in
2736 let outline = (s, l, (n, float t /. float h, 0.0)) in
2737 begin match state.currently with
2738 | Outlining outlines ->
2739 state.currently <- Outlining (outline :: outlines)
2740 | Idle ->
2741 state.currently <- Outlining [outline]
2742 | currently ->
2743 dolog "invalid outlining state";
2744 logcurrently currently
2747 | "a" :: args :: [] ->
2748 let (n, l, t) =
2749 scan args "%u %d %d" (fun n l t -> n, l, t)
2751 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2753 | "info" :: args :: [] ->
2754 state.docinfo <- (1, args) :: state.docinfo
2756 | "infoend" :: [] ->
2757 state.uioh#infochanged Docinfo;
2758 state.docinfo <- List.rev state.docinfo
2760 | _ ->
2761 error "unknown cmd `%S'" cmds
2764 let onhist cb =
2765 let rc = cb.rc in
2766 let action = function
2767 | HCprev -> cbget cb ~-1
2768 | HCnext -> cbget cb 1
2769 | HCfirst -> cbget cb ~-(cb.rc)
2770 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2771 and cancel () = cb.rc <- rc
2772 in (action, cancel)
2775 let search pattern forward =
2776 match conf.columns with
2777 | Csplit _ ->
2778 showtext '!' "searching does not work properly in split columns mode"
2779 | _ ->
2780 if nonemptystr pattern
2781 then
2782 let pn, py =
2783 match state.layout with
2784 | [] -> 0, 0
2785 | l :: _ ->
2786 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2788 wcmd "search %d %d %d %d,%s\000"
2789 (btod conf.icase) pn py (btod forward) pattern;
2792 let intentry text key =
2793 let c =
2794 if key >= 32 && key < 127
2795 then Char.chr key
2796 else '\000'
2798 match c with
2799 | '0' .. '9' ->
2800 let text = addchar text c in
2801 TEcont text
2803 | _ ->
2804 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2805 TEcont text
2808 let linknentry text key =
2809 let c =
2810 if key >= 32 && key < 127
2811 then Char.chr key
2812 else '\000'
2814 match c with
2815 | 'a' .. 'z' ->
2816 let text = addchar text c in
2817 TEcont text
2819 | _ ->
2820 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2821 TEcont text
2824 let linkndone f s =
2825 if nonemptystr s
2826 then (
2827 let n =
2828 let l = String.length s in
2829 let rec loop pos n = if pos = l then n else
2830 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2831 loop (pos+1) (n*26 + m)
2832 in loop 0 0
2834 let rec loop n = function
2835 | [] -> ()
2836 | l :: rest ->
2837 match getopaque l.pageno with
2838 | None -> loop n rest
2839 | Some opaque ->
2840 let m = getlinkcount opaque in
2841 if n < m
2842 then (
2843 let under = getlink opaque n in
2844 f under
2846 else loop (n-m) rest
2848 loop n state.layout;
2852 let textentry text key =
2853 if key land 0xff00 = 0xff00
2854 then TEcont text
2855 else TEcont (text ^ toutf8 key)
2858 let reqlayout angle fitmodel =
2859 match state.throttle with
2860 | None ->
2861 if nogeomcmds state.geomcmds
2862 then state.anchor <- getanchor ();
2863 conf.angle <- angle mod 360;
2864 if conf.angle != 0
2865 then (
2866 match state.mode with
2867 | LinkNav _ -> state.mode <- View
2868 | _ -> ()
2870 conf.fitmodel <- fitmodel;
2871 invalidate "reqlayout"
2872 (fun () ->
2873 wcmd "reqlayout %d %d %d"
2874 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2876 | _ -> ()
2879 let settrim trimmargins trimfuzz =
2880 if nogeomcmds state.geomcmds
2881 then state.anchor <- getanchor ();
2882 conf.trimmargins <- trimmargins;
2883 conf.trimfuzz <- trimfuzz;
2884 let x0, y0, x1, y1 = trimfuzz in
2885 invalidate "settrim"
2886 (fun () ->
2887 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2888 flushpages ();
2891 let setzoom zoom =
2892 match state.throttle with
2893 | None ->
2894 let zoom = max 0.0001 zoom in
2895 if zoom <> conf.zoom
2896 then (
2897 state.prevzoom <- (conf.zoom, state.x);
2898 conf.zoom <- zoom;
2899 reshape state.winw state.winh;
2900 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2903 | Some (layout, y, started) ->
2904 let time =
2905 match conf.maxwait with
2906 | None -> 0.0
2907 | Some t -> t
2909 let dt = now () -. started in
2910 if dt > time
2911 then (
2912 state.y <- y;
2913 load layout;
2917 let setcolumns mode columns coverA coverB =
2918 state.prevcolumns <- Some (conf.columns, conf.zoom);
2919 if columns < 0
2920 then (
2921 if isbirdseye mode
2922 then showtext '!' "split mode doesn't work in bird's eye"
2923 else (
2924 conf.columns <- Csplit (-columns, [||]);
2925 state.x <- 0;
2926 conf.zoom <- 1.0;
2929 else (
2930 if columns < 2
2931 then (
2932 conf.columns <- Csingle [||];
2933 state.x <- 0;
2934 setzoom 1.0;
2936 else (
2937 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2938 conf.zoom <- 1.0;
2941 reshape state.winw state.winh;
2944 let enterbirdseye () =
2945 let zoom = float conf.thumbw /. float state.winw in
2946 let birdseyepageno =
2947 let cy = state.winh / 2 in
2948 let fold = function
2949 | [] -> 0
2950 | l :: rest ->
2951 let rec fold best = function
2952 | [] -> best.pageno
2953 | l :: rest ->
2954 let d = cy - (l.pagedispy + l.pagevh/2)
2955 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2956 if abs d < abs dbest
2957 then fold l rest
2958 else best.pageno
2959 in fold l rest
2961 fold state.layout
2963 state.mode <- Birdseye (
2964 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2966 conf.zoom <- zoom;
2967 conf.presentation <- false;
2968 conf.interpagespace <- 10;
2969 conf.hlinks <- false;
2970 conf.fitmodel <- FitProportional;
2971 state.x <- 0;
2972 state.mstate <- Mnone;
2973 conf.maxwait <- None;
2974 conf.columns <- (
2975 match conf.beyecolumns with
2976 | Some c ->
2977 conf.zoom <- 1.0;
2978 Cmulti ((c, 0, 0), [||])
2979 | None -> Csingle [||]
2981 Wsi.setcursor Wsi.CURSOR_INHERIT;
2982 if conf.verbose
2983 then
2984 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2985 (100.0*.zoom)
2986 else
2987 state.text <- ""
2989 reshape state.winw state.winh;
2992 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2993 state.mode <- View;
2994 conf.zoom <- c.zoom;
2995 conf.presentation <- c.presentation;
2996 conf.interpagespace <- c.interpagespace;
2997 conf.maxwait <- c.maxwait;
2998 conf.hlinks <- c.hlinks;
2999 conf.fitmodel <- c.fitmodel;
3000 conf.beyecolumns <- (
3001 match conf.columns with
3002 | Cmulti ((c, _, _), _) -> Some c
3003 | Csingle _ -> None
3004 | Csplit _ -> failwith "leaving bird's eye split mode"
3006 conf.columns <- (
3007 match c.columns with
3008 | Cmulti (c, _) -> Cmulti (c, [||])
3009 | Csingle _ -> Csingle [||]
3010 | Csplit (c, _) -> Csplit (c, [||])
3012 if conf.verbose
3013 then
3014 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3015 (100.0*.conf.zoom)
3017 reshape state.winw state.winh;
3018 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3019 state.x <- leftx;
3022 let togglebirdseye () =
3023 match state.mode with
3024 | Birdseye vals -> leavebirdseye vals true
3025 | View -> enterbirdseye ()
3026 | _ -> ()
3029 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3030 let pageno = max 0 (pageno - incr) in
3031 let rec loop = function
3032 | [] -> gotopage1 pageno 0
3033 | l :: _ when l.pageno = pageno ->
3034 if l.pagedispy >= 0 && l.pagey = 0
3035 then G.postRedisplay "upbirdseye"
3036 else gotopage1 pageno 0
3037 | _ :: rest -> loop rest
3039 loop state.layout;
3040 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3043 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3044 let pageno = min (state.pagecount - 1) (pageno + incr) in
3045 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3046 let rec loop = function
3047 | [] ->
3048 let y, h = getpageyh pageno in
3049 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3050 gotoy (clamp dy)
3051 | l :: _ when l.pageno = pageno ->
3052 if l.pagevh != l.pageh
3053 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3054 else G.postRedisplay "downbirdseye"
3055 | _ :: rest -> loop rest
3057 loop state.layout
3060 let optentry mode _ key =
3061 let btos b = if b then "on" else "off" in
3062 if key >= 32 && key < 127
3063 then
3064 let c = Char.chr key in
3065 match c with
3066 | 's' ->
3067 let ondone s =
3068 try conf.scrollstep <- int_of_string s with exc ->
3069 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3071 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3073 | 'A' ->
3074 let ondone s =
3076 conf.autoscrollstep <- int_of_string s;
3077 if state.autoscroll <> None
3078 then state.autoscroll <- Some conf.autoscrollstep
3079 with exc ->
3080 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3082 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3084 | 'C' ->
3085 let ondone s =
3087 let n, a, b = multicolumns_of_string s in
3088 setcolumns mode n a b;
3089 with exc ->
3090 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3092 TEswitch ("columns: ", "", None, textentry, ondone, true)
3094 | 'Z' ->
3095 let ondone s =
3097 let zoom = float (int_of_string s) /. 100.0 in
3098 setzoom zoom
3099 with exc ->
3100 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3102 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3104 | 't' ->
3105 let ondone s =
3107 conf.thumbw <- bound (int_of_string s) 2 4096;
3108 state.text <-
3109 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3110 begin match mode with
3111 | Birdseye beye ->
3112 leavebirdseye beye false;
3113 enterbirdseye ();
3114 | _ -> ();
3116 with exc ->
3117 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3119 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3121 | 'R' ->
3122 let ondone s =
3123 match try
3124 Some (int_of_string s)
3125 with exc ->
3126 state.text <- Printf.sprintf "bad integer `%s': %s"
3127 s (exntos exc);
3128 None
3129 with
3130 | Some angle -> reqlayout angle conf.fitmodel
3131 | None -> ()
3133 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3135 | 'i' ->
3136 conf.icase <- not conf.icase;
3137 TEdone ("case insensitive search " ^ (btos conf.icase))
3139 | 'p' ->
3140 conf.preload <- not conf.preload;
3141 gotoy state.y;
3142 TEdone ("preload " ^ (btos conf.preload))
3144 | 'v' ->
3145 conf.verbose <- not conf.verbose;
3146 TEdone ("verbose " ^ (btos conf.verbose))
3148 | 'd' ->
3149 conf.debug <- not conf.debug;
3150 TEdone ("debug " ^ (btos conf.debug))
3152 | 'h' ->
3153 conf.maxhfit <- not conf.maxhfit;
3154 state.maxy <- calcheight ();
3155 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3157 | 'c' ->
3158 conf.crophack <- not conf.crophack;
3159 TEdone ("crophack " ^ btos conf.crophack)
3161 | 'a' ->
3162 let s =
3163 match conf.maxwait with
3164 | None ->
3165 conf.maxwait <- Some infinity;
3166 "always wait for page to complete"
3167 | Some _ ->
3168 conf.maxwait <- None;
3169 "show placeholder if page is not ready"
3171 TEdone s
3173 | 'f' ->
3174 conf.underinfo <- not conf.underinfo;
3175 TEdone ("underinfo " ^ btos conf.underinfo)
3177 | 'P' ->
3178 conf.savebmarks <- not conf.savebmarks;
3179 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3181 | 'S' ->
3182 let ondone s =
3184 let pageno, py =
3185 match state.layout with
3186 | [] -> 0, 0
3187 | l :: _ ->
3188 l.pageno, l.pagey
3190 conf.interpagespace <- int_of_string s;
3191 docolumns conf.columns;
3192 state.maxy <- calcheight ();
3193 let y = getpagey pageno in
3194 gotoy (y + py)
3195 with exc ->
3196 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3198 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3200 | 'l' ->
3201 let fm =
3202 match conf.fitmodel with
3203 | FitProportional -> FitWidth
3204 | _ -> FitProportional
3206 reqlayout conf.angle fm;
3207 TEdone ("proportional display " ^ btos (fm == FitProportional))
3209 | 'T' ->
3210 settrim (not conf.trimmargins) conf.trimfuzz;
3211 TEdone ("trim margins " ^ btos conf.trimmargins)
3213 | 'I' ->
3214 conf.invert <- not conf.invert;
3215 TEdone ("invert colors " ^ btos conf.invert)
3217 | 'x' ->
3218 let ondone s =
3219 cbput state.hists.sel s;
3220 conf.selcmd <- s;
3222 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3223 textentry, ondone, true)
3225 | 'M' ->
3226 if conf.pax == None
3227 then conf.pax <- Some (ref (0.0, 0, 0))
3228 else conf.pax <- None;
3229 TEdone ("PAX " ^ btos (conf.pax != None))
3231 | _ ->
3232 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3233 TEstop
3234 else
3235 TEcont state.text
3238 class type lvsource = object
3239 method getitemcount : int
3240 method getitem : int -> (string * int)
3241 method hasaction : int -> bool
3242 method exit :
3243 uioh:uioh ->
3244 cancel:bool ->
3245 active:int ->
3246 first:int ->
3247 pan:int ->
3248 qsearch:string ->
3249 uioh option
3250 method getactive : int
3251 method getfirst : int
3252 method getqsearch : string
3253 method setqsearch : string -> unit
3254 method getpan : int
3255 end;;
3257 class virtual lvsourcebase = object
3258 val mutable m_active = 0
3259 val mutable m_first = 0
3260 val mutable m_qsearch = ""
3261 val mutable m_pan = 0
3262 method getactive = m_active
3263 method getfirst = m_first
3264 method getqsearch = m_qsearch
3265 method getpan = m_pan
3266 method setqsearch s = m_qsearch <- s
3267 end;;
3269 let withoutlastutf8 s =
3270 let len = String.length s in
3271 if len = 0
3272 then s
3273 else
3274 let rec find pos =
3275 if pos = 0
3276 then pos
3277 else
3278 let b = Char.code s.[pos] in
3279 if b land 0b11000000 = 0b11000000
3280 then pos
3281 else find (pos-1)
3283 let first =
3284 if Char.code s.[len-1] land 0x80 = 0
3285 then len-1
3286 else find (len-1)
3288 String.sub s 0 first;
3291 let textentrykeyboard
3292 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3293 let key =
3294 if key >= 0xffb0 && key <= 0xffb9
3295 then key - 0xffb0 + 48 else key
3297 let enttext te =
3298 state.mode <- Textentry (te, onleave);
3299 state.text <- "";
3300 enttext ();
3301 G.postRedisplay "textentrykeyboard enttext";
3303 let histaction cmd =
3304 match opthist with
3305 | None -> ()
3306 | Some (action, _) ->
3307 state.mode <- Textentry (
3308 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3310 G.postRedisplay "textentry histaction"
3312 match key with
3313 | 0xff08 -> (* backspace *)
3314 let s = withoutlastutf8 text in
3315 let len = String.length s in
3316 if cancelonempty && len = 0
3317 then (
3318 onleave Cancel;
3319 G.postRedisplay "textentrykeyboard after cancel";
3321 else (
3322 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3325 | 0xff0d | 0xff8d -> (* (kp) enter *)
3326 ondone text;
3327 onleave Confirm;
3328 G.postRedisplay "textentrykeyboard after confirm"
3330 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3331 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3332 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3333 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3335 | 0xff1b -> (* escape*)
3336 if emptystr text
3337 then (
3338 begin match opthist with
3339 | None -> ()
3340 | Some (_, onhistcancel) -> onhistcancel ()
3341 end;
3342 onleave Cancel;
3343 state.text <- "";
3344 G.postRedisplay "textentrykeyboard after cancel2"
3346 else (
3347 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3350 | 0xff9f | 0xffff -> () (* delete *)
3352 | _ when key != 0
3353 && key land 0xff00 != 0xff00 (* keyboard *)
3354 && key land 0xfe00 != 0xfe00 (* xkb *)
3355 && key land 0xfd00 != 0xfd00 (* 3270 *)
3357 begin match onkey text key with
3358 | TEdone text ->
3359 ondone text;
3360 onleave Confirm;
3361 G.postRedisplay "textentrykeyboard after confirm2";
3363 | TEcont text ->
3364 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3366 | TEstop ->
3367 onleave Cancel;
3368 G.postRedisplay "textentrykeyboard after cancel3"
3370 | TEswitch te ->
3371 state.mode <- Textentry (te, onleave);
3372 G.postRedisplay "textentrykeyboard switch";
3373 end;
3375 | _ ->
3376 vlog "unhandled key %s" (Wsi.keyname key)
3379 let firstof first active =
3380 if first > active || abs (first - active) > fstate.maxrows - 1
3381 then max 0 (active - (fstate.maxrows/2))
3382 else first
3385 let calcfirst first active =
3386 if active > first
3387 then
3388 let rows = active - first in
3389 if rows > fstate.maxrows then active - fstate.maxrows else first
3390 else active
3393 let scrollph y maxy =
3394 let sh = float (maxy + state.winh) /. float state.winh in
3395 let sh = float state.winh /. sh in
3396 let sh = max sh (float conf.scrollh) in
3398 let percent = float y /. float maxy in
3399 let position = (float state.winh -. sh) *. percent in
3401 let position =
3402 if position +. sh > float state.winh
3403 then float state.winh -. sh
3404 else position
3406 position, sh;
3409 let coe s = (s :> uioh);;
3411 class listview ~(source:lvsource) ~trusted ~modehash =
3412 object (self)
3413 val m_pan = source#getpan
3414 val m_first = source#getfirst
3415 val m_active = source#getactive
3416 val m_qsearch = source#getqsearch
3417 val m_prev_uioh = state.uioh
3419 method private elemunder y =
3420 let n = y / (fstate.fontsize+1) in
3421 if m_first + n < source#getitemcount
3422 then (
3423 if source#hasaction (m_first + n)
3424 then Some (m_first + n)
3425 else None
3427 else None
3429 method display =
3430 Gl.enable `blend;
3431 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3432 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3433 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3434 GlDraw.color (1., 1., 1.);
3435 Gl.enable `texture_2d;
3436 let fs = fstate.fontsize in
3437 let nfs = fs + 1 in
3438 let ww = fstate.wwidth in
3439 let tabw = 30.0*.ww in
3440 let itemcount = source#getitemcount in
3441 let rec loop row =
3442 if (row - m_first) > fstate.maxrows
3443 then ()
3444 else (
3445 if row >= 0 && row < itemcount
3446 then (
3447 let (s, level) = source#getitem row in
3448 let y = (row - m_first) * nfs in
3449 let x = 5.0 +. float (level + m_pan) *. ww in
3450 if row = m_active
3451 then (
3452 Gl.disable `texture_2d;
3453 GlDraw.polygon_mode `both `line;
3454 let alpha = if source#hasaction row then 0.9 else 0.3 in
3455 GlDraw.color (1., 1., 1.) ~alpha;
3456 GlDraw.rect (1., float (y + 1))
3457 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3458 GlDraw.polygon_mode `both `fill;
3459 GlDraw.color (1., 1., 1.);
3460 Gl.enable `texture_2d;
3463 let drawtabularstring s =
3464 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3465 if trusted
3466 then
3467 let tabpos = try String.index s '\t' with Not_found -> -1 in
3468 if tabpos > 0
3469 then
3470 let len = String.length s - tabpos - 1 in
3471 let s1 = String.sub s 0 tabpos
3472 and s2 = String.sub s (tabpos + 1) len in
3473 let nx = drawstr x s1 in
3474 let sw = nx -. x in
3475 let x = x +. (max tabw sw) in
3476 drawstr x s2
3477 else
3478 drawstr x s
3479 else
3480 drawstr x s
3482 let _ = drawtabularstring s in
3483 loop (row+1)
3487 loop m_first;
3488 Gl.disable `blend;
3489 Gl.disable `texture_2d;
3491 method updownlevel incr =
3492 let len = source#getitemcount in
3493 let curlevel =
3494 if m_active >= 0 && m_active < len
3495 then snd (source#getitem m_active)
3496 else -1
3498 let rec flow i =
3499 if i = len then i-1 else if i = -1 then 0 else
3500 let _, l = source#getitem i in
3501 if l != curlevel then i else flow (i+incr)
3503 let active = flow m_active in
3504 let first = calcfirst m_first active in
3505 G.postRedisplay "outline updownlevel";
3506 {< m_active = active; m_first = first >}
3508 method private key1 key mask =
3509 let set1 active first qsearch =
3510 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3512 let search active pattern incr =
3513 let active = if active = -1 then m_first else active in
3514 let dosearch re =
3515 let rec loop n =
3516 if n >= 0 && n < source#getitemcount
3517 then (
3518 let s, _ = source#getitem n in
3520 (try ignore (Str.search_forward re s 0); true
3521 with Not_found -> false)
3522 then Some n
3523 else loop (n + incr)
3525 else None
3527 loop active
3530 let re = Str.regexp_case_fold pattern in
3531 dosearch re
3532 with Failure s ->
3533 state.text <- s;
3534 None
3536 let itemcount = source#getitemcount in
3537 let find start incr =
3538 let rec find i =
3539 if i = -1 || i = itemcount
3540 then -1
3541 else (
3542 if source#hasaction i
3543 then i
3544 else find (i + incr)
3547 find start
3549 let set active first =
3550 let first = bound first 0 (itemcount - fstate.maxrows) in
3551 state.text <- "";
3552 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3554 let navigate incr =
3555 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3556 let active, first =
3557 let incr1 = if incr > 0 then 1 else -1 in
3558 if isvisible m_first m_active
3559 then
3560 let next =
3561 let next = m_active + incr in
3562 let next =
3563 if next < 0 || next >= itemcount
3564 then -1
3565 else find next incr1
3567 if abs (m_active - next) > fstate.maxrows
3568 then -1
3569 else next
3571 if next = -1
3572 then
3573 let first = m_first + incr in
3574 let first = bound first 0 (itemcount - fstate.maxrows) in
3575 let next =
3576 let next = m_active + incr in
3577 let next = bound next 0 (itemcount - 1) in
3578 find next ~-incr1
3580 let active =
3581 if next = -1
3582 then m_active
3583 else (
3584 if isvisible first next
3585 then next
3586 else m_active
3589 active, first
3590 else
3591 let first = min next m_first in
3592 let first =
3593 if abs (next - first) > fstate.maxrows
3594 then first + incr
3595 else first
3597 next, first
3598 else
3599 let first = m_first + incr in
3600 let first = bound first 0 (itemcount - 1) in
3601 let active =
3602 let next = m_active + incr in
3603 let next = bound next 0 (itemcount - 1) in
3604 let next = find next incr1 in
3605 let active =
3606 if next = -1 || abs (m_active - first) > fstate.maxrows
3607 then (
3608 let active = if m_active = -1 then next else m_active in
3609 active
3611 else next
3613 if isvisible first active
3614 then active
3615 else -1
3617 active, first
3619 G.postRedisplay "listview navigate";
3620 set active first;
3622 match key with
3623 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3624 let incr = if key = 0x72 then -1 else 1 in
3625 let active, first =
3626 match search (m_active + incr) m_qsearch incr with
3627 | None ->
3628 state.text <- m_qsearch ^ " [not found]";
3629 m_active, m_first
3630 | Some active ->
3631 state.text <- m_qsearch;
3632 active, firstof m_first active
3634 G.postRedisplay "listview ctrl-r/s";
3635 set1 active first m_qsearch;
3637 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3638 if m_active >= 0 && m_active < source#getitemcount
3639 then (
3640 let s, _ = source#getitem m_active in
3641 selstring s;
3643 coe self
3645 | 0xff08 -> (* backspace *)
3646 if emptystr m_qsearch
3647 then coe self
3648 else (
3649 let qsearch = withoutlastutf8 m_qsearch in
3650 if emptystr qsearch
3651 then (
3652 state.text <- "";
3653 G.postRedisplay "listview empty qsearch";
3654 set1 m_active m_first "";
3656 else
3657 let active, first =
3658 match search m_active qsearch ~-1 with
3659 | None ->
3660 state.text <- qsearch ^ " [not found]";
3661 m_active, m_first
3662 | Some active ->
3663 state.text <- qsearch;
3664 active, firstof m_first active
3666 G.postRedisplay "listview backspace qsearch";
3667 set1 active first qsearch
3670 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3671 let pattern = m_qsearch ^ toutf8 key in
3672 let active, first =
3673 match search m_active pattern 1 with
3674 | None ->
3675 state.text <- pattern ^ " [not found]";
3676 m_active, m_first
3677 | Some active ->
3678 state.text <- pattern;
3679 active, firstof m_first active
3681 G.postRedisplay "listview qsearch add";
3682 set1 active first pattern;
3684 | 0xff1b -> (* escape *)
3685 state.text <- "";
3686 if emptystr m_qsearch
3687 then (
3688 G.postRedisplay "list view escape";
3689 begin
3690 match
3691 source#exit (coe self) true m_active m_first m_pan m_qsearch
3692 with
3693 | None -> m_prev_uioh
3694 | Some uioh -> uioh
3697 else (
3698 G.postRedisplay "list view kill qsearch";
3699 source#setqsearch "";
3700 coe {< m_qsearch = "" >}
3703 | 0xff0d | 0xff8d -> (* (kp) enter *)
3704 state.text <- "";
3705 let self = {< m_qsearch = "" >} in
3706 source#setqsearch "";
3707 let opt =
3708 G.postRedisplay "listview enter";
3709 if m_active >= 0 && m_active < source#getitemcount
3710 then (
3711 source#exit (coe self) false m_active m_first m_pan "";
3713 else (
3714 source#exit (coe self) true m_active m_first m_pan "";
3717 begin match opt with
3718 | None -> m_prev_uioh
3719 | Some uioh -> uioh
3722 | 0xff9f | 0xffff -> (* (kp) delete *)
3723 coe self
3725 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3726 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3727 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3728 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3730 | 0xff53 | 0xff98 -> (* (kp) right *)
3731 state.text <- "";
3732 G.postRedisplay "listview right";
3733 coe {< m_pan = m_pan - 1 >}
3735 | 0xff51 | 0xff96 -> (* (kp) left *)
3736 state.text <- "";
3737 G.postRedisplay "listview left";
3738 coe {< m_pan = m_pan + 1 >}
3740 | 0xff50 | 0xff95 -> (* (kp) home *)
3741 let active = find 0 1 in
3742 G.postRedisplay "listview home";
3743 set active 0;
3745 | 0xff57 | 0xff9c -> (* (kp) end *)
3746 let first = max 0 (itemcount - fstate.maxrows) in
3747 let active = find (itemcount - 1) ~-1 in
3748 G.postRedisplay "listview end";
3749 set active first;
3751 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3752 coe self
3754 | _ ->
3755 dolog "listview unknown key %#x" key; coe self
3757 method key key mask =
3758 match state.mode with
3759 | Textentry te -> textentrykeyboard key mask te; coe self
3760 | _ -> self#key1 key mask
3762 method button button down x y _ =
3763 let opt =
3764 match button with
3765 | 1 when x > state.winw - conf.scrollbw ->
3766 G.postRedisplay "listview scroll";
3767 if down
3768 then
3769 let _, position, sh = self#scrollph in
3770 if y > truncate position && y < truncate (position +. sh)
3771 then (
3772 state.mstate <- Mscrolly;
3773 Some (coe self)
3775 else
3776 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3777 let first = truncate (s *. float source#getitemcount) in
3778 let first = min source#getitemcount first in
3779 Some (coe {< m_first = first; m_active = first >})
3780 else (
3781 state.mstate <- Mnone;
3782 Some (coe self);
3784 | 1 when not down ->
3785 begin match self#elemunder y with
3786 | Some n ->
3787 G.postRedisplay "listview click";
3788 source#exit
3789 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3790 | _ ->
3791 Some (coe self)
3793 | n when (n == 4 || n == 5) && not down ->
3794 let len = source#getitemcount in
3795 let first =
3796 if n = 5 && m_first + fstate.maxrows >= len
3797 then
3798 m_first
3799 else
3800 let first = m_first + (if n == 4 then -1 else 1) in
3801 bound first 0 (len - 1)
3803 G.postRedisplay "listview wheel";
3804 Some (coe {< m_first = first >})
3805 | n when (n = 6 || n = 7) && not down ->
3806 let inc = if n = 7 then -1 else 1 in
3807 G.postRedisplay "listview hwheel";
3808 Some (coe {< m_pan = m_pan + inc >})
3809 | _ ->
3810 Some (coe self)
3812 match opt with
3813 | None -> m_prev_uioh
3814 | Some uioh -> uioh
3816 method motion _ y =
3817 match state.mstate with
3818 | Mscrolly ->
3819 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3820 let first = truncate (s *. float source#getitemcount) in
3821 let first = min source#getitemcount first in
3822 G.postRedisplay "listview motion";
3823 coe {< m_first = first; m_active = first >}
3824 | _ -> coe self
3826 method pmotion x y =
3827 if x < state.winw - conf.scrollbw
3828 then
3829 let n =
3830 match self#elemunder y with
3831 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3832 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3834 let o =
3835 if n != m_active
3836 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3837 else self
3839 coe o
3840 else (
3841 Wsi.setcursor Wsi.CURSOR_INHERIT;
3842 coe self
3845 method infochanged _ = ()
3847 method scrollpw = (0, 0.0, 0.0)
3848 method scrollph =
3849 let nfs = fstate.fontsize + 1 in
3850 let y = m_first * nfs in
3851 let itemcount = source#getitemcount in
3852 let maxi = max 0 (itemcount - fstate.maxrows) in
3853 let maxy = maxi * nfs in
3854 let p, h = scrollph y maxy in
3855 conf.scrollbw, p, h
3857 method modehash = modehash
3858 method eformsgs = false
3859 end;;
3861 class outlinelistview ~source =
3862 let settext autonarrow s =
3863 if autonarrow
3864 then state.text <- "[" ^ s ^ "]"
3865 else state.text <- s
3867 object (self)
3868 inherit listview
3869 ~source:(source :> lvsource)
3870 ~trusted:false
3871 ~modehash:(findkeyhash conf "outline")
3872 as super
3874 val m_autonarrow = false
3876 method key key mask =
3877 let maxrows =
3878 if emptystr state.text
3879 then fstate.maxrows
3880 else fstate.maxrows - 2
3882 let calcfirst first active =
3883 if active > first
3884 then
3885 let rows = active - first in
3886 if rows > maxrows then active - maxrows else first
3887 else active
3889 let navigate incr =
3890 let active = m_active + incr in
3891 let active = bound active 0 (source#getitemcount - 1) in
3892 let first = calcfirst m_first active in
3893 G.postRedisplay "outline navigate";
3894 coe {< m_active = active; m_first = first >}
3896 let navscroll first =
3897 let active =
3898 let dist = m_active - first in
3899 if dist < 0
3900 then first
3901 else (
3902 if dist < maxrows
3903 then m_active
3904 else first + maxrows
3907 G.postRedisplay "outline navscroll";
3908 coe {< m_first = first; m_active = active >}
3910 let ctrl = Wsi.withctrl mask in
3911 match key with
3912 | 97 when ctrl -> (* ctrl-a *)
3913 if m_autonarrow
3914 then source#denarrow
3915 else source#narrow m_qsearch;
3916 settext (not m_autonarrow) m_qsearch;
3917 G.postRedisplay "toggle auto narrowing";
3918 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3920 | 110 when ctrl -> (* ctrl-n *)
3921 source#narrow m_qsearch;
3922 if not m_autonarrow
3923 then source#add_narrow_pattern m_qsearch;
3924 G.postRedisplay "outline ctrl-n";
3925 coe {< m_first = 0; m_active = 0 >}
3927 | 117 when ctrl -> (* ctrl-u *)
3928 source#del_narrow_pattern;
3929 let pattern = source#renarrow in
3930 G.postRedisplay "outline ctrl-u";
3931 let text =
3932 if emptystr pattern then "" else "Narrowed to " ^ pattern
3934 settext m_autonarrow text;
3935 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
3937 | 108 when ctrl -> (* ctrl-l *)
3938 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3939 G.postRedisplay "outline ctrl-l";
3940 coe {< m_first = first >}
3942 | 0xff1b -> (* escape *)
3943 let o = super#key key mask in
3944 if m_autonarrow
3945 then (
3946 if nonemptystr m_qsearch
3947 then (
3948 source#add_narrow_pattern m_qsearch;
3949 settext true "";
3954 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
3955 if nonemptystr m_qsearch
3956 then source#add_narrow_pattern m_qsearch;
3957 super#key key mask
3959 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3960 let pattern = m_qsearch ^ toutf8 key in
3961 G.postRedisplay "outlinelistview autonarrow add";
3962 source#narrow pattern;
3963 settext true pattern;
3964 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3966 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
3967 if emptystr m_qsearch
3968 then coe self
3969 else
3970 let pattern = withoutlastutf8 m_qsearch in
3971 G.postRedisplay "outlinelistview autonarrow backspace";
3972 ignore (source#renarrow);
3973 source#narrow pattern;
3974 settext true pattern;
3975 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3977 | 0xff9f | 0xffff -> (* (kp) delete *)
3978 source#remove m_active;
3979 G.postRedisplay "outline delete";
3980 let active = max 0 (m_active-1) in
3981 coe {< m_first = firstof m_first active;
3982 m_active = active >}
3984 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
3985 navscroll (max 0 (m_first - 1))
3987 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
3988 navscroll (min (source#getitemcount - 1) (m_first + 1))
3990 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3991 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3992 | 0xff55 | 0xff9a -> (* (kp) prior *)
3993 navigate ~-(fstate.maxrows)
3994 | 0xff56 | 0xff9b -> (* (kp) next *)
3995 navigate fstate.maxrows
3997 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3998 let o =
3999 if ctrl
4000 then (
4001 G.postRedisplay "outline ctrl right";
4002 {< m_pan = m_pan + 1 >}
4004 else self#updownlevel 1
4006 coe o
4008 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4009 let o =
4010 if ctrl
4011 then (
4012 G.postRedisplay "outline ctrl left";
4013 {< m_pan = m_pan - 1 >}
4015 else self#updownlevel ~-1
4017 coe o
4019 | 0xff50 | 0xff95 -> (* (kp) home *)
4020 G.postRedisplay "outline home";
4021 coe {< m_first = 0; m_active = 0 >}
4023 | 0xff57 | 0xff9c -> (* (kp) end *)
4024 let active = source#getitemcount - 1 in
4025 let first = max 0 (active - fstate.maxrows) in
4026 G.postRedisplay "outline end";
4027 coe {< m_active = active; m_first = first >}
4029 | _ -> super#key key mask
4032 let outlinesource usebookmarks =
4033 let empty = [||] in
4034 (object (self)
4035 inherit lvsourcebase
4036 val mutable m_items = empty
4037 val mutable m_orig_items = empty
4038 val mutable m_narrow_patterns = []
4039 val mutable m_hadremovals = false
4041 method getitemcount =
4042 Array.length m_items + (if m_hadremovals then 1 else 0)
4044 method getitem n =
4045 if n == Array.length m_items && m_hadremovals
4046 then
4047 ("[Confirm removal]", 0)
4048 else
4049 let s, n, _ = m_items.(n) in
4050 (s, n)
4052 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4053 ignore (uioh, first, qsearch);
4054 let confrimremoval = m_hadremovals && active = Array.length m_items in
4055 let items =
4056 if m_narrow_patterns = []
4057 then m_orig_items
4058 else m_items
4060 if not cancel
4061 then (
4062 if not confrimremoval
4063 then (
4064 let _, _, ((pageno, y, _) as anchor) = m_items.(active) in
4065 let y = getanchory
4066 (if conf.presentation then (pageno, y, 1.0) else anchor)
4068 gotoghyll y;
4069 m_items <- items;
4071 else (
4072 state.bookmarks <- Array.to_list m_items;
4073 m_orig_items <- m_items;
4076 else m_items <- items;
4077 m_pan <- pan;
4078 None
4080 method hasaction _ = true
4082 method greetmsg =
4083 if Array.length m_items != Array.length m_orig_items
4084 then
4085 let s =
4086 match m_narrow_patterns with
4087 | one :: [] -> one
4088 | many -> String.concat " --> " (List.rev many)
4090 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4091 else ""
4093 method narrow pattern =
4094 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4095 match reopt with
4096 | None -> ()
4097 | Some re ->
4098 let rec loop accu n =
4099 if n = -1
4100 then m_items <- Array.of_list accu
4101 else
4102 let (s, _, _) as o = m_items.(n) in
4103 let accu =
4104 if (try ignore (Str.search_forward re s 0); true
4105 with Not_found -> false)
4106 then o :: accu
4107 else accu
4109 loop accu (n-1)
4111 loop [] (Array.length m_items - 1)
4113 method denarrow =
4114 m_orig_items <- (
4115 if usebookmarks
4116 then Array.of_list state.bookmarks
4117 else state.outlines
4119 m_items <- m_orig_items
4121 method remove m =
4122 if usebookmarks
4123 then
4124 if m >= 0 && m < Array.length m_items
4125 then (
4126 m_hadremovals <- true;
4127 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4128 let n = if n >= m then n+1 else n in
4129 m_items.(n)
4133 method add_narrow_pattern pattern =
4134 m_narrow_patterns <- pattern :: m_narrow_patterns
4136 method del_narrow_pattern =
4137 match m_narrow_patterns with
4138 | _ :: rest -> m_narrow_patterns <- rest
4139 | [] -> ()
4141 method renarrow =
4142 self#denarrow;
4143 match m_narrow_patterns with
4144 | pattern :: [] -> self#narrow pattern; pattern
4145 | list ->
4146 List.fold_left (fun accu pattern ->
4147 self#narrow pattern;
4148 accu ^ " --> " ^ pattern) "" list
4150 method reset anchor items =
4151 m_hadremovals <- false;
4152 if m_orig_items == empty
4153 then (
4154 m_orig_items <- items;
4155 if m_narrow_patterns == []
4156 then m_items <- items;
4158 let rely = getanchory anchor in
4159 let active =
4160 let rec loop n best bestd =
4161 if n = Array.length m_items
4162 then best
4163 else
4164 let (_, _, anchor) = m_items.(n) in
4165 let orely = getanchory anchor in
4166 let d = abs (orely - rely) in
4167 if d < bestd
4168 then loop (n+1) n d
4169 else loop (n+1) best bestd
4171 loop 0 ~-1 max_int
4173 m_active <- active;
4174 m_first <- firstof m_first active
4175 end)
4178 let enterselector usebookmarks =
4179 let source = outlinesource usebookmarks in
4180 fun errmsg ->
4181 let outlines =
4182 if usebookmarks
4183 then Array.of_list state.bookmarks
4184 else state.outlines
4186 if Array.length outlines = 0
4187 then (
4188 showtext ' ' errmsg;
4190 else (
4191 state.text <- source#greetmsg;
4192 Wsi.setcursor Wsi.CURSOR_INHERIT;
4193 let anchor = getanchor () in
4194 source#reset anchor outlines;
4195 state.uioh <- coe (new outlinelistview ~source);
4196 G.postRedisplay "enter selector";
4200 let enteroutlinemode =
4201 let f = enterselector false in
4202 fun ()-> f "Document has no outline";
4205 let enterbookmarkmode =
4206 let f = enterselector true in
4207 fun () -> f "Document has no bookmarks (yet)";
4210 let color_of_string s =
4211 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4212 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4216 let color_to_string (r, g, b) =
4217 let r = truncate (r *. 256.0)
4218 and g = truncate (g *. 256.0)
4219 and b = truncate (b *. 256.0) in
4220 Printf.sprintf "%d/%d/%d" r g b
4223 let irect_of_string s =
4224 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4227 let irect_to_string (x0,y0,x1,y1) =
4228 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4231 let makecheckers () =
4232 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4233 following to say:
4234 converted by Issac Trotts. July 25, 2002 *)
4235 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4236 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4237 let id = GlTex.gen_texture () in
4238 GlTex.bind_texture `texture_2d id;
4239 GlPix.store (`unpack_alignment 1);
4240 GlTex.image2d image;
4241 List.iter (GlTex.parameter ~target:`texture_2d)
4242 [ `mag_filter `nearest; `min_filter `nearest ];
4246 let setcheckers enabled =
4247 match state.texid with
4248 | None ->
4249 if enabled then state.texid <- Some (makecheckers ())
4251 | Some texid ->
4252 if not enabled
4253 then (
4254 GlTex.delete_texture texid;
4255 state.texid <- None;
4259 let int_of_string_with_suffix s =
4260 let l = String.length s in
4261 let s1, shift =
4262 if l > 1
4263 then
4264 let suffix = Char.lowercase s.[l-1] in
4265 match suffix with
4266 | 'k' -> String.sub s 0 (l-1), 10
4267 | 'm' -> String.sub s 0 (l-1), 20
4268 | 'g' -> String.sub s 0 (l-1), 30
4269 | _ -> s, 0
4270 else s, 0
4272 let n = int_of_string s1 in
4273 let m = n lsl shift in
4274 if m < 0 || m < n
4275 then raise (Failure "value too large")
4276 else m
4279 let string_with_suffix_of_int n =
4280 if n = 0
4281 then "0"
4282 else
4283 let units = [(30, "G"); (20, "M"); (10, "K")] in
4284 let prettyint n =
4285 let rec loop s n =
4286 let h = n mod 1000 in
4287 let n = n / 1000 in
4288 if n = 0
4289 then string_of_int h ^ s
4290 else (
4291 let s = Printf.sprintf "_%03d%s" h s in
4292 loop s n
4295 loop "" n
4297 let rec find = function
4298 | [] -> prettyint n
4299 | (shift, suffix) :: rest ->
4300 if (n land ((1 lsl shift) - 1)) = 0
4301 then prettyint (n lsr shift) ^ suffix
4302 else find rest
4304 find units
4307 let defghyllscroll = (40, 8, 32);;
4308 let ghyllscroll_of_string s =
4309 let (n, a, b) as nab =
4310 if s = "default"
4311 then defghyllscroll
4312 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4314 if n <= a || n <= b || a >= b
4315 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4316 n a b;
4317 nab;
4320 let ghyllscroll_to_string ((n, a, b) as nab) =
4321 if nab = defghyllscroll
4322 then "default"
4323 else Printf.sprintf "%d,%d,%d" n a b;
4326 let describe_location () =
4327 let fn = page_of_y state.y in
4328 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4329 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4330 let percent =
4331 if maxy <= 0
4332 then 100.
4333 else (100. *. (float state.y /. float maxy))
4335 if fn = ln
4336 then
4337 Printf.sprintf "page %d of %d [%.2f%%]"
4338 (fn+1) state.pagecount percent
4339 else
4340 Printf.sprintf
4341 "pages %d-%d of %d [%.2f%%]"
4342 (fn+1) (ln+1) state.pagecount percent
4345 let setpresentationmode v =
4346 let n = page_of_y state.y in
4347 state.anchor <- (n, 0.0, 1.0);
4348 conf.presentation <- v;
4349 if conf.fitmodel = FitPage
4350 then reqlayout conf.angle conf.fitmodel;
4351 represent ();
4354 let enterinfomode =
4355 let btos b = if b then "\xe2\x88\x9a" else "" in
4356 let showextended = ref false in
4357 let leave mode = function
4358 | Confirm -> state.mode <- mode
4359 | Cancel -> state.mode <- mode in
4360 let src =
4361 (object
4362 val mutable m_first_time = true
4363 val mutable m_l = []
4364 val mutable m_a = [||]
4365 val mutable m_prev_uioh = nouioh
4366 val mutable m_prev_mode = View
4368 inherit lvsourcebase
4370 method reset prev_mode prev_uioh =
4371 m_a <- Array.of_list (List.rev m_l);
4372 m_l <- [];
4373 m_prev_mode <- prev_mode;
4374 m_prev_uioh <- prev_uioh;
4375 if m_first_time
4376 then (
4377 let rec loop n =
4378 if n >= Array.length m_a
4379 then ()
4380 else
4381 match m_a.(n) with
4382 | _, _, _, Action _ -> m_active <- n
4383 | _ -> loop (n+1)
4385 loop 0;
4386 m_first_time <- false;
4389 method int name get set =
4390 m_l <-
4391 (name, `int get, 1, Action (
4392 fun u ->
4393 let ondone s =
4394 try set (int_of_string s)
4395 with exn ->
4396 state.text <- Printf.sprintf "bad integer `%s': %s"
4397 s (exntos exn)
4399 state.text <- "";
4400 let te = name ^ ": ", "", None, intentry, ondone, true in
4401 state.mode <- Textentry (te, leave m_prev_mode);
4403 )) :: m_l
4405 method int_with_suffix name get set =
4406 m_l <-
4407 (name, `intws get, 1, Action (
4408 fun u ->
4409 let ondone s =
4410 try set (int_of_string_with_suffix s)
4411 with exn ->
4412 state.text <- Printf.sprintf "bad integer `%s': %s"
4413 s (exntos exn)
4415 state.text <- "";
4416 let te =
4417 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4419 state.mode <- Textentry (te, leave m_prev_mode);
4421 )) :: m_l
4423 method bool ?(offset=1) ?(btos=btos) name get set =
4424 m_l <-
4425 (name, `bool (btos, get), offset, Action (
4426 fun u ->
4427 let v = get () in
4428 set (not v);
4430 )) :: m_l
4432 method color name get set =
4433 m_l <-
4434 (name, `color get, 1, Action (
4435 fun u ->
4436 let invalid = (nan, nan, nan) in
4437 let ondone s =
4438 let c =
4439 try color_of_string s
4440 with exn ->
4441 state.text <- Printf.sprintf "bad color `%s': %s"
4442 s (exntos exn);
4443 invalid
4445 if c <> invalid
4446 then set c;
4448 let te = name ^ ": ", "", None, textentry, ondone, true in
4449 state.text <- color_to_string (get ());
4450 state.mode <- Textentry (te, leave m_prev_mode);
4452 )) :: m_l
4454 method string name get set =
4455 m_l <-
4456 (name, `string get, 1, Action (
4457 fun u ->
4458 let ondone s = set s in
4459 let te = name ^ ": ", "", None, textentry, ondone, true in
4460 state.mode <- Textentry (te, leave m_prev_mode);
4462 )) :: m_l
4464 method colorspace name get set =
4465 m_l <-
4466 (name, `string get, 1, Action (
4467 fun _ ->
4468 let source =
4469 (object
4470 inherit lvsourcebase
4472 initializer
4473 m_active <- CSTE.to_int conf.colorspace;
4474 m_first <- 0;
4476 method getitemcount =
4477 Array.length CSTE.names
4478 method getitem n =
4479 (CSTE.names.(n), 0)
4480 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4481 ignore (uioh, first, pan, qsearch);
4482 if not cancel then set active;
4483 None
4484 method hasaction _ = true
4485 end)
4487 state.text <- "";
4488 let modehash = findkeyhash conf "info" in
4489 coe (new listview ~source ~trusted:true ~modehash)
4490 )) :: m_l
4492 method paxmark name get set =
4493 m_l <-
4494 (name, `string get, 1, Action (
4495 fun _ ->
4496 let source =
4497 (object
4498 inherit lvsourcebase
4500 initializer
4501 m_active <- MTE.to_int conf.paxmark;
4502 m_first <- 0;
4504 method getitemcount = Array.length MTE.names
4505 method getitem n = (MTE.names.(n), 0)
4506 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4507 ignore (uioh, first, pan, qsearch);
4508 if not cancel then set active;
4509 None
4510 method hasaction _ = true
4511 end)
4513 state.text <- "";
4514 let modehash = findkeyhash conf "info" in
4515 coe (new listview ~source ~trusted:true ~modehash)
4516 )) :: m_l
4518 method fitmodel name get set =
4519 m_l <-
4520 (name, `string get, 1, Action (
4521 fun _ ->
4522 let source =
4523 (object
4524 inherit lvsourcebase
4526 initializer
4527 m_active <- FMTE.to_int conf.fitmodel;
4528 m_first <- 0;
4530 method getitemcount = Array.length FMTE.names
4531 method getitem n = (FMTE.names.(n), 0)
4532 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4533 ignore (uioh, first, pan, qsearch);
4534 if not cancel then set active;
4535 None
4536 method hasaction _ = true
4537 end)
4539 state.text <- "";
4540 let modehash = findkeyhash conf "info" in
4541 coe (new listview ~source ~trusted:true ~modehash)
4542 )) :: m_l
4544 method caption s offset =
4545 m_l <- (s, `empty, offset, Noaction) :: m_l
4547 method caption2 s f offset =
4548 m_l <- (s, `string f, offset, Noaction) :: m_l
4550 method getitemcount = Array.length m_a
4552 method getitem n =
4553 let tostr = function
4554 | `int f -> string_of_int (f ())
4555 | `intws f -> string_with_suffix_of_int (f ())
4556 | `string f -> f ()
4557 | `color f -> color_to_string (f ())
4558 | `bool (btos, f) -> btos (f ())
4559 | `empty -> ""
4561 let name, t, offset, _ = m_a.(n) in
4562 ((let s = tostr t in
4563 if nonemptystr s
4564 then Printf.sprintf "%s\t%s" name s
4565 else name),
4566 offset)
4568 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4569 let uiohopt =
4570 if not cancel
4571 then (
4572 m_qsearch <- qsearch;
4573 let uioh =
4574 match m_a.(active) with
4575 | _, _, _, Action f -> f uioh
4576 | _ -> uioh
4578 Some uioh
4580 else None
4582 m_active <- active;
4583 m_first <- first;
4584 m_pan <- pan;
4585 uiohopt
4587 method hasaction n =
4588 match m_a.(n) with
4589 | _, _, _, Action _ -> true
4590 | _ -> false
4591 end)
4593 let rec fillsrc prevmode prevuioh =
4594 let sep () = src#caption "" 0 in
4595 let colorp name get set =
4596 src#string name
4597 (fun () -> color_to_string (get ()))
4598 (fun v ->
4600 let c = color_of_string v in
4601 set c
4602 with exn ->
4603 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4606 let oldmode = state.mode in
4607 let birdseye = isbirdseye state.mode in
4609 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4611 src#bool "presentation mode"
4612 (fun () -> conf.presentation)
4613 (fun v -> setpresentationmode v);
4615 src#bool "ignore case in searches"
4616 (fun () -> conf.icase)
4617 (fun v -> conf.icase <- v);
4619 src#bool "preload"
4620 (fun () -> conf.preload)
4621 (fun v -> conf.preload <- v);
4623 src#bool "highlight links"
4624 (fun () -> conf.hlinks)
4625 (fun v -> conf.hlinks <- v);
4627 src#bool "under info"
4628 (fun () -> conf.underinfo)
4629 (fun v -> conf.underinfo <- v);
4631 src#bool "persistent bookmarks"
4632 (fun () -> conf.savebmarks)
4633 (fun v -> conf.savebmarks <- v);
4635 src#fitmodel "fit model"
4636 (fun () -> FMTE.to_string conf.fitmodel)
4637 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4639 src#bool "trim margins"
4640 (fun () -> conf.trimmargins)
4641 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4643 src#bool "persistent location"
4644 (fun () -> conf.jumpback)
4645 (fun v -> conf.jumpback <- v);
4647 sep ();
4648 src#int "inter-page space"
4649 (fun () -> conf.interpagespace)
4650 (fun n ->
4651 conf.interpagespace <- n;
4652 docolumns conf.columns;
4653 let pageno, py =
4654 match state.layout with
4655 | [] -> 0, 0
4656 | l :: _ ->
4657 l.pageno, l.pagey
4659 state.maxy <- calcheight ();
4660 let y = getpagey pageno in
4661 gotoy (y + py)
4664 src#int "page bias"
4665 (fun () -> conf.pagebias)
4666 (fun v -> conf.pagebias <- v);
4668 src#int "scroll step"
4669 (fun () -> conf.scrollstep)
4670 (fun n -> conf.scrollstep <- n);
4672 src#int "horizontal scroll step"
4673 (fun () -> conf.hscrollstep)
4674 (fun v -> conf.hscrollstep <- v);
4676 src#int "auto scroll step"
4677 (fun () ->
4678 match state.autoscroll with
4679 | Some step -> step
4680 | _ -> conf.autoscrollstep)
4681 (fun n ->
4682 if state.autoscroll <> None
4683 then state.autoscroll <- Some n;
4684 conf.autoscrollstep <- n);
4686 src#int "zoom"
4687 (fun () -> truncate (conf.zoom *. 100.))
4688 (fun v -> setzoom ((float v) /. 100.));
4690 src#int "rotation"
4691 (fun () -> conf.angle)
4692 (fun v -> reqlayout v conf.fitmodel);
4694 src#int "scroll bar width"
4695 (fun () -> conf.scrollbw)
4696 (fun v ->
4697 conf.scrollbw <- v;
4698 reshape state.winw state.winh;
4701 src#int "scroll handle height"
4702 (fun () -> conf.scrollh)
4703 (fun v -> conf.scrollh <- v;);
4705 src#int "thumbnail width"
4706 (fun () -> conf.thumbw)
4707 (fun v ->
4708 conf.thumbw <- min 4096 v;
4709 match oldmode with
4710 | Birdseye beye ->
4711 leavebirdseye beye false;
4712 enterbirdseye ()
4713 | _ -> ()
4716 let mode = state.mode in
4717 src#string "columns"
4718 (fun () ->
4719 match conf.columns with
4720 | Csingle _ -> "1"
4721 | Cmulti (multi, _) -> multicolumns_to_string multi
4722 | Csplit (count, _) -> "-" ^ string_of_int count
4724 (fun v ->
4725 let n, a, b = multicolumns_of_string v in
4726 setcolumns mode n a b);
4728 sep ();
4729 src#caption "Pixmap cache" 0;
4730 src#int_with_suffix "size (advisory)"
4731 (fun () -> conf.memlimit)
4732 (fun v -> conf.memlimit <- v);
4734 src#caption2 "used"
4735 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4736 (string_with_suffix_of_int state.memused)
4737 (Hashtbl.length state.tilemap)) 1;
4739 sep ();
4740 src#caption "Layout" 0;
4741 src#caption2 "Dimension"
4742 (fun () ->
4743 Printf.sprintf "%dx%d (virtual %dx%d)"
4744 state.winw state.winh
4745 state.w state.maxy)
4747 if conf.debug
4748 then
4749 src#caption2 "Position" (fun () ->
4750 Printf.sprintf "%dx%d" state.x state.y
4752 else
4753 src#caption2 "Position" (fun () -> describe_location ()) 1
4756 sep ();
4757 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4758 "Save these parameters as global defaults at exit"
4759 (fun () -> conf.bedefault)
4760 (fun v -> conf.bedefault <- v)
4763 sep ();
4764 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4765 src#bool ~offset:0 ~btos "Extended parameters"
4766 (fun () -> !showextended)
4767 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4768 if !showextended
4769 then (
4770 src#bool "checkers"
4771 (fun () -> conf.checkers)
4772 (fun v -> conf.checkers <- v; setcheckers v);
4773 src#bool "update cursor"
4774 (fun () -> conf.updatecurs)
4775 (fun v -> conf.updatecurs <- v);
4776 src#bool "verbose"
4777 (fun () -> conf.verbose)
4778 (fun v -> conf.verbose <- v);
4779 src#bool "invert colors"
4780 (fun () -> conf.invert)
4781 (fun v -> conf.invert <- v);
4782 src#bool "max fit"
4783 (fun () -> conf.maxhfit)
4784 (fun v -> conf.maxhfit <- v);
4785 src#bool "redirect stderr"
4786 (fun () -> conf.redirectstderr)
4787 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4788 src#bool "pax mode"
4789 (fun () -> conf.pax != None)
4790 (fun v ->
4791 if v
4792 then conf.pax <- Some (ref (now (), 0, 0))
4793 else conf.pax <- None);
4794 src#string "uri launcher"
4795 (fun () -> conf.urilauncher)
4796 (fun v -> conf.urilauncher <- v);
4797 src#string "path launcher"
4798 (fun () -> conf.pathlauncher)
4799 (fun v -> conf.pathlauncher <- v);
4800 src#string "tile size"
4801 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4802 (fun v ->
4804 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4805 conf.tilew <- max 64 w;
4806 conf.tileh <- max 64 h;
4807 flushtiles ();
4808 with exn ->
4809 state.text <- Printf.sprintf "bad tile size `%s': %s"
4810 v (exntos exn)
4812 src#int "texture count"
4813 (fun () -> conf.texcount)
4814 (fun v ->
4815 if realloctexts v
4816 then conf.texcount <- v
4817 else showtext '!' " Failed to set texture count please retry later"
4819 src#int "slice height"
4820 (fun () -> conf.sliceheight)
4821 (fun v ->
4822 conf.sliceheight <- v;
4823 wcmd "sliceh %d" conf.sliceheight;
4825 src#int "anti-aliasing level"
4826 (fun () -> conf.aalevel)
4827 (fun v ->
4828 conf.aalevel <- bound v 0 8;
4829 state.anchor <- getanchor ();
4830 opendoc state.path state.password;
4832 src#string "page scroll scaling factor"
4833 (fun () -> string_of_float conf.pgscale)
4834 (fun v ->
4836 let s = float_of_string v in
4837 conf.pgscale <- s
4838 with exn ->
4839 state.text <- Printf.sprintf
4840 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4843 src#int "ui font size"
4844 (fun () -> fstate.fontsize)
4845 (fun v -> setfontsize (bound v 5 100));
4846 src#int "hint font size"
4847 (fun () -> conf.hfsize)
4848 (fun v -> conf.hfsize <- bound v 5 100);
4849 colorp "background color"
4850 (fun () -> conf.bgcolor)
4851 (fun v -> conf.bgcolor <- v);
4852 src#bool "crop hack"
4853 (fun () -> conf.crophack)
4854 (fun v -> conf.crophack <- v);
4855 src#string "trim fuzz"
4856 (fun () -> irect_to_string conf.trimfuzz)
4857 (fun v ->
4859 conf.trimfuzz <- irect_of_string v;
4860 if conf.trimmargins
4861 then settrim true conf.trimfuzz;
4862 with exn ->
4863 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4865 src#string "throttle"
4866 (fun () ->
4867 match conf.maxwait with
4868 | None -> "show place holder if page is not ready"
4869 | Some time ->
4870 if time = infinity
4871 then "wait for page to fully render"
4872 else
4873 "wait " ^ string_of_float time
4874 ^ " seconds before showing placeholder"
4876 (fun v ->
4878 let f = float_of_string v in
4879 if f <= 0.0
4880 then conf.maxwait <- None
4881 else conf.maxwait <- Some f
4882 with exn ->
4883 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4885 src#string "ghyll scroll"
4886 (fun () ->
4887 match conf.ghyllscroll with
4888 | None -> ""
4889 | Some nab -> ghyllscroll_to_string nab
4891 (fun v ->
4893 let gs =
4894 if emptystr v
4895 then None
4896 else Some (ghyllscroll_of_string v)
4898 conf.ghyllscroll <- gs
4899 with exn ->
4900 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4902 src#string "selection command"
4903 (fun () -> conf.selcmd)
4904 (fun v -> conf.selcmd <- v);
4905 src#string "synctex command"
4906 (fun () -> conf.stcmd)
4907 (fun v -> conf.stcmd <- v);
4908 src#string "pax command"
4909 (fun () -> conf.paxcmd)
4910 (fun v -> conf.paxcmd <- v);
4911 src#colorspace "color space"
4912 (fun () -> CSTE.to_string conf.colorspace)
4913 (fun v ->
4914 conf.colorspace <- CSTE.of_int v;
4915 wcmd "cs %d" v;
4916 load state.layout;
4918 src#paxmark "pax mark method"
4919 (fun () -> MTE.to_string conf.paxmark)
4920 (fun v -> conf.paxmark <- MTE.of_int v);
4921 if pbousable ()
4922 then
4923 src#bool "use PBO"
4924 (fun () -> conf.usepbo)
4925 (fun v -> conf.usepbo <- v);
4926 src#bool "mouse wheel scrolls pages"
4927 (fun () -> conf.wheelbypage)
4928 (fun v -> conf.wheelbypage <- v);
4929 src#bool "open remote links in a new instance"
4930 (fun () -> conf.riani)
4931 (fun v -> conf.riani <- v);
4934 sep ();
4935 src#caption "Document" 0;
4936 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4937 src#caption2 "Pages"
4938 (fun () -> string_of_int state.pagecount) 1;
4939 src#caption2 "Dimensions"
4940 (fun () -> string_of_int (List.length state.pdims)) 1;
4941 if conf.trimmargins
4942 then (
4943 sep ();
4944 src#caption "Trimmed margins" 0;
4945 src#caption2 "Dimensions"
4946 (fun () -> string_of_int (List.length state.pdims)) 1;
4949 sep ();
4950 src#caption "OpenGL" 0;
4951 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4952 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4954 sep ();
4955 src#caption "Location" 0;
4956 if nonemptystr state.origin
4957 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4958 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4960 src#reset prevmode prevuioh;
4962 fun () ->
4963 state.text <- "";
4964 let prevmode = state.mode
4965 and prevuioh = state.uioh in
4966 fillsrc prevmode prevuioh;
4967 let source = (src :> lvsource) in
4968 let modehash = findkeyhash conf "info" in
4969 state.uioh <- coe (object (self)
4970 inherit listview ~source ~trusted:true ~modehash as super
4971 val mutable m_prevmemused = 0
4972 method infochanged = function
4973 | Memused ->
4974 if m_prevmemused != state.memused
4975 then (
4976 m_prevmemused <- state.memused;
4977 G.postRedisplay "memusedchanged";
4979 | Pdim -> G.postRedisplay "pdimchanged"
4980 | Docinfo -> fillsrc prevmode prevuioh
4982 method key key mask =
4983 if not (Wsi.withctrl mask)
4984 then
4985 match key with
4986 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4987 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4988 | _ -> super#key key mask
4989 else super#key key mask
4990 end);
4991 G.postRedisplay "info";
4994 let enterhelpmode =
4995 let source =
4996 (object
4997 inherit lvsourcebase
4998 method getitemcount = Array.length state.help
4999 method getitem n =
5000 let s, l, _ = state.help.(n) in
5001 (s, l)
5003 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5004 let optuioh =
5005 if not cancel
5006 then (
5007 m_qsearch <- qsearch;
5008 match state.help.(active) with
5009 | _, _, Action f -> Some (f uioh)
5010 | _ -> Some (uioh)
5012 else None
5014 m_active <- active;
5015 m_first <- first;
5016 m_pan <- pan;
5017 optuioh
5019 method hasaction n =
5020 match state.help.(n) with
5021 | _, _, Action _ -> true
5022 | _ -> false
5024 initializer
5025 m_active <- -1
5026 end)
5027 in fun () ->
5028 let modehash = findkeyhash conf "help" in
5029 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5030 G.postRedisplay "help";
5033 let entermsgsmode =
5034 let msgsource =
5035 let re = Str.regexp "[\r\n]" in
5036 (object
5037 inherit lvsourcebase
5038 val mutable m_items = [||]
5040 method getitemcount = 1 + Array.length m_items
5042 method getitem n =
5043 if n = 0
5044 then "[Clear]", 0
5045 else m_items.(n-1), 0
5047 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5048 ignore uioh;
5049 if not cancel
5050 then (
5051 if active = 0
5052 then Buffer.clear state.errmsgs;
5053 m_qsearch <- qsearch;
5055 m_active <- active;
5056 m_first <- first;
5057 m_pan <- pan;
5058 None
5060 method hasaction n =
5061 n = 0
5063 method reset =
5064 state.newerrmsgs <- false;
5065 let l = Str.split re (Buffer.contents state.errmsgs) in
5066 m_items <- Array.of_list l
5068 initializer
5069 m_active <- 0
5070 end)
5071 in fun () ->
5072 state.text <- "";
5073 msgsource#reset;
5074 let source = (msgsource :> lvsource) in
5075 let modehash = findkeyhash conf "listview" in
5076 state.uioh <- coe (object
5077 inherit listview ~source ~trusted:false ~modehash as super
5078 method display =
5079 if state.newerrmsgs
5080 then msgsource#reset;
5081 super#display
5082 end);
5083 G.postRedisplay "msgs";
5086 let quickbookmark ?title () =
5087 match state.layout with
5088 | [] -> ()
5089 | l :: _ ->
5090 let title =
5091 match title with
5092 | None ->
5093 let sec = Unix.gettimeofday () in
5094 let tm = Unix.localtime sec in
5095 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5096 (l.pageno+1)
5097 tm.Unix.tm_mday
5098 tm.Unix.tm_mon
5099 (tm.Unix.tm_year + 1900)
5100 tm.Unix.tm_hour
5101 tm.Unix.tm_min
5102 | Some title -> title
5104 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
5107 let setautoscrollspeed step goingdown =
5108 let incr = max 1 ((abs step) / 2) in
5109 let incr = if goingdown then incr else -incr in
5110 let astep = step + incr in
5111 state.autoscroll <- Some astep;
5114 let gotounder under =
5115 let getpath filename =
5116 let path =
5117 if nonemptystr filename
5118 then
5119 if Filename.is_relative filename
5120 then
5121 let dir = Filename.dirname state.path in
5122 let dir =
5123 if Filename.is_implicit dir
5124 then Filename.concat (Sys.getcwd ()) dir
5125 else dir
5127 Filename.concat dir filename
5128 else filename
5129 else ""
5131 if Sys.file_exists path
5132 then path
5133 else ""
5135 match under with
5136 | Ulinkgoto (pageno, top) ->
5137 if pageno >= 0
5138 then (
5139 addnav ();
5140 gotopage1 pageno top;
5143 | Ulinkuri s ->
5144 gotouri s
5146 | Uremote (filename, pageno) ->
5147 let path = getpath filename in
5148 if nonemptystr path
5149 then (
5150 if conf.riani
5151 then
5152 let command = !selfexec ^ " " ^ path in
5153 try popen command []
5154 with exn ->
5155 Printf.eprintf
5156 "failed to execute `%s': %s\n" command (exntos exn);
5157 flush stderr;
5158 else
5159 let anchor = getanchor () in
5160 let ranchor = state.path, state.password, anchor, state.origin in
5161 state.origin <- "";
5162 state.anchor <- (pageno, 0.0, 0.0);
5163 state.ranchors <- ranchor :: state.ranchors;
5164 opendoc path "";
5166 else showtext '!' ("Could not find " ^ filename)
5168 | Uremotedest (filename, destname) ->
5169 let path = getpath filename in
5170 if nonemptystr path
5171 then (
5172 if conf.riani
5173 then
5174 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
5175 try popen command []
5176 with exn ->
5177 Printf.eprintf
5178 "failed to execute `%s': %s\n" command (exntos exn);
5179 flush stderr;
5180 else
5181 let anchor = getanchor () in
5182 let ranchor = state.path, state.password, anchor, state.origin in
5183 state.origin <- "";
5184 state.nameddest <- destname;
5185 state.ranchors <- ranchor :: state.ranchors;
5186 opendoc path "";
5188 else showtext '!' ("Could not find " ^ filename)
5190 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5193 let canpan () =
5194 match conf.columns with
5195 | Csplit _ -> true
5196 | _ -> state.x != 0 || conf.zoom > 1.0
5199 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5201 let existsinrow pageno (columns, coverA, coverB) p =
5202 let last = ((pageno - coverA) mod columns) + columns in
5203 let rec any = function
5204 | [] -> false
5205 | l :: rest ->
5206 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5207 then p l
5208 else (
5209 if not (p l)
5210 then (if l.pageno = last then false else any rest)
5211 else true
5214 any state.layout
5217 let nextpage () =
5218 match state.layout with
5219 | [] ->
5220 let pageno = page_of_y state.y in
5221 gotoghyll (getpagey (pageno+1))
5222 | l :: rest ->
5223 match conf.columns with
5224 | Csingle _ ->
5225 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5226 then
5227 let y = clamp (pgscale state.winh) in
5228 gotoghyll y
5229 else
5230 let pageno = min (l.pageno+1) (state.pagecount-1) in
5231 gotoghyll (getpagey pageno)
5232 | Cmulti ((c, _, _) as cl, _) ->
5233 if conf.presentation
5234 && (existsinrow l.pageno cl
5235 (fun l -> l.pageh > l.pagey + l.pagevh))
5236 then
5237 let y = clamp (pgscale state.winh) in
5238 gotoghyll y
5239 else
5240 let pageno = min (l.pageno+c) (state.pagecount-1) in
5241 gotoghyll (getpagey pageno)
5242 | Csplit (n, _) ->
5243 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5244 then
5245 let pagey, pageh = getpageyh l.pageno in
5246 let pagey = pagey + pageh * l.pagecol in
5247 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5248 gotoghyll (pagey + pageh + ips)
5251 let prevpage () =
5252 match state.layout with
5253 | [] ->
5254 let pageno = page_of_y state.y in
5255 gotoghyll (getpagey (pageno-1))
5256 | l :: _ ->
5257 match conf.columns with
5258 | Csingle _ ->
5259 if conf.presentation && l.pagey != 0
5260 then
5261 gotoghyll (clamp (pgscale ~-(state.winh)))
5262 else
5263 let pageno = max 0 (l.pageno-1) in
5264 gotoghyll (getpagey pageno)
5265 | Cmulti ((c, _, coverB) as cl, _) ->
5266 if conf.presentation &&
5267 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5268 then
5269 gotoghyll (clamp (pgscale ~-(state.winh)))
5270 else
5271 let decr =
5272 if l.pageno = state.pagecount - coverB
5273 then 1
5274 else c
5276 let pageno = max 0 (l.pageno-decr) in
5277 gotoghyll (getpagey pageno)
5278 | Csplit (n, _) ->
5279 let y =
5280 if l.pagecol = 0
5281 then
5282 if l.pageno = 0
5283 then l.pagey
5284 else
5285 let pageno = max 0 (l.pageno-1) in
5286 let pagey, pageh = getpageyh pageno in
5287 pagey + (n-1)*pageh
5288 else
5289 let pagey, pageh = getpageyh l.pageno in
5290 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5292 gotoghyll y
5295 let viewkeyboard key mask =
5296 let enttext te =
5297 let mode = state.mode in
5298 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5299 state.text <- "";
5300 enttext ();
5301 G.postRedisplay "view:enttext"
5303 let ctrl = Wsi.withctrl mask in
5304 let key =
5305 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5307 match key with
5308 | 81 -> (* Q *)
5309 exit 0
5311 | 0xff63 -> (* insert *)
5312 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5313 then (
5314 state.mode <- LinkNav (Ltgendir 0);
5315 gotoy state.y;
5317 else showtext '!' "Keyboard link navigation does not work under rotation"
5319 | 0xff1b | 113 -> (* escape / q *)
5320 begin match state.mstate with
5321 | Mzoomrect _ ->
5322 state.mstate <- Mnone;
5323 Wsi.setcursor Wsi.CURSOR_INHERIT;
5324 G.postRedisplay "kill zoom rect";
5325 | _ ->
5326 begin match state.mode with
5327 | LinkNav _ ->
5328 state.mode <- View;
5329 G.postRedisplay "esc leave linknav"
5330 | _ ->
5331 match state.ranchors with
5332 | [] -> raise Quit
5333 | (path, password, anchor, origin) :: rest ->
5334 state.ranchors <- rest;
5335 state.anchor <- anchor;
5336 state.origin <- origin;
5337 state.nameddest <- "";
5338 opendoc path password
5339 end;
5340 end;
5342 | 0xff08 -> (* backspace *)
5343 gotoghyll (getnav ~-1)
5345 | 111 -> (* o *)
5346 enteroutlinemode ()
5348 | 117 -> (* u *)
5349 state.rects <- [];
5350 state.text <- "";
5351 G.postRedisplay "dehighlight";
5353 | 47 | 63 -> (* / ? *)
5354 let ondone isforw s =
5355 cbput state.hists.pat s;
5356 state.searchpattern <- s;
5357 search s isforw
5359 let s = String.create 1 in
5360 s.[0] <- Char.chr key;
5361 enttext (s, "", Some (onhist state.hists.pat),
5362 textentry, ondone (key = 47), true)
5364 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5365 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5366 setzoom (conf.zoom +. incr)
5368 | 43 | 0xffab -> (* + *)
5369 let ondone s =
5370 let n =
5371 try int_of_string s with exc ->
5372 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5373 max_int
5375 if n != max_int
5376 then (
5377 conf.pagebias <- n;
5378 state.text <- "page bias is now " ^ string_of_int n;
5381 enttext ("page bias: ", "", None, intentry, ondone, true)
5383 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5384 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5385 setzoom (max 0.01 (conf.zoom -. decr))
5387 | 45 | 0xffad -> (* - *)
5388 let ondone msg = state.text <- msg in
5389 enttext (
5390 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5391 optentry state.mode, ondone, true
5394 | 48 when ctrl -> (* ctrl-0 *)
5395 if conf.zoom = 1.0
5396 then (
5397 state.x <- 0;
5398 gotoy state.y
5400 else setzoom 1.0
5402 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5403 let cols =
5404 match conf.columns with
5405 | Csingle _ | Cmulti _ -> 1
5406 | Csplit (n, _) -> n
5408 let h = state.winh -
5409 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5411 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5412 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5413 then setzoom zoom
5415 | 51 when ctrl -> (* ctrl-3 *)
5416 let fm =
5417 match conf.fitmodel with
5418 | FitWidth -> FitProportional
5419 | FitProportional -> FitPage
5420 | FitPage -> FitWidth
5422 state.text <- "fit model: " ^ FMTE.to_string fm;
5423 reqlayout conf.angle fm
5425 | 0xffc6 -> (* f9 *)
5426 togglebirdseye ()
5428 | 57 when ctrl -> (* ctrl-9 *)
5429 togglebirdseye ()
5431 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5432 when not ctrl -> (* 0..9 *)
5433 let ondone s =
5434 let n =
5435 try int_of_string s with exc ->
5436 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5439 if n >= 0
5440 then (
5441 addnav ();
5442 cbput state.hists.pag (string_of_int n);
5443 gotopage1 (n + conf.pagebias - 1) 0;
5446 let pageentry text key =
5447 match Char.unsafe_chr key with
5448 | 'g' -> TEdone text
5449 | _ -> intentry text key
5451 let text = "x" in text.[0] <- Char.chr key;
5452 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5454 | 98 -> (* b *)
5455 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5456 reshape state.winw state.winh;
5458 | 66 -> (* B *)
5459 state.bzoom <- not state.bzoom;
5460 state.rects <- [];
5461 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5463 | 108 -> (* l *)
5464 conf.hlinks <- not conf.hlinks;
5465 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5466 G.postRedisplay "toggle highlightlinks";
5468 | 70 -> (* F *)
5469 state.glinks <- true;
5470 let mode = state.mode in
5471 state.mode <- Textentry (
5472 (":", "", None, linknentry, linkndone gotounder, false),
5473 (fun _ ->
5474 state.glinks <- false;
5475 state.mode <- mode)
5477 state.text <- "";
5478 G.postRedisplay "view:linkent(F)"
5480 | 121 -> (* y *)
5481 state.glinks <- true;
5482 let mode = state.mode in
5483 state.mode <- Textentry (
5485 ":", "", None, linknentry, linkndone (fun under ->
5486 selstring (undertext under);
5487 ), false
5489 fun _ ->
5490 state.glinks <- false;
5491 state.mode <- mode
5493 state.text <- "";
5494 G.postRedisplay "view:linkent"
5496 | 97 -> (* a *)
5497 begin match state.autoscroll with
5498 | Some step ->
5499 conf.autoscrollstep <- step;
5500 state.autoscroll <- None
5501 | None ->
5502 if conf.autoscrollstep = 0
5503 then state.autoscroll <- Some 1
5504 else state.autoscroll <- Some conf.autoscrollstep
5507 | 112 when ctrl -> (* ctrl-p *)
5508 launchpath ()
5510 | 80 -> (* P *)
5511 setpresentationmode (not conf.presentation);
5512 showtext ' ' ("presentation mode " ^
5513 if conf.presentation then "on" else "off");
5515 | 102 -> (* f *)
5516 if List.mem Wsi.Fullscreen state.winstate
5517 then Wsi.reshape conf.cwinw conf.cwinh
5518 else Wsi.fullscreen ()
5520 | 112 | 78 -> (* p|N *)
5521 search state.searchpattern false
5523 | 110 | 0xffc0 -> (* n|F3 *)
5524 search state.searchpattern true
5526 | 116 -> (* t *)
5527 begin match state.layout with
5528 | [] -> ()
5529 | l :: _ ->
5530 gotoghyll (getpagey l.pageno)
5533 | 32 -> (* space *)
5534 nextpage ()
5536 | 0xff9f | 0xffff -> (* delete *)
5537 prevpage ()
5539 | 61 -> (* = *)
5540 showtext ' ' (describe_location ());
5542 | 119 -> (* w *)
5543 begin match state.layout with
5544 | [] -> ()
5545 | l :: _ ->
5546 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5547 G.postRedisplay "w"
5550 | 39 -> (* ' *)
5551 enterbookmarkmode ()
5553 | 104 | 0xffbe -> (* h|F1 *)
5554 enterhelpmode ()
5556 | 105 -> (* i *)
5557 enterinfomode ()
5559 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5560 entermsgsmode ()
5562 | 109 -> (* m *)
5563 let ondone s =
5564 match state.layout with
5565 | l :: _ ->
5566 if nonemptystr s
5567 then
5568 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5569 | _ -> ()
5571 enttext ("bookmark: ", "", None, textentry, ondone, true)
5573 | 126 -> (* ~ *)
5574 quickbookmark ();
5575 showtext ' ' "Quick bookmark added";
5577 | 122 -> (* z *)
5578 begin match state.layout with
5579 | l :: _ ->
5580 let rect = getpdimrect l.pagedimno in
5581 let w, h =
5582 if conf.crophack
5583 then
5584 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5585 truncate (1.2 *. (rect.(3) -. rect.(0))))
5586 else
5587 (truncate (rect.(1) -. rect.(0)),
5588 truncate (rect.(3) -. rect.(0)))
5590 let w = truncate ((float w)*.conf.zoom)
5591 and h = truncate ((float h)*.conf.zoom) in
5592 if w != 0 && h != 0
5593 then (
5594 state.anchor <- getanchor ();
5595 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5597 G.postRedisplay "z";
5599 | [] -> ()
5602 | 120 -> state.roam ()
5603 | 60 | 62 -> (* < > *)
5604 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5606 | 91 | 93 -> (* [ ] *)
5607 conf.colorscale <-
5608 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5610 G.postRedisplay "brightness";
5612 | 99 when state.mode = View -> (* [alt-]c *)
5613 if Wsi.withalt mask
5614 then (
5615 if conf.zoom > 1.0
5616 then
5617 let m = (wadjsb state.winw - state.w) / 2 in
5618 state.x <- m;
5619 gotoy_and_clear_text state.y
5621 else
5622 let (c, a, b), z =
5623 match state.prevcolumns with
5624 | None -> (1, 0, 0), 1.0
5625 | Some (columns, z) ->
5626 let cab =
5627 match columns with
5628 | Csplit (c, _) -> -c, 0, 0
5629 | Cmulti ((c, a, b), _) -> c, a, b
5630 | Csingle _ -> 1, 0, 0
5632 cab, z
5634 setcolumns View c a b;
5635 setzoom z
5637 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5638 -> (* ctrl-shift- (kp) [up|down] *)
5639 let zoom, x = state.prevzoom in
5640 setzoom zoom;
5641 state.x <- x;
5643 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5644 begin match state.autoscroll with
5645 | None ->
5646 begin match state.mode with
5647 | Birdseye beye -> upbirdseye 1 beye
5648 | _ ->
5649 if ctrl
5650 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5651 else (
5652 if not (Wsi.withshift mask) && conf.presentation
5653 then prevpage ()
5654 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5657 | Some n ->
5658 setautoscrollspeed n false
5661 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5662 begin match state.autoscroll with
5663 | None ->
5664 begin match state.mode with
5665 | Birdseye beye -> downbirdseye 1 beye
5666 | _ ->
5667 if ctrl
5668 then gotoy_and_clear_text (clamp (state.winh/2))
5669 else (
5670 if not (Wsi.withshift mask) && conf.presentation
5671 then nextpage ()
5672 else gotoy_and_clear_text (clamp conf.scrollstep)
5675 | Some n ->
5676 setautoscrollspeed n true
5679 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5680 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5681 if canpan ()
5682 then
5683 let dx =
5684 if ctrl
5685 then state.winw / 2
5686 else conf.hscrollstep
5688 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5689 state.x <- panbound (state.x + dx);
5690 gotoy_and_clear_text state.y
5691 else (
5692 state.text <- "";
5693 G.postRedisplay "left/right"
5696 | 0xff55 | 0xff9a -> (* (kp) prior *)
5697 let y =
5698 if ctrl
5699 then
5700 match state.layout with
5701 | [] -> state.y
5702 | l :: _ -> state.y - l.pagey
5703 else
5704 clamp (pgscale (-state.winh))
5706 gotoghyll y
5708 | 0xff56 | 0xff9b -> (* (kp) next *)
5709 let y =
5710 if ctrl
5711 then
5712 match List.rev state.layout with
5713 | [] -> state.y
5714 | l :: _ -> getpagey l.pageno
5715 else
5716 clamp (pgscale state.winh)
5718 gotoghyll y
5720 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5721 gotoghyll 0
5722 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5723 gotoghyll (clamp state.maxy)
5725 | 0xff53 | 0xff98
5726 when Wsi.withalt mask -> (* alt-(kp) right *)
5727 gotoghyll (getnav 1)
5728 | 0xff51 | 0xff96
5729 when Wsi.withalt mask -> (* alt-(kp) left *)
5730 gotoghyll (getnav ~-1)
5732 | 114 -> (* r *)
5733 reload ()
5735 | 118 when conf.debug -> (* v *)
5736 state.rects <- [];
5737 List.iter (fun l ->
5738 match getopaque l.pageno with
5739 | None -> ()
5740 | Some opaque ->
5741 let x0, y0, x1, y1 = pagebbox opaque in
5742 let a,b = float x0, float y0 in
5743 let c,d = float x1, float y0 in
5744 let e,f = float x1, float y1 in
5745 let h,j = float x0, float y1 in
5746 let rect = (a,b,c,d,e,f,h,j) in
5747 debugrect rect;
5748 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5749 ) state.layout;
5750 G.postRedisplay "v";
5752 | _ ->
5753 vlog "huh? %s" (Wsi.keyname key)
5756 let linknavkeyboard key mask linknav =
5757 let getpage pageno =
5758 let rec loop = function
5759 | [] -> None
5760 | l :: _ when l.pageno = pageno -> Some l
5761 | _ :: rest -> loop rest
5762 in loop state.layout
5764 let doexact (pageno, n) =
5765 match getopaque pageno, getpage pageno with
5766 | Some opaque, Some l ->
5767 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5768 then
5769 let under = getlink opaque n in
5770 G.postRedisplay "link gotounder";
5771 gotounder under;
5772 state.mode <- View;
5773 else
5774 let opt, dir =
5775 match key with
5776 | 0xff50 -> (* home *)
5777 Some (findlink opaque LDfirst), -1
5779 | 0xff57 -> (* end *)
5780 Some (findlink opaque LDlast), 1
5782 | 0xff51 -> (* left *)
5783 Some (findlink opaque (LDleft n)), -1
5785 | 0xff53 -> (* right *)
5786 Some (findlink opaque (LDright n)), 1
5788 | 0xff52 -> (* up *)
5789 Some (findlink opaque (LDup n)), -1
5791 | 0xff54 -> (* down *)
5792 Some (findlink opaque (LDdown n)), 1
5794 | _ -> None, 0
5796 let pwl l dir =
5797 begin match findpwl l.pageno dir with
5798 | Pwlnotfound -> ()
5799 | Pwl pageno ->
5800 let notfound dir =
5801 state.mode <- LinkNav (Ltgendir dir);
5802 let y, h = getpageyh pageno in
5803 let y =
5804 if dir < 0
5805 then y + h - state.winh
5806 else y
5808 gotoy y
5810 begin match getopaque pageno, getpage pageno with
5811 | Some opaque, Some _ ->
5812 let link =
5813 let ld = if dir > 0 then LDfirst else LDlast in
5814 findlink opaque ld
5816 begin match link with
5817 | Lfound m ->
5818 showlinktype (getlink opaque m);
5819 state.mode <- LinkNav (Ltexact (pageno, m));
5820 G.postRedisplay "linknav jpage";
5821 | _ -> notfound dir
5822 end;
5823 | _ -> notfound dir
5824 end;
5825 end;
5827 begin match opt with
5828 | Some Lnotfound -> pwl l dir;
5829 | Some (Lfound m) ->
5830 if m = n
5831 then pwl l dir
5832 else (
5833 let _, y0, _, y1 = getlinkrect opaque m in
5834 if y0 < l.pagey
5835 then gotopage1 l.pageno y0
5836 else (
5837 let d = fstate.fontsize + 1 in
5838 if y1 - l.pagey > l.pagevh - d
5839 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5840 else G.postRedisplay "linknav";
5842 showlinktype (getlink opaque m);
5843 state.mode <- LinkNav (Ltexact (l.pageno, m));
5846 | None -> viewkeyboard key mask
5847 end;
5848 | _ -> viewkeyboard key mask
5850 if key = 0xff63
5851 then (
5852 state.mode <- View;
5853 G.postRedisplay "leave linknav"
5855 else
5856 match linknav with
5857 | Ltgendir _ -> viewkeyboard key mask
5858 | Ltexact exact -> doexact exact
5861 let keyboard key mask =
5862 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5863 then wcmd "interrupt"
5864 else state.uioh <- state.uioh#key key mask
5867 let birdseyekeyboard key mask
5868 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5869 let incr =
5870 match conf.columns with
5871 | Csingle _ -> 1
5872 | Cmulti ((c, _, _), _) -> c
5873 | Csplit _ -> failwith "bird's eye split mode"
5875 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5876 match key with
5877 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5878 let y, h = getpageyh pageno in
5879 let top = (state.winh - h) / 2 in
5880 gotoy (max 0 (y - top))
5881 | 0xff0d (* enter *)
5882 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5883 | 0xff1b -> leavebirdseye beye true (* escape *)
5884 | 0xff52 -> upbirdseye incr beye (* up *)
5885 | 0xff54 -> downbirdseye incr beye (* down *)
5886 | 0xff51 -> upbirdseye 1 beye (* left *)
5887 | 0xff53 -> downbirdseye 1 beye (* right *)
5889 | 0xff55 -> (* prior *)
5890 begin match state.layout with
5891 | l :: _ ->
5892 if l.pagey != 0
5893 then (
5894 state.mode <- Birdseye (
5895 oconf, leftx, l.pageno, hooverpageno, anchor
5897 gotopage1 l.pageno 0;
5899 else (
5900 let layout = layout (state.y-state.winh) (pgh state.layout) in
5901 match layout with
5902 | [] -> gotoy (clamp (-state.winh))
5903 | l :: _ ->
5904 state.mode <- Birdseye (
5905 oconf, leftx, l.pageno, hooverpageno, anchor
5907 gotopage1 l.pageno 0
5910 | [] -> gotoy (clamp (-state.winh))
5911 end;
5913 | 0xff56 -> (* next *)
5914 begin match List.rev state.layout with
5915 | l :: _ ->
5916 let layout = layout (state.y + (pgh state.layout)) state.winh in
5917 begin match layout with
5918 | [] ->
5919 let incr = l.pageh - l.pagevh in
5920 if incr = 0
5921 then (
5922 state.mode <-
5923 Birdseye (
5924 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5926 G.postRedisplay "birdseye pagedown";
5928 else gotoy (clamp (incr + conf.interpagespace*2));
5930 | l :: _ ->
5931 state.mode <-
5932 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5933 gotopage1 l.pageno 0;
5936 | [] -> gotoy (clamp state.winh)
5937 end;
5939 | 0xff50 -> (* home *)
5940 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5941 gotopage1 0 0
5943 | 0xff57 -> (* end *)
5944 let pageno = state.pagecount - 1 in
5945 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5946 if not (pagevisible state.layout pageno)
5947 then
5948 let h =
5949 match List.rev state.pdims with
5950 | [] -> state.winh
5951 | (_, _, h, _) :: _ -> h
5953 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5954 else G.postRedisplay "birdseye end";
5955 | _ -> viewkeyboard key mask
5958 let drawpage l =
5959 let color =
5960 match state.mode with
5961 | Textentry _ -> scalecolor 0.4
5962 | LinkNav _
5963 | View -> scalecolor 1.0
5964 | Birdseye (_, _, pageno, hooverpageno, _) ->
5965 if l.pageno = hooverpageno
5966 then scalecolor 0.9
5967 else (
5968 if l.pageno = pageno
5969 then scalecolor 1.0
5970 else scalecolor 0.8
5973 drawtiles l color;
5976 let postdrawpage l linkindexbase =
5977 match getopaque l.pageno with
5978 | Some opaque ->
5979 if tileready l l.pagex l.pagey
5980 then
5981 let x = l.pagedispx - l.pagex
5982 and y = l.pagedispy - l.pagey in
5983 let hlmask =
5984 match conf.columns with
5985 | Csingle _ | Cmulti _ ->
5986 (if conf.hlinks then 1 else 0)
5987 + (if state.glinks
5988 && not (isbirdseye state.mode) then 2 else 0)
5989 | _ -> 0
5991 let s =
5992 match state.mode with
5993 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5994 | _ -> ""
5996 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5997 else 0
5998 | _ -> 0
6001 let scrollindicator () =
6002 let sbw, ph, sh = state.uioh#scrollph in
6003 let sbh, pw, sw = state.uioh#scrollpw in
6005 GlDraw.color (0.64, 0.64, 0.64);
6006 GlDraw.rect
6007 (float (state.winw - sbw), 0.)
6008 (float state.winw, float state.winh)
6010 GlDraw.rect
6011 (0., float (state.winh - sbh))
6012 (float (wadjsb state.winw - 1), float state.winh)
6014 GlDraw.color (0.0, 0.0, 0.0);
6016 GlDraw.rect
6017 (float (state.winw - sbw), ph)
6018 (float state.winw, ph +. sh)
6020 GlDraw.rect
6021 (pw, float (state.winh - sbh))
6022 (pw +. sw, float state.winh)
6026 let showsel () =
6027 match state.mstate with
6028 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6031 | Msel ((x0, y0), (x1, y1)) ->
6032 let rec loop = function
6033 | l :: ls ->
6034 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
6035 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
6036 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
6037 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
6038 then
6039 match getopaque l.pageno with
6040 | Some opaque ->
6041 let x0, y0 = pagetranslatepoint l x0 y0 in
6042 let x1, y1 = pagetranslatepoint l x1 y1 in
6043 seltext opaque (x0, y0, x1, y1);
6044 | _ -> ()
6045 else loop ls
6046 | [] -> ()
6048 loop state.layout
6051 let showrects rects =
6052 Gl.enable `blend;
6053 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6054 GlDraw.polygon_mode `both `fill;
6055 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6056 List.iter
6057 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6058 List.iter (fun l ->
6059 if l.pageno = pageno
6060 then (
6061 let dx = float (l.pagedispx - l.pagex) in
6062 let dy = float (l.pagedispy - l.pagey) in
6063 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6064 GlDraw.begins `quads;
6066 GlDraw.vertex2 (x0+.dx, y0+.dy);
6067 GlDraw.vertex2 (x1+.dx, y1+.dy);
6068 GlDraw.vertex2 (x2+.dx, y2+.dy);
6069 GlDraw.vertex2 (x3+.dx, y3+.dy);
6071 GlDraw.ends ();
6073 ) state.layout
6074 ) rects
6076 Gl.disable `blend;
6079 let display () =
6080 GlClear.color (scalecolor2 conf.bgcolor);
6081 GlClear.clear [`color];
6082 List.iter drawpage state.layout;
6083 let rects =
6084 match state.mode with
6085 | LinkNav (Ltexact (pageno, linkno)) ->
6086 begin match getopaque pageno with
6087 | Some opaque ->
6088 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6089 (pageno, 5, (
6090 float x0, float y0,
6091 float x1, float y0,
6092 float x1, float y1,
6093 float x0, float y1)
6094 ) :: state.rects
6095 | None -> state.rects
6097 | _ -> state.rects
6099 showrects rects;
6100 let rec postloop linkindexbase = function
6101 | l :: rest ->
6102 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6103 postloop linkindexbase rest
6104 | [] -> ()
6106 showsel ();
6107 postloop 0 state.layout;
6108 state.uioh#display;
6109 begin match state.mstate with
6110 | Mzoomrect ((x0, y0), (x1, y1)) ->
6111 Gl.enable `blend;
6112 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6113 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6114 GlDraw.rect (float x0, float y0)
6115 (float x1, float y1);
6116 Gl.disable `blend;
6117 | _ -> ()
6118 end;
6119 enttext ();
6120 scrollindicator ();
6121 Wsi.swapb ();
6124 let zoomrect x y x1 y1 =
6125 let x0 = min x x1
6126 and x1 = max x x1
6127 and y0 = min y y1 in
6128 gotoy (state.y + y0);
6129 state.anchor <- getanchor ();
6130 let zoom = (float state.w) /. float (x1 - x0) in
6131 let margin =
6132 match conf.fitmodel, conf.columns with
6133 | FitPage, Csplit _ ->
6134 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6136 | _, _ ->
6137 let adjw = wadjsb state.winw in
6138 if state.w < adjw
6139 then (adjw - state.w) / 2
6140 else 0
6142 state.x <- (state.x + margin) - x0;
6143 setzoom zoom;
6144 Wsi.setcursor Wsi.CURSOR_INHERIT;
6145 state.mstate <- Mnone;
6148 let zoomblock x y =
6149 let g opaque l px py =
6150 match rectofblock opaque px py with
6151 | Some a ->
6152 let x0 = a.(0) -. 20. in
6153 let x1 = a.(1) +. 20. in
6154 let y0 = a.(2) -. 20. in
6155 let zoom = (float state.w) /. (x1 -. x0) in
6156 let pagey = getpagey l.pageno in
6157 gotoy_and_clear_text (pagey + truncate y0);
6158 state.anchor <- getanchor ();
6159 let margin = (state.w - l.pagew)/2 in
6160 state.x <- -truncate x0 - margin;
6161 setzoom zoom;
6162 None
6163 | None -> None
6165 match conf.columns with
6166 | Csplit _ ->
6167 showtext '!' "block zooming does not work properly in split columns mode"
6168 | _ -> onppundermouse g x y ()
6171 let scrollx x =
6172 let winw = wadjsb state.winw - 1 in
6173 let s = float x /. float winw in
6174 let destx = truncate (float (state.w + winw) *. s) in
6175 state.x <- winw - destx;
6176 gotoy_and_clear_text state.y;
6177 state.mstate <- Mscrollx;
6180 let scrolly y =
6181 let s = float y /. float state.winh in
6182 let desty = truncate (float (state.maxy - state.winh) *. s) in
6183 gotoy_and_clear_text desty;
6184 state.mstate <- Mscrolly;
6187 let viewmouse button down x y mask =
6188 match button with
6189 | n when (n == 4 || n == 5) && not down ->
6190 if Wsi.withctrl mask
6191 then (
6192 match state.mstate with
6193 | Mzoom (oldn, i) ->
6194 if oldn = n
6195 then (
6196 if i = 2
6197 then
6198 let incr =
6199 match n with
6200 | 5 ->
6201 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6202 | _ ->
6203 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6205 let zoom = conf.zoom -. incr in
6206 setzoom zoom;
6207 state.mstate <- Mzoom (n, 0);
6208 else
6209 state.mstate <- Mzoom (n, i+1);
6211 else state.mstate <- Mzoom (n, 0)
6213 | _ -> state.mstate <- Mzoom (n, 0)
6215 else (
6216 match state.autoscroll with
6217 | Some step -> setautoscrollspeed step (n=4)
6218 | None ->
6219 if conf.wheelbypage || conf.presentation
6220 then (
6221 if n = 4
6222 then prevpage ()
6223 else nextpage ()
6225 else
6226 let incr =
6227 if n = 4
6228 then -conf.scrollstep
6229 else conf.scrollstep
6231 let incr = incr * 2 in
6232 let y = clamp incr in
6233 gotoy_and_clear_text y
6236 | n when (n = 6 || n = 7) && not down && canpan () ->
6237 state.x <-
6238 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6239 gotoy_and_clear_text state.y
6241 | 1 when Wsi.withshift mask ->
6242 state.mstate <- Mnone;
6243 if not down
6244 then (
6245 match unproject x y with
6246 | Some (pageno, ux, uy) ->
6247 let cmd = Printf.sprintf
6248 "%s %s %d %d %d"
6249 conf.stcmd state.path pageno ux uy
6251 popen cmd []
6252 | None -> ()
6255 | 1 when Wsi.withctrl mask ->
6256 if down
6257 then (
6258 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6259 state.mstate <- Mpan (x, y)
6261 else
6262 state.mstate <- Mnone
6264 | 3 ->
6265 if down
6266 then (
6267 Wsi.setcursor Wsi.CURSOR_CYCLE;
6268 let p = (x, y) in
6269 state.mstate <- Mzoomrect (p, p)
6271 else (
6272 match state.mstate with
6273 | Mzoomrect ((x0, y0), _) ->
6274 if abs (x-x0) > 10 && abs (y - y0) > 10
6275 then zoomrect x0 y0 x y
6276 else (
6277 state.mstate <- Mnone;
6278 Wsi.setcursor Wsi.CURSOR_INHERIT;
6279 G.postRedisplay "kill accidental zoom rect";
6281 | _ ->
6282 Wsi.setcursor Wsi.CURSOR_INHERIT;
6283 state.mstate <- Mnone
6286 | 1 when x > state.winw - vscrollw () ->
6287 if down
6288 then
6289 let _, position, sh = state.uioh#scrollph in
6290 if y > truncate position && y < truncate (position +. sh)
6291 then state.mstate <- Mscrolly
6292 else scrolly y
6293 else
6294 state.mstate <- Mnone
6296 | 1 when y > state.winh - hscrollh () ->
6297 if down
6298 then
6299 let _, position, sw = state.uioh#scrollpw in
6300 if x > truncate position && x < truncate (position +. sw)
6301 then state.mstate <- Mscrollx
6302 else scrollx x
6303 else
6304 state.mstate <- Mnone
6306 | 1 when state.bzoom -> if not down then zoomblock x y
6308 | 1 ->
6309 let dest = if down then getunder x y else Unone in
6310 begin match dest with
6311 | Ulinkgoto _
6312 | Ulinkuri _
6313 | Uremote _ | Uremotedest _
6314 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6315 gotounder dest
6317 | Unone when down ->
6318 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6319 state.mstate <- Mpan (x, y);
6321 | Unone | Utext _ ->
6322 if down
6323 then (
6324 if conf.angle mod 360 = 0
6325 then (
6326 state.mstate <- Msel ((x, y), (x, y));
6327 G.postRedisplay "mouse select";
6330 else (
6331 match state.mstate with
6332 | Mnone -> ()
6334 | Mzoom _ | Mscrollx | Mscrolly ->
6335 state.mstate <- Mnone
6337 | Mzoomrect ((x0, y0), _) ->
6338 zoomrect x0 y0 x y
6340 | Mpan _ ->
6341 Wsi.setcursor Wsi.CURSOR_INHERIT;
6342 state.mstate <- Mnone
6344 | Msel ((x0, y0), (x1, y1)) ->
6345 let rec loop = function
6346 | [] -> ()
6347 | l :: rest ->
6348 let inside =
6349 let a0 = l.pagedispy in
6350 let a1 = a0 + l.pagevh in
6351 let b0 = l.pagedispx in
6352 let b1 = b0 + l.pagevw in
6353 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6354 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6356 if inside
6357 then
6358 match getopaque l.pageno with
6359 | Some opaque ->
6360 begin
6361 match Ne.pipe () with
6362 | Ne.Exn exn ->
6363 showtext '!'
6364 (Printf.sprintf
6365 "can not create sel pipe: %s"
6366 (exntos exn));
6367 | Ne.Res (r, w) ->
6368 let doclose what fd =
6369 Ne.clo fd (fun msg ->
6370 dolog "%s close failed: %s" what msg)
6373 popen conf.selcmd [r, 0; w, -1];
6374 copysel w opaque true;
6375 doclose "pipe/r" r;
6376 G.postRedisplay "copysel";
6377 with exn ->
6378 dolog "can not execute %S: %s"
6379 conf.selcmd (exntos exn);
6380 doclose "pipe/r" r;
6381 doclose "pipe/w" w;
6383 | None -> ()
6384 else loop rest
6386 loop state.layout;
6387 Wsi.setcursor Wsi.CURSOR_INHERIT;
6388 state.mstate <- Mnone;
6392 | _ -> ()
6395 let birdseyemouse button down x y mask
6396 (conf, leftx, _, hooverpageno, anchor) =
6397 match button with
6398 | 1 when down ->
6399 let rec loop = function
6400 | [] -> ()
6401 | l :: rest ->
6402 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6403 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6404 then (
6405 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6407 else loop rest
6409 loop state.layout
6410 | 3 -> ()
6411 | _ -> viewmouse button down x y mask
6414 let mouse button down x y mask =
6415 state.uioh <- state.uioh#button button down x y mask;
6418 let motion ~x ~y =
6419 state.uioh <- state.uioh#motion x y
6422 let pmotion ~x ~y =
6423 state.uioh <- state.uioh#pmotion x y;
6426 let uioh = object
6427 method display = ()
6429 method key key mask =
6430 begin match state.mode with
6431 | Textentry textentry -> textentrykeyboard key mask textentry
6432 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6433 | View -> viewkeyboard key mask
6434 | LinkNav linknav -> linknavkeyboard key mask linknav
6435 end;
6436 state.uioh
6438 method button button bstate x y mask =
6439 begin match state.mode with
6440 | LinkNav _
6441 | View -> viewmouse button bstate x y mask
6442 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6443 | Textentry _ -> ()
6444 end;
6445 state.uioh
6447 method motion x y =
6448 begin match state.mode with
6449 | Textentry _ -> ()
6450 | View | Birdseye _ | LinkNav _ ->
6451 match state.mstate with
6452 | Mzoom _ | Mnone -> ()
6454 | Mpan (x0, y0) ->
6455 let dx = x - x0
6456 and dy = y0 - y in
6457 state.mstate <- Mpan (x, y);
6458 if canpan ()
6459 then state.x <- panbound (state.x + dx);
6460 let y = clamp dy in
6461 gotoy_and_clear_text y
6463 | Msel (a, _) ->
6464 state.mstate <- Msel (a, (x, y));
6465 G.postRedisplay "motion select";
6467 | Mscrolly ->
6468 let y = min state.winh (max 0 y) in
6469 scrolly y
6471 | Mscrollx ->
6472 let x = min state.winw (max 0 x) in
6473 scrollx x
6475 | Mzoomrect (p0, _) ->
6476 state.mstate <- Mzoomrect (p0, (x, y));
6477 G.postRedisplay "motion zoomrect";
6478 end;
6479 state.uioh
6481 method pmotion x y =
6482 begin match state.mode with
6483 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6484 let rec loop = function
6485 | [] ->
6486 if hooverpageno != -1
6487 then (
6488 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6489 G.postRedisplay "pmotion birdseye no hoover";
6491 | l :: rest ->
6492 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6493 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6494 then (
6495 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6496 G.postRedisplay "pmotion birdseye hoover";
6498 else loop rest
6500 loop state.layout
6502 | Textentry _ -> ()
6504 | LinkNav _
6505 | View ->
6506 match state.mstate with
6507 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6509 | Mnone ->
6510 updateunder x y;
6511 match conf.pax with
6512 | None -> ()
6513 | Some r ->
6514 let past, _, _ = !r in
6515 let now = now () in
6516 let delta = now -. past in
6517 if delta > 0.01
6518 then paxunder x y
6519 else r := (now, x, y)
6520 end;
6521 state.uioh
6523 method infochanged _ = ()
6525 method scrollph =
6526 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6527 let p, h =
6528 if maxy = 0
6529 then 0.0, float state.winh
6530 else scrollph state.y maxy
6532 vscrollw (), p, h
6534 method scrollpw =
6535 let winw = wadjsb state.winw in
6536 let fwinw = float winw in
6537 let sw =
6538 let sw = fwinw /. float state.w in
6539 let sw = fwinw *. sw in
6540 max sw (float conf.scrollh)
6542 let position =
6543 let maxx = state.w + winw in
6544 let x = winw - state.x in
6545 let percent = float x /. float maxx in
6546 (fwinw -. sw) *. percent
6548 hscrollh (), position, sw
6550 method modehash =
6551 let modename =
6552 match state.mode with
6553 | LinkNav _ -> "links"
6554 | Textentry _ -> "textentry"
6555 | Birdseye _ -> "birdseye"
6556 | View -> "view"
6558 findkeyhash conf modename
6560 method eformsgs = true
6561 end;;
6563 module Config =
6564 struct
6565 open Parser
6567 let fontpath = ref "";;
6569 module KeyMap =
6570 Map.Make (struct type t = (int * int) let compare = compare end);;
6572 let unent s =
6573 let l = String.length s in
6574 let b = Buffer.create l in
6575 unent b s 0 l;
6576 Buffer.contents b;
6579 let home =
6580 try Sys.getenv "HOME"
6581 with exn ->
6582 prerr_endline
6583 ("Can not determine home directory location: " ^ exntos exn);
6587 let modifier_of_string = function
6588 | "alt" -> Wsi.altmask
6589 | "shift" -> Wsi.shiftmask
6590 | "ctrl" | "control" -> Wsi.ctrlmask
6591 | "meta" -> Wsi.metamask
6592 | _ -> 0
6595 let key_of_string =
6596 let r = Str.regexp "-" in
6597 fun s ->
6598 let elems = Str.full_split r s in
6599 let f n k m =
6600 let g s =
6601 let m1 = modifier_of_string s in
6602 if m1 = 0
6603 then (Wsi.namekey s, m)
6604 else (k, m lor m1)
6605 in function
6606 | Str.Delim s when n land 1 = 0 -> g s
6607 | Str.Text s -> g s
6608 | Str.Delim _ -> (k, m)
6610 let rec loop n k m = function
6611 | [] -> (k, m)
6612 | x :: xs ->
6613 let k, m = f n k m x in
6614 loop (n+1) k m xs
6616 loop 0 0 0 elems
6619 let keys_of_string =
6620 let r = Str.regexp "[ \t]" in
6621 fun s ->
6622 let elems = Str.split r s in
6623 List.map key_of_string elems
6626 let copykeyhashes c =
6627 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6630 let config_of c attrs =
6631 let apply c k v =
6633 match k with
6634 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6635 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6636 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6637 | "preload" -> { c with preload = bool_of_string v }
6638 | "page-bias" -> { c with pagebias = int_of_string v }
6639 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6640 | "horizontal-scroll-step" ->
6641 { c with hscrollstep = max (int_of_string v) 1 }
6642 | "auto-scroll-step" ->
6643 { c with autoscrollstep = max 0 (int_of_string v) }
6644 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6645 | "crop-hack" -> { c with crophack = bool_of_string v }
6646 | "throttle" ->
6647 let mw =
6648 match String.lowercase v with
6649 | "true" -> Some infinity
6650 | "false" -> None
6651 | f -> Some (float_of_string f)
6653 { c with maxwait = mw}
6654 | "highlight-links" -> { c with hlinks = bool_of_string v }
6655 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6656 | "vertical-margin" ->
6657 { c with interpagespace = max 0 (int_of_string v) }
6658 | "zoom" ->
6659 let zoom = float_of_string v /. 100. in
6660 let zoom = max zoom 0.0 in
6661 { c with zoom = zoom }
6662 | "presentation" -> { c with presentation = bool_of_string v }
6663 | "rotation-angle" -> { c with angle = int_of_string v }
6664 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6665 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6666 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6667 | "proportional-display" ->
6668 let fm =
6669 if bool_of_string v
6670 then FitProportional
6671 else FitWidth
6673 { c with fitmodel = fm }
6674 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6675 | "pixmap-cache-size" ->
6676 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6677 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6678 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6679 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6680 | "persistent-location" -> { c with jumpback = bool_of_string v }
6681 | "background-color" -> { c with bgcolor = color_of_string v }
6682 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6683 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6684 | "mupdf-store-size" ->
6685 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6686 | "checkers" -> { c with checkers = bool_of_string v }
6687 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6688 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6689 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6690 | "uri-launcher" -> { c with urilauncher = unent v }
6691 | "path-launcher" -> { c with pathlauncher = unent v }
6692 | "color-space" -> { c with colorspace = CSTE.of_string v }
6693 | "invert-colors" -> { c with invert = bool_of_string v }
6694 | "brightness" -> { c with colorscale = float_of_string v }
6695 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6696 | "ghyllscroll" ->
6697 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6698 | "columns" ->
6699 let (n, _, _) as nab = multicolumns_of_string v in
6700 if n < 0
6701 then { c with columns = Csplit (-n, [||]) }
6702 else { c with columns = Cmulti (nab, [||]) }
6703 | "birds-eye-columns" ->
6704 { c with beyecolumns = Some (max (int_of_string v) 2) }
6705 | "selection-command" -> { c with selcmd = unent v }
6706 | "synctex-command" -> { c with stcmd = unent v }
6707 | "pax-command" -> { c with paxcmd = unent v }
6708 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6709 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6710 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6711 | "use-pbo" -> { c with usepbo = bool_of_string v }
6712 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6713 | "horizontal-scrollbar-visible" ->
6714 let b =
6715 if bool_of_string v
6716 then c.scrollb lor scrollbhv
6717 else c.scrollb land (lnot scrollbhv)
6719 { c with scrollb = b }
6720 | "vertical-scrollbar-visible" ->
6721 let b =
6722 if bool_of_string v
6723 then c.scrollb lor scrollbvv
6724 else c.scrollb land (lnot scrollbvv)
6726 { c with scrollb = b }
6727 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6728 | "point-and-x" ->
6729 { c with pax =
6730 if bool_of_string v
6731 then Some (ref (0.0, 0, 0))
6732 else None }
6733 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6734 | _ -> c
6735 with exn ->
6736 prerr_endline ("Error processing attribute (`" ^
6737 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6740 let rec fold c = function
6741 | [] -> c
6742 | (k, v) :: rest ->
6743 let c = apply c k v in
6744 fold c rest
6746 fold { c with keyhashes = copykeyhashes c } attrs;
6749 let fromstring f pos n v d =
6750 try f v
6751 with exn ->
6752 dolog "Error processing attribute (%S=%S) at %d\n%s"
6753 n v pos (exntos exn)
6758 let bookmark_of attrs =
6759 let rec fold title page rely visy = function
6760 | ("title", v) :: rest -> fold v page rely visy rest
6761 | ("page", v) :: rest -> fold title v rely visy rest
6762 | ("rely", v) :: rest -> fold title page v visy rest
6763 | ("visy", v) :: rest -> fold title page rely v rest
6764 | _ :: rest -> fold title page rely visy rest
6765 | [] -> title, page, rely, visy
6767 fold "invalid" "0" "0" "0" attrs
6770 let doc_of attrs =
6771 let rec fold path page rely pan visy = function
6772 | ("path", v) :: rest -> fold v page rely pan visy rest
6773 | ("page", v) :: rest -> fold path v rely pan visy rest
6774 | ("rely", v) :: rest -> fold path page v pan visy rest
6775 | ("pan", v) :: rest -> fold path page rely v visy rest
6776 | ("visy", v) :: rest -> fold path page rely pan v rest
6777 | _ :: rest -> fold path page rely pan visy rest
6778 | [] -> path, page, rely, pan, visy
6780 fold "" "0" "0" "0" "0" attrs
6783 let map_of attrs =
6784 let rec fold rs ls = function
6785 | ("out", v) :: rest -> fold v ls rest
6786 | ("in", v) :: rest -> fold rs v rest
6787 | _ :: rest -> fold ls rs rest
6788 | [] -> ls, rs
6790 fold "" "" attrs
6793 let setconf dst src =
6794 dst.scrollbw <- src.scrollbw;
6795 dst.scrollh <- src.scrollh;
6796 dst.icase <- src.icase;
6797 dst.preload <- src.preload;
6798 dst.pagebias <- src.pagebias;
6799 dst.verbose <- src.verbose;
6800 dst.scrollstep <- src.scrollstep;
6801 dst.maxhfit <- src.maxhfit;
6802 dst.crophack <- src.crophack;
6803 dst.autoscrollstep <- src.autoscrollstep;
6804 dst.maxwait <- src.maxwait;
6805 dst.hlinks <- src.hlinks;
6806 dst.underinfo <- src.underinfo;
6807 dst.interpagespace <- src.interpagespace;
6808 dst.zoom <- src.zoom;
6809 dst.presentation <- src.presentation;
6810 dst.angle <- src.angle;
6811 dst.cwinw <- src.cwinw;
6812 dst.cwinh <- src.cwinh;
6813 dst.savebmarks <- src.savebmarks;
6814 dst.memlimit <- src.memlimit;
6815 dst.fitmodel <- src.fitmodel;
6816 dst.texcount <- src.texcount;
6817 dst.sliceheight <- src.sliceheight;
6818 dst.thumbw <- src.thumbw;
6819 dst.jumpback <- src.jumpback;
6820 dst.bgcolor <- src.bgcolor;
6821 dst.tilew <- src.tilew;
6822 dst.tileh <- src.tileh;
6823 dst.mustoresize <- src.mustoresize;
6824 dst.checkers <- src.checkers;
6825 dst.aalevel <- src.aalevel;
6826 dst.trimmargins <- src.trimmargins;
6827 dst.trimfuzz <- src.trimfuzz;
6828 dst.urilauncher <- src.urilauncher;
6829 dst.colorspace <- src.colorspace;
6830 dst.invert <- src.invert;
6831 dst.colorscale <- src.colorscale;
6832 dst.redirectstderr <- src.redirectstderr;
6833 dst.ghyllscroll <- src.ghyllscroll;
6834 dst.columns <- src.columns;
6835 dst.beyecolumns <- src.beyecolumns;
6836 dst.selcmd <- src.selcmd;
6837 dst.updatecurs <- src.updatecurs;
6838 dst.pathlauncher <- src.pathlauncher;
6839 dst.keyhashes <- copykeyhashes src;
6840 dst.hfsize <- src.hfsize;
6841 dst.hscrollstep <- src.hscrollstep;
6842 dst.pgscale <- src.pgscale;
6843 dst.usepbo <- src.usepbo;
6844 dst.wheelbypage <- src.wheelbypage;
6845 dst.stcmd <- src.stcmd;
6846 dst.paxcmd <- src.paxcmd;
6847 dst.scrollb <- src.scrollb;
6848 dst.riani <- src.riani;
6849 dst.paxmark <- src.paxmark;
6850 dst.pax <-
6851 if src.pax = None
6852 then None
6853 else Some ((ref (0.0, 0, 0)));
6856 let get s =
6857 let h = Hashtbl.create 10 in
6858 let dc = { defconf with angle = defconf.angle } in
6859 let rec toplevel v t spos _ =
6860 match t with
6861 | Vdata | Vcdata | Vend -> v
6862 | Vopen ("llppconfig", _, closed) ->
6863 if closed
6864 then v
6865 else { v with f = llppconfig }
6866 | Vopen _ ->
6867 error "unexpected subelement at top level" s spos
6868 | Vclose _ -> error "unexpected close at top level" s spos
6870 and llppconfig v t spos _ =
6871 match t with
6872 | Vdata | Vcdata -> v
6873 | Vend -> error "unexpected end of input in llppconfig" s spos
6874 | Vopen ("defaults", attrs, closed) ->
6875 let c = config_of dc attrs in
6876 setconf dc c;
6877 if closed
6878 then v
6879 else { v with f = defaults }
6881 | Vopen ("ui-font", attrs, closed) ->
6882 let rec getsize size = function
6883 | [] -> size
6884 | ("size", v) :: rest ->
6885 let size =
6886 fromstring int_of_string spos "size" v fstate.fontsize in
6887 getsize size rest
6888 | l -> getsize size l
6890 fstate.fontsize <- getsize fstate.fontsize attrs;
6891 if closed
6892 then v
6893 else { v with f = uifont (Buffer.create 10) }
6895 | Vopen ("doc", attrs, closed) ->
6896 let pathent, spage, srely, span, svisy = doc_of attrs in
6897 let path = unent pathent
6898 and pageno = fromstring int_of_string spos "page" spage 0
6899 and rely = fromstring float_of_string spos "rely" srely 0.0
6900 and pan = fromstring int_of_string spos "pan" span 0
6901 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6902 let c = config_of dc attrs in
6903 let anchor = (pageno, rely, visy) in
6904 if closed
6905 then (Hashtbl.add h path (c, [], pan, anchor); v)
6906 else { v with f = doc path pan anchor c [] }
6908 | Vopen _ ->
6909 error "unexpected subelement in llppconfig" s spos
6911 | Vclose "llppconfig" -> { v with f = toplevel }
6912 | Vclose _ -> error "unexpected close in llppconfig" s spos
6914 and defaults v t spos _ =
6915 match t with
6916 | Vdata | Vcdata -> v
6917 | Vend -> error "unexpected end of input in defaults" s spos
6918 | Vopen ("keymap", attrs, closed) ->
6919 let modename =
6920 try List.assoc "mode" attrs
6921 with Not_found -> "global" in
6922 if closed
6923 then v
6924 else
6925 let ret keymap =
6926 let h = findkeyhash dc modename in
6927 KeyMap.iter (Hashtbl.replace h) keymap;
6928 defaults
6930 { v with f = pkeymap ret KeyMap.empty }
6932 | Vopen (_, _, _) ->
6933 error "unexpected subelement in defaults" s spos
6935 | Vclose "defaults" ->
6936 { v with f = llppconfig }
6938 | Vclose _ -> error "unexpected close in defaults" s spos
6940 and uifont b v t spos epos =
6941 match t with
6942 | Vdata | Vcdata ->
6943 Buffer.add_substring b s spos (epos - spos);
6945 | Vopen (_, _, _) ->
6946 error "unexpected subelement in ui-font" s spos
6947 | Vclose "ui-font" ->
6948 if emptystr !fontpath
6949 then fontpath := Buffer.contents b;
6950 { v with f = llppconfig }
6951 | Vclose _ -> error "unexpected close in ui-font" s spos
6952 | Vend -> error "unexpected end of input in ui-font" s spos
6954 and doc path pan anchor c bookmarks v t spos _ =
6955 match t with
6956 | Vdata | Vcdata -> v
6957 | Vend -> error "unexpected end of input in doc" s spos
6958 | Vopen ("bookmarks", _, closed) ->
6959 if closed
6960 then v
6961 else { v with f = pbookmarks path pan anchor c bookmarks }
6963 | Vopen ("keymap", attrs, closed) ->
6964 let modename =
6965 try List.assoc "mode" attrs
6966 with Not_found -> "global"
6968 if closed
6969 then v
6970 else
6971 let ret keymap =
6972 let h = findkeyhash c modename in
6973 KeyMap.iter (Hashtbl.replace h) keymap;
6974 doc path pan anchor c bookmarks
6976 { v with f = pkeymap ret KeyMap.empty }
6978 | Vopen (_, _, _) ->
6979 error "unexpected subelement in doc" s spos
6981 | Vclose "doc" ->
6982 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6983 { v with f = llppconfig }
6985 | Vclose _ -> error "unexpected close in doc" s spos
6987 and pkeymap ret keymap v t spos _ =
6988 match t with
6989 | Vdata | Vcdata -> v
6990 | Vend -> error "unexpected end of input in keymap" s spos
6991 | Vopen ("map", attrs, closed) ->
6992 let r, l = map_of attrs in
6993 let kss = fromstring keys_of_string spos "in" r [] in
6994 let lss = fromstring keys_of_string spos "out" l [] in
6995 let keymap =
6996 match kss with
6997 | [] -> keymap
6998 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6999 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7001 if closed
7002 then { v with f = pkeymap ret keymap }
7003 else
7004 let f () = v in
7005 { v with f = skip "map" f }
7007 | Vopen _ ->
7008 error "unexpected subelement in keymap" s spos
7010 | Vclose "keymap" ->
7011 { v with f = ret keymap }
7013 | Vclose _ -> error "unexpected close in keymap" s spos
7015 and pbookmarks path pan anchor c bookmarks v t spos _ =
7016 match t with
7017 | Vdata | Vcdata -> v
7018 | Vend -> error "unexpected end of input in bookmarks" s spos
7019 | Vopen ("item", attrs, closed) ->
7020 let titleent, spage, srely, svisy = bookmark_of attrs in
7021 let page = fromstring int_of_string spos "page" spage 0
7022 and rely = fromstring float_of_string spos "rely" srely 0.0
7023 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7024 let bookmarks =
7025 (unent titleent, 0, (page, rely, visy)) :: bookmarks
7027 if closed
7028 then { v with f = pbookmarks path pan anchor c bookmarks }
7029 else
7030 let f () = v in
7031 { v with f = skip "item" f }
7033 | Vopen _ ->
7034 error "unexpected subelement in bookmarks" s spos
7036 | Vclose "bookmarks" ->
7037 { v with f = doc path pan anchor c bookmarks }
7039 | Vclose _ -> error "unexpected close in bookmarks" s spos
7041 and skip tag f v t spos _ =
7042 match t with
7043 | Vdata | Vcdata -> v
7044 | Vend ->
7045 error ("unexpected end of input in skipped " ^ tag) s spos
7046 | Vopen (tag', _, closed) ->
7047 if closed
7048 then v
7049 else
7050 let f' () = { v with f = skip tag f } in
7051 { v with f = skip tag' f' }
7052 | Vclose ctag ->
7053 if tag = ctag
7054 then f ()
7055 else error ("unexpected close in skipped " ^ tag) s spos
7058 parse { f = toplevel; accu = () } s;
7059 h, dc;
7062 let do_load f ic =
7064 let len = in_channel_length ic in
7065 let s = String.create len in
7066 really_input ic s 0 len;
7067 f s;
7068 with
7069 | Parse_error (msg, s, pos) ->
7070 let subs = subs s pos in
7071 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7073 | exn ->
7074 failwith ("config load error: " ^ exntos exn)
7077 let defconfpath =
7078 let dir =
7080 let dir = Filename.concat home ".config" in
7081 if Sys.is_directory dir then dir else home
7082 with _ -> home
7084 Filename.concat dir "llpp.conf"
7087 let confpath = ref defconfpath;;
7089 let load1 f =
7090 if Sys.file_exists !confpath
7091 then
7092 match
7093 (try Some (open_in_bin !confpath)
7094 with exn ->
7095 prerr_endline
7096 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7097 exntos exn);
7098 None
7100 with
7101 | Some ic ->
7102 let success =
7104 f (do_load get ic)
7105 with exn ->
7106 prerr_endline
7107 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7108 exntos exn);
7109 false
7111 close_in ic;
7112 success
7114 | None -> false
7115 else
7116 f (Hashtbl.create 0, defconf)
7119 let load () =
7120 let f (h, dc) =
7121 let pc, pb, px, pa =
7123 let key =
7124 if emptystr state.origin
7125 then state.path
7126 else state.origin
7128 Hashtbl.find h (Filename.basename key)
7129 with Not_found -> dc, [], 0, emptyanchor
7131 setconf defconf dc;
7132 setconf conf pc;
7133 state.bookmarks <- pb;
7134 state.x <- px;
7135 if conf.jumpback
7136 then state.anchor <- pa;
7137 cbput state.hists.nav pa;
7138 true
7140 load1 f
7143 let add_attrs bb always dc c =
7144 let ob s a b =
7145 if always || a != b
7146 then Printf.bprintf bb "\n %s='%b'" s a
7147 and op s a b =
7148 if always || a <> b
7149 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7150 and oi s a b =
7151 if always || a != b
7152 then Printf.bprintf bb "\n %s='%d'" s a
7153 and oI s a b =
7154 if always || a != b
7155 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7156 and oz s a b =
7157 if always || a <> b
7158 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7159 and oF s a b =
7160 if always || a <> b
7161 then Printf.bprintf bb "\n %s='%f'" s a
7162 and oc s a b =
7163 if always || a <> b
7164 then
7165 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7166 and oC s a b =
7167 if always || a <> b
7168 then
7169 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7170 and oR s a b =
7171 if always || a <> b
7172 then
7173 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7174 and os s a b =
7175 if always || a <> b
7176 then
7177 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7178 and og s a b =
7179 if always || a <> b
7180 then
7181 match a with
7182 | None -> ()
7183 | Some (_N, _A, _B) ->
7184 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7185 and oW s a b =
7186 if always || a <> b
7187 then
7188 let v =
7189 match a with
7190 | None -> "false"
7191 | Some f ->
7192 if f = infinity
7193 then "true"
7194 else string_of_float f
7196 Printf.bprintf bb "\n %s='%s'" s v
7197 and oco s a b =
7198 if always || a <> b
7199 then
7200 match a with
7201 | Cmulti ((n, a, b), _) when n > 1 ->
7202 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7203 | Csplit (n, _) when n > 1 ->
7204 Printf.bprintf bb "\n %s='%d'" s ~-n
7205 | _ -> ()
7206 and obeco s a b =
7207 if always || a <> b
7208 then
7209 match a with
7210 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7211 | _ -> ()
7212 and oFm s a b =
7213 if always || a <> b
7214 then
7215 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7216 and oSv s a b m =
7217 if always || a <> b
7218 then
7219 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7220 and oPm s a b =
7221 if always || a <> b
7222 then
7223 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7225 oi "width" c.cwinw dc.cwinw;
7226 oi "height" c.cwinh dc.cwinh;
7227 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7228 oi "scroll-handle-height" c.scrollh dc.scrollh;
7229 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7230 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7231 ob "case-insensitive-search" c.icase dc.icase;
7232 ob "preload" c.preload dc.preload;
7233 oi "page-bias" c.pagebias dc.pagebias;
7234 oi "scroll-step" c.scrollstep dc.scrollstep;
7235 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7236 ob "max-height-fit" c.maxhfit dc.maxhfit;
7237 ob "crop-hack" c.crophack dc.crophack;
7238 oW "throttle" c.maxwait dc.maxwait;
7239 ob "highlight-links" c.hlinks dc.hlinks;
7240 ob "under-cursor-info" c.underinfo dc.underinfo;
7241 oi "vertical-margin" c.interpagespace dc.interpagespace;
7242 oz "zoom" c.zoom dc.zoom;
7243 ob "presentation" c.presentation dc.presentation;
7244 oi "rotation-angle" c.angle dc.angle;
7245 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7246 oFm "fit-model" c.fitmodel dc.fitmodel;
7247 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7248 oi "tex-count" c.texcount dc.texcount;
7249 oi "slice-height" c.sliceheight dc.sliceheight;
7250 oi "thumbnail-width" c.thumbw dc.thumbw;
7251 ob "persistent-location" c.jumpback dc.jumpback;
7252 oc "background-color" c.bgcolor dc.bgcolor;
7253 oi "tile-width" c.tilew dc.tilew;
7254 oi "tile-height" c.tileh dc.tileh;
7255 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7256 ob "checkers" c.checkers dc.checkers;
7257 oi "aalevel" c.aalevel dc.aalevel;
7258 ob "trim-margins" c.trimmargins dc.trimmargins;
7259 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7260 os "uri-launcher" c.urilauncher dc.urilauncher;
7261 os "path-launcher" c.pathlauncher dc.pathlauncher;
7262 oC "color-space" c.colorspace dc.colorspace;
7263 ob "invert-colors" c.invert dc.invert;
7264 oF "brightness" c.colorscale dc.colorscale;
7265 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7266 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7267 oco "columns" c.columns dc.columns;
7268 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7269 os "selection-command" c.selcmd dc.selcmd;
7270 os "synctex-command" c.stcmd dc.stcmd;
7271 os "pax-command" c.paxcmd dc.paxcmd;
7272 ob "update-cursor" c.updatecurs dc.updatecurs;
7273 oi "hint-font-size" c.hfsize dc.hfsize;
7274 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7275 oF "page-scroll-scale" c.pgscale dc.pgscale;
7276 ob "use-pbo" c.usepbo dc.usepbo;
7277 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7278 ob "remote-in-a-new-instance" c.riani dc.riani;
7279 op "point-and-x" c.pax dc.pax;
7280 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7283 let keymapsbuf always dc c =
7284 let bb = Buffer.create 16 in
7285 let rec loop = function
7286 | [] -> ()
7287 | (modename, h) :: rest ->
7288 let dh = findkeyhash dc modename in
7289 if always || h <> dh
7290 then (
7291 if Hashtbl.length h > 0
7292 then (
7293 if Buffer.length bb > 0
7294 then Buffer.add_char bb '\n';
7295 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7296 Hashtbl.iter (fun i o ->
7297 let isdifferent = always ||
7299 let dO = Hashtbl.find dh i in
7300 dO <> o
7301 with Not_found -> true
7303 if isdifferent
7304 then
7305 let addkm (k, m) =
7306 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7307 if Wsi.withalt m then Buffer.add_string bb "alt-";
7308 if Wsi.withshift m then Buffer.add_string bb "shift-";
7309 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7310 Buffer.add_string bb (Wsi.keyname k);
7312 let addkms l =
7313 let rec loop = function
7314 | [] -> ()
7315 | km :: [] -> addkm km
7316 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7318 loop l
7320 Buffer.add_string bb "<map in='";
7321 addkm i;
7322 match o with
7323 | KMinsrt km ->
7324 Buffer.add_string bb "' out='";
7325 addkm km;
7326 Buffer.add_string bb "'/>\n"
7328 | KMinsrl kms ->
7329 Buffer.add_string bb "' out='";
7330 addkms kms;
7331 Buffer.add_string bb "'/>\n"
7333 | KMmulti (ins, kms) ->
7334 Buffer.add_char bb ' ';
7335 addkms ins;
7336 Buffer.add_string bb "' out='";
7337 addkms kms;
7338 Buffer.add_string bb "'/>\n"
7339 ) h;
7340 Buffer.add_string bb "</keymap>";
7343 loop rest
7345 loop c.keyhashes;
7349 let save () =
7350 let uifontsize = fstate.fontsize in
7351 let bb = Buffer.create 32768 in
7352 let relx = float state.x /. float state.winw in
7353 let w, h, x =
7354 let cx w = truncate (relx *. float w) in
7355 List.fold_left
7356 (fun (w, h, x) ws ->
7357 match ws with
7358 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7359 | Wsi.MaxVert -> (w, conf.cwinh, x)
7360 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7362 (state.winw, state.winh, state.x) state.winstate
7364 conf.cwinw <- w;
7365 conf.cwinh <- h;
7366 let f (h, dc) =
7367 let dc = if conf.bedefault then conf else dc in
7368 Buffer.add_string bb "<llppconfig>\n";
7370 if nonemptystr !fontpath
7371 then
7372 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7373 uifontsize
7374 !fontpath
7375 else (
7376 if uifontsize <> 14
7377 then
7378 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7381 Buffer.add_string bb "<defaults ";
7382 add_attrs bb true dc dc;
7383 let kb = keymapsbuf true dc dc in
7384 if Buffer.length kb > 0
7385 then (
7386 Buffer.add_string bb ">\n";
7387 Buffer.add_buffer bb kb;
7388 Buffer.add_string bb "\n</defaults>\n";
7390 else Buffer.add_string bb "/>\n";
7392 let adddoc path pan anchor c bookmarks =
7393 if bookmarks == [] && c = dc && anchor = emptyanchor
7394 then ()
7395 else (
7396 Printf.bprintf bb "<doc path='%s'"
7397 (enent path 0 (String.length path));
7399 if anchor <> emptyanchor
7400 then (
7401 let n, rely, visy = anchor in
7402 Printf.bprintf bb " page='%d'" n;
7403 if rely > 1e-6
7404 then
7405 Printf.bprintf bb " rely='%f'" rely
7407 if abs_float visy > 1e-6
7408 then
7409 Printf.bprintf bb " visy='%f'" visy
7413 if pan != 0
7414 then Printf.bprintf bb " pan='%d'" pan;
7416 add_attrs bb false dc c;
7417 let kb = keymapsbuf false dc c in
7419 begin match bookmarks with
7420 | [] ->
7421 if Buffer.length kb > 0
7422 then (
7423 Buffer.add_string bb ">\n";
7424 Buffer.add_buffer bb kb;
7425 Buffer.add_string bb "\n</doc>\n";
7427 else Buffer.add_string bb "/>\n"
7428 | _ ->
7429 Buffer.add_string bb ">\n<bookmarks>\n";
7430 List.iter (fun (title, _level, (page, rely, visy)) ->
7431 Printf.bprintf bb
7432 "<item title='%s' page='%d'"
7433 (enent title 0 (String.length title))
7434 page
7436 if rely > 1e-6
7437 then
7438 Printf.bprintf bb " rely='%f'" rely
7440 if abs_float visy > 1e-6
7441 then
7442 Printf.bprintf bb " visy='%f'" visy
7444 Buffer.add_string bb "/>\n";
7445 ) bookmarks;
7446 Buffer.add_string bb "</bookmarks>";
7447 if Buffer.length kb > 0
7448 then (
7449 Buffer.add_string bb "\n";
7450 Buffer.add_buffer bb kb;
7452 Buffer.add_string bb "\n</doc>\n";
7453 end;
7457 let pan, conf =
7458 match state.mode with
7459 | Birdseye (c, pan, _, _, _) ->
7460 let beyecolumns =
7461 match conf.columns with
7462 | Cmulti ((c, _, _), _) -> Some c
7463 | Csingle _ -> None
7464 | Csplit _ -> None
7465 and columns =
7466 match c.columns with
7467 | Cmulti (c, _) -> Cmulti (c, [||])
7468 | Csingle _ -> Csingle [||]
7469 | Csplit _ -> failwith "quit from bird's eye while split"
7471 pan, { c with beyecolumns = beyecolumns; columns = columns }
7472 | _ -> x, conf
7474 let basename = Filename.basename
7475 (if emptystr state.origin then state.path else state.origin)
7477 adddoc basename pan (getanchor ())
7478 (let conf =
7479 let autoscrollstep =
7480 match state.autoscroll with
7481 | Some step -> step
7482 | None -> conf.autoscrollstep
7484 match state.mode with
7485 | Birdseye (bc, _, _, _, _) ->
7486 { conf with
7487 zoom = bc.zoom;
7488 presentation = bc.presentation;
7489 interpagespace = bc.interpagespace;
7490 maxwait = bc.maxwait;
7491 autoscrollstep = autoscrollstep }
7492 | _ -> { conf with autoscrollstep = autoscrollstep }
7493 in conf)
7494 (if conf.savebmarks then state.bookmarks else []);
7496 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7497 if basename <> path
7498 then adddoc path x anchor c bookmarks
7499 ) h;
7500 Buffer.add_string bb "</llppconfig>\n";
7501 true;
7503 if load1 f && Buffer.length bb > 0
7504 then
7506 let tmp = !confpath ^ ".tmp" in
7507 let oc = open_out_bin tmp in
7508 Buffer.output_buffer oc bb;
7509 close_out oc;
7510 Unix.rename tmp !confpath;
7511 with exn ->
7512 prerr_endline
7513 ("error while saving configuration: " ^ exntos exn)
7515 end;;
7517 let adderrmsg src msg =
7518 Buffer.add_string state.errmsgs msg;
7519 state.newerrmsgs <- true;
7520 G.postRedisplay src
7523 let adderrfmt src fmt =
7524 Format.kprintf (fun s -> adderrmsg src s) fmt;
7527 let ract cmds =
7528 let cl = splitatspace cmds in
7529 let scan s fmt f =
7530 try Scanf.sscanf s fmt f
7531 with exn ->
7532 adderrfmt "remote exec"
7533 "error processing '%S': %s\n" cmds (exntos exn)
7535 match cl with
7536 | "reload" :: [] -> reload ()
7537 | "goto" :: args :: [] ->
7538 scan args "%u %f %f"
7539 (fun pageno x y ->
7540 let cmd, _ = state.geomcmds in
7541 if emptystr cmd
7542 then gotopagexy pageno x y
7543 else
7544 let f prevf () =
7545 gotopagexy pageno x y;
7546 prevf ()
7548 state.reprf <- f state.reprf
7550 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7551 | "rect" :: args :: [] ->
7552 scan args "%u %u %f %f %f %f"
7553 (fun pageno color x0 y0 x1 y1 ->
7554 onpagerect pageno (fun w h ->
7555 let _,w1,h1,_ = getpagedim pageno in
7556 let sw = float w1 /. float w
7557 and sh = float h1 /. float h in
7558 let x0s = x0 *. sw
7559 and x1s = x1 *. sw
7560 and y0s = y0 *. sh
7561 and y1s = y1 *. sh in
7562 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7563 debugrect rect;
7564 state.rects <- (pageno, color, rect) :: state.rects;
7565 G.postRedisplay "rect";
7568 | "activatewin" :: [] -> Wsi.activatewin ()
7569 | "quit" :: [] -> raise Quit
7570 | _ ->
7571 adderrfmt "remote command"
7572 "error processing remote command: %S\n" cmds;
7575 let remote =
7576 let scratch = String.create 80 in
7577 let buf = Buffer.create 80 in
7578 fun fd ->
7579 let rec tempfr () =
7580 try Some (Unix.read fd scratch 0 80)
7581 with
7582 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7583 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7584 | exn -> raise exn
7586 match tempfr () with
7587 | None -> Some fd
7588 | Some n ->
7589 if n = 0
7590 then (
7591 Unix.close fd;
7592 if Buffer.length buf > 0
7593 then (
7594 let s = Buffer.contents buf in
7595 Buffer.clear buf;
7596 ract s;
7598 None
7600 else
7601 let rec eat ppos =
7602 let nlpos =
7604 let pos = String.index_from scratch ppos '\n' in
7605 if pos >= n then -1 else pos
7606 with Not_found -> -1
7608 if nlpos >= 0
7609 then (
7610 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7611 let s = Buffer.contents buf in
7612 Buffer.clear buf;
7613 ract s;
7614 eat (nlpos+1);
7616 else (
7617 Buffer.add_substring buf scratch ppos (n-ppos);
7618 Some fd
7620 in eat 0
7623 let remoteopen path =
7624 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7625 with exn ->
7626 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7627 None
7630 let () =
7631 let trimcachepath = ref "" in
7632 let rcmdpath = ref "" in
7633 selfexec := Sys.executable_name;
7634 Arg.parse
7635 (Arg.align
7636 [("-p", Arg.String (fun s -> state.password <- s),
7637 "<password> Set password");
7639 ("-f", Arg.String
7640 (fun s ->
7641 Config.fontpath := s;
7642 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7644 "<path> Set path to the user interface font");
7646 ("-c", Arg.String
7647 (fun s ->
7648 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7649 Config.confpath := s),
7650 "<path> Set path to the configuration file");
7652 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7653 "<path> Set path to the trim cache file");
7655 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7656 "<named-destination> Set named destination");
7658 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7659 ("-cxack", Arg.Set cxack, " Cut corners");
7661 ("-remote", Arg.String (fun s -> rcmdpath := s),
7662 "<path> Set path to the remote commands source");
7664 ("-origin", Arg.String (fun s -> state.origin <- s),
7665 "<original-path> Set original path");
7667 ("-v", Arg.Unit (fun () ->
7668 Printf.printf
7669 "%s\nconfiguration path: %s\n"
7670 (version ())
7671 Config.defconfpath
7673 exit 0), " Print version and exit");
7676 (fun s -> state.path <- s)
7677 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7679 if !wtmode
7680 then selfexec := !selfexec ^ " -wtmode";
7682 if emptystr state.path
7683 then (prerr_endline "file name missing"; exit 1);
7685 if not (Config.load ())
7686 then prerr_endline "failed to load configuration";
7688 let wsfd, winw, winh = Wsi.init (object
7689 val mutable m_hack = false
7690 method expose = if not m_hack then G.postRedisplay "expose"
7691 method visible = G.postRedisplay "visible"
7692 method display = m_hack <- false; display ()
7693 method reshape w h =
7694 m_hack <- w < state.winw && h < state.winh;
7695 reshape w h
7696 method mouse b d x y m = mouse b d x y m
7697 method motion x y = state.mpos <- (x, y); motion x y
7698 method pmotion x y = state.mpos <- (x, y); pmotion x y
7699 method key k m =
7700 let mascm = m land (
7701 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7702 ) in
7703 match state.keystate with
7704 | KSnone ->
7705 let km = k, mascm in
7706 begin
7707 match
7708 let modehash = state.uioh#modehash in
7709 try Hashtbl.find modehash km
7710 with Not_found ->
7711 try Hashtbl.find (findkeyhash conf "global") km
7712 with Not_found -> KMinsrt (k, m)
7713 with
7714 | KMinsrt (k, m) -> keyboard k m
7715 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7716 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7718 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7719 List.iter (fun (k, m) -> keyboard k m) insrt;
7720 state.keystate <- KSnone
7721 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7722 state.keystate <- KSinto (keys, insrt)
7723 | _ ->
7724 state.keystate <- KSnone
7726 method enter x y = state.mpos <- (x, y); pmotion x y
7727 method leave = state.mpos <- (-1, -1)
7728 method winstate wsl = state.winstate <- wsl; m_hack <- false
7729 method quit = raise Quit
7730 end) conf.cwinw conf.cwinh (platform = Posx) in
7732 state.wsfd <- wsfd;
7734 if not (
7735 List.exists GlMisc.check_extension
7736 [ "GL_ARB_texture_rectangle"
7737 ; "GL_EXT_texture_recangle"
7738 ; "GL_NV_texture_rectangle" ]
7740 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7742 if (
7743 let r = GlMisc.get_string `renderer in
7744 let p = "Mesa DRI Intel(" in
7745 let l = String.length p in
7746 String.length r > l && String.sub r 0 l = p
7748 then (
7749 defconf.sliceheight <- 1024;
7750 defconf.texcount <- 32;
7751 defconf.usepbo <- true;
7754 let cr, sw =
7755 match Ne.pipe () with
7756 | Ne.Exn exn ->
7757 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7758 exit 1
7759 | Ne.Res rw -> rw
7760 and sr, cw =
7761 match Ne.pipe () with
7762 | Ne.Exn exn ->
7763 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7764 exit 1
7765 | Ne.Res rw -> rw
7768 cloexec cr;
7769 cloexec sw;
7770 cloexec sr;
7771 cloexec cw;
7773 setcheckers conf.checkers;
7774 redirectstderr ();
7775 if conf.redirectstderr
7776 then
7777 at_exit (fun () ->
7778 let s = Buffer.contents state.errmsgs ^
7779 (match state.errfd with
7780 | Some fd ->
7781 let s = String.create (80*24) in
7782 let n =
7784 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7785 if List.mem fd r
7786 then Unix.read fd s 0 (String.length s)
7787 else 0
7788 with _ -> 0
7790 if n = 0
7791 then ""
7792 else String.sub s 0 n
7793 | None -> ""
7796 try ignore (Unix.write state.stderr s 0 (String.length s))
7797 with exn -> print_endline (exntos exn)
7801 init (cr, cw) (
7802 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7803 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7804 !Config.fontpath, !trimcachepath,
7805 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7807 state.sr <- sr;
7808 state.sw <- sw;
7809 state.text <- "Opening " ^ (mbtoutf8 state.path);
7810 reshape winw winh;
7811 opendoc state.path state.password;
7812 state.uioh <- uioh;
7813 display ();
7814 Wsi.mapwin ();
7815 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7816 let optrfd =
7817 ref (
7818 if nonemptystr !rcmdpath
7819 then remoteopen !rcmdpath
7820 else None
7824 let rec loop deadline =
7825 let r =
7826 match state.errfd with
7827 | None -> [state.sr; state.wsfd]
7828 | Some fd -> [state.sr; state.wsfd; fd]
7830 let r =
7831 match !optrfd with
7832 | None -> r
7833 | Some fd -> fd :: r
7835 if state.redisplay
7836 then (
7837 state.redisplay <- false;
7838 display ();
7840 let timeout =
7841 let now = now () in
7842 if deadline > now
7843 then (
7844 if deadline = infinity
7845 then ~-.1.0
7846 else max 0.0 (deadline -. now)
7848 else 0.0
7850 let r, _, _ =
7851 try Unix.select r [] [] timeout
7852 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7854 begin match r with
7855 | [] ->
7856 state.ghyll None;
7857 let newdeadline =
7858 if state.ghyll == noghyll
7859 then
7860 match state.autoscroll with
7861 | Some step when step != 0 ->
7862 let y = state.y + step in
7863 let y =
7864 if y < 0
7865 then state.maxy
7866 else if y >= state.maxy then 0 else y
7868 gotoy y;
7869 if state.mode = View
7870 then state.text <- "";
7871 deadline +. 0.01
7872 | _ -> infinity
7873 else deadline +. 0.01
7875 loop newdeadline
7877 | l ->
7878 let rec checkfds = function
7879 | [] -> ()
7880 | fd :: rest when fd = state.sr ->
7881 let cmd = readcmd state.sr in
7882 act cmd;
7883 checkfds rest
7885 | fd :: rest when fd = state.wsfd ->
7886 Wsi.readresp fd;
7887 checkfds rest
7889 | fd :: rest when Some fd = !optrfd ->
7890 begin match remote fd with
7891 | None -> optrfd := remoteopen !rcmdpath;
7892 | opt -> optrfd := opt
7893 end;
7894 checkfds rest
7896 | fd :: rest ->
7897 let s = String.create 80 in
7898 let n = tempfailureretry (Unix.read fd s 0) 80 in
7899 if conf.redirectstderr
7900 then (
7901 Buffer.add_substring state.errmsgs s 0 n;
7902 state.newerrmsgs <- true;
7903 state.redisplay <- true;
7905 else (
7906 prerr_string (String.sub s 0 n);
7907 flush stderr;
7909 checkfds rest
7911 checkfds l;
7912 let newdeadline =
7913 let deadline1 =
7914 if deadline = infinity
7915 then now () +. 0.01
7916 else deadline
7918 match state.autoscroll with
7919 | Some step when step != 0 -> deadline1
7920 | _ -> if state.ghyll == noghyll then infinity else deadline1
7922 loop newdeadline
7923 end;
7926 loop infinity;
7927 with Quit ->
7928 Config.save ();