Cosmetics
[llpp.git] / main.ml
blobf80f5b7dc63970fae89c18248216fdd8384ffa3d
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 f n a b =
1982 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1983 let iv x = x**3.-.0.5*.x**4. in
1984 let iv1 = iv f in
1985 let ins = float a *. iv1
1986 and outs = float (n-b) *. iv1 in
1987 let ones = b - a in
1988 ins +. outs +. float ones
1990 let rec set (_N, _A, _B) y sy =
1991 let sum = summa 1.0 _N _A _B in
1992 let dy = float (y - sy) in
1993 state.ghyll <- (
1994 let rec gf n y1 o =
1995 if n >= _N
1996 then state.ghyll <- noghyll
1997 else
1998 let go n =
1999 let s = scroll n _N _A _B in
2000 let y1 = y1 +. ((s *. dy) /. sum) in
2001 gotoy_and_clear_text (truncate y1);
2002 state.ghyll <- gf (n+1) y1;
2004 match o with
2005 | None -> go n
2006 | Some y' -> set (_N/2, 1, 1) y' state.y
2008 gf 0 (float state.y)
2011 match conf.ghyllscroll with
2012 | None ->
2013 gotoy_and_clear_text y
2014 | Some nab ->
2015 if state.ghyll == noghyll
2016 then set nab y state.y
2017 else state.ghyll (Some y)
2020 let gotopage n top =
2021 let y, h = getpageyh n in
2022 let y = y + (truncate (top *. float h)) in
2023 gotoghyll y
2026 let gotopage1 n top =
2027 let y = getpagey n in
2028 let y = y + top in
2029 gotoghyll y
2032 let invalidate s f =
2033 state.layout <- [];
2034 state.pdims <- [];
2035 state.rects <- [];
2036 state.rects1 <- [];
2037 match state.geomcmds with
2038 | ps, [] when emptystr ps ->
2039 f ();
2040 state.geomcmds <- s, [];
2042 | ps, [] ->
2043 state.geomcmds <- ps, [s, f];
2045 | ps, (s', _) :: rest when s' = s ->
2046 state.geomcmds <- ps, ((s, f) :: rest);
2048 | ps, cmds ->
2049 state.geomcmds <- ps, ((s, f) :: cmds);
2052 let flushpages () =
2053 Hashtbl.iter (fun _ opaque ->
2054 wcmd "freepage %s" opaque;
2055 ) state.pagemap;
2056 Hashtbl.clear state.pagemap;
2059 let flushtiles () =
2060 if not (Queue.is_empty state.tilelru)
2061 then (
2062 Queue.iter (fun (k, p, s) ->
2063 wcmd "freetile %s" p;
2064 state.memused <- state.memused - s;
2065 Hashtbl.remove state.tilemap k;
2066 ) state.tilelru;
2067 state.uioh#infochanged Memused;
2068 Queue.clear state.tilelru;
2070 load state.layout;
2073 let stateh h =
2074 let h = truncate (float h*.conf.zoom) in
2075 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2076 h - d
2079 let opendoc path password =
2080 state.path <- path;
2081 state.password <- password;
2082 state.gen <- state.gen + 1;
2083 state.docinfo <- [];
2085 flushpages ();
2086 setaalevel conf.aalevel;
2087 let titlepath =
2088 if emptystr state.origin
2089 then path
2090 else state.origin
2092 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2093 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2094 invalidate "reqlayout"
2095 (fun () ->
2096 wcmd "reqlayout %d %d %d %s\000"
2097 conf.angle (FMTE.to_int conf.fitmodel)
2098 (stateh state.winh) state.nameddest
2102 let reload () =
2103 state.anchor <- getanchor ();
2104 opendoc state.path state.password;
2107 let scalecolor c =
2108 let c = c *. conf.colorscale in
2109 (c, c, c);
2112 let scalecolor2 (r, g, b) =
2113 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2116 let docolumns = function
2117 | Csingle _ ->
2118 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2119 let rec loop pageno pdimno pdim y ph pdims =
2120 if pageno = state.pagecount
2121 then ()
2122 else
2123 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2124 match pdims with
2125 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2126 pdimno+1, pdim, rest
2127 | _ ->
2128 pdimno, pdim, pdims
2130 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2131 let y = y +
2132 (if conf.presentation
2133 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2134 else (if pageno = 0 then 0 else conf.interpagespace)
2137 a.(pageno) <- (pdimno, x, y, pdim);
2138 loop (pageno+1) pdimno pdim (y + h) h pdims
2140 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2141 conf.columns <- Csingle a;
2143 | Cmulti ((columns, coverA, coverB), _) ->
2144 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2145 let rec loop pageno pdimno pdim x y rowh pdims =
2146 let rec fixrow m = if m = pageno then () else
2147 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2148 if h < rowh
2149 then (
2150 let y = y + (rowh - h) / 2 in
2151 a.(m) <- (pdimno, x, y, pdim);
2153 fixrow (m+1)
2155 if pageno = state.pagecount
2156 then fixrow (((pageno - 1) / columns) * columns)
2157 else
2158 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2159 match pdims with
2160 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2161 pdimno+1, pdim, rest
2162 | _ ->
2163 pdimno, pdim, pdims
2165 let x, y, rowh' =
2166 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2167 then (
2168 let x = (wadjsb state.winw - w) / 2 in
2169 let ips =
2170 if conf.presentation then calcips h else conf.interpagespace in
2171 x, y + ips + rowh, h
2173 else (
2174 if (pageno - coverA) mod columns = 0
2175 then (
2176 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2177 let y =
2178 if conf.presentation
2179 then
2180 let ips = calcips h in
2181 y + (if pageno = 0 then 0 else calcips rowh + ips)
2182 else
2183 y + (if pageno = 0 then 0 else conf.interpagespace)
2185 x, y + rowh, h
2187 else x, y, max rowh h
2190 let y =
2191 if pageno > 1 && (pageno - coverA) mod columns = 0
2192 then (
2193 let y =
2194 if pageno = columns && conf.presentation
2195 then (
2196 let ips = calcips rowh in
2197 for i = 0 to pred columns
2199 let (pdimno, x, y, pdim) = a.(i) in
2200 a.(i) <- (pdimno, x, y+ips, pdim)
2201 done;
2202 y+ips;
2204 else y
2206 fixrow (pageno - columns);
2209 else y
2211 a.(pageno) <- (pdimno, x, y, pdim);
2212 let x = x + w + xoff*2 + conf.interpagespace in
2213 loop (pageno+1) pdimno pdim x y rowh' pdims
2215 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2216 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2218 | Csplit (c, _) ->
2219 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2220 let rec loop pageno pdimno pdim y pdims =
2221 if pageno = state.pagecount
2222 then ()
2223 else
2224 let pdimno, ((_, w, h, _) as pdim), pdims =
2225 match pdims with
2226 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2227 pdimno+1, pdim, rest
2228 | _ ->
2229 pdimno, pdim, pdims
2231 let cw = w / c in
2232 let rec loop1 n x y =
2233 if n = c then y else (
2234 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2235 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2238 let y = loop1 0 0 y in
2239 loop (pageno+1) pdimno pdim y pdims
2241 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2242 conf.columns <- Csplit (c, a);
2245 let represent () =
2246 docolumns conf.columns;
2247 state.maxy <- calcheight ();
2248 if state.reprf == noreprf
2249 then (
2250 match state.mode with
2251 | Birdseye (_, _, pageno, _, _) ->
2252 let y, h = getpageyh pageno in
2253 let top = (state.winh - h) / 2 in
2254 gotoy (max 0 (y - top))
2255 | _ -> gotoanchor state.anchor
2257 else (
2258 state.reprf ();
2259 state.reprf <- noreprf;
2263 let reshape w h =
2264 GlDraw.viewport 0 0 w h;
2265 let firsttime = state.geomcmds == firstgeomcmds in
2266 if not firsttime && nogeomcmds state.geomcmds
2267 then state.anchor <- getanchor ();
2269 state.winw <- w;
2270 let w = wadjsb (truncate (float w *. conf.zoom)) in
2271 let w = max w 2 in
2272 state.winh <- h;
2273 setfontsize fstate.fontsize;
2274 GlMat.mode `modelview;
2275 GlMat.load_identity ();
2277 GlMat.mode `projection;
2278 GlMat.load_identity ();
2279 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2280 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2281 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2283 let relx =
2284 if conf.zoom <= 1.0
2285 then 0.0
2286 else float state.x /. float state.w
2288 invalidate "geometry"
2289 (fun () ->
2290 state.w <- w;
2291 if not firsttime
2292 then state.x <- truncate (relx *. float w);
2293 let w =
2294 match conf.columns with
2295 | Csingle _ -> w
2296 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2297 | Csplit (c, _) -> w * c
2299 wcmd "geometry %d %d %d"
2300 w (stateh h) (FMTE.to_int conf.fitmodel)
2304 let enttext () =
2305 let len = String.length state.text in
2306 let drawstring s =
2307 let hscrollh =
2308 match state.mode with
2309 | Textentry _ | View | LinkNav _ ->
2310 let h, _, _ = state.uioh#scrollpw in
2312 | _ -> 0
2314 let rect x w =
2315 GlDraw.rect
2316 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2317 (x+.w, float (state.winh - hscrollh))
2320 let w = float (wadjsb state.winw - 1) in
2321 if state.progress >= 0.0 && state.progress < 1.0
2322 then (
2323 GlDraw.color (0.3, 0.3, 0.3);
2324 let w1 = w *. state.progress in
2325 rect 0.0 w1;
2326 GlDraw.color (0.0, 0.0, 0.0);
2327 rect w1 (w-.w1)
2329 else (
2330 GlDraw.color (0.0, 0.0, 0.0);
2331 rect 0.0 w;
2334 GlDraw.color (1.0, 1.0, 1.0);
2335 drawstring fstate.fontsize
2336 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2338 let s =
2339 match state.mode with
2340 | Textentry ((prefix, text, _, _, _, _), _) ->
2341 let s =
2342 if len > 0
2343 then
2344 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2345 else
2346 Printf.sprintf "%s%s_" prefix text
2350 | _ -> state.text
2352 let s =
2353 if state.newerrmsgs
2354 then (
2355 if not (istextentry state.mode) && state.uioh#eformsgs
2356 then
2357 let s1 = "(press 'e' to review error messasges)" in
2358 if nonemptystr s then s ^ " " ^ s1 else s1
2359 else s
2361 else s
2363 if nonemptystr s
2364 then drawstring s
2367 let gctiles () =
2368 let len = Queue.length state.tilelru in
2369 let layout = lazy (
2370 match state.throttle with
2371 | None ->
2372 if conf.preload
2373 then preloadlayout state.y
2374 else state.layout
2375 | Some (layout, _, _) ->
2376 layout
2377 ) in
2378 let rec loop qpos =
2379 if state.memused <= conf.memlimit
2380 then ()
2381 else (
2382 if qpos < len
2383 then
2384 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2385 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2386 let (_, pw, ph, _) = getpagedim n in
2388 gen = state.gen
2389 && colorspace = conf.colorspace
2390 && angle = conf.angle
2391 && pagew = pw
2392 && pageh = ph
2393 && (
2394 let x = col*conf.tilew
2395 and y = row*conf.tileh in
2396 tilevisible (Lazy.force_val layout) n x y
2398 then Queue.push lruitem state.tilelru
2399 else (
2400 freepbo p;
2401 wcmd "freetile %s" p;
2402 state.memused <- state.memused - s;
2403 state.uioh#infochanged Memused;
2404 Hashtbl.remove state.tilemap k;
2406 loop (qpos+1)
2409 loop 0
2412 let logcurrently = function
2413 | Idle -> dolog "Idle"
2414 | Loading (l, gen) ->
2415 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2416 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2417 dolog
2418 "Tiling %d[%d,%d] page=%s cs=%s angle"
2419 l.pageno col row pageopaque
2420 (CSTE.to_string colorspace)
2422 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2423 angle gen conf.angle state.gen
2424 tilew tileh
2425 conf.tilew conf.tileh
2427 | Outlining _ ->
2428 dolog "outlining"
2431 let splitatspace =
2432 let r = Str.regexp " " in
2433 fun s -> Str.bounded_split r s 2;
2436 let onpagerect pageno f =
2437 let b =
2438 match conf.columns with
2439 | Cmulti (_, b) -> b
2440 | Csingle b -> b
2441 | Csplit (_, b) -> b
2443 if pageno >= 0 && pageno < Array.length b
2444 then
2445 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2446 f w h
2449 let gotopagexy1 pageno x y =
2450 let _,w1,h1,leftx = getpagedim pageno in
2451 let top = y /. (float h1) in
2452 let left = x /. (float w1) in
2453 let py, w, h = getpageywh pageno in
2454 let wh = state.winh - hscrollh () in
2455 let x = left *. (float w) in
2456 let x = leftx + state.x + truncate x in
2457 let sx =
2458 if x < 0 || x >= wadjsb state.winw
2459 then state.x - x
2460 else state.x
2462 let pdy = truncate (top *. float h) in
2463 let y' = py + pdy in
2464 let dy = y' - state.y in
2465 let sy =
2466 if x != state.x || not (dy > 0 && dy < wh)
2467 then (
2468 if conf.presentation
2469 then
2470 if abs (py - y') > wh
2471 then y'
2472 else py
2473 else y';
2475 else state.y
2477 if state.x != sx || state.y != sy
2478 then (
2479 let x, y =
2480 if !wtmode
2481 then (
2482 let ww = wadjsb state.winw in
2483 let qx = sx / ww
2484 and qy = pdy / wh in
2485 let x = qx * ww
2486 and y = py + qy * wh in
2487 let x = if -x + ww > w1 then -(w1-ww) else x
2488 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2489 let y =
2490 if conf.presentation
2491 then
2492 if abs (py - y') > wh
2493 then y'
2494 else py
2495 else y';
2497 (x, y)
2499 else (sx, sy)
2501 state.x <- x;
2502 gotoy_and_clear_text y;
2504 else gotoy_and_clear_text state.y;
2507 let gotopagexy pageno x y =
2508 match state.mode with
2509 | Birdseye _ -> gotopage pageno 0.0
2510 | _ -> gotopagexy1 pageno x y
2513 let act cmds =
2514 (* dolog "%S" cmds; *)
2515 let cl = splitatspace cmds in
2516 let scan s fmt f =
2517 try Scanf.sscanf s fmt f
2518 with exn ->
2519 dolog "error processing '%S': %s" cmds (exntos exn);
2520 exit 1
2522 match cl with
2523 | "clear" :: [] ->
2524 state.uioh#infochanged Pdim;
2525 state.pdims <- [];
2527 | "clearrects" :: [] ->
2528 state.rects <- state.rects1;
2529 G.postRedisplay "clearrects";
2531 | "continue" :: args :: [] ->
2532 let n = scan args "%u" (fun n -> n) in
2533 state.pagecount <- n;
2534 begin match state.currently with
2535 | Outlining l ->
2536 state.currently <- Idle;
2537 state.outlines <- Array.of_list (List.rev l)
2538 | _ -> ()
2539 end;
2541 let cur, cmds = state.geomcmds in
2542 if emptystr cur
2543 then failwith "umpossible";
2545 begin match List.rev cmds with
2546 | [] ->
2547 state.geomcmds <- "", [];
2548 represent ();
2549 | (s, f) :: rest ->
2550 f ();
2551 state.geomcmds <- s, List.rev rest;
2552 end;
2553 if conf.maxwait = None && not !wtmode
2554 then G.postRedisplay "continue";
2556 | "title" :: args :: [] ->
2557 Wsi.settitle args
2559 | "msg" :: args :: [] ->
2560 showtext ' ' args
2562 | "vmsg" :: args :: [] ->
2563 if conf.verbose
2564 then showtext ' ' args
2566 | "emsg" :: args :: [] ->
2567 Buffer.add_string state.errmsgs args;
2568 state.newerrmsgs <- true;
2569 G.postRedisplay "error message"
2571 | "progress" :: args :: [] ->
2572 let progress, text =
2573 scan args "%f %n"
2574 (fun f pos ->
2575 f, String.sub args pos (String.length args - pos))
2577 state.text <- text;
2578 state.progress <- progress;
2579 G.postRedisplay "progress"
2581 | "firstmatch" :: args :: [] ->
2582 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2583 scan args "%u %d %f %f %f %f %f %f %f %f"
2584 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2585 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2587 let y = (getpagey pageno) + truncate y0 in
2588 addnav ();
2589 gotoy y;
2590 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2592 | "match" :: args :: [] ->
2593 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2594 scan args "%u %d %f %f %f %f %f %f %f %f"
2595 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2596 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2598 state.rects1 <-
2599 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2601 | "page" :: args :: [] ->
2602 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2603 begin match state.currently with
2604 | Loading (l, gen) ->
2605 vlog "page %d took %f sec" l.pageno t;
2606 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2607 begin match state.throttle with
2608 | None ->
2609 let preloadedpages =
2610 if conf.preload
2611 then preloadlayout state.y
2612 else state.layout
2614 let evict () =
2615 let set =
2616 List.fold_left (fun s l -> IntSet.add l.pageno s)
2617 IntSet.empty preloadedpages
2619 let evictedpages =
2620 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2621 if not (IntSet.mem pageno set)
2622 then (
2623 wcmd "freepage %s" opaque;
2624 key :: accu
2626 else accu
2627 ) state.pagemap []
2629 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2631 evict ();
2632 state.currently <- Idle;
2633 if gen = state.gen
2634 then (
2635 tilepage l.pageno pageopaque state.layout;
2636 load state.layout;
2637 load preloadedpages;
2638 if pagevisible state.layout l.pageno
2639 && layoutready state.layout
2640 then G.postRedisplay "page";
2643 | Some (layout, _, _) ->
2644 state.currently <- Idle;
2645 tilepage l.pageno pageopaque layout;
2646 load state.layout
2647 end;
2649 | _ ->
2650 dolog "Inconsistent loading state";
2651 logcurrently state.currently;
2652 exit 1
2655 | "tile" :: args :: [] ->
2656 let (x, y, opaque, size, t) =
2657 scan args "%u %u %s %u %f"
2658 (fun x y p size t -> (x, y, p, size, t))
2660 begin match state.currently with
2661 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2662 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2664 unmappbo opaque;
2665 if tilew != conf.tilew || tileh != conf.tileh
2666 then (
2667 wcmd "freetile %s" opaque;
2668 state.currently <- Idle;
2669 load state.layout;
2671 else (
2672 puttileopaque l col row gen cs angle opaque size t;
2673 state.memused <- state.memused + size;
2674 state.uioh#infochanged Memused;
2675 gctiles ();
2676 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2677 opaque, size) state.tilelru;
2679 let layout =
2680 match state.throttle with
2681 | None -> state.layout
2682 | Some (layout, _, _) -> layout
2685 state.currently <- Idle;
2686 if gen = state.gen
2687 && conf.colorspace = cs
2688 && conf.angle = angle
2689 && tilevisible layout l.pageno x y
2690 then conttiling l.pageno pageopaque;
2692 begin match state.throttle with
2693 | None ->
2694 preload state.layout;
2695 if gen = state.gen
2696 && conf.colorspace = cs
2697 && conf.angle = angle
2698 && tilevisible state.layout l.pageno x y
2699 && (not !wtmode || layoutready state.layout)
2700 then G.postRedisplay "tile nothrottle";
2702 | Some (layout, y, _) ->
2703 let ready = layoutready layout in
2704 if ready
2705 then (
2706 state.y <- y;
2707 state.layout <- layout;
2708 state.throttle <- None;
2709 G.postRedisplay "throttle";
2711 else load layout;
2712 end;
2715 | _ ->
2716 dolog "Inconsistent tiling state";
2717 logcurrently state.currently;
2718 exit 1
2721 | "pdim" :: args :: [] ->
2722 let (n, w, h, _) as pdim =
2723 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2725 let pdim =
2726 match conf.fitmodel, conf.columns with
2727 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2728 | _ -> pdim
2730 state.uioh#infochanged Pdim;
2731 state.pdims <- pdim :: state.pdims
2733 | "o" :: args :: [] ->
2734 let (l, n, t, h, pos) =
2735 scan args "%u %u %d %u %n"
2736 (fun l n t h pos -> l, n, t, h, pos)
2738 let s = String.sub args pos (String.length args - pos) in
2739 let outline = (s, l, (n, float t /. float h, 0.0)) in
2740 begin match state.currently with
2741 | Outlining outlines ->
2742 state.currently <- Outlining (outline :: outlines)
2743 | Idle ->
2744 state.currently <- Outlining [outline]
2745 | currently ->
2746 dolog "invalid outlining state";
2747 logcurrently currently
2750 | "a" :: args :: [] ->
2751 let (n, l, t) =
2752 scan args "%u %d %d" (fun n l t -> n, l, t)
2754 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2756 | "info" :: args :: [] ->
2757 state.docinfo <- (1, args) :: state.docinfo
2759 | "infoend" :: [] ->
2760 state.uioh#infochanged Docinfo;
2761 state.docinfo <- List.rev state.docinfo
2763 | _ ->
2764 error "unknown cmd `%S'" cmds
2767 let onhist cb =
2768 let rc = cb.rc in
2769 let action = function
2770 | HCprev -> cbget cb ~-1
2771 | HCnext -> cbget cb 1
2772 | HCfirst -> cbget cb ~-(cb.rc)
2773 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2774 and cancel () = cb.rc <- rc
2775 in (action, cancel)
2778 let search pattern forward =
2779 match conf.columns with
2780 | Csplit _ ->
2781 showtext '!' "searching does not work properly in split columns mode"
2782 | _ ->
2783 if nonemptystr pattern
2784 then
2785 let pn, py =
2786 match state.layout with
2787 | [] -> 0, 0
2788 | l :: _ ->
2789 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2791 wcmd "search %d %d %d %d,%s\000"
2792 (btod conf.icase) pn py (btod forward) pattern;
2795 let intentry text key =
2796 let c =
2797 if key >= 32 && key < 127
2798 then Char.chr key
2799 else '\000'
2801 match c with
2802 | '0' .. '9' ->
2803 let text = addchar text c in
2804 TEcont text
2806 | _ ->
2807 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2808 TEcont text
2811 let linknentry text key =
2812 let c =
2813 if key >= 32 && key < 127
2814 then Char.chr key
2815 else '\000'
2817 match c with
2818 | 'a' .. 'z' ->
2819 let text = addchar text c in
2820 TEcont text
2822 | _ ->
2823 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2824 TEcont text
2827 let linkndone f s =
2828 if nonemptystr s
2829 then (
2830 let n =
2831 let l = String.length s in
2832 let rec loop pos n = if pos = l then n else
2833 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2834 loop (pos+1) (n*26 + m)
2835 in loop 0 0
2837 let rec loop n = function
2838 | [] -> ()
2839 | l :: rest ->
2840 match getopaque l.pageno with
2841 | None -> loop n rest
2842 | Some opaque ->
2843 let m = getlinkcount opaque in
2844 if n < m
2845 then (
2846 let under = getlink opaque n in
2847 f under
2849 else loop (n-m) rest
2851 loop n state.layout;
2855 let textentry text key =
2856 if key land 0xff00 = 0xff00
2857 then TEcont text
2858 else TEcont (text ^ toutf8 key)
2861 let reqlayout angle fitmodel =
2862 match state.throttle with
2863 | None ->
2864 if nogeomcmds state.geomcmds
2865 then state.anchor <- getanchor ();
2866 conf.angle <- angle mod 360;
2867 if conf.angle != 0
2868 then (
2869 match state.mode with
2870 | LinkNav _ -> state.mode <- View
2871 | _ -> ()
2873 conf.fitmodel <- fitmodel;
2874 invalidate "reqlayout"
2875 (fun () ->
2876 wcmd "reqlayout %d %d %d"
2877 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2879 | _ -> ()
2882 let settrim trimmargins trimfuzz =
2883 if nogeomcmds state.geomcmds
2884 then state.anchor <- getanchor ();
2885 conf.trimmargins <- trimmargins;
2886 conf.trimfuzz <- trimfuzz;
2887 let x0, y0, x1, y1 = trimfuzz in
2888 invalidate "settrim"
2889 (fun () ->
2890 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2891 flushpages ();
2894 let setzoom zoom =
2895 match state.throttle with
2896 | None ->
2897 let zoom = max 0.0001 zoom in
2898 if zoom <> conf.zoom
2899 then (
2900 state.prevzoom <- (conf.zoom, state.x);
2901 conf.zoom <- zoom;
2902 reshape state.winw state.winh;
2903 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2906 | Some (layout, y, started) ->
2907 let time =
2908 match conf.maxwait with
2909 | None -> 0.0
2910 | Some t -> t
2912 let dt = now () -. started in
2913 if dt > time
2914 then (
2915 state.y <- y;
2916 load layout;
2920 let setcolumns mode columns coverA coverB =
2921 state.prevcolumns <- Some (conf.columns, conf.zoom);
2922 if columns < 0
2923 then (
2924 if isbirdseye mode
2925 then showtext '!' "split mode doesn't work in bird's eye"
2926 else (
2927 conf.columns <- Csplit (-columns, [||]);
2928 state.x <- 0;
2929 conf.zoom <- 1.0;
2932 else (
2933 if columns < 2
2934 then (
2935 conf.columns <- Csingle [||];
2936 state.x <- 0;
2937 setzoom 1.0;
2939 else (
2940 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2941 conf.zoom <- 1.0;
2944 reshape state.winw state.winh;
2947 let enterbirdseye () =
2948 let zoom = float conf.thumbw /. float state.winw in
2949 let birdseyepageno =
2950 let cy = state.winh / 2 in
2951 let fold = function
2952 | [] -> 0
2953 | l :: rest ->
2954 let rec fold best = function
2955 | [] -> best.pageno
2956 | l :: rest ->
2957 let d = cy - (l.pagedispy + l.pagevh/2)
2958 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2959 if abs d < abs dbest
2960 then fold l rest
2961 else best.pageno
2962 in fold l rest
2964 fold state.layout
2966 state.mode <- Birdseye (
2967 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2969 conf.zoom <- zoom;
2970 conf.presentation <- false;
2971 conf.interpagespace <- 10;
2972 conf.hlinks <- false;
2973 conf.fitmodel <- FitProportional;
2974 state.x <- 0;
2975 state.mstate <- Mnone;
2976 conf.maxwait <- None;
2977 conf.columns <- (
2978 match conf.beyecolumns with
2979 | Some c ->
2980 conf.zoom <- 1.0;
2981 Cmulti ((c, 0, 0), [||])
2982 | None -> Csingle [||]
2984 Wsi.setcursor Wsi.CURSOR_INHERIT;
2985 if conf.verbose
2986 then
2987 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2988 (100.0*.zoom)
2989 else
2990 state.text <- ""
2992 reshape state.winw state.winh;
2995 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2996 state.mode <- View;
2997 conf.zoom <- c.zoom;
2998 conf.presentation <- c.presentation;
2999 conf.interpagespace <- c.interpagespace;
3000 conf.maxwait <- c.maxwait;
3001 conf.hlinks <- c.hlinks;
3002 conf.fitmodel <- c.fitmodel;
3003 conf.beyecolumns <- (
3004 match conf.columns with
3005 | Cmulti ((c, _, _), _) -> Some c
3006 | Csingle _ -> None
3007 | Csplit _ -> failwith "leaving bird's eye split mode"
3009 conf.columns <- (
3010 match c.columns with
3011 | Cmulti (c, _) -> Cmulti (c, [||])
3012 | Csingle _ -> Csingle [||]
3013 | Csplit (c, _) -> Csplit (c, [||])
3015 if conf.verbose
3016 then
3017 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3018 (100.0*.conf.zoom)
3020 reshape state.winw state.winh;
3021 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3022 state.x <- leftx;
3025 let togglebirdseye () =
3026 match state.mode with
3027 | Birdseye vals -> leavebirdseye vals true
3028 | View -> enterbirdseye ()
3029 | _ -> ()
3032 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3033 let pageno = max 0 (pageno - incr) in
3034 let rec loop = function
3035 | [] -> gotopage1 pageno 0
3036 | l :: _ when l.pageno = pageno ->
3037 if l.pagedispy >= 0 && l.pagey = 0
3038 then G.postRedisplay "upbirdseye"
3039 else gotopage1 pageno 0
3040 | _ :: rest -> loop rest
3042 loop state.layout;
3043 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3046 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3047 let pageno = min (state.pagecount - 1) (pageno + incr) in
3048 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3049 let rec loop = function
3050 | [] ->
3051 let y, h = getpageyh pageno in
3052 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3053 gotoy (clamp dy)
3054 | l :: _ when l.pageno = pageno ->
3055 if l.pagevh != l.pageh
3056 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3057 else G.postRedisplay "downbirdseye"
3058 | _ :: rest -> loop rest
3060 loop state.layout
3063 let optentry mode _ key =
3064 let btos b = if b then "on" else "off" in
3065 if key >= 32 && key < 127
3066 then
3067 let c = Char.chr key in
3068 match c with
3069 | 's' ->
3070 let ondone s =
3071 try conf.scrollstep <- int_of_string s with exc ->
3072 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3074 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3076 | 'A' ->
3077 let ondone s =
3079 conf.autoscrollstep <- int_of_string s;
3080 if state.autoscroll <> None
3081 then state.autoscroll <- Some conf.autoscrollstep
3082 with exc ->
3083 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3085 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3087 | 'C' ->
3088 let ondone s =
3090 let n, a, b = multicolumns_of_string s in
3091 setcolumns mode n a b;
3092 with exc ->
3093 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3095 TEswitch ("columns: ", "", None, textentry, ondone, true)
3097 | 'Z' ->
3098 let ondone s =
3100 let zoom = float (int_of_string s) /. 100.0 in
3101 setzoom zoom
3102 with exc ->
3103 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3105 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3107 | 't' ->
3108 let ondone s =
3110 conf.thumbw <- bound (int_of_string s) 2 4096;
3111 state.text <-
3112 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3113 begin match mode with
3114 | Birdseye beye ->
3115 leavebirdseye beye false;
3116 enterbirdseye ();
3117 | _ -> ();
3119 with exc ->
3120 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3122 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3124 | 'R' ->
3125 let ondone s =
3126 match try
3127 Some (int_of_string s)
3128 with exc ->
3129 state.text <- Printf.sprintf "bad integer `%s': %s"
3130 s (exntos exc);
3131 None
3132 with
3133 | Some angle -> reqlayout angle conf.fitmodel
3134 | None -> ()
3136 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3138 | 'i' ->
3139 conf.icase <- not conf.icase;
3140 TEdone ("case insensitive search " ^ (btos conf.icase))
3142 | 'p' ->
3143 conf.preload <- not conf.preload;
3144 gotoy state.y;
3145 TEdone ("preload " ^ (btos conf.preload))
3147 | 'v' ->
3148 conf.verbose <- not conf.verbose;
3149 TEdone ("verbose " ^ (btos conf.verbose))
3151 | 'd' ->
3152 conf.debug <- not conf.debug;
3153 TEdone ("debug " ^ (btos conf.debug))
3155 | 'h' ->
3156 conf.maxhfit <- not conf.maxhfit;
3157 state.maxy <- calcheight ();
3158 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3160 | 'c' ->
3161 conf.crophack <- not conf.crophack;
3162 TEdone ("crophack " ^ btos conf.crophack)
3164 | 'a' ->
3165 let s =
3166 match conf.maxwait with
3167 | None ->
3168 conf.maxwait <- Some infinity;
3169 "always wait for page to complete"
3170 | Some _ ->
3171 conf.maxwait <- None;
3172 "show placeholder if page is not ready"
3174 TEdone s
3176 | 'f' ->
3177 conf.underinfo <- not conf.underinfo;
3178 TEdone ("underinfo " ^ btos conf.underinfo)
3180 | 'P' ->
3181 conf.savebmarks <- not conf.savebmarks;
3182 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3184 | 'S' ->
3185 let ondone s =
3187 let pageno, py =
3188 match state.layout with
3189 | [] -> 0, 0
3190 | l :: _ ->
3191 l.pageno, l.pagey
3193 conf.interpagespace <- int_of_string s;
3194 docolumns conf.columns;
3195 state.maxy <- calcheight ();
3196 let y = getpagey pageno in
3197 gotoy (y + py)
3198 with exc ->
3199 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3201 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3203 | 'l' ->
3204 let fm =
3205 match conf.fitmodel with
3206 | FitProportional -> FitWidth
3207 | _ -> FitProportional
3209 reqlayout conf.angle fm;
3210 TEdone ("proportional display " ^ btos (fm == FitProportional))
3212 | 'T' ->
3213 settrim (not conf.trimmargins) conf.trimfuzz;
3214 TEdone ("trim margins " ^ btos conf.trimmargins)
3216 | 'I' ->
3217 conf.invert <- not conf.invert;
3218 TEdone ("invert colors " ^ btos conf.invert)
3220 | 'x' ->
3221 let ondone s =
3222 cbput state.hists.sel s;
3223 conf.selcmd <- s;
3225 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3226 textentry, ondone, true)
3228 | 'M' ->
3229 if conf.pax == None
3230 then conf.pax <- Some (ref (0.0, 0, 0))
3231 else conf.pax <- None;
3232 TEdone ("PAX " ^ btos (conf.pax != None))
3234 | _ ->
3235 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3236 TEstop
3237 else
3238 TEcont state.text
3241 class type lvsource = object
3242 method getitemcount : int
3243 method getitem : int -> (string * int)
3244 method hasaction : int -> bool
3245 method exit :
3246 uioh:uioh ->
3247 cancel:bool ->
3248 active:int ->
3249 first:int ->
3250 pan:int ->
3251 qsearch:string ->
3252 uioh option
3253 method getactive : int
3254 method getfirst : int
3255 method getqsearch : string
3256 method setqsearch : string -> unit
3257 method getpan : int
3258 end;;
3260 class virtual lvsourcebase = object
3261 val mutable m_active = 0
3262 val mutable m_first = 0
3263 val mutable m_qsearch = ""
3264 val mutable m_pan = 0
3265 method getactive = m_active
3266 method getfirst = m_first
3267 method getqsearch = m_qsearch
3268 method getpan = m_pan
3269 method setqsearch s = m_qsearch <- s
3270 end;;
3272 let withoutlastutf8 s =
3273 let len = String.length s in
3274 if len = 0
3275 then s
3276 else
3277 let rec find pos =
3278 if pos = 0
3279 then pos
3280 else
3281 let b = Char.code s.[pos] in
3282 if b land 0b11000000 = 0b11000000
3283 then pos
3284 else find (pos-1)
3286 let first =
3287 if Char.code s.[len-1] land 0x80 = 0
3288 then len-1
3289 else find (len-1)
3291 String.sub s 0 first;
3294 let textentrykeyboard
3295 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3296 let key =
3297 if key >= 0xffb0 && key <= 0xffb9
3298 then key - 0xffb0 + 48 else key
3300 let enttext te =
3301 state.mode <- Textentry (te, onleave);
3302 state.text <- "";
3303 enttext ();
3304 G.postRedisplay "textentrykeyboard enttext";
3306 let histaction cmd =
3307 match opthist with
3308 | None -> ()
3309 | Some (action, _) ->
3310 state.mode <- Textentry (
3311 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3313 G.postRedisplay "textentry histaction"
3315 match key with
3316 | 0xff08 -> (* backspace *)
3317 let s = withoutlastutf8 text in
3318 let len = String.length s in
3319 if cancelonempty && len = 0
3320 then (
3321 onleave Cancel;
3322 G.postRedisplay "textentrykeyboard after cancel";
3324 else (
3325 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3328 | 0xff0d | 0xff8d -> (* (kp) enter *)
3329 ondone text;
3330 onleave Confirm;
3331 G.postRedisplay "textentrykeyboard after confirm"
3333 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3334 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3335 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3336 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3338 | 0xff1b -> (* escape*)
3339 if emptystr text
3340 then (
3341 begin match opthist with
3342 | None -> ()
3343 | Some (_, onhistcancel) -> onhistcancel ()
3344 end;
3345 onleave Cancel;
3346 state.text <- "";
3347 G.postRedisplay "textentrykeyboard after cancel2"
3349 else (
3350 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3353 | 0xff9f | 0xffff -> () (* delete *)
3355 | _ when key != 0
3356 && key land 0xff00 != 0xff00 (* keyboard *)
3357 && key land 0xfe00 != 0xfe00 (* xkb *)
3358 && key land 0xfd00 != 0xfd00 (* 3270 *)
3360 begin match onkey text key with
3361 | TEdone text ->
3362 ondone text;
3363 onleave Confirm;
3364 G.postRedisplay "textentrykeyboard after confirm2";
3366 | TEcont text ->
3367 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3369 | TEstop ->
3370 onleave Cancel;
3371 G.postRedisplay "textentrykeyboard after cancel3"
3373 | TEswitch te ->
3374 state.mode <- Textentry (te, onleave);
3375 G.postRedisplay "textentrykeyboard switch";
3376 end;
3378 | _ ->
3379 vlog "unhandled key %s" (Wsi.keyname key)
3382 let firstof first active =
3383 if first > active || abs (first - active) > fstate.maxrows - 1
3384 then max 0 (active - (fstate.maxrows/2))
3385 else first
3388 let calcfirst first active =
3389 if active > first
3390 then
3391 let rows = active - first in
3392 if rows > fstate.maxrows then active - fstate.maxrows else first
3393 else active
3396 let scrollph y maxy =
3397 let sh = float (maxy + state.winh) /. float state.winh in
3398 let sh = float state.winh /. sh in
3399 let sh = max sh (float conf.scrollh) in
3401 let percent = float y /. float maxy in
3402 let position = (float state.winh -. sh) *. percent in
3404 let position =
3405 if position +. sh > float state.winh
3406 then float state.winh -. sh
3407 else position
3409 position, sh;
3412 let coe s = (s :> uioh);;
3414 class listview ~(source:lvsource) ~trusted ~modehash =
3415 object (self)
3416 val m_pan = source#getpan
3417 val m_first = source#getfirst
3418 val m_active = source#getactive
3419 val m_qsearch = source#getqsearch
3420 val m_prev_uioh = state.uioh
3422 method private elemunder y =
3423 let n = y / (fstate.fontsize+1) in
3424 if m_first + n < source#getitemcount
3425 then (
3426 if source#hasaction (m_first + n)
3427 then Some (m_first + n)
3428 else None
3430 else None
3432 method display =
3433 Gl.enable `blend;
3434 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3435 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3436 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3437 GlDraw.color (1., 1., 1.);
3438 Gl.enable `texture_2d;
3439 let fs = fstate.fontsize in
3440 let nfs = fs + 1 in
3441 let ww = fstate.wwidth in
3442 let tabw = 30.0*.ww in
3443 let itemcount = source#getitemcount in
3444 let rec loop row =
3445 if (row - m_first) > fstate.maxrows
3446 then ()
3447 else (
3448 if row >= 0 && row < itemcount
3449 then (
3450 let (s, level) = source#getitem row in
3451 let y = (row - m_first) * nfs in
3452 let x = 5.0 +. float (level + m_pan) *. ww in
3453 if row = m_active
3454 then (
3455 Gl.disable `texture_2d;
3456 GlDraw.polygon_mode `both `line;
3457 let alpha = if source#hasaction row then 0.9 else 0.3 in
3458 GlDraw.color (1., 1., 1.) ~alpha;
3459 GlDraw.rect (1., float (y + 1))
3460 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3461 GlDraw.polygon_mode `both `fill;
3462 GlDraw.color (1., 1., 1.);
3463 Gl.enable `texture_2d;
3466 let drawtabularstring s =
3467 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3468 if trusted
3469 then
3470 let tabpos = try String.index s '\t' with Not_found -> -1 in
3471 if tabpos > 0
3472 then
3473 let len = String.length s - tabpos - 1 in
3474 let s1 = String.sub s 0 tabpos
3475 and s2 = String.sub s (tabpos + 1) len in
3476 let nx = drawstr x s1 in
3477 let sw = nx -. x in
3478 let x = x +. (max tabw sw) in
3479 drawstr x s2
3480 else
3481 drawstr x s
3482 else
3483 drawstr x s
3485 let _ = drawtabularstring s in
3486 loop (row+1)
3490 loop m_first;
3491 Gl.disable `blend;
3492 Gl.disable `texture_2d;
3494 method updownlevel incr =
3495 let len = source#getitemcount in
3496 let curlevel =
3497 if m_active >= 0 && m_active < len
3498 then snd (source#getitem m_active)
3499 else -1
3501 let rec flow i =
3502 if i = len then i-1 else if i = -1 then 0 else
3503 let _, l = source#getitem i in
3504 if l != curlevel then i else flow (i+incr)
3506 let active = flow m_active in
3507 let first = calcfirst m_first active in
3508 G.postRedisplay "outline updownlevel";
3509 {< m_active = active; m_first = first >}
3511 method private key1 key mask =
3512 let set1 active first qsearch =
3513 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3515 let search active pattern incr =
3516 let active = if active = -1 then m_first else active in
3517 let dosearch re =
3518 let rec loop n =
3519 if n >= 0 && n < source#getitemcount
3520 then (
3521 let s, _ = source#getitem n in
3523 (try ignore (Str.search_forward re s 0); true
3524 with Not_found -> false)
3525 then Some n
3526 else loop (n + incr)
3528 else None
3530 loop active
3533 let re = Str.regexp_case_fold pattern in
3534 dosearch re
3535 with Failure s ->
3536 state.text <- s;
3537 None
3539 let itemcount = source#getitemcount in
3540 let find start incr =
3541 let rec find i =
3542 if i = -1 || i = itemcount
3543 then -1
3544 else (
3545 if source#hasaction i
3546 then i
3547 else find (i + incr)
3550 find start
3552 let set active first =
3553 let first = bound first 0 (itemcount - fstate.maxrows) in
3554 state.text <- "";
3555 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3557 let navigate incr =
3558 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3559 let active, first =
3560 let incr1 = if incr > 0 then 1 else -1 in
3561 if isvisible m_first m_active
3562 then
3563 let next =
3564 let next = m_active + incr in
3565 let next =
3566 if next < 0 || next >= itemcount
3567 then -1
3568 else find next incr1
3570 if abs (m_active - next) > fstate.maxrows
3571 then -1
3572 else next
3574 if next = -1
3575 then
3576 let first = m_first + incr in
3577 let first = bound first 0 (itemcount - fstate.maxrows) in
3578 let next =
3579 let next = m_active + incr in
3580 let next = bound next 0 (itemcount - 1) in
3581 find next ~-incr1
3583 let active =
3584 if next = -1
3585 then m_active
3586 else (
3587 if isvisible first next
3588 then next
3589 else m_active
3592 active, first
3593 else
3594 let first = min next m_first in
3595 let first =
3596 if abs (next - first) > fstate.maxrows
3597 then first + incr
3598 else first
3600 next, first
3601 else
3602 let first = m_first + incr in
3603 let first = bound first 0 (itemcount - 1) in
3604 let active =
3605 let next = m_active + incr in
3606 let next = bound next 0 (itemcount - 1) in
3607 let next = find next incr1 in
3608 let active =
3609 if next = -1 || abs (m_active - first) > fstate.maxrows
3610 then (
3611 let active = if m_active = -1 then next else m_active in
3612 active
3614 else next
3616 if isvisible first active
3617 then active
3618 else -1
3620 active, first
3622 G.postRedisplay "listview navigate";
3623 set active first;
3625 match key with
3626 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3627 let incr = if key = 0x72 then -1 else 1 in
3628 let active, first =
3629 match search (m_active + incr) m_qsearch incr with
3630 | None ->
3631 state.text <- m_qsearch ^ " [not found]";
3632 m_active, m_first
3633 | Some active ->
3634 state.text <- m_qsearch;
3635 active, firstof m_first active
3637 G.postRedisplay "listview ctrl-r/s";
3638 set1 active first m_qsearch;
3640 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3641 if m_active >= 0 && m_active < source#getitemcount
3642 then (
3643 let s, _ = source#getitem m_active in
3644 selstring s;
3646 coe self
3648 | 0xff08 -> (* backspace *)
3649 if emptystr m_qsearch
3650 then coe self
3651 else (
3652 let qsearch = withoutlastutf8 m_qsearch in
3653 if emptystr qsearch
3654 then (
3655 state.text <- "";
3656 G.postRedisplay "listview empty qsearch";
3657 set1 m_active m_first "";
3659 else
3660 let active, first =
3661 match search m_active qsearch ~-1 with
3662 | None ->
3663 state.text <- qsearch ^ " [not found]";
3664 m_active, m_first
3665 | Some active ->
3666 state.text <- qsearch;
3667 active, firstof m_first active
3669 G.postRedisplay "listview backspace qsearch";
3670 set1 active first qsearch
3673 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3674 let pattern = m_qsearch ^ toutf8 key in
3675 let active, first =
3676 match search m_active pattern 1 with
3677 | None ->
3678 state.text <- pattern ^ " [not found]";
3679 m_active, m_first
3680 | Some active ->
3681 state.text <- pattern;
3682 active, firstof m_first active
3684 G.postRedisplay "listview qsearch add";
3685 set1 active first pattern;
3687 | 0xff1b -> (* escape *)
3688 state.text <- "";
3689 if emptystr m_qsearch
3690 then (
3691 G.postRedisplay "list view escape";
3692 begin
3693 match
3694 source#exit (coe self) true m_active m_first m_pan m_qsearch
3695 with
3696 | None -> m_prev_uioh
3697 | Some uioh -> uioh
3700 else (
3701 G.postRedisplay "list view kill qsearch";
3702 source#setqsearch "";
3703 coe {< m_qsearch = "" >}
3706 | 0xff0d | 0xff8d -> (* (kp) enter *)
3707 state.text <- "";
3708 let self = {< m_qsearch = "" >} in
3709 source#setqsearch "";
3710 let opt =
3711 G.postRedisplay "listview enter";
3712 if m_active >= 0 && m_active < source#getitemcount
3713 then (
3714 source#exit (coe self) false m_active m_first m_pan "";
3716 else (
3717 source#exit (coe self) true m_active m_first m_pan "";
3720 begin match opt with
3721 | None -> m_prev_uioh
3722 | Some uioh -> uioh
3725 | 0xff9f | 0xffff -> (* (kp) delete *)
3726 coe self
3728 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3729 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3730 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3731 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3733 | 0xff53 | 0xff98 -> (* (kp) right *)
3734 state.text <- "";
3735 G.postRedisplay "listview right";
3736 coe {< m_pan = m_pan - 1 >}
3738 | 0xff51 | 0xff96 -> (* (kp) left *)
3739 state.text <- "";
3740 G.postRedisplay "listview left";
3741 coe {< m_pan = m_pan + 1 >}
3743 | 0xff50 | 0xff95 -> (* (kp) home *)
3744 let active = find 0 1 in
3745 G.postRedisplay "listview home";
3746 set active 0;
3748 | 0xff57 | 0xff9c -> (* (kp) end *)
3749 let first = max 0 (itemcount - fstate.maxrows) in
3750 let active = find (itemcount - 1) ~-1 in
3751 G.postRedisplay "listview end";
3752 set active first;
3754 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3755 coe self
3757 | _ ->
3758 dolog "listview unknown key %#x" key; coe self
3760 method key key mask =
3761 match state.mode with
3762 | Textentry te -> textentrykeyboard key mask te; coe self
3763 | _ -> self#key1 key mask
3765 method button button down x y _ =
3766 let opt =
3767 match button with
3768 | 1 when x > state.winw - conf.scrollbw ->
3769 G.postRedisplay "listview scroll";
3770 if down
3771 then
3772 let _, position, sh = self#scrollph in
3773 if y > truncate position && y < truncate (position +. sh)
3774 then (
3775 state.mstate <- Mscrolly;
3776 Some (coe self)
3778 else
3779 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3780 let first = truncate (s *. float source#getitemcount) in
3781 let first = min source#getitemcount first in
3782 Some (coe {< m_first = first; m_active = first >})
3783 else (
3784 state.mstate <- Mnone;
3785 Some (coe self);
3787 | 1 when not down ->
3788 begin match self#elemunder y with
3789 | Some n ->
3790 G.postRedisplay "listview click";
3791 source#exit
3792 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3793 | _ ->
3794 Some (coe self)
3796 | n when (n == 4 || n == 5) && not down ->
3797 let len = source#getitemcount in
3798 let first =
3799 if n = 5 && m_first + fstate.maxrows >= len
3800 then
3801 m_first
3802 else
3803 let first = m_first + (if n == 4 then -1 else 1) in
3804 bound first 0 (len - 1)
3806 G.postRedisplay "listview wheel";
3807 Some (coe {< m_first = first >})
3808 | n when (n = 6 || n = 7) && not down ->
3809 let inc = if n = 7 then -1 else 1 in
3810 G.postRedisplay "listview hwheel";
3811 Some (coe {< m_pan = m_pan + inc >})
3812 | _ ->
3813 Some (coe self)
3815 match opt with
3816 | None -> m_prev_uioh
3817 | Some uioh -> uioh
3819 method motion _ y =
3820 match state.mstate with
3821 | Mscrolly ->
3822 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3823 let first = truncate (s *. float source#getitemcount) in
3824 let first = min source#getitemcount first in
3825 G.postRedisplay "listview motion";
3826 coe {< m_first = first; m_active = first >}
3827 | _ -> coe self
3829 method pmotion x y =
3830 if x < state.winw - conf.scrollbw
3831 then
3832 let n =
3833 match self#elemunder y with
3834 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3835 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3837 let o =
3838 if n != m_active
3839 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3840 else self
3842 coe o
3843 else (
3844 Wsi.setcursor Wsi.CURSOR_INHERIT;
3845 coe self
3848 method infochanged _ = ()
3850 method scrollpw = (0, 0.0, 0.0)
3851 method scrollph =
3852 let nfs = fstate.fontsize + 1 in
3853 let y = m_first * nfs in
3854 let itemcount = source#getitemcount in
3855 let maxi = max 0 (itemcount - fstate.maxrows) in
3856 let maxy = maxi * nfs in
3857 let p, h = scrollph y maxy in
3858 conf.scrollbw, p, h
3860 method modehash = modehash
3861 method eformsgs = false
3862 end;;
3864 class outlinelistview ~source =
3865 let settext autonarrow s =
3866 if autonarrow
3867 then state.text <- "[" ^ s ^ "]"
3868 else state.text <- s
3870 object (self)
3871 inherit listview
3872 ~source:(source :> lvsource)
3873 ~trusted:false
3874 ~modehash:(findkeyhash conf "outline")
3875 as super
3877 val m_autonarrow = false
3879 method key key mask =
3880 let maxrows =
3881 if emptystr state.text
3882 then fstate.maxrows
3883 else fstate.maxrows - 2
3885 let calcfirst first active =
3886 if active > first
3887 then
3888 let rows = active - first in
3889 if rows > maxrows then active - maxrows else first
3890 else active
3892 let navigate incr =
3893 let active = m_active + incr in
3894 let active = bound active 0 (source#getitemcount - 1) in
3895 let first = calcfirst m_first active in
3896 G.postRedisplay "outline navigate";
3897 coe {< m_active = active; m_first = first >}
3899 let navscroll first =
3900 let active =
3901 let dist = m_active - first in
3902 if dist < 0
3903 then first
3904 else (
3905 if dist < maxrows
3906 then m_active
3907 else first + maxrows
3910 G.postRedisplay "outline navscroll";
3911 coe {< m_first = first; m_active = active >}
3913 let ctrl = Wsi.withctrl mask in
3914 match key with
3915 | 97 when ctrl -> (* ctrl-a *)
3916 if m_autonarrow
3917 then source#denarrow
3918 else source#narrow m_qsearch;
3919 settext (not m_autonarrow) m_qsearch;
3920 G.postRedisplay "toggle auto narrowing";
3921 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3923 | 110 when ctrl -> (* ctrl-n *)
3924 source#narrow m_qsearch;
3925 if not m_autonarrow
3926 then source#add_narrow_pattern m_qsearch;
3927 G.postRedisplay "outline ctrl-n";
3928 coe {< m_first = 0; m_active = 0 >}
3930 | 117 when ctrl -> (* ctrl-u *)
3931 source#del_narrow_pattern;
3932 let pattern = source#renarrow in
3933 G.postRedisplay "outline ctrl-u";
3934 let text =
3935 if emptystr pattern then "" else "Narrowed to " ^ pattern
3937 settext m_autonarrow text;
3938 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
3940 | 108 when ctrl -> (* ctrl-l *)
3941 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3942 G.postRedisplay "outline ctrl-l";
3943 coe {< m_first = first >}
3945 | 0xff1b -> (* escape *)
3946 let o = super#key key mask in
3947 if m_autonarrow
3948 then (
3949 if nonemptystr m_qsearch
3950 then (
3951 source#add_narrow_pattern m_qsearch;
3952 settext true "";
3957 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
3958 if nonemptystr m_qsearch
3959 then source#add_narrow_pattern m_qsearch;
3960 super#key key mask
3962 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3963 let pattern = m_qsearch ^ toutf8 key in
3964 G.postRedisplay "outlinelistview autonarrow add";
3965 source#narrow pattern;
3966 settext true pattern;
3967 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3969 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
3970 if emptystr m_qsearch
3971 then coe self
3972 else
3973 let pattern = withoutlastutf8 m_qsearch in
3974 G.postRedisplay "outlinelistview autonarrow backspace";
3975 ignore (source#renarrow);
3976 source#narrow pattern;
3977 settext true pattern;
3978 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3980 | 0xff9f | 0xffff -> (* (kp) delete *)
3981 source#remove m_active;
3982 G.postRedisplay "outline delete";
3983 let active = max 0 (m_active-1) in
3984 coe {< m_first = firstof m_first active;
3985 m_active = active >}
3987 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
3988 navscroll (max 0 (m_first - 1))
3990 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
3991 navscroll (min (source#getitemcount - 1) (m_first + 1))
3993 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3994 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3995 | 0xff55 | 0xff9a -> (* (kp) prior *)
3996 navigate ~-(fstate.maxrows)
3997 | 0xff56 | 0xff9b -> (* (kp) next *)
3998 navigate fstate.maxrows
4000 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4001 let o =
4002 if ctrl
4003 then (
4004 G.postRedisplay "outline ctrl right";
4005 {< m_pan = m_pan + 1 >}
4007 else self#updownlevel 1
4009 coe o
4011 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4012 let o =
4013 if ctrl
4014 then (
4015 G.postRedisplay "outline ctrl left";
4016 {< m_pan = m_pan - 1 >}
4018 else self#updownlevel ~-1
4020 coe o
4022 | 0xff50 | 0xff95 -> (* (kp) home *)
4023 G.postRedisplay "outline home";
4024 coe {< m_first = 0; m_active = 0 >}
4026 | 0xff57 | 0xff9c -> (* (kp) end *)
4027 let active = source#getitemcount - 1 in
4028 let first = max 0 (active - fstate.maxrows) in
4029 G.postRedisplay "outline end";
4030 coe {< m_active = active; m_first = first >}
4032 | _ -> super#key key mask
4035 let outlinesource usebookmarks =
4036 let empty = [||] in
4037 (object (self)
4038 inherit lvsourcebase
4039 val mutable m_items = empty
4040 val mutable m_orig_items = empty
4041 val mutable m_narrow_patterns = []
4042 val mutable m_hadremovals = false
4044 method getitemcount =
4045 Array.length m_items + (if m_hadremovals then 1 else 0)
4047 method getitem n =
4048 if n == Array.length m_items && m_hadremovals
4049 then
4050 ("[Confirm removal]", 0)
4051 else
4052 let s, n, _ = m_items.(n) in
4053 (s, n)
4055 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4056 ignore (uioh, first, qsearch);
4057 let confrimremoval = m_hadremovals && active = Array.length m_items in
4058 let items =
4059 if m_narrow_patterns = []
4060 then m_orig_items
4061 else m_items
4063 if not cancel
4064 then (
4065 if not confrimremoval
4066 then (
4067 let _, _, ((pageno, y, _) as anchor) = m_items.(active) in
4068 let y = getanchory
4069 (if conf.presentation then (pageno, y, 1.0) else anchor)
4071 gotoghyll y;
4072 m_items <- items;
4074 else (
4075 state.bookmarks <- Array.to_list m_items;
4076 m_orig_items <- m_items;
4079 else m_items <- items;
4080 m_pan <- pan;
4081 None
4083 method hasaction _ = true
4085 method greetmsg =
4086 if Array.length m_items != Array.length m_orig_items
4087 then
4088 let s = String.concat " --> " (List.rev m_narrow_patterns) in
4089 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4090 else ""
4092 method narrow pattern =
4093 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4094 match reopt with
4095 | None -> ()
4096 | Some re ->
4097 let rec loop accu n =
4098 if n = -1
4099 then m_items <- Array.of_list accu
4100 else
4101 let (s, _, _) as o = m_items.(n) in
4102 let accu =
4103 if (try ignore (Str.search_forward re s 0); true
4104 with Not_found -> false)
4105 then o :: accu
4106 else accu
4108 loop accu (n-1)
4110 loop [] (Array.length m_items - 1)
4112 method denarrow =
4113 m_orig_items <- (
4114 if usebookmarks
4115 then Array.of_list state.bookmarks
4116 else state.outlines
4118 m_items <- m_orig_items
4120 method remove m =
4121 if usebookmarks
4122 then
4123 if m >= 0 && m < Array.length m_items
4124 then (
4125 m_hadremovals <- true;
4126 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4127 let n = if n >= m then n+1 else n in
4128 m_items.(n)
4132 method add_narrow_pattern pattern =
4133 m_narrow_patterns <- pattern :: m_narrow_patterns
4135 method del_narrow_pattern =
4136 match m_narrow_patterns with
4137 | _ :: rest -> m_narrow_patterns <- rest
4138 | [] -> ()
4140 method renarrow =
4141 self#denarrow;
4142 match m_narrow_patterns with
4143 | pattern :: [] -> self#narrow pattern; pattern
4144 | list ->
4145 List.fold_left (fun accu pattern ->
4146 self#narrow pattern;
4147 accu ^ " --> " ^ pattern) "" list
4149 method reset anchor items =
4150 m_hadremovals <- false;
4151 if m_orig_items == empty
4152 then (
4153 m_orig_items <- items;
4154 if m_narrow_patterns == []
4155 then m_items <- items;
4157 let rely = getanchory anchor in
4158 let active =
4159 let rec loop n best bestd =
4160 if n = Array.length m_items
4161 then best
4162 else
4163 let (_, _, anchor) = m_items.(n) in
4164 let orely = getanchory anchor in
4165 let d = abs (orely - rely) in
4166 if d < bestd
4167 then loop (n+1) n d
4168 else loop (n+1) best bestd
4170 loop 0 ~-1 max_int
4172 m_active <- active;
4173 m_first <- firstof m_first active
4174 end)
4177 let enterselector usebookmarks =
4178 let source = outlinesource usebookmarks in
4179 fun errmsg ->
4180 let outlines =
4181 if usebookmarks
4182 then Array.of_list state.bookmarks
4183 else state.outlines
4185 if Array.length outlines = 0
4186 then (
4187 showtext ' ' errmsg;
4189 else (
4190 state.text <- source#greetmsg;
4191 Wsi.setcursor Wsi.CURSOR_INHERIT;
4192 let anchor = getanchor () in
4193 source#reset anchor outlines;
4194 state.uioh <- coe (new outlinelistview ~source);
4195 G.postRedisplay "enter selector";
4199 let enteroutlinemode =
4200 let f = enterselector false in
4201 fun ()-> f "Document has no outline";
4204 let enterbookmarkmode =
4205 let f = enterselector true in
4206 fun () -> f "Document has no bookmarks (yet)";
4209 let color_of_string s =
4210 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4211 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4215 let color_to_string (r, g, b) =
4216 let r = truncate (r *. 256.0)
4217 and g = truncate (g *. 256.0)
4218 and b = truncate (b *. 256.0) in
4219 Printf.sprintf "%d/%d/%d" r g b
4222 let irect_of_string s =
4223 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4226 let irect_to_string (x0,y0,x1,y1) =
4227 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4230 let makecheckers () =
4231 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4232 following to say:
4233 converted by Issac Trotts. July 25, 2002 *)
4234 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4235 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4236 let id = GlTex.gen_texture () in
4237 GlTex.bind_texture `texture_2d id;
4238 GlPix.store (`unpack_alignment 1);
4239 GlTex.image2d image;
4240 List.iter (GlTex.parameter ~target:`texture_2d)
4241 [ `mag_filter `nearest; `min_filter `nearest ];
4245 let setcheckers enabled =
4246 match state.texid with
4247 | None ->
4248 if enabled then state.texid <- Some (makecheckers ())
4250 | Some texid ->
4251 if not enabled
4252 then (
4253 GlTex.delete_texture texid;
4254 state.texid <- None;
4258 let int_of_string_with_suffix s =
4259 let l = String.length s in
4260 let s1, shift =
4261 if l > 1
4262 then
4263 let suffix = Char.lowercase s.[l-1] in
4264 match suffix with
4265 | 'k' -> String.sub s 0 (l-1), 10
4266 | 'm' -> String.sub s 0 (l-1), 20
4267 | 'g' -> String.sub s 0 (l-1), 30
4268 | _ -> s, 0
4269 else s, 0
4271 let n = int_of_string s1 in
4272 let m = n lsl shift in
4273 if m < 0 || m < n
4274 then raise (Failure "value too large")
4275 else m
4278 let string_with_suffix_of_int n =
4279 if n = 0
4280 then "0"
4281 else
4282 let n, s =
4283 if n land ((1 lsl 30) - 1) = 0
4284 then n lsr 30, "G"
4285 else (
4286 if n land ((1 lsl 20) - 1) = 0
4287 then n lsr 20, "M"
4288 else (
4289 if n land ((1 lsl 10) - 1) = 0
4290 then n lsr 10, "K"
4291 else n, ""
4295 let rec loop s n =
4296 let h = n mod 1000 in
4297 let n = n / 1000 in
4298 if n = 0
4299 then string_of_int h ^ s
4300 else (
4301 let s = Printf.sprintf "_%03d%s" h s in
4302 loop s n
4305 loop "" n ^ s;
4308 let defghyllscroll = (40, 8, 32);;
4309 let ghyllscroll_of_string s =
4310 let (n, a, b) as nab =
4311 if s = "default"
4312 then defghyllscroll
4313 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4315 if n <= a || n <= b || a >= b
4316 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4317 n a b;
4318 nab;
4321 let ghyllscroll_to_string ((n, a, b) as nab) =
4322 if nab = defghyllscroll
4323 then "default"
4324 else Printf.sprintf "%d,%d,%d" n a b;
4327 let describe_location () =
4328 let fn = page_of_y state.y in
4329 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4330 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4331 let percent =
4332 if maxy <= 0
4333 then 100.
4334 else (100. *. (float state.y /. float maxy))
4336 if fn = ln
4337 then
4338 Printf.sprintf "page %d of %d [%.2f%%]"
4339 (fn+1) state.pagecount percent
4340 else
4341 Printf.sprintf
4342 "pages %d-%d of %d [%.2f%%]"
4343 (fn+1) (ln+1) state.pagecount percent
4346 let setpresentationmode v =
4347 let n = page_of_y state.y in
4348 state.anchor <- (n, 0.0, 1.0);
4349 conf.presentation <- v;
4350 if conf.fitmodel = FitPage
4351 then reqlayout conf.angle conf.fitmodel;
4352 represent ();
4355 let enterinfomode =
4356 let btos b = if b then "\xe2\x88\x9a" else "" in
4357 let showextended = ref false in
4358 let leave mode = function
4359 | Confirm -> state.mode <- mode
4360 | Cancel -> state.mode <- mode in
4361 let src =
4362 (object
4363 val mutable m_first_time = true
4364 val mutable m_l = []
4365 val mutable m_a = [||]
4366 val mutable m_prev_uioh = nouioh
4367 val mutable m_prev_mode = View
4369 inherit lvsourcebase
4371 method reset prev_mode prev_uioh =
4372 m_a <- Array.of_list (List.rev m_l);
4373 m_l <- [];
4374 m_prev_mode <- prev_mode;
4375 m_prev_uioh <- prev_uioh;
4376 if m_first_time
4377 then (
4378 let rec loop n =
4379 if n >= Array.length m_a
4380 then ()
4381 else
4382 match m_a.(n) with
4383 | _, _, _, Action _ -> m_active <- n
4384 | _ -> loop (n+1)
4386 loop 0;
4387 m_first_time <- false;
4390 method int name get set =
4391 m_l <-
4392 (name, `int get, 1, Action (
4393 fun u ->
4394 let ondone s =
4395 try set (int_of_string s)
4396 with exn ->
4397 state.text <- Printf.sprintf "bad integer `%s': %s"
4398 s (exntos exn)
4400 state.text <- "";
4401 let te = name ^ ": ", "", None, intentry, ondone, true in
4402 state.mode <- Textentry (te, leave m_prev_mode);
4404 )) :: m_l
4406 method int_with_suffix name get set =
4407 m_l <-
4408 (name, `intws get, 1, Action (
4409 fun u ->
4410 let ondone s =
4411 try set (int_of_string_with_suffix s)
4412 with exn ->
4413 state.text <- Printf.sprintf "bad integer `%s': %s"
4414 s (exntos exn)
4416 state.text <- "";
4417 let te =
4418 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4420 state.mode <- Textentry (te, leave m_prev_mode);
4422 )) :: m_l
4424 method bool ?(offset=1) ?(btos=btos) name get set =
4425 m_l <-
4426 (name, `bool (btos, get), offset, Action (
4427 fun u ->
4428 let v = get () in
4429 set (not v);
4431 )) :: m_l
4433 method color name get set =
4434 m_l <-
4435 (name, `color get, 1, Action (
4436 fun u ->
4437 let invalid = (nan, nan, nan) in
4438 let ondone s =
4439 let c =
4440 try color_of_string s
4441 with exn ->
4442 state.text <- Printf.sprintf "bad color `%s': %s"
4443 s (exntos exn);
4444 invalid
4446 if c <> invalid
4447 then set c;
4449 let te = name ^ ": ", "", None, textentry, ondone, true in
4450 state.text <- color_to_string (get ());
4451 state.mode <- Textentry (te, leave m_prev_mode);
4453 )) :: m_l
4455 method string name get set =
4456 m_l <-
4457 (name, `string get, 1, Action (
4458 fun u ->
4459 let ondone s = set s in
4460 let te = name ^ ": ", "", None, textentry, ondone, true in
4461 state.mode <- Textentry (te, leave m_prev_mode);
4463 )) :: m_l
4465 method colorspace name get set =
4466 m_l <-
4467 (name, `string get, 1, Action (
4468 fun _ ->
4469 let source =
4470 (object
4471 inherit lvsourcebase
4473 initializer
4474 m_active <- CSTE.to_int conf.colorspace;
4475 m_first <- 0;
4477 method getitemcount =
4478 Array.length CSTE.names
4479 method getitem n =
4480 (CSTE.names.(n), 0)
4481 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4482 ignore (uioh, first, pan, qsearch);
4483 if not cancel then set active;
4484 None
4485 method hasaction _ = true
4486 end)
4488 state.text <- "";
4489 let modehash = findkeyhash conf "info" in
4490 coe (new listview ~source ~trusted:true ~modehash)
4491 )) :: m_l
4493 method paxmark name get set =
4494 m_l <-
4495 (name, `string get, 1, Action (
4496 fun _ ->
4497 let source =
4498 (object
4499 inherit lvsourcebase
4501 initializer
4502 m_active <- MTE.to_int conf.paxmark;
4503 m_first <- 0;
4505 method getitemcount = Array.length MTE.names
4506 method getitem n = (MTE.names.(n), 0)
4507 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4508 ignore (uioh, first, pan, qsearch);
4509 if not cancel then set active;
4510 None
4511 method hasaction _ = true
4512 end)
4514 state.text <- "";
4515 let modehash = findkeyhash conf "info" in
4516 coe (new listview ~source ~trusted:true ~modehash)
4517 )) :: m_l
4519 method fitmodel name get set =
4520 m_l <-
4521 (name, `string get, 1, Action (
4522 fun _ ->
4523 let source =
4524 (object
4525 inherit lvsourcebase
4527 initializer
4528 m_active <- FMTE.to_int conf.fitmodel;
4529 m_first <- 0;
4531 method getitemcount = Array.length FMTE.names
4532 method getitem n = (FMTE.names.(n), 0)
4533 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4534 ignore (uioh, first, pan, qsearch);
4535 if not cancel then set active;
4536 None
4537 method hasaction _ = true
4538 end)
4540 state.text <- "";
4541 let modehash = findkeyhash conf "info" in
4542 coe (new listview ~source ~trusted:true ~modehash)
4543 )) :: m_l
4545 method caption s offset =
4546 m_l <- (s, `empty, offset, Noaction) :: m_l
4548 method caption2 s f offset =
4549 m_l <- (s, `string f, offset, Noaction) :: m_l
4551 method getitemcount = Array.length m_a
4553 method getitem n =
4554 let tostr = function
4555 | `int f -> string_of_int (f ())
4556 | `intws f -> string_with_suffix_of_int (f ())
4557 | `string f -> f ()
4558 | `color f -> color_to_string (f ())
4559 | `bool (btos, f) -> btos (f ())
4560 | `empty -> ""
4562 let name, t, offset, _ = m_a.(n) in
4563 ((let s = tostr t in
4564 if nonemptystr s
4565 then Printf.sprintf "%s\t%s" name s
4566 else name),
4567 offset)
4569 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4570 let uiohopt =
4571 if not cancel
4572 then (
4573 m_qsearch <- qsearch;
4574 let uioh =
4575 match m_a.(active) with
4576 | _, _, _, Action f -> f uioh
4577 | _ -> uioh
4579 Some uioh
4581 else None
4583 m_active <- active;
4584 m_first <- first;
4585 m_pan <- pan;
4586 uiohopt
4588 method hasaction n =
4589 match m_a.(n) with
4590 | _, _, _, Action _ -> true
4591 | _ -> false
4592 end)
4594 let rec fillsrc prevmode prevuioh =
4595 let sep () = src#caption "" 0 in
4596 let colorp name get set =
4597 src#string name
4598 (fun () -> color_to_string (get ()))
4599 (fun v ->
4601 let c = color_of_string v in
4602 set c
4603 with exn ->
4604 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4607 let oldmode = state.mode in
4608 let birdseye = isbirdseye state.mode in
4610 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4612 src#bool "presentation mode"
4613 (fun () -> conf.presentation)
4614 (fun v -> setpresentationmode v);
4616 src#bool "ignore case in searches"
4617 (fun () -> conf.icase)
4618 (fun v -> conf.icase <- v);
4620 src#bool "preload"
4621 (fun () -> conf.preload)
4622 (fun v -> conf.preload <- v);
4624 src#bool "highlight links"
4625 (fun () -> conf.hlinks)
4626 (fun v -> conf.hlinks <- v);
4628 src#bool "under info"
4629 (fun () -> conf.underinfo)
4630 (fun v -> conf.underinfo <- v);
4632 src#bool "persistent bookmarks"
4633 (fun () -> conf.savebmarks)
4634 (fun v -> conf.savebmarks <- v);
4636 src#fitmodel "fit model"
4637 (fun () -> FMTE.to_string conf.fitmodel)
4638 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4640 src#bool "trim margins"
4641 (fun () -> conf.trimmargins)
4642 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4644 src#bool "persistent location"
4645 (fun () -> conf.jumpback)
4646 (fun v -> conf.jumpback <- v);
4648 sep ();
4649 src#int "inter-page space"
4650 (fun () -> conf.interpagespace)
4651 (fun n ->
4652 conf.interpagespace <- n;
4653 docolumns conf.columns;
4654 let pageno, py =
4655 match state.layout with
4656 | [] -> 0, 0
4657 | l :: _ ->
4658 l.pageno, l.pagey
4660 state.maxy <- calcheight ();
4661 let y = getpagey pageno in
4662 gotoy (y + py)
4665 src#int "page bias"
4666 (fun () -> conf.pagebias)
4667 (fun v -> conf.pagebias <- v);
4669 src#int "scroll step"
4670 (fun () -> conf.scrollstep)
4671 (fun n -> conf.scrollstep <- n);
4673 src#int "horizontal scroll step"
4674 (fun () -> conf.hscrollstep)
4675 (fun v -> conf.hscrollstep <- v);
4677 src#int "auto scroll step"
4678 (fun () ->
4679 match state.autoscroll with
4680 | Some step -> step
4681 | _ -> conf.autoscrollstep)
4682 (fun n ->
4683 if state.autoscroll <> None
4684 then state.autoscroll <- Some n;
4685 conf.autoscrollstep <- n);
4687 src#int "zoom"
4688 (fun () -> truncate (conf.zoom *. 100.))
4689 (fun v -> setzoom ((float v) /. 100.));
4691 src#int "rotation"
4692 (fun () -> conf.angle)
4693 (fun v -> reqlayout v conf.fitmodel);
4695 src#int "scroll bar width"
4696 (fun () -> conf.scrollbw)
4697 (fun v ->
4698 conf.scrollbw <- v;
4699 reshape state.winw state.winh;
4702 src#int "scroll handle height"
4703 (fun () -> conf.scrollh)
4704 (fun v -> conf.scrollh <- v;);
4706 src#int "thumbnail width"
4707 (fun () -> conf.thumbw)
4708 (fun v ->
4709 conf.thumbw <- min 4096 v;
4710 match oldmode with
4711 | Birdseye beye ->
4712 leavebirdseye beye false;
4713 enterbirdseye ()
4714 | _ -> ()
4717 let mode = state.mode in
4718 src#string "columns"
4719 (fun () ->
4720 match conf.columns with
4721 | Csingle _ -> "1"
4722 | Cmulti (multi, _) -> multicolumns_to_string multi
4723 | Csplit (count, _) -> "-" ^ string_of_int count
4725 (fun v ->
4726 let n, a, b = multicolumns_of_string v in
4727 setcolumns mode n a b);
4729 sep ();
4730 src#caption "Pixmap cache" 0;
4731 src#int_with_suffix "size (advisory)"
4732 (fun () -> conf.memlimit)
4733 (fun v -> conf.memlimit <- v);
4735 src#caption2 "used"
4736 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4737 (string_with_suffix_of_int state.memused)
4738 (Hashtbl.length state.tilemap)) 1;
4740 sep ();
4741 src#caption "Layout" 0;
4742 src#caption2 "Dimension"
4743 (fun () ->
4744 Printf.sprintf "%dx%d (virtual %dx%d)"
4745 state.winw state.winh
4746 state.w state.maxy)
4748 if conf.debug
4749 then
4750 src#caption2 "Position" (fun () ->
4751 Printf.sprintf "%dx%d" state.x state.y
4753 else
4754 src#caption2 "Position" (fun () -> describe_location ()) 1
4757 sep ();
4758 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4759 "Save these parameters as global defaults at exit"
4760 (fun () -> conf.bedefault)
4761 (fun v -> conf.bedefault <- v)
4764 sep ();
4765 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4766 src#bool ~offset:0 ~btos "Extended parameters"
4767 (fun () -> !showextended)
4768 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4769 if !showextended
4770 then (
4771 src#bool "checkers"
4772 (fun () -> conf.checkers)
4773 (fun v -> conf.checkers <- v; setcheckers v);
4774 src#bool "update cursor"
4775 (fun () -> conf.updatecurs)
4776 (fun v -> conf.updatecurs <- v);
4777 src#bool "verbose"
4778 (fun () -> conf.verbose)
4779 (fun v -> conf.verbose <- v);
4780 src#bool "invert colors"
4781 (fun () -> conf.invert)
4782 (fun v -> conf.invert <- v);
4783 src#bool "max fit"
4784 (fun () -> conf.maxhfit)
4785 (fun v -> conf.maxhfit <- v);
4786 src#bool "redirect stderr"
4787 (fun () -> conf.redirectstderr)
4788 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4789 src#bool "pax mode"
4790 (fun () -> conf.pax != None)
4791 (fun v ->
4792 if v
4793 then conf.pax <- Some (ref (now (), 0, 0))
4794 else conf.pax <- None);
4795 src#string "uri launcher"
4796 (fun () -> conf.urilauncher)
4797 (fun v -> conf.urilauncher <- v);
4798 src#string "path launcher"
4799 (fun () -> conf.pathlauncher)
4800 (fun v -> conf.pathlauncher <- v);
4801 src#string "tile size"
4802 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4803 (fun v ->
4805 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4806 conf.tilew <- max 64 w;
4807 conf.tileh <- max 64 h;
4808 flushtiles ();
4809 with exn ->
4810 state.text <- Printf.sprintf "bad tile size `%s': %s"
4811 v (exntos exn)
4813 src#int "texture count"
4814 (fun () -> conf.texcount)
4815 (fun v ->
4816 if realloctexts v
4817 then conf.texcount <- v
4818 else showtext '!' " Failed to set texture count please retry later"
4820 src#int "slice height"
4821 (fun () -> conf.sliceheight)
4822 (fun v ->
4823 conf.sliceheight <- v;
4824 wcmd "sliceh %d" conf.sliceheight;
4826 src#int "anti-aliasing level"
4827 (fun () -> conf.aalevel)
4828 (fun v ->
4829 conf.aalevel <- bound v 0 8;
4830 state.anchor <- getanchor ();
4831 opendoc state.path state.password;
4833 src#string "page scroll scaling factor"
4834 (fun () -> string_of_float conf.pgscale)
4835 (fun v ->
4837 let s = float_of_string v in
4838 conf.pgscale <- s
4839 with exn ->
4840 state.text <- Printf.sprintf
4841 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4844 src#int "ui font size"
4845 (fun () -> fstate.fontsize)
4846 (fun v -> setfontsize (bound v 5 100));
4847 src#int "hint font size"
4848 (fun () -> conf.hfsize)
4849 (fun v -> conf.hfsize <- bound v 5 100);
4850 colorp "background color"
4851 (fun () -> conf.bgcolor)
4852 (fun v -> conf.bgcolor <- v);
4853 src#bool "crop hack"
4854 (fun () -> conf.crophack)
4855 (fun v -> conf.crophack <- v);
4856 src#string "trim fuzz"
4857 (fun () -> irect_to_string conf.trimfuzz)
4858 (fun v ->
4860 conf.trimfuzz <- irect_of_string v;
4861 if conf.trimmargins
4862 then settrim true conf.trimfuzz;
4863 with exn ->
4864 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4866 src#string "throttle"
4867 (fun () ->
4868 match conf.maxwait with
4869 | None -> "show place holder if page is not ready"
4870 | Some time ->
4871 if time = infinity
4872 then "wait for page to fully render"
4873 else
4874 "wait " ^ string_of_float time
4875 ^ " seconds before showing placeholder"
4877 (fun v ->
4879 let f = float_of_string v in
4880 if f <= 0.0
4881 then conf.maxwait <- None
4882 else conf.maxwait <- Some f
4883 with exn ->
4884 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4886 src#string "ghyll scroll"
4887 (fun () ->
4888 match conf.ghyllscroll with
4889 | None -> ""
4890 | Some nab -> ghyllscroll_to_string nab
4892 (fun v ->
4894 let gs =
4895 if emptystr v
4896 then None
4897 else Some (ghyllscroll_of_string v)
4899 conf.ghyllscroll <- gs
4900 with exn ->
4901 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4903 src#string "selection command"
4904 (fun () -> conf.selcmd)
4905 (fun v -> conf.selcmd <- v);
4906 src#string "synctex command"
4907 (fun () -> conf.stcmd)
4908 (fun v -> conf.stcmd <- v);
4909 src#string "pax command"
4910 (fun () -> conf.paxcmd)
4911 (fun v -> conf.paxcmd <- v);
4912 src#colorspace "color space"
4913 (fun () -> CSTE.to_string conf.colorspace)
4914 (fun v ->
4915 conf.colorspace <- CSTE.of_int v;
4916 wcmd "cs %d" v;
4917 load state.layout;
4919 src#paxmark "pax mark method"
4920 (fun () -> MTE.to_string conf.paxmark)
4921 (fun v -> conf.paxmark <- MTE.of_int v);
4922 if pbousable ()
4923 then
4924 src#bool "use PBO"
4925 (fun () -> conf.usepbo)
4926 (fun v -> conf.usepbo <- v);
4927 src#bool "mouse wheel scrolls pages"
4928 (fun () -> conf.wheelbypage)
4929 (fun v -> conf.wheelbypage <- v);
4930 src#bool "open remote links in a new instance"
4931 (fun () -> conf.riani)
4932 (fun v -> conf.riani <- v);
4935 sep ();
4936 src#caption "Document" 0;
4937 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4938 src#caption2 "Pages"
4939 (fun () -> string_of_int state.pagecount) 1;
4940 src#caption2 "Dimensions"
4941 (fun () -> string_of_int (List.length state.pdims)) 1;
4942 if conf.trimmargins
4943 then (
4944 sep ();
4945 src#caption "Trimmed margins" 0;
4946 src#caption2 "Dimensions"
4947 (fun () -> string_of_int (List.length state.pdims)) 1;
4950 sep ();
4951 src#caption "OpenGL" 0;
4952 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4953 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4955 sep ();
4956 src#caption "Location" 0;
4957 if nonemptystr state.origin
4958 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4959 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4961 src#reset prevmode prevuioh;
4963 fun () ->
4964 state.text <- "";
4965 let prevmode = state.mode
4966 and prevuioh = state.uioh in
4967 fillsrc prevmode prevuioh;
4968 let source = (src :> lvsource) in
4969 let modehash = findkeyhash conf "info" in
4970 state.uioh <- coe (object (self)
4971 inherit listview ~source ~trusted:true ~modehash as super
4972 val mutable m_prevmemused = 0
4973 method infochanged = function
4974 | Memused ->
4975 if m_prevmemused != state.memused
4976 then (
4977 m_prevmemused <- state.memused;
4978 G.postRedisplay "memusedchanged";
4980 | Pdim -> G.postRedisplay "pdimchanged"
4981 | Docinfo -> fillsrc prevmode prevuioh
4983 method key key mask =
4984 if not (Wsi.withctrl mask)
4985 then
4986 match key with
4987 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4988 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4989 | _ -> super#key key mask
4990 else super#key key mask
4991 end);
4992 G.postRedisplay "info";
4995 let enterhelpmode =
4996 let source =
4997 (object
4998 inherit lvsourcebase
4999 method getitemcount = Array.length state.help
5000 method getitem n =
5001 let s, l, _ = state.help.(n) in
5002 (s, l)
5004 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5005 let optuioh =
5006 if not cancel
5007 then (
5008 m_qsearch <- qsearch;
5009 match state.help.(active) with
5010 | _, _, Action f -> Some (f uioh)
5011 | _ -> Some (uioh)
5013 else None
5015 m_active <- active;
5016 m_first <- first;
5017 m_pan <- pan;
5018 optuioh
5020 method hasaction n =
5021 match state.help.(n) with
5022 | _, _, Action _ -> true
5023 | _ -> false
5025 initializer
5026 m_active <- -1
5027 end)
5028 in fun () ->
5029 let modehash = findkeyhash conf "help" in
5030 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5031 G.postRedisplay "help";
5034 let entermsgsmode =
5035 let msgsource =
5036 let re = Str.regexp "[\r\n]" in
5037 (object
5038 inherit lvsourcebase
5039 val mutable m_items = [||]
5041 method getitemcount = 1 + Array.length m_items
5043 method getitem n =
5044 if n = 0
5045 then "[Clear]", 0
5046 else m_items.(n-1), 0
5048 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5049 ignore uioh;
5050 if not cancel
5051 then (
5052 if active = 0
5053 then Buffer.clear state.errmsgs;
5054 m_qsearch <- qsearch;
5056 m_active <- active;
5057 m_first <- first;
5058 m_pan <- pan;
5059 None
5061 method hasaction n =
5062 n = 0
5064 method reset =
5065 state.newerrmsgs <- false;
5066 let l = Str.split re (Buffer.contents state.errmsgs) in
5067 m_items <- Array.of_list l
5069 initializer
5070 m_active <- 0
5071 end)
5072 in fun () ->
5073 state.text <- "";
5074 msgsource#reset;
5075 let source = (msgsource :> lvsource) in
5076 let modehash = findkeyhash conf "listview" in
5077 state.uioh <- coe (object
5078 inherit listview ~source ~trusted:false ~modehash as super
5079 method display =
5080 if state.newerrmsgs
5081 then msgsource#reset;
5082 super#display
5083 end);
5084 G.postRedisplay "msgs";
5087 let quickbookmark ?title () =
5088 match state.layout with
5089 | [] -> ()
5090 | l :: _ ->
5091 let title =
5092 match title with
5093 | None ->
5094 let sec = Unix.gettimeofday () in
5095 let tm = Unix.localtime sec in
5096 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5097 (l.pageno+1)
5098 tm.Unix.tm_mday
5099 tm.Unix.tm_mon
5100 (tm.Unix.tm_year + 1900)
5101 tm.Unix.tm_hour
5102 tm.Unix.tm_min
5103 | Some title -> title
5105 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
5108 let setautoscrollspeed step goingdown =
5109 let incr = max 1 ((abs step) / 2) in
5110 let incr = if goingdown then incr else -incr in
5111 let astep = step + incr in
5112 state.autoscroll <- Some astep;
5115 let gotounder under =
5116 let getpath filename =
5117 let path =
5118 if nonemptystr filename
5119 then
5120 if Filename.is_relative filename
5121 then
5122 let dir = Filename.dirname state.path in
5123 let dir =
5124 if Filename.is_implicit dir
5125 then Filename.concat (Sys.getcwd ()) dir
5126 else dir
5128 Filename.concat dir filename
5129 else filename
5130 else ""
5132 if Sys.file_exists path
5133 then path
5134 else ""
5136 match under with
5137 | Ulinkgoto (pageno, top) ->
5138 if pageno >= 0
5139 then (
5140 addnav ();
5141 gotopage1 pageno top;
5144 | Ulinkuri s ->
5145 gotouri s
5147 | Uremote (filename, pageno) ->
5148 let path = getpath filename in
5149 if nonemptystr path
5150 then (
5151 if conf.riani
5152 then
5153 let command = !selfexec ^ " " ^ path in
5154 try popen command []
5155 with exn ->
5156 Printf.eprintf
5157 "failed to execute `%s': %s\n" command (exntos exn);
5158 flush stderr;
5159 else
5160 let anchor = getanchor () in
5161 let ranchor = state.path, state.password, anchor, state.origin in
5162 state.origin <- "";
5163 state.anchor <- (pageno, 0.0, 0.0);
5164 state.ranchors <- ranchor :: state.ranchors;
5165 opendoc path "";
5167 else showtext '!' ("Could not find " ^ filename)
5169 | Uremotedest (filename, destname) ->
5170 let path = getpath filename in
5171 if nonemptystr path
5172 then (
5173 if conf.riani
5174 then
5175 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
5176 try popen command []
5177 with exn ->
5178 Printf.eprintf
5179 "failed to execute `%s': %s\n" command (exntos exn);
5180 flush stderr;
5181 else
5182 let anchor = getanchor () in
5183 let ranchor = state.path, state.password, anchor, state.origin in
5184 state.origin <- "";
5185 state.nameddest <- destname;
5186 state.ranchors <- ranchor :: state.ranchors;
5187 opendoc path "";
5189 else showtext '!' ("Could not find " ^ filename)
5191 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5194 let canpan () =
5195 match conf.columns with
5196 | Csplit _ -> true
5197 | _ -> state.x != 0 || conf.zoom > 1.0
5200 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5202 let existsinrow pageno (columns, coverA, coverB) p =
5203 let last = ((pageno - coverA) mod columns) + columns in
5204 let rec any = function
5205 | [] -> false
5206 | l :: rest ->
5207 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5208 then p l
5209 else (
5210 if not (p l)
5211 then (if l.pageno = last then false else any rest)
5212 else true
5215 any state.layout
5218 let nextpage () =
5219 match state.layout with
5220 | [] ->
5221 let pageno = page_of_y state.y in
5222 gotoghyll (getpagey (pageno+1))
5223 | l :: rest ->
5224 match conf.columns with
5225 | Csingle _ ->
5226 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5227 then
5228 let y = clamp (pgscale state.winh) in
5229 gotoghyll y
5230 else
5231 let pageno = min (l.pageno+1) (state.pagecount-1) in
5232 gotoghyll (getpagey pageno)
5233 | Cmulti ((c, _, _) as cl, _) ->
5234 if conf.presentation
5235 && (existsinrow l.pageno cl
5236 (fun l -> l.pageh > l.pagey + l.pagevh))
5237 then
5238 let y = clamp (pgscale state.winh) in
5239 gotoghyll y
5240 else
5241 let pageno = min (l.pageno+c) (state.pagecount-1) in
5242 gotoghyll (getpagey pageno)
5243 | Csplit (n, _) ->
5244 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5245 then
5246 let pagey, pageh = getpageyh l.pageno in
5247 let pagey = pagey + pageh * l.pagecol in
5248 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5249 gotoghyll (pagey + pageh + ips)
5252 let prevpage () =
5253 match state.layout with
5254 | [] ->
5255 let pageno = page_of_y state.y in
5256 gotoghyll (getpagey (pageno-1))
5257 | l :: _ ->
5258 match conf.columns with
5259 | Csingle _ ->
5260 if conf.presentation && l.pagey != 0
5261 then
5262 gotoghyll (clamp (pgscale ~-(state.winh)))
5263 else
5264 let pageno = max 0 (l.pageno-1) in
5265 gotoghyll (getpagey pageno)
5266 | Cmulti ((c, _, coverB) as cl, _) ->
5267 if conf.presentation &&
5268 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5269 then
5270 gotoghyll (clamp (pgscale ~-(state.winh)))
5271 else
5272 let decr =
5273 if l.pageno = state.pagecount - coverB
5274 then 1
5275 else c
5277 let pageno = max 0 (l.pageno-decr) in
5278 gotoghyll (getpagey pageno)
5279 | Csplit (n, _) ->
5280 let y =
5281 if l.pagecol = 0
5282 then
5283 if l.pageno = 0
5284 then l.pagey
5285 else
5286 let pageno = max 0 (l.pageno-1) in
5287 let pagey, pageh = getpageyh pageno in
5288 pagey + (n-1)*pageh
5289 else
5290 let pagey, pageh = getpageyh l.pageno in
5291 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5293 gotoghyll y
5296 let viewkeyboard key mask =
5297 let enttext te =
5298 let mode = state.mode in
5299 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5300 state.text <- "";
5301 enttext ();
5302 G.postRedisplay "view:enttext"
5304 let ctrl = Wsi.withctrl mask in
5305 let key =
5306 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5308 match key with
5309 | 81 -> (* Q *)
5310 exit 0
5312 | 0xff63 -> (* insert *)
5313 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5314 then (
5315 state.mode <- LinkNav (Ltgendir 0);
5316 gotoy state.y;
5318 else showtext '!' "Keyboard link navigation does not work under rotation"
5320 | 0xff1b | 113 -> (* escape / q *)
5321 begin match state.mstate with
5322 | Mzoomrect _ ->
5323 state.mstate <- Mnone;
5324 Wsi.setcursor Wsi.CURSOR_INHERIT;
5325 G.postRedisplay "kill zoom rect";
5326 | _ ->
5327 begin match state.mode with
5328 | LinkNav _ ->
5329 state.mode <- View;
5330 G.postRedisplay "esc leave linknav"
5331 | _ ->
5332 match state.ranchors with
5333 | [] -> raise Quit
5334 | (path, password, anchor, origin) :: rest ->
5335 state.ranchors <- rest;
5336 state.anchor <- anchor;
5337 state.origin <- origin;
5338 state.nameddest <- "";
5339 opendoc path password
5340 end;
5341 end;
5343 | 0xff08 -> (* backspace *)
5344 gotoghyll (getnav ~-1)
5346 | 111 -> (* o *)
5347 enteroutlinemode ()
5349 | 117 -> (* u *)
5350 state.rects <- [];
5351 state.text <- "";
5352 G.postRedisplay "dehighlight";
5354 | 47 | 63 -> (* / ? *)
5355 let ondone isforw s =
5356 cbput state.hists.pat s;
5357 state.searchpattern <- s;
5358 search s isforw
5360 let s = String.create 1 in
5361 s.[0] <- Char.chr key;
5362 enttext (s, "", Some (onhist state.hists.pat),
5363 textentry, ondone (key = 47), true)
5365 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5366 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5367 setzoom (conf.zoom +. incr)
5369 | 43 | 0xffab -> (* + *)
5370 let ondone s =
5371 let n =
5372 try int_of_string s with exc ->
5373 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5374 max_int
5376 if n != max_int
5377 then (
5378 conf.pagebias <- n;
5379 state.text <- "page bias is now " ^ string_of_int n;
5382 enttext ("page bias: ", "", None, intentry, ondone, true)
5384 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5385 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5386 setzoom (max 0.01 (conf.zoom -. decr))
5388 | 45 | 0xffad -> (* - *)
5389 let ondone msg = state.text <- msg in
5390 enttext (
5391 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5392 optentry state.mode, ondone, true
5395 | 48 when ctrl -> (* ctrl-0 *)
5396 if conf.zoom = 1.0
5397 then (
5398 state.x <- 0;
5399 gotoy state.y
5401 else setzoom 1.0
5403 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5404 let cols =
5405 match conf.columns with
5406 | Csingle _ | Cmulti _ -> 1
5407 | Csplit (n, _) -> n
5409 let h = state.winh -
5410 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5412 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5413 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5414 then setzoom zoom
5416 | 51 when ctrl -> (* ctrl-3 *)
5417 let fm =
5418 match conf.fitmodel with
5419 | FitWidth -> FitProportional
5420 | FitProportional -> FitPage
5421 | FitPage -> FitWidth
5423 state.text <- "fit model: " ^ FMTE.to_string fm;
5424 reqlayout conf.angle fm
5426 | 0xffc6 -> (* f9 *)
5427 togglebirdseye ()
5429 | 57 when ctrl -> (* ctrl-9 *)
5430 togglebirdseye ()
5432 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5433 when not ctrl -> (* 0..9 *)
5434 let ondone s =
5435 let n =
5436 try int_of_string s with exc ->
5437 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5440 if n >= 0
5441 then (
5442 addnav ();
5443 cbput state.hists.pag (string_of_int n);
5444 gotopage1 (n + conf.pagebias - 1) 0;
5447 let pageentry text key =
5448 match Char.unsafe_chr key with
5449 | 'g' -> TEdone text
5450 | _ -> intentry text key
5452 let text = "x" in text.[0] <- Char.chr key;
5453 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5455 | 98 -> (* b *)
5456 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5457 reshape state.winw state.winh;
5459 | 66 -> (* B *)
5460 state.bzoom <- not state.bzoom;
5461 state.rects <- [];
5462 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5464 | 108 -> (* l *)
5465 conf.hlinks <- not conf.hlinks;
5466 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5467 G.postRedisplay "toggle highlightlinks";
5469 | 70 -> (* F *)
5470 state.glinks <- true;
5471 let mode = state.mode in
5472 state.mode <- Textentry (
5473 (":", "", None, linknentry, linkndone gotounder, false),
5474 (fun _ ->
5475 state.glinks <- false;
5476 state.mode <- mode)
5478 state.text <- "";
5479 G.postRedisplay "view:linkent(F)"
5481 | 121 -> (* y *)
5482 state.glinks <- true;
5483 let mode = state.mode in
5484 state.mode <- Textentry (
5486 ":", "", None, linknentry, linkndone (fun under ->
5487 selstring (undertext under);
5488 ), false
5490 fun _ ->
5491 state.glinks <- false;
5492 state.mode <- mode
5494 state.text <- "";
5495 G.postRedisplay "view:linkent"
5497 | 97 -> (* a *)
5498 begin match state.autoscroll with
5499 | Some step ->
5500 conf.autoscrollstep <- step;
5501 state.autoscroll <- None
5502 | None ->
5503 if conf.autoscrollstep = 0
5504 then state.autoscroll <- Some 1
5505 else state.autoscroll <- Some conf.autoscrollstep
5508 | 112 when ctrl -> (* ctrl-p *)
5509 launchpath ()
5511 | 80 -> (* P *)
5512 setpresentationmode (not conf.presentation);
5513 showtext ' ' ("presentation mode " ^
5514 if conf.presentation then "on" else "off");
5516 | 102 -> (* f *)
5517 if List.mem Wsi.Fullscreen state.winstate
5518 then Wsi.reshape conf.cwinw conf.cwinh
5519 else Wsi.fullscreen ()
5521 | 112 | 78 -> (* p|N *)
5522 search state.searchpattern false
5524 | 110 | 0xffc0 -> (* n|F3 *)
5525 search state.searchpattern true
5527 | 116 -> (* t *)
5528 begin match state.layout with
5529 | [] -> ()
5530 | l :: _ ->
5531 gotoghyll (getpagey l.pageno)
5534 | 32 -> (* space *)
5535 nextpage ()
5537 | 0xff9f | 0xffff -> (* delete *)
5538 prevpage ()
5540 | 61 -> (* = *)
5541 showtext ' ' (describe_location ());
5543 | 119 -> (* w *)
5544 begin match state.layout with
5545 | [] -> ()
5546 | l :: _ ->
5547 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5548 G.postRedisplay "w"
5551 | 39 -> (* ' *)
5552 enterbookmarkmode ()
5554 | 104 | 0xffbe -> (* h|F1 *)
5555 enterhelpmode ()
5557 | 105 -> (* i *)
5558 enterinfomode ()
5560 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5561 entermsgsmode ()
5563 | 109 -> (* m *)
5564 let ondone s =
5565 match state.layout with
5566 | l :: _ ->
5567 if nonemptystr s
5568 then
5569 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5570 | _ -> ()
5572 enttext ("bookmark: ", "", None, textentry, ondone, true)
5574 | 126 -> (* ~ *)
5575 quickbookmark ();
5576 showtext ' ' "Quick bookmark added";
5578 | 122 -> (* z *)
5579 begin match state.layout with
5580 | l :: _ ->
5581 let rect = getpdimrect l.pagedimno in
5582 let w, h =
5583 if conf.crophack
5584 then
5585 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5586 truncate (1.2 *. (rect.(3) -. rect.(0))))
5587 else
5588 (truncate (rect.(1) -. rect.(0)),
5589 truncate (rect.(3) -. rect.(0)))
5591 let w = truncate ((float w)*.conf.zoom)
5592 and h = truncate ((float h)*.conf.zoom) in
5593 if w != 0 && h != 0
5594 then (
5595 state.anchor <- getanchor ();
5596 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5598 G.postRedisplay "z";
5600 | [] -> ()
5603 | 120 -> state.roam ()
5604 | 60 | 62 -> (* < > *)
5605 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5607 | 91 | 93 -> (* [ ] *)
5608 conf.colorscale <-
5609 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5611 G.postRedisplay "brightness";
5613 | 99 when state.mode = View -> (* [alt-]c *)
5614 if Wsi.withalt mask
5615 then (
5616 if conf.zoom > 1.0
5617 then
5618 let m = (wadjsb state.winw - state.w) / 2 in
5619 state.x <- m;
5620 gotoy_and_clear_text state.y
5622 else
5623 let (c, a, b), z =
5624 match state.prevcolumns with
5625 | None -> (1, 0, 0), 1.0
5626 | Some (columns, z) ->
5627 let cab =
5628 match columns with
5629 | Csplit (c, _) -> -c, 0, 0
5630 | Cmulti ((c, a, b), _) -> c, a, b
5631 | Csingle _ -> 1, 0, 0
5633 cab, z
5635 setcolumns View c a b;
5636 setzoom z
5638 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5639 -> (* ctrl-shift- (kp) [up|down] *)
5640 let zoom, x = state.prevzoom in
5641 setzoom zoom;
5642 state.x <- x;
5644 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5645 begin match state.autoscroll with
5646 | None ->
5647 begin match state.mode with
5648 | Birdseye beye -> upbirdseye 1 beye
5649 | _ ->
5650 if ctrl
5651 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5652 else (
5653 if not (Wsi.withshift mask) && conf.presentation
5654 then prevpage ()
5655 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5658 | Some n ->
5659 setautoscrollspeed n false
5662 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5663 begin match state.autoscroll with
5664 | None ->
5665 begin match state.mode with
5666 | Birdseye beye -> downbirdseye 1 beye
5667 | _ ->
5668 if ctrl
5669 then gotoy_and_clear_text (clamp (state.winh/2))
5670 else (
5671 if not (Wsi.withshift mask) && conf.presentation
5672 then nextpage ()
5673 else gotoy_and_clear_text (clamp conf.scrollstep)
5676 | Some n ->
5677 setautoscrollspeed n true
5680 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5681 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5682 if canpan ()
5683 then
5684 let dx =
5685 if ctrl
5686 then state.winw / 2
5687 else conf.hscrollstep
5689 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5690 state.x <- panbound (state.x + dx);
5691 gotoy_and_clear_text state.y
5692 else (
5693 state.text <- "";
5694 G.postRedisplay "left/right"
5697 | 0xff55 | 0xff9a -> (* (kp) prior *)
5698 let y =
5699 if ctrl
5700 then
5701 match state.layout with
5702 | [] -> state.y
5703 | l :: _ -> state.y - l.pagey
5704 else
5705 clamp (pgscale (-state.winh))
5707 gotoghyll y
5709 | 0xff56 | 0xff9b -> (* (kp) next *)
5710 let y =
5711 if ctrl
5712 then
5713 match List.rev state.layout with
5714 | [] -> state.y
5715 | l :: _ -> getpagey l.pageno
5716 else
5717 clamp (pgscale state.winh)
5719 gotoghyll y
5721 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5722 gotoghyll 0
5723 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5724 gotoghyll (clamp state.maxy)
5726 | 0xff53 | 0xff98
5727 when Wsi.withalt mask -> (* alt-(kp) right *)
5728 gotoghyll (getnav 1)
5729 | 0xff51 | 0xff96
5730 when Wsi.withalt mask -> (* alt-(kp) left *)
5731 gotoghyll (getnav ~-1)
5733 | 114 -> (* r *)
5734 reload ()
5736 | 118 when conf.debug -> (* v *)
5737 state.rects <- [];
5738 List.iter (fun l ->
5739 match getopaque l.pageno with
5740 | None -> ()
5741 | Some opaque ->
5742 let x0, y0, x1, y1 = pagebbox opaque in
5743 let a,b = float x0, float y0 in
5744 let c,d = float x1, float y0 in
5745 let e,f = float x1, float y1 in
5746 let h,j = float x0, float y1 in
5747 let rect = (a,b,c,d,e,f,h,j) in
5748 debugrect rect;
5749 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5750 ) state.layout;
5751 G.postRedisplay "v";
5753 | _ ->
5754 vlog "huh? %s" (Wsi.keyname key)
5757 let linknavkeyboard key mask linknav =
5758 let getpage pageno =
5759 let rec loop = function
5760 | [] -> None
5761 | l :: _ when l.pageno = pageno -> Some l
5762 | _ :: rest -> loop rest
5763 in loop state.layout
5765 let doexact (pageno, n) =
5766 match getopaque pageno, getpage pageno with
5767 | Some opaque, Some l ->
5768 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5769 then
5770 let under = getlink opaque n in
5771 G.postRedisplay "link gotounder";
5772 gotounder under;
5773 state.mode <- View;
5774 else
5775 let opt, dir =
5776 match key with
5777 | 0xff50 -> (* home *)
5778 Some (findlink opaque LDfirst), -1
5780 | 0xff57 -> (* end *)
5781 Some (findlink opaque LDlast), 1
5783 | 0xff51 -> (* left *)
5784 Some (findlink opaque (LDleft n)), -1
5786 | 0xff53 -> (* right *)
5787 Some (findlink opaque (LDright n)), 1
5789 | 0xff52 -> (* up *)
5790 Some (findlink opaque (LDup n)), -1
5792 | 0xff54 -> (* down *)
5793 Some (findlink opaque (LDdown n)), 1
5795 | _ -> None, 0
5797 let pwl l dir =
5798 begin match findpwl l.pageno dir with
5799 | Pwlnotfound -> ()
5800 | Pwl pageno ->
5801 let notfound dir =
5802 state.mode <- LinkNav (Ltgendir dir);
5803 let y, h = getpageyh pageno in
5804 let y =
5805 if dir < 0
5806 then y + h - state.winh
5807 else y
5809 gotoy y
5811 begin match getopaque pageno, getpage pageno with
5812 | Some opaque, Some _ ->
5813 let link =
5814 let ld = if dir > 0 then LDfirst else LDlast in
5815 findlink opaque ld
5817 begin match link with
5818 | Lfound m ->
5819 showlinktype (getlink opaque m);
5820 state.mode <- LinkNav (Ltexact (pageno, m));
5821 G.postRedisplay "linknav jpage";
5822 | _ -> notfound dir
5823 end;
5824 | _ -> notfound dir
5825 end;
5826 end;
5828 begin match opt with
5829 | Some Lnotfound -> pwl l dir;
5830 | Some (Lfound m) ->
5831 if m = n
5832 then pwl l dir
5833 else (
5834 let _, y0, _, y1 = getlinkrect opaque m in
5835 if y0 < l.pagey
5836 then gotopage1 l.pageno y0
5837 else (
5838 let d = fstate.fontsize + 1 in
5839 if y1 - l.pagey > l.pagevh - d
5840 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5841 else G.postRedisplay "linknav";
5843 showlinktype (getlink opaque m);
5844 state.mode <- LinkNav (Ltexact (l.pageno, m));
5847 | None -> viewkeyboard key mask
5848 end;
5849 | _ -> viewkeyboard key mask
5851 if key = 0xff63
5852 then (
5853 state.mode <- View;
5854 G.postRedisplay "leave linknav"
5856 else
5857 match linknav with
5858 | Ltgendir _ -> viewkeyboard key mask
5859 | Ltexact exact -> doexact exact
5862 let keyboard key mask =
5863 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5864 then wcmd "interrupt"
5865 else state.uioh <- state.uioh#key key mask
5868 let birdseyekeyboard key mask
5869 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5870 let incr =
5871 match conf.columns with
5872 | Csingle _ -> 1
5873 | Cmulti ((c, _, _), _) -> c
5874 | Csplit _ -> failwith "bird's eye split mode"
5876 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5877 match key with
5878 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5879 let y, h = getpageyh pageno in
5880 let top = (state.winh - h) / 2 in
5881 gotoy (max 0 (y - top))
5882 | 0xff0d (* enter *)
5883 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5884 | 0xff1b -> leavebirdseye beye true (* escape *)
5885 | 0xff52 -> upbirdseye incr beye (* up *)
5886 | 0xff54 -> downbirdseye incr beye (* down *)
5887 | 0xff51 -> upbirdseye 1 beye (* left *)
5888 | 0xff53 -> downbirdseye 1 beye (* right *)
5890 | 0xff55 -> (* prior *)
5891 begin match state.layout with
5892 | l :: _ ->
5893 if l.pagey != 0
5894 then (
5895 state.mode <- Birdseye (
5896 oconf, leftx, l.pageno, hooverpageno, anchor
5898 gotopage1 l.pageno 0;
5900 else (
5901 let layout = layout (state.y-state.winh) (pgh state.layout) in
5902 match layout with
5903 | [] -> gotoy (clamp (-state.winh))
5904 | l :: _ ->
5905 state.mode <- Birdseye (
5906 oconf, leftx, l.pageno, hooverpageno, anchor
5908 gotopage1 l.pageno 0
5911 | [] -> gotoy (clamp (-state.winh))
5912 end;
5914 | 0xff56 -> (* next *)
5915 begin match List.rev state.layout with
5916 | l :: _ ->
5917 let layout = layout (state.y + (pgh state.layout)) state.winh in
5918 begin match layout with
5919 | [] ->
5920 let incr = l.pageh - l.pagevh in
5921 if incr = 0
5922 then (
5923 state.mode <-
5924 Birdseye (
5925 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5927 G.postRedisplay "birdseye pagedown";
5929 else gotoy (clamp (incr + conf.interpagespace*2));
5931 | l :: _ ->
5932 state.mode <-
5933 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5934 gotopage1 l.pageno 0;
5937 | [] -> gotoy (clamp state.winh)
5938 end;
5940 | 0xff50 -> (* home *)
5941 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5942 gotopage1 0 0
5944 | 0xff57 -> (* end *)
5945 let pageno = state.pagecount - 1 in
5946 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5947 if not (pagevisible state.layout pageno)
5948 then
5949 let h =
5950 match List.rev state.pdims with
5951 | [] -> state.winh
5952 | (_, _, h, _) :: _ -> h
5954 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5955 else G.postRedisplay "birdseye end";
5956 | _ -> viewkeyboard key mask
5959 let drawpage l =
5960 let color =
5961 match state.mode with
5962 | Textentry _ -> scalecolor 0.4
5963 | LinkNav _
5964 | View -> scalecolor 1.0
5965 | Birdseye (_, _, pageno, hooverpageno, _) ->
5966 if l.pageno = hooverpageno
5967 then scalecolor 0.9
5968 else (
5969 if l.pageno = pageno
5970 then scalecolor 1.0
5971 else scalecolor 0.8
5974 drawtiles l color;
5977 let postdrawpage l linkindexbase =
5978 match getopaque l.pageno with
5979 | Some opaque ->
5980 if tileready l l.pagex l.pagey
5981 then
5982 let x = l.pagedispx - l.pagex
5983 and y = l.pagedispy - l.pagey in
5984 let hlmask =
5985 match conf.columns with
5986 | Csingle _ | Cmulti _ ->
5987 (if conf.hlinks then 1 else 0)
5988 + (if state.glinks
5989 && not (isbirdseye state.mode) then 2 else 0)
5990 | _ -> 0
5992 let s =
5993 match state.mode with
5994 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5995 | _ -> ""
5997 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5998 else 0
5999 | _ -> 0
6002 let scrollindicator () =
6003 let sbw, ph, sh = state.uioh#scrollph in
6004 let sbh, pw, sw = state.uioh#scrollpw in
6006 GlDraw.color (0.64, 0.64, 0.64);
6007 GlDraw.rect
6008 (float (state.winw - sbw), 0.)
6009 (float state.winw, float state.winh)
6011 GlDraw.rect
6012 (0., float (state.winh - sbh))
6013 (float (wadjsb state.winw - 1), float state.winh)
6015 GlDraw.color (0.0, 0.0, 0.0);
6017 GlDraw.rect
6018 (float (state.winw - sbw), ph)
6019 (float state.winw, ph +. sh)
6021 GlDraw.rect
6022 (pw, float (state.winh - sbh))
6023 (pw +. sw, float state.winh)
6027 let showsel () =
6028 match state.mstate with
6029 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6032 | Msel ((x0, y0), (x1, y1)) ->
6033 let rec loop = function
6034 | l :: ls ->
6035 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
6036 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
6037 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
6038 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
6039 then
6040 match getopaque l.pageno with
6041 | Some opaque ->
6042 let x0, y0 = pagetranslatepoint l x0 y0 in
6043 let x1, y1 = pagetranslatepoint l x1 y1 in
6044 seltext opaque (x0, y0, x1, y1);
6045 | _ -> ()
6046 else loop ls
6047 | [] -> ()
6049 loop state.layout
6052 let showrects rects =
6053 Gl.enable `blend;
6054 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6055 GlDraw.polygon_mode `both `fill;
6056 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6057 List.iter
6058 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6059 List.iter (fun l ->
6060 if l.pageno = pageno
6061 then (
6062 let dx = float (l.pagedispx - l.pagex) in
6063 let dy = float (l.pagedispy - l.pagey) in
6064 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6065 GlDraw.begins `quads;
6067 GlDraw.vertex2 (x0+.dx, y0+.dy);
6068 GlDraw.vertex2 (x1+.dx, y1+.dy);
6069 GlDraw.vertex2 (x2+.dx, y2+.dy);
6070 GlDraw.vertex2 (x3+.dx, y3+.dy);
6072 GlDraw.ends ();
6074 ) state.layout
6075 ) rects
6077 Gl.disable `blend;
6080 let display () =
6081 GlClear.color (scalecolor2 conf.bgcolor);
6082 GlClear.clear [`color];
6083 List.iter drawpage state.layout;
6084 let rects =
6085 match state.mode with
6086 | LinkNav (Ltexact (pageno, linkno)) ->
6087 begin match getopaque pageno with
6088 | Some opaque ->
6089 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6090 (pageno, 5, (
6091 float x0, float y0,
6092 float x1, float y0,
6093 float x1, float y1,
6094 float x0, float y1)
6095 ) :: state.rects
6096 | None -> state.rects
6098 | _ -> state.rects
6100 showrects rects;
6101 let rec postloop linkindexbase = function
6102 | l :: rest ->
6103 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6104 postloop linkindexbase rest
6105 | [] -> ()
6107 showsel ();
6108 postloop 0 state.layout;
6109 state.uioh#display;
6110 begin match state.mstate with
6111 | Mzoomrect ((x0, y0), (x1, y1)) ->
6112 Gl.enable `blend;
6113 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6114 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6115 GlDraw.rect (float x0, float y0)
6116 (float x1, float y1);
6117 Gl.disable `blend;
6118 | _ -> ()
6119 end;
6120 enttext ();
6121 scrollindicator ();
6122 Wsi.swapb ();
6125 let zoomrect x y x1 y1 =
6126 let x0 = min x x1
6127 and x1 = max x x1
6128 and y0 = min y y1 in
6129 gotoy (state.y + y0);
6130 state.anchor <- getanchor ();
6131 let zoom = (float state.w) /. float (x1 - x0) in
6132 let margin =
6133 match conf.fitmodel, conf.columns with
6134 | FitPage, Csplit _ ->
6135 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6137 | _, _ ->
6138 let adjw = wadjsb state.winw in
6139 if state.w < adjw
6140 then (adjw - state.w) / 2
6141 else 0
6143 state.x <- (state.x + margin) - x0;
6144 setzoom zoom;
6145 Wsi.setcursor Wsi.CURSOR_INHERIT;
6146 state.mstate <- Mnone;
6149 let zoomblock x y =
6150 let g opaque l px py =
6151 match rectofblock opaque px py with
6152 | Some a ->
6153 let x0 = a.(0) -. 20. in
6154 let x1 = a.(1) +. 20. in
6155 let y0 = a.(2) -. 20. in
6156 let zoom = (float state.w) /. (x1 -. x0) in
6157 let pagey = getpagey l.pageno in
6158 gotoy_and_clear_text (pagey + truncate y0);
6159 state.anchor <- getanchor ();
6160 let margin = (state.w - l.pagew)/2 in
6161 state.x <- -truncate x0 - margin;
6162 setzoom zoom;
6163 None
6164 | None -> None
6166 match conf.columns with
6167 | Csplit _ ->
6168 showtext '!' "block zooming does not work properly in split columns mode"
6169 | _ -> onppundermouse g x y ()
6172 let scrollx x =
6173 let winw = wadjsb state.winw - 1 in
6174 let s = float x /. float winw in
6175 let destx = truncate (float (state.w + winw) *. s) in
6176 state.x <- winw - destx;
6177 gotoy_and_clear_text state.y;
6178 state.mstate <- Mscrollx;
6181 let scrolly y =
6182 let s = float y /. float state.winh in
6183 let desty = truncate (float (state.maxy - state.winh) *. s) in
6184 gotoy_and_clear_text desty;
6185 state.mstate <- Mscrolly;
6188 let viewmouse button down x y mask =
6189 match button with
6190 | n when (n == 4 || n == 5) && not down ->
6191 if Wsi.withctrl mask
6192 then (
6193 match state.mstate with
6194 | Mzoom (oldn, i) ->
6195 if oldn = n
6196 then (
6197 if i = 2
6198 then
6199 let incr =
6200 match n with
6201 | 5 ->
6202 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6203 | _ ->
6204 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6206 let zoom = conf.zoom -. incr in
6207 setzoom zoom;
6208 state.mstate <- Mzoom (n, 0);
6209 else
6210 state.mstate <- Mzoom (n, i+1);
6212 else state.mstate <- Mzoom (n, 0)
6214 | _ -> state.mstate <- Mzoom (n, 0)
6216 else (
6217 match state.autoscroll with
6218 | Some step -> setautoscrollspeed step (n=4)
6219 | None ->
6220 if conf.wheelbypage || conf.presentation
6221 then (
6222 if n = 4
6223 then prevpage ()
6224 else nextpage ()
6226 else
6227 let incr =
6228 if n = 4
6229 then -conf.scrollstep
6230 else conf.scrollstep
6232 let incr = incr * 2 in
6233 let y = clamp incr in
6234 gotoy_and_clear_text y
6237 | n when (n = 6 || n = 7) && not down && canpan () ->
6238 state.x <-
6239 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6240 gotoy_and_clear_text state.y
6242 | 1 when Wsi.withshift mask ->
6243 state.mstate <- Mnone;
6244 if not down
6245 then (
6246 match unproject x y with
6247 | Some (pageno, ux, uy) ->
6248 let cmd = Printf.sprintf
6249 "%s %s %d %d %d"
6250 conf.stcmd state.path pageno ux uy
6252 popen cmd []
6253 | None -> ()
6256 | 1 when Wsi.withctrl mask ->
6257 if down
6258 then (
6259 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6260 state.mstate <- Mpan (x, y)
6262 else
6263 state.mstate <- Mnone
6265 | 3 ->
6266 if down
6267 then (
6268 Wsi.setcursor Wsi.CURSOR_CYCLE;
6269 let p = (x, y) in
6270 state.mstate <- Mzoomrect (p, p)
6272 else (
6273 match state.mstate with
6274 | Mzoomrect ((x0, y0), _) ->
6275 if abs (x-x0) > 10 && abs (y - y0) > 10
6276 then zoomrect x0 y0 x y
6277 else (
6278 state.mstate <- Mnone;
6279 Wsi.setcursor Wsi.CURSOR_INHERIT;
6280 G.postRedisplay "kill accidental zoom rect";
6282 | _ ->
6283 Wsi.setcursor Wsi.CURSOR_INHERIT;
6284 state.mstate <- Mnone
6287 | 1 when x > state.winw - vscrollw () ->
6288 if down
6289 then
6290 let _, position, sh = state.uioh#scrollph in
6291 if y > truncate position && y < truncate (position +. sh)
6292 then state.mstate <- Mscrolly
6293 else scrolly y
6294 else
6295 state.mstate <- Mnone
6297 | 1 when y > state.winh - hscrollh () ->
6298 if down
6299 then
6300 let _, position, sw = state.uioh#scrollpw in
6301 if x > truncate position && x < truncate (position +. sw)
6302 then state.mstate <- Mscrollx
6303 else scrollx x
6304 else
6305 state.mstate <- Mnone
6307 | 1 when state.bzoom -> if not down then zoomblock x y
6309 | 1 ->
6310 let dest = if down then getunder x y else Unone in
6311 begin match dest with
6312 | Ulinkgoto _
6313 | Ulinkuri _
6314 | Uremote _ | Uremotedest _
6315 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6316 gotounder dest
6318 | Unone when down ->
6319 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6320 state.mstate <- Mpan (x, y);
6322 | Unone | Utext _ ->
6323 if down
6324 then (
6325 if conf.angle mod 360 = 0
6326 then (
6327 state.mstate <- Msel ((x, y), (x, y));
6328 G.postRedisplay "mouse select";
6331 else (
6332 match state.mstate with
6333 | Mnone -> ()
6335 | Mzoom _ | Mscrollx | Mscrolly ->
6336 state.mstate <- Mnone
6338 | Mzoomrect ((x0, y0), _) ->
6339 zoomrect x0 y0 x y
6341 | Mpan _ ->
6342 Wsi.setcursor Wsi.CURSOR_INHERIT;
6343 state.mstate <- Mnone
6345 | Msel ((x0, y0), (x1, y1)) ->
6346 let rec loop = function
6347 | [] -> ()
6348 | l :: rest ->
6349 let inside =
6350 let a0 = l.pagedispy in
6351 let a1 = a0 + l.pagevh in
6352 let b0 = l.pagedispx in
6353 let b1 = b0 + l.pagevw in
6354 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6355 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6357 if inside
6358 then
6359 match getopaque l.pageno with
6360 | Some opaque ->
6361 begin
6362 match Ne.pipe () with
6363 | Ne.Exn exn ->
6364 showtext '!'
6365 (Printf.sprintf
6366 "can not create sel pipe: %s"
6367 (exntos exn));
6368 | Ne.Res (r, w) ->
6369 let doclose what fd =
6370 Ne.clo fd (fun msg ->
6371 dolog "%s close failed: %s" what msg)
6374 popen conf.selcmd [r, 0; w, -1];
6375 copysel w opaque true;
6376 doclose "pipe/r" r;
6377 G.postRedisplay "copysel";
6378 with exn ->
6379 dolog "can not execute %S: %s"
6380 conf.selcmd (exntos exn);
6381 doclose "pipe/r" r;
6382 doclose "pipe/w" w;
6384 | None -> ()
6385 else loop rest
6387 loop state.layout;
6388 Wsi.setcursor Wsi.CURSOR_INHERIT;
6389 state.mstate <- Mnone;
6393 | _ -> ()
6396 let birdseyemouse button down x y mask
6397 (conf, leftx, _, hooverpageno, anchor) =
6398 match button with
6399 | 1 when down ->
6400 let rec loop = function
6401 | [] -> ()
6402 | l :: rest ->
6403 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6404 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6405 then (
6406 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6408 else loop rest
6410 loop state.layout
6411 | 3 -> ()
6412 | _ -> viewmouse button down x y mask
6415 let mouse button down x y mask =
6416 state.uioh <- state.uioh#button button down x y mask;
6419 let motion ~x ~y =
6420 state.uioh <- state.uioh#motion x y
6423 let pmotion ~x ~y =
6424 state.uioh <- state.uioh#pmotion x y;
6427 let uioh = object
6428 method display = ()
6430 method key key mask =
6431 begin match state.mode with
6432 | Textentry textentry -> textentrykeyboard key mask textentry
6433 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6434 | View -> viewkeyboard key mask
6435 | LinkNav linknav -> linknavkeyboard key mask linknav
6436 end;
6437 state.uioh
6439 method button button bstate x y mask =
6440 begin match state.mode with
6441 | LinkNav _
6442 | View -> viewmouse button bstate x y mask
6443 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6444 | Textentry _ -> ()
6445 end;
6446 state.uioh
6448 method motion x y =
6449 begin match state.mode with
6450 | Textentry _ -> ()
6451 | View | Birdseye _ | LinkNav _ ->
6452 match state.mstate with
6453 | Mzoom _ | Mnone -> ()
6455 | Mpan (x0, y0) ->
6456 let dx = x - x0
6457 and dy = y0 - y in
6458 state.mstate <- Mpan (x, y);
6459 if canpan ()
6460 then state.x <- panbound (state.x + dx);
6461 let y = clamp dy in
6462 gotoy_and_clear_text y
6464 | Msel (a, _) ->
6465 state.mstate <- Msel (a, (x, y));
6466 G.postRedisplay "motion select";
6468 | Mscrolly ->
6469 let y = min state.winh (max 0 y) in
6470 scrolly y
6472 | Mscrollx ->
6473 let x = min state.winw (max 0 x) in
6474 scrollx x
6476 | Mzoomrect (p0, _) ->
6477 state.mstate <- Mzoomrect (p0, (x, y));
6478 G.postRedisplay "motion zoomrect";
6479 end;
6480 state.uioh
6482 method pmotion x y =
6483 begin match state.mode with
6484 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6485 let rec loop = function
6486 | [] ->
6487 if hooverpageno != -1
6488 then (
6489 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6490 G.postRedisplay "pmotion birdseye no hoover";
6492 | l :: rest ->
6493 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6494 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6495 then (
6496 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6497 G.postRedisplay "pmotion birdseye hoover";
6499 else loop rest
6501 loop state.layout
6503 | Textentry _ -> ()
6505 | LinkNav _
6506 | View ->
6507 match state.mstate with
6508 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6510 | Mnone ->
6511 updateunder x y;
6512 match conf.pax with
6513 | None -> ()
6514 | Some r ->
6515 let past, _, _ = !r in
6516 let now = now () in
6517 let delta = now -. past in
6518 if delta > 0.01
6519 then paxunder x y
6520 else r := (now, x, y)
6521 end;
6522 state.uioh
6524 method infochanged _ = ()
6526 method scrollph =
6527 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6528 let p, h =
6529 if maxy = 0
6530 then 0.0, float state.winh
6531 else scrollph state.y maxy
6533 vscrollw (), p, h
6535 method scrollpw =
6536 let winw = wadjsb state.winw in
6537 let fwinw = float winw in
6538 let sw =
6539 let sw = fwinw /. float state.w in
6540 let sw = fwinw *. sw in
6541 max sw (float conf.scrollh)
6543 let position =
6544 let maxx = state.w + winw in
6545 let x = winw - state.x in
6546 let percent = float x /. float maxx in
6547 (fwinw -. sw) *. percent
6549 hscrollh (), position, sw
6551 method modehash =
6552 let modename =
6553 match state.mode with
6554 | LinkNav _ -> "links"
6555 | Textentry _ -> "textentry"
6556 | Birdseye _ -> "birdseye"
6557 | View -> "view"
6559 findkeyhash conf modename
6561 method eformsgs = true
6562 end;;
6564 module Config =
6565 struct
6566 open Parser
6568 let fontpath = ref "";;
6570 module KeyMap =
6571 Map.Make (struct type t = (int * int) let compare = compare end);;
6573 let unent s =
6574 let l = String.length s in
6575 let b = Buffer.create l in
6576 unent b s 0 l;
6577 Buffer.contents b;
6580 let home =
6581 try Sys.getenv "HOME"
6582 with exn ->
6583 prerr_endline
6584 ("Can not determine home directory location: " ^ exntos exn);
6588 let modifier_of_string = function
6589 | "alt" -> Wsi.altmask
6590 | "shift" -> Wsi.shiftmask
6591 | "ctrl" | "control" -> Wsi.ctrlmask
6592 | "meta" -> Wsi.metamask
6593 | _ -> 0
6596 let key_of_string =
6597 let r = Str.regexp "-" in
6598 fun s ->
6599 let elems = Str.full_split r s in
6600 let f n k m =
6601 let g s =
6602 let m1 = modifier_of_string s in
6603 if m1 = 0
6604 then (Wsi.namekey s, m)
6605 else (k, m lor m1)
6606 in function
6607 | Str.Delim s when n land 1 = 0 -> g s
6608 | Str.Text s -> g s
6609 | Str.Delim _ -> (k, m)
6611 let rec loop n k m = function
6612 | [] -> (k, m)
6613 | x :: xs ->
6614 let k, m = f n k m x in
6615 loop (n+1) k m xs
6617 loop 0 0 0 elems
6620 let keys_of_string =
6621 let r = Str.regexp "[ \t]" in
6622 fun s ->
6623 let elems = Str.split r s in
6624 List.map key_of_string elems
6627 let copykeyhashes c =
6628 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6631 let config_of c attrs =
6632 let apply c k v =
6634 match k with
6635 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6636 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6637 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6638 | "preload" -> { c with preload = bool_of_string v }
6639 | "page-bias" -> { c with pagebias = int_of_string v }
6640 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6641 | "horizontal-scroll-step" ->
6642 { c with hscrollstep = max (int_of_string v) 1 }
6643 | "auto-scroll-step" ->
6644 { c with autoscrollstep = max 0 (int_of_string v) }
6645 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6646 | "crop-hack" -> { c with crophack = bool_of_string v }
6647 | "throttle" ->
6648 let mw =
6649 match String.lowercase v with
6650 | "true" -> Some infinity
6651 | "false" -> None
6652 | f -> Some (float_of_string f)
6654 { c with maxwait = mw}
6655 | "highlight-links" -> { c with hlinks = bool_of_string v }
6656 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6657 | "vertical-margin" ->
6658 { c with interpagespace = max 0 (int_of_string v) }
6659 | "zoom" ->
6660 let zoom = float_of_string v /. 100. in
6661 let zoom = max zoom 0.0 in
6662 { c with zoom = zoom }
6663 | "presentation" -> { c with presentation = bool_of_string v }
6664 | "rotation-angle" -> { c with angle = int_of_string v }
6665 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6666 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6667 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6668 | "proportional-display" ->
6669 let fm =
6670 if bool_of_string v
6671 then FitProportional
6672 else FitWidth
6674 { c with fitmodel = fm }
6675 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6676 | "pixmap-cache-size" ->
6677 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6678 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6679 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6680 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6681 | "persistent-location" -> { c with jumpback = bool_of_string v }
6682 | "background-color" -> { c with bgcolor = color_of_string v }
6683 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6684 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6685 | "mupdf-store-size" ->
6686 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6687 | "checkers" -> { c with checkers = bool_of_string v }
6688 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6689 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6690 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6691 | "uri-launcher" -> { c with urilauncher = unent v }
6692 | "path-launcher" -> { c with pathlauncher = unent v }
6693 | "color-space" -> { c with colorspace = CSTE.of_string v }
6694 | "invert-colors" -> { c with invert = bool_of_string v }
6695 | "brightness" -> { c with colorscale = float_of_string v }
6696 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6697 | "ghyllscroll" ->
6698 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6699 | "columns" ->
6700 let (n, _, _) as nab = multicolumns_of_string v in
6701 if n < 0
6702 then { c with columns = Csplit (-n, [||]) }
6703 else { c with columns = Cmulti (nab, [||]) }
6704 | "birds-eye-columns" ->
6705 { c with beyecolumns = Some (max (int_of_string v) 2) }
6706 | "selection-command" -> { c with selcmd = unent v }
6707 | "synctex-command" -> { c with stcmd = unent v }
6708 | "pax-command" -> { c with paxcmd = unent v }
6709 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6710 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6711 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6712 | "use-pbo" -> { c with usepbo = bool_of_string v }
6713 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6714 | "horizontal-scrollbar-visible" ->
6715 let b =
6716 if bool_of_string v
6717 then c.scrollb lor scrollbhv
6718 else c.scrollb land (lnot scrollbhv)
6720 { c with scrollb = b }
6721 | "vertical-scrollbar-visible" ->
6722 let b =
6723 if bool_of_string v
6724 then c.scrollb lor scrollbvv
6725 else c.scrollb land (lnot scrollbvv)
6727 { c with scrollb = b }
6728 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6729 | "point-and-x" ->
6730 { c with pax =
6731 if bool_of_string v
6732 then Some (ref (0.0, 0, 0))
6733 else None }
6734 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6735 | _ -> c
6736 with exn ->
6737 prerr_endline ("Error processing attribute (`" ^
6738 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6741 let rec fold c = function
6742 | [] -> c
6743 | (k, v) :: rest ->
6744 let c = apply c k v in
6745 fold c rest
6747 fold { c with keyhashes = copykeyhashes c } attrs;
6750 let fromstring f pos n v d =
6751 try f v
6752 with exn ->
6753 dolog "Error processing attribute (%S=%S) at %d\n%s"
6754 n v pos (exntos exn)
6759 let bookmark_of attrs =
6760 let rec fold title page rely visy = function
6761 | ("title", v) :: rest -> fold v page rely visy rest
6762 | ("page", v) :: rest -> fold title v rely visy rest
6763 | ("rely", v) :: rest -> fold title page v visy rest
6764 | ("visy", v) :: rest -> fold title page rely v rest
6765 | _ :: rest -> fold title page rely visy rest
6766 | [] -> title, page, rely, visy
6768 fold "invalid" "0" "0" "0" attrs
6771 let doc_of attrs =
6772 let rec fold path page rely pan visy = function
6773 | ("path", v) :: rest -> fold v page rely pan visy rest
6774 | ("page", v) :: rest -> fold path v rely pan visy rest
6775 | ("rely", v) :: rest -> fold path page v pan visy rest
6776 | ("pan", v) :: rest -> fold path page rely v visy rest
6777 | ("visy", v) :: rest -> fold path page rely pan v rest
6778 | _ :: rest -> fold path page rely pan visy rest
6779 | [] -> path, page, rely, pan, visy
6781 fold "" "0" "0" "0" "0" attrs
6784 let map_of attrs =
6785 let rec fold rs ls = function
6786 | ("out", v) :: rest -> fold v ls rest
6787 | ("in", v) :: rest -> fold rs v rest
6788 | _ :: rest -> fold ls rs rest
6789 | [] -> ls, rs
6791 fold "" "" attrs
6794 let setconf dst src =
6795 dst.scrollbw <- src.scrollbw;
6796 dst.scrollh <- src.scrollh;
6797 dst.icase <- src.icase;
6798 dst.preload <- src.preload;
6799 dst.pagebias <- src.pagebias;
6800 dst.verbose <- src.verbose;
6801 dst.scrollstep <- src.scrollstep;
6802 dst.maxhfit <- src.maxhfit;
6803 dst.crophack <- src.crophack;
6804 dst.autoscrollstep <- src.autoscrollstep;
6805 dst.maxwait <- src.maxwait;
6806 dst.hlinks <- src.hlinks;
6807 dst.underinfo <- src.underinfo;
6808 dst.interpagespace <- src.interpagespace;
6809 dst.zoom <- src.zoom;
6810 dst.presentation <- src.presentation;
6811 dst.angle <- src.angle;
6812 dst.cwinw <- src.cwinw;
6813 dst.cwinh <- src.cwinh;
6814 dst.savebmarks <- src.savebmarks;
6815 dst.memlimit <- src.memlimit;
6816 dst.fitmodel <- src.fitmodel;
6817 dst.texcount <- src.texcount;
6818 dst.sliceheight <- src.sliceheight;
6819 dst.thumbw <- src.thumbw;
6820 dst.jumpback <- src.jumpback;
6821 dst.bgcolor <- src.bgcolor;
6822 dst.tilew <- src.tilew;
6823 dst.tileh <- src.tileh;
6824 dst.mustoresize <- src.mustoresize;
6825 dst.checkers <- src.checkers;
6826 dst.aalevel <- src.aalevel;
6827 dst.trimmargins <- src.trimmargins;
6828 dst.trimfuzz <- src.trimfuzz;
6829 dst.urilauncher <- src.urilauncher;
6830 dst.colorspace <- src.colorspace;
6831 dst.invert <- src.invert;
6832 dst.colorscale <- src.colorscale;
6833 dst.redirectstderr <- src.redirectstderr;
6834 dst.ghyllscroll <- src.ghyllscroll;
6835 dst.columns <- src.columns;
6836 dst.beyecolumns <- src.beyecolumns;
6837 dst.selcmd <- src.selcmd;
6838 dst.updatecurs <- src.updatecurs;
6839 dst.pathlauncher <- src.pathlauncher;
6840 dst.keyhashes <- copykeyhashes src;
6841 dst.hfsize <- src.hfsize;
6842 dst.hscrollstep <- src.hscrollstep;
6843 dst.pgscale <- src.pgscale;
6844 dst.usepbo <- src.usepbo;
6845 dst.wheelbypage <- src.wheelbypage;
6846 dst.stcmd <- src.stcmd;
6847 dst.paxcmd <- src.paxcmd;
6848 dst.scrollb <- src.scrollb;
6849 dst.riani <- src.riani;
6850 dst.paxmark <- src.paxmark;
6851 dst.pax <-
6852 if src.pax = None
6853 then None
6854 else Some ((ref (0.0, 0, 0)));
6857 let get s =
6858 let h = Hashtbl.create 10 in
6859 let dc = { defconf with angle = defconf.angle } in
6860 let rec toplevel v t spos _ =
6861 match t with
6862 | Vdata | Vcdata | Vend -> v
6863 | Vopen ("llppconfig", _, closed) ->
6864 if closed
6865 then v
6866 else { v with f = llppconfig }
6867 | Vopen _ ->
6868 error "unexpected subelement at top level" s spos
6869 | Vclose _ -> error "unexpected close at top level" s spos
6871 and llppconfig v t spos _ =
6872 match t with
6873 | Vdata | Vcdata -> v
6874 | Vend -> error "unexpected end of input in llppconfig" s spos
6875 | Vopen ("defaults", attrs, closed) ->
6876 let c = config_of dc attrs in
6877 setconf dc c;
6878 if closed
6879 then v
6880 else { v with f = defaults }
6882 | Vopen ("ui-font", attrs, closed) ->
6883 let rec getsize size = function
6884 | [] -> size
6885 | ("size", v) :: rest ->
6886 let size =
6887 fromstring int_of_string spos "size" v fstate.fontsize in
6888 getsize size rest
6889 | l -> getsize size l
6891 fstate.fontsize <- getsize fstate.fontsize attrs;
6892 if closed
6893 then v
6894 else { v with f = uifont (Buffer.create 10) }
6896 | Vopen ("doc", attrs, closed) ->
6897 let pathent, spage, srely, span, svisy = doc_of attrs in
6898 let path = unent pathent
6899 and pageno = fromstring int_of_string spos "page" spage 0
6900 and rely = fromstring float_of_string spos "rely" srely 0.0
6901 and pan = fromstring int_of_string spos "pan" span 0
6902 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6903 let c = config_of dc attrs in
6904 let anchor = (pageno, rely, visy) in
6905 if closed
6906 then (Hashtbl.add h path (c, [], pan, anchor); v)
6907 else { v with f = doc path pan anchor c [] }
6909 | Vopen _ ->
6910 error "unexpected subelement in llppconfig" s spos
6912 | Vclose "llppconfig" -> { v with f = toplevel }
6913 | Vclose _ -> error "unexpected close in llppconfig" s spos
6915 and defaults v t spos _ =
6916 match t with
6917 | Vdata | Vcdata -> v
6918 | Vend -> error "unexpected end of input in defaults" s spos
6919 | Vopen ("keymap", attrs, closed) ->
6920 let modename =
6921 try List.assoc "mode" attrs
6922 with Not_found -> "global" in
6923 if closed
6924 then v
6925 else
6926 let ret keymap =
6927 let h = findkeyhash dc modename in
6928 KeyMap.iter (Hashtbl.replace h) keymap;
6929 defaults
6931 { v with f = pkeymap ret KeyMap.empty }
6933 | Vopen (_, _, _) ->
6934 error "unexpected subelement in defaults" s spos
6936 | Vclose "defaults" ->
6937 { v with f = llppconfig }
6939 | Vclose _ -> error "unexpected close in defaults" s spos
6941 and uifont b v t spos epos =
6942 match t with
6943 | Vdata | Vcdata ->
6944 Buffer.add_substring b s spos (epos - spos);
6946 | Vopen (_, _, _) ->
6947 error "unexpected subelement in ui-font" s spos
6948 | Vclose "ui-font" ->
6949 if emptystr !fontpath
6950 then fontpath := Buffer.contents b;
6951 { v with f = llppconfig }
6952 | Vclose _ -> error "unexpected close in ui-font" s spos
6953 | Vend -> error "unexpected end of input in ui-font" s spos
6955 and doc path pan anchor c bookmarks v t spos _ =
6956 match t with
6957 | Vdata | Vcdata -> v
6958 | Vend -> error "unexpected end of input in doc" s spos
6959 | Vopen ("bookmarks", _, closed) ->
6960 if closed
6961 then v
6962 else { v with f = pbookmarks path pan anchor c bookmarks }
6964 | Vopen ("keymap", attrs, closed) ->
6965 let modename =
6966 try List.assoc "mode" attrs
6967 with Not_found -> "global"
6969 if closed
6970 then v
6971 else
6972 let ret keymap =
6973 let h = findkeyhash c modename in
6974 KeyMap.iter (Hashtbl.replace h) keymap;
6975 doc path pan anchor c bookmarks
6977 { v with f = pkeymap ret KeyMap.empty }
6979 | Vopen (_, _, _) ->
6980 error "unexpected subelement in doc" s spos
6982 | Vclose "doc" ->
6983 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6984 { v with f = llppconfig }
6986 | Vclose _ -> error "unexpected close in doc" s spos
6988 and pkeymap ret keymap v t spos _ =
6989 match t with
6990 | Vdata | Vcdata -> v
6991 | Vend -> error "unexpected end of input in keymap" s spos
6992 | Vopen ("map", attrs, closed) ->
6993 let r, l = map_of attrs in
6994 let kss = fromstring keys_of_string spos "in" r [] in
6995 let lss = fromstring keys_of_string spos "out" l [] in
6996 let keymap =
6997 match kss with
6998 | [] -> keymap
6999 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7000 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7002 if closed
7003 then { v with f = pkeymap ret keymap }
7004 else
7005 let f () = v in
7006 { v with f = skip "map" f }
7008 | Vopen _ ->
7009 error "unexpected subelement in keymap" s spos
7011 | Vclose "keymap" ->
7012 { v with f = ret keymap }
7014 | Vclose _ -> error "unexpected close in keymap" s spos
7016 and pbookmarks path pan anchor c bookmarks v t spos _ =
7017 match t with
7018 | Vdata | Vcdata -> v
7019 | Vend -> error "unexpected end of input in bookmarks" s spos
7020 | Vopen ("item", attrs, closed) ->
7021 let titleent, spage, srely, svisy = bookmark_of attrs in
7022 let page = fromstring int_of_string spos "page" spage 0
7023 and rely = fromstring float_of_string spos "rely" srely 0.0
7024 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7025 let bookmarks =
7026 (unent titleent, 0, (page, rely, visy)) :: bookmarks
7028 if closed
7029 then { v with f = pbookmarks path pan anchor c bookmarks }
7030 else
7031 let f () = v in
7032 { v with f = skip "item" f }
7034 | Vopen _ ->
7035 error "unexpected subelement in bookmarks" s spos
7037 | Vclose "bookmarks" ->
7038 { v with f = doc path pan anchor c bookmarks }
7040 | Vclose _ -> error "unexpected close in bookmarks" s spos
7042 and skip tag f v t spos _ =
7043 match t with
7044 | Vdata | Vcdata -> v
7045 | Vend ->
7046 error ("unexpected end of input in skipped " ^ tag) s spos
7047 | Vopen (tag', _, closed) ->
7048 if closed
7049 then v
7050 else
7051 let f' () = { v with f = skip tag f } in
7052 { v with f = skip tag' f' }
7053 | Vclose ctag ->
7054 if tag = ctag
7055 then f ()
7056 else error ("unexpected close in skipped " ^ tag) s spos
7059 parse { f = toplevel; accu = () } s;
7060 h, dc;
7063 let do_load f ic =
7065 let len = in_channel_length ic in
7066 let s = String.create len in
7067 really_input ic s 0 len;
7068 f s;
7069 with
7070 | Parse_error (msg, s, pos) ->
7071 let subs = subs s pos in
7072 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7074 | exn ->
7075 failwith ("config load error: " ^ exntos exn)
7078 let defconfpath =
7079 let dir =
7081 let dir = Filename.concat home ".config" in
7082 if Sys.is_directory dir then dir else home
7083 with _ -> home
7085 Filename.concat dir "llpp.conf"
7088 let confpath = ref defconfpath;;
7090 let load1 f =
7091 if Sys.file_exists !confpath
7092 then
7093 match
7094 (try Some (open_in_bin !confpath)
7095 with exn ->
7096 prerr_endline
7097 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7098 exntos exn);
7099 None
7101 with
7102 | Some ic ->
7103 let success =
7105 f (do_load get ic)
7106 with exn ->
7107 prerr_endline
7108 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7109 exntos exn);
7110 false
7112 close_in ic;
7113 success
7115 | None -> false
7116 else
7117 f (Hashtbl.create 0, defconf)
7120 let load () =
7121 let f (h, dc) =
7122 let pc, pb, px, pa =
7124 let key =
7125 if emptystr state.origin
7126 then state.path
7127 else state.origin
7129 Hashtbl.find h (Filename.basename key)
7130 with Not_found -> dc, [], 0, emptyanchor
7132 setconf defconf dc;
7133 setconf conf pc;
7134 state.bookmarks <- pb;
7135 state.x <- px;
7136 if conf.jumpback
7137 then state.anchor <- pa;
7138 cbput state.hists.nav pa;
7139 true
7141 load1 f
7144 let add_attrs bb always dc c =
7145 let ob s a b =
7146 if always || a != b
7147 then Printf.bprintf bb "\n %s='%b'" s a
7148 and op s a b =
7149 if always || a <> b
7150 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7151 and oi s a b =
7152 if always || a != b
7153 then Printf.bprintf bb "\n %s='%d'" s a
7154 and oI s a b =
7155 if always || a != b
7156 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7157 and oz s a b =
7158 if always || a <> b
7159 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7160 and oF s a b =
7161 if always || a <> b
7162 then Printf.bprintf bb "\n %s='%f'" s a
7163 and oc s a b =
7164 if always || a <> b
7165 then
7166 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7167 and oC s a b =
7168 if always || a <> b
7169 then
7170 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7171 and oR s a b =
7172 if always || a <> b
7173 then
7174 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7175 and os s a b =
7176 if always || a <> b
7177 then
7178 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7179 and og s a b =
7180 if always || a <> b
7181 then
7182 match a with
7183 | None -> ()
7184 | Some (_N, _A, _B) ->
7185 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7186 and oW s a b =
7187 if always || a <> b
7188 then
7189 let v =
7190 match a with
7191 | None -> "false"
7192 | Some f ->
7193 if f = infinity
7194 then "true"
7195 else string_of_float f
7197 Printf.bprintf bb "\n %s='%s'" s v
7198 and oco s a b =
7199 if always || a <> b
7200 then
7201 match a with
7202 | Cmulti ((n, a, b), _) when n > 1 ->
7203 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7204 | Csplit (n, _) when n > 1 ->
7205 Printf.bprintf bb "\n %s='%d'" s ~-n
7206 | _ -> ()
7207 and obeco s a b =
7208 if always || a <> b
7209 then
7210 match a with
7211 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7212 | _ -> ()
7213 and oFm s a b =
7214 if always || a <> b
7215 then
7216 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7217 and oSv s a b m =
7218 if always || a <> b
7219 then
7220 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7221 and oPm s a b =
7222 if always || a <> b
7223 then
7224 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7226 oi "width" c.cwinw dc.cwinw;
7227 oi "height" c.cwinh dc.cwinh;
7228 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7229 oi "scroll-handle-height" c.scrollh dc.scrollh;
7230 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7231 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7232 ob "case-insensitive-search" c.icase dc.icase;
7233 ob "preload" c.preload dc.preload;
7234 oi "page-bias" c.pagebias dc.pagebias;
7235 oi "scroll-step" c.scrollstep dc.scrollstep;
7236 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7237 ob "max-height-fit" c.maxhfit dc.maxhfit;
7238 ob "crop-hack" c.crophack dc.crophack;
7239 oW "throttle" c.maxwait dc.maxwait;
7240 ob "highlight-links" c.hlinks dc.hlinks;
7241 ob "under-cursor-info" c.underinfo dc.underinfo;
7242 oi "vertical-margin" c.interpagespace dc.interpagespace;
7243 oz "zoom" c.zoom dc.zoom;
7244 ob "presentation" c.presentation dc.presentation;
7245 oi "rotation-angle" c.angle dc.angle;
7246 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7247 oFm "fit-model" c.fitmodel dc.fitmodel;
7248 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7249 oi "tex-count" c.texcount dc.texcount;
7250 oi "slice-height" c.sliceheight dc.sliceheight;
7251 oi "thumbnail-width" c.thumbw dc.thumbw;
7252 ob "persistent-location" c.jumpback dc.jumpback;
7253 oc "background-color" c.bgcolor dc.bgcolor;
7254 oi "tile-width" c.tilew dc.tilew;
7255 oi "tile-height" c.tileh dc.tileh;
7256 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7257 ob "checkers" c.checkers dc.checkers;
7258 oi "aalevel" c.aalevel dc.aalevel;
7259 ob "trim-margins" c.trimmargins dc.trimmargins;
7260 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7261 os "uri-launcher" c.urilauncher dc.urilauncher;
7262 os "path-launcher" c.pathlauncher dc.pathlauncher;
7263 oC "color-space" c.colorspace dc.colorspace;
7264 ob "invert-colors" c.invert dc.invert;
7265 oF "brightness" c.colorscale dc.colorscale;
7266 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7267 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7268 oco "columns" c.columns dc.columns;
7269 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7270 os "selection-command" c.selcmd dc.selcmd;
7271 os "synctex-command" c.stcmd dc.stcmd;
7272 os "pax-command" c.paxcmd dc.paxcmd;
7273 ob "update-cursor" c.updatecurs dc.updatecurs;
7274 oi "hint-font-size" c.hfsize dc.hfsize;
7275 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7276 oF "page-scroll-scale" c.pgscale dc.pgscale;
7277 ob "use-pbo" c.usepbo dc.usepbo;
7278 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7279 ob "remote-in-a-new-instance" c.riani dc.riani;
7280 op "point-and-x" c.pax dc.pax;
7281 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7284 let keymapsbuf always dc c =
7285 let bb = Buffer.create 16 in
7286 let rec loop = function
7287 | [] -> ()
7288 | (modename, h) :: rest ->
7289 let dh = findkeyhash dc modename in
7290 if always || h <> dh
7291 then (
7292 if Hashtbl.length h > 0
7293 then (
7294 if Buffer.length bb > 0
7295 then Buffer.add_char bb '\n';
7296 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7297 Hashtbl.iter (fun i o ->
7298 let isdifferent = always ||
7300 let dO = Hashtbl.find dh i in
7301 dO <> o
7302 with Not_found -> true
7304 if isdifferent
7305 then
7306 let addkm (k, m) =
7307 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7308 if Wsi.withalt m then Buffer.add_string bb "alt-";
7309 if Wsi.withshift m then Buffer.add_string bb "shift-";
7310 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7311 Buffer.add_string bb (Wsi.keyname k);
7313 let addkms l =
7314 let rec loop = function
7315 | [] -> ()
7316 | km :: [] -> addkm km
7317 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7319 loop l
7321 Buffer.add_string bb "<map in='";
7322 addkm i;
7323 match o with
7324 | KMinsrt km ->
7325 Buffer.add_string bb "' out='";
7326 addkm km;
7327 Buffer.add_string bb "'/>\n"
7329 | KMinsrl kms ->
7330 Buffer.add_string bb "' out='";
7331 addkms kms;
7332 Buffer.add_string bb "'/>\n"
7334 | KMmulti (ins, kms) ->
7335 Buffer.add_char bb ' ';
7336 addkms ins;
7337 Buffer.add_string bb "' out='";
7338 addkms kms;
7339 Buffer.add_string bb "'/>\n"
7340 ) h;
7341 Buffer.add_string bb "</keymap>";
7344 loop rest
7346 loop c.keyhashes;
7350 let save () =
7351 let uifontsize = fstate.fontsize in
7352 let bb = Buffer.create 32768 in
7353 let relx = float state.x /. float state.winw in
7354 let w, h, x =
7355 let cx w = truncate (relx *. float w) in
7356 List.fold_left
7357 (fun (w, h, x) ws ->
7358 match ws with
7359 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7360 | Wsi.MaxVert -> (w, conf.cwinh, x)
7361 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7363 (state.winw, state.winh, state.x) state.winstate
7365 conf.cwinw <- w;
7366 conf.cwinh <- h;
7367 let f (h, dc) =
7368 let dc = if conf.bedefault then conf else dc in
7369 Buffer.add_string bb "<llppconfig>\n";
7371 if nonemptystr !fontpath
7372 then
7373 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7374 uifontsize
7375 !fontpath
7376 else (
7377 if uifontsize <> 14
7378 then
7379 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7382 Buffer.add_string bb "<defaults ";
7383 add_attrs bb true dc dc;
7384 let kb = keymapsbuf true dc dc in
7385 if Buffer.length kb > 0
7386 then (
7387 Buffer.add_string bb ">\n";
7388 Buffer.add_buffer bb kb;
7389 Buffer.add_string bb "\n</defaults>\n";
7391 else Buffer.add_string bb "/>\n";
7393 let adddoc path pan anchor c bookmarks =
7394 if bookmarks == [] && c = dc && anchor = emptyanchor
7395 then ()
7396 else (
7397 Printf.bprintf bb "<doc path='%s'"
7398 (enent path 0 (String.length path));
7400 if anchor <> emptyanchor
7401 then (
7402 let n, rely, visy = anchor in
7403 Printf.bprintf bb " page='%d'" n;
7404 if rely > 1e-6
7405 then
7406 Printf.bprintf bb " rely='%f'" rely
7408 if abs_float visy > 1e-6
7409 then
7410 Printf.bprintf bb " visy='%f'" visy
7414 if pan != 0
7415 then Printf.bprintf bb " pan='%d'" pan;
7417 add_attrs bb false dc c;
7418 let kb = keymapsbuf false dc c in
7420 begin match bookmarks with
7421 | [] ->
7422 if Buffer.length kb > 0
7423 then (
7424 Buffer.add_string bb ">\n";
7425 Buffer.add_buffer bb kb;
7426 Buffer.add_string bb "\n</doc>\n";
7428 else Buffer.add_string bb "/>\n"
7429 | _ ->
7430 Buffer.add_string bb ">\n<bookmarks>\n";
7431 List.iter (fun (title, _level, (page, rely, visy)) ->
7432 Printf.bprintf bb
7433 "<item title='%s' page='%d'"
7434 (enent title 0 (String.length title))
7435 page
7437 if rely > 1e-6
7438 then
7439 Printf.bprintf bb " rely='%f'" rely
7441 if abs_float visy > 1e-6
7442 then
7443 Printf.bprintf bb " visy='%f'" visy
7445 Buffer.add_string bb "/>\n";
7446 ) bookmarks;
7447 Buffer.add_string bb "</bookmarks>";
7448 if Buffer.length kb > 0
7449 then (
7450 Buffer.add_string bb "\n";
7451 Buffer.add_buffer bb kb;
7453 Buffer.add_string bb "\n</doc>\n";
7454 end;
7458 let pan, conf =
7459 match state.mode with
7460 | Birdseye (c, pan, _, _, _) ->
7461 let beyecolumns =
7462 match conf.columns with
7463 | Cmulti ((c, _, _), _) -> Some c
7464 | Csingle _ -> None
7465 | Csplit _ -> None
7466 and columns =
7467 match c.columns with
7468 | Cmulti (c, _) -> Cmulti (c, [||])
7469 | Csingle _ -> Csingle [||]
7470 | Csplit _ -> failwith "quit from bird's eye while split"
7472 pan, { c with beyecolumns = beyecolumns; columns = columns }
7473 | _ -> x, conf
7475 let basename = Filename.basename
7476 (if emptystr state.origin then state.path else state.origin)
7478 adddoc basename pan (getanchor ())
7479 (let conf =
7480 let autoscrollstep =
7481 match state.autoscroll with
7482 | Some step -> step
7483 | None -> conf.autoscrollstep
7485 match state.mode with
7486 | Birdseye (bc, _, _, _, _) ->
7487 { conf with
7488 zoom = bc.zoom;
7489 presentation = bc.presentation;
7490 interpagespace = bc.interpagespace;
7491 maxwait = bc.maxwait;
7492 autoscrollstep = autoscrollstep }
7493 | _ -> { conf with autoscrollstep = autoscrollstep }
7494 in conf)
7495 (if conf.savebmarks then state.bookmarks else []);
7497 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7498 if basename <> path
7499 then adddoc path x anchor c bookmarks
7500 ) h;
7501 Buffer.add_string bb "</llppconfig>\n";
7502 true;
7504 if load1 f && Buffer.length bb > 0
7505 then
7507 let tmp = !confpath ^ ".tmp" in
7508 let oc = open_out_bin tmp in
7509 Buffer.output_buffer oc bb;
7510 close_out oc;
7511 Unix.rename tmp !confpath;
7512 with exn ->
7513 prerr_endline
7514 ("error while saving configuration: " ^ exntos exn)
7516 end;;
7518 let adderrmsg src msg =
7519 Buffer.add_string state.errmsgs msg;
7520 state.newerrmsgs <- true;
7521 G.postRedisplay src
7524 let adderrfmt src fmt =
7525 Format.kprintf (fun s -> adderrmsg src s) fmt;
7528 let ract cmds =
7529 let cl = splitatspace cmds in
7530 let scan s fmt f =
7531 try Scanf.sscanf s fmt f
7532 with exn ->
7533 adderrfmt "remote exec"
7534 "error processing '%S': %s\n" cmds (exntos exn)
7536 match cl with
7537 | "reload" :: [] -> reload ()
7538 | "goto" :: args :: [] ->
7539 scan args "%u %f %f"
7540 (fun pageno x y ->
7541 let cmd, _ = state.geomcmds in
7542 if emptystr cmd
7543 then gotopagexy pageno x y
7544 else
7545 let f prevf () =
7546 gotopagexy pageno x y;
7547 prevf ()
7549 state.reprf <- f state.reprf
7551 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7552 | "rect" :: args :: [] ->
7553 scan args "%u %u %f %f %f %f"
7554 (fun pageno color x0 y0 x1 y1 ->
7555 onpagerect pageno (fun w h ->
7556 let _,w1,h1,_ = getpagedim pageno in
7557 let sw = float w1 /. float w
7558 and sh = float h1 /. float h in
7559 let x0s = x0 *. sw
7560 and x1s = x1 *. sw
7561 and y0s = y0 *. sh
7562 and y1s = y1 *. sh in
7563 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7564 debugrect rect;
7565 state.rects <- (pageno, color, rect) :: state.rects;
7566 G.postRedisplay "rect";
7569 | "activatewin" :: [] -> Wsi.activatewin ()
7570 | "quit" :: [] -> raise Quit
7571 | _ ->
7572 adderrfmt "remote command"
7573 "error processing remote command: %S\n" cmds;
7576 let remote =
7577 let scratch = String.create 80 in
7578 let buf = Buffer.create 80 in
7579 fun fd ->
7580 let rec tempfr () =
7581 try Some (Unix.read fd scratch 0 80)
7582 with
7583 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7584 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7585 | exn -> raise exn
7587 match tempfr () with
7588 | None -> Some fd
7589 | Some n ->
7590 if n = 0
7591 then (
7592 Unix.close fd;
7593 if Buffer.length buf > 0
7594 then (
7595 let s = Buffer.contents buf in
7596 Buffer.clear buf;
7597 ract s;
7599 None
7601 else
7602 let rec eat ppos =
7603 let nlpos =
7605 let pos = String.index_from scratch ppos '\n' in
7606 if pos >= n then -1 else pos
7607 with Not_found -> -1
7609 if nlpos >= 0
7610 then (
7611 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7612 let s = Buffer.contents buf in
7613 Buffer.clear buf;
7614 ract s;
7615 eat (nlpos+1);
7617 else (
7618 Buffer.add_substring buf scratch ppos (n-ppos);
7619 Some fd
7621 in eat 0
7624 let remoteopen path =
7625 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7626 with exn ->
7627 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7628 None
7631 let () =
7632 let trimcachepath = ref "" in
7633 let rcmdpath = ref "" in
7634 selfexec := Sys.executable_name;
7635 Arg.parse
7636 (Arg.align
7637 [("-p", Arg.String (fun s -> state.password <- s),
7638 "<password> Set password");
7640 ("-f", Arg.String
7641 (fun s ->
7642 Config.fontpath := s;
7643 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7645 "<path> Set path to the user interface font");
7647 ("-c", Arg.String
7648 (fun s ->
7649 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7650 Config.confpath := s),
7651 "<path> Set path to the configuration file");
7653 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7654 "<path> Set path to the trim cache file");
7656 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7657 "<named-destination> Set named destination");
7659 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7660 ("-cxack", Arg.Set cxack, " Cut corners");
7662 ("-remote", Arg.String (fun s -> rcmdpath := s),
7663 "<path> Set path to the remote commands source");
7665 ("-origin", Arg.String (fun s -> state.origin <- s),
7666 "<original-path> Set original path");
7668 ("-v", Arg.Unit (fun () ->
7669 Printf.printf
7670 "%s\nconfiguration path: %s\n"
7671 (version ())
7672 Config.defconfpath
7674 exit 0), " Print version and exit");
7677 (fun s -> state.path <- s)
7678 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7680 if !wtmode
7681 then selfexec := !selfexec ^ " -wtmode";
7683 if emptystr state.path
7684 then (prerr_endline "file name missing"; exit 1);
7686 if not (Config.load ())
7687 then prerr_endline "failed to load configuration";
7689 let wsfd, winw, winh = Wsi.init (object
7690 val mutable m_hack = false
7691 method expose = if not m_hack then G.postRedisplay "expose"
7692 method visible = G.postRedisplay "visible"
7693 method display = m_hack <- false; display ()
7694 method reshape w h =
7695 m_hack <- w < state.winw && h < state.winh;
7696 reshape w h
7697 method mouse b d x y m = mouse b d x y m
7698 method motion x y = state.mpos <- (x, y); motion x y
7699 method pmotion x y = state.mpos <- (x, y); pmotion x y
7700 method key k m =
7701 let mascm = m land (
7702 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7703 ) in
7704 match state.keystate with
7705 | KSnone ->
7706 let km = k, mascm in
7707 begin
7708 match
7709 let modehash = state.uioh#modehash in
7710 try Hashtbl.find modehash km
7711 with Not_found ->
7712 try Hashtbl.find (findkeyhash conf "global") km
7713 with Not_found -> KMinsrt (k, m)
7714 with
7715 | KMinsrt (k, m) -> keyboard k m
7716 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7717 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7719 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7720 List.iter (fun (k, m) -> keyboard k m) insrt;
7721 state.keystate <- KSnone
7722 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7723 state.keystate <- KSinto (keys, insrt)
7724 | _ ->
7725 state.keystate <- KSnone
7727 method enter x y = state.mpos <- (x, y); pmotion x y
7728 method leave = state.mpos <- (-1, -1)
7729 method winstate wsl = state.winstate <- wsl; m_hack <- false
7730 method quit = raise Quit
7731 end) conf.cwinw conf.cwinh (platform = Posx) in
7733 state.wsfd <- wsfd;
7735 if not (
7736 List.exists GlMisc.check_extension
7737 [ "GL_ARB_texture_rectangle"
7738 ; "GL_EXT_texture_recangle"
7739 ; "GL_NV_texture_rectangle" ]
7741 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7743 if (
7744 let r = GlMisc.get_string `renderer in
7745 let p = "Mesa DRI Intel(" in
7746 let l = String.length p in
7747 String.length r > l && String.sub r 0 l = p
7749 then (
7750 defconf.sliceheight <- 1024;
7751 defconf.texcount <- 32;
7752 defconf.usepbo <- true;
7755 let cr, sw =
7756 match Ne.pipe () with
7757 | Ne.Exn exn ->
7758 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7759 exit 1
7760 | Ne.Res rw -> rw
7761 and sr, cw =
7762 match Ne.pipe () with
7763 | Ne.Exn exn ->
7764 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7765 exit 1
7766 | Ne.Res rw -> rw
7769 cloexec cr;
7770 cloexec sw;
7771 cloexec sr;
7772 cloexec cw;
7774 setcheckers conf.checkers;
7775 redirectstderr ();
7776 if conf.redirectstderr
7777 then
7778 at_exit (fun () ->
7779 let s = Buffer.contents state.errmsgs ^
7780 (match state.errfd with
7781 | Some fd ->
7782 let s = String.create (80*24) in
7783 let n =
7785 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7786 if List.mem fd r
7787 then Unix.read fd s 0 (String.length s)
7788 else 0
7789 with _ -> 0
7791 if n = 0
7792 then ""
7793 else String.sub s 0 n
7794 | None -> ""
7797 try ignore (Unix.write state.stderr s 0 (String.length s))
7798 with exn -> print_endline (exntos exn)
7802 init (cr, cw) (
7803 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7804 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7805 !Config.fontpath, !trimcachepath,
7806 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7808 state.sr <- sr;
7809 state.sw <- sw;
7810 state.text <- "Opening " ^ (mbtoutf8 state.path);
7811 reshape winw winh;
7812 opendoc state.path state.password;
7813 state.uioh <- uioh;
7814 display ();
7815 Wsi.mapwin ();
7816 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7817 let optrfd =
7818 ref (
7819 if nonemptystr !rcmdpath
7820 then remoteopen !rcmdpath
7821 else None
7825 let rec loop deadline =
7826 let r =
7827 match state.errfd with
7828 | None -> [state.sr; state.wsfd]
7829 | Some fd -> [state.sr; state.wsfd; fd]
7831 let r =
7832 match !optrfd with
7833 | None -> r
7834 | Some fd -> fd :: r
7836 if state.redisplay
7837 then (
7838 state.redisplay <- false;
7839 display ();
7841 let timeout =
7842 let now = now () in
7843 if deadline > now
7844 then (
7845 if deadline = infinity
7846 then ~-.1.0
7847 else max 0.0 (deadline -. now)
7849 else 0.0
7851 let r, _, _ =
7852 try Unix.select r [] [] timeout
7853 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7855 begin match r with
7856 | [] ->
7857 state.ghyll None;
7858 let newdeadline =
7859 if state.ghyll == noghyll
7860 then
7861 match state.autoscroll with
7862 | Some step when step != 0 ->
7863 let y = state.y + step in
7864 let y =
7865 if y < 0
7866 then state.maxy
7867 else if y >= state.maxy then 0 else y
7869 gotoy y;
7870 if state.mode = View
7871 then state.text <- "";
7872 deadline +. 0.01
7873 | _ -> infinity
7874 else deadline +. 0.01
7876 loop newdeadline
7878 | l ->
7879 let rec checkfds = function
7880 | [] -> ()
7881 | fd :: rest when fd = state.sr ->
7882 let cmd = readcmd state.sr in
7883 act cmd;
7884 checkfds rest
7886 | fd :: rest when fd = state.wsfd ->
7887 Wsi.readresp fd;
7888 checkfds rest
7890 | fd :: rest when Some fd = !optrfd ->
7891 begin match remote fd with
7892 | None -> optrfd := remoteopen !rcmdpath;
7893 | opt -> optrfd := opt
7894 end;
7895 checkfds rest
7897 | fd :: rest ->
7898 let s = String.create 80 in
7899 let n = tempfailureretry (Unix.read fd s 0) 80 in
7900 if conf.redirectstderr
7901 then (
7902 Buffer.add_substring state.errmsgs s 0 n;
7903 state.newerrmsgs <- true;
7904 state.redisplay <- true;
7906 else (
7907 prerr_string (String.sub s 0 n);
7908 flush stderr;
7910 checkfds rest
7912 checkfds l;
7913 let newdeadline =
7914 let deadline1 =
7915 if deadline = infinity
7916 then now () +. 0.01
7917 else deadline
7919 match state.autoscroll with
7920 | Some step when step != 0 -> deadline1
7921 | _ -> if state.ghyll == noghyll then infinity else deadline1
7923 loop newdeadline
7924 end;
7927 loop infinity;
7928 with Quit ->
7929 Config.save ();