Cosmetics
[llpp.git] / main.ml
blobaf1a3d83a9c3ed7aa74180582d117d77d8279aaf
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 units = [(30, "G"); (20, "M"); (10, "K")] in
4283 let prettyint n =
4284 let rec loop s n =
4285 let h = n mod 1000 in
4286 let n = n / 1000 in
4287 if n = 0
4288 then string_of_int h ^ s
4289 else (
4290 let s = Printf.sprintf "_%03d%s" h s in
4291 loop s n
4294 loop "" n
4296 let rec find = function
4297 | [] -> prettyint n
4298 | (shift, suffix) :: rest ->
4299 if (n land ((1 lsl shift) - 1)) = 0
4300 then prettyint (n lsr shift) ^ suffix
4301 else find rest
4303 find units
4306 let defghyllscroll = (40, 8, 32);;
4307 let ghyllscroll_of_string s =
4308 let (n, a, b) as nab =
4309 if s = "default"
4310 then defghyllscroll
4311 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4313 if n <= a || n <= b || a >= b
4314 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4315 n a b;
4316 nab;
4319 let ghyllscroll_to_string ((n, a, b) as nab) =
4320 if nab = defghyllscroll
4321 then "default"
4322 else Printf.sprintf "%d,%d,%d" n a b;
4325 let describe_location () =
4326 let fn = page_of_y state.y in
4327 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4328 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4329 let percent =
4330 if maxy <= 0
4331 then 100.
4332 else (100. *. (float state.y /. float maxy))
4334 if fn = ln
4335 then
4336 Printf.sprintf "page %d of %d [%.2f%%]"
4337 (fn+1) state.pagecount percent
4338 else
4339 Printf.sprintf
4340 "pages %d-%d of %d [%.2f%%]"
4341 (fn+1) (ln+1) state.pagecount percent
4344 let setpresentationmode v =
4345 let n = page_of_y state.y in
4346 state.anchor <- (n, 0.0, 1.0);
4347 conf.presentation <- v;
4348 if conf.fitmodel = FitPage
4349 then reqlayout conf.angle conf.fitmodel;
4350 represent ();
4353 let enterinfomode =
4354 let btos b = if b then "\xe2\x88\x9a" else "" in
4355 let showextended = ref false in
4356 let leave mode = function
4357 | Confirm -> state.mode <- mode
4358 | Cancel -> state.mode <- mode in
4359 let src =
4360 (object
4361 val mutable m_first_time = true
4362 val mutable m_l = []
4363 val mutable m_a = [||]
4364 val mutable m_prev_uioh = nouioh
4365 val mutable m_prev_mode = View
4367 inherit lvsourcebase
4369 method reset prev_mode prev_uioh =
4370 m_a <- Array.of_list (List.rev m_l);
4371 m_l <- [];
4372 m_prev_mode <- prev_mode;
4373 m_prev_uioh <- prev_uioh;
4374 if m_first_time
4375 then (
4376 let rec loop n =
4377 if n >= Array.length m_a
4378 then ()
4379 else
4380 match m_a.(n) with
4381 | _, _, _, Action _ -> m_active <- n
4382 | _ -> loop (n+1)
4384 loop 0;
4385 m_first_time <- false;
4388 method int name get set =
4389 m_l <-
4390 (name, `int get, 1, Action (
4391 fun u ->
4392 let ondone s =
4393 try set (int_of_string s)
4394 with exn ->
4395 state.text <- Printf.sprintf "bad integer `%s': %s"
4396 s (exntos exn)
4398 state.text <- "";
4399 let te = name ^ ": ", "", None, intentry, ondone, true in
4400 state.mode <- Textentry (te, leave m_prev_mode);
4402 )) :: m_l
4404 method int_with_suffix name get set =
4405 m_l <-
4406 (name, `intws get, 1, Action (
4407 fun u ->
4408 let ondone s =
4409 try set (int_of_string_with_suffix s)
4410 with exn ->
4411 state.text <- Printf.sprintf "bad integer `%s': %s"
4412 s (exntos exn)
4414 state.text <- "";
4415 let te =
4416 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4418 state.mode <- Textentry (te, leave m_prev_mode);
4420 )) :: m_l
4422 method bool ?(offset=1) ?(btos=btos) name get set =
4423 m_l <-
4424 (name, `bool (btos, get), offset, Action (
4425 fun u ->
4426 let v = get () in
4427 set (not v);
4429 )) :: m_l
4431 method color name get set =
4432 m_l <-
4433 (name, `color get, 1, Action (
4434 fun u ->
4435 let invalid = (nan, nan, nan) in
4436 let ondone s =
4437 let c =
4438 try color_of_string s
4439 with exn ->
4440 state.text <- Printf.sprintf "bad color `%s': %s"
4441 s (exntos exn);
4442 invalid
4444 if c <> invalid
4445 then set c;
4447 let te = name ^ ": ", "", None, textentry, ondone, true in
4448 state.text <- color_to_string (get ());
4449 state.mode <- Textentry (te, leave m_prev_mode);
4451 )) :: m_l
4453 method string name get set =
4454 m_l <-
4455 (name, `string get, 1, Action (
4456 fun u ->
4457 let ondone s = set s in
4458 let te = name ^ ": ", "", None, textentry, ondone, true in
4459 state.mode <- Textentry (te, leave m_prev_mode);
4461 )) :: m_l
4463 method colorspace name get set =
4464 m_l <-
4465 (name, `string get, 1, Action (
4466 fun _ ->
4467 let source =
4468 (object
4469 inherit lvsourcebase
4471 initializer
4472 m_active <- CSTE.to_int conf.colorspace;
4473 m_first <- 0;
4475 method getitemcount =
4476 Array.length CSTE.names
4477 method getitem n =
4478 (CSTE.names.(n), 0)
4479 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4480 ignore (uioh, first, pan, qsearch);
4481 if not cancel then set active;
4482 None
4483 method hasaction _ = true
4484 end)
4486 state.text <- "";
4487 let modehash = findkeyhash conf "info" in
4488 coe (new listview ~source ~trusted:true ~modehash)
4489 )) :: m_l
4491 method paxmark name get set =
4492 m_l <-
4493 (name, `string get, 1, Action (
4494 fun _ ->
4495 let source =
4496 (object
4497 inherit lvsourcebase
4499 initializer
4500 m_active <- MTE.to_int conf.paxmark;
4501 m_first <- 0;
4503 method getitemcount = Array.length MTE.names
4504 method getitem n = (MTE.names.(n), 0)
4505 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4506 ignore (uioh, first, pan, qsearch);
4507 if not cancel then set active;
4508 None
4509 method hasaction _ = true
4510 end)
4512 state.text <- "";
4513 let modehash = findkeyhash conf "info" in
4514 coe (new listview ~source ~trusted:true ~modehash)
4515 )) :: m_l
4517 method fitmodel name get set =
4518 m_l <-
4519 (name, `string get, 1, Action (
4520 fun _ ->
4521 let source =
4522 (object
4523 inherit lvsourcebase
4525 initializer
4526 m_active <- FMTE.to_int conf.fitmodel;
4527 m_first <- 0;
4529 method getitemcount = Array.length FMTE.names
4530 method getitem n = (FMTE.names.(n), 0)
4531 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4532 ignore (uioh, first, pan, qsearch);
4533 if not cancel then set active;
4534 None
4535 method hasaction _ = true
4536 end)
4538 state.text <- "";
4539 let modehash = findkeyhash conf "info" in
4540 coe (new listview ~source ~trusted:true ~modehash)
4541 )) :: m_l
4543 method caption s offset =
4544 m_l <- (s, `empty, offset, Noaction) :: m_l
4546 method caption2 s f offset =
4547 m_l <- (s, `string f, offset, Noaction) :: m_l
4549 method getitemcount = Array.length m_a
4551 method getitem n =
4552 let tostr = function
4553 | `int f -> string_of_int (f ())
4554 | `intws f -> string_with_suffix_of_int (f ())
4555 | `string f -> f ()
4556 | `color f -> color_to_string (f ())
4557 | `bool (btos, f) -> btos (f ())
4558 | `empty -> ""
4560 let name, t, offset, _ = m_a.(n) in
4561 ((let s = tostr t in
4562 if nonemptystr s
4563 then Printf.sprintf "%s\t%s" name s
4564 else name),
4565 offset)
4567 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4568 let uiohopt =
4569 if not cancel
4570 then (
4571 m_qsearch <- qsearch;
4572 let uioh =
4573 match m_a.(active) with
4574 | _, _, _, Action f -> f uioh
4575 | _ -> uioh
4577 Some uioh
4579 else None
4581 m_active <- active;
4582 m_first <- first;
4583 m_pan <- pan;
4584 uiohopt
4586 method hasaction n =
4587 match m_a.(n) with
4588 | _, _, _, Action _ -> true
4589 | _ -> false
4590 end)
4592 let rec fillsrc prevmode prevuioh =
4593 let sep () = src#caption "" 0 in
4594 let colorp name get set =
4595 src#string name
4596 (fun () -> color_to_string (get ()))
4597 (fun v ->
4599 let c = color_of_string v in
4600 set c
4601 with exn ->
4602 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4605 let oldmode = state.mode in
4606 let birdseye = isbirdseye state.mode in
4608 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4610 src#bool "presentation mode"
4611 (fun () -> conf.presentation)
4612 (fun v -> setpresentationmode v);
4614 src#bool "ignore case in searches"
4615 (fun () -> conf.icase)
4616 (fun v -> conf.icase <- v);
4618 src#bool "preload"
4619 (fun () -> conf.preload)
4620 (fun v -> conf.preload <- v);
4622 src#bool "highlight links"
4623 (fun () -> conf.hlinks)
4624 (fun v -> conf.hlinks <- v);
4626 src#bool "under info"
4627 (fun () -> conf.underinfo)
4628 (fun v -> conf.underinfo <- v);
4630 src#bool "persistent bookmarks"
4631 (fun () -> conf.savebmarks)
4632 (fun v -> conf.savebmarks <- v);
4634 src#fitmodel "fit model"
4635 (fun () -> FMTE.to_string conf.fitmodel)
4636 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4638 src#bool "trim margins"
4639 (fun () -> conf.trimmargins)
4640 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4642 src#bool "persistent location"
4643 (fun () -> conf.jumpback)
4644 (fun v -> conf.jumpback <- v);
4646 sep ();
4647 src#int "inter-page space"
4648 (fun () -> conf.interpagespace)
4649 (fun n ->
4650 conf.interpagespace <- n;
4651 docolumns conf.columns;
4652 let pageno, py =
4653 match state.layout with
4654 | [] -> 0, 0
4655 | l :: _ ->
4656 l.pageno, l.pagey
4658 state.maxy <- calcheight ();
4659 let y = getpagey pageno in
4660 gotoy (y + py)
4663 src#int "page bias"
4664 (fun () -> conf.pagebias)
4665 (fun v -> conf.pagebias <- v);
4667 src#int "scroll step"
4668 (fun () -> conf.scrollstep)
4669 (fun n -> conf.scrollstep <- n);
4671 src#int "horizontal scroll step"
4672 (fun () -> conf.hscrollstep)
4673 (fun v -> conf.hscrollstep <- v);
4675 src#int "auto scroll step"
4676 (fun () ->
4677 match state.autoscroll with
4678 | Some step -> step
4679 | _ -> conf.autoscrollstep)
4680 (fun n ->
4681 if state.autoscroll <> None
4682 then state.autoscroll <- Some n;
4683 conf.autoscrollstep <- n);
4685 src#int "zoom"
4686 (fun () -> truncate (conf.zoom *. 100.))
4687 (fun v -> setzoom ((float v) /. 100.));
4689 src#int "rotation"
4690 (fun () -> conf.angle)
4691 (fun v -> reqlayout v conf.fitmodel);
4693 src#int "scroll bar width"
4694 (fun () -> conf.scrollbw)
4695 (fun v ->
4696 conf.scrollbw <- v;
4697 reshape state.winw state.winh;
4700 src#int "scroll handle height"
4701 (fun () -> conf.scrollh)
4702 (fun v -> conf.scrollh <- v;);
4704 src#int "thumbnail width"
4705 (fun () -> conf.thumbw)
4706 (fun v ->
4707 conf.thumbw <- min 4096 v;
4708 match oldmode with
4709 | Birdseye beye ->
4710 leavebirdseye beye false;
4711 enterbirdseye ()
4712 | _ -> ()
4715 let mode = state.mode in
4716 src#string "columns"
4717 (fun () ->
4718 match conf.columns with
4719 | Csingle _ -> "1"
4720 | Cmulti (multi, _) -> multicolumns_to_string multi
4721 | Csplit (count, _) -> "-" ^ string_of_int count
4723 (fun v ->
4724 let n, a, b = multicolumns_of_string v in
4725 setcolumns mode n a b);
4727 sep ();
4728 src#caption "Pixmap cache" 0;
4729 src#int_with_suffix "size (advisory)"
4730 (fun () -> conf.memlimit)
4731 (fun v -> conf.memlimit <- v);
4733 src#caption2 "used"
4734 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4735 (string_with_suffix_of_int state.memused)
4736 (Hashtbl.length state.tilemap)) 1;
4738 sep ();
4739 src#caption "Layout" 0;
4740 src#caption2 "Dimension"
4741 (fun () ->
4742 Printf.sprintf "%dx%d (virtual %dx%d)"
4743 state.winw state.winh
4744 state.w state.maxy)
4746 if conf.debug
4747 then
4748 src#caption2 "Position" (fun () ->
4749 Printf.sprintf "%dx%d" state.x state.y
4751 else
4752 src#caption2 "Position" (fun () -> describe_location ()) 1
4755 sep ();
4756 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4757 "Save these parameters as global defaults at exit"
4758 (fun () -> conf.bedefault)
4759 (fun v -> conf.bedefault <- v)
4762 sep ();
4763 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4764 src#bool ~offset:0 ~btos "Extended parameters"
4765 (fun () -> !showextended)
4766 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4767 if !showextended
4768 then (
4769 src#bool "checkers"
4770 (fun () -> conf.checkers)
4771 (fun v -> conf.checkers <- v; setcheckers v);
4772 src#bool "update cursor"
4773 (fun () -> conf.updatecurs)
4774 (fun v -> conf.updatecurs <- v);
4775 src#bool "verbose"
4776 (fun () -> conf.verbose)
4777 (fun v -> conf.verbose <- v);
4778 src#bool "invert colors"
4779 (fun () -> conf.invert)
4780 (fun v -> conf.invert <- v);
4781 src#bool "max fit"
4782 (fun () -> conf.maxhfit)
4783 (fun v -> conf.maxhfit <- v);
4784 src#bool "redirect stderr"
4785 (fun () -> conf.redirectstderr)
4786 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4787 src#bool "pax mode"
4788 (fun () -> conf.pax != None)
4789 (fun v ->
4790 if v
4791 then conf.pax <- Some (ref (now (), 0, 0))
4792 else conf.pax <- None);
4793 src#string "uri launcher"
4794 (fun () -> conf.urilauncher)
4795 (fun v -> conf.urilauncher <- v);
4796 src#string "path launcher"
4797 (fun () -> conf.pathlauncher)
4798 (fun v -> conf.pathlauncher <- v);
4799 src#string "tile size"
4800 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4801 (fun v ->
4803 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4804 conf.tilew <- max 64 w;
4805 conf.tileh <- max 64 h;
4806 flushtiles ();
4807 with exn ->
4808 state.text <- Printf.sprintf "bad tile size `%s': %s"
4809 v (exntos exn)
4811 src#int "texture count"
4812 (fun () -> conf.texcount)
4813 (fun v ->
4814 if realloctexts v
4815 then conf.texcount <- v
4816 else showtext '!' " Failed to set texture count please retry later"
4818 src#int "slice height"
4819 (fun () -> conf.sliceheight)
4820 (fun v ->
4821 conf.sliceheight <- v;
4822 wcmd "sliceh %d" conf.sliceheight;
4824 src#int "anti-aliasing level"
4825 (fun () -> conf.aalevel)
4826 (fun v ->
4827 conf.aalevel <- bound v 0 8;
4828 state.anchor <- getanchor ();
4829 opendoc state.path state.password;
4831 src#string "page scroll scaling factor"
4832 (fun () -> string_of_float conf.pgscale)
4833 (fun v ->
4835 let s = float_of_string v in
4836 conf.pgscale <- s
4837 with exn ->
4838 state.text <- Printf.sprintf
4839 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4842 src#int "ui font size"
4843 (fun () -> fstate.fontsize)
4844 (fun v -> setfontsize (bound v 5 100));
4845 src#int "hint font size"
4846 (fun () -> conf.hfsize)
4847 (fun v -> conf.hfsize <- bound v 5 100);
4848 colorp "background color"
4849 (fun () -> conf.bgcolor)
4850 (fun v -> conf.bgcolor <- v);
4851 src#bool "crop hack"
4852 (fun () -> conf.crophack)
4853 (fun v -> conf.crophack <- v);
4854 src#string "trim fuzz"
4855 (fun () -> irect_to_string conf.trimfuzz)
4856 (fun v ->
4858 conf.trimfuzz <- irect_of_string v;
4859 if conf.trimmargins
4860 then settrim true conf.trimfuzz;
4861 with exn ->
4862 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4864 src#string "throttle"
4865 (fun () ->
4866 match conf.maxwait with
4867 | None -> "show place holder if page is not ready"
4868 | Some time ->
4869 if time = infinity
4870 then "wait for page to fully render"
4871 else
4872 "wait " ^ string_of_float time
4873 ^ " seconds before showing placeholder"
4875 (fun v ->
4877 let f = float_of_string v in
4878 if f <= 0.0
4879 then conf.maxwait <- None
4880 else conf.maxwait <- Some f
4881 with exn ->
4882 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4884 src#string "ghyll scroll"
4885 (fun () ->
4886 match conf.ghyllscroll with
4887 | None -> ""
4888 | Some nab -> ghyllscroll_to_string nab
4890 (fun v ->
4892 let gs =
4893 if emptystr v
4894 then None
4895 else Some (ghyllscroll_of_string v)
4897 conf.ghyllscroll <- gs
4898 with exn ->
4899 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4901 src#string "selection command"
4902 (fun () -> conf.selcmd)
4903 (fun v -> conf.selcmd <- v);
4904 src#string "synctex command"
4905 (fun () -> conf.stcmd)
4906 (fun v -> conf.stcmd <- v);
4907 src#string "pax command"
4908 (fun () -> conf.paxcmd)
4909 (fun v -> conf.paxcmd <- v);
4910 src#colorspace "color space"
4911 (fun () -> CSTE.to_string conf.colorspace)
4912 (fun v ->
4913 conf.colorspace <- CSTE.of_int v;
4914 wcmd "cs %d" v;
4915 load state.layout;
4917 src#paxmark "pax mark method"
4918 (fun () -> MTE.to_string conf.paxmark)
4919 (fun v -> conf.paxmark <- MTE.of_int v);
4920 if pbousable ()
4921 then
4922 src#bool "use PBO"
4923 (fun () -> conf.usepbo)
4924 (fun v -> conf.usepbo <- v);
4925 src#bool "mouse wheel scrolls pages"
4926 (fun () -> conf.wheelbypage)
4927 (fun v -> conf.wheelbypage <- v);
4928 src#bool "open remote links in a new instance"
4929 (fun () -> conf.riani)
4930 (fun v -> conf.riani <- v);
4933 sep ();
4934 src#caption "Document" 0;
4935 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4936 src#caption2 "Pages"
4937 (fun () -> string_of_int state.pagecount) 1;
4938 src#caption2 "Dimensions"
4939 (fun () -> string_of_int (List.length state.pdims)) 1;
4940 if conf.trimmargins
4941 then (
4942 sep ();
4943 src#caption "Trimmed margins" 0;
4944 src#caption2 "Dimensions"
4945 (fun () -> string_of_int (List.length state.pdims)) 1;
4948 sep ();
4949 src#caption "OpenGL" 0;
4950 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4951 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4953 sep ();
4954 src#caption "Location" 0;
4955 if nonemptystr state.origin
4956 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4957 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4959 src#reset prevmode prevuioh;
4961 fun () ->
4962 state.text <- "";
4963 let prevmode = state.mode
4964 and prevuioh = state.uioh in
4965 fillsrc prevmode prevuioh;
4966 let source = (src :> lvsource) in
4967 let modehash = findkeyhash conf "info" in
4968 state.uioh <- coe (object (self)
4969 inherit listview ~source ~trusted:true ~modehash as super
4970 val mutable m_prevmemused = 0
4971 method infochanged = function
4972 | Memused ->
4973 if m_prevmemused != state.memused
4974 then (
4975 m_prevmemused <- state.memused;
4976 G.postRedisplay "memusedchanged";
4978 | Pdim -> G.postRedisplay "pdimchanged"
4979 | Docinfo -> fillsrc prevmode prevuioh
4981 method key key mask =
4982 if not (Wsi.withctrl mask)
4983 then
4984 match key with
4985 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4986 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4987 | _ -> super#key key mask
4988 else super#key key mask
4989 end);
4990 G.postRedisplay "info";
4993 let enterhelpmode =
4994 let source =
4995 (object
4996 inherit lvsourcebase
4997 method getitemcount = Array.length state.help
4998 method getitem n =
4999 let s, l, _ = state.help.(n) in
5000 (s, l)
5002 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5003 let optuioh =
5004 if not cancel
5005 then (
5006 m_qsearch <- qsearch;
5007 match state.help.(active) with
5008 | _, _, Action f -> Some (f uioh)
5009 | _ -> Some (uioh)
5011 else None
5013 m_active <- active;
5014 m_first <- first;
5015 m_pan <- pan;
5016 optuioh
5018 method hasaction n =
5019 match state.help.(n) with
5020 | _, _, Action _ -> true
5021 | _ -> false
5023 initializer
5024 m_active <- -1
5025 end)
5026 in fun () ->
5027 let modehash = findkeyhash conf "help" in
5028 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5029 G.postRedisplay "help";
5032 let entermsgsmode =
5033 let msgsource =
5034 let re = Str.regexp "[\r\n]" in
5035 (object
5036 inherit lvsourcebase
5037 val mutable m_items = [||]
5039 method getitemcount = 1 + Array.length m_items
5041 method getitem n =
5042 if n = 0
5043 then "[Clear]", 0
5044 else m_items.(n-1), 0
5046 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5047 ignore uioh;
5048 if not cancel
5049 then (
5050 if active = 0
5051 then Buffer.clear state.errmsgs;
5052 m_qsearch <- qsearch;
5054 m_active <- active;
5055 m_first <- first;
5056 m_pan <- pan;
5057 None
5059 method hasaction n =
5060 n = 0
5062 method reset =
5063 state.newerrmsgs <- false;
5064 let l = Str.split re (Buffer.contents state.errmsgs) in
5065 m_items <- Array.of_list l
5067 initializer
5068 m_active <- 0
5069 end)
5070 in fun () ->
5071 state.text <- "";
5072 msgsource#reset;
5073 let source = (msgsource :> lvsource) in
5074 let modehash = findkeyhash conf "listview" in
5075 state.uioh <- coe (object
5076 inherit listview ~source ~trusted:false ~modehash as super
5077 method display =
5078 if state.newerrmsgs
5079 then msgsource#reset;
5080 super#display
5081 end);
5082 G.postRedisplay "msgs";
5085 let quickbookmark ?title () =
5086 match state.layout with
5087 | [] -> ()
5088 | l :: _ ->
5089 let title =
5090 match title with
5091 | None ->
5092 let sec = Unix.gettimeofday () in
5093 let tm = Unix.localtime sec in
5094 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5095 (l.pageno+1)
5096 tm.Unix.tm_mday
5097 tm.Unix.tm_mon
5098 (tm.Unix.tm_year + 1900)
5099 tm.Unix.tm_hour
5100 tm.Unix.tm_min
5101 | Some title -> title
5103 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
5106 let setautoscrollspeed step goingdown =
5107 let incr = max 1 ((abs step) / 2) in
5108 let incr = if goingdown then incr else -incr in
5109 let astep = step + incr in
5110 state.autoscroll <- Some astep;
5113 let gotounder under =
5114 let getpath filename =
5115 let path =
5116 if nonemptystr filename
5117 then
5118 if Filename.is_relative filename
5119 then
5120 let dir = Filename.dirname state.path in
5121 let dir =
5122 if Filename.is_implicit dir
5123 then Filename.concat (Sys.getcwd ()) dir
5124 else dir
5126 Filename.concat dir filename
5127 else filename
5128 else ""
5130 if Sys.file_exists path
5131 then path
5132 else ""
5134 match under with
5135 | Ulinkgoto (pageno, top) ->
5136 if pageno >= 0
5137 then (
5138 addnav ();
5139 gotopage1 pageno top;
5142 | Ulinkuri s ->
5143 gotouri s
5145 | Uremote (filename, pageno) ->
5146 let path = getpath filename in
5147 if nonemptystr path
5148 then (
5149 if conf.riani
5150 then
5151 let command = !selfexec ^ " " ^ path in
5152 try popen command []
5153 with exn ->
5154 Printf.eprintf
5155 "failed to execute `%s': %s\n" command (exntos exn);
5156 flush stderr;
5157 else
5158 let anchor = getanchor () in
5159 let ranchor = state.path, state.password, anchor, state.origin in
5160 state.origin <- "";
5161 state.anchor <- (pageno, 0.0, 0.0);
5162 state.ranchors <- ranchor :: state.ranchors;
5163 opendoc path "";
5165 else showtext '!' ("Could not find " ^ filename)
5167 | Uremotedest (filename, destname) ->
5168 let path = getpath filename in
5169 if nonemptystr path
5170 then (
5171 if conf.riani
5172 then
5173 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
5174 try popen command []
5175 with exn ->
5176 Printf.eprintf
5177 "failed to execute `%s': %s\n" command (exntos exn);
5178 flush stderr;
5179 else
5180 let anchor = getanchor () in
5181 let ranchor = state.path, state.password, anchor, state.origin in
5182 state.origin <- "";
5183 state.nameddest <- destname;
5184 state.ranchors <- ranchor :: state.ranchors;
5185 opendoc path "";
5187 else showtext '!' ("Could not find " ^ filename)
5189 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5192 let canpan () =
5193 match conf.columns with
5194 | Csplit _ -> true
5195 | _ -> state.x != 0 || conf.zoom > 1.0
5198 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5200 let existsinrow pageno (columns, coverA, coverB) p =
5201 let last = ((pageno - coverA) mod columns) + columns in
5202 let rec any = function
5203 | [] -> false
5204 | l :: rest ->
5205 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5206 then p l
5207 else (
5208 if not (p l)
5209 then (if l.pageno = last then false else any rest)
5210 else true
5213 any state.layout
5216 let nextpage () =
5217 match state.layout with
5218 | [] ->
5219 let pageno = page_of_y state.y in
5220 gotoghyll (getpagey (pageno+1))
5221 | l :: rest ->
5222 match conf.columns with
5223 | Csingle _ ->
5224 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5225 then
5226 let y = clamp (pgscale state.winh) in
5227 gotoghyll y
5228 else
5229 let pageno = min (l.pageno+1) (state.pagecount-1) in
5230 gotoghyll (getpagey pageno)
5231 | Cmulti ((c, _, _) as cl, _) ->
5232 if conf.presentation
5233 && (existsinrow l.pageno cl
5234 (fun l -> l.pageh > l.pagey + l.pagevh))
5235 then
5236 let y = clamp (pgscale state.winh) in
5237 gotoghyll y
5238 else
5239 let pageno = min (l.pageno+c) (state.pagecount-1) in
5240 gotoghyll (getpagey pageno)
5241 | Csplit (n, _) ->
5242 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5243 then
5244 let pagey, pageh = getpageyh l.pageno in
5245 let pagey = pagey + pageh * l.pagecol in
5246 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5247 gotoghyll (pagey + pageh + ips)
5250 let prevpage () =
5251 match state.layout with
5252 | [] ->
5253 let pageno = page_of_y state.y in
5254 gotoghyll (getpagey (pageno-1))
5255 | l :: _ ->
5256 match conf.columns with
5257 | Csingle _ ->
5258 if conf.presentation && l.pagey != 0
5259 then
5260 gotoghyll (clamp (pgscale ~-(state.winh)))
5261 else
5262 let pageno = max 0 (l.pageno-1) in
5263 gotoghyll (getpagey pageno)
5264 | Cmulti ((c, _, coverB) as cl, _) ->
5265 if conf.presentation &&
5266 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5267 then
5268 gotoghyll (clamp (pgscale ~-(state.winh)))
5269 else
5270 let decr =
5271 if l.pageno = state.pagecount - coverB
5272 then 1
5273 else c
5275 let pageno = max 0 (l.pageno-decr) in
5276 gotoghyll (getpagey pageno)
5277 | Csplit (n, _) ->
5278 let y =
5279 if l.pagecol = 0
5280 then
5281 if l.pageno = 0
5282 then l.pagey
5283 else
5284 let pageno = max 0 (l.pageno-1) in
5285 let pagey, pageh = getpageyh pageno in
5286 pagey + (n-1)*pageh
5287 else
5288 let pagey, pageh = getpageyh l.pageno in
5289 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5291 gotoghyll y
5294 let viewkeyboard key mask =
5295 let enttext te =
5296 let mode = state.mode in
5297 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5298 state.text <- "";
5299 enttext ();
5300 G.postRedisplay "view:enttext"
5302 let ctrl = Wsi.withctrl mask in
5303 let key =
5304 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5306 match key with
5307 | 81 -> (* Q *)
5308 exit 0
5310 | 0xff63 -> (* insert *)
5311 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5312 then (
5313 state.mode <- LinkNav (Ltgendir 0);
5314 gotoy state.y;
5316 else showtext '!' "Keyboard link navigation does not work under rotation"
5318 | 0xff1b | 113 -> (* escape / q *)
5319 begin match state.mstate with
5320 | Mzoomrect _ ->
5321 state.mstate <- Mnone;
5322 Wsi.setcursor Wsi.CURSOR_INHERIT;
5323 G.postRedisplay "kill zoom rect";
5324 | _ ->
5325 begin match state.mode with
5326 | LinkNav _ ->
5327 state.mode <- View;
5328 G.postRedisplay "esc leave linknav"
5329 | _ ->
5330 match state.ranchors with
5331 | [] -> raise Quit
5332 | (path, password, anchor, origin) :: rest ->
5333 state.ranchors <- rest;
5334 state.anchor <- anchor;
5335 state.origin <- origin;
5336 state.nameddest <- "";
5337 opendoc path password
5338 end;
5339 end;
5341 | 0xff08 -> (* backspace *)
5342 gotoghyll (getnav ~-1)
5344 | 111 -> (* o *)
5345 enteroutlinemode ()
5347 | 117 -> (* u *)
5348 state.rects <- [];
5349 state.text <- "";
5350 G.postRedisplay "dehighlight";
5352 | 47 | 63 -> (* / ? *)
5353 let ondone isforw s =
5354 cbput state.hists.pat s;
5355 state.searchpattern <- s;
5356 search s isforw
5358 let s = String.create 1 in
5359 s.[0] <- Char.chr key;
5360 enttext (s, "", Some (onhist state.hists.pat),
5361 textentry, ondone (key = 47), true)
5363 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5364 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5365 setzoom (conf.zoom +. incr)
5367 | 43 | 0xffab -> (* + *)
5368 let ondone s =
5369 let n =
5370 try int_of_string s with exc ->
5371 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5372 max_int
5374 if n != max_int
5375 then (
5376 conf.pagebias <- n;
5377 state.text <- "page bias is now " ^ string_of_int n;
5380 enttext ("page bias: ", "", None, intentry, ondone, true)
5382 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5383 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5384 setzoom (max 0.01 (conf.zoom -. decr))
5386 | 45 | 0xffad -> (* - *)
5387 let ondone msg = state.text <- msg in
5388 enttext (
5389 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5390 optentry state.mode, ondone, true
5393 | 48 when ctrl -> (* ctrl-0 *)
5394 if conf.zoom = 1.0
5395 then (
5396 state.x <- 0;
5397 gotoy state.y
5399 else setzoom 1.0
5401 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5402 let cols =
5403 match conf.columns with
5404 | Csingle _ | Cmulti _ -> 1
5405 | Csplit (n, _) -> n
5407 let h = state.winh -
5408 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5410 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5411 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5412 then setzoom zoom
5414 | 51 when ctrl -> (* ctrl-3 *)
5415 let fm =
5416 match conf.fitmodel with
5417 | FitWidth -> FitProportional
5418 | FitProportional -> FitPage
5419 | FitPage -> FitWidth
5421 state.text <- "fit model: " ^ FMTE.to_string fm;
5422 reqlayout conf.angle fm
5424 | 0xffc6 -> (* f9 *)
5425 togglebirdseye ()
5427 | 57 when ctrl -> (* ctrl-9 *)
5428 togglebirdseye ()
5430 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5431 when not ctrl -> (* 0..9 *)
5432 let ondone s =
5433 let n =
5434 try int_of_string s with exc ->
5435 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5438 if n >= 0
5439 then (
5440 addnav ();
5441 cbput state.hists.pag (string_of_int n);
5442 gotopage1 (n + conf.pagebias - 1) 0;
5445 let pageentry text key =
5446 match Char.unsafe_chr key with
5447 | 'g' -> TEdone text
5448 | _ -> intentry text key
5450 let text = "x" in text.[0] <- Char.chr key;
5451 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5453 | 98 -> (* b *)
5454 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5455 reshape state.winw state.winh;
5457 | 66 -> (* B *)
5458 state.bzoom <- not state.bzoom;
5459 state.rects <- [];
5460 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5462 | 108 -> (* l *)
5463 conf.hlinks <- not conf.hlinks;
5464 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5465 G.postRedisplay "toggle highlightlinks";
5467 | 70 -> (* F *)
5468 state.glinks <- true;
5469 let mode = state.mode in
5470 state.mode <- Textentry (
5471 (":", "", None, linknentry, linkndone gotounder, false),
5472 (fun _ ->
5473 state.glinks <- false;
5474 state.mode <- mode)
5476 state.text <- "";
5477 G.postRedisplay "view:linkent(F)"
5479 | 121 -> (* y *)
5480 state.glinks <- true;
5481 let mode = state.mode in
5482 state.mode <- Textentry (
5484 ":", "", None, linknentry, linkndone (fun under ->
5485 selstring (undertext under);
5486 ), false
5488 fun _ ->
5489 state.glinks <- false;
5490 state.mode <- mode
5492 state.text <- "";
5493 G.postRedisplay "view:linkent"
5495 | 97 -> (* a *)
5496 begin match state.autoscroll with
5497 | Some step ->
5498 conf.autoscrollstep <- step;
5499 state.autoscroll <- None
5500 | None ->
5501 if conf.autoscrollstep = 0
5502 then state.autoscroll <- Some 1
5503 else state.autoscroll <- Some conf.autoscrollstep
5506 | 112 when ctrl -> (* ctrl-p *)
5507 launchpath ()
5509 | 80 -> (* P *)
5510 setpresentationmode (not conf.presentation);
5511 showtext ' ' ("presentation mode " ^
5512 if conf.presentation then "on" else "off");
5514 | 102 -> (* f *)
5515 if List.mem Wsi.Fullscreen state.winstate
5516 then Wsi.reshape conf.cwinw conf.cwinh
5517 else Wsi.fullscreen ()
5519 | 112 | 78 -> (* p|N *)
5520 search state.searchpattern false
5522 | 110 | 0xffc0 -> (* n|F3 *)
5523 search state.searchpattern true
5525 | 116 -> (* t *)
5526 begin match state.layout with
5527 | [] -> ()
5528 | l :: _ ->
5529 gotoghyll (getpagey l.pageno)
5532 | 32 -> (* space *)
5533 nextpage ()
5535 | 0xff9f | 0xffff -> (* delete *)
5536 prevpage ()
5538 | 61 -> (* = *)
5539 showtext ' ' (describe_location ());
5541 | 119 -> (* w *)
5542 begin match state.layout with
5543 | [] -> ()
5544 | l :: _ ->
5545 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5546 G.postRedisplay "w"
5549 | 39 -> (* ' *)
5550 enterbookmarkmode ()
5552 | 104 | 0xffbe -> (* h|F1 *)
5553 enterhelpmode ()
5555 | 105 -> (* i *)
5556 enterinfomode ()
5558 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5559 entermsgsmode ()
5561 | 109 -> (* m *)
5562 let ondone s =
5563 match state.layout with
5564 | l :: _ ->
5565 if nonemptystr s
5566 then
5567 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5568 | _ -> ()
5570 enttext ("bookmark: ", "", None, textentry, ondone, true)
5572 | 126 -> (* ~ *)
5573 quickbookmark ();
5574 showtext ' ' "Quick bookmark added";
5576 | 122 -> (* z *)
5577 begin match state.layout with
5578 | l :: _ ->
5579 let rect = getpdimrect l.pagedimno in
5580 let w, h =
5581 if conf.crophack
5582 then
5583 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5584 truncate (1.2 *. (rect.(3) -. rect.(0))))
5585 else
5586 (truncate (rect.(1) -. rect.(0)),
5587 truncate (rect.(3) -. rect.(0)))
5589 let w = truncate ((float w)*.conf.zoom)
5590 and h = truncate ((float h)*.conf.zoom) in
5591 if w != 0 && h != 0
5592 then (
5593 state.anchor <- getanchor ();
5594 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5596 G.postRedisplay "z";
5598 | [] -> ()
5601 | 120 -> state.roam ()
5602 | 60 | 62 -> (* < > *)
5603 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5605 | 91 | 93 -> (* [ ] *)
5606 conf.colorscale <-
5607 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5609 G.postRedisplay "brightness";
5611 | 99 when state.mode = View -> (* [alt-]c *)
5612 if Wsi.withalt mask
5613 then (
5614 if conf.zoom > 1.0
5615 then
5616 let m = (wadjsb state.winw - state.w) / 2 in
5617 state.x <- m;
5618 gotoy_and_clear_text state.y
5620 else
5621 let (c, a, b), z =
5622 match state.prevcolumns with
5623 | None -> (1, 0, 0), 1.0
5624 | Some (columns, z) ->
5625 let cab =
5626 match columns with
5627 | Csplit (c, _) -> -c, 0, 0
5628 | Cmulti ((c, a, b), _) -> c, a, b
5629 | Csingle _ -> 1, 0, 0
5631 cab, z
5633 setcolumns View c a b;
5634 setzoom z
5636 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5637 -> (* ctrl-shift- (kp) [up|down] *)
5638 let zoom, x = state.prevzoom in
5639 setzoom zoom;
5640 state.x <- x;
5642 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5643 begin match state.autoscroll with
5644 | None ->
5645 begin match state.mode with
5646 | Birdseye beye -> upbirdseye 1 beye
5647 | _ ->
5648 if ctrl
5649 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5650 else (
5651 if not (Wsi.withshift mask) && conf.presentation
5652 then prevpage ()
5653 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5656 | Some n ->
5657 setautoscrollspeed n false
5660 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5661 begin match state.autoscroll with
5662 | None ->
5663 begin match state.mode with
5664 | Birdseye beye -> downbirdseye 1 beye
5665 | _ ->
5666 if ctrl
5667 then gotoy_and_clear_text (clamp (state.winh/2))
5668 else (
5669 if not (Wsi.withshift mask) && conf.presentation
5670 then nextpage ()
5671 else gotoy_and_clear_text (clamp conf.scrollstep)
5674 | Some n ->
5675 setautoscrollspeed n true
5678 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5679 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5680 if canpan ()
5681 then
5682 let dx =
5683 if ctrl
5684 then state.winw / 2
5685 else conf.hscrollstep
5687 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5688 state.x <- panbound (state.x + dx);
5689 gotoy_and_clear_text state.y
5690 else (
5691 state.text <- "";
5692 G.postRedisplay "left/right"
5695 | 0xff55 | 0xff9a -> (* (kp) prior *)
5696 let y =
5697 if ctrl
5698 then
5699 match state.layout with
5700 | [] -> state.y
5701 | l :: _ -> state.y - l.pagey
5702 else
5703 clamp (pgscale (-state.winh))
5705 gotoghyll y
5707 | 0xff56 | 0xff9b -> (* (kp) next *)
5708 let y =
5709 if ctrl
5710 then
5711 match List.rev state.layout with
5712 | [] -> state.y
5713 | l :: _ -> getpagey l.pageno
5714 else
5715 clamp (pgscale state.winh)
5717 gotoghyll y
5719 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5720 gotoghyll 0
5721 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5722 gotoghyll (clamp state.maxy)
5724 | 0xff53 | 0xff98
5725 when Wsi.withalt mask -> (* alt-(kp) right *)
5726 gotoghyll (getnav 1)
5727 | 0xff51 | 0xff96
5728 when Wsi.withalt mask -> (* alt-(kp) left *)
5729 gotoghyll (getnav ~-1)
5731 | 114 -> (* r *)
5732 reload ()
5734 | 118 when conf.debug -> (* v *)
5735 state.rects <- [];
5736 List.iter (fun l ->
5737 match getopaque l.pageno with
5738 | None -> ()
5739 | Some opaque ->
5740 let x0, y0, x1, y1 = pagebbox opaque in
5741 let a,b = float x0, float y0 in
5742 let c,d = float x1, float y0 in
5743 let e,f = float x1, float y1 in
5744 let h,j = float x0, float y1 in
5745 let rect = (a,b,c,d,e,f,h,j) in
5746 debugrect rect;
5747 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5748 ) state.layout;
5749 G.postRedisplay "v";
5751 | _ ->
5752 vlog "huh? %s" (Wsi.keyname key)
5755 let linknavkeyboard key mask linknav =
5756 let getpage pageno =
5757 let rec loop = function
5758 | [] -> None
5759 | l :: _ when l.pageno = pageno -> Some l
5760 | _ :: rest -> loop rest
5761 in loop state.layout
5763 let doexact (pageno, n) =
5764 match getopaque pageno, getpage pageno with
5765 | Some opaque, Some l ->
5766 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5767 then
5768 let under = getlink opaque n in
5769 G.postRedisplay "link gotounder";
5770 gotounder under;
5771 state.mode <- View;
5772 else
5773 let opt, dir =
5774 match key with
5775 | 0xff50 -> (* home *)
5776 Some (findlink opaque LDfirst), -1
5778 | 0xff57 -> (* end *)
5779 Some (findlink opaque LDlast), 1
5781 | 0xff51 -> (* left *)
5782 Some (findlink opaque (LDleft n)), -1
5784 | 0xff53 -> (* right *)
5785 Some (findlink opaque (LDright n)), 1
5787 | 0xff52 -> (* up *)
5788 Some (findlink opaque (LDup n)), -1
5790 | 0xff54 -> (* down *)
5791 Some (findlink opaque (LDdown n)), 1
5793 | _ -> None, 0
5795 let pwl l dir =
5796 begin match findpwl l.pageno dir with
5797 | Pwlnotfound -> ()
5798 | Pwl pageno ->
5799 let notfound dir =
5800 state.mode <- LinkNav (Ltgendir dir);
5801 let y, h = getpageyh pageno in
5802 let y =
5803 if dir < 0
5804 then y + h - state.winh
5805 else y
5807 gotoy y
5809 begin match getopaque pageno, getpage pageno with
5810 | Some opaque, Some _ ->
5811 let link =
5812 let ld = if dir > 0 then LDfirst else LDlast in
5813 findlink opaque ld
5815 begin match link with
5816 | Lfound m ->
5817 showlinktype (getlink opaque m);
5818 state.mode <- LinkNav (Ltexact (pageno, m));
5819 G.postRedisplay "linknav jpage";
5820 | _ -> notfound dir
5821 end;
5822 | _ -> notfound dir
5823 end;
5824 end;
5826 begin match opt with
5827 | Some Lnotfound -> pwl l dir;
5828 | Some (Lfound m) ->
5829 if m = n
5830 then pwl l dir
5831 else (
5832 let _, y0, _, y1 = getlinkrect opaque m in
5833 if y0 < l.pagey
5834 then gotopage1 l.pageno y0
5835 else (
5836 let d = fstate.fontsize + 1 in
5837 if y1 - l.pagey > l.pagevh - d
5838 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5839 else G.postRedisplay "linknav";
5841 showlinktype (getlink opaque m);
5842 state.mode <- LinkNav (Ltexact (l.pageno, m));
5845 | None -> viewkeyboard key mask
5846 end;
5847 | _ -> viewkeyboard key mask
5849 if key = 0xff63
5850 then (
5851 state.mode <- View;
5852 G.postRedisplay "leave linknav"
5854 else
5855 match linknav with
5856 | Ltgendir _ -> viewkeyboard key mask
5857 | Ltexact exact -> doexact exact
5860 let keyboard key mask =
5861 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5862 then wcmd "interrupt"
5863 else state.uioh <- state.uioh#key key mask
5866 let birdseyekeyboard key mask
5867 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5868 let incr =
5869 match conf.columns with
5870 | Csingle _ -> 1
5871 | Cmulti ((c, _, _), _) -> c
5872 | Csplit _ -> failwith "bird's eye split mode"
5874 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5875 match key with
5876 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5877 let y, h = getpageyh pageno in
5878 let top = (state.winh - h) / 2 in
5879 gotoy (max 0 (y - top))
5880 | 0xff0d (* enter *)
5881 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5882 | 0xff1b -> leavebirdseye beye true (* escape *)
5883 | 0xff52 -> upbirdseye incr beye (* up *)
5884 | 0xff54 -> downbirdseye incr beye (* down *)
5885 | 0xff51 -> upbirdseye 1 beye (* left *)
5886 | 0xff53 -> downbirdseye 1 beye (* right *)
5888 | 0xff55 -> (* prior *)
5889 begin match state.layout with
5890 | l :: _ ->
5891 if l.pagey != 0
5892 then (
5893 state.mode <- Birdseye (
5894 oconf, leftx, l.pageno, hooverpageno, anchor
5896 gotopage1 l.pageno 0;
5898 else (
5899 let layout = layout (state.y-state.winh) (pgh state.layout) in
5900 match layout with
5901 | [] -> gotoy (clamp (-state.winh))
5902 | l :: _ ->
5903 state.mode <- Birdseye (
5904 oconf, leftx, l.pageno, hooverpageno, anchor
5906 gotopage1 l.pageno 0
5909 | [] -> gotoy (clamp (-state.winh))
5910 end;
5912 | 0xff56 -> (* next *)
5913 begin match List.rev state.layout with
5914 | l :: _ ->
5915 let layout = layout (state.y + (pgh state.layout)) state.winh in
5916 begin match layout with
5917 | [] ->
5918 let incr = l.pageh - l.pagevh in
5919 if incr = 0
5920 then (
5921 state.mode <-
5922 Birdseye (
5923 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5925 G.postRedisplay "birdseye pagedown";
5927 else gotoy (clamp (incr + conf.interpagespace*2));
5929 | l :: _ ->
5930 state.mode <-
5931 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5932 gotopage1 l.pageno 0;
5935 | [] -> gotoy (clamp state.winh)
5936 end;
5938 | 0xff50 -> (* home *)
5939 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5940 gotopage1 0 0
5942 | 0xff57 -> (* end *)
5943 let pageno = state.pagecount - 1 in
5944 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5945 if not (pagevisible state.layout pageno)
5946 then
5947 let h =
5948 match List.rev state.pdims with
5949 | [] -> state.winh
5950 | (_, _, h, _) :: _ -> h
5952 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5953 else G.postRedisplay "birdseye end";
5954 | _ -> viewkeyboard key mask
5957 let drawpage l =
5958 let color =
5959 match state.mode with
5960 | Textentry _ -> scalecolor 0.4
5961 | LinkNav _
5962 | View -> scalecolor 1.0
5963 | Birdseye (_, _, pageno, hooverpageno, _) ->
5964 if l.pageno = hooverpageno
5965 then scalecolor 0.9
5966 else (
5967 if l.pageno = pageno
5968 then scalecolor 1.0
5969 else scalecolor 0.8
5972 drawtiles l color;
5975 let postdrawpage l linkindexbase =
5976 match getopaque l.pageno with
5977 | Some opaque ->
5978 if tileready l l.pagex l.pagey
5979 then
5980 let x = l.pagedispx - l.pagex
5981 and y = l.pagedispy - l.pagey in
5982 let hlmask =
5983 match conf.columns with
5984 | Csingle _ | Cmulti _ ->
5985 (if conf.hlinks then 1 else 0)
5986 + (if state.glinks
5987 && not (isbirdseye state.mode) then 2 else 0)
5988 | _ -> 0
5990 let s =
5991 match state.mode with
5992 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5993 | _ -> ""
5995 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5996 else 0
5997 | _ -> 0
6000 let scrollindicator () =
6001 let sbw, ph, sh = state.uioh#scrollph in
6002 let sbh, pw, sw = state.uioh#scrollpw in
6004 GlDraw.color (0.64, 0.64, 0.64);
6005 GlDraw.rect
6006 (float (state.winw - sbw), 0.)
6007 (float state.winw, float state.winh)
6009 GlDraw.rect
6010 (0., float (state.winh - sbh))
6011 (float (wadjsb state.winw - 1), float state.winh)
6013 GlDraw.color (0.0, 0.0, 0.0);
6015 GlDraw.rect
6016 (float (state.winw - sbw), ph)
6017 (float state.winw, ph +. sh)
6019 GlDraw.rect
6020 (pw, float (state.winh - sbh))
6021 (pw +. sw, float state.winh)
6025 let showsel () =
6026 match state.mstate with
6027 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6030 | Msel ((x0, y0), (x1, y1)) ->
6031 let rec loop = function
6032 | l :: ls ->
6033 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
6034 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
6035 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
6036 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
6037 then
6038 match getopaque l.pageno with
6039 | Some opaque ->
6040 let x0, y0 = pagetranslatepoint l x0 y0 in
6041 let x1, y1 = pagetranslatepoint l x1 y1 in
6042 seltext opaque (x0, y0, x1, y1);
6043 | _ -> ()
6044 else loop ls
6045 | [] -> ()
6047 loop state.layout
6050 let showrects rects =
6051 Gl.enable `blend;
6052 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6053 GlDraw.polygon_mode `both `fill;
6054 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6055 List.iter
6056 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6057 List.iter (fun l ->
6058 if l.pageno = pageno
6059 then (
6060 let dx = float (l.pagedispx - l.pagex) in
6061 let dy = float (l.pagedispy - l.pagey) in
6062 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6063 GlDraw.begins `quads;
6065 GlDraw.vertex2 (x0+.dx, y0+.dy);
6066 GlDraw.vertex2 (x1+.dx, y1+.dy);
6067 GlDraw.vertex2 (x2+.dx, y2+.dy);
6068 GlDraw.vertex2 (x3+.dx, y3+.dy);
6070 GlDraw.ends ();
6072 ) state.layout
6073 ) rects
6075 Gl.disable `blend;
6078 let display () =
6079 GlClear.color (scalecolor2 conf.bgcolor);
6080 GlClear.clear [`color];
6081 List.iter drawpage state.layout;
6082 let rects =
6083 match state.mode with
6084 | LinkNav (Ltexact (pageno, linkno)) ->
6085 begin match getopaque pageno with
6086 | Some opaque ->
6087 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6088 (pageno, 5, (
6089 float x0, float y0,
6090 float x1, float y0,
6091 float x1, float y1,
6092 float x0, float y1)
6093 ) :: state.rects
6094 | None -> state.rects
6096 | _ -> state.rects
6098 showrects rects;
6099 let rec postloop linkindexbase = function
6100 | l :: rest ->
6101 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6102 postloop linkindexbase rest
6103 | [] -> ()
6105 showsel ();
6106 postloop 0 state.layout;
6107 state.uioh#display;
6108 begin match state.mstate with
6109 | Mzoomrect ((x0, y0), (x1, y1)) ->
6110 Gl.enable `blend;
6111 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6112 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6113 GlDraw.rect (float x0, float y0)
6114 (float x1, float y1);
6115 Gl.disable `blend;
6116 | _ -> ()
6117 end;
6118 enttext ();
6119 scrollindicator ();
6120 Wsi.swapb ();
6123 let zoomrect x y x1 y1 =
6124 let x0 = min x x1
6125 and x1 = max x x1
6126 and y0 = min y y1 in
6127 gotoy (state.y + y0);
6128 state.anchor <- getanchor ();
6129 let zoom = (float state.w) /. float (x1 - x0) in
6130 let margin =
6131 match conf.fitmodel, conf.columns with
6132 | FitPage, Csplit _ ->
6133 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6135 | _, _ ->
6136 let adjw = wadjsb state.winw in
6137 if state.w < adjw
6138 then (adjw - state.w) / 2
6139 else 0
6141 state.x <- (state.x + margin) - x0;
6142 setzoom zoom;
6143 Wsi.setcursor Wsi.CURSOR_INHERIT;
6144 state.mstate <- Mnone;
6147 let zoomblock x y =
6148 let g opaque l px py =
6149 match rectofblock opaque px py with
6150 | Some a ->
6151 let x0 = a.(0) -. 20. in
6152 let x1 = a.(1) +. 20. in
6153 let y0 = a.(2) -. 20. in
6154 let zoom = (float state.w) /. (x1 -. x0) in
6155 let pagey = getpagey l.pageno in
6156 gotoy_and_clear_text (pagey + truncate y0);
6157 state.anchor <- getanchor ();
6158 let margin = (state.w - l.pagew)/2 in
6159 state.x <- -truncate x0 - margin;
6160 setzoom zoom;
6161 None
6162 | None -> None
6164 match conf.columns with
6165 | Csplit _ ->
6166 showtext '!' "block zooming does not work properly in split columns mode"
6167 | _ -> onppundermouse g x y ()
6170 let scrollx x =
6171 let winw = wadjsb state.winw - 1 in
6172 let s = float x /. float winw in
6173 let destx = truncate (float (state.w + winw) *. s) in
6174 state.x <- winw - destx;
6175 gotoy_and_clear_text state.y;
6176 state.mstate <- Mscrollx;
6179 let scrolly y =
6180 let s = float y /. float state.winh in
6181 let desty = truncate (float (state.maxy - state.winh) *. s) in
6182 gotoy_and_clear_text desty;
6183 state.mstate <- Mscrolly;
6186 let viewmouse button down x y mask =
6187 match button with
6188 | n when (n == 4 || n == 5) && not down ->
6189 if Wsi.withctrl mask
6190 then (
6191 match state.mstate with
6192 | Mzoom (oldn, i) ->
6193 if oldn = n
6194 then (
6195 if i = 2
6196 then
6197 let incr =
6198 match n with
6199 | 5 ->
6200 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6201 | _ ->
6202 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6204 let zoom = conf.zoom -. incr in
6205 setzoom zoom;
6206 state.mstate <- Mzoom (n, 0);
6207 else
6208 state.mstate <- Mzoom (n, i+1);
6210 else state.mstate <- Mzoom (n, 0)
6212 | _ -> state.mstate <- Mzoom (n, 0)
6214 else (
6215 match state.autoscroll with
6216 | Some step -> setautoscrollspeed step (n=4)
6217 | None ->
6218 if conf.wheelbypage || conf.presentation
6219 then (
6220 if n = 4
6221 then prevpage ()
6222 else nextpage ()
6224 else
6225 let incr =
6226 if n = 4
6227 then -conf.scrollstep
6228 else conf.scrollstep
6230 let incr = incr * 2 in
6231 let y = clamp incr in
6232 gotoy_and_clear_text y
6235 | n when (n = 6 || n = 7) && not down && canpan () ->
6236 state.x <-
6237 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6238 gotoy_and_clear_text state.y
6240 | 1 when Wsi.withshift mask ->
6241 state.mstate <- Mnone;
6242 if not down
6243 then (
6244 match unproject x y with
6245 | Some (pageno, ux, uy) ->
6246 let cmd = Printf.sprintf
6247 "%s %s %d %d %d"
6248 conf.stcmd state.path pageno ux uy
6250 popen cmd []
6251 | None -> ()
6254 | 1 when Wsi.withctrl mask ->
6255 if down
6256 then (
6257 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6258 state.mstate <- Mpan (x, y)
6260 else
6261 state.mstate <- Mnone
6263 | 3 ->
6264 if down
6265 then (
6266 Wsi.setcursor Wsi.CURSOR_CYCLE;
6267 let p = (x, y) in
6268 state.mstate <- Mzoomrect (p, p)
6270 else (
6271 match state.mstate with
6272 | Mzoomrect ((x0, y0), _) ->
6273 if abs (x-x0) > 10 && abs (y - y0) > 10
6274 then zoomrect x0 y0 x y
6275 else (
6276 state.mstate <- Mnone;
6277 Wsi.setcursor Wsi.CURSOR_INHERIT;
6278 G.postRedisplay "kill accidental zoom rect";
6280 | _ ->
6281 Wsi.setcursor Wsi.CURSOR_INHERIT;
6282 state.mstate <- Mnone
6285 | 1 when x > state.winw - vscrollw () ->
6286 if down
6287 then
6288 let _, position, sh = state.uioh#scrollph in
6289 if y > truncate position && y < truncate (position +. sh)
6290 then state.mstate <- Mscrolly
6291 else scrolly y
6292 else
6293 state.mstate <- Mnone
6295 | 1 when y > state.winh - hscrollh () ->
6296 if down
6297 then
6298 let _, position, sw = state.uioh#scrollpw in
6299 if x > truncate position && x < truncate (position +. sw)
6300 then state.mstate <- Mscrollx
6301 else scrollx x
6302 else
6303 state.mstate <- Mnone
6305 | 1 when state.bzoom -> if not down then zoomblock x y
6307 | 1 ->
6308 let dest = if down then getunder x y else Unone in
6309 begin match dest with
6310 | Ulinkgoto _
6311 | Ulinkuri _
6312 | Uremote _ | Uremotedest _
6313 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6314 gotounder dest
6316 | Unone when down ->
6317 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6318 state.mstate <- Mpan (x, y);
6320 | Unone | Utext _ ->
6321 if down
6322 then (
6323 if conf.angle mod 360 = 0
6324 then (
6325 state.mstate <- Msel ((x, y), (x, y));
6326 G.postRedisplay "mouse select";
6329 else (
6330 match state.mstate with
6331 | Mnone -> ()
6333 | Mzoom _ | Mscrollx | Mscrolly ->
6334 state.mstate <- Mnone
6336 | Mzoomrect ((x0, y0), _) ->
6337 zoomrect x0 y0 x y
6339 | Mpan _ ->
6340 Wsi.setcursor Wsi.CURSOR_INHERIT;
6341 state.mstate <- Mnone
6343 | Msel ((x0, y0), (x1, y1)) ->
6344 let rec loop = function
6345 | [] -> ()
6346 | l :: rest ->
6347 let inside =
6348 let a0 = l.pagedispy in
6349 let a1 = a0 + l.pagevh in
6350 let b0 = l.pagedispx in
6351 let b1 = b0 + l.pagevw in
6352 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6353 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6355 if inside
6356 then
6357 match getopaque l.pageno with
6358 | Some opaque ->
6359 begin
6360 match Ne.pipe () with
6361 | Ne.Exn exn ->
6362 showtext '!'
6363 (Printf.sprintf
6364 "can not create sel pipe: %s"
6365 (exntos exn));
6366 | Ne.Res (r, w) ->
6367 let doclose what fd =
6368 Ne.clo fd (fun msg ->
6369 dolog "%s close failed: %s" what msg)
6372 popen conf.selcmd [r, 0; w, -1];
6373 copysel w opaque true;
6374 doclose "pipe/r" r;
6375 G.postRedisplay "copysel";
6376 with exn ->
6377 dolog "can not execute %S: %s"
6378 conf.selcmd (exntos exn);
6379 doclose "pipe/r" r;
6380 doclose "pipe/w" w;
6382 | None -> ()
6383 else loop rest
6385 loop state.layout;
6386 Wsi.setcursor Wsi.CURSOR_INHERIT;
6387 state.mstate <- Mnone;
6391 | _ -> ()
6394 let birdseyemouse button down x y mask
6395 (conf, leftx, _, hooverpageno, anchor) =
6396 match button with
6397 | 1 when down ->
6398 let rec loop = function
6399 | [] -> ()
6400 | l :: rest ->
6401 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6402 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6403 then (
6404 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6406 else loop rest
6408 loop state.layout
6409 | 3 -> ()
6410 | _ -> viewmouse button down x y mask
6413 let mouse button down x y mask =
6414 state.uioh <- state.uioh#button button down x y mask;
6417 let motion ~x ~y =
6418 state.uioh <- state.uioh#motion x y
6421 let pmotion ~x ~y =
6422 state.uioh <- state.uioh#pmotion x y;
6425 let uioh = object
6426 method display = ()
6428 method key key mask =
6429 begin match state.mode with
6430 | Textentry textentry -> textentrykeyboard key mask textentry
6431 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6432 | View -> viewkeyboard key mask
6433 | LinkNav linknav -> linknavkeyboard key mask linknav
6434 end;
6435 state.uioh
6437 method button button bstate x y mask =
6438 begin match state.mode with
6439 | LinkNav _
6440 | View -> viewmouse button bstate x y mask
6441 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6442 | Textentry _ -> ()
6443 end;
6444 state.uioh
6446 method motion x y =
6447 begin match state.mode with
6448 | Textentry _ -> ()
6449 | View | Birdseye _ | LinkNav _ ->
6450 match state.mstate with
6451 | Mzoom _ | Mnone -> ()
6453 | Mpan (x0, y0) ->
6454 let dx = x - x0
6455 and dy = y0 - y in
6456 state.mstate <- Mpan (x, y);
6457 if canpan ()
6458 then state.x <- panbound (state.x + dx);
6459 let y = clamp dy in
6460 gotoy_and_clear_text y
6462 | Msel (a, _) ->
6463 state.mstate <- Msel (a, (x, y));
6464 G.postRedisplay "motion select";
6466 | Mscrolly ->
6467 let y = min state.winh (max 0 y) in
6468 scrolly y
6470 | Mscrollx ->
6471 let x = min state.winw (max 0 x) in
6472 scrollx x
6474 | Mzoomrect (p0, _) ->
6475 state.mstate <- Mzoomrect (p0, (x, y));
6476 G.postRedisplay "motion zoomrect";
6477 end;
6478 state.uioh
6480 method pmotion x y =
6481 begin match state.mode with
6482 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6483 let rec loop = function
6484 | [] ->
6485 if hooverpageno != -1
6486 then (
6487 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6488 G.postRedisplay "pmotion birdseye no hoover";
6490 | l :: rest ->
6491 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6492 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6493 then (
6494 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6495 G.postRedisplay "pmotion birdseye hoover";
6497 else loop rest
6499 loop state.layout
6501 | Textentry _ -> ()
6503 | LinkNav _
6504 | View ->
6505 match state.mstate with
6506 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6508 | Mnone ->
6509 updateunder x y;
6510 match conf.pax with
6511 | None -> ()
6512 | Some r ->
6513 let past, _, _ = !r in
6514 let now = now () in
6515 let delta = now -. past in
6516 if delta > 0.01
6517 then paxunder x y
6518 else r := (now, x, y)
6519 end;
6520 state.uioh
6522 method infochanged _ = ()
6524 method scrollph =
6525 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6526 let p, h =
6527 if maxy = 0
6528 then 0.0, float state.winh
6529 else scrollph state.y maxy
6531 vscrollw (), p, h
6533 method scrollpw =
6534 let winw = wadjsb state.winw in
6535 let fwinw = float winw in
6536 let sw =
6537 let sw = fwinw /. float state.w in
6538 let sw = fwinw *. sw in
6539 max sw (float conf.scrollh)
6541 let position =
6542 let maxx = state.w + winw in
6543 let x = winw - state.x in
6544 let percent = float x /. float maxx in
6545 (fwinw -. sw) *. percent
6547 hscrollh (), position, sw
6549 method modehash =
6550 let modename =
6551 match state.mode with
6552 | LinkNav _ -> "links"
6553 | Textentry _ -> "textentry"
6554 | Birdseye _ -> "birdseye"
6555 | View -> "view"
6557 findkeyhash conf modename
6559 method eformsgs = true
6560 end;;
6562 module Config =
6563 struct
6564 open Parser
6566 let fontpath = ref "";;
6568 module KeyMap =
6569 Map.Make (struct type t = (int * int) let compare = compare end);;
6571 let unent s =
6572 let l = String.length s in
6573 let b = Buffer.create l in
6574 unent b s 0 l;
6575 Buffer.contents b;
6578 let home =
6579 try Sys.getenv "HOME"
6580 with exn ->
6581 prerr_endline
6582 ("Can not determine home directory location: " ^ exntos exn);
6586 let modifier_of_string = function
6587 | "alt" -> Wsi.altmask
6588 | "shift" -> Wsi.shiftmask
6589 | "ctrl" | "control" -> Wsi.ctrlmask
6590 | "meta" -> Wsi.metamask
6591 | _ -> 0
6594 let key_of_string =
6595 let r = Str.regexp "-" in
6596 fun s ->
6597 let elems = Str.full_split r s in
6598 let f n k m =
6599 let g s =
6600 let m1 = modifier_of_string s in
6601 if m1 = 0
6602 then (Wsi.namekey s, m)
6603 else (k, m lor m1)
6604 in function
6605 | Str.Delim s when n land 1 = 0 -> g s
6606 | Str.Text s -> g s
6607 | Str.Delim _ -> (k, m)
6609 let rec loop n k m = function
6610 | [] -> (k, m)
6611 | x :: xs ->
6612 let k, m = f n k m x in
6613 loop (n+1) k m xs
6615 loop 0 0 0 elems
6618 let keys_of_string =
6619 let r = Str.regexp "[ \t]" in
6620 fun s ->
6621 let elems = Str.split r s in
6622 List.map key_of_string elems
6625 let copykeyhashes c =
6626 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6629 let config_of c attrs =
6630 let apply c k v =
6632 match k with
6633 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6634 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6635 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6636 | "preload" -> { c with preload = bool_of_string v }
6637 | "page-bias" -> { c with pagebias = int_of_string v }
6638 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6639 | "horizontal-scroll-step" ->
6640 { c with hscrollstep = max (int_of_string v) 1 }
6641 | "auto-scroll-step" ->
6642 { c with autoscrollstep = max 0 (int_of_string v) }
6643 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6644 | "crop-hack" -> { c with crophack = bool_of_string v }
6645 | "throttle" ->
6646 let mw =
6647 match String.lowercase v with
6648 | "true" -> Some infinity
6649 | "false" -> None
6650 | f -> Some (float_of_string f)
6652 { c with maxwait = mw}
6653 | "highlight-links" -> { c with hlinks = bool_of_string v }
6654 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6655 | "vertical-margin" ->
6656 { c with interpagespace = max 0 (int_of_string v) }
6657 | "zoom" ->
6658 let zoom = float_of_string v /. 100. in
6659 let zoom = max zoom 0.0 in
6660 { c with zoom = zoom }
6661 | "presentation" -> { c with presentation = bool_of_string v }
6662 | "rotation-angle" -> { c with angle = int_of_string v }
6663 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6664 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6665 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6666 | "proportional-display" ->
6667 let fm =
6668 if bool_of_string v
6669 then FitProportional
6670 else FitWidth
6672 { c with fitmodel = fm }
6673 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6674 | "pixmap-cache-size" ->
6675 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6676 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6677 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6678 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6679 | "persistent-location" -> { c with jumpback = bool_of_string v }
6680 | "background-color" -> { c with bgcolor = color_of_string v }
6681 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6682 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6683 | "mupdf-store-size" ->
6684 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6685 | "checkers" -> { c with checkers = bool_of_string v }
6686 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6687 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6688 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6689 | "uri-launcher" -> { c with urilauncher = unent v }
6690 | "path-launcher" -> { c with pathlauncher = unent v }
6691 | "color-space" -> { c with colorspace = CSTE.of_string v }
6692 | "invert-colors" -> { c with invert = bool_of_string v }
6693 | "brightness" -> { c with colorscale = float_of_string v }
6694 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6695 | "ghyllscroll" ->
6696 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6697 | "columns" ->
6698 let (n, _, _) as nab = multicolumns_of_string v in
6699 if n < 0
6700 then { c with columns = Csplit (-n, [||]) }
6701 else { c with columns = Cmulti (nab, [||]) }
6702 | "birds-eye-columns" ->
6703 { c with beyecolumns = Some (max (int_of_string v) 2) }
6704 | "selection-command" -> { c with selcmd = unent v }
6705 | "synctex-command" -> { c with stcmd = unent v }
6706 | "pax-command" -> { c with paxcmd = unent v }
6707 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6708 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6709 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6710 | "use-pbo" -> { c with usepbo = bool_of_string v }
6711 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6712 | "horizontal-scrollbar-visible" ->
6713 let b =
6714 if bool_of_string v
6715 then c.scrollb lor scrollbhv
6716 else c.scrollb land (lnot scrollbhv)
6718 { c with scrollb = b }
6719 | "vertical-scrollbar-visible" ->
6720 let b =
6721 if bool_of_string v
6722 then c.scrollb lor scrollbvv
6723 else c.scrollb land (lnot scrollbvv)
6725 { c with scrollb = b }
6726 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6727 | "point-and-x" ->
6728 { c with pax =
6729 if bool_of_string v
6730 then Some (ref (0.0, 0, 0))
6731 else None }
6732 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6733 | _ -> c
6734 with exn ->
6735 prerr_endline ("Error processing attribute (`" ^
6736 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6739 let rec fold c = function
6740 | [] -> c
6741 | (k, v) :: rest ->
6742 let c = apply c k v in
6743 fold c rest
6745 fold { c with keyhashes = copykeyhashes c } attrs;
6748 let fromstring f pos n v d =
6749 try f v
6750 with exn ->
6751 dolog "Error processing attribute (%S=%S) at %d\n%s"
6752 n v pos (exntos exn)
6757 let bookmark_of attrs =
6758 let rec fold title page rely visy = function
6759 | ("title", v) :: rest -> fold v page rely visy rest
6760 | ("page", v) :: rest -> fold title v rely visy rest
6761 | ("rely", v) :: rest -> fold title page v visy rest
6762 | ("visy", v) :: rest -> fold title page rely v rest
6763 | _ :: rest -> fold title page rely visy rest
6764 | [] -> title, page, rely, visy
6766 fold "invalid" "0" "0" "0" attrs
6769 let doc_of attrs =
6770 let rec fold path page rely pan visy = function
6771 | ("path", v) :: rest -> fold v page rely pan visy rest
6772 | ("page", v) :: rest -> fold path v rely pan visy rest
6773 | ("rely", v) :: rest -> fold path page v pan visy rest
6774 | ("pan", v) :: rest -> fold path page rely v visy rest
6775 | ("visy", v) :: rest -> fold path page rely pan v rest
6776 | _ :: rest -> fold path page rely pan visy rest
6777 | [] -> path, page, rely, pan, visy
6779 fold "" "0" "0" "0" "0" attrs
6782 let map_of attrs =
6783 let rec fold rs ls = function
6784 | ("out", v) :: rest -> fold v ls rest
6785 | ("in", v) :: rest -> fold rs v rest
6786 | _ :: rest -> fold ls rs rest
6787 | [] -> ls, rs
6789 fold "" "" attrs
6792 let setconf dst src =
6793 dst.scrollbw <- src.scrollbw;
6794 dst.scrollh <- src.scrollh;
6795 dst.icase <- src.icase;
6796 dst.preload <- src.preload;
6797 dst.pagebias <- src.pagebias;
6798 dst.verbose <- src.verbose;
6799 dst.scrollstep <- src.scrollstep;
6800 dst.maxhfit <- src.maxhfit;
6801 dst.crophack <- src.crophack;
6802 dst.autoscrollstep <- src.autoscrollstep;
6803 dst.maxwait <- src.maxwait;
6804 dst.hlinks <- src.hlinks;
6805 dst.underinfo <- src.underinfo;
6806 dst.interpagespace <- src.interpagespace;
6807 dst.zoom <- src.zoom;
6808 dst.presentation <- src.presentation;
6809 dst.angle <- src.angle;
6810 dst.cwinw <- src.cwinw;
6811 dst.cwinh <- src.cwinh;
6812 dst.savebmarks <- src.savebmarks;
6813 dst.memlimit <- src.memlimit;
6814 dst.fitmodel <- src.fitmodel;
6815 dst.texcount <- src.texcount;
6816 dst.sliceheight <- src.sliceheight;
6817 dst.thumbw <- src.thumbw;
6818 dst.jumpback <- src.jumpback;
6819 dst.bgcolor <- src.bgcolor;
6820 dst.tilew <- src.tilew;
6821 dst.tileh <- src.tileh;
6822 dst.mustoresize <- src.mustoresize;
6823 dst.checkers <- src.checkers;
6824 dst.aalevel <- src.aalevel;
6825 dst.trimmargins <- src.trimmargins;
6826 dst.trimfuzz <- src.trimfuzz;
6827 dst.urilauncher <- src.urilauncher;
6828 dst.colorspace <- src.colorspace;
6829 dst.invert <- src.invert;
6830 dst.colorscale <- src.colorscale;
6831 dst.redirectstderr <- src.redirectstderr;
6832 dst.ghyllscroll <- src.ghyllscroll;
6833 dst.columns <- src.columns;
6834 dst.beyecolumns <- src.beyecolumns;
6835 dst.selcmd <- src.selcmd;
6836 dst.updatecurs <- src.updatecurs;
6837 dst.pathlauncher <- src.pathlauncher;
6838 dst.keyhashes <- copykeyhashes src;
6839 dst.hfsize <- src.hfsize;
6840 dst.hscrollstep <- src.hscrollstep;
6841 dst.pgscale <- src.pgscale;
6842 dst.usepbo <- src.usepbo;
6843 dst.wheelbypage <- src.wheelbypage;
6844 dst.stcmd <- src.stcmd;
6845 dst.paxcmd <- src.paxcmd;
6846 dst.scrollb <- src.scrollb;
6847 dst.riani <- src.riani;
6848 dst.paxmark <- src.paxmark;
6849 dst.pax <-
6850 if src.pax = None
6851 then None
6852 else Some ((ref (0.0, 0, 0)));
6855 let get s =
6856 let h = Hashtbl.create 10 in
6857 let dc = { defconf with angle = defconf.angle } in
6858 let rec toplevel v t spos _ =
6859 match t with
6860 | Vdata | Vcdata | Vend -> v
6861 | Vopen ("llppconfig", _, closed) ->
6862 if closed
6863 then v
6864 else { v with f = llppconfig }
6865 | Vopen _ ->
6866 error "unexpected subelement at top level" s spos
6867 | Vclose _ -> error "unexpected close at top level" s spos
6869 and llppconfig v t spos _ =
6870 match t with
6871 | Vdata | Vcdata -> v
6872 | Vend -> error "unexpected end of input in llppconfig" s spos
6873 | Vopen ("defaults", attrs, closed) ->
6874 let c = config_of dc attrs in
6875 setconf dc c;
6876 if closed
6877 then v
6878 else { v with f = defaults }
6880 | Vopen ("ui-font", attrs, closed) ->
6881 let rec getsize size = function
6882 | [] -> size
6883 | ("size", v) :: rest ->
6884 let size =
6885 fromstring int_of_string spos "size" v fstate.fontsize in
6886 getsize size rest
6887 | l -> getsize size l
6889 fstate.fontsize <- getsize fstate.fontsize attrs;
6890 if closed
6891 then v
6892 else { v with f = uifont (Buffer.create 10) }
6894 | Vopen ("doc", attrs, closed) ->
6895 let pathent, spage, srely, span, svisy = doc_of attrs in
6896 let path = unent pathent
6897 and pageno = fromstring int_of_string spos "page" spage 0
6898 and rely = fromstring float_of_string spos "rely" srely 0.0
6899 and pan = fromstring int_of_string spos "pan" span 0
6900 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6901 let c = config_of dc attrs in
6902 let anchor = (pageno, rely, visy) in
6903 if closed
6904 then (Hashtbl.add h path (c, [], pan, anchor); v)
6905 else { v with f = doc path pan anchor c [] }
6907 | Vopen _ ->
6908 error "unexpected subelement in llppconfig" s spos
6910 | Vclose "llppconfig" -> { v with f = toplevel }
6911 | Vclose _ -> error "unexpected close in llppconfig" s spos
6913 and defaults v t spos _ =
6914 match t with
6915 | Vdata | Vcdata -> v
6916 | Vend -> error "unexpected end of input in defaults" s spos
6917 | Vopen ("keymap", attrs, closed) ->
6918 let modename =
6919 try List.assoc "mode" attrs
6920 with Not_found -> "global" in
6921 if closed
6922 then v
6923 else
6924 let ret keymap =
6925 let h = findkeyhash dc modename in
6926 KeyMap.iter (Hashtbl.replace h) keymap;
6927 defaults
6929 { v with f = pkeymap ret KeyMap.empty }
6931 | Vopen (_, _, _) ->
6932 error "unexpected subelement in defaults" s spos
6934 | Vclose "defaults" ->
6935 { v with f = llppconfig }
6937 | Vclose _ -> error "unexpected close in defaults" s spos
6939 and uifont b v t spos epos =
6940 match t with
6941 | Vdata | Vcdata ->
6942 Buffer.add_substring b s spos (epos - spos);
6944 | Vopen (_, _, _) ->
6945 error "unexpected subelement in ui-font" s spos
6946 | Vclose "ui-font" ->
6947 if emptystr !fontpath
6948 then fontpath := Buffer.contents b;
6949 { v with f = llppconfig }
6950 | Vclose _ -> error "unexpected close in ui-font" s spos
6951 | Vend -> error "unexpected end of input in ui-font" s spos
6953 and doc path pan anchor c bookmarks v t spos _ =
6954 match t with
6955 | Vdata | Vcdata -> v
6956 | Vend -> error "unexpected end of input in doc" s spos
6957 | Vopen ("bookmarks", _, closed) ->
6958 if closed
6959 then v
6960 else { v with f = pbookmarks path pan anchor c bookmarks }
6962 | Vopen ("keymap", attrs, closed) ->
6963 let modename =
6964 try List.assoc "mode" attrs
6965 with Not_found -> "global"
6967 if closed
6968 then v
6969 else
6970 let ret keymap =
6971 let h = findkeyhash c modename in
6972 KeyMap.iter (Hashtbl.replace h) keymap;
6973 doc path pan anchor c bookmarks
6975 { v with f = pkeymap ret KeyMap.empty }
6977 | Vopen (_, _, _) ->
6978 error "unexpected subelement in doc" s spos
6980 | Vclose "doc" ->
6981 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6982 { v with f = llppconfig }
6984 | Vclose _ -> error "unexpected close in doc" s spos
6986 and pkeymap ret keymap v t spos _ =
6987 match t with
6988 | Vdata | Vcdata -> v
6989 | Vend -> error "unexpected end of input in keymap" s spos
6990 | Vopen ("map", attrs, closed) ->
6991 let r, l = map_of attrs in
6992 let kss = fromstring keys_of_string spos "in" r [] in
6993 let lss = fromstring keys_of_string spos "out" l [] in
6994 let keymap =
6995 match kss with
6996 | [] -> keymap
6997 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6998 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7000 if closed
7001 then { v with f = pkeymap ret keymap }
7002 else
7003 let f () = v in
7004 { v with f = skip "map" f }
7006 | Vopen _ ->
7007 error "unexpected subelement in keymap" s spos
7009 | Vclose "keymap" ->
7010 { v with f = ret keymap }
7012 | Vclose _ -> error "unexpected close in keymap" s spos
7014 and pbookmarks path pan anchor c bookmarks v t spos _ =
7015 match t with
7016 | Vdata | Vcdata -> v
7017 | Vend -> error "unexpected end of input in bookmarks" s spos
7018 | Vopen ("item", attrs, closed) ->
7019 let titleent, spage, srely, svisy = bookmark_of attrs in
7020 let page = fromstring int_of_string spos "page" spage 0
7021 and rely = fromstring float_of_string spos "rely" srely 0.0
7022 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7023 let bookmarks =
7024 (unent titleent, 0, (page, rely, visy)) :: bookmarks
7026 if closed
7027 then { v with f = pbookmarks path pan anchor c bookmarks }
7028 else
7029 let f () = v in
7030 { v with f = skip "item" f }
7032 | Vopen _ ->
7033 error "unexpected subelement in bookmarks" s spos
7035 | Vclose "bookmarks" ->
7036 { v with f = doc path pan anchor c bookmarks }
7038 | Vclose _ -> error "unexpected close in bookmarks" s spos
7040 and skip tag f v t spos _ =
7041 match t with
7042 | Vdata | Vcdata -> v
7043 | Vend ->
7044 error ("unexpected end of input in skipped " ^ tag) s spos
7045 | Vopen (tag', _, closed) ->
7046 if closed
7047 then v
7048 else
7049 let f' () = { v with f = skip tag f } in
7050 { v with f = skip tag' f' }
7051 | Vclose ctag ->
7052 if tag = ctag
7053 then f ()
7054 else error ("unexpected close in skipped " ^ tag) s spos
7057 parse { f = toplevel; accu = () } s;
7058 h, dc;
7061 let do_load f ic =
7063 let len = in_channel_length ic in
7064 let s = String.create len in
7065 really_input ic s 0 len;
7066 f s;
7067 with
7068 | Parse_error (msg, s, pos) ->
7069 let subs = subs s pos in
7070 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7072 | exn ->
7073 failwith ("config load error: " ^ exntos exn)
7076 let defconfpath =
7077 let dir =
7079 let dir = Filename.concat home ".config" in
7080 if Sys.is_directory dir then dir else home
7081 with _ -> home
7083 Filename.concat dir "llpp.conf"
7086 let confpath = ref defconfpath;;
7088 let load1 f =
7089 if Sys.file_exists !confpath
7090 then
7091 match
7092 (try Some (open_in_bin !confpath)
7093 with exn ->
7094 prerr_endline
7095 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7096 exntos exn);
7097 None
7099 with
7100 | Some ic ->
7101 let success =
7103 f (do_load get ic)
7104 with exn ->
7105 prerr_endline
7106 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7107 exntos exn);
7108 false
7110 close_in ic;
7111 success
7113 | None -> false
7114 else
7115 f (Hashtbl.create 0, defconf)
7118 let load () =
7119 let f (h, dc) =
7120 let pc, pb, px, pa =
7122 let key =
7123 if emptystr state.origin
7124 then state.path
7125 else state.origin
7127 Hashtbl.find h (Filename.basename key)
7128 with Not_found -> dc, [], 0, emptyanchor
7130 setconf defconf dc;
7131 setconf conf pc;
7132 state.bookmarks <- pb;
7133 state.x <- px;
7134 if conf.jumpback
7135 then state.anchor <- pa;
7136 cbput state.hists.nav pa;
7137 true
7139 load1 f
7142 let add_attrs bb always dc c =
7143 let ob s a b =
7144 if always || a != b
7145 then Printf.bprintf bb "\n %s='%b'" s a
7146 and op s a b =
7147 if always || a <> b
7148 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7149 and oi s a b =
7150 if always || a != b
7151 then Printf.bprintf bb "\n %s='%d'" s a
7152 and oI s a b =
7153 if always || a != b
7154 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7155 and oz s a b =
7156 if always || a <> b
7157 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7158 and oF s a b =
7159 if always || a <> b
7160 then Printf.bprintf bb "\n %s='%f'" s a
7161 and oc s a b =
7162 if always || a <> b
7163 then
7164 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7165 and oC s a b =
7166 if always || a <> b
7167 then
7168 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7169 and oR s a b =
7170 if always || a <> b
7171 then
7172 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7173 and os s a b =
7174 if always || a <> b
7175 then
7176 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7177 and og s a b =
7178 if always || a <> b
7179 then
7180 match a with
7181 | None -> ()
7182 | Some (_N, _A, _B) ->
7183 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7184 and oW s a b =
7185 if always || a <> b
7186 then
7187 let v =
7188 match a with
7189 | None -> "false"
7190 | Some f ->
7191 if f = infinity
7192 then "true"
7193 else string_of_float f
7195 Printf.bprintf bb "\n %s='%s'" s v
7196 and oco s a b =
7197 if always || a <> b
7198 then
7199 match a with
7200 | Cmulti ((n, a, b), _) when n > 1 ->
7201 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7202 | Csplit (n, _) when n > 1 ->
7203 Printf.bprintf bb "\n %s='%d'" s ~-n
7204 | _ -> ()
7205 and obeco s a b =
7206 if always || a <> b
7207 then
7208 match a with
7209 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7210 | _ -> ()
7211 and oFm s a b =
7212 if always || a <> b
7213 then
7214 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7215 and oSv s a b m =
7216 if always || a <> b
7217 then
7218 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7219 and oPm s a b =
7220 if always || a <> b
7221 then
7222 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7224 oi "width" c.cwinw dc.cwinw;
7225 oi "height" c.cwinh dc.cwinh;
7226 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7227 oi "scroll-handle-height" c.scrollh dc.scrollh;
7228 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7229 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7230 ob "case-insensitive-search" c.icase dc.icase;
7231 ob "preload" c.preload dc.preload;
7232 oi "page-bias" c.pagebias dc.pagebias;
7233 oi "scroll-step" c.scrollstep dc.scrollstep;
7234 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7235 ob "max-height-fit" c.maxhfit dc.maxhfit;
7236 ob "crop-hack" c.crophack dc.crophack;
7237 oW "throttle" c.maxwait dc.maxwait;
7238 ob "highlight-links" c.hlinks dc.hlinks;
7239 ob "under-cursor-info" c.underinfo dc.underinfo;
7240 oi "vertical-margin" c.interpagespace dc.interpagespace;
7241 oz "zoom" c.zoom dc.zoom;
7242 ob "presentation" c.presentation dc.presentation;
7243 oi "rotation-angle" c.angle dc.angle;
7244 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7245 oFm "fit-model" c.fitmodel dc.fitmodel;
7246 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7247 oi "tex-count" c.texcount dc.texcount;
7248 oi "slice-height" c.sliceheight dc.sliceheight;
7249 oi "thumbnail-width" c.thumbw dc.thumbw;
7250 ob "persistent-location" c.jumpback dc.jumpback;
7251 oc "background-color" c.bgcolor dc.bgcolor;
7252 oi "tile-width" c.tilew dc.tilew;
7253 oi "tile-height" c.tileh dc.tileh;
7254 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7255 ob "checkers" c.checkers dc.checkers;
7256 oi "aalevel" c.aalevel dc.aalevel;
7257 ob "trim-margins" c.trimmargins dc.trimmargins;
7258 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7259 os "uri-launcher" c.urilauncher dc.urilauncher;
7260 os "path-launcher" c.pathlauncher dc.pathlauncher;
7261 oC "color-space" c.colorspace dc.colorspace;
7262 ob "invert-colors" c.invert dc.invert;
7263 oF "brightness" c.colorscale dc.colorscale;
7264 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7265 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7266 oco "columns" c.columns dc.columns;
7267 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7268 os "selection-command" c.selcmd dc.selcmd;
7269 os "synctex-command" c.stcmd dc.stcmd;
7270 os "pax-command" c.paxcmd dc.paxcmd;
7271 ob "update-cursor" c.updatecurs dc.updatecurs;
7272 oi "hint-font-size" c.hfsize dc.hfsize;
7273 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7274 oF "page-scroll-scale" c.pgscale dc.pgscale;
7275 ob "use-pbo" c.usepbo dc.usepbo;
7276 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7277 ob "remote-in-a-new-instance" c.riani dc.riani;
7278 op "point-and-x" c.pax dc.pax;
7279 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7282 let keymapsbuf always dc c =
7283 let bb = Buffer.create 16 in
7284 let rec loop = function
7285 | [] -> ()
7286 | (modename, h) :: rest ->
7287 let dh = findkeyhash dc modename in
7288 if always || h <> dh
7289 then (
7290 if Hashtbl.length h > 0
7291 then (
7292 if Buffer.length bb > 0
7293 then Buffer.add_char bb '\n';
7294 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7295 Hashtbl.iter (fun i o ->
7296 let isdifferent = always ||
7298 let dO = Hashtbl.find dh i in
7299 dO <> o
7300 with Not_found -> true
7302 if isdifferent
7303 then
7304 let addkm (k, m) =
7305 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7306 if Wsi.withalt m then Buffer.add_string bb "alt-";
7307 if Wsi.withshift m then Buffer.add_string bb "shift-";
7308 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7309 Buffer.add_string bb (Wsi.keyname k);
7311 let addkms l =
7312 let rec loop = function
7313 | [] -> ()
7314 | km :: [] -> addkm km
7315 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7317 loop l
7319 Buffer.add_string bb "<map in='";
7320 addkm i;
7321 match o with
7322 | KMinsrt km ->
7323 Buffer.add_string bb "' out='";
7324 addkm km;
7325 Buffer.add_string bb "'/>\n"
7327 | KMinsrl kms ->
7328 Buffer.add_string bb "' out='";
7329 addkms kms;
7330 Buffer.add_string bb "'/>\n"
7332 | KMmulti (ins, kms) ->
7333 Buffer.add_char bb ' ';
7334 addkms ins;
7335 Buffer.add_string bb "' out='";
7336 addkms kms;
7337 Buffer.add_string bb "'/>\n"
7338 ) h;
7339 Buffer.add_string bb "</keymap>";
7342 loop rest
7344 loop c.keyhashes;
7348 let save () =
7349 let uifontsize = fstate.fontsize in
7350 let bb = Buffer.create 32768 in
7351 let relx = float state.x /. float state.winw in
7352 let w, h, x =
7353 let cx w = truncate (relx *. float w) in
7354 List.fold_left
7355 (fun (w, h, x) ws ->
7356 match ws with
7357 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7358 | Wsi.MaxVert -> (w, conf.cwinh, x)
7359 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7361 (state.winw, state.winh, state.x) state.winstate
7363 conf.cwinw <- w;
7364 conf.cwinh <- h;
7365 let f (h, dc) =
7366 let dc = if conf.bedefault then conf else dc in
7367 Buffer.add_string bb "<llppconfig>\n";
7369 if nonemptystr !fontpath
7370 then
7371 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7372 uifontsize
7373 !fontpath
7374 else (
7375 if uifontsize <> 14
7376 then
7377 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7380 Buffer.add_string bb "<defaults ";
7381 add_attrs bb true dc dc;
7382 let kb = keymapsbuf true dc dc in
7383 if Buffer.length kb > 0
7384 then (
7385 Buffer.add_string bb ">\n";
7386 Buffer.add_buffer bb kb;
7387 Buffer.add_string bb "\n</defaults>\n";
7389 else Buffer.add_string bb "/>\n";
7391 let adddoc path pan anchor c bookmarks =
7392 if bookmarks == [] && c = dc && anchor = emptyanchor
7393 then ()
7394 else (
7395 Printf.bprintf bb "<doc path='%s'"
7396 (enent path 0 (String.length path));
7398 if anchor <> emptyanchor
7399 then (
7400 let n, rely, visy = anchor in
7401 Printf.bprintf bb " page='%d'" n;
7402 if rely > 1e-6
7403 then
7404 Printf.bprintf bb " rely='%f'" rely
7406 if abs_float visy > 1e-6
7407 then
7408 Printf.bprintf bb " visy='%f'" visy
7412 if pan != 0
7413 then Printf.bprintf bb " pan='%d'" pan;
7415 add_attrs bb false dc c;
7416 let kb = keymapsbuf false dc c in
7418 begin match bookmarks with
7419 | [] ->
7420 if Buffer.length kb > 0
7421 then (
7422 Buffer.add_string bb ">\n";
7423 Buffer.add_buffer bb kb;
7424 Buffer.add_string bb "\n</doc>\n";
7426 else Buffer.add_string bb "/>\n"
7427 | _ ->
7428 Buffer.add_string bb ">\n<bookmarks>\n";
7429 List.iter (fun (title, _level, (page, rely, visy)) ->
7430 Printf.bprintf bb
7431 "<item title='%s' page='%d'"
7432 (enent title 0 (String.length title))
7433 page
7435 if rely > 1e-6
7436 then
7437 Printf.bprintf bb " rely='%f'" rely
7439 if abs_float visy > 1e-6
7440 then
7441 Printf.bprintf bb " visy='%f'" visy
7443 Buffer.add_string bb "/>\n";
7444 ) bookmarks;
7445 Buffer.add_string bb "</bookmarks>";
7446 if Buffer.length kb > 0
7447 then (
7448 Buffer.add_string bb "\n";
7449 Buffer.add_buffer bb kb;
7451 Buffer.add_string bb "\n</doc>\n";
7452 end;
7456 let pan, conf =
7457 match state.mode with
7458 | Birdseye (c, pan, _, _, _) ->
7459 let beyecolumns =
7460 match conf.columns with
7461 | Cmulti ((c, _, _), _) -> Some c
7462 | Csingle _ -> None
7463 | Csplit _ -> None
7464 and columns =
7465 match c.columns with
7466 | Cmulti (c, _) -> Cmulti (c, [||])
7467 | Csingle _ -> Csingle [||]
7468 | Csplit _ -> failwith "quit from bird's eye while split"
7470 pan, { c with beyecolumns = beyecolumns; columns = columns }
7471 | _ -> x, conf
7473 let basename = Filename.basename
7474 (if emptystr state.origin then state.path else state.origin)
7476 adddoc basename pan (getanchor ())
7477 (let conf =
7478 let autoscrollstep =
7479 match state.autoscroll with
7480 | Some step -> step
7481 | None -> conf.autoscrollstep
7483 match state.mode with
7484 | Birdseye (bc, _, _, _, _) ->
7485 { conf with
7486 zoom = bc.zoom;
7487 presentation = bc.presentation;
7488 interpagespace = bc.interpagespace;
7489 maxwait = bc.maxwait;
7490 autoscrollstep = autoscrollstep }
7491 | _ -> { conf with autoscrollstep = autoscrollstep }
7492 in conf)
7493 (if conf.savebmarks then state.bookmarks else []);
7495 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7496 if basename <> path
7497 then adddoc path x anchor c bookmarks
7498 ) h;
7499 Buffer.add_string bb "</llppconfig>\n";
7500 true;
7502 if load1 f && Buffer.length bb > 0
7503 then
7505 let tmp = !confpath ^ ".tmp" in
7506 let oc = open_out_bin tmp in
7507 Buffer.output_buffer oc bb;
7508 close_out oc;
7509 Unix.rename tmp !confpath;
7510 with exn ->
7511 prerr_endline
7512 ("error while saving configuration: " ^ exntos exn)
7514 end;;
7516 let adderrmsg src msg =
7517 Buffer.add_string state.errmsgs msg;
7518 state.newerrmsgs <- true;
7519 G.postRedisplay src
7522 let adderrfmt src fmt =
7523 Format.kprintf (fun s -> adderrmsg src s) fmt;
7526 let ract cmds =
7527 let cl = splitatspace cmds in
7528 let scan s fmt f =
7529 try Scanf.sscanf s fmt f
7530 with exn ->
7531 adderrfmt "remote exec"
7532 "error processing '%S': %s\n" cmds (exntos exn)
7534 match cl with
7535 | "reload" :: [] -> reload ()
7536 | "goto" :: args :: [] ->
7537 scan args "%u %f %f"
7538 (fun pageno x y ->
7539 let cmd, _ = state.geomcmds in
7540 if emptystr cmd
7541 then gotopagexy pageno x y
7542 else
7543 let f prevf () =
7544 gotopagexy pageno x y;
7545 prevf ()
7547 state.reprf <- f state.reprf
7549 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7550 | "rect" :: args :: [] ->
7551 scan args "%u %u %f %f %f %f"
7552 (fun pageno color x0 y0 x1 y1 ->
7553 onpagerect pageno (fun w h ->
7554 let _,w1,h1,_ = getpagedim pageno in
7555 let sw = float w1 /. float w
7556 and sh = float h1 /. float h in
7557 let x0s = x0 *. sw
7558 and x1s = x1 *. sw
7559 and y0s = y0 *. sh
7560 and y1s = y1 *. sh in
7561 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7562 debugrect rect;
7563 state.rects <- (pageno, color, rect) :: state.rects;
7564 G.postRedisplay "rect";
7567 | "activatewin" :: [] -> Wsi.activatewin ()
7568 | "quit" :: [] -> raise Quit
7569 | _ ->
7570 adderrfmt "remote command"
7571 "error processing remote command: %S\n" cmds;
7574 let remote =
7575 let scratch = String.create 80 in
7576 let buf = Buffer.create 80 in
7577 fun fd ->
7578 let rec tempfr () =
7579 try Some (Unix.read fd scratch 0 80)
7580 with
7581 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7582 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7583 | exn -> raise exn
7585 match tempfr () with
7586 | None -> Some fd
7587 | Some n ->
7588 if n = 0
7589 then (
7590 Unix.close fd;
7591 if Buffer.length buf > 0
7592 then (
7593 let s = Buffer.contents buf in
7594 Buffer.clear buf;
7595 ract s;
7597 None
7599 else
7600 let rec eat ppos =
7601 let nlpos =
7603 let pos = String.index_from scratch ppos '\n' in
7604 if pos >= n then -1 else pos
7605 with Not_found -> -1
7607 if nlpos >= 0
7608 then (
7609 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7610 let s = Buffer.contents buf in
7611 Buffer.clear buf;
7612 ract s;
7613 eat (nlpos+1);
7615 else (
7616 Buffer.add_substring buf scratch ppos (n-ppos);
7617 Some fd
7619 in eat 0
7622 let remoteopen path =
7623 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7624 with exn ->
7625 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7626 None
7629 let () =
7630 let trimcachepath = ref "" in
7631 let rcmdpath = ref "" in
7632 selfexec := Sys.executable_name;
7633 Arg.parse
7634 (Arg.align
7635 [("-p", Arg.String (fun s -> state.password <- s),
7636 "<password> Set password");
7638 ("-f", Arg.String
7639 (fun s ->
7640 Config.fontpath := s;
7641 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7643 "<path> Set path to the user interface font");
7645 ("-c", Arg.String
7646 (fun s ->
7647 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7648 Config.confpath := s),
7649 "<path> Set path to the configuration file");
7651 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7652 "<path> Set path to the trim cache file");
7654 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7655 "<named-destination> Set named destination");
7657 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7658 ("-cxack", Arg.Set cxack, " Cut corners");
7660 ("-remote", Arg.String (fun s -> rcmdpath := s),
7661 "<path> Set path to the remote commands source");
7663 ("-origin", Arg.String (fun s -> state.origin <- s),
7664 "<original-path> Set original path");
7666 ("-v", Arg.Unit (fun () ->
7667 Printf.printf
7668 "%s\nconfiguration path: %s\n"
7669 (version ())
7670 Config.defconfpath
7672 exit 0), " Print version and exit");
7675 (fun s -> state.path <- s)
7676 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7678 if !wtmode
7679 then selfexec := !selfexec ^ " -wtmode";
7681 if emptystr state.path
7682 then (prerr_endline "file name missing"; exit 1);
7684 if not (Config.load ())
7685 then prerr_endline "failed to load configuration";
7687 let wsfd, winw, winh = Wsi.init (object
7688 val mutable m_hack = false
7689 method expose = if not m_hack then G.postRedisplay "expose"
7690 method visible = G.postRedisplay "visible"
7691 method display = m_hack <- false; display ()
7692 method reshape w h =
7693 m_hack <- w < state.winw && h < state.winh;
7694 reshape w h
7695 method mouse b d x y m = mouse b d x y m
7696 method motion x y = state.mpos <- (x, y); motion x y
7697 method pmotion x y = state.mpos <- (x, y); pmotion x y
7698 method key k m =
7699 let mascm = m land (
7700 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7701 ) in
7702 match state.keystate with
7703 | KSnone ->
7704 let km = k, mascm in
7705 begin
7706 match
7707 let modehash = state.uioh#modehash in
7708 try Hashtbl.find modehash km
7709 with Not_found ->
7710 try Hashtbl.find (findkeyhash conf "global") km
7711 with Not_found -> KMinsrt (k, m)
7712 with
7713 | KMinsrt (k, m) -> keyboard k m
7714 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7715 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7717 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7718 List.iter (fun (k, m) -> keyboard k m) insrt;
7719 state.keystate <- KSnone
7720 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7721 state.keystate <- KSinto (keys, insrt)
7722 | _ ->
7723 state.keystate <- KSnone
7725 method enter x y = state.mpos <- (x, y); pmotion x y
7726 method leave = state.mpos <- (-1, -1)
7727 method winstate wsl = state.winstate <- wsl; m_hack <- false
7728 method quit = raise Quit
7729 end) conf.cwinw conf.cwinh (platform = Posx) in
7731 state.wsfd <- wsfd;
7733 if not (
7734 List.exists GlMisc.check_extension
7735 [ "GL_ARB_texture_rectangle"
7736 ; "GL_EXT_texture_recangle"
7737 ; "GL_NV_texture_rectangle" ]
7739 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7741 if (
7742 let r = GlMisc.get_string `renderer in
7743 let p = "Mesa DRI Intel(" in
7744 let l = String.length p in
7745 String.length r > l && String.sub r 0 l = p
7747 then (
7748 defconf.sliceheight <- 1024;
7749 defconf.texcount <- 32;
7750 defconf.usepbo <- true;
7753 let cr, sw =
7754 match Ne.pipe () with
7755 | Ne.Exn exn ->
7756 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7757 exit 1
7758 | Ne.Res rw -> rw
7759 and sr, cw =
7760 match Ne.pipe () with
7761 | Ne.Exn exn ->
7762 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7763 exit 1
7764 | Ne.Res rw -> rw
7767 cloexec cr;
7768 cloexec sw;
7769 cloexec sr;
7770 cloexec cw;
7772 setcheckers conf.checkers;
7773 redirectstderr ();
7774 if conf.redirectstderr
7775 then
7776 at_exit (fun () ->
7777 let s = Buffer.contents state.errmsgs ^
7778 (match state.errfd with
7779 | Some fd ->
7780 let s = String.create (80*24) in
7781 let n =
7783 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7784 if List.mem fd r
7785 then Unix.read fd s 0 (String.length s)
7786 else 0
7787 with _ -> 0
7789 if n = 0
7790 then ""
7791 else String.sub s 0 n
7792 | None -> ""
7795 try ignore (Unix.write state.stderr s 0 (String.length s))
7796 with exn -> print_endline (exntos exn)
7800 init (cr, cw) (
7801 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7802 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7803 !Config.fontpath, !trimcachepath,
7804 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7806 state.sr <- sr;
7807 state.sw <- sw;
7808 state.text <- "Opening " ^ (mbtoutf8 state.path);
7809 reshape winw winh;
7810 opendoc state.path state.password;
7811 state.uioh <- uioh;
7812 display ();
7813 Wsi.mapwin ();
7814 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7815 let optrfd =
7816 ref (
7817 if nonemptystr !rcmdpath
7818 then remoteopen !rcmdpath
7819 else None
7823 let rec loop deadline =
7824 let r =
7825 match state.errfd with
7826 | None -> [state.sr; state.wsfd]
7827 | Some fd -> [state.sr; state.wsfd; fd]
7829 let r =
7830 match !optrfd with
7831 | None -> r
7832 | Some fd -> fd :: r
7834 if state.redisplay
7835 then (
7836 state.redisplay <- false;
7837 display ();
7839 let timeout =
7840 let now = now () in
7841 if deadline > now
7842 then (
7843 if deadline = infinity
7844 then ~-.1.0
7845 else max 0.0 (deadline -. now)
7847 else 0.0
7849 let r, _, _ =
7850 try Unix.select r [] [] timeout
7851 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7853 begin match r with
7854 | [] ->
7855 state.ghyll None;
7856 let newdeadline =
7857 if state.ghyll == noghyll
7858 then
7859 match state.autoscroll with
7860 | Some step when step != 0 ->
7861 let y = state.y + step in
7862 let y =
7863 if y < 0
7864 then state.maxy
7865 else if y >= state.maxy then 0 else y
7867 gotoy y;
7868 if state.mode = View
7869 then state.text <- "";
7870 deadline +. 0.01
7871 | _ -> infinity
7872 else deadline +. 0.01
7874 loop newdeadline
7876 | l ->
7877 let rec checkfds = function
7878 | [] -> ()
7879 | fd :: rest when fd = state.sr ->
7880 let cmd = readcmd state.sr in
7881 act cmd;
7882 checkfds rest
7884 | fd :: rest when fd = state.wsfd ->
7885 Wsi.readresp fd;
7886 checkfds rest
7888 | fd :: rest when Some fd = !optrfd ->
7889 begin match remote fd with
7890 | None -> optrfd := remoteopen !rcmdpath;
7891 | opt -> optrfd := opt
7892 end;
7893 checkfds rest
7895 | fd :: rest ->
7896 let s = String.create 80 in
7897 let n = tempfailureretry (Unix.read fd s 0) 80 in
7898 if conf.redirectstderr
7899 then (
7900 Buffer.add_substring state.errmsgs s 0 n;
7901 state.newerrmsgs <- true;
7902 state.redisplay <- true;
7904 else (
7905 prerr_string (String.sub s 0 n);
7906 flush stderr;
7908 checkfds rest
7910 checkfds l;
7911 let newdeadline =
7912 let deadline1 =
7913 if deadline = infinity
7914 then now () +. 0.01
7915 else deadline
7917 match state.autoscroll with
7918 | Some step when step != 0 -> deadline1
7919 | _ -> if state.ghyll == noghyll then infinity else deadline1
7921 loop newdeadline
7922 end;
7925 loop infinity;
7926 with Quit ->
7927 Config.save ();