Handle compressed pngs and jpegs (wtf?) via convert
[llpp.git] / main.ml
blob4cdc7f43f0f02ae8e116fc4c57849d48b41803d0
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 if Array.length b = 0
1415 then []
1416 else List.rev (fold [] (page_of_y y))
1419 let layoutS (columns, b) y sh =
1420 let sh = sh - hscrollh () in
1421 let rec fold accu n =
1422 if n = Array.length b
1423 then accu
1424 else
1425 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1426 if (vy - y) > sh
1427 then accu
1428 else
1429 let accu =
1430 if vy + pageh > y
1431 then
1432 let x = xoff + state.x in
1433 let pagey = max 0 (y - vy) in
1434 let pagedispy = if pagey > 0 then 0 else vy - y in
1435 let pagedispx, pagex =
1436 if px = 0
1437 then (
1438 if x < 0
1439 then 0, -x
1440 else x, 0
1442 else (
1443 let px = px - x in
1444 if px < 0
1445 then -px, 0
1446 else 0, px
1449 let pagecolw = pagew/columns in
1450 let pagedispx =
1451 if pagecolw < state.winw
1452 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1453 else pagedispx
1455 let pagevw =
1456 let vw = wadjsb state.winw - pagedispx in
1457 let pw = pagew - pagex in
1458 min vw pw
1460 let pagevw = min pagevw pagecolw in
1461 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1462 if pagevw > 0 && pagevh > 0
1463 then
1464 let e =
1465 { pageno = n/columns
1466 ; pagedimno = pdimno
1467 ; pagew = pagew
1468 ; pageh = pageh
1469 ; pagex = pagex
1470 ; pagey = pagey
1471 ; pagevw = pagevw
1472 ; pagevh = pagevh
1473 ; pagedispx = pagedispx
1474 ; pagedispy = pagedispy
1475 ; pagecol = n mod columns
1478 e :: accu
1479 else
1480 accu
1481 else
1482 accu
1484 fold accu (n+1)
1486 List.rev (fold [] 0)
1489 let layout y sh =
1490 if nogeomcmds state.geomcmds
1491 then
1492 match conf.columns with
1493 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1494 | Cmulti c -> layoutN c y sh
1495 | Csplit s -> layoutS s y sh
1496 else []
1499 let clamp incr =
1500 let y = state.y + incr in
1501 let y = max 0 y in
1502 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1506 let itertiles l f =
1507 let tilex = l.pagex mod conf.tilew in
1508 let tiley = l.pagey mod conf.tileh in
1510 let col = l.pagex / conf.tilew in
1511 let row = l.pagey / conf.tileh in
1513 let rec rowloop row y0 dispy h =
1514 if h = 0
1515 then ()
1516 else (
1517 let dh = conf.tileh - y0 in
1518 let dh = min h dh in
1519 let rec colloop col x0 dispx w =
1520 if w = 0
1521 then ()
1522 else (
1523 let dw = conf.tilew - x0 in
1524 let dw = min w dw in
1526 f col row dispx dispy x0 y0 dw dh;
1527 colloop (col+1) 0 (dispx+dw) (w-dw)
1530 colloop col tilex l.pagedispx l.pagevw;
1531 rowloop (row+1) 0 (dispy+dh) (h-dh)
1534 if l.pagevw > 0 && l.pagevh > 0
1535 then rowloop row tiley l.pagedispy l.pagevh;
1538 let gettileopaque l col row =
1539 let key =
1540 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1542 try Some (Hashtbl.find state.tilemap key)
1543 with Not_found -> None
1546 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1547 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1548 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1551 let drawtiles l color =
1552 GlDraw.color color;
1553 let f col row x y tilex tiley w h =
1554 match gettileopaque l col row with
1555 | Some (opaque, _, t) ->
1556 let params = x, y, w, h, tilex, tiley in
1557 if conf.invert
1558 then (
1559 Gl.enable `blend;
1560 GlFunc.blend_func `zero `one_minus_src_color;
1562 drawtile params opaque;
1563 if conf.invert
1564 then Gl.disable `blend;
1565 if conf.debug
1566 then (
1567 let s = Printf.sprintf
1568 "%d[%d,%d] %f sec"
1569 l.pageno col row t
1571 let w = measurestr fstate.fontsize s in
1572 GlMisc.push_attrib [`current];
1573 GlDraw.color (0.0, 0.0, 0.0);
1574 GlDraw.rect
1575 (float (x-2), float (y-2))
1576 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1577 GlDraw.color (1.0, 1.0, 1.0);
1578 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1579 GlMisc.pop_attrib ();
1582 | _ ->
1583 let w =
1584 let lw = wadjsb state.winw - x in
1585 min lw w
1586 and h =
1587 let lh = state.winh - y in
1588 min lh h
1590 begin match state.texid with
1591 | Some id ->
1592 Gl.enable `texture_2d;
1593 GlTex.bind_texture `texture_2d id;
1594 let x0 = float x
1595 and y0 = float y
1596 and x1 = float (x+w)
1597 and y1 = float (y+h) in
1599 let tw = float w /. 16.0
1600 and th = float h /. 16.0 in
1601 let tx0 = float tilex /. 16.0
1602 and ty0 = float tiley /. 16.0 in
1603 let tx1 = tx0 +. tw
1604 and ty1 = ty0 +. th in
1605 GlDraw.begins `quads;
1606 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1607 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1608 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1609 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1610 GlDraw.ends ();
1612 Gl.disable `texture_2d;
1613 | None ->
1614 GlDraw.color (1.0, 1.0, 1.0);
1615 GlDraw.rect
1616 (float x, float y)
1617 (float (x+w), float (y+h));
1618 end;
1619 if w > 128 && h > fstate.fontsize + 10
1620 then (
1621 GlDraw.color (0.0, 0.0, 0.0);
1622 let c, r =
1623 if conf.verbose
1624 then (col*conf.tilew, row*conf.tileh)
1625 else col, row
1627 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1629 GlDraw.color color;
1631 itertiles l f
1634 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1636 let tilevisible1 l x y =
1637 let ax0 = l.pagex
1638 and ax1 = l.pagex + l.pagevw
1639 and ay0 = l.pagey
1640 and ay1 = l.pagey + l.pagevh in
1642 let bx0 = x
1643 and by0 = y in
1644 let bx1 = min (bx0 + conf.tilew) l.pagew
1645 and by1 = min (by0 + conf.tileh) l.pageh in
1647 let rx0 = max ax0 bx0
1648 and ry0 = max ay0 by0
1649 and rx1 = min ax1 bx1
1650 and ry1 = min ay1 by1 in
1652 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1653 nonemptyintersection
1656 let tilevisible layout n x y =
1657 let rec findpageinlayout m = function
1658 | l :: rest when l.pageno = n ->
1659 tilevisible1 l x y || (
1660 match conf.columns with
1661 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1662 | _ -> false
1664 | _ :: rest -> findpageinlayout 0 rest
1665 | [] -> false
1667 findpageinlayout 0 layout;
1670 let tileready l x y =
1671 tilevisible1 l x y &&
1672 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1675 let tilepage n p layout =
1676 let rec loop = function
1677 | l :: rest ->
1678 if l.pageno = n
1679 then
1680 let f col row _ _ _ _ _ _ =
1681 if state.currently = Idle
1682 then
1683 match gettileopaque l col row with
1684 | Some _ -> ()
1685 | None ->
1686 let x = col*conf.tilew
1687 and y = row*conf.tileh in
1688 let w =
1689 let w = l.pagew - x in
1690 min w conf.tilew
1692 let h =
1693 let h = l.pageh - y in
1694 min h conf.tileh
1696 let pbo =
1697 if conf.usepbo
1698 then getpbo w h conf.colorspace
1699 else "0"
1701 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1702 state.currently <-
1703 Tiling (
1704 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1705 conf.tilew, conf.tileh
1708 itertiles l f;
1709 else
1710 loop rest
1712 | [] -> ()
1714 if nogeomcmds state.geomcmds
1715 then loop layout;
1718 let preloadlayout y =
1719 let y = if y < state.winh then 0 else y - state.winh in
1720 let h = state.winh*3 in
1721 layout y h;
1724 let load pages =
1725 let rec loop pages =
1726 if state.currently != Idle
1727 then ()
1728 else
1729 match pages with
1730 | l :: rest ->
1731 begin match getopaque l.pageno with
1732 | None ->
1733 wcmd "page %d %d" l.pageno l.pagedimno;
1734 state.currently <- Loading (l, state.gen);
1735 | Some opaque ->
1736 tilepage l.pageno opaque pages;
1737 loop rest
1738 end;
1739 | _ -> ()
1741 if nogeomcmds state.geomcmds
1742 then loop pages
1745 let preload pages =
1746 load pages;
1747 if conf.preload && state.currently = Idle
1748 then load (preloadlayout state.y);
1751 let layoutready layout =
1752 let rec fold all ls =
1753 all && match ls with
1754 | l :: rest ->
1755 let seen = ref false in
1756 let allvisible = ref true in
1757 let foo col row _ _ _ _ _ _ =
1758 seen := true;
1759 allvisible := !allvisible &&
1760 begin match gettileopaque l col row with
1761 | Some _ -> true
1762 | None -> false
1765 itertiles l foo;
1766 fold (!seen && !allvisible) rest
1767 | [] -> true
1769 let alltilesvisible = fold true layout in
1770 alltilesvisible;
1773 let gotoy y =
1774 let y = bound y 0 state.maxy in
1775 let y, layout, proceed =
1776 match conf.maxwait with
1777 | Some time when state.ghyll == noghyll ->
1778 begin match state.throttle with
1779 | None ->
1780 let layout = layout y state.winh in
1781 let ready = layoutready layout in
1782 if not ready
1783 then (
1784 load layout;
1785 state.throttle <- Some (layout, y, now ());
1787 else G.postRedisplay "gotoy showall (None)";
1788 y, layout, ready
1789 | Some (_, _, started) ->
1790 let dt = now () -. started in
1791 if dt > time
1792 then (
1793 state.throttle <- None;
1794 let layout = layout y state.winh in
1795 load layout;
1796 G.postRedisplay "maxwait";
1797 y, layout, true
1799 else -1, [], false
1802 | _ ->
1803 let layout = layout y state.winh in
1804 if not !wtmode || layoutready layout
1805 then G.postRedisplay "gotoy ready";
1806 y, layout, true
1808 if proceed
1809 then (
1810 state.y <- y;
1811 state.layout <- layout;
1812 begin match state.mode with
1813 | LinkNav (Ltexact (pageno, linkno)) ->
1814 let rec loop = function
1815 | [] ->
1816 state.mode <- LinkNav (Ltgendir 0)
1817 | l :: _ when l.pageno = pageno ->
1818 begin match getopaque pageno with
1819 | None ->
1820 state.mode <- LinkNav (Ltgendir 0)
1821 | Some opaque ->
1822 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1823 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1824 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1825 then state.mode <- LinkNav (Ltgendir 0)
1827 | _ :: rest -> loop rest
1829 loop layout
1830 | _ -> ()
1831 end;
1832 begin match state.mode with
1833 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1834 if not (pagevisible layout pageno)
1835 then (
1836 match state.layout with
1837 | [] -> ()
1838 | l :: _ ->
1839 state.mode <- Birdseye (
1840 conf, leftx, l.pageno, hooverpageno, anchor
1843 | LinkNav (Ltgendir dir as lt) ->
1844 let linknav =
1845 let rec loop = function
1846 | [] -> lt
1847 | l :: rest ->
1848 match getopaque l.pageno with
1849 | None -> loop rest
1850 | Some opaque ->
1851 let link =
1852 let ld =
1853 if dir = 0
1854 then LDfirstvisible (l.pagex, l.pagey, dir)
1855 else (
1856 if dir > 0 then LDfirst else LDlast
1859 findlink opaque ld
1861 match link with
1862 | Lnotfound -> loop rest
1863 | Lfound n ->
1864 showlinktype (getlink opaque n);
1865 Ltexact (l.pageno, n)
1867 loop state.layout
1869 state.mode <- LinkNav linknav
1870 | _ -> ()
1871 end;
1872 preload layout;
1874 state.ghyll <- noghyll;
1875 if conf.updatecurs
1876 then (
1877 let mx, my = state.mpos in
1878 updateunder mx my;
1882 let conttiling pageno opaque =
1883 tilepage pageno opaque
1884 (if conf.preload then preloadlayout state.y else state.layout)
1887 let gotoy_and_clear_text y =
1888 if not conf.verbose then state.text <- "";
1889 gotoy y;
1892 let getanchor1 l =
1893 let top =
1894 let coloff = l.pagecol * l.pageh in
1895 float (l.pagey + coloff) /. float l.pageh
1897 let dtop =
1898 if l.pagedispy = 0
1899 then
1901 else (
1902 if conf.presentation
1903 then float l.pagedispy /. float (calcips l.pageh)
1904 else float l.pagedispy /. float conf.interpagespace
1907 (l.pageno, top, dtop)
1910 let getanchor () =
1911 match state.layout with
1912 | l :: _ -> getanchor1 l
1913 | [] ->
1914 let n = page_of_y state.y in
1915 if n = -1
1916 then state.anchor
1917 else
1918 let y, h = getpageyh n in
1919 let dy = y - state.y in
1920 let dtop =
1921 if conf.presentation
1922 then
1923 let ips = calcips h in
1924 float (dy + ips) /. float ips
1925 else
1926 float dy /. float conf.interpagespace
1928 (n, 0.0, dtop)
1931 let getanchory (n, top, dtop) =
1932 let y, h = getpageyh n in
1933 if conf.presentation
1934 then
1935 let ips = calcips h in
1936 y + truncate (top*.float h -. dtop*.float ips) + ips;
1937 else
1938 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1941 let gotoanchor anchor =
1942 gotoy (getanchory anchor);
1945 let addnav () =
1946 cbput state.hists.nav (getanchor ());
1949 let getnav dir =
1950 let anchor = cbgetc state.hists.nav dir in
1951 getanchory anchor;
1954 let gotoghyll y =
1955 let scroll f n a b =
1956 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1957 let snake f a b =
1958 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1959 if f < a
1960 then s (float f /. float a)
1961 else (
1962 if f > b
1963 then 1.0 -. s ((float (f-b) /. float (n-b)))
1964 else 1.0
1967 snake f a b
1968 and summa f n a b =
1969 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1970 let iv x = x**3.-.0.5*.x**4. in
1971 let iv1 = iv f in
1972 let ins = float a *. iv1
1973 and outs = float (n-b) *. iv1 in
1974 let ones = b - a in
1975 ins +. outs +. float ones
1977 let rec set (_N, _A, _B) y sy =
1978 let sum = summa 1.0 _N _A _B in
1979 let dy = float (y - sy) in
1980 state.ghyll <- (
1981 let rec gf n y1 o =
1982 if n >= _N
1983 then state.ghyll <- noghyll
1984 else
1985 let go n =
1986 let s = scroll n _N _A _B in
1987 let y1 = y1 +. ((s *. dy) /. sum) in
1988 gotoy_and_clear_text (truncate y1);
1989 state.ghyll <- gf (n+1) y1;
1991 match o with
1992 | None -> go n
1993 | Some y' -> set (_N/2, 1, 1) y' state.y
1995 gf 0 (float state.y)
1998 match conf.ghyllscroll with
1999 | None ->
2000 gotoy_and_clear_text y
2001 | Some nab ->
2002 if state.ghyll == noghyll
2003 then set nab y state.y
2004 else state.ghyll (Some y)
2007 let gotopage n top =
2008 let y, h = getpageyh n in
2009 let y = y + (truncate (top *. float h)) in
2010 gotoghyll y
2013 let gotopage1 n top =
2014 let y = getpagey n in
2015 let y = y + top in
2016 gotoghyll y
2019 let invalidate s f =
2020 state.layout <- [];
2021 state.pdims <- [];
2022 state.rects <- [];
2023 state.rects1 <- [];
2024 match state.geomcmds with
2025 | ps, [] when String.length ps = 0 ->
2026 f ();
2027 state.geomcmds <- s, [];
2029 | ps, [] ->
2030 state.geomcmds <- ps, [s, f];
2032 | ps, (s', _) :: rest when s' = s ->
2033 state.geomcmds <- ps, ((s, f) :: rest);
2035 | ps, cmds ->
2036 state.geomcmds <- ps, ((s, f) :: cmds);
2039 let flushpages () =
2040 Hashtbl.iter (fun _ opaque ->
2041 wcmd "freepage %s" opaque;
2042 ) state.pagemap;
2043 Hashtbl.clear state.pagemap;
2046 let flushtiles () =
2047 if not (Queue.is_empty state.tilelru)
2048 then (
2049 Queue.iter (fun (k, p, s) ->
2050 wcmd "freetile %s" p;
2051 state.memused <- state.memused - s;
2052 Hashtbl.remove state.tilemap k;
2053 ) state.tilelru;
2054 state.uioh#infochanged Memused;
2055 Queue.clear state.tilelru;
2057 load state.layout;
2060 let stateh h =
2061 let h = truncate (float h*.conf.zoom) in
2062 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2063 h - d
2066 let opendoc path password =
2067 state.path <- path;
2068 state.password <- password;
2069 state.gen <- state.gen + 1;
2070 state.docinfo <- [];
2072 flushpages ();
2073 setaalevel conf.aalevel;
2074 let titlepath =
2075 if String.length state.origin = 0
2076 then path
2077 else state.origin
2079 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2080 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2081 invalidate "reqlayout"
2082 (fun () ->
2083 wcmd "reqlayout %d %d %d %s\000"
2084 conf.angle (FMTE.to_int conf.fitmodel)
2085 (stateh state.winh) state.nameddest
2089 let reload () =
2090 state.anchor <- getanchor ();
2091 opendoc state.path state.password;
2094 let scalecolor c =
2095 let c = c *. conf.colorscale in
2096 (c, c, c);
2099 let scalecolor2 (r, g, b) =
2100 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2103 let docolumns = function
2104 | Csingle _ ->
2105 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2106 let rec loop pageno pdimno pdim y ph pdims =
2107 if pageno = state.pagecount
2108 then ()
2109 else
2110 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2111 match pdims with
2112 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2113 pdimno+1, pdim, rest
2114 | _ ->
2115 pdimno, pdim, pdims
2117 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2118 let y = y +
2119 (if conf.presentation
2120 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2121 else (if pageno = 0 then 0 else conf.interpagespace)
2124 a.(pageno) <- (pdimno, x, y, pdim);
2125 loop (pageno+1) pdimno pdim (y + h) h pdims
2127 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2128 conf.columns <- Csingle a;
2130 | Cmulti ((columns, coverA, coverB), _) ->
2131 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2132 let rec loop pageno pdimno pdim x y rowh pdims =
2133 let rec fixrow m = if m = pageno then () else
2134 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2135 if h < rowh
2136 then (
2137 let y = y + (rowh - h) / 2 in
2138 a.(m) <- (pdimno, x, y, pdim);
2140 fixrow (m+1)
2142 if pageno = state.pagecount
2143 then fixrow (((pageno - 1) / columns) * columns)
2144 else
2145 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2146 match pdims with
2147 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2148 pdimno+1, pdim, rest
2149 | _ ->
2150 pdimno, pdim, pdims
2152 let x, y, rowh' =
2153 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2154 then (
2155 let x = (wadjsb state.winw - w) / 2 in
2156 let ips =
2157 if conf.presentation then calcips h else conf.interpagespace in
2158 x, y + ips + rowh, h
2160 else (
2161 if (pageno - coverA) mod columns = 0
2162 then (
2163 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2164 let y =
2165 if conf.presentation
2166 then
2167 let ips = calcips h in
2168 y + (if pageno = 0 then 0 else calcips rowh + ips)
2169 else
2170 y + (if pageno = 0 then 0 else conf.interpagespace)
2172 x, y + rowh, h
2174 else x, y, max rowh h
2177 let y =
2178 if pageno > 1 && (pageno - coverA) mod columns = 0
2179 then (
2180 let y =
2181 if pageno = columns && conf.presentation
2182 then (
2183 let ips = calcips rowh in
2184 for i = 0 to pred columns
2186 let (pdimno, x, y, pdim) = a.(i) in
2187 a.(i) <- (pdimno, x, y+ips, pdim)
2188 done;
2189 y+ips;
2191 else y
2193 fixrow (pageno - columns);
2196 else y
2198 a.(pageno) <- (pdimno, x, y, pdim);
2199 let x = x + w + xoff*2 + conf.interpagespace in
2200 loop (pageno+1) pdimno pdim x y rowh' pdims
2202 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2203 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2205 | Csplit (c, _) ->
2206 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2207 let rec loop pageno pdimno pdim y pdims =
2208 if pageno = state.pagecount
2209 then ()
2210 else
2211 let pdimno, ((_, w, h, _) as pdim), pdims =
2212 match pdims with
2213 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2214 pdimno+1, pdim, rest
2215 | _ ->
2216 pdimno, pdim, pdims
2218 let cw = w / c in
2219 let rec loop1 n x y =
2220 if n = c then y else (
2221 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2222 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2225 let y = loop1 0 0 y in
2226 loop (pageno+1) pdimno pdim y pdims
2228 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2229 conf.columns <- Csplit (c, a);
2232 let represent () =
2233 docolumns conf.columns;
2234 state.maxy <- calcheight ();
2235 if state.reprf == noreprf
2236 then (
2237 match state.mode with
2238 | Birdseye (_, _, pageno, _, _) ->
2239 let y, h = getpageyh pageno in
2240 let top = (state.winh - h) / 2 in
2241 gotoy (max 0 (y - top))
2242 | _ -> gotoanchor state.anchor
2244 else (
2245 state.reprf ();
2246 state.reprf <- noreprf;
2250 let reshape w h =
2251 GlDraw.viewport 0 0 w h;
2252 let firsttime = state.geomcmds == firstgeomcmds in
2253 if not firsttime && nogeomcmds state.geomcmds
2254 then state.anchor <- getanchor ();
2256 state.winw <- w;
2257 let w = wadjsb (truncate (float w *. conf.zoom)) in
2258 let w = max w 2 in
2259 state.winh <- h;
2260 setfontsize fstate.fontsize;
2261 GlMat.mode `modelview;
2262 GlMat.load_identity ();
2264 GlMat.mode `projection;
2265 GlMat.load_identity ();
2266 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2267 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2268 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2270 let relx =
2271 if conf.zoom <= 1.0
2272 then 0.0
2273 else float state.x /. float state.w
2275 invalidate "geometry"
2276 (fun () ->
2277 state.w <- w;
2278 if not firsttime
2279 then state.x <- truncate (relx *. float w);
2280 let w =
2281 match conf.columns with
2282 | Csingle _ -> w
2283 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2284 | Csplit (c, _) -> w * c
2286 wcmd "geometry %d %d %d"
2287 w (stateh h) (FMTE.to_int conf.fitmodel)
2291 let enttext () =
2292 let len = String.length state.text in
2293 let drawstring s =
2294 let hscrollh =
2295 match state.mode with
2296 | Textentry _ | View | LinkNav _ ->
2297 let h, _, _ = state.uioh#scrollpw in
2299 | _ -> 0
2301 let rect x w =
2302 GlDraw.rect
2303 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2304 (x+.w, float (state.winh - hscrollh))
2307 let w = float (wadjsb state.winw - 1) in
2308 if state.progress >= 0.0 && state.progress < 1.0
2309 then (
2310 GlDraw.color (0.3, 0.3, 0.3);
2311 let w1 = w *. state.progress in
2312 rect 0.0 w1;
2313 GlDraw.color (0.0, 0.0, 0.0);
2314 rect w1 (w-.w1)
2316 else (
2317 GlDraw.color (0.0, 0.0, 0.0);
2318 rect 0.0 w;
2321 GlDraw.color (1.0, 1.0, 1.0);
2322 drawstring fstate.fontsize
2323 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2325 let s =
2326 match state.mode with
2327 | Textentry ((prefix, text, _, _, _, _), _) ->
2328 let s =
2329 if len > 0
2330 then
2331 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2332 else
2333 Printf.sprintf "%s%s_" prefix text
2337 | _ -> state.text
2339 let s =
2340 if state.newerrmsgs
2341 then (
2342 if not (istextentry state.mode) && state.uioh#eformsgs
2343 then
2344 let s1 = "(press 'e' to review error messasges)" in
2345 if String.length s > 0 then s ^ " " ^ s1 else s1
2346 else s
2348 else s
2350 if String.length s > 0
2351 then drawstring s
2354 let gctiles () =
2355 let len = Queue.length state.tilelru in
2356 let layout = lazy (
2357 match state.throttle with
2358 | None ->
2359 if conf.preload
2360 then preloadlayout state.y
2361 else state.layout
2362 | Some (layout, _, _) ->
2363 layout
2364 ) in
2365 let rec loop qpos =
2366 if state.memused <= conf.memlimit
2367 then ()
2368 else (
2369 if qpos < len
2370 then
2371 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2372 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2373 let (_, pw, ph, _) = getpagedim n in
2375 gen = state.gen
2376 && colorspace = conf.colorspace
2377 && angle = conf.angle
2378 && pagew = pw
2379 && pageh = ph
2380 && (
2381 let x = col*conf.tilew
2382 and y = row*conf.tileh in
2383 tilevisible (Lazy.force_val layout) n x y
2385 then Queue.push lruitem state.tilelru
2386 else (
2387 freepbo p;
2388 wcmd "freetile %s" p;
2389 state.memused <- state.memused - s;
2390 state.uioh#infochanged Memused;
2391 Hashtbl.remove state.tilemap k;
2393 loop (qpos+1)
2396 loop 0
2399 let logcurrently = function
2400 | Idle -> dolog "Idle"
2401 | Loading (l, gen) ->
2402 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2403 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2404 dolog
2405 "Tiling %d[%d,%d] page=%s cs=%s angle"
2406 l.pageno col row pageopaque
2407 (CSTE.to_string colorspace)
2409 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2410 angle gen conf.angle state.gen
2411 tilew tileh
2412 conf.tilew conf.tileh
2414 | Outlining _ ->
2415 dolog "outlining"
2418 let splitatspace =
2419 let r = Str.regexp " " in
2420 fun s -> Str.bounded_split r s 2;
2423 let onpagerect pageno f =
2424 let b =
2425 match conf.columns with
2426 | Cmulti (_, b) -> b
2427 | Csingle b -> b
2428 | Csplit (_, b) -> b
2430 if pageno >= 0 && pageno < Array.length b
2431 then
2432 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2433 let r = getpdimrect pdimno in
2434 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2437 let gotopagexy1 pageno x y =
2438 onpagerect pageno (fun w h ->
2439 let top = y /. h in
2440 let _,w1,_,leftx = getpagedim pageno in
2441 let wh = state.winh - hscrollh () in
2442 let sw = float w1 /. w in
2443 let x = sw *. x in
2444 let x = leftx + state.x + truncate x in
2445 let sx =
2446 if x < 0 || x >= wadjsb state.winw
2447 then state.x - x
2448 else state.x
2450 let py, h = getpageyh pageno in
2451 let pdy = truncate (top *. float h) in
2452 let y' = py + pdy in
2453 let dy = y' - state.y in
2454 let sy =
2455 if x != state.x || not (dy > 0 && dy < wh)
2456 then (
2457 if conf.presentation
2458 then
2459 if abs (py - y') > wh
2460 then y'
2461 else py
2462 else y';
2464 else state.y
2466 if state.x != sx || state.y != sy
2467 then (
2468 let x, y =
2469 if !wtmode
2470 then (
2471 let ww = wadjsb state.winw in
2472 let qx = sx / ww
2473 and qy = pdy / wh in
2474 let x = qx * ww
2475 and y = py + qy * wh in
2476 let x = if -x + ww > w1 then -(w1-ww) else x
2477 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2478 let y =
2479 if conf.presentation
2480 then
2481 if abs (py - y') > wh
2482 then y'
2483 else py
2484 else y';
2486 (x, y)
2488 else (sx, sy)
2490 state.x <- x;
2491 gotoy_and_clear_text y;
2493 else gotoy_and_clear_text state.y;
2497 let gotopagexy pageno x y =
2498 match state.mode with
2499 | Birdseye _ -> gotopage pageno 0.0
2500 | _ -> gotopagexy1 pageno x y
2503 let act cmds =
2504 (* dolog "%S" cmds; *)
2505 let cl = splitatspace cmds in
2506 let scan s fmt f =
2507 try Scanf.sscanf s fmt f
2508 with exn ->
2509 dolog "error processing '%S': %s" cmds (exntos exn);
2510 exit 1
2512 match cl with
2513 | "clear" :: [] ->
2514 state.uioh#infochanged Pdim;
2515 state.pdims <- [];
2517 | "clearrects" :: [] ->
2518 state.rects <- state.rects1;
2519 G.postRedisplay "clearrects";
2521 | "continue" :: args :: [] ->
2522 let n = scan args "%u" (fun n -> n) in
2523 state.pagecount <- n;
2524 begin match state.currently with
2525 | Outlining l ->
2526 state.currently <- Idle;
2527 state.outlines <- Array.of_list (List.rev l)
2528 | _ -> ()
2529 end;
2531 let cur, cmds = state.geomcmds in
2532 if String.length cur = 0
2533 then failwith "umpossible";
2535 begin match List.rev cmds with
2536 | [] ->
2537 state.geomcmds <- "", [];
2538 represent ();
2539 | (s, f) :: rest ->
2540 f ();
2541 state.geomcmds <- s, List.rev rest;
2542 end;
2543 if conf.maxwait = None && not !wtmode
2544 then G.postRedisplay "continue";
2546 | "title" :: args :: [] ->
2547 Wsi.settitle args
2549 | "msg" :: args :: [] ->
2550 showtext ' ' args
2552 | "vmsg" :: args :: [] ->
2553 if conf.verbose
2554 then showtext ' ' args
2556 | "emsg" :: args :: [] ->
2557 Buffer.add_string state.errmsgs args;
2558 state.newerrmsgs <- true;
2559 G.postRedisplay "error message"
2561 | "progress" :: args :: [] ->
2562 let progress, text =
2563 scan args "%f %n"
2564 (fun f pos ->
2565 f, String.sub args pos (String.length args - pos))
2567 state.text <- text;
2568 state.progress <- progress;
2569 G.postRedisplay "progress"
2571 | "firstmatch" :: args :: [] ->
2572 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2573 scan args "%u %d %f %f %f %f %f %f %f %f"
2574 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2575 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2577 let y = (getpagey pageno) + truncate y0 in
2578 addnav ();
2579 gotoy y;
2580 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2582 | "match" :: args :: [] ->
2583 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2584 scan args "%u %d %f %f %f %f %f %f %f %f"
2585 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2586 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2588 state.rects1 <-
2589 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2591 | "page" :: args :: [] ->
2592 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2593 begin match state.currently with
2594 | Loading (l, gen) ->
2595 vlog "page %d took %f sec" l.pageno t;
2596 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2597 begin match state.throttle with
2598 | None ->
2599 let preloadedpages =
2600 if conf.preload
2601 then preloadlayout state.y
2602 else state.layout
2604 let evict () =
2605 let set =
2606 List.fold_left (fun s l -> IntSet.add l.pageno s)
2607 IntSet.empty preloadedpages
2609 let evictedpages =
2610 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2611 if not (IntSet.mem pageno set)
2612 then (
2613 wcmd "freepage %s" opaque;
2614 key :: accu
2616 else accu
2617 ) state.pagemap []
2619 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2621 evict ();
2622 state.currently <- Idle;
2623 if gen = state.gen
2624 then (
2625 tilepage l.pageno pageopaque state.layout;
2626 load state.layout;
2627 load preloadedpages;
2628 if pagevisible state.layout l.pageno
2629 && layoutready state.layout
2630 then G.postRedisplay "page";
2633 | Some (layout, _, _) ->
2634 state.currently <- Idle;
2635 tilepage l.pageno pageopaque layout;
2636 load state.layout
2637 end;
2639 | _ ->
2640 dolog "Inconsistent loading state";
2641 logcurrently state.currently;
2642 exit 1
2645 | "tile" :: args :: [] ->
2646 let (x, y, opaque, size, t) =
2647 scan args "%u %u %s %u %f"
2648 (fun x y p size t -> (x, y, p, size, t))
2650 begin match state.currently with
2651 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2652 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2654 unmappbo opaque;
2655 if tilew != conf.tilew || tileh != conf.tileh
2656 then (
2657 wcmd "freetile %s" opaque;
2658 state.currently <- Idle;
2659 load state.layout;
2661 else (
2662 puttileopaque l col row gen cs angle opaque size t;
2663 state.memused <- state.memused + size;
2664 state.uioh#infochanged Memused;
2665 gctiles ();
2666 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2667 opaque, size) state.tilelru;
2669 let layout =
2670 match state.throttle with
2671 | None -> state.layout
2672 | Some (layout, _, _) -> layout
2675 state.currently <- Idle;
2676 if gen = state.gen
2677 && conf.colorspace = cs
2678 && conf.angle = angle
2679 && tilevisible layout l.pageno x y
2680 then conttiling l.pageno pageopaque;
2682 begin match state.throttle with
2683 | None ->
2684 preload state.layout;
2685 if gen = state.gen
2686 && conf.colorspace = cs
2687 && conf.angle = angle
2688 && tilevisible state.layout l.pageno x y
2689 && (not !wtmode || layoutready state.layout)
2690 then G.postRedisplay "tile nothrottle";
2692 | Some (layout, y, _) ->
2693 let ready = layoutready layout in
2694 if ready
2695 then (
2696 state.y <- y;
2697 state.layout <- layout;
2698 state.throttle <- None;
2699 G.postRedisplay "throttle";
2701 else load layout;
2702 end;
2705 | _ ->
2706 dolog "Inconsistent tiling state";
2707 logcurrently state.currently;
2708 exit 1
2711 | "pdim" :: args :: [] ->
2712 let (n, w, h, _) as pdim =
2713 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2715 let pdim =
2716 match conf.fitmodel, conf.columns with
2717 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2718 | _ -> pdim
2720 state.uioh#infochanged Pdim;
2721 state.pdims <- pdim :: state.pdims
2723 | "o" :: args :: [] ->
2724 let (l, n, t, h, pos) =
2725 scan args "%u %u %d %u %n"
2726 (fun l n t h pos -> l, n, t, h, pos)
2728 let s = String.sub args pos (String.length args - pos) in
2729 let outline = (s, l, (n, float t /. float h, 0.0)) in
2730 begin match state.currently with
2731 | Outlining outlines ->
2732 state.currently <- Outlining (outline :: outlines)
2733 | Idle ->
2734 state.currently <- Outlining [outline]
2735 | currently ->
2736 dolog "invalid outlining state";
2737 logcurrently currently
2740 | "a" :: args :: [] ->
2741 let (n, l, t) =
2742 scan args "%u %d %d" (fun n l t -> n, l, t)
2744 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2746 | "info" :: args :: [] ->
2747 state.docinfo <- (1, args) :: state.docinfo
2749 | "infoend" :: [] ->
2750 state.uioh#infochanged Docinfo;
2751 state.docinfo <- List.rev state.docinfo
2753 | _ ->
2754 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2757 let onhist cb =
2758 let rc = cb.rc in
2759 let action = function
2760 | HCprev -> cbget cb ~-1
2761 | HCnext -> cbget cb 1
2762 | HCfirst -> cbget cb ~-(cb.rc)
2763 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2764 and cancel () = cb.rc <- rc
2765 in (action, cancel)
2768 let search pattern forward =
2769 match conf.columns with
2770 | Csplit _ ->
2771 showtext '!' "searching does not work properly in split columns mode"
2772 | _ ->
2773 if String.length pattern > 0
2774 then
2775 let pn, py =
2776 match state.layout with
2777 | [] -> 0, 0
2778 | l :: _ ->
2779 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2781 wcmd "search %d %d %d %d,%s\000"
2782 (btod conf.icase) pn py (btod forward) pattern;
2785 let intentry text key =
2786 let c =
2787 if key >= 32 && key < 127
2788 then Char.chr key
2789 else '\000'
2791 match c with
2792 | '0' .. '9' ->
2793 let text = addchar text c in
2794 TEcont text
2796 | _ ->
2797 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2798 TEcont text
2801 let linknentry text key =
2802 let c =
2803 if key >= 32 && key < 127
2804 then Char.chr key
2805 else '\000'
2807 match c with
2808 | 'a' .. 'z' ->
2809 let text = addchar text c in
2810 TEcont text
2812 | _ ->
2813 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2814 TEcont text
2817 let linkndone f s =
2818 if String.length s > 0
2819 then (
2820 let n =
2821 let l = String.length s in
2822 let rec loop pos n = if pos = l then n else
2823 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2824 loop (pos+1) (n*26 + m)
2825 in loop 0 0
2827 let rec loop n = function
2828 | [] -> ()
2829 | l :: rest ->
2830 match getopaque l.pageno with
2831 | None -> loop n rest
2832 | Some opaque ->
2833 let m = getlinkcount opaque in
2834 if n < m
2835 then (
2836 let under = getlink opaque n in
2837 f under
2839 else loop (n-m) rest
2841 loop n state.layout;
2845 let textentry text key =
2846 if key land 0xff00 = 0xff00
2847 then TEcont text
2848 else TEcont (text ^ toutf8 key)
2851 let reqlayout angle fitmodel =
2852 match state.throttle with
2853 | None ->
2854 if nogeomcmds state.geomcmds
2855 then state.anchor <- getanchor ();
2856 conf.angle <- angle mod 360;
2857 if conf.angle != 0
2858 then (
2859 match state.mode with
2860 | LinkNav _ -> state.mode <- View
2861 | _ -> ()
2863 conf.fitmodel <- fitmodel;
2864 invalidate "reqlayout"
2865 (fun () ->
2866 wcmd "reqlayout %d %d %d"
2867 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2869 | _ -> ()
2872 let settrim trimmargins trimfuzz =
2873 if nogeomcmds state.geomcmds
2874 then state.anchor <- getanchor ();
2875 conf.trimmargins <- trimmargins;
2876 conf.trimfuzz <- trimfuzz;
2877 let x0, y0, x1, y1 = trimfuzz in
2878 invalidate "settrim"
2879 (fun () ->
2880 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2881 flushpages ();
2884 let setzoom zoom =
2885 match state.throttle with
2886 | None ->
2887 let zoom = max 0.0001 zoom in
2888 if zoom <> conf.zoom
2889 then (
2890 state.prevzoom <- (conf.zoom, state.x);
2891 conf.zoom <- zoom;
2892 reshape state.winw state.winh;
2893 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2896 | Some (layout, y, started) ->
2897 let time =
2898 match conf.maxwait with
2899 | None -> 0.0
2900 | Some t -> t
2902 let dt = now () -. started in
2903 if dt > time
2904 then (
2905 state.y <- y;
2906 load layout;
2910 let setcolumns mode columns coverA coverB =
2911 state.prevcolumns <- Some (conf.columns, conf.zoom);
2912 if columns < 0
2913 then (
2914 if isbirdseye mode
2915 then showtext '!' "split mode doesn't work in bird's eye"
2916 else (
2917 conf.columns <- Csplit (-columns, [||]);
2918 state.x <- 0;
2919 conf.zoom <- 1.0;
2922 else (
2923 if columns < 2
2924 then (
2925 conf.columns <- Csingle [||];
2926 state.x <- 0;
2927 setzoom 1.0;
2929 else (
2930 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2931 conf.zoom <- 1.0;
2934 reshape state.winw state.winh;
2937 let enterbirdseye () =
2938 let zoom = float conf.thumbw /. float state.winw in
2939 let birdseyepageno =
2940 let cy = state.winh / 2 in
2941 let fold = function
2942 | [] -> 0
2943 | l :: rest ->
2944 let rec fold best = function
2945 | [] -> best.pageno
2946 | l :: rest ->
2947 let d = cy - (l.pagedispy + l.pagevh/2)
2948 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2949 if abs d < abs dbest
2950 then fold l rest
2951 else best.pageno
2952 in fold l rest
2954 fold state.layout
2956 state.mode <- Birdseye (
2957 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2959 conf.zoom <- zoom;
2960 conf.presentation <- false;
2961 conf.interpagespace <- 10;
2962 conf.hlinks <- false;
2963 conf.fitmodel <- FitProportional;
2964 state.x <- 0;
2965 state.mstate <- Mnone;
2966 conf.maxwait <- None;
2967 conf.columns <- (
2968 match conf.beyecolumns with
2969 | Some c ->
2970 conf.zoom <- 1.0;
2971 Cmulti ((c, 0, 0), [||])
2972 | None -> Csingle [||]
2974 Wsi.setcursor Wsi.CURSOR_INHERIT;
2975 if conf.verbose
2976 then
2977 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2978 (100.0*.zoom)
2979 else
2980 state.text <- ""
2982 reshape state.winw state.winh;
2985 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2986 state.mode <- View;
2987 conf.zoom <- c.zoom;
2988 conf.presentation <- c.presentation;
2989 conf.interpagespace <- c.interpagespace;
2990 conf.maxwait <- c.maxwait;
2991 conf.hlinks <- c.hlinks;
2992 conf.fitmodel <- c.fitmodel;
2993 conf.beyecolumns <- (
2994 match conf.columns with
2995 | Cmulti ((c, _, _), _) -> Some c
2996 | Csingle _ -> None
2997 | Csplit _ -> failwith "leaving bird's eye split mode"
2999 conf.columns <- (
3000 match c.columns with
3001 | Cmulti (c, _) -> Cmulti (c, [||])
3002 | Csingle _ -> Csingle [||]
3003 | Csplit (c, _) -> Csplit (c, [||])
3005 state.x <- leftx;
3006 if conf.verbose
3007 then
3008 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3009 (100.0*.conf.zoom)
3011 reshape state.winw state.winh;
3012 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3015 let togglebirdseye () =
3016 match state.mode with
3017 | Birdseye vals -> leavebirdseye vals true
3018 | View -> enterbirdseye ()
3019 | _ -> ()
3022 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3023 let pageno = max 0 (pageno - incr) in
3024 let rec loop = function
3025 | [] -> gotopage1 pageno 0
3026 | l :: _ when l.pageno = pageno ->
3027 if l.pagedispy >= 0 && l.pagey = 0
3028 then G.postRedisplay "upbirdseye"
3029 else gotopage1 pageno 0
3030 | _ :: rest -> loop rest
3032 loop state.layout;
3033 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3036 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3037 let pageno = min (state.pagecount - 1) (pageno + incr) in
3038 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3039 let rec loop = function
3040 | [] ->
3041 let y, h = getpageyh pageno in
3042 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3043 gotoy (clamp dy)
3044 | l :: _ when l.pageno = pageno ->
3045 if l.pagevh != l.pageh
3046 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3047 else G.postRedisplay "downbirdseye"
3048 | _ :: rest -> loop rest
3050 loop state.layout
3053 let optentry mode _ key =
3054 let btos b = if b then "on" else "off" in
3055 if key >= 32 && key < 127
3056 then
3057 let c = Char.chr key in
3058 match c with
3059 | 's' ->
3060 let ondone s =
3061 try conf.scrollstep <- int_of_string s with exc ->
3062 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3064 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3066 | 'A' ->
3067 let ondone s =
3069 conf.autoscrollstep <- int_of_string s;
3070 if state.autoscroll <> None
3071 then state.autoscroll <- Some conf.autoscrollstep
3072 with exc ->
3073 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3075 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3077 | 'C' ->
3078 let ondone s =
3080 let n, a, b = multicolumns_of_string s in
3081 setcolumns mode n a b;
3082 with exc ->
3083 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3085 TEswitch ("columns: ", "", None, textentry, ondone, true)
3087 | 'Z' ->
3088 let ondone s =
3090 let zoom = float (int_of_string s) /. 100.0 in
3091 setzoom zoom
3092 with exc ->
3093 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3095 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3097 | 't' ->
3098 let ondone s =
3100 conf.thumbw <- bound (int_of_string s) 2 4096;
3101 state.text <-
3102 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3103 begin match mode with
3104 | Birdseye beye ->
3105 leavebirdseye beye false;
3106 enterbirdseye ();
3107 | _ -> ();
3109 with exc ->
3110 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3112 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3114 | 'R' ->
3115 let ondone s =
3116 match try
3117 Some (int_of_string s)
3118 with exc ->
3119 state.text <- Printf.sprintf "bad integer `%s': %s"
3120 s (exntos exc);
3121 None
3122 with
3123 | Some angle -> reqlayout angle conf.fitmodel
3124 | None -> ()
3126 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3128 | 'i' ->
3129 conf.icase <- not conf.icase;
3130 TEdone ("case insensitive search " ^ (btos conf.icase))
3132 | 'p' ->
3133 conf.preload <- not conf.preload;
3134 gotoy state.y;
3135 TEdone ("preload " ^ (btos conf.preload))
3137 | 'v' ->
3138 conf.verbose <- not conf.verbose;
3139 TEdone ("verbose " ^ (btos conf.verbose))
3141 | 'd' ->
3142 conf.debug <- not conf.debug;
3143 TEdone ("debug " ^ (btos conf.debug))
3145 | 'h' ->
3146 conf.maxhfit <- not conf.maxhfit;
3147 state.maxy <- calcheight ();
3148 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3150 | 'c' ->
3151 conf.crophack <- not conf.crophack;
3152 TEdone ("crophack " ^ btos conf.crophack)
3154 | 'a' ->
3155 let s =
3156 match conf.maxwait with
3157 | None ->
3158 conf.maxwait <- Some infinity;
3159 "always wait for page to complete"
3160 | Some _ ->
3161 conf.maxwait <- None;
3162 "show placeholder if page is not ready"
3164 TEdone s
3166 | 'f' ->
3167 conf.underinfo <- not conf.underinfo;
3168 TEdone ("underinfo " ^ btos conf.underinfo)
3170 | 'P' ->
3171 conf.savebmarks <- not conf.savebmarks;
3172 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3174 | 'S' ->
3175 let ondone s =
3177 let pageno, py =
3178 match state.layout with
3179 | [] -> 0, 0
3180 | l :: _ ->
3181 l.pageno, l.pagey
3183 conf.interpagespace <- int_of_string s;
3184 docolumns conf.columns;
3185 state.maxy <- calcheight ();
3186 let y = getpagey pageno in
3187 gotoy (y + py)
3188 with exc ->
3189 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3191 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3193 | 'l' ->
3194 let fm =
3195 match conf.fitmodel with
3196 | FitProportional -> FitWidth
3197 | _ -> FitProportional
3199 reqlayout conf.angle fm;
3200 TEdone ("proportional display " ^ btos (fm == FitProportional))
3202 | 'T' ->
3203 settrim (not conf.trimmargins) conf.trimfuzz;
3204 TEdone ("trim margins " ^ btos conf.trimmargins)
3206 | 'I' ->
3207 conf.invert <- not conf.invert;
3208 TEdone ("invert colors " ^ btos conf.invert)
3210 | 'x' ->
3211 let ondone s =
3212 cbput state.hists.sel s;
3213 conf.selcmd <- s;
3215 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3216 textentry, ondone, true)
3218 | 'M' ->
3219 if conf.pax == None
3220 then conf.pax <- Some (ref (0.0, 0, 0))
3221 else conf.pax <- None;
3222 TEdone ("PAX " ^ btos (conf.pax != None))
3224 | _ ->
3225 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3226 TEstop
3227 else
3228 TEcont state.text
3231 class type lvsource = object
3232 method getitemcount : int
3233 method getitem : int -> (string * int)
3234 method hasaction : int -> bool
3235 method exit :
3236 uioh:uioh ->
3237 cancel:bool ->
3238 active:int ->
3239 first:int ->
3240 pan:int ->
3241 qsearch:string ->
3242 uioh option
3243 method getactive : int
3244 method getfirst : int
3245 method getqsearch : string
3246 method setqsearch : string -> unit
3247 method getpan : int
3248 end;;
3250 class virtual lvsourcebase = object
3251 val mutable m_active = 0
3252 val mutable m_first = 0
3253 val mutable m_qsearch = ""
3254 val mutable m_pan = 0
3255 method getactive = m_active
3256 method getfirst = m_first
3257 method getqsearch = m_qsearch
3258 method getpan = m_pan
3259 method setqsearch s = m_qsearch <- s
3260 end;;
3262 let withoutlastutf8 s =
3263 let len = String.length s in
3264 if len = 0
3265 then s
3266 else
3267 let rec find pos =
3268 if pos = 0
3269 then pos
3270 else
3271 let b = Char.code s.[pos] in
3272 if b land 0b11000000 = 0b11000000
3273 then pos
3274 else find (pos-1)
3276 let first =
3277 if Char.code s.[len-1] land 0x80 = 0
3278 then len-1
3279 else find (len-1)
3281 String.sub s 0 first;
3284 let textentrykeyboard
3285 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3286 let key =
3287 if key >= 0xffb0 && key <= 0xffb9
3288 then key - 0xffb0 + 48 else key
3290 let enttext te =
3291 state.mode <- Textentry (te, onleave);
3292 state.text <- "";
3293 enttext ();
3294 G.postRedisplay "textentrykeyboard enttext";
3296 let histaction cmd =
3297 match opthist with
3298 | None -> ()
3299 | Some (action, _) ->
3300 state.mode <- Textentry (
3301 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3303 G.postRedisplay "textentry histaction"
3305 match key with
3306 | 0xff08 -> (* backspace *)
3307 let s = withoutlastutf8 text in
3308 let len = String.length s in
3309 if cancelonempty && len = 0
3310 then (
3311 onleave Cancel;
3312 G.postRedisplay "textentrykeyboard after cancel";
3314 else (
3315 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3318 | 0xff0d | 0xff8d -> (* (kp) enter *)
3319 ondone text;
3320 onleave Confirm;
3321 G.postRedisplay "textentrykeyboard after confirm"
3323 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3324 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3325 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3326 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3328 | 0xff1b -> (* escape*)
3329 if String.length text = 0
3330 then (
3331 begin match opthist with
3332 | None -> ()
3333 | Some (_, onhistcancel) -> onhistcancel ()
3334 end;
3335 onleave Cancel;
3336 state.text <- "";
3337 G.postRedisplay "textentrykeyboard after cancel2"
3339 else (
3340 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3343 | 0xff9f | 0xffff -> () (* delete *)
3345 | _ when key != 0
3346 && key land 0xff00 != 0xff00 (* keyboard *)
3347 && key land 0xfe00 != 0xfe00 (* xkb *)
3348 && key land 0xfd00 != 0xfd00 (* 3270 *)
3350 begin match onkey text key with
3351 | TEdone text ->
3352 ondone text;
3353 onleave Confirm;
3354 G.postRedisplay "textentrykeyboard after confirm2";
3356 | TEcont text ->
3357 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3359 | TEstop ->
3360 onleave Cancel;
3361 G.postRedisplay "textentrykeyboard after cancel3"
3363 | TEswitch te ->
3364 state.mode <- Textentry (te, onleave);
3365 G.postRedisplay "textentrykeyboard switch";
3366 end;
3368 | _ ->
3369 vlog "unhandled key %s" (Wsi.keyname key)
3372 let firstof first active =
3373 if first > active || abs (first - active) > fstate.maxrows - 1
3374 then max 0 (active - (fstate.maxrows/2))
3375 else first
3378 let calcfirst first active =
3379 if active > first
3380 then
3381 let rows = active - first in
3382 if rows > fstate.maxrows then active - fstate.maxrows else first
3383 else active
3386 let scrollph y maxy =
3387 let sh = float (maxy + state.winh) /. float state.winh in
3388 let sh = float state.winh /. sh in
3389 let sh = max sh (float conf.scrollh) in
3391 let percent = float y /. float maxy in
3392 let position = (float state.winh -. sh) *. percent in
3394 let position =
3395 if position +. sh > float state.winh
3396 then float state.winh -. sh
3397 else position
3399 position, sh;
3402 let coe s = (s :> uioh);;
3404 class listview ~(source:lvsource) ~trusted ~modehash =
3405 object (self)
3406 val m_pan = source#getpan
3407 val m_first = source#getfirst
3408 val m_active = source#getactive
3409 val m_qsearch = source#getqsearch
3410 val m_prev_uioh = state.uioh
3412 method private elemunder y =
3413 let n = y / (fstate.fontsize+1) in
3414 if m_first + n < source#getitemcount
3415 then (
3416 if source#hasaction (m_first + n)
3417 then Some (m_first + n)
3418 else None
3420 else None
3422 method display =
3423 Gl.enable `blend;
3424 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3425 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3426 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3427 GlDraw.color (1., 1., 1.);
3428 Gl.enable `texture_2d;
3429 let fs = fstate.fontsize in
3430 let nfs = fs + 1 in
3431 let ww = fstate.wwidth in
3432 let tabw = 30.0*.ww in
3433 let itemcount = source#getitemcount in
3434 let rec loop row =
3435 if (row - m_first) > fstate.maxrows
3436 then ()
3437 else (
3438 if row >= 0 && row < itemcount
3439 then (
3440 let (s, level) = source#getitem row in
3441 let y = (row - m_first) * nfs in
3442 let x = 5.0 +. float (level + m_pan) *. ww in
3443 if row = m_active
3444 then (
3445 Gl.disable `texture_2d;
3446 GlDraw.polygon_mode `both `line;
3447 let alpha = if source#hasaction row then 0.9 else 0.3 in
3448 GlDraw.color (1., 1., 1.) ~alpha;
3449 GlDraw.rect (1., float (y + 1))
3450 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3451 GlDraw.polygon_mode `both `fill;
3452 GlDraw.color (1., 1., 1.);
3453 Gl.enable `texture_2d;
3456 let drawtabularstring s =
3457 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3458 if trusted
3459 then
3460 let tabpos = try String.index s '\t' with Not_found -> -1 in
3461 if tabpos > 0
3462 then
3463 let len = String.length s - tabpos - 1 in
3464 let s1 = String.sub s 0 tabpos
3465 and s2 = String.sub s (tabpos + 1) len in
3466 let nx = drawstr x s1 in
3467 let sw = nx -. x in
3468 let x = x +. (max tabw sw) in
3469 drawstr x s2
3470 else
3471 drawstr x s
3472 else
3473 drawstr x s
3475 let _ = drawtabularstring s in
3476 loop (row+1)
3480 loop m_first;
3481 Gl.disable `blend;
3482 Gl.disable `texture_2d;
3484 method updownlevel incr =
3485 let len = source#getitemcount in
3486 let curlevel =
3487 if m_active >= 0 && m_active < len
3488 then snd (source#getitem m_active)
3489 else -1
3491 let rec flow i =
3492 if i = len then i-1 else if i = -1 then 0 else
3493 let _, l = source#getitem i in
3494 if l != curlevel then i else flow (i+incr)
3496 let active = flow m_active in
3497 let first = calcfirst m_first active in
3498 G.postRedisplay "outline updownlevel";
3499 {< m_active = active; m_first = first >}
3501 method private key1 key mask =
3502 let set1 active first qsearch =
3503 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3505 let search active pattern incr =
3506 let active = if active = -1 then m_first else active in
3507 let dosearch re =
3508 let rec loop n =
3509 if n >= 0 && n < source#getitemcount
3510 then (
3511 let s, _ = source#getitem n in
3513 (try ignore (Str.search_forward re s 0); true
3514 with Not_found -> false)
3515 then Some n
3516 else loop (n + incr)
3518 else None
3520 loop active
3523 let re = Str.regexp_case_fold pattern in
3524 dosearch re
3525 with Failure s ->
3526 state.text <- s;
3527 None
3529 let itemcount = source#getitemcount in
3530 let find start incr =
3531 let rec find i =
3532 if i = -1 || i = itemcount
3533 then -1
3534 else (
3535 if source#hasaction i
3536 then i
3537 else find (i + incr)
3540 find start
3542 let set active first =
3543 let first = bound first 0 (itemcount - fstate.maxrows) in
3544 state.text <- "";
3545 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3547 let navigate incr =
3548 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3549 let active, first =
3550 let incr1 = if incr > 0 then 1 else -1 in
3551 if isvisible m_first m_active
3552 then
3553 let next =
3554 let next = m_active + incr in
3555 let next =
3556 if next < 0 || next >= itemcount
3557 then -1
3558 else find next incr1
3560 if abs (m_active - next) > fstate.maxrows
3561 then -1
3562 else next
3564 if next = -1
3565 then
3566 let first = m_first + incr in
3567 let first = bound first 0 (itemcount - fstate.maxrows) in
3568 let next =
3569 let next = m_active + incr in
3570 let next = bound next 0 (itemcount - 1) in
3571 find next ~-incr1
3573 let active =
3574 if next = -1
3575 then m_active
3576 else (
3577 if isvisible first next
3578 then next
3579 else m_active
3582 active, first
3583 else
3584 let first = min next m_first in
3585 let first =
3586 if abs (next - first) > fstate.maxrows
3587 then first + incr
3588 else first
3590 next, first
3591 else
3592 let first = m_first + incr in
3593 let first = bound first 0 (itemcount - 1) in
3594 let active =
3595 let next = m_active + incr in
3596 let next = bound next 0 (itemcount - 1) in
3597 let next = find next incr1 in
3598 let active =
3599 if next = -1 || abs (m_active - first) > fstate.maxrows
3600 then (
3601 let active = if m_active = -1 then next else m_active in
3602 active
3604 else next
3606 if isvisible first active
3607 then active
3608 else -1
3610 active, first
3612 G.postRedisplay "listview navigate";
3613 set active first;
3615 match key with
3616 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3617 let incr = if key = 0x72 then -1 else 1 in
3618 let active, first =
3619 match search (m_active + incr) m_qsearch incr with
3620 | None ->
3621 state.text <- m_qsearch ^ " [not found]";
3622 m_active, m_first
3623 | Some active ->
3624 state.text <- m_qsearch;
3625 active, firstof m_first active
3627 G.postRedisplay "listview ctrl-r/s";
3628 set1 active first m_qsearch;
3630 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3631 if m_active >= 0 && m_active < source#getitemcount
3632 then (
3633 let s, _ = source#getitem m_active in
3634 selstring s;
3636 coe self
3638 | 0xff08 -> (* backspace *)
3639 if String.length m_qsearch = 0
3640 then coe self
3641 else (
3642 let qsearch = withoutlastutf8 m_qsearch in
3643 let len = String.length qsearch in
3644 if len = 0
3645 then (
3646 state.text <- "";
3647 G.postRedisplay "listview empty qsearch";
3648 set1 m_active m_first "";
3650 else
3651 let active, first =
3652 match search m_active qsearch ~-1 with
3653 | None ->
3654 state.text <- qsearch ^ " [not found]";
3655 m_active, m_first
3656 | Some active ->
3657 state.text <- qsearch;
3658 active, firstof m_first active
3660 G.postRedisplay "listview backspace qsearch";
3661 set1 active first qsearch
3664 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3665 let pattern = m_qsearch ^ toutf8 key in
3666 let active, first =
3667 match search m_active pattern 1 with
3668 | None ->
3669 state.text <- pattern ^ " [not found]";
3670 m_active, m_first
3671 | Some active ->
3672 state.text <- pattern;
3673 active, firstof m_first active
3675 G.postRedisplay "listview qsearch add";
3676 set1 active first pattern;
3678 | 0xff1b -> (* escape *)
3679 state.text <- "";
3680 if String.length m_qsearch = 0
3681 then (
3682 G.postRedisplay "list view escape";
3683 begin
3684 match
3685 source#exit (coe self) true m_active m_first m_pan m_qsearch
3686 with
3687 | None -> m_prev_uioh
3688 | Some uioh -> uioh
3691 else (
3692 G.postRedisplay "list view kill qsearch";
3693 source#setqsearch "";
3694 coe {< m_qsearch = "" >}
3697 | 0xff0d | 0xff8d -> (* (kp) enter *)
3698 state.text <- "";
3699 let self = {< m_qsearch = "" >} in
3700 source#setqsearch "";
3701 let opt =
3702 G.postRedisplay "listview enter";
3703 if m_active >= 0 && m_active < source#getitemcount
3704 then (
3705 source#exit (coe self) false m_active m_first m_pan "";
3707 else (
3708 source#exit (coe self) true m_active m_first m_pan "";
3711 begin match opt with
3712 | None -> m_prev_uioh
3713 | Some uioh -> uioh
3716 | 0xff9f | 0xffff -> (* (kp) delete *)
3717 coe self
3719 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3720 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3721 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3722 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3724 | 0xff53 | 0xff98 -> (* (kp) right *)
3725 state.text <- "";
3726 G.postRedisplay "listview right";
3727 coe {< m_pan = m_pan - 1 >}
3729 | 0xff51 | 0xff96 -> (* (kp) left *)
3730 state.text <- "";
3731 G.postRedisplay "listview left";
3732 coe {< m_pan = m_pan + 1 >}
3734 | 0xff50 | 0xff95 -> (* (kp) home *)
3735 let active = find 0 1 in
3736 G.postRedisplay "listview home";
3737 set active 0;
3739 | 0xff57 | 0xff9c -> (* (kp) end *)
3740 let first = max 0 (itemcount - fstate.maxrows) in
3741 let active = find (itemcount - 1) ~-1 in
3742 G.postRedisplay "listview end";
3743 set active first;
3745 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3746 coe self
3748 | _ ->
3749 dolog "listview unknown key %#x" key; coe self
3751 method key key mask =
3752 match state.mode with
3753 | Textentry te -> textentrykeyboard key mask te; coe self
3754 | _ -> self#key1 key mask
3756 method button button down x y _ =
3757 let opt =
3758 match button with
3759 | 1 when x > state.winw - conf.scrollbw ->
3760 G.postRedisplay "listview scroll";
3761 if down
3762 then
3763 let _, position, sh = self#scrollph in
3764 if y > truncate position && y < truncate (position +. sh)
3765 then (
3766 state.mstate <- Mscrolly;
3767 Some (coe self)
3769 else
3770 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3771 let first = truncate (s *. float source#getitemcount) in
3772 let first = min source#getitemcount first in
3773 Some (coe {< m_first = first; m_active = first >})
3774 else (
3775 state.mstate <- Mnone;
3776 Some (coe self);
3778 | 1 when not down ->
3779 begin match self#elemunder y with
3780 | Some n ->
3781 G.postRedisplay "listview click";
3782 source#exit
3783 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3784 | _ ->
3785 Some (coe self)
3787 | n when (n == 4 || n == 5) && not down ->
3788 let len = source#getitemcount in
3789 let first =
3790 if n = 5 && m_first + fstate.maxrows >= len
3791 then
3792 m_first
3793 else
3794 let first = m_first + (if n == 4 then -1 else 1) in
3795 bound first 0 (len - 1)
3797 G.postRedisplay "listview wheel";
3798 Some (coe {< m_first = first >})
3799 | n when (n = 6 || n = 7) && not down ->
3800 let inc = m_first + (if n = 7 then -1 else 1) in
3801 G.postRedisplay "listview hwheel";
3802 Some (coe {< m_pan = m_pan + inc >})
3803 | _ ->
3804 Some (coe self)
3806 match opt with
3807 | None -> m_prev_uioh
3808 | Some uioh -> uioh
3810 method motion _ y =
3811 match state.mstate with
3812 | Mscrolly ->
3813 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3814 let first = truncate (s *. float source#getitemcount) in
3815 let first = min source#getitemcount first in
3816 G.postRedisplay "listview motion";
3817 coe {< m_first = first; m_active = first >}
3818 | _ -> coe self
3820 method pmotion x y =
3821 if x < state.winw - conf.scrollbw
3822 then
3823 let n =
3824 match self#elemunder y with
3825 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3826 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3828 let o =
3829 if n != m_active
3830 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3831 else self
3833 coe o
3834 else (
3835 Wsi.setcursor Wsi.CURSOR_INHERIT;
3836 coe self
3839 method infochanged _ = ()
3841 method scrollpw = (0, 0.0, 0.0)
3842 method scrollph =
3843 let nfs = fstate.fontsize + 1 in
3844 let y = m_first * nfs in
3845 let itemcount = source#getitemcount in
3846 let maxi = max 0 (itemcount - fstate.maxrows) in
3847 let maxy = maxi * nfs in
3848 let p, h = scrollph y maxy in
3849 conf.scrollbw, p, h
3851 method modehash = modehash
3852 method eformsgs = false
3853 end;;
3855 class outlinelistview ~source =
3856 object (self)
3857 inherit listview
3858 ~source:(source :> lvsource)
3859 ~trusted:false
3860 ~modehash:(findkeyhash conf "outline")
3861 as super
3863 method key key mask =
3864 let calcfirst first active =
3865 if active > first
3866 then
3867 let rows = active - first in
3868 let maxrows =
3869 if String.length state.text = 0
3870 then fstate.maxrows
3871 else fstate.maxrows - 2
3873 if rows > maxrows then active - maxrows else first
3874 else active
3876 let navigate incr =
3877 let active = m_active + incr in
3878 let active = bound active 0 (source#getitemcount - 1) in
3879 let first = calcfirst m_first active in
3880 G.postRedisplay "outline navigate";
3881 coe {< m_active = active; m_first = first >}
3883 let ctrl = Wsi.withctrl mask in
3884 match key with
3885 | 110 when ctrl -> (* ctrl-n *)
3886 source#narrow m_qsearch;
3887 G.postRedisplay "outline ctrl-n";
3888 coe {< m_first = 0; m_active = 0 >}
3890 | 117 when ctrl -> (* ctrl-u *)
3891 source#denarrow;
3892 G.postRedisplay "outline ctrl-u";
3893 state.text <- "";
3894 coe {< m_first = 0; m_active = 0 >}
3896 | 108 when ctrl -> (* ctrl-l *)
3897 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3898 G.postRedisplay "outline ctrl-l";
3899 coe {< m_first = first >}
3901 | 0xff9f | 0xffff -> (* (kp) delete *)
3902 source#remove m_active;
3903 G.postRedisplay "outline delete";
3904 let active = max 0 (m_active-1) in
3905 coe {< m_first = firstof m_first active;
3906 m_active = active >}
3908 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3909 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3910 | 0xff55 | 0xff9a -> (* (kp) prior *)
3911 navigate ~-(fstate.maxrows)
3912 | 0xff56 | 0xff9b -> (* (kp) next *)
3913 navigate fstate.maxrows
3915 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3916 let o =
3917 if ctrl
3918 then (
3919 G.postRedisplay "outline ctrl right";
3920 {< m_pan = m_pan + 1 >}
3922 else self#updownlevel 1
3924 coe o
3926 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3927 let o =
3928 if ctrl
3929 then (
3930 G.postRedisplay "outline ctrl left";
3931 {< m_pan = m_pan - 1 >}
3933 else self#updownlevel ~-1
3935 coe o
3937 | 0xff50 | 0xff95 -> (* (kp) home *)
3938 G.postRedisplay "outline home";
3939 coe {< m_first = 0; m_active = 0 >}
3941 | 0xff57 | 0xff9c -> (* (kp) end *)
3942 let active = source#getitemcount - 1 in
3943 let first = max 0 (active - fstate.maxrows) in
3944 G.postRedisplay "outline end";
3945 coe {< m_active = active; m_first = first >}
3947 | _ -> super#key key mask
3950 let outlinesource usebookmarks =
3951 let empty = [||] in
3952 (object
3953 inherit lvsourcebase
3954 val mutable m_items = empty
3955 val mutable m_orig_items = empty
3956 val mutable m_prev_items = empty
3957 val mutable m_narrow_pattern = ""
3958 val mutable m_hadremovals = false
3960 method getitemcount =
3961 Array.length m_items + (if m_hadremovals then 1 else 0)
3963 method getitem n =
3964 if n == Array.length m_items && m_hadremovals
3965 then
3966 ("[Confirm removal]", 0)
3967 else
3968 let s, n, _ = m_items.(n) in
3969 (s, n)
3971 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3972 ignore (uioh, first, qsearch);
3973 let confrimremoval = m_hadremovals && active = Array.length m_items in
3974 let items =
3975 if String.length m_narrow_pattern = 0
3976 then m_orig_items
3977 else m_items
3979 if not cancel
3980 then (
3981 if not confrimremoval
3982 then(
3983 let _, _, anchor = m_items.(active) in
3984 gotoghyll (getanchory anchor);
3985 m_items <- items;
3987 else (
3988 state.bookmarks <- Array.to_list m_items;
3989 m_orig_items <- m_items;
3992 else m_items <- items;
3993 m_pan <- pan;
3994 None
3996 method hasaction _ = true
3998 method greetmsg =
3999 if Array.length m_items != Array.length m_orig_items
4000 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
4001 else ""
4003 method narrow pattern =
4004 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4005 match reopt with
4006 | None -> ()
4007 | Some re ->
4008 let rec loop accu n =
4009 if n = -1
4010 then (
4011 m_narrow_pattern <- pattern;
4012 m_items <- Array.of_list accu
4014 else
4015 let (s, _, _) as o = m_items.(n) in
4016 let accu =
4017 if (try ignore (Str.search_forward re s 0); true
4018 with Not_found -> false)
4019 then o :: accu
4020 else accu
4022 loop accu (n-1)
4024 loop [] (Array.length m_items - 1)
4026 method denarrow =
4027 m_orig_items <- (
4028 if usebookmarks
4029 then Array.of_list state.bookmarks
4030 else state.outlines
4032 m_items <- m_orig_items
4034 method remove m =
4035 if usebookmarks
4036 then
4037 if m >= 0 && m < Array.length m_items
4038 then (
4039 m_hadremovals <- true;
4040 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4041 let n = if n >= m then n+1 else n in
4042 m_items.(n)
4046 method reset anchor items =
4047 m_hadremovals <- false;
4048 if m_orig_items == empty || m_prev_items != items
4049 then (
4050 m_orig_items <- items;
4051 if String.length m_narrow_pattern = 0
4052 then m_items <- items;
4054 m_prev_items <- items;
4055 let rely = getanchory anchor in
4056 let active =
4057 let rec loop n best bestd =
4058 if n = Array.length m_items
4059 then best
4060 else
4061 let (_, _, anchor) = m_items.(n) in
4062 let orely = getanchory anchor in
4063 let d = abs (orely - rely) in
4064 if d < bestd
4065 then loop (n+1) n d
4066 else loop (n+1) best bestd
4068 loop 0 ~-1 max_int
4070 m_active <- active;
4071 m_first <- firstof m_first active
4072 end)
4075 let enterselector usebookmarks =
4076 let source = outlinesource usebookmarks in
4077 fun errmsg ->
4078 let outlines =
4079 if usebookmarks
4080 then Array.of_list state.bookmarks
4081 else state.outlines
4083 if Array.length outlines = 0
4084 then (
4085 showtext ' ' errmsg;
4087 else (
4088 state.text <- source#greetmsg;
4089 Wsi.setcursor Wsi.CURSOR_INHERIT;
4090 let anchor = getanchor () in
4091 source#reset anchor outlines;
4092 state.uioh <- coe (new outlinelistview ~source);
4093 G.postRedisplay "enter selector";
4097 let enteroutlinemode =
4098 let f = enterselector false in
4099 fun ()-> f "Document has no outline";
4102 let enterbookmarkmode =
4103 let f = enterselector true in
4104 fun () -> f "Document has no bookmarks (yet)";
4107 let color_of_string s =
4108 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4109 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4113 let color_to_string (r, g, b) =
4114 let r = truncate (r *. 256.0)
4115 and g = truncate (g *. 256.0)
4116 and b = truncate (b *. 256.0) in
4117 Printf.sprintf "%d/%d/%d" r g b
4120 let irect_of_string s =
4121 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4124 let irect_to_string (x0,y0,x1,y1) =
4125 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4128 let makecheckers () =
4129 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4130 following to say:
4131 converted by Issac Trotts. July 25, 2002 *)
4132 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4133 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4134 let id = GlTex.gen_texture () in
4135 GlTex.bind_texture `texture_2d id;
4136 GlPix.store (`unpack_alignment 1);
4137 GlTex.image2d image;
4138 List.iter (GlTex.parameter ~target:`texture_2d)
4139 [ `mag_filter `nearest; `min_filter `nearest ];
4143 let setcheckers enabled =
4144 match state.texid with
4145 | None ->
4146 if enabled then state.texid <- Some (makecheckers ())
4148 | Some texid ->
4149 if not enabled
4150 then (
4151 GlTex.delete_texture texid;
4152 state.texid <- None;
4156 let int_of_string_with_suffix s =
4157 let l = String.length s in
4158 let s1, shift =
4159 if l > 1
4160 then
4161 let suffix = Char.lowercase s.[l-1] in
4162 match suffix with
4163 | 'k' -> String.sub s 0 (l-1), 10
4164 | 'm' -> String.sub s 0 (l-1), 20
4165 | 'g' -> String.sub s 0 (l-1), 30
4166 | _ -> s, 0
4167 else s, 0
4169 let n = int_of_string s1 in
4170 let m = n lsl shift in
4171 if m < 0 || m < n
4172 then raise (Failure "value too large")
4173 else m
4176 let string_with_suffix_of_int n =
4177 if n = 0
4178 then "0"
4179 else
4180 let n, s =
4181 if n land ((1 lsl 30) - 1) = 0
4182 then n lsr 30, "G"
4183 else (
4184 if n land ((1 lsl 20) - 1) = 0
4185 then n lsr 20, "M"
4186 else (
4187 if n land ((1 lsl 10) - 1) = 0
4188 then n lsr 10, "K"
4189 else n, ""
4193 let rec loop s n =
4194 let h = n mod 1000 in
4195 let n = n / 1000 in
4196 if n = 0
4197 then string_of_int h ^ s
4198 else (
4199 let s = Printf.sprintf "_%03d%s" h s in
4200 loop s n
4203 loop "" n ^ s;
4206 let defghyllscroll = (40, 8, 32);;
4207 let ghyllscroll_of_string s =
4208 let (n, a, b) as nab =
4209 if s = "default"
4210 then defghyllscroll
4211 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4213 if n <= a || n <= b || a >= b
4214 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4215 nab;
4218 let ghyllscroll_to_string ((n, a, b) as nab) =
4219 if nab = defghyllscroll
4220 then "default"
4221 else Printf.sprintf "%d,%d,%d" n a b;
4224 let describe_location () =
4225 let fn = page_of_y state.y in
4226 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4227 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4228 let percent =
4229 if maxy <= 0
4230 then 100.
4231 else (100. *. (float state.y /. float maxy))
4233 if fn = ln
4234 then
4235 Printf.sprintf "page %d of %d [%.2f%%]"
4236 (fn+1) state.pagecount percent
4237 else
4238 Printf.sprintf
4239 "pages %d-%d of %d [%.2f%%]"
4240 (fn+1) (ln+1) state.pagecount percent
4243 let setpresentationmode v =
4244 let n = page_of_y state.y in
4245 state.anchor <- (n, 0.0, 1.0);
4246 conf.presentation <- v;
4247 if conf.fitmodel = FitPage
4248 then reqlayout conf.angle conf.fitmodel;
4249 represent ();
4252 let enterinfomode =
4253 let btos b = if b then "\xe2\x88\x9a" else "" in
4254 let showextended = ref false in
4255 let leave mode = function
4256 | Confirm -> state.mode <- mode
4257 | Cancel -> state.mode <- mode in
4258 let src =
4259 (object
4260 val mutable m_first_time = true
4261 val mutable m_l = []
4262 val mutable m_a = [||]
4263 val mutable m_prev_uioh = nouioh
4264 val mutable m_prev_mode = View
4266 inherit lvsourcebase
4268 method reset prev_mode prev_uioh =
4269 m_a <- Array.of_list (List.rev m_l);
4270 m_l <- [];
4271 m_prev_mode <- prev_mode;
4272 m_prev_uioh <- prev_uioh;
4273 if m_first_time
4274 then (
4275 let rec loop n =
4276 if n >= Array.length m_a
4277 then ()
4278 else
4279 match m_a.(n) with
4280 | _, _, _, Action _ -> m_active <- n
4281 | _ -> loop (n+1)
4283 loop 0;
4284 m_first_time <- false;
4287 method int name get set =
4288 m_l <-
4289 (name, `int get, 1, Action (
4290 fun u ->
4291 let ondone s =
4292 try set (int_of_string s)
4293 with exn ->
4294 state.text <- Printf.sprintf "bad integer `%s': %s"
4295 s (exntos exn)
4297 state.text <- "";
4298 let te = name ^ ": ", "", None, intentry, ondone, true in
4299 state.mode <- Textentry (te, leave m_prev_mode);
4301 )) :: m_l
4303 method int_with_suffix name get set =
4304 m_l <-
4305 (name, `intws get, 1, Action (
4306 fun u ->
4307 let ondone s =
4308 try set (int_of_string_with_suffix s)
4309 with exn ->
4310 state.text <- Printf.sprintf "bad integer `%s': %s"
4311 s (exntos exn)
4313 state.text <- "";
4314 let te =
4315 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4317 state.mode <- Textentry (te, leave m_prev_mode);
4319 )) :: m_l
4321 method bool ?(offset=1) ?(btos=btos) name get set =
4322 m_l <-
4323 (name, `bool (btos, get), offset, Action (
4324 fun u ->
4325 let v = get () in
4326 set (not v);
4328 )) :: m_l
4330 method color name get set =
4331 m_l <-
4332 (name, `color get, 1, Action (
4333 fun u ->
4334 let invalid = (nan, nan, nan) in
4335 let ondone s =
4336 let c =
4337 try color_of_string s
4338 with exn ->
4339 state.text <- Printf.sprintf "bad color `%s': %s"
4340 s (exntos exn);
4341 invalid
4343 if c <> invalid
4344 then set c;
4346 let te = name ^ ": ", "", None, textentry, ondone, true in
4347 state.text <- color_to_string (get ());
4348 state.mode <- Textentry (te, leave m_prev_mode);
4350 )) :: m_l
4352 method string name get set =
4353 m_l <-
4354 (name, `string get, 1, Action (
4355 fun u ->
4356 let ondone s = set s in
4357 let te = name ^ ": ", "", None, textentry, ondone, true in
4358 state.mode <- Textentry (te, leave m_prev_mode);
4360 )) :: m_l
4362 method colorspace name get set =
4363 m_l <-
4364 (name, `string get, 1, Action (
4365 fun _ ->
4366 let source =
4367 (object
4368 inherit lvsourcebase
4370 initializer
4371 m_active <- CSTE.to_int conf.colorspace;
4372 m_first <- 0;
4374 method getitemcount =
4375 Array.length CSTE.names
4376 method getitem n =
4377 (CSTE.names.(n), 0)
4378 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4379 ignore (uioh, first, pan, qsearch);
4380 if not cancel then set active;
4381 None
4382 method hasaction _ = true
4383 end)
4385 state.text <- "";
4386 let modehash = findkeyhash conf "info" in
4387 coe (new listview ~source ~trusted:true ~modehash)
4388 )) :: m_l
4390 method paxmark name get set =
4391 m_l <-
4392 (name, `string get, 1, Action (
4393 fun _ ->
4394 let source =
4395 (object
4396 inherit lvsourcebase
4398 initializer
4399 m_active <- MTE.to_int conf.paxmark;
4400 m_first <- 0;
4402 method getitemcount = Array.length MTE.names
4403 method getitem n = (MTE.names.(n), 0)
4404 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4405 ignore (uioh, first, pan, qsearch);
4406 if not cancel then set active;
4407 None
4408 method hasaction _ = true
4409 end)
4411 state.text <- "";
4412 let modehash = findkeyhash conf "info" in
4413 coe (new listview ~source ~trusted:true ~modehash)
4414 )) :: m_l
4416 method fitmodel name get set =
4417 m_l <-
4418 (name, `string get, 1, Action (
4419 fun _ ->
4420 let source =
4421 (object
4422 inherit lvsourcebase
4424 initializer
4425 m_active <- FMTE.to_int conf.fitmodel;
4426 m_first <- 0;
4428 method getitemcount = Array.length FMTE.names
4429 method getitem n = (FMTE.names.(n), 0)
4430 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4431 ignore (uioh, first, pan, qsearch);
4432 if not cancel then set active;
4433 None
4434 method hasaction _ = true
4435 end)
4437 state.text <- "";
4438 let modehash = findkeyhash conf "info" in
4439 coe (new listview ~source ~trusted:true ~modehash)
4440 )) :: m_l
4442 method caption s offset =
4443 m_l <- (s, `empty, offset, Noaction) :: m_l
4445 method caption2 s f offset =
4446 m_l <- (s, `string f, offset, Noaction) :: m_l
4448 method getitemcount = Array.length m_a
4450 method getitem n =
4451 let tostr = function
4452 | `int f -> string_of_int (f ())
4453 | `intws f -> string_with_suffix_of_int (f ())
4454 | `string f -> f ()
4455 | `color f -> color_to_string (f ())
4456 | `bool (btos, f) -> btos (f ())
4457 | `empty -> ""
4459 let name, t, offset, _ = m_a.(n) in
4460 ((let s = tostr t in
4461 if String.length s > 0
4462 then Printf.sprintf "%s\t%s" name s
4463 else name),
4464 offset)
4466 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4467 let uiohopt =
4468 if not cancel
4469 then (
4470 m_qsearch <- qsearch;
4471 let uioh =
4472 match m_a.(active) with
4473 | _, _, _, Action f -> f uioh
4474 | _ -> uioh
4476 Some uioh
4478 else None
4480 m_active <- active;
4481 m_first <- first;
4482 m_pan <- pan;
4483 uiohopt
4485 method hasaction n =
4486 match m_a.(n) with
4487 | _, _, _, Action _ -> true
4488 | _ -> false
4489 end)
4491 let rec fillsrc prevmode prevuioh =
4492 let sep () = src#caption "" 0 in
4493 let colorp name get set =
4494 src#string name
4495 (fun () -> color_to_string (get ()))
4496 (fun v ->
4498 let c = color_of_string v in
4499 set c
4500 with exn ->
4501 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4504 let oldmode = state.mode in
4505 let birdseye = isbirdseye state.mode in
4507 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4509 src#bool "presentation mode"
4510 (fun () -> conf.presentation)
4511 (fun v -> setpresentationmode v);
4513 src#bool "ignore case in searches"
4514 (fun () -> conf.icase)
4515 (fun v -> conf.icase <- v);
4517 src#bool "preload"
4518 (fun () -> conf.preload)
4519 (fun v -> conf.preload <- v);
4521 src#bool "highlight links"
4522 (fun () -> conf.hlinks)
4523 (fun v -> conf.hlinks <- v);
4525 src#bool "under info"
4526 (fun () -> conf.underinfo)
4527 (fun v -> conf.underinfo <- v);
4529 src#bool "persistent bookmarks"
4530 (fun () -> conf.savebmarks)
4531 (fun v -> conf.savebmarks <- v);
4533 src#fitmodel "fit model"
4534 (fun () -> FMTE.to_string conf.fitmodel)
4535 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4537 src#bool "trim margins"
4538 (fun () -> conf.trimmargins)
4539 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4541 src#bool "persistent location"
4542 (fun () -> conf.jumpback)
4543 (fun v -> conf.jumpback <- v);
4545 sep ();
4546 src#int "inter-page space"
4547 (fun () -> conf.interpagespace)
4548 (fun n ->
4549 conf.interpagespace <- n;
4550 docolumns conf.columns;
4551 let pageno, py =
4552 match state.layout with
4553 | [] -> 0, 0
4554 | l :: _ ->
4555 l.pageno, l.pagey
4557 state.maxy <- calcheight ();
4558 let y = getpagey pageno in
4559 gotoy (y + py)
4562 src#int "page bias"
4563 (fun () -> conf.pagebias)
4564 (fun v -> conf.pagebias <- v);
4566 src#int "scroll step"
4567 (fun () -> conf.scrollstep)
4568 (fun n -> conf.scrollstep <- n);
4570 src#int "horizontal scroll step"
4571 (fun () -> conf.hscrollstep)
4572 (fun v -> conf.hscrollstep <- v);
4574 src#int "auto scroll step"
4575 (fun () ->
4576 match state.autoscroll with
4577 | Some step -> step
4578 | _ -> conf.autoscrollstep)
4579 (fun n ->
4580 if state.autoscroll <> None
4581 then state.autoscroll <- Some n;
4582 conf.autoscrollstep <- n);
4584 src#int "zoom"
4585 (fun () -> truncate (conf.zoom *. 100.))
4586 (fun v -> setzoom ((float v) /. 100.));
4588 src#int "rotation"
4589 (fun () -> conf.angle)
4590 (fun v -> reqlayout v conf.fitmodel);
4592 src#int "scroll bar width"
4593 (fun () -> conf.scrollbw)
4594 (fun v ->
4595 conf.scrollbw <- v;
4596 reshape state.winw state.winh;
4599 src#int "scroll handle height"
4600 (fun () -> conf.scrollh)
4601 (fun v -> conf.scrollh <- v;);
4603 src#int "thumbnail width"
4604 (fun () -> conf.thumbw)
4605 (fun v ->
4606 conf.thumbw <- min 4096 v;
4607 match oldmode with
4608 | Birdseye beye ->
4609 leavebirdseye beye false;
4610 enterbirdseye ()
4611 | _ -> ()
4614 let mode = state.mode in
4615 src#string "columns"
4616 (fun () ->
4617 match conf.columns with
4618 | Csingle _ -> "1"
4619 | Cmulti (multi, _) -> multicolumns_to_string multi
4620 | Csplit (count, _) -> "-" ^ string_of_int count
4622 (fun v ->
4623 let n, a, b = multicolumns_of_string v in
4624 setcolumns mode n a b);
4626 sep ();
4627 src#caption "Pixmap cache" 0;
4628 src#int_with_suffix "size (advisory)"
4629 (fun () -> conf.memlimit)
4630 (fun v -> conf.memlimit <- v);
4632 src#caption2 "used"
4633 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4634 (string_with_suffix_of_int state.memused)
4635 (Hashtbl.length state.tilemap)) 1;
4637 sep ();
4638 src#caption "Layout" 0;
4639 src#caption2 "Dimension"
4640 (fun () ->
4641 Printf.sprintf "%dx%d (virtual %dx%d)"
4642 state.winw state.winh
4643 state.w state.maxy)
4645 if conf.debug
4646 then
4647 src#caption2 "Position" (fun () ->
4648 Printf.sprintf "%dx%d" state.x state.y
4650 else
4651 src#caption2 "Position" (fun () -> describe_location ()) 1
4654 sep ();
4655 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4656 "Save these parameters as global defaults at exit"
4657 (fun () -> conf.bedefault)
4658 (fun v -> conf.bedefault <- v)
4661 sep ();
4662 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4663 src#bool ~offset:0 ~btos "Extended parameters"
4664 (fun () -> !showextended)
4665 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4666 if !showextended
4667 then (
4668 src#bool "checkers"
4669 (fun () -> conf.checkers)
4670 (fun v -> conf.checkers <- v; setcheckers v);
4671 src#bool "update cursor"
4672 (fun () -> conf.updatecurs)
4673 (fun v -> conf.updatecurs <- v);
4674 src#bool "verbose"
4675 (fun () -> conf.verbose)
4676 (fun v -> conf.verbose <- v);
4677 src#bool "invert colors"
4678 (fun () -> conf.invert)
4679 (fun v -> conf.invert <- v);
4680 src#bool "max fit"
4681 (fun () -> conf.maxhfit)
4682 (fun v -> conf.maxhfit <- v);
4683 src#bool "redirect stderr"
4684 (fun () -> conf.redirectstderr)
4685 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4686 src#bool "pax mode"
4687 (fun () -> conf.pax != None)
4688 (fun v ->
4689 if v
4690 then conf.pax <- Some (ref (now (), 0, 0))
4691 else conf.pax <- None);
4692 src#string "uri launcher"
4693 (fun () -> conf.urilauncher)
4694 (fun v -> conf.urilauncher <- v);
4695 src#string "path launcher"
4696 (fun () -> conf.pathlauncher)
4697 (fun v -> conf.pathlauncher <- v);
4698 src#string "tile size"
4699 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4700 (fun v ->
4702 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4703 conf.tilew <- max 64 w;
4704 conf.tileh <- max 64 h;
4705 flushtiles ();
4706 with exn ->
4707 state.text <- Printf.sprintf "bad tile size `%s': %s"
4708 v (exntos exn)
4710 src#int "texture count"
4711 (fun () -> conf.texcount)
4712 (fun v ->
4713 if realloctexts v
4714 then conf.texcount <- v
4715 else showtext '!' " Failed to set texture count please retry later"
4717 src#int "slice height"
4718 (fun () -> conf.sliceheight)
4719 (fun v ->
4720 conf.sliceheight <- v;
4721 wcmd "sliceh %d" conf.sliceheight;
4723 src#int "anti-aliasing level"
4724 (fun () -> conf.aalevel)
4725 (fun v ->
4726 conf.aalevel <- bound v 0 8;
4727 state.anchor <- getanchor ();
4728 opendoc state.path state.password;
4730 src#string "page scroll scaling factor"
4731 (fun () -> string_of_float conf.pgscale)
4732 (fun v ->
4734 let s = float_of_string v in
4735 conf.pgscale <- s
4736 with exn ->
4737 state.text <- Printf.sprintf
4738 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4741 src#int "ui font size"
4742 (fun () -> fstate.fontsize)
4743 (fun v -> setfontsize (bound v 5 100));
4744 src#int "hint font size"
4745 (fun () -> conf.hfsize)
4746 (fun v -> conf.hfsize <- bound v 5 100);
4747 colorp "background color"
4748 (fun () -> conf.bgcolor)
4749 (fun v -> conf.bgcolor <- v);
4750 src#bool "crop hack"
4751 (fun () -> conf.crophack)
4752 (fun v -> conf.crophack <- v);
4753 src#string "trim fuzz"
4754 (fun () -> irect_to_string conf.trimfuzz)
4755 (fun v ->
4757 conf.trimfuzz <- irect_of_string v;
4758 if conf.trimmargins
4759 then settrim true conf.trimfuzz;
4760 with exn ->
4761 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4763 src#string "throttle"
4764 (fun () ->
4765 match conf.maxwait with
4766 | None -> "show place holder if page is not ready"
4767 | Some time ->
4768 if time = infinity
4769 then "wait for page to fully render"
4770 else
4771 "wait " ^ string_of_float time
4772 ^ " seconds before showing placeholder"
4774 (fun v ->
4776 let f = float_of_string v in
4777 if f <= 0.0
4778 then conf.maxwait <- None
4779 else conf.maxwait <- Some f
4780 with exn ->
4781 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4783 src#string "ghyll scroll"
4784 (fun () ->
4785 match conf.ghyllscroll with
4786 | None -> ""
4787 | Some nab -> ghyllscroll_to_string nab
4789 (fun v ->
4791 let gs =
4792 if String.length v = 0
4793 then None
4794 else Some (ghyllscroll_of_string v)
4796 conf.ghyllscroll <- gs
4797 with exn ->
4798 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4800 src#string "selection command"
4801 (fun () -> conf.selcmd)
4802 (fun v -> conf.selcmd <- v);
4803 src#string "synctex command"
4804 (fun () -> conf.stcmd)
4805 (fun v -> conf.stcmd <- v);
4806 src#string "pax command"
4807 (fun () -> conf.paxcmd)
4808 (fun v -> conf.paxcmd <- v);
4809 src#colorspace "color space"
4810 (fun () -> CSTE.to_string conf.colorspace)
4811 (fun v ->
4812 conf.colorspace <- CSTE.of_int v;
4813 wcmd "cs %d" v;
4814 load state.layout;
4816 src#paxmark "pax mark method"
4817 (fun () -> MTE.to_string conf.paxmark)
4818 (fun v -> conf.paxmark <- MTE.of_int v);
4819 if pbousable ()
4820 then
4821 src#bool "use PBO"
4822 (fun () -> conf.usepbo)
4823 (fun v -> conf.usepbo <- v);
4824 src#bool "mouse wheel scrolls pages"
4825 (fun () -> conf.wheelbypage)
4826 (fun v -> conf.wheelbypage <- v);
4827 src#bool "open remote links in a new instance"
4828 (fun () -> conf.riani)
4829 (fun v -> conf.riani <- v);
4832 sep ();
4833 src#caption "Document" 0;
4834 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4835 src#caption2 "Pages"
4836 (fun () -> string_of_int state.pagecount) 1;
4837 src#caption2 "Dimensions"
4838 (fun () -> string_of_int (List.length state.pdims)) 1;
4839 if conf.trimmargins
4840 then (
4841 sep ();
4842 src#caption "Trimmed margins" 0;
4843 src#caption2 "Dimensions"
4844 (fun () -> string_of_int (List.length state.pdims)) 1;
4847 sep ();
4848 src#caption "OpenGL" 0;
4849 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4850 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4852 sep ();
4853 src#caption "Location" 0;
4854 if String.length state.origin > 0
4855 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4856 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4858 src#reset prevmode prevuioh;
4860 fun () ->
4861 state.text <- "";
4862 let prevmode = state.mode
4863 and prevuioh = state.uioh in
4864 fillsrc prevmode prevuioh;
4865 let source = (src :> lvsource) in
4866 let modehash = findkeyhash conf "info" in
4867 state.uioh <- coe (object (self)
4868 inherit listview ~source ~trusted:true ~modehash as super
4869 val mutable m_prevmemused = 0
4870 method infochanged = function
4871 | Memused ->
4872 if m_prevmemused != state.memused
4873 then (
4874 m_prevmemused <- state.memused;
4875 G.postRedisplay "memusedchanged";
4877 | Pdim -> G.postRedisplay "pdimchanged"
4878 | Docinfo -> fillsrc prevmode prevuioh
4880 method key key mask =
4881 if not (Wsi.withctrl mask)
4882 then
4883 match key with
4884 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4885 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4886 | _ -> super#key key mask
4887 else super#key key mask
4888 end);
4889 G.postRedisplay "info";
4892 let enterhelpmode =
4893 let source =
4894 (object
4895 inherit lvsourcebase
4896 method getitemcount = Array.length state.help
4897 method getitem n =
4898 let s, l, _ = state.help.(n) in
4899 (s, l)
4901 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4902 let optuioh =
4903 if not cancel
4904 then (
4905 m_qsearch <- qsearch;
4906 match state.help.(active) with
4907 | _, _, Action f -> Some (f uioh)
4908 | _ -> Some (uioh)
4910 else None
4912 m_active <- active;
4913 m_first <- first;
4914 m_pan <- pan;
4915 optuioh
4917 method hasaction n =
4918 match state.help.(n) with
4919 | _, _, Action _ -> true
4920 | _ -> false
4922 initializer
4923 m_active <- -1
4924 end)
4925 in fun () ->
4926 let modehash = findkeyhash conf "help" in
4927 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4928 G.postRedisplay "help";
4931 let entermsgsmode =
4932 let msgsource =
4933 let re = Str.regexp "[\r\n]" in
4934 (object
4935 inherit lvsourcebase
4936 val mutable m_items = [||]
4938 method getitemcount = 1 + Array.length m_items
4940 method getitem n =
4941 if n = 0
4942 then "[Clear]", 0
4943 else m_items.(n-1), 0
4945 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4946 ignore uioh;
4947 if not cancel
4948 then (
4949 if active = 0
4950 then Buffer.clear state.errmsgs;
4951 m_qsearch <- qsearch;
4953 m_active <- active;
4954 m_first <- first;
4955 m_pan <- pan;
4956 None
4958 method hasaction n =
4959 n = 0
4961 method reset =
4962 state.newerrmsgs <- false;
4963 let l = Str.split re (Buffer.contents state.errmsgs) in
4964 m_items <- Array.of_list l
4966 initializer
4967 m_active <- 0
4968 end)
4969 in fun () ->
4970 state.text <- "";
4971 msgsource#reset;
4972 let source = (msgsource :> lvsource) in
4973 let modehash = findkeyhash conf "listview" in
4974 state.uioh <- coe (object
4975 inherit listview ~source ~trusted:false ~modehash as super
4976 method display =
4977 if state.newerrmsgs
4978 then msgsource#reset;
4979 super#display
4980 end);
4981 G.postRedisplay "msgs";
4984 let quickbookmark ?title () =
4985 match state.layout with
4986 | [] -> ()
4987 | l :: _ ->
4988 let title =
4989 match title with
4990 | None ->
4991 let sec = Unix.gettimeofday () in
4992 let tm = Unix.localtime sec in
4993 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4994 (l.pageno+1)
4995 tm.Unix.tm_mday
4996 tm.Unix.tm_mon
4997 (tm.Unix.tm_year + 1900)
4998 tm.Unix.tm_hour
4999 tm.Unix.tm_min
5000 | Some title -> title
5002 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
5005 let setautoscrollspeed step goingdown =
5006 let incr = max 1 ((abs step) / 2) in
5007 let incr = if goingdown then incr else -incr in
5008 let astep = step + incr in
5009 state.autoscroll <- Some astep;
5012 let gotounder = function
5013 | Ulinkgoto (pageno, top) ->
5014 if pageno >= 0
5015 then (
5016 addnav ();
5017 gotopage1 pageno top;
5020 | Ulinkuri s ->
5021 gotouri s
5023 | Uremote (filename, pageno) ->
5024 let path =
5025 if String.length filename > 0
5026 then
5027 if Filename.is_relative filename
5028 then
5029 let dir = Filename.dirname state.path in
5030 let dir =
5031 if Filename.is_implicit dir
5032 then Filename.concat (Sys.getcwd ()) dir
5033 else dir
5035 Filename.concat dir filename
5036 else filename
5037 else ""
5039 let path =
5040 if Sys.file_exists path
5041 then path
5042 else ""
5044 if String.length path > 0
5045 then (
5046 if conf.riani
5047 then
5048 let command = !selfexec ^ " " ^ path in
5049 try popen command []
5050 with exn ->
5051 Printf.eprintf
5052 "failed to execute `%s': %s\n" command (exntos exn);
5053 flush stderr;
5054 else
5055 let anchor = getanchor () in
5056 let ranchor = state.path, state.password, anchor, state.origin in
5057 state.origin <- "";
5058 state.anchor <- (pageno, 0.0, 0.0);
5059 state.ranchors <- ranchor :: state.ranchors;
5060 opendoc path "";
5062 else showtext '!' ("Could not find " ^ filename)
5064 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5067 let canpan () =
5068 match conf.columns with
5069 | Csplit _ -> true
5070 | _ -> state.x != 0 || conf.zoom > 1.0
5073 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5075 let existsinrow pageno (columns, coverA, coverB) p =
5076 let last = ((pageno - coverA) mod columns) + columns in
5077 let rec any = function
5078 | [] -> false
5079 | l :: rest ->
5080 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5081 then p l
5082 else (
5083 if not (p l)
5084 then (if l.pageno = last then false else any rest)
5085 else true
5088 any state.layout
5091 let nextpage () =
5092 match state.layout with
5093 | [] ->
5094 let pageno = page_of_y state.y in
5095 gotoghyll (getpagey (pageno+1))
5096 | l :: rest ->
5097 match conf.columns with
5098 | Csingle _ ->
5099 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5100 then
5101 let y = clamp (pgscale state.winh) in
5102 gotoghyll y
5103 else
5104 let pageno = min (l.pageno+1) (state.pagecount-1) in
5105 gotoghyll (getpagey pageno)
5106 | Cmulti ((c, _, _) as cl, _) ->
5107 if conf.presentation
5108 && (existsinrow l.pageno cl
5109 (fun l -> l.pageh > l.pagey + l.pagevh))
5110 then
5111 let y = clamp (pgscale state.winh) in
5112 gotoghyll y
5113 else
5114 let pageno = min (l.pageno+c) (state.pagecount-1) in
5115 gotoghyll (getpagey pageno)
5116 | Csplit (n, _) ->
5117 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5118 then
5119 let pagey, pageh = getpageyh l.pageno in
5120 let pagey = pagey + pageh * l.pagecol in
5121 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5122 gotoghyll (pagey + pageh + ips)
5125 let prevpage () =
5126 match state.layout with
5127 | [] ->
5128 let pageno = page_of_y state.y in
5129 gotoghyll (getpagey (pageno-1))
5130 | l :: _ ->
5131 match conf.columns with
5132 | Csingle _ ->
5133 if conf.presentation && l.pagey != 0
5134 then
5135 gotoghyll (clamp (pgscale ~-(state.winh)))
5136 else
5137 let pageno = max 0 (l.pageno-1) in
5138 gotoghyll (getpagey pageno)
5139 | Cmulti ((c, _, coverB) as cl, _) ->
5140 if conf.presentation &&
5141 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5142 then
5143 gotoghyll (clamp (pgscale ~-(state.winh)))
5144 else
5145 let decr =
5146 if l.pageno = state.pagecount - coverB
5147 then 1
5148 else c
5150 let pageno = max 0 (l.pageno-decr) in
5151 gotoghyll (getpagey pageno)
5152 | Csplit (n, _) ->
5153 let y =
5154 if l.pagecol = 0
5155 then
5156 if l.pageno = 0
5157 then l.pagey
5158 else
5159 let pageno = max 0 (l.pageno-1) in
5160 let pagey, pageh = getpageyh pageno in
5161 pagey + (n-1)*pageh
5162 else
5163 let pagey, pageh = getpageyh l.pageno in
5164 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5166 gotoghyll y
5169 let viewkeyboard key mask =
5170 let enttext te =
5171 let mode = state.mode in
5172 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5173 state.text <- "";
5174 enttext ();
5175 G.postRedisplay "view:enttext"
5177 let ctrl = Wsi.withctrl mask in
5178 let key =
5179 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5181 match key with
5182 | 81 -> (* Q *)
5183 exit 0
5185 | 0xff63 -> (* insert *)
5186 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5187 then (
5188 state.mode <- LinkNav (Ltgendir 0);
5189 gotoy state.y;
5191 else showtext '!' "Keyboard link navigation does not work under rotation"
5193 | 0xff1b | 113 -> (* escape / q *)
5194 begin match state.mstate with
5195 | Mzoomrect _ ->
5196 state.mstate <- Mnone;
5197 Wsi.setcursor Wsi.CURSOR_INHERIT;
5198 G.postRedisplay "kill zoom rect";
5199 | _ ->
5200 begin match state.mode with
5201 | LinkNav _ ->
5202 state.mode <- View;
5203 G.postRedisplay "esc leave linknav"
5204 | _ ->
5205 match state.ranchors with
5206 | [] -> raise Quit
5207 | (path, password, anchor, origin) :: rest ->
5208 state.ranchors <- rest;
5209 state.anchor <- anchor;
5210 state.origin <- origin;
5211 opendoc path password
5212 end;
5213 end;
5215 | 0xff08 -> (* backspace *)
5216 gotoghyll (getnav ~-1)
5218 | 111 -> (* o *)
5219 enteroutlinemode ()
5221 | 117 -> (* u *)
5222 state.rects <- [];
5223 state.text <- "";
5224 G.postRedisplay "dehighlight";
5226 | 47 | 63 -> (* / ? *)
5227 let ondone isforw s =
5228 cbput state.hists.pat s;
5229 state.searchpattern <- s;
5230 search s isforw
5232 let s = String.create 1 in
5233 s.[0] <- Char.chr key;
5234 enttext (s, "", Some (onhist state.hists.pat),
5235 textentry, ondone (key = 47), true)
5237 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5238 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5239 setzoom (conf.zoom +. incr)
5241 | 43 | 0xffab -> (* + *)
5242 let ondone s =
5243 let n =
5244 try int_of_string s with exc ->
5245 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5246 max_int
5248 if n != max_int
5249 then (
5250 conf.pagebias <- n;
5251 state.text <- "page bias is now " ^ string_of_int n;
5254 enttext ("page bias: ", "", None, intentry, ondone, true)
5256 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5257 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5258 setzoom (max 0.01 (conf.zoom -. decr))
5260 | 45 | 0xffad -> (* - *)
5261 let ondone msg = state.text <- msg in
5262 enttext (
5263 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5264 optentry state.mode, ondone, true
5267 | 48 when ctrl -> (* ctrl-0 *)
5268 if conf.zoom = 1.0
5269 then (
5270 state.x <- 0;
5271 gotoy state.y
5273 else setzoom 1.0
5275 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5276 let cols =
5277 match conf.columns with
5278 | Csingle _ | Cmulti _ -> 1
5279 | Csplit (n, _) -> n
5281 let h = state.winh -
5282 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5284 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5285 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5286 then setzoom zoom
5288 | 51 when ctrl -> (* ctrl-3 *)
5289 let fm =
5290 match conf.fitmodel with
5291 | FitWidth -> FitProportional
5292 | FitProportional -> FitPage
5293 | FitPage -> FitWidth
5295 state.text <- "fit model: " ^ FMTE.to_string fm;
5296 reqlayout conf.angle fm
5298 | 0xffc6 -> (* f9 *)
5299 togglebirdseye ()
5301 | 57 when ctrl -> (* ctrl-9 *)
5302 togglebirdseye ()
5304 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5305 when not ctrl -> (* 0..9 *)
5306 let ondone s =
5307 let n =
5308 try int_of_string s with exc ->
5309 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5312 if n >= 0
5313 then (
5314 addnav ();
5315 cbput state.hists.pag (string_of_int n);
5316 gotopage1 (n + conf.pagebias - 1) 0;
5319 let pageentry text key =
5320 match Char.unsafe_chr key with
5321 | 'g' -> TEdone text
5322 | _ -> intentry text key
5324 let text = "x" in text.[0] <- Char.chr key;
5325 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5327 | 98 -> (* b *)
5328 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5329 reshape state.winw state.winh;
5331 | 66 -> (* B *)
5332 state.bzoom <- not state.bzoom;
5333 state.rects <- [];
5334 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5336 | 108 -> (* l *)
5337 conf.hlinks <- not conf.hlinks;
5338 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5339 G.postRedisplay "toggle highlightlinks";
5341 | 70 -> (* F *)
5342 state.glinks <- true;
5343 let mode = state.mode in
5344 state.mode <- Textentry (
5345 (":", "", None, linknentry, linkndone gotounder, false),
5346 (fun _ ->
5347 state.glinks <- false;
5348 state.mode <- mode)
5350 state.text <- "";
5351 G.postRedisplay "view:linkent(F)"
5353 | 121 -> (* y *)
5354 state.glinks <- true;
5355 let mode = state.mode in
5356 state.mode <- Textentry (
5358 ":", "", None, linknentry, linkndone (fun under ->
5359 selstring (undertext under);
5360 ), false
5362 fun _ ->
5363 state.glinks <- false;
5364 state.mode <- mode
5366 state.text <- "";
5367 G.postRedisplay "view:linkent"
5369 | 97 -> (* a *)
5370 begin match state.autoscroll with
5371 | Some step ->
5372 conf.autoscrollstep <- step;
5373 state.autoscroll <- None
5374 | None ->
5375 if conf.autoscrollstep = 0
5376 then state.autoscroll <- Some 1
5377 else state.autoscroll <- Some conf.autoscrollstep
5380 | 112 when ctrl -> (* ctrl-p *)
5381 launchpath ()
5383 | 80 -> (* P *)
5384 setpresentationmode (not conf.presentation);
5385 showtext ' ' ("presentation mode " ^
5386 if conf.presentation then "on" else "off");
5388 | 102 -> (* f *)
5389 if List.mem Wsi.Fullscreen state.winstate
5390 then Wsi.reshape conf.cwinw conf.cwinh
5391 else Wsi.fullscreen ()
5393 | 112 | 78 -> (* p|N *)
5394 search state.searchpattern false
5396 | 110 | 0xffc0 -> (* n|F3 *)
5397 search state.searchpattern true
5399 | 116 -> (* t *)
5400 begin match state.layout with
5401 | [] -> ()
5402 | l :: _ ->
5403 gotoghyll (getpagey l.pageno)
5406 | 32 -> (* space *)
5407 nextpage ()
5409 | 0xff9f | 0xffff -> (* delete *)
5410 prevpage ()
5412 | 61 -> (* = *)
5413 showtext ' ' (describe_location ());
5415 | 119 -> (* w *)
5416 begin match state.layout with
5417 | [] -> ()
5418 | l :: _ ->
5419 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5420 G.postRedisplay "w"
5423 | 39 -> (* ' *)
5424 enterbookmarkmode ()
5426 | 104 | 0xffbe -> (* h|F1 *)
5427 enterhelpmode ()
5429 | 105 -> (* i *)
5430 enterinfomode ()
5432 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5433 entermsgsmode ()
5435 | 109 -> (* m *)
5436 let ondone s =
5437 match state.layout with
5438 | l :: _ ->
5439 if String.length s > 0
5440 then
5441 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5442 | _ -> ()
5444 enttext ("bookmark: ", "", None, textentry, ondone, true)
5446 | 126 -> (* ~ *)
5447 quickbookmark ();
5448 showtext ' ' "Quick bookmark added";
5450 | 122 -> (* z *)
5451 begin match state.layout with
5452 | l :: _ ->
5453 let rect = getpdimrect l.pagedimno in
5454 let w, h =
5455 if conf.crophack
5456 then
5457 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5458 truncate (1.2 *. (rect.(3) -. rect.(0))))
5459 else
5460 (truncate (rect.(1) -. rect.(0)),
5461 truncate (rect.(3) -. rect.(0)))
5463 let w = truncate ((float w)*.conf.zoom)
5464 and h = truncate ((float h)*.conf.zoom) in
5465 if w != 0 && h != 0
5466 then (
5467 state.anchor <- getanchor ();
5468 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5470 G.postRedisplay "z";
5472 | [] -> ()
5475 | 120 -> state.roam ()
5476 | 60 | 62 -> (* < > *)
5477 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5479 | 91 | 93 -> (* [ ] *)
5480 conf.colorscale <-
5481 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5483 G.postRedisplay "brightness";
5485 | 99 when state.mode = View -> (* [alt-]c *)
5486 if Wsi.withalt mask
5487 then (
5488 if conf.zoom > 1.0
5489 then
5490 let m = (wadjsb state.winw - state.w) / 2 in
5491 state.x <- m;
5492 gotoy_and_clear_text state.y
5494 else
5495 let (c, a, b), z =
5496 match state.prevcolumns with
5497 | None -> (1, 0, 0), 1.0
5498 | Some (columns, z) ->
5499 let cab =
5500 match columns with
5501 | Csplit (c, _) -> -c, 0, 0
5502 | Cmulti ((c, a, b), _) -> c, a, b
5503 | Csingle _ -> 1, 0, 0
5505 cab, z
5507 setcolumns View c a b;
5508 setzoom z
5510 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5511 -> (* ctrl-shift- (kp) [up|down] *)
5512 let zoom, x = state.prevzoom in
5513 setzoom zoom;
5514 state.x <- x;
5516 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5517 begin match state.autoscroll with
5518 | None ->
5519 begin match state.mode with
5520 | Birdseye beye -> upbirdseye 1 beye
5521 | _ ->
5522 if ctrl
5523 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5524 else (
5525 if not (Wsi.withshift mask) && conf.presentation
5526 then prevpage ()
5527 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5530 | Some n ->
5531 setautoscrollspeed n false
5534 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5535 begin match state.autoscroll with
5536 | None ->
5537 begin match state.mode with
5538 | Birdseye beye -> downbirdseye 1 beye
5539 | _ ->
5540 if ctrl
5541 then gotoy_and_clear_text (clamp (state.winh/2))
5542 else (
5543 if not (Wsi.withshift mask) && conf.presentation
5544 then nextpage ()
5545 else gotoy_and_clear_text (clamp conf.scrollstep)
5548 | Some n ->
5549 setautoscrollspeed n true
5552 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5553 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5554 if canpan ()
5555 then
5556 let dx =
5557 if ctrl
5558 then state.winw / 2
5559 else conf.hscrollstep
5561 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5562 state.x <- panbound (state.x + dx);
5563 gotoy_and_clear_text state.y
5564 else (
5565 state.text <- "";
5566 G.postRedisplay "left/right"
5569 | 0xff55 | 0xff9a -> (* (kp) prior *)
5570 let y =
5571 if ctrl
5572 then
5573 match state.layout with
5574 | [] -> state.y
5575 | l :: _ -> state.y - l.pagey
5576 else
5577 clamp (pgscale (-state.winh))
5579 gotoghyll y
5581 | 0xff56 | 0xff9b -> (* (kp) next *)
5582 let y =
5583 if ctrl
5584 then
5585 match List.rev state.layout with
5586 | [] -> state.y
5587 | l :: _ -> getpagey l.pageno
5588 else
5589 clamp (pgscale state.winh)
5591 gotoghyll y
5593 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5594 gotoghyll 0
5595 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5596 gotoghyll (clamp state.maxy)
5598 | 0xff53 | 0xff98
5599 when Wsi.withalt mask -> (* alt-(kp) right *)
5600 gotoghyll (getnav 1)
5601 | 0xff51 | 0xff96
5602 when Wsi.withalt mask -> (* alt-(kp) left *)
5603 gotoghyll (getnav ~-1)
5605 | 114 -> (* r *)
5606 reload ()
5608 | 118 when conf.debug -> (* v *)
5609 state.rects <- [];
5610 List.iter (fun l ->
5611 match getopaque l.pageno with
5612 | None -> ()
5613 | Some opaque ->
5614 let x0, y0, x1, y1 = pagebbox opaque in
5615 let a,b = float x0, float y0 in
5616 let c,d = float x1, float y0 in
5617 let e,f = float x1, float y1 in
5618 let h,j = float x0, float y1 in
5619 let rect = (a,b,c,d,e,f,h,j) in
5620 debugrect rect;
5621 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5622 ) state.layout;
5623 G.postRedisplay "v";
5625 | _ ->
5626 vlog "huh? %s" (Wsi.keyname key)
5629 let linknavkeyboard key mask linknav =
5630 let getpage pageno =
5631 let rec loop = function
5632 | [] -> None
5633 | l :: _ when l.pageno = pageno -> Some l
5634 | _ :: rest -> loop rest
5635 in loop state.layout
5637 let doexact (pageno, n) =
5638 match getopaque pageno, getpage pageno with
5639 | Some opaque, Some l ->
5640 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5641 then
5642 let under = getlink opaque n in
5643 G.postRedisplay "link gotounder";
5644 gotounder under;
5645 state.mode <- View;
5646 else
5647 let opt, dir =
5648 match key with
5649 | 0xff50 -> (* home *)
5650 Some (findlink opaque LDfirst), -1
5652 | 0xff57 -> (* end *)
5653 Some (findlink opaque LDlast), 1
5655 | 0xff51 -> (* left *)
5656 Some (findlink opaque (LDleft n)), -1
5658 | 0xff53 -> (* right *)
5659 Some (findlink opaque (LDright n)), 1
5661 | 0xff52 -> (* up *)
5662 Some (findlink opaque (LDup n)), -1
5664 | 0xff54 -> (* down *)
5665 Some (findlink opaque (LDdown n)), 1
5667 | _ -> None, 0
5669 let pwl l dir =
5670 begin match findpwl l.pageno dir with
5671 | Pwlnotfound -> ()
5672 | Pwl pageno ->
5673 let notfound dir =
5674 state.mode <- LinkNav (Ltgendir dir);
5675 let y, h = getpageyh pageno in
5676 let y =
5677 if dir < 0
5678 then y + h - state.winh
5679 else y
5681 gotoy y
5683 begin match getopaque pageno, getpage pageno with
5684 | Some opaque, Some _ ->
5685 let link =
5686 let ld = if dir > 0 then LDfirst else LDlast in
5687 findlink opaque ld
5689 begin match link with
5690 | Lfound m ->
5691 showlinktype (getlink opaque m);
5692 state.mode <- LinkNav (Ltexact (pageno, m));
5693 G.postRedisplay "linknav jpage";
5694 | _ -> notfound dir
5695 end;
5696 | _ -> notfound dir
5697 end;
5698 end;
5700 begin match opt with
5701 | Some Lnotfound -> pwl l dir;
5702 | Some (Lfound m) ->
5703 if m = n
5704 then pwl l dir
5705 else (
5706 let _, y0, _, y1 = getlinkrect opaque m in
5707 if y0 < l.pagey
5708 then gotopage1 l.pageno y0
5709 else (
5710 let d = fstate.fontsize + 1 in
5711 if y1 - l.pagey > l.pagevh - d
5712 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5713 else G.postRedisplay "linknav";
5715 showlinktype (getlink opaque m);
5716 state.mode <- LinkNav (Ltexact (l.pageno, m));
5719 | None -> viewkeyboard key mask
5720 end;
5721 | _ -> viewkeyboard key mask
5723 if key = 0xff63
5724 then (
5725 state.mode <- View;
5726 G.postRedisplay "leave linknav"
5728 else
5729 match linknav with
5730 | Ltgendir _ -> viewkeyboard key mask
5731 | Ltexact exact -> doexact exact
5734 let keyboard key mask =
5735 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5736 then wcmd "interrupt"
5737 else state.uioh <- state.uioh#key key mask
5740 let birdseyekeyboard key mask
5741 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5742 let incr =
5743 match conf.columns with
5744 | Csingle _ -> 1
5745 | Cmulti ((c, _, _), _) -> c
5746 | Csplit _ -> failwith "bird's eye split mode"
5748 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5749 match key with
5750 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5751 let y, h = getpageyh pageno in
5752 let top = (state.winh - h) / 2 in
5753 gotoy (max 0 (y - top))
5754 | 0xff0d (* enter *)
5755 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5756 | 0xff1b -> leavebirdseye beye true (* escape *)
5757 | 0xff52 -> upbirdseye incr beye (* up *)
5758 | 0xff54 -> downbirdseye incr beye (* down *)
5759 | 0xff51 -> upbirdseye 1 beye (* left *)
5760 | 0xff53 -> downbirdseye 1 beye (* right *)
5762 | 0xff55 -> (* prior *)
5763 begin match state.layout with
5764 | l :: _ ->
5765 if l.pagey != 0
5766 then (
5767 state.mode <- Birdseye (
5768 oconf, leftx, l.pageno, hooverpageno, anchor
5770 gotopage1 l.pageno 0;
5772 else (
5773 let layout = layout (state.y-state.winh) (pgh state.layout) in
5774 match layout with
5775 | [] -> gotoy (clamp (-state.winh))
5776 | l :: _ ->
5777 state.mode <- Birdseye (
5778 oconf, leftx, l.pageno, hooverpageno, anchor
5780 gotopage1 l.pageno 0
5783 | [] -> gotoy (clamp (-state.winh))
5784 end;
5786 | 0xff56 -> (* next *)
5787 begin match List.rev state.layout with
5788 | l :: _ ->
5789 let layout = layout (state.y + (pgh state.layout)) state.winh in
5790 begin match layout with
5791 | [] ->
5792 let incr = l.pageh - l.pagevh in
5793 if incr = 0
5794 then (
5795 state.mode <-
5796 Birdseye (
5797 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5799 G.postRedisplay "birdseye pagedown";
5801 else gotoy (clamp (incr + conf.interpagespace*2));
5803 | l :: _ ->
5804 state.mode <-
5805 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5806 gotopage1 l.pageno 0;
5809 | [] -> gotoy (clamp state.winh)
5810 end;
5812 | 0xff50 -> (* home *)
5813 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5814 gotopage1 0 0
5816 | 0xff57 -> (* end *)
5817 let pageno = state.pagecount - 1 in
5818 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5819 if not (pagevisible state.layout pageno)
5820 then
5821 let h =
5822 match List.rev state.pdims with
5823 | [] -> state.winh
5824 | (_, _, h, _) :: _ -> h
5826 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5827 else G.postRedisplay "birdseye end";
5828 | _ -> viewkeyboard key mask
5831 let drawpage l =
5832 let color =
5833 match state.mode with
5834 | Textentry _ -> scalecolor 0.4
5835 | LinkNav _
5836 | View -> scalecolor 1.0
5837 | Birdseye (_, _, pageno, hooverpageno, _) ->
5838 if l.pageno = hooverpageno
5839 then scalecolor 0.9
5840 else (
5841 if l.pageno = pageno
5842 then scalecolor 1.0
5843 else scalecolor 0.8
5846 drawtiles l color;
5849 let postdrawpage l linkindexbase =
5850 match getopaque l.pageno with
5851 | Some opaque ->
5852 if tileready l l.pagex l.pagey
5853 then
5854 let x = l.pagedispx - l.pagex
5855 and y = l.pagedispy - l.pagey in
5856 let hlmask =
5857 match conf.columns with
5858 | Csingle _ | Cmulti _ ->
5859 (if conf.hlinks then 1 else 0)
5860 + (if state.glinks
5861 && not (isbirdseye state.mode) then 2 else 0)
5862 | _ -> 0
5864 let s =
5865 match state.mode with
5866 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5867 | _ -> ""
5869 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5870 else 0
5871 | _ -> 0
5874 let scrollindicator () =
5875 let sbw, ph, sh = state.uioh#scrollph in
5876 let sbh, pw, sw = state.uioh#scrollpw in
5878 GlDraw.color (0.64, 0.64, 0.64);
5879 GlDraw.rect
5880 (float (state.winw - sbw), 0.)
5881 (float state.winw, float state.winh)
5883 GlDraw.rect
5884 (0., float (state.winh - sbh))
5885 (float (wadjsb state.winw - 1), float state.winh)
5887 GlDraw.color (0.0, 0.0, 0.0);
5889 GlDraw.rect
5890 (float (state.winw - sbw), ph)
5891 (float state.winw, ph +. sh)
5893 GlDraw.rect
5894 (pw, float (state.winh - sbh))
5895 (pw +. sw, float state.winh)
5899 let showsel () =
5900 match state.mstate with
5901 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5904 | Msel ((x0, y0), (x1, y1)) ->
5905 let rec loop = function
5906 | l :: ls ->
5907 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5908 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5909 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5910 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5911 then
5912 match getopaque l.pageno with
5913 | Some opaque ->
5914 let x0, y0 = pagetranslatepoint l x0 y0 in
5915 let x1, y1 = pagetranslatepoint l x1 y1 in
5916 seltext opaque (x0, y0, x1, y1);
5917 | _ -> ()
5918 else loop ls
5919 | [] -> ()
5921 loop state.layout
5924 let showrects rects =
5925 Gl.enable `blend;
5926 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5927 GlDraw.polygon_mode `both `fill;
5928 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5929 List.iter
5930 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5931 List.iter (fun l ->
5932 if l.pageno = pageno
5933 then (
5934 let dx = float (l.pagedispx - l.pagex) in
5935 let dy = float (l.pagedispy - l.pagey) in
5936 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5937 GlDraw.begins `quads;
5939 GlDraw.vertex2 (x0+.dx, y0+.dy);
5940 GlDraw.vertex2 (x1+.dx, y1+.dy);
5941 GlDraw.vertex2 (x2+.dx, y2+.dy);
5942 GlDraw.vertex2 (x3+.dx, y3+.dy);
5944 GlDraw.ends ();
5946 ) state.layout
5947 ) rects
5949 Gl.disable `blend;
5952 let display () =
5953 GlClear.color (scalecolor2 conf.bgcolor);
5954 GlClear.clear [`color];
5955 List.iter drawpage state.layout;
5956 let rects =
5957 match state.mode with
5958 | LinkNav (Ltexact (pageno, linkno)) ->
5959 begin match getopaque pageno with
5960 | Some opaque ->
5961 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5962 (pageno, 5, (
5963 float x0, float y0,
5964 float x1, float y0,
5965 float x1, float y1,
5966 float x0, float y1)
5967 ) :: state.rects
5968 | None -> state.rects
5970 | _ -> state.rects
5972 showrects rects;
5973 let rec postloop linkindexbase = function
5974 | l :: rest ->
5975 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5976 postloop linkindexbase rest
5977 | [] -> ()
5979 showsel ();
5980 postloop 0 state.layout;
5981 state.uioh#display;
5982 begin match state.mstate with
5983 | Mzoomrect ((x0, y0), (x1, y1)) ->
5984 Gl.enable `blend;
5985 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5986 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5987 GlDraw.rect (float x0, float y0)
5988 (float x1, float y1);
5989 Gl.disable `blend;
5990 | _ -> ()
5991 end;
5992 enttext ();
5993 scrollindicator ();
5994 Wsi.swapb ();
5997 let zoomrect x y x1 y1 =
5998 let x0 = min x x1
5999 and x1 = max x x1
6000 and y0 = min y y1 in
6001 gotoy (state.y + y0);
6002 state.anchor <- getanchor ();
6003 let zoom = (float state.w) /. float (x1 - x0) in
6004 let margin =
6005 match conf.fitmodel, conf.columns with
6006 | FitPage, Csplit _ ->
6007 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6009 | _, _ ->
6010 let adjw = wadjsb state.winw in
6011 if state.w < adjw
6012 then (adjw - state.w) / 2
6013 else 0
6015 state.x <- (state.x + margin) - x0;
6016 setzoom zoom;
6017 Wsi.setcursor Wsi.CURSOR_INHERIT;
6018 state.mstate <- Mnone;
6021 let zoomblock x y =
6022 let g opaque l px py =
6023 match rectofblock opaque px py with
6024 | Some a ->
6025 let x0 = a.(0) -. 20. in
6026 let x1 = a.(1) +. 20. in
6027 let y0 = a.(2) -. 20. in
6028 let zoom = (float state.w) /. (x1 -. x0) in
6029 let pagey = getpagey l.pageno in
6030 gotoy_and_clear_text (pagey + truncate y0);
6031 state.anchor <- getanchor ();
6032 let margin = (state.w - l.pagew)/2 in
6033 state.x <- -truncate x0 - margin;
6034 setzoom zoom;
6035 None
6036 | None -> None
6038 match conf.columns with
6039 | Csplit _ ->
6040 showtext '!' "block zooming does not work properly in split columns mode"
6041 | _ -> onppundermouse g x y ()
6044 let scrollx x =
6045 let winw = wadjsb state.winw - 1 in
6046 let s = float x /. float winw in
6047 let destx = truncate (float (state.w + winw) *. s) in
6048 state.x <- winw - destx;
6049 gotoy_and_clear_text state.y;
6050 state.mstate <- Mscrollx;
6053 let scrolly y =
6054 let s = float y /. float state.winh in
6055 let desty = truncate (float (state.maxy - state.winh) *. s) in
6056 gotoy_and_clear_text desty;
6057 state.mstate <- Mscrolly;
6060 let viewmouse button down x y mask =
6061 match button with
6062 | n when (n == 4 || n == 5) && not down ->
6063 if Wsi.withctrl mask
6064 then (
6065 match state.mstate with
6066 | Mzoom (oldn, i) ->
6067 if oldn = n
6068 then (
6069 if i = 2
6070 then
6071 let incr =
6072 match n with
6073 | 5 ->
6074 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6075 | _ ->
6076 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6078 let zoom = conf.zoom -. incr in
6079 setzoom zoom;
6080 state.mstate <- Mzoom (n, 0);
6081 else
6082 state.mstate <- Mzoom (n, i+1);
6084 else state.mstate <- Mzoom (n, 0)
6086 | _ -> state.mstate <- Mzoom (n, 0)
6088 else (
6089 match state.autoscroll with
6090 | Some step -> setautoscrollspeed step (n=4)
6091 | None ->
6092 if conf.wheelbypage || conf.presentation
6093 then (
6094 if n = 4
6095 then prevpage ()
6096 else nextpage ()
6098 else
6099 let incr =
6100 if n = 4
6101 then -conf.scrollstep
6102 else conf.scrollstep
6104 let incr = incr * 2 in
6105 let y = clamp incr in
6106 gotoy_and_clear_text y
6109 | n when (n = 6 || n = 7) && not down && canpan () ->
6110 state.x <-
6111 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6112 gotoy_and_clear_text state.y
6114 | 1 when Wsi.withshift mask ->
6115 state.mstate <- Mnone;
6116 if not down
6117 then (
6118 match unproject x y with
6119 | Some (pageno, ux, uy) ->
6120 let cmd = Printf.sprintf
6121 "%s %s %d %d %d"
6122 conf.stcmd state.path pageno ux uy
6124 popen cmd []
6125 | None -> ()
6128 | 1 when Wsi.withctrl mask ->
6129 if down
6130 then (
6131 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6132 state.mstate <- Mpan (x, y)
6134 else
6135 state.mstate <- Mnone
6137 | 3 ->
6138 if down
6139 then (
6140 Wsi.setcursor Wsi.CURSOR_CYCLE;
6141 let p = (x, y) in
6142 state.mstate <- Mzoomrect (p, p)
6144 else (
6145 match state.mstate with
6146 | Mzoomrect ((x0, y0), _) ->
6147 if abs (x-x0) > 10 && abs (y - y0) > 10
6148 then zoomrect x0 y0 x y
6149 else (
6150 state.mstate <- Mnone;
6151 Wsi.setcursor Wsi.CURSOR_INHERIT;
6152 G.postRedisplay "kill accidental zoom rect";
6154 | _ ->
6155 Wsi.setcursor Wsi.CURSOR_INHERIT;
6156 state.mstate <- Mnone
6159 | 1 when x > state.winw - vscrollw () ->
6160 if down
6161 then
6162 let _, position, sh = state.uioh#scrollph in
6163 if y > truncate position && y < truncate (position +. sh)
6164 then state.mstate <- Mscrolly
6165 else scrolly y
6166 else
6167 state.mstate <- Mnone
6169 | 1 when y > state.winh - hscrollh () ->
6170 if down
6171 then
6172 let _, position, sw = state.uioh#scrollpw in
6173 if x > truncate position && x < truncate (position +. sw)
6174 then state.mstate <- Mscrollx
6175 else scrollx x
6176 else
6177 state.mstate <- Mnone
6179 | 1 when state.bzoom -> if not down then zoomblock x y
6181 | 1 ->
6182 let dest = if down then getunder x y else Unone in
6183 begin match dest with
6184 | Ulinkgoto _
6185 | Ulinkuri _
6186 | Uremote _
6187 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6188 gotounder dest
6190 | Unone when down ->
6191 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6192 state.mstate <- Mpan (x, y);
6194 | Unone | Utext _ ->
6195 if down
6196 then (
6197 if conf.angle mod 360 = 0
6198 then (
6199 state.mstate <- Msel ((x, y), (x, y));
6200 G.postRedisplay "mouse select";
6203 else (
6204 match state.mstate with
6205 | Mnone -> ()
6207 | Mzoom _ | Mscrollx | Mscrolly ->
6208 state.mstate <- Mnone
6210 | Mzoomrect ((x0, y0), _) ->
6211 zoomrect x0 y0 x y
6213 | Mpan _ ->
6214 Wsi.setcursor Wsi.CURSOR_INHERIT;
6215 state.mstate <- Mnone
6217 | Msel ((x0, y0), (x1, y1)) ->
6218 let rec loop = function
6219 | [] -> ()
6220 | l :: rest ->
6221 let inside =
6222 let a0 = l.pagedispy in
6223 let a1 = a0 + l.pagevh in
6224 let b0 = l.pagedispx in
6225 let b1 = b0 + l.pagevw in
6226 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6227 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6229 if inside
6230 then
6231 match getopaque l.pageno with
6232 | Some opaque ->
6233 begin
6234 match Ne.pipe () with
6235 | Ne.Exn exn ->
6236 showtext '!'
6237 (Printf.sprintf
6238 "can not create sel pipe: %s"
6239 (exntos exn));
6240 | Ne.Res (r, w) ->
6241 let doclose what fd =
6242 Ne.clo fd (fun msg ->
6243 dolog "%s close failed: %s" what msg)
6246 popen conf.selcmd [r, 0; w, -1];
6247 copysel w opaque true;
6248 doclose "pipe/r" r;
6249 G.postRedisplay "copysel";
6250 with exn ->
6251 dolog "can not execute %S: %s"
6252 conf.selcmd (exntos exn);
6253 doclose "pipe/r" r;
6254 doclose "pipe/w" w;
6256 | None -> ()
6257 else loop rest
6259 loop state.layout;
6260 Wsi.setcursor Wsi.CURSOR_INHERIT;
6261 state.mstate <- Mnone;
6265 | _ -> ()
6268 let birdseyemouse button down x y mask
6269 (conf, leftx, _, hooverpageno, anchor) =
6270 match button with
6271 | 1 when down ->
6272 let rec loop = function
6273 | [] -> ()
6274 | l :: rest ->
6275 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6276 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6277 then (
6278 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6280 else loop rest
6282 loop state.layout
6283 | 3 -> ()
6284 | _ -> viewmouse button down x y mask
6287 let mouse button down x y mask =
6288 state.uioh <- state.uioh#button button down x y mask;
6291 let motion ~x ~y =
6292 state.uioh <- state.uioh#motion x y
6295 let pmotion ~x ~y =
6296 state.uioh <- state.uioh#pmotion x y;
6299 let uioh = object
6300 method display = ()
6302 method key key mask =
6303 begin match state.mode with
6304 | Textentry textentry -> textentrykeyboard key mask textentry
6305 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6306 | View -> viewkeyboard key mask
6307 | LinkNav linknav -> linknavkeyboard key mask linknav
6308 end;
6309 state.uioh
6311 method button button bstate x y mask =
6312 begin match state.mode with
6313 | LinkNav _
6314 | View -> viewmouse button bstate x y mask
6315 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6316 | Textentry _ -> ()
6317 end;
6318 state.uioh
6320 method motion x y =
6321 begin match state.mode with
6322 | Textentry _ -> ()
6323 | View | Birdseye _ | LinkNav _ ->
6324 match state.mstate with
6325 | Mzoom _ | Mnone -> ()
6327 | Mpan (x0, y0) ->
6328 let dx = x - x0
6329 and dy = y0 - y in
6330 state.mstate <- Mpan (x, y);
6331 if canpan ()
6332 then state.x <- panbound (state.x + dx);
6333 let y = clamp dy in
6334 gotoy_and_clear_text y
6336 | Msel (a, _) ->
6337 state.mstate <- Msel (a, (x, y));
6338 G.postRedisplay "motion select";
6340 | Mscrolly ->
6341 let y = min state.winh (max 0 y) in
6342 scrolly y
6344 | Mscrollx ->
6345 let x = min state.winw (max 0 x) in
6346 scrollx x
6348 | Mzoomrect (p0, _) ->
6349 state.mstate <- Mzoomrect (p0, (x, y));
6350 G.postRedisplay "motion zoomrect";
6351 end;
6352 state.uioh
6354 method pmotion x y =
6355 begin match state.mode with
6356 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6357 let rec loop = function
6358 | [] ->
6359 if hooverpageno != -1
6360 then (
6361 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6362 G.postRedisplay "pmotion birdseye no hoover";
6364 | l :: rest ->
6365 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6366 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6367 then (
6368 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6369 G.postRedisplay "pmotion birdseye hoover";
6371 else loop rest
6373 loop state.layout
6375 | Textentry _ -> ()
6377 | LinkNav _
6378 | View ->
6379 match state.mstate with
6380 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6382 | Mnone ->
6383 updateunder x y;
6384 match conf.pax with
6385 | None -> ()
6386 | Some r ->
6387 let past, _, _ = !r in
6388 let now = now () in
6389 let delta = now -. past in
6390 if delta > 0.01
6391 then paxunder x y
6392 else r := (now, x, y)
6393 end;
6394 state.uioh
6396 method infochanged _ = ()
6398 method scrollph =
6399 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6400 let p, h =
6401 if maxy = 0
6402 then 0.0, float state.winh
6403 else scrollph state.y maxy
6405 vscrollw (), p, h
6407 method scrollpw =
6408 let winw = wadjsb state.winw in
6409 let fwinw = float winw in
6410 let sw =
6411 let sw = fwinw /. float state.w in
6412 let sw = fwinw *. sw in
6413 max sw (float conf.scrollh)
6415 let position =
6416 let maxx = state.w + winw in
6417 let x = winw - state.x in
6418 let percent = float x /. float maxx in
6419 (fwinw -. sw) *. percent
6421 hscrollh (), position, sw
6423 method modehash =
6424 let modename =
6425 match state.mode with
6426 | LinkNav _ -> "links"
6427 | Textentry _ -> "textentry"
6428 | Birdseye _ -> "birdseye"
6429 | View -> "view"
6431 findkeyhash conf modename
6433 method eformsgs = true
6434 end;;
6436 module Config =
6437 struct
6438 open Parser
6440 let fontpath = ref "";;
6442 module KeyMap =
6443 Map.Make (struct type t = (int * int) let compare = compare end);;
6445 let unent s =
6446 let l = String.length s in
6447 let b = Buffer.create l in
6448 unent b s 0 l;
6449 Buffer.contents b;
6452 let home =
6453 try Sys.getenv "HOME"
6454 with exn ->
6455 prerr_endline
6456 ("Can not determine home directory location: " ^ exntos exn);
6460 let modifier_of_string = function
6461 | "alt" -> Wsi.altmask
6462 | "shift" -> Wsi.shiftmask
6463 | "ctrl" | "control" -> Wsi.ctrlmask
6464 | "meta" -> Wsi.metamask
6465 | _ -> 0
6468 let key_of_string =
6469 let r = Str.regexp "-" in
6470 fun s ->
6471 let elems = Str.full_split r s in
6472 let f n k m =
6473 let g s =
6474 let m1 = modifier_of_string s in
6475 if m1 = 0
6476 then (Wsi.namekey s, m)
6477 else (k, m lor m1)
6478 in function
6479 | Str.Delim s when n land 1 = 0 -> g s
6480 | Str.Text s -> g s
6481 | Str.Delim _ -> (k, m)
6483 let rec loop n k m = function
6484 | [] -> (k, m)
6485 | x :: xs ->
6486 let k, m = f n k m x in
6487 loop (n+1) k m xs
6489 loop 0 0 0 elems
6492 let keys_of_string =
6493 let r = Str.regexp "[ \t]" in
6494 fun s ->
6495 let elems = Str.split r s in
6496 List.map key_of_string elems
6499 let copykeyhashes c =
6500 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6503 let config_of c attrs =
6504 let apply c k v =
6506 match k with
6507 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6508 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6509 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6510 | "preload" -> { c with preload = bool_of_string v }
6511 | "page-bias" -> { c with pagebias = int_of_string v }
6512 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6513 | "horizontal-scroll-step" ->
6514 { c with hscrollstep = max (int_of_string v) 1 }
6515 | "auto-scroll-step" ->
6516 { c with autoscrollstep = max 0 (int_of_string v) }
6517 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6518 | "crop-hack" -> { c with crophack = bool_of_string v }
6519 | "throttle" ->
6520 let mw =
6521 match String.lowercase v with
6522 | "true" -> Some infinity
6523 | "false" -> None
6524 | f -> Some (float_of_string f)
6526 { c with maxwait = mw}
6527 | "highlight-links" -> { c with hlinks = bool_of_string v }
6528 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6529 | "vertical-margin" ->
6530 { c with interpagespace = max 0 (int_of_string v) }
6531 | "zoom" ->
6532 let zoom = float_of_string v /. 100. in
6533 let zoom = max zoom 0.0 in
6534 { c with zoom = zoom }
6535 | "presentation" -> { c with presentation = bool_of_string v }
6536 | "rotation-angle" -> { c with angle = int_of_string v }
6537 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6538 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6539 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6540 | "proportional-display" ->
6541 let fm =
6542 if bool_of_string v
6543 then FitProportional
6544 else FitWidth
6546 { c with fitmodel = fm }
6547 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6548 | "pixmap-cache-size" ->
6549 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6550 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6551 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6552 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6553 | "persistent-location" -> { c with jumpback = bool_of_string v }
6554 | "background-color" -> { c with bgcolor = color_of_string v }
6555 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6556 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6557 | "mupdf-store-size" ->
6558 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6559 | "checkers" -> { c with checkers = bool_of_string v }
6560 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6561 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6562 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6563 | "uri-launcher" -> { c with urilauncher = unent v }
6564 | "path-launcher" -> { c with pathlauncher = unent v }
6565 | "color-space" -> { c with colorspace = CSTE.of_string v }
6566 | "invert-colors" -> { c with invert = bool_of_string v }
6567 | "brightness" -> { c with colorscale = float_of_string v }
6568 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6569 | "ghyllscroll" ->
6570 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6571 | "columns" ->
6572 let (n, _, _) as nab = multicolumns_of_string v in
6573 if n < 0
6574 then { c with columns = Csplit (-n, [||]) }
6575 else { c with columns = Cmulti (nab, [||]) }
6576 | "birds-eye-columns" ->
6577 { c with beyecolumns = Some (max (int_of_string v) 2) }
6578 | "selection-command" -> { c with selcmd = unent v }
6579 | "synctex-command" -> { c with stcmd = unent v }
6580 | "pax-command" -> { c with paxcmd = unent v }
6581 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6582 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6583 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6584 | "use-pbo" -> { c with usepbo = bool_of_string v }
6585 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6586 | "horizontal-scrollbar-visible" ->
6587 let b =
6588 if bool_of_string v
6589 then c.scrollb lor scrollbhv
6590 else c.scrollb land (lnot scrollbhv)
6592 { c with scrollb = b }
6593 | "vertical-scrollbar-visible" ->
6594 let b =
6595 if bool_of_string v
6596 then c.scrollb lor scrollbvv
6597 else c.scrollb land (lnot scrollbvv)
6599 { c with scrollb = b }
6600 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6601 | "point-and-x" ->
6602 { c with pax =
6603 if bool_of_string v
6604 then Some (ref (0.0, 0, 0))
6605 else None }
6606 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6607 | _ -> c
6608 with exn ->
6609 prerr_endline ("Error processing attribute (`" ^
6610 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6613 let rec fold c = function
6614 | [] -> c
6615 | (k, v) :: rest ->
6616 let c = apply c k v in
6617 fold c rest
6619 fold { c with keyhashes = copykeyhashes c } attrs;
6622 let fromstring f pos n v d =
6623 try f v
6624 with exn ->
6625 dolog "Error processing attribute (%S=%S) at %d\n%s"
6626 n v pos (exntos exn)
6631 let bookmark_of attrs =
6632 let rec fold title page rely visy = function
6633 | ("title", v) :: rest -> fold v page rely visy rest
6634 | ("page", v) :: rest -> fold title v rely visy rest
6635 | ("rely", v) :: rest -> fold title page v visy rest
6636 | ("visy", v) :: rest -> fold title page rely v rest
6637 | _ :: rest -> fold title page rely visy rest
6638 | [] -> title, page, rely, visy
6640 fold "invalid" "0" "0" "0" attrs
6643 let doc_of attrs =
6644 let rec fold path page rely pan visy = function
6645 | ("path", v) :: rest -> fold v page rely pan visy rest
6646 | ("page", v) :: rest -> fold path v rely pan visy rest
6647 | ("rely", v) :: rest -> fold path page v pan visy rest
6648 | ("pan", v) :: rest -> fold path page rely v visy rest
6649 | ("visy", v) :: rest -> fold path page rely pan v rest
6650 | _ :: rest -> fold path page rely pan visy rest
6651 | [] -> path, page, rely, pan, visy
6653 fold "" "0" "0" "0" "0" attrs
6656 let map_of attrs =
6657 let rec fold rs ls = function
6658 | ("out", v) :: rest -> fold v ls rest
6659 | ("in", v) :: rest -> fold rs v rest
6660 | _ :: rest -> fold ls rs rest
6661 | [] -> ls, rs
6663 fold "" "" attrs
6666 let setconf dst src =
6667 dst.scrollbw <- src.scrollbw;
6668 dst.scrollh <- src.scrollh;
6669 dst.icase <- src.icase;
6670 dst.preload <- src.preload;
6671 dst.pagebias <- src.pagebias;
6672 dst.verbose <- src.verbose;
6673 dst.scrollstep <- src.scrollstep;
6674 dst.maxhfit <- src.maxhfit;
6675 dst.crophack <- src.crophack;
6676 dst.autoscrollstep <- src.autoscrollstep;
6677 dst.maxwait <- src.maxwait;
6678 dst.hlinks <- src.hlinks;
6679 dst.underinfo <- src.underinfo;
6680 dst.interpagespace <- src.interpagespace;
6681 dst.zoom <- src.zoom;
6682 dst.presentation <- src.presentation;
6683 dst.angle <- src.angle;
6684 dst.cwinw <- src.cwinw;
6685 dst.cwinh <- src.cwinh;
6686 dst.savebmarks <- src.savebmarks;
6687 dst.memlimit <- src.memlimit;
6688 dst.fitmodel <- src.fitmodel;
6689 dst.texcount <- src.texcount;
6690 dst.sliceheight <- src.sliceheight;
6691 dst.thumbw <- src.thumbw;
6692 dst.jumpback <- src.jumpback;
6693 dst.bgcolor <- src.bgcolor;
6694 dst.tilew <- src.tilew;
6695 dst.tileh <- src.tileh;
6696 dst.mustoresize <- src.mustoresize;
6697 dst.checkers <- src.checkers;
6698 dst.aalevel <- src.aalevel;
6699 dst.trimmargins <- src.trimmargins;
6700 dst.trimfuzz <- src.trimfuzz;
6701 dst.urilauncher <- src.urilauncher;
6702 dst.colorspace <- src.colorspace;
6703 dst.invert <- src.invert;
6704 dst.colorscale <- src.colorscale;
6705 dst.redirectstderr <- src.redirectstderr;
6706 dst.ghyllscroll <- src.ghyllscroll;
6707 dst.columns <- src.columns;
6708 dst.beyecolumns <- src.beyecolumns;
6709 dst.selcmd <- src.selcmd;
6710 dst.updatecurs <- src.updatecurs;
6711 dst.pathlauncher <- src.pathlauncher;
6712 dst.keyhashes <- copykeyhashes src;
6713 dst.hfsize <- src.hfsize;
6714 dst.hscrollstep <- src.hscrollstep;
6715 dst.pgscale <- src.pgscale;
6716 dst.usepbo <- src.usepbo;
6717 dst.wheelbypage <- src.wheelbypage;
6718 dst.stcmd <- src.stcmd;
6719 dst.paxcmd <- src.paxcmd;
6720 dst.scrollb <- src.scrollb;
6721 dst.riani <- src.riani;
6722 dst.paxmark <- src.paxmark;
6723 dst.pax <-
6724 if src.pax = None
6725 then None
6726 else Some ((ref (0.0, 0, 0)));
6729 let get s =
6730 let h = Hashtbl.create 10 in
6731 let dc = { defconf with angle = defconf.angle } in
6732 let rec toplevel v t spos _ =
6733 match t with
6734 | Vdata | Vcdata | Vend -> v
6735 | Vopen ("llppconfig", _, closed) ->
6736 if closed
6737 then v
6738 else { v with f = llppconfig }
6739 | Vopen _ ->
6740 error "unexpected subelement at top level" s spos
6741 | Vclose _ -> error "unexpected close at top level" s spos
6743 and llppconfig v t spos _ =
6744 match t with
6745 | Vdata | Vcdata -> v
6746 | Vend -> error "unexpected end of input in llppconfig" s spos
6747 | Vopen ("defaults", attrs, closed) ->
6748 let c = config_of dc attrs in
6749 setconf dc c;
6750 if closed
6751 then v
6752 else { v with f = defaults }
6754 | Vopen ("ui-font", attrs, closed) ->
6755 let rec getsize size = function
6756 | [] -> size
6757 | ("size", v) :: rest ->
6758 let size =
6759 fromstring int_of_string spos "size" v fstate.fontsize in
6760 getsize size rest
6761 | l -> getsize size l
6763 fstate.fontsize <- getsize fstate.fontsize attrs;
6764 if closed
6765 then v
6766 else { v with f = uifont (Buffer.create 10) }
6768 | Vopen ("doc", attrs, closed) ->
6769 let pathent, spage, srely, span, svisy = doc_of attrs in
6770 let path = unent pathent
6771 and pageno = fromstring int_of_string spos "page" spage 0
6772 and rely = fromstring float_of_string spos "rely" srely 0.0
6773 and pan = fromstring int_of_string spos "pan" span 0
6774 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6775 let c = config_of dc attrs in
6776 let anchor = (pageno, rely, visy) in
6777 if closed
6778 then (Hashtbl.add h path (c, [], pan, anchor); v)
6779 else { v with f = doc path pan anchor c [] }
6781 | Vopen _ ->
6782 error "unexpected subelement in llppconfig" s spos
6784 | Vclose "llppconfig" -> { v with f = toplevel }
6785 | Vclose _ -> error "unexpected close in llppconfig" s spos
6787 and defaults v t spos _ =
6788 match t with
6789 | Vdata | Vcdata -> v
6790 | Vend -> error "unexpected end of input in defaults" s spos
6791 | Vopen ("keymap", attrs, closed) ->
6792 let modename =
6793 try List.assoc "mode" attrs
6794 with Not_found -> "global" in
6795 if closed
6796 then v
6797 else
6798 let ret keymap =
6799 let h = findkeyhash dc modename in
6800 KeyMap.iter (Hashtbl.replace h) keymap;
6801 defaults
6803 { v with f = pkeymap ret KeyMap.empty }
6805 | Vopen (_, _, _) ->
6806 error "unexpected subelement in defaults" s spos
6808 | Vclose "defaults" ->
6809 { v with f = llppconfig }
6811 | Vclose _ -> error "unexpected close in defaults" s spos
6813 and uifont b v t spos epos =
6814 match t with
6815 | Vdata | Vcdata ->
6816 Buffer.add_substring b s spos (epos - spos);
6818 | Vopen (_, _, _) ->
6819 error "unexpected subelement in ui-font" s spos
6820 | Vclose "ui-font" ->
6821 if String.length !fontpath = 0
6822 then fontpath := Buffer.contents b;
6823 { v with f = llppconfig }
6824 | Vclose _ -> error "unexpected close in ui-font" s spos
6825 | Vend -> error "unexpected end of input in ui-font" s spos
6827 and doc path pan anchor c bookmarks v t spos _ =
6828 match t with
6829 | Vdata | Vcdata -> v
6830 | Vend -> error "unexpected end of input in doc" s spos
6831 | Vopen ("bookmarks", _, closed) ->
6832 if closed
6833 then v
6834 else { v with f = pbookmarks path pan anchor c bookmarks }
6836 | Vopen ("keymap", attrs, closed) ->
6837 let modename =
6838 try List.assoc "mode" attrs
6839 with Not_found -> "global"
6841 if closed
6842 then v
6843 else
6844 let ret keymap =
6845 let h = findkeyhash c modename in
6846 KeyMap.iter (Hashtbl.replace h) keymap;
6847 doc path pan anchor c bookmarks
6849 { v with f = pkeymap ret KeyMap.empty }
6851 | Vopen (_, _, _) ->
6852 error "unexpected subelement in doc" s spos
6854 | Vclose "doc" ->
6855 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6856 { v with f = llppconfig }
6858 | Vclose _ -> error "unexpected close in doc" s spos
6860 and pkeymap ret keymap v t spos _ =
6861 match t with
6862 | Vdata | Vcdata -> v
6863 | Vend -> error "unexpected end of input in keymap" s spos
6864 | Vopen ("map", attrs, closed) ->
6865 let r, l = map_of attrs in
6866 let kss = fromstring keys_of_string spos "in" r [] in
6867 let lss = fromstring keys_of_string spos "out" l [] in
6868 let keymap =
6869 match kss with
6870 | [] -> keymap
6871 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6872 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6874 if closed
6875 then { v with f = pkeymap ret keymap }
6876 else
6877 let f () = v in
6878 { v with f = skip "map" f }
6880 | Vopen _ ->
6881 error "unexpected subelement in keymap" s spos
6883 | Vclose "keymap" ->
6884 { v with f = ret keymap }
6886 | Vclose _ -> error "unexpected close in keymap" s spos
6888 and pbookmarks path pan anchor c bookmarks v t spos _ =
6889 match t with
6890 | Vdata | Vcdata -> v
6891 | Vend -> error "unexpected end of input in bookmarks" s spos
6892 | Vopen ("item", attrs, closed) ->
6893 let titleent, spage, srely, svisy = bookmark_of attrs in
6894 let page = fromstring int_of_string spos "page" spage 0
6895 and rely = fromstring float_of_string spos "rely" srely 0.0
6896 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6897 let bookmarks =
6898 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6900 if closed
6901 then { v with f = pbookmarks path pan anchor c bookmarks }
6902 else
6903 let f () = v in
6904 { v with f = skip "item" f }
6906 | Vopen _ ->
6907 error "unexpected subelement in bookmarks" s spos
6909 | Vclose "bookmarks" ->
6910 { v with f = doc path pan anchor c bookmarks }
6912 | Vclose _ -> error "unexpected close in bookmarks" s spos
6914 and skip tag f v t spos _ =
6915 match t with
6916 | Vdata | Vcdata -> v
6917 | Vend ->
6918 error ("unexpected end of input in skipped " ^ tag) s spos
6919 | Vopen (tag', _, closed) ->
6920 if closed
6921 then v
6922 else
6923 let f' () = { v with f = skip tag f } in
6924 { v with f = skip tag' f' }
6925 | Vclose ctag ->
6926 if tag = ctag
6927 then f ()
6928 else error ("unexpected close in skipped " ^ tag) s spos
6931 parse { f = toplevel; accu = () } s;
6932 h, dc;
6935 let do_load f ic =
6937 let len = in_channel_length ic in
6938 let s = String.create len in
6939 really_input ic s 0 len;
6940 f s;
6941 with
6942 | Parse_error (msg, s, pos) ->
6943 let subs = subs s pos in
6944 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6945 failwith ("parse error: " ^ s)
6947 | exn ->
6948 failwith ("config load error: " ^ exntos exn)
6951 let defconfpath =
6952 let dir =
6954 let dir = Filename.concat home ".config" in
6955 if Sys.is_directory dir then dir else home
6956 with _ -> home
6958 Filename.concat dir "llpp.conf"
6961 let confpath = ref defconfpath;;
6963 let load1 f =
6964 if Sys.file_exists !confpath
6965 then
6966 match
6967 (try Some (open_in_bin !confpath)
6968 with exn ->
6969 prerr_endline
6970 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6971 exntos exn);
6972 None
6974 with
6975 | Some ic ->
6976 let success =
6978 f (do_load get ic)
6979 with exn ->
6980 prerr_endline
6981 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6982 exntos exn);
6983 false
6985 close_in ic;
6986 success
6988 | None -> false
6989 else
6990 f (Hashtbl.create 0, defconf)
6993 let load () =
6994 let f (h, dc) =
6995 let pc, pb, px, pa =
6997 let key =
6998 if String.length state.origin = 0
6999 then state.path
7000 else state.origin
7002 Hashtbl.find h (Filename.basename key)
7003 with Not_found -> dc, [], 0, emptyanchor
7005 setconf defconf dc;
7006 setconf conf pc;
7007 state.bookmarks <- pb;
7008 state.x <- px;
7009 if conf.jumpback
7010 then state.anchor <- pa;
7011 cbput state.hists.nav pa;
7012 true
7014 load1 f
7017 let add_attrs bb always dc c =
7018 let ob s a b =
7019 if always || a != b
7020 then Printf.bprintf bb "\n %s='%b'" s a
7021 and op s a b =
7022 if always || a <> b
7023 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7024 and oi s a b =
7025 if always || a != b
7026 then Printf.bprintf bb "\n %s='%d'" s a
7027 and oI s a b =
7028 if always || a != b
7029 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7030 and oz s a b =
7031 if always || a <> b
7032 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7033 and oF s a b =
7034 if always || a <> b
7035 then Printf.bprintf bb "\n %s='%f'" s a
7036 and oc s a b =
7037 if always || a <> b
7038 then
7039 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7040 and oC s a b =
7041 if always || a <> b
7042 then
7043 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7044 and oR s a b =
7045 if always || a <> b
7046 then
7047 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7048 and os s a b =
7049 if always || a <> b
7050 then
7051 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7052 and og s a b =
7053 if always || a <> b
7054 then
7055 match a with
7056 | None -> ()
7057 | Some (_N, _A, _B) ->
7058 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7059 and oW s a b =
7060 if always || a <> b
7061 then
7062 let v =
7063 match a with
7064 | None -> "false"
7065 | Some f ->
7066 if f = infinity
7067 then "true"
7068 else string_of_float f
7070 Printf.bprintf bb "\n %s='%s'" s v
7071 and oco s a b =
7072 if always || a <> b
7073 then
7074 match a with
7075 | Cmulti ((n, a, b), _) when n > 1 ->
7076 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7077 | Csplit (n, _) when n > 1 ->
7078 Printf.bprintf bb "\n %s='%d'" s ~-n
7079 | _ -> ()
7080 and obeco s a b =
7081 if always || a <> b
7082 then
7083 match a with
7084 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7085 | _ -> ()
7086 and oFm s a b =
7087 if always || a <> b
7088 then
7089 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7090 and oSv s a b m =
7091 if always || a <> b
7092 then
7093 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7094 and oPm s a b =
7095 if always || a <> b
7096 then
7097 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7099 oi "width" c.cwinw dc.cwinw;
7100 oi "height" c.cwinh dc.cwinh;
7101 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7102 oi "scroll-handle-height" c.scrollh dc.scrollh;
7103 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7104 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7105 ob "case-insensitive-search" c.icase dc.icase;
7106 ob "preload" c.preload dc.preload;
7107 oi "page-bias" c.pagebias dc.pagebias;
7108 oi "scroll-step" c.scrollstep dc.scrollstep;
7109 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7110 ob "max-height-fit" c.maxhfit dc.maxhfit;
7111 ob "crop-hack" c.crophack dc.crophack;
7112 oW "throttle" c.maxwait dc.maxwait;
7113 ob "highlight-links" c.hlinks dc.hlinks;
7114 ob "under-cursor-info" c.underinfo dc.underinfo;
7115 oi "vertical-margin" c.interpagespace dc.interpagespace;
7116 oz "zoom" c.zoom dc.zoom;
7117 ob "presentation" c.presentation dc.presentation;
7118 oi "rotation-angle" c.angle dc.angle;
7119 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7120 oFm "fit-model" c.fitmodel dc.fitmodel;
7121 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7122 oi "tex-count" c.texcount dc.texcount;
7123 oi "slice-height" c.sliceheight dc.sliceheight;
7124 oi "thumbnail-width" c.thumbw dc.thumbw;
7125 ob "persistent-location" c.jumpback dc.jumpback;
7126 oc "background-color" c.bgcolor dc.bgcolor;
7127 oi "tile-width" c.tilew dc.tilew;
7128 oi "tile-height" c.tileh dc.tileh;
7129 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7130 ob "checkers" c.checkers dc.checkers;
7131 oi "aalevel" c.aalevel dc.aalevel;
7132 ob "trim-margins" c.trimmargins dc.trimmargins;
7133 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7134 os "uri-launcher" c.urilauncher dc.urilauncher;
7135 os "path-launcher" c.pathlauncher dc.pathlauncher;
7136 oC "color-space" c.colorspace dc.colorspace;
7137 ob "invert-colors" c.invert dc.invert;
7138 oF "brightness" c.colorscale dc.colorscale;
7139 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7140 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7141 oco "columns" c.columns dc.columns;
7142 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7143 os "selection-command" c.selcmd dc.selcmd;
7144 os "synctex-command" c.stcmd dc.stcmd;
7145 os "pax-command" c.paxcmd dc.paxcmd;
7146 ob "update-cursor" c.updatecurs dc.updatecurs;
7147 oi "hint-font-size" c.hfsize dc.hfsize;
7148 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7149 oF "page-scroll-scale" c.pgscale dc.pgscale;
7150 ob "use-pbo" c.usepbo dc.usepbo;
7151 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7152 ob "remote-in-a-new-instance" c.riani dc.riani;
7153 op "point-and-x" c.pax dc.pax;
7154 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7157 let keymapsbuf always dc c =
7158 let bb = Buffer.create 16 in
7159 let rec loop = function
7160 | [] -> ()
7161 | (modename, h) :: rest ->
7162 let dh = findkeyhash dc modename in
7163 if always || h <> dh
7164 then (
7165 if Hashtbl.length h > 0
7166 then (
7167 if Buffer.length bb > 0
7168 then Buffer.add_char bb '\n';
7169 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7170 Hashtbl.iter (fun i o ->
7171 let isdifferent = always ||
7173 let dO = Hashtbl.find dh i in
7174 dO <> o
7175 with Not_found -> true
7177 if isdifferent
7178 then
7179 let addkm (k, m) =
7180 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7181 if Wsi.withalt m then Buffer.add_string bb "alt-";
7182 if Wsi.withshift m then Buffer.add_string bb "shift-";
7183 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7184 Buffer.add_string bb (Wsi.keyname k);
7186 let addkms l =
7187 let rec loop = function
7188 | [] -> ()
7189 | km :: [] -> addkm km
7190 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7192 loop l
7194 Buffer.add_string bb "<map in='";
7195 addkm i;
7196 match o with
7197 | KMinsrt km ->
7198 Buffer.add_string bb "' out='";
7199 addkm km;
7200 Buffer.add_string bb "'/>\n"
7202 | KMinsrl kms ->
7203 Buffer.add_string bb "' out='";
7204 addkms kms;
7205 Buffer.add_string bb "'/>\n"
7207 | KMmulti (ins, kms) ->
7208 Buffer.add_char bb ' ';
7209 addkms ins;
7210 Buffer.add_string bb "' out='";
7211 addkms kms;
7212 Buffer.add_string bb "'/>\n"
7213 ) h;
7214 Buffer.add_string bb "</keymap>";
7217 loop rest
7219 loop c.keyhashes;
7223 let save () =
7224 let uifontsize = fstate.fontsize in
7225 let bb = Buffer.create 32768 in
7226 let relx = float state.x /. float state.winw in
7227 let w, h, x =
7228 let cx w = truncate (relx *. float w) in
7229 List.fold_left
7230 (fun (w, h, x) ws ->
7231 match ws with
7232 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7233 | Wsi.MaxVert -> (w, conf.cwinh, x)
7234 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7236 (state.winw, state.winh, state.x) state.winstate
7238 conf.cwinw <- w;
7239 conf.cwinh <- h;
7240 let f (h, dc) =
7241 let dc = if conf.bedefault then conf else dc in
7242 Buffer.add_string bb "<llppconfig>\n";
7244 if String.length !fontpath > 0
7245 then
7246 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7247 uifontsize
7248 !fontpath
7249 else (
7250 if uifontsize <> 14
7251 then
7252 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7255 Buffer.add_string bb "<defaults ";
7256 add_attrs bb true dc dc;
7257 let kb = keymapsbuf true dc dc in
7258 if Buffer.length kb > 0
7259 then (
7260 Buffer.add_string bb ">\n";
7261 Buffer.add_buffer bb kb;
7262 Buffer.add_string bb "\n</defaults>\n";
7264 else Buffer.add_string bb "/>\n";
7266 let adddoc path pan anchor c bookmarks =
7267 if bookmarks == [] && c = dc && anchor = emptyanchor
7268 then ()
7269 else (
7270 Printf.bprintf bb "<doc path='%s'"
7271 (enent path 0 (String.length path));
7273 if anchor <> emptyanchor
7274 then (
7275 let n, rely, visy = anchor in
7276 Printf.bprintf bb " page='%d'" n;
7277 if rely > 1e-6
7278 then
7279 Printf.bprintf bb " rely='%f'" rely
7281 if abs_float visy > 1e-6
7282 then
7283 Printf.bprintf bb " visy='%f'" visy
7287 if pan != 0
7288 then Printf.bprintf bb " pan='%d'" pan;
7290 add_attrs bb false dc c;
7291 let kb = keymapsbuf false dc c in
7293 begin match bookmarks with
7294 | [] ->
7295 if Buffer.length kb > 0
7296 then (
7297 Buffer.add_string bb ">\n";
7298 Buffer.add_buffer bb kb;
7299 Buffer.add_string bb "\n</doc>\n";
7301 else Buffer.add_string bb "/>\n"
7302 | _ ->
7303 Buffer.add_string bb ">\n<bookmarks>\n";
7304 List.iter (fun (title, _level, (page, rely, visy)) ->
7305 Printf.bprintf bb
7306 "<item title='%s' page='%d'"
7307 (enent title 0 (String.length title))
7308 page
7310 if rely > 1e-6
7311 then
7312 Printf.bprintf bb " rely='%f'" rely
7314 if abs_float visy > 1e-6
7315 then
7316 Printf.bprintf bb " visy='%f'" visy
7318 Buffer.add_string bb "/>\n";
7319 ) bookmarks;
7320 Buffer.add_string bb "</bookmarks>";
7321 if Buffer.length kb > 0
7322 then (
7323 Buffer.add_string bb "\n";
7324 Buffer.add_buffer bb kb;
7326 Buffer.add_string bb "\n</doc>\n";
7327 end;
7331 let pan, conf =
7332 match state.mode with
7333 | Birdseye (c, pan, _, _, _) ->
7334 let beyecolumns =
7335 match conf.columns with
7336 | Cmulti ((c, _, _), _) -> Some c
7337 | Csingle _ -> None
7338 | Csplit _ -> None
7339 and columns =
7340 match c.columns with
7341 | Cmulti (c, _) -> Cmulti (c, [||])
7342 | Csingle _ -> Csingle [||]
7343 | Csplit _ -> failwith "quit from bird's eye while split"
7345 pan, { c with beyecolumns = beyecolumns; columns = columns }
7346 | _ -> x, conf
7348 let basename = Filename.basename
7349 (if String.length state.origin = 0 then state.path else state.origin)
7351 adddoc basename pan (getanchor ())
7352 (let conf =
7353 let autoscrollstep =
7354 match state.autoscroll with
7355 | Some step -> step
7356 | None -> conf.autoscrollstep
7358 match state.mode with
7359 | Birdseye (bc, _, _, _, _) ->
7360 { conf with
7361 zoom = bc.zoom;
7362 presentation = bc.presentation;
7363 interpagespace = bc.interpagespace;
7364 maxwait = bc.maxwait;
7365 autoscrollstep = autoscrollstep }
7366 | _ -> { conf with autoscrollstep = autoscrollstep }
7367 in conf)
7368 (if conf.savebmarks then state.bookmarks else []);
7370 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7371 if basename <> path
7372 then adddoc path x anchor c bookmarks
7373 ) h;
7374 Buffer.add_string bb "</llppconfig>\n";
7375 true;
7377 if load1 f && Buffer.length bb > 0
7378 then
7380 let tmp = !confpath ^ ".tmp" in
7381 let oc = open_out_bin tmp in
7382 Buffer.output_buffer oc bb;
7383 close_out oc;
7384 Unix.rename tmp !confpath;
7385 with exn ->
7386 prerr_endline
7387 ("error while saving configuration: " ^ exntos exn)
7389 end;;
7391 let adderrmsg src msg =
7392 Buffer.add_string state.errmsgs msg;
7393 state.newerrmsgs <- true;
7394 G.postRedisplay src
7397 let adderrfmt src fmt =
7398 Format.kprintf (fun s -> adderrmsg src s) fmt;
7401 let ract cmds =
7402 let cl = splitatspace cmds in
7403 let scan s fmt f =
7404 try Scanf.sscanf s fmt f
7405 with exn ->
7406 adderrfmt "remote exec"
7407 "error processing '%S': %s\n" cmds (exntos exn)
7409 match cl with
7410 | "reload" :: [] -> reload ()
7411 | "goto" :: args :: [] ->
7412 scan args "%u %f %f"
7413 (fun pageno x y ->
7414 let cmd, _ = state.geomcmds in
7415 if String.length cmd = 0
7416 then gotopagexy pageno x y
7417 else
7418 let f prevf () =
7419 gotopagexy pageno x y;
7420 prevf ()
7422 state.reprf <- f state.reprf
7424 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7425 | "rect" :: args :: [] ->
7426 scan args "%u %u %f %f %f %f"
7427 (fun pageno color x0 y0 x1 y1 ->
7428 onpagerect pageno (fun w h ->
7429 let _,w1,h1,_ = getpagedim pageno in
7430 let sw = float w1 /. w
7431 and sh = float h1 /. h in
7432 let x0s = x0 *. sw
7433 and x1s = x1 *. sw
7434 and y0s = y0 *. sh
7435 and y1s = y1 *. sh in
7436 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7437 debugrect rect;
7438 state.rects <- (pageno, color, rect) :: state.rects;
7439 G.postRedisplay "rect";
7442 | "activatewin" :: [] -> Wsi.activatewin ()
7443 | "quit" :: [] -> raise Quit
7444 | _ ->
7445 adderrfmt "remote command"
7446 "error processing remote command: %S\n" cmds;
7449 let remote =
7450 let scratch = String.create 80 in
7451 let buf = Buffer.create 80 in
7452 fun fd ->
7453 let rec tempfr () =
7454 try Some (Unix.read fd scratch 0 80)
7455 with
7456 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7457 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7458 | exn -> raise exn
7460 match tempfr () with
7461 | None -> Some fd
7462 | Some n ->
7463 if n = 0
7464 then (
7465 Unix.close fd;
7466 if Buffer.length buf > 0
7467 then (
7468 let s = Buffer.contents buf in
7469 Buffer.clear buf;
7470 ract s;
7472 None
7474 else
7475 let rec eat ppos =
7476 let nlpos =
7478 let pos = String.index_from scratch ppos '\n' in
7479 if pos >= n then -1 else pos
7480 with Not_found -> -1
7482 if nlpos >= 0
7483 then (
7484 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7485 let s = Buffer.contents buf in
7486 Buffer.clear buf;
7487 ract s;
7488 eat (nlpos+1);
7490 else (
7491 Buffer.add_substring buf scratch ppos (n-ppos);
7492 Some fd
7494 in eat 0
7497 let remoteopen path =
7498 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7499 with exn ->
7500 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7501 None
7504 let () =
7505 let trimcachepath = ref "" in
7506 let rcmdpath = ref "" in
7507 selfexec := Sys.executable_name;
7508 Arg.parse
7509 (Arg.align
7510 [("-p", Arg.String (fun s -> state.password <- s),
7511 "<password> Set password");
7513 ("-f", Arg.String
7514 (fun s ->
7515 Config.fontpath := s;
7516 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7518 "<path> Set path to the user interface font");
7520 ("-c", Arg.String
7521 (fun s ->
7522 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7523 Config.confpath := s),
7524 "<path> Set path to the configuration file");
7526 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7527 "<path> Set path to the trim cache file");
7529 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7530 "<named-destination> Set named destination");
7532 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7533 ("-cxack", Arg.Set cxack, " Cut corners");
7535 ("-remote", Arg.String (fun s -> rcmdpath := s),
7536 "<path> Set path to the remote commands source");
7538 ("-origin", Arg.String (fun s -> state.origin <- s),
7539 "<original-path> Set original path");
7541 ("-v", Arg.Unit (fun () ->
7542 Printf.printf
7543 "%s\nconfiguration path: %s\n"
7544 (version ())
7545 Config.defconfpath
7547 exit 0), " Print version and exit");
7550 (fun s -> state.path <- s)
7551 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7553 if !wtmode
7554 then selfexec := !selfexec ^ " -wtmode";
7556 if String.length state.path = 0
7557 then (prerr_endline "file name missing"; exit 1);
7559 if not (Config.load ())
7560 then prerr_endline "failed to load configuration";
7562 let wsfd, winw, winh = Wsi.init (object
7563 val mutable m_hack = false
7564 method expose = if not m_hack then G.postRedisplay "expose"
7565 method visible = G.postRedisplay "visible"
7566 method display = m_hack <- false; display ()
7567 method reshape w h =
7568 m_hack <- w < state.winw && h < state.winh;
7569 reshape w h
7570 method mouse b d x y m = mouse b d x y m
7571 method motion x y = state.mpos <- (x, y); motion x y
7572 method pmotion x y = state.mpos <- (x, y); pmotion x y
7573 method key k m =
7574 let mascm = m land (
7575 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7576 ) in
7577 match state.keystate with
7578 | KSnone ->
7579 let km = k, mascm in
7580 begin
7581 match
7582 let modehash = state.uioh#modehash in
7583 try Hashtbl.find modehash km
7584 with Not_found ->
7585 try Hashtbl.find (findkeyhash conf "global") km
7586 with Not_found -> KMinsrt (k, m)
7587 with
7588 | KMinsrt (k, m) -> keyboard k m
7589 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7590 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7592 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7593 List.iter (fun (k, m) -> keyboard k m) insrt;
7594 state.keystate <- KSnone
7595 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7596 state.keystate <- KSinto (keys, insrt)
7597 | _ ->
7598 state.keystate <- KSnone
7600 method enter x y = state.mpos <- (x, y); pmotion x y
7601 method leave = state.mpos <- (-1, -1)
7602 method winstate wsl = state.winstate <- wsl
7603 method quit = raise Quit
7604 end) conf.cwinw conf.cwinh (platform = Posx) in
7606 state.wsfd <- wsfd;
7608 if not (
7609 List.exists GlMisc.check_extension
7610 [ "GL_ARB_texture_rectangle"
7611 ; "GL_EXT_texture_recangle"
7612 ; "GL_NV_texture_rectangle" ]
7614 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7616 if (
7617 let r = GlMisc.get_string `renderer in
7618 let p = "Mesa DRI Intel(" in
7619 let l = String.length p in
7620 String.length r > l && String.sub r 0 l = p
7622 then (
7623 defconf.sliceheight <- 1024;
7624 defconf.texcount <- 32;
7625 defconf.usepbo <- true;
7628 let cr, sw =
7629 match Ne.pipe () with
7630 | Ne.Exn exn ->
7631 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7632 exit 1
7633 | Ne.Res rw -> rw
7634 and sr, cw =
7635 match Ne.pipe () with
7636 | Ne.Exn exn ->
7637 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7638 exit 1
7639 | Ne.Res rw -> rw
7642 cloexec cr;
7643 cloexec sw;
7644 cloexec sr;
7645 cloexec cw;
7647 setcheckers conf.checkers;
7648 redirectstderr ();
7649 if conf.redirectstderr
7650 then
7651 at_exit (fun () ->
7652 let s = Buffer.contents state.errmsgs ^
7653 (match state.errfd with
7654 | Some fd ->
7655 let s = String.create (80*24) in
7656 let n =
7658 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7659 if List.mem fd r
7660 then Unix.read fd s 0 (String.length s)
7661 else 0
7662 with _ -> 0
7664 if n = 0
7665 then ""
7666 else String.sub s 0 n
7667 | None -> ""
7670 try ignore (Unix.write state.stderr s 0 (String.length s))
7671 with exn -> print_endline (exntos exn)
7675 init (cr, cw) (
7676 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7677 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7678 !Config.fontpath, !trimcachepath,
7679 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7681 state.sr <- sr;
7682 state.sw <- sw;
7683 state.text <- "Opening " ^ (mbtoutf8 state.path);
7684 reshape winw winh;
7685 opendoc state.path state.password;
7686 state.uioh <- uioh;
7687 display ();
7688 Wsi.mapwin ();
7689 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7690 let optrfd =
7691 ref (
7692 if String.length !rcmdpath > 0
7693 then remoteopen !rcmdpath
7694 else None
7698 let rec loop deadline =
7699 let r =
7700 match state.errfd with
7701 | None -> [state.sr; state.wsfd]
7702 | Some fd -> [state.sr; state.wsfd; fd]
7704 let r =
7705 match !optrfd with
7706 | None -> r
7707 | Some fd -> fd :: r
7709 if state.redisplay
7710 then (
7711 state.redisplay <- false;
7712 display ();
7714 let timeout =
7715 let now = now () in
7716 if deadline > now
7717 then (
7718 if deadline = infinity
7719 then ~-.1.0
7720 else max 0.0 (deadline -. now)
7722 else 0.0
7724 let r, _, _ =
7725 try Unix.select r [] [] timeout
7726 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7728 begin match r with
7729 | [] ->
7730 state.ghyll None;
7731 let newdeadline =
7732 if state.ghyll == noghyll
7733 then
7734 match state.autoscroll with
7735 | Some step when step != 0 ->
7736 let y = state.y + step in
7737 let y =
7738 if y < 0
7739 then state.maxy
7740 else if y >= state.maxy then 0 else y
7742 gotoy y;
7743 if state.mode = View
7744 then state.text <- "";
7745 deadline +. 0.01
7746 | _ -> infinity
7747 else deadline +. 0.01
7749 loop newdeadline
7751 | l ->
7752 let rec checkfds = function
7753 | [] -> ()
7754 | fd :: rest when fd = state.sr ->
7755 let cmd = readcmd state.sr in
7756 act cmd;
7757 checkfds rest
7759 | fd :: rest when fd = state.wsfd ->
7760 Wsi.readresp fd;
7761 checkfds rest
7763 | fd :: rest when Some fd = !optrfd ->
7764 begin match remote fd with
7765 | None -> optrfd := remoteopen !rcmdpath;
7766 | opt -> optrfd := opt
7767 end;
7768 checkfds rest
7770 | fd :: rest ->
7771 let s = String.create 80 in
7772 let n = tempfailureretry (Unix.read fd s 0) 80 in
7773 if conf.redirectstderr
7774 then (
7775 Buffer.add_substring state.errmsgs s 0 n;
7776 state.newerrmsgs <- true;
7777 state.redisplay <- true;
7779 else (
7780 prerr_string (String.sub s 0 n);
7781 flush stderr;
7783 checkfds rest
7785 checkfds l;
7786 let newdeadline =
7787 let deadline1 =
7788 if deadline = infinity
7789 then now () +. 0.01
7790 else deadline
7792 match state.autoscroll with
7793 | Some step when step != 0 -> deadline1
7794 | _ -> if state.ghyll == noghyll then infinity else deadline1
7796 loop newdeadline
7797 end;
7800 loop infinity;
7801 with Quit ->
7802 Config.save ();