Cygwin's X Server sends configure instead of exposure/visibility it seems
[llpp.git] / main.ml
blob5740294eb8c58631172273831f535379dd9d60b9
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and fontpath = string
36 and memsize = int
37 and aalevel = int
38 and irect = (int * int * int * int)
39 and trimparams = (trimmargins * irect)
40 and colorspace = | Rgb | Bgr | Gray
43 type link =
44 | Lnotfound
45 | Lfound of int
46 and linkdir =
47 | LDfirst
48 | LDlast
49 | LDfirstvisible of (int * int * int)
50 | LDleft of int
51 | LDright of int
52 | LDdown of int
53 | LDup of int
56 type pagewithlinks =
57 | Pwlnotfound
58 | Pwl of int
61 type keymap =
62 | KMinsrt of key
63 | KMinsrl of key list
64 | KMmulti of key list * key list
65 and key = int * int
66 and keyhash = (key, keymap) Hashtbl.t
67 and keystate =
68 | KSnone
69 | KSinto of (key list * key list)
72 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
73 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
75 type pipe = (Unix.file_descr * Unix.file_descr);;
77 external init : pipe -> params -> unit = "ml_init";;
78 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
79 external copysel : string -> opaque -> unit = "ml_copysel";;
80 external getpdimrect : int -> float array = "ml_getpdimrect";;
81 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
82 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
83 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
84 external measurestr : int -> string -> float = "ml_measure_string";;
85 external getmaxw : unit -> float = "ml_getmaxw";;
86 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
87 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
88 external platform : unit -> platform = "ml_platform";;
89 external setaalevel : int -> unit = "ml_setaalevel";;
90 external realloctexts : int -> bool = "ml_realloctexts";;
91 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
92 external findlink : opaque -> linkdir -> link = "ml_findlink";;
93 external getlink : opaque -> int -> under = "ml_getlink";;
94 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
95 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
97 let platform_to_string = function
98 | Punknown -> "unknown"
99 | Plinux -> "Linux"
100 | Posx -> "OSX"
101 | Psun -> "Sun"
102 | Pfreebsd -> "FreeBSD"
103 | Pdragonflybsd -> "DragonflyBSD"
104 | Popenbsd -> "OpenBSD"
105 | Pnetbsd -> "NetBSD"
106 | Pcygwin -> "Cygwin"
109 let platform = platform ();;
111 type x = int
112 and y = int
113 and tilex = int
114 and tiley = int
115 and tileparams = (x * y * width * height * tilex * tiley)
118 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
120 type mpos = int * int
121 and mstate =
122 | Msel of (mpos * mpos)
123 | Mpan of mpos
124 | Mscrolly | Mscrollx
125 | Mzoom of (int * int)
126 | Mzoomrect of (mpos * mpos)
127 | Mnone
130 type textentry = string * string * onhist option * onkey * ondone
131 and onkey = string -> int -> te
132 and ondone = string -> unit
133 and histcancel = unit -> unit
134 and onhist = ((histcmd -> string) * histcancel)
135 and histcmd = HCnext | HCprev | HCfirst | HClast
136 and te =
137 | TEstop
138 | TEdone of string
139 | TEcont of string
140 | TEswitch of textentry
143 type 'a circbuf =
144 { store : 'a array
145 ; mutable rc : int
146 ; mutable wc : int
147 ; mutable len : int
151 let bound v minv maxv =
152 max minv (min maxv v);
155 let cbnew n v =
156 { store = Array.create n v
157 ; rc = 0
158 ; wc = 0
159 ; len = 0
163 let drawstring size x y s =
164 Gl.enable `blend;
165 Gl.enable `texture_2d;
166 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
167 ignore (drawstr size x y s);
168 Gl.disable `blend;
169 Gl.disable `texture_2d;
172 let drawstring1 size x y s =
173 drawstr size x y s;
176 let drawstring2 size x y fmt =
177 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
180 let cbcap b = Array.length b.store;;
182 let cbput b v =
183 let cap = cbcap b in
184 b.store.(b.wc) <- v;
185 b.wc <- (b.wc + 1) mod cap;
186 b.rc <- b.wc;
187 b.len <- min (b.len + 1) cap;
190 let cbempty b = b.len = 0;;
192 let cbgetg b circular dir =
193 if cbempty b
194 then b.store.(0)
195 else
196 let rc = b.rc + dir in
197 let rc =
198 if circular
199 then (
200 if rc = -1
201 then b.len-1
202 else (
203 if rc = b.len
204 then 0
205 else rc
208 else max 0 (min rc (b.len-1))
210 b.rc <- rc;
211 b.store.(rc);
214 let cbget b = cbgetg b false;;
215 let cbgetc b = cbgetg b true;;
217 type page =
218 { pageno : int
219 ; pagedimno : int
220 ; pagew : int
221 ; pageh : int
222 ; pagex : int
223 ; pagey : int
224 ; pagevw : int
225 ; pagevh : int
226 ; pagedispx : int
227 ; pagedispy : int
231 let debugl l =
232 dolog "l %d dim=%d {" l.pageno l.pagedimno;
233 dolog " WxH %dx%d" l.pagew l.pageh;
234 dolog " vWxH %dx%d" l.pagevw l.pagevh;
235 dolog " pagex,y %d,%d" l.pagex l.pagey;
236 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
237 dolog "}";
240 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
241 dolog "rect {";
242 dolog " x0,y0=(% f, % f)" x0 y0;
243 dolog " x1,y1=(% f, % f)" x1 y1;
244 dolog " x2,y2=(% f, % f)" x2 y2;
245 dolog " x3,y3=(% f, % f)" x3 y3;
246 dolog "}";
249 type columns =
250 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
251 and multicol = columncount * covercount * covercount
252 and pdimno = int
253 and columncount = int
254 and covercount = int;;
256 type conf =
257 { mutable scrollbw : int
258 ; mutable scrollh : int
259 ; mutable icase : bool
260 ; mutable preload : bool
261 ; mutable pagebias : int
262 ; mutable verbose : bool
263 ; mutable debug : bool
264 ; mutable scrollstep : int
265 ; mutable maxhfit : bool
266 ; mutable crophack : bool
267 ; mutable autoscrollstep : int
268 ; mutable maxwait : float option
269 ; mutable hlinks : bool
270 ; mutable underinfo : bool
271 ; mutable interpagespace : interpagespace
272 ; mutable zoom : float
273 ; mutable presentation : bool
274 ; mutable angle : angle
275 ; mutable winw : int
276 ; mutable winh : int
277 ; mutable savebmarks : bool
278 ; mutable proportional : proportional
279 ; mutable trimmargins : trimmargins
280 ; mutable trimfuzz : irect
281 ; mutable memlimit : memsize
282 ; mutable texcount : texcount
283 ; mutable sliceheight : sliceheight
284 ; mutable thumbw : width
285 ; mutable jumpback : bool
286 ; mutable bgcolor : float * float * float
287 ; mutable bedefault : bool
288 ; mutable scrollbarinpm : bool
289 ; mutable tilew : int
290 ; mutable tileh : int
291 ; mutable mustoresize : memsize
292 ; mutable checkers : bool
293 ; mutable aalevel : int
294 ; mutable urilauncher : string
295 ; mutable pathlauncher : string
296 ; mutable colorspace : colorspace
297 ; mutable invert : bool
298 ; mutable colorscale : float
299 ; mutable redirectstderr : bool
300 ; mutable ghyllscroll : (int * int * int) option
301 ; mutable columns : columns option
302 ; mutable beyecolumns : columncount option
303 ; mutable selcmd : string
304 ; mutable updatecurs : bool
305 ; mutable keyhashes : (string * keyhash) list
309 type anchor = pageno * top;;
311 type outline = string * int * anchor;;
313 type rect = float * float * float * float * float * float * float * float;;
315 type tile = opaque * pixmapsize * elapsed
316 and elapsed = float;;
317 type pagemapkey = pageno * gen;;
318 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
319 and row = int
320 and col = int;;
322 let emptyanchor = (0, 0.0);;
324 type infochange = | Memused | Docinfo | Pdim;;
326 class type uioh = object
327 method display : unit
328 method key : int -> int -> uioh
329 method button : int -> bool -> int -> int -> int -> uioh
330 method motion : int -> int -> uioh
331 method pmotion : int -> int -> uioh
332 method infochanged : infochange -> unit
333 method scrollpw : (int * float * float)
334 method scrollph : (int * float * float)
335 method modehash : keyhash
336 end;;
338 type mode =
339 | Birdseye of (conf * leftx * pageno * pageno * anchor)
340 | Textentry of (textentry * onleave)
341 | View
342 | LinkNav of linktarget
343 and onleave = leavetextentrystatus -> unit
344 and leavetextentrystatus = | Cancel | Confirm
345 and helpitem = string * int * action
346 and action =
347 | Noaction
348 | Action of (uioh -> uioh)
349 and linktarget =
350 | Ltexact of (pageno * int)
351 | Ltgendir of int
354 let isbirdseye = function Birdseye _ -> true | _ -> false;;
355 let istextentry = function Textentry _ -> true | _ -> false;;
357 type currently =
358 | Idle
359 | Loading of (page * gen)
360 | Tiling of (
361 page * opaque * colorspace * angle * gen * col * row * width * height
363 | Outlining of outline list
366 let emptykeyhash = Hashtbl.create 0;;
367 let nouioh : uioh = object (self)
368 method display = ()
369 method key _ _ = self
370 method button _ _ _ _ _ = self
371 method motion _ _ = self
372 method pmotion _ _ = self
373 method infochanged _ = ()
374 method scrollpw = (0, nan, nan)
375 method scrollph = (0, nan, nan)
376 method modehash = emptykeyhash
377 end;;
379 type state =
380 { mutable sr : Unix.file_descr
381 ; mutable sw : Unix.file_descr
382 ; mutable wsfd : Unix.file_descr
383 ; mutable errfd : Unix.file_descr option
384 ; mutable stderr : Unix.file_descr
385 ; mutable errmsgs : Buffer.t
386 ; mutable newerrmsgs : bool
387 ; mutable w : int
388 ; mutable x : int
389 ; mutable y : int
390 ; mutable scrollw : int
391 ; mutable hscrollh : int
392 ; mutable anchor : anchor
393 ; mutable ranchors : (string * string * anchor) list
394 ; mutable maxy : int
395 ; mutable layout : page list
396 ; pagemap : (pagemapkey, opaque) Hashtbl.t
397 ; tilemap : (tilemapkey, tile) Hashtbl.t
398 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
399 ; mutable pdims : (pageno * width * height * leftx) list
400 ; mutable pagecount : int
401 ; mutable currently : currently
402 ; mutable mstate : mstate
403 ; mutable searchpattern : string
404 ; mutable rects : (pageno * recttype * rect) list
405 ; mutable rects1 : (pageno * recttype * rect) list
406 ; mutable text : string
407 ; mutable fullscreen : (width * height) option
408 ; mutable mode : mode
409 ; mutable uioh : uioh
410 ; mutable outlines : outline array
411 ; mutable bookmarks : outline list
412 ; mutable path : string
413 ; mutable password : string
414 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
415 ; mutable memused : memsize
416 ; mutable gen : gen
417 ; mutable throttle : (page list * int * float) option
418 ; mutable autoscroll : int option
419 ; mutable ghyll : (int option -> unit)
420 ; mutable help : helpitem array
421 ; mutable docinfo : (int * string) list
422 ; mutable texid : GlTex.texture_id option
423 ; hists : hists
424 ; mutable prevzoom : float
425 ; mutable progress : float
426 ; mutable redisplay : bool
427 ; mutable mpos : mpos
428 ; mutable keystate : keystate
430 and hists =
431 { pat : string circbuf
432 ; pag : string circbuf
433 ; nav : anchor circbuf
434 ; sel : string circbuf
438 let defconf =
439 { scrollbw = 7
440 ; scrollh = 12
441 ; icase = true
442 ; preload = true
443 ; pagebias = 0
444 ; verbose = false
445 ; debug = false
446 ; scrollstep = 24
447 ; maxhfit = true
448 ; crophack = false
449 ; autoscrollstep = 2
450 ; maxwait = None
451 ; hlinks = false
452 ; underinfo = false
453 ; interpagespace = 2
454 ; zoom = 1.0
455 ; presentation = false
456 ; angle = 0
457 ; winw = 900
458 ; winh = 900
459 ; savebmarks = true
460 ; proportional = true
461 ; trimmargins = false
462 ; trimfuzz = (0,0,0,0)
463 ; memlimit = 32 lsl 20
464 ; texcount = 256
465 ; sliceheight = 24
466 ; thumbw = 76
467 ; jumpback = true
468 ; bgcolor = (0.5, 0.5, 0.5)
469 ; bedefault = false
470 ; scrollbarinpm = true
471 ; tilew = 2048
472 ; tileh = 2048
473 ; mustoresize = 128 lsl 20
474 ; checkers = true
475 ; aalevel = 8
476 ; urilauncher =
477 (match platform with
478 | Plinux | Pfreebsd | Pdragonflybsd
479 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
480 | Posx -> "open \"%s\""
481 | Pcygwin -> "cygstart %s"
482 | Punknown -> "echo %s")
483 ; pathlauncher = "lp \"%s\""
484 ; selcmd =
485 (match platform with
486 | Plinux | Pfreebsd | Pdragonflybsd
487 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
488 | Posx -> "pbcopy"
489 | Pcygwin -> "wsel"
490 | Punknown -> "cat")
491 ; colorspace = Rgb
492 ; invert = false
493 ; colorscale = 1.0
494 ; redirectstderr = false
495 ; ghyllscroll = None
496 ; columns = None
497 ; beyecolumns = None
498 ; updatecurs = false
499 ; keyhashes =
500 let mk n = (n, Hashtbl.create 1) in
501 [ mk "global"
502 ; mk "info"
503 ; mk "help"
504 ; mk "outline"
505 ; mk "listview"
506 ; mk "birdseye"
507 ; mk "textentry"
508 ; mk "links"
513 let findkeyhash c name =
514 try List.assoc name c.keyhashes
515 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
518 let conf = { defconf with angle = defconf.angle };;
520 type fontstate =
521 { mutable fontsize : int
522 ; mutable wwidth : float
523 ; mutable maxrows : int
527 let fstate =
528 { fontsize = 14
529 ; wwidth = nan
530 ; maxrows = -1
534 let setfontsize n =
535 fstate.fontsize <- n;
536 fstate.wwidth <- measurestr fstate.fontsize "w";
537 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
540 let geturl s =
541 let colonpos = try String.index s ':' with Not_found -> -1 in
542 let len = String.length s in
543 if colonpos >= 0 && colonpos + 3 < len
544 then (
545 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
546 then
547 let schemestartpos =
548 try String.rindex_from s colonpos ' '
549 with Not_found -> -1
551 let scheme =
552 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
554 match scheme with
555 | "http" | "ftp" | "mailto" ->
556 let epos =
557 try String.index_from s colonpos ' '
558 with Not_found -> len
560 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
561 | _ -> ""
562 else ""
564 else ""
567 let popen =
568 let shell, farg = "/bin/sh", "-c" in
569 fun s ->
570 let args = [|shell; farg; s|] in
571 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
574 let gotouri uri =
575 if String.length conf.urilauncher = 0
576 then print_endline uri
577 else (
578 let url = geturl uri in
579 if String.length url = 0
580 then print_endline uri
581 else
582 let re = Str.regexp "%s" in
583 let command = Str.global_replace re url conf.urilauncher in
584 try popen command
585 with exn ->
586 Printf.eprintf
587 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
588 flush stderr;
592 let version () =
593 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
594 (platform_to_string platform) Sys.word_size Sys.ocaml_version
597 let makehelp () =
598 let strings = version () :: "" :: Help.keys in
599 Array.of_list (
600 List.map (fun s ->
601 let url = geturl s in
602 if String.length url > 0
603 then (s, 0, Action (fun u -> gotouri url; u))
604 else (s, 0, Noaction)
605 ) strings);
608 let noghyll _ = ();;
609 let firstgeomcmds = "", [];;
611 let state =
612 { sr = Unix.stdin
613 ; sw = Unix.stdin
614 ; wsfd = Unix.stdin
615 ; errfd = None
616 ; stderr = Unix.stderr
617 ; errmsgs = Buffer.create 0
618 ; newerrmsgs = false
619 ; x = 0
620 ; y = 0
621 ; w = 0
622 ; scrollw = 0
623 ; hscrollh = 0
624 ; anchor = emptyanchor
625 ; ranchors = []
626 ; layout = []
627 ; maxy = max_int
628 ; tilelru = Queue.create ()
629 ; pagemap = Hashtbl.create 10
630 ; tilemap = Hashtbl.create 10
631 ; pdims = []
632 ; pagecount = 0
633 ; currently = Idle
634 ; mstate = Mnone
635 ; rects = []
636 ; rects1 = []
637 ; text = ""
638 ; mode = View
639 ; fullscreen = None
640 ; searchpattern = ""
641 ; outlines = [||]
642 ; bookmarks = []
643 ; path = ""
644 ; password = ""
645 ; geomcmds = firstgeomcmds
646 ; hists =
647 { nav = cbnew 10 (0, 0.0)
648 ; pat = cbnew 10 ""
649 ; pag = cbnew 10 ""
650 ; sel = cbnew 10 ""
652 ; memused = 0
653 ; gen = 0
654 ; throttle = None
655 ; autoscroll = None
656 ; ghyll = noghyll
657 ; help = makehelp ()
658 ; docinfo = []
659 ; texid = None
660 ; prevzoom = 1.0
661 ; progress = -1.0
662 ; uioh = nouioh
663 ; redisplay = true
664 ; mpos = (-1, -1)
665 ; keystate = KSnone
669 let vlog fmt =
670 if conf.verbose
671 then
672 Printf.kprintf prerr_endline fmt
673 else
674 Printf.kprintf ignore fmt
677 let launchpath () =
678 if String.length conf.pathlauncher = 0
679 then print_endline state.path
680 else (
681 let re = Str.regexp "%s" in
682 let command = Str.global_replace re state.path conf.pathlauncher in
683 try popen command
684 with exn ->
685 Printf.eprintf
686 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
687 flush stderr;
691 let redirectstderr () =
692 if conf.redirectstderr
693 then
694 let rfd, wfd = Unix.pipe () in
695 state.stderr <- Unix.dup Unix.stderr;
696 state.errfd <- Some rfd;
697 Unix.dup2 wfd Unix.stderr;
698 else (
699 state.newerrmsgs <- false;
700 begin match state.errfd with
701 | Some fd ->
702 Unix.close fd;
703 Unix.dup2 state.stderr Unix.stderr;
704 state.errfd <- None;
705 | None -> ()
706 end;
707 prerr_string (Buffer.contents state.errmsgs);
708 flush stderr;
709 Buffer.clear state.errmsgs;
713 module G =
714 struct
715 let postRedisplay who =
716 if conf.verbose
717 then prerr_endline ("redisplay for " ^ who);
718 state.redisplay <- true;
720 end;;
722 let getopaque pageno =
723 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
724 with Not_found -> None
727 let putopaque pageno opaque =
728 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
731 let pagetranslatepoint l x y =
732 let dy = y - l.pagedispy in
733 let y = dy + l.pagey in
734 let dx = x - l.pagedispx in
735 let x = dx + l.pagex in
736 (x, y);
739 let getunder x y =
740 let rec f = function
741 | l :: rest ->
742 begin match getopaque l.pageno with
743 | Some opaque ->
744 let x0 = l.pagedispx in
745 let x1 = x0 + l.pagevw in
746 let y0 = l.pagedispy in
747 let y1 = y0 + l.pagevh in
748 if y >= y0 && y <= y1 && x >= x0 && x <= x1
749 then
750 let px, py = pagetranslatepoint l x y in
751 match whatsunder opaque px py with
752 | Unone -> f rest
753 | under -> under
754 else f rest
755 | _ ->
756 f rest
758 | [] -> Unone
760 f state.layout
763 let showtext c s =
764 state.text <- Printf.sprintf "%c%s" c s;
765 G.postRedisplay "showtext";
768 let updateunder x y =
769 match getunder x y with
770 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
771 | Ulinkuri uri ->
772 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
773 Wsi.setcursor Wsi.CURSOR_INFO
774 | Ulinkgoto (page, _) ->
775 if conf.underinfo
776 then showtext 'p' ("age: " ^ string_of_int (page+1));
777 Wsi.setcursor Wsi.CURSOR_INFO
778 | Utext s ->
779 if conf.underinfo then showtext 'f' ("ont: " ^ s);
780 Wsi.setcursor Wsi.CURSOR_TEXT
781 | Uunexpected s ->
782 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
783 Wsi.setcursor Wsi.CURSOR_INHERIT
784 | Ulaunch s ->
785 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
786 Wsi.setcursor Wsi.CURSOR_INHERIT
787 | Unamed s ->
788 if conf.underinfo then showtext 'n' ("amed: " ^ s);
789 Wsi.setcursor Wsi.CURSOR_INHERIT
790 | Uremote (filename, pageno) ->
791 if conf.underinfo then showtext 'r'
792 (Printf.sprintf "emote: %s (%d)" filename pageno);
793 Wsi.setcursor Wsi.CURSOR_INFO
796 let showlinktype under =
797 if conf.underinfo
798 then
799 match under with
800 | Unone -> ()
801 | Ulinkuri uri ->
802 showtext 'u' ("ri: " ^ uri)
803 | Ulinkgoto (page, _) ->
804 showtext 'p' ("age: " ^ string_of_int (page+1));
805 | Utext s ->
806 showtext 'f' ("ont: " ^ s);
807 | Uunexpected s ->
808 showtext 'u' ("nexpected: " ^ s);
809 | Ulaunch s ->
810 showtext 'l' ("aunch: " ^ s);
811 | Unamed s ->
812 showtext 'n' ("amed: " ^ s);
813 | Uremote (filename, pageno) ->
814 showtext 'r' (Printf.sprintf "emote: %s (%d)" filename pageno);
817 let addchar s c =
818 let b = Buffer.create (String.length s + 1) in
819 Buffer.add_string b s;
820 Buffer.add_char b c;
821 Buffer.contents b;
824 let colorspace_of_string s =
825 match String.lowercase s with
826 | "rgb" -> Rgb
827 | "bgr" -> Bgr
828 | "gray" -> Gray
829 | _ -> failwith "invalid colorspace"
832 let int_of_colorspace = function
833 | Rgb -> 0
834 | Bgr -> 1
835 | Gray -> 2
838 let colorspace_of_int = function
839 | 0 -> Rgb
840 | 1 -> Bgr
841 | 2 -> Gray
842 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
845 let colorspace_to_string = function
846 | Rgb -> "rgb"
847 | Bgr -> "bgr"
848 | Gray -> "gray"
851 let intentry_with_suffix text key =
852 let c =
853 if key >= 32 && key < 127
854 then Char.chr key
855 else '\000'
857 match Char.lowercase c with
858 | '0' .. '9' ->
859 let text = addchar text c in
860 TEcont text
862 | 'k' | 'm' | 'g' ->
863 let text = addchar text c in
864 TEcont text
866 | _ ->
867 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
868 TEcont text
871 let columns_to_string (n, a, b) =
872 if a = 0 && b = 0
873 then Printf.sprintf "%d" n
874 else Printf.sprintf "%d,%d,%d" n a b;
877 let columns_of_string s =
879 (int_of_string s, 0, 0)
880 with _ ->
881 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
884 let readcmd fd =
885 let s = "xxxx" in
886 let n = Unix.read fd s 0 4 in
887 if n != 4 then failwith "incomplete read(len)";
888 let len = 0
889 lor (Char.code s.[0] lsl 24)
890 lor (Char.code s.[1] lsl 16)
891 lor (Char.code s.[2] lsl 8)
892 lor (Char.code s.[3] lsl 0)
894 let s = String.create len in
895 let n = Unix.read fd s 0 len in
896 if n != len then failwith "incomplete read(data)";
900 let btod b = if b then 1 else 0;;
902 let wcmd fmt =
903 let b = Buffer.create 16 in
904 Buffer.add_string b "llll";
905 Printf.kbprintf
906 (fun b ->
907 let s = Buffer.contents b in
908 let n = String.length s in
909 let len = n - 4 in
910 (* dolog "wcmd %S" (String.sub s 4 len); *)
911 s.[0] <- Char.chr ((len lsr 24) land 0xff);
912 s.[1] <- Char.chr ((len lsr 16) land 0xff);
913 s.[2] <- Char.chr ((len lsr 8) land 0xff);
914 s.[3] <- Char.chr (len land 0xff);
915 let n' = Unix.write state.sw s 0 n in
916 if n' != n then failwith "write failed";
917 ) b fmt;
920 let calcips h =
921 if conf.presentation
922 then
923 let d = conf.winh - h in
924 max 0 ((d + 1) / 2)
925 else
926 conf.interpagespace
929 let calcheight () =
930 let rec f pn ph pi fh l =
931 match l with
932 | (n, _, h, _) :: rest ->
933 let ips = calcips h in
934 let fh =
935 if conf.presentation
936 then fh+ips
937 else (
938 if isbirdseye state.mode && pn = 0
939 then fh + ips
940 else fh
943 let fh = fh + ((n - pn) * (ph + pi)) in
944 f n h ips fh rest;
946 | [] ->
947 let inc =
948 if conf.presentation || (isbirdseye state.mode && pn = 0)
949 then 0
950 else -pi
952 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
953 max 0 fh
955 let fh = f 0 0 0 0 state.pdims in
959 let calcheight () =
960 match conf.columns with
961 | None -> calcheight ()
962 | Some (_, b) ->
963 if Array.length b > 0
964 then
965 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
966 y + h
967 else 0
970 let getpageyh pageno =
971 let rec f pn ph pi y l =
972 match l with
973 | (n, _, h, _) :: rest ->
974 let ips = calcips h in
975 if n >= pageno
976 then
977 let h = if n = pageno then h else ph in
978 if conf.presentation && n = pageno
979 then
980 y + (pageno - pn) * (ph + pi) + pi, h
981 else
982 y + (pageno - pn) * (ph + pi), h
983 else
984 let y = y + (if conf.presentation then pi else 0) in
985 let y = y + (n - pn) * (ph + pi) in
986 f n h ips y rest
988 | [] ->
989 y + (pageno - pn) * (ph + pi), ph
991 f 0 0 0 0 state.pdims
994 let getpageyh pageno =
995 match conf.columns with
996 | None -> getpageyh pageno
997 | Some (_, b) ->
998 let (_, _, y, (_, _, h, _)) = b.(pageno) in
999 y, h
1002 let getpagedim pageno =
1003 let rec f ppdim l =
1004 match l with
1005 | (n, _, _, _) as pdim :: rest ->
1006 if n >= pageno
1007 then (if n = pageno then pdim else ppdim)
1008 else f pdim rest
1010 | [] -> ppdim
1012 f (-1, -1, -1, -1) state.pdims
1015 let getpagey pageno = fst (getpageyh pageno);;
1017 let nogeomcmds cmds =
1018 match cmds with
1019 | s, [] -> String.length s = 0
1020 | _ -> false
1023 let layout1 y sh =
1024 let sh = sh - state.hscrollh in
1025 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1026 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1027 match pdims with
1028 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1029 let ips = calcips h in
1030 let yinc =
1031 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1032 then ips
1033 else 0
1035 (w, h, ips, xoff), rest, pdimno + 1, yinc
1036 | _ ->
1037 prev, pdims, pdimno, 0
1039 let dy = dy + yinc in
1040 let py = py + yinc in
1041 if pageno = state.pagecount || dy >= sh
1042 then
1043 accu
1044 else
1045 let vy = y + dy in
1046 if py + h <= vy - yinc
1047 then
1048 let py = py + h + ips in
1049 let dy = max 0 (py - y) in
1050 f ~pageno:(pageno+1)
1051 ~pdimno
1052 ~prev:curr
1055 ~pdims:rest
1056 ~accu
1057 else
1058 let pagey = vy - py in
1059 let pagevh = h - pagey in
1060 let pagevh = min (sh - dy) pagevh in
1061 let off = if yinc > 0 then py - vy else 0 in
1062 let py = py + h + ips in
1063 let pagex, dx =
1064 let xoff = xoff +
1065 if state.w < conf.winw - state.scrollw
1066 then (conf.winw - state.scrollw - state.w) / 2
1067 else 0
1069 let dispx = xoff + state.x in
1070 if dispx < 0
1071 then (-dispx, 0)
1072 else (0, dispx)
1074 let pagevw =
1075 let lw = w - pagex in
1076 min lw (conf.winw - state.scrollw)
1078 let e =
1079 { pageno = pageno
1080 ; pagedimno = pdimno
1081 ; pagew = w
1082 ; pageh = h
1083 ; pagex = pagex
1084 ; pagey = pagey + off
1085 ; pagevw = pagevw
1086 ; pagevh = pagevh - off
1087 ; pagedispx = dx
1088 ; pagedispy = dy + off
1091 let accu = e :: accu in
1092 f ~pageno:(pageno+1)
1093 ~pdimno
1094 ~prev:curr
1096 ~dy:(dy+pagevh+ips)
1097 ~pdims:rest
1098 ~accu
1100 if nogeomcmds state.geomcmds
1101 then (
1102 let accu =
1104 ~pageno:0
1105 ~pdimno:~-1
1106 ~prev:(0,0,0,0)
1107 ~py:0
1108 ~dy:0
1109 ~pdims:state.pdims
1110 ~accu:[]
1112 List.rev accu
1114 else
1118 let layoutN ((columns, coverA, coverB), b) y sh =
1119 let sh = sh - state.hscrollh in
1120 let rec fold accu n =
1121 if n = Array.length b
1122 then accu
1123 else
1124 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1125 if (vy - y) > sh &&
1126 (n = coverA - 1
1127 || n = state.pagecount - coverB
1128 || (n - coverA) mod columns = columns - 1)
1129 then accu
1130 else
1131 let accu =
1132 if vy + h > y
1133 then
1134 let pagey = max 0 (y - vy) in
1135 let pagedispy = if pagey > 0 then 0 else vy - y in
1136 let pagedispx, pagex, pagevw =
1137 let pdx =
1138 if n = coverA - 1 || n = state.pagecount - coverB
1139 then state.x + (conf.winw - state.scrollw - w) / 2
1140 else dx + xoff + state.x
1142 if pdx < 0
1143 then 0, -pdx, w + pdx
1144 else pdx, 0, min (conf.winw - state.scrollw) w
1146 let pagevh = min (h - pagey) (sh - pagedispy) in
1147 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1148 then
1149 let e =
1150 { pageno = n
1151 ; pagedimno = pdimno
1152 ; pagew = w
1153 ; pageh = h
1154 ; pagex = pagex
1155 ; pagey = pagey
1156 ; pagevw = pagevw
1157 ; pagevh = pagevh
1158 ; pagedispx = pagedispx
1159 ; pagedispy = pagedispy
1162 e :: accu
1163 else
1164 accu
1165 else
1166 accu
1168 fold accu (n+1)
1170 if nogeomcmds state.geomcmds
1171 then List.rev (fold [] 0)
1172 else []
1175 let layout y sh =
1176 match conf.columns with
1177 | None -> layout1 y sh
1178 | Some c -> layoutN c y sh
1181 let clamp incr =
1182 let y = state.y + incr in
1183 let y = max 0 y in
1184 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1188 let itertiles l f =
1189 let tilex = l.pagex mod conf.tilew in
1190 let tiley = l.pagey mod conf.tileh in
1192 let col = l.pagex / conf.tilew in
1193 let row = l.pagey / conf.tileh in
1195 let vw =
1196 let a = l.pagew - l.pagex in
1197 let b = conf.winw - state.scrollw in
1198 min a b
1199 and vh = l.pagevh in
1201 let rec rowloop row y0 dispy h =
1202 if h = 0
1203 then ()
1204 else (
1205 let dh = conf.tileh - y0 in
1206 let dh = min h dh in
1207 let rec colloop col x0 dispx w =
1208 if w = 0
1209 then ()
1210 else (
1211 let dw = conf.tilew - x0 in
1212 let dw = min w dw in
1214 f col row dispx dispy x0 y0 dw dh;
1215 colloop (col+1) 0 (dispx+dw) (w-dw)
1218 colloop col tilex l.pagedispx vw;
1219 rowloop (row+1) 0 (dispy+dh) (h-dh)
1222 if vw > 0 && vh > 0
1223 then rowloop row tiley l.pagedispy vh;
1226 let gettileopaque l col row =
1227 let key =
1228 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1230 try Some (Hashtbl.find state.tilemap key)
1231 with Not_found -> None
1234 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1235 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1236 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1239 let drawtiles l color =
1240 GlDraw.color color;
1241 let f col row x y tilex tiley w h =
1242 match gettileopaque l col row with
1243 | Some (opaque, _, t) ->
1244 let params = x, y, w, h, tilex, tiley in
1245 if conf.invert
1246 then (
1247 Gl.enable `blend;
1248 GlFunc.blend_func `zero `one_minus_src_color;
1250 drawtile params opaque;
1251 if conf.invert
1252 then Gl.disable `blend;
1253 if conf.debug
1254 then (
1255 let s = Printf.sprintf
1256 "%d[%d,%d] %f sec"
1257 l.pageno col row t
1259 let w = measurestr fstate.fontsize s in
1260 GlMisc.push_attrib [`current];
1261 GlDraw.color (0.0, 0.0, 0.0);
1262 GlDraw.rect
1263 (float (x-2), float (y-2))
1264 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1265 GlDraw.color (1.0, 1.0, 1.0);
1266 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1267 GlMisc.pop_attrib ();
1270 | _ ->
1271 let w =
1272 let lw = conf.winw - state.scrollw - x in
1273 min lw w
1274 and h =
1275 let lh = conf.winh - y in
1276 min lh h
1278 Gl.enable `texture_2d;
1279 begin match state.texid with
1280 | Some id ->
1281 GlTex.bind_texture `texture_2d id;
1282 let x0 = float x
1283 and y0 = float y
1284 and x1 = float (x+w)
1285 and y1 = float (y+h) in
1287 let tw = float w /. 64.0
1288 and th = float h /. 64.0 in
1289 let tx0 = float tilex /. 64.0
1290 and ty0 = float tiley /. 64.0 in
1291 let tx1 = tx0 +. tw
1292 and ty1 = ty0 +. th in
1293 GlDraw.begins `quads;
1294 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1295 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1296 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1297 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1298 GlDraw.ends ();
1300 Gl.disable `texture_2d;
1301 | None ->
1302 GlDraw.color (1.0, 1.0, 1.0);
1303 GlDraw.rect
1304 (float x, float y)
1305 (float (x+w), float (y+h));
1306 end;
1307 if w > 128 && h > fstate.fontsize + 10
1308 then (
1309 GlDraw.color (0.0, 0.0, 0.0);
1310 let c, r =
1311 if conf.verbose
1312 then (col*conf.tilew, row*conf.tileh)
1313 else col, row
1315 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1317 GlDraw.color color;
1319 itertiles l f
1322 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1324 let tilevisible1 l x y =
1325 let ax0 = l.pagex
1326 and ax1 = l.pagex + l.pagevw
1327 and ay0 = l.pagey
1328 and ay1 = l.pagey + l.pagevh in
1330 let bx0 = x
1331 and by0 = y in
1332 let bx1 = min (bx0 + conf.tilew) l.pagew
1333 and by1 = min (by0 + conf.tileh) l.pageh in
1335 let rx0 = max ax0 bx0
1336 and ry0 = max ay0 by0
1337 and rx1 = min ax1 bx1
1338 and ry1 = min ay1 by1 in
1340 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1341 nonemptyintersection
1344 let tilevisible layout n x y =
1345 let rec findpageinlayout = function
1346 | l :: _ when l.pageno = n -> tilevisible1 l x y
1347 | _ :: rest -> findpageinlayout rest
1348 | [] -> false
1350 findpageinlayout layout
1353 let tileready l x y =
1354 tilevisible1 l x y &&
1355 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1358 let tilepage n p layout =
1359 let rec loop = function
1360 | l :: rest ->
1361 if l.pageno = n
1362 then
1363 let f col row _ _ _ _ _ _ =
1364 if state.currently = Idle
1365 then
1366 match gettileopaque l col row with
1367 | Some _ -> ()
1368 | None ->
1369 let x = col*conf.tilew
1370 and y = row*conf.tileh in
1371 let w =
1372 let w = l.pagew - x in
1373 min w conf.tilew
1375 let h =
1376 let h = l.pageh - y in
1377 min h conf.tileh
1379 wcmd "tile %s %d %d %d %d" p x y w h;
1380 state.currently <-
1381 Tiling (
1382 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1383 conf.tilew, conf.tileh
1386 itertiles l f;
1387 else
1388 loop rest
1390 | [] -> ()
1392 if nogeomcmds state.geomcmds
1393 then loop layout;
1396 let preloadlayout visiblepages =
1397 let presentation = conf.presentation in
1398 let interpagespace = conf.interpagespace in
1399 let maxy = state.maxy in
1400 conf.presentation <- false;
1401 conf.interpagespace <- 0;
1402 state.maxy <- calcheight ();
1403 let y =
1404 match visiblepages with
1405 | [] -> 0
1406 | l :: _ -> getpagey l.pageno + l.pagey
1408 let y = if y < conf.winh then 0 else y - conf.winh in
1409 let h = state.y - y + conf.winh*3 in
1410 let pages = layout y h in
1411 conf.presentation <- presentation;
1412 conf.interpagespace <- interpagespace;
1413 state.maxy <- maxy;
1414 pages;
1417 let load pages =
1418 let rec loop pages =
1419 if state.currently != Idle
1420 then ()
1421 else
1422 match pages with
1423 | l :: rest ->
1424 begin match getopaque l.pageno with
1425 | None ->
1426 wcmd "page %d %d" l.pageno l.pagedimno;
1427 state.currently <- Loading (l, state.gen);
1428 | Some opaque ->
1429 tilepage l.pageno opaque pages;
1430 loop rest
1431 end;
1432 | _ -> ()
1434 if nogeomcmds state.geomcmds
1435 then loop pages
1438 let preload pages =
1439 load pages;
1440 if conf.preload && state.currently = Idle
1441 then load (preloadlayout pages);
1444 let layoutready layout =
1445 let rec fold all ls =
1446 all && match ls with
1447 | l :: rest ->
1448 let seen = ref false in
1449 let allvisible = ref true in
1450 let foo col row _ _ _ _ _ _ =
1451 seen := true;
1452 allvisible := !allvisible &&
1453 begin match gettileopaque l col row with
1454 | Some _ -> true
1455 | None -> false
1458 itertiles l foo;
1459 fold (!seen && !allvisible) rest
1460 | [] -> true
1462 let alltilesvisible = fold true layout in
1463 alltilesvisible;
1466 let gotoy y =
1467 let y = bound y 0 state.maxy in
1468 let y, layout, proceed =
1469 match conf.maxwait with
1470 | Some time when state.ghyll == noghyll ->
1471 begin match state.throttle with
1472 | None ->
1473 let layout = layout y conf.winh in
1474 let ready = layoutready layout in
1475 if not ready
1476 then (
1477 load layout;
1478 state.throttle <- Some (layout, y, now ());
1480 else G.postRedisplay "gotoy showall (None)";
1481 y, layout, ready
1482 | Some (_, _, started) ->
1483 let dt = now () -. started in
1484 if dt > time
1485 then (
1486 state.throttle <- None;
1487 let layout = layout y conf.winh in
1488 load layout;
1489 G.postRedisplay "maxwait";
1490 y, layout, true
1492 else -1, [], false
1495 | _ ->
1496 let layout = layout y conf.winh in
1497 if true || layoutready layout
1498 then G.postRedisplay "gotoy ready";
1499 y, layout, true
1501 if proceed
1502 then (
1503 state.y <- y;
1504 state.layout <- layout;
1505 begin match state.mode with
1506 | LinkNav (Ltexact (pageno, linkno)) ->
1507 let rec loop = function
1508 | [] ->
1509 state.mode <- LinkNav (Ltgendir 0)
1510 | l :: _ when l.pageno = pageno ->
1511 begin match getopaque pageno with
1512 | None ->
1513 state.mode <- LinkNav (Ltgendir 0)
1514 | Some opaque ->
1515 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1516 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1517 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1518 then state.mode <- LinkNav (Ltgendir 0)
1520 | _ :: rest -> loop rest
1522 loop layout
1523 | _ -> ()
1524 end;
1525 begin match state.mode with
1526 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1527 if not (pagevisible layout pageno)
1528 then (
1529 match state.layout with
1530 | [] -> ()
1531 | l :: _ ->
1532 state.mode <- Birdseye (
1533 conf, leftx, l.pageno, hooverpageno, anchor
1536 | LinkNav (Ltgendir dir as lt) ->
1537 let linknav =
1538 let rec loop = function
1539 | [] -> lt
1540 | l :: rest ->
1541 match getopaque l.pageno with
1542 | None -> loop rest
1543 | Some opaque ->
1544 let link =
1545 let ld =
1546 if dir = 0
1547 then LDfirstvisible (l.pagex, l.pagey, dir)
1548 else (
1549 if dir > 0 then LDfirst else LDlast
1552 findlink opaque ld
1554 match link with
1555 | Lnotfound -> loop rest
1556 | Lfound n ->
1557 showlinktype (getlink opaque n);
1558 Ltexact (l.pageno, n)
1560 loop state.layout
1562 state.mode <- LinkNav linknav
1563 | _ -> ()
1564 end;
1565 preload layout;
1567 state.ghyll <- noghyll;
1568 if conf.updatecurs
1569 then (
1570 let mx, my = state.mpos in
1571 updateunder mx my;
1575 let conttiling pageno opaque =
1576 tilepage pageno opaque
1577 (if conf.preload then preloadlayout state.layout else state.layout)
1580 let gotoy_and_clear_text y =
1581 if not conf.verbose then state.text <- "";
1582 gotoy y;
1585 let getanchor () =
1586 match state.layout with
1587 | [] -> emptyanchor
1588 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1591 let getanchory (n, top) =
1592 let y, h = getpageyh n in
1593 y + (truncate (top *. float h));
1596 let gotoanchor anchor =
1597 gotoy (getanchory anchor);
1600 let addnav () =
1601 cbput state.hists.nav (getanchor ());
1604 let getnav dir =
1605 let anchor = cbgetc state.hists.nav dir in
1606 getanchory anchor;
1609 let gotoghyll y =
1610 let rec scroll f n a b =
1611 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1612 let snake f a b =
1613 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1614 if f < a
1615 then s (float f /. float a)
1616 else (
1617 if f > b
1618 then 1.0 -. s ((float (f-b) /. float (n-b)))
1619 else 1.0
1622 snake f a b
1623 and summa f n a b =
1624 (* courtesy:
1625 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1626 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1627 let iv1 = iv f in
1628 let ins = float a *. iv1
1629 and outs = float (n-b) *. iv1 in
1630 let ones = b - a in
1631 ins +. outs +. float ones
1633 let rec set (_N, _A, _B) y sy =
1634 let sum = summa 1.0 _N _A _B in
1635 let dy = float (y - sy) in
1636 state.ghyll <- (
1637 let rec gf n y1 o =
1638 if n >= _N
1639 then state.ghyll <- noghyll
1640 else
1641 let go n =
1642 let s = scroll n _N _A _B in
1643 let y1 = y1 +. ((s *. dy) /. sum) in
1644 gotoy_and_clear_text (truncate y1);
1645 state.ghyll <- gf (n+1) y1;
1647 match o with
1648 | None -> go n
1649 | Some y' -> set (_N/2, 0, 0) y' state.y
1651 gf 0 (float state.y)
1654 match conf.ghyllscroll with
1655 | None ->
1656 gotoy_and_clear_text y
1657 | Some nab ->
1658 if state.ghyll == noghyll
1659 then set nab y state.y
1660 else state.ghyll (Some y)
1663 let gotopage n top =
1664 let y, h = getpageyh n in
1665 let y = y + (truncate (top *. float h)) in
1666 gotoghyll y
1669 let gotopage1 n top =
1670 let y = getpagey n in
1671 let y = y + top in
1672 gotoghyll y
1675 let invalidate s f =
1676 state.layout <- [];
1677 state.pdims <- [];
1678 state.rects <- [];
1679 state.rects1 <- [];
1680 match state.geomcmds with
1681 | ps, [] when String.length ps = 0 ->
1682 f ();
1683 state.geomcmds <- s, [];
1685 | ps, [] ->
1686 state.geomcmds <- ps, [s, f];
1688 | ps, (s', _) :: rest when s' = s ->
1689 state.geomcmds <- ps, ((s, f) :: rest);
1691 | ps, cmds ->
1692 state.geomcmds <- ps, ((s, f) :: cmds);
1695 let opendoc path password =
1696 state.path <- path;
1697 state.password <- password;
1698 state.gen <- state.gen + 1;
1699 state.docinfo <- [];
1701 setaalevel conf.aalevel;
1702 Wsi.settitle ("llpp " ^ Filename.basename path);
1703 wcmd "open %s\000%s\000" path password;
1704 invalidate "reqlayout"
1705 (fun () ->
1706 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1709 let scalecolor c =
1710 let c = c *. conf.colorscale in
1711 (c, c, c);
1714 let scalecolor2 (r, g, b) =
1715 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1718 let represent () =
1719 let docolumns = function
1720 | None -> ()
1721 | Some ((columns, coverA, coverB), _) ->
1722 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1723 let rec loop pageno pdimno pdim x y rowh pdims =
1724 if pageno = state.pagecount
1725 then ()
1726 else
1727 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1728 match pdims with
1729 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1730 pdimno+1, pdim, rest
1731 | _ ->
1732 pdimno, pdim, pdims
1734 let x, y, rowh' =
1735 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1736 then (
1737 (conf.winw - state.scrollw - w) / 2,
1738 y + rowh + conf.interpagespace, h
1740 else (
1741 if (pageno - coverA) mod columns = 0
1742 then 0, y + rowh + conf.interpagespace, h
1743 else x, y, max rowh h
1746 let rec fixrow m = if m = pageno then () else
1747 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1748 if h < rowh
1749 then (
1750 let y = y + (rowh - h) / 2 in
1751 a.(m) <- (pdimno, x, y, pdim);
1753 fixrow (m+1)
1755 if pageno > 1 && (pageno - coverA) mod columns = 0
1756 then fixrow (pageno - columns);
1757 a.(pageno) <- (pdimno, x, y, pdim);
1758 let x = x + w + xoff*2 + conf.interpagespace in
1759 loop (pageno+1) pdimno pdim x y rowh' pdims
1761 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1762 conf.columns <- Some ((columns, coverA, coverB), a);
1764 docolumns conf.columns;
1765 state.maxy <- calcheight ();
1766 state.hscrollh <-
1767 if state.w <= conf.winw - state.scrollw
1768 then 0
1769 else state.scrollw
1771 match state.mode with
1772 | Birdseye (_, _, pageno, _, _) ->
1773 let y, h = getpageyh pageno in
1774 let top = (conf.winh - h) / 2 in
1775 gotoy (max 0 (y - top))
1776 | _ -> gotoanchor state.anchor
1779 let reshape w h =
1780 GlDraw.viewport 0 0 w h;
1781 if state.geomcmds != firstgeomcmds && nogeomcmds state.geomcmds
1782 then state.anchor <- getanchor ();
1784 conf.winw <- w;
1785 let w = truncate (float w *. conf.zoom) - state.scrollw in
1786 let w = max w 2 in
1787 conf.winh <- h;
1788 setfontsize fstate.fontsize;
1789 GlMat.mode `modelview;
1790 GlMat.load_identity ();
1792 GlMat.mode `projection;
1793 GlMat.load_identity ();
1794 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1795 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1796 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1798 let relx =
1799 if conf.zoom <= 1.0
1800 then 0.0
1801 else float state.x /. float state.w
1803 invalidate "geometry"
1804 (fun () ->
1805 state.w <- w;
1806 state.x <- truncate (relx *. float w);
1807 let w =
1808 match conf.columns with
1809 | None -> w
1810 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1812 wcmd "geometry %d %d" w h);
1815 let enttext () =
1816 let len = String.length state.text in
1817 let drawstring s =
1818 let hscrollh =
1819 match state.mode with
1820 | Textentry _
1821 | View -> state.hscrollh
1822 | _ -> 0
1824 let rect x w =
1825 GlDraw.rect
1826 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1827 (x+.w, float (conf.winh - hscrollh))
1830 let w = float (conf.winw - state.scrollw - 1) in
1831 if state.progress >= 0.0 && state.progress < 1.0
1832 then (
1833 GlDraw.color (0.3, 0.3, 0.3);
1834 let w1 = w *. state.progress in
1835 rect 0.0 w1;
1836 GlDraw.color (0.0, 0.0, 0.0);
1837 rect w1 (w-.w1)
1839 else (
1840 GlDraw.color (0.0, 0.0, 0.0);
1841 rect 0.0 w;
1844 GlDraw.color (1.0, 1.0, 1.0);
1845 drawstring fstate.fontsize
1846 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1848 let s =
1849 match state.mode with
1850 | Textentry ((prefix, text, _, _, _), _) ->
1851 let s =
1852 if len > 0
1853 then
1854 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1855 else
1856 Printf.sprintf "%s%s_" prefix text
1860 | _ -> state.text
1862 let s =
1863 if state.newerrmsgs
1864 then (
1865 if not (istextentry state.mode)
1866 then
1867 let s1 = "(press 'e' to review error messasges)" in
1868 if String.length s > 0 then s ^ " " ^ s1 else s1
1869 else s
1871 else s
1873 if String.length s > 0
1874 then drawstring s
1877 let gctiles () =
1878 let len = Queue.length state.tilelru in
1879 let rec loop qpos =
1880 if state.memused <= conf.memlimit
1881 then ()
1882 else (
1883 if qpos < len
1884 then
1885 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1886 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1887 let (_, pw, ph, _) = getpagedim n in
1889 gen = state.gen
1890 && colorspace = conf.colorspace
1891 && angle = conf.angle
1892 && pagew = pw
1893 && pageh = ph
1894 && (
1895 let layout =
1896 match state.throttle with
1897 | None ->
1898 if conf.preload
1899 then preloadlayout state.layout
1900 else state.layout
1901 | Some (layout, _, _) ->
1902 layout
1904 let x = col*conf.tilew
1905 and y = row*conf.tileh in
1906 tilevisible layout n x y
1908 then Queue.push lruitem state.tilelru
1909 else (
1910 wcmd "freetile %s" p;
1911 state.memused <- state.memused - s;
1912 state.uioh#infochanged Memused;
1913 Hashtbl.remove state.tilemap k;
1915 loop (qpos+1)
1918 loop 0
1921 let flushtiles () =
1922 Queue.iter (fun (k, p, s) ->
1923 wcmd "freetile %s" p;
1924 state.memused <- state.memused - s;
1925 state.uioh#infochanged Memused;
1926 Hashtbl.remove state.tilemap k;
1927 ) state.tilelru;
1928 Queue.clear state.tilelru;
1929 load state.layout;
1932 let logcurrently = function
1933 | Idle -> dolog "Idle"
1934 | Loading (l, gen) ->
1935 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1936 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1937 dolog
1938 "Tiling %d[%d,%d] page=%s cs=%s angle"
1939 l.pageno col row pageopaque
1940 (colorspace_to_string colorspace)
1942 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1943 angle gen conf.angle state.gen
1944 tilew tileh
1945 conf.tilew conf.tileh
1947 | Outlining _ ->
1948 dolog "outlining"
1951 let act cmds =
1952 (* dolog "%S" cmds; *)
1953 let op, args =
1954 let spacepos =
1955 try String.index cmds ' '
1956 with Not_found -> -1
1958 if spacepos = -1
1959 then cmds, ""
1960 else
1961 let l = String.length cmds in
1962 let op = String.sub cmds 0 spacepos in
1963 op, begin
1964 if l - spacepos < 2 then ""
1965 else String.sub cmds (spacepos+1) (l-spacepos-1)
1968 match op with
1969 | "clear" ->
1970 state.uioh#infochanged Pdim;
1971 state.pdims <- [];
1973 | "clearrects" ->
1974 state.rects <- state.rects1;
1975 G.postRedisplay "clearrects";
1977 | "continue" ->
1978 let n =
1979 try Scanf.sscanf args "%u" (fun n -> n)
1980 with exn ->
1981 dolog "error processing 'continue' %S: %s"
1982 cmds (Printexc.to_string exn);
1983 exit 1;
1985 state.pagecount <- n;
1986 begin match state.currently with
1987 | Outlining l ->
1988 state.currently <- Idle;
1989 state.outlines <- Array.of_list (List.rev l)
1990 | _ -> ()
1991 end;
1993 let cur, cmds = state.geomcmds in
1994 if String.length cur = 0
1995 then failwith "umpossible";
1997 begin match List.rev cmds with
1998 | [] ->
1999 state.geomcmds <- "", [];
2000 represent ();
2001 | (s, f) :: rest ->
2002 f ();
2003 state.geomcmds <- s, List.rev rest;
2004 end;
2005 if conf.maxwait = None
2006 then G.postRedisplay "continue";
2008 | "title" ->
2009 Wsi.settitle args
2011 | "msg" ->
2012 showtext ' ' args
2014 | "vmsg" ->
2015 if conf.verbose
2016 then showtext ' ' args
2018 | "progress" ->
2019 let progress, text =
2021 Scanf.sscanf args "%f %n"
2022 (fun f pos ->
2023 f, String.sub args pos (String.length args - pos))
2024 with exn ->
2025 dolog "error processing 'progress' %S: %s"
2026 cmds (Printexc.to_string exn);
2027 exit 1;
2029 state.text <- text;
2030 state.progress <- progress;
2031 G.postRedisplay "progress"
2033 | "firstmatch" ->
2034 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2036 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2037 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2038 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2039 with exn ->
2040 dolog "error processing 'firstmatch' %S: %s"
2041 cmds (Printexc.to_string exn);
2042 exit 1;
2044 let y = (getpagey pageno) + truncate y0 in
2045 addnav ();
2046 gotoy y;
2047 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2049 | "match" ->
2050 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2052 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2053 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2054 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2055 with exn ->
2056 dolog "error processing 'match' %S: %s"
2057 cmds (Printexc.to_string exn);
2058 exit 1;
2060 state.rects1 <-
2061 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2063 | "page" ->
2064 let pageopaque, t =
2066 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2067 with exn ->
2068 dolog "error processing 'page' %S: %s"
2069 cmds (Printexc.to_string exn);
2070 exit 1;
2072 begin match state.currently with
2073 | Loading (l, gen) ->
2074 vlog "page %d took %f sec" l.pageno t;
2075 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2076 begin match state.throttle with
2077 | None ->
2078 let preloadedpages =
2079 if conf.preload
2080 then preloadlayout state.layout
2081 else state.layout
2083 let evict () =
2084 let module IntSet =
2085 Set.Make (struct type t = int let compare = (-) end) in
2086 let set =
2087 List.fold_left (fun s l -> IntSet.add l.pageno s)
2088 IntSet.empty preloadedpages
2090 let evictedpages =
2091 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2092 if not (IntSet.mem pageno set)
2093 then (
2094 wcmd "freepage %s" opaque;
2095 key :: accu
2097 else accu
2098 ) state.pagemap []
2100 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2102 evict ();
2103 state.currently <- Idle;
2104 if gen = state.gen
2105 then (
2106 tilepage l.pageno pageopaque state.layout;
2107 load state.layout;
2108 load preloadedpages;
2109 if pagevisible state.layout l.pageno
2110 && layoutready state.layout
2111 then G.postRedisplay "page";
2114 | Some (layout, _, _) ->
2115 state.currently <- Idle;
2116 tilepage l.pageno pageopaque layout;
2117 load state.layout
2118 end;
2120 | _ ->
2121 dolog "Inconsistent loading state";
2122 logcurrently state.currently;
2123 exit 1
2126 | "tile" ->
2127 let (x, y, opaque, size, t) =
2129 Scanf.sscanf args "%u %u %s %u %f"
2130 (fun x y p size t -> (x, y, p, size, t))
2131 with exn ->
2132 dolog "error processing 'tile' %S: %s"
2133 cmds (Printexc.to_string exn);
2134 exit 1;
2136 begin match state.currently with
2137 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2138 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2140 if tilew != conf.tilew || tileh != conf.tileh
2141 then (
2142 wcmd "freetile %s" opaque;
2143 state.currently <- Idle;
2144 load state.layout;
2146 else (
2147 puttileopaque l col row gen cs angle opaque size t;
2148 state.memused <- state.memused + size;
2149 state.uioh#infochanged Memused;
2150 gctiles ();
2151 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2152 opaque, size) state.tilelru;
2154 let layout =
2155 match state.throttle with
2156 | None -> state.layout
2157 | Some (layout, _, _) -> layout
2160 state.currently <- Idle;
2161 if gen = state.gen
2162 && conf.colorspace = cs
2163 && conf.angle = angle
2164 && tilevisible layout l.pageno x y
2165 then conttiling l.pageno pageopaque;
2167 begin match state.throttle with
2168 | None ->
2169 preload state.layout;
2170 if gen = state.gen
2171 && conf.colorspace = cs
2172 && conf.angle = angle
2173 && tilevisible state.layout l.pageno x y
2174 then G.postRedisplay "tile nothrottle";
2176 | Some (layout, y, _) ->
2177 let ready = layoutready layout in
2178 if ready
2179 then (
2180 state.y <- y;
2181 state.layout <- layout;
2182 state.throttle <- None;
2183 G.postRedisplay "throttle";
2185 else load layout;
2186 end;
2189 | _ ->
2190 dolog "Inconsistent tiling state";
2191 logcurrently state.currently;
2192 exit 1
2195 | "pdim" ->
2196 let pdim =
2198 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2199 with exn ->
2200 dolog "error processing 'pdim' %S: %s"
2201 cmds (Printexc.to_string exn);
2202 exit 1;
2204 state.uioh#infochanged Pdim;
2205 state.pdims <- pdim :: state.pdims
2207 | "o" ->
2208 let (l, n, t, h, pos) =
2210 Scanf.sscanf args "%u %u %d %u %n"
2211 (fun l n t h pos -> l, n, t, h, pos)
2212 with exn ->
2213 dolog "error processing 'o' %S: %s"
2214 cmds (Printexc.to_string exn);
2215 exit 1;
2217 let s = String.sub args pos (String.length args - pos) in
2218 let outline = (s, l, (n, float t /. float h)) in
2219 begin match state.currently with
2220 | Outlining outlines ->
2221 state.currently <- Outlining (outline :: outlines)
2222 | Idle ->
2223 state.currently <- Outlining [outline]
2224 | currently ->
2225 dolog "invalid outlining state";
2226 logcurrently currently
2229 | "info" ->
2230 state.docinfo <- (1, args) :: state.docinfo
2232 | "infoend" ->
2233 state.uioh#infochanged Docinfo;
2234 state.docinfo <- List.rev state.docinfo
2236 | _ ->
2237 dolog "unknown cmd `%S'" cmds
2240 let onhist cb =
2241 let rc = cb.rc in
2242 let action = function
2243 | HCprev -> cbget cb ~-1
2244 | HCnext -> cbget cb 1
2245 | HCfirst -> cbget cb ~-(cb.rc)
2246 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2247 and cancel () = cb.rc <- rc
2248 in (action, cancel)
2251 let search pattern forward =
2252 if String.length pattern > 0
2253 then
2254 let pn, py =
2255 match state.layout with
2256 | [] -> 0, 0
2257 | l :: _ ->
2258 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2260 wcmd "search %d %d %d %d,%s\000"
2261 (btod conf.icase) pn py (btod forward) pattern;
2264 let intentry text key =
2265 let c =
2266 if key >= 32 && key < 127
2267 then Char.chr key
2268 else '\000'
2270 match c with
2271 | '0' .. '9' ->
2272 let text = addchar text c in
2273 TEcont text
2275 | _ ->
2276 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2277 TEcont text
2280 let textentry text key =
2281 if key land 0xff00 = 0xff00
2282 then TEcont text
2283 else TEcont (text ^ Wsi.toutf8 key)
2286 let reqlayout angle proportional =
2287 match state.throttle with
2288 | None ->
2289 if nogeomcmds state.geomcmds
2290 then state.anchor <- getanchor ();
2291 conf.angle <- angle mod 360;
2292 if conf.angle != 0
2293 then (
2294 match state.mode with
2295 | LinkNav _ -> state.mode <- View
2296 | _ -> ()
2298 conf.proportional <- proportional;
2299 invalidate "reqlayout"
2300 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2301 | _ -> ()
2304 let settrim trimmargins trimfuzz =
2305 if nogeomcmds state.geomcmds
2306 then state.anchor <- getanchor ();
2307 conf.trimmargins <- trimmargins;
2308 conf.trimfuzz <- trimfuzz;
2309 let x0, y0, x1, y1 = trimfuzz in
2310 invalidate "settrim"
2311 (fun () ->
2312 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2313 Hashtbl.iter (fun _ opaque ->
2314 wcmd "freepage %s" opaque;
2315 ) state.pagemap;
2316 Hashtbl.clear state.pagemap;
2319 let setzoom zoom =
2320 match state.throttle with
2321 | None ->
2322 let zoom = max 0.01 zoom in
2323 if zoom <> conf.zoom
2324 then (
2325 state.prevzoom <- conf.zoom;
2326 conf.zoom <- zoom;
2327 reshape conf.winw conf.winh;
2328 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2331 | Some (layout, y, started) ->
2332 let time =
2333 match conf.maxwait with
2334 | None -> 0.0
2335 | Some t -> t
2337 let dt = now () -. started in
2338 if dt > time
2339 then (
2340 state.y <- y;
2341 load layout;
2345 let setcolumns columns coverA coverB =
2346 if columns < 2
2347 then (
2348 conf.columns <- None;
2349 state.x <- 0;
2350 setzoom 1.0;
2352 else (
2353 conf.columns <- Some ((columns, coverA, coverB), [||]);
2354 conf.zoom <- 1.0;
2356 reshape conf.winw conf.winh;
2359 let enterbirdseye () =
2360 let zoom = float conf.thumbw /. float conf.winw in
2361 let birdseyepageno =
2362 let cy = conf.winh / 2 in
2363 let fold = function
2364 | [] -> 0
2365 | l :: rest ->
2366 let rec fold best = function
2367 | [] -> best.pageno
2368 | l :: rest ->
2369 let d = cy - (l.pagedispy + l.pagevh/2)
2370 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2371 if abs d < abs dbest
2372 then fold l rest
2373 else best.pageno
2374 in fold l rest
2376 fold state.layout
2378 state.mode <- Birdseye (
2379 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2381 conf.zoom <- zoom;
2382 conf.presentation <- false;
2383 conf.interpagespace <- 10;
2384 conf.hlinks <- false;
2385 state.x <- 0;
2386 state.mstate <- Mnone;
2387 conf.maxwait <- None;
2388 conf.columns <- (
2389 match conf.beyecolumns with
2390 | Some c ->
2391 conf.zoom <- 1.0;
2392 Some ((c, 0, 0), [||])
2393 | None -> None
2395 Wsi.setcursor Wsi.CURSOR_INHERIT;
2396 if conf.verbose
2397 then
2398 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2399 (100.0*.zoom)
2400 else
2401 state.text <- ""
2403 reshape conf.winw conf.winh;
2406 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2407 state.mode <- View;
2408 conf.zoom <- c.zoom;
2409 conf.presentation <- c.presentation;
2410 conf.interpagespace <- c.interpagespace;
2411 conf.maxwait <- c.maxwait;
2412 conf.hlinks <- c.hlinks;
2413 conf.beyecolumns <- (
2414 match conf.columns with
2415 | Some ((c, _, _), _) -> Some c
2416 | None -> None
2418 conf.columns <- (
2419 match c.columns with
2420 | Some (c, _) -> Some (c, [||])
2421 | None -> None
2423 state.x <- leftx;
2424 if conf.verbose
2425 then
2426 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2427 (100.0*.conf.zoom)
2429 reshape conf.winw conf.winh;
2430 state.anchor <- if goback then anchor else (pageno, 0.0);
2433 let togglebirdseye () =
2434 match state.mode with
2435 | Birdseye vals -> leavebirdseye vals true
2436 | View -> enterbirdseye ()
2437 | _ -> ()
2440 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2441 let pageno = max 0 (pageno - incr) in
2442 let rec loop = function
2443 | [] -> gotopage1 pageno 0
2444 | l :: _ when l.pageno = pageno ->
2445 if l.pagedispy >= 0 && l.pagey = 0
2446 then G.postRedisplay "upbirdseye"
2447 else gotopage1 pageno 0
2448 | _ :: rest -> loop rest
2450 loop state.layout;
2451 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2454 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2455 let pageno = min (state.pagecount - 1) (pageno + incr) in
2456 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2457 let rec loop = function
2458 | [] ->
2459 let y, h = getpageyh pageno in
2460 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2461 gotoy (clamp dy)
2462 | l :: _ when l.pageno = pageno ->
2463 if l.pagevh != l.pageh
2464 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2465 else G.postRedisplay "downbirdseye"
2466 | _ :: rest -> loop rest
2468 loop state.layout
2471 let optentry mode _ key =
2472 let btos b = if b then "on" else "off" in
2473 if key >= 32 && key < 127
2474 then
2475 let c = Char.chr key in
2476 match c with
2477 | 's' ->
2478 let ondone s =
2479 try conf.scrollstep <- int_of_string s with exc ->
2480 state.text <- Printf.sprintf "bad integer `%s': %s"
2481 s (Printexc.to_string exc)
2483 TEswitch ("scroll step: ", "", None, intentry, ondone)
2485 | 'A' ->
2486 let ondone s =
2488 conf.autoscrollstep <- int_of_string s;
2489 if state.autoscroll <> None
2490 then state.autoscroll <- Some conf.autoscrollstep
2491 with exc ->
2492 state.text <- Printf.sprintf "bad integer `%s': %s"
2493 s (Printexc.to_string exc)
2495 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2497 | 'C' ->
2498 let ondone s =
2500 let n, a, b = columns_of_string s in
2501 setcolumns n a b;
2502 with exc ->
2503 state.text <- Printf.sprintf "bad columns `%s': %s"
2504 s (Printexc.to_string exc)
2506 TEswitch ("columns: ", "", None, textentry, ondone)
2508 | 'Z' ->
2509 let ondone s =
2511 let zoom = float (int_of_string s) /. 100.0 in
2512 setzoom zoom
2513 with exc ->
2514 state.text <- Printf.sprintf "bad integer `%s': %s"
2515 s (Printexc.to_string exc)
2517 TEswitch ("zoom: ", "", None, intentry, ondone)
2519 | 't' ->
2520 let ondone s =
2522 conf.thumbw <- bound (int_of_string s) 2 4096;
2523 state.text <-
2524 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2525 begin match mode with
2526 | Birdseye beye ->
2527 leavebirdseye beye false;
2528 enterbirdseye ();
2529 | _ -> ();
2531 with exc ->
2532 state.text <- Printf.sprintf "bad integer `%s': %s"
2533 s (Printexc.to_string exc)
2535 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2537 | 'R' ->
2538 let ondone s =
2539 match try
2540 Some (int_of_string s)
2541 with exc ->
2542 state.text <- Printf.sprintf "bad integer `%s': %s"
2543 s (Printexc.to_string exc);
2544 None
2545 with
2546 | Some angle -> reqlayout angle conf.proportional
2547 | None -> ()
2549 TEswitch ("rotation: ", "", None, intentry, ondone)
2551 | 'i' ->
2552 conf.icase <- not conf.icase;
2553 TEdone ("case insensitive search " ^ (btos conf.icase))
2555 | 'p' ->
2556 conf.preload <- not conf.preload;
2557 gotoy state.y;
2558 TEdone ("preload " ^ (btos conf.preload))
2560 | 'v' ->
2561 conf.verbose <- not conf.verbose;
2562 TEdone ("verbose " ^ (btos conf.verbose))
2564 | 'd' ->
2565 conf.debug <- not conf.debug;
2566 TEdone ("debug " ^ (btos conf.debug))
2568 | 'h' ->
2569 conf.maxhfit <- not conf.maxhfit;
2570 state.maxy <-
2571 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2572 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2574 | 'c' ->
2575 conf.crophack <- not conf.crophack;
2576 TEdone ("crophack " ^ btos conf.crophack)
2578 | 'a' ->
2579 let s =
2580 match conf.maxwait with
2581 | None ->
2582 conf.maxwait <- Some infinity;
2583 "always wait for page to complete"
2584 | Some _ ->
2585 conf.maxwait <- None;
2586 "show placeholder if page is not ready"
2588 TEdone s
2590 | 'f' ->
2591 conf.underinfo <- not conf.underinfo;
2592 TEdone ("underinfo " ^ btos conf.underinfo)
2594 | 'P' ->
2595 conf.savebmarks <- not conf.savebmarks;
2596 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2598 | 'S' ->
2599 let ondone s =
2601 let pageno, py =
2602 match state.layout with
2603 | [] -> 0, 0
2604 | l :: _ ->
2605 l.pageno, l.pagey
2607 conf.interpagespace <- int_of_string s;
2608 state.maxy <- calcheight ();
2609 let y = getpagey pageno in
2610 gotoy (y + py)
2611 with exc ->
2612 state.text <- Printf.sprintf "bad integer `%s': %s"
2613 s (Printexc.to_string exc)
2615 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2617 | 'l' ->
2618 reqlayout conf.angle (not conf.proportional);
2619 TEdone ("proportional display " ^ btos conf.proportional)
2621 | 'T' ->
2622 settrim (not conf.trimmargins) conf.trimfuzz;
2623 TEdone ("trim margins " ^ btos conf.trimmargins)
2625 | 'I' ->
2626 conf.invert <- not conf.invert;
2627 TEdone ("invert colors " ^ btos conf.invert)
2629 | 'x' ->
2630 let ondone s =
2631 cbput state.hists.sel s;
2632 conf.selcmd <- s;
2634 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2635 textentry, ondone)
2637 | _ ->
2638 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2639 TEstop
2640 else
2641 TEcont state.text
2644 class type lvsource = object
2645 method getitemcount : int
2646 method getitem : int -> (string * int)
2647 method hasaction : int -> bool
2648 method exit :
2649 uioh:uioh ->
2650 cancel:bool ->
2651 active:int ->
2652 first:int ->
2653 pan:int ->
2654 qsearch:string ->
2655 uioh option
2656 method getactive : int
2657 method getfirst : int
2658 method getqsearch : string
2659 method setqsearch : string -> unit
2660 method getpan : int
2661 end;;
2663 class virtual lvsourcebase = object
2664 val mutable m_active = 0
2665 val mutable m_first = 0
2666 val mutable m_qsearch = ""
2667 val mutable m_pan = 0
2668 method getactive = m_active
2669 method getfirst = m_first
2670 method getqsearch = m_qsearch
2671 method getpan = m_pan
2672 method setqsearch s = m_qsearch <- s
2673 end;;
2675 let withoutlastutf8 s =
2676 let len = String.length s in
2677 if len = 0
2678 then s
2679 else
2680 let rec find pos =
2681 if pos = 0
2682 then pos
2683 else
2684 let b = Char.code s.[pos] in
2685 if b land 0b110000 = 0b11000000
2686 then find (pos-1)
2687 else pos-1
2689 let first =
2690 if Char.code s.[len-1] land 0x80 = 0
2691 then len-1
2692 else find (len-1)
2694 String.sub s 0 first;
2697 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2698 let enttext te =
2699 state.mode <- Textentry (te, onleave);
2700 state.text <- "";
2701 enttext ();
2702 G.postRedisplay "textentrykeyboard enttext";
2704 let histaction cmd =
2705 match opthist with
2706 | None -> ()
2707 | Some (action, _) ->
2708 state.mode <- Textentry (
2709 (c, action cmd, opthist, onkey, ondone), onleave
2711 G.postRedisplay "textentry histaction"
2713 match key with
2714 | 0xff08 -> (* backspace *)
2715 let s = withoutlastutf8 text in
2716 let len = String.length s in
2717 if len = 0
2718 then (
2719 onleave Cancel;
2720 G.postRedisplay "textentrykeyboard after cancel";
2722 else (
2723 enttext (c, s, opthist, onkey, ondone)
2726 | 0xff0d ->
2727 ondone text;
2728 onleave Confirm;
2729 G.postRedisplay "textentrykeyboard after confirm"
2731 | 0xff52 -> histaction HCprev
2732 | 0xff54 -> histaction HCnext
2733 | 0xff50 -> histaction HCfirst
2734 | 0xff57 -> histaction HClast
2736 | 0xff1b -> (* escape*)
2737 if String.length text = 0
2738 then (
2739 begin match opthist with
2740 | None -> ()
2741 | Some (_, onhistcancel) -> onhistcancel ()
2742 end;
2743 onleave Cancel;
2744 state.text <- "";
2745 G.postRedisplay "textentrykeyboard after cancel2"
2747 else (
2748 enttext (c, "", opthist, onkey, ondone)
2751 | 0xff9f | 0xffff -> () (* delete *)
2753 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2754 begin match onkey text key with
2755 | TEdone text ->
2756 ondone text;
2757 onleave Confirm;
2758 G.postRedisplay "textentrykeyboard after confirm2";
2760 | TEcont text ->
2761 enttext (c, text, opthist, onkey, ondone);
2763 | TEstop ->
2764 onleave Cancel;
2765 G.postRedisplay "textentrykeyboard after cancel3"
2767 | TEswitch te ->
2768 state.mode <- Textentry (te, onleave);
2769 G.postRedisplay "textentrykeyboard switch";
2770 end;
2772 | _ ->
2773 vlog "unhandled key %s" (Wsi.keyname key)
2776 let firstof first active =
2777 if first > active || abs (first - active) > fstate.maxrows - 1
2778 then max 0 (active - (fstate.maxrows/2))
2779 else first
2782 let calcfirst first active =
2783 if active > first
2784 then
2785 let rows = active - first in
2786 if rows > fstate.maxrows then active - fstate.maxrows else first
2787 else active
2790 let scrollph y maxy =
2791 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2792 let sh = float conf.winh /. sh in
2793 let sh = max sh (float conf.scrollh) in
2795 let percent =
2796 if y = state.maxy
2797 then 1.0
2798 else float y /. float maxy
2800 let position = (float conf.winh -. sh) *. percent in
2802 let position =
2803 if position +. sh > float conf.winh
2804 then float conf.winh -. sh
2805 else position
2807 position, sh;
2810 let coe s = (s :> uioh);;
2812 class listview ~(source:lvsource) ~trusted ~modehash =
2813 object (self)
2814 val m_pan = source#getpan
2815 val m_first = source#getfirst
2816 val m_active = source#getactive
2817 val m_qsearch = source#getqsearch
2818 val m_prev_uioh = state.uioh
2820 method private elemunder y =
2821 let n = y / (fstate.fontsize+1) in
2822 if m_first + n < source#getitemcount
2823 then (
2824 if source#hasaction (m_first + n)
2825 then Some (m_first + n)
2826 else None
2828 else None
2830 method display =
2831 Gl.enable `blend;
2832 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2833 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2834 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2835 GlDraw.color (1., 1., 1.);
2836 Gl.enable `texture_2d;
2837 let fs = fstate.fontsize in
2838 let nfs = fs + 1 in
2839 let ww = fstate.wwidth in
2840 let tabw = 30.0*.ww in
2841 let itemcount = source#getitemcount in
2842 let rec loop row =
2843 if (row - m_first) * nfs > conf.winh
2844 then ()
2845 else (
2846 if row >= 0 && row < itemcount
2847 then (
2848 let (s, level) = source#getitem row in
2849 let y = (row - m_first) * nfs in
2850 let x = 5.0 +. float (level + m_pan) *. ww in
2851 if row = m_active
2852 then (
2853 Gl.disable `texture_2d;
2854 GlDraw.polygon_mode `both `line;
2855 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2856 GlDraw.rect (1., float (y + 1))
2857 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2858 GlDraw.polygon_mode `both `fill;
2859 GlDraw.color (1., 1., 1.);
2860 Gl.enable `texture_2d;
2863 let drawtabularstring s =
2864 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2865 if trusted
2866 then
2867 let tabpos = try String.index s '\t' with Not_found -> -1 in
2868 if tabpos > 0
2869 then
2870 let len = String.length s - tabpos - 1 in
2871 let s1 = String.sub s 0 tabpos
2872 and s2 = String.sub s (tabpos + 1) len in
2873 let nx = drawstr x s1 in
2874 let sw = nx -. x in
2875 let x = x +. (max tabw sw) in
2876 drawstr x s2
2877 else
2878 drawstr x s
2879 else
2880 drawstr x s
2882 let _ = drawtabularstring s in
2883 loop (row+1)
2887 loop m_first;
2888 Gl.disable `blend;
2889 Gl.disable `texture_2d;
2891 method updownlevel incr =
2892 let len = source#getitemcount in
2893 let curlevel =
2894 if m_active >= 0 && m_active < len
2895 then snd (source#getitem m_active)
2896 else -1
2898 let rec flow i =
2899 if i = len then i-1 else if i = -1 then 0 else
2900 let _, l = source#getitem i in
2901 if l != curlevel then i else flow (i+incr)
2903 let active = flow m_active in
2904 let first = calcfirst m_first active in
2905 G.postRedisplay "outline updownlevel";
2906 {< m_active = active; m_first = first >}
2908 method private key1 key mask =
2909 let set1 active first qsearch =
2910 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2912 let search active pattern incr =
2913 let dosearch re =
2914 let rec loop n =
2915 if n >= 0 && n < source#getitemcount
2916 then (
2917 let s, _ = source#getitem n in
2919 (try ignore (Str.search_forward re s 0); true
2920 with Not_found -> false)
2921 then Some n
2922 else loop (n + incr)
2924 else None
2926 loop active
2929 let re = Str.regexp_case_fold pattern in
2930 dosearch re
2931 with Failure s ->
2932 state.text <- s;
2933 None
2935 let itemcount = source#getitemcount in
2936 let find start incr =
2937 let rec find i =
2938 if i = -1 || i = itemcount
2939 then -1
2940 else (
2941 if source#hasaction i
2942 then i
2943 else find (i + incr)
2946 find start
2948 let set active first =
2949 let first = bound first 0 (itemcount - fstate.maxrows) in
2950 state.text <- "";
2951 coe {< m_active = active; m_first = first >}
2953 let navigate incr =
2954 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2955 let active, first =
2956 let incr1 = if incr > 0 then 1 else -1 in
2957 if isvisible m_first m_active
2958 then
2959 let next =
2960 let next = m_active + incr in
2961 let next =
2962 if next < 0 || next >= itemcount
2963 then -1
2964 else find next incr1
2966 if next = -1 || abs (m_active - next) > fstate.maxrows
2967 then -1
2968 else next
2970 if next = -1
2971 then
2972 let first = m_first + incr in
2973 let first = bound first 0 (itemcount - 1) in
2974 let next =
2975 let next = m_active + incr in
2976 let next = bound next 0 (itemcount - 1) in
2977 find next ~-incr1
2979 let active = if next = -1 then m_active else next in
2980 active, first
2981 else
2982 let first = min next m_first in
2983 let first =
2984 if abs (next - first) > fstate.maxrows
2985 then first + incr
2986 else first
2988 next, first
2989 else
2990 let first = m_first + incr in
2991 let first = bound first 0 (itemcount - 1) in
2992 let active =
2993 let next = m_active + incr in
2994 let next = bound next 0 (itemcount - 1) in
2995 let next = find next incr1 in
2996 let active =
2997 if next = -1 || abs (m_active - first) > fstate.maxrows
2998 then (
2999 let active = if m_active = -1 then next else m_active in
3000 active
3002 else next
3004 if isvisible first active
3005 then active
3006 else -1
3008 active, first
3010 G.postRedisplay "listview navigate";
3011 set active first;
3013 match key with
3014 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3015 let incr = if key = 0x72 then -1 else 1 in
3016 let active, first =
3017 match search (m_active + incr) m_qsearch incr with
3018 | None ->
3019 state.text <- m_qsearch ^ " [not found]";
3020 m_active, m_first
3021 | Some active ->
3022 state.text <- m_qsearch;
3023 active, firstof m_first active
3025 G.postRedisplay "listview ctrl-r/s";
3026 set1 active first m_qsearch;
3028 | 0xff08 -> (* backspace *)
3029 if String.length m_qsearch = 0
3030 then coe self
3031 else (
3032 let qsearch = withoutlastutf8 m_qsearch in
3033 let len = String.length qsearch in
3034 if len = 0
3035 then (
3036 state.text <- "";
3037 G.postRedisplay "listview empty qsearch";
3038 set1 m_active m_first "";
3040 else
3041 let active, first =
3042 match search m_active qsearch ~-1 with
3043 | None ->
3044 state.text <- qsearch ^ " [not found]";
3045 m_active, m_first
3046 | Some active ->
3047 state.text <- qsearch;
3048 active, firstof m_first active
3050 G.postRedisplay "listview backspace qsearch";
3051 set1 active first qsearch
3054 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3055 let pattern = m_qsearch ^ Wsi.toutf8 key in
3056 let active, first =
3057 match search m_active pattern 1 with
3058 | None ->
3059 state.text <- pattern ^ " [not found]";
3060 m_active, m_first
3061 | Some active ->
3062 state.text <- pattern;
3063 active, firstof m_first active
3065 G.postRedisplay "listview qsearch add";
3066 set1 active first pattern;
3068 | 0xff1b -> (* escape *)
3069 state.text <- "";
3070 if String.length m_qsearch = 0
3071 then (
3072 G.postRedisplay "list view escape";
3073 begin
3074 match
3075 source#exit (coe self) true m_active m_first m_pan m_qsearch
3076 with
3077 | None -> m_prev_uioh
3078 | Some uioh -> uioh
3081 else (
3082 G.postRedisplay "list view kill qsearch";
3083 source#setqsearch "";
3084 coe {< m_qsearch = "" >}
3087 | 0xff0d -> (* return *)
3088 state.text <- "";
3089 let self = {< m_qsearch = "" >} in
3090 source#setqsearch "";
3091 let opt =
3092 G.postRedisplay "listview enter";
3093 if m_active >= 0 && m_active < source#getitemcount
3094 then (
3095 source#exit (coe self) false m_active m_first m_pan "";
3097 else (
3098 source#exit (coe self) true m_active m_first m_pan "";
3101 begin match opt with
3102 | None -> m_prev_uioh
3103 | Some uioh -> uioh
3106 | 0xff9f | 0xffff -> (* delete *)
3107 coe self
3109 | 0xff52 -> navigate ~-1 (* up *)
3110 | 0xff54 -> navigate 1 (* down *)
3111 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3112 | 0xff56 -> navigate fstate.maxrows (* next *)
3114 | 0xff53 -> (* right *)
3115 state.text <- "";
3116 G.postRedisplay "listview right";
3117 coe {< m_pan = m_pan - 1 >}
3119 | 0xff51 -> (* left *)
3120 state.text <- "";
3121 G.postRedisplay "listview left";
3122 coe {< m_pan = m_pan + 1 >}
3124 | 0xff50 -> (* home *)
3125 let active = find 0 1 in
3126 G.postRedisplay "listview home";
3127 set active 0;
3129 | 0xff57 -> (* end *)
3130 let first = max 0 (itemcount - fstate.maxrows) in
3131 let active = find (itemcount - 1) ~-1 in
3132 G.postRedisplay "listview end";
3133 set active first;
3135 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3136 coe self
3138 | _ ->
3139 dolog "listview unknown key %#x" key; coe self
3141 method key key mask =
3142 match state.mode with
3143 | Textentry te -> textentrykeyboard key mask te; coe self
3144 | _ -> self#key1 key mask
3146 method button button down x y _ =
3147 let opt =
3148 match button with
3149 | 1 when x > conf.winw - conf.scrollbw ->
3150 G.postRedisplay "listview scroll";
3151 if down
3152 then
3153 let _, position, sh = self#scrollph in
3154 if y > truncate position && y < truncate (position +. sh)
3155 then (
3156 state.mstate <- Mscrolly;
3157 Some (coe self)
3159 else
3160 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3161 let first = truncate (s *. float source#getitemcount) in
3162 let first = min source#getitemcount first in
3163 Some (coe {< m_first = first; m_active = first >})
3164 else (
3165 state.mstate <- Mnone;
3166 Some (coe self);
3168 | 1 when not down ->
3169 begin match self#elemunder y with
3170 | Some n ->
3171 G.postRedisplay "listview click";
3172 source#exit
3173 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3174 | _ ->
3175 Some (coe self)
3177 | n when (n == 4 || n == 5) && not down ->
3178 let len = source#getitemcount in
3179 let first =
3180 if n = 5 && m_first + fstate.maxrows >= len
3181 then
3182 m_first
3183 else
3184 let first = m_first + (if n == 4 then -1 else 1) in
3185 bound first 0 (len - 1)
3187 G.postRedisplay "listview wheel";
3188 Some (coe {< m_first = first >})
3189 | _ ->
3190 Some (coe self)
3192 match opt with
3193 | None -> m_prev_uioh
3194 | Some uioh -> uioh
3196 method motion _ y =
3197 match state.mstate with
3198 | Mscrolly ->
3199 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3200 let first = truncate (s *. float source#getitemcount) in
3201 let first = min source#getitemcount first in
3202 G.postRedisplay "listview motion";
3203 coe {< m_first = first; m_active = first >}
3204 | _ -> coe self
3206 method pmotion x y =
3207 if x < conf.winw - conf.scrollbw
3208 then
3209 let n =
3210 match self#elemunder y with
3211 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3212 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3214 let o =
3215 if n != m_active
3216 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3217 else self
3219 coe o
3220 else (
3221 Wsi.setcursor Wsi.CURSOR_INHERIT;
3222 coe self
3225 method infochanged _ = ()
3227 method scrollpw = (0, 0.0, 0.0)
3228 method scrollph =
3229 let nfs = fstate.fontsize + 1 in
3230 let y = m_first * nfs in
3231 let itemcount = source#getitemcount in
3232 let maxi = max 0 (itemcount - fstate.maxrows) in
3233 let maxy = maxi * nfs in
3234 let p, h = scrollph y maxy in
3235 conf.scrollbw, p, h
3237 method modehash = modehash
3238 end;;
3240 class outlinelistview ~source =
3241 object (self)
3242 inherit listview
3243 ~source:(source :> lvsource)
3244 ~trusted:false
3245 ~modehash:(findkeyhash conf "outline")
3246 as super
3248 method key key mask =
3249 let calcfirst first active =
3250 if active > first
3251 then
3252 let rows = active - first in
3253 if rows > fstate.maxrows then active - fstate.maxrows else first
3254 else active
3256 let navigate incr =
3257 let active = m_active + incr in
3258 let active = bound active 0 (source#getitemcount - 1) in
3259 let first = calcfirst m_first active in
3260 G.postRedisplay "outline navigate";
3261 coe {< m_active = active; m_first = first >}
3263 let ctrl = Wsi.withctrl mask in
3264 match key with
3265 | 110 when ctrl -> (* ctrl-n *)
3266 source#narrow m_qsearch;
3267 G.postRedisplay "outline ctrl-n";
3268 coe {< m_first = 0; m_active = 0 >}
3270 | 117 when ctrl -> (* ctrl-u *)
3271 source#denarrow;
3272 G.postRedisplay "outline ctrl-u";
3273 state.text <- "";
3274 coe {< m_first = 0; m_active = 0 >}
3276 | 108 when ctrl -> (* ctrl-l *)
3277 let first = m_active - (fstate.maxrows / 2) in
3278 G.postRedisplay "outline ctrl-l";
3279 coe {< m_first = first >}
3281 | 0xff9f | 0xffff -> (* delete *)
3282 source#remove m_active;
3283 G.postRedisplay "outline delete";
3284 let active = max 0 (m_active-1) in
3285 coe {< m_first = firstof m_first active;
3286 m_active = active >}
3288 | 0xff52 -> navigate ~-1 (* up *)
3289 | 0xff54 -> navigate 1 (* down *)
3290 | 0xff55 -> (* prior *)
3291 navigate ~-(fstate.maxrows)
3292 | 0xff56 -> (* next *)
3293 navigate fstate.maxrows
3295 | 0xff53 -> (* [ctrl-]right *)
3296 let o =
3297 if ctrl
3298 then (
3299 G.postRedisplay "outline ctrl right";
3300 {< m_pan = m_pan + 1 >}
3302 else self#updownlevel 1
3304 coe o
3306 | 0xff51 -> (* [ctrl-]left *)
3307 let o =
3308 if ctrl
3309 then (
3310 G.postRedisplay "outline ctrl left";
3311 {< m_pan = m_pan - 1 >}
3313 else self#updownlevel ~-1
3315 coe o
3317 | 0xff50 -> (* home *)
3318 G.postRedisplay "outline home";
3319 coe {< m_first = 0; m_active = 0 >}
3321 | 0xff57 -> (* end *)
3322 let active = source#getitemcount - 1 in
3323 let first = max 0 (active - fstate.maxrows) in
3324 G.postRedisplay "outline end";
3325 coe {< m_active = active; m_first = first >}
3327 | _ -> super#key key mask
3330 let outlinesource usebookmarks =
3331 let empty = [||] in
3332 (object
3333 inherit lvsourcebase
3334 val mutable m_items = empty
3335 val mutable m_orig_items = empty
3336 val mutable m_prev_items = empty
3337 val mutable m_narrow_pattern = ""
3338 val mutable m_hadremovals = false
3340 method getitemcount =
3341 Array.length m_items + (if m_hadremovals then 1 else 0)
3343 method getitem n =
3344 if n == Array.length m_items && m_hadremovals
3345 then
3346 ("[Confirm removal]", 0)
3347 else
3348 let s, n, _ = m_items.(n) in
3349 (s, n)
3351 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3352 ignore (uioh, first, qsearch);
3353 let confrimremoval = m_hadremovals && active = Array.length m_items in
3354 let items =
3355 if String.length m_narrow_pattern = 0
3356 then m_orig_items
3357 else m_items
3359 if not cancel
3360 then (
3361 if not confrimremoval
3362 then(
3363 let _, _, anchor = m_items.(active) in
3364 gotoanchor anchor;
3365 m_items <- items;
3367 else (
3368 state.bookmarks <- Array.to_list m_items;
3369 m_orig_items <- m_items;
3372 else m_items <- items;
3373 m_pan <- pan;
3374 None
3376 method hasaction _ = true
3378 method greetmsg =
3379 if Array.length m_items != Array.length m_orig_items
3380 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3381 else ""
3383 method narrow pattern =
3384 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3385 match reopt with
3386 | None -> ()
3387 | Some re ->
3388 let rec loop accu n =
3389 if n = -1
3390 then (
3391 m_narrow_pattern <- pattern;
3392 m_items <- Array.of_list accu
3394 else
3395 let (s, _, _) as o = m_items.(n) in
3396 let accu =
3397 if (try ignore (Str.search_forward re s 0); true
3398 with Not_found -> false)
3399 then o :: accu
3400 else accu
3402 loop accu (n-1)
3404 loop [] (Array.length m_items - 1)
3406 method denarrow =
3407 m_orig_items <- (
3408 if usebookmarks
3409 then Array.of_list state.bookmarks
3410 else state.outlines
3412 m_items <- m_orig_items
3414 method remove m =
3415 if usebookmarks
3416 then
3417 if m >= 0 && m < Array.length m_items
3418 then (
3419 m_hadremovals <- true;
3420 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3421 let n = if n >= m then n+1 else n in
3422 m_items.(n)
3426 method reset anchor items =
3427 m_hadremovals <- false;
3428 if m_orig_items == empty || m_prev_items != items
3429 then (
3430 m_orig_items <- items;
3431 if String.length m_narrow_pattern = 0
3432 then m_items <- items;
3434 m_prev_items <- items;
3435 let rely = getanchory anchor in
3436 let active =
3437 let rec loop n best bestd =
3438 if n = Array.length m_items
3439 then best
3440 else
3441 let (_, _, anchor) = m_items.(n) in
3442 let orely = getanchory anchor in
3443 let d = abs (orely - rely) in
3444 if d < bestd
3445 then loop (n+1) n d
3446 else loop (n+1) best bestd
3448 loop 0 ~-1 max_int
3450 m_active <- active;
3451 m_first <- firstof m_first active
3452 end)
3455 let enterselector usebookmarks =
3456 let source = outlinesource usebookmarks in
3457 fun errmsg ->
3458 let outlines =
3459 if usebookmarks
3460 then Array.of_list state.bookmarks
3461 else state.outlines
3463 if Array.length outlines = 0
3464 then (
3465 showtext ' ' errmsg;
3467 else (
3468 state.text <- source#greetmsg;
3469 Wsi.setcursor Wsi.CURSOR_INHERIT;
3470 let anchor = getanchor () in
3471 source#reset anchor outlines;
3472 state.uioh <- coe (new outlinelistview ~source);
3473 G.postRedisplay "enter selector";
3477 let enteroutlinemode =
3478 let f = enterselector false in
3479 fun ()-> f "Document has no outline";
3482 let enterbookmarkmode =
3483 let f = enterselector true in
3484 fun () -> f "Document has no bookmarks (yet)";
3487 let color_of_string s =
3488 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3489 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3493 let color_to_string (r, g, b) =
3494 let r = truncate (r *. 256.0)
3495 and g = truncate (g *. 256.0)
3496 and b = truncate (b *. 256.0) in
3497 Printf.sprintf "%d/%d/%d" r g b
3500 let irect_of_string s =
3501 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3504 let irect_to_string (x0,y0,x1,y1) =
3505 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3508 let makecheckers () =
3509 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3510 following to say:
3511 converted by Issac Trotts. July 25, 2002 *)
3512 let image_height = 64
3513 and image_width = 64 in
3515 let make_image () =
3516 let image =
3517 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3519 for i = 0 to image_width - 1 do
3520 for j = 0 to image_height - 1 do
3521 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3522 (if (i land 8 ) lxor (j land 8) = 0
3523 then [|255;255;255|] else [|200;200;200|])
3524 done
3525 done;
3526 image
3528 let image = make_image () in
3529 let id = GlTex.gen_texture () in
3530 GlTex.bind_texture `texture_2d id;
3531 GlPix.store (`unpack_alignment 1);
3532 GlTex.image2d image;
3533 List.iter (GlTex.parameter ~target:`texture_2d)
3534 [ `wrap_s `repeat;
3535 `wrap_t `repeat;
3536 `mag_filter `nearest;
3537 `min_filter `nearest ];
3541 let setcheckers enabled =
3542 match state.texid with
3543 | None ->
3544 if enabled then state.texid <- Some (makecheckers ())
3546 | Some texid ->
3547 if not enabled
3548 then (
3549 GlTex.delete_texture texid;
3550 state.texid <- None;
3554 let int_of_string_with_suffix s =
3555 let l = String.length s in
3556 let s1, shift =
3557 if l > 1
3558 then
3559 let suffix = Char.lowercase s.[l-1] in
3560 match suffix with
3561 | 'k' -> String.sub s 0 (l-1), 10
3562 | 'm' -> String.sub s 0 (l-1), 20
3563 | 'g' -> String.sub s 0 (l-1), 30
3564 | _ -> s, 0
3565 else s, 0
3567 let n = int_of_string s1 in
3568 let m = n lsl shift in
3569 if m < 0 || m < n
3570 then raise (Failure "value too large")
3571 else m
3574 let string_with_suffix_of_int n =
3575 if n = 0
3576 then "0"
3577 else
3578 let n, s =
3579 if n = 0
3580 then 0, ""
3581 else (
3582 if n land ((1 lsl 20) - 1) = 0
3583 then n lsr 20, "M"
3584 else (
3585 if n land ((1 lsl 10) - 1) = 0
3586 then n lsr 10, "K"
3587 else n, ""
3591 let rec loop s n =
3592 let h = n mod 1000 in
3593 let n = n / 1000 in
3594 if n = 0
3595 then string_of_int h ^ s
3596 else (
3597 let s = Printf.sprintf "_%03d%s" h s in
3598 loop s n
3601 loop "" n ^ s;
3604 let defghyllscroll = (40, 8, 32);;
3605 let ghyllscroll_of_string s =
3606 let (n, a, b) as nab =
3607 if s = "default"
3608 then defghyllscroll
3609 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3611 if n <= a || n <= b || a >= b
3612 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3613 nab;
3616 let ghyllscroll_to_string ((n, a, b) as nab) =
3617 if nab = defghyllscroll
3618 then "default"
3619 else Printf.sprintf "%d,%d,%d" n a b;
3622 let describe_location () =
3623 let f (fn, _) l =
3624 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3626 let fn, ln = List.fold_left f (-1, -1) state.layout in
3627 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3628 let percent =
3629 if maxy <= 0
3630 then 100.
3631 else (100. *. (float state.y /. float maxy))
3633 if fn = ln
3634 then
3635 Printf.sprintf "page %d of %d [%.2f%%]"
3636 (fn+1) state.pagecount percent
3637 else
3638 Printf.sprintf
3639 "pages %d-%d of %d [%.2f%%]"
3640 (fn+1) (ln+1) state.pagecount percent
3643 let enterinfomode =
3644 let btos b = if b then "\xe2\x88\x9a" else "" in
3645 let showextended = ref false in
3646 let leave mode = function
3647 | Confirm -> state.mode <- mode
3648 | Cancel -> state.mode <- mode in
3649 let src =
3650 (object
3651 val mutable m_first_time = true
3652 val mutable m_l = []
3653 val mutable m_a = [||]
3654 val mutable m_prev_uioh = nouioh
3655 val mutable m_prev_mode = View
3657 inherit lvsourcebase
3659 method reset prev_mode prev_uioh =
3660 m_a <- Array.of_list (List.rev m_l);
3661 m_l <- [];
3662 m_prev_mode <- prev_mode;
3663 m_prev_uioh <- prev_uioh;
3664 if m_first_time
3665 then (
3666 let rec loop n =
3667 if n >= Array.length m_a
3668 then ()
3669 else
3670 match m_a.(n) with
3671 | _, _, _, Action _ -> m_active <- n
3672 | _ -> loop (n+1)
3674 loop 0;
3675 m_first_time <- false;
3678 method int name get set =
3679 m_l <-
3680 (name, `int get, 1, Action (
3681 fun u ->
3682 let ondone s =
3683 try set (int_of_string s)
3684 with exn ->
3685 state.text <- Printf.sprintf "bad integer `%s': %s"
3686 s (Printexc.to_string exn)
3688 state.text <- "";
3689 let te = name ^ ": ", "", None, intentry, ondone in
3690 state.mode <- Textentry (te, leave m_prev_mode);
3692 )) :: m_l
3694 method int_with_suffix name get set =
3695 m_l <-
3696 (name, `intws get, 1, Action (
3697 fun u ->
3698 let ondone s =
3699 try set (int_of_string_with_suffix s)
3700 with exn ->
3701 state.text <- Printf.sprintf "bad integer `%s': %s"
3702 s (Printexc.to_string exn)
3704 state.text <- "";
3705 let te =
3706 name ^ ": ", "", None, intentry_with_suffix, ondone
3708 state.mode <- Textentry (te, leave m_prev_mode);
3710 )) :: m_l
3712 method bool ?(offset=1) ?(btos=btos) name get set =
3713 m_l <-
3714 (name, `bool (btos, get), offset, Action (
3715 fun u ->
3716 let v = get () in
3717 set (not v);
3719 )) :: m_l
3721 method color name get set =
3722 m_l <-
3723 (name, `color get, 1, Action (
3724 fun u ->
3725 let invalid = (nan, nan, nan) in
3726 let ondone s =
3727 let c =
3728 try color_of_string s
3729 with exn ->
3730 state.text <- Printf.sprintf "bad color `%s': %s"
3731 s (Printexc.to_string exn);
3732 invalid
3734 if c <> invalid
3735 then set c;
3737 let te = name ^ ": ", "", None, textentry, ondone in
3738 state.text <- color_to_string (get ());
3739 state.mode <- Textentry (te, leave m_prev_mode);
3741 )) :: m_l
3743 method string name get set =
3744 m_l <-
3745 (name, `string get, 1, Action (
3746 fun u ->
3747 let ondone s = set s in
3748 let te = name ^ ": ", "", None, textentry, ondone in
3749 state.mode <- Textentry (te, leave m_prev_mode);
3751 )) :: m_l
3753 method colorspace name get set =
3754 m_l <-
3755 (name, `string get, 1, Action (
3756 fun _ ->
3757 let source =
3758 let vals = [| "rgb"; "bgr"; "gray" |] in
3759 (object
3760 inherit lvsourcebase
3762 initializer
3763 m_active <- int_of_colorspace conf.colorspace;
3764 m_first <- 0;
3766 method getitemcount = Array.length vals
3767 method getitem n = (vals.(n), 0)
3768 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3769 ignore (uioh, first, pan, qsearch);
3770 if not cancel then set active;
3771 None
3772 method hasaction _ = true
3773 end)
3775 state.text <- "";
3776 let modehash = findkeyhash conf "info" in
3777 coe (new listview ~source ~trusted:true ~modehash)
3778 )) :: m_l
3780 method caption s offset =
3781 m_l <- (s, `empty, offset, Noaction) :: m_l
3783 method caption2 s f offset =
3784 m_l <- (s, `string f, offset, Noaction) :: m_l
3786 method getitemcount = Array.length m_a
3788 method getitem n =
3789 let tostr = function
3790 | `int f -> string_of_int (f ())
3791 | `intws f -> string_with_suffix_of_int (f ())
3792 | `string f -> f ()
3793 | `color f -> color_to_string (f ())
3794 | `bool (btos, f) -> btos (f ())
3795 | `empty -> ""
3797 let name, t, offset, _ = m_a.(n) in
3798 ((let s = tostr t in
3799 if String.length s > 0
3800 then Printf.sprintf "%s\t%s" name s
3801 else name),
3802 offset)
3804 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3805 let uiohopt =
3806 if not cancel
3807 then (
3808 m_qsearch <- qsearch;
3809 let uioh =
3810 match m_a.(active) with
3811 | _, _, _, Action f -> f uioh
3812 | _ -> uioh
3814 Some uioh
3816 else None
3818 m_active <- active;
3819 m_first <- first;
3820 m_pan <- pan;
3821 uiohopt
3823 method hasaction n =
3824 match m_a.(n) with
3825 | _, _, _, Action _ -> true
3826 | _ -> false
3827 end)
3829 let rec fillsrc prevmode prevuioh =
3830 let sep () = src#caption "" 0 in
3831 let colorp name get set =
3832 src#string name
3833 (fun () -> color_to_string (get ()))
3834 (fun v ->
3836 let c = color_of_string v in
3837 set c
3838 with exn ->
3839 state.text <- Printf.sprintf "bad color `%s': %s"
3840 v (Printexc.to_string exn);
3843 let oldmode = state.mode in
3844 let birdseye = isbirdseye state.mode in
3846 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3848 src#bool "presentation mode"
3849 (fun () -> conf.presentation)
3850 (fun v ->
3851 conf.presentation <- v;
3852 state.anchor <- getanchor ();
3853 represent ());
3855 src#bool "ignore case in searches"
3856 (fun () -> conf.icase)
3857 (fun v -> conf.icase <- v);
3859 src#bool "preload"
3860 (fun () -> conf.preload)
3861 (fun v -> conf.preload <- v);
3863 src#bool "highlight links"
3864 (fun () -> conf.hlinks)
3865 (fun v -> conf.hlinks <- v);
3867 src#bool "under info"
3868 (fun () -> conf.underinfo)
3869 (fun v -> conf.underinfo <- v);
3871 src#bool "persistent bookmarks"
3872 (fun () -> conf.savebmarks)
3873 (fun v -> conf.savebmarks <- v);
3875 src#bool "proportional display"
3876 (fun () -> conf.proportional)
3877 (fun v -> reqlayout conf.angle v);
3879 src#bool "trim margins"
3880 (fun () -> conf.trimmargins)
3881 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3883 src#bool "persistent location"
3884 (fun () -> conf.jumpback)
3885 (fun v -> conf.jumpback <- v);
3887 sep ();
3888 src#int "inter-page space"
3889 (fun () -> conf.interpagespace)
3890 (fun n ->
3891 conf.interpagespace <- n;
3892 let pageno, py =
3893 match state.layout with
3894 | [] -> 0, 0
3895 | l :: _ ->
3896 l.pageno, l.pagey
3898 state.maxy <- calcheight ();
3899 let y = getpagey pageno in
3900 gotoy (y + py)
3903 src#int "page bias"
3904 (fun () -> conf.pagebias)
3905 (fun v -> conf.pagebias <- v);
3907 src#int "scroll step"
3908 (fun () -> conf.scrollstep)
3909 (fun n -> conf.scrollstep <- n);
3911 src#int "auto scroll step"
3912 (fun () ->
3913 match state.autoscroll with
3914 | Some step -> step
3915 | _ -> conf.autoscrollstep)
3916 (fun n ->
3917 if state.autoscroll <> None
3918 then state.autoscroll <- Some n;
3919 conf.autoscrollstep <- n);
3921 src#int "zoom"
3922 (fun () -> truncate (conf.zoom *. 100.))
3923 (fun v -> setzoom ((float v) /. 100.));
3925 src#int "rotation"
3926 (fun () -> conf.angle)
3927 (fun v -> reqlayout v conf.proportional);
3929 src#int "scroll bar width"
3930 (fun () -> state.scrollw)
3931 (fun v ->
3932 state.scrollw <- v;
3933 conf.scrollbw <- v;
3934 reshape conf.winw conf.winh;
3937 src#int "scroll handle height"
3938 (fun () -> conf.scrollh)
3939 (fun v -> conf.scrollh <- v;);
3941 src#int "thumbnail width"
3942 (fun () -> conf.thumbw)
3943 (fun v ->
3944 conf.thumbw <- min 4096 v;
3945 match oldmode with
3946 | Birdseye beye ->
3947 leavebirdseye beye false;
3948 enterbirdseye ()
3949 | _ -> ()
3952 src#string "columns"
3953 (fun () ->
3954 match conf.columns with
3955 | None -> "1"
3956 | Some (multicol, _) -> columns_to_string multicol)
3957 (fun v ->
3958 let n, a, b = columns_of_string v in
3959 setcolumns n a b);
3961 sep ();
3962 src#caption "Presentation mode" 0;
3963 src#bool "scrollbar visible"
3964 (fun () -> conf.scrollbarinpm)
3965 (fun v ->
3966 if v != conf.scrollbarinpm
3967 then (
3968 conf.scrollbarinpm <- v;
3969 if conf.presentation
3970 then (
3971 state.scrollw <- if v then conf.scrollbw else 0;
3972 reshape conf.winw conf.winh;
3977 sep ();
3978 src#caption "Pixmap cache" 0;
3979 src#int_with_suffix "size (advisory)"
3980 (fun () -> conf.memlimit)
3981 (fun v -> conf.memlimit <- v);
3983 src#caption2 "used"
3984 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3985 (string_with_suffix_of_int state.memused)
3986 (Hashtbl.length state.tilemap)) 1;
3988 sep ();
3989 src#caption "Layout" 0;
3990 src#caption2 "Dimension"
3991 (fun () ->
3992 Printf.sprintf "%dx%d (virtual %dx%d)"
3993 conf.winw conf.winh
3994 state.w state.maxy)
3996 if conf.debug
3997 then
3998 src#caption2 "Position" (fun () ->
3999 Printf.sprintf "%dx%d" state.x state.y
4001 else
4002 src#caption2 "Visible" (fun () -> describe_location ()) 1
4005 sep ();
4006 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4007 "Save these parameters as global defaults at exit"
4008 (fun () -> conf.bedefault)
4009 (fun v -> conf.bedefault <- v)
4012 sep ();
4013 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4014 src#bool ~offset:0 ~btos "Extended parameters"
4015 (fun () -> !showextended)
4016 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4017 if !showextended
4018 then (
4019 src#bool "checkers"
4020 (fun () -> conf.checkers)
4021 (fun v -> conf.checkers <- v; setcheckers v);
4022 src#bool "update cursor"
4023 (fun () -> conf.updatecurs)
4024 (fun v -> conf.updatecurs <- v);
4025 src#bool "verbose"
4026 (fun () -> conf.verbose)
4027 (fun v -> conf.verbose <- v);
4028 src#bool "invert colors"
4029 (fun () -> conf.invert)
4030 (fun v -> conf.invert <- v);
4031 src#bool "max fit"
4032 (fun () -> conf.maxhfit)
4033 (fun v -> conf.maxhfit <- v);
4034 src#bool "redirect stderr"
4035 (fun () -> conf.redirectstderr)
4036 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4037 src#string "uri launcher"
4038 (fun () -> conf.urilauncher)
4039 (fun v -> conf.urilauncher <- v);
4040 src#string "path launcher"
4041 (fun () -> conf.pathlauncher)
4042 (fun v -> conf.pathlauncher <- v);
4043 src#string "tile size"
4044 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4045 (fun v ->
4047 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4048 conf.tileh <- max 64 w;
4049 conf.tilew <- max 64 h;
4050 flushtiles ();
4051 with exn ->
4052 state.text <- Printf.sprintf "bad tile size `%s': %s"
4053 v (Printexc.to_string exn));
4054 src#int "texture count"
4055 (fun () -> conf.texcount)
4056 (fun v ->
4057 if realloctexts v
4058 then conf.texcount <- v
4059 else showtext '!' " Failed to set texture count please retry later"
4061 src#int "slice height"
4062 (fun () -> conf.sliceheight)
4063 (fun v ->
4064 conf.sliceheight <- v;
4065 wcmd "sliceh %d" conf.sliceheight;
4067 src#int "anti-aliasing level"
4068 (fun () -> conf.aalevel)
4069 (fun v ->
4070 conf.aalevel <- bound v 0 8;
4071 state.anchor <- getanchor ();
4072 opendoc state.path state.password;
4074 src#int "ui font size"
4075 (fun () -> fstate.fontsize)
4076 (fun v -> setfontsize (bound v 5 100));
4077 colorp "background color"
4078 (fun () -> conf.bgcolor)
4079 (fun v -> conf.bgcolor <- v);
4080 src#bool "crop hack"
4081 (fun () -> conf.crophack)
4082 (fun v -> conf.crophack <- v);
4083 src#string "trim fuzz"
4084 (fun () -> irect_to_string conf.trimfuzz)
4085 (fun v ->
4087 conf.trimfuzz <- irect_of_string v;
4088 if conf.trimmargins
4089 then settrim true conf.trimfuzz;
4090 with exn ->
4091 state.text <- Printf.sprintf "bad irect `%s': %s"
4092 v (Printexc.to_string exn)
4094 src#string "throttle"
4095 (fun () ->
4096 match conf.maxwait with
4097 | None -> "show place holder if page is not ready"
4098 | Some time ->
4099 if time = infinity
4100 then "wait for page to fully render"
4101 else
4102 "wait " ^ string_of_float time
4103 ^ " seconds before showing placeholder"
4105 (fun v ->
4107 let f = float_of_string v in
4108 if f <= 0.0
4109 then conf.maxwait <- None
4110 else conf.maxwait <- Some f
4111 with exn ->
4112 state.text <- Printf.sprintf "bad time `%s': %s"
4113 v (Printexc.to_string exn)
4115 src#string "ghyll scroll"
4116 (fun () ->
4117 match conf.ghyllscroll with
4118 | None -> ""
4119 | Some nab -> ghyllscroll_to_string nab
4121 (fun v ->
4123 let gs =
4124 if String.length v = 0
4125 then None
4126 else Some (ghyllscroll_of_string v)
4128 conf.ghyllscroll <- gs
4129 with exn ->
4130 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4131 v (Printexc.to_string exn)
4133 src#string "selection command"
4134 (fun () -> conf.selcmd)
4135 (fun v -> conf.selcmd <- v);
4136 src#colorspace "color space"
4137 (fun () -> colorspace_to_string conf.colorspace)
4138 (fun v ->
4139 conf.colorspace <- colorspace_of_int v;
4140 wcmd "cs %d" v;
4141 load state.layout;
4145 sep ();
4146 src#caption "Document" 0;
4147 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4148 src#caption2 "Pages"
4149 (fun () -> string_of_int state.pagecount) 1;
4150 src#caption2 "Dimensions"
4151 (fun () -> string_of_int (List.length state.pdims)) 1;
4152 if conf.trimmargins
4153 then (
4154 sep ();
4155 src#caption "Trimmed margins" 0;
4156 src#caption2 "Dimensions"
4157 (fun () -> string_of_int (List.length state.pdims)) 1;
4160 src#reset prevmode prevuioh;
4162 fun () ->
4163 state.text <- "";
4164 let prevmode = state.mode
4165 and prevuioh = state.uioh in
4166 fillsrc prevmode prevuioh;
4167 let source = (src :> lvsource) in
4168 let modehash = findkeyhash conf "info" in
4169 state.uioh <- coe (object (self)
4170 inherit listview ~source ~trusted:true ~modehash as super
4171 val mutable m_prevmemused = 0
4172 method infochanged = function
4173 | Memused ->
4174 if m_prevmemused != state.memused
4175 then (
4176 m_prevmemused <- state.memused;
4177 G.postRedisplay "memusedchanged";
4179 | Pdim -> G.postRedisplay "pdimchanged"
4180 | Docinfo -> fillsrc prevmode prevuioh
4182 method key key mask =
4183 if not (Wsi.withctrl mask)
4184 then
4185 match key with
4186 | 0xff51 -> coe (self#updownlevel ~-1)
4187 | 0xff53 -> coe (self#updownlevel 1)
4188 | _ -> super#key key mask
4189 else super#key key mask
4190 end);
4191 G.postRedisplay "info";
4194 let enterhelpmode =
4195 let source =
4196 (object
4197 inherit lvsourcebase
4198 method getitemcount = Array.length state.help
4199 method getitem n =
4200 let s, n, _ = state.help.(n) in
4201 (s, n)
4203 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4204 let optuioh =
4205 if not cancel
4206 then (
4207 m_qsearch <- qsearch;
4208 match state.help.(active) with
4209 | _, _, Action f -> Some (f uioh)
4210 | _ -> Some (uioh)
4212 else None
4214 m_active <- active;
4215 m_first <- first;
4216 m_pan <- pan;
4217 optuioh
4219 method hasaction n =
4220 match state.help.(n) with
4221 | _, _, Action _ -> true
4222 | _ -> false
4224 initializer
4225 m_active <- -1
4226 end)
4227 in fun () ->
4228 let modehash = findkeyhash conf "help" in
4229 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4230 G.postRedisplay "help";
4233 let entermsgsmode =
4234 let msgsource =
4235 let re = Str.regexp "[\r\n]" in
4236 (object
4237 inherit lvsourcebase
4238 val mutable m_items = [||]
4240 method getitemcount = 1 + Array.length m_items
4242 method getitem n =
4243 if n = 0
4244 then "[Clear]", 0
4245 else m_items.(n-1), 0
4247 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4248 ignore uioh;
4249 if not cancel
4250 then (
4251 if active = 0
4252 then Buffer.clear state.errmsgs;
4253 m_qsearch <- qsearch;
4255 m_active <- active;
4256 m_first <- first;
4257 m_pan <- pan;
4258 None
4260 method hasaction n =
4261 n = 0
4263 method reset =
4264 state.newerrmsgs <- false;
4265 let l = Str.split re (Buffer.contents state.errmsgs) in
4266 m_items <- Array.of_list l
4268 initializer
4269 m_active <- 0
4270 end)
4271 in fun () ->
4272 state.text <- "";
4273 msgsource#reset;
4274 let source = (msgsource :> lvsource) in
4275 let modehash = findkeyhash conf "listview" in
4276 state.uioh <- coe (object
4277 inherit listview ~source ~trusted:false ~modehash as super
4278 method display =
4279 if state.newerrmsgs
4280 then msgsource#reset;
4281 super#display
4282 end);
4283 G.postRedisplay "msgs";
4286 let quickbookmark ?title () =
4287 match state.layout with
4288 | [] -> ()
4289 | l :: _ ->
4290 let title =
4291 match title with
4292 | None ->
4293 let sec = Unix.gettimeofday () in
4294 let tm = Unix.localtime sec in
4295 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4296 (l.pageno+1)
4297 tm.Unix.tm_mday
4298 tm.Unix.tm_mon
4299 (tm.Unix.tm_year + 1900)
4300 tm.Unix.tm_hour
4301 tm.Unix.tm_min
4302 | Some title -> title
4304 state.bookmarks <-
4305 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4306 :: state.bookmarks
4309 let doreshape w h =
4310 state.fullscreen <- None;
4311 Wsi.reshape w h;
4314 let setautoscrollspeed step goingdown =
4315 let incr = max 1 ((abs step) / 2) in
4316 let incr = if goingdown then incr else -incr in
4317 let astep = step + incr in
4318 state.autoscroll <- Some astep;
4321 let viewkeyboard key mask =
4322 let enttext te =
4323 let mode = state.mode in
4324 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4325 state.text <- "";
4326 enttext ();
4327 G.postRedisplay "view:enttext"
4329 let ctrl = Wsi.withctrl mask in
4330 match key with
4331 | 81 -> (* Q *)
4332 exit 0
4334 | 0xff63 -> (* insert *)
4335 if conf.angle mod 360 = 0
4336 then (
4337 state.mode <- LinkNav (Ltgendir 0);
4338 gotoy state.y;
4340 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4342 | 0xff1b | 113 -> (* escape / q *)
4343 begin match state.mstate with
4344 | Mzoomrect _ ->
4345 state.mstate <- Mnone;
4346 Wsi.setcursor Wsi.CURSOR_INHERIT;
4347 G.postRedisplay "kill zoom rect";
4348 | _ ->
4349 match state.ranchors with
4350 | [] -> raise Quit
4351 | (path, password, anchor) :: rest ->
4352 state.ranchors <- rest;
4353 state.anchor <- anchor;
4354 opendoc path password
4355 end;
4357 | 0xff08 -> (* backspace *)
4358 let y = getnav ~-1 in
4359 gotoy_and_clear_text y
4361 | 111 -> (* o *)
4362 enteroutlinemode ()
4364 | 117 -> (* u *)
4365 state.rects <- [];
4366 state.text <- "";
4367 G.postRedisplay "dehighlight";
4369 | 47 | 63 -> (* / ? *)
4370 let ondone isforw s =
4371 cbput state.hists.pat s;
4372 state.searchpattern <- s;
4373 search s isforw
4375 let s = String.create 1 in
4376 s.[0] <- Char.chr key;
4377 enttext (s, "", Some (onhist state.hists.pat),
4378 textentry, ondone (key = 47))
4380 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4381 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4382 setzoom (conf.zoom +. incr)
4384 | 43 | 0xffab -> (* + *)
4385 let ondone s =
4386 let n =
4387 try int_of_string s with exc ->
4388 state.text <- Printf.sprintf "bad integer `%s': %s"
4389 s (Printexc.to_string exc);
4390 max_int
4392 if n != max_int
4393 then (
4394 conf.pagebias <- n;
4395 state.text <- "page bias is now " ^ string_of_int n;
4398 enttext ("page bias: ", "", None, intentry, ondone)
4400 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4401 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4402 setzoom (max 0.01 (conf.zoom -. decr))
4404 | 45 | 0xffad -> (* - *)
4405 let ondone msg = state.text <- msg in
4406 enttext (
4407 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4408 optentry state.mode, ondone
4411 | 48 when ctrl -> (* ctrl-0 *)
4412 setzoom 1.0
4414 | 49 when ctrl -> (* 1 *)
4415 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4416 if zoom < 1.0
4417 then setzoom zoom
4419 | 0xffc6 -> (* f9 *)
4420 togglebirdseye ()
4422 | 57 when ctrl -> (* ctrl-9 *)
4423 togglebirdseye ()
4425 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4426 when not ctrl -> (* 0..9 *)
4427 let ondone s =
4428 let n =
4429 try int_of_string s with exc ->
4430 state.text <- Printf.sprintf "bad integer `%s': %s"
4431 s (Printexc.to_string exc);
4434 if n >= 0
4435 then (
4436 addnav ();
4437 cbput state.hists.pag (string_of_int n);
4438 gotopage1 (n + conf.pagebias - 1) 0;
4441 let pageentry text key =
4442 match Char.unsafe_chr key with
4443 | 'g' -> TEdone text
4444 | _ -> intentry text key
4446 let text = "x" in text.[0] <- Char.chr key;
4447 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4449 | 98 -> (* b *)
4450 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4451 reshape conf.winw conf.winh;
4453 | 108 -> (* l *)
4454 conf.hlinks <- not conf.hlinks;
4455 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4456 G.postRedisplay "toggle highlightlinks";
4458 | 97 -> (* a *)
4459 begin match state.autoscroll with
4460 | Some step ->
4461 conf.autoscrollstep <- step;
4462 state.autoscroll <- None
4463 | None ->
4464 if conf.autoscrollstep = 0
4465 then state.autoscroll <- Some 1
4466 else state.autoscroll <- Some conf.autoscrollstep
4469 | 80 -> (* P *)
4470 conf.presentation <- not conf.presentation;
4471 if conf.presentation
4472 then (
4473 if not conf.scrollbarinpm
4474 then state.scrollw <- 0;
4476 else
4477 state.scrollw <- conf.scrollbw;
4479 showtext ' ' ("presentation mode " ^
4480 if conf.presentation then "on" else "off");
4481 state.anchor <- getanchor ();
4482 represent ()
4484 | 102 -> (* f *)
4485 begin match state.fullscreen with
4486 | None ->
4487 state.fullscreen <- Some (conf.winw, conf.winh);
4488 Wsi.fullscreen ()
4489 | Some (w, h) ->
4490 state.fullscreen <- None;
4491 doreshape w h
4494 | 103 -> (* g *)
4495 gotoy_and_clear_text 0
4497 | 71 -> (* G *)
4498 gotopage1 (state.pagecount - 1) 0
4500 | 112 | 78 -> (* p|N *)
4501 search state.searchpattern false
4503 | 110 | 0xffc0 -> (* n|F3 *)
4504 search state.searchpattern true
4506 | 116 -> (* t *)
4507 begin match state.layout with
4508 | [] -> ()
4509 | l :: _ ->
4510 gotoy_and_clear_text (getpagey l.pageno)
4513 | 32 -> (* ' ' *)
4514 begin match List.rev state.layout with
4515 | [] -> ()
4516 | l :: _ ->
4517 let pageno = min (l.pageno+1) (state.pagecount-1) in
4518 gotoy_and_clear_text (getpagey pageno)
4521 | 0xff9f | 0xffff -> (* delete *)
4522 begin match state.layout with
4523 | [] -> ()
4524 | l :: _ ->
4525 let pageno = max 0 (l.pageno-1) in
4526 gotoy_and_clear_text (getpagey pageno)
4529 | 61 -> (* = *)
4530 showtext ' ' (describe_location ());
4532 | 119 -> (* w *)
4533 begin match state.layout with
4534 | [] -> ()
4535 | l :: _ ->
4536 doreshape (l.pagew + state.scrollw) l.pageh;
4537 G.postRedisplay "w"
4540 | 39 -> (* ' *)
4541 enterbookmarkmode ()
4543 | 104 | 0xffbe -> (* h|F1 *)
4544 enterhelpmode ()
4546 | 105 -> (* i *)
4547 enterinfomode ()
4549 | 101 when conf.redirectstderr -> (* e *)
4550 entermsgsmode ()
4552 | 109 -> (* m *)
4553 let ondone s =
4554 match state.layout with
4555 | l :: _ ->
4556 state.bookmarks <-
4557 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4558 :: state.bookmarks
4559 | _ -> ()
4561 enttext ("bookmark: ", "", None, textentry, ondone)
4563 | 126 -> (* ~ *)
4564 quickbookmark ();
4565 showtext ' ' "Quick bookmark added";
4567 | 122 -> (* z *)
4568 begin match state.layout with
4569 | l :: _ ->
4570 let rect = getpdimrect l.pagedimno in
4571 let w, h =
4572 if conf.crophack
4573 then
4574 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4575 truncate (1.2 *. (rect.(3) -. rect.(0))))
4576 else
4577 (truncate (rect.(1) -. rect.(0)),
4578 truncate (rect.(3) -. rect.(0)))
4580 let w = truncate ((float w)*.conf.zoom)
4581 and h = truncate ((float h)*.conf.zoom) in
4582 if w != 0 && h != 0
4583 then (
4584 state.anchor <- getanchor ();
4585 doreshape (w + state.scrollw) (h + conf.interpagespace)
4587 G.postRedisplay "z";
4589 | [] -> ()
4592 | 50 when ctrl -> (* ctrl-2 *)
4593 let maxw = getmaxw () in
4594 if maxw > 0.0
4595 then setzoom (maxw /. float conf.winw)
4597 | 60 | 62 -> (* < > *)
4598 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4600 | 91 | 93 -> (* [ ] *)
4601 conf.colorscale <-
4602 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4604 G.postRedisplay "brightness";
4606 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4607 setzoom state.prevzoom
4609 | 107 | 0xff52 -> (* k up *)
4610 begin match state.autoscroll with
4611 | None ->
4612 begin match state.mode with
4613 | Birdseye beye -> upbirdseye 1 beye
4614 | _ ->
4615 if ctrl
4616 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4617 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4619 | Some n ->
4620 setautoscrollspeed n false
4623 | 106 | 0xff54 -> (* j down *)
4624 begin match state.autoscroll with
4625 | None ->
4626 begin match state.mode with
4627 | Birdseye beye -> downbirdseye 1 beye
4628 | _ ->
4629 if ctrl
4630 then gotoy_and_clear_text (clamp (conf.winh/2))
4631 else gotoy_and_clear_text (clamp conf.scrollstep)
4633 | Some n ->
4634 setautoscrollspeed n true
4637 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4638 if conf.zoom > 1.0
4639 then
4640 let dx =
4641 if ctrl
4642 then conf.winw / 2
4643 else 10
4645 let dx = if key = 0xff51 then dx else -dx in
4646 state.x <- state.x + dx;
4647 gotoy_and_clear_text state.y
4648 else (
4649 state.text <- "";
4650 G.postRedisplay "lef/right"
4653 | 0xff55 -> (* prior *)
4654 let y =
4655 if ctrl
4656 then
4657 match state.layout with
4658 | [] -> state.y
4659 | l :: _ -> state.y - l.pagey
4660 else
4661 clamp (-conf.winh)
4663 gotoghyll y
4665 | 0xff56 -> (* next *)
4666 let y =
4667 if ctrl
4668 then
4669 match List.rev state.layout with
4670 | [] -> state.y
4671 | l :: _ -> getpagey l.pageno
4672 else
4673 clamp conf.winh
4675 gotoghyll y
4677 | 0xff50 -> gotoghyll 0
4678 | 0xff57 -> gotoghyll (clamp state.maxy)
4679 | 0xff53 when Wsi.withalt mask ->
4680 gotoghyll (getnav ~-1)
4681 | 0xff51 when Wsi.withalt mask ->
4682 gotoghyll (getnav 1)
4684 | 114 -> (* r *)
4685 state.anchor <- getanchor ();
4686 opendoc state.path state.password
4688 | 76 -> (* L *)
4689 launchpath ()
4691 | 118 when conf.debug -> (* v *)
4692 state.rects <- [];
4693 List.iter (fun l ->
4694 match getopaque l.pageno with
4695 | None -> ()
4696 | Some opaque ->
4697 let x0, y0, x1, y1 = pagebbox opaque in
4698 let a,b = float x0, float y0 in
4699 let c,d = float x1, float y0 in
4700 let e,f = float x1, float y1 in
4701 let h,j = float x0, float y1 in
4702 let rect = (a,b,c,d,e,f,h,j) in
4703 debugrect rect;
4704 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4705 ) state.layout;
4706 G.postRedisplay "v";
4708 | _ ->
4709 vlog "huh? %s" (Wsi.keyname key)
4712 let gotounder = function
4713 | Ulinkgoto (pageno, top) ->
4714 if pageno >= 0
4715 then (
4716 addnav ();
4717 gotopage1 pageno top;
4720 | Ulinkuri s ->
4721 gotouri s
4723 | Uremote (filename, pageno) ->
4724 let path =
4725 if Sys.file_exists filename
4726 then filename
4727 else
4728 let dir = Filename.dirname state.path in
4729 let path = Filename.concat dir filename in
4730 if Sys.file_exists path
4731 then path
4732 else ""
4734 if String.length path > 0
4735 then (
4736 let anchor = getanchor () in
4737 let ranchor = state.path, state.password, anchor in
4738 state.anchor <- (pageno, 0.0);
4739 state.ranchors <- ranchor :: state.ranchors;
4740 opendoc path "";
4742 else showtext '!' ("Could not find " ^ filename)
4744 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4747 let linknavkeyboard key mask linknav =
4748 let getpage pageno =
4749 let rec loop = function
4750 | [] -> None
4751 | l :: _ when l.pageno = pageno -> Some l
4752 | _ :: rest -> loop rest
4753 in loop state.layout
4755 let doexact (pageno, n) =
4756 match getopaque pageno, getpage pageno with
4757 | Some opaque, Some l ->
4758 if key = 0xff0d
4759 then
4760 let under = getlink opaque n in
4761 G.postRedisplay "link gotounder";
4762 gotounder under;
4763 state.mode <- View;
4764 else
4765 let opt, dir =
4766 match key with
4767 | 0xff50 -> (* home *)
4768 Some (findlink opaque LDfirst), -1
4770 | 0xff57 -> (* end *)
4771 Some (findlink opaque LDlast), 1
4773 | 0xff51 -> (* left *)
4774 Some (findlink opaque (LDleft n)), -1
4776 | 0xff53 -> (* right *)
4777 Some (findlink opaque (LDright n)), 1
4779 | 0xff52 -> (* up *)
4780 Some (findlink opaque (LDup n)), -1
4782 | 0xff54 -> (* down *)
4783 Some (findlink opaque (LDdown n)), 1
4785 | _ -> None, 0
4787 let pwl l dir =
4788 begin match findpwl l.pageno dir with
4789 | Pwlnotfound -> ()
4790 | Pwl pageno ->
4791 let notfound dir =
4792 state.mode <- LinkNav (Ltgendir dir);
4793 let y, h = getpageyh pageno in
4794 let y =
4795 if dir < 0
4796 then y + h - conf.winh
4797 else y
4799 gotoy y
4801 begin match getopaque pageno, getpage pageno with
4802 | Some opaque, Some _ ->
4803 let link =
4804 let ld = if dir > 0 then LDfirst else LDlast in
4805 findlink opaque ld
4807 begin match link with
4808 | Lfound m ->
4809 showlinktype (getlink opaque m);
4810 state.mode <- LinkNav (Ltexact (pageno, m));
4811 G.postRedisplay "linknav jpage";
4812 | _ -> notfound dir
4813 end;
4814 | _ -> notfound dir
4815 end;
4816 end;
4818 begin match opt with
4819 | Some Lnotfound -> pwl l dir;
4820 | Some (Lfound m) ->
4821 if m = n
4822 then pwl l dir
4823 else (
4824 let _, y0, _, y1 = getlinkrect opaque m in
4825 if y0 < l.pagey
4826 then gotopage1 l.pageno y0
4827 else (
4828 let d = fstate.fontsize + 1 in
4829 if y1 - l.pagey > l.pagevh - d
4830 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
4831 else G.postRedisplay "linknav";
4833 showlinktype (getlink opaque m);
4834 state.mode <- LinkNav (Ltexact (l.pageno, m));
4837 | None -> viewkeyboard key mask
4838 end;
4839 | _ -> viewkeyboard key mask
4841 if key = 0xff63
4842 then (
4843 state.mode <- View;
4844 G.postRedisplay "leave linknav"
4846 else
4847 match linknav with
4848 | Ltgendir _ -> viewkeyboard key mask
4849 | Ltexact exact -> doexact exact
4852 let keyboard key mask =
4853 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4854 then wcmd "interrupt"
4855 else state.uioh <- state.uioh#key key mask
4858 let birdseyekeyboard key mask
4859 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4860 let incr =
4861 match conf.columns with
4862 | None -> 1
4863 | Some ((c, _, _), _) -> c
4865 match key with
4866 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4867 let y, h = getpageyh pageno in
4868 let top = (conf.winh - h) / 2 in
4869 gotoy (max 0 (y - top))
4870 | 0xff0d -> leavebirdseye beye false
4871 | 0xff1b -> leavebirdseye beye true (* escape *)
4872 | 0xff52 -> upbirdseye incr beye (* prior *)
4873 | 0xff54 -> downbirdseye incr beye (* next *)
4874 | 0xff51 -> upbirdseye 1 beye (* up *)
4875 | 0xff53 -> downbirdseye 1 beye (* down *)
4877 | 0xff55 ->
4878 begin match state.layout with
4879 | l :: _ ->
4880 if l.pagey != 0
4881 then (
4882 state.mode <- Birdseye (
4883 oconf, leftx, l.pageno, hooverpageno, anchor
4885 gotopage1 l.pageno 0;
4887 else (
4888 let layout = layout (state.y-conf.winh) conf.winh in
4889 match layout with
4890 | [] -> gotoy (clamp (-conf.winh))
4891 | l :: _ ->
4892 state.mode <- Birdseye (
4893 oconf, leftx, l.pageno, hooverpageno, anchor
4895 gotopage1 l.pageno 0
4898 | [] -> gotoy (clamp (-conf.winh))
4899 end;
4901 | 0xff56 ->
4902 begin match List.rev state.layout with
4903 | l :: _ ->
4904 let layout = layout (state.y + conf.winh) conf.winh in
4905 begin match layout with
4906 | [] ->
4907 let incr = l.pageh - l.pagevh in
4908 if incr = 0
4909 then (
4910 state.mode <-
4911 Birdseye (
4912 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4914 G.postRedisplay "birdseye pagedown";
4916 else gotoy (clamp (incr + conf.interpagespace*2));
4918 | l :: _ ->
4919 state.mode <-
4920 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4921 gotopage1 l.pageno 0;
4924 | [] -> gotoy (clamp conf.winh)
4925 end;
4927 | 0xff50 ->
4928 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4929 gotopage1 0 0
4931 | 0xff57 ->
4932 let pageno = state.pagecount - 1 in
4933 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4934 if not (pagevisible state.layout pageno)
4935 then
4936 let h =
4937 match List.rev state.pdims with
4938 | [] -> conf.winh
4939 | (_, _, h, _) :: _ -> h
4941 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4942 else G.postRedisplay "birdseye end";
4943 | _ -> viewkeyboard key mask
4946 let drawpage l =
4947 let color =
4948 match state.mode with
4949 | Textentry _ -> scalecolor 0.4
4950 | LinkNav _
4951 | View -> scalecolor 1.0
4952 | Birdseye (_, _, pageno, hooverpageno, _) ->
4953 if l.pageno = hooverpageno
4954 then scalecolor 0.9
4955 else (
4956 if l.pageno = pageno
4957 then scalecolor 1.0
4958 else scalecolor 0.8
4961 drawtiles l color;
4962 begin match getopaque l.pageno with
4963 | Some opaque ->
4964 if tileready l l.pagex l.pagey
4965 then
4966 let x = l.pagedispx - l.pagex
4967 and y = l.pagedispy - l.pagey in
4968 postprocess opaque conf.hlinks x y;
4970 | _ -> ()
4971 end;
4974 let scrollindicator () =
4975 let sbw, ph, sh = state.uioh#scrollph in
4976 let sbh, pw, sw = state.uioh#scrollpw in
4978 GlDraw.color (0.64, 0.64, 0.64);
4979 GlDraw.rect
4980 (float (conf.winw - sbw), 0.)
4981 (float conf.winw, float conf.winh)
4983 GlDraw.rect
4984 (0., float (conf.winh - sbh))
4985 (float (conf.winw - state.scrollw - 1), float conf.winh)
4987 GlDraw.color (0.0, 0.0, 0.0);
4989 GlDraw.rect
4990 (float (conf.winw - sbw), ph)
4991 (float conf.winw, ph +. sh)
4993 GlDraw.rect
4994 (pw, float (conf.winh - sbh))
4995 (pw +. sw, float conf.winh)
4999 let showsel () =
5000 match state.mstate with
5001 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5004 | Msel ((x0, y0), (x1, y1)) ->
5005 let rec loop = function
5006 | l :: ls ->
5007 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5008 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5009 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5010 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5011 then
5012 match getopaque l.pageno with
5013 | Some opaque ->
5014 let x0, y0 = pagetranslatepoint l x0 y0 in
5015 let x1, y1 = pagetranslatepoint l x1 y1 in
5016 seltext opaque (x0, y0, x1, y1);
5017 | _ -> ()
5018 else loop ls
5019 | [] -> ()
5021 loop state.layout
5024 let showrects rects =
5025 Gl.enable `blend;
5026 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5027 GlDraw.polygon_mode `both `fill;
5028 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5029 List.iter
5030 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5031 List.iter (fun l ->
5032 if l.pageno = pageno
5033 then (
5034 let dx = float (l.pagedispx - l.pagex) in
5035 let dy = float (l.pagedispy - l.pagey) in
5036 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5037 GlDraw.begins `quads;
5039 GlDraw.vertex2 (x0+.dx, y0+.dy);
5040 GlDraw.vertex2 (x1+.dx, y1+.dy);
5041 GlDraw.vertex2 (x2+.dx, y2+.dy);
5042 GlDraw.vertex2 (x3+.dx, y3+.dy);
5044 GlDraw.ends ();
5046 ) state.layout
5047 ) rects
5049 Gl.disable `blend;
5052 let display () =
5053 GlClear.color (scalecolor2 conf.bgcolor);
5054 GlClear.clear [`color];
5055 List.iter drawpage state.layout;
5056 let rects =
5057 match state.mode with
5058 | LinkNav (Ltexact (pageno, linkno)) ->
5059 begin match getopaque pageno with
5060 | Some opaque ->
5061 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5062 (pageno, 5, (
5063 float x0, float y0,
5064 float x1, float y0,
5065 float x1, float y1,
5066 float x0, float y1)
5067 ) :: state.rects
5068 | None -> state.rects
5070 | _ -> state.rects
5072 showrects rects;
5073 showsel ();
5074 state.uioh#display;
5075 begin match state.mstate with
5076 | Mzoomrect ((x0, y0), (x1, y1)) ->
5077 Gl.enable `blend;
5078 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5079 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5080 GlDraw.rect (float x0, float y0)
5081 (float x1, float y1);
5082 Gl.disable `blend;
5083 | _ -> ()
5084 end;
5085 enttext ();
5086 scrollindicator ();
5087 Wsi.swapb ();
5090 let zoomrect x y x1 y1 =
5091 let x0 = min x x1
5092 and x1 = max x x1
5093 and y0 = min y y1 in
5094 gotoy (state.y + y0);
5095 state.anchor <- getanchor ();
5096 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5097 let margin =
5098 if state.w < conf.winw - state.scrollw
5099 then (conf.winw - state.scrollw - state.w) / 2
5100 else 0
5102 state.x <- (state.x + margin) - x0;
5103 setzoom zoom;
5104 Wsi.setcursor Wsi.CURSOR_INHERIT;
5105 state.mstate <- Mnone;
5108 let scrollx x =
5109 let winw = conf.winw - state.scrollw - 1 in
5110 let s = float x /. float winw in
5111 let destx = truncate (float (state.w + winw) *. s) in
5112 state.x <- winw - destx;
5113 gotoy_and_clear_text state.y;
5114 state.mstate <- Mscrollx;
5117 let scrolly y =
5118 let s = float y /. float conf.winh in
5119 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5120 gotoy_and_clear_text desty;
5121 state.mstate <- Mscrolly;
5124 let viewmouse button down x y mask =
5125 match button with
5126 | n when (n == 4 || n == 5) && not down ->
5127 if Wsi.withctrl mask
5128 then (
5129 match state.mstate with
5130 | Mzoom (oldn, i) ->
5131 if oldn = n
5132 then (
5133 if i = 2
5134 then
5135 let incr =
5136 match n with
5137 | 5 ->
5138 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5139 | _ ->
5140 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5142 let zoom = conf.zoom -. incr in
5143 setzoom zoom;
5144 state.mstate <- Mzoom (n, 0);
5145 else
5146 state.mstate <- Mzoom (n, i+1);
5148 else state.mstate <- Mzoom (n, 0)
5150 | _ -> state.mstate <- Mzoom (n, 0)
5152 else (
5153 match state.autoscroll with
5154 | Some step -> setautoscrollspeed step (n=4)
5155 | None ->
5156 let incr =
5157 if n = 4
5158 then -conf.scrollstep
5159 else conf.scrollstep
5161 let incr = incr * 2 in
5162 let y = clamp incr in
5163 gotoy_and_clear_text y
5166 | 1 when Wsi.withctrl mask ->
5167 if down
5168 then (
5169 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5170 state.mstate <- Mpan (x, y)
5172 else
5173 state.mstate <- Mnone
5175 | 3 ->
5176 if down
5177 then (
5178 Wsi.setcursor Wsi.CURSOR_CYCLE;
5179 let p = (x, y) in
5180 state.mstate <- Mzoomrect (p, p)
5182 else (
5183 match state.mstate with
5184 | Mzoomrect ((x0, y0), _) ->
5185 if abs (x-x0) > 10 && abs (y - y0) > 10
5186 then zoomrect x0 y0 x y
5187 else (
5188 state.mstate <- Mnone;
5189 Wsi.setcursor Wsi.CURSOR_INHERIT;
5190 G.postRedisplay "kill accidental zoom rect";
5192 | _ ->
5193 Wsi.setcursor Wsi.CURSOR_INHERIT;
5194 state.mstate <- Mnone
5197 | 1 when x > conf.winw - state.scrollw ->
5198 if down
5199 then
5200 let _, position, sh = state.uioh#scrollph in
5201 if y > truncate position && y < truncate (position +. sh)
5202 then state.mstate <- Mscrolly
5203 else scrolly y
5204 else
5205 state.mstate <- Mnone
5207 | 1 when y > conf.winh - state.hscrollh ->
5208 if down
5209 then
5210 let _, position, sw = state.uioh#scrollpw in
5211 if x > truncate position && x < truncate (position +. sw)
5212 then state.mstate <- Mscrollx
5213 else scrollx x
5214 else
5215 state.mstate <- Mnone
5217 | 1 ->
5218 let dest = if down then getunder x y else Unone in
5219 begin match dest with
5220 | Ulinkgoto (pageno, top) ->
5221 if pageno >= 0
5222 then (
5223 addnav ();
5224 gotopage1 pageno top;
5227 | Ulinkuri s ->
5228 gotouri s
5230 | Uremote (filename, pageno) ->
5231 let path =
5232 if Sys.file_exists filename
5233 then filename
5234 else
5235 let dir = Filename.dirname state.path in
5236 let path = Filename.concat dir filename in
5237 if Sys.file_exists path
5238 then path
5239 else ""
5241 if String.length path > 0
5242 then (
5243 let anchor = getanchor () in
5244 let ranchor = state.path, state.password, anchor in
5245 state.anchor <- (pageno, 0.0);
5246 state.ranchors <- ranchor :: state.ranchors;
5247 opendoc path "";
5249 else showtext '!' ("Could not find " ^ filename)
5251 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
5253 | Unone when down ->
5254 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5255 state.mstate <- Mpan (x, y);
5257 | Unone | Utext _ ->
5258 if down
5259 then (
5260 if conf.angle mod 360 = 0
5261 then (
5262 state.mstate <- Msel ((x, y), (x, y));
5263 G.postRedisplay "mouse select";
5266 else (
5267 match state.mstate with
5268 | Mnone -> ()
5270 | Mzoom _ | Mscrollx | Mscrolly ->
5271 state.mstate <- Mnone
5273 | Mzoomrect ((x0, y0), _) ->
5274 zoomrect x0 y0 x y
5276 | Mpan _ ->
5277 Wsi.setcursor Wsi.CURSOR_INHERIT;
5278 state.mstate <- Mnone
5280 | Msel ((_, y0), (_, y1)) ->
5281 let rec loop = function
5282 | [] -> ()
5283 | l :: rest ->
5284 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5285 || ((y1 >= l.pagedispy
5286 && y1 <= (l.pagedispy + l.pagevh)))
5287 then
5288 match getopaque l.pageno with
5289 | Some opaque ->
5290 copysel conf.selcmd opaque;
5291 G.postRedisplay "copysel"
5292 | _ -> ()
5293 else loop rest
5295 loop state.layout;
5296 Wsi.setcursor Wsi.CURSOR_INHERIT;
5297 state.mstate <- Mnone;
5301 | _ -> ()
5304 let birdseyemouse button down x y mask
5305 (conf, leftx, _, hooverpageno, anchor) =
5306 match button with
5307 | 1 when down ->
5308 let rec loop = function
5309 | [] -> ()
5310 | l :: rest ->
5311 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5312 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5313 then (
5314 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5316 else loop rest
5318 loop state.layout
5319 | 3 -> ()
5320 | _ -> viewmouse button down x y mask
5323 let mouse button down x y mask =
5324 state.uioh <- state.uioh#button button down x y mask;
5327 let motion ~x ~y =
5328 state.uioh <- state.uioh#motion x y
5331 let pmotion ~x ~y =
5332 state.uioh <- state.uioh#pmotion x y;
5335 let uioh = object
5336 method display = ()
5338 method key key mask =
5339 begin match state.mode with
5340 | Textentry textentry -> textentrykeyboard key mask textentry
5341 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5342 | View -> viewkeyboard key mask
5343 | LinkNav linknav -> linknavkeyboard key mask linknav
5344 end;
5345 state.uioh
5347 method button button bstate x y mask =
5348 begin match state.mode with
5349 | LinkNav _
5350 | View -> viewmouse button bstate x y mask
5351 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5352 | Textentry _ -> ()
5353 end;
5354 state.uioh
5356 method motion x y =
5357 begin match state.mode with
5358 | Textentry _ -> ()
5359 | View | Birdseye _ | LinkNav _ ->
5360 match state.mstate with
5361 | Mzoom _ | Mnone -> ()
5363 | Mpan (x0, y0) ->
5364 let dx = x - x0
5365 and dy = y0 - y in
5366 state.mstate <- Mpan (x, y);
5367 if conf.zoom > 1.0 then state.x <- state.x + dx;
5368 let y = clamp dy in
5369 gotoy_and_clear_text y
5371 | Msel (a, _) ->
5372 state.mstate <- Msel (a, (x, y));
5373 G.postRedisplay "motion select";
5375 | Mscrolly ->
5376 let y = min conf.winh (max 0 y) in
5377 scrolly y
5379 | Mscrollx ->
5380 let x = min conf.winw (max 0 x) in
5381 scrollx x
5383 | Mzoomrect (p0, _) ->
5384 state.mstate <- Mzoomrect (p0, (x, y));
5385 G.postRedisplay "motion zoomrect";
5386 end;
5387 state.uioh
5389 method pmotion x y =
5390 begin match state.mode with
5391 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5392 let rec loop = function
5393 | [] ->
5394 if hooverpageno != -1
5395 then (
5396 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5397 G.postRedisplay "pmotion birdseye no hoover";
5399 | l :: rest ->
5400 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5401 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5402 then (
5403 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5404 G.postRedisplay "pmotion birdseye hoover";
5406 else loop rest
5408 loop state.layout
5410 | Textentry _ -> ()
5412 | LinkNav _
5413 | View ->
5414 match state.mstate with
5415 | Mnone -> updateunder x y
5416 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5418 end;
5419 state.uioh
5421 method infochanged _ = ()
5423 method scrollph =
5424 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5425 let p, h = scrollph state.y maxy in
5426 state.scrollw, p, h
5428 method scrollpw =
5429 let winw = conf.winw - state.scrollw - 1 in
5430 let fwinw = float winw in
5431 let sw =
5432 let sw = fwinw /. float state.w in
5433 let sw = fwinw *. sw in
5434 max sw (float conf.scrollh)
5436 let position, sw =
5437 let f = state.w+winw in
5438 let r = float (winw-state.x) /. float f in
5439 let p = fwinw *. r in
5440 p-.sw/.2., sw
5442 let sw =
5443 if position +. sw > fwinw
5444 then fwinw -. position
5445 else sw
5447 state.hscrollh, position, sw
5449 method modehash =
5450 let modename =
5451 match state.mode with
5452 | LinkNav _ -> "links"
5453 | Textentry _ -> "textentry"
5454 | Birdseye _ -> "birdseye"
5455 | View -> "global"
5457 findkeyhash conf modename
5458 end;;
5460 module Config =
5461 struct
5462 open Parser
5464 let fontpath = ref "";;
5466 module KeyMap =
5467 Map.Make (struct type t = (int * int) let compare = compare end);;
5469 let unent s =
5470 let l = String.length s in
5471 let b = Buffer.create l in
5472 unent b s 0 l;
5473 Buffer.contents b;
5476 let home =
5477 try Sys.getenv "HOME"
5478 with exn ->
5479 prerr_endline
5480 ("Can not determine home directory location: " ^
5481 Printexc.to_string exn);
5485 let modifier_of_string = function
5486 | "alt" -> Wsi.altmask
5487 | "shift" -> Wsi.shiftmask
5488 | "ctrl" | "control" -> Wsi.ctrlmask
5489 | "meta" -> Wsi.metamask
5490 | _ -> 0
5493 let key_of_string =
5494 let r = Str.regexp "-" in
5495 fun s ->
5496 let elems = Str.full_split r s in
5497 let f n k m =
5498 let g s =
5499 let m1 = modifier_of_string s in
5500 if m1 = 0
5501 then (Wsi.namekey s, m)
5502 else (k, m lor m1)
5503 in function
5504 | Str.Delim s when n land 1 = 0 -> g s
5505 | Str.Text s -> g s
5506 | Str.Delim _ -> (k, m)
5508 let rec loop n k m = function
5509 | [] -> (k, m)
5510 | x :: xs ->
5511 let k, m = f n k m x in
5512 loop (n+1) k m xs
5514 loop 0 0 0 elems
5517 let keys_of_string =
5518 let r = Str.regexp "[ \t]" in
5519 fun s ->
5520 let elems = Str.split r s in
5521 List.map key_of_string elems
5524 let copykeyhashes c =
5525 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5528 let config_of c attrs =
5529 let apply c k v =
5531 match k with
5532 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5533 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5534 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5535 | "preload" -> { c with preload = bool_of_string v }
5536 | "page-bias" -> { c with pagebias = int_of_string v }
5537 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5538 | "auto-scroll-step" ->
5539 { c with autoscrollstep = max 0 (int_of_string v) }
5540 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5541 | "crop-hack" -> { c with crophack = bool_of_string v }
5542 | "throttle" ->
5543 let mw =
5544 match String.lowercase v with
5545 | "true" -> Some infinity
5546 | "false" -> None
5547 | f -> Some (float_of_string f)
5549 { c with maxwait = mw}
5550 | "highlight-links" -> { c with hlinks = bool_of_string v }
5551 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5552 | "vertical-margin" ->
5553 { c with interpagespace = max 0 (int_of_string v) }
5554 | "zoom" ->
5555 let zoom = float_of_string v /. 100. in
5556 let zoom = max zoom 0.0 in
5557 { c with zoom = zoom }
5558 | "presentation" -> { c with presentation = bool_of_string v }
5559 | "rotation-angle" -> { c with angle = int_of_string v }
5560 | "width" -> { c with winw = max 20 (int_of_string v) }
5561 | "height" -> { c with winh = max 20 (int_of_string v) }
5562 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5563 | "proportional-display" -> { c with proportional = bool_of_string v }
5564 | "pixmap-cache-size" ->
5565 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5566 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5567 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5568 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5569 | "persistent-location" -> { c with jumpback = bool_of_string v }
5570 | "background-color" -> { c with bgcolor = color_of_string v }
5571 | "scrollbar-in-presentation" ->
5572 { c with scrollbarinpm = bool_of_string v }
5573 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5574 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5575 | "mupdf-store-size" ->
5576 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5577 | "checkers" -> { c with checkers = bool_of_string v }
5578 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5579 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5580 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5581 | "uri-launcher" -> { c with urilauncher = unent v }
5582 | "path-launcher" -> { c with pathlauncher = unent v }
5583 | "color-space" -> { c with colorspace = colorspace_of_string v }
5584 | "invert-colors" -> { c with invert = bool_of_string v }
5585 | "brightness" -> { c with colorscale = float_of_string v }
5586 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5587 | "ghyllscroll" ->
5588 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5589 | "columns" ->
5590 let nab = columns_of_string v in
5591 { c with columns = Some (nab, [||]) }
5592 | "birds-eye-columns" ->
5593 { c with beyecolumns = Some (max (int_of_string v) 2) }
5594 | "selection-command" -> { c with selcmd = unent v }
5595 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5596 | _ -> c
5597 with exn ->
5598 prerr_endline ("Error processing attribute (`" ^
5599 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5602 let rec fold c = function
5603 | [] -> c
5604 | (k, v) :: rest ->
5605 let c = apply c k v in
5606 fold c rest
5608 fold { c with keyhashes = copykeyhashes c } attrs;
5611 let fromstring f pos n v d =
5612 try f v
5613 with exn ->
5614 dolog "Error processing attribute (%S=%S) at %d\n%s"
5615 n v pos (Printexc.to_string exn)
5620 let bookmark_of attrs =
5621 let rec fold title page rely = function
5622 | ("title", v) :: rest -> fold v page rely rest
5623 | ("page", v) :: rest -> fold title v rely rest
5624 | ("rely", v) :: rest -> fold title page v rest
5625 | _ :: rest -> fold title page rely rest
5626 | [] -> title, page, rely
5628 fold "invalid" "0" "0" attrs
5631 let doc_of attrs =
5632 let rec fold path page rely pan = function
5633 | ("path", v) :: rest -> fold v page rely pan rest
5634 | ("page", v) :: rest -> fold path v rely pan rest
5635 | ("rely", v) :: rest -> fold path page v pan rest
5636 | ("pan", v) :: rest -> fold path page rely v rest
5637 | _ :: rest -> fold path page rely pan rest
5638 | [] -> path, page, rely, pan
5640 fold "" "0" "0" "0" attrs
5643 let map_of attrs =
5644 let rec fold rs ls = function
5645 | ("out", v) :: rest -> fold v ls rest
5646 | ("in", v) :: rest -> fold rs v rest
5647 | _ :: rest -> fold ls rs rest
5648 | [] -> ls, rs
5650 fold "" "" attrs
5653 let setconf dst src =
5654 dst.scrollbw <- src.scrollbw;
5655 dst.scrollh <- src.scrollh;
5656 dst.icase <- src.icase;
5657 dst.preload <- src.preload;
5658 dst.pagebias <- src.pagebias;
5659 dst.verbose <- src.verbose;
5660 dst.scrollstep <- src.scrollstep;
5661 dst.maxhfit <- src.maxhfit;
5662 dst.crophack <- src.crophack;
5663 dst.autoscrollstep <- src.autoscrollstep;
5664 dst.maxwait <- src.maxwait;
5665 dst.hlinks <- src.hlinks;
5666 dst.underinfo <- src.underinfo;
5667 dst.interpagespace <- src.interpagespace;
5668 dst.zoom <- src.zoom;
5669 dst.presentation <- src.presentation;
5670 dst.angle <- src.angle;
5671 dst.winw <- src.winw;
5672 dst.winh <- src.winh;
5673 dst.savebmarks <- src.savebmarks;
5674 dst.memlimit <- src.memlimit;
5675 dst.proportional <- src.proportional;
5676 dst.texcount <- src.texcount;
5677 dst.sliceheight <- src.sliceheight;
5678 dst.thumbw <- src.thumbw;
5679 dst.jumpback <- src.jumpback;
5680 dst.bgcolor <- src.bgcolor;
5681 dst.scrollbarinpm <- src.scrollbarinpm;
5682 dst.tilew <- src.tilew;
5683 dst.tileh <- src.tileh;
5684 dst.mustoresize <- src.mustoresize;
5685 dst.checkers <- src.checkers;
5686 dst.aalevel <- src.aalevel;
5687 dst.trimmargins <- src.trimmargins;
5688 dst.trimfuzz <- src.trimfuzz;
5689 dst.urilauncher <- src.urilauncher;
5690 dst.colorspace <- src.colorspace;
5691 dst.invert <- src.invert;
5692 dst.colorscale <- src.colorscale;
5693 dst.redirectstderr <- src.redirectstderr;
5694 dst.ghyllscroll <- src.ghyllscroll;
5695 dst.columns <- src.columns;
5696 dst.beyecolumns <- src.beyecolumns;
5697 dst.selcmd <- src.selcmd;
5698 dst.updatecurs <- src.updatecurs;
5699 dst.pathlauncher <- src.pathlauncher;
5700 dst.keyhashes <- copykeyhashes src;
5703 let get s =
5704 let h = Hashtbl.create 10 in
5705 let dc = { defconf with angle = defconf.angle } in
5706 let rec toplevel v t spos _ =
5707 match t with
5708 | Vdata | Vcdata | Vend -> v
5709 | Vopen ("llppconfig", _, closed) ->
5710 if closed
5711 then v
5712 else { v with f = llppconfig }
5713 | Vopen _ ->
5714 error "unexpected subelement at top level" s spos
5715 | Vclose _ -> error "unexpected close at top level" s spos
5717 and llppconfig v t spos _ =
5718 match t with
5719 | Vdata | Vcdata -> v
5720 | Vend -> error "unexpected end of input in llppconfig" s spos
5721 | Vopen ("defaults", attrs, closed) ->
5722 let c = config_of dc attrs in
5723 setconf dc c;
5724 if closed
5725 then v
5726 else { v with f = defaults }
5728 | Vopen ("ui-font", attrs, closed) ->
5729 let rec getsize size = function
5730 | [] -> size
5731 | ("size", v) :: rest ->
5732 let size =
5733 fromstring int_of_string spos "size" v fstate.fontsize in
5734 getsize size rest
5735 | l -> getsize size l
5737 fstate.fontsize <- getsize fstate.fontsize attrs;
5738 if closed
5739 then v
5740 else { v with f = uifont (Buffer.create 10) }
5742 | Vopen ("doc", attrs, closed) ->
5743 let pathent, spage, srely, span = doc_of attrs in
5744 let path = unent pathent
5745 and pageno = fromstring int_of_string spos "page" spage 0
5746 and rely = fromstring float_of_string spos "rely" srely 0.0
5747 and pan = fromstring int_of_string spos "pan" span 0 in
5748 let c = config_of dc attrs in
5749 let anchor = (pageno, rely) in
5750 if closed
5751 then (Hashtbl.add h path (c, [], pan, anchor); v)
5752 else { v with f = doc path pan anchor c [] }
5754 | Vopen _ ->
5755 error "unexpected subelement in llppconfig" s spos
5757 | Vclose "llppconfig" -> { v with f = toplevel }
5758 | Vclose _ -> error "unexpected close in llppconfig" s spos
5760 and defaults v t spos _ =
5761 match t with
5762 | Vdata | Vcdata -> v
5763 | Vend -> error "unexpected end of input in defaults" s spos
5764 | Vopen ("keymap", attrs, closed) ->
5765 let modename =
5766 try List.assoc "mode" attrs
5767 with Not_found -> "global" in
5768 if closed
5769 then v
5770 else
5771 let ret keymap =
5772 let h = findkeyhash dc modename in
5773 KeyMap.iter (Hashtbl.replace h) keymap;
5774 defaults
5776 { v with f = pkeymap ret KeyMap.empty }
5778 | Vopen (_, _, _) ->
5779 error "unexpected subelement in defaults" s spos
5781 | Vclose "defaults" ->
5782 { v with f = llppconfig }
5784 | Vclose _ -> error "unexpected close in defaults" s spos
5786 and uifont b v t spos epos =
5787 match t with
5788 | Vdata | Vcdata ->
5789 Buffer.add_substring b s spos (epos - spos);
5791 | Vopen (_, _, _) ->
5792 error "unexpected subelement in ui-font" s spos
5793 | Vclose "ui-font" ->
5794 if String.length !fontpath = 0
5795 then fontpath := Buffer.contents b;
5796 { v with f = llppconfig }
5797 | Vclose _ -> error "unexpected close in ui-font" s spos
5798 | Vend -> error "unexpected end of input in ui-font" s spos
5800 and doc path pan anchor c bookmarks v t spos _ =
5801 match t with
5802 | Vdata | Vcdata -> v
5803 | Vend -> error "unexpected end of input in doc" s spos
5804 | Vopen ("bookmarks", _, closed) ->
5805 if closed
5806 then v
5807 else { v with f = pbookmarks path pan anchor c bookmarks }
5809 | Vopen ("keymap", attrs, closed) ->
5810 let modename =
5811 try List.assoc "mode" attrs
5812 with Not_found -> "global"
5814 if closed
5815 then v
5816 else
5817 let ret keymap =
5818 let h = findkeyhash c modename in
5819 KeyMap.iter (Hashtbl.replace h) keymap;
5820 doc path pan anchor c bookmarks
5822 { v with f = pkeymap ret KeyMap.empty }
5824 | Vopen (_, _, _) ->
5825 error "unexpected subelement in doc" s spos
5827 | Vclose "doc" ->
5828 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5829 { v with f = llppconfig }
5831 | Vclose _ -> error "unexpected close in doc" s spos
5833 and pkeymap ret keymap v t spos _ =
5834 match t with
5835 | Vdata | Vcdata -> v
5836 | Vend -> error "unexpected end of input in keymap" s spos
5837 | Vopen ("map", attrs, closed) ->
5838 let r, l = map_of attrs in
5839 let kss = fromstring keys_of_string spos "in" r [] in
5840 let lss = fromstring keys_of_string spos "out" l [] in
5841 let keymap =
5842 match kss with
5843 | [] -> keymap
5844 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5845 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5847 if closed
5848 then { v with f = pkeymap ret keymap }
5849 else
5850 let f () = v in
5851 { v with f = skip "map" f }
5853 | Vopen _ ->
5854 error "unexpected subelement in keymap" s spos
5856 | Vclose "keymap" ->
5857 { v with f = ret keymap }
5859 | Vclose _ -> error "unexpected close in keymap" s spos
5861 and pbookmarks path pan anchor c bookmarks v t spos _ =
5862 match t with
5863 | Vdata | Vcdata -> v
5864 | Vend -> error "unexpected end of input in bookmarks" s spos
5865 | Vopen ("item", attrs, closed) ->
5866 let titleent, spage, srely = bookmark_of attrs in
5867 let page = fromstring int_of_string spos "page" spage 0
5868 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5869 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5870 if closed
5871 then { v with f = pbookmarks path pan anchor c bookmarks }
5872 else
5873 let f () = v in
5874 { v with f = skip "item" f }
5876 | Vopen _ ->
5877 error "unexpected subelement in bookmarks" s spos
5879 | Vclose "bookmarks" ->
5880 { v with f = doc path pan anchor c bookmarks }
5882 | Vclose _ -> error "unexpected close in bookmarks" s spos
5884 and skip tag f v t spos _ =
5885 match t with
5886 | Vdata | Vcdata -> v
5887 | Vend ->
5888 error ("unexpected end of input in skipped " ^ tag) s spos
5889 | Vopen (tag', _, closed) ->
5890 if closed
5891 then v
5892 else
5893 let f' () = { v with f = skip tag f } in
5894 { v with f = skip tag' f' }
5895 | Vclose ctag ->
5896 if tag = ctag
5897 then f ()
5898 else error ("unexpected close in skipped " ^ tag) s spos
5901 parse { f = toplevel; accu = () } s;
5902 h, dc;
5905 let do_load f ic =
5907 let len = in_channel_length ic in
5908 let s = String.create len in
5909 really_input ic s 0 len;
5910 f s;
5911 with
5912 | Parse_error (msg, s, pos) ->
5913 let subs = subs s pos in
5914 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5915 failwith ("parse error: " ^ s)
5917 | exn ->
5918 failwith ("config load error: " ^ Printexc.to_string exn)
5921 let defconfpath =
5922 let dir =
5924 let dir = Filename.concat home ".config" in
5925 if Sys.is_directory dir then dir else home
5926 with _ -> home
5928 Filename.concat dir "llpp.conf"
5931 let confpath = ref defconfpath;;
5933 let load1 f =
5934 if Sys.file_exists !confpath
5935 then
5936 match
5937 (try Some (open_in_bin !confpath)
5938 with exn ->
5939 prerr_endline
5940 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5941 Printexc.to_string exn);
5942 None
5944 with
5945 | Some ic ->
5946 begin try
5947 f (do_load get ic)
5948 with exn ->
5949 prerr_endline
5950 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5951 Printexc.to_string exn);
5952 end;
5953 close_in ic;
5955 | None -> ()
5956 else
5957 f (Hashtbl.create 0, defconf)
5960 let load () =
5961 let f (h, dc) =
5962 let pc, pb, px, pa =
5964 Hashtbl.find h (Filename.basename state.path)
5965 with Not_found -> dc, [], 0, (0, 0.0)
5967 setconf defconf dc;
5968 setconf conf pc;
5969 state.bookmarks <- pb;
5970 state.x <- px;
5971 state.scrollw <- conf.scrollbw;
5972 if conf.jumpback
5973 then state.anchor <- pa;
5974 cbput state.hists.nav pa;
5976 load1 f
5979 let add_attrs bb always dc c =
5980 let ob s a b =
5981 if always || a != b
5982 then Printf.bprintf bb "\n %s='%b'" s a
5983 and oi s a b =
5984 if always || a != b
5985 then Printf.bprintf bb "\n %s='%d'" s a
5986 and oI s a b =
5987 if always || a != b
5988 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5989 and oz s a b =
5990 if always || a <> b
5991 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5992 and oF s a b =
5993 if always || a <> b
5994 then Printf.bprintf bb "\n %s='%f'" s a
5995 and oc s a b =
5996 if always || a <> b
5997 then
5998 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5999 and oC s a b =
6000 if always || a <> b
6001 then
6002 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6003 and oR s a b =
6004 if always || a <> b
6005 then
6006 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6007 and os s a b =
6008 if always || a <> b
6009 then
6010 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6011 and og s a b =
6012 if always || a <> b
6013 then
6014 match a with
6015 | None -> ()
6016 | Some (_N, _A, _B) ->
6017 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6018 and oW s a b =
6019 if always || a <> b
6020 then
6021 let v =
6022 match a with
6023 | None -> "false"
6024 | Some f ->
6025 if f = infinity
6026 then "true"
6027 else string_of_float f
6029 Printf.bprintf bb "\n %s='%s'" s v
6030 and oco s a b =
6031 if always || a <> b
6032 then
6033 match a with
6034 | Some ((n, a, b), _) when n > 1 ->
6035 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6036 | _ -> ()
6037 and obeco s a b =
6038 if always || a <> b
6039 then
6040 match a with
6041 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6042 | _ -> ()
6044 let w, h =
6045 if always
6046 then dc.winw, dc.winh
6047 else
6048 match state.fullscreen with
6049 | Some wh -> wh
6050 | None -> c.winw, c.winh
6052 let zoom, presentation, interpagespace, maxwait =
6053 if always
6054 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6055 else
6056 match state.mode with
6057 | Birdseye (bc, _, _, _, _) ->
6058 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6059 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6061 oi "width" w dc.winw;
6062 oi "height" h dc.winh;
6063 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6064 oi "scroll-handle-height" c.scrollh dc.scrollh;
6065 ob "case-insensitive-search" c.icase dc.icase;
6066 ob "preload" c.preload dc.preload;
6067 oi "page-bias" c.pagebias dc.pagebias;
6068 oi "scroll-step" c.scrollstep dc.scrollstep;
6069 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6070 ob "max-height-fit" c.maxhfit dc.maxhfit;
6071 ob "crop-hack" c.crophack dc.crophack;
6072 oW "throttle" maxwait dc.maxwait;
6073 ob "highlight-links" c.hlinks dc.hlinks;
6074 ob "under-cursor-info" c.underinfo dc.underinfo;
6075 oi "vertical-margin" interpagespace dc.interpagespace;
6076 oz "zoom" zoom dc.zoom;
6077 ob "presentation" presentation dc.presentation;
6078 oi "rotation-angle" c.angle dc.angle;
6079 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6080 ob "proportional-display" c.proportional dc.proportional;
6081 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6082 oi "tex-count" c.texcount dc.texcount;
6083 oi "slice-height" c.sliceheight dc.sliceheight;
6084 oi "thumbnail-width" c.thumbw dc.thumbw;
6085 ob "persistent-location" c.jumpback dc.jumpback;
6086 oc "background-color" c.bgcolor dc.bgcolor;
6087 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6088 oi "tile-width" c.tilew dc.tilew;
6089 oi "tile-height" c.tileh dc.tileh;
6090 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6091 ob "checkers" c.checkers dc.checkers;
6092 oi "aalevel" c.aalevel dc.aalevel;
6093 ob "trim-margins" c.trimmargins dc.trimmargins;
6094 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6095 os "uri-launcher" c.urilauncher dc.urilauncher;
6096 os "path-launcher" c.pathlauncher dc.pathlauncher;
6097 oC "color-space" c.colorspace dc.colorspace;
6098 ob "invert-colors" c.invert dc.invert;
6099 oF "brightness" c.colorscale dc.colorscale;
6100 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6101 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6102 oco "columns" c.columns dc.columns;
6103 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6104 os "selection-command" c.selcmd dc.selcmd;
6105 ob "update-cursor" c.updatecurs dc.updatecurs;
6108 let keymapsbuf always dc c =
6109 let bb = Buffer.create 16 in
6110 let rec loop = function
6111 | [] -> ()
6112 | (modename, h) :: rest ->
6113 let dh = findkeyhash dc modename in
6114 if always || h <> dh
6115 then (
6116 if Hashtbl.length h > 0
6117 then (
6118 if Buffer.length bb > 0
6119 then Buffer.add_char bb '\n';
6120 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6121 Hashtbl.iter (fun i o ->
6122 let isdifferent = always ||
6124 let dO = Hashtbl.find dh i in
6125 dO <> o
6126 with Not_found -> true
6128 if isdifferent
6129 then
6130 let addkm (k, m) =
6131 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6132 if Wsi.withalt m then Buffer.add_string bb "alt-";
6133 if Wsi.withshift m then Buffer.add_string bb "shift-";
6134 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6135 Buffer.add_string bb (Wsi.keyname k);
6137 let addkms l =
6138 let rec loop = function
6139 | [] -> ()
6140 | km :: [] -> addkm km
6141 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6143 loop l
6145 Buffer.add_string bb "<map in='";
6146 addkm i;
6147 match o with
6148 | KMinsrt km ->
6149 Buffer.add_string bb "' out='";
6150 addkm km;
6151 Buffer.add_string bb "'/>\n"
6153 | KMinsrl kms ->
6154 Buffer.add_string bb "' out='";
6155 addkms kms;
6156 Buffer.add_string bb "'/>\n"
6158 | KMmulti (ins, kms) ->
6159 Buffer.add_char bb ' ';
6160 addkms ins;
6161 Buffer.add_string bb "' out='";
6162 addkms kms;
6163 Buffer.add_string bb "'/>\n"
6164 ) h;
6165 Buffer.add_string bb "</keymap>";
6168 loop rest
6170 loop c.keyhashes;
6174 let save () =
6175 let uifontsize = fstate.fontsize in
6176 let bb = Buffer.create 32768 in
6177 let f (h, dc) =
6178 let dc = if conf.bedefault then conf else dc in
6179 Buffer.add_string bb "<llppconfig>\n";
6181 if String.length !fontpath > 0
6182 then
6183 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6184 uifontsize
6185 !fontpath
6186 else (
6187 if uifontsize <> 14
6188 then
6189 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6192 Buffer.add_string bb "<defaults ";
6193 add_attrs bb true dc dc;
6194 let kb = keymapsbuf true dc dc in
6195 if Buffer.length kb > 0
6196 then (
6197 Buffer.add_string bb ">\n";
6198 Buffer.add_buffer bb kb;
6199 Buffer.add_string bb "\n</defaults>\n";
6201 else Buffer.add_string bb "/>\n";
6203 let adddoc path pan anchor c bookmarks =
6204 if bookmarks == [] && c = dc && anchor = emptyanchor
6205 then ()
6206 else (
6207 Printf.bprintf bb "<doc path='%s'"
6208 (enent path 0 (String.length path));
6210 if anchor <> emptyanchor
6211 then (
6212 let n, y = anchor in
6213 Printf.bprintf bb " page='%d'" n;
6214 if y > 1e-6
6215 then
6216 Printf.bprintf bb " rely='%f'" y
6220 if pan != 0
6221 then Printf.bprintf bb " pan='%d'" pan;
6223 add_attrs bb false dc c;
6224 let kb = keymapsbuf false dc c in
6226 begin match bookmarks with
6227 | [] ->
6228 if Buffer.length kb > 0
6229 then (
6230 Buffer.add_string bb ">\n";
6231 Buffer.add_buffer bb kb;
6232 Buffer.add_string bb "</doc>\n";
6234 else Buffer.add_string bb "/>\n"
6235 | _ ->
6236 Buffer.add_string bb ">\n<bookmarks>\n";
6237 List.iter (fun (title, _level, (page, rely)) ->
6238 Printf.bprintf bb
6239 "<item title='%s' page='%d'"
6240 (enent title 0 (String.length title))
6241 page
6243 if rely > 1e-6
6244 then
6245 Printf.bprintf bb " rely='%f'" rely
6247 Buffer.add_string bb "/>\n";
6248 ) bookmarks;
6249 Buffer.add_string bb "</bookmarks>";
6250 if Buffer.length kb > 0
6251 then (
6252 Buffer.add_string bb "\n";
6253 Buffer.add_buffer bb kb;
6255 Buffer.add_string bb "\n</doc>\n";
6256 end;
6260 let pan, conf =
6261 match state.mode with
6262 | Birdseye (c, pan, _, _, _) ->
6263 let beyecolumns =
6264 match conf.columns with
6265 | Some ((c, _, _), _) -> Some c
6266 | None -> None
6267 and columns =
6268 match c.columns with
6269 | Some (c, _) -> Some (c, [||])
6270 | None -> None
6272 pan, { c with beyecolumns = beyecolumns; columns = columns }
6273 | _ -> state.x, conf
6275 let basename = Filename.basename state.path in
6276 adddoc basename pan (getanchor ())
6277 { conf with
6278 autoscrollstep =
6279 match state.autoscroll with
6280 | Some step -> step
6281 | None -> conf.autoscrollstep }
6282 (if conf.savebmarks then state.bookmarks else []);
6284 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6285 if basename <> path
6286 then adddoc path x y c bookmarks
6287 ) h;
6288 Buffer.add_string bb "</llppconfig>";
6290 load1 f;
6291 if Buffer.length bb > 0
6292 then
6294 let tmp = !confpath ^ ".tmp" in
6295 let oc = open_out_bin tmp in
6296 Buffer.output_buffer oc bb;
6297 close_out oc;
6298 Unix.rename tmp !confpath;
6299 with exn ->
6300 prerr_endline
6301 ("error while saving configuration: " ^ Printexc.to_string exn)
6303 end;;
6305 let () =
6306 Arg.parse
6307 (Arg.align
6308 [("-p", Arg.String (fun s -> state.password <- s) ,
6309 "<password> Set password");
6311 ("-f", Arg.String (fun s -> Config.fontpath := s),
6312 "<path> Set path to the user interface font");
6314 ("-c", Arg.String (fun s -> Config.confpath := s),
6315 "<path> Set path to the configuration file");
6317 ("-v", Arg.Unit (fun () ->
6318 Printf.printf
6319 "%s\nconfiguration path: %s\n"
6320 (version ())
6321 Config.defconfpath
6323 exit 0), " Print version and exit");
6326 (fun s -> state.path <- s)
6327 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6329 if String.length state.path = 0
6330 then (prerr_endline "file name missing"; exit 1);
6332 Config.load ();
6334 let globalkeyhash = findkeyhash conf "global" in
6335 let wsfd, winw, winh = Wsi.init (object
6336 method expose =
6337 if nogeomcmds state.geomcmds
6338 then display ()
6339 method display = display ()
6340 method reshape w h = reshape w h
6341 method mouse b d x y m = mouse b d x y m
6342 method motion x y = state.mpos <- (x, y); motion x y
6343 method pmotion x y = state.mpos <- (x, y); pmotion x y
6344 method key k m =
6345 let mascm = m land (
6346 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6347 ) in
6348 match state.keystate with
6349 | KSnone ->
6350 let km = k, mascm in
6351 begin
6352 match
6353 try Hashtbl.find globalkeyhash km
6354 with Not_found ->
6355 let modehash = state.uioh#modehash in
6356 try Hashtbl.find modehash km
6357 with Not_found -> KMinsrt (k, m)
6358 with
6359 | KMinsrt (k, m) -> keyboard k m
6360 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6361 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6363 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6364 List.iter (fun (k, m) -> keyboard k m) insrt;
6365 state.keystate <- KSnone
6366 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6367 state.keystate <- KSinto (keys, insrt)
6368 | _ ->
6369 state.keystate <- KSnone
6371 method enter x y = state.mpos <- (x, y); pmotion x y
6372 method leave = state.mpos <- (-1, -1)
6373 method quit = raise Quit
6374 end) conf.winw conf.winh in
6376 state.wsfd <- wsfd;
6378 if not (
6379 List.exists GlMisc.check_extension
6380 [ "GL_ARB_texture_rectangle"
6381 ; "GL_EXT_texture_recangle"
6382 ; "GL_NV_texture_rectangle" ]
6384 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6386 let cr, sw = Unix.pipe ()
6387 and sr, cw = Unix.pipe () in
6389 cloexec cr;
6390 cloexec sw;
6391 cloexec sr;
6392 cloexec cw;
6394 setcheckers conf.checkers;
6395 redirectstderr ();
6397 init (cr, cw) (
6398 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6399 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6400 !Config.fontpath
6402 state.sr <- sr;
6403 state.sw <- sw;
6404 state.text <- "Opening " ^ state.path;
6405 opendoc state.path state.password;
6406 state.uioh <- uioh;
6407 setfontsize fstate.fontsize;
6408 reshape winw winh;
6410 let rec loop deadline =
6411 let r =
6412 match state.errfd with
6413 | None -> [state.sr; state.wsfd]
6414 | Some fd -> [state.sr; state.wsfd; fd]
6416 if state.redisplay
6417 then (
6418 state.redisplay <- false;
6419 display ();
6421 let timeout =
6422 let now = now () in
6423 if deadline > now
6424 then (
6425 if deadline = infinity
6426 then ~-.1.0
6427 else max 0.0 (deadline -. now)
6429 else 0.0
6431 let r, _, _ =
6432 try Unix.select r [] [] timeout
6433 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6435 begin match r with
6436 | [] ->
6437 state.ghyll None;
6438 let newdeadline =
6439 if state.ghyll == noghyll
6440 then
6441 match state.autoscroll with
6442 | Some step when step != 0 ->
6443 let y = state.y + step in
6444 let y =
6445 if y < 0
6446 then state.maxy
6447 else if y >= state.maxy then 0 else y
6449 gotoy y;
6450 if state.mode = View
6451 then state.text <- "";
6452 deadline +. 0.01
6453 | _ -> infinity
6454 else deadline +. 0.01
6456 loop newdeadline
6458 | l ->
6459 let rec checkfds = function
6460 | [] -> ()
6461 | fd :: rest when fd = state.sr ->
6462 let cmd = readcmd state.sr in
6463 act cmd;
6464 checkfds rest
6466 | fd :: rest when fd = state.wsfd ->
6467 Wsi.readresp fd;
6468 checkfds rest
6470 | fd :: rest ->
6471 let s = String.create 80 in
6472 let n = Unix.read fd s 0 80 in
6473 if conf.redirectstderr
6474 then (
6475 Buffer.add_substring state.errmsgs s 0 n;
6476 state.newerrmsgs <- true;
6477 state.redisplay <- true;
6479 else (
6480 prerr_string (String.sub s 0 n);
6481 flush stderr;
6483 checkfds rest
6485 checkfds l;
6486 let newdeadline =
6487 let deadline1 =
6488 if deadline = infinity
6489 then now () +. 0.01
6490 else deadline
6492 match state.autoscroll with
6493 | Some step when step != 0 -> deadline1
6494 | _ -> if state.ghyll == noghyll then infinity else deadline1
6496 loop newdeadline
6497 end;
6500 loop infinity;
6501 with Quit ->
6502 wcmd "quit";
6503 Config.save ();
6504 exit 0;