Usability
[llpp.git] / main.ml
blobbee110f0344f64454e084b03b6bdf7a669ca1eca
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 * int * int * int * 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 findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
96 let platform_to_string = function
97 | Punknown -> "unknown"
98 | Plinux -> "Linux"
99 | Posx -> "OSX"
100 | Psun -> "Sun"
101 | Pfreebsd -> "FreeBSD"
102 | Pdragonflybsd -> "DragonflyBSD"
103 | Popenbsd -> "OpenBSD"
104 | Pnetbsd -> "NetBSD"
105 | Pcygwin -> "Cygwin"
108 let platform = platform ();;
110 type x = int
111 and y = int
112 and tilex = int
113 and tiley = int
114 and tileparams = (x * y * width * height * tilex * tiley)
117 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
119 type mpos = int * int
120 and mstate =
121 | Msel of (mpos * mpos)
122 | Mpan of mpos
123 | Mscrolly | Mscrollx
124 | Mzoom of (int * int)
125 | Mzoomrect of (mpos * mpos)
126 | Mnone
129 type textentry = string * string * onhist option * onkey * ondone
130 and onkey = string -> int -> te
131 and ondone = string -> unit
132 and histcancel = unit -> unit
133 and onhist = ((histcmd -> string) * histcancel)
134 and histcmd = HCnext | HCprev | HCfirst | HClast
135 and te =
136 | TEstop
137 | TEdone of string
138 | TEcont of string
139 | TEswitch of textentry
142 type 'a circbuf =
143 { store : 'a array
144 ; mutable rc : int
145 ; mutable wc : int
146 ; mutable len : int
150 let bound v minv maxv =
151 max minv (min maxv v);
154 let cbnew n v =
155 { store = Array.create n v
156 ; rc = 0
157 ; wc = 0
158 ; len = 0
162 let drawstring size x y s =
163 Gl.enable `blend;
164 Gl.enable `texture_2d;
165 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
166 ignore (drawstr size x y s);
167 Gl.disable `blend;
168 Gl.disable `texture_2d;
171 let drawstring1 size x y s =
172 drawstr size x y s;
175 let drawstring2 size x y fmt =
176 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
179 let cbcap b = Array.length b.store;;
181 let cbput b v =
182 let cap = cbcap b in
183 b.store.(b.wc) <- v;
184 b.wc <- (b.wc + 1) mod cap;
185 b.rc <- b.wc;
186 b.len <- min (b.len + 1) cap;
189 let cbempty b = b.len = 0;;
191 let cbgetg b circular dir =
192 if cbempty b
193 then b.store.(0)
194 else
195 let rc = b.rc + dir in
196 let rc =
197 if circular
198 then (
199 if rc = -1
200 then b.len-1
201 else (
202 if rc = b.len
203 then 0
204 else rc
207 else max 0 (min rc (b.len-1))
209 b.rc <- rc;
210 b.store.(rc);
213 let cbget b = cbgetg b false;;
214 let cbgetc b = cbgetg b true;;
216 type page =
217 { pageno : int
218 ; pagedimno : int
219 ; pagew : int
220 ; pageh : int
221 ; pagex : int
222 ; pagey : int
223 ; pagevw : int
224 ; pagevh : int
225 ; pagedispx : int
226 ; pagedispy : int
230 let debugl l =
231 dolog "l %d dim=%d {" l.pageno l.pagedimno;
232 dolog " WxH %dx%d" l.pagew l.pageh;
233 dolog " vWxH %dx%d" l.pagevw l.pagevh;
234 dolog " pagex,y %d,%d" l.pagex l.pagey;
235 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
236 dolog "}";
239 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
240 dolog "rect {";
241 dolog " x0,y0=(% f, % f)" x0 y0;
242 dolog " x1,y1=(% f, % f)" x1 y1;
243 dolog " x2,y2=(% f, % f)" x2 y2;
244 dolog " x3,y3=(% f, % f)" x3 y3;
245 dolog "}";
248 type columns =
249 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
250 and multicol = columncount * covercount * covercount
251 and pdimno = int
252 and columncount = int
253 and covercount = int;;
255 type conf =
256 { mutable scrollbw : int
257 ; mutable scrollh : int
258 ; mutable icase : bool
259 ; mutable preload : bool
260 ; mutable pagebias : int
261 ; mutable verbose : bool
262 ; mutable debug : bool
263 ; mutable scrollstep : int
264 ; mutable maxhfit : bool
265 ; mutable crophack : bool
266 ; mutable autoscrollstep : int
267 ; mutable maxwait : float option
268 ; mutable hlinks : bool
269 ; mutable underinfo : bool
270 ; mutable interpagespace : interpagespace
271 ; mutable zoom : float
272 ; mutable presentation : bool
273 ; mutable angle : angle
274 ; mutable winw : int
275 ; mutable winh : int
276 ; mutable savebmarks : bool
277 ; mutable proportional : proportional
278 ; mutable trimmargins : trimmargins
279 ; mutable trimfuzz : irect
280 ; mutable memlimit : memsize
281 ; mutable texcount : texcount
282 ; mutable sliceheight : sliceheight
283 ; mutable thumbw : width
284 ; mutable jumpback : bool
285 ; mutable bgcolor : float * float * float
286 ; mutable bedefault : bool
287 ; mutable scrollbarinpm : bool
288 ; mutable tilew : int
289 ; mutable tileh : int
290 ; mutable mustoresize : memsize
291 ; mutable checkers : bool
292 ; mutable aalevel : int
293 ; mutable urilauncher : string
294 ; mutable pathlauncher : string
295 ; mutable colorspace : colorspace
296 ; mutable invert : bool
297 ; mutable colorscale : float
298 ; mutable redirectstderr : bool
299 ; mutable ghyllscroll : (int * int * int) option
300 ; mutable columns : columns option
301 ; mutable beyecolumns : columncount option
302 ; mutable selcmd : string
303 ; mutable updatecurs : bool
304 ; mutable keyhashes : (string * keyhash) list
308 type anchor = pageno * top;;
310 type outline = string * int * anchor;;
312 type rect = float * float * float * float * float * float * float * float;;
314 type tile = opaque * pixmapsize * elapsed
315 and elapsed = float;;
316 type pagemapkey = pageno * gen;;
317 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
318 and row = int
319 and col = int;;
321 let emptyanchor = (0, 0.0);;
323 type infochange = | Memused | Docinfo | Pdim;;
325 class type uioh = object
326 method display : unit
327 method key : int -> int -> uioh
328 method button : int -> bool -> int -> int -> int -> uioh
329 method motion : int -> int -> uioh
330 method pmotion : int -> int -> uioh
331 method infochanged : infochange -> unit
332 method scrollpw : (int * float * float)
333 method scrollph : (int * float * float)
334 method modehash : keyhash
335 end;;
337 type mode =
338 | Birdseye of (conf * leftx * pageno * pageno * anchor)
339 | Textentry of (textentry * onleave)
340 | View
341 | LinkNav of linktarget
342 and onleave = leavetextentrystatus -> unit
343 and leavetextentrystatus = | Cancel | Confirm
344 and helpitem = string * int * action
345 and action =
346 | Noaction
347 | Action of (uioh -> uioh)
348 and linktarget =
349 | Ltexact of ((pageno * int) * (int * int * int * int))
350 | Ltgendir of int
353 let isbirdseye = function Birdseye _ -> true | _ -> false;;
354 let istextentry = function Textentry _ -> true | _ -> false;;
356 type currently =
357 | Idle
358 | Loading of (page * gen)
359 | Tiling of (
360 page * opaque * colorspace * angle * gen * col * row * width * height
362 | Outlining of outline list
365 let emptykeyhash = Hashtbl.create 0;;
366 let nouioh : uioh = object (self)
367 method display = ()
368 method key _ _ = self
369 method button _ _ _ _ _ = self
370 method motion _ _ = self
371 method pmotion _ _ = self
372 method infochanged _ = ()
373 method scrollpw = (0, nan, nan)
374 method scrollph = (0, nan, nan)
375 method modehash = emptykeyhash
376 end;;
378 type state =
379 { mutable sr : Unix.file_descr
380 ; mutable sw : Unix.file_descr
381 ; mutable wsfd : Unix.file_descr
382 ; mutable errfd : Unix.file_descr option
383 ; mutable stderr : Unix.file_descr
384 ; mutable errmsgs : Buffer.t
385 ; mutable newerrmsgs : bool
386 ; mutable w : int
387 ; mutable x : int
388 ; mutable y : int
389 ; mutable scrollw : int
390 ; mutable hscrollh : int
391 ; mutable anchor : anchor
392 ; mutable ranchors : (string * string * anchor) list
393 ; mutable maxy : int
394 ; mutable layout : page list
395 ; pagemap : (pagemapkey, opaque) Hashtbl.t
396 ; tilemap : (tilemapkey, tile) Hashtbl.t
397 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
398 ; mutable pdims : (pageno * width * height * leftx) list
399 ; mutable pagecount : int
400 ; mutable currently : currently
401 ; mutable mstate : mstate
402 ; mutable searchpattern : string
403 ; mutable rects : (pageno * recttype * rect) list
404 ; mutable rects1 : (pageno * recttype * rect) list
405 ; mutable text : string
406 ; mutable fullscreen : (width * height) option
407 ; mutable mode : mode
408 ; mutable uioh : uioh
409 ; mutable outlines : outline array
410 ; mutable bookmarks : outline list
411 ; mutable path : string
412 ; mutable password : string
413 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
414 ; mutable memused : memsize
415 ; mutable gen : gen
416 ; mutable throttle : (page list * int * float) option
417 ; mutable autoscroll : int option
418 ; mutable ghyll : (int option -> unit)
419 ; mutable help : helpitem array
420 ; mutable docinfo : (int * string) list
421 ; mutable texid : GlTex.texture_id option
422 ; hists : hists
423 ; mutable prevzoom : float
424 ; mutable progress : float
425 ; mutable redisplay : bool
426 ; mutable mpos : mpos
427 ; mutable keystate : keystate
429 and hists =
430 { pat : string circbuf
431 ; pag : string circbuf
432 ; nav : anchor circbuf
433 ; sel : string circbuf
437 let defconf =
438 { scrollbw = 7
439 ; scrollh = 12
440 ; icase = true
441 ; preload = true
442 ; pagebias = 0
443 ; verbose = false
444 ; debug = false
445 ; scrollstep = 24
446 ; maxhfit = true
447 ; crophack = false
448 ; autoscrollstep = 2
449 ; maxwait = None
450 ; hlinks = false
451 ; underinfo = false
452 ; interpagespace = 2
453 ; zoom = 1.0
454 ; presentation = false
455 ; angle = 0
456 ; winw = 900
457 ; winh = 900
458 ; savebmarks = true
459 ; proportional = true
460 ; trimmargins = false
461 ; trimfuzz = (0,0,0,0)
462 ; memlimit = 32 lsl 20
463 ; texcount = 256
464 ; sliceheight = 24
465 ; thumbw = 76
466 ; jumpback = true
467 ; bgcolor = (0.5, 0.5, 0.5)
468 ; bedefault = false
469 ; scrollbarinpm = true
470 ; tilew = 2048
471 ; tileh = 2048
472 ; mustoresize = 128 lsl 20
473 ; checkers = true
474 ; aalevel = 8
475 ; urilauncher =
476 (match platform with
477 | Plinux | Pfreebsd | Pdragonflybsd
478 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
479 | Posx -> "open \"%s\""
480 | Pcygwin -> "cygstart %s"
481 | Punknown -> "echo %s")
482 ; pathlauncher = "lp \"%s\""
483 ; selcmd =
484 (match platform with
485 | Plinux | Pfreebsd | Pdragonflybsd
486 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
487 | Posx -> "pbcopy"
488 | Pcygwin -> "wsel"
489 | Punknown -> "cat")
490 ; colorspace = Rgb
491 ; invert = false
492 ; colorscale = 1.0
493 ; redirectstderr = false
494 ; ghyllscroll = None
495 ; columns = None
496 ; beyecolumns = None
497 ; updatecurs = false
498 ; keyhashes =
499 let mk n = (n, Hashtbl.create 1) in
500 [ mk "global"
501 ; mk "info"
502 ; mk "help"
503 ; mk "outline"
504 ; mk "listview"
505 ; mk "birdseye"
506 ; mk "textentry"
507 ; mk "links"
512 let findkeyhash c name =
513 try List.assoc name c.keyhashes
514 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
517 let conf = { defconf with angle = defconf.angle };;
519 type fontstate =
520 { mutable fontsize : int
521 ; mutable wwidth : float
522 ; mutable maxrows : int
526 let fstate =
527 { fontsize = 14
528 ; wwidth = nan
529 ; maxrows = -1
533 let setfontsize n =
534 fstate.fontsize <- n;
535 fstate.wwidth <- measurestr fstate.fontsize "w";
536 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
539 let geturl s =
540 let colonpos = try String.index s ':' with Not_found -> -1 in
541 let len = String.length s in
542 if colonpos >= 0 && colonpos + 3 < len
543 then (
544 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
545 then
546 let schemestartpos =
547 try String.rindex_from s colonpos ' '
548 with Not_found -> -1
550 let scheme =
551 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
553 match scheme with
554 | "http" | "ftp" | "mailto" ->
555 let epos =
556 try String.index_from s colonpos ' '
557 with Not_found -> len
559 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
560 | _ -> ""
561 else ""
563 else ""
566 let popen =
567 let shell, farg = "/bin/sh", "-c" in
568 fun s ->
569 let args = [|shell; farg; s|] in
570 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
573 let gotouri uri =
574 if String.length conf.urilauncher = 0
575 then print_endline uri
576 else (
577 let url = geturl uri in
578 if String.length url = 0
579 then print_endline uri
580 else
581 let re = Str.regexp "%s" in
582 let command = Str.global_replace re url conf.urilauncher in
583 try popen command
584 with exn ->
585 Printf.eprintf
586 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
587 flush stderr;
591 let version () =
592 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
593 (platform_to_string platform) Sys.word_size Sys.ocaml_version
596 let makehelp () =
597 let strings = version () :: "" :: Help.keys in
598 Array.of_list (
599 List.map (fun s ->
600 let url = geturl s in
601 if String.length url > 0
602 then (s, 0, Action (fun u -> gotouri url; u))
603 else (s, 0, Noaction)
604 ) strings);
607 let noghyll _ = ();;
608 let firstgeomcmds = "", [];;
610 let state =
611 { sr = Unix.stdin
612 ; sw = Unix.stdin
613 ; wsfd = Unix.stdin
614 ; errfd = None
615 ; stderr = Unix.stderr
616 ; errmsgs = Buffer.create 0
617 ; newerrmsgs = false
618 ; x = 0
619 ; y = 0
620 ; w = 0
621 ; scrollw = 0
622 ; hscrollh = 0
623 ; anchor = emptyanchor
624 ; ranchors = []
625 ; layout = []
626 ; maxy = max_int
627 ; tilelru = Queue.create ()
628 ; pagemap = Hashtbl.create 10
629 ; tilemap = Hashtbl.create 10
630 ; pdims = []
631 ; pagecount = 0
632 ; currently = Idle
633 ; mstate = Mnone
634 ; rects = []
635 ; rects1 = []
636 ; text = ""
637 ; mode = View
638 ; fullscreen = None
639 ; searchpattern = ""
640 ; outlines = [||]
641 ; bookmarks = []
642 ; path = ""
643 ; password = ""
644 ; geomcmds = firstgeomcmds
645 ; hists =
646 { nav = cbnew 10 (0, 0.0)
647 ; pat = cbnew 10 ""
648 ; pag = cbnew 10 ""
649 ; sel = cbnew 10 ""
651 ; memused = 0
652 ; gen = 0
653 ; throttle = None
654 ; autoscroll = None
655 ; ghyll = noghyll
656 ; help = makehelp ()
657 ; docinfo = []
658 ; texid = None
659 ; prevzoom = 1.0
660 ; progress = -1.0
661 ; uioh = nouioh
662 ; redisplay = false
663 ; mpos = (-1, -1)
664 ; keystate = KSnone
668 let vlog fmt =
669 if conf.verbose
670 then
671 Printf.kprintf prerr_endline fmt
672 else
673 Printf.kprintf ignore fmt
676 let launchpath () =
677 if String.length conf.pathlauncher = 0
678 then print_endline state.path
679 else (
680 let re = Str.regexp "%s" in
681 let command = Str.global_replace re state.path conf.pathlauncher in
682 try popen command
683 with exn ->
684 Printf.eprintf
685 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
686 flush stderr;
690 let redirectstderr () =
691 if conf.redirectstderr
692 then
693 let rfd, wfd = Unix.pipe () in
694 state.stderr <- Unix.dup Unix.stderr;
695 state.errfd <- Some rfd;
696 Unix.dup2 wfd Unix.stderr;
697 else (
698 state.newerrmsgs <- false;
699 begin match state.errfd with
700 | Some fd ->
701 Unix.close fd;
702 Unix.dup2 state.stderr Unix.stderr;
703 state.errfd <- None;
704 | None -> ()
705 end;
706 prerr_string (Buffer.contents state.errmsgs);
707 flush stderr;
708 Buffer.clear state.errmsgs;
712 module G =
713 struct
714 let postRedisplay who =
715 if conf.verbose
716 then prerr_endline ("redisplay for " ^ who);
717 state.redisplay <- true;
719 end;;
721 let getopaque pageno =
722 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
723 with Not_found -> None
726 let putopaque pageno opaque =
727 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
730 let pagetranslatepoint l x y =
731 let dy = y - l.pagedispy in
732 let y = dy + l.pagey in
733 let dx = x - l.pagedispx in
734 let x = dx + l.pagex in
735 (x, y);
738 let getunder x y =
739 let rec f = function
740 | l :: rest ->
741 begin match getopaque l.pageno with
742 | Some opaque ->
743 let x0 = l.pagedispx in
744 let x1 = x0 + l.pagevw in
745 let y0 = l.pagedispy in
746 let y1 = y0 + l.pagevh in
747 if y >= y0 && y <= y1 && x >= x0 && x <= x1
748 then
749 let px, py = pagetranslatepoint l x y in
750 match whatsunder opaque px py with
751 | Unone -> f rest
752 | under -> under
753 else f rest
754 | _ ->
755 f rest
757 | [] -> Unone
759 f state.layout
762 let showtext c s =
763 state.text <- Printf.sprintf "%c%s" c s;
764 G.postRedisplay "showtext";
767 let updateunder x y =
768 match getunder x y with
769 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
770 | Ulinkuri uri ->
771 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
772 Wsi.setcursor Wsi.CURSOR_INFO
773 | Ulinkgoto (page, _) ->
774 if conf.underinfo
775 then showtext 'p' ("age: " ^ string_of_int (page+1));
776 Wsi.setcursor Wsi.CURSOR_INFO
777 | Utext s ->
778 if conf.underinfo then showtext 'f' ("ont: " ^ s);
779 Wsi.setcursor Wsi.CURSOR_TEXT
780 | Uunexpected s ->
781 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
782 Wsi.setcursor Wsi.CURSOR_INHERIT
783 | Ulaunch s ->
784 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
785 Wsi.setcursor Wsi.CURSOR_INHERIT
786 | Unamed s ->
787 if conf.underinfo then showtext 'n' ("amed: " ^ s);
788 Wsi.setcursor Wsi.CURSOR_INHERIT
789 | Uremote (filename, pageno) ->
790 if conf.underinfo then showtext 'r'
791 (Printf.sprintf "emote: %s (%d)" filename pageno);
792 Wsi.setcursor Wsi.CURSOR_INFO
795 let showlinktype under =
796 if conf.underinfo
797 then
798 match under with
799 | Unone -> ()
800 | Ulinkuri uri ->
801 showtext 'u' ("ri: " ^ uri)
802 | Ulinkgoto (page, _) ->
803 showtext 'p' ("age: " ^ string_of_int (page+1));
804 | Utext s ->
805 showtext 'f' ("ont: " ^ s);
806 | Uunexpected s ->
807 showtext 'u' ("nexpected: " ^ s);
808 | Ulaunch s ->
809 showtext 'l' ("aunch: " ^ s);
810 | Unamed s ->
811 showtext 'n' ("amed: " ^ s);
812 | Uremote (filename, pageno) ->
813 showtext 'r' (Printf.sprintf "emote: %s (%d)" filename pageno);
816 let addchar s c =
817 let b = Buffer.create (String.length s + 1) in
818 Buffer.add_string b s;
819 Buffer.add_char b c;
820 Buffer.contents b;
823 let colorspace_of_string s =
824 match String.lowercase s with
825 | "rgb" -> Rgb
826 | "bgr" -> Bgr
827 | "gray" -> Gray
828 | _ -> failwith "invalid colorspace"
831 let int_of_colorspace = function
832 | Rgb -> 0
833 | Bgr -> 1
834 | Gray -> 2
837 let colorspace_of_int = function
838 | 0 -> Rgb
839 | 1 -> Bgr
840 | 2 -> Gray
841 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
844 let colorspace_to_string = function
845 | Rgb -> "rgb"
846 | Bgr -> "bgr"
847 | Gray -> "gray"
850 let intentry_with_suffix text key =
851 let c =
852 if key >= 32 && key < 127
853 then Char.chr key
854 else '\000'
856 match Char.lowercase c with
857 | '0' .. '9' ->
858 let text = addchar text c in
859 TEcont text
861 | 'k' | 'm' | 'g' ->
862 let text = addchar text c in
863 TEcont text
865 | _ ->
866 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
867 TEcont text
870 let columns_to_string (n, a, b) =
871 if a = 0 && b = 0
872 then Printf.sprintf "%d" n
873 else Printf.sprintf "%d,%d,%d" n a b;
876 let columns_of_string s =
878 (int_of_string s, 0, 0)
879 with _ ->
880 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
883 let readcmd fd =
884 let s = "xxxx" in
885 let n = Unix.read fd s 0 4 in
886 if n != 4 then failwith "incomplete read(len)";
887 let len = 0
888 lor (Char.code s.[0] lsl 24)
889 lor (Char.code s.[1] lsl 16)
890 lor (Char.code s.[2] lsl 8)
891 lor (Char.code s.[3] lsl 0)
893 let s = String.create len in
894 let n = Unix.read fd s 0 len in
895 if n != len then failwith "incomplete read(data)";
899 let btod b = if b then 1 else 0;;
901 let wcmd fmt =
902 let b = Buffer.create 16 in
903 Buffer.add_string b "llll";
904 Printf.kbprintf
905 (fun b ->
906 let s = Buffer.contents b in
907 let n = String.length s in
908 let len = n - 4 in
909 (* dolog "wcmd %S" (String.sub s 4 len); *)
910 s.[0] <- Char.chr ((len lsr 24) land 0xff);
911 s.[1] <- Char.chr ((len lsr 16) land 0xff);
912 s.[2] <- Char.chr ((len lsr 8) land 0xff);
913 s.[3] <- Char.chr (len land 0xff);
914 let n' = Unix.write state.sw s 0 n in
915 if n' != n then failwith "write failed";
916 ) b fmt;
919 let calcips h =
920 if conf.presentation
921 then
922 let d = conf.winh - h in
923 max 0 ((d + 1) / 2)
924 else
925 conf.interpagespace
928 let calcheight () =
929 let rec f pn ph pi fh l =
930 match l with
931 | (n, _, h, _) :: rest ->
932 let ips = calcips h in
933 let fh =
934 if conf.presentation
935 then fh+ips
936 else (
937 if isbirdseye state.mode && pn = 0
938 then fh + ips
939 else fh
942 let fh = fh + ((n - pn) * (ph + pi)) in
943 f n h ips fh rest;
945 | [] ->
946 let inc =
947 if conf.presentation || (isbirdseye state.mode && pn = 0)
948 then 0
949 else -pi
951 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
952 max 0 fh
954 let fh = f 0 0 0 0 state.pdims in
958 let calcheight () =
959 match conf.columns with
960 | None -> calcheight ()
961 | Some (_, b) ->
962 if Array.length b > 0
963 then
964 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
965 y + h
966 else 0
969 let getpageyh pageno =
970 let rec f pn ph pi y l =
971 match l with
972 | (n, _, h, _) :: rest ->
973 let ips = calcips h in
974 if n >= pageno
975 then
976 let h = if n = pageno then h else ph in
977 if conf.presentation && n = pageno
978 then
979 y + (pageno - pn) * (ph + pi) + pi, h
980 else
981 y + (pageno - pn) * (ph + pi), h
982 else
983 let y = y + (if conf.presentation then pi else 0) in
984 let y = y + (n - pn) * (ph + pi) in
985 f n h ips y rest
987 | [] ->
988 y + (pageno - pn) * (ph + pi), ph
990 f 0 0 0 0 state.pdims
993 let getpageyh pageno =
994 match conf.columns with
995 | None -> getpageyh pageno
996 | Some (_, b) ->
997 let (_, _, y, (_, _, h, _)) = b.(pageno) in
998 y, h
1001 let getpagedim pageno =
1002 let rec f ppdim l =
1003 match l with
1004 | (n, _, _, _) as pdim :: rest ->
1005 if n >= pageno
1006 then (if n = pageno then pdim else ppdim)
1007 else f pdim rest
1009 | [] -> ppdim
1011 f (-1, -1, -1, -1) state.pdims
1014 let getpagey pageno = fst (getpageyh pageno);;
1016 let nogeomcmds cmds =
1017 match cmds with
1018 | s, [] -> String.length s = 0
1019 | _ -> false
1022 let layout1 y sh =
1023 let sh = sh - state.hscrollh in
1024 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1025 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1026 match pdims with
1027 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1028 let ips = calcips h in
1029 let yinc =
1030 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1031 then ips
1032 else 0
1034 (w, h, ips, xoff), rest, pdimno + 1, yinc
1035 | _ ->
1036 prev, pdims, pdimno, 0
1038 let dy = dy + yinc in
1039 let py = py + yinc in
1040 if pageno = state.pagecount || dy >= sh
1041 then
1042 accu
1043 else
1044 let vy = y + dy in
1045 if py + h <= vy - yinc
1046 then
1047 let py = py + h + ips in
1048 let dy = max 0 (py - y) in
1049 f ~pageno:(pageno+1)
1050 ~pdimno
1051 ~prev:curr
1054 ~pdims:rest
1055 ~accu
1056 else
1057 let pagey = vy - py in
1058 let pagevh = h - pagey in
1059 let pagevh = min (sh - dy) pagevh in
1060 let off = if yinc > 0 then py - vy else 0 in
1061 let py = py + h + ips in
1062 let pagex, dx =
1063 let xoff = xoff +
1064 if state.w < conf.winw - state.scrollw
1065 then (conf.winw - state.scrollw - state.w) / 2
1066 else 0
1068 let dispx = xoff + state.x in
1069 if dispx < 0
1070 then (-dispx, 0)
1071 else (0, dispx)
1073 let pagevw =
1074 let lw = w - pagex in
1075 min lw (conf.winw - state.scrollw)
1077 let e =
1078 { pageno = pageno
1079 ; pagedimno = pdimno
1080 ; pagew = w
1081 ; pageh = h
1082 ; pagex = pagex
1083 ; pagey = pagey + off
1084 ; pagevw = pagevw
1085 ; pagevh = pagevh - off
1086 ; pagedispx = dx
1087 ; pagedispy = dy + off
1090 let accu = e :: accu in
1091 f ~pageno:(pageno+1)
1092 ~pdimno
1093 ~prev:curr
1095 ~dy:(dy+pagevh+ips)
1096 ~pdims:rest
1097 ~accu
1099 if nogeomcmds state.geomcmds
1100 then (
1101 let accu =
1103 ~pageno:0
1104 ~pdimno:~-1
1105 ~prev:(0,0,0,0)
1106 ~py:0
1107 ~dy:0
1108 ~pdims:state.pdims
1109 ~accu:[]
1111 List.rev accu
1113 else
1117 let layoutN ((columns, coverA, coverB), b) y sh =
1118 let sh = sh - state.hscrollh in
1119 let rec fold accu n =
1120 if n = Array.length b
1121 then accu
1122 else
1123 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1124 if (vy - y) > sh &&
1125 (n = coverA - 1
1126 || n = state.pagecount - coverB
1127 || (n - coverA) mod columns = columns - 1)
1128 then accu
1129 else
1130 let accu =
1131 if vy + h > y
1132 then
1133 let pagey = max 0 (y - vy) in
1134 let pagedispy = if pagey > 0 then 0 else vy - y in
1135 let pagedispx, pagex, pagevw =
1136 let pdx =
1137 if n = coverA - 1 || n = state.pagecount - coverB
1138 then state.x + (conf.winw - state.scrollw - w) / 2
1139 else dx + xoff + state.x
1141 if pdx < 0
1142 then 0, -pdx, w + pdx
1143 else pdx, 0, min (conf.winw - state.scrollw) w
1145 let pagevh = min (h - pagey) (sh - pagedispy) in
1146 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1147 then
1148 let e =
1149 { pageno = n
1150 ; pagedimno = pdimno
1151 ; pagew = w
1152 ; pageh = h
1153 ; pagex = pagex
1154 ; pagey = pagey
1155 ; pagevw = pagevw
1156 ; pagevh = pagevh
1157 ; pagedispx = pagedispx
1158 ; pagedispy = pagedispy
1161 e :: accu
1162 else
1163 accu
1164 else
1165 accu
1167 fold accu (n+1)
1169 if nogeomcmds state.geomcmds
1170 then List.rev (fold [] 0)
1171 else []
1174 let layout y sh =
1175 match conf.columns with
1176 | None -> layout1 y sh
1177 | Some c -> layoutN c y sh
1180 let clamp incr =
1181 let y = state.y + incr in
1182 let y = max 0 y in
1183 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1187 let itertiles l f =
1188 let tilex = l.pagex mod conf.tilew in
1189 let tiley = l.pagey mod conf.tileh in
1191 let col = l.pagex / conf.tilew in
1192 let row = l.pagey / conf.tileh in
1194 let vw =
1195 let a = l.pagew - l.pagex in
1196 let b = conf.winw - state.scrollw in
1197 min a b
1198 and vh = l.pagevh in
1200 let rec rowloop row y0 dispy h =
1201 if h = 0
1202 then ()
1203 else (
1204 let dh = conf.tileh - y0 in
1205 let dh = min h dh in
1206 let rec colloop col x0 dispx w =
1207 if w = 0
1208 then ()
1209 else (
1210 let dw = conf.tilew - x0 in
1211 let dw = min w dw in
1213 f col row dispx dispy x0 y0 dw dh;
1214 colloop (col+1) 0 (dispx+dw) (w-dw)
1217 colloop col tilex l.pagedispx vw;
1218 rowloop (row+1) 0 (dispy+dh) (h-dh)
1221 if vw > 0 && vh > 0
1222 then rowloop row tiley l.pagedispy vh;
1225 let gettileopaque l col row =
1226 let key =
1227 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1229 try Some (Hashtbl.find state.tilemap key)
1230 with Not_found -> None
1233 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1234 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1235 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1238 let drawtiles l color =
1239 GlDraw.color color;
1240 let f col row x y tilex tiley w h =
1241 match gettileopaque l col row with
1242 | Some (opaque, _, t) ->
1243 let params = x, y, w, h, tilex, tiley in
1244 if conf.invert
1245 then (
1246 Gl.enable `blend;
1247 GlFunc.blend_func `zero `one_minus_src_color;
1249 drawtile params opaque;
1250 if conf.invert
1251 then Gl.disable `blend;
1252 if conf.debug
1253 then (
1254 let s = Printf.sprintf
1255 "%d[%d,%d] %f sec"
1256 l.pageno col row t
1258 let w = measurestr fstate.fontsize s in
1259 GlMisc.push_attrib [`current];
1260 GlDraw.color (0.0, 0.0, 0.0);
1261 GlDraw.rect
1262 (float (x-2), float (y-2))
1263 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1264 GlDraw.color (1.0, 1.0, 1.0);
1265 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1266 GlMisc.pop_attrib ();
1269 | _ ->
1270 let w =
1271 let lw = conf.winw - state.scrollw - x in
1272 min lw w
1273 and h =
1274 let lh = conf.winh - y in
1275 min lh h
1277 Gl.enable `texture_2d;
1278 begin match state.texid with
1279 | Some id ->
1280 GlTex.bind_texture `texture_2d id;
1281 let x0 = float x
1282 and y0 = float y
1283 and x1 = float (x+w)
1284 and y1 = float (y+h) in
1286 let tw = float w /. 64.0
1287 and th = float h /. 64.0 in
1288 let tx0 = float tilex /. 64.0
1289 and ty0 = float tiley /. 64.0 in
1290 let tx1 = tx0 +. tw
1291 and ty1 = ty0 +. th in
1292 GlDraw.begins `quads;
1293 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1294 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1295 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1296 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1297 GlDraw.ends ();
1299 Gl.disable `texture_2d;
1300 | None ->
1301 GlDraw.color (1.0, 1.0, 1.0);
1302 GlDraw.rect
1303 (float x, float y)
1304 (float (x+w), float (y+h));
1305 end;
1306 if w > 128 && h > fstate.fontsize + 10
1307 then (
1308 GlDraw.color (0.0, 0.0, 0.0);
1309 let c, r =
1310 if conf.verbose
1311 then (col*conf.tilew, row*conf.tileh)
1312 else col, row
1314 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1316 GlDraw.color color;
1318 itertiles l f
1321 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1323 let tilevisible1 l x y =
1324 let ax0 = l.pagex
1325 and ax1 = l.pagex + l.pagevw
1326 and ay0 = l.pagey
1327 and ay1 = l.pagey + l.pagevh in
1329 let bx0 = x
1330 and by0 = y in
1331 let bx1 = min (bx0 + conf.tilew) l.pagew
1332 and by1 = min (by0 + conf.tileh) l.pageh in
1334 let rx0 = max ax0 bx0
1335 and ry0 = max ay0 by0
1336 and rx1 = min ax1 bx1
1337 and ry1 = min ay1 by1 in
1339 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1340 nonemptyintersection
1343 let tilevisible layout n x y =
1344 let rec findpageinlayout = function
1345 | l :: _ when l.pageno = n -> tilevisible1 l x y
1346 | _ :: rest -> findpageinlayout rest
1347 | [] -> false
1349 findpageinlayout layout
1352 let tileready l x y =
1353 tilevisible1 l x y &&
1354 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1357 let tilepage n p layout =
1358 let rec loop = function
1359 | l :: rest ->
1360 if l.pageno = n
1361 then
1362 let f col row _ _ _ _ _ _ =
1363 if state.currently = Idle
1364 then
1365 match gettileopaque l col row with
1366 | Some _ -> ()
1367 | None ->
1368 let x = col*conf.tilew
1369 and y = row*conf.tileh in
1370 let w =
1371 let w = l.pagew - x in
1372 min w conf.tilew
1374 let h =
1375 let h = l.pageh - y in
1376 min h conf.tileh
1378 wcmd "tile %s %d %d %d %d" p x y w h;
1379 state.currently <-
1380 Tiling (
1381 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1382 conf.tilew, conf.tileh
1385 itertiles l f;
1386 else
1387 loop rest
1389 | [] -> ()
1391 if nogeomcmds state.geomcmds
1392 then loop layout;
1395 let preloadlayout visiblepages =
1396 let presentation = conf.presentation in
1397 let interpagespace = conf.interpagespace in
1398 let maxy = state.maxy in
1399 conf.presentation <- false;
1400 conf.interpagespace <- 0;
1401 state.maxy <- calcheight ();
1402 let y =
1403 match visiblepages with
1404 | [] -> 0
1405 | l :: _ -> getpagey l.pageno + l.pagey
1407 let y = if y < conf.winh then 0 else y - conf.winh in
1408 let h = state.y - y + conf.winh*3 in
1409 let pages = layout y h in
1410 conf.presentation <- presentation;
1411 conf.interpagespace <- interpagespace;
1412 state.maxy <- maxy;
1413 pages;
1416 let load pages =
1417 let rec loop pages =
1418 if state.currently != Idle
1419 then ()
1420 else
1421 match pages with
1422 | l :: rest ->
1423 begin match getopaque l.pageno with
1424 | None ->
1425 wcmd "page %d %d" l.pageno l.pagedimno;
1426 state.currently <- Loading (l, state.gen);
1427 | Some opaque ->
1428 tilepage l.pageno opaque pages;
1429 loop rest
1430 end;
1431 | _ -> ()
1433 if nogeomcmds state.geomcmds
1434 then loop pages
1437 let preload pages =
1438 load pages;
1439 if conf.preload && state.currently = Idle
1440 then load (preloadlayout pages);
1443 let layoutready layout =
1444 let rec fold all ls =
1445 all && match ls with
1446 | l :: rest ->
1447 let seen = ref false in
1448 let allvisible = ref true in
1449 let foo col row _ _ _ _ _ _ =
1450 seen := true;
1451 allvisible := !allvisible &&
1452 begin match gettileopaque l col row with
1453 | Some _ -> true
1454 | None -> false
1457 itertiles l foo;
1458 fold (!seen && !allvisible) rest
1459 | [] -> true
1461 let alltilesvisible = fold true layout in
1462 alltilesvisible;
1465 let gotoy y =
1466 let y = bound y 0 state.maxy in
1467 let y, layout, proceed =
1468 match conf.maxwait with
1469 | Some time when state.ghyll == noghyll ->
1470 begin match state.throttle with
1471 | None ->
1472 let layout = layout y conf.winh in
1473 let ready = layoutready layout in
1474 if not ready
1475 then (
1476 load layout;
1477 state.throttle <- Some (layout, y, now ());
1479 else G.postRedisplay "gotoy showall (None)";
1480 y, layout, ready
1481 | Some (_, _, started) ->
1482 let dt = now () -. started in
1483 if dt > time
1484 then (
1485 state.throttle <- None;
1486 let layout = layout y conf.winh in
1487 load layout;
1488 G.postRedisplay "maxwait";
1489 y, layout, true
1491 else -1, [], false
1494 | _ ->
1495 let layout = layout y conf.winh in
1496 if true || layoutready layout
1497 then G.postRedisplay "gotoy ready";
1498 y, layout, true
1500 if proceed
1501 then (
1502 state.y <- y;
1503 state.layout <- layout;
1504 begin match state.mode with
1505 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1506 if not (pagevisible layout pageno)
1507 then (
1508 match state.layout with
1509 | [] -> ()
1510 | l :: _ ->
1511 state.mode <- Birdseye (
1512 conf, leftx, l.pageno, hooverpageno, anchor
1515 | LinkNav (Ltgendir dir as lt) ->
1516 let linknav =
1517 let rec loop = function
1518 | [] -> lt
1519 | l :: rest ->
1520 match getopaque l.pageno with
1521 | None -> loop rest
1522 | Some opaque ->
1523 let link =
1524 let ld =
1525 if dir = 0
1526 then LDfirstvisible (l.pagex, l.pagey, dir)
1527 else (
1528 if dir > 0 then LDfirst else LDlast
1531 findlink opaque ld
1533 match link with
1534 | Lnotfound -> loop rest
1535 | Lfound (n, x0, y0, x1, y1) ->
1536 showlinktype (getlink opaque n);
1537 Ltexact ((l.pageno, n), (x0, y0, x1, y1))
1539 loop state.layout
1541 state.mode <- LinkNav linknav
1542 | _ -> ()
1543 end;
1544 preload layout;
1546 state.ghyll <- noghyll;
1547 if conf.updatecurs
1548 then (
1549 let mx, my = state.mpos in
1550 updateunder mx my;
1554 let conttiling pageno opaque =
1555 tilepage pageno opaque
1556 (if conf.preload then preloadlayout state.layout else state.layout)
1559 let gotoy_and_clear_text y =
1560 if not conf.verbose then state.text <- "";
1561 gotoy y;
1564 let getanchor () =
1565 match state.layout with
1566 | [] -> emptyanchor
1567 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1570 let getanchory (n, top) =
1571 let y, h = getpageyh n in
1572 y + (truncate (top *. float h));
1575 let gotoanchor anchor =
1576 gotoy (getanchory anchor);
1579 let addnav () =
1580 cbput state.hists.nav (getanchor ());
1583 let getnav dir =
1584 let anchor = cbgetc state.hists.nav dir in
1585 getanchory anchor;
1588 let gotoghyll y =
1589 let rec scroll f n a b =
1590 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1591 let snake f a b =
1592 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1593 if f < a
1594 then s (float f /. float a)
1595 else (
1596 if f > b
1597 then 1.0 -. s ((float (f-b) /. float (n-b)))
1598 else 1.0
1601 snake f a b
1602 and summa f n a b =
1603 (* courtesy:
1604 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1605 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1606 let iv1 = iv f in
1607 let ins = float a *. iv1
1608 and outs = float (n-b) *. iv1 in
1609 let ones = b - a in
1610 ins +. outs +. float ones
1612 let rec set (_N, _A, _B) y sy =
1613 let sum = summa 1.0 _N _A _B in
1614 let dy = float (y - sy) in
1615 state.ghyll <- (
1616 let rec gf n y1 o =
1617 if n >= _N
1618 then state.ghyll <- noghyll
1619 else
1620 let go n =
1621 let s = scroll n _N _A _B in
1622 let y1 = y1 +. ((s *. dy) /. sum) in
1623 gotoy_and_clear_text (truncate y1);
1624 state.ghyll <- gf (n+1) y1;
1626 match o with
1627 | None -> go n
1628 | Some y' -> set (_N/2, 0, 0) y' state.y
1630 gf 0 (float state.y)
1633 match conf.ghyllscroll with
1634 | None ->
1635 gotoy_and_clear_text y
1636 | Some nab ->
1637 if state.ghyll == noghyll
1638 then set nab y state.y
1639 else state.ghyll (Some y)
1642 let gotopage n top =
1643 let y, h = getpageyh n in
1644 let y = y + (truncate (top *. float h)) in
1645 gotoghyll y
1648 let gotopage1 n top =
1649 let y = getpagey n in
1650 let y = y + top in
1651 gotoghyll y
1654 let invalidate s f =
1655 state.layout <- [];
1656 state.pdims <- [];
1657 state.rects <- [];
1658 state.rects1 <- [];
1659 match state.geomcmds with
1660 | ps, [] when String.length ps = 0 ->
1661 f ();
1662 state.geomcmds <- s, [];
1664 | ps, [] ->
1665 state.geomcmds <- ps, [s, f];
1667 | ps, (s', _) :: rest when s' = s ->
1668 state.geomcmds <- ps, ((s, f) :: rest);
1670 | ps, cmds ->
1671 state.geomcmds <- ps, ((s, f) :: cmds);
1674 let opendoc path password =
1675 state.path <- path;
1676 state.password <- password;
1677 state.gen <- state.gen + 1;
1678 state.docinfo <- [];
1680 setaalevel conf.aalevel;
1681 Wsi.settitle ("llpp " ^ Filename.basename path);
1682 wcmd "open %s\000%s\000" path password;
1683 invalidate "reqlayout"
1684 (fun () ->
1685 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1688 let scalecolor c =
1689 let c = c *. conf.colorscale in
1690 (c, c, c);
1693 let scalecolor2 (r, g, b) =
1694 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1697 let represent () =
1698 let docolumns = function
1699 | None -> ()
1700 | Some ((columns, coverA, coverB), _) ->
1701 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1702 let rec loop pageno pdimno pdim x y rowh pdims =
1703 if pageno = state.pagecount
1704 then ()
1705 else
1706 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1707 match pdims with
1708 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1709 pdimno+1, pdim, rest
1710 | _ ->
1711 pdimno, pdim, pdims
1713 let x, y, rowh' =
1714 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1715 then (
1716 (conf.winw - state.scrollw - w) / 2,
1717 y + rowh + conf.interpagespace, h
1719 else (
1720 if (pageno - coverA) mod columns = 0
1721 then 0, y + rowh + conf.interpagespace, h
1722 else x, y, max rowh h
1725 let rec fixrow m = if m = pageno then () else
1726 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1727 if h < rowh
1728 then (
1729 let y = y + (rowh - h) / 2 in
1730 a.(m) <- (pdimno, x, y, pdim);
1732 fixrow (m+1)
1734 if pageno > 1 && (pageno - coverA) mod columns = 0
1735 then fixrow (pageno - columns);
1736 a.(pageno) <- (pdimno, x, y, pdim);
1737 let x = x + w + xoff*2 + conf.interpagespace in
1738 loop (pageno+1) pdimno pdim x y rowh' pdims
1740 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1741 conf.columns <- Some ((columns, coverA, coverB), a);
1743 docolumns conf.columns;
1744 state.maxy <- calcheight ();
1745 state.hscrollh <-
1746 if state.w <= conf.winw - state.scrollw
1747 then 0
1748 else state.scrollw
1750 match state.mode with
1751 | Birdseye (_, _, pageno, _, _) ->
1752 let y, h = getpageyh pageno in
1753 let top = (conf.winh - h) / 2 in
1754 gotoy (max 0 (y - top))
1755 | _ -> gotoanchor state.anchor
1758 let reshape w h =
1759 GlDraw.viewport 0 0 w h;
1760 if state.geomcmds != firstgeomcmds && nogeomcmds state.geomcmds
1761 then state.anchor <- getanchor ();
1763 conf.winw <- w;
1764 let w = truncate (float w *. conf.zoom) - state.scrollw in
1765 let w = max w 2 in
1766 conf.winh <- h;
1767 setfontsize fstate.fontsize;
1768 GlMat.mode `modelview;
1769 GlMat.load_identity ();
1771 GlMat.mode `projection;
1772 GlMat.load_identity ();
1773 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1774 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1775 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1777 invalidate "geometry"
1778 (fun () ->
1779 state.w <- w;
1780 let w =
1781 match conf.columns with
1782 | None -> w
1783 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1785 wcmd "geometry %d %d" w h);
1788 let enttext () =
1789 let len = String.length state.text in
1790 let drawstring s =
1791 let hscrollh =
1792 match state.mode with
1793 | View -> state.hscrollh
1794 | _ -> 0
1796 let rect x w =
1797 GlDraw.rect
1798 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1799 (x+.w, float (conf.winh - hscrollh))
1802 let w = float (conf.winw - state.scrollw - 1) in
1803 if state.progress >= 0.0 && state.progress < 1.0
1804 then (
1805 GlDraw.color (0.3, 0.3, 0.3);
1806 let w1 = w *. state.progress in
1807 rect 0.0 w1;
1808 GlDraw.color (0.0, 0.0, 0.0);
1809 rect w1 (w-.w1)
1811 else (
1812 GlDraw.color (0.0, 0.0, 0.0);
1813 rect 0.0 w;
1816 GlDraw.color (1.0, 1.0, 1.0);
1817 drawstring fstate.fontsize
1818 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1820 let s =
1821 match state.mode with
1822 | Textentry ((prefix, text, _, _, _), _) ->
1823 let s =
1824 if len > 0
1825 then
1826 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1827 else
1828 Printf.sprintf "%s%s_" prefix text
1832 | _ -> state.text
1834 let s =
1835 if state.newerrmsgs
1836 then (
1837 if not (istextentry state.mode)
1838 then
1839 let s1 = "(press 'e' to review error messasges)" in
1840 if String.length s > 0 then s ^ " " ^ s1 else s1
1841 else s
1843 else s
1845 if String.length s > 0
1846 then drawstring s
1849 let gctiles () =
1850 let len = Queue.length state.tilelru in
1851 let rec loop qpos =
1852 if state.memused <= conf.memlimit
1853 then ()
1854 else (
1855 if qpos < len
1856 then
1857 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1858 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1859 let (_, pw, ph, _) = getpagedim n in
1861 gen = state.gen
1862 && colorspace = conf.colorspace
1863 && angle = conf.angle
1864 && pagew = pw
1865 && pageh = ph
1866 && (
1867 let layout =
1868 match state.throttle with
1869 | None ->
1870 if conf.preload
1871 then preloadlayout state.layout
1872 else state.layout
1873 | Some (layout, _, _) ->
1874 layout
1876 let x = col*conf.tilew
1877 and y = row*conf.tileh in
1878 tilevisible layout n x y
1880 then Queue.push lruitem state.tilelru
1881 else (
1882 wcmd "freetile %s" p;
1883 state.memused <- state.memused - s;
1884 state.uioh#infochanged Memused;
1885 Hashtbl.remove state.tilemap k;
1887 loop (qpos+1)
1890 loop 0
1893 let flushtiles () =
1894 Queue.iter (fun (k, p, s) ->
1895 wcmd "freetile %s" p;
1896 state.memused <- state.memused - s;
1897 state.uioh#infochanged Memused;
1898 Hashtbl.remove state.tilemap k;
1899 ) state.tilelru;
1900 Queue.clear state.tilelru;
1901 load state.layout;
1904 let logcurrently = function
1905 | Idle -> dolog "Idle"
1906 | Loading (l, gen) ->
1907 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1908 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1909 dolog
1910 "Tiling %d[%d,%d] page=%s cs=%s angle"
1911 l.pageno col row pageopaque
1912 (colorspace_to_string colorspace)
1914 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1915 angle gen conf.angle state.gen
1916 tilew tileh
1917 conf.tilew conf.tileh
1919 | Outlining _ ->
1920 dolog "outlining"
1923 let act cmds =
1924 (* dolog "%S" cmds; *)
1925 let op, args =
1926 let spacepos =
1927 try String.index cmds ' '
1928 with Not_found -> -1
1930 if spacepos = -1
1931 then cmds, ""
1932 else
1933 let l = String.length cmds in
1934 let op = String.sub cmds 0 spacepos in
1935 op, begin
1936 if l - spacepos < 2 then ""
1937 else String.sub cmds (spacepos+1) (l-spacepos-1)
1940 match op with
1941 | "clear" ->
1942 state.uioh#infochanged Pdim;
1943 state.pdims <- [];
1945 | "clearrects" ->
1946 state.rects <- state.rects1;
1947 G.postRedisplay "clearrects";
1949 | "continue" ->
1950 let n =
1951 try Scanf.sscanf args "%u" (fun n -> n)
1952 with exn ->
1953 dolog "error processing 'continue' %S: %s"
1954 cmds (Printexc.to_string exn);
1955 exit 1;
1957 state.pagecount <- n;
1958 begin match state.currently with
1959 | Outlining l ->
1960 state.currently <- Idle;
1961 state.outlines <- Array.of_list (List.rev l)
1962 | _ -> ()
1963 end;
1965 let cur, cmds = state.geomcmds in
1966 if String.length cur = 0
1967 then failwith "umpossible";
1969 begin match List.rev cmds with
1970 | [] ->
1971 state.geomcmds <- "", [];
1972 represent ();
1973 | (s, f) :: rest ->
1974 f ();
1975 state.geomcmds <- s, List.rev rest;
1976 end;
1977 if conf.maxwait = None
1978 then G.postRedisplay "continue";
1980 | "title" ->
1981 Wsi.settitle args
1983 | "msg" ->
1984 showtext ' ' args
1986 | "vmsg" ->
1987 if conf.verbose
1988 then showtext ' ' args
1990 | "progress" ->
1991 let progress, text =
1993 Scanf.sscanf args "%f %n"
1994 (fun f pos ->
1995 f, String.sub args pos (String.length args - pos))
1996 with exn ->
1997 dolog "error processing 'progress' %S: %s"
1998 cmds (Printexc.to_string exn);
1999 exit 1;
2001 state.text <- text;
2002 state.progress <- progress;
2003 G.postRedisplay "progress"
2005 | "firstmatch" ->
2006 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2008 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2009 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2010 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2011 with exn ->
2012 dolog "error processing 'firstmatch' %S: %s"
2013 cmds (Printexc.to_string exn);
2014 exit 1;
2016 let y = (getpagey pageno) + truncate y0 in
2017 addnav ();
2018 gotoy y;
2019 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2021 | "match" ->
2022 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2024 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2025 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2026 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2027 with exn ->
2028 dolog "error processing 'match' %S: %s"
2029 cmds (Printexc.to_string exn);
2030 exit 1;
2032 state.rects1 <-
2033 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2035 | "page" ->
2036 let pageopaque, t =
2038 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2039 with exn ->
2040 dolog "error processing 'page' %S: %s"
2041 cmds (Printexc.to_string exn);
2042 exit 1;
2044 begin match state.currently with
2045 | Loading (l, gen) ->
2046 vlog "page %d took %f sec" l.pageno t;
2047 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2048 begin match state.throttle with
2049 | None ->
2050 let preloadedpages =
2051 if conf.preload
2052 then preloadlayout state.layout
2053 else state.layout
2055 let evict () =
2056 let module IntSet =
2057 Set.Make (struct type t = int let compare = (-) end) in
2058 let set =
2059 List.fold_left (fun s l -> IntSet.add l.pageno s)
2060 IntSet.empty preloadedpages
2062 let evictedpages =
2063 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2064 if not (IntSet.mem pageno set)
2065 then (
2066 wcmd "freepage %s" opaque;
2067 key :: accu
2069 else accu
2070 ) state.pagemap []
2072 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2074 evict ();
2075 state.currently <- Idle;
2076 if gen = state.gen
2077 then (
2078 tilepage l.pageno pageopaque state.layout;
2079 load state.layout;
2080 load preloadedpages;
2081 if pagevisible state.layout l.pageno
2082 && layoutready state.layout
2083 then G.postRedisplay "page";
2086 | Some (layout, _, _) ->
2087 state.currently <- Idle;
2088 tilepage l.pageno pageopaque layout;
2089 load state.layout
2090 end;
2092 | _ ->
2093 dolog "Inconsistent loading state";
2094 logcurrently state.currently;
2095 exit 1
2098 | "tile" ->
2099 let (x, y, opaque, size, t) =
2101 Scanf.sscanf args "%u %u %s %u %f"
2102 (fun x y p size t -> (x, y, p, size, t))
2103 with exn ->
2104 dolog "error processing 'tile' %S: %s"
2105 cmds (Printexc.to_string exn);
2106 exit 1;
2108 begin match state.currently with
2109 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2110 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2112 if tilew != conf.tilew || tileh != conf.tileh
2113 then (
2114 wcmd "freetile %s" opaque;
2115 state.currently <- Idle;
2116 load state.layout;
2118 else (
2119 puttileopaque l col row gen cs angle opaque size t;
2120 state.memused <- state.memused + size;
2121 state.uioh#infochanged Memused;
2122 gctiles ();
2123 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2124 opaque, size) state.tilelru;
2126 let layout =
2127 match state.throttle with
2128 | None -> state.layout
2129 | Some (layout, _, _) -> layout
2132 state.currently <- Idle;
2133 if gen = state.gen
2134 && conf.colorspace = cs
2135 && conf.angle = angle
2136 && tilevisible layout l.pageno x y
2137 then conttiling l.pageno pageopaque;
2139 begin match state.throttle with
2140 | None ->
2141 preload state.layout;
2142 if gen = state.gen
2143 && conf.colorspace = cs
2144 && conf.angle = angle
2145 && tilevisible state.layout l.pageno x y
2146 then G.postRedisplay "tile nothrottle";
2148 | Some (layout, y, _) ->
2149 let ready = layoutready layout in
2150 if ready
2151 then (
2152 state.y <- y;
2153 state.layout <- layout;
2154 state.throttle <- None;
2155 G.postRedisplay "throttle";
2157 else load layout;
2158 end;
2161 | _ ->
2162 dolog "Inconsistent tiling state";
2163 logcurrently state.currently;
2164 exit 1
2167 | "pdim" ->
2168 let pdim =
2170 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2171 with exn ->
2172 dolog "error processing 'pdim' %S: %s"
2173 cmds (Printexc.to_string exn);
2174 exit 1;
2176 state.uioh#infochanged Pdim;
2177 state.pdims <- pdim :: state.pdims
2179 | "o" ->
2180 let (l, n, t, h, pos) =
2182 Scanf.sscanf args "%u %u %d %u %n"
2183 (fun l n t h pos -> l, n, t, h, pos)
2184 with exn ->
2185 dolog "error processing 'o' %S: %s"
2186 cmds (Printexc.to_string exn);
2187 exit 1;
2189 let s = String.sub args pos (String.length args - pos) in
2190 let outline = (s, l, (n, float t /. float h)) in
2191 begin match state.currently with
2192 | Outlining outlines ->
2193 state.currently <- Outlining (outline :: outlines)
2194 | Idle ->
2195 state.currently <- Outlining [outline]
2196 | currently ->
2197 dolog "invalid outlining state";
2198 logcurrently currently
2201 | "info" ->
2202 state.docinfo <- (1, args) :: state.docinfo
2204 | "infoend" ->
2205 state.uioh#infochanged Docinfo;
2206 state.docinfo <- List.rev state.docinfo
2208 | _ ->
2209 dolog "unknown cmd `%S'" cmds
2212 let onhist cb =
2213 let rc = cb.rc in
2214 let action = function
2215 | HCprev -> cbget cb ~-1
2216 | HCnext -> cbget cb 1
2217 | HCfirst -> cbget cb ~-(cb.rc)
2218 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2219 and cancel () = cb.rc <- rc
2220 in (action, cancel)
2223 let search pattern forward =
2224 if String.length pattern > 0
2225 then
2226 let pn, py =
2227 match state.layout with
2228 | [] -> 0, 0
2229 | l :: _ ->
2230 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2232 wcmd "search %d %d %d %d,%s\000"
2233 (btod conf.icase) pn py (btod forward) pattern;
2236 let intentry text key =
2237 let c =
2238 if key >= 32 && key < 127
2239 then Char.chr key
2240 else '\000'
2242 match c with
2243 | '0' .. '9' ->
2244 let text = addchar text c in
2245 TEcont text
2247 | _ ->
2248 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2249 TEcont text
2252 let textentry text key =
2253 if key land 0xff00 = 0xff00
2254 then TEcont text
2255 else TEcont (text ^ Wsi.toutf8 key)
2258 let reqlayout angle proportional =
2259 match state.throttle with
2260 | None ->
2261 if nogeomcmds state.geomcmds
2262 then state.anchor <- getanchor ();
2263 conf.angle <- angle mod 360;
2264 if conf.angle != 0
2265 then (
2266 match state.mode with
2267 | LinkNav _ -> state.mode <- View
2268 | _ -> ()
2270 conf.proportional <- proportional;
2271 invalidate "reqlayout"
2272 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2273 | _ -> ()
2276 let settrim trimmargins trimfuzz =
2277 if nogeomcmds state.geomcmds
2278 then state.anchor <- getanchor ();
2279 conf.trimmargins <- trimmargins;
2280 conf.trimfuzz <- trimfuzz;
2281 let x0, y0, x1, y1 = trimfuzz in
2282 invalidate "settrim"
2283 (fun () ->
2284 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2285 Hashtbl.iter (fun _ opaque ->
2286 wcmd "freepage %s" opaque;
2287 ) state.pagemap;
2288 Hashtbl.clear state.pagemap;
2291 let setzoom zoom =
2292 match state.throttle with
2293 | None ->
2294 let zoom = max 0.01 zoom in
2295 if zoom <> conf.zoom
2296 then (
2297 state.prevzoom <- conf.zoom;
2298 let relx =
2299 if zoom <= 1.0
2300 then (state.x <- 0; 0.0)
2301 else float state.x /. float state.w
2303 conf.zoom <- zoom;
2304 reshape conf.winw conf.winh;
2305 if zoom > 1.0
2306 then (
2307 let x = relx *. float state.w in
2308 state.x <- truncate x;
2310 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2313 | Some (layout, y, started) ->
2314 let time =
2315 match conf.maxwait with
2316 | None -> 0.0
2317 | Some t -> t
2319 let dt = now () -. started in
2320 if dt > time
2321 then (
2322 state.y <- y;
2323 load layout;
2327 let setcolumns columns coverA coverB =
2328 if columns < 2
2329 then (
2330 conf.columns <- None;
2331 state.x <- 0;
2332 setzoom 1.0;
2334 else (
2335 conf.columns <- Some ((columns, coverA, coverB), [||]);
2336 conf.zoom <- 1.0;
2338 reshape conf.winw conf.winh;
2341 let enterbirdseye () =
2342 let zoom = float conf.thumbw /. float conf.winw in
2343 let birdseyepageno =
2344 let cy = conf.winh / 2 in
2345 let fold = function
2346 | [] -> 0
2347 | l :: rest ->
2348 let rec fold best = function
2349 | [] -> best.pageno
2350 | l :: rest ->
2351 let d = cy - (l.pagedispy + l.pagevh/2)
2352 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2353 if abs d < abs dbest
2354 then fold l rest
2355 else best.pageno
2356 in fold l rest
2358 fold state.layout
2360 state.mode <- Birdseye (
2361 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2363 conf.zoom <- zoom;
2364 conf.presentation <- false;
2365 conf.interpagespace <- 10;
2366 conf.hlinks <- false;
2367 state.x <- 0;
2368 state.mstate <- Mnone;
2369 conf.maxwait <- None;
2370 conf.columns <- (
2371 match conf.beyecolumns with
2372 | Some c ->
2373 conf.zoom <- 1.0;
2374 Some ((c, 0, 0), [||])
2375 | None -> None
2377 Wsi.setcursor Wsi.CURSOR_INHERIT;
2378 if conf.verbose
2379 then
2380 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2381 (100.0*.zoom)
2382 else
2383 state.text <- ""
2385 reshape conf.winw conf.winh;
2388 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2389 state.mode <- View;
2390 conf.zoom <- c.zoom;
2391 conf.presentation <- c.presentation;
2392 conf.interpagespace <- c.interpagespace;
2393 conf.maxwait <- c.maxwait;
2394 conf.hlinks <- c.hlinks;
2395 conf.beyecolumns <- (
2396 match conf.columns with
2397 | Some ((c, _, _), _) -> Some c
2398 | None -> None
2400 conf.columns <- (
2401 match c.columns with
2402 | Some (c, _) -> Some (c, [||])
2403 | None -> None
2405 state.x <- leftx;
2406 if conf.verbose
2407 then
2408 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2409 (100.0*.conf.zoom)
2411 reshape conf.winw conf.winh;
2412 state.anchor <- if goback then anchor else (pageno, 0.0);
2415 let togglebirdseye () =
2416 match state.mode with
2417 | Birdseye vals -> leavebirdseye vals true
2418 | View -> enterbirdseye ()
2419 | _ -> ()
2422 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2423 let pageno = max 0 (pageno - incr) in
2424 let rec loop = function
2425 | [] -> gotopage1 pageno 0
2426 | l :: _ when l.pageno = pageno ->
2427 if l.pagedispy >= 0 && l.pagey = 0
2428 then G.postRedisplay "upbirdseye"
2429 else gotopage1 pageno 0
2430 | _ :: rest -> loop rest
2432 loop state.layout;
2433 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2436 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2437 let pageno = min (state.pagecount - 1) (pageno + incr) in
2438 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2439 let rec loop = function
2440 | [] ->
2441 let y, h = getpageyh pageno in
2442 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2443 gotoy (clamp dy)
2444 | l :: _ when l.pageno = pageno ->
2445 if l.pagevh != l.pageh
2446 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2447 else G.postRedisplay "downbirdseye"
2448 | _ :: rest -> loop rest
2450 loop state.layout
2453 let optentry mode _ key =
2454 let btos b = if b then "on" else "off" in
2455 if key >= 32 && key < 127
2456 then
2457 let c = Char.chr key in
2458 match c with
2459 | 's' ->
2460 let ondone s =
2461 try conf.scrollstep <- int_of_string s with exc ->
2462 state.text <- Printf.sprintf "bad integer `%s': %s"
2463 s (Printexc.to_string exc)
2465 TEswitch ("scroll step: ", "", None, intentry, ondone)
2467 | 'A' ->
2468 let ondone s =
2470 conf.autoscrollstep <- int_of_string s;
2471 if state.autoscroll <> None
2472 then state.autoscroll <- Some conf.autoscrollstep
2473 with exc ->
2474 state.text <- Printf.sprintf "bad integer `%s': %s"
2475 s (Printexc.to_string exc)
2477 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2479 | 'C' ->
2480 let ondone s =
2482 let n, a, b = columns_of_string s in
2483 setcolumns n a b;
2484 with exc ->
2485 state.text <- Printf.sprintf "bad columns `%s': %s"
2486 s (Printexc.to_string exc)
2488 TEswitch ("columns: ", "", None, textentry, ondone)
2490 | 'Z' ->
2491 let ondone s =
2493 let zoom = float (int_of_string s) /. 100.0 in
2494 setzoom zoom
2495 with exc ->
2496 state.text <- Printf.sprintf "bad integer `%s': %s"
2497 s (Printexc.to_string exc)
2499 TEswitch ("zoom: ", "", None, intentry, ondone)
2501 | 't' ->
2502 let ondone s =
2504 conf.thumbw <- bound (int_of_string s) 2 4096;
2505 state.text <-
2506 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2507 begin match mode with
2508 | Birdseye beye ->
2509 leavebirdseye beye false;
2510 enterbirdseye ();
2511 | _ -> ();
2513 with exc ->
2514 state.text <- Printf.sprintf "bad integer `%s': %s"
2515 s (Printexc.to_string exc)
2517 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2519 | 'R' ->
2520 let ondone s =
2521 match try
2522 Some (int_of_string s)
2523 with exc ->
2524 state.text <- Printf.sprintf "bad integer `%s': %s"
2525 s (Printexc.to_string exc);
2526 None
2527 with
2528 | Some angle -> reqlayout angle conf.proportional
2529 | None -> ()
2531 TEswitch ("rotation: ", "", None, intentry, ondone)
2533 | 'i' ->
2534 conf.icase <- not conf.icase;
2535 TEdone ("case insensitive search " ^ (btos conf.icase))
2537 | 'p' ->
2538 conf.preload <- not conf.preload;
2539 gotoy state.y;
2540 TEdone ("preload " ^ (btos conf.preload))
2542 | 'v' ->
2543 conf.verbose <- not conf.verbose;
2544 TEdone ("verbose " ^ (btos conf.verbose))
2546 | 'd' ->
2547 conf.debug <- not conf.debug;
2548 TEdone ("debug " ^ (btos conf.debug))
2550 | 'h' ->
2551 conf.maxhfit <- not conf.maxhfit;
2552 state.maxy <-
2553 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2554 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2556 | 'c' ->
2557 conf.crophack <- not conf.crophack;
2558 TEdone ("crophack " ^ btos conf.crophack)
2560 | 'a' ->
2561 let s =
2562 match conf.maxwait with
2563 | None ->
2564 conf.maxwait <- Some infinity;
2565 "always wait for page to complete"
2566 | Some _ ->
2567 conf.maxwait <- None;
2568 "show placeholder if page is not ready"
2570 TEdone s
2572 | 'f' ->
2573 conf.underinfo <- not conf.underinfo;
2574 TEdone ("underinfo " ^ btos conf.underinfo)
2576 | 'P' ->
2577 conf.savebmarks <- not conf.savebmarks;
2578 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2580 | 'S' ->
2581 let ondone s =
2583 let pageno, py =
2584 match state.layout with
2585 | [] -> 0, 0
2586 | l :: _ ->
2587 l.pageno, l.pagey
2589 conf.interpagespace <- int_of_string s;
2590 state.maxy <- calcheight ();
2591 let y = getpagey pageno in
2592 gotoy (y + py)
2593 with exc ->
2594 state.text <- Printf.sprintf "bad integer `%s': %s"
2595 s (Printexc.to_string exc)
2597 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2599 | 'l' ->
2600 reqlayout conf.angle (not conf.proportional);
2601 TEdone ("proportional display " ^ btos conf.proportional)
2603 | 'T' ->
2604 settrim (not conf.trimmargins) conf.trimfuzz;
2605 TEdone ("trim margins " ^ btos conf.trimmargins)
2607 | 'I' ->
2608 conf.invert <- not conf.invert;
2609 TEdone ("invert colors " ^ btos conf.invert)
2611 | 'x' ->
2612 let ondone s =
2613 cbput state.hists.sel s;
2614 conf.selcmd <- s;
2616 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2617 textentry, ondone)
2619 | _ ->
2620 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2621 TEstop
2622 else
2623 TEcont state.text
2626 class type lvsource = object
2627 method getitemcount : int
2628 method getitem : int -> (string * int)
2629 method hasaction : int -> bool
2630 method exit :
2631 uioh:uioh ->
2632 cancel:bool ->
2633 active:int ->
2634 first:int ->
2635 pan:int ->
2636 qsearch:string ->
2637 uioh option
2638 method getactive : int
2639 method getfirst : int
2640 method getqsearch : string
2641 method setqsearch : string -> unit
2642 method getpan : int
2643 end;;
2645 class virtual lvsourcebase = object
2646 val mutable m_active = 0
2647 val mutable m_first = 0
2648 val mutable m_qsearch = ""
2649 val mutable m_pan = 0
2650 method getactive = m_active
2651 method getfirst = m_first
2652 method getqsearch = m_qsearch
2653 method getpan = m_pan
2654 method setqsearch s = m_qsearch <- s
2655 end;;
2657 let withoutlastutf8 s =
2658 let len = String.length s in
2659 if len = 0
2660 then s
2661 else
2662 let rec find pos =
2663 if pos = 0
2664 then pos
2665 else
2666 let b = Char.code s.[pos] in
2667 if b land 0b110000 = 0b11000000
2668 then find (pos-1)
2669 else pos-1
2671 let first =
2672 if Char.code s.[len-1] land 0x80 = 0
2673 then len-1
2674 else find (len-1)
2676 String.sub s 0 first;
2679 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2680 let enttext te =
2681 state.mode <- Textentry (te, onleave);
2682 state.text <- "";
2683 enttext ();
2684 G.postRedisplay "textentrykeyboard enttext";
2686 let histaction cmd =
2687 match opthist with
2688 | None -> ()
2689 | Some (action, _) ->
2690 state.mode <- Textentry (
2691 (c, action cmd, opthist, onkey, ondone), onleave
2693 G.postRedisplay "textentry histaction"
2695 match key with
2696 | 0xff08 -> (* backspace *)
2697 let s = withoutlastutf8 text in
2698 let len = String.length s in
2699 if len = 0
2700 then (
2701 onleave Cancel;
2702 G.postRedisplay "textentrykeyboard after cancel";
2704 else (
2705 enttext (c, s, opthist, onkey, ondone)
2708 | 0xff0d ->
2709 ondone text;
2710 onleave Confirm;
2711 G.postRedisplay "textentrykeyboard after confirm"
2713 | 0xff52 -> histaction HCprev
2714 | 0xff54 -> histaction HCnext
2715 | 0xff50 -> histaction HCfirst
2716 | 0xff57 -> histaction HClast
2718 | 0xff1b -> (* escape*)
2719 if String.length text = 0
2720 then (
2721 begin match opthist with
2722 | None -> ()
2723 | Some (_, onhistcancel) -> onhistcancel ()
2724 end;
2725 onleave Cancel;
2726 state.text <- "";
2727 G.postRedisplay "textentrykeyboard after cancel2"
2729 else (
2730 enttext (c, "", opthist, onkey, ondone)
2733 | 0xff9f | 0xffff -> () (* delete *)
2735 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2736 begin match onkey text key with
2737 | TEdone text ->
2738 ondone text;
2739 onleave Confirm;
2740 G.postRedisplay "textentrykeyboard after confirm2";
2742 | TEcont text ->
2743 enttext (c, text, opthist, onkey, ondone);
2745 | TEstop ->
2746 onleave Cancel;
2747 G.postRedisplay "textentrykeyboard after cancel3"
2749 | TEswitch te ->
2750 state.mode <- Textentry (te, onleave);
2751 G.postRedisplay "textentrykeyboard switch";
2752 end;
2754 | _ ->
2755 vlog "unhandled key %s" (Wsi.keyname key)
2758 let firstof first active =
2759 if first > active || abs (first - active) > fstate.maxrows - 1
2760 then max 0 (active - (fstate.maxrows/2))
2761 else first
2764 let calcfirst first active =
2765 if active > first
2766 then
2767 let rows = active - first in
2768 if rows > fstate.maxrows then active - fstate.maxrows else first
2769 else active
2772 let scrollph y maxy =
2773 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2774 let sh = float conf.winh /. sh in
2775 let sh = max sh (float conf.scrollh) in
2777 let percent =
2778 if y = state.maxy
2779 then 1.0
2780 else float y /. float maxy
2782 let position = (float conf.winh -. sh) *. percent in
2784 let position =
2785 if position +. sh > float conf.winh
2786 then float conf.winh -. sh
2787 else position
2789 position, sh;
2792 let coe s = (s :> uioh);;
2794 class listview ~(source:lvsource) ~trusted ~modehash =
2795 object (self)
2796 val m_pan = source#getpan
2797 val m_first = source#getfirst
2798 val m_active = source#getactive
2799 val m_qsearch = source#getqsearch
2800 val m_prev_uioh = state.uioh
2802 method private elemunder y =
2803 let n = y / (fstate.fontsize+1) in
2804 if m_first + n < source#getitemcount
2805 then (
2806 if source#hasaction (m_first + n)
2807 then Some (m_first + n)
2808 else None
2810 else None
2812 method display =
2813 Gl.enable `blend;
2814 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2815 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2816 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2817 GlDraw.color (1., 1., 1.);
2818 Gl.enable `texture_2d;
2819 let fs = fstate.fontsize in
2820 let nfs = fs + 1 in
2821 let ww = fstate.wwidth in
2822 let tabw = 30.0*.ww in
2823 let itemcount = source#getitemcount in
2824 let rec loop row =
2825 if (row - m_first) * nfs > conf.winh
2826 then ()
2827 else (
2828 if row >= 0 && row < itemcount
2829 then (
2830 let (s, level) = source#getitem row in
2831 let y = (row - m_first) * nfs in
2832 let x = 5.0 +. float (level + m_pan) *. ww in
2833 if row = m_active
2834 then (
2835 Gl.disable `texture_2d;
2836 GlDraw.polygon_mode `both `line;
2837 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2838 GlDraw.rect (1., float (y + 1))
2839 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2840 GlDraw.polygon_mode `both `fill;
2841 GlDraw.color (1., 1., 1.);
2842 Gl.enable `texture_2d;
2845 let drawtabularstring s =
2846 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2847 if trusted
2848 then
2849 let tabpos = try String.index s '\t' with Not_found -> -1 in
2850 if tabpos > 0
2851 then
2852 let len = String.length s - tabpos - 1 in
2853 let s1 = String.sub s 0 tabpos
2854 and s2 = String.sub s (tabpos + 1) len in
2855 let nx = drawstr x s1 in
2856 let sw = nx -. x in
2857 let x = x +. (max tabw sw) in
2858 drawstr x s2
2859 else
2860 drawstr x s
2861 else
2862 drawstr x s
2864 let _ = drawtabularstring s in
2865 loop (row+1)
2869 loop m_first;
2870 Gl.disable `blend;
2871 Gl.disable `texture_2d;
2873 method updownlevel incr =
2874 let len = source#getitemcount in
2875 let curlevel =
2876 if m_active >= 0 && m_active < len
2877 then snd (source#getitem m_active)
2878 else -1
2880 let rec flow i =
2881 if i = len then i-1 else if i = -1 then 0 else
2882 let _, l = source#getitem i in
2883 if l != curlevel then i else flow (i+incr)
2885 let active = flow m_active in
2886 let first = calcfirst m_first active in
2887 G.postRedisplay "outline updownlevel";
2888 {< m_active = active; m_first = first >}
2890 method private key1 key mask =
2891 let set1 active first qsearch =
2892 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2894 let search active pattern incr =
2895 let dosearch re =
2896 let rec loop n =
2897 if n >= 0 && n < source#getitemcount
2898 then (
2899 let s, _ = source#getitem n in
2901 (try ignore (Str.search_forward re s 0); true
2902 with Not_found -> false)
2903 then Some n
2904 else loop (n + incr)
2906 else None
2908 loop active
2911 let re = Str.regexp_case_fold pattern in
2912 dosearch re
2913 with Failure s ->
2914 state.text <- s;
2915 None
2917 let itemcount = source#getitemcount in
2918 let find start incr =
2919 let rec find i =
2920 if i = -1 || i = itemcount
2921 then -1
2922 else (
2923 if source#hasaction i
2924 then i
2925 else find (i + incr)
2928 find start
2930 let set active first =
2931 let first = bound first 0 (itemcount - fstate.maxrows) in
2932 state.text <- "";
2933 coe {< m_active = active; m_first = first >}
2935 let navigate incr =
2936 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2937 let active, first =
2938 let incr1 = if incr > 0 then 1 else -1 in
2939 if isvisible m_first m_active
2940 then
2941 let next =
2942 let next = m_active + incr in
2943 let next =
2944 if next < 0 || next >= itemcount
2945 then -1
2946 else find next incr1
2948 if next = -1 || abs (m_active - next) > fstate.maxrows
2949 then -1
2950 else next
2952 if next = -1
2953 then
2954 let first = m_first + incr in
2955 let first = bound first 0 (itemcount - 1) in
2956 let next =
2957 let next = m_active + incr in
2958 let next = bound next 0 (itemcount - 1) in
2959 find next ~-incr1
2961 let active = if next = -1 then m_active else next in
2962 active, first
2963 else
2964 let first = min next m_first in
2965 let first =
2966 if abs (next - first) > fstate.maxrows
2967 then first + incr
2968 else first
2970 next, first
2971 else
2972 let first = m_first + incr in
2973 let first = bound first 0 (itemcount - 1) in
2974 let active =
2975 let next = m_active + incr in
2976 let next = bound next 0 (itemcount - 1) in
2977 let next = find next incr1 in
2978 let active =
2979 if next = -1 || abs (m_active - first) > fstate.maxrows
2980 then (
2981 let active = if m_active = -1 then next else m_active in
2982 active
2984 else next
2986 if isvisible first active
2987 then active
2988 else -1
2990 active, first
2992 G.postRedisplay "listview navigate";
2993 set active first;
2995 match key with
2996 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2997 let incr = if key = 0x72 then -1 else 1 in
2998 let active, first =
2999 match search (m_active + incr) m_qsearch incr with
3000 | None ->
3001 state.text <- m_qsearch ^ " [not found]";
3002 m_active, m_first
3003 | Some active ->
3004 state.text <- m_qsearch;
3005 active, firstof m_first active
3007 G.postRedisplay "listview ctrl-r/s";
3008 set1 active first m_qsearch;
3010 | 0xff08 -> (* backspace *)
3011 if String.length m_qsearch = 0
3012 then coe self
3013 else (
3014 let qsearch = withoutlastutf8 m_qsearch in
3015 let len = String.length qsearch in
3016 if len = 0
3017 then (
3018 state.text <- "";
3019 G.postRedisplay "listview empty qsearch";
3020 set1 m_active m_first "";
3022 else
3023 let active, first =
3024 match search m_active qsearch ~-1 with
3025 | None ->
3026 state.text <- qsearch ^ " [not found]";
3027 m_active, m_first
3028 | Some active ->
3029 state.text <- qsearch;
3030 active, firstof m_first active
3032 G.postRedisplay "listview backspace qsearch";
3033 set1 active first qsearch
3036 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3037 let pattern = m_qsearch ^ Wsi.toutf8 key in
3038 let active, first =
3039 match search m_active pattern 1 with
3040 | None ->
3041 state.text <- pattern ^ " [not found]";
3042 m_active, m_first
3043 | Some active ->
3044 state.text <- pattern;
3045 active, firstof m_first active
3047 G.postRedisplay "listview qsearch add";
3048 set1 active first pattern;
3050 | 0xff1b -> (* escape *)
3051 state.text <- "";
3052 if String.length m_qsearch = 0
3053 then (
3054 G.postRedisplay "list view escape";
3055 begin
3056 match
3057 source#exit (coe self) true m_active m_first m_pan m_qsearch
3058 with
3059 | None -> m_prev_uioh
3060 | Some uioh -> uioh
3063 else (
3064 G.postRedisplay "list view kill qsearch";
3065 source#setqsearch "";
3066 coe {< m_qsearch = "" >}
3069 | 0xff0d -> (* return *)
3070 state.text <- "";
3071 let self = {< m_qsearch = "" >} in
3072 source#setqsearch "";
3073 let opt =
3074 G.postRedisplay "listview enter";
3075 if m_active >= 0 && m_active < source#getitemcount
3076 then (
3077 source#exit (coe self) false m_active m_first m_pan "";
3079 else (
3080 source#exit (coe self) true m_active m_first m_pan "";
3083 begin match opt with
3084 | None -> m_prev_uioh
3085 | Some uioh -> uioh
3088 | 0xff9f | 0xffff -> (* delete *)
3089 coe self
3091 | 0xff52 -> navigate ~-1 (* up *)
3092 | 0xff54 -> navigate 1 (* down *)
3093 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3094 | 0xff56 -> navigate fstate.maxrows (* next *)
3096 | 0xff53 -> (* right *)
3097 state.text <- "";
3098 G.postRedisplay "listview right";
3099 coe {< m_pan = m_pan - 1 >}
3101 | 0xff51 -> (* left *)
3102 state.text <- "";
3103 G.postRedisplay "listview left";
3104 coe {< m_pan = m_pan + 1 >}
3106 | 0xff50 -> (* home *)
3107 let active = find 0 1 in
3108 G.postRedisplay "listview home";
3109 set active 0;
3111 | 0xff57 -> (* end *)
3112 let first = max 0 (itemcount - fstate.maxrows) in
3113 let active = find (itemcount - 1) ~-1 in
3114 G.postRedisplay "listview end";
3115 set active first;
3117 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3118 coe self
3120 | _ ->
3121 dolog "listview unknown key %#x" key; coe self
3123 method key key mask =
3124 match state.mode with
3125 | Textentry te -> textentrykeyboard key mask te; coe self
3126 | _ -> self#key1 key mask
3128 method button button down x y _ =
3129 let opt =
3130 match button with
3131 | 1 when x > conf.winw - conf.scrollbw ->
3132 G.postRedisplay "listview scroll";
3133 if down
3134 then
3135 let _, position, sh = self#scrollph in
3136 if y > truncate position && y < truncate (position +. sh)
3137 then (
3138 state.mstate <- Mscrolly;
3139 Some (coe self)
3141 else
3142 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3143 let first = truncate (s *. float source#getitemcount) in
3144 let first = min source#getitemcount first in
3145 Some (coe {< m_first = first; m_active = first >})
3146 else (
3147 state.mstate <- Mnone;
3148 Some (coe self);
3150 | 1 when not down ->
3151 begin match self#elemunder y with
3152 | Some n ->
3153 G.postRedisplay "listview click";
3154 source#exit
3155 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3156 | _ ->
3157 Some (coe self)
3159 | n when (n == 4 || n == 5) && not down ->
3160 let len = source#getitemcount in
3161 let first =
3162 if n = 5 && m_first + fstate.maxrows >= len
3163 then
3164 m_first
3165 else
3166 let first = m_first + (if n == 4 then -1 else 1) in
3167 bound first 0 (len - 1)
3169 G.postRedisplay "listview wheel";
3170 Some (coe {< m_first = first >})
3171 | _ ->
3172 Some (coe self)
3174 match opt with
3175 | None -> m_prev_uioh
3176 | Some uioh -> uioh
3178 method motion _ y =
3179 match state.mstate with
3180 | Mscrolly ->
3181 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3182 let first = truncate (s *. float source#getitemcount) in
3183 let first = min source#getitemcount first in
3184 G.postRedisplay "listview motion";
3185 coe {< m_first = first; m_active = first >}
3186 | _ -> coe self
3188 method pmotion x y =
3189 if x < conf.winw - conf.scrollbw
3190 then
3191 let n =
3192 match self#elemunder y with
3193 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3194 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3196 let o =
3197 if n != m_active
3198 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3199 else self
3201 coe o
3202 else (
3203 Wsi.setcursor Wsi.CURSOR_INHERIT;
3204 coe self
3207 method infochanged _ = ()
3209 method scrollpw = (0, 0.0, 0.0)
3210 method scrollph =
3211 let nfs = fstate.fontsize + 1 in
3212 let y = m_first * nfs in
3213 let itemcount = source#getitemcount in
3214 let maxi = max 0 (itemcount - fstate.maxrows) in
3215 let maxy = maxi * nfs in
3216 let p, h = scrollph y maxy in
3217 conf.scrollbw, p, h
3219 method modehash = modehash
3220 end;;
3222 class outlinelistview ~source =
3223 object (self)
3224 inherit listview
3225 ~source:(source :> lvsource)
3226 ~trusted:false
3227 ~modehash:(findkeyhash conf "outline")
3228 as super
3230 method key key mask =
3231 let calcfirst first active =
3232 if active > first
3233 then
3234 let rows = active - first in
3235 if rows > fstate.maxrows then active - fstate.maxrows else first
3236 else active
3238 let navigate incr =
3239 let active = m_active + incr in
3240 let active = bound active 0 (source#getitemcount - 1) in
3241 let first = calcfirst m_first active in
3242 G.postRedisplay "outline navigate";
3243 coe {< m_active = active; m_first = first >}
3245 let ctrl = Wsi.withctrl mask in
3246 match key with
3247 | 110 when ctrl -> (* ctrl-n *)
3248 source#narrow m_qsearch;
3249 G.postRedisplay "outline ctrl-n";
3250 coe {< m_first = 0; m_active = 0 >}
3252 | 117 when ctrl -> (* ctrl-u *)
3253 source#denarrow;
3254 G.postRedisplay "outline ctrl-u";
3255 state.text <- "";
3256 coe {< m_first = 0; m_active = 0 >}
3258 | 108 when ctrl -> (* ctrl-l *)
3259 let first = m_active - (fstate.maxrows / 2) in
3260 G.postRedisplay "outline ctrl-l";
3261 coe {< m_first = first >}
3263 | 0xff9f | 0xffff -> (* delete *)
3264 source#remove m_active;
3265 G.postRedisplay "outline delete";
3266 let active = max 0 (m_active-1) in
3267 coe {< m_first = firstof m_first active;
3268 m_active = active >}
3270 | 0xff52 -> navigate ~-1 (* up *)
3271 | 0xff54 -> navigate 1 (* down *)
3272 | 0xff55 -> (* prior *)
3273 navigate ~-(fstate.maxrows)
3274 | 0xff56 -> (* next *)
3275 navigate fstate.maxrows
3277 | 0xff53 -> (* [ctrl-]right *)
3278 let o =
3279 if ctrl
3280 then (
3281 G.postRedisplay "outline ctrl right";
3282 {< m_pan = m_pan + 1 >}
3284 else self#updownlevel 1
3286 coe o
3288 | 0xff51 -> (* [ctrl-]left *)
3289 let o =
3290 if ctrl
3291 then (
3292 G.postRedisplay "outline ctrl left";
3293 {< m_pan = m_pan - 1 >}
3295 else self#updownlevel ~-1
3297 coe o
3299 | 0xff50 -> (* home *)
3300 G.postRedisplay "outline home";
3301 coe {< m_first = 0; m_active = 0 >}
3303 | 0xff57 -> (* end *)
3304 let active = source#getitemcount - 1 in
3305 let first = max 0 (active - fstate.maxrows) in
3306 G.postRedisplay "outline end";
3307 coe {< m_active = active; m_first = first >}
3309 | _ -> super#key key mask
3312 let outlinesource usebookmarks =
3313 let empty = [||] in
3314 (object
3315 inherit lvsourcebase
3316 val mutable m_items = empty
3317 val mutable m_orig_items = empty
3318 val mutable m_prev_items = empty
3319 val mutable m_narrow_pattern = ""
3320 val mutable m_hadremovals = false
3322 method getitemcount =
3323 Array.length m_items + (if m_hadremovals then 1 else 0)
3325 method getitem n =
3326 if n == Array.length m_items && m_hadremovals
3327 then
3328 ("[Confirm removal]", 0)
3329 else
3330 let s, n, _ = m_items.(n) in
3331 (s, n)
3333 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3334 ignore (uioh, first, qsearch);
3335 let confrimremoval = m_hadremovals && active = Array.length m_items in
3336 let items =
3337 if String.length m_narrow_pattern = 0
3338 then m_orig_items
3339 else m_items
3341 if not cancel
3342 then (
3343 if not confrimremoval
3344 then(
3345 let _, _, anchor = m_items.(active) in
3346 gotoanchor anchor;
3347 m_items <- items;
3349 else (
3350 state.bookmarks <- Array.to_list m_items;
3351 m_orig_items <- m_items;
3354 else m_items <- items;
3355 m_pan <- pan;
3356 None
3358 method hasaction _ = true
3360 method greetmsg =
3361 if Array.length m_items != Array.length m_orig_items
3362 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3363 else ""
3365 method narrow pattern =
3366 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3367 match reopt with
3368 | None -> ()
3369 | Some re ->
3370 let rec loop accu n =
3371 if n = -1
3372 then (
3373 m_narrow_pattern <- pattern;
3374 m_items <- Array.of_list accu
3376 else
3377 let (s, _, _) as o = m_items.(n) in
3378 let accu =
3379 if (try ignore (Str.search_forward re s 0); true
3380 with Not_found -> false)
3381 then o :: accu
3382 else accu
3384 loop accu (n-1)
3386 loop [] (Array.length m_items - 1)
3388 method denarrow =
3389 m_orig_items <- (
3390 if usebookmarks
3391 then Array.of_list state.bookmarks
3392 else state.outlines
3394 m_items <- m_orig_items
3396 method remove m =
3397 if usebookmarks
3398 then
3399 if m >= 0 && m < Array.length m_items
3400 then (
3401 m_hadremovals <- true;
3402 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3403 let n = if n >= m then n+1 else n in
3404 m_items.(n)
3408 method reset anchor items =
3409 m_hadremovals <- false;
3410 if m_orig_items == empty || m_prev_items != items
3411 then (
3412 m_orig_items <- items;
3413 if String.length m_narrow_pattern = 0
3414 then m_items <- items;
3416 m_prev_items <- items;
3417 let rely = getanchory anchor in
3418 let active =
3419 let rec loop n best bestd =
3420 if n = Array.length m_items
3421 then best
3422 else
3423 let (_, _, anchor) = m_items.(n) in
3424 let orely = getanchory anchor in
3425 let d = abs (orely - rely) in
3426 if d < bestd
3427 then loop (n+1) n d
3428 else loop (n+1) best bestd
3430 loop 0 ~-1 max_int
3432 m_active <- active;
3433 m_first <- firstof m_first active
3434 end)
3437 let enterselector usebookmarks =
3438 let source = outlinesource usebookmarks in
3439 fun errmsg ->
3440 let outlines =
3441 if usebookmarks
3442 then Array.of_list state.bookmarks
3443 else state.outlines
3445 if Array.length outlines = 0
3446 then (
3447 showtext ' ' errmsg;
3449 else (
3450 state.text <- source#greetmsg;
3451 Wsi.setcursor Wsi.CURSOR_INHERIT;
3452 let anchor = getanchor () in
3453 source#reset anchor outlines;
3454 state.uioh <- coe (new outlinelistview ~source);
3455 G.postRedisplay "enter selector";
3459 let enteroutlinemode =
3460 let f = enterselector false in
3461 fun ()-> f "Document has no outline";
3464 let enterbookmarkmode =
3465 let f = enterselector true in
3466 fun () -> f "Document has no bookmarks (yet)";
3469 let color_of_string s =
3470 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3471 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3475 let color_to_string (r, g, b) =
3476 let r = truncate (r *. 256.0)
3477 and g = truncate (g *. 256.0)
3478 and b = truncate (b *. 256.0) in
3479 Printf.sprintf "%d/%d/%d" r g b
3482 let irect_of_string s =
3483 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3486 let irect_to_string (x0,y0,x1,y1) =
3487 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3490 let makecheckers () =
3491 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3492 following to say:
3493 converted by Issac Trotts. July 25, 2002 *)
3494 let image_height = 64
3495 and image_width = 64 in
3497 let make_image () =
3498 let image =
3499 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3501 for i = 0 to image_width - 1 do
3502 for j = 0 to image_height - 1 do
3503 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3504 (if (i land 8 ) lxor (j land 8) = 0
3505 then [|255;255;255|] else [|200;200;200|])
3506 done
3507 done;
3508 image
3510 let image = make_image () in
3511 let id = GlTex.gen_texture () in
3512 GlTex.bind_texture `texture_2d id;
3513 GlPix.store (`unpack_alignment 1);
3514 GlTex.image2d image;
3515 List.iter (GlTex.parameter ~target:`texture_2d)
3516 [ `wrap_s `repeat;
3517 `wrap_t `repeat;
3518 `mag_filter `nearest;
3519 `min_filter `nearest ];
3523 let setcheckers enabled =
3524 match state.texid with
3525 | None ->
3526 if enabled then state.texid <- Some (makecheckers ())
3528 | Some texid ->
3529 if not enabled
3530 then (
3531 GlTex.delete_texture texid;
3532 state.texid <- None;
3536 let int_of_string_with_suffix s =
3537 let l = String.length s in
3538 let s1, shift =
3539 if l > 1
3540 then
3541 let suffix = Char.lowercase s.[l-1] in
3542 match suffix with
3543 | 'k' -> String.sub s 0 (l-1), 10
3544 | 'm' -> String.sub s 0 (l-1), 20
3545 | 'g' -> String.sub s 0 (l-1), 30
3546 | _ -> s, 0
3547 else s, 0
3549 let n = int_of_string s1 in
3550 let m = n lsl shift in
3551 if m < 0 || m < n
3552 then raise (Failure "value too large")
3553 else m
3556 let string_with_suffix_of_int n =
3557 if n = 0
3558 then "0"
3559 else
3560 let n, s =
3561 if n = 0
3562 then 0, ""
3563 else (
3564 if n land ((1 lsl 20) - 1) = 0
3565 then n lsr 20, "M"
3566 else (
3567 if n land ((1 lsl 10) - 1) = 0
3568 then n lsr 10, "K"
3569 else n, ""
3573 let rec loop s n =
3574 let h = n mod 1000 in
3575 let n = n / 1000 in
3576 if n = 0
3577 then string_of_int h ^ s
3578 else (
3579 let s = Printf.sprintf "_%03d%s" h s in
3580 loop s n
3583 loop "" n ^ s;
3586 let defghyllscroll = (40, 8, 32);;
3587 let ghyllscroll_of_string s =
3588 let (n, a, b) as nab =
3589 if s = "default"
3590 then defghyllscroll
3591 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3593 if n <= a || n <= b || a >= b
3594 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3595 nab;
3598 let ghyllscroll_to_string ((n, a, b) as nab) =
3599 if nab = defghyllscroll
3600 then "default"
3601 else Printf.sprintf "%d,%d,%d" n a b;
3604 let describe_location () =
3605 let f (fn, _) l =
3606 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3608 let fn, ln = List.fold_left f (-1, -1) state.layout in
3609 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3610 let percent =
3611 if maxy <= 0
3612 then 100.
3613 else (100. *. (float state.y /. float maxy))
3615 if fn = ln
3616 then
3617 Printf.sprintf "page %d of %d [%.2f%%]"
3618 (fn+1) state.pagecount percent
3619 else
3620 Printf.sprintf
3621 "pages %d-%d of %d [%.2f%%]"
3622 (fn+1) (ln+1) state.pagecount percent
3625 let enterinfomode =
3626 let btos b = if b then "\xe2\x88\x9a" else "" in
3627 let showextended = ref false in
3628 let leave mode = function
3629 | Confirm -> state.mode <- mode
3630 | Cancel -> state.mode <- mode in
3631 let src =
3632 (object
3633 val mutable m_first_time = true
3634 val mutable m_l = []
3635 val mutable m_a = [||]
3636 val mutable m_prev_uioh = nouioh
3637 val mutable m_prev_mode = View
3639 inherit lvsourcebase
3641 method reset prev_mode prev_uioh =
3642 m_a <- Array.of_list (List.rev m_l);
3643 m_l <- [];
3644 m_prev_mode <- prev_mode;
3645 m_prev_uioh <- prev_uioh;
3646 if m_first_time
3647 then (
3648 let rec loop n =
3649 if n >= Array.length m_a
3650 then ()
3651 else
3652 match m_a.(n) with
3653 | _, _, _, Action _ -> m_active <- n
3654 | _ -> loop (n+1)
3656 loop 0;
3657 m_first_time <- false;
3660 method int name get set =
3661 m_l <-
3662 (name, `int get, 1, Action (
3663 fun u ->
3664 let ondone s =
3665 try set (int_of_string s)
3666 with exn ->
3667 state.text <- Printf.sprintf "bad integer `%s': %s"
3668 s (Printexc.to_string exn)
3670 state.text <- "";
3671 let te = name ^ ": ", "", None, intentry, ondone in
3672 state.mode <- Textentry (te, leave m_prev_mode);
3674 )) :: m_l
3676 method int_with_suffix name get set =
3677 m_l <-
3678 (name, `intws get, 1, Action (
3679 fun u ->
3680 let ondone s =
3681 try set (int_of_string_with_suffix s)
3682 with exn ->
3683 state.text <- Printf.sprintf "bad integer `%s': %s"
3684 s (Printexc.to_string exn)
3686 state.text <- "";
3687 let te =
3688 name ^ ": ", "", None, intentry_with_suffix, ondone
3690 state.mode <- Textentry (te, leave m_prev_mode);
3692 )) :: m_l
3694 method bool ?(offset=1) ?(btos=btos) name get set =
3695 m_l <-
3696 (name, `bool (btos, get), offset, Action (
3697 fun u ->
3698 let v = get () in
3699 set (not v);
3701 )) :: m_l
3703 method color name get set =
3704 m_l <-
3705 (name, `color get, 1, Action (
3706 fun u ->
3707 let invalid = (nan, nan, nan) in
3708 let ondone s =
3709 let c =
3710 try color_of_string s
3711 with exn ->
3712 state.text <- Printf.sprintf "bad color `%s': %s"
3713 s (Printexc.to_string exn);
3714 invalid
3716 if c <> invalid
3717 then set c;
3719 let te = name ^ ": ", "", None, textentry, ondone in
3720 state.text <- color_to_string (get ());
3721 state.mode <- Textentry (te, leave m_prev_mode);
3723 )) :: m_l
3725 method string name get set =
3726 m_l <-
3727 (name, `string get, 1, Action (
3728 fun u ->
3729 let ondone s = set s in
3730 let te = name ^ ": ", "", None, textentry, ondone in
3731 state.mode <- Textentry (te, leave m_prev_mode);
3733 )) :: m_l
3735 method colorspace name get set =
3736 m_l <-
3737 (name, `string get, 1, Action (
3738 fun _ ->
3739 let source =
3740 let vals = [| "rgb"; "bgr"; "gray" |] in
3741 (object
3742 inherit lvsourcebase
3744 initializer
3745 m_active <- int_of_colorspace conf.colorspace;
3746 m_first <- 0;
3748 method getitemcount = Array.length vals
3749 method getitem n = (vals.(n), 0)
3750 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3751 ignore (uioh, first, pan, qsearch);
3752 if not cancel then set active;
3753 None
3754 method hasaction _ = true
3755 end)
3757 state.text <- "";
3758 let modehash = findkeyhash conf "info" in
3759 coe (new listview ~source ~trusted:true ~modehash)
3760 )) :: m_l
3762 method caption s offset =
3763 m_l <- (s, `empty, offset, Noaction) :: m_l
3765 method caption2 s f offset =
3766 m_l <- (s, `string f, offset, Noaction) :: m_l
3768 method getitemcount = Array.length m_a
3770 method getitem n =
3771 let tostr = function
3772 | `int f -> string_of_int (f ())
3773 | `intws f -> string_with_suffix_of_int (f ())
3774 | `string f -> f ()
3775 | `color f -> color_to_string (f ())
3776 | `bool (btos, f) -> btos (f ())
3777 | `empty -> ""
3779 let name, t, offset, _ = m_a.(n) in
3780 ((let s = tostr t in
3781 if String.length s > 0
3782 then Printf.sprintf "%s\t%s" name s
3783 else name),
3784 offset)
3786 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3787 let uiohopt =
3788 if not cancel
3789 then (
3790 m_qsearch <- qsearch;
3791 let uioh =
3792 match m_a.(active) with
3793 | _, _, _, Action f -> f uioh
3794 | _ -> uioh
3796 Some uioh
3798 else None
3800 m_active <- active;
3801 m_first <- first;
3802 m_pan <- pan;
3803 uiohopt
3805 method hasaction n =
3806 match m_a.(n) with
3807 | _, _, _, Action _ -> true
3808 | _ -> false
3809 end)
3811 let rec fillsrc prevmode prevuioh =
3812 let sep () = src#caption "" 0 in
3813 let colorp name get set =
3814 src#string name
3815 (fun () -> color_to_string (get ()))
3816 (fun v ->
3818 let c = color_of_string v in
3819 set c
3820 with exn ->
3821 state.text <- Printf.sprintf "bad color `%s': %s"
3822 v (Printexc.to_string exn);
3825 let oldmode = state.mode in
3826 let birdseye = isbirdseye state.mode in
3828 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3830 src#bool "presentation mode"
3831 (fun () -> conf.presentation)
3832 (fun v ->
3833 conf.presentation <- v;
3834 state.anchor <- getanchor ();
3835 represent ());
3837 src#bool "ignore case in searches"
3838 (fun () -> conf.icase)
3839 (fun v -> conf.icase <- v);
3841 src#bool "preload"
3842 (fun () -> conf.preload)
3843 (fun v -> conf.preload <- v);
3845 src#bool "highlight links"
3846 (fun () -> conf.hlinks)
3847 (fun v -> conf.hlinks <- v);
3849 src#bool "under info"
3850 (fun () -> conf.underinfo)
3851 (fun v -> conf.underinfo <- v);
3853 src#bool "persistent bookmarks"
3854 (fun () -> conf.savebmarks)
3855 (fun v -> conf.savebmarks <- v);
3857 src#bool "proportional display"
3858 (fun () -> conf.proportional)
3859 (fun v -> reqlayout conf.angle v);
3861 src#bool "trim margins"
3862 (fun () -> conf.trimmargins)
3863 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3865 src#bool "persistent location"
3866 (fun () -> conf.jumpback)
3867 (fun v -> conf.jumpback <- v);
3869 sep ();
3870 src#int "inter-page space"
3871 (fun () -> conf.interpagespace)
3872 (fun n ->
3873 conf.interpagespace <- n;
3874 let pageno, py =
3875 match state.layout with
3876 | [] -> 0, 0
3877 | l :: _ ->
3878 l.pageno, l.pagey
3880 state.maxy <- calcheight ();
3881 let y = getpagey pageno in
3882 gotoy (y + py)
3885 src#int "page bias"
3886 (fun () -> conf.pagebias)
3887 (fun v -> conf.pagebias <- v);
3889 src#int "scroll step"
3890 (fun () -> conf.scrollstep)
3891 (fun n -> conf.scrollstep <- n);
3893 src#int "auto scroll step"
3894 (fun () ->
3895 match state.autoscroll with
3896 | Some step -> step
3897 | _ -> conf.autoscrollstep)
3898 (fun n ->
3899 if state.autoscroll <> None
3900 then state.autoscroll <- Some n;
3901 conf.autoscrollstep <- n);
3903 src#int "zoom"
3904 (fun () -> truncate (conf.zoom *. 100.))
3905 (fun v -> setzoom ((float v) /. 100.));
3907 src#int "rotation"
3908 (fun () -> conf.angle)
3909 (fun v -> reqlayout v conf.proportional);
3911 src#int "scroll bar width"
3912 (fun () -> state.scrollw)
3913 (fun v ->
3914 state.scrollw <- v;
3915 conf.scrollbw <- v;
3916 reshape conf.winw conf.winh;
3919 src#int "scroll handle height"
3920 (fun () -> conf.scrollh)
3921 (fun v -> conf.scrollh <- v;);
3923 src#int "thumbnail width"
3924 (fun () -> conf.thumbw)
3925 (fun v ->
3926 conf.thumbw <- min 4096 v;
3927 match oldmode with
3928 | Birdseye beye ->
3929 leavebirdseye beye false;
3930 enterbirdseye ()
3931 | _ -> ()
3934 src#string "columns"
3935 (fun () ->
3936 match conf.columns with
3937 | None -> "1"
3938 | Some (multicol, _) -> columns_to_string multicol)
3939 (fun v ->
3940 let n, a, b = columns_of_string v in
3941 setcolumns n a b);
3943 sep ();
3944 src#caption "Presentation mode" 0;
3945 src#bool "scrollbar visible"
3946 (fun () -> conf.scrollbarinpm)
3947 (fun v ->
3948 if v != conf.scrollbarinpm
3949 then (
3950 conf.scrollbarinpm <- v;
3951 if conf.presentation
3952 then (
3953 state.scrollw <- if v then conf.scrollbw else 0;
3954 reshape conf.winw conf.winh;
3959 sep ();
3960 src#caption "Pixmap cache" 0;
3961 src#int_with_suffix "size (advisory)"
3962 (fun () -> conf.memlimit)
3963 (fun v -> conf.memlimit <- v);
3965 src#caption2 "used"
3966 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3967 (string_with_suffix_of_int state.memused)
3968 (Hashtbl.length state.tilemap)) 1;
3970 sep ();
3971 src#caption "Layout" 0;
3972 src#caption2 "Dimension"
3973 (fun () ->
3974 Printf.sprintf "%dx%d (virtual %dx%d)"
3975 conf.winw conf.winh
3976 state.w state.maxy)
3978 if conf.debug
3979 then
3980 src#caption2 "Position" (fun () ->
3981 Printf.sprintf "%dx%d" state.x state.y
3983 else
3984 src#caption2 "Visible" (fun () -> describe_location ()) 1
3987 sep ();
3988 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3989 "Save these parameters as global defaults at exit"
3990 (fun () -> conf.bedefault)
3991 (fun v -> conf.bedefault <- v)
3994 sep ();
3995 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3996 src#bool ~offset:0 ~btos "Extended parameters"
3997 (fun () -> !showextended)
3998 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3999 if !showextended
4000 then (
4001 src#bool "checkers"
4002 (fun () -> conf.checkers)
4003 (fun v -> conf.checkers <- v; setcheckers v);
4004 src#bool "update cursor"
4005 (fun () -> conf.updatecurs)
4006 (fun v -> conf.updatecurs <- v);
4007 src#bool "verbose"
4008 (fun () -> conf.verbose)
4009 (fun v -> conf.verbose <- v);
4010 src#bool "invert colors"
4011 (fun () -> conf.invert)
4012 (fun v -> conf.invert <- v);
4013 src#bool "max fit"
4014 (fun () -> conf.maxhfit)
4015 (fun v -> conf.maxhfit <- v);
4016 src#bool "redirect stderr"
4017 (fun () -> conf.redirectstderr)
4018 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4019 src#string "uri launcher"
4020 (fun () -> conf.urilauncher)
4021 (fun v -> conf.urilauncher <- v);
4022 src#string "path launcher"
4023 (fun () -> conf.pathlauncher)
4024 (fun v -> conf.pathlauncher <- v);
4025 src#string "tile size"
4026 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4027 (fun v ->
4029 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4030 conf.tileh <- max 64 w;
4031 conf.tilew <- max 64 h;
4032 flushtiles ();
4033 with exn ->
4034 state.text <- Printf.sprintf "bad tile size `%s': %s"
4035 v (Printexc.to_string exn));
4036 src#int "texture count"
4037 (fun () -> conf.texcount)
4038 (fun v ->
4039 if realloctexts v
4040 then conf.texcount <- v
4041 else showtext '!' " Failed to set texture count please retry later"
4043 src#int "slice height"
4044 (fun () -> conf.sliceheight)
4045 (fun v ->
4046 conf.sliceheight <- v;
4047 wcmd "sliceh %d" conf.sliceheight;
4049 src#int "anti-aliasing level"
4050 (fun () -> conf.aalevel)
4051 (fun v ->
4052 conf.aalevel <- bound v 0 8;
4053 state.anchor <- getanchor ();
4054 opendoc state.path state.password;
4056 src#int "ui font size"
4057 (fun () -> fstate.fontsize)
4058 (fun v -> setfontsize (bound v 5 100));
4059 colorp "background color"
4060 (fun () -> conf.bgcolor)
4061 (fun v -> conf.bgcolor <- v);
4062 src#bool "crop hack"
4063 (fun () -> conf.crophack)
4064 (fun v -> conf.crophack <- v);
4065 src#string "trim fuzz"
4066 (fun () -> irect_to_string conf.trimfuzz)
4067 (fun v ->
4069 conf.trimfuzz <- irect_of_string v;
4070 if conf.trimmargins
4071 then settrim true conf.trimfuzz;
4072 with exn ->
4073 state.text <- Printf.sprintf "bad irect `%s': %s"
4074 v (Printexc.to_string exn)
4076 src#string "throttle"
4077 (fun () ->
4078 match conf.maxwait with
4079 | None -> "show place holder if page is not ready"
4080 | Some time ->
4081 if time = infinity
4082 then "wait for page to fully render"
4083 else
4084 "wait " ^ string_of_float time
4085 ^ " seconds before showing placeholder"
4087 (fun v ->
4089 let f = float_of_string v in
4090 if f <= 0.0
4091 then conf.maxwait <- None
4092 else conf.maxwait <- Some f
4093 with exn ->
4094 state.text <- Printf.sprintf "bad time `%s': %s"
4095 v (Printexc.to_string exn)
4097 src#string "ghyll scroll"
4098 (fun () ->
4099 match conf.ghyllscroll with
4100 | None -> ""
4101 | Some nab -> ghyllscroll_to_string nab
4103 (fun v ->
4105 let gs =
4106 if String.length v = 0
4107 then None
4108 else Some (ghyllscroll_of_string v)
4110 conf.ghyllscroll <- gs
4111 with exn ->
4112 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4113 v (Printexc.to_string exn)
4115 src#string "selection command"
4116 (fun () -> conf.selcmd)
4117 (fun v -> conf.selcmd <- v);
4118 src#colorspace "color space"
4119 (fun () -> colorspace_to_string conf.colorspace)
4120 (fun v ->
4121 conf.colorspace <- colorspace_of_int v;
4122 wcmd "cs %d" v;
4123 load state.layout;
4127 sep ();
4128 src#caption "Document" 0;
4129 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4130 src#caption2 "Pages"
4131 (fun () -> string_of_int state.pagecount) 1;
4132 src#caption2 "Dimensions"
4133 (fun () -> string_of_int (List.length state.pdims)) 1;
4134 if conf.trimmargins
4135 then (
4136 sep ();
4137 src#caption "Trimmed margins" 0;
4138 src#caption2 "Dimensions"
4139 (fun () -> string_of_int (List.length state.pdims)) 1;
4142 src#reset prevmode prevuioh;
4144 fun () ->
4145 state.text <- "";
4146 let prevmode = state.mode
4147 and prevuioh = state.uioh in
4148 fillsrc prevmode prevuioh;
4149 let source = (src :> lvsource) in
4150 let modehash = findkeyhash conf "info" in
4151 state.uioh <- coe (object (self)
4152 inherit listview ~source ~trusted:true ~modehash as super
4153 val mutable m_prevmemused = 0
4154 method infochanged = function
4155 | Memused ->
4156 if m_prevmemused != state.memused
4157 then (
4158 m_prevmemused <- state.memused;
4159 G.postRedisplay "memusedchanged";
4161 | Pdim -> G.postRedisplay "pdimchanged"
4162 | Docinfo -> fillsrc prevmode prevuioh
4164 method key key mask =
4165 if not (Wsi.withctrl mask)
4166 then
4167 match key with
4168 | 0xff51 -> coe (self#updownlevel ~-1)
4169 | 0xff53 -> coe (self#updownlevel 1)
4170 | _ -> super#key key mask
4171 else super#key key mask
4172 end);
4173 G.postRedisplay "info";
4176 let enterhelpmode =
4177 let source =
4178 (object
4179 inherit lvsourcebase
4180 method getitemcount = Array.length state.help
4181 method getitem n =
4182 let s, n, _ = state.help.(n) in
4183 (s, n)
4185 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4186 let optuioh =
4187 if not cancel
4188 then (
4189 m_qsearch <- qsearch;
4190 match state.help.(active) with
4191 | _, _, Action f -> Some (f uioh)
4192 | _ -> Some (uioh)
4194 else None
4196 m_active <- active;
4197 m_first <- first;
4198 m_pan <- pan;
4199 optuioh
4201 method hasaction n =
4202 match state.help.(n) with
4203 | _, _, Action _ -> true
4204 | _ -> false
4206 initializer
4207 m_active <- -1
4208 end)
4209 in fun () ->
4210 let modehash = findkeyhash conf "help" in
4211 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4212 G.postRedisplay "help";
4215 let entermsgsmode =
4216 let msgsource =
4217 let re = Str.regexp "[\r\n]" in
4218 (object
4219 inherit lvsourcebase
4220 val mutable m_items = [||]
4222 method getitemcount = 1 + Array.length m_items
4224 method getitem n =
4225 if n = 0
4226 then "[Clear]", 0
4227 else m_items.(n-1), 0
4229 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4230 ignore uioh;
4231 if not cancel
4232 then (
4233 if active = 0
4234 then Buffer.clear state.errmsgs;
4235 m_qsearch <- qsearch;
4237 m_active <- active;
4238 m_first <- first;
4239 m_pan <- pan;
4240 None
4242 method hasaction n =
4243 n = 0
4245 method reset =
4246 state.newerrmsgs <- false;
4247 let l = Str.split re (Buffer.contents state.errmsgs) in
4248 m_items <- Array.of_list l
4250 initializer
4251 m_active <- 0
4252 end)
4253 in fun () ->
4254 state.text <- "";
4255 msgsource#reset;
4256 let source = (msgsource :> lvsource) in
4257 let modehash = findkeyhash conf "listview" in
4258 state.uioh <- coe (object
4259 inherit listview ~source ~trusted:false ~modehash as super
4260 method display =
4261 if state.newerrmsgs
4262 then msgsource#reset;
4263 super#display
4264 end);
4265 G.postRedisplay "msgs";
4268 let quickbookmark ?title () =
4269 match state.layout with
4270 | [] -> ()
4271 | l :: _ ->
4272 let title =
4273 match title with
4274 | None ->
4275 let sec = Unix.gettimeofday () in
4276 let tm = Unix.localtime sec in
4277 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4278 (l.pageno+1)
4279 tm.Unix.tm_mday
4280 tm.Unix.tm_mon
4281 (tm.Unix.tm_year + 1900)
4282 tm.Unix.tm_hour
4283 tm.Unix.tm_min
4284 | Some title -> title
4286 state.bookmarks <-
4287 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4288 :: state.bookmarks
4291 let doreshape w h =
4292 state.fullscreen <- None;
4293 Wsi.reshape w h;
4296 let setautoscrollspeed step goingdown =
4297 let incr = max 1 ((abs step) / 2) in
4298 let incr = if goingdown then incr else -incr in
4299 let astep = step + incr in
4300 state.autoscroll <- Some astep;
4303 let viewkeyboard key mask =
4304 let enttext te =
4305 let mode = state.mode in
4306 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4307 state.text <- "";
4308 enttext ();
4309 G.postRedisplay "view:enttext"
4311 let ctrl = Wsi.withctrl mask in
4312 match key with
4313 | 81 -> (* Q *)
4314 exit 0
4316 | 0xff63 -> (* insert *)
4317 if conf.angle mod 360 = 0
4318 then (
4319 state.mode <- LinkNav (Ltgendir 0);
4320 gotoy state.y;
4322 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4324 | 0xff1b | 113 -> (* escape / q *)
4325 begin match state.mstate with
4326 | Mzoomrect _ ->
4327 state.mstate <- Mnone;
4328 Wsi.setcursor Wsi.CURSOR_INHERIT;
4329 G.postRedisplay "kill zoom rect";
4330 | _ ->
4331 match state.ranchors with
4332 | [] -> raise Quit
4333 | (path, password, anchor) :: rest ->
4334 state.ranchors <- rest;
4335 state.anchor <- anchor;
4336 opendoc path password
4337 end;
4339 | 0xff08 -> (* backspace *)
4340 let y = getnav ~-1 in
4341 gotoy_and_clear_text y
4343 | 111 -> (* o *)
4344 enteroutlinemode ()
4346 | 117 -> (* u *)
4347 state.rects <- [];
4348 state.text <- "";
4349 G.postRedisplay "dehighlight";
4351 | 47 | 63 -> (* / ? *)
4352 let ondone isforw s =
4353 cbput state.hists.pat s;
4354 state.searchpattern <- s;
4355 search s isforw
4357 let s = String.create 1 in
4358 s.[0] <- Char.chr key;
4359 enttext (s, "", Some (onhist state.hists.pat),
4360 textentry, ondone (key = 47))
4362 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4363 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4364 setzoom (conf.zoom +. incr)
4366 | 43 | 0xffab -> (* + *)
4367 let ondone s =
4368 let n =
4369 try int_of_string s with exc ->
4370 state.text <- Printf.sprintf "bad integer `%s': %s"
4371 s (Printexc.to_string exc);
4372 max_int
4374 if n != max_int
4375 then (
4376 conf.pagebias <- n;
4377 state.text <- "page bias is now " ^ string_of_int n;
4380 enttext ("page bias: ", "", None, intentry, ondone)
4382 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4383 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4384 setzoom (max 0.01 (conf.zoom -. decr))
4386 | 45 | 0xffad -> (* - *)
4387 let ondone msg = state.text <- msg in
4388 enttext (
4389 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4390 optentry state.mode, ondone
4393 | 48 when ctrl -> (* ctrl-0 *)
4394 setzoom 1.0
4396 | 49 when ctrl -> (* 1 *)
4397 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4398 if zoom < 1.0
4399 then setzoom zoom
4401 | 0xffc6 -> (* f9 *)
4402 togglebirdseye ()
4404 | 57 when ctrl -> (* ctrl-9 *)
4405 togglebirdseye ()
4407 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4408 when not ctrl -> (* 0..9 *)
4409 let ondone s =
4410 let n =
4411 try int_of_string s with exc ->
4412 state.text <- Printf.sprintf "bad integer `%s': %s"
4413 s (Printexc.to_string exc);
4416 if n >= 0
4417 then (
4418 addnav ();
4419 cbput state.hists.pag (string_of_int n);
4420 gotopage1 (n + conf.pagebias - 1) 0;
4423 let pageentry text key =
4424 match Char.unsafe_chr key with
4425 | 'g' -> TEdone text
4426 | _ -> intentry text key
4428 let text = "x" in text.[0] <- Char.chr key;
4429 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4431 | 98 -> (* b *)
4432 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4433 reshape conf.winw conf.winh;
4435 | 108 -> (* l *)
4436 conf.hlinks <- not conf.hlinks;
4437 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4438 G.postRedisplay "toggle highlightlinks";
4440 | 97 -> (* a *)
4441 begin match state.autoscroll with
4442 | Some step ->
4443 conf.autoscrollstep <- step;
4444 state.autoscroll <- None
4445 | None ->
4446 if conf.autoscrollstep = 0
4447 then state.autoscroll <- Some 1
4448 else state.autoscroll <- Some conf.autoscrollstep
4451 | 80 -> (* P *)
4452 conf.presentation <- not conf.presentation;
4453 if conf.presentation
4454 then (
4455 if not conf.scrollbarinpm
4456 then state.scrollw <- 0;
4458 else
4459 state.scrollw <- conf.scrollbw;
4461 showtext ' ' ("presentation mode " ^
4462 if conf.presentation then "on" else "off");
4463 state.anchor <- getanchor ();
4464 represent ()
4466 | 102 -> (* f *)
4467 begin match state.fullscreen with
4468 | None ->
4469 state.fullscreen <- Some (conf.winw, conf.winh);
4470 Wsi.fullscreen ()
4471 | Some (w, h) ->
4472 state.fullscreen <- None;
4473 doreshape w h
4476 | 103 -> (* g *)
4477 gotoy_and_clear_text 0
4479 | 71 -> (* G *)
4480 gotopage1 (state.pagecount - 1) 0
4482 | 112 | 78 -> (* p|N *)
4483 search state.searchpattern false
4485 | 110 | 0xffc0 -> (* n|F3 *)
4486 search state.searchpattern true
4488 | 116 -> (* t *)
4489 begin match state.layout with
4490 | [] -> ()
4491 | l :: _ ->
4492 gotoy_and_clear_text (getpagey l.pageno)
4495 | 32 -> (* ' ' *)
4496 begin match List.rev state.layout with
4497 | [] -> ()
4498 | l :: _ ->
4499 let pageno = min (l.pageno+1) (state.pagecount-1) in
4500 gotoy_and_clear_text (getpagey pageno)
4503 | 0xff9f | 0xffff -> (* delete *)
4504 begin match state.layout with
4505 | [] -> ()
4506 | l :: _ ->
4507 let pageno = max 0 (l.pageno-1) in
4508 gotoy_and_clear_text (getpagey pageno)
4511 | 61 -> (* = *)
4512 showtext ' ' (describe_location ());
4514 | 119 -> (* w *)
4515 begin match state.layout with
4516 | [] -> ()
4517 | l :: _ ->
4518 doreshape (l.pagew + state.scrollw) l.pageh;
4519 G.postRedisplay "w"
4522 | 39 -> (* ' *)
4523 enterbookmarkmode ()
4525 | 104 | 0xffbe -> (* h|F1 *)
4526 enterhelpmode ()
4528 | 105 -> (* i *)
4529 enterinfomode ()
4531 | 101 when conf.redirectstderr -> (* e *)
4532 entermsgsmode ()
4534 | 109 -> (* m *)
4535 let ondone s =
4536 match state.layout with
4537 | l :: _ ->
4538 state.bookmarks <-
4539 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4540 :: state.bookmarks
4541 | _ -> ()
4543 enttext ("bookmark: ", "", None, textentry, ondone)
4545 | 126 -> (* ~ *)
4546 quickbookmark ();
4547 showtext ' ' "Quick bookmark added";
4549 | 122 -> (* z *)
4550 begin match state.layout with
4551 | l :: _ ->
4552 let rect = getpdimrect l.pagedimno in
4553 let w, h =
4554 if conf.crophack
4555 then
4556 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4557 truncate (1.2 *. (rect.(3) -. rect.(0))))
4558 else
4559 (truncate (rect.(1) -. rect.(0)),
4560 truncate (rect.(3) -. rect.(0)))
4562 let w = truncate ((float w)*.conf.zoom)
4563 and h = truncate ((float h)*.conf.zoom) in
4564 if w != 0 && h != 0
4565 then (
4566 state.anchor <- getanchor ();
4567 doreshape (w + state.scrollw) (h + conf.interpagespace)
4569 G.postRedisplay "z";
4571 | [] -> ()
4574 | 50 when ctrl -> (* ctrl-2 *)
4575 let maxw = getmaxw () in
4576 if maxw > 0.0
4577 then setzoom (maxw /. float conf.winw)
4579 | 60 | 62 -> (* < > *)
4580 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4582 | 91 | 93 -> (* [ ] *)
4583 conf.colorscale <-
4584 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4586 G.postRedisplay "brightness";
4588 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4589 setzoom state.prevzoom
4591 | 107 | 0xff52 -> (* k up *)
4592 begin match state.autoscroll with
4593 | None ->
4594 begin match state.mode with
4595 | Birdseye beye -> upbirdseye 1 beye
4596 | _ ->
4597 if ctrl
4598 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4599 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4601 | Some n ->
4602 setautoscrollspeed n false
4605 | 106 | 0xff54 -> (* j down *)
4606 begin match state.autoscroll with
4607 | None ->
4608 begin match state.mode with
4609 | Birdseye beye -> downbirdseye 1 beye
4610 | _ ->
4611 if ctrl
4612 then gotoy_and_clear_text (clamp (conf.winh/2))
4613 else gotoy_and_clear_text (clamp conf.scrollstep)
4615 | Some n ->
4616 setautoscrollspeed n true
4619 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4620 if conf.zoom > 1.0
4621 then
4622 let dx =
4623 if ctrl
4624 then conf.winw / 2
4625 else 10
4627 let dx = if key = 0xff51 then dx else -dx in
4628 state.x <- state.x + dx;
4629 gotoy_and_clear_text state.y
4630 else (
4631 state.text <- "";
4632 G.postRedisplay "lef/right"
4635 | 0xff55 -> (* prior *)
4636 let y =
4637 if ctrl
4638 then
4639 match state.layout with
4640 | [] -> state.y
4641 | l :: _ -> state.y - l.pagey
4642 else
4643 clamp (-conf.winh)
4645 gotoghyll y
4647 | 0xff56 -> (* next *)
4648 let y =
4649 if ctrl
4650 then
4651 match List.rev state.layout with
4652 | [] -> state.y
4653 | l :: _ -> getpagey l.pageno
4654 else
4655 clamp conf.winh
4657 gotoghyll y
4659 | 0xff50 -> gotoghyll 0
4660 | 0xff57 -> gotoghyll (clamp state.maxy)
4661 | 0xff53 when Wsi.withalt mask ->
4662 gotoghyll (getnav ~-1)
4663 | 0xff51 when Wsi.withalt mask ->
4664 gotoghyll (getnav 1)
4666 | 114 -> (* r *)
4667 state.anchor <- getanchor ();
4668 opendoc state.path state.password
4670 | 76 -> (* L *)
4671 launchpath ()
4673 | 118 when conf.debug -> (* v *)
4674 state.rects <- [];
4675 List.iter (fun l ->
4676 match getopaque l.pageno with
4677 | None -> ()
4678 | Some opaque ->
4679 let x0, y0, x1, y1 = pagebbox opaque in
4680 let a,b = float x0, float y0 in
4681 let c,d = float x1, float y0 in
4682 let e,f = float x1, float y1 in
4683 let h,j = float x0, float y1 in
4684 let rect = (a,b,c,d,e,f,h,j) in
4685 debugrect rect;
4686 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4687 ) state.layout;
4688 G.postRedisplay "v";
4690 | _ ->
4691 vlog "huh? %s" (Wsi.keyname key)
4694 let gotounder = function
4695 | Ulinkgoto (pageno, top) ->
4696 if pageno >= 0
4697 then (
4698 addnav ();
4699 gotopage1 pageno top;
4702 | Ulinkuri s ->
4703 gotouri s
4705 | Uremote (filename, pageno) ->
4706 let path =
4707 if Sys.file_exists filename
4708 then filename
4709 else
4710 let dir = Filename.dirname state.path in
4711 let path = Filename.concat dir filename in
4712 if Sys.file_exists path
4713 then path
4714 else ""
4716 if String.length path > 0
4717 then (
4718 let anchor = getanchor () in
4719 let ranchor = state.path, state.password, anchor in
4720 state.anchor <- (pageno, 0.0);
4721 state.ranchors <- ranchor :: state.ranchors;
4722 opendoc path "";
4724 else showtext '!' ("Could not find " ^ filename)
4726 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4729 let linknavkeyboard key mask linknav =
4730 let getpage pageno =
4731 let rec loop = function
4732 | [] -> None
4733 | l :: _ when l.pageno = pageno -> Some l
4734 | _ :: rest -> loop rest
4735 in loop state.layout
4737 let doexact (pageno, n) =
4738 match getopaque pageno, getpage pageno with
4739 | Some opaque, Some l ->
4740 if key = 0xff0d
4741 then
4742 let under = getlink opaque n in
4743 G.postRedisplay "link gotounder";
4744 gotounder under;
4745 state.mode <- View;
4746 else
4747 let opt, dir =
4748 match key with
4749 | 0xff50 -> (* home *)
4750 Some (findlink opaque LDfirst), -1
4752 | 0xff57 -> (* end *)
4753 Some (findlink opaque LDlast), 1
4755 | 0xff51 -> (* left *)
4756 Some (findlink opaque (LDleft n)), -1
4758 | 0xff53 -> (* right *)
4759 Some (findlink opaque (LDright n)), 1
4761 | 0xff52 -> (* up *)
4762 Some (findlink opaque (LDup n)), -1
4764 | 0xff54 -> (* down *)
4765 Some (findlink opaque (LDdown n)), 1
4767 | _ -> None, 0
4769 let pwl l dir =
4770 begin match findpwl l.pageno dir with
4771 | Pwlnotfound -> ()
4772 | Pwl pageno ->
4773 let notfound dir =
4774 state.mode <- LinkNav (Ltgendir dir);
4775 let y, h = getpageyh pageno in
4776 let y =
4777 if dir < 0
4778 then y + h - conf.winh
4779 else y
4781 gotoy y
4783 begin match getopaque pageno, getpage pageno with
4784 | Some opaque, Some _ ->
4785 let link =
4786 let ld = if dir > 0 then LDfirst else LDlast in
4787 findlink opaque ld
4789 begin match link with
4790 | Lfound (m, x0, y0, x1, y1) ->
4791 let r = x0, y0, x1, y1 in
4792 showlinktype (getlink opaque m);
4793 state.mode <- LinkNav (Ltexact ((pageno, m), r));
4794 G.postRedisplay "linknav jpage";
4795 | _ -> notfound dir
4796 end;
4797 | _ -> notfound dir
4798 end;
4799 end;
4801 begin match opt with
4802 | Some Lnotfound -> pwl l dir;
4803 | Some (Lfound (m, x0, y0, x1, y1)) ->
4804 if m = n
4805 then pwl l dir
4806 else (
4807 if y0 < l.pagey
4808 then gotopage1 l.pageno y0
4809 else (
4810 let d = fstate.fontsize + 1 in
4811 if y1 - l.pagey > l.pagevh - d
4812 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
4813 else G.postRedisplay "linknav";
4815 let r = x0, y0, x1, y1 in
4816 showlinktype (getlink opaque m);
4817 state.mode <- LinkNav (Ltexact ((l.pageno, m), r));
4820 | None ->
4821 state.mode <- LinkNav (Ltgendir 0);
4822 viewkeyboard key mask
4823 end;
4824 | _ -> viewkeyboard key mask
4826 if key = 0xff63
4827 then (
4828 state.mode <- View;
4829 G.postRedisplay "leave linknav"
4831 else
4832 match linknav with
4833 | Ltgendir _ -> viewkeyboard key mask
4834 | Ltexact (exact, _) -> doexact exact
4837 let keyboard key mask =
4838 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4839 then wcmd "interrupt"
4840 else state.uioh <- state.uioh#key key mask
4843 let birdseyekeyboard key mask
4844 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4845 let incr =
4846 match conf.columns with
4847 | None -> 1
4848 | Some ((c, _, _), _) -> c
4850 match key with
4851 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4852 let y, h = getpageyh pageno in
4853 let top = (conf.winh - h) / 2 in
4854 gotoy (max 0 (y - top))
4855 | 0xff0d -> leavebirdseye beye false
4856 | 0xff1b -> leavebirdseye beye true (* escape *)
4857 | 0xff52 -> upbirdseye incr beye (* prior *)
4858 | 0xff54 -> downbirdseye incr beye (* next *)
4859 | 0xff51 -> upbirdseye 1 beye (* up *)
4860 | 0xff53 -> downbirdseye 1 beye (* down *)
4862 | 0xff55 ->
4863 begin match state.layout with
4864 | l :: _ ->
4865 if l.pagey != 0
4866 then (
4867 state.mode <- Birdseye (
4868 oconf, leftx, l.pageno, hooverpageno, anchor
4870 gotopage1 l.pageno 0;
4872 else (
4873 let layout = layout (state.y-conf.winh) conf.winh in
4874 match layout with
4875 | [] -> gotoy (clamp (-conf.winh))
4876 | l :: _ ->
4877 state.mode <- Birdseye (
4878 oconf, leftx, l.pageno, hooverpageno, anchor
4880 gotopage1 l.pageno 0
4883 | [] -> gotoy (clamp (-conf.winh))
4884 end;
4886 | 0xff56 ->
4887 begin match List.rev state.layout with
4888 | l :: _ ->
4889 let layout = layout (state.y + conf.winh) conf.winh in
4890 begin match layout with
4891 | [] ->
4892 let incr = l.pageh - l.pagevh in
4893 if incr = 0
4894 then (
4895 state.mode <-
4896 Birdseye (
4897 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4899 G.postRedisplay "birdseye pagedown";
4901 else gotoy (clamp (incr + conf.interpagespace*2));
4903 | l :: _ ->
4904 state.mode <-
4905 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4906 gotopage1 l.pageno 0;
4909 | [] -> gotoy (clamp conf.winh)
4910 end;
4912 | 0xff50 ->
4913 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4914 gotopage1 0 0
4916 | 0xff57 ->
4917 let pageno = state.pagecount - 1 in
4918 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4919 if not (pagevisible state.layout pageno)
4920 then
4921 let h =
4922 match List.rev state.pdims with
4923 | [] -> conf.winh
4924 | (_, _, h, _) :: _ -> h
4926 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4927 else G.postRedisplay "birdseye end";
4928 | _ -> viewkeyboard key mask
4931 let drawpage l =
4932 let color =
4933 match state.mode with
4934 | Textentry _ -> scalecolor 0.4
4935 | LinkNav _
4936 | View -> scalecolor 1.0
4937 | Birdseye (_, _, pageno, hooverpageno, _) ->
4938 if l.pageno = hooverpageno
4939 then scalecolor 0.9
4940 else (
4941 if l.pageno = pageno
4942 then scalecolor 1.0
4943 else scalecolor 0.8
4946 drawtiles l color;
4947 begin match getopaque l.pageno with
4948 | Some opaque ->
4949 if tileready l l.pagex l.pagey
4950 then
4951 let x = l.pagedispx - l.pagex
4952 and y = l.pagedispy - l.pagey in
4953 postprocess opaque conf.hlinks x y;
4955 | _ -> ()
4956 end;
4959 let scrollindicator () =
4960 let sbw, ph, sh = state.uioh#scrollph in
4961 let sbh, pw, sw = state.uioh#scrollpw in
4963 GlDraw.color (0.64, 0.64, 0.64);
4964 GlDraw.rect
4965 (float (conf.winw - sbw), 0.)
4966 (float conf.winw, float conf.winh)
4968 GlDraw.rect
4969 (0., float (conf.winh - sbh))
4970 (float (conf.winw - state.scrollw - 1), float conf.winh)
4972 GlDraw.color (0.0, 0.0, 0.0);
4974 GlDraw.rect
4975 (float (conf.winw - sbw), ph)
4976 (float conf.winw, ph +. sh)
4978 GlDraw.rect
4979 (pw, float (conf.winh - sbh))
4980 (pw +. sw, float conf.winh)
4984 let showsel () =
4985 match state.mstate with
4986 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4989 | Msel ((x0, y0), (x1, y1)) ->
4990 let rec loop = function
4991 | l :: ls ->
4992 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4993 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4994 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4995 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4996 then
4997 match getopaque l.pageno with
4998 | Some opaque ->
4999 let x0, y0 = pagetranslatepoint l x0 y0 in
5000 let x1, y1 = pagetranslatepoint l x1 y1 in
5001 seltext opaque (x0, y0, x1, y1);
5002 | _ -> ()
5003 else loop ls
5004 | [] -> ()
5006 loop state.layout
5009 let showrects rects =
5010 Gl.enable `blend;
5011 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5012 GlDraw.polygon_mode `both `fill;
5013 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5014 List.iter
5015 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5016 List.iter (fun l ->
5017 if l.pageno = pageno
5018 then (
5019 let dx = float (l.pagedispx - l.pagex) in
5020 let dy = float (l.pagedispy - l.pagey) in
5021 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5022 GlDraw.begins `quads;
5024 GlDraw.vertex2 (x0+.dx, y0+.dy);
5025 GlDraw.vertex2 (x1+.dx, y1+.dy);
5026 GlDraw.vertex2 (x2+.dx, y2+.dy);
5027 GlDraw.vertex2 (x3+.dx, y3+.dy);
5029 GlDraw.ends ();
5031 ) state.layout
5032 ) rects
5034 Gl.disable `blend;
5037 let display () =
5038 GlClear.color (scalecolor2 conf.bgcolor);
5039 GlClear.clear [`color];
5040 List.iter drawpage state.layout;
5041 let rects =
5042 match state.mode with
5043 | LinkNav (Ltexact ((pageno, _), (x0, y0, x1, y1))) ->
5044 (pageno, 5, (
5045 float x0, float y0,
5046 float x1, float y0,
5047 float x1, float y1,
5048 float x0, float y1)
5049 ) :: state.rects
5050 | _ -> state.rects
5052 showrects rects;
5053 showsel ();
5054 state.uioh#display;
5055 begin match state.mstate with
5056 | Mzoomrect ((x0, y0), (x1, y1)) ->
5057 Gl.enable `blend;
5058 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5059 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5060 GlDraw.rect (float x0, float y0)
5061 (float x1, float y1);
5062 Gl.disable `blend;
5063 | _ -> ()
5064 end;
5065 enttext ();
5066 scrollindicator ();
5067 Wsi.swapb ();
5070 let zoomrect x y x1 y1 =
5071 let x0 = min x x1
5072 and x1 = max x x1
5073 and y0 = min y y1 in
5074 gotoy (state.y + y0);
5075 state.anchor <- getanchor ();
5076 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5077 let margin =
5078 if state.w < conf.winw - state.scrollw
5079 then (conf.winw - state.scrollw - state.w) / 2
5080 else 0
5082 state.x <- (state.x + margin) - x0;
5083 setzoom zoom;
5084 Wsi.setcursor Wsi.CURSOR_INHERIT;
5085 state.mstate <- Mnone;
5088 let scrollx x =
5089 let winw = conf.winw - state.scrollw - 1 in
5090 let s = float x /. float winw in
5091 let destx = truncate (float (state.w + winw) *. s) in
5092 state.x <- winw - destx;
5093 gotoy_and_clear_text state.y;
5094 state.mstate <- Mscrollx;
5097 let scrolly y =
5098 let s = float y /. float conf.winh in
5099 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5100 gotoy_and_clear_text desty;
5101 state.mstate <- Mscrolly;
5104 let viewmouse button down x y mask =
5105 match button with
5106 | n when (n == 4 || n == 5) && not down ->
5107 if Wsi.withctrl mask
5108 then (
5109 match state.mstate with
5110 | Mzoom (oldn, i) ->
5111 if oldn = n
5112 then (
5113 if i = 2
5114 then
5115 let incr =
5116 match n with
5117 | 5 ->
5118 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5119 | _ ->
5120 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5122 let zoom = conf.zoom -. incr in
5123 setzoom zoom;
5124 state.mstate <- Mzoom (n, 0);
5125 else
5126 state.mstate <- Mzoom (n, i+1);
5128 else state.mstate <- Mzoom (n, 0)
5130 | _ -> state.mstate <- Mzoom (n, 0)
5132 else (
5133 match state.autoscroll with
5134 | Some step -> setautoscrollspeed step (n=4)
5135 | None ->
5136 let incr =
5137 if n = 4
5138 then -conf.scrollstep
5139 else conf.scrollstep
5141 let incr = incr * 2 in
5142 let y = clamp incr in
5143 gotoy_and_clear_text y
5146 | 1 when Wsi.withctrl mask ->
5147 if down
5148 then (
5149 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5150 state.mstate <- Mpan (x, y)
5152 else
5153 state.mstate <- Mnone
5155 | 3 ->
5156 if down
5157 then (
5158 Wsi.setcursor Wsi.CURSOR_CYCLE;
5159 let p = (x, y) in
5160 state.mstate <- Mzoomrect (p, p)
5162 else (
5163 match state.mstate with
5164 | Mzoomrect ((x0, y0), _) ->
5165 if abs (x-x0) > 10 && abs (y - y0) > 10
5166 then zoomrect x0 y0 x y
5167 else (
5168 state.mstate <- Mnone;
5169 Wsi.setcursor Wsi.CURSOR_INHERIT;
5170 G.postRedisplay "kill accidental zoom rect";
5172 | _ ->
5173 Wsi.setcursor Wsi.CURSOR_INHERIT;
5174 state.mstate <- Mnone
5177 | 1 when x > conf.winw - state.scrollw ->
5178 if down
5179 then
5180 let _, position, sh = state.uioh#scrollph in
5181 if y > truncate position && y < truncate (position +. sh)
5182 then state.mstate <- Mscrolly
5183 else scrolly y
5184 else
5185 state.mstate <- Mnone
5187 | 1 when y > conf.winh - state.hscrollh ->
5188 if down
5189 then
5190 let _, position, sw = state.uioh#scrollpw in
5191 if x > truncate position && x < truncate (position +. sw)
5192 then state.mstate <- Mscrollx
5193 else scrollx x
5194 else
5195 state.mstate <- Mnone
5197 | 1 ->
5198 let dest = if down then getunder x y else Unone in
5199 begin match dest with
5200 | Ulinkgoto (pageno, top) ->
5201 if pageno >= 0
5202 then (
5203 addnav ();
5204 gotopage1 pageno top;
5207 | Ulinkuri s ->
5208 gotouri s
5210 | Uremote (filename, pageno) ->
5211 let path =
5212 if Sys.file_exists filename
5213 then filename
5214 else
5215 let dir = Filename.dirname state.path in
5216 let path = Filename.concat dir filename in
5217 if Sys.file_exists path
5218 then path
5219 else ""
5221 if String.length path > 0
5222 then (
5223 let anchor = getanchor () in
5224 let ranchor = state.path, state.password, anchor in
5225 state.anchor <- (pageno, 0.0);
5226 state.ranchors <- ranchor :: state.ranchors;
5227 opendoc path "";
5229 else showtext '!' ("Could not find " ^ filename)
5231 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
5233 | Unone when down ->
5234 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5235 state.mstate <- Mpan (x, y);
5237 | Unone | Utext _ ->
5238 if down
5239 then (
5240 if conf.angle mod 360 = 0
5241 then (
5242 state.mstate <- Msel ((x, y), (x, y));
5243 G.postRedisplay "mouse select";
5246 else (
5247 match state.mstate with
5248 | Mnone -> ()
5250 | Mzoom _ | Mscrollx | Mscrolly ->
5251 state.mstate <- Mnone
5253 | Mzoomrect ((x0, y0), _) ->
5254 zoomrect x0 y0 x y
5256 | Mpan _ ->
5257 Wsi.setcursor Wsi.CURSOR_INHERIT;
5258 state.mstate <- Mnone
5260 | Msel ((_, y0), (_, y1)) ->
5261 let rec loop = function
5262 | [] -> ()
5263 | l :: rest ->
5264 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5265 || ((y1 >= l.pagedispy
5266 && y1 <= (l.pagedispy + l.pagevh)))
5267 then
5268 match getopaque l.pageno with
5269 | Some opaque ->
5270 copysel conf.selcmd opaque;
5271 G.postRedisplay "copysel"
5272 | _ -> ()
5273 else loop rest
5275 loop state.layout;
5276 Wsi.setcursor Wsi.CURSOR_INHERIT;
5277 state.mstate <- Mnone;
5281 | _ -> ()
5284 let birdseyemouse button down x y mask
5285 (conf, leftx, _, hooverpageno, anchor) =
5286 match button with
5287 | 1 when down ->
5288 let rec loop = function
5289 | [] -> ()
5290 | l :: rest ->
5291 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5292 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5293 then (
5294 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5296 else loop rest
5298 loop state.layout
5299 | 3 -> ()
5300 | _ -> viewmouse button down x y mask
5303 let mouse button down x y mask =
5304 state.uioh <- state.uioh#button button down x y mask;
5307 let motion ~x ~y =
5308 state.uioh <- state.uioh#motion x y
5311 let pmotion ~x ~y =
5312 state.uioh <- state.uioh#pmotion x y;
5315 let uioh = object
5316 method display = ()
5318 method key key mask =
5319 begin match state.mode with
5320 | Textentry textentry -> textentrykeyboard key mask textentry
5321 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5322 | View -> viewkeyboard key mask
5323 | LinkNav linknav -> linknavkeyboard key mask linknav
5324 end;
5325 state.uioh
5327 method button button bstate x y mask =
5328 begin match state.mode with
5329 | LinkNav _
5330 | View -> viewmouse button bstate x y mask
5331 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5332 | Textentry _ -> ()
5333 end;
5334 state.uioh
5336 method motion x y =
5337 begin match state.mode with
5338 | Textentry _ -> ()
5339 | View | Birdseye _ | LinkNav _ ->
5340 match state.mstate with
5341 | Mzoom _ | Mnone -> ()
5343 | Mpan (x0, y0) ->
5344 let dx = x - x0
5345 and dy = y0 - y in
5346 state.mstate <- Mpan (x, y);
5347 if conf.zoom > 1.0 then state.x <- state.x + dx;
5348 let y = clamp dy in
5349 gotoy_and_clear_text y
5351 | Msel (a, _) ->
5352 state.mstate <- Msel (a, (x, y));
5353 G.postRedisplay "motion select";
5355 | Mscrolly ->
5356 let y = min conf.winh (max 0 y) in
5357 scrolly y
5359 | Mscrollx ->
5360 let x = min conf.winw (max 0 x) in
5361 scrollx x
5363 | Mzoomrect (p0, _) ->
5364 state.mstate <- Mzoomrect (p0, (x, y));
5365 G.postRedisplay "motion zoomrect";
5366 end;
5367 state.uioh
5369 method pmotion x y =
5370 begin match state.mode with
5371 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5372 let rec loop = function
5373 | [] ->
5374 if hooverpageno != -1
5375 then (
5376 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5377 G.postRedisplay "pmotion birdseye no hoover";
5379 | l :: rest ->
5380 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5381 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5382 then (
5383 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5384 G.postRedisplay "pmotion birdseye hoover";
5386 else loop rest
5388 loop state.layout
5390 | Textentry _ -> ()
5392 | LinkNav _
5393 | View ->
5394 match state.mstate with
5395 | Mnone -> updateunder x y
5396 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5398 end;
5399 state.uioh
5401 method infochanged _ = ()
5403 method scrollph =
5404 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5405 let p, h = scrollph state.y maxy in
5406 state.scrollw, p, h
5408 method scrollpw =
5409 let winw = conf.winw - state.scrollw - 1 in
5410 let fwinw = float winw in
5411 let sw =
5412 let sw = fwinw /. float state.w in
5413 let sw = fwinw *. sw in
5414 max sw (float conf.scrollh)
5416 let position, sw =
5417 let f = state.w+winw in
5418 let r = float (winw-state.x) /. float f in
5419 let p = fwinw *. r in
5420 p-.sw/.2., sw
5422 let sw =
5423 if position +. sw > fwinw
5424 then fwinw -. position
5425 else sw
5427 state.hscrollh, position, sw
5429 method modehash =
5430 let modename =
5431 match state.mode with
5432 | LinkNav _ -> "links"
5433 | Textentry _ -> "textentry"
5434 | Birdseye _ -> "birdseye"
5435 | View -> "global"
5437 findkeyhash conf modename
5438 end;;
5440 module Config =
5441 struct
5442 open Parser
5444 let fontpath = ref "";;
5446 module KeyMap =
5447 Map.Make (struct type t = (int * int) let compare = compare end);;
5449 let unent s =
5450 let l = String.length s in
5451 let b = Buffer.create l in
5452 unent b s 0 l;
5453 Buffer.contents b;
5456 let home =
5457 try Sys.getenv "HOME"
5458 with exn ->
5459 prerr_endline
5460 ("Can not determine home directory location: " ^
5461 Printexc.to_string exn);
5465 let modifier_of_string = function
5466 | "alt" -> Wsi.altmask
5467 | "shift" -> Wsi.shiftmask
5468 | "ctrl" | "control" -> Wsi.ctrlmask
5469 | "meta" -> Wsi.metamask
5470 | _ -> 0
5473 let key_of_string =
5474 let r = Str.regexp "-" in
5475 fun s ->
5476 let elems = Str.full_split r s in
5477 let f n k m =
5478 let g s =
5479 let m1 = modifier_of_string s in
5480 if m1 = 0
5481 then (Wsi.namekey s, m)
5482 else (k, m lor m1)
5483 in function
5484 | Str.Delim s when n land 1 = 0 -> g s
5485 | Str.Text s -> g s
5486 | Str.Delim _ -> (k, m)
5488 let rec loop n k m = function
5489 | [] -> (k, m)
5490 | x :: xs ->
5491 let k, m = f n k m x in
5492 loop (n+1) k m xs
5494 loop 0 0 0 elems
5497 let keys_of_string =
5498 let r = Str.regexp "[ \t]" in
5499 fun s ->
5500 let elems = Str.split r s in
5501 List.map key_of_string elems
5504 let copykeyhashes c =
5505 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5508 let config_of c attrs =
5509 let apply c k v =
5511 match k with
5512 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5513 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5514 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5515 | "preload" -> { c with preload = bool_of_string v }
5516 | "page-bias" -> { c with pagebias = int_of_string v }
5517 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5518 | "auto-scroll-step" ->
5519 { c with autoscrollstep = max 0 (int_of_string v) }
5520 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5521 | "crop-hack" -> { c with crophack = bool_of_string v }
5522 | "throttle" ->
5523 let mw =
5524 match String.lowercase v with
5525 | "true" -> Some infinity
5526 | "false" -> None
5527 | f -> Some (float_of_string f)
5529 { c with maxwait = mw}
5530 | "highlight-links" -> { c with hlinks = bool_of_string v }
5531 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5532 | "vertical-margin" ->
5533 { c with interpagespace = max 0 (int_of_string v) }
5534 | "zoom" ->
5535 let zoom = float_of_string v /. 100. in
5536 let zoom = max zoom 0.0 in
5537 { c with zoom = zoom }
5538 | "presentation" -> { c with presentation = bool_of_string v }
5539 | "rotation-angle" -> { c with angle = int_of_string v }
5540 | "width" -> { c with winw = max 20 (int_of_string v) }
5541 | "height" -> { c with winh = max 20 (int_of_string v) }
5542 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5543 | "proportional-display" -> { c with proportional = bool_of_string v }
5544 | "pixmap-cache-size" ->
5545 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5546 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5547 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5548 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5549 | "persistent-location" -> { c with jumpback = bool_of_string v }
5550 | "background-color" -> { c with bgcolor = color_of_string v }
5551 | "scrollbar-in-presentation" ->
5552 { c with scrollbarinpm = bool_of_string v }
5553 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5554 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5555 | "mupdf-store-size" ->
5556 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5557 | "checkers" -> { c with checkers = bool_of_string v }
5558 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5559 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5560 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5561 | "uri-launcher" -> { c with urilauncher = unent v }
5562 | "path-launcher" -> { c with pathlauncher = unent v }
5563 | "color-space" -> { c with colorspace = colorspace_of_string v }
5564 | "invert-colors" -> { c with invert = bool_of_string v }
5565 | "brightness" -> { c with colorscale = float_of_string v }
5566 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5567 | "ghyllscroll" ->
5568 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5569 | "columns" ->
5570 let nab = columns_of_string v in
5571 { c with columns = Some (nab, [||]) }
5572 | "birds-eye-columns" ->
5573 { c with beyecolumns = Some (max (int_of_string v) 2) }
5574 | "selection-command" -> { c with selcmd = unent v }
5575 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5576 | _ -> c
5577 with exn ->
5578 prerr_endline ("Error processing attribute (`" ^
5579 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5582 let rec fold c = function
5583 | [] -> c
5584 | (k, v) :: rest ->
5585 let c = apply c k v in
5586 fold c rest
5588 fold { c with keyhashes = copykeyhashes c } attrs;
5591 let fromstring f pos n v d =
5592 try f v
5593 with exn ->
5594 dolog "Error processing attribute (%S=%S) at %d\n%s"
5595 n v pos (Printexc.to_string exn)
5600 let bookmark_of attrs =
5601 let rec fold title page rely = function
5602 | ("title", v) :: rest -> fold v page rely rest
5603 | ("page", v) :: rest -> fold title v rely rest
5604 | ("rely", v) :: rest -> fold title page v rest
5605 | _ :: rest -> fold title page rely rest
5606 | [] -> title, page, rely
5608 fold "invalid" "0" "0" attrs
5611 let doc_of attrs =
5612 let rec fold path page rely pan = function
5613 | ("path", v) :: rest -> fold v page rely pan rest
5614 | ("page", v) :: rest -> fold path v rely pan rest
5615 | ("rely", v) :: rest -> fold path page v pan rest
5616 | ("pan", v) :: rest -> fold path page rely v rest
5617 | _ :: rest -> fold path page rely pan rest
5618 | [] -> path, page, rely, pan
5620 fold "" "0" "0" "0" attrs
5623 let map_of attrs =
5624 let rec fold rs ls = function
5625 | ("out", v) :: rest -> fold v ls rest
5626 | ("in", v) :: rest -> fold rs v rest
5627 | _ :: rest -> fold ls rs rest
5628 | [] -> ls, rs
5630 fold "" "" attrs
5633 let setconf dst src =
5634 dst.scrollbw <- src.scrollbw;
5635 dst.scrollh <- src.scrollh;
5636 dst.icase <- src.icase;
5637 dst.preload <- src.preload;
5638 dst.pagebias <- src.pagebias;
5639 dst.verbose <- src.verbose;
5640 dst.scrollstep <- src.scrollstep;
5641 dst.maxhfit <- src.maxhfit;
5642 dst.crophack <- src.crophack;
5643 dst.autoscrollstep <- src.autoscrollstep;
5644 dst.maxwait <- src.maxwait;
5645 dst.hlinks <- src.hlinks;
5646 dst.underinfo <- src.underinfo;
5647 dst.interpagespace <- src.interpagespace;
5648 dst.zoom <- src.zoom;
5649 dst.presentation <- src.presentation;
5650 dst.angle <- src.angle;
5651 dst.winw <- src.winw;
5652 dst.winh <- src.winh;
5653 dst.savebmarks <- src.savebmarks;
5654 dst.memlimit <- src.memlimit;
5655 dst.proportional <- src.proportional;
5656 dst.texcount <- src.texcount;
5657 dst.sliceheight <- src.sliceheight;
5658 dst.thumbw <- src.thumbw;
5659 dst.jumpback <- src.jumpback;
5660 dst.bgcolor <- src.bgcolor;
5661 dst.scrollbarinpm <- src.scrollbarinpm;
5662 dst.tilew <- src.tilew;
5663 dst.tileh <- src.tileh;
5664 dst.mustoresize <- src.mustoresize;
5665 dst.checkers <- src.checkers;
5666 dst.aalevel <- src.aalevel;
5667 dst.trimmargins <- src.trimmargins;
5668 dst.trimfuzz <- src.trimfuzz;
5669 dst.urilauncher <- src.urilauncher;
5670 dst.colorspace <- src.colorspace;
5671 dst.invert <- src.invert;
5672 dst.colorscale <- src.colorscale;
5673 dst.redirectstderr <- src.redirectstderr;
5674 dst.ghyllscroll <- src.ghyllscroll;
5675 dst.columns <- src.columns;
5676 dst.beyecolumns <- src.beyecolumns;
5677 dst.selcmd <- src.selcmd;
5678 dst.updatecurs <- src.updatecurs;
5679 dst.pathlauncher <- src.pathlauncher;
5680 dst.keyhashes <- copykeyhashes src;
5683 let get s =
5684 let h = Hashtbl.create 10 in
5685 let dc = { defconf with angle = defconf.angle } in
5686 let rec toplevel v t spos _ =
5687 match t with
5688 | Vdata | Vcdata | Vend -> v
5689 | Vopen ("llppconfig", _, closed) ->
5690 if closed
5691 then v
5692 else { v with f = llppconfig }
5693 | Vopen _ ->
5694 error "unexpected subelement at top level" s spos
5695 | Vclose _ -> error "unexpected close at top level" s spos
5697 and llppconfig v t spos _ =
5698 match t with
5699 | Vdata | Vcdata -> v
5700 | Vend -> error "unexpected end of input in llppconfig" s spos
5701 | Vopen ("defaults", attrs, closed) ->
5702 let c = config_of dc attrs in
5703 setconf dc c;
5704 if closed
5705 then v
5706 else { v with f = defaults }
5708 | Vopen ("ui-font", attrs, closed) ->
5709 let rec getsize size = function
5710 | [] -> size
5711 | ("size", v) :: rest ->
5712 let size =
5713 fromstring int_of_string spos "size" v fstate.fontsize in
5714 getsize size rest
5715 | l -> getsize size l
5717 fstate.fontsize <- getsize fstate.fontsize attrs;
5718 if closed
5719 then v
5720 else { v with f = uifont (Buffer.create 10) }
5722 | Vopen ("doc", attrs, closed) ->
5723 let pathent, spage, srely, span = doc_of attrs in
5724 let path = unent pathent
5725 and pageno = fromstring int_of_string spos "page" spage 0
5726 and rely = fromstring float_of_string spos "rely" srely 0.0
5727 and pan = fromstring int_of_string spos "pan" span 0 in
5728 let c = config_of dc attrs in
5729 let anchor = (pageno, rely) in
5730 if closed
5731 then (Hashtbl.add h path (c, [], pan, anchor); v)
5732 else { v with f = doc path pan anchor c [] }
5734 | Vopen _ ->
5735 error "unexpected subelement in llppconfig" s spos
5737 | Vclose "llppconfig" -> { v with f = toplevel }
5738 | Vclose _ -> error "unexpected close in llppconfig" s spos
5740 and defaults v t spos _ =
5741 match t with
5742 | Vdata | Vcdata -> v
5743 | Vend -> error "unexpected end of input in defaults" s spos
5744 | Vopen ("keymap", attrs, closed) ->
5745 let modename =
5746 try List.assoc "mode" attrs
5747 with Not_found -> "global" in
5748 if closed
5749 then v
5750 else
5751 let ret keymap =
5752 let h = findkeyhash dc modename in
5753 KeyMap.iter (Hashtbl.replace h) keymap;
5754 defaults
5756 { v with f = pkeymap ret KeyMap.empty }
5758 | Vopen (_, _, _) ->
5759 error "unexpected subelement in defaults" s spos
5761 | Vclose "defaults" ->
5762 { v with f = llppconfig }
5764 | Vclose _ -> error "unexpected close in defaults" s spos
5766 and uifont b v t spos epos =
5767 match t with
5768 | Vdata | Vcdata ->
5769 Buffer.add_substring b s spos (epos - spos);
5771 | Vopen (_, _, _) ->
5772 error "unexpected subelement in ui-font" s spos
5773 | Vclose "ui-font" ->
5774 if String.length !fontpath = 0
5775 then fontpath := Buffer.contents b;
5776 { v with f = llppconfig }
5777 | Vclose _ -> error "unexpected close in ui-font" s spos
5778 | Vend -> error "unexpected end of input in ui-font" s spos
5780 and doc path pan anchor c bookmarks v t spos _ =
5781 match t with
5782 | Vdata | Vcdata -> v
5783 | Vend -> error "unexpected end of input in doc" s spos
5784 | Vopen ("bookmarks", _, closed) ->
5785 if closed
5786 then v
5787 else { v with f = pbookmarks path pan anchor c bookmarks }
5789 | Vopen ("keymap", attrs, closed) ->
5790 let modename =
5791 try List.assoc "mode" attrs
5792 with Not_found -> "global"
5794 if closed
5795 then v
5796 else
5797 let ret keymap =
5798 let h = findkeyhash c modename in
5799 KeyMap.iter (Hashtbl.replace h) keymap;
5800 doc path pan anchor c bookmarks
5802 { v with f = pkeymap ret KeyMap.empty }
5804 | Vopen (_, _, _) ->
5805 error "unexpected subelement in doc" s spos
5807 | Vclose "doc" ->
5808 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5809 { v with f = llppconfig }
5811 | Vclose _ -> error "unexpected close in doc" s spos
5813 and pkeymap ret keymap v t spos _ =
5814 match t with
5815 | Vdata | Vcdata -> v
5816 | Vend -> error "unexpected end of input in keymap" s spos
5817 | Vopen ("map", attrs, closed) ->
5818 let r, l = map_of attrs in
5819 let kss = fromstring keys_of_string spos "in" r [] in
5820 let lss = fromstring keys_of_string spos "out" l [] in
5821 let keymap =
5822 match kss with
5823 | [] -> keymap
5824 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5825 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5827 if closed
5828 then { v with f = pkeymap ret keymap }
5829 else
5830 let f () = v in
5831 { v with f = skip "map" f }
5833 | Vopen _ ->
5834 error "unexpected subelement in keymap" s spos
5836 | Vclose "keymap" ->
5837 { v with f = ret keymap }
5839 | Vclose _ -> error "unexpected close in keymap" s spos
5841 and pbookmarks path pan anchor c bookmarks v t spos _ =
5842 match t with
5843 | Vdata | Vcdata -> v
5844 | Vend -> error "unexpected end of input in bookmarks" s spos
5845 | Vopen ("item", attrs, closed) ->
5846 let titleent, spage, srely = bookmark_of attrs in
5847 let page = fromstring int_of_string spos "page" spage 0
5848 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5849 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5850 if closed
5851 then { v with f = pbookmarks path pan anchor c bookmarks }
5852 else
5853 let f () = v in
5854 { v with f = skip "item" f }
5856 | Vopen _ ->
5857 error "unexpected subelement in bookmarks" s spos
5859 | Vclose "bookmarks" ->
5860 { v with f = doc path pan anchor c bookmarks }
5862 | Vclose _ -> error "unexpected close in bookmarks" s spos
5864 and skip tag f v t spos _ =
5865 match t with
5866 | Vdata | Vcdata -> v
5867 | Vend ->
5868 error ("unexpected end of input in skipped " ^ tag) s spos
5869 | Vopen (tag', _, closed) ->
5870 if closed
5871 then v
5872 else
5873 let f' () = { v with f = skip tag f } in
5874 { v with f = skip tag' f' }
5875 | Vclose ctag ->
5876 if tag = ctag
5877 then f ()
5878 else error ("unexpected close in skipped " ^ tag) s spos
5881 parse { f = toplevel; accu = () } s;
5882 h, dc;
5885 let do_load f ic =
5887 let len = in_channel_length ic in
5888 let s = String.create len in
5889 really_input ic s 0 len;
5890 f s;
5891 with
5892 | Parse_error (msg, s, pos) ->
5893 let subs = subs s pos in
5894 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5895 failwith ("parse error: " ^ s)
5897 | exn ->
5898 failwith ("config load error: " ^ Printexc.to_string exn)
5901 let defconfpath =
5902 let dir =
5904 let dir = Filename.concat home ".config" in
5905 if Sys.is_directory dir then dir else home
5906 with _ -> home
5908 Filename.concat dir "llpp.conf"
5911 let confpath = ref defconfpath;;
5913 let load1 f =
5914 if Sys.file_exists !confpath
5915 then
5916 match
5917 (try Some (open_in_bin !confpath)
5918 with exn ->
5919 prerr_endline
5920 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5921 Printexc.to_string exn);
5922 None
5924 with
5925 | Some ic ->
5926 begin try
5927 f (do_load get ic)
5928 with exn ->
5929 prerr_endline
5930 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5931 Printexc.to_string exn);
5932 end;
5933 close_in ic;
5935 | None -> ()
5936 else
5937 f (Hashtbl.create 0, defconf)
5940 let load () =
5941 let f (h, dc) =
5942 let pc, pb, px, pa =
5944 Hashtbl.find h (Filename.basename state.path)
5945 with Not_found -> dc, [], 0, (0, 0.0)
5947 setconf defconf dc;
5948 setconf conf pc;
5949 state.bookmarks <- pb;
5950 state.x <- px;
5951 state.scrollw <- conf.scrollbw;
5952 if conf.jumpback
5953 then state.anchor <- pa;
5954 cbput state.hists.nav pa;
5956 load1 f
5959 let add_attrs bb always dc c =
5960 let ob s a b =
5961 if always || a != b
5962 then Printf.bprintf bb "\n %s='%b'" s a
5963 and oi s a b =
5964 if always || a != b
5965 then Printf.bprintf bb "\n %s='%d'" s a
5966 and oI s a b =
5967 if always || a != b
5968 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5969 and oz s a b =
5970 if always || a <> b
5971 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5972 and oF s a b =
5973 if always || a <> b
5974 then Printf.bprintf bb "\n %s='%f'" s a
5975 and oc s a b =
5976 if always || a <> b
5977 then
5978 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5979 and oC s a b =
5980 if always || a <> b
5981 then
5982 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5983 and oR s a b =
5984 if always || a <> b
5985 then
5986 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5987 and os s a b =
5988 if always || a <> b
5989 then
5990 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5991 and og s a b =
5992 if always || a <> b
5993 then
5994 match a with
5995 | None -> ()
5996 | Some (_N, _A, _B) ->
5997 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5998 and oW s a b =
5999 if always || a <> b
6000 then
6001 let v =
6002 match a with
6003 | None -> "false"
6004 | Some f ->
6005 if f = infinity
6006 then "true"
6007 else string_of_float f
6009 Printf.bprintf bb "\n %s='%s'" s v
6010 and oco s a b =
6011 if always || a <> b
6012 then
6013 match a with
6014 | Some ((n, a, b), _) when n > 1 ->
6015 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6016 | _ -> ()
6017 and obeco s a b =
6018 if always || a <> b
6019 then
6020 match a with
6021 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6022 | _ -> ()
6024 let w, h =
6025 if always
6026 then dc.winw, dc.winh
6027 else
6028 match state.fullscreen with
6029 | Some wh -> wh
6030 | None -> c.winw, c.winh
6032 let zoom, presentation, interpagespace, maxwait =
6033 if always
6034 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6035 else
6036 match state.mode with
6037 | Birdseye (bc, _, _, _, _) ->
6038 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6039 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6041 oi "width" w dc.winw;
6042 oi "height" h dc.winh;
6043 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6044 oi "scroll-handle-height" c.scrollh dc.scrollh;
6045 ob "case-insensitive-search" c.icase dc.icase;
6046 ob "preload" c.preload dc.preload;
6047 oi "page-bias" c.pagebias dc.pagebias;
6048 oi "scroll-step" c.scrollstep dc.scrollstep;
6049 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6050 ob "max-height-fit" c.maxhfit dc.maxhfit;
6051 ob "crop-hack" c.crophack dc.crophack;
6052 oW "throttle" maxwait dc.maxwait;
6053 ob "highlight-links" c.hlinks dc.hlinks;
6054 ob "under-cursor-info" c.underinfo dc.underinfo;
6055 oi "vertical-margin" interpagespace dc.interpagespace;
6056 oz "zoom" zoom dc.zoom;
6057 ob "presentation" presentation dc.presentation;
6058 oi "rotation-angle" c.angle dc.angle;
6059 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6060 ob "proportional-display" c.proportional dc.proportional;
6061 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6062 oi "tex-count" c.texcount dc.texcount;
6063 oi "slice-height" c.sliceheight dc.sliceheight;
6064 oi "thumbnail-width" c.thumbw dc.thumbw;
6065 ob "persistent-location" c.jumpback dc.jumpback;
6066 oc "background-color" c.bgcolor dc.bgcolor;
6067 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6068 oi "tile-width" c.tilew dc.tilew;
6069 oi "tile-height" c.tileh dc.tileh;
6070 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6071 ob "checkers" c.checkers dc.checkers;
6072 oi "aalevel" c.aalevel dc.aalevel;
6073 ob "trim-margins" c.trimmargins dc.trimmargins;
6074 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6075 os "uri-launcher" c.urilauncher dc.urilauncher;
6076 os "path-launcher" c.pathlauncher dc.pathlauncher;
6077 oC "color-space" c.colorspace dc.colorspace;
6078 ob "invert-colors" c.invert dc.invert;
6079 oF "brightness" c.colorscale dc.colorscale;
6080 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6081 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6082 oco "columns" c.columns dc.columns;
6083 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6084 os "selection-command" c.selcmd dc.selcmd;
6085 ob "update-cursor" c.updatecurs dc.updatecurs;
6088 let keymapsbuf always dc c =
6089 let bb = Buffer.create 16 in
6090 let rec loop = function
6091 | [] -> ()
6092 | (modename, h) :: rest ->
6093 let dh = findkeyhash dc modename in
6094 if always || h <> dh
6095 then (
6096 if Hashtbl.length h > 0
6097 then (
6098 if Buffer.length bb > 0
6099 then Buffer.add_char bb '\n';
6100 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6101 Hashtbl.iter (fun i o ->
6102 let isdifferent = always ||
6104 let dO = Hashtbl.find dh i in
6105 dO <> o
6106 with Not_found -> true
6108 if isdifferent
6109 then
6110 let addkm (k, m) =
6111 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6112 if Wsi.withalt m then Buffer.add_string bb "alt-";
6113 if Wsi.withshift m then Buffer.add_string bb "shift-";
6114 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6115 Buffer.add_string bb (Wsi.keyname k);
6117 let addkms l =
6118 let rec loop = function
6119 | [] -> ()
6120 | km :: [] -> addkm km
6121 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6123 loop l
6125 Buffer.add_string bb "<map in='";
6126 addkm i;
6127 match o with
6128 | KMinsrt km ->
6129 Buffer.add_string bb "' out='";
6130 addkm km;
6131 Buffer.add_string bb "'/>\n"
6133 | KMinsrl kms ->
6134 Buffer.add_string bb "' out='";
6135 addkms kms;
6136 Buffer.add_string bb "'/>\n"
6138 | KMmulti (ins, kms) ->
6139 Buffer.add_char bb ' ';
6140 addkms ins;
6141 Buffer.add_string bb "' out='";
6142 addkms kms;
6143 Buffer.add_string bb "'/>\n"
6144 ) h;
6145 Buffer.add_string bb "</keymap>";
6148 loop rest
6150 loop c.keyhashes;
6154 let save () =
6155 let uifontsize = fstate.fontsize in
6156 let bb = Buffer.create 32768 in
6157 let f (h, dc) =
6158 let dc = if conf.bedefault then conf else dc in
6159 Buffer.add_string bb "<llppconfig>\n";
6161 if String.length !fontpath > 0
6162 then
6163 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6164 uifontsize
6165 !fontpath
6166 else (
6167 if uifontsize <> 14
6168 then
6169 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6172 Buffer.add_string bb "<defaults ";
6173 add_attrs bb true dc dc;
6174 let kb = keymapsbuf true dc dc in
6175 if Buffer.length kb > 0
6176 then (
6177 Buffer.add_string bb ">\n";
6178 Buffer.add_buffer bb kb;
6179 Buffer.add_string bb "\n</defaults>\n";
6181 else Buffer.add_string bb "/>\n";
6183 let adddoc path pan anchor c bookmarks =
6184 if bookmarks == [] && c = dc && anchor = emptyanchor
6185 then ()
6186 else (
6187 Printf.bprintf bb "<doc path='%s'"
6188 (enent path 0 (String.length path));
6190 if anchor <> emptyanchor
6191 then (
6192 let n, y = anchor in
6193 Printf.bprintf bb " page='%d'" n;
6194 if y > 1e-6
6195 then
6196 Printf.bprintf bb " rely='%f'" y
6200 if pan != 0
6201 then Printf.bprintf bb " pan='%d'" pan;
6203 add_attrs bb false dc c;
6204 let kb = keymapsbuf false dc c in
6206 begin match bookmarks with
6207 | [] ->
6208 if Buffer.length kb > 0
6209 then (
6210 Buffer.add_string bb ">\n";
6211 Buffer.add_buffer bb kb;
6212 Buffer.add_string bb "</doc>\n";
6214 else Buffer.add_string bb "/>\n"
6215 | _ ->
6216 Buffer.add_string bb ">\n<bookmarks>\n";
6217 List.iter (fun (title, _level, (page, rely)) ->
6218 Printf.bprintf bb
6219 "<item title='%s' page='%d'"
6220 (enent title 0 (String.length title))
6221 page
6223 if rely > 1e-6
6224 then
6225 Printf.bprintf bb " rely='%f'" rely
6227 Buffer.add_string bb "/>\n";
6228 ) bookmarks;
6229 Buffer.add_string bb "</bookmarks>";
6230 if Buffer.length kb > 0
6231 then (
6232 Buffer.add_string bb "\n";
6233 Buffer.add_buffer bb kb;
6235 Buffer.add_string bb "\n</doc>\n";
6236 end;
6240 let pan, conf =
6241 match state.mode with
6242 | Birdseye (c, pan, _, _, _) ->
6243 let beyecolumns =
6244 match conf.columns with
6245 | Some ((c, _, _), _) -> Some c
6246 | None -> None
6247 and columns =
6248 match c.columns with
6249 | Some (c, _) -> Some (c, [||])
6250 | None -> None
6252 pan, { c with beyecolumns = beyecolumns; columns = columns }
6253 | _ -> state.x, conf
6255 let basename = Filename.basename state.path in
6256 adddoc basename pan (getanchor ())
6257 { conf with
6258 autoscrollstep =
6259 match state.autoscroll with
6260 | Some step -> step
6261 | None -> conf.autoscrollstep }
6262 (if conf.savebmarks then state.bookmarks else []);
6264 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6265 if basename <> path
6266 then adddoc path x y c bookmarks
6267 ) h;
6268 Buffer.add_string bb "</llppconfig>";
6270 load1 f;
6271 if Buffer.length bb > 0
6272 then
6274 let tmp = !confpath ^ ".tmp" in
6275 let oc = open_out_bin tmp in
6276 Buffer.output_buffer oc bb;
6277 close_out oc;
6278 Unix.rename tmp !confpath;
6279 with exn ->
6280 prerr_endline
6281 ("error while saving configuration: " ^ Printexc.to_string exn)
6283 end;;
6285 let () =
6286 Arg.parse
6287 (Arg.align
6288 [("-p", Arg.String (fun s -> state.password <- s) ,
6289 "<password> Set password");
6291 ("-f", Arg.String (fun s -> Config.fontpath := s),
6292 "<path> Set path to the user interface font");
6294 ("-c", Arg.String (fun s -> Config.confpath := s),
6295 "<path> Set path to the configuration file");
6297 ("-v", Arg.Unit (fun () ->
6298 Printf.printf
6299 "%s\nconfiguration path: %s\n"
6300 (version ())
6301 Config.defconfpath
6303 exit 0), " Print version and exit");
6306 (fun s -> state.path <- s)
6307 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6309 if String.length state.path = 0
6310 then (prerr_endline "file name missing"; exit 1);
6312 Config.load ();
6314 let globalkeyhash = findkeyhash conf "global" in
6315 state.wsfd <- Wsi.init (object
6316 method expose =
6317 if nogeomcmds state.geomcmds
6318 then display ()
6319 method display = display ()
6320 method reshape w h = reshape w h
6321 method mouse b d x y m = mouse b d x y m
6322 method motion x y = state.mpos <- (x, y); motion x y
6323 method pmotion x y = state.mpos <- (x, y); pmotion x y
6324 method key k m =
6325 match state.keystate with
6326 | KSnone ->
6327 let km = k, m in
6328 begin
6329 match
6330 try Hashtbl.find globalkeyhash km
6331 with Not_found ->
6332 let modehash = state.uioh#modehash in
6333 try Hashtbl.find modehash km
6334 with Not_found -> KMinsrt (k, m)
6335 with
6336 | KMinsrt (k, m) -> keyboard k m
6337 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6338 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6340 | KSinto ((k', m') :: [], insrt) when k'=k && m' land m = m' ->
6341 List.iter (fun (k, m) -> keyboard k m) insrt;
6342 state.keystate <- KSnone
6343 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land m = m' ->
6344 state.keystate <- KSinto (keys, insrt)
6345 | _ ->
6346 state.keystate <- KSnone
6348 method enter x y = state.mpos <- (x, y); pmotion x y
6349 method leave = state.mpos <- (-1, -1)
6350 method quit = raise Quit
6351 end) conf.winw conf.winh;
6353 if not (
6354 List.exists GlMisc.check_extension
6355 [ "GL_ARB_texture_rectangle"
6356 ; "GL_EXT_texture_recangle"
6357 ; "GL_NV_texture_rectangle" ]
6359 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6361 let cr, sw = Unix.pipe ()
6362 and sr, cw = Unix.pipe () in
6364 cloexec cr;
6365 cloexec sw;
6366 cloexec sr;
6367 cloexec cw;
6369 setcheckers conf.checkers;
6370 redirectstderr ();
6372 init (cr, cw) (
6373 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6374 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6375 !Config.fontpath
6377 state.sr <- sr;
6378 state.sw <- sw;
6379 state.text <- "Opening " ^ state.path;
6380 opendoc state.path state.password;
6381 state.uioh <- uioh;
6382 setfontsize fstate.fontsize;
6383 doreshape conf.winw conf.winh;
6385 let rec loop deadline =
6386 let r =
6387 match state.errfd with
6388 | None -> [state.sr; state.wsfd]
6389 | Some fd -> [state.sr; state.wsfd; fd]
6391 if state.redisplay
6392 then (
6393 state.redisplay <- false;
6394 display ();
6396 let timeout =
6397 let now = now () in
6398 if deadline > now
6399 then (
6400 if deadline = infinity
6401 then ~-.1.0
6402 else max 0.0 (deadline -. now)
6404 else 0.0
6406 let r, _, _ =
6407 try Unix.select r [] [] timeout
6408 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6410 begin match r with
6411 | [] ->
6412 state.ghyll None;
6413 let newdeadline =
6414 if state.ghyll == noghyll
6415 then
6416 match state.autoscroll with
6417 | Some step when step != 0 ->
6418 let y = state.y + step in
6419 let y =
6420 if y < 0
6421 then state.maxy
6422 else if y >= state.maxy then 0 else y
6424 gotoy y;
6425 if state.mode = View
6426 then state.text <- "";
6427 deadline +. 0.01
6428 | _ -> infinity
6429 else deadline +. 0.01
6431 loop newdeadline
6433 | l ->
6434 let rec checkfds = function
6435 | [] -> ()
6436 | fd :: rest when fd = state.sr ->
6437 let cmd = readcmd state.sr in
6438 act cmd;
6439 checkfds rest
6441 | fd :: rest when fd = state.wsfd ->
6442 Wsi.readresp fd;
6443 checkfds rest
6445 | fd :: rest ->
6446 let s = String.create 80 in
6447 let n = Unix.read fd s 0 80 in
6448 if conf.redirectstderr
6449 then (
6450 Buffer.add_substring state.errmsgs s 0 n;
6451 state.newerrmsgs <- true;
6452 state.redisplay <- true;
6454 else (
6455 prerr_string (String.sub s 0 n);
6456 flush stderr;
6458 checkfds rest
6460 checkfds l;
6461 let newdeadline =
6462 let deadline1 =
6463 if deadline = infinity
6464 then now () +. 0.01
6465 else deadline
6467 match state.autoscroll with
6468 | Some step when step != 0 -> deadline1
6469 | _ -> if state.ghyll == noghyll then infinity else deadline1
6471 loop newdeadline
6472 end;
6475 loop infinity;
6476 with Quit ->
6477 wcmd "quit";
6478 Config.save ();
6479 exit 0;