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