Tweaks for intel cards
[llpp/slowscroll.git] / main.ml
blob5f40f8b46901b4d222d6d87b81c064d692ea97b9
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 and facename = string;;
16 type mark =
17 | Mark_page
18 | Mark_block
19 | Mark_line
20 | Mark_word
23 type params = (angle * fitmodel * trimparams
24 * texcount * sliceheight * memsize
25 * colorspace * fontpath * trimcachepath
26 * haspbo)
27 and pageno = int
28 and width = int
29 and height = int
30 and leftx = int
31 and opaque = string
32 and recttype = int
33 and pixmapsize = int
34 and angle = int
35 and trimmargins = bool
36 and interpagespace = int
37 and texcount = int
38 and sliceheight = int
39 and gen = int
40 and top = float
41 and dtop = float
42 and fontpath = string
43 and trimcachepath = string
44 and memsize = int
45 and aalevel = int
46 and irect = (int * int * int * int)
47 and trimparams = (trimmargins * irect)
48 and colorspace = | Rgb | Bgr | Gray
49 and fitmodel = | FitWidth | FitProportional | FitPage
50 and haspbo = bool
53 type x = int
54 and y = int
55 and tilex = int
56 and tiley = int
57 and tileparams = (x * y * width * height * tilex * tiley)
60 type link =
61 | Lnotfound
62 | Lfound of int
63 and linkdir =
64 | LDfirst
65 | LDlast
66 | LDfirstvisible of (int * int * int)
67 | LDleft of int
68 | LDright of int
69 | LDdown of int
70 | LDup of int
73 type pagewithlinks =
74 | Pwlnotfound
75 | Pwl of int
78 type keymap =
79 | KMinsrt of key
80 | KMinsrl of key list
81 | KMmulti of key list * key list
82 and key = int * int
83 and keyhash = (key, keymap) Hashtbl.t
84 and keystate =
85 | KSnone
86 | KSinto of (key list * key list)
89 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
90 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
92 type pipe = (Unix.file_descr * Unix.file_descr);;
94 external init : pipe -> params -> unit = "ml_init";;
95 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
96 external copysel : Unix.file_descr -> opaque -> bool -> unit = "ml_copysel";;
97 external getpdimrect : int -> float array = "ml_getpdimrect";;
98 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
99 external markunder : string -> int -> int -> mark -> bool = "ml_markunder";;
100 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
101 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
102 external measurestr : int -> string -> float = "ml_measure_string";;
103 external postprocess :
104 opaque -> int -> int -> int -> (int * string * int) -> int
105 = "ml_postprocess";;
106 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
107 external platform : unit -> platform = "ml_platform";;
108 external setaalevel : int -> unit = "ml_setaalevel";;
109 external realloctexts : int -> bool = "ml_realloctexts";;
110 external findlink : opaque -> linkdir -> link = "ml_findlink";;
111 external getlink : opaque -> int -> under = "ml_getlink";;
112 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
113 external getlinkcount : opaque -> int = "ml_getlinkcount";;
114 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
115 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
116 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
117 external freepbo : string -> unit = "ml_freepbo";;
118 external unmappbo : string -> unit = "ml_unmappbo";;
119 external pbousable : unit -> bool = "ml_pbo_usable";;
120 external unproject : opaque -> int -> int -> (int * int) option
121 = "ml_unproject";;
122 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
123 external rectofblock : opaque -> int -> int -> float array option
124 = "ml_rectofblock";;
126 let platform_to_string = function
127 | Punknown -> "unknown"
128 | Plinux -> "Linux"
129 | Posx -> "OSX"
130 | Psun -> "Sun"
131 | Pfreebsd -> "FreeBSD"
132 | Pdragonflybsd -> "DragonflyBSD"
133 | Popenbsd -> "OpenBSD"
134 | Pnetbsd -> "NetBSD"
135 | Pcygwin -> "Cygwin"
138 let platform = platform ();;
140 let now = Unix.gettimeofday;;
142 let selfexec = ref "";;
144 let popen cmd fda =
145 if platform = Pcygwin
146 then (
147 let sh = "/bin/sh" in
148 let args = [|sh; "-c"; cmd|] in
149 let rec std si so se = function
150 | [] -> si, so, se
151 | (fd, 0) :: rest -> std fd so se rest
152 | (fd, -1) :: rest ->
153 Unix.set_close_on_exec fd;
154 std si so se rest
155 | (_, n) :: _ ->
156 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
158 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
159 ignore (Unix.create_process sh args si so se)
161 else popen cmd fda;
164 type mpos = int * int
165 and mstate =
166 | Msel of (mpos * mpos)
167 | Mpan of mpos
168 | Mscrolly | Mscrollx
169 | Mzoom of (int * int)
170 | Mzoomrect of (mpos * mpos)
171 | Mnone
174 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
175 and onkey = string -> int -> te
176 and ondone = string -> unit
177 and histcancel = unit -> unit
178 and onhist = ((histcmd -> string) * histcancel)
179 and histcmd = HCnext | HCprev | HCfirst | HClast
180 and cancelonempty = bool
181 and te =
182 | TEstop
183 | TEdone of string
184 | TEcont of string
185 | TEswitch of textentry
188 type 'a circbuf =
189 { store : 'a array
190 ; mutable rc : int
191 ; mutable wc : int
192 ; mutable len : int
196 let bound v minv maxv =
197 max minv (min maxv v);
200 let cbnew n v =
201 { store = Array.create n v
202 ; rc = 0
203 ; wc = 0
204 ; len = 0
208 let cbcap b = Array.length b.store;;
210 let cbput b v =
211 let cap = cbcap b in
212 b.store.(b.wc) <- v;
213 b.wc <- (b.wc + 1) mod cap;
214 b.rc <- b.wc;
215 b.len <- min (b.len + 1) cap;
218 let cbempty b = b.len = 0;;
220 let cbgetg b circular dir =
221 if cbempty b
222 then b.store.(0)
223 else
224 let rc = b.rc + dir in
225 let rc =
226 if circular
227 then (
228 if rc = -1
229 then b.len-1
230 else (
231 if rc >= b.len
232 then 0
233 else rc
236 else bound rc 0 (b.len-1)
238 b.rc <- rc;
239 b.store.(rc);
242 let cbget b = cbgetg b false;;
243 let cbgetc b = cbgetg b true;;
245 let drawstring size x y s =
246 Gl.enable `blend;
247 Gl.enable `texture_2d;
248 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
249 ignore (drawstr size x y s);
250 Gl.disable `blend;
251 Gl.disable `texture_2d;
254 let drawstring1 size x y s =
255 drawstr size x y s;
258 let drawstring2 size x y fmt =
259 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
262 type page =
263 { pageno : int
264 ; pagedimno : int
265 ; pagew : int
266 ; pageh : int
267 ; pagex : int
268 ; pagey : int
269 ; pagevw : int
270 ; pagevh : int
271 ; pagedispx : int
272 ; pagedispy : int
273 ; pagecol : int
277 let debugl l =
278 dolog "l %d dim=%d {" l.pageno l.pagedimno;
279 dolog " WxH %dx%d" l.pagew l.pageh;
280 dolog " vWxH %dx%d" l.pagevw l.pagevh;
281 dolog " pagex,y %d,%d" l.pagex l.pagey;
282 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
283 dolog " column %d" l.pagecol;
284 dolog "}";
287 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
288 dolog "rect {";
289 dolog " x0,y0=(% f, % f)" x0 y0;
290 dolog " x1,y1=(% f, % f)" x1 y1;
291 dolog " x2,y2=(% f, % f)" x2 y2;
292 dolog " x3,y3=(% f, % f)" x3 y3;
293 dolog "}";
296 type multicolumns = multicol * pagegeom
297 and singlecolumn = pagegeom
298 and splitcolumns = columncount * pagegeom
299 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
300 and multicol = columncount * covercount * covercount
301 and pdimno = int
302 and columncount = int
303 and covercount = int;;
305 type scrollb = int;;
306 let scrollbvv = 1;;
307 let scrollbhv = 2;;
309 type conf =
310 { mutable scrollbw : int
311 ; mutable scrollh : int
312 ; mutable scrollb : scrollb
313 ; mutable icase : bool
314 ; mutable preload : bool
315 ; mutable pagebias : int
316 ; mutable verbose : bool
317 ; mutable debug : bool
318 ; mutable scrollstep : int
319 ; mutable hscrollstep : int
320 ; mutable maxhfit : bool
321 ; mutable crophack : bool
322 ; mutable autoscrollstep : int
323 ; mutable maxwait : float option
324 ; mutable hlinks : bool
325 ; mutable underinfo : bool
326 ; mutable interpagespace : interpagespace
327 ; mutable zoom : float
328 ; mutable presentation : bool
329 ; mutable angle : angle
330 ; mutable cwinw : int
331 ; mutable cwinh : int
332 ; mutable savebmarks : bool
333 ; mutable fitmodel : fitmodel
334 ; mutable trimmargins : trimmargins
335 ; mutable trimfuzz : irect
336 ; mutable memlimit : memsize
337 ; mutable texcount : texcount
338 ; mutable sliceheight : sliceheight
339 ; mutable thumbw : width
340 ; mutable jumpback : bool
341 ; mutable bgcolor : (float * float * float)
342 ; mutable bedefault : bool
343 ; mutable tilew : int
344 ; mutable tileh : int
345 ; mutable mustoresize : memsize
346 ; mutable checkers : bool
347 ; mutable aalevel : int
348 ; mutable urilauncher : string
349 ; mutable pathlauncher : string
350 ; mutable colorspace : colorspace
351 ; mutable invert : bool
352 ; mutable colorscale : float
353 ; mutable redirectstderr : bool
354 ; mutable ghyllscroll : (int * int * int) option
355 ; mutable columns : columns
356 ; mutable beyecolumns : columncount option
357 ; mutable selcmd : string
358 ; mutable paxcmd : string
359 ; mutable updatecurs : bool
360 ; mutable keyhashes : (string * keyhash) list
361 ; mutable hfsize : int
362 ; mutable pgscale : float
363 ; mutable usepbo : bool
364 ; mutable wheelbypage : bool
365 ; mutable stcmd : string
366 ; mutable riani : bool
367 ; mutable pax : (float * int * int) ref option
368 ; mutable paxmark : mark
370 and columns =
371 | Csingle of singlecolumn
372 | Cmulti of multicolumns
373 | Csplit of splitcolumns
376 type anchor = pageno * top * dtop;;
378 type outline = string * int * anchor;;
380 type rect = float * float * float * float * float * float * float * float;;
382 type tile = opaque * pixmapsize * elapsed
383 and elapsed = float;;
384 type pagemapkey = pageno * gen;;
385 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
386 and row = int
387 and col = int;;
389 let emptyanchor = (0, 0.0, 0.0);;
391 type infochange = | Memused | Docinfo | Pdim;;
393 class type uioh = object
394 method display : unit
395 method key : int -> int -> uioh
396 method button : int -> bool -> int -> int -> int -> uioh
397 method motion : int -> int -> uioh
398 method pmotion : int -> int -> uioh
399 method infochanged : infochange -> unit
400 method scrollpw : (int * float * float)
401 method scrollph : (int * float * float)
402 method modehash : keyhash
403 method eformsgs : bool
404 end;;
406 type mode =
407 | Birdseye of (conf * leftx * pageno * pageno * anchor)
408 | Textentry of (textentry * onleave)
409 | View
410 | LinkNav of linktarget
411 and onleave = leavetextentrystatus -> unit
412 and leavetextentrystatus = | Cancel | Confirm
413 and helpitem = string * int * action
414 and action =
415 | Noaction
416 | Action of (uioh -> uioh)
417 and linktarget =
418 | Ltexact of (pageno * int)
419 | Ltgendir of int
422 let isbirdseye = function Birdseye _ -> true | _ -> false;;
423 let istextentry = function Textentry _ -> true | _ -> false;;
425 type currently =
426 | Idle
427 | Loading of (page * gen)
428 | Tiling of (
429 page * opaque * colorspace * angle * gen * col * row * width * height
431 | Outlining of outline list
434 let emptykeyhash = Hashtbl.create 0;;
435 let nouioh : uioh = object (self)
436 method display = ()
437 method key _ _ = self
438 method button _ _ _ _ _ = self
439 method motion _ _ = self
440 method pmotion _ _ = self
441 method infochanged _ = ()
442 method scrollpw = (0, nan, nan)
443 method scrollph = (0, nan, nan)
444 method modehash = emptykeyhash
445 method eformsgs = false
446 end;;
448 type state =
449 { mutable sr : Unix.file_descr
450 ; mutable sw : Unix.file_descr
451 ; mutable wsfd : Unix.file_descr
452 ; mutable errfd : Unix.file_descr option
453 ; mutable stderr : Unix.file_descr
454 ; mutable errmsgs : Buffer.t
455 ; mutable newerrmsgs : bool
456 ; mutable w : int
457 ; mutable x : int
458 ; mutable y : int
459 ; mutable anchor : anchor
460 ; mutable ranchors : (string * string * anchor * string) list
461 ; mutable maxy : int
462 ; mutable layout : page list
463 ; pagemap : (pagemapkey, opaque) Hashtbl.t
464 ; tilemap : (tilemapkey, tile) Hashtbl.t
465 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
466 ; mutable pdims : (pageno * width * height * leftx) list
467 ; mutable pagecount : int
468 ; mutable currently : currently
469 ; mutable mstate : mstate
470 ; mutable searchpattern : string
471 ; mutable rects : (pageno * recttype * rect) list
472 ; mutable rects1 : (pageno * recttype * rect) list
473 ; mutable text : string
474 ; mutable winstate : Wsi.winstate list
475 ; mutable mode : mode
476 ; mutable uioh : uioh
477 ; mutable outlines : outline array
478 ; mutable bookmarks : outline list
479 ; mutable path : string
480 ; mutable password : string
481 ; mutable nameddest : string
482 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
483 ; mutable memused : memsize
484 ; mutable gen : gen
485 ; mutable throttle : (page list * int * float) option
486 ; mutable autoscroll : int option
487 ; mutable ghyll : (int option -> unit)
488 ; mutable help : helpitem array
489 ; mutable docinfo : (int * string) list
490 ; mutable texid : GlTex.texture_id option
491 ; hists : hists
492 ; mutable prevzoom : (float * int)
493 ; mutable progress : float
494 ; mutable redisplay : bool
495 ; mutable mpos : mpos
496 ; mutable keystate : keystate
497 ; mutable glinks : bool
498 ; mutable prevcolumns : (columns * float) option
499 ; mutable winw : int
500 ; mutable winh : int
501 ; mutable reprf : (unit -> unit)
502 ; mutable origin : string
503 ; mutable roam : (unit -> unit)
504 ; mutable bzoom : bool
506 and hists =
507 { pat : string circbuf
508 ; pag : string circbuf
509 ; nav : anchor circbuf
510 ; sel : string circbuf
514 let defconf =
515 { scrollbw = 7
516 ; scrollh = 12
517 ; scrollb = scrollbhv lor scrollbvv
518 ; icase = true
519 ; preload = true
520 ; pagebias = 0
521 ; verbose = false
522 ; debug = false
523 ; scrollstep = 24
524 ; hscrollstep = 24
525 ; maxhfit = true
526 ; crophack = false
527 ; autoscrollstep = 2
528 ; maxwait = None
529 ; hlinks = false
530 ; underinfo = false
531 ; interpagespace = 2
532 ; zoom = 1.0
533 ; presentation = false
534 ; angle = 0
535 ; cwinw = 900
536 ; cwinh = 900
537 ; savebmarks = true
538 ; fitmodel = FitProportional
539 ; trimmargins = false
540 ; trimfuzz = (0,0,0,0)
541 ; memlimit = 32 lsl 20
542 ; texcount = 256
543 ; sliceheight = 24
544 ; thumbw = 76
545 ; jumpback = true
546 ; bgcolor = (0.5, 0.5, 0.5)
547 ; bedefault = false
548 ; tilew = 2048
549 ; tileh = 2048
550 ; mustoresize = 256 lsl 20
551 ; checkers = true
552 ; aalevel = 8
553 ; urilauncher =
554 (match platform with
555 | Plinux | Pfreebsd | Pdragonflybsd
556 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
557 | Posx -> "open \"%s\""
558 | Pcygwin -> "cygstart \"%s\""
559 | Punknown -> "echo %s")
560 ; pathlauncher = "lp \"%s\""
561 ; selcmd =
562 (match platform with
563 | Plinux | Pfreebsd | Pdragonflybsd
564 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
565 | Posx -> "pbcopy"
566 | Pcygwin -> "wsel"
567 | Punknown -> "cat")
568 ; paxcmd = "cat"
569 ; colorspace = Rgb
570 ; invert = false
571 ; colorscale = 1.0
572 ; redirectstderr = false
573 ; ghyllscroll = None
574 ; columns = Csingle [||]
575 ; beyecolumns = None
576 ; updatecurs = false
577 ; hfsize = 12
578 ; pgscale = 1.0
579 ; usepbo = false
580 ; wheelbypage = false
581 ; stcmd = "echo SyncTex"
582 ; riani = false
583 ; pax = None
584 ; paxmark = Mark_word
585 ; keyhashes =
586 let mk n = (n, Hashtbl.create 1) in
587 [ mk "global"
588 ; mk "info"
589 ; mk "help"
590 ; mk "outline"
591 ; mk "listview"
592 ; mk "birdseye"
593 ; mk "textentry"
594 ; mk "links"
595 ; mk "view"
600 let wtmode = ref false;;
601 let cxack = ref false;;
603 let findkeyhash c name =
604 try List.assoc name c.keyhashes
605 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
608 let conf = { defconf with angle = defconf.angle };;
610 let pgscale h = truncate (float h *. conf.pgscale);;
612 type fontstate =
613 { mutable fontsize : int
614 ; mutable wwidth : float
615 ; mutable maxrows : int
619 let fstate =
620 { fontsize = 14
621 ; wwidth = nan
622 ; maxrows = -1
626 let geturl s =
627 let colonpos = try String.index s ':' with Not_found -> -1 in
628 let len = String.length s in
629 if colonpos >= 0 && colonpos + 3 < len
630 then (
631 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
632 then
633 let schemestartpos =
634 try String.rindex_from s colonpos ' '
635 with Not_found -> -1
637 let scheme =
638 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
640 match scheme with
641 | "http" | "ftp" | "mailto" ->
642 let epos =
643 try String.index_from s colonpos ' '
644 with Not_found -> len
646 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
647 | _ -> ""
648 else ""
650 else ""
653 let gotouri uri =
654 if String.length conf.urilauncher = 0
655 then print_endline uri
656 else (
657 let url = geturl uri in
658 if String.length url = 0
659 then Printf.eprintf "obtained empty url from uri %S" uri
660 else
661 let re = Str.regexp "%s" in
662 let command = Str.global_replace re url conf.urilauncher in
663 try popen command []
664 with exn ->
665 Printf.eprintf
666 "failed to execute `%s': %s\n" command (exntos exn);
667 flush stderr;
671 let version () =
672 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
673 (platform_to_string platform) Sys.word_size Sys.ocaml_version
676 let makehelp () =
677 let strings = version () :: "" :: Help.keys in
678 Array.of_list (
679 List.map (fun s ->
680 let url = geturl s in
681 if String.length url > 0
682 then (s, 0, Action (fun u -> gotouri url; u))
683 else (s, 0, Noaction)
684 ) strings);
687 let noghyll _ = ();;
688 let firstgeomcmds = "", [];;
689 let noreprf () = ();;
691 let state =
692 { sr = Unix.stdin
693 ; sw = Unix.stdin
694 ; wsfd = Unix.stdin
695 ; errfd = None
696 ; stderr = Unix.stderr
697 ; errmsgs = Buffer.create 0
698 ; newerrmsgs = false
699 ; x = 0
700 ; y = 0
701 ; w = 0
702 ; anchor = emptyanchor
703 ; ranchors = []
704 ; layout = []
705 ; maxy = max_int
706 ; tilelru = Queue.create ()
707 ; pagemap = Hashtbl.create 10
708 ; tilemap = Hashtbl.create 10
709 ; pdims = []
710 ; pagecount = 0
711 ; currently = Idle
712 ; mstate = Mnone
713 ; rects = []
714 ; rects1 = []
715 ; text = ""
716 ; mode = View
717 ; winstate = []
718 ; searchpattern = ""
719 ; outlines = [||]
720 ; bookmarks = []
721 ; path = ""
722 ; password = ""
723 ; nameddest = ""
724 ; geomcmds = firstgeomcmds
725 ; hists =
726 { nav = cbnew 10 emptyanchor
727 ; pat = cbnew 10 ""
728 ; pag = cbnew 10 ""
729 ; sel = cbnew 10 ""
731 ; memused = 0
732 ; gen = 0
733 ; throttle = None
734 ; autoscroll = None
735 ; ghyll = noghyll
736 ; help = makehelp ()
737 ; docinfo = []
738 ; texid = None
739 ; prevzoom = (1.0, 0)
740 ; progress = -1.0
741 ; uioh = nouioh
742 ; redisplay = true
743 ; mpos = (-1, -1)
744 ; keystate = KSnone
745 ; glinks = false
746 ; prevcolumns = None
747 ; winw = -1
748 ; winh = -1
749 ; reprf = noreprf
750 ; origin = ""
751 ; roam = (fun () -> ())
752 ; bzoom = false
756 let hscrollh () =
757 if (conf.scrollb land scrollbhv = 0)
758 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
759 then 0
760 else conf.scrollbw
763 let vscrollw () =
764 if (conf.scrollb land scrollbvv = 0)
765 then 0
766 else conf.scrollbw
769 let wadjsb w = w - vscrollw ();;
771 let setfontsize n =
772 fstate.fontsize <- n;
773 fstate.wwidth <- measurestr fstate.fontsize "w";
774 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
777 let vlog fmt =
778 if conf.verbose
779 then
780 Printf.kprintf prerr_endline fmt
781 else
782 Printf.kprintf ignore fmt
785 let launchpath () =
786 if String.length conf.pathlauncher = 0
787 then print_endline state.path
788 else (
789 let re = Str.regexp "%s" in
790 let command = Str.global_replace re state.path conf.pathlauncher in
791 try popen command []
792 with exn ->
793 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
794 flush stderr;
798 module Ne = struct
799 type 'a t = | Res of 'a | Exn of exn;;
801 let pipe () =
802 try Res (Unix.pipe ())
803 with exn -> Exn exn
806 let clo fd f =
807 try tempfailureretry Unix.close fd
808 with exn -> f (exntos exn)
811 let dup fd =
812 try Res (tempfailureretry Unix.dup fd)
813 with exn -> Exn exn
816 let dup2 fd1 fd2 =
817 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
818 with exn -> Exn exn
820 end;;
822 let redirectstderr () =
823 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
824 if conf.redirectstderr
825 then
826 match Ne.pipe () with
827 | Ne.Exn exn ->
828 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
830 | Ne.Res (r, w) ->
831 begin match Ne.dup Unix.stderr with
832 | Ne.Exn exn ->
833 dolog "failed to dup stderr: %s" (exntos exn);
834 Ne.clo r (clofail "pipe/r");
835 Ne.clo w (clofail "pipe/w");
837 | Ne.Res dupstderr ->
838 begin match Ne.dup2 w Unix.stderr with
839 | Ne.Exn exn ->
840 dolog "failed to dup2 to stderr: %s" (exntos exn);
841 Ne.clo dupstderr (clofail "stderr duplicate");
842 Ne.clo r (clofail "redir pipe/r");
843 Ne.clo w (clofail "redir pipe/w");
845 | Ne.Res () ->
846 state.stderr <- dupstderr;
847 state.errfd <- Some r;
848 end;
850 else (
851 state.newerrmsgs <- false;
852 begin match state.errfd with
853 | Some fd ->
854 begin match Ne.dup2 state.stderr Unix.stderr with
855 | Ne.Exn exn ->
856 dolog "failed to dup2 original stderr: %s" (exntos exn)
857 | Ne.Res () ->
858 Ne.clo fd (clofail "dup of stderr");
859 state.errfd <- None;
860 end;
861 | None -> ()
862 end;
863 prerr_string (Buffer.contents state.errmsgs);
864 flush stderr;
865 Buffer.clear state.errmsgs;
869 module G =
870 struct
871 let postRedisplay who =
872 if conf.verbose
873 then prerr_endline ("redisplay for " ^ who);
874 state.redisplay <- true;
876 end;;
878 let getopaque pageno =
879 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
880 with Not_found -> None
883 let putopaque pageno opaque =
884 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
887 let pagetranslatepoint l x y =
888 let dy = y - l.pagedispy in
889 let y = dy + l.pagey in
890 let dx = x - l.pagedispx in
891 let x = dx + l.pagex in
892 (x, y);
895 let onppundermouse g x y d =
896 let rec f = function
897 | l :: rest ->
898 begin match getopaque l.pageno with
899 | Some opaque ->
900 let x0 = l.pagedispx in
901 let x1 = x0 + l.pagevw in
902 let y0 = l.pagedispy in
903 let y1 = y0 + l.pagevh in
904 if y >= y0 && y <= y1 && x >= x0 && x <= x1
905 then
906 let px, py = pagetranslatepoint l x y in
907 match g opaque l px py with
908 | Some res -> res
909 | None -> f rest
910 else f rest
911 | _ ->
912 f rest
914 | [] -> d
916 f state.layout
919 let getunder x y =
920 let g opaque l px py =
921 if state.bzoom
922 then (
923 match rectofblock opaque px py with
924 | Some a ->
925 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
926 state.rects <- [l.pageno, l.pageno mod 3, rect];
927 G.postRedisplay "rectofblock";
928 | None -> ()
930 match whatsunder opaque px py with
931 | Unone -> None
932 | under -> Some under
934 onppundermouse g x y Unone
937 let unproject x y =
938 let g opaque l x y =
939 match unproject opaque x y with
940 | Some (x, y) -> Some (Some (l.pageno, x, y))
941 | None -> None
943 onppundermouse g x y None;
946 let showtext c s =
947 state.text <- Printf.sprintf "%c%s" c s;
948 G.postRedisplay "showtext";
951 let paxunder x y =
952 let g opaque l px py =
953 if markunder opaque px py conf.paxmark
954 then (
955 Some (fun () ->
956 match getopaque l.pageno with
957 | None -> ()
958 | Some opaque ->
959 match Ne.pipe () with
960 | Ne.Exn exn ->
961 showtext '!'
962 (Printf.sprintf
963 "can not create mark pipe: %s"
964 (exntos exn));
965 | Ne.Res (r, w) ->
966 let doclose what fd =
967 Ne.clo fd (fun msg ->
968 dolog "%s close failed: %s" what msg)
971 popen conf.paxcmd [r, 0; w, -1];
972 copysel w opaque false;
973 doclose "pipe/r" r;
974 G.postRedisplay "paxunder";
975 with exn ->
976 dolog "can not execute %S: %s"
977 conf.paxcmd (exntos exn);
978 doclose "pipe/r" r;
979 doclose "pipe/w" w;
982 else None
984 G.postRedisplay "paxunder";
985 state.roam <-
986 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
989 let selstring s =
990 match Ne.pipe () with
991 | Ne.Exn exn ->
992 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
993 | Ne.Res (r, w) ->
994 let popened =
995 try popen conf.selcmd [r, 0; w, -1]; true
996 with exn ->
997 showtext '!'
998 (Printf.sprintf "failed to execute %s: %s"
999 conf.selcmd (exntos exn));
1000 false
1002 let clo cap fd =
1003 Ne.clo fd (fun msg ->
1004 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1007 if popened
1008 then
1009 (try
1010 let l = String.length s in
1011 let n = tempfailureretry (Unix.write w s 0) l in
1012 if n != l
1013 then
1014 showtext '!'
1015 (Printf.sprintf
1016 "failed to write %d characters to sel pipe, wrote %d"
1019 with exn ->
1020 showtext '!'
1021 (Printf.sprintf "failed to write to sel pipe: %s"
1022 (exntos exn)
1025 else dolog "%s" s;
1026 clo "pipe/r" r;
1027 clo "pipe/w" w;
1030 let undertext = function
1031 | Unone -> "none"
1032 | Ulinkuri s -> s
1033 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1034 | Utext s -> "font: " ^ s
1035 | Uunexpected s -> "unexpected: " ^ s
1036 | Ulaunch s -> "launch: " ^ s
1037 | Unamed s -> "named: " ^ s
1038 | Uremote (filename, pageno) ->
1039 Printf.sprintf "%s: page %d" filename (pageno+1)
1042 let updateunder x y =
1043 match getunder x y with
1044 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1045 | Ulinkuri uri ->
1046 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1047 Wsi.setcursor Wsi.CURSOR_INFO
1048 | Ulinkgoto (pageno, _) ->
1049 if conf.underinfo
1050 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1051 Wsi.setcursor Wsi.CURSOR_INFO
1052 | Utext s ->
1053 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1054 Wsi.setcursor Wsi.CURSOR_TEXT
1055 | Uunexpected s ->
1056 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1057 Wsi.setcursor Wsi.CURSOR_INHERIT
1058 | Ulaunch s ->
1059 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1060 Wsi.setcursor Wsi.CURSOR_INHERIT
1061 | Unamed s ->
1062 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1063 Wsi.setcursor Wsi.CURSOR_INHERIT
1064 | Uremote (filename, pageno) ->
1065 if conf.underinfo then showtext 'r'
1066 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1067 Wsi.setcursor Wsi.CURSOR_INFO
1070 let showlinktype under =
1071 if conf.underinfo
1072 then
1073 match under with
1074 | Unone -> ()
1075 | under ->
1076 let s = undertext under in
1077 showtext ' ' s
1080 let addchar s c =
1081 let b = Buffer.create (String.length s + 1) in
1082 Buffer.add_string b s;
1083 Buffer.add_char b c;
1084 Buffer.contents b;
1087 module type TextEnumType =
1089 type t
1090 val name : string
1091 val names : string array
1092 end;;
1094 module TextEnumMake (Ten : TextEnumType) =
1095 struct
1096 let names = Ten.names;;
1097 let to_int (t : Ten.t) = Obj.magic t;;
1098 let to_string t = names.(to_int t);;
1099 let of_int n : Ten.t = Obj.magic n;;
1100 let of_string s =
1101 let rec find i =
1102 if i = Array.length names
1103 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1104 else (
1105 if Ten.names.(i) = s
1106 then of_int i
1107 else find (i+1)
1109 in find 0;;
1110 end;;
1112 module CSTE = TextEnumMake (struct
1113 type t = colorspace;;
1114 let name = "colorspace";;
1115 let names = [|"rgb"; "bgr"; "gray"|];;
1116 end);;
1118 module MTE = TextEnumMake (struct
1119 type t = mark;;
1120 let name = "mark";;
1121 let names = [|"page"; "block"; "line"; "word"|];;
1122 end);;
1124 module FMTE = TextEnumMake (struct
1125 type t= fitmodel;;
1126 let name = "fitmodel";;
1127 let names = [|"width"; "proportional"; "page"|];;
1128 end);;
1130 let intentry_with_suffix text key =
1131 let c =
1132 if key >= 32 && key < 127
1133 then Char.chr key
1134 else '\000'
1136 match Char.lowercase c with
1137 | '0' .. '9' ->
1138 let text = addchar text c in
1139 TEcont text
1141 | 'k' | 'm' | 'g' ->
1142 let text = addchar text c in
1143 TEcont text
1145 | _ ->
1146 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1147 TEcont text
1150 let multicolumns_to_string (n, a, b) =
1151 if a = 0 && b = 0
1152 then Printf.sprintf "%d" n
1153 else Printf.sprintf "%d,%d,%d" n a b;
1156 let multicolumns_of_string s =
1158 (int_of_string s, 0, 0)
1159 with _ ->
1160 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1161 if a > 1 || b > 1
1162 then failwith "subtly broken"; (n, a, b)
1166 let readcmd fd =
1167 let s = "xxxx" in
1168 let n = tempfailureretry (Unix.read fd s 0) 4 in
1169 if n != 4 then failwith "incomplete read(len)";
1170 let len = 0
1171 lor (Char.code s.[0] lsl 24)
1172 lor (Char.code s.[1] lsl 16)
1173 lor (Char.code s.[2] lsl 8)
1174 lor (Char.code s.[3] lsl 0)
1176 let s = String.create len in
1177 let n = tempfailureretry (Unix.read fd s 0) len in
1178 if n != len then failwith "incomplete read(data)";
1182 let btod b = if b then 1 else 0;;
1184 let wcmd fmt =
1185 let b = Buffer.create 16 in
1186 Buffer.add_string b "llll";
1187 Printf.kbprintf
1188 (fun b ->
1189 let s = Buffer.contents b in
1190 let n = String.length s in
1191 let len = n - 4 in
1192 (* dolog "wcmd %S" (String.sub s 4 len); *)
1193 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1194 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1195 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1196 s.[3] <- Char.chr (len land 0xff);
1197 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1198 if n' != n then failwith "write failed";
1199 ) b fmt;
1202 let calcips h =
1203 let d = state.winh - h in
1204 max conf.interpagespace ((d + 1) / 2)
1207 let rowyh (c, coverA, coverB) b n =
1208 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1209 then
1210 let _, _, vy, (_, _, h, _) = b.(n) in
1211 (vy, h)
1212 else
1213 let n' = n - coverA in
1214 let d = n' mod c in
1215 let s = n - d in
1216 let e = min state.pagecount (s + c) in
1217 let rec find m miny maxh = if m = e then miny, maxh else
1218 let _, _, y, (_, _, h, _) = b.(m) in
1219 let miny = min miny y in
1220 let maxh = max maxh h in
1221 find (m+1) miny maxh
1222 in find s max_int 0
1225 let calcheight () =
1226 match conf.columns with
1227 | Cmulti ((_, _, _) as cl, b) ->
1228 if Array.length b > 0
1229 then
1230 let y, h = rowyh cl b (Array.length b - 1) in
1231 y + h + (if conf.presentation then calcips h else 0)
1232 else 0
1233 | Csingle b ->
1234 if Array.length b > 0
1235 then
1236 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1237 y + h + (if conf.presentation then calcips h else 0)
1238 else 0
1239 | Csplit (_, b) ->
1240 if Array.length b > 0
1241 then
1242 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1243 y + h
1244 else 0
1247 let getpageyh pageno =
1248 let pageno = bound pageno 0 (state.pagecount-1) in
1249 match conf.columns with
1250 | Csingle b ->
1251 if Array.length b = 0
1252 then 0, 0
1253 else
1254 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1255 let y =
1256 if conf.presentation
1257 then y - calcips h
1258 else y
1260 y, h
1261 | Cmulti (cl, b) ->
1262 if Array.length b = 0
1263 then 0, 0
1264 else
1265 let y, h = rowyh cl b pageno in
1266 let y =
1267 if conf.presentation
1268 then y - calcips h
1269 else y
1271 y, h
1272 | Csplit (c, b) ->
1273 if Array.length b = 0
1274 then 0, 0
1275 else
1276 let n = pageno*c in
1277 let (_, _, y, (_, _, h, _)) = b.(n) in
1278 y, h
1281 let getpagedim pageno =
1282 let rec f ppdim l =
1283 match l with
1284 | (n, _, _, _) as pdim :: rest ->
1285 if n >= pageno
1286 then (if n = pageno then pdim else ppdim)
1287 else f pdim rest
1289 | [] -> ppdim
1291 f (-1, -1, -1, -1) state.pdims
1294 let getpagey pageno = fst (getpageyh pageno);;
1296 let nogeomcmds cmds =
1297 match cmds with
1298 | s, [] -> String.length s = 0
1299 | _ -> false
1302 let page_of_y y =
1303 let ((c, coverA, coverB) as cl), b =
1304 match conf.columns with
1305 | Csingle b -> (1, 0, 0), b
1306 | Cmulti (c, b) -> c, b
1307 | Csplit (_, b) -> (1, 0, 0), b
1309 if Array.length b = 0
1310 then -1
1311 else
1312 let rec bsearch nmin nmax =
1313 if nmin > nmax
1314 then bound nmin 0 (state.pagecount-1)
1315 else
1316 let n = (nmax + nmin) / 2 in
1317 let vy, h = rowyh cl b n in
1318 let y0, y1 =
1319 if conf.presentation
1320 then
1321 let ips = calcips h in
1322 let y0 = vy - ips in
1323 let y1 = vy + h + ips in
1324 y0, y1
1325 else (
1326 if n = 0
1327 then 0, vy + h + conf.interpagespace
1328 else
1329 let y0 = vy - conf.interpagespace in
1330 y0, y0 + h + conf.interpagespace
1333 if y >= y0 && y < y1
1334 then (
1335 if c = 1
1336 then n
1337 else (
1338 if n > coverA
1339 then
1340 if n < state.pagecount - coverB
1341 then ((n-coverA)/c)*c + coverA
1342 else n
1343 else n
1346 else (
1347 if y > y0
1348 then bsearch (n+1) nmax
1349 else bsearch nmin (n-1)
1352 let r = bsearch 0 (state.pagecount-1) in
1356 let layoutN ((columns, coverA, coverB), b) y sh =
1357 let sh = sh - (hscrollh ()) in
1358 let rec fold accu n =
1359 if n = Array.length b
1360 then accu
1361 else
1362 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1363 if (vy - y) > sh &&
1364 (n = coverA - 1
1365 || n = state.pagecount - coverB
1366 || (n - coverA) mod columns = columns - 1)
1367 then accu
1368 else
1369 let accu =
1370 if vy + h > y
1371 then
1372 let pagey = max 0 (y - vy) in
1373 let pagedispy = if pagey > 0 then 0 else vy - y in
1374 let pagedispx, pagex =
1375 let pdx =
1376 if n = coverA - 1 || n = state.pagecount - coverB
1377 then state.x + (wadjsb state.winw - w) / 2
1378 else dx + xoff + state.x
1380 if pdx < 0
1381 then 0, -pdx
1382 else pdx, 0
1384 let pagevw =
1385 let vw = wadjsb state.winw - pagedispx in
1386 let pw = w - pagex in
1387 min vw pw
1389 let pagevh = min (h - pagey) (sh - pagedispy) in
1390 if pagevw > 0 && pagevh > 0
1391 then
1392 let e =
1393 { pageno = n
1394 ; pagedimno = pdimno
1395 ; pagew = w
1396 ; pageh = h
1397 ; pagex = pagex
1398 ; pagey = pagey
1399 ; pagevw = pagevw
1400 ; pagevh = pagevh
1401 ; pagedispx = pagedispx
1402 ; pagedispy = pagedispy
1403 ; pagecol = 0
1406 e :: accu
1407 else
1408 accu
1409 else
1410 accu
1412 fold accu (n+1)
1414 List.rev (fold [] (page_of_y y));
1417 let layoutS (columns, b) y sh =
1418 let sh = sh - hscrollh () in
1419 let rec fold accu n =
1420 if n = Array.length b
1421 then accu
1422 else
1423 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1424 if (vy - y) > sh
1425 then accu
1426 else
1427 let accu =
1428 if vy + pageh > y
1429 then
1430 let x = xoff + state.x in
1431 let pagey = max 0 (y - vy) in
1432 let pagedispy = if pagey > 0 then 0 else vy - y in
1433 let pagedispx, pagex =
1434 if px = 0
1435 then (
1436 if x < 0
1437 then 0, -x
1438 else x, 0
1440 else (
1441 let px = px - x in
1442 if px < 0
1443 then -px, 0
1444 else 0, px
1447 let pagecolw = pagew/columns in
1448 let pagedispx =
1449 if pagecolw < state.winw
1450 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1451 else pagedispx
1453 let pagevw =
1454 let vw = wadjsb state.winw - pagedispx in
1455 let pw = pagew - pagex in
1456 min vw pw
1458 let pagevw = min pagevw pagecolw in
1459 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1460 if pagevw > 0 && pagevh > 0
1461 then
1462 let e =
1463 { pageno = n/columns
1464 ; pagedimno = pdimno
1465 ; pagew = pagew
1466 ; pageh = pageh
1467 ; pagex = pagex
1468 ; pagey = pagey
1469 ; pagevw = pagevw
1470 ; pagevh = pagevh
1471 ; pagedispx = pagedispx
1472 ; pagedispy = pagedispy
1473 ; pagecol = n mod columns
1476 e :: accu
1477 else
1478 accu
1479 else
1480 accu
1482 fold accu (n+1)
1484 List.rev (fold [] 0)
1487 let layout y sh =
1488 if nogeomcmds state.geomcmds
1489 then
1490 match conf.columns with
1491 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1492 | Cmulti c -> layoutN c y sh
1493 | Csplit s -> layoutS s y sh
1494 else []
1497 let clamp incr =
1498 let y = state.y + incr in
1499 let y = max 0 y in
1500 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1504 let itertiles l f =
1505 let tilex = l.pagex mod conf.tilew in
1506 let tiley = l.pagey mod conf.tileh in
1508 let col = l.pagex / conf.tilew in
1509 let row = l.pagey / conf.tileh in
1511 let rec rowloop row y0 dispy h =
1512 if h = 0
1513 then ()
1514 else (
1515 let dh = conf.tileh - y0 in
1516 let dh = min h dh in
1517 let rec colloop col x0 dispx w =
1518 if w = 0
1519 then ()
1520 else (
1521 let dw = conf.tilew - x0 in
1522 let dw = min w dw in
1524 f col row dispx dispy x0 y0 dw dh;
1525 colloop (col+1) 0 (dispx+dw) (w-dw)
1528 colloop col tilex l.pagedispx l.pagevw;
1529 rowloop (row+1) 0 (dispy+dh) (h-dh)
1532 if l.pagevw > 0 && l.pagevh > 0
1533 then rowloop row tiley l.pagedispy l.pagevh;
1536 let gettileopaque l col row =
1537 let key =
1538 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1540 try Some (Hashtbl.find state.tilemap key)
1541 with Not_found -> None
1544 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1545 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1546 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1549 let drawtiles l color =
1550 GlDraw.color color;
1551 let f col row x y tilex tiley w h =
1552 match gettileopaque l col row with
1553 | Some (opaque, _, t) ->
1554 let params = x, y, w, h, tilex, tiley in
1555 if conf.invert
1556 then (
1557 Gl.enable `blend;
1558 GlFunc.blend_func `zero `one_minus_src_color;
1560 drawtile params opaque;
1561 if conf.invert
1562 then Gl.disable `blend;
1563 if conf.debug
1564 then (
1565 let s = Printf.sprintf
1566 "%d[%d,%d] %f sec"
1567 l.pageno col row t
1569 let w = measurestr fstate.fontsize s in
1570 GlMisc.push_attrib [`current];
1571 GlDraw.color (0.0, 0.0, 0.0);
1572 GlDraw.rect
1573 (float (x-2), float (y-2))
1574 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1575 GlDraw.color (1.0, 1.0, 1.0);
1576 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1577 GlMisc.pop_attrib ();
1580 | _ ->
1581 let w =
1582 let lw = wadjsb state.winw - x in
1583 min lw w
1584 and h =
1585 let lh = state.winh - y in
1586 min lh h
1588 begin match state.texid with
1589 | Some id ->
1590 Gl.enable `texture_2d;
1591 GlTex.bind_texture `texture_2d id;
1592 let x0 = float x
1593 and y0 = float y
1594 and x1 = float (x+w)
1595 and y1 = float (y+h) in
1597 let tw = float w /. 16.0
1598 and th = float h /. 16.0 in
1599 let tx0 = float tilex /. 16.0
1600 and ty0 = float tiley /. 16.0 in
1601 let tx1 = tx0 +. tw
1602 and ty1 = ty0 +. th in
1603 GlDraw.begins `quads;
1604 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1605 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1606 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1607 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1608 GlDraw.ends ();
1610 Gl.disable `texture_2d;
1611 | None ->
1612 GlDraw.color (1.0, 1.0, 1.0);
1613 GlDraw.rect
1614 (float x, float y)
1615 (float (x+w), float (y+h));
1616 end;
1617 if w > 128 && h > fstate.fontsize + 10
1618 then (
1619 GlDraw.color (0.0, 0.0, 0.0);
1620 let c, r =
1621 if conf.verbose
1622 then (col*conf.tilew, row*conf.tileh)
1623 else col, row
1625 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1627 GlDraw.color color;
1629 itertiles l f
1632 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1634 let tilevisible1 l x y =
1635 let ax0 = l.pagex
1636 and ax1 = l.pagex + l.pagevw
1637 and ay0 = l.pagey
1638 and ay1 = l.pagey + l.pagevh in
1640 let bx0 = x
1641 and by0 = y in
1642 let bx1 = min (bx0 + conf.tilew) l.pagew
1643 and by1 = min (by0 + conf.tileh) l.pageh in
1645 let rx0 = max ax0 bx0
1646 and ry0 = max ay0 by0
1647 and rx1 = min ax1 bx1
1648 and ry1 = min ay1 by1 in
1650 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1651 nonemptyintersection
1654 let tilevisible layout n x y =
1655 let rec findpageinlayout m = function
1656 | l :: rest when l.pageno = n ->
1657 tilevisible1 l x y || (
1658 match conf.columns with
1659 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1660 | _ -> false
1662 | _ :: rest -> findpageinlayout 0 rest
1663 | [] -> false
1665 findpageinlayout 0 layout;
1668 let tileready l x y =
1669 tilevisible1 l x y &&
1670 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1673 let tilepage n p layout =
1674 let rec loop = function
1675 | l :: rest ->
1676 if l.pageno = n
1677 then
1678 let f col row _ _ _ _ _ _ =
1679 if state.currently = Idle
1680 then
1681 match gettileopaque l col row with
1682 | Some _ -> ()
1683 | None ->
1684 let x = col*conf.tilew
1685 and y = row*conf.tileh in
1686 let w =
1687 let w = l.pagew - x in
1688 min w conf.tilew
1690 let h =
1691 let h = l.pageh - y in
1692 min h conf.tileh
1694 let pbo =
1695 if conf.usepbo
1696 then getpbo w h conf.colorspace
1697 else "0"
1699 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1700 state.currently <-
1701 Tiling (
1702 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1703 conf.tilew, conf.tileh
1706 itertiles l f;
1707 else
1708 loop rest
1710 | [] -> ()
1712 if nogeomcmds state.geomcmds
1713 then loop layout;
1716 let preloadlayout y =
1717 let y = if y < state.winh then 0 else y - state.winh in
1718 let h = state.winh*3 in
1719 layout y h;
1722 let load pages =
1723 let rec loop pages =
1724 if state.currently != Idle
1725 then ()
1726 else
1727 match pages with
1728 | l :: rest ->
1729 begin match getopaque l.pageno with
1730 | None ->
1731 wcmd "page %d %d" l.pageno l.pagedimno;
1732 state.currently <- Loading (l, state.gen);
1733 | Some opaque ->
1734 tilepage l.pageno opaque pages;
1735 loop rest
1736 end;
1737 | _ -> ()
1739 if nogeomcmds state.geomcmds
1740 then loop pages
1743 let preload pages =
1744 load pages;
1745 if conf.preload && state.currently = Idle
1746 then load (preloadlayout state.y);
1749 let layoutready layout =
1750 let rec fold all ls =
1751 all && match ls with
1752 | l :: rest ->
1753 let seen = ref false in
1754 let allvisible = ref true in
1755 let foo col row _ _ _ _ _ _ =
1756 seen := true;
1757 allvisible := !allvisible &&
1758 begin match gettileopaque l col row with
1759 | Some _ -> true
1760 | None -> false
1763 itertiles l foo;
1764 fold (!seen && !allvisible) rest
1765 | [] -> true
1767 let alltilesvisible = fold true layout in
1768 alltilesvisible;
1771 let gotoy y =
1772 let y = bound y 0 state.maxy in
1773 let y, layout, proceed =
1774 match conf.maxwait with
1775 | Some time when state.ghyll == noghyll ->
1776 begin match state.throttle with
1777 | None ->
1778 let layout = layout y state.winh in
1779 let ready = layoutready layout in
1780 if not ready
1781 then (
1782 load layout;
1783 state.throttle <- Some (layout, y, now ());
1785 else G.postRedisplay "gotoy showall (None)";
1786 y, layout, ready
1787 | Some (_, _, started) ->
1788 let dt = now () -. started in
1789 if dt > time
1790 then (
1791 state.throttle <- None;
1792 let layout = layout y state.winh in
1793 load layout;
1794 G.postRedisplay "maxwait";
1795 y, layout, true
1797 else -1, [], false
1800 | _ ->
1801 let layout = layout y state.winh in
1802 if not !wtmode || layoutready layout
1803 then G.postRedisplay "gotoy ready";
1804 y, layout, true
1806 if proceed
1807 then (
1808 state.y <- y;
1809 state.layout <- layout;
1810 begin match state.mode with
1811 | LinkNav (Ltexact (pageno, linkno)) ->
1812 let rec loop = function
1813 | [] ->
1814 state.mode <- LinkNav (Ltgendir 0)
1815 | l :: _ when l.pageno = pageno ->
1816 begin match getopaque pageno with
1817 | None ->
1818 state.mode <- LinkNav (Ltgendir 0)
1819 | Some opaque ->
1820 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1821 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1822 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1823 then state.mode <- LinkNav (Ltgendir 0)
1825 | _ :: rest -> loop rest
1827 loop layout
1828 | _ -> ()
1829 end;
1830 begin match state.mode with
1831 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1832 if not (pagevisible layout pageno)
1833 then (
1834 match state.layout with
1835 | [] -> ()
1836 | l :: _ ->
1837 state.mode <- Birdseye (
1838 conf, leftx, l.pageno, hooverpageno, anchor
1841 | LinkNav (Ltgendir dir as lt) ->
1842 let linknav =
1843 let rec loop = function
1844 | [] -> lt
1845 | l :: rest ->
1846 match getopaque l.pageno with
1847 | None -> loop rest
1848 | Some opaque ->
1849 let link =
1850 let ld =
1851 if dir = 0
1852 then LDfirstvisible (l.pagex, l.pagey, dir)
1853 else (
1854 if dir > 0 then LDfirst else LDlast
1857 findlink opaque ld
1859 match link with
1860 | Lnotfound -> loop rest
1861 | Lfound n ->
1862 showlinktype (getlink opaque n);
1863 Ltexact (l.pageno, n)
1865 loop state.layout
1867 state.mode <- LinkNav linknav
1868 | _ -> ()
1869 end;
1870 preload layout;
1872 state.ghyll <- noghyll;
1873 if conf.updatecurs
1874 then (
1875 let mx, my = state.mpos in
1876 updateunder mx my;
1880 let conttiling pageno opaque =
1881 tilepage pageno opaque
1882 (if conf.preload then preloadlayout state.y else state.layout)
1885 let gotoy_and_clear_text y =
1886 if not conf.verbose then state.text <- "";
1887 gotoy y;
1890 let getanchor1 l =
1891 let top =
1892 let coloff = l.pagecol * l.pageh in
1893 float (l.pagey + coloff) /. float l.pageh
1895 let dtop =
1896 if l.pagedispy = 0
1897 then
1899 else (
1900 if conf.presentation
1901 then float l.pagedispy /. float (calcips l.pageh)
1902 else float l.pagedispy /. float conf.interpagespace
1905 (l.pageno, top, dtop)
1908 let getanchor () =
1909 match state.layout with
1910 | l :: _ -> getanchor1 l
1911 | [] ->
1912 let n = page_of_y state.y in
1913 if n = -1
1914 then state.anchor
1915 else
1916 let y, h = getpageyh n in
1917 let dy = y - state.y in
1918 let dtop =
1919 if conf.presentation
1920 then
1921 let ips = calcips h in
1922 float (dy + ips) /. float ips
1923 else
1924 float dy /. float conf.interpagespace
1926 (n, 0.0, dtop)
1929 let getanchory (n, top, dtop) =
1930 let y, h = getpageyh n in
1931 if conf.presentation
1932 then
1933 let ips = calcips h in
1934 y + truncate (top*.float h -. dtop*.float ips) + ips;
1935 else
1936 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1939 let gotoanchor anchor =
1940 gotoy (getanchory anchor);
1943 let addnav () =
1944 cbput state.hists.nav (getanchor ());
1947 let getnav dir =
1948 let anchor = cbgetc state.hists.nav dir in
1949 getanchory anchor;
1952 let gotoghyll y =
1953 let scroll f n a b =
1954 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1955 let snake f a b =
1956 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1957 if f < a
1958 then s (float f /. float a)
1959 else (
1960 if f > b
1961 then 1.0 -. s ((float (f-b) /. float (n-b)))
1962 else 1.0
1965 snake f a b
1966 and summa f n a b =
1967 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1968 let iv x = x**3.-.0.5*.x**4. in
1969 let iv1 = iv f in
1970 let ins = float a *. iv1
1971 and outs = float (n-b) *. iv1 in
1972 let ones = b - a in
1973 ins +. outs +. float ones
1975 let rec set (_N, _A, _B) y sy =
1976 let sum = summa 1.0 _N _A _B in
1977 let dy = float (y - sy) in
1978 state.ghyll <- (
1979 let rec gf n y1 o =
1980 if n >= _N
1981 then state.ghyll <- noghyll
1982 else
1983 let go n =
1984 let s = scroll n _N _A _B in
1985 let y1 = y1 +. ((s *. dy) /. sum) in
1986 gotoy_and_clear_text (truncate y1);
1987 state.ghyll <- gf (n+1) y1;
1989 match o with
1990 | None -> go n
1991 | Some y' -> set (_N/2, 1, 1) y' state.y
1993 gf 0 (float state.y)
1996 match conf.ghyllscroll with
1997 | None ->
1998 gotoy_and_clear_text y
1999 | Some nab ->
2000 if state.ghyll == noghyll
2001 then set nab y state.y
2002 else state.ghyll (Some y)
2005 let gotopage n top =
2006 let y, h = getpageyh n in
2007 let y = y + (truncate (top *. float h)) in
2008 gotoghyll y
2011 let gotopage1 n top =
2012 let y = getpagey n in
2013 let y = y + top in
2014 gotoghyll y
2017 let invalidate s f =
2018 state.layout <- [];
2019 state.pdims <- [];
2020 state.rects <- [];
2021 state.rects1 <- [];
2022 match state.geomcmds with
2023 | ps, [] when String.length ps = 0 ->
2024 f ();
2025 state.geomcmds <- s, [];
2027 | ps, [] ->
2028 state.geomcmds <- ps, [s, f];
2030 | ps, (s', _) :: rest when s' = s ->
2031 state.geomcmds <- ps, ((s, f) :: rest);
2033 | ps, cmds ->
2034 state.geomcmds <- ps, ((s, f) :: cmds);
2037 let flushpages () =
2038 Hashtbl.iter (fun _ opaque ->
2039 wcmd "freepage %s" opaque;
2040 ) state.pagemap;
2041 Hashtbl.clear state.pagemap;
2044 let flushtiles () =
2045 if not (Queue.is_empty state.tilelru)
2046 then (
2047 Queue.iter (fun (k, p, s) ->
2048 wcmd "freetile %s" p;
2049 state.memused <- state.memused - s;
2050 Hashtbl.remove state.tilemap k;
2051 ) state.tilelru;
2052 state.uioh#infochanged Memused;
2053 Queue.clear state.tilelru;
2055 load state.layout;
2058 let stateh h =
2059 let h = truncate (float h*.conf.zoom) in
2060 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2061 h - d
2064 let opendoc path password =
2065 state.path <- path;
2066 state.password <- password;
2067 state.gen <- state.gen + 1;
2068 state.docinfo <- [];
2070 flushpages ();
2071 setaalevel conf.aalevel;
2072 let titlepath =
2073 if String.length state.origin = 0
2074 then path
2075 else state.origin
2077 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2078 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2079 invalidate "reqlayout"
2080 (fun () ->
2081 wcmd "reqlayout %d %d %d %s\000"
2082 conf.angle (FMTE.to_int conf.fitmodel)
2083 (stateh state.winh) state.nameddest
2087 let reload () =
2088 state.anchor <- getanchor ();
2089 opendoc state.path state.password;
2092 let scalecolor c =
2093 let c = c *. conf.colorscale in
2094 (c, c, c);
2097 let scalecolor2 (r, g, b) =
2098 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2101 let docolumns = function
2102 | Csingle _ ->
2103 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2104 let rec loop pageno pdimno pdim y ph pdims =
2105 if pageno = state.pagecount
2106 then ()
2107 else
2108 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2109 match pdims with
2110 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2111 pdimno+1, pdim, rest
2112 | _ ->
2113 pdimno, pdim, pdims
2115 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2116 let y = y +
2117 (if conf.presentation
2118 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2119 else (if pageno = 0 then 0 else conf.interpagespace)
2122 a.(pageno) <- (pdimno, x, y, pdim);
2123 loop (pageno+1) pdimno pdim (y + h) h pdims
2125 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2126 conf.columns <- Csingle a;
2128 | Cmulti ((columns, coverA, coverB), _) ->
2129 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2130 let rec loop pageno pdimno pdim x y rowh pdims =
2131 let rec fixrow m = if m = pageno then () else
2132 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2133 if h < rowh
2134 then (
2135 let y = y + (rowh - h) / 2 in
2136 a.(m) <- (pdimno, x, y, pdim);
2138 fixrow (m+1)
2140 if pageno = state.pagecount
2141 then fixrow (((pageno - 1) / columns) * columns)
2142 else
2143 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2144 match pdims with
2145 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2146 pdimno+1, pdim, rest
2147 | _ ->
2148 pdimno, pdim, pdims
2150 let x, y, rowh' =
2151 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2152 then (
2153 let x = (wadjsb state.winw - w) / 2 in
2154 let ips =
2155 if conf.presentation then calcips h else conf.interpagespace in
2156 x, y + ips + rowh, h
2158 else (
2159 if (pageno - coverA) mod columns = 0
2160 then (
2161 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2162 let y =
2163 if conf.presentation
2164 then
2165 let ips = calcips h in
2166 y + (if pageno = 0 then 0 else calcips rowh + ips)
2167 else
2168 y + (if pageno = 0 then 0 else conf.interpagespace)
2170 x, y + rowh, h
2172 else x, y, max rowh h
2175 let y =
2176 if pageno > 1 && (pageno - coverA) mod columns = 0
2177 then (
2178 let y =
2179 if pageno = columns && conf.presentation
2180 then (
2181 let ips = calcips rowh in
2182 for i = 0 to pred columns
2184 let (pdimno, x, y, pdim) = a.(i) in
2185 a.(i) <- (pdimno, x, y+ips, pdim)
2186 done;
2187 y+ips;
2189 else y
2191 fixrow (pageno - columns);
2194 else y
2196 a.(pageno) <- (pdimno, x, y, pdim);
2197 let x = x + w + xoff*2 + conf.interpagespace in
2198 loop (pageno+1) pdimno pdim x y rowh' pdims
2200 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2201 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2203 | Csplit (c, _) ->
2204 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2205 let rec loop pageno pdimno pdim y pdims =
2206 if pageno = state.pagecount
2207 then ()
2208 else
2209 let pdimno, ((_, w, h, _) as pdim), pdims =
2210 match pdims with
2211 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2212 pdimno+1, pdim, rest
2213 | _ ->
2214 pdimno, pdim, pdims
2216 let cw = w / c in
2217 let rec loop1 n x y =
2218 if n = c then y else (
2219 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2220 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2223 let y = loop1 0 0 y in
2224 loop (pageno+1) pdimno pdim y pdims
2226 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2227 conf.columns <- Csplit (c, a);
2230 let represent () =
2231 docolumns conf.columns;
2232 state.maxy <- calcheight ();
2233 if state.reprf == noreprf
2234 then (
2235 match state.mode with
2236 | Birdseye (_, _, pageno, _, _) ->
2237 let y, h = getpageyh pageno in
2238 let top = (state.winh - h) / 2 in
2239 gotoy (max 0 (y - top))
2240 | _ -> gotoanchor state.anchor
2242 else (
2243 state.reprf ();
2244 state.reprf <- noreprf;
2248 let reshape w h =
2249 GlDraw.viewport 0 0 w h;
2250 let firsttime = state.geomcmds == firstgeomcmds in
2251 if not firsttime && nogeomcmds state.geomcmds
2252 then state.anchor <- getanchor ();
2254 state.winw <- w;
2255 let w = wadjsb (truncate (float w *. conf.zoom)) in
2256 let w = max w 2 in
2257 state.winh <- h;
2258 setfontsize fstate.fontsize;
2259 GlMat.mode `modelview;
2260 GlMat.load_identity ();
2262 GlMat.mode `projection;
2263 GlMat.load_identity ();
2264 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2265 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2266 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2268 let relx =
2269 if conf.zoom <= 1.0
2270 then 0.0
2271 else float state.x /. float state.w
2273 invalidate "geometry"
2274 (fun () ->
2275 state.w <- w;
2276 if not firsttime
2277 then state.x <- truncate (relx *. float w);
2278 let w =
2279 match conf.columns with
2280 | Csingle _ -> w
2281 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2282 | Csplit (c, _) -> w * c
2284 wcmd "geometry %d %d %d"
2285 w (stateh h) (FMTE.to_int conf.fitmodel)
2289 let enttext () =
2290 let len = String.length state.text in
2291 let drawstring s =
2292 let hscrollh =
2293 match state.mode with
2294 | Textentry _ | View | LinkNav _ ->
2295 let h, _, _ = state.uioh#scrollpw in
2297 | _ -> 0
2299 let rect x w =
2300 GlDraw.rect
2301 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2302 (x+.w, float (state.winh - hscrollh))
2305 let w = float (wadjsb state.winw - 1) in
2306 if state.progress >= 0.0 && state.progress < 1.0
2307 then (
2308 GlDraw.color (0.3, 0.3, 0.3);
2309 let w1 = w *. state.progress in
2310 rect 0.0 w1;
2311 GlDraw.color (0.0, 0.0, 0.0);
2312 rect w1 (w-.w1)
2314 else (
2315 GlDraw.color (0.0, 0.0, 0.0);
2316 rect 0.0 w;
2319 GlDraw.color (1.0, 1.0, 1.0);
2320 drawstring fstate.fontsize
2321 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2323 let s =
2324 match state.mode with
2325 | Textentry ((prefix, text, _, _, _, _), _) ->
2326 let s =
2327 if len > 0
2328 then
2329 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2330 else
2331 Printf.sprintf "%s%s_" prefix text
2335 | _ -> state.text
2337 let s =
2338 if state.newerrmsgs
2339 then (
2340 if not (istextentry state.mode) && state.uioh#eformsgs
2341 then
2342 let s1 = "(press 'e' to review error messasges)" in
2343 if String.length s > 0 then s ^ " " ^ s1 else s1
2344 else s
2346 else s
2348 if String.length s > 0
2349 then drawstring s
2352 let gctiles () =
2353 let len = Queue.length state.tilelru in
2354 let layout = lazy (
2355 match state.throttle with
2356 | None ->
2357 if conf.preload
2358 then preloadlayout state.y
2359 else state.layout
2360 | Some (layout, _, _) ->
2361 layout
2362 ) in
2363 let rec loop qpos =
2364 if state.memused <= conf.memlimit
2365 then ()
2366 else (
2367 if qpos < len
2368 then
2369 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2370 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2371 let (_, pw, ph, _) = getpagedim n in
2373 gen = state.gen
2374 && colorspace = conf.colorspace
2375 && angle = conf.angle
2376 && pagew = pw
2377 && pageh = ph
2378 && (
2379 let x = col*conf.tilew
2380 and y = row*conf.tileh in
2381 tilevisible (Lazy.force_val layout) n x y
2383 then Queue.push lruitem state.tilelru
2384 else (
2385 freepbo p;
2386 wcmd "freetile %s" p;
2387 state.memused <- state.memused - s;
2388 state.uioh#infochanged Memused;
2389 Hashtbl.remove state.tilemap k;
2391 loop (qpos+1)
2394 loop 0
2397 let logcurrently = function
2398 | Idle -> dolog "Idle"
2399 | Loading (l, gen) ->
2400 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2401 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2402 dolog
2403 "Tiling %d[%d,%d] page=%s cs=%s angle"
2404 l.pageno col row pageopaque
2405 (CSTE.to_string colorspace)
2407 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2408 angle gen conf.angle state.gen
2409 tilew tileh
2410 conf.tilew conf.tileh
2412 | Outlining _ ->
2413 dolog "outlining"
2416 let splitatspace =
2417 let r = Str.regexp " " in
2418 fun s -> Str.bounded_split r s 2;
2421 let onpagerect pageno f =
2422 let b =
2423 match conf.columns with
2424 | Cmulti (_, b) -> b
2425 | Csingle b -> b
2426 | Csplit (_, b) -> b
2428 if pageno >= 0 && pageno < Array.length b
2429 then
2430 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2431 let r = getpdimrect pdimno in
2432 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2435 let gotopagexy1 pageno x y =
2436 onpagerect pageno (fun w h ->
2437 let top = y /. h in
2438 let _,w1,_,leftx = getpagedim pageno in
2439 let wh = state.winh - hscrollh () in
2440 let sw = float w1 /. w in
2441 let x = sw *. x in
2442 let x = leftx + state.x + truncate x in
2443 let sx =
2444 if x < 0 || x >= wadjsb state.winw
2445 then state.x - x
2446 else state.x
2448 let py, h = getpageyh pageno in
2449 let pdy = truncate (top *. float h) in
2450 let y' = py + pdy in
2451 let dy = y' - state.y in
2452 let sy =
2453 if x != state.x || not (dy > 0 && dy < wh)
2454 then (
2455 if conf.presentation
2456 then
2457 if abs (py - y') > wh
2458 then y'
2459 else py
2460 else y';
2462 else state.y
2464 if state.x != sx || state.y != sy
2465 then (
2466 let x, y =
2467 if !wtmode
2468 then (
2469 let ww = wadjsb state.winw in
2470 let qx = sx / ww
2471 and qy = pdy / wh in
2472 let x = qx * ww
2473 and y = py + qy * wh in
2474 let x = if -x + ww > w1 then -(w1-ww) else x
2475 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2476 let y =
2477 if conf.presentation
2478 then
2479 if abs (py - y') > wh
2480 then y'
2481 else py
2482 else y';
2484 (x, y)
2486 else (sx, sy)
2488 state.x <- x;
2489 gotoy_and_clear_text y;
2491 else gotoy_and_clear_text state.y;
2495 let gotopagexy pageno x y =
2496 match state.mode with
2497 | Birdseye _ -> gotopage pageno 0.0
2498 | _ -> gotopagexy1 pageno x y
2501 let act cmds =
2502 (* dolog "%S" cmds; *)
2503 let cl = splitatspace cmds in
2504 let scan s fmt f =
2505 try Scanf.sscanf s fmt f
2506 with exn ->
2507 dolog "error processing '%S': %s" cmds (exntos exn);
2508 exit 1
2510 match cl with
2511 | "clear" :: [] ->
2512 state.uioh#infochanged Pdim;
2513 state.pdims <- [];
2515 | "clearrects" :: [] ->
2516 state.rects <- state.rects1;
2517 G.postRedisplay "clearrects";
2519 | "continue" :: args :: [] ->
2520 let n = scan args "%u" (fun n -> n) in
2521 state.pagecount <- n;
2522 begin match state.currently with
2523 | Outlining l ->
2524 state.currently <- Idle;
2525 state.outlines <- Array.of_list (List.rev l)
2526 | _ -> ()
2527 end;
2529 let cur, cmds = state.geomcmds in
2530 if String.length cur = 0
2531 then failwith "umpossible";
2533 begin match List.rev cmds with
2534 | [] ->
2535 state.geomcmds <- "", [];
2536 represent ();
2537 | (s, f) :: rest ->
2538 f ();
2539 state.geomcmds <- s, List.rev rest;
2540 end;
2541 if conf.maxwait = None && not !wtmode
2542 then G.postRedisplay "continue";
2544 | "title" :: args :: [] ->
2545 Wsi.settitle args
2547 | "msg" :: args :: [] ->
2548 showtext ' ' args
2550 | "vmsg" :: args :: [] ->
2551 if conf.verbose
2552 then showtext ' ' args
2554 | "emsg" :: args :: [] ->
2555 Buffer.add_string state.errmsgs args;
2556 state.newerrmsgs <- true;
2557 G.postRedisplay "error message"
2559 | "progress" :: args :: [] ->
2560 let progress, text =
2561 scan args "%f %n"
2562 (fun f pos ->
2563 f, String.sub args pos (String.length args - pos))
2565 state.text <- text;
2566 state.progress <- progress;
2567 G.postRedisplay "progress"
2569 | "firstmatch" :: args :: [] ->
2570 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2571 scan args "%u %d %f %f %f %f %f %f %f %f"
2572 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2573 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2575 let y = (getpagey pageno) + truncate y0 in
2576 addnav ();
2577 gotoy y;
2578 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2580 | "match" :: args :: [] ->
2581 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2582 scan args "%u %d %f %f %f %f %f %f %f %f"
2583 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2584 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2586 state.rects1 <-
2587 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2589 | "page" :: args :: [] ->
2590 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2591 begin match state.currently with
2592 | Loading (l, gen) ->
2593 vlog "page %d took %f sec" l.pageno t;
2594 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2595 begin match state.throttle with
2596 | None ->
2597 let preloadedpages =
2598 if conf.preload
2599 then preloadlayout state.y
2600 else state.layout
2602 let evict () =
2603 let set =
2604 List.fold_left (fun s l -> IntSet.add l.pageno s)
2605 IntSet.empty preloadedpages
2607 let evictedpages =
2608 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2609 if not (IntSet.mem pageno set)
2610 then (
2611 wcmd "freepage %s" opaque;
2612 key :: accu
2614 else accu
2615 ) state.pagemap []
2617 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2619 evict ();
2620 state.currently <- Idle;
2621 if gen = state.gen
2622 then (
2623 tilepage l.pageno pageopaque state.layout;
2624 load state.layout;
2625 load preloadedpages;
2626 if pagevisible state.layout l.pageno
2627 && layoutready state.layout
2628 then G.postRedisplay "page";
2631 | Some (layout, _, _) ->
2632 state.currently <- Idle;
2633 tilepage l.pageno pageopaque layout;
2634 load state.layout
2635 end;
2637 | _ ->
2638 dolog "Inconsistent loading state";
2639 logcurrently state.currently;
2640 exit 1
2643 | "tile" :: args :: [] ->
2644 let (x, y, opaque, size, t) =
2645 scan args "%u %u %s %u %f"
2646 (fun x y p size t -> (x, y, p, size, t))
2648 begin match state.currently with
2649 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2650 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2652 unmappbo opaque;
2653 if tilew != conf.tilew || tileh != conf.tileh
2654 then (
2655 wcmd "freetile %s" opaque;
2656 state.currently <- Idle;
2657 load state.layout;
2659 else (
2660 puttileopaque l col row gen cs angle opaque size t;
2661 state.memused <- state.memused + size;
2662 state.uioh#infochanged Memused;
2663 gctiles ();
2664 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2665 opaque, size) state.tilelru;
2667 let layout =
2668 match state.throttle with
2669 | None -> state.layout
2670 | Some (layout, _, _) -> layout
2673 state.currently <- Idle;
2674 if gen = state.gen
2675 && conf.colorspace = cs
2676 && conf.angle = angle
2677 && tilevisible layout l.pageno x y
2678 then conttiling l.pageno pageopaque;
2680 begin match state.throttle with
2681 | None ->
2682 preload state.layout;
2683 if gen = state.gen
2684 && conf.colorspace = cs
2685 && conf.angle = angle
2686 && tilevisible state.layout l.pageno x y
2687 && (not !wtmode || layoutready state.layout)
2688 then G.postRedisplay "tile nothrottle";
2690 | Some (layout, y, _) ->
2691 let ready = layoutready layout in
2692 if ready
2693 then (
2694 state.y <- y;
2695 state.layout <- layout;
2696 state.throttle <- None;
2697 G.postRedisplay "throttle";
2699 else load layout;
2700 end;
2703 | _ ->
2704 dolog "Inconsistent tiling state";
2705 logcurrently state.currently;
2706 exit 1
2709 | "pdim" :: args :: [] ->
2710 let (n, w, h, _) as pdim =
2711 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2713 let pdim =
2714 match conf.fitmodel, conf.columns with
2715 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2716 | _ -> pdim
2718 state.uioh#infochanged Pdim;
2719 state.pdims <- pdim :: state.pdims
2721 | "o" :: args :: [] ->
2722 let (l, n, t, h, pos) =
2723 scan args "%u %u %d %u %n"
2724 (fun l n t h pos -> l, n, t, h, pos)
2726 let s = String.sub args pos (String.length args - pos) in
2727 let outline = (s, l, (n, float t /. float h, 0.0)) in
2728 begin match state.currently with
2729 | Outlining outlines ->
2730 state.currently <- Outlining (outline :: outlines)
2731 | Idle ->
2732 state.currently <- Outlining [outline]
2733 | currently ->
2734 dolog "invalid outlining state";
2735 logcurrently currently
2738 | "a" :: args :: [] ->
2739 let (n, l, t) =
2740 scan args "%u %d %d" (fun n l t -> n, l, t)
2742 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2744 | "info" :: args :: [] ->
2745 state.docinfo <- (1, args) :: state.docinfo
2747 | "infoend" :: [] ->
2748 state.uioh#infochanged Docinfo;
2749 state.docinfo <- List.rev state.docinfo
2751 | _ ->
2752 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2755 let onhist cb =
2756 let rc = cb.rc in
2757 let action = function
2758 | HCprev -> cbget cb ~-1
2759 | HCnext -> cbget cb 1
2760 | HCfirst -> cbget cb ~-(cb.rc)
2761 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2762 and cancel () = cb.rc <- rc
2763 in (action, cancel)
2766 let search pattern forward =
2767 match conf.columns with
2768 | Csplit _ ->
2769 showtext '!' "searching does not work properly in split columns mode"
2770 | _ ->
2771 if String.length pattern > 0
2772 then
2773 let pn, py =
2774 match state.layout with
2775 | [] -> 0, 0
2776 | l :: _ ->
2777 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2779 wcmd "search %d %d %d %d,%s\000"
2780 (btod conf.icase) pn py (btod forward) pattern;
2783 let intentry text key =
2784 let c =
2785 if key >= 32 && key < 127
2786 then Char.chr key
2787 else '\000'
2789 match c with
2790 | '0' .. '9' ->
2791 let text = addchar text c in
2792 TEcont text
2794 | _ ->
2795 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2796 TEcont text
2799 let linknentry text key =
2800 let c =
2801 if key >= 32 && key < 127
2802 then Char.chr key
2803 else '\000'
2805 match c with
2806 | 'a' .. 'z' ->
2807 let text = addchar text c in
2808 TEcont text
2810 | _ ->
2811 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2812 TEcont text
2815 let linkndone f s =
2816 if String.length s > 0
2817 then (
2818 let n =
2819 let l = String.length s in
2820 let rec loop pos n = if pos = l then n else
2821 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2822 loop (pos+1) (n*26 + m)
2823 in loop 0 0
2825 let rec loop n = function
2826 | [] -> ()
2827 | l :: rest ->
2828 match getopaque l.pageno with
2829 | None -> loop n rest
2830 | Some opaque ->
2831 let m = getlinkcount opaque in
2832 if n < m
2833 then (
2834 let under = getlink opaque n in
2835 f under
2837 else loop (n-m) rest
2839 loop n state.layout;
2843 let textentry text key =
2844 if key land 0xff00 = 0xff00
2845 then TEcont text
2846 else TEcont (text ^ toutf8 key)
2849 let reqlayout angle fitmodel =
2850 match state.throttle with
2851 | None ->
2852 if nogeomcmds state.geomcmds
2853 then state.anchor <- getanchor ();
2854 conf.angle <- angle mod 360;
2855 if conf.angle != 0
2856 then (
2857 match state.mode with
2858 | LinkNav _ -> state.mode <- View
2859 | _ -> ()
2861 conf.fitmodel <- fitmodel;
2862 invalidate "reqlayout"
2863 (fun () ->
2864 wcmd "reqlayout %d %d %d"
2865 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2867 | _ -> ()
2870 let settrim trimmargins trimfuzz =
2871 if nogeomcmds state.geomcmds
2872 then state.anchor <- getanchor ();
2873 conf.trimmargins <- trimmargins;
2874 conf.trimfuzz <- trimfuzz;
2875 let x0, y0, x1, y1 = trimfuzz in
2876 invalidate "settrim"
2877 (fun () ->
2878 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2879 flushpages ();
2882 let setzoom zoom =
2883 match state.throttle with
2884 | None ->
2885 let zoom = max 0.0001 zoom in
2886 if zoom <> conf.zoom
2887 then (
2888 state.prevzoom <- (conf.zoom, state.x);
2889 conf.zoom <- zoom;
2890 reshape state.winw state.winh;
2891 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2894 | Some (layout, y, started) ->
2895 let time =
2896 match conf.maxwait with
2897 | None -> 0.0
2898 | Some t -> t
2900 let dt = now () -. started in
2901 if dt > time
2902 then (
2903 state.y <- y;
2904 load layout;
2908 let setcolumns mode columns coverA coverB =
2909 state.prevcolumns <- Some (conf.columns, conf.zoom);
2910 if columns < 0
2911 then (
2912 if isbirdseye mode
2913 then showtext '!' "split mode doesn't work in bird's eye"
2914 else (
2915 conf.columns <- Csplit (-columns, [||]);
2916 state.x <- 0;
2917 conf.zoom <- 1.0;
2920 else (
2921 if columns < 2
2922 then (
2923 conf.columns <- Csingle [||];
2924 state.x <- 0;
2925 setzoom 1.0;
2927 else (
2928 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2929 conf.zoom <- 1.0;
2932 reshape state.winw state.winh;
2935 let enterbirdseye () =
2936 let zoom = float conf.thumbw /. float state.winw in
2937 let birdseyepageno =
2938 let cy = state.winh / 2 in
2939 let fold = function
2940 | [] -> 0
2941 | l :: rest ->
2942 let rec fold best = function
2943 | [] -> best.pageno
2944 | l :: rest ->
2945 let d = cy - (l.pagedispy + l.pagevh/2)
2946 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2947 if abs d < abs dbest
2948 then fold l rest
2949 else best.pageno
2950 in fold l rest
2952 fold state.layout
2954 state.mode <- Birdseye (
2955 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2957 conf.zoom <- zoom;
2958 conf.presentation <- false;
2959 conf.interpagespace <- 10;
2960 conf.hlinks <- false;
2961 conf.fitmodel <- FitProportional;
2962 state.x <- 0;
2963 state.mstate <- Mnone;
2964 conf.maxwait <- None;
2965 conf.columns <- (
2966 match conf.beyecolumns with
2967 | Some c ->
2968 conf.zoom <- 1.0;
2969 Cmulti ((c, 0, 0), [||])
2970 | None -> Csingle [||]
2972 Wsi.setcursor Wsi.CURSOR_INHERIT;
2973 if conf.verbose
2974 then
2975 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2976 (100.0*.zoom)
2977 else
2978 state.text <- ""
2980 reshape state.winw state.winh;
2983 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2984 state.mode <- View;
2985 conf.zoom <- c.zoom;
2986 conf.presentation <- c.presentation;
2987 conf.interpagespace <- c.interpagespace;
2988 conf.maxwait <- c.maxwait;
2989 conf.hlinks <- c.hlinks;
2990 conf.fitmodel <- c.fitmodel;
2991 conf.beyecolumns <- (
2992 match conf.columns with
2993 | Cmulti ((c, _, _), _) -> Some c
2994 | Csingle _ -> None
2995 | Csplit _ -> failwith "leaving bird's eye split mode"
2997 conf.columns <- (
2998 match c.columns with
2999 | Cmulti (c, _) -> Cmulti (c, [||])
3000 | Csingle _ -> Csingle [||]
3001 | Csplit (c, _) -> Csplit (c, [||])
3003 state.x <- leftx;
3004 if conf.verbose
3005 then
3006 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3007 (100.0*.conf.zoom)
3009 reshape state.winw state.winh;
3010 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3013 let togglebirdseye () =
3014 match state.mode with
3015 | Birdseye vals -> leavebirdseye vals true
3016 | View -> enterbirdseye ()
3017 | _ -> ()
3020 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3021 let pageno = max 0 (pageno - incr) in
3022 let rec loop = function
3023 | [] -> gotopage1 pageno 0
3024 | l :: _ when l.pageno = pageno ->
3025 if l.pagedispy >= 0 && l.pagey = 0
3026 then G.postRedisplay "upbirdseye"
3027 else gotopage1 pageno 0
3028 | _ :: rest -> loop rest
3030 loop state.layout;
3031 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3034 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3035 let pageno = min (state.pagecount - 1) (pageno + incr) in
3036 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3037 let rec loop = function
3038 | [] ->
3039 let y, h = getpageyh pageno in
3040 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3041 gotoy (clamp dy)
3042 | l :: _ when l.pageno = pageno ->
3043 if l.pagevh != l.pageh
3044 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3045 else G.postRedisplay "downbirdseye"
3046 | _ :: rest -> loop rest
3048 loop state.layout
3051 let optentry mode _ key =
3052 let btos b = if b then "on" else "off" in
3053 if key >= 32 && key < 127
3054 then
3055 let c = Char.chr key in
3056 match c with
3057 | 's' ->
3058 let ondone s =
3059 try conf.scrollstep <- int_of_string s with exc ->
3060 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3062 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3064 | 'A' ->
3065 let ondone s =
3067 conf.autoscrollstep <- int_of_string s;
3068 if state.autoscroll <> None
3069 then state.autoscroll <- Some conf.autoscrollstep
3070 with exc ->
3071 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3073 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3075 | 'C' ->
3076 let ondone s =
3078 let n, a, b = multicolumns_of_string s in
3079 setcolumns mode n a b;
3080 with exc ->
3081 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3083 TEswitch ("columns: ", "", None, textentry, ondone, true)
3085 | 'Z' ->
3086 let ondone s =
3088 let zoom = float (int_of_string s) /. 100.0 in
3089 setzoom zoom
3090 with exc ->
3091 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3093 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3095 | 't' ->
3096 let ondone s =
3098 conf.thumbw <- bound (int_of_string s) 2 4096;
3099 state.text <-
3100 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3101 begin match mode with
3102 | Birdseye beye ->
3103 leavebirdseye beye false;
3104 enterbirdseye ();
3105 | _ -> ();
3107 with exc ->
3108 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3110 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3112 | 'R' ->
3113 let ondone s =
3114 match try
3115 Some (int_of_string s)
3116 with exc ->
3117 state.text <- Printf.sprintf "bad integer `%s': %s"
3118 s (exntos exc);
3119 None
3120 with
3121 | Some angle -> reqlayout angle conf.fitmodel
3122 | None -> ()
3124 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3126 | 'i' ->
3127 conf.icase <- not conf.icase;
3128 TEdone ("case insensitive search " ^ (btos conf.icase))
3130 | 'p' ->
3131 conf.preload <- not conf.preload;
3132 gotoy state.y;
3133 TEdone ("preload " ^ (btos conf.preload))
3135 | 'v' ->
3136 conf.verbose <- not conf.verbose;
3137 TEdone ("verbose " ^ (btos conf.verbose))
3139 | 'd' ->
3140 conf.debug <- not conf.debug;
3141 TEdone ("debug " ^ (btos conf.debug))
3143 | 'h' ->
3144 conf.maxhfit <- not conf.maxhfit;
3145 state.maxy <- calcheight ();
3146 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3148 | 'c' ->
3149 conf.crophack <- not conf.crophack;
3150 TEdone ("crophack " ^ btos conf.crophack)
3152 | 'a' ->
3153 let s =
3154 match conf.maxwait with
3155 | None ->
3156 conf.maxwait <- Some infinity;
3157 "always wait for page to complete"
3158 | Some _ ->
3159 conf.maxwait <- None;
3160 "show placeholder if page is not ready"
3162 TEdone s
3164 | 'f' ->
3165 conf.underinfo <- not conf.underinfo;
3166 TEdone ("underinfo " ^ btos conf.underinfo)
3168 | 'P' ->
3169 conf.savebmarks <- not conf.savebmarks;
3170 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3172 | 'S' ->
3173 let ondone s =
3175 let pageno, py =
3176 match state.layout with
3177 | [] -> 0, 0
3178 | l :: _ ->
3179 l.pageno, l.pagey
3181 conf.interpagespace <- int_of_string s;
3182 docolumns conf.columns;
3183 state.maxy <- calcheight ();
3184 let y = getpagey pageno in
3185 gotoy (y + py)
3186 with exc ->
3187 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3189 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3191 | 'l' ->
3192 let fm =
3193 match conf.fitmodel with
3194 | FitProportional -> FitWidth
3195 | _ -> FitProportional
3197 reqlayout conf.angle fm;
3198 TEdone ("proportional display " ^ btos (fm == FitProportional))
3200 | 'T' ->
3201 settrim (not conf.trimmargins) conf.trimfuzz;
3202 TEdone ("trim margins " ^ btos conf.trimmargins)
3204 | 'I' ->
3205 conf.invert <- not conf.invert;
3206 TEdone ("invert colors " ^ btos conf.invert)
3208 | 'x' ->
3209 let ondone s =
3210 cbput state.hists.sel s;
3211 conf.selcmd <- s;
3213 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3214 textentry, ondone, true)
3216 | 'M' ->
3217 if conf.pax == None
3218 then conf.pax <- Some (ref (0.0, 0, 0))
3219 else conf.pax <- None;
3220 TEdone ("PAX " ^ btos (conf.pax != None))
3222 | _ ->
3223 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3224 TEstop
3225 else
3226 TEcont state.text
3229 class type lvsource = object
3230 method getitemcount : int
3231 method getitem : int -> (string * int)
3232 method hasaction : int -> bool
3233 method exit :
3234 uioh:uioh ->
3235 cancel:bool ->
3236 active:int ->
3237 first:int ->
3238 pan:int ->
3239 qsearch:string ->
3240 uioh option
3241 method getactive : int
3242 method getfirst : int
3243 method getqsearch : string
3244 method setqsearch : string -> unit
3245 method getpan : int
3246 end;;
3248 class virtual lvsourcebase = object
3249 val mutable m_active = 0
3250 val mutable m_first = 0
3251 val mutable m_qsearch = ""
3252 val mutable m_pan = 0
3253 method getactive = m_active
3254 method getfirst = m_first
3255 method getqsearch = m_qsearch
3256 method getpan = m_pan
3257 method setqsearch s = m_qsearch <- s
3258 end;;
3260 let withoutlastutf8 s =
3261 let len = String.length s in
3262 if len = 0
3263 then s
3264 else
3265 let rec find pos =
3266 if pos = 0
3267 then pos
3268 else
3269 let b = Char.code s.[pos] in
3270 if b land 0b11000000 = 0b11000000
3271 then pos
3272 else find (pos-1)
3274 let first =
3275 if Char.code s.[len-1] land 0x80 = 0
3276 then len-1
3277 else find (len-1)
3279 String.sub s 0 first;
3282 let textentrykeyboard
3283 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3284 let key =
3285 if key >= 0xffb0 && key <= 0xffb9
3286 then key - 0xffb0 + 48 else key
3288 let enttext te =
3289 state.mode <- Textentry (te, onleave);
3290 state.text <- "";
3291 enttext ();
3292 G.postRedisplay "textentrykeyboard enttext";
3294 let histaction cmd =
3295 match opthist with
3296 | None -> ()
3297 | Some (action, _) ->
3298 state.mode <- Textentry (
3299 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3301 G.postRedisplay "textentry histaction"
3303 match key with
3304 | 0xff08 -> (* backspace *)
3305 let s = withoutlastutf8 text in
3306 let len = String.length s in
3307 if cancelonempty && len = 0
3308 then (
3309 onleave Cancel;
3310 G.postRedisplay "textentrykeyboard after cancel";
3312 else (
3313 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3316 | 0xff0d | 0xff8d -> (* (kp) enter *)
3317 ondone text;
3318 onleave Confirm;
3319 G.postRedisplay "textentrykeyboard after confirm"
3321 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3322 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3323 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3324 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3326 | 0xff1b -> (* escape*)
3327 if String.length text = 0
3328 then (
3329 begin match opthist with
3330 | None -> ()
3331 | Some (_, onhistcancel) -> onhistcancel ()
3332 end;
3333 onleave Cancel;
3334 state.text <- "";
3335 G.postRedisplay "textentrykeyboard after cancel2"
3337 else (
3338 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3341 | 0xff9f | 0xffff -> () (* delete *)
3343 | _ when key != 0
3344 && key land 0xff00 != 0xff00 (* keyboard *)
3345 && key land 0xfe00 != 0xfe00 (* xkb *)
3346 && key land 0xfd00 != 0xfd00 (* 3270 *)
3348 begin match onkey text key with
3349 | TEdone text ->
3350 ondone text;
3351 onleave Confirm;
3352 G.postRedisplay "textentrykeyboard after confirm2";
3354 | TEcont text ->
3355 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3357 | TEstop ->
3358 onleave Cancel;
3359 G.postRedisplay "textentrykeyboard after cancel3"
3361 | TEswitch te ->
3362 state.mode <- Textentry (te, onleave);
3363 G.postRedisplay "textentrykeyboard switch";
3364 end;
3366 | _ ->
3367 vlog "unhandled key %s" (Wsi.keyname key)
3370 let firstof first active =
3371 if first > active || abs (first - active) > fstate.maxrows - 1
3372 then max 0 (active - (fstate.maxrows/2))
3373 else first
3376 let calcfirst first active =
3377 if active > first
3378 then
3379 let rows = active - first in
3380 if rows > fstate.maxrows then active - fstate.maxrows else first
3381 else active
3384 let scrollph y maxy =
3385 let sh = float (maxy + state.winh) /. float state.winh in
3386 let sh = float state.winh /. sh in
3387 let sh = max sh (float conf.scrollh) in
3389 let percent = float y /. float maxy in
3390 let position = (float state.winh -. sh) *. percent in
3392 let position =
3393 if position +. sh > float state.winh
3394 then float state.winh -. sh
3395 else position
3397 position, sh;
3400 let coe s = (s :> uioh);;
3402 class listview ~(source:lvsource) ~trusted ~modehash =
3403 object (self)
3404 val m_pan = source#getpan
3405 val m_first = source#getfirst
3406 val m_active = source#getactive
3407 val m_qsearch = source#getqsearch
3408 val m_prev_uioh = state.uioh
3410 method private elemunder y =
3411 let n = y / (fstate.fontsize+1) in
3412 if m_first + n < source#getitemcount
3413 then (
3414 if source#hasaction (m_first + n)
3415 then Some (m_first + n)
3416 else None
3418 else None
3420 method display =
3421 Gl.enable `blend;
3422 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3423 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3424 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3425 GlDraw.color (1., 1., 1.);
3426 Gl.enable `texture_2d;
3427 let fs = fstate.fontsize in
3428 let nfs = fs + 1 in
3429 let ww = fstate.wwidth in
3430 let tabw = 30.0*.ww in
3431 let itemcount = source#getitemcount in
3432 let rec loop row =
3433 if (row - m_first) > fstate.maxrows
3434 then ()
3435 else (
3436 if row >= 0 && row < itemcount
3437 then (
3438 let (s, level) = source#getitem row in
3439 let y = (row - m_first) * nfs in
3440 let x = 5.0 +. float (level + m_pan) *. ww in
3441 if row = m_active
3442 then (
3443 Gl.disable `texture_2d;
3444 GlDraw.polygon_mode `both `line;
3445 let alpha = if source#hasaction row then 0.9 else 0.3 in
3446 GlDraw.color (1., 1., 1.) ~alpha;
3447 GlDraw.rect (1., float (y + 1))
3448 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3449 GlDraw.polygon_mode `both `fill;
3450 GlDraw.color (1., 1., 1.);
3451 Gl.enable `texture_2d;
3454 let drawtabularstring s =
3455 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3456 if trusted
3457 then
3458 let tabpos = try String.index s '\t' with Not_found -> -1 in
3459 if tabpos > 0
3460 then
3461 let len = String.length s - tabpos - 1 in
3462 let s1 = String.sub s 0 tabpos
3463 and s2 = String.sub s (tabpos + 1) len in
3464 let nx = drawstr x s1 in
3465 let sw = nx -. x in
3466 let x = x +. (max tabw sw) in
3467 drawstr x s2
3468 else
3469 drawstr x s
3470 else
3471 drawstr x s
3473 let _ = drawtabularstring s in
3474 loop (row+1)
3478 loop m_first;
3479 Gl.disable `blend;
3480 Gl.disable `texture_2d;
3482 method updownlevel incr =
3483 let len = source#getitemcount in
3484 let curlevel =
3485 if m_active >= 0 && m_active < len
3486 then snd (source#getitem m_active)
3487 else -1
3489 let rec flow i =
3490 if i = len then i-1 else if i = -1 then 0 else
3491 let _, l = source#getitem i in
3492 if l != curlevel then i else flow (i+incr)
3494 let active = flow m_active in
3495 let first = calcfirst m_first active in
3496 G.postRedisplay "outline updownlevel";
3497 {< m_active = active; m_first = first >}
3499 method private key1 key mask =
3500 let set1 active first qsearch =
3501 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3503 let search active pattern incr =
3504 let active = if active = -1 then m_first else active in
3505 let dosearch re =
3506 let rec loop n =
3507 if n >= 0 && n < source#getitemcount
3508 then (
3509 let s, _ = source#getitem n in
3511 (try ignore (Str.search_forward re s 0); true
3512 with Not_found -> false)
3513 then Some n
3514 else loop (n + incr)
3516 else None
3518 loop active
3521 let re = Str.regexp_case_fold pattern in
3522 dosearch re
3523 with Failure s ->
3524 state.text <- s;
3525 None
3527 let itemcount = source#getitemcount in
3528 let find start incr =
3529 let rec find i =
3530 if i = -1 || i = itemcount
3531 then -1
3532 else (
3533 if source#hasaction i
3534 then i
3535 else find (i + incr)
3538 find start
3540 let set active first =
3541 let first = bound first 0 (itemcount - fstate.maxrows) in
3542 state.text <- "";
3543 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3545 let navigate incr =
3546 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3547 let active, first =
3548 let incr1 = if incr > 0 then 1 else -1 in
3549 if isvisible m_first m_active
3550 then
3551 let next =
3552 let next = m_active + incr in
3553 let next =
3554 if next < 0 || next >= itemcount
3555 then -1
3556 else find next incr1
3558 if abs (m_active - next) > fstate.maxrows
3559 then -1
3560 else next
3562 if next = -1
3563 then
3564 let first = m_first + incr in
3565 let first = bound first 0 (itemcount - fstate.maxrows) in
3566 let next =
3567 let next = m_active + incr in
3568 let next = bound next 0 (itemcount - 1) in
3569 find next ~-incr1
3571 let active =
3572 if next = -1
3573 then m_active
3574 else (
3575 if isvisible first next
3576 then next
3577 else m_active
3580 active, first
3581 else
3582 let first = min next m_first in
3583 let first =
3584 if abs (next - first) > fstate.maxrows
3585 then first + incr
3586 else first
3588 next, first
3589 else
3590 let first = m_first + incr in
3591 let first = bound first 0 (itemcount - 1) in
3592 let active =
3593 let next = m_active + incr in
3594 let next = bound next 0 (itemcount - 1) in
3595 let next = find next incr1 in
3596 let active =
3597 if next = -1 || abs (m_active - first) > fstate.maxrows
3598 then (
3599 let active = if m_active = -1 then next else m_active in
3600 active
3602 else next
3604 if isvisible first active
3605 then active
3606 else -1
3608 active, first
3610 G.postRedisplay "listview navigate";
3611 set active first;
3613 match key with
3614 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3615 let incr = if key = 0x72 then -1 else 1 in
3616 let active, first =
3617 match search (m_active + incr) m_qsearch incr with
3618 | None ->
3619 state.text <- m_qsearch ^ " [not found]";
3620 m_active, m_first
3621 | Some active ->
3622 state.text <- m_qsearch;
3623 active, firstof m_first active
3625 G.postRedisplay "listview ctrl-r/s";
3626 set1 active first m_qsearch;
3628 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3629 if m_active >= 0 && m_active < source#getitemcount
3630 then (
3631 let s, _ = source#getitem m_active in
3632 selstring s;
3634 coe self
3636 | 0xff08 -> (* backspace *)
3637 if String.length m_qsearch = 0
3638 then coe self
3639 else (
3640 let qsearch = withoutlastutf8 m_qsearch in
3641 let len = String.length qsearch in
3642 if len = 0
3643 then (
3644 state.text <- "";
3645 G.postRedisplay "listview empty qsearch";
3646 set1 m_active m_first "";
3648 else
3649 let active, first =
3650 match search m_active qsearch ~-1 with
3651 | None ->
3652 state.text <- qsearch ^ " [not found]";
3653 m_active, m_first
3654 | Some active ->
3655 state.text <- qsearch;
3656 active, firstof m_first active
3658 G.postRedisplay "listview backspace qsearch";
3659 set1 active first qsearch
3662 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3663 let pattern = m_qsearch ^ toutf8 key in
3664 let active, first =
3665 match search m_active pattern 1 with
3666 | None ->
3667 state.text <- pattern ^ " [not found]";
3668 m_active, m_first
3669 | Some active ->
3670 state.text <- pattern;
3671 active, firstof m_first active
3673 G.postRedisplay "listview qsearch add";
3674 set1 active first pattern;
3676 | 0xff1b -> (* escape *)
3677 state.text <- "";
3678 if String.length m_qsearch = 0
3679 then (
3680 G.postRedisplay "list view escape";
3681 begin
3682 match
3683 source#exit (coe self) true m_active m_first m_pan m_qsearch
3684 with
3685 | None -> m_prev_uioh
3686 | Some uioh -> uioh
3689 else (
3690 G.postRedisplay "list view kill qsearch";
3691 source#setqsearch "";
3692 coe {< m_qsearch = "" >}
3695 | 0xff0d | 0xff8d -> (* (kp) enter *)
3696 state.text <- "";
3697 let self = {< m_qsearch = "" >} in
3698 source#setqsearch "";
3699 let opt =
3700 G.postRedisplay "listview enter";
3701 if m_active >= 0 && m_active < source#getitemcount
3702 then (
3703 source#exit (coe self) false m_active m_first m_pan "";
3705 else (
3706 source#exit (coe self) true m_active m_first m_pan "";
3709 begin match opt with
3710 | None -> m_prev_uioh
3711 | Some uioh -> uioh
3714 | 0xff9f | 0xffff -> (* (kp) delete *)
3715 coe self
3717 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3718 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3719 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3720 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3722 | 0xff53 | 0xff98 -> (* (kp) right *)
3723 state.text <- "";
3724 G.postRedisplay "listview right";
3725 coe {< m_pan = m_pan - 1 >}
3727 | 0xff51 | 0xff96 -> (* (kp) left *)
3728 state.text <- "";
3729 G.postRedisplay "listview left";
3730 coe {< m_pan = m_pan + 1 >}
3732 | 0xff50 | 0xff95 -> (* (kp) home *)
3733 let active = find 0 1 in
3734 G.postRedisplay "listview home";
3735 set active 0;
3737 | 0xff57 | 0xff9c -> (* (kp) end *)
3738 let first = max 0 (itemcount - fstate.maxrows) in
3739 let active = find (itemcount - 1) ~-1 in
3740 G.postRedisplay "listview end";
3741 set active first;
3743 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3744 coe self
3746 | _ ->
3747 dolog "listview unknown key %#x" key; coe self
3749 method key key mask =
3750 match state.mode with
3751 | Textentry te -> textentrykeyboard key mask te; coe self
3752 | _ -> self#key1 key mask
3754 method button button down x y _ =
3755 let opt =
3756 match button with
3757 | 1 when x > state.winw - conf.scrollbw ->
3758 G.postRedisplay "listview scroll";
3759 if down
3760 then
3761 let _, position, sh = self#scrollph in
3762 if y > truncate position && y < truncate (position +. sh)
3763 then (
3764 state.mstate <- Mscrolly;
3765 Some (coe self)
3767 else
3768 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3769 let first = truncate (s *. float source#getitemcount) in
3770 let first = min source#getitemcount first in
3771 Some (coe {< m_first = first; m_active = first >})
3772 else (
3773 state.mstate <- Mnone;
3774 Some (coe self);
3776 | 1 when not down ->
3777 begin match self#elemunder y with
3778 | Some n ->
3779 G.postRedisplay "listview click";
3780 source#exit
3781 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3782 | _ ->
3783 Some (coe self)
3785 | n when (n == 4 || n == 5) && not down ->
3786 let len = source#getitemcount in
3787 let first =
3788 if n = 5 && m_first + fstate.maxrows >= len
3789 then
3790 m_first
3791 else
3792 let first = m_first + (if n == 4 then -1 else 1) in
3793 bound first 0 (len - 1)
3795 G.postRedisplay "listview wheel";
3796 Some (coe {< m_first = first >})
3797 | n when (n = 6 || n = 7) && not down ->
3798 let inc = m_first + (if n = 7 then -1 else 1) in
3799 G.postRedisplay "listview hwheel";
3800 Some (coe {< m_pan = m_pan + inc >})
3801 | _ ->
3802 Some (coe self)
3804 match opt with
3805 | None -> m_prev_uioh
3806 | Some uioh -> uioh
3808 method motion _ y =
3809 match state.mstate with
3810 | Mscrolly ->
3811 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3812 let first = truncate (s *. float source#getitemcount) in
3813 let first = min source#getitemcount first in
3814 G.postRedisplay "listview motion";
3815 coe {< m_first = first; m_active = first >}
3816 | _ -> coe self
3818 method pmotion x y =
3819 if x < state.winw - conf.scrollbw
3820 then
3821 let n =
3822 match self#elemunder y with
3823 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3824 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3826 let o =
3827 if n != m_active
3828 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3829 else self
3831 coe o
3832 else (
3833 Wsi.setcursor Wsi.CURSOR_INHERIT;
3834 coe self
3837 method infochanged _ = ()
3839 method scrollpw = (0, 0.0, 0.0)
3840 method scrollph =
3841 let nfs = fstate.fontsize + 1 in
3842 let y = m_first * nfs in
3843 let itemcount = source#getitemcount in
3844 let maxi = max 0 (itemcount - fstate.maxrows) in
3845 let maxy = maxi * nfs in
3846 let p, h = scrollph y maxy in
3847 conf.scrollbw, p, h
3849 method modehash = modehash
3850 method eformsgs = false
3851 end;;
3853 class outlinelistview ~source =
3854 object (self)
3855 inherit listview
3856 ~source:(source :> lvsource)
3857 ~trusted:false
3858 ~modehash:(findkeyhash conf "outline")
3859 as super
3861 method key key mask =
3862 let calcfirst first active =
3863 if active > first
3864 then
3865 let rows = active - first in
3866 let maxrows =
3867 if String.length state.text = 0
3868 then fstate.maxrows
3869 else fstate.maxrows - 2
3871 if rows > maxrows then active - maxrows else first
3872 else active
3874 let navigate incr =
3875 let active = m_active + incr in
3876 let active = bound active 0 (source#getitemcount - 1) in
3877 let first = calcfirst m_first active in
3878 G.postRedisplay "outline navigate";
3879 coe {< m_active = active; m_first = first >}
3881 let ctrl = Wsi.withctrl mask in
3882 match key with
3883 | 110 when ctrl -> (* ctrl-n *)
3884 source#narrow m_qsearch;
3885 G.postRedisplay "outline ctrl-n";
3886 coe {< m_first = 0; m_active = 0 >}
3888 | 117 when ctrl -> (* ctrl-u *)
3889 source#denarrow;
3890 G.postRedisplay "outline ctrl-u";
3891 state.text <- "";
3892 coe {< m_first = 0; m_active = 0 >}
3894 | 108 when ctrl -> (* ctrl-l *)
3895 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3896 G.postRedisplay "outline ctrl-l";
3897 coe {< m_first = first >}
3899 | 0xff9f | 0xffff -> (* (kp) delete *)
3900 source#remove m_active;
3901 G.postRedisplay "outline delete";
3902 let active = max 0 (m_active-1) in
3903 coe {< m_first = firstof m_first active;
3904 m_active = active >}
3906 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3907 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3908 | 0xff55 | 0xff9a -> (* (kp) prior *)
3909 navigate ~-(fstate.maxrows)
3910 | 0xff56 | 0xff9b -> (* (kp) next *)
3911 navigate fstate.maxrows
3913 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3914 let o =
3915 if ctrl
3916 then (
3917 G.postRedisplay "outline ctrl right";
3918 {< m_pan = m_pan + 1 >}
3920 else self#updownlevel 1
3922 coe o
3924 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3925 let o =
3926 if ctrl
3927 then (
3928 G.postRedisplay "outline ctrl left";
3929 {< m_pan = m_pan - 1 >}
3931 else self#updownlevel ~-1
3933 coe o
3935 | 0xff50 | 0xff95 -> (* (kp) home *)
3936 G.postRedisplay "outline home";
3937 coe {< m_first = 0; m_active = 0 >}
3939 | 0xff57 | 0xff9c -> (* (kp) end *)
3940 let active = source#getitemcount - 1 in
3941 let first = max 0 (active - fstate.maxrows) in
3942 G.postRedisplay "outline end";
3943 coe {< m_active = active; m_first = first >}
3945 | _ -> super#key key mask
3948 let outlinesource usebookmarks =
3949 let empty = [||] in
3950 (object
3951 inherit lvsourcebase
3952 val mutable m_items = empty
3953 val mutable m_orig_items = empty
3954 val mutable m_prev_items = empty
3955 val mutable m_narrow_pattern = ""
3956 val mutable m_hadremovals = false
3958 method getitemcount =
3959 Array.length m_items + (if m_hadremovals then 1 else 0)
3961 method getitem n =
3962 if n == Array.length m_items && m_hadremovals
3963 then
3964 ("[Confirm removal]", 0)
3965 else
3966 let s, n, _ = m_items.(n) in
3967 (s, n)
3969 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3970 ignore (uioh, first, qsearch);
3971 let confrimremoval = m_hadremovals && active = Array.length m_items in
3972 let items =
3973 if String.length m_narrow_pattern = 0
3974 then m_orig_items
3975 else m_items
3977 if not cancel
3978 then (
3979 if not confrimremoval
3980 then(
3981 let _, _, anchor = m_items.(active) in
3982 gotoghyll (getanchory anchor);
3983 m_items <- items;
3985 else (
3986 state.bookmarks <- Array.to_list m_items;
3987 m_orig_items <- m_items;
3990 else m_items <- items;
3991 m_pan <- pan;
3992 None
3994 method hasaction _ = true
3996 method greetmsg =
3997 if Array.length m_items != Array.length m_orig_items
3998 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3999 else ""
4001 method narrow pattern =
4002 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4003 match reopt with
4004 | None -> ()
4005 | Some re ->
4006 let rec loop accu n =
4007 if n = -1
4008 then (
4009 m_narrow_pattern <- pattern;
4010 m_items <- Array.of_list accu
4012 else
4013 let (s, _, _) as o = m_items.(n) in
4014 let accu =
4015 if (try ignore (Str.search_forward re s 0); true
4016 with Not_found -> false)
4017 then o :: accu
4018 else accu
4020 loop accu (n-1)
4022 loop [] (Array.length m_items - 1)
4024 method denarrow =
4025 m_orig_items <- (
4026 if usebookmarks
4027 then Array.of_list state.bookmarks
4028 else state.outlines
4030 m_items <- m_orig_items
4032 method remove m =
4033 if usebookmarks
4034 then
4035 if m >= 0 && m < Array.length m_items
4036 then (
4037 m_hadremovals <- true;
4038 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4039 let n = if n >= m then n+1 else n in
4040 m_items.(n)
4044 method reset anchor items =
4045 m_hadremovals <- false;
4046 if m_orig_items == empty || m_prev_items != items
4047 then (
4048 m_orig_items <- items;
4049 if String.length m_narrow_pattern = 0
4050 then m_items <- items;
4052 m_prev_items <- items;
4053 let rely = getanchory anchor in
4054 let active =
4055 let rec loop n best bestd =
4056 if n = Array.length m_items
4057 then best
4058 else
4059 let (_, _, anchor) = m_items.(n) in
4060 let orely = getanchory anchor in
4061 let d = abs (orely - rely) in
4062 if d < bestd
4063 then loop (n+1) n d
4064 else loop (n+1) best bestd
4066 loop 0 ~-1 max_int
4068 m_active <- active;
4069 m_first <- firstof m_first active
4070 end)
4073 let enterselector usebookmarks =
4074 let source = outlinesource usebookmarks in
4075 fun errmsg ->
4076 let outlines =
4077 if usebookmarks
4078 then Array.of_list state.bookmarks
4079 else state.outlines
4081 if Array.length outlines = 0
4082 then (
4083 showtext ' ' errmsg;
4085 else (
4086 state.text <- source#greetmsg;
4087 Wsi.setcursor Wsi.CURSOR_INHERIT;
4088 let anchor = getanchor () in
4089 source#reset anchor outlines;
4090 state.uioh <- coe (new outlinelistview ~source);
4091 G.postRedisplay "enter selector";
4095 let enteroutlinemode =
4096 let f = enterselector false in
4097 fun ()-> f "Document has no outline";
4100 let enterbookmarkmode =
4101 let f = enterselector true in
4102 fun () -> f "Document has no bookmarks (yet)";
4105 let color_of_string s =
4106 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4107 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4111 let color_to_string (r, g, b) =
4112 let r = truncate (r *. 256.0)
4113 and g = truncate (g *. 256.0)
4114 and b = truncate (b *. 256.0) in
4115 Printf.sprintf "%d/%d/%d" r g b
4118 let irect_of_string s =
4119 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4122 let irect_to_string (x0,y0,x1,y1) =
4123 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4126 let makecheckers () =
4127 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4128 following to say:
4129 converted by Issac Trotts. July 25, 2002 *)
4130 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4131 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4132 let id = GlTex.gen_texture () in
4133 GlTex.bind_texture `texture_2d id;
4134 GlPix.store (`unpack_alignment 1);
4135 GlTex.image2d image;
4136 List.iter (GlTex.parameter ~target:`texture_2d)
4137 [ `mag_filter `nearest; `min_filter `nearest ];
4141 let setcheckers enabled =
4142 match state.texid with
4143 | None ->
4144 if enabled then state.texid <- Some (makecheckers ())
4146 | Some texid ->
4147 if not enabled
4148 then (
4149 GlTex.delete_texture texid;
4150 state.texid <- None;
4154 let int_of_string_with_suffix s =
4155 let l = String.length s in
4156 let s1, shift =
4157 if l > 1
4158 then
4159 let suffix = Char.lowercase s.[l-1] in
4160 match suffix with
4161 | 'k' -> String.sub s 0 (l-1), 10
4162 | 'm' -> String.sub s 0 (l-1), 20
4163 | 'g' -> String.sub s 0 (l-1), 30
4164 | _ -> s, 0
4165 else s, 0
4167 let n = int_of_string s1 in
4168 let m = n lsl shift in
4169 if m < 0 || m < n
4170 then raise (Failure "value too large")
4171 else m
4174 let string_with_suffix_of_int n =
4175 if n = 0
4176 then "0"
4177 else
4178 let n, s =
4179 if n land ((1 lsl 30) - 1) = 0
4180 then n lsr 30, "G"
4181 else (
4182 if n land ((1 lsl 20) - 1) = 0
4183 then n lsr 20, "M"
4184 else (
4185 if n land ((1 lsl 10) - 1) = 0
4186 then n lsr 10, "K"
4187 else n, ""
4191 let rec loop s n =
4192 let h = n mod 1000 in
4193 let n = n / 1000 in
4194 if n = 0
4195 then string_of_int h ^ s
4196 else (
4197 let s = Printf.sprintf "_%03d%s" h s in
4198 loop s n
4201 loop "" n ^ s;
4204 let defghyllscroll = (40, 8, 32);;
4205 let ghyllscroll_of_string s =
4206 let (n, a, b) as nab =
4207 if s = "default"
4208 then defghyllscroll
4209 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4211 if n <= a || n <= b || a >= b
4212 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4213 nab;
4216 let ghyllscroll_to_string ((n, a, b) as nab) =
4217 if nab = defghyllscroll
4218 then "default"
4219 else Printf.sprintf "%d,%d,%d" n a b;
4222 let describe_location () =
4223 let fn = page_of_y state.y in
4224 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4225 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4226 let percent =
4227 if maxy <= 0
4228 then 100.
4229 else (100. *. (float state.y /. float maxy))
4231 if fn = ln
4232 then
4233 Printf.sprintf "page %d of %d [%.2f%%]"
4234 (fn+1) state.pagecount percent
4235 else
4236 Printf.sprintf
4237 "pages %d-%d of %d [%.2f%%]"
4238 (fn+1) (ln+1) state.pagecount percent
4241 let setpresentationmode v =
4242 let n = page_of_y state.y in
4243 state.anchor <- (n, 0.0, 1.0);
4244 conf.presentation <- v;
4245 if conf.fitmodel = FitPage
4246 then reqlayout conf.angle conf.fitmodel;
4247 represent ();
4250 let enterinfomode =
4251 let btos b = if b then "\xe2\x88\x9a" else "" in
4252 let showextended = ref false in
4253 let leave mode = function
4254 | Confirm -> state.mode <- mode
4255 | Cancel -> state.mode <- mode in
4256 let src =
4257 (object
4258 val mutable m_first_time = true
4259 val mutable m_l = []
4260 val mutable m_a = [||]
4261 val mutable m_prev_uioh = nouioh
4262 val mutable m_prev_mode = View
4264 inherit lvsourcebase
4266 method reset prev_mode prev_uioh =
4267 m_a <- Array.of_list (List.rev m_l);
4268 m_l <- [];
4269 m_prev_mode <- prev_mode;
4270 m_prev_uioh <- prev_uioh;
4271 if m_first_time
4272 then (
4273 let rec loop n =
4274 if n >= Array.length m_a
4275 then ()
4276 else
4277 match m_a.(n) with
4278 | _, _, _, Action _ -> m_active <- n
4279 | _ -> loop (n+1)
4281 loop 0;
4282 m_first_time <- false;
4285 method int name get set =
4286 m_l <-
4287 (name, `int get, 1, Action (
4288 fun u ->
4289 let ondone s =
4290 try set (int_of_string s)
4291 with exn ->
4292 state.text <- Printf.sprintf "bad integer `%s': %s"
4293 s (exntos exn)
4295 state.text <- "";
4296 let te = name ^ ": ", "", None, intentry, ondone, true in
4297 state.mode <- Textentry (te, leave m_prev_mode);
4299 )) :: m_l
4301 method int_with_suffix name get set =
4302 m_l <-
4303 (name, `intws get, 1, Action (
4304 fun u ->
4305 let ondone s =
4306 try set (int_of_string_with_suffix s)
4307 with exn ->
4308 state.text <- Printf.sprintf "bad integer `%s': %s"
4309 s (exntos exn)
4311 state.text <- "";
4312 let te =
4313 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4315 state.mode <- Textentry (te, leave m_prev_mode);
4317 )) :: m_l
4319 method bool ?(offset=1) ?(btos=btos) name get set =
4320 m_l <-
4321 (name, `bool (btos, get), offset, Action (
4322 fun u ->
4323 let v = get () in
4324 set (not v);
4326 )) :: m_l
4328 method color name get set =
4329 m_l <-
4330 (name, `color get, 1, Action (
4331 fun u ->
4332 let invalid = (nan, nan, nan) in
4333 let ondone s =
4334 let c =
4335 try color_of_string s
4336 with exn ->
4337 state.text <- Printf.sprintf "bad color `%s': %s"
4338 s (exntos exn);
4339 invalid
4341 if c <> invalid
4342 then set c;
4344 let te = name ^ ": ", "", None, textentry, ondone, true in
4345 state.text <- color_to_string (get ());
4346 state.mode <- Textentry (te, leave m_prev_mode);
4348 )) :: m_l
4350 method string name get set =
4351 m_l <-
4352 (name, `string get, 1, Action (
4353 fun u ->
4354 let ondone s = set s in
4355 let te = name ^ ": ", "", None, textentry, ondone, true in
4356 state.mode <- Textentry (te, leave m_prev_mode);
4358 )) :: m_l
4360 method colorspace name get set =
4361 m_l <-
4362 (name, `string get, 1, Action (
4363 fun _ ->
4364 let source =
4365 (object
4366 inherit lvsourcebase
4368 initializer
4369 m_active <- CSTE.to_int conf.colorspace;
4370 m_first <- 0;
4372 method getitemcount =
4373 Array.length CSTE.names
4374 method getitem n =
4375 (CSTE.names.(n), 0)
4376 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4377 ignore (uioh, first, pan, qsearch);
4378 if not cancel then set active;
4379 None
4380 method hasaction _ = true
4381 end)
4383 state.text <- "";
4384 let modehash = findkeyhash conf "info" in
4385 coe (new listview ~source ~trusted:true ~modehash)
4386 )) :: m_l
4388 method paxmark name get set =
4389 m_l <-
4390 (name, `string get, 1, Action (
4391 fun _ ->
4392 let source =
4393 (object
4394 inherit lvsourcebase
4396 initializer
4397 m_active <- MTE.to_int conf.paxmark;
4398 m_first <- 0;
4400 method getitemcount = Array.length MTE.names
4401 method getitem n = (MTE.names.(n), 0)
4402 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4403 ignore (uioh, first, pan, qsearch);
4404 if not cancel then set active;
4405 None
4406 method hasaction _ = true
4407 end)
4409 state.text <- "";
4410 let modehash = findkeyhash conf "info" in
4411 coe (new listview ~source ~trusted:true ~modehash)
4412 )) :: m_l
4414 method fitmodel name get set =
4415 m_l <-
4416 (name, `string get, 1, Action (
4417 fun _ ->
4418 let source =
4419 (object
4420 inherit lvsourcebase
4422 initializer
4423 m_active <- FMTE.to_int conf.fitmodel;
4424 m_first <- 0;
4426 method getitemcount = Array.length FMTE.names
4427 method getitem n = (FMTE.names.(n), 0)
4428 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4429 ignore (uioh, first, pan, qsearch);
4430 if not cancel then set active;
4431 None
4432 method hasaction _ = true
4433 end)
4435 state.text <- "";
4436 let modehash = findkeyhash conf "info" in
4437 coe (new listview ~source ~trusted:true ~modehash)
4438 )) :: m_l
4440 method caption s offset =
4441 m_l <- (s, `empty, offset, Noaction) :: m_l
4443 method caption2 s f offset =
4444 m_l <- (s, `string f, offset, Noaction) :: m_l
4446 method getitemcount = Array.length m_a
4448 method getitem n =
4449 let tostr = function
4450 | `int f -> string_of_int (f ())
4451 | `intws f -> string_with_suffix_of_int (f ())
4452 | `string f -> f ()
4453 | `color f -> color_to_string (f ())
4454 | `bool (btos, f) -> btos (f ())
4455 | `empty -> ""
4457 let name, t, offset, _ = m_a.(n) in
4458 ((let s = tostr t in
4459 if String.length s > 0
4460 then Printf.sprintf "%s\t%s" name s
4461 else name),
4462 offset)
4464 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4465 let uiohopt =
4466 if not cancel
4467 then (
4468 m_qsearch <- qsearch;
4469 let uioh =
4470 match m_a.(active) with
4471 | _, _, _, Action f -> f uioh
4472 | _ -> uioh
4474 Some uioh
4476 else None
4478 m_active <- active;
4479 m_first <- first;
4480 m_pan <- pan;
4481 uiohopt
4483 method hasaction n =
4484 match m_a.(n) with
4485 | _, _, _, Action _ -> true
4486 | _ -> false
4487 end)
4489 let rec fillsrc prevmode prevuioh =
4490 let sep () = src#caption "" 0 in
4491 let colorp name get set =
4492 src#string name
4493 (fun () -> color_to_string (get ()))
4494 (fun v ->
4496 let c = color_of_string v in
4497 set c
4498 with exn ->
4499 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4502 let oldmode = state.mode in
4503 let birdseye = isbirdseye state.mode in
4505 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4507 src#bool "presentation mode"
4508 (fun () -> conf.presentation)
4509 (fun v -> setpresentationmode v);
4511 src#bool "ignore case in searches"
4512 (fun () -> conf.icase)
4513 (fun v -> conf.icase <- v);
4515 src#bool "preload"
4516 (fun () -> conf.preload)
4517 (fun v -> conf.preload <- v);
4519 src#bool "highlight links"
4520 (fun () -> conf.hlinks)
4521 (fun v -> conf.hlinks <- v);
4523 src#bool "under info"
4524 (fun () -> conf.underinfo)
4525 (fun v -> conf.underinfo <- v);
4527 src#bool "persistent bookmarks"
4528 (fun () -> conf.savebmarks)
4529 (fun v -> conf.savebmarks <- v);
4531 src#fitmodel "fit model"
4532 (fun () -> FMTE.to_string conf.fitmodel)
4533 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4535 src#bool "trim margins"
4536 (fun () -> conf.trimmargins)
4537 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4539 src#bool "persistent location"
4540 (fun () -> conf.jumpback)
4541 (fun v -> conf.jumpback <- v);
4543 sep ();
4544 src#int "inter-page space"
4545 (fun () -> conf.interpagespace)
4546 (fun n ->
4547 conf.interpagespace <- n;
4548 docolumns conf.columns;
4549 let pageno, py =
4550 match state.layout with
4551 | [] -> 0, 0
4552 | l :: _ ->
4553 l.pageno, l.pagey
4555 state.maxy <- calcheight ();
4556 let y = getpagey pageno in
4557 gotoy (y + py)
4560 src#int "page bias"
4561 (fun () -> conf.pagebias)
4562 (fun v -> conf.pagebias <- v);
4564 src#int "scroll step"
4565 (fun () -> conf.scrollstep)
4566 (fun n -> conf.scrollstep <- n);
4568 src#int "horizontal scroll step"
4569 (fun () -> conf.hscrollstep)
4570 (fun v -> conf.hscrollstep <- v);
4572 src#int "auto scroll step"
4573 (fun () ->
4574 match state.autoscroll with
4575 | Some step -> step
4576 | _ -> conf.autoscrollstep)
4577 (fun n ->
4578 if state.autoscroll <> None
4579 then state.autoscroll <- Some n;
4580 conf.autoscrollstep <- n);
4582 src#int "zoom"
4583 (fun () -> truncate (conf.zoom *. 100.))
4584 (fun v -> setzoom ((float v) /. 100.));
4586 src#int "rotation"
4587 (fun () -> conf.angle)
4588 (fun v -> reqlayout v conf.fitmodel);
4590 src#int "scroll bar width"
4591 (fun () -> conf.scrollbw)
4592 (fun v ->
4593 conf.scrollbw <- v;
4594 reshape state.winw state.winh;
4597 src#int "scroll handle height"
4598 (fun () -> conf.scrollh)
4599 (fun v -> conf.scrollh <- v;);
4601 src#int "thumbnail width"
4602 (fun () -> conf.thumbw)
4603 (fun v ->
4604 conf.thumbw <- min 4096 v;
4605 match oldmode with
4606 | Birdseye beye ->
4607 leavebirdseye beye false;
4608 enterbirdseye ()
4609 | _ -> ()
4612 let mode = state.mode in
4613 src#string "columns"
4614 (fun () ->
4615 match conf.columns with
4616 | Csingle _ -> "1"
4617 | Cmulti (multi, _) -> multicolumns_to_string multi
4618 | Csplit (count, _) -> "-" ^ string_of_int count
4620 (fun v ->
4621 let n, a, b = multicolumns_of_string v in
4622 setcolumns mode n a b);
4624 sep ();
4625 src#caption "Pixmap cache" 0;
4626 src#int_with_suffix "size (advisory)"
4627 (fun () -> conf.memlimit)
4628 (fun v -> conf.memlimit <- v);
4630 src#caption2 "used"
4631 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4632 (string_with_suffix_of_int state.memused)
4633 (Hashtbl.length state.tilemap)) 1;
4635 sep ();
4636 src#caption "Layout" 0;
4637 src#caption2 "Dimension"
4638 (fun () ->
4639 Printf.sprintf "%dx%d (virtual %dx%d)"
4640 state.winw state.winh
4641 state.w state.maxy)
4643 if conf.debug
4644 then
4645 src#caption2 "Position" (fun () ->
4646 Printf.sprintf "%dx%d" state.x state.y
4648 else
4649 src#caption2 "Position" (fun () -> describe_location ()) 1
4652 sep ();
4653 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4654 "Save these parameters as global defaults at exit"
4655 (fun () -> conf.bedefault)
4656 (fun v -> conf.bedefault <- v)
4659 sep ();
4660 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4661 src#bool ~offset:0 ~btos "Extended parameters"
4662 (fun () -> !showextended)
4663 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4664 if !showextended
4665 then (
4666 src#bool "checkers"
4667 (fun () -> conf.checkers)
4668 (fun v -> conf.checkers <- v; setcheckers v);
4669 src#bool "update cursor"
4670 (fun () -> conf.updatecurs)
4671 (fun v -> conf.updatecurs <- v);
4672 src#bool "verbose"
4673 (fun () -> conf.verbose)
4674 (fun v -> conf.verbose <- v);
4675 src#bool "invert colors"
4676 (fun () -> conf.invert)
4677 (fun v -> conf.invert <- v);
4678 src#bool "max fit"
4679 (fun () -> conf.maxhfit)
4680 (fun v -> conf.maxhfit <- v);
4681 src#bool "redirect stderr"
4682 (fun () -> conf.redirectstderr)
4683 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4684 src#bool "pax mode"
4685 (fun () -> conf.pax != None)
4686 (fun v ->
4687 if v
4688 then conf.pax <- Some (ref (now (), 0, 0))
4689 else conf.pax <- None);
4690 src#string "uri launcher"
4691 (fun () -> conf.urilauncher)
4692 (fun v -> conf.urilauncher <- v);
4693 src#string "path launcher"
4694 (fun () -> conf.pathlauncher)
4695 (fun v -> conf.pathlauncher <- v);
4696 src#string "tile size"
4697 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4698 (fun v ->
4700 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4701 conf.tilew <- max 64 w;
4702 conf.tileh <- max 64 h;
4703 flushtiles ();
4704 with exn ->
4705 state.text <- Printf.sprintf "bad tile size `%s': %s"
4706 v (exntos exn)
4708 src#int "texture count"
4709 (fun () -> conf.texcount)
4710 (fun v ->
4711 if realloctexts v
4712 then conf.texcount <- v
4713 else showtext '!' " Failed to set texture count please retry later"
4715 src#int "slice height"
4716 (fun () -> conf.sliceheight)
4717 (fun v ->
4718 conf.sliceheight <- v;
4719 wcmd "sliceh %d" conf.sliceheight;
4721 src#int "anti-aliasing level"
4722 (fun () -> conf.aalevel)
4723 (fun v ->
4724 conf.aalevel <- bound v 0 8;
4725 state.anchor <- getanchor ();
4726 opendoc state.path state.password;
4728 src#string "page scroll scaling factor"
4729 (fun () -> string_of_float conf.pgscale)
4730 (fun v ->
4732 let s = float_of_string v in
4733 conf.pgscale <- s
4734 with exn ->
4735 state.text <- Printf.sprintf
4736 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4739 src#int "ui font size"
4740 (fun () -> fstate.fontsize)
4741 (fun v -> setfontsize (bound v 5 100));
4742 src#int "hint font size"
4743 (fun () -> conf.hfsize)
4744 (fun v -> conf.hfsize <- bound v 5 100);
4745 colorp "background color"
4746 (fun () -> conf.bgcolor)
4747 (fun v -> conf.bgcolor <- v);
4748 src#bool "crop hack"
4749 (fun () -> conf.crophack)
4750 (fun v -> conf.crophack <- v);
4751 src#string "trim fuzz"
4752 (fun () -> irect_to_string conf.trimfuzz)
4753 (fun v ->
4755 conf.trimfuzz <- irect_of_string v;
4756 if conf.trimmargins
4757 then settrim true conf.trimfuzz;
4758 with exn ->
4759 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4761 src#string "throttle"
4762 (fun () ->
4763 match conf.maxwait with
4764 | None -> "show place holder if page is not ready"
4765 | Some time ->
4766 if time = infinity
4767 then "wait for page to fully render"
4768 else
4769 "wait " ^ string_of_float time
4770 ^ " seconds before showing placeholder"
4772 (fun v ->
4774 let f = float_of_string v in
4775 if f <= 0.0
4776 then conf.maxwait <- None
4777 else conf.maxwait <- Some f
4778 with exn ->
4779 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4781 src#string "ghyll scroll"
4782 (fun () ->
4783 match conf.ghyllscroll with
4784 | None -> ""
4785 | Some nab -> ghyllscroll_to_string nab
4787 (fun v ->
4789 let gs =
4790 if String.length v = 0
4791 then None
4792 else Some (ghyllscroll_of_string v)
4794 conf.ghyllscroll <- gs
4795 with exn ->
4796 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4798 src#string "selection command"
4799 (fun () -> conf.selcmd)
4800 (fun v -> conf.selcmd <- v);
4801 src#string "synctex command"
4802 (fun () -> conf.stcmd)
4803 (fun v -> conf.stcmd <- v);
4804 src#string "pax command"
4805 (fun () -> conf.paxcmd)
4806 (fun v -> conf.paxcmd <- v);
4807 src#colorspace "color space"
4808 (fun () -> CSTE.to_string conf.colorspace)
4809 (fun v ->
4810 conf.colorspace <- CSTE.of_int v;
4811 wcmd "cs %d" v;
4812 load state.layout;
4814 src#paxmark "pax mark method"
4815 (fun () -> MTE.to_string conf.paxmark)
4816 (fun v -> conf.paxmark <- MTE.of_int v);
4817 if pbousable ()
4818 then
4819 src#bool "use PBO"
4820 (fun () -> conf.usepbo)
4821 (fun v -> conf.usepbo <- v);
4822 src#bool "mouse wheel scrolls pages"
4823 (fun () -> conf.wheelbypage)
4824 (fun v -> conf.wheelbypage <- v);
4825 src#bool "open remote links in a new instance"
4826 (fun () -> conf.riani)
4827 (fun v -> conf.riani <- v);
4830 sep ();
4831 src#caption "Document" 0;
4832 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4833 src#caption2 "Pages"
4834 (fun () -> string_of_int state.pagecount) 1;
4835 src#caption2 "Dimensions"
4836 (fun () -> string_of_int (List.length state.pdims)) 1;
4837 if conf.trimmargins
4838 then (
4839 sep ();
4840 src#caption "Trimmed margins" 0;
4841 src#caption2 "Dimensions"
4842 (fun () -> string_of_int (List.length state.pdims)) 1;
4845 sep ();
4846 src#caption "OpenGL" 0;
4847 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4848 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4850 sep ();
4851 src#caption "Location" 0;
4852 if String.length state.origin > 0
4853 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4854 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4856 src#reset prevmode prevuioh;
4858 fun () ->
4859 state.text <- "";
4860 let prevmode = state.mode
4861 and prevuioh = state.uioh in
4862 fillsrc prevmode prevuioh;
4863 let source = (src :> lvsource) in
4864 let modehash = findkeyhash conf "info" in
4865 state.uioh <- coe (object (self)
4866 inherit listview ~source ~trusted:true ~modehash as super
4867 val mutable m_prevmemused = 0
4868 method infochanged = function
4869 | Memused ->
4870 if m_prevmemused != state.memused
4871 then (
4872 m_prevmemused <- state.memused;
4873 G.postRedisplay "memusedchanged";
4875 | Pdim -> G.postRedisplay "pdimchanged"
4876 | Docinfo -> fillsrc prevmode prevuioh
4878 method key key mask =
4879 if not (Wsi.withctrl mask)
4880 then
4881 match key with
4882 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4883 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4884 | _ -> super#key key mask
4885 else super#key key mask
4886 end);
4887 G.postRedisplay "info";
4890 let enterhelpmode =
4891 let source =
4892 (object
4893 inherit lvsourcebase
4894 method getitemcount = Array.length state.help
4895 method getitem n =
4896 let s, l, _ = state.help.(n) in
4897 (s, l)
4899 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4900 let optuioh =
4901 if not cancel
4902 then (
4903 m_qsearch <- qsearch;
4904 match state.help.(active) with
4905 | _, _, Action f -> Some (f uioh)
4906 | _ -> Some (uioh)
4908 else None
4910 m_active <- active;
4911 m_first <- first;
4912 m_pan <- pan;
4913 optuioh
4915 method hasaction n =
4916 match state.help.(n) with
4917 | _, _, Action _ -> true
4918 | _ -> false
4920 initializer
4921 m_active <- -1
4922 end)
4923 in fun () ->
4924 let modehash = findkeyhash conf "help" in
4925 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4926 G.postRedisplay "help";
4929 let entermsgsmode =
4930 let msgsource =
4931 let re = Str.regexp "[\r\n]" in
4932 (object
4933 inherit lvsourcebase
4934 val mutable m_items = [||]
4936 method getitemcount = 1 + Array.length m_items
4938 method getitem n =
4939 if n = 0
4940 then "[Clear]", 0
4941 else m_items.(n-1), 0
4943 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4944 ignore uioh;
4945 if not cancel
4946 then (
4947 if active = 0
4948 then Buffer.clear state.errmsgs;
4949 m_qsearch <- qsearch;
4951 m_active <- active;
4952 m_first <- first;
4953 m_pan <- pan;
4954 None
4956 method hasaction n =
4957 n = 0
4959 method reset =
4960 state.newerrmsgs <- false;
4961 let l = Str.split re (Buffer.contents state.errmsgs) in
4962 m_items <- Array.of_list l
4964 initializer
4965 m_active <- 0
4966 end)
4967 in fun () ->
4968 state.text <- "";
4969 msgsource#reset;
4970 let source = (msgsource :> lvsource) in
4971 let modehash = findkeyhash conf "listview" in
4972 state.uioh <- coe (object
4973 inherit listview ~source ~trusted:false ~modehash as super
4974 method display =
4975 if state.newerrmsgs
4976 then msgsource#reset;
4977 super#display
4978 end);
4979 G.postRedisplay "msgs";
4982 let quickbookmark ?title () =
4983 match state.layout with
4984 | [] -> ()
4985 | l :: _ ->
4986 let title =
4987 match title with
4988 | None ->
4989 let sec = Unix.gettimeofday () in
4990 let tm = Unix.localtime sec in
4991 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4992 (l.pageno+1)
4993 tm.Unix.tm_mday
4994 tm.Unix.tm_mon
4995 (tm.Unix.tm_year + 1900)
4996 tm.Unix.tm_hour
4997 tm.Unix.tm_min
4998 | Some title -> title
5000 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
5003 let setautoscrollspeed step goingdown =
5004 let incr = max 1 ((abs step) / 2) in
5005 let incr = if goingdown then incr else -incr in
5006 let astep = step + incr in
5007 state.autoscroll <- Some astep;
5010 let gotounder = function
5011 | Ulinkgoto (pageno, top) ->
5012 if pageno >= 0
5013 then (
5014 addnav ();
5015 gotopage1 pageno top;
5018 | Ulinkuri s ->
5019 gotouri s
5021 | Uremote (filename, pageno) ->
5022 let path =
5023 if String.length filename > 0
5024 then
5025 if Filename.is_relative filename
5026 then
5027 let dir = Filename.dirname state.path in
5028 let dir =
5029 if Filename.is_implicit dir
5030 then Filename.concat (Sys.getcwd ()) dir
5031 else dir
5033 Filename.concat dir filename
5034 else filename
5035 else ""
5037 let path =
5038 if Sys.file_exists path
5039 then path
5040 else ""
5042 if String.length path > 0
5043 then (
5044 if conf.riani
5045 then
5046 let command = !selfexec ^ " " ^ path in
5047 try popen command []
5048 with exn ->
5049 Printf.eprintf
5050 "failed to execute `%s': %s\n" command (exntos exn);
5051 flush stderr;
5052 else
5053 let anchor = getanchor () in
5054 let ranchor = state.path, state.password, anchor, state.origin in
5055 state.origin <- "";
5056 state.anchor <- (pageno, 0.0, 0.0);
5057 state.ranchors <- ranchor :: state.ranchors;
5058 opendoc path "";
5060 else showtext '!' ("Could not find " ^ filename)
5062 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5065 let canpan () =
5066 match conf.columns with
5067 | Csplit _ -> true
5068 | _ -> state.x != 0 || conf.zoom > 1.0
5071 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5073 let existsinrow pageno (columns, coverA, coverB) p =
5074 let last = ((pageno - coverA) mod columns) + columns in
5075 let rec any = function
5076 | [] -> false
5077 | l :: rest ->
5078 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5079 then p l
5080 else (
5081 if not (p l)
5082 then (if l.pageno = last then false else any rest)
5083 else true
5086 any state.layout
5089 let nextpage () =
5090 match state.layout with
5091 | [] ->
5092 let pageno = page_of_y state.y in
5093 gotoghyll (getpagey (pageno+1))
5094 | l :: rest ->
5095 match conf.columns with
5096 | Csingle _ ->
5097 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5098 then
5099 let y = clamp (pgscale state.winh) in
5100 gotoghyll y
5101 else
5102 let pageno = min (l.pageno+1) (state.pagecount-1) in
5103 gotoghyll (getpagey pageno)
5104 | Cmulti ((c, _, _) as cl, _) ->
5105 if conf.presentation
5106 && (existsinrow l.pageno cl
5107 (fun l -> l.pageh > l.pagey + l.pagevh))
5108 then
5109 let y = clamp (pgscale state.winh) in
5110 gotoghyll y
5111 else
5112 let pageno = min (l.pageno+c) (state.pagecount-1) in
5113 gotoghyll (getpagey pageno)
5114 | Csplit (n, _) ->
5115 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5116 then
5117 let pagey, pageh = getpageyh l.pageno in
5118 let pagey = pagey + pageh * l.pagecol in
5119 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5120 gotoghyll (pagey + pageh + ips)
5123 let prevpage () =
5124 match state.layout with
5125 | [] ->
5126 let pageno = page_of_y state.y in
5127 gotoghyll (getpagey (pageno-1))
5128 | l :: _ ->
5129 match conf.columns with
5130 | Csingle _ ->
5131 if conf.presentation && l.pagey != 0
5132 then
5133 gotoghyll (clamp (pgscale ~-(state.winh)))
5134 else
5135 let pageno = max 0 (l.pageno-1) in
5136 gotoghyll (getpagey pageno)
5137 | Cmulti ((c, _, coverB) as cl, _) ->
5138 if conf.presentation &&
5139 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5140 then
5141 gotoghyll (clamp (pgscale ~-(state.winh)))
5142 else
5143 let decr =
5144 if l.pageno = state.pagecount - coverB
5145 then 1
5146 else c
5148 let pageno = max 0 (l.pageno-decr) in
5149 gotoghyll (getpagey pageno)
5150 | Csplit (n, _) ->
5151 let y =
5152 if l.pagecol = 0
5153 then
5154 if l.pageno = 0
5155 then l.pagey
5156 else
5157 let pageno = max 0 (l.pageno-1) in
5158 let pagey, pageh = getpageyh pageno in
5159 pagey + (n-1)*pageh
5160 else
5161 let pagey, pageh = getpageyh l.pageno in
5162 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5164 gotoghyll y
5167 let viewkeyboard key mask =
5168 let enttext te =
5169 let mode = state.mode in
5170 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5171 state.text <- "";
5172 enttext ();
5173 G.postRedisplay "view:enttext"
5175 let ctrl = Wsi.withctrl mask in
5176 let key =
5177 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5179 match key with
5180 | 81 -> (* Q *)
5181 exit 0
5183 | 0xff63 -> (* insert *)
5184 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5185 then (
5186 state.mode <- LinkNav (Ltgendir 0);
5187 gotoy state.y;
5189 else showtext '!' "Keyboard link navigation does not work under rotation"
5191 | 0xff1b | 113 -> (* escape / q *)
5192 begin match state.mstate with
5193 | Mzoomrect _ ->
5194 state.mstate <- Mnone;
5195 Wsi.setcursor Wsi.CURSOR_INHERIT;
5196 G.postRedisplay "kill zoom rect";
5197 | _ ->
5198 begin match state.mode with
5199 | LinkNav _ ->
5200 state.mode <- View;
5201 G.postRedisplay "esc leave linknav"
5202 | _ ->
5203 match state.ranchors with
5204 | [] -> raise Quit
5205 | (path, password, anchor, origin) :: rest ->
5206 state.ranchors <- rest;
5207 state.anchor <- anchor;
5208 state.origin <- origin;
5209 opendoc path password
5210 end;
5211 end;
5213 | 0xff08 -> (* backspace *)
5214 gotoghyll (getnav ~-1)
5216 | 111 -> (* o *)
5217 enteroutlinemode ()
5219 | 117 -> (* u *)
5220 state.rects <- [];
5221 state.text <- "";
5222 G.postRedisplay "dehighlight";
5224 | 47 | 63 -> (* / ? *)
5225 let ondone isforw s =
5226 cbput state.hists.pat s;
5227 state.searchpattern <- s;
5228 search s isforw
5230 let s = String.create 1 in
5231 s.[0] <- Char.chr key;
5232 enttext (s, "", Some (onhist state.hists.pat),
5233 textentry, ondone (key = 47), true)
5235 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5236 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5237 setzoom (conf.zoom +. incr)
5239 | 43 | 0xffab -> (* + *)
5240 let ondone s =
5241 let n =
5242 try int_of_string s with exc ->
5243 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5244 max_int
5246 if n != max_int
5247 then (
5248 conf.pagebias <- n;
5249 state.text <- "page bias is now " ^ string_of_int n;
5252 enttext ("page bias: ", "", None, intentry, ondone, true)
5254 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5255 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5256 setzoom (max 0.01 (conf.zoom -. decr))
5258 | 45 | 0xffad -> (* - *)
5259 let ondone msg = state.text <- msg in
5260 enttext (
5261 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5262 optentry state.mode, ondone, true
5265 | 48 when ctrl -> (* ctrl-0 *)
5266 if conf.zoom = 1.0
5267 then (
5268 state.x <- 0;
5269 gotoy state.y
5271 else setzoom 1.0
5273 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5274 let cols =
5275 match conf.columns with
5276 | Csingle _ | Cmulti _ -> 1
5277 | Csplit (n, _) -> n
5279 let h = state.winh -
5280 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5282 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5283 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5284 then setzoom zoom
5286 | 51 when ctrl -> (* ctrl-3 *)
5287 let fm =
5288 match conf.fitmodel with
5289 | FitWidth -> FitProportional
5290 | FitProportional -> FitPage
5291 | FitPage -> FitWidth
5293 state.text <- "fit model: " ^ FMTE.to_string fm;
5294 reqlayout conf.angle fm
5296 | 0xffc6 -> (* f9 *)
5297 togglebirdseye ()
5299 | 57 when ctrl -> (* ctrl-9 *)
5300 togglebirdseye ()
5302 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5303 when not ctrl -> (* 0..9 *)
5304 let ondone s =
5305 let n =
5306 try int_of_string s with exc ->
5307 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5310 if n >= 0
5311 then (
5312 addnav ();
5313 cbput state.hists.pag (string_of_int n);
5314 gotopage1 (n + conf.pagebias - 1) 0;
5317 let pageentry text key =
5318 match Char.unsafe_chr key with
5319 | 'g' -> TEdone text
5320 | _ -> intentry text key
5322 let text = "x" in text.[0] <- Char.chr key;
5323 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5325 | 98 -> (* b *)
5326 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5327 reshape state.winw state.winh;
5329 | 66 -> (* B *)
5330 state.bzoom <- not state.bzoom;
5331 state.rects <- [];
5332 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5334 | 108 -> (* l *)
5335 conf.hlinks <- not conf.hlinks;
5336 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5337 G.postRedisplay "toggle highlightlinks";
5339 | 70 -> (* F *)
5340 state.glinks <- true;
5341 let mode = state.mode in
5342 state.mode <- Textentry (
5343 (":", "", None, linknentry, linkndone gotounder, false),
5344 (fun _ ->
5345 state.glinks <- false;
5346 state.mode <- mode)
5348 state.text <- "";
5349 G.postRedisplay "view:linkent(F)"
5351 | 121 -> (* y *)
5352 state.glinks <- true;
5353 let mode = state.mode in
5354 state.mode <- Textentry (
5356 ":", "", None, linknentry, linkndone (fun under ->
5357 selstring (undertext under);
5358 ), false
5360 fun _ ->
5361 state.glinks <- false;
5362 state.mode <- mode
5364 state.text <- "";
5365 G.postRedisplay "view:linkent"
5367 | 97 -> (* a *)
5368 begin match state.autoscroll with
5369 | Some step ->
5370 conf.autoscrollstep <- step;
5371 state.autoscroll <- None
5372 | None ->
5373 if conf.autoscrollstep = 0
5374 then state.autoscroll <- Some 1
5375 else state.autoscroll <- Some conf.autoscrollstep
5378 | 112 when ctrl -> (* ctrl-p *)
5379 launchpath ()
5381 | 80 -> (* P *)
5382 setpresentationmode (not conf.presentation);
5383 showtext ' ' ("presentation mode " ^
5384 if conf.presentation then "on" else "off");
5386 | 102 -> (* f *)
5387 if List.mem Wsi.Fullscreen state.winstate
5388 then Wsi.reshape conf.cwinw conf.cwinh
5389 else Wsi.fullscreen ()
5391 | 112 | 78 -> (* p|N *)
5392 search state.searchpattern false
5394 | 110 | 0xffc0 -> (* n|F3 *)
5395 search state.searchpattern true
5397 | 116 -> (* t *)
5398 begin match state.layout with
5399 | [] -> ()
5400 | l :: _ ->
5401 gotoghyll (getpagey l.pageno)
5404 | 32 -> (* space *)
5405 nextpage ()
5407 | 0xff9f | 0xffff -> (* delete *)
5408 prevpage ()
5410 | 61 -> (* = *)
5411 showtext ' ' (describe_location ());
5413 | 119 -> (* w *)
5414 begin match state.layout with
5415 | [] -> ()
5416 | l :: _ ->
5417 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5418 G.postRedisplay "w"
5421 | 39 -> (* ' *)
5422 enterbookmarkmode ()
5424 | 104 | 0xffbe -> (* h|F1 *)
5425 enterhelpmode ()
5427 | 105 -> (* i *)
5428 enterinfomode ()
5430 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5431 entermsgsmode ()
5433 | 109 -> (* m *)
5434 let ondone s =
5435 match state.layout with
5436 | l :: _ ->
5437 if String.length s > 0
5438 then
5439 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5440 | _ -> ()
5442 enttext ("bookmark: ", "", None, textentry, ondone, true)
5444 | 126 -> (* ~ *)
5445 quickbookmark ();
5446 showtext ' ' "Quick bookmark added";
5448 | 122 -> (* z *)
5449 begin match state.layout with
5450 | l :: _ ->
5451 let rect = getpdimrect l.pagedimno in
5452 let w, h =
5453 if conf.crophack
5454 then
5455 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5456 truncate (1.2 *. (rect.(3) -. rect.(0))))
5457 else
5458 (truncate (rect.(1) -. rect.(0)),
5459 truncate (rect.(3) -. rect.(0)))
5461 let w = truncate ((float w)*.conf.zoom)
5462 and h = truncate ((float h)*.conf.zoom) in
5463 if w != 0 && h != 0
5464 then (
5465 state.anchor <- getanchor ();
5466 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5468 G.postRedisplay "z";
5470 | [] -> ()
5473 | 120 -> state.roam ()
5474 | 60 | 62 -> (* < > *)
5475 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5477 | 91 | 93 -> (* [ ] *)
5478 conf.colorscale <-
5479 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5481 G.postRedisplay "brightness";
5483 | 99 when state.mode = View -> (* [alt-]c *)
5484 if Wsi.withalt mask
5485 then (
5486 if conf.zoom > 1.0
5487 then
5488 let m = (wadjsb state.winw - state.w) / 2 in
5489 state.x <- m;
5490 gotoy_and_clear_text state.y
5492 else
5493 let (c, a, b), z =
5494 match state.prevcolumns with
5495 | None -> (1, 0, 0), 1.0
5496 | Some (columns, z) ->
5497 let cab =
5498 match columns with
5499 | Csplit (c, _) -> -c, 0, 0
5500 | Cmulti ((c, a, b), _) -> c, a, b
5501 | Csingle _ -> 1, 0, 0
5503 cab, z
5505 setcolumns View c a b;
5506 setzoom z
5508 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5509 -> (* ctrl-shift- (kp) [up|down] *)
5510 let zoom, x = state.prevzoom in
5511 setzoom zoom;
5512 state.x <- x;
5514 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5515 begin match state.autoscroll with
5516 | None ->
5517 begin match state.mode with
5518 | Birdseye beye -> upbirdseye 1 beye
5519 | _ ->
5520 if ctrl
5521 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5522 else (
5523 if not (Wsi.withshift mask) && conf.presentation
5524 then prevpage ()
5525 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5528 | Some n ->
5529 setautoscrollspeed n false
5532 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5533 begin match state.autoscroll with
5534 | None ->
5535 begin match state.mode with
5536 | Birdseye beye -> downbirdseye 1 beye
5537 | _ ->
5538 if ctrl
5539 then gotoy_and_clear_text (clamp (state.winh/2))
5540 else (
5541 if not (Wsi.withshift mask) && conf.presentation
5542 then nextpage ()
5543 else gotoy_and_clear_text (clamp conf.scrollstep)
5546 | Some n ->
5547 setautoscrollspeed n true
5550 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5551 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5552 if canpan ()
5553 then
5554 let dx =
5555 if ctrl
5556 then state.winw / 2
5557 else conf.hscrollstep
5559 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5560 state.x <- panbound (state.x + dx);
5561 gotoy_and_clear_text state.y
5562 else (
5563 state.text <- "";
5564 G.postRedisplay "left/right"
5567 | 0xff55 | 0xff9a -> (* (kp) prior *)
5568 let y =
5569 if ctrl
5570 then
5571 match state.layout with
5572 | [] -> state.y
5573 | l :: _ -> state.y - l.pagey
5574 else
5575 clamp (pgscale (-state.winh))
5577 gotoghyll y
5579 | 0xff56 | 0xff9b -> (* (kp) next *)
5580 let y =
5581 if ctrl
5582 then
5583 match List.rev state.layout with
5584 | [] -> state.y
5585 | l :: _ -> getpagey l.pageno
5586 else
5587 clamp (pgscale state.winh)
5589 gotoghyll y
5591 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5592 gotoghyll 0
5593 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5594 gotoghyll (clamp state.maxy)
5596 | 0xff53 | 0xff98
5597 when Wsi.withalt mask -> (* alt-(kp) right *)
5598 gotoghyll (getnav 1)
5599 | 0xff51 | 0xff96
5600 when Wsi.withalt mask -> (* alt-(kp) left *)
5601 gotoghyll (getnav ~-1)
5603 | 114 -> (* r *)
5604 reload ()
5606 | 118 when conf.debug -> (* v *)
5607 state.rects <- [];
5608 List.iter (fun l ->
5609 match getopaque l.pageno with
5610 | None -> ()
5611 | Some opaque ->
5612 let x0, y0, x1, y1 = pagebbox opaque in
5613 let a,b = float x0, float y0 in
5614 let c,d = float x1, float y0 in
5615 let e,f = float x1, float y1 in
5616 let h,j = float x0, float y1 in
5617 let rect = (a,b,c,d,e,f,h,j) in
5618 debugrect rect;
5619 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5620 ) state.layout;
5621 G.postRedisplay "v";
5623 | _ ->
5624 vlog "huh? %s" (Wsi.keyname key)
5627 let linknavkeyboard key mask linknav =
5628 let getpage pageno =
5629 let rec loop = function
5630 | [] -> None
5631 | l :: _ when l.pageno = pageno -> Some l
5632 | _ :: rest -> loop rest
5633 in loop state.layout
5635 let doexact (pageno, n) =
5636 match getopaque pageno, getpage pageno with
5637 | Some opaque, Some l ->
5638 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5639 then
5640 let under = getlink opaque n in
5641 G.postRedisplay "link gotounder";
5642 gotounder under;
5643 state.mode <- View;
5644 else
5645 let opt, dir =
5646 match key with
5647 | 0xff50 -> (* home *)
5648 Some (findlink opaque LDfirst), -1
5650 | 0xff57 -> (* end *)
5651 Some (findlink opaque LDlast), 1
5653 | 0xff51 -> (* left *)
5654 Some (findlink opaque (LDleft n)), -1
5656 | 0xff53 -> (* right *)
5657 Some (findlink opaque (LDright n)), 1
5659 | 0xff52 -> (* up *)
5660 Some (findlink opaque (LDup n)), -1
5662 | 0xff54 -> (* down *)
5663 Some (findlink opaque (LDdown n)), 1
5665 | _ -> None, 0
5667 let pwl l dir =
5668 begin match findpwl l.pageno dir with
5669 | Pwlnotfound -> ()
5670 | Pwl pageno ->
5671 let notfound dir =
5672 state.mode <- LinkNav (Ltgendir dir);
5673 let y, h = getpageyh pageno in
5674 let y =
5675 if dir < 0
5676 then y + h - state.winh
5677 else y
5679 gotoy y
5681 begin match getopaque pageno, getpage pageno with
5682 | Some opaque, Some _ ->
5683 let link =
5684 let ld = if dir > 0 then LDfirst else LDlast in
5685 findlink opaque ld
5687 begin match link with
5688 | Lfound m ->
5689 showlinktype (getlink opaque m);
5690 state.mode <- LinkNav (Ltexact (pageno, m));
5691 G.postRedisplay "linknav jpage";
5692 | _ -> notfound dir
5693 end;
5694 | _ -> notfound dir
5695 end;
5696 end;
5698 begin match opt with
5699 | Some Lnotfound -> pwl l dir;
5700 | Some (Lfound m) ->
5701 if m = n
5702 then pwl l dir
5703 else (
5704 let _, y0, _, y1 = getlinkrect opaque m in
5705 if y0 < l.pagey
5706 then gotopage1 l.pageno y0
5707 else (
5708 let d = fstate.fontsize + 1 in
5709 if y1 - l.pagey > l.pagevh - d
5710 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5711 else G.postRedisplay "linknav";
5713 showlinktype (getlink opaque m);
5714 state.mode <- LinkNav (Ltexact (l.pageno, m));
5717 | None -> viewkeyboard key mask
5718 end;
5719 | _ -> viewkeyboard key mask
5721 if key = 0xff63
5722 then (
5723 state.mode <- View;
5724 G.postRedisplay "leave linknav"
5726 else
5727 match linknav with
5728 | Ltgendir _ -> viewkeyboard key mask
5729 | Ltexact exact -> doexact exact
5732 let keyboard key mask =
5733 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5734 then wcmd "interrupt"
5735 else state.uioh <- state.uioh#key key mask
5738 let birdseyekeyboard key mask
5739 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5740 let incr =
5741 match conf.columns with
5742 | Csingle _ -> 1
5743 | Cmulti ((c, _, _), _) -> c
5744 | Csplit _ -> failwith "bird's eye split mode"
5746 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5747 match key with
5748 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5749 let y, h = getpageyh pageno in
5750 let top = (state.winh - h) / 2 in
5751 gotoy (max 0 (y - top))
5752 | 0xff0d (* enter *)
5753 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5754 | 0xff1b -> leavebirdseye beye true (* escape *)
5755 | 0xff52 -> upbirdseye incr beye (* up *)
5756 | 0xff54 -> downbirdseye incr beye (* down *)
5757 | 0xff51 -> upbirdseye 1 beye (* left *)
5758 | 0xff53 -> downbirdseye 1 beye (* right *)
5760 | 0xff55 -> (* prior *)
5761 begin match state.layout with
5762 | l :: _ ->
5763 if l.pagey != 0
5764 then (
5765 state.mode <- Birdseye (
5766 oconf, leftx, l.pageno, hooverpageno, anchor
5768 gotopage1 l.pageno 0;
5770 else (
5771 let layout = layout (state.y-state.winh) (pgh state.layout) in
5772 match layout with
5773 | [] -> gotoy (clamp (-state.winh))
5774 | l :: _ ->
5775 state.mode <- Birdseye (
5776 oconf, leftx, l.pageno, hooverpageno, anchor
5778 gotopage1 l.pageno 0
5781 | [] -> gotoy (clamp (-state.winh))
5782 end;
5784 | 0xff56 -> (* next *)
5785 begin match List.rev state.layout with
5786 | l :: _ ->
5787 let layout = layout (state.y + (pgh state.layout)) state.winh in
5788 begin match layout with
5789 | [] ->
5790 let incr = l.pageh - l.pagevh in
5791 if incr = 0
5792 then (
5793 state.mode <-
5794 Birdseye (
5795 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5797 G.postRedisplay "birdseye pagedown";
5799 else gotoy (clamp (incr + conf.interpagespace*2));
5801 | l :: _ ->
5802 state.mode <-
5803 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5804 gotopage1 l.pageno 0;
5807 | [] -> gotoy (clamp state.winh)
5808 end;
5810 | 0xff50 -> (* home *)
5811 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5812 gotopage1 0 0
5814 | 0xff57 -> (* end *)
5815 let pageno = state.pagecount - 1 in
5816 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5817 if not (pagevisible state.layout pageno)
5818 then
5819 let h =
5820 match List.rev state.pdims with
5821 | [] -> state.winh
5822 | (_, _, h, _) :: _ -> h
5824 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5825 else G.postRedisplay "birdseye end";
5826 | _ -> viewkeyboard key mask
5829 let drawpage l =
5830 let color =
5831 match state.mode with
5832 | Textentry _ -> scalecolor 0.4
5833 | LinkNav _
5834 | View -> scalecolor 1.0
5835 | Birdseye (_, _, pageno, hooverpageno, _) ->
5836 if l.pageno = hooverpageno
5837 then scalecolor 0.9
5838 else (
5839 if l.pageno = pageno
5840 then scalecolor 1.0
5841 else scalecolor 0.8
5844 drawtiles l color;
5847 let postdrawpage l linkindexbase =
5848 match getopaque l.pageno with
5849 | Some opaque ->
5850 if tileready l l.pagex l.pagey
5851 then
5852 let x = l.pagedispx - l.pagex
5853 and y = l.pagedispy - l.pagey in
5854 let hlmask =
5855 match conf.columns with
5856 | Csingle _ | Cmulti _ ->
5857 (if conf.hlinks then 1 else 0)
5858 + (if state.glinks
5859 && not (isbirdseye state.mode) then 2 else 0)
5860 | _ -> 0
5862 let s =
5863 match state.mode with
5864 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5865 | _ -> ""
5867 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5868 else 0
5869 | _ -> 0
5872 let scrollindicator () =
5873 let sbw, ph, sh = state.uioh#scrollph in
5874 let sbh, pw, sw = state.uioh#scrollpw in
5876 GlDraw.color (0.64, 0.64, 0.64);
5877 GlDraw.rect
5878 (float (state.winw - sbw), 0.)
5879 (float state.winw, float state.winh)
5881 GlDraw.rect
5882 (0., float (state.winh - sbh))
5883 (float (wadjsb state.winw - 1), float state.winh)
5885 GlDraw.color (0.0, 0.0, 0.0);
5887 GlDraw.rect
5888 (float (state.winw - sbw), ph)
5889 (float state.winw, ph +. sh)
5891 GlDraw.rect
5892 (pw, float (state.winh - sbh))
5893 (pw +. sw, float state.winh)
5897 let showsel () =
5898 match state.mstate with
5899 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5902 | Msel ((x0, y0), (x1, y1)) ->
5903 let rec loop = function
5904 | l :: ls ->
5905 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5906 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5907 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5908 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5909 then
5910 match getopaque l.pageno with
5911 | Some opaque ->
5912 let x0, y0 = pagetranslatepoint l x0 y0 in
5913 let x1, y1 = pagetranslatepoint l x1 y1 in
5914 seltext opaque (x0, y0, x1, y1);
5915 | _ -> ()
5916 else loop ls
5917 | [] -> ()
5919 loop state.layout
5922 let showrects rects =
5923 Gl.enable `blend;
5924 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5925 GlDraw.polygon_mode `both `fill;
5926 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5927 List.iter
5928 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5929 List.iter (fun l ->
5930 if l.pageno = pageno
5931 then (
5932 let dx = float (l.pagedispx - l.pagex) in
5933 let dy = float (l.pagedispy - l.pagey) in
5934 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5935 GlDraw.begins `quads;
5937 GlDraw.vertex2 (x0+.dx, y0+.dy);
5938 GlDraw.vertex2 (x1+.dx, y1+.dy);
5939 GlDraw.vertex2 (x2+.dx, y2+.dy);
5940 GlDraw.vertex2 (x3+.dx, y3+.dy);
5942 GlDraw.ends ();
5944 ) state.layout
5945 ) rects
5947 Gl.disable `blend;
5950 let display () =
5951 GlClear.color (scalecolor2 conf.bgcolor);
5952 GlClear.clear [`color];
5953 List.iter drawpage state.layout;
5954 let rects =
5955 match state.mode with
5956 | LinkNav (Ltexact (pageno, linkno)) ->
5957 begin match getopaque pageno with
5958 | Some opaque ->
5959 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5960 (pageno, 5, (
5961 float x0, float y0,
5962 float x1, float y0,
5963 float x1, float y1,
5964 float x0, float y1)
5965 ) :: state.rects
5966 | None -> state.rects
5968 | _ -> state.rects
5970 showrects rects;
5971 let rec postloop linkindexbase = function
5972 | l :: rest ->
5973 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5974 postloop linkindexbase rest
5975 | [] -> ()
5977 showsel ();
5978 postloop 0 state.layout;
5979 state.uioh#display;
5980 begin match state.mstate with
5981 | Mzoomrect ((x0, y0), (x1, y1)) ->
5982 Gl.enable `blend;
5983 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5984 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5985 GlDraw.rect (float x0, float y0)
5986 (float x1, float y1);
5987 Gl.disable `blend;
5988 | _ -> ()
5989 end;
5990 enttext ();
5991 scrollindicator ();
5992 Wsi.swapb ();
5995 let zoomrect x y x1 y1 =
5996 let x0 = min x x1
5997 and x1 = max x x1
5998 and y0 = min y y1 in
5999 gotoy (state.y + y0);
6000 state.anchor <- getanchor ();
6001 let zoom = (float state.w) /. float (x1 - x0) in
6002 let margin =
6003 match conf.fitmodel, conf.columns with
6004 | FitPage, Csplit _ ->
6005 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6007 | _, _ ->
6008 let adjw = wadjsb state.winw in
6009 if state.w < adjw
6010 then (adjw - state.w) / 2
6011 else 0
6013 state.x <- (state.x + margin) - x0;
6014 setzoom zoom;
6015 Wsi.setcursor Wsi.CURSOR_INHERIT;
6016 state.mstate <- Mnone;
6019 let zoomblock x y =
6020 let g opaque l px py =
6021 match rectofblock opaque px py with
6022 | Some a ->
6023 let x0 = a.(0) -. 20. in
6024 let x1 = a.(1) +. 20. in
6025 let y0 = a.(2) -. 20. in
6026 let zoom = (float state.w) /. (x1 -. x0) in
6027 let pagey = getpagey l.pageno in
6028 gotoy_and_clear_text (pagey + truncate y0);
6029 state.anchor <- getanchor ();
6030 let margin = (state.w - l.pagew)/2 in
6031 state.x <- -truncate x0 - margin;
6032 setzoom zoom;
6033 None
6034 | None -> None
6036 match conf.columns with
6037 | Csplit _ ->
6038 showtext '!' "block zooming does not work properly in split columns mode"
6039 | _ -> onppundermouse g x y ()
6042 let scrollx x =
6043 let winw = wadjsb state.winw - 1 in
6044 let s = float x /. float winw in
6045 let destx = truncate (float (state.w + winw) *. s) in
6046 state.x <- winw - destx;
6047 gotoy_and_clear_text state.y;
6048 state.mstate <- Mscrollx;
6051 let scrolly y =
6052 let s = float y /. float state.winh in
6053 let desty = truncate (float (state.maxy - state.winh) *. s) in
6054 gotoy_and_clear_text desty;
6055 state.mstate <- Mscrolly;
6058 let viewmouse button down x y mask =
6059 match button with
6060 | n when (n == 4 || n == 5) && not down ->
6061 if Wsi.withctrl mask
6062 then (
6063 match state.mstate with
6064 | Mzoom (oldn, i) ->
6065 if oldn = n
6066 then (
6067 if i = 2
6068 then
6069 let incr =
6070 match n with
6071 | 5 ->
6072 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6073 | _ ->
6074 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6076 let zoom = conf.zoom -. incr in
6077 setzoom zoom;
6078 state.mstate <- Mzoom (n, 0);
6079 else
6080 state.mstate <- Mzoom (n, i+1);
6082 else state.mstate <- Mzoom (n, 0)
6084 | _ -> state.mstate <- Mzoom (n, 0)
6086 else (
6087 match state.autoscroll with
6088 | Some step -> setautoscrollspeed step (n=4)
6089 | None ->
6090 if conf.wheelbypage || conf.presentation
6091 then (
6092 if n = 4
6093 then prevpage ()
6094 else nextpage ()
6096 else
6097 let incr =
6098 if n = 4
6099 then -conf.scrollstep
6100 else conf.scrollstep
6102 let incr = incr * 2 in
6103 let y = clamp incr in
6104 gotoy_and_clear_text y
6107 | n when (n = 6 || n = 7) && not down && canpan () ->
6108 state.x <-
6109 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6110 gotoy_and_clear_text state.y
6112 | 1 when Wsi.withshift mask ->
6113 state.mstate <- Mnone;
6114 if not down
6115 then (
6116 match unproject x y with
6117 | Some (pageno, ux, uy) ->
6118 let cmd = Printf.sprintf
6119 "%s %s %d %d %d"
6120 conf.stcmd state.path pageno ux uy
6122 popen cmd []
6123 | None -> ()
6126 | 1 when Wsi.withctrl mask ->
6127 if down
6128 then (
6129 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6130 state.mstate <- Mpan (x, y)
6132 else
6133 state.mstate <- Mnone
6135 | 3 ->
6136 if down
6137 then (
6138 Wsi.setcursor Wsi.CURSOR_CYCLE;
6139 let p = (x, y) in
6140 state.mstate <- Mzoomrect (p, p)
6142 else (
6143 match state.mstate with
6144 | Mzoomrect ((x0, y0), _) ->
6145 if abs (x-x0) > 10 && abs (y - y0) > 10
6146 then zoomrect x0 y0 x y
6147 else (
6148 state.mstate <- Mnone;
6149 Wsi.setcursor Wsi.CURSOR_INHERIT;
6150 G.postRedisplay "kill accidental zoom rect";
6152 | _ ->
6153 Wsi.setcursor Wsi.CURSOR_INHERIT;
6154 state.mstate <- Mnone
6157 | 1 when x > state.winw - vscrollw () ->
6158 if down
6159 then
6160 let _, position, sh = state.uioh#scrollph in
6161 if y > truncate position && y < truncate (position +. sh)
6162 then state.mstate <- Mscrolly
6163 else scrolly y
6164 else
6165 state.mstate <- Mnone
6167 | 1 when y > state.winh - hscrollh () ->
6168 if down
6169 then
6170 let _, position, sw = state.uioh#scrollpw in
6171 if x > truncate position && x < truncate (position +. sw)
6172 then state.mstate <- Mscrollx
6173 else scrollx x
6174 else
6175 state.mstate <- Mnone
6177 | 1 when state.bzoom -> if not down then zoomblock x y
6179 | 1 ->
6180 let dest = if down then getunder x y else Unone in
6181 begin match dest with
6182 | Ulinkgoto _
6183 | Ulinkuri _
6184 | Uremote _
6185 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6186 gotounder dest
6188 | Unone when down ->
6189 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6190 state.mstate <- Mpan (x, y);
6192 | Unone | Utext _ ->
6193 if down
6194 then (
6195 if conf.angle mod 360 = 0
6196 then (
6197 state.mstate <- Msel ((x, y), (x, y));
6198 G.postRedisplay "mouse select";
6201 else (
6202 match state.mstate with
6203 | Mnone -> ()
6205 | Mzoom _ | Mscrollx | Mscrolly ->
6206 state.mstate <- Mnone
6208 | Mzoomrect ((x0, y0), _) ->
6209 zoomrect x0 y0 x y
6211 | Mpan _ ->
6212 Wsi.setcursor Wsi.CURSOR_INHERIT;
6213 state.mstate <- Mnone
6215 | Msel ((x0, y0), (x1, y1)) ->
6216 let rec loop = function
6217 | [] -> ()
6218 | l :: rest ->
6219 let inside =
6220 let a0 = l.pagedispy in
6221 let a1 = a0 + l.pagevh in
6222 let b0 = l.pagedispx in
6223 let b1 = b0 + l.pagevw in
6224 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6225 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6227 if inside
6228 then
6229 match getopaque l.pageno with
6230 | Some opaque ->
6231 begin
6232 match Ne.pipe () with
6233 | Ne.Exn exn ->
6234 showtext '!'
6235 (Printf.sprintf
6236 "can not create sel pipe: %s"
6237 (exntos exn));
6238 | Ne.Res (r, w) ->
6239 let doclose what fd =
6240 Ne.clo fd (fun msg ->
6241 dolog "%s close failed: %s" what msg)
6244 popen conf.selcmd [r, 0; w, -1];
6245 copysel w opaque true;
6246 doclose "pipe/r" r;
6247 G.postRedisplay "copysel";
6248 with exn ->
6249 dolog "can not execute %S: %s"
6250 conf.selcmd (exntos exn);
6251 doclose "pipe/r" r;
6252 doclose "pipe/w" w;
6254 | None -> ()
6255 else loop rest
6257 loop state.layout;
6258 Wsi.setcursor Wsi.CURSOR_INHERIT;
6259 state.mstate <- Mnone;
6263 | _ -> ()
6266 let birdseyemouse button down x y mask
6267 (conf, leftx, _, hooverpageno, anchor) =
6268 match button with
6269 | 1 when down ->
6270 let rec loop = function
6271 | [] -> ()
6272 | l :: rest ->
6273 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6274 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6275 then (
6276 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6278 else loop rest
6280 loop state.layout
6281 | 3 -> ()
6282 | _ -> viewmouse button down x y mask
6285 let mouse button down x y mask =
6286 state.uioh <- state.uioh#button button down x y mask;
6289 let motion ~x ~y =
6290 state.uioh <- state.uioh#motion x y
6293 let pmotion ~x ~y =
6294 state.uioh <- state.uioh#pmotion x y;
6297 let uioh = object
6298 method display = ()
6300 method key key mask =
6301 begin match state.mode with
6302 | Textentry textentry -> textentrykeyboard key mask textentry
6303 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6304 | View -> viewkeyboard key mask
6305 | LinkNav linknav -> linknavkeyboard key mask linknav
6306 end;
6307 state.uioh
6309 method button button bstate x y mask =
6310 begin match state.mode with
6311 | LinkNav _
6312 | View -> viewmouse button bstate x y mask
6313 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6314 | Textentry _ -> ()
6315 end;
6316 state.uioh
6318 method motion x y =
6319 begin match state.mode with
6320 | Textentry _ -> ()
6321 | View | Birdseye _ | LinkNav _ ->
6322 match state.mstate with
6323 | Mzoom _ | Mnone -> ()
6325 | Mpan (x0, y0) ->
6326 let dx = x - x0
6327 and dy = y0 - y in
6328 state.mstate <- Mpan (x, y);
6329 if canpan ()
6330 then state.x <- panbound (state.x + dx);
6331 let y = clamp dy in
6332 gotoy_and_clear_text y
6334 | Msel (a, _) ->
6335 state.mstate <- Msel (a, (x, y));
6336 G.postRedisplay "motion select";
6338 | Mscrolly ->
6339 let y = min state.winh (max 0 y) in
6340 scrolly y
6342 | Mscrollx ->
6343 let x = min state.winw (max 0 x) in
6344 scrollx x
6346 | Mzoomrect (p0, _) ->
6347 state.mstate <- Mzoomrect (p0, (x, y));
6348 G.postRedisplay "motion zoomrect";
6349 end;
6350 state.uioh
6352 method pmotion x y =
6353 begin match state.mode with
6354 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6355 let rec loop = function
6356 | [] ->
6357 if hooverpageno != -1
6358 then (
6359 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6360 G.postRedisplay "pmotion birdseye no hoover";
6362 | l :: rest ->
6363 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6364 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6365 then (
6366 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6367 G.postRedisplay "pmotion birdseye hoover";
6369 else loop rest
6371 loop state.layout
6373 | Textentry _ -> ()
6375 | LinkNav _
6376 | View ->
6377 match state.mstate with
6378 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6380 | Mnone ->
6381 updateunder x y;
6382 match conf.pax with
6383 | None -> ()
6384 | Some r ->
6385 let past, _, _ = !r in
6386 let now = now () in
6387 let delta = now -. past in
6388 if delta > 0.01
6389 then paxunder x y
6390 else r := (now, x, y)
6391 end;
6392 state.uioh
6394 method infochanged _ = ()
6396 method scrollph =
6397 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6398 let p, h =
6399 if maxy = 0
6400 then 0.0, float state.winh
6401 else scrollph state.y maxy
6403 vscrollw (), p, h
6405 method scrollpw =
6406 let winw = wadjsb state.winw in
6407 let fwinw = float winw in
6408 let sw =
6409 let sw = fwinw /. float state.w in
6410 let sw = fwinw *. sw in
6411 max sw (float conf.scrollh)
6413 let position =
6414 let maxx = state.w + winw in
6415 let x = winw - state.x in
6416 let percent = float x /. float maxx in
6417 (fwinw -. sw) *. percent
6419 hscrollh (), position, sw
6421 method modehash =
6422 let modename =
6423 match state.mode with
6424 | LinkNav _ -> "links"
6425 | Textentry _ -> "textentry"
6426 | Birdseye _ -> "birdseye"
6427 | View -> "view"
6429 findkeyhash conf modename
6431 method eformsgs = true
6432 end;;
6434 module Config =
6435 struct
6436 open Parser
6438 let fontpath = ref "";;
6440 module KeyMap =
6441 Map.Make (struct type t = (int * int) let compare = compare end);;
6443 let unent s =
6444 let l = String.length s in
6445 let b = Buffer.create l in
6446 unent b s 0 l;
6447 Buffer.contents b;
6450 let home =
6451 try Sys.getenv "HOME"
6452 with exn ->
6453 prerr_endline
6454 ("Can not determine home directory location: " ^ exntos exn);
6458 let modifier_of_string = function
6459 | "alt" -> Wsi.altmask
6460 | "shift" -> Wsi.shiftmask
6461 | "ctrl" | "control" -> Wsi.ctrlmask
6462 | "meta" -> Wsi.metamask
6463 | _ -> 0
6466 let key_of_string =
6467 let r = Str.regexp "-" in
6468 fun s ->
6469 let elems = Str.full_split r s in
6470 let f n k m =
6471 let g s =
6472 let m1 = modifier_of_string s in
6473 if m1 = 0
6474 then (Wsi.namekey s, m)
6475 else (k, m lor m1)
6476 in function
6477 | Str.Delim s when n land 1 = 0 -> g s
6478 | Str.Text s -> g s
6479 | Str.Delim _ -> (k, m)
6481 let rec loop n k m = function
6482 | [] -> (k, m)
6483 | x :: xs ->
6484 let k, m = f n k m x in
6485 loop (n+1) k m xs
6487 loop 0 0 0 elems
6490 let keys_of_string =
6491 let r = Str.regexp "[ \t]" in
6492 fun s ->
6493 let elems = Str.split r s in
6494 List.map key_of_string elems
6497 let copykeyhashes c =
6498 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6501 let config_of c attrs =
6502 let apply c k v =
6504 match k with
6505 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6506 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6507 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6508 | "preload" -> { c with preload = bool_of_string v }
6509 | "page-bias" -> { c with pagebias = int_of_string v }
6510 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6511 | "horizontal-scroll-step" ->
6512 { c with hscrollstep = max (int_of_string v) 1 }
6513 | "auto-scroll-step" ->
6514 { c with autoscrollstep = max 0 (int_of_string v) }
6515 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6516 | "crop-hack" -> { c with crophack = bool_of_string v }
6517 | "throttle" ->
6518 let mw =
6519 match String.lowercase v with
6520 | "true" -> Some infinity
6521 | "false" -> None
6522 | f -> Some (float_of_string f)
6524 { c with maxwait = mw}
6525 | "highlight-links" -> { c with hlinks = bool_of_string v }
6526 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6527 | "vertical-margin" ->
6528 { c with interpagespace = max 0 (int_of_string v) }
6529 | "zoom" ->
6530 let zoom = float_of_string v /. 100. in
6531 let zoom = max zoom 0.0 in
6532 { c with zoom = zoom }
6533 | "presentation" -> { c with presentation = bool_of_string v }
6534 | "rotation-angle" -> { c with angle = int_of_string v }
6535 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6536 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6537 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6538 | "proportional-display" ->
6539 let fm =
6540 if bool_of_string v
6541 then FitProportional
6542 else FitWidth
6544 { c with fitmodel = fm }
6545 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6546 | "pixmap-cache-size" ->
6547 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6548 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6549 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6550 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6551 | "persistent-location" -> { c with jumpback = bool_of_string v }
6552 | "background-color" -> { c with bgcolor = color_of_string v }
6553 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6554 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6555 | "mupdf-store-size" ->
6556 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6557 | "checkers" -> { c with checkers = bool_of_string v }
6558 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6559 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6560 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6561 | "uri-launcher" -> { c with urilauncher = unent v }
6562 | "path-launcher" -> { c with pathlauncher = unent v }
6563 | "color-space" -> { c with colorspace = CSTE.of_string v }
6564 | "invert-colors" -> { c with invert = bool_of_string v }
6565 | "brightness" -> { c with colorscale = float_of_string v }
6566 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6567 | "ghyllscroll" ->
6568 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6569 | "columns" ->
6570 let (n, _, _) as nab = multicolumns_of_string v in
6571 if n < 0
6572 then { c with columns = Csplit (-n, [||]) }
6573 else { c with columns = Cmulti (nab, [||]) }
6574 | "birds-eye-columns" ->
6575 { c with beyecolumns = Some (max (int_of_string v) 2) }
6576 | "selection-command" -> { c with selcmd = unent v }
6577 | "synctex-command" -> { c with stcmd = unent v }
6578 | "pax-command" -> { c with paxcmd = unent v }
6579 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6580 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6581 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6582 | "use-pbo" -> { c with usepbo = bool_of_string v }
6583 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6584 | "horizontal-scrollbar-visible" ->
6585 let b =
6586 if bool_of_string v
6587 then c.scrollb lor scrollbhv
6588 else c.scrollb land (lnot scrollbhv)
6590 { c with scrollb = b }
6591 | "vertical-scrollbar-visible" ->
6592 let b =
6593 if bool_of_string v
6594 then c.scrollb lor scrollbvv
6595 else c.scrollb land (lnot scrollbvv)
6597 { c with scrollb = b }
6598 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6599 | "point-and-x" ->
6600 { c with pax =
6601 if bool_of_string v
6602 then Some (ref (0.0, 0, 0))
6603 else None }
6604 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6605 | _ -> c
6606 with exn ->
6607 prerr_endline ("Error processing attribute (`" ^
6608 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6611 let rec fold c = function
6612 | [] -> c
6613 | (k, v) :: rest ->
6614 let c = apply c k v in
6615 fold c rest
6617 fold { c with keyhashes = copykeyhashes c } attrs;
6620 let fromstring f pos n v d =
6621 try f v
6622 with exn ->
6623 dolog "Error processing attribute (%S=%S) at %d\n%s"
6624 n v pos (exntos exn)
6629 let bookmark_of attrs =
6630 let rec fold title page rely visy = function
6631 | ("title", v) :: rest -> fold v page rely visy rest
6632 | ("page", v) :: rest -> fold title v rely visy rest
6633 | ("rely", v) :: rest -> fold title page v visy rest
6634 | ("visy", v) :: rest -> fold title page rely v rest
6635 | _ :: rest -> fold title page rely visy rest
6636 | [] -> title, page, rely, visy
6638 fold "invalid" "0" "0" "0" attrs
6641 let doc_of attrs =
6642 let rec fold path page rely pan visy = function
6643 | ("path", v) :: rest -> fold v page rely pan visy rest
6644 | ("page", v) :: rest -> fold path v rely pan visy rest
6645 | ("rely", v) :: rest -> fold path page v pan visy rest
6646 | ("pan", v) :: rest -> fold path page rely v visy rest
6647 | ("visy", v) :: rest -> fold path page rely pan v rest
6648 | _ :: rest -> fold path page rely pan visy rest
6649 | [] -> path, page, rely, pan, visy
6651 fold "" "0" "0" "0" "0" attrs
6654 let map_of attrs =
6655 let rec fold rs ls = function
6656 | ("out", v) :: rest -> fold v ls rest
6657 | ("in", v) :: rest -> fold rs v rest
6658 | _ :: rest -> fold ls rs rest
6659 | [] -> ls, rs
6661 fold "" "" attrs
6664 let setconf dst src =
6665 dst.scrollbw <- src.scrollbw;
6666 dst.scrollh <- src.scrollh;
6667 dst.icase <- src.icase;
6668 dst.preload <- src.preload;
6669 dst.pagebias <- src.pagebias;
6670 dst.verbose <- src.verbose;
6671 dst.scrollstep <- src.scrollstep;
6672 dst.maxhfit <- src.maxhfit;
6673 dst.crophack <- src.crophack;
6674 dst.autoscrollstep <- src.autoscrollstep;
6675 dst.maxwait <- src.maxwait;
6676 dst.hlinks <- src.hlinks;
6677 dst.underinfo <- src.underinfo;
6678 dst.interpagespace <- src.interpagespace;
6679 dst.zoom <- src.zoom;
6680 dst.presentation <- src.presentation;
6681 dst.angle <- src.angle;
6682 dst.cwinw <- src.cwinw;
6683 dst.cwinh <- src.cwinh;
6684 dst.savebmarks <- src.savebmarks;
6685 dst.memlimit <- src.memlimit;
6686 dst.fitmodel <- src.fitmodel;
6687 dst.texcount <- src.texcount;
6688 dst.sliceheight <- src.sliceheight;
6689 dst.thumbw <- src.thumbw;
6690 dst.jumpback <- src.jumpback;
6691 dst.bgcolor <- src.bgcolor;
6692 dst.tilew <- src.tilew;
6693 dst.tileh <- src.tileh;
6694 dst.mustoresize <- src.mustoresize;
6695 dst.checkers <- src.checkers;
6696 dst.aalevel <- src.aalevel;
6697 dst.trimmargins <- src.trimmargins;
6698 dst.trimfuzz <- src.trimfuzz;
6699 dst.urilauncher <- src.urilauncher;
6700 dst.colorspace <- src.colorspace;
6701 dst.invert <- src.invert;
6702 dst.colorscale <- src.colorscale;
6703 dst.redirectstderr <- src.redirectstderr;
6704 dst.ghyllscroll <- src.ghyllscroll;
6705 dst.columns <- src.columns;
6706 dst.beyecolumns <- src.beyecolumns;
6707 dst.selcmd <- src.selcmd;
6708 dst.updatecurs <- src.updatecurs;
6709 dst.pathlauncher <- src.pathlauncher;
6710 dst.keyhashes <- copykeyhashes src;
6711 dst.hfsize <- src.hfsize;
6712 dst.hscrollstep <- src.hscrollstep;
6713 dst.pgscale <- src.pgscale;
6714 dst.usepbo <- src.usepbo;
6715 dst.wheelbypage <- src.wheelbypage;
6716 dst.stcmd <- src.stcmd;
6717 dst.paxcmd <- src.paxcmd;
6718 dst.scrollb <- src.scrollb;
6719 dst.riani <- src.riani;
6720 dst.paxmark <- src.paxmark;
6721 dst.pax <-
6722 if src.pax = None
6723 then None
6724 else Some ((ref (0.0, 0, 0)));
6727 let get s =
6728 let h = Hashtbl.create 10 in
6729 let dc = { defconf with angle = defconf.angle } in
6730 let rec toplevel v t spos _ =
6731 match t with
6732 | Vdata | Vcdata | Vend -> v
6733 | Vopen ("llppconfig", _, closed) ->
6734 if closed
6735 then v
6736 else { v with f = llppconfig }
6737 | Vopen _ ->
6738 error "unexpected subelement at top level" s spos
6739 | Vclose _ -> error "unexpected close at top level" s spos
6741 and llppconfig v t spos _ =
6742 match t with
6743 | Vdata | Vcdata -> v
6744 | Vend -> error "unexpected end of input in llppconfig" s spos
6745 | Vopen ("defaults", attrs, closed) ->
6746 let c = config_of dc attrs in
6747 setconf dc c;
6748 if closed
6749 then v
6750 else { v with f = defaults }
6752 | Vopen ("ui-font", attrs, closed) ->
6753 let rec getsize size = function
6754 | [] -> size
6755 | ("size", v) :: rest ->
6756 let size =
6757 fromstring int_of_string spos "size" v fstate.fontsize in
6758 getsize size rest
6759 | l -> getsize size l
6761 fstate.fontsize <- getsize fstate.fontsize attrs;
6762 if closed
6763 then v
6764 else { v with f = uifont (Buffer.create 10) }
6766 | Vopen ("doc", attrs, closed) ->
6767 let pathent, spage, srely, span, svisy = doc_of attrs in
6768 let path = unent pathent
6769 and pageno = fromstring int_of_string spos "page" spage 0
6770 and rely = fromstring float_of_string spos "rely" srely 0.0
6771 and pan = fromstring int_of_string spos "pan" span 0
6772 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6773 let c = config_of dc attrs in
6774 let anchor = (pageno, rely, visy) in
6775 if closed
6776 then (Hashtbl.add h path (c, [], pan, anchor); v)
6777 else { v with f = doc path pan anchor c [] }
6779 | Vopen _ ->
6780 error "unexpected subelement in llppconfig" s spos
6782 | Vclose "llppconfig" -> { v with f = toplevel }
6783 | Vclose _ -> error "unexpected close in llppconfig" s spos
6785 and defaults v t spos _ =
6786 match t with
6787 | Vdata | Vcdata -> v
6788 | Vend -> error "unexpected end of input in defaults" s spos
6789 | Vopen ("keymap", attrs, closed) ->
6790 let modename =
6791 try List.assoc "mode" attrs
6792 with Not_found -> "global" in
6793 if closed
6794 then v
6795 else
6796 let ret keymap =
6797 let h = findkeyhash dc modename in
6798 KeyMap.iter (Hashtbl.replace h) keymap;
6799 defaults
6801 { v with f = pkeymap ret KeyMap.empty }
6803 | Vopen (_, _, _) ->
6804 error "unexpected subelement in defaults" s spos
6806 | Vclose "defaults" ->
6807 { v with f = llppconfig }
6809 | Vclose _ -> error "unexpected close in defaults" s spos
6811 and uifont b v t spos epos =
6812 match t with
6813 | Vdata | Vcdata ->
6814 Buffer.add_substring b s spos (epos - spos);
6816 | Vopen (_, _, _) ->
6817 error "unexpected subelement in ui-font" s spos
6818 | Vclose "ui-font" ->
6819 if String.length !fontpath = 0
6820 then fontpath := Buffer.contents b;
6821 { v with f = llppconfig }
6822 | Vclose _ -> error "unexpected close in ui-font" s spos
6823 | Vend -> error "unexpected end of input in ui-font" s spos
6825 and doc path pan anchor c bookmarks v t spos _ =
6826 match t with
6827 | Vdata | Vcdata -> v
6828 | Vend -> error "unexpected end of input in doc" s spos
6829 | Vopen ("bookmarks", _, closed) ->
6830 if closed
6831 then v
6832 else { v with f = pbookmarks path pan anchor c bookmarks }
6834 | Vopen ("keymap", attrs, closed) ->
6835 let modename =
6836 try List.assoc "mode" attrs
6837 with Not_found -> "global"
6839 if closed
6840 then v
6841 else
6842 let ret keymap =
6843 let h = findkeyhash c modename in
6844 KeyMap.iter (Hashtbl.replace h) keymap;
6845 doc path pan anchor c bookmarks
6847 { v with f = pkeymap ret KeyMap.empty }
6849 | Vopen (_, _, _) ->
6850 error "unexpected subelement in doc" s spos
6852 | Vclose "doc" ->
6853 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6854 { v with f = llppconfig }
6856 | Vclose _ -> error "unexpected close in doc" s spos
6858 and pkeymap ret keymap v t spos _ =
6859 match t with
6860 | Vdata | Vcdata -> v
6861 | Vend -> error "unexpected end of input in keymap" s spos
6862 | Vopen ("map", attrs, closed) ->
6863 let r, l = map_of attrs in
6864 let kss = fromstring keys_of_string spos "in" r [] in
6865 let lss = fromstring keys_of_string spos "out" l [] in
6866 let keymap =
6867 match kss with
6868 | [] -> keymap
6869 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6870 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6872 if closed
6873 then { v with f = pkeymap ret keymap }
6874 else
6875 let f () = v in
6876 { v with f = skip "map" f }
6878 | Vopen _ ->
6879 error "unexpected subelement in keymap" s spos
6881 | Vclose "keymap" ->
6882 { v with f = ret keymap }
6884 | Vclose _ -> error "unexpected close in keymap" s spos
6886 and pbookmarks path pan anchor c bookmarks v t spos _ =
6887 match t with
6888 | Vdata | Vcdata -> v
6889 | Vend -> error "unexpected end of input in bookmarks" s spos
6890 | Vopen ("item", attrs, closed) ->
6891 let titleent, spage, srely, svisy = bookmark_of attrs in
6892 let page = fromstring int_of_string spos "page" spage 0
6893 and rely = fromstring float_of_string spos "rely" srely 0.0
6894 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6895 let bookmarks =
6896 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6898 if closed
6899 then { v with f = pbookmarks path pan anchor c bookmarks }
6900 else
6901 let f () = v in
6902 { v with f = skip "item" f }
6904 | Vopen _ ->
6905 error "unexpected subelement in bookmarks" s spos
6907 | Vclose "bookmarks" ->
6908 { v with f = doc path pan anchor c bookmarks }
6910 | Vclose _ -> error "unexpected close in bookmarks" s spos
6912 and skip tag f v t spos _ =
6913 match t with
6914 | Vdata | Vcdata -> v
6915 | Vend ->
6916 error ("unexpected end of input in skipped " ^ tag) s spos
6917 | Vopen (tag', _, closed) ->
6918 if closed
6919 then v
6920 else
6921 let f' () = { v with f = skip tag f } in
6922 { v with f = skip tag' f' }
6923 | Vclose ctag ->
6924 if tag = ctag
6925 then f ()
6926 else error ("unexpected close in skipped " ^ tag) s spos
6929 parse { f = toplevel; accu = () } s;
6930 h, dc;
6933 let do_load f ic =
6935 let len = in_channel_length ic in
6936 let s = String.create len in
6937 really_input ic s 0 len;
6938 f s;
6939 with
6940 | Parse_error (msg, s, pos) ->
6941 let subs = subs s pos in
6942 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6943 failwith ("parse error: " ^ s)
6945 | exn ->
6946 failwith ("config load error: " ^ exntos exn)
6949 let defconfpath =
6950 let dir =
6952 let dir = Filename.concat home ".config" in
6953 if Sys.is_directory dir then dir else home
6954 with _ -> home
6956 Filename.concat dir "llpp.conf"
6959 let confpath = ref defconfpath;;
6961 let load1 f =
6962 if Sys.file_exists !confpath
6963 then
6964 match
6965 (try Some (open_in_bin !confpath)
6966 with exn ->
6967 prerr_endline
6968 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6969 exntos exn);
6970 None
6972 with
6973 | Some ic ->
6974 let success =
6976 f (do_load get ic)
6977 with exn ->
6978 prerr_endline
6979 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6980 exntos exn);
6981 false
6983 close_in ic;
6984 success
6986 | None -> false
6987 else
6988 f (Hashtbl.create 0, defconf)
6991 let load () =
6992 let f (h, dc) =
6993 let pc, pb, px, pa =
6995 let key =
6996 if String.length state.origin = 0
6997 then state.path
6998 else state.origin
7000 Hashtbl.find h (Filename.basename key)
7001 with Not_found -> dc, [], 0, emptyanchor
7003 setconf defconf dc;
7004 setconf conf pc;
7005 state.bookmarks <- pb;
7006 state.x <- px;
7007 if conf.jumpback
7008 then state.anchor <- pa;
7009 cbput state.hists.nav pa;
7010 true
7012 load1 f
7015 let add_attrs bb always dc c =
7016 let ob s a b =
7017 if always || a != b
7018 then Printf.bprintf bb "\n %s='%b'" s a
7019 and op s a b =
7020 if always || a <> b
7021 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7022 and oi s a b =
7023 if always || a != b
7024 then Printf.bprintf bb "\n %s='%d'" s a
7025 and oI s a b =
7026 if always || a != b
7027 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7028 and oz s a b =
7029 if always || a <> b
7030 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7031 and oF s a b =
7032 if always || a <> b
7033 then Printf.bprintf bb "\n %s='%f'" s a
7034 and oc s a b =
7035 if always || a <> b
7036 then
7037 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7038 and oC s a b =
7039 if always || a <> b
7040 then
7041 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7042 and oR s a b =
7043 if always || a <> b
7044 then
7045 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7046 and os s a b =
7047 if always || a <> b
7048 then
7049 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7050 and og s a b =
7051 if always || a <> b
7052 then
7053 match a with
7054 | None -> ()
7055 | Some (_N, _A, _B) ->
7056 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7057 and oW s a b =
7058 if always || a <> b
7059 then
7060 let v =
7061 match a with
7062 | None -> "false"
7063 | Some f ->
7064 if f = infinity
7065 then "true"
7066 else string_of_float f
7068 Printf.bprintf bb "\n %s='%s'" s v
7069 and oco s a b =
7070 if always || a <> b
7071 then
7072 match a with
7073 | Cmulti ((n, a, b), _) when n > 1 ->
7074 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7075 | Csplit (n, _) when n > 1 ->
7076 Printf.bprintf bb "\n %s='%d'" s ~-n
7077 | _ -> ()
7078 and obeco s a b =
7079 if always || a <> b
7080 then
7081 match a with
7082 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7083 | _ -> ()
7084 and oFm s a b =
7085 if always || a <> b
7086 then
7087 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7088 and oSv s a b m =
7089 if always || a <> b
7090 then
7091 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7092 and oPm s a b =
7093 if always || a <> b
7094 then
7095 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7097 oi "width" c.cwinw dc.cwinw;
7098 oi "height" c.cwinh dc.cwinh;
7099 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7100 oi "scroll-handle-height" c.scrollh dc.scrollh;
7101 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7102 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7103 ob "case-insensitive-search" c.icase dc.icase;
7104 ob "preload" c.preload dc.preload;
7105 oi "page-bias" c.pagebias dc.pagebias;
7106 oi "scroll-step" c.scrollstep dc.scrollstep;
7107 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7108 ob "max-height-fit" c.maxhfit dc.maxhfit;
7109 ob "crop-hack" c.crophack dc.crophack;
7110 oW "throttle" c.maxwait dc.maxwait;
7111 ob "highlight-links" c.hlinks dc.hlinks;
7112 ob "under-cursor-info" c.underinfo dc.underinfo;
7113 oi "vertical-margin" c.interpagespace dc.interpagespace;
7114 oz "zoom" c.zoom dc.zoom;
7115 ob "presentation" c.presentation dc.presentation;
7116 oi "rotation-angle" c.angle dc.angle;
7117 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7118 oFm "fit-model" c.fitmodel dc.fitmodel;
7119 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7120 oi "tex-count" c.texcount dc.texcount;
7121 oi "slice-height" c.sliceheight dc.sliceheight;
7122 oi "thumbnail-width" c.thumbw dc.thumbw;
7123 ob "persistent-location" c.jumpback dc.jumpback;
7124 oc "background-color" c.bgcolor dc.bgcolor;
7125 oi "tile-width" c.tilew dc.tilew;
7126 oi "tile-height" c.tileh dc.tileh;
7127 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7128 ob "checkers" c.checkers dc.checkers;
7129 oi "aalevel" c.aalevel dc.aalevel;
7130 ob "trim-margins" c.trimmargins dc.trimmargins;
7131 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7132 os "uri-launcher" c.urilauncher dc.urilauncher;
7133 os "path-launcher" c.pathlauncher dc.pathlauncher;
7134 oC "color-space" c.colorspace dc.colorspace;
7135 ob "invert-colors" c.invert dc.invert;
7136 oF "brightness" c.colorscale dc.colorscale;
7137 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7138 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7139 oco "columns" c.columns dc.columns;
7140 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7141 os "selection-command" c.selcmd dc.selcmd;
7142 os "synctex-command" c.stcmd dc.stcmd;
7143 os "pax-command" c.paxcmd dc.paxcmd;
7144 ob "update-cursor" c.updatecurs dc.updatecurs;
7145 oi "hint-font-size" c.hfsize dc.hfsize;
7146 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7147 oF "page-scroll-scale" c.pgscale dc.pgscale;
7148 ob "use-pbo" c.usepbo dc.usepbo;
7149 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7150 ob "remote-in-a-new-instance" c.riani dc.riani;
7151 op "point-and-x" c.pax dc.pax;
7152 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7155 let keymapsbuf always dc c =
7156 let bb = Buffer.create 16 in
7157 let rec loop = function
7158 | [] -> ()
7159 | (modename, h) :: rest ->
7160 let dh = findkeyhash dc modename in
7161 if always || h <> dh
7162 then (
7163 if Hashtbl.length h > 0
7164 then (
7165 if Buffer.length bb > 0
7166 then Buffer.add_char bb '\n';
7167 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7168 Hashtbl.iter (fun i o ->
7169 let isdifferent = always ||
7171 let dO = Hashtbl.find dh i in
7172 dO <> o
7173 with Not_found -> true
7175 if isdifferent
7176 then
7177 let addkm (k, m) =
7178 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7179 if Wsi.withalt m then Buffer.add_string bb "alt-";
7180 if Wsi.withshift m then Buffer.add_string bb "shift-";
7181 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7182 Buffer.add_string bb (Wsi.keyname k);
7184 let addkms l =
7185 let rec loop = function
7186 | [] -> ()
7187 | km :: [] -> addkm km
7188 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7190 loop l
7192 Buffer.add_string bb "<map in='";
7193 addkm i;
7194 match o with
7195 | KMinsrt km ->
7196 Buffer.add_string bb "' out='";
7197 addkm km;
7198 Buffer.add_string bb "'/>\n"
7200 | KMinsrl kms ->
7201 Buffer.add_string bb "' out='";
7202 addkms kms;
7203 Buffer.add_string bb "'/>\n"
7205 | KMmulti (ins, kms) ->
7206 Buffer.add_char bb ' ';
7207 addkms ins;
7208 Buffer.add_string bb "' out='";
7209 addkms kms;
7210 Buffer.add_string bb "'/>\n"
7211 ) h;
7212 Buffer.add_string bb "</keymap>";
7215 loop rest
7217 loop c.keyhashes;
7221 let save () =
7222 let uifontsize = fstate.fontsize in
7223 let bb = Buffer.create 32768 in
7224 let relx = float state.x /. float state.winw in
7225 let w, h, x =
7226 let cx w = truncate (relx *. float w) in
7227 List.fold_left
7228 (fun (w, h, x) ws ->
7229 match ws with
7230 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7231 | Wsi.MaxVert -> (w, conf.cwinh, x)
7232 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7234 (state.winw, state.winh, state.x) state.winstate
7236 conf.cwinw <- w;
7237 conf.cwinh <- h;
7238 let f (h, dc) =
7239 let dc = if conf.bedefault then conf else dc in
7240 Buffer.add_string bb "<llppconfig>\n";
7242 if String.length !fontpath > 0
7243 then
7244 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7245 uifontsize
7246 !fontpath
7247 else (
7248 if uifontsize <> 14
7249 then
7250 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7253 Buffer.add_string bb "<defaults ";
7254 add_attrs bb true dc dc;
7255 let kb = keymapsbuf true dc dc in
7256 if Buffer.length kb > 0
7257 then (
7258 Buffer.add_string bb ">\n";
7259 Buffer.add_buffer bb kb;
7260 Buffer.add_string bb "\n</defaults>\n";
7262 else Buffer.add_string bb "/>\n";
7264 let adddoc path pan anchor c bookmarks =
7265 if bookmarks == [] && c = dc && anchor = emptyanchor
7266 then ()
7267 else (
7268 Printf.bprintf bb "<doc path='%s'"
7269 (enent path 0 (String.length path));
7271 if anchor <> emptyanchor
7272 then (
7273 let n, rely, visy = anchor in
7274 Printf.bprintf bb " page='%d'" n;
7275 if rely > 1e-6
7276 then
7277 Printf.bprintf bb " rely='%f'" rely
7279 if abs_float visy > 1e-6
7280 then
7281 Printf.bprintf bb " visy='%f'" visy
7285 if pan != 0
7286 then Printf.bprintf bb " pan='%d'" pan;
7288 add_attrs bb false dc c;
7289 let kb = keymapsbuf false dc c in
7291 begin match bookmarks with
7292 | [] ->
7293 if Buffer.length kb > 0
7294 then (
7295 Buffer.add_string bb ">\n";
7296 Buffer.add_buffer bb kb;
7297 Buffer.add_string bb "\n</doc>\n";
7299 else Buffer.add_string bb "/>\n"
7300 | _ ->
7301 Buffer.add_string bb ">\n<bookmarks>\n";
7302 List.iter (fun (title, _level, (page, rely, visy)) ->
7303 Printf.bprintf bb
7304 "<item title='%s' page='%d'"
7305 (enent title 0 (String.length title))
7306 page
7308 if rely > 1e-6
7309 then
7310 Printf.bprintf bb " rely='%f'" rely
7312 if abs_float visy > 1e-6
7313 then
7314 Printf.bprintf bb " visy='%f'" visy
7316 Buffer.add_string bb "/>\n";
7317 ) bookmarks;
7318 Buffer.add_string bb "</bookmarks>";
7319 if Buffer.length kb > 0
7320 then (
7321 Buffer.add_string bb "\n";
7322 Buffer.add_buffer bb kb;
7324 Buffer.add_string bb "\n</doc>\n";
7325 end;
7329 let pan, conf =
7330 match state.mode with
7331 | Birdseye (c, pan, _, _, _) ->
7332 let beyecolumns =
7333 match conf.columns with
7334 | Cmulti ((c, _, _), _) -> Some c
7335 | Csingle _ -> None
7336 | Csplit _ -> None
7337 and columns =
7338 match c.columns with
7339 | Cmulti (c, _) -> Cmulti (c, [||])
7340 | Csingle _ -> Csingle [||]
7341 | Csplit _ -> failwith "quit from bird's eye while split"
7343 pan, { c with beyecolumns = beyecolumns; columns = columns }
7344 | _ -> x, conf
7346 let basename = Filename.basename
7347 (if String.length state.origin = 0 then state.path else state.origin)
7349 adddoc basename pan (getanchor ())
7350 (let conf =
7351 let autoscrollstep =
7352 match state.autoscroll with
7353 | Some step -> step
7354 | None -> conf.autoscrollstep
7356 match state.mode with
7357 | Birdseye (bc, _, _, _, _) ->
7358 { conf with
7359 zoom = bc.zoom;
7360 presentation = bc.presentation;
7361 interpagespace = bc.interpagespace;
7362 maxwait = bc.maxwait;
7363 autoscrollstep = autoscrollstep }
7364 | _ -> { conf with autoscrollstep = autoscrollstep }
7365 in conf)
7366 (if conf.savebmarks then state.bookmarks else []);
7368 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7369 if basename <> path
7370 then adddoc path x anchor c bookmarks
7371 ) h;
7372 Buffer.add_string bb "</llppconfig>\n";
7373 true;
7375 if load1 f && Buffer.length bb > 0
7376 then
7378 let tmp = !confpath ^ ".tmp" in
7379 let oc = open_out_bin tmp in
7380 Buffer.output_buffer oc bb;
7381 close_out oc;
7382 Unix.rename tmp !confpath;
7383 with exn ->
7384 prerr_endline
7385 ("error while saving configuration: " ^ exntos exn)
7387 end;;
7389 let adderrmsg src msg =
7390 Buffer.add_string state.errmsgs msg;
7391 state.newerrmsgs <- true;
7392 G.postRedisplay src
7395 let adderrfmt src fmt =
7396 Format.kprintf (fun s -> adderrmsg src s) fmt;
7399 let ract cmds =
7400 let cl = splitatspace cmds in
7401 let scan s fmt f =
7402 try Scanf.sscanf s fmt f
7403 with exn ->
7404 adderrfmt "remote exec"
7405 "error processing '%S': %s\n" cmds (exntos exn)
7407 match cl with
7408 | "reload" :: [] -> reload ()
7409 | "goto" :: args :: [] ->
7410 scan args "%u %f %f"
7411 (fun pageno x y ->
7412 let cmd, _ = state.geomcmds in
7413 if String.length cmd = 0
7414 then gotopagexy pageno x y
7415 else
7416 let f prevf () =
7417 gotopagexy pageno x y;
7418 prevf ()
7420 state.reprf <- f state.reprf
7422 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7423 | "rect" :: args :: [] ->
7424 scan args "%u %u %f %f %f %f"
7425 (fun pageno color x0 y0 x1 y1 ->
7426 onpagerect pageno (fun w h ->
7427 let _,w1,h1,_ = getpagedim pageno in
7428 let sw = float w1 /. w
7429 and sh = float h1 /. h in
7430 let x0s = x0 *. sw
7431 and x1s = x1 *. sw
7432 and y0s = y0 *. sh
7433 and y1s = y1 *. sh in
7434 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7435 debugrect rect;
7436 state.rects <- (pageno, color, rect) :: state.rects;
7437 G.postRedisplay "rect";
7440 | "activatewin" :: [] -> Wsi.activatewin ()
7441 | "quit" :: [] -> raise Quit
7442 | _ ->
7443 adderrfmt "remote command"
7444 "error processing remote command: %S\n" cmds;
7447 let remote =
7448 let scratch = String.create 80 in
7449 let buf = Buffer.create 80 in
7450 fun fd ->
7451 let rec tempfr () =
7452 try Some (Unix.read fd scratch 0 80)
7453 with
7454 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7455 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7456 | exn -> raise exn
7458 match tempfr () with
7459 | None -> Some fd
7460 | Some n ->
7461 if n = 0
7462 then (
7463 Unix.close fd;
7464 if Buffer.length buf > 0
7465 then (
7466 let s = Buffer.contents buf in
7467 Buffer.clear buf;
7468 ract s;
7470 None
7472 else
7473 let rec eat ppos =
7474 let nlpos =
7476 let pos = String.index_from scratch ppos '\n' in
7477 if pos >= n then -1 else pos
7478 with Not_found -> -1
7480 if nlpos >= 0
7481 then (
7482 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7483 let s = Buffer.contents buf in
7484 Buffer.clear buf;
7485 ract s;
7486 eat (nlpos+1);
7488 else (
7489 Buffer.add_substring buf scratch ppos (n-ppos);
7490 Some fd
7492 in eat 0
7495 let remoteopen path =
7496 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7497 with exn ->
7498 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7499 None
7502 let () =
7503 let trimcachepath = ref "" in
7504 let rcmdpath = ref "" in
7505 selfexec := Sys.executable_name;
7506 Arg.parse
7507 (Arg.align
7508 [("-p", Arg.String (fun s -> state.password <- s),
7509 "<password> Set password");
7511 ("-f", Arg.String
7512 (fun s ->
7513 Config.fontpath := s;
7514 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7516 "<path> Set path to the user interface font");
7518 ("-c", Arg.String
7519 (fun s ->
7520 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7521 Config.confpath := s),
7522 "<path> Set path to the configuration file");
7524 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7525 "<path> Set path to the trim cache file");
7527 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7528 "<named-destination> Set named destination");
7530 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7531 ("-cxack", Arg.Set cxack, " Cut corners");
7533 ("-remote", Arg.String (fun s -> rcmdpath := s),
7534 "<path> Set path to the remote commands source");
7536 ("-origin", Arg.String (fun s -> state.origin <- s),
7537 "<original-path> Set original path");
7539 ("-v", Arg.Unit (fun () ->
7540 Printf.printf
7541 "%s\nconfiguration path: %s\n"
7542 (version ())
7543 Config.defconfpath
7545 exit 0), " Print version and exit");
7548 (fun s -> state.path <- s)
7549 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7551 if !wtmode
7552 then selfexec := !selfexec ^ " -wtmode";
7554 if String.length state.path = 0
7555 then (prerr_endline "file name missing"; exit 1);
7557 if not (Config.load ())
7558 then prerr_endline "failed to load configuration";
7560 let wsfd, winw, winh = Wsi.init (object
7561 val mutable m_hack = false
7562 method expose = if not m_hack then G.postRedisplay "expose"
7563 method visible = G.postRedisplay "visible"
7564 method display = m_hack <- false; display ()
7565 method reshape w h =
7566 m_hack <- w < state.winw && h < state.winh;
7567 reshape w h
7568 method mouse b d x y m = mouse b d x y m
7569 method motion x y = state.mpos <- (x, y); motion x y
7570 method pmotion x y = state.mpos <- (x, y); pmotion x y
7571 method key k m =
7572 let mascm = m land (
7573 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7574 ) in
7575 match state.keystate with
7576 | KSnone ->
7577 let km = k, mascm in
7578 begin
7579 match
7580 let modehash = state.uioh#modehash in
7581 try Hashtbl.find modehash km
7582 with Not_found ->
7583 try Hashtbl.find (findkeyhash conf "global") km
7584 with Not_found -> KMinsrt (k, m)
7585 with
7586 | KMinsrt (k, m) -> keyboard k m
7587 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7588 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7590 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7591 List.iter (fun (k, m) -> keyboard k m) insrt;
7592 state.keystate <- KSnone
7593 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7594 state.keystate <- KSinto (keys, insrt)
7595 | _ ->
7596 state.keystate <- KSnone
7598 method enter x y = state.mpos <- (x, y); pmotion x y
7599 method leave = state.mpos <- (-1, -1)
7600 method winstate wsl = state.winstate <- wsl
7601 method quit = raise Quit
7602 end) conf.cwinw conf.cwinh (platform = Posx) in
7604 state.wsfd <- wsfd;
7606 if not (
7607 List.exists GlMisc.check_extension
7608 [ "GL_ARB_texture_rectangle"
7609 ; "GL_EXT_texture_recangle"
7610 ; "GL_NV_texture_rectangle" ]
7612 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7614 if (
7615 let r = GlMisc.get_string `renderer in
7616 let p = "Mesa DRI Intel(" in
7617 let l = String.length p in
7618 String.length r > l && String.sub r 0 l = p
7620 then (
7621 defconf.sliceheight <- 1024;
7622 defconf.texcount <- 32;
7623 defconf.usepbo <- true;
7626 let cr, sw =
7627 match Ne.pipe () with
7628 | Ne.Exn exn ->
7629 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7630 exit 1
7631 | Ne.Res rw -> rw
7632 and sr, cw =
7633 match Ne.pipe () with
7634 | Ne.Exn exn ->
7635 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7636 exit 1
7637 | Ne.Res rw -> rw
7640 cloexec cr;
7641 cloexec sw;
7642 cloexec sr;
7643 cloexec cw;
7645 setcheckers conf.checkers;
7646 redirectstderr ();
7647 if conf.redirectstderr
7648 then
7649 at_exit (fun () ->
7650 let s = Buffer.contents state.errmsgs ^
7651 (match state.errfd with
7652 | Some fd ->
7653 let s = String.create (80*24) in
7654 let n =
7656 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7657 if List.mem fd r
7658 then Unix.read fd s 0 (String.length s)
7659 else 0
7660 with _ -> 0
7662 if n = 0
7663 then ""
7664 else String.sub s 0 n
7665 | None -> ""
7668 try ignore (Unix.write state.stderr s 0 (String.length s))
7669 with exn -> print_endline (exntos exn)
7673 init (cr, cw) (
7674 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7675 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7676 !Config.fontpath, !trimcachepath,
7677 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7679 state.sr <- sr;
7680 state.sw <- sw;
7681 state.text <- "Opening " ^ (mbtoutf8 state.path);
7682 reshape winw winh;
7683 opendoc state.path state.password;
7684 state.uioh <- uioh;
7685 display ();
7686 Wsi.mapwin ();
7687 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7688 let optrfd =
7689 ref (
7690 if String.length !rcmdpath > 0
7691 then remoteopen !rcmdpath
7692 else None
7696 let rec loop deadline =
7697 let r =
7698 match state.errfd with
7699 | None -> [state.sr; state.wsfd]
7700 | Some fd -> [state.sr; state.wsfd; fd]
7702 let r =
7703 match !optrfd with
7704 | None -> r
7705 | Some fd -> fd :: r
7707 if state.redisplay
7708 then (
7709 state.redisplay <- false;
7710 display ();
7712 let timeout =
7713 let now = now () in
7714 if deadline > now
7715 then (
7716 if deadline = infinity
7717 then ~-.1.0
7718 else max 0.0 (deadline -. now)
7720 else 0.0
7722 let r, _, _ =
7723 try Unix.select r [] [] timeout
7724 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7726 begin match r with
7727 | [] ->
7728 state.ghyll None;
7729 let newdeadline =
7730 if state.ghyll == noghyll
7731 then
7732 match state.autoscroll with
7733 | Some step when step != 0 ->
7734 let y = state.y + step in
7735 let y =
7736 if y < 0
7737 then state.maxy
7738 else if y >= state.maxy then 0 else y
7740 gotoy y;
7741 if state.mode = View
7742 then state.text <- "";
7743 deadline +. 0.01
7744 | _ -> infinity
7745 else deadline +. 0.01
7747 loop newdeadline
7749 | l ->
7750 let rec checkfds = function
7751 | [] -> ()
7752 | fd :: rest when fd = state.sr ->
7753 let cmd = readcmd state.sr in
7754 act cmd;
7755 checkfds rest
7757 | fd :: rest when fd = state.wsfd ->
7758 Wsi.readresp fd;
7759 checkfds rest
7761 | fd :: rest when Some fd = !optrfd ->
7762 begin match remote fd with
7763 | None -> optrfd := remoteopen !rcmdpath;
7764 | opt -> optrfd := opt
7765 end;
7766 checkfds rest
7768 | fd :: rest ->
7769 let s = String.create 80 in
7770 let n = tempfailureretry (Unix.read fd s 0) 80 in
7771 if conf.redirectstderr
7772 then (
7773 Buffer.add_substring state.errmsgs s 0 n;
7774 state.newerrmsgs <- true;
7775 state.redisplay <- true;
7777 else (
7778 prerr_string (String.sub s 0 n);
7779 flush stderr;
7781 checkfds rest
7783 checkfds l;
7784 let newdeadline =
7785 let deadline1 =
7786 if deadline = infinity
7787 then now () +. 0.01
7788 else deadline
7790 match state.autoscroll with
7791 | Some step when step != 0 -> deadline1
7792 | _ -> if state.ghyll == noghyll then infinity else deadline1
7794 loop newdeadline
7795 end;
7798 loop infinity;
7799 with Quit ->
7800 Config.save ();