Unions are fun
[llpp.git] / main.ml
blob2075da99ce27fc60e7b47cb5423285b19273de20
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 | Uunexpected of string
7 | Ulaunch of string
8 | Unamed of string
9 | Uremote of (string * int)
10 and facename = string;;
12 let dolog fmt = Printf.kprintf prerr_endline fmt;;
13 let now = Unix.gettimeofday;;
15 type params = (angle * proportional * trimparams
16 * texcount * sliceheight * memsize
17 * colorspace * fontpath)
18 and pageno = int
19 and width = int
20 and height = int
21 and leftx = int
22 and opaque = string
23 and recttype = int
24 and pixmapsize = int
25 and angle = int
26 and proportional = bool
27 and trimmargins = bool
28 and interpagespace = int
29 and texcount = int
30 and sliceheight = int
31 and gen = int
32 and top = float
33 and fontpath = string
34 and memsize = int
35 and aalevel = int
36 and irect = (int * int * int * int)
37 and trimparams = (trimmargins * irect)
38 and colorspace = | Rgb | Bgr | Gray
41 type link =
42 | Lnone
43 | Lfound of (int * int * int * int * int)
44 and linkdir =
45 | LDfirst
46 | LDlast
47 | LDfirstvisible of (int * int)
48 | LDleft of int
49 | LDright of int
50 | LDdown of int
51 | LDup of int
54 type pagewithlinks =
55 | Pwlnotfound
56 | Pwl of int
59 type keymap =
60 | KMinsrt of key
61 | KMinsrl of key list
62 | KMmulti of key list * key list
63 and key = int * int
64 and keyhash = (key, keymap) Hashtbl.t
65 and keystate =
66 | KSnone
67 | KSinto of (key list * key list)
70 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
71 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
73 type pipe = (Unix.file_descr * Unix.file_descr);;
75 external init : pipe -> params -> unit = "ml_init";;
76 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
77 external copysel : string -> opaque -> unit = "ml_copysel";;
78 external getpdimrect : int -> float array = "ml_getpdimrect";;
79 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
80 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
81 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
82 external measurestr : int -> string -> float = "ml_measure_string";;
83 external getmaxw : unit -> float = "ml_getmaxw";;
84 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
85 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
86 external platform : unit -> platform = "ml_platform";;
87 external setaalevel : int -> unit = "ml_setaalevel";;
88 external realloctexts : int -> bool = "ml_realloctexts";;
89 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
90 external findlink : opaque -> linkdir -> link = "ml_findlink";;
91 external getlink : opaque -> int -> under = "ml_getlink";;
92 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
94 let platform_to_string = function
95 | Punknown -> "unknown"
96 | Plinux -> "Linux"
97 | Posx -> "OSX"
98 | Psun -> "Sun"
99 | Pfreebsd -> "FreeBSD"
100 | Pdragonflybsd -> "DragonflyBSD"
101 | Popenbsd -> "OpenBSD"
102 | Pnetbsd -> "NetBSD"
103 | Pcygwin -> "Cygwin"
106 let platform = platform ();;
108 type x = int
109 and y = int
110 and tilex = int
111 and tiley = int
112 and tileparams = (x * y * width * height * tilex * tiley)
115 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
117 type mpos = int * int
118 and mstate =
119 | Msel of (mpos * mpos)
120 | Mpan of mpos
121 | Mscrolly | Mscrollx
122 | Mzoom of (int * int)
123 | Mzoomrect of (mpos * mpos)
124 | Mnone
127 type textentry = string * string * onhist option * onkey * ondone
128 and onkey = string -> int -> te
129 and ondone = string -> unit
130 and histcancel = unit -> unit
131 and onhist = ((histcmd -> string) * histcancel)
132 and histcmd = HCnext | HCprev | HCfirst | HClast
133 and te =
134 | TEstop
135 | TEdone of string
136 | TEcont of string
137 | TEswitch of textentry
140 type 'a circbuf =
141 { store : 'a array
142 ; mutable rc : int
143 ; mutable wc : int
144 ; mutable len : int
148 let bound v minv maxv =
149 max minv (min maxv v);
152 let cbnew n v =
153 { store = Array.create n v
154 ; rc = 0
155 ; wc = 0
156 ; len = 0
160 let drawstring size x y s =
161 Gl.enable `blend;
162 Gl.enable `texture_2d;
163 ignore (drawstr size x y s);
164 Gl.disable `blend;
165 Gl.disable `texture_2d;
168 let drawstring1 size x y s =
169 drawstr size x y s;
172 let drawstring2 size x y fmt =
173 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
176 let cbcap b = Array.length b.store;;
178 let cbput b v =
179 let cap = cbcap b in
180 b.store.(b.wc) <- v;
181 b.wc <- (b.wc + 1) mod cap;
182 b.rc <- b.wc;
183 b.len <- min (b.len + 1) cap;
186 let cbempty b = b.len = 0;;
188 let cbgetg b circular dir =
189 if cbempty b
190 then b.store.(0)
191 else
192 let rc = b.rc + dir in
193 let rc =
194 if circular
195 then (
196 if rc = -1
197 then b.len-1
198 else (
199 if rc = b.len
200 then 0
201 else rc
204 else max 0 (min rc (b.len-1))
206 b.rc <- rc;
207 b.store.(rc);
210 let cbget b = cbgetg b false;;
211 let cbgetc b = cbgetg b true;;
213 type page =
214 { pageno : int
215 ; pagedimno : int
216 ; pagew : int
217 ; pageh : int
218 ; pagex : int
219 ; pagey : int
220 ; pagevw : int
221 ; pagevh : int
222 ; pagedispx : int
223 ; pagedispy : int
227 let debugl l =
228 dolog "l %d dim=%d {" l.pageno l.pagedimno;
229 dolog " WxH %dx%d" l.pagew l.pageh;
230 dolog " vWxH %dx%d" l.pagevw l.pagevh;
231 dolog " pagex,y %d,%d" l.pagex l.pagey;
232 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
233 dolog "}";
236 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
237 dolog "rect {";
238 dolog " x0,y0=(% f, % f)" x0 y0;
239 dolog " x1,y1=(% f, % f)" x1 y1;
240 dolog " x2,y2=(% f, % f)" x2 y2;
241 dolog " x3,y3=(% f, % f)" x3 y3;
242 dolog "}";
245 type columns =
246 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
247 and multicol = columncount * covercount * covercount
248 and pdimno = int
249 and columncount = int
250 and covercount = int;;
252 type conf =
253 { mutable scrollbw : int
254 ; mutable scrollh : int
255 ; mutable icase : bool
256 ; mutable preload : bool
257 ; mutable pagebias : int
258 ; mutable verbose : bool
259 ; mutable debug : bool
260 ; mutable scrollstep : int
261 ; mutable maxhfit : bool
262 ; mutable crophack : bool
263 ; mutable autoscrollstep : int
264 ; mutable maxwait : float option
265 ; mutable hlinks : bool
266 ; mutable underinfo : bool
267 ; mutable interpagespace : interpagespace
268 ; mutable zoom : float
269 ; mutable presentation : bool
270 ; mutable angle : angle
271 ; mutable winw : int
272 ; mutable winh : int
273 ; mutable savebmarks : bool
274 ; mutable proportional : proportional
275 ; mutable trimmargins : trimmargins
276 ; mutable trimfuzz : irect
277 ; mutable memlimit : memsize
278 ; mutable texcount : texcount
279 ; mutable sliceheight : sliceheight
280 ; mutable thumbw : width
281 ; mutable jumpback : bool
282 ; mutable bgcolor : float * float * float
283 ; mutable bedefault : bool
284 ; mutable scrollbarinpm : bool
285 ; mutable tilew : int
286 ; mutable tileh : int
287 ; mutable mustoresize : memsize
288 ; mutable checkers : bool
289 ; mutable aalevel : int
290 ; mutable urilauncher : string
291 ; mutable pathlauncher : string
292 ; mutable colorspace : colorspace
293 ; mutable invert : bool
294 ; mutable colorscale : float
295 ; mutable redirectstderr : bool
296 ; mutable ghyllscroll : (int * int * int) option
297 ; mutable columns : columns option
298 ; mutable beyecolumns : columncount option
299 ; mutable selcmd : string
300 ; mutable updatecurs : bool
301 ; mutable keyhashes : (string * keyhash) list
305 type anchor = pageno * top;;
307 type outline = string * int * anchor;;
309 type rect = float * float * float * float * float * float * float * float;;
311 type tile = opaque * pixmapsize * elapsed
312 and elapsed = float;;
313 type pagemapkey = pageno * gen;;
314 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
315 and row = int
316 and col = int;;
318 let emptyanchor = (0, 0.0);;
320 type infochange = | Memused | Docinfo | Pdim;;
322 class type uioh = object
323 method display : unit
324 method key : int -> int -> uioh
325 method button : int -> bool -> int -> int -> int -> uioh
326 method motion : int -> int -> uioh
327 method pmotion : int -> int -> uioh
328 method infochanged : infochange -> unit
329 method scrollpw : (int * float * float)
330 method scrollph : (int * float * float)
331 method modehash : keyhash
332 end;;
334 type mode =
335 | Birdseye of (conf * leftx * pageno * pageno * anchor)
336 | Textentry of (textentry * onleave)
337 | View
338 | LinkNav of ((page * opaque * int) * (int * int * int * int)) option
339 and onleave = leavetextentrystatus -> unit
340 and leavetextentrystatus = | Cancel | Confirm
341 and helpitem = string * int * action
342 and action =
343 | Noaction
344 | Action of (uioh -> uioh)
347 let isbirdseye = function Birdseye _ -> true | _ -> false;;
348 let istextentry = function Textentry _ -> true | _ -> false;;
350 type currently =
351 | Idle
352 | Loading of (page * gen)
353 | Tiling of (
354 page * opaque * colorspace * angle * gen * col * row * width * height
356 | Outlining of outline list
359 let emptykeyhash = Hashtbl.create 0;;
360 let nouioh : uioh = object (self)
361 method display = ()
362 method key _ _ = self
363 method button _ _ _ _ _ = self
364 method motion _ _ = self
365 method pmotion _ _ = self
366 method infochanged _ = ()
367 method scrollpw = (0, nan, nan)
368 method scrollph = (0, nan, nan)
369 method modehash = emptykeyhash
370 end;;
372 type state =
373 { mutable sr : Unix.file_descr
374 ; mutable sw : Unix.file_descr
375 ; mutable wsfd : Unix.file_descr
376 ; mutable errfd : Unix.file_descr option
377 ; mutable stderr : Unix.file_descr
378 ; mutable errmsgs : Buffer.t
379 ; mutable newerrmsgs : bool
380 ; mutable w : int
381 ; mutable x : int
382 ; mutable y : int
383 ; mutable scrollw : int
384 ; mutable hscrollh : int
385 ; mutable anchor : anchor
386 ; mutable ranchors : (string * string * anchor) list
387 ; mutable maxy : int
388 ; mutable layout : page list
389 ; pagemap : (pagemapkey, opaque) Hashtbl.t
390 ; tilemap : (tilemapkey, tile) Hashtbl.t
391 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
392 ; mutable pdims : (pageno * width * height * leftx) list
393 ; mutable pagecount : int
394 ; mutable currently : currently
395 ; mutable mstate : mstate
396 ; mutable searchpattern : string
397 ; mutable rects : (pageno * recttype * rect) list
398 ; mutable rects1 : (pageno * recttype * rect) list
399 ; mutable text : string
400 ; mutable fullscreen : (width * height) option
401 ; mutable mode : mode
402 ; mutable uioh : uioh
403 ; mutable outlines : outline array
404 ; mutable bookmarks : outline list
405 ; mutable path : string
406 ; mutable password : string
407 ; mutable invalidated : int
408 ; mutable memused : memsize
409 ; mutable gen : gen
410 ; mutable throttle : (page list * int * float) option
411 ; mutable autoscroll : int option
412 ; mutable ghyll : (int option -> unit)
413 ; mutable help : helpitem array
414 ; mutable docinfo : (int * string) list
415 ; mutable texid : GlTex.texture_id option
416 ; hists : hists
417 ; mutable prevzoom : float
418 ; mutable progress : float
419 ; mutable redisplay : bool
420 ; mutable mpos : mpos
421 ; mutable keystate : keystate
423 and hists =
424 { pat : string circbuf
425 ; pag : string circbuf
426 ; nav : anchor circbuf
427 ; sel : string circbuf
431 let defconf =
432 { scrollbw = 7
433 ; scrollh = 12
434 ; icase = true
435 ; preload = true
436 ; pagebias = 0
437 ; verbose = false
438 ; debug = false
439 ; scrollstep = 24
440 ; maxhfit = true
441 ; crophack = false
442 ; autoscrollstep = 2
443 ; maxwait = None
444 ; hlinks = false
445 ; underinfo = false
446 ; interpagespace = 2
447 ; zoom = 1.0
448 ; presentation = false
449 ; angle = 0
450 ; winw = 900
451 ; winh = 900
452 ; savebmarks = true
453 ; proportional = true
454 ; trimmargins = false
455 ; trimfuzz = (0,0,0,0)
456 ; memlimit = 32 lsl 20
457 ; texcount = 256
458 ; sliceheight = 24
459 ; thumbw = 76
460 ; jumpback = true
461 ; bgcolor = (0.5, 0.5, 0.5)
462 ; bedefault = false
463 ; scrollbarinpm = true
464 ; tilew = 2048
465 ; tileh = 2048
466 ; mustoresize = 128 lsl 20
467 ; checkers = true
468 ; aalevel = 8
469 ; urilauncher =
470 (match platform with
471 | Plinux | Pfreebsd | Pdragonflybsd
472 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
473 | Posx -> "open \"%s\""
474 | Pcygwin -> "cygstart %s"
475 | Punknown -> "echo %s")
476 ; pathlauncher = "lp \"%s\""
477 ; selcmd =
478 (match platform with
479 | Plinux | Pfreebsd | Pdragonflybsd
480 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
481 | Posx -> "pbcopy"
482 | Pcygwin -> "wsel"
483 | Punknown -> "cat")
484 ; colorspace = Rgb
485 ; invert = false
486 ; colorscale = 1.0
487 ; redirectstderr = false
488 ; ghyllscroll = None
489 ; columns = None
490 ; beyecolumns = None
491 ; updatecurs = false
492 ; keyhashes =
493 let mk n = (n, Hashtbl.create 1) in
494 [ mk "global"
495 ; mk "info"
496 ; mk "help"
497 ; mk "outline"
498 ; mk "listview"
499 ; mk "birdseye"
500 ; mk "textentry"
501 ; mk "links"
506 let findkeyhash c name =
507 try List.assoc name c.keyhashes
508 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
511 let conf = { defconf with angle = defconf.angle };;
513 type fontstate =
514 { mutable fontsize : int
515 ; mutable wwidth : float
516 ; mutable maxrows : int
520 let fstate =
521 { fontsize = 14
522 ; wwidth = nan
523 ; maxrows = -1
527 let setfontsize n =
528 fstate.fontsize <- n;
529 fstate.wwidth <- measurestr fstate.fontsize "w";
530 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
533 let geturl s =
534 let colonpos = try String.index s ':' with Not_found -> -1 in
535 let len = String.length s in
536 if colonpos >= 0 && colonpos + 3 < len
537 then (
538 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
539 then
540 let schemestartpos =
541 try String.rindex_from s colonpos ' '
542 with Not_found -> -1
544 let scheme =
545 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
547 match scheme with
548 | "http" | "ftp" | "mailto" ->
549 let epos =
550 try String.index_from s colonpos ' '
551 with Not_found -> len
553 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
554 | _ -> ""
555 else ""
557 else ""
560 let popen =
561 let shell, farg = "/bin/sh", "-c" in
562 fun s ->
563 let args = [|shell; farg; s|] in
564 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
567 let gotouri uri =
568 if String.length conf.urilauncher = 0
569 then print_endline uri
570 else (
571 let url = geturl uri in
572 if String.length url = 0
573 then print_endline uri
574 else
575 let re = Str.regexp "%s" in
576 let command = Str.global_replace re url conf.urilauncher in
577 try popen command
578 with exn ->
579 Printf.eprintf
580 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
581 flush stderr;
585 let version () =
586 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
587 (platform_to_string platform) Sys.word_size Sys.ocaml_version
590 let makehelp () =
591 let strings = version () :: "" :: Help.keys in
592 Array.of_list (
593 List.map (fun s ->
594 let url = geturl s in
595 if String.length url > 0
596 then (s, 0, Action (fun u -> gotouri url; u))
597 else (s, 0, Noaction)
598 ) strings);
601 let noghyll _ = ();;
603 let state =
604 { sr = Unix.stdin
605 ; sw = Unix.stdin
606 ; wsfd = Unix.stdin
607 ; errfd = None
608 ; stderr = Unix.stderr
609 ; errmsgs = Buffer.create 0
610 ; newerrmsgs = false
611 ; x = 0
612 ; y = 0
613 ; w = 0
614 ; scrollw = 0
615 ; hscrollh = 0
616 ; anchor = emptyanchor
617 ; ranchors = []
618 ; layout = []
619 ; maxy = max_int
620 ; tilelru = Queue.create ()
621 ; pagemap = Hashtbl.create 10
622 ; tilemap = Hashtbl.create 10
623 ; pdims = []
624 ; pagecount = 0
625 ; currently = Idle
626 ; mstate = Mnone
627 ; rects = []
628 ; rects1 = []
629 ; text = ""
630 ; mode = View
631 ; fullscreen = None
632 ; searchpattern = ""
633 ; outlines = [||]
634 ; bookmarks = []
635 ; path = ""
636 ; password = ""
637 ; invalidated = 0
638 ; hists =
639 { nav = cbnew 10 (0, 0.0)
640 ; pat = cbnew 10 ""
641 ; pag = cbnew 10 ""
642 ; sel = cbnew 10 ""
644 ; memused = 0
645 ; gen = 0
646 ; throttle = None
647 ; autoscroll = None
648 ; ghyll = noghyll
649 ; help = makehelp ()
650 ; docinfo = []
651 ; texid = None
652 ; prevzoom = 1.0
653 ; progress = -1.0
654 ; uioh = nouioh
655 ; redisplay = false
656 ; mpos = (-1, -1)
657 ; keystate = KSnone
661 let vlog fmt =
662 if conf.verbose
663 then
664 Printf.kprintf prerr_endline fmt
665 else
666 Printf.kprintf ignore fmt
669 let launchpath () =
670 if String.length conf.pathlauncher = 0
671 then print_endline state.path
672 else (
673 let re = Str.regexp "%s" in
674 let command = Str.global_replace re state.path conf.pathlauncher in
675 try popen command
676 with exn ->
677 Printf.eprintf
678 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
679 flush stderr;
683 let redirectstderr () =
684 if conf.redirectstderr
685 then
686 let rfd, wfd = Unix.pipe () in
687 state.stderr <- Unix.dup Unix.stderr;
688 state.errfd <- Some rfd;
689 Unix.dup2 wfd Unix.stderr;
690 else (
691 state.newerrmsgs <- false;
692 begin match state.errfd with
693 | Some fd ->
694 Unix.close fd;
695 Unix.dup2 state.stderr Unix.stderr;
696 state.errfd <- None;
697 | None -> ()
698 end;
699 prerr_string (Buffer.contents state.errmsgs);
700 flush stderr;
701 Buffer.clear state.errmsgs;
705 module G =
706 struct
707 let postRedisplay who =
708 if conf.verbose
709 then prerr_endline ("redisplay for " ^ who);
710 state.redisplay <- true;
712 end;;
714 let getopaque pageno =
715 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
716 with Not_found -> None
719 let putopaque pageno opaque =
720 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
723 let pagetranslatepoint l x y =
724 let dy = y - l.pagedispy in
725 let y = dy + l.pagey in
726 let dx = x - l.pagedispx in
727 let x = dx + l.pagex in
728 (x, y);
731 let getunder x y =
732 let rec f = function
733 | l :: rest ->
734 begin match getopaque l.pageno with
735 | Some opaque ->
736 let x0 = l.pagedispx in
737 let x1 = x0 + l.pagevw in
738 let y0 = l.pagedispy in
739 let y1 = y0 + l.pagevh in
740 if y >= y0 && y <= y1 && x >= x0 && x <= x1
741 then
742 let px, py = pagetranslatepoint l x y in
743 match whatsunder opaque px py with
744 | Unone -> f rest
745 | under -> under
746 else f rest
747 | _ ->
748 f rest
750 | [] -> Unone
752 f state.layout
755 let showtext c s =
756 state.text <- Printf.sprintf "%c%s" c s;
757 G.postRedisplay "showtext";
760 let updateunder x y =
761 match getunder x y with
762 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
763 | Ulinkuri uri ->
764 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
765 Wsi.setcursor Wsi.CURSOR_INFO
766 | Ulinkgoto (page, _) ->
767 if conf.underinfo
768 then showtext 'p' ("age: " ^ string_of_int (page+1));
769 Wsi.setcursor Wsi.CURSOR_INFO
770 | Utext s ->
771 if conf.underinfo then showtext 'f' ("ont: " ^ s);
772 Wsi.setcursor Wsi.CURSOR_TEXT
773 | Uunexpected s ->
774 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
775 Wsi.setcursor Wsi.CURSOR_INHERIT
776 | Ulaunch s ->
777 if conf.underinfo then showtext 'l' ("launch: " ^ s);
778 Wsi.setcursor Wsi.CURSOR_INHERIT
779 | Unamed s ->
780 if conf.underinfo then showtext 'n' ("named: " ^ s);
781 Wsi.setcursor Wsi.CURSOR_INHERIT
782 | Uremote (filename, pageno) ->
783 if conf.underinfo then showtext 'r'
784 (Printf.sprintf "emote: %s (%d)" filename pageno);
785 Wsi.setcursor Wsi.CURSOR_INFO
788 let addchar s c =
789 let b = Buffer.create (String.length s + 1) in
790 Buffer.add_string b s;
791 Buffer.add_char b c;
792 Buffer.contents b;
795 let colorspace_of_string s =
796 match String.lowercase s with
797 | "rgb" -> Rgb
798 | "bgr" -> Bgr
799 | "gray" -> Gray
800 | _ -> failwith "invalid colorspace"
803 let int_of_colorspace = function
804 | Rgb -> 0
805 | Bgr -> 1
806 | Gray -> 2
809 let colorspace_of_int = function
810 | 0 -> Rgb
811 | 1 -> Bgr
812 | 2 -> Gray
813 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
816 let colorspace_to_string = function
817 | Rgb -> "rgb"
818 | Bgr -> "bgr"
819 | Gray -> "gray"
822 let intentry_with_suffix text key =
823 let c =
824 if key >= 32 && key < 127
825 then Char.chr key
826 else '\000'
828 match Char.lowercase c with
829 | '0' .. '9' ->
830 let text = addchar text c in
831 TEcont text
833 | 'k' | 'm' | 'g' ->
834 let text = addchar text c in
835 TEcont text
837 | _ ->
838 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
839 TEcont text
842 let columns_to_string (n, a, b) =
843 if a = 0 && b = 0
844 then Printf.sprintf "%d" n
845 else Printf.sprintf "%d,%d,%d" n a b;
848 let columns_of_string s =
850 (int_of_string s, 0, 0)
851 with _ ->
852 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
855 let readcmd fd =
856 let s = "xxxx" in
857 let n = Unix.read fd s 0 4 in
858 if n != 4 then failwith "incomplete read(len)";
859 let len = 0
860 lor (Char.code s.[0] lsl 24)
861 lor (Char.code s.[1] lsl 16)
862 lor (Char.code s.[2] lsl 8)
863 lor (Char.code s.[3] lsl 0)
865 let s = String.create len in
866 let n = Unix.read fd s 0 len in
867 if n != len then failwith "incomplete read(data)";
871 let btod b = if b then 1 else 0;;
873 let wcmd fmt =
874 let b = Buffer.create 16 in
875 Buffer.add_string b "llll";
876 Printf.kbprintf
877 (fun b ->
878 let s = Buffer.contents b in
879 let n = String.length s in
880 let len = n - 4 in
881 s.[0] <- Char.chr ((len lsr 24) land 0xff);
882 s.[1] <- Char.chr ((len lsr 16) land 0xff);
883 s.[2] <- Char.chr ((len lsr 8) land 0xff);
884 s.[3] <- Char.chr (len land 0xff);
885 let n' = Unix.write state.sw s 0 n in
886 if n' != n then failwith "write failed";
887 ) b fmt;
890 let calcips h =
891 if conf.presentation
892 then
893 let d = conf.winh - h in
894 max 0 ((d + 1) / 2)
895 else
896 conf.interpagespace
899 let calcheight () =
900 let rec f pn ph pi fh l =
901 match l with
902 | (n, _, h, _) :: rest ->
903 let ips = calcips h in
904 let fh =
905 if conf.presentation
906 then fh+ips
907 else (
908 if isbirdseye state.mode && pn = 0
909 then fh + ips
910 else fh
913 let fh = fh + ((n - pn) * (ph + pi)) in
914 f n h ips fh rest;
916 | [] ->
917 let inc =
918 if conf.presentation || (isbirdseye state.mode && pn = 0)
919 then 0
920 else -pi
922 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
923 max 0 fh
925 let fh = f 0 0 0 0 state.pdims in
929 let calcheight () =
930 match conf.columns with
931 | None -> calcheight ()
932 | Some (_, b) ->
933 if Array.length b > 0
934 then
935 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
936 y + h
937 else 0
940 let getpageyh pageno =
941 let rec f pn ph pi y l =
942 match l with
943 | (n, _, h, _) :: rest ->
944 let ips = calcips h in
945 if n >= pageno
946 then
947 let h = if n = pageno then h else ph in
948 if conf.presentation && n = pageno
949 then
950 y + (pageno - pn) * (ph + pi) + pi, h
951 else
952 y + (pageno - pn) * (ph + pi), h
953 else
954 let y = y + (if conf.presentation then pi else 0) in
955 let y = y + (n - pn) * (ph + pi) in
956 f n h ips y rest
958 | [] ->
959 y + (pageno - pn) * (ph + pi), ph
961 f 0 0 0 0 state.pdims
964 let getpageyh pageno =
965 match conf.columns with
966 | None -> getpageyh pageno
967 | Some (_, b) ->
968 let (_, _, y, (_, _, h, _)) = b.(pageno) in
969 y, h
972 let getpagedim pageno =
973 let rec f ppdim l =
974 match l with
975 | (n, _, _, _) as pdim :: rest ->
976 if n >= pageno
977 then (if n = pageno then pdim else ppdim)
978 else f pdim rest
980 | [] -> ppdim
982 f (-1, -1, -1, -1) state.pdims
985 let getpagey pageno = fst (getpageyh pageno);;
987 let layout1 y sh =
988 let sh = sh - state.hscrollh in
989 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
990 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
991 match pdims with
992 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
993 let ips = calcips h in
994 let yinc =
995 if conf.presentation || (isbirdseye state.mode && pageno = 0)
996 then ips
997 else 0
999 (w, h, ips, xoff), rest, pdimno + 1, yinc
1000 | _ ->
1001 prev, pdims, pdimno, 0
1003 let dy = dy + yinc in
1004 let py = py + yinc in
1005 if pageno = state.pagecount || dy >= sh
1006 then
1007 accu
1008 else
1009 let vy = y + dy in
1010 if py + h <= vy - yinc
1011 then
1012 let py = py + h + ips in
1013 let dy = max 0 (py - y) in
1014 f ~pageno:(pageno+1)
1015 ~pdimno
1016 ~prev:curr
1019 ~pdims:rest
1020 ~accu
1021 else
1022 let pagey = vy - py in
1023 let pagevh = h - pagey in
1024 let pagevh = min (sh - dy) pagevh in
1025 let off = if yinc > 0 then py - vy else 0 in
1026 let py = py + h + ips in
1027 let pagex, dx =
1028 let xoff = xoff +
1029 if state.w < conf.winw - state.scrollw
1030 then (conf.winw - state.scrollw - state.w) / 2
1031 else 0
1033 let dispx = xoff + state.x in
1034 if dispx < 0
1035 then (-dispx, 0)
1036 else (0, dispx)
1038 let pagevw =
1039 let lw = w - pagex in
1040 min lw (conf.winw - state.scrollw)
1042 let e =
1043 { pageno = pageno
1044 ; pagedimno = pdimno
1045 ; pagew = w
1046 ; pageh = h
1047 ; pagex = pagex
1048 ; pagey = pagey + off
1049 ; pagevw = pagevw
1050 ; pagevh = pagevh - off
1051 ; pagedispx = dx
1052 ; pagedispy = dy + off
1055 let accu = e :: accu in
1056 f ~pageno:(pageno+1)
1057 ~pdimno
1058 ~prev:curr
1060 ~dy:(dy+pagevh+ips)
1061 ~pdims:rest
1062 ~accu
1064 if state.invalidated = 0
1065 then (
1066 let accu =
1068 ~pageno:0
1069 ~pdimno:~-1
1070 ~prev:(0,0,0,0)
1071 ~py:0
1072 ~dy:0
1073 ~pdims:state.pdims
1074 ~accu:[]
1076 List.rev accu
1078 else
1082 let layoutN ((columns, coverA, coverB), b) y sh =
1083 let sh = sh - state.hscrollh in
1084 let rec fold accu n =
1085 if n = Array.length b
1086 then accu
1087 else
1088 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1089 if (vy - y) > sh &&
1090 (n = coverA - 1
1091 || n = state.pagecount - coverB
1092 || (n - coverA) mod columns = columns - 1)
1093 then accu
1094 else
1095 let accu =
1096 if vy + h > y
1097 then
1098 let pagey = max 0 (y - vy) in
1099 let pagedispy = if pagey > 0 then 0 else vy - y in
1100 let pagedispx, pagex, pagevw =
1101 let pdx =
1102 if n = coverA - 1 || n = state.pagecount - coverB
1103 then state.x + (conf.winw - state.scrollw - w) / 2
1104 else dx + xoff + state.x
1106 if pdx < 0
1107 then 0, -pdx, w + pdx
1108 else pdx, 0, min (conf.winw - state.scrollw) w
1110 let pagevh = min (h - pagey) (sh - pagedispy) in
1111 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1112 then
1113 let e =
1114 { pageno = n
1115 ; pagedimno = pdimno
1116 ; pagew = w
1117 ; pageh = h
1118 ; pagex = pagex
1119 ; pagey = pagey
1120 ; pagevw = pagevw
1121 ; pagevh = pagevh
1122 ; pagedispx = pagedispx
1123 ; pagedispy = pagedispy
1126 e :: accu
1127 else
1128 accu
1129 else
1130 accu
1132 fold accu (n+1)
1134 if state.invalidated = 0
1135 then List.rev (fold [] 0)
1136 else []
1139 let layout y sh =
1140 match conf.columns with
1141 | None -> layout1 y sh
1142 | Some c -> layoutN c y sh
1145 let clamp incr =
1146 let y = state.y + incr in
1147 let y = max 0 y in
1148 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1152 let itertiles l f =
1153 let tilex = l.pagex mod conf.tilew in
1154 let tiley = l.pagey mod conf.tileh in
1156 let col = l.pagex / conf.tilew in
1157 let row = l.pagey / conf.tileh in
1159 let vw =
1160 let a = l.pagew - l.pagex in
1161 let b = conf.winw - state.scrollw in
1162 min a b
1163 and vh = l.pagevh in
1165 let rec rowloop row y0 dispy h =
1166 if h = 0
1167 then ()
1168 else (
1169 let dh = conf.tileh - y0 in
1170 let dh = min h dh in
1171 let rec colloop col x0 dispx w =
1172 if w = 0
1173 then ()
1174 else (
1175 let dw = conf.tilew - x0 in
1176 let dw = min w dw in
1178 f col row dispx dispy x0 y0 dw dh;
1179 colloop (col+1) 0 (dispx+dw) (w-dw)
1182 colloop col tilex l.pagedispx vw;
1183 rowloop (row+1) 0 (dispy+dh) (h-dh)
1186 if vw > 0 && vh > 0
1187 then rowloop row tiley l.pagedispy vh;
1190 let gettileopaque l col row =
1191 let key =
1192 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1194 try Some (Hashtbl.find state.tilemap key)
1195 with Not_found -> None
1198 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1199 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1200 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1203 let drawtiles l color =
1204 GlDraw.color color;
1205 let f col row x y tilex tiley w h =
1206 match gettileopaque l col row with
1207 | Some (opaque, _, t) ->
1208 let params = x, y, w, h, tilex, tiley in
1209 if conf.invert
1210 then (
1211 Gl.enable `blend;
1212 GlFunc.blend_func `zero `one_minus_src_color;
1214 drawtile params opaque;
1215 if conf.invert
1216 then Gl.disable `blend;
1217 if conf.debug
1218 then (
1219 let s = Printf.sprintf
1220 "%d[%d,%d] %f sec"
1221 l.pageno col row t
1223 let w = measurestr fstate.fontsize s in
1224 GlMisc.push_attrib [`current];
1225 GlDraw.color (0.0, 0.0, 0.0);
1226 GlDraw.rect
1227 (float (x-2), float (y-2))
1228 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1229 GlDraw.color (1.0, 1.0, 1.0);
1230 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1231 GlMisc.pop_attrib ();
1234 | _ ->
1235 let w =
1236 let lw = conf.winw - state.scrollw - x in
1237 min lw w
1238 and h =
1239 let lh = conf.winh - y in
1240 min lh h
1242 Gl.enable `texture_2d;
1243 begin match state.texid with
1244 | Some id ->
1245 GlTex.bind_texture `texture_2d id;
1246 let x0 = float x
1247 and y0 = float y
1248 and x1 = float (x+w)
1249 and y1 = float (y+h) in
1251 let tw = float w /. 64.0
1252 and th = float h /. 64.0 in
1253 let tx0 = float tilex /. 64.0
1254 and ty0 = float tiley /. 64.0 in
1255 let tx1 = tx0 +. tw
1256 and ty1 = ty0 +. th in
1257 GlDraw.begins `quads;
1258 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1259 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1260 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1261 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1262 GlDraw.ends ();
1264 Gl.disable `texture_2d;
1265 | None ->
1266 GlDraw.color (1.0, 1.0, 1.0);
1267 GlDraw.rect
1268 (float x, float y)
1269 (float (x+w), float (y+h));
1270 end;
1271 if w > 128 && h > fstate.fontsize + 10
1272 then (
1273 GlDraw.color (0.0, 0.0, 0.0);
1274 let c, r =
1275 if conf.verbose
1276 then (col*conf.tilew, row*conf.tileh)
1277 else col, row
1279 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1281 GlDraw.color color;
1283 itertiles l f
1286 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1288 let tilevisible1 l x y =
1289 let ax0 = l.pagex
1290 and ax1 = l.pagex + l.pagevw
1291 and ay0 = l.pagey
1292 and ay1 = l.pagey + l.pagevh in
1294 let bx0 = x
1295 and by0 = y in
1296 let bx1 = min (bx0 + conf.tilew) l.pagew
1297 and by1 = min (by0 + conf.tileh) l.pageh in
1299 let rx0 = max ax0 bx0
1300 and ry0 = max ay0 by0
1301 and rx1 = min ax1 bx1
1302 and ry1 = min ay1 by1 in
1304 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1305 nonemptyintersection
1308 let tilevisible layout n x y =
1309 let rec findpageinlayout = function
1310 | l :: _ when l.pageno = n -> tilevisible1 l x y
1311 | _ :: rest -> findpageinlayout rest
1312 | [] -> false
1314 findpageinlayout layout
1317 let tileready l x y =
1318 tilevisible1 l x y &&
1319 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1322 let tilepage n p layout =
1323 let rec loop = function
1324 | l :: rest ->
1325 if l.pageno = n
1326 then
1327 let f col row _ _ _ _ _ _ =
1328 if state.currently = Idle
1329 then
1330 match gettileopaque l col row with
1331 | Some _ -> ()
1332 | None ->
1333 let x = col*conf.tilew
1334 and y = row*conf.tileh in
1335 let w =
1336 let w = l.pagew - x in
1337 min w conf.tilew
1339 let h =
1340 let h = l.pageh - y in
1341 min h conf.tileh
1343 wcmd "tile %s %d %d %d %d" p x y w h;
1344 state.currently <-
1345 Tiling (
1346 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1347 conf.tilew, conf.tileh
1350 itertiles l f;
1351 else
1352 loop rest
1354 | [] -> ()
1356 if state.invalidated = 0 then loop layout;
1359 let preloadlayout visiblepages =
1360 let presentation = conf.presentation in
1361 let interpagespace = conf.interpagespace in
1362 let maxy = state.maxy in
1363 conf.presentation <- false;
1364 conf.interpagespace <- 0;
1365 state.maxy <- calcheight ();
1366 let y =
1367 match visiblepages with
1368 | [] -> 0
1369 | l :: _ -> getpagey l.pageno + l.pagey
1371 let y = if y < conf.winh then 0 else y - conf.winh in
1372 let h = state.y - y + conf.winh*3 in
1373 let pages = layout y h in
1374 conf.presentation <- presentation;
1375 conf.interpagespace <- interpagespace;
1376 state.maxy <- maxy;
1377 pages;
1380 let load pages =
1381 let rec loop pages =
1382 if state.currently != Idle
1383 then ()
1384 else
1385 match pages with
1386 | l :: rest ->
1387 begin match getopaque l.pageno with
1388 | None ->
1389 wcmd "page %d %d" l.pageno l.pagedimno;
1390 state.currently <- Loading (l, state.gen);
1391 | Some opaque ->
1392 tilepage l.pageno opaque pages;
1393 loop rest
1394 end;
1395 | _ -> ()
1397 if state.invalidated = 0 then loop pages
1400 let preload pages =
1401 load pages;
1402 if conf.preload && state.currently = Idle
1403 then load (preloadlayout pages);
1406 let layoutready layout =
1407 let rec fold all ls =
1408 all && match ls with
1409 | l :: rest ->
1410 let seen = ref false in
1411 let allvisible = ref true in
1412 let foo col row _ _ _ _ _ _ =
1413 seen := true;
1414 allvisible := !allvisible &&
1415 begin match gettileopaque l col row with
1416 | Some _ -> true
1417 | None -> false
1420 itertiles l foo;
1421 fold (!seen && !allvisible) rest
1422 | [] -> true
1424 let alltilesvisible = fold true layout in
1425 alltilesvisible;
1428 let gotoy y =
1429 let y = bound y 0 state.maxy in
1430 let y, layout, proceed =
1431 match conf.maxwait with
1432 | Some time when state.ghyll == noghyll ->
1433 begin match state.throttle with
1434 | None ->
1435 let layout = layout y conf.winh in
1436 let ready = layoutready layout in
1437 if not ready
1438 then (
1439 load layout;
1440 state.throttle <- Some (layout, y, now ());
1442 else G.postRedisplay "gotoy showall (None)";
1443 y, layout, ready
1444 | Some (_, _, started) ->
1445 let dt = now () -. started in
1446 if dt > time
1447 then (
1448 state.throttle <- None;
1449 let layout = layout y conf.winh in
1450 load layout;
1451 G.postRedisplay "maxwait";
1452 y, layout, true
1454 else -1, [], false
1457 | _ ->
1458 let layout = layout y conf.winh in
1459 if true || layoutready layout
1460 then G.postRedisplay "gotoy ready";
1461 y, layout, true
1463 if proceed
1464 then (
1465 state.y <- y;
1466 state.layout <- layout;
1467 begin match state.mode with
1468 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1469 if not (pagevisible layout pageno)
1470 then (
1471 match state.layout with
1472 | [] -> ()
1473 | l :: _ ->
1474 state.mode <- Birdseye (
1475 conf, leftx, l.pageno, hooverpageno, anchor
1478 | LinkNav _ ->
1479 let linknav =
1480 let rec loop = function
1481 | [] -> None
1482 | l :: rest ->
1483 match getopaque l.pageno with
1484 | None -> loop rest
1485 | Some opaque ->
1486 let link =
1487 findlink opaque (LDfirstvisible (l.pagex, l.pagey))
1489 match link with
1490 | Lnone -> loop rest
1491 | Lfound (n, x0, y0, x1, y1) ->
1492 Some ((l, opaque, n), (x0, y0, x1, y1))
1494 loop state.layout
1496 state.mode <- LinkNav linknav
1497 | _ -> ()
1498 end;
1499 preload layout;
1501 state.ghyll <- noghyll;
1504 let conttiling pageno opaque =
1505 tilepage pageno opaque
1506 (if conf.preload then preloadlayout state.layout else state.layout)
1509 let gotoy_and_clear_text y =
1510 gotoy y;
1511 if not conf.verbose then state.text <- "";
1514 let getanchor () =
1515 match state.layout with
1516 | [] -> emptyanchor
1517 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1520 let getanchory (n, top) =
1521 let y, h = getpageyh n in
1522 y + (truncate (top *. float h));
1525 let gotoanchor anchor =
1526 gotoy (getanchory anchor);
1529 let addnav () =
1530 cbput state.hists.nav (getanchor ());
1533 let getnav dir =
1534 let anchor = cbgetc state.hists.nav dir in
1535 getanchory anchor;
1538 let gotoghyll y =
1539 let rec scroll f n a b =
1540 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1541 let snake f a b =
1542 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1543 if f < a
1544 then s (float f /. float a)
1545 else (
1546 if f > b
1547 then 1.0 -. s ((float (f-b) /. float (n-b)))
1548 else 1.0
1551 snake f a b
1552 and summa f n a b =
1553 (* courtesy:
1554 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1555 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1556 let iv1 = iv f in
1557 let ins = float a *. iv1
1558 and outs = float (n-b) *. iv1 in
1559 let ones = b - a in
1560 ins +. outs +. float ones
1562 let rec set (_N, _A, _B) y sy =
1563 let sum = summa 1.0 _N _A _B in
1564 let dy = float (y - sy) in
1565 state.ghyll <- (
1566 let rec gf n y1 o =
1567 if n >= _N
1568 then state.ghyll <- noghyll
1569 else
1570 let go n =
1571 let s = scroll n _N _A _B in
1572 let y1 = y1 +. ((s *. dy) /. sum) in
1573 gotoy_and_clear_text (truncate y1);
1574 state.ghyll <- gf (n+1) y1;
1576 match o with
1577 | None -> go n
1578 | Some y' -> set (_N/2, 0, 0) y' state.y
1580 gf 0 (float state.y)
1583 match conf.ghyllscroll with
1584 | None ->
1585 gotoy_and_clear_text y
1586 | Some nab ->
1587 if state.ghyll == noghyll
1588 then set nab y state.y
1589 else state.ghyll (Some y)
1592 let gotopage n top =
1593 let y, h = getpageyh n in
1594 let y = y + (truncate (top *. float h)) in
1595 gotoghyll y
1598 let gotopage1 n top =
1599 let y = getpagey n in
1600 let y = y + top in
1601 gotoghyll y
1604 let invalidate () =
1605 state.layout <- [];
1606 state.pdims <- [];
1607 state.rects <- [];
1608 state.rects1 <- [];
1609 state.invalidated <- state.invalidated + 1;
1612 let writeopen path password =
1613 wcmd "open %s\000%s\000" path password;
1616 let opendoc path password =
1617 invalidate ();
1618 state.path <- path;
1619 state.password <- password;
1620 state.gen <- state.gen + 1;
1621 state.docinfo <- [];
1623 setaalevel conf.aalevel;
1624 writeopen path password;
1625 Wsi.settitle ("llpp " ^ Filename.basename path);
1626 wcmd "geometry %d %d" state.w conf.winh;
1629 let scalecolor c =
1630 let c = c *. conf.colorscale in
1631 (c, c, c);
1634 let scalecolor2 (r, g, b) =
1635 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1638 let represent () =
1639 let docolumns = function
1640 | None -> ()
1641 | Some ((columns, coverA, coverB), _) ->
1642 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1643 let rec loop pageno pdimno pdim x y rowh pdims =
1644 if pageno = state.pagecount
1645 then ()
1646 else
1647 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1648 match pdims with
1649 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1650 pdimno+1, pdim, rest
1651 | _ ->
1652 pdimno, pdim, pdims
1654 let x, y, rowh' =
1655 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1656 then (
1657 (conf.winw - state.scrollw - w) / 2,
1658 y + rowh + conf.interpagespace, h
1660 else (
1661 if (pageno - coverA) mod columns = 0
1662 then 0, y + rowh + conf.interpagespace, h
1663 else x, y, max rowh h
1666 let rec fixrow m = if m = pageno then () else
1667 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1668 if h < rowh
1669 then (
1670 let y = y + (rowh - h) / 2 in
1671 a.(m) <- (pdimno, x, y, pdim);
1673 fixrow (m+1)
1675 if pageno > 1 && (pageno - coverA) mod columns = 0
1676 then fixrow (pageno - columns);
1677 a.(pageno) <- (pdimno, x, y, pdim);
1678 let x = x + w + xoff*2 + conf.interpagespace in
1679 loop (pageno+1) pdimno pdim x y rowh' pdims
1681 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1682 conf.columns <- Some ((columns, coverA, coverB), a);
1684 docolumns conf.columns;
1685 state.maxy <- calcheight ();
1686 state.hscrollh <-
1687 if state.w <= conf.winw - state.scrollw
1688 then 0
1689 else state.scrollw
1691 match state.mode with
1692 | Birdseye (_, _, pageno, _, _) ->
1693 let y, h = getpageyh pageno in
1694 let top = (conf.winh - h) / 2 in
1695 gotoy (max 0 (y - top))
1696 | _ -> gotoanchor state.anchor
1699 let reshape =
1700 let firsttime = ref true in
1701 fun ~w ~h ->
1702 GlDraw.viewport 0 0 w h;
1703 if state.invalidated = 0 && not !firsttime
1704 then state.anchor <- getanchor ();
1706 firsttime := false;
1707 conf.winw <- w;
1708 let w = truncate (float w *. conf.zoom) - state.scrollw in
1709 let w = max w 2 in
1710 state.w <- w;
1711 conf.winh <- h;
1712 setfontsize fstate.fontsize;
1713 GlMat.mode `modelview;
1714 GlMat.load_identity ();
1716 GlMat.mode `projection;
1717 GlMat.load_identity ();
1718 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1719 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1720 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1722 let w =
1723 match conf.columns with
1724 | None -> w
1725 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1727 invalidate ();
1728 wcmd "geometry %d %d" w h;
1731 let enttext () =
1732 let len = String.length state.text in
1733 let drawstring s =
1734 let hscrollh =
1735 match state.mode with
1736 | View -> state.hscrollh
1737 | _ -> 0
1739 let rect x w =
1740 GlDraw.rect
1741 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1742 (x+.w, float (conf.winh - hscrollh))
1745 let w = float (conf.winw - state.scrollw - 1) in
1746 if state.progress >= 0.0 && state.progress < 1.0
1747 then (
1748 GlDraw.color (0.3, 0.3, 0.3);
1749 let w1 = w *. state.progress in
1750 rect 0.0 w1;
1751 GlDraw.color (0.0, 0.0, 0.0);
1752 rect w1 (w-.w1)
1754 else (
1755 GlDraw.color (0.0, 0.0, 0.0);
1756 rect 0.0 w;
1759 GlDraw.color (1.0, 1.0, 1.0);
1760 drawstring fstate.fontsize
1761 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1763 let s =
1764 match state.mode with
1765 | Textentry ((prefix, text, _, _, _), _) ->
1766 let s =
1767 if len > 0
1768 then
1769 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1770 else
1771 Printf.sprintf "%s%s_" prefix text
1775 | _ -> state.text
1777 let s =
1778 if state.newerrmsgs
1779 then (
1780 if not (istextentry state.mode)
1781 then
1782 let s1 = "(press 'e' to review error messasges)" in
1783 if String.length s > 0 then s ^ " " ^ s1 else s1
1784 else s
1786 else s
1788 if String.length s > 0
1789 then drawstring s
1792 let gctiles () =
1793 let len = Queue.length state.tilelru in
1794 let rec loop qpos =
1795 if state.memused <= conf.memlimit
1796 then ()
1797 else (
1798 if qpos < len
1799 then
1800 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1801 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1802 let (_, pw, ph, _) = getpagedim n in
1804 gen = state.gen
1805 && colorspace = conf.colorspace
1806 && angle = conf.angle
1807 && pagew = pw
1808 && pageh = ph
1809 && (
1810 let layout =
1811 match state.throttle with
1812 | None ->
1813 if conf.preload
1814 then preloadlayout state.layout
1815 else state.layout
1816 | Some (layout, _, _) ->
1817 layout
1819 let x = col*conf.tilew
1820 and y = row*conf.tileh in
1821 tilevisible layout n x y
1823 then Queue.push lruitem state.tilelru
1824 else (
1825 wcmd "freetile %s" p;
1826 state.memused <- state.memused - s;
1827 state.uioh#infochanged Memused;
1828 Hashtbl.remove state.tilemap k;
1830 loop (qpos+1)
1833 loop 0
1836 let flushtiles () =
1837 Queue.iter (fun (k, p, s) ->
1838 wcmd "freetile %s" p;
1839 state.memused <- state.memused - s;
1840 state.uioh#infochanged Memused;
1841 Hashtbl.remove state.tilemap k;
1842 ) state.tilelru;
1843 Queue.clear state.tilelru;
1844 load state.layout;
1847 let logcurrently = function
1848 | Idle -> dolog "Idle"
1849 | Loading (l, gen) ->
1850 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1851 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1852 dolog
1853 "Tiling %d[%d,%d] page=%s cs=%s angle"
1854 l.pageno col row pageopaque
1855 (colorspace_to_string colorspace)
1857 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1858 angle gen conf.angle state.gen
1859 tilew tileh
1860 conf.tilew conf.tileh
1862 | Outlining _ ->
1863 dolog "outlining"
1866 let act cmds =
1867 (* dolog "%S" cmds; *)
1868 let op, args =
1869 let spacepos =
1870 try String.index cmds ' '
1871 with Not_found -> -1
1873 if spacepos = -1
1874 then cmds, ""
1875 else
1876 let l = String.length cmds in
1877 let op = String.sub cmds 0 spacepos in
1878 op, begin
1879 if l - spacepos < 2 then ""
1880 else String.sub cmds (spacepos+1) (l-spacepos-1)
1883 match op with
1884 | "clear" ->
1885 state.uioh#infochanged Pdim;
1886 state.pdims <- [];
1888 | "clearrects" ->
1889 state.rects <- state.rects1;
1890 G.postRedisplay "clearrects";
1892 | "continue" ->
1893 let n =
1894 try Scanf.sscanf args "%u" (fun n -> n)
1895 with exn ->
1896 dolog "error processing 'continue' %S: %s"
1897 cmds (Printexc.to_string exn);
1898 exit 1;
1900 state.pagecount <- n;
1901 state.invalidated <- state.invalidated - 1;
1902 begin match state.currently with
1903 | Outlining l ->
1904 state.currently <- Idle;
1905 state.outlines <- Array.of_list (List.rev l)
1906 | _ -> ()
1907 end;
1908 if state.invalidated = 0
1909 then represent ();
1910 if conf.maxwait = None
1911 then G.postRedisplay "continue";
1913 | "title" ->
1914 Wsi.settitle args
1916 | "msg" ->
1917 showtext ' ' args
1919 | "vmsg" ->
1920 if conf.verbose
1921 then showtext ' ' args
1923 | "progress" ->
1924 let progress, text =
1926 Scanf.sscanf args "%f %n"
1927 (fun f pos ->
1928 f, String.sub args pos (String.length args - pos))
1929 with exn ->
1930 dolog "error processing 'progress' %S: %s"
1931 cmds (Printexc.to_string exn);
1932 exit 1;
1934 state.text <- text;
1935 state.progress <- progress;
1936 G.postRedisplay "progress"
1938 | "firstmatch" ->
1939 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1941 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1942 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1943 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1944 with exn ->
1945 dolog "error processing 'firstmatch' %S: %s"
1946 cmds (Printexc.to_string exn);
1947 exit 1;
1949 let y = (getpagey pageno) + truncate y0 in
1950 addnav ();
1951 gotoy y;
1952 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1954 | "match" ->
1955 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1957 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1958 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1959 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1960 with exn ->
1961 dolog "error processing 'match' %S: %s"
1962 cmds (Printexc.to_string exn);
1963 exit 1;
1965 state.rects1 <-
1966 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1968 | "page" ->
1969 let pageopaque, t =
1971 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1972 with exn ->
1973 dolog "error processing 'page' %S: %s"
1974 cmds (Printexc.to_string exn);
1975 exit 1;
1977 begin match state.currently with
1978 | Loading (l, gen) ->
1979 vlog "page %d took %f sec" l.pageno t;
1980 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1981 begin match state.throttle with
1982 | None ->
1983 let preloadedpages =
1984 if conf.preload
1985 then preloadlayout state.layout
1986 else state.layout
1988 let evict () =
1989 let module IntSet =
1990 Set.Make (struct type t = int let compare = (-) end) in
1991 let set =
1992 List.fold_left (fun s l -> IntSet.add l.pageno s)
1993 IntSet.empty preloadedpages
1995 let evictedpages =
1996 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1997 if not (IntSet.mem pageno set)
1998 then (
1999 wcmd "freepage %s" opaque;
2000 key :: accu
2002 else accu
2003 ) state.pagemap []
2005 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2007 evict ();
2008 state.currently <- Idle;
2009 if gen = state.gen
2010 then (
2011 tilepage l.pageno pageopaque state.layout;
2012 load state.layout;
2013 load preloadedpages;
2014 if pagevisible state.layout l.pageno
2015 && layoutready state.layout
2016 then G.postRedisplay "page";
2019 | Some (layout, _, _) ->
2020 state.currently <- Idle;
2021 tilepage l.pageno pageopaque layout;
2022 load state.layout
2023 end;
2025 | _ ->
2026 dolog "Inconsistent loading state";
2027 logcurrently state.currently;
2028 exit 1
2031 | "tile" ->
2032 let (x, y, opaque, size, t) =
2034 Scanf.sscanf args "%u %u %s %u %f"
2035 (fun x y p size t -> (x, y, p, size, t))
2036 with exn ->
2037 dolog "error processing 'tile' %S: %s"
2038 cmds (Printexc.to_string exn);
2039 exit 1;
2041 begin match state.currently with
2042 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2043 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2045 if tilew != conf.tilew || tileh != conf.tileh
2046 then (
2047 wcmd "freetile %s" opaque;
2048 state.currently <- Idle;
2049 load state.layout;
2051 else (
2052 puttileopaque l col row gen cs angle opaque size t;
2053 state.memused <- state.memused + size;
2054 state.uioh#infochanged Memused;
2055 gctiles ();
2056 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2057 opaque, size) state.tilelru;
2059 let layout =
2060 match state.throttle with
2061 | None -> state.layout
2062 | Some (layout, _, _) -> layout
2065 state.currently <- Idle;
2066 if gen = state.gen
2067 && conf.colorspace = cs
2068 && conf.angle = angle
2069 && tilevisible layout l.pageno x y
2070 then conttiling l.pageno pageopaque;
2072 begin match state.throttle with
2073 | None ->
2074 preload state.layout;
2075 if gen = state.gen
2076 && conf.colorspace = cs
2077 && conf.angle = angle
2078 && tilevisible state.layout l.pageno x y
2079 then G.postRedisplay "tile nothrottle";
2081 | Some (layout, y, _) ->
2082 let ready = layoutready layout in
2083 if ready
2084 then (
2085 state.y <- y;
2086 state.layout <- layout;
2087 state.throttle <- None;
2088 G.postRedisplay "throttle";
2090 else load layout;
2091 end;
2094 | _ ->
2095 dolog "Inconsistent tiling state";
2096 logcurrently state.currently;
2097 exit 1
2100 | "pdim" ->
2101 let pdim =
2103 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2104 with exn ->
2105 dolog "error processing 'pdim' %S: %s"
2106 cmds (Printexc.to_string exn);
2107 exit 1;
2109 state.uioh#infochanged Pdim;
2110 state.pdims <- pdim :: state.pdims
2112 | "o" ->
2113 let (l, n, t, h, pos) =
2115 Scanf.sscanf args "%u %u %d %u %n"
2116 (fun l n t h pos -> l, n, t, h, pos)
2117 with exn ->
2118 dolog "error processing 'o' %S: %s"
2119 cmds (Printexc.to_string exn);
2120 exit 1;
2122 let s = String.sub args pos (String.length args - pos) in
2123 let outline = (s, l, (n, float t /. float h)) in
2124 begin match state.currently with
2125 | Outlining outlines ->
2126 state.currently <- Outlining (outline :: outlines)
2127 | Idle ->
2128 state.currently <- Outlining [outline]
2129 | currently ->
2130 dolog "invalid outlining state";
2131 logcurrently currently
2134 | "info" ->
2135 state.docinfo <- (1, args) :: state.docinfo
2137 | "infoend" ->
2138 state.uioh#infochanged Docinfo;
2139 state.docinfo <- List.rev state.docinfo
2141 | _ ->
2142 dolog "unknown cmd `%S'" cmds
2145 let onhist cb =
2146 let rc = cb.rc in
2147 let action = function
2148 | HCprev -> cbget cb ~-1
2149 | HCnext -> cbget cb 1
2150 | HCfirst -> cbget cb ~-(cb.rc)
2151 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2152 and cancel () = cb.rc <- rc
2153 in (action, cancel)
2156 let search pattern forward =
2157 if String.length pattern > 0
2158 then
2159 let pn, py =
2160 match state.layout with
2161 | [] -> 0, 0
2162 | l :: _ ->
2163 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2165 wcmd "search %d %d %d %d,%s\000"
2166 (btod conf.icase) pn py (btod forward) pattern;
2169 let intentry text key =
2170 let c =
2171 if key >= 32 && key < 127
2172 then Char.chr key
2173 else '\000'
2175 match c with
2176 | '0' .. '9' ->
2177 let text = addchar text c in
2178 TEcont text
2180 | _ ->
2181 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2182 TEcont text
2185 let textentry text key =
2186 if key land 0xff00 = 0xff00
2187 then TEcont text
2188 else TEcont (text ^ Wsi.toutf8 key)
2191 let reqlayout angle proportional =
2192 match state.throttle with
2193 | None ->
2194 if state.invalidated = 0 then state.anchor <- getanchor ();
2195 conf.angle <- angle mod 360;
2196 conf.proportional <- proportional;
2197 invalidate ();
2198 wcmd "reqlayout %d %d" conf.angle (btod proportional);
2199 | _ -> ()
2202 let settrim trimmargins trimfuzz =
2203 if state.invalidated = 0 then state.anchor <- getanchor ();
2204 conf.trimmargins <- trimmargins;
2205 conf.trimfuzz <- trimfuzz;
2206 let x0, y0, x1, y1 = trimfuzz in
2207 invalidate ();
2208 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
2209 Hashtbl.iter (fun _ opaque ->
2210 wcmd "freepage %s" opaque;
2211 ) state.pagemap;
2212 Hashtbl.clear state.pagemap;
2215 let setzoom zoom =
2216 match state.throttle with
2217 | None ->
2218 let zoom = max 0.01 zoom in
2219 if zoom <> conf.zoom
2220 then (
2221 state.prevzoom <- conf.zoom;
2222 let relx =
2223 if zoom <= 1.0
2224 then (state.x <- 0; 0.0)
2225 else float state.x /. float state.w
2227 conf.zoom <- zoom;
2228 reshape conf.winw conf.winh;
2229 if zoom > 1.0
2230 then (
2231 let x = relx *. float state.w in
2232 state.x <- truncate x;
2234 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2237 | Some (layout, y, started) ->
2238 let time =
2239 match conf.maxwait with
2240 | None -> 0.0
2241 | Some t -> t
2243 let dt = now () -. started in
2244 if dt > time
2245 then (
2246 state.y <- y;
2247 load layout;
2251 let setcolumns columns coverA coverB =
2252 if columns < 2
2253 then (
2254 conf.columns <- None;
2255 state.x <- 0;
2256 setzoom 1.0;
2258 else (
2259 conf.columns <- Some ((columns, coverA, coverB), [||]);
2260 conf.zoom <- 1.0;
2262 reshape conf.winw conf.winh;
2265 let enterbirdseye () =
2266 let zoom = float conf.thumbw /. float conf.winw in
2267 let birdseyepageno =
2268 let cy = conf.winh / 2 in
2269 let fold = function
2270 | [] -> 0
2271 | l :: rest ->
2272 let rec fold best = function
2273 | [] -> best.pageno
2274 | l :: rest ->
2275 let d = cy - (l.pagedispy + l.pagevh/2)
2276 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2277 if abs d < abs dbest
2278 then fold l rest
2279 else best.pageno
2280 in fold l rest
2282 fold state.layout
2284 state.mode <- Birdseye (
2285 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2287 conf.zoom <- zoom;
2288 conf.presentation <- false;
2289 conf.interpagespace <- 10;
2290 conf.hlinks <- false;
2291 state.x <- 0;
2292 state.mstate <- Mnone;
2293 conf.maxwait <- None;
2294 conf.columns <- (
2295 match conf.beyecolumns with
2296 | Some c ->
2297 conf.zoom <- 1.0;
2298 Some ((c, 0, 0), [||])
2299 | None -> None
2301 Wsi.setcursor Wsi.CURSOR_INHERIT;
2302 if conf.verbose
2303 then
2304 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2305 (100.0*.zoom)
2306 else
2307 state.text <- ""
2309 reshape conf.winw conf.winh;
2312 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2313 state.mode <- View;
2314 conf.zoom <- c.zoom;
2315 conf.presentation <- c.presentation;
2316 conf.interpagespace <- c.interpagespace;
2317 conf.maxwait <- c.maxwait;
2318 conf.hlinks <- c.hlinks;
2319 conf.beyecolumns <- (
2320 match conf.columns with
2321 | Some ((c, _, _), _) -> Some c
2322 | None -> None
2324 conf.columns <- (
2325 match c.columns with
2326 | Some (c, _) -> Some (c, [||])
2327 | None -> None
2329 state.x <- leftx;
2330 if conf.verbose
2331 then
2332 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2333 (100.0*.conf.zoom)
2335 reshape conf.winw conf.winh;
2336 state.anchor <- if goback then anchor else (pageno, 0.0);
2339 let togglebirdseye () =
2340 match state.mode with
2341 | Birdseye vals -> leavebirdseye vals true
2342 | View -> enterbirdseye ()
2343 | _ -> ()
2346 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2347 let pageno = max 0 (pageno - incr) in
2348 let rec loop = function
2349 | [] -> gotopage1 pageno 0
2350 | l :: _ when l.pageno = pageno ->
2351 if l.pagedispy >= 0 && l.pagey = 0
2352 then G.postRedisplay "upbirdseye"
2353 else gotopage1 pageno 0
2354 | _ :: rest -> loop rest
2356 loop state.layout;
2357 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2360 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2361 let pageno = min (state.pagecount - 1) (pageno + incr) in
2362 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2363 let rec loop = function
2364 | [] ->
2365 let y, h = getpageyh pageno in
2366 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2367 gotoy (clamp dy)
2368 | l :: _ when l.pageno = pageno ->
2369 if l.pagevh != l.pageh
2370 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2371 else G.postRedisplay "downbirdseye"
2372 | _ :: rest -> loop rest
2374 loop state.layout
2377 let optentry mode _ key =
2378 let btos b = if b then "on" else "off" in
2379 if key >= 32 && key < 127
2380 then
2381 let c = Char.chr key in
2382 match c with
2383 | 's' ->
2384 let ondone s =
2385 try conf.scrollstep <- int_of_string s with exc ->
2386 state.text <- Printf.sprintf "bad integer `%s': %s"
2387 s (Printexc.to_string exc)
2389 TEswitch ("scroll step: ", "", None, intentry, ondone)
2391 | 'A' ->
2392 let ondone s =
2394 conf.autoscrollstep <- int_of_string s;
2395 if state.autoscroll <> None
2396 then state.autoscroll <- Some conf.autoscrollstep
2397 with exc ->
2398 state.text <- Printf.sprintf "bad integer `%s': %s"
2399 s (Printexc.to_string exc)
2401 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2403 | 'C' ->
2404 let ondone s =
2406 let n, a, b = columns_of_string s in
2407 setcolumns n a b;
2408 with exc ->
2409 state.text <- Printf.sprintf "bad columns `%s': %s"
2410 s (Printexc.to_string exc)
2412 TEswitch ("columns: ", "", None, textentry, ondone)
2414 | 'Z' ->
2415 let ondone s =
2417 let zoom = float (int_of_string s) /. 100.0 in
2418 setzoom zoom
2419 with exc ->
2420 state.text <- Printf.sprintf "bad integer `%s': %s"
2421 s (Printexc.to_string exc)
2423 TEswitch ("zoom: ", "", None, intentry, ondone)
2425 | 't' ->
2426 let ondone s =
2428 conf.thumbw <- bound (int_of_string s) 2 4096;
2429 state.text <-
2430 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2431 begin match mode with
2432 | Birdseye beye ->
2433 leavebirdseye beye false;
2434 enterbirdseye ();
2435 | _ -> ();
2437 with exc ->
2438 state.text <- Printf.sprintf "bad integer `%s': %s"
2439 s (Printexc.to_string exc)
2441 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2443 | 'R' ->
2444 let ondone s =
2445 match try
2446 Some (int_of_string s)
2447 with exc ->
2448 state.text <- Printf.sprintf "bad integer `%s': %s"
2449 s (Printexc.to_string exc);
2450 None
2451 with
2452 | Some angle -> reqlayout angle conf.proportional
2453 | None -> ()
2455 TEswitch ("rotation: ", "", None, intentry, ondone)
2457 | 'i' ->
2458 conf.icase <- not conf.icase;
2459 TEdone ("case insensitive search " ^ (btos conf.icase))
2461 | 'p' ->
2462 conf.preload <- not conf.preload;
2463 gotoy state.y;
2464 TEdone ("preload " ^ (btos conf.preload))
2466 | 'v' ->
2467 conf.verbose <- not conf.verbose;
2468 TEdone ("verbose " ^ (btos conf.verbose))
2470 | 'd' ->
2471 conf.debug <- not conf.debug;
2472 TEdone ("debug " ^ (btos conf.debug))
2474 | 'h' ->
2475 conf.maxhfit <- not conf.maxhfit;
2476 state.maxy <-
2477 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2478 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2480 | 'c' ->
2481 conf.crophack <- not conf.crophack;
2482 TEdone ("crophack " ^ btos conf.crophack)
2484 | 'a' ->
2485 let s =
2486 match conf.maxwait with
2487 | None ->
2488 conf.maxwait <- Some infinity;
2489 "always wait for page to complete"
2490 | Some _ ->
2491 conf.maxwait <- None;
2492 "show placeholder if page is not ready"
2494 TEdone s
2496 | 'f' ->
2497 conf.underinfo <- not conf.underinfo;
2498 TEdone ("underinfo " ^ btos conf.underinfo)
2500 | 'P' ->
2501 conf.savebmarks <- not conf.savebmarks;
2502 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2504 | 'S' ->
2505 let ondone s =
2507 let pageno, py =
2508 match state.layout with
2509 | [] -> 0, 0
2510 | l :: _ ->
2511 l.pageno, l.pagey
2513 conf.interpagespace <- int_of_string s;
2514 state.maxy <- calcheight ();
2515 let y = getpagey pageno in
2516 gotoy (y + py)
2517 with exc ->
2518 state.text <- Printf.sprintf "bad integer `%s': %s"
2519 s (Printexc.to_string exc)
2521 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2523 | 'l' ->
2524 reqlayout conf.angle (not conf.proportional);
2525 TEdone ("proportional display " ^ btos conf.proportional)
2527 | 'T' ->
2528 settrim (not conf.trimmargins) conf.trimfuzz;
2529 TEdone ("trim margins " ^ btos conf.trimmargins)
2531 | 'I' ->
2532 conf.invert <- not conf.invert;
2533 TEdone ("invert colors " ^ btos conf.invert)
2535 | 'x' ->
2536 let ondone s =
2537 cbput state.hists.sel s;
2538 conf.selcmd <- s;
2540 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2541 textentry, ondone)
2543 | _ ->
2544 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2545 TEstop
2546 else
2547 TEcont state.text
2550 class type lvsource = object
2551 method getitemcount : int
2552 method getitem : int -> (string * int)
2553 method hasaction : int -> bool
2554 method exit :
2555 uioh:uioh ->
2556 cancel:bool ->
2557 active:int ->
2558 first:int ->
2559 pan:int ->
2560 qsearch:string ->
2561 uioh option
2562 method getactive : int
2563 method getfirst : int
2564 method getqsearch : string
2565 method setqsearch : string -> unit
2566 method getpan : int
2567 end;;
2569 class virtual lvsourcebase = object
2570 val mutable m_active = 0
2571 val mutable m_first = 0
2572 val mutable m_qsearch = ""
2573 val mutable m_pan = 0
2574 method getactive = m_active
2575 method getfirst = m_first
2576 method getqsearch = m_qsearch
2577 method getpan = m_pan
2578 method setqsearch s = m_qsearch <- s
2579 end;;
2581 let withoutlastutf8 s =
2582 let len = String.length s in
2583 if len = 0
2584 then s
2585 else
2586 let rec find pos =
2587 if pos = 0
2588 then pos
2589 else
2590 let b = Char.code s.[pos] in
2591 if b land 0b110000 = 0b11000000
2592 then find (pos-1)
2593 else pos-1
2595 let first =
2596 if Char.code s.[len-1] land 0x80 = 0
2597 then len-1
2598 else find (len-1)
2600 String.sub s 0 first;
2603 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2604 let enttext te =
2605 state.mode <- Textentry (te, onleave);
2606 state.text <- "";
2607 enttext ();
2608 G.postRedisplay "textentrykeyboard enttext";
2610 let histaction cmd =
2611 match opthist with
2612 | None -> ()
2613 | Some (action, _) ->
2614 state.mode <- Textentry (
2615 (c, action cmd, opthist, onkey, ondone), onleave
2617 G.postRedisplay "textentry histaction"
2619 match key with
2620 | 0xff08 -> (* backspace *)
2621 let s = withoutlastutf8 text in
2622 let len = String.length s in
2623 if len = 0
2624 then (
2625 onleave Cancel;
2626 G.postRedisplay "textentrykeyboard after cancel";
2628 else (
2629 enttext (c, s, opthist, onkey, ondone)
2632 | 0xff0d ->
2633 ondone text;
2634 onleave Confirm;
2635 G.postRedisplay "textentrykeyboard after confirm"
2637 | 0xff52 -> histaction HCprev
2638 | 0xff54 -> histaction HCnext
2639 | 0xff50 -> histaction HCfirst
2640 | 0xff57 -> histaction HClast
2642 | 0xff1b -> (* escape*)
2643 if String.length text = 0
2644 then (
2645 begin match opthist with
2646 | None -> ()
2647 | Some (_, onhistcancel) -> onhistcancel ()
2648 end;
2649 onleave Cancel;
2650 state.text <- "";
2651 G.postRedisplay "textentrykeyboard after cancel2"
2653 else (
2654 enttext (c, "", opthist, onkey, ondone)
2657 | 0xff9f | 0xffff -> () (* delete *)
2659 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2660 begin match onkey text key with
2661 | TEdone text ->
2662 ondone text;
2663 onleave Confirm;
2664 G.postRedisplay "textentrykeyboard after confirm2";
2666 | TEcont text ->
2667 enttext (c, text, opthist, onkey, ondone);
2669 | TEstop ->
2670 onleave Cancel;
2671 G.postRedisplay "textentrykeyboard after cancel3"
2673 | TEswitch te ->
2674 state.mode <- Textentry (te, onleave);
2675 G.postRedisplay "textentrykeyboard switch";
2676 end;
2678 | _ ->
2679 vlog "unhandled key %s" (Wsi.keyname key)
2682 let firstof first active =
2683 if first > active || abs (first - active) > fstate.maxrows - 1
2684 then max 0 (active - (fstate.maxrows/2))
2685 else first
2688 let calcfirst first active =
2689 if active > first
2690 then
2691 let rows = active - first in
2692 if rows > fstate.maxrows then active - fstate.maxrows else first
2693 else active
2696 let scrollph y maxy =
2697 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2698 let sh = float conf.winh /. sh in
2699 let sh = max sh (float conf.scrollh) in
2701 let percent =
2702 if y = state.maxy
2703 then 1.0
2704 else float y /. float maxy
2706 let position = (float conf.winh -. sh) *. percent in
2708 let position =
2709 if position +. sh > float conf.winh
2710 then float conf.winh -. sh
2711 else position
2713 position, sh;
2716 let coe s = (s :> uioh);;
2718 class listview ~(source:lvsource) ~trusted ~modehash =
2719 object (self)
2720 val m_pan = source#getpan
2721 val m_first = source#getfirst
2722 val m_active = source#getactive
2723 val m_qsearch = source#getqsearch
2724 val m_prev_uioh = state.uioh
2726 method private elemunder y =
2727 let n = y / (fstate.fontsize+1) in
2728 if m_first + n < source#getitemcount
2729 then (
2730 if source#hasaction (m_first + n)
2731 then Some (m_first + n)
2732 else None
2734 else None
2736 method display =
2737 Gl.enable `blend;
2738 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2739 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2740 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2741 GlDraw.color (1., 1., 1.);
2742 Gl.enable `texture_2d;
2743 let fs = fstate.fontsize in
2744 let nfs = fs + 1 in
2745 let ww = fstate.wwidth in
2746 let tabw = 30.0*.ww in
2747 let itemcount = source#getitemcount in
2748 let rec loop row =
2749 if (row - m_first) * nfs > conf.winh
2750 then ()
2751 else (
2752 if row >= 0 && row < itemcount
2753 then (
2754 let (s, level) = source#getitem row in
2755 let y = (row - m_first) * nfs in
2756 let x = 5.0 +. float (level + m_pan) *. ww in
2757 if row = m_active
2758 then (
2759 Gl.disable `texture_2d;
2760 GlDraw.polygon_mode `both `line;
2761 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2762 GlDraw.rect (1., float (y + 1))
2763 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2764 GlDraw.polygon_mode `both `fill;
2765 GlDraw.color (1., 1., 1.);
2766 Gl.enable `texture_2d;
2769 let drawtabularstring s =
2770 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2771 if trusted
2772 then
2773 let tabpos = try String.index s '\t' with Not_found -> -1 in
2774 if tabpos > 0
2775 then
2776 let len = String.length s - tabpos - 1 in
2777 let s1 = String.sub s 0 tabpos
2778 and s2 = String.sub s (tabpos + 1) len in
2779 let nx = drawstr x s1 in
2780 let sw = nx -. x in
2781 let x = x +. (max tabw sw) in
2782 drawstr x s2
2783 else
2784 drawstr x s
2785 else
2786 drawstr x s
2788 let _ = drawtabularstring s in
2789 loop (row+1)
2793 loop m_first;
2794 Gl.disable `blend;
2795 Gl.disable `texture_2d;
2797 method updownlevel incr =
2798 let len = source#getitemcount in
2799 let curlevel =
2800 if m_active >= 0 && m_active < len
2801 then snd (source#getitem m_active)
2802 else -1
2804 let rec flow i =
2805 if i = len then i-1 else if i = -1 then 0 else
2806 let _, l = source#getitem i in
2807 if l != curlevel then i else flow (i+incr)
2809 let active = flow m_active in
2810 let first = calcfirst m_first active in
2811 G.postRedisplay "outline updownlevel";
2812 {< m_active = active; m_first = first >}
2814 method private key1 key mask =
2815 let set1 active first qsearch =
2816 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2818 let search active pattern incr =
2819 let dosearch re =
2820 let rec loop n =
2821 if n >= 0 && n < source#getitemcount
2822 then (
2823 let s, _ = source#getitem n in
2825 (try ignore (Str.search_forward re s 0); true
2826 with Not_found -> false)
2827 then Some n
2828 else loop (n + incr)
2830 else None
2832 loop active
2835 let re = Str.regexp_case_fold pattern in
2836 dosearch re
2837 with Failure s ->
2838 state.text <- s;
2839 None
2841 let itemcount = source#getitemcount in
2842 let find start incr =
2843 let rec find i =
2844 if i = -1 || i = itemcount
2845 then -1
2846 else (
2847 if source#hasaction i
2848 then i
2849 else find (i + incr)
2852 find start
2854 let set active first =
2855 let first = bound first 0 (itemcount - fstate.maxrows) in
2856 state.text <- "";
2857 coe {< m_active = active; m_first = first >}
2859 let navigate incr =
2860 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2861 let active, first =
2862 let incr1 = if incr > 0 then 1 else -1 in
2863 if isvisible m_first m_active
2864 then
2865 let next =
2866 let next = m_active + incr in
2867 let next =
2868 if next < 0 || next >= itemcount
2869 then -1
2870 else find next incr1
2872 if next = -1 || abs (m_active - next) > fstate.maxrows
2873 then -1
2874 else next
2876 if next = -1
2877 then
2878 let first = m_first + incr in
2879 let first = bound first 0 (itemcount - 1) in
2880 let next =
2881 let next = m_active + incr in
2882 let next = bound next 0 (itemcount - 1) in
2883 find next ~-incr1
2885 let active = if next = -1 then m_active else next in
2886 active, first
2887 else
2888 let first = min next m_first in
2889 let first =
2890 if abs (next - first) > fstate.maxrows
2891 then first + incr
2892 else first
2894 next, first
2895 else
2896 let first = m_first + incr in
2897 let first = bound first 0 (itemcount - 1) in
2898 let active =
2899 let next = m_active + incr in
2900 let next = bound next 0 (itemcount - 1) in
2901 let next = find next incr1 in
2902 let active =
2903 if next = -1 || abs (m_active - first) > fstate.maxrows
2904 then (
2905 let active = if m_active = -1 then next else m_active in
2906 active
2908 else next
2910 if isvisible first active
2911 then active
2912 else -1
2914 active, first
2916 G.postRedisplay "listview navigate";
2917 set active first;
2919 match key with
2920 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2921 let incr = if key = 0x72 then -1 else 1 in
2922 let active, first =
2923 match search (m_active + incr) m_qsearch incr with
2924 | None ->
2925 state.text <- m_qsearch ^ " [not found]";
2926 m_active, m_first
2927 | Some active ->
2928 state.text <- m_qsearch;
2929 active, firstof m_first active
2931 G.postRedisplay "listview ctrl-r/s";
2932 set1 active first m_qsearch;
2934 | 0xff08 -> (* backspace *)
2935 if String.length m_qsearch = 0
2936 then coe self
2937 else (
2938 let qsearch = withoutlastutf8 m_qsearch in
2939 let len = String.length qsearch in
2940 if len = 0
2941 then (
2942 state.text <- "";
2943 G.postRedisplay "listview empty qsearch";
2944 set1 m_active m_first "";
2946 else
2947 let active, first =
2948 match search m_active qsearch ~-1 with
2949 | None ->
2950 state.text <- qsearch ^ " [not found]";
2951 m_active, m_first
2952 | Some active ->
2953 state.text <- qsearch;
2954 active, firstof m_first active
2956 G.postRedisplay "listview backspace qsearch";
2957 set1 active first qsearch
2960 | key when ((key >= 32 && key < 127)
2961 || (key != 0 && key land 0xff00 != 0xff00)) ->
2962 let pattern =
2963 if key >= 32 && key < 127
2964 then addchar m_qsearch (Char.chr key)
2965 else m_qsearch ^ Wsi.toutf8 key
2967 let active, first =
2968 match search m_active pattern 1 with
2969 | None ->
2970 state.text <- pattern ^ " [not found]";
2971 m_active, m_first
2972 | Some active ->
2973 state.text <- pattern;
2974 active, firstof m_first active
2976 G.postRedisplay "listview qsearch add";
2977 set1 active first pattern;
2979 | 0xff1b -> (* escape *)
2980 state.text <- "";
2981 if String.length m_qsearch = 0
2982 then (
2983 G.postRedisplay "list view escape";
2984 begin
2985 match
2986 source#exit (coe self) true m_active m_first m_pan m_qsearch
2987 with
2988 | None -> m_prev_uioh
2989 | Some uioh -> uioh
2992 else (
2993 G.postRedisplay "list view kill qsearch";
2994 source#setqsearch "";
2995 coe {< m_qsearch = "" >}
2998 | 0xff0d -> (* return *)
2999 state.text <- "";
3000 let self = {< m_qsearch = "" >} in
3001 source#setqsearch "";
3002 let opt =
3003 G.postRedisplay "listview enter";
3004 if m_active >= 0 && m_active < source#getitemcount
3005 then (
3006 source#exit (coe self) false m_active m_first m_pan "";
3008 else (
3009 source#exit (coe self) true m_active m_first m_pan "";
3012 begin match opt with
3013 | None -> m_prev_uioh
3014 | Some uioh -> uioh
3017 | 0xff9f | 0xffff -> (* delete *)
3018 coe self
3020 | 0xff52 -> navigate ~-1 (* up *)
3021 | 0xff54 -> navigate 1 (* down *)
3022 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3023 | 0xff56 -> navigate fstate.maxrows (* next *)
3025 | 0xff53 -> (* right *)
3026 state.text <- "";
3027 G.postRedisplay "listview right";
3028 coe {< m_pan = m_pan - 1 >}
3030 | 0xff51 -> (* left *)
3031 state.text <- "";
3032 G.postRedisplay "listview left";
3033 coe {< m_pan = m_pan + 1 >}
3035 | 0xff50 -> (* home *)
3036 let active = find 0 1 in
3037 G.postRedisplay "listview home";
3038 set active 0;
3040 | 0xff57 -> (* end *)
3041 let first = max 0 (itemcount - fstate.maxrows) in
3042 let active = find (itemcount - 1) ~-1 in
3043 G.postRedisplay "listview end";
3044 set active first;
3046 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3047 coe self
3049 | _ ->
3050 dolog "listview unknown key %#x" key; coe self
3052 method key key mask =
3053 match state.mode with
3054 | Textentry te -> textentrykeyboard key mask te; coe self
3055 | _ -> self#key1 key mask
3057 method button button down x y _ =
3058 let opt =
3059 match button with
3060 | 1 when x > conf.winw - conf.scrollbw ->
3061 G.postRedisplay "listview scroll";
3062 if down
3063 then
3064 let _, position, sh = self#scrollph in
3065 if y > truncate position && y < truncate (position +. sh)
3066 then (
3067 state.mstate <- Mscrolly;
3068 Some (coe self)
3070 else
3071 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3072 let first = truncate (s *. float source#getitemcount) in
3073 let first = min source#getitemcount first in
3074 Some (coe {< m_first = first; m_active = first >})
3075 else (
3076 state.mstate <- Mnone;
3077 Some (coe self);
3079 | 1 when not down ->
3080 begin match self#elemunder y with
3081 | Some n ->
3082 G.postRedisplay "listview click";
3083 source#exit
3084 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3085 | _ ->
3086 Some (coe self)
3088 | n when (n == 4 || n == 5) && not down ->
3089 let len = source#getitemcount in
3090 let first =
3091 if n = 5 && m_first + fstate.maxrows >= len
3092 then
3093 m_first
3094 else
3095 let first = m_first + (if n == 4 then -1 else 1) in
3096 bound first 0 (len - 1)
3098 G.postRedisplay "listview wheel";
3099 Some (coe {< m_first = first >})
3100 | _ ->
3101 Some (coe self)
3103 match opt with
3104 | None -> m_prev_uioh
3105 | Some uioh -> uioh
3107 method motion _ y =
3108 match state.mstate with
3109 | Mscrolly ->
3110 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3111 let first = truncate (s *. float source#getitemcount) in
3112 let first = min source#getitemcount first in
3113 G.postRedisplay "listview motion";
3114 coe {< m_first = first; m_active = first >}
3115 | _ -> coe self
3117 method pmotion x y =
3118 if x < conf.winw - conf.scrollbw
3119 then
3120 let n =
3121 match self#elemunder y with
3122 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3123 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3125 let o =
3126 if n != m_active
3127 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3128 else self
3130 coe o
3131 else (
3132 Wsi.setcursor Wsi.CURSOR_INHERIT;
3133 coe self
3136 method infochanged _ = ()
3138 method scrollpw = (0, 0.0, 0.0)
3139 method scrollph =
3140 let nfs = fstate.fontsize + 1 in
3141 let y = m_first * nfs in
3142 let itemcount = source#getitemcount in
3143 let maxi = max 0 (itemcount - fstate.maxrows) in
3144 let maxy = maxi * nfs in
3145 let p, h = scrollph y maxy in
3146 conf.scrollbw, p, h
3148 method modehash = modehash
3149 end;;
3151 class outlinelistview ~source =
3152 object (self)
3153 inherit listview
3154 ~source:(source :> lvsource)
3155 ~trusted:false
3156 ~modehash:(findkeyhash conf "outline")
3157 as super
3159 method key key mask =
3160 let calcfirst first active =
3161 if active > first
3162 then
3163 let rows = active - first in
3164 if rows > fstate.maxrows then active - fstate.maxrows else first
3165 else active
3167 let navigate incr =
3168 let active = m_active + incr in
3169 let active = bound active 0 (source#getitemcount - 1) in
3170 let first = calcfirst m_first active in
3171 G.postRedisplay "outline navigate";
3172 coe {< m_active = active; m_first = first >}
3174 let ctrl = Wsi.withctrl mask in
3175 match key with
3176 | 110 when ctrl -> (* ctrl-n *)
3177 source#narrow m_qsearch;
3178 G.postRedisplay "outline ctrl-n";
3179 coe {< m_first = 0; m_active = 0 >}
3181 | 117 when ctrl -> (* ctrl-u *)
3182 source#denarrow;
3183 G.postRedisplay "outline ctrl-u";
3184 state.text <- "";
3185 coe {< m_first = 0; m_active = 0 >}
3187 | 108 when ctrl -> (* ctrl-l *)
3188 let first = m_active - (fstate.maxrows / 2) in
3189 G.postRedisplay "outline ctrl-l";
3190 coe {< m_first = first >}
3192 | 0xff9f | 0xffff -> (* delete *)
3193 source#remove m_active;
3194 G.postRedisplay "outline delete";
3195 let active = max 0 (m_active-1) in
3196 coe {< m_first = firstof m_first active;
3197 m_active = active >}
3199 | 0xff52 -> navigate ~-1 (* up *)
3200 | 0xff54 -> navigate 1 (* down *)
3201 | 0xff55 -> (* prior *)
3202 navigate ~-(fstate.maxrows)
3203 | 0xff56 -> (* next *)
3204 navigate fstate.maxrows
3206 | 0xff53 -> (* [ctrl-]right *)
3207 let o =
3208 if ctrl
3209 then (
3210 G.postRedisplay "outline ctrl right";
3211 {< m_pan = m_pan + 1 >}
3213 else self#updownlevel 1
3215 coe o
3217 | 0xff51 -> (* [ctrl-]left *)
3218 let o =
3219 if ctrl
3220 then (
3221 G.postRedisplay "outline ctrl left";
3222 {< m_pan = m_pan - 1 >}
3224 else self#updownlevel ~-1
3226 coe o
3228 | 0xff50 -> (* home *)
3229 G.postRedisplay "outline home";
3230 coe {< m_first = 0; m_active = 0 >}
3232 | 0xff57 -> (* end *)
3233 let active = source#getitemcount - 1 in
3234 let first = max 0 (active - fstate.maxrows) in
3235 G.postRedisplay "outline end";
3236 coe {< m_active = active; m_first = first >}
3238 | _ -> super#key key mask
3241 let outlinesource usebookmarks =
3242 let empty = [||] in
3243 (object
3244 inherit lvsourcebase
3245 val mutable m_items = empty
3246 val mutable m_orig_items = empty
3247 val mutable m_prev_items = empty
3248 val mutable m_narrow_pattern = ""
3249 val mutable m_hadremovals = false
3251 method getitemcount =
3252 Array.length m_items + (if m_hadremovals then 1 else 0)
3254 method getitem n =
3255 if n == Array.length m_items && m_hadremovals
3256 then
3257 ("[Confirm removal]", 0)
3258 else
3259 let s, n, _ = m_items.(n) in
3260 (s, n)
3262 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3263 ignore (uioh, first, qsearch);
3264 let confrimremoval = m_hadremovals && active = Array.length m_items in
3265 let items =
3266 if String.length m_narrow_pattern = 0
3267 then m_orig_items
3268 else m_items
3270 if not cancel
3271 then (
3272 if not confrimremoval
3273 then(
3274 let _, _, anchor = m_items.(active) in
3275 gotoanchor anchor;
3276 m_items <- items;
3278 else (
3279 state.bookmarks <- Array.to_list m_items;
3280 m_orig_items <- m_items;
3283 else m_items <- items;
3284 m_pan <- pan;
3285 None
3287 method hasaction _ = true
3289 method greetmsg =
3290 if Array.length m_items != Array.length m_orig_items
3291 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3292 else ""
3294 method narrow pattern =
3295 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3296 match reopt with
3297 | None -> ()
3298 | Some re ->
3299 let rec loop accu n =
3300 if n = -1
3301 then (
3302 m_narrow_pattern <- pattern;
3303 m_items <- Array.of_list accu
3305 else
3306 let (s, _, _) as o = m_items.(n) in
3307 let accu =
3308 if (try ignore (Str.search_forward re s 0); true
3309 with Not_found -> false)
3310 then o :: accu
3311 else accu
3313 loop accu (n-1)
3315 loop [] (Array.length m_items - 1)
3317 method denarrow =
3318 m_orig_items <- (
3319 if usebookmarks
3320 then Array.of_list state.bookmarks
3321 else state.outlines
3323 m_items <- m_orig_items
3325 method remove m =
3326 if usebookmarks
3327 then
3328 if m >= 0 && m < Array.length m_items
3329 then (
3330 m_hadremovals <- true;
3331 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3332 let n = if n >= m then n+1 else n in
3333 m_items.(n)
3337 method reset anchor items =
3338 m_hadremovals <- false;
3339 if m_orig_items == empty || m_prev_items != items
3340 then (
3341 m_orig_items <- items;
3342 if String.length m_narrow_pattern = 0
3343 then m_items <- items;
3345 m_prev_items <- items;
3346 let rely = getanchory anchor in
3347 let active =
3348 let rec loop n best bestd =
3349 if n = Array.length m_items
3350 then best
3351 else
3352 let (_, _, anchor) = m_items.(n) in
3353 let orely = getanchory anchor in
3354 let d = abs (orely - rely) in
3355 if d < bestd
3356 then loop (n+1) n d
3357 else loop (n+1) best bestd
3359 loop 0 ~-1 max_int
3361 m_active <- active;
3362 m_first <- firstof m_first active
3363 end)
3366 let enterselector usebookmarks =
3367 let source = outlinesource usebookmarks in
3368 fun errmsg ->
3369 let outlines =
3370 if usebookmarks
3371 then Array.of_list state.bookmarks
3372 else state.outlines
3374 if Array.length outlines = 0
3375 then (
3376 showtext ' ' errmsg;
3378 else (
3379 state.text <- source#greetmsg;
3380 Wsi.setcursor Wsi.CURSOR_INHERIT;
3381 let anchor = getanchor () in
3382 source#reset anchor outlines;
3383 state.uioh <- coe (new outlinelistview ~source);
3384 G.postRedisplay "enter selector";
3388 let enteroutlinemode =
3389 let f = enterselector false in
3390 fun ()-> f "Document has no outline";
3393 let enterbookmarkmode =
3394 let f = enterselector true in
3395 fun () -> f "Document has no bookmarks (yet)";
3398 let color_of_string s =
3399 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3400 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3404 let color_to_string (r, g, b) =
3405 let r = truncate (r *. 256.0)
3406 and g = truncate (g *. 256.0)
3407 and b = truncate (b *. 256.0) in
3408 Printf.sprintf "%d/%d/%d" r g b
3411 let irect_of_string s =
3412 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3415 let irect_to_string (x0,y0,x1,y1) =
3416 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3419 let makecheckers () =
3420 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3421 following to say:
3422 converted by Issac Trotts. July 25, 2002 *)
3423 let image_height = 64
3424 and image_width = 64 in
3426 let make_image () =
3427 let image =
3428 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3430 for i = 0 to image_width - 1 do
3431 for j = 0 to image_height - 1 do
3432 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3433 (if (i land 8 ) lxor (j land 8) = 0
3434 then [|255;255;255|] else [|200;200;200|])
3435 done
3436 done;
3437 image
3439 let image = make_image () in
3440 let id = GlTex.gen_texture () in
3441 GlTex.bind_texture `texture_2d id;
3442 GlPix.store (`unpack_alignment 1);
3443 GlTex.image2d image;
3444 List.iter (GlTex.parameter ~target:`texture_2d)
3445 [ `wrap_s `repeat;
3446 `wrap_t `repeat;
3447 `mag_filter `nearest;
3448 `min_filter `nearest ];
3452 let setcheckers enabled =
3453 match state.texid with
3454 | None ->
3455 if enabled then state.texid <- Some (makecheckers ())
3457 | Some texid ->
3458 if not enabled
3459 then (
3460 GlTex.delete_texture texid;
3461 state.texid <- None;
3465 let int_of_string_with_suffix s =
3466 let l = String.length s in
3467 let s1, shift =
3468 if l > 1
3469 then
3470 let suffix = Char.lowercase s.[l-1] in
3471 match suffix with
3472 | 'k' -> String.sub s 0 (l-1), 10
3473 | 'm' -> String.sub s 0 (l-1), 20
3474 | 'g' -> String.sub s 0 (l-1), 30
3475 | _ -> s, 0
3476 else s, 0
3478 let n = int_of_string s1 in
3479 let m = n lsl shift in
3480 if m < 0 || m < n
3481 then raise (Failure "value too large")
3482 else m
3485 let string_with_suffix_of_int n =
3486 if n = 0
3487 then "0"
3488 else
3489 let n, s =
3490 if n = 0
3491 then 0, ""
3492 else (
3493 if n land ((1 lsl 20) - 1) = 0
3494 then n lsr 20, "M"
3495 else (
3496 if n land ((1 lsl 10) - 1) = 0
3497 then n lsr 10, "K"
3498 else n, ""
3502 let rec loop s n =
3503 let h = n mod 1000 in
3504 let n = n / 1000 in
3505 if n = 0
3506 then string_of_int h ^ s
3507 else (
3508 let s = Printf.sprintf "_%03d%s" h s in
3509 loop s n
3512 loop "" n ^ s;
3515 let defghyllscroll = (40, 8, 32);;
3516 let ghyllscroll_of_string s =
3517 let (n, a, b) as nab =
3518 if s = "default"
3519 then defghyllscroll
3520 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3522 if n <= a || n <= b || a >= b
3523 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3524 nab;
3527 let ghyllscroll_to_string ((n, a, b) as nab) =
3528 if nab = defghyllscroll
3529 then "default"
3530 else Printf.sprintf "%d,%d,%d" n a b;
3533 let describe_location () =
3534 let f (fn, _) l =
3535 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3537 let fn, ln = List.fold_left f (-1, -1) state.layout in
3538 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3539 let percent =
3540 if maxy <= 0
3541 then 100.
3542 else (100. *. (float state.y /. float maxy))
3544 if fn = ln
3545 then
3546 Printf.sprintf "page %d of %d [%.2f%%]"
3547 (fn+1) state.pagecount percent
3548 else
3549 Printf.sprintf
3550 "pages %d-%d of %d [%.2f%%]"
3551 (fn+1) (ln+1) state.pagecount percent
3554 let enterinfomode =
3555 let btos b = if b then "\xe2\x88\x9a" else "" in
3556 let showextended = ref false in
3557 let leave mode = function
3558 | Confirm -> state.mode <- mode
3559 | Cancel -> state.mode <- mode in
3560 let src =
3561 (object
3562 val mutable m_first_time = true
3563 val mutable m_l = []
3564 val mutable m_a = [||]
3565 val mutable m_prev_uioh = nouioh
3566 val mutable m_prev_mode = View
3568 inherit lvsourcebase
3570 method reset prev_mode prev_uioh =
3571 m_a <- Array.of_list (List.rev m_l);
3572 m_l <- [];
3573 m_prev_mode <- prev_mode;
3574 m_prev_uioh <- prev_uioh;
3575 if m_first_time
3576 then (
3577 let rec loop n =
3578 if n >= Array.length m_a
3579 then ()
3580 else
3581 match m_a.(n) with
3582 | _, _, _, Action _ -> m_active <- n
3583 | _ -> loop (n+1)
3585 loop 0;
3586 m_first_time <- false;
3589 method int name get set =
3590 m_l <-
3591 (name, `int get, 1, Action (
3592 fun u ->
3593 let ondone s =
3594 try set (int_of_string s)
3595 with exn ->
3596 state.text <- Printf.sprintf "bad integer `%s': %s"
3597 s (Printexc.to_string exn)
3599 state.text <- "";
3600 let te = name ^ ": ", "", None, intentry, ondone in
3601 state.mode <- Textentry (te, leave m_prev_mode);
3603 )) :: m_l
3605 method int_with_suffix name get set =
3606 m_l <-
3607 (name, `intws get, 1, Action (
3608 fun u ->
3609 let ondone s =
3610 try set (int_of_string_with_suffix s)
3611 with exn ->
3612 state.text <- Printf.sprintf "bad integer `%s': %s"
3613 s (Printexc.to_string exn)
3615 state.text <- "";
3616 let te =
3617 name ^ ": ", "", None, intentry_with_suffix, ondone
3619 state.mode <- Textentry (te, leave m_prev_mode);
3621 )) :: m_l
3623 method bool ?(offset=1) ?(btos=btos) name get set =
3624 m_l <-
3625 (name, `bool (btos, get), offset, Action (
3626 fun u ->
3627 let v = get () in
3628 set (not v);
3630 )) :: m_l
3632 method color name get set =
3633 m_l <-
3634 (name, `color get, 1, Action (
3635 fun u ->
3636 let invalid = (nan, nan, nan) in
3637 let ondone s =
3638 let c =
3639 try color_of_string s
3640 with exn ->
3641 state.text <- Printf.sprintf "bad color `%s': %s"
3642 s (Printexc.to_string exn);
3643 invalid
3645 if c <> invalid
3646 then set c;
3648 let te = name ^ ": ", "", None, textentry, ondone in
3649 state.text <- color_to_string (get ());
3650 state.mode <- Textentry (te, leave m_prev_mode);
3652 )) :: m_l
3654 method string name get set =
3655 m_l <-
3656 (name, `string get, 1, Action (
3657 fun u ->
3658 let ondone s = set s in
3659 let te = name ^ ": ", "", None, textentry, ondone in
3660 state.mode <- Textentry (te, leave m_prev_mode);
3662 )) :: m_l
3664 method colorspace name get set =
3665 m_l <-
3666 (name, `string get, 1, Action (
3667 fun _ ->
3668 let source =
3669 let vals = [| "rgb"; "bgr"; "gray" |] in
3670 (object
3671 inherit lvsourcebase
3673 initializer
3674 m_active <- int_of_colorspace conf.colorspace;
3675 m_first <- 0;
3677 method getitemcount = Array.length vals
3678 method getitem n = (vals.(n), 0)
3679 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3680 ignore (uioh, first, pan, qsearch);
3681 if not cancel then set active;
3682 None
3683 method hasaction _ = true
3684 end)
3686 state.text <- "";
3687 let modehash = findkeyhash conf "info" in
3688 coe (new listview ~source ~trusted:true ~modehash)
3689 )) :: m_l
3691 method caption s offset =
3692 m_l <- (s, `empty, offset, Noaction) :: m_l
3694 method caption2 s f offset =
3695 m_l <- (s, `string f, offset, Noaction) :: m_l
3697 method getitemcount = Array.length m_a
3699 method getitem n =
3700 let tostr = function
3701 | `int f -> string_of_int (f ())
3702 | `intws f -> string_with_suffix_of_int (f ())
3703 | `string f -> f ()
3704 | `color f -> color_to_string (f ())
3705 | `bool (btos, f) -> btos (f ())
3706 | `empty -> ""
3708 let name, t, offset, _ = m_a.(n) in
3709 ((let s = tostr t in
3710 if String.length s > 0
3711 then Printf.sprintf "%s\t%s" name s
3712 else name),
3713 offset)
3715 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3716 let uiohopt =
3717 if not cancel
3718 then (
3719 m_qsearch <- qsearch;
3720 let uioh =
3721 match m_a.(active) with
3722 | _, _, _, Action f -> f uioh
3723 | _ -> uioh
3725 Some uioh
3727 else None
3729 m_active <- active;
3730 m_first <- first;
3731 m_pan <- pan;
3732 uiohopt
3734 method hasaction n =
3735 match m_a.(n) with
3736 | _, _, _, Action _ -> true
3737 | _ -> false
3738 end)
3740 let rec fillsrc prevmode prevuioh =
3741 let sep () = src#caption "" 0 in
3742 let colorp name get set =
3743 src#string name
3744 (fun () -> color_to_string (get ()))
3745 (fun v ->
3747 let c = color_of_string v in
3748 set c
3749 with exn ->
3750 state.text <- Printf.sprintf "bad color `%s': %s"
3751 v (Printexc.to_string exn);
3754 let oldmode = state.mode in
3755 let birdseye = isbirdseye state.mode in
3757 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3759 src#bool "presentation mode"
3760 (fun () -> conf.presentation)
3761 (fun v ->
3762 conf.presentation <- v;
3763 state.anchor <- getanchor ();
3764 represent ());
3766 src#bool "ignore case in searches"
3767 (fun () -> conf.icase)
3768 (fun v -> conf.icase <- v);
3770 src#bool "preload"
3771 (fun () -> conf.preload)
3772 (fun v -> conf.preload <- v);
3774 src#bool "highlight links"
3775 (fun () -> conf.hlinks)
3776 (fun v -> conf.hlinks <- v);
3778 src#bool "under info"
3779 (fun () -> conf.underinfo)
3780 (fun v -> conf.underinfo <- v);
3782 src#bool "persistent bookmarks"
3783 (fun () -> conf.savebmarks)
3784 (fun v -> conf.savebmarks <- v);
3786 src#bool "proportional display"
3787 (fun () -> conf.proportional)
3788 (fun v -> reqlayout conf.angle v);
3790 src#bool "trim margins"
3791 (fun () -> conf.trimmargins)
3792 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3794 src#bool "persistent location"
3795 (fun () -> conf.jumpback)
3796 (fun v -> conf.jumpback <- v);
3798 sep ();
3799 src#int "inter-page space"
3800 (fun () -> conf.interpagespace)
3801 (fun n ->
3802 conf.interpagespace <- n;
3803 let pageno, py =
3804 match state.layout with
3805 | [] -> 0, 0
3806 | l :: _ ->
3807 l.pageno, l.pagey
3809 state.maxy <- calcheight ();
3810 let y = getpagey pageno in
3811 gotoy (y + py)
3814 src#int "page bias"
3815 (fun () -> conf.pagebias)
3816 (fun v -> conf.pagebias <- v);
3818 src#int "scroll step"
3819 (fun () -> conf.scrollstep)
3820 (fun n -> conf.scrollstep <- n);
3822 src#int "auto scroll step"
3823 (fun () ->
3824 match state.autoscroll with
3825 | Some step -> step
3826 | _ -> conf.autoscrollstep)
3827 (fun n ->
3828 if state.autoscroll <> None
3829 then state.autoscroll <- Some n;
3830 conf.autoscrollstep <- n);
3832 src#int "zoom"
3833 (fun () -> truncate (conf.zoom *. 100.))
3834 (fun v -> setzoom ((float v) /. 100.));
3836 src#int "rotation"
3837 (fun () -> conf.angle)
3838 (fun v -> reqlayout v conf.proportional);
3840 src#int "scroll bar width"
3841 (fun () -> state.scrollw)
3842 (fun v ->
3843 state.scrollw <- v;
3844 conf.scrollbw <- v;
3845 reshape conf.winw conf.winh;
3848 src#int "scroll handle height"
3849 (fun () -> conf.scrollh)
3850 (fun v -> conf.scrollh <- v;);
3852 src#int "thumbnail width"
3853 (fun () -> conf.thumbw)
3854 (fun v ->
3855 conf.thumbw <- min 4096 v;
3856 match oldmode with
3857 | Birdseye beye ->
3858 leavebirdseye beye false;
3859 enterbirdseye ()
3860 | _ -> ()
3863 src#string "columns"
3864 (fun () ->
3865 match conf.columns with
3866 | None -> "1"
3867 | Some (multicol, _) -> columns_to_string multicol)
3868 (fun v ->
3869 let n, a, b = columns_of_string v in
3870 setcolumns n a b);
3872 sep ();
3873 src#caption "Presentation mode" 0;
3874 src#bool "scrollbar visible"
3875 (fun () -> conf.scrollbarinpm)
3876 (fun v ->
3877 if v != conf.scrollbarinpm
3878 then (
3879 conf.scrollbarinpm <- v;
3880 if conf.presentation
3881 then (
3882 state.scrollw <- if v then conf.scrollbw else 0;
3883 reshape conf.winw conf.winh;
3888 sep ();
3889 src#caption "Pixmap cache" 0;
3890 src#int_with_suffix "size (advisory)"
3891 (fun () -> conf.memlimit)
3892 (fun v -> conf.memlimit <- v);
3894 src#caption2 "used"
3895 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3896 (string_with_suffix_of_int state.memused)
3897 (Hashtbl.length state.tilemap)) 1;
3899 sep ();
3900 src#caption "Layout" 0;
3901 src#caption2 "Dimension"
3902 (fun () ->
3903 Printf.sprintf "%dx%d (virtual %dx%d)"
3904 conf.winw conf.winh
3905 state.w state.maxy)
3907 if conf.debug
3908 then
3909 src#caption2 "Position" (fun () ->
3910 Printf.sprintf "%dx%d" state.x state.y
3912 else
3913 src#caption2 "Visible" (fun () -> describe_location ()) 1
3916 sep ();
3917 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3918 "Save these parameters as global defaults at exit"
3919 (fun () -> conf.bedefault)
3920 (fun v -> conf.bedefault <- v)
3923 sep ();
3924 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3925 src#bool ~offset:0 ~btos "Extended parameters"
3926 (fun () -> !showextended)
3927 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3928 if !showextended
3929 then (
3930 src#bool "checkers"
3931 (fun () -> conf.checkers)
3932 (fun v -> conf.checkers <- v; setcheckers v);
3933 src#bool "update cursor"
3934 (fun () -> conf.updatecurs)
3935 (fun v -> conf.updatecurs <- v);
3936 src#bool "verbose"
3937 (fun () -> conf.verbose)
3938 (fun v -> conf.verbose <- v);
3939 src#bool "invert colors"
3940 (fun () -> conf.invert)
3941 (fun v -> conf.invert <- v);
3942 src#bool "max fit"
3943 (fun () -> conf.maxhfit)
3944 (fun v -> conf.maxhfit <- v);
3945 src#bool "redirect stderr"
3946 (fun () -> conf.redirectstderr)
3947 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3948 src#string "uri launcher"
3949 (fun () -> conf.urilauncher)
3950 (fun v -> conf.urilauncher <- v);
3951 src#string "path launcher"
3952 (fun () -> conf.pathlauncher)
3953 (fun v -> conf.pathlauncher <- v);
3954 src#string "tile size"
3955 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3956 (fun v ->
3958 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3959 conf.tileh <- max 64 w;
3960 conf.tilew <- max 64 h;
3961 flushtiles ();
3962 with exn ->
3963 state.text <- Printf.sprintf "bad tile size `%s': %s"
3964 v (Printexc.to_string exn));
3965 src#int "texture count"
3966 (fun () -> conf.texcount)
3967 (fun v ->
3968 if realloctexts v
3969 then conf.texcount <- v
3970 else showtext '!' " Failed to set texture count please retry later"
3972 src#int "slice height"
3973 (fun () -> conf.sliceheight)
3974 (fun v ->
3975 conf.sliceheight <- v;
3976 wcmd "sliceh %d" conf.sliceheight;
3978 src#int "anti-aliasing level"
3979 (fun () -> conf.aalevel)
3980 (fun v ->
3981 conf.aalevel <- bound v 0 8;
3982 state.anchor <- getanchor ();
3983 opendoc state.path state.password;
3985 src#int "ui font size"
3986 (fun () -> fstate.fontsize)
3987 (fun v -> setfontsize (bound v 5 100));
3988 colorp "background color"
3989 (fun () -> conf.bgcolor)
3990 (fun v -> conf.bgcolor <- v);
3991 src#bool "crop hack"
3992 (fun () -> conf.crophack)
3993 (fun v -> conf.crophack <- v);
3994 src#string "trim fuzz"
3995 (fun () -> irect_to_string conf.trimfuzz)
3996 (fun v ->
3998 conf.trimfuzz <- irect_of_string v;
3999 if conf.trimmargins
4000 then settrim true conf.trimfuzz;
4001 with exn ->
4002 state.text <- Printf.sprintf "bad irect `%s': %s"
4003 v (Printexc.to_string exn)
4005 src#string "throttle"
4006 (fun () ->
4007 match conf.maxwait with
4008 | None -> "show place holder if page is not ready"
4009 | Some time ->
4010 if time = infinity
4011 then "wait for page to fully render"
4012 else
4013 "wait " ^ string_of_float time
4014 ^ " seconds before showing placeholder"
4016 (fun v ->
4018 let f = float_of_string v in
4019 if f <= 0.0
4020 then conf.maxwait <- None
4021 else conf.maxwait <- Some f
4022 with exn ->
4023 state.text <- Printf.sprintf "bad time `%s': %s"
4024 v (Printexc.to_string exn)
4026 src#string "ghyll scroll"
4027 (fun () ->
4028 match conf.ghyllscroll with
4029 | None -> ""
4030 | Some nab -> ghyllscroll_to_string nab
4032 (fun v ->
4034 let gs =
4035 if String.length v = 0
4036 then None
4037 else Some (ghyllscroll_of_string v)
4039 conf.ghyllscroll <- gs
4040 with exn ->
4041 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4042 v (Printexc.to_string exn)
4044 src#string "selection command"
4045 (fun () -> conf.selcmd)
4046 (fun v -> conf.selcmd <- v);
4047 src#colorspace "color space"
4048 (fun () -> colorspace_to_string conf.colorspace)
4049 (fun v ->
4050 conf.colorspace <- colorspace_of_int v;
4051 wcmd "cs %d" v;
4052 load state.layout;
4056 sep ();
4057 src#caption "Document" 0;
4058 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4059 src#caption2 "Pages"
4060 (fun () -> string_of_int state.pagecount) 1;
4061 src#caption2 "Dimensions"
4062 (fun () -> string_of_int (List.length state.pdims)) 1;
4063 if conf.trimmargins
4064 then (
4065 sep ();
4066 src#caption "Trimmed margins" 0;
4067 src#caption2 "Dimensions"
4068 (fun () -> string_of_int (List.length state.pdims)) 1;
4071 src#reset prevmode prevuioh;
4073 fun () ->
4074 state.text <- "";
4075 let prevmode = state.mode
4076 and prevuioh = state.uioh in
4077 fillsrc prevmode prevuioh;
4078 let source = (src :> lvsource) in
4079 let modehash = findkeyhash conf "info" in
4080 state.uioh <- coe (object (self)
4081 inherit listview ~source ~trusted:true ~modehash as super
4082 val mutable m_prevmemused = 0
4083 method infochanged = function
4084 | Memused ->
4085 if m_prevmemused != state.memused
4086 then (
4087 m_prevmemused <- state.memused;
4088 G.postRedisplay "memusedchanged";
4090 | Pdim -> G.postRedisplay "pdimchanged"
4091 | Docinfo -> fillsrc prevmode prevuioh
4093 method key key mask =
4094 if not (Wsi.withctrl mask)
4095 then
4096 match key with
4097 | 0xff51 -> coe (self#updownlevel ~-1)
4098 | 0xff53 -> coe (self#updownlevel 1)
4099 | _ -> super#key key mask
4100 else super#key key mask
4101 end);
4102 G.postRedisplay "info";
4105 let enterhelpmode =
4106 let source =
4107 (object
4108 inherit lvsourcebase
4109 method getitemcount = Array.length state.help
4110 method getitem n =
4111 let s, n, _ = state.help.(n) in
4112 (s, n)
4114 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4115 let optuioh =
4116 if not cancel
4117 then (
4118 m_qsearch <- qsearch;
4119 match state.help.(active) with
4120 | _, _, Action f -> Some (f uioh)
4121 | _ -> Some (uioh)
4123 else None
4125 m_active <- active;
4126 m_first <- first;
4127 m_pan <- pan;
4128 optuioh
4130 method hasaction n =
4131 match state.help.(n) with
4132 | _, _, Action _ -> true
4133 | _ -> false
4135 initializer
4136 m_active <- -1
4137 end)
4138 in fun () ->
4139 let modehash = findkeyhash conf "help" in
4140 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4141 G.postRedisplay "help";
4144 let entermsgsmode =
4145 let msgsource =
4146 let re = Str.regexp "[\r\n]" in
4147 (object
4148 inherit lvsourcebase
4149 val mutable m_items = [||]
4151 method getitemcount = 1 + Array.length m_items
4153 method getitem n =
4154 if n = 0
4155 then "[Clear]", 0
4156 else m_items.(n-1), 0
4158 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4159 ignore uioh;
4160 if not cancel
4161 then (
4162 if active = 0
4163 then Buffer.clear state.errmsgs;
4164 m_qsearch <- qsearch;
4166 m_active <- active;
4167 m_first <- first;
4168 m_pan <- pan;
4169 None
4171 method hasaction n =
4172 n = 0
4174 method reset =
4175 state.newerrmsgs <- false;
4176 let l = Str.split re (Buffer.contents state.errmsgs) in
4177 m_items <- Array.of_list l
4179 initializer
4180 m_active <- 0
4181 end)
4182 in fun () ->
4183 state.text <- "";
4184 msgsource#reset;
4185 let source = (msgsource :> lvsource) in
4186 let modehash = findkeyhash conf "listview" in
4187 state.uioh <- coe (object
4188 inherit listview ~source ~trusted:false ~modehash as super
4189 method display =
4190 if state.newerrmsgs
4191 then msgsource#reset;
4192 super#display
4193 end);
4194 G.postRedisplay "msgs";
4197 let quickbookmark ?title () =
4198 match state.layout with
4199 | [] -> ()
4200 | l :: _ ->
4201 let title =
4202 match title with
4203 | None ->
4204 let sec = Unix.gettimeofday () in
4205 let tm = Unix.localtime sec in
4206 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4207 (l.pageno+1)
4208 tm.Unix.tm_mday
4209 tm.Unix.tm_mon
4210 (tm.Unix.tm_year + 1900)
4211 tm.Unix.tm_hour
4212 tm.Unix.tm_min
4213 | Some title -> title
4215 state.bookmarks <-
4216 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4217 :: state.bookmarks
4220 let doreshape w h =
4221 state.fullscreen <- None;
4222 Wsi.reshape w h;
4225 let setautoscrollspeed step goingdown =
4226 let incr = max 1 ((abs step) / 2) in
4227 let incr = if goingdown then incr else -incr in
4228 let astep = step + incr in
4229 state.autoscroll <- Some astep;
4232 let viewkeyboard key mask =
4233 let enttext te =
4234 let mode = state.mode in
4235 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4236 state.text <- "";
4237 enttext ();
4238 G.postRedisplay "view:enttext"
4240 let ctrl = Wsi.withctrl mask in
4241 match key with
4242 | 81 -> (* Q *)
4243 exit 0
4245 | 0xff63 -> (* insert *)
4246 state.mode <- LinkNav None;
4247 gotoy state.y;
4249 | 0xff1b | 113 -> (* escape / q *)
4250 begin match state.mstate with
4251 | Mzoomrect _ ->
4252 state.mstate <- Mnone;
4253 Wsi.setcursor Wsi.CURSOR_INHERIT;
4254 G.postRedisplay "kill zoom rect";
4255 | _ ->
4256 match state.ranchors with
4257 | [] -> raise Wsi.Quit
4258 | (path, password, anchor) :: rest ->
4259 state.ranchors <- rest;
4260 state.anchor <- anchor;
4261 opendoc path password
4262 end;
4264 | 0xff08 -> (* backspace *)
4265 let y = getnav ~-1 in
4266 gotoy_and_clear_text y
4268 | 111 -> (* o *)
4269 enteroutlinemode ()
4271 | 117 -> (* u *)
4272 state.rects <- [];
4273 state.text <- "";
4274 G.postRedisplay "dehighlight";
4276 | 47 | 63 -> (* / ? *)
4277 let ondone isforw s =
4278 cbput state.hists.pat s;
4279 state.searchpattern <- s;
4280 search s isforw
4282 let s = String.create 1 in
4283 s.[0] <- Char.chr key;
4284 enttext (s, "", Some (onhist state.hists.pat),
4285 textentry, ondone (key = 47))
4287 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4288 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4289 setzoom (conf.zoom +. incr)
4291 | 43 | 0xffab -> (* + *)
4292 let ondone s =
4293 let n =
4294 try int_of_string s with exc ->
4295 state.text <- Printf.sprintf "bad integer `%s': %s"
4296 s (Printexc.to_string exc);
4297 max_int
4299 if n != max_int
4300 then (
4301 conf.pagebias <- n;
4302 state.text <- "page bias is now " ^ string_of_int n;
4305 enttext ("page bias: ", "", None, intentry, ondone)
4307 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4308 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4309 setzoom (max 0.01 (conf.zoom -. decr))
4311 | 45 | 0xffad -> (* - *)
4312 let ondone msg = state.text <- msg in
4313 enttext (
4314 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4315 optentry state.mode, ondone
4318 | 48 when ctrl -> (* ctrl-0 *)
4319 setzoom 1.0
4321 | 49 when ctrl -> (* 1 *)
4322 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4323 if zoom < 1.0
4324 then setzoom zoom
4326 | 0xffc6 -> (* f9 *)
4327 togglebirdseye ()
4329 | 57 when ctrl -> (* ctrl-9 *)
4330 togglebirdseye ()
4332 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4333 when not ctrl -> (* 0..9 *)
4334 let ondone s =
4335 let n =
4336 try int_of_string s with exc ->
4337 state.text <- Printf.sprintf "bad integer `%s': %s"
4338 s (Printexc.to_string exc);
4341 if n >= 0
4342 then (
4343 addnav ();
4344 cbput state.hists.pag (string_of_int n);
4345 gotopage1 (n + conf.pagebias - 1) 0;
4348 let pageentry text key =
4349 match Char.unsafe_chr key with
4350 | 'g' -> TEdone text
4351 | _ -> intentry text key
4353 let text = "x" in text.[0] <- Char.chr key;
4354 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4356 | 98 -> (* b *)
4357 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4358 reshape conf.winw conf.winh;
4360 | 108 -> (* l *)
4361 conf.hlinks <- not conf.hlinks;
4362 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4363 G.postRedisplay "toggle highlightlinks";
4365 | 97 -> (* a *)
4366 begin match state.autoscroll with
4367 | Some step ->
4368 conf.autoscrollstep <- step;
4369 state.autoscroll <- None
4370 | None ->
4371 if conf.autoscrollstep = 0
4372 then state.autoscroll <- Some 1
4373 else state.autoscroll <- Some conf.autoscrollstep
4376 | 80 -> (* P *)
4377 conf.presentation <- not conf.presentation;
4378 if conf.presentation
4379 then (
4380 if not conf.scrollbarinpm
4381 then state.scrollw <- 0;
4383 else
4384 state.scrollw <- conf.scrollbw;
4386 showtext ' ' ("presentation mode " ^
4387 if conf.presentation then "on" else "off");
4388 state.anchor <- getanchor ();
4389 represent ()
4391 | 102 -> (* f *)
4392 begin match state.fullscreen with
4393 | None ->
4394 state.fullscreen <- Some (conf.winw, conf.winh);
4395 Wsi.fullscreen ()
4396 | Some (w, h) ->
4397 state.fullscreen <- None;
4398 doreshape w h
4401 | 103 -> (* g *)
4402 gotoy_and_clear_text 0
4404 | 71 -> (* G *)
4405 gotopage1 (state.pagecount - 1) 0
4407 | 112 | 78 -> (* p|N *)
4408 search state.searchpattern false
4410 | 110 | 0xffc0 -> (* n|F3 *)
4411 search state.searchpattern true
4413 | 116 -> (* t *)
4414 begin match state.layout with
4415 | [] -> ()
4416 | l :: _ ->
4417 gotoy_and_clear_text (getpagey l.pageno)
4420 | 32 -> (* ' ' *)
4421 begin match List.rev state.layout with
4422 | [] -> ()
4423 | l :: _ ->
4424 let pageno = min (l.pageno+1) (state.pagecount-1) in
4425 gotoy_and_clear_text (getpagey pageno)
4428 | 0xff9f | 0xffff -> (* delete *)
4429 begin match state.layout with
4430 | [] -> ()
4431 | l :: _ ->
4432 let pageno = max 0 (l.pageno-1) in
4433 gotoy_and_clear_text (getpagey pageno)
4436 | 61 -> (* = *)
4437 showtext ' ' (describe_location ());
4439 | 119 -> (* w *)
4440 begin match state.layout with
4441 | [] -> ()
4442 | l :: _ ->
4443 doreshape (l.pagew + state.scrollw) l.pageh;
4444 G.postRedisplay "w"
4447 | 39 -> (* ' *)
4448 enterbookmarkmode ()
4450 | 104 | 0xffbe -> (* h|F1 *)
4451 enterhelpmode ()
4453 | 105 -> (* i *)
4454 enterinfomode ()
4456 | 101 when conf.redirectstderr -> (* e *)
4457 entermsgsmode ()
4459 | 109 -> (* m *)
4460 let ondone s =
4461 match state.layout with
4462 | l :: _ ->
4463 state.bookmarks <-
4464 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4465 :: state.bookmarks
4466 | _ -> ()
4468 enttext ("bookmark: ", "", None, textentry, ondone)
4470 | 126 -> (* ~ *)
4471 quickbookmark ();
4472 showtext ' ' "Quick bookmark added";
4474 | 122 -> (* z *)
4475 begin match state.layout with
4476 | l :: _ ->
4477 let rect = getpdimrect l.pagedimno in
4478 let w, h =
4479 if conf.crophack
4480 then
4481 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4482 truncate (1.2 *. (rect.(3) -. rect.(0))))
4483 else
4484 (truncate (rect.(1) -. rect.(0)),
4485 truncate (rect.(3) -. rect.(0)))
4487 let w = truncate ((float w)*.conf.zoom)
4488 and h = truncate ((float h)*.conf.zoom) in
4489 if w != 0 && h != 0
4490 then (
4491 state.anchor <- getanchor ();
4492 doreshape (w + state.scrollw) (h + conf.interpagespace)
4494 G.postRedisplay "z";
4496 | [] -> ()
4499 | 50 when ctrl -> (* ctrl-2 *)
4500 let maxw = getmaxw () in
4501 if maxw > 0.0
4502 then setzoom (maxw /. float conf.winw)
4504 | 60 | 62 -> (* < > *)
4505 reqlayout (conf.angle + (if key = 60 then 30 else -30)) conf.proportional
4507 | 91 | 93 -> (* [ ] *)
4508 conf.colorscale <-
4509 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4511 G.postRedisplay "brightness";
4513 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4514 setzoom state.prevzoom
4516 | 107 | 0xff52 -> (* k up *)
4517 begin match state.autoscroll with
4518 | None ->
4519 begin match state.mode with
4520 | Birdseye beye -> upbirdseye 1 beye
4521 | _ ->
4522 if ctrl
4523 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4524 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4526 | Some n ->
4527 setautoscrollspeed n false
4530 | 106 | 0xff54 -> (* j down *)
4531 begin match state.autoscroll with
4532 | None ->
4533 begin match state.mode with
4534 | Birdseye beye -> downbirdseye 1 beye
4535 | _ ->
4536 if ctrl
4537 then gotoy_and_clear_text (clamp (conf.winh/2))
4538 else gotoy_and_clear_text (clamp conf.scrollstep)
4540 | Some n ->
4541 setautoscrollspeed n true
4544 | 0xff51 | 0xff53 when Wsi.withnone mask -> (* left / right *)
4545 if conf.zoom > 1.0
4546 then
4547 let dx =
4548 if ctrl
4549 then conf.winw / 2
4550 else 10
4552 let dx = if key = 0xff51 then dx else -dx in
4553 state.x <- state.x + dx;
4554 gotoy_and_clear_text state.y
4555 else (
4556 state.text <- "";
4557 G.postRedisplay "lef/right"
4560 | 0xff55 -> (* prior *)
4561 let y =
4562 if ctrl
4563 then
4564 match state.layout with
4565 | [] -> state.y
4566 | l :: _ -> state.y - l.pagey
4567 else
4568 clamp (-conf.winh)
4570 gotoghyll y
4572 | 0xff56 -> (* next *)
4573 let y =
4574 if ctrl
4575 then
4576 match List.rev state.layout with
4577 | [] -> state.y
4578 | l :: _ -> getpagey l.pageno
4579 else
4580 clamp conf.winh
4582 gotoghyll y
4584 | 0xff50 -> gotoghyll 0
4585 | 0xff57 -> gotoghyll (clamp state.maxy)
4586 | 0xff53 when Wsi.withalt mask ->
4587 gotoghyll (getnav ~-1)
4588 | 0xff51 when Wsi.withalt mask ->
4589 gotoghyll (getnav 1)
4591 | 114 -> (* r *)
4592 state.anchor <- getanchor ();
4593 opendoc state.path state.password
4595 | 76 -> (* L *)
4596 launchpath ()
4598 | 118 when conf.debug -> (* v *)
4599 state.rects <- [];
4600 List.iter (fun l ->
4601 match getopaque l.pageno with
4602 | None -> ()
4603 | Some opaque ->
4604 let x0, y0, x1, y1 = pagebbox opaque in
4605 let a,b = float x0, float y0 in
4606 let c,d = float x1, float y0 in
4607 let e,f = float x1, float y1 in
4608 let h,j = float x0, float y1 in
4609 let rect = (a,b,c,d,e,f,h,j) in
4610 debugrect rect;
4611 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4612 ) state.layout;
4613 G.postRedisplay "v";
4615 | _ ->
4616 vlog "huh? %s" (Wsi.keyname key)
4619 let gotounder = function
4620 | Ulinkgoto (pageno, top) ->
4621 if pageno >= 0
4622 then (
4623 addnav ();
4624 gotopage1 pageno top;
4627 | Ulinkuri s ->
4628 gotouri s
4630 | Uremote (filename, pageno) ->
4631 let path =
4632 if Sys.file_exists filename
4633 then filename
4634 else
4635 let dir = Filename.dirname state.path in
4636 let path = Filename.concat dir filename in
4637 if Sys.file_exists path
4638 then path
4639 else ""
4641 if String.length path > 0
4642 then (
4643 let anchor = getanchor () in
4644 let ranchor = state.path, state.password, anchor in
4645 state.anchor <- (pageno, 0.0);
4646 state.ranchors <- ranchor :: state.ranchors;
4647 opendoc path "";
4649 else showtext '!' ("Could not find " ^ filename)
4651 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4654 let linknavkeyboard key mask linknav =
4655 if key = 0xff63
4656 then (
4657 state.mode <- View;
4658 G.postRedisplay "leave linknav"
4660 else
4661 begin match if state.currently = Idle then linknav else None with
4662 | None -> viewkeyboard key mask
4663 | Some ((l, opaque, n), _) ->
4664 if key = 0xff0d
4665 then
4666 let under = getlink opaque n in
4667 gotounder under;
4668 state.mode <- View;
4669 else
4670 let opt, dir =
4671 match key with
4672 | 0xff50 -> (* home *)
4673 Some (findlink opaque LDfirst), 1
4675 | 0xff57 -> (* end *)
4676 Some (findlink opaque LDlast), -1
4678 | 0xff51 | 0xff53 -> (* left right *)
4679 let ld, dir =
4680 if key = 0xff51
4681 then LDleft n, -1
4682 else LDright n, 1
4684 Some (findlink opaque ld), dir
4686 | 0xff52 | 0xff54 -> (* up down *)
4687 let ld, dir =
4688 if key = 0xff52
4689 then LDup n, -1
4690 else LDdown n, 1
4692 Some (findlink opaque ld), dir
4694 | _ -> None, 0
4696 begin match opt with
4697 | Some Lnone ->
4698 begin match findpwl l.pageno dir with
4699 | Pwlnotfound -> ()
4700 | Pwl pageno ->
4701 state.mode <- LinkNav None;
4702 let y, h = getpageyh pageno in
4703 let y =
4704 if dir < 0
4705 then y + h - conf.winh
4706 else y
4708 gotoy y;
4709 end;
4711 | Some (Lfound (m, x0, y0, x1, y1)) ->
4712 if y0 < l.pagey || l.pagedispy + (y0 - l.pagey) > conf.winh
4713 then (
4714 state.mode <- LinkNav None;
4715 gotoy (state.y + (y0 - l.pagey))
4717 else (
4718 if m = n
4719 then (
4720 match findpwl l.pageno dir with
4721 | Pwlnotfound -> ()
4722 | Pwl pageno ->
4723 state.mode <- LinkNav None;
4724 let y, h = getpageyh pageno in
4725 let y =
4726 if dir < 0
4727 then y + h - conf.winh
4728 else y
4730 gotoy y;
4732 else
4733 let r = x0, y0, x1, y1 in
4734 state.mode <- LinkNav (Some ((l, opaque, m), r));
4735 G.postRedisplay "linknav up"
4737 | None ->
4738 viewkeyboard key mask
4739 end;
4740 end;
4743 let keyboard key mask =
4744 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4745 then wcmd "interrupt"
4746 else state.uioh <- state.uioh#key key mask
4749 let birdseyekeyboard key mask
4750 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4751 let incr =
4752 match conf.columns with
4753 | None -> 1
4754 | Some ((c, _, _), _) -> c
4756 match key with
4757 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4758 let y, h = getpageyh pageno in
4759 let top = (conf.winh - h) / 2 in
4760 gotoy (max 0 (y - top))
4761 | 0xff0d -> leavebirdseye beye false
4762 | 0xff1b -> leavebirdseye beye true (* escape *)
4763 | 0xff52 -> upbirdseye incr beye (* prior *)
4764 | 0xff54 -> downbirdseye incr beye (* next *)
4765 | 0xff51 -> upbirdseye 1 beye (* up *)
4766 | 0xff53 -> downbirdseye 1 beye (* down *)
4768 | 0xff55 ->
4769 begin match state.layout with
4770 | l :: _ ->
4771 if l.pagey != 0
4772 then (
4773 state.mode <- Birdseye (
4774 oconf, leftx, l.pageno, hooverpageno, anchor
4776 gotopage1 l.pageno 0;
4778 else (
4779 let layout = layout (state.y-conf.winh) conf.winh in
4780 match layout with
4781 | [] -> gotoy (clamp (-conf.winh))
4782 | l :: _ ->
4783 state.mode <- Birdseye (
4784 oconf, leftx, l.pageno, hooverpageno, anchor
4786 gotopage1 l.pageno 0
4789 | [] -> gotoy (clamp (-conf.winh))
4790 end;
4792 | 0xff56 ->
4793 begin match List.rev state.layout with
4794 | l :: _ ->
4795 let layout = layout (state.y + conf.winh) conf.winh in
4796 begin match layout with
4797 | [] ->
4798 let incr = l.pageh - l.pagevh in
4799 if incr = 0
4800 then (
4801 state.mode <-
4802 Birdseye (
4803 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4805 G.postRedisplay "birdseye pagedown";
4807 else gotoy (clamp (incr + conf.interpagespace*2));
4809 | l :: _ ->
4810 state.mode <-
4811 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4812 gotopage1 l.pageno 0;
4815 | [] -> gotoy (clamp conf.winh)
4816 end;
4818 | 0xff50 ->
4819 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4820 gotopage1 0 0
4822 | 0xff57 ->
4823 let pageno = state.pagecount - 1 in
4824 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4825 if not (pagevisible state.layout pageno)
4826 then
4827 let h =
4828 match List.rev state.pdims with
4829 | [] -> conf.winh
4830 | (_, _, h, _) :: _ -> h
4832 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4833 else G.postRedisplay "birdseye end";
4834 | _ -> viewkeyboard key mask
4837 let drawpage l =
4838 let color =
4839 match state.mode with
4840 | Textentry _ -> scalecolor 0.4
4841 | LinkNav _
4842 | View -> scalecolor 1.0
4843 | Birdseye (_, _, pageno, hooverpageno, _) ->
4844 if l.pageno = hooverpageno
4845 then scalecolor 0.9
4846 else (
4847 if l.pageno = pageno
4848 then scalecolor 1.0
4849 else scalecolor 0.8
4852 drawtiles l color;
4853 begin match getopaque l.pageno with
4854 | Some opaque ->
4855 if tileready l l.pagex l.pagey
4856 then
4857 let x = l.pagedispx - l.pagex
4858 and y = l.pagedispy - l.pagey in
4859 postprocess opaque conf.hlinks x y;
4861 | _ -> ()
4862 end;
4865 let scrollindicator () =
4866 let sbw, ph, sh = state.uioh#scrollph in
4867 let sbh, pw, sw = state.uioh#scrollpw in
4869 GlDraw.color (0.64, 0.64, 0.64);
4870 GlDraw.rect
4871 (float (conf.winw - sbw), 0.)
4872 (float conf.winw, float conf.winh)
4874 GlDraw.rect
4875 (0., float (conf.winh - sbh))
4876 (float (conf.winw - state.scrollw - 1), float conf.winh)
4878 GlDraw.color (0.0, 0.0, 0.0);
4880 GlDraw.rect
4881 (float (conf.winw - sbw), ph)
4882 (float conf.winw, ph +. sh)
4884 GlDraw.rect
4885 (pw, float (conf.winh - sbh))
4886 (pw +. sw, float conf.winh)
4890 let showsel () =
4891 match state.mstate with
4892 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4895 | Msel ((x0, y0), (x1, y1)) ->
4896 let rec loop = function
4897 | l :: ls ->
4898 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4899 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4900 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4901 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4902 then
4903 match getopaque l.pageno with
4904 | Some opaque ->
4905 let dx, dy = pagetranslatepoint l 0 0 in
4906 let x0 = x0 + dx
4907 and y0 = y0 + dy
4908 and x1 = x1 + dx
4909 and y1 = y1 + dy in
4910 GlMat.mode `modelview;
4911 GlMat.push ();
4912 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4913 seltext opaque (x0, y0, x1, y1);
4914 GlMat.pop ();
4915 | _ -> ()
4916 else loop ls
4917 | [] -> ()
4919 loop state.layout
4922 let showrects rects =
4923 Gl.enable `blend;
4924 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4925 GlDraw.polygon_mode `both `fill;
4926 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4927 List.iter
4928 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4929 List.iter (fun l ->
4930 if l.pageno = pageno
4931 then (
4932 let dx = float (l.pagedispx - l.pagex) in
4933 let dy = float (l.pagedispy - l.pagey) in
4934 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4935 GlDraw.begins `quads;
4937 GlDraw.vertex2 (x0+.dx, y0+.dy);
4938 GlDraw.vertex2 (x1+.dx, y1+.dy);
4939 GlDraw.vertex2 (x2+.dx, y2+.dy);
4940 GlDraw.vertex2 (x3+.dx, y3+.dy);
4942 GlDraw.ends ();
4944 ) state.layout
4945 ) rects
4947 Gl.disable `blend;
4950 let display () =
4951 GlClear.color (scalecolor2 conf.bgcolor);
4952 GlClear.clear [`color];
4953 List.iter drawpage state.layout;
4954 let rects =
4955 match state.mode with
4956 | LinkNav (Some ((page, _, _), (x0, y0, x1, y1))) ->
4957 (page.pageno, 5, (
4958 float x0, float y0,
4959 float x1, float y0,
4960 float x1, float y1,
4961 float x0, float y1)
4962 ) :: state.rects
4963 | _ -> state.rects
4965 showrects rects;
4966 showsel ();
4967 state.uioh#display;
4968 scrollindicator ();
4969 begin match state.mstate with
4970 | Mzoomrect ((x0, y0), (x1, y1)) ->
4971 Gl.enable `blend;
4972 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4973 GlDraw.polygon_mode `both `fill;
4974 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4975 GlDraw.rect (float x0, float y0)
4976 (float x1, float y1);
4977 Gl.disable `blend;
4978 | _ -> ()
4979 end;
4980 enttext ();
4981 if conf.updatecurs
4982 then (
4983 let mx, my = state.mpos in
4984 updateunder mx my;
4986 Wsi.swapb ();
4989 let zoomrect x y x1 y1 =
4990 let x0 = min x x1
4991 and x1 = max x x1
4992 and y0 = min y y1 in
4993 gotoy (state.y + y0);
4994 state.anchor <- getanchor ();
4995 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4996 let margin =
4997 if state.w < conf.winw - state.scrollw
4998 then (conf.winw - state.scrollw - state.w) / 2
4999 else 0
5001 state.x <- (state.x + margin) - x0;
5002 setzoom zoom;
5003 Wsi.setcursor Wsi.CURSOR_INHERIT;
5004 state.mstate <- Mnone;
5007 let scrollx x =
5008 let winw = conf.winw - state.scrollw - 1 in
5009 let s = float x /. float winw in
5010 let destx = truncate (float (state.w + winw) *. s) in
5011 state.x <- winw - destx;
5012 gotoy_and_clear_text state.y;
5013 state.mstate <- Mscrollx;
5016 let scrolly y =
5017 let s = float y /. float conf.winh in
5018 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5019 gotoy_and_clear_text desty;
5020 state.mstate <- Mscrolly;
5023 let viewmouse button down x y mask =
5024 match button with
5025 | n when (n == 4 || n == 5) && not down ->
5026 if Wsi.withctrl mask
5027 then (
5028 match state.mstate with
5029 | Mzoom (oldn, i) ->
5030 if oldn = n
5031 then (
5032 if i = 2
5033 then
5034 let incr =
5035 match n with
5036 | 5 ->
5037 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5038 | _ ->
5039 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5041 let zoom = conf.zoom -. incr in
5042 setzoom zoom;
5043 state.mstate <- Mzoom (n, 0);
5044 else
5045 state.mstate <- Mzoom (n, i+1);
5047 else state.mstate <- Mzoom (n, 0)
5049 | _ -> state.mstate <- Mzoom (n, 0)
5051 else (
5052 match state.autoscroll with
5053 | Some step -> setautoscrollspeed step (n=4)
5054 | None ->
5055 let incr =
5056 if n = 4
5057 then -conf.scrollstep
5058 else conf.scrollstep
5060 let incr = incr * 2 in
5061 let y = clamp incr in
5062 gotoy_and_clear_text y
5065 | 1 when Wsi.withctrl mask ->
5066 if down
5067 then (
5068 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5069 state.mstate <- Mpan (x, y)
5071 else
5072 state.mstate <- Mnone
5074 | 3 ->
5075 if down
5076 then (
5077 Wsi.setcursor Wsi.CURSOR_CYCLE;
5078 let p = (x, y) in
5079 state.mstate <- Mzoomrect (p, p)
5081 else (
5082 match state.mstate with
5083 | Mzoomrect ((x0, y0), _) ->
5084 if abs (x-x0) > 10 && abs (y - y0) > 10
5085 then zoomrect x0 y0 x y
5086 else (
5087 state.mstate <- Mnone;
5088 Wsi.setcursor Wsi.CURSOR_INHERIT;
5089 G.postRedisplay "kill accidental zoom rect";
5091 | _ ->
5092 Wsi.setcursor Wsi.CURSOR_INHERIT;
5093 state.mstate <- Mnone
5096 | 1 when x > conf.winw - state.scrollw ->
5097 if down
5098 then
5099 let _, position, sh = state.uioh#scrollph in
5100 if y > truncate position && y < truncate (position +. sh)
5101 then state.mstate <- Mscrolly
5102 else scrolly y
5103 else
5104 state.mstate <- Mnone
5106 | 1 when y > conf.winh - state.hscrollh ->
5107 if down
5108 then
5109 let _, position, sw = state.uioh#scrollpw in
5110 if x > truncate position && x < truncate (position +. sw)
5111 then state.mstate <- Mscrollx
5112 else scrollx x
5113 else
5114 state.mstate <- Mnone
5116 | 1 ->
5117 let dest = if down then getunder x y else Unone in
5118 begin match dest with
5119 | Ulinkgoto (pageno, top) ->
5120 if pageno >= 0
5121 then (
5122 addnav ();
5123 gotopage1 pageno top;
5126 | Ulinkuri s ->
5127 gotouri s
5129 | Uremote (filename, pageno) ->
5130 let path =
5131 if Sys.file_exists filename
5132 then filename
5133 else
5134 let dir = Filename.dirname state.path in
5135 let path = Filename.concat dir filename in
5136 if Sys.file_exists path
5137 then path
5138 else ""
5140 if String.length path > 0
5141 then (
5142 let anchor = getanchor () in
5143 let ranchor = state.path, state.password, anchor in
5144 state.anchor <- (pageno, 0.0);
5145 state.ranchors <- ranchor :: state.ranchors;
5146 opendoc path "";
5148 else showtext '!' ("Could not find " ^ filename)
5150 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
5152 | Unone when down ->
5153 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5154 state.mstate <- Mpan (x, y);
5156 | Unone | Utext _ ->
5157 if down
5158 then (
5159 if conf.angle mod 360 = 0
5160 then (
5161 state.mstate <- Msel ((x, y), (x, y));
5162 G.postRedisplay "mouse select";
5165 else (
5166 match state.mstate with
5167 | Mnone -> ()
5169 | Mzoom _ | Mscrollx | Mscrolly ->
5170 state.mstate <- Mnone
5172 | Mzoomrect ((x0, y0), _) ->
5173 zoomrect x0 y0 x y
5175 | Mpan _ ->
5176 Wsi.setcursor Wsi.CURSOR_INHERIT;
5177 state.mstate <- Mnone
5179 | Msel ((_, y0), (_, y1)) ->
5180 let rec loop = function
5181 | [] -> ()
5182 | l :: rest ->
5183 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5184 || ((y1 >= l.pagedispy
5185 && y1 <= (l.pagedispy + l.pagevh)))
5186 then
5187 match getopaque l.pageno with
5188 | Some opaque ->
5189 copysel conf.selcmd opaque;
5190 G.postRedisplay "copysel"
5191 | _ -> ()
5192 else loop rest
5194 loop state.layout;
5195 Wsi.setcursor Wsi.CURSOR_INHERIT;
5196 state.mstate <- Mnone;
5200 | _ -> ()
5203 let birdseyemouse button down x y mask
5204 (conf, leftx, _, hooverpageno, anchor) =
5205 match button with
5206 | 1 when down ->
5207 let rec loop = function
5208 | [] -> ()
5209 | l :: rest ->
5210 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5211 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5212 then (
5213 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5215 else loop rest
5217 loop state.layout
5218 | 3 -> ()
5219 | _ -> viewmouse button down x y mask
5222 let mouse button down x y mask =
5223 state.uioh <- state.uioh#button button down x y mask;
5226 let motion ~x ~y =
5227 state.uioh <- state.uioh#motion x y
5230 let pmotion ~x ~y =
5231 state.uioh <- state.uioh#pmotion x y;
5234 let uioh = object
5235 method display = ()
5237 method key key mask =
5238 begin match state.mode with
5239 | Textentry textentry -> textentrykeyboard key mask textentry
5240 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5241 | View -> viewkeyboard key mask
5242 | LinkNav linknav -> linknavkeyboard key mask linknav
5243 end;
5244 state.uioh
5246 method button button bstate x y mask =
5247 begin match state.mode with
5248 | LinkNav _
5249 | View -> viewmouse button bstate x y mask
5250 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5251 | Textentry _ -> ()
5252 end;
5253 state.uioh
5255 method motion x y =
5256 begin match state.mode with
5257 | Textentry _ -> ()
5258 | View | Birdseye _ | LinkNav _ ->
5259 match state.mstate with
5260 | Mzoom _ | Mnone -> ()
5262 | Mpan (x0, y0) ->
5263 let dx = x - x0
5264 and dy = y0 - y in
5265 state.mstate <- Mpan (x, y);
5266 if conf.zoom > 1.0 then state.x <- state.x + dx;
5267 let y = clamp dy in
5268 gotoy_and_clear_text y
5270 | Msel (a, _) ->
5271 state.mstate <- Msel (a, (x, y));
5272 G.postRedisplay "motion select";
5274 | Mscrolly ->
5275 let y = min conf.winh (max 0 y) in
5276 scrolly y
5278 | Mscrollx ->
5279 let x = min conf.winw (max 0 x) in
5280 scrollx x
5282 | Mzoomrect (p0, _) ->
5283 state.mstate <- Mzoomrect (p0, (x, y));
5284 G.postRedisplay "motion zoomrect";
5285 end;
5286 state.uioh
5288 method pmotion x y =
5289 begin match state.mode with
5290 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5291 let rec loop = function
5292 | [] ->
5293 if hooverpageno != -1
5294 then (
5295 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5296 G.postRedisplay "pmotion birdseye no hoover";
5298 | l :: rest ->
5299 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5300 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5301 then (
5302 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5303 G.postRedisplay "pmotion birdseye hoover";
5305 else loop rest
5307 loop state.layout
5309 | Textentry _ -> ()
5311 | LinkNav _
5312 | View ->
5313 match state.mstate with
5314 | Mnone -> updateunder x y
5315 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5317 end;
5318 state.uioh
5320 method infochanged _ = ()
5322 method scrollph =
5323 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5324 let p, h = scrollph state.y maxy in
5325 state.scrollw, p, h
5327 method scrollpw =
5328 let winw = conf.winw - state.scrollw - 1 in
5329 let fwinw = float winw in
5330 let sw =
5331 let sw = fwinw /. float state.w in
5332 let sw = fwinw *. sw in
5333 max sw (float conf.scrollh)
5335 let position, sw =
5336 let f = state.w+winw in
5337 let r = float (winw-state.x) /. float f in
5338 let p = fwinw *. r in
5339 p-.sw/.2., sw
5341 let sw =
5342 if position +. sw > fwinw
5343 then fwinw -. position
5344 else sw
5346 state.hscrollh, position, sw
5348 method modehash =
5349 let modename =
5350 match state.mode with
5351 | LinkNav _ -> "links"
5352 | Textentry _ -> "textentry"
5353 | Birdseye _ -> "birdseye"
5354 | View -> "global"
5356 findkeyhash conf modename
5357 end;;
5359 module Config =
5360 struct
5361 open Parser
5363 let fontpath = ref "";;
5365 module KeyMap =
5366 Map.Make (struct type t = (int * int) let compare = compare end);;
5368 let unent s =
5369 let l = String.length s in
5370 let b = Buffer.create l in
5371 unent b s 0 l;
5372 Buffer.contents b;
5375 let home =
5376 try Sys.getenv "HOME"
5377 with exn ->
5378 prerr_endline
5379 ("Can not determine home directory location: " ^
5380 Printexc.to_string exn);
5384 let modifier_of_string = function
5385 | "alt" -> Wsi.altmask
5386 | "shift" -> Wsi.shiftmask
5387 | "ctrl" | "control" -> Wsi.ctrlmask
5388 | "meta" -> Wsi.metamask
5389 | _ -> 0
5392 let key_of_string =
5393 let r = Str.regexp "-" in
5394 fun s ->
5395 let elems = Str.full_split r s in
5396 let f n k m =
5397 let g s =
5398 let m1 = modifier_of_string s in
5399 if m1 = 0
5400 then (Wsi.namekey s, m)
5401 else (k, m lor m1)
5402 in function
5403 | Str.Delim s when n land 1 = 0 -> g s
5404 | Str.Text s -> g s
5405 | Str.Delim _ -> (k, m)
5407 let rec loop n k m = function
5408 | [] -> (k, m)
5409 | x :: xs ->
5410 let k, m = f n k m x in
5411 loop (n+1) k m xs
5413 loop 0 0 0 elems
5416 let keys_of_string =
5417 let r = Str.regexp "[ \t]" in
5418 fun s ->
5419 let elems = Str.split r s in
5420 List.map key_of_string elems
5423 let copykeyhashes c =
5424 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5427 let config_of c attrs =
5428 let apply c k v =
5430 match k with
5431 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5432 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5433 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5434 | "preload" -> { c with preload = bool_of_string v }
5435 | "page-bias" -> { c with pagebias = int_of_string v }
5436 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5437 | "auto-scroll-step" ->
5438 { c with autoscrollstep = max 0 (int_of_string v) }
5439 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5440 | "crop-hack" -> { c with crophack = bool_of_string v }
5441 | "throttle" ->
5442 let mw =
5443 match String.lowercase v with
5444 | "true" -> Some infinity
5445 | "false" -> None
5446 | f -> Some (float_of_string f)
5448 { c with maxwait = mw}
5449 | "highlight-links" -> { c with hlinks = bool_of_string v }
5450 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5451 | "vertical-margin" ->
5452 { c with interpagespace = max 0 (int_of_string v) }
5453 | "zoom" ->
5454 let zoom = float_of_string v /. 100. in
5455 let zoom = max zoom 0.0 in
5456 { c with zoom = zoom }
5457 | "presentation" -> { c with presentation = bool_of_string v }
5458 | "rotation-angle" -> { c with angle = int_of_string v }
5459 | "width" -> { c with winw = max 20 (int_of_string v) }
5460 | "height" -> { c with winh = max 20 (int_of_string v) }
5461 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5462 | "proportional-display" -> { c with proportional = bool_of_string v }
5463 | "pixmap-cache-size" ->
5464 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5465 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5466 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5467 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5468 | "persistent-location" -> { c with jumpback = bool_of_string v }
5469 | "background-color" -> { c with bgcolor = color_of_string v }
5470 | "scrollbar-in-presentation" ->
5471 { c with scrollbarinpm = bool_of_string v }
5472 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5473 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5474 | "mupdf-store-size" ->
5475 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5476 | "checkers" -> { c with checkers = bool_of_string v }
5477 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5478 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5479 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5480 | "uri-launcher" -> { c with urilauncher = unent v }
5481 | "path-launcher" -> { c with pathlauncher = unent v }
5482 | "color-space" -> { c with colorspace = colorspace_of_string v }
5483 | "invert-colors" -> { c with invert = bool_of_string v }
5484 | "brightness" -> { c with colorscale = float_of_string v }
5485 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5486 | "ghyllscroll" ->
5487 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5488 | "columns" ->
5489 let nab = columns_of_string v in
5490 { c with columns = Some (nab, [||]) }
5491 | "birds-eye-columns" ->
5492 { c with beyecolumns = Some (max (int_of_string v) 2) }
5493 | "selection-command" -> { c with selcmd = unent v }
5494 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5495 | _ -> c
5496 with exn ->
5497 prerr_endline ("Error processing attribute (`" ^
5498 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5501 let rec fold c = function
5502 | [] -> c
5503 | (k, v) :: rest ->
5504 let c = apply c k v in
5505 fold c rest
5507 fold { c with keyhashes = copykeyhashes c } attrs;
5510 let fromstring f pos n v d =
5511 try f v
5512 with exn ->
5513 dolog "Error processing attribute (%S=%S) at %d\n%s"
5514 n v pos (Printexc.to_string exn)
5519 let bookmark_of attrs =
5520 let rec fold title page rely = function
5521 | ("title", v) :: rest -> fold v page rely rest
5522 | ("page", v) :: rest -> fold title v rely rest
5523 | ("rely", v) :: rest -> fold title page v rest
5524 | _ :: rest -> fold title page rely rest
5525 | [] -> title, page, rely
5527 fold "invalid" "0" "0" attrs
5530 let doc_of attrs =
5531 let rec fold path page rely pan = function
5532 | ("path", v) :: rest -> fold v page rely pan rest
5533 | ("page", v) :: rest -> fold path v rely pan rest
5534 | ("rely", v) :: rest -> fold path page v pan rest
5535 | ("pan", v) :: rest -> fold path page rely v rest
5536 | _ :: rest -> fold path page rely pan rest
5537 | [] -> path, page, rely, pan
5539 fold "" "0" "0" "0" attrs
5542 let map_of attrs =
5543 let rec fold rs ls = function
5544 | ("out", v) :: rest -> fold v ls rest
5545 | ("in", v) :: rest -> fold rs v rest
5546 | _ :: rest -> fold ls rs rest
5547 | [] -> ls, rs
5549 fold "" "" attrs
5552 let setconf dst src =
5553 dst.scrollbw <- src.scrollbw;
5554 dst.scrollh <- src.scrollh;
5555 dst.icase <- src.icase;
5556 dst.preload <- src.preload;
5557 dst.pagebias <- src.pagebias;
5558 dst.verbose <- src.verbose;
5559 dst.scrollstep <- src.scrollstep;
5560 dst.maxhfit <- src.maxhfit;
5561 dst.crophack <- src.crophack;
5562 dst.autoscrollstep <- src.autoscrollstep;
5563 dst.maxwait <- src.maxwait;
5564 dst.hlinks <- src.hlinks;
5565 dst.underinfo <- src.underinfo;
5566 dst.interpagespace <- src.interpagespace;
5567 dst.zoom <- src.zoom;
5568 dst.presentation <- src.presentation;
5569 dst.angle <- src.angle;
5570 dst.winw <- src.winw;
5571 dst.winh <- src.winh;
5572 dst.savebmarks <- src.savebmarks;
5573 dst.memlimit <- src.memlimit;
5574 dst.proportional <- src.proportional;
5575 dst.texcount <- src.texcount;
5576 dst.sliceheight <- src.sliceheight;
5577 dst.thumbw <- src.thumbw;
5578 dst.jumpback <- src.jumpback;
5579 dst.bgcolor <- src.bgcolor;
5580 dst.scrollbarinpm <- src.scrollbarinpm;
5581 dst.tilew <- src.tilew;
5582 dst.tileh <- src.tileh;
5583 dst.mustoresize <- src.mustoresize;
5584 dst.checkers <- src.checkers;
5585 dst.aalevel <- src.aalevel;
5586 dst.trimmargins <- src.trimmargins;
5587 dst.trimfuzz <- src.trimfuzz;
5588 dst.urilauncher <- src.urilauncher;
5589 dst.colorspace <- src.colorspace;
5590 dst.invert <- src.invert;
5591 dst.colorscale <- src.colorscale;
5592 dst.redirectstderr <- src.redirectstderr;
5593 dst.ghyllscroll <- src.ghyllscroll;
5594 dst.columns <- src.columns;
5595 dst.beyecolumns <- src.beyecolumns;
5596 dst.selcmd <- src.selcmd;
5597 dst.updatecurs <- src.updatecurs;
5598 dst.pathlauncher <- src.pathlauncher;
5599 dst.keyhashes <- copykeyhashes src;
5602 let get s =
5603 let h = Hashtbl.create 10 in
5604 let dc = { defconf with angle = defconf.angle } in
5605 let rec toplevel v t spos _ =
5606 match t with
5607 | Vdata | Vcdata | Vend -> v
5608 | Vopen ("llppconfig", _, closed) ->
5609 if closed
5610 then v
5611 else { v with f = llppconfig }
5612 | Vopen _ ->
5613 error "unexpected subelement at top level" s spos
5614 | Vclose _ -> error "unexpected close at top level" s spos
5616 and llppconfig v t spos _ =
5617 match t with
5618 | Vdata | Vcdata -> v
5619 | Vend -> error "unexpected end of input in llppconfig" s spos
5620 | Vopen ("defaults", attrs, closed) ->
5621 let c = config_of dc attrs in
5622 setconf dc c;
5623 if closed
5624 then v
5625 else { v with f = defaults }
5627 | Vopen ("ui-font", attrs, closed) ->
5628 let rec getsize size = function
5629 | [] -> size
5630 | ("size", v) :: rest ->
5631 let size =
5632 fromstring int_of_string spos "size" v fstate.fontsize in
5633 getsize size rest
5634 | l -> getsize size l
5636 fstate.fontsize <- getsize fstate.fontsize attrs;
5637 if closed
5638 then v
5639 else { v with f = uifont (Buffer.create 10) }
5641 | Vopen ("doc", attrs, closed) ->
5642 let pathent, spage, srely, span = doc_of attrs in
5643 let path = unent pathent
5644 and pageno = fromstring int_of_string spos "page" spage 0
5645 and rely = fromstring float_of_string spos "rely" srely 0.0
5646 and pan = fromstring int_of_string spos "pan" span 0 in
5647 let c = config_of dc attrs in
5648 let anchor = (pageno, rely) in
5649 if closed
5650 then (Hashtbl.add h path (c, [], pan, anchor); v)
5651 else { v with f = doc path pan anchor c [] }
5653 | Vopen _ ->
5654 error "unexpected subelement in llppconfig" s spos
5656 | Vclose "llppconfig" -> { v with f = toplevel }
5657 | Vclose _ -> error "unexpected close in llppconfig" s spos
5659 and defaults v t spos _ =
5660 match t with
5661 | Vdata | Vcdata -> v
5662 | Vend -> error "unexpected end of input in defaults" s spos
5663 | Vopen ("keymap", attrs, closed) ->
5664 let modename =
5665 try List.assoc "mode" attrs
5666 with Not_found -> "global" in
5667 if closed
5668 then v
5669 else
5670 let ret keymap =
5671 let h = findkeyhash dc modename in
5672 KeyMap.iter (Hashtbl.replace h) keymap;
5673 defaults
5675 { v with f = pkeymap ret KeyMap.empty }
5677 | Vopen (_, _, _) ->
5678 error "unexpected subelement in defaults" s spos
5680 | Vclose "defaults" ->
5681 { v with f = llppconfig }
5683 | Vclose _ -> error "unexpected close in defaults" s spos
5685 and uifont b v t spos epos =
5686 match t with
5687 | Vdata | Vcdata ->
5688 Buffer.add_substring b s spos (epos - spos);
5690 | Vopen (_, _, _) ->
5691 error "unexpected subelement in ui-font" s spos
5692 | Vclose "ui-font" ->
5693 if String.length !fontpath = 0
5694 then fontpath := Buffer.contents b;
5695 { v with f = llppconfig }
5696 | Vclose _ -> error "unexpected close in ui-font" s spos
5697 | Vend -> error "unexpected end of input in ui-font" s spos
5699 and doc path pan anchor c bookmarks v t spos _ =
5700 match t with
5701 | Vdata | Vcdata -> v
5702 | Vend -> error "unexpected end of input in doc" s spos
5703 | Vopen ("bookmarks", _, closed) ->
5704 if closed
5705 then v
5706 else { v with f = pbookmarks path pan anchor c bookmarks }
5708 | Vopen ("keymap", attrs, closed) ->
5709 let modename =
5710 try List.assoc "mode" attrs
5711 with Not_found -> "global"
5713 if closed
5714 then v
5715 else
5716 let ret keymap =
5717 let h = findkeyhash c modename in
5718 KeyMap.iter (Hashtbl.replace h) keymap;
5719 doc path pan anchor c bookmarks
5721 { v with f = pkeymap ret KeyMap.empty }
5723 | Vopen (_, _, _) ->
5724 error "unexpected subelement in doc" s spos
5726 | Vclose "doc" ->
5727 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5728 { v with f = llppconfig }
5730 | Vclose _ -> error "unexpected close in doc" s spos
5732 and pkeymap ret keymap v t spos _ =
5733 match t with
5734 | Vdata | Vcdata -> v
5735 | Vend -> error "unexpected end of input in keymap" s spos
5736 | Vopen ("map", attrs, closed) ->
5737 let r, l = map_of attrs in
5738 let kss = fromstring keys_of_string spos "in" r [] in
5739 let lss = fromstring keys_of_string spos "out" l [] in
5740 let keymap =
5741 match kss with
5742 | [] -> keymap
5743 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5744 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5746 if closed
5747 then { v with f = pkeymap ret keymap }
5748 else
5749 let f () = v in
5750 { v with f = skip "map" f }
5752 | Vopen _ ->
5753 error "unexpected subelement in keymap" s spos
5755 | Vclose "keymap" ->
5756 { v with f = ret keymap }
5758 | Vclose _ -> error "unexpected close in keymap" s spos
5760 and pbookmarks path pan anchor c bookmarks v t spos _ =
5761 match t with
5762 | Vdata | Vcdata -> v
5763 | Vend -> error "unexpected end of input in bookmarks" s spos
5764 | Vopen ("item", attrs, closed) ->
5765 let titleent, spage, srely = bookmark_of attrs in
5766 let page = fromstring int_of_string spos "page" spage 0
5767 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5768 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5769 if closed
5770 then { v with f = pbookmarks path pan anchor c bookmarks }
5771 else
5772 let f () = v in
5773 { v with f = skip "item" f }
5775 | Vopen _ ->
5776 error "unexpected subelement in bookmarks" s spos
5778 | Vclose "bookmarks" ->
5779 { v with f = doc path pan anchor c bookmarks }
5781 | Vclose _ -> error "unexpected close in bookmarks" s spos
5783 and skip tag f v t spos _ =
5784 match t with
5785 | Vdata | Vcdata -> v
5786 | Vend ->
5787 error ("unexpected end of input in skipped " ^ tag) s spos
5788 | Vopen (tag', _, closed) ->
5789 if closed
5790 then v
5791 else
5792 let f' () = { v with f = skip tag f } in
5793 { v with f = skip tag' f' }
5794 | Vclose ctag ->
5795 if tag = ctag
5796 then f ()
5797 else error ("unexpected close in skipped " ^ tag) s spos
5800 parse { f = toplevel; accu = () } s;
5801 h, dc;
5804 let do_load f ic =
5806 let len = in_channel_length ic in
5807 let s = String.create len in
5808 really_input ic s 0 len;
5809 f s;
5810 with
5811 | Parse_error (msg, s, pos) ->
5812 let subs = subs s pos in
5813 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5814 failwith ("parse error: " ^ s)
5816 | exn ->
5817 failwith ("config load error: " ^ Printexc.to_string exn)
5820 let defconfpath =
5821 let dir =
5823 let dir = Filename.concat home ".config" in
5824 if Sys.is_directory dir then dir else home
5825 with _ -> home
5827 Filename.concat dir "llpp.conf"
5830 let confpath = ref defconfpath;;
5832 let load1 f =
5833 if Sys.file_exists !confpath
5834 then
5835 match
5836 (try Some (open_in_bin !confpath)
5837 with exn ->
5838 prerr_endline
5839 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5840 Printexc.to_string exn);
5841 None
5843 with
5844 | Some ic ->
5845 begin try
5846 f (do_load get ic)
5847 with exn ->
5848 prerr_endline
5849 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5850 Printexc.to_string exn);
5851 end;
5852 close_in ic;
5854 | None -> ()
5855 else
5856 f (Hashtbl.create 0, defconf)
5859 let load () =
5860 let f (h, dc) =
5861 let pc, pb, px, pa =
5863 Hashtbl.find h (Filename.basename state.path)
5864 with Not_found -> dc, [], 0, (0, 0.0)
5866 setconf defconf dc;
5867 setconf conf pc;
5868 state.bookmarks <- pb;
5869 state.x <- px;
5870 state.scrollw <- conf.scrollbw;
5871 if conf.jumpback
5872 then state.anchor <- pa;
5873 cbput state.hists.nav pa;
5875 load1 f
5878 let add_attrs bb always dc c =
5879 let ob s a b =
5880 if always || a != b
5881 then Printf.bprintf bb "\n %s='%b'" s a
5882 and oi s a b =
5883 if always || a != b
5884 then Printf.bprintf bb "\n %s='%d'" s a
5885 and oI s a b =
5886 if always || a != b
5887 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5888 and oz s a b =
5889 if always || a <> b
5890 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5891 and oF s a b =
5892 if always || a <> b
5893 then Printf.bprintf bb "\n %s='%f'" s a
5894 and oc s a b =
5895 if always || a <> b
5896 then
5897 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5898 and oC s a b =
5899 if always || a <> b
5900 then
5901 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5902 and oR s a b =
5903 if always || a <> b
5904 then
5905 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5906 and os s a b =
5907 if always || a <> b
5908 then
5909 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5910 and og s a b =
5911 if always || a <> b
5912 then
5913 match a with
5914 | None -> ()
5915 | Some (_N, _A, _B) ->
5916 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5917 and oW s a b =
5918 if always || a <> b
5919 then
5920 let v =
5921 match a with
5922 | None -> "false"
5923 | Some f ->
5924 if f = infinity
5925 then "true"
5926 else string_of_float f
5928 Printf.bprintf bb "\n %s='%s'" s v
5929 and oco s a b =
5930 if always || a <> b
5931 then
5932 match a with
5933 | Some ((n, a, b), _) when n > 1 ->
5934 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5935 | _ -> ()
5936 and obeco s a b =
5937 if always || a <> b
5938 then
5939 match a with
5940 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5941 | _ -> ()
5943 let w, h =
5944 if always
5945 then dc.winw, dc.winh
5946 else
5947 match state.fullscreen with
5948 | Some wh -> wh
5949 | None -> c.winw, c.winh
5951 let zoom, presentation, interpagespace, maxwait =
5952 if always
5953 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5954 else
5955 match state.mode with
5956 | Birdseye (bc, _, _, _, _) ->
5957 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5958 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5960 oi "width" w dc.winw;
5961 oi "height" h dc.winh;
5962 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5963 oi "scroll-handle-height" c.scrollh dc.scrollh;
5964 ob "case-insensitive-search" c.icase dc.icase;
5965 ob "preload" c.preload dc.preload;
5966 oi "page-bias" c.pagebias dc.pagebias;
5967 oi "scroll-step" c.scrollstep dc.scrollstep;
5968 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5969 ob "max-height-fit" c.maxhfit dc.maxhfit;
5970 ob "crop-hack" c.crophack dc.crophack;
5971 oW "throttle" maxwait dc.maxwait;
5972 ob "highlight-links" c.hlinks dc.hlinks;
5973 ob "under-cursor-info" c.underinfo dc.underinfo;
5974 oi "vertical-margin" interpagespace dc.interpagespace;
5975 oz "zoom" zoom dc.zoom;
5976 ob "presentation" presentation dc.presentation;
5977 oi "rotation-angle" c.angle dc.angle;
5978 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5979 ob "proportional-display" c.proportional dc.proportional;
5980 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5981 oi "tex-count" c.texcount dc.texcount;
5982 oi "slice-height" c.sliceheight dc.sliceheight;
5983 oi "thumbnail-width" c.thumbw dc.thumbw;
5984 ob "persistent-location" c.jumpback dc.jumpback;
5985 oc "background-color" c.bgcolor dc.bgcolor;
5986 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5987 oi "tile-width" c.tilew dc.tilew;
5988 oi "tile-height" c.tileh dc.tileh;
5989 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5990 ob "checkers" c.checkers dc.checkers;
5991 oi "aalevel" c.aalevel dc.aalevel;
5992 ob "trim-margins" c.trimmargins dc.trimmargins;
5993 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5994 os "uri-launcher" c.urilauncher dc.urilauncher;
5995 os "path-launcher" c.pathlauncher dc.pathlauncher;
5996 oC "color-space" c.colorspace dc.colorspace;
5997 ob "invert-colors" c.invert dc.invert;
5998 oF "brightness" c.colorscale dc.colorscale;
5999 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6000 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6001 oco "columns" c.columns dc.columns;
6002 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6003 os "selection-command" c.selcmd dc.selcmd;
6004 ob "update-cursor" c.updatecurs dc.updatecurs;
6007 let keymapsbuf always dc c =
6008 let bb = Buffer.create 16 in
6009 let rec loop = function
6010 | [] -> ()
6011 | (modename, h) :: rest ->
6012 let dh = findkeyhash dc modename in
6013 if always || h <> dh
6014 then (
6015 if Hashtbl.length h > 0
6016 then (
6017 if Buffer.length bb > 0
6018 then Buffer.add_char bb '\n';
6019 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6020 Hashtbl.iter (fun i o ->
6021 let isdifferent = always ||
6023 let dO = Hashtbl.find dh i in
6024 dO <> o
6025 with Not_found -> true
6027 if isdifferent
6028 then
6029 let addkm (k, m) =
6030 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6031 if Wsi.withalt m then Buffer.add_string bb "alt-";
6032 if Wsi.withshift m then Buffer.add_string bb "shift-";
6033 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6034 Buffer.add_string bb (Wsi.keyname k);
6036 let addkms l =
6037 let rec loop = function
6038 | [] -> ()
6039 | km :: [] -> addkm km
6040 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6042 loop l
6044 Buffer.add_string bb "<map in='";
6045 addkm i;
6046 match o with
6047 | KMinsrt km ->
6048 Buffer.add_char bb '\'';
6049 Buffer.add_string bb " out='";
6050 addkm km;
6051 Buffer.add_string bb "'/>\n"
6053 | KMinsrl kms ->
6054 Buffer.add_char bb '\'';
6055 Buffer.add_string bb " out='";
6056 addkms kms;
6057 Buffer.add_string bb "'/>\n"
6059 | KMmulti (ins, kms) ->
6060 Buffer.add_char bb ' ';
6061 addkms ins;
6062 Buffer.add_char bb '\'';
6063 Buffer.add_string bb " out='";
6064 addkms kms;
6065 Buffer.add_string bb "'/>\n"
6066 ) h;
6067 Buffer.add_string bb "</keymap>";
6070 loop rest
6072 loop c.keyhashes;
6076 let save () =
6077 let uifontsize = fstate.fontsize in
6078 let bb = Buffer.create 32768 in
6079 let f (h, dc) =
6080 let dc = if conf.bedefault then conf else dc in
6081 Buffer.add_string bb "<llppconfig>\n";
6083 if String.length !fontpath > 0
6084 then
6085 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6086 uifontsize
6087 !fontpath
6088 else (
6089 if uifontsize <> 14
6090 then
6091 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6094 Buffer.add_string bb "<defaults ";
6095 add_attrs bb true dc dc;
6096 let kb = keymapsbuf true dc dc in
6097 if Buffer.length kb > 0
6098 then (
6099 Buffer.add_string bb ">\n";
6100 Buffer.add_buffer bb kb;
6101 Buffer.add_string bb "\n</defaults>\n";
6103 else Buffer.add_string bb "/>\n";
6105 let adddoc path pan anchor c bookmarks =
6106 if bookmarks == [] && c = dc && anchor = emptyanchor
6107 then ()
6108 else (
6109 Printf.bprintf bb "<doc path='%s'"
6110 (enent path 0 (String.length path));
6112 if anchor <> emptyanchor
6113 then (
6114 let n, y = anchor in
6115 Printf.bprintf bb " page='%d'" n;
6116 if y > 1e-6
6117 then
6118 Printf.bprintf bb " rely='%f'" y
6122 if pan != 0
6123 then Printf.bprintf bb " pan='%d'" pan;
6125 add_attrs bb false dc c;
6126 let kb = keymapsbuf false dc c in
6128 begin match bookmarks with
6129 | [] ->
6130 if Buffer.length kb > 0
6131 then (
6132 Buffer.add_string bb ">\n";
6133 Buffer.add_buffer bb kb;
6134 Buffer.add_string bb "</doc>\n";
6136 else Buffer.add_string bb "/>\n"
6137 | _ ->
6138 Buffer.add_string bb ">\n<bookmarks>\n";
6139 List.iter (fun (title, _level, (page, rely)) ->
6140 Printf.bprintf bb
6141 "<item title='%s' page='%d'"
6142 (enent title 0 (String.length title))
6143 page
6145 if rely > 1e-6
6146 then
6147 Printf.bprintf bb " rely='%f'" rely
6149 Buffer.add_string bb "/>\n";
6150 ) bookmarks;
6151 Buffer.add_string bb "</bookmarks>";
6152 if Buffer.length kb > 0
6153 then (
6154 Buffer.add_string bb "\n";
6155 Buffer.add_buffer bb kb;
6157 Buffer.add_string bb "\n</doc>\n";
6158 end;
6162 let pan, conf =
6163 match state.mode with
6164 | Birdseye (c, pan, _, _, _) ->
6165 let beyecolumns =
6166 match conf.columns with
6167 | Some ((c, _, _), _) -> Some c
6168 | None -> None
6169 and columns =
6170 match c.columns with
6171 | Some (c, _) -> Some (c, [||])
6172 | None -> None
6174 pan, { c with beyecolumns = beyecolumns; columns = columns }
6175 | _ -> state.x, conf
6177 let basename = Filename.basename state.path in
6178 adddoc basename pan (getanchor ())
6179 { conf with
6180 autoscrollstep =
6181 match state.autoscroll with
6182 | Some step -> step
6183 | None -> conf.autoscrollstep }
6184 (if conf.savebmarks then state.bookmarks else []);
6186 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6187 if basename <> path
6188 then adddoc path x y c bookmarks
6189 ) h;
6190 Buffer.add_string bb "</llppconfig>";
6192 load1 f;
6193 if Buffer.length bb > 0
6194 then
6196 let tmp = !confpath ^ ".tmp" in
6197 let oc = open_out_bin tmp in
6198 Buffer.output_buffer oc bb;
6199 close_out oc;
6200 Unix.rename tmp !confpath;
6201 with exn ->
6202 prerr_endline
6203 ("error while saving configuration: " ^ Printexc.to_string exn)
6205 end;;
6207 let () =
6208 Arg.parse
6209 (Arg.align
6210 [("-p", Arg.String (fun s -> state.password <- s) ,
6211 "<password> Set password");
6213 ("-f", Arg.String (fun s -> Config.fontpath := s),
6214 "<path> Set path to the user interface font");
6216 ("-c", Arg.String (fun s -> Config.confpath := s),
6217 "<path> Set path to the configuration file");
6219 ("-v", Arg.Unit (fun () ->
6220 Printf.printf
6221 "%s\nconfiguration path: %s\n"
6222 (version ())
6223 Config.defconfpath
6225 exit 0), " Print version and exit");
6228 (fun s -> state.path <- s)
6229 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6231 if String.length state.path = 0
6232 then (prerr_endline "file name missing"; exit 1);
6234 Config.load ();
6236 let globalkeyhash = findkeyhash conf "global" in
6237 state.wsfd <- Wsi.init (object
6238 method display = display ()
6239 method reshape w h = reshape w h
6240 method mouse b d x y m = mouse b d x y m
6241 method motion x y = state.mpos <- (x, y); motion x y
6242 method pmotion x y = state.mpos <- (x, y); pmotion x y
6243 method key k m =
6244 match state.keystate with
6245 | KSnone ->
6246 let km = k, m in
6247 begin
6248 match
6249 try Hashtbl.find globalkeyhash km
6250 with Not_found ->
6251 let modehash = state.uioh#modehash in
6252 try Hashtbl.find modehash km
6253 with Not_found -> KMinsrt (k, m)
6254 with
6255 | KMinsrt (k, m) -> keyboard k m
6256 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6257 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6259 | KSinto ((k', m') :: [], insrt) when k'=k && m' land m = m' ->
6260 List.iter (fun (k, m) -> keyboard k m) insrt;
6261 state.keystate <- KSnone
6262 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land m = m' ->
6263 state.keystate <- KSinto (keys, insrt)
6264 | _ ->
6265 state.keystate <- KSnone
6267 method enter x y = state.mpos <- (x, y); pmotion x y
6268 method leave = state.mpos <- (-1, -1)
6269 end) conf.winw conf.winh;
6271 if not (
6272 List.exists GlMisc.check_extension
6273 [ "GL_ARB_texture_rectangle"
6274 ; "GL_EXT_texture_recangle"
6275 ; "GL_NV_texture_rectangle" ]
6277 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6279 let cr, sw = Unix.pipe ()
6280 and sr, cw = Unix.pipe () in
6282 cloexec cr;
6283 cloexec sw;
6284 cloexec sr;
6285 cloexec cw;
6287 setcheckers conf.checkers;
6288 redirectstderr ();
6290 init (cr, cw) (
6291 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6292 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6293 !Config.fontpath
6295 state.sr <- sr;
6296 state.sw <- sw;
6297 state.text <- "Opening " ^ state.path;
6298 setaalevel conf.aalevel;
6299 writeopen state.path state.password;
6300 state.uioh <- uioh;
6301 setfontsize fstate.fontsize;
6302 doreshape conf.winw conf.winh;
6304 let rec loop deadline =
6305 let r =
6306 match state.errfd with
6307 | None -> [state.sr; state.wsfd]
6308 | Some fd -> [state.sr; state.wsfd; fd]
6310 if state.redisplay
6311 then (
6312 state.redisplay <- false;
6313 display ();
6315 let timeout =
6316 let now = now () in
6317 if deadline > now
6318 then (
6319 if deadline = infinity
6320 then ~-.1.0
6321 else max 0.0 (deadline -. now)
6323 else 0.0
6325 let r, _, _ =
6326 try Unix.select r [] [] timeout
6327 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6329 begin match r with
6330 | [] ->
6331 state.ghyll None;
6332 let newdeadline =
6333 if state.ghyll == noghyll
6334 then
6335 match state.autoscroll with
6336 | Some step when step != 0 ->
6337 let y = state.y + step in
6338 let y =
6339 if y < 0
6340 then state.maxy
6341 else if y >= state.maxy then 0 else y
6343 gotoy y;
6344 if state.mode = View
6345 then state.text <- "";
6346 deadline +. 0.01
6347 | _ -> infinity
6348 else deadline +. 0.01
6350 loop newdeadline
6352 | l ->
6353 let rec checkfds = function
6354 | [] -> ()
6355 | fd :: rest when fd = state.sr ->
6356 let cmd = readcmd state.sr in
6357 act cmd;
6358 checkfds rest
6360 | fd :: rest when fd = state.wsfd ->
6361 Wsi.readresp fd;
6362 checkfds rest
6364 | fd :: rest ->
6365 let s = String.create 80 in
6366 let n = Unix.read fd s 0 80 in
6367 if conf.redirectstderr
6368 then (
6369 Buffer.add_substring state.errmsgs s 0 n;
6370 state.newerrmsgs <- true;
6371 state.redisplay <- true;
6373 else (
6374 prerr_string (String.sub s 0 n);
6375 flush stderr;
6377 checkfds rest
6379 checkfds l;
6380 let newdeadline =
6381 let deadline1 =
6382 if deadline = infinity
6383 then now () +. 0.01
6384 else deadline
6386 match state.autoscroll with
6387 | Some step when step != 0 -> deadline1
6388 | _ -> if state.ghyll == noghyll then infinity else deadline1
6390 loop newdeadline
6391 end;
6394 loop infinity;
6395 with Wsi.Quit ->
6396 wcmd "quit";
6397 Config.save ();
6398 exit 0;