Refactor
[llpp.git] / main.ml
blob096ad825e497185bf9beaa59821a08ab70586138
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 ignore (drawstr size x y s);
166 Gl.disable `blend;
167 Gl.disable `texture_2d;
170 let drawstring1 size x y s =
171 drawstr size x y s;
174 let drawstring2 size x y fmt =
175 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
178 let cbcap b = Array.length b.store;;
180 let cbput b v =
181 let cap = cbcap b in
182 b.store.(b.wc) <- v;
183 b.wc <- (b.wc + 1) mod cap;
184 b.rc <- b.wc;
185 b.len <- min (b.len + 1) cap;
188 let cbempty b = b.len = 0;;
190 let cbgetg b circular dir =
191 if cbempty b
192 then b.store.(0)
193 else
194 let rc = b.rc + dir in
195 let rc =
196 if circular
197 then (
198 if rc = -1
199 then b.len-1
200 else (
201 if rc = b.len
202 then 0
203 else rc
206 else max 0 (min rc (b.len-1))
208 b.rc <- rc;
209 b.store.(rc);
212 let cbget b = cbgetg b false;;
213 let cbgetc b = cbgetg b true;;
215 type page =
216 { pageno : int
217 ; pagedimno : int
218 ; pagew : int
219 ; pageh : int
220 ; pagex : int
221 ; pagey : int
222 ; pagevw : int
223 ; pagevh : int
224 ; pagedispx : int
225 ; pagedispy : int
229 let debugl l =
230 dolog "l %d dim=%d {" l.pageno l.pagedimno;
231 dolog " WxH %dx%d" l.pagew l.pageh;
232 dolog " vWxH %dx%d" l.pagevw l.pagevh;
233 dolog " pagex,y %d,%d" l.pagex l.pagey;
234 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
235 dolog "}";
238 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
239 dolog "rect {";
240 dolog " x0,y0=(% f, % f)" x0 y0;
241 dolog " x1,y1=(% f, % f)" x1 y1;
242 dolog " x2,y2=(% f, % f)" x2 y2;
243 dolog " x3,y3=(% f, % f)" x3 y3;
244 dolog "}";
247 type columns =
248 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
249 and multicol = columncount * covercount * covercount
250 and pdimno = int
251 and columncount = int
252 and covercount = int;;
254 type conf =
255 { mutable scrollbw : int
256 ; mutable scrollh : int
257 ; mutable icase : bool
258 ; mutable preload : bool
259 ; mutable pagebias : int
260 ; mutable verbose : bool
261 ; mutable debug : bool
262 ; mutable scrollstep : int
263 ; mutable maxhfit : bool
264 ; mutable crophack : bool
265 ; mutable autoscrollstep : int
266 ; mutable maxwait : float option
267 ; mutable hlinks : bool
268 ; mutable underinfo : bool
269 ; mutable interpagespace : interpagespace
270 ; mutable zoom : float
271 ; mutable presentation : bool
272 ; mutable angle : angle
273 ; mutable winw : int
274 ; mutable winh : int
275 ; mutable savebmarks : bool
276 ; mutable proportional : proportional
277 ; mutable trimmargins : trimmargins
278 ; mutable trimfuzz : irect
279 ; mutable memlimit : memsize
280 ; mutable texcount : texcount
281 ; mutable sliceheight : sliceheight
282 ; mutable thumbw : width
283 ; mutable jumpback : bool
284 ; mutable bgcolor : float * float * float
285 ; mutable bedefault : bool
286 ; mutable scrollbarinpm : bool
287 ; mutable tilew : int
288 ; mutable tileh : int
289 ; mutable mustoresize : memsize
290 ; mutable checkers : bool
291 ; mutable aalevel : int
292 ; mutable urilauncher : string
293 ; mutable pathlauncher : string
294 ; mutable colorspace : colorspace
295 ; mutable invert : bool
296 ; mutable colorscale : float
297 ; mutable redirectstderr : bool
298 ; mutable ghyllscroll : (int * int * int) option
299 ; mutable columns : columns option
300 ; mutable beyecolumns : columncount option
301 ; mutable selcmd : string
302 ; mutable updatecurs : bool
303 ; mutable keyhashes : (string * keyhash) list
307 type anchor = pageno * top;;
309 type outline = string * int * anchor;;
311 type rect = float * float * float * float * float * float * float * float;;
313 type tile = opaque * pixmapsize * elapsed
314 and elapsed = float;;
315 type pagemapkey = pageno * gen;;
316 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
317 and row = int
318 and col = int;;
320 let emptyanchor = (0, 0.0);;
322 type infochange = | Memused | Docinfo | Pdim;;
324 class type uioh = object
325 method display : unit
326 method key : int -> int -> uioh
327 method button : int -> bool -> int -> int -> int -> uioh
328 method motion : int -> int -> uioh
329 method pmotion : int -> int -> uioh
330 method infochanged : infochange -> unit
331 method scrollpw : (int * float * float)
332 method scrollph : (int * float * float)
333 method modehash : keyhash
334 end;;
336 type mode =
337 | Birdseye of (conf * leftx * pageno * pageno * anchor)
338 | Textentry of (textentry * onleave)
339 | View
340 | LinkNav of linktarget
341 and onleave = leavetextentrystatus -> unit
342 and leavetextentrystatus = | Cancel | Confirm
343 and helpitem = string * int * action
344 and action =
345 | Noaction
346 | Action of (uioh -> uioh)
347 and linktarget =
348 | Ltexact of ((pageno * int) * (int * int * int * int))
349 | Ltgendir of int
352 let isbirdseye = function Birdseye _ -> true | _ -> false;;
353 let istextentry = function Textentry _ -> true | _ -> false;;
355 type currently =
356 | Idle
357 | Loading of (page * gen)
358 | Tiling of (
359 page * opaque * colorspace * angle * gen * col * row * width * height
361 | Outlining of outline list
364 let emptykeyhash = Hashtbl.create 0;;
365 let nouioh : uioh = object (self)
366 method display = ()
367 method key _ _ = self
368 method button _ _ _ _ _ = self
369 method motion _ _ = self
370 method pmotion _ _ = self
371 method infochanged _ = ()
372 method scrollpw = (0, nan, nan)
373 method scrollph = (0, nan, nan)
374 method modehash = emptykeyhash
375 end;;
377 type state =
378 { mutable sr : Unix.file_descr
379 ; mutable sw : Unix.file_descr
380 ; mutable wsfd : Unix.file_descr
381 ; mutable errfd : Unix.file_descr option
382 ; mutable stderr : Unix.file_descr
383 ; mutable errmsgs : Buffer.t
384 ; mutable newerrmsgs : bool
385 ; mutable w : int
386 ; mutable x : int
387 ; mutable y : int
388 ; mutable scrollw : int
389 ; mutable hscrollh : int
390 ; mutable anchor : anchor
391 ; mutable ranchors : (string * string * anchor) list
392 ; mutable maxy : int
393 ; mutable layout : page list
394 ; pagemap : (pagemapkey, opaque) Hashtbl.t
395 ; tilemap : (tilemapkey, tile) Hashtbl.t
396 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
397 ; mutable pdims : (pageno * width * height * leftx) list
398 ; mutable pagecount : int
399 ; mutable currently : currently
400 ; mutable mstate : mstate
401 ; mutable searchpattern : string
402 ; mutable rects : (pageno * recttype * rect) list
403 ; mutable rects1 : (pageno * recttype * rect) list
404 ; mutable text : string
405 ; mutable fullscreen : (width * height) option
406 ; mutable mode : mode
407 ; mutable uioh : uioh
408 ; mutable outlines : outline array
409 ; mutable bookmarks : outline list
410 ; mutable path : string
411 ; mutable password : string
412 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
413 ; mutable memused : memsize
414 ; mutable gen : gen
415 ; mutable throttle : (page list * int * float) option
416 ; mutable autoscroll : int option
417 ; mutable ghyll : (int option -> unit)
418 ; mutable help : helpitem array
419 ; mutable docinfo : (int * string) list
420 ; mutable texid : GlTex.texture_id option
421 ; hists : hists
422 ; mutable prevzoom : float
423 ; mutable progress : float
424 ; mutable redisplay : bool
425 ; mutable mpos : mpos
426 ; mutable keystate : keystate
428 and hists =
429 { pat : string circbuf
430 ; pag : string circbuf
431 ; nav : anchor circbuf
432 ; sel : string circbuf
436 let defconf =
437 { scrollbw = 7
438 ; scrollh = 12
439 ; icase = true
440 ; preload = true
441 ; pagebias = 0
442 ; verbose = false
443 ; debug = false
444 ; scrollstep = 24
445 ; maxhfit = true
446 ; crophack = false
447 ; autoscrollstep = 2
448 ; maxwait = None
449 ; hlinks = false
450 ; underinfo = false
451 ; interpagespace = 2
452 ; zoom = 1.0
453 ; presentation = false
454 ; angle = 0
455 ; winw = 900
456 ; winh = 900
457 ; savebmarks = true
458 ; proportional = true
459 ; trimmargins = false
460 ; trimfuzz = (0,0,0,0)
461 ; memlimit = 32 lsl 20
462 ; texcount = 256
463 ; sliceheight = 24
464 ; thumbw = 76
465 ; jumpback = true
466 ; bgcolor = (0.5, 0.5, 0.5)
467 ; bedefault = false
468 ; scrollbarinpm = true
469 ; tilew = 2048
470 ; tileh = 2048
471 ; mustoresize = 128 lsl 20
472 ; checkers = true
473 ; aalevel = 8
474 ; urilauncher =
475 (match platform with
476 | Plinux | Pfreebsd | Pdragonflybsd
477 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
478 | Posx -> "open \"%s\""
479 | Pcygwin -> "cygstart %s"
480 | Punknown -> "echo %s")
481 ; pathlauncher = "lp \"%s\""
482 ; selcmd =
483 (match platform with
484 | Plinux | Pfreebsd | Pdragonflybsd
485 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
486 | Posx -> "pbcopy"
487 | Pcygwin -> "wsel"
488 | Punknown -> "cat")
489 ; colorspace = Rgb
490 ; invert = false
491 ; colorscale = 1.0
492 ; redirectstderr = false
493 ; ghyllscroll = None
494 ; columns = None
495 ; beyecolumns = None
496 ; updatecurs = false
497 ; keyhashes =
498 let mk n = (n, Hashtbl.create 1) in
499 [ mk "global"
500 ; mk "info"
501 ; mk "help"
502 ; mk "outline"
503 ; mk "listview"
504 ; mk "birdseye"
505 ; mk "textentry"
506 ; mk "links"
511 let findkeyhash c name =
512 try List.assoc name c.keyhashes
513 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
516 let conf = { defconf with angle = defconf.angle };;
518 type fontstate =
519 { mutable fontsize : int
520 ; mutable wwidth : float
521 ; mutable maxrows : int
525 let fstate =
526 { fontsize = 14
527 ; wwidth = nan
528 ; maxrows = -1
532 let setfontsize n =
533 fstate.fontsize <- n;
534 fstate.wwidth <- measurestr fstate.fontsize "w";
535 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
538 let geturl s =
539 let colonpos = try String.index s ':' with Not_found -> -1 in
540 let len = String.length s in
541 if colonpos >= 0 && colonpos + 3 < len
542 then (
543 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
544 then
545 let schemestartpos =
546 try String.rindex_from s colonpos ' '
547 with Not_found -> -1
549 let scheme =
550 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
552 match scheme with
553 | "http" | "ftp" | "mailto" ->
554 let epos =
555 try String.index_from s colonpos ' '
556 with Not_found -> len
558 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
559 | _ -> ""
560 else ""
562 else ""
565 let popen =
566 let shell, farg = "/bin/sh", "-c" in
567 fun s ->
568 let args = [|shell; farg; s|] in
569 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
572 let gotouri uri =
573 if String.length conf.urilauncher = 0
574 then print_endline uri
575 else (
576 let url = geturl uri in
577 if String.length url = 0
578 then print_endline uri
579 else
580 let re = Str.regexp "%s" in
581 let command = Str.global_replace re url conf.urilauncher in
582 try popen command
583 with exn ->
584 Printf.eprintf
585 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
586 flush stderr;
590 let version () =
591 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
592 (platform_to_string platform) Sys.word_size Sys.ocaml_version
595 let makehelp () =
596 let strings = version () :: "" :: Help.keys in
597 Array.of_list (
598 List.map (fun s ->
599 let url = geturl s in
600 if String.length url > 0
601 then (s, 0, Action (fun u -> gotouri url; u))
602 else (s, 0, Noaction)
603 ) strings);
606 let noghyll _ = ();;
607 let firstgeomcmds = "", [];;
609 let state =
610 { sr = Unix.stdin
611 ; sw = Unix.stdin
612 ; wsfd = Unix.stdin
613 ; errfd = None
614 ; stderr = Unix.stderr
615 ; errmsgs = Buffer.create 0
616 ; newerrmsgs = false
617 ; x = 0
618 ; y = 0
619 ; w = 0
620 ; scrollw = 0
621 ; hscrollh = 0
622 ; anchor = emptyanchor
623 ; ranchors = []
624 ; layout = []
625 ; maxy = max_int
626 ; tilelru = Queue.create ()
627 ; pagemap = Hashtbl.create 10
628 ; tilemap = Hashtbl.create 10
629 ; pdims = []
630 ; pagecount = 0
631 ; currently = Idle
632 ; mstate = Mnone
633 ; rects = []
634 ; rects1 = []
635 ; text = ""
636 ; mode = View
637 ; fullscreen = None
638 ; searchpattern = ""
639 ; outlines = [||]
640 ; bookmarks = []
641 ; path = ""
642 ; password = ""
643 ; geomcmds = firstgeomcmds
644 ; hists =
645 { nav = cbnew 10 (0, 0.0)
646 ; pat = cbnew 10 ""
647 ; pag = cbnew 10 ""
648 ; sel = cbnew 10 ""
650 ; memused = 0
651 ; gen = 0
652 ; throttle = None
653 ; autoscroll = None
654 ; ghyll = noghyll
655 ; help = makehelp ()
656 ; docinfo = []
657 ; texid = None
658 ; prevzoom = 1.0
659 ; progress = -1.0
660 ; uioh = nouioh
661 ; redisplay = false
662 ; mpos = (-1, -1)
663 ; keystate = KSnone
667 let vlog fmt =
668 if conf.verbose
669 then
670 Printf.kprintf prerr_endline fmt
671 else
672 Printf.kprintf ignore fmt
675 let launchpath () =
676 if String.length conf.pathlauncher = 0
677 then print_endline state.path
678 else (
679 let re = Str.regexp "%s" in
680 let command = Str.global_replace re state.path conf.pathlauncher in
681 try popen command
682 with exn ->
683 Printf.eprintf
684 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
685 flush stderr;
689 let redirectstderr () =
690 if conf.redirectstderr
691 then
692 let rfd, wfd = Unix.pipe () in
693 state.stderr <- Unix.dup Unix.stderr;
694 state.errfd <- Some rfd;
695 Unix.dup2 wfd Unix.stderr;
696 else (
697 state.newerrmsgs <- false;
698 begin match state.errfd with
699 | Some fd ->
700 Unix.close fd;
701 Unix.dup2 state.stderr Unix.stderr;
702 state.errfd <- None;
703 | None -> ()
704 end;
705 prerr_string (Buffer.contents state.errmsgs);
706 flush stderr;
707 Buffer.clear state.errmsgs;
711 module G =
712 struct
713 let postRedisplay who =
714 if conf.verbose
715 then prerr_endline ("redisplay for " ^ who);
716 state.redisplay <- true;
718 end;;
720 let getopaque pageno =
721 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
722 with Not_found -> None
725 let putopaque pageno opaque =
726 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
729 let pagetranslatepoint l x y =
730 let dy = y - l.pagedispy in
731 let y = dy + l.pagey in
732 let dx = x - l.pagedispx in
733 let x = dx + l.pagex in
734 (x, y);
737 let getunder x y =
738 let rec f = function
739 | l :: rest ->
740 begin match getopaque l.pageno with
741 | Some opaque ->
742 let x0 = l.pagedispx in
743 let x1 = x0 + l.pagevw in
744 let y0 = l.pagedispy in
745 let y1 = y0 + l.pagevh in
746 if y >= y0 && y <= y1 && x >= x0 && x <= x1
747 then
748 let px, py = pagetranslatepoint l x y in
749 match whatsunder opaque px py with
750 | Unone -> f rest
751 | under -> under
752 else f rest
753 | _ ->
754 f rest
756 | [] -> Unone
758 f state.layout
761 let showtext c s =
762 state.text <- Printf.sprintf "%c%s" c s;
763 G.postRedisplay "showtext";
766 let updateunder x y =
767 match getunder x y with
768 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
769 | Ulinkuri uri ->
770 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
771 Wsi.setcursor Wsi.CURSOR_INFO
772 | Ulinkgoto (page, _) ->
773 if conf.underinfo
774 then showtext 'p' ("age: " ^ string_of_int (page+1));
775 Wsi.setcursor Wsi.CURSOR_INFO
776 | Utext s ->
777 if conf.underinfo then showtext 'f' ("ont: " ^ s);
778 Wsi.setcursor Wsi.CURSOR_TEXT
779 | Uunexpected s ->
780 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
781 Wsi.setcursor Wsi.CURSOR_INHERIT
782 | Ulaunch s ->
783 if conf.underinfo then showtext 'l' ("launch: " ^ s);
784 Wsi.setcursor Wsi.CURSOR_INHERIT
785 | Unamed s ->
786 if conf.underinfo then showtext 'n' ("named: " ^ s);
787 Wsi.setcursor Wsi.CURSOR_INHERIT
788 | Uremote (filename, pageno) ->
789 if conf.underinfo then showtext 'r'
790 (Printf.sprintf "emote: %s (%d)" filename pageno);
791 Wsi.setcursor Wsi.CURSOR_INFO
794 let addchar s c =
795 let b = Buffer.create (String.length s + 1) in
796 Buffer.add_string b s;
797 Buffer.add_char b c;
798 Buffer.contents b;
801 let colorspace_of_string s =
802 match String.lowercase s with
803 | "rgb" -> Rgb
804 | "bgr" -> Bgr
805 | "gray" -> Gray
806 | _ -> failwith "invalid colorspace"
809 let int_of_colorspace = function
810 | Rgb -> 0
811 | Bgr -> 1
812 | Gray -> 2
815 let colorspace_of_int = function
816 | 0 -> Rgb
817 | 1 -> Bgr
818 | 2 -> Gray
819 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
822 let colorspace_to_string = function
823 | Rgb -> "rgb"
824 | Bgr -> "bgr"
825 | Gray -> "gray"
828 let intentry_with_suffix text key =
829 let c =
830 if key >= 32 && key < 127
831 then Char.chr key
832 else '\000'
834 match Char.lowercase c with
835 | '0' .. '9' ->
836 let text = addchar text c in
837 TEcont text
839 | 'k' | 'm' | 'g' ->
840 let text = addchar text c in
841 TEcont text
843 | _ ->
844 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
845 TEcont text
848 let columns_to_string (n, a, b) =
849 if a = 0 && b = 0
850 then Printf.sprintf "%d" n
851 else Printf.sprintf "%d,%d,%d" n a b;
854 let columns_of_string s =
856 (int_of_string s, 0, 0)
857 with _ ->
858 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
861 let readcmd fd =
862 let s = "xxxx" in
863 let n = Unix.read fd s 0 4 in
864 if n != 4 then failwith "incomplete read(len)";
865 let len = 0
866 lor (Char.code s.[0] lsl 24)
867 lor (Char.code s.[1] lsl 16)
868 lor (Char.code s.[2] lsl 8)
869 lor (Char.code s.[3] lsl 0)
871 let s = String.create len in
872 let n = Unix.read fd s 0 len in
873 if n != len then failwith "incomplete read(data)";
877 let btod b = if b then 1 else 0;;
879 let wcmd fmt =
880 let b = Buffer.create 16 in
881 Buffer.add_string b "llll";
882 Printf.kbprintf
883 (fun b ->
884 let s = Buffer.contents b in
885 let n = String.length s in
886 let len = n - 4 in
887 (* dolog "wcmd %S" (String.sub s 4 len); *)
888 s.[0] <- Char.chr ((len lsr 24) land 0xff);
889 s.[1] <- Char.chr ((len lsr 16) land 0xff);
890 s.[2] <- Char.chr ((len lsr 8) land 0xff);
891 s.[3] <- Char.chr (len land 0xff);
892 let n' = Unix.write state.sw s 0 n in
893 if n' != n then failwith "write failed";
894 ) b fmt;
897 let calcips h =
898 if conf.presentation
899 then
900 let d = conf.winh - h in
901 max 0 ((d + 1) / 2)
902 else
903 conf.interpagespace
906 let calcheight () =
907 let rec f pn ph pi fh l =
908 match l with
909 | (n, _, h, _) :: rest ->
910 let ips = calcips h in
911 let fh =
912 if conf.presentation
913 then fh+ips
914 else (
915 if isbirdseye state.mode && pn = 0
916 then fh + ips
917 else fh
920 let fh = fh + ((n - pn) * (ph + pi)) in
921 f n h ips fh rest;
923 | [] ->
924 let inc =
925 if conf.presentation || (isbirdseye state.mode && pn = 0)
926 then 0
927 else -pi
929 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
930 max 0 fh
932 let fh = f 0 0 0 0 state.pdims in
936 let calcheight () =
937 match conf.columns with
938 | None -> calcheight ()
939 | Some (_, b) ->
940 if Array.length b > 0
941 then
942 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
943 y + h
944 else 0
947 let getpageyh pageno =
948 let rec f pn ph pi y l =
949 match l with
950 | (n, _, h, _) :: rest ->
951 let ips = calcips h in
952 if n >= pageno
953 then
954 let h = if n = pageno then h else ph in
955 if conf.presentation && n = pageno
956 then
957 y + (pageno - pn) * (ph + pi) + pi, h
958 else
959 y + (pageno - pn) * (ph + pi), h
960 else
961 let y = y + (if conf.presentation then pi else 0) in
962 let y = y + (n - pn) * (ph + pi) in
963 f n h ips y rest
965 | [] ->
966 y + (pageno - pn) * (ph + pi), ph
968 f 0 0 0 0 state.pdims
971 let getpageyh pageno =
972 match conf.columns with
973 | None -> getpageyh pageno
974 | Some (_, b) ->
975 let (_, _, y, (_, _, h, _)) = b.(pageno) in
976 y, h
979 let getpagedim pageno =
980 let rec f ppdim l =
981 match l with
982 | (n, _, _, _) as pdim :: rest ->
983 if n >= pageno
984 then (if n = pageno then pdim else ppdim)
985 else f pdim rest
987 | [] -> ppdim
989 f (-1, -1, -1, -1) state.pdims
992 let getpagey pageno = fst (getpageyh pageno);;
994 let nogeomcmds cmds =
995 match cmds with
996 | s, [] -> String.length s = 0
997 | _ -> false
1000 let layout1 y sh =
1001 let sh = sh - state.hscrollh in
1002 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1003 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1004 match pdims with
1005 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1006 let ips = calcips h in
1007 let yinc =
1008 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1009 then ips
1010 else 0
1012 (w, h, ips, xoff), rest, pdimno + 1, yinc
1013 | _ ->
1014 prev, pdims, pdimno, 0
1016 let dy = dy + yinc in
1017 let py = py + yinc in
1018 if pageno = state.pagecount || dy >= sh
1019 then
1020 accu
1021 else
1022 let vy = y + dy in
1023 if py + h <= vy - yinc
1024 then
1025 let py = py + h + ips in
1026 let dy = max 0 (py - y) in
1027 f ~pageno:(pageno+1)
1028 ~pdimno
1029 ~prev:curr
1032 ~pdims:rest
1033 ~accu
1034 else
1035 let pagey = vy - py in
1036 let pagevh = h - pagey in
1037 let pagevh = min (sh - dy) pagevh in
1038 let off = if yinc > 0 then py - vy else 0 in
1039 let py = py + h + ips in
1040 let pagex, dx =
1041 let xoff = xoff +
1042 if state.w < conf.winw - state.scrollw
1043 then (conf.winw - state.scrollw - state.w) / 2
1044 else 0
1046 let dispx = xoff + state.x in
1047 if dispx < 0
1048 then (-dispx, 0)
1049 else (0, dispx)
1051 let pagevw =
1052 let lw = w - pagex in
1053 min lw (conf.winw - state.scrollw)
1055 let e =
1056 { pageno = pageno
1057 ; pagedimno = pdimno
1058 ; pagew = w
1059 ; pageh = h
1060 ; pagex = pagex
1061 ; pagey = pagey + off
1062 ; pagevw = pagevw
1063 ; pagevh = pagevh - off
1064 ; pagedispx = dx
1065 ; pagedispy = dy + off
1068 let accu = e :: accu in
1069 f ~pageno:(pageno+1)
1070 ~pdimno
1071 ~prev:curr
1073 ~dy:(dy+pagevh+ips)
1074 ~pdims:rest
1075 ~accu
1077 if nogeomcmds state.geomcmds
1078 then (
1079 let accu =
1081 ~pageno:0
1082 ~pdimno:~-1
1083 ~prev:(0,0,0,0)
1084 ~py:0
1085 ~dy:0
1086 ~pdims:state.pdims
1087 ~accu:[]
1089 List.rev accu
1091 else
1095 let layoutN ((columns, coverA, coverB), b) y sh =
1096 let sh = sh - state.hscrollh in
1097 let rec fold accu n =
1098 if n = Array.length b
1099 then accu
1100 else
1101 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1102 if (vy - y) > sh &&
1103 (n = coverA - 1
1104 || n = state.pagecount - coverB
1105 || (n - coverA) mod columns = columns - 1)
1106 then accu
1107 else
1108 let accu =
1109 if vy + h > y
1110 then
1111 let pagey = max 0 (y - vy) in
1112 let pagedispy = if pagey > 0 then 0 else vy - y in
1113 let pagedispx, pagex, pagevw =
1114 let pdx =
1115 if n = coverA - 1 || n = state.pagecount - coverB
1116 then state.x + (conf.winw - state.scrollw - w) / 2
1117 else dx + xoff + state.x
1119 if pdx < 0
1120 then 0, -pdx, w + pdx
1121 else pdx, 0, min (conf.winw - state.scrollw) w
1123 let pagevh = min (h - pagey) (sh - pagedispy) in
1124 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1125 then
1126 let e =
1127 { pageno = n
1128 ; pagedimno = pdimno
1129 ; pagew = w
1130 ; pageh = h
1131 ; pagex = pagex
1132 ; pagey = pagey
1133 ; pagevw = pagevw
1134 ; pagevh = pagevh
1135 ; pagedispx = pagedispx
1136 ; pagedispy = pagedispy
1139 e :: accu
1140 else
1141 accu
1142 else
1143 accu
1145 fold accu (n+1)
1147 if nogeomcmds state.geomcmds
1148 then List.rev (fold [] 0)
1149 else []
1152 let layout y sh =
1153 match conf.columns with
1154 | None -> layout1 y sh
1155 | Some c -> layoutN c y sh
1158 let clamp incr =
1159 let y = state.y + incr in
1160 let y = max 0 y in
1161 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1165 let itertiles l f =
1166 let tilex = l.pagex mod conf.tilew in
1167 let tiley = l.pagey mod conf.tileh in
1169 let col = l.pagex / conf.tilew in
1170 let row = l.pagey / conf.tileh in
1172 let vw =
1173 let a = l.pagew - l.pagex in
1174 let b = conf.winw - state.scrollw in
1175 min a b
1176 and vh = l.pagevh in
1178 let rec rowloop row y0 dispy h =
1179 if h = 0
1180 then ()
1181 else (
1182 let dh = conf.tileh - y0 in
1183 let dh = min h dh in
1184 let rec colloop col x0 dispx w =
1185 if w = 0
1186 then ()
1187 else (
1188 let dw = conf.tilew - x0 in
1189 let dw = min w dw in
1191 f col row dispx dispy x0 y0 dw dh;
1192 colloop (col+1) 0 (dispx+dw) (w-dw)
1195 colloop col tilex l.pagedispx vw;
1196 rowloop (row+1) 0 (dispy+dh) (h-dh)
1199 if vw > 0 && vh > 0
1200 then rowloop row tiley l.pagedispy vh;
1203 let gettileopaque l col row =
1204 let key =
1205 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1207 try Some (Hashtbl.find state.tilemap key)
1208 with Not_found -> None
1211 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1212 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1213 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1216 let drawtiles l color =
1217 GlDraw.color color;
1218 let f col row x y tilex tiley w h =
1219 match gettileopaque l col row with
1220 | Some (opaque, _, t) ->
1221 let params = x, y, w, h, tilex, tiley in
1222 if conf.invert
1223 then (
1224 Gl.enable `blend;
1225 GlFunc.blend_func `zero `one_minus_src_color;
1227 drawtile params opaque;
1228 if conf.invert
1229 then Gl.disable `blend;
1230 if conf.debug
1231 then (
1232 let s = Printf.sprintf
1233 "%d[%d,%d] %f sec"
1234 l.pageno col row t
1236 let w = measurestr fstate.fontsize s in
1237 GlMisc.push_attrib [`current];
1238 GlDraw.color (0.0, 0.0, 0.0);
1239 GlDraw.rect
1240 (float (x-2), float (y-2))
1241 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1242 GlDraw.color (1.0, 1.0, 1.0);
1243 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1244 GlMisc.pop_attrib ();
1247 | _ ->
1248 let w =
1249 let lw = conf.winw - state.scrollw - x in
1250 min lw w
1251 and h =
1252 let lh = conf.winh - y in
1253 min lh h
1255 Gl.enable `texture_2d;
1256 begin match state.texid with
1257 | Some id ->
1258 GlTex.bind_texture `texture_2d id;
1259 let x0 = float x
1260 and y0 = float y
1261 and x1 = float (x+w)
1262 and y1 = float (y+h) in
1264 let tw = float w /. 64.0
1265 and th = float h /. 64.0 in
1266 let tx0 = float tilex /. 64.0
1267 and ty0 = float tiley /. 64.0 in
1268 let tx1 = tx0 +. tw
1269 and ty1 = ty0 +. th in
1270 GlDraw.begins `quads;
1271 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1272 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1273 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1274 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1275 GlDraw.ends ();
1277 Gl.disable `texture_2d;
1278 | None ->
1279 GlDraw.color (1.0, 1.0, 1.0);
1280 GlDraw.rect
1281 (float x, float y)
1282 (float (x+w), float (y+h));
1283 end;
1284 if w > 128 && h > fstate.fontsize + 10
1285 then (
1286 GlDraw.color (0.0, 0.0, 0.0);
1287 let c, r =
1288 if conf.verbose
1289 then (col*conf.tilew, row*conf.tileh)
1290 else col, row
1292 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1294 GlDraw.color color;
1296 itertiles l f
1299 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1301 let tilevisible1 l x y =
1302 let ax0 = l.pagex
1303 and ax1 = l.pagex + l.pagevw
1304 and ay0 = l.pagey
1305 and ay1 = l.pagey + l.pagevh in
1307 let bx0 = x
1308 and by0 = y in
1309 let bx1 = min (bx0 + conf.tilew) l.pagew
1310 and by1 = min (by0 + conf.tileh) l.pageh in
1312 let rx0 = max ax0 bx0
1313 and ry0 = max ay0 by0
1314 and rx1 = min ax1 bx1
1315 and ry1 = min ay1 by1 in
1317 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1318 nonemptyintersection
1321 let tilevisible layout n x y =
1322 let rec findpageinlayout = function
1323 | l :: _ when l.pageno = n -> tilevisible1 l x y
1324 | _ :: rest -> findpageinlayout rest
1325 | [] -> false
1327 findpageinlayout layout
1330 let tileready l x y =
1331 tilevisible1 l x y &&
1332 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1335 let tilepage n p layout =
1336 let rec loop = function
1337 | l :: rest ->
1338 if l.pageno = n
1339 then
1340 let f col row _ _ _ _ _ _ =
1341 if state.currently = Idle
1342 then
1343 match gettileopaque l col row with
1344 | Some _ -> ()
1345 | None ->
1346 let x = col*conf.tilew
1347 and y = row*conf.tileh in
1348 let w =
1349 let w = l.pagew - x in
1350 min w conf.tilew
1352 let h =
1353 let h = l.pageh - y in
1354 min h conf.tileh
1356 wcmd "tile %s %d %d %d %d" p x y w h;
1357 state.currently <-
1358 Tiling (
1359 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1360 conf.tilew, conf.tileh
1363 itertiles l f;
1364 else
1365 loop rest
1367 | [] -> ()
1369 if nogeomcmds state.geomcmds
1370 then loop layout;
1373 let preloadlayout visiblepages =
1374 let presentation = conf.presentation in
1375 let interpagespace = conf.interpagespace in
1376 let maxy = state.maxy in
1377 conf.presentation <- false;
1378 conf.interpagespace <- 0;
1379 state.maxy <- calcheight ();
1380 let y =
1381 match visiblepages with
1382 | [] -> 0
1383 | l :: _ -> getpagey l.pageno + l.pagey
1385 let y = if y < conf.winh then 0 else y - conf.winh in
1386 let h = state.y - y + conf.winh*3 in
1387 let pages = layout y h in
1388 conf.presentation <- presentation;
1389 conf.interpagespace <- interpagespace;
1390 state.maxy <- maxy;
1391 pages;
1394 let load pages =
1395 let rec loop pages =
1396 if state.currently != Idle
1397 then ()
1398 else
1399 match pages with
1400 | l :: rest ->
1401 begin match getopaque l.pageno with
1402 | None ->
1403 wcmd "page %d %d" l.pageno l.pagedimno;
1404 state.currently <- Loading (l, state.gen);
1405 | Some opaque ->
1406 tilepage l.pageno opaque pages;
1407 loop rest
1408 end;
1409 | _ -> ()
1411 if nogeomcmds state.geomcmds
1412 then loop pages
1415 let preload pages =
1416 load pages;
1417 if conf.preload && state.currently = Idle
1418 then load (preloadlayout pages);
1421 let layoutready layout =
1422 let rec fold all ls =
1423 all && match ls with
1424 | l :: rest ->
1425 let seen = ref false in
1426 let allvisible = ref true in
1427 let foo col row _ _ _ _ _ _ =
1428 seen := true;
1429 allvisible := !allvisible &&
1430 begin match gettileopaque l col row with
1431 | Some _ -> true
1432 | None -> false
1435 itertiles l foo;
1436 fold (!seen && !allvisible) rest
1437 | [] -> true
1439 let alltilesvisible = fold true layout in
1440 alltilesvisible;
1443 let gotoy y =
1444 let y = bound y 0 state.maxy in
1445 let y, layout, proceed =
1446 match conf.maxwait with
1447 | Some time when state.ghyll == noghyll ->
1448 begin match state.throttle with
1449 | None ->
1450 let layout = layout y conf.winh in
1451 let ready = layoutready layout in
1452 if not ready
1453 then (
1454 load layout;
1455 state.throttle <- Some (layout, y, now ());
1457 else G.postRedisplay "gotoy showall (None)";
1458 y, layout, ready
1459 | Some (_, _, started) ->
1460 let dt = now () -. started in
1461 if dt > time
1462 then (
1463 state.throttle <- None;
1464 let layout = layout y conf.winh in
1465 load layout;
1466 G.postRedisplay "maxwait";
1467 y, layout, true
1469 else -1, [], false
1472 | _ ->
1473 let layout = layout y conf.winh in
1474 if true || layoutready layout
1475 then G.postRedisplay "gotoy ready";
1476 y, layout, true
1478 if proceed
1479 then (
1480 state.y <- y;
1481 state.layout <- layout;
1482 begin match state.mode with
1483 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1484 if not (pagevisible layout pageno)
1485 then (
1486 match state.layout with
1487 | [] -> ()
1488 | l :: _ ->
1489 state.mode <- Birdseye (
1490 conf, leftx, l.pageno, hooverpageno, anchor
1493 | LinkNav (Ltgendir dir as lt) ->
1494 let linknav =
1495 let rec loop = function
1496 | [] -> lt
1497 | l :: rest ->
1498 match getopaque l.pageno with
1499 | None -> loop rest
1500 | Some opaque ->
1501 let link =
1502 let ld =
1503 if dir = 0
1504 then LDfirstvisible (l.pagex, l.pagey, dir)
1505 else (
1506 if dir > 0 then LDfirst else LDlast
1509 findlink opaque ld
1511 match link with
1512 | Lnotfound -> loop rest
1513 | Lfound (n, x0, y0, x1, y1) ->
1514 Ltexact ((l.pageno, n), (x0, y0, x1, y1))
1516 loop state.layout
1518 state.mode <- LinkNav linknav
1519 | _ -> ()
1520 end;
1521 preload layout;
1523 state.ghyll <- noghyll;
1526 let conttiling pageno opaque =
1527 tilepage pageno opaque
1528 (if conf.preload then preloadlayout state.layout else state.layout)
1531 let gotoy_and_clear_text y =
1532 gotoy y;
1533 if not conf.verbose then state.text <- "";
1536 let getanchor () =
1537 match state.layout with
1538 | [] -> emptyanchor
1539 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1542 let getanchory (n, top) =
1543 let y, h = getpageyh n in
1544 y + (truncate (top *. float h));
1547 let gotoanchor anchor =
1548 gotoy (getanchory anchor);
1551 let addnav () =
1552 cbput state.hists.nav (getanchor ());
1555 let getnav dir =
1556 let anchor = cbgetc state.hists.nav dir in
1557 getanchory anchor;
1560 let gotoghyll y =
1561 let rec scroll f n a b =
1562 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1563 let snake f a b =
1564 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1565 if f < a
1566 then s (float f /. float a)
1567 else (
1568 if f > b
1569 then 1.0 -. s ((float (f-b) /. float (n-b)))
1570 else 1.0
1573 snake f a b
1574 and summa f n a b =
1575 (* courtesy:
1576 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1577 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1578 let iv1 = iv f in
1579 let ins = float a *. iv1
1580 and outs = float (n-b) *. iv1 in
1581 let ones = b - a in
1582 ins +. outs +. float ones
1584 let rec set (_N, _A, _B) y sy =
1585 let sum = summa 1.0 _N _A _B in
1586 let dy = float (y - sy) in
1587 state.ghyll <- (
1588 let rec gf n y1 o =
1589 if n >= _N
1590 then state.ghyll <- noghyll
1591 else
1592 let go n =
1593 let s = scroll n _N _A _B in
1594 let y1 = y1 +. ((s *. dy) /. sum) in
1595 gotoy_and_clear_text (truncate y1);
1596 state.ghyll <- gf (n+1) y1;
1598 match o with
1599 | None -> go n
1600 | Some y' -> set (_N/2, 0, 0) y' state.y
1602 gf 0 (float state.y)
1605 match conf.ghyllscroll with
1606 | None ->
1607 gotoy_and_clear_text y
1608 | Some nab ->
1609 if state.ghyll == noghyll
1610 then set nab y state.y
1611 else state.ghyll (Some y)
1614 let gotopage n top =
1615 let y, h = getpageyh n in
1616 let y = y + (truncate (top *. float h)) in
1617 gotoghyll y
1620 let gotopage1 n top =
1621 let y = getpagey n in
1622 let y = y + top in
1623 gotoghyll y
1626 let invalidate s f =
1627 state.layout <- [];
1628 state.pdims <- [];
1629 state.rects <- [];
1630 state.rects1 <- [];
1631 match state.geomcmds with
1632 | ps, [] when String.length ps = 0 ->
1633 f ();
1634 state.geomcmds <- s, [];
1636 | ps, [] ->
1637 state.geomcmds <- ps, [s, f];
1639 | ps, (s', _) :: rest when s' = s ->
1640 state.geomcmds <- ps, ((s, f) :: rest);
1642 | ps, cmds ->
1643 state.geomcmds <- ps, ((s, f) :: cmds);
1646 let opendoc path password =
1647 state.path <- path;
1648 state.password <- password;
1649 state.gen <- state.gen + 1;
1650 state.docinfo <- [];
1652 setaalevel conf.aalevel;
1653 Wsi.settitle ("llpp " ^ Filename.basename path);
1654 wcmd "open %s\000%s\000" path password;
1655 invalidate "reqlayout"
1656 (fun () ->
1657 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1660 let scalecolor c =
1661 let c = c *. conf.colorscale in
1662 (c, c, c);
1665 let scalecolor2 (r, g, b) =
1666 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1669 let represent () =
1670 let docolumns = function
1671 | None -> ()
1672 | Some ((columns, coverA, coverB), _) ->
1673 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1674 let rec loop pageno pdimno pdim x y rowh pdims =
1675 if pageno = state.pagecount
1676 then ()
1677 else
1678 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1679 match pdims with
1680 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1681 pdimno+1, pdim, rest
1682 | _ ->
1683 pdimno, pdim, pdims
1685 let x, y, rowh' =
1686 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1687 then (
1688 (conf.winw - state.scrollw - w) / 2,
1689 y + rowh + conf.interpagespace, h
1691 else (
1692 if (pageno - coverA) mod columns = 0
1693 then 0, y + rowh + conf.interpagespace, h
1694 else x, y, max rowh h
1697 let rec fixrow m = if m = pageno then () else
1698 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1699 if h < rowh
1700 then (
1701 let y = y + (rowh - h) / 2 in
1702 a.(m) <- (pdimno, x, y, pdim);
1704 fixrow (m+1)
1706 if pageno > 1 && (pageno - coverA) mod columns = 0
1707 then fixrow (pageno - columns);
1708 a.(pageno) <- (pdimno, x, y, pdim);
1709 let x = x + w + xoff*2 + conf.interpagespace in
1710 loop (pageno+1) pdimno pdim x y rowh' pdims
1712 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1713 conf.columns <- Some ((columns, coverA, coverB), a);
1715 docolumns conf.columns;
1716 state.maxy <- calcheight ();
1717 state.hscrollh <-
1718 if state.w <= conf.winw - state.scrollw
1719 then 0
1720 else state.scrollw
1722 match state.mode with
1723 | Birdseye (_, _, pageno, _, _) ->
1724 let y, h = getpageyh pageno in
1725 let top = (conf.winh - h) / 2 in
1726 gotoy (max 0 (y - top))
1727 | _ -> gotoanchor state.anchor
1730 let reshape w h =
1731 GlDraw.viewport 0 0 w h;
1732 if state.geomcmds != firstgeomcmds && nogeomcmds state.geomcmds
1733 then state.anchor <- getanchor ();
1735 conf.winw <- w;
1736 let w = truncate (float w *. conf.zoom) - state.scrollw in
1737 let w = max w 2 in
1738 conf.winh <- h;
1739 setfontsize fstate.fontsize;
1740 GlMat.mode `modelview;
1741 GlMat.load_identity ();
1743 GlMat.mode `projection;
1744 GlMat.load_identity ();
1745 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1746 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1747 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1749 invalidate "geometry"
1750 (fun () ->
1751 state.w <- w;
1752 let w =
1753 match conf.columns with
1754 | None -> w
1755 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1757 wcmd "geometry %d %d" w h);
1760 let enttext () =
1761 let len = String.length state.text in
1762 let drawstring s =
1763 let hscrollh =
1764 match state.mode with
1765 | View -> state.hscrollh
1766 | _ -> 0
1768 let rect x w =
1769 GlDraw.rect
1770 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1771 (x+.w, float (conf.winh - hscrollh))
1774 let w = float (conf.winw - state.scrollw - 1) in
1775 if state.progress >= 0.0 && state.progress < 1.0
1776 then (
1777 GlDraw.color (0.3, 0.3, 0.3);
1778 let w1 = w *. state.progress in
1779 rect 0.0 w1;
1780 GlDraw.color (0.0, 0.0, 0.0);
1781 rect w1 (w-.w1)
1783 else (
1784 GlDraw.color (0.0, 0.0, 0.0);
1785 rect 0.0 w;
1788 GlDraw.color (1.0, 1.0, 1.0);
1789 drawstring fstate.fontsize
1790 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1792 let s =
1793 match state.mode with
1794 | Textentry ((prefix, text, _, _, _), _) ->
1795 let s =
1796 if len > 0
1797 then
1798 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1799 else
1800 Printf.sprintf "%s%s_" prefix text
1804 | _ -> state.text
1806 let s =
1807 if state.newerrmsgs
1808 then (
1809 if not (istextentry state.mode)
1810 then
1811 let s1 = "(press 'e' to review error messasges)" in
1812 if String.length s > 0 then s ^ " " ^ s1 else s1
1813 else s
1815 else s
1817 if String.length s > 0
1818 then drawstring s
1821 let gctiles () =
1822 let len = Queue.length state.tilelru in
1823 let rec loop qpos =
1824 if state.memused <= conf.memlimit
1825 then ()
1826 else (
1827 if qpos < len
1828 then
1829 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1830 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1831 let (_, pw, ph, _) = getpagedim n in
1833 gen = state.gen
1834 && colorspace = conf.colorspace
1835 && angle = conf.angle
1836 && pagew = pw
1837 && pageh = ph
1838 && (
1839 let layout =
1840 match state.throttle with
1841 | None ->
1842 if conf.preload
1843 then preloadlayout state.layout
1844 else state.layout
1845 | Some (layout, _, _) ->
1846 layout
1848 let x = col*conf.tilew
1849 and y = row*conf.tileh in
1850 tilevisible layout n x y
1852 then Queue.push lruitem state.tilelru
1853 else (
1854 wcmd "freetile %s" p;
1855 state.memused <- state.memused - s;
1856 state.uioh#infochanged Memused;
1857 Hashtbl.remove state.tilemap k;
1859 loop (qpos+1)
1862 loop 0
1865 let flushtiles () =
1866 Queue.iter (fun (k, p, s) ->
1867 wcmd "freetile %s" p;
1868 state.memused <- state.memused - s;
1869 state.uioh#infochanged Memused;
1870 Hashtbl.remove state.tilemap k;
1871 ) state.tilelru;
1872 Queue.clear state.tilelru;
1873 load state.layout;
1876 let logcurrently = function
1877 | Idle -> dolog "Idle"
1878 | Loading (l, gen) ->
1879 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1880 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1881 dolog
1882 "Tiling %d[%d,%d] page=%s cs=%s angle"
1883 l.pageno col row pageopaque
1884 (colorspace_to_string colorspace)
1886 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1887 angle gen conf.angle state.gen
1888 tilew tileh
1889 conf.tilew conf.tileh
1891 | Outlining _ ->
1892 dolog "outlining"
1895 let act cmds =
1896 (* dolog "%S" cmds; *)
1897 let op, args =
1898 let spacepos =
1899 try String.index cmds ' '
1900 with Not_found -> -1
1902 if spacepos = -1
1903 then cmds, ""
1904 else
1905 let l = String.length cmds in
1906 let op = String.sub cmds 0 spacepos in
1907 op, begin
1908 if l - spacepos < 2 then ""
1909 else String.sub cmds (spacepos+1) (l-spacepos-1)
1912 match op with
1913 | "clear" ->
1914 state.uioh#infochanged Pdim;
1915 state.pdims <- [];
1917 | "clearrects" ->
1918 state.rects <- state.rects1;
1919 G.postRedisplay "clearrects";
1921 | "continue" ->
1922 let n =
1923 try Scanf.sscanf args "%u" (fun n -> n)
1924 with exn ->
1925 dolog "error processing 'continue' %S: %s"
1926 cmds (Printexc.to_string exn);
1927 exit 1;
1929 state.pagecount <- n;
1930 begin match state.currently with
1931 | Outlining l ->
1932 state.currently <- Idle;
1933 state.outlines <- Array.of_list (List.rev l)
1934 | _ -> ()
1935 end;
1937 let cur, cmds = state.geomcmds in
1938 if String.length cur = 0
1939 then failwith "umpossible";
1941 begin match List.rev cmds with
1942 | [] ->
1943 state.geomcmds <- "", [];
1944 represent ();
1945 | (s, f) :: rest ->
1946 f ();
1947 state.geomcmds <- s, List.rev rest;
1948 end;
1949 if conf.maxwait = None
1950 then G.postRedisplay "continue";
1952 | "title" ->
1953 Wsi.settitle args
1955 | "msg" ->
1956 showtext ' ' args
1958 | "vmsg" ->
1959 if conf.verbose
1960 then showtext ' ' args
1962 | "progress" ->
1963 let progress, text =
1965 Scanf.sscanf args "%f %n"
1966 (fun f pos ->
1967 f, String.sub args pos (String.length args - pos))
1968 with exn ->
1969 dolog "error processing 'progress' %S: %s"
1970 cmds (Printexc.to_string exn);
1971 exit 1;
1973 state.text <- text;
1974 state.progress <- progress;
1975 G.postRedisplay "progress"
1977 | "firstmatch" ->
1978 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1980 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1981 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1982 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1983 with exn ->
1984 dolog "error processing 'firstmatch' %S: %s"
1985 cmds (Printexc.to_string exn);
1986 exit 1;
1988 let y = (getpagey pageno) + truncate y0 in
1989 addnav ();
1990 gotoy y;
1991 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1993 | "match" ->
1994 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1996 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1997 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1998 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1999 with exn ->
2000 dolog "error processing 'match' %S: %s"
2001 cmds (Printexc.to_string exn);
2002 exit 1;
2004 state.rects1 <-
2005 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2007 | "page" ->
2008 let pageopaque, t =
2010 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2011 with exn ->
2012 dolog "error processing 'page' %S: %s"
2013 cmds (Printexc.to_string exn);
2014 exit 1;
2016 begin match state.currently with
2017 | Loading (l, gen) ->
2018 vlog "page %d took %f sec" l.pageno t;
2019 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2020 begin match state.throttle with
2021 | None ->
2022 let preloadedpages =
2023 if conf.preload
2024 then preloadlayout state.layout
2025 else state.layout
2027 let evict () =
2028 let module IntSet =
2029 Set.Make (struct type t = int let compare = (-) end) in
2030 let set =
2031 List.fold_left (fun s l -> IntSet.add l.pageno s)
2032 IntSet.empty preloadedpages
2034 let evictedpages =
2035 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2036 if not (IntSet.mem pageno set)
2037 then (
2038 wcmd "freepage %s" opaque;
2039 key :: accu
2041 else accu
2042 ) state.pagemap []
2044 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2046 evict ();
2047 state.currently <- Idle;
2048 if gen = state.gen
2049 then (
2050 tilepage l.pageno pageopaque state.layout;
2051 load state.layout;
2052 load preloadedpages;
2053 if pagevisible state.layout l.pageno
2054 && layoutready state.layout
2055 then G.postRedisplay "page";
2058 | Some (layout, _, _) ->
2059 state.currently <- Idle;
2060 tilepage l.pageno pageopaque layout;
2061 load state.layout
2062 end;
2064 | _ ->
2065 dolog "Inconsistent loading state";
2066 logcurrently state.currently;
2067 exit 1
2070 | "tile" ->
2071 let (x, y, opaque, size, t) =
2073 Scanf.sscanf args "%u %u %s %u %f"
2074 (fun x y p size t -> (x, y, p, size, t))
2075 with exn ->
2076 dolog "error processing 'tile' %S: %s"
2077 cmds (Printexc.to_string exn);
2078 exit 1;
2080 begin match state.currently with
2081 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2082 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2084 if tilew != conf.tilew || tileh != conf.tileh
2085 then (
2086 wcmd "freetile %s" opaque;
2087 state.currently <- Idle;
2088 load state.layout;
2090 else (
2091 puttileopaque l col row gen cs angle opaque size t;
2092 state.memused <- state.memused + size;
2093 state.uioh#infochanged Memused;
2094 gctiles ();
2095 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2096 opaque, size) state.tilelru;
2098 let layout =
2099 match state.throttle with
2100 | None -> state.layout
2101 | Some (layout, _, _) -> layout
2104 state.currently <- Idle;
2105 if gen = state.gen
2106 && conf.colorspace = cs
2107 && conf.angle = angle
2108 && tilevisible layout l.pageno x y
2109 then conttiling l.pageno pageopaque;
2111 begin match state.throttle with
2112 | None ->
2113 preload state.layout;
2114 if gen = state.gen
2115 && conf.colorspace = cs
2116 && conf.angle = angle
2117 && tilevisible state.layout l.pageno x y
2118 then G.postRedisplay "tile nothrottle";
2120 | Some (layout, y, _) ->
2121 let ready = layoutready layout in
2122 if ready
2123 then (
2124 state.y <- y;
2125 state.layout <- layout;
2126 state.throttle <- None;
2127 G.postRedisplay "throttle";
2129 else load layout;
2130 end;
2133 | _ ->
2134 dolog "Inconsistent tiling state";
2135 logcurrently state.currently;
2136 exit 1
2139 | "pdim" ->
2140 let pdim =
2142 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2143 with exn ->
2144 dolog "error processing 'pdim' %S: %s"
2145 cmds (Printexc.to_string exn);
2146 exit 1;
2148 state.uioh#infochanged Pdim;
2149 state.pdims <- pdim :: state.pdims
2151 | "o" ->
2152 let (l, n, t, h, pos) =
2154 Scanf.sscanf args "%u %u %d %u %n"
2155 (fun l n t h pos -> l, n, t, h, pos)
2156 with exn ->
2157 dolog "error processing 'o' %S: %s"
2158 cmds (Printexc.to_string exn);
2159 exit 1;
2161 let s = String.sub args pos (String.length args - pos) in
2162 let outline = (s, l, (n, float t /. float h)) in
2163 begin match state.currently with
2164 | Outlining outlines ->
2165 state.currently <- Outlining (outline :: outlines)
2166 | Idle ->
2167 state.currently <- Outlining [outline]
2168 | currently ->
2169 dolog "invalid outlining state";
2170 logcurrently currently
2173 | "info" ->
2174 state.docinfo <- (1, args) :: state.docinfo
2176 | "infoend" ->
2177 state.uioh#infochanged Docinfo;
2178 state.docinfo <- List.rev state.docinfo
2180 | _ ->
2181 dolog "unknown cmd `%S'" cmds
2184 let onhist cb =
2185 let rc = cb.rc in
2186 let action = function
2187 | HCprev -> cbget cb ~-1
2188 | HCnext -> cbget cb 1
2189 | HCfirst -> cbget cb ~-(cb.rc)
2190 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2191 and cancel () = cb.rc <- rc
2192 in (action, cancel)
2195 let search pattern forward =
2196 if String.length pattern > 0
2197 then
2198 let pn, py =
2199 match state.layout with
2200 | [] -> 0, 0
2201 | l :: _ ->
2202 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2204 wcmd "search %d %d %d %d,%s\000"
2205 (btod conf.icase) pn py (btod forward) pattern;
2208 let intentry text key =
2209 let c =
2210 if key >= 32 && key < 127
2211 then Char.chr key
2212 else '\000'
2214 match c with
2215 | '0' .. '9' ->
2216 let text = addchar text c in
2217 TEcont text
2219 | _ ->
2220 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2221 TEcont text
2224 let textentry text key =
2225 if key land 0xff00 = 0xff00
2226 then TEcont text
2227 else TEcont (text ^ Wsi.toutf8 key)
2230 let reqlayout angle proportional =
2231 match state.throttle with
2232 | None ->
2233 if nogeomcmds state.geomcmds
2234 then state.anchor <- getanchor ();
2235 conf.angle <- angle mod 360;
2236 if conf.angle != 0
2237 then (
2238 match state.mode with
2239 | LinkNav _ -> state.mode <- View
2240 | _ -> ()
2242 conf.proportional <- proportional;
2243 invalidate "reqlayout"
2244 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2245 | _ -> ()
2248 let settrim trimmargins trimfuzz =
2249 if nogeomcmds state.geomcmds
2250 then state.anchor <- getanchor ();
2251 conf.trimmargins <- trimmargins;
2252 conf.trimfuzz <- trimfuzz;
2253 let x0, y0, x1, y1 = trimfuzz in
2254 invalidate "settrim"
2255 (fun () ->
2256 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2257 Hashtbl.iter (fun _ opaque ->
2258 wcmd "freepage %s" opaque;
2259 ) state.pagemap;
2260 Hashtbl.clear state.pagemap;
2263 let setzoom zoom =
2264 match state.throttle with
2265 | None ->
2266 let zoom = max 0.01 zoom in
2267 if zoom <> conf.zoom
2268 then (
2269 state.prevzoom <- conf.zoom;
2270 let relx =
2271 if zoom <= 1.0
2272 then (state.x <- 0; 0.0)
2273 else float state.x /. float state.w
2275 conf.zoom <- zoom;
2276 reshape conf.winw conf.winh;
2277 if zoom > 1.0
2278 then (
2279 let x = relx *. float state.w in
2280 state.x <- truncate x;
2282 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2285 | Some (layout, y, started) ->
2286 let time =
2287 match conf.maxwait with
2288 | None -> 0.0
2289 | Some t -> t
2291 let dt = now () -. started in
2292 if dt > time
2293 then (
2294 state.y <- y;
2295 load layout;
2299 let setcolumns columns coverA coverB =
2300 if columns < 2
2301 then (
2302 conf.columns <- None;
2303 state.x <- 0;
2304 setzoom 1.0;
2306 else (
2307 conf.columns <- Some ((columns, coverA, coverB), [||]);
2308 conf.zoom <- 1.0;
2310 reshape conf.winw conf.winh;
2313 let enterbirdseye () =
2314 let zoom = float conf.thumbw /. float conf.winw in
2315 let birdseyepageno =
2316 let cy = conf.winh / 2 in
2317 let fold = function
2318 | [] -> 0
2319 | l :: rest ->
2320 let rec fold best = function
2321 | [] -> best.pageno
2322 | l :: rest ->
2323 let d = cy - (l.pagedispy + l.pagevh/2)
2324 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2325 if abs d < abs dbest
2326 then fold l rest
2327 else best.pageno
2328 in fold l rest
2330 fold state.layout
2332 state.mode <- Birdseye (
2333 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2335 conf.zoom <- zoom;
2336 conf.presentation <- false;
2337 conf.interpagespace <- 10;
2338 conf.hlinks <- false;
2339 state.x <- 0;
2340 state.mstate <- Mnone;
2341 conf.maxwait <- None;
2342 conf.columns <- (
2343 match conf.beyecolumns with
2344 | Some c ->
2345 conf.zoom <- 1.0;
2346 Some ((c, 0, 0), [||])
2347 | None -> None
2349 Wsi.setcursor Wsi.CURSOR_INHERIT;
2350 if conf.verbose
2351 then
2352 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2353 (100.0*.zoom)
2354 else
2355 state.text <- ""
2357 reshape conf.winw conf.winh;
2360 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2361 state.mode <- View;
2362 conf.zoom <- c.zoom;
2363 conf.presentation <- c.presentation;
2364 conf.interpagespace <- c.interpagespace;
2365 conf.maxwait <- c.maxwait;
2366 conf.hlinks <- c.hlinks;
2367 conf.beyecolumns <- (
2368 match conf.columns with
2369 | Some ((c, _, _), _) -> Some c
2370 | None -> None
2372 conf.columns <- (
2373 match c.columns with
2374 | Some (c, _) -> Some (c, [||])
2375 | None -> None
2377 state.x <- leftx;
2378 if conf.verbose
2379 then
2380 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2381 (100.0*.conf.zoom)
2383 reshape conf.winw conf.winh;
2384 state.anchor <- if goback then anchor else (pageno, 0.0);
2387 let togglebirdseye () =
2388 match state.mode with
2389 | Birdseye vals -> leavebirdseye vals true
2390 | View -> enterbirdseye ()
2391 | _ -> ()
2394 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2395 let pageno = max 0 (pageno - incr) in
2396 let rec loop = function
2397 | [] -> gotopage1 pageno 0
2398 | l :: _ when l.pageno = pageno ->
2399 if l.pagedispy >= 0 && l.pagey = 0
2400 then G.postRedisplay "upbirdseye"
2401 else gotopage1 pageno 0
2402 | _ :: rest -> loop rest
2404 loop state.layout;
2405 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2408 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2409 let pageno = min (state.pagecount - 1) (pageno + incr) in
2410 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2411 let rec loop = function
2412 | [] ->
2413 let y, h = getpageyh pageno in
2414 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2415 gotoy (clamp dy)
2416 | l :: _ when l.pageno = pageno ->
2417 if l.pagevh != l.pageh
2418 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2419 else G.postRedisplay "downbirdseye"
2420 | _ :: rest -> loop rest
2422 loop state.layout
2425 let optentry mode _ key =
2426 let btos b = if b then "on" else "off" in
2427 if key >= 32 && key < 127
2428 then
2429 let c = Char.chr key in
2430 match c with
2431 | 's' ->
2432 let ondone s =
2433 try conf.scrollstep <- int_of_string s with exc ->
2434 state.text <- Printf.sprintf "bad integer `%s': %s"
2435 s (Printexc.to_string exc)
2437 TEswitch ("scroll step: ", "", None, intentry, ondone)
2439 | 'A' ->
2440 let ondone s =
2442 conf.autoscrollstep <- int_of_string s;
2443 if state.autoscroll <> None
2444 then state.autoscroll <- Some conf.autoscrollstep
2445 with exc ->
2446 state.text <- Printf.sprintf "bad integer `%s': %s"
2447 s (Printexc.to_string exc)
2449 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2451 | 'C' ->
2452 let ondone s =
2454 let n, a, b = columns_of_string s in
2455 setcolumns n a b;
2456 with exc ->
2457 state.text <- Printf.sprintf "bad columns `%s': %s"
2458 s (Printexc.to_string exc)
2460 TEswitch ("columns: ", "", None, textentry, ondone)
2462 | 'Z' ->
2463 let ondone s =
2465 let zoom = float (int_of_string s) /. 100.0 in
2466 setzoom zoom
2467 with exc ->
2468 state.text <- Printf.sprintf "bad integer `%s': %s"
2469 s (Printexc.to_string exc)
2471 TEswitch ("zoom: ", "", None, intentry, ondone)
2473 | 't' ->
2474 let ondone s =
2476 conf.thumbw <- bound (int_of_string s) 2 4096;
2477 state.text <-
2478 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2479 begin match mode with
2480 | Birdseye beye ->
2481 leavebirdseye beye false;
2482 enterbirdseye ();
2483 | _ -> ();
2485 with exc ->
2486 state.text <- Printf.sprintf "bad integer `%s': %s"
2487 s (Printexc.to_string exc)
2489 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2491 | 'R' ->
2492 let ondone s =
2493 match try
2494 Some (int_of_string s)
2495 with exc ->
2496 state.text <- Printf.sprintf "bad integer `%s': %s"
2497 s (Printexc.to_string exc);
2498 None
2499 with
2500 | Some angle -> reqlayout angle conf.proportional
2501 | None -> ()
2503 TEswitch ("rotation: ", "", None, intentry, ondone)
2505 | 'i' ->
2506 conf.icase <- not conf.icase;
2507 TEdone ("case insensitive search " ^ (btos conf.icase))
2509 | 'p' ->
2510 conf.preload <- not conf.preload;
2511 gotoy state.y;
2512 TEdone ("preload " ^ (btos conf.preload))
2514 | 'v' ->
2515 conf.verbose <- not conf.verbose;
2516 TEdone ("verbose " ^ (btos conf.verbose))
2518 | 'd' ->
2519 conf.debug <- not conf.debug;
2520 TEdone ("debug " ^ (btos conf.debug))
2522 | 'h' ->
2523 conf.maxhfit <- not conf.maxhfit;
2524 state.maxy <-
2525 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2526 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2528 | 'c' ->
2529 conf.crophack <- not conf.crophack;
2530 TEdone ("crophack " ^ btos conf.crophack)
2532 | 'a' ->
2533 let s =
2534 match conf.maxwait with
2535 | None ->
2536 conf.maxwait <- Some infinity;
2537 "always wait for page to complete"
2538 | Some _ ->
2539 conf.maxwait <- None;
2540 "show placeholder if page is not ready"
2542 TEdone s
2544 | 'f' ->
2545 conf.underinfo <- not conf.underinfo;
2546 TEdone ("underinfo " ^ btos conf.underinfo)
2548 | 'P' ->
2549 conf.savebmarks <- not conf.savebmarks;
2550 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2552 | 'S' ->
2553 let ondone s =
2555 let pageno, py =
2556 match state.layout with
2557 | [] -> 0, 0
2558 | l :: _ ->
2559 l.pageno, l.pagey
2561 conf.interpagespace <- int_of_string s;
2562 state.maxy <- calcheight ();
2563 let y = getpagey pageno in
2564 gotoy (y + py)
2565 with exc ->
2566 state.text <- Printf.sprintf "bad integer `%s': %s"
2567 s (Printexc.to_string exc)
2569 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2571 | 'l' ->
2572 reqlayout conf.angle (not conf.proportional);
2573 TEdone ("proportional display " ^ btos conf.proportional)
2575 | 'T' ->
2576 settrim (not conf.trimmargins) conf.trimfuzz;
2577 TEdone ("trim margins " ^ btos conf.trimmargins)
2579 | 'I' ->
2580 conf.invert <- not conf.invert;
2581 TEdone ("invert colors " ^ btos conf.invert)
2583 | 'x' ->
2584 let ondone s =
2585 cbput state.hists.sel s;
2586 conf.selcmd <- s;
2588 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2589 textentry, ondone)
2591 | _ ->
2592 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2593 TEstop
2594 else
2595 TEcont state.text
2598 class type lvsource = object
2599 method getitemcount : int
2600 method getitem : int -> (string * int)
2601 method hasaction : int -> bool
2602 method exit :
2603 uioh:uioh ->
2604 cancel:bool ->
2605 active:int ->
2606 first:int ->
2607 pan:int ->
2608 qsearch:string ->
2609 uioh option
2610 method getactive : int
2611 method getfirst : int
2612 method getqsearch : string
2613 method setqsearch : string -> unit
2614 method getpan : int
2615 end;;
2617 class virtual lvsourcebase = object
2618 val mutable m_active = 0
2619 val mutable m_first = 0
2620 val mutable m_qsearch = ""
2621 val mutable m_pan = 0
2622 method getactive = m_active
2623 method getfirst = m_first
2624 method getqsearch = m_qsearch
2625 method getpan = m_pan
2626 method setqsearch s = m_qsearch <- s
2627 end;;
2629 let withoutlastutf8 s =
2630 let len = String.length s in
2631 if len = 0
2632 then s
2633 else
2634 let rec find pos =
2635 if pos = 0
2636 then pos
2637 else
2638 let b = Char.code s.[pos] in
2639 if b land 0b110000 = 0b11000000
2640 then find (pos-1)
2641 else pos-1
2643 let first =
2644 if Char.code s.[len-1] land 0x80 = 0
2645 then len-1
2646 else find (len-1)
2648 String.sub s 0 first;
2651 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2652 let enttext te =
2653 state.mode <- Textentry (te, onleave);
2654 state.text <- "";
2655 enttext ();
2656 G.postRedisplay "textentrykeyboard enttext";
2658 let histaction cmd =
2659 match opthist with
2660 | None -> ()
2661 | Some (action, _) ->
2662 state.mode <- Textentry (
2663 (c, action cmd, opthist, onkey, ondone), onleave
2665 G.postRedisplay "textentry histaction"
2667 match key with
2668 | 0xff08 -> (* backspace *)
2669 let s = withoutlastutf8 text in
2670 let len = String.length s in
2671 if len = 0
2672 then (
2673 onleave Cancel;
2674 G.postRedisplay "textentrykeyboard after cancel";
2676 else (
2677 enttext (c, s, opthist, onkey, ondone)
2680 | 0xff0d ->
2681 ondone text;
2682 onleave Confirm;
2683 G.postRedisplay "textentrykeyboard after confirm"
2685 | 0xff52 -> histaction HCprev
2686 | 0xff54 -> histaction HCnext
2687 | 0xff50 -> histaction HCfirst
2688 | 0xff57 -> histaction HClast
2690 | 0xff1b -> (* escape*)
2691 if String.length text = 0
2692 then (
2693 begin match opthist with
2694 | None -> ()
2695 | Some (_, onhistcancel) -> onhistcancel ()
2696 end;
2697 onleave Cancel;
2698 state.text <- "";
2699 G.postRedisplay "textentrykeyboard after cancel2"
2701 else (
2702 enttext (c, "", opthist, onkey, ondone)
2705 | 0xff9f | 0xffff -> () (* delete *)
2707 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2708 begin match onkey text key with
2709 | TEdone text ->
2710 ondone text;
2711 onleave Confirm;
2712 G.postRedisplay "textentrykeyboard after confirm2";
2714 | TEcont text ->
2715 enttext (c, text, opthist, onkey, ondone);
2717 | TEstop ->
2718 onleave Cancel;
2719 G.postRedisplay "textentrykeyboard after cancel3"
2721 | TEswitch te ->
2722 state.mode <- Textentry (te, onleave);
2723 G.postRedisplay "textentrykeyboard switch";
2724 end;
2726 | _ ->
2727 vlog "unhandled key %s" (Wsi.keyname key)
2730 let firstof first active =
2731 if first > active || abs (first - active) > fstate.maxrows - 1
2732 then max 0 (active - (fstate.maxrows/2))
2733 else first
2736 let calcfirst first active =
2737 if active > first
2738 then
2739 let rows = active - first in
2740 if rows > fstate.maxrows then active - fstate.maxrows else first
2741 else active
2744 let scrollph y maxy =
2745 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2746 let sh = float conf.winh /. sh in
2747 let sh = max sh (float conf.scrollh) in
2749 let percent =
2750 if y = state.maxy
2751 then 1.0
2752 else float y /. float maxy
2754 let position = (float conf.winh -. sh) *. percent in
2756 let position =
2757 if position +. sh > float conf.winh
2758 then float conf.winh -. sh
2759 else position
2761 position, sh;
2764 let coe s = (s :> uioh);;
2766 class listview ~(source:lvsource) ~trusted ~modehash =
2767 object (self)
2768 val m_pan = source#getpan
2769 val m_first = source#getfirst
2770 val m_active = source#getactive
2771 val m_qsearch = source#getqsearch
2772 val m_prev_uioh = state.uioh
2774 method private elemunder y =
2775 let n = y / (fstate.fontsize+1) in
2776 if m_first + n < source#getitemcount
2777 then (
2778 if source#hasaction (m_first + n)
2779 then Some (m_first + n)
2780 else None
2782 else None
2784 method display =
2785 Gl.enable `blend;
2786 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2787 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2788 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2789 GlDraw.color (1., 1., 1.);
2790 Gl.enable `texture_2d;
2791 let fs = fstate.fontsize in
2792 let nfs = fs + 1 in
2793 let ww = fstate.wwidth in
2794 let tabw = 30.0*.ww in
2795 let itemcount = source#getitemcount in
2796 let rec loop row =
2797 if (row - m_first) * nfs > conf.winh
2798 then ()
2799 else (
2800 if row >= 0 && row < itemcount
2801 then (
2802 let (s, level) = source#getitem row in
2803 let y = (row - m_first) * nfs in
2804 let x = 5.0 +. float (level + m_pan) *. ww in
2805 if row = m_active
2806 then (
2807 Gl.disable `texture_2d;
2808 GlDraw.polygon_mode `both `line;
2809 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2810 GlDraw.rect (1., float (y + 1))
2811 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2812 GlDraw.polygon_mode `both `fill;
2813 GlDraw.color (1., 1., 1.);
2814 Gl.enable `texture_2d;
2817 let drawtabularstring s =
2818 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2819 if trusted
2820 then
2821 let tabpos = try String.index s '\t' with Not_found -> -1 in
2822 if tabpos > 0
2823 then
2824 let len = String.length s - tabpos - 1 in
2825 let s1 = String.sub s 0 tabpos
2826 and s2 = String.sub s (tabpos + 1) len in
2827 let nx = drawstr x s1 in
2828 let sw = nx -. x in
2829 let x = x +. (max tabw sw) in
2830 drawstr x s2
2831 else
2832 drawstr x s
2833 else
2834 drawstr x s
2836 let _ = drawtabularstring s in
2837 loop (row+1)
2841 loop m_first;
2842 Gl.disable `blend;
2843 Gl.disable `texture_2d;
2845 method updownlevel incr =
2846 let len = source#getitemcount in
2847 let curlevel =
2848 if m_active >= 0 && m_active < len
2849 then snd (source#getitem m_active)
2850 else -1
2852 let rec flow i =
2853 if i = len then i-1 else if i = -1 then 0 else
2854 let _, l = source#getitem i in
2855 if l != curlevel then i else flow (i+incr)
2857 let active = flow m_active in
2858 let first = calcfirst m_first active in
2859 G.postRedisplay "outline updownlevel";
2860 {< m_active = active; m_first = first >}
2862 method private key1 key mask =
2863 let set1 active first qsearch =
2864 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2866 let search active pattern incr =
2867 let dosearch re =
2868 let rec loop n =
2869 if n >= 0 && n < source#getitemcount
2870 then (
2871 let s, _ = source#getitem n in
2873 (try ignore (Str.search_forward re s 0); true
2874 with Not_found -> false)
2875 then Some n
2876 else loop (n + incr)
2878 else None
2880 loop active
2883 let re = Str.regexp_case_fold pattern in
2884 dosearch re
2885 with Failure s ->
2886 state.text <- s;
2887 None
2889 let itemcount = source#getitemcount in
2890 let find start incr =
2891 let rec find i =
2892 if i = -1 || i = itemcount
2893 then -1
2894 else (
2895 if source#hasaction i
2896 then i
2897 else find (i + incr)
2900 find start
2902 let set active first =
2903 let first = bound first 0 (itemcount - fstate.maxrows) in
2904 state.text <- "";
2905 coe {< m_active = active; m_first = first >}
2907 let navigate incr =
2908 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2909 let active, first =
2910 let incr1 = if incr > 0 then 1 else -1 in
2911 if isvisible m_first m_active
2912 then
2913 let next =
2914 let next = m_active + incr in
2915 let next =
2916 if next < 0 || next >= itemcount
2917 then -1
2918 else find next incr1
2920 if next = -1 || abs (m_active - next) > fstate.maxrows
2921 then -1
2922 else next
2924 if next = -1
2925 then
2926 let first = m_first + incr in
2927 let first = bound first 0 (itemcount - 1) in
2928 let next =
2929 let next = m_active + incr in
2930 let next = bound next 0 (itemcount - 1) in
2931 find next ~-incr1
2933 let active = if next = -1 then m_active else next in
2934 active, first
2935 else
2936 let first = min next m_first in
2937 let first =
2938 if abs (next - first) > fstate.maxrows
2939 then first + incr
2940 else first
2942 next, first
2943 else
2944 let first = m_first + incr in
2945 let first = bound first 0 (itemcount - 1) in
2946 let active =
2947 let next = m_active + incr in
2948 let next = bound next 0 (itemcount - 1) in
2949 let next = find next incr1 in
2950 let active =
2951 if next = -1 || abs (m_active - first) > fstate.maxrows
2952 then (
2953 let active = if m_active = -1 then next else m_active in
2954 active
2956 else next
2958 if isvisible first active
2959 then active
2960 else -1
2962 active, first
2964 G.postRedisplay "listview navigate";
2965 set active first;
2967 match key with
2968 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2969 let incr = if key = 0x72 then -1 else 1 in
2970 let active, first =
2971 match search (m_active + incr) m_qsearch incr with
2972 | None ->
2973 state.text <- m_qsearch ^ " [not found]";
2974 m_active, m_first
2975 | Some active ->
2976 state.text <- m_qsearch;
2977 active, firstof m_first active
2979 G.postRedisplay "listview ctrl-r/s";
2980 set1 active first m_qsearch;
2982 | 0xff08 -> (* backspace *)
2983 if String.length m_qsearch = 0
2984 then coe self
2985 else (
2986 let qsearch = withoutlastutf8 m_qsearch in
2987 let len = String.length qsearch in
2988 if len = 0
2989 then (
2990 state.text <- "";
2991 G.postRedisplay "listview empty qsearch";
2992 set1 m_active m_first "";
2994 else
2995 let active, first =
2996 match search m_active qsearch ~-1 with
2997 | None ->
2998 state.text <- qsearch ^ " [not found]";
2999 m_active, m_first
3000 | Some active ->
3001 state.text <- qsearch;
3002 active, firstof m_first active
3004 G.postRedisplay "listview backspace qsearch";
3005 set1 active first qsearch
3008 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3009 let pattern = m_qsearch ^ Wsi.toutf8 key in
3010 let active, first =
3011 match search m_active pattern 1 with
3012 | None ->
3013 state.text <- pattern ^ " [not found]";
3014 m_active, m_first
3015 | Some active ->
3016 state.text <- pattern;
3017 active, firstof m_first active
3019 G.postRedisplay "listview qsearch add";
3020 set1 active first pattern;
3022 | 0xff1b -> (* escape *)
3023 state.text <- "";
3024 if String.length m_qsearch = 0
3025 then (
3026 G.postRedisplay "list view escape";
3027 begin
3028 match
3029 source#exit (coe self) true m_active m_first m_pan m_qsearch
3030 with
3031 | None -> m_prev_uioh
3032 | Some uioh -> uioh
3035 else (
3036 G.postRedisplay "list view kill qsearch";
3037 source#setqsearch "";
3038 coe {< m_qsearch = "" >}
3041 | 0xff0d -> (* return *)
3042 state.text <- "";
3043 let self = {< m_qsearch = "" >} in
3044 source#setqsearch "";
3045 let opt =
3046 G.postRedisplay "listview enter";
3047 if m_active >= 0 && m_active < source#getitemcount
3048 then (
3049 source#exit (coe self) false m_active m_first m_pan "";
3051 else (
3052 source#exit (coe self) true m_active m_first m_pan "";
3055 begin match opt with
3056 | None -> m_prev_uioh
3057 | Some uioh -> uioh
3060 | 0xff9f | 0xffff -> (* delete *)
3061 coe self
3063 | 0xff52 -> navigate ~-1 (* up *)
3064 | 0xff54 -> navigate 1 (* down *)
3065 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3066 | 0xff56 -> navigate fstate.maxrows (* next *)
3068 | 0xff53 -> (* right *)
3069 state.text <- "";
3070 G.postRedisplay "listview right";
3071 coe {< m_pan = m_pan - 1 >}
3073 | 0xff51 -> (* left *)
3074 state.text <- "";
3075 G.postRedisplay "listview left";
3076 coe {< m_pan = m_pan + 1 >}
3078 | 0xff50 -> (* home *)
3079 let active = find 0 1 in
3080 G.postRedisplay "listview home";
3081 set active 0;
3083 | 0xff57 -> (* end *)
3084 let first = max 0 (itemcount - fstate.maxrows) in
3085 let active = find (itemcount - 1) ~-1 in
3086 G.postRedisplay "listview end";
3087 set active first;
3089 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3090 coe self
3092 | _ ->
3093 dolog "listview unknown key %#x" key; coe self
3095 method key key mask =
3096 match state.mode with
3097 | Textentry te -> textentrykeyboard key mask te; coe self
3098 | _ -> self#key1 key mask
3100 method button button down x y _ =
3101 let opt =
3102 match button with
3103 | 1 when x > conf.winw - conf.scrollbw ->
3104 G.postRedisplay "listview scroll";
3105 if down
3106 then
3107 let _, position, sh = self#scrollph in
3108 if y > truncate position && y < truncate (position +. sh)
3109 then (
3110 state.mstate <- Mscrolly;
3111 Some (coe self)
3113 else
3114 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3115 let first = truncate (s *. float source#getitemcount) in
3116 let first = min source#getitemcount first in
3117 Some (coe {< m_first = first; m_active = first >})
3118 else (
3119 state.mstate <- Mnone;
3120 Some (coe self);
3122 | 1 when not down ->
3123 begin match self#elemunder y with
3124 | Some n ->
3125 G.postRedisplay "listview click";
3126 source#exit
3127 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3128 | _ ->
3129 Some (coe self)
3131 | n when (n == 4 || n == 5) && not down ->
3132 let len = source#getitemcount in
3133 let first =
3134 if n = 5 && m_first + fstate.maxrows >= len
3135 then
3136 m_first
3137 else
3138 let first = m_first + (if n == 4 then -1 else 1) in
3139 bound first 0 (len - 1)
3141 G.postRedisplay "listview wheel";
3142 Some (coe {< m_first = first >})
3143 | _ ->
3144 Some (coe self)
3146 match opt with
3147 | None -> m_prev_uioh
3148 | Some uioh -> uioh
3150 method motion _ y =
3151 match state.mstate with
3152 | Mscrolly ->
3153 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3154 let first = truncate (s *. float source#getitemcount) in
3155 let first = min source#getitemcount first in
3156 G.postRedisplay "listview motion";
3157 coe {< m_first = first; m_active = first >}
3158 | _ -> coe self
3160 method pmotion x y =
3161 if x < conf.winw - conf.scrollbw
3162 then
3163 let n =
3164 match self#elemunder y with
3165 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3166 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3168 let o =
3169 if n != m_active
3170 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3171 else self
3173 coe o
3174 else (
3175 Wsi.setcursor Wsi.CURSOR_INHERIT;
3176 coe self
3179 method infochanged _ = ()
3181 method scrollpw = (0, 0.0, 0.0)
3182 method scrollph =
3183 let nfs = fstate.fontsize + 1 in
3184 let y = m_first * nfs in
3185 let itemcount = source#getitemcount in
3186 let maxi = max 0 (itemcount - fstate.maxrows) in
3187 let maxy = maxi * nfs in
3188 let p, h = scrollph y maxy in
3189 conf.scrollbw, p, h
3191 method modehash = modehash
3192 end;;
3194 class outlinelistview ~source =
3195 object (self)
3196 inherit listview
3197 ~source:(source :> lvsource)
3198 ~trusted:false
3199 ~modehash:(findkeyhash conf "outline")
3200 as super
3202 method key key mask =
3203 let calcfirst first active =
3204 if active > first
3205 then
3206 let rows = active - first in
3207 if rows > fstate.maxrows then active - fstate.maxrows else first
3208 else active
3210 let navigate incr =
3211 let active = m_active + incr in
3212 let active = bound active 0 (source#getitemcount - 1) in
3213 let first = calcfirst m_first active in
3214 G.postRedisplay "outline navigate";
3215 coe {< m_active = active; m_first = first >}
3217 let ctrl = Wsi.withctrl mask in
3218 match key with
3219 | 110 when ctrl -> (* ctrl-n *)
3220 source#narrow m_qsearch;
3221 G.postRedisplay "outline ctrl-n";
3222 coe {< m_first = 0; m_active = 0 >}
3224 | 117 when ctrl -> (* ctrl-u *)
3225 source#denarrow;
3226 G.postRedisplay "outline ctrl-u";
3227 state.text <- "";
3228 coe {< m_first = 0; m_active = 0 >}
3230 | 108 when ctrl -> (* ctrl-l *)
3231 let first = m_active - (fstate.maxrows / 2) in
3232 G.postRedisplay "outline ctrl-l";
3233 coe {< m_first = first >}
3235 | 0xff9f | 0xffff -> (* delete *)
3236 source#remove m_active;
3237 G.postRedisplay "outline delete";
3238 let active = max 0 (m_active-1) in
3239 coe {< m_first = firstof m_first active;
3240 m_active = active >}
3242 | 0xff52 -> navigate ~-1 (* up *)
3243 | 0xff54 -> navigate 1 (* down *)
3244 | 0xff55 -> (* prior *)
3245 navigate ~-(fstate.maxrows)
3246 | 0xff56 -> (* next *)
3247 navigate fstate.maxrows
3249 | 0xff53 -> (* [ctrl-]right *)
3250 let o =
3251 if ctrl
3252 then (
3253 G.postRedisplay "outline ctrl right";
3254 {< m_pan = m_pan + 1 >}
3256 else self#updownlevel 1
3258 coe o
3260 | 0xff51 -> (* [ctrl-]left *)
3261 let o =
3262 if ctrl
3263 then (
3264 G.postRedisplay "outline ctrl left";
3265 {< m_pan = m_pan - 1 >}
3267 else self#updownlevel ~-1
3269 coe o
3271 | 0xff50 -> (* home *)
3272 G.postRedisplay "outline home";
3273 coe {< m_first = 0; m_active = 0 >}
3275 | 0xff57 -> (* end *)
3276 let active = source#getitemcount - 1 in
3277 let first = max 0 (active - fstate.maxrows) in
3278 G.postRedisplay "outline end";
3279 coe {< m_active = active; m_first = first >}
3281 | _ -> super#key key mask
3284 let outlinesource usebookmarks =
3285 let empty = [||] in
3286 (object
3287 inherit lvsourcebase
3288 val mutable m_items = empty
3289 val mutable m_orig_items = empty
3290 val mutable m_prev_items = empty
3291 val mutable m_narrow_pattern = ""
3292 val mutable m_hadremovals = false
3294 method getitemcount =
3295 Array.length m_items + (if m_hadremovals then 1 else 0)
3297 method getitem n =
3298 if n == Array.length m_items && m_hadremovals
3299 then
3300 ("[Confirm removal]", 0)
3301 else
3302 let s, n, _ = m_items.(n) in
3303 (s, n)
3305 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3306 ignore (uioh, first, qsearch);
3307 let confrimremoval = m_hadremovals && active = Array.length m_items in
3308 let items =
3309 if String.length m_narrow_pattern = 0
3310 then m_orig_items
3311 else m_items
3313 if not cancel
3314 then (
3315 if not confrimremoval
3316 then(
3317 let _, _, anchor = m_items.(active) in
3318 gotoanchor anchor;
3319 m_items <- items;
3321 else (
3322 state.bookmarks <- Array.to_list m_items;
3323 m_orig_items <- m_items;
3326 else m_items <- items;
3327 m_pan <- pan;
3328 None
3330 method hasaction _ = true
3332 method greetmsg =
3333 if Array.length m_items != Array.length m_orig_items
3334 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3335 else ""
3337 method narrow pattern =
3338 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3339 match reopt with
3340 | None -> ()
3341 | Some re ->
3342 let rec loop accu n =
3343 if n = -1
3344 then (
3345 m_narrow_pattern <- pattern;
3346 m_items <- Array.of_list accu
3348 else
3349 let (s, _, _) as o = m_items.(n) in
3350 let accu =
3351 if (try ignore (Str.search_forward re s 0); true
3352 with Not_found -> false)
3353 then o :: accu
3354 else accu
3356 loop accu (n-1)
3358 loop [] (Array.length m_items - 1)
3360 method denarrow =
3361 m_orig_items <- (
3362 if usebookmarks
3363 then Array.of_list state.bookmarks
3364 else state.outlines
3366 m_items <- m_orig_items
3368 method remove m =
3369 if usebookmarks
3370 then
3371 if m >= 0 && m < Array.length m_items
3372 then (
3373 m_hadremovals <- true;
3374 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3375 let n = if n >= m then n+1 else n in
3376 m_items.(n)
3380 method reset anchor items =
3381 m_hadremovals <- false;
3382 if m_orig_items == empty || m_prev_items != items
3383 then (
3384 m_orig_items <- items;
3385 if String.length m_narrow_pattern = 0
3386 then m_items <- items;
3388 m_prev_items <- items;
3389 let rely = getanchory anchor in
3390 let active =
3391 let rec loop n best bestd =
3392 if n = Array.length m_items
3393 then best
3394 else
3395 let (_, _, anchor) = m_items.(n) in
3396 let orely = getanchory anchor in
3397 let d = abs (orely - rely) in
3398 if d < bestd
3399 then loop (n+1) n d
3400 else loop (n+1) best bestd
3402 loop 0 ~-1 max_int
3404 m_active <- active;
3405 m_first <- firstof m_first active
3406 end)
3409 let enterselector usebookmarks =
3410 let source = outlinesource usebookmarks in
3411 fun errmsg ->
3412 let outlines =
3413 if usebookmarks
3414 then Array.of_list state.bookmarks
3415 else state.outlines
3417 if Array.length outlines = 0
3418 then (
3419 showtext ' ' errmsg;
3421 else (
3422 state.text <- source#greetmsg;
3423 Wsi.setcursor Wsi.CURSOR_INHERIT;
3424 let anchor = getanchor () in
3425 source#reset anchor outlines;
3426 state.uioh <- coe (new outlinelistview ~source);
3427 G.postRedisplay "enter selector";
3431 let enteroutlinemode =
3432 let f = enterselector false in
3433 fun ()-> f "Document has no outline";
3436 let enterbookmarkmode =
3437 let f = enterselector true in
3438 fun () -> f "Document has no bookmarks (yet)";
3441 let color_of_string s =
3442 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3443 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3447 let color_to_string (r, g, b) =
3448 let r = truncate (r *. 256.0)
3449 and g = truncate (g *. 256.0)
3450 and b = truncate (b *. 256.0) in
3451 Printf.sprintf "%d/%d/%d" r g b
3454 let irect_of_string s =
3455 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3458 let irect_to_string (x0,y0,x1,y1) =
3459 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3462 let makecheckers () =
3463 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3464 following to say:
3465 converted by Issac Trotts. July 25, 2002 *)
3466 let image_height = 64
3467 and image_width = 64 in
3469 let make_image () =
3470 let image =
3471 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3473 for i = 0 to image_width - 1 do
3474 for j = 0 to image_height - 1 do
3475 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3476 (if (i land 8 ) lxor (j land 8) = 0
3477 then [|255;255;255|] else [|200;200;200|])
3478 done
3479 done;
3480 image
3482 let image = make_image () in
3483 let id = GlTex.gen_texture () in
3484 GlTex.bind_texture `texture_2d id;
3485 GlPix.store (`unpack_alignment 1);
3486 GlTex.image2d image;
3487 List.iter (GlTex.parameter ~target:`texture_2d)
3488 [ `wrap_s `repeat;
3489 `wrap_t `repeat;
3490 `mag_filter `nearest;
3491 `min_filter `nearest ];
3495 let setcheckers enabled =
3496 match state.texid with
3497 | None ->
3498 if enabled then state.texid <- Some (makecheckers ())
3500 | Some texid ->
3501 if not enabled
3502 then (
3503 GlTex.delete_texture texid;
3504 state.texid <- None;
3508 let int_of_string_with_suffix s =
3509 let l = String.length s in
3510 let s1, shift =
3511 if l > 1
3512 then
3513 let suffix = Char.lowercase s.[l-1] in
3514 match suffix with
3515 | 'k' -> String.sub s 0 (l-1), 10
3516 | 'm' -> String.sub s 0 (l-1), 20
3517 | 'g' -> String.sub s 0 (l-1), 30
3518 | _ -> s, 0
3519 else s, 0
3521 let n = int_of_string s1 in
3522 let m = n lsl shift in
3523 if m < 0 || m < n
3524 then raise (Failure "value too large")
3525 else m
3528 let string_with_suffix_of_int n =
3529 if n = 0
3530 then "0"
3531 else
3532 let n, s =
3533 if n = 0
3534 then 0, ""
3535 else (
3536 if n land ((1 lsl 20) - 1) = 0
3537 then n lsr 20, "M"
3538 else (
3539 if n land ((1 lsl 10) - 1) = 0
3540 then n lsr 10, "K"
3541 else n, ""
3545 let rec loop s n =
3546 let h = n mod 1000 in
3547 let n = n / 1000 in
3548 if n = 0
3549 then string_of_int h ^ s
3550 else (
3551 let s = Printf.sprintf "_%03d%s" h s in
3552 loop s n
3555 loop "" n ^ s;
3558 let defghyllscroll = (40, 8, 32);;
3559 let ghyllscroll_of_string s =
3560 let (n, a, b) as nab =
3561 if s = "default"
3562 then defghyllscroll
3563 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3565 if n <= a || n <= b || a >= b
3566 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3567 nab;
3570 let ghyllscroll_to_string ((n, a, b) as nab) =
3571 if nab = defghyllscroll
3572 then "default"
3573 else Printf.sprintf "%d,%d,%d" n a b;
3576 let describe_location () =
3577 let f (fn, _) l =
3578 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3580 let fn, ln = List.fold_left f (-1, -1) state.layout in
3581 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3582 let percent =
3583 if maxy <= 0
3584 then 100.
3585 else (100. *. (float state.y /. float maxy))
3587 if fn = ln
3588 then
3589 Printf.sprintf "page %d of %d [%.2f%%]"
3590 (fn+1) state.pagecount percent
3591 else
3592 Printf.sprintf
3593 "pages %d-%d of %d [%.2f%%]"
3594 (fn+1) (ln+1) state.pagecount percent
3597 let enterinfomode =
3598 let btos b = if b then "\xe2\x88\x9a" else "" in
3599 let showextended = ref false in
3600 let leave mode = function
3601 | Confirm -> state.mode <- mode
3602 | Cancel -> state.mode <- mode in
3603 let src =
3604 (object
3605 val mutable m_first_time = true
3606 val mutable m_l = []
3607 val mutable m_a = [||]
3608 val mutable m_prev_uioh = nouioh
3609 val mutable m_prev_mode = View
3611 inherit lvsourcebase
3613 method reset prev_mode prev_uioh =
3614 m_a <- Array.of_list (List.rev m_l);
3615 m_l <- [];
3616 m_prev_mode <- prev_mode;
3617 m_prev_uioh <- prev_uioh;
3618 if m_first_time
3619 then (
3620 let rec loop n =
3621 if n >= Array.length m_a
3622 then ()
3623 else
3624 match m_a.(n) with
3625 | _, _, _, Action _ -> m_active <- n
3626 | _ -> loop (n+1)
3628 loop 0;
3629 m_first_time <- false;
3632 method int name get set =
3633 m_l <-
3634 (name, `int get, 1, Action (
3635 fun u ->
3636 let ondone s =
3637 try set (int_of_string s)
3638 with exn ->
3639 state.text <- Printf.sprintf "bad integer `%s': %s"
3640 s (Printexc.to_string exn)
3642 state.text <- "";
3643 let te = name ^ ": ", "", None, intentry, ondone in
3644 state.mode <- Textentry (te, leave m_prev_mode);
3646 )) :: m_l
3648 method int_with_suffix name get set =
3649 m_l <-
3650 (name, `intws get, 1, Action (
3651 fun u ->
3652 let ondone s =
3653 try set (int_of_string_with_suffix s)
3654 with exn ->
3655 state.text <- Printf.sprintf "bad integer `%s': %s"
3656 s (Printexc.to_string exn)
3658 state.text <- "";
3659 let te =
3660 name ^ ": ", "", None, intentry_with_suffix, ondone
3662 state.mode <- Textentry (te, leave m_prev_mode);
3664 )) :: m_l
3666 method bool ?(offset=1) ?(btos=btos) name get set =
3667 m_l <-
3668 (name, `bool (btos, get), offset, Action (
3669 fun u ->
3670 let v = get () in
3671 set (not v);
3673 )) :: m_l
3675 method color name get set =
3676 m_l <-
3677 (name, `color get, 1, Action (
3678 fun u ->
3679 let invalid = (nan, nan, nan) in
3680 let ondone s =
3681 let c =
3682 try color_of_string s
3683 with exn ->
3684 state.text <- Printf.sprintf "bad color `%s': %s"
3685 s (Printexc.to_string exn);
3686 invalid
3688 if c <> invalid
3689 then set c;
3691 let te = name ^ ": ", "", None, textentry, ondone in
3692 state.text <- color_to_string (get ());
3693 state.mode <- Textentry (te, leave m_prev_mode);
3695 )) :: m_l
3697 method string name get set =
3698 m_l <-
3699 (name, `string get, 1, Action (
3700 fun u ->
3701 let ondone s = set s in
3702 let te = name ^ ": ", "", None, textentry, ondone in
3703 state.mode <- Textentry (te, leave m_prev_mode);
3705 )) :: m_l
3707 method colorspace name get set =
3708 m_l <-
3709 (name, `string get, 1, Action (
3710 fun _ ->
3711 let source =
3712 let vals = [| "rgb"; "bgr"; "gray" |] in
3713 (object
3714 inherit lvsourcebase
3716 initializer
3717 m_active <- int_of_colorspace conf.colorspace;
3718 m_first <- 0;
3720 method getitemcount = Array.length vals
3721 method getitem n = (vals.(n), 0)
3722 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3723 ignore (uioh, first, pan, qsearch);
3724 if not cancel then set active;
3725 None
3726 method hasaction _ = true
3727 end)
3729 state.text <- "";
3730 let modehash = findkeyhash conf "info" in
3731 coe (new listview ~source ~trusted:true ~modehash)
3732 )) :: m_l
3734 method caption s offset =
3735 m_l <- (s, `empty, offset, Noaction) :: m_l
3737 method caption2 s f offset =
3738 m_l <- (s, `string f, offset, Noaction) :: m_l
3740 method getitemcount = Array.length m_a
3742 method getitem n =
3743 let tostr = function
3744 | `int f -> string_of_int (f ())
3745 | `intws f -> string_with_suffix_of_int (f ())
3746 | `string f -> f ()
3747 | `color f -> color_to_string (f ())
3748 | `bool (btos, f) -> btos (f ())
3749 | `empty -> ""
3751 let name, t, offset, _ = m_a.(n) in
3752 ((let s = tostr t in
3753 if String.length s > 0
3754 then Printf.sprintf "%s\t%s" name s
3755 else name),
3756 offset)
3758 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3759 let uiohopt =
3760 if not cancel
3761 then (
3762 m_qsearch <- qsearch;
3763 let uioh =
3764 match m_a.(active) with
3765 | _, _, _, Action f -> f uioh
3766 | _ -> uioh
3768 Some uioh
3770 else None
3772 m_active <- active;
3773 m_first <- first;
3774 m_pan <- pan;
3775 uiohopt
3777 method hasaction n =
3778 match m_a.(n) with
3779 | _, _, _, Action _ -> true
3780 | _ -> false
3781 end)
3783 let rec fillsrc prevmode prevuioh =
3784 let sep () = src#caption "" 0 in
3785 let colorp name get set =
3786 src#string name
3787 (fun () -> color_to_string (get ()))
3788 (fun v ->
3790 let c = color_of_string v in
3791 set c
3792 with exn ->
3793 state.text <- Printf.sprintf "bad color `%s': %s"
3794 v (Printexc.to_string exn);
3797 let oldmode = state.mode in
3798 let birdseye = isbirdseye state.mode in
3800 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3802 src#bool "presentation mode"
3803 (fun () -> conf.presentation)
3804 (fun v ->
3805 conf.presentation <- v;
3806 state.anchor <- getanchor ();
3807 represent ());
3809 src#bool "ignore case in searches"
3810 (fun () -> conf.icase)
3811 (fun v -> conf.icase <- v);
3813 src#bool "preload"
3814 (fun () -> conf.preload)
3815 (fun v -> conf.preload <- v);
3817 src#bool "highlight links"
3818 (fun () -> conf.hlinks)
3819 (fun v -> conf.hlinks <- v);
3821 src#bool "under info"
3822 (fun () -> conf.underinfo)
3823 (fun v -> conf.underinfo <- v);
3825 src#bool "persistent bookmarks"
3826 (fun () -> conf.savebmarks)
3827 (fun v -> conf.savebmarks <- v);
3829 src#bool "proportional display"
3830 (fun () -> conf.proportional)
3831 (fun v -> reqlayout conf.angle v);
3833 src#bool "trim margins"
3834 (fun () -> conf.trimmargins)
3835 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3837 src#bool "persistent location"
3838 (fun () -> conf.jumpback)
3839 (fun v -> conf.jumpback <- v);
3841 sep ();
3842 src#int "inter-page space"
3843 (fun () -> conf.interpagespace)
3844 (fun n ->
3845 conf.interpagespace <- n;
3846 let pageno, py =
3847 match state.layout with
3848 | [] -> 0, 0
3849 | l :: _ ->
3850 l.pageno, l.pagey
3852 state.maxy <- calcheight ();
3853 let y = getpagey pageno in
3854 gotoy (y + py)
3857 src#int "page bias"
3858 (fun () -> conf.pagebias)
3859 (fun v -> conf.pagebias <- v);
3861 src#int "scroll step"
3862 (fun () -> conf.scrollstep)
3863 (fun n -> conf.scrollstep <- n);
3865 src#int "auto scroll step"
3866 (fun () ->
3867 match state.autoscroll with
3868 | Some step -> step
3869 | _ -> conf.autoscrollstep)
3870 (fun n ->
3871 if state.autoscroll <> None
3872 then state.autoscroll <- Some n;
3873 conf.autoscrollstep <- n);
3875 src#int "zoom"
3876 (fun () -> truncate (conf.zoom *. 100.))
3877 (fun v -> setzoom ((float v) /. 100.));
3879 src#int "rotation"
3880 (fun () -> conf.angle)
3881 (fun v -> reqlayout v conf.proportional);
3883 src#int "scroll bar width"
3884 (fun () -> state.scrollw)
3885 (fun v ->
3886 state.scrollw <- v;
3887 conf.scrollbw <- v;
3888 reshape conf.winw conf.winh;
3891 src#int "scroll handle height"
3892 (fun () -> conf.scrollh)
3893 (fun v -> conf.scrollh <- v;);
3895 src#int "thumbnail width"
3896 (fun () -> conf.thumbw)
3897 (fun v ->
3898 conf.thumbw <- min 4096 v;
3899 match oldmode with
3900 | Birdseye beye ->
3901 leavebirdseye beye false;
3902 enterbirdseye ()
3903 | _ -> ()
3906 src#string "columns"
3907 (fun () ->
3908 match conf.columns with
3909 | None -> "1"
3910 | Some (multicol, _) -> columns_to_string multicol)
3911 (fun v ->
3912 let n, a, b = columns_of_string v in
3913 setcolumns n a b);
3915 sep ();
3916 src#caption "Presentation mode" 0;
3917 src#bool "scrollbar visible"
3918 (fun () -> conf.scrollbarinpm)
3919 (fun v ->
3920 if v != conf.scrollbarinpm
3921 then (
3922 conf.scrollbarinpm <- v;
3923 if conf.presentation
3924 then (
3925 state.scrollw <- if v then conf.scrollbw else 0;
3926 reshape conf.winw conf.winh;
3931 sep ();
3932 src#caption "Pixmap cache" 0;
3933 src#int_with_suffix "size (advisory)"
3934 (fun () -> conf.memlimit)
3935 (fun v -> conf.memlimit <- v);
3937 src#caption2 "used"
3938 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3939 (string_with_suffix_of_int state.memused)
3940 (Hashtbl.length state.tilemap)) 1;
3942 sep ();
3943 src#caption "Layout" 0;
3944 src#caption2 "Dimension"
3945 (fun () ->
3946 Printf.sprintf "%dx%d (virtual %dx%d)"
3947 conf.winw conf.winh
3948 state.w state.maxy)
3950 if conf.debug
3951 then
3952 src#caption2 "Position" (fun () ->
3953 Printf.sprintf "%dx%d" state.x state.y
3955 else
3956 src#caption2 "Visible" (fun () -> describe_location ()) 1
3959 sep ();
3960 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3961 "Save these parameters as global defaults at exit"
3962 (fun () -> conf.bedefault)
3963 (fun v -> conf.bedefault <- v)
3966 sep ();
3967 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3968 src#bool ~offset:0 ~btos "Extended parameters"
3969 (fun () -> !showextended)
3970 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3971 if !showextended
3972 then (
3973 src#bool "checkers"
3974 (fun () -> conf.checkers)
3975 (fun v -> conf.checkers <- v; setcheckers v);
3976 src#bool "update cursor"
3977 (fun () -> conf.updatecurs)
3978 (fun v -> conf.updatecurs <- v);
3979 src#bool "verbose"
3980 (fun () -> conf.verbose)
3981 (fun v -> conf.verbose <- v);
3982 src#bool "invert colors"
3983 (fun () -> conf.invert)
3984 (fun v -> conf.invert <- v);
3985 src#bool "max fit"
3986 (fun () -> conf.maxhfit)
3987 (fun v -> conf.maxhfit <- v);
3988 src#bool "redirect stderr"
3989 (fun () -> conf.redirectstderr)
3990 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3991 src#string "uri launcher"
3992 (fun () -> conf.urilauncher)
3993 (fun v -> conf.urilauncher <- v);
3994 src#string "path launcher"
3995 (fun () -> conf.pathlauncher)
3996 (fun v -> conf.pathlauncher <- v);
3997 src#string "tile size"
3998 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3999 (fun v ->
4001 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4002 conf.tileh <- max 64 w;
4003 conf.tilew <- max 64 h;
4004 flushtiles ();
4005 with exn ->
4006 state.text <- Printf.sprintf "bad tile size `%s': %s"
4007 v (Printexc.to_string exn));
4008 src#int "texture count"
4009 (fun () -> conf.texcount)
4010 (fun v ->
4011 if realloctexts v
4012 then conf.texcount <- v
4013 else showtext '!' " Failed to set texture count please retry later"
4015 src#int "slice height"
4016 (fun () -> conf.sliceheight)
4017 (fun v ->
4018 conf.sliceheight <- v;
4019 wcmd "sliceh %d" conf.sliceheight;
4021 src#int "anti-aliasing level"
4022 (fun () -> conf.aalevel)
4023 (fun v ->
4024 conf.aalevel <- bound v 0 8;
4025 state.anchor <- getanchor ();
4026 opendoc state.path state.password;
4028 src#int "ui font size"
4029 (fun () -> fstate.fontsize)
4030 (fun v -> setfontsize (bound v 5 100));
4031 colorp "background color"
4032 (fun () -> conf.bgcolor)
4033 (fun v -> conf.bgcolor <- v);
4034 src#bool "crop hack"
4035 (fun () -> conf.crophack)
4036 (fun v -> conf.crophack <- v);
4037 src#string "trim fuzz"
4038 (fun () -> irect_to_string conf.trimfuzz)
4039 (fun v ->
4041 conf.trimfuzz <- irect_of_string v;
4042 if conf.trimmargins
4043 then settrim true conf.trimfuzz;
4044 with exn ->
4045 state.text <- Printf.sprintf "bad irect `%s': %s"
4046 v (Printexc.to_string exn)
4048 src#string "throttle"
4049 (fun () ->
4050 match conf.maxwait with
4051 | None -> "show place holder if page is not ready"
4052 | Some time ->
4053 if time = infinity
4054 then "wait for page to fully render"
4055 else
4056 "wait " ^ string_of_float time
4057 ^ " seconds before showing placeholder"
4059 (fun v ->
4061 let f = float_of_string v in
4062 if f <= 0.0
4063 then conf.maxwait <- None
4064 else conf.maxwait <- Some f
4065 with exn ->
4066 state.text <- Printf.sprintf "bad time `%s': %s"
4067 v (Printexc.to_string exn)
4069 src#string "ghyll scroll"
4070 (fun () ->
4071 match conf.ghyllscroll with
4072 | None -> ""
4073 | Some nab -> ghyllscroll_to_string nab
4075 (fun v ->
4077 let gs =
4078 if String.length v = 0
4079 then None
4080 else Some (ghyllscroll_of_string v)
4082 conf.ghyllscroll <- gs
4083 with exn ->
4084 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4085 v (Printexc.to_string exn)
4087 src#string "selection command"
4088 (fun () -> conf.selcmd)
4089 (fun v -> conf.selcmd <- v);
4090 src#colorspace "color space"
4091 (fun () -> colorspace_to_string conf.colorspace)
4092 (fun v ->
4093 conf.colorspace <- colorspace_of_int v;
4094 wcmd "cs %d" v;
4095 load state.layout;
4099 sep ();
4100 src#caption "Document" 0;
4101 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4102 src#caption2 "Pages"
4103 (fun () -> string_of_int state.pagecount) 1;
4104 src#caption2 "Dimensions"
4105 (fun () -> string_of_int (List.length state.pdims)) 1;
4106 if conf.trimmargins
4107 then (
4108 sep ();
4109 src#caption "Trimmed margins" 0;
4110 src#caption2 "Dimensions"
4111 (fun () -> string_of_int (List.length state.pdims)) 1;
4114 src#reset prevmode prevuioh;
4116 fun () ->
4117 state.text <- "";
4118 let prevmode = state.mode
4119 and prevuioh = state.uioh in
4120 fillsrc prevmode prevuioh;
4121 let source = (src :> lvsource) in
4122 let modehash = findkeyhash conf "info" in
4123 state.uioh <- coe (object (self)
4124 inherit listview ~source ~trusted:true ~modehash as super
4125 val mutable m_prevmemused = 0
4126 method infochanged = function
4127 | Memused ->
4128 if m_prevmemused != state.memused
4129 then (
4130 m_prevmemused <- state.memused;
4131 G.postRedisplay "memusedchanged";
4133 | Pdim -> G.postRedisplay "pdimchanged"
4134 | Docinfo -> fillsrc prevmode prevuioh
4136 method key key mask =
4137 if not (Wsi.withctrl mask)
4138 then
4139 match key with
4140 | 0xff51 -> coe (self#updownlevel ~-1)
4141 | 0xff53 -> coe (self#updownlevel 1)
4142 | _ -> super#key key mask
4143 else super#key key mask
4144 end);
4145 G.postRedisplay "info";
4148 let enterhelpmode =
4149 let source =
4150 (object
4151 inherit lvsourcebase
4152 method getitemcount = Array.length state.help
4153 method getitem n =
4154 let s, n, _ = state.help.(n) in
4155 (s, n)
4157 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4158 let optuioh =
4159 if not cancel
4160 then (
4161 m_qsearch <- qsearch;
4162 match state.help.(active) with
4163 | _, _, Action f -> Some (f uioh)
4164 | _ -> Some (uioh)
4166 else None
4168 m_active <- active;
4169 m_first <- first;
4170 m_pan <- pan;
4171 optuioh
4173 method hasaction n =
4174 match state.help.(n) with
4175 | _, _, Action _ -> true
4176 | _ -> false
4178 initializer
4179 m_active <- -1
4180 end)
4181 in fun () ->
4182 let modehash = findkeyhash conf "help" in
4183 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4184 G.postRedisplay "help";
4187 let entermsgsmode =
4188 let msgsource =
4189 let re = Str.regexp "[\r\n]" in
4190 (object
4191 inherit lvsourcebase
4192 val mutable m_items = [||]
4194 method getitemcount = 1 + Array.length m_items
4196 method getitem n =
4197 if n = 0
4198 then "[Clear]", 0
4199 else m_items.(n-1), 0
4201 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4202 ignore uioh;
4203 if not cancel
4204 then (
4205 if active = 0
4206 then Buffer.clear state.errmsgs;
4207 m_qsearch <- qsearch;
4209 m_active <- active;
4210 m_first <- first;
4211 m_pan <- pan;
4212 None
4214 method hasaction n =
4215 n = 0
4217 method reset =
4218 state.newerrmsgs <- false;
4219 let l = Str.split re (Buffer.contents state.errmsgs) in
4220 m_items <- Array.of_list l
4222 initializer
4223 m_active <- 0
4224 end)
4225 in fun () ->
4226 state.text <- "";
4227 msgsource#reset;
4228 let source = (msgsource :> lvsource) in
4229 let modehash = findkeyhash conf "listview" in
4230 state.uioh <- coe (object
4231 inherit listview ~source ~trusted:false ~modehash as super
4232 method display =
4233 if state.newerrmsgs
4234 then msgsource#reset;
4235 super#display
4236 end);
4237 G.postRedisplay "msgs";
4240 let quickbookmark ?title () =
4241 match state.layout with
4242 | [] -> ()
4243 | l :: _ ->
4244 let title =
4245 match title with
4246 | None ->
4247 let sec = Unix.gettimeofday () in
4248 let tm = Unix.localtime sec in
4249 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4250 (l.pageno+1)
4251 tm.Unix.tm_mday
4252 tm.Unix.tm_mon
4253 (tm.Unix.tm_year + 1900)
4254 tm.Unix.tm_hour
4255 tm.Unix.tm_min
4256 | Some title -> title
4258 state.bookmarks <-
4259 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4260 :: state.bookmarks
4263 let doreshape w h =
4264 state.fullscreen <- None;
4265 Wsi.reshape w h;
4268 let setautoscrollspeed step goingdown =
4269 let incr = max 1 ((abs step) / 2) in
4270 let incr = if goingdown then incr else -incr in
4271 let astep = step + incr in
4272 state.autoscroll <- Some astep;
4275 let viewkeyboard key mask =
4276 let enttext te =
4277 let mode = state.mode in
4278 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4279 state.text <- "";
4280 enttext ();
4281 G.postRedisplay "view:enttext"
4283 let ctrl = Wsi.withctrl mask in
4284 match key with
4285 | 81 -> (* Q *)
4286 exit 0
4288 | 0xff63 -> (* insert *)
4289 if conf.angle mod 360 = 0
4290 then (
4291 state.mode <- LinkNav (Ltgendir 0);
4292 gotoy state.y;
4294 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4296 | 0xff1b | 113 -> (* escape / q *)
4297 begin match state.mstate with
4298 | Mzoomrect _ ->
4299 state.mstate <- Mnone;
4300 Wsi.setcursor Wsi.CURSOR_INHERIT;
4301 G.postRedisplay "kill zoom rect";
4302 | _ ->
4303 match state.ranchors with
4304 | [] -> raise Quit
4305 | (path, password, anchor) :: rest ->
4306 state.ranchors <- rest;
4307 state.anchor <- anchor;
4308 opendoc path password
4309 end;
4311 | 0xff08 -> (* backspace *)
4312 let y = getnav ~-1 in
4313 gotoy_and_clear_text y
4315 | 111 -> (* o *)
4316 enteroutlinemode ()
4318 | 117 -> (* u *)
4319 state.rects <- [];
4320 state.text <- "";
4321 G.postRedisplay "dehighlight";
4323 | 47 | 63 -> (* / ? *)
4324 let ondone isforw s =
4325 cbput state.hists.pat s;
4326 state.searchpattern <- s;
4327 search s isforw
4329 let s = String.create 1 in
4330 s.[0] <- Char.chr key;
4331 enttext (s, "", Some (onhist state.hists.pat),
4332 textentry, ondone (key = 47))
4334 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4335 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4336 setzoom (conf.zoom +. incr)
4338 | 43 | 0xffab -> (* + *)
4339 let ondone s =
4340 let n =
4341 try int_of_string s with exc ->
4342 state.text <- Printf.sprintf "bad integer `%s': %s"
4343 s (Printexc.to_string exc);
4344 max_int
4346 if n != max_int
4347 then (
4348 conf.pagebias <- n;
4349 state.text <- "page bias is now " ^ string_of_int n;
4352 enttext ("page bias: ", "", None, intentry, ondone)
4354 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4355 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4356 setzoom (max 0.01 (conf.zoom -. decr))
4358 | 45 | 0xffad -> (* - *)
4359 let ondone msg = state.text <- msg in
4360 enttext (
4361 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4362 optentry state.mode, ondone
4365 | 48 when ctrl -> (* ctrl-0 *)
4366 setzoom 1.0
4368 | 49 when ctrl -> (* 1 *)
4369 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4370 if zoom < 1.0
4371 then setzoom zoom
4373 | 0xffc6 -> (* f9 *)
4374 togglebirdseye ()
4376 | 57 when ctrl -> (* ctrl-9 *)
4377 togglebirdseye ()
4379 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4380 when not ctrl -> (* 0..9 *)
4381 let ondone s =
4382 let n =
4383 try int_of_string s with exc ->
4384 state.text <- Printf.sprintf "bad integer `%s': %s"
4385 s (Printexc.to_string exc);
4388 if n >= 0
4389 then (
4390 addnav ();
4391 cbput state.hists.pag (string_of_int n);
4392 gotopage1 (n + conf.pagebias - 1) 0;
4395 let pageentry text key =
4396 match Char.unsafe_chr key with
4397 | 'g' -> TEdone text
4398 | _ -> intentry text key
4400 let text = "x" in text.[0] <- Char.chr key;
4401 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4403 | 98 -> (* b *)
4404 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4405 reshape conf.winw conf.winh;
4407 | 108 -> (* l *)
4408 conf.hlinks <- not conf.hlinks;
4409 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4410 G.postRedisplay "toggle highlightlinks";
4412 | 97 -> (* a *)
4413 begin match state.autoscroll with
4414 | Some step ->
4415 conf.autoscrollstep <- step;
4416 state.autoscroll <- None
4417 | None ->
4418 if conf.autoscrollstep = 0
4419 then state.autoscroll <- Some 1
4420 else state.autoscroll <- Some conf.autoscrollstep
4423 | 80 -> (* P *)
4424 conf.presentation <- not conf.presentation;
4425 if conf.presentation
4426 then (
4427 if not conf.scrollbarinpm
4428 then state.scrollw <- 0;
4430 else
4431 state.scrollw <- conf.scrollbw;
4433 showtext ' ' ("presentation mode " ^
4434 if conf.presentation then "on" else "off");
4435 state.anchor <- getanchor ();
4436 represent ()
4438 | 102 -> (* f *)
4439 begin match state.fullscreen with
4440 | None ->
4441 state.fullscreen <- Some (conf.winw, conf.winh);
4442 Wsi.fullscreen ()
4443 | Some (w, h) ->
4444 state.fullscreen <- None;
4445 doreshape w h
4448 | 103 -> (* g *)
4449 gotoy_and_clear_text 0
4451 | 71 -> (* G *)
4452 gotopage1 (state.pagecount - 1) 0
4454 | 112 | 78 -> (* p|N *)
4455 search state.searchpattern false
4457 | 110 | 0xffc0 -> (* n|F3 *)
4458 search state.searchpattern true
4460 | 116 -> (* t *)
4461 begin match state.layout with
4462 | [] -> ()
4463 | l :: _ ->
4464 gotoy_and_clear_text (getpagey l.pageno)
4467 | 32 -> (* ' ' *)
4468 begin match List.rev state.layout with
4469 | [] -> ()
4470 | l :: _ ->
4471 let pageno = min (l.pageno+1) (state.pagecount-1) in
4472 gotoy_and_clear_text (getpagey pageno)
4475 | 0xff9f | 0xffff -> (* delete *)
4476 begin match state.layout with
4477 | [] -> ()
4478 | l :: _ ->
4479 let pageno = max 0 (l.pageno-1) in
4480 gotoy_and_clear_text (getpagey pageno)
4483 | 61 -> (* = *)
4484 showtext ' ' (describe_location ());
4486 | 119 -> (* w *)
4487 begin match state.layout with
4488 | [] -> ()
4489 | l :: _ ->
4490 doreshape (l.pagew + state.scrollw) l.pageh;
4491 G.postRedisplay "w"
4494 | 39 -> (* ' *)
4495 enterbookmarkmode ()
4497 | 104 | 0xffbe -> (* h|F1 *)
4498 enterhelpmode ()
4500 | 105 -> (* i *)
4501 enterinfomode ()
4503 | 101 when conf.redirectstderr -> (* e *)
4504 entermsgsmode ()
4506 | 109 -> (* m *)
4507 let ondone s =
4508 match state.layout with
4509 | l :: _ ->
4510 state.bookmarks <-
4511 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4512 :: state.bookmarks
4513 | _ -> ()
4515 enttext ("bookmark: ", "", None, textentry, ondone)
4517 | 126 -> (* ~ *)
4518 quickbookmark ();
4519 showtext ' ' "Quick bookmark added";
4521 | 122 -> (* z *)
4522 begin match state.layout with
4523 | l :: _ ->
4524 let rect = getpdimrect l.pagedimno in
4525 let w, h =
4526 if conf.crophack
4527 then
4528 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4529 truncate (1.2 *. (rect.(3) -. rect.(0))))
4530 else
4531 (truncate (rect.(1) -. rect.(0)),
4532 truncate (rect.(3) -. rect.(0)))
4534 let w = truncate ((float w)*.conf.zoom)
4535 and h = truncate ((float h)*.conf.zoom) in
4536 if w != 0 && h != 0
4537 then (
4538 state.anchor <- getanchor ();
4539 doreshape (w + state.scrollw) (h + conf.interpagespace)
4541 G.postRedisplay "z";
4543 | [] -> ()
4546 | 50 when ctrl -> (* ctrl-2 *)
4547 let maxw = getmaxw () in
4548 if maxw > 0.0
4549 then setzoom (maxw /. float conf.winw)
4551 | 60 | 62 -> (* < > *)
4552 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4554 | 91 | 93 -> (* [ ] *)
4555 conf.colorscale <-
4556 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4558 G.postRedisplay "brightness";
4560 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4561 setzoom state.prevzoom
4563 | 107 | 0xff52 -> (* k up *)
4564 begin match state.autoscroll with
4565 | None ->
4566 begin match state.mode with
4567 | Birdseye beye -> upbirdseye 1 beye
4568 | _ ->
4569 if ctrl
4570 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4571 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4573 | Some n ->
4574 setautoscrollspeed n false
4577 | 106 | 0xff54 -> (* j down *)
4578 begin match state.autoscroll with
4579 | None ->
4580 begin match state.mode with
4581 | Birdseye beye -> downbirdseye 1 beye
4582 | _ ->
4583 if ctrl
4584 then gotoy_and_clear_text (clamp (conf.winh/2))
4585 else gotoy_and_clear_text (clamp conf.scrollstep)
4587 | Some n ->
4588 setautoscrollspeed n true
4591 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4592 if conf.zoom > 1.0
4593 then
4594 let dx =
4595 if ctrl
4596 then conf.winw / 2
4597 else 10
4599 let dx = if key = 0xff51 then dx else -dx in
4600 state.x <- state.x + dx;
4601 gotoy_and_clear_text state.y
4602 else (
4603 state.text <- "";
4604 G.postRedisplay "lef/right"
4607 | 0xff55 -> (* prior *)
4608 let y =
4609 if ctrl
4610 then
4611 match state.layout with
4612 | [] -> state.y
4613 | l :: _ -> state.y - l.pagey
4614 else
4615 clamp (-conf.winh)
4617 gotoghyll y
4619 | 0xff56 -> (* next *)
4620 let y =
4621 if ctrl
4622 then
4623 match List.rev state.layout with
4624 | [] -> state.y
4625 | l :: _ -> getpagey l.pageno
4626 else
4627 clamp conf.winh
4629 gotoghyll y
4631 | 0xff50 -> gotoghyll 0
4632 | 0xff57 -> gotoghyll (clamp state.maxy)
4633 | 0xff53 when Wsi.withalt mask ->
4634 gotoghyll (getnav ~-1)
4635 | 0xff51 when Wsi.withalt mask ->
4636 gotoghyll (getnav 1)
4638 | 114 -> (* r *)
4639 state.anchor <- getanchor ();
4640 opendoc state.path state.password
4642 | 76 -> (* L *)
4643 launchpath ()
4645 | 118 when conf.debug -> (* v *)
4646 state.rects <- [];
4647 List.iter (fun l ->
4648 match getopaque l.pageno with
4649 | None -> ()
4650 | Some opaque ->
4651 let x0, y0, x1, y1 = pagebbox opaque in
4652 let a,b = float x0, float y0 in
4653 let c,d = float x1, float y0 in
4654 let e,f = float x1, float y1 in
4655 let h,j = float x0, float y1 in
4656 let rect = (a,b,c,d,e,f,h,j) in
4657 debugrect rect;
4658 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4659 ) state.layout;
4660 G.postRedisplay "v";
4662 | _ ->
4663 vlog "huh? %s" (Wsi.keyname key)
4666 let gotounder = function
4667 | Ulinkgoto (pageno, top) ->
4668 if pageno >= 0
4669 then (
4670 addnav ();
4671 gotopage1 pageno top;
4674 | Ulinkuri s ->
4675 gotouri s
4677 | Uremote (filename, pageno) ->
4678 let path =
4679 if Sys.file_exists filename
4680 then filename
4681 else
4682 let dir = Filename.dirname state.path in
4683 let path = Filename.concat dir filename in
4684 if Sys.file_exists path
4685 then path
4686 else ""
4688 if String.length path > 0
4689 then (
4690 let anchor = getanchor () in
4691 let ranchor = state.path, state.password, anchor in
4692 state.anchor <- (pageno, 0.0);
4693 state.ranchors <- ranchor :: state.ranchors;
4694 opendoc path "";
4696 else showtext '!' ("Could not find " ^ filename)
4698 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4701 let linknavkeyboard key mask linknav =
4702 let getpage pageno =
4703 let rec loop = function
4704 | [] -> None
4705 | l :: _ when l.pageno = pageno -> Some l
4706 | _ :: rest -> loop rest
4707 in loop state.layout
4709 let doexact (pageno, n) =
4710 match getopaque pageno, getpage pageno with
4711 | Some opaque, Some l ->
4712 if key = 0xff0d
4713 then
4714 let under = getlink opaque n in
4715 G.postRedisplay "link gotounder";
4716 gotounder under;
4717 state.mode <- View;
4718 else
4719 let opt, dir =
4720 match key with
4721 | 0xff50 -> (* home *)
4722 Some (findlink opaque LDfirst), -1
4724 | 0xff57 -> (* end *)
4725 Some (findlink opaque LDlast), 1
4727 | 0xff51 -> (* left *)
4728 Some (findlink opaque (LDleft n)), -1
4730 | 0xff53 -> (* right *)
4731 Some (findlink opaque (LDright n)), 1
4733 | 0xff52 -> (* up *)
4734 Some (findlink opaque (LDup n)), -1
4736 | 0xff54 -> (* down *)
4737 Some (findlink opaque (LDdown n)), 1
4739 | _ -> None, 0
4741 let pwl l dir =
4742 begin match findpwl l.pageno dir with
4743 | Pwlnotfound -> ()
4744 | Pwl pageno ->
4745 let notfound dir =
4746 state.mode <- LinkNav (Ltgendir dir);
4747 let y, h = getpageyh pageno in
4748 let y =
4749 if dir < 0
4750 then y + h - conf.winh
4751 else y
4753 gotoy y
4755 begin match getopaque pageno, getpage pageno with
4756 | Some opaque, Some _ ->
4757 let link =
4758 let ld = if dir > 0 then LDfirst else LDlast in
4759 findlink opaque ld
4761 begin match link with
4762 | Lfound (m, x0, y0, x1, y1) ->
4763 let r = x0, y0, x1, y1 in
4764 state.mode <- LinkNav (Ltexact ((pageno, m), r));
4765 G.postRedisplay "linknav jpage";
4766 | _ -> notfound dir
4767 end;
4768 | _ -> notfound dir
4769 end;
4770 end;
4772 begin match opt with
4773 | Some Lnotfound -> pwl l dir;
4774 | Some (Lfound (m, x0, y0, x1, y1)) ->
4775 if m = n
4776 then pwl l dir
4777 else (
4778 if y0 < l.pagey
4779 then gotopage1 l.pageno y0
4780 else (
4781 if y1 - l.pagey > l.pagevh
4782 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh)
4783 else G.postRedisplay "linknav";
4785 let r = x0, y0, x1, y1 in
4786 state.mode <- LinkNav (Ltexact ((l.pageno, m), r));
4789 | None ->
4790 state.mode <- LinkNav (Ltgendir 0);
4791 viewkeyboard key mask
4792 end;
4793 | _ -> viewkeyboard key mask
4795 if key = 0xff63
4796 then (
4797 state.mode <- View;
4798 G.postRedisplay "leave linknav"
4800 else
4801 match linknav with
4802 | Ltgendir _ -> viewkeyboard key mask
4803 | Ltexact (exact, _) -> doexact exact
4806 let keyboard key mask =
4807 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4808 then wcmd "interrupt"
4809 else state.uioh <- state.uioh#key key mask
4812 let birdseyekeyboard key mask
4813 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4814 let incr =
4815 match conf.columns with
4816 | None -> 1
4817 | Some ((c, _, _), _) -> c
4819 match key with
4820 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4821 let y, h = getpageyh pageno in
4822 let top = (conf.winh - h) / 2 in
4823 gotoy (max 0 (y - top))
4824 | 0xff0d -> leavebirdseye beye false
4825 | 0xff1b -> leavebirdseye beye true (* escape *)
4826 | 0xff52 -> upbirdseye incr beye (* prior *)
4827 | 0xff54 -> downbirdseye incr beye (* next *)
4828 | 0xff51 -> upbirdseye 1 beye (* up *)
4829 | 0xff53 -> downbirdseye 1 beye (* down *)
4831 | 0xff55 ->
4832 begin match state.layout with
4833 | l :: _ ->
4834 if l.pagey != 0
4835 then (
4836 state.mode <- Birdseye (
4837 oconf, leftx, l.pageno, hooverpageno, anchor
4839 gotopage1 l.pageno 0;
4841 else (
4842 let layout = layout (state.y-conf.winh) conf.winh in
4843 match layout with
4844 | [] -> gotoy (clamp (-conf.winh))
4845 | l :: _ ->
4846 state.mode <- Birdseye (
4847 oconf, leftx, l.pageno, hooverpageno, anchor
4849 gotopage1 l.pageno 0
4852 | [] -> gotoy (clamp (-conf.winh))
4853 end;
4855 | 0xff56 ->
4856 begin match List.rev state.layout with
4857 | l :: _ ->
4858 let layout = layout (state.y + conf.winh) conf.winh in
4859 begin match layout with
4860 | [] ->
4861 let incr = l.pageh - l.pagevh in
4862 if incr = 0
4863 then (
4864 state.mode <-
4865 Birdseye (
4866 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4868 G.postRedisplay "birdseye pagedown";
4870 else gotoy (clamp (incr + conf.interpagespace*2));
4872 | l :: _ ->
4873 state.mode <-
4874 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4875 gotopage1 l.pageno 0;
4878 | [] -> gotoy (clamp conf.winh)
4879 end;
4881 | 0xff50 ->
4882 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4883 gotopage1 0 0
4885 | 0xff57 ->
4886 let pageno = state.pagecount - 1 in
4887 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4888 if not (pagevisible state.layout pageno)
4889 then
4890 let h =
4891 match List.rev state.pdims with
4892 | [] -> conf.winh
4893 | (_, _, h, _) :: _ -> h
4895 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4896 else G.postRedisplay "birdseye end";
4897 | _ -> viewkeyboard key mask
4900 let drawpage l =
4901 let color =
4902 match state.mode with
4903 | Textentry _ -> scalecolor 0.4
4904 | LinkNav _
4905 | View -> scalecolor 1.0
4906 | Birdseye (_, _, pageno, hooverpageno, _) ->
4907 if l.pageno = hooverpageno
4908 then scalecolor 0.9
4909 else (
4910 if l.pageno = pageno
4911 then scalecolor 1.0
4912 else scalecolor 0.8
4915 drawtiles l color;
4916 begin match getopaque l.pageno with
4917 | Some opaque ->
4918 if tileready l l.pagex l.pagey
4919 then
4920 let x = l.pagedispx - l.pagex
4921 and y = l.pagedispy - l.pagey in
4922 postprocess opaque conf.hlinks x y;
4924 | _ -> ()
4925 end;
4928 let scrollindicator () =
4929 let sbw, ph, sh = state.uioh#scrollph in
4930 let sbh, pw, sw = state.uioh#scrollpw in
4932 GlDraw.color (0.64, 0.64, 0.64);
4933 GlDraw.rect
4934 (float (conf.winw - sbw), 0.)
4935 (float conf.winw, float conf.winh)
4937 GlDraw.rect
4938 (0., float (conf.winh - sbh))
4939 (float (conf.winw - state.scrollw - 1), float conf.winh)
4941 GlDraw.color (0.0, 0.0, 0.0);
4943 GlDraw.rect
4944 (float (conf.winw - sbw), ph)
4945 (float conf.winw, ph +. sh)
4947 GlDraw.rect
4948 (pw, float (conf.winh - sbh))
4949 (pw +. sw, float conf.winh)
4953 let showsel () =
4954 match state.mstate with
4955 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4958 | Msel ((x0, y0), (x1, y1)) ->
4959 let rec loop = function
4960 | l :: ls ->
4961 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4962 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4963 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4964 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4965 then
4966 match getopaque l.pageno with
4967 | Some opaque ->
4968 let x0, y0 = pagetranslatepoint l x0 y0 in
4969 let x1, y1 = pagetranslatepoint l x1 y1 in
4970 seltext opaque (x0, y0, x1, y1);
4971 | _ -> ()
4972 else loop ls
4973 | [] -> ()
4975 loop state.layout
4978 let showrects rects =
4979 Gl.enable `blend;
4980 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4981 GlDraw.polygon_mode `both `fill;
4982 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4983 List.iter
4984 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4985 List.iter (fun l ->
4986 if l.pageno = pageno
4987 then (
4988 let dx = float (l.pagedispx - l.pagex) in
4989 let dy = float (l.pagedispy - l.pagey) in
4990 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4991 GlDraw.begins `quads;
4993 GlDraw.vertex2 (x0+.dx, y0+.dy);
4994 GlDraw.vertex2 (x1+.dx, y1+.dy);
4995 GlDraw.vertex2 (x2+.dx, y2+.dy);
4996 GlDraw.vertex2 (x3+.dx, y3+.dy);
4998 GlDraw.ends ();
5000 ) state.layout
5001 ) rects
5003 Gl.disable `blend;
5006 let display () =
5007 GlClear.color (scalecolor2 conf.bgcolor);
5008 GlClear.clear [`color];
5009 List.iter drawpage state.layout;
5010 let rects =
5011 match state.mode with
5012 | LinkNav (Ltexact ((pageno, _), (x0, y0, x1, y1))) ->
5013 (pageno, 5, (
5014 float x0, float y0,
5015 float x1, float y0,
5016 float x1, float y1,
5017 float x0, float y1)
5018 ) :: state.rects
5019 | _ -> state.rects
5021 showrects rects;
5022 showsel ();
5023 state.uioh#display;
5024 begin match state.mstate with
5025 | Mzoomrect ((x0, y0), (x1, y1)) ->
5026 Gl.enable `blend;
5027 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5028 GlDraw.polygon_mode `both `fill;
5029 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5030 GlDraw.rect (float x0, float y0)
5031 (float x1, float y1);
5032 Gl.disable `blend;
5033 | _ -> ()
5034 end;
5035 enttext ();
5036 scrollindicator ();
5037 if conf.updatecurs
5038 then (
5039 let mx, my = state.mpos in
5040 updateunder mx my;
5042 Wsi.swapb ();
5045 let display () =
5046 if nogeomcmds state.geomcmds
5047 then display ()
5048 else (
5049 GlFunc.draw_buffer `front;
5050 GlClear.color (scalecolor2 conf.bgcolor);
5051 GlClear.clear [`color];
5052 GlFunc.draw_buffer `back;
5056 let zoomrect x y x1 y1 =
5057 let x0 = min x x1
5058 and x1 = max x x1
5059 and y0 = min y y1 in
5060 gotoy (state.y + y0);
5061 state.anchor <- getanchor ();
5062 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5063 let margin =
5064 if state.w < conf.winw - state.scrollw
5065 then (conf.winw - state.scrollw - state.w) / 2
5066 else 0
5068 state.x <- (state.x + margin) - x0;
5069 setzoom zoom;
5070 Wsi.setcursor Wsi.CURSOR_INHERIT;
5071 state.mstate <- Mnone;
5074 let scrollx x =
5075 let winw = conf.winw - state.scrollw - 1 in
5076 let s = float x /. float winw in
5077 let destx = truncate (float (state.w + winw) *. s) in
5078 state.x <- winw - destx;
5079 gotoy_and_clear_text state.y;
5080 state.mstate <- Mscrollx;
5083 let scrolly y =
5084 let s = float y /. float conf.winh in
5085 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5086 gotoy_and_clear_text desty;
5087 state.mstate <- Mscrolly;
5090 let viewmouse button down x y mask =
5091 match button with
5092 | n when (n == 4 || n == 5) && not down ->
5093 if Wsi.withctrl mask
5094 then (
5095 match state.mstate with
5096 | Mzoom (oldn, i) ->
5097 if oldn = n
5098 then (
5099 if i = 2
5100 then
5101 let incr =
5102 match n with
5103 | 5 ->
5104 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5105 | _ ->
5106 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5108 let zoom = conf.zoom -. incr in
5109 setzoom zoom;
5110 state.mstate <- Mzoom (n, 0);
5111 else
5112 state.mstate <- Mzoom (n, i+1);
5114 else state.mstate <- Mzoom (n, 0)
5116 | _ -> state.mstate <- Mzoom (n, 0)
5118 else (
5119 match state.autoscroll with
5120 | Some step -> setautoscrollspeed step (n=4)
5121 | None ->
5122 let incr =
5123 if n = 4
5124 then -conf.scrollstep
5125 else conf.scrollstep
5127 let incr = incr * 2 in
5128 let y = clamp incr in
5129 gotoy_and_clear_text y
5132 | 1 when Wsi.withctrl mask ->
5133 if down
5134 then (
5135 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5136 state.mstate <- Mpan (x, y)
5138 else
5139 state.mstate <- Mnone
5141 | 3 ->
5142 if down
5143 then (
5144 Wsi.setcursor Wsi.CURSOR_CYCLE;
5145 let p = (x, y) in
5146 state.mstate <- Mzoomrect (p, p)
5148 else (
5149 match state.mstate with
5150 | Mzoomrect ((x0, y0), _) ->
5151 if abs (x-x0) > 10 && abs (y - y0) > 10
5152 then zoomrect x0 y0 x y
5153 else (
5154 state.mstate <- Mnone;
5155 Wsi.setcursor Wsi.CURSOR_INHERIT;
5156 G.postRedisplay "kill accidental zoom rect";
5158 | _ ->
5159 Wsi.setcursor Wsi.CURSOR_INHERIT;
5160 state.mstate <- Mnone
5163 | 1 when x > conf.winw - state.scrollw ->
5164 if down
5165 then
5166 let _, position, sh = state.uioh#scrollph in
5167 if y > truncate position && y < truncate (position +. sh)
5168 then state.mstate <- Mscrolly
5169 else scrolly y
5170 else
5171 state.mstate <- Mnone
5173 | 1 when y > conf.winh - state.hscrollh ->
5174 if down
5175 then
5176 let _, position, sw = state.uioh#scrollpw in
5177 if x > truncate position && x < truncate (position +. sw)
5178 then state.mstate <- Mscrollx
5179 else scrollx x
5180 else
5181 state.mstate <- Mnone
5183 | 1 ->
5184 let dest = if down then getunder x y else Unone in
5185 begin match dest with
5186 | Ulinkgoto (pageno, top) ->
5187 if pageno >= 0
5188 then (
5189 addnav ();
5190 gotopage1 pageno top;
5193 | Ulinkuri s ->
5194 gotouri s
5196 | Uremote (filename, pageno) ->
5197 let path =
5198 if Sys.file_exists filename
5199 then filename
5200 else
5201 let dir = Filename.dirname state.path in
5202 let path = Filename.concat dir filename in
5203 if Sys.file_exists path
5204 then path
5205 else ""
5207 if String.length path > 0
5208 then (
5209 let anchor = getanchor () in
5210 let ranchor = state.path, state.password, anchor in
5211 state.anchor <- (pageno, 0.0);
5212 state.ranchors <- ranchor :: state.ranchors;
5213 opendoc path "";
5215 else showtext '!' ("Could not find " ^ filename)
5217 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
5219 | Unone when down ->
5220 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5221 state.mstate <- Mpan (x, y);
5223 | Unone | Utext _ ->
5224 if down
5225 then (
5226 if conf.angle mod 360 = 0
5227 then (
5228 state.mstate <- Msel ((x, y), (x, y));
5229 G.postRedisplay "mouse select";
5232 else (
5233 match state.mstate with
5234 | Mnone -> ()
5236 | Mzoom _ | Mscrollx | Mscrolly ->
5237 state.mstate <- Mnone
5239 | Mzoomrect ((x0, y0), _) ->
5240 zoomrect x0 y0 x y
5242 | Mpan _ ->
5243 Wsi.setcursor Wsi.CURSOR_INHERIT;
5244 state.mstate <- Mnone
5246 | Msel ((_, y0), (_, y1)) ->
5247 let rec loop = function
5248 | [] -> ()
5249 | l :: rest ->
5250 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5251 || ((y1 >= l.pagedispy
5252 && y1 <= (l.pagedispy + l.pagevh)))
5253 then
5254 match getopaque l.pageno with
5255 | Some opaque ->
5256 copysel conf.selcmd opaque;
5257 G.postRedisplay "copysel"
5258 | _ -> ()
5259 else loop rest
5261 loop state.layout;
5262 Wsi.setcursor Wsi.CURSOR_INHERIT;
5263 state.mstate <- Mnone;
5267 | _ -> ()
5270 let birdseyemouse button down x y mask
5271 (conf, leftx, _, hooverpageno, anchor) =
5272 match button with
5273 | 1 when down ->
5274 let rec loop = function
5275 | [] -> ()
5276 | l :: rest ->
5277 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5278 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5279 then (
5280 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5282 else loop rest
5284 loop state.layout
5285 | 3 -> ()
5286 | _ -> viewmouse button down x y mask
5289 let mouse button down x y mask =
5290 state.uioh <- state.uioh#button button down x y mask;
5293 let motion ~x ~y =
5294 state.uioh <- state.uioh#motion x y
5297 let pmotion ~x ~y =
5298 state.uioh <- state.uioh#pmotion x y;
5301 let uioh = object
5302 method display = ()
5304 method key key mask =
5305 begin match state.mode with
5306 | Textentry textentry -> textentrykeyboard key mask textentry
5307 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5308 | View -> viewkeyboard key mask
5309 | LinkNav linknav -> linknavkeyboard key mask linknav
5310 end;
5311 state.uioh
5313 method button button bstate x y mask =
5314 begin match state.mode with
5315 | LinkNav _
5316 | View -> viewmouse button bstate x y mask
5317 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5318 | Textentry _ -> ()
5319 end;
5320 state.uioh
5322 method motion x y =
5323 begin match state.mode with
5324 | Textentry _ -> ()
5325 | View | Birdseye _ | LinkNav _ ->
5326 match state.mstate with
5327 | Mzoom _ | Mnone -> ()
5329 | Mpan (x0, y0) ->
5330 let dx = x - x0
5331 and dy = y0 - y in
5332 state.mstate <- Mpan (x, y);
5333 if conf.zoom > 1.0 then state.x <- state.x + dx;
5334 let y = clamp dy in
5335 gotoy_and_clear_text y
5337 | Msel (a, _) ->
5338 state.mstate <- Msel (a, (x, y));
5339 G.postRedisplay "motion select";
5341 | Mscrolly ->
5342 let y = min conf.winh (max 0 y) in
5343 scrolly y
5345 | Mscrollx ->
5346 let x = min conf.winw (max 0 x) in
5347 scrollx x
5349 | Mzoomrect (p0, _) ->
5350 state.mstate <- Mzoomrect (p0, (x, y));
5351 G.postRedisplay "motion zoomrect";
5352 end;
5353 state.uioh
5355 method pmotion x y =
5356 begin match state.mode with
5357 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5358 let rec loop = function
5359 | [] ->
5360 if hooverpageno != -1
5361 then (
5362 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5363 G.postRedisplay "pmotion birdseye no hoover";
5365 | l :: rest ->
5366 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5367 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5368 then (
5369 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5370 G.postRedisplay "pmotion birdseye hoover";
5372 else loop rest
5374 loop state.layout
5376 | Textentry _ -> ()
5378 | LinkNav _
5379 | View ->
5380 match state.mstate with
5381 | Mnone -> updateunder x y
5382 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5384 end;
5385 state.uioh
5387 method infochanged _ = ()
5389 method scrollph =
5390 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5391 let p, h = scrollph state.y maxy in
5392 state.scrollw, p, h
5394 method scrollpw =
5395 let winw = conf.winw - state.scrollw - 1 in
5396 let fwinw = float winw in
5397 let sw =
5398 let sw = fwinw /. float state.w in
5399 let sw = fwinw *. sw in
5400 max sw (float conf.scrollh)
5402 let position, sw =
5403 let f = state.w+winw in
5404 let r = float (winw-state.x) /. float f in
5405 let p = fwinw *. r in
5406 p-.sw/.2., sw
5408 let sw =
5409 if position +. sw > fwinw
5410 then fwinw -. position
5411 else sw
5413 state.hscrollh, position, sw
5415 method modehash =
5416 let modename =
5417 match state.mode with
5418 | LinkNav _ -> "links"
5419 | Textentry _ -> "textentry"
5420 | Birdseye _ -> "birdseye"
5421 | View -> "global"
5423 findkeyhash conf modename
5424 end;;
5426 module Config =
5427 struct
5428 open Parser
5430 let fontpath = ref "";;
5432 module KeyMap =
5433 Map.Make (struct type t = (int * int) let compare = compare end);;
5435 let unent s =
5436 let l = String.length s in
5437 let b = Buffer.create l in
5438 unent b s 0 l;
5439 Buffer.contents b;
5442 let home =
5443 try Sys.getenv "HOME"
5444 with exn ->
5445 prerr_endline
5446 ("Can not determine home directory location: " ^
5447 Printexc.to_string exn);
5451 let modifier_of_string = function
5452 | "alt" -> Wsi.altmask
5453 | "shift" -> Wsi.shiftmask
5454 | "ctrl" | "control" -> Wsi.ctrlmask
5455 | "meta" -> Wsi.metamask
5456 | _ -> 0
5459 let key_of_string =
5460 let r = Str.regexp "-" in
5461 fun s ->
5462 let elems = Str.full_split r s in
5463 let f n k m =
5464 let g s =
5465 let m1 = modifier_of_string s in
5466 if m1 = 0
5467 then (Wsi.namekey s, m)
5468 else (k, m lor m1)
5469 in function
5470 | Str.Delim s when n land 1 = 0 -> g s
5471 | Str.Text s -> g s
5472 | Str.Delim _ -> (k, m)
5474 let rec loop n k m = function
5475 | [] -> (k, m)
5476 | x :: xs ->
5477 let k, m = f n k m x in
5478 loop (n+1) k m xs
5480 loop 0 0 0 elems
5483 let keys_of_string =
5484 let r = Str.regexp "[ \t]" in
5485 fun s ->
5486 let elems = Str.split r s in
5487 List.map key_of_string elems
5490 let copykeyhashes c =
5491 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5494 let config_of c attrs =
5495 let apply c k v =
5497 match k with
5498 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5499 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5500 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5501 | "preload" -> { c with preload = bool_of_string v }
5502 | "page-bias" -> { c with pagebias = int_of_string v }
5503 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5504 | "auto-scroll-step" ->
5505 { c with autoscrollstep = max 0 (int_of_string v) }
5506 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5507 | "crop-hack" -> { c with crophack = bool_of_string v }
5508 | "throttle" ->
5509 let mw =
5510 match String.lowercase v with
5511 | "true" -> Some infinity
5512 | "false" -> None
5513 | f -> Some (float_of_string f)
5515 { c with maxwait = mw}
5516 | "highlight-links" -> { c with hlinks = bool_of_string v }
5517 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5518 | "vertical-margin" ->
5519 { c with interpagespace = max 0 (int_of_string v) }
5520 | "zoom" ->
5521 let zoom = float_of_string v /. 100. in
5522 let zoom = max zoom 0.0 in
5523 { c with zoom = zoom }
5524 | "presentation" -> { c with presentation = bool_of_string v }
5525 | "rotation-angle" -> { c with angle = int_of_string v }
5526 | "width" -> { c with winw = max 20 (int_of_string v) }
5527 | "height" -> { c with winh = max 20 (int_of_string v) }
5528 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5529 | "proportional-display" -> { c with proportional = bool_of_string v }
5530 | "pixmap-cache-size" ->
5531 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5532 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5533 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5534 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5535 | "persistent-location" -> { c with jumpback = bool_of_string v }
5536 | "background-color" -> { c with bgcolor = color_of_string v }
5537 | "scrollbar-in-presentation" ->
5538 { c with scrollbarinpm = bool_of_string v }
5539 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5540 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5541 | "mupdf-store-size" ->
5542 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5543 | "checkers" -> { c with checkers = bool_of_string v }
5544 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5545 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5546 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5547 | "uri-launcher" -> { c with urilauncher = unent v }
5548 | "path-launcher" -> { c with pathlauncher = unent v }
5549 | "color-space" -> { c with colorspace = colorspace_of_string v }
5550 | "invert-colors" -> { c with invert = bool_of_string v }
5551 | "brightness" -> { c with colorscale = float_of_string v }
5552 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5553 | "ghyllscroll" ->
5554 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5555 | "columns" ->
5556 let nab = columns_of_string v in
5557 { c with columns = Some (nab, [||]) }
5558 | "birds-eye-columns" ->
5559 { c with beyecolumns = Some (max (int_of_string v) 2) }
5560 | "selection-command" -> { c with selcmd = unent v }
5561 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5562 | _ -> c
5563 with exn ->
5564 prerr_endline ("Error processing attribute (`" ^
5565 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5568 let rec fold c = function
5569 | [] -> c
5570 | (k, v) :: rest ->
5571 let c = apply c k v in
5572 fold c rest
5574 fold { c with keyhashes = copykeyhashes c } attrs;
5577 let fromstring f pos n v d =
5578 try f v
5579 with exn ->
5580 dolog "Error processing attribute (%S=%S) at %d\n%s"
5581 n v pos (Printexc.to_string exn)
5586 let bookmark_of attrs =
5587 let rec fold title page rely = function
5588 | ("title", v) :: rest -> fold v page rely rest
5589 | ("page", v) :: rest -> fold title v rely rest
5590 | ("rely", v) :: rest -> fold title page v rest
5591 | _ :: rest -> fold title page rely rest
5592 | [] -> title, page, rely
5594 fold "invalid" "0" "0" attrs
5597 let doc_of attrs =
5598 let rec fold path page rely pan = function
5599 | ("path", v) :: rest -> fold v page rely pan rest
5600 | ("page", v) :: rest -> fold path v rely pan rest
5601 | ("rely", v) :: rest -> fold path page v pan rest
5602 | ("pan", v) :: rest -> fold path page rely v rest
5603 | _ :: rest -> fold path page rely pan rest
5604 | [] -> path, page, rely, pan
5606 fold "" "0" "0" "0" attrs
5609 let map_of attrs =
5610 let rec fold rs ls = function
5611 | ("out", v) :: rest -> fold v ls rest
5612 | ("in", v) :: rest -> fold rs v rest
5613 | _ :: rest -> fold ls rs rest
5614 | [] -> ls, rs
5616 fold "" "" attrs
5619 let setconf dst src =
5620 dst.scrollbw <- src.scrollbw;
5621 dst.scrollh <- src.scrollh;
5622 dst.icase <- src.icase;
5623 dst.preload <- src.preload;
5624 dst.pagebias <- src.pagebias;
5625 dst.verbose <- src.verbose;
5626 dst.scrollstep <- src.scrollstep;
5627 dst.maxhfit <- src.maxhfit;
5628 dst.crophack <- src.crophack;
5629 dst.autoscrollstep <- src.autoscrollstep;
5630 dst.maxwait <- src.maxwait;
5631 dst.hlinks <- src.hlinks;
5632 dst.underinfo <- src.underinfo;
5633 dst.interpagespace <- src.interpagespace;
5634 dst.zoom <- src.zoom;
5635 dst.presentation <- src.presentation;
5636 dst.angle <- src.angle;
5637 dst.winw <- src.winw;
5638 dst.winh <- src.winh;
5639 dst.savebmarks <- src.savebmarks;
5640 dst.memlimit <- src.memlimit;
5641 dst.proportional <- src.proportional;
5642 dst.texcount <- src.texcount;
5643 dst.sliceheight <- src.sliceheight;
5644 dst.thumbw <- src.thumbw;
5645 dst.jumpback <- src.jumpback;
5646 dst.bgcolor <- src.bgcolor;
5647 dst.scrollbarinpm <- src.scrollbarinpm;
5648 dst.tilew <- src.tilew;
5649 dst.tileh <- src.tileh;
5650 dst.mustoresize <- src.mustoresize;
5651 dst.checkers <- src.checkers;
5652 dst.aalevel <- src.aalevel;
5653 dst.trimmargins <- src.trimmargins;
5654 dst.trimfuzz <- src.trimfuzz;
5655 dst.urilauncher <- src.urilauncher;
5656 dst.colorspace <- src.colorspace;
5657 dst.invert <- src.invert;
5658 dst.colorscale <- src.colorscale;
5659 dst.redirectstderr <- src.redirectstderr;
5660 dst.ghyllscroll <- src.ghyllscroll;
5661 dst.columns <- src.columns;
5662 dst.beyecolumns <- src.beyecolumns;
5663 dst.selcmd <- src.selcmd;
5664 dst.updatecurs <- src.updatecurs;
5665 dst.pathlauncher <- src.pathlauncher;
5666 dst.keyhashes <- copykeyhashes src;
5669 let get s =
5670 let h = Hashtbl.create 10 in
5671 let dc = { defconf with angle = defconf.angle } in
5672 let rec toplevel v t spos _ =
5673 match t with
5674 | Vdata | Vcdata | Vend -> v
5675 | Vopen ("llppconfig", _, closed) ->
5676 if closed
5677 then v
5678 else { v with f = llppconfig }
5679 | Vopen _ ->
5680 error "unexpected subelement at top level" s spos
5681 | Vclose _ -> error "unexpected close at top level" s spos
5683 and llppconfig v t spos _ =
5684 match t with
5685 | Vdata | Vcdata -> v
5686 | Vend -> error "unexpected end of input in llppconfig" s spos
5687 | Vopen ("defaults", attrs, closed) ->
5688 let c = config_of dc attrs in
5689 setconf dc c;
5690 if closed
5691 then v
5692 else { v with f = defaults }
5694 | Vopen ("ui-font", attrs, closed) ->
5695 let rec getsize size = function
5696 | [] -> size
5697 | ("size", v) :: rest ->
5698 let size =
5699 fromstring int_of_string spos "size" v fstate.fontsize in
5700 getsize size rest
5701 | l -> getsize size l
5703 fstate.fontsize <- getsize fstate.fontsize attrs;
5704 if closed
5705 then v
5706 else { v with f = uifont (Buffer.create 10) }
5708 | Vopen ("doc", attrs, closed) ->
5709 let pathent, spage, srely, span = doc_of attrs in
5710 let path = unent pathent
5711 and pageno = fromstring int_of_string spos "page" spage 0
5712 and rely = fromstring float_of_string spos "rely" srely 0.0
5713 and pan = fromstring int_of_string spos "pan" span 0 in
5714 let c = config_of dc attrs in
5715 let anchor = (pageno, rely) in
5716 if closed
5717 then (Hashtbl.add h path (c, [], pan, anchor); v)
5718 else { v with f = doc path pan anchor c [] }
5720 | Vopen _ ->
5721 error "unexpected subelement in llppconfig" s spos
5723 | Vclose "llppconfig" -> { v with f = toplevel }
5724 | Vclose _ -> error "unexpected close in llppconfig" s spos
5726 and defaults v t spos _ =
5727 match t with
5728 | Vdata | Vcdata -> v
5729 | Vend -> error "unexpected end of input in defaults" s spos
5730 | Vopen ("keymap", attrs, closed) ->
5731 let modename =
5732 try List.assoc "mode" attrs
5733 with Not_found -> "global" in
5734 if closed
5735 then v
5736 else
5737 let ret keymap =
5738 let h = findkeyhash dc modename in
5739 KeyMap.iter (Hashtbl.replace h) keymap;
5740 defaults
5742 { v with f = pkeymap ret KeyMap.empty }
5744 | Vopen (_, _, _) ->
5745 error "unexpected subelement in defaults" s spos
5747 | Vclose "defaults" ->
5748 { v with f = llppconfig }
5750 | Vclose _ -> error "unexpected close in defaults" s spos
5752 and uifont b v t spos epos =
5753 match t with
5754 | Vdata | Vcdata ->
5755 Buffer.add_substring b s spos (epos - spos);
5757 | Vopen (_, _, _) ->
5758 error "unexpected subelement in ui-font" s spos
5759 | Vclose "ui-font" ->
5760 if String.length !fontpath = 0
5761 then fontpath := Buffer.contents b;
5762 { v with f = llppconfig }
5763 | Vclose _ -> error "unexpected close in ui-font" s spos
5764 | Vend -> error "unexpected end of input in ui-font" s spos
5766 and doc path pan anchor c bookmarks v t spos _ =
5767 match t with
5768 | Vdata | Vcdata -> v
5769 | Vend -> error "unexpected end of input in doc" s spos
5770 | Vopen ("bookmarks", _, closed) ->
5771 if closed
5772 then v
5773 else { v with f = pbookmarks path pan anchor c bookmarks }
5775 | Vopen ("keymap", attrs, closed) ->
5776 let modename =
5777 try List.assoc "mode" attrs
5778 with Not_found -> "global"
5780 if closed
5781 then v
5782 else
5783 let ret keymap =
5784 let h = findkeyhash c modename in
5785 KeyMap.iter (Hashtbl.replace h) keymap;
5786 doc path pan anchor c bookmarks
5788 { v with f = pkeymap ret KeyMap.empty }
5790 | Vopen (_, _, _) ->
5791 error "unexpected subelement in doc" s spos
5793 | Vclose "doc" ->
5794 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5795 { v with f = llppconfig }
5797 | Vclose _ -> error "unexpected close in doc" s spos
5799 and pkeymap ret keymap v t spos _ =
5800 match t with
5801 | Vdata | Vcdata -> v
5802 | Vend -> error "unexpected end of input in keymap" s spos
5803 | Vopen ("map", attrs, closed) ->
5804 let r, l = map_of attrs in
5805 let kss = fromstring keys_of_string spos "in" r [] in
5806 let lss = fromstring keys_of_string spos "out" l [] in
5807 let keymap =
5808 match kss with
5809 | [] -> keymap
5810 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5811 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5813 if closed
5814 then { v with f = pkeymap ret keymap }
5815 else
5816 let f () = v in
5817 { v with f = skip "map" f }
5819 | Vopen _ ->
5820 error "unexpected subelement in keymap" s spos
5822 | Vclose "keymap" ->
5823 { v with f = ret keymap }
5825 | Vclose _ -> error "unexpected close in keymap" s spos
5827 and pbookmarks path pan anchor c bookmarks v t spos _ =
5828 match t with
5829 | Vdata | Vcdata -> v
5830 | Vend -> error "unexpected end of input in bookmarks" s spos
5831 | Vopen ("item", attrs, closed) ->
5832 let titleent, spage, srely = bookmark_of attrs in
5833 let page = fromstring int_of_string spos "page" spage 0
5834 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5835 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5836 if closed
5837 then { v with f = pbookmarks path pan anchor c bookmarks }
5838 else
5839 let f () = v in
5840 { v with f = skip "item" f }
5842 | Vopen _ ->
5843 error "unexpected subelement in bookmarks" s spos
5845 | Vclose "bookmarks" ->
5846 { v with f = doc path pan anchor c bookmarks }
5848 | Vclose _ -> error "unexpected close in bookmarks" s spos
5850 and skip tag f v t spos _ =
5851 match t with
5852 | Vdata | Vcdata -> v
5853 | Vend ->
5854 error ("unexpected end of input in skipped " ^ tag) s spos
5855 | Vopen (tag', _, closed) ->
5856 if closed
5857 then v
5858 else
5859 let f' () = { v with f = skip tag f } in
5860 { v with f = skip tag' f' }
5861 | Vclose ctag ->
5862 if tag = ctag
5863 then f ()
5864 else error ("unexpected close in skipped " ^ tag) s spos
5867 parse { f = toplevel; accu = () } s;
5868 h, dc;
5871 let do_load f ic =
5873 let len = in_channel_length ic in
5874 let s = String.create len in
5875 really_input ic s 0 len;
5876 f s;
5877 with
5878 | Parse_error (msg, s, pos) ->
5879 let subs = subs s pos in
5880 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5881 failwith ("parse error: " ^ s)
5883 | exn ->
5884 failwith ("config load error: " ^ Printexc.to_string exn)
5887 let defconfpath =
5888 let dir =
5890 let dir = Filename.concat home ".config" in
5891 if Sys.is_directory dir then dir else home
5892 with _ -> home
5894 Filename.concat dir "llpp.conf"
5897 let confpath = ref defconfpath;;
5899 let load1 f =
5900 if Sys.file_exists !confpath
5901 then
5902 match
5903 (try Some (open_in_bin !confpath)
5904 with exn ->
5905 prerr_endline
5906 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5907 Printexc.to_string exn);
5908 None
5910 with
5911 | Some ic ->
5912 begin try
5913 f (do_load get ic)
5914 with exn ->
5915 prerr_endline
5916 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5917 Printexc.to_string exn);
5918 end;
5919 close_in ic;
5921 | None -> ()
5922 else
5923 f (Hashtbl.create 0, defconf)
5926 let load () =
5927 let f (h, dc) =
5928 let pc, pb, px, pa =
5930 Hashtbl.find h (Filename.basename state.path)
5931 with Not_found -> dc, [], 0, (0, 0.0)
5933 setconf defconf dc;
5934 setconf conf pc;
5935 state.bookmarks <- pb;
5936 state.x <- px;
5937 state.scrollw <- conf.scrollbw;
5938 if conf.jumpback
5939 then state.anchor <- pa;
5940 cbput state.hists.nav pa;
5942 load1 f
5945 let add_attrs bb always dc c =
5946 let ob s a b =
5947 if always || a != b
5948 then Printf.bprintf bb "\n %s='%b'" s a
5949 and oi s a b =
5950 if always || a != b
5951 then Printf.bprintf bb "\n %s='%d'" s a
5952 and oI s a b =
5953 if always || a != b
5954 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5955 and oz s a b =
5956 if always || a <> b
5957 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5958 and oF s a b =
5959 if always || a <> b
5960 then Printf.bprintf bb "\n %s='%f'" s a
5961 and oc s a b =
5962 if always || a <> b
5963 then
5964 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5965 and oC s a b =
5966 if always || a <> b
5967 then
5968 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5969 and oR s a b =
5970 if always || a <> b
5971 then
5972 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5973 and os s a b =
5974 if always || a <> b
5975 then
5976 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5977 and og s a b =
5978 if always || a <> b
5979 then
5980 match a with
5981 | None -> ()
5982 | Some (_N, _A, _B) ->
5983 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5984 and oW s a b =
5985 if always || a <> b
5986 then
5987 let v =
5988 match a with
5989 | None -> "false"
5990 | Some f ->
5991 if f = infinity
5992 then "true"
5993 else string_of_float f
5995 Printf.bprintf bb "\n %s='%s'" s v
5996 and oco s a b =
5997 if always || a <> b
5998 then
5999 match a with
6000 | Some ((n, a, b), _) when n > 1 ->
6001 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6002 | _ -> ()
6003 and obeco s a b =
6004 if always || a <> b
6005 then
6006 match a with
6007 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6008 | _ -> ()
6010 let w, h =
6011 if always
6012 then dc.winw, dc.winh
6013 else
6014 match state.fullscreen with
6015 | Some wh -> wh
6016 | None -> c.winw, c.winh
6018 let zoom, presentation, interpagespace, maxwait =
6019 if always
6020 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6021 else
6022 match state.mode with
6023 | Birdseye (bc, _, _, _, _) ->
6024 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6025 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6027 oi "width" w dc.winw;
6028 oi "height" h dc.winh;
6029 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6030 oi "scroll-handle-height" c.scrollh dc.scrollh;
6031 ob "case-insensitive-search" c.icase dc.icase;
6032 ob "preload" c.preload dc.preload;
6033 oi "page-bias" c.pagebias dc.pagebias;
6034 oi "scroll-step" c.scrollstep dc.scrollstep;
6035 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6036 ob "max-height-fit" c.maxhfit dc.maxhfit;
6037 ob "crop-hack" c.crophack dc.crophack;
6038 oW "throttle" maxwait dc.maxwait;
6039 ob "highlight-links" c.hlinks dc.hlinks;
6040 ob "under-cursor-info" c.underinfo dc.underinfo;
6041 oi "vertical-margin" interpagespace dc.interpagespace;
6042 oz "zoom" zoom dc.zoom;
6043 ob "presentation" presentation dc.presentation;
6044 oi "rotation-angle" c.angle dc.angle;
6045 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6046 ob "proportional-display" c.proportional dc.proportional;
6047 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6048 oi "tex-count" c.texcount dc.texcount;
6049 oi "slice-height" c.sliceheight dc.sliceheight;
6050 oi "thumbnail-width" c.thumbw dc.thumbw;
6051 ob "persistent-location" c.jumpback dc.jumpback;
6052 oc "background-color" c.bgcolor dc.bgcolor;
6053 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6054 oi "tile-width" c.tilew dc.tilew;
6055 oi "tile-height" c.tileh dc.tileh;
6056 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6057 ob "checkers" c.checkers dc.checkers;
6058 oi "aalevel" c.aalevel dc.aalevel;
6059 ob "trim-margins" c.trimmargins dc.trimmargins;
6060 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6061 os "uri-launcher" c.urilauncher dc.urilauncher;
6062 os "path-launcher" c.pathlauncher dc.pathlauncher;
6063 oC "color-space" c.colorspace dc.colorspace;
6064 ob "invert-colors" c.invert dc.invert;
6065 oF "brightness" c.colorscale dc.colorscale;
6066 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6067 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6068 oco "columns" c.columns dc.columns;
6069 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6070 os "selection-command" c.selcmd dc.selcmd;
6071 ob "update-cursor" c.updatecurs dc.updatecurs;
6074 let keymapsbuf always dc c =
6075 let bb = Buffer.create 16 in
6076 let rec loop = function
6077 | [] -> ()
6078 | (modename, h) :: rest ->
6079 let dh = findkeyhash dc modename in
6080 if always || h <> dh
6081 then (
6082 if Hashtbl.length h > 0
6083 then (
6084 if Buffer.length bb > 0
6085 then Buffer.add_char bb '\n';
6086 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6087 Hashtbl.iter (fun i o ->
6088 let isdifferent = always ||
6090 let dO = Hashtbl.find dh i in
6091 dO <> o
6092 with Not_found -> true
6094 if isdifferent
6095 then
6096 let addkm (k, m) =
6097 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6098 if Wsi.withalt m then Buffer.add_string bb "alt-";
6099 if Wsi.withshift m then Buffer.add_string bb "shift-";
6100 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6101 Buffer.add_string bb (Wsi.keyname k);
6103 let addkms l =
6104 let rec loop = function
6105 | [] -> ()
6106 | km :: [] -> addkm km
6107 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6109 loop l
6111 Buffer.add_string bb "<map in='";
6112 addkm i;
6113 match o with
6114 | KMinsrt km ->
6115 Buffer.add_string bb "' out='";
6116 addkm km;
6117 Buffer.add_string bb "'/>\n"
6119 | KMinsrl kms ->
6120 Buffer.add_string bb "' out='";
6121 addkms kms;
6122 Buffer.add_string bb "'/>\n"
6124 | KMmulti (ins, kms) ->
6125 Buffer.add_char bb ' ';
6126 addkms ins;
6127 Buffer.add_string bb "' out='";
6128 addkms kms;
6129 Buffer.add_string bb "'/>\n"
6130 ) h;
6131 Buffer.add_string bb "</keymap>";
6134 loop rest
6136 loop c.keyhashes;
6140 let save () =
6141 let uifontsize = fstate.fontsize in
6142 let bb = Buffer.create 32768 in
6143 let f (h, dc) =
6144 let dc = if conf.bedefault then conf else dc in
6145 Buffer.add_string bb "<llppconfig>\n";
6147 if String.length !fontpath > 0
6148 then
6149 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6150 uifontsize
6151 !fontpath
6152 else (
6153 if uifontsize <> 14
6154 then
6155 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6158 Buffer.add_string bb "<defaults ";
6159 add_attrs bb true dc dc;
6160 let kb = keymapsbuf true dc dc in
6161 if Buffer.length kb > 0
6162 then (
6163 Buffer.add_string bb ">\n";
6164 Buffer.add_buffer bb kb;
6165 Buffer.add_string bb "\n</defaults>\n";
6167 else Buffer.add_string bb "/>\n";
6169 let adddoc path pan anchor c bookmarks =
6170 if bookmarks == [] && c = dc && anchor = emptyanchor
6171 then ()
6172 else (
6173 Printf.bprintf bb "<doc path='%s'"
6174 (enent path 0 (String.length path));
6176 if anchor <> emptyanchor
6177 then (
6178 let n, y = anchor in
6179 Printf.bprintf bb " page='%d'" n;
6180 if y > 1e-6
6181 then
6182 Printf.bprintf bb " rely='%f'" y
6186 if pan != 0
6187 then Printf.bprintf bb " pan='%d'" pan;
6189 add_attrs bb false dc c;
6190 let kb = keymapsbuf false dc c in
6192 begin match bookmarks with
6193 | [] ->
6194 if Buffer.length kb > 0
6195 then (
6196 Buffer.add_string bb ">\n";
6197 Buffer.add_buffer bb kb;
6198 Buffer.add_string bb "</doc>\n";
6200 else Buffer.add_string bb "/>\n"
6201 | _ ->
6202 Buffer.add_string bb ">\n<bookmarks>\n";
6203 List.iter (fun (title, _level, (page, rely)) ->
6204 Printf.bprintf bb
6205 "<item title='%s' page='%d'"
6206 (enent title 0 (String.length title))
6207 page
6209 if rely > 1e-6
6210 then
6211 Printf.bprintf bb " rely='%f'" rely
6213 Buffer.add_string bb "/>\n";
6214 ) bookmarks;
6215 Buffer.add_string bb "</bookmarks>";
6216 if Buffer.length kb > 0
6217 then (
6218 Buffer.add_string bb "\n";
6219 Buffer.add_buffer bb kb;
6221 Buffer.add_string bb "\n</doc>\n";
6222 end;
6226 let pan, conf =
6227 match state.mode with
6228 | Birdseye (c, pan, _, _, _) ->
6229 let beyecolumns =
6230 match conf.columns with
6231 | Some ((c, _, _), _) -> Some c
6232 | None -> None
6233 and columns =
6234 match c.columns with
6235 | Some (c, _) -> Some (c, [||])
6236 | None -> None
6238 pan, { c with beyecolumns = beyecolumns; columns = columns }
6239 | _ -> state.x, conf
6241 let basename = Filename.basename state.path in
6242 adddoc basename pan (getanchor ())
6243 { conf with
6244 autoscrollstep =
6245 match state.autoscroll with
6246 | Some step -> step
6247 | None -> conf.autoscrollstep }
6248 (if conf.savebmarks then state.bookmarks else []);
6250 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6251 if basename <> path
6252 then adddoc path x y c bookmarks
6253 ) h;
6254 Buffer.add_string bb "</llppconfig>";
6256 load1 f;
6257 if Buffer.length bb > 0
6258 then
6260 let tmp = !confpath ^ ".tmp" in
6261 let oc = open_out_bin tmp in
6262 Buffer.output_buffer oc bb;
6263 close_out oc;
6264 Unix.rename tmp !confpath;
6265 with exn ->
6266 prerr_endline
6267 ("error while saving configuration: " ^ Printexc.to_string exn)
6269 end;;
6271 let () =
6272 Arg.parse
6273 (Arg.align
6274 [("-p", Arg.String (fun s -> state.password <- s) ,
6275 "<password> Set password");
6277 ("-f", Arg.String (fun s -> Config.fontpath := s),
6278 "<path> Set path to the user interface font");
6280 ("-c", Arg.String (fun s -> Config.confpath := s),
6281 "<path> Set path to the configuration file");
6283 ("-v", Arg.Unit (fun () ->
6284 Printf.printf
6285 "%s\nconfiguration path: %s\n"
6286 (version ())
6287 Config.defconfpath
6289 exit 0), " Print version and exit");
6292 (fun s -> state.path <- s)
6293 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6295 if String.length state.path = 0
6296 then (prerr_endline "file name missing"; exit 1);
6298 Config.load ();
6300 let globalkeyhash = findkeyhash conf "global" in
6301 state.wsfd <- Wsi.init (object
6302 method display = display ()
6303 method reshape w h = reshape w h
6304 method mouse b d x y m = mouse b d x y m
6305 method motion x y = state.mpos <- (x, y); motion x y
6306 method pmotion x y = state.mpos <- (x, y); pmotion x y
6307 method key k m =
6308 match state.keystate with
6309 | KSnone ->
6310 let km = k, m in
6311 begin
6312 match
6313 try Hashtbl.find globalkeyhash km
6314 with Not_found ->
6315 let modehash = state.uioh#modehash in
6316 try Hashtbl.find modehash km
6317 with Not_found -> KMinsrt (k, m)
6318 with
6319 | KMinsrt (k, m) -> keyboard k m
6320 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6321 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6323 | KSinto ((k', m') :: [], insrt) when k'=k && m' land m = m' ->
6324 List.iter (fun (k, m) -> keyboard k m) insrt;
6325 state.keystate <- KSnone
6326 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land m = m' ->
6327 state.keystate <- KSinto (keys, insrt)
6328 | _ ->
6329 state.keystate <- KSnone
6331 method enter x y = state.mpos <- (x, y); pmotion x y
6332 method leave = state.mpos <- (-1, -1)
6333 method quit = raise Quit
6334 end) conf.winw conf.winh;
6336 if not (
6337 List.exists GlMisc.check_extension
6338 [ "GL_ARB_texture_rectangle"
6339 ; "GL_EXT_texture_recangle"
6340 ; "GL_NV_texture_rectangle" ]
6342 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6344 let cr, sw = Unix.pipe ()
6345 and sr, cw = Unix.pipe () in
6347 cloexec cr;
6348 cloexec sw;
6349 cloexec sr;
6350 cloexec cw;
6352 setcheckers conf.checkers;
6353 redirectstderr ();
6355 init (cr, cw) (
6356 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6357 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6358 !Config.fontpath
6360 state.sr <- sr;
6361 state.sw <- sw;
6362 state.text <- "Opening " ^ state.path;
6363 opendoc state.path state.password;
6364 state.uioh <- uioh;
6365 setfontsize fstate.fontsize;
6366 doreshape conf.winw conf.winh;
6368 let rec loop deadline =
6369 let r =
6370 match state.errfd with
6371 | None -> [state.sr; state.wsfd]
6372 | Some fd -> [state.sr; state.wsfd; fd]
6374 if state.redisplay
6375 then (
6376 state.redisplay <- false;
6377 display ();
6379 let timeout =
6380 let now = now () in
6381 if deadline > now
6382 then (
6383 if deadline = infinity
6384 then ~-.1.0
6385 else max 0.0 (deadline -. now)
6387 else 0.0
6389 let r, _, _ =
6390 try Unix.select r [] [] timeout
6391 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6393 begin match r with
6394 | [] ->
6395 state.ghyll None;
6396 let newdeadline =
6397 if state.ghyll == noghyll
6398 then
6399 match state.autoscroll with
6400 | Some step when step != 0 ->
6401 let y = state.y + step in
6402 let y =
6403 if y < 0
6404 then state.maxy
6405 else if y >= state.maxy then 0 else y
6407 gotoy y;
6408 if state.mode = View
6409 then state.text <- "";
6410 deadline +. 0.01
6411 | _ -> infinity
6412 else deadline +. 0.01
6414 loop newdeadline
6416 | l ->
6417 let rec checkfds = function
6418 | [] -> ()
6419 | fd :: rest when fd = state.sr ->
6420 let cmd = readcmd state.sr in
6421 act cmd;
6422 checkfds rest
6424 | fd :: rest when fd = state.wsfd ->
6425 Wsi.readresp fd;
6426 checkfds rest
6428 | fd :: rest ->
6429 let s = String.create 80 in
6430 let n = Unix.read fd s 0 80 in
6431 if conf.redirectstderr
6432 then (
6433 Buffer.add_substring state.errmsgs s 0 n;
6434 state.newerrmsgs <- true;
6435 state.redisplay <- true;
6437 else (
6438 prerr_string (String.sub s 0 n);
6439 flush stderr;
6441 checkfds rest
6443 checkfds l;
6444 let newdeadline =
6445 let deadline1 =
6446 if deadline = infinity
6447 then now () +. 0.01
6448 else deadline
6450 match state.autoscroll with
6451 | Some step when step != 0 -> deadline1
6452 | _ -> if state.ghyll == noghyll then infinity else deadline1
6454 loop newdeadline
6455 end;
6458 loop infinity;
6459 with Quit ->
6460 wcmd "quit";
6461 Config.save ();
6462 exit 0;